blob: 2f92d3b0d8a83d24085f12540124cd3aac9fdf50 [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
Linus Torvalds1da177e2005-04-16 15:20:36 -070057STATIC int xfs_qm_init_quotainos(xfs_mount_t *);
Christoph Hellwigba0f32d2005-06-21 15:36:52 +100058STATIC int xfs_qm_init_quotainfo(xfs_mount_t *);
Ying Han1495f232011-05-24 17:12:27 -070059STATIC int xfs_qm_shake(struct shrinker *, struct shrink_control *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
Linus Torvalds1da177e2005-04-16 15:20:36 -070061/*
62 * Initialize the XQM structure.
63 * Note that there is not one quota manager per file system.
64 */
65STATIC struct xfs_qm *
66xfs_Gqm_init(void)
67{
Nathan Scott6b3f6b52005-11-02 15:08:25 +110068 xfs_qm_t *xqm;
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
Nathan Scott6b3f6b52005-11-02 15:08:25 +110070 xqm = kmem_zalloc(sizeof(xfs_qm_t), KM_SLEEP);
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
72 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 * dquot zone. we register our own low-memory callback.
74 */
75 if (!qm_dqzone) {
76 xqm->qm_dqzone = kmem_zone_init(sizeof(xfs_dquot_t),
77 "xfs_dquots");
78 qm_dqzone = xqm->qm_dqzone;
79 } else
80 xqm->qm_dqzone = qm_dqzone;
81
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 /*
83 * The t_dqinfo portion of transactions.
84 */
85 if (!qm_dqtrxzone) {
86 xqm->qm_dqtrxzone = kmem_zone_init(sizeof(xfs_dquot_acct_t),
87 "xfs_dqtrx");
88 qm_dqtrxzone = xqm->qm_dqtrxzone;
89 } else
90 xqm->qm_dqtrxzone = qm_dqtrxzone;
91
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 xqm->qm_nrefs = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 return xqm;
94}
95
96/*
97 * Destroy the global quota manager when its reference count goes to zero.
98 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +100099STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100xfs_qm_destroy(
101 struct xfs_qm *xqm)
102{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 ASSERT(xqm != NULL);
104 ASSERT(xqm->qm_nrefs == 0);
Christoph Hellwig80a376b2011-12-06 21:58:16 +0000105
Denys Vlasenkof0e2d932008-05-19 16:31:57 +1000106 kmem_free(xqm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107}
108
109/*
110 * Called at mount time to let XQM know that another file system is
111 * starting quotas. This isn't crucial information as the individual mount
112 * structures are pretty independent, but it helps the XQM keep a
113 * global view of what's going on.
114 */
115/* ARGSUSED */
116STATIC int
117xfs_qm_hold_quotafs_ref(
118 struct xfs_mount *mp)
119{
120 /*
121 * Need to lock the xfs_Gqm structure for things like this. For example,
122 * the structure could disappear between the entry to this routine and
123 * a HOLD operation if not locked.
124 */
Christoph Hellwige2494582009-02-09 08:38:39 +0100125 mutex_lock(&xfs_Gqm_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126
Christoph Hellwigbdfb0432010-01-20 21:55:30 +0000127 if (!xfs_Gqm) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 xfs_Gqm = xfs_Gqm_init();
Julia Lawall38e712a2010-05-26 15:57:23 +0000129 if (!xfs_Gqm) {
130 mutex_unlock(&xfs_Gqm_lock);
Christoph Hellwigbdfb0432010-01-20 21:55:30 +0000131 return ENOMEM;
Julia Lawall38e712a2010-05-26 15:57:23 +0000132 }
Christoph Hellwigbdfb0432010-01-20 21:55:30 +0000133 }
134
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 /*
136 * We can keep a list of all filesystems with quotas mounted for
137 * debugging and statistical purposes, but ...
138 * Just take a reference and get out.
139 */
Christoph Hellwige2494582009-02-09 08:38:39 +0100140 xfs_Gqm->qm_nrefs++;
141 mutex_unlock(&xfs_Gqm_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142
143 return 0;
144}
145
146
147/*
148 * Release the reference that a filesystem took at mount time,
149 * so that we know when we need to destroy the entire quota manager.
150 */
151/* ARGSUSED */
152STATIC void
153xfs_qm_rele_quotafs_ref(
154 struct xfs_mount *mp)
155{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 ASSERT(xfs_Gqm);
157 ASSERT(xfs_Gqm->qm_nrefs > 0);
158
159 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 * Destroy the entire XQM. If somebody mounts with quotaon, this'll
161 * be restarted.
162 */
Christoph Hellwige2494582009-02-09 08:38:39 +0100163 mutex_lock(&xfs_Gqm_lock);
164 if (--xfs_Gqm->qm_nrefs == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 xfs_qm_destroy(xfs_Gqm);
166 xfs_Gqm = NULL;
167 }
Christoph Hellwige2494582009-02-09 08:38:39 +0100168 mutex_unlock(&xfs_Gqm_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169}
170
171/*
Christoph Hellwigb84a3a92012-03-14 11:53:34 -0500172 * We use the batch lookup interface to iterate over the dquots as it
173 * currently is the only interface into the radix tree code that allows
174 * fuzzy lookups instead of exact matches. Holding the lock over multiple
175 * operations is fine as all callers are used either during mount/umount
176 * or quotaoff.
177 */
178#define XFS_DQ_LOOKUP_BATCH 32
179
180STATIC int
181xfs_qm_dquot_walk(
182 struct xfs_mount *mp,
183 int type,
184 int (*execute)(struct xfs_dquot *dqp))
185{
186 struct xfs_quotainfo *qi = mp->m_quotainfo;
187 struct radix_tree_root *tree = XFS_DQUOT_TREE(qi, type);
188 uint32_t next_index;
189 int last_error = 0;
190 int skipped;
191 int nr_found;
192
193restart:
194 skipped = 0;
195 next_index = 0;
196 nr_found = 0;
197
198 while (1) {
199 struct xfs_dquot *batch[XFS_DQ_LOOKUP_BATCH];
200 int error = 0;
201 int i;
202
203 mutex_lock(&qi->qi_tree_lock);
204 nr_found = radix_tree_gang_lookup(tree, (void **)batch,
205 next_index, XFS_DQ_LOOKUP_BATCH);
206 if (!nr_found) {
207 mutex_unlock(&qi->qi_tree_lock);
208 break;
209 }
210
211 for (i = 0; i < nr_found; i++) {
212 struct xfs_dquot *dqp = batch[i];
213
214 next_index = be32_to_cpu(dqp->q_core.d_id) + 1;
215
216 error = execute(batch[i]);
217 if (error == EAGAIN) {
218 skipped++;
219 continue;
220 }
221 if (error && last_error != EFSCORRUPTED)
222 last_error = error;
223 }
224
225 mutex_unlock(&qi->qi_tree_lock);
226
227 /* bail out if the filesystem is corrupted. */
228 if (last_error == EFSCORRUPTED) {
229 skipped = 0;
230 break;
231 }
232 }
233
234 if (skipped) {
235 delay(1);
236 goto restart;
237 }
238
239 return last_error;
240}
241
242
243/*
244 * Purge a dquot from all tracking data structures and free it.
245 */
246STATIC int
247xfs_qm_dqpurge(
248 struct xfs_dquot *dqp)
249{
250 struct xfs_mount *mp = dqp->q_mount;
251 struct xfs_quotainfo *qi = mp->m_quotainfo;
252 struct xfs_dquot *gdqp = NULL;
253
254 xfs_dqlock(dqp);
255 if ((dqp->dq_flags & XFS_DQ_FREEING) || dqp->q_nrefs != 0) {
256 xfs_dqunlock(dqp);
257 return EAGAIN;
258 }
259
260 /*
261 * If this quota has a group hint attached, prepare for releasing it
262 * now.
263 */
264 gdqp = dqp->q_gdquot;
265 if (gdqp) {
266 xfs_dqlock(gdqp);
267 dqp->q_gdquot = NULL;
268 }
269
270 dqp->dq_flags |= XFS_DQ_FREEING;
271
272 /*
273 * If we're turning off quotas, we have to make sure that, for
274 * example, we don't delete quota disk blocks while dquots are
275 * in the process of getting written to those disk blocks.
276 * This dquot might well be on AIL, and we can't leave it there
277 * if we're turning off quotas. Basically, we need this flush
278 * lock, and are willing to block on it.
279 */
280 if (!xfs_dqflock_nowait(dqp)) {
281 /*
282 * Block on the flush lock after nudging dquot buffer,
283 * if it is incore.
284 */
285 xfs_dqflock_pushbuf_wait(dqp);
286 }
287
288 /*
289 * If we are turning this type of quotas off, we don't care
290 * about the dirty metadata sitting in this dquot. OTOH, if
291 * we're unmounting, we do care, so we flush it and wait.
292 */
293 if (XFS_DQ_IS_DIRTY(dqp)) {
294 int error;
295
296 /*
297 * We don't care about getting disk errors here. We need
298 * to purge this dquot anyway, so we go ahead regardless.
299 */
300 error = xfs_qm_dqflush(dqp, SYNC_WAIT);
301 if (error)
302 xfs_warn(mp, "%s: dquot %p flush failed",
303 __func__, dqp);
304 xfs_dqflock(dqp);
305 }
306
307 ASSERT(atomic_read(&dqp->q_pincount) == 0);
308 ASSERT(XFS_FORCED_SHUTDOWN(mp) ||
309 !(dqp->q_logitem.qli_item.li_flags & XFS_LI_IN_AIL));
310
311 xfs_dqfunlock(dqp);
312 xfs_dqunlock(dqp);
313
314 radix_tree_delete(XFS_DQUOT_TREE(qi, dqp->q_core.d_flags),
315 be32_to_cpu(dqp->q_core.d_id));
316 qi->qi_dquots--;
317
318 /*
319 * We move dquots to the freelist as soon as their reference count
320 * hits zero, so it really should be on the freelist here.
321 */
322 mutex_lock(&qi->qi_lru_lock);
323 ASSERT(!list_empty(&dqp->q_lru));
324 list_del_init(&dqp->q_lru);
325 qi->qi_lru_count--;
326 XFS_STATS_DEC(xs_qm_dquot_unused);
327 mutex_unlock(&qi->qi_lru_lock);
328
329 xfs_qm_dqdestroy(dqp);
330
331 if (gdqp)
332 xfs_qm_dqput(gdqp);
333 return 0;
334}
335
336/*
337 * Purge the dquot cache.
338 */
339void
340xfs_qm_dqpurge_all(
341 struct xfs_mount *mp,
342 uint flags)
343{
344 if (flags & XFS_QMOPT_UQUOTA)
345 xfs_qm_dquot_walk(mp, XFS_DQ_USER, xfs_qm_dqpurge);
346 if (flags & XFS_QMOPT_GQUOTA)
347 xfs_qm_dquot_walk(mp, XFS_DQ_GROUP, xfs_qm_dqpurge);
348 if (flags & XFS_QMOPT_PQUOTA)
349 xfs_qm_dquot_walk(mp, XFS_DQ_PROJ, xfs_qm_dqpurge);
350}
351
352/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 * Just destroy the quotainfo structure.
354 */
355void
Christoph Hellwig7d095252009-06-08 15:33:32 +0200356xfs_qm_unmount(
357 struct xfs_mount *mp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358{
Christoph Hellwig7d095252009-06-08 15:33:32 +0200359 if (mp->m_quotainfo) {
Christoph Hellwig8112e9d2010-04-20 17:02:29 +1000360 xfs_qm_dqpurge_all(mp, XFS_QMOPT_QUOTALL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 xfs_qm_destroy_quotainfo(mp);
Christoph Hellwig7d095252009-06-08 15:33:32 +0200362 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363}
364
365
366/*
367 * This is called from xfs_mountfs to start quotas and initialize all
368 * necessary data structures like quotainfo. This is also responsible for
369 * running a quotacheck as necessary. We are guaranteed that the superblock
370 * is consistently read in at this point.
David Chinner53aa7912008-04-10 12:20:31 +1000371 *
372 * If we fail here, the mount will continue with quota turned off. We don't
373 * need to inidicate success or failure at all.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 */
David Chinner53aa7912008-04-10 12:20:31 +1000375void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376xfs_qm_mount_quotas(
Christoph Hellwig42490232008-08-13 16:49:32 +1000377 xfs_mount_t *mp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 int error = 0;
380 uint sbf;
381
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 /*
383 * If quotas on realtime volumes is not supported, we disable
384 * quotas immediately.
385 */
386 if (mp->m_sb.sb_rextents) {
Dave Chinner0b932cc2011-03-07 10:08:35 +1100387 xfs_notice(mp, "Cannot turn on quotas for realtime filesystem");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 mp->m_qflags = 0;
389 goto write_changes;
390 }
391
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 ASSERT(XFS_IS_QUOTA_RUNNING(mp));
Nathan Scott155ffd02005-09-02 16:43:48 +1000393
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 /*
395 * Allocate the quotainfo structure inside the mount struct, and
396 * create quotainode(s), and change/rev superblock if necessary.
397 */
David Chinner53aa7912008-04-10 12:20:31 +1000398 error = xfs_qm_init_quotainfo(mp);
399 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 /*
401 * We must turn off quotas.
402 */
403 ASSERT(mp->m_quotainfo == NULL);
404 mp->m_qflags = 0;
405 goto write_changes;
406 }
407 /*
408 * If any of the quotas are not consistent, do a quotacheck.
409 */
Christoph Hellwig42490232008-08-13 16:49:32 +1000410 if (XFS_QM_NEED_QUOTACHECK(mp)) {
David Chinner53aa7912008-04-10 12:20:31 +1000411 error = xfs_qm_quotacheck(mp);
412 if (error) {
413 /* Quotacheck failed and disabled quotas. */
414 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 }
Donald Douwsma646d5bd2007-05-08 13:49:09 +1000417 /*
418 * If one type of quotas is off, then it will lose its
419 * quotachecked status, since we won't be doing accounting for
420 * that type anymore.
421 */
David Chinner53aa7912008-04-10 12:20:31 +1000422 if (!XFS_IS_UQUOTA_ON(mp))
Donald Douwsma646d5bd2007-05-08 13:49:09 +1000423 mp->m_qflags &= ~XFS_UQUOTA_CHKD;
David Chinner53aa7912008-04-10 12:20:31 +1000424 if (!(XFS_IS_GQUOTA_ON(mp) || XFS_IS_PQUOTA_ON(mp)))
Donald Douwsma646d5bd2007-05-08 13:49:09 +1000425 mp->m_qflags &= ~XFS_OQUOTA_CHKD;
Nathan Scott155ffd02005-09-02 16:43:48 +1000426
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 write_changes:
428 /*
Eric Sandeen3685c2a2007-10-11 17:42:32 +1000429 * We actually don't have to acquire the m_sb_lock at all.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 * This can only be called from mount, and that's single threaded. XXX
431 */
Eric Sandeen3685c2a2007-10-11 17:42:32 +1000432 spin_lock(&mp->m_sb_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 sbf = mp->m_sb.sb_qflags;
434 mp->m_sb.sb_qflags = mp->m_qflags & XFS_MOUNT_QUOTA_ALL;
Eric Sandeen3685c2a2007-10-11 17:42:32 +1000435 spin_unlock(&mp->m_sb_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436
437 if (sbf != (mp->m_qflags & XFS_MOUNT_QUOTA_ALL)) {
438 if (xfs_qm_write_sb_changes(mp, XFS_SB_QFLAGS)) {
439 /*
440 * We could only have been turning quotas off.
441 * We aren't in very good shape actually because
442 * the incore structures are convinced that quotas are
443 * off, but the on disk superblock doesn't know that !
444 */
445 ASSERT(!(XFS_IS_QUOTA_RUNNING(mp)));
Dave Chinner53487782011-03-07 10:05:35 +1100446 xfs_alert(mp, "%s: Superblock update failed!",
447 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 }
449 }
450
451 if (error) {
Dave Chinner53487782011-03-07 10:05:35 +1100452 xfs_warn(mp, "Failed to initialize disk quotas.");
Christoph Hellwig7d095252009-06-08 15:33:32 +0200453 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455}
456
457/*
458 * Called from the vfsops layer.
459 */
Christoph Hellwige57481d2008-12-03 12:20:36 +0100460void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461xfs_qm_unmount_quotas(
462 xfs_mount_t *mp)
463{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 /*
465 * Release the dquots that root inode, et al might be holding,
466 * before we flush quotas and blow away the quotainfo structure.
467 */
468 ASSERT(mp->m_rootip);
469 xfs_qm_dqdetach(mp->m_rootip);
470 if (mp->m_rbmip)
471 xfs_qm_dqdetach(mp->m_rbmip);
472 if (mp->m_rsumip)
473 xfs_qm_dqdetach(mp->m_rsumip);
474
475 /*
Christoph Hellwige57481d2008-12-03 12:20:36 +0100476 * Release the quota inodes.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 if (mp->m_quotainfo) {
Christoph Hellwige57481d2008-12-03 12:20:36 +0100479 if (mp->m_quotainfo->qi_uquotaip) {
480 IRELE(mp->m_quotainfo->qi_uquotaip);
481 mp->m_quotainfo->qi_uquotaip = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 }
Christoph Hellwige57481d2008-12-03 12:20:36 +0100483 if (mp->m_quotainfo->qi_gquotaip) {
484 IRELE(mp->m_quotainfo->qi_gquotaip);
485 mp->m_quotainfo->qi_gquotaip = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 }
487 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488}
489
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490STATIC int
491xfs_qm_dqattach_one(
492 xfs_inode_t *ip,
493 xfs_dqid_t id,
494 uint type,
495 uint doalloc,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 xfs_dquot_t *udqhint, /* hint */
497 xfs_dquot_t **IO_idqpp)
498{
499 xfs_dquot_t *dqp;
500 int error;
501
Christoph Hellwig579aa9c2008-04-22 17:34:00 +1000502 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 error = 0;
Christoph Hellwig8e9b6e72009-02-08 21:51:42 +0100504
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 /*
506 * See if we already have it in the inode itself. IO_idqpp is
507 * &i_udquot or &i_gdquot. This made the code look weird, but
508 * made the logic a lot simpler.
509 */
Christoph Hellwig8e9b6e72009-02-08 21:51:42 +0100510 dqp = *IO_idqpp;
511 if (dqp) {
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000512 trace_xfs_dqattach_found(dqp);
Christoph Hellwig8e9b6e72009-02-08 21:51:42 +0100513 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 }
515
516 /*
517 * udqhint is the i_udquot field in inode, and is non-NULL only
Nathan Scottc8ad20f2005-06-21 15:38:48 +1000518 * when the type arg is group/project. Its purpose is to save a
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 * lookup by dqid (xfs_qm_dqget) by caching a group dquot inside
520 * the user dquot.
521 */
Christoph Hellwig8e9b6e72009-02-08 21:51:42 +0100522 if (udqhint) {
523 ASSERT(type == XFS_DQ_GROUP || type == XFS_DQ_PROJ);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 xfs_dqlock(udqhint);
525
Christoph Hellwig8e9b6e72009-02-08 21:51:42 +0100526 /*
527 * No need to take dqlock to look at the id.
528 *
529 * The ID can't change until it gets reclaimed, and it won't
530 * be reclaimed as long as we have a ref from inode and we
531 * hold the ilock.
532 */
533 dqp = udqhint->q_gdquot;
534 if (dqp && be32_to_cpu(dqp->q_core.d_id) == id) {
Christoph Hellwig8e9b6e72009-02-08 21:51:42 +0100535 ASSERT(*IO_idqpp == NULL);
Christoph Hellwig8e9b6e72009-02-08 21:51:42 +0100536
Christoph Hellwig78e55892011-12-06 21:58:22 +0000537 *IO_idqpp = xfs_qm_dqhold(dqp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 xfs_dqunlock(udqhint);
Christoph Hellwig8e9b6e72009-02-08 21:51:42 +0100539 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 }
Christoph Hellwig8e9b6e72009-02-08 21:51:42 +0100541
542 /*
543 * We can't hold a dquot lock when we call the dqget code.
544 * We'll deadlock in no time, because of (not conforming to)
545 * lock ordering - the inodelock comes before any dquot lock,
546 * and we may drop and reacquire the ilock in xfs_qm_dqget().
547 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 xfs_dqunlock(udqhint);
Christoph Hellwig8e9b6e72009-02-08 21:51:42 +0100549 }
550
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 /*
552 * Find the dquot from somewhere. This bumps the
553 * reference count of dquot and returns it locked.
554 * This can return ENOENT if dquot didn't exist on
555 * disk and we didn't ask it to allocate;
556 * ESRCH if quotas got turned off suddenly.
557 */
Mitsuo Hayasakadb3e74b2011-11-10 01:33:10 +0000558 error = xfs_qm_dqget(ip->i_mount, ip, id, type,
559 doalloc | XFS_QMOPT_DOWARN, &dqp);
Christoph Hellwig8e9b6e72009-02-08 21:51:42 +0100560 if (error)
561 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000563 trace_xfs_dqattach_get(dqp);
Christoph Hellwig8e9b6e72009-02-08 21:51:42 +0100564
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 /*
566 * dqget may have dropped and re-acquired the ilock, but it guarantees
567 * that the dquot returned is the one that should go in the inode.
568 */
569 *IO_idqpp = dqp;
Christoph Hellwig8e9b6e72009-02-08 21:51:42 +0100570 xfs_dqunlock(dqp);
571 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572}
573
574
575/*
576 * Given a udquot and gdquot, attach a ptr to the group dquot in the
Christoph Hellwigab680bb2011-12-06 21:58:20 +0000577 * udquot as a hint for future lookups.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 */
579STATIC void
580xfs_qm_dqattach_grouphint(
581 xfs_dquot_t *udq,
Christoph Hellwig8e9b6e72009-02-08 21:51:42 +0100582 xfs_dquot_t *gdq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583{
584 xfs_dquot_t *tmp;
585
Christoph Hellwig8e9b6e72009-02-08 21:51:42 +0100586 xfs_dqlock(udq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587
Christoph Hellwigab680bb2011-12-06 21:58:20 +0000588 tmp = udq->q_gdquot;
589 if (tmp) {
590 if (tmp == gdq)
591 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592
593 udq->q_gdquot = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 xfs_qm_dqrele(tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 }
596
Christoph Hellwig78e55892011-12-06 21:58:22 +0000597 udq->q_gdquot = xfs_qm_dqhold(gdq);
Christoph Hellwigab680bb2011-12-06 21:58:20 +0000598done:
Christoph Hellwig8e9b6e72009-02-08 21:51:42 +0100599 xfs_dqunlock(udq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600}
601
602
603/*
Nathan Scottc8ad20f2005-06-21 15:38:48 +1000604 * Given a locked inode, attach dquot(s) to it, taking U/G/P-QUOTAON
605 * into account.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 * If XFS_QMOPT_DQALLOC, the dquot(s) will be allocated if needed.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 * Inode may get unlocked and relocked in here, and the caller must deal with
608 * the consequences.
609 */
610int
Christoph Hellwig7d095252009-06-08 15:33:32 +0200611xfs_qm_dqattach_locked(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 xfs_inode_t *ip,
613 uint flags)
614{
615 xfs_mount_t *mp = ip->i_mount;
616 uint nquotas = 0;
617 int error = 0;
618
Christoph Hellwig7d095252009-06-08 15:33:32 +0200619 if (!XFS_IS_QUOTA_RUNNING(mp) ||
620 !XFS_IS_QUOTA_ON(mp) ||
621 !XFS_NOT_DQATTACHED(mp, ip) ||
622 ip->i_ino == mp->m_sb.sb_uquotino ||
623 ip->i_ino == mp->m_sb.sb_gquotino)
Jesper Juhl014c2542006-01-15 02:37:08 +0100624 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625
Christoph Hellwig7d095252009-06-08 15:33:32 +0200626 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627
628 if (XFS_IS_UQUOTA_ON(mp)) {
629 error = xfs_qm_dqattach_one(ip, ip->i_d.di_uid, XFS_DQ_USER,
630 flags & XFS_QMOPT_DQALLOC,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 NULL, &ip->i_udquot);
632 if (error)
633 goto done;
634 nquotas++;
635 }
Christoph Hellwig579aa9c2008-04-22 17:34:00 +1000636
637 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
Nathan Scottc8ad20f2005-06-21 15:38:48 +1000638 if (XFS_IS_OQUOTA_ON(mp)) {
639 error = XFS_IS_GQUOTA_ON(mp) ?
640 xfs_qm_dqattach_one(ip, ip->i_d.di_gid, XFS_DQ_GROUP,
641 flags & XFS_QMOPT_DQALLOC,
Nathan Scottc8ad20f2005-06-21 15:38:48 +1000642 ip->i_udquot, &ip->i_gdquot) :
Arkadiusz Mi?kiewicz67430992010-09-26 06:10:18 +0000643 xfs_qm_dqattach_one(ip, xfs_get_projid(ip), XFS_DQ_PROJ,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 flags & XFS_QMOPT_DQALLOC,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 ip->i_udquot, &ip->i_gdquot);
646 /*
647 * Don't worry about the udquot that we may have
648 * attached above. It'll get detached, if not already.
649 */
650 if (error)
651 goto done;
652 nquotas++;
653 }
654
655 /*
656 * Attach this group quota to the user quota as a hint.
657 * This WON'T, in general, result in a thrash.
658 */
659 if (nquotas == 2) {
Christoph Hellwig579aa9c2008-04-22 17:34:00 +1000660 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 ASSERT(ip->i_udquot);
662 ASSERT(ip->i_gdquot);
663
664 /*
Christoph Hellwigab680bb2011-12-06 21:58:20 +0000665 * We do not have i_udquot locked at this point, but this check
666 * is OK since we don't depend on the i_gdquot to be accurate
667 * 100% all the time. It is just a hint, and this will
668 * succeed in general.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 */
Christoph Hellwigab680bb2011-12-06 21:58:20 +0000670 if (ip->i_udquot->q_gdquot != ip->i_gdquot)
671 xfs_qm_dqattach_grouphint(ip->i_udquot, ip->i_gdquot);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 }
673
Christoph Hellwig7d095252009-06-08 15:33:32 +0200674 done:
Christoph Hellwigea15ab32011-07-13 13:43:50 +0200675#ifdef DEBUG
676 if (!error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 if (XFS_IS_UQUOTA_ON(mp))
678 ASSERT(ip->i_udquot);
Nathan Scottc8ad20f2005-06-21 15:38:48 +1000679 if (XFS_IS_OQUOTA_ON(mp))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 ASSERT(ip->i_gdquot);
681 }
Christoph Hellwig7d095252009-06-08 15:33:32 +0200682 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683#endif
Christoph Hellwig7d095252009-06-08 15:33:32 +0200684 return error;
685}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686
Christoph Hellwig7d095252009-06-08 15:33:32 +0200687int
688xfs_qm_dqattach(
689 struct xfs_inode *ip,
690 uint flags)
691{
692 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693
Christoph Hellwig7d095252009-06-08 15:33:32 +0200694 xfs_ilock(ip, XFS_ILOCK_EXCL);
695 error = xfs_qm_dqattach_locked(ip, flags);
696 xfs_iunlock(ip, XFS_ILOCK_EXCL);
697
Jesper Juhl014c2542006-01-15 02:37:08 +0100698 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699}
700
701/*
702 * Release dquots (and their references) if any.
703 * The inode should be locked EXCL except when this's called by
704 * xfs_ireclaim.
705 */
706void
707xfs_qm_dqdetach(
708 xfs_inode_t *ip)
709{
710 if (!(ip->i_udquot || ip->i_gdquot))
711 return;
712
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000713 trace_xfs_dquot_dqdetach(ip);
714
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 ASSERT(ip->i_ino != ip->i_mount->m_sb.sb_uquotino);
716 ASSERT(ip->i_ino != ip->i_mount->m_sb.sb_gquotino);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 if (ip->i_udquot) {
718 xfs_qm_dqrele(ip->i_udquot);
719 ip->i_udquot = NULL;
720 }
721 if (ip->i_gdquot) {
722 xfs_qm_dqrele(ip->i_gdquot);
723 ip->i_gdquot = NULL;
724 }
725}
726
Christoph Hellwiga4edd1d2009-01-19 02:03:11 +0100727/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 * This initializes all the quota information that's kept in the
729 * mount structure
730 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +1000731STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732xfs_qm_init_quotainfo(
733 xfs_mount_t *mp)
734{
735 xfs_quotainfo_t *qinf;
736 int error;
737 xfs_dquot_t *dqp;
738
739 ASSERT(XFS_IS_QUOTA_RUNNING(mp));
740
741 /*
742 * Tell XQM that we exist as soon as possible.
743 */
744 if ((error = xfs_qm_hold_quotafs_ref(mp))) {
Jesper Juhl014c2542006-01-15 02:37:08 +0100745 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 }
747
748 qinf = mp->m_quotainfo = kmem_zalloc(sizeof(xfs_quotainfo_t), KM_SLEEP);
749
750 /*
751 * See if quotainodes are setup, and if not, allocate them,
752 * and change the superblock accordingly.
753 */
754 if ((error = xfs_qm_init_quotainos(mp))) {
Denys Vlasenkof0e2d932008-05-19 16:31:57 +1000755 kmem_free(qinf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 mp->m_quotainfo = NULL;
Jesper Juhl014c2542006-01-15 02:37:08 +0100757 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 }
759
Christoph Hellwig9f920f12012-03-13 08:52:35 +0000760 INIT_RADIX_TREE(&qinf->qi_uquota_tree, GFP_NOFS);
761 INIT_RADIX_TREE(&qinf->qi_gquota_tree, GFP_NOFS);
762 mutex_init(&qinf->qi_tree_lock);
763
Christoph Hellwigf8739c32012-03-13 08:52:34 +0000764 INIT_LIST_HEAD(&qinf->qi_lru_list);
765 qinf->qi_lru_count = 0;
766 mutex_init(&qinf->qi_lru_lock);
767
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 /* mutex used to serialize quotaoffs */
Jes Sorensen794ee1b2006-01-09 15:59:21 -0800769 mutex_init(&qinf->qi_quotaofflock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770
771 /* Precalc some constants */
772 qinf->qi_dqchunklen = XFS_FSB_TO_BB(mp, XFS_DQUOT_CLUSTER_SIZE_FSB);
773 ASSERT(qinf->qi_dqchunklen);
774 qinf->qi_dqperchunk = BBTOB(qinf->qi_dqchunklen);
775 do_div(qinf->qi_dqperchunk, sizeof(xfs_dqblk_t));
776
777 mp->m_qflags |= (mp->m_sb.sb_qflags & XFS_ALL_QUOTA_CHKD);
778
779 /*
780 * We try to get the limits from the superuser's limits fields.
781 * This is quite hacky, but it is standard quota practice.
Christoph Hellwig7ae44402011-12-06 21:58:25 +0000782 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 * We look at the USR dquot with id == 0 first, but if user quotas
784 * are not enabled we goto the GRP dquot with id == 0.
785 * We don't really care to keep separate default limits for user
786 * and group quotas, at least not at this point.
Christoph Hellwig7ae44402011-12-06 21:58:25 +0000787 *
788 * Since we may not have done a quotacheck by this point, just read
789 * the dquot without attaching it to any hashtables or lists.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 */
Christoph Hellwig7ae44402011-12-06 21:58:25 +0000791 error = xfs_qm_dqread(mp, 0,
792 XFS_IS_UQUOTA_RUNNING(mp) ? XFS_DQ_USER :
793 (XFS_IS_GQUOTA_RUNNING(mp) ? XFS_DQ_GROUP :
794 XFS_DQ_PROJ),
795 XFS_QMOPT_DOWARN, &dqp);
796 if (!error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 xfs_disk_dquot_t *ddqp = &dqp->q_core;
798
799 /*
800 * The warnings and timers set the grace period given to
801 * a user or group before he or she can not perform any
802 * more writing. If it is zero, a default is used.
803 */
Christoph Hellwig1149d962005-11-02 15:01:12 +1100804 qinf->qi_btimelimit = ddqp->d_btimer ?
805 be32_to_cpu(ddqp->d_btimer) : XFS_QM_BTIMELIMIT;
806 qinf->qi_itimelimit = ddqp->d_itimer ?
807 be32_to_cpu(ddqp->d_itimer) : XFS_QM_ITIMELIMIT;
808 qinf->qi_rtbtimelimit = ddqp->d_rtbtimer ?
809 be32_to_cpu(ddqp->d_rtbtimer) : XFS_QM_RTBTIMELIMIT;
810 qinf->qi_bwarnlimit = ddqp->d_bwarns ?
811 be16_to_cpu(ddqp->d_bwarns) : XFS_QM_BWARNLIMIT;
812 qinf->qi_iwarnlimit = ddqp->d_iwarns ?
813 be16_to_cpu(ddqp->d_iwarns) : XFS_QM_IWARNLIMIT;
814 qinf->qi_rtbwarnlimit = ddqp->d_rtbwarns ?
815 be16_to_cpu(ddqp->d_rtbwarns) : XFS_QM_RTBWARNLIMIT;
816 qinf->qi_bhardlimit = be64_to_cpu(ddqp->d_blk_hardlimit);
817 qinf->qi_bsoftlimit = be64_to_cpu(ddqp->d_blk_softlimit);
818 qinf->qi_ihardlimit = be64_to_cpu(ddqp->d_ino_hardlimit);
819 qinf->qi_isoftlimit = be64_to_cpu(ddqp->d_ino_softlimit);
820 qinf->qi_rtbhardlimit = be64_to_cpu(ddqp->d_rtb_hardlimit);
821 qinf->qi_rtbsoftlimit = be64_to_cpu(ddqp->d_rtb_softlimit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 xfs_qm_dqdestroy(dqp);
824 } else {
825 qinf->qi_btimelimit = XFS_QM_BTIMELIMIT;
826 qinf->qi_itimelimit = XFS_QM_ITIMELIMIT;
827 qinf->qi_rtbtimelimit = XFS_QM_RTBTIMELIMIT;
828 qinf->qi_bwarnlimit = XFS_QM_BWARNLIMIT;
829 qinf->qi_iwarnlimit = XFS_QM_IWARNLIMIT;
Nathan Scott06d10dd2005-06-21 15:48:47 +1000830 qinf->qi_rtbwarnlimit = XFS_QM_RTBWARNLIMIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 }
832
Christoph Hellwigf8739c32012-03-13 08:52:34 +0000833 qinf->qi_shrinker.shrink = xfs_qm_shake;
834 qinf->qi_shrinker.seeks = DEFAULT_SEEKS;
835 register_shrinker(&qinf->qi_shrinker);
Jesper Juhl014c2542006-01-15 02:37:08 +0100836 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837}
838
839
840/*
841 * Gets called when unmounting a filesystem or when all quotas get
842 * turned off.
843 * This purges the quota inodes, destroys locks and frees itself.
844 */
845void
846xfs_qm_destroy_quotainfo(
847 xfs_mount_t *mp)
848{
849 xfs_quotainfo_t *qi;
850
851 qi = mp->m_quotainfo;
852 ASSERT(qi != NULL);
853 ASSERT(xfs_Gqm != NULL);
854
Christoph Hellwigf8739c32012-03-13 08:52:34 +0000855 unregister_shrinker(&qi->qi_shrinker);
856
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 /*
858 * Release the reference that XQM kept, so that we know
859 * when the XQM structure should be freed. We cannot assume
860 * that xfs_Gqm is non-null after this point.
861 */
862 xfs_qm_rele_quotafs_ref(mp);
863
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 if (qi->qi_uquotaip) {
Christoph Hellwig26cc0022008-07-18 17:12:43 +1000865 IRELE(qi->qi_uquotaip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 qi->qi_uquotaip = NULL; /* paranoia */
867 }
868 if (qi->qi_gquotaip) {
Christoph Hellwig26cc0022008-07-18 17:12:43 +1000869 IRELE(qi->qi_gquotaip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 qi->qi_gquotaip = NULL;
871 }
872 mutex_destroy(&qi->qi_quotaofflock);
Denys Vlasenkof0e2d932008-05-19 16:31:57 +1000873 kmem_free(qi);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 mp->m_quotainfo = NULL;
875}
876
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877/*
878 * Create an inode and return with a reference already taken, but unlocked
879 * This is how we create quota inodes
880 */
881STATIC int
882xfs_qm_qino_alloc(
883 xfs_mount_t *mp,
884 xfs_inode_t **ip,
885 __int64_t sbfields,
886 uint flags)
887{
888 xfs_trans_t *tp;
889 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 int committed;
891
Nathan Scott061f7202006-01-11 15:27:50 +1100892 tp = xfs_trans_alloc(mp, XFS_TRANS_QM_QINOCREATE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 if ((error = xfs_trans_reserve(tp,
894 XFS_QM_QINOCREATE_SPACE_RES(mp),
895 XFS_CREATE_LOG_RES(mp), 0,
896 XFS_TRANS_PERM_LOG_RES,
897 XFS_CREATE_LOG_COUNT))) {
898 xfs_trans_cancel(tp, 0);
Jesper Juhl014c2542006-01-15 02:37:08 +0100899 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901
Christoph Hellwig6c77b0e2010-10-06 18:41:17 +0000902 error = xfs_dir_ialloc(&tp, NULL, S_IFREG, 1, 0, 0, 1, ip, &committed);
903 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES |
905 XFS_TRANS_ABORT);
Jesper Juhl014c2542006-01-15 02:37:08 +0100906 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 }
908
909 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 * Make the changes in the superblock, and log those too.
911 * sbfields arg may contain fields other than *QUOTINO;
912 * VERSIONNUM for example.
913 */
Eric Sandeen3685c2a2007-10-11 17:42:32 +1000914 spin_lock(&mp->m_sb_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 if (flags & XFS_QMOPT_SBVERSION) {
Eric Sandeen62118702008-03-06 13:44:28 +1100916 ASSERT(!xfs_sb_version_hasquota(&mp->m_sb));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 ASSERT((sbfields & (XFS_SB_VERSIONNUM | XFS_SB_UQUOTINO |
918 XFS_SB_GQUOTINO | XFS_SB_QFLAGS)) ==
919 (XFS_SB_VERSIONNUM | XFS_SB_UQUOTINO |
920 XFS_SB_GQUOTINO | XFS_SB_QFLAGS));
921
Eric Sandeen62118702008-03-06 13:44:28 +1100922 xfs_sb_version_addquota(&mp->m_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 mp->m_sb.sb_uquotino = NULLFSINO;
924 mp->m_sb.sb_gquotino = NULLFSINO;
925
926 /* qflags will get updated _after_ quotacheck */
927 mp->m_sb.sb_qflags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 }
929 if (flags & XFS_QMOPT_UQUOTA)
930 mp->m_sb.sb_uquotino = (*ip)->i_ino;
931 else
932 mp->m_sb.sb_gquotino = (*ip)->i_ino;
Eric Sandeen3685c2a2007-10-11 17:42:32 +1000933 spin_unlock(&mp->m_sb_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 xfs_mod_sb(tp, sbfields);
935
Eric Sandeen1c72bf92007-05-08 13:48:42 +1000936 if ((error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES))) {
Dave Chinner53487782011-03-07 10:05:35 +1100937 xfs_alert(mp, "%s failed (error %d)!", __func__, error);
Jesper Juhl014c2542006-01-15 02:37:08 +0100938 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 }
Jesper Juhl014c2542006-01-15 02:37:08 +0100940 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941}
942
943
David Chinner5b139732008-04-10 12:20:10 +1000944STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945xfs_qm_reset_dqcounts(
946 xfs_mount_t *mp,
947 xfs_buf_t *bp,
948 xfs_dqid_t id,
949 uint type)
950{
951 xfs_disk_dquot_t *ddq;
952 int j;
953
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000954 trace_xfs_reset_dqcounts(bp, _RET_IP_);
955
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 /*
957 * Reset all counters and timers. They'll be
958 * started afresh by xfs_qm_quotacheck.
959 */
960#ifdef DEBUG
961 j = XFS_FSB_TO_B(mp, XFS_DQUOT_CLUSTER_SIZE_FSB);
962 do_div(j, sizeof(xfs_dqblk_t));
Christoph Hellwig8a7b8a82010-04-20 17:01:30 +1000963 ASSERT(mp->m_quotainfo->qi_dqperchunk == j);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964#endif
Chandra Seetharaman62926042011-07-22 23:40:15 +0000965 ddq = bp->b_addr;
Christoph Hellwig8a7b8a82010-04-20 17:01:30 +1000966 for (j = 0; j < mp->m_quotainfo->qi_dqperchunk; j++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 /*
968 * Do a sanity check, and if needed, repair the dqblk. Don't
969 * output any warnings because it's perfectly possible to
Nathan Scottc41564b2006-03-29 08:55:14 +1000970 * find uninitialised dquot blks. See comment in xfs_qm_dqcheck.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 */
Dave Chinnera0fa2b62011-03-07 10:01:35 +1100972 (void) xfs_qm_dqcheck(mp, ddq, id+j, type, XFS_QMOPT_DQREPAIR,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 "xfs_quotacheck");
Christoph Hellwig1149d962005-11-02 15:01:12 +1100974 ddq->d_bcount = 0;
975 ddq->d_icount = 0;
976 ddq->d_rtbcount = 0;
977 ddq->d_btimer = 0;
978 ddq->d_itimer = 0;
979 ddq->d_rtbtimer = 0;
980 ddq->d_bwarns = 0;
981 ddq->d_iwarns = 0;
982 ddq->d_rtbwarns = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 ddq = (xfs_disk_dquot_t *) ((xfs_dqblk_t *)ddq + 1);
984 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985}
986
987STATIC int
988xfs_qm_dqiter_bufs(
989 xfs_mount_t *mp,
990 xfs_dqid_t firstid,
991 xfs_fsblock_t bno,
992 xfs_filblks_t blkcnt,
993 uint flags)
994{
995 xfs_buf_t *bp;
996 int error;
Nathan Scottc8ad20f2005-06-21 15:38:48 +1000997 int type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998
999 ASSERT(blkcnt > 0);
Nathan Scottc8ad20f2005-06-21 15:38:48 +10001000 type = flags & XFS_QMOPT_UQUOTA ? XFS_DQ_USER :
1001 (flags & XFS_QMOPT_PQUOTA ? XFS_DQ_PROJ : XFS_DQ_GROUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 error = 0;
1003
1004 /*
1005 * Blkcnt arg can be a very big number, and might even be
1006 * larger than the log itself. So, we have to break it up into
1007 * manageable-sized transactions.
1008 * Note that we don't start a permanent transaction here; we might
1009 * not be able to get a log reservation for the whole thing up front,
1010 * and we don't really care to either, because we just discard
1011 * everything if we were to crash in the middle of this loop.
1012 */
1013 while (blkcnt--) {
1014 error = xfs_trans_read_buf(mp, NULL, mp->m_ddev_targp,
1015 XFS_FSB_TO_DADDR(mp, bno),
Christoph Hellwig8a7b8a82010-04-20 17:01:30 +10001016 mp->m_quotainfo->qi_dqchunklen, 0, &bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 if (error)
1018 break;
1019
David Chinner5b139732008-04-10 12:20:10 +10001020 xfs_qm_reset_dqcounts(mp, bp, firstid, type);
Christoph Hellwig61551f12011-08-23 08:28:06 +00001021 xfs_buf_delwri_queue(bp);
1022 xfs_buf_relse(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 /*
1024 * goto the next block.
1025 */
1026 bno++;
Christoph Hellwig8a7b8a82010-04-20 17:01:30 +10001027 firstid += mp->m_quotainfo->qi_dqperchunk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 }
Jesper Juhl014c2542006-01-15 02:37:08 +01001029 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030}
1031
1032/*
Nathan Scottc8ad20f2005-06-21 15:38:48 +10001033 * Iterate over all allocated USR/GRP/PRJ dquots in the system, calling a
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 * caller supplied function for every chunk of dquots that we find.
1035 */
1036STATIC int
1037xfs_qm_dqiterate(
1038 xfs_mount_t *mp,
1039 xfs_inode_t *qip,
1040 uint flags)
1041{
1042 xfs_bmbt_irec_t *map;
1043 int i, nmaps; /* number of map entries */
1044 int error; /* return value */
1045 xfs_fileoff_t lblkno;
1046 xfs_filblks_t maxlblkcnt;
1047 xfs_dqid_t firstid;
1048 xfs_fsblock_t rablkno;
1049 xfs_filblks_t rablkcnt;
1050
1051 error = 0;
1052 /*
Nathan Scottc41564b2006-03-29 08:55:14 +10001053 * This looks racy, but we can't keep an inode lock across a
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 * trans_reserve. But, this gets called during quotacheck, and that
1055 * happens only at mount time which is single threaded.
1056 */
1057 if (qip->i_d.di_nblocks == 0)
Jesper Juhl014c2542006-01-15 02:37:08 +01001058 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059
1060 map = kmem_alloc(XFS_DQITER_MAP_SIZE * sizeof(*map), KM_SLEEP);
1061
1062 lblkno = 0;
1063 maxlblkcnt = XFS_B_TO_FSB(mp, (xfs_ufsize_t)XFS_MAXIOFFSET(mp));
1064 do {
1065 nmaps = XFS_DQITER_MAP_SIZE;
1066 /*
1067 * We aren't changing the inode itself. Just changing
1068 * some of its data. No new blocks are added here, and
1069 * the inode is never added to the transaction.
1070 */
1071 xfs_ilock(qip, XFS_ILOCK_SHARED);
Dave Chinner5c8ed202011-09-18 20:40:45 +00001072 error = xfs_bmapi_read(qip, lblkno, maxlblkcnt - lblkno,
1073 map, &nmaps, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 xfs_iunlock(qip, XFS_ILOCK_SHARED);
1075 if (error)
1076 break;
1077
1078 ASSERT(nmaps <= XFS_DQITER_MAP_SIZE);
1079 for (i = 0; i < nmaps; i++) {
1080 ASSERT(map[i].br_startblock != DELAYSTARTBLOCK);
1081 ASSERT(map[i].br_blockcount);
1082
1083
1084 lblkno += map[i].br_blockcount;
1085
1086 if (map[i].br_startblock == HOLESTARTBLOCK)
1087 continue;
1088
1089 firstid = (xfs_dqid_t) map[i].br_startoff *
Christoph Hellwig8a7b8a82010-04-20 17:01:30 +10001090 mp->m_quotainfo->qi_dqperchunk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 /*
1092 * Do a read-ahead on the next extent.
1093 */
1094 if ((i+1 < nmaps) &&
1095 (map[i+1].br_startblock != HOLESTARTBLOCK)) {
1096 rablkcnt = map[i+1].br_blockcount;
1097 rablkno = map[i+1].br_startblock;
1098 while (rablkcnt--) {
Christoph Hellwig1a1a3e92010-10-06 18:41:18 +00001099 xfs_buf_readahead(mp->m_ddev_targp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 XFS_FSB_TO_DADDR(mp, rablkno),
Christoph Hellwig8a7b8a82010-04-20 17:01:30 +10001101 mp->m_quotainfo->qi_dqchunklen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 rablkno++;
1103 }
1104 }
1105 /*
1106 * Iterate thru all the blks in the extent and
1107 * reset the counters of all the dquots inside them.
1108 */
1109 if ((error = xfs_qm_dqiter_bufs(mp,
1110 firstid,
1111 map[i].br_startblock,
1112 map[i].br_blockcount,
1113 flags))) {
1114 break;
1115 }
1116 }
1117
1118 if (error)
1119 break;
1120 } while (nmaps > 0);
1121
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10001122 kmem_free(map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123
Jesper Juhl014c2542006-01-15 02:37:08 +01001124 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125}
1126
1127/*
1128 * Called by dqusage_adjust in doing a quotacheck.
Christoph Hellwig52fda112010-09-06 01:44:22 +00001129 *
1130 * Given the inode, and a dquot id this updates both the incore dqout as well
1131 * as the buffer copy. This is so that once the quotacheck is done, we can
1132 * just log all the buffers, as opposed to logging numerous updates to
1133 * individual dquots.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 */
Christoph Hellwig52fda112010-09-06 01:44:22 +00001135STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136xfs_qm_quotacheck_dqadjust(
Christoph Hellwig52fda112010-09-06 01:44:22 +00001137 struct xfs_inode *ip,
1138 xfs_dqid_t id,
1139 uint type,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 xfs_qcnt_t nblks,
1141 xfs_qcnt_t rtblks)
1142{
Christoph Hellwig52fda112010-09-06 01:44:22 +00001143 struct xfs_mount *mp = ip->i_mount;
1144 struct xfs_dquot *dqp;
1145 int error;
1146
1147 error = xfs_qm_dqget(mp, ip, id, type,
1148 XFS_QMOPT_DQALLOC | XFS_QMOPT_DOWARN, &dqp);
1149 if (error) {
1150 /*
1151 * Shouldn't be able to turn off quotas here.
1152 */
1153 ASSERT(error != ESRCH);
1154 ASSERT(error != ENOENT);
1155 return error;
1156 }
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001157
1158 trace_xfs_dqadjust(dqp);
1159
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 /*
1161 * Adjust the inode count and the block count to reflect this inode's
1162 * resource usage.
1163 */
Marcin Slusarz413d57c2008-02-13 15:03:29 -08001164 be64_add_cpu(&dqp->q_core.d_icount, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 dqp->q_res_icount++;
1166 if (nblks) {
Marcin Slusarz413d57c2008-02-13 15:03:29 -08001167 be64_add_cpu(&dqp->q_core.d_bcount, nblks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 dqp->q_res_bcount += nblks;
1169 }
1170 if (rtblks) {
Marcin Slusarz413d57c2008-02-13 15:03:29 -08001171 be64_add_cpu(&dqp->q_core.d_rtbcount, rtblks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 dqp->q_res_rtbcount += rtblks;
1173 }
1174
1175 /*
1176 * Set default limits, adjust timers (since we changed usages)
Christoph Hellwig191f8482010-04-20 17:01:53 +10001177 *
1178 * There are no timers for the default values set in the root dquot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 */
Christoph Hellwig191f8482010-04-20 17:01:53 +10001180 if (dqp->q_core.d_id) {
Christoph Hellwig52fda112010-09-06 01:44:22 +00001181 xfs_qm_adjust_dqlimits(mp, &dqp->q_core);
1182 xfs_qm_adjust_dqtimers(mp, &dqp->q_core);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 }
1184
1185 dqp->dq_flags |= XFS_DQ_DIRTY;
Christoph Hellwig52fda112010-09-06 01:44:22 +00001186 xfs_qm_dqput(dqp);
1187 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188}
1189
1190STATIC int
1191xfs_qm_get_rtblks(
1192 xfs_inode_t *ip,
1193 xfs_qcnt_t *O_rtblks)
1194{
1195 xfs_filblks_t rtblks; /* total rt blks */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001196 xfs_extnum_t idx; /* extent record index */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 xfs_ifork_t *ifp; /* inode fork pointer */
1198 xfs_extnum_t nextents; /* number of extent entries */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 int error;
1200
1201 ASSERT(XFS_IS_REALTIME_INODE(ip));
1202 ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
1203 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
1204 if ((error = xfs_iread_extents(NULL, ip, XFS_DATA_FORK)))
Jesper Juhl014c2542006-01-15 02:37:08 +01001205 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 }
1207 rtblks = 0;
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001208 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10001209 for (idx = 0; idx < nextents; idx++)
1210 rtblks += xfs_bmbt_get_blockcount(xfs_iext_get_ext(ifp, idx));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 *O_rtblks = (xfs_qcnt_t)rtblks;
Jesper Juhl014c2542006-01-15 02:37:08 +01001212 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213}
1214
1215/*
1216 * callback routine supplied to bulkstat(). Given an inumber, find its
1217 * dquots and update them to account for resources taken by that inode.
1218 */
1219/* ARGSUSED */
1220STATIC int
1221xfs_qm_dqusage_adjust(
1222 xfs_mount_t *mp, /* mount point for filesystem */
1223 xfs_ino_t ino, /* inode number to get data for */
1224 void __user *buffer, /* not used */
1225 int ubsize, /* not used */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 int *ubused, /* not used */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 int *res) /* result code value */
1228{
1229 xfs_inode_t *ip;
Christoph Hellwig52fda112010-09-06 01:44:22 +00001230 xfs_qcnt_t nblks, rtblks = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 int error;
1232
1233 ASSERT(XFS_IS_QUOTA_RUNNING(mp));
1234
1235 /*
1236 * rootino must have its resources accounted for, not so with the quota
1237 * inodes.
1238 */
1239 if (ino == mp->m_sb.sb_uquotino || ino == mp->m_sb.sb_gquotino) {
1240 *res = BULKSTAT_RV_NOTHING;
1241 return XFS_ERROR(EINVAL);
1242 }
1243
1244 /*
1245 * We don't _need_ to take the ilock EXCL. However, the xfs_qm_dqget
1246 * interface expects the inode to be exclusively locked because that's
1247 * the case in all other instances. It's OK that we do this because
1248 * quotacheck is done only at mount time.
1249 */
Christoph Hellwig52fda112010-09-06 01:44:22 +00001250 error = xfs_iget(mp, NULL, ino, 0, XFS_ILOCK_EXCL, &ip);
1251 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 *res = BULKSTAT_RV_NOTHING;
Jesper Juhl014c2542006-01-15 02:37:08 +01001253 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 }
1255
Christoph Hellwig52fda112010-09-06 01:44:22 +00001256 ASSERT(ip->i_delayed_blks == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257
Christoph Hellwig52fda112010-09-06 01:44:22 +00001258 if (XFS_IS_REALTIME_INODE(ip)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 /*
1260 * Walk thru the extent list and count the realtime blocks.
1261 */
Christoph Hellwig52fda112010-09-06 01:44:22 +00001262 error = xfs_qm_get_rtblks(ip, &rtblks);
1263 if (error)
1264 goto error0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266
Christoph Hellwig52fda112010-09-06 01:44:22 +00001267 nblks = (xfs_qcnt_t)ip->i_d.di_nblocks - rtblks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268
1269 /*
1270 * Add the (disk blocks and inode) resources occupied by this
1271 * inode to its dquots. We do this adjustment in the incore dquot,
1272 * and also copy the changes to its buffer.
1273 * We don't care about putting these changes in a transaction
1274 * envelope because if we crash in the middle of a 'quotacheck'
1275 * we have to start from the beginning anyway.
1276 * Once we're done, we'll log all the dquot bufs.
1277 *
Nathan Scottc41564b2006-03-29 08:55:14 +10001278 * The *QUOTA_ON checks below may look pretty racy, but quotachecks
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 * and quotaoffs don't race. (Quotachecks happen at mount time only).
1280 */
1281 if (XFS_IS_UQUOTA_ON(mp)) {
Christoph Hellwig52fda112010-09-06 01:44:22 +00001282 error = xfs_qm_quotacheck_dqadjust(ip, ip->i_d.di_uid,
1283 XFS_DQ_USER, nblks, rtblks);
1284 if (error)
1285 goto error0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287
Christoph Hellwig52fda112010-09-06 01:44:22 +00001288 if (XFS_IS_GQUOTA_ON(mp)) {
1289 error = xfs_qm_quotacheck_dqadjust(ip, ip->i_d.di_gid,
1290 XFS_DQ_GROUP, nblks, rtblks);
1291 if (error)
1292 goto error0;
1293 }
1294
1295 if (XFS_IS_PQUOTA_ON(mp)) {
Arkadiusz Mi?kiewicz67430992010-09-26 06:10:18 +00001296 error = xfs_qm_quotacheck_dqadjust(ip, xfs_get_projid(ip),
Christoph Hellwig52fda112010-09-06 01:44:22 +00001297 XFS_DQ_PROJ, nblks, rtblks);
1298 if (error)
1299 goto error0;
1300 }
1301
1302 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1303 IRELE(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 *res = BULKSTAT_RV_DIDONE;
Jesper Juhl014c2542006-01-15 02:37:08 +01001305 return 0;
Christoph Hellwig52fda112010-09-06 01:44:22 +00001306
1307error0:
1308 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1309 IRELE(ip);
1310 *res = BULKSTAT_RV_GIVEUP;
1311 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312}
1313
Christoph Hellwigb84a3a92012-03-14 11:53:34 -05001314STATIC int
1315xfs_qm_flush_one(
1316 struct xfs_dquot *dqp)
1317{
1318 int error = 0;
1319
1320 xfs_dqlock(dqp);
1321 if (dqp->dq_flags & XFS_DQ_FREEING)
1322 goto out_unlock;
1323 if (!XFS_DQ_IS_DIRTY(dqp))
1324 goto out_unlock;
1325
1326 if (!xfs_dqflock_nowait(dqp))
1327 xfs_dqflock_pushbuf_wait(dqp);
1328
1329 error = xfs_qm_dqflush(dqp, 0);
1330
1331out_unlock:
1332 xfs_dqunlock(dqp);
1333 return error;
1334}
1335
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336/*
1337 * Walk thru all the filesystem inodes and construct a consistent view
1338 * of the disk quota world. If the quotacheck fails, disable quotas.
1339 */
1340int
1341xfs_qm_quotacheck(
1342 xfs_mount_t *mp)
1343{
Christoph Hellwigb84a3a92012-03-14 11:53:34 -05001344 int done, count, error, error2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 xfs_ino_t lastino;
1346 size_t structsz;
1347 xfs_inode_t *uip, *gip;
1348 uint flags;
1349
1350 count = INT_MAX;
1351 structsz = 1;
1352 lastino = 0;
1353 flags = 0;
1354
Christoph Hellwig8a7b8a82010-04-20 17:01:30 +10001355 ASSERT(mp->m_quotainfo->qi_uquotaip || mp->m_quotainfo->qi_gquotaip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 ASSERT(XFS_IS_QUOTA_RUNNING(mp));
1357
Dave Chinner0b932cc2011-03-07 10:08:35 +11001358 xfs_notice(mp, "Quotacheck needed: Please wait.");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359
1360 /*
Nathan Scottc8ad20f2005-06-21 15:38:48 +10001361 * First we go thru all the dquots on disk, USR and GRP/PRJ, and reset
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 * their counters to zero. We need a clean slate.
1363 * We don't log our changes till later.
1364 */
Christoph Hellwig8a7b8a82010-04-20 17:01:30 +10001365 uip = mp->m_quotainfo->qi_uquotaip;
1366 if (uip) {
1367 error = xfs_qm_dqiterate(mp, uip, XFS_QMOPT_UQUOTA);
1368 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 goto error_return;
1370 flags |= XFS_UQUOTA_CHKD;
1371 }
1372
Christoph Hellwig8a7b8a82010-04-20 17:01:30 +10001373 gip = mp->m_quotainfo->qi_gquotaip;
1374 if (gip) {
1375 error = xfs_qm_dqiterate(mp, gip, XFS_IS_GQUOTA_ON(mp) ?
1376 XFS_QMOPT_GQUOTA : XFS_QMOPT_PQUOTA);
1377 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378 goto error_return;
Nathan Scottc8ad20f2005-06-21 15:38:48 +10001379 flags |= XFS_OQUOTA_CHKD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380 }
1381
1382 do {
1383 /*
1384 * Iterate thru all the inodes in the file system,
1385 * adjusting the corresponding dquot counters in core.
1386 */
Christoph Hellwig7dce11d2010-06-23 18:11:11 +10001387 error = xfs_bulkstat(mp, &lastino, &count,
1388 xfs_qm_dqusage_adjust,
1389 structsz, NULL, &done);
1390 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 break;
1392
Christoph Hellwig7dce11d2010-06-23 18:11:11 +10001393 } while (!done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394
1395 /*
Christoph Hellwigb84a3a92012-03-14 11:53:34 -05001396 * We've made all the changes that we need to make incore. Flush them
1397 * down to disk buffers if everything was updated successfully.
David Chinner4b8879d2008-04-10 12:20:17 +10001398 */
Christoph Hellwigb84a3a92012-03-14 11:53:34 -05001399 if (XFS_IS_UQUOTA_ON(mp))
1400 error = xfs_qm_dquot_walk(mp, XFS_DQ_USER, xfs_qm_flush_one);
1401 if (XFS_IS_GQUOTA_ON(mp)) {
1402 error2 = xfs_qm_dquot_walk(mp, XFS_DQ_GROUP, xfs_qm_flush_one);
1403 if (!error)
1404 error = error2;
1405 }
1406 if (XFS_IS_PQUOTA_ON(mp)) {
1407 error2 = xfs_qm_dquot_walk(mp, XFS_DQ_PROJ, xfs_qm_flush_one);
1408 if (!error)
1409 error = error2;
1410 }
David Chinner4b8879d2008-04-10 12:20:17 +10001411
1412 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 * We can get this error if we couldn't do a dquot allocation inside
1414 * xfs_qm_dqusage_adjust (via bulkstat). We don't care about the
1415 * dirty dquots that might be cached, we just want to get rid of them
1416 * and turn quotaoff. The dquots won't be attached to any of the inodes
1417 * at this point (because we intentionally didn't in dqget_noattach).
1418 */
1419 if (error) {
Christoph Hellwig8112e9d2010-04-20 17:02:29 +10001420 xfs_qm_dqpurge_all(mp, XFS_QMOPT_QUOTALL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421 goto error_return;
1422 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423
1424 /*
1425 * We didn't log anything, because if we crashed, we'll have to
1426 * start the quotacheck from scratch anyway. However, we must make
1427 * sure that our dquot changes are secure before we put the
1428 * quotacheck'd stamp on the superblock. So, here we do a synchronous
1429 * flush.
1430 */
Christoph Hellwiga9add832011-10-10 16:52:52 +00001431 xfs_flush_buftarg(mp->m_ddev_targp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432
1433 /*
1434 * If one type of quotas is off, then it will lose its
1435 * quotachecked status, since we won't be doing accounting for
1436 * that type anymore.
1437 */
Chandra Seetharaman4177af32012-01-23 17:31:43 +00001438 mp->m_qflags &= ~XFS_ALL_QUOTA_CHKD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439 mp->m_qflags |= flags;
1440
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 error_return:
1442 if (error) {
Dave Chinner0b932cc2011-03-07 10:08:35 +11001443 xfs_warn(mp,
1444 "Quotacheck: Unsuccessful (Error %d): Disabling quotas.",
1445 error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 /*
1447 * We must turn off quotas.
1448 */
1449 ASSERT(mp->m_quotainfo != NULL);
1450 ASSERT(xfs_Gqm != NULL);
1451 xfs_qm_destroy_quotainfo(mp);
David Chinner31d55772008-04-10 12:20:38 +10001452 if (xfs_mount_reset_sbqflags(mp)) {
Dave Chinner0b932cc2011-03-07 10:08:35 +11001453 xfs_warn(mp,
1454 "Quotacheck: Failed to reset quota flags.");
David Chinner31d55772008-04-10 12:20:38 +10001455 }
Dave Chinner0b932cc2011-03-07 10:08:35 +11001456 } else
1457 xfs_notice(mp, "Quotacheck: Done.");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458 return (error);
1459}
1460
1461/*
1462 * This is called after the superblock has been read in and we're ready to
1463 * iget the quota inodes.
1464 */
1465STATIC int
1466xfs_qm_init_quotainos(
1467 xfs_mount_t *mp)
1468{
1469 xfs_inode_t *uip, *gip;
1470 int error;
1471 __int64_t sbflags;
1472 uint flags;
1473
1474 ASSERT(mp->m_quotainfo);
1475 uip = gip = NULL;
1476 sbflags = 0;
1477 flags = 0;
1478
1479 /*
1480 * Get the uquota and gquota inodes
1481 */
Eric Sandeen62118702008-03-06 13:44:28 +11001482 if (xfs_sb_version_hasquota(&mp->m_sb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483 if (XFS_IS_UQUOTA_ON(mp) &&
1484 mp->m_sb.sb_uquotino != NULLFSINO) {
1485 ASSERT(mp->m_sb.sb_uquotino > 0);
1486 if ((error = xfs_iget(mp, NULL, mp->m_sb.sb_uquotino,
Dave Chinner7b6259e2010-06-24 11:35:17 +10001487 0, 0, &uip)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488 return XFS_ERROR(error);
1489 }
Nathan Scottc8ad20f2005-06-21 15:38:48 +10001490 if (XFS_IS_OQUOTA_ON(mp) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491 mp->m_sb.sb_gquotino != NULLFSINO) {
1492 ASSERT(mp->m_sb.sb_gquotino > 0);
1493 if ((error = xfs_iget(mp, NULL, mp->m_sb.sb_gquotino,
Dave Chinner7b6259e2010-06-24 11:35:17 +10001494 0, 0, &gip))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 if (uip)
Christoph Hellwig43355092008-03-27 18:01:08 +11001496 IRELE(uip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 return XFS_ERROR(error);
1498 }
1499 }
1500 } else {
1501 flags |= XFS_QMOPT_SBVERSION;
1502 sbflags |= (XFS_SB_VERSIONNUM | XFS_SB_UQUOTINO |
1503 XFS_SB_GQUOTINO | XFS_SB_QFLAGS);
1504 }
1505
1506 /*
1507 * Create the two inodes, if they don't exist already. The changes
1508 * made above will get added to a transaction and logged in one of
1509 * the qino_alloc calls below. If the device is readonly,
1510 * temporarily switch to read-write to do this.
1511 */
1512 if (XFS_IS_UQUOTA_ON(mp) && uip == NULL) {
1513 if ((error = xfs_qm_qino_alloc(mp, &uip,
1514 sbflags | XFS_SB_UQUOTINO,
1515 flags | XFS_QMOPT_UQUOTA)))
1516 return XFS_ERROR(error);
1517
1518 flags &= ~XFS_QMOPT_SBVERSION;
1519 }
Nathan Scottc8ad20f2005-06-21 15:38:48 +10001520 if (XFS_IS_OQUOTA_ON(mp) && gip == NULL) {
1521 flags |= (XFS_IS_GQUOTA_ON(mp) ?
1522 XFS_QMOPT_GQUOTA : XFS_QMOPT_PQUOTA);
1523 error = xfs_qm_qino_alloc(mp, &gip,
1524 sbflags | XFS_SB_GQUOTINO, flags);
1525 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526 if (uip)
Christoph Hellwig43355092008-03-27 18:01:08 +11001527 IRELE(uip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528
1529 return XFS_ERROR(error);
1530 }
1531 }
1532
Christoph Hellwig8a7b8a82010-04-20 17:01:30 +10001533 mp->m_quotainfo->qi_uquotaip = uip;
1534 mp->m_quotainfo->qi_gquotaip = gip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535
Jesper Juhl014c2542006-01-15 02:37:08 +01001536 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537}
1538
Christoph Hellwig92b2e5b32012-02-01 13:57:20 +00001539STATIC void
1540xfs_qm_dqfree_one(
1541 struct xfs_dquot *dqp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542{
Christoph Hellwig92b2e5b32012-02-01 13:57:20 +00001543 struct xfs_mount *mp = dqp->q_mount;
1544 struct xfs_quotainfo *qi = mp->m_quotainfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545
Christoph Hellwig9f920f12012-03-13 08:52:35 +00001546 mutex_lock(&qi->qi_tree_lock);
1547 radix_tree_delete(XFS_DQUOT_TREE(qi, dqp->q_core.d_flags),
1548 be32_to_cpu(dqp->q_core.d_id));
Christoph Hellwigbf72de32011-12-06 21:58:19 +00001549
Christoph Hellwig92b2e5b32012-02-01 13:57:20 +00001550 qi->qi_dquots--;
Christoph Hellwigb84a3a92012-03-14 11:53:34 -05001551 mutex_unlock(&qi->qi_tree_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552
Christoph Hellwig92b2e5b32012-02-01 13:57:20 +00001553 xfs_qm_dqdestroy(dqp);
1554}
Christoph Hellwigbe7ffc32011-12-06 21:58:17 +00001555
Christoph Hellwig92b2e5b32012-02-01 13:57:20 +00001556STATIC void
1557xfs_qm_dqreclaim_one(
1558 struct xfs_dquot *dqp,
1559 struct list_head *dispose_list)
1560{
1561 struct xfs_mount *mp = dqp->q_mount;
Christoph Hellwigf8739c32012-03-13 08:52:34 +00001562 struct xfs_quotainfo *qi = mp->m_quotainfo;
Christoph Hellwig92b2e5b32012-02-01 13:57:20 +00001563 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564
Christoph Hellwig92b2e5b32012-02-01 13:57:20 +00001565 if (!xfs_dqlock_nowait(dqp))
1566 goto out_busy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567
Christoph Hellwig92b2e5b32012-02-01 13:57:20 +00001568 /*
1569 * This dquot has acquired a reference in the meantime remove it from
1570 * the freelist and try again.
1571 */
1572 if (dqp->q_nrefs) {
Christoph Hellwig92678552011-12-06 21:58:18 +00001573 xfs_dqunlock(dqp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574
Christoph Hellwig92b2e5b32012-02-01 13:57:20 +00001575 trace_xfs_dqreclaim_want(dqp);
Christoph Hellwig48776fd2012-03-13 08:52:33 +00001576 XFS_STATS_INC(xs_qm_dqwants);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001577
Christoph Hellwigf8739c32012-03-13 08:52:34 +00001578 list_del_init(&dqp->q_lru);
1579 qi->qi_lru_count--;
Christoph Hellwig48776fd2012-03-13 08:52:33 +00001580 XFS_STATS_DEC(xs_qm_dquot_unused);
Christoph Hellwig92b2e5b32012-02-01 13:57:20 +00001581 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582 }
Christoph Hellwig92678552011-12-06 21:58:18 +00001583
Christoph Hellwig92b2e5b32012-02-01 13:57:20 +00001584 /*
1585 * Try to grab the flush lock. If this dquot is in the process of
1586 * getting flushed to disk, we don't want to reclaim it.
1587 */
1588 if (!xfs_dqflock_nowait(dqp))
1589 goto out_busy;
Dave Chinner368e1362010-04-13 15:06:50 +10001590
Christoph Hellwig92b2e5b32012-02-01 13:57:20 +00001591 /*
1592 * We have the flush lock so we know that this is not in the
1593 * process of being flushed. So, if this is dirty, flush it
1594 * DELWRI so that we don't get a freelist infested with
1595 * dirty dquots.
1596 */
1597 if (XFS_DQ_IS_DIRTY(dqp)) {
1598 trace_xfs_dqreclaim_dirty(dqp);
Dave Chinner368e1362010-04-13 15:06:50 +10001599
Christoph Hellwig92b2e5b32012-02-01 13:57:20 +00001600 /*
1601 * We flush it delayed write, so don't bother releasing the
1602 * freelist lock.
1603 */
1604 error = xfs_qm_dqflush(dqp, 0);
1605 if (error) {
1606 xfs_warn(mp, "%s: dquot %p flush failed",
1607 __func__, dqp);
1608 }
1609
1610 /*
1611 * Give the dquot another try on the freelist, as the
1612 * flushing will take some time.
1613 */
1614 goto out_busy;
Dave Chinner368e1362010-04-13 15:06:50 +10001615 }
Christoph Hellwig92b2e5b32012-02-01 13:57:20 +00001616 xfs_dqfunlock(dqp);
1617
1618 /*
1619 * Prevent lookups now that we are past the point of no return.
1620 */
1621 dqp->dq_flags |= XFS_DQ_FREEING;
1622 xfs_dqunlock(dqp);
1623
1624 ASSERT(dqp->q_nrefs == 0);
Christoph Hellwigf8739c32012-03-13 08:52:34 +00001625 list_move_tail(&dqp->q_lru, dispose_list);
1626 qi->qi_lru_count--;
Christoph Hellwig48776fd2012-03-13 08:52:33 +00001627 XFS_STATS_DEC(xs_qm_dquot_unused);
Christoph Hellwig92b2e5b32012-02-01 13:57:20 +00001628
1629 trace_xfs_dqreclaim_done(dqp);
Christoph Hellwig48776fd2012-03-13 08:52:33 +00001630 XFS_STATS_INC(xs_qm_dqreclaims);
Christoph Hellwig92b2e5b32012-02-01 13:57:20 +00001631 return;
1632
1633out_busy:
1634 xfs_dqunlock(dqp);
1635
1636 /*
1637 * Move the dquot to the tail of the list so that we don't spin on it.
1638 */
Christoph Hellwigf8739c32012-03-13 08:52:34 +00001639 list_move_tail(&dqp->q_lru, &qi->qi_lru_list);
Christoph Hellwig92b2e5b32012-02-01 13:57:20 +00001640
1641 trace_xfs_dqreclaim_busy(dqp);
Christoph Hellwig48776fd2012-03-13 08:52:33 +00001642 XFS_STATS_INC(xs_qm_dqreclaim_misses);
Dave Chinner368e1362010-04-13 15:06:50 +10001643}
1644
Dave Chinner368e1362010-04-13 15:06:50 +10001645STATIC int
Dave Chinner7f8275d2010-07-19 14:56:17 +10001646xfs_qm_shake(
Christoph Hellwig92b2e5b32012-02-01 13:57:20 +00001647 struct shrinker *shrink,
1648 struct shrink_control *sc)
Dave Chinner368e1362010-04-13 15:06:50 +10001649{
Christoph Hellwigf8739c32012-03-13 08:52:34 +00001650 struct xfs_quotainfo *qi =
1651 container_of(shrink, struct xfs_quotainfo, qi_shrinker);
Christoph Hellwig92b2e5b32012-02-01 13:57:20 +00001652 int nr_to_scan = sc->nr_to_scan;
1653 LIST_HEAD (dispose_list);
1654 struct xfs_dquot *dqp;
Dave Chinner368e1362010-04-13 15:06:50 +10001655
Christoph Hellwig92b2e5b32012-02-01 13:57:20 +00001656 if ((sc->gfp_mask & (__GFP_FS|__GFP_WAIT)) != (__GFP_FS|__GFP_WAIT))
Dave Chinner368e1362010-04-13 15:06:50 +10001657 return 0;
Christoph Hellwig92b2e5b32012-02-01 13:57:20 +00001658 if (!nr_to_scan)
1659 goto out;
Dave Chinner368e1362010-04-13 15:06:50 +10001660
Christoph Hellwigf8739c32012-03-13 08:52:34 +00001661 mutex_lock(&qi->qi_lru_lock);
1662 while (!list_empty(&qi->qi_lru_list)) {
Christoph Hellwig92b2e5b32012-02-01 13:57:20 +00001663 if (nr_to_scan-- <= 0)
1664 break;
Christoph Hellwigf8739c32012-03-13 08:52:34 +00001665 dqp = list_first_entry(&qi->qi_lru_list, struct xfs_dquot,
1666 q_lru);
Christoph Hellwig92b2e5b32012-02-01 13:57:20 +00001667 xfs_qm_dqreclaim_one(dqp, &dispose_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668 }
Christoph Hellwigf8739c32012-03-13 08:52:34 +00001669 mutex_unlock(&qi->qi_lru_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670
Christoph Hellwig92b2e5b32012-02-01 13:57:20 +00001671 while (!list_empty(&dispose_list)) {
Christoph Hellwigf8739c32012-03-13 08:52:34 +00001672 dqp = list_first_entry(&dispose_list, struct xfs_dquot, q_lru);
1673 list_del_init(&dqp->q_lru);
Christoph Hellwig92b2e5b32012-02-01 13:57:20 +00001674 xfs_qm_dqfree_one(dqp);
1675 }
1676out:
Christoph Hellwigf8739c32012-03-13 08:52:34 +00001677 return (qi->qi_lru_count / 100) * sysctl_vfs_cache_pressure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678}
1679
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680/*
1681 * Start a transaction and write the incore superblock changes to
1682 * disk. flags parameter indicates which fields have changed.
1683 */
1684int
1685xfs_qm_write_sb_changes(
1686 xfs_mount_t *mp,
1687 __int64_t flags)
1688{
1689 xfs_trans_t *tp;
1690 int error;
1691
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692 tp = xfs_trans_alloc(mp, XFS_TRANS_QM_SBCHANGE);
1693 if ((error = xfs_trans_reserve(tp, 0,
1694 mp->m_sb.sb_sectsize + 128, 0,
1695 0,
1696 XFS_DEFAULT_LOG_COUNT))) {
1697 xfs_trans_cancel(tp, 0);
Jesper Juhl014c2542006-01-15 02:37:08 +01001698 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699 }
1700
1701 xfs_mod_sb(tp, flags);
David Chinnere5720ee2008-04-10 12:21:18 +10001702 error = xfs_trans_commit(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703
David Chinnere5720ee2008-04-10 12:21:18 +10001704 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705}
1706
1707
1708/* --------------- utility functions for vnodeops ---------------- */
1709
1710
1711/*
Christoph Hellwig6c77b0e2010-10-06 18:41:17 +00001712 * Given an inode, a uid, gid and prid make sure that we have
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713 * allocated relevant dquot(s) on disk, and that we won't exceed inode
1714 * quotas by creating this file.
1715 * This also attaches dquot(s) to the given inode after locking it,
1716 * and returns the dquots corresponding to the uid and/or gid.
1717 *
1718 * in : inode (unlocked)
1719 * out : udquot, gdquot with references taken and unlocked
1720 */
1721int
1722xfs_qm_vop_dqalloc(
Christoph Hellwig7d095252009-06-08 15:33:32 +02001723 struct xfs_inode *ip,
1724 uid_t uid,
1725 gid_t gid,
1726 prid_t prid,
1727 uint flags,
1728 struct xfs_dquot **O_udqpp,
1729 struct xfs_dquot **O_gdqpp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730{
Christoph Hellwig7d095252009-06-08 15:33:32 +02001731 struct xfs_mount *mp = ip->i_mount;
1732 struct xfs_dquot *uq, *gq;
1733 int error;
1734 uint lockflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735
Christoph Hellwig7d095252009-06-08 15:33:32 +02001736 if (!XFS_IS_QUOTA_RUNNING(mp) || !XFS_IS_QUOTA_ON(mp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737 return 0;
1738
1739 lockflags = XFS_ILOCK_EXCL;
1740 xfs_ilock(ip, lockflags);
1741
Christoph Hellwigbd186aa2007-08-30 17:21:12 +10001742 if ((flags & XFS_QMOPT_INHERIT) && XFS_INHERIT_GID(ip))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743 gid = ip->i_d.di_gid;
1744
1745 /*
1746 * Attach the dquot(s) to this inode, doing a dquot allocation
1747 * if necessary. The dquot(s) will not be locked.
1748 */
1749 if (XFS_NOT_DQATTACHED(mp, ip)) {
Christoph Hellwig7d095252009-06-08 15:33:32 +02001750 error = xfs_qm_dqattach_locked(ip, XFS_QMOPT_DQALLOC);
1751 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752 xfs_iunlock(ip, lockflags);
Jesper Juhl014c2542006-01-15 02:37:08 +01001753 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754 }
1755 }
1756
1757 uq = gq = NULL;
Nathan Scottc8ad20f2005-06-21 15:38:48 +10001758 if ((flags & XFS_QMOPT_UQUOTA) && XFS_IS_UQUOTA_ON(mp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759 if (ip->i_d.di_uid != uid) {
1760 /*
1761 * What we need is the dquot that has this uid, and
1762 * if we send the inode to dqget, the uid of the inode
1763 * takes priority over what's sent in the uid argument.
1764 * We must unlock inode here before calling dqget if
1765 * we're not sending the inode, because otherwise
1766 * we'll deadlock by doing trans_reserve while
1767 * holding ilock.
1768 */
1769 xfs_iunlock(ip, lockflags);
1770 if ((error = xfs_qm_dqget(mp, NULL, (xfs_dqid_t) uid,
1771 XFS_DQ_USER,
1772 XFS_QMOPT_DQALLOC |
1773 XFS_QMOPT_DOWARN,
1774 &uq))) {
1775 ASSERT(error != ENOENT);
Jesper Juhl014c2542006-01-15 02:37:08 +01001776 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777 }
1778 /*
1779 * Get the ilock in the right order.
1780 */
1781 xfs_dqunlock(uq);
1782 lockflags = XFS_ILOCK_SHARED;
1783 xfs_ilock(ip, lockflags);
1784 } else {
1785 /*
1786 * Take an extra reference, because we'll return
1787 * this to caller
1788 */
1789 ASSERT(ip->i_udquot);
Christoph Hellwig78e55892011-12-06 21:58:22 +00001790 uq = xfs_qm_dqhold(ip->i_udquot);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791 }
1792 }
Nathan Scottc8ad20f2005-06-21 15:38:48 +10001793 if ((flags & XFS_QMOPT_GQUOTA) && XFS_IS_GQUOTA_ON(mp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794 if (ip->i_d.di_gid != gid) {
1795 xfs_iunlock(ip, lockflags);
1796 if ((error = xfs_qm_dqget(mp, NULL, (xfs_dqid_t)gid,
1797 XFS_DQ_GROUP,
1798 XFS_QMOPT_DQALLOC |
1799 XFS_QMOPT_DOWARN,
1800 &gq))) {
1801 if (uq)
1802 xfs_qm_dqrele(uq);
1803 ASSERT(error != ENOENT);
Jesper Juhl014c2542006-01-15 02:37:08 +01001804 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805 }
1806 xfs_dqunlock(gq);
1807 lockflags = XFS_ILOCK_SHARED;
1808 xfs_ilock(ip, lockflags);
1809 } else {
1810 ASSERT(ip->i_gdquot);
Christoph Hellwig78e55892011-12-06 21:58:22 +00001811 gq = xfs_qm_dqhold(ip->i_gdquot);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812 }
Nathan Scottc8ad20f2005-06-21 15:38:48 +10001813 } else if ((flags & XFS_QMOPT_PQUOTA) && XFS_IS_PQUOTA_ON(mp)) {
Arkadiusz Mi?kiewicz67430992010-09-26 06:10:18 +00001814 if (xfs_get_projid(ip) != prid) {
Nathan Scottc8ad20f2005-06-21 15:38:48 +10001815 xfs_iunlock(ip, lockflags);
1816 if ((error = xfs_qm_dqget(mp, NULL, (xfs_dqid_t)prid,
1817 XFS_DQ_PROJ,
1818 XFS_QMOPT_DQALLOC |
1819 XFS_QMOPT_DOWARN,
1820 &gq))) {
1821 if (uq)
1822 xfs_qm_dqrele(uq);
1823 ASSERT(error != ENOENT);
1824 return (error);
1825 }
1826 xfs_dqunlock(gq);
1827 lockflags = XFS_ILOCK_SHARED;
1828 xfs_ilock(ip, lockflags);
1829 } else {
1830 ASSERT(ip->i_gdquot);
Christoph Hellwig78e55892011-12-06 21:58:22 +00001831 gq = xfs_qm_dqhold(ip->i_gdquot);
Nathan Scottc8ad20f2005-06-21 15:38:48 +10001832 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833 }
1834 if (uq)
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001835 trace_xfs_dquot_dqalloc(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836
1837 xfs_iunlock(ip, lockflags);
1838 if (O_udqpp)
1839 *O_udqpp = uq;
1840 else if (uq)
1841 xfs_qm_dqrele(uq);
1842 if (O_gdqpp)
1843 *O_gdqpp = gq;
1844 else if (gq)
1845 xfs_qm_dqrele(gq);
Jesper Juhl014c2542006-01-15 02:37:08 +01001846 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847}
1848
1849/*
1850 * Actually transfer ownership, and do dquot modifications.
1851 * These were already reserved.
1852 */
1853xfs_dquot_t *
1854xfs_qm_vop_chown(
1855 xfs_trans_t *tp,
1856 xfs_inode_t *ip,
1857 xfs_dquot_t **IO_olddq,
1858 xfs_dquot_t *newdq)
1859{
1860 xfs_dquot_t *prevdq;
Nathan Scott06d10dd2005-06-21 15:48:47 +10001861 uint bfield = XFS_IS_REALTIME_INODE(ip) ?
1862 XFS_TRANS_DQ_RTBCOUNT : XFS_TRANS_DQ_BCOUNT;
1863
Christoph Hellwig7d095252009-06-08 15:33:32 +02001864
Christoph Hellwig579aa9c2008-04-22 17:34:00 +10001865 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866 ASSERT(XFS_IS_QUOTA_RUNNING(ip->i_mount));
1867
1868 /* old dquot */
1869 prevdq = *IO_olddq;
1870 ASSERT(prevdq);
1871 ASSERT(prevdq != newdq);
1872
Nathan Scott06d10dd2005-06-21 15:48:47 +10001873 xfs_trans_mod_dquot(tp, prevdq, bfield, -(ip->i_d.di_nblocks));
1874 xfs_trans_mod_dquot(tp, prevdq, XFS_TRANS_DQ_ICOUNT, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875
1876 /* the sparkling new dquot */
Nathan Scott06d10dd2005-06-21 15:48:47 +10001877 xfs_trans_mod_dquot(tp, newdq, bfield, ip->i_d.di_nblocks);
1878 xfs_trans_mod_dquot(tp, newdq, XFS_TRANS_DQ_ICOUNT, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879
1880 /*
Christoph Hellwig78e55892011-12-06 21:58:22 +00001881 * Take an extra reference, because the inode is going to keep
1882 * this dquot pointer even after the trans_commit.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883 */
Christoph Hellwig78e55892011-12-06 21:58:22 +00001884 *IO_olddq = xfs_qm_dqhold(newdq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885
Jesper Juhl014c2542006-01-15 02:37:08 +01001886 return prevdq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887}
1888
1889/*
Nathan Scottc8ad20f2005-06-21 15:38:48 +10001890 * Quota reservations for setattr(AT_UID|AT_GID|AT_PROJID).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891 */
1892int
1893xfs_qm_vop_chown_reserve(
1894 xfs_trans_t *tp,
1895 xfs_inode_t *ip,
1896 xfs_dquot_t *udqp,
1897 xfs_dquot_t *gdqp,
1898 uint flags)
1899{
Christoph Hellwig7d095252009-06-08 15:33:32 +02001900 xfs_mount_t *mp = ip->i_mount;
Nathan Scott9a2a7de2006-03-31 13:04:49 +10001901 uint delblks, blkflags, prjflags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902 xfs_dquot_t *unresudq, *unresgdq, *delblksudq, *delblksgdq;
Christoph Hellwig7d095252009-06-08 15:33:32 +02001903 int error;
1904
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905
Christoph Hellwig579aa9c2008-04-22 17:34:00 +10001906 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_ILOCK_SHARED));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907 ASSERT(XFS_IS_QUOTA_RUNNING(mp));
1908
1909 delblks = ip->i_delayed_blks;
1910 delblksudq = delblksgdq = unresudq = unresgdq = NULL;
Nathan Scott06d10dd2005-06-21 15:48:47 +10001911 blkflags = XFS_IS_REALTIME_INODE(ip) ?
1912 XFS_QMOPT_RES_RTBLKS : XFS_QMOPT_RES_REGBLKS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913
1914 if (XFS_IS_UQUOTA_ON(mp) && udqp &&
Christoph Hellwig1149d962005-11-02 15:01:12 +11001915 ip->i_d.di_uid != (uid_t)be32_to_cpu(udqp->q_core.d_id)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916 delblksudq = udqp;
1917 /*
1918 * If there are delayed allocation blocks, then we have to
1919 * unreserve those from the old dquot, and add them to the
1920 * new dquot.
1921 */
1922 if (delblks) {
1923 ASSERT(ip->i_udquot);
1924 unresudq = ip->i_udquot;
1925 }
1926 }
Nathan Scottc8ad20f2005-06-21 15:38:48 +10001927 if (XFS_IS_OQUOTA_ON(ip->i_mount) && gdqp) {
Nathan Scott9a2a7de2006-03-31 13:04:49 +10001928 if (XFS_IS_PQUOTA_ON(ip->i_mount) &&
Arkadiusz Mi?kiewicz67430992010-09-26 06:10:18 +00001929 xfs_get_projid(ip) != be32_to_cpu(gdqp->q_core.d_id))
Nathan Scott9a2a7de2006-03-31 13:04:49 +10001930 prjflags = XFS_QMOPT_ENOSPC;
1931
1932 if (prjflags ||
1933 (XFS_IS_GQUOTA_ON(ip->i_mount) &&
1934 ip->i_d.di_gid != be32_to_cpu(gdqp->q_core.d_id))) {
Nathan Scottc8ad20f2005-06-21 15:38:48 +10001935 delblksgdq = gdqp;
1936 if (delblks) {
1937 ASSERT(ip->i_gdquot);
1938 unresgdq = ip->i_gdquot;
1939 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940 }
1941 }
1942
1943 if ((error = xfs_trans_reserve_quota_bydquots(tp, ip->i_mount,
1944 delblksudq, delblksgdq, ip->i_d.di_nblocks, 1,
Nathan Scott9a2a7de2006-03-31 13:04:49 +10001945 flags | blkflags | prjflags)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946 return (error);
1947
1948 /*
1949 * Do the delayed blks reservations/unreservations now. Since, these
1950 * are done without the help of a transaction, if a reservation fails
1951 * its previous reservations won't be automatically undone by trans
1952 * code. So, we have to do it manually here.
1953 */
1954 if (delblks) {
1955 /*
1956 * Do the reservations first. Unreservation can't fail.
1957 */
1958 ASSERT(delblksudq || delblksgdq);
1959 ASSERT(unresudq || unresgdq);
1960 if ((error = xfs_trans_reserve_quota_bydquots(NULL, ip->i_mount,
1961 delblksudq, delblksgdq, (xfs_qcnt_t)delblks, 0,
Nathan Scott9a2a7de2006-03-31 13:04:49 +10001962 flags | blkflags | prjflags)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963 return (error);
1964 xfs_trans_reserve_quota_bydquots(NULL, ip->i_mount,
1965 unresudq, unresgdq, -((xfs_qcnt_t)delblks), 0,
Nathan Scott06d10dd2005-06-21 15:48:47 +10001966 blkflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967 }
1968
1969 return (0);
1970}
1971
1972int
1973xfs_qm_vop_rename_dqattach(
Christoph Hellwig7d095252009-06-08 15:33:32 +02001974 struct xfs_inode **i_tab)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975{
Christoph Hellwig7d095252009-06-08 15:33:32 +02001976 struct xfs_mount *mp = i_tab[0]->i_mount;
1977 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978
Christoph Hellwig7d095252009-06-08 15:33:32 +02001979 if (!XFS_IS_QUOTA_RUNNING(mp) || !XFS_IS_QUOTA_ON(mp))
Jesper Juhl014c2542006-01-15 02:37:08 +01001980 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981
Christoph Hellwig7d095252009-06-08 15:33:32 +02001982 for (i = 0; (i < 4 && i_tab[i]); i++) {
1983 struct xfs_inode *ip = i_tab[i];
1984 int error;
1985
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986 /*
1987 * Watch out for duplicate entries in the table.
1988 */
Christoph Hellwig7d095252009-06-08 15:33:32 +02001989 if (i == 0 || ip != i_tab[i-1]) {
1990 if (XFS_NOT_DQATTACHED(mp, ip)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991 error = xfs_qm_dqattach(ip, 0);
1992 if (error)
Jesper Juhl014c2542006-01-15 02:37:08 +01001993 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994 }
1995 }
1996 }
Jesper Juhl014c2542006-01-15 02:37:08 +01001997 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998}
1999
2000void
Christoph Hellwig7d095252009-06-08 15:33:32 +02002001xfs_qm_vop_create_dqattach(
2002 struct xfs_trans *tp,
2003 struct xfs_inode *ip,
2004 struct xfs_dquot *udqp,
2005 struct xfs_dquot *gdqp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006{
Christoph Hellwig7d095252009-06-08 15:33:32 +02002007 struct xfs_mount *mp = tp->t_mountp;
2008
2009 if (!XFS_IS_QUOTA_RUNNING(mp) || !XFS_IS_QUOTA_ON(mp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010 return;
2011
Christoph Hellwig579aa9c2008-04-22 17:34:00 +10002012 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
Christoph Hellwig7d095252009-06-08 15:33:32 +02002013 ASSERT(XFS_IS_QUOTA_RUNNING(mp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014
2015 if (udqp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016 ASSERT(ip->i_udquot == NULL);
Christoph Hellwig7d095252009-06-08 15:33:32 +02002017 ASSERT(XFS_IS_UQUOTA_ON(mp));
Christoph Hellwig1149d962005-11-02 15:01:12 +11002018 ASSERT(ip->i_d.di_uid == be32_to_cpu(udqp->q_core.d_id));
Christoph Hellwig78e55892011-12-06 21:58:22 +00002019
2020 ip->i_udquot = xfs_qm_dqhold(udqp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021 xfs_trans_mod_dquot(tp, udqp, XFS_TRANS_DQ_ICOUNT, 1);
2022 }
2023 if (gdqp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024 ASSERT(ip->i_gdquot == NULL);
Christoph Hellwig7d095252009-06-08 15:33:32 +02002025 ASSERT(XFS_IS_OQUOTA_ON(mp));
2026 ASSERT((XFS_IS_GQUOTA_ON(mp) ?
Arkadiusz Mi?kiewicz67430992010-09-26 06:10:18 +00002027 ip->i_d.di_gid : xfs_get_projid(ip)) ==
Nathan Scottee2a4f72006-01-11 15:33:36 +11002028 be32_to_cpu(gdqp->q_core.d_id));
Christoph Hellwig78e55892011-12-06 21:58:22 +00002029
2030 ip->i_gdquot = xfs_qm_dqhold(gdqp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031 xfs_trans_mod_dquot(tp, gdqp, XFS_TRANS_DQ_ICOUNT, 1);
2032 }
2033}
2034