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