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