blob: a2579e1d687fc3924b56fcd18187435479e6373c [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_alloc.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include "xfs_quota.h"
28#include "xfs_mount.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include "xfs_bmap_btree.h"
30#include "xfs_ialloc_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include "xfs_dinode.h"
32#include "xfs_inode.h"
Nathan Scotta844f452005-11-02 14:38:42 +110033#include "xfs_ialloc.h"
34#include "xfs_itable.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include "xfs_rtalloc.h"
36#include "xfs_error.h"
Nathan Scotta844f452005-11-02 14:38:42 +110037#include "xfs_bmap.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#include "xfs_attr.h"
39#include "xfs_buf_item.h"
40#include "xfs_trans_space.h"
41#include "xfs_utils.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#include "xfs_qm.h"
Christoph Hellwig0b1b2132009-12-14 23:14:59 +000043#include "xfs_trace.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
45/*
46 * The global quota manager. There is only one of these for the entire
47 * system, _not_ one per file system. XQM keeps track of the overall
48 * quota functionality, including maintaining the freelist and hash
49 * tables of dquots.
50 */
Christoph Hellwiga0b0b8a2009-03-29 09:51:00 +020051struct mutex xfs_Gqm_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -070052struct xfs_qm *xfs_Gqm;
53
54kmem_zone_t *qm_dqzone;
55kmem_zone_t *qm_dqtrxzone;
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
57STATIC void xfs_qm_list_init(xfs_dqlist_t *, char *, int);
58STATIC void xfs_qm_list_destroy(xfs_dqlist_t *);
59
60STATIC int xfs_qm_init_quotainos(xfs_mount_t *);
Christoph Hellwigba0f32d2005-06-21 15:36:52 +100061STATIC int xfs_qm_init_quotainfo(xfs_mount_t *);
Ying Han1495f232011-05-24 17:12:27 -070062STATIC int xfs_qm_shake(struct shrinker *, struct shrink_control *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
Linus Torvalds1da177e2005-04-16 15:20:36 -070064/*
65 * Initialize the XQM structure.
66 * Note that there is not one quota manager per file system.
67 */
68STATIC struct xfs_qm *
69xfs_Gqm_init(void)
70{
Nathan Scott6b3f6b52005-11-02 15:08:25 +110071 xfs_dqhash_t *udqhash, *gdqhash;
72 xfs_qm_t *xqm;
Nathan Scott215101c2006-09-28 11:04:43 +100073 size_t hsize;
74 uint i;
Linus Torvalds1da177e2005-04-16 15:20:36 -070075
76 /*
77 * Initialize the dquot hash tables.
78 */
Nathan Scott77e46352006-09-28 11:03:27 +100079 udqhash = kmem_zalloc_greedy(&hsize,
Eric Sandeen5995cb72007-08-16 16:49:11 +100080 XFS_QM_HASHSIZE_LOW * sizeof(xfs_dqhash_t),
Christoph Hellwigbdfb0432010-01-20 21:55:30 +000081 XFS_QM_HASHSIZE_HIGH * sizeof(xfs_dqhash_t));
82 if (!udqhash)
83 goto out;
84
85 gdqhash = kmem_zalloc_large(hsize);
Julia Lawalld67b1b02010-02-06 08:45:15 +000086 if (!gdqhash)
Christoph Hellwigbdfb0432010-01-20 21:55:30 +000087 goto out_free_udqhash;
88
Nathan Scott77e46352006-09-28 11:03:27 +100089 hsize /= sizeof(xfs_dqhash_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -070090
Nathan Scott6b3f6b52005-11-02 15:08:25 +110091 xqm = kmem_zalloc(sizeof(xfs_qm_t), KM_SLEEP);
92 xqm->qm_dqhashmask = hsize - 1;
93 xqm->qm_usr_dqhtable = udqhash;
94 xqm->qm_grp_dqhtable = gdqhash;
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 ASSERT(xqm->qm_usr_dqhtable != NULL);
96 ASSERT(xqm->qm_grp_dqhtable != NULL);
97
98 for (i = 0; i < hsize; i++) {
99 xfs_qm_list_init(&(xqm->qm_usr_dqhtable[i]), "uxdqh", i);
100 xfs_qm_list_init(&(xqm->qm_grp_dqhtable[i]), "gxdqh", i);
101 }
102
103 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 * dquot zone. we register our own low-memory callback.
105 */
106 if (!qm_dqzone) {
107 xqm->qm_dqzone = kmem_zone_init(sizeof(xfs_dquot_t),
108 "xfs_dquots");
109 qm_dqzone = xqm->qm_dqzone;
110 } else
111 xqm->qm_dqzone = qm_dqzone;
112
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 /*
114 * The t_dqinfo portion of transactions.
115 */
116 if (!qm_dqtrxzone) {
117 xqm->qm_dqtrxzone = kmem_zone_init(sizeof(xfs_dquot_acct_t),
118 "xfs_dqtrx");
119 qm_dqtrxzone = xqm->qm_dqtrxzone;
120 } else
121 xqm->qm_dqtrxzone = qm_dqtrxzone;
122
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 xqm->qm_nrefs = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 return xqm;
Christoph Hellwigbdfb0432010-01-20 21:55:30 +0000125
126 out_free_udqhash:
127 kmem_free_large(udqhash);
128 out:
129 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130}
131
132/*
133 * Destroy the global quota manager when its reference count goes to zero.
134 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +1000135STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136xfs_qm_destroy(
137 struct xfs_qm *xqm)
138{
139 int hsize, i;
140
141 ASSERT(xqm != NULL);
142 ASSERT(xqm->qm_nrefs == 0);
Christoph Hellwig80a376b2011-12-06 21:58:16 +0000143
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 hsize = xqm->qm_dqhashmask + 1;
145 for (i = 0; i < hsize; i++) {
146 xfs_qm_list_destroy(&(xqm->qm_usr_dqhtable[i]));
147 xfs_qm_list_destroy(&(xqm->qm_grp_dqhtable[i]));
148 }
Christoph Hellwigbdfb0432010-01-20 21:55:30 +0000149 kmem_free_large(xqm->qm_usr_dqhtable);
150 kmem_free_large(xqm->qm_grp_dqhtable);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 xqm->qm_usr_dqhtable = NULL;
152 xqm->qm_grp_dqhtable = NULL;
153 xqm->qm_dqhashmask = 0;
Dave Chinner3a8406f2010-04-13 15:06:52 +1000154
Denys Vlasenkof0e2d932008-05-19 16:31:57 +1000155 kmem_free(xqm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156}
157
158/*
159 * Called at mount time to let XQM know that another file system is
160 * starting quotas. This isn't crucial information as the individual mount
161 * structures are pretty independent, but it helps the XQM keep a
162 * global view of what's going on.
163 */
164/* ARGSUSED */
165STATIC int
166xfs_qm_hold_quotafs_ref(
167 struct xfs_mount *mp)
168{
169 /*
170 * Need to lock the xfs_Gqm structure for things like this. For example,
171 * the structure could disappear between the entry to this routine and
172 * a HOLD operation if not locked.
173 */
Christoph Hellwige2494582009-02-09 08:38:39 +0100174 mutex_lock(&xfs_Gqm_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175
Christoph Hellwigbdfb0432010-01-20 21:55:30 +0000176 if (!xfs_Gqm) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 xfs_Gqm = xfs_Gqm_init();
Julia Lawall38e712a2010-05-26 15:57:23 +0000178 if (!xfs_Gqm) {
179 mutex_unlock(&xfs_Gqm_lock);
Christoph Hellwigbdfb0432010-01-20 21:55:30 +0000180 return ENOMEM;
Julia Lawall38e712a2010-05-26 15:57:23 +0000181 }
Christoph Hellwigbdfb0432010-01-20 21:55:30 +0000182 }
183
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 /*
185 * We can keep a list of all filesystems with quotas mounted for
186 * debugging and statistical purposes, but ...
187 * Just take a reference and get out.
188 */
Christoph Hellwige2494582009-02-09 08:38:39 +0100189 xfs_Gqm->qm_nrefs++;
190 mutex_unlock(&xfs_Gqm_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191
192 return 0;
193}
194
195
196/*
197 * Release the reference that a filesystem took at mount time,
198 * so that we know when we need to destroy the entire quota manager.
199 */
200/* ARGSUSED */
201STATIC void
202xfs_qm_rele_quotafs_ref(
203 struct xfs_mount *mp)
204{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 ASSERT(xfs_Gqm);
206 ASSERT(xfs_Gqm->qm_nrefs > 0);
207
208 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 * Destroy the entire XQM. If somebody mounts with quotaon, this'll
210 * be restarted.
211 */
Christoph Hellwige2494582009-02-09 08:38:39 +0100212 mutex_lock(&xfs_Gqm_lock);
213 if (--xfs_Gqm->qm_nrefs == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 xfs_qm_destroy(xfs_Gqm);
215 xfs_Gqm = NULL;
216 }
Christoph Hellwige2494582009-02-09 08:38:39 +0100217 mutex_unlock(&xfs_Gqm_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218}
219
220/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 * Just destroy the quotainfo structure.
222 */
223void
Christoph Hellwig7d095252009-06-08 15:33:32 +0200224xfs_qm_unmount(
225 struct xfs_mount *mp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226{
Christoph Hellwig7d095252009-06-08 15:33:32 +0200227 if (mp->m_quotainfo) {
Christoph Hellwig8112e9d2010-04-20 17:02:29 +1000228 xfs_qm_dqpurge_all(mp, XFS_QMOPT_QUOTALL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 xfs_qm_destroy_quotainfo(mp);
Christoph Hellwig7d095252009-06-08 15:33:32 +0200230 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231}
232
233
234/*
235 * This is called from xfs_mountfs to start quotas and initialize all
236 * necessary data structures like quotainfo. This is also responsible for
237 * running a quotacheck as necessary. We are guaranteed that the superblock
238 * is consistently read in at this point.
David Chinner53aa7912008-04-10 12:20:31 +1000239 *
240 * If we fail here, the mount will continue with quota turned off. We don't
241 * need to inidicate success or failure at all.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 */
David Chinner53aa7912008-04-10 12:20:31 +1000243void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244xfs_qm_mount_quotas(
Christoph Hellwig42490232008-08-13 16:49:32 +1000245 xfs_mount_t *mp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 int error = 0;
248 uint sbf;
249
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 /*
251 * If quotas on realtime volumes is not supported, we disable
252 * quotas immediately.
253 */
254 if (mp->m_sb.sb_rextents) {
Dave Chinner0b932cc2011-03-07 10:08:35 +1100255 xfs_notice(mp, "Cannot turn on quotas for realtime filesystem");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 mp->m_qflags = 0;
257 goto write_changes;
258 }
259
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 ASSERT(XFS_IS_QUOTA_RUNNING(mp));
Nathan Scott155ffd02005-09-02 16:43:48 +1000261
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 /*
263 * Allocate the quotainfo structure inside the mount struct, and
264 * create quotainode(s), and change/rev superblock if necessary.
265 */
David Chinner53aa7912008-04-10 12:20:31 +1000266 error = xfs_qm_init_quotainfo(mp);
267 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 /*
269 * We must turn off quotas.
270 */
271 ASSERT(mp->m_quotainfo == NULL);
272 mp->m_qflags = 0;
273 goto write_changes;
274 }
275 /*
276 * If any of the quotas are not consistent, do a quotacheck.
277 */
Christoph Hellwig42490232008-08-13 16:49:32 +1000278 if (XFS_QM_NEED_QUOTACHECK(mp)) {
David Chinner53aa7912008-04-10 12:20:31 +1000279 error = xfs_qm_quotacheck(mp);
280 if (error) {
281 /* Quotacheck failed and disabled quotas. */
282 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 }
Donald Douwsma646d5bd2007-05-08 13:49:09 +1000285 /*
286 * If one type of quotas is off, then it will lose its
287 * quotachecked status, since we won't be doing accounting for
288 * that type anymore.
289 */
David Chinner53aa7912008-04-10 12:20:31 +1000290 if (!XFS_IS_UQUOTA_ON(mp))
Donald Douwsma646d5bd2007-05-08 13:49:09 +1000291 mp->m_qflags &= ~XFS_UQUOTA_CHKD;
David Chinner53aa7912008-04-10 12:20:31 +1000292 if (!(XFS_IS_GQUOTA_ON(mp) || XFS_IS_PQUOTA_ON(mp)))
Donald Douwsma646d5bd2007-05-08 13:49:09 +1000293 mp->m_qflags &= ~XFS_OQUOTA_CHKD;
Nathan Scott155ffd02005-09-02 16:43:48 +1000294
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 write_changes:
296 /*
Eric Sandeen3685c2a2007-10-11 17:42:32 +1000297 * We actually don't have to acquire the m_sb_lock at all.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 * This can only be called from mount, and that's single threaded. XXX
299 */
Eric Sandeen3685c2a2007-10-11 17:42:32 +1000300 spin_lock(&mp->m_sb_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 sbf = mp->m_sb.sb_qflags;
302 mp->m_sb.sb_qflags = mp->m_qflags & XFS_MOUNT_QUOTA_ALL;
Eric Sandeen3685c2a2007-10-11 17:42:32 +1000303 spin_unlock(&mp->m_sb_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304
305 if (sbf != (mp->m_qflags & XFS_MOUNT_QUOTA_ALL)) {
306 if (xfs_qm_write_sb_changes(mp, XFS_SB_QFLAGS)) {
307 /*
308 * We could only have been turning quotas off.
309 * We aren't in very good shape actually because
310 * the incore structures are convinced that quotas are
311 * off, but the on disk superblock doesn't know that !
312 */
313 ASSERT(!(XFS_IS_QUOTA_RUNNING(mp)));
Dave Chinner53487782011-03-07 10:05:35 +1100314 xfs_alert(mp, "%s: Superblock update failed!",
315 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 }
317 }
318
319 if (error) {
Dave Chinner53487782011-03-07 10:05:35 +1100320 xfs_warn(mp, "Failed to initialize disk quotas.");
Christoph Hellwig7d095252009-06-08 15:33:32 +0200321 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323}
324
325/*
326 * Called from the vfsops layer.
327 */
Christoph Hellwige57481d2008-12-03 12:20:36 +0100328void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329xfs_qm_unmount_quotas(
330 xfs_mount_t *mp)
331{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 /*
333 * Release the dquots that root inode, et al might be holding,
334 * before we flush quotas and blow away the quotainfo structure.
335 */
336 ASSERT(mp->m_rootip);
337 xfs_qm_dqdetach(mp->m_rootip);
338 if (mp->m_rbmip)
339 xfs_qm_dqdetach(mp->m_rbmip);
340 if (mp->m_rsumip)
341 xfs_qm_dqdetach(mp->m_rsumip);
342
343 /*
Christoph Hellwige57481d2008-12-03 12:20:36 +0100344 * Release the quota inodes.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 if (mp->m_quotainfo) {
Christoph Hellwige57481d2008-12-03 12:20:36 +0100347 if (mp->m_quotainfo->qi_uquotaip) {
348 IRELE(mp->m_quotainfo->qi_uquotaip);
349 mp->m_quotainfo->qi_uquotaip = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 }
Christoph Hellwige57481d2008-12-03 12:20:36 +0100351 if (mp->m_quotainfo->qi_gquotaip) {
352 IRELE(mp->m_quotainfo->qi_gquotaip);
353 mp->m_quotainfo->qi_gquotaip = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 }
355 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356}
357
358/*
359 * Flush all dquots of the given file system to disk. The dquots are
360 * _not_ purged from memory here, just their data written to disk.
361 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +1000362STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363xfs_qm_dqflush_all(
Christoph Hellwiga7ef9bd2011-12-06 21:58:13 +0000364 struct xfs_mount *mp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365{
Christoph Hellwig8a7b8a82010-04-20 17:01:30 +1000366 struct xfs_quotainfo *q = mp->m_quotainfo;
367 int recl;
368 struct xfs_dquot *dqp;
Christoph Hellwig8a7b8a82010-04-20 17:01:30 +1000369 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370
Christoph Hellwig8a7b8a82010-04-20 17:01:30 +1000371 if (!q)
Jesper Juhl014c2542006-01-15 02:37:08 +0100372 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373again:
Christoph Hellwig8a7b8a82010-04-20 17:01:30 +1000374 mutex_lock(&q->qi_dqlist_lock);
375 list_for_each_entry(dqp, &q->qi_dqlist, q_mplist) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 xfs_dqlock(dqp);
Christoph Hellwig92678552011-12-06 21:58:18 +0000377 if ((dqp->dq_flags & XFS_DQ_FREEING) ||
378 !XFS_DQ_IS_DIRTY(dqp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 xfs_dqunlock(dqp);
380 continue;
381 }
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000382
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 /* XXX a sentinel would be better */
Christoph Hellwig8a7b8a82010-04-20 17:01:30 +1000384 recl = q->qi_dqreclaims;
David Chinnere1f49cf2008-08-13 16:41:43 +1000385 if (!xfs_dqflock_nowait(dqp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 /*
387 * If we can't grab the flush lock then check
388 * to see if the dquot has been flushed delayed
389 * write. If so, grab its buffer and send it
390 * out immediately. We'll be able to acquire
391 * the flush lock when the I/O completes.
392 */
Christoph Hellwig800b4842011-12-06 21:58:14 +0000393 xfs_dqflock_pushbuf_wait(dqp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 }
395 /*
396 * Let go of the mplist lock. We don't want to hold it
397 * across a disk write.
398 */
Christoph Hellwig8a7b8a82010-04-20 17:01:30 +1000399 mutex_unlock(&q->qi_dqlist_lock);
Christoph Hellwiga7ef9bd2011-12-06 21:58:13 +0000400 error = xfs_qm_dqflush(dqp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 xfs_dqunlock(dqp);
402 if (error)
Jesper Juhl014c2542006-01-15 02:37:08 +0100403 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404
Christoph Hellwig8a7b8a82010-04-20 17:01:30 +1000405 mutex_lock(&q->qi_dqlist_lock);
406 if (recl != q->qi_dqreclaims) {
407 mutex_unlock(&q->qi_dqlist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 /* XXX restart limit */
409 goto again;
410 }
411 }
412
Christoph Hellwig8a7b8a82010-04-20 17:01:30 +1000413 mutex_unlock(&q->qi_dqlist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 /* return ! busy */
Jesper Juhl014c2542006-01-15 02:37:08 +0100415 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416}
Christoph Hellwig92678552011-12-06 21:58:18 +0000417
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418/*
419 * Release the group dquot pointers the user dquots may be
420 * carrying around as a hint. mplist is locked on entry and exit.
421 */
422STATIC void
423xfs_qm_detach_gdquots(
Christoph Hellwig8a7b8a82010-04-20 17:01:30 +1000424 struct xfs_mount *mp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425{
Christoph Hellwig8a7b8a82010-04-20 17:01:30 +1000426 struct xfs_quotainfo *q = mp->m_quotainfo;
427 struct xfs_dquot *dqp, *gdqp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428
429 again:
Christoph Hellwig8a7b8a82010-04-20 17:01:30 +1000430 ASSERT(mutex_is_locked(&q->qi_dqlist_lock));
431 list_for_each_entry(dqp, &q->qi_dqlist, q_mplist) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 xfs_dqlock(dqp);
Christoph Hellwig92678552011-12-06 21:58:18 +0000433 if (dqp->dq_flags & XFS_DQ_FREEING) {
434 xfs_dqunlock(dqp);
435 mutex_unlock(&q->qi_dqlist_lock);
436 delay(1);
437 mutex_lock(&q->qi_dqlist_lock);
438 goto again;
439 }
Christoph Hellwig28fb5882011-12-06 21:58:21 +0000440
441 gdqp = dqp->q_gdquot;
442 if (gdqp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 dqp->q_gdquot = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 xfs_dqunlock(dqp);
445
Christoph Hellwig28fb5882011-12-06 21:58:21 +0000446 if (gdqp)
447 xfs_qm_dqrele(gdqp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 }
449}
450
451/*
452 * Go through all the incore dquots of this file system and take them
453 * off the mplist and hashlist, if the dquot type matches the dqtype
454 * parameter. This is used when turning off quota accounting for
455 * users and/or groups, as well as when the filesystem is unmounting.
456 */
457STATIC int
458xfs_qm_dqpurge_int(
Christoph Hellwig8a7b8a82010-04-20 17:01:30 +1000459 struct xfs_mount *mp,
460 uint flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461{
Christoph Hellwig8a7b8a82010-04-20 17:01:30 +1000462 struct xfs_quotainfo *q = mp->m_quotainfo;
463 struct xfs_dquot *dqp, *n;
464 uint dqtype;
Christoph Hellwig92678552011-12-06 21:58:18 +0000465 int nmisses = 0;
466 LIST_HEAD (dispose_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467
Christoph Hellwig8a7b8a82010-04-20 17:01:30 +1000468 if (!q)
Jesper Juhl014c2542006-01-15 02:37:08 +0100469 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470
471 dqtype = (flags & XFS_QMOPT_UQUOTA) ? XFS_DQ_USER : 0;
Nathan Scottc8ad20f2005-06-21 15:38:48 +1000472 dqtype |= (flags & XFS_QMOPT_PQUOTA) ? XFS_DQ_PROJ : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 dqtype |= (flags & XFS_QMOPT_GQUOTA) ? XFS_DQ_GROUP : 0;
474
Christoph Hellwig8a7b8a82010-04-20 17:01:30 +1000475 mutex_lock(&q->qi_dqlist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476
477 /*
478 * In the first pass through all incore dquots of this filesystem,
479 * we release the group dquot pointers the user dquots may be
480 * carrying around as a hint. We need to do this irrespective of
481 * what's being turned off.
482 */
483 xfs_qm_detach_gdquots(mp);
484
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 /*
Christoph Hellwig92678552011-12-06 21:58:18 +0000486 * Try to get rid of all of the unwanted dquots.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 */
Christoph Hellwig8a7b8a82010-04-20 17:01:30 +1000488 list_for_each_entry_safe(dqp, n, &q->qi_dqlist, q_mplist) {
Christoph Hellwigbe7ffc32011-12-06 21:58:17 +0000489 xfs_dqlock(dqp);
Christoph Hellwig92678552011-12-06 21:58:18 +0000490 if ((dqp->dq_flags & dqtype) != 0 &&
491 !(dqp->dq_flags & XFS_DQ_FREEING)) {
492 if (dqp->q_nrefs == 0) {
493 dqp->dq_flags |= XFS_DQ_FREEING;
494 list_move_tail(&dqp->q_mplist, &dispose_list);
495 } else
496 nmisses++;
Christoph Hellwigbe7ffc32011-12-06 21:58:17 +0000497 }
498 xfs_dqunlock(dqp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 }
Christoph Hellwig8a7b8a82010-04-20 17:01:30 +1000500 mutex_unlock(&q->qi_dqlist_lock);
Christoph Hellwig92678552011-12-06 21:58:18 +0000501
502 list_for_each_entry_safe(dqp, n, &dispose_list, q_mplist)
503 xfs_qm_dqpurge(dqp);
504
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 return nmisses;
506}
507
508int
509xfs_qm_dqpurge_all(
510 xfs_mount_t *mp,
511 uint flags)
512{
513 int ndquots;
514
515 /*
516 * Purge the dquot cache.
517 * None of the dquots should really be busy at this point.
518 */
519 if (mp->m_quotainfo) {
520 while ((ndquots = xfs_qm_dqpurge_int(mp, flags))) {
521 delay(ndquots * 10);
522 }
523 }
524 return 0;
525}
526
527STATIC int
528xfs_qm_dqattach_one(
529 xfs_inode_t *ip,
530 xfs_dqid_t id,
531 uint type,
532 uint doalloc,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 xfs_dquot_t *udqhint, /* hint */
534 xfs_dquot_t **IO_idqpp)
535{
536 xfs_dquot_t *dqp;
537 int error;
538
Christoph Hellwig579aa9c2008-04-22 17:34:00 +1000539 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 error = 0;
Christoph Hellwig8e9b6e72009-02-08 21:51:42 +0100541
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 /*
543 * See if we already have it in the inode itself. IO_idqpp is
544 * &i_udquot or &i_gdquot. This made the code look weird, but
545 * made the logic a lot simpler.
546 */
Christoph Hellwig8e9b6e72009-02-08 21:51:42 +0100547 dqp = *IO_idqpp;
548 if (dqp) {
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000549 trace_xfs_dqattach_found(dqp);
Christoph Hellwig8e9b6e72009-02-08 21:51:42 +0100550 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 }
552
553 /*
554 * udqhint is the i_udquot field in inode, and is non-NULL only
Nathan Scottc8ad20f2005-06-21 15:38:48 +1000555 * when the type arg is group/project. Its purpose is to save a
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 * lookup by dqid (xfs_qm_dqget) by caching a group dquot inside
557 * the user dquot.
558 */
Christoph Hellwig8e9b6e72009-02-08 21:51:42 +0100559 if (udqhint) {
560 ASSERT(type == XFS_DQ_GROUP || type == XFS_DQ_PROJ);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 xfs_dqlock(udqhint);
562
Christoph Hellwig8e9b6e72009-02-08 21:51:42 +0100563 /*
564 * No need to take dqlock to look at the id.
565 *
566 * The ID can't change until it gets reclaimed, and it won't
567 * be reclaimed as long as we have a ref from inode and we
568 * hold the ilock.
569 */
570 dqp = udqhint->q_gdquot;
571 if (dqp && be32_to_cpu(dqp->q_core.d_id) == id) {
Christoph Hellwig8e9b6e72009-02-08 21:51:42 +0100572 ASSERT(*IO_idqpp == NULL);
Christoph Hellwig8e9b6e72009-02-08 21:51:42 +0100573
Christoph Hellwig78e55892011-12-06 21:58:22 +0000574 *IO_idqpp = xfs_qm_dqhold(dqp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 xfs_dqunlock(udqhint);
Christoph Hellwig8e9b6e72009-02-08 21:51:42 +0100576 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 }
Christoph Hellwig8e9b6e72009-02-08 21:51:42 +0100578
579 /*
580 * We can't hold a dquot lock when we call the dqget code.
581 * We'll deadlock in no time, because of (not conforming to)
582 * lock ordering - the inodelock comes before any dquot lock,
583 * and we may drop and reacquire the ilock in xfs_qm_dqget().
584 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 xfs_dqunlock(udqhint);
Christoph Hellwig8e9b6e72009-02-08 21:51:42 +0100586 }
587
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 /*
589 * Find the dquot from somewhere. This bumps the
590 * reference count of dquot and returns it locked.
591 * This can return ENOENT if dquot didn't exist on
592 * disk and we didn't ask it to allocate;
593 * ESRCH if quotas got turned off suddenly.
594 */
Mitsuo Hayasakadb3e74b2011-11-10 01:33:10 +0000595 error = xfs_qm_dqget(ip->i_mount, ip, id, type,
596 doalloc | XFS_QMOPT_DOWARN, &dqp);
Christoph Hellwig8e9b6e72009-02-08 21:51:42 +0100597 if (error)
598 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000600 trace_xfs_dqattach_get(dqp);
Christoph Hellwig8e9b6e72009-02-08 21:51:42 +0100601
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 /*
603 * dqget may have dropped and re-acquired the ilock, but it guarantees
604 * that the dquot returned is the one that should go in the inode.
605 */
606 *IO_idqpp = dqp;
Christoph Hellwig8e9b6e72009-02-08 21:51:42 +0100607 xfs_dqunlock(dqp);
608 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609}
610
611
612/*
613 * Given a udquot and gdquot, attach a ptr to the group dquot in the
Christoph Hellwigab680bb2011-12-06 21:58:20 +0000614 * udquot as a hint for future lookups.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 */
616STATIC void
617xfs_qm_dqattach_grouphint(
618 xfs_dquot_t *udq,
Christoph Hellwig8e9b6e72009-02-08 21:51:42 +0100619 xfs_dquot_t *gdq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620{
621 xfs_dquot_t *tmp;
622
Christoph Hellwig8e9b6e72009-02-08 21:51:42 +0100623 xfs_dqlock(udq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624
Christoph Hellwigab680bb2011-12-06 21:58:20 +0000625 tmp = udq->q_gdquot;
626 if (tmp) {
627 if (tmp == gdq)
628 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629
630 udq->q_gdquot = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 xfs_qm_dqrele(tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 }
633
Christoph Hellwig78e55892011-12-06 21:58:22 +0000634 udq->q_gdquot = xfs_qm_dqhold(gdq);
Christoph Hellwigab680bb2011-12-06 21:58:20 +0000635done:
Christoph Hellwig8e9b6e72009-02-08 21:51:42 +0100636 xfs_dqunlock(udq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637}
638
639
640/*
Nathan Scottc8ad20f2005-06-21 15:38:48 +1000641 * Given a locked inode, attach dquot(s) to it, taking U/G/P-QUOTAON
642 * into account.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 * If XFS_QMOPT_DQALLOC, the dquot(s) will be allocated if needed.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 * Inode may get unlocked and relocked in here, and the caller must deal with
645 * the consequences.
646 */
647int
Christoph Hellwig7d095252009-06-08 15:33:32 +0200648xfs_qm_dqattach_locked(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 xfs_inode_t *ip,
650 uint flags)
651{
652 xfs_mount_t *mp = ip->i_mount;
653 uint nquotas = 0;
654 int error = 0;
655
Christoph Hellwig7d095252009-06-08 15:33:32 +0200656 if (!XFS_IS_QUOTA_RUNNING(mp) ||
657 !XFS_IS_QUOTA_ON(mp) ||
658 !XFS_NOT_DQATTACHED(mp, ip) ||
659 ip->i_ino == mp->m_sb.sb_uquotino ||
660 ip->i_ino == mp->m_sb.sb_gquotino)
Jesper Juhl014c2542006-01-15 02:37:08 +0100661 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662
Christoph Hellwig7d095252009-06-08 15:33:32 +0200663 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664
665 if (XFS_IS_UQUOTA_ON(mp)) {
666 error = xfs_qm_dqattach_one(ip, ip->i_d.di_uid, XFS_DQ_USER,
667 flags & XFS_QMOPT_DQALLOC,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 NULL, &ip->i_udquot);
669 if (error)
670 goto done;
671 nquotas++;
672 }
Christoph Hellwig579aa9c2008-04-22 17:34:00 +1000673
674 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
Nathan Scottc8ad20f2005-06-21 15:38:48 +1000675 if (XFS_IS_OQUOTA_ON(mp)) {
676 error = XFS_IS_GQUOTA_ON(mp) ?
677 xfs_qm_dqattach_one(ip, ip->i_d.di_gid, XFS_DQ_GROUP,
678 flags & XFS_QMOPT_DQALLOC,
Nathan Scottc8ad20f2005-06-21 15:38:48 +1000679 ip->i_udquot, &ip->i_gdquot) :
Arkadiusz Mi?kiewicz67430992010-09-26 06:10:18 +0000680 xfs_qm_dqattach_one(ip, xfs_get_projid(ip), XFS_DQ_PROJ,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 flags & XFS_QMOPT_DQALLOC,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 ip->i_udquot, &ip->i_gdquot);
683 /*
684 * Don't worry about the udquot that we may have
685 * attached above. It'll get detached, if not already.
686 */
687 if (error)
688 goto done;
689 nquotas++;
690 }
691
692 /*
693 * Attach this group quota to the user quota as a hint.
694 * This WON'T, in general, result in a thrash.
695 */
696 if (nquotas == 2) {
Christoph Hellwig579aa9c2008-04-22 17:34:00 +1000697 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 ASSERT(ip->i_udquot);
699 ASSERT(ip->i_gdquot);
700
701 /*
Christoph Hellwigab680bb2011-12-06 21:58:20 +0000702 * We do not have i_udquot locked at this point, but this check
703 * is OK since we don't depend on the i_gdquot to be accurate
704 * 100% all the time. It is just a hint, and this will
705 * succeed in general.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 */
Christoph Hellwigab680bb2011-12-06 21:58:20 +0000707 if (ip->i_udquot->q_gdquot != ip->i_gdquot)
708 xfs_qm_dqattach_grouphint(ip->i_udquot, ip->i_gdquot);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 }
710
Christoph Hellwig7d095252009-06-08 15:33:32 +0200711 done:
Christoph Hellwigea15ab32011-07-13 13:43:50 +0200712#ifdef DEBUG
713 if (!error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 if (XFS_IS_UQUOTA_ON(mp))
715 ASSERT(ip->i_udquot);
Nathan Scottc8ad20f2005-06-21 15:38:48 +1000716 if (XFS_IS_OQUOTA_ON(mp))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 ASSERT(ip->i_gdquot);
718 }
Christoph Hellwig7d095252009-06-08 15:33:32 +0200719 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720#endif
Christoph Hellwig7d095252009-06-08 15:33:32 +0200721 return error;
722}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723
Christoph Hellwig7d095252009-06-08 15:33:32 +0200724int
725xfs_qm_dqattach(
726 struct xfs_inode *ip,
727 uint flags)
728{
729 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730
Christoph Hellwig7d095252009-06-08 15:33:32 +0200731 xfs_ilock(ip, XFS_ILOCK_EXCL);
732 error = xfs_qm_dqattach_locked(ip, flags);
733 xfs_iunlock(ip, XFS_ILOCK_EXCL);
734
Jesper Juhl014c2542006-01-15 02:37:08 +0100735 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736}
737
738/*
739 * Release dquots (and their references) if any.
740 * The inode should be locked EXCL except when this's called by
741 * xfs_ireclaim.
742 */
743void
744xfs_qm_dqdetach(
745 xfs_inode_t *ip)
746{
747 if (!(ip->i_udquot || ip->i_gdquot))
748 return;
749
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000750 trace_xfs_dquot_dqdetach(ip);
751
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 ASSERT(ip->i_ino != ip->i_mount->m_sb.sb_uquotino);
753 ASSERT(ip->i_ino != ip->i_mount->m_sb.sb_gquotino);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 if (ip->i_udquot) {
755 xfs_qm_dqrele(ip->i_udquot);
756 ip->i_udquot = NULL;
757 }
758 if (ip->i_gdquot) {
759 xfs_qm_dqrele(ip->i_gdquot);
760 ip->i_gdquot = NULL;
761 }
762}
763
Christoph Hellwiga4edd1d2009-01-19 02:03:11 +0100764/*
765 * The hash chains and the mplist use the same xfs_dqhash structure as
766 * their list head, but we can take the mplist qh_lock and one of the
767 * hash qh_locks at the same time without any problem as they aren't
768 * related.
769 */
770static struct lock_class_key xfs_quota_mplist_class;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771
772/*
773 * This initializes all the quota information that's kept in the
774 * mount structure
775 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +1000776STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777xfs_qm_init_quotainfo(
778 xfs_mount_t *mp)
779{
780 xfs_quotainfo_t *qinf;
781 int error;
782 xfs_dquot_t *dqp;
783
784 ASSERT(XFS_IS_QUOTA_RUNNING(mp));
785
786 /*
787 * Tell XQM that we exist as soon as possible.
788 */
789 if ((error = xfs_qm_hold_quotafs_ref(mp))) {
Jesper Juhl014c2542006-01-15 02:37:08 +0100790 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 }
792
793 qinf = mp->m_quotainfo = kmem_zalloc(sizeof(xfs_quotainfo_t), KM_SLEEP);
794
795 /*
796 * See if quotainodes are setup, and if not, allocate them,
797 * and change the superblock accordingly.
798 */
799 if ((error = xfs_qm_init_quotainos(mp))) {
Denys Vlasenkof0e2d932008-05-19 16:31:57 +1000800 kmem_free(qinf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 mp->m_quotainfo = NULL;
Jesper Juhl014c2542006-01-15 02:37:08 +0100802 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 }
804
Dave Chinner3a254042010-04-13 15:06:48 +1000805 INIT_LIST_HEAD(&qinf->qi_dqlist);
806 mutex_init(&qinf->qi_dqlist_lock);
807 lockdep_set_class(&qinf->qi_dqlist_lock, &xfs_quota_mplist_class);
Christoph Hellwiga4edd1d2009-01-19 02:03:11 +0100808
Christoph Hellwigf8739c32012-03-13 08:52:34 +0000809 INIT_LIST_HEAD(&qinf->qi_lru_list);
810 qinf->qi_lru_count = 0;
811 mutex_init(&qinf->qi_lru_lock);
812
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 qinf->qi_dqreclaims = 0;
814
815 /* mutex used to serialize quotaoffs */
Jes Sorensen794ee1b2006-01-09 15:59:21 -0800816 mutex_init(&qinf->qi_quotaofflock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817
818 /* Precalc some constants */
819 qinf->qi_dqchunklen = XFS_FSB_TO_BB(mp, XFS_DQUOT_CLUSTER_SIZE_FSB);
820 ASSERT(qinf->qi_dqchunklen);
821 qinf->qi_dqperchunk = BBTOB(qinf->qi_dqchunklen);
822 do_div(qinf->qi_dqperchunk, sizeof(xfs_dqblk_t));
823
824 mp->m_qflags |= (mp->m_sb.sb_qflags & XFS_ALL_QUOTA_CHKD);
825
826 /*
827 * We try to get the limits from the superuser's limits fields.
828 * This is quite hacky, but it is standard quota practice.
Christoph Hellwig7ae44402011-12-06 21:58:25 +0000829 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 * We look at the USR dquot with id == 0 first, but if user quotas
831 * are not enabled we goto the GRP dquot with id == 0.
832 * We don't really care to keep separate default limits for user
833 * and group quotas, at least not at this point.
Christoph Hellwig7ae44402011-12-06 21:58:25 +0000834 *
835 * Since we may not have done a quotacheck by this point, just read
836 * the dquot without attaching it to any hashtables or lists.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 */
Christoph Hellwig7ae44402011-12-06 21:58:25 +0000838 error = xfs_qm_dqread(mp, 0,
839 XFS_IS_UQUOTA_RUNNING(mp) ? XFS_DQ_USER :
840 (XFS_IS_GQUOTA_RUNNING(mp) ? XFS_DQ_GROUP :
841 XFS_DQ_PROJ),
842 XFS_QMOPT_DOWARN, &dqp);
843 if (!error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 xfs_disk_dquot_t *ddqp = &dqp->q_core;
845
846 /*
847 * The warnings and timers set the grace period given to
848 * a user or group before he or she can not perform any
849 * more writing. If it is zero, a default is used.
850 */
Christoph Hellwig1149d962005-11-02 15:01:12 +1100851 qinf->qi_btimelimit = ddqp->d_btimer ?
852 be32_to_cpu(ddqp->d_btimer) : XFS_QM_BTIMELIMIT;
853 qinf->qi_itimelimit = ddqp->d_itimer ?
854 be32_to_cpu(ddqp->d_itimer) : XFS_QM_ITIMELIMIT;
855 qinf->qi_rtbtimelimit = ddqp->d_rtbtimer ?
856 be32_to_cpu(ddqp->d_rtbtimer) : XFS_QM_RTBTIMELIMIT;
857 qinf->qi_bwarnlimit = ddqp->d_bwarns ?
858 be16_to_cpu(ddqp->d_bwarns) : XFS_QM_BWARNLIMIT;
859 qinf->qi_iwarnlimit = ddqp->d_iwarns ?
860 be16_to_cpu(ddqp->d_iwarns) : XFS_QM_IWARNLIMIT;
861 qinf->qi_rtbwarnlimit = ddqp->d_rtbwarns ?
862 be16_to_cpu(ddqp->d_rtbwarns) : XFS_QM_RTBWARNLIMIT;
863 qinf->qi_bhardlimit = be64_to_cpu(ddqp->d_blk_hardlimit);
864 qinf->qi_bsoftlimit = be64_to_cpu(ddqp->d_blk_softlimit);
865 qinf->qi_ihardlimit = be64_to_cpu(ddqp->d_ino_hardlimit);
866 qinf->qi_isoftlimit = be64_to_cpu(ddqp->d_ino_softlimit);
867 qinf->qi_rtbhardlimit = be64_to_cpu(ddqp->d_rtb_hardlimit);
868 qinf->qi_rtbsoftlimit = be64_to_cpu(ddqp->d_rtb_softlimit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 xfs_qm_dqdestroy(dqp);
871 } else {
872 qinf->qi_btimelimit = XFS_QM_BTIMELIMIT;
873 qinf->qi_itimelimit = XFS_QM_ITIMELIMIT;
874 qinf->qi_rtbtimelimit = XFS_QM_RTBTIMELIMIT;
875 qinf->qi_bwarnlimit = XFS_QM_BWARNLIMIT;
876 qinf->qi_iwarnlimit = XFS_QM_IWARNLIMIT;
Nathan Scott06d10dd2005-06-21 15:48:47 +1000877 qinf->qi_rtbwarnlimit = XFS_QM_RTBWARNLIMIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 }
879
Christoph Hellwigf8739c32012-03-13 08:52:34 +0000880 qinf->qi_shrinker.shrink = xfs_qm_shake;
881 qinf->qi_shrinker.seeks = DEFAULT_SEEKS;
882 register_shrinker(&qinf->qi_shrinker);
Jesper Juhl014c2542006-01-15 02:37:08 +0100883 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884}
885
886
887/*
888 * Gets called when unmounting a filesystem or when all quotas get
889 * turned off.
890 * This purges the quota inodes, destroys locks and frees itself.
891 */
892void
893xfs_qm_destroy_quotainfo(
894 xfs_mount_t *mp)
895{
896 xfs_quotainfo_t *qi;
897
898 qi = mp->m_quotainfo;
899 ASSERT(qi != NULL);
900 ASSERT(xfs_Gqm != NULL);
901
Christoph Hellwigf8739c32012-03-13 08:52:34 +0000902 unregister_shrinker(&qi->qi_shrinker);
903
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 /*
905 * Release the reference that XQM kept, so that we know
906 * when the XQM structure should be freed. We cannot assume
907 * that xfs_Gqm is non-null after this point.
908 */
909 xfs_qm_rele_quotafs_ref(mp);
910
Dave Chinner3a254042010-04-13 15:06:48 +1000911 ASSERT(list_empty(&qi->qi_dqlist));
912 mutex_destroy(&qi->qi_dqlist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913
914 if (qi->qi_uquotaip) {
Christoph Hellwig26cc0022008-07-18 17:12:43 +1000915 IRELE(qi->qi_uquotaip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 qi->qi_uquotaip = NULL; /* paranoia */
917 }
918 if (qi->qi_gquotaip) {
Christoph Hellwig26cc0022008-07-18 17:12:43 +1000919 IRELE(qi->qi_gquotaip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 qi->qi_gquotaip = NULL;
921 }
922 mutex_destroy(&qi->qi_quotaofflock);
Denys Vlasenkof0e2d932008-05-19 16:31:57 +1000923 kmem_free(qi);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 mp->m_quotainfo = NULL;
925}
926
927
928
929/* ------------------- PRIVATE STATIC FUNCTIONS ----------------------- */
930
931/* ARGSUSED */
932STATIC void
933xfs_qm_list_init(
934 xfs_dqlist_t *list,
935 char *str,
936 int n)
937{
Jes Sorensen794ee1b2006-01-09 15:59:21 -0800938 mutex_init(&list->qh_lock);
Dave Chinnere6a81f12010-04-13 15:06:51 +1000939 INIT_LIST_HEAD(&list->qh_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 list->qh_version = 0;
941 list->qh_nelems = 0;
942}
943
944STATIC void
945xfs_qm_list_destroy(
946 xfs_dqlist_t *list)
947{
948 mutex_destroy(&(list->qh_lock));
949}
950
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951/*
952 * Create an inode and return with a reference already taken, but unlocked
953 * This is how we create quota inodes
954 */
955STATIC int
956xfs_qm_qino_alloc(
957 xfs_mount_t *mp,
958 xfs_inode_t **ip,
959 __int64_t sbfields,
960 uint flags)
961{
962 xfs_trans_t *tp;
963 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 int committed;
965
Nathan Scott061f7202006-01-11 15:27:50 +1100966 tp = xfs_trans_alloc(mp, XFS_TRANS_QM_QINOCREATE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 if ((error = xfs_trans_reserve(tp,
968 XFS_QM_QINOCREATE_SPACE_RES(mp),
969 XFS_CREATE_LOG_RES(mp), 0,
970 XFS_TRANS_PERM_LOG_RES,
971 XFS_CREATE_LOG_COUNT))) {
972 xfs_trans_cancel(tp, 0);
Jesper Juhl014c2542006-01-15 02:37:08 +0100973 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975
Christoph Hellwig6c77b0e2010-10-06 18:41:17 +0000976 error = xfs_dir_ialloc(&tp, NULL, S_IFREG, 1, 0, 0, 1, ip, &committed);
977 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES |
979 XFS_TRANS_ABORT);
Jesper Juhl014c2542006-01-15 02:37:08 +0100980 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 }
982
983 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 * Make the changes in the superblock, and log those too.
985 * sbfields arg may contain fields other than *QUOTINO;
986 * VERSIONNUM for example.
987 */
Eric Sandeen3685c2a2007-10-11 17:42:32 +1000988 spin_lock(&mp->m_sb_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 if (flags & XFS_QMOPT_SBVERSION) {
Eric Sandeen62118702008-03-06 13:44:28 +1100990 ASSERT(!xfs_sb_version_hasquota(&mp->m_sb));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 ASSERT((sbfields & (XFS_SB_VERSIONNUM | XFS_SB_UQUOTINO |
992 XFS_SB_GQUOTINO | XFS_SB_QFLAGS)) ==
993 (XFS_SB_VERSIONNUM | XFS_SB_UQUOTINO |
994 XFS_SB_GQUOTINO | XFS_SB_QFLAGS));
995
Eric Sandeen62118702008-03-06 13:44:28 +1100996 xfs_sb_version_addquota(&mp->m_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 mp->m_sb.sb_uquotino = NULLFSINO;
998 mp->m_sb.sb_gquotino = NULLFSINO;
999
1000 /* qflags will get updated _after_ quotacheck */
1001 mp->m_sb.sb_qflags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 }
1003 if (flags & XFS_QMOPT_UQUOTA)
1004 mp->m_sb.sb_uquotino = (*ip)->i_ino;
1005 else
1006 mp->m_sb.sb_gquotino = (*ip)->i_ino;
Eric Sandeen3685c2a2007-10-11 17:42:32 +10001007 spin_unlock(&mp->m_sb_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 xfs_mod_sb(tp, sbfields);
1009
Eric Sandeen1c72bf92007-05-08 13:48:42 +10001010 if ((error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES))) {
Dave Chinner53487782011-03-07 10:05:35 +11001011 xfs_alert(mp, "%s failed (error %d)!", __func__, error);
Jesper Juhl014c2542006-01-15 02:37:08 +01001012 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 }
Jesper Juhl014c2542006-01-15 02:37:08 +01001014 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015}
1016
1017
David Chinner5b139732008-04-10 12:20:10 +10001018STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019xfs_qm_reset_dqcounts(
1020 xfs_mount_t *mp,
1021 xfs_buf_t *bp,
1022 xfs_dqid_t id,
1023 uint type)
1024{
1025 xfs_disk_dquot_t *ddq;
1026 int j;
1027
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001028 trace_xfs_reset_dqcounts(bp, _RET_IP_);
1029
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 /*
1031 * Reset all counters and timers. They'll be
1032 * started afresh by xfs_qm_quotacheck.
1033 */
1034#ifdef DEBUG
1035 j = XFS_FSB_TO_B(mp, XFS_DQUOT_CLUSTER_SIZE_FSB);
1036 do_div(j, sizeof(xfs_dqblk_t));
Christoph Hellwig8a7b8a82010-04-20 17:01:30 +10001037 ASSERT(mp->m_quotainfo->qi_dqperchunk == j);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038#endif
Chandra Seetharaman62926042011-07-22 23:40:15 +00001039 ddq = bp->b_addr;
Christoph Hellwig8a7b8a82010-04-20 17:01:30 +10001040 for (j = 0; j < mp->m_quotainfo->qi_dqperchunk; j++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 /*
1042 * Do a sanity check, and if needed, repair the dqblk. Don't
1043 * output any warnings because it's perfectly possible to
Nathan Scottc41564b2006-03-29 08:55:14 +10001044 * find uninitialised dquot blks. See comment in xfs_qm_dqcheck.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 */
Dave Chinnera0fa2b62011-03-07 10:01:35 +11001046 (void) xfs_qm_dqcheck(mp, ddq, id+j, type, XFS_QMOPT_DQREPAIR,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 "xfs_quotacheck");
Christoph Hellwig1149d962005-11-02 15:01:12 +11001048 ddq->d_bcount = 0;
1049 ddq->d_icount = 0;
1050 ddq->d_rtbcount = 0;
1051 ddq->d_btimer = 0;
1052 ddq->d_itimer = 0;
1053 ddq->d_rtbtimer = 0;
1054 ddq->d_bwarns = 0;
1055 ddq->d_iwarns = 0;
1056 ddq->d_rtbwarns = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 ddq = (xfs_disk_dquot_t *) ((xfs_dqblk_t *)ddq + 1);
1058 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059}
1060
1061STATIC int
1062xfs_qm_dqiter_bufs(
1063 xfs_mount_t *mp,
1064 xfs_dqid_t firstid,
1065 xfs_fsblock_t bno,
1066 xfs_filblks_t blkcnt,
1067 uint flags)
1068{
1069 xfs_buf_t *bp;
1070 int error;
Nathan Scottc8ad20f2005-06-21 15:38:48 +10001071 int type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072
1073 ASSERT(blkcnt > 0);
Nathan Scottc8ad20f2005-06-21 15:38:48 +10001074 type = flags & XFS_QMOPT_UQUOTA ? XFS_DQ_USER :
1075 (flags & XFS_QMOPT_PQUOTA ? XFS_DQ_PROJ : XFS_DQ_GROUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 error = 0;
1077
1078 /*
1079 * Blkcnt arg can be a very big number, and might even be
1080 * larger than the log itself. So, we have to break it up into
1081 * manageable-sized transactions.
1082 * Note that we don't start a permanent transaction here; we might
1083 * not be able to get a log reservation for the whole thing up front,
1084 * and we don't really care to either, because we just discard
1085 * everything if we were to crash in the middle of this loop.
1086 */
1087 while (blkcnt--) {
1088 error = xfs_trans_read_buf(mp, NULL, mp->m_ddev_targp,
1089 XFS_FSB_TO_DADDR(mp, bno),
Christoph Hellwig8a7b8a82010-04-20 17:01:30 +10001090 mp->m_quotainfo->qi_dqchunklen, 0, &bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 if (error)
1092 break;
1093
David Chinner5b139732008-04-10 12:20:10 +10001094 xfs_qm_reset_dqcounts(mp, bp, firstid, type);
Christoph Hellwig61551f12011-08-23 08:28:06 +00001095 xfs_buf_delwri_queue(bp);
1096 xfs_buf_relse(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 /*
1098 * goto the next block.
1099 */
1100 bno++;
Christoph Hellwig8a7b8a82010-04-20 17:01:30 +10001101 firstid += mp->m_quotainfo->qi_dqperchunk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 }
Jesper Juhl014c2542006-01-15 02:37:08 +01001103 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104}
1105
1106/*
Nathan Scottc8ad20f2005-06-21 15:38:48 +10001107 * Iterate over all allocated USR/GRP/PRJ dquots in the system, calling a
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 * caller supplied function for every chunk of dquots that we find.
1109 */
1110STATIC int
1111xfs_qm_dqiterate(
1112 xfs_mount_t *mp,
1113 xfs_inode_t *qip,
1114 uint flags)
1115{
1116 xfs_bmbt_irec_t *map;
1117 int i, nmaps; /* number of map entries */
1118 int error; /* return value */
1119 xfs_fileoff_t lblkno;
1120 xfs_filblks_t maxlblkcnt;
1121 xfs_dqid_t firstid;
1122 xfs_fsblock_t rablkno;
1123 xfs_filblks_t rablkcnt;
1124
1125 error = 0;
1126 /*
Nathan Scottc41564b2006-03-29 08:55:14 +10001127 * This looks racy, but we can't keep an inode lock across a
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 * trans_reserve. But, this gets called during quotacheck, and that
1129 * happens only at mount time which is single threaded.
1130 */
1131 if (qip->i_d.di_nblocks == 0)
Jesper Juhl014c2542006-01-15 02:37:08 +01001132 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133
1134 map = kmem_alloc(XFS_DQITER_MAP_SIZE * sizeof(*map), KM_SLEEP);
1135
1136 lblkno = 0;
1137 maxlblkcnt = XFS_B_TO_FSB(mp, (xfs_ufsize_t)XFS_MAXIOFFSET(mp));
1138 do {
1139 nmaps = XFS_DQITER_MAP_SIZE;
1140 /*
1141 * We aren't changing the inode itself. Just changing
1142 * some of its data. No new blocks are added here, and
1143 * the inode is never added to the transaction.
1144 */
1145 xfs_ilock(qip, XFS_ILOCK_SHARED);
Dave Chinner5c8ed202011-09-18 20:40:45 +00001146 error = xfs_bmapi_read(qip, lblkno, maxlblkcnt - lblkno,
1147 map, &nmaps, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 xfs_iunlock(qip, XFS_ILOCK_SHARED);
1149 if (error)
1150 break;
1151
1152 ASSERT(nmaps <= XFS_DQITER_MAP_SIZE);
1153 for (i = 0; i < nmaps; i++) {
1154 ASSERT(map[i].br_startblock != DELAYSTARTBLOCK);
1155 ASSERT(map[i].br_blockcount);
1156
1157
1158 lblkno += map[i].br_blockcount;
1159
1160 if (map[i].br_startblock == HOLESTARTBLOCK)
1161 continue;
1162
1163 firstid = (xfs_dqid_t) map[i].br_startoff *
Christoph Hellwig8a7b8a82010-04-20 17:01:30 +10001164 mp->m_quotainfo->qi_dqperchunk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 /*
1166 * Do a read-ahead on the next extent.
1167 */
1168 if ((i+1 < nmaps) &&
1169 (map[i+1].br_startblock != HOLESTARTBLOCK)) {
1170 rablkcnt = map[i+1].br_blockcount;
1171 rablkno = map[i+1].br_startblock;
1172 while (rablkcnt--) {
Christoph Hellwig1a1a3e92010-10-06 18:41:18 +00001173 xfs_buf_readahead(mp->m_ddev_targp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 XFS_FSB_TO_DADDR(mp, rablkno),
Christoph Hellwig8a7b8a82010-04-20 17:01:30 +10001175 mp->m_quotainfo->qi_dqchunklen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 rablkno++;
1177 }
1178 }
1179 /*
1180 * Iterate thru all the blks in the extent and
1181 * reset the counters of all the dquots inside them.
1182 */
1183 if ((error = xfs_qm_dqiter_bufs(mp,
1184 firstid,
1185 map[i].br_startblock,
1186 map[i].br_blockcount,
1187 flags))) {
1188 break;
1189 }
1190 }
1191
1192 if (error)
1193 break;
1194 } while (nmaps > 0);
1195
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10001196 kmem_free(map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197
Jesper Juhl014c2542006-01-15 02:37:08 +01001198 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199}
1200
1201/*
1202 * Called by dqusage_adjust in doing a quotacheck.
Christoph Hellwig52fda112010-09-06 01:44:22 +00001203 *
1204 * Given the inode, and a dquot id this updates both the incore dqout as well
1205 * as the buffer copy. This is so that once the quotacheck is done, we can
1206 * just log all the buffers, as opposed to logging numerous updates to
1207 * individual dquots.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 */
Christoph Hellwig52fda112010-09-06 01:44:22 +00001209STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210xfs_qm_quotacheck_dqadjust(
Christoph Hellwig52fda112010-09-06 01:44:22 +00001211 struct xfs_inode *ip,
1212 xfs_dqid_t id,
1213 uint type,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 xfs_qcnt_t nblks,
1215 xfs_qcnt_t rtblks)
1216{
Christoph Hellwig52fda112010-09-06 01:44:22 +00001217 struct xfs_mount *mp = ip->i_mount;
1218 struct xfs_dquot *dqp;
1219 int error;
1220
1221 error = xfs_qm_dqget(mp, ip, id, type,
1222 XFS_QMOPT_DQALLOC | XFS_QMOPT_DOWARN, &dqp);
1223 if (error) {
1224 /*
1225 * Shouldn't be able to turn off quotas here.
1226 */
1227 ASSERT(error != ESRCH);
1228 ASSERT(error != ENOENT);
1229 return error;
1230 }
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001231
1232 trace_xfs_dqadjust(dqp);
1233
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 /*
1235 * Adjust the inode count and the block count to reflect this inode's
1236 * resource usage.
1237 */
Marcin Slusarz413d57c2008-02-13 15:03:29 -08001238 be64_add_cpu(&dqp->q_core.d_icount, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 dqp->q_res_icount++;
1240 if (nblks) {
Marcin Slusarz413d57c2008-02-13 15:03:29 -08001241 be64_add_cpu(&dqp->q_core.d_bcount, nblks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 dqp->q_res_bcount += nblks;
1243 }
1244 if (rtblks) {
Marcin Slusarz413d57c2008-02-13 15:03:29 -08001245 be64_add_cpu(&dqp->q_core.d_rtbcount, rtblks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 dqp->q_res_rtbcount += rtblks;
1247 }
1248
1249 /*
1250 * Set default limits, adjust timers (since we changed usages)
Christoph Hellwig191f8482010-04-20 17:01:53 +10001251 *
1252 * There are no timers for the default values set in the root dquot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 */
Christoph Hellwig191f8482010-04-20 17:01:53 +10001254 if (dqp->q_core.d_id) {
Christoph Hellwig52fda112010-09-06 01:44:22 +00001255 xfs_qm_adjust_dqlimits(mp, &dqp->q_core);
1256 xfs_qm_adjust_dqtimers(mp, &dqp->q_core);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257 }
1258
1259 dqp->dq_flags |= XFS_DQ_DIRTY;
Christoph Hellwig52fda112010-09-06 01:44:22 +00001260 xfs_qm_dqput(dqp);
1261 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262}
1263
1264STATIC int
1265xfs_qm_get_rtblks(
1266 xfs_inode_t *ip,
1267 xfs_qcnt_t *O_rtblks)
1268{
1269 xfs_filblks_t rtblks; /* total rt blks */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001270 xfs_extnum_t idx; /* extent record index */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 xfs_ifork_t *ifp; /* inode fork pointer */
1272 xfs_extnum_t nextents; /* number of extent entries */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 int error;
1274
1275 ASSERT(XFS_IS_REALTIME_INODE(ip));
1276 ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
1277 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
1278 if ((error = xfs_iread_extents(NULL, ip, XFS_DATA_FORK)))
Jesper Juhl014c2542006-01-15 02:37:08 +01001279 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 }
1281 rtblks = 0;
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001282 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10001283 for (idx = 0; idx < nextents; idx++)
1284 rtblks += xfs_bmbt_get_blockcount(xfs_iext_get_ext(ifp, idx));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 *O_rtblks = (xfs_qcnt_t)rtblks;
Jesper Juhl014c2542006-01-15 02:37:08 +01001286 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287}
1288
1289/*
1290 * callback routine supplied to bulkstat(). Given an inumber, find its
1291 * dquots and update them to account for resources taken by that inode.
1292 */
1293/* ARGSUSED */
1294STATIC int
1295xfs_qm_dqusage_adjust(
1296 xfs_mount_t *mp, /* mount point for filesystem */
1297 xfs_ino_t ino, /* inode number to get data for */
1298 void __user *buffer, /* not used */
1299 int ubsize, /* not used */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 int *ubused, /* not used */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 int *res) /* result code value */
1302{
1303 xfs_inode_t *ip;
Christoph Hellwig52fda112010-09-06 01:44:22 +00001304 xfs_qcnt_t nblks, rtblks = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 int error;
1306
1307 ASSERT(XFS_IS_QUOTA_RUNNING(mp));
1308
1309 /*
1310 * rootino must have its resources accounted for, not so with the quota
1311 * inodes.
1312 */
1313 if (ino == mp->m_sb.sb_uquotino || ino == mp->m_sb.sb_gquotino) {
1314 *res = BULKSTAT_RV_NOTHING;
1315 return XFS_ERROR(EINVAL);
1316 }
1317
1318 /*
1319 * We don't _need_ to take the ilock EXCL. However, the xfs_qm_dqget
1320 * interface expects the inode to be exclusively locked because that's
1321 * the case in all other instances. It's OK that we do this because
1322 * quotacheck is done only at mount time.
1323 */
Christoph Hellwig52fda112010-09-06 01:44:22 +00001324 error = xfs_iget(mp, NULL, ino, 0, XFS_ILOCK_EXCL, &ip);
1325 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 *res = BULKSTAT_RV_NOTHING;
Jesper Juhl014c2542006-01-15 02:37:08 +01001327 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 }
1329
Christoph Hellwig52fda112010-09-06 01:44:22 +00001330 ASSERT(ip->i_delayed_blks == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331
Christoph Hellwig52fda112010-09-06 01:44:22 +00001332 if (XFS_IS_REALTIME_INODE(ip)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333 /*
1334 * Walk thru the extent list and count the realtime blocks.
1335 */
Christoph Hellwig52fda112010-09-06 01:44:22 +00001336 error = xfs_qm_get_rtblks(ip, &rtblks);
1337 if (error)
1338 goto error0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340
Christoph Hellwig52fda112010-09-06 01:44:22 +00001341 nblks = (xfs_qcnt_t)ip->i_d.di_nblocks - rtblks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342
1343 /*
1344 * Add the (disk blocks and inode) resources occupied by this
1345 * inode to its dquots. We do this adjustment in the incore dquot,
1346 * and also copy the changes to its buffer.
1347 * We don't care about putting these changes in a transaction
1348 * envelope because if we crash in the middle of a 'quotacheck'
1349 * we have to start from the beginning anyway.
1350 * Once we're done, we'll log all the dquot bufs.
1351 *
Nathan Scottc41564b2006-03-29 08:55:14 +10001352 * The *QUOTA_ON checks below may look pretty racy, but quotachecks
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 * and quotaoffs don't race. (Quotachecks happen at mount time only).
1354 */
1355 if (XFS_IS_UQUOTA_ON(mp)) {
Christoph Hellwig52fda112010-09-06 01:44:22 +00001356 error = xfs_qm_quotacheck_dqadjust(ip, ip->i_d.di_uid,
1357 XFS_DQ_USER, nblks, rtblks);
1358 if (error)
1359 goto error0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361
Christoph Hellwig52fda112010-09-06 01:44:22 +00001362 if (XFS_IS_GQUOTA_ON(mp)) {
1363 error = xfs_qm_quotacheck_dqadjust(ip, ip->i_d.di_gid,
1364 XFS_DQ_GROUP, nblks, rtblks);
1365 if (error)
1366 goto error0;
1367 }
1368
1369 if (XFS_IS_PQUOTA_ON(mp)) {
Arkadiusz Mi?kiewicz67430992010-09-26 06:10:18 +00001370 error = xfs_qm_quotacheck_dqadjust(ip, xfs_get_projid(ip),
Christoph Hellwig52fda112010-09-06 01:44:22 +00001371 XFS_DQ_PROJ, nblks, rtblks);
1372 if (error)
1373 goto error0;
1374 }
1375
1376 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1377 IRELE(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378 *res = BULKSTAT_RV_DIDONE;
Jesper Juhl014c2542006-01-15 02:37:08 +01001379 return 0;
Christoph Hellwig52fda112010-09-06 01:44:22 +00001380
1381error0:
1382 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1383 IRELE(ip);
1384 *res = BULKSTAT_RV_GIVEUP;
1385 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386}
1387
1388/*
1389 * Walk thru all the filesystem inodes and construct a consistent view
1390 * of the disk quota world. If the quotacheck fails, disable quotas.
1391 */
1392int
1393xfs_qm_quotacheck(
1394 xfs_mount_t *mp)
1395{
1396 int done, count, error;
1397 xfs_ino_t lastino;
1398 size_t structsz;
1399 xfs_inode_t *uip, *gip;
1400 uint flags;
1401
1402 count = INT_MAX;
1403 structsz = 1;
1404 lastino = 0;
1405 flags = 0;
1406
Christoph Hellwig8a7b8a82010-04-20 17:01:30 +10001407 ASSERT(mp->m_quotainfo->qi_uquotaip || mp->m_quotainfo->qi_gquotaip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408 ASSERT(XFS_IS_QUOTA_RUNNING(mp));
1409
1410 /*
1411 * There should be no cached dquots. The (simplistic) quotacheck
1412 * algorithm doesn't like that.
1413 */
Dave Chinner3a254042010-04-13 15:06:48 +10001414 ASSERT(list_empty(&mp->m_quotainfo->qi_dqlist));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415
Dave Chinner0b932cc2011-03-07 10:08:35 +11001416 xfs_notice(mp, "Quotacheck needed: Please wait.");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417
1418 /*
Nathan Scottc8ad20f2005-06-21 15:38:48 +10001419 * First we go thru all the dquots on disk, USR and GRP/PRJ, and reset
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 * their counters to zero. We need a clean slate.
1421 * We don't log our changes till later.
1422 */
Christoph Hellwig8a7b8a82010-04-20 17:01:30 +10001423 uip = mp->m_quotainfo->qi_uquotaip;
1424 if (uip) {
1425 error = xfs_qm_dqiterate(mp, uip, XFS_QMOPT_UQUOTA);
1426 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 goto error_return;
1428 flags |= XFS_UQUOTA_CHKD;
1429 }
1430
Christoph Hellwig8a7b8a82010-04-20 17:01:30 +10001431 gip = mp->m_quotainfo->qi_gquotaip;
1432 if (gip) {
1433 error = xfs_qm_dqiterate(mp, gip, XFS_IS_GQUOTA_ON(mp) ?
1434 XFS_QMOPT_GQUOTA : XFS_QMOPT_PQUOTA);
1435 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436 goto error_return;
Nathan Scottc8ad20f2005-06-21 15:38:48 +10001437 flags |= XFS_OQUOTA_CHKD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 }
1439
1440 do {
1441 /*
1442 * Iterate thru all the inodes in the file system,
1443 * adjusting the corresponding dquot counters in core.
1444 */
Christoph Hellwig7dce11d2010-06-23 18:11:11 +10001445 error = xfs_bulkstat(mp, &lastino, &count,
1446 xfs_qm_dqusage_adjust,
1447 structsz, NULL, &done);
1448 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449 break;
1450
Christoph Hellwig7dce11d2010-06-23 18:11:11 +10001451 } while (!done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452
1453 /*
David Chinner4b8879d2008-04-10 12:20:17 +10001454 * We've made all the changes that we need to make incore.
1455 * Flush them down to disk buffers if everything was updated
1456 * successfully.
1457 */
1458 if (!error)
Christoph Hellwiga7ef9bd2011-12-06 21:58:13 +00001459 error = xfs_qm_dqflush_all(mp);
David Chinner4b8879d2008-04-10 12:20:17 +10001460
1461 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 * We can get this error if we couldn't do a dquot allocation inside
1463 * xfs_qm_dqusage_adjust (via bulkstat). We don't care about the
1464 * dirty dquots that might be cached, we just want to get rid of them
1465 * and turn quotaoff. The dquots won't be attached to any of the inodes
1466 * at this point (because we intentionally didn't in dqget_noattach).
1467 */
1468 if (error) {
Christoph Hellwig8112e9d2010-04-20 17:02:29 +10001469 xfs_qm_dqpurge_all(mp, XFS_QMOPT_QUOTALL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470 goto error_return;
1471 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472
1473 /*
1474 * We didn't log anything, because if we crashed, we'll have to
1475 * start the quotacheck from scratch anyway. However, we must make
1476 * sure that our dquot changes are secure before we put the
1477 * quotacheck'd stamp on the superblock. So, here we do a synchronous
1478 * flush.
1479 */
Christoph Hellwiga9add832011-10-10 16:52:52 +00001480 xfs_flush_buftarg(mp->m_ddev_targp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481
1482 /*
1483 * If one type of quotas is off, then it will lose its
1484 * quotachecked status, since we won't be doing accounting for
1485 * that type anymore.
1486 */
Chandra Seetharaman4177af32012-01-23 17:31:43 +00001487 mp->m_qflags &= ~XFS_ALL_QUOTA_CHKD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488 mp->m_qflags |= flags;
1489
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490 error_return:
1491 if (error) {
Dave Chinner0b932cc2011-03-07 10:08:35 +11001492 xfs_warn(mp,
1493 "Quotacheck: Unsuccessful (Error %d): Disabling quotas.",
1494 error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 /*
1496 * We must turn off quotas.
1497 */
1498 ASSERT(mp->m_quotainfo != NULL);
1499 ASSERT(xfs_Gqm != NULL);
1500 xfs_qm_destroy_quotainfo(mp);
David Chinner31d55772008-04-10 12:20:38 +10001501 if (xfs_mount_reset_sbqflags(mp)) {
Dave Chinner0b932cc2011-03-07 10:08:35 +11001502 xfs_warn(mp,
1503 "Quotacheck: Failed to reset quota flags.");
David Chinner31d55772008-04-10 12:20:38 +10001504 }
Dave Chinner0b932cc2011-03-07 10:08:35 +11001505 } else
1506 xfs_notice(mp, "Quotacheck: Done.");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 return (error);
1508}
1509
1510/*
1511 * This is called after the superblock has been read in and we're ready to
1512 * iget the quota inodes.
1513 */
1514STATIC int
1515xfs_qm_init_quotainos(
1516 xfs_mount_t *mp)
1517{
1518 xfs_inode_t *uip, *gip;
1519 int error;
1520 __int64_t sbflags;
1521 uint flags;
1522
1523 ASSERT(mp->m_quotainfo);
1524 uip = gip = NULL;
1525 sbflags = 0;
1526 flags = 0;
1527
1528 /*
1529 * Get the uquota and gquota inodes
1530 */
Eric Sandeen62118702008-03-06 13:44:28 +11001531 if (xfs_sb_version_hasquota(&mp->m_sb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532 if (XFS_IS_UQUOTA_ON(mp) &&
1533 mp->m_sb.sb_uquotino != NULLFSINO) {
1534 ASSERT(mp->m_sb.sb_uquotino > 0);
1535 if ((error = xfs_iget(mp, NULL, mp->m_sb.sb_uquotino,
Dave Chinner7b6259e2010-06-24 11:35:17 +10001536 0, 0, &uip)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537 return XFS_ERROR(error);
1538 }
Nathan Scottc8ad20f2005-06-21 15:38:48 +10001539 if (XFS_IS_OQUOTA_ON(mp) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540 mp->m_sb.sb_gquotino != NULLFSINO) {
1541 ASSERT(mp->m_sb.sb_gquotino > 0);
1542 if ((error = xfs_iget(mp, NULL, mp->m_sb.sb_gquotino,
Dave Chinner7b6259e2010-06-24 11:35:17 +10001543 0, 0, &gip))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544 if (uip)
Christoph Hellwig43355092008-03-27 18:01:08 +11001545 IRELE(uip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546 return XFS_ERROR(error);
1547 }
1548 }
1549 } else {
1550 flags |= XFS_QMOPT_SBVERSION;
1551 sbflags |= (XFS_SB_VERSIONNUM | XFS_SB_UQUOTINO |
1552 XFS_SB_GQUOTINO | XFS_SB_QFLAGS);
1553 }
1554
1555 /*
1556 * Create the two inodes, if they don't exist already. The changes
1557 * made above will get added to a transaction and logged in one of
1558 * the qino_alloc calls below. If the device is readonly,
1559 * temporarily switch to read-write to do this.
1560 */
1561 if (XFS_IS_UQUOTA_ON(mp) && uip == NULL) {
1562 if ((error = xfs_qm_qino_alloc(mp, &uip,
1563 sbflags | XFS_SB_UQUOTINO,
1564 flags | XFS_QMOPT_UQUOTA)))
1565 return XFS_ERROR(error);
1566
1567 flags &= ~XFS_QMOPT_SBVERSION;
1568 }
Nathan Scottc8ad20f2005-06-21 15:38:48 +10001569 if (XFS_IS_OQUOTA_ON(mp) && gip == NULL) {
1570 flags |= (XFS_IS_GQUOTA_ON(mp) ?
1571 XFS_QMOPT_GQUOTA : XFS_QMOPT_PQUOTA);
1572 error = xfs_qm_qino_alloc(mp, &gip,
1573 sbflags | XFS_SB_GQUOTINO, flags);
1574 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575 if (uip)
Christoph Hellwig43355092008-03-27 18:01:08 +11001576 IRELE(uip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577
1578 return XFS_ERROR(error);
1579 }
1580 }
1581
Christoph Hellwig8a7b8a82010-04-20 17:01:30 +10001582 mp->m_quotainfo->qi_uquotaip = uip;
1583 mp->m_quotainfo->qi_gquotaip = gip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584
Jesper Juhl014c2542006-01-15 02:37:08 +01001585 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586}
1587
Christoph Hellwig92b2e5b32012-02-01 13:57:20 +00001588STATIC void
1589xfs_qm_dqfree_one(
1590 struct xfs_dquot *dqp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591{
Christoph Hellwig92b2e5b32012-02-01 13:57:20 +00001592 struct xfs_mount *mp = dqp->q_mount;
1593 struct xfs_quotainfo *qi = mp->m_quotainfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594
Christoph Hellwig92b2e5b32012-02-01 13:57:20 +00001595 mutex_lock(&dqp->q_hash->qh_lock);
1596 list_del_init(&dqp->q_hashlist);
1597 dqp->q_hash->qh_version++;
1598 mutex_unlock(&dqp->q_hash->qh_lock);
Christoph Hellwigbf72de32011-12-06 21:58:19 +00001599
Christoph Hellwig92b2e5b32012-02-01 13:57:20 +00001600 mutex_lock(&qi->qi_dqlist_lock);
1601 list_del_init(&dqp->q_mplist);
1602 qi->qi_dquots--;
1603 qi->qi_dqreclaims++;
1604 mutex_unlock(&qi->qi_dqlist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605
Christoph Hellwig92b2e5b32012-02-01 13:57:20 +00001606 xfs_qm_dqdestroy(dqp);
1607}
Christoph Hellwigbe7ffc32011-12-06 21:58:17 +00001608
Christoph Hellwig92b2e5b32012-02-01 13:57:20 +00001609STATIC void
1610xfs_qm_dqreclaim_one(
1611 struct xfs_dquot *dqp,
1612 struct list_head *dispose_list)
1613{
1614 struct xfs_mount *mp = dqp->q_mount;
Christoph Hellwigf8739c32012-03-13 08:52:34 +00001615 struct xfs_quotainfo *qi = mp->m_quotainfo;
Christoph Hellwig92b2e5b32012-02-01 13:57:20 +00001616 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617
Christoph Hellwig92b2e5b32012-02-01 13:57:20 +00001618 if (!xfs_dqlock_nowait(dqp))
1619 goto out_busy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620
Christoph Hellwig92b2e5b32012-02-01 13:57:20 +00001621 /*
1622 * This dquot has acquired a reference in the meantime remove it from
1623 * the freelist and try again.
1624 */
1625 if (dqp->q_nrefs) {
Christoph Hellwig92678552011-12-06 21:58:18 +00001626 xfs_dqunlock(dqp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627
Christoph Hellwig92b2e5b32012-02-01 13:57:20 +00001628 trace_xfs_dqreclaim_want(dqp);
Christoph Hellwig48776fd2012-03-13 08:52:33 +00001629 XFS_STATS_INC(xs_qm_dqwants);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001630
Christoph Hellwigf8739c32012-03-13 08:52:34 +00001631 list_del_init(&dqp->q_lru);
1632 qi->qi_lru_count--;
Christoph Hellwig48776fd2012-03-13 08:52:33 +00001633 XFS_STATS_DEC(xs_qm_dquot_unused);
Christoph Hellwig92b2e5b32012-02-01 13:57:20 +00001634 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635 }
Christoph Hellwig92678552011-12-06 21:58:18 +00001636
Christoph Hellwig92b2e5b32012-02-01 13:57:20 +00001637 ASSERT(dqp->q_hash);
1638 ASSERT(!list_empty(&dqp->q_mplist));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639
Christoph Hellwig92b2e5b32012-02-01 13:57:20 +00001640 /*
1641 * Try to grab the flush lock. If this dquot is in the process of
1642 * getting flushed to disk, we don't want to reclaim it.
1643 */
1644 if (!xfs_dqflock_nowait(dqp))
1645 goto out_busy;
Dave Chinner368e1362010-04-13 15:06:50 +10001646
Christoph Hellwig92b2e5b32012-02-01 13:57:20 +00001647 /*
1648 * We have the flush lock so we know that this is not in the
1649 * process of being flushed. So, if this is dirty, flush it
1650 * DELWRI so that we don't get a freelist infested with
1651 * dirty dquots.
1652 */
1653 if (XFS_DQ_IS_DIRTY(dqp)) {
1654 trace_xfs_dqreclaim_dirty(dqp);
Dave Chinner368e1362010-04-13 15:06:50 +10001655
Christoph Hellwig92b2e5b32012-02-01 13:57:20 +00001656 /*
1657 * We flush it delayed write, so don't bother releasing the
1658 * freelist lock.
1659 */
1660 error = xfs_qm_dqflush(dqp, 0);
1661 if (error) {
1662 xfs_warn(mp, "%s: dquot %p flush failed",
1663 __func__, dqp);
1664 }
1665
1666 /*
1667 * Give the dquot another try on the freelist, as the
1668 * flushing will take some time.
1669 */
1670 goto out_busy;
Dave Chinner368e1362010-04-13 15:06:50 +10001671 }
Christoph Hellwig92b2e5b32012-02-01 13:57:20 +00001672 xfs_dqfunlock(dqp);
1673
1674 /*
1675 * Prevent lookups now that we are past the point of no return.
1676 */
1677 dqp->dq_flags |= XFS_DQ_FREEING;
1678 xfs_dqunlock(dqp);
1679
1680 ASSERT(dqp->q_nrefs == 0);
Christoph Hellwigf8739c32012-03-13 08:52:34 +00001681 list_move_tail(&dqp->q_lru, dispose_list);
1682 qi->qi_lru_count--;
Christoph Hellwig48776fd2012-03-13 08:52:33 +00001683 XFS_STATS_DEC(xs_qm_dquot_unused);
Christoph Hellwig92b2e5b32012-02-01 13:57:20 +00001684
1685 trace_xfs_dqreclaim_done(dqp);
Christoph Hellwig48776fd2012-03-13 08:52:33 +00001686 XFS_STATS_INC(xs_qm_dqreclaims);
Christoph Hellwig92b2e5b32012-02-01 13:57:20 +00001687 return;
1688
1689out_busy:
1690 xfs_dqunlock(dqp);
1691
1692 /*
1693 * Move the dquot to the tail of the list so that we don't spin on it.
1694 */
Christoph Hellwigf8739c32012-03-13 08:52:34 +00001695 list_move_tail(&dqp->q_lru, &qi->qi_lru_list);
Christoph Hellwig92b2e5b32012-02-01 13:57:20 +00001696
1697 trace_xfs_dqreclaim_busy(dqp);
Christoph Hellwig48776fd2012-03-13 08:52:33 +00001698 XFS_STATS_INC(xs_qm_dqreclaim_misses);
Dave Chinner368e1362010-04-13 15:06:50 +10001699}
1700
Dave Chinner368e1362010-04-13 15:06:50 +10001701STATIC int
Dave Chinner7f8275d2010-07-19 14:56:17 +10001702xfs_qm_shake(
Christoph Hellwig92b2e5b32012-02-01 13:57:20 +00001703 struct shrinker *shrink,
1704 struct shrink_control *sc)
Dave Chinner368e1362010-04-13 15:06:50 +10001705{
Christoph Hellwigf8739c32012-03-13 08:52:34 +00001706 struct xfs_quotainfo *qi =
1707 container_of(shrink, struct xfs_quotainfo, qi_shrinker);
Christoph Hellwig92b2e5b32012-02-01 13:57:20 +00001708 int nr_to_scan = sc->nr_to_scan;
1709 LIST_HEAD (dispose_list);
1710 struct xfs_dquot *dqp;
Dave Chinner368e1362010-04-13 15:06:50 +10001711
Christoph Hellwig92b2e5b32012-02-01 13:57:20 +00001712 if ((sc->gfp_mask & (__GFP_FS|__GFP_WAIT)) != (__GFP_FS|__GFP_WAIT))
Dave Chinner368e1362010-04-13 15:06:50 +10001713 return 0;
Christoph Hellwig92b2e5b32012-02-01 13:57:20 +00001714 if (!nr_to_scan)
1715 goto out;
Dave Chinner368e1362010-04-13 15:06:50 +10001716
Christoph Hellwigf8739c32012-03-13 08:52:34 +00001717 mutex_lock(&qi->qi_lru_lock);
1718 while (!list_empty(&qi->qi_lru_list)) {
Christoph Hellwig92b2e5b32012-02-01 13:57:20 +00001719 if (nr_to_scan-- <= 0)
1720 break;
Christoph Hellwigf8739c32012-03-13 08:52:34 +00001721 dqp = list_first_entry(&qi->qi_lru_list, struct xfs_dquot,
1722 q_lru);
Christoph Hellwig92b2e5b32012-02-01 13:57:20 +00001723 xfs_qm_dqreclaim_one(dqp, &dispose_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724 }
Christoph Hellwigf8739c32012-03-13 08:52:34 +00001725 mutex_unlock(&qi->qi_lru_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726
Christoph Hellwig92b2e5b32012-02-01 13:57:20 +00001727 while (!list_empty(&dispose_list)) {
Christoph Hellwigf8739c32012-03-13 08:52:34 +00001728 dqp = list_first_entry(&dispose_list, struct xfs_dquot, q_lru);
1729 list_del_init(&dqp->q_lru);
Christoph Hellwig92b2e5b32012-02-01 13:57:20 +00001730 xfs_qm_dqfree_one(dqp);
1731 }
1732out:
Christoph Hellwigf8739c32012-03-13 08:52:34 +00001733 return (qi->qi_lru_count / 100) * sysctl_vfs_cache_pressure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734}
1735
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736/*
1737 * Start a transaction and write the incore superblock changes to
1738 * disk. flags parameter indicates which fields have changed.
1739 */
1740int
1741xfs_qm_write_sb_changes(
1742 xfs_mount_t *mp,
1743 __int64_t flags)
1744{
1745 xfs_trans_t *tp;
1746 int error;
1747
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748 tp = xfs_trans_alloc(mp, XFS_TRANS_QM_SBCHANGE);
1749 if ((error = xfs_trans_reserve(tp, 0,
1750 mp->m_sb.sb_sectsize + 128, 0,
1751 0,
1752 XFS_DEFAULT_LOG_COUNT))) {
1753 xfs_trans_cancel(tp, 0);
Jesper Juhl014c2542006-01-15 02:37:08 +01001754 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755 }
1756
1757 xfs_mod_sb(tp, flags);
David Chinnere5720ee2008-04-10 12:21:18 +10001758 error = xfs_trans_commit(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759
David Chinnere5720ee2008-04-10 12:21:18 +10001760 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761}
1762
1763
1764/* --------------- utility functions for vnodeops ---------------- */
1765
1766
1767/*
Christoph Hellwig6c77b0e2010-10-06 18:41:17 +00001768 * Given an inode, a uid, gid and prid make sure that we have
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769 * allocated relevant dquot(s) on disk, and that we won't exceed inode
1770 * quotas by creating this file.
1771 * This also attaches dquot(s) to the given inode after locking it,
1772 * and returns the dquots corresponding to the uid and/or gid.
1773 *
1774 * in : inode (unlocked)
1775 * out : udquot, gdquot with references taken and unlocked
1776 */
1777int
1778xfs_qm_vop_dqalloc(
Christoph Hellwig7d095252009-06-08 15:33:32 +02001779 struct xfs_inode *ip,
1780 uid_t uid,
1781 gid_t gid,
1782 prid_t prid,
1783 uint flags,
1784 struct xfs_dquot **O_udqpp,
1785 struct xfs_dquot **O_gdqpp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786{
Christoph Hellwig7d095252009-06-08 15:33:32 +02001787 struct xfs_mount *mp = ip->i_mount;
1788 struct xfs_dquot *uq, *gq;
1789 int error;
1790 uint lockflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791
Christoph Hellwig7d095252009-06-08 15:33:32 +02001792 if (!XFS_IS_QUOTA_RUNNING(mp) || !XFS_IS_QUOTA_ON(mp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793 return 0;
1794
1795 lockflags = XFS_ILOCK_EXCL;
1796 xfs_ilock(ip, lockflags);
1797
Christoph Hellwigbd186aa2007-08-30 17:21:12 +10001798 if ((flags & XFS_QMOPT_INHERIT) && XFS_INHERIT_GID(ip))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799 gid = ip->i_d.di_gid;
1800
1801 /*
1802 * Attach the dquot(s) to this inode, doing a dquot allocation
1803 * if necessary. The dquot(s) will not be locked.
1804 */
1805 if (XFS_NOT_DQATTACHED(mp, ip)) {
Christoph Hellwig7d095252009-06-08 15:33:32 +02001806 error = xfs_qm_dqattach_locked(ip, XFS_QMOPT_DQALLOC);
1807 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808 xfs_iunlock(ip, lockflags);
Jesper Juhl014c2542006-01-15 02:37:08 +01001809 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810 }
1811 }
1812
1813 uq = gq = NULL;
Nathan Scottc8ad20f2005-06-21 15:38:48 +10001814 if ((flags & XFS_QMOPT_UQUOTA) && XFS_IS_UQUOTA_ON(mp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815 if (ip->i_d.di_uid != uid) {
1816 /*
1817 * What we need is the dquot that has this uid, and
1818 * if we send the inode to dqget, the uid of the inode
1819 * takes priority over what's sent in the uid argument.
1820 * We must unlock inode here before calling dqget if
1821 * we're not sending the inode, because otherwise
1822 * we'll deadlock by doing trans_reserve while
1823 * holding ilock.
1824 */
1825 xfs_iunlock(ip, lockflags);
1826 if ((error = xfs_qm_dqget(mp, NULL, (xfs_dqid_t) uid,
1827 XFS_DQ_USER,
1828 XFS_QMOPT_DQALLOC |
1829 XFS_QMOPT_DOWARN,
1830 &uq))) {
1831 ASSERT(error != ENOENT);
Jesper Juhl014c2542006-01-15 02:37:08 +01001832 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833 }
1834 /*
1835 * Get the ilock in the right order.
1836 */
1837 xfs_dqunlock(uq);
1838 lockflags = XFS_ILOCK_SHARED;
1839 xfs_ilock(ip, lockflags);
1840 } else {
1841 /*
1842 * Take an extra reference, because we'll return
1843 * this to caller
1844 */
1845 ASSERT(ip->i_udquot);
Christoph Hellwig78e55892011-12-06 21:58:22 +00001846 uq = xfs_qm_dqhold(ip->i_udquot);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847 }
1848 }
Nathan Scottc8ad20f2005-06-21 15:38:48 +10001849 if ((flags & XFS_QMOPT_GQUOTA) && XFS_IS_GQUOTA_ON(mp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850 if (ip->i_d.di_gid != gid) {
1851 xfs_iunlock(ip, lockflags);
1852 if ((error = xfs_qm_dqget(mp, NULL, (xfs_dqid_t)gid,
1853 XFS_DQ_GROUP,
1854 XFS_QMOPT_DQALLOC |
1855 XFS_QMOPT_DOWARN,
1856 &gq))) {
1857 if (uq)
1858 xfs_qm_dqrele(uq);
1859 ASSERT(error != ENOENT);
Jesper Juhl014c2542006-01-15 02:37:08 +01001860 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861 }
1862 xfs_dqunlock(gq);
1863 lockflags = XFS_ILOCK_SHARED;
1864 xfs_ilock(ip, lockflags);
1865 } else {
1866 ASSERT(ip->i_gdquot);
Christoph Hellwig78e55892011-12-06 21:58:22 +00001867 gq = xfs_qm_dqhold(ip->i_gdquot);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868 }
Nathan Scottc8ad20f2005-06-21 15:38:48 +10001869 } else if ((flags & XFS_QMOPT_PQUOTA) && XFS_IS_PQUOTA_ON(mp)) {
Arkadiusz Mi?kiewicz67430992010-09-26 06:10:18 +00001870 if (xfs_get_projid(ip) != prid) {
Nathan Scottc8ad20f2005-06-21 15:38:48 +10001871 xfs_iunlock(ip, lockflags);
1872 if ((error = xfs_qm_dqget(mp, NULL, (xfs_dqid_t)prid,
1873 XFS_DQ_PROJ,
1874 XFS_QMOPT_DQALLOC |
1875 XFS_QMOPT_DOWARN,
1876 &gq))) {
1877 if (uq)
1878 xfs_qm_dqrele(uq);
1879 ASSERT(error != ENOENT);
1880 return (error);
1881 }
1882 xfs_dqunlock(gq);
1883 lockflags = XFS_ILOCK_SHARED;
1884 xfs_ilock(ip, lockflags);
1885 } else {
1886 ASSERT(ip->i_gdquot);
Christoph Hellwig78e55892011-12-06 21:58:22 +00001887 gq = xfs_qm_dqhold(ip->i_gdquot);
Nathan Scottc8ad20f2005-06-21 15:38:48 +10001888 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889 }
1890 if (uq)
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001891 trace_xfs_dquot_dqalloc(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892
1893 xfs_iunlock(ip, lockflags);
1894 if (O_udqpp)
1895 *O_udqpp = uq;
1896 else if (uq)
1897 xfs_qm_dqrele(uq);
1898 if (O_gdqpp)
1899 *O_gdqpp = gq;
1900 else if (gq)
1901 xfs_qm_dqrele(gq);
Jesper Juhl014c2542006-01-15 02:37:08 +01001902 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903}
1904
1905/*
1906 * Actually transfer ownership, and do dquot modifications.
1907 * These were already reserved.
1908 */
1909xfs_dquot_t *
1910xfs_qm_vop_chown(
1911 xfs_trans_t *tp,
1912 xfs_inode_t *ip,
1913 xfs_dquot_t **IO_olddq,
1914 xfs_dquot_t *newdq)
1915{
1916 xfs_dquot_t *prevdq;
Nathan Scott06d10dd2005-06-21 15:48:47 +10001917 uint bfield = XFS_IS_REALTIME_INODE(ip) ?
1918 XFS_TRANS_DQ_RTBCOUNT : XFS_TRANS_DQ_BCOUNT;
1919
Christoph Hellwig7d095252009-06-08 15:33:32 +02001920
Christoph Hellwig579aa9c2008-04-22 17:34:00 +10001921 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922 ASSERT(XFS_IS_QUOTA_RUNNING(ip->i_mount));
1923
1924 /* old dquot */
1925 prevdq = *IO_olddq;
1926 ASSERT(prevdq);
1927 ASSERT(prevdq != newdq);
1928
Nathan Scott06d10dd2005-06-21 15:48:47 +10001929 xfs_trans_mod_dquot(tp, prevdq, bfield, -(ip->i_d.di_nblocks));
1930 xfs_trans_mod_dquot(tp, prevdq, XFS_TRANS_DQ_ICOUNT, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931
1932 /* the sparkling new dquot */
Nathan Scott06d10dd2005-06-21 15:48:47 +10001933 xfs_trans_mod_dquot(tp, newdq, bfield, ip->i_d.di_nblocks);
1934 xfs_trans_mod_dquot(tp, newdq, XFS_TRANS_DQ_ICOUNT, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935
1936 /*
Christoph Hellwig78e55892011-12-06 21:58:22 +00001937 * Take an extra reference, because the inode is going to keep
1938 * this dquot pointer even after the trans_commit.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939 */
Christoph Hellwig78e55892011-12-06 21:58:22 +00001940 *IO_olddq = xfs_qm_dqhold(newdq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941
Jesper Juhl014c2542006-01-15 02:37:08 +01001942 return prevdq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943}
1944
1945/*
Nathan Scottc8ad20f2005-06-21 15:38:48 +10001946 * Quota reservations for setattr(AT_UID|AT_GID|AT_PROJID).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947 */
1948int
1949xfs_qm_vop_chown_reserve(
1950 xfs_trans_t *tp,
1951 xfs_inode_t *ip,
1952 xfs_dquot_t *udqp,
1953 xfs_dquot_t *gdqp,
1954 uint flags)
1955{
Christoph Hellwig7d095252009-06-08 15:33:32 +02001956 xfs_mount_t *mp = ip->i_mount;
Nathan Scott9a2a7de2006-03-31 13:04:49 +10001957 uint delblks, blkflags, prjflags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958 xfs_dquot_t *unresudq, *unresgdq, *delblksudq, *delblksgdq;
Christoph Hellwig7d095252009-06-08 15:33:32 +02001959 int error;
1960
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961
Christoph Hellwig579aa9c2008-04-22 17:34:00 +10001962 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_ILOCK_SHARED));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963 ASSERT(XFS_IS_QUOTA_RUNNING(mp));
1964
1965 delblks = ip->i_delayed_blks;
1966 delblksudq = delblksgdq = unresudq = unresgdq = NULL;
Nathan Scott06d10dd2005-06-21 15:48:47 +10001967 blkflags = XFS_IS_REALTIME_INODE(ip) ?
1968 XFS_QMOPT_RES_RTBLKS : XFS_QMOPT_RES_REGBLKS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969
1970 if (XFS_IS_UQUOTA_ON(mp) && udqp &&
Christoph Hellwig1149d962005-11-02 15:01:12 +11001971 ip->i_d.di_uid != (uid_t)be32_to_cpu(udqp->q_core.d_id)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972 delblksudq = udqp;
1973 /*
1974 * If there are delayed allocation blocks, then we have to
1975 * unreserve those from the old dquot, and add them to the
1976 * new dquot.
1977 */
1978 if (delblks) {
1979 ASSERT(ip->i_udquot);
1980 unresudq = ip->i_udquot;
1981 }
1982 }
Nathan Scottc8ad20f2005-06-21 15:38:48 +10001983 if (XFS_IS_OQUOTA_ON(ip->i_mount) && gdqp) {
Nathan Scott9a2a7de2006-03-31 13:04:49 +10001984 if (XFS_IS_PQUOTA_ON(ip->i_mount) &&
Arkadiusz Mi?kiewicz67430992010-09-26 06:10:18 +00001985 xfs_get_projid(ip) != be32_to_cpu(gdqp->q_core.d_id))
Nathan Scott9a2a7de2006-03-31 13:04:49 +10001986 prjflags = XFS_QMOPT_ENOSPC;
1987
1988 if (prjflags ||
1989 (XFS_IS_GQUOTA_ON(ip->i_mount) &&
1990 ip->i_d.di_gid != be32_to_cpu(gdqp->q_core.d_id))) {
Nathan Scottc8ad20f2005-06-21 15:38:48 +10001991 delblksgdq = gdqp;
1992 if (delblks) {
1993 ASSERT(ip->i_gdquot);
1994 unresgdq = ip->i_gdquot;
1995 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996 }
1997 }
1998
1999 if ((error = xfs_trans_reserve_quota_bydquots(tp, ip->i_mount,
2000 delblksudq, delblksgdq, ip->i_d.di_nblocks, 1,
Nathan Scott9a2a7de2006-03-31 13:04:49 +10002001 flags | blkflags | prjflags)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002 return (error);
2003
2004 /*
2005 * Do the delayed blks reservations/unreservations now. Since, these
2006 * are done without the help of a transaction, if a reservation fails
2007 * its previous reservations won't be automatically undone by trans
2008 * code. So, we have to do it manually here.
2009 */
2010 if (delblks) {
2011 /*
2012 * Do the reservations first. Unreservation can't fail.
2013 */
2014 ASSERT(delblksudq || delblksgdq);
2015 ASSERT(unresudq || unresgdq);
2016 if ((error = xfs_trans_reserve_quota_bydquots(NULL, ip->i_mount,
2017 delblksudq, delblksgdq, (xfs_qcnt_t)delblks, 0,
Nathan Scott9a2a7de2006-03-31 13:04:49 +10002018 flags | blkflags | prjflags)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019 return (error);
2020 xfs_trans_reserve_quota_bydquots(NULL, ip->i_mount,
2021 unresudq, unresgdq, -((xfs_qcnt_t)delblks), 0,
Nathan Scott06d10dd2005-06-21 15:48:47 +10002022 blkflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023 }
2024
2025 return (0);
2026}
2027
2028int
2029xfs_qm_vop_rename_dqattach(
Christoph Hellwig7d095252009-06-08 15:33:32 +02002030 struct xfs_inode **i_tab)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031{
Christoph Hellwig7d095252009-06-08 15:33:32 +02002032 struct xfs_mount *mp = i_tab[0]->i_mount;
2033 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034
Christoph Hellwig7d095252009-06-08 15:33:32 +02002035 if (!XFS_IS_QUOTA_RUNNING(mp) || !XFS_IS_QUOTA_ON(mp))
Jesper Juhl014c2542006-01-15 02:37:08 +01002036 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037
Christoph Hellwig7d095252009-06-08 15:33:32 +02002038 for (i = 0; (i < 4 && i_tab[i]); i++) {
2039 struct xfs_inode *ip = i_tab[i];
2040 int error;
2041
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042 /*
2043 * Watch out for duplicate entries in the table.
2044 */
Christoph Hellwig7d095252009-06-08 15:33:32 +02002045 if (i == 0 || ip != i_tab[i-1]) {
2046 if (XFS_NOT_DQATTACHED(mp, ip)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047 error = xfs_qm_dqattach(ip, 0);
2048 if (error)
Jesper Juhl014c2542006-01-15 02:37:08 +01002049 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050 }
2051 }
2052 }
Jesper Juhl014c2542006-01-15 02:37:08 +01002053 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054}
2055
2056void
Christoph Hellwig7d095252009-06-08 15:33:32 +02002057xfs_qm_vop_create_dqattach(
2058 struct xfs_trans *tp,
2059 struct xfs_inode *ip,
2060 struct xfs_dquot *udqp,
2061 struct xfs_dquot *gdqp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062{
Christoph Hellwig7d095252009-06-08 15:33:32 +02002063 struct xfs_mount *mp = tp->t_mountp;
2064
2065 if (!XFS_IS_QUOTA_RUNNING(mp) || !XFS_IS_QUOTA_ON(mp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066 return;
2067
Christoph Hellwig579aa9c2008-04-22 17:34:00 +10002068 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
Christoph Hellwig7d095252009-06-08 15:33:32 +02002069 ASSERT(XFS_IS_QUOTA_RUNNING(mp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070
2071 if (udqp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072 ASSERT(ip->i_udquot == NULL);
Christoph Hellwig7d095252009-06-08 15:33:32 +02002073 ASSERT(XFS_IS_UQUOTA_ON(mp));
Christoph Hellwig1149d962005-11-02 15:01:12 +11002074 ASSERT(ip->i_d.di_uid == be32_to_cpu(udqp->q_core.d_id));
Christoph Hellwig78e55892011-12-06 21:58:22 +00002075
2076 ip->i_udquot = xfs_qm_dqhold(udqp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077 xfs_trans_mod_dquot(tp, udqp, XFS_TRANS_DQ_ICOUNT, 1);
2078 }
2079 if (gdqp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080 ASSERT(ip->i_gdquot == NULL);
Christoph Hellwig7d095252009-06-08 15:33:32 +02002081 ASSERT(XFS_IS_OQUOTA_ON(mp));
2082 ASSERT((XFS_IS_GQUOTA_ON(mp) ?
Arkadiusz Mi?kiewicz67430992010-09-26 06:10:18 +00002083 ip->i_d.di_gid : xfs_get_projid(ip)) ==
Nathan Scottee2a4f72006-01-11 15:33:36 +11002084 be32_to_cpu(gdqp->q_core.d_id));
Christoph Hellwig78e55892011-12-06 21:58:22 +00002085
2086 ip->i_gdquot = xfs_qm_dqhold(gdqp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087 xfs_trans_mod_dquot(tp, gdqp, XFS_TRANS_DQ_ICOUNT, 1);
2088 }
2089}
2090