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