Fedora kernel-2.6.17-1.2142_FC4 patched with stable patch-2.6.17.4-vs2.0.2-rc26.diff
[linux-2.6.git] / fs / xfs / quota / xfs_trans_dquot.c
1 /*
2  * Copyright (c) 2000-2002 Silicon Graphics, Inc.
3  * All Rights Reserved.
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License as
7  * published by the Free Software Foundation.
8  *
9  * This program is distributed in the hope that it would be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write the Free Software Foundation,
16  * Inc.,  51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17  */
18 #include "xfs.h"
19 #include "xfs_fs.h"
20 #include "xfs_bit.h"
21 #include "xfs_log.h"
22 #include "xfs_inum.h"
23 #include "xfs_trans.h"
24 #include "xfs_sb.h"
25 #include "xfs_ag.h"
26 #include "xfs_dir.h"
27 #include "xfs_dir2.h"
28 #include "xfs_alloc.h"
29 #include "xfs_dmapi.h"
30 #include "xfs_quota.h"
31 #include "xfs_mount.h"
32 #include "xfs_bmap_btree.h"
33 #include "xfs_alloc_btree.h"
34 #include "xfs_ialloc_btree.h"
35 #include "xfs_attr_sf.h"
36 #include "xfs_dir_sf.h"
37 #include "xfs_dir2_sf.h"
38 #include "xfs_dinode.h"
39 #include "xfs_inode.h"
40 #include "xfs_ialloc.h"
41 #include "xfs_itable.h"
42 #include "xfs_btree.h"
43 #include "xfs_bmap.h"
44 #include "xfs_rtalloc.h"
45 #include "xfs_error.h"
46 #include "xfs_rw.h"
47 #include "xfs_acl.h"
48 #include "xfs_cap.h"
49 #include "xfs_mac.h"
50 #include "xfs_attr.h"
51 #include "xfs_buf_item.h"
52 #include "xfs_trans_priv.h"
53 #include "xfs_qm.h"
54
55 STATIC void     xfs_trans_alloc_dqinfo(xfs_trans_t *);
56
57 /*
58  * Add the locked dquot to the transaction.
59  * The dquot must be locked, and it cannot be associated with any
60  * transaction.
61  */
62 void
63 xfs_trans_dqjoin(
64         xfs_trans_t     *tp,
65         xfs_dquot_t     *dqp)
66 {
67         xfs_dq_logitem_t    *lp;
68
69         ASSERT(! XFS_DQ_IS_ADDEDTO_TRX(tp, dqp));
70         ASSERT(XFS_DQ_IS_LOCKED(dqp));
71         ASSERT(XFS_DQ_IS_LOGITEM_INITD(dqp));
72         lp = &dqp->q_logitem;
73
74         /*
75          * Get a log_item_desc to point at the new item.
76          */
77         (void) xfs_trans_add_item(tp, (xfs_log_item_t*)(lp));
78
79         /*
80          * Initialize i_transp so we can later determine if this dquot is
81          * associated with this transaction.
82          */
83         dqp->q_transp = tp;
84 }
85
86
87 /*
88  * This is called to mark the dquot as needing
89  * to be logged when the transaction is committed.  The dquot must
90  * already be associated with the given transaction.
91  * Note that it marks the entire transaction as dirty. In the ordinary
92  * case, this gets called via xfs_trans_commit, after the transaction
93  * is already dirty. However, there's nothing stop this from getting
94  * called directly, as done by xfs_qm_scall_setqlim. Hence, the TRANS_DIRTY
95  * flag.
96  */
97 void
98 xfs_trans_log_dquot(
99         xfs_trans_t     *tp,
100         xfs_dquot_t     *dqp)
101 {
102         xfs_log_item_desc_t     *lidp;
103
104         ASSERT(XFS_DQ_IS_ADDEDTO_TRX(tp, dqp));
105         ASSERT(XFS_DQ_IS_LOCKED(dqp));
106
107         lidp = xfs_trans_find_item(tp, (xfs_log_item_t*)(&dqp->q_logitem));
108         ASSERT(lidp != NULL);
109
110         tp->t_flags |= XFS_TRANS_DIRTY;
111         lidp->lid_flags |= XFS_LID_DIRTY;
112 }
113
114 /*
115  * Carry forward whatever is left of the quota blk reservation to
116  * the spanky new transaction
117  */
118 STATIC void
119 xfs_trans_dup_dqinfo(
120         xfs_trans_t     *otp,
121         xfs_trans_t     *ntp)
122 {
123         xfs_dqtrx_t     *oq, *nq;
124         int             i,j;
125         xfs_dqtrx_t     *oqa, *nqa;
126
127         if (!otp->t_dqinfo)
128                 return;
129
130         xfs_trans_alloc_dqinfo(ntp);
131         oqa = otp->t_dqinfo->dqa_usrdquots;
132         nqa = ntp->t_dqinfo->dqa_usrdquots;
133
134         /*
135          * Because the quota blk reservation is carried forward,
136          * it is also necessary to carry forward the DQ_DIRTY flag.
137          */
138         if(otp->t_flags & XFS_TRANS_DQ_DIRTY)
139                 ntp->t_flags |= XFS_TRANS_DQ_DIRTY;
140
141         for (j = 0; j < 2; j++) {
142                 for (i = 0; i < XFS_QM_TRANS_MAXDQS; i++) {
143                         if (oqa[i].qt_dquot == NULL)
144                                 break;
145                         oq = &oqa[i];
146                         nq = &nqa[i];
147
148                         nq->qt_dquot = oq->qt_dquot;
149                         nq->qt_bcount_delta = nq->qt_icount_delta = 0;
150                         nq->qt_rtbcount_delta = 0;
151
152                         /*
153                          * Transfer whatever is left of the reservations.
154                          */
155                         nq->qt_blk_res = oq->qt_blk_res - oq->qt_blk_res_used;
156                         oq->qt_blk_res = oq->qt_blk_res_used;
157
158                         nq->qt_rtblk_res = oq->qt_rtblk_res -
159                                 oq->qt_rtblk_res_used;
160                         oq->qt_rtblk_res = oq->qt_rtblk_res_used;
161
162                         nq->qt_ino_res = oq->qt_ino_res - oq->qt_ino_res_used;
163                         oq->qt_ino_res = oq->qt_ino_res_used;
164
165                 }
166                 oqa = otp->t_dqinfo->dqa_grpdquots;
167                 nqa = ntp->t_dqinfo->dqa_grpdquots;
168         }
169 }
170
171 /*
172  * Wrap around mod_dquot to account for both user and group quotas.
173  */
174 STATIC void
175 xfs_trans_mod_dquot_byino(
176         xfs_trans_t     *tp,
177         xfs_inode_t     *ip,
178         uint            field,
179         long            delta)
180 {
181         xfs_mount_t     *mp;
182
183         ASSERT(tp);
184         mp = tp->t_mountp;
185
186         if (!XFS_IS_QUOTA_ON(mp) ||
187             ip->i_ino == mp->m_sb.sb_uquotino ||
188             ip->i_ino == mp->m_sb.sb_gquotino)
189                 return;
190
191         if (tp->t_dqinfo == NULL)
192                 xfs_trans_alloc_dqinfo(tp);
193
194         if (XFS_IS_UQUOTA_ON(mp) && ip->i_udquot)
195                 (void) xfs_trans_mod_dquot(tp, ip->i_udquot, field, delta);
196         if (XFS_IS_OQUOTA_ON(mp) && ip->i_gdquot)
197                 (void) xfs_trans_mod_dquot(tp, ip->i_gdquot, field, delta);
198 }
199
200 STATIC xfs_dqtrx_t *
201 xfs_trans_get_dqtrx(
202         xfs_trans_t     *tp,
203         xfs_dquot_t     *dqp)
204 {
205         int             i;
206         xfs_dqtrx_t     *qa;
207
208         for (i = 0; i < XFS_QM_TRANS_MAXDQS; i++) {
209                 qa = XFS_QM_DQP_TO_DQACCT(tp, dqp);
210
211                 if (qa[i].qt_dquot == NULL ||
212                     qa[i].qt_dquot == dqp) {
213                         return (&qa[i]);
214                 }
215         }
216
217         return (NULL);
218 }
219
220 /*
221  * Make the changes in the transaction structure.
222  * The moral equivalent to xfs_trans_mod_sb().
223  * We don't touch any fields in the dquot, so we don't care
224  * if it's locked or not (most of the time it won't be).
225  */
226 void
227 xfs_trans_mod_dquot(
228         xfs_trans_t     *tp,
229         xfs_dquot_t     *dqp,
230         uint            field,
231         long            delta)
232 {
233         xfs_dqtrx_t     *qtrx;
234
235         ASSERT(tp);
236         qtrx = NULL;
237
238         if (tp->t_dqinfo == NULL)
239                 xfs_trans_alloc_dqinfo(tp);
240         /*
241          * Find either the first free slot or the slot that belongs
242          * to this dquot.
243          */
244         qtrx = xfs_trans_get_dqtrx(tp, dqp);
245         ASSERT(qtrx);
246         if (qtrx->qt_dquot == NULL)
247                 qtrx->qt_dquot = dqp;
248
249         switch (field) {
250
251                 /*
252                  * regular disk blk reservation
253                  */
254               case XFS_TRANS_DQ_RES_BLKS:
255                 qtrx->qt_blk_res += (ulong)delta;
256                 break;
257
258                 /*
259                  * inode reservation
260                  */
261               case XFS_TRANS_DQ_RES_INOS:
262                 qtrx->qt_ino_res += (ulong)delta;
263                 break;
264
265                 /*
266                  * disk blocks used.
267                  */
268               case XFS_TRANS_DQ_BCOUNT:
269                 if (qtrx->qt_blk_res && delta > 0) {
270                         qtrx->qt_blk_res_used += (ulong)delta;
271                         ASSERT(qtrx->qt_blk_res >= qtrx->qt_blk_res_used);
272                 }
273                 qtrx->qt_bcount_delta += delta;
274                 break;
275
276               case XFS_TRANS_DQ_DELBCOUNT:
277                 qtrx->qt_delbcnt_delta += delta;
278                 break;
279
280                 /*
281                  * Inode Count
282                  */
283               case XFS_TRANS_DQ_ICOUNT:
284                 if (qtrx->qt_ino_res && delta > 0) {
285                         qtrx->qt_ino_res_used += (ulong)delta;
286                         ASSERT(qtrx->qt_ino_res >= qtrx->qt_ino_res_used);
287                 }
288                 qtrx->qt_icount_delta += delta;
289                 break;
290
291                 /*
292                  * rtblk reservation
293                  */
294               case XFS_TRANS_DQ_RES_RTBLKS:
295                 qtrx->qt_rtblk_res += (ulong)delta;
296                 break;
297
298                 /*
299                  * rtblk count
300                  */
301               case XFS_TRANS_DQ_RTBCOUNT:
302                 if (qtrx->qt_rtblk_res && delta > 0) {
303                         qtrx->qt_rtblk_res_used += (ulong)delta;
304                         ASSERT(qtrx->qt_rtblk_res >= qtrx->qt_rtblk_res_used);
305                 }
306                 qtrx->qt_rtbcount_delta += delta;
307                 break;
308
309               case XFS_TRANS_DQ_DELRTBCOUNT:
310                 qtrx->qt_delrtb_delta += delta;
311                 break;
312
313               default:
314                 ASSERT(0);
315         }
316         tp->t_flags |= XFS_TRANS_DQ_DIRTY;
317 }
318
319
320 /*
321  * Given an array of dqtrx structures, lock all the dquots associated
322  * and join them to the transaction, provided they have been modified.
323  * We know that the highest number of dquots (of one type - usr OR grp),
324  * involved in a transaction is 2 and that both usr and grp combined - 3.
325  * So, we don't attempt to make this very generic.
326  */
327 STATIC void
328 xfs_trans_dqlockedjoin(
329         xfs_trans_t     *tp,
330         xfs_dqtrx_t     *q)
331 {
332         ASSERT(q[0].qt_dquot != NULL);
333         if (q[1].qt_dquot == NULL) {
334                 xfs_dqlock(q[0].qt_dquot);
335                 xfs_trans_dqjoin(tp, q[0].qt_dquot);
336         } else {
337                 ASSERT(XFS_QM_TRANS_MAXDQS == 2);
338                 xfs_dqlock2(q[0].qt_dquot, q[1].qt_dquot);
339                 xfs_trans_dqjoin(tp, q[0].qt_dquot);
340                 xfs_trans_dqjoin(tp, q[1].qt_dquot);
341         }
342 }
343
344
345 /*
346  * Called by xfs_trans_commit() and similar in spirit to
347  * xfs_trans_apply_sb_deltas().
348  * Go thru all the dquots belonging to this transaction and modify the
349  * INCORE dquot to reflect the actual usages.
350  * Unreserve just the reservations done by this transaction.
351  * dquot is still left locked at exit.
352  */
353 STATIC void
354 xfs_trans_apply_dquot_deltas(
355         xfs_trans_t             *tp)
356 {
357         int                     i, j;
358         xfs_dquot_t             *dqp;
359         xfs_dqtrx_t             *qtrx, *qa;
360         xfs_disk_dquot_t        *d;
361         long                    totalbdelta;
362         long                    totalrtbdelta;
363
364         if (! (tp->t_flags & XFS_TRANS_DQ_DIRTY))
365                 return;
366
367         ASSERT(tp->t_dqinfo);
368         qa = tp->t_dqinfo->dqa_usrdquots;
369         for (j = 0; j < 2; j++) {
370                 if (qa[0].qt_dquot == NULL) {
371                         qa = tp->t_dqinfo->dqa_grpdquots;
372                         continue;
373                 }
374
375                 /*
376                  * Lock all of the dquots and join them to the transaction.
377                  */
378                 xfs_trans_dqlockedjoin(tp, qa);
379
380                 for (i = 0; i < XFS_QM_TRANS_MAXDQS; i++) {
381                         qtrx = &qa[i];
382                         /*
383                          * The array of dquots is filled
384                          * sequentially, not sparsely.
385                          */
386                         if ((dqp = qtrx->qt_dquot) == NULL)
387                                 break;
388
389                         ASSERT(XFS_DQ_IS_LOCKED(dqp));
390                         ASSERT(XFS_DQ_IS_ADDEDTO_TRX(tp, dqp));
391
392                         /*
393                          * adjust the actual number of blocks used
394                          */
395                         d = &dqp->q_core;
396
397                         /*
398                          * The issue here is - sometimes we don't make a blkquota
399                          * reservation intentionally to be fair to users
400                          * (when the amount is small). On the other hand,
401                          * delayed allocs do make reservations, but that's
402                          * outside of a transaction, so we have no
403                          * idea how much was really reserved.
404                          * So, here we've accumulated delayed allocation blks and
405                          * non-delay blks. The assumption is that the
406                          * delayed ones are always reserved (outside of a
407                          * transaction), and the others may or may not have
408                          * quota reservations.
409                          */
410                         totalbdelta = qtrx->qt_bcount_delta +
411                                 qtrx->qt_delbcnt_delta;
412                         totalrtbdelta = qtrx->qt_rtbcount_delta +
413                                 qtrx->qt_delrtb_delta;
414 #ifdef QUOTADEBUG
415                         if (totalbdelta < 0)
416                                 ASSERT(be64_to_cpu(d->d_bcount) >=
417                                        (xfs_qcnt_t) -totalbdelta);
418
419                         if (totalrtbdelta < 0)
420                                 ASSERT(be64_to_cpu(d->d_rtbcount) >=
421                                        (xfs_qcnt_t) -totalrtbdelta);
422
423                         if (qtrx->qt_icount_delta < 0)
424                                 ASSERT(be64_to_cpu(d->d_icount) >=
425                                        (xfs_qcnt_t) -qtrx->qt_icount_delta);
426 #endif
427                         if (totalbdelta)
428                                 be64_add(&d->d_bcount, (xfs_qcnt_t)totalbdelta);
429
430                         if (qtrx->qt_icount_delta)
431                                 be64_add(&d->d_icount, (xfs_qcnt_t)qtrx->qt_icount_delta);
432
433                         if (totalrtbdelta)
434                                 be64_add(&d->d_rtbcount, (xfs_qcnt_t)totalrtbdelta);
435
436                         /*
437                          * Get any default limits in use.
438                          * Start/reset the timer(s) if needed.
439                          */
440                         if (d->d_id) {
441                                 xfs_qm_adjust_dqlimits(tp->t_mountp, d);
442                                 xfs_qm_adjust_dqtimers(tp->t_mountp, d);
443                         }
444
445                         dqp->dq_flags |= XFS_DQ_DIRTY;
446                         /*
447                          * add this to the list of items to get logged
448                          */
449                         xfs_trans_log_dquot(tp, dqp);
450                         /*
451                          * Take off what's left of the original reservation.
452                          * In case of delayed allocations, there's no
453                          * reservation that a transaction structure knows of.
454                          */
455                         if (qtrx->qt_blk_res != 0) {
456                                 if (qtrx->qt_blk_res != qtrx->qt_blk_res_used) {
457                                         if (qtrx->qt_blk_res >
458                                             qtrx->qt_blk_res_used)
459                                                 dqp->q_res_bcount -= (xfs_qcnt_t)
460                                                         (qtrx->qt_blk_res -
461                                                          qtrx->qt_blk_res_used);
462                                         else
463                                                 dqp->q_res_bcount -= (xfs_qcnt_t)
464                                                         (qtrx->qt_blk_res_used -
465                                                          qtrx->qt_blk_res);
466                                 }
467                         } else {
468                                 /*
469                                  * These blks were never reserved, either inside
470                                  * a transaction or outside one (in a delayed
471                                  * allocation). Also, this isn't always a
472                                  * negative number since we sometimes
473                                  * deliberately skip quota reservations.
474                                  */
475                                 if (qtrx->qt_bcount_delta) {
476                                         dqp->q_res_bcount +=
477                                               (xfs_qcnt_t)qtrx->qt_bcount_delta;
478                                 }
479                         }
480                         /*
481                          * Adjust the RT reservation.
482                          */
483                         if (qtrx->qt_rtblk_res != 0) {
484                                 if (qtrx->qt_rtblk_res != qtrx->qt_rtblk_res_used) {
485                                         if (qtrx->qt_rtblk_res >
486                                             qtrx->qt_rtblk_res_used)
487                                                dqp->q_res_rtbcount -= (xfs_qcnt_t)
488                                                        (qtrx->qt_rtblk_res -
489                                                         qtrx->qt_rtblk_res_used);
490                                         else
491                                                dqp->q_res_rtbcount -= (xfs_qcnt_t)
492                                                        (qtrx->qt_rtblk_res_used -
493                                                         qtrx->qt_rtblk_res);
494                                 }
495                         } else {
496                                 if (qtrx->qt_rtbcount_delta)
497                                         dqp->q_res_rtbcount +=
498                                             (xfs_qcnt_t)qtrx->qt_rtbcount_delta;
499                         }
500
501                         /*
502                          * Adjust the inode reservation.
503                          */
504                         if (qtrx->qt_ino_res != 0) {
505                                 ASSERT(qtrx->qt_ino_res >=
506                                        qtrx->qt_ino_res_used);
507                                 if (qtrx->qt_ino_res > qtrx->qt_ino_res_used)
508                                         dqp->q_res_icount -= (xfs_qcnt_t)
509                                                 (qtrx->qt_ino_res -
510                                                  qtrx->qt_ino_res_used);
511                         } else {
512                                 if (qtrx->qt_icount_delta)
513                                         dqp->q_res_icount +=
514                                             (xfs_qcnt_t)qtrx->qt_icount_delta;
515                         }
516
517                         ASSERT(dqp->q_res_bcount >=
518                                 be64_to_cpu(dqp->q_core.d_bcount));
519                         ASSERT(dqp->q_res_icount >=
520                                 be64_to_cpu(dqp->q_core.d_icount));
521                         ASSERT(dqp->q_res_rtbcount >=
522                                 be64_to_cpu(dqp->q_core.d_rtbcount));
523                 }
524                 /*
525                  * Do the group quotas next
526                  */
527                 qa = tp->t_dqinfo->dqa_grpdquots;
528         }
529 }
530
531 /*
532  * Release the reservations, and adjust the dquots accordingly.
533  * This is called only when the transaction is being aborted. If by
534  * any chance we have done dquot modifications incore (ie. deltas) already,
535  * we simply throw those away, since that's the expected behavior
536  * when a transaction is curtailed without a commit.
537  */
538 STATIC void
539 xfs_trans_unreserve_and_mod_dquots(
540         xfs_trans_t             *tp)
541 {
542         int                     i, j;
543         xfs_dquot_t             *dqp;
544         xfs_dqtrx_t             *qtrx, *qa;
545         boolean_t               locked;
546
547         if (!tp->t_dqinfo || !(tp->t_flags & XFS_TRANS_DQ_DIRTY))
548                 return;
549
550         qa = tp->t_dqinfo->dqa_usrdquots;
551
552         for (j = 0; j < 2; j++) {
553                 for (i = 0; i < XFS_QM_TRANS_MAXDQS; i++) {
554                         qtrx = &qa[i];
555                         /*
556                          * We assume that the array of dquots is filled
557                          * sequentially, not sparsely.
558                          */
559                         if ((dqp = qtrx->qt_dquot) == NULL)
560                                 break;
561                         /*
562                          * Unreserve the original reservation. We don't care
563                          * about the number of blocks used field, or deltas.
564                          * Also we don't bother to zero the fields.
565                          */
566                         locked = B_FALSE;
567                         if (qtrx->qt_blk_res) {
568                                 xfs_dqlock(dqp);
569                                 locked = B_TRUE;
570                                 dqp->q_res_bcount -=
571                                         (xfs_qcnt_t)qtrx->qt_blk_res;
572                         }
573                         if (qtrx->qt_ino_res) {
574                                 if (!locked) {
575                                         xfs_dqlock(dqp);
576                                         locked = B_TRUE;
577                                 }
578                                 dqp->q_res_icount -=
579                                         (xfs_qcnt_t)qtrx->qt_ino_res;
580                         }
581
582                         if (qtrx->qt_rtblk_res) {
583                                 if (!locked) {
584                                         xfs_dqlock(dqp);
585                                         locked = B_TRUE;
586                                 }
587                                 dqp->q_res_rtbcount -=
588                                         (xfs_qcnt_t)qtrx->qt_rtblk_res;
589                         }
590                         if (locked)
591                                 xfs_dqunlock(dqp);
592
593                 }
594                 qa = tp->t_dqinfo->dqa_grpdquots;
595         }
596 }
597
598 STATIC int
599 xfs_quota_error(uint flags)
600 {
601         if (flags & XFS_QMOPT_ENOSPC)
602                 return ENOSPC;
603         return EDQUOT;
604 }
605
606 /*
607  * This reserves disk blocks and inodes against a dquot.
608  * Flags indicate if the dquot is to be locked here and also
609  * if the blk reservation is for RT or regular blocks.
610  * Sending in XFS_QMOPT_FORCE_RES flag skips the quota check.
611  */
612 STATIC int
613 xfs_trans_dqresv(
614         xfs_trans_t     *tp,
615         xfs_mount_t     *mp,
616         xfs_dquot_t     *dqp,
617         long            nblks,
618         long            ninos,
619         uint            flags)
620 {
621         int             error;
622         xfs_qcnt_t      hardlimit;
623         xfs_qcnt_t      softlimit;
624         time_t          timer;
625         xfs_qwarncnt_t  warns;
626         xfs_qwarncnt_t  warnlimit;
627         xfs_qcnt_t      count;
628         xfs_qcnt_t      *resbcountp;
629         xfs_quotainfo_t *q = mp->m_quotainfo;
630
631         if (! (flags & XFS_QMOPT_DQLOCK)) {
632                 xfs_dqlock(dqp);
633         }
634         ASSERT(XFS_DQ_IS_LOCKED(dqp));
635         if (flags & XFS_TRANS_DQ_RES_BLKS) {
636                 hardlimit = be64_to_cpu(dqp->q_core.d_blk_hardlimit);
637                 if (!hardlimit)
638                         hardlimit = q->qi_bhardlimit;
639                 softlimit = be64_to_cpu(dqp->q_core.d_blk_softlimit);
640                 if (!softlimit)
641                         softlimit = q->qi_bsoftlimit;
642                 timer = be32_to_cpu(dqp->q_core.d_btimer);
643                 warns = be16_to_cpu(dqp->q_core.d_bwarns);
644                 warnlimit = XFS_QI_BWARNLIMIT(dqp->q_mount);
645                 resbcountp = &dqp->q_res_bcount;
646         } else {
647                 ASSERT(flags & XFS_TRANS_DQ_RES_RTBLKS);
648                 hardlimit = be64_to_cpu(dqp->q_core.d_rtb_hardlimit);
649                 if (!hardlimit)
650                         hardlimit = q->qi_rtbhardlimit;
651                 softlimit = be64_to_cpu(dqp->q_core.d_rtb_softlimit);
652                 if (!softlimit)
653                         softlimit = q->qi_rtbsoftlimit;
654                 timer = be32_to_cpu(dqp->q_core.d_rtbtimer);
655                 warns = be16_to_cpu(dqp->q_core.d_rtbwarns);
656                 warnlimit = XFS_QI_RTBWARNLIMIT(dqp->q_mount);
657                 resbcountp = &dqp->q_res_rtbcount;
658         }
659         error = 0;
660
661         if ((flags & XFS_QMOPT_FORCE_RES) == 0 &&
662             dqp->q_core.d_id &&
663             XFS_IS_QUOTA_ENFORCED(dqp->q_mount)) {
664 #ifdef QUOTADEBUG
665                 cmn_err(CE_DEBUG, "BLK Res: nblks=%ld + resbcount=%Ld"
666                           " > hardlimit=%Ld?", nblks, *resbcountp, hardlimit);
667 #endif
668                 if (nblks > 0) {
669                         /*
670                          * dquot is locked already. See if we'd go over the
671                          * hardlimit or exceed the timelimit if we allocate
672                          * nblks.
673                          */
674                         if (hardlimit > 0ULL &&
675                              (hardlimit <= nblks + *resbcountp)) {
676                                 error = xfs_quota_error(flags);
677                                 goto error_return;
678                         }
679
680                         if (softlimit > 0ULL &&
681                              (softlimit <= nblks + *resbcountp)) {
682                                 if ((timer != 0 && get_seconds() > timer) ||
683                                     (warns != 0 && warns >= warnlimit)) {
684                                         error = xfs_quota_error(flags);
685                                         goto error_return;
686                                 }
687                         }
688                 }
689                 if (ninos > 0) {
690                         count = be64_to_cpu(dqp->q_core.d_icount);
691                         timer = be32_to_cpu(dqp->q_core.d_itimer);
692                         warns = be16_to_cpu(dqp->q_core.d_iwarns);
693                         warnlimit = XFS_QI_IWARNLIMIT(dqp->q_mount);
694                         hardlimit = be64_to_cpu(dqp->q_core.d_ino_hardlimit);
695                         if (!hardlimit)
696                                 hardlimit = q->qi_ihardlimit;
697                         softlimit = be64_to_cpu(dqp->q_core.d_ino_softlimit);
698                         if (!softlimit)
699                                 softlimit = q->qi_isoftlimit;
700                         if (hardlimit > 0ULL && count >= hardlimit) {
701                                 error = xfs_quota_error(flags);
702                                 goto error_return;
703                         } else if (softlimit > 0ULL && count >= softlimit) {
704                                 if ((timer != 0 && get_seconds() > timer) ||
705                                      (warns != 0 && warns >= warnlimit)) {
706                                         error = xfs_quota_error(flags);
707                                         goto error_return;
708                                 }
709                         }
710                 }
711         }
712
713         /*
714          * Change the reservation, but not the actual usage.
715          * Note that q_res_bcount = q_core.d_bcount + resv
716          */
717         (*resbcountp) += (xfs_qcnt_t)nblks;
718         if (ninos != 0)
719                 dqp->q_res_icount += (xfs_qcnt_t)ninos;
720
721         /*
722          * note the reservation amt in the trans struct too,
723          * so that the transaction knows how much was reserved by
724          * it against this particular dquot.
725          * We don't do this when we are reserving for a delayed allocation,
726          * because we don't have the luxury of a transaction envelope then.
727          */
728         if (tp) {
729                 ASSERT(tp->t_dqinfo);
730                 ASSERT(flags & XFS_QMOPT_RESBLK_MASK);
731                 if (nblks != 0)
732                         xfs_trans_mod_dquot(tp, dqp,
733                                             flags & XFS_QMOPT_RESBLK_MASK,
734                                             nblks);
735                 if (ninos != 0)
736                         xfs_trans_mod_dquot(tp, dqp,
737                                             XFS_TRANS_DQ_RES_INOS,
738                                             ninos);
739         }
740         ASSERT(dqp->q_res_bcount >= be64_to_cpu(dqp->q_core.d_bcount));
741         ASSERT(dqp->q_res_rtbcount >= be64_to_cpu(dqp->q_core.d_rtbcount));
742         ASSERT(dqp->q_res_icount >= be64_to_cpu(dqp->q_core.d_icount));
743
744 error_return:
745         if (! (flags & XFS_QMOPT_DQLOCK)) {
746                 xfs_dqunlock(dqp);
747         }
748         return (error);
749 }
750
751
752 /*
753  * Given dquot(s), make disk block and/or inode reservations against them.
754  * The fact that this does the reservation against both the usr and
755  * grp/prj quotas is important, because this follows a both-or-nothing
756  * approach.
757  *
758  * flags = XFS_QMOPT_DQLOCK indicate if dquot(s) need to be locked.
759  *         XFS_QMOPT_FORCE_RES evades limit enforcement. Used by chown.
760  *         XFS_QMOPT_ENOSPC returns ENOSPC not EDQUOT.  Used by pquota.
761  *         XFS_TRANS_DQ_RES_BLKS reserves regular disk blocks
762  *         XFS_TRANS_DQ_RES_RTBLKS reserves realtime disk blocks
763  * dquots are unlocked on return, if they were not locked by caller.
764  */
765 int
766 xfs_trans_reserve_quota_bydquots(
767         xfs_trans_t     *tp,
768         xfs_mount_t     *mp,
769         xfs_dquot_t     *udqp,
770         xfs_dquot_t     *gdqp,
771         long            nblks,
772         long            ninos,
773         uint            flags)
774 {
775         int             resvd = 0, error;
776
777         if (!XFS_IS_QUOTA_ON(mp))
778                 return 0;
779
780         if (tp && tp->t_dqinfo == NULL)
781                 xfs_trans_alloc_dqinfo(tp);
782
783         ASSERT(flags & XFS_QMOPT_RESBLK_MASK);
784
785         if (udqp) {
786                 error = xfs_trans_dqresv(tp, mp, udqp, nblks, ninos,
787                                         (flags & ~XFS_QMOPT_ENOSPC));
788                 if (error)
789                         return error;
790                 resvd = 1;
791         }
792
793         if (gdqp) {
794                 error = xfs_trans_dqresv(tp, mp, gdqp, nblks, ninos, flags);
795                 if (error) {
796                         /*
797                          * can't do it, so backout previous reservation
798                          */
799                         if (resvd) {
800                                 flags |= XFS_QMOPT_FORCE_RES;
801                                 xfs_trans_dqresv(tp, mp, udqp,
802                                                  -nblks, -ninos, flags);
803                         }
804                         return error;
805                 }
806         }
807
808         /*
809          * Didn't change anything critical, so, no need to log
810          */
811         return 0;
812 }
813
814
815 /*
816  * Lock the dquot and change the reservation if we can.
817  * This doesn't change the actual usage, just the reservation.
818  * The inode sent in is locked.
819  */
820 STATIC int
821 xfs_trans_reserve_quota_nblks(
822         xfs_trans_t     *tp,
823         xfs_mount_t     *mp,
824         xfs_inode_t     *ip,
825         long            nblks,
826         long            ninos,
827         uint            flags)
828 {
829         int             error;
830
831         if (!XFS_IS_QUOTA_ON(mp))
832                 return 0;
833         if (XFS_IS_PQUOTA_ON(mp))
834                 flags |= XFS_QMOPT_ENOSPC;
835
836         ASSERT(ip->i_ino != mp->m_sb.sb_uquotino);
837         ASSERT(ip->i_ino != mp->m_sb.sb_gquotino);
838
839         ASSERT(XFS_ISLOCKED_INODE_EXCL(ip));
840         ASSERT(XFS_IS_QUOTA_RUNNING(ip->i_mount));
841         ASSERT((flags & ~(XFS_QMOPT_FORCE_RES | XFS_QMOPT_ENOSPC)) ==
842                                 XFS_TRANS_DQ_RES_RTBLKS ||
843                (flags & ~(XFS_QMOPT_FORCE_RES | XFS_QMOPT_ENOSPC)) ==
844                                 XFS_TRANS_DQ_RES_BLKS);
845
846         /*
847          * Reserve nblks against these dquots, with trans as the mediator.
848          */
849         error = xfs_trans_reserve_quota_bydquots(tp, mp,
850                                                  ip->i_udquot, ip->i_gdquot,
851                                                  nblks, ninos,
852                                                  flags);
853         return error;
854 }
855
856 /*
857  * This routine is called to allocate a quotaoff log item.
858  */
859 xfs_qoff_logitem_t *
860 xfs_trans_get_qoff_item(
861         xfs_trans_t             *tp,
862         xfs_qoff_logitem_t      *startqoff,
863         uint                    flags)
864 {
865         xfs_qoff_logitem_t      *q;
866
867         ASSERT(tp != NULL);
868
869         q = xfs_qm_qoff_logitem_init(tp->t_mountp, startqoff, flags);
870         ASSERT(q != NULL);
871
872         /*
873          * Get a log_item_desc to point at the new item.
874          */
875         (void) xfs_trans_add_item(tp, (xfs_log_item_t*)q);
876
877         return (q);
878 }
879
880
881 /*
882  * This is called to mark the quotaoff logitem as needing
883  * to be logged when the transaction is committed.  The logitem must
884  * already be associated with the given transaction.
885  */
886 void
887 xfs_trans_log_quotaoff_item(
888         xfs_trans_t             *tp,
889         xfs_qoff_logitem_t      *qlp)
890 {
891         xfs_log_item_desc_t     *lidp;
892
893         lidp = xfs_trans_find_item(tp, (xfs_log_item_t *)qlp);
894         ASSERT(lidp != NULL);
895
896         tp->t_flags |= XFS_TRANS_DIRTY;
897         lidp->lid_flags |= XFS_LID_DIRTY;
898 }
899
900 STATIC void
901 xfs_trans_alloc_dqinfo(
902         xfs_trans_t     *tp)
903 {
904         (tp)->t_dqinfo = kmem_zone_zalloc(xfs_Gqm->qm_dqtrxzone, KM_SLEEP);
905 }
906
907 STATIC void
908 xfs_trans_free_dqinfo(
909         xfs_trans_t     *tp)
910 {
911         if (!tp->t_dqinfo)
912                 return;
913         kmem_zone_free(xfs_Gqm->qm_dqtrxzone, (tp)->t_dqinfo);
914         (tp)->t_dqinfo = NULL;
915 }
916
917 xfs_dqtrxops_t  xfs_trans_dquot_ops = {
918         .qo_dup_dqinfo                  = xfs_trans_dup_dqinfo,
919         .qo_free_dqinfo                 = xfs_trans_free_dqinfo,
920         .qo_mod_dquot_byino             = xfs_trans_mod_dquot_byino,
921         .qo_apply_dquot_deltas          = xfs_trans_apply_dquot_deltas,
922         .qo_reserve_quota_nblks         = xfs_trans_reserve_quota_nblks,
923         .qo_reserve_quota_bydquots      = xfs_trans_reserve_quota_bydquots,
924         .qo_unreserve_and_mod_dquots    = xfs_trans_unreserve_and_mod_dquots,
925 };