blob: fd0b383d72a56fc7ed3a247b6315f547f9d7a679 [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 */
222 XFS_QM_LOCK(xfs_Gqm);
223
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 */
231 XFS_QM_HOLD(xfs_Gqm);
232 XFS_QM_UNLOCK(xfs_Gqm);
233
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 */
280 XFS_QM_LOCK(xfs_Gqm);
281 XFS_QM_RELE(xfs_Gqm);
282 if (xfs_Gqm->qm_nrefs == 0) {
283 xfs_qm_destroy(xfs_Gqm);
284 xfs_Gqm = NULL;
285 }
286 XFS_QM_UNLOCK(xfs_Gqm);
287}
288
289/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 * Just destroy the quotainfo structure.
291 */
292void
293xfs_qm_unmount_quotadestroy(
294 xfs_mount_t *mp)
295{
296 if (mp->m_quotainfo)
297 xfs_qm_destroy_quotainfo(mp);
298}
299
300
301/*
302 * This is called from xfs_mountfs to start quotas and initialize all
303 * necessary data structures like quotainfo. This is also responsible for
304 * running a quotacheck as necessary. We are guaranteed that the superblock
305 * is consistently read in at this point.
David Chinner53aa7912008-04-10 12:20:31 +1000306 *
307 * If we fail here, the mount will continue with quota turned off. We don't
308 * need to inidicate success or failure at all.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 */
David Chinner53aa7912008-04-10 12:20:31 +1000310void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311xfs_qm_mount_quotas(
Christoph Hellwig42490232008-08-13 16:49:32 +1000312 xfs_mount_t *mp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 int error = 0;
315 uint sbf;
316
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 /*
318 * If quotas on realtime volumes is not supported, we disable
319 * quotas immediately.
320 */
321 if (mp->m_sb.sb_rextents) {
322 cmn_err(CE_NOTE,
323 "Cannot turn on quotas for realtime filesystem %s",
324 mp->m_fsname);
325 mp->m_qflags = 0;
326 goto write_changes;
327 }
328
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 ASSERT(XFS_IS_QUOTA_RUNNING(mp));
Nathan Scott155ffd02005-09-02 16:43:48 +1000330
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 /*
332 * Allocate the quotainfo structure inside the mount struct, and
333 * create quotainode(s), and change/rev superblock if necessary.
334 */
David Chinner53aa7912008-04-10 12:20:31 +1000335 error = xfs_qm_init_quotainfo(mp);
336 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 /*
338 * We must turn off quotas.
339 */
340 ASSERT(mp->m_quotainfo == NULL);
341 mp->m_qflags = 0;
342 goto write_changes;
343 }
344 /*
345 * If any of the quotas are not consistent, do a quotacheck.
346 */
Christoph Hellwig42490232008-08-13 16:49:32 +1000347 if (XFS_QM_NEED_QUOTACHECK(mp)) {
David Chinner53aa7912008-04-10 12:20:31 +1000348 error = xfs_qm_quotacheck(mp);
349 if (error) {
350 /* Quotacheck failed and disabled quotas. */
351 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 }
Donald Douwsma646d5bd2007-05-08 13:49:09 +1000354 /*
355 * If one type of quotas is off, then it will lose its
356 * quotachecked status, since we won't be doing accounting for
357 * that type anymore.
358 */
David Chinner53aa7912008-04-10 12:20:31 +1000359 if (!XFS_IS_UQUOTA_ON(mp))
Donald Douwsma646d5bd2007-05-08 13:49:09 +1000360 mp->m_qflags &= ~XFS_UQUOTA_CHKD;
David Chinner53aa7912008-04-10 12:20:31 +1000361 if (!(XFS_IS_GQUOTA_ON(mp) || XFS_IS_PQUOTA_ON(mp)))
Donald Douwsma646d5bd2007-05-08 13:49:09 +1000362 mp->m_qflags &= ~XFS_OQUOTA_CHKD;
Nathan Scott155ffd02005-09-02 16:43:48 +1000363
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 write_changes:
365 /*
Eric Sandeen3685c2a2007-10-11 17:42:32 +1000366 * We actually don't have to acquire the m_sb_lock at all.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 * This can only be called from mount, and that's single threaded. XXX
368 */
Eric Sandeen3685c2a2007-10-11 17:42:32 +1000369 spin_lock(&mp->m_sb_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 sbf = mp->m_sb.sb_qflags;
371 mp->m_sb.sb_qflags = mp->m_qflags & XFS_MOUNT_QUOTA_ALL;
Eric Sandeen3685c2a2007-10-11 17:42:32 +1000372 spin_unlock(&mp->m_sb_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373
374 if (sbf != (mp->m_qflags & XFS_MOUNT_QUOTA_ALL)) {
375 if (xfs_qm_write_sb_changes(mp, XFS_SB_QFLAGS)) {
376 /*
377 * We could only have been turning quotas off.
378 * We aren't in very good shape actually because
379 * the incore structures are convinced that quotas are
380 * off, but the on disk superblock doesn't know that !
381 */
382 ASSERT(!(XFS_IS_QUOTA_RUNNING(mp)));
383 xfs_fs_cmn_err(CE_ALERT, mp,
384 "XFS mount_quotas: Superblock update failed!");
385 }
386 }
387
388 if (error) {
389 xfs_fs_cmn_err(CE_WARN, mp,
390 "Failed to initialize disk quotas.");
391 }
David Chinner53aa7912008-04-10 12:20:31 +1000392 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393}
394
395/*
396 * Called from the vfsops layer.
397 */
Christoph Hellwige57481d2008-12-03 12:20:36 +0100398void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399xfs_qm_unmount_quotas(
400 xfs_mount_t *mp)
401{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 /*
403 * Release the dquots that root inode, et al might be holding,
404 * before we flush quotas and blow away the quotainfo structure.
405 */
406 ASSERT(mp->m_rootip);
407 xfs_qm_dqdetach(mp->m_rootip);
408 if (mp->m_rbmip)
409 xfs_qm_dqdetach(mp->m_rbmip);
410 if (mp->m_rsumip)
411 xfs_qm_dqdetach(mp->m_rsumip);
412
413 /*
Christoph Hellwige57481d2008-12-03 12:20:36 +0100414 * Release the quota inodes.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 if (mp->m_quotainfo) {
Christoph Hellwige57481d2008-12-03 12:20:36 +0100417 if (mp->m_quotainfo->qi_uquotaip) {
418 IRELE(mp->m_quotainfo->qi_uquotaip);
419 mp->m_quotainfo->qi_uquotaip = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 }
Christoph Hellwige57481d2008-12-03 12:20:36 +0100421 if (mp->m_quotainfo->qi_gquotaip) {
422 IRELE(mp->m_quotainfo->qi_gquotaip);
423 mp->m_quotainfo->qi_gquotaip = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 }
425 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426}
427
428/*
429 * Flush all dquots of the given file system to disk. The dquots are
430 * _not_ purged from memory here, just their data written to disk.
431 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +1000432STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433xfs_qm_dqflush_all(
434 xfs_mount_t *mp,
435 int flags)
436{
437 int recl;
438 xfs_dquot_t *dqp;
439 int niters;
440 int error;
441
442 if (mp->m_quotainfo == NULL)
Jesper Juhl014c2542006-01-15 02:37:08 +0100443 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 niters = 0;
445again:
446 xfs_qm_mplist_lock(mp);
447 FOREACH_DQUOT_IN_MP(dqp, mp) {
448 xfs_dqlock(dqp);
449 if (! XFS_DQ_IS_DIRTY(dqp)) {
450 xfs_dqunlock(dqp);
451 continue;
452 }
453 xfs_dqtrace_entry(dqp, "FLUSHALL: DQDIRTY");
454 /* XXX a sentinel would be better */
455 recl = XFS_QI_MPLRECLAIMS(mp);
David Chinnere1f49cf2008-08-13 16:41:43 +1000456 if (!xfs_dqflock_nowait(dqp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 /*
458 * If we can't grab the flush lock then check
459 * to see if the dquot has been flushed delayed
460 * write. If so, grab its buffer and send it
461 * out immediately. We'll be able to acquire
462 * the flush lock when the I/O completes.
463 */
464 xfs_qm_dqflock_pushbuf_wait(dqp);
465 }
466 /*
467 * Let go of the mplist lock. We don't want to hold it
468 * across a disk write.
469 */
470 xfs_qm_mplist_unlock(mp);
471 error = xfs_qm_dqflush(dqp, flags);
472 xfs_dqunlock(dqp);
473 if (error)
Jesper Juhl014c2542006-01-15 02:37:08 +0100474 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475
476 xfs_qm_mplist_lock(mp);
477 if (recl != XFS_QI_MPLRECLAIMS(mp)) {
478 xfs_qm_mplist_unlock(mp);
479 /* XXX restart limit */
480 goto again;
481 }
482 }
483
484 xfs_qm_mplist_unlock(mp);
485 /* return ! busy */
Jesper Juhl014c2542006-01-15 02:37:08 +0100486 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487}
488/*
489 * Release the group dquot pointers the user dquots may be
490 * carrying around as a hint. mplist is locked on entry and exit.
491 */
492STATIC void
493xfs_qm_detach_gdquots(
494 xfs_mount_t *mp)
495{
496 xfs_dquot_t *dqp, *gdqp;
497 int nrecl;
498
499 again:
500 ASSERT(XFS_QM_IS_MPLIST_LOCKED(mp));
501 dqp = XFS_QI_MPLNEXT(mp);
502 while (dqp) {
503 xfs_dqlock(dqp);
504 if ((gdqp = dqp->q_gdquot)) {
505 xfs_dqlock(gdqp);
506 dqp->q_gdquot = NULL;
507 }
508 xfs_dqunlock(dqp);
509
510 if (gdqp) {
511 /*
512 * Can't hold the mplist lock across a dqput.
513 * XXXmust convert to marker based iterations here.
514 */
515 nrecl = XFS_QI_MPLRECLAIMS(mp);
516 xfs_qm_mplist_unlock(mp);
517 xfs_qm_dqput(gdqp);
518
519 xfs_qm_mplist_lock(mp);
520 if (nrecl != XFS_QI_MPLRECLAIMS(mp))
521 goto again;
522 }
523 dqp = dqp->MPL_NEXT;
524 }
525}
526
527/*
528 * Go through all the incore dquots of this file system and take them
529 * off the mplist and hashlist, if the dquot type matches the dqtype
530 * parameter. This is used when turning off quota accounting for
531 * users and/or groups, as well as when the filesystem is unmounting.
532 */
533STATIC int
534xfs_qm_dqpurge_int(
535 xfs_mount_t *mp,
Nathan Scottc8ad20f2005-06-21 15:38:48 +1000536 uint flags) /* QUOTAOFF/UMOUNTING/UQUOTA/PQUOTA/GQUOTA */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537{
538 xfs_dquot_t *dqp;
539 uint dqtype;
540 int nrecl;
541 xfs_dquot_t *nextdqp;
542 int nmisses;
543
544 if (mp->m_quotainfo == NULL)
Jesper Juhl014c2542006-01-15 02:37:08 +0100545 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546
547 dqtype = (flags & XFS_QMOPT_UQUOTA) ? XFS_DQ_USER : 0;
Nathan Scottc8ad20f2005-06-21 15:38:48 +1000548 dqtype |= (flags & XFS_QMOPT_PQUOTA) ? XFS_DQ_PROJ : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 dqtype |= (flags & XFS_QMOPT_GQUOTA) ? XFS_DQ_GROUP : 0;
550
551 xfs_qm_mplist_lock(mp);
552
553 /*
554 * In the first pass through all incore dquots of this filesystem,
555 * we release the group dquot pointers the user dquots may be
556 * carrying around as a hint. We need to do this irrespective of
557 * what's being turned off.
558 */
559 xfs_qm_detach_gdquots(mp);
560
561 again:
562 nmisses = 0;
563 ASSERT(XFS_QM_IS_MPLIST_LOCKED(mp));
564 /*
565 * Try to get rid of all of the unwanted dquots. The idea is to
566 * get them off mplist and hashlist, but leave them on freelist.
567 */
568 dqp = XFS_QI_MPLNEXT(mp);
569 while (dqp) {
570 /*
571 * It's OK to look at the type without taking dqlock here.
572 * We're holding the mplist lock here, and that's needed for
573 * a dqreclaim.
574 */
575 if ((dqp->dq_flags & dqtype) == 0) {
576 dqp = dqp->MPL_NEXT;
577 continue;
578 }
579
580 if (! xfs_qm_dqhashlock_nowait(dqp)) {
581 nrecl = XFS_QI_MPLRECLAIMS(mp);
582 xfs_qm_mplist_unlock(mp);
583 XFS_DQ_HASH_LOCK(dqp->q_hash);
584 xfs_qm_mplist_lock(mp);
585
586 /*
587 * XXXTheoretically, we can get into a very long
588 * ping pong game here.
589 * No one can be adding dquots to the mplist at
590 * this point, but somebody might be taking things off.
591 */
592 if (nrecl != XFS_QI_MPLRECLAIMS(mp)) {
593 XFS_DQ_HASH_UNLOCK(dqp->q_hash);
594 goto again;
595 }
596 }
597
598 /*
599 * Take the dquot off the mplist and hashlist. It may remain on
600 * freelist in INACTIVE state.
601 */
602 nextdqp = dqp->MPL_NEXT;
Denys Vlasenko4f0e8a92008-05-19 16:34:04 +1000603 nmisses += xfs_qm_dqpurge(dqp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 dqp = nextdqp;
605 }
606 xfs_qm_mplist_unlock(mp);
607 return nmisses;
608}
609
610int
611xfs_qm_dqpurge_all(
612 xfs_mount_t *mp,
613 uint flags)
614{
615 int ndquots;
616
617 /*
618 * Purge the dquot cache.
619 * None of the dquots should really be busy at this point.
620 */
621 if (mp->m_quotainfo) {
622 while ((ndquots = xfs_qm_dqpurge_int(mp, flags))) {
623 delay(ndquots * 10);
624 }
625 }
626 return 0;
627}
628
629STATIC int
630xfs_qm_dqattach_one(
631 xfs_inode_t *ip,
632 xfs_dqid_t id,
633 uint type,
634 uint doalloc,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 xfs_dquot_t *udqhint, /* hint */
636 xfs_dquot_t **IO_idqpp)
637{
638 xfs_dquot_t *dqp;
639 int error;
640
Christoph Hellwig579aa9c2008-04-22 17:34:00 +1000641 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 error = 0;
Christoph Hellwig8e9b6e72009-02-08 21:51:42 +0100643
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 /*
645 * See if we already have it in the inode itself. IO_idqpp is
646 * &i_udquot or &i_gdquot. This made the code look weird, but
647 * made the logic a lot simpler.
648 */
Christoph Hellwig8e9b6e72009-02-08 21:51:42 +0100649 dqp = *IO_idqpp;
650 if (dqp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 xfs_dqtrace_entry(dqp, "DQATTACH: found in ip");
Christoph Hellwig8e9b6e72009-02-08 21:51:42 +0100652 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 }
654
655 /*
656 * udqhint is the i_udquot field in inode, and is non-NULL only
Nathan Scottc8ad20f2005-06-21 15:38:48 +1000657 * when the type arg is group/project. Its purpose is to save a
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 * lookup by dqid (xfs_qm_dqget) by caching a group dquot inside
659 * the user dquot.
660 */
Christoph Hellwig8e9b6e72009-02-08 21:51:42 +0100661 if (udqhint) {
662 ASSERT(type == XFS_DQ_GROUP || type == XFS_DQ_PROJ);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 xfs_dqlock(udqhint);
664
Christoph Hellwig8e9b6e72009-02-08 21:51:42 +0100665 /*
666 * No need to take dqlock to look at the id.
667 *
668 * The ID can't change until it gets reclaimed, and it won't
669 * be reclaimed as long as we have a ref from inode and we
670 * hold the ilock.
671 */
672 dqp = udqhint->q_gdquot;
673 if (dqp && be32_to_cpu(dqp->q_core.d_id) == id) {
674 xfs_dqlock(dqp);
675 XFS_DQHOLD(dqp);
676 ASSERT(*IO_idqpp == NULL);
677 *IO_idqpp = dqp;
678
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 xfs_dqunlock(dqp);
680 xfs_dqunlock(udqhint);
Christoph Hellwig8e9b6e72009-02-08 21:51:42 +0100681 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 }
Christoph Hellwig8e9b6e72009-02-08 21:51:42 +0100683
684 /*
685 * We can't hold a dquot lock when we call the dqget code.
686 * We'll deadlock in no time, because of (not conforming to)
687 * lock ordering - the inodelock comes before any dquot lock,
688 * and we may drop and reacquire the ilock in xfs_qm_dqget().
689 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 xfs_dqunlock(udqhint);
Christoph Hellwig8e9b6e72009-02-08 21:51:42 +0100691 }
692
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 /*
694 * Find the dquot from somewhere. This bumps the
695 * reference count of dquot and returns it locked.
696 * This can return ENOENT if dquot didn't exist on
697 * disk and we didn't ask it to allocate;
698 * ESRCH if quotas got turned off suddenly.
699 */
Christoph Hellwig8e9b6e72009-02-08 21:51:42 +0100700 error = xfs_qm_dqget(ip->i_mount, ip, id, type, XFS_QMOPT_DOWARN, &dqp);
701 if (error)
702 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703
704 xfs_dqtrace_entry(dqp, "DQATTACH: found by dqget");
Christoph Hellwig8e9b6e72009-02-08 21:51:42 +0100705
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 /*
707 * dqget may have dropped and re-acquired the ilock, but it guarantees
708 * that the dquot returned is the one that should go in the inode.
709 */
710 *IO_idqpp = dqp;
Christoph Hellwig8e9b6e72009-02-08 21:51:42 +0100711 xfs_dqunlock(dqp);
712 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713}
714
715
716/*
717 * Given a udquot and gdquot, attach a ptr to the group dquot in the
718 * udquot as a hint for future lookups. The idea sounds simple, but the
719 * execution isn't, because the udquot might have a group dquot attached
Nathan Scottc41564b2006-03-29 08:55:14 +1000720 * already and getting rid of that gets us into lock ordering constraints.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 * The process is complicated more by the fact that the dquots may or may not
722 * be locked on entry.
723 */
724STATIC void
725xfs_qm_dqattach_grouphint(
726 xfs_dquot_t *udq,
Christoph Hellwig8e9b6e72009-02-08 21:51:42 +0100727 xfs_dquot_t *gdq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728{
729 xfs_dquot_t *tmp;
730
Christoph Hellwig8e9b6e72009-02-08 21:51:42 +0100731 xfs_dqlock(udq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732
733 if ((tmp = udq->q_gdquot)) {
734 if (tmp == gdq) {
Christoph Hellwig8e9b6e72009-02-08 21:51:42 +0100735 xfs_dqunlock(udq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 return;
737 }
738
739 udq->q_gdquot = NULL;
740 /*
741 * We can't keep any dqlocks when calling dqrele,
742 * because the freelist lock comes before dqlocks.
743 */
744 xfs_dqunlock(udq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 /*
746 * we took a hard reference once upon a time in dqget,
747 * so give it back when the udquot no longer points at it
748 * dqput() does the unlocking of the dquot.
749 */
750 xfs_qm_dqrele(tmp);
751
752 xfs_dqlock(udq);
753 xfs_dqlock(gdq);
754
755 } else {
756 ASSERT(XFS_DQ_IS_LOCKED(udq));
Christoph Hellwig8e9b6e72009-02-08 21:51:42 +0100757 xfs_dqlock(gdq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 }
759
760 ASSERT(XFS_DQ_IS_LOCKED(udq));
761 ASSERT(XFS_DQ_IS_LOCKED(gdq));
762 /*
763 * Somebody could have attached a gdquot here,
764 * when we dropped the uqlock. If so, just do nothing.
765 */
766 if (udq->q_gdquot == NULL) {
767 XFS_DQHOLD(gdq);
768 udq->q_gdquot = gdq;
769 }
Christoph Hellwig8e9b6e72009-02-08 21:51:42 +0100770
771 xfs_dqunlock(gdq);
772 xfs_dqunlock(udq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773}
774
775
776/*
Nathan Scottc8ad20f2005-06-21 15:38:48 +1000777 * Given a locked inode, attach dquot(s) to it, taking U/G/P-QUOTAON
778 * into account.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 * If XFS_QMOPT_DQALLOC, the dquot(s) will be allocated if needed.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 * If XFS_QMOPT_ILOCKED, then inode sent is already locked EXCL.
781 * Inode may get unlocked and relocked in here, and the caller must deal with
782 * the consequences.
783 */
784int
785xfs_qm_dqattach(
786 xfs_inode_t *ip,
787 uint flags)
788{
789 xfs_mount_t *mp = ip->i_mount;
790 uint nquotas = 0;
791 int error = 0;
792
793 if ((! XFS_IS_QUOTA_ON(mp)) ||
794 (! XFS_NOT_DQATTACHED(mp, ip)) ||
795 (ip->i_ino == mp->m_sb.sb_uquotino) ||
796 (ip->i_ino == mp->m_sb.sb_gquotino))
Jesper Juhl014c2542006-01-15 02:37:08 +0100797 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798
799 ASSERT((flags & XFS_QMOPT_ILOCKED) == 0 ||
Christoph Hellwig579aa9c2008-04-22 17:34:00 +1000800 xfs_isilocked(ip, XFS_ILOCK_EXCL));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801
802 if (! (flags & XFS_QMOPT_ILOCKED))
803 xfs_ilock(ip, XFS_ILOCK_EXCL);
804
805 if (XFS_IS_UQUOTA_ON(mp)) {
806 error = xfs_qm_dqattach_one(ip, ip->i_d.di_uid, XFS_DQ_USER,
807 flags & XFS_QMOPT_DQALLOC,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 NULL, &ip->i_udquot);
809 if (error)
810 goto done;
811 nquotas++;
812 }
Christoph Hellwig579aa9c2008-04-22 17:34:00 +1000813
814 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
Nathan Scottc8ad20f2005-06-21 15:38:48 +1000815 if (XFS_IS_OQUOTA_ON(mp)) {
816 error = XFS_IS_GQUOTA_ON(mp) ?
817 xfs_qm_dqattach_one(ip, ip->i_d.di_gid, XFS_DQ_GROUP,
818 flags & XFS_QMOPT_DQALLOC,
Nathan Scottc8ad20f2005-06-21 15:38:48 +1000819 ip->i_udquot, &ip->i_gdquot) :
820 xfs_qm_dqattach_one(ip, ip->i_d.di_projid, XFS_DQ_PROJ,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 flags & XFS_QMOPT_DQALLOC,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 ip->i_udquot, &ip->i_gdquot);
823 /*
824 * Don't worry about the udquot that we may have
825 * attached above. It'll get detached, if not already.
826 */
827 if (error)
828 goto done;
829 nquotas++;
830 }
831
832 /*
833 * Attach this group quota to the user quota as a hint.
834 * This WON'T, in general, result in a thrash.
835 */
836 if (nquotas == 2) {
Christoph Hellwig579aa9c2008-04-22 17:34:00 +1000837 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 ASSERT(ip->i_udquot);
839 ASSERT(ip->i_gdquot);
840
841 /*
842 * We may or may not have the i_udquot locked at this point,
843 * but this check is OK since we don't depend on the i_gdquot to
844 * be accurate 100% all the time. It is just a hint, and this
845 * will succeed in general.
846 */
847 if (ip->i_udquot->q_gdquot == ip->i_gdquot)
848 goto done;
849 /*
850 * Attach i_gdquot to the gdquot hint inside the i_udquot.
851 */
Christoph Hellwig8e9b6e72009-02-08 21:51:42 +0100852 xfs_qm_dqattach_grouphint(ip->i_udquot, ip->i_gdquot);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 }
854
855 done:
856
857#ifdef QUOTADEBUG
858 if (! error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 if (XFS_IS_UQUOTA_ON(mp))
860 ASSERT(ip->i_udquot);
Nathan Scottc8ad20f2005-06-21 15:38:48 +1000861 if (XFS_IS_OQUOTA_ON(mp))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 ASSERT(ip->i_gdquot);
863 }
864#endif
865
866 if (! (flags & XFS_QMOPT_ILOCKED))
867 xfs_iunlock(ip, XFS_ILOCK_EXCL);
868
869#ifdef QUOTADEBUG
870 else
Christoph Hellwig579aa9c2008-04-22 17:34:00 +1000871 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872#endif
Jesper Juhl014c2542006-01-15 02:37:08 +0100873 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874}
875
876/*
877 * Release dquots (and their references) if any.
878 * The inode should be locked EXCL except when this's called by
879 * xfs_ireclaim.
880 */
881void
882xfs_qm_dqdetach(
883 xfs_inode_t *ip)
884{
885 if (!(ip->i_udquot || ip->i_gdquot))
886 return;
887
888 ASSERT(ip->i_ino != ip->i_mount->m_sb.sb_uquotino);
889 ASSERT(ip->i_ino != ip->i_mount->m_sb.sb_gquotino);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 if (ip->i_udquot) {
Nathan Scottc8ad20f2005-06-21 15:38:48 +1000891 xfs_dqtrace_entry_ino(ip->i_udquot, "DQDETTACH", ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 xfs_qm_dqrele(ip->i_udquot);
893 ip->i_udquot = NULL;
894 }
895 if (ip->i_gdquot) {
Nathan Scottc8ad20f2005-06-21 15:38:48 +1000896 xfs_dqtrace_entry_ino(ip->i_gdquot, "DQDETTACH", ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 xfs_qm_dqrele(ip->i_gdquot);
898 ip->i_gdquot = NULL;
899 }
900}
901
902/*
David Chinnera4e4c4f2008-10-30 17:16:11 +1100903 * This is called to sync quotas. We can be told to use non-blocking
904 * semantics by either the SYNC_BDFLUSH flag or the absence of the
905 * SYNC_WAIT flag.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907int
908xfs_qm_sync(
909 xfs_mount_t *mp,
Christoph Hellwigb09cc772007-08-30 17:19:57 +1000910 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911{
912 int recl, restarts;
913 xfs_dquot_t *dqp;
914 uint flush_flags;
915 boolean_t nowait;
916 int error;
917
Donald Douwsmad7577622007-11-23 16:27:42 +1100918 if (! XFS_IS_QUOTA_ON(mp))
919 return 0;
920
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 restarts = 0;
922 /*
923 * We won't block unless we are asked to.
924 */
925 nowait = (boolean_t)(flags & SYNC_BDFLUSH || (flags & SYNC_WAIT) == 0);
926
927 again:
928 xfs_qm_mplist_lock(mp);
929 /*
930 * dqpurge_all() also takes the mplist lock and iterate thru all dquots
931 * in quotaoff. However, if the QUOTA_ACTIVE bits are not cleared
932 * when we have the mplist lock, we know that dquots will be consistent
933 * as long as we have it locked.
934 */
935 if (! XFS_IS_QUOTA_ON(mp)) {
936 xfs_qm_mplist_unlock(mp);
Jesper Juhl014c2542006-01-15 02:37:08 +0100937 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 }
939 FOREACH_DQUOT_IN_MP(dqp, mp) {
940 /*
941 * If this is vfs_sync calling, then skip the dquots that
942 * don't 'seem' to be dirty. ie. don't acquire dqlock.
943 * This is very similar to what xfs_sync does with inodes.
944 */
945 if (flags & SYNC_BDFLUSH) {
946 if (! XFS_DQ_IS_DIRTY(dqp))
947 continue;
948 }
949
950 if (nowait) {
951 /*
952 * Try to acquire the dquot lock. We are NOT out of
953 * lock order, but we just don't want to wait for this
954 * lock, unless somebody wanted us to.
955 */
956 if (! xfs_qm_dqlock_nowait(dqp))
957 continue;
958 } else {
959 xfs_dqlock(dqp);
960 }
961
962 /*
963 * Now, find out for sure if this dquot is dirty or not.
964 */
965 if (! XFS_DQ_IS_DIRTY(dqp)) {
966 xfs_dqunlock(dqp);
967 continue;
968 }
969
970 /* XXX a sentinel would be better */
971 recl = XFS_QI_MPLRECLAIMS(mp);
David Chinnere1f49cf2008-08-13 16:41:43 +1000972 if (!xfs_dqflock_nowait(dqp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 if (nowait) {
974 xfs_dqunlock(dqp);
975 continue;
976 }
977 /*
978 * If we can't grab the flush lock then if the caller
Nathan Scottc41564b2006-03-29 08:55:14 +1000979 * really wanted us to give this our best shot, so
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 * see if we can give a push to the buffer before we wait
981 * on the flush lock. At this point, we know that
Nathan Scottc41564b2006-03-29 08:55:14 +1000982 * even though the dquot is being flushed,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 * it has (new) dirty data.
984 */
985 xfs_qm_dqflock_pushbuf_wait(dqp);
986 }
987 /*
988 * Let go of the mplist lock. We don't want to hold it
989 * across a disk write
990 */
991 flush_flags = (nowait) ? XFS_QMOPT_DELWRI : XFS_QMOPT_SYNC;
992 xfs_qm_mplist_unlock(mp);
993 xfs_dqtrace_entry(dqp, "XQM_SYNC: DQFLUSH");
994 error = xfs_qm_dqflush(dqp, flush_flags);
995 xfs_dqunlock(dqp);
996 if (error && XFS_FORCED_SHUTDOWN(mp))
Jesper Juhl014c2542006-01-15 02:37:08 +0100997 return 0; /* Need to prevent umount failure */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 else if (error)
Jesper Juhl014c2542006-01-15 02:37:08 +0100999 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000
1001 xfs_qm_mplist_lock(mp);
1002 if (recl != XFS_QI_MPLRECLAIMS(mp)) {
1003 if (++restarts >= XFS_QM_SYNC_MAX_RESTARTS)
1004 break;
1005
1006 xfs_qm_mplist_unlock(mp);
1007 goto again;
1008 }
1009 }
1010
1011 xfs_qm_mplist_unlock(mp);
Jesper Juhl014c2542006-01-15 02:37:08 +01001012 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013}
1014
Christoph Hellwiga4edd1d2009-01-19 02:03:11 +01001015/*
1016 * The hash chains and the mplist use the same xfs_dqhash structure as
1017 * their list head, but we can take the mplist qh_lock and one of the
1018 * hash qh_locks at the same time without any problem as they aren't
1019 * related.
1020 */
1021static struct lock_class_key xfs_quota_mplist_class;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022
1023/*
1024 * This initializes all the quota information that's kept in the
1025 * mount structure
1026 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +10001027STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028xfs_qm_init_quotainfo(
1029 xfs_mount_t *mp)
1030{
1031 xfs_quotainfo_t *qinf;
1032 int error;
1033 xfs_dquot_t *dqp;
1034
1035 ASSERT(XFS_IS_QUOTA_RUNNING(mp));
1036
1037 /*
1038 * Tell XQM that we exist as soon as possible.
1039 */
1040 if ((error = xfs_qm_hold_quotafs_ref(mp))) {
Jesper Juhl014c2542006-01-15 02:37:08 +01001041 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 }
1043
1044 qinf = mp->m_quotainfo = kmem_zalloc(sizeof(xfs_quotainfo_t), KM_SLEEP);
1045
1046 /*
1047 * See if quotainodes are setup, and if not, allocate them,
1048 * and change the superblock accordingly.
1049 */
1050 if ((error = xfs_qm_init_quotainos(mp))) {
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10001051 kmem_free(qinf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 mp->m_quotainfo = NULL;
Jesper Juhl014c2542006-01-15 02:37:08 +01001053 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 }
1055
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 xfs_qm_list_init(&qinf->qi_dqlist, "mpdqlist", 0);
Christoph Hellwiga4edd1d2009-01-19 02:03:11 +01001057 lockdep_set_class(&qinf->qi_dqlist.qh_lock, &xfs_quota_mplist_class);
1058
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 qinf->qi_dqreclaims = 0;
1060
1061 /* mutex used to serialize quotaoffs */
Jes Sorensen794ee1b2006-01-09 15:59:21 -08001062 mutex_init(&qinf->qi_quotaofflock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063
1064 /* Precalc some constants */
1065 qinf->qi_dqchunklen = XFS_FSB_TO_BB(mp, XFS_DQUOT_CLUSTER_SIZE_FSB);
1066 ASSERT(qinf->qi_dqchunklen);
1067 qinf->qi_dqperchunk = BBTOB(qinf->qi_dqchunklen);
1068 do_div(qinf->qi_dqperchunk, sizeof(xfs_dqblk_t));
1069
1070 mp->m_qflags |= (mp->m_sb.sb_qflags & XFS_ALL_QUOTA_CHKD);
1071
1072 /*
1073 * We try to get the limits from the superuser's limits fields.
1074 * This is quite hacky, but it is standard quota practice.
1075 * We look at the USR dquot with id == 0 first, but if user quotas
1076 * are not enabled we goto the GRP dquot with id == 0.
1077 * We don't really care to keep separate default limits for user
1078 * and group quotas, at least not at this point.
1079 */
1080 error = xfs_qm_dqget(mp, NULL, (xfs_dqid_t)0,
Nathan Scottc8ad20f2005-06-21 15:38:48 +10001081 XFS_IS_UQUOTA_RUNNING(mp) ? XFS_DQ_USER :
1082 (XFS_IS_GQUOTA_RUNNING(mp) ? XFS_DQ_GROUP :
1083 XFS_DQ_PROJ),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 XFS_QMOPT_DQSUSER|XFS_QMOPT_DOWARN,
1085 &dqp);
1086 if (! error) {
1087 xfs_disk_dquot_t *ddqp = &dqp->q_core;
1088
1089 /*
1090 * The warnings and timers set the grace period given to
1091 * a user or group before he or she can not perform any
1092 * more writing. If it is zero, a default is used.
1093 */
Christoph Hellwig1149d962005-11-02 15:01:12 +11001094 qinf->qi_btimelimit = ddqp->d_btimer ?
1095 be32_to_cpu(ddqp->d_btimer) : XFS_QM_BTIMELIMIT;
1096 qinf->qi_itimelimit = ddqp->d_itimer ?
1097 be32_to_cpu(ddqp->d_itimer) : XFS_QM_ITIMELIMIT;
1098 qinf->qi_rtbtimelimit = ddqp->d_rtbtimer ?
1099 be32_to_cpu(ddqp->d_rtbtimer) : XFS_QM_RTBTIMELIMIT;
1100 qinf->qi_bwarnlimit = ddqp->d_bwarns ?
1101 be16_to_cpu(ddqp->d_bwarns) : XFS_QM_BWARNLIMIT;
1102 qinf->qi_iwarnlimit = ddqp->d_iwarns ?
1103 be16_to_cpu(ddqp->d_iwarns) : XFS_QM_IWARNLIMIT;
1104 qinf->qi_rtbwarnlimit = ddqp->d_rtbwarns ?
1105 be16_to_cpu(ddqp->d_rtbwarns) : XFS_QM_RTBWARNLIMIT;
1106 qinf->qi_bhardlimit = be64_to_cpu(ddqp->d_blk_hardlimit);
1107 qinf->qi_bsoftlimit = be64_to_cpu(ddqp->d_blk_softlimit);
1108 qinf->qi_ihardlimit = be64_to_cpu(ddqp->d_ino_hardlimit);
1109 qinf->qi_isoftlimit = be64_to_cpu(ddqp->d_ino_softlimit);
1110 qinf->qi_rtbhardlimit = be64_to_cpu(ddqp->d_rtb_hardlimit);
1111 qinf->qi_rtbsoftlimit = be64_to_cpu(ddqp->d_rtb_softlimit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112
1113 /*
1114 * We sent the XFS_QMOPT_DQSUSER flag to dqget because
1115 * we don't want this dquot cached. We haven't done a
1116 * quotacheck yet, and quotacheck doesn't like incore dquots.
1117 */
1118 xfs_qm_dqdestroy(dqp);
1119 } else {
1120 qinf->qi_btimelimit = XFS_QM_BTIMELIMIT;
1121 qinf->qi_itimelimit = XFS_QM_ITIMELIMIT;
1122 qinf->qi_rtbtimelimit = XFS_QM_RTBTIMELIMIT;
1123 qinf->qi_bwarnlimit = XFS_QM_BWARNLIMIT;
1124 qinf->qi_iwarnlimit = XFS_QM_IWARNLIMIT;
Nathan Scott06d10dd2005-06-21 15:48:47 +10001125 qinf->qi_rtbwarnlimit = XFS_QM_RTBWARNLIMIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 }
1127
Jesper Juhl014c2542006-01-15 02:37:08 +01001128 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129}
1130
1131
1132/*
1133 * Gets called when unmounting a filesystem or when all quotas get
1134 * turned off.
1135 * This purges the quota inodes, destroys locks and frees itself.
1136 */
1137void
1138xfs_qm_destroy_quotainfo(
1139 xfs_mount_t *mp)
1140{
1141 xfs_quotainfo_t *qi;
1142
1143 qi = mp->m_quotainfo;
1144 ASSERT(qi != NULL);
1145 ASSERT(xfs_Gqm != NULL);
1146
1147 /*
1148 * Release the reference that XQM kept, so that we know
1149 * when the XQM structure should be freed. We cannot assume
1150 * that xfs_Gqm is non-null after this point.
1151 */
1152 xfs_qm_rele_quotafs_ref(mp);
1153
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 xfs_qm_list_destroy(&qi->qi_dqlist);
1155
1156 if (qi->qi_uquotaip) {
Christoph Hellwig26cc0022008-07-18 17:12:43 +10001157 IRELE(qi->qi_uquotaip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 qi->qi_uquotaip = NULL; /* paranoia */
1159 }
1160 if (qi->qi_gquotaip) {
Christoph Hellwig26cc0022008-07-18 17:12:43 +10001161 IRELE(qi->qi_gquotaip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 qi->qi_gquotaip = NULL;
1163 }
1164 mutex_destroy(&qi->qi_quotaofflock);
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10001165 kmem_free(qi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 mp->m_quotainfo = NULL;
1167}
1168
1169
1170
1171/* ------------------- PRIVATE STATIC FUNCTIONS ----------------------- */
1172
1173/* ARGSUSED */
1174STATIC void
1175xfs_qm_list_init(
1176 xfs_dqlist_t *list,
1177 char *str,
1178 int n)
1179{
Jes Sorensen794ee1b2006-01-09 15:59:21 -08001180 mutex_init(&list->qh_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 list->qh_next = NULL;
1182 list->qh_version = 0;
1183 list->qh_nelems = 0;
1184}
1185
1186STATIC void
1187xfs_qm_list_destroy(
1188 xfs_dqlist_t *list)
1189{
1190 mutex_destroy(&(list->qh_lock));
1191}
1192
1193
1194/*
1195 * Stripped down version of dqattach. This doesn't attach, or even look at the
1196 * dquots attached to the inode. The rationale is that there won't be any
1197 * attached at the time this is called from quotacheck.
1198 */
1199STATIC int
1200xfs_qm_dqget_noattach(
1201 xfs_inode_t *ip,
1202 xfs_dquot_t **O_udqpp,
1203 xfs_dquot_t **O_gdqpp)
1204{
1205 int error;
1206 xfs_mount_t *mp;
1207 xfs_dquot_t *udqp, *gdqp;
1208
Christoph Hellwig579aa9c2008-04-22 17:34:00 +10001209 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 mp = ip->i_mount;
1211 udqp = NULL;
1212 gdqp = NULL;
1213
1214 if (XFS_IS_UQUOTA_ON(mp)) {
1215 ASSERT(ip->i_udquot == NULL);
1216 /*
1217 * We want the dquot allocated if it doesn't exist.
1218 */
1219 if ((error = xfs_qm_dqget(mp, ip, ip->i_d.di_uid, XFS_DQ_USER,
1220 XFS_QMOPT_DQALLOC | XFS_QMOPT_DOWARN,
1221 &udqp))) {
1222 /*
1223 * Shouldn't be able to turn off quotas here.
1224 */
1225 ASSERT(error != ESRCH);
1226 ASSERT(error != ENOENT);
Jesper Juhl014c2542006-01-15 02:37:08 +01001227 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 }
1229 ASSERT(udqp);
1230 }
1231
Nathan Scottc8ad20f2005-06-21 15:38:48 +10001232 if (XFS_IS_OQUOTA_ON(mp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 ASSERT(ip->i_gdquot == NULL);
1234 if (udqp)
1235 xfs_dqunlock(udqp);
Nathan Scottc8ad20f2005-06-21 15:38:48 +10001236 error = XFS_IS_GQUOTA_ON(mp) ?
1237 xfs_qm_dqget(mp, ip,
1238 ip->i_d.di_gid, XFS_DQ_GROUP,
1239 XFS_QMOPT_DQALLOC|XFS_QMOPT_DOWARN,
1240 &gdqp) :
1241 xfs_qm_dqget(mp, ip,
1242 ip->i_d.di_projid, XFS_DQ_PROJ,
1243 XFS_QMOPT_DQALLOC|XFS_QMOPT_DOWARN,
1244 &gdqp);
1245 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 if (udqp)
1247 xfs_qm_dqrele(udqp);
1248 ASSERT(error != ESRCH);
1249 ASSERT(error != ENOENT);
Jesper Juhl014c2542006-01-15 02:37:08 +01001250 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 }
1252 ASSERT(gdqp);
1253
1254 /* Reacquire the locks in the right order */
1255 if (udqp) {
1256 if (! xfs_qm_dqlock_nowait(udqp)) {
1257 xfs_dqunlock(gdqp);
1258 xfs_dqlock(udqp);
1259 xfs_dqlock(gdqp);
1260 }
1261 }
1262 }
1263
1264 *O_udqpp = udqp;
1265 *O_gdqpp = gdqp;
1266
1267#ifdef QUOTADEBUG
1268 if (udqp) ASSERT(XFS_DQ_IS_LOCKED(udqp));
1269 if (gdqp) ASSERT(XFS_DQ_IS_LOCKED(gdqp));
1270#endif
Jesper Juhl014c2542006-01-15 02:37:08 +01001271 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272}
1273
1274/*
1275 * Create an inode and return with a reference already taken, but unlocked
1276 * This is how we create quota inodes
1277 */
1278STATIC int
1279xfs_qm_qino_alloc(
1280 xfs_mount_t *mp,
1281 xfs_inode_t **ip,
1282 __int64_t sbfields,
1283 uint flags)
1284{
1285 xfs_trans_t *tp;
1286 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 int committed;
1288
Nathan Scott061f7202006-01-11 15:27:50 +11001289 tp = xfs_trans_alloc(mp, XFS_TRANS_QM_QINOCREATE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 if ((error = xfs_trans_reserve(tp,
1291 XFS_QM_QINOCREATE_SPACE_RES(mp),
1292 XFS_CREATE_LOG_RES(mp), 0,
1293 XFS_TRANS_PERM_LOG_RES,
1294 XFS_CREATE_LOG_COUNT))) {
1295 xfs_trans_cancel(tp, 0);
Jesper Juhl014c2542006-01-15 02:37:08 +01001296 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298
David Chinnerb11f94d2007-07-11 11:09:33 +10001299 if ((error = xfs_dir_ialloc(&tp, NULL, S_IFREG, 1, 0,
Nathan Scottdae81d42006-02-28 12:30:13 +11001300 &xfs_zerocr, 0, 1, ip, &committed))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES |
1302 XFS_TRANS_ABORT);
Jesper Juhl014c2542006-01-15 02:37:08 +01001303 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 }
1305
1306 /*
1307 * Keep an extra reference to this quota inode. This inode is
1308 * locked exclusively and joined to the transaction already.
1309 */
Christoph Hellwig579aa9c2008-04-22 17:34:00 +10001310 ASSERT(xfs_isilocked(*ip, XFS_ILOCK_EXCL));
Christoph Hellwig26cc0022008-07-18 17:12:43 +10001311 IHOLD(*ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312
1313 /*
1314 * Make the changes in the superblock, and log those too.
1315 * sbfields arg may contain fields other than *QUOTINO;
1316 * VERSIONNUM for example.
1317 */
Eric Sandeen3685c2a2007-10-11 17:42:32 +10001318 spin_lock(&mp->m_sb_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 if (flags & XFS_QMOPT_SBVERSION) {
1320#if defined(DEBUG) && defined(XFS_LOUD_RECOVERY)
1321 unsigned oldv = mp->m_sb.sb_versionnum;
1322#endif
Eric Sandeen62118702008-03-06 13:44:28 +11001323 ASSERT(!xfs_sb_version_hasquota(&mp->m_sb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 ASSERT((sbfields & (XFS_SB_VERSIONNUM | XFS_SB_UQUOTINO |
1325 XFS_SB_GQUOTINO | XFS_SB_QFLAGS)) ==
1326 (XFS_SB_VERSIONNUM | XFS_SB_UQUOTINO |
1327 XFS_SB_GQUOTINO | XFS_SB_QFLAGS));
1328
Eric Sandeen62118702008-03-06 13:44:28 +11001329 xfs_sb_version_addquota(&mp->m_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 mp->m_sb.sb_uquotino = NULLFSINO;
1331 mp->m_sb.sb_gquotino = NULLFSINO;
1332
1333 /* qflags will get updated _after_ quotacheck */
1334 mp->m_sb.sb_qflags = 0;
1335#if defined(DEBUG) && defined(XFS_LOUD_RECOVERY)
1336 cmn_err(CE_NOTE,
1337 "Old superblock version %x, converting to %x.",
1338 oldv, mp->m_sb.sb_versionnum);
1339#endif
1340 }
1341 if (flags & XFS_QMOPT_UQUOTA)
1342 mp->m_sb.sb_uquotino = (*ip)->i_ino;
1343 else
1344 mp->m_sb.sb_gquotino = (*ip)->i_ino;
Eric Sandeen3685c2a2007-10-11 17:42:32 +10001345 spin_unlock(&mp->m_sb_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 xfs_mod_sb(tp, sbfields);
1347
Eric Sandeen1c72bf92007-05-08 13:48:42 +10001348 if ((error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 xfs_fs_cmn_err(CE_ALERT, mp, "XFS qino_alloc failed!");
Jesper Juhl014c2542006-01-15 02:37:08 +01001350 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 }
Jesper Juhl014c2542006-01-15 02:37:08 +01001352 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353}
1354
1355
David Chinner5b139732008-04-10 12:20:10 +10001356STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357xfs_qm_reset_dqcounts(
1358 xfs_mount_t *mp,
1359 xfs_buf_t *bp,
1360 xfs_dqid_t id,
1361 uint type)
1362{
1363 xfs_disk_dquot_t *ddq;
1364 int j;
1365
1366 xfs_buftrace("RESET DQUOTS", bp);
1367 /*
1368 * Reset all counters and timers. They'll be
1369 * started afresh by xfs_qm_quotacheck.
1370 */
1371#ifdef DEBUG
1372 j = XFS_FSB_TO_B(mp, XFS_DQUOT_CLUSTER_SIZE_FSB);
1373 do_div(j, sizeof(xfs_dqblk_t));
1374 ASSERT(XFS_QM_DQPERBLK(mp) == j);
1375#endif
1376 ddq = (xfs_disk_dquot_t *)XFS_BUF_PTR(bp);
1377 for (j = 0; j < XFS_QM_DQPERBLK(mp); j++) {
1378 /*
1379 * Do a sanity check, and if needed, repair the dqblk. Don't
1380 * output any warnings because it's perfectly possible to
Nathan Scottc41564b2006-03-29 08:55:14 +10001381 * find uninitialised dquot blks. See comment in xfs_qm_dqcheck.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 */
1383 (void) xfs_qm_dqcheck(ddq, id+j, type, XFS_QMOPT_DQREPAIR,
1384 "xfs_quotacheck");
Christoph Hellwig1149d962005-11-02 15:01:12 +11001385 ddq->d_bcount = 0;
1386 ddq->d_icount = 0;
1387 ddq->d_rtbcount = 0;
1388 ddq->d_btimer = 0;
1389 ddq->d_itimer = 0;
1390 ddq->d_rtbtimer = 0;
1391 ddq->d_bwarns = 0;
1392 ddq->d_iwarns = 0;
1393 ddq->d_rtbwarns = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 ddq = (xfs_disk_dquot_t *) ((xfs_dqblk_t *)ddq + 1);
1395 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396}
1397
1398STATIC int
1399xfs_qm_dqiter_bufs(
1400 xfs_mount_t *mp,
1401 xfs_dqid_t firstid,
1402 xfs_fsblock_t bno,
1403 xfs_filblks_t blkcnt,
1404 uint flags)
1405{
1406 xfs_buf_t *bp;
1407 int error;
1408 int notcommitted;
1409 int incr;
Nathan Scottc8ad20f2005-06-21 15:38:48 +10001410 int type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411
1412 ASSERT(blkcnt > 0);
1413 notcommitted = 0;
1414 incr = (blkcnt > XFS_QM_MAX_DQCLUSTER_LOGSZ) ?
1415 XFS_QM_MAX_DQCLUSTER_LOGSZ : blkcnt;
Nathan Scottc8ad20f2005-06-21 15:38:48 +10001416 type = flags & XFS_QMOPT_UQUOTA ? XFS_DQ_USER :
1417 (flags & XFS_QMOPT_PQUOTA ? XFS_DQ_PROJ : XFS_DQ_GROUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418 error = 0;
1419
1420 /*
1421 * Blkcnt arg can be a very big number, and might even be
1422 * larger than the log itself. So, we have to break it up into
1423 * manageable-sized transactions.
1424 * Note that we don't start a permanent transaction here; we might
1425 * not be able to get a log reservation for the whole thing up front,
1426 * and we don't really care to either, because we just discard
1427 * everything if we were to crash in the middle of this loop.
1428 */
1429 while (blkcnt--) {
1430 error = xfs_trans_read_buf(mp, NULL, mp->m_ddev_targp,
1431 XFS_FSB_TO_DADDR(mp, bno),
1432 (int)XFS_QI_DQCHUNKLEN(mp), 0, &bp);
1433 if (error)
1434 break;
1435
David Chinner5b139732008-04-10 12:20:10 +10001436 xfs_qm_reset_dqcounts(mp, bp, firstid, type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437 xfs_bdwrite(mp, bp);
1438 /*
1439 * goto the next block.
1440 */
1441 bno++;
1442 firstid += XFS_QM_DQPERBLK(mp);
1443 }
Jesper Juhl014c2542006-01-15 02:37:08 +01001444 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445}
1446
1447/*
Nathan Scottc8ad20f2005-06-21 15:38:48 +10001448 * Iterate over all allocated USR/GRP/PRJ dquots in the system, calling a
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449 * caller supplied function for every chunk of dquots that we find.
1450 */
1451STATIC int
1452xfs_qm_dqiterate(
1453 xfs_mount_t *mp,
1454 xfs_inode_t *qip,
1455 uint flags)
1456{
1457 xfs_bmbt_irec_t *map;
1458 int i, nmaps; /* number of map entries */
1459 int error; /* return value */
1460 xfs_fileoff_t lblkno;
1461 xfs_filblks_t maxlblkcnt;
1462 xfs_dqid_t firstid;
1463 xfs_fsblock_t rablkno;
1464 xfs_filblks_t rablkcnt;
1465
1466 error = 0;
1467 /*
Nathan Scottc41564b2006-03-29 08:55:14 +10001468 * This looks racy, but we can't keep an inode lock across a
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 * trans_reserve. But, this gets called during quotacheck, and that
1470 * happens only at mount time which is single threaded.
1471 */
1472 if (qip->i_d.di_nblocks == 0)
Jesper Juhl014c2542006-01-15 02:37:08 +01001473 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474
1475 map = kmem_alloc(XFS_DQITER_MAP_SIZE * sizeof(*map), KM_SLEEP);
1476
1477 lblkno = 0;
1478 maxlblkcnt = XFS_B_TO_FSB(mp, (xfs_ufsize_t)XFS_MAXIOFFSET(mp));
1479 do {
1480 nmaps = XFS_DQITER_MAP_SIZE;
1481 /*
1482 * We aren't changing the inode itself. Just changing
1483 * some of its data. No new blocks are added here, and
1484 * the inode is never added to the transaction.
1485 */
1486 xfs_ilock(qip, XFS_ILOCK_SHARED);
1487 error = xfs_bmapi(NULL, qip, lblkno,
1488 maxlblkcnt - lblkno,
1489 XFS_BMAPI_METADATA,
1490 NULL,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001491 0, map, &nmaps, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492 xfs_iunlock(qip, XFS_ILOCK_SHARED);
1493 if (error)
1494 break;
1495
1496 ASSERT(nmaps <= XFS_DQITER_MAP_SIZE);
1497 for (i = 0; i < nmaps; i++) {
1498 ASSERT(map[i].br_startblock != DELAYSTARTBLOCK);
1499 ASSERT(map[i].br_blockcount);
1500
1501
1502 lblkno += map[i].br_blockcount;
1503
1504 if (map[i].br_startblock == HOLESTARTBLOCK)
1505 continue;
1506
1507 firstid = (xfs_dqid_t) map[i].br_startoff *
1508 XFS_QM_DQPERBLK(mp);
1509 /*
1510 * Do a read-ahead on the next extent.
1511 */
1512 if ((i+1 < nmaps) &&
1513 (map[i+1].br_startblock != HOLESTARTBLOCK)) {
1514 rablkcnt = map[i+1].br_blockcount;
1515 rablkno = map[i+1].br_startblock;
1516 while (rablkcnt--) {
1517 xfs_baread(mp->m_ddev_targp,
1518 XFS_FSB_TO_DADDR(mp, rablkno),
1519 (int)XFS_QI_DQCHUNKLEN(mp));
1520 rablkno++;
1521 }
1522 }
1523 /*
1524 * Iterate thru all the blks in the extent and
1525 * reset the counters of all the dquots inside them.
1526 */
1527 if ((error = xfs_qm_dqiter_bufs(mp,
1528 firstid,
1529 map[i].br_startblock,
1530 map[i].br_blockcount,
1531 flags))) {
1532 break;
1533 }
1534 }
1535
1536 if (error)
1537 break;
1538 } while (nmaps > 0);
1539
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10001540 kmem_free(map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541
Jesper Juhl014c2542006-01-15 02:37:08 +01001542 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543}
1544
1545/*
1546 * Called by dqusage_adjust in doing a quotacheck.
1547 * Given the inode, and a dquot (either USR or GRP, doesn't matter),
1548 * this updates its incore copy as well as the buffer copy. This is
1549 * so that once the quotacheck is done, we can just log all the buffers,
1550 * as opposed to logging numerous updates to individual dquots.
1551 */
1552STATIC void
1553xfs_qm_quotacheck_dqadjust(
1554 xfs_dquot_t *dqp,
1555 xfs_qcnt_t nblks,
1556 xfs_qcnt_t rtblks)
1557{
1558 ASSERT(XFS_DQ_IS_LOCKED(dqp));
1559 xfs_dqtrace_entry(dqp, "QCHECK DQADJUST");
1560 /*
1561 * Adjust the inode count and the block count to reflect this inode's
1562 * resource usage.
1563 */
Marcin Slusarz413d57c2008-02-13 15:03:29 -08001564 be64_add_cpu(&dqp->q_core.d_icount, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565 dqp->q_res_icount++;
1566 if (nblks) {
Marcin Slusarz413d57c2008-02-13 15:03:29 -08001567 be64_add_cpu(&dqp->q_core.d_bcount, nblks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568 dqp->q_res_bcount += nblks;
1569 }
1570 if (rtblks) {
Marcin Slusarz413d57c2008-02-13 15:03:29 -08001571 be64_add_cpu(&dqp->q_core.d_rtbcount, rtblks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572 dqp->q_res_rtbcount += rtblks;
1573 }
1574
1575 /*
1576 * Set default limits, adjust timers (since we changed usages)
1577 */
1578 if (! XFS_IS_SUSER_DQUOT(dqp)) {
1579 xfs_qm_adjust_dqlimits(dqp->q_mount, &dqp->q_core);
1580 xfs_qm_adjust_dqtimers(dqp->q_mount, &dqp->q_core);
1581 }
1582
1583 dqp->dq_flags |= XFS_DQ_DIRTY;
1584}
1585
1586STATIC int
1587xfs_qm_get_rtblks(
1588 xfs_inode_t *ip,
1589 xfs_qcnt_t *O_rtblks)
1590{
1591 xfs_filblks_t rtblks; /* total rt blks */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001592 xfs_extnum_t idx; /* extent record index */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593 xfs_ifork_t *ifp; /* inode fork pointer */
1594 xfs_extnum_t nextents; /* number of extent entries */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595 int error;
1596
1597 ASSERT(XFS_IS_REALTIME_INODE(ip));
1598 ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
1599 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
1600 if ((error = xfs_iread_extents(NULL, ip, XFS_DATA_FORK)))
Jesper Juhl014c2542006-01-15 02:37:08 +01001601 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602 }
1603 rtblks = 0;
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001604 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10001605 for (idx = 0; idx < nextents; idx++)
1606 rtblks += xfs_bmbt_get_blockcount(xfs_iext_get_ext(ifp, idx));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607 *O_rtblks = (xfs_qcnt_t)rtblks;
Jesper Juhl014c2542006-01-15 02:37:08 +01001608 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609}
1610
1611/*
1612 * callback routine supplied to bulkstat(). Given an inumber, find its
1613 * dquots and update them to account for resources taken by that inode.
1614 */
1615/* ARGSUSED */
1616STATIC int
1617xfs_qm_dqusage_adjust(
1618 xfs_mount_t *mp, /* mount point for filesystem */
1619 xfs_ino_t ino, /* inode number to get data for */
1620 void __user *buffer, /* not used */
1621 int ubsize, /* not used */
1622 void *private_data, /* not used */
1623 xfs_daddr_t bno, /* starting block of inode cluster */
1624 int *ubused, /* not used */
1625 void *dip, /* on-disk inode pointer (not used) */
1626 int *res) /* result code value */
1627{
1628 xfs_inode_t *ip;
1629 xfs_dquot_t *udqp, *gdqp;
1630 xfs_qcnt_t nblks, rtblks;
1631 int error;
1632
1633 ASSERT(XFS_IS_QUOTA_RUNNING(mp));
1634
1635 /*
1636 * rootino must have its resources accounted for, not so with the quota
1637 * inodes.
1638 */
1639 if (ino == mp->m_sb.sb_uquotino || ino == mp->m_sb.sb_gquotino) {
1640 *res = BULKSTAT_RV_NOTHING;
1641 return XFS_ERROR(EINVAL);
1642 }
1643
1644 /*
1645 * We don't _need_ to take the ilock EXCL. However, the xfs_qm_dqget
1646 * interface expects the inode to be exclusively locked because that's
1647 * the case in all other instances. It's OK that we do this because
1648 * quotacheck is done only at mount time.
1649 */
1650 if ((error = xfs_iget(mp, NULL, ino, 0, XFS_ILOCK_EXCL, &ip, bno))) {
1651 *res = BULKSTAT_RV_NOTHING;
Jesper Juhl014c2542006-01-15 02:37:08 +01001652 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653 }
1654
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655 /*
1656 * Obtain the locked dquots. In case of an error (eg. allocation
1657 * fails for ENOSPC), we return the negative of the error number
1658 * to bulkstat, so that it can get propagated to quotacheck() and
1659 * making us disable quotas for the file system.
1660 */
1661 if ((error = xfs_qm_dqget_noattach(ip, &udqp, &gdqp))) {
1662 xfs_iput(ip, XFS_ILOCK_EXCL);
1663 *res = BULKSTAT_RV_GIVEUP;
Jesper Juhl014c2542006-01-15 02:37:08 +01001664 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665 }
1666
1667 rtblks = 0;
1668 if (! XFS_IS_REALTIME_INODE(ip)) {
1669 nblks = (xfs_qcnt_t)ip->i_d.di_nblocks;
1670 } else {
1671 /*
1672 * Walk thru the extent list and count the realtime blocks.
1673 */
1674 if ((error = xfs_qm_get_rtblks(ip, &rtblks))) {
1675 xfs_iput(ip, XFS_ILOCK_EXCL);
1676 if (udqp)
1677 xfs_qm_dqput(udqp);
1678 if (gdqp)
1679 xfs_qm_dqput(gdqp);
1680 *res = BULKSTAT_RV_GIVEUP;
Jesper Juhl014c2542006-01-15 02:37:08 +01001681 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682 }
1683 nblks = (xfs_qcnt_t)ip->i_d.di_nblocks - rtblks;
1684 }
1685 ASSERT(ip->i_delayed_blks == 0);
1686
1687 /*
1688 * We can't release the inode while holding its dquot locks.
1689 * The inode can go into inactive and might try to acquire the dquotlocks.
1690 * So, just unlock here and do a vn_rele at the end.
1691 */
1692 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1693
1694 /*
1695 * Add the (disk blocks and inode) resources occupied by this
1696 * inode to its dquots. We do this adjustment in the incore dquot,
1697 * and also copy the changes to its buffer.
1698 * We don't care about putting these changes in a transaction
1699 * envelope because if we crash in the middle of a 'quotacheck'
1700 * we have to start from the beginning anyway.
1701 * Once we're done, we'll log all the dquot bufs.
1702 *
Nathan Scottc41564b2006-03-29 08:55:14 +10001703 * The *QUOTA_ON checks below may look pretty racy, but quotachecks
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704 * and quotaoffs don't race. (Quotachecks happen at mount time only).
1705 */
1706 if (XFS_IS_UQUOTA_ON(mp)) {
1707 ASSERT(udqp);
1708 xfs_qm_quotacheck_dqadjust(udqp, nblks, rtblks);
1709 xfs_qm_dqput(udqp);
1710 }
Nathan Scottc8ad20f2005-06-21 15:38:48 +10001711 if (XFS_IS_OQUOTA_ON(mp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712 ASSERT(gdqp);
1713 xfs_qm_quotacheck_dqadjust(gdqp, nblks, rtblks);
1714 xfs_qm_dqput(gdqp);
1715 }
1716 /*
1717 * Now release the inode. This will send it to 'inactive', and
1718 * possibly even free blocks.
1719 */
Christoph Hellwig43355092008-03-27 18:01:08 +11001720 IRELE(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721
1722 /*
1723 * Goto next inode.
1724 */
1725 *res = BULKSTAT_RV_DIDONE;
Jesper Juhl014c2542006-01-15 02:37:08 +01001726 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727}
1728
1729/*
1730 * Walk thru all the filesystem inodes and construct a consistent view
1731 * of the disk quota world. If the quotacheck fails, disable quotas.
1732 */
1733int
1734xfs_qm_quotacheck(
1735 xfs_mount_t *mp)
1736{
1737 int done, count, error;
1738 xfs_ino_t lastino;
1739 size_t structsz;
1740 xfs_inode_t *uip, *gip;
1741 uint flags;
1742
1743 count = INT_MAX;
1744 structsz = 1;
1745 lastino = 0;
1746 flags = 0;
1747
1748 ASSERT(XFS_QI_UQIP(mp) || XFS_QI_GQIP(mp));
1749 ASSERT(XFS_IS_QUOTA_RUNNING(mp));
1750
1751 /*
1752 * There should be no cached dquots. The (simplistic) quotacheck
1753 * algorithm doesn't like that.
1754 */
1755 ASSERT(XFS_QI_MPLNDQUOTS(mp) == 0);
1756
1757 cmn_err(CE_NOTE, "XFS quotacheck %s: Please wait.", mp->m_fsname);
1758
1759 /*
Nathan Scottc8ad20f2005-06-21 15:38:48 +10001760 * First we go thru all the dquots on disk, USR and GRP/PRJ, and reset
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761 * their counters to zero. We need a clean slate.
1762 * We don't log our changes till later.
1763 */
1764 if ((uip = XFS_QI_UQIP(mp))) {
1765 if ((error = xfs_qm_dqiterate(mp, uip, XFS_QMOPT_UQUOTA)))
1766 goto error_return;
1767 flags |= XFS_UQUOTA_CHKD;
1768 }
1769
1770 if ((gip = XFS_QI_GQIP(mp))) {
Nathan Scottc8ad20f2005-06-21 15:38:48 +10001771 if ((error = xfs_qm_dqiterate(mp, gip, XFS_IS_GQUOTA_ON(mp) ?
1772 XFS_QMOPT_GQUOTA : XFS_QMOPT_PQUOTA)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773 goto error_return;
Nathan Scottc8ad20f2005-06-21 15:38:48 +10001774 flags |= XFS_OQUOTA_CHKD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775 }
1776
1777 do {
1778 /*
1779 * Iterate thru all the inodes in the file system,
1780 * adjusting the corresponding dquot counters in core.
1781 */
1782 if ((error = xfs_bulkstat(mp, &lastino, &count,
1783 xfs_qm_dqusage_adjust, NULL,
Nathan Scott9c488762006-06-09 15:29:22 +10001784 structsz, NULL, BULKSTAT_FG_IGET, &done)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785 break;
1786
1787 } while (! done);
1788
1789 /*
David Chinner4b8879d2008-04-10 12:20:17 +10001790 * We've made all the changes that we need to make incore.
1791 * Flush them down to disk buffers if everything was updated
1792 * successfully.
1793 */
1794 if (!error)
1795 error = xfs_qm_dqflush_all(mp, XFS_QMOPT_DELWRI);
1796
1797 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798 * We can get this error if we couldn't do a dquot allocation inside
1799 * xfs_qm_dqusage_adjust (via bulkstat). We don't care about the
1800 * dirty dquots that might be cached, we just want to get rid of them
1801 * and turn quotaoff. The dquots won't be attached to any of the inodes
1802 * at this point (because we intentionally didn't in dqget_noattach).
1803 */
1804 if (error) {
Nathan Scottee2a4f72006-01-11 15:33:36 +11001805 xfs_qm_dqpurge_all(mp, XFS_QMOPT_QUOTALL | XFS_QMOPT_QUOTAOFF);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806 goto error_return;
1807 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808
1809 /*
1810 * We didn't log anything, because if we crashed, we'll have to
1811 * start the quotacheck from scratch anyway. However, we must make
1812 * sure that our dquot changes are secure before we put the
1813 * quotacheck'd stamp on the superblock. So, here we do a synchronous
1814 * flush.
1815 */
1816 XFS_bflush(mp->m_ddev_targp);
1817
1818 /*
1819 * If one type of quotas is off, then it will lose its
1820 * quotachecked status, since we won't be doing accounting for
1821 * that type anymore.
1822 */
Nathan Scottc8ad20f2005-06-21 15:38:48 +10001823 mp->m_qflags &= ~(XFS_OQUOTA_CHKD | XFS_UQUOTA_CHKD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824 mp->m_qflags |= flags;
1825
1826 XQM_LIST_PRINT(&(XFS_QI_MPL_LIST(mp)), MPL_NEXT, "++++ Mp list +++");
1827
1828 error_return:
1829 if (error) {
1830 cmn_err(CE_WARN, "XFS quotacheck %s: Unsuccessful (Error %d): "
1831 "Disabling quotas.",
1832 mp->m_fsname, error);
1833 /*
1834 * We must turn off quotas.
1835 */
1836 ASSERT(mp->m_quotainfo != NULL);
1837 ASSERT(xfs_Gqm != NULL);
1838 xfs_qm_destroy_quotainfo(mp);
David Chinner31d55772008-04-10 12:20:38 +10001839 if (xfs_mount_reset_sbqflags(mp)) {
1840 cmn_err(CE_WARN, "XFS quotacheck %s: "
1841 "Failed to reset quota flags.", mp->m_fsname);
1842 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843 } else {
1844 cmn_err(CE_NOTE, "XFS quotacheck %s: Done.", mp->m_fsname);
1845 }
1846 return (error);
1847}
1848
1849/*
1850 * This is called after the superblock has been read in and we're ready to
1851 * iget the quota inodes.
1852 */
1853STATIC int
1854xfs_qm_init_quotainos(
1855 xfs_mount_t *mp)
1856{
1857 xfs_inode_t *uip, *gip;
1858 int error;
1859 __int64_t sbflags;
1860 uint flags;
1861
1862 ASSERT(mp->m_quotainfo);
1863 uip = gip = NULL;
1864 sbflags = 0;
1865 flags = 0;
1866
1867 /*
1868 * Get the uquota and gquota inodes
1869 */
Eric Sandeen62118702008-03-06 13:44:28 +11001870 if (xfs_sb_version_hasquota(&mp->m_sb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871 if (XFS_IS_UQUOTA_ON(mp) &&
1872 mp->m_sb.sb_uquotino != NULLFSINO) {
1873 ASSERT(mp->m_sb.sb_uquotino > 0);
1874 if ((error = xfs_iget(mp, NULL, mp->m_sb.sb_uquotino,
1875 0, 0, &uip, 0)))
1876 return XFS_ERROR(error);
1877 }
Nathan Scottc8ad20f2005-06-21 15:38:48 +10001878 if (XFS_IS_OQUOTA_ON(mp) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879 mp->m_sb.sb_gquotino != NULLFSINO) {
1880 ASSERT(mp->m_sb.sb_gquotino > 0);
1881 if ((error = xfs_iget(mp, NULL, mp->m_sb.sb_gquotino,
1882 0, 0, &gip, 0))) {
1883 if (uip)
Christoph Hellwig43355092008-03-27 18:01:08 +11001884 IRELE(uip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885 return XFS_ERROR(error);
1886 }
1887 }
1888 } else {
1889 flags |= XFS_QMOPT_SBVERSION;
1890 sbflags |= (XFS_SB_VERSIONNUM | XFS_SB_UQUOTINO |
1891 XFS_SB_GQUOTINO | XFS_SB_QFLAGS);
1892 }
1893
1894 /*
1895 * Create the two inodes, if they don't exist already. The changes
1896 * made above will get added to a transaction and logged in one of
1897 * the qino_alloc calls below. If the device is readonly,
1898 * temporarily switch to read-write to do this.
1899 */
1900 if (XFS_IS_UQUOTA_ON(mp) && uip == NULL) {
1901 if ((error = xfs_qm_qino_alloc(mp, &uip,
1902 sbflags | XFS_SB_UQUOTINO,
1903 flags | XFS_QMOPT_UQUOTA)))
1904 return XFS_ERROR(error);
1905
1906 flags &= ~XFS_QMOPT_SBVERSION;
1907 }
Nathan Scottc8ad20f2005-06-21 15:38:48 +10001908 if (XFS_IS_OQUOTA_ON(mp) && gip == NULL) {
1909 flags |= (XFS_IS_GQUOTA_ON(mp) ?
1910 XFS_QMOPT_GQUOTA : XFS_QMOPT_PQUOTA);
1911 error = xfs_qm_qino_alloc(mp, &gip,
1912 sbflags | XFS_SB_GQUOTINO, flags);
1913 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914 if (uip)
Christoph Hellwig43355092008-03-27 18:01:08 +11001915 IRELE(uip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916
1917 return XFS_ERROR(error);
1918 }
1919 }
1920
1921 XFS_QI_UQIP(mp) = uip;
1922 XFS_QI_GQIP(mp) = gip;
1923
Jesper Juhl014c2542006-01-15 02:37:08 +01001924 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925}
1926
1927
1928/*
1929 * Traverse the freelist of dquots and attempt to reclaim a maximum of
1930 * 'howmany' dquots. This operation races with dqlookup(), and attempts to
1931 * favor the lookup function ...
1932 * XXXsup merge this with qm_reclaim_one().
1933 */
1934STATIC int
1935xfs_qm_shake_freelist(
1936 int howmany)
1937{
1938 int nreclaimed;
1939 xfs_dqhash_t *hash;
1940 xfs_dquot_t *dqp, *nextdqp;
1941 int restarts;
1942 int nflushes;
1943
1944 if (howmany <= 0)
Jesper Juhl014c2542006-01-15 02:37:08 +01001945 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946
1947 nreclaimed = 0;
1948 restarts = 0;
1949 nflushes = 0;
1950
1951#ifdef QUOTADEBUG
1952 cmn_err(CE_DEBUG, "Shake free 0x%x", howmany);
1953#endif
1954 /* lock order is : hashchainlock, freelistlock, mplistlock */
1955 tryagain:
1956 xfs_qm_freelist_lock(xfs_Gqm);
1957
1958 for (dqp = xfs_Gqm->qm_dqfreelist.qh_next;
1959 ((dqp != (xfs_dquot_t *) &xfs_Gqm->qm_dqfreelist) &&
1960 nreclaimed < howmany); ) {
1961 xfs_dqlock(dqp);
1962
1963 /*
1964 * We are racing with dqlookup here. Naturally we don't
1965 * want to reclaim a dquot that lookup wants.
1966 */
1967 if (dqp->dq_flags & XFS_DQ_WANT) {
1968 xfs_dqunlock(dqp);
1969 xfs_qm_freelist_unlock(xfs_Gqm);
1970 if (++restarts >= XFS_QM_RECLAIM_MAX_RESTARTS)
Jesper Juhl014c2542006-01-15 02:37:08 +01001971 return nreclaimed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972 XQM_STATS_INC(xqmstats.xs_qm_dqwants);
1973 goto tryagain;
1974 }
1975
1976 /*
1977 * If the dquot is inactive, we are assured that it is
1978 * not on the mplist or the hashlist, and that makes our
1979 * life easier.
1980 */
1981 if (dqp->dq_flags & XFS_DQ_INACTIVE) {
1982 ASSERT(dqp->q_mount == NULL);
1983 ASSERT(! XFS_DQ_IS_DIRTY(dqp));
1984 ASSERT(dqp->HL_PREVP == NULL);
1985 ASSERT(dqp->MPL_PREVP == NULL);
1986 XQM_STATS_INC(xqmstats.xs_qm_dqinact_reclaims);
1987 nextdqp = dqp->dq_flnext;
1988 goto off_freelist;
1989 }
1990
1991 ASSERT(dqp->MPL_PREVP);
1992 /*
1993 * Try to grab the flush lock. If this dquot is in the process of
1994 * getting flushed to disk, we don't want to reclaim it.
1995 */
David Chinnere1f49cf2008-08-13 16:41:43 +10001996 if (!xfs_dqflock_nowait(dqp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997 xfs_dqunlock(dqp);
1998 dqp = dqp->dq_flnext;
1999 continue;
2000 }
2001
2002 /*
2003 * We have the flush lock so we know that this is not in the
2004 * process of being flushed. So, if this is dirty, flush it
2005 * DELWRI so that we don't get a freelist infested with
2006 * dirty dquots.
2007 */
2008 if (XFS_DQ_IS_DIRTY(dqp)) {
David Chinner3c568362008-04-10 12:20:24 +10002009 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010 xfs_dqtrace_entry(dqp, "DQSHAKE: DQDIRTY");
2011 /*
2012 * We flush it delayed write, so don't bother
2013 * releasing the mplock.
2014 */
David Chinner3c568362008-04-10 12:20:24 +10002015 error = xfs_qm_dqflush(dqp, XFS_QMOPT_DELWRI);
2016 if (error) {
2017 xfs_fs_cmn_err(CE_WARN, dqp->q_mount,
2018 "xfs_qm_dqflush_all: dquot %p flush failed", dqp);
2019 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020 xfs_dqunlock(dqp); /* dqflush unlocks dqflock */
2021 dqp = dqp->dq_flnext;
2022 continue;
2023 }
2024 /*
2025 * We're trying to get the hashlock out of order. This races
2026 * with dqlookup; so, we giveup and goto the next dquot if
2027 * we couldn't get the hashlock. This way, we won't starve
2028 * a dqlookup process that holds the hashlock that is
2029 * waiting for the freelist lock.
2030 */
2031 if (! xfs_qm_dqhashlock_nowait(dqp)) {
2032 xfs_dqfunlock(dqp);
2033 xfs_dqunlock(dqp);
2034 dqp = dqp->dq_flnext;
2035 continue;
2036 }
2037 /*
2038 * This races with dquot allocation code as well as dqflush_all
2039 * and reclaim code. So, if we failed to grab the mplist lock,
2040 * giveup everything and start over.
2041 */
2042 hash = dqp->q_hash;
2043 ASSERT(hash);
2044 if (! xfs_qm_mplist_nowait(dqp->q_mount)) {
2045 /* XXX put a sentinel so that we can come back here */
2046 xfs_dqfunlock(dqp);
2047 xfs_dqunlock(dqp);
2048 XFS_DQ_HASH_UNLOCK(hash);
2049 xfs_qm_freelist_unlock(xfs_Gqm);
2050 if (++restarts >= XFS_QM_RECLAIM_MAX_RESTARTS)
Jesper Juhl014c2542006-01-15 02:37:08 +01002051 return nreclaimed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052 goto tryagain;
2053 }
2054 xfs_dqtrace_entry(dqp, "DQSHAKE: UNLINKING");
2055#ifdef QUOTADEBUG
2056 cmn_err(CE_DEBUG, "Shake 0x%p, ID 0x%x\n",
Christoph Hellwig1149d962005-11-02 15:01:12 +11002057 dqp, be32_to_cpu(dqp->q_core.d_id));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058#endif
2059 ASSERT(dqp->q_nrefs == 0);
2060 nextdqp = dqp->dq_flnext;
2061 XQM_MPLIST_REMOVE(&(XFS_QI_MPL_LIST(dqp->q_mount)), dqp);
2062 XQM_HASHLIST_REMOVE(hash, dqp);
2063 xfs_dqfunlock(dqp);
2064 xfs_qm_mplist_unlock(dqp->q_mount);
2065 XFS_DQ_HASH_UNLOCK(hash);
2066
2067 off_freelist:
2068 XQM_FREELIST_REMOVE(dqp);
2069 xfs_dqunlock(dqp);
2070 nreclaimed++;
2071 XQM_STATS_INC(xqmstats.xs_qm_dqshake_reclaims);
2072 xfs_qm_dqdestroy(dqp);
2073 dqp = nextdqp;
2074 }
2075 xfs_qm_freelist_unlock(xfs_Gqm);
Jesper Juhl014c2542006-01-15 02:37:08 +01002076 return nreclaimed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077}
2078
2079
2080/*
2081 * The kmem_shake interface is invoked when memory is running low.
2082 */
2083/* ARGSUSED */
2084STATIC int
Al Viro51bfb752005-12-15 09:17:24 +00002085xfs_qm_shake(int nr_to_scan, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086{
2087 int ndqused, nfree, n;
2088
2089 if (!kmem_shake_allow(gfp_mask))
Jesper Juhl014c2542006-01-15 02:37:08 +01002090 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091 if (!xfs_Gqm)
Jesper Juhl014c2542006-01-15 02:37:08 +01002092 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093
2094 nfree = xfs_Gqm->qm_dqfreelist.qh_nelems; /* free dquots */
2095 /* incore dquots in all f/s's */
2096 ndqused = atomic_read(&xfs_Gqm->qm_totaldquots) - nfree;
2097
2098 ASSERT(ndqused >= 0);
2099
2100 if (nfree <= ndqused && nfree < ndquot)
Jesper Juhl014c2542006-01-15 02:37:08 +01002101 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102
2103 ndqused *= xfs_Gqm->qm_dqfree_ratio; /* target # of free dquots */
2104 n = nfree - ndqused - ndquot; /* # over target */
2105
2106 return xfs_qm_shake_freelist(MAX(nfree, n));
2107}
2108
2109
2110/*
2111 * Just pop the least recently used dquot off the freelist and
2112 * recycle it. The returned dquot is locked.
2113 */
2114STATIC xfs_dquot_t *
2115xfs_qm_dqreclaim_one(void)
2116{
2117 xfs_dquot_t *dqpout;
2118 xfs_dquot_t *dqp;
2119 int restarts;
2120 int nflushes;
2121
2122 restarts = 0;
2123 dqpout = NULL;
2124 nflushes = 0;
2125
2126 /* lockorder: hashchainlock, freelistlock, mplistlock, dqlock, dqflock */
2127 startagain:
2128 xfs_qm_freelist_lock(xfs_Gqm);
2129
2130 FOREACH_DQUOT_IN_FREELIST(dqp, &(xfs_Gqm->qm_dqfreelist)) {
2131 xfs_dqlock(dqp);
2132
2133 /*
2134 * We are racing with dqlookup here. Naturally we don't
2135 * want to reclaim a dquot that lookup wants. We release the
2136 * freelist lock and start over, so that lookup will grab
2137 * both the dquot and the freelistlock.
2138 */
2139 if (dqp->dq_flags & XFS_DQ_WANT) {
2140 ASSERT(! (dqp->dq_flags & XFS_DQ_INACTIVE));
2141 xfs_dqtrace_entry(dqp, "DQRECLAIM: DQWANT");
2142 xfs_dqunlock(dqp);
2143 xfs_qm_freelist_unlock(xfs_Gqm);
2144 if (++restarts >= XFS_QM_RECLAIM_MAX_RESTARTS)
Jesper Juhl014c2542006-01-15 02:37:08 +01002145 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146 XQM_STATS_INC(xqmstats.xs_qm_dqwants);
2147 goto startagain;
2148 }
2149
2150 /*
2151 * If the dquot is inactive, we are assured that it is
2152 * not on the mplist or the hashlist, and that makes our
2153 * life easier.
2154 */
2155 if (dqp->dq_flags & XFS_DQ_INACTIVE) {
2156 ASSERT(dqp->q_mount == NULL);
2157 ASSERT(! XFS_DQ_IS_DIRTY(dqp));
2158 ASSERT(dqp->HL_PREVP == NULL);
2159 ASSERT(dqp->MPL_PREVP == NULL);
2160 XQM_FREELIST_REMOVE(dqp);
2161 xfs_dqunlock(dqp);
2162 dqpout = dqp;
2163 XQM_STATS_INC(xqmstats.xs_qm_dqinact_reclaims);
2164 break;
2165 }
2166
2167 ASSERT(dqp->q_hash);
2168 ASSERT(dqp->MPL_PREVP);
2169
2170 /*
2171 * Try to grab the flush lock. If this dquot is in the process of
2172 * getting flushed to disk, we don't want to reclaim it.
2173 */
David Chinnere1f49cf2008-08-13 16:41:43 +10002174 if (!xfs_dqflock_nowait(dqp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175 xfs_dqunlock(dqp);
2176 continue;
2177 }
2178
2179 /*
2180 * We have the flush lock so we know that this is not in the
2181 * process of being flushed. So, if this is dirty, flush it
2182 * DELWRI so that we don't get a freelist infested with
2183 * dirty dquots.
2184 */
2185 if (XFS_DQ_IS_DIRTY(dqp)) {
David Chinner3c568362008-04-10 12:20:24 +10002186 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187 xfs_dqtrace_entry(dqp, "DQRECLAIM: DQDIRTY");
2188 /*
2189 * We flush it delayed write, so don't bother
2190 * releasing the freelist lock.
2191 */
David Chinner3c568362008-04-10 12:20:24 +10002192 error = xfs_qm_dqflush(dqp, XFS_QMOPT_DELWRI);
2193 if (error) {
2194 xfs_fs_cmn_err(CE_WARN, dqp->q_mount,
2195 "xfs_qm_dqreclaim: dquot %p flush failed", dqp);
2196 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197 xfs_dqunlock(dqp); /* dqflush unlocks dqflock */
2198 continue;
2199 }
2200
2201 if (! xfs_qm_mplist_nowait(dqp->q_mount)) {
2202 xfs_dqfunlock(dqp);
2203 xfs_dqunlock(dqp);
2204 continue;
2205 }
2206
2207 if (! xfs_qm_dqhashlock_nowait(dqp))
2208 goto mplistunlock;
2209
2210 ASSERT(dqp->q_nrefs == 0);
2211 xfs_dqtrace_entry(dqp, "DQRECLAIM: UNLINKING");
2212 XQM_MPLIST_REMOVE(&(XFS_QI_MPL_LIST(dqp->q_mount)), dqp);
2213 XQM_HASHLIST_REMOVE(dqp->q_hash, dqp);
2214 XQM_FREELIST_REMOVE(dqp);
2215 dqpout = dqp;
2216 XFS_DQ_HASH_UNLOCK(dqp->q_hash);
2217 mplistunlock:
2218 xfs_qm_mplist_unlock(dqp->q_mount);
2219 xfs_dqfunlock(dqp);
2220 xfs_dqunlock(dqp);
2221 if (dqpout)
2222 break;
2223 }
2224
2225 xfs_qm_freelist_unlock(xfs_Gqm);
Jesper Juhl014c2542006-01-15 02:37:08 +01002226 return dqpout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227}
2228
2229
2230/*------------------------------------------------------------------*/
2231
2232/*
2233 * Return a new incore dquot. Depending on the number of
2234 * dquots in the system, we either allocate a new one on the kernel heap,
2235 * or reclaim a free one.
2236 * Return value is B_TRUE if we allocated a new dquot, B_FALSE if we managed
2237 * to reclaim an existing one from the freelist.
2238 */
2239boolean_t
2240xfs_qm_dqalloc_incore(
2241 xfs_dquot_t **O_dqpp)
2242{
2243 xfs_dquot_t *dqp;
2244
2245 /*
2246 * Check against high water mark to see if we want to pop
2247 * a nincompoop dquot off the freelist.
2248 */
2249 if (atomic_read(&xfs_Gqm->qm_totaldquots) >= ndquot) {
2250 /*
2251 * Try to recycle a dquot from the freelist.
2252 */
2253 if ((dqp = xfs_qm_dqreclaim_one())) {
2254 XQM_STATS_INC(xqmstats.xs_qm_dqreclaims);
2255 /*
2256 * Just zero the core here. The rest will get
2257 * reinitialized by caller. XXX we shouldn't even
2258 * do this zero ...
2259 */
2260 memset(&dqp->q_core, 0, sizeof(dqp->q_core));
2261 *O_dqpp = dqp;
Jesper Juhl014c2542006-01-15 02:37:08 +01002262 return B_FALSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263 }
2264 XQM_STATS_INC(xqmstats.xs_qm_dqreclaim_misses);
2265 }
2266
2267 /*
2268 * Allocate a brand new dquot on the kernel heap and return it
2269 * to the caller to initialize.
2270 */
2271 ASSERT(xfs_Gqm->qm_dqzone != NULL);
2272 *O_dqpp = kmem_zone_zalloc(xfs_Gqm->qm_dqzone, KM_SLEEP);
2273 atomic_inc(&xfs_Gqm->qm_totaldquots);
2274
Jesper Juhl014c2542006-01-15 02:37:08 +01002275 return B_TRUE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276}
2277
2278
2279/*
2280 * Start a transaction and write the incore superblock changes to
2281 * disk. flags parameter indicates which fields have changed.
2282 */
2283int
2284xfs_qm_write_sb_changes(
2285 xfs_mount_t *mp,
2286 __int64_t flags)
2287{
2288 xfs_trans_t *tp;
2289 int error;
2290
2291#ifdef QUOTADEBUG
2292 cmn_err(CE_NOTE, "Writing superblock quota changes :%s", mp->m_fsname);
2293#endif
2294 tp = xfs_trans_alloc(mp, XFS_TRANS_QM_SBCHANGE);
2295 if ((error = xfs_trans_reserve(tp, 0,
2296 mp->m_sb.sb_sectsize + 128, 0,
2297 0,
2298 XFS_DEFAULT_LOG_COUNT))) {
2299 xfs_trans_cancel(tp, 0);
Jesper Juhl014c2542006-01-15 02:37:08 +01002300 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301 }
2302
2303 xfs_mod_sb(tp, flags);
David Chinnere5720ee2008-04-10 12:21:18 +10002304 error = xfs_trans_commit(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305
David Chinnere5720ee2008-04-10 12:21:18 +10002306 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307}
2308
2309
2310/* --------------- utility functions for vnodeops ---------------- */
2311
2312
2313/*
2314 * Given an inode, a uid and gid (from cred_t) make sure that we have
2315 * allocated relevant dquot(s) on disk, and that we won't exceed inode
2316 * quotas by creating this file.
2317 * This also attaches dquot(s) to the given inode after locking it,
2318 * and returns the dquots corresponding to the uid and/or gid.
2319 *
2320 * in : inode (unlocked)
2321 * out : udquot, gdquot with references taken and unlocked
2322 */
2323int
2324xfs_qm_vop_dqalloc(
2325 xfs_mount_t *mp,
2326 xfs_inode_t *ip,
2327 uid_t uid,
2328 gid_t gid,
Nathan Scottc8ad20f2005-06-21 15:38:48 +10002329 prid_t prid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330 uint flags,
2331 xfs_dquot_t **O_udqpp,
2332 xfs_dquot_t **O_gdqpp)
2333{
2334 int error;
2335 xfs_dquot_t *uq, *gq;
2336 uint lockflags;
2337
2338 if (!XFS_IS_QUOTA_ON(mp))
2339 return 0;
2340
2341 lockflags = XFS_ILOCK_EXCL;
2342 xfs_ilock(ip, lockflags);
2343
Christoph Hellwigbd186aa2007-08-30 17:21:12 +10002344 if ((flags & XFS_QMOPT_INHERIT) && XFS_INHERIT_GID(ip))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345 gid = ip->i_d.di_gid;
2346
2347 /*
2348 * Attach the dquot(s) to this inode, doing a dquot allocation
2349 * if necessary. The dquot(s) will not be locked.
2350 */
2351 if (XFS_NOT_DQATTACHED(mp, ip)) {
2352 if ((error = xfs_qm_dqattach(ip, XFS_QMOPT_DQALLOC |
2353 XFS_QMOPT_ILOCKED))) {
2354 xfs_iunlock(ip, lockflags);
Jesper Juhl014c2542006-01-15 02:37:08 +01002355 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002356 }
2357 }
2358
2359 uq = gq = NULL;
Nathan Scottc8ad20f2005-06-21 15:38:48 +10002360 if ((flags & XFS_QMOPT_UQUOTA) && XFS_IS_UQUOTA_ON(mp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361 if (ip->i_d.di_uid != uid) {
2362 /*
2363 * What we need is the dquot that has this uid, and
2364 * if we send the inode to dqget, the uid of the inode
2365 * takes priority over what's sent in the uid argument.
2366 * We must unlock inode here before calling dqget if
2367 * we're not sending the inode, because otherwise
2368 * we'll deadlock by doing trans_reserve while
2369 * holding ilock.
2370 */
2371 xfs_iunlock(ip, lockflags);
2372 if ((error = xfs_qm_dqget(mp, NULL, (xfs_dqid_t) uid,
2373 XFS_DQ_USER,
2374 XFS_QMOPT_DQALLOC |
2375 XFS_QMOPT_DOWARN,
2376 &uq))) {
2377 ASSERT(error != ENOENT);
Jesper Juhl014c2542006-01-15 02:37:08 +01002378 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002379 }
2380 /*
2381 * Get the ilock in the right order.
2382 */
2383 xfs_dqunlock(uq);
2384 lockflags = XFS_ILOCK_SHARED;
2385 xfs_ilock(ip, lockflags);
2386 } else {
2387 /*
2388 * Take an extra reference, because we'll return
2389 * this to caller
2390 */
2391 ASSERT(ip->i_udquot);
2392 uq = ip->i_udquot;
2393 xfs_dqlock(uq);
2394 XFS_DQHOLD(uq);
2395 xfs_dqunlock(uq);
2396 }
2397 }
Nathan Scottc8ad20f2005-06-21 15:38:48 +10002398 if ((flags & XFS_QMOPT_GQUOTA) && XFS_IS_GQUOTA_ON(mp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002399 if (ip->i_d.di_gid != gid) {
2400 xfs_iunlock(ip, lockflags);
2401 if ((error = xfs_qm_dqget(mp, NULL, (xfs_dqid_t)gid,
2402 XFS_DQ_GROUP,
2403 XFS_QMOPT_DQALLOC |
2404 XFS_QMOPT_DOWARN,
2405 &gq))) {
2406 if (uq)
2407 xfs_qm_dqrele(uq);
2408 ASSERT(error != ENOENT);
Jesper Juhl014c2542006-01-15 02:37:08 +01002409 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002410 }
2411 xfs_dqunlock(gq);
2412 lockflags = XFS_ILOCK_SHARED;
2413 xfs_ilock(ip, lockflags);
2414 } else {
2415 ASSERT(ip->i_gdquot);
2416 gq = ip->i_gdquot;
2417 xfs_dqlock(gq);
2418 XFS_DQHOLD(gq);
2419 xfs_dqunlock(gq);
2420 }
Nathan Scottc8ad20f2005-06-21 15:38:48 +10002421 } else if ((flags & XFS_QMOPT_PQUOTA) && XFS_IS_PQUOTA_ON(mp)) {
2422 if (ip->i_d.di_projid != prid) {
2423 xfs_iunlock(ip, lockflags);
2424 if ((error = xfs_qm_dqget(mp, NULL, (xfs_dqid_t)prid,
2425 XFS_DQ_PROJ,
2426 XFS_QMOPT_DQALLOC |
2427 XFS_QMOPT_DOWARN,
2428 &gq))) {
2429 if (uq)
2430 xfs_qm_dqrele(uq);
2431 ASSERT(error != ENOENT);
2432 return (error);
2433 }
2434 xfs_dqunlock(gq);
2435 lockflags = XFS_ILOCK_SHARED;
2436 xfs_ilock(ip, lockflags);
2437 } else {
2438 ASSERT(ip->i_gdquot);
2439 gq = ip->i_gdquot;
2440 xfs_dqlock(gq);
2441 XFS_DQHOLD(gq);
2442 xfs_dqunlock(gq);
2443 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444 }
2445 if (uq)
2446 xfs_dqtrace_entry_ino(uq, "DQALLOC", ip);
2447
2448 xfs_iunlock(ip, lockflags);
2449 if (O_udqpp)
2450 *O_udqpp = uq;
2451 else if (uq)
2452 xfs_qm_dqrele(uq);
2453 if (O_gdqpp)
2454 *O_gdqpp = gq;
2455 else if (gq)
2456 xfs_qm_dqrele(gq);
Jesper Juhl014c2542006-01-15 02:37:08 +01002457 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458}
2459
2460/*
2461 * Actually transfer ownership, and do dquot modifications.
2462 * These were already reserved.
2463 */
2464xfs_dquot_t *
2465xfs_qm_vop_chown(
2466 xfs_trans_t *tp,
2467 xfs_inode_t *ip,
2468 xfs_dquot_t **IO_olddq,
2469 xfs_dquot_t *newdq)
2470{
2471 xfs_dquot_t *prevdq;
Nathan Scott06d10dd2005-06-21 15:48:47 +10002472 uint bfield = XFS_IS_REALTIME_INODE(ip) ?
2473 XFS_TRANS_DQ_RTBCOUNT : XFS_TRANS_DQ_BCOUNT;
2474
Christoph Hellwig579aa9c2008-04-22 17:34:00 +10002475 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002476 ASSERT(XFS_IS_QUOTA_RUNNING(ip->i_mount));
2477
2478 /* old dquot */
2479 prevdq = *IO_olddq;
2480 ASSERT(prevdq);
2481 ASSERT(prevdq != newdq);
2482
Nathan Scott06d10dd2005-06-21 15:48:47 +10002483 xfs_trans_mod_dquot(tp, prevdq, bfield, -(ip->i_d.di_nblocks));
2484 xfs_trans_mod_dquot(tp, prevdq, XFS_TRANS_DQ_ICOUNT, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002485
2486 /* the sparkling new dquot */
Nathan Scott06d10dd2005-06-21 15:48:47 +10002487 xfs_trans_mod_dquot(tp, newdq, bfield, ip->i_d.di_nblocks);
2488 xfs_trans_mod_dquot(tp, newdq, XFS_TRANS_DQ_ICOUNT, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002489
2490 /*
2491 * Take an extra reference, because the inode
2492 * is going to keep this dquot pointer even
2493 * after the trans_commit.
2494 */
2495 xfs_dqlock(newdq);
2496 XFS_DQHOLD(newdq);
2497 xfs_dqunlock(newdq);
2498 *IO_olddq = newdq;
2499
Jesper Juhl014c2542006-01-15 02:37:08 +01002500 return prevdq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002501}
2502
2503/*
Nathan Scottc8ad20f2005-06-21 15:38:48 +10002504 * Quota reservations for setattr(AT_UID|AT_GID|AT_PROJID).
Linus Torvalds1da177e2005-04-16 15:20:36 -07002505 */
2506int
2507xfs_qm_vop_chown_reserve(
2508 xfs_trans_t *tp,
2509 xfs_inode_t *ip,
2510 xfs_dquot_t *udqp,
2511 xfs_dquot_t *gdqp,
2512 uint flags)
2513{
2514 int error;
2515 xfs_mount_t *mp;
Nathan Scott9a2a7de2006-03-31 13:04:49 +10002516 uint delblks, blkflags, prjflags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002517 xfs_dquot_t *unresudq, *unresgdq, *delblksudq, *delblksgdq;
2518
Christoph Hellwig579aa9c2008-04-22 17:34:00 +10002519 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_ILOCK_SHARED));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002520 mp = ip->i_mount;
2521 ASSERT(XFS_IS_QUOTA_RUNNING(mp));
2522
2523 delblks = ip->i_delayed_blks;
2524 delblksudq = delblksgdq = unresudq = unresgdq = NULL;
Nathan Scott06d10dd2005-06-21 15:48:47 +10002525 blkflags = XFS_IS_REALTIME_INODE(ip) ?
2526 XFS_QMOPT_RES_RTBLKS : XFS_QMOPT_RES_REGBLKS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002527
2528 if (XFS_IS_UQUOTA_ON(mp) && udqp &&
Christoph Hellwig1149d962005-11-02 15:01:12 +11002529 ip->i_d.di_uid != (uid_t)be32_to_cpu(udqp->q_core.d_id)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530 delblksudq = udqp;
2531 /*
2532 * If there are delayed allocation blocks, then we have to
2533 * unreserve those from the old dquot, and add them to the
2534 * new dquot.
2535 */
2536 if (delblks) {
2537 ASSERT(ip->i_udquot);
2538 unresudq = ip->i_udquot;
2539 }
2540 }
Nathan Scottc8ad20f2005-06-21 15:38:48 +10002541 if (XFS_IS_OQUOTA_ON(ip->i_mount) && gdqp) {
Nathan Scott9a2a7de2006-03-31 13:04:49 +10002542 if (XFS_IS_PQUOTA_ON(ip->i_mount) &&
2543 ip->i_d.di_projid != be32_to_cpu(gdqp->q_core.d_id))
2544 prjflags = XFS_QMOPT_ENOSPC;
2545
2546 if (prjflags ||
2547 (XFS_IS_GQUOTA_ON(ip->i_mount) &&
2548 ip->i_d.di_gid != be32_to_cpu(gdqp->q_core.d_id))) {
Nathan Scottc8ad20f2005-06-21 15:38:48 +10002549 delblksgdq = gdqp;
2550 if (delblks) {
2551 ASSERT(ip->i_gdquot);
2552 unresgdq = ip->i_gdquot;
2553 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002554 }
2555 }
2556
2557 if ((error = xfs_trans_reserve_quota_bydquots(tp, ip->i_mount,
2558 delblksudq, delblksgdq, ip->i_d.di_nblocks, 1,
Nathan Scott9a2a7de2006-03-31 13:04:49 +10002559 flags | blkflags | prjflags)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002560 return (error);
2561
2562 /*
2563 * Do the delayed blks reservations/unreservations now. Since, these
2564 * are done without the help of a transaction, if a reservation fails
2565 * its previous reservations won't be automatically undone by trans
2566 * code. So, we have to do it manually here.
2567 */
2568 if (delblks) {
2569 /*
2570 * Do the reservations first. Unreservation can't fail.
2571 */
2572 ASSERT(delblksudq || delblksgdq);
2573 ASSERT(unresudq || unresgdq);
2574 if ((error = xfs_trans_reserve_quota_bydquots(NULL, ip->i_mount,
2575 delblksudq, delblksgdq, (xfs_qcnt_t)delblks, 0,
Nathan Scott9a2a7de2006-03-31 13:04:49 +10002576 flags | blkflags | prjflags)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002577 return (error);
2578 xfs_trans_reserve_quota_bydquots(NULL, ip->i_mount,
2579 unresudq, unresgdq, -((xfs_qcnt_t)delblks), 0,
Nathan Scott06d10dd2005-06-21 15:48:47 +10002580 blkflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002581 }
2582
2583 return (0);
2584}
2585
2586int
2587xfs_qm_vop_rename_dqattach(
2588 xfs_inode_t **i_tab)
2589{
2590 xfs_inode_t *ip;
2591 int i;
2592 int error;
2593
2594 ip = i_tab[0];
2595
2596 if (! XFS_IS_QUOTA_ON(ip->i_mount))
Jesper Juhl014c2542006-01-15 02:37:08 +01002597 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002598
2599 if (XFS_NOT_DQATTACHED(ip->i_mount, ip)) {
2600 error = xfs_qm_dqattach(ip, 0);
2601 if (error)
Jesper Juhl014c2542006-01-15 02:37:08 +01002602 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002603 }
2604 for (i = 1; (i < 4 && i_tab[i]); i++) {
2605 /*
2606 * Watch out for duplicate entries in the table.
2607 */
2608 if ((ip = i_tab[i]) != i_tab[i-1]) {
2609 if (XFS_NOT_DQATTACHED(ip->i_mount, ip)) {
2610 error = xfs_qm_dqattach(ip, 0);
2611 if (error)
Jesper Juhl014c2542006-01-15 02:37:08 +01002612 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002613 }
2614 }
2615 }
Jesper Juhl014c2542006-01-15 02:37:08 +01002616 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002617}
2618
2619void
2620xfs_qm_vop_dqattach_and_dqmod_newinode(
2621 xfs_trans_t *tp,
2622 xfs_inode_t *ip,
2623 xfs_dquot_t *udqp,
2624 xfs_dquot_t *gdqp)
2625{
2626 if (!XFS_IS_QUOTA_ON(tp->t_mountp))
2627 return;
2628
Christoph Hellwig579aa9c2008-04-22 17:34:00 +10002629 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002630 ASSERT(XFS_IS_QUOTA_RUNNING(tp->t_mountp));
2631
2632 if (udqp) {
2633 xfs_dqlock(udqp);
2634 XFS_DQHOLD(udqp);
2635 xfs_dqunlock(udqp);
2636 ASSERT(ip->i_udquot == NULL);
2637 ip->i_udquot = udqp;
Nathan Scottee2a4f72006-01-11 15:33:36 +11002638 ASSERT(XFS_IS_UQUOTA_ON(tp->t_mountp));
Christoph Hellwig1149d962005-11-02 15:01:12 +11002639 ASSERT(ip->i_d.di_uid == be32_to_cpu(udqp->q_core.d_id));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002640 xfs_trans_mod_dquot(tp, udqp, XFS_TRANS_DQ_ICOUNT, 1);
2641 }
2642 if (gdqp) {
2643 xfs_dqlock(gdqp);
2644 XFS_DQHOLD(gdqp);
2645 xfs_dqunlock(gdqp);
2646 ASSERT(ip->i_gdquot == NULL);
2647 ip->i_gdquot = gdqp;
Nathan Scottee2a4f72006-01-11 15:33:36 +11002648 ASSERT(XFS_IS_OQUOTA_ON(tp->t_mountp));
2649 ASSERT((XFS_IS_GQUOTA_ON(tp->t_mountp) ?
2650 ip->i_d.di_gid : ip->i_d.di_projid) ==
2651 be32_to_cpu(gdqp->q_core.d_id));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002652 xfs_trans_mod_dquot(tp, gdqp, XFS_TRANS_DQ_ICOUNT, 1);
2653 }
2654}
2655
2656/* ------------- list stuff -----------------*/
Christoph Hellwigba0f32d2005-06-21 15:36:52 +10002657STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -07002658xfs_qm_freelist_init(xfs_frlist_t *ql)
2659{
2660 ql->qh_next = ql->qh_prev = (xfs_dquot_t *) ql;
Jes Sorensen794ee1b2006-01-09 15:59:21 -08002661 mutex_init(&ql->qh_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002662 ql->qh_version = 0;
2663 ql->qh_nelems = 0;
2664}
2665
Christoph Hellwigba0f32d2005-06-21 15:36:52 +10002666STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -07002667xfs_qm_freelist_destroy(xfs_frlist_t *ql)
2668{
2669 xfs_dquot_t *dqp, *nextdqp;
2670
Jes Sorensen794ee1b2006-01-09 15:59:21 -08002671 mutex_lock(&ql->qh_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002672 for (dqp = ql->qh_next;
2673 dqp != (xfs_dquot_t *)ql; ) {
2674 xfs_dqlock(dqp);
2675 nextdqp = dqp->dq_flnext;
2676#ifdef QUOTADEBUG
2677 cmn_err(CE_DEBUG, "FREELIST destroy 0x%p", dqp);
2678#endif
2679 XQM_FREELIST_REMOVE(dqp);
2680 xfs_dqunlock(dqp);
2681 xfs_qm_dqdestroy(dqp);
2682 dqp = nextdqp;
2683 }
Nathan Scott20722a92006-03-14 13:19:08 +11002684 mutex_unlock(&ql->qh_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002685 mutex_destroy(&ql->qh_lock);
2686
2687 ASSERT(ql->qh_nelems == 0);
2688}
2689
Christoph Hellwigba0f32d2005-06-21 15:36:52 +10002690STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -07002691xfs_qm_freelist_insert(xfs_frlist_t *ql, xfs_dquot_t *dq)
2692{
2693 dq->dq_flnext = ql->qh_next;
2694 dq->dq_flprev = (xfs_dquot_t *)ql;
2695 ql->qh_next = dq;
2696 dq->dq_flnext->dq_flprev = dq;
2697 xfs_Gqm->qm_dqfreelist.qh_nelems++;
2698 xfs_Gqm->qm_dqfreelist.qh_version++;
2699}
2700
2701void
2702xfs_qm_freelist_unlink(xfs_dquot_t *dq)
2703{
2704 xfs_dquot_t *next = dq->dq_flnext;
2705 xfs_dquot_t *prev = dq->dq_flprev;
2706
2707 next->dq_flprev = prev;
2708 prev->dq_flnext = next;
2709 dq->dq_flnext = dq->dq_flprev = dq;
2710 xfs_Gqm->qm_dqfreelist.qh_nelems--;
2711 xfs_Gqm->qm_dqfreelist.qh_version++;
2712}
2713
2714void
2715xfs_qm_freelist_append(xfs_frlist_t *ql, xfs_dquot_t *dq)
2716{
2717 xfs_qm_freelist_insert((xfs_frlist_t *)ql->qh_prev, dq);
2718}
2719
Christoph Hellwigba0f32d2005-06-21 15:36:52 +10002720STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -07002721xfs_qm_dqhashlock_nowait(
2722 xfs_dquot_t *dqp)
2723{
2724 int locked;
2725
2726 locked = mutex_trylock(&((dqp)->q_hash->qh_lock));
Jesper Juhl014c2542006-01-15 02:37:08 +01002727 return locked;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002728}
2729
2730int
2731xfs_qm_freelist_lock_nowait(
2732 xfs_qm_t *xqm)
2733{
2734 int locked;
2735
2736 locked = mutex_trylock(&(xqm->qm_dqfreelist.qh_lock));
Jesper Juhl014c2542006-01-15 02:37:08 +01002737 return locked;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002738}
2739
Christoph Hellwigba0f32d2005-06-21 15:36:52 +10002740STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -07002741xfs_qm_mplist_nowait(
2742 xfs_mount_t *mp)
2743{
2744 int locked;
2745
2746 ASSERT(mp->m_quotainfo);
2747 locked = mutex_trylock(&(XFS_QI_MPLLOCK(mp)));
Jesper Juhl014c2542006-01-15 02:37:08 +01002748 return locked;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002749}