blob: 133fc6fc3edd228994d2953eaf9ed08443c08c0b [file] [log] [blame]
Dave Chinner0b61f8a2018-06-05 19:42:14 -07001// SPDX-License-Identifier: GPL-2.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
Nathan Scott4ce31212005-11-02 14:59:41 +11003 * Copyright (c) 2000-2002 Silicon Graphics, Inc.
4 * All Rights Reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006#include "xfs.h"
7#include "xfs_fs.h"
Dave Chinner70a98832013-10-23 10:36:05 +11008#include "xfs_shared.h"
Dave Chinner239880e2013-10-23 10:50:10 +11009#include "xfs_format.h"
10#include "xfs_log_format.h"
11#include "xfs_trans_resv.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include "xfs_mount.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include "xfs_inode.h"
Dave Chinner239880e2013-10-23 10:50:10 +110014#include "xfs_trans.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include "xfs_trans_priv.h"
Dave Chinnera4fbe6a2013-10-23 10:51:50 +110016#include "xfs_quota.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include "xfs_qm.h"
Darrick J. Wong2cb91ba2020-07-14 10:37:35 -070018#include "xfs_trace.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070019
20STATIC 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 */
27void
28xfs_trans_dqjoin(
Pavel Reichlaefe69a2019-11-12 17:04:02 -080029 struct xfs_trans *tp,
30 struct xfs_dquot *dqp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070031{
Linus Torvalds1da177e2005-04-16 15:20:36 -070032 ASSERT(XFS_DQ_IS_LOCKED(dqp));
Christoph Hellwige98c4142010-06-23 18:11:15 +100033 ASSERT(dqp->q_logitem.qli_dquot == dqp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
35 /*
36 * Get a log_item_desc to point at the new item.
37 */
Christoph Hellwige98c4142010-06-23 18:11:15 +100038 xfs_trans_add_item(tp, &dqp->q_logitem.qli_item);
Linus Torvalds1da177e2005-04-16 15:20:36 -070039}
40
Linus Torvalds1da177e2005-04-16 15:20:36 -070041/*
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 */
51void
52xfs_trans_log_dquot(
Pavel Reichlaefe69a2019-11-12 17:04:02 -080053 struct xfs_trans *tp,
54 struct xfs_dquot *dqp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070055{
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 ASSERT(XFS_DQ_IS_LOCKED(dqp));
57
Darrick J. Wong4ea1ff32020-08-17 09:59:51 -070058 /* 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
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 tp->t_flags |= XFS_TRANS_DIRTY;
Dave Chinnere6631f82018-05-09 07:49:37 -070065 set_bit(XFS_LI_DIRTY, &dqp->q_logitem.qli_item.li_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -070066}
67
68/*
69 * Carry forward whatever is left of the quota blk reservation to
70 * the spanky new transaction
71 */
Christoph Hellwig7d095252009-06-08 15:33:32 +020072void
Linus Torvalds1da177e2005-04-16 15:20:36 -070073xfs_trans_dup_dqinfo(
Darrick J. Wong078f4a72019-04-17 16:30:24 -070074 struct xfs_trans *otp,
75 struct xfs_trans *ntp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070076{
Darrick J. Wong078f4a72019-04-17 16:30:24 -070077 struct xfs_dqtrx *oq, *nq;
78 int i, j;
79 struct xfs_dqtrx *oqa, *nqa;
80 uint64_t blk_res_used;
Linus Torvalds1da177e2005-04-16 15:20:36 -070081
82 if (!otp->t_dqinfo)
83 return;
84
85 xfs_trans_alloc_dqinfo(ntp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
87 /*
88 * Because the quota blk reservation is carried forward,
89 * it is also necessary to carry forward the DQ_DIRTY flag.
90 */
Nan Jia339e4f62015-06-01 10:50:00 +100091 if (otp->t_flags & XFS_TRANS_DQ_DIRTY)
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 ntp->t_flags |= XFS_TRANS_DQ_DIRTY;
93
Chandra Seetharaman0e6436d2013-06-27 17:25:09 -050094 for (j = 0; j < XFS_QM_TRANS_DQTYPES; j++) {
95 oqa = otp->t_dqinfo->dqs[j];
96 nqa = ntp->t_dqinfo->dqs[j];
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 for (i = 0; i < XFS_QM_TRANS_MAXDQS; i++) {
Brian Foster7f884dc2015-06-01 07:15:37 +100098 blk_res_used = 0;
99
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 if (oqa[i].qt_dquot == NULL)
101 break;
102 oq = &oqa[i];
103 nq = &nqa[i];
104
Brian Foster7f884dc2015-06-01 07:15:37 +1000105 if (oq->qt_blk_res && oq->qt_bcount_delta > 0)
106 blk_res_used = oq->qt_bcount_delta;
107
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 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 */
Brian Foster7f884dc2015-06-01 07:15:37 +1000115 nq->qt_blk_res = oq->qt_blk_res - blk_res_used;
116 oq->qt_blk_res = blk_res_used;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117
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 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 }
127}
128
129/*
130 * Wrap around mod_dquot to account for both user and group quotas.
131 */
Christoph Hellwig7d095252009-06-08 15:33:32 +0200132void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133xfs_trans_mod_dquot_byino(
134 xfs_trans_t *tp,
135 xfs_inode_t *ip,
136 uint field,
Darrick J. Wong903b1fc2019-04-17 16:30:24 -0700137 int64_t delta)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138{
Christoph Hellwig7d095252009-06-08 15:33:32 +0200139 xfs_mount_t *mp = tp->t_mountp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140
Christoph Hellwig7d095252009-06-08 15:33:32 +0200141 if (!XFS_IS_QUOTA_RUNNING(mp) ||
142 !XFS_IS_QUOTA_ON(mp) ||
Chandra Seetharaman9cad19d2013-06-27 17:25:04 -0500143 xfs_is_quota_inode(&mp->m_sb, ip->i_ino))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 return;
145
146 if (tp->t_dqinfo == NULL)
147 xfs_trans_alloc_dqinfo(tp);
148
Nathan Scottc8ad20f2005-06-21 15:38:48 +1000149 if (XFS_IS_UQUOTA_ON(mp) && ip->i_udquot)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 (void) xfs_trans_mod_dquot(tp, ip->i_udquot, field, delta);
Chandra Seetharaman92f8ff72013-07-11 00:00:40 -0500151 if (XFS_IS_GQUOTA_ON(mp) && ip->i_gdquot)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 (void) xfs_trans_mod_dquot(tp, ip->i_gdquot, field, delta);
Chandra Seetharaman92f8ff72013-07-11 00:00:40 -0500153 if (XFS_IS_PQUOTA_ON(mp) && ip->i_pdquot)
154 (void) xfs_trans_mod_dquot(tp, ip->i_pdquot, field, delta);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155}
156
Chandra Seetharaman113a5682013-06-27 17:25:07 -0500157STATIC struct xfs_dqtrx *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158xfs_trans_get_dqtrx(
Chandra Seetharaman113a5682013-06-27 17:25:07 -0500159 struct xfs_trans *tp,
160 struct xfs_dquot *dqp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161{
Chandra Seetharaman113a5682013-06-27 17:25:07 -0500162 int i;
163 struct xfs_dqtrx *qa;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164
Darrick J. Wong00a342e2020-07-15 17:47:13 -0700165 switch (xfs_dquot_type(dqp)) {
166 case XFS_DQTYPE_USER:
Chandra Seetharaman0e6436d2013-06-27 17:25:09 -0500167 qa = tp->t_dqinfo->dqs[XFS_QM_TRANS_USR];
Darrick J. Wong00a342e2020-07-15 17:47:13 -0700168 break;
169 case XFS_DQTYPE_GROUP:
Chandra Seetharaman0e6436d2013-06-27 17:25:09 -0500170 qa = tp->t_dqinfo->dqs[XFS_QM_TRANS_GRP];
Darrick J. Wong00a342e2020-07-15 17:47:13 -0700171 break;
172 case XFS_DQTYPE_PROJ:
Chandra Seetharaman92f8ff72013-07-11 00:00:40 -0500173 qa = tp->t_dqinfo->dqs[XFS_QM_TRANS_PRJ];
Darrick J. Wong00a342e2020-07-15 17:47:13 -0700174 break;
175 default:
Chandra Seetharaman92f8ff72013-07-11 00:00:40 -0500176 return NULL;
Darrick J. Wong00a342e2020-07-15 17:47:13 -0700177 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178
Christoph Hellwig191f8482010-04-20 17:01:53 +1000179 for (i = 0; i < XFS_QM_TRANS_MAXDQS; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 if (qa[i].qt_dquot == NULL ||
Christoph Hellwig191f8482010-04-20 17:01:53 +1000181 qa[i].qt_dquot == dqp)
182 return &qa[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 }
184
Christoph Hellwig191f8482010-04-20 17:01:53 +1000185 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186}
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 */
194void
195xfs_trans_mod_dquot(
Darrick J. Wong078f4a72019-04-17 16:30:24 -0700196 struct xfs_trans *tp,
197 struct xfs_dquot *dqp,
198 uint field,
199 int64_t delta)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200{
Darrick J. Wong078f4a72019-04-17 16:30:24 -0700201 struct xfs_dqtrx *qtrx;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202
203 ASSERT(tp);
Christoph Hellwig7d095252009-06-08 15:33:32 +0200204 ASSERT(XFS_IS_QUOTA_RUNNING(tp->t_mountp));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 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
Darrick J. Wong2cb91ba2020-07-14 10:37:35 -0700218 if (delta) {
219 trace_xfs_trans_mod_dquot_before(qtrx);
220 trace_xfs_trans_mod_dquot(tp, dqp, field, delta);
221 }
222
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 switch (field) {
224
225 /*
226 * regular disk blk reservation
227 */
228 case XFS_TRANS_DQ_RES_BLKS:
Darrick J. Wong903b1fc2019-04-17 16:30:24 -0700229 qtrx->qt_blk_res += delta;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 break;
231
232 /*
233 * inode reservation
234 */
235 case XFS_TRANS_DQ_RES_INOS:
Darrick J. Wong903b1fc2019-04-17 16:30:24 -0700236 qtrx->qt_ino_res += delta;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 break;
238
239 /*
240 * disk blocks used.
241 */
242 case XFS_TRANS_DQ_BCOUNT:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 qtrx->qt_bcount_delta += delta;
244 break;
245
246 case XFS_TRANS_DQ_DELBCOUNT:
247 qtrx->qt_delbcnt_delta += delta;
248 break;
249
250 /*
251 * Inode Count
252 */
253 case XFS_TRANS_DQ_ICOUNT:
254 if (qtrx->qt_ino_res && delta > 0) {
Darrick J. Wong903b1fc2019-04-17 16:30:24 -0700255 qtrx->qt_ino_res_used += delta;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 ASSERT(qtrx->qt_ino_res >= qtrx->qt_ino_res_used);
257 }
258 qtrx->qt_icount_delta += delta;
259 break;
260
261 /*
262 * rtblk reservation
263 */
264 case XFS_TRANS_DQ_RES_RTBLKS:
Darrick J. Wong903b1fc2019-04-17 16:30:24 -0700265 qtrx->qt_rtblk_res += delta;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 break;
267
268 /*
269 * rtblk count
270 */
271 case XFS_TRANS_DQ_RTBCOUNT:
272 if (qtrx->qt_rtblk_res && delta > 0) {
Darrick J. Wong903b1fc2019-04-17 16:30:24 -0700273 qtrx->qt_rtblk_res_used += delta;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 ASSERT(qtrx->qt_rtblk_res >= qtrx->qt_rtblk_res_used);
275 }
276 qtrx->qt_rtbcount_delta += delta;
277 break;
278
279 case XFS_TRANS_DQ_DELRTBCOUNT:
280 qtrx->qt_delrtb_delta += delta;
281 break;
282
283 default:
284 ASSERT(0);
285 }
Darrick J. Wong2cb91ba2020-07-14 10:37:35 -0700286
287 if (delta)
288 trace_xfs_trans_mod_dquot_after(qtrx);
289
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 tp->t_flags |= XFS_TRANS_DQ_DIRTY;
291}
292
293
294/*
Dave Chinnerb0a9dab2013-07-10 07:04:01 +1000295 * Given an array of dqtrx structures, lock all the dquots associated and join
296 * them to the transaction, provided they have been modified. We know that the
Christoph Hellwig10f73d22013-11-06 03:45:36 -0800297 * highest number of dquots of one type - usr, grp and prj - involved in a
298 * transaction is 3 so we don't need to make this very generic.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 */
300STATIC void
301xfs_trans_dqlockedjoin(
Darrick J. Wong078f4a72019-04-17 16:30:24 -0700302 struct xfs_trans *tp,
303 struct xfs_dqtrx *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304{
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
Darrick J. Wongd92c8812020-07-14 10:37:34 -0700317/* Apply dqtrx changes to the quota reservation counters. */
318static inline void
319xfs_apply_quota_reservation_deltas(
320 struct xfs_dquot_res *res,
321 uint64_t reserved,
322 int64_t res_used,
323 int64_t count_delta)
324{
325 if (reserved != 0) {
326 /*
327 * Subtle math here: If reserved > res_used (the normal case),
328 * we're simply subtracting the unused transaction quota
329 * reservation from the dquot reservation.
330 *
331 * If, however, res_used > reserved, then we have allocated
332 * more quota blocks than were reserved for the transaction.
333 * We must add that excess to the dquot reservation since it
334 * tracks (usage + resv) and by definition we didn't reserve
335 * that excess.
336 */
337 res->reserved -= abs(reserved - res_used);
338 } else if (count_delta != 0) {
339 /*
340 * These blks were never reserved, either inside a transaction
341 * or outside one (in a delayed allocation). Also, this isn't
342 * always a negative number since we sometimes deliberately
343 * skip quota reservations.
344 */
345 res->reserved += count_delta;
346 }
347}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348
349/*
350 * Called by xfs_trans_commit() and similar in spirit to
351 * xfs_trans_apply_sb_deltas().
352 * Go thru all the dquots belonging to this transaction and modify the
353 * INCORE dquot to reflect the actual usages.
354 * Unreserve just the reservations done by this transaction.
355 * dquot is still left locked at exit.
356 */
Christoph Hellwig7d095252009-06-08 15:33:32 +0200357void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358xfs_trans_apply_dquot_deltas(
Brian Foster4b6eae2e2013-03-18 10:51:45 -0400359 struct xfs_trans *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360{
361 int i, j;
Brian Foster4b6eae2e2013-03-18 10:51:45 -0400362 struct xfs_dquot *dqp;
363 struct xfs_dqtrx *qtrx, *qa;
Darrick J. Wong903b1fc2019-04-17 16:30:24 -0700364 int64_t totalbdelta;
365 int64_t totalrtbdelta;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366
Christoph Hellwig7d095252009-06-08 15:33:32 +0200367 if (!(tp->t_flags & XFS_TRANS_DQ_DIRTY))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 return;
369
370 ASSERT(tp->t_dqinfo);
Chandra Seetharaman0e6436d2013-06-27 17:25:09 -0500371 for (j = 0; j < XFS_QM_TRANS_DQTYPES; j++) {
372 qa = tp->t_dqinfo->dqs[j];
373 if (qa[0].qt_dquot == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375
376 /*
377 * Lock all of the dquots and join them to the transaction.
378 */
379 xfs_trans_dqlockedjoin(tp, qa);
380
381 for (i = 0; i < XFS_QM_TRANS_MAXDQS; i++) {
Darrick J. Wongd92c8812020-07-14 10:37:34 -0700382 uint64_t blk_res_used;
383
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 qtrx = &qa[i];
385 /*
386 * The array of dquots is filled
387 * sequentially, not sparsely.
388 */
389 if ((dqp = qtrx->qt_dquot) == NULL)
390 break;
391
392 ASSERT(XFS_DQ_IS_LOCKED(dqp));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393
394 /*
395 * adjust the actual number of blocks used
396 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397
398 /*
399 * The issue here is - sometimes we don't make a blkquota
400 * reservation intentionally to be fair to users
401 * (when the amount is small). On the other hand,
402 * delayed allocs do make reservations, but that's
403 * outside of a transaction, so we have no
404 * idea how much was really reserved.
405 * So, here we've accumulated delayed allocation blks and
406 * non-delay blks. The assumption is that the
407 * delayed ones are always reserved (outside of a
408 * transaction), and the others may or may not have
409 * quota reservations.
410 */
411 totalbdelta = qtrx->qt_bcount_delta +
412 qtrx->qt_delbcnt_delta;
413 totalrtbdelta = qtrx->qt_rtbcount_delta +
414 qtrx->qt_delrtb_delta;
Darrick J. Wong2cb91ba2020-07-14 10:37:35 -0700415
416 if (totalbdelta != 0 || totalrtbdelta != 0 ||
417 qtrx->qt_icount_delta != 0) {
418 trace_xfs_trans_apply_dquot_deltas_before(dqp);
419 trace_xfs_trans_apply_dquot_deltas(qtrx);
420 }
421
Christoph Hellwigea15ab32011-07-13 13:43:50 +0200422#ifdef DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 if (totalbdelta < 0)
Darrick J. Wongbe37d402020-07-14 10:37:31 -0700424 ASSERT(dqp->q_blk.count >= -totalbdelta);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425
426 if (totalrtbdelta < 0)
Darrick J. Wongbe37d402020-07-14 10:37:31 -0700427 ASSERT(dqp->q_rtb.count >= -totalrtbdelta);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428
429 if (qtrx->qt_icount_delta < 0)
Darrick J. Wongbe37d402020-07-14 10:37:31 -0700430 ASSERT(dqp->q_ino.count >= -qtrx->qt_icount_delta);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431#endif
432 if (totalbdelta)
Darrick J. Wongbe37d402020-07-14 10:37:31 -0700433 dqp->q_blk.count += totalbdelta;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434
435 if (qtrx->qt_icount_delta)
Darrick J. Wongbe37d402020-07-14 10:37:31 -0700436 dqp->q_ino.count += qtrx->qt_icount_delta;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437
438 if (totalrtbdelta)
Darrick J. Wongbe37d402020-07-14 10:37:31 -0700439 dqp->q_rtb.count += totalrtbdelta;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440
Darrick J. Wong2cb91ba2020-07-14 10:37:35 -0700441 if (totalbdelta != 0 || totalrtbdelta != 0 ||
442 qtrx->qt_icount_delta != 0)
443 trace_xfs_trans_apply_dquot_deltas_after(dqp);
444
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 /*
446 * Get any default limits in use.
447 * Start/reset the timer(s) if needed.
448 */
Darrick J. Wongc51df732020-07-14 10:37:30 -0700449 if (dqp->q_id) {
Darrick J. Wongc8c753e2020-07-14 10:37:33 -0700450 xfs_qm_adjust_dqlimits(dqp);
451 xfs_qm_adjust_dqtimers(dqp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 }
453
Darrick J. Wong985a78f2020-07-14 10:37:13 -0700454 dqp->q_flags |= XFS_DQFLAG_DIRTY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 /*
456 * add this to the list of items to get logged
457 */
458 xfs_trans_log_dquot(tp, dqp);
459 /*
460 * Take off what's left of the original reservation.
461 * In case of delayed allocations, there's no
462 * reservation that a transaction structure knows of.
463 */
Darrick J. Wongd92c8812020-07-14 10:37:34 -0700464 blk_res_used = max_t(int64_t, 0, qtrx->qt_bcount_delta);
465 xfs_apply_quota_reservation_deltas(&dqp->q_blk,
466 qtrx->qt_blk_res, blk_res_used,
467 qtrx->qt_bcount_delta);
Brian Foster7f884dc2015-06-01 07:15:37 +1000468
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 /*
470 * Adjust the RT reservation.
471 */
Darrick J. Wongd92c8812020-07-14 10:37:34 -0700472 xfs_apply_quota_reservation_deltas(&dqp->q_rtb,
473 qtrx->qt_rtblk_res,
474 qtrx->qt_rtblk_res_used,
475 qtrx->qt_rtbcount_delta);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476
477 /*
478 * Adjust the inode reservation.
479 */
Darrick J. Wongd92c8812020-07-14 10:37:34 -0700480 ASSERT(qtrx->qt_ino_res >= qtrx->qt_ino_res_used);
481 xfs_apply_quota_reservation_deltas(&dqp->q_ino,
482 qtrx->qt_ino_res,
483 qtrx->qt_ino_res_used,
484 qtrx->qt_icount_delta);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485
Darrick J. Wongbe37d402020-07-14 10:37:31 -0700486 ASSERT(dqp->q_blk.reserved >= dqp->q_blk.count);
487 ASSERT(dqp->q_ino.reserved >= dqp->q_ino.count);
488 ASSERT(dqp->q_rtb.reserved >= dqp->q_rtb.count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 }
491}
492
493/*
494 * Release the reservations, and adjust the dquots accordingly.
495 * This is called only when the transaction is being aborted. If by
496 * any chance we have done dquot modifications incore (ie. deltas) already,
497 * we simply throw those away, since that's the expected behavior
498 * when a transaction is curtailed without a commit.
499 */
Christoph Hellwig7d095252009-06-08 15:33:32 +0200500void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501xfs_trans_unreserve_and_mod_dquots(
Pavel Reichlaefe69a2019-11-12 17:04:02 -0800502 struct xfs_trans *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503{
504 int i, j;
Pavel Reichlaefe69a2019-11-12 17:04:02 -0800505 struct xfs_dquot *dqp;
Darrick J. Wong078f4a72019-04-17 16:30:24 -0700506 struct xfs_dqtrx *qtrx, *qa;
Pavel Reichlaefe69a2019-11-12 17:04:02 -0800507 bool locked;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508
509 if (!tp->t_dqinfo || !(tp->t_flags & XFS_TRANS_DQ_DIRTY))
510 return;
511
Chandra Seetharaman0e6436d2013-06-27 17:25:09 -0500512 for (j = 0; j < XFS_QM_TRANS_DQTYPES; j++) {
513 qa = tp->t_dqinfo->dqs[j];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 for (i = 0; i < XFS_QM_TRANS_MAXDQS; i++) {
516 qtrx = &qa[i];
517 /*
518 * We assume that the array of dquots is filled
519 * sequentially, not sparsely.
520 */
521 if ((dqp = qtrx->qt_dquot) == NULL)
522 break;
523 /*
524 * Unreserve the original reservation. We don't care
525 * about the number of blocks used field, or deltas.
526 * Also we don't bother to zero the fields.
527 */
Thiago Farina667a9292012-11-12 21:32:59 -0200528 locked = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 if (qtrx->qt_blk_res) {
530 xfs_dqlock(dqp);
Thiago Farina667a9292012-11-12 21:32:59 -0200531 locked = true;
Darrick J. Wong784e80f2020-07-14 10:37:30 -0700532 dqp->q_blk.reserved -=
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 (xfs_qcnt_t)qtrx->qt_blk_res;
534 }
535 if (qtrx->qt_ino_res) {
536 if (!locked) {
537 xfs_dqlock(dqp);
Thiago Farina667a9292012-11-12 21:32:59 -0200538 locked = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 }
Darrick J. Wong784e80f2020-07-14 10:37:30 -0700540 dqp->q_ino.reserved -=
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 (xfs_qcnt_t)qtrx->qt_ino_res;
542 }
543
544 if (qtrx->qt_rtblk_res) {
545 if (!locked) {
546 xfs_dqlock(dqp);
Thiago Farina667a9292012-11-12 21:32:59 -0200547 locked = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 }
Darrick J. Wong784e80f2020-07-14 10:37:30 -0700549 dqp->q_rtb.reserved -=
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 (xfs_qcnt_t)qtrx->qt_rtblk_res;
551 }
552 if (locked)
553 xfs_dqunlock(dqp);
554
555 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 }
557}
558
Christoph Hellwiga210c1a2010-01-17 22:36:19 +0000559STATIC void
560xfs_quota_warn(
561 struct xfs_mount *mp,
562 struct xfs_dquot *dqp,
563 int type)
564{
Darrick J. Wonge6eb6032020-07-15 17:50:57 -0700565 enum quota_type qtype;
Masatake YAMATOffc671f2016-01-04 16:10:42 +1100566
Darrick J. Wonge6eb6032020-07-15 17:50:57 -0700567 switch (xfs_dquot_type(dqp)) {
568 case XFS_DQTYPE_PROJ:
Masatake YAMATOffc671f2016-01-04 16:10:42 +1100569 qtype = PRJQUOTA;
Darrick J. Wonge6eb6032020-07-15 17:50:57 -0700570 break;
571 case XFS_DQTYPE_USER:
Masatake YAMATOffc671f2016-01-04 16:10:42 +1100572 qtype = USRQUOTA;
Darrick J. Wonge6eb6032020-07-15 17:50:57 -0700573 break;
574 case XFS_DQTYPE_GROUP:
Masatake YAMATOffc671f2016-01-04 16:10:42 +1100575 qtype = GRPQUOTA;
Darrick J. Wonge6eb6032020-07-15 17:50:57 -0700576 break;
577 default:
578 return;
579 }
Masatake YAMATOffc671f2016-01-04 16:10:42 +1100580
Darrick J. Wongc51df732020-07-14 10:37:30 -0700581 quota_send_warning(make_kqid(&init_user_ns, qtype, dqp->q_id),
Eric W. Biederman431f1972012-09-16 02:32:43 -0700582 mp->m_super->s_dev, type);
Christoph Hellwiga210c1a2010-01-17 22:36:19 +0000583}
584
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585/*
Darrick J. Wong292b47b2020-07-14 10:37:34 -0700586 * Decide if we can make an additional reservation against a quota resource.
587 * Returns an inode QUOTA_NL_ warning code and whether or not it's fatal.
588 *
589 * Note that we assume that the numeric difference between the inode and block
590 * warning codes will always be 3 since it's userspace ABI now, and will never
591 * decrease the quota reservation, so the *BELOW messages are irrelevant.
592 */
593static inline int
594xfs_dqresv_check(
595 struct xfs_dquot_res *res,
596 struct xfs_quota_limits *qlim,
597 int64_t delta,
598 bool *fatal)
599{
600 xfs_qcnt_t hardlimit = res->hardlimit;
601 xfs_qcnt_t softlimit = res->softlimit;
602 xfs_qcnt_t total_count = res->reserved + delta;
603
604 BUILD_BUG_ON(QUOTA_NL_BHARDWARN != QUOTA_NL_IHARDWARN + 3);
605 BUILD_BUG_ON(QUOTA_NL_BSOFTLONGWARN != QUOTA_NL_ISOFTLONGWARN + 3);
606 BUILD_BUG_ON(QUOTA_NL_BSOFTWARN != QUOTA_NL_ISOFTWARN + 3);
607
608 *fatal = false;
609 if (delta <= 0)
610 return QUOTA_NL_NOWARN;
611
612 if (!hardlimit)
613 hardlimit = qlim->hard;
614 if (!softlimit)
615 softlimit = qlim->soft;
616
617 if (hardlimit && total_count > hardlimit) {
618 *fatal = true;
619 return QUOTA_NL_IHARDWARN;
620 }
621
622 if (softlimit && total_count > softlimit) {
623 time64_t now = ktime_get_real_seconds();
624
625 if ((res->timer != 0 && now > res->timer) ||
626 (res->warnings != 0 && res->warnings >= qlim->warn)) {
627 *fatal = true;
628 return QUOTA_NL_ISOFTLONGWARN;
629 }
630
Darrick J. Wong4b8628d2020-07-14 10:37:35 -0700631 res->warnings++;
Darrick J. Wong292b47b2020-07-14 10:37:34 -0700632 return QUOTA_NL_ISOFTWARN;
633 }
634
635 return QUOTA_NL_NOWARN;
636}
637
638/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 * This reserves disk blocks and inodes against a dquot.
640 * Flags indicate if the dquot is to be locked here and also
641 * if the blk reservation is for RT or regular blocks.
642 * Sending in XFS_QMOPT_FORCE_RES flag skips the quota check.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 */
644STATIC int
645xfs_trans_dqresv(
Pavel Reichlaefe69a2019-11-12 17:04:02 -0800646 struct xfs_trans *tp,
647 struct xfs_mount *mp,
648 struct xfs_dquot *dqp,
649 int64_t nblks,
650 long ninos,
651 uint flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652{
Pavel Reichlc072fbe2019-11-12 17:04:26 -0800653 struct xfs_quotainfo *q = mp->m_quotainfo;
Carlos Maiolinobe607942016-02-08 11:27:55 +1100654 struct xfs_def_quota *defq;
Darrick J. Wong292b47b2020-07-14 10:37:34 -0700655 struct xfs_dquot_res *blkres;
656 struct xfs_quota_limits *qlim;
Christoph Hellwig8e9b6e72009-02-08 21:51:42 +0100657
658 xfs_dqlock(dqp);
659
Eric Sandeence6e7e79c2020-05-21 13:07:00 -0700660 defq = xfs_get_defquota(q, xfs_dquot_type(dqp));
Carlos Maiolinobe607942016-02-08 11:27:55 +1100661
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 if (flags & XFS_TRANS_DQ_RES_BLKS) {
Darrick J. Wong292b47b2020-07-14 10:37:34 -0700663 blkres = &dqp->q_blk;
664 qlim = &defq->blk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 } else {
Darrick J. Wong292b47b2020-07-14 10:37:34 -0700666 blkres = &dqp->q_rtb;
667 qlim = &defq->rtb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669
Darrick J. Wongc51df732020-07-14 10:37:30 -0700670 if ((flags & XFS_QMOPT_FORCE_RES) == 0 && dqp->q_id &&
Darrick J. Wongdbcbc7b2020-07-15 17:48:31 -0700671 xfs_dquot_is_enforced(dqp)) {
Darrick J. Wong292b47b2020-07-14 10:37:34 -0700672 int quota_nl;
673 bool fatal;
674
675 /*
676 * dquot is locked already. See if we'd go over the hardlimit
677 * or exceed the timelimit if we'd reserve resources.
678 */
679 quota_nl = xfs_dqresv_check(blkres, qlim, nblks, &fatal);
680 if (quota_nl != QUOTA_NL_NOWARN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 /*
Darrick J. Wong292b47b2020-07-14 10:37:34 -0700682 * Quota block warning codes are 3 more than the inode
683 * codes, which we check above.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 */
Darrick J. Wong292b47b2020-07-14 10:37:34 -0700685 xfs_quota_warn(mp, dqp, quota_nl + 3);
686 if (fatal)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 goto error_return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 }
Christoph Hellwig4d1f88d2010-01-13 22:05:49 +0000689
Darrick J. Wong292b47b2020-07-14 10:37:34 -0700690 quota_nl = xfs_dqresv_check(&dqp->q_ino, &defq->ino, ninos,
691 &fatal);
692 if (quota_nl != QUOTA_NL_NOWARN) {
693 xfs_quota_warn(mp, dqp, quota_nl);
694 if (fatal)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 goto error_return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 }
697 }
698
699 /*
700 * Change the reservation, but not the actual usage.
Darrick J. Wongbe37d402020-07-14 10:37:31 -0700701 * Note that q_blk.reserved = q_blk.count + resv
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 */
Darrick J. Wong292b47b2020-07-14 10:37:34 -0700703 blkres->reserved += (xfs_qcnt_t)nblks;
704 dqp->q_ino.reserved += (xfs_qcnt_t)ninos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705
706 /*
707 * note the reservation amt in the trans struct too,
708 * so that the transaction knows how much was reserved by
709 * it against this particular dquot.
710 * We don't do this when we are reserving for a delayed allocation,
711 * because we don't have the luxury of a transaction envelope then.
712 */
713 if (tp) {
714 ASSERT(tp->t_dqinfo);
715 ASSERT(flags & XFS_QMOPT_RESBLK_MASK);
716 if (nblks != 0)
717 xfs_trans_mod_dquot(tp, dqp,
718 flags & XFS_QMOPT_RESBLK_MASK,
719 nblks);
720 if (ninos != 0)
721 xfs_trans_mod_dquot(tp, dqp,
722 XFS_TRANS_DQ_RES_INOS,
723 ninos);
724 }
Darrick J. Wongbe37d402020-07-14 10:37:31 -0700725 ASSERT(dqp->q_blk.reserved >= dqp->q_blk.count);
726 ASSERT(dqp->q_rtb.reserved >= dqp->q_rtb.count);
727 ASSERT(dqp->q_ino.reserved >= dqp->q_ino.count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728
Christoph Hellwig4d1f88d2010-01-13 22:05:49 +0000729 xfs_dqunlock(dqp);
730 return 0;
731
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732error_return:
Christoph Hellwig8e9b6e72009-02-08 21:51:42 +0100733 xfs_dqunlock(dqp);
Darrick J. Wong00a342e2020-07-15 17:47:13 -0700734 if (xfs_dquot_type(dqp) == XFS_DQTYPE_PROJ)
Dave Chinner24513372014-06-25 14:58:08 +1000735 return -ENOSPC;
736 return -EDQUOT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737}
738
739
740/*
Nathan Scott9a2a7de2006-03-31 13:04:49 +1000741 * Given dquot(s), make disk block and/or inode reservations against them.
Chandra Seetharaman92f8ff72013-07-11 00:00:40 -0500742 * The fact that this does the reservation against user, group and
743 * project quotas is important, because this follows a all-or-nothing
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 * approach.
745 *
Christoph Hellwig8e9b6e72009-02-08 21:51:42 +0100746 * flags = XFS_QMOPT_FORCE_RES evades limit enforcement. Used by chown.
Nathan Scott9a2a7de2006-03-31 13:04:49 +1000747 * XFS_QMOPT_ENOSPC returns ENOSPC not EDQUOT. Used by pquota.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 * XFS_TRANS_DQ_RES_BLKS reserves regular disk blocks
749 * XFS_TRANS_DQ_RES_RTBLKS reserves realtime disk blocks
750 * dquots are unlocked on return, if they were not locked by caller.
751 */
752int
753xfs_trans_reserve_quota_bydquots(
Chandra Seetharaman113a5682013-06-27 17:25:07 -0500754 struct xfs_trans *tp,
755 struct xfs_mount *mp,
756 struct xfs_dquot *udqp,
757 struct xfs_dquot *gdqp,
Chandra Seetharaman92f8ff72013-07-11 00:00:40 -0500758 struct xfs_dquot *pdqp,
Darrick J. Wong903b1fc2019-04-17 16:30:24 -0700759 int64_t nblks,
Chandra Seetharaman113a5682013-06-27 17:25:07 -0500760 long ninos,
761 uint flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762{
Chandra Seetharaman113a5682013-06-27 17:25:07 -0500763 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764
Christoph Hellwig7d095252009-06-08 15:33:32 +0200765 if (!XFS_IS_QUOTA_RUNNING(mp) || !XFS_IS_QUOTA_ON(mp))
Nathan Scott9a2a7de2006-03-31 13:04:49 +1000766 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767
768 if (tp && tp->t_dqinfo == NULL)
769 xfs_trans_alloc_dqinfo(tp);
770
771 ASSERT(flags & XFS_QMOPT_RESBLK_MASK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772
773 if (udqp) {
Eric Sandeendcf1ccc2020-05-21 13:06:59 -0700774 error = xfs_trans_dqresv(tp, mp, udqp, nblks, ninos, flags);
Nathan Scott9a2a7de2006-03-31 13:04:49 +1000775 if (error)
776 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 }
778
779 if (gdqp) {
Eric Sandeendcf1ccc2020-05-21 13:06:59 -0700780 error = xfs_trans_dqresv(tp, mp, gdqp, nblks, ninos, flags);
Chandra Seetharaman113a5682013-06-27 17:25:07 -0500781 if (error)
782 goto unwind_usr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 }
784
Chandra Seetharaman92f8ff72013-07-11 00:00:40 -0500785 if (pdqp) {
786 error = xfs_trans_dqresv(tp, mp, pdqp, nblks, ninos, flags);
787 if (error)
788 goto unwind_grp;
789 }
790
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 /*
Nathan Scottc41564b2006-03-29 08:55:14 +1000792 * Didn't change anything critical, so, no need to log
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 */
Nathan Scott9a2a7de2006-03-31 13:04:49 +1000794 return 0;
Chandra Seetharaman113a5682013-06-27 17:25:07 -0500795
Chandra Seetharaman92f8ff72013-07-11 00:00:40 -0500796unwind_grp:
797 flags |= XFS_QMOPT_FORCE_RES;
798 if (gdqp)
799 xfs_trans_dqresv(tp, mp, gdqp, -nblks, -ninos, flags);
Chandra Seetharaman113a5682013-06-27 17:25:07 -0500800unwind_usr:
801 flags |= XFS_QMOPT_FORCE_RES;
802 if (udqp)
803 xfs_trans_dqresv(tp, mp, udqp, -nblks, -ninos, flags);
804 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805}
806
807
808/*
809 * Lock the dquot and change the reservation if we can.
810 * This doesn't change the actual usage, just the reservation.
811 * The inode sent in is locked.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 */
Christoph Hellwig7d095252009-06-08 15:33:32 +0200813int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814xfs_trans_reserve_quota_nblks(
Christoph Hellwig7d095252009-06-08 15:33:32 +0200815 struct xfs_trans *tp,
816 struct xfs_inode *ip,
Darrick J. Wong903b1fc2019-04-17 16:30:24 -0700817 int64_t nblks,
Christoph Hellwig7d095252009-06-08 15:33:32 +0200818 long ninos,
819 uint flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820{
Christoph Hellwig7d095252009-06-08 15:33:32 +0200821 struct xfs_mount *mp = ip->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822
Christoph Hellwig7d095252009-06-08 15:33:32 +0200823 if (!XFS_IS_QUOTA_RUNNING(mp) || !XFS_IS_QUOTA_ON(mp))
Nathan Scott9a2a7de2006-03-31 13:04:49 +1000824 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825
Chandra Seetharaman9cad19d2013-06-27 17:25:04 -0500826 ASSERT(!xfs_is_quota_inode(&mp->m_sb, ip->i_ino));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827
Christoph Hellwig579aa9c2008-04-22 17:34:00 +1000828 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
Eric Sandeendcf1ccc2020-05-21 13:06:59 -0700829 ASSERT((flags & ~(XFS_QMOPT_FORCE_RES)) == XFS_TRANS_DQ_RES_RTBLKS ||
830 (flags & ~(XFS_QMOPT_FORCE_RES)) == XFS_TRANS_DQ_RES_BLKS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831
832 /*
833 * Reserve nblks against these dquots, with trans as the mediator.
834 */
Christoph Hellwig7d095252009-06-08 15:33:32 +0200835 return xfs_trans_reserve_quota_bydquots(tp, mp,
836 ip->i_udquot, ip->i_gdquot,
Chandra Seetharaman92f8ff72013-07-11 00:00:40 -0500837 ip->i_pdquot,
Christoph Hellwig7d095252009-06-08 15:33:32 +0200838 nblks, ninos, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839}
840
841/*
842 * This routine is called to allocate a quotaoff log item.
843 */
Pavel Reichld0bdfb12019-11-12 17:04:27 -0800844struct xfs_qoff_logitem *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845xfs_trans_get_qoff_item(
Pavel Reichld0bdfb12019-11-12 17:04:27 -0800846 struct xfs_trans *tp,
847 struct xfs_qoff_logitem *startqoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 uint flags)
849{
Pavel Reichld0bdfb12019-11-12 17:04:27 -0800850 struct xfs_qoff_logitem *q;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851
852 ASSERT(tp != NULL);
853
854 q = xfs_qm_qoff_logitem_init(tp->t_mountp, startqoff, flags);
855 ASSERT(q != NULL);
856
857 /*
858 * Get a log_item_desc to point at the new item.
859 */
Christoph Hellwige98c4142010-06-23 18:11:15 +1000860 xfs_trans_add_item(tp, &q->qql_item);
861 return q;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862}
863
864
865/*
866 * This is called to mark the quotaoff logitem as needing
867 * to be logged when the transaction is committed. The logitem must
868 * already be associated with the given transaction.
869 */
870void
871xfs_trans_log_quotaoff_item(
Pavel Reichld0bdfb12019-11-12 17:04:27 -0800872 struct xfs_trans *tp,
873 struct xfs_qoff_logitem *qlp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 tp->t_flags |= XFS_TRANS_DIRTY;
Dave Chinnere6631f82018-05-09 07:49:37 -0700876 set_bit(XFS_LI_DIRTY, &qlp->qql_item.li_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877}
878
879STATIC void
880xfs_trans_alloc_dqinfo(
881 xfs_trans_t *tp)
882{
Carlos Maiolino32a2b112020-07-22 09:23:10 -0700883 tp->t_dqinfo = kmem_cache_zalloc(xfs_qm_dqtrxzone,
884 GFP_KERNEL | __GFP_NOFAIL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885}
886
Christoph Hellwig7d095252009-06-08 15:33:32 +0200887void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888xfs_trans_free_dqinfo(
889 xfs_trans_t *tp)
890{
891 if (!tp->t_dqinfo)
892 return;
Carlos Maiolino377bcd52019-11-14 12:43:04 -0800893 kmem_cache_free(xfs_qm_dqtrxzone, tp->t_dqinfo);
Christoph Hellwig7d095252009-06-08 15:33:32 +0200894 tp->t_dqinfo = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895}