blob: 2eebf72d07c876b270517d966459e9efbf23be67 [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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231
Jan Kara0a5a9c72010-02-09 18:20:39 +0100232static qsize_t inode_get_rsv_space(struct inode *inode);
Christoph Hellwig871a2932010-03-03 09:05:07 -0500233static void __dquot_initialize(struct inode *inode, int type);
Jan Kara0a5a9c72010-02-09 18:20:39 +0100234
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235static inline unsigned int
236hashfn(const struct super_block *sb, unsigned int id, int type)
237{
238 unsigned long tmp;
239
240 tmp = (((unsigned long)sb>>L1_CACHE_SHIFT) ^ id) * (MAXQUOTAS - type);
241 return (tmp + (tmp >> dq_hash_bits)) & dq_hash_mask;
242}
243
244/*
245 * Following list functions expect dq_list_lock to be held
246 */
247static inline void insert_dquot_hash(struct dquot *dquot)
248{
Jan Kara268157b2009-01-27 15:47:22 +0100249 struct hlist_head *head;
250 head = dquot_hash + hashfn(dquot->dq_sb, dquot->dq_id, dquot->dq_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 hlist_add_head(&dquot->dq_hash, head);
252}
253
254static inline void remove_dquot_hash(struct dquot *dquot)
255{
256 hlist_del_init(&dquot->dq_hash);
257}
258
Jan Kara7a2435d2009-01-27 01:47:11 +0100259static struct dquot *find_dquot(unsigned int hashent, struct super_block *sb,
260 unsigned int id, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261{
262 struct hlist_node *node;
263 struct dquot *dquot;
264
265 hlist_for_each (node, dquot_hash+hashent) {
266 dquot = hlist_entry(node, struct dquot, dq_hash);
Jan Kara268157b2009-01-27 15:47:22 +0100267 if (dquot->dq_sb == sb && dquot->dq_id == id &&
268 dquot->dq_type == type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 return dquot;
270 }
Jan Karadd6f3c62009-01-26 16:01:43 +0100271 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272}
273
274/* Add a dquot to the tail of the free list */
275static inline void put_dquot_last(struct dquot *dquot)
276{
Akinobu Mita8e130592006-06-26 00:24:37 -0700277 list_add_tail(&dquot->dq_free, &free_dquots);
Dmitry Monakhovdde95882010-04-26 20:03:33 +0400278 dqstats_inc(DQST_FREE_DQUOTS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279}
280
281static inline void remove_free_dquot(struct dquot *dquot)
282{
283 if (list_empty(&dquot->dq_free))
284 return;
285 list_del_init(&dquot->dq_free);
Dmitry Monakhovdde95882010-04-26 20:03:33 +0400286 dqstats_dec(DQST_FREE_DQUOTS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287}
288
289static inline void put_inuse(struct dquot *dquot)
290{
291 /* We add to the back of inuse list so we don't have to restart
292 * when traversing this list and we block */
Akinobu Mita8e130592006-06-26 00:24:37 -0700293 list_add_tail(&dquot->dq_inuse, &inuse_list);
Dmitry Monakhovdde95882010-04-26 20:03:33 +0400294 dqstats_inc(DQST_ALLOC_DQUOTS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295}
296
297static inline void remove_inuse(struct dquot *dquot)
298{
Dmitry Monakhovdde95882010-04-26 20:03:33 +0400299 dqstats_dec(DQST_ALLOC_DQUOTS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 list_del(&dquot->dq_inuse);
301}
302/*
303 * End of list functions needing dq_list_lock
304 */
305
306static void wait_on_dquot(struct dquot *dquot)
307{
Ingo Molnard3be9152006-03-23 03:00:29 -0800308 mutex_lock(&dquot->dq_lock);
309 mutex_unlock(&dquot->dq_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310}
311
Jan Kara03f6e922008-04-28 02:14:32 -0700312static inline int dquot_dirty(struct dquot *dquot)
313{
314 return test_bit(DQ_MOD_B, &dquot->dq_flags);
315}
316
317static inline int mark_dquot_dirty(struct dquot *dquot)
318{
319 return dquot->dq_sb->dq_op->mark_dirty(dquot);
320}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321
Dmitry Monakhoveabf2902010-03-27 15:15:38 +0300322/* Mark dquot dirty in atomic manner, and return it's old dirty flag state */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323int dquot_mark_dquot_dirty(struct dquot *dquot)
324{
Dmitry Monakhoveabf2902010-03-27 15:15:38 +0300325 int ret = 1;
326
327 /* If quota is dirty already, we don't have to acquire dq_list_lock */
328 if (test_bit(DQ_MOD_B, &dquot->dq_flags))
329 return 1;
330
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 spin_lock(&dq_list_lock);
Dmitry Monakhoveabf2902010-03-27 15:15:38 +0300332 if (!test_and_set_bit(DQ_MOD_B, &dquot->dq_flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 list_add(&dquot->dq_dirty, &sb_dqopt(dquot->dq_sb)->
334 info[dquot->dq_type].dqi_dirty_list);
Dmitry Monakhoveabf2902010-03-27 15:15:38 +0300335 ret = 0;
336 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 spin_unlock(&dq_list_lock);
Dmitry Monakhoveabf2902010-03-27 15:15:38 +0300338 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339}
Mingming Cao08d03502009-01-14 16:19:32 +0100340EXPORT_SYMBOL(dquot_mark_dquot_dirty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341
Dmitry Monakhovdc52dd32009-12-14 15:21:15 +0300342/* Dirtify all the dquots - this can block when journalling */
343static inline int mark_all_dquot_dirty(struct dquot * const *dquot)
344{
345 int ret, err, cnt;
346
347 ret = err = 0;
348 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
349 if (dquot[cnt])
350 /* Even in case of error we have to continue */
351 ret = mark_dquot_dirty(dquot[cnt]);
352 if (!err)
353 err = ret;
354 }
355 return err;
356}
357
358static inline void dqput_all(struct dquot **dquot)
359{
360 unsigned int cnt;
361
362 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
363 dqput(dquot[cnt]);
364}
365
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366/* This function needs dq_list_lock */
367static inline int clear_dquot_dirty(struct dquot *dquot)
368{
369 if (!test_and_clear_bit(DQ_MOD_B, &dquot->dq_flags))
370 return 0;
371 list_del_init(&dquot->dq_dirty);
372 return 1;
373}
374
375void mark_info_dirty(struct super_block *sb, int type)
376{
377 set_bit(DQF_INFO_DIRTY_B, &sb_dqopt(sb)->info[type].dqi_flags);
378}
379EXPORT_SYMBOL(mark_info_dirty);
380
381/*
382 * Read dquot from disk and alloc space for it
383 */
384
385int dquot_acquire(struct dquot *dquot)
386{
387 int ret = 0, ret2 = 0;
388 struct quota_info *dqopt = sb_dqopt(dquot->dq_sb);
389
Ingo Molnard3be9152006-03-23 03:00:29 -0800390 mutex_lock(&dquot->dq_lock);
391 mutex_lock(&dqopt->dqio_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 if (!test_bit(DQ_READ_B, &dquot->dq_flags))
393 ret = dqopt->ops[dquot->dq_type]->read_dqblk(dquot);
394 if (ret < 0)
395 goto out_iolock;
396 set_bit(DQ_READ_B, &dquot->dq_flags);
397 /* Instantiate dquot if needed */
398 if (!test_bit(DQ_ACTIVE_B, &dquot->dq_flags) && !dquot->dq_off) {
399 ret = dqopt->ops[dquot->dq_type]->commit_dqblk(dquot);
400 /* Write the info if needed */
Jan Kara268157b2009-01-27 15:47:22 +0100401 if (info_dirty(&dqopt->info[dquot->dq_type])) {
402 ret2 = dqopt->ops[dquot->dq_type]->write_file_info(
403 dquot->dq_sb, dquot->dq_type);
404 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 if (ret < 0)
406 goto out_iolock;
407 if (ret2 < 0) {
408 ret = ret2;
409 goto out_iolock;
410 }
411 }
412 set_bit(DQ_ACTIVE_B, &dquot->dq_flags);
413out_iolock:
Ingo Molnard3be9152006-03-23 03:00:29 -0800414 mutex_unlock(&dqopt->dqio_mutex);
415 mutex_unlock(&dquot->dq_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 return ret;
417}
Mingming Cao08d03502009-01-14 16:19:32 +0100418EXPORT_SYMBOL(dquot_acquire);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419
420/*
421 * Write dquot to disk
422 */
423int dquot_commit(struct dquot *dquot)
424{
425 int ret = 0, ret2 = 0;
426 struct quota_info *dqopt = sb_dqopt(dquot->dq_sb);
427
Ingo Molnard3be9152006-03-23 03:00:29 -0800428 mutex_lock(&dqopt->dqio_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 spin_lock(&dq_list_lock);
430 if (!clear_dquot_dirty(dquot)) {
431 spin_unlock(&dq_list_lock);
432 goto out_sem;
433 }
434 spin_unlock(&dq_list_lock);
435 /* Inactive dquot can be only if there was error during read/init
436 * => we have better not writing it */
437 if (test_bit(DQ_ACTIVE_B, &dquot->dq_flags)) {
438 ret = dqopt->ops[dquot->dq_type]->commit_dqblk(dquot);
Jan Kara268157b2009-01-27 15:47:22 +0100439 if (info_dirty(&dqopt->info[dquot->dq_type])) {
440 ret2 = dqopt->ops[dquot->dq_type]->write_file_info(
441 dquot->dq_sb, dquot->dq_type);
442 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 if (ret >= 0)
444 ret = ret2;
445 }
446out_sem:
Ingo Molnard3be9152006-03-23 03:00:29 -0800447 mutex_unlock(&dqopt->dqio_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 return ret;
449}
Mingming Cao08d03502009-01-14 16:19:32 +0100450EXPORT_SYMBOL(dquot_commit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451
452/*
453 * Release dquot
454 */
455int dquot_release(struct dquot *dquot)
456{
457 int ret = 0, ret2 = 0;
458 struct quota_info *dqopt = sb_dqopt(dquot->dq_sb);
459
Ingo Molnard3be9152006-03-23 03:00:29 -0800460 mutex_lock(&dquot->dq_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 /* Check whether we are not racing with some other dqget() */
462 if (atomic_read(&dquot->dq_count) > 1)
463 goto out_dqlock;
Ingo Molnard3be9152006-03-23 03:00:29 -0800464 mutex_lock(&dqopt->dqio_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 if (dqopt->ops[dquot->dq_type]->release_dqblk) {
466 ret = dqopt->ops[dquot->dq_type]->release_dqblk(dquot);
467 /* Write the info */
Jan Kara268157b2009-01-27 15:47:22 +0100468 if (info_dirty(&dqopt->info[dquot->dq_type])) {
469 ret2 = dqopt->ops[dquot->dq_type]->write_file_info(
470 dquot->dq_sb, dquot->dq_type);
471 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 if (ret >= 0)
473 ret = ret2;
474 }
475 clear_bit(DQ_ACTIVE_B, &dquot->dq_flags);
Ingo Molnard3be9152006-03-23 03:00:29 -0800476 mutex_unlock(&dqopt->dqio_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477out_dqlock:
Ingo Molnard3be9152006-03-23 03:00:29 -0800478 mutex_unlock(&dquot->dq_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 return ret;
480}
Mingming Cao08d03502009-01-14 16:19:32 +0100481EXPORT_SYMBOL(dquot_release);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482
Jan Kara7d9056b2008-11-25 15:31:32 +0100483void dquot_destroy(struct dquot *dquot)
Jan Kara74f783a2008-08-19 14:51:22 +0200484{
485 kmem_cache_free(dquot_cachep, dquot);
486}
Jan Kara7d9056b2008-11-25 15:31:32 +0100487EXPORT_SYMBOL(dquot_destroy);
Jan Kara74f783a2008-08-19 14:51:22 +0200488
489static inline void do_destroy_dquot(struct dquot *dquot)
490{
491 dquot->dq_sb->dq_op->destroy_dquot(dquot);
492}
493
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494/* Invalidate all dquots on the list. Note that this function is called after
495 * quota is disabled and pointers from inodes removed so there cannot be new
Jan Kara6362e4d2006-03-23 03:00:17 -0800496 * quota users. There can still be some users of quotas due to inodes being
497 * just deleted or pruned by prune_icache() (those are not attached to any
Jan Karacc334122009-01-12 17:23:05 +0100498 * list) or parallel quotactl call. We have to wait for such users.
Jan Kara6362e4d2006-03-23 03:00:17 -0800499 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500static void invalidate_dquots(struct super_block *sb, int type)
501{
Domen Puncerc33ed272005-06-25 14:59:36 -0700502 struct dquot *dquot, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503
Jan Kara6362e4d2006-03-23 03:00:17 -0800504restart:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 spin_lock(&dq_list_lock);
Domen Puncerc33ed272005-06-25 14:59:36 -0700506 list_for_each_entry_safe(dquot, tmp, &inuse_list, dq_inuse) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 if (dquot->dq_sb != sb)
508 continue;
509 if (dquot->dq_type != type)
510 continue;
Jan Kara6362e4d2006-03-23 03:00:17 -0800511 /* Wait for dquot users */
512 if (atomic_read(&dquot->dq_count)) {
513 DEFINE_WAIT(wait);
514
515 atomic_inc(&dquot->dq_count);
516 prepare_to_wait(&dquot->dq_wait_unused, &wait,
517 TASK_UNINTERRUPTIBLE);
518 spin_unlock(&dq_list_lock);
519 /* Once dqput() wakes us up, we know it's time to free
520 * the dquot.
521 * IMPORTANT: we rely on the fact that there is always
522 * at most one process waiting for dquot to free.
523 * Otherwise dq_count would be > 1 and we would never
524 * wake up.
525 */
526 if (atomic_read(&dquot->dq_count) > 1)
527 schedule();
528 finish_wait(&dquot->dq_wait_unused, &wait);
529 dqput(dquot);
530 /* At this moment dquot() need not exist (it could be
531 * reclaimed by prune_dqcache(). Hence we must
532 * restart. */
533 goto restart;
534 }
535 /*
536 * Quota now has no users and it has been written on last
537 * dqput()
538 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 remove_dquot_hash(dquot);
540 remove_free_dquot(dquot);
541 remove_inuse(dquot);
Jan Kara74f783a2008-08-19 14:51:22 +0200542 do_destroy_dquot(dquot);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 }
544 spin_unlock(&dq_list_lock);
545}
546
Jan Kara12c77522008-10-20 17:05:00 +0200547/* Call callback for every active dquot on given filesystem */
548int dquot_scan_active(struct super_block *sb,
549 int (*fn)(struct dquot *dquot, unsigned long priv),
550 unsigned long priv)
551{
552 struct dquot *dquot, *old_dquot = NULL;
553 int ret = 0;
554
555 mutex_lock(&sb_dqopt(sb)->dqonoff_mutex);
556 spin_lock(&dq_list_lock);
557 list_for_each_entry(dquot, &inuse_list, dq_inuse) {
558 if (!test_bit(DQ_ACTIVE_B, &dquot->dq_flags))
559 continue;
560 if (dquot->dq_sb != sb)
561 continue;
562 /* Now we have active dquot so we can just increase use count */
563 atomic_inc(&dquot->dq_count);
Jan Kara12c77522008-10-20 17:05:00 +0200564 spin_unlock(&dq_list_lock);
Dmitry Monakhovdde95882010-04-26 20:03:33 +0400565 dqstats_inc(DQST_LOOKUPS);
Jan Kara12c77522008-10-20 17:05:00 +0200566 dqput(old_dquot);
567 old_dquot = dquot;
568 ret = fn(dquot, priv);
569 if (ret < 0)
570 goto out;
571 spin_lock(&dq_list_lock);
572 /* We are safe to continue now because our dquot could not
573 * be moved out of the inuse list while we hold the reference */
574 }
575 spin_unlock(&dq_list_lock);
576out:
577 dqput(old_dquot);
578 mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
579 return ret;
580}
Mingming Cao08d03502009-01-14 16:19:32 +0100581EXPORT_SYMBOL(dquot_scan_active);
Jan Kara12c77522008-10-20 17:05:00 +0200582
Christoph Hellwig287a8092010-05-19 07:16:45 -0400583int dquot_quota_sync(struct super_block *sb, int type, int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584{
585 struct list_head *dirty;
586 struct dquot *dquot;
587 struct quota_info *dqopt = sb_dqopt(sb);
588 int cnt;
589
Ingo Molnard3be9152006-03-23 03:00:29 -0800590 mutex_lock(&dqopt->dqonoff_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
592 if (type != -1 && cnt != type)
593 continue;
Jan Karaf55abc02008-08-20 17:50:32 +0200594 if (!sb_has_quota_active(sb, cnt))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 continue;
596 spin_lock(&dq_list_lock);
597 dirty = &dqopt->info[cnt].dqi_dirty_list;
598 while (!list_empty(dirty)) {
Jan Kara268157b2009-01-27 15:47:22 +0100599 dquot = list_first_entry(dirty, struct dquot,
600 dq_dirty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 /* Dirty and inactive can be only bad dquot... */
602 if (!test_bit(DQ_ACTIVE_B, &dquot->dq_flags)) {
603 clear_dquot_dirty(dquot);
604 continue;
605 }
606 /* Now we have active dquot from which someone is
607 * holding reference so we can safely just increase
608 * use count */
609 atomic_inc(&dquot->dq_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 spin_unlock(&dq_list_lock);
Dmitry Monakhovdde95882010-04-26 20:03:33 +0400611 dqstats_inc(DQST_LOOKUPS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 sb->dq_op->write_dquot(dquot);
613 dqput(dquot);
614 spin_lock(&dq_list_lock);
615 }
616 spin_unlock(&dq_list_lock);
617 }
618
619 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
Jan Karaf55abc02008-08-20 17:50:32 +0200620 if ((cnt == type || type == -1) && sb_has_quota_active(sb, cnt)
621 && info_dirty(&dqopt->info[cnt]))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 sb->dq_op->write_info(sb, cnt);
Dmitry Monakhovdde95882010-04-26 20:03:33 +0400623 dqstats_inc(DQST_SYNCS);
Ingo Molnard3be9152006-03-23 03:00:29 -0800624 mutex_unlock(&dqopt->dqonoff_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625
Christoph Hellwig5fb324a2010-02-16 03:44:52 -0500626 if (!wait || (sb_dqopt(sb)->flags & DQUOT_QUOTA_SYS_FILE))
627 return 0;
628
629 /* This is not very clever (and fast) but currently I don't know about
630 * any other simple way of getting quota data to disk and we must get
631 * them there for userspace to be visible... */
632 if (sb->s_op->sync_fs)
633 sb->s_op->sync_fs(sb, 1);
634 sync_blockdev(sb->s_bdev);
635
636 /*
637 * Now when everything is written we can discard the pagecache so
638 * that userspace sees the changes.
639 */
640 mutex_lock(&sb_dqopt(sb)->dqonoff_mutex);
641 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
642 if (type != -1 && cnt != type)
643 continue;
644 if (!sb_has_quota_active(sb, cnt))
645 continue;
646 mutex_lock_nested(&sb_dqopt(sb)->files[cnt]->i_mutex,
647 I_MUTEX_QUOTA);
648 truncate_inode_pages(&sb_dqopt(sb)->files[cnt]->i_data, 0);
649 mutex_unlock(&sb_dqopt(sb)->files[cnt]->i_mutex);
650 }
651 mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
652
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 return 0;
654}
Christoph Hellwig287a8092010-05-19 07:16:45 -0400655EXPORT_SYMBOL(dquot_quota_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656
657/* Free unused dquots from cache */
658static void prune_dqcache(int count)
659{
660 struct list_head *head;
661 struct dquot *dquot;
662
663 head = free_dquots.prev;
664 while (head != &free_dquots && count) {
665 dquot = list_entry(head, struct dquot, dq_free);
666 remove_dquot_hash(dquot);
667 remove_free_dquot(dquot);
668 remove_inuse(dquot);
Jan Kara74f783a2008-08-19 14:51:22 +0200669 do_destroy_dquot(dquot);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 count--;
671 head = free_dquots.prev;
672 }
673}
674
675/*
676 * This is called from kswapd when we think we need some
677 * more memory
678 */
Al Viro27496a82005-10-21 03:20:48 -0400679static int shrink_dqcache_memory(int nr, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680{
681 if (nr) {
682 spin_lock(&dq_list_lock);
683 prune_dqcache(nr);
684 spin_unlock(&dq_list_lock);
685 }
Dmitry Monakhovf32764b2010-05-26 23:21:58 +0200686 return ((unsigned)
687 percpu_counter_read_positive(&dqstats.counter[DQST_FREE_DQUOTS])
688 /100) * sysctl_vfs_cache_pressure;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689}
690
Rusty Russell8e1f9362007-07-17 04:03:17 -0700691static struct shrinker dqcache_shrinker = {
692 .shrink = shrink_dqcache_memory,
693 .seeks = DEFAULT_SEEKS,
694};
695
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696/*
697 * Put reference to dquot
698 * NOTE: If you change this function please check whether dqput_blocks() works right...
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 */
Jan Kara3d9ea252008-10-10 16:12:23 +0200700void dqput(struct dquot *dquot)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701{
Jan Karab48d3802008-07-25 01:46:49 -0700702 int ret;
703
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 if (!dquot)
705 return;
Jan Kara62af9b52010-04-19 16:47:20 +0200706#ifdef CONFIG_QUOTA_DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 if (!atomic_read(&dquot->dq_count)) {
708 printk("VFS: dqput: trying to free free dquot\n");
709 printk("VFS: device %s, dquot of %s %d\n",
710 dquot->dq_sb->s_id,
711 quotatypes[dquot->dq_type],
712 dquot->dq_id);
713 BUG();
714 }
715#endif
Dmitry Monakhovdde95882010-04-26 20:03:33 +0400716 dqstats_inc(DQST_DROPS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717we_slept:
718 spin_lock(&dq_list_lock);
719 if (atomic_read(&dquot->dq_count) > 1) {
720 /* We have more than one user... nothing to do */
721 atomic_dec(&dquot->dq_count);
Jan Kara6362e4d2006-03-23 03:00:17 -0800722 /* Releasing dquot during quotaoff phase? */
Jan Karaf55abc02008-08-20 17:50:32 +0200723 if (!sb_has_quota_active(dquot->dq_sb, dquot->dq_type) &&
Jan Kara6362e4d2006-03-23 03:00:17 -0800724 atomic_read(&dquot->dq_count) == 1)
725 wake_up(&dquot->dq_wait_unused);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 spin_unlock(&dq_list_lock);
727 return;
728 }
729 /* Need to release dquot? */
730 if (test_bit(DQ_ACTIVE_B, &dquot->dq_flags) && dquot_dirty(dquot)) {
731 spin_unlock(&dq_list_lock);
732 /* Commit dquot before releasing */
Jan Karab48d3802008-07-25 01:46:49 -0700733 ret = dquot->dq_sb->dq_op->write_dquot(dquot);
734 if (ret < 0) {
735 printk(KERN_ERR "VFS: cannot write quota structure on "
736 "device %s (error %d). Quota may get out of "
737 "sync!\n", dquot->dq_sb->s_id, ret);
738 /*
739 * We clear dirty bit anyway, so that we avoid
740 * infinite loop here
741 */
742 spin_lock(&dq_list_lock);
743 clear_dquot_dirty(dquot);
744 spin_unlock(&dq_list_lock);
745 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 goto we_slept;
747 }
748 /* Clear flag in case dquot was inactive (something bad happened) */
749 clear_dquot_dirty(dquot);
750 if (test_bit(DQ_ACTIVE_B, &dquot->dq_flags)) {
751 spin_unlock(&dq_list_lock);
752 dquot->dq_sb->dq_op->release_dquot(dquot);
753 goto we_slept;
754 }
755 atomic_dec(&dquot->dq_count);
Jan Kara62af9b52010-04-19 16:47:20 +0200756#ifdef CONFIG_QUOTA_DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 /* sanity check */
Eric Sesterhenn8abf6a42006-04-02 13:36:13 +0200758 BUG_ON(!list_empty(&dquot->dq_free));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759#endif
760 put_dquot_last(dquot);
761 spin_unlock(&dq_list_lock);
762}
Mingming Cao08d03502009-01-14 16:19:32 +0100763EXPORT_SYMBOL(dqput);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764
Jan Kara7d9056b2008-11-25 15:31:32 +0100765struct dquot *dquot_alloc(struct super_block *sb, int type)
Jan Kara74f783a2008-08-19 14:51:22 +0200766{
767 return kmem_cache_zalloc(dquot_cachep, GFP_NOFS);
768}
Jan Kara7d9056b2008-11-25 15:31:32 +0100769EXPORT_SYMBOL(dquot_alloc);
Jan Kara74f783a2008-08-19 14:51:22 +0200770
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771static struct dquot *get_empty_dquot(struct super_block *sb, int type)
772{
773 struct dquot *dquot;
774
Jan Kara74f783a2008-08-19 14:51:22 +0200775 dquot = sb->dq_op->alloc_dquot(sb, type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 if(!dquot)
Jan Karadd6f3c62009-01-26 16:01:43 +0100777 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778
Ingo Molnard3be9152006-03-23 03:00:29 -0800779 mutex_init(&dquot->dq_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 INIT_LIST_HEAD(&dquot->dq_free);
781 INIT_LIST_HEAD(&dquot->dq_inuse);
782 INIT_HLIST_NODE(&dquot->dq_hash);
783 INIT_LIST_HEAD(&dquot->dq_dirty);
Jan Kara6362e4d2006-03-23 03:00:17 -0800784 init_waitqueue_head(&dquot->dq_wait_unused);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 dquot->dq_sb = sb;
786 dquot->dq_type = type;
787 atomic_set(&dquot->dq_count, 1);
788
789 return dquot;
790}
791
792/*
793 * Get reference to dquot
Jan Karacc334122009-01-12 17:23:05 +0100794 *
795 * Locking is slightly tricky here. We are guarded from parallel quotaoff()
796 * destroying our dquot by:
797 * a) checking for quota flags under dq_list_lock and
798 * b) getting a reference to dquot before we release dq_list_lock
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 */
Jan Kara3d9ea252008-10-10 16:12:23 +0200800struct dquot *dqget(struct super_block *sb, unsigned int id, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801{
802 unsigned int hashent = hashfn(sb, id, type);
Jan Karadd6f3c62009-01-26 16:01:43 +0100803 struct dquot *dquot = NULL, *empty = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804
Jan Karaf55abc02008-08-20 17:50:32 +0200805 if (!sb_has_quota_active(sb, type))
Jan Karadd6f3c62009-01-26 16:01:43 +0100806 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807we_slept:
808 spin_lock(&dq_list_lock);
Jan Karacc334122009-01-12 17:23:05 +0100809 spin_lock(&dq_state_lock);
810 if (!sb_has_quota_active(sb, type)) {
811 spin_unlock(&dq_state_lock);
812 spin_unlock(&dq_list_lock);
813 goto out;
814 }
815 spin_unlock(&dq_state_lock);
816
Jan Karadd6f3c62009-01-26 16:01:43 +0100817 dquot = find_dquot(hashent, sb, id, type);
818 if (!dquot) {
819 if (!empty) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 spin_unlock(&dq_list_lock);
Jan Karadd6f3c62009-01-26 16:01:43 +0100821 empty = get_empty_dquot(sb, type);
822 if (!empty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 schedule(); /* Try to wait for a moment... */
824 goto we_slept;
825 }
826 dquot = empty;
Jan Karadd6f3c62009-01-26 16:01:43 +0100827 empty = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 dquot->dq_id = id;
829 /* all dquots go on the inuse_list */
830 put_inuse(dquot);
831 /* hash it first so it can be found */
832 insert_dquot_hash(dquot);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 spin_unlock(&dq_list_lock);
Dmitry Monakhovdde95882010-04-26 20:03:33 +0400834 dqstats_inc(DQST_LOOKUPS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 } else {
836 if (!atomic_read(&dquot->dq_count))
837 remove_free_dquot(dquot);
838 atomic_inc(&dquot->dq_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 spin_unlock(&dq_list_lock);
Dmitry Monakhovdde95882010-04-26 20:03:33 +0400840 dqstats_inc(DQST_CACHE_HITS);
841 dqstats_inc(DQST_LOOKUPS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 }
Jan Kara268157b2009-01-27 15:47:22 +0100843 /* Wait for dq_lock - after this we know that either dquot_release() is
844 * already finished or it will be canceled due to dq_count > 1 test */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 wait_on_dquot(dquot);
Jan Kara268157b2009-01-27 15:47:22 +0100846 /* Read the dquot / allocate space in quota file */
847 if (!test_bit(DQ_ACTIVE_B, &dquot->dq_flags) &&
848 sb->dq_op->acquire_dquot(dquot) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 dqput(dquot);
Jan Karadd6f3c62009-01-26 16:01:43 +0100850 dquot = NULL;
Jan Karacc334122009-01-12 17:23:05 +0100851 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 }
Jan Kara62af9b52010-04-19 16:47:20 +0200853#ifdef CONFIG_QUOTA_DEBUG
Eric Sesterhenn8abf6a42006-04-02 13:36:13 +0200854 BUG_ON(!dquot->dq_sb); /* Has somebody invalidated entry under us? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855#endif
Jan Karacc334122009-01-12 17:23:05 +0100856out:
857 if (empty)
858 do_destroy_dquot(empty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859
860 return dquot;
861}
Mingming Cao08d03502009-01-14 16:19:32 +0100862EXPORT_SYMBOL(dqget);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863
864static int dqinit_needed(struct inode *inode, int type)
865{
866 int cnt;
867
868 if (IS_NOQUOTA(inode))
869 return 0;
870 if (type != -1)
Jan Karadd6f3c62009-01-26 16:01:43 +0100871 return !inode->i_dquot[type];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
Jan Karadd6f3c62009-01-26 16:01:43 +0100873 if (!inode->i_dquot[cnt])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 return 1;
875 return 0;
876}
877
Ingo Molnard3be9152006-03-23 03:00:29 -0800878/* This routine is guarded by dqonoff_mutex mutex */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879static void add_dquot_ref(struct super_block *sb, int type)
880{
Jan Kara941d2382008-02-06 01:37:36 -0800881 struct inode *inode, *old_inode = NULL;
Jan Kara62af9b52010-04-19 16:47:20 +0200882#ifdef CONFIG_QUOTA_DEBUG
Jan Kara0a5a9c72010-02-09 18:20:39 +0100883 int reserved = 0;
Jan Kara4c5e6c02010-04-06 18:52:47 +0200884#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885
Christoph Hellwigd003fb72007-02-12 00:51:58 -0800886 spin_lock(&inode_lock);
887 list_for_each_entry(inode, &sb->s_inodes, i_sb_list) {
Wu Fengguangb6fac632009-04-02 16:56:34 -0700888 if (inode->i_state & (I_FREEING|I_CLEAR|I_WILL_FREE|I_NEW))
Nick Pigginaabb8fd2009-03-11 13:17:36 -0700889 continue;
Jan Kara62af9b52010-04-19 16:47:20 +0200890#ifdef CONFIG_QUOTA_DEBUG
Jan Kara0a5a9c72010-02-09 18:20:39 +0100891 if (unlikely(inode_get_rsv_space(inode) > 0))
892 reserved = 1;
Jan Kara4c5e6c02010-04-06 18:52:47 +0200893#endif
Christoph Hellwigd003fb72007-02-12 00:51:58 -0800894 if (!atomic_read(&inode->i_writecount))
895 continue;
896 if (!dqinit_needed(inode, type))
897 continue;
Christoph Hellwigd003fb72007-02-12 00:51:58 -0800898
899 __iget(inode);
900 spin_unlock(&inode_lock);
901
Jan Kara941d2382008-02-06 01:37:36 -0800902 iput(old_inode);
Christoph Hellwig871a2932010-03-03 09:05:07 -0500903 __dquot_initialize(inode, type);
Jan Kara941d2382008-02-06 01:37:36 -0800904 /* We hold a reference to 'inode' so it couldn't have been
905 * removed from s_inodes list while we dropped the inode_lock.
906 * We cannot iput the inode now as we can be holding the last
907 * reference and we cannot iput it under inode_lock. So we
908 * keep the reference and iput it later. */
909 old_inode = inode;
910 spin_lock(&inode_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 }
Christoph Hellwigd003fb72007-02-12 00:51:58 -0800912 spin_unlock(&inode_lock);
Jan Kara941d2382008-02-06 01:37:36 -0800913 iput(old_inode);
Jan Kara0a5a9c72010-02-09 18:20:39 +0100914
Jan Kara62af9b52010-04-19 16:47:20 +0200915#ifdef CONFIG_QUOTA_DEBUG
Jan Kara0a5a9c72010-02-09 18:20:39 +0100916 if (reserved) {
917 printk(KERN_WARNING "VFS (%s): Writes happened before quota"
918 " was turned on thus quota information is probably "
919 "inconsistent. Please run quotacheck(8).\n", sb->s_id);
920 }
Jan Kara4c5e6c02010-04-06 18:52:47 +0200921#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922}
923
Jan Kara268157b2009-01-27 15:47:22 +0100924/*
925 * Return 0 if dqput() won't block.
926 * (note that 1 doesn't necessarily mean blocking)
927 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928static inline int dqput_blocks(struct dquot *dquot)
929{
930 if (atomic_read(&dquot->dq_count) <= 1)
931 return 1;
932 return 0;
933}
934
Jan Kara268157b2009-01-27 15:47:22 +0100935/*
936 * Remove references to dquots from inode and add dquot to list for freeing
937 * if we have the last referece to dquot
938 * We can't race with anybody because we hold dqptr_sem for writing...
939 */
Adrian Bunke5f00f42007-05-08 00:27:22 -0700940static int remove_inode_dquot_ref(struct inode *inode, int type,
941 struct list_head *tofree_head)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942{
943 struct dquot *dquot = inode->i_dquot[type];
944
Jan Karadd6f3c62009-01-26 16:01:43 +0100945 inode->i_dquot[type] = NULL;
946 if (dquot) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 if (dqput_blocks(dquot)) {
Jan Kara62af9b52010-04-19 16:47:20 +0200948#ifdef CONFIG_QUOTA_DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 if (atomic_read(&dquot->dq_count) != 1)
950 printk(KERN_WARNING "VFS: Adding dquot with dq_count %d to dispose list.\n", atomic_read(&dquot->dq_count));
951#endif
952 spin_lock(&dq_list_lock);
Jan Kara268157b2009-01-27 15:47:22 +0100953 /* As dquot must have currently users it can't be on
954 * the free list... */
955 list_add(&dquot->dq_free, tofree_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 spin_unlock(&dq_list_lock);
957 return 1;
958 }
959 else
960 dqput(dquot); /* We have guaranteed we won't block */
961 }
962 return 0;
963}
964
Jan Kara268157b2009-01-27 15:47:22 +0100965/*
966 * Free list of dquots
967 * Dquots are removed from inodes and no new references can be got so we are
968 * the only ones holding reference
969 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970static void put_dquot_list(struct list_head *tofree_head)
971{
972 struct list_head *act_head;
973 struct dquot *dquot;
974
975 act_head = tofree_head->next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 while (act_head != tofree_head) {
977 dquot = list_entry(act_head, struct dquot, dq_free);
978 act_head = act_head->next;
Jan Kara268157b2009-01-27 15:47:22 +0100979 /* Remove dquot from the list so we won't have problems... */
980 list_del_init(&dquot->dq_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 dqput(dquot);
982 }
983}
984
Christoph Hellwigfb58b732007-02-12 00:51:57 -0800985static void remove_dquot_ref(struct super_block *sb, int type,
986 struct list_head *tofree_head)
987{
988 struct inode *inode;
Dmitry Monakhov7af9cce2010-06-01 11:39:48 +0400989 int reserved = 0;
Christoph Hellwigfb58b732007-02-12 00:51:57 -0800990
991 spin_lock(&inode_lock);
992 list_for_each_entry(inode, &sb->s_inodes, i_sb_list) {
Nick Pigginaabb8fd2009-03-11 13:17:36 -0700993 /*
994 * We have to scan also I_NEW inodes because they can already
995 * have quota pointer initialized. Luckily, we need to touch
996 * only quota pointers and these have separate locking
997 * (dqptr_sem).
998 */
Dmitry Monakhov7af9cce2010-06-01 11:39:48 +0400999 if (!IS_NOQUOTA(inode)) {
1000 if (unlikely(inode_get_rsv_space(inode) > 0))
1001 reserved = 1;
Christoph Hellwigfb58b732007-02-12 00:51:57 -08001002 remove_inode_dquot_ref(inode, type, tofree_head);
Dmitry Monakhov7af9cce2010-06-01 11:39:48 +04001003 }
Christoph Hellwigfb58b732007-02-12 00:51:57 -08001004 }
1005 spin_unlock(&inode_lock);
Dmitry Monakhov7af9cce2010-06-01 11:39:48 +04001006#ifdef CONFIG_QUOTA_DEBUG
1007 if (reserved) {
1008 printk(KERN_WARNING "VFS (%s): Writes happened after quota"
1009 " was disabled thus quota information is probably "
1010 "inconsistent. Please run quotacheck(8).\n", sb->s_id);
1011 }
1012#endif
Christoph Hellwigfb58b732007-02-12 00:51:57 -08001013}
1014
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015/* Gather all references from inodes and drop them */
1016static void drop_dquot_ref(struct super_block *sb, int type)
1017{
1018 LIST_HEAD(tofree_head);
1019
Christoph Hellwigfb58b732007-02-12 00:51:57 -08001020 if (sb->dq_op) {
1021 down_write(&sb_dqopt(sb)->dqptr_sem);
1022 remove_dquot_ref(sb, type, &tofree_head);
1023 up_write(&sb_dqopt(sb)->dqptr_sem);
1024 put_dquot_list(&tofree_head);
1025 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026}
1027
Jan Kara12095462008-08-20 14:45:12 +02001028static inline void dquot_incr_inodes(struct dquot *dquot, qsize_t number)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029{
1030 dquot->dq_dqb.dqb_curinodes += number;
1031}
1032
1033static inline void dquot_incr_space(struct dquot *dquot, qsize_t number)
1034{
1035 dquot->dq_dqb.dqb_curspace += number;
1036}
1037
Mingming Caof18df222009-01-13 16:43:09 +01001038static inline void dquot_resv_space(struct dquot *dquot, qsize_t number)
1039{
1040 dquot->dq_dqb.dqb_rsvspace += number;
1041}
1042
Mingming Cao740d9dc2009-01-13 16:43:14 +01001043/*
1044 * Claim reserved quota space
1045 */
Jan Kara0a5a9c72010-02-09 18:20:39 +01001046static void dquot_claim_reserved_space(struct dquot *dquot, qsize_t number)
Mingming Cao740d9dc2009-01-13 16:43:14 +01001047{
Jan Kara0a5a9c72010-02-09 18:20:39 +01001048 if (dquot->dq_dqb.dqb_rsvspace < number) {
1049 WARN_ON_ONCE(1);
1050 number = dquot->dq_dqb.dqb_rsvspace;
1051 }
Mingming Cao740d9dc2009-01-13 16:43:14 +01001052 dquot->dq_dqb.dqb_curspace += number;
1053 dquot->dq_dqb.dqb_rsvspace -= number;
1054}
1055
1056static inline
1057void dquot_free_reserved_space(struct dquot *dquot, qsize_t number)
1058{
Jan Kara0a5a9c72010-02-09 18:20:39 +01001059 if (dquot->dq_dqb.dqb_rsvspace >= number)
1060 dquot->dq_dqb.dqb_rsvspace -= number;
1061 else {
1062 WARN_ON_ONCE(1);
1063 dquot->dq_dqb.dqb_rsvspace = 0;
1064 }
Mingming Cao740d9dc2009-01-13 16:43:14 +01001065}
1066
Jan Kara7a2435d2009-01-27 01:47:11 +01001067static void dquot_decr_inodes(struct dquot *dquot, qsize_t number)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068{
Jan Karadb49d2d2008-10-01 18:21:39 +02001069 if (sb_dqopt(dquot->dq_sb)->flags & DQUOT_NEGATIVE_USAGE ||
1070 dquot->dq_dqb.dqb_curinodes >= number)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 dquot->dq_dqb.dqb_curinodes -= number;
1072 else
1073 dquot->dq_dqb.dqb_curinodes = 0;
1074 if (dquot->dq_dqb.dqb_curinodes <= dquot->dq_dqb.dqb_isoftlimit)
1075 dquot->dq_dqb.dqb_itime = (time_t) 0;
1076 clear_bit(DQ_INODES_B, &dquot->dq_flags);
1077}
1078
Jan Kara7a2435d2009-01-27 01:47:11 +01001079static void dquot_decr_space(struct dquot *dquot, qsize_t number)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080{
Jan Karadb49d2d2008-10-01 18:21:39 +02001081 if (sb_dqopt(dquot->dq_sb)->flags & DQUOT_NEGATIVE_USAGE ||
1082 dquot->dq_dqb.dqb_curspace >= number)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 dquot->dq_dqb.dqb_curspace -= number;
1084 else
1085 dquot->dq_dqb.dqb_curspace = 0;
Jan Kara12095462008-08-20 14:45:12 +02001086 if (dquot->dq_dqb.dqb_curspace <= dquot->dq_dqb.dqb_bsoftlimit)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 dquot->dq_dqb.dqb_btime = (time_t) 0;
1088 clear_bit(DQ_BLKS_B, &dquot->dq_flags);
1089}
1090
Jan Karac5254602007-12-22 14:03:25 -08001091static int warning_issued(struct dquot *dquot, const int warntype)
1092{
1093 int flag = (warntype == QUOTA_NL_BHARDWARN ||
1094 warntype == QUOTA_NL_BSOFTLONGWARN) ? DQ_BLKS_B :
1095 ((warntype == QUOTA_NL_IHARDWARN ||
1096 warntype == QUOTA_NL_ISOFTLONGWARN) ? DQ_INODES_B : 0);
1097
1098 if (!flag)
1099 return 0;
1100 return test_and_set_bit(flag, &dquot->dq_flags);
1101}
1102
Jan Kara8e893462007-10-16 23:29:31 -07001103#ifdef CONFIG_PRINT_QUOTA_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104static int flag_print_warnings = 1;
1105
Jan Kara7a2435d2009-01-27 01:47:11 +01001106static int need_print_warning(struct dquot *dquot)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107{
1108 if (!flag_print_warnings)
1109 return 0;
1110
1111 switch (dquot->dq_type) {
1112 case USRQUOTA:
David Howellsda9592e2008-11-14 10:39:05 +11001113 return current_fsuid() == dquot->dq_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 case GRPQUOTA:
1115 return in_group_p(dquot->dq_id);
1116 }
1117 return 0;
1118}
1119
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120/* Print warning to user which exceeded quota */
Jan Karac5254602007-12-22 14:03:25 -08001121static void print_warning(struct dquot *dquot, const int warntype)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122{
1123 char *msg = NULL;
Peter Zijlstra24ec8392006-12-08 02:36:04 -08001124 struct tty_struct *tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125
Jan Kara657d3bf2008-07-25 01:46:52 -07001126 if (warntype == QUOTA_NL_IHARDBELOW ||
1127 warntype == QUOTA_NL_ISOFTBELOW ||
1128 warntype == QUOTA_NL_BHARDBELOW ||
1129 warntype == QUOTA_NL_BSOFTBELOW || !need_print_warning(dquot))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 return;
1131
Peter Zijlstra24ec8392006-12-08 02:36:04 -08001132 tty = get_current_tty();
1133 if (!tty)
Alan Cox452a00d2008-10-13 10:39:13 +01001134 return;
Peter Zijlstra24ec8392006-12-08 02:36:04 -08001135 tty_write_message(tty, dquot->dq_sb->s_id);
Jan Kara8e893462007-10-16 23:29:31 -07001136 if (warntype == QUOTA_NL_ISOFTWARN || warntype == QUOTA_NL_BSOFTWARN)
Peter Zijlstra24ec8392006-12-08 02:36:04 -08001137 tty_write_message(tty, ": warning, ");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 else
Peter Zijlstra24ec8392006-12-08 02:36:04 -08001139 tty_write_message(tty, ": write failed, ");
1140 tty_write_message(tty, quotatypes[dquot->dq_type]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 switch (warntype) {
Jan Kara8e893462007-10-16 23:29:31 -07001142 case QUOTA_NL_IHARDWARN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 msg = " file limit reached.\r\n";
1144 break;
Jan Kara8e893462007-10-16 23:29:31 -07001145 case QUOTA_NL_ISOFTLONGWARN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 msg = " file quota exceeded too long.\r\n";
1147 break;
Jan Kara8e893462007-10-16 23:29:31 -07001148 case QUOTA_NL_ISOFTWARN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 msg = " file quota exceeded.\r\n";
1150 break;
Jan Kara8e893462007-10-16 23:29:31 -07001151 case QUOTA_NL_BHARDWARN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 msg = " block limit reached.\r\n";
1153 break;
Jan Kara8e893462007-10-16 23:29:31 -07001154 case QUOTA_NL_BSOFTLONGWARN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 msg = " block quota exceeded too long.\r\n";
1156 break;
Jan Kara8e893462007-10-16 23:29:31 -07001157 case QUOTA_NL_BSOFTWARN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 msg = " block quota exceeded.\r\n";
1159 break;
1160 }
Peter Zijlstra24ec8392006-12-08 02:36:04 -08001161 tty_write_message(tty, msg);
Alan Cox452a00d2008-10-13 10:39:13 +01001162 tty_kref_put(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163}
Jan Kara8e893462007-10-16 23:29:31 -07001164#endif
1165
Mingming Caof18df222009-01-13 16:43:09 +01001166/*
1167 * Write warnings to the console and send warning messages over netlink.
1168 *
1169 * Note that this function can sleep.
1170 */
Jan Kara7a2435d2009-01-27 01:47:11 +01001171static void flush_warnings(struct dquot *const *dquots, char *warntype)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172{
Steven Whitehouse86e931a2009-09-28 12:35:17 +01001173 struct dquot *dq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 int i;
1175
Steven Whitehouse86e931a2009-09-28 12:35:17 +01001176 for (i = 0; i < MAXQUOTAS; i++) {
1177 dq = dquots[i];
1178 if (dq && warntype[i] != QUOTA_NL_NOWARN &&
1179 !warning_issued(dq, warntype[i])) {
Jan Kara8e893462007-10-16 23:29:31 -07001180#ifdef CONFIG_PRINT_QUOTA_WARNING
Steven Whitehouse86e931a2009-09-28 12:35:17 +01001181 print_warning(dq, warntype[i]);
Jan Kara8e893462007-10-16 23:29:31 -07001182#endif
Steven Whitehouse86e931a2009-09-28 12:35:17 +01001183 quota_send_warning(dq->dq_type, dq->dq_id,
1184 dq->dq_sb->s_dev, warntype[i]);
Jan Kara8e893462007-10-16 23:29:31 -07001185 }
Steven Whitehouse86e931a2009-09-28 12:35:17 +01001186 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187}
1188
Jan Kara7a2435d2009-01-27 01:47:11 +01001189static int ignore_hardlimit(struct dquot *dquot)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190{
1191 struct mem_dqinfo *info = &sb_dqopt(dquot->dq_sb)->info[dquot->dq_type];
1192
1193 return capable(CAP_SYS_RESOURCE) &&
Jan Kara268157b2009-01-27 15:47:22 +01001194 (info->dqi_format->qf_fmt_id != QFMT_VFS_OLD ||
1195 !(info->dqi_flags & V1_DQF_RSQUASH));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196}
1197
1198/* needs dq_data_lock */
Jan Kara12095462008-08-20 14:45:12 +02001199static int check_idq(struct dquot *dquot, qsize_t inodes, char *warntype)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200{
Jan Kara268157b2009-01-27 15:47:22 +01001201 qsize_t newinodes = dquot->dq_dqb.dqb_curinodes + inodes;
1202
Jan Kara8e893462007-10-16 23:29:31 -07001203 *warntype = QUOTA_NL_NOWARN;
Jan Karaf55abc02008-08-20 17:50:32 +02001204 if (!sb_has_quota_limits_enabled(dquot->dq_sb, dquot->dq_type) ||
1205 test_bit(DQ_FAKE_B, &dquot->dq_flags))
Christoph Hellwigefd8f0e2010-03-03 09:05:08 -05001206 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207
1208 if (dquot->dq_dqb.dqb_ihardlimit &&
Jan Kara268157b2009-01-27 15:47:22 +01001209 newinodes > dquot->dq_dqb.dqb_ihardlimit &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 !ignore_hardlimit(dquot)) {
Jan Kara8e893462007-10-16 23:29:31 -07001211 *warntype = QUOTA_NL_IHARDWARN;
Christoph Hellwigefd8f0e2010-03-03 09:05:08 -05001212 return -EDQUOT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213 }
1214
1215 if (dquot->dq_dqb.dqb_isoftlimit &&
Jan Kara268157b2009-01-27 15:47:22 +01001216 newinodes > dquot->dq_dqb.dqb_isoftlimit &&
1217 dquot->dq_dqb.dqb_itime &&
1218 get_seconds() >= dquot->dq_dqb.dqb_itime &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 !ignore_hardlimit(dquot)) {
Jan Kara8e893462007-10-16 23:29:31 -07001220 *warntype = QUOTA_NL_ISOFTLONGWARN;
Christoph Hellwigefd8f0e2010-03-03 09:05:08 -05001221 return -EDQUOT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 }
1223
1224 if (dquot->dq_dqb.dqb_isoftlimit &&
Jan Kara268157b2009-01-27 15:47:22 +01001225 newinodes > dquot->dq_dqb.dqb_isoftlimit &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 dquot->dq_dqb.dqb_itime == 0) {
Jan Kara8e893462007-10-16 23:29:31 -07001227 *warntype = QUOTA_NL_ISOFTWARN;
Jan Kara268157b2009-01-27 15:47:22 +01001228 dquot->dq_dqb.dqb_itime = get_seconds() +
1229 sb_dqopt(dquot->dq_sb)->info[dquot->dq_type].dqi_igrace;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 }
1231
Christoph Hellwigefd8f0e2010-03-03 09:05:08 -05001232 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233}
1234
1235/* needs dq_data_lock */
1236static int check_bdq(struct dquot *dquot, qsize_t space, int prealloc, char *warntype)
1237{
Mingming Caof18df222009-01-13 16:43:09 +01001238 qsize_t tspace;
Jan Kara268157b2009-01-27 15:47:22 +01001239 struct super_block *sb = dquot->dq_sb;
Mingming Caof18df222009-01-13 16:43:09 +01001240
Jan Kara8e893462007-10-16 23:29:31 -07001241 *warntype = QUOTA_NL_NOWARN;
Jan Kara268157b2009-01-27 15:47:22 +01001242 if (!sb_has_quota_limits_enabled(sb, dquot->dq_type) ||
Jan Karaf55abc02008-08-20 17:50:32 +02001243 test_bit(DQ_FAKE_B, &dquot->dq_flags))
Christoph Hellwigefd8f0e2010-03-03 09:05:08 -05001244 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245
Mingming Caof18df222009-01-13 16:43:09 +01001246 tspace = dquot->dq_dqb.dqb_curspace + dquot->dq_dqb.dqb_rsvspace
1247 + space;
1248
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 if (dquot->dq_dqb.dqb_bhardlimit &&
Mingming Caof18df222009-01-13 16:43:09 +01001250 tspace > dquot->dq_dqb.dqb_bhardlimit &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 !ignore_hardlimit(dquot)) {
1252 if (!prealloc)
Jan Kara8e893462007-10-16 23:29:31 -07001253 *warntype = QUOTA_NL_BHARDWARN;
Christoph Hellwigefd8f0e2010-03-03 09:05:08 -05001254 return -EDQUOT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 }
1256
1257 if (dquot->dq_dqb.dqb_bsoftlimit &&
Mingming Caof18df222009-01-13 16:43:09 +01001258 tspace > dquot->dq_dqb.dqb_bsoftlimit &&
Jan Kara268157b2009-01-27 15:47:22 +01001259 dquot->dq_dqb.dqb_btime &&
1260 get_seconds() >= dquot->dq_dqb.dqb_btime &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 !ignore_hardlimit(dquot)) {
1262 if (!prealloc)
Jan Kara8e893462007-10-16 23:29:31 -07001263 *warntype = QUOTA_NL_BSOFTLONGWARN;
Christoph Hellwigefd8f0e2010-03-03 09:05:08 -05001264 return -EDQUOT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 }
1266
1267 if (dquot->dq_dqb.dqb_bsoftlimit &&
Mingming Caof18df222009-01-13 16:43:09 +01001268 tspace > dquot->dq_dqb.dqb_bsoftlimit &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 dquot->dq_dqb.dqb_btime == 0) {
1270 if (!prealloc) {
Jan Kara8e893462007-10-16 23:29:31 -07001271 *warntype = QUOTA_NL_BSOFTWARN;
Jan Kara268157b2009-01-27 15:47:22 +01001272 dquot->dq_dqb.dqb_btime = get_seconds() +
1273 sb_dqopt(sb)->info[dquot->dq_type].dqi_bgrace;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 }
1275 else
1276 /*
1277 * We don't allow preallocation to exceed softlimit so exceeding will
1278 * be always printed
1279 */
Christoph Hellwigefd8f0e2010-03-03 09:05:08 -05001280 return -EDQUOT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 }
1282
Christoph Hellwigefd8f0e2010-03-03 09:05:08 -05001283 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284}
1285
Jan Kara12095462008-08-20 14:45:12 +02001286static int info_idq_free(struct dquot *dquot, qsize_t inodes)
Jan Kara657d3bf2008-07-25 01:46:52 -07001287{
Jan Kara268157b2009-01-27 15:47:22 +01001288 qsize_t newinodes;
1289
Jan Kara657d3bf2008-07-25 01:46:52 -07001290 if (test_bit(DQ_FAKE_B, &dquot->dq_flags) ||
Jan Karaf55abc02008-08-20 17:50:32 +02001291 dquot->dq_dqb.dqb_curinodes <= dquot->dq_dqb.dqb_isoftlimit ||
1292 !sb_has_quota_limits_enabled(dquot->dq_sb, dquot->dq_type))
Jan Kara657d3bf2008-07-25 01:46:52 -07001293 return QUOTA_NL_NOWARN;
1294
Jan Kara268157b2009-01-27 15:47:22 +01001295 newinodes = dquot->dq_dqb.dqb_curinodes - inodes;
1296 if (newinodes <= dquot->dq_dqb.dqb_isoftlimit)
Jan Kara657d3bf2008-07-25 01:46:52 -07001297 return QUOTA_NL_ISOFTBELOW;
1298 if (dquot->dq_dqb.dqb_curinodes >= dquot->dq_dqb.dqb_ihardlimit &&
Jan Kara268157b2009-01-27 15:47:22 +01001299 newinodes < dquot->dq_dqb.dqb_ihardlimit)
Jan Kara657d3bf2008-07-25 01:46:52 -07001300 return QUOTA_NL_IHARDBELOW;
1301 return QUOTA_NL_NOWARN;
1302}
1303
1304static int info_bdq_free(struct dquot *dquot, qsize_t space)
1305{
1306 if (test_bit(DQ_FAKE_B, &dquot->dq_flags) ||
Jan Kara12095462008-08-20 14:45:12 +02001307 dquot->dq_dqb.dqb_curspace <= dquot->dq_dqb.dqb_bsoftlimit)
Jan Kara657d3bf2008-07-25 01:46:52 -07001308 return QUOTA_NL_NOWARN;
1309
Jan Kara12095462008-08-20 14:45:12 +02001310 if (dquot->dq_dqb.dqb_curspace - space <= dquot->dq_dqb.dqb_bsoftlimit)
Jan Kara657d3bf2008-07-25 01:46:52 -07001311 return QUOTA_NL_BSOFTBELOW;
Jan Kara12095462008-08-20 14:45:12 +02001312 if (dquot->dq_dqb.dqb_curspace >= dquot->dq_dqb.dqb_bhardlimit &&
1313 dquot->dq_dqb.dqb_curspace - space < dquot->dq_dqb.dqb_bhardlimit)
Jan Kara657d3bf2008-07-25 01:46:52 -07001314 return QUOTA_NL_BHARDBELOW;
1315 return QUOTA_NL_NOWARN;
1316}
Jan Kara0a5a9c72010-02-09 18:20:39 +01001317
Christoph Hellwig189eef52010-06-04 10:56:29 +02001318static int dquot_active(const struct inode *inode)
1319{
1320 struct super_block *sb = inode->i_sb;
1321
1322 if (IS_NOQUOTA(inode))
1323 return 0;
1324 return sb_any_quota_loaded(sb) & ~sb_any_quota_suspended(sb);
1325}
1326
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327/*
Christoph Hellwig871a2932010-03-03 09:05:07 -05001328 * Initialize quota pointers in inode
1329 *
1330 * We do things in a bit complicated way but by that we avoid calling
1331 * dqget() and thus filesystem callbacks under dqptr_sem.
1332 *
1333 * It is better to call this function outside of any transaction as it
1334 * might need a lot of space in journal for dquot structure allocation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335 */
Christoph Hellwig871a2932010-03-03 09:05:07 -05001336static void __dquot_initialize(struct inode *inode, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337{
1338 unsigned int id = 0;
Christoph Hellwig871a2932010-03-03 09:05:07 -05001339 int cnt;
Dmitry Monakhovad1e6e82010-02-16 08:31:49 +03001340 struct dquot *got[MAXQUOTAS];
Jan Karacc334122009-01-12 17:23:05 +01001341 struct super_block *sb = inode->i_sb;
Jan Kara0a5a9c72010-02-09 18:20:39 +01001342 qsize_t rsv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343
Ingo Molnard3be9152006-03-23 03:00:29 -08001344 /* First test before acquiring mutex - solves deadlocks when we
1345 * re-enter the quota code and are already holding the mutex */
Christoph Hellwig189eef52010-06-04 10:56:29 +02001346 if (!dquot_active(inode))
Christoph Hellwig871a2932010-03-03 09:05:07 -05001347 return;
Jan Karacc334122009-01-12 17:23:05 +01001348
1349 /* First get references to structures we might need. */
1350 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
Dmitry Monakhovad1e6e82010-02-16 08:31:49 +03001351 got[cnt] = NULL;
Jan Karacc334122009-01-12 17:23:05 +01001352 if (type != -1 && cnt != type)
1353 continue;
1354 switch (cnt) {
1355 case USRQUOTA:
1356 id = inode->i_uid;
1357 break;
1358 case GRPQUOTA:
1359 id = inode->i_gid;
1360 break;
1361 }
1362 got[cnt] = dqget(sb, id, cnt);
1363 }
1364
1365 down_write(&sb_dqopt(sb)->dqptr_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 if (IS_NOQUOTA(inode))
1367 goto out_err;
1368 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1369 if (type != -1 && cnt != type)
1370 continue;
Jan Karacc334122009-01-12 17:23:05 +01001371 /* Avoid races with quotaoff() */
1372 if (!sb_has_quota_active(sb, cnt))
1373 continue;
Jan Karadd6f3c62009-01-26 16:01:43 +01001374 if (!inode->i_dquot[cnt]) {
Jan Karacc334122009-01-12 17:23:05 +01001375 inode->i_dquot[cnt] = got[cnt];
Jan Karadd6f3c62009-01-26 16:01:43 +01001376 got[cnt] = NULL;
Jan Kara0a5a9c72010-02-09 18:20:39 +01001377 /*
1378 * Make quota reservation system happy if someone
1379 * did a write before quota was turned on
1380 */
1381 rsv = inode_get_rsv_space(inode);
1382 if (unlikely(rsv))
1383 dquot_resv_space(inode->i_dquot[cnt], rsv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 }
1385 }
1386out_err:
Jan Karacc334122009-01-12 17:23:05 +01001387 up_write(&sb_dqopt(sb)->dqptr_sem);
1388 /* Drop unused references */
Dmitry Monakhovdc52dd32009-12-14 15:21:15 +03001389 dqput_all(got);
Christoph Hellwig871a2932010-03-03 09:05:07 -05001390}
1391
1392void dquot_initialize(struct inode *inode)
1393{
1394 __dquot_initialize(inode, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395}
Mingming Cao08d03502009-01-14 16:19:32 +01001396EXPORT_SYMBOL(dquot_initialize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397
1398/*
1399 * Release all quotas referenced by inode
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 */
Christoph Hellwig9f754752010-03-03 09:05:05 -05001401static void __dquot_drop(struct inode *inode)
Jan Kara3d9ea252008-10-10 16:12:23 +02001402{
Jan Karacc334122009-01-12 17:23:05 +01001403 int cnt;
1404 struct dquot *put[MAXQUOTAS];
1405
Jan Kara3d9ea252008-10-10 16:12:23 +02001406 down_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
Jan Karacc334122009-01-12 17:23:05 +01001407 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1408 put[cnt] = inode->i_dquot[cnt];
Jan Karadd6f3c62009-01-26 16:01:43 +01001409 inode->i_dquot[cnt] = NULL;
Jan Karacc334122009-01-12 17:23:05 +01001410 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411 up_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
Dmitry Monakhovdc52dd32009-12-14 15:21:15 +03001412 dqput_all(put);
Christoph Hellwig9f754752010-03-03 09:05:05 -05001413}
1414
1415void dquot_drop(struct inode *inode)
1416{
1417 int cnt;
1418
1419 if (IS_NOQUOTA(inode))
1420 return;
1421
1422 /*
1423 * Test before calling to rule out calls from proc and such
1424 * where we are not allowed to block. Note that this is
1425 * actually reliable test even without the lock - the caller
1426 * must assure that nobody can come after the DQUOT_DROP and
1427 * add quota pointers back anyway.
1428 */
1429 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1430 if (inode->i_dquot[cnt])
1431 break;
1432 }
1433
1434 if (cnt < MAXQUOTAS)
1435 __dquot_drop(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436}
Mingming Cao08d03502009-01-14 16:19:32 +01001437EXPORT_SYMBOL(dquot_drop);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438
1439/*
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001440 * inode_reserved_space is managed internally by quota, and protected by
1441 * i_lock similar to i_blocks+i_bytes.
1442 */
1443static qsize_t *inode_reserved_space(struct inode * inode)
1444{
1445 /* Filesystem must explicitly define it's own method in order to use
1446 * quota reservation interface */
1447 BUG_ON(!inode->i_sb->dq_op->get_reserved_space);
1448 return inode->i_sb->dq_op->get_reserved_space(inode);
1449}
1450
Dmitry Monakhovc4690702010-02-09 17:53:36 +01001451void inode_add_rsv_space(struct inode *inode, qsize_t number)
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001452{
1453 spin_lock(&inode->i_lock);
1454 *inode_reserved_space(inode) += number;
1455 spin_unlock(&inode->i_lock);
1456}
Dmitry Monakhovc4690702010-02-09 17:53:36 +01001457EXPORT_SYMBOL(inode_add_rsv_space);
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001458
Dmitry Monakhovc4690702010-02-09 17:53:36 +01001459void inode_claim_rsv_space(struct inode *inode, qsize_t number)
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001460{
1461 spin_lock(&inode->i_lock);
1462 *inode_reserved_space(inode) -= number;
1463 __inode_add_bytes(inode, number);
1464 spin_unlock(&inode->i_lock);
1465}
Dmitry Monakhovc4690702010-02-09 17:53:36 +01001466EXPORT_SYMBOL(inode_claim_rsv_space);
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001467
Dmitry Monakhovc4690702010-02-09 17:53:36 +01001468void inode_sub_rsv_space(struct inode *inode, qsize_t number)
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001469{
1470 spin_lock(&inode->i_lock);
1471 *inode_reserved_space(inode) -= number;
1472 spin_unlock(&inode->i_lock);
1473}
Dmitry Monakhovc4690702010-02-09 17:53:36 +01001474EXPORT_SYMBOL(inode_sub_rsv_space);
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001475
1476static qsize_t inode_get_rsv_space(struct inode *inode)
1477{
1478 qsize_t ret;
Jan Kara05b5d892010-01-06 18:03:36 +01001479
1480 if (!inode->i_sb->dq_op->get_reserved_space)
1481 return 0;
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001482 spin_lock(&inode->i_lock);
1483 ret = *inode_reserved_space(inode);
1484 spin_unlock(&inode->i_lock);
1485 return ret;
1486}
1487
1488static void inode_incr_space(struct inode *inode, qsize_t number,
1489 int reserve)
1490{
1491 if (reserve)
1492 inode_add_rsv_space(inode, number);
1493 else
1494 inode_add_bytes(inode, number);
1495}
1496
1497static void inode_decr_space(struct inode *inode, qsize_t number, int reserve)
1498{
1499 if (reserve)
1500 inode_sub_rsv_space(inode, number);
1501 else
1502 inode_sub_bytes(inode, number);
1503}
1504
1505/*
Christoph Hellwig5dd40562010-03-03 09:05:00 -05001506 * This functions updates i_blocks+i_bytes fields and quota information
1507 * (together with appropriate checks).
1508 *
1509 * NOTE: We absolutely rely on the fact that caller dirties the inode
1510 * (usually helpers in quotaops.h care about this) and holds a handle for
1511 * the current transaction so that dquot write and inode write go into the
1512 * same transaction.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513 */
1514
1515/*
1516 * This operation can block, but only after everything is updated
1517 */
Eric Sandeen56246f92010-05-16 09:00:00 -04001518int __dquot_alloc_space(struct inode *inode, qsize_t number, int flags)
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];
Eric Sandeen56246f92010-05-16 09:00:00 -04001522 int warn = flags & DQUOT_SPACE_WARN;
1523 int reserve = flags & DQUOT_SPACE_RESERVE;
Eric Sandeen0e058422010-05-16 10:00:00 -04001524 int nofail = flags & DQUOT_SPACE_NOFAIL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001526 /*
1527 * First test before acquiring mutex - solves deadlocks when we
1528 * re-enter the quota code and are already holding the mutex
1529 */
Christoph Hellwig189eef52010-06-04 10:56:29 +02001530 if (!dquot_active(inode)) {
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001531 inode_incr_space(inode, number, reserve);
1532 goto out;
1533 }
1534
1535 down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
Jan Kara8e893462007-10-16 23:29:31 -07001537 warntype[cnt] = QUOTA_NL_NOWARN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539 spin_lock(&dq_data_lock);
1540 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
Jan Karadd6f3c62009-01-26 16:01:43 +01001541 if (!inode->i_dquot[cnt])
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542 continue;
Christoph Hellwigefd8f0e2010-03-03 09:05:08 -05001543 ret = check_bdq(inode->i_dquot[cnt], number, !warn,
1544 warntype+cnt);
Eric Sandeen0e058422010-05-16 10:00:00 -04001545 if (ret && !nofail) {
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001546 spin_unlock(&dq_data_lock);
1547 goto out_flush_warn;
Mingming Caof18df222009-01-13 16:43:09 +01001548 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549 }
1550 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
Jan Karadd6f3c62009-01-26 16:01:43 +01001551 if (!inode->i_dquot[cnt])
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552 continue;
Mingming Caof18df222009-01-13 16:43:09 +01001553 if (reserve)
1554 dquot_resv_space(inode->i_dquot[cnt], number);
1555 else
1556 dquot_incr_space(inode->i_dquot[cnt], number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557 }
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001558 inode_incr_space(inode, number, reserve);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559 spin_unlock(&dq_data_lock);
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001560
1561 if (reserve)
1562 goto out_flush_warn;
Dmitry Monakhovdc52dd32009-12-14 15:21:15 +03001563 mark_all_dquot_dirty(inode->i_dquot);
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001564out_flush_warn:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565 flush_warnings(inode->i_dquot, warntype);
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001566 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1567out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568 return ret;
1569}
Christoph Hellwig5dd40562010-03-03 09:05:00 -05001570EXPORT_SYMBOL(__dquot_alloc_space);
Mingming Caof18df222009-01-13 16:43:09 +01001571
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572/*
1573 * This operation can block, but only after everything is updated
1574 */
Christoph Hellwig63936dd2010-03-03 09:05:01 -05001575int dquot_alloc_inode(const struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576{
Christoph Hellwigefd8f0e2010-03-03 09:05:08 -05001577 int cnt, ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578 char warntype[MAXQUOTAS];
1579
Ingo Molnard3be9152006-03-23 03:00:29 -08001580 /* First test before acquiring mutex - solves deadlocks when we
1581 * re-enter the quota code and are already holding the mutex */
Christoph Hellwig189eef52010-06-04 10:56:29 +02001582 if (!dquot_active(inode))
Christoph Hellwig63936dd2010-03-03 09:05:01 -05001583 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
Jan Kara8e893462007-10-16 23:29:31 -07001585 warntype[cnt] = QUOTA_NL_NOWARN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586 down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587 spin_lock(&dq_data_lock);
1588 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
Jan Karadd6f3c62009-01-26 16:01:43 +01001589 if (!inode->i_dquot[cnt])
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590 continue;
Christoph Hellwigefd8f0e2010-03-03 09:05:08 -05001591 ret = check_idq(inode->i_dquot[cnt], 1, warntype + cnt);
1592 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593 goto warn_put_all;
1594 }
1595
1596 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
Jan Karadd6f3c62009-01-26 16:01:43 +01001597 if (!inode->i_dquot[cnt])
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598 continue;
Christoph Hellwig63936dd2010-03-03 09:05:01 -05001599 dquot_incr_inodes(inode->i_dquot[cnt], 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600 }
Christoph Hellwigefd8f0e2010-03-03 09:05:08 -05001601
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602warn_put_all:
1603 spin_unlock(&dq_data_lock);
Christoph Hellwig63936dd2010-03-03 09:05:01 -05001604 if (ret == 0)
Dmitry Monakhovdc52dd32009-12-14 15:21:15 +03001605 mark_all_dquot_dirty(inode->i_dquot);
Jan Kara087ee8d2007-12-17 16:20:26 -08001606 flush_warnings(inode->i_dquot, warntype);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1608 return ret;
1609}
Mingming Cao08d03502009-01-14 16:19:32 +01001610EXPORT_SYMBOL(dquot_alloc_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611
Christoph Hellwig5dd40562010-03-03 09:05:00 -05001612/*
1613 * Convert in-memory reserved quotas to real consumed quotas
1614 */
1615int dquot_claim_space_nodirty(struct inode *inode, qsize_t number)
Mingming Cao740d9dc2009-01-13 16:43:14 +01001616{
1617 int cnt;
Mingming Cao740d9dc2009-01-13 16:43:14 +01001618
Christoph Hellwig189eef52010-06-04 10:56:29 +02001619 if (!dquot_active(inode)) {
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001620 inode_claim_rsv_space(inode, number);
Christoph Hellwig5dd40562010-03-03 09:05:00 -05001621 return 0;
Mingming Cao740d9dc2009-01-13 16:43:14 +01001622 }
1623
1624 down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
Mingming Cao740d9dc2009-01-13 16:43:14 +01001625 spin_lock(&dq_data_lock);
1626 /* Claim reserved quotas to allocated quotas */
1627 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
Jan Karadd6f3c62009-01-26 16:01:43 +01001628 if (inode->i_dquot[cnt])
Mingming Cao740d9dc2009-01-13 16:43:14 +01001629 dquot_claim_reserved_space(inode->i_dquot[cnt],
1630 number);
1631 }
1632 /* Update inode bytes */
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001633 inode_claim_rsv_space(inode, number);
Mingming Cao740d9dc2009-01-13 16:43:14 +01001634 spin_unlock(&dq_data_lock);
Dmitry Monakhovdc52dd32009-12-14 15:21:15 +03001635 mark_all_dquot_dirty(inode->i_dquot);
Mingming Cao740d9dc2009-01-13 16:43:14 +01001636 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
Christoph Hellwig5dd40562010-03-03 09:05:00 -05001637 return 0;
Mingming Cao740d9dc2009-01-13 16:43:14 +01001638}
Christoph Hellwig5dd40562010-03-03 09:05:00 -05001639EXPORT_SYMBOL(dquot_claim_space_nodirty);
Mingming Cao740d9dc2009-01-13 16:43:14 +01001640
1641/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642 * This operation can block, but only after everything is updated
1643 */
Eric Sandeen56246f92010-05-16 09:00:00 -04001644void __dquot_free_space(struct inode *inode, qsize_t number, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645{
1646 unsigned int cnt;
Jan Kara657d3bf2008-07-25 01:46:52 -07001647 char warntype[MAXQUOTAS];
Eric Sandeen56246f92010-05-16 09:00:00 -04001648 int reserve = flags & DQUOT_SPACE_RESERVE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649
Ingo Molnard3be9152006-03-23 03:00:29 -08001650 /* First test before acquiring mutex - solves deadlocks when we
1651 * re-enter the quota code and are already holding the mutex */
Christoph Hellwig189eef52010-06-04 10:56:29 +02001652 if (!dquot_active(inode)) {
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001653 inode_decr_space(inode, number, reserve);
Christoph Hellwig5dd40562010-03-03 09:05:00 -05001654 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655 }
Jan Kara657d3bf2008-07-25 01:46:52 -07001656
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657 down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658 spin_lock(&dq_data_lock);
1659 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
Jan Karadd6f3c62009-01-26 16:01:43 +01001660 if (!inode->i_dquot[cnt])
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661 continue;
Jan Kara657d3bf2008-07-25 01:46:52 -07001662 warntype[cnt] = info_bdq_free(inode->i_dquot[cnt], number);
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001663 if (reserve)
1664 dquot_free_reserved_space(inode->i_dquot[cnt], number);
1665 else
1666 dquot_decr_space(inode->i_dquot[cnt], number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667 }
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001668 inode_decr_space(inode, number, reserve);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669 spin_unlock(&dq_data_lock);
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001670
1671 if (reserve)
1672 goto out_unlock;
Dmitry Monakhovdc52dd32009-12-14 15:21:15 +03001673 mark_all_dquot_dirty(inode->i_dquot);
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001674out_unlock:
Jan Kara657d3bf2008-07-25 01:46:52 -07001675 flush_warnings(inode->i_dquot, warntype);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677}
Christoph Hellwig5dd40562010-03-03 09:05:00 -05001678EXPORT_SYMBOL(__dquot_free_space);
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001679
1680/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681 * This operation can block, but only after everything is updated
1682 */
Christoph Hellwig63936dd2010-03-03 09:05:01 -05001683void dquot_free_inode(const struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684{
1685 unsigned int cnt;
Jan Kara657d3bf2008-07-25 01:46:52 -07001686 char warntype[MAXQUOTAS];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687
Ingo Molnard3be9152006-03-23 03:00:29 -08001688 /* First test before acquiring mutex - solves deadlocks when we
1689 * re-enter the quota code and are already holding the mutex */
Christoph Hellwig189eef52010-06-04 10:56:29 +02001690 if (!dquot_active(inode))
Christoph Hellwig63936dd2010-03-03 09:05:01 -05001691 return;
Jan Kara657d3bf2008-07-25 01:46:52 -07001692
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693 down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694 spin_lock(&dq_data_lock);
1695 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
Jan Karadd6f3c62009-01-26 16:01:43 +01001696 if (!inode->i_dquot[cnt])
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697 continue;
Christoph Hellwig63936dd2010-03-03 09:05:01 -05001698 warntype[cnt] = info_idq_free(inode->i_dquot[cnt], 1);
1699 dquot_decr_inodes(inode->i_dquot[cnt], 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700 }
1701 spin_unlock(&dq_data_lock);
Dmitry Monakhovdc52dd32009-12-14 15:21:15 +03001702 mark_all_dquot_dirty(inode->i_dquot);
Jan Kara657d3bf2008-07-25 01:46:52 -07001703 flush_warnings(inode->i_dquot, warntype);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705}
Mingming Cao08d03502009-01-14 16:19:32 +01001706EXPORT_SYMBOL(dquot_free_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707
1708/*
1709 * Transfer the number of inode and blocks from one diskquota to an other.
Jan Karabc8e5f02010-05-13 19:58:50 +02001710 * On success, dquot references in transfer_to are consumed and references
1711 * to original dquots that need to be released are placed there. On failure,
1712 * references are kept untouched.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713 *
1714 * This operation can block, but only after everything is updated
1715 * A transaction must be started when entering this function.
Jan Karabc8e5f02010-05-13 19:58:50 +02001716 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717 */
Jan Karabc8e5f02010-05-13 19:58:50 +02001718int __dquot_transfer(struct inode *inode, struct dquot **transfer_to)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719{
Mingming Cao740d9dc2009-01-13 16:43:14 +01001720 qsize_t space, cur_space;
1721 qsize_t rsv_space = 0;
Jan Karabc8e5f02010-05-13 19:58:50 +02001722 struct dquot *transfer_from[MAXQUOTAS] = {};
Christoph Hellwigefd8f0e2010-03-03 09:05:08 -05001723 int cnt, ret = 0;
Jan Kara657d3bf2008-07-25 01:46:52 -07001724 char warntype_to[MAXQUOTAS];
1725 char warntype_from_inodes[MAXQUOTAS], warntype_from_space[MAXQUOTAS];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726
Ingo Molnard3be9152006-03-23 03:00:29 -08001727 /* First test before acquiring mutex - solves deadlocks when we
1728 * re-enter the quota code and are already holding the mutex */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729 if (IS_NOQUOTA(inode))
Christoph Hellwigefd8f0e2010-03-03 09:05:08 -05001730 return 0;
Jan Karacc334122009-01-12 17:23:05 +01001731 /* Initialize the arrays */
Jan Karabc8e5f02010-05-13 19:58:50 +02001732 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
Jan Kara657d3bf2008-07-25 01:46:52 -07001733 warntype_to[cnt] = QUOTA_NL_NOWARN;
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);
Jan Karabc8e5f02010-05-13 19:58:50 +02001737 return 0;
Jan Karacc334122009-01-12 17:23:05 +01001738 }
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);
Jan Karabc8e5f02010-05-13 19:58:50 +02001789 /* Pass back references to put */
Dmitry Monakhovdc52dd32009-12-14 15:21:15 +03001790 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
Jan Karabc8e5f02010-05-13 19:58:50 +02001791 transfer_to[cnt] = transfer_from[cnt];
1792warn:
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);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796 return ret;
Jan Karacc334122009-01-12 17:23:05 +01001797over_quota:
1798 spin_unlock(&dq_data_lock);
1799 up_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
Jan Karabc8e5f02010-05-13 19:58:50 +02001800 goto warn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801}
Jan Karabc8e5f02010-05-13 19:58:50 +02001802EXPORT_SYMBOL(__dquot_transfer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803
Dmitry Monakhov8ddd69d2010-02-16 08:31:50 +03001804/* Wrapper for transferring ownership of an inode for uid/gid only
1805 * Called from FSXXX_setattr()
1806 */
Christoph Hellwigb43fa822010-03-03 09:05:03 -05001807int dquot_transfer(struct inode *inode, struct iattr *iattr)
Jan Karab85f4b82008-07-25 01:46:50 -07001808{
Jan Karabc8e5f02010-05-13 19:58:50 +02001809 struct dquot *transfer_to[MAXQUOTAS] = {};
1810 struct super_block *sb = inode->i_sb;
1811 int ret;
Dmitry Monakhov8ddd69d2010-02-16 08:31:50 +03001812
Christoph Hellwig189eef52010-06-04 10:56:29 +02001813 if (!dquot_active(inode))
Jan Karabc8e5f02010-05-13 19:58:50 +02001814 return 0;
Dmitry Monakhov12755622010-04-08 22:04:20 +04001815
Jan Karabc8e5f02010-05-13 19:58:50 +02001816 if (iattr->ia_valid & ATTR_UID && iattr->ia_uid != inode->i_uid)
1817 transfer_to[USRQUOTA] = dqget(sb, iattr->ia_uid, USRQUOTA);
1818 if (iattr->ia_valid & ATTR_GID && iattr->ia_gid != inode->i_gid)
Jan Kara4dea4962010-05-24 12:39:49 +02001819 transfer_to[GRPQUOTA] = dqget(sb, iattr->ia_gid, GRPQUOTA);
Jan Karabc8e5f02010-05-13 19:58:50 +02001820
1821 ret = __dquot_transfer(inode, transfer_to);
1822 dqput_all(transfer_to);
1823 return ret;
Jan Karab85f4b82008-07-25 01:46:50 -07001824}
Christoph Hellwigb43fa822010-03-03 09:05:03 -05001825EXPORT_SYMBOL(dquot_transfer);
Jan Karab85f4b82008-07-25 01:46:50 -07001826
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827/*
1828 * Write info of quota file to disk
1829 */
1830int dquot_commit_info(struct super_block *sb, int type)
1831{
1832 int ret;
1833 struct quota_info *dqopt = sb_dqopt(sb);
1834
Ingo Molnard3be9152006-03-23 03:00:29 -08001835 mutex_lock(&dqopt->dqio_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836 ret = dqopt->ops[type]->write_file_info(sb, type);
Ingo Molnard3be9152006-03-23 03:00:29 -08001837 mutex_unlock(&dqopt->dqio_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838 return ret;
1839}
Mingming Cao08d03502009-01-14 16:19:32 +01001840EXPORT_SYMBOL(dquot_commit_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841
1842/*
1843 * Definitions of diskquota operations.
1844 */
Alexey Dobriyan61e225d2009-09-21 17:01:08 -07001845const struct dquot_operations dquot_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846 .write_dquot = dquot_commit,
1847 .acquire_dquot = dquot_acquire,
1848 .release_dquot = dquot_release,
1849 .mark_dirty = dquot_mark_dquot_dirty,
Jan Kara74f783a2008-08-19 14:51:22 +02001850 .write_info = dquot_commit_info,
1851 .alloc_dquot = dquot_alloc,
1852 .destroy_dquot = dquot_destroy,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853};
Christoph Hellwig123e9ca2010-05-19 07:16:44 -04001854EXPORT_SYMBOL(dquot_operations);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856/*
Christoph Hellwig907f4552010-03-03 09:05:06 -05001857 * Generic helper for ->open on filesystems supporting disk quotas.
1858 */
1859int dquot_file_open(struct inode *inode, struct file *file)
1860{
1861 int error;
1862
1863 error = generic_file_open(inode, file);
1864 if (!error && (file->f_mode & FMODE_WRITE))
Christoph Hellwig871a2932010-03-03 09:05:07 -05001865 dquot_initialize(inode);
Christoph Hellwig907f4552010-03-03 09:05:06 -05001866 return error;
1867}
1868EXPORT_SYMBOL(dquot_file_open);
1869
1870/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871 * Turn quota off on a device. type == -1 ==> quotaoff for all types (umount)
1872 */
Christoph Hellwig0f0dd622010-05-19 07:16:41 -04001873int dquot_disable(struct super_block *sb, int type, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874{
Jan Kara0ff5af82008-04-28 02:14:33 -07001875 int cnt, ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876 struct quota_info *dqopt = sb_dqopt(sb);
1877 struct inode *toputinode[MAXQUOTAS];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878
Jan Karaf55abc02008-08-20 17:50:32 +02001879 /* Cannot turn off usage accounting without turning off limits, or
1880 * suspend quotas and simultaneously turn quotas off. */
1881 if ((flags & DQUOT_USAGE_ENABLED && !(flags & DQUOT_LIMITS_ENABLED))
1882 || (flags & DQUOT_SUSPENDED && flags & (DQUOT_LIMITS_ENABLED |
1883 DQUOT_USAGE_ENABLED)))
1884 return -EINVAL;
1885
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886 /* We need to serialize quota_off() for device */
Ingo Molnard3be9152006-03-23 03:00:29 -08001887 mutex_lock(&dqopt->dqonoff_mutex);
Jan Kara9377abd2008-05-12 14:02:08 -07001888
1889 /*
1890 * Skip everything if there's nothing to do. We have to do this because
1891 * sometimes we are called when fill_super() failed and calling
1892 * sync_fs() in such cases does no good.
1893 */
Jan Karaf55abc02008-08-20 17:50:32 +02001894 if (!sb_any_quota_loaded(sb)) {
Jan Kara9377abd2008-05-12 14:02:08 -07001895 mutex_unlock(&dqopt->dqonoff_mutex);
1896 return 0;
1897 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1899 toputinode[cnt] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900 if (type != -1 && cnt != type)
1901 continue;
Jan Karaf55abc02008-08-20 17:50:32 +02001902 if (!sb_has_quota_loaded(sb, cnt))
Jan Kara0ff5af82008-04-28 02:14:33 -07001903 continue;
Jan Karaf55abc02008-08-20 17:50:32 +02001904
1905 if (flags & DQUOT_SUSPENDED) {
Jan Karacc334122009-01-12 17:23:05 +01001906 spin_lock(&dq_state_lock);
Jan Karaf55abc02008-08-20 17:50:32 +02001907 dqopt->flags |=
1908 dquot_state_flag(DQUOT_SUSPENDED, cnt);
Jan Karacc334122009-01-12 17:23:05 +01001909 spin_unlock(&dq_state_lock);
Jan Karaf55abc02008-08-20 17:50:32 +02001910 } else {
Jan Karacc334122009-01-12 17:23:05 +01001911 spin_lock(&dq_state_lock);
Jan Karaf55abc02008-08-20 17:50:32 +02001912 dqopt->flags &= ~dquot_state_flag(flags, cnt);
1913 /* Turning off suspended quotas? */
1914 if (!sb_has_quota_loaded(sb, cnt) &&
1915 sb_has_quota_suspended(sb, cnt)) {
1916 dqopt->flags &= ~dquot_state_flag(
1917 DQUOT_SUSPENDED, cnt);
Jan Karacc334122009-01-12 17:23:05 +01001918 spin_unlock(&dq_state_lock);
Jan Karaf55abc02008-08-20 17:50:32 +02001919 iput(dqopt->files[cnt]);
1920 dqopt->files[cnt] = NULL;
1921 continue;
1922 }
Jan Karacc334122009-01-12 17:23:05 +01001923 spin_unlock(&dq_state_lock);
Jan Kara0ff5af82008-04-28 02:14:33 -07001924 }
Jan Karaf55abc02008-08-20 17:50:32 +02001925
1926 /* We still have to keep quota loaded? */
1927 if (sb_has_quota_loaded(sb, cnt) && !(flags & DQUOT_SUSPENDED))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929
1930 /* Note: these are blocking operations */
1931 drop_dquot_ref(sb, cnt);
1932 invalidate_dquots(sb, cnt);
1933 /*
Jan Kara268157b2009-01-27 15:47:22 +01001934 * Now all dquots should be invalidated, all writes done so we
1935 * should be only users of the info. No locks needed.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936 */
1937 if (info_dirty(&dqopt->info[cnt]))
1938 sb->dq_op->write_info(sb, cnt);
1939 if (dqopt->ops[cnt]->free_file_info)
1940 dqopt->ops[cnt]->free_file_info(sb, cnt);
1941 put_quota_format(dqopt->info[cnt].dqi_format);
1942
1943 toputinode[cnt] = dqopt->files[cnt];
Jan Karaf55abc02008-08-20 17:50:32 +02001944 if (!sb_has_quota_loaded(sb, cnt))
Jan Kara0ff5af82008-04-28 02:14:33 -07001945 dqopt->files[cnt] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946 dqopt->info[cnt].dqi_flags = 0;
1947 dqopt->info[cnt].dqi_igrace = 0;
1948 dqopt->info[cnt].dqi_bgrace = 0;
1949 dqopt->ops[cnt] = NULL;
1950 }
Ingo Molnard3be9152006-03-23 03:00:29 -08001951 mutex_unlock(&dqopt->dqonoff_mutex);
Jan Karaca785ec2008-09-30 17:53:37 +02001952
1953 /* Skip syncing and setting flags if quota files are hidden */
1954 if (dqopt->flags & DQUOT_QUOTA_SYS_FILE)
1955 goto put_inodes;
1956
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957 /* Sync the superblock so that buffers with quota data are written to
Al Viro7b7b1ac2005-11-07 17:13:39 -05001958 * disk (and so userspace sees correct data afterwards). */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959 if (sb->s_op->sync_fs)
1960 sb->s_op->sync_fs(sb, 1);
1961 sync_blockdev(sb->s_bdev);
1962 /* Now the quota files are just ordinary files and we can set the
1963 * inode flags back. Moreover we discard the pagecache so that
1964 * userspace sees the writes we did bypassing the pagecache. We
1965 * must also discard the blockdev buffers so that we see the
1966 * changes done by userspace on the next quotaon() */
1967 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1968 if (toputinode[cnt]) {
Ingo Molnard3be9152006-03-23 03:00:29 -08001969 mutex_lock(&dqopt->dqonoff_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970 /* If quota was reenabled in the meantime, we have
1971 * nothing to do */
Jan Karaf55abc02008-08-20 17:50:32 +02001972 if (!sb_has_quota_loaded(sb, cnt)) {
Jan Kara268157b2009-01-27 15:47:22 +01001973 mutex_lock_nested(&toputinode[cnt]->i_mutex,
1974 I_MUTEX_QUOTA);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975 toputinode[cnt]->i_flags &= ~(S_IMMUTABLE |
1976 S_NOATIME | S_NOQUOTA);
Jan Kara268157b2009-01-27 15:47:22 +01001977 truncate_inode_pages(&toputinode[cnt]->i_data,
1978 0);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001979 mutex_unlock(&toputinode[cnt]->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980 mark_inode_dirty(toputinode[cnt]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981 }
Ingo Molnard3be9152006-03-23 03:00:29 -08001982 mutex_unlock(&dqopt->dqonoff_mutex);
Jan Karaca785ec2008-09-30 17:53:37 +02001983 }
1984 if (sb->s_bdev)
1985 invalidate_bdev(sb->s_bdev);
1986put_inodes:
1987 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1988 if (toputinode[cnt]) {
Jan Kara0ff5af82008-04-28 02:14:33 -07001989 /* On remount RO, we keep the inode pointer so that we
Jan Karaf55abc02008-08-20 17:50:32 +02001990 * can reenable quota on the subsequent remount RW. We
1991 * have to check 'flags' variable and not use sb_has_
1992 * function because another quotaon / quotaoff could
1993 * change global state before we got here. We refuse
1994 * to suspend quotas when there is pending delete on
1995 * the quota file... */
1996 if (!(flags & DQUOT_SUSPENDED))
Jan Kara0ff5af82008-04-28 02:14:33 -07001997 iput(toputinode[cnt]);
1998 else if (!toputinode[cnt]->i_nlink)
1999 ret = -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000 }
Jan Kara0ff5af82008-04-28 02:14:33 -07002001 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002}
Christoph Hellwig0f0dd622010-05-19 07:16:41 -04002003EXPORT_SYMBOL(dquot_disable);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004
Christoph Hellwig287a8092010-05-19 07:16:45 -04002005int dquot_quota_off(struct super_block *sb, int type)
Jan Karaf55abc02008-08-20 17:50:32 +02002006{
Christoph Hellwig0f0dd622010-05-19 07:16:41 -04002007 return dquot_disable(sb, type,
2008 DQUOT_USAGE_ENABLED | DQUOT_LIMITS_ENABLED);
Jan Karaf55abc02008-08-20 17:50:32 +02002009}
Christoph Hellwig287a8092010-05-19 07:16:45 -04002010EXPORT_SYMBOL(dquot_quota_off);
Christoph Hellwig0f0dd622010-05-19 07:16:41 -04002011
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012/*
2013 * Turn quotas on on a device
2014 */
2015
Jan Karaf55abc02008-08-20 17:50:32 +02002016/*
2017 * Helper function to turn quotas on when we already have the inode of
2018 * quota file and no quota information is loaded.
2019 */
2020static int vfs_load_quota_inode(struct inode *inode, int type, int format_id,
2021 unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022{
2023 struct quota_format_type *fmt = find_quota_format(format_id);
2024 struct super_block *sb = inode->i_sb;
2025 struct quota_info *dqopt = sb_dqopt(sb);
2026 int error;
2027 int oldflags = -1;
2028
2029 if (!fmt)
2030 return -ESRCH;
2031 if (!S_ISREG(inode->i_mode)) {
2032 error = -EACCES;
2033 goto out_fmt;
2034 }
2035 if (IS_RDONLY(inode)) {
2036 error = -EROFS;
2037 goto out_fmt;
2038 }
2039 if (!sb->s_op->quota_write || !sb->s_op->quota_read) {
2040 error = -EINVAL;
2041 goto out_fmt;
2042 }
Jan Karaf55abc02008-08-20 17:50:32 +02002043 /* Usage always has to be set... */
2044 if (!(flags & DQUOT_USAGE_ENABLED)) {
2045 error = -EINVAL;
2046 goto out_fmt;
2047 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048
Jan Karaca785ec2008-09-30 17:53:37 +02002049 if (!(dqopt->flags & DQUOT_QUOTA_SYS_FILE)) {
Jan Karaab94c392010-02-22 21:07:17 +01002050 /* As we bypass the pagecache we must now flush all the
2051 * dirty data and invalidate caches so that kernel sees
2052 * changes from userspace. It is not enough to just flush
2053 * the quota file since if blocksize < pagesize, invalidation
2054 * of the cache could fail because of other unrelated dirty
2055 * data */
2056 sync_filesystem(sb);
Jan Karaca785ec2008-09-30 17:53:37 +02002057 invalidate_bdev(sb->s_bdev);
2058 }
Ingo Molnard3be9152006-03-23 03:00:29 -08002059 mutex_lock(&dqopt->dqonoff_mutex);
Jan Karaf55abc02008-08-20 17:50:32 +02002060 if (sb_has_quota_loaded(sb, type)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061 error = -EBUSY;
2062 goto out_lock;
2063 }
Jan Karaca785ec2008-09-30 17:53:37 +02002064
2065 if (!(dqopt->flags & DQUOT_QUOTA_SYS_FILE)) {
2066 /* We don't want quota and atime on quota files (deadlocks
2067 * possible) Also nobody should write to the file - we use
2068 * special IO operations which ignore the immutable bit. */
Jan Karadee86562009-07-22 18:12:17 +02002069 mutex_lock_nested(&inode->i_mutex, I_MUTEX_QUOTA);
Jan Kara268157b2009-01-27 15:47:22 +01002070 oldflags = inode->i_flags & (S_NOATIME | S_IMMUTABLE |
2071 S_NOQUOTA);
Jan Karaca785ec2008-09-30 17:53:37 +02002072 inode->i_flags |= S_NOQUOTA | S_NOATIME | S_IMMUTABLE;
Jan Karadee86562009-07-22 18:12:17 +02002073 mutex_unlock(&inode->i_mutex);
Jan Kara26245c92010-01-06 17:20:35 +01002074 /*
2075 * When S_NOQUOTA is set, remove dquot references as no more
2076 * references can be added
2077 */
Christoph Hellwig9f754752010-03-03 09:05:05 -05002078 __dquot_drop(inode);
Jan Karaca785ec2008-09-30 17:53:37 +02002079 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080
2081 error = -EIO;
2082 dqopt->files[type] = igrab(inode);
2083 if (!dqopt->files[type])
2084 goto out_lock;
2085 error = -EINVAL;
2086 if (!fmt->qf_ops->check_quota_file(sb, type))
2087 goto out_file_init;
2088
2089 dqopt->ops[type] = fmt->qf_ops;
2090 dqopt->info[type].dqi_format = fmt;
Jan Kara0ff5af82008-04-28 02:14:33 -07002091 dqopt->info[type].dqi_fmt_id = format_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092 INIT_LIST_HEAD(&dqopt->info[type].dqi_dirty_list);
Ingo Molnard3be9152006-03-23 03:00:29 -08002093 mutex_lock(&dqopt->dqio_mutex);
Jan Kara268157b2009-01-27 15:47:22 +01002094 error = dqopt->ops[type]->read_file_info(sb, type);
2095 if (error < 0) {
Ingo Molnard3be9152006-03-23 03:00:29 -08002096 mutex_unlock(&dqopt->dqio_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097 goto out_file_init;
2098 }
Ingo Molnard3be9152006-03-23 03:00:29 -08002099 mutex_unlock(&dqopt->dqio_mutex);
Jan Karacc334122009-01-12 17:23:05 +01002100 spin_lock(&dq_state_lock);
Jan Karaf55abc02008-08-20 17:50:32 +02002101 dqopt->flags |= dquot_state_flag(flags, type);
Jan Karacc334122009-01-12 17:23:05 +01002102 spin_unlock(&dq_state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103
2104 add_dquot_ref(sb, type);
Ingo Molnard3be9152006-03-23 03:00:29 -08002105 mutex_unlock(&dqopt->dqonoff_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106
2107 return 0;
2108
2109out_file_init:
2110 dqopt->files[type] = NULL;
2111 iput(inode);
2112out_lock:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113 if (oldflags != -1) {
Jan Karadee86562009-07-22 18:12:17 +02002114 mutex_lock_nested(&inode->i_mutex, I_MUTEX_QUOTA);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115 /* Set the flags back (in the case of accidental quotaon()
2116 * on a wrong file we don't want to mess up the flags) */
2117 inode->i_flags &= ~(S_NOATIME | S_NOQUOTA | S_IMMUTABLE);
2118 inode->i_flags |= oldflags;
Jan Karadee86562009-07-22 18:12:17 +02002119 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120 }
Jiaying Zhangd01730d2009-07-07 18:15:21 +02002121 mutex_unlock(&dqopt->dqonoff_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122out_fmt:
2123 put_quota_format(fmt);
2124
2125 return error;
2126}
2127
Jan Kara0ff5af82008-04-28 02:14:33 -07002128/* Reenable quotas on remount RW */
Christoph Hellwig0f0dd622010-05-19 07:16:41 -04002129int dquot_resume(struct super_block *sb, int type)
Jan Kara0ff5af82008-04-28 02:14:33 -07002130{
2131 struct quota_info *dqopt = sb_dqopt(sb);
2132 struct inode *inode;
Christoph Hellwig0f0dd622010-05-19 07:16:41 -04002133 int ret = 0, cnt;
Jan Karaf55abc02008-08-20 17:50:32 +02002134 unsigned int flags;
Jan Kara0ff5af82008-04-28 02:14:33 -07002135
Christoph Hellwig0f0dd622010-05-19 07:16:41 -04002136 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
2137 if (type != -1 && cnt != type)
2138 continue;
Jan Kara0ff5af82008-04-28 02:14:33 -07002139
Christoph Hellwig0f0dd622010-05-19 07:16:41 -04002140 mutex_lock(&dqopt->dqonoff_mutex);
2141 if (!sb_has_quota_suspended(sb, cnt)) {
2142 mutex_unlock(&dqopt->dqonoff_mutex);
2143 continue;
2144 }
2145 inode = dqopt->files[cnt];
2146 dqopt->files[cnt] = NULL;
2147 spin_lock(&dq_state_lock);
2148 flags = dqopt->flags & dquot_state_flag(DQUOT_USAGE_ENABLED |
2149 DQUOT_LIMITS_ENABLED,
2150 cnt);
2151 dqopt->flags &= ~dquot_state_flag(DQUOT_STATE_FLAGS, cnt);
2152 spin_unlock(&dq_state_lock);
2153 mutex_unlock(&dqopt->dqonoff_mutex);
2154
2155 flags = dquot_generic_flag(flags, cnt);
2156 ret = vfs_load_quota_inode(inode, cnt,
2157 dqopt->info[cnt].dqi_fmt_id, flags);
2158 iput(inode);
2159 }
Jan Kara0ff5af82008-04-28 02:14:33 -07002160
2161 return ret;
2162}
Christoph Hellwig0f0dd622010-05-19 07:16:41 -04002163EXPORT_SYMBOL(dquot_resume);
Jan Kara0ff5af82008-04-28 02:14:33 -07002164
Christoph Hellwig287a8092010-05-19 07:16:45 -04002165int dquot_quota_on_path(struct super_block *sb, int type, int format_id,
Al Viro77e69da2008-08-01 04:29:18 -04002166 struct path *path)
2167{
2168 int error = security_quota_on(path->dentry);
2169 if (error)
2170 return error;
2171 /* Quota file not on the same filesystem? */
2172 if (path->mnt->mnt_sb != sb)
2173 error = -EXDEV;
2174 else
Jan Karaf55abc02008-08-20 17:50:32 +02002175 error = vfs_load_quota_inode(path->dentry->d_inode, type,
2176 format_id, DQUOT_USAGE_ENABLED |
2177 DQUOT_LIMITS_ENABLED);
Al Viro77e69da2008-08-01 04:29:18 -04002178 return error;
2179}
Christoph Hellwig287a8092010-05-19 07:16:45 -04002180EXPORT_SYMBOL(dquot_quota_on_path);
Al Viro77e69da2008-08-01 04:29:18 -04002181
Christoph Hellwig287a8092010-05-19 07:16:45 -04002182int dquot_quota_on(struct super_block *sb, int type, int format_id, char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183{
Al Viro82646132008-08-02 00:57:06 -04002184 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185 int error;
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) {
Christoph Hellwig287a8092010-05-19 07:16:45 -04002189 error = dquot_quota_on_path(sb, type, format_id, &path);
Al Viro82646132008-08-02 00:57:06 -04002190 path_put(&path);
Al Viro77e69da2008-08-01 04:29:18 -04002191 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192 return error;
2193}
Christoph Hellwig287a8092010-05-19 07:16:45 -04002194EXPORT_SYMBOL(dquot_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 */
Christoph Hellwig287a8092010-05-19 07:16:45 -04002200int dquot_enable(struct inode *inode, int type, int format_id,
2201 unsigned int flags)
Jan Karaf55abc02008-08-20 17:50:32 +02002202{
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? */
Christoph Hellwig0f0dd622010-05-19 07:16:41 -04002208 BUG_ON(flags & DQUOT_SUSPENDED);
2209
Jan Karaf55abc02008-08-20 17:50:32 +02002210 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}
Christoph Hellwig287a8092010-05-19 07:16:45 -04002241EXPORT_SYMBOL(dquot_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 Hellwig287a8092010-05-19 07:16:45 -04002247int dquot_quota_on_mount(struct super_block *sb, char *qf_name,
Christoph Hellwig84de8562005-06-23 00:09:16 -07002248 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}
Christoph Hellwig287a8092010-05-19 07:16:45 -04002273EXPORT_SYMBOL(dquot_quota_on_mount);
Jan Karab85f4b82008-07-25 01:46:50 -07002274
Jan Kara12095462008-08-20 14:45:12 +02002275static inline qsize_t qbtos(qsize_t blocks)
2276{
2277 return blocks << QIF_DQBLKSIZE_BITS;
2278}
2279
2280static inline qsize_t stoqb(qsize_t space)
2281{
2282 return (space + QIF_DQBLKSIZE - 1) >> QIF_DQBLKSIZE_BITS;
2283}
2284
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285/* Generic routine for getting common part of quota structure */
Christoph Hellwigb9b2dd32010-05-06 17:04:58 -04002286static void do_get_dqblk(struct dquot *dquot, struct fs_disk_quota *di)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287{
2288 struct mem_dqblk *dm = &dquot->dq_dqb;
2289
Christoph Hellwigb9b2dd32010-05-06 17:04:58 -04002290 memset(di, 0, sizeof(*di));
2291 di->d_version = FS_DQUOT_VERSION;
2292 di->d_flags = dquot->dq_type == USRQUOTA ?
Christoph Hellwigade7ce32010-06-04 10:56:01 +02002293 FS_USER_QUOTA : FS_GROUP_QUOTA;
Christoph Hellwigb9b2dd32010-05-06 17:04:58 -04002294 di->d_id = dquot->dq_id;
2295
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296 spin_lock(&dq_data_lock);
Christoph Hellwigb9b2dd32010-05-06 17:04:58 -04002297 di->d_blk_hardlimit = stoqb(dm->dqb_bhardlimit);
2298 di->d_blk_softlimit = stoqb(dm->dqb_bsoftlimit);
2299 di->d_ino_hardlimit = dm->dqb_ihardlimit;
2300 di->d_ino_softlimit = dm->dqb_isoftlimit;
2301 di->d_bcount = dm->dqb_curspace + dm->dqb_rsvspace;
2302 di->d_icount = dm->dqb_curinodes;
2303 di->d_btimer = dm->dqb_btime;
2304 di->d_itimer = dm->dqb_itime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305 spin_unlock(&dq_data_lock);
2306}
2307
Christoph Hellwig287a8092010-05-19 07:16:45 -04002308int dquot_get_dqblk(struct super_block *sb, int type, qid_t id,
2309 struct fs_disk_quota *di)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310{
2311 struct dquot *dquot;
2312
Jan Karacc334122009-01-12 17:23:05 +01002313 dquot = dqget(sb, id, type);
Jan Karadd6f3c62009-01-26 16:01:43 +01002314 if (!dquot)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315 return -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316 do_get_dqblk(dquot, di);
2317 dqput(dquot);
Jan Karacc334122009-01-12 17:23:05 +01002318
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319 return 0;
2320}
Christoph Hellwig287a8092010-05-19 07:16:45 -04002321EXPORT_SYMBOL(dquot_get_dqblk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322
Christoph Hellwigc472b432010-05-06 17:05:17 -04002323#define VFS_FS_DQ_MASK \
2324 (FS_DQ_BCOUNT | FS_DQ_BSOFT | FS_DQ_BHARD | \
2325 FS_DQ_ICOUNT | FS_DQ_ISOFT | FS_DQ_IHARD | \
2326 FS_DQ_BTIMER | FS_DQ_ITIMER)
2327
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328/* Generic routine for setting common part of quota structure */
Christoph Hellwigc472b432010-05-06 17:05:17 -04002329static int do_set_dqblk(struct dquot *dquot, struct fs_disk_quota *di)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330{
2331 struct mem_dqblk *dm = &dquot->dq_dqb;
2332 int check_blim = 0, check_ilim = 0;
Andrew Perepechko338bf9a2008-04-28 02:14:31 -07002333 struct mem_dqinfo *dqi = &sb_dqopt(dquot->dq_sb)->info[dquot->dq_type];
2334
Christoph Hellwigc472b432010-05-06 17:05:17 -04002335 if (di->d_fieldmask & ~VFS_FS_DQ_MASK)
2336 return -EINVAL;
2337
2338 if (((di->d_fieldmask & FS_DQ_BSOFT) &&
2339 (di->d_blk_softlimit > dqi->dqi_maxblimit)) ||
2340 ((di->d_fieldmask & FS_DQ_BHARD) &&
2341 (di->d_blk_hardlimit > dqi->dqi_maxblimit)) ||
2342 ((di->d_fieldmask & FS_DQ_ISOFT) &&
2343 (di->d_ino_softlimit > dqi->dqi_maxilimit)) ||
2344 ((di->d_fieldmask & FS_DQ_IHARD) &&
2345 (di->d_ino_hardlimit > dqi->dqi_maxilimit)))
Andrew Perepechko338bf9a2008-04-28 02:14:31 -07002346 return -ERANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002347
2348 spin_lock(&dq_data_lock);
Christoph Hellwigc472b432010-05-06 17:05:17 -04002349 if (di->d_fieldmask & FS_DQ_BCOUNT) {
2350 dm->dqb_curspace = di->d_bcount - dm->dqb_rsvspace;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351 check_blim = 1;
Andrew Perepechko08261672010-04-12 22:16:50 +04002352 set_bit(DQ_LASTSET_B + QIF_SPACE_B, &dquot->dq_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002353 }
Christoph Hellwigc472b432010-05-06 17:05:17 -04002354
2355 if (di->d_fieldmask & FS_DQ_BSOFT)
2356 dm->dqb_bsoftlimit = qbtos(di->d_blk_softlimit);
2357 if (di->d_fieldmask & FS_DQ_BHARD)
2358 dm->dqb_bhardlimit = qbtos(di->d_blk_hardlimit);
2359 if (di->d_fieldmask & (FS_DQ_BSOFT | FS_DQ_BHARD)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360 check_blim = 1;
Andrew Perepechko08261672010-04-12 22:16:50 +04002361 set_bit(DQ_LASTSET_B + QIF_BLIMITS_B, &dquot->dq_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362 }
Christoph Hellwigc472b432010-05-06 17:05:17 -04002363
2364 if (di->d_fieldmask & FS_DQ_ICOUNT) {
2365 dm->dqb_curinodes = di->d_icount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366 check_ilim = 1;
Andrew Perepechko08261672010-04-12 22:16:50 +04002367 set_bit(DQ_LASTSET_B + QIF_INODES_B, &dquot->dq_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002368 }
Christoph Hellwigc472b432010-05-06 17:05:17 -04002369
2370 if (di->d_fieldmask & FS_DQ_ISOFT)
2371 dm->dqb_isoftlimit = di->d_ino_softlimit;
2372 if (di->d_fieldmask & FS_DQ_IHARD)
2373 dm->dqb_ihardlimit = di->d_ino_hardlimit;
2374 if (di->d_fieldmask & (FS_DQ_ISOFT | FS_DQ_IHARD)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375 check_ilim = 1;
Andrew Perepechko08261672010-04-12 22:16:50 +04002376 set_bit(DQ_LASTSET_B + QIF_ILIMITS_B, &dquot->dq_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377 }
Christoph Hellwigc472b432010-05-06 17:05:17 -04002378
2379 if (di->d_fieldmask & FS_DQ_BTIMER) {
2380 dm->dqb_btime = di->d_btimer;
Jan Karae04a88a92009-01-07 18:07:29 -08002381 check_blim = 1;
Andrew Perepechko08261672010-04-12 22:16:50 +04002382 set_bit(DQ_LASTSET_B + QIF_BTIME_B, &dquot->dq_flags);
Jan Kara4d59bce2008-10-02 16:48:10 +02002383 }
Christoph Hellwigc472b432010-05-06 17:05:17 -04002384
2385 if (di->d_fieldmask & FS_DQ_ITIMER) {
2386 dm->dqb_itime = di->d_itimer;
Jan Karae04a88a92009-01-07 18:07:29 -08002387 check_ilim = 1;
Andrew Perepechko08261672010-04-12 22:16:50 +04002388 set_bit(DQ_LASTSET_B + QIF_ITIME_B, &dquot->dq_flags);
Jan Kara4d59bce2008-10-02 16:48:10 +02002389 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002390
2391 if (check_blim) {
Jan Kara268157b2009-01-27 15:47:22 +01002392 if (!dm->dqb_bsoftlimit ||
2393 dm->dqb_curspace < dm->dqb_bsoftlimit) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394 dm->dqb_btime = 0;
2395 clear_bit(DQ_BLKS_B, &dquot->dq_flags);
Christoph Hellwigc472b432010-05-06 17:05:17 -04002396 } else if (!(di->d_fieldmask & FS_DQ_BTIMER))
Jan Kara268157b2009-01-27 15:47:22 +01002397 /* Set grace only if user hasn't provided his own... */
Andrew Perepechko338bf9a2008-04-28 02:14:31 -07002398 dm->dqb_btime = get_seconds() + dqi->dqi_bgrace;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002399 }
2400 if (check_ilim) {
Jan Kara268157b2009-01-27 15:47:22 +01002401 if (!dm->dqb_isoftlimit ||
2402 dm->dqb_curinodes < dm->dqb_isoftlimit) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002403 dm->dqb_itime = 0;
2404 clear_bit(DQ_INODES_B, &dquot->dq_flags);
Christoph Hellwigc472b432010-05-06 17:05:17 -04002405 } else if (!(di->d_fieldmask & FS_DQ_ITIMER))
Jan Kara268157b2009-01-27 15:47:22 +01002406 /* Set grace only if user hasn't provided his own... */
Andrew Perepechko338bf9a2008-04-28 02:14:31 -07002407 dm->dqb_itime = get_seconds() + dqi->dqi_igrace;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002408 }
Jan Kara268157b2009-01-27 15:47:22 +01002409 if (dm->dqb_bhardlimit || dm->dqb_bsoftlimit || dm->dqb_ihardlimit ||
2410 dm->dqb_isoftlimit)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002411 clear_bit(DQ_FAKE_B, &dquot->dq_flags);
2412 else
2413 set_bit(DQ_FAKE_B, &dquot->dq_flags);
2414 spin_unlock(&dq_data_lock);
2415 mark_dquot_dirty(dquot);
Andrew Perepechko338bf9a2008-04-28 02:14:31 -07002416
2417 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002418}
2419
Christoph Hellwig287a8092010-05-19 07:16:45 -04002420int dquot_set_dqblk(struct super_block *sb, int type, qid_t id,
Christoph Hellwigc472b432010-05-06 17:05:17 -04002421 struct fs_disk_quota *di)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002422{
2423 struct dquot *dquot;
Andrew Perepechko338bf9a2008-04-28 02:14:31 -07002424 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002425
Jan Karaf55abc02008-08-20 17:50:32 +02002426 dquot = dqget(sb, id, type);
2427 if (!dquot) {
2428 rc = -ESRCH;
2429 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002430 }
Andrew Perepechko338bf9a2008-04-28 02:14:31 -07002431 rc = do_set_dqblk(dquot, di);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002432 dqput(dquot);
Jan Karaf55abc02008-08-20 17:50:32 +02002433out:
Andrew Perepechko338bf9a2008-04-28 02:14:31 -07002434 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002435}
Christoph Hellwig287a8092010-05-19 07:16:45 -04002436EXPORT_SYMBOL(dquot_set_dqblk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002437
2438/* Generic routine for getting common part of quota file information */
Christoph Hellwig287a8092010-05-19 07:16:45 -04002439int dquot_get_dqinfo(struct super_block *sb, int type, struct if_dqinfo *ii)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002440{
2441 struct mem_dqinfo *mi;
2442
Ingo Molnard3be9152006-03-23 03:00:29 -08002443 mutex_lock(&sb_dqopt(sb)->dqonoff_mutex);
Jan Karaf55abc02008-08-20 17:50:32 +02002444 if (!sb_has_quota_active(sb, type)) {
Ingo Molnard3be9152006-03-23 03:00:29 -08002445 mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002446 return -ESRCH;
2447 }
2448 mi = sb_dqopt(sb)->info + type;
2449 spin_lock(&dq_data_lock);
2450 ii->dqi_bgrace = mi->dqi_bgrace;
2451 ii->dqi_igrace = mi->dqi_igrace;
2452 ii->dqi_flags = mi->dqi_flags & DQF_MASK;
2453 ii->dqi_valid = IIF_ALL;
2454 spin_unlock(&dq_data_lock);
Ingo Molnard3be9152006-03-23 03:00:29 -08002455 mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002456 return 0;
2457}
Christoph Hellwig287a8092010-05-19 07:16:45 -04002458EXPORT_SYMBOL(dquot_get_dqinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459
2460/* Generic routine for setting common part of quota file information */
Christoph Hellwig287a8092010-05-19 07:16:45 -04002461int dquot_set_dqinfo(struct super_block *sb, int type, struct if_dqinfo *ii)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002462{
2463 struct mem_dqinfo *mi;
Jan Karaf55abc02008-08-20 17:50:32 +02002464 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002465
Ingo Molnard3be9152006-03-23 03:00:29 -08002466 mutex_lock(&sb_dqopt(sb)->dqonoff_mutex);
Jan Karaf55abc02008-08-20 17:50:32 +02002467 if (!sb_has_quota_active(sb, type)) {
2468 err = -ESRCH;
2469 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470 }
2471 mi = sb_dqopt(sb)->info + type;
2472 spin_lock(&dq_data_lock);
2473 if (ii->dqi_valid & IIF_BGRACE)
2474 mi->dqi_bgrace = ii->dqi_bgrace;
2475 if (ii->dqi_valid & IIF_IGRACE)
2476 mi->dqi_igrace = ii->dqi_igrace;
2477 if (ii->dqi_valid & IIF_FLAGS)
Jan Kara268157b2009-01-27 15:47:22 +01002478 mi->dqi_flags = (mi->dqi_flags & ~DQF_MASK) |
2479 (ii->dqi_flags & DQF_MASK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002480 spin_unlock(&dq_data_lock);
2481 mark_info_dirty(sb, type);
2482 /* Force write to disk */
2483 sb->dq_op->write_info(sb, type);
Jan Karaf55abc02008-08-20 17:50:32 +02002484out:
Ingo Molnard3be9152006-03-23 03:00:29 -08002485 mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
Jan Karaf55abc02008-08-20 17:50:32 +02002486 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002487}
Christoph Hellwig287a8092010-05-19 07:16:45 -04002488EXPORT_SYMBOL(dquot_set_dqinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002489
Christoph Hellwig287a8092010-05-19 07:16:45 -04002490const struct quotactl_ops dquot_quotactl_ops = {
2491 .quota_on = dquot_quota_on,
2492 .quota_off = dquot_quota_off,
2493 .quota_sync = dquot_quota_sync,
2494 .get_info = dquot_get_dqinfo,
2495 .set_info = dquot_set_dqinfo,
2496 .get_dqblk = dquot_get_dqblk,
2497 .set_dqblk = dquot_set_dqblk
Linus Torvalds1da177e2005-04-16 15:20:36 -07002498};
Christoph Hellwig287a8092010-05-19 07:16:45 -04002499EXPORT_SYMBOL(dquot_quotactl_ops);
Dmitry Monakhovdde95882010-04-26 20:03:33 +04002500
2501static int do_proc_dqstats(struct ctl_table *table, int write,
2502 void __user *buffer, size_t *lenp, loff_t *ppos)
2503{
Dmitry Monakhovdde95882010-04-26 20:03:33 +04002504 unsigned int type = (int *)table->data - dqstats.stat;
Dmitry Monakhovf32764b2010-05-26 23:21:58 +02002505
2506 /* Update global table */
2507 dqstats.stat[type] =
2508 percpu_counter_sum_positive(&dqstats.counter[type]);
Dmitry Monakhovdde95882010-04-26 20:03:33 +04002509 return proc_dointvec(table, write, buffer, lenp, ppos);
2510}
2511
Linus Torvalds1da177e2005-04-16 15:20:36 -07002512static ctl_table fs_dqstats_table[] = {
2513 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002514 .procname = "lookups",
Dmitry Monakhovdde95882010-04-26 20:03:33 +04002515 .data = &dqstats.stat[DQST_LOOKUPS],
Linus Torvalds1da177e2005-04-16 15:20:36 -07002516 .maxlen = sizeof(int),
2517 .mode = 0444,
Dmitry Monakhovdde95882010-04-26 20:03:33 +04002518 .proc_handler = do_proc_dqstats,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002519 },
2520 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002521 .procname = "drops",
Dmitry Monakhovdde95882010-04-26 20:03:33 +04002522 .data = &dqstats.stat[DQST_DROPS],
Linus Torvalds1da177e2005-04-16 15:20:36 -07002523 .maxlen = sizeof(int),
2524 .mode = 0444,
Dmitry Monakhovdde95882010-04-26 20:03:33 +04002525 .proc_handler = do_proc_dqstats,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002526 },
2527 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002528 .procname = "reads",
Dmitry Monakhovdde95882010-04-26 20:03:33 +04002529 .data = &dqstats.stat[DQST_READS],
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530 .maxlen = sizeof(int),
2531 .mode = 0444,
Dmitry Monakhovdde95882010-04-26 20:03:33 +04002532 .proc_handler = do_proc_dqstats,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002533 },
2534 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002535 .procname = "writes",
Dmitry Monakhovdde95882010-04-26 20:03:33 +04002536 .data = &dqstats.stat[DQST_WRITES],
Linus Torvalds1da177e2005-04-16 15:20:36 -07002537 .maxlen = sizeof(int),
2538 .mode = 0444,
Dmitry Monakhovdde95882010-04-26 20:03:33 +04002539 .proc_handler = do_proc_dqstats,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002540 },
2541 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002542 .procname = "cache_hits",
Dmitry Monakhovdde95882010-04-26 20:03:33 +04002543 .data = &dqstats.stat[DQST_CACHE_HITS],
Linus Torvalds1da177e2005-04-16 15:20:36 -07002544 .maxlen = sizeof(int),
2545 .mode = 0444,
Dmitry Monakhovdde95882010-04-26 20:03:33 +04002546 .proc_handler = do_proc_dqstats,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002547 },
2548 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002549 .procname = "allocated_dquots",
Dmitry Monakhovdde95882010-04-26 20:03:33 +04002550 .data = &dqstats.stat[DQST_ALLOC_DQUOTS],
Linus Torvalds1da177e2005-04-16 15:20:36 -07002551 .maxlen = sizeof(int),
2552 .mode = 0444,
Dmitry Monakhovdde95882010-04-26 20:03:33 +04002553 .proc_handler = do_proc_dqstats,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002554 },
2555 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002556 .procname = "free_dquots",
Dmitry Monakhovdde95882010-04-26 20:03:33 +04002557 .data = &dqstats.stat[DQST_FREE_DQUOTS],
Linus Torvalds1da177e2005-04-16 15:20:36 -07002558 .maxlen = sizeof(int),
2559 .mode = 0444,
Dmitry Monakhovdde95882010-04-26 20:03:33 +04002560 .proc_handler = do_proc_dqstats,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002561 },
2562 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002563 .procname = "syncs",
Dmitry Monakhovdde95882010-04-26 20:03:33 +04002564 .data = &dqstats.stat[DQST_SYNCS],
Linus Torvalds1da177e2005-04-16 15:20:36 -07002565 .maxlen = sizeof(int),
2566 .mode = 0444,
Dmitry Monakhovdde95882010-04-26 20:03:33 +04002567 .proc_handler = do_proc_dqstats,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002568 },
Jan Kara8e893462007-10-16 23:29:31 -07002569#ifdef CONFIG_PRINT_QUOTA_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07002570 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002571 .procname = "warnings",
2572 .data = &flag_print_warnings,
2573 .maxlen = sizeof(int),
2574 .mode = 0644,
Eric W. Biederman6d456112009-11-16 03:11:48 -08002575 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002576 },
Jan Kara8e893462007-10-16 23:29:31 -07002577#endif
Eric W. Biedermanab092032009-11-05 14:25:10 -08002578 { },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002579};
2580
2581static ctl_table fs_table[] = {
2582 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002583 .procname = "quota",
2584 .mode = 0555,
2585 .child = fs_dqstats_table,
2586 },
Eric W. Biedermanab092032009-11-05 14:25:10 -08002587 { },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002588};
2589
2590static ctl_table sys_table[] = {
2591 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002592 .procname = "fs",
2593 .mode = 0555,
2594 .child = fs_table,
2595 },
Eric W. Biedermanab092032009-11-05 14:25:10 -08002596 { },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002597};
2598
2599static int __init dquot_init(void)
2600{
Dmitry Monakhovf32764b2010-05-26 23:21:58 +02002601 int i, ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002602 unsigned long nr_hash, order;
2603
2604 printk(KERN_NOTICE "VFS: Disk quotas %s\n", __DQUOT_VERSION__);
2605
Eric W. Biederman0b4d4142007-02-14 00:34:09 -08002606 register_sysctl_table(sys_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002607
Paul Mundt20c2df82007-07-20 10:11:58 +09002608 dquot_cachep = kmem_cache_create("dquot",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002609 sizeof(struct dquot), sizeof(unsigned long) * 4,
Paul Jacksonfffb60f2006-03-24 03:16:06 -08002610 (SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT|
2611 SLAB_MEM_SPREAD|SLAB_PANIC),
Paul Mundt20c2df82007-07-20 10:11:58 +09002612 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002613
2614 order = 0;
2615 dquot_hash = (struct hlist_head *)__get_free_pages(GFP_ATOMIC, order);
2616 if (!dquot_hash)
2617 panic("Cannot create dquot hash table");
2618
Dmitry Monakhovf32764b2010-05-26 23:21:58 +02002619 for (i = 0; i < _DQST_DQSTAT_LAST; i++) {
2620 ret = percpu_counter_init(&dqstats.counter[i], 0);
2621 if (ret)
2622 panic("Cannot create dquot stat counters");
2623 }
Dmitry Monakhovdde95882010-04-26 20:03:33 +04002624
Linus Torvalds1da177e2005-04-16 15:20:36 -07002625 /* Find power-of-two hlist_heads which can fit into allocation */
2626 nr_hash = (1UL << order) * PAGE_SIZE / sizeof(struct hlist_head);
2627 dq_hash_bits = 0;
2628 do {
2629 dq_hash_bits++;
2630 } while (nr_hash >> dq_hash_bits);
2631 dq_hash_bits--;
2632
2633 nr_hash = 1UL << dq_hash_bits;
2634 dq_hash_mask = nr_hash - 1;
2635 for (i = 0; i < nr_hash; i++)
2636 INIT_HLIST_HEAD(dquot_hash + i);
2637
2638 printk("Dquot-cache hash table entries: %ld (order %ld, %ld bytes)\n",
2639 nr_hash, order, (PAGE_SIZE << order));
2640
Rusty Russell8e1f9362007-07-17 04:03:17 -07002641 register_shrinker(&dqcache_shrinker);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002642
Linus Torvalds1da177e2005-04-16 15:20:36 -07002643 return 0;
2644}
2645module_init(dquot_init);