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