[XFS] Update license/copyright notices to match the prefered SGI
[pandora-kernel.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(INT_GET(d->d_bcount, ARCH_CONVERT) >=
417                                        (xfs_qcnt_t) -totalbdelta);
418
419                         if (totalrtbdelta < 0)
420                                 ASSERT(INT_GET(d->d_rtbcount, ARCH_CONVERT) >=
421                                        (xfs_qcnt_t) -totalrtbdelta);
422
423                         if (qtrx->qt_icount_delta < 0)
424                                 ASSERT(INT_GET(d->d_icount, ARCH_CONVERT) >=
425                                        (xfs_qcnt_t) -qtrx->qt_icount_delta);
426 #endif
427                         if (totalbdelta)
428                                 INT_MOD(d->d_bcount, ARCH_CONVERT, (xfs_qcnt_t)totalbdelta);
429
430                         if (qtrx->qt_icount_delta)
431                                 INT_MOD(d->d_icount, ARCH_CONVERT, (xfs_qcnt_t)qtrx->qt_icount_delta);
432
433                         if (totalrtbdelta)
434                                 INT_MOD(d->d_rtbcount, ARCH_CONVERT, (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                                 INT_GET(dqp->q_core.d_bcount, ARCH_CONVERT));
519                         ASSERT(dqp->q_res_icount >=
520                                 INT_GET(dqp->q_core.d_icount, ARCH_CONVERT));
521                         ASSERT(dqp->q_res_rtbcount >=
522                                 INT_GET(dqp->q_core.d_rtbcount, ARCH_CONVERT));
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 /*
599  * This reserves disk blocks and inodes against a dquot.
600  * Flags indicate if the dquot is to be locked here and also
601  * if the blk reservation is for RT or regular blocks.
602  * Sending in XFS_QMOPT_FORCE_RES flag skips the quota check.
603  * Returns EDQUOT if quota is exceeded.
604  */
605 STATIC int
606 xfs_trans_dqresv(
607         xfs_trans_t     *tp,
608         xfs_mount_t     *mp,
609         xfs_dquot_t     *dqp,
610         long            nblks,
611         long            ninos,
612         uint            flags)
613 {
614         int             error;
615         xfs_qcnt_t      hardlimit;
616         xfs_qcnt_t      softlimit;
617         time_t          timer;
618         xfs_qwarncnt_t  warns;
619         xfs_qwarncnt_t  warnlimit;
620         xfs_qcnt_t      count;
621         xfs_qcnt_t      *resbcountp;
622         xfs_quotainfo_t *q = mp->m_quotainfo;
623
624         if (! (flags & XFS_QMOPT_DQLOCK)) {
625                 xfs_dqlock(dqp);
626         }
627         ASSERT(XFS_DQ_IS_LOCKED(dqp));
628         if (flags & XFS_TRANS_DQ_RES_BLKS) {
629                 hardlimit = INT_GET(dqp->q_core.d_blk_hardlimit, ARCH_CONVERT);
630                 if (!hardlimit)
631                         hardlimit = q->qi_bhardlimit;
632                 softlimit = INT_GET(dqp->q_core.d_blk_softlimit, ARCH_CONVERT);
633                 if (!softlimit)
634                         softlimit = q->qi_bsoftlimit;
635                 timer = INT_GET(dqp->q_core.d_btimer, ARCH_CONVERT);
636                 warns = INT_GET(dqp->q_core.d_bwarns, ARCH_CONVERT);
637                 warnlimit = XFS_QI_BWARNLIMIT(dqp->q_mount);
638                 resbcountp = &dqp->q_res_bcount;
639         } else {
640                 ASSERT(flags & XFS_TRANS_DQ_RES_RTBLKS);
641                 hardlimit = INT_GET(dqp->q_core.d_rtb_hardlimit, ARCH_CONVERT);
642                 if (!hardlimit)
643                         hardlimit = q->qi_rtbhardlimit;
644                 softlimit = INT_GET(dqp->q_core.d_rtb_softlimit, ARCH_CONVERT);
645                 if (!softlimit)
646                         softlimit = q->qi_rtbsoftlimit;
647                 timer = INT_GET(dqp->q_core.d_rtbtimer, ARCH_CONVERT);
648                 warns = INT_GET(dqp->q_core.d_rtbwarns, ARCH_CONVERT);
649                 warnlimit = XFS_QI_RTBWARNLIMIT(dqp->q_mount);
650                 resbcountp = &dqp->q_res_rtbcount;
651         }
652         error = 0;
653
654         if ((flags & XFS_QMOPT_FORCE_RES) == 0 &&
655             dqp->q_core.d_id &&
656             XFS_IS_QUOTA_ENFORCED(dqp->q_mount)) {
657 #ifdef QUOTADEBUG
658                 cmn_err(CE_DEBUG, "BLK Res: nblks=%ld + resbcount=%Ld"
659                           " > hardlimit=%Ld?", nblks, *resbcountp, hardlimit);
660 #endif
661                 if (nblks > 0) {
662                         /*
663                          * dquot is locked already. See if we'd go over the
664                          * hardlimit or exceed the timelimit if we allocate
665                          * nblks.
666                          */
667                         if (hardlimit > 0ULL &&
668                              (hardlimit <= nblks + *resbcountp)) {
669                                 error = EDQUOT;
670                                 goto error_return;
671                         }
672
673                         if (softlimit > 0ULL &&
674                              (softlimit <= nblks + *resbcountp)) {
675                                 /*
676                                  * If timer or warnings has expired,
677                                  * return EDQUOT
678                                  */
679                                 if ((timer != 0 && get_seconds() > timer) ||
680                                     (warns != 0 && warns >= warnlimit)) {
681                                         error = EDQUOT;
682                                         goto error_return;
683                                 }
684                         }
685                 }
686                 if (ninos > 0) {
687                         count = INT_GET(dqp->q_core.d_icount, ARCH_CONVERT);
688                         timer = INT_GET(dqp->q_core.d_itimer, ARCH_CONVERT);
689                         warns = INT_GET(dqp->q_core.d_iwarns, ARCH_CONVERT);
690                         warnlimit = XFS_QI_IWARNLIMIT(dqp->q_mount);
691                         hardlimit = INT_GET(dqp->q_core.d_ino_hardlimit,
692                                                 ARCH_CONVERT);
693                         if (!hardlimit)
694                                 hardlimit = q->qi_ihardlimit;
695                         softlimit = INT_GET(dqp->q_core.d_ino_softlimit,
696                                                 ARCH_CONVERT);
697                         if (!softlimit)
698                                 softlimit = q->qi_isoftlimit;
699                         if (hardlimit > 0ULL && count >= hardlimit) {
700                                 error = EDQUOT;
701                                 goto error_return;
702                         } else if (softlimit > 0ULL && count >= softlimit) {
703                                 /*
704                                  * If timer or warnings has expired,
705                                  * return EDQUOT
706                                  */
707                                 if ((timer != 0 && get_seconds() > timer) ||
708                                      (warns != 0 && warns >= warnlimit)) {
709                                         error = EDQUOT;
710                                         goto error_return;
711                                 }
712                         }
713                 }
714         }
715
716         /*
717          * Change the reservation, but not the actual usage.
718          * Note that q_res_bcount = q_core.d_bcount + resv
719          */
720         (*resbcountp) += (xfs_qcnt_t)nblks;
721         if (ninos != 0)
722                 dqp->q_res_icount += (xfs_qcnt_t)ninos;
723
724         /*
725          * note the reservation amt in the trans struct too,
726          * so that the transaction knows how much was reserved by
727          * it against this particular dquot.
728          * We don't do this when we are reserving for a delayed allocation,
729          * because we don't have the luxury of a transaction envelope then.
730          */
731         if (tp) {
732                 ASSERT(tp->t_dqinfo);
733                 ASSERT(flags & XFS_QMOPT_RESBLK_MASK);
734                 if (nblks != 0)
735                         xfs_trans_mod_dquot(tp, dqp,
736                                             flags & XFS_QMOPT_RESBLK_MASK,
737                                             nblks);
738                 if (ninos != 0)
739                         xfs_trans_mod_dquot(tp, dqp,
740                                             XFS_TRANS_DQ_RES_INOS,
741                                             ninos);
742         }
743         ASSERT(dqp->q_res_bcount >= INT_GET(dqp->q_core.d_bcount, ARCH_CONVERT));
744         ASSERT(dqp->q_res_rtbcount >= INT_GET(dqp->q_core.d_rtbcount, ARCH_CONVERT));
745         ASSERT(dqp->q_res_icount >= INT_GET(dqp->q_core.d_icount, ARCH_CONVERT));
746
747 error_return:
748         if (! (flags & XFS_QMOPT_DQLOCK)) {
749                 xfs_dqunlock(dqp);
750         }
751         return (error);
752 }
753
754
755 /*
756  * Given a dquot(s), make disk block and/or inode reservations against them.
757  * The fact that this does the reservation against both the usr and
758  * grp quotas is important, because this follows a both-or-nothing
759  * approach.
760  *
761  * flags = XFS_QMOPT_DQLOCK indicate if dquot(s) need to be locked.
762  *         XFS_QMOPT_FORCE_RES evades limit enforcement. Used by chown.
763  *         XFS_TRANS_DQ_RES_BLKS reserves regular disk blocks
764  *         XFS_TRANS_DQ_RES_RTBLKS reserves realtime disk blocks
765  * dquots are unlocked on return, if they were not locked by caller.
766  */
767 int
768 xfs_trans_reserve_quota_bydquots(
769         xfs_trans_t     *tp,
770         xfs_mount_t     *mp,
771         xfs_dquot_t     *udqp,
772         xfs_dquot_t     *gdqp,
773         long            nblks,
774         long            ninos,
775         uint            flags)
776 {
777         int             resvd;
778
779         if (! XFS_IS_QUOTA_ON(mp))
780                 return (0);
781
782         if (tp && tp->t_dqinfo == NULL)
783                 xfs_trans_alloc_dqinfo(tp);
784
785         ASSERT(flags & XFS_QMOPT_RESBLK_MASK);
786         resvd = 0;
787
788         if (udqp) {
789                 if (xfs_trans_dqresv(tp, mp, udqp, nblks, ninos, flags))
790                         return (EDQUOT);
791                 resvd = 1;
792         }
793
794         if (gdqp) {
795                 if (xfs_trans_dqresv(tp, mp, gdqp, nblks, ninos, flags)) {
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 (EDQUOT);
805                 }
806         }
807
808         /*
809          * Didnt 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  * Returns 0 on success, EDQUOT or other errors otherwise
821  */
822 STATIC int
823 xfs_trans_reserve_quota_nblks(
824         xfs_trans_t     *tp,
825         xfs_mount_t     *mp,
826         xfs_inode_t     *ip,
827         long            nblks,
828         long            ninos,
829         uint            type)
830 {
831         int             error;
832
833         if (!XFS_IS_QUOTA_ON(mp))
834                 return (0);
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((type & ~XFS_QMOPT_FORCE_RES) == XFS_TRANS_DQ_RES_RTBLKS ||
842                (type & ~XFS_QMOPT_FORCE_RES) == XFS_TRANS_DQ_RES_BLKS);
843
844         /*
845          * Reserve nblks against these dquots, with trans as the mediator.
846          */
847         error = xfs_trans_reserve_quota_bydquots(tp, mp,
848                                                  ip->i_udquot, ip->i_gdquot,
849                                                  nblks, ninos,
850                                                  type);
851         return (error);
852 }
853
854 /*
855  * This routine is called to allocate a quotaoff log item.
856  */
857 xfs_qoff_logitem_t *
858 xfs_trans_get_qoff_item(
859         xfs_trans_t             *tp,
860         xfs_qoff_logitem_t      *startqoff,
861         uint                    flags)
862 {
863         xfs_qoff_logitem_t      *q;
864
865         ASSERT(tp != NULL);
866
867         q = xfs_qm_qoff_logitem_init(tp->t_mountp, startqoff, flags);
868         ASSERT(q != NULL);
869
870         /*
871          * Get a log_item_desc to point at the new item.
872          */
873         (void) xfs_trans_add_item(tp, (xfs_log_item_t*)q);
874
875         return (q);
876 }
877
878
879 /*
880  * This is called to mark the quotaoff logitem as needing
881  * to be logged when the transaction is committed.  The logitem must
882  * already be associated with the given transaction.
883  */
884 void
885 xfs_trans_log_quotaoff_item(
886         xfs_trans_t             *tp,
887         xfs_qoff_logitem_t      *qlp)
888 {
889         xfs_log_item_desc_t     *lidp;
890
891         lidp = xfs_trans_find_item(tp, (xfs_log_item_t *)qlp);
892         ASSERT(lidp != NULL);
893
894         tp->t_flags |= XFS_TRANS_DIRTY;
895         lidp->lid_flags |= XFS_LID_DIRTY;
896 }
897
898 STATIC void
899 xfs_trans_alloc_dqinfo(
900         xfs_trans_t     *tp)
901 {
902         (tp)->t_dqinfo = kmem_zone_zalloc(xfs_Gqm->qm_dqtrxzone, KM_SLEEP);
903 }
904
905 STATIC void
906 xfs_trans_free_dqinfo(
907         xfs_trans_t     *tp)
908 {
909         if (!tp->t_dqinfo)
910                 return;
911         kmem_zone_free(xfs_Gqm->qm_dqtrxzone, (tp)->t_dqinfo);
912         (tp)->t_dqinfo = NULL;
913 }
914
915 xfs_dqtrxops_t  xfs_trans_dquot_ops = {
916         .qo_dup_dqinfo                  = xfs_trans_dup_dqinfo,
917         .qo_free_dqinfo                 = xfs_trans_free_dqinfo,
918         .qo_mod_dquot_byino             = xfs_trans_mod_dquot_byino,
919         .qo_apply_dquot_deltas          = xfs_trans_apply_dquot_deltas,
920         .qo_reserve_quota_nblks         = xfs_trans_reserve_quota_nblks,
921         .qo_reserve_quota_bydquots      = xfs_trans_reserve_quota_bydquots,
922         .qo_unreserve_and_mod_dquots    = xfs_trans_unreserve_and_mod_dquots,
923 };