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 4573388181 : xfs_trans_dqjoin(
31 : struct xfs_trans *tp,
32 : struct xfs_dquot *dqp)
33 : {
34 4573388181 : ASSERT(XFS_DQ_IS_LOCKED(dqp));
35 4573388725 : ASSERT(dqp->q_logitem.qli_dquot == dqp);
36 :
37 : /*
38 : * Get a log_item_desc to point at the new item.
39 : */
40 4573388725 : xfs_trans_add_item(tp, &dqp->q_logitem.qli_item);
41 4573418051 : }
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 4570872086 : xfs_trans_log_dquot(
55 : struct xfs_trans *tp,
56 : struct xfs_dquot *dqp)
57 : {
58 4570872086 : ASSERT(XFS_DQ_IS_LOCKED(dqp));
59 :
60 : /* Upgrade the dquot to bigtime format if possible. */
61 4570879631 : if (dqp->q_id != 0 &&
62 291314966 : xfs_has_bigtime(tp->t_mountp) &&
63 291260474 : !(dqp->q_type & XFS_DQTYPE_BIGTIME))
64 105 : dqp->q_type |= XFS_DQTYPE_BIGTIME;
65 :
66 4570879631 : tp->t_flags |= XFS_TRANS_DIRTY;
67 4570879631 : set_bit(XFS_LI_DIRTY, &dqp->q_logitem.qli_item.li_flags);
68 4570881398 : }
69 :
70 : /*
71 : * Carry forward whatever is left of the quota blk reservation to
72 : * the spanky new transaction
73 : */
74 : void
75 962336240 : xfs_trans_dup_dqinfo(
76 : struct xfs_trans *otp,
77 : struct xfs_trans *ntp)
78 : {
79 962336240 : struct xfs_dqtrx *oq, *nq;
80 962336240 : int i, j;
81 962336240 : struct xfs_dqtrx *oqa, *nqa;
82 962336240 : uint64_t blk_res_used;
83 :
84 962336240 : if (!otp->t_dqinfo)
85 : return;
86 :
87 868482414 : xfs_trans_alloc_dqinfo(ntp);
88 :
89 4342173553 : for (j = 0; j < XFS_QM_TRANS_DQTYPES; j++) {
90 2605163169 : oqa = otp->t_dqinfo->dqs[j];
91 2605163169 : nqa = ntp->t_dqinfo->dqs[j];
92 5212253152 : for (i = 0; i < XFS_QM_TRANS_MAXDQS; i++) {
93 5212628370 : blk_res_used = 0;
94 :
95 5212628370 : if (oqa[i].qt_dquot == NULL)
96 : break;
97 2607089983 : oq = &oqa[i];
98 2607089983 : nq = &nqa[i];
99 :
100 2607089983 : if (oq->qt_blk_res && oq->qt_bcount_delta > 0)
101 285204044 : blk_res_used = oq->qt_bcount_delta;
102 :
103 2607089983 : nq->qt_dquot = oq->qt_dquot;
104 2607089983 : nq->qt_bcount_delta = nq->qt_icount_delta = 0;
105 2607089983 : nq->qt_rtbcount_delta = 0;
106 :
107 : /*
108 : * Transfer whatever is left of the reservations.
109 : */
110 2607089983 : nq->qt_blk_res = oq->qt_blk_res - blk_res_used;
111 2607089983 : oq->qt_blk_res = blk_res_used;
112 :
113 2607089983 : nq->qt_rtblk_res = oq->qt_rtblk_res -
114 2607089983 : oq->qt_rtblk_res_used;
115 2607089983 : oq->qt_rtblk_res = oq->qt_rtblk_res_used;
116 :
117 2607089983 : nq->qt_ino_res = oq->qt_ino_res - oq->qt_ino_res_used;
118 2607089983 : 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 11554 : xfs_dqtrx_hook_disable(void)
140 : {
141 11554 : xfs_hooks_switch_off(&xfs_dqtrx_hooks_switch);
142 11554 : }
143 :
144 : void
145 11554 : xfs_dqtrx_hook_enable(void)
146 : {
147 11554 : xfs_hooks_switch_on(&xfs_dqtrx_hooks_switch);
148 11554 : }
149 :
150 : /* Schedule a transactional dquot update on behalf of an inode. */
151 : void
152 1193726089 : 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 1193726089 : ASSERT(!xfs_is_metadir_inode(ip) || XFS_IS_DQDETACHED(mp, ip));
160 :
161 1193726089 : xfs_trans_mod_dquot(tp, dqp, field, delta);
162 :
163 1205744560 : if (xfs_hooks_switched_on(&xfs_dqtrx_hooks_switch)) {
164 11905076 : struct xfs_mod_ino_dqtrx_params p = {
165 11905076 : .tx_id = (uintptr_t)tp,
166 11905076 : .ino = ip->i_ino,
167 : .q_type = xfs_dquot_type(dqp),
168 11905076 : .q_id = dqp->q_id,
169 : .delta = delta
170 : };
171 11905076 : struct xfs_quotainfo *qi = tp->t_mountp->m_quotainfo;
172 :
173 11905076 : xfs_hooks_call(&qi->qi_mod_ino_dqtrx_hooks, field, &p);
174 : }
175 1193862677 : }
176 :
177 : /* Call the specified functions during a dquot counter update. */
178 : int
179 11554 : xfs_dqtrx_hook_add(
180 : struct xfs_quotainfo *qi,
181 : struct xfs_dqtrx_hook *hook)
182 : {
183 11554 : 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 11554 : error = xfs_hooks_add(&qi->qi_apply_dqtrx_hooks, &hook->apply_hook);
194 11554 : if (error)
195 0 : goto out;
196 :
197 11554 : error = xfs_hooks_add(&qi->qi_mod_ino_dqtrx_hooks, &hook->mod_hook);
198 11554 : 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 11554 : 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 11554 : xfs_hooks_del(&qi->qi_mod_ino_dqtrx_hooks, &hook->mod_hook);
221 11554 : xfs_hooks_del(&qi->qi_apply_dqtrx_hooks, &hook->apply_hook);
222 11554 : }
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 378445287 : xfs_trans_mod_dquot_byino(
230 : xfs_trans_t *tp,
231 : xfs_inode_t *ip,
232 : uint field,
233 : int64_t delta)
234 : {
235 378445287 : xfs_mount_t *mp = tp->t_mountp;
236 :
237 756250079 : if (!XFS_IS_QUOTA_ON(mp) ||
238 377804792 : xfs_is_quota_inode(&mp->m_sb, ip->i_ino))
239 : return;
240 :
241 374025651 : ASSERT(!xfs_is_metadir_inode(ip) || XFS_IS_DQDETACHED(mp, ip));
242 :
243 374025651 : if (XFS_IS_UQUOTA_ON(mp) && ip->i_udquot)
244 373884824 : xfs_trans_mod_ino_dquot(tp, ip, ip->i_udquot, field, delta);
245 374015448 : if (XFS_IS_GQUOTA_ON(mp) && ip->i_gdquot)
246 373693327 : xfs_trans_mod_ino_dquot(tp, ip, ip->i_gdquot, field, delta);
247 374030675 : if (XFS_IS_PQUOTA_ON(mp) && ip->i_pdquot)
248 373717784 : xfs_trans_mod_ino_dquot(tp, ip, ip->i_pdquot, field, delta);
249 : }
250 :
251 : STATIC struct xfs_dqtrx *
252 4616283509 : xfs_trans_get_dqtrx(
253 : struct xfs_trans *tp,
254 : struct xfs_dquot *dqp)
255 : {
256 4616283509 : int i;
257 4616283509 : struct xfs_dqtrx *qa;
258 :
259 4616283509 : switch (xfs_dquot_type(dqp)) {
260 1546934907 : case XFS_DQTYPE_USER:
261 1546934907 : qa = tp->t_dqinfo->dqs[XFS_QM_TRANS_USR];
262 1546934907 : break;
263 1546292505 : case XFS_DQTYPE_GROUP:
264 1546292505 : qa = tp->t_dqinfo->dqs[XFS_QM_TRANS_GRP];
265 1546292505 : break;
266 1523056097 : case XFS_DQTYPE_PROJ:
267 1523056097 : qa = tp->t_dqinfo->dqs[XFS_QM_TRANS_PRJ];
268 1523056097 : break;
269 : default:
270 : return NULL;
271 : }
272 :
273 4632808814 : for (i = 0; i < XFS_QM_TRANS_MAXDQS; i++) {
274 4631427284 : if (qa[i].qt_dquot == NULL ||
275 : qa[i].qt_dquot == dqp)
276 4614901979 : 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 12458483227 : xfs_trans_mod_dquot(
290 : struct xfs_trans *tp,
291 : struct xfs_dquot *dqp,
292 : uint field,
293 : int64_t delta)
294 : {
295 12458483227 : struct xfs_dqtrx *qtrx;
296 :
297 12458483227 : ASSERT(tp);
298 12458483227 : ASSERT(XFS_IS_QUOTA_ON(tp->t_mountp));
299 12458483227 : qtrx = NULL;
300 :
301 12458483227 : if (!delta)
302 : return;
303 :
304 4614772382 : if (tp->t_dqinfo == NULL)
305 1025283385 : xfs_trans_alloc_dqinfo(tp);
306 : /*
307 : * Find either the first free slot or the slot that belongs
308 : * to this dquot.
309 : */
310 4614777008 : qtrx = xfs_trans_get_dqtrx(tp, dqp);
311 4614777008 : ASSERT(qtrx);
312 4614777008 : if (qtrx->qt_dquot == NULL)
313 3079785332 : qtrx->qt_dquot = dqp;
314 :
315 4614777008 : trace_xfs_trans_mod_dquot_before(qtrx);
316 4614962244 : trace_xfs_trans_mod_dquot(tp, dqp, field, delta);
317 :
318 4616180143 : switch (field) {
319 : /* regular disk blk reservation */
320 2909117272 : case XFS_TRANS_DQ_RES_BLKS:
321 2909117272 : qtrx->qt_blk_res += delta;
322 2909117272 : break;
323 :
324 : /* inode reservation */
325 204976432 : case XFS_TRANS_DQ_RES_INOS:
326 204976432 : qtrx->qt_ino_res += delta;
327 204976432 : break;
328 :
329 : /* disk blocks used. */
330 580729507 : case XFS_TRANS_DQ_BCOUNT:
331 580729507 : qtrx->qt_bcount_delta += delta;
332 580729507 : break;
333 :
334 54410571 : case XFS_TRANS_DQ_DELBCOUNT:
335 54410571 : qtrx->qt_delbcnt_delta += delta;
336 54410571 : break;
337 :
338 : /* Inode Count */
339 330398070 : case XFS_TRANS_DQ_ICOUNT:
340 330398070 : if (qtrx->qt_ino_res && delta > 0) {
341 204543300 : qtrx->qt_ino_res_used += delta;
342 204543300 : ASSERT(qtrx->qt_ino_res >= qtrx->qt_ino_res_used);
343 : }
344 330398070 : qtrx->qt_icount_delta += delta;
345 330398070 : break;
346 :
347 : /* rtblk reservation */
348 378639987 : case XFS_TRANS_DQ_RES_RTBLKS:
349 378639987 : qtrx->qt_rtblk_res += delta;
350 378639987 : break;
351 :
352 : /* rtblk count */
353 154543153 : case XFS_TRANS_DQ_RTBCOUNT:
354 154543153 : if (qtrx->qt_rtblk_res && delta > 0) {
355 91919213 : qtrx->qt_rtblk_res_used += delta;
356 91919213 : ASSERT(qtrx->qt_rtblk_res >= qtrx->qt_rtblk_res_used);
357 : }
358 154543153 : qtrx->qt_rtbcount_delta += delta;
359 154543153 : break;
360 :
361 3365151 : case XFS_TRANS_DQ_DELRTBCOUNT:
362 3365151 : qtrx->qt_delrtb_delta += delta;
363 3365151 : break;
364 :
365 0 : default:
366 0 : ASSERT(0);
367 : }
368 :
369 4616180143 : 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 4554364186 : xfs_trans_dqlockedjoin(
379 : struct xfs_trans *tp,
380 : struct xfs_dqtrx *q)
381 : {
382 4554364186 : unsigned int i;
383 4554364186 : ASSERT(q[0].qt_dquot != NULL);
384 4554364186 : if (q[1].qt_dquot == NULL) {
385 4540288810 : xfs_dqlock(q[0].qt_dquot);
386 4542731903 : xfs_trans_dqjoin(tp, q[0].qt_dquot);
387 14075376 : } else if (q[2].qt_dquot == NULL) {
388 14070836 : xfs_dqlock2(q[0].qt_dquot, q[1].qt_dquot);
389 14071151 : xfs_trans_dqjoin(tp, q[0].qt_dquot);
390 14071137 : xfs_trans_dqjoin(tp, q[1].qt_dquot);
391 : } else {
392 4540 : xfs_dqlockn(q);
393 22700 : for (i = 0; i < XFS_QM_TRANS_MAXDQS; i++) {
394 18160 : if (q[i].qt_dquot == NULL)
395 : break;
396 13620 : xfs_trans_dqjoin(tp, q[i].qt_dquot);
397 : }
398 : }
399 4556802010 : }
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 13712627880 : 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 2349555074 : res->reserved -= abs(reserved - res_used);
422 11363072806 : } 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 387414846 : 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 4570868564 : xfs_trans_apply_dquot_deltas_hook(
437 : struct xfs_trans *tp,
438 : struct xfs_dquot *dqp)
439 : {
440 4608516997 : if (xfs_hooks_switched_on(&xfs_dqtrx_hooks_switch)) {
441 37648407 : struct xfs_apply_dqtrx_params p = {
442 37648407 : .tx_id = (uintptr_t)tp,
443 : .q_type = xfs_dquot_type(dqp),
444 37648407 : .q_id = dqp->q_id,
445 : };
446 37648407 : struct xfs_quotainfo *qi = tp->t_mountp->m_quotainfo;
447 :
448 37648407 : xfs_hooks_call(&qi->qi_apply_dqtrx_hooks,
449 : XFS_APPLY_DQTRX_COMMIT, &p);
450 : }
451 4570880194 : }
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 1774613920 : xfs_trans_apply_dquot_deltas(
466 : struct xfs_trans *tp)
467 : {
468 1774613920 : int i, j;
469 1774613920 : struct xfs_dquot *dqp;
470 1774613920 : struct xfs_dqtrx *qtrx, *qa;
471 1774613920 : int64_t totalbdelta;
472 1774613920 : int64_t totalrtbdelta;
473 :
474 1774613920 : if (!tp->t_dqinfo)
475 : return;
476 :
477 : ASSERT(tp->t_dqinfo);
478 6084069059 : for (j = 0; j < XFS_QM_TRANS_DQTYPES; j++) {
479 4562453885 : qa = tp->t_dqinfo->dqs[j];
480 4562453885 : if (qa[0].qt_dquot == NULL)
481 7981247 : continue;
482 :
483 : /*
484 : * Lock all of the dquots and join them to the transaction.
485 : */
486 4554472638 : xfs_trans_dqlockedjoin(tp, qa);
487 :
488 13682143735 : for (i = 0; i < XFS_QM_TRANS_MAXDQS; i++) {
489 9129527006 : uint64_t blk_res_used;
490 :
491 9129527006 : qtrx = &qa[i];
492 : /*
493 : * The array of dquots is filled
494 : * sequentially, not sparsely.
495 : */
496 9129527006 : if ((dqp = qtrx->qt_dquot) == NULL)
497 : break;
498 :
499 4572737232 : ASSERT(XFS_DQ_IS_LOCKED(dqp));
500 :
501 4570876733 : 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 4570905516 : totalbdelta = qtrx->qt_bcount_delta +
521 4570905516 : qtrx->qt_delbcnt_delta;
522 4570905516 : totalrtbdelta = qtrx->qt_rtbcount_delta +
523 4570905516 : qtrx->qt_delrtb_delta;
524 :
525 4570905516 : if (totalbdelta != 0 || totalrtbdelta != 0 ||
526 3891366760 : qtrx->qt_icount_delta != 0) {
527 936271003 : trace_xfs_trans_apply_dquot_deltas_before(dqp);
528 936268996 : trace_xfs_trans_apply_dquot_deltas(qtrx);
529 : }
530 :
531 : #ifdef DEBUG
532 4570905318 : if (totalbdelta < 0)
533 203180240 : ASSERT(dqp->q_blk.count >= -totalbdelta);
534 :
535 4570905318 : if (totalrtbdelta < 0)
536 48639576 : ASSERT(dqp->q_rtb.count >= -totalrtbdelta);
537 :
538 4570905318 : if (qtrx->qt_icount_delta < 0)
539 125932131 : ASSERT(dqp->q_ino.count >= -qtrx->qt_icount_delta);
540 : #endif
541 4570905318 : if (totalbdelta)
542 541275570 : dqp->q_blk.count += totalbdelta;
543 :
544 4570905318 : if (qtrx->qt_icount_delta)
545 330481013 : dqp->q_ino.count += qtrx->qt_icount_delta;
546 :
547 4570905318 : if (totalrtbdelta)
548 141379971 : dqp->q_rtb.count += totalrtbdelta;
549 :
550 4570905318 : if (totalbdelta != 0 || totalrtbdelta != 0 ||
551 3891367177 : qtrx->qt_icount_delta != 0)
552 936270026 : 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 4570901587 : if (dqp->q_id) {
559 291294445 : xfs_qm_adjust_dqlimits(dqp);
560 291291812 : xfs_qm_adjust_dqtimers(dqp);
561 : }
562 :
563 4570903159 : dqp->q_flags |= XFS_DQFLAG_DIRTY;
564 : /*
565 : * add this to the list of items to get logged
566 : */
567 4570903159 : 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 4570875960 : blk_res_used = max_t(int64_t, 0, qtrx->qt_bcount_delta);
574 4570875960 : 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 4570875960 : xfs_apply_quota_reservation_deltas(&dqp->q_rtb,
582 : qtrx->qt_rtblk_res,
583 4570875960 : qtrx->qt_rtblk_res_used,
584 : qtrx->qt_rtbcount_delta);
585 :
586 : /*
587 : * Adjust the inode reservation.
588 : */
589 4570875960 : ASSERT(qtrx->qt_ino_res >= qtrx->qt_ino_res_used);
590 4570875960 : xfs_apply_quota_reservation_deltas(&dqp->q_ino,
591 : qtrx->qt_ino_res,
592 4570875960 : qtrx->qt_ino_res_used,
593 : qtrx->qt_icount_delta);
594 :
595 4570875960 : ASSERT(dqp->q_blk.reserved >= dqp->q_blk.count);
596 4570875960 : ASSERT(dqp->q_ino.reserved >= dqp->q_ino.count);
597 4570875960 : 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 1116983310 : xfs_trans_unreserve_and_mod_dquots_hook(
606 : struct xfs_trans *tp,
607 : struct xfs_dquot *dqp)
608 : {
609 1139822097 : if (xfs_hooks_switched_on(&xfs_dqtrx_hooks_switch)) {
610 22838740 : struct xfs_apply_dqtrx_params p = {
611 22838740 : .tx_id = (uintptr_t)tp,
612 : .q_type = xfs_dquot_type(dqp),
613 22838740 : .q_id = dqp->q_id,
614 : };
615 22838740 : struct xfs_quotainfo *qi = tp->t_mountp->m_quotainfo;
616 :
617 22838740 : xfs_hooks_call(&qi->qi_apply_dqtrx_hooks,
618 : XFS_APPLY_DQTRX_UNRESERVE, &p);
619 : }
620 1116984215 : }
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 1446303668 : xfs_trans_unreserve_and_mod_dquots(
634 : struct xfs_trans *tp)
635 : {
636 1446303668 : int i, j;
637 1446303668 : struct xfs_dquot *dqp;
638 1446303668 : struct xfs_dqtrx *qtrx, *qa;
639 1446303668 : bool locked;
640 :
641 1446303668 : if (!tp->t_dqinfo)
642 : return;
643 :
644 1489240561 : for (j = 0; j < XFS_QM_TRANS_DQTYPES; j++) {
645 1116906042 : qa = tp->t_dqinfo->dqs[j];
646 :
647 2233993195 : for (i = 0; i < XFS_QM_TRANS_MAXDQS; i++) {
648 2233874913 : qtrx = &qa[i];
649 : /*
650 : * We assume that the array of dquots is filled
651 : * sequentially, not sparsely.
652 : */
653 2233874913 : if ((dqp = qtrx->qt_dquot) == NULL)
654 : break;
655 :
656 1116976126 : 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 1117073700 : locked = false;
664 1117073700 : if (qtrx->qt_blk_res) {
665 1116730369 : xfs_dqlock(dqp);
666 1116778223 : locked = true;
667 1116778223 : dqp->q_blk.reserved -=
668 1116778223 : (xfs_qcnt_t)qtrx->qt_blk_res;
669 : }
670 1117121554 : if (qtrx->qt_ino_res) {
671 442035 : if (!locked) {
672 3 : xfs_dqlock(dqp);
673 3 : locked = true;
674 : }
675 442035 : dqp->q_ino.reserved -=
676 442035 : (xfs_qcnt_t)qtrx->qt_ino_res;
677 : }
678 :
679 1117121554 : if (qtrx->qt_rtblk_res) {
680 244287506 : if (!locked) {
681 0 : xfs_dqlock(dqp);
682 0 : locked = true;
683 : }
684 244287506 : dqp->q_rtb.reserved -=
685 244287506 : (xfs_qcnt_t)qtrx->qt_rtblk_res;
686 : }
687 1117121554 : if (locked)
688 1116779593 : xfs_dqunlock(dqp);
689 :
690 : }
691 : }
692 : }
693 :
694 : STATIC void
695 24561 : xfs_quota_warn(
696 : struct xfs_mount *mp,
697 : struct xfs_dquot *dqp,
698 : int type)
699 : {
700 24561 : enum quota_type qtype;
701 :
702 24561 : switch (xfs_dquot_type(dqp)) {
703 : case XFS_DQTYPE_PROJ:
704 : qtype = PRJQUOTA;
705 : break;
706 16420 : case XFS_DQTYPE_USER:
707 16420 : qtype = USRQUOTA;
708 16420 : break;
709 4027 : case XFS_DQTYPE_GROUP:
710 4027 : qtype = GRPQUOTA;
711 4027 : break;
712 : default:
713 : return;
714 : }
715 :
716 24561 : quota_send_warning(make_kqid(&init_user_ns, qtype, dqp->q_id),
717 24561 : 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 764453053 : xfs_dqresv_check(
730 : struct xfs_dquot_res *res,
731 : struct xfs_quota_limits *qlim,
732 : int64_t delta,
733 : bool *fatal)
734 : {
735 764453053 : xfs_qcnt_t hardlimit = res->hardlimit;
736 764453053 : xfs_qcnt_t softlimit = res->softlimit;
737 764453053 : xfs_qcnt_t total_count = res->reserved + delta;
738 :
739 764453053 : BUILD_BUG_ON(QUOTA_NL_BHARDWARN != QUOTA_NL_IHARDWARN + 3);
740 764453053 : BUILD_BUG_ON(QUOTA_NL_BSOFTLONGWARN != QUOTA_NL_ISOFTLONGWARN + 3);
741 764453053 : BUILD_BUG_ON(QUOTA_NL_BSOFTWARN != QUOTA_NL_ISOFTWARN + 3);
742 :
743 764453053 : *fatal = false;
744 764453053 : if (delta <= 0)
745 : return QUOTA_NL_NOWARN;
746 :
747 263668932 : if (!hardlimit)
748 263574117 : hardlimit = qlim->hard;
749 263668932 : if (!softlimit)
750 263578614 : softlimit = qlim->soft;
751 :
752 263668932 : if (hardlimit && total_count > hardlimit) {
753 13122 : *fatal = true;
754 13122 : return QUOTA_NL_IHARDWARN;
755 : }
756 :
757 263655810 : if (softlimit && total_count > softlimit) {
758 11439 : time64_t now = ktime_get_real_seconds();
759 :
760 11439 : if (res->timer != 0 && now > res->timer) {
761 44 : *fatal = true;
762 44 : 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 5768999457 : 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 5768999457 : struct xfs_quotainfo *q = mp->m_quotainfo;
787 5768999457 : struct xfs_def_quota *defq;
788 5768999457 : struct xfs_dquot_res *blkres;
789 5768999457 : struct xfs_quota_limits *qlim;
790 :
791 5768999457 : xfs_dqlock(dqp);
792 :
793 5771400402 : defq = xfs_get_defquota(q, xfs_dquot_type(dqp));
794 :
795 5772162664 : if (flags & XFS_TRANS_DQ_RES_BLKS) {
796 2982523638 : blkres = &dqp->q_blk;
797 2982523638 : qlim = &defq->blk;
798 : } else {
799 2789639026 : blkres = &dqp->q_rtb;
800 2789639026 : qlim = &defq->rtb;
801 : }
802 :
803 6154465955 : if ((flags & XFS_QMOPT_FORCE_RES) == 0 && dqp->q_id &&
804 382305336 : xfs_dquot_is_enforced(dqp)) {
805 382237545 : int quota_nl;
806 382237545 : 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 382237545 : quota_nl = xfs_dqresv_check(blkres, qlim, nblks, &fatal);
813 382237666 : 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 23879 : xfs_quota_warn(mp, dqp, quota_nl + 3);
819 23879 : if (fatal)
820 12600 : goto error_return;
821 : }
822 :
823 382225066 : quota_nl = xfs_dqresv_check(&dqp->q_ino, &defq->ino, ninos,
824 : &fatal);
825 382216713 : if (quota_nl != QUOTA_NL_NOWARN) {
826 682 : xfs_quota_warn(mp, dqp, quota_nl);
827 682 : if (fatal)
828 566 : 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 5772139221 : blkres->reserved += (xfs_qcnt_t)nblks;
837 5772139221 : 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 5772139221 : if (tp) {
847 5627714125 : ASSERT(flags & XFS_QMOPT_RESBLK_MASK);
848 5627714125 : xfs_trans_mod_dquot(tp, dqp, flags & XFS_QMOPT_RESBLK_MASK,
849 : nblks);
850 5626394087 : xfs_trans_mod_dquot(tp, dqp, XFS_TRANS_DQ_RES_INOS, ninos);
851 : }
852 :
853 5770636330 : if (XFS_IS_CORRUPT(mp, dqp->q_blk.reserved < dqp->q_blk.count) ||
854 5770636330 : XFS_IS_CORRUPT(mp, dqp->q_rtb.reserved < dqp->q_rtb.count) ||
855 5770636330 : XFS_IS_CORRUPT(mp, dqp->q_ino.reserved < dqp->q_ino.count))
856 0 : goto error_corrupt;
857 :
858 5770636330 : xfs_dqunlock(dqp);
859 5770636330 : return 0;
860 :
861 : error_return:
862 13166 : xfs_dqunlock(dqp);
863 13166 : if (xfs_dquot_type(dqp) == XFS_DQTYPE_PROJ)
864 330 : 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 1929112158 : 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 1929112158 : int error;
898 :
899 1929112158 : if (!XFS_IS_QUOTA_ON(mp))
900 : return 0;
901 :
902 1929112158 : ASSERT(flags & XFS_QMOPT_RESBLK_MASK);
903 :
904 1929112158 : if (udqp) {
905 1927746834 : error = xfs_trans_dqresv(tp, mp, udqp, nblks, ninos, flags);
906 1928211175 : if (error)
907 : return error;
908 : }
909 :
910 1929563895 : if (gdqp) {
911 1927462689 : error = xfs_trans_dqresv(tp, mp, gdqp, nblks, ninos, flags);
912 1927459594 : if (error)
913 232 : goto unwind_usr;
914 : }
915 :
916 1929560568 : if (pdqp) {
917 1916914814 : error = xfs_trans_dqresv(tp, mp, pdqp, nblks, ninos, flags);
918 1916951562 : if (error)
919 330 : 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 330 : flags |= XFS_QMOPT_FORCE_RES;
929 330 : if (gdqp)
930 100 : xfs_trans_dqresv(tp, mp, gdqp, -nblks, -ninos, flags);
931 230 : unwind_usr:
932 562 : flags |= XFS_QMOPT_FORCE_RES;
933 562 : if (udqp)
934 156 : 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 927297169 : 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 927297169 : struct xfs_mount *mp = ip->i_mount;
953 927297169 : unsigned int qflags = 0;
954 927297169 : int error;
955 :
956 927297169 : if (!XFS_IS_QUOTA_ON(mp))
957 : return 0;
958 :
959 1852191486 : ASSERT(!xfs_is_quota_inode(&mp->m_sb, ip->i_ino));
960 926095743 : ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
961 926194991 : ASSERT(!xfs_is_metadir_inode(ip) || XFS_IS_DQDETACHED(mp, ip));
962 :
963 926194991 : if (force)
964 160197653 : qflags |= XFS_QMOPT_FORCE_RES;
965 :
966 : /* Reserve data device quota against the inode's dquots. */
967 926194991 : 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 926256736 : if (error)
971 : return error;
972 :
973 : /* Do the same but for realtime blocks. */
974 926252534 : 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 926182524 : if (error) {
978 0 : 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 0 : return error;
982 : }
983 :
984 : return 0;
985 : }
986 :
987 : /* Change the quota reservations for an inode creation activity. */
988 : int
989 64500932 : 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 64500932 : struct xfs_mount *mp = tp->t_mountp;
997 :
998 64500932 : if (!XFS_IS_QUOTA_ON(mp))
999 : return 0;
1000 :
1001 64366400 : return xfs_trans_reserve_quota_bydquots(tp, mp, udqp, gdqp, pdqp,
1002 : dblocks, 1, XFS_QMOPT_RES_REGBLKS);
1003 : }
1004 :
1005 : STATIC void
1006 1893359021 : xfs_trans_alloc_dqinfo(
1007 : xfs_trans_t *tp)
1008 : {
1009 1893359021 : tp->t_dqinfo = kmem_cache_zalloc(xfs_dqtrx_cache,
1010 : GFP_KERNEL | __GFP_NOFAIL);
1011 1893581695 : }
1012 :
1013 : void
1014 3221236745 : xfs_trans_free_dqinfo(
1015 : xfs_trans_t *tp)
1016 : {
1017 3221236745 : if (!tp->t_dqinfo)
1018 : return;
1019 1893784360 : kmem_cache_free(xfs_dqtrx_cache, tp->t_dqinfo);
1020 1893749559 : tp->t_dqinfo = NULL;
1021 : }
1022 :
1023 : int
1024 24375068 : xfs_quota_reserve_blkres(
1025 : struct xfs_inode *ip,
1026 : int64_t blocks)
1027 : {
1028 24375068 : if (XFS_IS_REALTIME_INODE(ip))
1029 346956 : return xfs_trans_reserve_quota_nblks(NULL, ip, 0, blocks,
1030 : false);
1031 24028112 : return xfs_trans_reserve_quota_nblks(NULL, ip, blocks, 0, false);
1032 : }
|