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