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