blob: a0a9405b202a95dbb440fb38104f6f9a346ebd64 [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
83#define __DQUOT_PARANOIA
84
85/*
Jan Karacc334122009-01-12 17:23:05 +010086 * There are three quota SMP locks. dq_list_lock protects all lists with quotas
87 * and quota formats, dqstats structure containing statistics about the lists
88 * dq_data_lock protects data from dq_dqb and also mem_dqinfo structures and
89 * also guards consistency of dquot->dq_dqb with inode->i_blocks, i_bytes.
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 * i_blocks and i_bytes updates itself are guarded by i_lock acquired directly
Jan Karacc334122009-01-12 17:23:05 +010091 * in inode_add_bytes() and inode_sub_bytes(). dq_state_lock protects
92 * modifications of quota state (on quotaon and quotaoff) and readers who care
93 * about latest values take it as well.
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 *
Jan Karacc334122009-01-12 17:23:05 +010095 * The spinlock ordering is hence: dq_data_lock > dq_list_lock > i_lock,
96 * dq_list_lock > dq_state_lock
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 *
98 * Note that some things (eg. sb pointer, type, id) doesn't change during
99 * the life of the dquot structure and so needn't to be protected by a lock
100 *
101 * Any operation working on dquots via inode pointers must hold dqptr_sem. If
102 * operation is just reading pointers from inode (or not using them at all) the
Jan Kara26245c92010-01-06 17:20:35 +0100103 * read lock is enough. If pointers are altered function must hold write lock.
104 * Special care needs to be taken about S_NOQUOTA inode flag (marking that
105 * inode is a quota file). Functions adding pointers from inode to dquots have
106 * to check this flag under dqptr_sem and then (if S_NOQUOTA is not set) they
107 * have to do all pointer modifications before dropping dqptr_sem. This makes
108 * sure they cannot race with quotaon which first sets S_NOQUOTA flag and
109 * then drops all pointers to dquots from an inode.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 *
Ingo Molnard3be9152006-03-23 03:00:29 -0800111 * Each dquot has its dq_lock mutex. Locked dquots might not be referenced
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 * from inodes (dquot_alloc_space() and such don't check the dq_lock).
113 * Currently dquot is locked only when it is being read to memory (or space for
114 * it is being allocated) on the first dqget() and when it is being released on
115 * the last dqput(). The allocation and release oparations are serialized by
116 * the dq_lock and by checking the use count in dquot_release(). Write
117 * operations on dquots don't hold dq_lock as they copy data under dq_data_lock
118 * spinlock to internal buffers before writing.
119 *
120 * Lock ordering (including related VFS locks) is the following:
Ingo Molnard3be9152006-03-23 03:00:29 -0800121 * i_mutex > dqonoff_sem > journal_lock > dqptr_sem > dquot->dq_lock >
122 * dqio_mutex
Jan Karacc334122009-01-12 17:23:05 +0100123 * The lock ordering of dqptr_sem imposed by quota code is only dqonoff_sem >
124 * dqptr_sem. But filesystem has to count with the fact that functions such as
125 * dquot_alloc_space() acquire dqptr_sem and they usually have to be called
126 * from inside a transaction to keep filesystem consistency after a crash. Also
127 * filesystems usually want to do some IO on dquot from ->mark_dirty which is
128 * called with dqptr_sem held.
Ingo Molnard3be9152006-03-23 03:00:29 -0800129 * i_mutex on quota files is special (it's below dqio_mutex)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 */
131
Jan Karac5166102009-01-26 15:32:46 +0100132static __cacheline_aligned_in_smp DEFINE_SPINLOCK(dq_list_lock);
133static __cacheline_aligned_in_smp DEFINE_SPINLOCK(dq_state_lock);
134__cacheline_aligned_in_smp DEFINE_SPINLOCK(dq_data_lock);
Mingming Cao08d03502009-01-14 16:19:32 +0100135EXPORT_SYMBOL(dq_data_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136
137static char *quotatypes[] = INITQFNAMES;
138static 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
322int dquot_mark_dquot_dirty(struct dquot *dquot)
323{
324 spin_lock(&dq_list_lock);
325 if (!test_and_set_bit(DQ_MOD_B, &dquot->dq_flags))
326 list_add(&dquot->dq_dirty, &sb_dqopt(dquot->dq_sb)->
327 info[dquot->dq_type].dqi_dirty_list);
328 spin_unlock(&dq_list_lock);
329 return 0;
330}
Mingming Cao08d03502009-01-14 16:19:32 +0100331EXPORT_SYMBOL(dquot_mark_dquot_dirty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332
Dmitry Monakhovdc52dd32009-12-14 15:21:15 +0300333/* Dirtify all the dquots - this can block when journalling */
334static inline int mark_all_dquot_dirty(struct dquot * const *dquot)
335{
336 int ret, err, cnt;
337
338 ret = err = 0;
339 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
340 if (dquot[cnt])
341 /* Even in case of error we have to continue */
342 ret = mark_dquot_dirty(dquot[cnt]);
343 if (!err)
344 err = ret;
345 }
346 return err;
347}
348
349static inline void dqput_all(struct dquot **dquot)
350{
351 unsigned int cnt;
352
353 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
354 dqput(dquot[cnt]);
355}
356
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357/* This function needs dq_list_lock */
358static inline int clear_dquot_dirty(struct dquot *dquot)
359{
360 if (!test_and_clear_bit(DQ_MOD_B, &dquot->dq_flags))
361 return 0;
362 list_del_init(&dquot->dq_dirty);
363 return 1;
364}
365
366void mark_info_dirty(struct super_block *sb, int type)
367{
368 set_bit(DQF_INFO_DIRTY_B, &sb_dqopt(sb)->info[type].dqi_flags);
369}
370EXPORT_SYMBOL(mark_info_dirty);
371
372/*
373 * Read dquot from disk and alloc space for it
374 */
375
376int dquot_acquire(struct dquot *dquot)
377{
378 int ret = 0, ret2 = 0;
379 struct quota_info *dqopt = sb_dqopt(dquot->dq_sb);
380
Ingo Molnard3be9152006-03-23 03:00:29 -0800381 mutex_lock(&dquot->dq_lock);
382 mutex_lock(&dqopt->dqio_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 if (!test_bit(DQ_READ_B, &dquot->dq_flags))
384 ret = dqopt->ops[dquot->dq_type]->read_dqblk(dquot);
385 if (ret < 0)
386 goto out_iolock;
387 set_bit(DQ_READ_B, &dquot->dq_flags);
388 /* Instantiate dquot if needed */
389 if (!test_bit(DQ_ACTIVE_B, &dquot->dq_flags) && !dquot->dq_off) {
390 ret = dqopt->ops[dquot->dq_type]->commit_dqblk(dquot);
391 /* Write the info if needed */
Jan Kara268157b2009-01-27 15:47:22 +0100392 if (info_dirty(&dqopt->info[dquot->dq_type])) {
393 ret2 = dqopt->ops[dquot->dq_type]->write_file_info(
394 dquot->dq_sb, dquot->dq_type);
395 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 if (ret < 0)
397 goto out_iolock;
398 if (ret2 < 0) {
399 ret = ret2;
400 goto out_iolock;
401 }
402 }
403 set_bit(DQ_ACTIVE_B, &dquot->dq_flags);
404out_iolock:
Ingo Molnard3be9152006-03-23 03:00:29 -0800405 mutex_unlock(&dqopt->dqio_mutex);
406 mutex_unlock(&dquot->dq_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 return ret;
408}
Mingming Cao08d03502009-01-14 16:19:32 +0100409EXPORT_SYMBOL(dquot_acquire);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410
411/*
412 * Write dquot to disk
413 */
414int dquot_commit(struct dquot *dquot)
415{
416 int ret = 0, ret2 = 0;
417 struct quota_info *dqopt = sb_dqopt(dquot->dq_sb);
418
Ingo Molnard3be9152006-03-23 03:00:29 -0800419 mutex_lock(&dqopt->dqio_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 spin_lock(&dq_list_lock);
421 if (!clear_dquot_dirty(dquot)) {
422 spin_unlock(&dq_list_lock);
423 goto out_sem;
424 }
425 spin_unlock(&dq_list_lock);
426 /* Inactive dquot can be only if there was error during read/init
427 * => we have better not writing it */
428 if (test_bit(DQ_ACTIVE_B, &dquot->dq_flags)) {
429 ret = dqopt->ops[dquot->dq_type]->commit_dqblk(dquot);
Jan Kara268157b2009-01-27 15:47:22 +0100430 if (info_dirty(&dqopt->info[dquot->dq_type])) {
431 ret2 = dqopt->ops[dquot->dq_type]->write_file_info(
432 dquot->dq_sb, dquot->dq_type);
433 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 if (ret >= 0)
435 ret = ret2;
436 }
437out_sem:
Ingo Molnard3be9152006-03-23 03:00:29 -0800438 mutex_unlock(&dqopt->dqio_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 return ret;
440}
Mingming Cao08d03502009-01-14 16:19:32 +0100441EXPORT_SYMBOL(dquot_commit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442
443/*
444 * Release dquot
445 */
446int dquot_release(struct dquot *dquot)
447{
448 int ret = 0, ret2 = 0;
449 struct quota_info *dqopt = sb_dqopt(dquot->dq_sb);
450
Ingo Molnard3be9152006-03-23 03:00:29 -0800451 mutex_lock(&dquot->dq_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 /* Check whether we are not racing with some other dqget() */
453 if (atomic_read(&dquot->dq_count) > 1)
454 goto out_dqlock;
Ingo Molnard3be9152006-03-23 03:00:29 -0800455 mutex_lock(&dqopt->dqio_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 if (dqopt->ops[dquot->dq_type]->release_dqblk) {
457 ret = dqopt->ops[dquot->dq_type]->release_dqblk(dquot);
458 /* Write the info */
Jan Kara268157b2009-01-27 15:47:22 +0100459 if (info_dirty(&dqopt->info[dquot->dq_type])) {
460 ret2 = dqopt->ops[dquot->dq_type]->write_file_info(
461 dquot->dq_sb, dquot->dq_type);
462 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 if (ret >= 0)
464 ret = ret2;
465 }
466 clear_bit(DQ_ACTIVE_B, &dquot->dq_flags);
Ingo Molnard3be9152006-03-23 03:00:29 -0800467 mutex_unlock(&dqopt->dqio_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468out_dqlock:
Ingo Molnard3be9152006-03-23 03:00:29 -0800469 mutex_unlock(&dquot->dq_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 return ret;
471}
Mingming Cao08d03502009-01-14 16:19:32 +0100472EXPORT_SYMBOL(dquot_release);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473
Jan Kara7d9056b2008-11-25 15:31:32 +0100474void dquot_destroy(struct dquot *dquot)
Jan Kara74f783a2008-08-19 14:51:22 +0200475{
476 kmem_cache_free(dquot_cachep, dquot);
477}
Jan Kara7d9056b2008-11-25 15:31:32 +0100478EXPORT_SYMBOL(dquot_destroy);
Jan Kara74f783a2008-08-19 14:51:22 +0200479
480static inline void do_destroy_dquot(struct dquot *dquot)
481{
482 dquot->dq_sb->dq_op->destroy_dquot(dquot);
483}
484
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485/* Invalidate all dquots on the list. Note that this function is called after
486 * quota is disabled and pointers from inodes removed so there cannot be new
Jan Kara6362e4d2006-03-23 03:00:17 -0800487 * quota users. There can still be some users of quotas due to inodes being
488 * just deleted or pruned by prune_icache() (those are not attached to any
Jan Karacc334122009-01-12 17:23:05 +0100489 * list) or parallel quotactl call. We have to wait for such users.
Jan Kara6362e4d2006-03-23 03:00:17 -0800490 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491static void invalidate_dquots(struct super_block *sb, int type)
492{
Domen Puncerc33ed272005-06-25 14:59:36 -0700493 struct dquot *dquot, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494
Jan Kara6362e4d2006-03-23 03:00:17 -0800495restart:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 spin_lock(&dq_list_lock);
Domen Puncerc33ed272005-06-25 14:59:36 -0700497 list_for_each_entry_safe(dquot, tmp, &inuse_list, dq_inuse) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 if (dquot->dq_sb != sb)
499 continue;
500 if (dquot->dq_type != type)
501 continue;
Jan Kara6362e4d2006-03-23 03:00:17 -0800502 /* Wait for dquot users */
503 if (atomic_read(&dquot->dq_count)) {
504 DEFINE_WAIT(wait);
505
506 atomic_inc(&dquot->dq_count);
507 prepare_to_wait(&dquot->dq_wait_unused, &wait,
508 TASK_UNINTERRUPTIBLE);
509 spin_unlock(&dq_list_lock);
510 /* Once dqput() wakes us up, we know it's time to free
511 * the dquot.
512 * IMPORTANT: we rely on the fact that there is always
513 * at most one process waiting for dquot to free.
514 * Otherwise dq_count would be > 1 and we would never
515 * wake up.
516 */
517 if (atomic_read(&dquot->dq_count) > 1)
518 schedule();
519 finish_wait(&dquot->dq_wait_unused, &wait);
520 dqput(dquot);
521 /* At this moment dquot() need not exist (it could be
522 * reclaimed by prune_dqcache(). Hence we must
523 * restart. */
524 goto restart;
525 }
526 /*
527 * Quota now has no users and it has been written on last
528 * dqput()
529 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 remove_dquot_hash(dquot);
531 remove_free_dquot(dquot);
532 remove_inuse(dquot);
Jan Kara74f783a2008-08-19 14:51:22 +0200533 do_destroy_dquot(dquot);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 }
535 spin_unlock(&dq_list_lock);
536}
537
Jan Kara12c77522008-10-20 17:05:00 +0200538/* Call callback for every active dquot on given filesystem */
539int dquot_scan_active(struct super_block *sb,
540 int (*fn)(struct dquot *dquot, unsigned long priv),
541 unsigned long priv)
542{
543 struct dquot *dquot, *old_dquot = NULL;
544 int ret = 0;
545
546 mutex_lock(&sb_dqopt(sb)->dqonoff_mutex);
547 spin_lock(&dq_list_lock);
548 list_for_each_entry(dquot, &inuse_list, dq_inuse) {
549 if (!test_bit(DQ_ACTIVE_B, &dquot->dq_flags))
550 continue;
551 if (dquot->dq_sb != sb)
552 continue;
553 /* Now we have active dquot so we can just increase use count */
554 atomic_inc(&dquot->dq_count);
555 dqstats.lookups++;
556 spin_unlock(&dq_list_lock);
557 dqput(old_dquot);
558 old_dquot = dquot;
559 ret = fn(dquot, priv);
560 if (ret < 0)
561 goto out;
562 spin_lock(&dq_list_lock);
563 /* We are safe to continue now because our dquot could not
564 * be moved out of the inuse list while we hold the reference */
565 }
566 spin_unlock(&dq_list_lock);
567out:
568 dqput(old_dquot);
569 mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
570 return ret;
571}
Mingming Cao08d03502009-01-14 16:19:32 +0100572EXPORT_SYMBOL(dquot_scan_active);
Jan Kara12c77522008-10-20 17:05:00 +0200573
Christoph Hellwig5fb324a2010-02-16 03:44:52 -0500574int vfs_quota_sync(struct super_block *sb, int type, int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575{
576 struct list_head *dirty;
577 struct dquot *dquot;
578 struct quota_info *dqopt = sb_dqopt(sb);
579 int cnt;
580
Ingo Molnard3be9152006-03-23 03:00:29 -0800581 mutex_lock(&dqopt->dqonoff_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
583 if (type != -1 && cnt != type)
584 continue;
Jan Karaf55abc02008-08-20 17:50:32 +0200585 if (!sb_has_quota_active(sb, cnt))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 continue;
587 spin_lock(&dq_list_lock);
588 dirty = &dqopt->info[cnt].dqi_dirty_list;
589 while (!list_empty(dirty)) {
Jan Kara268157b2009-01-27 15:47:22 +0100590 dquot = list_first_entry(dirty, struct dquot,
591 dq_dirty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 /* Dirty and inactive can be only bad dquot... */
593 if (!test_bit(DQ_ACTIVE_B, &dquot->dq_flags)) {
594 clear_dquot_dirty(dquot);
595 continue;
596 }
597 /* Now we have active dquot from which someone is
598 * holding reference so we can safely just increase
599 * use count */
600 atomic_inc(&dquot->dq_count);
601 dqstats.lookups++;
602 spin_unlock(&dq_list_lock);
603 sb->dq_op->write_dquot(dquot);
604 dqput(dquot);
605 spin_lock(&dq_list_lock);
606 }
607 spin_unlock(&dq_list_lock);
608 }
609
610 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
Jan Karaf55abc02008-08-20 17:50:32 +0200611 if ((cnt == type || type == -1) && sb_has_quota_active(sb, cnt)
612 && info_dirty(&dqopt->info[cnt]))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 sb->dq_op->write_info(sb, cnt);
614 spin_lock(&dq_list_lock);
615 dqstats.syncs++;
616 spin_unlock(&dq_list_lock);
Ingo Molnard3be9152006-03-23 03:00:29 -0800617 mutex_unlock(&dqopt->dqonoff_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618
Christoph Hellwig5fb324a2010-02-16 03:44:52 -0500619 if (!wait || (sb_dqopt(sb)->flags & DQUOT_QUOTA_SYS_FILE))
620 return 0;
621
622 /* This is not very clever (and fast) but currently I don't know about
623 * any other simple way of getting quota data to disk and we must get
624 * them there for userspace to be visible... */
625 if (sb->s_op->sync_fs)
626 sb->s_op->sync_fs(sb, 1);
627 sync_blockdev(sb->s_bdev);
628
629 /*
630 * Now when everything is written we can discard the pagecache so
631 * that userspace sees the changes.
632 */
633 mutex_lock(&sb_dqopt(sb)->dqonoff_mutex);
634 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
635 if (type != -1 && cnt != type)
636 continue;
637 if (!sb_has_quota_active(sb, cnt))
638 continue;
639 mutex_lock_nested(&sb_dqopt(sb)->files[cnt]->i_mutex,
640 I_MUTEX_QUOTA);
641 truncate_inode_pages(&sb_dqopt(sb)->files[cnt]->i_data, 0);
642 mutex_unlock(&sb_dqopt(sb)->files[cnt]->i_mutex);
643 }
644 mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
645
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 return 0;
647}
Mingming Cao08d03502009-01-14 16:19:32 +0100648EXPORT_SYMBOL(vfs_quota_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649
650/* Free unused dquots from cache */
651static void prune_dqcache(int count)
652{
653 struct list_head *head;
654 struct dquot *dquot;
655
656 head = free_dquots.prev;
657 while (head != &free_dquots && count) {
658 dquot = list_entry(head, struct dquot, dq_free);
659 remove_dquot_hash(dquot);
660 remove_free_dquot(dquot);
661 remove_inuse(dquot);
Jan Kara74f783a2008-08-19 14:51:22 +0200662 do_destroy_dquot(dquot);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 count--;
664 head = free_dquots.prev;
665 }
666}
667
668/*
669 * This is called from kswapd when we think we need some
670 * more memory
671 */
672
Al Viro27496a82005-10-21 03:20:48 -0400673static int shrink_dqcache_memory(int nr, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674{
675 if (nr) {
676 spin_lock(&dq_list_lock);
677 prune_dqcache(nr);
678 spin_unlock(&dq_list_lock);
679 }
680 return (dqstats.free_dquots / 100) * sysctl_vfs_cache_pressure;
681}
682
Rusty Russell8e1f9362007-07-17 04:03:17 -0700683static struct shrinker dqcache_shrinker = {
684 .shrink = shrink_dqcache_memory,
685 .seeks = DEFAULT_SEEKS,
686};
687
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688/*
689 * Put reference to dquot
690 * NOTE: If you change this function please check whether dqput_blocks() works right...
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 */
Jan Kara3d9ea252008-10-10 16:12:23 +0200692void dqput(struct dquot *dquot)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693{
Jan Karab48d3802008-07-25 01:46:49 -0700694 int ret;
695
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 if (!dquot)
697 return;
698#ifdef __DQUOT_PARANOIA
699 if (!atomic_read(&dquot->dq_count)) {
700 printk("VFS: dqput: trying to free free dquot\n");
701 printk("VFS: device %s, dquot of %s %d\n",
702 dquot->dq_sb->s_id,
703 quotatypes[dquot->dq_type],
704 dquot->dq_id);
705 BUG();
706 }
707#endif
708
709 spin_lock(&dq_list_lock);
710 dqstats.drops++;
711 spin_unlock(&dq_list_lock);
712we_slept:
713 spin_lock(&dq_list_lock);
714 if (atomic_read(&dquot->dq_count) > 1) {
715 /* We have more than one user... nothing to do */
716 atomic_dec(&dquot->dq_count);
Jan Kara6362e4d2006-03-23 03:00:17 -0800717 /* Releasing dquot during quotaoff phase? */
Jan Karaf55abc02008-08-20 17:50:32 +0200718 if (!sb_has_quota_active(dquot->dq_sb, dquot->dq_type) &&
Jan Kara6362e4d2006-03-23 03:00:17 -0800719 atomic_read(&dquot->dq_count) == 1)
720 wake_up(&dquot->dq_wait_unused);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 spin_unlock(&dq_list_lock);
722 return;
723 }
724 /* Need to release dquot? */
725 if (test_bit(DQ_ACTIVE_B, &dquot->dq_flags) && dquot_dirty(dquot)) {
726 spin_unlock(&dq_list_lock);
727 /* Commit dquot before releasing */
Jan Karab48d3802008-07-25 01:46:49 -0700728 ret = dquot->dq_sb->dq_op->write_dquot(dquot);
729 if (ret < 0) {
730 printk(KERN_ERR "VFS: cannot write quota structure on "
731 "device %s (error %d). Quota may get out of "
732 "sync!\n", dquot->dq_sb->s_id, ret);
733 /*
734 * We clear dirty bit anyway, so that we avoid
735 * infinite loop here
736 */
737 spin_lock(&dq_list_lock);
738 clear_dquot_dirty(dquot);
739 spin_unlock(&dq_list_lock);
740 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 goto we_slept;
742 }
743 /* Clear flag in case dquot was inactive (something bad happened) */
744 clear_dquot_dirty(dquot);
745 if (test_bit(DQ_ACTIVE_B, &dquot->dq_flags)) {
746 spin_unlock(&dq_list_lock);
747 dquot->dq_sb->dq_op->release_dquot(dquot);
748 goto we_slept;
749 }
750 atomic_dec(&dquot->dq_count);
751#ifdef __DQUOT_PARANOIA
752 /* sanity check */
Eric Sesterhenn8abf6a42006-04-02 13:36:13 +0200753 BUG_ON(!list_empty(&dquot->dq_free));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754#endif
755 put_dquot_last(dquot);
756 spin_unlock(&dq_list_lock);
757}
Mingming Cao08d03502009-01-14 16:19:32 +0100758EXPORT_SYMBOL(dqput);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759
Jan Kara7d9056b2008-11-25 15:31:32 +0100760struct dquot *dquot_alloc(struct super_block *sb, int type)
Jan Kara74f783a2008-08-19 14:51:22 +0200761{
762 return kmem_cache_zalloc(dquot_cachep, GFP_NOFS);
763}
Jan Kara7d9056b2008-11-25 15:31:32 +0100764EXPORT_SYMBOL(dquot_alloc);
Jan Kara74f783a2008-08-19 14:51:22 +0200765
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766static struct dquot *get_empty_dquot(struct super_block *sb, int type)
767{
768 struct dquot *dquot;
769
Jan Kara74f783a2008-08-19 14:51:22 +0200770 dquot = sb->dq_op->alloc_dquot(sb, type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 if(!dquot)
Jan Karadd6f3c62009-01-26 16:01:43 +0100772 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773
Ingo Molnard3be9152006-03-23 03:00:29 -0800774 mutex_init(&dquot->dq_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 INIT_LIST_HEAD(&dquot->dq_free);
776 INIT_LIST_HEAD(&dquot->dq_inuse);
777 INIT_HLIST_NODE(&dquot->dq_hash);
778 INIT_LIST_HEAD(&dquot->dq_dirty);
Jan Kara6362e4d2006-03-23 03:00:17 -0800779 init_waitqueue_head(&dquot->dq_wait_unused);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 dquot->dq_sb = sb;
781 dquot->dq_type = type;
782 atomic_set(&dquot->dq_count, 1);
783
784 return dquot;
785}
786
787/*
788 * Get reference to dquot
Jan Karacc334122009-01-12 17:23:05 +0100789 *
790 * Locking is slightly tricky here. We are guarded from parallel quotaoff()
791 * destroying our dquot by:
792 * a) checking for quota flags under dq_list_lock and
793 * b) getting a reference to dquot before we release dq_list_lock
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 */
Jan Kara3d9ea252008-10-10 16:12:23 +0200795struct dquot *dqget(struct super_block *sb, unsigned int id, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796{
797 unsigned int hashent = hashfn(sb, id, type);
Jan Karadd6f3c62009-01-26 16:01:43 +0100798 struct dquot *dquot = NULL, *empty = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799
Jan Karaf55abc02008-08-20 17:50:32 +0200800 if (!sb_has_quota_active(sb, type))
Jan Karadd6f3c62009-01-26 16:01:43 +0100801 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802we_slept:
803 spin_lock(&dq_list_lock);
Jan Karacc334122009-01-12 17:23:05 +0100804 spin_lock(&dq_state_lock);
805 if (!sb_has_quota_active(sb, type)) {
806 spin_unlock(&dq_state_lock);
807 spin_unlock(&dq_list_lock);
808 goto out;
809 }
810 spin_unlock(&dq_state_lock);
811
Jan Karadd6f3c62009-01-26 16:01:43 +0100812 dquot = find_dquot(hashent, sb, id, type);
813 if (!dquot) {
814 if (!empty) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 spin_unlock(&dq_list_lock);
Jan Karadd6f3c62009-01-26 16:01:43 +0100816 empty = get_empty_dquot(sb, type);
817 if (!empty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 schedule(); /* Try to wait for a moment... */
819 goto we_slept;
820 }
821 dquot = empty;
Jan Karadd6f3c62009-01-26 16:01:43 +0100822 empty = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 dquot->dq_id = id;
824 /* all dquots go on the inuse_list */
825 put_inuse(dquot);
826 /* hash it first so it can be found */
827 insert_dquot_hash(dquot);
828 dqstats.lookups++;
829 spin_unlock(&dq_list_lock);
830 } else {
831 if (!atomic_read(&dquot->dq_count))
832 remove_free_dquot(dquot);
833 atomic_inc(&dquot->dq_count);
834 dqstats.cache_hits++;
835 dqstats.lookups++;
836 spin_unlock(&dq_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 }
Jan Kara268157b2009-01-27 15:47:22 +0100838 /* Wait for dq_lock - after this we know that either dquot_release() is
839 * already finished or it will be canceled due to dq_count > 1 test */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 wait_on_dquot(dquot);
Jan Kara268157b2009-01-27 15:47:22 +0100841 /* Read the dquot / allocate space in quota file */
842 if (!test_bit(DQ_ACTIVE_B, &dquot->dq_flags) &&
843 sb->dq_op->acquire_dquot(dquot) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 dqput(dquot);
Jan Karadd6f3c62009-01-26 16:01:43 +0100845 dquot = NULL;
Jan Karacc334122009-01-12 17:23:05 +0100846 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 }
848#ifdef __DQUOT_PARANOIA
Eric Sesterhenn8abf6a42006-04-02 13:36:13 +0200849 BUG_ON(!dquot->dq_sb); /* Has somebody invalidated entry under us? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850#endif
Jan Karacc334122009-01-12 17:23:05 +0100851out:
852 if (empty)
853 do_destroy_dquot(empty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854
855 return dquot;
856}
Mingming Cao08d03502009-01-14 16:19:32 +0100857EXPORT_SYMBOL(dqget);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858
859static int dqinit_needed(struct inode *inode, int type)
860{
861 int cnt;
862
863 if (IS_NOQUOTA(inode))
864 return 0;
865 if (type != -1)
Jan Karadd6f3c62009-01-26 16:01:43 +0100866 return !inode->i_dquot[type];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
Jan Karadd6f3c62009-01-26 16:01:43 +0100868 if (!inode->i_dquot[cnt])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 return 1;
870 return 0;
871}
872
Ingo Molnard3be9152006-03-23 03:00:29 -0800873/* This routine is guarded by dqonoff_mutex mutex */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874static void add_dquot_ref(struct super_block *sb, int type)
875{
Jan Kara941d2382008-02-06 01:37:36 -0800876 struct inode *inode, *old_inode = NULL;
Jan Kara4c5e6c02010-04-06 18:52:47 +0200877#ifdef __DQUOT_PARANOIA
Jan Kara0a5a9c72010-02-09 18:20:39 +0100878 int reserved = 0;
Jan Kara4c5e6c02010-04-06 18:52:47 +0200879#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880
Christoph Hellwigd003fb72007-02-12 00:51:58 -0800881 spin_lock(&inode_lock);
882 list_for_each_entry(inode, &sb->s_inodes, i_sb_list) {
Wu Fengguangb6fac632009-04-02 16:56:34 -0700883 if (inode->i_state & (I_FREEING|I_CLEAR|I_WILL_FREE|I_NEW))
Nick Pigginaabb8fd2009-03-11 13:17:36 -0700884 continue;
Jan Kara4c5e6c02010-04-06 18:52:47 +0200885#ifdef __DQUOT_PARANOIA
Jan Kara0a5a9c72010-02-09 18:20:39 +0100886 if (unlikely(inode_get_rsv_space(inode) > 0))
887 reserved = 1;
Jan Kara4c5e6c02010-04-06 18:52:47 +0200888#endif
Christoph Hellwigd003fb72007-02-12 00:51:58 -0800889 if (!atomic_read(&inode->i_writecount))
890 continue;
891 if (!dqinit_needed(inode, type))
892 continue;
Christoph Hellwigd003fb72007-02-12 00:51:58 -0800893
894 __iget(inode);
895 spin_unlock(&inode_lock);
896
Jan Kara941d2382008-02-06 01:37:36 -0800897 iput(old_inode);
Christoph Hellwig871a2932010-03-03 09:05:07 -0500898 __dquot_initialize(inode, type);
Jan Kara941d2382008-02-06 01:37:36 -0800899 /* We hold a reference to 'inode' so it couldn't have been
900 * removed from s_inodes list while we dropped the inode_lock.
901 * We cannot iput the inode now as we can be holding the last
902 * reference and we cannot iput it under inode_lock. So we
903 * keep the reference and iput it later. */
904 old_inode = inode;
905 spin_lock(&inode_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 }
Christoph Hellwigd003fb72007-02-12 00:51:58 -0800907 spin_unlock(&inode_lock);
Jan Kara941d2382008-02-06 01:37:36 -0800908 iput(old_inode);
Jan Kara0a5a9c72010-02-09 18:20:39 +0100909
Jan Kara4c5e6c02010-04-06 18:52:47 +0200910#ifdef __DQUOT_PARANOIA
Jan Kara0a5a9c72010-02-09 18:20:39 +0100911 if (reserved) {
912 printk(KERN_WARNING "VFS (%s): Writes happened before quota"
913 " was turned on thus quota information is probably "
914 "inconsistent. Please run quotacheck(8).\n", sb->s_id);
915 }
Jan Kara4c5e6c02010-04-06 18:52:47 +0200916#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917}
918
Jan Kara268157b2009-01-27 15:47:22 +0100919/*
920 * Return 0 if dqput() won't block.
921 * (note that 1 doesn't necessarily mean blocking)
922 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923static inline int dqput_blocks(struct dquot *dquot)
924{
925 if (atomic_read(&dquot->dq_count) <= 1)
926 return 1;
927 return 0;
928}
929
Jan Kara268157b2009-01-27 15:47:22 +0100930/*
931 * Remove references to dquots from inode and add dquot to list for freeing
932 * if we have the last referece to dquot
933 * We can't race with anybody because we hold dqptr_sem for writing...
934 */
Adrian Bunke5f00f42007-05-08 00:27:22 -0700935static int remove_inode_dquot_ref(struct inode *inode, int type,
936 struct list_head *tofree_head)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937{
938 struct dquot *dquot = inode->i_dquot[type];
939
Jan Karadd6f3c62009-01-26 16:01:43 +0100940 inode->i_dquot[type] = NULL;
941 if (dquot) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 if (dqput_blocks(dquot)) {
943#ifdef __DQUOT_PARANOIA
944 if (atomic_read(&dquot->dq_count) != 1)
945 printk(KERN_WARNING "VFS: Adding dquot with dq_count %d to dispose list.\n", atomic_read(&dquot->dq_count));
946#endif
947 spin_lock(&dq_list_lock);
Jan Kara268157b2009-01-27 15:47:22 +0100948 /* As dquot must have currently users it can't be on
949 * the free list... */
950 list_add(&dquot->dq_free, tofree_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 spin_unlock(&dq_list_lock);
952 return 1;
953 }
954 else
955 dqput(dquot); /* We have guaranteed we won't block */
956 }
957 return 0;
958}
959
Jan Kara268157b2009-01-27 15:47:22 +0100960/*
961 * Free list of dquots
962 * Dquots are removed from inodes and no new references can be got so we are
963 * the only ones holding reference
964 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965static void put_dquot_list(struct list_head *tofree_head)
966{
967 struct list_head *act_head;
968 struct dquot *dquot;
969
970 act_head = tofree_head->next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 while (act_head != tofree_head) {
972 dquot = list_entry(act_head, struct dquot, dq_free);
973 act_head = act_head->next;
Jan Kara268157b2009-01-27 15:47:22 +0100974 /* Remove dquot from the list so we won't have problems... */
975 list_del_init(&dquot->dq_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 dqput(dquot);
977 }
978}
979
Christoph Hellwigfb58b732007-02-12 00:51:57 -0800980static void remove_dquot_ref(struct super_block *sb, int type,
981 struct list_head *tofree_head)
982{
983 struct inode *inode;
984
985 spin_lock(&inode_lock);
986 list_for_each_entry(inode, &sb->s_inodes, i_sb_list) {
Nick Pigginaabb8fd2009-03-11 13:17:36 -0700987 /*
988 * We have to scan also I_NEW inodes because they can already
989 * have quota pointer initialized. Luckily, we need to touch
990 * only quota pointers and these have separate locking
991 * (dqptr_sem).
992 */
Christoph Hellwigfb58b732007-02-12 00:51:57 -0800993 if (!IS_NOQUOTA(inode))
994 remove_inode_dquot_ref(inode, type, tofree_head);
995 }
996 spin_unlock(&inode_lock);
997}
998
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999/* Gather all references from inodes and drop them */
1000static void drop_dquot_ref(struct super_block *sb, int type)
1001{
1002 LIST_HEAD(tofree_head);
1003
Christoph Hellwigfb58b732007-02-12 00:51:57 -08001004 if (sb->dq_op) {
1005 down_write(&sb_dqopt(sb)->dqptr_sem);
1006 remove_dquot_ref(sb, type, &tofree_head);
1007 up_write(&sb_dqopt(sb)->dqptr_sem);
1008 put_dquot_list(&tofree_head);
1009 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010}
1011
Jan Kara12095462008-08-20 14:45:12 +02001012static inline void dquot_incr_inodes(struct dquot *dquot, qsize_t number)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013{
1014 dquot->dq_dqb.dqb_curinodes += number;
1015}
1016
1017static inline void dquot_incr_space(struct dquot *dquot, qsize_t number)
1018{
1019 dquot->dq_dqb.dqb_curspace += number;
1020}
1021
Mingming Caof18df222009-01-13 16:43:09 +01001022static inline void dquot_resv_space(struct dquot *dquot, qsize_t number)
1023{
1024 dquot->dq_dqb.dqb_rsvspace += number;
1025}
1026
Mingming Cao740d9dc2009-01-13 16:43:14 +01001027/*
1028 * Claim reserved quota space
1029 */
Jan Kara0a5a9c72010-02-09 18:20:39 +01001030static void dquot_claim_reserved_space(struct dquot *dquot, qsize_t number)
Mingming Cao740d9dc2009-01-13 16:43:14 +01001031{
Jan Kara0a5a9c72010-02-09 18:20:39 +01001032 if (dquot->dq_dqb.dqb_rsvspace < number) {
1033 WARN_ON_ONCE(1);
1034 number = dquot->dq_dqb.dqb_rsvspace;
1035 }
Mingming Cao740d9dc2009-01-13 16:43:14 +01001036 dquot->dq_dqb.dqb_curspace += number;
1037 dquot->dq_dqb.dqb_rsvspace -= number;
1038}
1039
1040static inline
1041void dquot_free_reserved_space(struct dquot *dquot, qsize_t number)
1042{
Jan Kara0a5a9c72010-02-09 18:20:39 +01001043 if (dquot->dq_dqb.dqb_rsvspace >= number)
1044 dquot->dq_dqb.dqb_rsvspace -= number;
1045 else {
1046 WARN_ON_ONCE(1);
1047 dquot->dq_dqb.dqb_rsvspace = 0;
1048 }
Mingming Cao740d9dc2009-01-13 16:43:14 +01001049}
1050
Jan Kara7a2435d2009-01-27 01:47:11 +01001051static void dquot_decr_inodes(struct dquot *dquot, qsize_t number)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052{
Jan Karadb49d2d2008-10-01 18:21:39 +02001053 if (sb_dqopt(dquot->dq_sb)->flags & DQUOT_NEGATIVE_USAGE ||
1054 dquot->dq_dqb.dqb_curinodes >= number)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 dquot->dq_dqb.dqb_curinodes -= number;
1056 else
1057 dquot->dq_dqb.dqb_curinodes = 0;
1058 if (dquot->dq_dqb.dqb_curinodes <= dquot->dq_dqb.dqb_isoftlimit)
1059 dquot->dq_dqb.dqb_itime = (time_t) 0;
1060 clear_bit(DQ_INODES_B, &dquot->dq_flags);
1061}
1062
Jan Kara7a2435d2009-01-27 01:47:11 +01001063static void dquot_decr_space(struct dquot *dquot, qsize_t number)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064{
Jan Karadb49d2d2008-10-01 18:21:39 +02001065 if (sb_dqopt(dquot->dq_sb)->flags & DQUOT_NEGATIVE_USAGE ||
1066 dquot->dq_dqb.dqb_curspace >= number)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 dquot->dq_dqb.dqb_curspace -= number;
1068 else
1069 dquot->dq_dqb.dqb_curspace = 0;
Jan Kara12095462008-08-20 14:45:12 +02001070 if (dquot->dq_dqb.dqb_curspace <= dquot->dq_dqb.dqb_bsoftlimit)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 dquot->dq_dqb.dqb_btime = (time_t) 0;
1072 clear_bit(DQ_BLKS_B, &dquot->dq_flags);
1073}
1074
Jan Karac5254602007-12-22 14:03:25 -08001075static int warning_issued(struct dquot *dquot, const int warntype)
1076{
1077 int flag = (warntype == QUOTA_NL_BHARDWARN ||
1078 warntype == QUOTA_NL_BSOFTLONGWARN) ? DQ_BLKS_B :
1079 ((warntype == QUOTA_NL_IHARDWARN ||
1080 warntype == QUOTA_NL_ISOFTLONGWARN) ? DQ_INODES_B : 0);
1081
1082 if (!flag)
1083 return 0;
1084 return test_and_set_bit(flag, &dquot->dq_flags);
1085}
1086
Jan Kara8e893462007-10-16 23:29:31 -07001087#ifdef CONFIG_PRINT_QUOTA_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088static int flag_print_warnings = 1;
1089
Jan Kara7a2435d2009-01-27 01:47:11 +01001090static int need_print_warning(struct dquot *dquot)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091{
1092 if (!flag_print_warnings)
1093 return 0;
1094
1095 switch (dquot->dq_type) {
1096 case USRQUOTA:
David Howellsda9592e2008-11-14 10:39:05 +11001097 return current_fsuid() == dquot->dq_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 case GRPQUOTA:
1099 return in_group_p(dquot->dq_id);
1100 }
1101 return 0;
1102}
1103
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104/* Print warning to user which exceeded quota */
Jan Karac5254602007-12-22 14:03:25 -08001105static void print_warning(struct dquot *dquot, const int warntype)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106{
1107 char *msg = NULL;
Peter Zijlstra24ec8392006-12-08 02:36:04 -08001108 struct tty_struct *tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109
Jan Kara657d3bf2008-07-25 01:46:52 -07001110 if (warntype == QUOTA_NL_IHARDBELOW ||
1111 warntype == QUOTA_NL_ISOFTBELOW ||
1112 warntype == QUOTA_NL_BHARDBELOW ||
1113 warntype == QUOTA_NL_BSOFTBELOW || !need_print_warning(dquot))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 return;
1115
Peter Zijlstra24ec8392006-12-08 02:36:04 -08001116 tty = get_current_tty();
1117 if (!tty)
Alan Cox452a00d2008-10-13 10:39:13 +01001118 return;
Peter Zijlstra24ec8392006-12-08 02:36:04 -08001119 tty_write_message(tty, dquot->dq_sb->s_id);
Jan Kara8e893462007-10-16 23:29:31 -07001120 if (warntype == QUOTA_NL_ISOFTWARN || warntype == QUOTA_NL_BSOFTWARN)
Peter Zijlstra24ec8392006-12-08 02:36:04 -08001121 tty_write_message(tty, ": warning, ");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 else
Peter Zijlstra24ec8392006-12-08 02:36:04 -08001123 tty_write_message(tty, ": write failed, ");
1124 tty_write_message(tty, quotatypes[dquot->dq_type]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 switch (warntype) {
Jan Kara8e893462007-10-16 23:29:31 -07001126 case QUOTA_NL_IHARDWARN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 msg = " file limit reached.\r\n";
1128 break;
Jan Kara8e893462007-10-16 23:29:31 -07001129 case QUOTA_NL_ISOFTLONGWARN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 msg = " file quota exceeded too long.\r\n";
1131 break;
Jan Kara8e893462007-10-16 23:29:31 -07001132 case QUOTA_NL_ISOFTWARN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 msg = " file quota exceeded.\r\n";
1134 break;
Jan Kara8e893462007-10-16 23:29:31 -07001135 case QUOTA_NL_BHARDWARN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 msg = " block limit reached.\r\n";
1137 break;
Jan Kara8e893462007-10-16 23:29:31 -07001138 case QUOTA_NL_BSOFTLONGWARN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 msg = " block quota exceeded too long.\r\n";
1140 break;
Jan Kara8e893462007-10-16 23:29:31 -07001141 case QUOTA_NL_BSOFTWARN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 msg = " block quota exceeded.\r\n";
1143 break;
1144 }
Peter Zijlstra24ec8392006-12-08 02:36:04 -08001145 tty_write_message(tty, msg);
Alan Cox452a00d2008-10-13 10:39:13 +01001146 tty_kref_put(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147}
Jan Kara8e893462007-10-16 23:29:31 -07001148#endif
1149
Mingming Caof18df222009-01-13 16:43:09 +01001150/*
1151 * Write warnings to the console and send warning messages over netlink.
1152 *
1153 * Note that this function can sleep.
1154 */
Jan Kara7a2435d2009-01-27 01:47:11 +01001155static void flush_warnings(struct dquot *const *dquots, char *warntype)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156{
Steven Whitehouse86e931a2009-09-28 12:35:17 +01001157 struct dquot *dq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 int i;
1159
Steven Whitehouse86e931a2009-09-28 12:35:17 +01001160 for (i = 0; i < MAXQUOTAS; i++) {
1161 dq = dquots[i];
1162 if (dq && warntype[i] != QUOTA_NL_NOWARN &&
1163 !warning_issued(dq, warntype[i])) {
Jan Kara8e893462007-10-16 23:29:31 -07001164#ifdef CONFIG_PRINT_QUOTA_WARNING
Steven Whitehouse86e931a2009-09-28 12:35:17 +01001165 print_warning(dq, warntype[i]);
Jan Kara8e893462007-10-16 23:29:31 -07001166#endif
Steven Whitehouse86e931a2009-09-28 12:35:17 +01001167 quota_send_warning(dq->dq_type, dq->dq_id,
1168 dq->dq_sb->s_dev, warntype[i]);
Jan Kara8e893462007-10-16 23:29:31 -07001169 }
Steven Whitehouse86e931a2009-09-28 12:35:17 +01001170 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171}
1172
Jan Kara7a2435d2009-01-27 01:47:11 +01001173static int ignore_hardlimit(struct dquot *dquot)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174{
1175 struct mem_dqinfo *info = &sb_dqopt(dquot->dq_sb)->info[dquot->dq_type];
1176
1177 return capable(CAP_SYS_RESOURCE) &&
Jan Kara268157b2009-01-27 15:47:22 +01001178 (info->dqi_format->qf_fmt_id != QFMT_VFS_OLD ||
1179 !(info->dqi_flags & V1_DQF_RSQUASH));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180}
1181
1182/* needs dq_data_lock */
Jan Kara12095462008-08-20 14:45:12 +02001183static int check_idq(struct dquot *dquot, qsize_t inodes, char *warntype)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184{
Jan Kara268157b2009-01-27 15:47:22 +01001185 qsize_t newinodes = dquot->dq_dqb.dqb_curinodes + inodes;
1186
Jan Kara8e893462007-10-16 23:29:31 -07001187 *warntype = QUOTA_NL_NOWARN;
Jan Karaf55abc02008-08-20 17:50:32 +02001188 if (!sb_has_quota_limits_enabled(dquot->dq_sb, dquot->dq_type) ||
1189 test_bit(DQ_FAKE_B, &dquot->dq_flags))
Christoph Hellwigefd8f0e2010-03-03 09:05:08 -05001190 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191
1192 if (dquot->dq_dqb.dqb_ihardlimit &&
Jan Kara268157b2009-01-27 15:47:22 +01001193 newinodes > dquot->dq_dqb.dqb_ihardlimit &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 !ignore_hardlimit(dquot)) {
Jan Kara8e893462007-10-16 23:29:31 -07001195 *warntype = QUOTA_NL_IHARDWARN;
Christoph Hellwigefd8f0e2010-03-03 09:05:08 -05001196 return -EDQUOT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 }
1198
1199 if (dquot->dq_dqb.dqb_isoftlimit &&
Jan Kara268157b2009-01-27 15:47:22 +01001200 newinodes > dquot->dq_dqb.dqb_isoftlimit &&
1201 dquot->dq_dqb.dqb_itime &&
1202 get_seconds() >= dquot->dq_dqb.dqb_itime &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 !ignore_hardlimit(dquot)) {
Jan Kara8e893462007-10-16 23:29:31 -07001204 *warntype = QUOTA_NL_ISOFTLONGWARN;
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 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 dquot->dq_dqb.dqb_itime == 0) {
Jan Kara8e893462007-10-16 23:29:31 -07001211 *warntype = QUOTA_NL_ISOFTWARN;
Jan Kara268157b2009-01-27 15:47:22 +01001212 dquot->dq_dqb.dqb_itime = get_seconds() +
1213 sb_dqopt(dquot->dq_sb)->info[dquot->dq_type].dqi_igrace;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 }
1215
Christoph Hellwigefd8f0e2010-03-03 09:05:08 -05001216 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217}
1218
1219/* needs dq_data_lock */
1220static int check_bdq(struct dquot *dquot, qsize_t space, int prealloc, char *warntype)
1221{
Mingming Caof18df222009-01-13 16:43:09 +01001222 qsize_t tspace;
Jan Kara268157b2009-01-27 15:47:22 +01001223 struct super_block *sb = dquot->dq_sb;
Mingming Caof18df222009-01-13 16:43:09 +01001224
Jan Kara8e893462007-10-16 23:29:31 -07001225 *warntype = QUOTA_NL_NOWARN;
Jan Kara268157b2009-01-27 15:47:22 +01001226 if (!sb_has_quota_limits_enabled(sb, dquot->dq_type) ||
Jan Karaf55abc02008-08-20 17:50:32 +02001227 test_bit(DQ_FAKE_B, &dquot->dq_flags))
Christoph Hellwigefd8f0e2010-03-03 09:05:08 -05001228 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229
Mingming Caof18df222009-01-13 16:43:09 +01001230 tspace = dquot->dq_dqb.dqb_curspace + dquot->dq_dqb.dqb_rsvspace
1231 + space;
1232
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 if (dquot->dq_dqb.dqb_bhardlimit &&
Mingming Caof18df222009-01-13 16:43:09 +01001234 tspace > dquot->dq_dqb.dqb_bhardlimit &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 !ignore_hardlimit(dquot)) {
1236 if (!prealloc)
Jan Kara8e893462007-10-16 23:29:31 -07001237 *warntype = QUOTA_NL_BHARDWARN;
Christoph Hellwigefd8f0e2010-03-03 09:05:08 -05001238 return -EDQUOT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 }
1240
1241 if (dquot->dq_dqb.dqb_bsoftlimit &&
Mingming Caof18df222009-01-13 16:43:09 +01001242 tspace > dquot->dq_dqb.dqb_bsoftlimit &&
Jan Kara268157b2009-01-27 15:47:22 +01001243 dquot->dq_dqb.dqb_btime &&
1244 get_seconds() >= dquot->dq_dqb.dqb_btime &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 !ignore_hardlimit(dquot)) {
1246 if (!prealloc)
Jan Kara8e893462007-10-16 23:29:31 -07001247 *warntype = QUOTA_NL_BSOFTLONGWARN;
Christoph Hellwigefd8f0e2010-03-03 09:05:08 -05001248 return -EDQUOT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 }
1250
1251 if (dquot->dq_dqb.dqb_bsoftlimit &&
Mingming Caof18df222009-01-13 16:43:09 +01001252 tspace > dquot->dq_dqb.dqb_bsoftlimit &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 dquot->dq_dqb.dqb_btime == 0) {
1254 if (!prealloc) {
Jan Kara8e893462007-10-16 23:29:31 -07001255 *warntype = QUOTA_NL_BSOFTWARN;
Jan Kara268157b2009-01-27 15:47:22 +01001256 dquot->dq_dqb.dqb_btime = get_seconds() +
1257 sb_dqopt(sb)->info[dquot->dq_type].dqi_bgrace;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 }
1259 else
1260 /*
1261 * We don't allow preallocation to exceed softlimit so exceeding will
1262 * be always printed
1263 */
Christoph Hellwigefd8f0e2010-03-03 09:05:08 -05001264 return -EDQUOT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 }
1266
Christoph Hellwigefd8f0e2010-03-03 09:05:08 -05001267 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268}
1269
Jan Kara12095462008-08-20 14:45:12 +02001270static int info_idq_free(struct dquot *dquot, qsize_t inodes)
Jan Kara657d3bf2008-07-25 01:46:52 -07001271{
Jan Kara268157b2009-01-27 15:47:22 +01001272 qsize_t newinodes;
1273
Jan Kara657d3bf2008-07-25 01:46:52 -07001274 if (test_bit(DQ_FAKE_B, &dquot->dq_flags) ||
Jan Karaf55abc02008-08-20 17:50:32 +02001275 dquot->dq_dqb.dqb_curinodes <= dquot->dq_dqb.dqb_isoftlimit ||
1276 !sb_has_quota_limits_enabled(dquot->dq_sb, dquot->dq_type))
Jan Kara657d3bf2008-07-25 01:46:52 -07001277 return QUOTA_NL_NOWARN;
1278
Jan Kara268157b2009-01-27 15:47:22 +01001279 newinodes = dquot->dq_dqb.dqb_curinodes - inodes;
1280 if (newinodes <= dquot->dq_dqb.dqb_isoftlimit)
Jan Kara657d3bf2008-07-25 01:46:52 -07001281 return QUOTA_NL_ISOFTBELOW;
1282 if (dquot->dq_dqb.dqb_curinodes >= dquot->dq_dqb.dqb_ihardlimit &&
Jan Kara268157b2009-01-27 15:47:22 +01001283 newinodes < dquot->dq_dqb.dqb_ihardlimit)
Jan Kara657d3bf2008-07-25 01:46:52 -07001284 return QUOTA_NL_IHARDBELOW;
1285 return QUOTA_NL_NOWARN;
1286}
1287
1288static int info_bdq_free(struct dquot *dquot, qsize_t space)
1289{
1290 if (test_bit(DQ_FAKE_B, &dquot->dq_flags) ||
Jan Kara12095462008-08-20 14:45:12 +02001291 dquot->dq_dqb.dqb_curspace <= dquot->dq_dqb.dqb_bsoftlimit)
Jan Kara657d3bf2008-07-25 01:46:52 -07001292 return QUOTA_NL_NOWARN;
1293
Jan Kara12095462008-08-20 14:45:12 +02001294 if (dquot->dq_dqb.dqb_curspace - space <= dquot->dq_dqb.dqb_bsoftlimit)
Jan Kara657d3bf2008-07-25 01:46:52 -07001295 return QUOTA_NL_BSOFTBELOW;
Jan Kara12095462008-08-20 14:45:12 +02001296 if (dquot->dq_dqb.dqb_curspace >= dquot->dq_dqb.dqb_bhardlimit &&
1297 dquot->dq_dqb.dqb_curspace - space < dquot->dq_dqb.dqb_bhardlimit)
Jan Kara657d3bf2008-07-25 01:46:52 -07001298 return QUOTA_NL_BHARDBELOW;
1299 return QUOTA_NL_NOWARN;
1300}
Jan Kara0a5a9c72010-02-09 18:20:39 +01001301
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302/*
Christoph Hellwig871a2932010-03-03 09:05:07 -05001303 * Initialize quota pointers in inode
1304 *
1305 * We do things in a bit complicated way but by that we avoid calling
1306 * dqget() and thus filesystem callbacks under dqptr_sem.
1307 *
1308 * It is better to call this function outside of any transaction as it
1309 * might need a lot of space in journal for dquot structure allocation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 */
Christoph Hellwig871a2932010-03-03 09:05:07 -05001311static void __dquot_initialize(struct inode *inode, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312{
1313 unsigned int id = 0;
Christoph Hellwig871a2932010-03-03 09:05:07 -05001314 int cnt;
Dmitry Monakhovad1e6e82010-02-16 08:31:49 +03001315 struct dquot *got[MAXQUOTAS];
Jan Karacc334122009-01-12 17:23:05 +01001316 struct super_block *sb = inode->i_sb;
Jan Kara0a5a9c72010-02-09 18:20:39 +01001317 qsize_t rsv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318
Ingo Molnard3be9152006-03-23 03:00:29 -08001319 /* First test before acquiring mutex - solves deadlocks when we
1320 * re-enter the quota code and are already holding the mutex */
Christoph Hellwig871a2932010-03-03 09:05:07 -05001321 if (!sb_any_quota_active(inode->i_sb) || IS_NOQUOTA(inode))
1322 return;
Jan Karacc334122009-01-12 17:23:05 +01001323
1324 /* First get references to structures we might need. */
1325 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
Dmitry Monakhovad1e6e82010-02-16 08:31:49 +03001326 got[cnt] = NULL;
Jan Karacc334122009-01-12 17:23:05 +01001327 if (type != -1 && cnt != type)
1328 continue;
1329 switch (cnt) {
1330 case USRQUOTA:
1331 id = inode->i_uid;
1332 break;
1333 case GRPQUOTA:
1334 id = inode->i_gid;
1335 break;
1336 }
1337 got[cnt] = dqget(sb, id, cnt);
1338 }
1339
1340 down_write(&sb_dqopt(sb)->dqptr_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 if (IS_NOQUOTA(inode))
1342 goto out_err;
1343 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1344 if (type != -1 && cnt != type)
1345 continue;
Jan Karacc334122009-01-12 17:23:05 +01001346 /* Avoid races with quotaoff() */
1347 if (!sb_has_quota_active(sb, cnt))
1348 continue;
Jan Karadd6f3c62009-01-26 16:01:43 +01001349 if (!inode->i_dquot[cnt]) {
Jan Karacc334122009-01-12 17:23:05 +01001350 inode->i_dquot[cnt] = got[cnt];
Jan Karadd6f3c62009-01-26 16:01:43 +01001351 got[cnt] = NULL;
Jan Kara0a5a9c72010-02-09 18:20:39 +01001352 /*
1353 * Make quota reservation system happy if someone
1354 * did a write before quota was turned on
1355 */
1356 rsv = inode_get_rsv_space(inode);
1357 if (unlikely(rsv))
1358 dquot_resv_space(inode->i_dquot[cnt], rsv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 }
1360 }
1361out_err:
Jan Karacc334122009-01-12 17:23:05 +01001362 up_write(&sb_dqopt(sb)->dqptr_sem);
1363 /* Drop unused references */
Dmitry Monakhovdc52dd32009-12-14 15:21:15 +03001364 dqput_all(got);
Christoph Hellwig871a2932010-03-03 09:05:07 -05001365}
1366
1367void dquot_initialize(struct inode *inode)
1368{
1369 __dquot_initialize(inode, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370}
Mingming Cao08d03502009-01-14 16:19:32 +01001371EXPORT_SYMBOL(dquot_initialize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372
1373/*
1374 * Release all quotas referenced by inode
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 */
Christoph Hellwig9f754752010-03-03 09:05:05 -05001376static void __dquot_drop(struct inode *inode)
Jan Kara3d9ea252008-10-10 16:12:23 +02001377{
Jan Karacc334122009-01-12 17:23:05 +01001378 int cnt;
1379 struct dquot *put[MAXQUOTAS];
1380
Jan Kara3d9ea252008-10-10 16:12:23 +02001381 down_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
Jan Karacc334122009-01-12 17:23:05 +01001382 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1383 put[cnt] = inode->i_dquot[cnt];
Jan Karadd6f3c62009-01-26 16:01:43 +01001384 inode->i_dquot[cnt] = NULL;
Jan Karacc334122009-01-12 17:23:05 +01001385 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 up_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
Dmitry Monakhovdc52dd32009-12-14 15:21:15 +03001387 dqput_all(put);
Christoph Hellwig9f754752010-03-03 09:05:05 -05001388}
1389
1390void dquot_drop(struct inode *inode)
1391{
1392 int cnt;
1393
1394 if (IS_NOQUOTA(inode))
1395 return;
1396
1397 /*
1398 * Test before calling to rule out calls from proc and such
1399 * where we are not allowed to block. Note that this is
1400 * actually reliable test even without the lock - the caller
1401 * must assure that nobody can come after the DQUOT_DROP and
1402 * add quota pointers back anyway.
1403 */
1404 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1405 if (inode->i_dquot[cnt])
1406 break;
1407 }
1408
1409 if (cnt < MAXQUOTAS)
1410 __dquot_drop(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411}
Mingming Cao08d03502009-01-14 16:19:32 +01001412EXPORT_SYMBOL(dquot_drop);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413
1414/*
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001415 * inode_reserved_space is managed internally by quota, and protected by
1416 * i_lock similar to i_blocks+i_bytes.
1417 */
1418static qsize_t *inode_reserved_space(struct inode * inode)
1419{
1420 /* Filesystem must explicitly define it's own method in order to use
1421 * quota reservation interface */
1422 BUG_ON(!inode->i_sb->dq_op->get_reserved_space);
1423 return inode->i_sb->dq_op->get_reserved_space(inode);
1424}
1425
Dmitry Monakhovc4690702010-02-09 17:53:36 +01001426void inode_add_rsv_space(struct inode *inode, qsize_t number)
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001427{
1428 spin_lock(&inode->i_lock);
1429 *inode_reserved_space(inode) += number;
1430 spin_unlock(&inode->i_lock);
1431}
Dmitry Monakhovc4690702010-02-09 17:53:36 +01001432EXPORT_SYMBOL(inode_add_rsv_space);
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001433
Dmitry Monakhovc4690702010-02-09 17:53:36 +01001434void inode_claim_rsv_space(struct inode *inode, qsize_t number)
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001435{
1436 spin_lock(&inode->i_lock);
1437 *inode_reserved_space(inode) -= number;
1438 __inode_add_bytes(inode, number);
1439 spin_unlock(&inode->i_lock);
1440}
Dmitry Monakhovc4690702010-02-09 17:53:36 +01001441EXPORT_SYMBOL(inode_claim_rsv_space);
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001442
Dmitry Monakhovc4690702010-02-09 17:53:36 +01001443void inode_sub_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 spin_unlock(&inode->i_lock);
1448}
Dmitry Monakhovc4690702010-02-09 17:53:36 +01001449EXPORT_SYMBOL(inode_sub_rsv_space);
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001450
1451static qsize_t inode_get_rsv_space(struct inode *inode)
1452{
1453 qsize_t ret;
Jan Kara05b5d892010-01-06 18:03:36 +01001454
1455 if (!inode->i_sb->dq_op->get_reserved_space)
1456 return 0;
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001457 spin_lock(&inode->i_lock);
1458 ret = *inode_reserved_space(inode);
1459 spin_unlock(&inode->i_lock);
1460 return ret;
1461}
1462
1463static void inode_incr_space(struct inode *inode, qsize_t number,
1464 int reserve)
1465{
1466 if (reserve)
1467 inode_add_rsv_space(inode, number);
1468 else
1469 inode_add_bytes(inode, number);
1470}
1471
1472static void inode_decr_space(struct inode *inode, qsize_t number, int reserve)
1473{
1474 if (reserve)
1475 inode_sub_rsv_space(inode, number);
1476 else
1477 inode_sub_bytes(inode, number);
1478}
1479
1480/*
Christoph Hellwig5dd40562010-03-03 09:05:00 -05001481 * This functions updates i_blocks+i_bytes fields and quota information
1482 * (together with appropriate checks).
1483 *
1484 * NOTE: We absolutely rely on the fact that caller dirties the inode
1485 * (usually helpers in quotaops.h care about this) and holds a handle for
1486 * the current transaction so that dquot write and inode write go into the
1487 * same transaction.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488 */
1489
1490/*
1491 * This operation can block, but only after everything is updated
1492 */
Mingming Caof18df222009-01-13 16:43:09 +01001493int __dquot_alloc_space(struct inode *inode, qsize_t number,
Christoph Hellwig5dd40562010-03-03 09:05:00 -05001494 int warn, int reserve)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495{
Christoph Hellwig5dd40562010-03-03 09:05:00 -05001496 int cnt, ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 char warntype[MAXQUOTAS];
1498
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001499 /*
1500 * First test before acquiring mutex - solves deadlocks when we
1501 * re-enter the quota code and are already holding the mutex
1502 */
Christoph Hellwig5dd40562010-03-03 09:05:00 -05001503 if (!sb_any_quota_active(inode->i_sb) || IS_NOQUOTA(inode)) {
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001504 inode_incr_space(inode, number, reserve);
1505 goto out;
1506 }
1507
1508 down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
Jan Kara8e893462007-10-16 23:29:31 -07001510 warntype[cnt] = QUOTA_NL_NOWARN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512 spin_lock(&dq_data_lock);
1513 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
Jan Karadd6f3c62009-01-26 16:01:43 +01001514 if (!inode->i_dquot[cnt])
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515 continue;
Christoph Hellwigefd8f0e2010-03-03 09:05:08 -05001516 ret = check_bdq(inode->i_dquot[cnt], number, !warn,
1517 warntype+cnt);
1518 if (ret) {
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001519 spin_unlock(&dq_data_lock);
1520 goto out_flush_warn;
Mingming Caof18df222009-01-13 16:43:09 +01001521 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522 }
1523 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
Jan Karadd6f3c62009-01-26 16:01:43 +01001524 if (!inode->i_dquot[cnt])
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525 continue;
Mingming Caof18df222009-01-13 16:43:09 +01001526 if (reserve)
1527 dquot_resv_space(inode->i_dquot[cnt], number);
1528 else
1529 dquot_incr_space(inode->i_dquot[cnt], number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530 }
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001531 inode_incr_space(inode, number, reserve);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532 spin_unlock(&dq_data_lock);
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001533
1534 if (reserve)
1535 goto out_flush_warn;
Dmitry Monakhovdc52dd32009-12-14 15:21:15 +03001536 mark_all_dquot_dirty(inode->i_dquot);
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001537out_flush_warn:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538 flush_warnings(inode->i_dquot, warntype);
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001539 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1540out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541 return ret;
1542}
Christoph Hellwig5dd40562010-03-03 09:05:00 -05001543EXPORT_SYMBOL(__dquot_alloc_space);
Mingming Caof18df222009-01-13 16:43:09 +01001544
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545/*
1546 * This operation can block, but only after everything is updated
1547 */
Christoph Hellwig63936dd2010-03-03 09:05:01 -05001548int dquot_alloc_inode(const struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549{
Christoph Hellwigefd8f0e2010-03-03 09:05:08 -05001550 int cnt, ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551 char warntype[MAXQUOTAS];
1552
Ingo Molnard3be9152006-03-23 03:00:29 -08001553 /* First test before acquiring mutex - solves deadlocks when we
1554 * re-enter the quota code and are already holding the mutex */
Christoph Hellwig63936dd2010-03-03 09:05:01 -05001555 if (!sb_any_quota_active(inode->i_sb) || IS_NOQUOTA(inode))
1556 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
Jan Kara8e893462007-10-16 23:29:31 -07001558 warntype[cnt] = QUOTA_NL_NOWARN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559 down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560 spin_lock(&dq_data_lock);
1561 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
Jan Karadd6f3c62009-01-26 16:01:43 +01001562 if (!inode->i_dquot[cnt])
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563 continue;
Christoph Hellwigefd8f0e2010-03-03 09:05:08 -05001564 ret = check_idq(inode->i_dquot[cnt], 1, warntype + cnt);
1565 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566 goto warn_put_all;
1567 }
1568
1569 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
Jan Karadd6f3c62009-01-26 16:01:43 +01001570 if (!inode->i_dquot[cnt])
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571 continue;
Christoph Hellwig63936dd2010-03-03 09:05:01 -05001572 dquot_incr_inodes(inode->i_dquot[cnt], 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573 }
Christoph Hellwigefd8f0e2010-03-03 09:05:08 -05001574
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575warn_put_all:
1576 spin_unlock(&dq_data_lock);
Christoph Hellwig63936dd2010-03-03 09:05:01 -05001577 if (ret == 0)
Dmitry Monakhovdc52dd32009-12-14 15:21:15 +03001578 mark_all_dquot_dirty(inode->i_dquot);
Jan Kara087ee8d2007-12-17 16:20:26 -08001579 flush_warnings(inode->i_dquot, warntype);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1581 return ret;
1582}
Mingming Cao08d03502009-01-14 16:19:32 +01001583EXPORT_SYMBOL(dquot_alloc_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584
Christoph Hellwig5dd40562010-03-03 09:05:00 -05001585/*
1586 * Convert in-memory reserved quotas to real consumed quotas
1587 */
1588int dquot_claim_space_nodirty(struct inode *inode, qsize_t number)
Mingming Cao740d9dc2009-01-13 16:43:14 +01001589{
1590 int cnt;
Mingming Cao740d9dc2009-01-13 16:43:14 +01001591
Christoph Hellwig5dd40562010-03-03 09:05:00 -05001592 if (!sb_any_quota_active(inode->i_sb) || IS_NOQUOTA(inode)) {
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001593 inode_claim_rsv_space(inode, number);
Christoph Hellwig5dd40562010-03-03 09:05:00 -05001594 return 0;
Mingming Cao740d9dc2009-01-13 16:43:14 +01001595 }
1596
1597 down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
Mingming Cao740d9dc2009-01-13 16:43:14 +01001598 spin_lock(&dq_data_lock);
1599 /* Claim reserved quotas to allocated quotas */
1600 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
Jan Karadd6f3c62009-01-26 16:01:43 +01001601 if (inode->i_dquot[cnt])
Mingming Cao740d9dc2009-01-13 16:43:14 +01001602 dquot_claim_reserved_space(inode->i_dquot[cnt],
1603 number);
1604 }
1605 /* Update inode bytes */
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001606 inode_claim_rsv_space(inode, number);
Mingming Cao740d9dc2009-01-13 16:43:14 +01001607 spin_unlock(&dq_data_lock);
Dmitry Monakhovdc52dd32009-12-14 15:21:15 +03001608 mark_all_dquot_dirty(inode->i_dquot);
Mingming Cao740d9dc2009-01-13 16:43:14 +01001609 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
Christoph Hellwig5dd40562010-03-03 09:05:00 -05001610 return 0;
Mingming Cao740d9dc2009-01-13 16:43:14 +01001611}
Christoph Hellwig5dd40562010-03-03 09:05:00 -05001612EXPORT_SYMBOL(dquot_claim_space_nodirty);
Mingming Cao740d9dc2009-01-13 16:43:14 +01001613
1614/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615 * This operation can block, but only after everything is updated
1616 */
Christoph Hellwig5dd40562010-03-03 09:05:00 -05001617void __dquot_free_space(struct inode *inode, qsize_t number, int reserve)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618{
1619 unsigned int cnt;
Jan Kara657d3bf2008-07-25 01:46:52 -07001620 char warntype[MAXQUOTAS];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621
Ingo Molnard3be9152006-03-23 03:00:29 -08001622 /* First test before acquiring mutex - solves deadlocks when we
1623 * re-enter the quota code and are already holding the mutex */
Christoph Hellwig5dd40562010-03-03 09:05:00 -05001624 if (!sb_any_quota_active(inode->i_sb) || IS_NOQUOTA(inode)) {
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001625 inode_decr_space(inode, number, reserve);
Christoph Hellwig5dd40562010-03-03 09:05:00 -05001626 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627 }
Jan Kara657d3bf2008-07-25 01:46:52 -07001628
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629 down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630 spin_lock(&dq_data_lock);
1631 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
Jan Karadd6f3c62009-01-26 16:01:43 +01001632 if (!inode->i_dquot[cnt])
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633 continue;
Jan Kara657d3bf2008-07-25 01:46:52 -07001634 warntype[cnt] = info_bdq_free(inode->i_dquot[cnt], number);
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001635 if (reserve)
1636 dquot_free_reserved_space(inode->i_dquot[cnt], number);
1637 else
1638 dquot_decr_space(inode->i_dquot[cnt], number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639 }
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001640 inode_decr_space(inode, number, reserve);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641 spin_unlock(&dq_data_lock);
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001642
1643 if (reserve)
1644 goto out_unlock;
Dmitry Monakhovdc52dd32009-12-14 15:21:15 +03001645 mark_all_dquot_dirty(inode->i_dquot);
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001646out_unlock:
Jan Kara657d3bf2008-07-25 01:46:52 -07001647 flush_warnings(inode->i_dquot, warntype);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649}
Christoph Hellwig5dd40562010-03-03 09:05:00 -05001650EXPORT_SYMBOL(__dquot_free_space);
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001651
1652/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653 * This operation can block, but only after everything is updated
1654 */
Christoph Hellwig63936dd2010-03-03 09:05:01 -05001655void dquot_free_inode(const struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656{
1657 unsigned int cnt;
Jan Kara657d3bf2008-07-25 01:46:52 -07001658 char warntype[MAXQUOTAS];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659
Ingo Molnard3be9152006-03-23 03:00:29 -08001660 /* First test before acquiring mutex - solves deadlocks when we
1661 * re-enter the quota code and are already holding the mutex */
Christoph Hellwig63936dd2010-03-03 09:05:01 -05001662 if (!sb_any_quota_active(inode->i_sb) || IS_NOQUOTA(inode))
1663 return;
Jan Kara657d3bf2008-07-25 01:46:52 -07001664
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665 down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666 spin_lock(&dq_data_lock);
1667 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
Jan Karadd6f3c62009-01-26 16:01:43 +01001668 if (!inode->i_dquot[cnt])
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669 continue;
Christoph Hellwig63936dd2010-03-03 09:05:01 -05001670 warntype[cnt] = info_idq_free(inode->i_dquot[cnt], 1);
1671 dquot_decr_inodes(inode->i_dquot[cnt], 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672 }
1673 spin_unlock(&dq_data_lock);
Dmitry Monakhovdc52dd32009-12-14 15:21:15 +03001674 mark_all_dquot_dirty(inode->i_dquot);
Jan Kara657d3bf2008-07-25 01:46:52 -07001675 flush_warnings(inode->i_dquot, warntype);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677}
Mingming Cao08d03502009-01-14 16:19:32 +01001678EXPORT_SYMBOL(dquot_free_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679
1680/*
1681 * Transfer the number of inode and blocks from one diskquota to an other.
1682 *
1683 * This operation can block, but only after everything is updated
1684 * A transaction must be started when entering this function.
1685 */
Christoph Hellwigb43fa822010-03-03 09:05:03 -05001686static int __dquot_transfer(struct inode *inode, qid_t *chid, unsigned long mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687{
Mingming Cao740d9dc2009-01-13 16:43:14 +01001688 qsize_t space, cur_space;
1689 qsize_t rsv_space = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690 struct dquot *transfer_from[MAXQUOTAS];
1691 struct dquot *transfer_to[MAXQUOTAS];
Christoph Hellwigefd8f0e2010-03-03 09:05:08 -05001692 int cnt, ret = 0;
Jan Kara657d3bf2008-07-25 01:46:52 -07001693 char warntype_to[MAXQUOTAS];
1694 char warntype_from_inodes[MAXQUOTAS], warntype_from_space[MAXQUOTAS];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695
Ingo Molnard3be9152006-03-23 03:00:29 -08001696 /* First test before acquiring mutex - solves deadlocks when we
1697 * re-enter the quota code and are already holding the mutex */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698 if (IS_NOQUOTA(inode))
Christoph Hellwigefd8f0e2010-03-03 09:05:08 -05001699 return 0;
Jan Karacc334122009-01-12 17:23:05 +01001700 /* Initialize the arrays */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
Jan Karadd6f3c62009-01-26 16:01:43 +01001702 transfer_from[cnt] = NULL;
1703 transfer_to[cnt] = NULL;
Jan Kara657d3bf2008-07-25 01:46:52 -07001704 warntype_to[cnt] = QUOTA_NL_NOWARN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705 }
Dmitry Monakhov8ddd69d2010-02-16 08:31:50 +03001706 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1707 if (mask & (1 << cnt))
1708 transfer_to[cnt] = dqget(inode->i_sb, chid[cnt], cnt);
1709 }
Jan Karacc334122009-01-12 17:23:05 +01001710 down_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
Jan Karacc334122009-01-12 17:23:05 +01001711 if (IS_NOQUOTA(inode)) { /* File without quota accounting? */
1712 up_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
1713 goto put_all;
1714 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715 spin_lock(&dq_data_lock);
Mingming Cao740d9dc2009-01-13 16:43:14 +01001716 cur_space = inode_get_bytes(inode);
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001717 rsv_space = inode_get_rsv_space(inode);
Mingming Cao740d9dc2009-01-13 16:43:14 +01001718 space = cur_space + rsv_space;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719 /* Build the transfer_from list and check the limits */
1720 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
Jan Karadd6f3c62009-01-26 16:01:43 +01001721 if (!transfer_to[cnt])
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722 continue;
1723 transfer_from[cnt] = inode->i_dquot[cnt];
Christoph Hellwigefd8f0e2010-03-03 09:05:08 -05001724 ret = check_idq(transfer_to[cnt], 1, warntype_to + cnt);
1725 if (ret)
1726 goto over_quota;
1727 ret = check_bdq(transfer_to[cnt], space, 0, warntype_to + cnt);
1728 if (ret)
Jan Karacc334122009-01-12 17:23:05 +01001729 goto over_quota;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730 }
1731
1732 /*
1733 * Finally perform the needed transfer from transfer_from to transfer_to
1734 */
1735 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1736 /*
1737 * Skip changes for same uid or gid or for turned off quota-type.
1738 */
Jan Karadd6f3c62009-01-26 16:01:43 +01001739 if (!transfer_to[cnt])
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740 continue;
1741
1742 /* Due to IO error we might not have transfer_from[] structure */
1743 if (transfer_from[cnt]) {
Jan Kara657d3bf2008-07-25 01:46:52 -07001744 warntype_from_inodes[cnt] =
1745 info_idq_free(transfer_from[cnt], 1);
1746 warntype_from_space[cnt] =
1747 info_bdq_free(transfer_from[cnt], space);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748 dquot_decr_inodes(transfer_from[cnt], 1);
Mingming Cao740d9dc2009-01-13 16:43:14 +01001749 dquot_decr_space(transfer_from[cnt], cur_space);
1750 dquot_free_reserved_space(transfer_from[cnt],
1751 rsv_space);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752 }
1753
1754 dquot_incr_inodes(transfer_to[cnt], 1);
Mingming Cao740d9dc2009-01-13 16:43:14 +01001755 dquot_incr_space(transfer_to[cnt], cur_space);
1756 dquot_resv_space(transfer_to[cnt], rsv_space);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757
1758 inode->i_dquot[cnt] = transfer_to[cnt];
1759 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760 spin_unlock(&dq_data_lock);
Jan Karacc334122009-01-12 17:23:05 +01001761 up_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
1762
Dmitry Monakhovdc52dd32009-12-14 15:21:15 +03001763 mark_all_dquot_dirty(transfer_from);
1764 mark_all_dquot_dirty(transfer_to);
1765 /* The reference we got is transferred to the inode */
1766 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1767 transfer_to[cnt] = NULL;
Jan Karacc334122009-01-12 17:23:05 +01001768warn_put_all:
Jan Kara657d3bf2008-07-25 01:46:52 -07001769 flush_warnings(transfer_to, warntype_to);
1770 flush_warnings(transfer_from, warntype_from_inodes);
1771 flush_warnings(transfer_from, warntype_from_space);
Jan Karacc334122009-01-12 17:23:05 +01001772put_all:
Dmitry Monakhovdc52dd32009-12-14 15:21:15 +03001773 dqput_all(transfer_from);
1774 dqput_all(transfer_to);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775 return ret;
Jan Karacc334122009-01-12 17:23:05 +01001776over_quota:
1777 spin_unlock(&dq_data_lock);
1778 up_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
1779 /* Clear dquot pointers we don't want to dqput() */
1780 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
Jan Karadd6f3c62009-01-26 16:01:43 +01001781 transfer_from[cnt] = NULL;
Jan Karacc334122009-01-12 17:23:05 +01001782 goto warn_put_all;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783}
1784
Dmitry Monakhov8ddd69d2010-02-16 08:31:50 +03001785/* Wrapper for transferring ownership of an inode for uid/gid only
1786 * Called from FSXXX_setattr()
1787 */
Christoph Hellwigb43fa822010-03-03 09:05:03 -05001788int dquot_transfer(struct inode *inode, struct iattr *iattr)
Jan Karab85f4b82008-07-25 01:46:50 -07001789{
Dmitry Monakhov8ddd69d2010-02-16 08:31:50 +03001790 qid_t chid[MAXQUOTAS];
1791 unsigned long mask = 0;
1792
1793 if (iattr->ia_valid & ATTR_UID && iattr->ia_uid != inode->i_uid) {
1794 mask |= 1 << USRQUOTA;
1795 chid[USRQUOTA] = iattr->ia_uid;
1796 }
1797 if (iattr->ia_valid & ATTR_GID && iattr->ia_gid != inode->i_gid) {
1798 mask |= 1 << GRPQUOTA;
1799 chid[GRPQUOTA] = iattr->ia_gid;
1800 }
Jan Karaf55abc02008-08-20 17:50:32 +02001801 if (sb_any_quota_active(inode->i_sb) && !IS_NOQUOTA(inode)) {
Christoph Hellwig871a2932010-03-03 09:05:07 -05001802 dquot_initialize(inode);
Christoph Hellwigefd8f0e2010-03-03 09:05:08 -05001803 return __dquot_transfer(inode, chid, mask);
Jan Karab85f4b82008-07-25 01:46:50 -07001804 }
1805 return 0;
1806}
Christoph Hellwigb43fa822010-03-03 09:05:03 -05001807EXPORT_SYMBOL(dquot_transfer);
Jan Karab85f4b82008-07-25 01:46:50 -07001808
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809/*
1810 * Write info of quota file to disk
1811 */
1812int dquot_commit_info(struct super_block *sb, int type)
1813{
1814 int ret;
1815 struct quota_info *dqopt = sb_dqopt(sb);
1816
Ingo Molnard3be9152006-03-23 03:00:29 -08001817 mutex_lock(&dqopt->dqio_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818 ret = dqopt->ops[type]->write_file_info(sb, type);
Ingo Molnard3be9152006-03-23 03:00:29 -08001819 mutex_unlock(&dqopt->dqio_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820 return ret;
1821}
Mingming Cao08d03502009-01-14 16:19:32 +01001822EXPORT_SYMBOL(dquot_commit_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823
1824/*
1825 * Definitions of diskquota operations.
1826 */
Alexey Dobriyan61e225d2009-09-21 17:01:08 -07001827const struct dquot_operations dquot_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828 .write_dquot = dquot_commit,
1829 .acquire_dquot = dquot_acquire,
1830 .release_dquot = dquot_release,
1831 .mark_dirty = dquot_mark_dquot_dirty,
Jan Kara74f783a2008-08-19 14:51:22 +02001832 .write_info = dquot_commit_info,
1833 .alloc_dquot = dquot_alloc,
1834 .destroy_dquot = dquot_destroy,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835};
1836
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837/*
Christoph Hellwig907f4552010-03-03 09:05:06 -05001838 * Generic helper for ->open on filesystems supporting disk quotas.
1839 */
1840int dquot_file_open(struct inode *inode, struct file *file)
1841{
1842 int error;
1843
1844 error = generic_file_open(inode, file);
1845 if (!error && (file->f_mode & FMODE_WRITE))
Christoph Hellwig871a2932010-03-03 09:05:07 -05001846 dquot_initialize(inode);
Christoph Hellwig907f4552010-03-03 09:05:06 -05001847 return error;
1848}
1849EXPORT_SYMBOL(dquot_file_open);
1850
1851/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852 * Turn quota off on a device. type == -1 ==> quotaoff for all types (umount)
1853 */
Jan Karaf55abc02008-08-20 17:50:32 +02001854int vfs_quota_disable(struct super_block *sb, int type, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855{
Jan Kara0ff5af82008-04-28 02:14:33 -07001856 int cnt, ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857 struct quota_info *dqopt = sb_dqopt(sb);
1858 struct inode *toputinode[MAXQUOTAS];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859
Jan Karaf55abc02008-08-20 17:50:32 +02001860 /* Cannot turn off usage accounting without turning off limits, or
1861 * suspend quotas and simultaneously turn quotas off. */
1862 if ((flags & DQUOT_USAGE_ENABLED && !(flags & DQUOT_LIMITS_ENABLED))
1863 || (flags & DQUOT_SUSPENDED && flags & (DQUOT_LIMITS_ENABLED |
1864 DQUOT_USAGE_ENABLED)))
1865 return -EINVAL;
1866
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867 /* We need to serialize quota_off() for device */
Ingo Molnard3be9152006-03-23 03:00:29 -08001868 mutex_lock(&dqopt->dqonoff_mutex);
Jan Kara9377abd2008-05-12 14:02:08 -07001869
1870 /*
1871 * Skip everything if there's nothing to do. We have to do this because
1872 * sometimes we are called when fill_super() failed and calling
1873 * sync_fs() in such cases does no good.
1874 */
Jan Karaf55abc02008-08-20 17:50:32 +02001875 if (!sb_any_quota_loaded(sb)) {
Jan Kara9377abd2008-05-12 14:02:08 -07001876 mutex_unlock(&dqopt->dqonoff_mutex);
1877 return 0;
1878 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1880 toputinode[cnt] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881 if (type != -1 && cnt != type)
1882 continue;
Jan Karaf55abc02008-08-20 17:50:32 +02001883 if (!sb_has_quota_loaded(sb, cnt))
Jan Kara0ff5af82008-04-28 02:14:33 -07001884 continue;
Jan Karaf55abc02008-08-20 17:50:32 +02001885
1886 if (flags & DQUOT_SUSPENDED) {
Jan Karacc334122009-01-12 17:23:05 +01001887 spin_lock(&dq_state_lock);
Jan Karaf55abc02008-08-20 17:50:32 +02001888 dqopt->flags |=
1889 dquot_state_flag(DQUOT_SUSPENDED, cnt);
Jan Karacc334122009-01-12 17:23:05 +01001890 spin_unlock(&dq_state_lock);
Jan Karaf55abc02008-08-20 17:50:32 +02001891 } else {
Jan Karacc334122009-01-12 17:23:05 +01001892 spin_lock(&dq_state_lock);
Jan Karaf55abc02008-08-20 17:50:32 +02001893 dqopt->flags &= ~dquot_state_flag(flags, cnt);
1894 /* Turning off suspended quotas? */
1895 if (!sb_has_quota_loaded(sb, cnt) &&
1896 sb_has_quota_suspended(sb, cnt)) {
1897 dqopt->flags &= ~dquot_state_flag(
1898 DQUOT_SUSPENDED, cnt);
Jan Karacc334122009-01-12 17:23:05 +01001899 spin_unlock(&dq_state_lock);
Jan Karaf55abc02008-08-20 17:50:32 +02001900 iput(dqopt->files[cnt]);
1901 dqopt->files[cnt] = NULL;
1902 continue;
1903 }
Jan Karacc334122009-01-12 17:23:05 +01001904 spin_unlock(&dq_state_lock);
Jan Kara0ff5af82008-04-28 02:14:33 -07001905 }
Jan Karaf55abc02008-08-20 17:50:32 +02001906
1907 /* We still have to keep quota loaded? */
1908 if (sb_has_quota_loaded(sb, cnt) && !(flags & DQUOT_SUSPENDED))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910
1911 /* Note: these are blocking operations */
1912 drop_dquot_ref(sb, cnt);
1913 invalidate_dquots(sb, cnt);
1914 /*
Jan Kara268157b2009-01-27 15:47:22 +01001915 * Now all dquots should be invalidated, all writes done so we
1916 * should be only users of the info. No locks needed.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917 */
1918 if (info_dirty(&dqopt->info[cnt]))
1919 sb->dq_op->write_info(sb, cnt);
1920 if (dqopt->ops[cnt]->free_file_info)
1921 dqopt->ops[cnt]->free_file_info(sb, cnt);
1922 put_quota_format(dqopt->info[cnt].dqi_format);
1923
1924 toputinode[cnt] = dqopt->files[cnt];
Jan Karaf55abc02008-08-20 17:50:32 +02001925 if (!sb_has_quota_loaded(sb, cnt))
Jan Kara0ff5af82008-04-28 02:14:33 -07001926 dqopt->files[cnt] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927 dqopt->info[cnt].dqi_flags = 0;
1928 dqopt->info[cnt].dqi_igrace = 0;
1929 dqopt->info[cnt].dqi_bgrace = 0;
1930 dqopt->ops[cnt] = NULL;
1931 }
Ingo Molnard3be9152006-03-23 03:00:29 -08001932 mutex_unlock(&dqopt->dqonoff_mutex);
Jan Karaca785ec2008-09-30 17:53:37 +02001933
1934 /* Skip syncing and setting flags if quota files are hidden */
1935 if (dqopt->flags & DQUOT_QUOTA_SYS_FILE)
1936 goto put_inodes;
1937
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938 /* Sync the superblock so that buffers with quota data are written to
Al Viro7b7b1ac2005-11-07 17:13:39 -05001939 * disk (and so userspace sees correct data afterwards). */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940 if (sb->s_op->sync_fs)
1941 sb->s_op->sync_fs(sb, 1);
1942 sync_blockdev(sb->s_bdev);
1943 /* Now the quota files are just ordinary files and we can set the
1944 * inode flags back. Moreover we discard the pagecache so that
1945 * userspace sees the writes we did bypassing the pagecache. We
1946 * must also discard the blockdev buffers so that we see the
1947 * changes done by userspace on the next quotaon() */
1948 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1949 if (toputinode[cnt]) {
Ingo Molnard3be9152006-03-23 03:00:29 -08001950 mutex_lock(&dqopt->dqonoff_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951 /* If quota was reenabled in the meantime, we have
1952 * nothing to do */
Jan Karaf55abc02008-08-20 17:50:32 +02001953 if (!sb_has_quota_loaded(sb, cnt)) {
Jan Kara268157b2009-01-27 15:47:22 +01001954 mutex_lock_nested(&toputinode[cnt]->i_mutex,
1955 I_MUTEX_QUOTA);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956 toputinode[cnt]->i_flags &= ~(S_IMMUTABLE |
1957 S_NOATIME | S_NOQUOTA);
Jan Kara268157b2009-01-27 15:47:22 +01001958 truncate_inode_pages(&toputinode[cnt]->i_data,
1959 0);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001960 mutex_unlock(&toputinode[cnt]->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961 mark_inode_dirty(toputinode[cnt]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962 }
Ingo Molnard3be9152006-03-23 03:00:29 -08001963 mutex_unlock(&dqopt->dqonoff_mutex);
Jan Karaca785ec2008-09-30 17:53:37 +02001964 }
1965 if (sb->s_bdev)
1966 invalidate_bdev(sb->s_bdev);
1967put_inodes:
1968 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1969 if (toputinode[cnt]) {
Jan Kara0ff5af82008-04-28 02:14:33 -07001970 /* On remount RO, we keep the inode pointer so that we
Jan Karaf55abc02008-08-20 17:50:32 +02001971 * can reenable quota on the subsequent remount RW. We
1972 * have to check 'flags' variable and not use sb_has_
1973 * function because another quotaon / quotaoff could
1974 * change global state before we got here. We refuse
1975 * to suspend quotas when there is pending delete on
1976 * the quota file... */
1977 if (!(flags & DQUOT_SUSPENDED))
Jan Kara0ff5af82008-04-28 02:14:33 -07001978 iput(toputinode[cnt]);
1979 else if (!toputinode[cnt]->i_nlink)
1980 ret = -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981 }
Jan Kara0ff5af82008-04-28 02:14:33 -07001982 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983}
Mingming Cao08d03502009-01-14 16:19:32 +01001984EXPORT_SYMBOL(vfs_quota_disable);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985
Jan Karaf55abc02008-08-20 17:50:32 +02001986int vfs_quota_off(struct super_block *sb, int type, int remount)
1987{
1988 return vfs_quota_disable(sb, type, remount ? DQUOT_SUSPENDED :
1989 (DQUOT_USAGE_ENABLED | DQUOT_LIMITS_ENABLED));
1990}
Mingming Cao08d03502009-01-14 16:19:32 +01001991EXPORT_SYMBOL(vfs_quota_off);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992/*
1993 * Turn quotas on on a device
1994 */
1995
Jan Karaf55abc02008-08-20 17:50:32 +02001996/*
1997 * Helper function to turn quotas on when we already have the inode of
1998 * quota file and no quota information is loaded.
1999 */
2000static int vfs_load_quota_inode(struct inode *inode, int type, int format_id,
2001 unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002{
2003 struct quota_format_type *fmt = find_quota_format(format_id);
2004 struct super_block *sb = inode->i_sb;
2005 struct quota_info *dqopt = sb_dqopt(sb);
2006 int error;
2007 int oldflags = -1;
2008
2009 if (!fmt)
2010 return -ESRCH;
2011 if (!S_ISREG(inode->i_mode)) {
2012 error = -EACCES;
2013 goto out_fmt;
2014 }
2015 if (IS_RDONLY(inode)) {
2016 error = -EROFS;
2017 goto out_fmt;
2018 }
2019 if (!sb->s_op->quota_write || !sb->s_op->quota_read) {
2020 error = -EINVAL;
2021 goto out_fmt;
2022 }
Jan Karaf55abc02008-08-20 17:50:32 +02002023 /* Usage always has to be set... */
2024 if (!(flags & DQUOT_USAGE_ENABLED)) {
2025 error = -EINVAL;
2026 goto out_fmt;
2027 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028
Jan Karaca785ec2008-09-30 17:53:37 +02002029 if (!(dqopt->flags & DQUOT_QUOTA_SYS_FILE)) {
Jan Karaab94c392010-02-22 21:07:17 +01002030 /* As we bypass the pagecache we must now flush all the
2031 * dirty data and invalidate caches so that kernel sees
2032 * changes from userspace. It is not enough to just flush
2033 * the quota file since if blocksize < pagesize, invalidation
2034 * of the cache could fail because of other unrelated dirty
2035 * data */
2036 sync_filesystem(sb);
Jan Karaca785ec2008-09-30 17:53:37 +02002037 invalidate_bdev(sb->s_bdev);
2038 }
Ingo Molnard3be9152006-03-23 03:00:29 -08002039 mutex_lock(&dqopt->dqonoff_mutex);
Jan Karaf55abc02008-08-20 17:50:32 +02002040 if (sb_has_quota_loaded(sb, type)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041 error = -EBUSY;
2042 goto out_lock;
2043 }
Jan Karaca785ec2008-09-30 17:53:37 +02002044
2045 if (!(dqopt->flags & DQUOT_QUOTA_SYS_FILE)) {
2046 /* We don't want quota and atime on quota files (deadlocks
2047 * possible) Also nobody should write to the file - we use
2048 * special IO operations which ignore the immutable bit. */
Jan Karadee86562009-07-22 18:12:17 +02002049 mutex_lock_nested(&inode->i_mutex, I_MUTEX_QUOTA);
Jan Kara268157b2009-01-27 15:47:22 +01002050 oldflags = inode->i_flags & (S_NOATIME | S_IMMUTABLE |
2051 S_NOQUOTA);
Jan Karaca785ec2008-09-30 17:53:37 +02002052 inode->i_flags |= S_NOQUOTA | S_NOATIME | S_IMMUTABLE;
Jan Karadee86562009-07-22 18:12:17 +02002053 mutex_unlock(&inode->i_mutex);
Jan Kara26245c92010-01-06 17:20:35 +01002054 /*
2055 * When S_NOQUOTA is set, remove dquot references as no more
2056 * references can be added
2057 */
Christoph Hellwig9f754752010-03-03 09:05:05 -05002058 __dquot_drop(inode);
Jan Karaca785ec2008-09-30 17:53:37 +02002059 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060
2061 error = -EIO;
2062 dqopt->files[type] = igrab(inode);
2063 if (!dqopt->files[type])
2064 goto out_lock;
2065 error = -EINVAL;
2066 if (!fmt->qf_ops->check_quota_file(sb, type))
2067 goto out_file_init;
2068
2069 dqopt->ops[type] = fmt->qf_ops;
2070 dqopt->info[type].dqi_format = fmt;
Jan Kara0ff5af82008-04-28 02:14:33 -07002071 dqopt->info[type].dqi_fmt_id = format_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072 INIT_LIST_HEAD(&dqopt->info[type].dqi_dirty_list);
Ingo Molnard3be9152006-03-23 03:00:29 -08002073 mutex_lock(&dqopt->dqio_mutex);
Jan Kara268157b2009-01-27 15:47:22 +01002074 error = dqopt->ops[type]->read_file_info(sb, type);
2075 if (error < 0) {
Ingo Molnard3be9152006-03-23 03:00:29 -08002076 mutex_unlock(&dqopt->dqio_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077 goto out_file_init;
2078 }
Ingo Molnard3be9152006-03-23 03:00:29 -08002079 mutex_unlock(&dqopt->dqio_mutex);
Jan Karacc334122009-01-12 17:23:05 +01002080 spin_lock(&dq_state_lock);
Jan Karaf55abc02008-08-20 17:50:32 +02002081 dqopt->flags |= dquot_state_flag(flags, type);
Jan Karacc334122009-01-12 17:23:05 +01002082 spin_unlock(&dq_state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083
2084 add_dquot_ref(sb, type);
Ingo Molnard3be9152006-03-23 03:00:29 -08002085 mutex_unlock(&dqopt->dqonoff_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086
2087 return 0;
2088
2089out_file_init:
2090 dqopt->files[type] = NULL;
2091 iput(inode);
2092out_lock:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093 if (oldflags != -1) {
Jan Karadee86562009-07-22 18:12:17 +02002094 mutex_lock_nested(&inode->i_mutex, I_MUTEX_QUOTA);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095 /* Set the flags back (in the case of accidental quotaon()
2096 * on a wrong file we don't want to mess up the flags) */
2097 inode->i_flags &= ~(S_NOATIME | S_NOQUOTA | S_IMMUTABLE);
2098 inode->i_flags |= oldflags;
Jan Karadee86562009-07-22 18:12:17 +02002099 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100 }
Jiaying Zhangd01730d2009-07-07 18:15:21 +02002101 mutex_unlock(&dqopt->dqonoff_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102out_fmt:
2103 put_quota_format(fmt);
2104
2105 return error;
2106}
2107
Jan Kara0ff5af82008-04-28 02:14:33 -07002108/* Reenable quotas on remount RW */
2109static int vfs_quota_on_remount(struct super_block *sb, int type)
2110{
2111 struct quota_info *dqopt = sb_dqopt(sb);
2112 struct inode *inode;
2113 int ret;
Jan Karaf55abc02008-08-20 17:50:32 +02002114 unsigned int flags;
Jan Kara0ff5af82008-04-28 02:14:33 -07002115
2116 mutex_lock(&dqopt->dqonoff_mutex);
2117 if (!sb_has_quota_suspended(sb, type)) {
2118 mutex_unlock(&dqopt->dqonoff_mutex);
2119 return 0;
2120 }
Jan Kara0ff5af82008-04-28 02:14:33 -07002121 inode = dqopt->files[type];
2122 dqopt->files[type] = NULL;
Jan Karacc334122009-01-12 17:23:05 +01002123 spin_lock(&dq_state_lock);
Jan Karaf55abc02008-08-20 17:50:32 +02002124 flags = dqopt->flags & dquot_state_flag(DQUOT_USAGE_ENABLED |
2125 DQUOT_LIMITS_ENABLED, type);
2126 dqopt->flags &= ~dquot_state_flag(DQUOT_STATE_FLAGS, type);
Jan Karacc334122009-01-12 17:23:05 +01002127 spin_unlock(&dq_state_lock);
Jan Kara0ff5af82008-04-28 02:14:33 -07002128 mutex_unlock(&dqopt->dqonoff_mutex);
2129
Jan Karaf55abc02008-08-20 17:50:32 +02002130 flags = dquot_generic_flag(flags, type);
2131 ret = vfs_load_quota_inode(inode, type, dqopt->info[type].dqi_fmt_id,
2132 flags);
Jan Kara0ff5af82008-04-28 02:14:33 -07002133 iput(inode);
2134
2135 return ret;
2136}
2137
Al Viro77e69da2008-08-01 04:29:18 -04002138int vfs_quota_on_path(struct super_block *sb, int type, int format_id,
2139 struct path *path)
2140{
2141 int error = security_quota_on(path->dentry);
2142 if (error)
2143 return error;
2144 /* Quota file not on the same filesystem? */
2145 if (path->mnt->mnt_sb != sb)
2146 error = -EXDEV;
2147 else
Jan Karaf55abc02008-08-20 17:50:32 +02002148 error = vfs_load_quota_inode(path->dentry->d_inode, type,
2149 format_id, DQUOT_USAGE_ENABLED |
2150 DQUOT_LIMITS_ENABLED);
Al Viro77e69da2008-08-01 04:29:18 -04002151 return error;
2152}
Mingming Cao08d03502009-01-14 16:19:32 +01002153EXPORT_SYMBOL(vfs_quota_on_path);
Al Viro77e69da2008-08-01 04:29:18 -04002154
Al Viro82646132008-08-02 00:57:06 -04002155int vfs_quota_on(struct super_block *sb, int type, int format_id, char *name,
Jan Kara0ff5af82008-04-28 02:14:33 -07002156 int remount)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157{
Al Viro82646132008-08-02 00:57:06 -04002158 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159 int error;
2160
Jan Kara0ff5af82008-04-28 02:14:33 -07002161 if (remount)
2162 return vfs_quota_on_remount(sb, type);
2163
Al Viro82646132008-08-02 00:57:06 -04002164 error = kern_path(name, LOOKUP_FOLLOW, &path);
Al Viro77e69da2008-08-01 04:29:18 -04002165 if (!error) {
Al Viro82646132008-08-02 00:57:06 -04002166 error = vfs_quota_on_path(sb, type, format_id, &path);
2167 path_put(&path);
Al Viro77e69da2008-08-01 04:29:18 -04002168 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169 return error;
2170}
Mingming Cao08d03502009-01-14 16:19:32 +01002171EXPORT_SYMBOL(vfs_quota_on);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172
2173/*
Jan Karaf55abc02008-08-20 17:50:32 +02002174 * More powerful function for turning on quotas allowing setting
2175 * of individual quota flags
2176 */
2177int vfs_quota_enable(struct inode *inode, int type, int format_id,
2178 unsigned int flags)
2179{
2180 int ret = 0;
2181 struct super_block *sb = inode->i_sb;
2182 struct quota_info *dqopt = sb_dqopt(sb);
2183
2184 /* Just unsuspend quotas? */
2185 if (flags & DQUOT_SUSPENDED)
2186 return vfs_quota_on_remount(sb, type);
2187 if (!flags)
2188 return 0;
2189 /* Just updating flags needed? */
2190 if (sb_has_quota_loaded(sb, type)) {
2191 mutex_lock(&dqopt->dqonoff_mutex);
2192 /* Now do a reliable test... */
2193 if (!sb_has_quota_loaded(sb, type)) {
2194 mutex_unlock(&dqopt->dqonoff_mutex);
2195 goto load_quota;
2196 }
2197 if (flags & DQUOT_USAGE_ENABLED &&
2198 sb_has_quota_usage_enabled(sb, type)) {
2199 ret = -EBUSY;
2200 goto out_lock;
2201 }
2202 if (flags & DQUOT_LIMITS_ENABLED &&
2203 sb_has_quota_limits_enabled(sb, type)) {
2204 ret = -EBUSY;
2205 goto out_lock;
2206 }
Jan Karacc334122009-01-12 17:23:05 +01002207 spin_lock(&dq_state_lock);
Jan Karaf55abc02008-08-20 17:50:32 +02002208 sb_dqopt(sb)->flags |= dquot_state_flag(flags, type);
Jan Karacc334122009-01-12 17:23:05 +01002209 spin_unlock(&dq_state_lock);
Jan Karaf55abc02008-08-20 17:50:32 +02002210out_lock:
2211 mutex_unlock(&dqopt->dqonoff_mutex);
2212 return ret;
2213 }
2214
2215load_quota:
2216 return vfs_load_quota_inode(inode, type, format_id, flags);
2217}
Mingming Cao08d03502009-01-14 16:19:32 +01002218EXPORT_SYMBOL(vfs_quota_enable);
Jan Karaf55abc02008-08-20 17:50:32 +02002219
2220/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221 * This function is used when filesystem needs to initialize quotas
2222 * during mount time.
2223 */
Christoph Hellwig84de8562005-06-23 00:09:16 -07002224int vfs_quota_on_mount(struct super_block *sb, char *qf_name,
2225 int format_id, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226{
Christoph Hellwig84de8562005-06-23 00:09:16 -07002227 struct dentry *dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228 int error;
2229
Jan Karac56818d2009-11-12 15:42:08 +01002230 mutex_lock(&sb->s_root->d_inode->i_mutex);
Christoph Hellwig2fa389c2005-06-23 00:09:16 -07002231 dentry = lookup_one_len(qf_name, sb->s_root, strlen(qf_name));
Jan Karac56818d2009-11-12 15:42:08 +01002232 mutex_unlock(&sb->s_root->d_inode->i_mutex);
Christoph Hellwig84de8562005-06-23 00:09:16 -07002233 if (IS_ERR(dentry))
2234 return PTR_ERR(dentry);
2235
Jan Kara154f4842005-11-28 13:44:14 -08002236 if (!dentry->d_inode) {
2237 error = -ENOENT;
2238 goto out;
2239 }
2240
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241 error = security_quota_on(dentry);
Christoph Hellwig84de8562005-06-23 00:09:16 -07002242 if (!error)
Jan Karaf55abc02008-08-20 17:50:32 +02002243 error = vfs_load_quota_inode(dentry->d_inode, type, format_id,
2244 DQUOT_USAGE_ENABLED | DQUOT_LIMITS_ENABLED);
Christoph Hellwig84de8562005-06-23 00:09:16 -07002245
Jan Kara154f4842005-11-28 13:44:14 -08002246out:
Christoph Hellwig84de8562005-06-23 00:09:16 -07002247 dput(dentry);
2248 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249}
Mingming Cao08d03502009-01-14 16:19:32 +01002250EXPORT_SYMBOL(vfs_quota_on_mount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251
Jan Karab85f4b82008-07-25 01:46:50 -07002252/* Wrapper to turn on quotas when remounting rw */
2253int vfs_dq_quota_on_remount(struct super_block *sb)
2254{
2255 int cnt;
2256 int ret = 0, err;
2257
2258 if (!sb->s_qcop || !sb->s_qcop->quota_on)
2259 return -ENOSYS;
2260 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
2261 err = sb->s_qcop->quota_on(sb, cnt, 0, NULL, 1);
2262 if (err < 0 && !ret)
2263 ret = err;
2264 }
2265 return ret;
2266}
Mingming Cao08d03502009-01-14 16:19:32 +01002267EXPORT_SYMBOL(vfs_dq_quota_on_remount);
Jan Karab85f4b82008-07-25 01:46:50 -07002268
Jan Kara12095462008-08-20 14:45:12 +02002269static inline qsize_t qbtos(qsize_t blocks)
2270{
2271 return blocks << QIF_DQBLKSIZE_BITS;
2272}
2273
2274static inline qsize_t stoqb(qsize_t space)
2275{
2276 return (space + QIF_DQBLKSIZE - 1) >> QIF_DQBLKSIZE_BITS;
2277}
2278
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279/* Generic routine for getting common part of quota structure */
2280static void do_get_dqblk(struct dquot *dquot, struct if_dqblk *di)
2281{
2282 struct mem_dqblk *dm = &dquot->dq_dqb;
2283
2284 spin_lock(&dq_data_lock);
Jan Kara12095462008-08-20 14:45:12 +02002285 di->dqb_bhardlimit = stoqb(dm->dqb_bhardlimit);
2286 di->dqb_bsoftlimit = stoqb(dm->dqb_bsoftlimit);
Mingming Caof18df222009-01-13 16:43:09 +01002287 di->dqb_curspace = dm->dqb_curspace + dm->dqb_rsvspace;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288 di->dqb_ihardlimit = dm->dqb_ihardlimit;
2289 di->dqb_isoftlimit = dm->dqb_isoftlimit;
2290 di->dqb_curinodes = dm->dqb_curinodes;
2291 di->dqb_btime = dm->dqb_btime;
2292 di->dqb_itime = dm->dqb_itime;
2293 di->dqb_valid = QIF_ALL;
2294 spin_unlock(&dq_data_lock);
2295}
2296
Jan Kara268157b2009-01-27 15:47:22 +01002297int vfs_get_dqblk(struct super_block *sb, int type, qid_t id,
2298 struct if_dqblk *di)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299{
2300 struct dquot *dquot;
2301
Jan Karacc334122009-01-12 17:23:05 +01002302 dquot = dqget(sb, id, type);
Jan Karadd6f3c62009-01-26 16:01:43 +01002303 if (!dquot)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304 return -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305 do_get_dqblk(dquot, di);
2306 dqput(dquot);
Jan Karacc334122009-01-12 17:23:05 +01002307
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308 return 0;
2309}
Mingming Cao08d03502009-01-14 16:19:32 +01002310EXPORT_SYMBOL(vfs_get_dqblk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311
2312/* Generic routine for setting common part of quota structure */
Andrew Perepechko338bf9a2008-04-28 02:14:31 -07002313static int do_set_dqblk(struct dquot *dquot, struct if_dqblk *di)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314{
2315 struct mem_dqblk *dm = &dquot->dq_dqb;
2316 int check_blim = 0, check_ilim = 0;
Andrew Perepechko338bf9a2008-04-28 02:14:31 -07002317 struct mem_dqinfo *dqi = &sb_dqopt(dquot->dq_sb)->info[dquot->dq_type];
2318
2319 if ((di->dqb_valid & QIF_BLIMITS &&
2320 (di->dqb_bhardlimit > dqi->dqi_maxblimit ||
2321 di->dqb_bsoftlimit > dqi->dqi_maxblimit)) ||
2322 (di->dqb_valid & QIF_ILIMITS &&
2323 (di->dqb_ihardlimit > dqi->dqi_maxilimit ||
2324 di->dqb_isoftlimit > dqi->dqi_maxilimit)))
2325 return -ERANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002326
2327 spin_lock(&dq_data_lock);
2328 if (di->dqb_valid & QIF_SPACE) {
Mingming Caof18df222009-01-13 16:43:09 +01002329 dm->dqb_curspace = di->dqb_curspace - dm->dqb_rsvspace;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330 check_blim = 1;
Andrew Perepechko08261672010-04-12 22:16:50 +04002331 set_bit(DQ_LASTSET_B + QIF_SPACE_B, &dquot->dq_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002332 }
2333 if (di->dqb_valid & QIF_BLIMITS) {
Jan Kara12095462008-08-20 14:45:12 +02002334 dm->dqb_bsoftlimit = qbtos(di->dqb_bsoftlimit);
2335 dm->dqb_bhardlimit = qbtos(di->dqb_bhardlimit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336 check_blim = 1;
Andrew Perepechko08261672010-04-12 22:16:50 +04002337 set_bit(DQ_LASTSET_B + QIF_BLIMITS_B, &dquot->dq_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338 }
2339 if (di->dqb_valid & QIF_INODES) {
2340 dm->dqb_curinodes = di->dqb_curinodes;
2341 check_ilim = 1;
Andrew Perepechko08261672010-04-12 22:16:50 +04002342 set_bit(DQ_LASTSET_B + QIF_INODES_B, &dquot->dq_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343 }
2344 if (di->dqb_valid & QIF_ILIMITS) {
2345 dm->dqb_isoftlimit = di->dqb_isoftlimit;
2346 dm->dqb_ihardlimit = di->dqb_ihardlimit;
2347 check_ilim = 1;
Andrew Perepechko08261672010-04-12 22:16:50 +04002348 set_bit(DQ_LASTSET_B + QIF_ILIMITS_B, &dquot->dq_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349 }
Jan Kara4d59bce2008-10-02 16:48:10 +02002350 if (di->dqb_valid & QIF_BTIME) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351 dm->dqb_btime = di->dqb_btime;
Jan Karae04a88a92009-01-07 18:07:29 -08002352 check_blim = 1;
Andrew Perepechko08261672010-04-12 22:16:50 +04002353 set_bit(DQ_LASTSET_B + QIF_BTIME_B, &dquot->dq_flags);
Jan Kara4d59bce2008-10-02 16:48:10 +02002354 }
2355 if (di->dqb_valid & QIF_ITIME) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002356 dm->dqb_itime = di->dqb_itime;
Jan Karae04a88a92009-01-07 18:07:29 -08002357 check_ilim = 1;
Andrew Perepechko08261672010-04-12 22:16:50 +04002358 set_bit(DQ_LASTSET_B + QIF_ITIME_B, &dquot->dq_flags);
Jan Kara4d59bce2008-10-02 16:48:10 +02002359 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360
2361 if (check_blim) {
Jan Kara268157b2009-01-27 15:47:22 +01002362 if (!dm->dqb_bsoftlimit ||
2363 dm->dqb_curspace < dm->dqb_bsoftlimit) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364 dm->dqb_btime = 0;
2365 clear_bit(DQ_BLKS_B, &dquot->dq_flags);
Jan Kara268157b2009-01-27 15:47:22 +01002366 } else if (!(di->dqb_valid & QIF_BTIME))
2367 /* Set grace only if user hasn't provided his own... */
Andrew Perepechko338bf9a2008-04-28 02:14:31 -07002368 dm->dqb_btime = get_seconds() + dqi->dqi_bgrace;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369 }
2370 if (check_ilim) {
Jan Kara268157b2009-01-27 15:47:22 +01002371 if (!dm->dqb_isoftlimit ||
2372 dm->dqb_curinodes < dm->dqb_isoftlimit) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373 dm->dqb_itime = 0;
2374 clear_bit(DQ_INODES_B, &dquot->dq_flags);
Jan Kara268157b2009-01-27 15:47:22 +01002375 } else if (!(di->dqb_valid & QIF_ITIME))
2376 /* Set grace only if user hasn't provided his own... */
Andrew Perepechko338bf9a2008-04-28 02:14:31 -07002377 dm->dqb_itime = get_seconds() + dqi->dqi_igrace;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002378 }
Jan Kara268157b2009-01-27 15:47:22 +01002379 if (dm->dqb_bhardlimit || dm->dqb_bsoftlimit || dm->dqb_ihardlimit ||
2380 dm->dqb_isoftlimit)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381 clear_bit(DQ_FAKE_B, &dquot->dq_flags);
2382 else
2383 set_bit(DQ_FAKE_B, &dquot->dq_flags);
2384 spin_unlock(&dq_data_lock);
2385 mark_dquot_dirty(dquot);
Andrew Perepechko338bf9a2008-04-28 02:14:31 -07002386
2387 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002388}
2389
Jan Kara268157b2009-01-27 15:47:22 +01002390int vfs_set_dqblk(struct super_block *sb, int type, qid_t id,
2391 struct if_dqblk *di)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392{
2393 struct dquot *dquot;
Andrew Perepechko338bf9a2008-04-28 02:14:31 -07002394 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002395
Jan Karaf55abc02008-08-20 17:50:32 +02002396 dquot = dqget(sb, id, type);
2397 if (!dquot) {
2398 rc = -ESRCH;
2399 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002400 }
Andrew Perepechko338bf9a2008-04-28 02:14:31 -07002401 rc = do_set_dqblk(dquot, di);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002402 dqput(dquot);
Jan Karaf55abc02008-08-20 17:50:32 +02002403out:
Andrew Perepechko338bf9a2008-04-28 02:14:31 -07002404 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002405}
Mingming Cao08d03502009-01-14 16:19:32 +01002406EXPORT_SYMBOL(vfs_set_dqblk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002407
2408/* Generic routine for getting common part of quota file information */
2409int vfs_get_dqinfo(struct super_block *sb, int type, struct if_dqinfo *ii)
2410{
2411 struct mem_dqinfo *mi;
2412
Ingo Molnard3be9152006-03-23 03:00:29 -08002413 mutex_lock(&sb_dqopt(sb)->dqonoff_mutex);
Jan Karaf55abc02008-08-20 17:50:32 +02002414 if (!sb_has_quota_active(sb, type)) {
Ingo Molnard3be9152006-03-23 03:00:29 -08002415 mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002416 return -ESRCH;
2417 }
2418 mi = sb_dqopt(sb)->info + type;
2419 spin_lock(&dq_data_lock);
2420 ii->dqi_bgrace = mi->dqi_bgrace;
2421 ii->dqi_igrace = mi->dqi_igrace;
2422 ii->dqi_flags = mi->dqi_flags & DQF_MASK;
2423 ii->dqi_valid = IIF_ALL;
2424 spin_unlock(&dq_data_lock);
Ingo Molnard3be9152006-03-23 03:00:29 -08002425 mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002426 return 0;
2427}
Mingming Cao08d03502009-01-14 16:19:32 +01002428EXPORT_SYMBOL(vfs_get_dqinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002429
2430/* Generic routine for setting common part of quota file information */
2431int vfs_set_dqinfo(struct super_block *sb, int type, struct if_dqinfo *ii)
2432{
2433 struct mem_dqinfo *mi;
Jan Karaf55abc02008-08-20 17:50:32 +02002434 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002435
Ingo Molnard3be9152006-03-23 03:00:29 -08002436 mutex_lock(&sb_dqopt(sb)->dqonoff_mutex);
Jan Karaf55abc02008-08-20 17:50:32 +02002437 if (!sb_has_quota_active(sb, type)) {
2438 err = -ESRCH;
2439 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002440 }
2441 mi = sb_dqopt(sb)->info + type;
2442 spin_lock(&dq_data_lock);
2443 if (ii->dqi_valid & IIF_BGRACE)
2444 mi->dqi_bgrace = ii->dqi_bgrace;
2445 if (ii->dqi_valid & IIF_IGRACE)
2446 mi->dqi_igrace = ii->dqi_igrace;
2447 if (ii->dqi_valid & IIF_FLAGS)
Jan Kara268157b2009-01-27 15:47:22 +01002448 mi->dqi_flags = (mi->dqi_flags & ~DQF_MASK) |
2449 (ii->dqi_flags & DQF_MASK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450 spin_unlock(&dq_data_lock);
2451 mark_info_dirty(sb, type);
2452 /* Force write to disk */
2453 sb->dq_op->write_info(sb, type);
Jan Karaf55abc02008-08-20 17:50:32 +02002454out:
Ingo Molnard3be9152006-03-23 03:00:29 -08002455 mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
Jan Karaf55abc02008-08-20 17:50:32 +02002456 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002457}
Mingming Cao08d03502009-01-14 16:19:32 +01002458EXPORT_SYMBOL(vfs_set_dqinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459
Alexey Dobriyan0d54b212009-09-21 17:01:09 -07002460const struct quotactl_ops vfs_quotactl_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002461 .quota_on = vfs_quota_on,
2462 .quota_off = vfs_quota_off,
2463 .quota_sync = vfs_quota_sync,
2464 .get_info = vfs_get_dqinfo,
2465 .set_info = vfs_set_dqinfo,
2466 .get_dqblk = vfs_get_dqblk,
2467 .set_dqblk = vfs_set_dqblk
2468};
2469
2470static ctl_table fs_dqstats_table[] = {
2471 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002472 .procname = "lookups",
2473 .data = &dqstats.lookups,
2474 .maxlen = sizeof(int),
2475 .mode = 0444,
Eric W. Biederman6d456112009-11-16 03:11:48 -08002476 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002477 },
2478 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002479 .procname = "drops",
2480 .data = &dqstats.drops,
2481 .maxlen = sizeof(int),
2482 .mode = 0444,
Eric W. Biederman6d456112009-11-16 03:11:48 -08002483 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002484 },
2485 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002486 .procname = "reads",
2487 .data = &dqstats.reads,
2488 .maxlen = sizeof(int),
2489 .mode = 0444,
Eric W. Biederman6d456112009-11-16 03:11:48 -08002490 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002491 },
2492 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002493 .procname = "writes",
2494 .data = &dqstats.writes,
2495 .maxlen = sizeof(int),
2496 .mode = 0444,
Eric W. Biederman6d456112009-11-16 03:11:48 -08002497 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002498 },
2499 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002500 .procname = "cache_hits",
2501 .data = &dqstats.cache_hits,
2502 .maxlen = sizeof(int),
2503 .mode = 0444,
Eric W. Biederman6d456112009-11-16 03:11:48 -08002504 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002505 },
2506 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002507 .procname = "allocated_dquots",
2508 .data = &dqstats.allocated_dquots,
2509 .maxlen = sizeof(int),
2510 .mode = 0444,
Eric W. Biederman6d456112009-11-16 03:11:48 -08002511 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002512 },
2513 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002514 .procname = "free_dquots",
2515 .data = &dqstats.free_dquots,
2516 .maxlen = sizeof(int),
2517 .mode = 0444,
Eric W. Biederman6d456112009-11-16 03:11:48 -08002518 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002519 },
2520 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002521 .procname = "syncs",
2522 .data = &dqstats.syncs,
2523 .maxlen = sizeof(int),
2524 .mode = 0444,
Eric W. Biederman6d456112009-11-16 03:11:48 -08002525 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002526 },
Jan Kara8e893462007-10-16 23:29:31 -07002527#ifdef CONFIG_PRINT_QUOTA_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07002528 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002529 .procname = "warnings",
2530 .data = &flag_print_warnings,
2531 .maxlen = sizeof(int),
2532 .mode = 0644,
Eric W. Biederman6d456112009-11-16 03:11:48 -08002533 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002534 },
Jan Kara8e893462007-10-16 23:29:31 -07002535#endif
Eric W. Biedermanab092032009-11-05 14:25:10 -08002536 { },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002537};
2538
2539static ctl_table fs_table[] = {
2540 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002541 .procname = "quota",
2542 .mode = 0555,
2543 .child = fs_dqstats_table,
2544 },
Eric W. Biedermanab092032009-11-05 14:25:10 -08002545 { },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002546};
2547
2548static ctl_table sys_table[] = {
2549 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002550 .procname = "fs",
2551 .mode = 0555,
2552 .child = fs_table,
2553 },
Eric W. Biedermanab092032009-11-05 14:25:10 -08002554 { },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002555};
2556
2557static int __init dquot_init(void)
2558{
2559 int i;
2560 unsigned long nr_hash, order;
2561
2562 printk(KERN_NOTICE "VFS: Disk quotas %s\n", __DQUOT_VERSION__);
2563
Eric W. Biederman0b4d4142007-02-14 00:34:09 -08002564 register_sysctl_table(sys_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002565
Paul Mundt20c2df82007-07-20 10:11:58 +09002566 dquot_cachep = kmem_cache_create("dquot",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002567 sizeof(struct dquot), sizeof(unsigned long) * 4,
Paul Jacksonfffb60f2006-03-24 03:16:06 -08002568 (SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT|
2569 SLAB_MEM_SPREAD|SLAB_PANIC),
Paul Mundt20c2df82007-07-20 10:11:58 +09002570 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002571
2572 order = 0;
2573 dquot_hash = (struct hlist_head *)__get_free_pages(GFP_ATOMIC, order);
2574 if (!dquot_hash)
2575 panic("Cannot create dquot hash table");
2576
2577 /* Find power-of-two hlist_heads which can fit into allocation */
2578 nr_hash = (1UL << order) * PAGE_SIZE / sizeof(struct hlist_head);
2579 dq_hash_bits = 0;
2580 do {
2581 dq_hash_bits++;
2582 } while (nr_hash >> dq_hash_bits);
2583 dq_hash_bits--;
2584
2585 nr_hash = 1UL << dq_hash_bits;
2586 dq_hash_mask = nr_hash - 1;
2587 for (i = 0; i < nr_hash; i++)
2588 INIT_HLIST_HEAD(dquot_hash + i);
2589
2590 printk("Dquot-cache hash table entries: %ld (order %ld, %ld bytes)\n",
2591 nr_hash, order, (PAGE_SIZE << order));
2592
Rusty Russell8e1f9362007-07-17 04:03:17 -07002593 register_shrinker(&dqcache_shrinker);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002594
Linus Torvalds1da177e2005-04-16 15:20:36 -07002595 return 0;
2596}
2597module_init(dquot_init);