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