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 2605370768 : xfs_trans_dqjoin(
31 : struct xfs_trans *tp,
32 : struct xfs_dquot *dqp)
33 : {
34 2605370768 : ASSERT(XFS_DQ_IS_LOCKED(dqp));
35 2605372118 : ASSERT(dqp->q_logitem.qli_dquot == dqp);
36 :
37 : /*
38 : * Get a log_item_desc to point at the new item.
39 : */
40 2605372118 : xfs_trans_add_item(tp, &dqp->q_logitem.qli_item);
41 2605361203 : }
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 2603791336 : xfs_trans_log_dquot(
55 : struct xfs_trans *tp,
56 : struct xfs_dquot *dqp)
57 : {
58 2603791336 : ASSERT(XFS_DQ_IS_LOCKED(dqp));
59 :
60 : /* Upgrade the dquot to bigtime format if possible. */
61 2603794596 : if (dqp->q_id != 0 &&
62 146962212 : xfs_has_bigtime(tp->t_mountp) &&
63 146906905 : !(dqp->q_type & XFS_DQTYPE_BIGTIME))
64 128 : dqp->q_type |= XFS_DQTYPE_BIGTIME;
65 :
66 2603794596 : tp->t_flags |= XFS_TRANS_DIRTY;
67 2603794596 : set_bit(XFS_LI_DIRTY, &dqp->q_logitem.qli_item.li_flags);
68 2603796895 : }
69 :
70 : /*
71 : * Carry forward whatever is left of the quota blk reservation to
72 : * the spanky new transaction
73 : */
74 : void
75 705398791 : xfs_trans_dup_dqinfo(
76 : struct xfs_trans *otp,
77 : struct xfs_trans *ntp)
78 : {
79 705398791 : struct xfs_dqtrx *oq, *nq;
80 705398791 : int i, j;
81 705398791 : struct xfs_dqtrx *oqa, *nqa;
82 705398791 : uint64_t blk_res_used;
83 :
84 705398791 : if (!otp->t_dqinfo)
85 : return;
86 :
87 540521313 : xfs_trans_alloc_dqinfo(ntp);
88 :
89 2702588538 : for (j = 0; j < XFS_QM_TRANS_DQTYPES; j++) {
90 1621534553 : oqa = otp->t_dqinfo->dqs[j];
91 1621534553 : nqa = ntp->t_dqinfo->dqs[j];
92 3244043843 : for (i = 0; i < XFS_QM_TRANS_MAXDQS; i++) {
93 3244186086 : blk_res_used = 0;
94 :
95 3244186086 : if (oqa[i].qt_dquot == NULL)
96 : break;
97 1622509290 : oq = &oqa[i];
98 1622509290 : nq = &nqa[i];
99 :
100 1622509290 : if (oq->qt_blk_res && oq->qt_bcount_delta > 0)
101 226012396 : blk_res_used = oq->qt_bcount_delta;
102 :
103 1622509290 : nq->qt_dquot = oq->qt_dquot;
104 1622509290 : nq->qt_bcount_delta = nq->qt_icount_delta = 0;
105 1622509290 : nq->qt_rtbcount_delta = 0;
106 :
107 : /*
108 : * Transfer whatever is left of the reservations.
109 : */
110 1622509290 : nq->qt_blk_res = oq->qt_blk_res - blk_res_used;
111 1622509290 : oq->qt_blk_res = blk_res_used;
112 :
113 1622509290 : nq->qt_rtblk_res = oq->qt_rtblk_res -
114 1622509290 : oq->qt_rtblk_res_used;
115 1622509290 : oq->qt_rtblk_res = oq->qt_rtblk_res_used;
116 :
117 1622509290 : nq->qt_ino_res = oq->qt_ino_res - oq->qt_ino_res_used;
118 1622509290 : 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 7647 : xfs_dqtrx_hook_disable(void)
140 : {
141 7647 : xfs_hooks_switch_off(&xfs_dqtrx_hooks_switch);
142 7647 : }
143 :
144 : void
145 7647 : xfs_dqtrx_hook_enable(void)
146 : {
147 7647 : xfs_hooks_switch_on(&xfs_dqtrx_hooks_switch);
148 7647 : }
149 :
150 : /* Schedule a transactional dquot update on behalf of an inode. */
151 : void
152 747647142 : 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 747647142 : xfs_trans_mod_dquot(tp, dqp, field, delta);
160 :
161 752698153 : if (xfs_hooks_switched_on(&xfs_dqtrx_hooks_switch)) {
162 5015192 : struct xfs_mod_ino_dqtrx_params p = {
163 5015192 : .tx_id = (uintptr_t)tp,
164 5015192 : .ino = ip->i_ino,
165 : .q_type = xfs_dquot_type(dqp),
166 5015192 : .q_id = dqp->q_id,
167 : .delta = delta
168 : };
169 5015192 : struct xfs_quotainfo *qi = tp->t_mountp->m_quotainfo;
170 :
171 5015192 : xfs_hooks_call(&qi->qi_mod_ino_dqtrx_hooks, field, &p);
172 : }
173 747683919 : }
174 :
175 : /* Call the specified functions during a dquot counter update. */
176 : int
177 7647 : xfs_dqtrx_hook_add(
178 : struct xfs_quotainfo *qi,
179 : struct xfs_dqtrx_hook *hook)
180 : {
181 7647 : int error;
182 :
183 : /*
184 : * Transactional dquot updates first call the mod hook when changes
185 : * are attached to the transaction and then call the apply hook when
186 : * those changes are committed (or canceled).
187 : *
188 : * The apply hook must be installed before the mod hook so that we
189 : * never fail to catch the end of a quota update sequence.
190 : */
191 7647 : error = xfs_hooks_add(&qi->qi_apply_dqtrx_hooks, &hook->apply_hook);
192 7647 : if (error)
193 0 : goto out;
194 :
195 7647 : error = xfs_hooks_add(&qi->qi_mod_ino_dqtrx_hooks, &hook->mod_hook);
196 7647 : if (error)
197 0 : goto out_apply;
198 :
199 : return 0;
200 :
201 : out_apply:
202 0 : xfs_hooks_del(&qi->qi_apply_dqtrx_hooks, &hook->apply_hook);
203 : out:
204 : return error;
205 : }
206 :
207 : /* Stop calling the specified function during a dquot counter update. */
208 : void
209 7647 : xfs_dqtrx_hook_del(
210 : struct xfs_quotainfo *qi,
211 : struct xfs_dqtrx_hook *hook)
212 : {
213 : /*
214 : * The mod hook must be removed before apply hook to avoid giving the
215 : * hook consumer with an incomplete update. No hooks should be running
216 : * after these functions return.
217 : */
218 7647 : xfs_hooks_del(&qi->qi_mod_ino_dqtrx_hooks, &hook->mod_hook);
219 7647 : xfs_hooks_del(&qi->qi_apply_dqtrx_hooks, &hook->apply_hook);
220 7647 : }
221 : #endif /* CONFIG_XFS_LIVE_HOOKS */
222 :
223 : /*
224 : * Wrap around mod_dquot to account for both user and group quotas.
225 : */
226 : void
227 300842829 : xfs_trans_mod_dquot_byino(
228 : xfs_trans_t *tp,
229 : xfs_inode_t *ip,
230 : uint field,
231 : int64_t delta)
232 : {
233 300842829 : xfs_mount_t *mp = tp->t_mountp;
234 :
235 540910048 : if (!XFS_IS_QUOTA_ON(mp) ||
236 240067219 : xfs_is_quota_inode(&mp->m_sb, ip->i_ino))
237 : return;
238 :
239 237979301 : if (XFS_IS_UQUOTA_ON(mp) && ip->i_udquot)
240 237971349 : xfs_trans_mod_ino_dquot(tp, ip, ip->i_udquot, field, delta);
241 237968817 : if (XFS_IS_GQUOTA_ON(mp) && ip->i_gdquot)
242 237775818 : xfs_trans_mod_ino_dquot(tp, ip, ip->i_gdquot, field, delta);
243 237978884 : if (XFS_IS_PQUOTA_ON(mp) && ip->i_pdquot)
244 237787385 : xfs_trans_mod_ino_dquot(tp, ip, ip->i_pdquot, field, delta);
245 : }
246 :
247 : STATIC struct xfs_dqtrx *
248 2273745206 : xfs_trans_get_dqtrx(
249 : struct xfs_trans *tp,
250 : struct xfs_dquot *dqp)
251 : {
252 2273745206 : int i;
253 2273745206 : struct xfs_dqtrx *qa;
254 :
255 2273745206 : switch (xfs_dquot_type(dqp)) {
256 764089055 : case XFS_DQTYPE_USER:
257 764089055 : qa = tp->t_dqinfo->dqs[XFS_QM_TRANS_USR];
258 764089055 : break;
259 763400435 : case XFS_DQTYPE_GROUP:
260 763400435 : qa = tp->t_dqinfo->dqs[XFS_QM_TRANS_GRP];
261 763400435 : break;
262 746255716 : case XFS_DQTYPE_PROJ:
263 746255716 : qa = tp->t_dqinfo->dqs[XFS_QM_TRANS_PRJ];
264 746255716 : break;
265 : default:
266 : return NULL;
267 : }
268 :
269 2284741551 : for (i = 0; i < XFS_QM_TRANS_MAXDQS; i++) {
270 2284088612 : if (qa[i].qt_dquot == NULL ||
271 : qa[i].qt_dquot == dqp)
272 2273092267 : return &qa[i];
273 : }
274 :
275 : return NULL;
276 : }
277 :
278 : /*
279 : * Make the changes in the transaction structure.
280 : * The moral equivalent to xfs_trans_mod_sb().
281 : * We don't touch any fields in the dquot, so we don't care
282 : * if it's locked or not (most of the time it won't be).
283 : */
284 : void
285 6190736379 : xfs_trans_mod_dquot(
286 : struct xfs_trans *tp,
287 : struct xfs_dquot *dqp,
288 : uint field,
289 : int64_t delta)
290 : {
291 6190736379 : struct xfs_dqtrx *qtrx;
292 :
293 6190736379 : ASSERT(tp);
294 6190736379 : ASSERT(XFS_IS_QUOTA_ON(tp->t_mountp));
295 6190736379 : qtrx = NULL;
296 :
297 6190736379 : if (!delta)
298 : return;
299 :
300 2272981971 : if (tp->t_dqinfo == NULL)
301 516785127 : xfs_trans_alloc_dqinfo(tp);
302 : /*
303 : * Find either the first free slot or the slot that belongs
304 : * to this dquot.
305 : */
306 2272973528 : qtrx = xfs_trans_get_dqtrx(tp, dqp);
307 2272973528 : ASSERT(qtrx);
308 2272973528 : if (qtrx->qt_dquot == NULL)
309 1552994813 : qtrx->qt_dquot = dqp;
310 :
311 2272973528 : trace_xfs_trans_mod_dquot_before(qtrx);
312 2273089231 : trace_xfs_trans_mod_dquot(tp, dqp, field, delta);
313 :
314 2273691078 : switch (field) {
315 : /* regular disk blk reservation */
316 1423618091 : case XFS_TRANS_DQ_RES_BLKS:
317 1423618091 : qtrx->qt_blk_res += delta;
318 1423618091 : break;
319 :
320 : /* inode reservation */
321 126007477 : case XFS_TRANS_DQ_RES_INOS:
322 126007477 : qtrx->qt_ino_res += delta;
323 126007477 : break;
324 :
325 : /* disk blocks used. */
326 465103512 : case XFS_TRANS_DQ_BCOUNT:
327 465103512 : qtrx->qt_bcount_delta += delta;
328 465103512 : break;
329 :
330 51586211 : case XFS_TRANS_DQ_DELBCOUNT:
331 51586211 : qtrx->qt_delbcnt_delta += delta;
332 51586211 : break;
333 :
334 : /* Inode Count */
335 207375787 : case XFS_TRANS_DQ_ICOUNT:
336 207375787 : if (qtrx->qt_ino_res && delta > 0) {
337 125574132 : qtrx->qt_ino_res_used += delta;
338 125574132 : ASSERT(qtrx->qt_ino_res >= qtrx->qt_ino_res_used);
339 : }
340 207375787 : qtrx->qt_icount_delta += delta;
341 207375787 : break;
342 :
343 : /* rtblk reservation */
344 0 : case XFS_TRANS_DQ_RES_RTBLKS:
345 0 : qtrx->qt_rtblk_res += delta;
346 0 : break;
347 :
348 : /* rtblk count */
349 0 : case XFS_TRANS_DQ_RTBCOUNT:
350 0 : if (qtrx->qt_rtblk_res && delta > 0) {
351 0 : qtrx->qt_rtblk_res_used += delta;
352 0 : ASSERT(qtrx->qt_rtblk_res >= qtrx->qt_rtblk_res_used);
353 : }
354 0 : qtrx->qt_rtbcount_delta += delta;
355 0 : break;
356 :
357 0 : case XFS_TRANS_DQ_DELRTBCOUNT:
358 0 : qtrx->qt_delrtb_delta += delta;
359 0 : break;
360 :
361 0 : default:
362 0 : ASSERT(0);
363 : }
364 :
365 2273691078 : trace_xfs_trans_mod_dquot_after(qtrx);
366 : }
367 :
368 :
369 : /*
370 : * Given an array of dqtrx structures, lock all the dquots associated and join
371 : * them to the transaction, provided they have been modified.
372 : */
373 : STATIC void
374 2592866231 : xfs_trans_dqlockedjoin(
375 : struct xfs_trans *tp,
376 : struct xfs_dqtrx *q)
377 : {
378 2592866231 : unsigned int i;
379 2592866231 : ASSERT(q[0].qt_dquot != NULL);
380 2592866231 : if (q[1].qt_dquot == NULL) {
381 2583256848 : xfs_dqlock(q[0].qt_dquot);
382 2584564920 : xfs_trans_dqjoin(tp, q[0].qt_dquot);
383 9609383 : } else if (q[2].qt_dquot == NULL) {
384 9607207 : xfs_dqlock2(q[0].qt_dquot, q[1].qt_dquot);
385 9607636 : xfs_trans_dqjoin(tp, q[0].qt_dquot);
386 9607630 : xfs_trans_dqjoin(tp, q[1].qt_dquot);
387 : } else {
388 2176 : xfs_dqlockn(q);
389 10880 : for (i = 0; i < XFS_QM_TRANS_MAXDQS; i++) {
390 8704 : if (q[i].qt_dquot == NULL)
391 : break;
392 6528 : xfs_trans_dqjoin(tp, q[i].qt_dquot);
393 : }
394 : }
395 2594177722 : }
396 :
397 : /* Apply dqtrx changes to the quota reservation counters. */
398 : static inline void
399 : xfs_apply_quota_reservation_deltas(
400 : struct xfs_dquot_res *res,
401 : uint64_t reserved,
402 : int64_t res_used,
403 : int64_t count_delta)
404 : {
405 7811344503 : if (reserved != 0) {
406 : /*
407 : * Subtle math here: If reserved > res_used (the normal case),
408 : * we're simply subtracting the unused transaction quota
409 : * reservation from the dquot reservation.
410 : *
411 : * If, however, res_used > reserved, then we have allocated
412 : * more quota blocks than were reserved for the transaction.
413 : * We must add that excess to the dquot reservation since it
414 : * tracks (usage + resv) and by definition we didn't reserve
415 : * that excess.
416 : */
417 1139122799 : res->reserved -= abs(reserved - res_used);
418 6672221704 : } else if (count_delta != 0) {
419 : /*
420 : * These blks were never reserved, either inside a transaction
421 : * or outside one (in a delayed allocation). Also, this isn't
422 : * always a negative number since we sometimes deliberately
423 : * skip quota reservations.
424 : */
425 244347777 : res->reserved += count_delta;
426 : }
427 : }
428 :
429 : #ifdef CONFIG_XFS_LIVE_HOOKS
430 : /* Call downstream hooks now that it's time to apply dquot deltas. */
431 : static inline void
432 2603786159 : xfs_trans_apply_dquot_deltas_hook(
433 : struct xfs_trans *tp,
434 : struct xfs_dquot *dqp)
435 : {
436 2620167914 : if (xfs_hooks_switched_on(&xfs_dqtrx_hooks_switch)) {
437 16381748 : struct xfs_apply_dqtrx_params p = {
438 16381748 : .tx_id = (uintptr_t)tp,
439 : .q_type = xfs_dquot_type(dqp),
440 16381748 : .q_id = dqp->q_id,
441 : };
442 16381748 : struct xfs_quotainfo *qi = tp->t_mountp->m_quotainfo;
443 :
444 16381748 : xfs_hooks_call(&qi->qi_apply_dqtrx_hooks,
445 : XFS_APPLY_DQTRX_COMMIT, &p);
446 : }
447 2603788159 : }
448 : #else
449 : # define xfs_trans_apply_dquot_deltas_hook(tp, dqp) ((void)0)
450 : #endif /* CONFIG_XFS_LIVE_HOOKS */
451 :
452 : /*
453 : * Called by xfs_trans_commit() and similar in spirit to
454 : * xfs_trans_apply_sb_deltas().
455 : * Go thru all the dquots belonging to this transaction and modify the
456 : * INCORE dquot to reflect the actual usages.
457 : * Unreserve just the reservations done by this transaction.
458 : * dquot is still left locked at exit.
459 : */
460 : void
461 1326454738 : xfs_trans_apply_dquot_deltas(
462 : struct xfs_trans *tp)
463 : {
464 1326454738 : int i, j;
465 1326454738 : struct xfs_dquot *dqp;
466 1326454738 : struct xfs_dqtrx *qtrx, *qa;
467 1326454738 : int64_t totalbdelta;
468 1326454738 : int64_t totalrtbdelta;
469 :
470 1326454738 : if (!tp->t_dqinfo)
471 : return;
472 :
473 : ASSERT(tp->t_dqinfo);
474 3465460434 : for (j = 0; j < XFS_QM_TRANS_DQTYPES; j++) {
475 2598785557 : qa = tp->t_dqinfo->dqs[j];
476 2598785557 : if (qa[0].qt_dquot == NULL)
477 5817640 : continue;
478 :
479 : /*
480 : * Lock all of the dquots and join them to the transaction.
481 : */
482 2592967917 : xfs_trans_dqlockedjoin(tp, qa);
483 :
484 7790918602 : for (i = 0; i < XFS_QM_TRANS_MAXDQS; i++) {
485 5198891878 : uint64_t blk_res_used;
486 :
487 5198891878 : qtrx = &qa[i];
488 : /*
489 : * The array of dquots is filled
490 : * sequentially, not sparsely.
491 : */
492 5198891878 : if ((dqp = qtrx->qt_dquot) == NULL)
493 : break;
494 :
495 2604722563 : ASSERT(XFS_DQ_IS_LOCKED(dqp));
496 :
497 2603781086 : xfs_trans_apply_dquot_deltas_hook(tp, dqp);
498 :
499 : /*
500 : * adjust the actual number of blocks used
501 : */
502 :
503 : /*
504 : * The issue here is - sometimes we don't make a blkquota
505 : * reservation intentionally to be fair to users
506 : * (when the amount is small). On the other hand,
507 : * delayed allocs do make reservations, but that's
508 : * outside of a transaction, so we have no
509 : * idea how much was really reserved.
510 : * So, here we've accumulated delayed allocation blks and
511 : * non-delay blks. The assumption is that the
512 : * delayed ones are always reserved (outside of a
513 : * transaction), and the others may or may not have
514 : * quota reservations.
515 : */
516 2603800015 : totalbdelta = qtrx->qt_bcount_delta +
517 2603800015 : qtrx->qt_delbcnt_delta;
518 2603800015 : totalrtbdelta = qtrx->qt_rtbcount_delta +
519 2603800015 : qtrx->qt_delrtb_delta;
520 :
521 2603800015 : if (totalbdelta != 0 || totalrtbdelta != 0 ||
522 2172875361 : qtrx->qt_icount_delta != 0) {
523 593000776 : trace_xfs_trans_apply_dquot_deltas_before(dqp);
524 592998857 : trace_xfs_trans_apply_dquot_deltas(qtrx);
525 : }
526 :
527 : #ifdef DEBUG
528 2603797820 : if (totalbdelta < 0)
529 156012515 : ASSERT(dqp->q_blk.count >= -totalbdelta);
530 :
531 2603797820 : if (totalrtbdelta < 0)
532 0 : ASSERT(dqp->q_rtb.count >= -totalrtbdelta);
533 :
534 2603797820 : if (qtrx->qt_icount_delta < 0)
535 81860725 : ASSERT(dqp->q_ino.count >= -qtrx->qt_icount_delta);
536 : #endif
537 2603797820 : if (totalbdelta)
538 430924280 : dqp->q_blk.count += totalbdelta;
539 :
540 2603797820 : if (qtrx->qt_icount_delta)
541 207442821 : dqp->q_ino.count += qtrx->qt_icount_delta;
542 :
543 2603797820 : if (totalrtbdelta)
544 0 : dqp->q_rtb.count += totalrtbdelta;
545 :
546 2603797820 : if (totalbdelta != 0 || totalrtbdelta != 0 ||
547 2172873787 : qtrx->qt_icount_delta != 0)
548 593000031 : trace_xfs_trans_apply_dquot_deltas_after(dqp);
549 :
550 : /*
551 : * Get any default limits in use.
552 : * Start/reset the timer(s) if needed.
553 : */
554 2603795636 : if (dqp->q_id) {
555 146941904 : xfs_qm_adjust_dqlimits(dqp);
556 146939328 : xfs_qm_adjust_dqtimers(dqp);
557 : }
558 :
559 2603796284 : dqp->q_flags |= XFS_DQFLAG_DIRTY;
560 : /*
561 : * add this to the list of items to get logged
562 : */
563 2603796284 : xfs_trans_log_dquot(tp, dqp);
564 : /*
565 : * Take off what's left of the original reservation.
566 : * In case of delayed allocations, there's no
567 : * reservation that a transaction structure knows of.
568 : */
569 2603781501 : blk_res_used = max_t(int64_t, 0, qtrx->qt_bcount_delta);
570 2603781501 : xfs_apply_quota_reservation_deltas(&dqp->q_blk,
571 : qtrx->qt_blk_res, blk_res_used,
572 : qtrx->qt_bcount_delta);
573 :
574 : /*
575 : * Adjust the RT reservation.
576 : */
577 2603781501 : xfs_apply_quota_reservation_deltas(&dqp->q_rtb,
578 : qtrx->qt_rtblk_res,
579 2603781501 : qtrx->qt_rtblk_res_used,
580 : qtrx->qt_rtbcount_delta);
581 :
582 : /*
583 : * Adjust the inode reservation.
584 : */
585 2603781501 : ASSERT(qtrx->qt_ino_res >= qtrx->qt_ino_res_used);
586 2603781501 : xfs_apply_quota_reservation_deltas(&dqp->q_ino,
587 : qtrx->qt_ino_res,
588 2603781501 : qtrx->qt_ino_res_used,
589 : qtrx->qt_icount_delta);
590 :
591 2603781501 : ASSERT(dqp->q_blk.reserved >= dqp->q_blk.count);
592 2603781501 : ASSERT(dqp->q_ino.reserved >= dqp->q_ino.count);
593 2603781501 : ASSERT(dqp->q_rtb.reserved >= dqp->q_rtb.count);
594 : }
595 : }
596 : }
597 :
598 : #ifdef CONFIG_XFS_LIVE_HOOKS
599 : /* Call downstream hooks now that it's time to cancel dquot deltas. */
600 : static inline void
601 572280038 : xfs_trans_unreserve_and_mod_dquots_hook(
602 : struct xfs_trans *tp,
603 : struct xfs_dquot *dqp)
604 : {
605 586885285 : if (xfs_hooks_switched_on(&xfs_dqtrx_hooks_switch)) {
606 14605193 : struct xfs_apply_dqtrx_params p = {
607 14605193 : .tx_id = (uintptr_t)tp,
608 : .q_type = xfs_dquot_type(dqp),
609 14605193 : .q_id = dqp->q_id,
610 : };
611 14605193 : struct xfs_quotainfo *qi = tp->t_mountp->m_quotainfo;
612 :
613 14605193 : xfs_hooks_call(&qi->qi_apply_dqtrx_hooks,
614 : XFS_APPLY_DQTRX_UNRESERVE, &p);
615 : }
616 572281115 : }
617 : #else
618 : # define xfs_trans_unreserve_and_mod_dquots_hook(tp, dqp) ((void)0)
619 : #endif /* CONFIG_XFS_LIVE_HOOKS */
620 :
621 : /*
622 : * Release the reservations, and adjust the dquots accordingly.
623 : * This is called only when the transaction is being aborted. If by
624 : * any chance we have done dquot modifications incore (ie. deltas) already,
625 : * we simply throw those away, since that's the expected behavior
626 : * when a transaction is curtailed without a commit.
627 : */
628 : void
629 1401781403 : xfs_trans_unreserve_and_mod_dquots(
630 : struct xfs_trans *tp)
631 : {
632 1401781403 : int i, j;
633 1401781403 : struct xfs_dquot *dqp;
634 1401781403 : struct xfs_dqtrx *qtrx, *qa;
635 1401781403 : bool locked;
636 :
637 1401781403 : if (!tp->t_dqinfo)
638 : return;
639 :
640 763027624 : for (j = 0; j < XFS_QM_TRANS_DQTYPES; j++) {
641 572262246 : qa = tp->t_dqinfo->dqs[j];
642 :
643 1144627506 : for (i = 0; i < XFS_QM_TRANS_MAXDQS; i++) {
644 1144536343 : qtrx = &qa[i];
645 : /*
646 : * We assume that the array of dquots is filled
647 : * sequentially, not sparsely.
648 : */
649 1144536343 : if ((dqp = qtrx->qt_dquot) == NULL)
650 : break;
651 :
652 572276149 : xfs_trans_unreserve_and_mod_dquots_hook(tp, dqp);
653 :
654 : /*
655 : * Unreserve the original reservation. We don't care
656 : * about the number of blocks used field, or deltas.
657 : * Also we don't bother to zero the fields.
658 : */
659 572364163 : locked = false;
660 572364163 : if (qtrx->qt_blk_res) {
661 572081329 : xfs_dqlock(dqp);
662 572096670 : locked = true;
663 572096670 : dqp->q_blk.reserved -=
664 572096670 : (xfs_qcnt_t)qtrx->qt_blk_res;
665 : }
666 572379504 : if (qtrx->qt_ino_res) {
667 440922 : if (!locked) {
668 0 : xfs_dqlock(dqp);
669 0 : locked = true;
670 : }
671 440922 : dqp->q_ino.reserved -=
672 440922 : (xfs_qcnt_t)qtrx->qt_ino_res;
673 : }
674 :
675 572379504 : if (qtrx->qt_rtblk_res) {
676 0 : if (!locked) {
677 0 : xfs_dqlock(dqp);
678 0 : locked = true;
679 : }
680 0 : dqp->q_rtb.reserved -=
681 0 : (xfs_qcnt_t)qtrx->qt_rtblk_res;
682 : }
683 572379504 : if (locked)
684 572095642 : xfs_dqunlock(dqp);
685 :
686 : }
687 : }
688 : }
689 :
690 : STATIC void
691 26532 : xfs_quota_warn(
692 : struct xfs_mount *mp,
693 : struct xfs_dquot *dqp,
694 : int type)
695 : {
696 26532 : enum quota_type qtype;
697 :
698 26532 : switch (xfs_dquot_type(dqp)) {
699 : case XFS_DQTYPE_PROJ:
700 : qtype = PRJQUOTA;
701 : break;
702 18391 : case XFS_DQTYPE_USER:
703 18391 : qtype = USRQUOTA;
704 18391 : break;
705 4027 : case XFS_DQTYPE_GROUP:
706 4027 : qtype = GRPQUOTA;
707 4027 : break;
708 : default:
709 : return;
710 : }
711 :
712 26532 : quota_send_warning(make_kqid(&init_user_ns, qtype, dqp->q_id),
713 26532 : mp->m_super->s_dev, type);
714 : }
715 :
716 : /*
717 : * Decide if we can make an additional reservation against a quota resource.
718 : * Returns an inode QUOTA_NL_ warning code and whether or not it's fatal.
719 : *
720 : * Note that we assume that the numeric difference between the inode and block
721 : * warning codes will always be 3 since it's userspace ABI now, and will never
722 : * decrease the quota reservation, so the *BELOW messages are irrelevant.
723 : */
724 : static inline int
725 382407189 : xfs_dqresv_check(
726 : struct xfs_dquot_res *res,
727 : struct xfs_quota_limits *qlim,
728 : int64_t delta,
729 : bool *fatal)
730 : {
731 382407189 : xfs_qcnt_t hardlimit = res->hardlimit;
732 382407189 : xfs_qcnt_t softlimit = res->softlimit;
733 382407189 : xfs_qcnt_t total_count = res->reserved + delta;
734 :
735 382407189 : BUILD_BUG_ON(QUOTA_NL_BHARDWARN != QUOTA_NL_IHARDWARN + 3);
736 382407189 : BUILD_BUG_ON(QUOTA_NL_BSOFTLONGWARN != QUOTA_NL_ISOFTLONGWARN + 3);
737 382407189 : BUILD_BUG_ON(QUOTA_NL_BSOFTWARN != QUOTA_NL_ISOFTWARN + 3);
738 :
739 382407189 : *fatal = false;
740 382407189 : if (delta <= 0)
741 : return QUOTA_NL_NOWARN;
742 :
743 94366804 : if (!hardlimit)
744 94275888 : hardlimit = qlim->hard;
745 94366804 : if (!softlimit)
746 94277606 : softlimit = qlim->soft;
747 :
748 94366804 : if (hardlimit && total_count > hardlimit) {
749 15093 : *fatal = true;
750 15093 : return QUOTA_NL_IHARDWARN;
751 : }
752 :
753 94351711 : if (softlimit && total_count > softlimit) {
754 11439 : time64_t now = ktime_get_real_seconds();
755 :
756 11439 : if (res->timer != 0 && now > res->timer) {
757 44 : *fatal = true;
758 44 : return QUOTA_NL_ISOFTLONGWARN;
759 : }
760 :
761 : return QUOTA_NL_ISOFTWARN;
762 : }
763 :
764 : return QUOTA_NL_NOWARN;
765 : }
766 :
767 : /*
768 : * This reserves disk blocks and inodes against a dquot.
769 : * Flags indicate if the dquot is to be locked here and also
770 : * if the blk reservation is for RT or regular blocks.
771 : * Sending in XFS_QMOPT_FORCE_RES flag skips the quota check.
772 : */
773 : STATIC int
774 2840063809 : xfs_trans_dqresv(
775 : struct xfs_trans *tp,
776 : struct xfs_mount *mp,
777 : struct xfs_dquot *dqp,
778 : int64_t nblks,
779 : long ninos,
780 : uint flags)
781 : {
782 2840063809 : struct xfs_quotainfo *q = mp->m_quotainfo;
783 2840063809 : struct xfs_def_quota *defq;
784 2840063809 : struct xfs_dquot_res *blkres;
785 2840063809 : struct xfs_quota_limits *qlim;
786 :
787 2840063809 : xfs_dqlock(dqp);
788 :
789 2840894126 : defq = xfs_get_defquota(q, xfs_dquot_type(dqp));
790 :
791 2841176914 : if (flags & XFS_TRANS_DQ_RES_BLKS) {
792 1483621271 : blkres = &dqp->q_blk;
793 1483621271 : qlim = &defq->blk;
794 : } else {
795 1357555643 : blkres = &dqp->q_rtb;
796 1357555643 : qlim = &defq->rtb;
797 : }
798 :
799 3032413733 : if ((flags & XFS_QMOPT_FORCE_RES) == 0 && dqp->q_id &&
800 191236764 : xfs_dquot_is_enforced(dqp)) {
801 191210069 : int quota_nl;
802 191210069 : bool fatal;
803 :
804 : /*
805 : * dquot is locked already. See if we'd go over the hardlimit
806 : * or exceed the timelimit if we'd reserve resources.
807 : */
808 191210069 : quota_nl = xfs_dqresv_check(blkres, qlim, nblks, &fatal);
809 191209887 : if (quota_nl != QUOTA_NL_NOWARN) {
810 : /*
811 : * Quota block warning codes are 3 more than the inode
812 : * codes, which we check above.
813 : */
814 26048 : xfs_quota_warn(mp, dqp, quota_nl + 3);
815 26048 : if (fatal)
816 14769 : goto error_return;
817 : }
818 :
819 191195118 : quota_nl = xfs_dqresv_check(&dqp->q_ino, &defq->ino, ninos,
820 : &fatal);
821 191193677 : if (quota_nl != QUOTA_NL_NOWARN) {
822 484 : xfs_quota_warn(mp, dqp, quota_nl);
823 484 : if (fatal)
824 368 : goto error_return;
825 : }
826 : }
827 :
828 : /*
829 : * Change the reservation, but not the actual usage.
830 : * Note that q_blk.reserved = q_blk.count + resv
831 : */
832 2841160209 : blkres->reserved += (xfs_qcnt_t)nblks;
833 2841160209 : dqp->q_ino.reserved += (xfs_qcnt_t)ninos;
834 :
835 : /*
836 : * note the reservation amt in the trans struct too,
837 : * so that the transaction knows how much was reserved by
838 : * it against this particular dquot.
839 : * We don't do this when we are reserving for a delayed allocation,
840 : * because we don't have the luxury of a transaction envelope then.
841 : */
842 2841160209 : if (tp) {
843 2717720257 : ASSERT(flags & XFS_QMOPT_RESBLK_MASK);
844 2717720257 : xfs_trans_mod_dquot(tp, dqp, flags & XFS_QMOPT_RESBLK_MASK,
845 : nblks);
846 2717391606 : xfs_trans_mod_dquot(tp, dqp, XFS_TRANS_DQ_RES_INOS, ninos);
847 : }
848 :
849 2840764404 : if (XFS_IS_CORRUPT(mp, dqp->q_blk.reserved < dqp->q_blk.count) ||
850 2840764404 : XFS_IS_CORRUPT(mp, dqp->q_rtb.reserved < dqp->q_rtb.count) ||
851 2840764404 : XFS_IS_CORRUPT(mp, dqp->q_ino.reserved < dqp->q_ino.count))
852 0 : goto error_corrupt;
853 :
854 2840764404 : xfs_dqunlock(dqp);
855 2840764404 : return 0;
856 :
857 : error_return:
858 15137 : xfs_dqunlock(dqp);
859 15137 : if (xfs_dquot_type(dqp) == XFS_DQTYPE_PROJ)
860 330 : return -ENOSPC;
861 : return -EDQUOT;
862 : error_corrupt:
863 0 : xfs_dqunlock(dqp);
864 0 : xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
865 0 : xfs_fs_mark_sick(mp, XFS_SICK_FS_QUOTACHECK);
866 0 : return -EFSCORRUPTED;
867 : }
868 :
869 :
870 : /*
871 : * Given dquot(s), make disk block and/or inode reservations against them.
872 : * The fact that this does the reservation against user, group and
873 : * project quotas is important, because this follows a all-or-nothing
874 : * approach.
875 : *
876 : * flags = XFS_QMOPT_FORCE_RES evades limit enforcement. Used by chown.
877 : * XFS_QMOPT_ENOSPC returns ENOSPC not EDQUOT. Used by pquota.
878 : * XFS_TRANS_DQ_RES_BLKS reserves regular disk blocks
879 : * XFS_TRANS_DQ_RES_RTBLKS reserves realtime disk blocks
880 : * dquots are unlocked on return, if they were not locked by caller.
881 : */
882 : int
883 949138272 : xfs_trans_reserve_quota_bydquots(
884 : struct xfs_trans *tp,
885 : struct xfs_mount *mp,
886 : struct xfs_dquot *udqp,
887 : struct xfs_dquot *gdqp,
888 : struct xfs_dquot *pdqp,
889 : int64_t nblks,
890 : long ninos,
891 : uint flags)
892 : {
893 949138272 : int error;
894 :
895 949138272 : if (!XFS_IS_QUOTA_ON(mp))
896 : return 0;
897 :
898 949138272 : ASSERT(flags & XFS_QMOPT_RESBLK_MASK);
899 :
900 949138272 : if (udqp) {
901 948739346 : error = xfs_trans_dqresv(tp, mp, udqp, nblks, ninos, flags);
902 948917899 : if (error)
903 : return error;
904 : }
905 :
906 949302250 : if (gdqp) {
907 948176891 : error = xfs_trans_dqresv(tp, mp, gdqp, nblks, ninos, flags);
908 948176456 : if (error)
909 232 : goto unwind_usr;
910 : }
911 :
912 949301583 : if (pdqp) {
913 944385674 : error = xfs_trans_dqresv(tp, mp, pdqp, nblks, ninos, flags);
914 944391219 : if (error)
915 330 : goto unwind_grp;
916 : }
917 :
918 : /*
919 : * Didn't change anything critical, so, no need to log
920 : */
921 : return 0;
922 :
923 : unwind_grp:
924 330 : flags |= XFS_QMOPT_FORCE_RES;
925 330 : if (gdqp)
926 100 : xfs_trans_dqresv(tp, mp, gdqp, -nblks, -ninos, flags);
927 230 : unwind_usr:
928 562 : flags |= XFS_QMOPT_FORCE_RES;
929 562 : if (udqp)
930 156 : xfs_trans_dqresv(tp, mp, udqp, -nblks, -ninos, flags);
931 : return error;
932 : }
933 :
934 :
935 : /*
936 : * Lock the dquot and change the reservation if we can.
937 : * This doesn't change the actual usage, just the reservation.
938 : * The inode sent in is locked.
939 : */
940 : int
941 601024233 : xfs_trans_reserve_quota_nblks(
942 : struct xfs_trans *tp,
943 : struct xfs_inode *ip,
944 : int64_t dblocks,
945 : int64_t rblocks,
946 : bool force)
947 : {
948 601024233 : struct xfs_mount *mp = ip->i_mount;
949 601024233 : unsigned int qflags = 0;
950 601024233 : int error;
951 :
952 601024233 : if (!XFS_IS_QUOTA_ON(mp))
953 : return 0;
954 :
955 905544102 : ASSERT(!xfs_is_quota_inode(&mp->m_sb, ip->i_ino));
956 452772051 : ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
957 :
958 452784858 : if (force)
959 90468537 : qflags |= XFS_QMOPT_FORCE_RES;
960 :
961 : /* Reserve data device quota against the inode's dquots. */
962 452784858 : error = xfs_trans_reserve_quota_bydquots(tp, mp, ip->i_udquot,
963 : ip->i_gdquot, ip->i_pdquot, dblocks, 0,
964 : XFS_QMOPT_RES_REGBLKS | qflags);
965 452818029 : if (error)
966 : return error;
967 :
968 : /* Do the same but for realtime blocks. */
969 452808553 : error = xfs_trans_reserve_quota_bydquots(tp, mp, ip->i_udquot,
970 : ip->i_gdquot, ip->i_pdquot, rblocks, 0,
971 : XFS_QMOPT_RES_RTBLKS | qflags);
972 452809858 : if (error) {
973 0 : xfs_trans_reserve_quota_bydquots(tp, mp, ip->i_udquot,
974 : ip->i_gdquot, ip->i_pdquot, -dblocks, 0,
975 : XFS_QMOPT_RES_REGBLKS);
976 0 : return error;
977 : }
978 :
979 : return 0;
980 : }
981 :
982 : /* Change the quota reservations for an inode creation activity. */
983 : int
984 69579989 : xfs_trans_reserve_quota_icreate(
985 : struct xfs_trans *tp,
986 : struct xfs_dquot *udqp,
987 : struct xfs_dquot *gdqp,
988 : struct xfs_dquot *pdqp,
989 : int64_t dblocks)
990 : {
991 69579989 : struct xfs_mount *mp = tp->t_mountp;
992 :
993 69579989 : if (!XFS_IS_QUOTA_ON(mp))
994 : return 0;
995 :
996 39229680 : return xfs_trans_reserve_quota_bydquots(tp, mp, udqp, gdqp, pdqp,
997 : dblocks, 1, XFS_QMOPT_RES_REGBLKS);
998 : }
999 :
1000 : STATIC void
1001 1057120623 : xfs_trans_alloc_dqinfo(
1002 : xfs_trans_t *tp)
1003 : {
1004 1057120623 : tp->t_dqinfo = kmem_cache_zalloc(xfs_dqtrx_cache,
1005 : GFP_KERNEL | __GFP_NOFAIL);
1006 1057168272 : }
1007 :
1008 : void
1009 2728355280 : xfs_trans_free_dqinfo(
1010 : xfs_trans_t *tp)
1011 : {
1012 2728355280 : if (!tp->t_dqinfo)
1013 : return;
1014 1057362188 : kmem_cache_free(xfs_dqtrx_cache, tp->t_dqinfo);
1015 1057422234 : tp->t_dqinfo = NULL;
1016 : }
|