blob: 417e61e3d9dda0f18434498e51252fd08eca0473 [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"
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include "xfs_attr.h"
46#include "xfs_buf_item.h"
47#include "xfs_trans_space.h"
48#include "xfs_utils.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070049#include "xfs_qm.h"
Christoph Hellwig0b1b2132009-12-14 23:14:59 +000050#include "xfs_trace.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
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 */
Christoph Hellwiga0b0b8a2009-03-29 09:51:00 +020058struct mutex 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 *);
Christoph Hellwigba0f32d2005-06-21 15:36:52 +100072
Linus Torvalds1da177e2005-04-16 15:20:36 -070073STATIC int xfs_qm_init_quotainos(xfs_mount_t *);
Christoph Hellwigba0f32d2005-06-21 15:36:52 +100074STATIC int xfs_qm_init_quotainfo(xfs_mount_t *);
Al Viro51bfb752005-12-15 09:17:24 +000075STATIC int xfs_qm_shake(int, gfp_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -070076
Rusty Russell8e1f9362007-07-17 04:03:17 -070077static struct shrinker xfs_qm_shaker = {
78 .shrink = xfs_qm_shake,
79 .seeks = DEFAULT_SEEKS,
80};
81
Linus Torvalds1da177e2005-04-16 15:20:36 -070082#ifdef DEBUG
Christoph Hellwiga0b0b8a2009-03-29 09:51:00 +020083extern struct mutex qcheck_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -070084#endif
85
86#ifdef QUOTADEBUG
87#define XQM_LIST_PRINT(l, NXT, title) \
88{ \
89 xfs_dquot_t *dqp; int i = 0; \
90 cmn_err(CE_DEBUG, "%s (#%d)", title, (int) (l)->qh_nelems); \
91 for (dqp = (l)->qh_next; dqp != NULL; dqp = dqp->NXT) { \
92 cmn_err(CE_DEBUG, " %d. \"%d (%s)\" " \
93 "bcnt = %d, icnt = %d, refs = %d", \
Christoph Hellwig1149d962005-11-02 15:01:12 +110094 ++i, (int) be32_to_cpu(dqp->q_core.d_id), \
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 DQFLAGTO_TYPESTR(dqp), \
Christoph Hellwig1149d962005-11-02 15:01:12 +110096 (int) be64_to_cpu(dqp->q_core.d_bcount), \
97 (int) be64_to_cpu(dqp->q_core.d_icount), \
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 (int) dqp->q_nrefs); } \
99}
100#else
101#define XQM_LIST_PRINT(l, NXT, title) do { } while (0)
102#endif
103
104/*
105 * Initialize the XQM structure.
106 * Note that there is not one quota manager per file system.
107 */
108STATIC struct xfs_qm *
109xfs_Gqm_init(void)
110{
Nathan Scott6b3f6b52005-11-02 15:08:25 +1100111 xfs_dqhash_t *udqhash, *gdqhash;
112 xfs_qm_t *xqm;
Nathan Scott215101c2006-09-28 11:04:43 +1000113 size_t hsize;
114 uint i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115
116 /*
117 * Initialize the dquot hash tables.
118 */
Nathan Scott77e46352006-09-28 11:03:27 +1000119 udqhash = kmem_zalloc_greedy(&hsize,
Eric Sandeen5995cb72007-08-16 16:49:11 +1000120 XFS_QM_HASHSIZE_LOW * sizeof(xfs_dqhash_t),
Christoph Hellwigbdfb0432010-01-20 21:55:30 +0000121 XFS_QM_HASHSIZE_HIGH * sizeof(xfs_dqhash_t));
122 if (!udqhash)
123 goto out;
124
125 gdqhash = kmem_zalloc_large(hsize);
Julia Lawalld67b1b02010-02-06 08:45:15 +0000126 if (!gdqhash)
Christoph Hellwigbdfb0432010-01-20 21:55:30 +0000127 goto out_free_udqhash;
128
Nathan Scott77e46352006-09-28 11:03:27 +1000129 hsize /= sizeof(xfs_dqhash_t);
Nathan Scott6b3f6b52005-11-02 15:08:25 +1100130 ndquot = hsize << 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131
Nathan Scott6b3f6b52005-11-02 15:08:25 +1100132 xqm = kmem_zalloc(sizeof(xfs_qm_t), KM_SLEEP);
133 xqm->qm_dqhashmask = hsize - 1;
134 xqm->qm_usr_dqhtable = udqhash;
135 xqm->qm_grp_dqhtable = gdqhash;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 ASSERT(xqm->qm_usr_dqhtable != NULL);
137 ASSERT(xqm->qm_grp_dqhtable != NULL);
138
139 for (i = 0; i < hsize; i++) {
140 xfs_qm_list_init(&(xqm->qm_usr_dqhtable[i]), "uxdqh", i);
141 xfs_qm_list_init(&(xqm->qm_grp_dqhtable[i]), "gxdqh", i);
142 }
143
144 /*
145 * Freelist of all dquots of all file systems
146 */
147 xfs_qm_freelist_init(&(xqm->qm_dqfreelist));
148
149 /*
150 * dquot zone. we register our own low-memory callback.
151 */
152 if (!qm_dqzone) {
153 xqm->qm_dqzone = kmem_zone_init(sizeof(xfs_dquot_t),
154 "xfs_dquots");
155 qm_dqzone = xqm->qm_dqzone;
156 } else
157 xqm->qm_dqzone = qm_dqzone;
158
Rusty Russell8e1f9362007-07-17 04:03:17 -0700159 register_shrinker(&xfs_qm_shaker);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160
161 /*
162 * The t_dqinfo portion of transactions.
163 */
164 if (!qm_dqtrxzone) {
165 xqm->qm_dqtrxzone = kmem_zone_init(sizeof(xfs_dquot_acct_t),
166 "xfs_dqtrx");
167 qm_dqtrxzone = xqm->qm_dqtrxzone;
168 } else
169 xqm->qm_dqtrxzone = qm_dqtrxzone;
170
171 atomic_set(&xqm->qm_totaldquots, 0);
172 xqm->qm_dqfree_ratio = XFS_QM_DQFREE_RATIO;
173 xqm->qm_nrefs = 0;
174#ifdef DEBUG
Nathan Scottc2e81432006-01-16 16:21:34 +1100175 mutex_init(&qcheck_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176#endif
177 return xqm;
Christoph Hellwigbdfb0432010-01-20 21:55:30 +0000178
179 out_free_udqhash:
180 kmem_free_large(udqhash);
181 out:
182 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183}
184
185/*
186 * Destroy the global quota manager when its reference count goes to zero.
187 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +1000188STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189xfs_qm_destroy(
190 struct xfs_qm *xqm)
191{
192 int hsize, i;
193
194 ASSERT(xqm != NULL);
195 ASSERT(xqm->qm_nrefs == 0);
Rusty Russell8e1f9362007-07-17 04:03:17 -0700196 unregister_shrinker(&xfs_qm_shaker);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 hsize = xqm->qm_dqhashmask + 1;
198 for (i = 0; i < hsize; i++) {
199 xfs_qm_list_destroy(&(xqm->qm_usr_dqhtable[i]));
200 xfs_qm_list_destroy(&(xqm->qm_grp_dqhtable[i]));
201 }
Christoph Hellwigbdfb0432010-01-20 21:55:30 +0000202 kmem_free_large(xqm->qm_usr_dqhtable);
203 kmem_free_large(xqm->qm_grp_dqhtable);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 xqm->qm_usr_dqhtable = NULL;
205 xqm->qm_grp_dqhtable = NULL;
206 xqm->qm_dqhashmask = 0;
207 xfs_qm_freelist_destroy(&(xqm->qm_dqfreelist));
208#ifdef DEBUG
209 mutex_destroy(&qcheck_lock);
210#endif
Denys Vlasenkof0e2d932008-05-19 16:31:57 +1000211 kmem_free(xqm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212}
213
214/*
215 * Called at mount time to let XQM know that another file system is
216 * starting quotas. This isn't crucial information as the individual mount
217 * structures are pretty independent, but it helps the XQM keep a
218 * global view of what's going on.
219 */
220/* ARGSUSED */
221STATIC int
222xfs_qm_hold_quotafs_ref(
223 struct xfs_mount *mp)
224{
225 /*
226 * Need to lock the xfs_Gqm structure for things like this. For example,
227 * the structure could disappear between the entry to this routine and
228 * a HOLD operation if not locked.
229 */
Christoph Hellwige2494582009-02-09 08:38:39 +0100230 mutex_lock(&xfs_Gqm_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231
Christoph Hellwigbdfb0432010-01-20 21:55:30 +0000232 if (!xfs_Gqm) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 xfs_Gqm = xfs_Gqm_init();
Christoph Hellwigbdfb0432010-01-20 21:55:30 +0000234 if (!xfs_Gqm)
235 return ENOMEM;
236 }
237
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 /*
239 * We can keep a list of all filesystems with quotas mounted for
240 * debugging and statistical purposes, but ...
241 * Just take a reference and get out.
242 */
Christoph Hellwige2494582009-02-09 08:38:39 +0100243 xfs_Gqm->qm_nrefs++;
244 mutex_unlock(&xfs_Gqm_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245
246 return 0;
247}
248
249
250/*
251 * Release the reference that a filesystem took at mount time,
252 * so that we know when we need to destroy the entire quota manager.
253 */
254/* ARGSUSED */
255STATIC void
256xfs_qm_rele_quotafs_ref(
257 struct xfs_mount *mp)
258{
259 xfs_dquot_t *dqp, *nextdqp;
260
261 ASSERT(xfs_Gqm);
262 ASSERT(xfs_Gqm->qm_nrefs > 0);
263
264 /*
265 * Go thru the freelist and destroy all inactive dquots.
266 */
267 xfs_qm_freelist_lock(xfs_Gqm);
268
269 for (dqp = xfs_Gqm->qm_dqfreelist.qh_next;
270 dqp != (xfs_dquot_t *)&(xfs_Gqm->qm_dqfreelist); ) {
271 xfs_dqlock(dqp);
272 nextdqp = dqp->dq_flnext;
273 if (dqp->dq_flags & XFS_DQ_INACTIVE) {
274 ASSERT(dqp->q_mount == NULL);
275 ASSERT(! XFS_DQ_IS_DIRTY(dqp));
276 ASSERT(dqp->HL_PREVP == NULL);
277 ASSERT(dqp->MPL_PREVP == NULL);
278 XQM_FREELIST_REMOVE(dqp);
279 xfs_dqunlock(dqp);
280 xfs_qm_dqdestroy(dqp);
281 } else {
282 xfs_dqunlock(dqp);
283 }
284 dqp = nextdqp;
285 }
286 xfs_qm_freelist_unlock(xfs_Gqm);
287
288 /*
289 * Destroy the entire XQM. If somebody mounts with quotaon, this'll
290 * be restarted.
291 */
Christoph Hellwige2494582009-02-09 08:38:39 +0100292 mutex_lock(&xfs_Gqm_lock);
293 if (--xfs_Gqm->qm_nrefs == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 xfs_qm_destroy(xfs_Gqm);
295 xfs_Gqm = NULL;
296 }
Christoph Hellwige2494582009-02-09 08:38:39 +0100297 mutex_unlock(&xfs_Gqm_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298}
299
300/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 * Just destroy the quotainfo structure.
302 */
303void
Christoph Hellwig7d095252009-06-08 15:33:32 +0200304xfs_qm_unmount(
305 struct xfs_mount *mp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306{
Christoph Hellwig7d095252009-06-08 15:33:32 +0200307 if (mp->m_quotainfo) {
308 xfs_qm_dqpurge_all(mp, XFS_QMOPT_QUOTALL | XFS_QMOPT_UMOUNTING);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 xfs_qm_destroy_quotainfo(mp);
Christoph Hellwig7d095252009-06-08 15:33:32 +0200310 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311}
312
313
314/*
315 * This is called from xfs_mountfs to start quotas and initialize all
316 * necessary data structures like quotainfo. This is also responsible for
317 * running a quotacheck as necessary. We are guaranteed that the superblock
318 * is consistently read in at this point.
David Chinner53aa7912008-04-10 12:20:31 +1000319 *
320 * If we fail here, the mount will continue with quota turned off. We don't
321 * need to inidicate success or failure at all.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 */
David Chinner53aa7912008-04-10 12:20:31 +1000323void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324xfs_qm_mount_quotas(
Christoph Hellwig42490232008-08-13 16:49:32 +1000325 xfs_mount_t *mp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 int error = 0;
328 uint sbf;
329
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 /*
331 * If quotas on realtime volumes is not supported, we disable
332 * quotas immediately.
333 */
334 if (mp->m_sb.sb_rextents) {
335 cmn_err(CE_NOTE,
336 "Cannot turn on quotas for realtime filesystem %s",
337 mp->m_fsname);
338 mp->m_qflags = 0;
339 goto write_changes;
340 }
341
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 ASSERT(XFS_IS_QUOTA_RUNNING(mp));
Nathan Scott155ffd02005-09-02 16:43:48 +1000343
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 /*
345 * Allocate the quotainfo structure inside the mount struct, and
346 * create quotainode(s), and change/rev superblock if necessary.
347 */
David Chinner53aa7912008-04-10 12:20:31 +1000348 error = xfs_qm_init_quotainfo(mp);
349 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 /*
351 * We must turn off quotas.
352 */
353 ASSERT(mp->m_quotainfo == NULL);
354 mp->m_qflags = 0;
355 goto write_changes;
356 }
357 /*
358 * If any of the quotas are not consistent, do a quotacheck.
359 */
Christoph Hellwig42490232008-08-13 16:49:32 +1000360 if (XFS_QM_NEED_QUOTACHECK(mp)) {
David Chinner53aa7912008-04-10 12:20:31 +1000361 error = xfs_qm_quotacheck(mp);
362 if (error) {
363 /* Quotacheck failed and disabled quotas. */
364 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 }
Donald Douwsma646d5bd2007-05-08 13:49:09 +1000367 /*
368 * If one type of quotas is off, then it will lose its
369 * quotachecked status, since we won't be doing accounting for
370 * that type anymore.
371 */
David Chinner53aa7912008-04-10 12:20:31 +1000372 if (!XFS_IS_UQUOTA_ON(mp))
Donald Douwsma646d5bd2007-05-08 13:49:09 +1000373 mp->m_qflags &= ~XFS_UQUOTA_CHKD;
David Chinner53aa7912008-04-10 12:20:31 +1000374 if (!(XFS_IS_GQUOTA_ON(mp) || XFS_IS_PQUOTA_ON(mp)))
Donald Douwsma646d5bd2007-05-08 13:49:09 +1000375 mp->m_qflags &= ~XFS_OQUOTA_CHKD;
Nathan Scott155ffd02005-09-02 16:43:48 +1000376
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 write_changes:
378 /*
Eric Sandeen3685c2a2007-10-11 17:42:32 +1000379 * We actually don't have to acquire the m_sb_lock at all.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 * This can only be called from mount, and that's single threaded. XXX
381 */
Eric Sandeen3685c2a2007-10-11 17:42:32 +1000382 spin_lock(&mp->m_sb_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 sbf = mp->m_sb.sb_qflags;
384 mp->m_sb.sb_qflags = mp->m_qflags & XFS_MOUNT_QUOTA_ALL;
Eric Sandeen3685c2a2007-10-11 17:42:32 +1000385 spin_unlock(&mp->m_sb_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386
387 if (sbf != (mp->m_qflags & XFS_MOUNT_QUOTA_ALL)) {
388 if (xfs_qm_write_sb_changes(mp, XFS_SB_QFLAGS)) {
389 /*
390 * We could only have been turning quotas off.
391 * We aren't in very good shape actually because
392 * the incore structures are convinced that quotas are
393 * off, but the on disk superblock doesn't know that !
394 */
395 ASSERT(!(XFS_IS_QUOTA_RUNNING(mp)));
396 xfs_fs_cmn_err(CE_ALERT, mp,
397 "XFS mount_quotas: Superblock update failed!");
398 }
399 }
400
401 if (error) {
402 xfs_fs_cmn_err(CE_WARN, mp,
403 "Failed to initialize disk quotas.");
Christoph Hellwig7d095252009-06-08 15:33:32 +0200404 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 }
Christoph Hellwig7d095252009-06-08 15:33:32 +0200406
407#ifdef QUOTADEBUG
408 if (XFS_IS_QUOTA_ON(mp))
409 xfs_qm_internalqcheck(mp);
410#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411}
412
413/*
414 * Called from the vfsops layer.
415 */
Christoph Hellwige57481d2008-12-03 12:20:36 +0100416void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417xfs_qm_unmount_quotas(
418 xfs_mount_t *mp)
419{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 /*
421 * Release the dquots that root inode, et al might be holding,
422 * before we flush quotas and blow away the quotainfo structure.
423 */
424 ASSERT(mp->m_rootip);
425 xfs_qm_dqdetach(mp->m_rootip);
426 if (mp->m_rbmip)
427 xfs_qm_dqdetach(mp->m_rbmip);
428 if (mp->m_rsumip)
429 xfs_qm_dqdetach(mp->m_rsumip);
430
431 /*
Christoph Hellwige57481d2008-12-03 12:20:36 +0100432 * Release the quota inodes.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 if (mp->m_quotainfo) {
Christoph Hellwige57481d2008-12-03 12:20:36 +0100435 if (mp->m_quotainfo->qi_uquotaip) {
436 IRELE(mp->m_quotainfo->qi_uquotaip);
437 mp->m_quotainfo->qi_uquotaip = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 }
Christoph Hellwige57481d2008-12-03 12:20:36 +0100439 if (mp->m_quotainfo->qi_gquotaip) {
440 IRELE(mp->m_quotainfo->qi_gquotaip);
441 mp->m_quotainfo->qi_gquotaip = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 }
443 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444}
445
446/*
447 * Flush all dquots of the given file system to disk. The dquots are
448 * _not_ purged from memory here, just their data written to disk.
449 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +1000450STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451xfs_qm_dqflush_all(
452 xfs_mount_t *mp,
Dave Chinner20026d92010-02-04 09:48:58 +1100453 int sync_mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454{
455 int recl;
456 xfs_dquot_t *dqp;
457 int niters;
458 int error;
459
460 if (mp->m_quotainfo == NULL)
Jesper Juhl014c2542006-01-15 02:37:08 +0100461 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 niters = 0;
463again:
464 xfs_qm_mplist_lock(mp);
465 FOREACH_DQUOT_IN_MP(dqp, mp) {
466 xfs_dqlock(dqp);
467 if (! XFS_DQ_IS_DIRTY(dqp)) {
468 xfs_dqunlock(dqp);
469 continue;
470 }
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000471
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 /* XXX a sentinel would be better */
473 recl = XFS_QI_MPLRECLAIMS(mp);
David Chinnere1f49cf2008-08-13 16:41:43 +1000474 if (!xfs_dqflock_nowait(dqp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 /*
476 * If we can't grab the flush lock then check
477 * to see if the dquot has been flushed delayed
478 * write. If so, grab its buffer and send it
479 * out immediately. We'll be able to acquire
480 * the flush lock when the I/O completes.
481 */
482 xfs_qm_dqflock_pushbuf_wait(dqp);
483 }
484 /*
485 * Let go of the mplist lock. We don't want to hold it
486 * across a disk write.
487 */
488 xfs_qm_mplist_unlock(mp);
Dave Chinner20026d92010-02-04 09:48:58 +1100489 error = xfs_qm_dqflush(dqp, sync_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 xfs_dqunlock(dqp);
491 if (error)
Jesper Juhl014c2542006-01-15 02:37:08 +0100492 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493
494 xfs_qm_mplist_lock(mp);
495 if (recl != XFS_QI_MPLRECLAIMS(mp)) {
496 xfs_qm_mplist_unlock(mp);
497 /* XXX restart limit */
498 goto again;
499 }
500 }
501
502 xfs_qm_mplist_unlock(mp);
503 /* return ! busy */
Jesper Juhl014c2542006-01-15 02:37:08 +0100504 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505}
506/*
507 * Release the group dquot pointers the user dquots may be
508 * carrying around as a hint. mplist is locked on entry and exit.
509 */
510STATIC void
511xfs_qm_detach_gdquots(
512 xfs_mount_t *mp)
513{
514 xfs_dquot_t *dqp, *gdqp;
515 int nrecl;
516
517 again:
518 ASSERT(XFS_QM_IS_MPLIST_LOCKED(mp));
519 dqp = XFS_QI_MPLNEXT(mp);
520 while (dqp) {
521 xfs_dqlock(dqp);
522 if ((gdqp = dqp->q_gdquot)) {
523 xfs_dqlock(gdqp);
524 dqp->q_gdquot = NULL;
525 }
526 xfs_dqunlock(dqp);
527
528 if (gdqp) {
529 /*
530 * Can't hold the mplist lock across a dqput.
531 * XXXmust convert to marker based iterations here.
532 */
533 nrecl = XFS_QI_MPLRECLAIMS(mp);
534 xfs_qm_mplist_unlock(mp);
535 xfs_qm_dqput(gdqp);
536
537 xfs_qm_mplist_lock(mp);
538 if (nrecl != XFS_QI_MPLRECLAIMS(mp))
539 goto again;
540 }
541 dqp = dqp->MPL_NEXT;
542 }
543}
544
545/*
546 * Go through all the incore dquots of this file system and take them
547 * off the mplist and hashlist, if the dquot type matches the dqtype
548 * parameter. This is used when turning off quota accounting for
549 * users and/or groups, as well as when the filesystem is unmounting.
550 */
551STATIC int
552xfs_qm_dqpurge_int(
553 xfs_mount_t *mp,
Nathan Scottc8ad20f2005-06-21 15:38:48 +1000554 uint flags) /* QUOTAOFF/UMOUNTING/UQUOTA/PQUOTA/GQUOTA */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555{
556 xfs_dquot_t *dqp;
557 uint dqtype;
558 int nrecl;
559 xfs_dquot_t *nextdqp;
560 int nmisses;
561
562 if (mp->m_quotainfo == NULL)
Jesper Juhl014c2542006-01-15 02:37:08 +0100563 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564
565 dqtype = (flags & XFS_QMOPT_UQUOTA) ? XFS_DQ_USER : 0;
Nathan Scottc8ad20f2005-06-21 15:38:48 +1000566 dqtype |= (flags & XFS_QMOPT_PQUOTA) ? XFS_DQ_PROJ : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 dqtype |= (flags & XFS_QMOPT_GQUOTA) ? XFS_DQ_GROUP : 0;
568
569 xfs_qm_mplist_lock(mp);
570
571 /*
572 * In the first pass through all incore dquots of this filesystem,
573 * we release the group dquot pointers the user dquots may be
574 * carrying around as a hint. We need to do this irrespective of
575 * what's being turned off.
576 */
577 xfs_qm_detach_gdquots(mp);
578
579 again:
580 nmisses = 0;
581 ASSERT(XFS_QM_IS_MPLIST_LOCKED(mp));
582 /*
583 * Try to get rid of all of the unwanted dquots. The idea is to
584 * get them off mplist and hashlist, but leave them on freelist.
585 */
586 dqp = XFS_QI_MPLNEXT(mp);
587 while (dqp) {
588 /*
589 * It's OK to look at the type without taking dqlock here.
590 * We're holding the mplist lock here, and that's needed for
591 * a dqreclaim.
592 */
593 if ((dqp->dq_flags & dqtype) == 0) {
594 dqp = dqp->MPL_NEXT;
595 continue;
596 }
597
Christoph Hellwigc9a192d2009-02-09 08:47:22 +0100598 if (!mutex_trylock(&dqp->q_hash->qh_lock)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 nrecl = XFS_QI_MPLRECLAIMS(mp);
600 xfs_qm_mplist_unlock(mp);
Christoph Hellwigc9a192d2009-02-09 08:47:22 +0100601 mutex_lock(&dqp->q_hash->qh_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 xfs_qm_mplist_lock(mp);
603
604 /*
605 * XXXTheoretically, we can get into a very long
606 * ping pong game here.
607 * No one can be adding dquots to the mplist at
608 * this point, but somebody might be taking things off.
609 */
610 if (nrecl != XFS_QI_MPLRECLAIMS(mp)) {
Christoph Hellwigc9a192d2009-02-09 08:47:22 +0100611 mutex_unlock(&dqp->q_hash->qh_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 goto again;
613 }
614 }
615
616 /*
617 * Take the dquot off the mplist and hashlist. It may remain on
618 * freelist in INACTIVE state.
619 */
620 nextdqp = dqp->MPL_NEXT;
Denys Vlasenko4f0e8a92008-05-19 16:34:04 +1000621 nmisses += xfs_qm_dqpurge(dqp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 dqp = nextdqp;
623 }
624 xfs_qm_mplist_unlock(mp);
625 return nmisses;
626}
627
628int
629xfs_qm_dqpurge_all(
630 xfs_mount_t *mp,
631 uint flags)
632{
633 int ndquots;
634
635 /*
636 * Purge the dquot cache.
637 * None of the dquots should really be busy at this point.
638 */
639 if (mp->m_quotainfo) {
640 while ((ndquots = xfs_qm_dqpurge_int(mp, flags))) {
641 delay(ndquots * 10);
642 }
643 }
644 return 0;
645}
646
647STATIC int
648xfs_qm_dqattach_one(
649 xfs_inode_t *ip,
650 xfs_dqid_t id,
651 uint type,
652 uint doalloc,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 xfs_dquot_t *udqhint, /* hint */
654 xfs_dquot_t **IO_idqpp)
655{
656 xfs_dquot_t *dqp;
657 int error;
658
Christoph Hellwig579aa9c2008-04-22 17:34:00 +1000659 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 error = 0;
Christoph Hellwig8e9b6e72009-02-08 21:51:42 +0100661
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 /*
663 * See if we already have it in the inode itself. IO_idqpp is
664 * &i_udquot or &i_gdquot. This made the code look weird, but
665 * made the logic a lot simpler.
666 */
Christoph Hellwig8e9b6e72009-02-08 21:51:42 +0100667 dqp = *IO_idqpp;
668 if (dqp) {
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000669 trace_xfs_dqattach_found(dqp);
Christoph Hellwig8e9b6e72009-02-08 21:51:42 +0100670 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 }
672
673 /*
674 * udqhint is the i_udquot field in inode, and is non-NULL only
Nathan Scottc8ad20f2005-06-21 15:38:48 +1000675 * when the type arg is group/project. Its purpose is to save a
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 * lookup by dqid (xfs_qm_dqget) by caching a group dquot inside
677 * the user dquot.
678 */
Christoph Hellwig8e9b6e72009-02-08 21:51:42 +0100679 if (udqhint) {
680 ASSERT(type == XFS_DQ_GROUP || type == XFS_DQ_PROJ);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 xfs_dqlock(udqhint);
682
Christoph Hellwig8e9b6e72009-02-08 21:51:42 +0100683 /*
684 * No need to take dqlock to look at the id.
685 *
686 * The ID can't change until it gets reclaimed, and it won't
687 * be reclaimed as long as we have a ref from inode and we
688 * hold the ilock.
689 */
690 dqp = udqhint->q_gdquot;
691 if (dqp && be32_to_cpu(dqp->q_core.d_id) == id) {
692 xfs_dqlock(dqp);
693 XFS_DQHOLD(dqp);
694 ASSERT(*IO_idqpp == NULL);
695 *IO_idqpp = dqp;
696
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 xfs_dqunlock(dqp);
698 xfs_dqunlock(udqhint);
Christoph Hellwig8e9b6e72009-02-08 21:51:42 +0100699 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 }
Christoph Hellwig8e9b6e72009-02-08 21:51:42 +0100701
702 /*
703 * We can't hold a dquot lock when we call the dqget code.
704 * We'll deadlock in no time, because of (not conforming to)
705 * lock ordering - the inodelock comes before any dquot lock,
706 * and we may drop and reacquire the ilock in xfs_qm_dqget().
707 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 xfs_dqunlock(udqhint);
Christoph Hellwig8e9b6e72009-02-08 21:51:42 +0100709 }
710
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 /*
712 * Find the dquot from somewhere. This bumps the
713 * reference count of dquot and returns it locked.
714 * This can return ENOENT if dquot didn't exist on
715 * disk and we didn't ask it to allocate;
716 * ESRCH if quotas got turned off suddenly.
717 */
Christoph Hellwig8e9b6e72009-02-08 21:51:42 +0100718 error = xfs_qm_dqget(ip->i_mount, ip, id, type, XFS_QMOPT_DOWARN, &dqp);
719 if (error)
720 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000722 trace_xfs_dqattach_get(dqp);
Christoph Hellwig8e9b6e72009-02-08 21:51:42 +0100723
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 /*
725 * dqget may have dropped and re-acquired the ilock, but it guarantees
726 * that the dquot returned is the one that should go in the inode.
727 */
728 *IO_idqpp = dqp;
Christoph Hellwig8e9b6e72009-02-08 21:51:42 +0100729 xfs_dqunlock(dqp);
730 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731}
732
733
734/*
735 * Given a udquot and gdquot, attach a ptr to the group dquot in the
736 * udquot as a hint for future lookups. The idea sounds simple, but the
737 * execution isn't, because the udquot might have a group dquot attached
Nathan Scottc41564b2006-03-29 08:55:14 +1000738 * already and getting rid of that gets us into lock ordering constraints.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 * The process is complicated more by the fact that the dquots may or may not
740 * be locked on entry.
741 */
742STATIC void
743xfs_qm_dqattach_grouphint(
744 xfs_dquot_t *udq,
Christoph Hellwig8e9b6e72009-02-08 21:51:42 +0100745 xfs_dquot_t *gdq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746{
747 xfs_dquot_t *tmp;
748
Christoph Hellwig8e9b6e72009-02-08 21:51:42 +0100749 xfs_dqlock(udq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750
751 if ((tmp = udq->q_gdquot)) {
752 if (tmp == gdq) {
Christoph Hellwig8e9b6e72009-02-08 21:51:42 +0100753 xfs_dqunlock(udq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 return;
755 }
756
757 udq->q_gdquot = NULL;
758 /*
759 * We can't keep any dqlocks when calling dqrele,
760 * because the freelist lock comes before dqlocks.
761 */
762 xfs_dqunlock(udq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 /*
764 * we took a hard reference once upon a time in dqget,
765 * so give it back when the udquot no longer points at it
766 * dqput() does the unlocking of the dquot.
767 */
768 xfs_qm_dqrele(tmp);
769
770 xfs_dqlock(udq);
771 xfs_dqlock(gdq);
772
773 } else {
774 ASSERT(XFS_DQ_IS_LOCKED(udq));
Christoph Hellwig8e9b6e72009-02-08 21:51:42 +0100775 xfs_dqlock(gdq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 }
777
778 ASSERT(XFS_DQ_IS_LOCKED(udq));
779 ASSERT(XFS_DQ_IS_LOCKED(gdq));
780 /*
781 * Somebody could have attached a gdquot here,
782 * when we dropped the uqlock. If so, just do nothing.
783 */
784 if (udq->q_gdquot == NULL) {
785 XFS_DQHOLD(gdq);
786 udq->q_gdquot = gdq;
787 }
Christoph Hellwig8e9b6e72009-02-08 21:51:42 +0100788
789 xfs_dqunlock(gdq);
790 xfs_dqunlock(udq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791}
792
793
794/*
Nathan Scottc8ad20f2005-06-21 15:38:48 +1000795 * Given a locked inode, attach dquot(s) to it, taking U/G/P-QUOTAON
796 * into account.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 * If XFS_QMOPT_DQALLOC, the dquot(s) will be allocated if needed.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 * Inode may get unlocked and relocked in here, and the caller must deal with
799 * the consequences.
800 */
801int
Christoph Hellwig7d095252009-06-08 15:33:32 +0200802xfs_qm_dqattach_locked(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 xfs_inode_t *ip,
804 uint flags)
805{
806 xfs_mount_t *mp = ip->i_mount;
807 uint nquotas = 0;
808 int error = 0;
809
Christoph Hellwig7d095252009-06-08 15:33:32 +0200810 if (!XFS_IS_QUOTA_RUNNING(mp) ||
811 !XFS_IS_QUOTA_ON(mp) ||
812 !XFS_NOT_DQATTACHED(mp, ip) ||
813 ip->i_ino == mp->m_sb.sb_uquotino ||
814 ip->i_ino == mp->m_sb.sb_gquotino)
Jesper Juhl014c2542006-01-15 02:37:08 +0100815 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816
Christoph Hellwig7d095252009-06-08 15:33:32 +0200817 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818
819 if (XFS_IS_UQUOTA_ON(mp)) {
820 error = xfs_qm_dqattach_one(ip, ip->i_d.di_uid, XFS_DQ_USER,
821 flags & XFS_QMOPT_DQALLOC,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 NULL, &ip->i_udquot);
823 if (error)
824 goto done;
825 nquotas++;
826 }
Christoph Hellwig579aa9c2008-04-22 17:34:00 +1000827
828 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
Nathan Scottc8ad20f2005-06-21 15:38:48 +1000829 if (XFS_IS_OQUOTA_ON(mp)) {
830 error = XFS_IS_GQUOTA_ON(mp) ?
831 xfs_qm_dqattach_one(ip, ip->i_d.di_gid, XFS_DQ_GROUP,
832 flags & XFS_QMOPT_DQALLOC,
Nathan Scottc8ad20f2005-06-21 15:38:48 +1000833 ip->i_udquot, &ip->i_gdquot) :
834 xfs_qm_dqattach_one(ip, ip->i_d.di_projid, XFS_DQ_PROJ,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 flags & XFS_QMOPT_DQALLOC,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 ip->i_udquot, &ip->i_gdquot);
837 /*
838 * Don't worry about the udquot that we may have
839 * attached above. It'll get detached, if not already.
840 */
841 if (error)
842 goto done;
843 nquotas++;
844 }
845
846 /*
847 * Attach this group quota to the user quota as a hint.
848 * This WON'T, in general, result in a thrash.
849 */
850 if (nquotas == 2) {
Christoph Hellwig579aa9c2008-04-22 17:34:00 +1000851 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 ASSERT(ip->i_udquot);
853 ASSERT(ip->i_gdquot);
854
855 /*
856 * We may or may not have the i_udquot locked at this point,
857 * but this check is OK since we don't depend on the i_gdquot to
858 * be accurate 100% all the time. It is just a hint, and this
859 * will succeed in general.
860 */
861 if (ip->i_udquot->q_gdquot == ip->i_gdquot)
862 goto done;
863 /*
864 * Attach i_gdquot to the gdquot hint inside the i_udquot.
865 */
Christoph Hellwig8e9b6e72009-02-08 21:51:42 +0100866 xfs_qm_dqattach_grouphint(ip->i_udquot, ip->i_gdquot);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 }
868
Christoph Hellwig7d095252009-06-08 15:33:32 +0200869 done:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870#ifdef QUOTADEBUG
871 if (! error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 if (XFS_IS_UQUOTA_ON(mp))
873 ASSERT(ip->i_udquot);
Nathan Scottc8ad20f2005-06-21 15:38:48 +1000874 if (XFS_IS_OQUOTA_ON(mp))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 ASSERT(ip->i_gdquot);
876 }
Christoph Hellwig7d095252009-06-08 15:33:32 +0200877 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878#endif
Christoph Hellwig7d095252009-06-08 15:33:32 +0200879 return error;
880}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881
Christoph Hellwig7d095252009-06-08 15:33:32 +0200882int
883xfs_qm_dqattach(
884 struct xfs_inode *ip,
885 uint flags)
886{
887 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888
Christoph Hellwig7d095252009-06-08 15:33:32 +0200889 xfs_ilock(ip, XFS_ILOCK_EXCL);
890 error = xfs_qm_dqattach_locked(ip, flags);
891 xfs_iunlock(ip, XFS_ILOCK_EXCL);
892
Jesper Juhl014c2542006-01-15 02:37:08 +0100893 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894}
895
896/*
897 * Release dquots (and their references) if any.
898 * The inode should be locked EXCL except when this's called by
899 * xfs_ireclaim.
900 */
901void
902xfs_qm_dqdetach(
903 xfs_inode_t *ip)
904{
905 if (!(ip->i_udquot || ip->i_gdquot))
906 return;
907
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000908 trace_xfs_dquot_dqdetach(ip);
909
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 ASSERT(ip->i_ino != ip->i_mount->m_sb.sb_uquotino);
911 ASSERT(ip->i_ino != ip->i_mount->m_sb.sb_gquotino);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 if (ip->i_udquot) {
913 xfs_qm_dqrele(ip->i_udquot);
914 ip->i_udquot = NULL;
915 }
916 if (ip->i_gdquot) {
917 xfs_qm_dqrele(ip->i_gdquot);
918 ip->i_gdquot = NULL;
919 }
920}
921
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922int
923xfs_qm_sync(
924 xfs_mount_t *mp,
Christoph Hellwigb09cc772007-08-30 17:19:57 +1000925 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926{
927 int recl, restarts;
928 xfs_dquot_t *dqp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 int error;
930
Christoph Hellwig7d095252009-06-08 15:33:32 +0200931 if (!XFS_IS_QUOTA_RUNNING(mp) || !XFS_IS_QUOTA_ON(mp))
Donald Douwsmad7577622007-11-23 16:27:42 +1100932 return 0;
933
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 restarts = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935
936 again:
937 xfs_qm_mplist_lock(mp);
938 /*
939 * dqpurge_all() also takes the mplist lock and iterate thru all dquots
940 * in quotaoff. However, if the QUOTA_ACTIVE bits are not cleared
941 * when we have the mplist lock, we know that dquots will be consistent
942 * as long as we have it locked.
943 */
944 if (! XFS_IS_QUOTA_ON(mp)) {
945 xfs_qm_mplist_unlock(mp);
Jesper Juhl014c2542006-01-15 02:37:08 +0100946 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 }
948 FOREACH_DQUOT_IN_MP(dqp, mp) {
949 /*
950 * If this is vfs_sync calling, then skip the dquots that
951 * don't 'seem' to be dirty. ie. don't acquire dqlock.
952 * This is very similar to what xfs_sync does with inodes.
953 */
Christoph Hellwig8b5403a2009-06-08 15:37:16 +0200954 if (flags & SYNC_TRYLOCK) {
955 if (!XFS_DQ_IS_DIRTY(dqp))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 continue;
Christoph Hellwig8b5403a2009-06-08 15:37:16 +0200957 if (!xfs_qm_dqlock_nowait(dqp))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 continue;
959 } else {
960 xfs_dqlock(dqp);
961 }
962
963 /*
964 * Now, find out for sure if this dquot is dirty or not.
965 */
966 if (! XFS_DQ_IS_DIRTY(dqp)) {
967 xfs_dqunlock(dqp);
968 continue;
969 }
970
971 /* XXX a sentinel would be better */
972 recl = XFS_QI_MPLRECLAIMS(mp);
David Chinnere1f49cf2008-08-13 16:41:43 +1000973 if (!xfs_dqflock_nowait(dqp)) {
Christoph Hellwig8b5403a2009-06-08 15:37:16 +0200974 if (flags & SYNC_TRYLOCK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 xfs_dqunlock(dqp);
976 continue;
977 }
978 /*
979 * If we can't grab the flush lock then if the caller
Nathan Scottc41564b2006-03-29 08:55:14 +1000980 * really wanted us to give this our best shot, so
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 * see if we can give a push to the buffer before we wait
982 * on the flush lock. At this point, we know that
Nathan Scottc41564b2006-03-29 08:55:14 +1000983 * even though the dquot is being flushed,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 * it has (new) dirty data.
985 */
986 xfs_qm_dqflock_pushbuf_wait(dqp);
987 }
988 /*
989 * Let go of the mplist lock. We don't want to hold it
990 * across a disk write
991 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 xfs_qm_mplist_unlock(mp);
Dave Chinner20026d92010-02-04 09:48:58 +1100993 error = xfs_qm_dqflush(dqp, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 xfs_dqunlock(dqp);
995 if (error && XFS_FORCED_SHUTDOWN(mp))
Jesper Juhl014c2542006-01-15 02:37:08 +0100996 return 0; /* Need to prevent umount failure */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 else if (error)
Jesper Juhl014c2542006-01-15 02:37:08 +0100998 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999
1000 xfs_qm_mplist_lock(mp);
1001 if (recl != XFS_QI_MPLRECLAIMS(mp)) {
1002 if (++restarts >= XFS_QM_SYNC_MAX_RESTARTS)
1003 break;
1004
1005 xfs_qm_mplist_unlock(mp);
1006 goto again;
1007 }
1008 }
1009
1010 xfs_qm_mplist_unlock(mp);
Jesper Juhl014c2542006-01-15 02:37:08 +01001011 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012}
1013
Christoph Hellwiga4edd1d2009-01-19 02:03:11 +01001014/*
1015 * The hash chains and the mplist use the same xfs_dqhash structure as
1016 * their list head, but we can take the mplist qh_lock and one of the
1017 * hash qh_locks at the same time without any problem as they aren't
1018 * related.
1019 */
1020static struct lock_class_key xfs_quota_mplist_class;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021
1022/*
1023 * This initializes all the quota information that's kept in the
1024 * mount structure
1025 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +10001026STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027xfs_qm_init_quotainfo(
1028 xfs_mount_t *mp)
1029{
1030 xfs_quotainfo_t *qinf;
1031 int error;
1032 xfs_dquot_t *dqp;
1033
1034 ASSERT(XFS_IS_QUOTA_RUNNING(mp));
1035
1036 /*
1037 * Tell XQM that we exist as soon as possible.
1038 */
1039 if ((error = xfs_qm_hold_quotafs_ref(mp))) {
Jesper Juhl014c2542006-01-15 02:37:08 +01001040 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 }
1042
1043 qinf = mp->m_quotainfo = kmem_zalloc(sizeof(xfs_quotainfo_t), KM_SLEEP);
1044
1045 /*
1046 * See if quotainodes are setup, and if not, allocate them,
1047 * and change the superblock accordingly.
1048 */
1049 if ((error = xfs_qm_init_quotainos(mp))) {
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10001050 kmem_free(qinf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 mp->m_quotainfo = NULL;
Jesper Juhl014c2542006-01-15 02:37:08 +01001052 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 }
1054
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 xfs_qm_list_init(&qinf->qi_dqlist, "mpdqlist", 0);
Christoph Hellwiga4edd1d2009-01-19 02:03:11 +01001056 lockdep_set_class(&qinf->qi_dqlist.qh_lock, &xfs_quota_mplist_class);
1057
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 qinf->qi_dqreclaims = 0;
1059
1060 /* mutex used to serialize quotaoffs */
Jes Sorensen794ee1b2006-01-09 15:59:21 -08001061 mutex_init(&qinf->qi_quotaofflock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062
1063 /* Precalc some constants */
1064 qinf->qi_dqchunklen = XFS_FSB_TO_BB(mp, XFS_DQUOT_CLUSTER_SIZE_FSB);
1065 ASSERT(qinf->qi_dqchunklen);
1066 qinf->qi_dqperchunk = BBTOB(qinf->qi_dqchunklen);
1067 do_div(qinf->qi_dqperchunk, sizeof(xfs_dqblk_t));
1068
1069 mp->m_qflags |= (mp->m_sb.sb_qflags & XFS_ALL_QUOTA_CHKD);
1070
1071 /*
1072 * We try to get the limits from the superuser's limits fields.
1073 * This is quite hacky, but it is standard quota practice.
1074 * We look at the USR dquot with id == 0 first, but if user quotas
1075 * are not enabled we goto the GRP dquot with id == 0.
1076 * We don't really care to keep separate default limits for user
1077 * and group quotas, at least not at this point.
1078 */
1079 error = xfs_qm_dqget(mp, NULL, (xfs_dqid_t)0,
Nathan Scottc8ad20f2005-06-21 15:38:48 +10001080 XFS_IS_UQUOTA_RUNNING(mp) ? XFS_DQ_USER :
1081 (XFS_IS_GQUOTA_RUNNING(mp) ? XFS_DQ_GROUP :
1082 XFS_DQ_PROJ),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 XFS_QMOPT_DQSUSER|XFS_QMOPT_DOWARN,
1084 &dqp);
1085 if (! error) {
1086 xfs_disk_dquot_t *ddqp = &dqp->q_core;
1087
1088 /*
1089 * The warnings and timers set the grace period given to
1090 * a user or group before he or she can not perform any
1091 * more writing. If it is zero, a default is used.
1092 */
Christoph Hellwig1149d962005-11-02 15:01:12 +11001093 qinf->qi_btimelimit = ddqp->d_btimer ?
1094 be32_to_cpu(ddqp->d_btimer) : XFS_QM_BTIMELIMIT;
1095 qinf->qi_itimelimit = ddqp->d_itimer ?
1096 be32_to_cpu(ddqp->d_itimer) : XFS_QM_ITIMELIMIT;
1097 qinf->qi_rtbtimelimit = ddqp->d_rtbtimer ?
1098 be32_to_cpu(ddqp->d_rtbtimer) : XFS_QM_RTBTIMELIMIT;
1099 qinf->qi_bwarnlimit = ddqp->d_bwarns ?
1100 be16_to_cpu(ddqp->d_bwarns) : XFS_QM_BWARNLIMIT;
1101 qinf->qi_iwarnlimit = ddqp->d_iwarns ?
1102 be16_to_cpu(ddqp->d_iwarns) : XFS_QM_IWARNLIMIT;
1103 qinf->qi_rtbwarnlimit = ddqp->d_rtbwarns ?
1104 be16_to_cpu(ddqp->d_rtbwarns) : XFS_QM_RTBWARNLIMIT;
1105 qinf->qi_bhardlimit = be64_to_cpu(ddqp->d_blk_hardlimit);
1106 qinf->qi_bsoftlimit = be64_to_cpu(ddqp->d_blk_softlimit);
1107 qinf->qi_ihardlimit = be64_to_cpu(ddqp->d_ino_hardlimit);
1108 qinf->qi_isoftlimit = be64_to_cpu(ddqp->d_ino_softlimit);
1109 qinf->qi_rtbhardlimit = be64_to_cpu(ddqp->d_rtb_hardlimit);
1110 qinf->qi_rtbsoftlimit = be64_to_cpu(ddqp->d_rtb_softlimit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111
1112 /*
1113 * We sent the XFS_QMOPT_DQSUSER flag to dqget because
1114 * we don't want this dquot cached. We haven't done a
1115 * quotacheck yet, and quotacheck doesn't like incore dquots.
1116 */
1117 xfs_qm_dqdestroy(dqp);
1118 } else {
1119 qinf->qi_btimelimit = XFS_QM_BTIMELIMIT;
1120 qinf->qi_itimelimit = XFS_QM_ITIMELIMIT;
1121 qinf->qi_rtbtimelimit = XFS_QM_RTBTIMELIMIT;
1122 qinf->qi_bwarnlimit = XFS_QM_BWARNLIMIT;
1123 qinf->qi_iwarnlimit = XFS_QM_IWARNLIMIT;
Nathan Scott06d10dd2005-06-21 15:48:47 +10001124 qinf->qi_rtbwarnlimit = XFS_QM_RTBWARNLIMIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 }
1126
Jesper Juhl014c2542006-01-15 02:37:08 +01001127 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128}
1129
1130
1131/*
1132 * Gets called when unmounting a filesystem or when all quotas get
1133 * turned off.
1134 * This purges the quota inodes, destroys locks and frees itself.
1135 */
1136void
1137xfs_qm_destroy_quotainfo(
1138 xfs_mount_t *mp)
1139{
1140 xfs_quotainfo_t *qi;
1141
1142 qi = mp->m_quotainfo;
1143 ASSERT(qi != NULL);
1144 ASSERT(xfs_Gqm != NULL);
1145
1146 /*
1147 * Release the reference that XQM kept, so that we know
1148 * when the XQM structure should be freed. We cannot assume
1149 * that xfs_Gqm is non-null after this point.
1150 */
1151 xfs_qm_rele_quotafs_ref(mp);
1152
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 xfs_qm_list_destroy(&qi->qi_dqlist);
1154
1155 if (qi->qi_uquotaip) {
Christoph Hellwig26cc0022008-07-18 17:12:43 +10001156 IRELE(qi->qi_uquotaip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 qi->qi_uquotaip = NULL; /* paranoia */
1158 }
1159 if (qi->qi_gquotaip) {
Christoph Hellwig26cc0022008-07-18 17:12:43 +10001160 IRELE(qi->qi_gquotaip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 qi->qi_gquotaip = NULL;
1162 }
1163 mutex_destroy(&qi->qi_quotaofflock);
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10001164 kmem_free(qi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 mp->m_quotainfo = NULL;
1166}
1167
1168
1169
1170/* ------------------- PRIVATE STATIC FUNCTIONS ----------------------- */
1171
1172/* ARGSUSED */
1173STATIC void
1174xfs_qm_list_init(
1175 xfs_dqlist_t *list,
1176 char *str,
1177 int n)
1178{
Jes Sorensen794ee1b2006-01-09 15:59:21 -08001179 mutex_init(&list->qh_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 list->qh_next = NULL;
1181 list->qh_version = 0;
1182 list->qh_nelems = 0;
1183}
1184
1185STATIC void
1186xfs_qm_list_destroy(
1187 xfs_dqlist_t *list)
1188{
1189 mutex_destroy(&(list->qh_lock));
1190}
1191
1192
1193/*
1194 * Stripped down version of dqattach. This doesn't attach, or even look at the
1195 * dquots attached to the inode. The rationale is that there won't be any
1196 * attached at the time this is called from quotacheck.
1197 */
1198STATIC int
1199xfs_qm_dqget_noattach(
1200 xfs_inode_t *ip,
1201 xfs_dquot_t **O_udqpp,
1202 xfs_dquot_t **O_gdqpp)
1203{
1204 int error;
1205 xfs_mount_t *mp;
1206 xfs_dquot_t *udqp, *gdqp;
1207
Christoph Hellwig579aa9c2008-04-22 17:34:00 +10001208 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 mp = ip->i_mount;
1210 udqp = NULL;
1211 gdqp = NULL;
1212
1213 if (XFS_IS_UQUOTA_ON(mp)) {
1214 ASSERT(ip->i_udquot == NULL);
1215 /*
1216 * We want the dquot allocated if it doesn't exist.
1217 */
1218 if ((error = xfs_qm_dqget(mp, ip, ip->i_d.di_uid, XFS_DQ_USER,
1219 XFS_QMOPT_DQALLOC | XFS_QMOPT_DOWARN,
1220 &udqp))) {
1221 /*
1222 * Shouldn't be able to turn off quotas here.
1223 */
1224 ASSERT(error != ESRCH);
1225 ASSERT(error != ENOENT);
Jesper Juhl014c2542006-01-15 02:37:08 +01001226 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 }
1228 ASSERT(udqp);
1229 }
1230
Nathan Scottc8ad20f2005-06-21 15:38:48 +10001231 if (XFS_IS_OQUOTA_ON(mp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 ASSERT(ip->i_gdquot == NULL);
1233 if (udqp)
1234 xfs_dqunlock(udqp);
Nathan Scottc8ad20f2005-06-21 15:38:48 +10001235 error = XFS_IS_GQUOTA_ON(mp) ?
1236 xfs_qm_dqget(mp, ip,
1237 ip->i_d.di_gid, XFS_DQ_GROUP,
1238 XFS_QMOPT_DQALLOC|XFS_QMOPT_DOWARN,
1239 &gdqp) :
1240 xfs_qm_dqget(mp, ip,
1241 ip->i_d.di_projid, XFS_DQ_PROJ,
1242 XFS_QMOPT_DQALLOC|XFS_QMOPT_DOWARN,
1243 &gdqp);
1244 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 if (udqp)
1246 xfs_qm_dqrele(udqp);
1247 ASSERT(error != ESRCH);
1248 ASSERT(error != ENOENT);
Jesper Juhl014c2542006-01-15 02:37:08 +01001249 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 }
1251 ASSERT(gdqp);
1252
1253 /* Reacquire the locks in the right order */
1254 if (udqp) {
1255 if (! xfs_qm_dqlock_nowait(udqp)) {
1256 xfs_dqunlock(gdqp);
1257 xfs_dqlock(udqp);
1258 xfs_dqlock(gdqp);
1259 }
1260 }
1261 }
1262
1263 *O_udqpp = udqp;
1264 *O_gdqpp = gdqp;
1265
1266#ifdef QUOTADEBUG
1267 if (udqp) ASSERT(XFS_DQ_IS_LOCKED(udqp));
1268 if (gdqp) ASSERT(XFS_DQ_IS_LOCKED(gdqp));
1269#endif
Jesper Juhl014c2542006-01-15 02:37:08 +01001270 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271}
1272
1273/*
1274 * Create an inode and return with a reference already taken, but unlocked
1275 * This is how we create quota inodes
1276 */
1277STATIC int
1278xfs_qm_qino_alloc(
1279 xfs_mount_t *mp,
1280 xfs_inode_t **ip,
1281 __int64_t sbfields,
1282 uint flags)
1283{
1284 xfs_trans_t *tp;
1285 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 int committed;
1287
Nathan Scott061f7202006-01-11 15:27:50 +11001288 tp = xfs_trans_alloc(mp, XFS_TRANS_QM_QINOCREATE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 if ((error = xfs_trans_reserve(tp,
1290 XFS_QM_QINOCREATE_SPACE_RES(mp),
1291 XFS_CREATE_LOG_RES(mp), 0,
1292 XFS_TRANS_PERM_LOG_RES,
1293 XFS_CREATE_LOG_COUNT))) {
1294 xfs_trans_cancel(tp, 0);
Jesper Juhl014c2542006-01-15 02:37:08 +01001295 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297
David Chinnerb11f94d2007-07-11 11:09:33 +10001298 if ((error = xfs_dir_ialloc(&tp, NULL, S_IFREG, 1, 0,
Nathan Scottdae81d42006-02-28 12:30:13 +11001299 &xfs_zerocr, 0, 1, ip, &committed))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES |
1301 XFS_TRANS_ABORT);
Jesper Juhl014c2542006-01-15 02:37:08 +01001302 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 }
1304
1305 /*
1306 * Keep an extra reference to this quota inode. This inode is
1307 * locked exclusively and joined to the transaction already.
1308 */
Christoph Hellwig579aa9c2008-04-22 17:34:00 +10001309 ASSERT(xfs_isilocked(*ip, XFS_ILOCK_EXCL));
Christoph Hellwig26cc0022008-07-18 17:12:43 +10001310 IHOLD(*ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311
1312 /*
1313 * Make the changes in the superblock, and log those too.
1314 * sbfields arg may contain fields other than *QUOTINO;
1315 * VERSIONNUM for example.
1316 */
Eric Sandeen3685c2a2007-10-11 17:42:32 +10001317 spin_lock(&mp->m_sb_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 if (flags & XFS_QMOPT_SBVERSION) {
1319#if defined(DEBUG) && defined(XFS_LOUD_RECOVERY)
1320 unsigned oldv = mp->m_sb.sb_versionnum;
1321#endif
Eric Sandeen62118702008-03-06 13:44:28 +11001322 ASSERT(!xfs_sb_version_hasquota(&mp->m_sb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 ASSERT((sbfields & (XFS_SB_VERSIONNUM | XFS_SB_UQUOTINO |
1324 XFS_SB_GQUOTINO | XFS_SB_QFLAGS)) ==
1325 (XFS_SB_VERSIONNUM | XFS_SB_UQUOTINO |
1326 XFS_SB_GQUOTINO | XFS_SB_QFLAGS));
1327
Eric Sandeen62118702008-03-06 13:44:28 +11001328 xfs_sb_version_addquota(&mp->m_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 mp->m_sb.sb_uquotino = NULLFSINO;
1330 mp->m_sb.sb_gquotino = NULLFSINO;
1331
1332 /* qflags will get updated _after_ quotacheck */
1333 mp->m_sb.sb_qflags = 0;
1334#if defined(DEBUG) && defined(XFS_LOUD_RECOVERY)
1335 cmn_err(CE_NOTE,
1336 "Old superblock version %x, converting to %x.",
1337 oldv, mp->m_sb.sb_versionnum);
1338#endif
1339 }
1340 if (flags & XFS_QMOPT_UQUOTA)
1341 mp->m_sb.sb_uquotino = (*ip)->i_ino;
1342 else
1343 mp->m_sb.sb_gquotino = (*ip)->i_ino;
Eric Sandeen3685c2a2007-10-11 17:42:32 +10001344 spin_unlock(&mp->m_sb_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 xfs_mod_sb(tp, sbfields);
1346
Eric Sandeen1c72bf92007-05-08 13:48:42 +10001347 if ((error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 xfs_fs_cmn_err(CE_ALERT, mp, "XFS qino_alloc failed!");
Jesper Juhl014c2542006-01-15 02:37:08 +01001349 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 }
Jesper Juhl014c2542006-01-15 02:37:08 +01001351 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352}
1353
1354
David Chinner5b139732008-04-10 12:20:10 +10001355STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356xfs_qm_reset_dqcounts(
1357 xfs_mount_t *mp,
1358 xfs_buf_t *bp,
1359 xfs_dqid_t id,
1360 uint type)
1361{
1362 xfs_disk_dquot_t *ddq;
1363 int j;
1364
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001365 trace_xfs_reset_dqcounts(bp, _RET_IP_);
1366
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 /*
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));
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001559
1560 trace_xfs_dqadjust(dqp);
1561
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562 /*
1563 * Adjust the inode count and the block count to reflect this inode's
1564 * resource usage.
1565 */
Marcin Slusarz413d57c2008-02-13 15:03:29 -08001566 be64_add_cpu(&dqp->q_core.d_icount, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567 dqp->q_res_icount++;
1568 if (nblks) {
Marcin Slusarz413d57c2008-02-13 15:03:29 -08001569 be64_add_cpu(&dqp->q_core.d_bcount, nblks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570 dqp->q_res_bcount += nblks;
1571 }
1572 if (rtblks) {
Marcin Slusarz413d57c2008-02-13 15:03:29 -08001573 be64_add_cpu(&dqp->q_core.d_rtbcount, rtblks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574 dqp->q_res_rtbcount += rtblks;
1575 }
1576
1577 /*
1578 * Set default limits, adjust timers (since we changed usages)
1579 */
1580 if (! XFS_IS_SUSER_DQUOT(dqp)) {
1581 xfs_qm_adjust_dqlimits(dqp->q_mount, &dqp->q_core);
1582 xfs_qm_adjust_dqtimers(dqp->q_mount, &dqp->q_core);
1583 }
1584
1585 dqp->dq_flags |= XFS_DQ_DIRTY;
1586}
1587
1588STATIC int
1589xfs_qm_get_rtblks(
1590 xfs_inode_t *ip,
1591 xfs_qcnt_t *O_rtblks)
1592{
1593 xfs_filblks_t rtblks; /* total rt blks */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001594 xfs_extnum_t idx; /* extent record index */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595 xfs_ifork_t *ifp; /* inode fork pointer */
1596 xfs_extnum_t nextents; /* number of extent entries */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597 int error;
1598
1599 ASSERT(XFS_IS_REALTIME_INODE(ip));
1600 ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
1601 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
1602 if ((error = xfs_iread_extents(NULL, ip, XFS_DATA_FORK)))
Jesper Juhl014c2542006-01-15 02:37:08 +01001603 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604 }
1605 rtblks = 0;
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001606 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10001607 for (idx = 0; idx < nextents; idx++)
1608 rtblks += xfs_bmbt_get_blockcount(xfs_iext_get_ext(ifp, idx));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609 *O_rtblks = (xfs_qcnt_t)rtblks;
Jesper Juhl014c2542006-01-15 02:37:08 +01001610 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611}
1612
1613/*
1614 * callback routine supplied to bulkstat(). Given an inumber, find its
1615 * dquots and update them to account for resources taken by that inode.
1616 */
1617/* ARGSUSED */
1618STATIC int
1619xfs_qm_dqusage_adjust(
1620 xfs_mount_t *mp, /* mount point for filesystem */
1621 xfs_ino_t ino, /* inode number to get data for */
1622 void __user *buffer, /* not used */
1623 int ubsize, /* not used */
1624 void *private_data, /* not used */
1625 xfs_daddr_t bno, /* starting block of inode cluster */
1626 int *ubused, /* not used */
1627 void *dip, /* on-disk inode pointer (not used) */
1628 int *res) /* result code value */
1629{
1630 xfs_inode_t *ip;
1631 xfs_dquot_t *udqp, *gdqp;
1632 xfs_qcnt_t nblks, rtblks;
1633 int error;
1634
1635 ASSERT(XFS_IS_QUOTA_RUNNING(mp));
1636
1637 /*
1638 * rootino must have its resources accounted for, not so with the quota
1639 * inodes.
1640 */
1641 if (ino == mp->m_sb.sb_uquotino || ino == mp->m_sb.sb_gquotino) {
1642 *res = BULKSTAT_RV_NOTHING;
1643 return XFS_ERROR(EINVAL);
1644 }
1645
1646 /*
1647 * We don't _need_ to take the ilock EXCL. However, the xfs_qm_dqget
1648 * interface expects the inode to be exclusively locked because that's
1649 * the case in all other instances. It's OK that we do this because
1650 * quotacheck is done only at mount time.
1651 */
1652 if ((error = xfs_iget(mp, NULL, ino, 0, XFS_ILOCK_EXCL, &ip, bno))) {
1653 *res = BULKSTAT_RV_NOTHING;
Jesper Juhl014c2542006-01-15 02:37:08 +01001654 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655 }
1656
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657 /*
1658 * Obtain the locked dquots. In case of an error (eg. allocation
1659 * fails for ENOSPC), we return the negative of the error number
1660 * to bulkstat, so that it can get propagated to quotacheck() and
1661 * making us disable quotas for the file system.
1662 */
1663 if ((error = xfs_qm_dqget_noattach(ip, &udqp, &gdqp))) {
1664 xfs_iput(ip, XFS_ILOCK_EXCL);
1665 *res = BULKSTAT_RV_GIVEUP;
Jesper Juhl014c2542006-01-15 02:37:08 +01001666 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667 }
1668
1669 rtblks = 0;
1670 if (! XFS_IS_REALTIME_INODE(ip)) {
1671 nblks = (xfs_qcnt_t)ip->i_d.di_nblocks;
1672 } else {
1673 /*
1674 * Walk thru the extent list and count the realtime blocks.
1675 */
1676 if ((error = xfs_qm_get_rtblks(ip, &rtblks))) {
1677 xfs_iput(ip, XFS_ILOCK_EXCL);
1678 if (udqp)
1679 xfs_qm_dqput(udqp);
1680 if (gdqp)
1681 xfs_qm_dqput(gdqp);
1682 *res = BULKSTAT_RV_GIVEUP;
Jesper Juhl014c2542006-01-15 02:37:08 +01001683 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684 }
1685 nblks = (xfs_qcnt_t)ip->i_d.di_nblocks - rtblks;
1686 }
1687 ASSERT(ip->i_delayed_blks == 0);
1688
1689 /*
1690 * We can't release the inode while holding its dquot locks.
1691 * The inode can go into inactive and might try to acquire the dquotlocks.
1692 * So, just unlock here and do a vn_rele at the end.
1693 */
1694 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1695
1696 /*
1697 * Add the (disk blocks and inode) resources occupied by this
1698 * inode to its dquots. We do this adjustment in the incore dquot,
1699 * and also copy the changes to its buffer.
1700 * We don't care about putting these changes in a transaction
1701 * envelope because if we crash in the middle of a 'quotacheck'
1702 * we have to start from the beginning anyway.
1703 * Once we're done, we'll log all the dquot bufs.
1704 *
Nathan Scottc41564b2006-03-29 08:55:14 +10001705 * The *QUOTA_ON checks below may look pretty racy, but quotachecks
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706 * and quotaoffs don't race. (Quotachecks happen at mount time only).
1707 */
1708 if (XFS_IS_UQUOTA_ON(mp)) {
1709 ASSERT(udqp);
1710 xfs_qm_quotacheck_dqadjust(udqp, nblks, rtblks);
1711 xfs_qm_dqput(udqp);
1712 }
Nathan Scottc8ad20f2005-06-21 15:38:48 +10001713 if (XFS_IS_OQUOTA_ON(mp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714 ASSERT(gdqp);
1715 xfs_qm_quotacheck_dqadjust(gdqp, nblks, rtblks);
1716 xfs_qm_dqput(gdqp);
1717 }
1718 /*
1719 * Now release the inode. This will send it to 'inactive', and
1720 * possibly even free blocks.
1721 */
Christoph Hellwig43355092008-03-27 18:01:08 +11001722 IRELE(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723
1724 /*
1725 * Goto next inode.
1726 */
1727 *res = BULKSTAT_RV_DIDONE;
Jesper Juhl014c2542006-01-15 02:37:08 +01001728 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729}
1730
1731/*
1732 * Walk thru all the filesystem inodes and construct a consistent view
1733 * of the disk quota world. If the quotacheck fails, disable quotas.
1734 */
1735int
1736xfs_qm_quotacheck(
1737 xfs_mount_t *mp)
1738{
1739 int done, count, error;
1740 xfs_ino_t lastino;
1741 size_t structsz;
1742 xfs_inode_t *uip, *gip;
1743 uint flags;
1744
1745 count = INT_MAX;
1746 structsz = 1;
1747 lastino = 0;
1748 flags = 0;
1749
1750 ASSERT(XFS_QI_UQIP(mp) || XFS_QI_GQIP(mp));
1751 ASSERT(XFS_IS_QUOTA_RUNNING(mp));
1752
1753 /*
1754 * There should be no cached dquots. The (simplistic) quotacheck
1755 * algorithm doesn't like that.
1756 */
1757 ASSERT(XFS_QI_MPLNDQUOTS(mp) == 0);
1758
1759 cmn_err(CE_NOTE, "XFS quotacheck %s: Please wait.", mp->m_fsname);
1760
1761 /*
Nathan Scottc8ad20f2005-06-21 15:38:48 +10001762 * First we go thru all the dquots on disk, USR and GRP/PRJ, and reset
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763 * their counters to zero. We need a clean slate.
1764 * We don't log our changes till later.
1765 */
1766 if ((uip = XFS_QI_UQIP(mp))) {
1767 if ((error = xfs_qm_dqiterate(mp, uip, XFS_QMOPT_UQUOTA)))
1768 goto error_return;
1769 flags |= XFS_UQUOTA_CHKD;
1770 }
1771
1772 if ((gip = XFS_QI_GQIP(mp))) {
Nathan Scottc8ad20f2005-06-21 15:38:48 +10001773 if ((error = xfs_qm_dqiterate(mp, gip, XFS_IS_GQUOTA_ON(mp) ?
1774 XFS_QMOPT_GQUOTA : XFS_QMOPT_PQUOTA)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775 goto error_return;
Nathan Scottc8ad20f2005-06-21 15:38:48 +10001776 flags |= XFS_OQUOTA_CHKD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777 }
1778
1779 do {
1780 /*
1781 * Iterate thru all the inodes in the file system,
1782 * adjusting the corresponding dquot counters in core.
1783 */
1784 if ((error = xfs_bulkstat(mp, &lastino, &count,
1785 xfs_qm_dqusage_adjust, NULL,
Nathan Scott9c488762006-06-09 15:29:22 +10001786 structsz, NULL, BULKSTAT_FG_IGET, &done)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787 break;
1788
1789 } while (! done);
1790
1791 /*
David Chinner4b8879d2008-04-10 12:20:17 +10001792 * We've made all the changes that we need to make incore.
1793 * Flush them down to disk buffers if everything was updated
1794 * successfully.
1795 */
1796 if (!error)
Dave Chinner20026d92010-02-04 09:48:58 +11001797 error = xfs_qm_dqflush_all(mp, 0);
David Chinner4b8879d2008-04-10 12:20:17 +10001798
1799 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800 * We can get this error if we couldn't do a dquot allocation inside
1801 * xfs_qm_dqusage_adjust (via bulkstat). We don't care about the
1802 * dirty dquots that might be cached, we just want to get rid of them
1803 * and turn quotaoff. The dquots won't be attached to any of the inodes
1804 * at this point (because we intentionally didn't in dqget_noattach).
1805 */
1806 if (error) {
Nathan Scottee2a4f72006-01-11 15:33:36 +11001807 xfs_qm_dqpurge_all(mp, XFS_QMOPT_QUOTALL | XFS_QMOPT_QUOTAOFF);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808 goto error_return;
1809 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810
1811 /*
1812 * We didn't log anything, because if we crashed, we'll have to
1813 * start the quotacheck from scratch anyway. However, we must make
1814 * sure that our dquot changes are secure before we put the
1815 * quotacheck'd stamp on the superblock. So, here we do a synchronous
1816 * flush.
1817 */
1818 XFS_bflush(mp->m_ddev_targp);
1819
1820 /*
1821 * If one type of quotas is off, then it will lose its
1822 * quotachecked status, since we won't be doing accounting for
1823 * that type anymore.
1824 */
Nathan Scottc8ad20f2005-06-21 15:38:48 +10001825 mp->m_qflags &= ~(XFS_OQUOTA_CHKD | XFS_UQUOTA_CHKD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826 mp->m_qflags |= flags;
1827
1828 XQM_LIST_PRINT(&(XFS_QI_MPL_LIST(mp)), MPL_NEXT, "++++ Mp list +++");
1829
1830 error_return:
1831 if (error) {
1832 cmn_err(CE_WARN, "XFS quotacheck %s: Unsuccessful (Error %d): "
1833 "Disabling quotas.",
1834 mp->m_fsname, error);
1835 /*
1836 * We must turn off quotas.
1837 */
1838 ASSERT(mp->m_quotainfo != NULL);
1839 ASSERT(xfs_Gqm != NULL);
1840 xfs_qm_destroy_quotainfo(mp);
David Chinner31d55772008-04-10 12:20:38 +10001841 if (xfs_mount_reset_sbqflags(mp)) {
1842 cmn_err(CE_WARN, "XFS quotacheck %s: "
1843 "Failed to reset quota flags.", mp->m_fsname);
1844 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845 } else {
1846 cmn_err(CE_NOTE, "XFS quotacheck %s: Done.", mp->m_fsname);
1847 }
1848 return (error);
1849}
1850
1851/*
1852 * This is called after the superblock has been read in and we're ready to
1853 * iget the quota inodes.
1854 */
1855STATIC int
1856xfs_qm_init_quotainos(
1857 xfs_mount_t *mp)
1858{
1859 xfs_inode_t *uip, *gip;
1860 int error;
1861 __int64_t sbflags;
1862 uint flags;
1863
1864 ASSERT(mp->m_quotainfo);
1865 uip = gip = NULL;
1866 sbflags = 0;
1867 flags = 0;
1868
1869 /*
1870 * Get the uquota and gquota inodes
1871 */
Eric Sandeen62118702008-03-06 13:44:28 +11001872 if (xfs_sb_version_hasquota(&mp->m_sb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873 if (XFS_IS_UQUOTA_ON(mp) &&
1874 mp->m_sb.sb_uquotino != NULLFSINO) {
1875 ASSERT(mp->m_sb.sb_uquotino > 0);
1876 if ((error = xfs_iget(mp, NULL, mp->m_sb.sb_uquotino,
1877 0, 0, &uip, 0)))
1878 return XFS_ERROR(error);
1879 }
Nathan Scottc8ad20f2005-06-21 15:38:48 +10001880 if (XFS_IS_OQUOTA_ON(mp) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881 mp->m_sb.sb_gquotino != NULLFSINO) {
1882 ASSERT(mp->m_sb.sb_gquotino > 0);
1883 if ((error = xfs_iget(mp, NULL, mp->m_sb.sb_gquotino,
1884 0, 0, &gip, 0))) {
1885 if (uip)
Christoph Hellwig43355092008-03-27 18:01:08 +11001886 IRELE(uip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887 return XFS_ERROR(error);
1888 }
1889 }
1890 } else {
1891 flags |= XFS_QMOPT_SBVERSION;
1892 sbflags |= (XFS_SB_VERSIONNUM | XFS_SB_UQUOTINO |
1893 XFS_SB_GQUOTINO | XFS_SB_QFLAGS);
1894 }
1895
1896 /*
1897 * Create the two inodes, if they don't exist already. The changes
1898 * made above will get added to a transaction and logged in one of
1899 * the qino_alloc calls below. If the device is readonly,
1900 * temporarily switch to read-write to do this.
1901 */
1902 if (XFS_IS_UQUOTA_ON(mp) && uip == NULL) {
1903 if ((error = xfs_qm_qino_alloc(mp, &uip,
1904 sbflags | XFS_SB_UQUOTINO,
1905 flags | XFS_QMOPT_UQUOTA)))
1906 return XFS_ERROR(error);
1907
1908 flags &= ~XFS_QMOPT_SBVERSION;
1909 }
Nathan Scottc8ad20f2005-06-21 15:38:48 +10001910 if (XFS_IS_OQUOTA_ON(mp) && gip == NULL) {
1911 flags |= (XFS_IS_GQUOTA_ON(mp) ?
1912 XFS_QMOPT_GQUOTA : XFS_QMOPT_PQUOTA);
1913 error = xfs_qm_qino_alloc(mp, &gip,
1914 sbflags | XFS_SB_GQUOTINO, flags);
1915 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916 if (uip)
Christoph Hellwig43355092008-03-27 18:01:08 +11001917 IRELE(uip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918
1919 return XFS_ERROR(error);
1920 }
1921 }
1922
1923 XFS_QI_UQIP(mp) = uip;
1924 XFS_QI_GQIP(mp) = gip;
1925
Jesper Juhl014c2542006-01-15 02:37:08 +01001926 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927}
1928
1929
1930/*
1931 * Traverse the freelist of dquots and attempt to reclaim a maximum of
1932 * 'howmany' dquots. This operation races with dqlookup(), and attempts to
1933 * favor the lookup function ...
1934 * XXXsup merge this with qm_reclaim_one().
1935 */
1936STATIC int
1937xfs_qm_shake_freelist(
1938 int howmany)
1939{
1940 int nreclaimed;
1941 xfs_dqhash_t *hash;
1942 xfs_dquot_t *dqp, *nextdqp;
1943 int restarts;
1944 int nflushes;
1945
1946 if (howmany <= 0)
Jesper Juhl014c2542006-01-15 02:37:08 +01001947 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948
1949 nreclaimed = 0;
1950 restarts = 0;
1951 nflushes = 0;
1952
1953#ifdef QUOTADEBUG
1954 cmn_err(CE_DEBUG, "Shake free 0x%x", howmany);
1955#endif
1956 /* lock order is : hashchainlock, freelistlock, mplistlock */
1957 tryagain:
1958 xfs_qm_freelist_lock(xfs_Gqm);
1959
1960 for (dqp = xfs_Gqm->qm_dqfreelist.qh_next;
1961 ((dqp != (xfs_dquot_t *) &xfs_Gqm->qm_dqfreelist) &&
1962 nreclaimed < howmany); ) {
1963 xfs_dqlock(dqp);
1964
1965 /*
1966 * We are racing with dqlookup here. Naturally we don't
1967 * want to reclaim a dquot that lookup wants.
1968 */
1969 if (dqp->dq_flags & XFS_DQ_WANT) {
1970 xfs_dqunlock(dqp);
1971 xfs_qm_freelist_unlock(xfs_Gqm);
1972 if (++restarts >= XFS_QM_RECLAIM_MAX_RESTARTS)
Jesper Juhl014c2542006-01-15 02:37:08 +01001973 return nreclaimed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974 XQM_STATS_INC(xqmstats.xs_qm_dqwants);
1975 goto tryagain;
1976 }
1977
1978 /*
1979 * If the dquot is inactive, we are assured that it is
1980 * not on the mplist or the hashlist, and that makes our
1981 * life easier.
1982 */
1983 if (dqp->dq_flags & XFS_DQ_INACTIVE) {
1984 ASSERT(dqp->q_mount == NULL);
1985 ASSERT(! XFS_DQ_IS_DIRTY(dqp));
1986 ASSERT(dqp->HL_PREVP == NULL);
1987 ASSERT(dqp->MPL_PREVP == NULL);
1988 XQM_STATS_INC(xqmstats.xs_qm_dqinact_reclaims);
1989 nextdqp = dqp->dq_flnext;
1990 goto off_freelist;
1991 }
1992
1993 ASSERT(dqp->MPL_PREVP);
1994 /*
1995 * Try to grab the flush lock. If this dquot is in the process of
1996 * getting flushed to disk, we don't want to reclaim it.
1997 */
David Chinnere1f49cf2008-08-13 16:41:43 +10001998 if (!xfs_dqflock_nowait(dqp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999 xfs_dqunlock(dqp);
2000 dqp = dqp->dq_flnext;
2001 continue;
2002 }
2003
2004 /*
2005 * We have the flush lock so we know that this is not in the
2006 * process of being flushed. So, if this is dirty, flush it
2007 * DELWRI so that we don't get a freelist infested with
2008 * dirty dquots.
2009 */
2010 if (XFS_DQ_IS_DIRTY(dqp)) {
David Chinner3c568362008-04-10 12:20:24 +10002011 int error;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002012
2013 trace_xfs_dqshake_dirty(dqp);
2014
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015 /*
2016 * We flush it delayed write, so don't bother
2017 * releasing the mplock.
2018 */
Dave Chinner20026d92010-02-04 09:48:58 +11002019 error = xfs_qm_dqflush(dqp, 0);
David Chinner3c568362008-04-10 12:20:24 +10002020 if (error) {
2021 xfs_fs_cmn_err(CE_WARN, dqp->q_mount,
2022 "xfs_qm_dqflush_all: dquot %p flush failed", dqp);
2023 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024 xfs_dqunlock(dqp); /* dqflush unlocks dqflock */
2025 dqp = dqp->dq_flnext;
2026 continue;
2027 }
2028 /*
2029 * We're trying to get the hashlock out of order. This races
2030 * with dqlookup; so, we giveup and goto the next dquot if
2031 * we couldn't get the hashlock. This way, we won't starve
2032 * a dqlookup process that holds the hashlock that is
2033 * waiting for the freelist lock.
2034 */
Christoph Hellwigc9a192d2009-02-09 08:47:22 +01002035 if (!mutex_trylock(&dqp->q_hash->qh_lock)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036 xfs_dqfunlock(dqp);
2037 xfs_dqunlock(dqp);
2038 dqp = dqp->dq_flnext;
2039 continue;
2040 }
2041 /*
2042 * This races with dquot allocation code as well as dqflush_all
2043 * and reclaim code. So, if we failed to grab the mplist lock,
2044 * giveup everything and start over.
2045 */
2046 hash = dqp->q_hash;
2047 ASSERT(hash);
2048 if (! xfs_qm_mplist_nowait(dqp->q_mount)) {
2049 /* XXX put a sentinel so that we can come back here */
2050 xfs_dqfunlock(dqp);
2051 xfs_dqunlock(dqp);
Christoph Hellwigc9a192d2009-02-09 08:47:22 +01002052 mutex_unlock(&hash->qh_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053 xfs_qm_freelist_unlock(xfs_Gqm);
2054 if (++restarts >= XFS_QM_RECLAIM_MAX_RESTARTS)
Jesper Juhl014c2542006-01-15 02:37:08 +01002055 return nreclaimed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056 goto tryagain;
2057 }
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002058
2059 trace_xfs_dqshake_unlink(dqp);
2060
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061#ifdef QUOTADEBUG
2062 cmn_err(CE_DEBUG, "Shake 0x%p, ID 0x%x\n",
Christoph Hellwig1149d962005-11-02 15:01:12 +11002063 dqp, be32_to_cpu(dqp->q_core.d_id));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064#endif
2065 ASSERT(dqp->q_nrefs == 0);
2066 nextdqp = dqp->dq_flnext;
2067 XQM_MPLIST_REMOVE(&(XFS_QI_MPL_LIST(dqp->q_mount)), dqp);
2068 XQM_HASHLIST_REMOVE(hash, dqp);
2069 xfs_dqfunlock(dqp);
2070 xfs_qm_mplist_unlock(dqp->q_mount);
Christoph Hellwigc9a192d2009-02-09 08:47:22 +01002071 mutex_unlock(&hash->qh_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072
2073 off_freelist:
2074 XQM_FREELIST_REMOVE(dqp);
2075 xfs_dqunlock(dqp);
2076 nreclaimed++;
2077 XQM_STATS_INC(xqmstats.xs_qm_dqshake_reclaims);
2078 xfs_qm_dqdestroy(dqp);
2079 dqp = nextdqp;
2080 }
2081 xfs_qm_freelist_unlock(xfs_Gqm);
Jesper Juhl014c2542006-01-15 02:37:08 +01002082 return nreclaimed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083}
2084
2085
2086/*
2087 * The kmem_shake interface is invoked when memory is running low.
2088 */
2089/* ARGSUSED */
2090STATIC int
Al Viro51bfb752005-12-15 09:17:24 +00002091xfs_qm_shake(int nr_to_scan, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092{
2093 int ndqused, nfree, n;
2094
2095 if (!kmem_shake_allow(gfp_mask))
Jesper Juhl014c2542006-01-15 02:37:08 +01002096 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097 if (!xfs_Gqm)
Jesper Juhl014c2542006-01-15 02:37:08 +01002098 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099
2100 nfree = xfs_Gqm->qm_dqfreelist.qh_nelems; /* free dquots */
2101 /* incore dquots in all f/s's */
2102 ndqused = atomic_read(&xfs_Gqm->qm_totaldquots) - nfree;
2103
2104 ASSERT(ndqused >= 0);
2105
2106 if (nfree <= ndqused && nfree < ndquot)
Jesper Juhl014c2542006-01-15 02:37:08 +01002107 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108
2109 ndqused *= xfs_Gqm->qm_dqfree_ratio; /* target # of free dquots */
2110 n = nfree - ndqused - ndquot; /* # over target */
2111
2112 return xfs_qm_shake_freelist(MAX(nfree, n));
2113}
2114
2115
2116/*
2117 * Just pop the least recently used dquot off the freelist and
2118 * recycle it. The returned dquot is locked.
2119 */
2120STATIC xfs_dquot_t *
2121xfs_qm_dqreclaim_one(void)
2122{
2123 xfs_dquot_t *dqpout;
2124 xfs_dquot_t *dqp;
2125 int restarts;
2126 int nflushes;
2127
2128 restarts = 0;
2129 dqpout = NULL;
2130 nflushes = 0;
2131
2132 /* lockorder: hashchainlock, freelistlock, mplistlock, dqlock, dqflock */
2133 startagain:
2134 xfs_qm_freelist_lock(xfs_Gqm);
2135
2136 FOREACH_DQUOT_IN_FREELIST(dqp, &(xfs_Gqm->qm_dqfreelist)) {
2137 xfs_dqlock(dqp);
2138
2139 /*
2140 * We are racing with dqlookup here. Naturally we don't
2141 * want to reclaim a dquot that lookup wants. We release the
2142 * freelist lock and start over, so that lookup will grab
2143 * both the dquot and the freelistlock.
2144 */
2145 if (dqp->dq_flags & XFS_DQ_WANT) {
2146 ASSERT(! (dqp->dq_flags & XFS_DQ_INACTIVE));
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002147
2148 trace_xfs_dqreclaim_want(dqp);
2149
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150 xfs_dqunlock(dqp);
2151 xfs_qm_freelist_unlock(xfs_Gqm);
2152 if (++restarts >= XFS_QM_RECLAIM_MAX_RESTARTS)
Jesper Juhl014c2542006-01-15 02:37:08 +01002153 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154 XQM_STATS_INC(xqmstats.xs_qm_dqwants);
2155 goto startagain;
2156 }
2157
2158 /*
2159 * If the dquot is inactive, we are assured that it is
2160 * not on the mplist or the hashlist, and that makes our
2161 * life easier.
2162 */
2163 if (dqp->dq_flags & XFS_DQ_INACTIVE) {
2164 ASSERT(dqp->q_mount == NULL);
2165 ASSERT(! XFS_DQ_IS_DIRTY(dqp));
2166 ASSERT(dqp->HL_PREVP == NULL);
2167 ASSERT(dqp->MPL_PREVP == NULL);
2168 XQM_FREELIST_REMOVE(dqp);
2169 xfs_dqunlock(dqp);
2170 dqpout = dqp;
2171 XQM_STATS_INC(xqmstats.xs_qm_dqinact_reclaims);
2172 break;
2173 }
2174
2175 ASSERT(dqp->q_hash);
2176 ASSERT(dqp->MPL_PREVP);
2177
2178 /*
2179 * Try to grab the flush lock. If this dquot is in the process of
2180 * getting flushed to disk, we don't want to reclaim it.
2181 */
David Chinnere1f49cf2008-08-13 16:41:43 +10002182 if (!xfs_dqflock_nowait(dqp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183 xfs_dqunlock(dqp);
2184 continue;
2185 }
2186
2187 /*
2188 * We have the flush lock so we know that this is not in the
2189 * process of being flushed. So, if this is dirty, flush it
2190 * DELWRI so that we don't get a freelist infested with
2191 * dirty dquots.
2192 */
2193 if (XFS_DQ_IS_DIRTY(dqp)) {
David Chinner3c568362008-04-10 12:20:24 +10002194 int error;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002195
2196 trace_xfs_dqreclaim_dirty(dqp);
2197
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198 /*
2199 * We flush it delayed write, so don't bother
2200 * releasing the freelist lock.
2201 */
Dave Chinner20026d92010-02-04 09:48:58 +11002202 error = xfs_qm_dqflush(dqp, 0);
David Chinner3c568362008-04-10 12:20:24 +10002203 if (error) {
2204 xfs_fs_cmn_err(CE_WARN, dqp->q_mount,
2205 "xfs_qm_dqreclaim: dquot %p flush failed", dqp);
2206 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207 xfs_dqunlock(dqp); /* dqflush unlocks dqflock */
2208 continue;
2209 }
2210
2211 if (! xfs_qm_mplist_nowait(dqp->q_mount)) {
2212 xfs_dqfunlock(dqp);
2213 xfs_dqunlock(dqp);
2214 continue;
2215 }
2216
Christoph Hellwigc9a192d2009-02-09 08:47:22 +01002217 if (!mutex_trylock(&dqp->q_hash->qh_lock))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218 goto mplistunlock;
2219
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002220 trace_xfs_dqreclaim_unlink(dqp);
2221
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222 ASSERT(dqp->q_nrefs == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223 XQM_MPLIST_REMOVE(&(XFS_QI_MPL_LIST(dqp->q_mount)), dqp);
2224 XQM_HASHLIST_REMOVE(dqp->q_hash, dqp);
2225 XQM_FREELIST_REMOVE(dqp);
2226 dqpout = dqp;
Christoph Hellwigc9a192d2009-02-09 08:47:22 +01002227 mutex_unlock(&dqp->q_hash->qh_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228 mplistunlock:
2229 xfs_qm_mplist_unlock(dqp->q_mount);
2230 xfs_dqfunlock(dqp);
2231 xfs_dqunlock(dqp);
2232 if (dqpout)
2233 break;
2234 }
2235
2236 xfs_qm_freelist_unlock(xfs_Gqm);
Jesper Juhl014c2542006-01-15 02:37:08 +01002237 return dqpout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238}
2239
2240
2241/*------------------------------------------------------------------*/
2242
2243/*
2244 * Return a new incore dquot. Depending on the number of
2245 * dquots in the system, we either allocate a new one on the kernel heap,
2246 * or reclaim a free one.
2247 * Return value is B_TRUE if we allocated a new dquot, B_FALSE if we managed
2248 * to reclaim an existing one from the freelist.
2249 */
2250boolean_t
2251xfs_qm_dqalloc_incore(
2252 xfs_dquot_t **O_dqpp)
2253{
2254 xfs_dquot_t *dqp;
2255
2256 /*
2257 * Check against high water mark to see if we want to pop
2258 * a nincompoop dquot off the freelist.
2259 */
2260 if (atomic_read(&xfs_Gqm->qm_totaldquots) >= ndquot) {
2261 /*
2262 * Try to recycle a dquot from the freelist.
2263 */
2264 if ((dqp = xfs_qm_dqreclaim_one())) {
2265 XQM_STATS_INC(xqmstats.xs_qm_dqreclaims);
2266 /*
2267 * Just zero the core here. The rest will get
2268 * reinitialized by caller. XXX we shouldn't even
2269 * do this zero ...
2270 */
2271 memset(&dqp->q_core, 0, sizeof(dqp->q_core));
2272 *O_dqpp = dqp;
Jesper Juhl014c2542006-01-15 02:37:08 +01002273 return B_FALSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274 }
2275 XQM_STATS_INC(xqmstats.xs_qm_dqreclaim_misses);
2276 }
2277
2278 /*
2279 * Allocate a brand new dquot on the kernel heap and return it
2280 * to the caller to initialize.
2281 */
2282 ASSERT(xfs_Gqm->qm_dqzone != NULL);
2283 *O_dqpp = kmem_zone_zalloc(xfs_Gqm->qm_dqzone, KM_SLEEP);
2284 atomic_inc(&xfs_Gqm->qm_totaldquots);
2285
Jesper Juhl014c2542006-01-15 02:37:08 +01002286 return B_TRUE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287}
2288
2289
2290/*
2291 * Start a transaction and write the incore superblock changes to
2292 * disk. flags parameter indicates which fields have changed.
2293 */
2294int
2295xfs_qm_write_sb_changes(
2296 xfs_mount_t *mp,
2297 __int64_t flags)
2298{
2299 xfs_trans_t *tp;
2300 int error;
2301
2302#ifdef QUOTADEBUG
2303 cmn_err(CE_NOTE, "Writing superblock quota changes :%s", mp->m_fsname);
2304#endif
2305 tp = xfs_trans_alloc(mp, XFS_TRANS_QM_SBCHANGE);
2306 if ((error = xfs_trans_reserve(tp, 0,
2307 mp->m_sb.sb_sectsize + 128, 0,
2308 0,
2309 XFS_DEFAULT_LOG_COUNT))) {
2310 xfs_trans_cancel(tp, 0);
Jesper Juhl014c2542006-01-15 02:37:08 +01002311 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312 }
2313
2314 xfs_mod_sb(tp, flags);
David Chinnere5720ee2008-04-10 12:21:18 +10002315 error = xfs_trans_commit(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316
David Chinnere5720ee2008-04-10 12:21:18 +10002317 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318}
2319
2320
2321/* --------------- utility functions for vnodeops ---------------- */
2322
2323
2324/*
2325 * Given an inode, a uid and gid (from cred_t) make sure that we have
2326 * allocated relevant dquot(s) on disk, and that we won't exceed inode
2327 * quotas by creating this file.
2328 * This also attaches dquot(s) to the given inode after locking it,
2329 * and returns the dquots corresponding to the uid and/or gid.
2330 *
2331 * in : inode (unlocked)
2332 * out : udquot, gdquot with references taken and unlocked
2333 */
2334int
2335xfs_qm_vop_dqalloc(
Christoph Hellwig7d095252009-06-08 15:33:32 +02002336 struct xfs_inode *ip,
2337 uid_t uid,
2338 gid_t gid,
2339 prid_t prid,
2340 uint flags,
2341 struct xfs_dquot **O_udqpp,
2342 struct xfs_dquot **O_gdqpp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343{
Christoph Hellwig7d095252009-06-08 15:33:32 +02002344 struct xfs_mount *mp = ip->i_mount;
2345 struct xfs_dquot *uq, *gq;
2346 int error;
2347 uint lockflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348
Christoph Hellwig7d095252009-06-08 15:33:32 +02002349 if (!XFS_IS_QUOTA_RUNNING(mp) || !XFS_IS_QUOTA_ON(mp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350 return 0;
2351
2352 lockflags = XFS_ILOCK_EXCL;
2353 xfs_ilock(ip, lockflags);
2354
Christoph Hellwigbd186aa2007-08-30 17:21:12 +10002355 if ((flags & XFS_QMOPT_INHERIT) && XFS_INHERIT_GID(ip))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002356 gid = ip->i_d.di_gid;
2357
2358 /*
2359 * Attach the dquot(s) to this inode, doing a dquot allocation
2360 * if necessary. The dquot(s) will not be locked.
2361 */
2362 if (XFS_NOT_DQATTACHED(mp, ip)) {
Christoph Hellwig7d095252009-06-08 15:33:32 +02002363 error = xfs_qm_dqattach_locked(ip, XFS_QMOPT_DQALLOC);
2364 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365 xfs_iunlock(ip, lockflags);
Jesper Juhl014c2542006-01-15 02:37:08 +01002366 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367 }
2368 }
2369
2370 uq = gq = NULL;
Nathan Scottc8ad20f2005-06-21 15:38:48 +10002371 if ((flags & XFS_QMOPT_UQUOTA) && XFS_IS_UQUOTA_ON(mp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372 if (ip->i_d.di_uid != uid) {
2373 /*
2374 * What we need is the dquot that has this uid, and
2375 * if we send the inode to dqget, the uid of the inode
2376 * takes priority over what's sent in the uid argument.
2377 * We must unlock inode here before calling dqget if
2378 * we're not sending the inode, because otherwise
2379 * we'll deadlock by doing trans_reserve while
2380 * holding ilock.
2381 */
2382 xfs_iunlock(ip, lockflags);
2383 if ((error = xfs_qm_dqget(mp, NULL, (xfs_dqid_t) uid,
2384 XFS_DQ_USER,
2385 XFS_QMOPT_DQALLOC |
2386 XFS_QMOPT_DOWARN,
2387 &uq))) {
2388 ASSERT(error != ENOENT);
Jesper Juhl014c2542006-01-15 02:37:08 +01002389 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002390 }
2391 /*
2392 * Get the ilock in the right order.
2393 */
2394 xfs_dqunlock(uq);
2395 lockflags = XFS_ILOCK_SHARED;
2396 xfs_ilock(ip, lockflags);
2397 } else {
2398 /*
2399 * Take an extra reference, because we'll return
2400 * this to caller
2401 */
2402 ASSERT(ip->i_udquot);
2403 uq = ip->i_udquot;
2404 xfs_dqlock(uq);
2405 XFS_DQHOLD(uq);
2406 xfs_dqunlock(uq);
2407 }
2408 }
Nathan Scottc8ad20f2005-06-21 15:38:48 +10002409 if ((flags & XFS_QMOPT_GQUOTA) && XFS_IS_GQUOTA_ON(mp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002410 if (ip->i_d.di_gid != gid) {
2411 xfs_iunlock(ip, lockflags);
2412 if ((error = xfs_qm_dqget(mp, NULL, (xfs_dqid_t)gid,
2413 XFS_DQ_GROUP,
2414 XFS_QMOPT_DQALLOC |
2415 XFS_QMOPT_DOWARN,
2416 &gq))) {
2417 if (uq)
2418 xfs_qm_dqrele(uq);
2419 ASSERT(error != ENOENT);
Jesper Juhl014c2542006-01-15 02:37:08 +01002420 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002421 }
2422 xfs_dqunlock(gq);
2423 lockflags = XFS_ILOCK_SHARED;
2424 xfs_ilock(ip, lockflags);
2425 } else {
2426 ASSERT(ip->i_gdquot);
2427 gq = ip->i_gdquot;
2428 xfs_dqlock(gq);
2429 XFS_DQHOLD(gq);
2430 xfs_dqunlock(gq);
2431 }
Nathan Scottc8ad20f2005-06-21 15:38:48 +10002432 } else if ((flags & XFS_QMOPT_PQUOTA) && XFS_IS_PQUOTA_ON(mp)) {
2433 if (ip->i_d.di_projid != prid) {
2434 xfs_iunlock(ip, lockflags);
2435 if ((error = xfs_qm_dqget(mp, NULL, (xfs_dqid_t)prid,
2436 XFS_DQ_PROJ,
2437 XFS_QMOPT_DQALLOC |
2438 XFS_QMOPT_DOWARN,
2439 &gq))) {
2440 if (uq)
2441 xfs_qm_dqrele(uq);
2442 ASSERT(error != ENOENT);
2443 return (error);
2444 }
2445 xfs_dqunlock(gq);
2446 lockflags = XFS_ILOCK_SHARED;
2447 xfs_ilock(ip, lockflags);
2448 } else {
2449 ASSERT(ip->i_gdquot);
2450 gq = ip->i_gdquot;
2451 xfs_dqlock(gq);
2452 XFS_DQHOLD(gq);
2453 xfs_dqunlock(gq);
2454 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002455 }
2456 if (uq)
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002457 trace_xfs_dquot_dqalloc(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458
2459 xfs_iunlock(ip, lockflags);
2460 if (O_udqpp)
2461 *O_udqpp = uq;
2462 else if (uq)
2463 xfs_qm_dqrele(uq);
2464 if (O_gdqpp)
2465 *O_gdqpp = gq;
2466 else if (gq)
2467 xfs_qm_dqrele(gq);
Jesper Juhl014c2542006-01-15 02:37:08 +01002468 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002469}
2470
2471/*
2472 * Actually transfer ownership, and do dquot modifications.
2473 * These were already reserved.
2474 */
2475xfs_dquot_t *
2476xfs_qm_vop_chown(
2477 xfs_trans_t *tp,
2478 xfs_inode_t *ip,
2479 xfs_dquot_t **IO_olddq,
2480 xfs_dquot_t *newdq)
2481{
2482 xfs_dquot_t *prevdq;
Nathan Scott06d10dd2005-06-21 15:48:47 +10002483 uint bfield = XFS_IS_REALTIME_INODE(ip) ?
2484 XFS_TRANS_DQ_RTBCOUNT : XFS_TRANS_DQ_BCOUNT;
2485
Christoph Hellwig7d095252009-06-08 15:33:32 +02002486
Christoph Hellwig579aa9c2008-04-22 17:34:00 +10002487 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002488 ASSERT(XFS_IS_QUOTA_RUNNING(ip->i_mount));
2489
2490 /* old dquot */
2491 prevdq = *IO_olddq;
2492 ASSERT(prevdq);
2493 ASSERT(prevdq != newdq);
2494
Nathan Scott06d10dd2005-06-21 15:48:47 +10002495 xfs_trans_mod_dquot(tp, prevdq, bfield, -(ip->i_d.di_nblocks));
2496 xfs_trans_mod_dquot(tp, prevdq, XFS_TRANS_DQ_ICOUNT, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002497
2498 /* the sparkling new dquot */
Nathan Scott06d10dd2005-06-21 15:48:47 +10002499 xfs_trans_mod_dquot(tp, newdq, bfield, ip->i_d.di_nblocks);
2500 xfs_trans_mod_dquot(tp, newdq, XFS_TRANS_DQ_ICOUNT, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002501
2502 /*
2503 * Take an extra reference, because the inode
2504 * is going to keep this dquot pointer even
2505 * after the trans_commit.
2506 */
2507 xfs_dqlock(newdq);
2508 XFS_DQHOLD(newdq);
2509 xfs_dqunlock(newdq);
2510 *IO_olddq = newdq;
2511
Jesper Juhl014c2542006-01-15 02:37:08 +01002512 return prevdq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002513}
2514
2515/*
Nathan Scottc8ad20f2005-06-21 15:38:48 +10002516 * Quota reservations for setattr(AT_UID|AT_GID|AT_PROJID).
Linus Torvalds1da177e2005-04-16 15:20:36 -07002517 */
2518int
2519xfs_qm_vop_chown_reserve(
2520 xfs_trans_t *tp,
2521 xfs_inode_t *ip,
2522 xfs_dquot_t *udqp,
2523 xfs_dquot_t *gdqp,
2524 uint flags)
2525{
Christoph Hellwig7d095252009-06-08 15:33:32 +02002526 xfs_mount_t *mp = ip->i_mount;
Nathan Scott9a2a7de2006-03-31 13:04:49 +10002527 uint delblks, blkflags, prjflags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002528 xfs_dquot_t *unresudq, *unresgdq, *delblksudq, *delblksgdq;
Christoph Hellwig7d095252009-06-08 15:33:32 +02002529 int error;
2530
Linus Torvalds1da177e2005-04-16 15:20:36 -07002531
Christoph Hellwig579aa9c2008-04-22 17:34:00 +10002532 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_ILOCK_SHARED));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002533 ASSERT(XFS_IS_QUOTA_RUNNING(mp));
2534
2535 delblks = ip->i_delayed_blks;
2536 delblksudq = delblksgdq = unresudq = unresgdq = NULL;
Nathan Scott06d10dd2005-06-21 15:48:47 +10002537 blkflags = XFS_IS_REALTIME_INODE(ip) ?
2538 XFS_QMOPT_RES_RTBLKS : XFS_QMOPT_RES_REGBLKS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002539
2540 if (XFS_IS_UQUOTA_ON(mp) && udqp &&
Christoph Hellwig1149d962005-11-02 15:01:12 +11002541 ip->i_d.di_uid != (uid_t)be32_to_cpu(udqp->q_core.d_id)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002542 delblksudq = udqp;
2543 /*
2544 * If there are delayed allocation blocks, then we have to
2545 * unreserve those from the old dquot, and add them to the
2546 * new dquot.
2547 */
2548 if (delblks) {
2549 ASSERT(ip->i_udquot);
2550 unresudq = ip->i_udquot;
2551 }
2552 }
Nathan Scottc8ad20f2005-06-21 15:38:48 +10002553 if (XFS_IS_OQUOTA_ON(ip->i_mount) && gdqp) {
Nathan Scott9a2a7de2006-03-31 13:04:49 +10002554 if (XFS_IS_PQUOTA_ON(ip->i_mount) &&
2555 ip->i_d.di_projid != be32_to_cpu(gdqp->q_core.d_id))
2556 prjflags = XFS_QMOPT_ENOSPC;
2557
2558 if (prjflags ||
2559 (XFS_IS_GQUOTA_ON(ip->i_mount) &&
2560 ip->i_d.di_gid != be32_to_cpu(gdqp->q_core.d_id))) {
Nathan Scottc8ad20f2005-06-21 15:38:48 +10002561 delblksgdq = gdqp;
2562 if (delblks) {
2563 ASSERT(ip->i_gdquot);
2564 unresgdq = ip->i_gdquot;
2565 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002566 }
2567 }
2568
2569 if ((error = xfs_trans_reserve_quota_bydquots(tp, ip->i_mount,
2570 delblksudq, delblksgdq, ip->i_d.di_nblocks, 1,
Nathan Scott9a2a7de2006-03-31 13:04:49 +10002571 flags | blkflags | prjflags)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002572 return (error);
2573
2574 /*
2575 * Do the delayed blks reservations/unreservations now. Since, these
2576 * are done without the help of a transaction, if a reservation fails
2577 * its previous reservations won't be automatically undone by trans
2578 * code. So, we have to do it manually here.
2579 */
2580 if (delblks) {
2581 /*
2582 * Do the reservations first. Unreservation can't fail.
2583 */
2584 ASSERT(delblksudq || delblksgdq);
2585 ASSERT(unresudq || unresgdq);
2586 if ((error = xfs_trans_reserve_quota_bydquots(NULL, ip->i_mount,
2587 delblksudq, delblksgdq, (xfs_qcnt_t)delblks, 0,
Nathan Scott9a2a7de2006-03-31 13:04:49 +10002588 flags | blkflags | prjflags)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002589 return (error);
2590 xfs_trans_reserve_quota_bydquots(NULL, ip->i_mount,
2591 unresudq, unresgdq, -((xfs_qcnt_t)delblks), 0,
Nathan Scott06d10dd2005-06-21 15:48:47 +10002592 blkflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002593 }
2594
2595 return (0);
2596}
2597
2598int
2599xfs_qm_vop_rename_dqattach(
Christoph Hellwig7d095252009-06-08 15:33:32 +02002600 struct xfs_inode **i_tab)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002601{
Christoph Hellwig7d095252009-06-08 15:33:32 +02002602 struct xfs_mount *mp = i_tab[0]->i_mount;
2603 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002604
Christoph Hellwig7d095252009-06-08 15:33:32 +02002605 if (!XFS_IS_QUOTA_RUNNING(mp) || !XFS_IS_QUOTA_ON(mp))
Jesper Juhl014c2542006-01-15 02:37:08 +01002606 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002607
Christoph Hellwig7d095252009-06-08 15:33:32 +02002608 for (i = 0; (i < 4 && i_tab[i]); i++) {
2609 struct xfs_inode *ip = i_tab[i];
2610 int error;
2611
Linus Torvalds1da177e2005-04-16 15:20:36 -07002612 /*
2613 * Watch out for duplicate entries in the table.
2614 */
Christoph Hellwig7d095252009-06-08 15:33:32 +02002615 if (i == 0 || ip != i_tab[i-1]) {
2616 if (XFS_NOT_DQATTACHED(mp, ip)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002617 error = xfs_qm_dqattach(ip, 0);
2618 if (error)
Jesper Juhl014c2542006-01-15 02:37:08 +01002619 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002620 }
2621 }
2622 }
Jesper Juhl014c2542006-01-15 02:37:08 +01002623 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002624}
2625
2626void
Christoph Hellwig7d095252009-06-08 15:33:32 +02002627xfs_qm_vop_create_dqattach(
2628 struct xfs_trans *tp,
2629 struct xfs_inode *ip,
2630 struct xfs_dquot *udqp,
2631 struct xfs_dquot *gdqp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002632{
Christoph Hellwig7d095252009-06-08 15:33:32 +02002633 struct xfs_mount *mp = tp->t_mountp;
2634
2635 if (!XFS_IS_QUOTA_RUNNING(mp) || !XFS_IS_QUOTA_ON(mp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002636 return;
2637
Christoph Hellwig579aa9c2008-04-22 17:34:00 +10002638 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
Christoph Hellwig7d095252009-06-08 15:33:32 +02002639 ASSERT(XFS_IS_QUOTA_RUNNING(mp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002640
2641 if (udqp) {
2642 xfs_dqlock(udqp);
2643 XFS_DQHOLD(udqp);
2644 xfs_dqunlock(udqp);
2645 ASSERT(ip->i_udquot == NULL);
2646 ip->i_udquot = udqp;
Christoph Hellwig7d095252009-06-08 15:33:32 +02002647 ASSERT(XFS_IS_UQUOTA_ON(mp));
Christoph Hellwig1149d962005-11-02 15:01:12 +11002648 ASSERT(ip->i_d.di_uid == be32_to_cpu(udqp->q_core.d_id));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002649 xfs_trans_mod_dquot(tp, udqp, XFS_TRANS_DQ_ICOUNT, 1);
2650 }
2651 if (gdqp) {
2652 xfs_dqlock(gdqp);
2653 XFS_DQHOLD(gdqp);
2654 xfs_dqunlock(gdqp);
2655 ASSERT(ip->i_gdquot == NULL);
2656 ip->i_gdquot = gdqp;
Christoph Hellwig7d095252009-06-08 15:33:32 +02002657 ASSERT(XFS_IS_OQUOTA_ON(mp));
2658 ASSERT((XFS_IS_GQUOTA_ON(mp) ?
Nathan Scottee2a4f72006-01-11 15:33:36 +11002659 ip->i_d.di_gid : ip->i_d.di_projid) ==
2660 be32_to_cpu(gdqp->q_core.d_id));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002661 xfs_trans_mod_dquot(tp, gdqp, XFS_TRANS_DQ_ICOUNT, 1);
2662 }
2663}
2664
2665/* ------------- list stuff -----------------*/
Christoph Hellwigba0f32d2005-06-21 15:36:52 +10002666STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -07002667xfs_qm_freelist_init(xfs_frlist_t *ql)
2668{
2669 ql->qh_next = ql->qh_prev = (xfs_dquot_t *) ql;
Jes Sorensen794ee1b2006-01-09 15:59:21 -08002670 mutex_init(&ql->qh_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002671 ql->qh_version = 0;
2672 ql->qh_nelems = 0;
2673}
2674
Christoph Hellwigba0f32d2005-06-21 15:36:52 +10002675STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -07002676xfs_qm_freelist_destroy(xfs_frlist_t *ql)
2677{
2678 xfs_dquot_t *dqp, *nextdqp;
2679
Jes Sorensen794ee1b2006-01-09 15:59:21 -08002680 mutex_lock(&ql->qh_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002681 for (dqp = ql->qh_next;
2682 dqp != (xfs_dquot_t *)ql; ) {
2683 xfs_dqlock(dqp);
2684 nextdqp = dqp->dq_flnext;
2685#ifdef QUOTADEBUG
2686 cmn_err(CE_DEBUG, "FREELIST destroy 0x%p", dqp);
2687#endif
2688 XQM_FREELIST_REMOVE(dqp);
2689 xfs_dqunlock(dqp);
2690 xfs_qm_dqdestroy(dqp);
2691 dqp = nextdqp;
2692 }
Nathan Scott20722a92006-03-14 13:19:08 +11002693 mutex_unlock(&ql->qh_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002694 mutex_destroy(&ql->qh_lock);
2695
2696 ASSERT(ql->qh_nelems == 0);
2697}
2698
Christoph Hellwigba0f32d2005-06-21 15:36:52 +10002699STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -07002700xfs_qm_freelist_insert(xfs_frlist_t *ql, xfs_dquot_t *dq)
2701{
2702 dq->dq_flnext = ql->qh_next;
2703 dq->dq_flprev = (xfs_dquot_t *)ql;
2704 ql->qh_next = dq;
2705 dq->dq_flnext->dq_flprev = dq;
2706 xfs_Gqm->qm_dqfreelist.qh_nelems++;
2707 xfs_Gqm->qm_dqfreelist.qh_version++;
2708}
2709
2710void
2711xfs_qm_freelist_unlink(xfs_dquot_t *dq)
2712{
2713 xfs_dquot_t *next = dq->dq_flnext;
2714 xfs_dquot_t *prev = dq->dq_flprev;
2715
2716 next->dq_flprev = prev;
2717 prev->dq_flnext = next;
2718 dq->dq_flnext = dq->dq_flprev = dq;
2719 xfs_Gqm->qm_dqfreelist.qh_nelems--;
2720 xfs_Gqm->qm_dqfreelist.qh_version++;
2721}
2722
2723void
2724xfs_qm_freelist_append(xfs_frlist_t *ql, xfs_dquot_t *dq)
2725{
2726 xfs_qm_freelist_insert((xfs_frlist_t *)ql->qh_prev, dq);
2727}