blob: ae766056350d181bbc83718372d1928313160682 [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
85 * and quota formats, dqstats structure containing statistics about the lists
86 * 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 dqstats.free_dquots++;
279}
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);
286 dqstats.free_dquots--;
287}
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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 dqstats.allocated_dquots++;
295}
296
297static inline void remove_inuse(struct dquot *dquot)
298{
299 dqstats.allocated_dquots--;
300 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);
564 dqstats.lookups++;
565 spin_unlock(&dq_list_lock);
566 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 Hellwig5fb324a2010-02-16 03:44:52 -0500583int vfs_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);
610 dqstats.lookups++;
611 spin_unlock(&dq_list_lock);
612 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);
623 spin_lock(&dq_list_lock);
624 dqstats.syncs++;
625 spin_unlock(&dq_list_lock);
Ingo Molnard3be9152006-03-23 03:00:29 -0800626 mutex_unlock(&dqopt->dqonoff_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627
Christoph Hellwig5fb324a2010-02-16 03:44:52 -0500628 if (!wait || (sb_dqopt(sb)->flags & DQUOT_QUOTA_SYS_FILE))
629 return 0;
630
631 /* This is not very clever (and fast) but currently I don't know about
632 * any other simple way of getting quota data to disk and we must get
633 * them there for userspace to be visible... */
634 if (sb->s_op->sync_fs)
635 sb->s_op->sync_fs(sb, 1);
636 sync_blockdev(sb->s_bdev);
637
638 /*
639 * Now when everything is written we can discard the pagecache so
640 * that userspace sees the changes.
641 */
642 mutex_lock(&sb_dqopt(sb)->dqonoff_mutex);
643 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
644 if (type != -1 && cnt != type)
645 continue;
646 if (!sb_has_quota_active(sb, cnt))
647 continue;
648 mutex_lock_nested(&sb_dqopt(sb)->files[cnt]->i_mutex,
649 I_MUTEX_QUOTA);
650 truncate_inode_pages(&sb_dqopt(sb)->files[cnt]->i_data, 0);
651 mutex_unlock(&sb_dqopt(sb)->files[cnt]->i_mutex);
652 }
653 mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
654
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 return 0;
656}
Mingming Cao08d03502009-01-14 16:19:32 +0100657EXPORT_SYMBOL(vfs_quota_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658
659/* Free unused dquots from cache */
660static void prune_dqcache(int count)
661{
662 struct list_head *head;
663 struct dquot *dquot;
664
665 head = free_dquots.prev;
666 while (head != &free_dquots && count) {
667 dquot = list_entry(head, struct dquot, dq_free);
668 remove_dquot_hash(dquot);
669 remove_free_dquot(dquot);
670 remove_inuse(dquot);
Jan Kara74f783a2008-08-19 14:51:22 +0200671 do_destroy_dquot(dquot);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 count--;
673 head = free_dquots.prev;
674 }
675}
676
677/*
678 * This is called from kswapd when we think we need some
679 * more memory
680 */
681
Al Viro27496a82005-10-21 03:20:48 -0400682static int shrink_dqcache_memory(int nr, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683{
684 if (nr) {
685 spin_lock(&dq_list_lock);
686 prune_dqcache(nr);
687 spin_unlock(&dq_list_lock);
688 }
689 return (dqstats.free_dquots / 100) * sysctl_vfs_cache_pressure;
690}
691
Rusty Russell8e1f9362007-07-17 04:03:17 -0700692static struct shrinker dqcache_shrinker = {
693 .shrink = shrink_dqcache_memory,
694 .seeks = DEFAULT_SEEKS,
695};
696
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697/*
698 * Put reference to dquot
699 * NOTE: If you change this function please check whether dqput_blocks() works right...
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 */
Jan Kara3d9ea252008-10-10 16:12:23 +0200701void dqput(struct dquot *dquot)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702{
Jan Karab48d3802008-07-25 01:46:49 -0700703 int ret;
704
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 if (!dquot)
706 return;
Jan Kara62af9b52010-04-19 16:47:20 +0200707#ifdef CONFIG_QUOTA_DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 if (!atomic_read(&dquot->dq_count)) {
709 printk("VFS: dqput: trying to free free dquot\n");
710 printk("VFS: device %s, dquot of %s %d\n",
711 dquot->dq_sb->s_id,
712 quotatypes[dquot->dq_type],
713 dquot->dq_id);
714 BUG();
715 }
716#endif
717
718 spin_lock(&dq_list_lock);
719 dqstats.drops++;
720 spin_unlock(&dq_list_lock);
721we_slept:
722 spin_lock(&dq_list_lock);
723 if (atomic_read(&dquot->dq_count) > 1) {
724 /* We have more than one user... nothing to do */
725 atomic_dec(&dquot->dq_count);
Jan Kara6362e4d2006-03-23 03:00:17 -0800726 /* Releasing dquot during quotaoff phase? */
Jan Karaf55abc02008-08-20 17:50:32 +0200727 if (!sb_has_quota_active(dquot->dq_sb, dquot->dq_type) &&
Jan Kara6362e4d2006-03-23 03:00:17 -0800728 atomic_read(&dquot->dq_count) == 1)
729 wake_up(&dquot->dq_wait_unused);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 spin_unlock(&dq_list_lock);
731 return;
732 }
733 /* Need to release dquot? */
734 if (test_bit(DQ_ACTIVE_B, &dquot->dq_flags) && dquot_dirty(dquot)) {
735 spin_unlock(&dq_list_lock);
736 /* Commit dquot before releasing */
Jan Karab48d3802008-07-25 01:46:49 -0700737 ret = dquot->dq_sb->dq_op->write_dquot(dquot);
738 if (ret < 0) {
739 printk(KERN_ERR "VFS: cannot write quota structure on "
740 "device %s (error %d). Quota may get out of "
741 "sync!\n", dquot->dq_sb->s_id, ret);
742 /*
743 * We clear dirty bit anyway, so that we avoid
744 * infinite loop here
745 */
746 spin_lock(&dq_list_lock);
747 clear_dquot_dirty(dquot);
748 spin_unlock(&dq_list_lock);
749 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 goto we_slept;
751 }
752 /* Clear flag in case dquot was inactive (something bad happened) */
753 clear_dquot_dirty(dquot);
754 if (test_bit(DQ_ACTIVE_B, &dquot->dq_flags)) {
755 spin_unlock(&dq_list_lock);
756 dquot->dq_sb->dq_op->release_dquot(dquot);
757 goto we_slept;
758 }
759 atomic_dec(&dquot->dq_count);
Jan Kara62af9b52010-04-19 16:47:20 +0200760#ifdef CONFIG_QUOTA_DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 /* sanity check */
Eric Sesterhenn8abf6a42006-04-02 13:36:13 +0200762 BUG_ON(!list_empty(&dquot->dq_free));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763#endif
764 put_dquot_last(dquot);
765 spin_unlock(&dq_list_lock);
766}
Mingming Cao08d03502009-01-14 16:19:32 +0100767EXPORT_SYMBOL(dqput);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768
Jan Kara7d9056b2008-11-25 15:31:32 +0100769struct dquot *dquot_alloc(struct super_block *sb, int type)
Jan Kara74f783a2008-08-19 14:51:22 +0200770{
771 return kmem_cache_zalloc(dquot_cachep, GFP_NOFS);
772}
Jan Kara7d9056b2008-11-25 15:31:32 +0100773EXPORT_SYMBOL(dquot_alloc);
Jan Kara74f783a2008-08-19 14:51:22 +0200774
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775static struct dquot *get_empty_dquot(struct super_block *sb, int type)
776{
777 struct dquot *dquot;
778
Jan Kara74f783a2008-08-19 14:51:22 +0200779 dquot = sb->dq_op->alloc_dquot(sb, type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 if(!dquot)
Jan Karadd6f3c62009-01-26 16:01:43 +0100781 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782
Ingo Molnard3be9152006-03-23 03:00:29 -0800783 mutex_init(&dquot->dq_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 INIT_LIST_HEAD(&dquot->dq_free);
785 INIT_LIST_HEAD(&dquot->dq_inuse);
786 INIT_HLIST_NODE(&dquot->dq_hash);
787 INIT_LIST_HEAD(&dquot->dq_dirty);
Jan Kara6362e4d2006-03-23 03:00:17 -0800788 init_waitqueue_head(&dquot->dq_wait_unused);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 dquot->dq_sb = sb;
790 dquot->dq_type = type;
791 atomic_set(&dquot->dq_count, 1);
792
793 return dquot;
794}
795
796/*
797 * Get reference to dquot
Jan Karacc334122009-01-12 17:23:05 +0100798 *
799 * Locking is slightly tricky here. We are guarded from parallel quotaoff()
800 * destroying our dquot by:
801 * a) checking for quota flags under dq_list_lock and
802 * b) getting a reference to dquot before we release dq_list_lock
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 */
Jan Kara3d9ea252008-10-10 16:12:23 +0200804struct dquot *dqget(struct super_block *sb, unsigned int id, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805{
806 unsigned int hashent = hashfn(sb, id, type);
Jan Karadd6f3c62009-01-26 16:01:43 +0100807 struct dquot *dquot = NULL, *empty = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808
Jan Karaf55abc02008-08-20 17:50:32 +0200809 if (!sb_has_quota_active(sb, type))
Jan Karadd6f3c62009-01-26 16:01:43 +0100810 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811we_slept:
812 spin_lock(&dq_list_lock);
Jan Karacc334122009-01-12 17:23:05 +0100813 spin_lock(&dq_state_lock);
814 if (!sb_has_quota_active(sb, type)) {
815 spin_unlock(&dq_state_lock);
816 spin_unlock(&dq_list_lock);
817 goto out;
818 }
819 spin_unlock(&dq_state_lock);
820
Jan Karadd6f3c62009-01-26 16:01:43 +0100821 dquot = find_dquot(hashent, sb, id, type);
822 if (!dquot) {
823 if (!empty) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 spin_unlock(&dq_list_lock);
Jan Karadd6f3c62009-01-26 16:01:43 +0100825 empty = get_empty_dquot(sb, type);
826 if (!empty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 schedule(); /* Try to wait for a moment... */
828 goto we_slept;
829 }
830 dquot = empty;
Jan Karadd6f3c62009-01-26 16:01:43 +0100831 empty = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 dquot->dq_id = id;
833 /* all dquots go on the inuse_list */
834 put_inuse(dquot);
835 /* hash it first so it can be found */
836 insert_dquot_hash(dquot);
837 dqstats.lookups++;
838 spin_unlock(&dq_list_lock);
839 } else {
840 if (!atomic_read(&dquot->dq_count))
841 remove_free_dquot(dquot);
842 atomic_inc(&dquot->dq_count);
843 dqstats.cache_hits++;
844 dqstats.lookups++;
845 spin_unlock(&dq_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 }
Jan Kara268157b2009-01-27 15:47:22 +0100847 /* Wait for dq_lock - after this we know that either dquot_release() is
848 * already finished or it will be canceled due to dq_count > 1 test */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 wait_on_dquot(dquot);
Jan Kara268157b2009-01-27 15:47:22 +0100850 /* Read the dquot / allocate space in quota file */
851 if (!test_bit(DQ_ACTIVE_B, &dquot->dq_flags) &&
852 sb->dq_op->acquire_dquot(dquot) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 dqput(dquot);
Jan Karadd6f3c62009-01-26 16:01:43 +0100854 dquot = NULL;
Jan Karacc334122009-01-12 17:23:05 +0100855 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 }
Jan Kara62af9b52010-04-19 16:47:20 +0200857#ifdef CONFIG_QUOTA_DEBUG
Eric Sesterhenn8abf6a42006-04-02 13:36:13 +0200858 BUG_ON(!dquot->dq_sb); /* Has somebody invalidated entry under us? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859#endif
Jan Karacc334122009-01-12 17:23:05 +0100860out:
861 if (empty)
862 do_destroy_dquot(empty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863
864 return dquot;
865}
Mingming Cao08d03502009-01-14 16:19:32 +0100866EXPORT_SYMBOL(dqget);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867
868static int dqinit_needed(struct inode *inode, int type)
869{
870 int cnt;
871
872 if (IS_NOQUOTA(inode))
873 return 0;
874 if (type != -1)
Jan Karadd6f3c62009-01-26 16:01:43 +0100875 return !inode->i_dquot[type];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
Jan Karadd6f3c62009-01-26 16:01:43 +0100877 if (!inode->i_dquot[cnt])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 return 1;
879 return 0;
880}
881
Ingo Molnard3be9152006-03-23 03:00:29 -0800882/* This routine is guarded by dqonoff_mutex mutex */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883static void add_dquot_ref(struct super_block *sb, int type)
884{
Jan Kara941d2382008-02-06 01:37:36 -0800885 struct inode *inode, *old_inode = NULL;
Jan Kara62af9b52010-04-19 16:47:20 +0200886#ifdef CONFIG_QUOTA_DEBUG
Jan Kara0a5a9c72010-02-09 18:20:39 +0100887 int reserved = 0;
Jan Kara4c5e6c02010-04-06 18:52:47 +0200888#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889
Christoph Hellwigd003fb72007-02-12 00:51:58 -0800890 spin_lock(&inode_lock);
891 list_for_each_entry(inode, &sb->s_inodes, i_sb_list) {
Wu Fengguangb6fac632009-04-02 16:56:34 -0700892 if (inode->i_state & (I_FREEING|I_CLEAR|I_WILL_FREE|I_NEW))
Nick Pigginaabb8fd2009-03-11 13:17:36 -0700893 continue;
Jan Kara62af9b52010-04-19 16:47:20 +0200894#ifdef CONFIG_QUOTA_DEBUG
Jan Kara0a5a9c72010-02-09 18:20:39 +0100895 if (unlikely(inode_get_rsv_space(inode) > 0))
896 reserved = 1;
Jan Kara4c5e6c02010-04-06 18:52:47 +0200897#endif
Christoph Hellwigd003fb72007-02-12 00:51:58 -0800898 if (!atomic_read(&inode->i_writecount))
899 continue;
900 if (!dqinit_needed(inode, type))
901 continue;
Christoph Hellwigd003fb72007-02-12 00:51:58 -0800902
903 __iget(inode);
904 spin_unlock(&inode_lock);
905
Jan Kara941d2382008-02-06 01:37:36 -0800906 iput(old_inode);
Christoph Hellwig871a2932010-03-03 09:05:07 -0500907 __dquot_initialize(inode, type);
Jan Kara941d2382008-02-06 01:37:36 -0800908 /* We hold a reference to 'inode' so it couldn't have been
909 * removed from s_inodes list while we dropped the inode_lock.
910 * We cannot iput the inode now as we can be holding the last
911 * reference and we cannot iput it under inode_lock. So we
912 * keep the reference and iput it later. */
913 old_inode = inode;
914 spin_lock(&inode_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 }
Christoph Hellwigd003fb72007-02-12 00:51:58 -0800916 spin_unlock(&inode_lock);
Jan Kara941d2382008-02-06 01:37:36 -0800917 iput(old_inode);
Jan Kara0a5a9c72010-02-09 18:20:39 +0100918
Jan Kara62af9b52010-04-19 16:47:20 +0200919#ifdef CONFIG_QUOTA_DEBUG
Jan Kara0a5a9c72010-02-09 18:20:39 +0100920 if (reserved) {
921 printk(KERN_WARNING "VFS (%s): Writes happened before quota"
922 " was turned on thus quota information is probably "
923 "inconsistent. Please run quotacheck(8).\n", sb->s_id);
924 }
Jan Kara4c5e6c02010-04-06 18:52:47 +0200925#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926}
927
Jan Kara268157b2009-01-27 15:47:22 +0100928/*
929 * Return 0 if dqput() won't block.
930 * (note that 1 doesn't necessarily mean blocking)
931 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932static inline int dqput_blocks(struct dquot *dquot)
933{
934 if (atomic_read(&dquot->dq_count) <= 1)
935 return 1;
936 return 0;
937}
938
Jan Kara268157b2009-01-27 15:47:22 +0100939/*
940 * Remove references to dquots from inode and add dquot to list for freeing
941 * if we have the last referece to dquot
942 * We can't race with anybody because we hold dqptr_sem for writing...
943 */
Adrian Bunke5f00f42007-05-08 00:27:22 -0700944static int remove_inode_dquot_ref(struct inode *inode, int type,
945 struct list_head *tofree_head)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946{
947 struct dquot *dquot = inode->i_dquot[type];
948
Jan Karadd6f3c62009-01-26 16:01:43 +0100949 inode->i_dquot[type] = NULL;
950 if (dquot) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 if (dqput_blocks(dquot)) {
Jan Kara62af9b52010-04-19 16:47:20 +0200952#ifdef CONFIG_QUOTA_DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 if (atomic_read(&dquot->dq_count) != 1)
954 printk(KERN_WARNING "VFS: Adding dquot with dq_count %d to dispose list.\n", atomic_read(&dquot->dq_count));
955#endif
956 spin_lock(&dq_list_lock);
Jan Kara268157b2009-01-27 15:47:22 +0100957 /* As dquot must have currently users it can't be on
958 * the free list... */
959 list_add(&dquot->dq_free, tofree_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 spin_unlock(&dq_list_lock);
961 return 1;
962 }
963 else
964 dqput(dquot); /* We have guaranteed we won't block */
965 }
966 return 0;
967}
968
Jan Kara268157b2009-01-27 15:47:22 +0100969/*
970 * Free list of dquots
971 * Dquots are removed from inodes and no new references can be got so we are
972 * the only ones holding reference
973 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974static void put_dquot_list(struct list_head *tofree_head)
975{
976 struct list_head *act_head;
977 struct dquot *dquot;
978
979 act_head = tofree_head->next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 while (act_head != tofree_head) {
981 dquot = list_entry(act_head, struct dquot, dq_free);
982 act_head = act_head->next;
Jan Kara268157b2009-01-27 15:47:22 +0100983 /* Remove dquot from the list so we won't have problems... */
984 list_del_init(&dquot->dq_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 dqput(dquot);
986 }
987}
988
Christoph Hellwigfb58b732007-02-12 00:51:57 -0800989static void remove_dquot_ref(struct super_block *sb, int type,
990 struct list_head *tofree_head)
991{
992 struct inode *inode;
993
994 spin_lock(&inode_lock);
995 list_for_each_entry(inode, &sb->s_inodes, i_sb_list) {
Nick Pigginaabb8fd2009-03-11 13:17:36 -0700996 /*
997 * We have to scan also I_NEW inodes because they can already
998 * have quota pointer initialized. Luckily, we need to touch
999 * only quota pointers and these have separate locking
1000 * (dqptr_sem).
1001 */
Christoph Hellwigfb58b732007-02-12 00:51:57 -08001002 if (!IS_NOQUOTA(inode))
1003 remove_inode_dquot_ref(inode, type, tofree_head);
1004 }
1005 spin_unlock(&inode_lock);
1006}
1007
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008/* Gather all references from inodes and drop them */
1009static void drop_dquot_ref(struct super_block *sb, int type)
1010{
1011 LIST_HEAD(tofree_head);
1012
Christoph Hellwigfb58b732007-02-12 00:51:57 -08001013 if (sb->dq_op) {
1014 down_write(&sb_dqopt(sb)->dqptr_sem);
1015 remove_dquot_ref(sb, type, &tofree_head);
1016 up_write(&sb_dqopt(sb)->dqptr_sem);
1017 put_dquot_list(&tofree_head);
1018 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019}
1020
Jan Kara12095462008-08-20 14:45:12 +02001021static inline void dquot_incr_inodes(struct dquot *dquot, qsize_t number)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022{
1023 dquot->dq_dqb.dqb_curinodes += number;
1024}
1025
1026static inline void dquot_incr_space(struct dquot *dquot, qsize_t number)
1027{
1028 dquot->dq_dqb.dqb_curspace += number;
1029}
1030
Mingming Caof18df222009-01-13 16:43:09 +01001031static inline void dquot_resv_space(struct dquot *dquot, qsize_t number)
1032{
1033 dquot->dq_dqb.dqb_rsvspace += number;
1034}
1035
Mingming Cao740d9dc2009-01-13 16:43:14 +01001036/*
1037 * Claim reserved quota space
1038 */
Jan Kara0a5a9c72010-02-09 18:20:39 +01001039static void dquot_claim_reserved_space(struct dquot *dquot, qsize_t number)
Mingming Cao740d9dc2009-01-13 16:43:14 +01001040{
Jan Kara0a5a9c72010-02-09 18:20:39 +01001041 if (dquot->dq_dqb.dqb_rsvspace < number) {
1042 WARN_ON_ONCE(1);
1043 number = dquot->dq_dqb.dqb_rsvspace;
1044 }
Mingming Cao740d9dc2009-01-13 16:43:14 +01001045 dquot->dq_dqb.dqb_curspace += number;
1046 dquot->dq_dqb.dqb_rsvspace -= number;
1047}
1048
1049static inline
1050void dquot_free_reserved_space(struct dquot *dquot, qsize_t number)
1051{
Jan Kara0a5a9c72010-02-09 18:20:39 +01001052 if (dquot->dq_dqb.dqb_rsvspace >= number)
1053 dquot->dq_dqb.dqb_rsvspace -= number;
1054 else {
1055 WARN_ON_ONCE(1);
1056 dquot->dq_dqb.dqb_rsvspace = 0;
1057 }
Mingming Cao740d9dc2009-01-13 16:43:14 +01001058}
1059
Jan Kara7a2435d2009-01-27 01:47:11 +01001060static void dquot_decr_inodes(struct dquot *dquot, qsize_t number)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061{
Jan Karadb49d2d2008-10-01 18:21:39 +02001062 if (sb_dqopt(dquot->dq_sb)->flags & DQUOT_NEGATIVE_USAGE ||
1063 dquot->dq_dqb.dqb_curinodes >= number)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 dquot->dq_dqb.dqb_curinodes -= number;
1065 else
1066 dquot->dq_dqb.dqb_curinodes = 0;
1067 if (dquot->dq_dqb.dqb_curinodes <= dquot->dq_dqb.dqb_isoftlimit)
1068 dquot->dq_dqb.dqb_itime = (time_t) 0;
1069 clear_bit(DQ_INODES_B, &dquot->dq_flags);
1070}
1071
Jan Kara7a2435d2009-01-27 01:47:11 +01001072static void dquot_decr_space(struct dquot *dquot, qsize_t number)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073{
Jan Karadb49d2d2008-10-01 18:21:39 +02001074 if (sb_dqopt(dquot->dq_sb)->flags & DQUOT_NEGATIVE_USAGE ||
1075 dquot->dq_dqb.dqb_curspace >= number)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 dquot->dq_dqb.dqb_curspace -= number;
1077 else
1078 dquot->dq_dqb.dqb_curspace = 0;
Jan Kara12095462008-08-20 14:45:12 +02001079 if (dquot->dq_dqb.dqb_curspace <= dquot->dq_dqb.dqb_bsoftlimit)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 dquot->dq_dqb.dqb_btime = (time_t) 0;
1081 clear_bit(DQ_BLKS_B, &dquot->dq_flags);
1082}
1083
Jan Karac5254602007-12-22 14:03:25 -08001084static int warning_issued(struct dquot *dquot, const int warntype)
1085{
1086 int flag = (warntype == QUOTA_NL_BHARDWARN ||
1087 warntype == QUOTA_NL_BSOFTLONGWARN) ? DQ_BLKS_B :
1088 ((warntype == QUOTA_NL_IHARDWARN ||
1089 warntype == QUOTA_NL_ISOFTLONGWARN) ? DQ_INODES_B : 0);
1090
1091 if (!flag)
1092 return 0;
1093 return test_and_set_bit(flag, &dquot->dq_flags);
1094}
1095
Jan Kara8e893462007-10-16 23:29:31 -07001096#ifdef CONFIG_PRINT_QUOTA_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097static int flag_print_warnings = 1;
1098
Jan Kara7a2435d2009-01-27 01:47:11 +01001099static int need_print_warning(struct dquot *dquot)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100{
1101 if (!flag_print_warnings)
1102 return 0;
1103
1104 switch (dquot->dq_type) {
1105 case USRQUOTA:
David Howellsda9592e2008-11-14 10:39:05 +11001106 return current_fsuid() == dquot->dq_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 case GRPQUOTA:
1108 return in_group_p(dquot->dq_id);
1109 }
1110 return 0;
1111}
1112
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113/* Print warning to user which exceeded quota */
Jan Karac5254602007-12-22 14:03:25 -08001114static void print_warning(struct dquot *dquot, const int warntype)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115{
1116 char *msg = NULL;
Peter Zijlstra24ec8392006-12-08 02:36:04 -08001117 struct tty_struct *tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118
Jan Kara657d3bf2008-07-25 01:46:52 -07001119 if (warntype == QUOTA_NL_IHARDBELOW ||
1120 warntype == QUOTA_NL_ISOFTBELOW ||
1121 warntype == QUOTA_NL_BHARDBELOW ||
1122 warntype == QUOTA_NL_BSOFTBELOW || !need_print_warning(dquot))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 return;
1124
Peter Zijlstra24ec8392006-12-08 02:36:04 -08001125 tty = get_current_tty();
1126 if (!tty)
Alan Cox452a00d2008-10-13 10:39:13 +01001127 return;
Peter Zijlstra24ec8392006-12-08 02:36:04 -08001128 tty_write_message(tty, dquot->dq_sb->s_id);
Jan Kara8e893462007-10-16 23:29:31 -07001129 if (warntype == QUOTA_NL_ISOFTWARN || warntype == QUOTA_NL_BSOFTWARN)
Peter Zijlstra24ec8392006-12-08 02:36:04 -08001130 tty_write_message(tty, ": warning, ");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 else
Peter Zijlstra24ec8392006-12-08 02:36:04 -08001132 tty_write_message(tty, ": write failed, ");
1133 tty_write_message(tty, quotatypes[dquot->dq_type]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 switch (warntype) {
Jan Kara8e893462007-10-16 23:29:31 -07001135 case QUOTA_NL_IHARDWARN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 msg = " file limit reached.\r\n";
1137 break;
Jan Kara8e893462007-10-16 23:29:31 -07001138 case QUOTA_NL_ISOFTLONGWARN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 msg = " file quota exceeded too long.\r\n";
1140 break;
Jan Kara8e893462007-10-16 23:29:31 -07001141 case QUOTA_NL_ISOFTWARN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 msg = " file quota exceeded.\r\n";
1143 break;
Jan Kara8e893462007-10-16 23:29:31 -07001144 case QUOTA_NL_BHARDWARN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 msg = " block limit reached.\r\n";
1146 break;
Jan Kara8e893462007-10-16 23:29:31 -07001147 case QUOTA_NL_BSOFTLONGWARN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 msg = " block quota exceeded too long.\r\n";
1149 break;
Jan Kara8e893462007-10-16 23:29:31 -07001150 case QUOTA_NL_BSOFTWARN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 msg = " block quota exceeded.\r\n";
1152 break;
1153 }
Peter Zijlstra24ec8392006-12-08 02:36:04 -08001154 tty_write_message(tty, msg);
Alan Cox452a00d2008-10-13 10:39:13 +01001155 tty_kref_put(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156}
Jan Kara8e893462007-10-16 23:29:31 -07001157#endif
1158
Mingming Caof18df222009-01-13 16:43:09 +01001159/*
1160 * Write warnings to the console and send warning messages over netlink.
1161 *
1162 * Note that this function can sleep.
1163 */
Jan Kara7a2435d2009-01-27 01:47:11 +01001164static void flush_warnings(struct dquot *const *dquots, char *warntype)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165{
Steven Whitehouse86e931a2009-09-28 12:35:17 +01001166 struct dquot *dq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 int i;
1168
Steven Whitehouse86e931a2009-09-28 12:35:17 +01001169 for (i = 0; i < MAXQUOTAS; i++) {
1170 dq = dquots[i];
1171 if (dq && warntype[i] != QUOTA_NL_NOWARN &&
1172 !warning_issued(dq, warntype[i])) {
Jan Kara8e893462007-10-16 23:29:31 -07001173#ifdef CONFIG_PRINT_QUOTA_WARNING
Steven Whitehouse86e931a2009-09-28 12:35:17 +01001174 print_warning(dq, warntype[i]);
Jan Kara8e893462007-10-16 23:29:31 -07001175#endif
Steven Whitehouse86e931a2009-09-28 12:35:17 +01001176 quota_send_warning(dq->dq_type, dq->dq_id,
1177 dq->dq_sb->s_dev, warntype[i]);
Jan Kara8e893462007-10-16 23:29:31 -07001178 }
Steven Whitehouse86e931a2009-09-28 12:35:17 +01001179 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180}
1181
Jan Kara7a2435d2009-01-27 01:47:11 +01001182static int ignore_hardlimit(struct dquot *dquot)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183{
1184 struct mem_dqinfo *info = &sb_dqopt(dquot->dq_sb)->info[dquot->dq_type];
1185
1186 return capable(CAP_SYS_RESOURCE) &&
Jan Kara268157b2009-01-27 15:47:22 +01001187 (info->dqi_format->qf_fmt_id != QFMT_VFS_OLD ||
1188 !(info->dqi_flags & V1_DQF_RSQUASH));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189}
1190
1191/* needs dq_data_lock */
Jan Kara12095462008-08-20 14:45:12 +02001192static int check_idq(struct dquot *dquot, qsize_t inodes, char *warntype)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193{
Jan Kara268157b2009-01-27 15:47:22 +01001194 qsize_t newinodes = dquot->dq_dqb.dqb_curinodes + inodes;
1195
Jan Kara8e893462007-10-16 23:29:31 -07001196 *warntype = QUOTA_NL_NOWARN;
Jan Karaf55abc02008-08-20 17:50:32 +02001197 if (!sb_has_quota_limits_enabled(dquot->dq_sb, dquot->dq_type) ||
1198 test_bit(DQ_FAKE_B, &dquot->dq_flags))
Christoph Hellwigefd8f0e2010-03-03 09:05:08 -05001199 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200
1201 if (dquot->dq_dqb.dqb_ihardlimit &&
Jan Kara268157b2009-01-27 15:47:22 +01001202 newinodes > dquot->dq_dqb.dqb_ihardlimit &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 !ignore_hardlimit(dquot)) {
Jan Kara8e893462007-10-16 23:29:31 -07001204 *warntype = QUOTA_NL_IHARDWARN;
Christoph Hellwigefd8f0e2010-03-03 09:05:08 -05001205 return -EDQUOT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 }
1207
1208 if (dquot->dq_dqb.dqb_isoftlimit &&
Jan Kara268157b2009-01-27 15:47:22 +01001209 newinodes > dquot->dq_dqb.dqb_isoftlimit &&
1210 dquot->dq_dqb.dqb_itime &&
1211 get_seconds() >= dquot->dq_dqb.dqb_itime &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 !ignore_hardlimit(dquot)) {
Jan Kara8e893462007-10-16 23:29:31 -07001213 *warntype = QUOTA_NL_ISOFTLONGWARN;
Christoph Hellwigefd8f0e2010-03-03 09:05:08 -05001214 return -EDQUOT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 }
1216
1217 if (dquot->dq_dqb.dqb_isoftlimit &&
Jan Kara268157b2009-01-27 15:47:22 +01001218 newinodes > dquot->dq_dqb.dqb_isoftlimit &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 dquot->dq_dqb.dqb_itime == 0) {
Jan Kara8e893462007-10-16 23:29:31 -07001220 *warntype = QUOTA_NL_ISOFTWARN;
Jan Kara268157b2009-01-27 15:47:22 +01001221 dquot->dq_dqb.dqb_itime = get_seconds() +
1222 sb_dqopt(dquot->dq_sb)->info[dquot->dq_type].dqi_igrace;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 }
1224
Christoph Hellwigefd8f0e2010-03-03 09:05:08 -05001225 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226}
1227
1228/* needs dq_data_lock */
1229static int check_bdq(struct dquot *dquot, qsize_t space, int prealloc, char *warntype)
1230{
Mingming Caof18df222009-01-13 16:43:09 +01001231 qsize_t tspace;
Jan Kara268157b2009-01-27 15:47:22 +01001232 struct super_block *sb = dquot->dq_sb;
Mingming Caof18df222009-01-13 16:43:09 +01001233
Jan Kara8e893462007-10-16 23:29:31 -07001234 *warntype = QUOTA_NL_NOWARN;
Jan Kara268157b2009-01-27 15:47:22 +01001235 if (!sb_has_quota_limits_enabled(sb, dquot->dq_type) ||
Jan Karaf55abc02008-08-20 17:50:32 +02001236 test_bit(DQ_FAKE_B, &dquot->dq_flags))
Christoph Hellwigefd8f0e2010-03-03 09:05:08 -05001237 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238
Mingming Caof18df222009-01-13 16:43:09 +01001239 tspace = dquot->dq_dqb.dqb_curspace + dquot->dq_dqb.dqb_rsvspace
1240 + space;
1241
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 if (dquot->dq_dqb.dqb_bhardlimit &&
Mingming Caof18df222009-01-13 16:43:09 +01001243 tspace > dquot->dq_dqb.dqb_bhardlimit &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 !ignore_hardlimit(dquot)) {
1245 if (!prealloc)
Jan Kara8e893462007-10-16 23:29:31 -07001246 *warntype = QUOTA_NL_BHARDWARN;
Christoph Hellwigefd8f0e2010-03-03 09:05:08 -05001247 return -EDQUOT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 }
1249
1250 if (dquot->dq_dqb.dqb_bsoftlimit &&
Mingming Caof18df222009-01-13 16:43:09 +01001251 tspace > dquot->dq_dqb.dqb_bsoftlimit &&
Jan Kara268157b2009-01-27 15:47:22 +01001252 dquot->dq_dqb.dqb_btime &&
1253 get_seconds() >= dquot->dq_dqb.dqb_btime &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 !ignore_hardlimit(dquot)) {
1255 if (!prealloc)
Jan Kara8e893462007-10-16 23:29:31 -07001256 *warntype = QUOTA_NL_BSOFTLONGWARN;
Christoph Hellwigefd8f0e2010-03-03 09:05:08 -05001257 return -EDQUOT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 }
1259
1260 if (dquot->dq_dqb.dqb_bsoftlimit &&
Mingming Caof18df222009-01-13 16:43:09 +01001261 tspace > dquot->dq_dqb.dqb_bsoftlimit &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 dquot->dq_dqb.dqb_btime == 0) {
1263 if (!prealloc) {
Jan Kara8e893462007-10-16 23:29:31 -07001264 *warntype = QUOTA_NL_BSOFTWARN;
Jan Kara268157b2009-01-27 15:47:22 +01001265 dquot->dq_dqb.dqb_btime = get_seconds() +
1266 sb_dqopt(sb)->info[dquot->dq_type].dqi_bgrace;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 }
1268 else
1269 /*
1270 * We don't allow preallocation to exceed softlimit so exceeding will
1271 * be always printed
1272 */
Christoph Hellwigefd8f0e2010-03-03 09:05:08 -05001273 return -EDQUOT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 }
1275
Christoph Hellwigefd8f0e2010-03-03 09:05:08 -05001276 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277}
1278
Jan Kara12095462008-08-20 14:45:12 +02001279static int info_idq_free(struct dquot *dquot, qsize_t inodes)
Jan Kara657d3bf2008-07-25 01:46:52 -07001280{
Jan Kara268157b2009-01-27 15:47:22 +01001281 qsize_t newinodes;
1282
Jan Kara657d3bf2008-07-25 01:46:52 -07001283 if (test_bit(DQ_FAKE_B, &dquot->dq_flags) ||
Jan Karaf55abc02008-08-20 17:50:32 +02001284 dquot->dq_dqb.dqb_curinodes <= dquot->dq_dqb.dqb_isoftlimit ||
1285 !sb_has_quota_limits_enabled(dquot->dq_sb, dquot->dq_type))
Jan Kara657d3bf2008-07-25 01:46:52 -07001286 return QUOTA_NL_NOWARN;
1287
Jan Kara268157b2009-01-27 15:47:22 +01001288 newinodes = dquot->dq_dqb.dqb_curinodes - inodes;
1289 if (newinodes <= dquot->dq_dqb.dqb_isoftlimit)
Jan Kara657d3bf2008-07-25 01:46:52 -07001290 return QUOTA_NL_ISOFTBELOW;
1291 if (dquot->dq_dqb.dqb_curinodes >= dquot->dq_dqb.dqb_ihardlimit &&
Jan Kara268157b2009-01-27 15:47:22 +01001292 newinodes < dquot->dq_dqb.dqb_ihardlimit)
Jan Kara657d3bf2008-07-25 01:46:52 -07001293 return QUOTA_NL_IHARDBELOW;
1294 return QUOTA_NL_NOWARN;
1295}
1296
1297static int info_bdq_free(struct dquot *dquot, qsize_t space)
1298{
1299 if (test_bit(DQ_FAKE_B, &dquot->dq_flags) ||
Jan Kara12095462008-08-20 14:45:12 +02001300 dquot->dq_dqb.dqb_curspace <= dquot->dq_dqb.dqb_bsoftlimit)
Jan Kara657d3bf2008-07-25 01:46:52 -07001301 return QUOTA_NL_NOWARN;
1302
Jan Kara12095462008-08-20 14:45:12 +02001303 if (dquot->dq_dqb.dqb_curspace - space <= dquot->dq_dqb.dqb_bsoftlimit)
Jan Kara657d3bf2008-07-25 01:46:52 -07001304 return QUOTA_NL_BSOFTBELOW;
Jan Kara12095462008-08-20 14:45:12 +02001305 if (dquot->dq_dqb.dqb_curspace >= dquot->dq_dqb.dqb_bhardlimit &&
1306 dquot->dq_dqb.dqb_curspace - space < dquot->dq_dqb.dqb_bhardlimit)
Jan Kara657d3bf2008-07-25 01:46:52 -07001307 return QUOTA_NL_BHARDBELOW;
1308 return QUOTA_NL_NOWARN;
1309}
Jan Kara0a5a9c72010-02-09 18:20:39 +01001310
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311/*
Christoph Hellwig871a2932010-03-03 09:05:07 -05001312 * Initialize quota pointers in inode
1313 *
1314 * We do things in a bit complicated way but by that we avoid calling
1315 * dqget() and thus filesystem callbacks under dqptr_sem.
1316 *
1317 * It is better to call this function outside of any transaction as it
1318 * might need a lot of space in journal for dquot structure allocation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 */
Christoph Hellwig871a2932010-03-03 09:05:07 -05001320static void __dquot_initialize(struct inode *inode, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321{
1322 unsigned int id = 0;
Christoph Hellwig871a2932010-03-03 09:05:07 -05001323 int cnt;
Dmitry Monakhovad1e6e82010-02-16 08:31:49 +03001324 struct dquot *got[MAXQUOTAS];
Jan Karacc334122009-01-12 17:23:05 +01001325 struct super_block *sb = inode->i_sb;
Jan Kara0a5a9c72010-02-09 18:20:39 +01001326 qsize_t rsv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327
Ingo Molnard3be9152006-03-23 03:00:29 -08001328 /* First test before acquiring mutex - solves deadlocks when we
1329 * re-enter the quota code and are already holding the mutex */
Christoph Hellwig871a2932010-03-03 09:05:07 -05001330 if (!sb_any_quota_active(inode->i_sb) || IS_NOQUOTA(inode))
1331 return;
Jan Karacc334122009-01-12 17:23:05 +01001332
1333 /* First get references to structures we might need. */
1334 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
Dmitry Monakhovad1e6e82010-02-16 08:31:49 +03001335 got[cnt] = NULL;
Jan Karacc334122009-01-12 17:23:05 +01001336 if (type != -1 && cnt != type)
1337 continue;
1338 switch (cnt) {
1339 case USRQUOTA:
1340 id = inode->i_uid;
1341 break;
1342 case GRPQUOTA:
1343 id = inode->i_gid;
1344 break;
1345 }
1346 got[cnt] = dqget(sb, id, cnt);
1347 }
1348
1349 down_write(&sb_dqopt(sb)->dqptr_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 if (IS_NOQUOTA(inode))
1351 goto out_err;
1352 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1353 if (type != -1 && cnt != type)
1354 continue;
Jan Karacc334122009-01-12 17:23:05 +01001355 /* Avoid races with quotaoff() */
1356 if (!sb_has_quota_active(sb, cnt))
1357 continue;
Jan Karadd6f3c62009-01-26 16:01:43 +01001358 if (!inode->i_dquot[cnt]) {
Jan Karacc334122009-01-12 17:23:05 +01001359 inode->i_dquot[cnt] = got[cnt];
Jan Karadd6f3c62009-01-26 16:01:43 +01001360 got[cnt] = NULL;
Jan Kara0a5a9c72010-02-09 18:20:39 +01001361 /*
1362 * Make quota reservation system happy if someone
1363 * did a write before quota was turned on
1364 */
1365 rsv = inode_get_rsv_space(inode);
1366 if (unlikely(rsv))
1367 dquot_resv_space(inode->i_dquot[cnt], rsv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 }
1369 }
1370out_err:
Jan Karacc334122009-01-12 17:23:05 +01001371 up_write(&sb_dqopt(sb)->dqptr_sem);
1372 /* Drop unused references */
Dmitry Monakhovdc52dd32009-12-14 15:21:15 +03001373 dqput_all(got);
Christoph Hellwig871a2932010-03-03 09:05:07 -05001374}
1375
1376void dquot_initialize(struct inode *inode)
1377{
1378 __dquot_initialize(inode, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379}
Mingming Cao08d03502009-01-14 16:19:32 +01001380EXPORT_SYMBOL(dquot_initialize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381
1382/*
1383 * Release all quotas referenced by inode
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 */
Christoph Hellwig9f754752010-03-03 09:05:05 -05001385static void __dquot_drop(struct inode *inode)
Jan Kara3d9ea252008-10-10 16:12:23 +02001386{
Jan Karacc334122009-01-12 17:23:05 +01001387 int cnt;
1388 struct dquot *put[MAXQUOTAS];
1389
Jan Kara3d9ea252008-10-10 16:12:23 +02001390 down_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
Jan Karacc334122009-01-12 17:23:05 +01001391 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1392 put[cnt] = inode->i_dquot[cnt];
Jan Karadd6f3c62009-01-26 16:01:43 +01001393 inode->i_dquot[cnt] = NULL;
Jan Karacc334122009-01-12 17:23:05 +01001394 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395 up_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
Dmitry Monakhovdc52dd32009-12-14 15:21:15 +03001396 dqput_all(put);
Christoph Hellwig9f754752010-03-03 09:05:05 -05001397}
1398
1399void dquot_drop(struct inode *inode)
1400{
1401 int cnt;
1402
1403 if (IS_NOQUOTA(inode))
1404 return;
1405
1406 /*
1407 * Test before calling to rule out calls from proc and such
1408 * where we are not allowed to block. Note that this is
1409 * actually reliable test even without the lock - the caller
1410 * must assure that nobody can come after the DQUOT_DROP and
1411 * add quota pointers back anyway.
1412 */
1413 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1414 if (inode->i_dquot[cnt])
1415 break;
1416 }
1417
1418 if (cnt < MAXQUOTAS)
1419 __dquot_drop(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420}
Mingming Cao08d03502009-01-14 16:19:32 +01001421EXPORT_SYMBOL(dquot_drop);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422
1423/*
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001424 * inode_reserved_space is managed internally by quota, and protected by
1425 * i_lock similar to i_blocks+i_bytes.
1426 */
1427static qsize_t *inode_reserved_space(struct inode * inode)
1428{
1429 /* Filesystem must explicitly define it's own method in order to use
1430 * quota reservation interface */
1431 BUG_ON(!inode->i_sb->dq_op->get_reserved_space);
1432 return inode->i_sb->dq_op->get_reserved_space(inode);
1433}
1434
Dmitry Monakhovc4690702010-02-09 17:53:36 +01001435void inode_add_rsv_space(struct inode *inode, qsize_t number)
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001436{
1437 spin_lock(&inode->i_lock);
1438 *inode_reserved_space(inode) += number;
1439 spin_unlock(&inode->i_lock);
1440}
Dmitry Monakhovc4690702010-02-09 17:53:36 +01001441EXPORT_SYMBOL(inode_add_rsv_space);
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001442
Dmitry Monakhovc4690702010-02-09 17:53:36 +01001443void inode_claim_rsv_space(struct inode *inode, qsize_t number)
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001444{
1445 spin_lock(&inode->i_lock);
1446 *inode_reserved_space(inode) -= number;
1447 __inode_add_bytes(inode, number);
1448 spin_unlock(&inode->i_lock);
1449}
Dmitry Monakhovc4690702010-02-09 17:53:36 +01001450EXPORT_SYMBOL(inode_claim_rsv_space);
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001451
Dmitry Monakhovc4690702010-02-09 17:53:36 +01001452void inode_sub_rsv_space(struct inode *inode, qsize_t number)
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001453{
1454 spin_lock(&inode->i_lock);
1455 *inode_reserved_space(inode) -= number;
1456 spin_unlock(&inode->i_lock);
1457}
Dmitry Monakhovc4690702010-02-09 17:53:36 +01001458EXPORT_SYMBOL(inode_sub_rsv_space);
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001459
1460static qsize_t inode_get_rsv_space(struct inode *inode)
1461{
1462 qsize_t ret;
Jan Kara05b5d892010-01-06 18:03:36 +01001463
1464 if (!inode->i_sb->dq_op->get_reserved_space)
1465 return 0;
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001466 spin_lock(&inode->i_lock);
1467 ret = *inode_reserved_space(inode);
1468 spin_unlock(&inode->i_lock);
1469 return ret;
1470}
1471
1472static void inode_incr_space(struct inode *inode, qsize_t number,
1473 int reserve)
1474{
1475 if (reserve)
1476 inode_add_rsv_space(inode, number);
1477 else
1478 inode_add_bytes(inode, number);
1479}
1480
1481static void inode_decr_space(struct inode *inode, qsize_t number, int reserve)
1482{
1483 if (reserve)
1484 inode_sub_rsv_space(inode, number);
1485 else
1486 inode_sub_bytes(inode, number);
1487}
1488
1489/*
Christoph Hellwig5dd40562010-03-03 09:05:00 -05001490 * This functions updates i_blocks+i_bytes fields and quota information
1491 * (together with appropriate checks).
1492 *
1493 * NOTE: We absolutely rely on the fact that caller dirties the inode
1494 * (usually helpers in quotaops.h care about this) and holds a handle for
1495 * the current transaction so that dquot write and inode write go into the
1496 * same transaction.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 */
1498
1499/*
1500 * This operation can block, but only after everything is updated
1501 */
Mingming Caof18df222009-01-13 16:43:09 +01001502int __dquot_alloc_space(struct inode *inode, qsize_t number,
Christoph Hellwig5dd40562010-03-03 09:05:00 -05001503 int warn, int reserve)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504{
Christoph Hellwig5dd40562010-03-03 09:05:00 -05001505 int cnt, ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506 char warntype[MAXQUOTAS];
1507
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001508 /*
1509 * First test before acquiring mutex - solves deadlocks when we
1510 * re-enter the quota code and are already holding the mutex
1511 */
Christoph Hellwig5dd40562010-03-03 09:05:00 -05001512 if (!sb_any_quota_active(inode->i_sb) || IS_NOQUOTA(inode)) {
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001513 inode_incr_space(inode, number, reserve);
1514 goto out;
1515 }
1516
1517 down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
Jan Kara8e893462007-10-16 23:29:31 -07001519 warntype[cnt] = QUOTA_NL_NOWARN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521 spin_lock(&dq_data_lock);
1522 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
Jan Karadd6f3c62009-01-26 16:01:43 +01001523 if (!inode->i_dquot[cnt])
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524 continue;
Christoph Hellwigefd8f0e2010-03-03 09:05:08 -05001525 ret = check_bdq(inode->i_dquot[cnt], number, !warn,
1526 warntype+cnt);
1527 if (ret) {
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001528 spin_unlock(&dq_data_lock);
1529 goto out_flush_warn;
Mingming Caof18df222009-01-13 16:43:09 +01001530 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531 }
1532 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
Jan Karadd6f3c62009-01-26 16:01:43 +01001533 if (!inode->i_dquot[cnt])
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534 continue;
Mingming Caof18df222009-01-13 16:43:09 +01001535 if (reserve)
1536 dquot_resv_space(inode->i_dquot[cnt], number);
1537 else
1538 dquot_incr_space(inode->i_dquot[cnt], number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539 }
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001540 inode_incr_space(inode, number, reserve);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541 spin_unlock(&dq_data_lock);
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001542
1543 if (reserve)
1544 goto out_flush_warn;
Dmitry Monakhovdc52dd32009-12-14 15:21:15 +03001545 mark_all_dquot_dirty(inode->i_dquot);
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001546out_flush_warn:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547 flush_warnings(inode->i_dquot, warntype);
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001548 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1549out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550 return ret;
1551}
Christoph Hellwig5dd40562010-03-03 09:05:00 -05001552EXPORT_SYMBOL(__dquot_alloc_space);
Mingming Caof18df222009-01-13 16:43:09 +01001553
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554/*
1555 * This operation can block, but only after everything is updated
1556 */
Christoph Hellwig63936dd2010-03-03 09:05:01 -05001557int dquot_alloc_inode(const struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558{
Christoph Hellwigefd8f0e2010-03-03 09:05:08 -05001559 int cnt, ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560 char warntype[MAXQUOTAS];
1561
Ingo Molnard3be9152006-03-23 03:00:29 -08001562 /* First test before acquiring mutex - solves deadlocks when we
1563 * re-enter the quota code and are already holding the mutex */
Christoph Hellwig63936dd2010-03-03 09:05:01 -05001564 if (!sb_any_quota_active(inode->i_sb) || IS_NOQUOTA(inode))
1565 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
Jan Kara8e893462007-10-16 23:29:31 -07001567 warntype[cnt] = QUOTA_NL_NOWARN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568 down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569 spin_lock(&dq_data_lock);
1570 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
Jan Karadd6f3c62009-01-26 16:01:43 +01001571 if (!inode->i_dquot[cnt])
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572 continue;
Christoph Hellwigefd8f0e2010-03-03 09:05:08 -05001573 ret = check_idq(inode->i_dquot[cnt], 1, warntype + cnt);
1574 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575 goto warn_put_all;
1576 }
1577
1578 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
Jan Karadd6f3c62009-01-26 16:01:43 +01001579 if (!inode->i_dquot[cnt])
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580 continue;
Christoph Hellwig63936dd2010-03-03 09:05:01 -05001581 dquot_incr_inodes(inode->i_dquot[cnt], 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582 }
Christoph Hellwigefd8f0e2010-03-03 09:05:08 -05001583
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584warn_put_all:
1585 spin_unlock(&dq_data_lock);
Christoph Hellwig63936dd2010-03-03 09:05:01 -05001586 if (ret == 0)
Dmitry Monakhovdc52dd32009-12-14 15:21:15 +03001587 mark_all_dquot_dirty(inode->i_dquot);
Jan Kara087ee8d2007-12-17 16:20:26 -08001588 flush_warnings(inode->i_dquot, warntype);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1590 return ret;
1591}
Mingming Cao08d03502009-01-14 16:19:32 +01001592EXPORT_SYMBOL(dquot_alloc_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593
Christoph Hellwig5dd40562010-03-03 09:05:00 -05001594/*
1595 * Convert in-memory reserved quotas to real consumed quotas
1596 */
1597int dquot_claim_space_nodirty(struct inode *inode, qsize_t number)
Mingming Cao740d9dc2009-01-13 16:43:14 +01001598{
1599 int cnt;
Mingming Cao740d9dc2009-01-13 16:43:14 +01001600
Christoph Hellwig5dd40562010-03-03 09:05:00 -05001601 if (!sb_any_quota_active(inode->i_sb) || IS_NOQUOTA(inode)) {
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001602 inode_claim_rsv_space(inode, number);
Christoph Hellwig5dd40562010-03-03 09:05:00 -05001603 return 0;
Mingming Cao740d9dc2009-01-13 16:43:14 +01001604 }
1605
1606 down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
Mingming Cao740d9dc2009-01-13 16:43:14 +01001607 spin_lock(&dq_data_lock);
1608 /* Claim reserved quotas to allocated quotas */
1609 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
Jan Karadd6f3c62009-01-26 16:01:43 +01001610 if (inode->i_dquot[cnt])
Mingming Cao740d9dc2009-01-13 16:43:14 +01001611 dquot_claim_reserved_space(inode->i_dquot[cnt],
1612 number);
1613 }
1614 /* Update inode bytes */
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001615 inode_claim_rsv_space(inode, number);
Mingming Cao740d9dc2009-01-13 16:43:14 +01001616 spin_unlock(&dq_data_lock);
Dmitry Monakhovdc52dd32009-12-14 15:21:15 +03001617 mark_all_dquot_dirty(inode->i_dquot);
Mingming Cao740d9dc2009-01-13 16:43:14 +01001618 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
Christoph Hellwig5dd40562010-03-03 09:05:00 -05001619 return 0;
Mingming Cao740d9dc2009-01-13 16:43:14 +01001620}
Christoph Hellwig5dd40562010-03-03 09:05:00 -05001621EXPORT_SYMBOL(dquot_claim_space_nodirty);
Mingming Cao740d9dc2009-01-13 16:43:14 +01001622
1623/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624 * This operation can block, but only after everything is updated
1625 */
Christoph Hellwig5dd40562010-03-03 09:05:00 -05001626void __dquot_free_space(struct inode *inode, qsize_t number, int reserve)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627{
1628 unsigned int cnt;
Jan Kara657d3bf2008-07-25 01:46:52 -07001629 char warntype[MAXQUOTAS];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630
Ingo Molnard3be9152006-03-23 03:00:29 -08001631 /* First test before acquiring mutex - solves deadlocks when we
1632 * re-enter the quota code and are already holding the mutex */
Christoph Hellwig5dd40562010-03-03 09:05:00 -05001633 if (!sb_any_quota_active(inode->i_sb) || IS_NOQUOTA(inode)) {
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001634 inode_decr_space(inode, number, reserve);
Christoph Hellwig5dd40562010-03-03 09:05:00 -05001635 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636 }
Jan Kara657d3bf2008-07-25 01:46:52 -07001637
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638 down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639 spin_lock(&dq_data_lock);
1640 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
Jan Karadd6f3c62009-01-26 16:01:43 +01001641 if (!inode->i_dquot[cnt])
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642 continue;
Jan Kara657d3bf2008-07-25 01:46:52 -07001643 warntype[cnt] = info_bdq_free(inode->i_dquot[cnt], number);
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001644 if (reserve)
1645 dquot_free_reserved_space(inode->i_dquot[cnt], number);
1646 else
1647 dquot_decr_space(inode->i_dquot[cnt], number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648 }
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001649 inode_decr_space(inode, number, reserve);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650 spin_unlock(&dq_data_lock);
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001651
1652 if (reserve)
1653 goto out_unlock;
Dmitry Monakhovdc52dd32009-12-14 15:21:15 +03001654 mark_all_dquot_dirty(inode->i_dquot);
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001655out_unlock:
Jan Kara657d3bf2008-07-25 01:46:52 -07001656 flush_warnings(inode->i_dquot, warntype);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658}
Christoph Hellwig5dd40562010-03-03 09:05:00 -05001659EXPORT_SYMBOL(__dquot_free_space);
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001660
1661/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662 * This operation can block, but only after everything is updated
1663 */
Christoph Hellwig63936dd2010-03-03 09:05:01 -05001664void dquot_free_inode(const struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665{
1666 unsigned int cnt;
Jan Kara657d3bf2008-07-25 01:46:52 -07001667 char warntype[MAXQUOTAS];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668
Ingo Molnard3be9152006-03-23 03:00:29 -08001669 /* First test before acquiring mutex - solves deadlocks when we
1670 * re-enter the quota code and are already holding the mutex */
Christoph Hellwig63936dd2010-03-03 09:05:01 -05001671 if (!sb_any_quota_active(inode->i_sb) || IS_NOQUOTA(inode))
1672 return;
Jan Kara657d3bf2008-07-25 01:46:52 -07001673
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674 down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 spin_lock(&dq_data_lock);
1676 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
Jan Karadd6f3c62009-01-26 16:01:43 +01001677 if (!inode->i_dquot[cnt])
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678 continue;
Christoph Hellwig63936dd2010-03-03 09:05:01 -05001679 warntype[cnt] = info_idq_free(inode->i_dquot[cnt], 1);
1680 dquot_decr_inodes(inode->i_dquot[cnt], 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681 }
1682 spin_unlock(&dq_data_lock);
Dmitry Monakhovdc52dd32009-12-14 15:21:15 +03001683 mark_all_dquot_dirty(inode->i_dquot);
Jan Kara657d3bf2008-07-25 01:46:52 -07001684 flush_warnings(inode->i_dquot, warntype);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686}
Mingming Cao08d03502009-01-14 16:19:32 +01001687EXPORT_SYMBOL(dquot_free_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688
1689/*
1690 * Transfer the number of inode and blocks from one diskquota to an other.
1691 *
1692 * This operation can block, but only after everything is updated
1693 * A transaction must be started when entering this function.
1694 */
Christoph Hellwigb43fa822010-03-03 09:05:03 -05001695static int __dquot_transfer(struct inode *inode, qid_t *chid, unsigned long mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696{
Mingming Cao740d9dc2009-01-13 16:43:14 +01001697 qsize_t space, cur_space;
1698 qsize_t rsv_space = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699 struct dquot *transfer_from[MAXQUOTAS];
1700 struct dquot *transfer_to[MAXQUOTAS];
Christoph Hellwigefd8f0e2010-03-03 09:05:08 -05001701 int cnt, ret = 0;
Jan Kara657d3bf2008-07-25 01:46:52 -07001702 char warntype_to[MAXQUOTAS];
1703 char warntype_from_inodes[MAXQUOTAS], warntype_from_space[MAXQUOTAS];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704
Ingo Molnard3be9152006-03-23 03:00:29 -08001705 /* First test before acquiring mutex - solves deadlocks when we
1706 * re-enter the quota code and are already holding the mutex */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707 if (IS_NOQUOTA(inode))
Christoph Hellwigefd8f0e2010-03-03 09:05:08 -05001708 return 0;
Jan Karacc334122009-01-12 17:23:05 +01001709 /* Initialize the arrays */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
Jan Karadd6f3c62009-01-26 16:01:43 +01001711 transfer_from[cnt] = NULL;
1712 transfer_to[cnt] = NULL;
Jan Kara657d3bf2008-07-25 01:46:52 -07001713 warntype_to[cnt] = QUOTA_NL_NOWARN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714 }
Dmitry Monakhov8ddd69d2010-02-16 08:31:50 +03001715 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1716 if (mask & (1 << cnt))
1717 transfer_to[cnt] = dqget(inode->i_sb, chid[cnt], cnt);
1718 }
Jan Karacc334122009-01-12 17:23:05 +01001719 down_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
Jan Karacc334122009-01-12 17:23:05 +01001720 if (IS_NOQUOTA(inode)) { /* File without quota accounting? */
1721 up_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
1722 goto put_all;
1723 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724 spin_lock(&dq_data_lock);
Mingming Cao740d9dc2009-01-13 16:43:14 +01001725 cur_space = inode_get_bytes(inode);
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001726 rsv_space = inode_get_rsv_space(inode);
Mingming Cao740d9dc2009-01-13 16:43:14 +01001727 space = cur_space + rsv_space;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728 /* Build the transfer_from list and check the limits */
1729 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
Jan Karadd6f3c62009-01-26 16:01:43 +01001730 if (!transfer_to[cnt])
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731 continue;
1732 transfer_from[cnt] = inode->i_dquot[cnt];
Christoph Hellwigefd8f0e2010-03-03 09:05:08 -05001733 ret = check_idq(transfer_to[cnt], 1, warntype_to + cnt);
1734 if (ret)
1735 goto over_quota;
1736 ret = check_bdq(transfer_to[cnt], space, 0, warntype_to + cnt);
1737 if (ret)
Jan Karacc334122009-01-12 17:23:05 +01001738 goto over_quota;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739 }
1740
1741 /*
1742 * Finally perform the needed transfer from transfer_from to transfer_to
1743 */
1744 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1745 /*
1746 * Skip changes for same uid or gid or for turned off quota-type.
1747 */
Jan Karadd6f3c62009-01-26 16:01:43 +01001748 if (!transfer_to[cnt])
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749 continue;
1750
1751 /* Due to IO error we might not have transfer_from[] structure */
1752 if (transfer_from[cnt]) {
Jan Kara657d3bf2008-07-25 01:46:52 -07001753 warntype_from_inodes[cnt] =
1754 info_idq_free(transfer_from[cnt], 1);
1755 warntype_from_space[cnt] =
1756 info_bdq_free(transfer_from[cnt], space);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757 dquot_decr_inodes(transfer_from[cnt], 1);
Mingming Cao740d9dc2009-01-13 16:43:14 +01001758 dquot_decr_space(transfer_from[cnt], cur_space);
1759 dquot_free_reserved_space(transfer_from[cnt],
1760 rsv_space);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761 }
1762
1763 dquot_incr_inodes(transfer_to[cnt], 1);
Mingming Cao740d9dc2009-01-13 16:43:14 +01001764 dquot_incr_space(transfer_to[cnt], cur_space);
1765 dquot_resv_space(transfer_to[cnt], rsv_space);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766
1767 inode->i_dquot[cnt] = transfer_to[cnt];
1768 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769 spin_unlock(&dq_data_lock);
Jan Karacc334122009-01-12 17:23:05 +01001770 up_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
1771
Dmitry Monakhovdc52dd32009-12-14 15:21:15 +03001772 mark_all_dquot_dirty(transfer_from);
1773 mark_all_dquot_dirty(transfer_to);
1774 /* The reference we got is transferred to the inode */
1775 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1776 transfer_to[cnt] = NULL;
Jan Karacc334122009-01-12 17:23:05 +01001777warn_put_all:
Jan Kara657d3bf2008-07-25 01:46:52 -07001778 flush_warnings(transfer_to, warntype_to);
1779 flush_warnings(transfer_from, warntype_from_inodes);
1780 flush_warnings(transfer_from, warntype_from_space);
Jan Karacc334122009-01-12 17:23:05 +01001781put_all:
Dmitry Monakhovdc52dd32009-12-14 15:21:15 +03001782 dqput_all(transfer_from);
1783 dqput_all(transfer_to);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784 return ret;
Jan Karacc334122009-01-12 17:23:05 +01001785over_quota:
1786 spin_unlock(&dq_data_lock);
1787 up_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
1788 /* Clear dquot pointers we don't want to dqput() */
1789 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
Jan Karadd6f3c62009-01-26 16:01:43 +01001790 transfer_from[cnt] = NULL;
Jan Karacc334122009-01-12 17:23:05 +01001791 goto warn_put_all;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792}
1793
Dmitry Monakhov8ddd69d2010-02-16 08:31:50 +03001794/* Wrapper for transferring ownership of an inode for uid/gid only
1795 * Called from FSXXX_setattr()
1796 */
Christoph Hellwigb43fa822010-03-03 09:05:03 -05001797int dquot_transfer(struct inode *inode, struct iattr *iattr)
Jan Karab85f4b82008-07-25 01:46:50 -07001798{
Dmitry Monakhov8ddd69d2010-02-16 08:31:50 +03001799 qid_t chid[MAXQUOTAS];
1800 unsigned long mask = 0;
1801
1802 if (iattr->ia_valid & ATTR_UID && iattr->ia_uid != inode->i_uid) {
1803 mask |= 1 << USRQUOTA;
1804 chid[USRQUOTA] = iattr->ia_uid;
1805 }
1806 if (iattr->ia_valid & ATTR_GID && iattr->ia_gid != inode->i_gid) {
1807 mask |= 1 << GRPQUOTA;
1808 chid[GRPQUOTA] = iattr->ia_gid;
1809 }
Jan Karaf55abc02008-08-20 17:50:32 +02001810 if (sb_any_quota_active(inode->i_sb) && !IS_NOQUOTA(inode)) {
Christoph Hellwig871a2932010-03-03 09:05:07 -05001811 dquot_initialize(inode);
Christoph Hellwigefd8f0e2010-03-03 09:05:08 -05001812 return __dquot_transfer(inode, chid, mask);
Jan Karab85f4b82008-07-25 01:46:50 -07001813 }
1814 return 0;
1815}
Christoph Hellwigb43fa822010-03-03 09:05:03 -05001816EXPORT_SYMBOL(dquot_transfer);
Jan Karab85f4b82008-07-25 01:46:50 -07001817
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818/*
1819 * Write info of quota file to disk
1820 */
1821int dquot_commit_info(struct super_block *sb, int type)
1822{
1823 int ret;
1824 struct quota_info *dqopt = sb_dqopt(sb);
1825
Ingo Molnard3be9152006-03-23 03:00:29 -08001826 mutex_lock(&dqopt->dqio_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827 ret = dqopt->ops[type]->write_file_info(sb, type);
Ingo Molnard3be9152006-03-23 03:00:29 -08001828 mutex_unlock(&dqopt->dqio_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829 return ret;
1830}
Mingming Cao08d03502009-01-14 16:19:32 +01001831EXPORT_SYMBOL(dquot_commit_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832
1833/*
1834 * Definitions of diskquota operations.
1835 */
Alexey Dobriyan61e225d2009-09-21 17:01:08 -07001836const struct dquot_operations dquot_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837 .write_dquot = dquot_commit,
1838 .acquire_dquot = dquot_acquire,
1839 .release_dquot = dquot_release,
1840 .mark_dirty = dquot_mark_dquot_dirty,
Jan Kara74f783a2008-08-19 14:51:22 +02001841 .write_info = dquot_commit_info,
1842 .alloc_dquot = dquot_alloc,
1843 .destroy_dquot = dquot_destroy,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844};
1845
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846/*
Christoph Hellwig907f4552010-03-03 09:05:06 -05001847 * Generic helper for ->open on filesystems supporting disk quotas.
1848 */
1849int dquot_file_open(struct inode *inode, struct file *file)
1850{
1851 int error;
1852
1853 error = generic_file_open(inode, file);
1854 if (!error && (file->f_mode & FMODE_WRITE))
Christoph Hellwig871a2932010-03-03 09:05:07 -05001855 dquot_initialize(inode);
Christoph Hellwig907f4552010-03-03 09:05:06 -05001856 return error;
1857}
1858EXPORT_SYMBOL(dquot_file_open);
1859
1860/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861 * Turn quota off on a device. type == -1 ==> quotaoff for all types (umount)
1862 */
Jan Karaf55abc02008-08-20 17:50:32 +02001863int vfs_quota_disable(struct super_block *sb, int type, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864{
Jan Kara0ff5af82008-04-28 02:14:33 -07001865 int cnt, ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866 struct quota_info *dqopt = sb_dqopt(sb);
1867 struct inode *toputinode[MAXQUOTAS];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868
Jan Karaf55abc02008-08-20 17:50:32 +02001869 /* Cannot turn off usage accounting without turning off limits, or
1870 * suspend quotas and simultaneously turn quotas off. */
1871 if ((flags & DQUOT_USAGE_ENABLED && !(flags & DQUOT_LIMITS_ENABLED))
1872 || (flags & DQUOT_SUSPENDED && flags & (DQUOT_LIMITS_ENABLED |
1873 DQUOT_USAGE_ENABLED)))
1874 return -EINVAL;
1875
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876 /* We need to serialize quota_off() for device */
Ingo Molnard3be9152006-03-23 03:00:29 -08001877 mutex_lock(&dqopt->dqonoff_mutex);
Jan Kara9377abd2008-05-12 14:02:08 -07001878
1879 /*
1880 * Skip everything if there's nothing to do. We have to do this because
1881 * sometimes we are called when fill_super() failed and calling
1882 * sync_fs() in such cases does no good.
1883 */
Jan Karaf55abc02008-08-20 17:50:32 +02001884 if (!sb_any_quota_loaded(sb)) {
Jan Kara9377abd2008-05-12 14:02:08 -07001885 mutex_unlock(&dqopt->dqonoff_mutex);
1886 return 0;
1887 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1889 toputinode[cnt] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890 if (type != -1 && cnt != type)
1891 continue;
Jan Karaf55abc02008-08-20 17:50:32 +02001892 if (!sb_has_quota_loaded(sb, cnt))
Jan Kara0ff5af82008-04-28 02:14:33 -07001893 continue;
Jan Karaf55abc02008-08-20 17:50:32 +02001894
1895 if (flags & DQUOT_SUSPENDED) {
Jan Karacc334122009-01-12 17:23:05 +01001896 spin_lock(&dq_state_lock);
Jan Karaf55abc02008-08-20 17:50:32 +02001897 dqopt->flags |=
1898 dquot_state_flag(DQUOT_SUSPENDED, cnt);
Jan Karacc334122009-01-12 17:23:05 +01001899 spin_unlock(&dq_state_lock);
Jan Karaf55abc02008-08-20 17:50:32 +02001900 } else {
Jan Karacc334122009-01-12 17:23:05 +01001901 spin_lock(&dq_state_lock);
Jan Karaf55abc02008-08-20 17:50:32 +02001902 dqopt->flags &= ~dquot_state_flag(flags, cnt);
1903 /* Turning off suspended quotas? */
1904 if (!sb_has_quota_loaded(sb, cnt) &&
1905 sb_has_quota_suspended(sb, cnt)) {
1906 dqopt->flags &= ~dquot_state_flag(
1907 DQUOT_SUSPENDED, cnt);
Jan Karacc334122009-01-12 17:23:05 +01001908 spin_unlock(&dq_state_lock);
Jan Karaf55abc02008-08-20 17:50:32 +02001909 iput(dqopt->files[cnt]);
1910 dqopt->files[cnt] = NULL;
1911 continue;
1912 }
Jan Karacc334122009-01-12 17:23:05 +01001913 spin_unlock(&dq_state_lock);
Jan Kara0ff5af82008-04-28 02:14:33 -07001914 }
Jan Karaf55abc02008-08-20 17:50:32 +02001915
1916 /* We still have to keep quota loaded? */
1917 if (sb_has_quota_loaded(sb, cnt) && !(flags & DQUOT_SUSPENDED))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919
1920 /* Note: these are blocking operations */
1921 drop_dquot_ref(sb, cnt);
1922 invalidate_dquots(sb, cnt);
1923 /*
Jan Kara268157b2009-01-27 15:47:22 +01001924 * Now all dquots should be invalidated, all writes done so we
1925 * should be only users of the info. No locks needed.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926 */
1927 if (info_dirty(&dqopt->info[cnt]))
1928 sb->dq_op->write_info(sb, cnt);
1929 if (dqopt->ops[cnt]->free_file_info)
1930 dqopt->ops[cnt]->free_file_info(sb, cnt);
1931 put_quota_format(dqopt->info[cnt].dqi_format);
1932
1933 toputinode[cnt] = dqopt->files[cnt];
Jan Karaf55abc02008-08-20 17:50:32 +02001934 if (!sb_has_quota_loaded(sb, cnt))
Jan Kara0ff5af82008-04-28 02:14:33 -07001935 dqopt->files[cnt] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936 dqopt->info[cnt].dqi_flags = 0;
1937 dqopt->info[cnt].dqi_igrace = 0;
1938 dqopt->info[cnt].dqi_bgrace = 0;
1939 dqopt->ops[cnt] = NULL;
1940 }
Ingo Molnard3be9152006-03-23 03:00:29 -08001941 mutex_unlock(&dqopt->dqonoff_mutex);
Jan Karaca785ec2008-09-30 17:53:37 +02001942
1943 /* Skip syncing and setting flags if quota files are hidden */
1944 if (dqopt->flags & DQUOT_QUOTA_SYS_FILE)
1945 goto put_inodes;
1946
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947 /* Sync the superblock so that buffers with quota data are written to
Al Viro7b7b1ac2005-11-07 17:13:39 -05001948 * disk (and so userspace sees correct data afterwards). */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949 if (sb->s_op->sync_fs)
1950 sb->s_op->sync_fs(sb, 1);
1951 sync_blockdev(sb->s_bdev);
1952 /* Now the quota files are just ordinary files and we can set the
1953 * inode flags back. Moreover we discard the pagecache so that
1954 * userspace sees the writes we did bypassing the pagecache. We
1955 * must also discard the blockdev buffers so that we see the
1956 * changes done by userspace on the next quotaon() */
1957 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1958 if (toputinode[cnt]) {
Ingo Molnard3be9152006-03-23 03:00:29 -08001959 mutex_lock(&dqopt->dqonoff_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960 /* If quota was reenabled in the meantime, we have
1961 * nothing to do */
Jan Karaf55abc02008-08-20 17:50:32 +02001962 if (!sb_has_quota_loaded(sb, cnt)) {
Jan Kara268157b2009-01-27 15:47:22 +01001963 mutex_lock_nested(&toputinode[cnt]->i_mutex,
1964 I_MUTEX_QUOTA);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965 toputinode[cnt]->i_flags &= ~(S_IMMUTABLE |
1966 S_NOATIME | S_NOQUOTA);
Jan Kara268157b2009-01-27 15:47:22 +01001967 truncate_inode_pages(&toputinode[cnt]->i_data,
1968 0);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001969 mutex_unlock(&toputinode[cnt]->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970 mark_inode_dirty(toputinode[cnt]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971 }
Ingo Molnard3be9152006-03-23 03:00:29 -08001972 mutex_unlock(&dqopt->dqonoff_mutex);
Jan Karaca785ec2008-09-30 17:53:37 +02001973 }
1974 if (sb->s_bdev)
1975 invalidate_bdev(sb->s_bdev);
1976put_inodes:
1977 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1978 if (toputinode[cnt]) {
Jan Kara0ff5af82008-04-28 02:14:33 -07001979 /* On remount RO, we keep the inode pointer so that we
Jan Karaf55abc02008-08-20 17:50:32 +02001980 * can reenable quota on the subsequent remount RW. We
1981 * have to check 'flags' variable and not use sb_has_
1982 * function because another quotaon / quotaoff could
1983 * change global state before we got here. We refuse
1984 * to suspend quotas when there is pending delete on
1985 * the quota file... */
1986 if (!(flags & DQUOT_SUSPENDED))
Jan Kara0ff5af82008-04-28 02:14:33 -07001987 iput(toputinode[cnt]);
1988 else if (!toputinode[cnt]->i_nlink)
1989 ret = -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990 }
Jan Kara0ff5af82008-04-28 02:14:33 -07001991 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992}
Mingming Cao08d03502009-01-14 16:19:32 +01001993EXPORT_SYMBOL(vfs_quota_disable);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994
Jan Karaf55abc02008-08-20 17:50:32 +02001995int vfs_quota_off(struct super_block *sb, int type, int remount)
1996{
1997 return vfs_quota_disable(sb, type, remount ? DQUOT_SUSPENDED :
1998 (DQUOT_USAGE_ENABLED | DQUOT_LIMITS_ENABLED));
1999}
Mingming Cao08d03502009-01-14 16:19:32 +01002000EXPORT_SYMBOL(vfs_quota_off);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001/*
2002 * Turn quotas on on a device
2003 */
2004
Jan Karaf55abc02008-08-20 17:50:32 +02002005/*
2006 * Helper function to turn quotas on when we already have the inode of
2007 * quota file and no quota information is loaded.
2008 */
2009static int vfs_load_quota_inode(struct inode *inode, int type, int format_id,
2010 unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011{
2012 struct quota_format_type *fmt = find_quota_format(format_id);
2013 struct super_block *sb = inode->i_sb;
2014 struct quota_info *dqopt = sb_dqopt(sb);
2015 int error;
2016 int oldflags = -1;
2017
2018 if (!fmt)
2019 return -ESRCH;
2020 if (!S_ISREG(inode->i_mode)) {
2021 error = -EACCES;
2022 goto out_fmt;
2023 }
2024 if (IS_RDONLY(inode)) {
2025 error = -EROFS;
2026 goto out_fmt;
2027 }
2028 if (!sb->s_op->quota_write || !sb->s_op->quota_read) {
2029 error = -EINVAL;
2030 goto out_fmt;
2031 }
Jan Karaf55abc02008-08-20 17:50:32 +02002032 /* Usage always has to be set... */
2033 if (!(flags & DQUOT_USAGE_ENABLED)) {
2034 error = -EINVAL;
2035 goto out_fmt;
2036 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037
Jan Karaca785ec2008-09-30 17:53:37 +02002038 if (!(dqopt->flags & DQUOT_QUOTA_SYS_FILE)) {
Jan Karaab94c392010-02-22 21:07:17 +01002039 /* As we bypass the pagecache we must now flush all the
2040 * dirty data and invalidate caches so that kernel sees
2041 * changes from userspace. It is not enough to just flush
2042 * the quota file since if blocksize < pagesize, invalidation
2043 * of the cache could fail because of other unrelated dirty
2044 * data */
2045 sync_filesystem(sb);
Jan Karaca785ec2008-09-30 17:53:37 +02002046 invalidate_bdev(sb->s_bdev);
2047 }
Ingo Molnard3be9152006-03-23 03:00:29 -08002048 mutex_lock(&dqopt->dqonoff_mutex);
Jan Karaf55abc02008-08-20 17:50:32 +02002049 if (sb_has_quota_loaded(sb, type)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050 error = -EBUSY;
2051 goto out_lock;
2052 }
Jan Karaca785ec2008-09-30 17:53:37 +02002053
2054 if (!(dqopt->flags & DQUOT_QUOTA_SYS_FILE)) {
2055 /* We don't want quota and atime on quota files (deadlocks
2056 * possible) Also nobody should write to the file - we use
2057 * special IO operations which ignore the immutable bit. */
Jan Karadee86562009-07-22 18:12:17 +02002058 mutex_lock_nested(&inode->i_mutex, I_MUTEX_QUOTA);
Jan Kara268157b2009-01-27 15:47:22 +01002059 oldflags = inode->i_flags & (S_NOATIME | S_IMMUTABLE |
2060 S_NOQUOTA);
Jan Karaca785ec2008-09-30 17:53:37 +02002061 inode->i_flags |= S_NOQUOTA | S_NOATIME | S_IMMUTABLE;
Jan Karadee86562009-07-22 18:12:17 +02002062 mutex_unlock(&inode->i_mutex);
Jan Kara26245c92010-01-06 17:20:35 +01002063 /*
2064 * When S_NOQUOTA is set, remove dquot references as no more
2065 * references can be added
2066 */
Christoph Hellwig9f754752010-03-03 09:05:05 -05002067 __dquot_drop(inode);
Jan Karaca785ec2008-09-30 17:53:37 +02002068 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069
2070 error = -EIO;
2071 dqopt->files[type] = igrab(inode);
2072 if (!dqopt->files[type])
2073 goto out_lock;
2074 error = -EINVAL;
2075 if (!fmt->qf_ops->check_quota_file(sb, type))
2076 goto out_file_init;
2077
2078 dqopt->ops[type] = fmt->qf_ops;
2079 dqopt->info[type].dqi_format = fmt;
Jan Kara0ff5af82008-04-28 02:14:33 -07002080 dqopt->info[type].dqi_fmt_id = format_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081 INIT_LIST_HEAD(&dqopt->info[type].dqi_dirty_list);
Ingo Molnard3be9152006-03-23 03:00:29 -08002082 mutex_lock(&dqopt->dqio_mutex);
Jan Kara268157b2009-01-27 15:47:22 +01002083 error = dqopt->ops[type]->read_file_info(sb, type);
2084 if (error < 0) {
Ingo Molnard3be9152006-03-23 03:00:29 -08002085 mutex_unlock(&dqopt->dqio_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086 goto out_file_init;
2087 }
Ingo Molnard3be9152006-03-23 03:00:29 -08002088 mutex_unlock(&dqopt->dqio_mutex);
Jan Karacc334122009-01-12 17:23:05 +01002089 spin_lock(&dq_state_lock);
Jan Karaf55abc02008-08-20 17:50:32 +02002090 dqopt->flags |= dquot_state_flag(flags, type);
Jan Karacc334122009-01-12 17:23:05 +01002091 spin_unlock(&dq_state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092
2093 add_dquot_ref(sb, type);
Ingo Molnard3be9152006-03-23 03:00:29 -08002094 mutex_unlock(&dqopt->dqonoff_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095
2096 return 0;
2097
2098out_file_init:
2099 dqopt->files[type] = NULL;
2100 iput(inode);
2101out_lock:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102 if (oldflags != -1) {
Jan Karadee86562009-07-22 18:12:17 +02002103 mutex_lock_nested(&inode->i_mutex, I_MUTEX_QUOTA);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104 /* Set the flags back (in the case of accidental quotaon()
2105 * on a wrong file we don't want to mess up the flags) */
2106 inode->i_flags &= ~(S_NOATIME | S_NOQUOTA | S_IMMUTABLE);
2107 inode->i_flags |= oldflags;
Jan Karadee86562009-07-22 18:12:17 +02002108 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109 }
Jiaying Zhangd01730d2009-07-07 18:15:21 +02002110 mutex_unlock(&dqopt->dqonoff_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111out_fmt:
2112 put_quota_format(fmt);
2113
2114 return error;
2115}
2116
Jan Kara0ff5af82008-04-28 02:14:33 -07002117/* Reenable quotas on remount RW */
2118static int vfs_quota_on_remount(struct super_block *sb, int type)
2119{
2120 struct quota_info *dqopt = sb_dqopt(sb);
2121 struct inode *inode;
2122 int ret;
Jan Karaf55abc02008-08-20 17:50:32 +02002123 unsigned int flags;
Jan Kara0ff5af82008-04-28 02:14:33 -07002124
2125 mutex_lock(&dqopt->dqonoff_mutex);
2126 if (!sb_has_quota_suspended(sb, type)) {
2127 mutex_unlock(&dqopt->dqonoff_mutex);
2128 return 0;
2129 }
Jan Kara0ff5af82008-04-28 02:14:33 -07002130 inode = dqopt->files[type];
2131 dqopt->files[type] = NULL;
Jan Karacc334122009-01-12 17:23:05 +01002132 spin_lock(&dq_state_lock);
Jan Karaf55abc02008-08-20 17:50:32 +02002133 flags = dqopt->flags & dquot_state_flag(DQUOT_USAGE_ENABLED |
2134 DQUOT_LIMITS_ENABLED, type);
2135 dqopt->flags &= ~dquot_state_flag(DQUOT_STATE_FLAGS, type);
Jan Karacc334122009-01-12 17:23:05 +01002136 spin_unlock(&dq_state_lock);
Jan Kara0ff5af82008-04-28 02:14:33 -07002137 mutex_unlock(&dqopt->dqonoff_mutex);
2138
Jan Karaf55abc02008-08-20 17:50:32 +02002139 flags = dquot_generic_flag(flags, type);
2140 ret = vfs_load_quota_inode(inode, type, dqopt->info[type].dqi_fmt_id,
2141 flags);
Jan Kara0ff5af82008-04-28 02:14:33 -07002142 iput(inode);
2143
2144 return ret;
2145}
2146
Al Viro77e69da2008-08-01 04:29:18 -04002147int vfs_quota_on_path(struct super_block *sb, int type, int format_id,
2148 struct path *path)
2149{
2150 int error = security_quota_on(path->dentry);
2151 if (error)
2152 return error;
2153 /* Quota file not on the same filesystem? */
2154 if (path->mnt->mnt_sb != sb)
2155 error = -EXDEV;
2156 else
Jan Karaf55abc02008-08-20 17:50:32 +02002157 error = vfs_load_quota_inode(path->dentry->d_inode, type,
2158 format_id, DQUOT_USAGE_ENABLED |
2159 DQUOT_LIMITS_ENABLED);
Al Viro77e69da2008-08-01 04:29:18 -04002160 return error;
2161}
Mingming Cao08d03502009-01-14 16:19:32 +01002162EXPORT_SYMBOL(vfs_quota_on_path);
Al Viro77e69da2008-08-01 04:29:18 -04002163
Al Viro82646132008-08-02 00:57:06 -04002164int vfs_quota_on(struct super_block *sb, int type, int format_id, char *name,
Jan Kara0ff5af82008-04-28 02:14:33 -07002165 int remount)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166{
Al Viro82646132008-08-02 00:57:06 -04002167 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168 int error;
2169
Jan Kara0ff5af82008-04-28 02:14:33 -07002170 if (remount)
2171 return vfs_quota_on_remount(sb, type);
2172
Al Viro82646132008-08-02 00:57:06 -04002173 error = kern_path(name, LOOKUP_FOLLOW, &path);
Al Viro77e69da2008-08-01 04:29:18 -04002174 if (!error) {
Al Viro82646132008-08-02 00:57:06 -04002175 error = vfs_quota_on_path(sb, type, format_id, &path);
2176 path_put(&path);
Al Viro77e69da2008-08-01 04:29:18 -04002177 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178 return error;
2179}
Mingming Cao08d03502009-01-14 16:19:32 +01002180EXPORT_SYMBOL(vfs_quota_on);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181
2182/*
Jan Karaf55abc02008-08-20 17:50:32 +02002183 * More powerful function for turning on quotas allowing setting
2184 * of individual quota flags
2185 */
2186int vfs_quota_enable(struct inode *inode, int type, int format_id,
2187 unsigned int flags)
2188{
2189 int ret = 0;
2190 struct super_block *sb = inode->i_sb;
2191 struct quota_info *dqopt = sb_dqopt(sb);
2192
2193 /* Just unsuspend quotas? */
2194 if (flags & DQUOT_SUSPENDED)
2195 return vfs_quota_on_remount(sb, type);
2196 if (!flags)
2197 return 0;
2198 /* Just updating flags needed? */
2199 if (sb_has_quota_loaded(sb, type)) {
2200 mutex_lock(&dqopt->dqonoff_mutex);
2201 /* Now do a reliable test... */
2202 if (!sb_has_quota_loaded(sb, type)) {
2203 mutex_unlock(&dqopt->dqonoff_mutex);
2204 goto load_quota;
2205 }
2206 if (flags & DQUOT_USAGE_ENABLED &&
2207 sb_has_quota_usage_enabled(sb, type)) {
2208 ret = -EBUSY;
2209 goto out_lock;
2210 }
2211 if (flags & DQUOT_LIMITS_ENABLED &&
2212 sb_has_quota_limits_enabled(sb, type)) {
2213 ret = -EBUSY;
2214 goto out_lock;
2215 }
Jan Karacc334122009-01-12 17:23:05 +01002216 spin_lock(&dq_state_lock);
Jan Karaf55abc02008-08-20 17:50:32 +02002217 sb_dqopt(sb)->flags |= dquot_state_flag(flags, type);
Jan Karacc334122009-01-12 17:23:05 +01002218 spin_unlock(&dq_state_lock);
Jan Karaf55abc02008-08-20 17:50:32 +02002219out_lock:
2220 mutex_unlock(&dqopt->dqonoff_mutex);
2221 return ret;
2222 }
2223
2224load_quota:
2225 return vfs_load_quota_inode(inode, type, format_id, flags);
2226}
Mingming Cao08d03502009-01-14 16:19:32 +01002227EXPORT_SYMBOL(vfs_quota_enable);
Jan Karaf55abc02008-08-20 17:50:32 +02002228
2229/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230 * This function is used when filesystem needs to initialize quotas
2231 * during mount time.
2232 */
Christoph Hellwig84de8562005-06-23 00:09:16 -07002233int vfs_quota_on_mount(struct super_block *sb, char *qf_name,
2234 int format_id, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235{
Christoph Hellwig84de8562005-06-23 00:09:16 -07002236 struct dentry *dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237 int error;
2238
Jan Karac56818d2009-11-12 15:42:08 +01002239 mutex_lock(&sb->s_root->d_inode->i_mutex);
Christoph Hellwig2fa389c2005-06-23 00:09:16 -07002240 dentry = lookup_one_len(qf_name, sb->s_root, strlen(qf_name));
Jan Karac56818d2009-11-12 15:42:08 +01002241 mutex_unlock(&sb->s_root->d_inode->i_mutex);
Christoph Hellwig84de8562005-06-23 00:09:16 -07002242 if (IS_ERR(dentry))
2243 return PTR_ERR(dentry);
2244
Jan Kara154f4842005-11-28 13:44:14 -08002245 if (!dentry->d_inode) {
2246 error = -ENOENT;
2247 goto out;
2248 }
2249
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250 error = security_quota_on(dentry);
Christoph Hellwig84de8562005-06-23 00:09:16 -07002251 if (!error)
Jan Karaf55abc02008-08-20 17:50:32 +02002252 error = vfs_load_quota_inode(dentry->d_inode, type, format_id,
2253 DQUOT_USAGE_ENABLED | DQUOT_LIMITS_ENABLED);
Christoph Hellwig84de8562005-06-23 00:09:16 -07002254
Jan Kara154f4842005-11-28 13:44:14 -08002255out:
Christoph Hellwig84de8562005-06-23 00:09:16 -07002256 dput(dentry);
2257 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258}
Mingming Cao08d03502009-01-14 16:19:32 +01002259EXPORT_SYMBOL(vfs_quota_on_mount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260
Jan Karab85f4b82008-07-25 01:46:50 -07002261/* Wrapper to turn on quotas when remounting rw */
2262int vfs_dq_quota_on_remount(struct super_block *sb)
2263{
2264 int cnt;
2265 int ret = 0, err;
2266
2267 if (!sb->s_qcop || !sb->s_qcop->quota_on)
2268 return -ENOSYS;
2269 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
2270 err = sb->s_qcop->quota_on(sb, cnt, 0, NULL, 1);
2271 if (err < 0 && !ret)
2272 ret = err;
2273 }
2274 return ret;
2275}
Mingming Cao08d03502009-01-14 16:19:32 +01002276EXPORT_SYMBOL(vfs_dq_quota_on_remount);
Jan Karab85f4b82008-07-25 01:46:50 -07002277
Jan Kara12095462008-08-20 14:45:12 +02002278static inline qsize_t qbtos(qsize_t blocks)
2279{
2280 return blocks << QIF_DQBLKSIZE_BITS;
2281}
2282
2283static inline qsize_t stoqb(qsize_t space)
2284{
2285 return (space + QIF_DQBLKSIZE - 1) >> QIF_DQBLKSIZE_BITS;
2286}
2287
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288/* Generic routine for getting common part of quota structure */
2289static void do_get_dqblk(struct dquot *dquot, struct if_dqblk *di)
2290{
2291 struct mem_dqblk *dm = &dquot->dq_dqb;
2292
2293 spin_lock(&dq_data_lock);
Jan Kara12095462008-08-20 14:45:12 +02002294 di->dqb_bhardlimit = stoqb(dm->dqb_bhardlimit);
2295 di->dqb_bsoftlimit = stoqb(dm->dqb_bsoftlimit);
Mingming Caof18df222009-01-13 16:43:09 +01002296 di->dqb_curspace = dm->dqb_curspace + dm->dqb_rsvspace;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297 di->dqb_ihardlimit = dm->dqb_ihardlimit;
2298 di->dqb_isoftlimit = dm->dqb_isoftlimit;
2299 di->dqb_curinodes = dm->dqb_curinodes;
2300 di->dqb_btime = dm->dqb_btime;
2301 di->dqb_itime = dm->dqb_itime;
2302 di->dqb_valid = QIF_ALL;
2303 spin_unlock(&dq_data_lock);
2304}
2305
Jan Kara268157b2009-01-27 15:47:22 +01002306int vfs_get_dqblk(struct super_block *sb, int type, qid_t id,
2307 struct if_dqblk *di)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308{
2309 struct dquot *dquot;
2310
Jan Karacc334122009-01-12 17:23:05 +01002311 dquot = dqget(sb, id, type);
Jan Karadd6f3c62009-01-26 16:01:43 +01002312 if (!dquot)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313 return -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314 do_get_dqblk(dquot, di);
2315 dqput(dquot);
Jan Karacc334122009-01-12 17:23:05 +01002316
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317 return 0;
2318}
Mingming Cao08d03502009-01-14 16:19:32 +01002319EXPORT_SYMBOL(vfs_get_dqblk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320
2321/* Generic routine for setting common part of quota structure */
Andrew Perepechko338bf9a2008-04-28 02:14:31 -07002322static int do_set_dqblk(struct dquot *dquot, struct if_dqblk *di)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323{
2324 struct mem_dqblk *dm = &dquot->dq_dqb;
2325 int check_blim = 0, check_ilim = 0;
Andrew Perepechko338bf9a2008-04-28 02:14:31 -07002326 struct mem_dqinfo *dqi = &sb_dqopt(dquot->dq_sb)->info[dquot->dq_type];
2327
2328 if ((di->dqb_valid & QIF_BLIMITS &&
2329 (di->dqb_bhardlimit > dqi->dqi_maxblimit ||
2330 di->dqb_bsoftlimit > dqi->dqi_maxblimit)) ||
2331 (di->dqb_valid & QIF_ILIMITS &&
2332 (di->dqb_ihardlimit > dqi->dqi_maxilimit ||
2333 di->dqb_isoftlimit > dqi->dqi_maxilimit)))
2334 return -ERANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335
2336 spin_lock(&dq_data_lock);
2337 if (di->dqb_valid & QIF_SPACE) {
Mingming Caof18df222009-01-13 16:43:09 +01002338 dm->dqb_curspace = di->dqb_curspace - dm->dqb_rsvspace;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339 check_blim = 1;
Andrew Perepechko08261672010-04-12 22:16:50 +04002340 set_bit(DQ_LASTSET_B + QIF_SPACE_B, &dquot->dq_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341 }
2342 if (di->dqb_valid & QIF_BLIMITS) {
Jan Kara12095462008-08-20 14:45:12 +02002343 dm->dqb_bsoftlimit = qbtos(di->dqb_bsoftlimit);
2344 dm->dqb_bhardlimit = qbtos(di->dqb_bhardlimit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345 check_blim = 1;
Andrew Perepechko08261672010-04-12 22:16:50 +04002346 set_bit(DQ_LASTSET_B + QIF_BLIMITS_B, &dquot->dq_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002347 }
2348 if (di->dqb_valid & QIF_INODES) {
2349 dm->dqb_curinodes = di->dqb_curinodes;
2350 check_ilim = 1;
Andrew Perepechko08261672010-04-12 22:16:50 +04002351 set_bit(DQ_LASTSET_B + QIF_INODES_B, &dquot->dq_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352 }
2353 if (di->dqb_valid & QIF_ILIMITS) {
2354 dm->dqb_isoftlimit = di->dqb_isoftlimit;
2355 dm->dqb_ihardlimit = di->dqb_ihardlimit;
2356 check_ilim = 1;
Andrew Perepechko08261672010-04-12 22:16:50 +04002357 set_bit(DQ_LASTSET_B + QIF_ILIMITS_B, &dquot->dq_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002358 }
Jan Kara4d59bce2008-10-02 16:48:10 +02002359 if (di->dqb_valid & QIF_BTIME) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360 dm->dqb_btime = di->dqb_btime;
Jan Karae04a88a92009-01-07 18:07:29 -08002361 check_blim = 1;
Andrew Perepechko08261672010-04-12 22:16:50 +04002362 set_bit(DQ_LASTSET_B + QIF_BTIME_B, &dquot->dq_flags);
Jan Kara4d59bce2008-10-02 16:48:10 +02002363 }
2364 if (di->dqb_valid & QIF_ITIME) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365 dm->dqb_itime = di->dqb_itime;
Jan Karae04a88a92009-01-07 18:07:29 -08002366 check_ilim = 1;
Andrew Perepechko08261672010-04-12 22:16:50 +04002367 set_bit(DQ_LASTSET_B + QIF_ITIME_B, &dquot->dq_flags);
Jan Kara4d59bce2008-10-02 16:48:10 +02002368 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369
2370 if (check_blim) {
Jan Kara268157b2009-01-27 15:47:22 +01002371 if (!dm->dqb_bsoftlimit ||
2372 dm->dqb_curspace < dm->dqb_bsoftlimit) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373 dm->dqb_btime = 0;
2374 clear_bit(DQ_BLKS_B, &dquot->dq_flags);
Jan Kara268157b2009-01-27 15:47:22 +01002375 } else if (!(di->dqb_valid & QIF_BTIME))
2376 /* Set grace only if user hasn't provided his own... */
Andrew Perepechko338bf9a2008-04-28 02:14:31 -07002377 dm->dqb_btime = get_seconds() + dqi->dqi_bgrace;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002378 }
2379 if (check_ilim) {
Jan Kara268157b2009-01-27 15:47:22 +01002380 if (!dm->dqb_isoftlimit ||
2381 dm->dqb_curinodes < dm->dqb_isoftlimit) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382 dm->dqb_itime = 0;
2383 clear_bit(DQ_INODES_B, &dquot->dq_flags);
Jan Kara268157b2009-01-27 15:47:22 +01002384 } else if (!(di->dqb_valid & QIF_ITIME))
2385 /* Set grace only if user hasn't provided his own... */
Andrew Perepechko338bf9a2008-04-28 02:14:31 -07002386 dm->dqb_itime = get_seconds() + dqi->dqi_igrace;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002387 }
Jan Kara268157b2009-01-27 15:47:22 +01002388 if (dm->dqb_bhardlimit || dm->dqb_bsoftlimit || dm->dqb_ihardlimit ||
2389 dm->dqb_isoftlimit)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002390 clear_bit(DQ_FAKE_B, &dquot->dq_flags);
2391 else
2392 set_bit(DQ_FAKE_B, &dquot->dq_flags);
2393 spin_unlock(&dq_data_lock);
2394 mark_dquot_dirty(dquot);
Andrew Perepechko338bf9a2008-04-28 02:14:31 -07002395
2396 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397}
2398
Jan Kara268157b2009-01-27 15:47:22 +01002399int vfs_set_dqblk(struct super_block *sb, int type, qid_t id,
2400 struct if_dqblk *di)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401{
2402 struct dquot *dquot;
Andrew Perepechko338bf9a2008-04-28 02:14:31 -07002403 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404
Jan Karaf55abc02008-08-20 17:50:32 +02002405 dquot = dqget(sb, id, type);
2406 if (!dquot) {
2407 rc = -ESRCH;
2408 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002409 }
Andrew Perepechko338bf9a2008-04-28 02:14:31 -07002410 rc = do_set_dqblk(dquot, di);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002411 dqput(dquot);
Jan Karaf55abc02008-08-20 17:50:32 +02002412out:
Andrew Perepechko338bf9a2008-04-28 02:14:31 -07002413 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002414}
Mingming Cao08d03502009-01-14 16:19:32 +01002415EXPORT_SYMBOL(vfs_set_dqblk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002416
2417/* Generic routine for getting common part of quota file information */
2418int vfs_get_dqinfo(struct super_block *sb, int type, struct if_dqinfo *ii)
2419{
2420 struct mem_dqinfo *mi;
2421
Ingo Molnard3be9152006-03-23 03:00:29 -08002422 mutex_lock(&sb_dqopt(sb)->dqonoff_mutex);
Jan Karaf55abc02008-08-20 17:50:32 +02002423 if (!sb_has_quota_active(sb, type)) {
Ingo Molnard3be9152006-03-23 03:00:29 -08002424 mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002425 return -ESRCH;
2426 }
2427 mi = sb_dqopt(sb)->info + type;
2428 spin_lock(&dq_data_lock);
2429 ii->dqi_bgrace = mi->dqi_bgrace;
2430 ii->dqi_igrace = mi->dqi_igrace;
2431 ii->dqi_flags = mi->dqi_flags & DQF_MASK;
2432 ii->dqi_valid = IIF_ALL;
2433 spin_unlock(&dq_data_lock);
Ingo Molnard3be9152006-03-23 03:00:29 -08002434 mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002435 return 0;
2436}
Mingming Cao08d03502009-01-14 16:19:32 +01002437EXPORT_SYMBOL(vfs_get_dqinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002438
2439/* Generic routine for setting common part of quota file information */
2440int vfs_set_dqinfo(struct super_block *sb, int type, struct if_dqinfo *ii)
2441{
2442 struct mem_dqinfo *mi;
Jan Karaf55abc02008-08-20 17:50:32 +02002443 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444
Ingo Molnard3be9152006-03-23 03:00:29 -08002445 mutex_lock(&sb_dqopt(sb)->dqonoff_mutex);
Jan Karaf55abc02008-08-20 17:50:32 +02002446 if (!sb_has_quota_active(sb, type)) {
2447 err = -ESRCH;
2448 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002449 }
2450 mi = sb_dqopt(sb)->info + type;
2451 spin_lock(&dq_data_lock);
2452 if (ii->dqi_valid & IIF_BGRACE)
2453 mi->dqi_bgrace = ii->dqi_bgrace;
2454 if (ii->dqi_valid & IIF_IGRACE)
2455 mi->dqi_igrace = ii->dqi_igrace;
2456 if (ii->dqi_valid & IIF_FLAGS)
Jan Kara268157b2009-01-27 15:47:22 +01002457 mi->dqi_flags = (mi->dqi_flags & ~DQF_MASK) |
2458 (ii->dqi_flags & DQF_MASK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459 spin_unlock(&dq_data_lock);
2460 mark_info_dirty(sb, type);
2461 /* Force write to disk */
2462 sb->dq_op->write_info(sb, type);
Jan Karaf55abc02008-08-20 17:50:32 +02002463out:
Ingo Molnard3be9152006-03-23 03:00:29 -08002464 mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
Jan Karaf55abc02008-08-20 17:50:32 +02002465 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002466}
Mingming Cao08d03502009-01-14 16:19:32 +01002467EXPORT_SYMBOL(vfs_set_dqinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468
Alexey Dobriyan0d54b212009-09-21 17:01:09 -07002469const struct quotactl_ops vfs_quotactl_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470 .quota_on = vfs_quota_on,
2471 .quota_off = vfs_quota_off,
2472 .quota_sync = vfs_quota_sync,
2473 .get_info = vfs_get_dqinfo,
2474 .set_info = vfs_set_dqinfo,
2475 .get_dqblk = vfs_get_dqblk,
2476 .set_dqblk = vfs_set_dqblk
2477};
2478
2479static ctl_table fs_dqstats_table[] = {
2480 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002481 .procname = "lookups",
2482 .data = &dqstats.lookups,
2483 .maxlen = sizeof(int),
2484 .mode = 0444,
Eric W. Biederman6d456112009-11-16 03:11:48 -08002485 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002486 },
2487 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002488 .procname = "drops",
2489 .data = &dqstats.drops,
2490 .maxlen = sizeof(int),
2491 .mode = 0444,
Eric W. Biederman6d456112009-11-16 03:11:48 -08002492 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002493 },
2494 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002495 .procname = "reads",
2496 .data = &dqstats.reads,
2497 .maxlen = sizeof(int),
2498 .mode = 0444,
Eric W. Biederman6d456112009-11-16 03:11:48 -08002499 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002500 },
2501 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002502 .procname = "writes",
2503 .data = &dqstats.writes,
2504 .maxlen = sizeof(int),
2505 .mode = 0444,
Eric W. Biederman6d456112009-11-16 03:11:48 -08002506 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002507 },
2508 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002509 .procname = "cache_hits",
2510 .data = &dqstats.cache_hits,
2511 .maxlen = sizeof(int),
2512 .mode = 0444,
Eric W. Biederman6d456112009-11-16 03:11:48 -08002513 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002514 },
2515 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002516 .procname = "allocated_dquots",
2517 .data = &dqstats.allocated_dquots,
2518 .maxlen = sizeof(int),
2519 .mode = 0444,
Eric W. Biederman6d456112009-11-16 03:11:48 -08002520 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002521 },
2522 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002523 .procname = "free_dquots",
2524 .data = &dqstats.free_dquots,
2525 .maxlen = sizeof(int),
2526 .mode = 0444,
Eric W. Biederman6d456112009-11-16 03:11:48 -08002527 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002528 },
2529 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530 .procname = "syncs",
2531 .data = &dqstats.syncs,
2532 .maxlen = sizeof(int),
2533 .mode = 0444,
Eric W. Biederman6d456112009-11-16 03:11:48 -08002534 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002535 },
Jan Kara8e893462007-10-16 23:29:31 -07002536#ifdef CONFIG_PRINT_QUOTA_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07002537 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002538 .procname = "warnings",
2539 .data = &flag_print_warnings,
2540 .maxlen = sizeof(int),
2541 .mode = 0644,
Eric W. Biederman6d456112009-11-16 03:11:48 -08002542 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002543 },
Jan Kara8e893462007-10-16 23:29:31 -07002544#endif
Eric W. Biedermanab092032009-11-05 14:25:10 -08002545 { },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002546};
2547
2548static ctl_table fs_table[] = {
2549 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002550 .procname = "quota",
2551 .mode = 0555,
2552 .child = fs_dqstats_table,
2553 },
Eric W. Biedermanab092032009-11-05 14:25:10 -08002554 { },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002555};
2556
2557static ctl_table sys_table[] = {
2558 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002559 .procname = "fs",
2560 .mode = 0555,
2561 .child = fs_table,
2562 },
Eric W. Biedermanab092032009-11-05 14:25:10 -08002563 { },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002564};
2565
2566static int __init dquot_init(void)
2567{
2568 int i;
2569 unsigned long nr_hash, order;
2570
2571 printk(KERN_NOTICE "VFS: Disk quotas %s\n", __DQUOT_VERSION__);
2572
Eric W. Biederman0b4d4142007-02-14 00:34:09 -08002573 register_sysctl_table(sys_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002574
Paul Mundt20c2df82007-07-20 10:11:58 +09002575 dquot_cachep = kmem_cache_create("dquot",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002576 sizeof(struct dquot), sizeof(unsigned long) * 4,
Paul Jacksonfffb60f2006-03-24 03:16:06 -08002577 (SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT|
2578 SLAB_MEM_SPREAD|SLAB_PANIC),
Paul Mundt20c2df82007-07-20 10:11:58 +09002579 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002580
2581 order = 0;
2582 dquot_hash = (struct hlist_head *)__get_free_pages(GFP_ATOMIC, order);
2583 if (!dquot_hash)
2584 panic("Cannot create dquot hash table");
2585
2586 /* Find power-of-two hlist_heads which can fit into allocation */
2587 nr_hash = (1UL << order) * PAGE_SIZE / sizeof(struct hlist_head);
2588 dq_hash_bits = 0;
2589 do {
2590 dq_hash_bits++;
2591 } while (nr_hash >> dq_hash_bits);
2592 dq_hash_bits--;
2593
2594 nr_hash = 1UL << dq_hash_bits;
2595 dq_hash_mask = nr_hash - 1;
2596 for (i = 0; i < nr_hash; i++)
2597 INIT_HLIST_HEAD(dquot_hash + i);
2598
2599 printk("Dquot-cache hash table entries: %ld (order %ld, %ld bytes)\n",
2600 nr_hash, order, (PAGE_SIZE << order));
2601
Rusty Russell8e1f9362007-07-17 04:03:17 -07002602 register_shrinker(&dqcache_shrinker);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002603
Linus Torvalds1da177e2005-04-16 15:20:36 -07002604 return 0;
2605}
2606module_init(dquot_init);