blob: 6c849de5dc8f9b36d59faabbdcb53ce02466e4ab [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);
233
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234static inline unsigned int
235hashfn(const struct super_block *sb, unsigned int id, int type)
236{
237 unsigned long tmp;
238
239 tmp = (((unsigned long)sb>>L1_CACHE_SHIFT) ^ id) * (MAXQUOTAS - type);
240 return (tmp + (tmp >> dq_hash_bits)) & dq_hash_mask;
241}
242
243/*
244 * Following list functions expect dq_list_lock to be held
245 */
246static inline void insert_dquot_hash(struct dquot *dquot)
247{
Jan Kara268157b2009-01-27 15:47:22 +0100248 struct hlist_head *head;
249 head = dquot_hash + hashfn(dquot->dq_sb, dquot->dq_id, dquot->dq_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 hlist_add_head(&dquot->dq_hash, head);
251}
252
253static inline void remove_dquot_hash(struct dquot *dquot)
254{
255 hlist_del_init(&dquot->dq_hash);
256}
257
Jan Kara7a2435d2009-01-27 01:47:11 +0100258static struct dquot *find_dquot(unsigned int hashent, struct super_block *sb,
259 unsigned int id, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260{
261 struct hlist_node *node;
262 struct dquot *dquot;
263
264 hlist_for_each (node, dquot_hash+hashent) {
265 dquot = hlist_entry(node, struct dquot, dq_hash);
Jan Kara268157b2009-01-27 15:47:22 +0100266 if (dquot->dq_sb == sb && dquot->dq_id == id &&
267 dquot->dq_type == type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 return dquot;
269 }
Jan Karadd6f3c62009-01-26 16:01:43 +0100270 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271}
272
273/* Add a dquot to the tail of the free list */
274static inline void put_dquot_last(struct dquot *dquot)
275{
Akinobu Mita8e130592006-06-26 00:24:37 -0700276 list_add_tail(&dquot->dq_free, &free_dquots);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 dqstats.free_dquots++;
278}
279
280static inline void remove_free_dquot(struct dquot *dquot)
281{
282 if (list_empty(&dquot->dq_free))
283 return;
284 list_del_init(&dquot->dq_free);
285 dqstats.free_dquots--;
286}
287
288static inline void put_inuse(struct dquot *dquot)
289{
290 /* We add to the back of inuse list so we don't have to restart
291 * when traversing this list and we block */
Akinobu Mita8e130592006-06-26 00:24:37 -0700292 list_add_tail(&dquot->dq_inuse, &inuse_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 dqstats.allocated_dquots++;
294}
295
296static inline void remove_inuse(struct dquot *dquot)
297{
298 dqstats.allocated_dquots--;
299 list_del(&dquot->dq_inuse);
300}
301/*
302 * End of list functions needing dq_list_lock
303 */
304
305static void wait_on_dquot(struct dquot *dquot)
306{
Ingo Molnard3be9152006-03-23 03:00:29 -0800307 mutex_lock(&dquot->dq_lock);
308 mutex_unlock(&dquot->dq_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309}
310
Jan Kara03f6e922008-04-28 02:14:32 -0700311static inline int dquot_dirty(struct dquot *dquot)
312{
313 return test_bit(DQ_MOD_B, &dquot->dq_flags);
314}
315
316static inline int mark_dquot_dirty(struct dquot *dquot)
317{
318 return dquot->dq_sb->dq_op->mark_dirty(dquot);
319}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320
321int dquot_mark_dquot_dirty(struct dquot *dquot)
322{
323 spin_lock(&dq_list_lock);
324 if (!test_and_set_bit(DQ_MOD_B, &dquot->dq_flags))
325 list_add(&dquot->dq_dirty, &sb_dqopt(dquot->dq_sb)->
326 info[dquot->dq_type].dqi_dirty_list);
327 spin_unlock(&dq_list_lock);
328 return 0;
329}
Mingming Cao08d03502009-01-14 16:19:32 +0100330EXPORT_SYMBOL(dquot_mark_dquot_dirty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331
Dmitry Monakhovdc52dd32009-12-14 15:21:15 +0300332/* Dirtify all the dquots - this can block when journalling */
333static inline int mark_all_dquot_dirty(struct dquot * const *dquot)
334{
335 int ret, err, cnt;
336
337 ret = err = 0;
338 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
339 if (dquot[cnt])
340 /* Even in case of error we have to continue */
341 ret = mark_dquot_dirty(dquot[cnt]);
342 if (!err)
343 err = ret;
344 }
345 return err;
346}
347
348static inline void dqput_all(struct dquot **dquot)
349{
350 unsigned int cnt;
351
352 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
353 dqput(dquot[cnt]);
354}
355
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356/* This function needs dq_list_lock */
357static inline int clear_dquot_dirty(struct dquot *dquot)
358{
359 if (!test_and_clear_bit(DQ_MOD_B, &dquot->dq_flags))
360 return 0;
361 list_del_init(&dquot->dq_dirty);
362 return 1;
363}
364
365void mark_info_dirty(struct super_block *sb, int type)
366{
367 set_bit(DQF_INFO_DIRTY_B, &sb_dqopt(sb)->info[type].dqi_flags);
368}
369EXPORT_SYMBOL(mark_info_dirty);
370
371/*
372 * Read dquot from disk and alloc space for it
373 */
374
375int dquot_acquire(struct dquot *dquot)
376{
377 int ret = 0, ret2 = 0;
378 struct quota_info *dqopt = sb_dqopt(dquot->dq_sb);
379
Ingo Molnard3be9152006-03-23 03:00:29 -0800380 mutex_lock(&dquot->dq_lock);
381 mutex_lock(&dqopt->dqio_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 if (!test_bit(DQ_READ_B, &dquot->dq_flags))
383 ret = dqopt->ops[dquot->dq_type]->read_dqblk(dquot);
384 if (ret < 0)
385 goto out_iolock;
386 set_bit(DQ_READ_B, &dquot->dq_flags);
387 /* Instantiate dquot if needed */
388 if (!test_bit(DQ_ACTIVE_B, &dquot->dq_flags) && !dquot->dq_off) {
389 ret = dqopt->ops[dquot->dq_type]->commit_dqblk(dquot);
390 /* Write the info if needed */
Jan Kara268157b2009-01-27 15:47:22 +0100391 if (info_dirty(&dqopt->info[dquot->dq_type])) {
392 ret2 = dqopt->ops[dquot->dq_type]->write_file_info(
393 dquot->dq_sb, dquot->dq_type);
394 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 if (ret < 0)
396 goto out_iolock;
397 if (ret2 < 0) {
398 ret = ret2;
399 goto out_iolock;
400 }
401 }
402 set_bit(DQ_ACTIVE_B, &dquot->dq_flags);
403out_iolock:
Ingo Molnard3be9152006-03-23 03:00:29 -0800404 mutex_unlock(&dqopt->dqio_mutex);
405 mutex_unlock(&dquot->dq_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 return ret;
407}
Mingming Cao08d03502009-01-14 16:19:32 +0100408EXPORT_SYMBOL(dquot_acquire);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409
410/*
411 * Write dquot to disk
412 */
413int dquot_commit(struct dquot *dquot)
414{
415 int ret = 0, ret2 = 0;
416 struct quota_info *dqopt = sb_dqopt(dquot->dq_sb);
417
Ingo Molnard3be9152006-03-23 03:00:29 -0800418 mutex_lock(&dqopt->dqio_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 spin_lock(&dq_list_lock);
420 if (!clear_dquot_dirty(dquot)) {
421 spin_unlock(&dq_list_lock);
422 goto out_sem;
423 }
424 spin_unlock(&dq_list_lock);
425 /* Inactive dquot can be only if there was error during read/init
426 * => we have better not writing it */
427 if (test_bit(DQ_ACTIVE_B, &dquot->dq_flags)) {
428 ret = dqopt->ops[dquot->dq_type]->commit_dqblk(dquot);
Jan Kara268157b2009-01-27 15:47:22 +0100429 if (info_dirty(&dqopt->info[dquot->dq_type])) {
430 ret2 = dqopt->ops[dquot->dq_type]->write_file_info(
431 dquot->dq_sb, dquot->dq_type);
432 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 if (ret >= 0)
434 ret = ret2;
435 }
436out_sem:
Ingo Molnard3be9152006-03-23 03:00:29 -0800437 mutex_unlock(&dqopt->dqio_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 return ret;
439}
Mingming Cao08d03502009-01-14 16:19:32 +0100440EXPORT_SYMBOL(dquot_commit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441
442/*
443 * Release dquot
444 */
445int dquot_release(struct dquot *dquot)
446{
447 int ret = 0, ret2 = 0;
448 struct quota_info *dqopt = sb_dqopt(dquot->dq_sb);
449
Ingo Molnard3be9152006-03-23 03:00:29 -0800450 mutex_lock(&dquot->dq_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 /* Check whether we are not racing with some other dqget() */
452 if (atomic_read(&dquot->dq_count) > 1)
453 goto out_dqlock;
Ingo Molnard3be9152006-03-23 03:00:29 -0800454 mutex_lock(&dqopt->dqio_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 if (dqopt->ops[dquot->dq_type]->release_dqblk) {
456 ret = dqopt->ops[dquot->dq_type]->release_dqblk(dquot);
457 /* Write the info */
Jan Kara268157b2009-01-27 15:47:22 +0100458 if (info_dirty(&dqopt->info[dquot->dq_type])) {
459 ret2 = dqopt->ops[dquot->dq_type]->write_file_info(
460 dquot->dq_sb, dquot->dq_type);
461 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 if (ret >= 0)
463 ret = ret2;
464 }
465 clear_bit(DQ_ACTIVE_B, &dquot->dq_flags);
Ingo Molnard3be9152006-03-23 03:00:29 -0800466 mutex_unlock(&dqopt->dqio_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467out_dqlock:
Ingo Molnard3be9152006-03-23 03:00:29 -0800468 mutex_unlock(&dquot->dq_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 return ret;
470}
Mingming Cao08d03502009-01-14 16:19:32 +0100471EXPORT_SYMBOL(dquot_release);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472
Jan Kara7d9056b2008-11-25 15:31:32 +0100473void dquot_destroy(struct dquot *dquot)
Jan Kara74f783a2008-08-19 14:51:22 +0200474{
475 kmem_cache_free(dquot_cachep, dquot);
476}
Jan Kara7d9056b2008-11-25 15:31:32 +0100477EXPORT_SYMBOL(dquot_destroy);
Jan Kara74f783a2008-08-19 14:51:22 +0200478
479static inline void do_destroy_dquot(struct dquot *dquot)
480{
481 dquot->dq_sb->dq_op->destroy_dquot(dquot);
482}
483
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484/* Invalidate all dquots on the list. Note that this function is called after
485 * quota is disabled and pointers from inodes removed so there cannot be new
Jan Kara6362e4d2006-03-23 03:00:17 -0800486 * quota users. There can still be some users of quotas due to inodes being
487 * just deleted or pruned by prune_icache() (those are not attached to any
Jan Karacc334122009-01-12 17:23:05 +0100488 * list) or parallel quotactl call. We have to wait for such users.
Jan Kara6362e4d2006-03-23 03:00:17 -0800489 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490static void invalidate_dquots(struct super_block *sb, int type)
491{
Domen Puncerc33ed272005-06-25 14:59:36 -0700492 struct dquot *dquot, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493
Jan Kara6362e4d2006-03-23 03:00:17 -0800494restart:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 spin_lock(&dq_list_lock);
Domen Puncerc33ed272005-06-25 14:59:36 -0700496 list_for_each_entry_safe(dquot, tmp, &inuse_list, dq_inuse) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 if (dquot->dq_sb != sb)
498 continue;
499 if (dquot->dq_type != type)
500 continue;
Jan Kara6362e4d2006-03-23 03:00:17 -0800501 /* Wait for dquot users */
502 if (atomic_read(&dquot->dq_count)) {
503 DEFINE_WAIT(wait);
504
505 atomic_inc(&dquot->dq_count);
506 prepare_to_wait(&dquot->dq_wait_unused, &wait,
507 TASK_UNINTERRUPTIBLE);
508 spin_unlock(&dq_list_lock);
509 /* Once dqput() wakes us up, we know it's time to free
510 * the dquot.
511 * IMPORTANT: we rely on the fact that there is always
512 * at most one process waiting for dquot to free.
513 * Otherwise dq_count would be > 1 and we would never
514 * wake up.
515 */
516 if (atomic_read(&dquot->dq_count) > 1)
517 schedule();
518 finish_wait(&dquot->dq_wait_unused, &wait);
519 dqput(dquot);
520 /* At this moment dquot() need not exist (it could be
521 * reclaimed by prune_dqcache(). Hence we must
522 * restart. */
523 goto restart;
524 }
525 /*
526 * Quota now has no users and it has been written on last
527 * dqput()
528 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 remove_dquot_hash(dquot);
530 remove_free_dquot(dquot);
531 remove_inuse(dquot);
Jan Kara74f783a2008-08-19 14:51:22 +0200532 do_destroy_dquot(dquot);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 }
534 spin_unlock(&dq_list_lock);
535}
536
Jan Kara12c77522008-10-20 17:05:00 +0200537/* Call callback for every active dquot on given filesystem */
538int dquot_scan_active(struct super_block *sb,
539 int (*fn)(struct dquot *dquot, unsigned long priv),
540 unsigned long priv)
541{
542 struct dquot *dquot, *old_dquot = NULL;
543 int ret = 0;
544
545 mutex_lock(&sb_dqopt(sb)->dqonoff_mutex);
546 spin_lock(&dq_list_lock);
547 list_for_each_entry(dquot, &inuse_list, dq_inuse) {
548 if (!test_bit(DQ_ACTIVE_B, &dquot->dq_flags))
549 continue;
550 if (dquot->dq_sb != sb)
551 continue;
552 /* Now we have active dquot so we can just increase use count */
553 atomic_inc(&dquot->dq_count);
554 dqstats.lookups++;
555 spin_unlock(&dq_list_lock);
556 dqput(old_dquot);
557 old_dquot = dquot;
558 ret = fn(dquot, priv);
559 if (ret < 0)
560 goto out;
561 spin_lock(&dq_list_lock);
562 /* We are safe to continue now because our dquot could not
563 * be moved out of the inuse list while we hold the reference */
564 }
565 spin_unlock(&dq_list_lock);
566out:
567 dqput(old_dquot);
568 mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
569 return ret;
570}
Mingming Cao08d03502009-01-14 16:19:32 +0100571EXPORT_SYMBOL(dquot_scan_active);
Jan Kara12c77522008-10-20 17:05:00 +0200572
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573int vfs_quota_sync(struct super_block *sb, int type)
574{
575 struct list_head *dirty;
576 struct dquot *dquot;
577 struct quota_info *dqopt = sb_dqopt(sb);
578 int cnt;
579
Ingo Molnard3be9152006-03-23 03:00:29 -0800580 mutex_lock(&dqopt->dqonoff_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
582 if (type != -1 && cnt != type)
583 continue;
Jan Karaf55abc02008-08-20 17:50:32 +0200584 if (!sb_has_quota_active(sb, cnt))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 continue;
586 spin_lock(&dq_list_lock);
587 dirty = &dqopt->info[cnt].dqi_dirty_list;
588 while (!list_empty(dirty)) {
Jan Kara268157b2009-01-27 15:47:22 +0100589 dquot = list_first_entry(dirty, struct dquot,
590 dq_dirty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 /* Dirty and inactive can be only bad dquot... */
592 if (!test_bit(DQ_ACTIVE_B, &dquot->dq_flags)) {
593 clear_dquot_dirty(dquot);
594 continue;
595 }
596 /* Now we have active dquot from which someone is
597 * holding reference so we can safely just increase
598 * use count */
599 atomic_inc(&dquot->dq_count);
600 dqstats.lookups++;
601 spin_unlock(&dq_list_lock);
602 sb->dq_op->write_dquot(dquot);
603 dqput(dquot);
604 spin_lock(&dq_list_lock);
605 }
606 spin_unlock(&dq_list_lock);
607 }
608
609 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
Jan Karaf55abc02008-08-20 17:50:32 +0200610 if ((cnt == type || type == -1) && sb_has_quota_active(sb, cnt)
611 && info_dirty(&dqopt->info[cnt]))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 sb->dq_op->write_info(sb, cnt);
613 spin_lock(&dq_list_lock);
614 dqstats.syncs++;
615 spin_unlock(&dq_list_lock);
Ingo Molnard3be9152006-03-23 03:00:29 -0800616 mutex_unlock(&dqopt->dqonoff_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617
618 return 0;
619}
Mingming Cao08d03502009-01-14 16:19:32 +0100620EXPORT_SYMBOL(vfs_quota_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621
622/* Free unused dquots from cache */
623static void prune_dqcache(int count)
624{
625 struct list_head *head;
626 struct dquot *dquot;
627
628 head = free_dquots.prev;
629 while (head != &free_dquots && count) {
630 dquot = list_entry(head, struct dquot, dq_free);
631 remove_dquot_hash(dquot);
632 remove_free_dquot(dquot);
633 remove_inuse(dquot);
Jan Kara74f783a2008-08-19 14:51:22 +0200634 do_destroy_dquot(dquot);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 count--;
636 head = free_dquots.prev;
637 }
638}
639
640/*
641 * This is called from kswapd when we think we need some
642 * more memory
643 */
644
Al Viro27496a82005-10-21 03:20:48 -0400645static int shrink_dqcache_memory(int nr, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646{
647 if (nr) {
648 spin_lock(&dq_list_lock);
649 prune_dqcache(nr);
650 spin_unlock(&dq_list_lock);
651 }
652 return (dqstats.free_dquots / 100) * sysctl_vfs_cache_pressure;
653}
654
Rusty Russell8e1f9362007-07-17 04:03:17 -0700655static struct shrinker dqcache_shrinker = {
656 .shrink = shrink_dqcache_memory,
657 .seeks = DEFAULT_SEEKS,
658};
659
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660/*
661 * Put reference to dquot
662 * NOTE: If you change this function please check whether dqput_blocks() works right...
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 */
Jan Kara3d9ea252008-10-10 16:12:23 +0200664void dqput(struct dquot *dquot)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665{
Jan Karab48d3802008-07-25 01:46:49 -0700666 int ret;
667
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 if (!dquot)
669 return;
670#ifdef __DQUOT_PARANOIA
671 if (!atomic_read(&dquot->dq_count)) {
672 printk("VFS: dqput: trying to free free dquot\n");
673 printk("VFS: device %s, dquot of %s %d\n",
674 dquot->dq_sb->s_id,
675 quotatypes[dquot->dq_type],
676 dquot->dq_id);
677 BUG();
678 }
679#endif
680
681 spin_lock(&dq_list_lock);
682 dqstats.drops++;
683 spin_unlock(&dq_list_lock);
684we_slept:
685 spin_lock(&dq_list_lock);
686 if (atomic_read(&dquot->dq_count) > 1) {
687 /* We have more than one user... nothing to do */
688 atomic_dec(&dquot->dq_count);
Jan Kara6362e4d2006-03-23 03:00:17 -0800689 /* Releasing dquot during quotaoff phase? */
Jan Karaf55abc02008-08-20 17:50:32 +0200690 if (!sb_has_quota_active(dquot->dq_sb, dquot->dq_type) &&
Jan Kara6362e4d2006-03-23 03:00:17 -0800691 atomic_read(&dquot->dq_count) == 1)
692 wake_up(&dquot->dq_wait_unused);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 spin_unlock(&dq_list_lock);
694 return;
695 }
696 /* Need to release dquot? */
697 if (test_bit(DQ_ACTIVE_B, &dquot->dq_flags) && dquot_dirty(dquot)) {
698 spin_unlock(&dq_list_lock);
699 /* Commit dquot before releasing */
Jan Karab48d3802008-07-25 01:46:49 -0700700 ret = dquot->dq_sb->dq_op->write_dquot(dquot);
701 if (ret < 0) {
702 printk(KERN_ERR "VFS: cannot write quota structure on "
703 "device %s (error %d). Quota may get out of "
704 "sync!\n", dquot->dq_sb->s_id, ret);
705 /*
706 * We clear dirty bit anyway, so that we avoid
707 * infinite loop here
708 */
709 spin_lock(&dq_list_lock);
710 clear_dquot_dirty(dquot);
711 spin_unlock(&dq_list_lock);
712 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 goto we_slept;
714 }
715 /* Clear flag in case dquot was inactive (something bad happened) */
716 clear_dquot_dirty(dquot);
717 if (test_bit(DQ_ACTIVE_B, &dquot->dq_flags)) {
718 spin_unlock(&dq_list_lock);
719 dquot->dq_sb->dq_op->release_dquot(dquot);
720 goto we_slept;
721 }
722 atomic_dec(&dquot->dq_count);
723#ifdef __DQUOT_PARANOIA
724 /* sanity check */
Eric Sesterhenn8abf6a42006-04-02 13:36:13 +0200725 BUG_ON(!list_empty(&dquot->dq_free));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726#endif
727 put_dquot_last(dquot);
728 spin_unlock(&dq_list_lock);
729}
Mingming Cao08d03502009-01-14 16:19:32 +0100730EXPORT_SYMBOL(dqput);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731
Jan Kara7d9056b2008-11-25 15:31:32 +0100732struct dquot *dquot_alloc(struct super_block *sb, int type)
Jan Kara74f783a2008-08-19 14:51:22 +0200733{
734 return kmem_cache_zalloc(dquot_cachep, GFP_NOFS);
735}
Jan Kara7d9056b2008-11-25 15:31:32 +0100736EXPORT_SYMBOL(dquot_alloc);
Jan Kara74f783a2008-08-19 14:51:22 +0200737
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738static struct dquot *get_empty_dquot(struct super_block *sb, int type)
739{
740 struct dquot *dquot;
741
Jan Kara74f783a2008-08-19 14:51:22 +0200742 dquot = sb->dq_op->alloc_dquot(sb, type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 if(!dquot)
Jan Karadd6f3c62009-01-26 16:01:43 +0100744 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745
Ingo Molnard3be9152006-03-23 03:00:29 -0800746 mutex_init(&dquot->dq_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 INIT_LIST_HEAD(&dquot->dq_free);
748 INIT_LIST_HEAD(&dquot->dq_inuse);
749 INIT_HLIST_NODE(&dquot->dq_hash);
750 INIT_LIST_HEAD(&dquot->dq_dirty);
Jan Kara6362e4d2006-03-23 03:00:17 -0800751 init_waitqueue_head(&dquot->dq_wait_unused);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 dquot->dq_sb = sb;
753 dquot->dq_type = type;
754 atomic_set(&dquot->dq_count, 1);
755
756 return dquot;
757}
758
759/*
760 * Get reference to dquot
Jan Karacc334122009-01-12 17:23:05 +0100761 *
762 * Locking is slightly tricky here. We are guarded from parallel quotaoff()
763 * destroying our dquot by:
764 * a) checking for quota flags under dq_list_lock and
765 * b) getting a reference to dquot before we release dq_list_lock
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 */
Jan Kara3d9ea252008-10-10 16:12:23 +0200767struct dquot *dqget(struct super_block *sb, unsigned int id, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768{
769 unsigned int hashent = hashfn(sb, id, type);
Jan Karadd6f3c62009-01-26 16:01:43 +0100770 struct dquot *dquot = NULL, *empty = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771
Jan Karaf55abc02008-08-20 17:50:32 +0200772 if (!sb_has_quota_active(sb, type))
Jan Karadd6f3c62009-01-26 16:01:43 +0100773 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774we_slept:
775 spin_lock(&dq_list_lock);
Jan Karacc334122009-01-12 17:23:05 +0100776 spin_lock(&dq_state_lock);
777 if (!sb_has_quota_active(sb, type)) {
778 spin_unlock(&dq_state_lock);
779 spin_unlock(&dq_list_lock);
780 goto out;
781 }
782 spin_unlock(&dq_state_lock);
783
Jan Karadd6f3c62009-01-26 16:01:43 +0100784 dquot = find_dquot(hashent, sb, id, type);
785 if (!dquot) {
786 if (!empty) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 spin_unlock(&dq_list_lock);
Jan Karadd6f3c62009-01-26 16:01:43 +0100788 empty = get_empty_dquot(sb, type);
789 if (!empty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 schedule(); /* Try to wait for a moment... */
791 goto we_slept;
792 }
793 dquot = empty;
Jan Karadd6f3c62009-01-26 16:01:43 +0100794 empty = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 dquot->dq_id = id;
796 /* all dquots go on the inuse_list */
797 put_inuse(dquot);
798 /* hash it first so it can be found */
799 insert_dquot_hash(dquot);
800 dqstats.lookups++;
801 spin_unlock(&dq_list_lock);
802 } else {
803 if (!atomic_read(&dquot->dq_count))
804 remove_free_dquot(dquot);
805 atomic_inc(&dquot->dq_count);
806 dqstats.cache_hits++;
807 dqstats.lookups++;
808 spin_unlock(&dq_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 }
Jan Kara268157b2009-01-27 15:47:22 +0100810 /* Wait for dq_lock - after this we know that either dquot_release() is
811 * already finished or it will be canceled due to dq_count > 1 test */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 wait_on_dquot(dquot);
Jan Kara268157b2009-01-27 15:47:22 +0100813 /* Read the dquot / allocate space in quota file */
814 if (!test_bit(DQ_ACTIVE_B, &dquot->dq_flags) &&
815 sb->dq_op->acquire_dquot(dquot) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 dqput(dquot);
Jan Karadd6f3c62009-01-26 16:01:43 +0100817 dquot = NULL;
Jan Karacc334122009-01-12 17:23:05 +0100818 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 }
820#ifdef __DQUOT_PARANOIA
Eric Sesterhenn8abf6a42006-04-02 13:36:13 +0200821 BUG_ON(!dquot->dq_sb); /* Has somebody invalidated entry under us? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822#endif
Jan Karacc334122009-01-12 17:23:05 +0100823out:
824 if (empty)
825 do_destroy_dquot(empty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826
827 return dquot;
828}
Mingming Cao08d03502009-01-14 16:19:32 +0100829EXPORT_SYMBOL(dqget);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830
831static int dqinit_needed(struct inode *inode, int type)
832{
833 int cnt;
834
835 if (IS_NOQUOTA(inode))
836 return 0;
837 if (type != -1)
Jan Karadd6f3c62009-01-26 16:01:43 +0100838 return !inode->i_dquot[type];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
Jan Karadd6f3c62009-01-26 16:01:43 +0100840 if (!inode->i_dquot[cnt])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 return 1;
842 return 0;
843}
844
Ingo Molnard3be9152006-03-23 03:00:29 -0800845/* This routine is guarded by dqonoff_mutex mutex */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846static void add_dquot_ref(struct super_block *sb, int type)
847{
Jan Kara941d2382008-02-06 01:37:36 -0800848 struct inode *inode, *old_inode = NULL;
Jan Kara0a5a9c72010-02-09 18:20:39 +0100849 int reserved = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850
Christoph Hellwigd003fb72007-02-12 00:51:58 -0800851 spin_lock(&inode_lock);
852 list_for_each_entry(inode, &sb->s_inodes, i_sb_list) {
Wu Fengguangb6fac632009-04-02 16:56:34 -0700853 if (inode->i_state & (I_FREEING|I_CLEAR|I_WILL_FREE|I_NEW))
Nick Pigginaabb8fd2009-03-11 13:17:36 -0700854 continue;
Jan Kara0a5a9c72010-02-09 18:20:39 +0100855 if (unlikely(inode_get_rsv_space(inode) > 0))
856 reserved = 1;
Christoph Hellwigd003fb72007-02-12 00:51:58 -0800857 if (!atomic_read(&inode->i_writecount))
858 continue;
859 if (!dqinit_needed(inode, type))
860 continue;
Christoph Hellwigd003fb72007-02-12 00:51:58 -0800861
862 __iget(inode);
863 spin_unlock(&inode_lock);
864
Jan Kara941d2382008-02-06 01:37:36 -0800865 iput(old_inode);
Christoph Hellwigd003fb72007-02-12 00:51:58 -0800866 sb->dq_op->initialize(inode, type);
Jan Kara941d2382008-02-06 01:37:36 -0800867 /* We hold a reference to 'inode' so it couldn't have been
868 * removed from s_inodes list while we dropped the inode_lock.
869 * We cannot iput the inode now as we can be holding the last
870 * reference and we cannot iput it under inode_lock. So we
871 * keep the reference and iput it later. */
872 old_inode = inode;
873 spin_lock(&inode_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 }
Christoph Hellwigd003fb72007-02-12 00:51:58 -0800875 spin_unlock(&inode_lock);
Jan Kara941d2382008-02-06 01:37:36 -0800876 iput(old_inode);
Jan Kara0a5a9c72010-02-09 18:20:39 +0100877
878 if (reserved) {
879 printk(KERN_WARNING "VFS (%s): Writes happened before quota"
880 " was turned on thus quota information is probably "
881 "inconsistent. Please run quotacheck(8).\n", sb->s_id);
882 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883}
884
Jan Kara268157b2009-01-27 15:47:22 +0100885/*
886 * Return 0 if dqput() won't block.
887 * (note that 1 doesn't necessarily mean blocking)
888 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889static inline int dqput_blocks(struct dquot *dquot)
890{
891 if (atomic_read(&dquot->dq_count) <= 1)
892 return 1;
893 return 0;
894}
895
Jan Kara268157b2009-01-27 15:47:22 +0100896/*
897 * Remove references to dquots from inode and add dquot to list for freeing
898 * if we have the last referece to dquot
899 * We can't race with anybody because we hold dqptr_sem for writing...
900 */
Adrian Bunke5f00f42007-05-08 00:27:22 -0700901static int remove_inode_dquot_ref(struct inode *inode, int type,
902 struct list_head *tofree_head)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903{
904 struct dquot *dquot = inode->i_dquot[type];
905
Jan Karadd6f3c62009-01-26 16:01:43 +0100906 inode->i_dquot[type] = NULL;
907 if (dquot) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 if (dqput_blocks(dquot)) {
909#ifdef __DQUOT_PARANOIA
910 if (atomic_read(&dquot->dq_count) != 1)
911 printk(KERN_WARNING "VFS: Adding dquot with dq_count %d to dispose list.\n", atomic_read(&dquot->dq_count));
912#endif
913 spin_lock(&dq_list_lock);
Jan Kara268157b2009-01-27 15:47:22 +0100914 /* As dquot must have currently users it can't be on
915 * the free list... */
916 list_add(&dquot->dq_free, tofree_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 spin_unlock(&dq_list_lock);
918 return 1;
919 }
920 else
921 dqput(dquot); /* We have guaranteed we won't block */
922 }
923 return 0;
924}
925
Jan Kara268157b2009-01-27 15:47:22 +0100926/*
927 * Free list of dquots
928 * Dquots are removed from inodes and no new references can be got so we are
929 * the only ones holding reference
930 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931static void put_dquot_list(struct list_head *tofree_head)
932{
933 struct list_head *act_head;
934 struct dquot *dquot;
935
936 act_head = tofree_head->next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 while (act_head != tofree_head) {
938 dquot = list_entry(act_head, struct dquot, dq_free);
939 act_head = act_head->next;
Jan Kara268157b2009-01-27 15:47:22 +0100940 /* Remove dquot from the list so we won't have problems... */
941 list_del_init(&dquot->dq_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 dqput(dquot);
943 }
944}
945
Christoph Hellwigfb58b732007-02-12 00:51:57 -0800946static void remove_dquot_ref(struct super_block *sb, int type,
947 struct list_head *tofree_head)
948{
949 struct inode *inode;
950
951 spin_lock(&inode_lock);
952 list_for_each_entry(inode, &sb->s_inodes, i_sb_list) {
Nick Pigginaabb8fd2009-03-11 13:17:36 -0700953 /*
954 * We have to scan also I_NEW inodes because they can already
955 * have quota pointer initialized. Luckily, we need to touch
956 * only quota pointers and these have separate locking
957 * (dqptr_sem).
958 */
Christoph Hellwigfb58b732007-02-12 00:51:57 -0800959 if (!IS_NOQUOTA(inode))
960 remove_inode_dquot_ref(inode, type, tofree_head);
961 }
962 spin_unlock(&inode_lock);
963}
964
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965/* Gather all references from inodes and drop them */
966static void drop_dquot_ref(struct super_block *sb, int type)
967{
968 LIST_HEAD(tofree_head);
969
Christoph Hellwigfb58b732007-02-12 00:51:57 -0800970 if (sb->dq_op) {
971 down_write(&sb_dqopt(sb)->dqptr_sem);
972 remove_dquot_ref(sb, type, &tofree_head);
973 up_write(&sb_dqopt(sb)->dqptr_sem);
974 put_dquot_list(&tofree_head);
975 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976}
977
Jan Kara12095462008-08-20 14:45:12 +0200978static inline void dquot_incr_inodes(struct dquot *dquot, qsize_t number)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979{
980 dquot->dq_dqb.dqb_curinodes += number;
981}
982
983static inline void dquot_incr_space(struct dquot *dquot, qsize_t number)
984{
985 dquot->dq_dqb.dqb_curspace += number;
986}
987
Mingming Caof18df222009-01-13 16:43:09 +0100988static inline void dquot_resv_space(struct dquot *dquot, qsize_t number)
989{
990 dquot->dq_dqb.dqb_rsvspace += number;
991}
992
Mingming Cao740d9dc2009-01-13 16:43:14 +0100993/*
994 * Claim reserved quota space
995 */
Jan Kara0a5a9c72010-02-09 18:20:39 +0100996static void dquot_claim_reserved_space(struct dquot *dquot, qsize_t number)
Mingming Cao740d9dc2009-01-13 16:43:14 +0100997{
Jan Kara0a5a9c72010-02-09 18:20:39 +0100998 if (dquot->dq_dqb.dqb_rsvspace < number) {
999 WARN_ON_ONCE(1);
1000 number = dquot->dq_dqb.dqb_rsvspace;
1001 }
Mingming Cao740d9dc2009-01-13 16:43:14 +01001002 dquot->dq_dqb.dqb_curspace += number;
1003 dquot->dq_dqb.dqb_rsvspace -= number;
1004}
1005
1006static inline
1007void dquot_free_reserved_space(struct dquot *dquot, qsize_t number)
1008{
Jan Kara0a5a9c72010-02-09 18:20:39 +01001009 if (dquot->dq_dqb.dqb_rsvspace >= number)
1010 dquot->dq_dqb.dqb_rsvspace -= number;
1011 else {
1012 WARN_ON_ONCE(1);
1013 dquot->dq_dqb.dqb_rsvspace = 0;
1014 }
Mingming Cao740d9dc2009-01-13 16:43:14 +01001015}
1016
Jan Kara7a2435d2009-01-27 01:47:11 +01001017static void dquot_decr_inodes(struct dquot *dquot, qsize_t number)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018{
Jan Karadb49d2d2008-10-01 18:21:39 +02001019 if (sb_dqopt(dquot->dq_sb)->flags & DQUOT_NEGATIVE_USAGE ||
1020 dquot->dq_dqb.dqb_curinodes >= number)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 dquot->dq_dqb.dqb_curinodes -= number;
1022 else
1023 dquot->dq_dqb.dqb_curinodes = 0;
1024 if (dquot->dq_dqb.dqb_curinodes <= dquot->dq_dqb.dqb_isoftlimit)
1025 dquot->dq_dqb.dqb_itime = (time_t) 0;
1026 clear_bit(DQ_INODES_B, &dquot->dq_flags);
1027}
1028
Jan Kara7a2435d2009-01-27 01:47:11 +01001029static void dquot_decr_space(struct dquot *dquot, qsize_t number)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030{
Jan Karadb49d2d2008-10-01 18:21:39 +02001031 if (sb_dqopt(dquot->dq_sb)->flags & DQUOT_NEGATIVE_USAGE ||
1032 dquot->dq_dqb.dqb_curspace >= number)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 dquot->dq_dqb.dqb_curspace -= number;
1034 else
1035 dquot->dq_dqb.dqb_curspace = 0;
Jan Kara12095462008-08-20 14:45:12 +02001036 if (dquot->dq_dqb.dqb_curspace <= dquot->dq_dqb.dqb_bsoftlimit)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 dquot->dq_dqb.dqb_btime = (time_t) 0;
1038 clear_bit(DQ_BLKS_B, &dquot->dq_flags);
1039}
1040
Jan Karac5254602007-12-22 14:03:25 -08001041static int warning_issued(struct dquot *dquot, const int warntype)
1042{
1043 int flag = (warntype == QUOTA_NL_BHARDWARN ||
1044 warntype == QUOTA_NL_BSOFTLONGWARN) ? DQ_BLKS_B :
1045 ((warntype == QUOTA_NL_IHARDWARN ||
1046 warntype == QUOTA_NL_ISOFTLONGWARN) ? DQ_INODES_B : 0);
1047
1048 if (!flag)
1049 return 0;
1050 return test_and_set_bit(flag, &dquot->dq_flags);
1051}
1052
Jan Kara8e893462007-10-16 23:29:31 -07001053#ifdef CONFIG_PRINT_QUOTA_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054static int flag_print_warnings = 1;
1055
Jan Kara7a2435d2009-01-27 01:47:11 +01001056static int need_print_warning(struct dquot *dquot)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057{
1058 if (!flag_print_warnings)
1059 return 0;
1060
1061 switch (dquot->dq_type) {
1062 case USRQUOTA:
David Howellsda9592e2008-11-14 10:39:05 +11001063 return current_fsuid() == dquot->dq_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 case GRPQUOTA:
1065 return in_group_p(dquot->dq_id);
1066 }
1067 return 0;
1068}
1069
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070/* Print warning to user which exceeded quota */
Jan Karac5254602007-12-22 14:03:25 -08001071static void print_warning(struct dquot *dquot, const int warntype)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072{
1073 char *msg = NULL;
Peter Zijlstra24ec8392006-12-08 02:36:04 -08001074 struct tty_struct *tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075
Jan Kara657d3bf2008-07-25 01:46:52 -07001076 if (warntype == QUOTA_NL_IHARDBELOW ||
1077 warntype == QUOTA_NL_ISOFTBELOW ||
1078 warntype == QUOTA_NL_BHARDBELOW ||
1079 warntype == QUOTA_NL_BSOFTBELOW || !need_print_warning(dquot))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 return;
1081
Peter Zijlstra24ec8392006-12-08 02:36:04 -08001082 tty = get_current_tty();
1083 if (!tty)
Alan Cox452a00d2008-10-13 10:39:13 +01001084 return;
Peter Zijlstra24ec8392006-12-08 02:36:04 -08001085 tty_write_message(tty, dquot->dq_sb->s_id);
Jan Kara8e893462007-10-16 23:29:31 -07001086 if (warntype == QUOTA_NL_ISOFTWARN || warntype == QUOTA_NL_BSOFTWARN)
Peter Zijlstra24ec8392006-12-08 02:36:04 -08001087 tty_write_message(tty, ": warning, ");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 else
Peter Zijlstra24ec8392006-12-08 02:36:04 -08001089 tty_write_message(tty, ": write failed, ");
1090 tty_write_message(tty, quotatypes[dquot->dq_type]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 switch (warntype) {
Jan Kara8e893462007-10-16 23:29:31 -07001092 case QUOTA_NL_IHARDWARN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 msg = " file limit reached.\r\n";
1094 break;
Jan Kara8e893462007-10-16 23:29:31 -07001095 case QUOTA_NL_ISOFTLONGWARN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 msg = " file quota exceeded too long.\r\n";
1097 break;
Jan Kara8e893462007-10-16 23:29:31 -07001098 case QUOTA_NL_ISOFTWARN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 msg = " file quota exceeded.\r\n";
1100 break;
Jan Kara8e893462007-10-16 23:29:31 -07001101 case QUOTA_NL_BHARDWARN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 msg = " block limit reached.\r\n";
1103 break;
Jan Kara8e893462007-10-16 23:29:31 -07001104 case QUOTA_NL_BSOFTLONGWARN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 msg = " block quota exceeded too long.\r\n";
1106 break;
Jan Kara8e893462007-10-16 23:29:31 -07001107 case QUOTA_NL_BSOFTWARN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 msg = " block quota exceeded.\r\n";
1109 break;
1110 }
Peter Zijlstra24ec8392006-12-08 02:36:04 -08001111 tty_write_message(tty, msg);
Alan Cox452a00d2008-10-13 10:39:13 +01001112 tty_kref_put(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113}
Jan Kara8e893462007-10-16 23:29:31 -07001114#endif
1115
Mingming Caof18df222009-01-13 16:43:09 +01001116/*
1117 * Write warnings to the console and send warning messages over netlink.
1118 *
1119 * Note that this function can sleep.
1120 */
Jan Kara7a2435d2009-01-27 01:47:11 +01001121static void flush_warnings(struct dquot *const *dquots, char *warntype)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122{
Steven Whitehouse86e931a2009-09-28 12:35:17 +01001123 struct dquot *dq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 int i;
1125
Steven Whitehouse86e931a2009-09-28 12:35:17 +01001126 for (i = 0; i < MAXQUOTAS; i++) {
1127 dq = dquots[i];
1128 if (dq && warntype[i] != QUOTA_NL_NOWARN &&
1129 !warning_issued(dq, warntype[i])) {
Jan Kara8e893462007-10-16 23:29:31 -07001130#ifdef CONFIG_PRINT_QUOTA_WARNING
Steven Whitehouse86e931a2009-09-28 12:35:17 +01001131 print_warning(dq, warntype[i]);
Jan Kara8e893462007-10-16 23:29:31 -07001132#endif
Steven Whitehouse86e931a2009-09-28 12:35:17 +01001133 quota_send_warning(dq->dq_type, dq->dq_id,
1134 dq->dq_sb->s_dev, warntype[i]);
Jan Kara8e893462007-10-16 23:29:31 -07001135 }
Steven Whitehouse86e931a2009-09-28 12:35:17 +01001136 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137}
1138
Jan Kara7a2435d2009-01-27 01:47:11 +01001139static int ignore_hardlimit(struct dquot *dquot)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140{
1141 struct mem_dqinfo *info = &sb_dqopt(dquot->dq_sb)->info[dquot->dq_type];
1142
1143 return capable(CAP_SYS_RESOURCE) &&
Jan Kara268157b2009-01-27 15:47:22 +01001144 (info->dqi_format->qf_fmt_id != QFMT_VFS_OLD ||
1145 !(info->dqi_flags & V1_DQF_RSQUASH));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146}
1147
1148/* needs dq_data_lock */
Jan Kara12095462008-08-20 14:45:12 +02001149static int check_idq(struct dquot *dquot, qsize_t inodes, char *warntype)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150{
Jan Kara268157b2009-01-27 15:47:22 +01001151 qsize_t newinodes = dquot->dq_dqb.dqb_curinodes + inodes;
1152
Jan Kara8e893462007-10-16 23:29:31 -07001153 *warntype = QUOTA_NL_NOWARN;
Jan Karaf55abc02008-08-20 17:50:32 +02001154 if (!sb_has_quota_limits_enabled(dquot->dq_sb, dquot->dq_type) ||
1155 test_bit(DQ_FAKE_B, &dquot->dq_flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 return QUOTA_OK;
1157
1158 if (dquot->dq_dqb.dqb_ihardlimit &&
Jan Kara268157b2009-01-27 15:47:22 +01001159 newinodes > dquot->dq_dqb.dqb_ihardlimit &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 !ignore_hardlimit(dquot)) {
Jan Kara8e893462007-10-16 23:29:31 -07001161 *warntype = QUOTA_NL_IHARDWARN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 return NO_QUOTA;
1163 }
1164
1165 if (dquot->dq_dqb.dqb_isoftlimit &&
Jan Kara268157b2009-01-27 15:47:22 +01001166 newinodes > dquot->dq_dqb.dqb_isoftlimit &&
1167 dquot->dq_dqb.dqb_itime &&
1168 get_seconds() >= dquot->dq_dqb.dqb_itime &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 !ignore_hardlimit(dquot)) {
Jan Kara8e893462007-10-16 23:29:31 -07001170 *warntype = QUOTA_NL_ISOFTLONGWARN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 return NO_QUOTA;
1172 }
1173
1174 if (dquot->dq_dqb.dqb_isoftlimit &&
Jan Kara268157b2009-01-27 15:47:22 +01001175 newinodes > dquot->dq_dqb.dqb_isoftlimit &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 dquot->dq_dqb.dqb_itime == 0) {
Jan Kara8e893462007-10-16 23:29:31 -07001177 *warntype = QUOTA_NL_ISOFTWARN;
Jan Kara268157b2009-01-27 15:47:22 +01001178 dquot->dq_dqb.dqb_itime = get_seconds() +
1179 sb_dqopt(dquot->dq_sb)->info[dquot->dq_type].dqi_igrace;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 }
1181
1182 return QUOTA_OK;
1183}
1184
1185/* needs dq_data_lock */
1186static int check_bdq(struct dquot *dquot, qsize_t space, int prealloc, char *warntype)
1187{
Mingming Caof18df222009-01-13 16:43:09 +01001188 qsize_t tspace;
Jan Kara268157b2009-01-27 15:47:22 +01001189 struct super_block *sb = dquot->dq_sb;
Mingming Caof18df222009-01-13 16:43:09 +01001190
Jan Kara8e893462007-10-16 23:29:31 -07001191 *warntype = QUOTA_NL_NOWARN;
Jan Kara268157b2009-01-27 15:47:22 +01001192 if (!sb_has_quota_limits_enabled(sb, dquot->dq_type) ||
Jan Karaf55abc02008-08-20 17:50:32 +02001193 test_bit(DQ_FAKE_B, &dquot->dq_flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 return QUOTA_OK;
1195
Mingming Caof18df222009-01-13 16:43:09 +01001196 tspace = dquot->dq_dqb.dqb_curspace + dquot->dq_dqb.dqb_rsvspace
1197 + space;
1198
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 if (dquot->dq_dqb.dqb_bhardlimit &&
Mingming Caof18df222009-01-13 16:43:09 +01001200 tspace > dquot->dq_dqb.dqb_bhardlimit &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 !ignore_hardlimit(dquot)) {
1202 if (!prealloc)
Jan Kara8e893462007-10-16 23:29:31 -07001203 *warntype = QUOTA_NL_BHARDWARN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 return NO_QUOTA;
1205 }
1206
1207 if (dquot->dq_dqb.dqb_bsoftlimit &&
Mingming Caof18df222009-01-13 16:43:09 +01001208 tspace > dquot->dq_dqb.dqb_bsoftlimit &&
Jan Kara268157b2009-01-27 15:47:22 +01001209 dquot->dq_dqb.dqb_btime &&
1210 get_seconds() >= dquot->dq_dqb.dqb_btime &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 !ignore_hardlimit(dquot)) {
1212 if (!prealloc)
Jan Kara8e893462007-10-16 23:29:31 -07001213 *warntype = QUOTA_NL_BSOFTLONGWARN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 return NO_QUOTA;
1215 }
1216
1217 if (dquot->dq_dqb.dqb_bsoftlimit &&
Mingming Caof18df222009-01-13 16:43:09 +01001218 tspace > dquot->dq_dqb.dqb_bsoftlimit &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 dquot->dq_dqb.dqb_btime == 0) {
1220 if (!prealloc) {
Jan Kara8e893462007-10-16 23:29:31 -07001221 *warntype = QUOTA_NL_BSOFTWARN;
Jan Kara268157b2009-01-27 15:47:22 +01001222 dquot->dq_dqb.dqb_btime = get_seconds() +
1223 sb_dqopt(sb)->info[dquot->dq_type].dqi_bgrace;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 }
1225 else
1226 /*
1227 * We don't allow preallocation to exceed softlimit so exceeding will
1228 * be always printed
1229 */
1230 return NO_QUOTA;
1231 }
1232
1233 return QUOTA_OK;
1234}
1235
Jan Kara12095462008-08-20 14:45:12 +02001236static int info_idq_free(struct dquot *dquot, qsize_t inodes)
Jan Kara657d3bf2008-07-25 01:46:52 -07001237{
Jan Kara268157b2009-01-27 15:47:22 +01001238 qsize_t newinodes;
1239
Jan Kara657d3bf2008-07-25 01:46:52 -07001240 if (test_bit(DQ_FAKE_B, &dquot->dq_flags) ||
Jan Karaf55abc02008-08-20 17:50:32 +02001241 dquot->dq_dqb.dqb_curinodes <= dquot->dq_dqb.dqb_isoftlimit ||
1242 !sb_has_quota_limits_enabled(dquot->dq_sb, dquot->dq_type))
Jan Kara657d3bf2008-07-25 01:46:52 -07001243 return QUOTA_NL_NOWARN;
1244
Jan Kara268157b2009-01-27 15:47:22 +01001245 newinodes = dquot->dq_dqb.dqb_curinodes - inodes;
1246 if (newinodes <= dquot->dq_dqb.dqb_isoftlimit)
Jan Kara657d3bf2008-07-25 01:46:52 -07001247 return QUOTA_NL_ISOFTBELOW;
1248 if (dquot->dq_dqb.dqb_curinodes >= dquot->dq_dqb.dqb_ihardlimit &&
Jan Kara268157b2009-01-27 15:47:22 +01001249 newinodes < dquot->dq_dqb.dqb_ihardlimit)
Jan Kara657d3bf2008-07-25 01:46:52 -07001250 return QUOTA_NL_IHARDBELOW;
1251 return QUOTA_NL_NOWARN;
1252}
1253
1254static int info_bdq_free(struct dquot *dquot, qsize_t space)
1255{
1256 if (test_bit(DQ_FAKE_B, &dquot->dq_flags) ||
Jan Kara12095462008-08-20 14:45:12 +02001257 dquot->dq_dqb.dqb_curspace <= dquot->dq_dqb.dqb_bsoftlimit)
Jan Kara657d3bf2008-07-25 01:46:52 -07001258 return QUOTA_NL_NOWARN;
1259
Jan Kara12095462008-08-20 14:45:12 +02001260 if (dquot->dq_dqb.dqb_curspace - space <= dquot->dq_dqb.dqb_bsoftlimit)
Jan Kara657d3bf2008-07-25 01:46:52 -07001261 return QUOTA_NL_BSOFTBELOW;
Jan Kara12095462008-08-20 14:45:12 +02001262 if (dquot->dq_dqb.dqb_curspace >= dquot->dq_dqb.dqb_bhardlimit &&
1263 dquot->dq_dqb.dqb_curspace - space < dquot->dq_dqb.dqb_bhardlimit)
Jan Kara657d3bf2008-07-25 01:46:52 -07001264 return QUOTA_NL_BHARDBELOW;
1265 return QUOTA_NL_NOWARN;
1266}
Jan Kara0a5a9c72010-02-09 18:20:39 +01001267
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268/*
1269 * Initialize quota pointers in inode
Jan Karacc334122009-01-12 17:23:05 +01001270 * We do things in a bit complicated way but by that we avoid calling
1271 * dqget() and thus filesystem callbacks under dqptr_sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272 */
1273int dquot_initialize(struct inode *inode, int type)
1274{
1275 unsigned int id = 0;
1276 int cnt, ret = 0;
Jan Karadd6f3c62009-01-26 16:01:43 +01001277 struct dquot *got[MAXQUOTAS] = { NULL, NULL };
Jan Karacc334122009-01-12 17:23:05 +01001278 struct super_block *sb = inode->i_sb;
Jan Kara0a5a9c72010-02-09 18:20:39 +01001279 qsize_t rsv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280
Ingo Molnard3be9152006-03-23 03:00:29 -08001281 /* First test before acquiring mutex - solves deadlocks when we
1282 * re-enter the quota code and are already holding the mutex */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 if (IS_NOQUOTA(inode))
1284 return 0;
Jan Karacc334122009-01-12 17:23:05 +01001285
1286 /* First get references to structures we might need. */
1287 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1288 if (type != -1 && cnt != type)
1289 continue;
1290 switch (cnt) {
1291 case USRQUOTA:
1292 id = inode->i_uid;
1293 break;
1294 case GRPQUOTA:
1295 id = inode->i_gid;
1296 break;
1297 }
1298 got[cnt] = dqget(sb, id, cnt);
1299 }
1300
1301 down_write(&sb_dqopt(sb)->dqptr_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 if (IS_NOQUOTA(inode))
1303 goto out_err;
1304 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1305 if (type != -1 && cnt != type)
1306 continue;
Jan Karacc334122009-01-12 17:23:05 +01001307 /* Avoid races with quotaoff() */
1308 if (!sb_has_quota_active(sb, cnt))
1309 continue;
Jan Karadd6f3c62009-01-26 16:01:43 +01001310 if (!inode->i_dquot[cnt]) {
Jan Karacc334122009-01-12 17:23:05 +01001311 inode->i_dquot[cnt] = got[cnt];
Jan Karadd6f3c62009-01-26 16:01:43 +01001312 got[cnt] = NULL;
Jan Kara0a5a9c72010-02-09 18:20:39 +01001313 /*
1314 * Make quota reservation system happy if someone
1315 * did a write before quota was turned on
1316 */
1317 rsv = inode_get_rsv_space(inode);
1318 if (unlikely(rsv))
1319 dquot_resv_space(inode->i_dquot[cnt], rsv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320 }
1321 }
1322out_err:
Jan Karacc334122009-01-12 17:23:05 +01001323 up_write(&sb_dqopt(sb)->dqptr_sem);
1324 /* Drop unused references */
Dmitry Monakhovdc52dd32009-12-14 15:21:15 +03001325 dqput_all(got);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 return ret;
1327}
Mingming Cao08d03502009-01-14 16:19:32 +01001328EXPORT_SYMBOL(dquot_initialize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329
1330/*
1331 * Release all quotas referenced by inode
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 */
Jan Kara3d9ea252008-10-10 16:12:23 +02001333int dquot_drop(struct inode *inode)
1334{
Jan Karacc334122009-01-12 17:23:05 +01001335 int cnt;
1336 struct dquot *put[MAXQUOTAS];
1337
Jan Kara3d9ea252008-10-10 16:12:23 +02001338 down_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
Jan Karacc334122009-01-12 17:23:05 +01001339 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1340 put[cnt] = inode->i_dquot[cnt];
Jan Karadd6f3c62009-01-26 16:01:43 +01001341 inode->i_dquot[cnt] = NULL;
Jan Karacc334122009-01-12 17:23:05 +01001342 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 up_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
Dmitry Monakhovdc52dd32009-12-14 15:21:15 +03001344 dqput_all(put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 return 0;
1346}
Mingming Cao08d03502009-01-14 16:19:32 +01001347EXPORT_SYMBOL(dquot_drop);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348
Jan Karab85f4b82008-07-25 01:46:50 -07001349/* Wrapper to remove references to quota structures from inode */
1350void vfs_dq_drop(struct inode *inode)
1351{
1352 /* Here we can get arbitrary inode from clear_inode() so we have
1353 * to be careful. OTOH we don't need locking as quota operations
1354 * are allowed to change only at mount time */
1355 if (!IS_NOQUOTA(inode) && inode->i_sb && inode->i_sb->dq_op
1356 && inode->i_sb->dq_op->drop) {
1357 int cnt;
1358 /* Test before calling to rule out calls from proc and such
1359 * where we are not allowed to block. Note that this is
1360 * actually reliable test even without the lock - the caller
1361 * must assure that nobody can come after the DQUOT_DROP and
1362 * add quota pointers back anyway */
1363 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
Jan Karadd6f3c62009-01-26 16:01:43 +01001364 if (inode->i_dquot[cnt])
Jan Karab85f4b82008-07-25 01:46:50 -07001365 break;
1366 if (cnt < MAXQUOTAS)
1367 inode->i_sb->dq_op->drop(inode);
1368 }
1369}
Mingming Cao08d03502009-01-14 16:19:32 +01001370EXPORT_SYMBOL(vfs_dq_drop);
Jan Karab85f4b82008-07-25 01:46:50 -07001371
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372/*
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001373 * inode_reserved_space is managed internally by quota, and protected by
1374 * i_lock similar to i_blocks+i_bytes.
1375 */
1376static qsize_t *inode_reserved_space(struct inode * inode)
1377{
1378 /* Filesystem must explicitly define it's own method in order to use
1379 * quota reservation interface */
1380 BUG_ON(!inode->i_sb->dq_op->get_reserved_space);
1381 return inode->i_sb->dq_op->get_reserved_space(inode);
1382}
1383
Dmitry Monakhovc4690702010-02-09 17:53:36 +01001384void inode_add_rsv_space(struct inode *inode, qsize_t number)
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001385{
1386 spin_lock(&inode->i_lock);
1387 *inode_reserved_space(inode) += number;
1388 spin_unlock(&inode->i_lock);
1389}
Dmitry Monakhovc4690702010-02-09 17:53:36 +01001390EXPORT_SYMBOL(inode_add_rsv_space);
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001391
Dmitry Monakhovc4690702010-02-09 17:53:36 +01001392void inode_claim_rsv_space(struct inode *inode, qsize_t number)
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001393{
1394 spin_lock(&inode->i_lock);
1395 *inode_reserved_space(inode) -= number;
1396 __inode_add_bytes(inode, number);
1397 spin_unlock(&inode->i_lock);
1398}
Dmitry Monakhovc4690702010-02-09 17:53:36 +01001399EXPORT_SYMBOL(inode_claim_rsv_space);
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001400
Dmitry Monakhovc4690702010-02-09 17:53:36 +01001401void inode_sub_rsv_space(struct inode *inode, qsize_t number)
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001402{
1403 spin_lock(&inode->i_lock);
1404 *inode_reserved_space(inode) -= number;
1405 spin_unlock(&inode->i_lock);
1406}
Dmitry Monakhovc4690702010-02-09 17:53:36 +01001407EXPORT_SYMBOL(inode_sub_rsv_space);
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001408
1409static qsize_t inode_get_rsv_space(struct inode *inode)
1410{
1411 qsize_t ret;
Jan Kara05b5d892010-01-06 18:03:36 +01001412
1413 if (!inode->i_sb->dq_op->get_reserved_space)
1414 return 0;
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001415 spin_lock(&inode->i_lock);
1416 ret = *inode_reserved_space(inode);
1417 spin_unlock(&inode->i_lock);
1418 return ret;
1419}
1420
1421static void inode_incr_space(struct inode *inode, qsize_t number,
1422 int reserve)
1423{
1424 if (reserve)
1425 inode_add_rsv_space(inode, number);
1426 else
1427 inode_add_bytes(inode, number);
1428}
1429
1430static void inode_decr_space(struct inode *inode, qsize_t number, int reserve)
1431{
1432 if (reserve)
1433 inode_sub_rsv_space(inode, number);
1434 else
1435 inode_sub_bytes(inode, number);
1436}
1437
1438/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439 * Following four functions update i_blocks+i_bytes fields and
1440 * quota information (together with appropriate checks)
1441 * NOTE: We absolutely rely on the fact that caller dirties
1442 * the inode (usually macros in quotaops.h care about this) and
1443 * holds a handle for the current transaction so that dquot write and
1444 * inode write go into the same transaction.
1445 */
1446
1447/*
1448 * This operation can block, but only after everything is updated
1449 */
Mingming Caof18df222009-01-13 16:43:09 +01001450int __dquot_alloc_space(struct inode *inode, qsize_t number,
1451 int warn, int reserve)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452{
Mingming Caof18df222009-01-13 16:43:09 +01001453 int cnt, ret = QUOTA_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454 char warntype[MAXQUOTAS];
1455
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001456 /*
1457 * First test before acquiring mutex - solves deadlocks when we
1458 * re-enter the quota code and are already holding the mutex
1459 */
1460 if (IS_NOQUOTA(inode)) {
1461 inode_incr_space(inode, number, reserve);
1462 goto out;
1463 }
1464
1465 down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
Jan Kara8e893462007-10-16 23:29:31 -07001467 warntype[cnt] = QUOTA_NL_NOWARN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 spin_lock(&dq_data_lock);
1470 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
Jan Karadd6f3c62009-01-26 16:01:43 +01001471 if (!inode->i_dquot[cnt])
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 continue;
Mingming Caof18df222009-01-13 16:43:09 +01001473 if (check_bdq(inode->i_dquot[cnt], number, warn, warntype+cnt)
1474 == NO_QUOTA) {
1475 ret = NO_QUOTA;
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001476 spin_unlock(&dq_data_lock);
1477 goto out_flush_warn;
Mingming Caof18df222009-01-13 16:43:09 +01001478 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479 }
1480 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
Jan Karadd6f3c62009-01-26 16:01:43 +01001481 if (!inode->i_dquot[cnt])
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482 continue;
Mingming Caof18df222009-01-13 16:43:09 +01001483 if (reserve)
1484 dquot_resv_space(inode->i_dquot[cnt], number);
1485 else
1486 dquot_incr_space(inode->i_dquot[cnt], number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487 }
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001488 inode_incr_space(inode, number, reserve);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489 spin_unlock(&dq_data_lock);
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001490
1491 if (reserve)
1492 goto out_flush_warn;
Dmitry Monakhovdc52dd32009-12-14 15:21:15 +03001493 mark_all_dquot_dirty(inode->i_dquot);
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001494out_flush_warn:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 flush_warnings(inode->i_dquot, warntype);
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001496 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1497out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498 return ret;
1499}
1500
Mingming Caof18df222009-01-13 16:43:09 +01001501int dquot_alloc_space(struct inode *inode, qsize_t number, int warn)
1502{
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001503 return __dquot_alloc_space(inode, number, warn, 0);
Mingming Caof18df222009-01-13 16:43:09 +01001504}
Mingming Cao08d03502009-01-14 16:19:32 +01001505EXPORT_SYMBOL(dquot_alloc_space);
Mingming Caof18df222009-01-13 16:43:09 +01001506
1507int dquot_reserve_space(struct inode *inode, qsize_t number, int warn)
1508{
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001509 return __dquot_alloc_space(inode, number, warn, 1);
Mingming Caof18df222009-01-13 16:43:09 +01001510}
1511EXPORT_SYMBOL(dquot_reserve_space);
1512
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513/*
1514 * This operation can block, but only after everything is updated
1515 */
Jan Kara12095462008-08-20 14:45:12 +02001516int dquot_alloc_inode(const struct inode *inode, qsize_t number)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517{
1518 int cnt, ret = NO_QUOTA;
1519 char warntype[MAXQUOTAS];
1520
Ingo Molnard3be9152006-03-23 03:00:29 -08001521 /* First test before acquiring mutex - solves deadlocks when we
1522 * re-enter the quota code and are already holding the mutex */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523 if (IS_NOQUOTA(inode))
1524 return QUOTA_OK;
1525 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
Jan Kara8e893462007-10-16 23:29:31 -07001526 warntype[cnt] = QUOTA_NL_NOWARN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527 down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528 spin_lock(&dq_data_lock);
1529 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
Jan Karadd6f3c62009-01-26 16:01:43 +01001530 if (!inode->i_dquot[cnt])
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531 continue;
Jan Kara268157b2009-01-27 15:47:22 +01001532 if (check_idq(inode->i_dquot[cnt], number, warntype+cnt)
1533 == NO_QUOTA)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534 goto warn_put_all;
1535 }
1536
1537 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
Jan Karadd6f3c62009-01-26 16:01:43 +01001538 if (!inode->i_dquot[cnt])
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539 continue;
1540 dquot_incr_inodes(inode->i_dquot[cnt], number);
1541 }
1542 ret = QUOTA_OK;
1543warn_put_all:
1544 spin_unlock(&dq_data_lock);
1545 if (ret == QUOTA_OK)
Dmitry Monakhovdc52dd32009-12-14 15:21:15 +03001546 mark_all_dquot_dirty(inode->i_dquot);
Jan Kara087ee8d2007-12-17 16:20:26 -08001547 flush_warnings(inode->i_dquot, warntype);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1549 return ret;
1550}
Mingming Cao08d03502009-01-14 16:19:32 +01001551EXPORT_SYMBOL(dquot_alloc_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552
Mingming Cao740d9dc2009-01-13 16:43:14 +01001553int dquot_claim_space(struct inode *inode, qsize_t number)
1554{
1555 int cnt;
1556 int ret = QUOTA_OK;
1557
1558 if (IS_NOQUOTA(inode)) {
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001559 inode_claim_rsv_space(inode, number);
Mingming Cao740d9dc2009-01-13 16:43:14 +01001560 goto out;
1561 }
1562
1563 down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
Mingming Cao740d9dc2009-01-13 16:43:14 +01001564 spin_lock(&dq_data_lock);
1565 /* Claim reserved quotas to allocated quotas */
1566 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
Jan Karadd6f3c62009-01-26 16:01:43 +01001567 if (inode->i_dquot[cnt])
Mingming Cao740d9dc2009-01-13 16:43:14 +01001568 dquot_claim_reserved_space(inode->i_dquot[cnt],
1569 number);
1570 }
1571 /* Update inode bytes */
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001572 inode_claim_rsv_space(inode, number);
Mingming Cao740d9dc2009-01-13 16:43:14 +01001573 spin_unlock(&dq_data_lock);
Dmitry Monakhovdc52dd32009-12-14 15:21:15 +03001574 mark_all_dquot_dirty(inode->i_dquot);
Mingming Cao740d9dc2009-01-13 16:43:14 +01001575 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1576out:
1577 return ret;
1578}
1579EXPORT_SYMBOL(dquot_claim_space);
1580
1581/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582 * This operation can block, but only after everything is updated
1583 */
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001584int __dquot_free_space(struct inode *inode, qsize_t number, int reserve)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585{
1586 unsigned int cnt;
Jan Kara657d3bf2008-07-25 01:46:52 -07001587 char warntype[MAXQUOTAS];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588
Ingo Molnard3be9152006-03-23 03:00:29 -08001589 /* First test before acquiring mutex - solves deadlocks when we
1590 * re-enter the quota code and are already holding the mutex */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591 if (IS_NOQUOTA(inode)) {
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001592 inode_decr_space(inode, number, reserve);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593 return QUOTA_OK;
1594 }
Jan Kara657d3bf2008-07-25 01:46:52 -07001595
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596 down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597 spin_lock(&dq_data_lock);
1598 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
Jan Karadd6f3c62009-01-26 16:01:43 +01001599 if (!inode->i_dquot[cnt])
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600 continue;
Jan Kara657d3bf2008-07-25 01:46:52 -07001601 warntype[cnt] = info_bdq_free(inode->i_dquot[cnt], number);
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001602 if (reserve)
1603 dquot_free_reserved_space(inode->i_dquot[cnt], number);
1604 else
1605 dquot_decr_space(inode->i_dquot[cnt], number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606 }
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001607 inode_decr_space(inode, number, reserve);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608 spin_unlock(&dq_data_lock);
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001609
1610 if (reserve)
1611 goto out_unlock;
Dmitry Monakhovdc52dd32009-12-14 15:21:15 +03001612 mark_all_dquot_dirty(inode->i_dquot);
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001613out_unlock:
Jan Kara657d3bf2008-07-25 01:46:52 -07001614 flush_warnings(inode->i_dquot, warntype);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1616 return QUOTA_OK;
1617}
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001618
1619int dquot_free_space(struct inode *inode, qsize_t number)
1620{
1621 return __dquot_free_space(inode, number, 0);
1622}
Mingming Cao08d03502009-01-14 16:19:32 +01001623EXPORT_SYMBOL(dquot_free_space);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624
1625/*
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001626 * Release reserved quota space
1627 */
1628void dquot_release_reserved_space(struct inode *inode, qsize_t number)
1629{
1630 __dquot_free_space(inode, number, 1);
1631
1632}
1633EXPORT_SYMBOL(dquot_release_reserved_space);
1634
1635/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636 * This operation can block, but only after everything is updated
1637 */
Jan Kara12095462008-08-20 14:45:12 +02001638int dquot_free_inode(const struct inode *inode, qsize_t number)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639{
1640 unsigned int cnt;
Jan Kara657d3bf2008-07-25 01:46:52 -07001641 char warntype[MAXQUOTAS];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642
Ingo Molnard3be9152006-03-23 03:00:29 -08001643 /* First test before acquiring mutex - solves deadlocks when we
1644 * re-enter the quota code and are already holding the mutex */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645 if (IS_NOQUOTA(inode))
1646 return QUOTA_OK;
Jan Kara657d3bf2008-07-25 01:46:52 -07001647
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648 down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649 spin_lock(&dq_data_lock);
1650 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
Jan Karadd6f3c62009-01-26 16:01:43 +01001651 if (!inode->i_dquot[cnt])
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652 continue;
Jan Kara657d3bf2008-07-25 01:46:52 -07001653 warntype[cnt] = info_idq_free(inode->i_dquot[cnt], number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654 dquot_decr_inodes(inode->i_dquot[cnt], number);
1655 }
1656 spin_unlock(&dq_data_lock);
Dmitry Monakhovdc52dd32009-12-14 15:21:15 +03001657 mark_all_dquot_dirty(inode->i_dquot);
Jan Kara657d3bf2008-07-25 01:46:52 -07001658 flush_warnings(inode->i_dquot, warntype);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1660 return QUOTA_OK;
1661}
Mingming Cao08d03502009-01-14 16:19:32 +01001662EXPORT_SYMBOL(dquot_free_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663
1664/*
1665 * Transfer the number of inode and blocks from one diskquota to an other.
1666 *
1667 * This operation can block, but only after everything is updated
1668 * A transaction must be started when entering this function.
1669 */
1670int dquot_transfer(struct inode *inode, struct iattr *iattr)
1671{
Mingming Cao740d9dc2009-01-13 16:43:14 +01001672 qsize_t space, cur_space;
1673 qsize_t rsv_space = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674 struct dquot *transfer_from[MAXQUOTAS];
1675 struct dquot *transfer_to[MAXQUOTAS];
Jan Karacc334122009-01-12 17:23:05 +01001676 int cnt, ret = QUOTA_OK;
1677 int chuid = iattr->ia_valid & ATTR_UID && inode->i_uid != iattr->ia_uid,
1678 chgid = iattr->ia_valid & ATTR_GID && inode->i_gid != iattr->ia_gid;
Jan Kara657d3bf2008-07-25 01:46:52 -07001679 char warntype_to[MAXQUOTAS];
1680 char warntype_from_inodes[MAXQUOTAS], warntype_from_space[MAXQUOTAS];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681
Ingo Molnard3be9152006-03-23 03:00:29 -08001682 /* First test before acquiring mutex - solves deadlocks when we
1683 * re-enter the quota code and are already holding the mutex */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684 if (IS_NOQUOTA(inode))
1685 return QUOTA_OK;
Jan Karacc334122009-01-12 17:23:05 +01001686 /* Initialize the arrays */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
Jan Karadd6f3c62009-01-26 16:01:43 +01001688 transfer_from[cnt] = NULL;
1689 transfer_to[cnt] = NULL;
Jan Kara657d3bf2008-07-25 01:46:52 -07001690 warntype_to[cnt] = QUOTA_NL_NOWARN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691 }
Jan Kara268157b2009-01-27 15:47:22 +01001692 if (chuid)
1693 transfer_to[USRQUOTA] = dqget(inode->i_sb, iattr->ia_uid,
1694 USRQUOTA);
1695 if (chgid)
1696 transfer_to[GRPQUOTA] = dqget(inode->i_sb, iattr->ia_gid,
1697 GRPQUOTA);
Jan Karacc334122009-01-12 17:23:05 +01001698
1699 down_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
Jan Karacc334122009-01-12 17:23:05 +01001700 if (IS_NOQUOTA(inode)) { /* File without quota accounting? */
1701 up_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
1702 goto put_all;
1703 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704 spin_lock(&dq_data_lock);
Mingming Cao740d9dc2009-01-13 16:43:14 +01001705 cur_space = inode_get_bytes(inode);
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001706 rsv_space = inode_get_rsv_space(inode);
Mingming Cao740d9dc2009-01-13 16:43:14 +01001707 space = cur_space + rsv_space;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708 /* Build the transfer_from list and check the limits */
1709 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
Jan Karadd6f3c62009-01-26 16:01:43 +01001710 if (!transfer_to[cnt])
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711 continue;
1712 transfer_from[cnt] = inode->i_dquot[cnt];
Jan Kara657d3bf2008-07-25 01:46:52 -07001713 if (check_idq(transfer_to[cnt], 1, warntype_to + cnt) ==
1714 NO_QUOTA || check_bdq(transfer_to[cnt], space, 0,
1715 warntype_to + cnt) == NO_QUOTA)
Jan Karacc334122009-01-12 17:23:05 +01001716 goto over_quota;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717 }
1718
1719 /*
1720 * Finally perform the needed transfer from transfer_from to transfer_to
1721 */
1722 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1723 /*
1724 * Skip changes for same uid or gid or for turned off quota-type.
1725 */
Jan Karadd6f3c62009-01-26 16:01:43 +01001726 if (!transfer_to[cnt])
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727 continue;
1728
1729 /* Due to IO error we might not have transfer_from[] structure */
1730 if (transfer_from[cnt]) {
Jan Kara657d3bf2008-07-25 01:46:52 -07001731 warntype_from_inodes[cnt] =
1732 info_idq_free(transfer_from[cnt], 1);
1733 warntype_from_space[cnt] =
1734 info_bdq_free(transfer_from[cnt], space);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735 dquot_decr_inodes(transfer_from[cnt], 1);
Mingming Cao740d9dc2009-01-13 16:43:14 +01001736 dquot_decr_space(transfer_from[cnt], cur_space);
1737 dquot_free_reserved_space(transfer_from[cnt],
1738 rsv_space);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739 }
1740
1741 dquot_incr_inodes(transfer_to[cnt], 1);
Mingming Cao740d9dc2009-01-13 16:43:14 +01001742 dquot_incr_space(transfer_to[cnt], cur_space);
1743 dquot_resv_space(transfer_to[cnt], rsv_space);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744
1745 inode->i_dquot[cnt] = transfer_to[cnt];
1746 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747 spin_unlock(&dq_data_lock);
Jan Karacc334122009-01-12 17:23:05 +01001748 up_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
1749
Dmitry Monakhovdc52dd32009-12-14 15:21:15 +03001750 mark_all_dquot_dirty(transfer_from);
1751 mark_all_dquot_dirty(transfer_to);
1752 /* The reference we got is transferred to the inode */
1753 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1754 transfer_to[cnt] = NULL;
Jan Karacc334122009-01-12 17:23:05 +01001755warn_put_all:
Jan Kara657d3bf2008-07-25 01:46:52 -07001756 flush_warnings(transfer_to, warntype_to);
1757 flush_warnings(transfer_from, warntype_from_inodes);
1758 flush_warnings(transfer_from, warntype_from_space);
Jan Karacc334122009-01-12 17:23:05 +01001759put_all:
Dmitry Monakhovdc52dd32009-12-14 15:21:15 +03001760 dqput_all(transfer_from);
1761 dqput_all(transfer_to);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762 return ret;
Jan Karacc334122009-01-12 17:23:05 +01001763over_quota:
1764 spin_unlock(&dq_data_lock);
1765 up_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
1766 /* Clear dquot pointers we don't want to dqput() */
1767 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
Jan Karadd6f3c62009-01-26 16:01:43 +01001768 transfer_from[cnt] = NULL;
Jan Karacc334122009-01-12 17:23:05 +01001769 ret = NO_QUOTA;
1770 goto warn_put_all;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771}
Mingming Cao08d03502009-01-14 16:19:32 +01001772EXPORT_SYMBOL(dquot_transfer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773
Jan Karab85f4b82008-07-25 01:46:50 -07001774/* Wrapper for transferring ownership of an inode */
1775int vfs_dq_transfer(struct inode *inode, struct iattr *iattr)
1776{
Jan Karaf55abc02008-08-20 17:50:32 +02001777 if (sb_any_quota_active(inode->i_sb) && !IS_NOQUOTA(inode)) {
Jan Karab85f4b82008-07-25 01:46:50 -07001778 vfs_dq_init(inode);
1779 if (inode->i_sb->dq_op->transfer(inode, iattr) == NO_QUOTA)
1780 return 1;
1781 }
1782 return 0;
1783}
Mingming Cao08d03502009-01-14 16:19:32 +01001784EXPORT_SYMBOL(vfs_dq_transfer);
Jan Karab85f4b82008-07-25 01:46:50 -07001785
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786/*
1787 * Write info of quota file to disk
1788 */
1789int dquot_commit_info(struct super_block *sb, int type)
1790{
1791 int ret;
1792 struct quota_info *dqopt = sb_dqopt(sb);
1793
Ingo Molnard3be9152006-03-23 03:00:29 -08001794 mutex_lock(&dqopt->dqio_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795 ret = dqopt->ops[type]->write_file_info(sb, type);
Ingo Molnard3be9152006-03-23 03:00:29 -08001796 mutex_unlock(&dqopt->dqio_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797 return ret;
1798}
Mingming Cao08d03502009-01-14 16:19:32 +01001799EXPORT_SYMBOL(dquot_commit_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800
1801/*
1802 * Definitions of diskquota operations.
1803 */
Alexey Dobriyan61e225d2009-09-21 17:01:08 -07001804const struct dquot_operations dquot_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805 .initialize = dquot_initialize,
1806 .drop = dquot_drop,
1807 .alloc_space = dquot_alloc_space,
1808 .alloc_inode = dquot_alloc_inode,
1809 .free_space = dquot_free_space,
1810 .free_inode = dquot_free_inode,
1811 .transfer = dquot_transfer,
1812 .write_dquot = dquot_commit,
1813 .acquire_dquot = dquot_acquire,
1814 .release_dquot = dquot_release,
1815 .mark_dirty = dquot_mark_dquot_dirty,
Jan Kara74f783a2008-08-19 14:51:22 +02001816 .write_info = dquot_commit_info,
1817 .alloc_dquot = dquot_alloc,
1818 .destroy_dquot = dquot_destroy,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819};
1820
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821/*
1822 * Turn quota off on a device. type == -1 ==> quotaoff for all types (umount)
1823 */
Jan Karaf55abc02008-08-20 17:50:32 +02001824int vfs_quota_disable(struct super_block *sb, int type, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825{
Jan Kara0ff5af82008-04-28 02:14:33 -07001826 int cnt, ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827 struct quota_info *dqopt = sb_dqopt(sb);
1828 struct inode *toputinode[MAXQUOTAS];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829
Jan Karaf55abc02008-08-20 17:50:32 +02001830 /* Cannot turn off usage accounting without turning off limits, or
1831 * suspend quotas and simultaneously turn quotas off. */
1832 if ((flags & DQUOT_USAGE_ENABLED && !(flags & DQUOT_LIMITS_ENABLED))
1833 || (flags & DQUOT_SUSPENDED && flags & (DQUOT_LIMITS_ENABLED |
1834 DQUOT_USAGE_ENABLED)))
1835 return -EINVAL;
1836
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837 /* We need to serialize quota_off() for device */
Ingo Molnard3be9152006-03-23 03:00:29 -08001838 mutex_lock(&dqopt->dqonoff_mutex);
Jan Kara9377abd2008-05-12 14:02:08 -07001839
1840 /*
1841 * Skip everything if there's nothing to do. We have to do this because
1842 * sometimes we are called when fill_super() failed and calling
1843 * sync_fs() in such cases does no good.
1844 */
Jan Karaf55abc02008-08-20 17:50:32 +02001845 if (!sb_any_quota_loaded(sb)) {
Jan Kara9377abd2008-05-12 14:02:08 -07001846 mutex_unlock(&dqopt->dqonoff_mutex);
1847 return 0;
1848 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1850 toputinode[cnt] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851 if (type != -1 && cnt != type)
1852 continue;
Jan Karaf55abc02008-08-20 17:50:32 +02001853 if (!sb_has_quota_loaded(sb, cnt))
Jan Kara0ff5af82008-04-28 02:14:33 -07001854 continue;
Jan Karaf55abc02008-08-20 17:50:32 +02001855
1856 if (flags & DQUOT_SUSPENDED) {
Jan Karacc334122009-01-12 17:23:05 +01001857 spin_lock(&dq_state_lock);
Jan Karaf55abc02008-08-20 17:50:32 +02001858 dqopt->flags |=
1859 dquot_state_flag(DQUOT_SUSPENDED, cnt);
Jan Karacc334122009-01-12 17:23:05 +01001860 spin_unlock(&dq_state_lock);
Jan Karaf55abc02008-08-20 17:50:32 +02001861 } else {
Jan Karacc334122009-01-12 17:23:05 +01001862 spin_lock(&dq_state_lock);
Jan Karaf55abc02008-08-20 17:50:32 +02001863 dqopt->flags &= ~dquot_state_flag(flags, cnt);
1864 /* Turning off suspended quotas? */
1865 if (!sb_has_quota_loaded(sb, cnt) &&
1866 sb_has_quota_suspended(sb, cnt)) {
1867 dqopt->flags &= ~dquot_state_flag(
1868 DQUOT_SUSPENDED, cnt);
Jan Karacc334122009-01-12 17:23:05 +01001869 spin_unlock(&dq_state_lock);
Jan Karaf55abc02008-08-20 17:50:32 +02001870 iput(dqopt->files[cnt]);
1871 dqopt->files[cnt] = NULL;
1872 continue;
1873 }
Jan Karacc334122009-01-12 17:23:05 +01001874 spin_unlock(&dq_state_lock);
Jan Kara0ff5af82008-04-28 02:14:33 -07001875 }
Jan Karaf55abc02008-08-20 17:50:32 +02001876
1877 /* We still have to keep quota loaded? */
1878 if (sb_has_quota_loaded(sb, cnt) && !(flags & DQUOT_SUSPENDED))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880
1881 /* Note: these are blocking operations */
1882 drop_dquot_ref(sb, cnt);
1883 invalidate_dquots(sb, cnt);
1884 /*
Jan Kara268157b2009-01-27 15:47:22 +01001885 * Now all dquots should be invalidated, all writes done so we
1886 * should be only users of the info. No locks needed.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887 */
1888 if (info_dirty(&dqopt->info[cnt]))
1889 sb->dq_op->write_info(sb, cnt);
1890 if (dqopt->ops[cnt]->free_file_info)
1891 dqopt->ops[cnt]->free_file_info(sb, cnt);
1892 put_quota_format(dqopt->info[cnt].dqi_format);
1893
1894 toputinode[cnt] = dqopt->files[cnt];
Jan Karaf55abc02008-08-20 17:50:32 +02001895 if (!sb_has_quota_loaded(sb, cnt))
Jan Kara0ff5af82008-04-28 02:14:33 -07001896 dqopt->files[cnt] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897 dqopt->info[cnt].dqi_flags = 0;
1898 dqopt->info[cnt].dqi_igrace = 0;
1899 dqopt->info[cnt].dqi_bgrace = 0;
1900 dqopt->ops[cnt] = NULL;
1901 }
Ingo Molnard3be9152006-03-23 03:00:29 -08001902 mutex_unlock(&dqopt->dqonoff_mutex);
Jan Karaca785ec2008-09-30 17:53:37 +02001903
1904 /* Skip syncing and setting flags if quota files are hidden */
1905 if (dqopt->flags & DQUOT_QUOTA_SYS_FILE)
1906 goto put_inodes;
1907
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908 /* Sync the superblock so that buffers with quota data are written to
Al Viro7b7b1ac2005-11-07 17:13:39 -05001909 * disk (and so userspace sees correct data afterwards). */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910 if (sb->s_op->sync_fs)
1911 sb->s_op->sync_fs(sb, 1);
1912 sync_blockdev(sb->s_bdev);
1913 /* Now the quota files are just ordinary files and we can set the
1914 * inode flags back. Moreover we discard the pagecache so that
1915 * userspace sees the writes we did bypassing the pagecache. We
1916 * must also discard the blockdev buffers so that we see the
1917 * changes done by userspace on the next quotaon() */
1918 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1919 if (toputinode[cnt]) {
Ingo Molnard3be9152006-03-23 03:00:29 -08001920 mutex_lock(&dqopt->dqonoff_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921 /* If quota was reenabled in the meantime, we have
1922 * nothing to do */
Jan Karaf55abc02008-08-20 17:50:32 +02001923 if (!sb_has_quota_loaded(sb, cnt)) {
Jan Kara268157b2009-01-27 15:47:22 +01001924 mutex_lock_nested(&toputinode[cnt]->i_mutex,
1925 I_MUTEX_QUOTA);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926 toputinode[cnt]->i_flags &= ~(S_IMMUTABLE |
1927 S_NOATIME | S_NOQUOTA);
Jan Kara268157b2009-01-27 15:47:22 +01001928 truncate_inode_pages(&toputinode[cnt]->i_data,
1929 0);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001930 mutex_unlock(&toputinode[cnt]->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931 mark_inode_dirty(toputinode[cnt]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932 }
Ingo Molnard3be9152006-03-23 03:00:29 -08001933 mutex_unlock(&dqopt->dqonoff_mutex);
Jan Karaca785ec2008-09-30 17:53:37 +02001934 }
1935 if (sb->s_bdev)
1936 invalidate_bdev(sb->s_bdev);
1937put_inodes:
1938 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1939 if (toputinode[cnt]) {
Jan Kara0ff5af82008-04-28 02:14:33 -07001940 /* On remount RO, we keep the inode pointer so that we
Jan Karaf55abc02008-08-20 17:50:32 +02001941 * can reenable quota on the subsequent remount RW. We
1942 * have to check 'flags' variable and not use sb_has_
1943 * function because another quotaon / quotaoff could
1944 * change global state before we got here. We refuse
1945 * to suspend quotas when there is pending delete on
1946 * the quota file... */
1947 if (!(flags & DQUOT_SUSPENDED))
Jan Kara0ff5af82008-04-28 02:14:33 -07001948 iput(toputinode[cnt]);
1949 else if (!toputinode[cnt]->i_nlink)
1950 ret = -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951 }
Jan Kara0ff5af82008-04-28 02:14:33 -07001952 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953}
Mingming Cao08d03502009-01-14 16:19:32 +01001954EXPORT_SYMBOL(vfs_quota_disable);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955
Jan Karaf55abc02008-08-20 17:50:32 +02001956int vfs_quota_off(struct super_block *sb, int type, int remount)
1957{
1958 return vfs_quota_disable(sb, type, remount ? DQUOT_SUSPENDED :
1959 (DQUOT_USAGE_ENABLED | DQUOT_LIMITS_ENABLED));
1960}
Mingming Cao08d03502009-01-14 16:19:32 +01001961EXPORT_SYMBOL(vfs_quota_off);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962/*
1963 * Turn quotas on on a device
1964 */
1965
Jan Karaf55abc02008-08-20 17:50:32 +02001966/*
1967 * Helper function to turn quotas on when we already have the inode of
1968 * quota file and no quota information is loaded.
1969 */
1970static int vfs_load_quota_inode(struct inode *inode, int type, int format_id,
1971 unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972{
1973 struct quota_format_type *fmt = find_quota_format(format_id);
1974 struct super_block *sb = inode->i_sb;
1975 struct quota_info *dqopt = sb_dqopt(sb);
1976 int error;
1977 int oldflags = -1;
1978
1979 if (!fmt)
1980 return -ESRCH;
1981 if (!S_ISREG(inode->i_mode)) {
1982 error = -EACCES;
1983 goto out_fmt;
1984 }
1985 if (IS_RDONLY(inode)) {
1986 error = -EROFS;
1987 goto out_fmt;
1988 }
1989 if (!sb->s_op->quota_write || !sb->s_op->quota_read) {
1990 error = -EINVAL;
1991 goto out_fmt;
1992 }
Jan Karaf55abc02008-08-20 17:50:32 +02001993 /* Usage always has to be set... */
1994 if (!(flags & DQUOT_USAGE_ENABLED)) {
1995 error = -EINVAL;
1996 goto out_fmt;
1997 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998
Jan Karaca785ec2008-09-30 17:53:37 +02001999 if (!(dqopt->flags & DQUOT_QUOTA_SYS_FILE)) {
2000 /* As we bypass the pagecache we must now flush the inode so
2001 * that we see all the changes from userspace... */
2002 write_inode_now(inode, 1);
2003 /* And now flush the block cache so that kernel sees the
2004 * changes */
2005 invalidate_bdev(sb->s_bdev);
2006 }
Ingo Molnard3be9152006-03-23 03:00:29 -08002007 mutex_lock(&dqopt->dqonoff_mutex);
Jan Karaf55abc02008-08-20 17:50:32 +02002008 if (sb_has_quota_loaded(sb, type)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009 error = -EBUSY;
2010 goto out_lock;
2011 }
Jan Karaca785ec2008-09-30 17:53:37 +02002012
2013 if (!(dqopt->flags & DQUOT_QUOTA_SYS_FILE)) {
2014 /* We don't want quota and atime on quota files (deadlocks
2015 * possible) Also nobody should write to the file - we use
2016 * special IO operations which ignore the immutable bit. */
Jan Karadee86562009-07-22 18:12:17 +02002017 mutex_lock_nested(&inode->i_mutex, I_MUTEX_QUOTA);
Jan Kara268157b2009-01-27 15:47:22 +01002018 oldflags = inode->i_flags & (S_NOATIME | S_IMMUTABLE |
2019 S_NOQUOTA);
Jan Karaca785ec2008-09-30 17:53:37 +02002020 inode->i_flags |= S_NOQUOTA | S_NOATIME | S_IMMUTABLE;
Jan Karadee86562009-07-22 18:12:17 +02002021 mutex_unlock(&inode->i_mutex);
Jan Kara26245c92010-01-06 17:20:35 +01002022 /*
2023 * When S_NOQUOTA is set, remove dquot references as no more
2024 * references can be added
2025 */
Jan Karaca785ec2008-09-30 17:53:37 +02002026 sb->dq_op->drop(inode);
2027 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028
2029 error = -EIO;
2030 dqopt->files[type] = igrab(inode);
2031 if (!dqopt->files[type])
2032 goto out_lock;
2033 error = -EINVAL;
2034 if (!fmt->qf_ops->check_quota_file(sb, type))
2035 goto out_file_init;
2036
2037 dqopt->ops[type] = fmt->qf_ops;
2038 dqopt->info[type].dqi_format = fmt;
Jan Kara0ff5af82008-04-28 02:14:33 -07002039 dqopt->info[type].dqi_fmt_id = format_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040 INIT_LIST_HEAD(&dqopt->info[type].dqi_dirty_list);
Ingo Molnard3be9152006-03-23 03:00:29 -08002041 mutex_lock(&dqopt->dqio_mutex);
Jan Kara268157b2009-01-27 15:47:22 +01002042 error = dqopt->ops[type]->read_file_info(sb, type);
2043 if (error < 0) {
Ingo Molnard3be9152006-03-23 03:00:29 -08002044 mutex_unlock(&dqopt->dqio_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045 goto out_file_init;
2046 }
Ingo Molnard3be9152006-03-23 03:00:29 -08002047 mutex_unlock(&dqopt->dqio_mutex);
Jan Karacc334122009-01-12 17:23:05 +01002048 spin_lock(&dq_state_lock);
Jan Karaf55abc02008-08-20 17:50:32 +02002049 dqopt->flags |= dquot_state_flag(flags, type);
Jan Karacc334122009-01-12 17:23:05 +01002050 spin_unlock(&dq_state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051
2052 add_dquot_ref(sb, type);
Ingo Molnard3be9152006-03-23 03:00:29 -08002053 mutex_unlock(&dqopt->dqonoff_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054
2055 return 0;
2056
2057out_file_init:
2058 dqopt->files[type] = NULL;
2059 iput(inode);
2060out_lock:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061 if (oldflags != -1) {
Jan Karadee86562009-07-22 18:12:17 +02002062 mutex_lock_nested(&inode->i_mutex, I_MUTEX_QUOTA);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063 /* Set the flags back (in the case of accidental quotaon()
2064 * on a wrong file we don't want to mess up the flags) */
2065 inode->i_flags &= ~(S_NOATIME | S_NOQUOTA | S_IMMUTABLE);
2066 inode->i_flags |= oldflags;
Jan Karadee86562009-07-22 18:12:17 +02002067 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068 }
Jiaying Zhangd01730d2009-07-07 18:15:21 +02002069 mutex_unlock(&dqopt->dqonoff_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070out_fmt:
2071 put_quota_format(fmt);
2072
2073 return error;
2074}
2075
Jan Kara0ff5af82008-04-28 02:14:33 -07002076/* Reenable quotas on remount RW */
2077static int vfs_quota_on_remount(struct super_block *sb, int type)
2078{
2079 struct quota_info *dqopt = sb_dqopt(sb);
2080 struct inode *inode;
2081 int ret;
Jan Karaf55abc02008-08-20 17:50:32 +02002082 unsigned int flags;
Jan Kara0ff5af82008-04-28 02:14:33 -07002083
2084 mutex_lock(&dqopt->dqonoff_mutex);
2085 if (!sb_has_quota_suspended(sb, type)) {
2086 mutex_unlock(&dqopt->dqonoff_mutex);
2087 return 0;
2088 }
Jan Kara0ff5af82008-04-28 02:14:33 -07002089 inode = dqopt->files[type];
2090 dqopt->files[type] = NULL;
Jan Karacc334122009-01-12 17:23:05 +01002091 spin_lock(&dq_state_lock);
Jan Karaf55abc02008-08-20 17:50:32 +02002092 flags = dqopt->flags & dquot_state_flag(DQUOT_USAGE_ENABLED |
2093 DQUOT_LIMITS_ENABLED, type);
2094 dqopt->flags &= ~dquot_state_flag(DQUOT_STATE_FLAGS, type);
Jan Karacc334122009-01-12 17:23:05 +01002095 spin_unlock(&dq_state_lock);
Jan Kara0ff5af82008-04-28 02:14:33 -07002096 mutex_unlock(&dqopt->dqonoff_mutex);
2097
Jan Karaf55abc02008-08-20 17:50:32 +02002098 flags = dquot_generic_flag(flags, type);
2099 ret = vfs_load_quota_inode(inode, type, dqopt->info[type].dqi_fmt_id,
2100 flags);
Jan Kara0ff5af82008-04-28 02:14:33 -07002101 iput(inode);
2102
2103 return ret;
2104}
2105
Al Viro77e69da2008-08-01 04:29:18 -04002106int vfs_quota_on_path(struct super_block *sb, int type, int format_id,
2107 struct path *path)
2108{
2109 int error = security_quota_on(path->dentry);
2110 if (error)
2111 return error;
2112 /* Quota file not on the same filesystem? */
2113 if (path->mnt->mnt_sb != sb)
2114 error = -EXDEV;
2115 else
Jan Karaf55abc02008-08-20 17:50:32 +02002116 error = vfs_load_quota_inode(path->dentry->d_inode, type,
2117 format_id, DQUOT_USAGE_ENABLED |
2118 DQUOT_LIMITS_ENABLED);
Al Viro77e69da2008-08-01 04:29:18 -04002119 return error;
2120}
Mingming Cao08d03502009-01-14 16:19:32 +01002121EXPORT_SYMBOL(vfs_quota_on_path);
Al Viro77e69da2008-08-01 04:29:18 -04002122
Al Viro82646132008-08-02 00:57:06 -04002123int vfs_quota_on(struct super_block *sb, int type, int format_id, char *name,
Jan Kara0ff5af82008-04-28 02:14:33 -07002124 int remount)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125{
Al Viro82646132008-08-02 00:57:06 -04002126 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127 int error;
2128
Jan Kara0ff5af82008-04-28 02:14:33 -07002129 if (remount)
2130 return vfs_quota_on_remount(sb, type);
2131
Al Viro82646132008-08-02 00:57:06 -04002132 error = kern_path(name, LOOKUP_FOLLOW, &path);
Al Viro77e69da2008-08-01 04:29:18 -04002133 if (!error) {
Al Viro82646132008-08-02 00:57:06 -04002134 error = vfs_quota_on_path(sb, type, format_id, &path);
2135 path_put(&path);
Al Viro77e69da2008-08-01 04:29:18 -04002136 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137 return error;
2138}
Mingming Cao08d03502009-01-14 16:19:32 +01002139EXPORT_SYMBOL(vfs_quota_on);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140
2141/*
Jan Karaf55abc02008-08-20 17:50:32 +02002142 * More powerful function for turning on quotas allowing setting
2143 * of individual quota flags
2144 */
2145int vfs_quota_enable(struct inode *inode, int type, int format_id,
2146 unsigned int flags)
2147{
2148 int ret = 0;
2149 struct super_block *sb = inode->i_sb;
2150 struct quota_info *dqopt = sb_dqopt(sb);
2151
2152 /* Just unsuspend quotas? */
2153 if (flags & DQUOT_SUSPENDED)
2154 return vfs_quota_on_remount(sb, type);
2155 if (!flags)
2156 return 0;
2157 /* Just updating flags needed? */
2158 if (sb_has_quota_loaded(sb, type)) {
2159 mutex_lock(&dqopt->dqonoff_mutex);
2160 /* Now do a reliable test... */
2161 if (!sb_has_quota_loaded(sb, type)) {
2162 mutex_unlock(&dqopt->dqonoff_mutex);
2163 goto load_quota;
2164 }
2165 if (flags & DQUOT_USAGE_ENABLED &&
2166 sb_has_quota_usage_enabled(sb, type)) {
2167 ret = -EBUSY;
2168 goto out_lock;
2169 }
2170 if (flags & DQUOT_LIMITS_ENABLED &&
2171 sb_has_quota_limits_enabled(sb, type)) {
2172 ret = -EBUSY;
2173 goto out_lock;
2174 }
Jan Karacc334122009-01-12 17:23:05 +01002175 spin_lock(&dq_state_lock);
Jan Karaf55abc02008-08-20 17:50:32 +02002176 sb_dqopt(sb)->flags |= dquot_state_flag(flags, type);
Jan Karacc334122009-01-12 17:23:05 +01002177 spin_unlock(&dq_state_lock);
Jan Karaf55abc02008-08-20 17:50:32 +02002178out_lock:
2179 mutex_unlock(&dqopt->dqonoff_mutex);
2180 return ret;
2181 }
2182
2183load_quota:
2184 return vfs_load_quota_inode(inode, type, format_id, flags);
2185}
Mingming Cao08d03502009-01-14 16:19:32 +01002186EXPORT_SYMBOL(vfs_quota_enable);
Jan Karaf55abc02008-08-20 17:50:32 +02002187
2188/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189 * This function is used when filesystem needs to initialize quotas
2190 * during mount time.
2191 */
Christoph Hellwig84de8562005-06-23 00:09:16 -07002192int vfs_quota_on_mount(struct super_block *sb, char *qf_name,
2193 int format_id, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194{
Christoph Hellwig84de8562005-06-23 00:09:16 -07002195 struct dentry *dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196 int error;
2197
Jan Karac56818d2009-11-12 15:42:08 +01002198 mutex_lock(&sb->s_root->d_inode->i_mutex);
Christoph Hellwig2fa389c2005-06-23 00:09:16 -07002199 dentry = lookup_one_len(qf_name, sb->s_root, strlen(qf_name));
Jan Karac56818d2009-11-12 15:42:08 +01002200 mutex_unlock(&sb->s_root->d_inode->i_mutex);
Christoph Hellwig84de8562005-06-23 00:09:16 -07002201 if (IS_ERR(dentry))
2202 return PTR_ERR(dentry);
2203
Jan Kara154f4842005-11-28 13:44:14 -08002204 if (!dentry->d_inode) {
2205 error = -ENOENT;
2206 goto out;
2207 }
2208
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209 error = security_quota_on(dentry);
Christoph Hellwig84de8562005-06-23 00:09:16 -07002210 if (!error)
Jan Karaf55abc02008-08-20 17:50:32 +02002211 error = vfs_load_quota_inode(dentry->d_inode, type, format_id,
2212 DQUOT_USAGE_ENABLED | DQUOT_LIMITS_ENABLED);
Christoph Hellwig84de8562005-06-23 00:09:16 -07002213
Jan Kara154f4842005-11-28 13:44:14 -08002214out:
Christoph Hellwig84de8562005-06-23 00:09:16 -07002215 dput(dentry);
2216 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217}
Mingming Cao08d03502009-01-14 16:19:32 +01002218EXPORT_SYMBOL(vfs_quota_on_mount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219
Jan Karab85f4b82008-07-25 01:46:50 -07002220/* Wrapper to turn on quotas when remounting rw */
2221int vfs_dq_quota_on_remount(struct super_block *sb)
2222{
2223 int cnt;
2224 int ret = 0, err;
2225
2226 if (!sb->s_qcop || !sb->s_qcop->quota_on)
2227 return -ENOSYS;
2228 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
2229 err = sb->s_qcop->quota_on(sb, cnt, 0, NULL, 1);
2230 if (err < 0 && !ret)
2231 ret = err;
2232 }
2233 return ret;
2234}
Mingming Cao08d03502009-01-14 16:19:32 +01002235EXPORT_SYMBOL(vfs_dq_quota_on_remount);
Jan Karab85f4b82008-07-25 01:46:50 -07002236
Jan Kara12095462008-08-20 14:45:12 +02002237static inline qsize_t qbtos(qsize_t blocks)
2238{
2239 return blocks << QIF_DQBLKSIZE_BITS;
2240}
2241
2242static inline qsize_t stoqb(qsize_t space)
2243{
2244 return (space + QIF_DQBLKSIZE - 1) >> QIF_DQBLKSIZE_BITS;
2245}
2246
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247/* Generic routine for getting common part of quota structure */
2248static void do_get_dqblk(struct dquot *dquot, struct if_dqblk *di)
2249{
2250 struct mem_dqblk *dm = &dquot->dq_dqb;
2251
2252 spin_lock(&dq_data_lock);
Jan Kara12095462008-08-20 14:45:12 +02002253 di->dqb_bhardlimit = stoqb(dm->dqb_bhardlimit);
2254 di->dqb_bsoftlimit = stoqb(dm->dqb_bsoftlimit);
Mingming Caof18df222009-01-13 16:43:09 +01002255 di->dqb_curspace = dm->dqb_curspace + dm->dqb_rsvspace;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256 di->dqb_ihardlimit = dm->dqb_ihardlimit;
2257 di->dqb_isoftlimit = dm->dqb_isoftlimit;
2258 di->dqb_curinodes = dm->dqb_curinodes;
2259 di->dqb_btime = dm->dqb_btime;
2260 di->dqb_itime = dm->dqb_itime;
2261 di->dqb_valid = QIF_ALL;
2262 spin_unlock(&dq_data_lock);
2263}
2264
Jan Kara268157b2009-01-27 15:47:22 +01002265int vfs_get_dqblk(struct super_block *sb, int type, qid_t id,
2266 struct if_dqblk *di)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267{
2268 struct dquot *dquot;
2269
Jan Karacc334122009-01-12 17:23:05 +01002270 dquot = dqget(sb, id, type);
Jan Karadd6f3c62009-01-26 16:01:43 +01002271 if (!dquot)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272 return -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273 do_get_dqblk(dquot, di);
2274 dqput(dquot);
Jan Karacc334122009-01-12 17:23:05 +01002275
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276 return 0;
2277}
Mingming Cao08d03502009-01-14 16:19:32 +01002278EXPORT_SYMBOL(vfs_get_dqblk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279
2280/* Generic routine for setting common part of quota structure */
Andrew Perepechko338bf9a2008-04-28 02:14:31 -07002281static int do_set_dqblk(struct dquot *dquot, struct if_dqblk *di)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282{
2283 struct mem_dqblk *dm = &dquot->dq_dqb;
2284 int check_blim = 0, check_ilim = 0;
Andrew Perepechko338bf9a2008-04-28 02:14:31 -07002285 struct mem_dqinfo *dqi = &sb_dqopt(dquot->dq_sb)->info[dquot->dq_type];
2286
2287 if ((di->dqb_valid & QIF_BLIMITS &&
2288 (di->dqb_bhardlimit > dqi->dqi_maxblimit ||
2289 di->dqb_bsoftlimit > dqi->dqi_maxblimit)) ||
2290 (di->dqb_valid & QIF_ILIMITS &&
2291 (di->dqb_ihardlimit > dqi->dqi_maxilimit ||
2292 di->dqb_isoftlimit > dqi->dqi_maxilimit)))
2293 return -ERANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294
2295 spin_lock(&dq_data_lock);
2296 if (di->dqb_valid & QIF_SPACE) {
Mingming Caof18df222009-01-13 16:43:09 +01002297 dm->dqb_curspace = di->dqb_curspace - dm->dqb_rsvspace;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298 check_blim = 1;
Jan Kara4d59bce2008-10-02 16:48:10 +02002299 __set_bit(DQ_LASTSET_B + QIF_SPACE_B, &dquot->dq_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300 }
2301 if (di->dqb_valid & QIF_BLIMITS) {
Jan Kara12095462008-08-20 14:45:12 +02002302 dm->dqb_bsoftlimit = qbtos(di->dqb_bsoftlimit);
2303 dm->dqb_bhardlimit = qbtos(di->dqb_bhardlimit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304 check_blim = 1;
Jan Kara4d59bce2008-10-02 16:48:10 +02002305 __set_bit(DQ_LASTSET_B + QIF_BLIMITS_B, &dquot->dq_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306 }
2307 if (di->dqb_valid & QIF_INODES) {
2308 dm->dqb_curinodes = di->dqb_curinodes;
2309 check_ilim = 1;
Jan Kara4d59bce2008-10-02 16:48:10 +02002310 __set_bit(DQ_LASTSET_B + QIF_INODES_B, &dquot->dq_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311 }
2312 if (di->dqb_valid & QIF_ILIMITS) {
2313 dm->dqb_isoftlimit = di->dqb_isoftlimit;
2314 dm->dqb_ihardlimit = di->dqb_ihardlimit;
2315 check_ilim = 1;
Jan Kara4d59bce2008-10-02 16:48:10 +02002316 __set_bit(DQ_LASTSET_B + QIF_ILIMITS_B, &dquot->dq_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317 }
Jan Kara4d59bce2008-10-02 16:48:10 +02002318 if (di->dqb_valid & QIF_BTIME) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319 dm->dqb_btime = di->dqb_btime;
Jan Karae04a88a92009-01-07 18:07:29 -08002320 check_blim = 1;
Jan Kara4d59bce2008-10-02 16:48:10 +02002321 __set_bit(DQ_LASTSET_B + QIF_BTIME_B, &dquot->dq_flags);
2322 }
2323 if (di->dqb_valid & QIF_ITIME) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324 dm->dqb_itime = di->dqb_itime;
Jan Karae04a88a92009-01-07 18:07:29 -08002325 check_ilim = 1;
Jan Kara4d59bce2008-10-02 16:48:10 +02002326 __set_bit(DQ_LASTSET_B + QIF_ITIME_B, &dquot->dq_flags);
2327 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328
2329 if (check_blim) {
Jan Kara268157b2009-01-27 15:47:22 +01002330 if (!dm->dqb_bsoftlimit ||
2331 dm->dqb_curspace < dm->dqb_bsoftlimit) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002332 dm->dqb_btime = 0;
2333 clear_bit(DQ_BLKS_B, &dquot->dq_flags);
Jan Kara268157b2009-01-27 15:47:22 +01002334 } else if (!(di->dqb_valid & QIF_BTIME))
2335 /* Set grace only if user hasn't provided his own... */
Andrew Perepechko338bf9a2008-04-28 02:14:31 -07002336 dm->dqb_btime = get_seconds() + dqi->dqi_bgrace;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337 }
2338 if (check_ilim) {
Jan Kara268157b2009-01-27 15:47:22 +01002339 if (!dm->dqb_isoftlimit ||
2340 dm->dqb_curinodes < dm->dqb_isoftlimit) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341 dm->dqb_itime = 0;
2342 clear_bit(DQ_INODES_B, &dquot->dq_flags);
Jan Kara268157b2009-01-27 15:47:22 +01002343 } else if (!(di->dqb_valid & QIF_ITIME))
2344 /* Set grace only if user hasn't provided his own... */
Andrew Perepechko338bf9a2008-04-28 02:14:31 -07002345 dm->dqb_itime = get_seconds() + dqi->dqi_igrace;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346 }
Jan Kara268157b2009-01-27 15:47:22 +01002347 if (dm->dqb_bhardlimit || dm->dqb_bsoftlimit || dm->dqb_ihardlimit ||
2348 dm->dqb_isoftlimit)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349 clear_bit(DQ_FAKE_B, &dquot->dq_flags);
2350 else
2351 set_bit(DQ_FAKE_B, &dquot->dq_flags);
2352 spin_unlock(&dq_data_lock);
2353 mark_dquot_dirty(dquot);
Andrew Perepechko338bf9a2008-04-28 02:14:31 -07002354
2355 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002356}
2357
Jan Kara268157b2009-01-27 15:47:22 +01002358int vfs_set_dqblk(struct super_block *sb, int type, qid_t id,
2359 struct if_dqblk *di)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360{
2361 struct dquot *dquot;
Andrew Perepechko338bf9a2008-04-28 02:14:31 -07002362 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363
Jan Karaf55abc02008-08-20 17:50:32 +02002364 dquot = dqget(sb, id, type);
2365 if (!dquot) {
2366 rc = -ESRCH;
2367 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002368 }
Andrew Perepechko338bf9a2008-04-28 02:14:31 -07002369 rc = do_set_dqblk(dquot, di);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002370 dqput(dquot);
Jan Karaf55abc02008-08-20 17:50:32 +02002371out:
Andrew Perepechko338bf9a2008-04-28 02:14:31 -07002372 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373}
Mingming Cao08d03502009-01-14 16:19:32 +01002374EXPORT_SYMBOL(vfs_set_dqblk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375
2376/* Generic routine for getting common part of quota file information */
2377int vfs_get_dqinfo(struct super_block *sb, int type, struct if_dqinfo *ii)
2378{
2379 struct mem_dqinfo *mi;
2380
Ingo Molnard3be9152006-03-23 03:00:29 -08002381 mutex_lock(&sb_dqopt(sb)->dqonoff_mutex);
Jan Karaf55abc02008-08-20 17:50:32 +02002382 if (!sb_has_quota_active(sb, type)) {
Ingo Molnard3be9152006-03-23 03:00:29 -08002383 mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002384 return -ESRCH;
2385 }
2386 mi = sb_dqopt(sb)->info + type;
2387 spin_lock(&dq_data_lock);
2388 ii->dqi_bgrace = mi->dqi_bgrace;
2389 ii->dqi_igrace = mi->dqi_igrace;
2390 ii->dqi_flags = mi->dqi_flags & DQF_MASK;
2391 ii->dqi_valid = IIF_ALL;
2392 spin_unlock(&dq_data_lock);
Ingo Molnard3be9152006-03-23 03:00:29 -08002393 mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394 return 0;
2395}
Mingming Cao08d03502009-01-14 16:19:32 +01002396EXPORT_SYMBOL(vfs_get_dqinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397
2398/* Generic routine for setting common part of quota file information */
2399int vfs_set_dqinfo(struct super_block *sb, int type, struct if_dqinfo *ii)
2400{
2401 struct mem_dqinfo *mi;
Jan Karaf55abc02008-08-20 17:50:32 +02002402 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002403
Ingo Molnard3be9152006-03-23 03:00:29 -08002404 mutex_lock(&sb_dqopt(sb)->dqonoff_mutex);
Jan Karaf55abc02008-08-20 17:50:32 +02002405 if (!sb_has_quota_active(sb, type)) {
2406 err = -ESRCH;
2407 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002408 }
2409 mi = sb_dqopt(sb)->info + type;
2410 spin_lock(&dq_data_lock);
2411 if (ii->dqi_valid & IIF_BGRACE)
2412 mi->dqi_bgrace = ii->dqi_bgrace;
2413 if (ii->dqi_valid & IIF_IGRACE)
2414 mi->dqi_igrace = ii->dqi_igrace;
2415 if (ii->dqi_valid & IIF_FLAGS)
Jan Kara268157b2009-01-27 15:47:22 +01002416 mi->dqi_flags = (mi->dqi_flags & ~DQF_MASK) |
2417 (ii->dqi_flags & DQF_MASK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002418 spin_unlock(&dq_data_lock);
2419 mark_info_dirty(sb, type);
2420 /* Force write to disk */
2421 sb->dq_op->write_info(sb, type);
Jan Karaf55abc02008-08-20 17:50:32 +02002422out:
Ingo Molnard3be9152006-03-23 03:00:29 -08002423 mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
Jan Karaf55abc02008-08-20 17:50:32 +02002424 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002425}
Mingming Cao08d03502009-01-14 16:19:32 +01002426EXPORT_SYMBOL(vfs_set_dqinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002427
Alexey Dobriyan0d54b212009-09-21 17:01:09 -07002428const struct quotactl_ops vfs_quotactl_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002429 .quota_on = vfs_quota_on,
2430 .quota_off = vfs_quota_off,
2431 .quota_sync = vfs_quota_sync,
2432 .get_info = vfs_get_dqinfo,
2433 .set_info = vfs_set_dqinfo,
2434 .get_dqblk = vfs_get_dqblk,
2435 .set_dqblk = vfs_set_dqblk
2436};
2437
2438static ctl_table fs_dqstats_table[] = {
2439 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002440 .procname = "lookups",
2441 .data = &dqstats.lookups,
2442 .maxlen = sizeof(int),
2443 .mode = 0444,
Eric W. Biederman6d456112009-11-16 03:11:48 -08002444 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002445 },
2446 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002447 .procname = "drops",
2448 .data = &dqstats.drops,
2449 .maxlen = sizeof(int),
2450 .mode = 0444,
Eric W. Biederman6d456112009-11-16 03:11:48 -08002451 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002452 },
2453 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454 .procname = "reads",
2455 .data = &dqstats.reads,
2456 .maxlen = sizeof(int),
2457 .mode = 0444,
Eric W. Biederman6d456112009-11-16 03:11:48 -08002458 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459 },
2460 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002461 .procname = "writes",
2462 .data = &dqstats.writes,
2463 .maxlen = sizeof(int),
2464 .mode = 0444,
Eric W. Biederman6d456112009-11-16 03:11:48 -08002465 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002466 },
2467 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468 .procname = "cache_hits",
2469 .data = &dqstats.cache_hits,
2470 .maxlen = sizeof(int),
2471 .mode = 0444,
Eric W. Biederman6d456112009-11-16 03:11:48 -08002472 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473 },
2474 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475 .procname = "allocated_dquots",
2476 .data = &dqstats.allocated_dquots,
2477 .maxlen = sizeof(int),
2478 .mode = 0444,
Eric W. Biederman6d456112009-11-16 03:11:48 -08002479 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002480 },
2481 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002482 .procname = "free_dquots",
2483 .data = &dqstats.free_dquots,
2484 .maxlen = sizeof(int),
2485 .mode = 0444,
Eric W. Biederman6d456112009-11-16 03:11:48 -08002486 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002487 },
2488 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002489 .procname = "syncs",
2490 .data = &dqstats.syncs,
2491 .maxlen = sizeof(int),
2492 .mode = 0444,
Eric W. Biederman6d456112009-11-16 03:11:48 -08002493 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002494 },
Jan Kara8e893462007-10-16 23:29:31 -07002495#ifdef CONFIG_PRINT_QUOTA_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07002496 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002497 .procname = "warnings",
2498 .data = &flag_print_warnings,
2499 .maxlen = sizeof(int),
2500 .mode = 0644,
Eric W. Biederman6d456112009-11-16 03:11:48 -08002501 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002502 },
Jan Kara8e893462007-10-16 23:29:31 -07002503#endif
Eric W. Biedermanab092032009-11-05 14:25:10 -08002504 { },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002505};
2506
2507static ctl_table fs_table[] = {
2508 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002509 .procname = "quota",
2510 .mode = 0555,
2511 .child = fs_dqstats_table,
2512 },
Eric W. Biedermanab092032009-11-05 14:25:10 -08002513 { },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002514};
2515
2516static ctl_table sys_table[] = {
2517 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002518 .procname = "fs",
2519 .mode = 0555,
2520 .child = fs_table,
2521 },
Eric W. Biedermanab092032009-11-05 14:25:10 -08002522 { },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002523};
2524
2525static int __init dquot_init(void)
2526{
2527 int i;
2528 unsigned long nr_hash, order;
2529
2530 printk(KERN_NOTICE "VFS: Disk quotas %s\n", __DQUOT_VERSION__);
2531
Eric W. Biederman0b4d4142007-02-14 00:34:09 -08002532 register_sysctl_table(sys_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002533
Paul Mundt20c2df82007-07-20 10:11:58 +09002534 dquot_cachep = kmem_cache_create("dquot",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002535 sizeof(struct dquot), sizeof(unsigned long) * 4,
Paul Jacksonfffb60f2006-03-24 03:16:06 -08002536 (SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT|
2537 SLAB_MEM_SPREAD|SLAB_PANIC),
Paul Mundt20c2df82007-07-20 10:11:58 +09002538 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002539
2540 order = 0;
2541 dquot_hash = (struct hlist_head *)__get_free_pages(GFP_ATOMIC, order);
2542 if (!dquot_hash)
2543 panic("Cannot create dquot hash table");
2544
2545 /* Find power-of-two hlist_heads which can fit into allocation */
2546 nr_hash = (1UL << order) * PAGE_SIZE / sizeof(struct hlist_head);
2547 dq_hash_bits = 0;
2548 do {
2549 dq_hash_bits++;
2550 } while (nr_hash >> dq_hash_bits);
2551 dq_hash_bits--;
2552
2553 nr_hash = 1UL << dq_hash_bits;
2554 dq_hash_mask = nr_hash - 1;
2555 for (i = 0; i < nr_hash; i++)
2556 INIT_HLIST_HEAD(dquot_hash + i);
2557
2558 printk("Dquot-cache hash table entries: %ld (order %ld, %ld bytes)\n",
2559 nr_hash, order, (PAGE_SIZE << order));
2560
Rusty Russell8e1f9362007-07-17 04:03:17 -07002561 register_shrinker(&dqcache_shrinker);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002562
Linus Torvalds1da177e2005-04-16 15:20:36 -07002563 return 0;
2564}
2565module_init(dquot_init);