Line data Source code
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 : #include "xfs_error.h"
20 : #include "xfs_health.h"
21 :
22 : STATIC void xfs_trans_alloc_dqinfo(xfs_trans_t *);
23 :
24 : /*
25 : * Add the locked dquot to the transaction.
26 : * The dquot must be locked, and it cannot be associated with any
27 : * transaction.
28 : */
29 : void
30 7772628115 : xfs_trans_dqjoin(
31 : struct xfs_trans *tp,
32 : struct xfs_dquot *dqp)
33 : {
34 7772628115 : ASSERT(XFS_DQ_IS_LOCKED(dqp));
35 7772620350 : ASSERT(dqp->q_logitem.qli_dquot == dqp);
36 :
37 : /*
38 : * Get a log_item_desc to point at the new item.
39 : */
40 7772620350 : xfs_trans_add_item(tp, &dqp->q_logitem.qli_item);
41 7772489635 : }
42 :
43 : /*
44 : * This is called to mark the dquot as needing
45 : * to be logged when the transaction is committed. The dquot must
46 : * already be associated with the given transaction.
47 : * Note that it marks the entire transaction as dirty. In the ordinary
48 : * case, this gets called via xfs_trans_commit, after the transaction
49 : * is already dirty. However, there's nothing stop this from getting
50 : * called directly, as done by xfs_qm_scall_setqlim. Hence, the TRANS_DIRTY
51 : * flag.
52 : */
53 : void
54 7751174852 : xfs_trans_log_dquot(
55 : struct xfs_trans *tp,
56 : struct xfs_dquot *dqp)
57 : {
58 7751174852 : ASSERT(XFS_DQ_IS_LOCKED(dqp));
59 :
60 : /* Upgrade the dquot to bigtime format if possible. */
61 7751212026 : if (dqp->q_id != 0 &&
62 572032277 : xfs_has_bigtime(tp->t_mountp) &&
63 571793410 : !(dqp->q_type & XFS_DQTYPE_BIGTIME))
64 812 : dqp->q_type |= XFS_DQTYPE_BIGTIME;
65 :
66 7751212026 : tp->t_flags |= XFS_TRANS_DIRTY;
67 7751212026 : set_bit(XFS_LI_DIRTY, &dqp->q_logitem.qli_item.li_flags);
68 7751530694 : }
69 :
70 : /*
71 : * Carry forward whatever is left of the quota blk reservation to
72 : * the spanky new transaction
73 : */
74 : void
75 1962271375 : xfs_trans_dup_dqinfo(
76 : struct xfs_trans *otp,
77 : struct xfs_trans *ntp)
78 : {
79 1962271375 : struct xfs_dqtrx *oq, *nq;
80 1962271375 : int i, j;
81 1962271375 : struct xfs_dqtrx *oqa, *nqa;
82 1962271375 : uint64_t blk_res_used;
83 :
84 1962271375 : if (!otp->t_dqinfo)
85 : return;
86 :
87 1541902892 : xfs_trans_alloc_dqinfo(ntp);
88 :
89 7705576562 : for (j = 0; j < XFS_QM_TRANS_DQTYPES; j++) {
90 4622058262 : oqa = otp->t_dqinfo->dqs[j];
91 4622708851 : nqa = ntp->t_dqinfo->dqs[j];
92 9248493352 : for (i = 0; i < XFS_QM_TRANS_MAXDQS; i++) {
93 9250327017 : blk_res_used = 0;
94 :
95 9250327017 : if (oqa[i].qt_dquot == NULL)
96 : break;
97 4626948565 : oq = &oqa[i];
98 4626948565 : nq = &nqa[i];
99 :
100 4626948565 : if (oq->qt_blk_res && oq->qt_bcount_delta > 0)
101 362248445 : blk_res_used = oq->qt_bcount_delta;
102 :
103 4626948565 : nq->qt_dquot = oq->qt_dquot;
104 4626948565 : nq->qt_bcount_delta = nq->qt_icount_delta = 0;
105 4626948565 : nq->qt_rtbcount_delta = 0;
106 :
107 : /*
108 : * Transfer whatever is left of the reservations.
109 : */
110 4626948565 : nq->qt_blk_res = oq->qt_blk_res - blk_res_used;
111 4626948565 : oq->qt_blk_res = blk_res_used;
112 :
113 4626948565 : nq->qt_rtblk_res = oq->qt_rtblk_res -
114 4626948565 : oq->qt_rtblk_res_used;
115 4626948565 : oq->qt_rtblk_res = oq->qt_rtblk_res_used;
116 :
117 4626948565 : nq->qt_ino_res = oq->qt_ino_res - oq->qt_ino_res_used;
118 4626948565 : oq->qt_ino_res = oq->qt_ino_res_used;
119 :
120 : }
121 : }
122 : }
123 :
124 : #ifdef CONFIG_XFS_LIVE_HOOKS
125 : /*
126 : * Use a static key here to reduce the overhead of quota live updates. If the
127 : * compiler supports jump labels, the static branch will be replaced by a nop
128 : * sled when there are no hook users. Online fsck is currently the only
129 : * caller, so this is a reasonable tradeoff.
130 : *
131 : * Note: Patching the kernel code requires taking the cpu hotplug lock. Other
132 : * parts of the kernel allocate memory with that lock held, which means that
133 : * XFS callers cannot hold any locks that might be used by memory reclaim or
134 : * writeback when calling the static_branch_{inc,dec} functions.
135 : */
136 : DEFINE_STATIC_XFS_HOOK_SWITCH(xfs_dqtrx_hooks_switch);
137 :
138 : void
139 34637 : xfs_dqtrx_hook_disable(void)
140 : {
141 34637 : xfs_hooks_switch_off(&xfs_dqtrx_hooks_switch);
142 34637 : }
143 :
144 : void
145 34637 : xfs_dqtrx_hook_enable(void)
146 : {
147 34637 : xfs_hooks_switch_on(&xfs_dqtrx_hooks_switch);
148 34637 : }
149 :
150 : /* Schedule a transactional dquot update on behalf of an inode. */
151 : void
152 2358640851 : xfs_trans_mod_ino_dquot(
153 : struct xfs_trans *tp,
154 : struct xfs_inode *ip,
155 : struct xfs_dquot *dqp,
156 : unsigned int field,
157 : int64_t delta)
158 : {
159 2358640851 : ASSERT(!xfs_is_metadir_inode(ip) || XFS_IS_DQDETACHED(mp, ip));
160 :
161 2358640851 : xfs_trans_mod_dquot(tp, dqp, field, delta);
162 :
163 2382363495 : if (xfs_hooks_switched_on(&xfs_dqtrx_hooks_switch)) {
164 23984308 : struct xfs_mod_ino_dqtrx_params p = {
165 23984308 : .tx_id = (uintptr_t)tp,
166 23984308 : .ino = ip->i_ino,
167 : .q_type = xfs_dquot_type(dqp),
168 23984308 : .q_id = dqp->q_id,
169 : .delta = delta
170 : };
171 23984308 : struct xfs_quotainfo *qi = tp->t_mountp->m_quotainfo;
172 :
173 23984308 : xfs_hooks_call(&qi->qi_mod_ino_dqtrx_hooks, field, &p);
174 : }
175 2358332831 : }
176 :
177 : /* Call the specified functions during a dquot counter update. */
178 : int
179 34618 : xfs_dqtrx_hook_add(
180 : struct xfs_quotainfo *qi,
181 : struct xfs_dqtrx_hook *hook)
182 : {
183 34618 : int error;
184 :
185 : /*
186 : * Transactional dquot updates first call the mod hook when changes
187 : * are attached to the transaction and then call the apply hook when
188 : * those changes are committed (or canceled).
189 : *
190 : * The apply hook must be installed before the mod hook so that we
191 : * never fail to catch the end of a quota update sequence.
192 : */
193 34618 : error = xfs_hooks_add(&qi->qi_apply_dqtrx_hooks, &hook->apply_hook);
194 34618 : if (error)
195 0 : goto out;
196 :
197 34618 : error = xfs_hooks_add(&qi->qi_mod_ino_dqtrx_hooks, &hook->mod_hook);
198 34618 : if (error)
199 0 : goto out_apply;
200 :
201 : return 0;
202 :
203 : out_apply:
204 0 : xfs_hooks_del(&qi->qi_apply_dqtrx_hooks, &hook->apply_hook);
205 : out:
206 : return error;
207 : }
208 :
209 : /* Stop calling the specified function during a dquot counter update. */
210 : void
211 34618 : xfs_dqtrx_hook_del(
212 : struct xfs_quotainfo *qi,
213 : struct xfs_dqtrx_hook *hook)
214 : {
215 : /*
216 : * The mod hook must be removed before apply hook to avoid giving the
217 : * hook consumer with an incomplete update. No hooks should be running
218 : * after these functions return.
219 : */
220 34618 : xfs_hooks_del(&qi->qi_mod_ino_dqtrx_hooks, &hook->mod_hook);
221 34618 : xfs_hooks_del(&qi->qi_apply_dqtrx_hooks, &hook->apply_hook);
222 34618 : }
223 : #endif /* CONFIG_XFS_LIVE_HOOKS */
224 :
225 : /*
226 : * Wrap around mod_dquot to account for both user and group quotas.
227 : */
228 : void
229 759744689 : xfs_trans_mod_dquot_byino(
230 : xfs_trans_t *tp,
231 : xfs_inode_t *ip,
232 : uint field,
233 : int64_t delta)
234 : {
235 759744689 : xfs_mount_t *mp = tp->t_mountp;
236 :
237 1490693557 : if (!XFS_IS_QUOTA_ON(mp) ||
238 730948868 : xfs_is_quota_inode(&mp->m_sb, ip->i_ino))
239 : return;
240 :
241 724825823 : ASSERT(!xfs_is_metadir_inode(ip) || XFS_IS_DQDETACHED(mp, ip));
242 :
243 724825823 : if (XFS_IS_UQUOTA_ON(mp) && ip->i_udquot)
244 724568988 : xfs_trans_mod_ino_dquot(tp, ip, ip->i_udquot, field, delta);
245 723968244 : if (XFS_IS_GQUOTA_ON(mp) && ip->i_gdquot)
246 722633221 : xfs_trans_mod_ino_dquot(tp, ip, ip->i_gdquot, field, delta);
247 724388738 : if (XFS_IS_PQUOTA_ON(mp) && ip->i_pdquot)
248 722826882 : xfs_trans_mod_ino_dquot(tp, ip, ip->i_pdquot, field, delta);
249 : }
250 :
251 : STATIC struct xfs_dqtrx *
252 7587548802 : xfs_trans_get_dqtrx(
253 : struct xfs_trans *tp,
254 : struct xfs_dquot *dqp)
255 : {
256 7587548802 : int i;
257 7587548802 : struct xfs_dqtrx *qa;
258 :
259 7587548802 : switch (xfs_dquot_type(dqp)) {
260 2556386619 : case XFS_DQTYPE_USER:
261 2556386619 : qa = tp->t_dqinfo->dqs[XFS_QM_TRANS_USR];
262 2556386619 : break;
263 2551777657 : case XFS_DQTYPE_GROUP:
264 2551777657 : qa = tp->t_dqinfo->dqs[XFS_QM_TRANS_GRP];
265 2551777657 : break;
266 2479384526 : case XFS_DQTYPE_PROJ:
267 2479384526 : qa = tp->t_dqinfo->dqs[XFS_QM_TRANS_PRJ];
268 2479384526 : break;
269 : default:
270 : return NULL;
271 : }
272 :
273 7635821400 : for (i = 0; i < XFS_QM_TRANS_MAXDQS; i++) {
274 7632011999 : if (qa[i].qt_dquot == NULL ||
275 : qa[i].qt_dquot == dqp)
276 7583739401 : return &qa[i];
277 : }
278 :
279 : return NULL;
280 : }
281 :
282 : /*
283 : * Make the changes in the transaction structure.
284 : * The moral equivalent to xfs_trans_mod_sb().
285 : * We don't touch any fields in the dquot, so we don't care
286 : * if it's locked or not (most of the time it won't be).
287 : */
288 : void
289 18095085590 : xfs_trans_mod_dquot(
290 : struct xfs_trans *tp,
291 : struct xfs_dquot *dqp,
292 : uint field,
293 : int64_t delta)
294 : {
295 18095085590 : struct xfs_dqtrx *qtrx;
296 :
297 18095085590 : ASSERT(tp);
298 18095085590 : ASSERT(XFS_IS_QUOTA_ON(tp->t_mountp));
299 18095085590 : qtrx = NULL;
300 :
301 18095085590 : if (!delta)
302 : return;
303 :
304 7580901661 : if (tp->t_dqinfo == NULL)
305 1490691533 : xfs_trans_alloc_dqinfo(tp);
306 : /*
307 : * Find either the first free slot or the slot that belongs
308 : * to this dquot.
309 : */
310 7581094518 : qtrx = xfs_trans_get_dqtrx(tp, dqp);
311 7581094518 : ASSERT(qtrx);
312 7581094518 : if (qtrx->qt_dquot == NULL)
313 4480363730 : qtrx->qt_dquot = dqp;
314 :
315 7581094518 : trace_xfs_trans_mod_dquot_before(qtrx);
316 7583328034 : trace_xfs_trans_mod_dquot(tp, dqp, field, delta);
317 :
318 7585491993 : switch (field) {
319 : /* regular disk blk reservation */
320 4109079379 : case XFS_TRANS_DQ_RES_BLKS:
321 4109079379 : qtrx->qt_blk_res += delta;
322 4109079379 : break;
323 :
324 : /* inode reservation */
325 406879099 : case XFS_TRANS_DQ_RES_INOS:
326 406879099 : qtrx->qt_ino_res += delta;
327 406879099 : break;
328 :
329 : /* disk blocks used. */
330 792587333 : case XFS_TRANS_DQ_BCOUNT:
331 792587333 : qtrx->qt_bcount_delta += delta;
332 792587333 : break;
333 :
334 97924565 : case XFS_TRANS_DQ_DELBCOUNT:
335 97924565 : qtrx->qt_delbcnt_delta += delta;
336 97924565 : break;
337 :
338 : /* Inode Count */
339 629013311 : case XFS_TRANS_DQ_ICOUNT:
340 629013311 : if (qtrx->qt_ino_res && delta > 0) {
341 382543584 : qtrx->qt_ino_res_used += delta;
342 382543584 : ASSERT(qtrx->qt_ino_res >= qtrx->qt_ino_res_used);
343 : }
344 629013311 : qtrx->qt_icount_delta += delta;
345 629013311 : break;
346 :
347 : /* rtblk reservation */
348 895049729 : case XFS_TRANS_DQ_RES_RTBLKS:
349 895049729 : qtrx->qt_rtblk_res += delta;
350 895049729 : break;
351 :
352 : /* rtblk count */
353 643024890 : case XFS_TRANS_DQ_RTBCOUNT:
354 643024890 : if (qtrx->qt_rtblk_res && delta > 0) {
355 444780567 : qtrx->qt_rtblk_res_used += delta;
356 444780567 : ASSERT(qtrx->qt_rtblk_res >= qtrx->qt_rtblk_res_used);
357 : }
358 643024890 : qtrx->qt_rtbcount_delta += delta;
359 643024890 : break;
360 :
361 11933687 : case XFS_TRANS_DQ_DELRTBCOUNT:
362 11933687 : qtrx->qt_delrtb_delta += delta;
363 11933687 : break;
364 :
365 0 : default:
366 0 : ASSERT(0);
367 : }
368 :
369 7585491993 : trace_xfs_trans_mod_dquot_after(qtrx);
370 : }
371 :
372 :
373 : /*
374 : * Given an array of dqtrx structures, lock all the dquots associated and join
375 : * them to the transaction, provided they have been modified.
376 : */
377 : STATIC void
378 7711395031 : xfs_trans_dqlockedjoin(
379 : struct xfs_trans *tp,
380 : struct xfs_dqtrx *q)
381 : {
382 7711395031 : unsigned int i;
383 7711395031 : ASSERT(q[0].qt_dquot != NULL);
384 7711395031 : if (q[1].qt_dquot == NULL) {
385 7675433443 : xfs_dqlock(q[0].qt_dquot);
386 7679316284 : xfs_trans_dqjoin(tp, q[0].qt_dquot);
387 35961588 : } else if (q[2].qt_dquot == NULL) {
388 35955353 : xfs_dqlock2(q[0].qt_dquot, q[1].qt_dquot);
389 36036457 : xfs_trans_dqjoin(tp, q[0].qt_dquot);
390 36036260 : xfs_trans_dqjoin(tp, q[1].qt_dquot);
391 : } else {
392 6235 : xfs_dqlockn(q);
393 31175 : for (i = 0; i < XFS_QM_TRANS_MAXDQS; i++) {
394 24940 : if (q[i].qt_dquot == NULL)
395 : break;
396 18705 : xfs_trans_dqjoin(tp, q[i].qt_dquot);
397 : }
398 : }
399 7715174840 : }
400 :
401 : /* Apply dqtrx changes to the quota reservation counters. */
402 : static inline void
403 : xfs_apply_quota_reservation_deltas(
404 : struct xfs_dquot_res *res,
405 : uint64_t reserved,
406 : int64_t res_used,
407 : int64_t count_delta)
408 : {
409 23254156578 : if (reserved != 0) {
410 : /*
411 : * Subtle math here: If reserved > res_used (the normal case),
412 : * we're simply subtracting the unused transaction quota
413 : * reservation from the dquot reservation.
414 : *
415 : * If, however, res_used > reserved, then we have allocated
416 : * more quota blocks than were reserved for the transaction.
417 : * We must add that excess to the dquot reservation since it
418 : * tracks (usage + resv) and by definition we didn't reserve
419 : * that excess.
420 : */
421 3936151041 : res->reserved -= abs(reserved - res_used);
422 19318005537 : } else if (count_delta != 0) {
423 : /*
424 : * These blks were never reserved, either inside a transaction
425 : * or outside one (in a delayed allocation). Also, this isn't
426 : * always a negative number since we sometimes deliberately
427 : * skip quota reservations.
428 : */
429 728186134 : res->reserved += count_delta;
430 : }
431 : }
432 :
433 : #ifdef CONFIG_XFS_LIVE_HOOKS
434 : /* Call downstream hooks now that it's time to apply dquot deltas. */
435 : static inline void
436 7751185468 : xfs_trans_apply_dquot_deltas_hook(
437 : struct xfs_trans *tp,
438 : struct xfs_dquot *dqp)
439 : {
440 7827483017 : if (xfs_hooks_switched_on(&xfs_dqtrx_hooks_switch)) {
441 76296311 : struct xfs_apply_dqtrx_params p = {
442 76296311 : .tx_id = (uintptr_t)tp,
443 : .q_type = xfs_dquot_type(dqp),
444 76296311 : .q_id = dqp->q_id,
445 : };
446 76296311 : struct xfs_quotainfo *qi = tp->t_mountp->m_quotainfo;
447 :
448 76296311 : xfs_hooks_call(&qi->qi_apply_dqtrx_hooks,
449 : XFS_APPLY_DQTRX_COMMIT, &p);
450 : }
451 7751209506 : }
452 : #else
453 : # define xfs_trans_apply_dquot_deltas_hook(tp, dqp) ((void)0)
454 : #endif /* CONFIG_XFS_LIVE_HOOKS */
455 :
456 : /*
457 : * Called by xfs_trans_commit() and similar in spirit to
458 : * xfs_trans_apply_sb_deltas().
459 : * Go thru all the dquots belonging to this transaction and modify the
460 : * INCORE dquot to reflect the actual usages.
461 : * Unreserve just the reservations done by this transaction.
462 : * dquot is still left locked at exit.
463 : */
464 : void
465 3352201435 : xfs_trans_apply_dquot_deltas(
466 : struct xfs_trans *tp)
467 : {
468 3352201435 : int i, j;
469 3352201435 : struct xfs_dquot *dqp;
470 3352201435 : struct xfs_dqtrx *qtrx, *qa;
471 3352201435 : int64_t totalbdelta;
472 3352201435 : int64_t totalrtbdelta;
473 :
474 3352201435 : if (!tp->t_dqinfo)
475 : return;
476 :
477 : ASSERT(tp->t_dqinfo);
478 10316158975 : for (j = 0; j < XFS_QM_TRANS_DQTYPES; j++) {
479 7736424592 : qa = tp->t_dqinfo->dqs[j];
480 7735543406 : if (qa[0].qt_dquot == NULL)
481 23738841 : continue;
482 :
483 : /*
484 : * Lock all of the dquots and join them to the transaction.
485 : */
486 7711804565 : xfs_trans_dqlockedjoin(tp, qa);
487 :
488 23178339201 : for (i = 0; i < XFS_QM_TRANS_MAXDQS; i++) {
489 15466534636 : uint64_t blk_res_used;
490 :
491 15466534636 : qtrx = &qa[i];
492 : /*
493 : * The array of dquots is filled
494 : * sequentially, not sparsely.
495 : */
496 15466534636 : if ((dqp = qtrx->qt_dquot) == NULL)
497 : break;
498 :
499 7751286076 : ASSERT(XFS_DQ_IS_LOCKED(dqp));
500 :
501 7751204285 : xfs_trans_apply_dquot_deltas_hook(tp, dqp);
502 :
503 : /*
504 : * adjust the actual number of blocks used
505 : */
506 :
507 : /*
508 : * The issue here is - sometimes we don't make a blkquota
509 : * reservation intentionally to be fair to users
510 : * (when the amount is small). On the other hand,
511 : * delayed allocs do make reservations, but that's
512 : * outside of a transaction, so we have no
513 : * idea how much was really reserved.
514 : * So, here we've accumulated delayed allocation blks and
515 : * non-delay blks. The assumption is that the
516 : * delayed ones are always reserved (outside of a
517 : * transaction), and the others may or may not have
518 : * quota reservations.
519 : */
520 7751292794 : totalbdelta = qtrx->qt_bcount_delta +
521 7751292794 : qtrx->qt_delbcnt_delta;
522 7751292794 : totalrtbdelta = qtrx->qt_rtbcount_delta +
523 7751292794 : qtrx->qt_delrtb_delta;
524 :
525 7751292794 : if (totalbdelta != 0 || totalrtbdelta != 0 ||
526 6414872610 : qtrx->qt_icount_delta != 0) {
527 1863894589 : trace_xfs_trans_apply_dquot_deltas_before(dqp);
528 1863878630 : trace_xfs_trans_apply_dquot_deltas(qtrx);
529 : }
530 :
531 : #ifdef DEBUG
532 7751260108 : if (totalbdelta < 0)
533 298714103 : ASSERT(dqp->q_blk.count >= -totalbdelta);
534 :
535 7751260108 : if (totalrtbdelta < 0)
536 148540564 : ASSERT(dqp->q_rtb.count >= -totalrtbdelta);
537 :
538 7751260108 : if (qtrx->qt_icount_delta < 0)
539 247296674 : ASSERT(dqp->q_ino.count >= -qtrx->qt_icount_delta);
540 : #endif
541 7751260108 : if (totalbdelta)
542 748423870 : dqp->q_blk.count += totalbdelta;
543 :
544 7751260108 : if (qtrx->qt_icount_delta)
545 632292285 : dqp->q_ino.count += qtrx->qt_icount_delta;
546 :
547 7751260108 : if (totalrtbdelta)
548 594535002 : dqp->q_rtb.count += totalrtbdelta;
549 :
550 7751260108 : if (totalbdelta != 0 || totalrtbdelta != 0 ||
551 6414845468 : qtrx->qt_icount_delta != 0)
552 1863886019 : trace_xfs_trans_apply_dquot_deltas_after(dqp);
553 :
554 : /*
555 : * Get any default limits in use.
556 : * Start/reset the timer(s) if needed.
557 : */
558 7751241371 : if (dqp->q_id) {
559 571952607 : xfs_qm_adjust_dqlimits(dqp);
560 571944735 : xfs_qm_adjust_dqtimers(dqp);
561 : }
562 :
563 7751215320 : dqp->q_flags |= XFS_DQFLAG_DIRTY;
564 : /*
565 : * add this to the list of items to get logged
566 : */
567 7751215320 : xfs_trans_log_dquot(tp, dqp);
568 : /*
569 : * Take off what's left of the original reservation.
570 : * In case of delayed allocations, there's no
571 : * reservation that a transaction structure knows of.
572 : */
573 7751385526 : blk_res_used = max_t(int64_t, 0, qtrx->qt_bcount_delta);
574 7751385526 : xfs_apply_quota_reservation_deltas(&dqp->q_blk,
575 : qtrx->qt_blk_res, blk_res_used,
576 : qtrx->qt_bcount_delta);
577 :
578 : /*
579 : * Adjust the RT reservation.
580 : */
581 7751385526 : xfs_apply_quota_reservation_deltas(&dqp->q_rtb,
582 : qtrx->qt_rtblk_res,
583 7751385526 : qtrx->qt_rtblk_res_used,
584 : qtrx->qt_rtbcount_delta);
585 :
586 : /*
587 : * Adjust the inode reservation.
588 : */
589 7751385526 : ASSERT(qtrx->qt_ino_res >= qtrx->qt_ino_res_used);
590 7751385526 : xfs_apply_quota_reservation_deltas(&dqp->q_ino,
591 : qtrx->qt_ino_res,
592 7751385526 : qtrx->qt_ino_res_used,
593 : qtrx->qt_icount_delta);
594 :
595 7751385526 : ASSERT(dqp->q_blk.reserved >= dqp->q_blk.count);
596 7751385526 : ASSERT(dqp->q_ino.reserved >= dqp->q_ino.count);
597 7751385526 : ASSERT(dqp->q_rtb.reserved >= dqp->q_rtb.count);
598 : }
599 : }
600 : }
601 :
602 : #ifdef CONFIG_XFS_LIVE_HOOKS
603 : /* Call downstream hooks now that it's time to cancel dquot deltas. */
604 : static inline void
605 1361645395 : xfs_trans_unreserve_and_mod_dquots_hook(
606 : struct xfs_trans *tp,
607 : struct xfs_dquot *dqp)
608 : {
609 1394235014 : if (xfs_hooks_switched_on(&xfs_dqtrx_hooks_switch)) {
610 32588682 : struct xfs_apply_dqtrx_params p = {
611 32588682 : .tx_id = (uintptr_t)tp,
612 : .q_type = xfs_dquot_type(dqp),
613 32588682 : .q_id = dqp->q_id,
614 : };
615 32588682 : struct xfs_quotainfo *qi = tp->t_mountp->m_quotainfo;
616 :
617 32588682 : xfs_hooks_call(&qi->qi_apply_dqtrx_hooks,
618 : XFS_APPLY_DQTRX_UNRESERVE, &p);
619 : }
620 1361652699 : }
621 : #else
622 : # define xfs_trans_unreserve_and_mod_dquots_hook(tp, dqp) ((void)0)
623 : #endif /* CONFIG_XFS_LIVE_HOOKS */
624 :
625 : /*
626 : * Release the reservations, and adjust the dquots accordingly.
627 : * This is called only when the transaction is being aborted. If by
628 : * any chance we have done dquot modifications incore (ie. deltas) already,
629 : * we simply throw those away, since that's the expected behavior
630 : * when a transaction is curtailed without a commit.
631 : */
632 : void
633 2104523529 : xfs_trans_unreserve_and_mod_dquots(
634 : struct xfs_trans *tp)
635 : {
636 2104523529 : int i, j;
637 2104523529 : struct xfs_dquot *dqp;
638 2104523529 : struct xfs_dqtrx *qtrx, *qa;
639 2104523529 : bool locked;
640 :
641 2104523529 : if (!tp->t_dqinfo)
642 : return;
643 :
644 1813674384 : for (j = 0; j < XFS_QM_TRANS_DQTYPES; j++) {
645 1360036354 : qa = tp->t_dqinfo->dqs[j];
646 :
647 2722192831 : for (i = 0; i < XFS_QM_TRANS_MAXDQS; i++) {
648 2722203668 : qtrx = &qa[i];
649 : /*
650 : * We assume that the array of dquots is filled
651 : * sequentially, not sparsely.
652 : */
653 2722203668 : if ((dqp = qtrx->qt_dquot) == NULL)
654 : break;
655 :
656 1361690703 : xfs_trans_unreserve_and_mod_dquots_hook(tp, dqp);
657 :
658 : /*
659 : * Unreserve the original reservation. We don't care
660 : * about the number of blocks used field, or deltas.
661 : * Also we don't bother to zero the fields.
662 : */
663 1361458725 : locked = false;
664 1361458725 : if (qtrx->qt_blk_res) {
665 1359764519 : xfs_dqlock(dqp);
666 1360002002 : locked = true;
667 1360002002 : dqp->q_blk.reserved -=
668 1360002002 : (xfs_qcnt_t)qtrx->qt_blk_res;
669 : }
670 1361696208 : if (qtrx->qt_ino_res) {
671 21851836 : if (!locked) {
672 10 : xfs_dqlock(dqp);
673 10 : locked = true;
674 : }
675 21851836 : dqp->q_ino.reserved -=
676 21851836 : (xfs_qcnt_t)qtrx->qt_ino_res;
677 : }
678 :
679 1361696208 : if (qtrx->qt_rtblk_res) {
680 377254811 : if (!locked) {
681 0 : xfs_dqlock(dqp);
682 0 : locked = true;
683 : }
684 377254811 : dqp->q_rtb.reserved -=
685 377254811 : (xfs_qcnt_t)qtrx->qt_rtblk_res;
686 : }
687 1361696208 : if (locked)
688 1359952256 : xfs_dqunlock(dqp);
689 :
690 : }
691 : }
692 : }
693 :
694 : STATIC void
695 140115 : xfs_quota_warn(
696 : struct xfs_mount *mp,
697 : struct xfs_dquot *dqp,
698 : int type)
699 : {
700 140115 : enum quota_type qtype;
701 :
702 140115 : switch (xfs_dquot_type(dqp)) {
703 : case XFS_DQTYPE_PROJ:
704 : qtype = PRJQUOTA;
705 : break;
706 87082 : case XFS_DQTYPE_USER:
707 87082 : qtype = USRQUOTA;
708 87082 : break;
709 25064 : case XFS_DQTYPE_GROUP:
710 25064 : qtype = GRPQUOTA;
711 25064 : break;
712 : default:
713 : return;
714 : }
715 :
716 140115 : quota_send_warning(make_kqid(&init_user_ns, qtype, dqp->q_id),
717 140115 : mp->m_super->s_dev, type);
718 : }
719 :
720 : /*
721 : * Decide if we can make an additional reservation against a quota resource.
722 : * Returns an inode QUOTA_NL_ warning code and whether or not it's fatal.
723 : *
724 : * Note that we assume that the numeric difference between the inode and block
725 : * warning codes will always be 3 since it's userspace ABI now, and will never
726 : * decrease the quota reservation, so the *BELOW messages are irrelevant.
727 : */
728 : static inline int
729 1181591003 : xfs_dqresv_check(
730 : struct xfs_dquot_res *res,
731 : struct xfs_quota_limits *qlim,
732 : int64_t delta,
733 : bool *fatal)
734 : {
735 1181591003 : xfs_qcnt_t hardlimit = res->hardlimit;
736 1181591003 : xfs_qcnt_t softlimit = res->softlimit;
737 1181591003 : xfs_qcnt_t total_count = res->reserved + delta;
738 :
739 1181591003 : BUILD_BUG_ON(QUOTA_NL_BHARDWARN != QUOTA_NL_IHARDWARN + 3);
740 1181591003 : BUILD_BUG_ON(QUOTA_NL_BSOFTLONGWARN != QUOTA_NL_ISOFTLONGWARN + 3);
741 1181591003 : BUILD_BUG_ON(QUOTA_NL_BSOFTWARN != QUOTA_NL_ISOFTWARN + 3);
742 :
743 1181591003 : *fatal = false;
744 1181591003 : if (delta <= 0)
745 : return QUOTA_NL_NOWARN;
746 :
747 398829453 : if (!hardlimit)
748 398225130 : hardlimit = qlim->hard;
749 398829453 : if (!softlimit)
750 398232749 : softlimit = qlim->soft;
751 :
752 398829453 : if (hardlimit && total_count > hardlimit) {
753 66171 : *fatal = true;
754 66171 : return QUOTA_NL_IHARDWARN;
755 : }
756 :
757 398763282 : if (softlimit && total_count > softlimit) {
758 73944 : time64_t now = ktime_get_real_seconds();
759 :
760 73944 : if (res->timer != 0 && now > res->timer) {
761 262 : *fatal = true;
762 262 : return QUOTA_NL_ISOFTLONGWARN;
763 : }
764 :
765 : return QUOTA_NL_ISOFTWARN;
766 : }
767 :
768 : return QUOTA_NL_NOWARN;
769 : }
770 :
771 : /*
772 : * This reserves disk blocks and inodes against a dquot.
773 : * Flags indicate if the dquot is to be locked here and also
774 : * if the blk reservation is for RT or regular blocks.
775 : * Sending in XFS_QMOPT_FORCE_RES flag skips the quota check.
776 : */
777 : STATIC int
778 8257805787 : xfs_trans_dqresv(
779 : struct xfs_trans *tp,
780 : struct xfs_mount *mp,
781 : struct xfs_dquot *dqp,
782 : int64_t nblks,
783 : long ninos,
784 : uint flags)
785 : {
786 8257805787 : struct xfs_quotainfo *q = mp->m_quotainfo;
787 8257805787 : struct xfs_def_quota *defq;
788 8257805787 : struct xfs_dquot_res *blkres;
789 8257805787 : struct xfs_quota_limits *qlim;
790 :
791 8257805787 : xfs_dqlock(dqp);
792 :
793 8256535114 : defq = xfs_get_defquota(q, xfs_dquot_type(dqp));
794 :
795 8257322193 : if (flags & XFS_TRANS_DQ_RES_BLKS) {
796 4316332324 : blkres = &dqp->q_blk;
797 4316332324 : qlim = &defq->blk;
798 : } else {
799 3940989869 : blkres = &dqp->q_rtb;
800 3940989869 : qlim = &defq->rtb;
801 : }
802 :
803 8848510819 : if ((flags & XFS_QMOPT_FORCE_RES) == 0 && dqp->q_id &&
804 591200872 : xfs_dquot_is_enforced(dqp)) {
805 590819042 : int quota_nl;
806 590819042 : bool fatal;
807 :
808 : /*
809 : * dquot is locked already. See if we'd go over the hardlimit
810 : * or exceed the timelimit if we'd reserve resources.
811 : */
812 590819042 : quota_nl = xfs_dqresv_check(blkres, qlim, nblks, &fatal);
813 590814884 : if (quota_nl != QUOTA_NL_NOWARN) {
814 : /*
815 : * Quota block warning codes are 3 more than the inode
816 : * codes, which we check above.
817 : */
818 119846 : xfs_quota_warn(mp, dqp, quota_nl + 3);
819 119846 : if (fatal)
820 46802 : goto error_return;
821 : }
822 :
823 590768082 : quota_nl = xfs_dqresv_check(&dqp->q_ino, &defq->ino, ninos,
824 : &fatal);
825 590774555 : if (quota_nl != QUOTA_NL_NOWARN) {
826 20269 : xfs_quota_warn(mp, dqp, quota_nl);
827 20269 : if (fatal)
828 19631 : goto error_return;
829 : }
830 : }
831 :
832 : /*
833 : * Change the reservation, but not the actual usage.
834 : * Note that q_blk.reserved = q_blk.count + resv
835 : */
836 8257245829 : blkres->reserved += (xfs_qcnt_t)nblks;
837 8257245829 : dqp->q_ino.reserved += (xfs_qcnt_t)ninos;
838 :
839 : /*
840 : * note the reservation amt in the trans struct too,
841 : * so that the transaction knows how much was reserved by
842 : * it against this particular dquot.
843 : * We don't do this when we are reserving for a delayed allocation,
844 : * because we don't have the luxury of a transaction envelope then.
845 : */
846 8257245829 : if (tp) {
847 7858177423 : ASSERT(flags & XFS_QMOPT_RESBLK_MASK);
848 7858177423 : xfs_trans_mod_dquot(tp, dqp, flags & XFS_QMOPT_RESBLK_MASK,
849 : nblks);
850 7856194900 : xfs_trans_mod_dquot(tp, dqp, XFS_TRANS_DQ_RES_INOS, ninos);
851 : }
852 :
853 8255757747 : if (XFS_IS_CORRUPT(mp, dqp->q_blk.reserved < dqp->q_blk.count) ||
854 8255757747 : XFS_IS_CORRUPT(mp, dqp->q_rtb.reserved < dqp->q_rtb.count) ||
855 8255757747 : XFS_IS_CORRUPT(mp, dqp->q_ino.reserved < dqp->q_ino.count))
856 0 : goto error_corrupt;
857 :
858 8255757747 : xfs_dqunlock(dqp);
859 8255757747 : return 0;
860 :
861 : error_return:
862 66433 : xfs_dqunlock(dqp);
863 66433 : if (xfs_dquot_type(dqp) == XFS_DQTYPE_PROJ)
864 1881 : return -ENOSPC;
865 : return -EDQUOT;
866 : error_corrupt:
867 0 : xfs_dqunlock(dqp);
868 0 : xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
869 0 : xfs_fs_mark_sick(mp, XFS_SICK_FS_QUOTACHECK);
870 0 : return -EFSCORRUPTED;
871 : }
872 :
873 :
874 : /*
875 : * Given dquot(s), make disk block and/or inode reservations against them.
876 : * The fact that this does the reservation against user, group and
877 : * project quotas is important, because this follows a all-or-nothing
878 : * approach.
879 : *
880 : * flags = XFS_QMOPT_FORCE_RES evades limit enforcement. Used by chown.
881 : * XFS_QMOPT_ENOSPC returns ENOSPC not EDQUOT. Used by pquota.
882 : * XFS_TRANS_DQ_RES_BLKS reserves regular disk blocks
883 : * XFS_TRANS_DQ_RES_RTBLKS reserves realtime disk blocks
884 : * dquots are unlocked on return, if they were not locked by caller.
885 : */
886 : int
887 2767377021 : xfs_trans_reserve_quota_bydquots(
888 : struct xfs_trans *tp,
889 : struct xfs_mount *mp,
890 : struct xfs_dquot *udqp,
891 : struct xfs_dquot *gdqp,
892 : struct xfs_dquot *pdqp,
893 : int64_t nblks,
894 : long ninos,
895 : uint flags)
896 : {
897 2767377021 : int error;
898 :
899 2767377021 : if (!XFS_IS_QUOTA_ON(mp))
900 : return 0;
901 :
902 2767377021 : ASSERT(flags & XFS_QMOPT_RESBLK_MASK);
903 :
904 2767377021 : if (udqp) {
905 2765249888 : error = xfs_trans_dqresv(tp, mp, udqp, nblks, ninos, flags);
906 2767500577 : if (error)
907 : return error;
908 : }
909 :
910 2769564514 : if (gdqp) {
911 2763287817 : error = xfs_trans_dqresv(tp, mp, gdqp, nblks, ninos, flags);
912 2763294369 : if (error)
913 1356 : goto unwind_usr;
914 : }
915 :
916 2769569710 : if (pdqp) {
917 2732396869 : error = xfs_trans_dqresv(tp, mp, pdqp, nblks, ninos, flags);
918 2732415922 : if (error)
919 1881 : goto unwind_grp;
920 : }
921 :
922 : /*
923 : * Didn't change anything critical, so, no need to log
924 : */
925 : return 0;
926 :
927 : unwind_grp:
928 1881 : flags |= XFS_QMOPT_FORCE_RES;
929 1881 : if (gdqp)
930 562 : xfs_trans_dqresv(tp, mp, gdqp, -nblks, -ninos, flags);
931 1319 : unwind_usr:
932 3237 : flags |= XFS_QMOPT_FORCE_RES;
933 3237 : if (udqp)
934 882 : xfs_trans_dqresv(tp, mp, udqp, -nblks, -ninos, flags);
935 : return error;
936 : }
937 :
938 :
939 : /*
940 : * Lock the dquot and change the reservation if we can.
941 : * This doesn't change the actual usage, just the reservation.
942 : * The inode sent in is locked.
943 : */
944 : int
945 1352435943 : xfs_trans_reserve_quota_nblks(
946 : struct xfs_trans *tp,
947 : struct xfs_inode *ip,
948 : int64_t dblocks,
949 : int64_t rblocks,
950 : bool force)
951 : {
952 1352435943 : struct xfs_mount *mp = ip->i_mount;
953 1352435943 : unsigned int qflags = 0;
954 1352435943 : int error;
955 :
956 1352435943 : if (!XFS_IS_QUOTA_ON(mp))
957 : return 0;
958 :
959 2609800048 : ASSERT(!xfs_is_quota_inode(&mp->m_sb, ip->i_ino));
960 1304900024 : ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
961 1304282426 : ASSERT(!xfs_is_metadir_inode(ip) || XFS_IS_DQDETACHED(mp, ip));
962 :
963 1304282426 : if (force)
964 233251875 : qflags |= XFS_QMOPT_FORCE_RES;
965 :
966 : /* Reserve data device quota against the inode's dquots. */
967 1304282426 : error = xfs_trans_reserve_quota_bydquots(tp, mp, ip->i_udquot,
968 : ip->i_gdquot, ip->i_pdquot, dblocks, 0,
969 : XFS_QMOPT_RES_REGBLKS | qflags);
970 1305643400 : if (error)
971 : return error;
972 :
973 : /* Do the same but for realtime blocks. */
974 1305497256 : error = xfs_trans_reserve_quota_bydquots(tp, mp, ip->i_udquot,
975 : ip->i_gdquot, ip->i_pdquot, rblocks, 0,
976 : XFS_QMOPT_RES_RTBLKS | qflags);
977 1305632769 : if (error) {
978 110 : xfs_trans_reserve_quota_bydquots(tp, mp, ip->i_udquot,
979 : ip->i_gdquot, ip->i_pdquot, -dblocks, 0,
980 : XFS_QMOPT_RES_REGBLKS);
981 110 : return error;
982 : }
983 :
984 : return 0;
985 : }
986 :
987 : /* Change the quota reservations for an inode creation activity. */
988 : int
989 130488806 : xfs_trans_reserve_quota_icreate(
990 : struct xfs_trans *tp,
991 : struct xfs_dquot *udqp,
992 : struct xfs_dquot *gdqp,
993 : struct xfs_dquot *pdqp,
994 : int64_t dblocks)
995 : {
996 130488806 : struct xfs_mount *mp = tp->t_mountp;
997 :
998 130488806 : if (!XFS_IS_QUOTA_ON(mp))
999 : return 0;
1000 :
1001 124331997 : return xfs_trans_reserve_quota_bydquots(tp, mp, udqp, gdqp, pdqp,
1002 : dblocks, 1, XFS_QMOPT_RES_REGBLKS);
1003 : }
1004 :
1005 : STATIC void
1006 3032095123 : xfs_trans_alloc_dqinfo(
1007 : xfs_trans_t *tp)
1008 : {
1009 3032095123 : tp->t_dqinfo = kmem_cache_zalloc(xfs_dqtrx_cache,
1010 : GFP_KERNEL | __GFP_NOFAIL);
1011 3032932941 : }
1012 :
1013 : void
1014 5458255742 : xfs_trans_free_dqinfo(
1015 : xfs_trans_t *tp)
1016 : {
1017 5458255742 : if (!tp->t_dqinfo)
1018 : return;
1019 3032976428 : kmem_cache_free(xfs_dqtrx_cache, tp->t_dqinfo);
1020 3033059635 : tp->t_dqinfo = NULL;
1021 : }
1022 :
1023 : int
1024 74646731 : xfs_quota_reserve_blkres(
1025 : struct xfs_inode *ip,
1026 : int64_t blocks)
1027 : {
1028 74646731 : if (XFS_IS_REALTIME_INODE(ip))
1029 1082898 : return xfs_trans_reserve_quota_nblks(NULL, ip, 0, blocks,
1030 : false);
1031 73563833 : return xfs_trans_reserve_quota_nblks(NULL, ip, blocks, 0, false);
1032 : }
|