blob: 1056a21f0300c994e2cc56e147278ea869e278bb [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Implementation of the diskquota system for the LINUX operating system. QUOTA
3 * is implemented using the BSD system call interface as the means of
4 * communication with the user level. This file contains the generic routines
5 * called by the different filesystems on allocation of an inode or block.
6 * These routines take care of the administration needed to have a consistent
7 * diskquota tracking system. The ideas of both user and group quotas are based
8 * on the Melbourne quota system as used on BSD derived systems. The internal
9 * implementation is based on one of the several variants of the LINUX
10 * inode-subsystem with added complexity of the diskquota system.
11 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070012 * Author: Marco van Wieringen <mvw@planets.elm.net>
13 *
14 * Fixes: Dmitry Gorodchanin <pgmdsg@ibi.com>, 11 Feb 96
15 *
16 * Revised list management to avoid races
17 * -- Bill Hawes, <whawes@star.net>, 9/98
18 *
19 * Fixed races in dquot_transfer(), dqget() and dquot_alloc_...().
20 * As the consequence the locking was moved from dquot_decr_...(),
21 * dquot_incr_...() to calling functions.
22 * invalidate_dquots() now writes modified dquots.
23 * Serialized quota_off() and quota_on() for mount point.
24 * Fixed a few bugs in grow_dquots().
25 * Fixed deadlock in write_dquot() - we no longer account quotas on
26 * quota files
27 * remove_dquot_ref() moved to inode.c - it now traverses through inodes
28 * add_dquot_ref() restarts after blocking
29 * Added check for bogus uid and fixed check for group in quotactl.
30 * Jan Kara, <jack@suse.cz>, sponsored by SuSE CR, 10-11/99
31 *
32 * Used struct list_head instead of own list struct
33 * Invalidation of referenced dquots is no longer possible
34 * Improved free_dquots list management
35 * Quota and i_blocks are now updated in one place to avoid races
36 * Warnings are now delayed so we won't block in critical section
37 * Write updated not to require dquot lock
38 * Jan Kara, <jack@suse.cz>, 9/2000
39 *
40 * Added dynamic quota structure allocation
41 * Jan Kara <jack@suse.cz> 12/2000
42 *
43 * Rewritten quota interface. Implemented new quota format and
44 * formats registering.
45 * Jan Kara, <jack@suse.cz>, 2001,2002
46 *
47 * New SMP locking.
48 * Jan Kara, <jack@suse.cz>, 10/2002
49 *
50 * Added journalled quota support, fix lock inversion problems
51 * Jan Kara, <jack@suse.cz>, 2003,2004
52 *
53 * (C) Copyright 1994 - 1997 Marco van Wieringen
54 */
55
56#include <linux/errno.h>
57#include <linux/kernel.h>
58#include <linux/fs.h>
59#include <linux/mount.h>
60#include <linux/mm.h>
61#include <linux/time.h>
62#include <linux/types.h>
63#include <linux/string.h>
64#include <linux/fcntl.h>
65#include <linux/stat.h>
66#include <linux/tty.h>
67#include <linux/file.h>
68#include <linux/slab.h>
69#include <linux/sysctl.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070070#include <linux/init.h>
71#include <linux/module.h>
72#include <linux/proc_fs.h>
73#include <linux/security.h>
74#include <linux/kmod.h>
75#include <linux/namei.h>
76#include <linux/buffer_head.h>
Randy Dunlap16f7e0f2006-01-11 12:17:46 -080077#include <linux/capability.h>
Adrian Bunkbe586ba2005-11-07 00:59:35 -080078#include <linux/quotaops.h>
Christoph Hellwigfb58b732007-02-12 00:51:57 -080079#include <linux/writeback.h> /* for inode_lock, oddly enough.. */
Linus Torvalds1da177e2005-04-16 15:20:36 -070080
81#include <asm/uaccess.h>
82
Linus Torvalds1da177e2005-04-16 15:20:36 -070083/*
Jan Karacc334122009-01-12 17:23:05 +010084 * There are three quota SMP locks. dq_list_lock protects all lists with quotas
Dmitry Monakhovdde95882010-04-26 20:03:33 +040085 * and quota formats.
Jan Karacc334122009-01-12 17:23:05 +010086 * dq_data_lock protects data from dq_dqb and also mem_dqinfo structures and
87 * also guards consistency of dquot->dq_dqb with inode->i_blocks, i_bytes.
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 * i_blocks and i_bytes updates itself are guarded by i_lock acquired directly
Jan Karacc334122009-01-12 17:23:05 +010089 * in inode_add_bytes() and inode_sub_bytes(). dq_state_lock protects
90 * modifications of quota state (on quotaon and quotaoff) and readers who care
91 * about latest values take it as well.
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 *
Jan Karacc334122009-01-12 17:23:05 +010093 * The spinlock ordering is hence: dq_data_lock > dq_list_lock > i_lock,
94 * dq_list_lock > dq_state_lock
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 *
96 * Note that some things (eg. sb pointer, type, id) doesn't change during
97 * the life of the dquot structure and so needn't to be protected by a lock
98 *
99 * Any operation working on dquots via inode pointers must hold dqptr_sem. If
100 * operation is just reading pointers from inode (or not using them at all) the
Jan Kara26245c92010-01-06 17:20:35 +0100101 * read lock is enough. If pointers are altered function must hold write lock.
102 * Special care needs to be taken about S_NOQUOTA inode flag (marking that
103 * inode is a quota file). Functions adding pointers from inode to dquots have
104 * to check this flag under dqptr_sem and then (if S_NOQUOTA is not set) they
105 * have to do all pointer modifications before dropping dqptr_sem. This makes
106 * sure they cannot race with quotaon which first sets S_NOQUOTA flag and
107 * then drops all pointers to dquots from an inode.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 *
Ingo Molnard3be9152006-03-23 03:00:29 -0800109 * Each dquot has its dq_lock mutex. Locked dquots might not be referenced
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 * from inodes (dquot_alloc_space() and such don't check the dq_lock).
111 * Currently dquot is locked only when it is being read to memory (or space for
112 * it is being allocated) on the first dqget() and when it is being released on
113 * the last dqput(). The allocation and release oparations are serialized by
114 * the dq_lock and by checking the use count in dquot_release(). Write
115 * operations on dquots don't hold dq_lock as they copy data under dq_data_lock
116 * spinlock to internal buffers before writing.
117 *
118 * Lock ordering (including related VFS locks) is the following:
Ingo Molnard3be9152006-03-23 03:00:29 -0800119 * i_mutex > dqonoff_sem > journal_lock > dqptr_sem > dquot->dq_lock >
120 * dqio_mutex
Jan Karacc334122009-01-12 17:23:05 +0100121 * The lock ordering of dqptr_sem imposed by quota code is only dqonoff_sem >
122 * dqptr_sem. But filesystem has to count with the fact that functions such as
123 * dquot_alloc_space() acquire dqptr_sem and they usually have to be called
124 * from inside a transaction to keep filesystem consistency after a crash. Also
125 * filesystems usually want to do some IO on dquot from ->mark_dirty which is
126 * called with dqptr_sem held.
Ingo Molnard3be9152006-03-23 03:00:29 -0800127 * i_mutex on quota files is special (it's below dqio_mutex)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 */
129
Jan Karac5166102009-01-26 15:32:46 +0100130static __cacheline_aligned_in_smp DEFINE_SPINLOCK(dq_list_lock);
131static __cacheline_aligned_in_smp DEFINE_SPINLOCK(dq_state_lock);
132__cacheline_aligned_in_smp DEFINE_SPINLOCK(dq_data_lock);
Mingming Cao08d03502009-01-14 16:19:32 +0100133EXPORT_SYMBOL(dq_data_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134
Sergey Senozhatskyda8d1ba2010-04-26 12:09:26 +0200135#if defined(CONFIG_QUOTA_DEBUG) || defined(CONFIG_PRINT_QUOTA_WARNING)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136static char *quotatypes[] = INITQFNAMES;
Sergey Senozhatskyda8d1ba2010-04-26 12:09:26 +0200137#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138static struct quota_format_type *quota_formats; /* List of registered formats */
139static struct quota_module_name module_names[] = INIT_QUOTA_MODULE_NAMES;
140
141/* SLAB cache for dquot structures */
Christoph Lametere18b8902006-12-06 20:33:20 -0800142static struct kmem_cache *dquot_cachep;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143
144int register_quota_format(struct quota_format_type *fmt)
145{
146 spin_lock(&dq_list_lock);
147 fmt->qf_next = quota_formats;
148 quota_formats = fmt;
149 spin_unlock(&dq_list_lock);
150 return 0;
151}
Mingming Cao08d03502009-01-14 16:19:32 +0100152EXPORT_SYMBOL(register_quota_format);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153
154void unregister_quota_format(struct quota_format_type *fmt)
155{
156 struct quota_format_type **actqf;
157
158 spin_lock(&dq_list_lock);
Jan Kara268157b2009-01-27 15:47:22 +0100159 for (actqf = &quota_formats; *actqf && *actqf != fmt;
160 actqf = &(*actqf)->qf_next)
161 ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 if (*actqf)
163 *actqf = (*actqf)->qf_next;
164 spin_unlock(&dq_list_lock);
165}
Mingming Cao08d03502009-01-14 16:19:32 +0100166EXPORT_SYMBOL(unregister_quota_format);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167
168static struct quota_format_type *find_quota_format(int id)
169{
170 struct quota_format_type *actqf;
171
172 spin_lock(&dq_list_lock);
Jan Kara268157b2009-01-27 15:47:22 +0100173 for (actqf = quota_formats; actqf && actqf->qf_fmt_id != id;
174 actqf = actqf->qf_next)
175 ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 if (!actqf || !try_module_get(actqf->qf_owner)) {
177 int qm;
178
179 spin_unlock(&dq_list_lock);
180
Jan Kara268157b2009-01-27 15:47:22 +0100181 for (qm = 0; module_names[qm].qm_fmt_id &&
182 module_names[qm].qm_fmt_id != id; qm++)
183 ;
184 if (!module_names[qm].qm_fmt_id ||
185 request_module(module_names[qm].qm_mod_name))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 return NULL;
187
188 spin_lock(&dq_list_lock);
Jan Kara268157b2009-01-27 15:47:22 +0100189 for (actqf = quota_formats; actqf && actqf->qf_fmt_id != id;
190 actqf = actqf->qf_next)
191 ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 if (actqf && !try_module_get(actqf->qf_owner))
193 actqf = NULL;
194 }
195 spin_unlock(&dq_list_lock);
196 return actqf;
197}
198
199static void put_quota_format(struct quota_format_type *fmt)
200{
201 module_put(fmt->qf_owner);
202}
203
204/*
205 * Dquot List Management:
206 * The quota code uses three lists for dquot management: the inuse_list,
207 * free_dquots, and dquot_hash[] array. A single dquot structure may be
208 * on all three lists, depending on its current state.
209 *
210 * All dquots are placed to the end of inuse_list when first created, and this
211 * list is used for invalidate operation, which must look at every dquot.
212 *
213 * Unused dquots (dq_count == 0) are added to the free_dquots list when freed,
214 * and this list is searched whenever we need an available dquot. Dquots are
215 * removed from the list as soon as they are used again, and
216 * dqstats.free_dquots gives the number of dquots on the list. When
217 * dquot is invalidated it's completely released from memory.
218 *
219 * Dquots with a specific identity (device, type and id) are placed on
220 * one of the dquot_hash[] hash chains. The provides an efficient search
221 * mechanism to locate a specific dquot.
222 */
223
224static LIST_HEAD(inuse_list);
225static LIST_HEAD(free_dquots);
226static unsigned int dq_hash_bits, dq_hash_mask;
227static struct hlist_head *dquot_hash;
228
229struct dqstats dqstats;
Mingming Cao08d03502009-01-14 16:19:32 +0100230EXPORT_SYMBOL(dqstats);
Dmitry Monakhovdde95882010-04-26 20:03:33 +0400231#ifdef CONFIG_SMP
232struct dqstats *dqstats_pcpu;
233EXPORT_SYMBOL(dqstats_pcpu);
234#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235
Jan Kara0a5a9c72010-02-09 18:20:39 +0100236static qsize_t inode_get_rsv_space(struct inode *inode);
Christoph Hellwig871a2932010-03-03 09:05:07 -0500237static void __dquot_initialize(struct inode *inode, int type);
Jan Kara0a5a9c72010-02-09 18:20:39 +0100238
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239static inline unsigned int
240hashfn(const struct super_block *sb, unsigned int id, int type)
241{
242 unsigned long tmp;
243
244 tmp = (((unsigned long)sb>>L1_CACHE_SHIFT) ^ id) * (MAXQUOTAS - type);
245 return (tmp + (tmp >> dq_hash_bits)) & dq_hash_mask;
246}
247
248/*
249 * Following list functions expect dq_list_lock to be held
250 */
251static inline void insert_dquot_hash(struct dquot *dquot)
252{
Jan Kara268157b2009-01-27 15:47:22 +0100253 struct hlist_head *head;
254 head = dquot_hash + hashfn(dquot->dq_sb, dquot->dq_id, dquot->dq_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 hlist_add_head(&dquot->dq_hash, head);
256}
257
258static inline void remove_dquot_hash(struct dquot *dquot)
259{
260 hlist_del_init(&dquot->dq_hash);
261}
262
Jan Kara7a2435d2009-01-27 01:47:11 +0100263static struct dquot *find_dquot(unsigned int hashent, struct super_block *sb,
264 unsigned int id, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265{
266 struct hlist_node *node;
267 struct dquot *dquot;
268
269 hlist_for_each (node, dquot_hash+hashent) {
270 dquot = hlist_entry(node, struct dquot, dq_hash);
Jan Kara268157b2009-01-27 15:47:22 +0100271 if (dquot->dq_sb == sb && dquot->dq_id == id &&
272 dquot->dq_type == type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 return dquot;
274 }
Jan Karadd6f3c62009-01-26 16:01:43 +0100275 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276}
277
278/* Add a dquot to the tail of the free list */
279static inline void put_dquot_last(struct dquot *dquot)
280{
Akinobu Mita8e130592006-06-26 00:24:37 -0700281 list_add_tail(&dquot->dq_free, &free_dquots);
Dmitry Monakhovdde95882010-04-26 20:03:33 +0400282 dqstats_inc(DQST_FREE_DQUOTS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283}
284
285static inline void remove_free_dquot(struct dquot *dquot)
286{
287 if (list_empty(&dquot->dq_free))
288 return;
289 list_del_init(&dquot->dq_free);
Dmitry Monakhovdde95882010-04-26 20:03:33 +0400290 dqstats_dec(DQST_FREE_DQUOTS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291}
292
293static inline void put_inuse(struct dquot *dquot)
294{
295 /* We add to the back of inuse list so we don't have to restart
296 * when traversing this list and we block */
Akinobu Mita8e130592006-06-26 00:24:37 -0700297 list_add_tail(&dquot->dq_inuse, &inuse_list);
Dmitry Monakhovdde95882010-04-26 20:03:33 +0400298 dqstats_inc(DQST_ALLOC_DQUOTS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299}
300
301static inline void remove_inuse(struct dquot *dquot)
302{
Dmitry Monakhovdde95882010-04-26 20:03:33 +0400303 dqstats_dec(DQST_ALLOC_DQUOTS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 list_del(&dquot->dq_inuse);
305}
306/*
307 * End of list functions needing dq_list_lock
308 */
309
310static void wait_on_dquot(struct dquot *dquot)
311{
Ingo Molnard3be9152006-03-23 03:00:29 -0800312 mutex_lock(&dquot->dq_lock);
313 mutex_unlock(&dquot->dq_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314}
315
Jan Kara03f6e922008-04-28 02:14:32 -0700316static inline int dquot_dirty(struct dquot *dquot)
317{
318 return test_bit(DQ_MOD_B, &dquot->dq_flags);
319}
320
321static inline int mark_dquot_dirty(struct dquot *dquot)
322{
323 return dquot->dq_sb->dq_op->mark_dirty(dquot);
324}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325
Dmitry Monakhoveabf2902010-03-27 15:15:38 +0300326/* Mark dquot dirty in atomic manner, and return it's old dirty flag state */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327int dquot_mark_dquot_dirty(struct dquot *dquot)
328{
Dmitry Monakhoveabf2902010-03-27 15:15:38 +0300329 int ret = 1;
330
331 /* If quota is dirty already, we don't have to acquire dq_list_lock */
332 if (test_bit(DQ_MOD_B, &dquot->dq_flags))
333 return 1;
334
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 spin_lock(&dq_list_lock);
Dmitry Monakhoveabf2902010-03-27 15:15:38 +0300336 if (!test_and_set_bit(DQ_MOD_B, &dquot->dq_flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 list_add(&dquot->dq_dirty, &sb_dqopt(dquot->dq_sb)->
338 info[dquot->dq_type].dqi_dirty_list);
Dmitry Monakhoveabf2902010-03-27 15:15:38 +0300339 ret = 0;
340 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 spin_unlock(&dq_list_lock);
Dmitry Monakhoveabf2902010-03-27 15:15:38 +0300342 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343}
Mingming Cao08d03502009-01-14 16:19:32 +0100344EXPORT_SYMBOL(dquot_mark_dquot_dirty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345
Dmitry Monakhovdc52dd32009-12-14 15:21:15 +0300346/* Dirtify all the dquots - this can block when journalling */
347static inline int mark_all_dquot_dirty(struct dquot * const *dquot)
348{
349 int ret, err, cnt;
350
351 ret = err = 0;
352 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
353 if (dquot[cnt])
354 /* Even in case of error we have to continue */
355 ret = mark_dquot_dirty(dquot[cnt]);
356 if (!err)
357 err = ret;
358 }
359 return err;
360}
361
362static inline void dqput_all(struct dquot **dquot)
363{
364 unsigned int cnt;
365
366 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
367 dqput(dquot[cnt]);
368}
369
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370/* This function needs dq_list_lock */
371static inline int clear_dquot_dirty(struct dquot *dquot)
372{
373 if (!test_and_clear_bit(DQ_MOD_B, &dquot->dq_flags))
374 return 0;
375 list_del_init(&dquot->dq_dirty);
376 return 1;
377}
378
379void mark_info_dirty(struct super_block *sb, int type)
380{
381 set_bit(DQF_INFO_DIRTY_B, &sb_dqopt(sb)->info[type].dqi_flags);
382}
383EXPORT_SYMBOL(mark_info_dirty);
384
385/*
386 * Read dquot from disk and alloc space for it
387 */
388
389int dquot_acquire(struct dquot *dquot)
390{
391 int ret = 0, ret2 = 0;
392 struct quota_info *dqopt = sb_dqopt(dquot->dq_sb);
393
Ingo Molnard3be9152006-03-23 03:00:29 -0800394 mutex_lock(&dquot->dq_lock);
395 mutex_lock(&dqopt->dqio_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 if (!test_bit(DQ_READ_B, &dquot->dq_flags))
397 ret = dqopt->ops[dquot->dq_type]->read_dqblk(dquot);
398 if (ret < 0)
399 goto out_iolock;
400 set_bit(DQ_READ_B, &dquot->dq_flags);
401 /* Instantiate dquot if needed */
402 if (!test_bit(DQ_ACTIVE_B, &dquot->dq_flags) && !dquot->dq_off) {
403 ret = dqopt->ops[dquot->dq_type]->commit_dqblk(dquot);
404 /* Write the info if needed */
Jan Kara268157b2009-01-27 15:47:22 +0100405 if (info_dirty(&dqopt->info[dquot->dq_type])) {
406 ret2 = dqopt->ops[dquot->dq_type]->write_file_info(
407 dquot->dq_sb, dquot->dq_type);
408 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 if (ret < 0)
410 goto out_iolock;
411 if (ret2 < 0) {
412 ret = ret2;
413 goto out_iolock;
414 }
415 }
416 set_bit(DQ_ACTIVE_B, &dquot->dq_flags);
417out_iolock:
Ingo Molnard3be9152006-03-23 03:00:29 -0800418 mutex_unlock(&dqopt->dqio_mutex);
419 mutex_unlock(&dquot->dq_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 return ret;
421}
Mingming Cao08d03502009-01-14 16:19:32 +0100422EXPORT_SYMBOL(dquot_acquire);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423
424/*
425 * Write dquot to disk
426 */
427int dquot_commit(struct dquot *dquot)
428{
429 int ret = 0, ret2 = 0;
430 struct quota_info *dqopt = sb_dqopt(dquot->dq_sb);
431
Ingo Molnard3be9152006-03-23 03:00:29 -0800432 mutex_lock(&dqopt->dqio_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 spin_lock(&dq_list_lock);
434 if (!clear_dquot_dirty(dquot)) {
435 spin_unlock(&dq_list_lock);
436 goto out_sem;
437 }
438 spin_unlock(&dq_list_lock);
439 /* Inactive dquot can be only if there was error during read/init
440 * => we have better not writing it */
441 if (test_bit(DQ_ACTIVE_B, &dquot->dq_flags)) {
442 ret = dqopt->ops[dquot->dq_type]->commit_dqblk(dquot);
Jan Kara268157b2009-01-27 15:47:22 +0100443 if (info_dirty(&dqopt->info[dquot->dq_type])) {
444 ret2 = dqopt->ops[dquot->dq_type]->write_file_info(
445 dquot->dq_sb, dquot->dq_type);
446 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 if (ret >= 0)
448 ret = ret2;
449 }
450out_sem:
Ingo Molnard3be9152006-03-23 03:00:29 -0800451 mutex_unlock(&dqopt->dqio_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 return ret;
453}
Mingming Cao08d03502009-01-14 16:19:32 +0100454EXPORT_SYMBOL(dquot_commit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455
456/*
457 * Release dquot
458 */
459int dquot_release(struct dquot *dquot)
460{
461 int ret = 0, ret2 = 0;
462 struct quota_info *dqopt = sb_dqopt(dquot->dq_sb);
463
Ingo Molnard3be9152006-03-23 03:00:29 -0800464 mutex_lock(&dquot->dq_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 /* Check whether we are not racing with some other dqget() */
466 if (atomic_read(&dquot->dq_count) > 1)
467 goto out_dqlock;
Ingo Molnard3be9152006-03-23 03:00:29 -0800468 mutex_lock(&dqopt->dqio_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 if (dqopt->ops[dquot->dq_type]->release_dqblk) {
470 ret = dqopt->ops[dquot->dq_type]->release_dqblk(dquot);
471 /* Write the info */
Jan Kara268157b2009-01-27 15:47:22 +0100472 if (info_dirty(&dqopt->info[dquot->dq_type])) {
473 ret2 = dqopt->ops[dquot->dq_type]->write_file_info(
474 dquot->dq_sb, dquot->dq_type);
475 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 if (ret >= 0)
477 ret = ret2;
478 }
479 clear_bit(DQ_ACTIVE_B, &dquot->dq_flags);
Ingo Molnard3be9152006-03-23 03:00:29 -0800480 mutex_unlock(&dqopt->dqio_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481out_dqlock:
Ingo Molnard3be9152006-03-23 03:00:29 -0800482 mutex_unlock(&dquot->dq_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 return ret;
484}
Mingming Cao08d03502009-01-14 16:19:32 +0100485EXPORT_SYMBOL(dquot_release);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486
Jan Kara7d9056b2008-11-25 15:31:32 +0100487void dquot_destroy(struct dquot *dquot)
Jan Kara74f783a2008-08-19 14:51:22 +0200488{
489 kmem_cache_free(dquot_cachep, dquot);
490}
Jan Kara7d9056b2008-11-25 15:31:32 +0100491EXPORT_SYMBOL(dquot_destroy);
Jan Kara74f783a2008-08-19 14:51:22 +0200492
493static inline void do_destroy_dquot(struct dquot *dquot)
494{
495 dquot->dq_sb->dq_op->destroy_dquot(dquot);
496}
497
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498/* Invalidate all dquots on the list. Note that this function is called after
499 * quota is disabled and pointers from inodes removed so there cannot be new
Jan Kara6362e4d2006-03-23 03:00:17 -0800500 * quota users. There can still be some users of quotas due to inodes being
501 * just deleted or pruned by prune_icache() (those are not attached to any
Jan Karacc334122009-01-12 17:23:05 +0100502 * list) or parallel quotactl call. We have to wait for such users.
Jan Kara6362e4d2006-03-23 03:00:17 -0800503 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504static void invalidate_dquots(struct super_block *sb, int type)
505{
Domen Puncerc33ed272005-06-25 14:59:36 -0700506 struct dquot *dquot, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507
Jan Kara6362e4d2006-03-23 03:00:17 -0800508restart:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 spin_lock(&dq_list_lock);
Domen Puncerc33ed272005-06-25 14:59:36 -0700510 list_for_each_entry_safe(dquot, tmp, &inuse_list, dq_inuse) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 if (dquot->dq_sb != sb)
512 continue;
513 if (dquot->dq_type != type)
514 continue;
Jan Kara6362e4d2006-03-23 03:00:17 -0800515 /* Wait for dquot users */
516 if (atomic_read(&dquot->dq_count)) {
517 DEFINE_WAIT(wait);
518
519 atomic_inc(&dquot->dq_count);
520 prepare_to_wait(&dquot->dq_wait_unused, &wait,
521 TASK_UNINTERRUPTIBLE);
522 spin_unlock(&dq_list_lock);
523 /* Once dqput() wakes us up, we know it's time to free
524 * the dquot.
525 * IMPORTANT: we rely on the fact that there is always
526 * at most one process waiting for dquot to free.
527 * Otherwise dq_count would be > 1 and we would never
528 * wake up.
529 */
530 if (atomic_read(&dquot->dq_count) > 1)
531 schedule();
532 finish_wait(&dquot->dq_wait_unused, &wait);
533 dqput(dquot);
534 /* At this moment dquot() need not exist (it could be
535 * reclaimed by prune_dqcache(). Hence we must
536 * restart. */
537 goto restart;
538 }
539 /*
540 * Quota now has no users and it has been written on last
541 * dqput()
542 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 remove_dquot_hash(dquot);
544 remove_free_dquot(dquot);
545 remove_inuse(dquot);
Jan Kara74f783a2008-08-19 14:51:22 +0200546 do_destroy_dquot(dquot);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 }
548 spin_unlock(&dq_list_lock);
549}
550
Jan Kara12c77522008-10-20 17:05:00 +0200551/* Call callback for every active dquot on given filesystem */
552int dquot_scan_active(struct super_block *sb,
553 int (*fn)(struct dquot *dquot, unsigned long priv),
554 unsigned long priv)
555{
556 struct dquot *dquot, *old_dquot = NULL;
557 int ret = 0;
558
559 mutex_lock(&sb_dqopt(sb)->dqonoff_mutex);
560 spin_lock(&dq_list_lock);
561 list_for_each_entry(dquot, &inuse_list, dq_inuse) {
562 if (!test_bit(DQ_ACTIVE_B, &dquot->dq_flags))
563 continue;
564 if (dquot->dq_sb != sb)
565 continue;
566 /* Now we have active dquot so we can just increase use count */
567 atomic_inc(&dquot->dq_count);
Jan Kara12c77522008-10-20 17:05:00 +0200568 spin_unlock(&dq_list_lock);
Dmitry Monakhovdde95882010-04-26 20:03:33 +0400569 dqstats_inc(DQST_LOOKUPS);
Jan Kara12c77522008-10-20 17:05:00 +0200570 dqput(old_dquot);
571 old_dquot = dquot;
572 ret = fn(dquot, priv);
573 if (ret < 0)
574 goto out;
575 spin_lock(&dq_list_lock);
576 /* We are safe to continue now because our dquot could not
577 * be moved out of the inuse list while we hold the reference */
578 }
579 spin_unlock(&dq_list_lock);
580out:
581 dqput(old_dquot);
582 mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
583 return ret;
584}
Mingming Cao08d03502009-01-14 16:19:32 +0100585EXPORT_SYMBOL(dquot_scan_active);
Jan Kara12c77522008-10-20 17:05:00 +0200586
Christoph Hellwig5fb324a2010-02-16 03:44:52 -0500587int vfs_quota_sync(struct super_block *sb, int type, int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588{
589 struct list_head *dirty;
590 struct dquot *dquot;
591 struct quota_info *dqopt = sb_dqopt(sb);
592 int cnt;
593
Ingo Molnard3be9152006-03-23 03:00:29 -0800594 mutex_lock(&dqopt->dqonoff_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
596 if (type != -1 && cnt != type)
597 continue;
Jan Karaf55abc02008-08-20 17:50:32 +0200598 if (!sb_has_quota_active(sb, cnt))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 continue;
600 spin_lock(&dq_list_lock);
601 dirty = &dqopt->info[cnt].dqi_dirty_list;
602 while (!list_empty(dirty)) {
Jan Kara268157b2009-01-27 15:47:22 +0100603 dquot = list_first_entry(dirty, struct dquot,
604 dq_dirty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 /* Dirty and inactive can be only bad dquot... */
606 if (!test_bit(DQ_ACTIVE_B, &dquot->dq_flags)) {
607 clear_dquot_dirty(dquot);
608 continue;
609 }
610 /* Now we have active dquot from which someone is
611 * holding reference so we can safely just increase
612 * use count */
613 atomic_inc(&dquot->dq_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 spin_unlock(&dq_list_lock);
Dmitry Monakhovdde95882010-04-26 20:03:33 +0400615 dqstats_inc(DQST_LOOKUPS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 sb->dq_op->write_dquot(dquot);
617 dqput(dquot);
618 spin_lock(&dq_list_lock);
619 }
620 spin_unlock(&dq_list_lock);
621 }
622
623 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
Jan Karaf55abc02008-08-20 17:50:32 +0200624 if ((cnt == type || type == -1) && sb_has_quota_active(sb, cnt)
625 && info_dirty(&dqopt->info[cnt]))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 sb->dq_op->write_info(sb, cnt);
Dmitry Monakhovdde95882010-04-26 20:03:33 +0400627 dqstats_inc(DQST_SYNCS);
Ingo Molnard3be9152006-03-23 03:00:29 -0800628 mutex_unlock(&dqopt->dqonoff_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629
Christoph Hellwig5fb324a2010-02-16 03:44:52 -0500630 if (!wait || (sb_dqopt(sb)->flags & DQUOT_QUOTA_SYS_FILE))
631 return 0;
632
633 /* This is not very clever (and fast) but currently I don't know about
634 * any other simple way of getting quota data to disk and we must get
635 * them there for userspace to be visible... */
636 if (sb->s_op->sync_fs)
637 sb->s_op->sync_fs(sb, 1);
638 sync_blockdev(sb->s_bdev);
639
640 /*
641 * Now when everything is written we can discard the pagecache so
642 * that userspace sees the changes.
643 */
644 mutex_lock(&sb_dqopt(sb)->dqonoff_mutex);
645 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
646 if (type != -1 && cnt != type)
647 continue;
648 if (!sb_has_quota_active(sb, cnt))
649 continue;
650 mutex_lock_nested(&sb_dqopt(sb)->files[cnt]->i_mutex,
651 I_MUTEX_QUOTA);
652 truncate_inode_pages(&sb_dqopt(sb)->files[cnt]->i_data, 0);
653 mutex_unlock(&sb_dqopt(sb)->files[cnt]->i_mutex);
654 }
655 mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
656
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 return 0;
658}
Mingming Cao08d03502009-01-14 16:19:32 +0100659EXPORT_SYMBOL(vfs_quota_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660
661/* Free unused dquots from cache */
662static void prune_dqcache(int count)
663{
664 struct list_head *head;
665 struct dquot *dquot;
666
667 head = free_dquots.prev;
668 while (head != &free_dquots && count) {
669 dquot = list_entry(head, struct dquot, dq_free);
670 remove_dquot_hash(dquot);
671 remove_free_dquot(dquot);
672 remove_inuse(dquot);
Jan Kara74f783a2008-08-19 14:51:22 +0200673 do_destroy_dquot(dquot);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 count--;
675 head = free_dquots.prev;
676 }
677}
678
Dmitry Monakhovdde95882010-04-26 20:03:33 +0400679static int dqstats_read(unsigned int type)
680{
681 int count = 0;
682#ifdef CONFIG_SMP
683 int cpu;
684 for_each_possible_cpu(cpu)
685 count += per_cpu_ptr(dqstats_pcpu, cpu)->stat[type];
686 /* Statistics reading is racy, but absolute accuracy isn't required */
687 if (count < 0)
688 count = 0;
689#else
690 count = dqstats.stat[type];
691#endif
692 return count;
693}
694
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695/*
696 * This is called from kswapd when we think we need some
697 * more memory
698 */
699
Al Viro27496a82005-10-21 03:20:48 -0400700static int shrink_dqcache_memory(int nr, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701{
702 if (nr) {
703 spin_lock(&dq_list_lock);
704 prune_dqcache(nr);
705 spin_unlock(&dq_list_lock);
706 }
Dmitry Monakhovdde95882010-04-26 20:03:33 +0400707 return (dqstats_read(DQST_FREE_DQUOTS)/100) * sysctl_vfs_cache_pressure;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708}
709
Rusty Russell8e1f9362007-07-17 04:03:17 -0700710static struct shrinker dqcache_shrinker = {
711 .shrink = shrink_dqcache_memory,
712 .seeks = DEFAULT_SEEKS,
713};
714
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715/*
716 * Put reference to dquot
717 * NOTE: If you change this function please check whether dqput_blocks() works right...
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 */
Jan Kara3d9ea252008-10-10 16:12:23 +0200719void dqput(struct dquot *dquot)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720{
Jan Karab48d3802008-07-25 01:46:49 -0700721 int ret;
722
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 if (!dquot)
724 return;
Jan Kara62af9b52010-04-19 16:47:20 +0200725#ifdef CONFIG_QUOTA_DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 if (!atomic_read(&dquot->dq_count)) {
727 printk("VFS: dqput: trying to free free dquot\n");
728 printk("VFS: device %s, dquot of %s %d\n",
729 dquot->dq_sb->s_id,
730 quotatypes[dquot->dq_type],
731 dquot->dq_id);
732 BUG();
733 }
734#endif
Dmitry Monakhovdde95882010-04-26 20:03:33 +0400735 dqstats_inc(DQST_DROPS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736we_slept:
737 spin_lock(&dq_list_lock);
738 if (atomic_read(&dquot->dq_count) > 1) {
739 /* We have more than one user... nothing to do */
740 atomic_dec(&dquot->dq_count);
Jan Kara6362e4d2006-03-23 03:00:17 -0800741 /* Releasing dquot during quotaoff phase? */
Jan Karaf55abc02008-08-20 17:50:32 +0200742 if (!sb_has_quota_active(dquot->dq_sb, dquot->dq_type) &&
Jan Kara6362e4d2006-03-23 03:00:17 -0800743 atomic_read(&dquot->dq_count) == 1)
744 wake_up(&dquot->dq_wait_unused);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 spin_unlock(&dq_list_lock);
746 return;
747 }
748 /* Need to release dquot? */
749 if (test_bit(DQ_ACTIVE_B, &dquot->dq_flags) && dquot_dirty(dquot)) {
750 spin_unlock(&dq_list_lock);
751 /* Commit dquot before releasing */
Jan Karab48d3802008-07-25 01:46:49 -0700752 ret = dquot->dq_sb->dq_op->write_dquot(dquot);
753 if (ret < 0) {
754 printk(KERN_ERR "VFS: cannot write quota structure on "
755 "device %s (error %d). Quota may get out of "
756 "sync!\n", dquot->dq_sb->s_id, ret);
757 /*
758 * We clear dirty bit anyway, so that we avoid
759 * infinite loop here
760 */
761 spin_lock(&dq_list_lock);
762 clear_dquot_dirty(dquot);
763 spin_unlock(&dq_list_lock);
764 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 goto we_slept;
766 }
767 /* Clear flag in case dquot was inactive (something bad happened) */
768 clear_dquot_dirty(dquot);
769 if (test_bit(DQ_ACTIVE_B, &dquot->dq_flags)) {
770 spin_unlock(&dq_list_lock);
771 dquot->dq_sb->dq_op->release_dquot(dquot);
772 goto we_slept;
773 }
774 atomic_dec(&dquot->dq_count);
Jan Kara62af9b52010-04-19 16:47:20 +0200775#ifdef CONFIG_QUOTA_DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 /* sanity check */
Eric Sesterhenn8abf6a42006-04-02 13:36:13 +0200777 BUG_ON(!list_empty(&dquot->dq_free));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778#endif
779 put_dquot_last(dquot);
780 spin_unlock(&dq_list_lock);
781}
Mingming Cao08d03502009-01-14 16:19:32 +0100782EXPORT_SYMBOL(dqput);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783
Jan Kara7d9056b2008-11-25 15:31:32 +0100784struct dquot *dquot_alloc(struct super_block *sb, int type)
Jan Kara74f783a2008-08-19 14:51:22 +0200785{
786 return kmem_cache_zalloc(dquot_cachep, GFP_NOFS);
787}
Jan Kara7d9056b2008-11-25 15:31:32 +0100788EXPORT_SYMBOL(dquot_alloc);
Jan Kara74f783a2008-08-19 14:51:22 +0200789
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790static struct dquot *get_empty_dquot(struct super_block *sb, int type)
791{
792 struct dquot *dquot;
793
Jan Kara74f783a2008-08-19 14:51:22 +0200794 dquot = sb->dq_op->alloc_dquot(sb, type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 if(!dquot)
Jan Karadd6f3c62009-01-26 16:01:43 +0100796 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797
Ingo Molnard3be9152006-03-23 03:00:29 -0800798 mutex_init(&dquot->dq_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 INIT_LIST_HEAD(&dquot->dq_free);
800 INIT_LIST_HEAD(&dquot->dq_inuse);
801 INIT_HLIST_NODE(&dquot->dq_hash);
802 INIT_LIST_HEAD(&dquot->dq_dirty);
Jan Kara6362e4d2006-03-23 03:00:17 -0800803 init_waitqueue_head(&dquot->dq_wait_unused);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 dquot->dq_sb = sb;
805 dquot->dq_type = type;
806 atomic_set(&dquot->dq_count, 1);
807
808 return dquot;
809}
810
811/*
812 * Get reference to dquot
Jan Karacc334122009-01-12 17:23:05 +0100813 *
814 * Locking is slightly tricky here. We are guarded from parallel quotaoff()
815 * destroying our dquot by:
816 * a) checking for quota flags under dq_list_lock and
817 * b) getting a reference to dquot before we release dq_list_lock
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 */
Jan Kara3d9ea252008-10-10 16:12:23 +0200819struct dquot *dqget(struct super_block *sb, unsigned int id, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820{
821 unsigned int hashent = hashfn(sb, id, type);
Jan Karadd6f3c62009-01-26 16:01:43 +0100822 struct dquot *dquot = NULL, *empty = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823
Jan Karaf55abc02008-08-20 17:50:32 +0200824 if (!sb_has_quota_active(sb, type))
Jan Karadd6f3c62009-01-26 16:01:43 +0100825 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826we_slept:
827 spin_lock(&dq_list_lock);
Jan Karacc334122009-01-12 17:23:05 +0100828 spin_lock(&dq_state_lock);
829 if (!sb_has_quota_active(sb, type)) {
830 spin_unlock(&dq_state_lock);
831 spin_unlock(&dq_list_lock);
832 goto out;
833 }
834 spin_unlock(&dq_state_lock);
835
Jan Karadd6f3c62009-01-26 16:01:43 +0100836 dquot = find_dquot(hashent, sb, id, type);
837 if (!dquot) {
838 if (!empty) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 spin_unlock(&dq_list_lock);
Jan Karadd6f3c62009-01-26 16:01:43 +0100840 empty = get_empty_dquot(sb, type);
841 if (!empty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 schedule(); /* Try to wait for a moment... */
843 goto we_slept;
844 }
845 dquot = empty;
Jan Karadd6f3c62009-01-26 16:01:43 +0100846 empty = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 dquot->dq_id = id;
848 /* all dquots go on the inuse_list */
849 put_inuse(dquot);
850 /* hash it first so it can be found */
851 insert_dquot_hash(dquot);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 spin_unlock(&dq_list_lock);
Dmitry Monakhovdde95882010-04-26 20:03:33 +0400853 dqstats_inc(DQST_LOOKUPS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 } else {
855 if (!atomic_read(&dquot->dq_count))
856 remove_free_dquot(dquot);
857 atomic_inc(&dquot->dq_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 spin_unlock(&dq_list_lock);
Dmitry Monakhovdde95882010-04-26 20:03:33 +0400859 dqstats_inc(DQST_CACHE_HITS);
860 dqstats_inc(DQST_LOOKUPS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 }
Jan Kara268157b2009-01-27 15:47:22 +0100862 /* Wait for dq_lock - after this we know that either dquot_release() is
863 * already finished or it will be canceled due to dq_count > 1 test */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 wait_on_dquot(dquot);
Jan Kara268157b2009-01-27 15:47:22 +0100865 /* Read the dquot / allocate space in quota file */
866 if (!test_bit(DQ_ACTIVE_B, &dquot->dq_flags) &&
867 sb->dq_op->acquire_dquot(dquot) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 dqput(dquot);
Jan Karadd6f3c62009-01-26 16:01:43 +0100869 dquot = NULL;
Jan Karacc334122009-01-12 17:23:05 +0100870 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 }
Jan Kara62af9b52010-04-19 16:47:20 +0200872#ifdef CONFIG_QUOTA_DEBUG
Eric Sesterhenn8abf6a42006-04-02 13:36:13 +0200873 BUG_ON(!dquot->dq_sb); /* Has somebody invalidated entry under us? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874#endif
Jan Karacc334122009-01-12 17:23:05 +0100875out:
876 if (empty)
877 do_destroy_dquot(empty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878
879 return dquot;
880}
Mingming Cao08d03502009-01-14 16:19:32 +0100881EXPORT_SYMBOL(dqget);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882
883static int dqinit_needed(struct inode *inode, int type)
884{
885 int cnt;
886
887 if (IS_NOQUOTA(inode))
888 return 0;
889 if (type != -1)
Jan Karadd6f3c62009-01-26 16:01:43 +0100890 return !inode->i_dquot[type];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
Jan Karadd6f3c62009-01-26 16:01:43 +0100892 if (!inode->i_dquot[cnt])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 return 1;
894 return 0;
895}
896
Ingo Molnard3be9152006-03-23 03:00:29 -0800897/* This routine is guarded by dqonoff_mutex mutex */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898static void add_dquot_ref(struct super_block *sb, int type)
899{
Jan Kara941d2382008-02-06 01:37:36 -0800900 struct inode *inode, *old_inode = NULL;
Jan Kara62af9b52010-04-19 16:47:20 +0200901#ifdef CONFIG_QUOTA_DEBUG
Jan Kara0a5a9c72010-02-09 18:20:39 +0100902 int reserved = 0;
Jan Kara4c5e6c02010-04-06 18:52:47 +0200903#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904
Christoph Hellwigd003fb72007-02-12 00:51:58 -0800905 spin_lock(&inode_lock);
906 list_for_each_entry(inode, &sb->s_inodes, i_sb_list) {
Wu Fengguangb6fac632009-04-02 16:56:34 -0700907 if (inode->i_state & (I_FREEING|I_CLEAR|I_WILL_FREE|I_NEW))
Nick Pigginaabb8fd2009-03-11 13:17:36 -0700908 continue;
Jan Kara62af9b52010-04-19 16:47:20 +0200909#ifdef CONFIG_QUOTA_DEBUG
Jan Kara0a5a9c72010-02-09 18:20:39 +0100910 if (unlikely(inode_get_rsv_space(inode) > 0))
911 reserved = 1;
Jan Kara4c5e6c02010-04-06 18:52:47 +0200912#endif
Christoph Hellwigd003fb72007-02-12 00:51:58 -0800913 if (!atomic_read(&inode->i_writecount))
914 continue;
915 if (!dqinit_needed(inode, type))
916 continue;
Christoph Hellwigd003fb72007-02-12 00:51:58 -0800917
918 __iget(inode);
919 spin_unlock(&inode_lock);
920
Jan Kara941d2382008-02-06 01:37:36 -0800921 iput(old_inode);
Christoph Hellwig871a2932010-03-03 09:05:07 -0500922 __dquot_initialize(inode, type);
Jan Kara941d2382008-02-06 01:37:36 -0800923 /* We hold a reference to 'inode' so it couldn't have been
924 * removed from s_inodes list while we dropped the inode_lock.
925 * We cannot iput the inode now as we can be holding the last
926 * reference and we cannot iput it under inode_lock. So we
927 * keep the reference and iput it later. */
928 old_inode = inode;
929 spin_lock(&inode_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 }
Christoph Hellwigd003fb72007-02-12 00:51:58 -0800931 spin_unlock(&inode_lock);
Jan Kara941d2382008-02-06 01:37:36 -0800932 iput(old_inode);
Jan Kara0a5a9c72010-02-09 18:20:39 +0100933
Jan Kara62af9b52010-04-19 16:47:20 +0200934#ifdef CONFIG_QUOTA_DEBUG
Jan Kara0a5a9c72010-02-09 18:20:39 +0100935 if (reserved) {
936 printk(KERN_WARNING "VFS (%s): Writes happened before quota"
937 " was turned on thus quota information is probably "
938 "inconsistent. Please run quotacheck(8).\n", sb->s_id);
939 }
Jan Kara4c5e6c02010-04-06 18:52:47 +0200940#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941}
942
Jan Kara268157b2009-01-27 15:47:22 +0100943/*
944 * Return 0 if dqput() won't block.
945 * (note that 1 doesn't necessarily mean blocking)
946 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947static inline int dqput_blocks(struct dquot *dquot)
948{
949 if (atomic_read(&dquot->dq_count) <= 1)
950 return 1;
951 return 0;
952}
953
Jan Kara268157b2009-01-27 15:47:22 +0100954/*
955 * Remove references to dquots from inode and add dquot to list for freeing
956 * if we have the last referece to dquot
957 * We can't race with anybody because we hold dqptr_sem for writing...
958 */
Adrian Bunke5f00f42007-05-08 00:27:22 -0700959static int remove_inode_dquot_ref(struct inode *inode, int type,
960 struct list_head *tofree_head)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961{
962 struct dquot *dquot = inode->i_dquot[type];
963
Jan Karadd6f3c62009-01-26 16:01:43 +0100964 inode->i_dquot[type] = NULL;
965 if (dquot) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 if (dqput_blocks(dquot)) {
Jan Kara62af9b52010-04-19 16:47:20 +0200967#ifdef CONFIG_QUOTA_DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 if (atomic_read(&dquot->dq_count) != 1)
969 printk(KERN_WARNING "VFS: Adding dquot with dq_count %d to dispose list.\n", atomic_read(&dquot->dq_count));
970#endif
971 spin_lock(&dq_list_lock);
Jan Kara268157b2009-01-27 15:47:22 +0100972 /* As dquot must have currently users it can't be on
973 * the free list... */
974 list_add(&dquot->dq_free, tofree_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 spin_unlock(&dq_list_lock);
976 return 1;
977 }
978 else
979 dqput(dquot); /* We have guaranteed we won't block */
980 }
981 return 0;
982}
983
Jan Kara268157b2009-01-27 15:47:22 +0100984/*
985 * Free list of dquots
986 * Dquots are removed from inodes and no new references can be got so we are
987 * the only ones holding reference
988 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989static void put_dquot_list(struct list_head *tofree_head)
990{
991 struct list_head *act_head;
992 struct dquot *dquot;
993
994 act_head = tofree_head->next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 while (act_head != tofree_head) {
996 dquot = list_entry(act_head, struct dquot, dq_free);
997 act_head = act_head->next;
Jan Kara268157b2009-01-27 15:47:22 +0100998 /* Remove dquot from the list so we won't have problems... */
999 list_del_init(&dquot->dq_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 dqput(dquot);
1001 }
1002}
1003
Christoph Hellwigfb58b732007-02-12 00:51:57 -08001004static void remove_dquot_ref(struct super_block *sb, int type,
1005 struct list_head *tofree_head)
1006{
1007 struct inode *inode;
1008
1009 spin_lock(&inode_lock);
1010 list_for_each_entry(inode, &sb->s_inodes, i_sb_list) {
Nick Pigginaabb8fd2009-03-11 13:17:36 -07001011 /*
1012 * We have to scan also I_NEW inodes because they can already
1013 * have quota pointer initialized. Luckily, we need to touch
1014 * only quota pointers and these have separate locking
1015 * (dqptr_sem).
1016 */
Christoph Hellwigfb58b732007-02-12 00:51:57 -08001017 if (!IS_NOQUOTA(inode))
1018 remove_inode_dquot_ref(inode, type, tofree_head);
1019 }
1020 spin_unlock(&inode_lock);
1021}
1022
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023/* Gather all references from inodes and drop them */
1024static void drop_dquot_ref(struct super_block *sb, int type)
1025{
1026 LIST_HEAD(tofree_head);
1027
Christoph Hellwigfb58b732007-02-12 00:51:57 -08001028 if (sb->dq_op) {
1029 down_write(&sb_dqopt(sb)->dqptr_sem);
1030 remove_dquot_ref(sb, type, &tofree_head);
1031 up_write(&sb_dqopt(sb)->dqptr_sem);
1032 put_dquot_list(&tofree_head);
1033 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034}
1035
Jan Kara12095462008-08-20 14:45:12 +02001036static inline void dquot_incr_inodes(struct dquot *dquot, qsize_t number)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037{
1038 dquot->dq_dqb.dqb_curinodes += number;
1039}
1040
1041static inline void dquot_incr_space(struct dquot *dquot, qsize_t number)
1042{
1043 dquot->dq_dqb.dqb_curspace += number;
1044}
1045
Mingming Caof18df222009-01-13 16:43:09 +01001046static inline void dquot_resv_space(struct dquot *dquot, qsize_t number)
1047{
1048 dquot->dq_dqb.dqb_rsvspace += number;
1049}
1050
Mingming Cao740d9dc2009-01-13 16:43:14 +01001051/*
1052 * Claim reserved quota space
1053 */
Jan Kara0a5a9c72010-02-09 18:20:39 +01001054static void dquot_claim_reserved_space(struct dquot *dquot, qsize_t number)
Mingming Cao740d9dc2009-01-13 16:43:14 +01001055{
Jan Kara0a5a9c72010-02-09 18:20:39 +01001056 if (dquot->dq_dqb.dqb_rsvspace < number) {
1057 WARN_ON_ONCE(1);
1058 number = dquot->dq_dqb.dqb_rsvspace;
1059 }
Mingming Cao740d9dc2009-01-13 16:43:14 +01001060 dquot->dq_dqb.dqb_curspace += number;
1061 dquot->dq_dqb.dqb_rsvspace -= number;
1062}
1063
1064static inline
1065void dquot_free_reserved_space(struct dquot *dquot, qsize_t number)
1066{
Jan Kara0a5a9c72010-02-09 18:20:39 +01001067 if (dquot->dq_dqb.dqb_rsvspace >= number)
1068 dquot->dq_dqb.dqb_rsvspace -= number;
1069 else {
1070 WARN_ON_ONCE(1);
1071 dquot->dq_dqb.dqb_rsvspace = 0;
1072 }
Mingming Cao740d9dc2009-01-13 16:43:14 +01001073}
1074
Jan Kara7a2435d2009-01-27 01:47:11 +01001075static void dquot_decr_inodes(struct dquot *dquot, qsize_t number)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076{
Jan Karadb49d2d2008-10-01 18:21:39 +02001077 if (sb_dqopt(dquot->dq_sb)->flags & DQUOT_NEGATIVE_USAGE ||
1078 dquot->dq_dqb.dqb_curinodes >= number)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 dquot->dq_dqb.dqb_curinodes -= number;
1080 else
1081 dquot->dq_dqb.dqb_curinodes = 0;
1082 if (dquot->dq_dqb.dqb_curinodes <= dquot->dq_dqb.dqb_isoftlimit)
1083 dquot->dq_dqb.dqb_itime = (time_t) 0;
1084 clear_bit(DQ_INODES_B, &dquot->dq_flags);
1085}
1086
Jan Kara7a2435d2009-01-27 01:47:11 +01001087static void dquot_decr_space(struct dquot *dquot, qsize_t number)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088{
Jan Karadb49d2d2008-10-01 18:21:39 +02001089 if (sb_dqopt(dquot->dq_sb)->flags & DQUOT_NEGATIVE_USAGE ||
1090 dquot->dq_dqb.dqb_curspace >= number)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 dquot->dq_dqb.dqb_curspace -= number;
1092 else
1093 dquot->dq_dqb.dqb_curspace = 0;
Jan Kara12095462008-08-20 14:45:12 +02001094 if (dquot->dq_dqb.dqb_curspace <= dquot->dq_dqb.dqb_bsoftlimit)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 dquot->dq_dqb.dqb_btime = (time_t) 0;
1096 clear_bit(DQ_BLKS_B, &dquot->dq_flags);
1097}
1098
Jan Karac5254602007-12-22 14:03:25 -08001099static int warning_issued(struct dquot *dquot, const int warntype)
1100{
1101 int flag = (warntype == QUOTA_NL_BHARDWARN ||
1102 warntype == QUOTA_NL_BSOFTLONGWARN) ? DQ_BLKS_B :
1103 ((warntype == QUOTA_NL_IHARDWARN ||
1104 warntype == QUOTA_NL_ISOFTLONGWARN) ? DQ_INODES_B : 0);
1105
1106 if (!flag)
1107 return 0;
1108 return test_and_set_bit(flag, &dquot->dq_flags);
1109}
1110
Jan Kara8e893462007-10-16 23:29:31 -07001111#ifdef CONFIG_PRINT_QUOTA_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112static int flag_print_warnings = 1;
1113
Jan Kara7a2435d2009-01-27 01:47:11 +01001114static int need_print_warning(struct dquot *dquot)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115{
1116 if (!flag_print_warnings)
1117 return 0;
1118
1119 switch (dquot->dq_type) {
1120 case USRQUOTA:
David Howellsda9592e2008-11-14 10:39:05 +11001121 return current_fsuid() == dquot->dq_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 case GRPQUOTA:
1123 return in_group_p(dquot->dq_id);
1124 }
1125 return 0;
1126}
1127
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128/* Print warning to user which exceeded quota */
Jan Karac5254602007-12-22 14:03:25 -08001129static void print_warning(struct dquot *dquot, const int warntype)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130{
1131 char *msg = NULL;
Peter Zijlstra24ec8392006-12-08 02:36:04 -08001132 struct tty_struct *tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133
Jan Kara657d3bf2008-07-25 01:46:52 -07001134 if (warntype == QUOTA_NL_IHARDBELOW ||
1135 warntype == QUOTA_NL_ISOFTBELOW ||
1136 warntype == QUOTA_NL_BHARDBELOW ||
1137 warntype == QUOTA_NL_BSOFTBELOW || !need_print_warning(dquot))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 return;
1139
Peter Zijlstra24ec8392006-12-08 02:36:04 -08001140 tty = get_current_tty();
1141 if (!tty)
Alan Cox452a00d2008-10-13 10:39:13 +01001142 return;
Peter Zijlstra24ec8392006-12-08 02:36:04 -08001143 tty_write_message(tty, dquot->dq_sb->s_id);
Jan Kara8e893462007-10-16 23:29:31 -07001144 if (warntype == QUOTA_NL_ISOFTWARN || warntype == QUOTA_NL_BSOFTWARN)
Peter Zijlstra24ec8392006-12-08 02:36:04 -08001145 tty_write_message(tty, ": warning, ");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 else
Peter Zijlstra24ec8392006-12-08 02:36:04 -08001147 tty_write_message(tty, ": write failed, ");
1148 tty_write_message(tty, quotatypes[dquot->dq_type]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 switch (warntype) {
Jan Kara8e893462007-10-16 23:29:31 -07001150 case QUOTA_NL_IHARDWARN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 msg = " file limit reached.\r\n";
1152 break;
Jan Kara8e893462007-10-16 23:29:31 -07001153 case QUOTA_NL_ISOFTLONGWARN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 msg = " file quota exceeded too long.\r\n";
1155 break;
Jan Kara8e893462007-10-16 23:29:31 -07001156 case QUOTA_NL_ISOFTWARN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 msg = " file quota exceeded.\r\n";
1158 break;
Jan Kara8e893462007-10-16 23:29:31 -07001159 case QUOTA_NL_BHARDWARN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 msg = " block limit reached.\r\n";
1161 break;
Jan Kara8e893462007-10-16 23:29:31 -07001162 case QUOTA_NL_BSOFTLONGWARN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 msg = " block quota exceeded too long.\r\n";
1164 break;
Jan Kara8e893462007-10-16 23:29:31 -07001165 case QUOTA_NL_BSOFTWARN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 msg = " block quota exceeded.\r\n";
1167 break;
1168 }
Peter Zijlstra24ec8392006-12-08 02:36:04 -08001169 tty_write_message(tty, msg);
Alan Cox452a00d2008-10-13 10:39:13 +01001170 tty_kref_put(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171}
Jan Kara8e893462007-10-16 23:29:31 -07001172#endif
1173
Mingming Caof18df222009-01-13 16:43:09 +01001174/*
1175 * Write warnings to the console and send warning messages over netlink.
1176 *
1177 * Note that this function can sleep.
1178 */
Jan Kara7a2435d2009-01-27 01:47:11 +01001179static void flush_warnings(struct dquot *const *dquots, char *warntype)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180{
Steven Whitehouse86e931a2009-09-28 12:35:17 +01001181 struct dquot *dq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 int i;
1183
Steven Whitehouse86e931a2009-09-28 12:35:17 +01001184 for (i = 0; i < MAXQUOTAS; i++) {
1185 dq = dquots[i];
1186 if (dq && warntype[i] != QUOTA_NL_NOWARN &&
1187 !warning_issued(dq, warntype[i])) {
Jan Kara8e893462007-10-16 23:29:31 -07001188#ifdef CONFIG_PRINT_QUOTA_WARNING
Steven Whitehouse86e931a2009-09-28 12:35:17 +01001189 print_warning(dq, warntype[i]);
Jan Kara8e893462007-10-16 23:29:31 -07001190#endif
Steven Whitehouse86e931a2009-09-28 12:35:17 +01001191 quota_send_warning(dq->dq_type, dq->dq_id,
1192 dq->dq_sb->s_dev, warntype[i]);
Jan Kara8e893462007-10-16 23:29:31 -07001193 }
Steven Whitehouse86e931a2009-09-28 12:35:17 +01001194 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195}
1196
Jan Kara7a2435d2009-01-27 01:47:11 +01001197static int ignore_hardlimit(struct dquot *dquot)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198{
1199 struct mem_dqinfo *info = &sb_dqopt(dquot->dq_sb)->info[dquot->dq_type];
1200
1201 return capable(CAP_SYS_RESOURCE) &&
Jan Kara268157b2009-01-27 15:47:22 +01001202 (info->dqi_format->qf_fmt_id != QFMT_VFS_OLD ||
1203 !(info->dqi_flags & V1_DQF_RSQUASH));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204}
1205
1206/* needs dq_data_lock */
Jan Kara12095462008-08-20 14:45:12 +02001207static int check_idq(struct dquot *dquot, qsize_t inodes, char *warntype)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208{
Jan Kara268157b2009-01-27 15:47:22 +01001209 qsize_t newinodes = dquot->dq_dqb.dqb_curinodes + inodes;
1210
Jan Kara8e893462007-10-16 23:29:31 -07001211 *warntype = QUOTA_NL_NOWARN;
Jan Karaf55abc02008-08-20 17:50:32 +02001212 if (!sb_has_quota_limits_enabled(dquot->dq_sb, dquot->dq_type) ||
1213 test_bit(DQ_FAKE_B, &dquot->dq_flags))
Christoph Hellwigefd8f0e2010-03-03 09:05:08 -05001214 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215
1216 if (dquot->dq_dqb.dqb_ihardlimit &&
Jan Kara268157b2009-01-27 15:47:22 +01001217 newinodes > dquot->dq_dqb.dqb_ihardlimit &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 !ignore_hardlimit(dquot)) {
Jan Kara8e893462007-10-16 23:29:31 -07001219 *warntype = QUOTA_NL_IHARDWARN;
Christoph Hellwigefd8f0e2010-03-03 09:05:08 -05001220 return -EDQUOT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 }
1222
1223 if (dquot->dq_dqb.dqb_isoftlimit &&
Jan Kara268157b2009-01-27 15:47:22 +01001224 newinodes > dquot->dq_dqb.dqb_isoftlimit &&
1225 dquot->dq_dqb.dqb_itime &&
1226 get_seconds() >= dquot->dq_dqb.dqb_itime &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 !ignore_hardlimit(dquot)) {
Jan Kara8e893462007-10-16 23:29:31 -07001228 *warntype = QUOTA_NL_ISOFTLONGWARN;
Christoph Hellwigefd8f0e2010-03-03 09:05:08 -05001229 return -EDQUOT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 }
1231
1232 if (dquot->dq_dqb.dqb_isoftlimit &&
Jan Kara268157b2009-01-27 15:47:22 +01001233 newinodes > dquot->dq_dqb.dqb_isoftlimit &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 dquot->dq_dqb.dqb_itime == 0) {
Jan Kara8e893462007-10-16 23:29:31 -07001235 *warntype = QUOTA_NL_ISOFTWARN;
Jan Kara268157b2009-01-27 15:47:22 +01001236 dquot->dq_dqb.dqb_itime = get_seconds() +
1237 sb_dqopt(dquot->dq_sb)->info[dquot->dq_type].dqi_igrace;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 }
1239
Christoph Hellwigefd8f0e2010-03-03 09:05:08 -05001240 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241}
1242
1243/* needs dq_data_lock */
1244static int check_bdq(struct dquot *dquot, qsize_t space, int prealloc, char *warntype)
1245{
Mingming Caof18df222009-01-13 16:43:09 +01001246 qsize_t tspace;
Jan Kara268157b2009-01-27 15:47:22 +01001247 struct super_block *sb = dquot->dq_sb;
Mingming Caof18df222009-01-13 16:43:09 +01001248
Jan Kara8e893462007-10-16 23:29:31 -07001249 *warntype = QUOTA_NL_NOWARN;
Jan Kara268157b2009-01-27 15:47:22 +01001250 if (!sb_has_quota_limits_enabled(sb, dquot->dq_type) ||
Jan Karaf55abc02008-08-20 17:50:32 +02001251 test_bit(DQ_FAKE_B, &dquot->dq_flags))
Christoph Hellwigefd8f0e2010-03-03 09:05:08 -05001252 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253
Mingming Caof18df222009-01-13 16:43:09 +01001254 tspace = dquot->dq_dqb.dqb_curspace + dquot->dq_dqb.dqb_rsvspace
1255 + space;
1256
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257 if (dquot->dq_dqb.dqb_bhardlimit &&
Mingming Caof18df222009-01-13 16:43:09 +01001258 tspace > dquot->dq_dqb.dqb_bhardlimit &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 !ignore_hardlimit(dquot)) {
1260 if (!prealloc)
Jan Kara8e893462007-10-16 23:29:31 -07001261 *warntype = QUOTA_NL_BHARDWARN;
Christoph Hellwigefd8f0e2010-03-03 09:05:08 -05001262 return -EDQUOT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 }
1264
1265 if (dquot->dq_dqb.dqb_bsoftlimit &&
Mingming Caof18df222009-01-13 16:43:09 +01001266 tspace > dquot->dq_dqb.dqb_bsoftlimit &&
Jan Kara268157b2009-01-27 15:47:22 +01001267 dquot->dq_dqb.dqb_btime &&
1268 get_seconds() >= dquot->dq_dqb.dqb_btime &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 !ignore_hardlimit(dquot)) {
1270 if (!prealloc)
Jan Kara8e893462007-10-16 23:29:31 -07001271 *warntype = QUOTA_NL_BSOFTLONGWARN;
Christoph Hellwigefd8f0e2010-03-03 09:05:08 -05001272 return -EDQUOT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 }
1274
1275 if (dquot->dq_dqb.dqb_bsoftlimit &&
Mingming Caof18df222009-01-13 16:43:09 +01001276 tspace > dquot->dq_dqb.dqb_bsoftlimit &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 dquot->dq_dqb.dqb_btime == 0) {
1278 if (!prealloc) {
Jan Kara8e893462007-10-16 23:29:31 -07001279 *warntype = QUOTA_NL_BSOFTWARN;
Jan Kara268157b2009-01-27 15:47:22 +01001280 dquot->dq_dqb.dqb_btime = get_seconds() +
1281 sb_dqopt(sb)->info[dquot->dq_type].dqi_bgrace;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 }
1283 else
1284 /*
1285 * We don't allow preallocation to exceed softlimit so exceeding will
1286 * be always printed
1287 */
Christoph Hellwigefd8f0e2010-03-03 09:05:08 -05001288 return -EDQUOT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 }
1290
Christoph Hellwigefd8f0e2010-03-03 09:05:08 -05001291 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292}
1293
Jan Kara12095462008-08-20 14:45:12 +02001294static int info_idq_free(struct dquot *dquot, qsize_t inodes)
Jan Kara657d3bf2008-07-25 01:46:52 -07001295{
Jan Kara268157b2009-01-27 15:47:22 +01001296 qsize_t newinodes;
1297
Jan Kara657d3bf2008-07-25 01:46:52 -07001298 if (test_bit(DQ_FAKE_B, &dquot->dq_flags) ||
Jan Karaf55abc02008-08-20 17:50:32 +02001299 dquot->dq_dqb.dqb_curinodes <= dquot->dq_dqb.dqb_isoftlimit ||
1300 !sb_has_quota_limits_enabled(dquot->dq_sb, dquot->dq_type))
Jan Kara657d3bf2008-07-25 01:46:52 -07001301 return QUOTA_NL_NOWARN;
1302
Jan Kara268157b2009-01-27 15:47:22 +01001303 newinodes = dquot->dq_dqb.dqb_curinodes - inodes;
1304 if (newinodes <= dquot->dq_dqb.dqb_isoftlimit)
Jan Kara657d3bf2008-07-25 01:46:52 -07001305 return QUOTA_NL_ISOFTBELOW;
1306 if (dquot->dq_dqb.dqb_curinodes >= dquot->dq_dqb.dqb_ihardlimit &&
Jan Kara268157b2009-01-27 15:47:22 +01001307 newinodes < dquot->dq_dqb.dqb_ihardlimit)
Jan Kara657d3bf2008-07-25 01:46:52 -07001308 return QUOTA_NL_IHARDBELOW;
1309 return QUOTA_NL_NOWARN;
1310}
1311
1312static int info_bdq_free(struct dquot *dquot, qsize_t space)
1313{
1314 if (test_bit(DQ_FAKE_B, &dquot->dq_flags) ||
Jan Kara12095462008-08-20 14:45:12 +02001315 dquot->dq_dqb.dqb_curspace <= dquot->dq_dqb.dqb_bsoftlimit)
Jan Kara657d3bf2008-07-25 01:46:52 -07001316 return QUOTA_NL_NOWARN;
1317
Jan Kara12095462008-08-20 14:45:12 +02001318 if (dquot->dq_dqb.dqb_curspace - space <= dquot->dq_dqb.dqb_bsoftlimit)
Jan Kara657d3bf2008-07-25 01:46:52 -07001319 return QUOTA_NL_BSOFTBELOW;
Jan Kara12095462008-08-20 14:45:12 +02001320 if (dquot->dq_dqb.dqb_curspace >= dquot->dq_dqb.dqb_bhardlimit &&
1321 dquot->dq_dqb.dqb_curspace - space < dquot->dq_dqb.dqb_bhardlimit)
Jan Kara657d3bf2008-07-25 01:46:52 -07001322 return QUOTA_NL_BHARDBELOW;
1323 return QUOTA_NL_NOWARN;
1324}
Jan Kara0a5a9c72010-02-09 18:20:39 +01001325
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326/*
Christoph Hellwig871a2932010-03-03 09:05:07 -05001327 * Initialize quota pointers in inode
1328 *
1329 * We do things in a bit complicated way but by that we avoid calling
1330 * dqget() and thus filesystem callbacks under dqptr_sem.
1331 *
1332 * It is better to call this function outside of any transaction as it
1333 * might need a lot of space in journal for dquot structure allocation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 */
Christoph Hellwig871a2932010-03-03 09:05:07 -05001335static void __dquot_initialize(struct inode *inode, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336{
1337 unsigned int id = 0;
Christoph Hellwig871a2932010-03-03 09:05:07 -05001338 int cnt;
Dmitry Monakhovad1e6e82010-02-16 08:31:49 +03001339 struct dquot *got[MAXQUOTAS];
Jan Karacc334122009-01-12 17:23:05 +01001340 struct super_block *sb = inode->i_sb;
Jan Kara0a5a9c72010-02-09 18:20:39 +01001341 qsize_t rsv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342
Ingo Molnard3be9152006-03-23 03:00:29 -08001343 /* First test before acquiring mutex - solves deadlocks when we
1344 * re-enter the quota code and are already holding the mutex */
Christoph Hellwig871a2932010-03-03 09:05:07 -05001345 if (!sb_any_quota_active(inode->i_sb) || IS_NOQUOTA(inode))
1346 return;
Jan Karacc334122009-01-12 17:23:05 +01001347
1348 /* First get references to structures we might need. */
1349 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
Dmitry Monakhovad1e6e82010-02-16 08:31:49 +03001350 got[cnt] = NULL;
Jan Karacc334122009-01-12 17:23:05 +01001351 if (type != -1 && cnt != type)
1352 continue;
1353 switch (cnt) {
1354 case USRQUOTA:
1355 id = inode->i_uid;
1356 break;
1357 case GRPQUOTA:
1358 id = inode->i_gid;
1359 break;
1360 }
1361 got[cnt] = dqget(sb, id, cnt);
1362 }
1363
1364 down_write(&sb_dqopt(sb)->dqptr_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 if (IS_NOQUOTA(inode))
1366 goto out_err;
1367 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1368 if (type != -1 && cnt != type)
1369 continue;
Jan Karacc334122009-01-12 17:23:05 +01001370 /* Avoid races with quotaoff() */
1371 if (!sb_has_quota_active(sb, cnt))
1372 continue;
Jan Karadd6f3c62009-01-26 16:01:43 +01001373 if (!inode->i_dquot[cnt]) {
Jan Karacc334122009-01-12 17:23:05 +01001374 inode->i_dquot[cnt] = got[cnt];
Jan Karadd6f3c62009-01-26 16:01:43 +01001375 got[cnt] = NULL;
Jan Kara0a5a9c72010-02-09 18:20:39 +01001376 /*
1377 * Make quota reservation system happy if someone
1378 * did a write before quota was turned on
1379 */
1380 rsv = inode_get_rsv_space(inode);
1381 if (unlikely(rsv))
1382 dquot_resv_space(inode->i_dquot[cnt], rsv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 }
1384 }
1385out_err:
Jan Karacc334122009-01-12 17:23:05 +01001386 up_write(&sb_dqopt(sb)->dqptr_sem);
1387 /* Drop unused references */
Dmitry Monakhovdc52dd32009-12-14 15:21:15 +03001388 dqput_all(got);
Christoph Hellwig871a2932010-03-03 09:05:07 -05001389}
1390
1391void dquot_initialize(struct inode *inode)
1392{
1393 __dquot_initialize(inode, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394}
Mingming Cao08d03502009-01-14 16:19:32 +01001395EXPORT_SYMBOL(dquot_initialize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396
1397/*
1398 * Release all quotas referenced by inode
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399 */
Christoph Hellwig9f754752010-03-03 09:05:05 -05001400static void __dquot_drop(struct inode *inode)
Jan Kara3d9ea252008-10-10 16:12:23 +02001401{
Jan Karacc334122009-01-12 17:23:05 +01001402 int cnt;
1403 struct dquot *put[MAXQUOTAS];
1404
Jan Kara3d9ea252008-10-10 16:12:23 +02001405 down_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
Jan Karacc334122009-01-12 17:23:05 +01001406 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1407 put[cnt] = inode->i_dquot[cnt];
Jan Karadd6f3c62009-01-26 16:01:43 +01001408 inode->i_dquot[cnt] = NULL;
Jan Karacc334122009-01-12 17:23:05 +01001409 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410 up_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
Dmitry Monakhovdc52dd32009-12-14 15:21:15 +03001411 dqput_all(put);
Christoph Hellwig9f754752010-03-03 09:05:05 -05001412}
1413
1414void dquot_drop(struct inode *inode)
1415{
1416 int cnt;
1417
1418 if (IS_NOQUOTA(inode))
1419 return;
1420
1421 /*
1422 * Test before calling to rule out calls from proc and such
1423 * where we are not allowed to block. Note that this is
1424 * actually reliable test even without the lock - the caller
1425 * must assure that nobody can come after the DQUOT_DROP and
1426 * add quota pointers back anyway.
1427 */
1428 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1429 if (inode->i_dquot[cnt])
1430 break;
1431 }
1432
1433 if (cnt < MAXQUOTAS)
1434 __dquot_drop(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435}
Mingming Cao08d03502009-01-14 16:19:32 +01001436EXPORT_SYMBOL(dquot_drop);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437
1438/*
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001439 * inode_reserved_space is managed internally by quota, and protected by
1440 * i_lock similar to i_blocks+i_bytes.
1441 */
1442static qsize_t *inode_reserved_space(struct inode * inode)
1443{
1444 /* Filesystem must explicitly define it's own method in order to use
1445 * quota reservation interface */
1446 BUG_ON(!inode->i_sb->dq_op->get_reserved_space);
1447 return inode->i_sb->dq_op->get_reserved_space(inode);
1448}
1449
Dmitry Monakhovc4690702010-02-09 17:53:36 +01001450void inode_add_rsv_space(struct inode *inode, qsize_t number)
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001451{
1452 spin_lock(&inode->i_lock);
1453 *inode_reserved_space(inode) += number;
1454 spin_unlock(&inode->i_lock);
1455}
Dmitry Monakhovc4690702010-02-09 17:53:36 +01001456EXPORT_SYMBOL(inode_add_rsv_space);
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001457
Dmitry Monakhovc4690702010-02-09 17:53:36 +01001458void inode_claim_rsv_space(struct inode *inode, qsize_t number)
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001459{
1460 spin_lock(&inode->i_lock);
1461 *inode_reserved_space(inode) -= number;
1462 __inode_add_bytes(inode, number);
1463 spin_unlock(&inode->i_lock);
1464}
Dmitry Monakhovc4690702010-02-09 17:53:36 +01001465EXPORT_SYMBOL(inode_claim_rsv_space);
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001466
Dmitry Monakhovc4690702010-02-09 17:53:36 +01001467void inode_sub_rsv_space(struct inode *inode, qsize_t number)
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001468{
1469 spin_lock(&inode->i_lock);
1470 *inode_reserved_space(inode) -= number;
1471 spin_unlock(&inode->i_lock);
1472}
Dmitry Monakhovc4690702010-02-09 17:53:36 +01001473EXPORT_SYMBOL(inode_sub_rsv_space);
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001474
1475static qsize_t inode_get_rsv_space(struct inode *inode)
1476{
1477 qsize_t ret;
Jan Kara05b5d892010-01-06 18:03:36 +01001478
1479 if (!inode->i_sb->dq_op->get_reserved_space)
1480 return 0;
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001481 spin_lock(&inode->i_lock);
1482 ret = *inode_reserved_space(inode);
1483 spin_unlock(&inode->i_lock);
1484 return ret;
1485}
1486
1487static void inode_incr_space(struct inode *inode, qsize_t number,
1488 int reserve)
1489{
1490 if (reserve)
1491 inode_add_rsv_space(inode, number);
1492 else
1493 inode_add_bytes(inode, number);
1494}
1495
1496static void inode_decr_space(struct inode *inode, qsize_t number, int reserve)
1497{
1498 if (reserve)
1499 inode_sub_rsv_space(inode, number);
1500 else
1501 inode_sub_bytes(inode, number);
1502}
1503
1504/*
Christoph Hellwig5dd40562010-03-03 09:05:00 -05001505 * This functions updates i_blocks+i_bytes fields and quota information
1506 * (together with appropriate checks).
1507 *
1508 * NOTE: We absolutely rely on the fact that caller dirties the inode
1509 * (usually helpers in quotaops.h care about this) and holds a handle for
1510 * the current transaction so that dquot write and inode write go into the
1511 * same transaction.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512 */
1513
1514/*
1515 * This operation can block, but only after everything is updated
1516 */
Mingming Caof18df222009-01-13 16:43:09 +01001517int __dquot_alloc_space(struct inode *inode, qsize_t number,
Christoph Hellwig5dd40562010-03-03 09:05:00 -05001518 int warn, int reserve)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519{
Christoph Hellwig5dd40562010-03-03 09:05:00 -05001520 int cnt, ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521 char warntype[MAXQUOTAS];
1522
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001523 /*
1524 * First test before acquiring mutex - solves deadlocks when we
1525 * re-enter the quota code and are already holding the mutex
1526 */
Christoph Hellwig5dd40562010-03-03 09:05:00 -05001527 if (!sb_any_quota_active(inode->i_sb) || IS_NOQUOTA(inode)) {
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001528 inode_incr_space(inode, number, reserve);
1529 goto out;
1530 }
1531
1532 down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
Jan Kara8e893462007-10-16 23:29:31 -07001534 warntype[cnt] = QUOTA_NL_NOWARN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536 spin_lock(&dq_data_lock);
1537 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
Jan Karadd6f3c62009-01-26 16:01:43 +01001538 if (!inode->i_dquot[cnt])
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539 continue;
Christoph Hellwigefd8f0e2010-03-03 09:05:08 -05001540 ret = check_bdq(inode->i_dquot[cnt], number, !warn,
1541 warntype+cnt);
1542 if (ret) {
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001543 spin_unlock(&dq_data_lock);
1544 goto out_flush_warn;
Mingming Caof18df222009-01-13 16:43:09 +01001545 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546 }
1547 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
Jan Karadd6f3c62009-01-26 16:01:43 +01001548 if (!inode->i_dquot[cnt])
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549 continue;
Mingming Caof18df222009-01-13 16:43:09 +01001550 if (reserve)
1551 dquot_resv_space(inode->i_dquot[cnt], number);
1552 else
1553 dquot_incr_space(inode->i_dquot[cnt], number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554 }
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001555 inode_incr_space(inode, number, reserve);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556 spin_unlock(&dq_data_lock);
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001557
1558 if (reserve)
1559 goto out_flush_warn;
Dmitry Monakhovdc52dd32009-12-14 15:21:15 +03001560 mark_all_dquot_dirty(inode->i_dquot);
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001561out_flush_warn:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562 flush_warnings(inode->i_dquot, warntype);
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001563 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1564out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565 return ret;
1566}
Christoph Hellwig5dd40562010-03-03 09:05:00 -05001567EXPORT_SYMBOL(__dquot_alloc_space);
Mingming Caof18df222009-01-13 16:43:09 +01001568
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569/*
1570 * This operation can block, but only after everything is updated
1571 */
Christoph Hellwig63936dd2010-03-03 09:05:01 -05001572int dquot_alloc_inode(const struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573{
Christoph Hellwigefd8f0e2010-03-03 09:05:08 -05001574 int cnt, ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575 char warntype[MAXQUOTAS];
1576
Ingo Molnard3be9152006-03-23 03:00:29 -08001577 /* First test before acquiring mutex - solves deadlocks when we
1578 * re-enter the quota code and are already holding the mutex */
Christoph Hellwig63936dd2010-03-03 09:05:01 -05001579 if (!sb_any_quota_active(inode->i_sb) || IS_NOQUOTA(inode))
1580 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
Jan Kara8e893462007-10-16 23:29:31 -07001582 warntype[cnt] = QUOTA_NL_NOWARN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583 down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584 spin_lock(&dq_data_lock);
1585 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
Jan Karadd6f3c62009-01-26 16:01:43 +01001586 if (!inode->i_dquot[cnt])
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587 continue;
Christoph Hellwigefd8f0e2010-03-03 09:05:08 -05001588 ret = check_idq(inode->i_dquot[cnt], 1, warntype + cnt);
1589 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590 goto warn_put_all;
1591 }
1592
1593 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
Jan Karadd6f3c62009-01-26 16:01:43 +01001594 if (!inode->i_dquot[cnt])
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595 continue;
Christoph Hellwig63936dd2010-03-03 09:05:01 -05001596 dquot_incr_inodes(inode->i_dquot[cnt], 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597 }
Christoph Hellwigefd8f0e2010-03-03 09:05:08 -05001598
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599warn_put_all:
1600 spin_unlock(&dq_data_lock);
Christoph Hellwig63936dd2010-03-03 09:05:01 -05001601 if (ret == 0)
Dmitry Monakhovdc52dd32009-12-14 15:21:15 +03001602 mark_all_dquot_dirty(inode->i_dquot);
Jan Kara087ee8d2007-12-17 16:20:26 -08001603 flush_warnings(inode->i_dquot, warntype);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1605 return ret;
1606}
Mingming Cao08d03502009-01-14 16:19:32 +01001607EXPORT_SYMBOL(dquot_alloc_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608
Christoph Hellwig5dd40562010-03-03 09:05:00 -05001609/*
1610 * Convert in-memory reserved quotas to real consumed quotas
1611 */
1612int dquot_claim_space_nodirty(struct inode *inode, qsize_t number)
Mingming Cao740d9dc2009-01-13 16:43:14 +01001613{
1614 int cnt;
Mingming Cao740d9dc2009-01-13 16:43:14 +01001615
Christoph Hellwig5dd40562010-03-03 09:05:00 -05001616 if (!sb_any_quota_active(inode->i_sb) || IS_NOQUOTA(inode)) {
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001617 inode_claim_rsv_space(inode, number);
Christoph Hellwig5dd40562010-03-03 09:05:00 -05001618 return 0;
Mingming Cao740d9dc2009-01-13 16:43:14 +01001619 }
1620
1621 down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
Mingming Cao740d9dc2009-01-13 16:43:14 +01001622 spin_lock(&dq_data_lock);
1623 /* Claim reserved quotas to allocated quotas */
1624 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
Jan Karadd6f3c62009-01-26 16:01:43 +01001625 if (inode->i_dquot[cnt])
Mingming Cao740d9dc2009-01-13 16:43:14 +01001626 dquot_claim_reserved_space(inode->i_dquot[cnt],
1627 number);
1628 }
1629 /* Update inode bytes */
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001630 inode_claim_rsv_space(inode, number);
Mingming Cao740d9dc2009-01-13 16:43:14 +01001631 spin_unlock(&dq_data_lock);
Dmitry Monakhovdc52dd32009-12-14 15:21:15 +03001632 mark_all_dquot_dirty(inode->i_dquot);
Mingming Cao740d9dc2009-01-13 16:43:14 +01001633 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
Christoph Hellwig5dd40562010-03-03 09:05:00 -05001634 return 0;
Mingming Cao740d9dc2009-01-13 16:43:14 +01001635}
Christoph Hellwig5dd40562010-03-03 09:05:00 -05001636EXPORT_SYMBOL(dquot_claim_space_nodirty);
Mingming Cao740d9dc2009-01-13 16:43:14 +01001637
1638/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639 * This operation can block, but only after everything is updated
1640 */
Christoph Hellwig5dd40562010-03-03 09:05:00 -05001641void __dquot_free_space(struct inode *inode, qsize_t number, int reserve)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642{
1643 unsigned int cnt;
Jan Kara657d3bf2008-07-25 01:46:52 -07001644 char warntype[MAXQUOTAS];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645
Ingo Molnard3be9152006-03-23 03:00:29 -08001646 /* First test before acquiring mutex - solves deadlocks when we
1647 * re-enter the quota code and are already holding the mutex */
Christoph Hellwig5dd40562010-03-03 09:05:00 -05001648 if (!sb_any_quota_active(inode->i_sb) || IS_NOQUOTA(inode)) {
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001649 inode_decr_space(inode, number, reserve);
Christoph Hellwig5dd40562010-03-03 09:05:00 -05001650 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651 }
Jan Kara657d3bf2008-07-25 01:46:52 -07001652
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653 down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654 spin_lock(&dq_data_lock);
1655 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
Jan Karadd6f3c62009-01-26 16:01:43 +01001656 if (!inode->i_dquot[cnt])
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657 continue;
Jan Kara657d3bf2008-07-25 01:46:52 -07001658 warntype[cnt] = info_bdq_free(inode->i_dquot[cnt], number);
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001659 if (reserve)
1660 dquot_free_reserved_space(inode->i_dquot[cnt], number);
1661 else
1662 dquot_decr_space(inode->i_dquot[cnt], number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663 }
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001664 inode_decr_space(inode, number, reserve);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665 spin_unlock(&dq_data_lock);
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001666
1667 if (reserve)
1668 goto out_unlock;
Dmitry Monakhovdc52dd32009-12-14 15:21:15 +03001669 mark_all_dquot_dirty(inode->i_dquot);
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001670out_unlock:
Jan Kara657d3bf2008-07-25 01:46:52 -07001671 flush_warnings(inode->i_dquot, warntype);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673}
Christoph Hellwig5dd40562010-03-03 09:05:00 -05001674EXPORT_SYMBOL(__dquot_free_space);
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001675
1676/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677 * This operation can block, but only after everything is updated
1678 */
Christoph Hellwig63936dd2010-03-03 09:05:01 -05001679void dquot_free_inode(const struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680{
1681 unsigned int cnt;
Jan Kara657d3bf2008-07-25 01:46:52 -07001682 char warntype[MAXQUOTAS];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683
Ingo Molnard3be9152006-03-23 03:00:29 -08001684 /* First test before acquiring mutex - solves deadlocks when we
1685 * re-enter the quota code and are already holding the mutex */
Christoph Hellwig63936dd2010-03-03 09:05:01 -05001686 if (!sb_any_quota_active(inode->i_sb) || IS_NOQUOTA(inode))
1687 return;
Jan Kara657d3bf2008-07-25 01:46:52 -07001688
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689 down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690 spin_lock(&dq_data_lock);
1691 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
Jan Karadd6f3c62009-01-26 16:01:43 +01001692 if (!inode->i_dquot[cnt])
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693 continue;
Christoph Hellwig63936dd2010-03-03 09:05:01 -05001694 warntype[cnt] = info_idq_free(inode->i_dquot[cnt], 1);
1695 dquot_decr_inodes(inode->i_dquot[cnt], 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696 }
1697 spin_unlock(&dq_data_lock);
Dmitry Monakhovdc52dd32009-12-14 15:21:15 +03001698 mark_all_dquot_dirty(inode->i_dquot);
Jan Kara657d3bf2008-07-25 01:46:52 -07001699 flush_warnings(inode->i_dquot, warntype);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701}
Mingming Cao08d03502009-01-14 16:19:32 +01001702EXPORT_SYMBOL(dquot_free_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703
1704/*
1705 * Transfer the number of inode and blocks from one diskquota to an other.
1706 *
1707 * This operation can block, but only after everything is updated
1708 * A transaction must be started when entering this function.
1709 */
Christoph Hellwigb43fa822010-03-03 09:05:03 -05001710static int __dquot_transfer(struct inode *inode, qid_t *chid, unsigned long mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711{
Mingming Cao740d9dc2009-01-13 16:43:14 +01001712 qsize_t space, cur_space;
1713 qsize_t rsv_space = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714 struct dquot *transfer_from[MAXQUOTAS];
1715 struct dquot *transfer_to[MAXQUOTAS];
Christoph Hellwigefd8f0e2010-03-03 09:05:08 -05001716 int cnt, ret = 0;
Jan Kara657d3bf2008-07-25 01:46:52 -07001717 char warntype_to[MAXQUOTAS];
1718 char warntype_from_inodes[MAXQUOTAS], warntype_from_space[MAXQUOTAS];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719
Ingo Molnard3be9152006-03-23 03:00:29 -08001720 /* First test before acquiring mutex - solves deadlocks when we
1721 * re-enter the quota code and are already holding the mutex */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722 if (IS_NOQUOTA(inode))
Christoph Hellwigefd8f0e2010-03-03 09:05:08 -05001723 return 0;
Jan Karacc334122009-01-12 17:23:05 +01001724 /* Initialize the arrays */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
Jan Karadd6f3c62009-01-26 16:01:43 +01001726 transfer_from[cnt] = NULL;
1727 transfer_to[cnt] = NULL;
Jan Kara657d3bf2008-07-25 01:46:52 -07001728 warntype_to[cnt] = QUOTA_NL_NOWARN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729 }
Dmitry Monakhov8ddd69d2010-02-16 08:31:50 +03001730 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1731 if (mask & (1 << cnt))
1732 transfer_to[cnt] = dqget(inode->i_sb, chid[cnt], cnt);
1733 }
Jan Karacc334122009-01-12 17:23:05 +01001734 down_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
Jan Karacc334122009-01-12 17:23:05 +01001735 if (IS_NOQUOTA(inode)) { /* File without quota accounting? */
1736 up_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
1737 goto put_all;
1738 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739 spin_lock(&dq_data_lock);
Mingming Cao740d9dc2009-01-13 16:43:14 +01001740 cur_space = inode_get_bytes(inode);
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001741 rsv_space = inode_get_rsv_space(inode);
Mingming Cao740d9dc2009-01-13 16:43:14 +01001742 space = cur_space + rsv_space;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743 /* Build the transfer_from list and check the limits */
1744 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
Jan Karadd6f3c62009-01-26 16:01:43 +01001745 if (!transfer_to[cnt])
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746 continue;
1747 transfer_from[cnt] = inode->i_dquot[cnt];
Christoph Hellwigefd8f0e2010-03-03 09:05:08 -05001748 ret = check_idq(transfer_to[cnt], 1, warntype_to + cnt);
1749 if (ret)
1750 goto over_quota;
1751 ret = check_bdq(transfer_to[cnt], space, 0, warntype_to + cnt);
1752 if (ret)
Jan Karacc334122009-01-12 17:23:05 +01001753 goto over_quota;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754 }
1755
1756 /*
1757 * Finally perform the needed transfer from transfer_from to transfer_to
1758 */
1759 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1760 /*
1761 * Skip changes for same uid or gid or for turned off quota-type.
1762 */
Jan Karadd6f3c62009-01-26 16:01:43 +01001763 if (!transfer_to[cnt])
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764 continue;
1765
1766 /* Due to IO error we might not have transfer_from[] structure */
1767 if (transfer_from[cnt]) {
Jan Kara657d3bf2008-07-25 01:46:52 -07001768 warntype_from_inodes[cnt] =
1769 info_idq_free(transfer_from[cnt], 1);
1770 warntype_from_space[cnt] =
1771 info_bdq_free(transfer_from[cnt], space);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772 dquot_decr_inodes(transfer_from[cnt], 1);
Mingming Cao740d9dc2009-01-13 16:43:14 +01001773 dquot_decr_space(transfer_from[cnt], cur_space);
1774 dquot_free_reserved_space(transfer_from[cnt],
1775 rsv_space);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776 }
1777
1778 dquot_incr_inodes(transfer_to[cnt], 1);
Mingming Cao740d9dc2009-01-13 16:43:14 +01001779 dquot_incr_space(transfer_to[cnt], cur_space);
1780 dquot_resv_space(transfer_to[cnt], rsv_space);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781
1782 inode->i_dquot[cnt] = transfer_to[cnt];
1783 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784 spin_unlock(&dq_data_lock);
Jan Karacc334122009-01-12 17:23:05 +01001785 up_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
1786
Dmitry Monakhovdc52dd32009-12-14 15:21:15 +03001787 mark_all_dquot_dirty(transfer_from);
1788 mark_all_dquot_dirty(transfer_to);
1789 /* The reference we got is transferred to the inode */
1790 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1791 transfer_to[cnt] = NULL;
Jan Karacc334122009-01-12 17:23:05 +01001792warn_put_all:
Jan Kara657d3bf2008-07-25 01:46:52 -07001793 flush_warnings(transfer_to, warntype_to);
1794 flush_warnings(transfer_from, warntype_from_inodes);
1795 flush_warnings(transfer_from, warntype_from_space);
Jan Karacc334122009-01-12 17:23:05 +01001796put_all:
Dmitry Monakhovdc52dd32009-12-14 15:21:15 +03001797 dqput_all(transfer_from);
1798 dqput_all(transfer_to);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799 return ret;
Jan Karacc334122009-01-12 17:23:05 +01001800over_quota:
1801 spin_unlock(&dq_data_lock);
1802 up_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
1803 /* Clear dquot pointers we don't want to dqput() */
1804 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
Jan Karadd6f3c62009-01-26 16:01:43 +01001805 transfer_from[cnt] = NULL;
Jan Karacc334122009-01-12 17:23:05 +01001806 goto warn_put_all;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807}
1808
Dmitry Monakhov8ddd69d2010-02-16 08:31:50 +03001809/* Wrapper for transferring ownership of an inode for uid/gid only
1810 * Called from FSXXX_setattr()
1811 */
Christoph Hellwigb43fa822010-03-03 09:05:03 -05001812int dquot_transfer(struct inode *inode, struct iattr *iattr)
Jan Karab85f4b82008-07-25 01:46:50 -07001813{
Dmitry Monakhov8ddd69d2010-02-16 08:31:50 +03001814 qid_t chid[MAXQUOTAS];
1815 unsigned long mask = 0;
1816
1817 if (iattr->ia_valid & ATTR_UID && iattr->ia_uid != inode->i_uid) {
1818 mask |= 1 << USRQUOTA;
1819 chid[USRQUOTA] = iattr->ia_uid;
1820 }
1821 if (iattr->ia_valid & ATTR_GID && iattr->ia_gid != inode->i_gid) {
1822 mask |= 1 << GRPQUOTA;
1823 chid[GRPQUOTA] = iattr->ia_gid;
1824 }
Dmitry Monakhov12755622010-04-08 22:04:20 +04001825 if (sb_any_quota_active(inode->i_sb) && !IS_NOQUOTA(inode))
Christoph Hellwigefd8f0e2010-03-03 09:05:08 -05001826 return __dquot_transfer(inode, chid, mask);
Dmitry Monakhov12755622010-04-08 22:04:20 +04001827
Jan Karab85f4b82008-07-25 01:46:50 -07001828 return 0;
1829}
Christoph Hellwigb43fa822010-03-03 09:05:03 -05001830EXPORT_SYMBOL(dquot_transfer);
Jan Karab85f4b82008-07-25 01:46:50 -07001831
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832/*
1833 * Write info of quota file to disk
1834 */
1835int dquot_commit_info(struct super_block *sb, int type)
1836{
1837 int ret;
1838 struct quota_info *dqopt = sb_dqopt(sb);
1839
Ingo Molnard3be9152006-03-23 03:00:29 -08001840 mutex_lock(&dqopt->dqio_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841 ret = dqopt->ops[type]->write_file_info(sb, type);
Ingo Molnard3be9152006-03-23 03:00:29 -08001842 mutex_unlock(&dqopt->dqio_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843 return ret;
1844}
Mingming Cao08d03502009-01-14 16:19:32 +01001845EXPORT_SYMBOL(dquot_commit_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846
1847/*
1848 * Definitions of diskquota operations.
1849 */
Alexey Dobriyan61e225d2009-09-21 17:01:08 -07001850const struct dquot_operations dquot_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851 .write_dquot = dquot_commit,
1852 .acquire_dquot = dquot_acquire,
1853 .release_dquot = dquot_release,
1854 .mark_dirty = dquot_mark_dquot_dirty,
Jan Kara74f783a2008-08-19 14:51:22 +02001855 .write_info = dquot_commit_info,
1856 .alloc_dquot = dquot_alloc,
1857 .destroy_dquot = dquot_destroy,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858};
1859
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860/*
Christoph Hellwig907f4552010-03-03 09:05:06 -05001861 * Generic helper for ->open on filesystems supporting disk quotas.
1862 */
1863int dquot_file_open(struct inode *inode, struct file *file)
1864{
1865 int error;
1866
1867 error = generic_file_open(inode, file);
1868 if (!error && (file->f_mode & FMODE_WRITE))
Christoph Hellwig871a2932010-03-03 09:05:07 -05001869 dquot_initialize(inode);
Christoph Hellwig907f4552010-03-03 09:05:06 -05001870 return error;
1871}
1872EXPORT_SYMBOL(dquot_file_open);
1873
1874/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875 * Turn quota off on a device. type == -1 ==> quotaoff for all types (umount)
1876 */
Jan Karaf55abc02008-08-20 17:50:32 +02001877int vfs_quota_disable(struct super_block *sb, int type, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878{
Jan Kara0ff5af82008-04-28 02:14:33 -07001879 int cnt, ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880 struct quota_info *dqopt = sb_dqopt(sb);
1881 struct inode *toputinode[MAXQUOTAS];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882
Jan Karaf55abc02008-08-20 17:50:32 +02001883 /* Cannot turn off usage accounting without turning off limits, or
1884 * suspend quotas and simultaneously turn quotas off. */
1885 if ((flags & DQUOT_USAGE_ENABLED && !(flags & DQUOT_LIMITS_ENABLED))
1886 || (flags & DQUOT_SUSPENDED && flags & (DQUOT_LIMITS_ENABLED |
1887 DQUOT_USAGE_ENABLED)))
1888 return -EINVAL;
1889
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890 /* We need to serialize quota_off() for device */
Ingo Molnard3be9152006-03-23 03:00:29 -08001891 mutex_lock(&dqopt->dqonoff_mutex);
Jan Kara9377abd2008-05-12 14:02:08 -07001892
1893 /*
1894 * Skip everything if there's nothing to do. We have to do this because
1895 * sometimes we are called when fill_super() failed and calling
1896 * sync_fs() in such cases does no good.
1897 */
Jan Karaf55abc02008-08-20 17:50:32 +02001898 if (!sb_any_quota_loaded(sb)) {
Jan Kara9377abd2008-05-12 14:02:08 -07001899 mutex_unlock(&dqopt->dqonoff_mutex);
1900 return 0;
1901 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1903 toputinode[cnt] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904 if (type != -1 && cnt != type)
1905 continue;
Jan Karaf55abc02008-08-20 17:50:32 +02001906 if (!sb_has_quota_loaded(sb, cnt))
Jan Kara0ff5af82008-04-28 02:14:33 -07001907 continue;
Jan Karaf55abc02008-08-20 17:50:32 +02001908
1909 if (flags & DQUOT_SUSPENDED) {
Jan Karacc334122009-01-12 17:23:05 +01001910 spin_lock(&dq_state_lock);
Jan Karaf55abc02008-08-20 17:50:32 +02001911 dqopt->flags |=
1912 dquot_state_flag(DQUOT_SUSPENDED, cnt);
Jan Karacc334122009-01-12 17:23:05 +01001913 spin_unlock(&dq_state_lock);
Jan Karaf55abc02008-08-20 17:50:32 +02001914 } else {
Jan Karacc334122009-01-12 17:23:05 +01001915 spin_lock(&dq_state_lock);
Jan Karaf55abc02008-08-20 17:50:32 +02001916 dqopt->flags &= ~dquot_state_flag(flags, cnt);
1917 /* Turning off suspended quotas? */
1918 if (!sb_has_quota_loaded(sb, cnt) &&
1919 sb_has_quota_suspended(sb, cnt)) {
1920 dqopt->flags &= ~dquot_state_flag(
1921 DQUOT_SUSPENDED, cnt);
Jan Karacc334122009-01-12 17:23:05 +01001922 spin_unlock(&dq_state_lock);
Jan Karaf55abc02008-08-20 17:50:32 +02001923 iput(dqopt->files[cnt]);
1924 dqopt->files[cnt] = NULL;
1925 continue;
1926 }
Jan Karacc334122009-01-12 17:23:05 +01001927 spin_unlock(&dq_state_lock);
Jan Kara0ff5af82008-04-28 02:14:33 -07001928 }
Jan Karaf55abc02008-08-20 17:50:32 +02001929
1930 /* We still have to keep quota loaded? */
1931 if (sb_has_quota_loaded(sb, cnt) && !(flags & DQUOT_SUSPENDED))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933
1934 /* Note: these are blocking operations */
1935 drop_dquot_ref(sb, cnt);
1936 invalidate_dquots(sb, cnt);
1937 /*
Jan Kara268157b2009-01-27 15:47:22 +01001938 * Now all dquots should be invalidated, all writes done so we
1939 * should be only users of the info. No locks needed.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940 */
1941 if (info_dirty(&dqopt->info[cnt]))
1942 sb->dq_op->write_info(sb, cnt);
1943 if (dqopt->ops[cnt]->free_file_info)
1944 dqopt->ops[cnt]->free_file_info(sb, cnt);
1945 put_quota_format(dqopt->info[cnt].dqi_format);
1946
1947 toputinode[cnt] = dqopt->files[cnt];
Jan Karaf55abc02008-08-20 17:50:32 +02001948 if (!sb_has_quota_loaded(sb, cnt))
Jan Kara0ff5af82008-04-28 02:14:33 -07001949 dqopt->files[cnt] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950 dqopt->info[cnt].dqi_flags = 0;
1951 dqopt->info[cnt].dqi_igrace = 0;
1952 dqopt->info[cnt].dqi_bgrace = 0;
1953 dqopt->ops[cnt] = NULL;
1954 }
Ingo Molnard3be9152006-03-23 03:00:29 -08001955 mutex_unlock(&dqopt->dqonoff_mutex);
Jan Karaca785ec2008-09-30 17:53:37 +02001956
1957 /* Skip syncing and setting flags if quota files are hidden */
1958 if (dqopt->flags & DQUOT_QUOTA_SYS_FILE)
1959 goto put_inodes;
1960
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961 /* Sync the superblock so that buffers with quota data are written to
Al Viro7b7b1ac2005-11-07 17:13:39 -05001962 * disk (and so userspace sees correct data afterwards). */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963 if (sb->s_op->sync_fs)
1964 sb->s_op->sync_fs(sb, 1);
1965 sync_blockdev(sb->s_bdev);
1966 /* Now the quota files are just ordinary files and we can set the
1967 * inode flags back. Moreover we discard the pagecache so that
1968 * userspace sees the writes we did bypassing the pagecache. We
1969 * must also discard the blockdev buffers so that we see the
1970 * changes done by userspace on the next quotaon() */
1971 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1972 if (toputinode[cnt]) {
Ingo Molnard3be9152006-03-23 03:00:29 -08001973 mutex_lock(&dqopt->dqonoff_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974 /* If quota was reenabled in the meantime, we have
1975 * nothing to do */
Jan Karaf55abc02008-08-20 17:50:32 +02001976 if (!sb_has_quota_loaded(sb, cnt)) {
Jan Kara268157b2009-01-27 15:47:22 +01001977 mutex_lock_nested(&toputinode[cnt]->i_mutex,
1978 I_MUTEX_QUOTA);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979 toputinode[cnt]->i_flags &= ~(S_IMMUTABLE |
1980 S_NOATIME | S_NOQUOTA);
Jan Kara268157b2009-01-27 15:47:22 +01001981 truncate_inode_pages(&toputinode[cnt]->i_data,
1982 0);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001983 mutex_unlock(&toputinode[cnt]->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984 mark_inode_dirty(toputinode[cnt]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985 }
Ingo Molnard3be9152006-03-23 03:00:29 -08001986 mutex_unlock(&dqopt->dqonoff_mutex);
Jan Karaca785ec2008-09-30 17:53:37 +02001987 }
1988 if (sb->s_bdev)
1989 invalidate_bdev(sb->s_bdev);
1990put_inodes:
1991 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1992 if (toputinode[cnt]) {
Jan Kara0ff5af82008-04-28 02:14:33 -07001993 /* On remount RO, we keep the inode pointer so that we
Jan Karaf55abc02008-08-20 17:50:32 +02001994 * can reenable quota on the subsequent remount RW. We
1995 * have to check 'flags' variable and not use sb_has_
1996 * function because another quotaon / quotaoff could
1997 * change global state before we got here. We refuse
1998 * to suspend quotas when there is pending delete on
1999 * the quota file... */
2000 if (!(flags & DQUOT_SUSPENDED))
Jan Kara0ff5af82008-04-28 02:14:33 -07002001 iput(toputinode[cnt]);
2002 else if (!toputinode[cnt]->i_nlink)
2003 ret = -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004 }
Jan Kara0ff5af82008-04-28 02:14:33 -07002005 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006}
Mingming Cao08d03502009-01-14 16:19:32 +01002007EXPORT_SYMBOL(vfs_quota_disable);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008
Jan Karaf55abc02008-08-20 17:50:32 +02002009int vfs_quota_off(struct super_block *sb, int type, int remount)
2010{
2011 return vfs_quota_disable(sb, type, remount ? DQUOT_SUSPENDED :
2012 (DQUOT_USAGE_ENABLED | DQUOT_LIMITS_ENABLED));
2013}
Mingming Cao08d03502009-01-14 16:19:32 +01002014EXPORT_SYMBOL(vfs_quota_off);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015/*
2016 * Turn quotas on on a device
2017 */
2018
Jan Karaf55abc02008-08-20 17:50:32 +02002019/*
2020 * Helper function to turn quotas on when we already have the inode of
2021 * quota file and no quota information is loaded.
2022 */
2023static int vfs_load_quota_inode(struct inode *inode, int type, int format_id,
2024 unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025{
2026 struct quota_format_type *fmt = find_quota_format(format_id);
2027 struct super_block *sb = inode->i_sb;
2028 struct quota_info *dqopt = sb_dqopt(sb);
2029 int error;
2030 int oldflags = -1;
2031
2032 if (!fmt)
2033 return -ESRCH;
2034 if (!S_ISREG(inode->i_mode)) {
2035 error = -EACCES;
2036 goto out_fmt;
2037 }
2038 if (IS_RDONLY(inode)) {
2039 error = -EROFS;
2040 goto out_fmt;
2041 }
2042 if (!sb->s_op->quota_write || !sb->s_op->quota_read) {
2043 error = -EINVAL;
2044 goto out_fmt;
2045 }
Jan Karaf55abc02008-08-20 17:50:32 +02002046 /* Usage always has to be set... */
2047 if (!(flags & DQUOT_USAGE_ENABLED)) {
2048 error = -EINVAL;
2049 goto out_fmt;
2050 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051
Jan Karaca785ec2008-09-30 17:53:37 +02002052 if (!(dqopt->flags & DQUOT_QUOTA_SYS_FILE)) {
Jan Karaab94c392010-02-22 21:07:17 +01002053 /* As we bypass the pagecache we must now flush all the
2054 * dirty data and invalidate caches so that kernel sees
2055 * changes from userspace. It is not enough to just flush
2056 * the quota file since if blocksize < pagesize, invalidation
2057 * of the cache could fail because of other unrelated dirty
2058 * data */
2059 sync_filesystem(sb);
Jan Karaca785ec2008-09-30 17:53:37 +02002060 invalidate_bdev(sb->s_bdev);
2061 }
Ingo Molnard3be9152006-03-23 03:00:29 -08002062 mutex_lock(&dqopt->dqonoff_mutex);
Jan Karaf55abc02008-08-20 17:50:32 +02002063 if (sb_has_quota_loaded(sb, type)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064 error = -EBUSY;
2065 goto out_lock;
2066 }
Jan Karaca785ec2008-09-30 17:53:37 +02002067
2068 if (!(dqopt->flags & DQUOT_QUOTA_SYS_FILE)) {
2069 /* We don't want quota and atime on quota files (deadlocks
2070 * possible) Also nobody should write to the file - we use
2071 * special IO operations which ignore the immutable bit. */
Jan Karadee86562009-07-22 18:12:17 +02002072 mutex_lock_nested(&inode->i_mutex, I_MUTEX_QUOTA);
Jan Kara268157b2009-01-27 15:47:22 +01002073 oldflags = inode->i_flags & (S_NOATIME | S_IMMUTABLE |
2074 S_NOQUOTA);
Jan Karaca785ec2008-09-30 17:53:37 +02002075 inode->i_flags |= S_NOQUOTA | S_NOATIME | S_IMMUTABLE;
Jan Karadee86562009-07-22 18:12:17 +02002076 mutex_unlock(&inode->i_mutex);
Jan Kara26245c92010-01-06 17:20:35 +01002077 /*
2078 * When S_NOQUOTA is set, remove dquot references as no more
2079 * references can be added
2080 */
Christoph Hellwig9f754752010-03-03 09:05:05 -05002081 __dquot_drop(inode);
Jan Karaca785ec2008-09-30 17:53:37 +02002082 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083
2084 error = -EIO;
2085 dqopt->files[type] = igrab(inode);
2086 if (!dqopt->files[type])
2087 goto out_lock;
2088 error = -EINVAL;
2089 if (!fmt->qf_ops->check_quota_file(sb, type))
2090 goto out_file_init;
2091
2092 dqopt->ops[type] = fmt->qf_ops;
2093 dqopt->info[type].dqi_format = fmt;
Jan Kara0ff5af82008-04-28 02:14:33 -07002094 dqopt->info[type].dqi_fmt_id = format_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095 INIT_LIST_HEAD(&dqopt->info[type].dqi_dirty_list);
Ingo Molnard3be9152006-03-23 03:00:29 -08002096 mutex_lock(&dqopt->dqio_mutex);
Jan Kara268157b2009-01-27 15:47:22 +01002097 error = dqopt->ops[type]->read_file_info(sb, type);
2098 if (error < 0) {
Ingo Molnard3be9152006-03-23 03:00:29 -08002099 mutex_unlock(&dqopt->dqio_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100 goto out_file_init;
2101 }
Ingo Molnard3be9152006-03-23 03:00:29 -08002102 mutex_unlock(&dqopt->dqio_mutex);
Jan Karacc334122009-01-12 17:23:05 +01002103 spin_lock(&dq_state_lock);
Jan Karaf55abc02008-08-20 17:50:32 +02002104 dqopt->flags |= dquot_state_flag(flags, type);
Jan Karacc334122009-01-12 17:23:05 +01002105 spin_unlock(&dq_state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106
2107 add_dquot_ref(sb, type);
Ingo Molnard3be9152006-03-23 03:00:29 -08002108 mutex_unlock(&dqopt->dqonoff_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109
2110 return 0;
2111
2112out_file_init:
2113 dqopt->files[type] = NULL;
2114 iput(inode);
2115out_lock:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116 if (oldflags != -1) {
Jan Karadee86562009-07-22 18:12:17 +02002117 mutex_lock_nested(&inode->i_mutex, I_MUTEX_QUOTA);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118 /* Set the flags back (in the case of accidental quotaon()
2119 * on a wrong file we don't want to mess up the flags) */
2120 inode->i_flags &= ~(S_NOATIME | S_NOQUOTA | S_IMMUTABLE);
2121 inode->i_flags |= oldflags;
Jan Karadee86562009-07-22 18:12:17 +02002122 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123 }
Jiaying Zhangd01730d2009-07-07 18:15:21 +02002124 mutex_unlock(&dqopt->dqonoff_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125out_fmt:
2126 put_quota_format(fmt);
2127
2128 return error;
2129}
2130
Jan Kara0ff5af82008-04-28 02:14:33 -07002131/* Reenable quotas on remount RW */
2132static int vfs_quota_on_remount(struct super_block *sb, int type)
2133{
2134 struct quota_info *dqopt = sb_dqopt(sb);
2135 struct inode *inode;
2136 int ret;
Jan Karaf55abc02008-08-20 17:50:32 +02002137 unsigned int flags;
Jan Kara0ff5af82008-04-28 02:14:33 -07002138
2139 mutex_lock(&dqopt->dqonoff_mutex);
2140 if (!sb_has_quota_suspended(sb, type)) {
2141 mutex_unlock(&dqopt->dqonoff_mutex);
2142 return 0;
2143 }
Jan Kara0ff5af82008-04-28 02:14:33 -07002144 inode = dqopt->files[type];
2145 dqopt->files[type] = NULL;
Jan Karacc334122009-01-12 17:23:05 +01002146 spin_lock(&dq_state_lock);
Jan Karaf55abc02008-08-20 17:50:32 +02002147 flags = dqopt->flags & dquot_state_flag(DQUOT_USAGE_ENABLED |
2148 DQUOT_LIMITS_ENABLED, type);
2149 dqopt->flags &= ~dquot_state_flag(DQUOT_STATE_FLAGS, type);
Jan Karacc334122009-01-12 17:23:05 +01002150 spin_unlock(&dq_state_lock);
Jan Kara0ff5af82008-04-28 02:14:33 -07002151 mutex_unlock(&dqopt->dqonoff_mutex);
2152
Jan Karaf55abc02008-08-20 17:50:32 +02002153 flags = dquot_generic_flag(flags, type);
2154 ret = vfs_load_quota_inode(inode, type, dqopt->info[type].dqi_fmt_id,
2155 flags);
Jan Kara0ff5af82008-04-28 02:14:33 -07002156 iput(inode);
2157
2158 return ret;
2159}
2160
Al Viro77e69da2008-08-01 04:29:18 -04002161int vfs_quota_on_path(struct super_block *sb, int type, int format_id,
2162 struct path *path)
2163{
2164 int error = security_quota_on(path->dentry);
2165 if (error)
2166 return error;
2167 /* Quota file not on the same filesystem? */
2168 if (path->mnt->mnt_sb != sb)
2169 error = -EXDEV;
2170 else
Jan Karaf55abc02008-08-20 17:50:32 +02002171 error = vfs_load_quota_inode(path->dentry->d_inode, type,
2172 format_id, DQUOT_USAGE_ENABLED |
2173 DQUOT_LIMITS_ENABLED);
Al Viro77e69da2008-08-01 04:29:18 -04002174 return error;
2175}
Mingming Cao08d03502009-01-14 16:19:32 +01002176EXPORT_SYMBOL(vfs_quota_on_path);
Al Viro77e69da2008-08-01 04:29:18 -04002177
Al Viro82646132008-08-02 00:57:06 -04002178int vfs_quota_on(struct super_block *sb, int type, int format_id, char *name,
Jan Kara0ff5af82008-04-28 02:14:33 -07002179 int remount)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180{
Al Viro82646132008-08-02 00:57:06 -04002181 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182 int error;
2183
Jan Kara0ff5af82008-04-28 02:14:33 -07002184 if (remount)
2185 return vfs_quota_on_remount(sb, type);
2186
Al Viro82646132008-08-02 00:57:06 -04002187 error = kern_path(name, LOOKUP_FOLLOW, &path);
Al Viro77e69da2008-08-01 04:29:18 -04002188 if (!error) {
Al Viro82646132008-08-02 00:57:06 -04002189 error = vfs_quota_on_path(sb, type, format_id, &path);
2190 path_put(&path);
Al Viro77e69da2008-08-01 04:29:18 -04002191 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192 return error;
2193}
Mingming Cao08d03502009-01-14 16:19:32 +01002194EXPORT_SYMBOL(vfs_quota_on);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195
2196/*
Jan Karaf55abc02008-08-20 17:50:32 +02002197 * More powerful function for turning on quotas allowing setting
2198 * of individual quota flags
2199 */
2200int vfs_quota_enable(struct inode *inode, int type, int format_id,
2201 unsigned int flags)
2202{
2203 int ret = 0;
2204 struct super_block *sb = inode->i_sb;
2205 struct quota_info *dqopt = sb_dqopt(sb);
2206
2207 /* Just unsuspend quotas? */
2208 if (flags & DQUOT_SUSPENDED)
2209 return vfs_quota_on_remount(sb, type);
2210 if (!flags)
2211 return 0;
2212 /* Just updating flags needed? */
2213 if (sb_has_quota_loaded(sb, type)) {
2214 mutex_lock(&dqopt->dqonoff_mutex);
2215 /* Now do a reliable test... */
2216 if (!sb_has_quota_loaded(sb, type)) {
2217 mutex_unlock(&dqopt->dqonoff_mutex);
2218 goto load_quota;
2219 }
2220 if (flags & DQUOT_USAGE_ENABLED &&
2221 sb_has_quota_usage_enabled(sb, type)) {
2222 ret = -EBUSY;
2223 goto out_lock;
2224 }
2225 if (flags & DQUOT_LIMITS_ENABLED &&
2226 sb_has_quota_limits_enabled(sb, type)) {
2227 ret = -EBUSY;
2228 goto out_lock;
2229 }
Jan Karacc334122009-01-12 17:23:05 +01002230 spin_lock(&dq_state_lock);
Jan Karaf55abc02008-08-20 17:50:32 +02002231 sb_dqopt(sb)->flags |= dquot_state_flag(flags, type);
Jan Karacc334122009-01-12 17:23:05 +01002232 spin_unlock(&dq_state_lock);
Jan Karaf55abc02008-08-20 17:50:32 +02002233out_lock:
2234 mutex_unlock(&dqopt->dqonoff_mutex);
2235 return ret;
2236 }
2237
2238load_quota:
2239 return vfs_load_quota_inode(inode, type, format_id, flags);
2240}
Mingming Cao08d03502009-01-14 16:19:32 +01002241EXPORT_SYMBOL(vfs_quota_enable);
Jan Karaf55abc02008-08-20 17:50:32 +02002242
2243/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244 * This function is used when filesystem needs to initialize quotas
2245 * during mount time.
2246 */
Christoph Hellwig84de8562005-06-23 00:09:16 -07002247int vfs_quota_on_mount(struct super_block *sb, char *qf_name,
2248 int format_id, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249{
Christoph Hellwig84de8562005-06-23 00:09:16 -07002250 struct dentry *dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251 int error;
2252
Jan Karac56818d2009-11-12 15:42:08 +01002253 mutex_lock(&sb->s_root->d_inode->i_mutex);
Christoph Hellwig2fa389c2005-06-23 00:09:16 -07002254 dentry = lookup_one_len(qf_name, sb->s_root, strlen(qf_name));
Jan Karac56818d2009-11-12 15:42:08 +01002255 mutex_unlock(&sb->s_root->d_inode->i_mutex);
Christoph Hellwig84de8562005-06-23 00:09:16 -07002256 if (IS_ERR(dentry))
2257 return PTR_ERR(dentry);
2258
Jan Kara154f4842005-11-28 13:44:14 -08002259 if (!dentry->d_inode) {
2260 error = -ENOENT;
2261 goto out;
2262 }
2263
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264 error = security_quota_on(dentry);
Christoph Hellwig84de8562005-06-23 00:09:16 -07002265 if (!error)
Jan Karaf55abc02008-08-20 17:50:32 +02002266 error = vfs_load_quota_inode(dentry->d_inode, type, format_id,
2267 DQUOT_USAGE_ENABLED | DQUOT_LIMITS_ENABLED);
Christoph Hellwig84de8562005-06-23 00:09:16 -07002268
Jan Kara154f4842005-11-28 13:44:14 -08002269out:
Christoph Hellwig84de8562005-06-23 00:09:16 -07002270 dput(dentry);
2271 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272}
Mingming Cao08d03502009-01-14 16:19:32 +01002273EXPORT_SYMBOL(vfs_quota_on_mount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274
Jan Karab85f4b82008-07-25 01:46:50 -07002275/* Wrapper to turn on quotas when remounting rw */
2276int vfs_dq_quota_on_remount(struct super_block *sb)
2277{
2278 int cnt;
2279 int ret = 0, err;
2280
2281 if (!sb->s_qcop || !sb->s_qcop->quota_on)
2282 return -ENOSYS;
2283 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
2284 err = sb->s_qcop->quota_on(sb, cnt, 0, NULL, 1);
2285 if (err < 0 && !ret)
2286 ret = err;
2287 }
2288 return ret;
2289}
Mingming Cao08d03502009-01-14 16:19:32 +01002290EXPORT_SYMBOL(vfs_dq_quota_on_remount);
Jan Karab85f4b82008-07-25 01:46:50 -07002291
Jan Kara12095462008-08-20 14:45:12 +02002292static inline qsize_t qbtos(qsize_t blocks)
2293{
2294 return blocks << QIF_DQBLKSIZE_BITS;
2295}
2296
2297static inline qsize_t stoqb(qsize_t space)
2298{
2299 return (space + QIF_DQBLKSIZE - 1) >> QIF_DQBLKSIZE_BITS;
2300}
2301
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302/* Generic routine for getting common part of quota structure */
Christoph Hellwigb9b2dd32010-05-06 17:04:58 -04002303static void do_get_dqblk(struct dquot *dquot, struct fs_disk_quota *di)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304{
2305 struct mem_dqblk *dm = &dquot->dq_dqb;
2306
Christoph Hellwigb9b2dd32010-05-06 17:04:58 -04002307 memset(di, 0, sizeof(*di));
2308 di->d_version = FS_DQUOT_VERSION;
2309 di->d_flags = dquot->dq_type == USRQUOTA ?
2310 XFS_USER_QUOTA : XFS_GROUP_QUOTA;
2311 di->d_id = dquot->dq_id;
2312
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313 spin_lock(&dq_data_lock);
Christoph Hellwigb9b2dd32010-05-06 17:04:58 -04002314 di->d_blk_hardlimit = stoqb(dm->dqb_bhardlimit);
2315 di->d_blk_softlimit = stoqb(dm->dqb_bsoftlimit);
2316 di->d_ino_hardlimit = dm->dqb_ihardlimit;
2317 di->d_ino_softlimit = dm->dqb_isoftlimit;
2318 di->d_bcount = dm->dqb_curspace + dm->dqb_rsvspace;
2319 di->d_icount = dm->dqb_curinodes;
2320 di->d_btimer = dm->dqb_btime;
2321 di->d_itimer = dm->dqb_itime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322 spin_unlock(&dq_data_lock);
2323}
2324
Jan Kara268157b2009-01-27 15:47:22 +01002325int vfs_get_dqblk(struct super_block *sb, int type, qid_t id,
Christoph Hellwigb9b2dd32010-05-06 17:04:58 -04002326 struct fs_disk_quota *di)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327{
2328 struct dquot *dquot;
2329
Jan Karacc334122009-01-12 17:23:05 +01002330 dquot = dqget(sb, id, type);
Jan Karadd6f3c62009-01-26 16:01:43 +01002331 if (!dquot)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002332 return -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002333 do_get_dqblk(dquot, di);
2334 dqput(dquot);
Jan Karacc334122009-01-12 17:23:05 +01002335
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336 return 0;
2337}
Mingming Cao08d03502009-01-14 16:19:32 +01002338EXPORT_SYMBOL(vfs_get_dqblk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339
Christoph Hellwigc472b432010-05-06 17:05:17 -04002340#define VFS_FS_DQ_MASK \
2341 (FS_DQ_BCOUNT | FS_DQ_BSOFT | FS_DQ_BHARD | \
2342 FS_DQ_ICOUNT | FS_DQ_ISOFT | FS_DQ_IHARD | \
2343 FS_DQ_BTIMER | FS_DQ_ITIMER)
2344
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345/* Generic routine for setting common part of quota structure */
Christoph Hellwigc472b432010-05-06 17:05:17 -04002346static int do_set_dqblk(struct dquot *dquot, struct fs_disk_quota *di)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002347{
2348 struct mem_dqblk *dm = &dquot->dq_dqb;
2349 int check_blim = 0, check_ilim = 0;
Andrew Perepechko338bf9a2008-04-28 02:14:31 -07002350 struct mem_dqinfo *dqi = &sb_dqopt(dquot->dq_sb)->info[dquot->dq_type];
2351
Christoph Hellwigc472b432010-05-06 17:05:17 -04002352 if (di->d_fieldmask & ~VFS_FS_DQ_MASK)
2353 return -EINVAL;
2354
2355 if (((di->d_fieldmask & FS_DQ_BSOFT) &&
2356 (di->d_blk_softlimit > dqi->dqi_maxblimit)) ||
2357 ((di->d_fieldmask & FS_DQ_BHARD) &&
2358 (di->d_blk_hardlimit > dqi->dqi_maxblimit)) ||
2359 ((di->d_fieldmask & FS_DQ_ISOFT) &&
2360 (di->d_ino_softlimit > dqi->dqi_maxilimit)) ||
2361 ((di->d_fieldmask & FS_DQ_IHARD) &&
2362 (di->d_ino_hardlimit > dqi->dqi_maxilimit)))
Andrew Perepechko338bf9a2008-04-28 02:14:31 -07002363 return -ERANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364
2365 spin_lock(&dq_data_lock);
Christoph Hellwigc472b432010-05-06 17:05:17 -04002366 if (di->d_fieldmask & FS_DQ_BCOUNT) {
2367 dm->dqb_curspace = di->d_bcount - dm->dqb_rsvspace;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002368 check_blim = 1;
Andrew Perepechko08261672010-04-12 22:16:50 +04002369 set_bit(DQ_LASTSET_B + QIF_SPACE_B, &dquot->dq_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002370 }
Christoph Hellwigc472b432010-05-06 17:05:17 -04002371
2372 if (di->d_fieldmask & FS_DQ_BSOFT)
2373 dm->dqb_bsoftlimit = qbtos(di->d_blk_softlimit);
2374 if (di->d_fieldmask & FS_DQ_BHARD)
2375 dm->dqb_bhardlimit = qbtos(di->d_blk_hardlimit);
2376 if (di->d_fieldmask & (FS_DQ_BSOFT | FS_DQ_BHARD)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377 check_blim = 1;
Andrew Perepechko08261672010-04-12 22:16:50 +04002378 set_bit(DQ_LASTSET_B + QIF_BLIMITS_B, &dquot->dq_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002379 }
Christoph Hellwigc472b432010-05-06 17:05:17 -04002380
2381 if (di->d_fieldmask & FS_DQ_ICOUNT) {
2382 dm->dqb_curinodes = di->d_icount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002383 check_ilim = 1;
Andrew Perepechko08261672010-04-12 22:16:50 +04002384 set_bit(DQ_LASTSET_B + QIF_INODES_B, &dquot->dq_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002385 }
Christoph Hellwigc472b432010-05-06 17:05:17 -04002386
2387 if (di->d_fieldmask & FS_DQ_ISOFT)
2388 dm->dqb_isoftlimit = di->d_ino_softlimit;
2389 if (di->d_fieldmask & FS_DQ_IHARD)
2390 dm->dqb_ihardlimit = di->d_ino_hardlimit;
2391 if (di->d_fieldmask & (FS_DQ_ISOFT | FS_DQ_IHARD)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392 check_ilim = 1;
Andrew Perepechko08261672010-04-12 22:16:50 +04002393 set_bit(DQ_LASTSET_B + QIF_ILIMITS_B, &dquot->dq_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394 }
Christoph Hellwigc472b432010-05-06 17:05:17 -04002395
2396 if (di->d_fieldmask & FS_DQ_BTIMER) {
2397 dm->dqb_btime = di->d_btimer;
Jan Karae04a88a92009-01-07 18:07:29 -08002398 check_blim = 1;
Andrew Perepechko08261672010-04-12 22:16:50 +04002399 set_bit(DQ_LASTSET_B + QIF_BTIME_B, &dquot->dq_flags);
Jan Kara4d59bce2008-10-02 16:48:10 +02002400 }
Christoph Hellwigc472b432010-05-06 17:05:17 -04002401
2402 if (di->d_fieldmask & FS_DQ_ITIMER) {
2403 dm->dqb_itime = di->d_itimer;
Jan Karae04a88a92009-01-07 18:07:29 -08002404 check_ilim = 1;
Andrew Perepechko08261672010-04-12 22:16:50 +04002405 set_bit(DQ_LASTSET_B + QIF_ITIME_B, &dquot->dq_flags);
Jan Kara4d59bce2008-10-02 16:48:10 +02002406 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002407
2408 if (check_blim) {
Jan Kara268157b2009-01-27 15:47:22 +01002409 if (!dm->dqb_bsoftlimit ||
2410 dm->dqb_curspace < dm->dqb_bsoftlimit) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002411 dm->dqb_btime = 0;
2412 clear_bit(DQ_BLKS_B, &dquot->dq_flags);
Christoph Hellwigc472b432010-05-06 17:05:17 -04002413 } else if (!(di->d_fieldmask & FS_DQ_BTIMER))
Jan Kara268157b2009-01-27 15:47:22 +01002414 /* Set grace only if user hasn't provided his own... */
Andrew Perepechko338bf9a2008-04-28 02:14:31 -07002415 dm->dqb_btime = get_seconds() + dqi->dqi_bgrace;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002416 }
2417 if (check_ilim) {
Jan Kara268157b2009-01-27 15:47:22 +01002418 if (!dm->dqb_isoftlimit ||
2419 dm->dqb_curinodes < dm->dqb_isoftlimit) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002420 dm->dqb_itime = 0;
2421 clear_bit(DQ_INODES_B, &dquot->dq_flags);
Christoph Hellwigc472b432010-05-06 17:05:17 -04002422 } else if (!(di->d_fieldmask & FS_DQ_ITIMER))
Jan Kara268157b2009-01-27 15:47:22 +01002423 /* Set grace only if user hasn't provided his own... */
Andrew Perepechko338bf9a2008-04-28 02:14:31 -07002424 dm->dqb_itime = get_seconds() + dqi->dqi_igrace;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002425 }
Jan Kara268157b2009-01-27 15:47:22 +01002426 if (dm->dqb_bhardlimit || dm->dqb_bsoftlimit || dm->dqb_ihardlimit ||
2427 dm->dqb_isoftlimit)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002428 clear_bit(DQ_FAKE_B, &dquot->dq_flags);
2429 else
2430 set_bit(DQ_FAKE_B, &dquot->dq_flags);
2431 spin_unlock(&dq_data_lock);
2432 mark_dquot_dirty(dquot);
Andrew Perepechko338bf9a2008-04-28 02:14:31 -07002433
2434 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002435}
2436
Jan Kara268157b2009-01-27 15:47:22 +01002437int vfs_set_dqblk(struct super_block *sb, int type, qid_t id,
Christoph Hellwigc472b432010-05-06 17:05:17 -04002438 struct fs_disk_quota *di)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439{
2440 struct dquot *dquot;
Andrew Perepechko338bf9a2008-04-28 02:14:31 -07002441 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442
Jan Karaf55abc02008-08-20 17:50:32 +02002443 dquot = dqget(sb, id, type);
2444 if (!dquot) {
2445 rc = -ESRCH;
2446 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002447 }
Andrew Perepechko338bf9a2008-04-28 02:14:31 -07002448 rc = do_set_dqblk(dquot, di);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002449 dqput(dquot);
Jan Karaf55abc02008-08-20 17:50:32 +02002450out:
Andrew Perepechko338bf9a2008-04-28 02:14:31 -07002451 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002452}
Mingming Cao08d03502009-01-14 16:19:32 +01002453EXPORT_SYMBOL(vfs_set_dqblk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454
2455/* Generic routine for getting common part of quota file information */
2456int vfs_get_dqinfo(struct super_block *sb, int type, struct if_dqinfo *ii)
2457{
2458 struct mem_dqinfo *mi;
2459
Ingo Molnard3be9152006-03-23 03:00:29 -08002460 mutex_lock(&sb_dqopt(sb)->dqonoff_mutex);
Jan Karaf55abc02008-08-20 17:50:32 +02002461 if (!sb_has_quota_active(sb, type)) {
Ingo Molnard3be9152006-03-23 03:00:29 -08002462 mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002463 return -ESRCH;
2464 }
2465 mi = sb_dqopt(sb)->info + type;
2466 spin_lock(&dq_data_lock);
2467 ii->dqi_bgrace = mi->dqi_bgrace;
2468 ii->dqi_igrace = mi->dqi_igrace;
2469 ii->dqi_flags = mi->dqi_flags & DQF_MASK;
2470 ii->dqi_valid = IIF_ALL;
2471 spin_unlock(&dq_data_lock);
Ingo Molnard3be9152006-03-23 03:00:29 -08002472 mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473 return 0;
2474}
Mingming Cao08d03502009-01-14 16:19:32 +01002475EXPORT_SYMBOL(vfs_get_dqinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002476
2477/* Generic routine for setting common part of quota file information */
2478int vfs_set_dqinfo(struct super_block *sb, int type, struct if_dqinfo *ii)
2479{
2480 struct mem_dqinfo *mi;
Jan Karaf55abc02008-08-20 17:50:32 +02002481 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002482
Ingo Molnard3be9152006-03-23 03:00:29 -08002483 mutex_lock(&sb_dqopt(sb)->dqonoff_mutex);
Jan Karaf55abc02008-08-20 17:50:32 +02002484 if (!sb_has_quota_active(sb, type)) {
2485 err = -ESRCH;
2486 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002487 }
2488 mi = sb_dqopt(sb)->info + type;
2489 spin_lock(&dq_data_lock);
2490 if (ii->dqi_valid & IIF_BGRACE)
2491 mi->dqi_bgrace = ii->dqi_bgrace;
2492 if (ii->dqi_valid & IIF_IGRACE)
2493 mi->dqi_igrace = ii->dqi_igrace;
2494 if (ii->dqi_valid & IIF_FLAGS)
Jan Kara268157b2009-01-27 15:47:22 +01002495 mi->dqi_flags = (mi->dqi_flags & ~DQF_MASK) |
2496 (ii->dqi_flags & DQF_MASK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002497 spin_unlock(&dq_data_lock);
2498 mark_info_dirty(sb, type);
2499 /* Force write to disk */
2500 sb->dq_op->write_info(sb, type);
Jan Karaf55abc02008-08-20 17:50:32 +02002501out:
Ingo Molnard3be9152006-03-23 03:00:29 -08002502 mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
Jan Karaf55abc02008-08-20 17:50:32 +02002503 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002504}
Mingming Cao08d03502009-01-14 16:19:32 +01002505EXPORT_SYMBOL(vfs_set_dqinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002506
Alexey Dobriyan0d54b212009-09-21 17:01:09 -07002507const struct quotactl_ops vfs_quotactl_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002508 .quota_on = vfs_quota_on,
2509 .quota_off = vfs_quota_off,
2510 .quota_sync = vfs_quota_sync,
2511 .get_info = vfs_get_dqinfo,
2512 .set_info = vfs_set_dqinfo,
2513 .get_dqblk = vfs_get_dqblk,
2514 .set_dqblk = vfs_set_dqblk
2515};
2516
Dmitry Monakhovdde95882010-04-26 20:03:33 +04002517
2518static int do_proc_dqstats(struct ctl_table *table, int write,
2519 void __user *buffer, size_t *lenp, loff_t *ppos)
2520{
2521#ifdef CONFIG_SMP
2522 /* Update global table */
2523 unsigned int type = (int *)table->data - dqstats.stat;
2524 dqstats.stat[type] = dqstats_read(type);
2525#endif
2526 return proc_dointvec(table, write, buffer, lenp, ppos);
2527}
2528
Linus Torvalds1da177e2005-04-16 15:20:36 -07002529static ctl_table fs_dqstats_table[] = {
2530 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002531 .procname = "lookups",
Dmitry Monakhovdde95882010-04-26 20:03:33 +04002532 .data = &dqstats.stat[DQST_LOOKUPS],
Linus Torvalds1da177e2005-04-16 15:20:36 -07002533 .maxlen = sizeof(int),
2534 .mode = 0444,
Dmitry Monakhovdde95882010-04-26 20:03:33 +04002535 .proc_handler = do_proc_dqstats,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002536 },
2537 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002538 .procname = "drops",
Dmitry Monakhovdde95882010-04-26 20:03:33 +04002539 .data = &dqstats.stat[DQST_DROPS],
Linus Torvalds1da177e2005-04-16 15:20:36 -07002540 .maxlen = sizeof(int),
2541 .mode = 0444,
Dmitry Monakhovdde95882010-04-26 20:03:33 +04002542 .proc_handler = do_proc_dqstats,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002543 },
2544 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002545 .procname = "reads",
Dmitry Monakhovdde95882010-04-26 20:03:33 +04002546 .data = &dqstats.stat[DQST_READS],
Linus Torvalds1da177e2005-04-16 15:20:36 -07002547 .maxlen = sizeof(int),
2548 .mode = 0444,
Dmitry Monakhovdde95882010-04-26 20:03:33 +04002549 .proc_handler = do_proc_dqstats,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002550 },
2551 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002552 .procname = "writes",
Dmitry Monakhovdde95882010-04-26 20:03:33 +04002553 .data = &dqstats.stat[DQST_WRITES],
Linus Torvalds1da177e2005-04-16 15:20:36 -07002554 .maxlen = sizeof(int),
2555 .mode = 0444,
Dmitry Monakhovdde95882010-04-26 20:03:33 +04002556 .proc_handler = do_proc_dqstats,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002557 },
2558 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002559 .procname = "cache_hits",
Dmitry Monakhovdde95882010-04-26 20:03:33 +04002560 .data = &dqstats.stat[DQST_CACHE_HITS],
Linus Torvalds1da177e2005-04-16 15:20:36 -07002561 .maxlen = sizeof(int),
2562 .mode = 0444,
Dmitry Monakhovdde95882010-04-26 20:03:33 +04002563 .proc_handler = do_proc_dqstats,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002564 },
2565 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002566 .procname = "allocated_dquots",
Dmitry Monakhovdde95882010-04-26 20:03:33 +04002567 .data = &dqstats.stat[DQST_ALLOC_DQUOTS],
Linus Torvalds1da177e2005-04-16 15:20:36 -07002568 .maxlen = sizeof(int),
2569 .mode = 0444,
Dmitry Monakhovdde95882010-04-26 20:03:33 +04002570 .proc_handler = do_proc_dqstats,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002571 },
2572 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002573 .procname = "free_dquots",
Dmitry Monakhovdde95882010-04-26 20:03:33 +04002574 .data = &dqstats.stat[DQST_FREE_DQUOTS],
Linus Torvalds1da177e2005-04-16 15:20:36 -07002575 .maxlen = sizeof(int),
2576 .mode = 0444,
Dmitry Monakhovdde95882010-04-26 20:03:33 +04002577 .proc_handler = do_proc_dqstats,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002578 },
2579 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002580 .procname = "syncs",
Dmitry Monakhovdde95882010-04-26 20:03:33 +04002581 .data = &dqstats.stat[DQST_SYNCS],
Linus Torvalds1da177e2005-04-16 15:20:36 -07002582 .maxlen = sizeof(int),
2583 .mode = 0444,
Dmitry Monakhovdde95882010-04-26 20:03:33 +04002584 .proc_handler = do_proc_dqstats,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002585 },
Jan Kara8e893462007-10-16 23:29:31 -07002586#ifdef CONFIG_PRINT_QUOTA_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07002587 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002588 .procname = "warnings",
2589 .data = &flag_print_warnings,
2590 .maxlen = sizeof(int),
2591 .mode = 0644,
Eric W. Biederman6d456112009-11-16 03:11:48 -08002592 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002593 },
Jan Kara8e893462007-10-16 23:29:31 -07002594#endif
Eric W. Biedermanab092032009-11-05 14:25:10 -08002595 { },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002596};
2597
2598static ctl_table fs_table[] = {
2599 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002600 .procname = "quota",
2601 .mode = 0555,
2602 .child = fs_dqstats_table,
2603 },
Eric W. Biedermanab092032009-11-05 14:25:10 -08002604 { },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002605};
2606
2607static ctl_table sys_table[] = {
2608 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002609 .procname = "fs",
2610 .mode = 0555,
2611 .child = fs_table,
2612 },
Eric W. Biedermanab092032009-11-05 14:25:10 -08002613 { },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002614};
2615
2616static int __init dquot_init(void)
2617{
2618 int i;
2619 unsigned long nr_hash, order;
2620
2621 printk(KERN_NOTICE "VFS: Disk quotas %s\n", __DQUOT_VERSION__);
2622
Eric W. Biederman0b4d4142007-02-14 00:34:09 -08002623 register_sysctl_table(sys_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002624
Paul Mundt20c2df82007-07-20 10:11:58 +09002625 dquot_cachep = kmem_cache_create("dquot",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002626 sizeof(struct dquot), sizeof(unsigned long) * 4,
Paul Jacksonfffb60f2006-03-24 03:16:06 -08002627 (SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT|
2628 SLAB_MEM_SPREAD|SLAB_PANIC),
Paul Mundt20c2df82007-07-20 10:11:58 +09002629 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002630
2631 order = 0;
2632 dquot_hash = (struct hlist_head *)__get_free_pages(GFP_ATOMIC, order);
2633 if (!dquot_hash)
2634 panic("Cannot create dquot hash table");
2635
Dmitry Monakhovdde95882010-04-26 20:03:33 +04002636#ifdef CONFIG_SMP
2637 dqstats_pcpu = alloc_percpu(struct dqstats);
2638 if (!dqstats_pcpu)
2639 panic("Cannot create dquot stats table");
2640#endif
2641 memset(&dqstats, 0, sizeof(struct dqstats));
2642
Linus Torvalds1da177e2005-04-16 15:20:36 -07002643 /* Find power-of-two hlist_heads which can fit into allocation */
2644 nr_hash = (1UL << order) * PAGE_SIZE / sizeof(struct hlist_head);
2645 dq_hash_bits = 0;
2646 do {
2647 dq_hash_bits++;
2648 } while (nr_hash >> dq_hash_bits);
2649 dq_hash_bits--;
2650
2651 nr_hash = 1UL << dq_hash_bits;
2652 dq_hash_mask = nr_hash - 1;
2653 for (i = 0; i < nr_hash; i++)
2654 INIT_HLIST_HEAD(dquot_hash + i);
2655
2656 printk("Dquot-cache hash table entries: %ld (order %ld, %ld bytes)\n",
2657 nr_hash, order, (PAGE_SIZE << order));
2658
Rusty Russell8e1f9362007-07-17 04:03:17 -07002659 register_shrinker(&dqcache_shrinker);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002660
Linus Torvalds1da177e2005-04-16 15:20:36 -07002661 return 0;
2662}
2663module_init(dquot_init);