1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Copyright (c) 2000-2005 Silicon Graphics, Inc.
4 * All Rights Reserved.
5 */
6 #include "xfs_platform.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_bit.h"
13 #include "xfs_sb.h"
14 #include "xfs_mount.h"
15 #include "xfs_inode.h"
16 #include "xfs_iwalk.h"
17 #include "xfs_quota.h"
18 #include "xfs_bmap.h"
19 #include "xfs_bmap_util.h"
20 #include "xfs_trans.h"
21 #include "xfs_trans_space.h"
22 #include "xfs_qm.h"
23 #include "xfs_trace.h"
24 #include "xfs_icache.h"
25 #include "xfs_error.h"
26 #include "xfs_ag.h"
27 #include "xfs_ialloc.h"
28 #include "xfs_log_priv.h"
29 #include "xfs_health.h"
30 #include "xfs_da_format.h"
31 #include "xfs_metafile.h"
32 #include "xfs_rtgroup.h"
33
34 /*
35 * The global quota manager. There is only one of these for the entire
36 * system, _not_ one per file system. XQM keeps track of the overall
37 * quota functionality, including maintaining the freelist and hash
38 * tables of dquots.
39 */
40 STATIC int xfs_qm_init_quotainos(struct xfs_mount *mp);
41 STATIC int xfs_qm_init_quotainfo(struct xfs_mount *mp);
42
43 STATIC void xfs_qm_dqfree_one(struct xfs_dquot *dqp);
44 /*
45 * We use the batch lookup interface to iterate over the dquots as it
46 * currently is the only interface into the radix tree code that allows
47 * fuzzy lookups instead of exact matches. Holding the lock over multiple
48 * operations is fine as all callers are used either during mount/umount
49 * or quotaoff.
50 */
51 #define XFS_DQ_LOOKUP_BATCH 32
52
53 STATIC int
xfs_qm_dquot_walk(struct xfs_mount * mp,xfs_dqtype_t type,int (* execute)(struct xfs_dquot * dqp,void * data),void * data)54 xfs_qm_dquot_walk(
55 struct xfs_mount *mp,
56 xfs_dqtype_t type,
57 int (*execute)(struct xfs_dquot *dqp, void *data),
58 void *data)
59 {
60 struct xfs_quotainfo *qi = mp->m_quotainfo;
61 struct radix_tree_root *tree = xfs_dquot_tree(qi, type);
62 uint32_t next_index;
63 int last_error = 0;
64 int skipped;
65 int nr_found;
66
67 restart:
68 skipped = 0;
69 next_index = 0;
70 nr_found = 0;
71
72 while (1) {
73 struct xfs_dquot *batch[XFS_DQ_LOOKUP_BATCH];
74 int error;
75 int i;
76
77 mutex_lock(&qi->qi_tree_lock);
78 nr_found = radix_tree_gang_lookup(tree, (void **)batch,
79 next_index, XFS_DQ_LOOKUP_BATCH);
80 if (!nr_found) {
81 mutex_unlock(&qi->qi_tree_lock);
82 break;
83 }
84
85 for (i = 0; i < nr_found; i++) {
86 struct xfs_dquot *dqp = batch[i];
87
88 next_index = dqp->q_id + 1;
89
90 error = execute(batch[i], data);
91 if (error == -EAGAIN) {
92 skipped++;
93 continue;
94 }
95 if (error && last_error != -EFSCORRUPTED)
96 last_error = error;
97 }
98
99 mutex_unlock(&qi->qi_tree_lock);
100
101 /* bail out if the filesystem is corrupted. */
102 if (last_error == -EFSCORRUPTED) {
103 skipped = 0;
104 break;
105 }
106 /* we're done if id overflows back to zero */
107 if (!next_index)
108 break;
109 }
110
111 if (skipped) {
112 delay(1);
113 goto restart;
114 }
115
116 return last_error;
117 }
118
119
120 /*
121 * Purge a dquot from all tracking data structures and free it.
122 */
123 STATIC int
xfs_qm_dqpurge(struct xfs_dquot * dqp,void * data)124 xfs_qm_dqpurge(
125 struct xfs_dquot *dqp,
126 void *data)
127 {
128 struct xfs_quotainfo *qi = dqp->q_mount->m_quotainfo;
129
130 spin_lock(&dqp->q_lockref.lock);
131 if (dqp->q_lockref.count > 0 || lockref_is_dead(&dqp->q_lockref)) {
132 spin_unlock(&dqp->q_lockref.lock);
133 return -EAGAIN;
134 }
135 lockref_mark_dead(&dqp->q_lockref);
136 spin_unlock(&dqp->q_lockref.lock);
137
138 mutex_lock(&dqp->q_qlock);
139 xfs_qm_dqunpin_wait(dqp);
140 xfs_dqflock(dqp);
141
142 /*
143 * If we are turning this type of quotas off, we don't care
144 * about the dirty metadata sitting in this dquot. OTOH, if
145 * we're unmounting, we do care, so we flush it and wait.
146 */
147 if (XFS_DQ_IS_DIRTY(dqp)) {
148 struct xfs_buf *bp = NULL;
149 int error;
150
151 /*
152 * We don't care about getting disk errors here. We need
153 * to purge this dquot anyway, so we go ahead regardless.
154 */
155 error = xfs_dquot_use_attached_buf(dqp, &bp);
156 if (error == -EAGAIN) {
157 /* resurrect the refcount from the dead. */
158 dqp->q_lockref.count = 0;
159 goto out_funlock;
160 }
161 if (!bp)
162 goto out_funlock;
163
164 /*
165 * dqflush completes dqflock on error, and the bwrite ioend
166 * does it on success.
167 */
168 error = xfs_qm_dqflush(dqp, bp);
169 if (!error)
170 error = xfs_bwrite(bp);
171 xfs_buf_relse(bp);
172 xfs_dqflock(dqp);
173 }
174 xfs_dquot_detach_buf(dqp);
175
176 out_funlock:
177 ASSERT(atomic_read(&dqp->q_pincount) == 0);
178 ASSERT(xlog_is_shutdown(dqp->q_logitem.qli_item.li_log) ||
179 !test_bit(XFS_LI_IN_AIL, &dqp->q_logitem.qli_item.li_flags));
180
181 xfs_dqfunlock(dqp);
182 mutex_unlock(&dqp->q_qlock);
183
184 radix_tree_delete(xfs_dquot_tree(qi, xfs_dquot_type(dqp)), dqp->q_id);
185 qi->qi_dquots--;
186
187 /*
188 * We move dquots to the freelist as soon as their reference count
189 * hits zero, so it really should be on the freelist here.
190 */
191 ASSERT(!list_empty(&dqp->q_lru));
192 list_lru_del_obj(&qi->qi_lru, &dqp->q_lru);
193 XFS_STATS_DEC(dqp->q_mount, xs_qm_dquot_unused);
194
195 xfs_qm_dqdestroy(dqp);
196 return 0;
197 }
198
199 /*
200 * Purge the dquot cache.
201 */
202 static void
xfs_qm_dqpurge_all(struct xfs_mount * mp)203 xfs_qm_dqpurge_all(
204 struct xfs_mount *mp)
205 {
206 xfs_qm_dquot_walk(mp, XFS_DQTYPE_USER, xfs_qm_dqpurge, NULL);
207 xfs_qm_dquot_walk(mp, XFS_DQTYPE_GROUP, xfs_qm_dqpurge, NULL);
208 xfs_qm_dquot_walk(mp, XFS_DQTYPE_PROJ, xfs_qm_dqpurge, NULL);
209 }
210
211 /*
212 * Just destroy the quotainfo structure.
213 */
214 void
xfs_qm_unmount(struct xfs_mount * mp)215 xfs_qm_unmount(
216 struct xfs_mount *mp)
217 {
218 if (mp->m_quotainfo) {
219 xfs_qm_dqpurge_all(mp);
220 xfs_qm_destroy_quotainfo(mp);
221 }
222 }
223
224 static void
xfs_qm_unmount_rt(struct xfs_mount * mp)225 xfs_qm_unmount_rt(
226 struct xfs_mount *mp)
227 {
228 struct xfs_rtgroup *rtg = xfs_rtgroup_grab(mp, 0);
229
230 if (!rtg)
231 return;
232 if (rtg_bitmap(rtg))
233 xfs_qm_dqdetach(rtg_bitmap(rtg));
234 if (rtg_summary(rtg))
235 xfs_qm_dqdetach(rtg_summary(rtg));
236 xfs_rtgroup_rele(rtg);
237 }
238
239 STATIC void
xfs_qm_destroy_quotainos(struct xfs_quotainfo * qi)240 xfs_qm_destroy_quotainos(
241 struct xfs_quotainfo *qi)
242 {
243 if (qi->qi_uquotaip) {
244 xfs_irele(qi->qi_uquotaip);
245 qi->qi_uquotaip = NULL; /* paranoia */
246 }
247 if (qi->qi_gquotaip) {
248 xfs_irele(qi->qi_gquotaip);
249 qi->qi_gquotaip = NULL;
250 }
251 if (qi->qi_pquotaip) {
252 xfs_irele(qi->qi_pquotaip);
253 qi->qi_pquotaip = NULL;
254 }
255 if (qi->qi_dirip) {
256 xfs_irele(qi->qi_dirip);
257 qi->qi_dirip = NULL;
258 }
259 }
260
261 /*
262 * Called from the vfsops layer.
263 */
264 void
xfs_qm_unmount_quotas(xfs_mount_t * mp)265 xfs_qm_unmount_quotas(
266 xfs_mount_t *mp)
267 {
268 /*
269 * Release the dquots that root inode, et al might be holding,
270 * before we flush quotas and blow away the quotainfo structure.
271 */
272 ASSERT(mp->m_rootip);
273 xfs_qm_dqdetach(mp->m_rootip);
274
275 /*
276 * For pre-RTG file systems, the RT inodes have quotas attached,
277 * detach them now.
278 */
279 if (!xfs_has_rtgroups(mp))
280 xfs_qm_unmount_rt(mp);
281
282 /*
283 * Release the quota inodes.
284 */
285 if (mp->m_quotainfo)
286 xfs_qm_destroy_quotainos(mp->m_quotainfo);
287 }
288
289 static bool
xfs_qm_need_dqattach(struct xfs_inode * ip)290 xfs_qm_need_dqattach(
291 struct xfs_inode *ip)
292 {
293 struct xfs_mount *mp = ip->i_mount;
294
295 if (!XFS_IS_QUOTA_ON(mp))
296 return false;
297 if (!XFS_NOT_DQATTACHED(mp, ip))
298 return false;
299 if (xfs_is_quota_inode(&mp->m_sb, I_INO(ip)))
300 return false;
301 if (xfs_is_metadir_inode(ip))
302 return false;
303 return true;
304 }
305
306 /*
307 * Given a locked inode, attach dquot(s) to it, taking U/G/P-QUOTAON
308 * into account.
309 * If @doalloc is true, the dquot(s) will be allocated if needed.
310 * Inode may get unlocked and relocked in here, and the caller must deal with
311 * the consequences.
312 */
313 int
xfs_qm_dqattach_locked(xfs_inode_t * ip,bool doalloc)314 xfs_qm_dqattach_locked(
315 xfs_inode_t *ip,
316 bool doalloc)
317 {
318 xfs_mount_t *mp = ip->i_mount;
319 int error = 0;
320
321 if (!xfs_qm_need_dqattach(ip))
322 return 0;
323
324 xfs_assert_ilocked(ip, XFS_ILOCK_EXCL);
325 ASSERT(!xfs_is_metadir_inode(ip));
326
327 if (XFS_IS_UQUOTA_ON(mp) && !ip->i_udquot) {
328 error = xfs_qm_dqget_inode(ip, XFS_DQTYPE_USER,
329 doalloc, &ip->i_udquot);
330 if (error)
331 goto done;
332 ASSERT(ip->i_udquot);
333 }
334
335 if (XFS_IS_GQUOTA_ON(mp) && !ip->i_gdquot) {
336 error = xfs_qm_dqget_inode(ip, XFS_DQTYPE_GROUP,
337 doalloc, &ip->i_gdquot);
338 if (error)
339 goto done;
340 ASSERT(ip->i_gdquot);
341 }
342
343 if (XFS_IS_PQUOTA_ON(mp) && !ip->i_pdquot) {
344 error = xfs_qm_dqget_inode(ip, XFS_DQTYPE_PROJ,
345 doalloc, &ip->i_pdquot);
346 if (error)
347 goto done;
348 ASSERT(ip->i_pdquot);
349 }
350
351 done:
352 /*
353 * Don't worry about the dquots that we may have attached before any
354 * error - they'll get detached later if it has not already been done.
355 */
356 xfs_assert_ilocked(ip, XFS_ILOCK_EXCL);
357 return error;
358 }
359
360 int
xfs_qm_dqattach(struct xfs_inode * ip)361 xfs_qm_dqattach(
362 struct xfs_inode *ip)
363 {
364 int error;
365
366 if (!xfs_qm_need_dqattach(ip))
367 return 0;
368
369 xfs_ilock(ip, XFS_ILOCK_EXCL);
370 error = xfs_qm_dqattach_locked(ip, false);
371 xfs_iunlock(ip, XFS_ILOCK_EXCL);
372
373 return error;
374 }
375
376 /*
377 * Release dquots (and their references) if any.
378 * The inode should be locked EXCL except when this's called by
379 * xfs_ireclaim.
380 */
381 void
xfs_qm_dqdetach(xfs_inode_t * ip)382 xfs_qm_dqdetach(
383 xfs_inode_t *ip)
384 {
385 if (xfs_is_metadir_inode(ip))
386 return;
387 if (!(ip->i_udquot || ip->i_gdquot || ip->i_pdquot))
388 return;
389
390 trace_xfs_dquot_dqdetach(ip);
391
392 ASSERT(!xfs_is_quota_inode(&ip->i_mount->m_sb, I_INO(ip)));
393 if (ip->i_udquot) {
394 xfs_qm_dqrele(ip->i_udquot);
395 ip->i_udquot = NULL;
396 }
397 if (ip->i_gdquot) {
398 xfs_qm_dqrele(ip->i_gdquot);
399 ip->i_gdquot = NULL;
400 }
401 if (ip->i_pdquot) {
402 xfs_qm_dqrele(ip->i_pdquot);
403 ip->i_pdquot = NULL;
404 }
405 }
406
407 struct xfs_qm_isolate {
408 struct list_head buffers;
409 struct list_head dispose;
410 };
411
412 static enum lru_status
xfs_qm_dquot_isolate(struct list_head * item,struct list_lru_one * lru,void * arg)413 xfs_qm_dquot_isolate(
414 struct list_head *item,
415 struct list_lru_one *lru,
416 void *arg)
417 __releases(&lru->lock) __acquires(&lru->lock)
418 {
419 struct xfs_dquot *dqp = container_of(item,
420 struct xfs_dquot, q_lru);
421 struct xfs_qm_isolate *isol = arg;
422 enum lru_status ret = LRU_SKIP;
423
424 if (!spin_trylock(&dqp->q_lockref.lock))
425 goto out_miss_busy;
426
427 /*
428 * If something else is freeing this dquot and hasn't yet removed it
429 * from the LRU, leave it for the freeing task to complete the freeing
430 * process rather than risk it being free from under us here.
431 */
432 if (lockref_is_dead(&dqp->q_lockref))
433 goto out_miss_unlock;
434
435 /*
436 * If the dquot is pinned or dirty, rotate it to the end of the LRU to
437 * give some time for it to be cleaned before we try to isolate it
438 * again.
439 */
440 ret = LRU_ROTATE;
441 if (XFS_DQ_IS_DIRTY(dqp) || atomic_read(&dqp->q_pincount) > 0)
442 goto out_miss_unlock;
443
444 /*
445 * This dquot has acquired a reference in the meantime remove it from
446 * the freelist and try again.
447 */
448 if (dqp->q_lockref.count) {
449 spin_unlock(&dqp->q_lockref.lock);
450 XFS_STATS_INC(dqp->q_mount, xs_qm_dqwants);
451
452 trace_xfs_dqreclaim_want(dqp);
453 list_lru_isolate(lru, &dqp->q_lru);
454 XFS_STATS_DEC(dqp->q_mount, xs_qm_dquot_unused);
455 return LRU_REMOVED;
456 }
457
458 /*
459 * The dquot may still be under IO, in which case the flush lock will be
460 * held. If we can't get the flush lock now, just skip over the dquot as
461 * if it was dirty.
462 */
463 if (!xfs_dqflock_nowait(dqp))
464 goto out_miss_unlock;
465
466 ASSERT(!XFS_DQ_IS_DIRTY(dqp));
467 xfs_dquot_detach_buf(dqp);
468 xfs_dqfunlock(dqp);
469
470 /*
471 * Prevent lookups now that we are past the point of no return.
472 */
473 lockref_mark_dead(&dqp->q_lockref);
474 spin_unlock(&dqp->q_lockref.lock);
475
476 list_lru_isolate_move(lru, &dqp->q_lru, &isol->dispose);
477 XFS_STATS_DEC(dqp->q_mount, xs_qm_dquot_unused);
478 trace_xfs_dqreclaim_done(dqp);
479 XFS_STATS_INC(dqp->q_mount, xs_qm_dqreclaims);
480 return LRU_REMOVED;
481
482 out_miss_unlock:
483 spin_unlock(&dqp->q_lockref.lock);
484 out_miss_busy:
485 trace_xfs_dqreclaim_busy(dqp);
486 XFS_STATS_INC(dqp->q_mount, xs_qm_dqreclaim_misses);
487 return ret;
488 }
489
490 static unsigned long
xfs_qm_shrink_scan(struct shrinker * shrink,struct shrink_control * sc)491 xfs_qm_shrink_scan(
492 struct shrinker *shrink,
493 struct shrink_control *sc)
494 {
495 struct xfs_quotainfo *qi = shrink->private_data;
496 struct xfs_qm_isolate isol;
497 unsigned long freed;
498 int error;
499
500 if ((sc->gfp_mask & (__GFP_FS|__GFP_DIRECT_RECLAIM)) != (__GFP_FS|__GFP_DIRECT_RECLAIM))
501 return 0;
502
503 INIT_LIST_HEAD(&isol.buffers);
504 INIT_LIST_HEAD(&isol.dispose);
505
506 freed = list_lru_shrink_walk(&qi->qi_lru, sc,
507 xfs_qm_dquot_isolate, &isol);
508
509 error = xfs_buf_delwri_submit(&isol.buffers);
510 if (error)
511 xfs_warn(NULL, "%s: dquot reclaim failed", __func__);
512
513 while (!list_empty(&isol.dispose)) {
514 struct xfs_dquot *dqp;
515
516 dqp = list_first_entry(&isol.dispose, struct xfs_dquot, q_lru);
517 list_del_init(&dqp->q_lru);
518 xfs_qm_dqfree_one(dqp);
519 }
520
521 return freed;
522 }
523
524 static unsigned long
xfs_qm_shrink_count(struct shrinker * shrink,struct shrink_control * sc)525 xfs_qm_shrink_count(
526 struct shrinker *shrink,
527 struct shrink_control *sc)
528 {
529 struct xfs_quotainfo *qi = shrink->private_data;
530
531 return list_lru_shrink_count(&qi->qi_lru, sc);
532 }
533
534 STATIC void
xfs_qm_set_defquota(struct xfs_mount * mp,xfs_dqtype_t type,struct xfs_quotainfo * qinf)535 xfs_qm_set_defquota(
536 struct xfs_mount *mp,
537 xfs_dqtype_t type,
538 struct xfs_quotainfo *qinf)
539 {
540 struct xfs_dquot *dqp;
541 struct xfs_def_quota *defq;
542 int error;
543
544 error = xfs_qm_dqget_uncached(mp, 0, type, &dqp);
545 if (error)
546 return;
547
548 defq = xfs_get_defquota(qinf, xfs_dquot_type(dqp));
549
550 /*
551 * Timers and warnings have been already set, let's just set the
552 * default limits for this quota type
553 */
554 defq->blk.hard = dqp->q_blk.hardlimit;
555 defq->blk.soft = dqp->q_blk.softlimit;
556 defq->ino.hard = dqp->q_ino.hardlimit;
557 defq->ino.soft = dqp->q_ino.softlimit;
558 defq->rtb.hard = dqp->q_rtb.hardlimit;
559 defq->rtb.soft = dqp->q_rtb.softlimit;
560 xfs_qm_dqdestroy(dqp);
561 }
562
563 /* Initialize quota time limits from the root dquot. */
564 static void
xfs_qm_init_timelimits(struct xfs_mount * mp,xfs_dqtype_t type)565 xfs_qm_init_timelimits(
566 struct xfs_mount *mp,
567 xfs_dqtype_t type)
568 {
569 struct xfs_quotainfo *qinf = mp->m_quotainfo;
570 struct xfs_def_quota *defq;
571 struct xfs_dquot *dqp;
572 int error;
573
574 defq = xfs_get_defquota(qinf, type);
575
576 defq->blk.time = XFS_QM_BTIMELIMIT;
577 defq->ino.time = XFS_QM_ITIMELIMIT;
578 defq->rtb.time = XFS_QM_RTBTIMELIMIT;
579
580 /*
581 * We try to get the limits from the superuser's limits fields.
582 * This is quite hacky, but it is standard quota practice.
583 *
584 * Since we may not have done a quotacheck by this point, just read
585 * the dquot without attaching it to any hashtables or lists.
586 */
587 error = xfs_qm_dqget_uncached(mp, 0, type, &dqp);
588 if (error)
589 return;
590
591 /*
592 * The warnings and timers set the grace period given to
593 * a user or group before he or she can not perform any
594 * more writing. If it is zero, a default is used.
595 */
596 if (dqp->q_blk.timer)
597 defq->blk.time = dqp->q_blk.timer;
598 if (dqp->q_ino.timer)
599 defq->ino.time = dqp->q_ino.timer;
600 if (dqp->q_rtb.timer)
601 defq->rtb.time = dqp->q_rtb.timer;
602
603 xfs_qm_dqdestroy(dqp);
604 }
605
606 static int
xfs_qm_load_metadir_qinos(struct xfs_mount * mp,struct xfs_quotainfo * qi)607 xfs_qm_load_metadir_qinos(
608 struct xfs_mount *mp,
609 struct xfs_quotainfo *qi)
610 {
611 struct xfs_trans *tp;
612 int error;
613
614 tp = xfs_trans_alloc_empty(mp);
615 error = xfs_dqinode_load_parent(tp, &qi->qi_dirip);
616 if (error == -ENOENT) {
617 /* no quota dir directory, but we'll create one later */
618 error = 0;
619 goto out_trans;
620 }
621 if (error)
622 goto out_trans;
623
624 if (XFS_IS_UQUOTA_ON(mp)) {
625 error = xfs_dqinode_load(tp, qi->qi_dirip, XFS_DQTYPE_USER,
626 &qi->qi_uquotaip);
627 if (error && error != -ENOENT)
628 goto out_trans;
629 }
630
631 if (XFS_IS_GQUOTA_ON(mp)) {
632 error = xfs_dqinode_load(tp, qi->qi_dirip, XFS_DQTYPE_GROUP,
633 &qi->qi_gquotaip);
634 if (error && error != -ENOENT)
635 goto out_trans;
636 }
637
638 if (XFS_IS_PQUOTA_ON(mp)) {
639 error = xfs_dqinode_load(tp, qi->qi_dirip, XFS_DQTYPE_PROJ,
640 &qi->qi_pquotaip);
641 if (error && error != -ENOENT)
642 goto out_trans;
643 }
644
645 error = 0;
646 out_trans:
647 xfs_trans_cancel(tp);
648 return error;
649 }
650
651 /* Create quota inodes in the metadata directory tree. */
652 STATIC int
xfs_qm_create_metadir_qinos(struct xfs_mount * mp,struct xfs_quotainfo * qi)653 xfs_qm_create_metadir_qinos(
654 struct xfs_mount *mp,
655 struct xfs_quotainfo *qi)
656 {
657 int error;
658
659 if (!qi->qi_dirip) {
660 error = xfs_dqinode_mkdir_parent(mp, &qi->qi_dirip);
661 if (error && error != -EEXIST)
662 return error;
663 /*
664 * If the /quotas dirent points to an inode that isn't
665 * loadable, qi_dirip will be NULL but mkdir_parent will return
666 * -EEXIST. In this case the metadir is corrupt, so bail out.
667 */
668 if (XFS_IS_CORRUPT(mp, qi->qi_dirip == NULL))
669 return -EFSCORRUPTED;
670 }
671
672 if (XFS_IS_UQUOTA_ON(mp) && !qi->qi_uquotaip) {
673 error = xfs_dqinode_metadir_create(qi->qi_dirip,
674 XFS_DQTYPE_USER, &qi->qi_uquotaip);
675 if (error)
676 return error;
677 }
678
679 if (XFS_IS_GQUOTA_ON(mp) && !qi->qi_gquotaip) {
680 error = xfs_dqinode_metadir_create(qi->qi_dirip,
681 XFS_DQTYPE_GROUP, &qi->qi_gquotaip);
682 if (error)
683 return error;
684 }
685
686 if (XFS_IS_PQUOTA_ON(mp) && !qi->qi_pquotaip) {
687 error = xfs_dqinode_metadir_create(qi->qi_dirip,
688 XFS_DQTYPE_PROJ, &qi->qi_pquotaip);
689 if (error)
690 return error;
691 }
692
693 return 0;
694 }
695
696 /*
697 * Add QUOTABIT to sb_versionnum and initialize qflags in preparation for
698 * creating quota files on a metadir filesystem.
699 */
700 STATIC int
xfs_qm_prep_metadir_sb(struct xfs_mount * mp)701 xfs_qm_prep_metadir_sb(
702 struct xfs_mount *mp)
703 {
704 struct xfs_trans *tp;
705 int error;
706
707 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_sb, 0, 0, 0, &tp);
708 if (error)
709 return error;
710
711 spin_lock(&mp->m_sb_lock);
712
713 xfs_add_quota(mp);
714
715 /* qflags will get updated fully _after_ quotacheck */
716 mp->m_sb.sb_qflags = mp->m_qflags & XFS_ALL_QUOTA_ACCT;
717
718 spin_unlock(&mp->m_sb_lock);
719 xfs_log_sb(tp);
720
721 return xfs_trans_commit(tp);
722 }
723
724 /*
725 * Load existing quota inodes or create them. Since this is a V5 filesystem,
726 * we don't have to deal with the grp/prjquota switcheroo thing from V4.
727 */
728 STATIC int
xfs_qm_init_metadir_qinos(struct xfs_mount * mp)729 xfs_qm_init_metadir_qinos(
730 struct xfs_mount *mp)
731 {
732 struct xfs_quotainfo *qi = mp->m_quotainfo;
733 int error;
734
735 if (!xfs_has_quota(mp)) {
736 error = xfs_qm_prep_metadir_sb(mp);
737 if (error)
738 return error;
739 }
740
741 error = xfs_qm_load_metadir_qinos(mp, qi);
742 if (error)
743 goto out_err;
744
745 error = xfs_qm_create_metadir_qinos(mp, qi);
746 if (error)
747 goto out_err;
748
749 /* The only user of the quota dir inode is online fsck */
750 #if !IS_ENABLED(CONFIG_XFS_ONLINE_SCRUB)
751 xfs_irele(qi->qi_dirip);
752 qi->qi_dirip = NULL;
753 #endif
754 return 0;
755 out_err:
756 xfs_qm_destroy_quotainos(mp->m_quotainfo);
757 return error;
758 }
759
760 /*
761 * This initializes all the quota information that's kept in the
762 * mount structure
763 */
764 STATIC int
xfs_qm_init_quotainfo(struct xfs_mount * mp)765 xfs_qm_init_quotainfo(
766 struct xfs_mount *mp)
767 {
768 struct xfs_quotainfo *qinf;
769 int error;
770
771 ASSERT(XFS_IS_QUOTA_ON(mp));
772
773 qinf = mp->m_quotainfo = kzalloc_obj(struct xfs_quotainfo,
774 GFP_KERNEL | __GFP_NOFAIL);
775
776 error = list_lru_init(&qinf->qi_lru);
777 if (error)
778 goto out_free_qinf;
779
780 /*
781 * See if quotainodes are setup, and if not, allocate them,
782 * and change the superblock accordingly.
783 */
784 if (xfs_has_metadir(mp))
785 error = xfs_qm_init_metadir_qinos(mp);
786 else
787 error = xfs_qm_init_quotainos(mp);
788 if (error)
789 goto out_free_lru;
790
791 INIT_RADIX_TREE(&qinf->qi_uquota_tree, GFP_KERNEL);
792 INIT_RADIX_TREE(&qinf->qi_gquota_tree, GFP_KERNEL);
793 INIT_RADIX_TREE(&qinf->qi_pquota_tree, GFP_KERNEL);
794 mutex_init(&qinf->qi_tree_lock);
795
796 /* mutex used to serialize quotaoffs */
797 mutex_init(&qinf->qi_quotaofflock);
798
799 /* Precalc some constants */
800 qinf->qi_dqchunklen = XFS_FSB_TO_BB(mp, XFS_DQUOT_CLUSTER_SIZE_FSB);
801 qinf->qi_dqperchunk = xfs_calc_dquots_per_chunk(qinf->qi_dqchunklen);
802 if (xfs_has_bigtime(mp)) {
803 qinf->qi_expiry_min =
804 xfs_dq_bigtime_to_unix(XFS_DQ_BIGTIME_EXPIRY_MIN);
805 qinf->qi_expiry_max =
806 xfs_dq_bigtime_to_unix(XFS_DQ_BIGTIME_EXPIRY_MAX);
807 } else {
808 qinf->qi_expiry_min = XFS_DQ_LEGACY_EXPIRY_MIN;
809 qinf->qi_expiry_max = XFS_DQ_LEGACY_EXPIRY_MAX;
810 }
811 trace_xfs_quota_expiry_range(mp, qinf->qi_expiry_min,
812 qinf->qi_expiry_max);
813
814 mp->m_qflags |= (mp->m_sb.sb_qflags & XFS_ALL_QUOTA_CHKD);
815
816 xfs_qm_init_timelimits(mp, XFS_DQTYPE_USER);
817 xfs_qm_init_timelimits(mp, XFS_DQTYPE_GROUP);
818 xfs_qm_init_timelimits(mp, XFS_DQTYPE_PROJ);
819
820 if (XFS_IS_UQUOTA_ON(mp))
821 xfs_qm_set_defquota(mp, XFS_DQTYPE_USER, qinf);
822 if (XFS_IS_GQUOTA_ON(mp))
823 xfs_qm_set_defquota(mp, XFS_DQTYPE_GROUP, qinf);
824 if (XFS_IS_PQUOTA_ON(mp))
825 xfs_qm_set_defquota(mp, XFS_DQTYPE_PROJ, qinf);
826
827 qinf->qi_shrinker = shrinker_alloc(SHRINKER_NUMA_AWARE, "xfs-qm:%s",
828 mp->m_super->s_id);
829 if (!qinf->qi_shrinker) {
830 error = -ENOMEM;
831 goto out_free_inos;
832 }
833
834 qinf->qi_shrinker->count_objects = xfs_qm_shrink_count;
835 qinf->qi_shrinker->scan_objects = xfs_qm_shrink_scan;
836 qinf->qi_shrinker->private_data = qinf;
837
838 shrinker_register(qinf->qi_shrinker);
839
840 xfs_hooks_init(&qinf->qi_mod_ino_dqtrx_hooks);
841 xfs_hooks_init(&qinf->qi_apply_dqtrx_hooks);
842
843 return 0;
844
845 out_free_inos:
846 mutex_destroy(&qinf->qi_quotaofflock);
847 mutex_destroy(&qinf->qi_tree_lock);
848 xfs_qm_destroy_quotainos(qinf);
849 out_free_lru:
850 list_lru_destroy(&qinf->qi_lru);
851 out_free_qinf:
852 kfree(qinf);
853 mp->m_quotainfo = NULL;
854 return error;
855 }
856
857 /*
858 * Gets called when unmounting a filesystem or when all quotas get
859 * turned off.
860 * This purges the quota inodes, destroys locks and frees itself.
861 */
862 void
xfs_qm_destroy_quotainfo(struct xfs_mount * mp)863 xfs_qm_destroy_quotainfo(
864 struct xfs_mount *mp)
865 {
866 struct xfs_quotainfo *qi;
867
868 qi = mp->m_quotainfo;
869 ASSERT(qi != NULL);
870
871 shrinker_free(qi->qi_shrinker);
872 list_lru_destroy(&qi->qi_lru);
873 xfs_qm_destroy_quotainos(qi);
874 mutex_destroy(&qi->qi_tree_lock);
875 mutex_destroy(&qi->qi_quotaofflock);
876 kfree(qi);
877 mp->m_quotainfo = NULL;
878 }
879
880 static inline enum xfs_metafile_type
xfs_qm_metafile_type(unsigned int flags)881 xfs_qm_metafile_type(
882 unsigned int flags)
883 {
884 if (flags & XFS_QMOPT_UQUOTA)
885 return XFS_METAFILE_USRQUOTA;
886 else if (flags & XFS_QMOPT_GQUOTA)
887 return XFS_METAFILE_GRPQUOTA;
888 return XFS_METAFILE_PRJQUOTA;
889 }
890
891 /*
892 * Create an inode and return with a reference already taken, but unlocked
893 * This is how we create quota inodes
894 */
895 STATIC int
xfs_qm_qino_alloc(struct xfs_mount * mp,struct xfs_inode ** ipp,unsigned int flags)896 xfs_qm_qino_alloc(
897 struct xfs_mount *mp,
898 struct xfs_inode **ipp,
899 unsigned int flags)
900 {
901 struct xfs_trans *tp;
902 enum xfs_metafile_type metafile_type = xfs_qm_metafile_type(flags);
903 int error;
904 bool need_alloc = true;
905
906 *ipp = NULL;
907 /*
908 * With superblock that doesn't have separate pquotino, we
909 * share an inode between gquota and pquota. If the on-disk
910 * superblock has GQUOTA and the filesystem is now mounted
911 * with PQUOTA, just use sb_gquotino for sb_pquotino and
912 * vice-versa.
913 */
914 if (!xfs_has_pquotino(mp) &&
915 (flags & (XFS_QMOPT_PQUOTA|XFS_QMOPT_GQUOTA))) {
916 xfs_ino_t ino = NULLFSINO;
917
918 if ((flags & XFS_QMOPT_PQUOTA) &&
919 (mp->m_sb.sb_gquotino != NULLFSINO)) {
920 ino = mp->m_sb.sb_gquotino;
921 if (XFS_IS_CORRUPT(mp,
922 mp->m_sb.sb_pquotino != NULLFSINO)) {
923 xfs_fs_mark_sick(mp, XFS_SICK_FS_PQUOTA);
924 return -EFSCORRUPTED;
925 }
926 } else if ((flags & XFS_QMOPT_GQUOTA) &&
927 (mp->m_sb.sb_pquotino != NULLFSINO)) {
928 ino = mp->m_sb.sb_pquotino;
929 if (XFS_IS_CORRUPT(mp,
930 mp->m_sb.sb_gquotino != NULLFSINO)) {
931 xfs_fs_mark_sick(mp, XFS_SICK_FS_GQUOTA);
932 return -EFSCORRUPTED;
933 }
934 }
935 if (ino != NULLFSINO) {
936 error = xfs_metafile_iget(mp, ino, metafile_type, ipp);
937 if (error)
938 return error;
939
940 mp->m_sb.sb_gquotino = NULLFSINO;
941 mp->m_sb.sb_pquotino = NULLFSINO;
942 need_alloc = false;
943 }
944 }
945
946 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_create,
947 need_alloc ? XFS_QM_QINOCREATE_SPACE_RES(mp) : 0,
948 0, 0, &tp);
949 if (error)
950 return error;
951
952 if (need_alloc) {
953 struct xfs_icreate_args args = {
954 .mode = S_IFREG,
955 .flags = XFS_ICREATE_UNLINKABLE,
956 };
957 xfs_ino_t ino;
958
959 error = xfs_dialloc(&tp, &args, &ino);
960 if (!error)
961 error = xfs_icreate(tp, ino, &args, ipp);
962 if (error) {
963 xfs_trans_cancel(tp);
964 return error;
965 }
966 if (xfs_has_metadir(mp))
967 xfs_metafile_set_iflag(tp, *ipp, metafile_type);
968 }
969
970 /*
971 * Make the changes in the superblock, and log those too.
972 * sbfields arg may contain fields other than *QUOTINO;
973 * VERSIONNUM for example.
974 */
975 spin_lock(&mp->m_sb_lock);
976 if (flags & XFS_QMOPT_SBVERSION) {
977 ASSERT(!xfs_has_quota(mp));
978
979 xfs_add_quota(mp);
980 mp->m_sb.sb_uquotino = NULLFSINO;
981 mp->m_sb.sb_gquotino = NULLFSINO;
982 mp->m_sb.sb_pquotino = NULLFSINO;
983
984 /* qflags will get updated fully _after_ quotacheck */
985 mp->m_sb.sb_qflags = mp->m_qflags & XFS_ALL_QUOTA_ACCT;
986 }
987 if (flags & XFS_QMOPT_UQUOTA)
988 mp->m_sb.sb_uquotino = I_INO(*ipp);
989 else if (flags & XFS_QMOPT_GQUOTA)
990 mp->m_sb.sb_gquotino = I_INO(*ipp);
991 else
992 mp->m_sb.sb_pquotino = I_INO(*ipp);
993 spin_unlock(&mp->m_sb_lock);
994 xfs_log_sb(tp);
995
996 error = xfs_trans_commit(tp);
997 if (error) {
998 ASSERT(xfs_is_shutdown(mp));
999 xfs_alert(mp, "%s failed (error %d)!", __func__, error);
1000 }
1001 if (need_alloc) {
1002 xfs_iunlock(*ipp, XFS_ILOCK_EXCL);
1003 xfs_finish_inode_setup(*ipp);
1004 }
1005 return error;
1006 }
1007
1008
1009 STATIC void
xfs_qm_reset_dqcounts(struct xfs_mount * mp,struct xfs_buf * bp,xfs_dqid_t id,xfs_dqtype_t type)1010 xfs_qm_reset_dqcounts(
1011 struct xfs_mount *mp,
1012 struct xfs_buf *bp,
1013 xfs_dqid_t id,
1014 xfs_dqtype_t type)
1015 {
1016 struct xfs_dqblk *dqb;
1017 int j;
1018
1019 trace_xfs_reset_dqcounts(bp, _RET_IP_);
1020
1021 /*
1022 * Reset all counters and timers. They'll be
1023 * started afresh by xfs_qm_quotacheck.
1024 */
1025 #ifdef DEBUG
1026 j = (int)XFS_FSB_TO_B(mp, XFS_DQUOT_CLUSTER_SIZE_FSB) /
1027 sizeof(struct xfs_dqblk);
1028 ASSERT(mp->m_quotainfo->qi_dqperchunk == j);
1029 #endif
1030 dqb = bp->b_addr;
1031 for (j = 0; j < mp->m_quotainfo->qi_dqperchunk; j++) {
1032 struct xfs_disk_dquot *ddq;
1033
1034 ddq = (struct xfs_disk_dquot *)&dqb[j];
1035
1036 /*
1037 * Do a sanity check, and if needed, repair the dqblk. Don't
1038 * output any warnings because it's perfectly possible to
1039 * find uninitialised dquot blks. See comment in
1040 * xfs_dquot_verify.
1041 */
1042 if (xfs_dqblk_verify(mp, &dqb[j], id + j) ||
1043 (dqb[j].dd_diskdq.d_type & XFS_DQTYPE_REC_MASK) != type)
1044 xfs_dqblk_repair(mp, &dqb[j], id + j, type);
1045
1046 /*
1047 * Reset type in case we are reusing group quota file for
1048 * project quotas or vice versa
1049 */
1050 ddq->d_type = type;
1051 ddq->d_bcount = 0;
1052 ddq->d_icount = 0;
1053 ddq->d_rtbcount = 0;
1054
1055 /*
1056 * dquot id 0 stores the default grace period and the maximum
1057 * warning limit that were set by the administrator, so we
1058 * should not reset them.
1059 */
1060 if (ddq->d_id != 0) {
1061 ddq->d_btimer = 0;
1062 ddq->d_itimer = 0;
1063 ddq->d_rtbtimer = 0;
1064 ddq->d_bwarns = 0;
1065 ddq->d_iwarns = 0;
1066 ddq->d_rtbwarns = 0;
1067 if (xfs_has_bigtime(mp))
1068 ddq->d_type |= XFS_DQTYPE_BIGTIME;
1069 }
1070
1071 if (xfs_has_crc(mp)) {
1072 xfs_update_cksum((char *)&dqb[j],
1073 sizeof(struct xfs_dqblk),
1074 XFS_DQUOT_CRC_OFF);
1075 }
1076 }
1077 }
1078
1079 STATIC int
xfs_qm_reset_dqcounts_all(struct xfs_mount * mp,xfs_dqid_t firstid,xfs_fsblock_t bno,xfs_filblks_t blkcnt,xfs_dqtype_t type,struct list_head * buffer_list)1080 xfs_qm_reset_dqcounts_all(
1081 struct xfs_mount *mp,
1082 xfs_dqid_t firstid,
1083 xfs_fsblock_t bno,
1084 xfs_filblks_t blkcnt,
1085 xfs_dqtype_t type,
1086 struct list_head *buffer_list)
1087 {
1088 struct xfs_buf *bp;
1089 int error = 0;
1090
1091 ASSERT(blkcnt > 0);
1092
1093 /*
1094 * Blkcnt arg can be a very big number, and might even be
1095 * larger than the log itself. So, we have to break it up into
1096 * manageable-sized transactions.
1097 * Note that we don't start a permanent transaction here; we might
1098 * not be able to get a log reservation for the whole thing up front,
1099 * and we don't really care to either, because we just discard
1100 * everything if we were to crash in the middle of this loop.
1101 */
1102 while (blkcnt--) {
1103 error = xfs_trans_read_buf(mp, NULL, mp->m_ddev_targp,
1104 XFS_FSB_TO_DADDR(mp, bno),
1105 mp->m_quotainfo->qi_dqchunklen, 0, &bp,
1106 &xfs_dquot_buf_ops);
1107
1108 /*
1109 * CRC and validation errors will return a EFSCORRUPTED here. If
1110 * this occurs, re-read without CRC validation so that we can
1111 * repair the damage via xfs_qm_reset_dqcounts(). This process
1112 * will leave a trace in the log indicating corruption has
1113 * been detected.
1114 */
1115 if (error == -EFSCORRUPTED) {
1116 error = xfs_trans_read_buf(mp, NULL, mp->m_ddev_targp,
1117 XFS_FSB_TO_DADDR(mp, bno),
1118 mp->m_quotainfo->qi_dqchunklen, 0, &bp,
1119 NULL);
1120 }
1121
1122 if (error)
1123 break;
1124
1125 /*
1126 * A corrupt buffer might not have a verifier attached, so
1127 * make sure we have the correct one attached before writeback
1128 * occurs.
1129 */
1130 bp->b_ops = &xfs_dquot_buf_ops;
1131 xfs_qm_reset_dqcounts(mp, bp, firstid, type);
1132 xfs_buf_delwri_queue(bp, buffer_list);
1133 xfs_buf_relse(bp);
1134
1135 /* goto the next block. */
1136 bno++;
1137 firstid += mp->m_quotainfo->qi_dqperchunk;
1138 }
1139
1140 return error;
1141 }
1142
1143 /*
1144 * Iterate over all allocated dquot blocks in this quota inode, zeroing all
1145 * counters for every chunk of dquots that we find.
1146 */
1147 STATIC int
xfs_qm_reset_dqcounts_buf(struct xfs_mount * mp,struct xfs_inode * qip,xfs_dqtype_t type,struct list_head * buffer_list)1148 xfs_qm_reset_dqcounts_buf(
1149 struct xfs_mount *mp,
1150 struct xfs_inode *qip,
1151 xfs_dqtype_t type,
1152 struct list_head *buffer_list)
1153 {
1154 struct xfs_bmbt_irec *map;
1155 int i, nmaps; /* number of map entries */
1156 int error; /* return value */
1157 xfs_fileoff_t lblkno;
1158 xfs_filblks_t maxlblkcnt;
1159 xfs_dqid_t firstid;
1160 xfs_fsblock_t rablkno;
1161 xfs_filblks_t rablkcnt;
1162
1163 error = 0;
1164 /*
1165 * This looks racy, but we can't keep an inode lock across a
1166 * trans_reserve. But, this gets called during quotacheck, and that
1167 * happens only at mount time which is single threaded.
1168 */
1169 if (qip->i_nblocks == 0)
1170 return 0;
1171
1172 map = kmalloc(XFS_DQITER_MAP_SIZE * sizeof(*map),
1173 GFP_KERNEL | __GFP_NOFAIL);
1174
1175 lblkno = 0;
1176 maxlblkcnt = XFS_B_TO_FSB(mp, mp->m_super->s_maxbytes);
1177 do {
1178 uint lock_mode;
1179
1180 nmaps = XFS_DQITER_MAP_SIZE;
1181 /*
1182 * We aren't changing the inode itself. Just changing
1183 * some of its data. No new blocks are added here, and
1184 * the inode is never added to the transaction.
1185 */
1186 lock_mode = xfs_ilock_data_map_shared(qip);
1187 error = xfs_bmapi_read(qip, lblkno, maxlblkcnt - lblkno,
1188 map, &nmaps, 0);
1189 xfs_iunlock(qip, lock_mode);
1190 if (error)
1191 break;
1192
1193 ASSERT(nmaps <= XFS_DQITER_MAP_SIZE);
1194 for (i = 0; i < nmaps; i++) {
1195 ASSERT(map[i].br_startblock != DELAYSTARTBLOCK);
1196 ASSERT(map[i].br_blockcount);
1197
1198
1199 lblkno += map[i].br_blockcount;
1200
1201 if (map[i].br_startblock == HOLESTARTBLOCK)
1202 continue;
1203
1204 firstid = (xfs_dqid_t) map[i].br_startoff *
1205 mp->m_quotainfo->qi_dqperchunk;
1206 /*
1207 * Do a read-ahead on the next extent.
1208 */
1209 if ((i+1 < nmaps) &&
1210 (map[i+1].br_startblock != HOLESTARTBLOCK)) {
1211 rablkcnt = map[i+1].br_blockcount;
1212 rablkno = map[i+1].br_startblock;
1213 while (rablkcnt--) {
1214 xfs_buf_readahead(mp->m_ddev_targp,
1215 XFS_FSB_TO_DADDR(mp, rablkno),
1216 mp->m_quotainfo->qi_dqchunklen,
1217 &xfs_dquot_buf_ops);
1218 rablkno++;
1219 }
1220 }
1221 /*
1222 * Iterate thru all the blks in the extent and
1223 * reset the counters of all the dquots inside them.
1224 */
1225 error = xfs_qm_reset_dqcounts_all(mp, firstid,
1226 map[i].br_startblock,
1227 map[i].br_blockcount,
1228 type, buffer_list);
1229 if (error)
1230 goto out;
1231 }
1232 } while (nmaps > 0);
1233
1234 out:
1235 kfree(map);
1236 return error;
1237 }
1238
1239 /*
1240 * Called by dqusage_adjust in doing a quotacheck.
1241 *
1242 * Given the inode, and a dquot id this updates both the incore dqout as well
1243 * as the buffer copy. This is so that once the quotacheck is done, we can
1244 * just log all the buffers, as opposed to logging numerous updates to
1245 * individual dquots.
1246 */
1247 STATIC int
xfs_qm_quotacheck_dqadjust(struct xfs_inode * ip,xfs_dqtype_t type,xfs_qcnt_t nblks,xfs_qcnt_t rtblks)1248 xfs_qm_quotacheck_dqadjust(
1249 struct xfs_inode *ip,
1250 xfs_dqtype_t type,
1251 xfs_qcnt_t nblks,
1252 xfs_qcnt_t rtblks)
1253 {
1254 struct xfs_mount *mp = ip->i_mount;
1255 struct xfs_dquot *dqp;
1256 xfs_dqid_t id;
1257 int error;
1258
1259 id = xfs_qm_id_for_quotatype(ip, type);
1260 error = xfs_qm_dqget(mp, id, type, true, &dqp);
1261 if (error) {
1262 /*
1263 * Shouldn't be able to turn off quotas here.
1264 */
1265 ASSERT(error != -ESRCH);
1266 ASSERT(error != -ENOENT);
1267 return error;
1268 }
1269
1270 mutex_lock(&dqp->q_qlock);
1271 error = xfs_dquot_attach_buf(NULL, dqp);
1272 if (error)
1273 goto out_unlock;
1274
1275 trace_xfs_dqadjust(dqp);
1276
1277 /*
1278 * Adjust the inode count and the block count to reflect this inode's
1279 * resource usage.
1280 */
1281 dqp->q_ino.count++;
1282 dqp->q_ino.reserved++;
1283 if (nblks) {
1284 dqp->q_blk.count += nblks;
1285 dqp->q_blk.reserved += nblks;
1286 }
1287 if (rtblks) {
1288 dqp->q_rtb.count += rtblks;
1289 dqp->q_rtb.reserved += rtblks;
1290 }
1291
1292 /*
1293 * Set default limits, adjust timers (since we changed usages)
1294 *
1295 * There are no timers for the default values set in the root dquot.
1296 */
1297 if (dqp->q_id) {
1298 xfs_qm_adjust_dqlimits(dqp);
1299 xfs_qm_adjust_dqtimers(dqp);
1300 }
1301
1302 dqp->q_flags |= XFS_DQFLAG_DIRTY;
1303 out_unlock:
1304 mutex_unlock(&dqp->q_qlock);
1305 xfs_qm_dqrele(dqp);
1306 return error;
1307 }
1308
1309 /*
1310 * callback routine supplied to bulkstat(). Given an inumber, find its
1311 * dquots and update them to account for resources taken by that inode.
1312 */
1313 /* ARGSUSED */
1314 STATIC int
xfs_qm_dqusage_adjust(struct xfs_mount * mp,struct xfs_trans * tp,xfs_ino_t ino,void * data)1315 xfs_qm_dqusage_adjust(
1316 struct xfs_mount *mp,
1317 struct xfs_trans *tp,
1318 xfs_ino_t ino,
1319 void *data)
1320 {
1321 struct xfs_inode *ip;
1322 xfs_filblks_t nblks, rtblks;
1323 unsigned int lock_mode;
1324 int error;
1325
1326 ASSERT(XFS_IS_QUOTA_ON(mp));
1327
1328 /*
1329 * rootino must have its resources accounted for, not so with the quota
1330 * inodes.
1331 */
1332 if (xfs_is_quota_inode(&mp->m_sb, ino))
1333 return 0;
1334
1335 /*
1336 * We don't _need_ to take the ilock EXCL here because quotacheck runs
1337 * at mount time and therefore nobody will be racing chown/chproj.
1338 */
1339 error = xfs_iget(mp, tp, ino, XFS_IGET_DONTCACHE, 0, &ip);
1340 if (error == -EINVAL || error == -ENOENT)
1341 return 0;
1342 if (error)
1343 return error;
1344
1345 /*
1346 * Reload the incore unlinked list to avoid failure in inodegc.
1347 * Use an unlocked check here because unrecovered unlinked inodes
1348 * should be somewhat rare.
1349 */
1350 if (xfs_inode_unlinked_incomplete(ip)) {
1351 error = xfs_inode_reload_unlinked(ip);
1352 if (error) {
1353 xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
1354 goto error0;
1355 }
1356 }
1357
1358 /* Metadata directory files are not accounted to user-visible quotas. */
1359 if (xfs_is_metadir_inode(ip))
1360 goto error0;
1361
1362 ASSERT(ip->i_delayed_blks == 0);
1363
1364 lock_mode = xfs_ilock_data_map_shared(ip);
1365 if (XFS_IS_REALTIME_INODE(ip)) {
1366 error = xfs_iread_extents(tp, ip, XFS_DATA_FORK);
1367 if (error) {
1368 xfs_iunlock(ip, lock_mode);
1369 goto error0;
1370 }
1371 }
1372 xfs_inode_count_blocks(tp, ip, &nblks, &rtblks);
1373 xfs_iflags_clear(ip, XFS_IQUOTAUNCHECKED);
1374 xfs_iunlock(ip, lock_mode);
1375
1376 /*
1377 * Add the (disk blocks and inode) resources occupied by this
1378 * inode to its dquots. We do this adjustment in the incore dquot,
1379 * and also copy the changes to its buffer.
1380 * We don't care about putting these changes in a transaction
1381 * envelope because if we crash in the middle of a 'quotacheck'
1382 * we have to start from the beginning anyway.
1383 * Once we're done, we'll log all the dquot bufs.
1384 *
1385 * The *QUOTA_ON checks below may look pretty racy, but quotachecks
1386 * and quotaoffs don't race. (Quotachecks happen at mount time only).
1387 */
1388 if (XFS_IS_UQUOTA_ON(mp)) {
1389 error = xfs_qm_quotacheck_dqadjust(ip, XFS_DQTYPE_USER, nblks,
1390 rtblks);
1391 if (error)
1392 goto error0;
1393 }
1394
1395 if (XFS_IS_GQUOTA_ON(mp)) {
1396 error = xfs_qm_quotacheck_dqadjust(ip, XFS_DQTYPE_GROUP, nblks,
1397 rtblks);
1398 if (error)
1399 goto error0;
1400 }
1401
1402 if (XFS_IS_PQUOTA_ON(mp)) {
1403 error = xfs_qm_quotacheck_dqadjust(ip, XFS_DQTYPE_PROJ, nblks,
1404 rtblks);
1405 if (error)
1406 goto error0;
1407 }
1408
1409 error0:
1410 xfs_irele(ip);
1411 return error;
1412 }
1413
1414 STATIC int
xfs_qm_flush_one(struct xfs_dquot * dqp,void * data)1415 xfs_qm_flush_one(
1416 struct xfs_dquot *dqp,
1417 void *data)
1418 {
1419 struct list_head *buffer_list = data;
1420 struct xfs_buf *bp = NULL;
1421 int error = 0;
1422
1423 if (!lockref_get_not_dead(&dqp->q_lockref))
1424 return 0;
1425
1426 mutex_lock(&dqp->q_qlock);
1427 if (!XFS_DQ_IS_DIRTY(dqp))
1428 goto out_unlock;
1429
1430 xfs_qm_dqunpin_wait(dqp);
1431 xfs_dqflock(dqp);
1432
1433 error = xfs_dquot_use_attached_buf(dqp, &bp);
1434 if (error)
1435 goto out_unlock;
1436 if (!bp) {
1437 error = -EFSCORRUPTED;
1438 goto out_unlock;
1439 }
1440
1441 error = xfs_qm_dqflush(dqp, bp);
1442 if (!error)
1443 xfs_buf_delwri_queue(bp, buffer_list);
1444 xfs_buf_relse(bp);
1445 out_unlock:
1446 mutex_unlock(&dqp->q_qlock);
1447 xfs_qm_dqrele(dqp);
1448 return error;
1449 }
1450
1451 /*
1452 * Walk thru all the filesystem inodes and construct a consistent view
1453 * of the disk quota world. If the quotacheck fails, disable quotas.
1454 */
1455 STATIC int
xfs_qm_quotacheck(xfs_mount_t * mp)1456 xfs_qm_quotacheck(
1457 xfs_mount_t *mp)
1458 {
1459 int error, error2;
1460 uint flags;
1461 LIST_HEAD (buffer_list);
1462 struct xfs_inode *uip = mp->m_quotainfo->qi_uquotaip;
1463 struct xfs_inode *gip = mp->m_quotainfo->qi_gquotaip;
1464 struct xfs_inode *pip = mp->m_quotainfo->qi_pquotaip;
1465
1466 flags = 0;
1467
1468 ASSERT(uip || gip || pip);
1469 ASSERT(XFS_IS_QUOTA_ON(mp));
1470
1471 xfs_notice(mp, "Quotacheck needed: Please wait.");
1472
1473 /*
1474 * First we go thru all the dquots on disk, USR and GRP/PRJ, and reset
1475 * their counters to zero. We need a clean slate.
1476 * We don't log our changes till later.
1477 */
1478 if (uip) {
1479 error = xfs_qm_reset_dqcounts_buf(mp, uip, XFS_DQTYPE_USER,
1480 &buffer_list);
1481 if (error)
1482 goto error_return;
1483 flags |= XFS_UQUOTA_CHKD;
1484 }
1485
1486 if (gip) {
1487 error = xfs_qm_reset_dqcounts_buf(mp, gip, XFS_DQTYPE_GROUP,
1488 &buffer_list);
1489 if (error)
1490 goto error_return;
1491 flags |= XFS_GQUOTA_CHKD;
1492 }
1493
1494 if (pip) {
1495 error = xfs_qm_reset_dqcounts_buf(mp, pip, XFS_DQTYPE_PROJ,
1496 &buffer_list);
1497 if (error)
1498 goto error_return;
1499 flags |= XFS_PQUOTA_CHKD;
1500 }
1501
1502 xfs_set_quotacheck_running(mp);
1503 error = xfs_iwalk_threaded(mp, 0, 0, xfs_qm_dqusage_adjust, 0, true,
1504 NULL);
1505 xfs_clear_quotacheck_running(mp);
1506
1507 /*
1508 * On error, the inode walk may have partially populated the dquot
1509 * caches. We must purge them before disabling quota and tearing down
1510 * the quotainfo, or else the dquots will leak.
1511 */
1512 if (error)
1513 goto error_purge;
1514
1515 /*
1516 * We've made all the changes that we need to make incore. Flush them
1517 * down to disk buffers if everything was updated successfully.
1518 */
1519 if (XFS_IS_UQUOTA_ON(mp)) {
1520 error = xfs_qm_dquot_walk(mp, XFS_DQTYPE_USER, xfs_qm_flush_one,
1521 &buffer_list);
1522 }
1523 if (XFS_IS_GQUOTA_ON(mp)) {
1524 error2 = xfs_qm_dquot_walk(mp, XFS_DQTYPE_GROUP, xfs_qm_flush_one,
1525 &buffer_list);
1526 if (!error)
1527 error = error2;
1528 }
1529 if (XFS_IS_PQUOTA_ON(mp)) {
1530 error2 = xfs_qm_dquot_walk(mp, XFS_DQTYPE_PROJ, xfs_qm_flush_one,
1531 &buffer_list);
1532 if (!error)
1533 error = error2;
1534 }
1535
1536 error2 = xfs_buf_delwri_submit(&buffer_list);
1537 if (!error)
1538 error = error2;
1539
1540 /*
1541 * We can get this error if we couldn't do a dquot allocation inside
1542 * xfs_qm_dqusage_adjust (via bulkstat). We don't care about the
1543 * dirty dquots that might be cached, we just want to get rid of them
1544 * and turn quotaoff. The dquots won't be attached to any of the inodes
1545 * at this point (because we intentionally didn't in dqget_noattach).
1546 */
1547 if (error)
1548 goto error_purge;
1549
1550 /*
1551 * If one type of quotas is off, then it will lose its
1552 * quotachecked status, since we won't be doing accounting for
1553 * that type anymore.
1554 */
1555 mp->m_qflags &= ~XFS_ALL_QUOTA_CHKD;
1556 mp->m_qflags |= flags;
1557
1558 error_return:
1559 xfs_buf_delwri_cancel(&buffer_list);
1560
1561 if (error) {
1562 xfs_warn(mp,
1563 "Quotacheck: Unsuccessful (Error %d): Disabling quotas.",
1564 error);
1565 /*
1566 * We must turn off quotas.
1567 */
1568 ASSERT(mp->m_quotainfo != NULL);
1569 xfs_qm_destroy_quotainfo(mp);
1570 if (xfs_mount_reset_sbqflags(mp)) {
1571 xfs_warn(mp,
1572 "Quotacheck: Failed to reset quota flags.");
1573 }
1574 xfs_fs_mark_sick(mp, XFS_SICK_FS_QUOTACHECK);
1575 } else {
1576 xfs_notice(mp, "Quotacheck: Done.");
1577 xfs_fs_mark_healthy(mp, XFS_SICK_FS_QUOTACHECK);
1578 }
1579
1580 return error;
1581
1582 error_purge:
1583 /*
1584 * On error, we may have inodes queued for inactivation. This may try
1585 * to attach dquots to the inode before running cleanup operations on
1586 * the inode and this can race with the xfs_qm_destroy_quotainfo() call
1587 * below that frees mp->m_quotainfo. To avoid this race, flush all the
1588 * pending inodegc operations before we purge the dquots from memory,
1589 * ensuring that background inactivation is idle whilst we turn off
1590 * quotas.
1591 */
1592 xfs_inodegc_flush(mp);
1593 xfs_qm_dqpurge_all(mp);
1594 goto error_return;
1595
1596 }
1597
1598 /*
1599 * This is called from xfs_mountfs to start quotas and initialize all
1600 * necessary data structures like quotainfo. This is also responsible for
1601 * running a quotacheck as necessary. We are guaranteed that the superblock
1602 * is consistently read in at this point.
1603 *
1604 * If we fail here, the mount will continue with quota turned off. We don't
1605 * need to inidicate success or failure at all.
1606 */
1607 void
xfs_qm_mount_quotas(struct xfs_mount * mp)1608 xfs_qm_mount_quotas(
1609 struct xfs_mount *mp)
1610 {
1611 int error = 0;
1612 uint sbf;
1613
1614 /*
1615 * If quotas on realtime volumes is not supported, disable quotas
1616 * immediately. We only support rtquota if rtgroups are enabled to
1617 * avoid problems with older kernels.
1618 */
1619 if (mp->m_sb.sb_rextents &&
1620 (!xfs_has_rtgroups(mp) || xfs_has_zoned(mp))) {
1621 xfs_notice(mp, "Cannot turn on quotas for realtime filesystem");
1622 mp->m_qflags = 0;
1623 goto write_changes;
1624 }
1625
1626 ASSERT(XFS_IS_QUOTA_ON(mp));
1627
1628 /*
1629 * Allocate the quotainfo structure inside the mount struct, and
1630 * create quotainode(s), and change/rev superblock if necessary.
1631 */
1632 error = xfs_qm_init_quotainfo(mp);
1633 if (error) {
1634 /*
1635 * We must turn off quotas.
1636 */
1637 ASSERT(mp->m_quotainfo == NULL);
1638 mp->m_qflags = 0;
1639 goto write_changes;
1640 }
1641 /*
1642 * If any of the quotas are not consistent, do a quotacheck.
1643 */
1644 if (XFS_QM_NEED_QUOTACHECK(mp)) {
1645 error = xfs_qm_quotacheck(mp);
1646 if (error) {
1647 /* Quotacheck failed and disabled quotas. */
1648 return;
1649 }
1650 }
1651 /*
1652 * If one type of quotas is off, then it will lose its
1653 * quotachecked status, since we won't be doing accounting for
1654 * that type anymore.
1655 */
1656 if (!XFS_IS_UQUOTA_ON(mp))
1657 mp->m_qflags &= ~XFS_UQUOTA_CHKD;
1658 if (!XFS_IS_GQUOTA_ON(mp))
1659 mp->m_qflags &= ~XFS_GQUOTA_CHKD;
1660 if (!XFS_IS_PQUOTA_ON(mp))
1661 mp->m_qflags &= ~XFS_PQUOTA_CHKD;
1662
1663 write_changes:
1664 /*
1665 * We actually don't have to acquire the m_sb_lock at all.
1666 * This can only be called from mount, and that's single threaded. XXX
1667 */
1668 spin_lock(&mp->m_sb_lock);
1669 sbf = mp->m_sb.sb_qflags;
1670 mp->m_sb.sb_qflags = mp->m_qflags & XFS_MOUNT_QUOTA_ALL;
1671 spin_unlock(&mp->m_sb_lock);
1672
1673 if (sbf != (mp->m_qflags & XFS_MOUNT_QUOTA_ALL)) {
1674 if (xfs_sync_sb(mp, false)) {
1675 /*
1676 * We could only have been turning quotas off.
1677 * We aren't in very good shape actually because
1678 * the incore structures are convinced that quotas are
1679 * off, but the on disk superblock doesn't know that !
1680 */
1681 ASSERT(!(XFS_IS_QUOTA_ON(mp)));
1682 xfs_alert(mp, "%s: Superblock update failed!",
1683 __func__);
1684 }
1685 }
1686
1687 if (error) {
1688 xfs_warn(mp, "Failed to initialize disk quotas, err %d.", error);
1689 return;
1690 }
1691 }
1692
1693 /*
1694 * Load the inode for a given type of quota, assuming that the sb fields have
1695 * been sorted out. This is not true when switching quota types on a V4
1696 * filesystem, so do not use this function for that.
1697 *
1698 * Returns -ENOENT if the quota inode field is NULLFSINO; 0 and an inode on
1699 * success; or a negative errno.
1700 */
1701 int
xfs_qm_qino_load(struct xfs_mount * mp,xfs_dqtype_t type,struct xfs_inode ** ipp)1702 xfs_qm_qino_load(
1703 struct xfs_mount *mp,
1704 xfs_dqtype_t type,
1705 struct xfs_inode **ipp)
1706 {
1707 struct xfs_trans *tp;
1708 struct xfs_inode *dp = NULL;
1709 int error;
1710
1711 tp = xfs_trans_alloc_empty(mp);
1712 if (xfs_has_metadir(mp)) {
1713 error = xfs_dqinode_load_parent(tp, &dp);
1714 if (error)
1715 goto out_cancel;
1716 }
1717
1718 error = xfs_dqinode_load(tp, dp, type, ipp);
1719 if (dp)
1720 xfs_irele(dp);
1721 out_cancel:
1722 xfs_trans_cancel(tp);
1723 return error;
1724 }
1725
1726 /*
1727 * This is called after the superblock has been read in and we're ready to
1728 * iget the quota inodes.
1729 */
1730 STATIC int
xfs_qm_init_quotainos(xfs_mount_t * mp)1731 xfs_qm_init_quotainos(
1732 xfs_mount_t *mp)
1733 {
1734 struct xfs_inode *uip = NULL;
1735 struct xfs_inode *gip = NULL;
1736 struct xfs_inode *pip = NULL;
1737 int error;
1738 uint flags = 0;
1739
1740 ASSERT(mp->m_quotainfo);
1741
1742 /*
1743 * Get the uquota and gquota inodes
1744 */
1745 if (xfs_has_quota(mp)) {
1746 if (XFS_IS_UQUOTA_ON(mp) &&
1747 mp->m_sb.sb_uquotino != NULLFSINO) {
1748 ASSERT(mp->m_sb.sb_uquotino > 0);
1749 error = xfs_qm_qino_load(mp, XFS_DQTYPE_USER, &uip);
1750 if (error)
1751 return error;
1752 }
1753 if (XFS_IS_GQUOTA_ON(mp) &&
1754 mp->m_sb.sb_gquotino != NULLFSINO) {
1755 ASSERT(mp->m_sb.sb_gquotino > 0);
1756 error = xfs_qm_qino_load(mp, XFS_DQTYPE_GROUP, &gip);
1757 if (error)
1758 goto error_rele;
1759 }
1760 if (XFS_IS_PQUOTA_ON(mp) &&
1761 mp->m_sb.sb_pquotino != NULLFSINO) {
1762 ASSERT(mp->m_sb.sb_pquotino > 0);
1763 error = xfs_qm_qino_load(mp, XFS_DQTYPE_PROJ, &pip);
1764 if (error)
1765 goto error_rele;
1766 }
1767 } else {
1768 flags |= XFS_QMOPT_SBVERSION;
1769 }
1770
1771 /*
1772 * Create the three inodes, if they don't exist already. The changes
1773 * made above will get added to a transaction and logged in one of
1774 * the qino_alloc calls below. If the device is readonly,
1775 * temporarily switch to read-write to do this.
1776 */
1777 if (XFS_IS_UQUOTA_ON(mp) && uip == NULL) {
1778 error = xfs_qm_qino_alloc(mp, &uip,
1779 flags | XFS_QMOPT_UQUOTA);
1780 if (error)
1781 goto error_rele;
1782
1783 flags &= ~XFS_QMOPT_SBVERSION;
1784 }
1785 if (XFS_IS_GQUOTA_ON(mp) && gip == NULL) {
1786 error = xfs_qm_qino_alloc(mp, &gip,
1787 flags | XFS_QMOPT_GQUOTA);
1788 if (error)
1789 goto error_rele;
1790
1791 flags &= ~XFS_QMOPT_SBVERSION;
1792 }
1793 if (XFS_IS_PQUOTA_ON(mp) && pip == NULL) {
1794 error = xfs_qm_qino_alloc(mp, &pip,
1795 flags | XFS_QMOPT_PQUOTA);
1796 if (error)
1797 goto error_rele;
1798 }
1799
1800 mp->m_quotainfo->qi_uquotaip = uip;
1801 mp->m_quotainfo->qi_gquotaip = gip;
1802 mp->m_quotainfo->qi_pquotaip = pip;
1803
1804 return 0;
1805
1806 error_rele:
1807 if (uip)
1808 xfs_irele(uip);
1809 if (gip)
1810 xfs_irele(gip);
1811 if (pip)
1812 xfs_irele(pip);
1813 return error;
1814 }
1815
1816 STATIC void
xfs_qm_dqfree_one(struct xfs_dquot * dqp)1817 xfs_qm_dqfree_one(
1818 struct xfs_dquot *dqp)
1819 {
1820 struct xfs_mount *mp = dqp->q_mount;
1821 struct xfs_quotainfo *qi = mp->m_quotainfo;
1822
1823 mutex_lock(&qi->qi_tree_lock);
1824 radix_tree_delete(xfs_dquot_tree(qi, xfs_dquot_type(dqp)), dqp->q_id);
1825
1826 qi->qi_dquots--;
1827 mutex_unlock(&qi->qi_tree_lock);
1828
1829 xfs_qm_dqdestroy(dqp);
1830 }
1831
1832 /* --------------- utility functions for vnodeops ---------------- */
1833
1834
1835 /*
1836 * Given an inode, a uid, gid and prid make sure that we have
1837 * allocated relevant dquot(s) on disk, and that we won't exceed inode
1838 * quotas by creating this file.
1839 * This also attaches dquot(s) to the given inode after locking it,
1840 * and returns the dquots corresponding to the uid and/or gid.
1841 *
1842 * in : inode (unlocked)
1843 * out : udquot, gdquot with references taken and unlocked
1844 */
1845 int
xfs_qm_vop_dqalloc(struct xfs_inode * ip,kuid_t uid,kgid_t gid,prid_t prid,uint flags,struct xfs_dquot ** O_udqpp,struct xfs_dquot ** O_gdqpp,struct xfs_dquot ** O_pdqpp)1846 xfs_qm_vop_dqalloc(
1847 struct xfs_inode *ip,
1848 kuid_t uid,
1849 kgid_t gid,
1850 prid_t prid,
1851 uint flags,
1852 struct xfs_dquot **O_udqpp,
1853 struct xfs_dquot **O_gdqpp,
1854 struct xfs_dquot **O_pdqpp)
1855 {
1856 struct xfs_mount *mp = ip->i_mount;
1857 struct inode *inode = VFS_I(ip);
1858 struct user_namespace *user_ns = inode->i_sb->s_user_ns;
1859 struct xfs_dquot *uq = NULL;
1860 struct xfs_dquot *gq = NULL;
1861 struct xfs_dquot *pq = NULL;
1862 int error;
1863
1864 if (!XFS_IS_QUOTA_ON(mp))
1865 return 0;
1866
1867 ASSERT(!xfs_is_metadir_inode(ip));
1868
1869 if ((flags & XFS_QMOPT_INHERIT) && XFS_INHERIT_GID(ip))
1870 gid = inode->i_gid;
1871
1872 /*
1873 * Attach the dquot(s) to this inode, doing a dquot allocation
1874 * if necessary. The dquot(s) will not be locked.
1875 */
1876 if (XFS_NOT_DQATTACHED(mp, ip)) {
1877 xfs_ilock(ip, XFS_ILOCK_EXCL);
1878 error = xfs_qm_dqattach_locked(ip, true);
1879 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1880 if (error)
1881 return error;
1882 }
1883
1884 if ((flags & XFS_QMOPT_UQUOTA) && XFS_IS_UQUOTA_ON(mp)) {
1885 ASSERT(O_udqpp);
1886 if (!uid_eq(inode->i_uid, uid)) {
1887 error = xfs_qm_dqget(mp, from_kuid(user_ns, uid),
1888 XFS_DQTYPE_USER, true, &uq);
1889 if (error) {
1890 ASSERT(error != -ENOENT);
1891 return error;
1892 }
1893 } else {
1894 /*
1895 * Take an extra reference, because we'll return
1896 * this to caller
1897 */
1898 ASSERT(ip->i_udquot);
1899 uq = xfs_qm_dqhold(ip->i_udquot);
1900 }
1901 }
1902 if ((flags & XFS_QMOPT_GQUOTA) && XFS_IS_GQUOTA_ON(mp)) {
1903 ASSERT(O_gdqpp);
1904 if (!gid_eq(inode->i_gid, gid)) {
1905 error = xfs_qm_dqget(mp, from_kgid(user_ns, gid),
1906 XFS_DQTYPE_GROUP, true, &gq);
1907 if (error) {
1908 ASSERT(error != -ENOENT);
1909 goto error_rele;
1910 }
1911 } else {
1912 ASSERT(ip->i_gdquot);
1913 gq = xfs_qm_dqhold(ip->i_gdquot);
1914 }
1915 }
1916 if ((flags & XFS_QMOPT_PQUOTA) && XFS_IS_PQUOTA_ON(mp)) {
1917 ASSERT(O_pdqpp);
1918 if (ip->i_projid != prid) {
1919 error = xfs_qm_dqget(mp, prid,
1920 XFS_DQTYPE_PROJ, true, &pq);
1921 if (error) {
1922 ASSERT(error != -ENOENT);
1923 goto error_rele;
1924 }
1925 } else {
1926 ASSERT(ip->i_pdquot);
1927 pq = xfs_qm_dqhold(ip->i_pdquot);
1928 }
1929 }
1930 trace_xfs_dquot_dqalloc(ip);
1931
1932 if (O_udqpp)
1933 *O_udqpp = uq;
1934 else
1935 xfs_qm_dqrele(uq);
1936 if (O_gdqpp)
1937 *O_gdqpp = gq;
1938 else
1939 xfs_qm_dqrele(gq);
1940 if (O_pdqpp)
1941 *O_pdqpp = pq;
1942 else
1943 xfs_qm_dqrele(pq);
1944 return 0;
1945
1946 error_rele:
1947 xfs_qm_dqrele(gq);
1948 xfs_qm_dqrele(uq);
1949 return error;
1950 }
1951
1952 /*
1953 * Actually transfer ownership, and do dquot modifications.
1954 * These were already reserved.
1955 */
1956 struct xfs_dquot *
xfs_qm_vop_chown(struct xfs_trans * tp,struct xfs_inode * ip,struct xfs_dquot ** IO_olddq,struct xfs_dquot * newdq)1957 xfs_qm_vop_chown(
1958 struct xfs_trans *tp,
1959 struct xfs_inode *ip,
1960 struct xfs_dquot **IO_olddq,
1961 struct xfs_dquot *newdq)
1962 {
1963 struct xfs_dquot *prevdq;
1964 xfs_filblks_t dblocks, rblocks;
1965 bool isrt = XFS_IS_REALTIME_INODE(ip);
1966
1967 xfs_assert_ilocked(ip, XFS_ILOCK_EXCL);
1968 ASSERT(XFS_IS_QUOTA_ON(ip->i_mount));
1969 ASSERT(!xfs_is_metadir_inode(ip));
1970
1971 /* old dquot */
1972 prevdq = *IO_olddq;
1973 ASSERT(prevdq);
1974 ASSERT(prevdq != newdq);
1975
1976 xfs_inode_count_blocks(tp, ip, &dblocks, &rblocks);
1977
1978 xfs_trans_mod_ino_dquot(tp, ip, prevdq, XFS_TRANS_DQ_BCOUNT,
1979 -(xfs_qcnt_t)dblocks);
1980 xfs_trans_mod_ino_dquot(tp, ip, prevdq, XFS_TRANS_DQ_RTBCOUNT,
1981 -(xfs_qcnt_t)rblocks);
1982 xfs_trans_mod_ino_dquot(tp, ip, prevdq, XFS_TRANS_DQ_ICOUNT, -1);
1983
1984 /* the sparkling new dquot */
1985 xfs_trans_mod_ino_dquot(tp, ip, newdq, XFS_TRANS_DQ_BCOUNT, dblocks);
1986 xfs_trans_mod_ino_dquot(tp, ip, newdq, XFS_TRANS_DQ_RTBCOUNT, rblocks);
1987 xfs_trans_mod_ino_dquot(tp, ip, newdq, XFS_TRANS_DQ_ICOUNT, 1);
1988
1989 /*
1990 * Back when we made quota reservations for the chown, we reserved the
1991 * ondisk blocks + delalloc blocks with the new dquot. Now that we've
1992 * switched the dquots, decrease the new dquot's block reservation
1993 * (having already bumped up the real counter) so that we don't have
1994 * any reservation to give back when we commit.
1995 */
1996 xfs_trans_mod_dquot(tp, newdq,
1997 isrt ? XFS_TRANS_DQ_RES_RTBLKS : XFS_TRANS_DQ_RES_BLKS,
1998 -ip->i_delayed_blks);
1999
2000 /*
2001 * Give the incore reservation for delalloc blocks back to the old
2002 * dquot. We don't normally handle delalloc quota reservations
2003 * transactionally, so just lock the dquot and subtract from the
2004 * reservation. Dirty the transaction because it's too late to turn
2005 * back now.
2006 */
2007 tp->t_flags |= XFS_TRANS_DIRTY;
2008 mutex_lock(&prevdq->q_qlock);
2009 if (isrt) {
2010 ASSERT(prevdq->q_rtb.reserved >= ip->i_delayed_blks);
2011 prevdq->q_rtb.reserved -= ip->i_delayed_blks;
2012 } else {
2013 ASSERT(prevdq->q_blk.reserved >= ip->i_delayed_blks);
2014 prevdq->q_blk.reserved -= ip->i_delayed_blks;
2015 }
2016 mutex_unlock(&prevdq->q_qlock);
2017
2018 /*
2019 * Take an extra reference, because the inode is going to keep
2020 * this dquot pointer even after the trans_commit.
2021 */
2022 *IO_olddq = xfs_qm_dqhold(newdq);
2023
2024 return prevdq;
2025 }
2026
2027 int
xfs_qm_vop_rename_dqattach(struct xfs_inode ** i_tab)2028 xfs_qm_vop_rename_dqattach(
2029 struct xfs_inode **i_tab)
2030 {
2031 struct xfs_mount *mp = i_tab[0]->i_mount;
2032 int i;
2033
2034 if (!XFS_IS_QUOTA_ON(mp))
2035 return 0;
2036
2037 for (i = 0; (i < 4 && i_tab[i]); i++) {
2038 struct xfs_inode *ip = i_tab[i];
2039 int error;
2040
2041 /*
2042 * Watch out for duplicate entries in the table.
2043 */
2044 if (i == 0 || ip != i_tab[i-1]) {
2045 if (XFS_NOT_DQATTACHED(mp, ip)) {
2046 error = xfs_qm_dqattach(ip);
2047 if (error)
2048 return error;
2049 }
2050 }
2051 }
2052 return 0;
2053 }
2054
2055 void
xfs_qm_vop_create_dqattach(struct xfs_trans * tp,struct xfs_inode * ip,struct xfs_dquot * udqp,struct xfs_dquot * gdqp,struct xfs_dquot * pdqp)2056 xfs_qm_vop_create_dqattach(
2057 struct xfs_trans *tp,
2058 struct xfs_inode *ip,
2059 struct xfs_dquot *udqp,
2060 struct xfs_dquot *gdqp,
2061 struct xfs_dquot *pdqp)
2062 {
2063 struct xfs_mount *mp = tp->t_mountp;
2064
2065 if (!XFS_IS_QUOTA_ON(mp))
2066 return;
2067
2068 xfs_assert_ilocked(ip, XFS_ILOCK_EXCL);
2069 ASSERT(!xfs_is_metadir_inode(ip));
2070
2071 if (udqp && XFS_IS_UQUOTA_ON(mp)) {
2072 ASSERT(ip->i_udquot == NULL);
2073 ASSERT(i_uid_read(VFS_I(ip)) == udqp->q_id);
2074
2075 ip->i_udquot = xfs_qm_dqhold(udqp);
2076 }
2077 if (gdqp && XFS_IS_GQUOTA_ON(mp)) {
2078 ASSERT(ip->i_gdquot == NULL);
2079 ASSERT(i_gid_read(VFS_I(ip)) == gdqp->q_id);
2080
2081 ip->i_gdquot = xfs_qm_dqhold(gdqp);
2082 }
2083 if (pdqp && XFS_IS_PQUOTA_ON(mp)) {
2084 ASSERT(ip->i_pdquot == NULL);
2085 ASSERT(ip->i_projid == pdqp->q_id);
2086
2087 ip->i_pdquot = xfs_qm_dqhold(pdqp);
2088 }
2089
2090 xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_ICOUNT, 1);
2091 }
2092
2093 /* Decide if this inode's dquot is near an enforcement boundary. */
2094 bool
xfs_inode_near_dquot_enforcement(struct xfs_inode * ip,xfs_dqtype_t type)2095 xfs_inode_near_dquot_enforcement(
2096 struct xfs_inode *ip,
2097 xfs_dqtype_t type)
2098 {
2099 struct xfs_dquot *dqp;
2100 struct xfs_dquot_res *res;
2101 struct xfs_dquot_pre *pre;
2102 int64_t freesp;
2103
2104 /* We only care for quotas that are enabled and enforced. */
2105 dqp = xfs_inode_dquot(ip, type);
2106 if (!dqp || !xfs_dquot_is_enforced(dqp))
2107 return false;
2108
2109 if (xfs_dquot_res_over_limits(&dqp->q_ino) ||
2110 xfs_dquot_res_over_limits(&dqp->q_blk) ||
2111 xfs_dquot_res_over_limits(&dqp->q_rtb))
2112 return true;
2113
2114 if (XFS_IS_REALTIME_INODE(ip)) {
2115 res = &dqp->q_rtb;
2116 pre = &dqp->q_rtb_prealloc;
2117 } else {
2118 res = &dqp->q_blk;
2119 pre = &dqp->q_blk_prealloc;
2120 }
2121
2122 /* For space on the data device, check the various thresholds. */
2123 if (!pre->q_prealloc_hi_wmark)
2124 return false;
2125
2126 if (res->reserved < pre->q_prealloc_lo_wmark)
2127 return false;
2128
2129 if (res->reserved >= pre->q_prealloc_hi_wmark)
2130 return true;
2131
2132 freesp = pre->q_prealloc_hi_wmark - res->reserved;
2133 if (freesp < pre->q_low_space[XFS_QLOWSP_5_PCNT])
2134 return true;
2135
2136 return false;
2137 }
2138