blob: 246790ad9589e1b607c2ac7e2519c1b0293d0484 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Nathan Scott4ce31212005-11-02 14:59:41 +11002 * Copyright (c) 2000-2005 Silicon Graphics, Inc.
3 * All Rights Reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
Nathan Scott4ce31212005-11-02 14:59:41 +11005 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * published by the Free Software Foundation.
8 *
Nathan Scott4ce31212005-11-02 14:59:41 +11009 * 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.
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 *
Nathan Scott4ce31212005-11-02 14:59:41 +110014 * 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
Linus Torvalds1da177e2005-04-16 15:20:36 -070017 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include "xfs.h"
19#include "xfs_fs.h"
Nathan Scotta844f452005-11-02 14:38:42 +110020#include "xfs_bit.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include "xfs_log.h"
Nathan Scotta844f452005-11-02 14:38:42 +110022#include "xfs_inum.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include "xfs_trans.h"
24#include "xfs_sb.h"
25#include "xfs_ag.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include "xfs_dir2.h"
27#include "xfs_alloc.h"
28#include "xfs_dmapi.h"
29#include "xfs_quota.h"
30#include "xfs_mount.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include "xfs_bmap_btree.h"
Nathan Scotta844f452005-11-02 14:38:42 +110032#include "xfs_alloc_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include "xfs_ialloc_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include "xfs_dir2_sf.h"
Nathan Scotta844f452005-11-02 14:38:42 +110035#include "xfs_attr_sf.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include "xfs_dinode.h"
37#include "xfs_inode.h"
Nathan Scotta844f452005-11-02 14:38:42 +110038#include "xfs_btree.h"
39#include "xfs_ialloc.h"
40#include "xfs_itable.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#include "xfs_rtalloc.h"
42#include "xfs_error.h"
Nathan Scotta844f452005-11-02 14:38:42 +110043#include "xfs_bmap.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#include "xfs_rw.h"
45#include "xfs_acl.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070046#include "xfs_attr.h"
47#include "xfs_buf_item.h"
48#include "xfs_trans_space.h"
49#include "xfs_utils.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070050#include "xfs_qm.h"
51
52/*
53 * The global quota manager. There is only one of these for the entire
54 * system, _not_ one per file system. XQM keeps track of the overall
55 * quota functionality, including maintaining the freelist and hash
56 * tables of dquots.
57 */
Nathan Scott6b3f6b52005-11-02 15:08:25 +110058mutex_t xfs_Gqm_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -070059struct xfs_qm *xfs_Gqm;
Nathan Scott6b3f6b52005-11-02 15:08:25 +110060uint ndquot;
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
62kmem_zone_t *qm_dqzone;
63kmem_zone_t *qm_dqtrxzone;
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
David Chinner7989cb82007-02-10 18:34:56 +110065static cred_t xfs_zerocr;
Nathan Scottdae81d42006-02-28 12:30:13 +110066
Linus Torvalds1da177e2005-04-16 15:20:36 -070067STATIC void xfs_qm_list_init(xfs_dqlist_t *, char *, int);
68STATIC void xfs_qm_list_destroy(xfs_dqlist_t *);
69
Christoph Hellwigba0f32d2005-06-21 15:36:52 +100070STATIC void xfs_qm_freelist_init(xfs_frlist_t *);
71STATIC void xfs_qm_freelist_destroy(xfs_frlist_t *);
72STATIC int xfs_qm_mplist_nowait(xfs_mount_t *);
73STATIC int xfs_qm_dqhashlock_nowait(xfs_dquot_t *);
74
Linus Torvalds1da177e2005-04-16 15:20:36 -070075STATIC int xfs_qm_init_quotainos(xfs_mount_t *);
Christoph Hellwigba0f32d2005-06-21 15:36:52 +100076STATIC int xfs_qm_init_quotainfo(xfs_mount_t *);
Al Viro51bfb752005-12-15 09:17:24 +000077STATIC int xfs_qm_shake(int, gfp_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
Rusty Russell8e1f9362007-07-17 04:03:17 -070079static struct shrinker xfs_qm_shaker = {
80 .shrink = xfs_qm_shake,
81 .seeks = DEFAULT_SEEKS,
82};
83
Linus Torvalds1da177e2005-04-16 15:20:36 -070084#ifdef DEBUG
85extern mutex_t qcheck_lock;
86#endif
87
88#ifdef QUOTADEBUG
89#define XQM_LIST_PRINT(l, NXT, title) \
90{ \
91 xfs_dquot_t *dqp; int i = 0; \
92 cmn_err(CE_DEBUG, "%s (#%d)", title, (int) (l)->qh_nelems); \
93 for (dqp = (l)->qh_next; dqp != NULL; dqp = dqp->NXT) { \
94 cmn_err(CE_DEBUG, " %d. \"%d (%s)\" " \
95 "bcnt = %d, icnt = %d, refs = %d", \
Christoph Hellwig1149d962005-11-02 15:01:12 +110096 ++i, (int) be32_to_cpu(dqp->q_core.d_id), \
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 DQFLAGTO_TYPESTR(dqp), \
Christoph Hellwig1149d962005-11-02 15:01:12 +110098 (int) be64_to_cpu(dqp->q_core.d_bcount), \
99 (int) be64_to_cpu(dqp->q_core.d_icount), \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 (int) dqp->q_nrefs); } \
101}
102#else
103#define XQM_LIST_PRINT(l, NXT, title) do { } while (0)
104#endif
105
106/*
107 * Initialize the XQM structure.
108 * Note that there is not one quota manager per file system.
109 */
110STATIC struct xfs_qm *
111xfs_Gqm_init(void)
112{
Nathan Scott6b3f6b52005-11-02 15:08:25 +1100113 xfs_dqhash_t *udqhash, *gdqhash;
114 xfs_qm_t *xqm;
Nathan Scott215101c2006-09-28 11:04:43 +1000115 size_t hsize;
116 uint i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117
118 /*
119 * Initialize the dquot hash tables.
120 */
Nathan Scott77e46352006-09-28 11:03:27 +1000121 udqhash = kmem_zalloc_greedy(&hsize,
Eric Sandeen5995cb72007-08-16 16:49:11 +1000122 XFS_QM_HASHSIZE_LOW * sizeof(xfs_dqhash_t),
123 XFS_QM_HASHSIZE_HIGH * sizeof(xfs_dqhash_t),
Nathan Scott77e46352006-09-28 11:03:27 +1000124 KM_SLEEP | KM_MAYFAIL | KM_LARGE);
125 gdqhash = kmem_zalloc(hsize, KM_SLEEP | KM_LARGE);
126 hsize /= sizeof(xfs_dqhash_t);
Nathan Scott6b3f6b52005-11-02 15:08:25 +1100127 ndquot = hsize << 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128
Nathan Scott6b3f6b52005-11-02 15:08:25 +1100129 xqm = kmem_zalloc(sizeof(xfs_qm_t), KM_SLEEP);
130 xqm->qm_dqhashmask = hsize - 1;
131 xqm->qm_usr_dqhtable = udqhash;
132 xqm->qm_grp_dqhtable = gdqhash;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 ASSERT(xqm->qm_usr_dqhtable != NULL);
134 ASSERT(xqm->qm_grp_dqhtable != NULL);
135
136 for (i = 0; i < hsize; i++) {
137 xfs_qm_list_init(&(xqm->qm_usr_dqhtable[i]), "uxdqh", i);
138 xfs_qm_list_init(&(xqm->qm_grp_dqhtable[i]), "gxdqh", i);
139 }
140
141 /*
142 * Freelist of all dquots of all file systems
143 */
144 xfs_qm_freelist_init(&(xqm->qm_dqfreelist));
145
146 /*
147 * dquot zone. we register our own low-memory callback.
148 */
149 if (!qm_dqzone) {
150 xqm->qm_dqzone = kmem_zone_init(sizeof(xfs_dquot_t),
151 "xfs_dquots");
152 qm_dqzone = xqm->qm_dqzone;
153 } else
154 xqm->qm_dqzone = qm_dqzone;
155
Rusty Russell8e1f9362007-07-17 04:03:17 -0700156 register_shrinker(&xfs_qm_shaker);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157
158 /*
159 * The t_dqinfo portion of transactions.
160 */
161 if (!qm_dqtrxzone) {
162 xqm->qm_dqtrxzone = kmem_zone_init(sizeof(xfs_dquot_acct_t),
163 "xfs_dqtrx");
164 qm_dqtrxzone = xqm->qm_dqtrxzone;
165 } else
166 xqm->qm_dqtrxzone = qm_dqtrxzone;
167
168 atomic_set(&xqm->qm_totaldquots, 0);
169 xqm->qm_dqfree_ratio = XFS_QM_DQFREE_RATIO;
170 xqm->qm_nrefs = 0;
171#ifdef DEBUG
Nathan Scottc2e81432006-01-16 16:21:34 +1100172 mutex_init(&qcheck_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173#endif
174 return xqm;
175}
176
177/*
178 * Destroy the global quota manager when its reference count goes to zero.
179 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +1000180STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181xfs_qm_destroy(
182 struct xfs_qm *xqm)
183{
184 int hsize, i;
185
186 ASSERT(xqm != NULL);
187 ASSERT(xqm->qm_nrefs == 0);
Rusty Russell8e1f9362007-07-17 04:03:17 -0700188 unregister_shrinker(&xfs_qm_shaker);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 hsize = xqm->qm_dqhashmask + 1;
190 for (i = 0; i < hsize; i++) {
191 xfs_qm_list_destroy(&(xqm->qm_usr_dqhtable[i]));
192 xfs_qm_list_destroy(&(xqm->qm_grp_dqhtable[i]));
193 }
Denys Vlasenkof0e2d932008-05-19 16:31:57 +1000194 kmem_free(xqm->qm_usr_dqhtable);
195 kmem_free(xqm->qm_grp_dqhtable);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 xqm->qm_usr_dqhtable = NULL;
197 xqm->qm_grp_dqhtable = NULL;
198 xqm->qm_dqhashmask = 0;
199 xfs_qm_freelist_destroy(&(xqm->qm_dqfreelist));
200#ifdef DEBUG
201 mutex_destroy(&qcheck_lock);
202#endif
Denys Vlasenkof0e2d932008-05-19 16:31:57 +1000203 kmem_free(xqm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204}
205
206/*
207 * Called at mount time to let XQM know that another file system is
208 * starting quotas. This isn't crucial information as the individual mount
209 * structures are pretty independent, but it helps the XQM keep a
210 * global view of what's going on.
211 */
212/* ARGSUSED */
213STATIC int
214xfs_qm_hold_quotafs_ref(
215 struct xfs_mount *mp)
216{
217 /*
218 * Need to lock the xfs_Gqm structure for things like this. For example,
219 * the structure could disappear between the entry to this routine and
220 * a HOLD operation if not locked.
221 */
Christoph Hellwige2494582009-02-09 08:38:39 +0100222 mutex_lock(&xfs_Gqm_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223
224 if (xfs_Gqm == NULL)
225 xfs_Gqm = xfs_Gqm_init();
226 /*
227 * We can keep a list of all filesystems with quotas mounted for
228 * debugging and statistical purposes, but ...
229 * Just take a reference and get out.
230 */
Christoph Hellwige2494582009-02-09 08:38:39 +0100231 xfs_Gqm->qm_nrefs++;
232 mutex_unlock(&xfs_Gqm_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233
234 return 0;
235}
236
237
238/*
239 * Release the reference that a filesystem took at mount time,
240 * so that we know when we need to destroy the entire quota manager.
241 */
242/* ARGSUSED */
243STATIC void
244xfs_qm_rele_quotafs_ref(
245 struct xfs_mount *mp)
246{
247 xfs_dquot_t *dqp, *nextdqp;
248
249 ASSERT(xfs_Gqm);
250 ASSERT(xfs_Gqm->qm_nrefs > 0);
251
252 /*
253 * Go thru the freelist and destroy all inactive dquots.
254 */
255 xfs_qm_freelist_lock(xfs_Gqm);
256
257 for (dqp = xfs_Gqm->qm_dqfreelist.qh_next;
258 dqp != (xfs_dquot_t *)&(xfs_Gqm->qm_dqfreelist); ) {
259 xfs_dqlock(dqp);
260 nextdqp = dqp->dq_flnext;
261 if (dqp->dq_flags & XFS_DQ_INACTIVE) {
262 ASSERT(dqp->q_mount == NULL);
263 ASSERT(! XFS_DQ_IS_DIRTY(dqp));
264 ASSERT(dqp->HL_PREVP == NULL);
265 ASSERT(dqp->MPL_PREVP == NULL);
266 XQM_FREELIST_REMOVE(dqp);
267 xfs_dqunlock(dqp);
268 xfs_qm_dqdestroy(dqp);
269 } else {
270 xfs_dqunlock(dqp);
271 }
272 dqp = nextdqp;
273 }
274 xfs_qm_freelist_unlock(xfs_Gqm);
275
276 /*
277 * Destroy the entire XQM. If somebody mounts with quotaon, this'll
278 * be restarted.
279 */
Christoph Hellwige2494582009-02-09 08:38:39 +0100280 mutex_lock(&xfs_Gqm_lock);
281 if (--xfs_Gqm->qm_nrefs == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 xfs_qm_destroy(xfs_Gqm);
283 xfs_Gqm = NULL;
284 }
Christoph Hellwige2494582009-02-09 08:38:39 +0100285 mutex_unlock(&xfs_Gqm_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286}
287
288/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 * Just destroy the quotainfo structure.
290 */
291void
292xfs_qm_unmount_quotadestroy(
293 xfs_mount_t *mp)
294{
295 if (mp->m_quotainfo)
296 xfs_qm_destroy_quotainfo(mp);
297}
298
299
300/*
301 * This is called from xfs_mountfs to start quotas and initialize all
302 * necessary data structures like quotainfo. This is also responsible for
303 * running a quotacheck as necessary. We are guaranteed that the superblock
304 * is consistently read in at this point.
David Chinner53aa7912008-04-10 12:20:31 +1000305 *
306 * If we fail here, the mount will continue with quota turned off. We don't
307 * need to inidicate success or failure at all.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 */
David Chinner53aa7912008-04-10 12:20:31 +1000309void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310xfs_qm_mount_quotas(
Christoph Hellwig42490232008-08-13 16:49:32 +1000311 xfs_mount_t *mp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 int error = 0;
314 uint sbf;
315
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 /*
317 * If quotas on realtime volumes is not supported, we disable
318 * quotas immediately.
319 */
320 if (mp->m_sb.sb_rextents) {
321 cmn_err(CE_NOTE,
322 "Cannot turn on quotas for realtime filesystem %s",
323 mp->m_fsname);
324 mp->m_qflags = 0;
325 goto write_changes;
326 }
327
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 ASSERT(XFS_IS_QUOTA_RUNNING(mp));
Nathan Scott155ffd02005-09-02 16:43:48 +1000329
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 /*
331 * Allocate the quotainfo structure inside the mount struct, and
332 * create quotainode(s), and change/rev superblock if necessary.
333 */
David Chinner53aa7912008-04-10 12:20:31 +1000334 error = xfs_qm_init_quotainfo(mp);
335 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 /*
337 * We must turn off quotas.
338 */
339 ASSERT(mp->m_quotainfo == NULL);
340 mp->m_qflags = 0;
341 goto write_changes;
342 }
343 /*
344 * If any of the quotas are not consistent, do a quotacheck.
345 */
Christoph Hellwig42490232008-08-13 16:49:32 +1000346 if (XFS_QM_NEED_QUOTACHECK(mp)) {
David Chinner53aa7912008-04-10 12:20:31 +1000347 error = xfs_qm_quotacheck(mp);
348 if (error) {
349 /* Quotacheck failed and disabled quotas. */
350 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 }
Donald Douwsma646d5bd2007-05-08 13:49:09 +1000353 /*
354 * If one type of quotas is off, then it will lose its
355 * quotachecked status, since we won't be doing accounting for
356 * that type anymore.
357 */
David Chinner53aa7912008-04-10 12:20:31 +1000358 if (!XFS_IS_UQUOTA_ON(mp))
Donald Douwsma646d5bd2007-05-08 13:49:09 +1000359 mp->m_qflags &= ~XFS_UQUOTA_CHKD;
David Chinner53aa7912008-04-10 12:20:31 +1000360 if (!(XFS_IS_GQUOTA_ON(mp) || XFS_IS_PQUOTA_ON(mp)))
Donald Douwsma646d5bd2007-05-08 13:49:09 +1000361 mp->m_qflags &= ~XFS_OQUOTA_CHKD;
Nathan Scott155ffd02005-09-02 16:43:48 +1000362
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 write_changes:
364 /*
Eric Sandeen3685c2a2007-10-11 17:42:32 +1000365 * We actually don't have to acquire the m_sb_lock at all.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 * This can only be called from mount, and that's single threaded. XXX
367 */
Eric Sandeen3685c2a2007-10-11 17:42:32 +1000368 spin_lock(&mp->m_sb_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 sbf = mp->m_sb.sb_qflags;
370 mp->m_sb.sb_qflags = mp->m_qflags & XFS_MOUNT_QUOTA_ALL;
Eric Sandeen3685c2a2007-10-11 17:42:32 +1000371 spin_unlock(&mp->m_sb_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372
373 if (sbf != (mp->m_qflags & XFS_MOUNT_QUOTA_ALL)) {
374 if (xfs_qm_write_sb_changes(mp, XFS_SB_QFLAGS)) {
375 /*
376 * We could only have been turning quotas off.
377 * We aren't in very good shape actually because
378 * the incore structures are convinced that quotas are
379 * off, but the on disk superblock doesn't know that !
380 */
381 ASSERT(!(XFS_IS_QUOTA_RUNNING(mp)));
382 xfs_fs_cmn_err(CE_ALERT, mp,
383 "XFS mount_quotas: Superblock update failed!");
384 }
385 }
386
387 if (error) {
388 xfs_fs_cmn_err(CE_WARN, mp,
389 "Failed to initialize disk quotas.");
390 }
David Chinner53aa7912008-04-10 12:20:31 +1000391 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392}
393
394/*
395 * Called from the vfsops layer.
396 */
Christoph Hellwige57481d2008-12-03 12:20:36 +0100397void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398xfs_qm_unmount_quotas(
399 xfs_mount_t *mp)
400{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 /*
402 * Release the dquots that root inode, et al might be holding,
403 * before we flush quotas and blow away the quotainfo structure.
404 */
405 ASSERT(mp->m_rootip);
406 xfs_qm_dqdetach(mp->m_rootip);
407 if (mp->m_rbmip)
408 xfs_qm_dqdetach(mp->m_rbmip);
409 if (mp->m_rsumip)
410 xfs_qm_dqdetach(mp->m_rsumip);
411
412 /*
Christoph Hellwige57481d2008-12-03 12:20:36 +0100413 * Release the quota inodes.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 if (mp->m_quotainfo) {
Christoph Hellwige57481d2008-12-03 12:20:36 +0100416 if (mp->m_quotainfo->qi_uquotaip) {
417 IRELE(mp->m_quotainfo->qi_uquotaip);
418 mp->m_quotainfo->qi_uquotaip = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 }
Christoph Hellwige57481d2008-12-03 12:20:36 +0100420 if (mp->m_quotainfo->qi_gquotaip) {
421 IRELE(mp->m_quotainfo->qi_gquotaip);
422 mp->m_quotainfo->qi_gquotaip = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 }
424 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425}
426
427/*
428 * Flush all dquots of the given file system to disk. The dquots are
429 * _not_ purged from memory here, just their data written to disk.
430 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +1000431STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432xfs_qm_dqflush_all(
433 xfs_mount_t *mp,
434 int flags)
435{
436 int recl;
437 xfs_dquot_t *dqp;
438 int niters;
439 int error;
440
441 if (mp->m_quotainfo == NULL)
Jesper Juhl014c2542006-01-15 02:37:08 +0100442 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 niters = 0;
444again:
445 xfs_qm_mplist_lock(mp);
446 FOREACH_DQUOT_IN_MP(dqp, mp) {
447 xfs_dqlock(dqp);
448 if (! XFS_DQ_IS_DIRTY(dqp)) {
449 xfs_dqunlock(dqp);
450 continue;
451 }
452 xfs_dqtrace_entry(dqp, "FLUSHALL: DQDIRTY");
453 /* XXX a sentinel would be better */
454 recl = XFS_QI_MPLRECLAIMS(mp);
David Chinnere1f49cf2008-08-13 16:41:43 +1000455 if (!xfs_dqflock_nowait(dqp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 /*
457 * If we can't grab the flush lock then check
458 * to see if the dquot has been flushed delayed
459 * write. If so, grab its buffer and send it
460 * out immediately. We'll be able to acquire
461 * the flush lock when the I/O completes.
462 */
463 xfs_qm_dqflock_pushbuf_wait(dqp);
464 }
465 /*
466 * Let go of the mplist lock. We don't want to hold it
467 * across a disk write.
468 */
469 xfs_qm_mplist_unlock(mp);
470 error = xfs_qm_dqflush(dqp, flags);
471 xfs_dqunlock(dqp);
472 if (error)
Jesper Juhl014c2542006-01-15 02:37:08 +0100473 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474
475 xfs_qm_mplist_lock(mp);
476 if (recl != XFS_QI_MPLRECLAIMS(mp)) {
477 xfs_qm_mplist_unlock(mp);
478 /* XXX restart limit */
479 goto again;
480 }
481 }
482
483 xfs_qm_mplist_unlock(mp);
484 /* return ! busy */
Jesper Juhl014c2542006-01-15 02:37:08 +0100485 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486}
487/*
488 * Release the group dquot pointers the user dquots may be
489 * carrying around as a hint. mplist is locked on entry and exit.
490 */
491STATIC void
492xfs_qm_detach_gdquots(
493 xfs_mount_t *mp)
494{
495 xfs_dquot_t *dqp, *gdqp;
496 int nrecl;
497
498 again:
499 ASSERT(XFS_QM_IS_MPLIST_LOCKED(mp));
500 dqp = XFS_QI_MPLNEXT(mp);
501 while (dqp) {
502 xfs_dqlock(dqp);
503 if ((gdqp = dqp->q_gdquot)) {
504 xfs_dqlock(gdqp);
505 dqp->q_gdquot = NULL;
506 }
507 xfs_dqunlock(dqp);
508
509 if (gdqp) {
510 /*
511 * Can't hold the mplist lock across a dqput.
512 * XXXmust convert to marker based iterations here.
513 */
514 nrecl = XFS_QI_MPLRECLAIMS(mp);
515 xfs_qm_mplist_unlock(mp);
516 xfs_qm_dqput(gdqp);
517
518 xfs_qm_mplist_lock(mp);
519 if (nrecl != XFS_QI_MPLRECLAIMS(mp))
520 goto again;
521 }
522 dqp = dqp->MPL_NEXT;
523 }
524}
525
526/*
527 * Go through all the incore dquots of this file system and take them
528 * off the mplist and hashlist, if the dquot type matches the dqtype
529 * parameter. This is used when turning off quota accounting for
530 * users and/or groups, as well as when the filesystem is unmounting.
531 */
532STATIC int
533xfs_qm_dqpurge_int(
534 xfs_mount_t *mp,
Nathan Scottc8ad20f2005-06-21 15:38:48 +1000535 uint flags) /* QUOTAOFF/UMOUNTING/UQUOTA/PQUOTA/GQUOTA */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536{
537 xfs_dquot_t *dqp;
538 uint dqtype;
539 int nrecl;
540 xfs_dquot_t *nextdqp;
541 int nmisses;
542
543 if (mp->m_quotainfo == NULL)
Jesper Juhl014c2542006-01-15 02:37:08 +0100544 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545
546 dqtype = (flags & XFS_QMOPT_UQUOTA) ? XFS_DQ_USER : 0;
Nathan Scottc8ad20f2005-06-21 15:38:48 +1000547 dqtype |= (flags & XFS_QMOPT_PQUOTA) ? XFS_DQ_PROJ : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 dqtype |= (flags & XFS_QMOPT_GQUOTA) ? XFS_DQ_GROUP : 0;
549
550 xfs_qm_mplist_lock(mp);
551
552 /*
553 * In the first pass through all incore dquots of this filesystem,
554 * we release the group dquot pointers the user dquots may be
555 * carrying around as a hint. We need to do this irrespective of
556 * what's being turned off.
557 */
558 xfs_qm_detach_gdquots(mp);
559
560 again:
561 nmisses = 0;
562 ASSERT(XFS_QM_IS_MPLIST_LOCKED(mp));
563 /*
564 * Try to get rid of all of the unwanted dquots. The idea is to
565 * get them off mplist and hashlist, but leave them on freelist.
566 */
567 dqp = XFS_QI_MPLNEXT(mp);
568 while (dqp) {
569 /*
570 * It's OK to look at the type without taking dqlock here.
571 * We're holding the mplist lock here, and that's needed for
572 * a dqreclaim.
573 */
574 if ((dqp->dq_flags & dqtype) == 0) {
575 dqp = dqp->MPL_NEXT;
576 continue;
577 }
578
579 if (! xfs_qm_dqhashlock_nowait(dqp)) {
580 nrecl = XFS_QI_MPLRECLAIMS(mp);
581 xfs_qm_mplist_unlock(mp);
582 XFS_DQ_HASH_LOCK(dqp->q_hash);
583 xfs_qm_mplist_lock(mp);
584
585 /*
586 * XXXTheoretically, we can get into a very long
587 * ping pong game here.
588 * No one can be adding dquots to the mplist at
589 * this point, but somebody might be taking things off.
590 */
591 if (nrecl != XFS_QI_MPLRECLAIMS(mp)) {
592 XFS_DQ_HASH_UNLOCK(dqp->q_hash);
593 goto again;
594 }
595 }
596
597 /*
598 * Take the dquot off the mplist and hashlist. It may remain on
599 * freelist in INACTIVE state.
600 */
601 nextdqp = dqp->MPL_NEXT;
Denys Vlasenko4f0e8a92008-05-19 16:34:04 +1000602 nmisses += xfs_qm_dqpurge(dqp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 dqp = nextdqp;
604 }
605 xfs_qm_mplist_unlock(mp);
606 return nmisses;
607}
608
609int
610xfs_qm_dqpurge_all(
611 xfs_mount_t *mp,
612 uint flags)
613{
614 int ndquots;
615
616 /*
617 * Purge the dquot cache.
618 * None of the dquots should really be busy at this point.
619 */
620 if (mp->m_quotainfo) {
621 while ((ndquots = xfs_qm_dqpurge_int(mp, flags))) {
622 delay(ndquots * 10);
623 }
624 }
625 return 0;
626}
627
628STATIC int
629xfs_qm_dqattach_one(
630 xfs_inode_t *ip,
631 xfs_dqid_t id,
632 uint type,
633 uint doalloc,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 xfs_dquot_t *udqhint, /* hint */
635 xfs_dquot_t **IO_idqpp)
636{
637 xfs_dquot_t *dqp;
638 int error;
639
Christoph Hellwig579aa9c2008-04-22 17:34:00 +1000640 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 error = 0;
Christoph Hellwig8e9b6e72009-02-08 21:51:42 +0100642
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 /*
644 * See if we already have it in the inode itself. IO_idqpp is
645 * &i_udquot or &i_gdquot. This made the code look weird, but
646 * made the logic a lot simpler.
647 */
Christoph Hellwig8e9b6e72009-02-08 21:51:42 +0100648 dqp = *IO_idqpp;
649 if (dqp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 xfs_dqtrace_entry(dqp, "DQATTACH: found in ip");
Christoph Hellwig8e9b6e72009-02-08 21:51:42 +0100651 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 }
653
654 /*
655 * udqhint is the i_udquot field in inode, and is non-NULL only
Nathan Scottc8ad20f2005-06-21 15:38:48 +1000656 * when the type arg is group/project. Its purpose is to save a
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 * lookup by dqid (xfs_qm_dqget) by caching a group dquot inside
658 * the user dquot.
659 */
Christoph Hellwig8e9b6e72009-02-08 21:51:42 +0100660 if (udqhint) {
661 ASSERT(type == XFS_DQ_GROUP || type == XFS_DQ_PROJ);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 xfs_dqlock(udqhint);
663
Christoph Hellwig8e9b6e72009-02-08 21:51:42 +0100664 /*
665 * No need to take dqlock to look at the id.
666 *
667 * The ID can't change until it gets reclaimed, and it won't
668 * be reclaimed as long as we have a ref from inode and we
669 * hold the ilock.
670 */
671 dqp = udqhint->q_gdquot;
672 if (dqp && be32_to_cpu(dqp->q_core.d_id) == id) {
673 xfs_dqlock(dqp);
674 XFS_DQHOLD(dqp);
675 ASSERT(*IO_idqpp == NULL);
676 *IO_idqpp = dqp;
677
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 xfs_dqunlock(dqp);
679 xfs_dqunlock(udqhint);
Christoph Hellwig8e9b6e72009-02-08 21:51:42 +0100680 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 }
Christoph Hellwig8e9b6e72009-02-08 21:51:42 +0100682
683 /*
684 * We can't hold a dquot lock when we call the dqget code.
685 * We'll deadlock in no time, because of (not conforming to)
686 * lock ordering - the inodelock comes before any dquot lock,
687 * and we may drop and reacquire the ilock in xfs_qm_dqget().
688 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 xfs_dqunlock(udqhint);
Christoph Hellwig8e9b6e72009-02-08 21:51:42 +0100690 }
691
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 /*
693 * Find the dquot from somewhere. This bumps the
694 * reference count of dquot and returns it locked.
695 * This can return ENOENT if dquot didn't exist on
696 * disk and we didn't ask it to allocate;
697 * ESRCH if quotas got turned off suddenly.
698 */
Christoph Hellwig8e9b6e72009-02-08 21:51:42 +0100699 error = xfs_qm_dqget(ip->i_mount, ip, id, type, XFS_QMOPT_DOWARN, &dqp);
700 if (error)
701 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702
703 xfs_dqtrace_entry(dqp, "DQATTACH: found by dqget");
Christoph Hellwig8e9b6e72009-02-08 21:51:42 +0100704
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 /*
706 * dqget may have dropped and re-acquired the ilock, but it guarantees
707 * that the dquot returned is the one that should go in the inode.
708 */
709 *IO_idqpp = dqp;
Christoph Hellwig8e9b6e72009-02-08 21:51:42 +0100710 xfs_dqunlock(dqp);
711 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712}
713
714
715/*
716 * Given a udquot and gdquot, attach a ptr to the group dquot in the
717 * udquot as a hint for future lookups. The idea sounds simple, but the
718 * execution isn't, because the udquot might have a group dquot attached
Nathan Scottc41564b2006-03-29 08:55:14 +1000719 * already and getting rid of that gets us into lock ordering constraints.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 * The process is complicated more by the fact that the dquots may or may not
721 * be locked on entry.
722 */
723STATIC void
724xfs_qm_dqattach_grouphint(
725 xfs_dquot_t *udq,
Christoph Hellwig8e9b6e72009-02-08 21:51:42 +0100726 xfs_dquot_t *gdq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727{
728 xfs_dquot_t *tmp;
729
Christoph Hellwig8e9b6e72009-02-08 21:51:42 +0100730 xfs_dqlock(udq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731
732 if ((tmp = udq->q_gdquot)) {
733 if (tmp == gdq) {
Christoph Hellwig8e9b6e72009-02-08 21:51:42 +0100734 xfs_dqunlock(udq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 return;
736 }
737
738 udq->q_gdquot = NULL;
739 /*
740 * We can't keep any dqlocks when calling dqrele,
741 * because the freelist lock comes before dqlocks.
742 */
743 xfs_dqunlock(udq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 /*
745 * we took a hard reference once upon a time in dqget,
746 * so give it back when the udquot no longer points at it
747 * dqput() does the unlocking of the dquot.
748 */
749 xfs_qm_dqrele(tmp);
750
751 xfs_dqlock(udq);
752 xfs_dqlock(gdq);
753
754 } else {
755 ASSERT(XFS_DQ_IS_LOCKED(udq));
Christoph Hellwig8e9b6e72009-02-08 21:51:42 +0100756 xfs_dqlock(gdq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 }
758
759 ASSERT(XFS_DQ_IS_LOCKED(udq));
760 ASSERT(XFS_DQ_IS_LOCKED(gdq));
761 /*
762 * Somebody could have attached a gdquot here,
763 * when we dropped the uqlock. If so, just do nothing.
764 */
765 if (udq->q_gdquot == NULL) {
766 XFS_DQHOLD(gdq);
767 udq->q_gdquot = gdq;
768 }
Christoph Hellwig8e9b6e72009-02-08 21:51:42 +0100769
770 xfs_dqunlock(gdq);
771 xfs_dqunlock(udq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772}
773
774
775/*
Nathan Scottc8ad20f2005-06-21 15:38:48 +1000776 * Given a locked inode, attach dquot(s) to it, taking U/G/P-QUOTAON
777 * into account.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 * If XFS_QMOPT_DQALLOC, the dquot(s) will be allocated if needed.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 * If XFS_QMOPT_ILOCKED, then inode sent is already locked EXCL.
780 * Inode may get unlocked and relocked in here, and the caller must deal with
781 * the consequences.
782 */
783int
784xfs_qm_dqattach(
785 xfs_inode_t *ip,
786 uint flags)
787{
788 xfs_mount_t *mp = ip->i_mount;
789 uint nquotas = 0;
790 int error = 0;
791
792 if ((! XFS_IS_QUOTA_ON(mp)) ||
793 (! XFS_NOT_DQATTACHED(mp, ip)) ||
794 (ip->i_ino == mp->m_sb.sb_uquotino) ||
795 (ip->i_ino == mp->m_sb.sb_gquotino))
Jesper Juhl014c2542006-01-15 02:37:08 +0100796 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797
798 ASSERT((flags & XFS_QMOPT_ILOCKED) == 0 ||
Christoph Hellwig579aa9c2008-04-22 17:34:00 +1000799 xfs_isilocked(ip, XFS_ILOCK_EXCL));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800
801 if (! (flags & XFS_QMOPT_ILOCKED))
802 xfs_ilock(ip, XFS_ILOCK_EXCL);
803
804 if (XFS_IS_UQUOTA_ON(mp)) {
805 error = xfs_qm_dqattach_one(ip, ip->i_d.di_uid, XFS_DQ_USER,
806 flags & XFS_QMOPT_DQALLOC,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 NULL, &ip->i_udquot);
808 if (error)
809 goto done;
810 nquotas++;
811 }
Christoph Hellwig579aa9c2008-04-22 17:34:00 +1000812
813 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
Nathan Scottc8ad20f2005-06-21 15:38:48 +1000814 if (XFS_IS_OQUOTA_ON(mp)) {
815 error = XFS_IS_GQUOTA_ON(mp) ?
816 xfs_qm_dqattach_one(ip, ip->i_d.di_gid, XFS_DQ_GROUP,
817 flags & XFS_QMOPT_DQALLOC,
Nathan Scottc8ad20f2005-06-21 15:38:48 +1000818 ip->i_udquot, &ip->i_gdquot) :
819 xfs_qm_dqattach_one(ip, ip->i_d.di_projid, XFS_DQ_PROJ,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 flags & XFS_QMOPT_DQALLOC,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 ip->i_udquot, &ip->i_gdquot);
822 /*
823 * Don't worry about the udquot that we may have
824 * attached above. It'll get detached, if not already.
825 */
826 if (error)
827 goto done;
828 nquotas++;
829 }
830
831 /*
832 * Attach this group quota to the user quota as a hint.
833 * This WON'T, in general, result in a thrash.
834 */
835 if (nquotas == 2) {
Christoph Hellwig579aa9c2008-04-22 17:34:00 +1000836 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 ASSERT(ip->i_udquot);
838 ASSERT(ip->i_gdquot);
839
840 /*
841 * We may or may not have the i_udquot locked at this point,
842 * but this check is OK since we don't depend on the i_gdquot to
843 * be accurate 100% all the time. It is just a hint, and this
844 * will succeed in general.
845 */
846 if (ip->i_udquot->q_gdquot == ip->i_gdquot)
847 goto done;
848 /*
849 * Attach i_gdquot to the gdquot hint inside the i_udquot.
850 */
Christoph Hellwig8e9b6e72009-02-08 21:51:42 +0100851 xfs_qm_dqattach_grouphint(ip->i_udquot, ip->i_gdquot);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 }
853
854 done:
855
856#ifdef QUOTADEBUG
857 if (! error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 if (XFS_IS_UQUOTA_ON(mp))
859 ASSERT(ip->i_udquot);
Nathan Scottc8ad20f2005-06-21 15:38:48 +1000860 if (XFS_IS_OQUOTA_ON(mp))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 ASSERT(ip->i_gdquot);
862 }
863#endif
864
865 if (! (flags & XFS_QMOPT_ILOCKED))
866 xfs_iunlock(ip, XFS_ILOCK_EXCL);
867
868#ifdef QUOTADEBUG
869 else
Christoph Hellwig579aa9c2008-04-22 17:34:00 +1000870 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871#endif
Jesper Juhl014c2542006-01-15 02:37:08 +0100872 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873}
874
875/*
876 * Release dquots (and their references) if any.
877 * The inode should be locked EXCL except when this's called by
878 * xfs_ireclaim.
879 */
880void
881xfs_qm_dqdetach(
882 xfs_inode_t *ip)
883{
884 if (!(ip->i_udquot || ip->i_gdquot))
885 return;
886
887 ASSERT(ip->i_ino != ip->i_mount->m_sb.sb_uquotino);
888 ASSERT(ip->i_ino != ip->i_mount->m_sb.sb_gquotino);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 if (ip->i_udquot) {
Nathan Scottc8ad20f2005-06-21 15:38:48 +1000890 xfs_dqtrace_entry_ino(ip->i_udquot, "DQDETTACH", ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 xfs_qm_dqrele(ip->i_udquot);
892 ip->i_udquot = NULL;
893 }
894 if (ip->i_gdquot) {
Nathan Scottc8ad20f2005-06-21 15:38:48 +1000895 xfs_dqtrace_entry_ino(ip->i_gdquot, "DQDETTACH", ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 xfs_qm_dqrele(ip->i_gdquot);
897 ip->i_gdquot = NULL;
898 }
899}
900
901/*
David Chinnera4e4c4f2008-10-30 17:16:11 +1100902 * This is called to sync quotas. We can be told to use non-blocking
903 * semantics by either the SYNC_BDFLUSH flag or the absence of the
904 * SYNC_WAIT flag.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906int
907xfs_qm_sync(
908 xfs_mount_t *mp,
Christoph Hellwigb09cc772007-08-30 17:19:57 +1000909 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910{
911 int recl, restarts;
912 xfs_dquot_t *dqp;
913 uint flush_flags;
914 boolean_t nowait;
915 int error;
916
Donald Douwsmad7577622007-11-23 16:27:42 +1100917 if (! XFS_IS_QUOTA_ON(mp))
918 return 0;
919
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 restarts = 0;
921 /*
922 * We won't block unless we are asked to.
923 */
924 nowait = (boolean_t)(flags & SYNC_BDFLUSH || (flags & SYNC_WAIT) == 0);
925
926 again:
927 xfs_qm_mplist_lock(mp);
928 /*
929 * dqpurge_all() also takes the mplist lock and iterate thru all dquots
930 * in quotaoff. However, if the QUOTA_ACTIVE bits are not cleared
931 * when we have the mplist lock, we know that dquots will be consistent
932 * as long as we have it locked.
933 */
934 if (! XFS_IS_QUOTA_ON(mp)) {
935 xfs_qm_mplist_unlock(mp);
Jesper Juhl014c2542006-01-15 02:37:08 +0100936 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 }
938 FOREACH_DQUOT_IN_MP(dqp, mp) {
939 /*
940 * If this is vfs_sync calling, then skip the dquots that
941 * don't 'seem' to be dirty. ie. don't acquire dqlock.
942 * This is very similar to what xfs_sync does with inodes.
943 */
944 if (flags & SYNC_BDFLUSH) {
945 if (! XFS_DQ_IS_DIRTY(dqp))
946 continue;
947 }
948
949 if (nowait) {
950 /*
951 * Try to acquire the dquot lock. We are NOT out of
952 * lock order, but we just don't want to wait for this
953 * lock, unless somebody wanted us to.
954 */
955 if (! xfs_qm_dqlock_nowait(dqp))
956 continue;
957 } else {
958 xfs_dqlock(dqp);
959 }
960
961 /*
962 * Now, find out for sure if this dquot is dirty or not.
963 */
964 if (! XFS_DQ_IS_DIRTY(dqp)) {
965 xfs_dqunlock(dqp);
966 continue;
967 }
968
969 /* XXX a sentinel would be better */
970 recl = XFS_QI_MPLRECLAIMS(mp);
David Chinnere1f49cf2008-08-13 16:41:43 +1000971 if (!xfs_dqflock_nowait(dqp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 if (nowait) {
973 xfs_dqunlock(dqp);
974 continue;
975 }
976 /*
977 * If we can't grab the flush lock then if the caller
Nathan Scottc41564b2006-03-29 08:55:14 +1000978 * really wanted us to give this our best shot, so
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 * see if we can give a push to the buffer before we wait
980 * on the flush lock. At this point, we know that
Nathan Scottc41564b2006-03-29 08:55:14 +1000981 * even though the dquot is being flushed,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 * it has (new) dirty data.
983 */
984 xfs_qm_dqflock_pushbuf_wait(dqp);
985 }
986 /*
987 * Let go of the mplist lock. We don't want to hold it
988 * across a disk write
989 */
990 flush_flags = (nowait) ? XFS_QMOPT_DELWRI : XFS_QMOPT_SYNC;
991 xfs_qm_mplist_unlock(mp);
992 xfs_dqtrace_entry(dqp, "XQM_SYNC: DQFLUSH");
993 error = xfs_qm_dqflush(dqp, flush_flags);
994 xfs_dqunlock(dqp);
995 if (error && XFS_FORCED_SHUTDOWN(mp))
Jesper Juhl014c2542006-01-15 02:37:08 +0100996 return 0; /* Need to prevent umount failure */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 else if (error)
Jesper Juhl014c2542006-01-15 02:37:08 +0100998 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999
1000 xfs_qm_mplist_lock(mp);
1001 if (recl != XFS_QI_MPLRECLAIMS(mp)) {
1002 if (++restarts >= XFS_QM_SYNC_MAX_RESTARTS)
1003 break;
1004
1005 xfs_qm_mplist_unlock(mp);
1006 goto again;
1007 }
1008 }
1009
1010 xfs_qm_mplist_unlock(mp);
Jesper Juhl014c2542006-01-15 02:37:08 +01001011 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012}
1013
Christoph Hellwiga4edd1d2009-01-19 02:03:11 +01001014/*
1015 * The hash chains and the mplist use the same xfs_dqhash structure as
1016 * their list head, but we can take the mplist qh_lock and one of the
1017 * hash qh_locks at the same time without any problem as they aren't
1018 * related.
1019 */
1020static struct lock_class_key xfs_quota_mplist_class;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021
1022/*
1023 * This initializes all the quota information that's kept in the
1024 * mount structure
1025 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +10001026STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027xfs_qm_init_quotainfo(
1028 xfs_mount_t *mp)
1029{
1030 xfs_quotainfo_t *qinf;
1031 int error;
1032 xfs_dquot_t *dqp;
1033
1034 ASSERT(XFS_IS_QUOTA_RUNNING(mp));
1035
1036 /*
1037 * Tell XQM that we exist as soon as possible.
1038 */
1039 if ((error = xfs_qm_hold_quotafs_ref(mp))) {
Jesper Juhl014c2542006-01-15 02:37:08 +01001040 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 }
1042
1043 qinf = mp->m_quotainfo = kmem_zalloc(sizeof(xfs_quotainfo_t), KM_SLEEP);
1044
1045 /*
1046 * See if quotainodes are setup, and if not, allocate them,
1047 * and change the superblock accordingly.
1048 */
1049 if ((error = xfs_qm_init_quotainos(mp))) {
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10001050 kmem_free(qinf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 mp->m_quotainfo = NULL;
Jesper Juhl014c2542006-01-15 02:37:08 +01001052 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 }
1054
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 xfs_qm_list_init(&qinf->qi_dqlist, "mpdqlist", 0);
Christoph Hellwiga4edd1d2009-01-19 02:03:11 +01001056 lockdep_set_class(&qinf->qi_dqlist.qh_lock, &xfs_quota_mplist_class);
1057
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 qinf->qi_dqreclaims = 0;
1059
1060 /* mutex used to serialize quotaoffs */
Jes Sorensen794ee1b2006-01-09 15:59:21 -08001061 mutex_init(&qinf->qi_quotaofflock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062
1063 /* Precalc some constants */
1064 qinf->qi_dqchunklen = XFS_FSB_TO_BB(mp, XFS_DQUOT_CLUSTER_SIZE_FSB);
1065 ASSERT(qinf->qi_dqchunklen);
1066 qinf->qi_dqperchunk = BBTOB(qinf->qi_dqchunklen);
1067 do_div(qinf->qi_dqperchunk, sizeof(xfs_dqblk_t));
1068
1069 mp->m_qflags |= (mp->m_sb.sb_qflags & XFS_ALL_QUOTA_CHKD);
1070
1071 /*
1072 * We try to get the limits from the superuser's limits fields.
1073 * This is quite hacky, but it is standard quota practice.
1074 * We look at the USR dquot with id == 0 first, but if user quotas
1075 * are not enabled we goto the GRP dquot with id == 0.
1076 * We don't really care to keep separate default limits for user
1077 * and group quotas, at least not at this point.
1078 */
1079 error = xfs_qm_dqget(mp, NULL, (xfs_dqid_t)0,
Nathan Scottc8ad20f2005-06-21 15:38:48 +10001080 XFS_IS_UQUOTA_RUNNING(mp) ? XFS_DQ_USER :
1081 (XFS_IS_GQUOTA_RUNNING(mp) ? XFS_DQ_GROUP :
1082 XFS_DQ_PROJ),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 XFS_QMOPT_DQSUSER|XFS_QMOPT_DOWARN,
1084 &dqp);
1085 if (! error) {
1086 xfs_disk_dquot_t *ddqp = &dqp->q_core;
1087
1088 /*
1089 * The warnings and timers set the grace period given to
1090 * a user or group before he or she can not perform any
1091 * more writing. If it is zero, a default is used.
1092 */
Christoph Hellwig1149d962005-11-02 15:01:12 +11001093 qinf->qi_btimelimit = ddqp->d_btimer ?
1094 be32_to_cpu(ddqp->d_btimer) : XFS_QM_BTIMELIMIT;
1095 qinf->qi_itimelimit = ddqp->d_itimer ?
1096 be32_to_cpu(ddqp->d_itimer) : XFS_QM_ITIMELIMIT;
1097 qinf->qi_rtbtimelimit = ddqp->d_rtbtimer ?
1098 be32_to_cpu(ddqp->d_rtbtimer) : XFS_QM_RTBTIMELIMIT;
1099 qinf->qi_bwarnlimit = ddqp->d_bwarns ?
1100 be16_to_cpu(ddqp->d_bwarns) : XFS_QM_BWARNLIMIT;
1101 qinf->qi_iwarnlimit = ddqp->d_iwarns ?
1102 be16_to_cpu(ddqp->d_iwarns) : XFS_QM_IWARNLIMIT;
1103 qinf->qi_rtbwarnlimit = ddqp->d_rtbwarns ?
1104 be16_to_cpu(ddqp->d_rtbwarns) : XFS_QM_RTBWARNLIMIT;
1105 qinf->qi_bhardlimit = be64_to_cpu(ddqp->d_blk_hardlimit);
1106 qinf->qi_bsoftlimit = be64_to_cpu(ddqp->d_blk_softlimit);
1107 qinf->qi_ihardlimit = be64_to_cpu(ddqp->d_ino_hardlimit);
1108 qinf->qi_isoftlimit = be64_to_cpu(ddqp->d_ino_softlimit);
1109 qinf->qi_rtbhardlimit = be64_to_cpu(ddqp->d_rtb_hardlimit);
1110 qinf->qi_rtbsoftlimit = be64_to_cpu(ddqp->d_rtb_softlimit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111
1112 /*
1113 * We sent the XFS_QMOPT_DQSUSER flag to dqget because
1114 * we don't want this dquot cached. We haven't done a
1115 * quotacheck yet, and quotacheck doesn't like incore dquots.
1116 */
1117 xfs_qm_dqdestroy(dqp);
1118 } else {
1119 qinf->qi_btimelimit = XFS_QM_BTIMELIMIT;
1120 qinf->qi_itimelimit = XFS_QM_ITIMELIMIT;
1121 qinf->qi_rtbtimelimit = XFS_QM_RTBTIMELIMIT;
1122 qinf->qi_bwarnlimit = XFS_QM_BWARNLIMIT;
1123 qinf->qi_iwarnlimit = XFS_QM_IWARNLIMIT;
Nathan Scott06d10dd2005-06-21 15:48:47 +10001124 qinf->qi_rtbwarnlimit = XFS_QM_RTBWARNLIMIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 }
1126
Jesper Juhl014c2542006-01-15 02:37:08 +01001127 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128}
1129
1130
1131/*
1132 * Gets called when unmounting a filesystem or when all quotas get
1133 * turned off.
1134 * This purges the quota inodes, destroys locks and frees itself.
1135 */
1136void
1137xfs_qm_destroy_quotainfo(
1138 xfs_mount_t *mp)
1139{
1140 xfs_quotainfo_t *qi;
1141
1142 qi = mp->m_quotainfo;
1143 ASSERT(qi != NULL);
1144 ASSERT(xfs_Gqm != NULL);
1145
1146 /*
1147 * Release the reference that XQM kept, so that we know
1148 * when the XQM structure should be freed. We cannot assume
1149 * that xfs_Gqm is non-null after this point.
1150 */
1151 xfs_qm_rele_quotafs_ref(mp);
1152
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 xfs_qm_list_destroy(&qi->qi_dqlist);
1154
1155 if (qi->qi_uquotaip) {
Christoph Hellwig26cc0022008-07-18 17:12:43 +10001156 IRELE(qi->qi_uquotaip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 qi->qi_uquotaip = NULL; /* paranoia */
1158 }
1159 if (qi->qi_gquotaip) {
Christoph Hellwig26cc0022008-07-18 17:12:43 +10001160 IRELE(qi->qi_gquotaip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 qi->qi_gquotaip = NULL;
1162 }
1163 mutex_destroy(&qi->qi_quotaofflock);
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10001164 kmem_free(qi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 mp->m_quotainfo = NULL;
1166}
1167
1168
1169
1170/* ------------------- PRIVATE STATIC FUNCTIONS ----------------------- */
1171
1172/* ARGSUSED */
1173STATIC void
1174xfs_qm_list_init(
1175 xfs_dqlist_t *list,
1176 char *str,
1177 int n)
1178{
Jes Sorensen794ee1b2006-01-09 15:59:21 -08001179 mutex_init(&list->qh_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 list->qh_next = NULL;
1181 list->qh_version = 0;
1182 list->qh_nelems = 0;
1183}
1184
1185STATIC void
1186xfs_qm_list_destroy(
1187 xfs_dqlist_t *list)
1188{
1189 mutex_destroy(&(list->qh_lock));
1190}
1191
1192
1193/*
1194 * Stripped down version of dqattach. This doesn't attach, or even look at the
1195 * dquots attached to the inode. The rationale is that there won't be any
1196 * attached at the time this is called from quotacheck.
1197 */
1198STATIC int
1199xfs_qm_dqget_noattach(
1200 xfs_inode_t *ip,
1201 xfs_dquot_t **O_udqpp,
1202 xfs_dquot_t **O_gdqpp)
1203{
1204 int error;
1205 xfs_mount_t *mp;
1206 xfs_dquot_t *udqp, *gdqp;
1207
Christoph Hellwig579aa9c2008-04-22 17:34:00 +10001208 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 mp = ip->i_mount;
1210 udqp = NULL;
1211 gdqp = NULL;
1212
1213 if (XFS_IS_UQUOTA_ON(mp)) {
1214 ASSERT(ip->i_udquot == NULL);
1215 /*
1216 * We want the dquot allocated if it doesn't exist.
1217 */
1218 if ((error = xfs_qm_dqget(mp, ip, ip->i_d.di_uid, XFS_DQ_USER,
1219 XFS_QMOPT_DQALLOC | XFS_QMOPT_DOWARN,
1220 &udqp))) {
1221 /*
1222 * Shouldn't be able to turn off quotas here.
1223 */
1224 ASSERT(error != ESRCH);
1225 ASSERT(error != ENOENT);
Jesper Juhl014c2542006-01-15 02:37:08 +01001226 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 }
1228 ASSERT(udqp);
1229 }
1230
Nathan Scottc8ad20f2005-06-21 15:38:48 +10001231 if (XFS_IS_OQUOTA_ON(mp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 ASSERT(ip->i_gdquot == NULL);
1233 if (udqp)
1234 xfs_dqunlock(udqp);
Nathan Scottc8ad20f2005-06-21 15:38:48 +10001235 error = XFS_IS_GQUOTA_ON(mp) ?
1236 xfs_qm_dqget(mp, ip,
1237 ip->i_d.di_gid, XFS_DQ_GROUP,
1238 XFS_QMOPT_DQALLOC|XFS_QMOPT_DOWARN,
1239 &gdqp) :
1240 xfs_qm_dqget(mp, ip,
1241 ip->i_d.di_projid, XFS_DQ_PROJ,
1242 XFS_QMOPT_DQALLOC|XFS_QMOPT_DOWARN,
1243 &gdqp);
1244 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 if (udqp)
1246 xfs_qm_dqrele(udqp);
1247 ASSERT(error != ESRCH);
1248 ASSERT(error != ENOENT);
Jesper Juhl014c2542006-01-15 02:37:08 +01001249 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 }
1251 ASSERT(gdqp);
1252
1253 /* Reacquire the locks in the right order */
1254 if (udqp) {
1255 if (! xfs_qm_dqlock_nowait(udqp)) {
1256 xfs_dqunlock(gdqp);
1257 xfs_dqlock(udqp);
1258 xfs_dqlock(gdqp);
1259 }
1260 }
1261 }
1262
1263 *O_udqpp = udqp;
1264 *O_gdqpp = gdqp;
1265
1266#ifdef QUOTADEBUG
1267 if (udqp) ASSERT(XFS_DQ_IS_LOCKED(udqp));
1268 if (gdqp) ASSERT(XFS_DQ_IS_LOCKED(gdqp));
1269#endif
Jesper Juhl014c2542006-01-15 02:37:08 +01001270 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271}
1272
1273/*
1274 * Create an inode and return with a reference already taken, but unlocked
1275 * This is how we create quota inodes
1276 */
1277STATIC int
1278xfs_qm_qino_alloc(
1279 xfs_mount_t *mp,
1280 xfs_inode_t **ip,
1281 __int64_t sbfields,
1282 uint flags)
1283{
1284 xfs_trans_t *tp;
1285 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 int committed;
1287
Nathan Scott061f7202006-01-11 15:27:50 +11001288 tp = xfs_trans_alloc(mp, XFS_TRANS_QM_QINOCREATE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 if ((error = xfs_trans_reserve(tp,
1290 XFS_QM_QINOCREATE_SPACE_RES(mp),
1291 XFS_CREATE_LOG_RES(mp), 0,
1292 XFS_TRANS_PERM_LOG_RES,
1293 XFS_CREATE_LOG_COUNT))) {
1294 xfs_trans_cancel(tp, 0);
Jesper Juhl014c2542006-01-15 02:37:08 +01001295 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297
David Chinnerb11f94d2007-07-11 11:09:33 +10001298 if ((error = xfs_dir_ialloc(&tp, NULL, S_IFREG, 1, 0,
Nathan Scottdae81d42006-02-28 12:30:13 +11001299 &xfs_zerocr, 0, 1, ip, &committed))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES |
1301 XFS_TRANS_ABORT);
Jesper Juhl014c2542006-01-15 02:37:08 +01001302 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 }
1304
1305 /*
1306 * Keep an extra reference to this quota inode. This inode is
1307 * locked exclusively and joined to the transaction already.
1308 */
Christoph Hellwig579aa9c2008-04-22 17:34:00 +10001309 ASSERT(xfs_isilocked(*ip, XFS_ILOCK_EXCL));
Christoph Hellwig26cc0022008-07-18 17:12:43 +10001310 IHOLD(*ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311
1312 /*
1313 * Make the changes in the superblock, and log those too.
1314 * sbfields arg may contain fields other than *QUOTINO;
1315 * VERSIONNUM for example.
1316 */
Eric Sandeen3685c2a2007-10-11 17:42:32 +10001317 spin_lock(&mp->m_sb_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 if (flags & XFS_QMOPT_SBVERSION) {
1319#if defined(DEBUG) && defined(XFS_LOUD_RECOVERY)
1320 unsigned oldv = mp->m_sb.sb_versionnum;
1321#endif
Eric Sandeen62118702008-03-06 13:44:28 +11001322 ASSERT(!xfs_sb_version_hasquota(&mp->m_sb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 ASSERT((sbfields & (XFS_SB_VERSIONNUM | XFS_SB_UQUOTINO |
1324 XFS_SB_GQUOTINO | XFS_SB_QFLAGS)) ==
1325 (XFS_SB_VERSIONNUM | XFS_SB_UQUOTINO |
1326 XFS_SB_GQUOTINO | XFS_SB_QFLAGS));
1327
Eric Sandeen62118702008-03-06 13:44:28 +11001328 xfs_sb_version_addquota(&mp->m_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 mp->m_sb.sb_uquotino = NULLFSINO;
1330 mp->m_sb.sb_gquotino = NULLFSINO;
1331
1332 /* qflags will get updated _after_ quotacheck */
1333 mp->m_sb.sb_qflags = 0;
1334#if defined(DEBUG) && defined(XFS_LOUD_RECOVERY)
1335 cmn_err(CE_NOTE,
1336 "Old superblock version %x, converting to %x.",
1337 oldv, mp->m_sb.sb_versionnum);
1338#endif
1339 }
1340 if (flags & XFS_QMOPT_UQUOTA)
1341 mp->m_sb.sb_uquotino = (*ip)->i_ino;
1342 else
1343 mp->m_sb.sb_gquotino = (*ip)->i_ino;
Eric Sandeen3685c2a2007-10-11 17:42:32 +10001344 spin_unlock(&mp->m_sb_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 xfs_mod_sb(tp, sbfields);
1346
Eric Sandeen1c72bf92007-05-08 13:48:42 +10001347 if ((error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 xfs_fs_cmn_err(CE_ALERT, mp, "XFS qino_alloc failed!");
Jesper Juhl014c2542006-01-15 02:37:08 +01001349 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 }
Jesper Juhl014c2542006-01-15 02:37:08 +01001351 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352}
1353
1354
David Chinner5b139732008-04-10 12:20:10 +10001355STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356xfs_qm_reset_dqcounts(
1357 xfs_mount_t *mp,
1358 xfs_buf_t *bp,
1359 xfs_dqid_t id,
1360 uint type)
1361{
1362 xfs_disk_dquot_t *ddq;
1363 int j;
1364
1365 xfs_buftrace("RESET DQUOTS", bp);
1366 /*
1367 * Reset all counters and timers. They'll be
1368 * started afresh by xfs_qm_quotacheck.
1369 */
1370#ifdef DEBUG
1371 j = XFS_FSB_TO_B(mp, XFS_DQUOT_CLUSTER_SIZE_FSB);
1372 do_div(j, sizeof(xfs_dqblk_t));
1373 ASSERT(XFS_QM_DQPERBLK(mp) == j);
1374#endif
1375 ddq = (xfs_disk_dquot_t *)XFS_BUF_PTR(bp);
1376 for (j = 0; j < XFS_QM_DQPERBLK(mp); j++) {
1377 /*
1378 * Do a sanity check, and if needed, repair the dqblk. Don't
1379 * output any warnings because it's perfectly possible to
Nathan Scottc41564b2006-03-29 08:55:14 +10001380 * find uninitialised dquot blks. See comment in xfs_qm_dqcheck.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 */
1382 (void) xfs_qm_dqcheck(ddq, id+j, type, XFS_QMOPT_DQREPAIR,
1383 "xfs_quotacheck");
Christoph Hellwig1149d962005-11-02 15:01:12 +11001384 ddq->d_bcount = 0;
1385 ddq->d_icount = 0;
1386 ddq->d_rtbcount = 0;
1387 ddq->d_btimer = 0;
1388 ddq->d_itimer = 0;
1389 ddq->d_rtbtimer = 0;
1390 ddq->d_bwarns = 0;
1391 ddq->d_iwarns = 0;
1392 ddq->d_rtbwarns = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393 ddq = (xfs_disk_dquot_t *) ((xfs_dqblk_t *)ddq + 1);
1394 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395}
1396
1397STATIC int
1398xfs_qm_dqiter_bufs(
1399 xfs_mount_t *mp,
1400 xfs_dqid_t firstid,
1401 xfs_fsblock_t bno,
1402 xfs_filblks_t blkcnt,
1403 uint flags)
1404{
1405 xfs_buf_t *bp;
1406 int error;
1407 int notcommitted;
1408 int incr;
Nathan Scottc8ad20f2005-06-21 15:38:48 +10001409 int type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410
1411 ASSERT(blkcnt > 0);
1412 notcommitted = 0;
1413 incr = (blkcnt > XFS_QM_MAX_DQCLUSTER_LOGSZ) ?
1414 XFS_QM_MAX_DQCLUSTER_LOGSZ : blkcnt;
Nathan Scottc8ad20f2005-06-21 15:38:48 +10001415 type = flags & XFS_QMOPT_UQUOTA ? XFS_DQ_USER :
1416 (flags & XFS_QMOPT_PQUOTA ? XFS_DQ_PROJ : XFS_DQ_GROUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 error = 0;
1418
1419 /*
1420 * Blkcnt arg can be a very big number, and might even be
1421 * larger than the log itself. So, we have to break it up into
1422 * manageable-sized transactions.
1423 * Note that we don't start a permanent transaction here; we might
1424 * not be able to get a log reservation for the whole thing up front,
1425 * and we don't really care to either, because we just discard
1426 * everything if we were to crash in the middle of this loop.
1427 */
1428 while (blkcnt--) {
1429 error = xfs_trans_read_buf(mp, NULL, mp->m_ddev_targp,
1430 XFS_FSB_TO_DADDR(mp, bno),
1431 (int)XFS_QI_DQCHUNKLEN(mp), 0, &bp);
1432 if (error)
1433 break;
1434
David Chinner5b139732008-04-10 12:20:10 +10001435 xfs_qm_reset_dqcounts(mp, bp, firstid, type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436 xfs_bdwrite(mp, bp);
1437 /*
1438 * goto the next block.
1439 */
1440 bno++;
1441 firstid += XFS_QM_DQPERBLK(mp);
1442 }
Jesper Juhl014c2542006-01-15 02:37:08 +01001443 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444}
1445
1446/*
Nathan Scottc8ad20f2005-06-21 15:38:48 +10001447 * Iterate over all allocated USR/GRP/PRJ dquots in the system, calling a
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448 * caller supplied function for every chunk of dquots that we find.
1449 */
1450STATIC int
1451xfs_qm_dqiterate(
1452 xfs_mount_t *mp,
1453 xfs_inode_t *qip,
1454 uint flags)
1455{
1456 xfs_bmbt_irec_t *map;
1457 int i, nmaps; /* number of map entries */
1458 int error; /* return value */
1459 xfs_fileoff_t lblkno;
1460 xfs_filblks_t maxlblkcnt;
1461 xfs_dqid_t firstid;
1462 xfs_fsblock_t rablkno;
1463 xfs_filblks_t rablkcnt;
1464
1465 error = 0;
1466 /*
Nathan Scottc41564b2006-03-29 08:55:14 +10001467 * This looks racy, but we can't keep an inode lock across a
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468 * trans_reserve. But, this gets called during quotacheck, and that
1469 * happens only at mount time which is single threaded.
1470 */
1471 if (qip->i_d.di_nblocks == 0)
Jesper Juhl014c2542006-01-15 02:37:08 +01001472 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473
1474 map = kmem_alloc(XFS_DQITER_MAP_SIZE * sizeof(*map), KM_SLEEP);
1475
1476 lblkno = 0;
1477 maxlblkcnt = XFS_B_TO_FSB(mp, (xfs_ufsize_t)XFS_MAXIOFFSET(mp));
1478 do {
1479 nmaps = XFS_DQITER_MAP_SIZE;
1480 /*
1481 * We aren't changing the inode itself. Just changing
1482 * some of its data. No new blocks are added here, and
1483 * the inode is never added to the transaction.
1484 */
1485 xfs_ilock(qip, XFS_ILOCK_SHARED);
1486 error = xfs_bmapi(NULL, qip, lblkno,
1487 maxlblkcnt - lblkno,
1488 XFS_BMAPI_METADATA,
1489 NULL,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001490 0, map, &nmaps, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491 xfs_iunlock(qip, XFS_ILOCK_SHARED);
1492 if (error)
1493 break;
1494
1495 ASSERT(nmaps <= XFS_DQITER_MAP_SIZE);
1496 for (i = 0; i < nmaps; i++) {
1497 ASSERT(map[i].br_startblock != DELAYSTARTBLOCK);
1498 ASSERT(map[i].br_blockcount);
1499
1500
1501 lblkno += map[i].br_blockcount;
1502
1503 if (map[i].br_startblock == HOLESTARTBLOCK)
1504 continue;
1505
1506 firstid = (xfs_dqid_t) map[i].br_startoff *
1507 XFS_QM_DQPERBLK(mp);
1508 /*
1509 * Do a read-ahead on the next extent.
1510 */
1511 if ((i+1 < nmaps) &&
1512 (map[i+1].br_startblock != HOLESTARTBLOCK)) {
1513 rablkcnt = map[i+1].br_blockcount;
1514 rablkno = map[i+1].br_startblock;
1515 while (rablkcnt--) {
1516 xfs_baread(mp->m_ddev_targp,
1517 XFS_FSB_TO_DADDR(mp, rablkno),
1518 (int)XFS_QI_DQCHUNKLEN(mp));
1519 rablkno++;
1520 }
1521 }
1522 /*
1523 * Iterate thru all the blks in the extent and
1524 * reset the counters of all the dquots inside them.
1525 */
1526 if ((error = xfs_qm_dqiter_bufs(mp,
1527 firstid,
1528 map[i].br_startblock,
1529 map[i].br_blockcount,
1530 flags))) {
1531 break;
1532 }
1533 }
1534
1535 if (error)
1536 break;
1537 } while (nmaps > 0);
1538
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10001539 kmem_free(map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540
Jesper Juhl014c2542006-01-15 02:37:08 +01001541 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542}
1543
1544/*
1545 * Called by dqusage_adjust in doing a quotacheck.
1546 * Given the inode, and a dquot (either USR or GRP, doesn't matter),
1547 * this updates its incore copy as well as the buffer copy. This is
1548 * so that once the quotacheck is done, we can just log all the buffers,
1549 * as opposed to logging numerous updates to individual dquots.
1550 */
1551STATIC void
1552xfs_qm_quotacheck_dqadjust(
1553 xfs_dquot_t *dqp,
1554 xfs_qcnt_t nblks,
1555 xfs_qcnt_t rtblks)
1556{
1557 ASSERT(XFS_DQ_IS_LOCKED(dqp));
1558 xfs_dqtrace_entry(dqp, "QCHECK DQADJUST");
1559 /*
1560 * Adjust the inode count and the block count to reflect this inode's
1561 * resource usage.
1562 */
Marcin Slusarz413d57c2008-02-13 15:03:29 -08001563 be64_add_cpu(&dqp->q_core.d_icount, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564 dqp->q_res_icount++;
1565 if (nblks) {
Marcin Slusarz413d57c2008-02-13 15:03:29 -08001566 be64_add_cpu(&dqp->q_core.d_bcount, nblks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567 dqp->q_res_bcount += nblks;
1568 }
1569 if (rtblks) {
Marcin Slusarz413d57c2008-02-13 15:03:29 -08001570 be64_add_cpu(&dqp->q_core.d_rtbcount, rtblks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571 dqp->q_res_rtbcount += rtblks;
1572 }
1573
1574 /*
1575 * Set default limits, adjust timers (since we changed usages)
1576 */
1577 if (! XFS_IS_SUSER_DQUOT(dqp)) {
1578 xfs_qm_adjust_dqlimits(dqp->q_mount, &dqp->q_core);
1579 xfs_qm_adjust_dqtimers(dqp->q_mount, &dqp->q_core);
1580 }
1581
1582 dqp->dq_flags |= XFS_DQ_DIRTY;
1583}
1584
1585STATIC int
1586xfs_qm_get_rtblks(
1587 xfs_inode_t *ip,
1588 xfs_qcnt_t *O_rtblks)
1589{
1590 xfs_filblks_t rtblks; /* total rt blks */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001591 xfs_extnum_t idx; /* extent record index */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592 xfs_ifork_t *ifp; /* inode fork pointer */
1593 xfs_extnum_t nextents; /* number of extent entries */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594 int error;
1595
1596 ASSERT(XFS_IS_REALTIME_INODE(ip));
1597 ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
1598 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
1599 if ((error = xfs_iread_extents(NULL, ip, XFS_DATA_FORK)))
Jesper Juhl014c2542006-01-15 02:37:08 +01001600 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601 }
1602 rtblks = 0;
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001603 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10001604 for (idx = 0; idx < nextents; idx++)
1605 rtblks += xfs_bmbt_get_blockcount(xfs_iext_get_ext(ifp, idx));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606 *O_rtblks = (xfs_qcnt_t)rtblks;
Jesper Juhl014c2542006-01-15 02:37:08 +01001607 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608}
1609
1610/*
1611 * callback routine supplied to bulkstat(). Given an inumber, find its
1612 * dquots and update them to account for resources taken by that inode.
1613 */
1614/* ARGSUSED */
1615STATIC int
1616xfs_qm_dqusage_adjust(
1617 xfs_mount_t *mp, /* mount point for filesystem */
1618 xfs_ino_t ino, /* inode number to get data for */
1619 void __user *buffer, /* not used */
1620 int ubsize, /* not used */
1621 void *private_data, /* not used */
1622 xfs_daddr_t bno, /* starting block of inode cluster */
1623 int *ubused, /* not used */
1624 void *dip, /* on-disk inode pointer (not used) */
1625 int *res) /* result code value */
1626{
1627 xfs_inode_t *ip;
1628 xfs_dquot_t *udqp, *gdqp;
1629 xfs_qcnt_t nblks, rtblks;
1630 int error;
1631
1632 ASSERT(XFS_IS_QUOTA_RUNNING(mp));
1633
1634 /*
1635 * rootino must have its resources accounted for, not so with the quota
1636 * inodes.
1637 */
1638 if (ino == mp->m_sb.sb_uquotino || ino == mp->m_sb.sb_gquotino) {
1639 *res = BULKSTAT_RV_NOTHING;
1640 return XFS_ERROR(EINVAL);
1641 }
1642
1643 /*
1644 * We don't _need_ to take the ilock EXCL. However, the xfs_qm_dqget
1645 * interface expects the inode to be exclusively locked because that's
1646 * the case in all other instances. It's OK that we do this because
1647 * quotacheck is done only at mount time.
1648 */
1649 if ((error = xfs_iget(mp, NULL, ino, 0, XFS_ILOCK_EXCL, &ip, bno))) {
1650 *res = BULKSTAT_RV_NOTHING;
Jesper Juhl014c2542006-01-15 02:37:08 +01001651 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652 }
1653
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654 /*
1655 * Obtain the locked dquots. In case of an error (eg. allocation
1656 * fails for ENOSPC), we return the negative of the error number
1657 * to bulkstat, so that it can get propagated to quotacheck() and
1658 * making us disable quotas for the file system.
1659 */
1660 if ((error = xfs_qm_dqget_noattach(ip, &udqp, &gdqp))) {
1661 xfs_iput(ip, XFS_ILOCK_EXCL);
1662 *res = BULKSTAT_RV_GIVEUP;
Jesper Juhl014c2542006-01-15 02:37:08 +01001663 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664 }
1665
1666 rtblks = 0;
1667 if (! XFS_IS_REALTIME_INODE(ip)) {
1668 nblks = (xfs_qcnt_t)ip->i_d.di_nblocks;
1669 } else {
1670 /*
1671 * Walk thru the extent list and count the realtime blocks.
1672 */
1673 if ((error = xfs_qm_get_rtblks(ip, &rtblks))) {
1674 xfs_iput(ip, XFS_ILOCK_EXCL);
1675 if (udqp)
1676 xfs_qm_dqput(udqp);
1677 if (gdqp)
1678 xfs_qm_dqput(gdqp);
1679 *res = BULKSTAT_RV_GIVEUP;
Jesper Juhl014c2542006-01-15 02:37:08 +01001680 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681 }
1682 nblks = (xfs_qcnt_t)ip->i_d.di_nblocks - rtblks;
1683 }
1684 ASSERT(ip->i_delayed_blks == 0);
1685
1686 /*
1687 * We can't release the inode while holding its dquot locks.
1688 * The inode can go into inactive and might try to acquire the dquotlocks.
1689 * So, just unlock here and do a vn_rele at the end.
1690 */
1691 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1692
1693 /*
1694 * Add the (disk blocks and inode) resources occupied by this
1695 * inode to its dquots. We do this adjustment in the incore dquot,
1696 * and also copy the changes to its buffer.
1697 * We don't care about putting these changes in a transaction
1698 * envelope because if we crash in the middle of a 'quotacheck'
1699 * we have to start from the beginning anyway.
1700 * Once we're done, we'll log all the dquot bufs.
1701 *
Nathan Scottc41564b2006-03-29 08:55:14 +10001702 * The *QUOTA_ON checks below may look pretty racy, but quotachecks
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703 * and quotaoffs don't race. (Quotachecks happen at mount time only).
1704 */
1705 if (XFS_IS_UQUOTA_ON(mp)) {
1706 ASSERT(udqp);
1707 xfs_qm_quotacheck_dqadjust(udqp, nblks, rtblks);
1708 xfs_qm_dqput(udqp);
1709 }
Nathan Scottc8ad20f2005-06-21 15:38:48 +10001710 if (XFS_IS_OQUOTA_ON(mp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711 ASSERT(gdqp);
1712 xfs_qm_quotacheck_dqadjust(gdqp, nblks, rtblks);
1713 xfs_qm_dqput(gdqp);
1714 }
1715 /*
1716 * Now release the inode. This will send it to 'inactive', and
1717 * possibly even free blocks.
1718 */
Christoph Hellwig43355092008-03-27 18:01:08 +11001719 IRELE(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720
1721 /*
1722 * Goto next inode.
1723 */
1724 *res = BULKSTAT_RV_DIDONE;
Jesper Juhl014c2542006-01-15 02:37:08 +01001725 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726}
1727
1728/*
1729 * Walk thru all the filesystem inodes and construct a consistent view
1730 * of the disk quota world. If the quotacheck fails, disable quotas.
1731 */
1732int
1733xfs_qm_quotacheck(
1734 xfs_mount_t *mp)
1735{
1736 int done, count, error;
1737 xfs_ino_t lastino;
1738 size_t structsz;
1739 xfs_inode_t *uip, *gip;
1740 uint flags;
1741
1742 count = INT_MAX;
1743 structsz = 1;
1744 lastino = 0;
1745 flags = 0;
1746
1747 ASSERT(XFS_QI_UQIP(mp) || XFS_QI_GQIP(mp));
1748 ASSERT(XFS_IS_QUOTA_RUNNING(mp));
1749
1750 /*
1751 * There should be no cached dquots. The (simplistic) quotacheck
1752 * algorithm doesn't like that.
1753 */
1754 ASSERT(XFS_QI_MPLNDQUOTS(mp) == 0);
1755
1756 cmn_err(CE_NOTE, "XFS quotacheck %s: Please wait.", mp->m_fsname);
1757
1758 /*
Nathan Scottc8ad20f2005-06-21 15:38:48 +10001759 * First we go thru all the dquots on disk, USR and GRP/PRJ, and reset
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760 * their counters to zero. We need a clean slate.
1761 * We don't log our changes till later.
1762 */
1763 if ((uip = XFS_QI_UQIP(mp))) {
1764 if ((error = xfs_qm_dqiterate(mp, uip, XFS_QMOPT_UQUOTA)))
1765 goto error_return;
1766 flags |= XFS_UQUOTA_CHKD;
1767 }
1768
1769 if ((gip = XFS_QI_GQIP(mp))) {
Nathan Scottc8ad20f2005-06-21 15:38:48 +10001770 if ((error = xfs_qm_dqiterate(mp, gip, XFS_IS_GQUOTA_ON(mp) ?
1771 XFS_QMOPT_GQUOTA : XFS_QMOPT_PQUOTA)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772 goto error_return;
Nathan Scottc8ad20f2005-06-21 15:38:48 +10001773 flags |= XFS_OQUOTA_CHKD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774 }
1775
1776 do {
1777 /*
1778 * Iterate thru all the inodes in the file system,
1779 * adjusting the corresponding dquot counters in core.
1780 */
1781 if ((error = xfs_bulkstat(mp, &lastino, &count,
1782 xfs_qm_dqusage_adjust, NULL,
Nathan Scott9c488762006-06-09 15:29:22 +10001783 structsz, NULL, BULKSTAT_FG_IGET, &done)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784 break;
1785
1786 } while (! done);
1787
1788 /*
David Chinner4b8879d2008-04-10 12:20:17 +10001789 * We've made all the changes that we need to make incore.
1790 * Flush them down to disk buffers if everything was updated
1791 * successfully.
1792 */
1793 if (!error)
1794 error = xfs_qm_dqflush_all(mp, XFS_QMOPT_DELWRI);
1795
1796 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797 * We can get this error if we couldn't do a dquot allocation inside
1798 * xfs_qm_dqusage_adjust (via bulkstat). We don't care about the
1799 * dirty dquots that might be cached, we just want to get rid of them
1800 * and turn quotaoff. The dquots won't be attached to any of the inodes
1801 * at this point (because we intentionally didn't in dqget_noattach).
1802 */
1803 if (error) {
Nathan Scottee2a4f72006-01-11 15:33:36 +11001804 xfs_qm_dqpurge_all(mp, XFS_QMOPT_QUOTALL | XFS_QMOPT_QUOTAOFF);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805 goto error_return;
1806 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807
1808 /*
1809 * We didn't log anything, because if we crashed, we'll have to
1810 * start the quotacheck from scratch anyway. However, we must make
1811 * sure that our dquot changes are secure before we put the
1812 * quotacheck'd stamp on the superblock. So, here we do a synchronous
1813 * flush.
1814 */
1815 XFS_bflush(mp->m_ddev_targp);
1816
1817 /*
1818 * If one type of quotas is off, then it will lose its
1819 * quotachecked status, since we won't be doing accounting for
1820 * that type anymore.
1821 */
Nathan Scottc8ad20f2005-06-21 15:38:48 +10001822 mp->m_qflags &= ~(XFS_OQUOTA_CHKD | XFS_UQUOTA_CHKD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823 mp->m_qflags |= flags;
1824
1825 XQM_LIST_PRINT(&(XFS_QI_MPL_LIST(mp)), MPL_NEXT, "++++ Mp list +++");
1826
1827 error_return:
1828 if (error) {
1829 cmn_err(CE_WARN, "XFS quotacheck %s: Unsuccessful (Error %d): "
1830 "Disabling quotas.",
1831 mp->m_fsname, error);
1832 /*
1833 * We must turn off quotas.
1834 */
1835 ASSERT(mp->m_quotainfo != NULL);
1836 ASSERT(xfs_Gqm != NULL);
1837 xfs_qm_destroy_quotainfo(mp);
David Chinner31d55772008-04-10 12:20:38 +10001838 if (xfs_mount_reset_sbqflags(mp)) {
1839 cmn_err(CE_WARN, "XFS quotacheck %s: "
1840 "Failed to reset quota flags.", mp->m_fsname);
1841 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842 } else {
1843 cmn_err(CE_NOTE, "XFS quotacheck %s: Done.", mp->m_fsname);
1844 }
1845 return (error);
1846}
1847
1848/*
1849 * This is called after the superblock has been read in and we're ready to
1850 * iget the quota inodes.
1851 */
1852STATIC int
1853xfs_qm_init_quotainos(
1854 xfs_mount_t *mp)
1855{
1856 xfs_inode_t *uip, *gip;
1857 int error;
1858 __int64_t sbflags;
1859 uint flags;
1860
1861 ASSERT(mp->m_quotainfo);
1862 uip = gip = NULL;
1863 sbflags = 0;
1864 flags = 0;
1865
1866 /*
1867 * Get the uquota and gquota inodes
1868 */
Eric Sandeen62118702008-03-06 13:44:28 +11001869 if (xfs_sb_version_hasquota(&mp->m_sb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870 if (XFS_IS_UQUOTA_ON(mp) &&
1871 mp->m_sb.sb_uquotino != NULLFSINO) {
1872 ASSERT(mp->m_sb.sb_uquotino > 0);
1873 if ((error = xfs_iget(mp, NULL, mp->m_sb.sb_uquotino,
1874 0, 0, &uip, 0)))
1875 return XFS_ERROR(error);
1876 }
Nathan Scottc8ad20f2005-06-21 15:38:48 +10001877 if (XFS_IS_OQUOTA_ON(mp) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878 mp->m_sb.sb_gquotino != NULLFSINO) {
1879 ASSERT(mp->m_sb.sb_gquotino > 0);
1880 if ((error = xfs_iget(mp, NULL, mp->m_sb.sb_gquotino,
1881 0, 0, &gip, 0))) {
1882 if (uip)
Christoph Hellwig43355092008-03-27 18:01:08 +11001883 IRELE(uip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884 return XFS_ERROR(error);
1885 }
1886 }
1887 } else {
1888 flags |= XFS_QMOPT_SBVERSION;
1889 sbflags |= (XFS_SB_VERSIONNUM | XFS_SB_UQUOTINO |
1890 XFS_SB_GQUOTINO | XFS_SB_QFLAGS);
1891 }
1892
1893 /*
1894 * Create the two inodes, if they don't exist already. The changes
1895 * made above will get added to a transaction and logged in one of
1896 * the qino_alloc calls below. If the device is readonly,
1897 * temporarily switch to read-write to do this.
1898 */
1899 if (XFS_IS_UQUOTA_ON(mp) && uip == NULL) {
1900 if ((error = xfs_qm_qino_alloc(mp, &uip,
1901 sbflags | XFS_SB_UQUOTINO,
1902 flags | XFS_QMOPT_UQUOTA)))
1903 return XFS_ERROR(error);
1904
1905 flags &= ~XFS_QMOPT_SBVERSION;
1906 }
Nathan Scottc8ad20f2005-06-21 15:38:48 +10001907 if (XFS_IS_OQUOTA_ON(mp) && gip == NULL) {
1908 flags |= (XFS_IS_GQUOTA_ON(mp) ?
1909 XFS_QMOPT_GQUOTA : XFS_QMOPT_PQUOTA);
1910 error = xfs_qm_qino_alloc(mp, &gip,
1911 sbflags | XFS_SB_GQUOTINO, flags);
1912 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913 if (uip)
Christoph Hellwig43355092008-03-27 18:01:08 +11001914 IRELE(uip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915
1916 return XFS_ERROR(error);
1917 }
1918 }
1919
1920 XFS_QI_UQIP(mp) = uip;
1921 XFS_QI_GQIP(mp) = gip;
1922
Jesper Juhl014c2542006-01-15 02:37:08 +01001923 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924}
1925
1926
1927/*
1928 * Traverse the freelist of dquots and attempt to reclaim a maximum of
1929 * 'howmany' dquots. This operation races with dqlookup(), and attempts to
1930 * favor the lookup function ...
1931 * XXXsup merge this with qm_reclaim_one().
1932 */
1933STATIC int
1934xfs_qm_shake_freelist(
1935 int howmany)
1936{
1937 int nreclaimed;
1938 xfs_dqhash_t *hash;
1939 xfs_dquot_t *dqp, *nextdqp;
1940 int restarts;
1941 int nflushes;
1942
1943 if (howmany <= 0)
Jesper Juhl014c2542006-01-15 02:37:08 +01001944 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945
1946 nreclaimed = 0;
1947 restarts = 0;
1948 nflushes = 0;
1949
1950#ifdef QUOTADEBUG
1951 cmn_err(CE_DEBUG, "Shake free 0x%x", howmany);
1952#endif
1953 /* lock order is : hashchainlock, freelistlock, mplistlock */
1954 tryagain:
1955 xfs_qm_freelist_lock(xfs_Gqm);
1956
1957 for (dqp = xfs_Gqm->qm_dqfreelist.qh_next;
1958 ((dqp != (xfs_dquot_t *) &xfs_Gqm->qm_dqfreelist) &&
1959 nreclaimed < howmany); ) {
1960 xfs_dqlock(dqp);
1961
1962 /*
1963 * We are racing with dqlookup here. Naturally we don't
1964 * want to reclaim a dquot that lookup wants.
1965 */
1966 if (dqp->dq_flags & XFS_DQ_WANT) {
1967 xfs_dqunlock(dqp);
1968 xfs_qm_freelist_unlock(xfs_Gqm);
1969 if (++restarts >= XFS_QM_RECLAIM_MAX_RESTARTS)
Jesper Juhl014c2542006-01-15 02:37:08 +01001970 return nreclaimed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971 XQM_STATS_INC(xqmstats.xs_qm_dqwants);
1972 goto tryagain;
1973 }
1974
1975 /*
1976 * If the dquot is inactive, we are assured that it is
1977 * not on the mplist or the hashlist, and that makes our
1978 * life easier.
1979 */
1980 if (dqp->dq_flags & XFS_DQ_INACTIVE) {
1981 ASSERT(dqp->q_mount == NULL);
1982 ASSERT(! XFS_DQ_IS_DIRTY(dqp));
1983 ASSERT(dqp->HL_PREVP == NULL);
1984 ASSERT(dqp->MPL_PREVP == NULL);
1985 XQM_STATS_INC(xqmstats.xs_qm_dqinact_reclaims);
1986 nextdqp = dqp->dq_flnext;
1987 goto off_freelist;
1988 }
1989
1990 ASSERT(dqp->MPL_PREVP);
1991 /*
1992 * Try to grab the flush lock. If this dquot is in the process of
1993 * getting flushed to disk, we don't want to reclaim it.
1994 */
David Chinnere1f49cf2008-08-13 16:41:43 +10001995 if (!xfs_dqflock_nowait(dqp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996 xfs_dqunlock(dqp);
1997 dqp = dqp->dq_flnext;
1998 continue;
1999 }
2000
2001 /*
2002 * We have the flush lock so we know that this is not in the
2003 * process of being flushed. So, if this is dirty, flush it
2004 * DELWRI so that we don't get a freelist infested with
2005 * dirty dquots.
2006 */
2007 if (XFS_DQ_IS_DIRTY(dqp)) {
David Chinner3c568362008-04-10 12:20:24 +10002008 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009 xfs_dqtrace_entry(dqp, "DQSHAKE: DQDIRTY");
2010 /*
2011 * We flush it delayed write, so don't bother
2012 * releasing the mplock.
2013 */
David Chinner3c568362008-04-10 12:20:24 +10002014 error = xfs_qm_dqflush(dqp, XFS_QMOPT_DELWRI);
2015 if (error) {
2016 xfs_fs_cmn_err(CE_WARN, dqp->q_mount,
2017 "xfs_qm_dqflush_all: dquot %p flush failed", dqp);
2018 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019 xfs_dqunlock(dqp); /* dqflush unlocks dqflock */
2020 dqp = dqp->dq_flnext;
2021 continue;
2022 }
2023 /*
2024 * We're trying to get the hashlock out of order. This races
2025 * with dqlookup; so, we giveup and goto the next dquot if
2026 * we couldn't get the hashlock. This way, we won't starve
2027 * a dqlookup process that holds the hashlock that is
2028 * waiting for the freelist lock.
2029 */
2030 if (! xfs_qm_dqhashlock_nowait(dqp)) {
2031 xfs_dqfunlock(dqp);
2032 xfs_dqunlock(dqp);
2033 dqp = dqp->dq_flnext;
2034 continue;
2035 }
2036 /*
2037 * This races with dquot allocation code as well as dqflush_all
2038 * and reclaim code. So, if we failed to grab the mplist lock,
2039 * giveup everything and start over.
2040 */
2041 hash = dqp->q_hash;
2042 ASSERT(hash);
2043 if (! xfs_qm_mplist_nowait(dqp->q_mount)) {
2044 /* XXX put a sentinel so that we can come back here */
2045 xfs_dqfunlock(dqp);
2046 xfs_dqunlock(dqp);
2047 XFS_DQ_HASH_UNLOCK(hash);
2048 xfs_qm_freelist_unlock(xfs_Gqm);
2049 if (++restarts >= XFS_QM_RECLAIM_MAX_RESTARTS)
Jesper Juhl014c2542006-01-15 02:37:08 +01002050 return nreclaimed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051 goto tryagain;
2052 }
2053 xfs_dqtrace_entry(dqp, "DQSHAKE: UNLINKING");
2054#ifdef QUOTADEBUG
2055 cmn_err(CE_DEBUG, "Shake 0x%p, ID 0x%x\n",
Christoph Hellwig1149d962005-11-02 15:01:12 +11002056 dqp, be32_to_cpu(dqp->q_core.d_id));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057#endif
2058 ASSERT(dqp->q_nrefs == 0);
2059 nextdqp = dqp->dq_flnext;
2060 XQM_MPLIST_REMOVE(&(XFS_QI_MPL_LIST(dqp->q_mount)), dqp);
2061 XQM_HASHLIST_REMOVE(hash, dqp);
2062 xfs_dqfunlock(dqp);
2063 xfs_qm_mplist_unlock(dqp->q_mount);
2064 XFS_DQ_HASH_UNLOCK(hash);
2065
2066 off_freelist:
2067 XQM_FREELIST_REMOVE(dqp);
2068 xfs_dqunlock(dqp);
2069 nreclaimed++;
2070 XQM_STATS_INC(xqmstats.xs_qm_dqshake_reclaims);
2071 xfs_qm_dqdestroy(dqp);
2072 dqp = nextdqp;
2073 }
2074 xfs_qm_freelist_unlock(xfs_Gqm);
Jesper Juhl014c2542006-01-15 02:37:08 +01002075 return nreclaimed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076}
2077
2078
2079/*
2080 * The kmem_shake interface is invoked when memory is running low.
2081 */
2082/* ARGSUSED */
2083STATIC int
Al Viro51bfb752005-12-15 09:17:24 +00002084xfs_qm_shake(int nr_to_scan, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085{
2086 int ndqused, nfree, n;
2087
2088 if (!kmem_shake_allow(gfp_mask))
Jesper Juhl014c2542006-01-15 02:37:08 +01002089 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090 if (!xfs_Gqm)
Jesper Juhl014c2542006-01-15 02:37:08 +01002091 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092
2093 nfree = xfs_Gqm->qm_dqfreelist.qh_nelems; /* free dquots */
2094 /* incore dquots in all f/s's */
2095 ndqused = atomic_read(&xfs_Gqm->qm_totaldquots) - nfree;
2096
2097 ASSERT(ndqused >= 0);
2098
2099 if (nfree <= ndqused && nfree < ndquot)
Jesper Juhl014c2542006-01-15 02:37:08 +01002100 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101
2102 ndqused *= xfs_Gqm->qm_dqfree_ratio; /* target # of free dquots */
2103 n = nfree - ndqused - ndquot; /* # over target */
2104
2105 return xfs_qm_shake_freelist(MAX(nfree, n));
2106}
2107
2108
2109/*
2110 * Just pop the least recently used dquot off the freelist and
2111 * recycle it. The returned dquot is locked.
2112 */
2113STATIC xfs_dquot_t *
2114xfs_qm_dqreclaim_one(void)
2115{
2116 xfs_dquot_t *dqpout;
2117 xfs_dquot_t *dqp;
2118 int restarts;
2119 int nflushes;
2120
2121 restarts = 0;
2122 dqpout = NULL;
2123 nflushes = 0;
2124
2125 /* lockorder: hashchainlock, freelistlock, mplistlock, dqlock, dqflock */
2126 startagain:
2127 xfs_qm_freelist_lock(xfs_Gqm);
2128
2129 FOREACH_DQUOT_IN_FREELIST(dqp, &(xfs_Gqm->qm_dqfreelist)) {
2130 xfs_dqlock(dqp);
2131
2132 /*
2133 * We are racing with dqlookup here. Naturally we don't
2134 * want to reclaim a dquot that lookup wants. We release the
2135 * freelist lock and start over, so that lookup will grab
2136 * both the dquot and the freelistlock.
2137 */
2138 if (dqp->dq_flags & XFS_DQ_WANT) {
2139 ASSERT(! (dqp->dq_flags & XFS_DQ_INACTIVE));
2140 xfs_dqtrace_entry(dqp, "DQRECLAIM: DQWANT");
2141 xfs_dqunlock(dqp);
2142 xfs_qm_freelist_unlock(xfs_Gqm);
2143 if (++restarts >= XFS_QM_RECLAIM_MAX_RESTARTS)
Jesper Juhl014c2542006-01-15 02:37:08 +01002144 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145 XQM_STATS_INC(xqmstats.xs_qm_dqwants);
2146 goto startagain;
2147 }
2148
2149 /*
2150 * If the dquot is inactive, we are assured that it is
2151 * not on the mplist or the hashlist, and that makes our
2152 * life easier.
2153 */
2154 if (dqp->dq_flags & XFS_DQ_INACTIVE) {
2155 ASSERT(dqp->q_mount == NULL);
2156 ASSERT(! XFS_DQ_IS_DIRTY(dqp));
2157 ASSERT(dqp->HL_PREVP == NULL);
2158 ASSERT(dqp->MPL_PREVP == NULL);
2159 XQM_FREELIST_REMOVE(dqp);
2160 xfs_dqunlock(dqp);
2161 dqpout = dqp;
2162 XQM_STATS_INC(xqmstats.xs_qm_dqinact_reclaims);
2163 break;
2164 }
2165
2166 ASSERT(dqp->q_hash);
2167 ASSERT(dqp->MPL_PREVP);
2168
2169 /*
2170 * Try to grab the flush lock. If this dquot is in the process of
2171 * getting flushed to disk, we don't want to reclaim it.
2172 */
David Chinnere1f49cf2008-08-13 16:41:43 +10002173 if (!xfs_dqflock_nowait(dqp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174 xfs_dqunlock(dqp);
2175 continue;
2176 }
2177
2178 /*
2179 * We have the flush lock so we know that this is not in the
2180 * process of being flushed. So, if this is dirty, flush it
2181 * DELWRI so that we don't get a freelist infested with
2182 * dirty dquots.
2183 */
2184 if (XFS_DQ_IS_DIRTY(dqp)) {
David Chinner3c568362008-04-10 12:20:24 +10002185 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186 xfs_dqtrace_entry(dqp, "DQRECLAIM: DQDIRTY");
2187 /*
2188 * We flush it delayed write, so don't bother
2189 * releasing the freelist lock.
2190 */
David Chinner3c568362008-04-10 12:20:24 +10002191 error = xfs_qm_dqflush(dqp, XFS_QMOPT_DELWRI);
2192 if (error) {
2193 xfs_fs_cmn_err(CE_WARN, dqp->q_mount,
2194 "xfs_qm_dqreclaim: dquot %p flush failed", dqp);
2195 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196 xfs_dqunlock(dqp); /* dqflush unlocks dqflock */
2197 continue;
2198 }
2199
2200 if (! xfs_qm_mplist_nowait(dqp->q_mount)) {
2201 xfs_dqfunlock(dqp);
2202 xfs_dqunlock(dqp);
2203 continue;
2204 }
2205
2206 if (! xfs_qm_dqhashlock_nowait(dqp))
2207 goto mplistunlock;
2208
2209 ASSERT(dqp->q_nrefs == 0);
2210 xfs_dqtrace_entry(dqp, "DQRECLAIM: UNLINKING");
2211 XQM_MPLIST_REMOVE(&(XFS_QI_MPL_LIST(dqp->q_mount)), dqp);
2212 XQM_HASHLIST_REMOVE(dqp->q_hash, dqp);
2213 XQM_FREELIST_REMOVE(dqp);
2214 dqpout = dqp;
2215 XFS_DQ_HASH_UNLOCK(dqp->q_hash);
2216 mplistunlock:
2217 xfs_qm_mplist_unlock(dqp->q_mount);
2218 xfs_dqfunlock(dqp);
2219 xfs_dqunlock(dqp);
2220 if (dqpout)
2221 break;
2222 }
2223
2224 xfs_qm_freelist_unlock(xfs_Gqm);
Jesper Juhl014c2542006-01-15 02:37:08 +01002225 return dqpout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226}
2227
2228
2229/*------------------------------------------------------------------*/
2230
2231/*
2232 * Return a new incore dquot. Depending on the number of
2233 * dquots in the system, we either allocate a new one on the kernel heap,
2234 * or reclaim a free one.
2235 * Return value is B_TRUE if we allocated a new dquot, B_FALSE if we managed
2236 * to reclaim an existing one from the freelist.
2237 */
2238boolean_t
2239xfs_qm_dqalloc_incore(
2240 xfs_dquot_t **O_dqpp)
2241{
2242 xfs_dquot_t *dqp;
2243
2244 /*
2245 * Check against high water mark to see if we want to pop
2246 * a nincompoop dquot off the freelist.
2247 */
2248 if (atomic_read(&xfs_Gqm->qm_totaldquots) >= ndquot) {
2249 /*
2250 * Try to recycle a dquot from the freelist.
2251 */
2252 if ((dqp = xfs_qm_dqreclaim_one())) {
2253 XQM_STATS_INC(xqmstats.xs_qm_dqreclaims);
2254 /*
2255 * Just zero the core here. The rest will get
2256 * reinitialized by caller. XXX we shouldn't even
2257 * do this zero ...
2258 */
2259 memset(&dqp->q_core, 0, sizeof(dqp->q_core));
2260 *O_dqpp = dqp;
Jesper Juhl014c2542006-01-15 02:37:08 +01002261 return B_FALSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262 }
2263 XQM_STATS_INC(xqmstats.xs_qm_dqreclaim_misses);
2264 }
2265
2266 /*
2267 * Allocate a brand new dquot on the kernel heap and return it
2268 * to the caller to initialize.
2269 */
2270 ASSERT(xfs_Gqm->qm_dqzone != NULL);
2271 *O_dqpp = kmem_zone_zalloc(xfs_Gqm->qm_dqzone, KM_SLEEP);
2272 atomic_inc(&xfs_Gqm->qm_totaldquots);
2273
Jesper Juhl014c2542006-01-15 02:37:08 +01002274 return B_TRUE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275}
2276
2277
2278/*
2279 * Start a transaction and write the incore superblock changes to
2280 * disk. flags parameter indicates which fields have changed.
2281 */
2282int
2283xfs_qm_write_sb_changes(
2284 xfs_mount_t *mp,
2285 __int64_t flags)
2286{
2287 xfs_trans_t *tp;
2288 int error;
2289
2290#ifdef QUOTADEBUG
2291 cmn_err(CE_NOTE, "Writing superblock quota changes :%s", mp->m_fsname);
2292#endif
2293 tp = xfs_trans_alloc(mp, XFS_TRANS_QM_SBCHANGE);
2294 if ((error = xfs_trans_reserve(tp, 0,
2295 mp->m_sb.sb_sectsize + 128, 0,
2296 0,
2297 XFS_DEFAULT_LOG_COUNT))) {
2298 xfs_trans_cancel(tp, 0);
Jesper Juhl014c2542006-01-15 02:37:08 +01002299 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300 }
2301
2302 xfs_mod_sb(tp, flags);
David Chinnere5720ee2008-04-10 12:21:18 +10002303 error = xfs_trans_commit(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304
David Chinnere5720ee2008-04-10 12:21:18 +10002305 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306}
2307
2308
2309/* --------------- utility functions for vnodeops ---------------- */
2310
2311
2312/*
2313 * Given an inode, a uid and gid (from cred_t) make sure that we have
2314 * allocated relevant dquot(s) on disk, and that we won't exceed inode
2315 * quotas by creating this file.
2316 * This also attaches dquot(s) to the given inode after locking it,
2317 * and returns the dquots corresponding to the uid and/or gid.
2318 *
2319 * in : inode (unlocked)
2320 * out : udquot, gdquot with references taken and unlocked
2321 */
2322int
2323xfs_qm_vop_dqalloc(
2324 xfs_mount_t *mp,
2325 xfs_inode_t *ip,
2326 uid_t uid,
2327 gid_t gid,
Nathan Scottc8ad20f2005-06-21 15:38:48 +10002328 prid_t prid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329 uint flags,
2330 xfs_dquot_t **O_udqpp,
2331 xfs_dquot_t **O_gdqpp)
2332{
2333 int error;
2334 xfs_dquot_t *uq, *gq;
2335 uint lockflags;
2336
2337 if (!XFS_IS_QUOTA_ON(mp))
2338 return 0;
2339
2340 lockflags = XFS_ILOCK_EXCL;
2341 xfs_ilock(ip, lockflags);
2342
Christoph Hellwigbd186aa2007-08-30 17:21:12 +10002343 if ((flags & XFS_QMOPT_INHERIT) && XFS_INHERIT_GID(ip))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344 gid = ip->i_d.di_gid;
2345
2346 /*
2347 * Attach the dquot(s) to this inode, doing a dquot allocation
2348 * if necessary. The dquot(s) will not be locked.
2349 */
2350 if (XFS_NOT_DQATTACHED(mp, ip)) {
2351 if ((error = xfs_qm_dqattach(ip, XFS_QMOPT_DQALLOC |
2352 XFS_QMOPT_ILOCKED))) {
2353 xfs_iunlock(ip, lockflags);
Jesper Juhl014c2542006-01-15 02:37:08 +01002354 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002355 }
2356 }
2357
2358 uq = gq = NULL;
Nathan Scottc8ad20f2005-06-21 15:38:48 +10002359 if ((flags & XFS_QMOPT_UQUOTA) && XFS_IS_UQUOTA_ON(mp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360 if (ip->i_d.di_uid != uid) {
2361 /*
2362 * What we need is the dquot that has this uid, and
2363 * if we send the inode to dqget, the uid of the inode
2364 * takes priority over what's sent in the uid argument.
2365 * We must unlock inode here before calling dqget if
2366 * we're not sending the inode, because otherwise
2367 * we'll deadlock by doing trans_reserve while
2368 * holding ilock.
2369 */
2370 xfs_iunlock(ip, lockflags);
2371 if ((error = xfs_qm_dqget(mp, NULL, (xfs_dqid_t) uid,
2372 XFS_DQ_USER,
2373 XFS_QMOPT_DQALLOC |
2374 XFS_QMOPT_DOWARN,
2375 &uq))) {
2376 ASSERT(error != ENOENT);
Jesper Juhl014c2542006-01-15 02:37:08 +01002377 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002378 }
2379 /*
2380 * Get the ilock in the right order.
2381 */
2382 xfs_dqunlock(uq);
2383 lockflags = XFS_ILOCK_SHARED;
2384 xfs_ilock(ip, lockflags);
2385 } else {
2386 /*
2387 * Take an extra reference, because we'll return
2388 * this to caller
2389 */
2390 ASSERT(ip->i_udquot);
2391 uq = ip->i_udquot;
2392 xfs_dqlock(uq);
2393 XFS_DQHOLD(uq);
2394 xfs_dqunlock(uq);
2395 }
2396 }
Nathan Scottc8ad20f2005-06-21 15:38:48 +10002397 if ((flags & XFS_QMOPT_GQUOTA) && XFS_IS_GQUOTA_ON(mp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398 if (ip->i_d.di_gid != gid) {
2399 xfs_iunlock(ip, lockflags);
2400 if ((error = xfs_qm_dqget(mp, NULL, (xfs_dqid_t)gid,
2401 XFS_DQ_GROUP,
2402 XFS_QMOPT_DQALLOC |
2403 XFS_QMOPT_DOWARN,
2404 &gq))) {
2405 if (uq)
2406 xfs_qm_dqrele(uq);
2407 ASSERT(error != ENOENT);
Jesper Juhl014c2542006-01-15 02:37:08 +01002408 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002409 }
2410 xfs_dqunlock(gq);
2411 lockflags = XFS_ILOCK_SHARED;
2412 xfs_ilock(ip, lockflags);
2413 } else {
2414 ASSERT(ip->i_gdquot);
2415 gq = ip->i_gdquot;
2416 xfs_dqlock(gq);
2417 XFS_DQHOLD(gq);
2418 xfs_dqunlock(gq);
2419 }
Nathan Scottc8ad20f2005-06-21 15:38:48 +10002420 } else if ((flags & XFS_QMOPT_PQUOTA) && XFS_IS_PQUOTA_ON(mp)) {
2421 if (ip->i_d.di_projid != prid) {
2422 xfs_iunlock(ip, lockflags);
2423 if ((error = xfs_qm_dqget(mp, NULL, (xfs_dqid_t)prid,
2424 XFS_DQ_PROJ,
2425 XFS_QMOPT_DQALLOC |
2426 XFS_QMOPT_DOWARN,
2427 &gq))) {
2428 if (uq)
2429 xfs_qm_dqrele(uq);
2430 ASSERT(error != ENOENT);
2431 return (error);
2432 }
2433 xfs_dqunlock(gq);
2434 lockflags = XFS_ILOCK_SHARED;
2435 xfs_ilock(ip, lockflags);
2436 } else {
2437 ASSERT(ip->i_gdquot);
2438 gq = ip->i_gdquot;
2439 xfs_dqlock(gq);
2440 XFS_DQHOLD(gq);
2441 xfs_dqunlock(gq);
2442 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002443 }
2444 if (uq)
2445 xfs_dqtrace_entry_ino(uq, "DQALLOC", ip);
2446
2447 xfs_iunlock(ip, lockflags);
2448 if (O_udqpp)
2449 *O_udqpp = uq;
2450 else if (uq)
2451 xfs_qm_dqrele(uq);
2452 if (O_gdqpp)
2453 *O_gdqpp = gq;
2454 else if (gq)
2455 xfs_qm_dqrele(gq);
Jesper Juhl014c2542006-01-15 02:37:08 +01002456 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002457}
2458
2459/*
2460 * Actually transfer ownership, and do dquot modifications.
2461 * These were already reserved.
2462 */
2463xfs_dquot_t *
2464xfs_qm_vop_chown(
2465 xfs_trans_t *tp,
2466 xfs_inode_t *ip,
2467 xfs_dquot_t **IO_olddq,
2468 xfs_dquot_t *newdq)
2469{
2470 xfs_dquot_t *prevdq;
Nathan Scott06d10dd2005-06-21 15:48:47 +10002471 uint bfield = XFS_IS_REALTIME_INODE(ip) ?
2472 XFS_TRANS_DQ_RTBCOUNT : XFS_TRANS_DQ_BCOUNT;
2473
Christoph Hellwig579aa9c2008-04-22 17:34:00 +10002474 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475 ASSERT(XFS_IS_QUOTA_RUNNING(ip->i_mount));
2476
2477 /* old dquot */
2478 prevdq = *IO_olddq;
2479 ASSERT(prevdq);
2480 ASSERT(prevdq != newdq);
2481
Nathan Scott06d10dd2005-06-21 15:48:47 +10002482 xfs_trans_mod_dquot(tp, prevdq, bfield, -(ip->i_d.di_nblocks));
2483 xfs_trans_mod_dquot(tp, prevdq, XFS_TRANS_DQ_ICOUNT, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002484
2485 /* the sparkling new dquot */
Nathan Scott06d10dd2005-06-21 15:48:47 +10002486 xfs_trans_mod_dquot(tp, newdq, bfield, ip->i_d.di_nblocks);
2487 xfs_trans_mod_dquot(tp, newdq, XFS_TRANS_DQ_ICOUNT, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002488
2489 /*
2490 * Take an extra reference, because the inode
2491 * is going to keep this dquot pointer even
2492 * after the trans_commit.
2493 */
2494 xfs_dqlock(newdq);
2495 XFS_DQHOLD(newdq);
2496 xfs_dqunlock(newdq);
2497 *IO_olddq = newdq;
2498
Jesper Juhl014c2542006-01-15 02:37:08 +01002499 return prevdq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002500}
2501
2502/*
Nathan Scottc8ad20f2005-06-21 15:38:48 +10002503 * Quota reservations for setattr(AT_UID|AT_GID|AT_PROJID).
Linus Torvalds1da177e2005-04-16 15:20:36 -07002504 */
2505int
2506xfs_qm_vop_chown_reserve(
2507 xfs_trans_t *tp,
2508 xfs_inode_t *ip,
2509 xfs_dquot_t *udqp,
2510 xfs_dquot_t *gdqp,
2511 uint flags)
2512{
2513 int error;
2514 xfs_mount_t *mp;
Nathan Scott9a2a7de2006-03-31 13:04:49 +10002515 uint delblks, blkflags, prjflags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002516 xfs_dquot_t *unresudq, *unresgdq, *delblksudq, *delblksgdq;
2517
Christoph Hellwig579aa9c2008-04-22 17:34:00 +10002518 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_ILOCK_SHARED));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002519 mp = ip->i_mount;
2520 ASSERT(XFS_IS_QUOTA_RUNNING(mp));
2521
2522 delblks = ip->i_delayed_blks;
2523 delblksudq = delblksgdq = unresudq = unresgdq = NULL;
Nathan Scott06d10dd2005-06-21 15:48:47 +10002524 blkflags = XFS_IS_REALTIME_INODE(ip) ?
2525 XFS_QMOPT_RES_RTBLKS : XFS_QMOPT_RES_REGBLKS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002526
2527 if (XFS_IS_UQUOTA_ON(mp) && udqp &&
Christoph Hellwig1149d962005-11-02 15:01:12 +11002528 ip->i_d.di_uid != (uid_t)be32_to_cpu(udqp->q_core.d_id)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002529 delblksudq = udqp;
2530 /*
2531 * If there are delayed allocation blocks, then we have to
2532 * unreserve those from the old dquot, and add them to the
2533 * new dquot.
2534 */
2535 if (delblks) {
2536 ASSERT(ip->i_udquot);
2537 unresudq = ip->i_udquot;
2538 }
2539 }
Nathan Scottc8ad20f2005-06-21 15:38:48 +10002540 if (XFS_IS_OQUOTA_ON(ip->i_mount) && gdqp) {
Nathan Scott9a2a7de2006-03-31 13:04:49 +10002541 if (XFS_IS_PQUOTA_ON(ip->i_mount) &&
2542 ip->i_d.di_projid != be32_to_cpu(gdqp->q_core.d_id))
2543 prjflags = XFS_QMOPT_ENOSPC;
2544
2545 if (prjflags ||
2546 (XFS_IS_GQUOTA_ON(ip->i_mount) &&
2547 ip->i_d.di_gid != be32_to_cpu(gdqp->q_core.d_id))) {
Nathan Scottc8ad20f2005-06-21 15:38:48 +10002548 delblksgdq = gdqp;
2549 if (delblks) {
2550 ASSERT(ip->i_gdquot);
2551 unresgdq = ip->i_gdquot;
2552 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002553 }
2554 }
2555
2556 if ((error = xfs_trans_reserve_quota_bydquots(tp, ip->i_mount,
2557 delblksudq, delblksgdq, ip->i_d.di_nblocks, 1,
Nathan Scott9a2a7de2006-03-31 13:04:49 +10002558 flags | blkflags | prjflags)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002559 return (error);
2560
2561 /*
2562 * Do the delayed blks reservations/unreservations now. Since, these
2563 * are done without the help of a transaction, if a reservation fails
2564 * its previous reservations won't be automatically undone by trans
2565 * code. So, we have to do it manually here.
2566 */
2567 if (delblks) {
2568 /*
2569 * Do the reservations first. Unreservation can't fail.
2570 */
2571 ASSERT(delblksudq || delblksgdq);
2572 ASSERT(unresudq || unresgdq);
2573 if ((error = xfs_trans_reserve_quota_bydquots(NULL, ip->i_mount,
2574 delblksudq, delblksgdq, (xfs_qcnt_t)delblks, 0,
Nathan Scott9a2a7de2006-03-31 13:04:49 +10002575 flags | blkflags | prjflags)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002576 return (error);
2577 xfs_trans_reserve_quota_bydquots(NULL, ip->i_mount,
2578 unresudq, unresgdq, -((xfs_qcnt_t)delblks), 0,
Nathan Scott06d10dd2005-06-21 15:48:47 +10002579 blkflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002580 }
2581
2582 return (0);
2583}
2584
2585int
2586xfs_qm_vop_rename_dqattach(
2587 xfs_inode_t **i_tab)
2588{
2589 xfs_inode_t *ip;
2590 int i;
2591 int error;
2592
2593 ip = i_tab[0];
2594
2595 if (! XFS_IS_QUOTA_ON(ip->i_mount))
Jesper Juhl014c2542006-01-15 02:37:08 +01002596 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002597
2598 if (XFS_NOT_DQATTACHED(ip->i_mount, ip)) {
2599 error = xfs_qm_dqattach(ip, 0);
2600 if (error)
Jesper Juhl014c2542006-01-15 02:37:08 +01002601 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002602 }
2603 for (i = 1; (i < 4 && i_tab[i]); i++) {
2604 /*
2605 * Watch out for duplicate entries in the table.
2606 */
2607 if ((ip = i_tab[i]) != i_tab[i-1]) {
2608 if (XFS_NOT_DQATTACHED(ip->i_mount, ip)) {
2609 error = xfs_qm_dqattach(ip, 0);
2610 if (error)
Jesper Juhl014c2542006-01-15 02:37:08 +01002611 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002612 }
2613 }
2614 }
Jesper Juhl014c2542006-01-15 02:37:08 +01002615 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002616}
2617
2618void
2619xfs_qm_vop_dqattach_and_dqmod_newinode(
2620 xfs_trans_t *tp,
2621 xfs_inode_t *ip,
2622 xfs_dquot_t *udqp,
2623 xfs_dquot_t *gdqp)
2624{
2625 if (!XFS_IS_QUOTA_ON(tp->t_mountp))
2626 return;
2627
Christoph Hellwig579aa9c2008-04-22 17:34:00 +10002628 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002629 ASSERT(XFS_IS_QUOTA_RUNNING(tp->t_mountp));
2630
2631 if (udqp) {
2632 xfs_dqlock(udqp);
2633 XFS_DQHOLD(udqp);
2634 xfs_dqunlock(udqp);
2635 ASSERT(ip->i_udquot == NULL);
2636 ip->i_udquot = udqp;
Nathan Scottee2a4f72006-01-11 15:33:36 +11002637 ASSERT(XFS_IS_UQUOTA_ON(tp->t_mountp));
Christoph Hellwig1149d962005-11-02 15:01:12 +11002638 ASSERT(ip->i_d.di_uid == be32_to_cpu(udqp->q_core.d_id));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002639 xfs_trans_mod_dquot(tp, udqp, XFS_TRANS_DQ_ICOUNT, 1);
2640 }
2641 if (gdqp) {
2642 xfs_dqlock(gdqp);
2643 XFS_DQHOLD(gdqp);
2644 xfs_dqunlock(gdqp);
2645 ASSERT(ip->i_gdquot == NULL);
2646 ip->i_gdquot = gdqp;
Nathan Scottee2a4f72006-01-11 15:33:36 +11002647 ASSERT(XFS_IS_OQUOTA_ON(tp->t_mountp));
2648 ASSERT((XFS_IS_GQUOTA_ON(tp->t_mountp) ?
2649 ip->i_d.di_gid : ip->i_d.di_projid) ==
2650 be32_to_cpu(gdqp->q_core.d_id));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002651 xfs_trans_mod_dquot(tp, gdqp, XFS_TRANS_DQ_ICOUNT, 1);
2652 }
2653}
2654
2655/* ------------- list stuff -----------------*/
Christoph Hellwigba0f32d2005-06-21 15:36:52 +10002656STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -07002657xfs_qm_freelist_init(xfs_frlist_t *ql)
2658{
2659 ql->qh_next = ql->qh_prev = (xfs_dquot_t *) ql;
Jes Sorensen794ee1b2006-01-09 15:59:21 -08002660 mutex_init(&ql->qh_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002661 ql->qh_version = 0;
2662 ql->qh_nelems = 0;
2663}
2664
Christoph Hellwigba0f32d2005-06-21 15:36:52 +10002665STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -07002666xfs_qm_freelist_destroy(xfs_frlist_t *ql)
2667{
2668 xfs_dquot_t *dqp, *nextdqp;
2669
Jes Sorensen794ee1b2006-01-09 15:59:21 -08002670 mutex_lock(&ql->qh_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002671 for (dqp = ql->qh_next;
2672 dqp != (xfs_dquot_t *)ql; ) {
2673 xfs_dqlock(dqp);
2674 nextdqp = dqp->dq_flnext;
2675#ifdef QUOTADEBUG
2676 cmn_err(CE_DEBUG, "FREELIST destroy 0x%p", dqp);
2677#endif
2678 XQM_FREELIST_REMOVE(dqp);
2679 xfs_dqunlock(dqp);
2680 xfs_qm_dqdestroy(dqp);
2681 dqp = nextdqp;
2682 }
Nathan Scott20722a92006-03-14 13:19:08 +11002683 mutex_unlock(&ql->qh_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002684 mutex_destroy(&ql->qh_lock);
2685
2686 ASSERT(ql->qh_nelems == 0);
2687}
2688
Christoph Hellwigba0f32d2005-06-21 15:36:52 +10002689STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -07002690xfs_qm_freelist_insert(xfs_frlist_t *ql, xfs_dquot_t *dq)
2691{
2692 dq->dq_flnext = ql->qh_next;
2693 dq->dq_flprev = (xfs_dquot_t *)ql;
2694 ql->qh_next = dq;
2695 dq->dq_flnext->dq_flprev = dq;
2696 xfs_Gqm->qm_dqfreelist.qh_nelems++;
2697 xfs_Gqm->qm_dqfreelist.qh_version++;
2698}
2699
2700void
2701xfs_qm_freelist_unlink(xfs_dquot_t *dq)
2702{
2703 xfs_dquot_t *next = dq->dq_flnext;
2704 xfs_dquot_t *prev = dq->dq_flprev;
2705
2706 next->dq_flprev = prev;
2707 prev->dq_flnext = next;
2708 dq->dq_flnext = dq->dq_flprev = dq;
2709 xfs_Gqm->qm_dqfreelist.qh_nelems--;
2710 xfs_Gqm->qm_dqfreelist.qh_version++;
2711}
2712
2713void
2714xfs_qm_freelist_append(xfs_frlist_t *ql, xfs_dquot_t *dq)
2715{
2716 xfs_qm_freelist_insert((xfs_frlist_t *)ql->qh_prev, dq);
2717}
2718
Christoph Hellwigba0f32d2005-06-21 15:36:52 +10002719STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -07002720xfs_qm_dqhashlock_nowait(
2721 xfs_dquot_t *dqp)
2722{
2723 int locked;
2724
2725 locked = mutex_trylock(&((dqp)->q_hash->qh_lock));
Jesper Juhl014c2542006-01-15 02:37:08 +01002726 return locked;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002727}
2728
2729int
2730xfs_qm_freelist_lock_nowait(
2731 xfs_qm_t *xqm)
2732{
2733 int locked;
2734
2735 locked = mutex_trylock(&(xqm->qm_dqfreelist.qh_lock));
Jesper Juhl014c2542006-01-15 02:37:08 +01002736 return locked;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002737}
2738
Christoph Hellwigba0f32d2005-06-21 15:36:52 +10002739STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -07002740xfs_qm_mplist_nowait(
2741 xfs_mount_t *mp)
2742{
2743 int locked;
2744
2745 ASSERT(mp->m_quotainfo);
2746 locked = mutex_trylock(&(XFS_QI_MPLLOCK(mp)));
Jesper Juhl014c2542006-01-15 02:37:08 +01002747 return locked;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002748}