blob: a1bd5eabbe50d8d81a6a408f622bf6c7d27f3684 [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.. */
Jan Kara8e893462007-10-16 23:29:31 -070080#ifdef CONFIG_QUOTA_NETLINK_INTERFACE
81#include <net/netlink.h>
82#include <net/genetlink.h>
83#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070084
85#include <asm/uaccess.h>
86
87#define __DQUOT_PARANOIA
88
89/*
Jan Karacc334122009-01-12 17:23:05 +010090 * There are three quota SMP locks. dq_list_lock protects all lists with quotas
91 * and quota formats, dqstats structure containing statistics about the lists
92 * dq_data_lock protects data from dq_dqb and also mem_dqinfo structures and
93 * also guards consistency of dquot->dq_dqb with inode->i_blocks, i_bytes.
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 * i_blocks and i_bytes updates itself are guarded by i_lock acquired directly
Jan Karacc334122009-01-12 17:23:05 +010095 * in inode_add_bytes() and inode_sub_bytes(). dq_state_lock protects
96 * modifications of quota state (on quotaon and quotaoff) and readers who care
97 * about latest values take it as well.
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 *
Jan Karacc334122009-01-12 17:23:05 +010099 * The spinlock ordering is hence: dq_data_lock > dq_list_lock > i_lock,
100 * dq_list_lock > dq_state_lock
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 *
102 * Note that some things (eg. sb pointer, type, id) doesn't change during
103 * the life of the dquot structure and so needn't to be protected by a lock
104 *
105 * Any operation working on dquots via inode pointers must hold dqptr_sem. If
106 * operation is just reading pointers from inode (or not using them at all) the
107 * read lock is enough. If pointers are altered function must hold write lock
108 * (these locking rules also apply for S_NOQUOTA flag in the inode - note that
Jan Karacc334122009-01-12 17:23:05 +0100109 * for altering the flag i_mutex is also needed).
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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232static inline unsigned int
233hashfn(const struct super_block *sb, unsigned int id, int type)
234{
235 unsigned long tmp;
236
237 tmp = (((unsigned long)sb>>L1_CACHE_SHIFT) ^ id) * (MAXQUOTAS - type);
238 return (tmp + (tmp >> dq_hash_bits)) & dq_hash_mask;
239}
240
241/*
242 * Following list functions expect dq_list_lock to be held
243 */
244static inline void insert_dquot_hash(struct dquot *dquot)
245{
Jan Kara268157b2009-01-27 15:47:22 +0100246 struct hlist_head *head;
247 head = dquot_hash + hashfn(dquot->dq_sb, dquot->dq_id, dquot->dq_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 hlist_add_head(&dquot->dq_hash, head);
249}
250
251static inline void remove_dquot_hash(struct dquot *dquot)
252{
253 hlist_del_init(&dquot->dq_hash);
254}
255
Jan Kara7a2435d2009-01-27 01:47:11 +0100256static struct dquot *find_dquot(unsigned int hashent, struct super_block *sb,
257 unsigned int id, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258{
259 struct hlist_node *node;
260 struct dquot *dquot;
261
262 hlist_for_each (node, dquot_hash+hashent) {
263 dquot = hlist_entry(node, struct dquot, dq_hash);
Jan Kara268157b2009-01-27 15:47:22 +0100264 if (dquot->dq_sb == sb && dquot->dq_id == id &&
265 dquot->dq_type == type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 return dquot;
267 }
Jan Karadd6f3c62009-01-26 16:01:43 +0100268 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269}
270
271/* Add a dquot to the tail of the free list */
272static inline void put_dquot_last(struct dquot *dquot)
273{
Akinobu Mita8e130592006-06-26 00:24:37 -0700274 list_add_tail(&dquot->dq_free, &free_dquots);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 dqstats.free_dquots++;
276}
277
278static inline void remove_free_dquot(struct dquot *dquot)
279{
280 if (list_empty(&dquot->dq_free))
281 return;
282 list_del_init(&dquot->dq_free);
283 dqstats.free_dquots--;
284}
285
286static inline void put_inuse(struct dquot *dquot)
287{
288 /* We add to the back of inuse list so we don't have to restart
289 * when traversing this list and we block */
Akinobu Mita8e130592006-06-26 00:24:37 -0700290 list_add_tail(&dquot->dq_inuse, &inuse_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 dqstats.allocated_dquots++;
292}
293
294static inline void remove_inuse(struct dquot *dquot)
295{
296 dqstats.allocated_dquots--;
297 list_del(&dquot->dq_inuse);
298}
299/*
300 * End of list functions needing dq_list_lock
301 */
302
303static void wait_on_dquot(struct dquot *dquot)
304{
Ingo Molnard3be9152006-03-23 03:00:29 -0800305 mutex_lock(&dquot->dq_lock);
306 mutex_unlock(&dquot->dq_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307}
308
Jan Kara03f6e922008-04-28 02:14:32 -0700309static inline int dquot_dirty(struct dquot *dquot)
310{
311 return test_bit(DQ_MOD_B, &dquot->dq_flags);
312}
313
314static inline int mark_dquot_dirty(struct dquot *dquot)
315{
316 return dquot->dq_sb->dq_op->mark_dirty(dquot);
317}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318
319int dquot_mark_dquot_dirty(struct dquot *dquot)
320{
321 spin_lock(&dq_list_lock);
322 if (!test_and_set_bit(DQ_MOD_B, &dquot->dq_flags))
323 list_add(&dquot->dq_dirty, &sb_dqopt(dquot->dq_sb)->
324 info[dquot->dq_type].dqi_dirty_list);
325 spin_unlock(&dq_list_lock);
326 return 0;
327}
Mingming Cao08d03502009-01-14 16:19:32 +0100328EXPORT_SYMBOL(dquot_mark_dquot_dirty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329
330/* This function needs dq_list_lock */
331static inline int clear_dquot_dirty(struct dquot *dquot)
332{
333 if (!test_and_clear_bit(DQ_MOD_B, &dquot->dq_flags))
334 return 0;
335 list_del_init(&dquot->dq_dirty);
336 return 1;
337}
338
339void mark_info_dirty(struct super_block *sb, int type)
340{
341 set_bit(DQF_INFO_DIRTY_B, &sb_dqopt(sb)->info[type].dqi_flags);
342}
343EXPORT_SYMBOL(mark_info_dirty);
344
345/*
346 * Read dquot from disk and alloc space for it
347 */
348
349int dquot_acquire(struct dquot *dquot)
350{
351 int ret = 0, ret2 = 0;
352 struct quota_info *dqopt = sb_dqopt(dquot->dq_sb);
353
Ingo Molnard3be9152006-03-23 03:00:29 -0800354 mutex_lock(&dquot->dq_lock);
355 mutex_lock(&dqopt->dqio_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 if (!test_bit(DQ_READ_B, &dquot->dq_flags))
357 ret = dqopt->ops[dquot->dq_type]->read_dqblk(dquot);
358 if (ret < 0)
359 goto out_iolock;
360 set_bit(DQ_READ_B, &dquot->dq_flags);
361 /* Instantiate dquot if needed */
362 if (!test_bit(DQ_ACTIVE_B, &dquot->dq_flags) && !dquot->dq_off) {
363 ret = dqopt->ops[dquot->dq_type]->commit_dqblk(dquot);
364 /* Write the info if needed */
Jan Kara268157b2009-01-27 15:47:22 +0100365 if (info_dirty(&dqopt->info[dquot->dq_type])) {
366 ret2 = dqopt->ops[dquot->dq_type]->write_file_info(
367 dquot->dq_sb, dquot->dq_type);
368 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 if (ret < 0)
370 goto out_iolock;
371 if (ret2 < 0) {
372 ret = ret2;
373 goto out_iolock;
374 }
375 }
376 set_bit(DQ_ACTIVE_B, &dquot->dq_flags);
377out_iolock:
Ingo Molnard3be9152006-03-23 03:00:29 -0800378 mutex_unlock(&dqopt->dqio_mutex);
379 mutex_unlock(&dquot->dq_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 return ret;
381}
Mingming Cao08d03502009-01-14 16:19:32 +0100382EXPORT_SYMBOL(dquot_acquire);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383
384/*
385 * Write dquot to disk
386 */
387int dquot_commit(struct dquot *dquot)
388{
389 int ret = 0, ret2 = 0;
390 struct quota_info *dqopt = sb_dqopt(dquot->dq_sb);
391
Ingo Molnard3be9152006-03-23 03:00:29 -0800392 mutex_lock(&dqopt->dqio_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 spin_lock(&dq_list_lock);
394 if (!clear_dquot_dirty(dquot)) {
395 spin_unlock(&dq_list_lock);
396 goto out_sem;
397 }
398 spin_unlock(&dq_list_lock);
399 /* Inactive dquot can be only if there was error during read/init
400 * => we have better not writing it */
401 if (test_bit(DQ_ACTIVE_B, &dquot->dq_flags)) {
402 ret = dqopt->ops[dquot->dq_type]->commit_dqblk(dquot);
Jan Kara268157b2009-01-27 15:47:22 +0100403 if (info_dirty(&dqopt->info[dquot->dq_type])) {
404 ret2 = dqopt->ops[dquot->dq_type]->write_file_info(
405 dquot->dq_sb, dquot->dq_type);
406 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 if (ret >= 0)
408 ret = ret2;
409 }
410out_sem:
Ingo Molnard3be9152006-03-23 03:00:29 -0800411 mutex_unlock(&dqopt->dqio_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 return ret;
413}
Mingming Cao08d03502009-01-14 16:19:32 +0100414EXPORT_SYMBOL(dquot_commit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415
416/*
417 * Release dquot
418 */
419int dquot_release(struct dquot *dquot)
420{
421 int ret = 0, ret2 = 0;
422 struct quota_info *dqopt = sb_dqopt(dquot->dq_sb);
423
Ingo Molnard3be9152006-03-23 03:00:29 -0800424 mutex_lock(&dquot->dq_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 /* Check whether we are not racing with some other dqget() */
426 if (atomic_read(&dquot->dq_count) > 1)
427 goto out_dqlock;
Ingo Molnard3be9152006-03-23 03:00:29 -0800428 mutex_lock(&dqopt->dqio_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 if (dqopt->ops[dquot->dq_type]->release_dqblk) {
430 ret = dqopt->ops[dquot->dq_type]->release_dqblk(dquot);
431 /* Write the info */
Jan Kara268157b2009-01-27 15:47:22 +0100432 if (info_dirty(&dqopt->info[dquot->dq_type])) {
433 ret2 = dqopt->ops[dquot->dq_type]->write_file_info(
434 dquot->dq_sb, dquot->dq_type);
435 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 if (ret >= 0)
437 ret = ret2;
438 }
439 clear_bit(DQ_ACTIVE_B, &dquot->dq_flags);
Ingo Molnard3be9152006-03-23 03:00:29 -0800440 mutex_unlock(&dqopt->dqio_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441out_dqlock:
Ingo Molnard3be9152006-03-23 03:00:29 -0800442 mutex_unlock(&dquot->dq_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 return ret;
444}
Mingming Cao08d03502009-01-14 16:19:32 +0100445EXPORT_SYMBOL(dquot_release);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446
Jan Kara7d9056b2008-11-25 15:31:32 +0100447void dquot_destroy(struct dquot *dquot)
Jan Kara74f783a2008-08-19 14:51:22 +0200448{
449 kmem_cache_free(dquot_cachep, dquot);
450}
Jan Kara7d9056b2008-11-25 15:31:32 +0100451EXPORT_SYMBOL(dquot_destroy);
Jan Kara74f783a2008-08-19 14:51:22 +0200452
453static inline void do_destroy_dquot(struct dquot *dquot)
454{
455 dquot->dq_sb->dq_op->destroy_dquot(dquot);
456}
457
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458/* Invalidate all dquots on the list. Note that this function is called after
459 * quota is disabled and pointers from inodes removed so there cannot be new
Jan Kara6362e4d2006-03-23 03:00:17 -0800460 * quota users. There can still be some users of quotas due to inodes being
461 * just deleted or pruned by prune_icache() (those are not attached to any
Jan Karacc334122009-01-12 17:23:05 +0100462 * list) or parallel quotactl call. We have to wait for such users.
Jan Kara6362e4d2006-03-23 03:00:17 -0800463 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464static void invalidate_dquots(struct super_block *sb, int type)
465{
Domen Puncerc33ed272005-06-25 14:59:36 -0700466 struct dquot *dquot, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467
Jan Kara6362e4d2006-03-23 03:00:17 -0800468restart:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 spin_lock(&dq_list_lock);
Domen Puncerc33ed272005-06-25 14:59:36 -0700470 list_for_each_entry_safe(dquot, tmp, &inuse_list, dq_inuse) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 if (dquot->dq_sb != sb)
472 continue;
473 if (dquot->dq_type != type)
474 continue;
Jan Kara6362e4d2006-03-23 03:00:17 -0800475 /* Wait for dquot users */
476 if (atomic_read(&dquot->dq_count)) {
477 DEFINE_WAIT(wait);
478
479 atomic_inc(&dquot->dq_count);
480 prepare_to_wait(&dquot->dq_wait_unused, &wait,
481 TASK_UNINTERRUPTIBLE);
482 spin_unlock(&dq_list_lock);
483 /* Once dqput() wakes us up, we know it's time to free
484 * the dquot.
485 * IMPORTANT: we rely on the fact that there is always
486 * at most one process waiting for dquot to free.
487 * Otherwise dq_count would be > 1 and we would never
488 * wake up.
489 */
490 if (atomic_read(&dquot->dq_count) > 1)
491 schedule();
492 finish_wait(&dquot->dq_wait_unused, &wait);
493 dqput(dquot);
494 /* At this moment dquot() need not exist (it could be
495 * reclaimed by prune_dqcache(). Hence we must
496 * restart. */
497 goto restart;
498 }
499 /*
500 * Quota now has no users and it has been written on last
501 * dqput()
502 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 remove_dquot_hash(dquot);
504 remove_free_dquot(dquot);
505 remove_inuse(dquot);
Jan Kara74f783a2008-08-19 14:51:22 +0200506 do_destroy_dquot(dquot);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 }
508 spin_unlock(&dq_list_lock);
509}
510
Jan Kara12c77522008-10-20 17:05:00 +0200511/* Call callback for every active dquot on given filesystem */
512int dquot_scan_active(struct super_block *sb,
513 int (*fn)(struct dquot *dquot, unsigned long priv),
514 unsigned long priv)
515{
516 struct dquot *dquot, *old_dquot = NULL;
517 int ret = 0;
518
519 mutex_lock(&sb_dqopt(sb)->dqonoff_mutex);
520 spin_lock(&dq_list_lock);
521 list_for_each_entry(dquot, &inuse_list, dq_inuse) {
522 if (!test_bit(DQ_ACTIVE_B, &dquot->dq_flags))
523 continue;
524 if (dquot->dq_sb != sb)
525 continue;
526 /* Now we have active dquot so we can just increase use count */
527 atomic_inc(&dquot->dq_count);
528 dqstats.lookups++;
529 spin_unlock(&dq_list_lock);
530 dqput(old_dquot);
531 old_dquot = dquot;
532 ret = fn(dquot, priv);
533 if (ret < 0)
534 goto out;
535 spin_lock(&dq_list_lock);
536 /* We are safe to continue now because our dquot could not
537 * be moved out of the inuse list while we hold the reference */
538 }
539 spin_unlock(&dq_list_lock);
540out:
541 dqput(old_dquot);
542 mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
543 return ret;
544}
Mingming Cao08d03502009-01-14 16:19:32 +0100545EXPORT_SYMBOL(dquot_scan_active);
Jan Kara12c77522008-10-20 17:05:00 +0200546
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547int vfs_quota_sync(struct super_block *sb, int type)
548{
549 struct list_head *dirty;
550 struct dquot *dquot;
551 struct quota_info *dqopt = sb_dqopt(sb);
552 int cnt;
553
Ingo Molnard3be9152006-03-23 03:00:29 -0800554 mutex_lock(&dqopt->dqonoff_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
556 if (type != -1 && cnt != type)
557 continue;
Jan Karaf55abc02008-08-20 17:50:32 +0200558 if (!sb_has_quota_active(sb, cnt))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 continue;
560 spin_lock(&dq_list_lock);
561 dirty = &dqopt->info[cnt].dqi_dirty_list;
562 while (!list_empty(dirty)) {
Jan Kara268157b2009-01-27 15:47:22 +0100563 dquot = list_first_entry(dirty, struct dquot,
564 dq_dirty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 /* Dirty and inactive can be only bad dquot... */
566 if (!test_bit(DQ_ACTIVE_B, &dquot->dq_flags)) {
567 clear_dquot_dirty(dquot);
568 continue;
569 }
570 /* Now we have active dquot from which someone is
571 * holding reference so we can safely just increase
572 * use count */
573 atomic_inc(&dquot->dq_count);
574 dqstats.lookups++;
575 spin_unlock(&dq_list_lock);
576 sb->dq_op->write_dquot(dquot);
577 dqput(dquot);
578 spin_lock(&dq_list_lock);
579 }
580 spin_unlock(&dq_list_lock);
581 }
582
583 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
Jan Karaf55abc02008-08-20 17:50:32 +0200584 if ((cnt == type || type == -1) && sb_has_quota_active(sb, cnt)
585 && info_dirty(&dqopt->info[cnt]))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 sb->dq_op->write_info(sb, cnt);
587 spin_lock(&dq_list_lock);
588 dqstats.syncs++;
589 spin_unlock(&dq_list_lock);
Ingo Molnard3be9152006-03-23 03:00:29 -0800590 mutex_unlock(&dqopt->dqonoff_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591
592 return 0;
593}
Mingming Cao08d03502009-01-14 16:19:32 +0100594EXPORT_SYMBOL(vfs_quota_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595
596/* Free unused dquots from cache */
597static void prune_dqcache(int count)
598{
599 struct list_head *head;
600 struct dquot *dquot;
601
602 head = free_dquots.prev;
603 while (head != &free_dquots && count) {
604 dquot = list_entry(head, struct dquot, dq_free);
605 remove_dquot_hash(dquot);
606 remove_free_dquot(dquot);
607 remove_inuse(dquot);
Jan Kara74f783a2008-08-19 14:51:22 +0200608 do_destroy_dquot(dquot);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 count--;
610 head = free_dquots.prev;
611 }
612}
613
614/*
615 * This is called from kswapd when we think we need some
616 * more memory
617 */
618
Al Viro27496a82005-10-21 03:20:48 -0400619static int shrink_dqcache_memory(int nr, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620{
621 if (nr) {
622 spin_lock(&dq_list_lock);
623 prune_dqcache(nr);
624 spin_unlock(&dq_list_lock);
625 }
626 return (dqstats.free_dquots / 100) * sysctl_vfs_cache_pressure;
627}
628
Rusty Russell8e1f9362007-07-17 04:03:17 -0700629static struct shrinker dqcache_shrinker = {
630 .shrink = shrink_dqcache_memory,
631 .seeks = DEFAULT_SEEKS,
632};
633
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634/*
635 * Put reference to dquot
636 * NOTE: If you change this function please check whether dqput_blocks() works right...
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 */
Jan Kara3d9ea252008-10-10 16:12:23 +0200638void dqput(struct dquot *dquot)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639{
Jan Karab48d3802008-07-25 01:46:49 -0700640 int ret;
641
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 if (!dquot)
643 return;
644#ifdef __DQUOT_PARANOIA
645 if (!atomic_read(&dquot->dq_count)) {
646 printk("VFS: dqput: trying to free free dquot\n");
647 printk("VFS: device %s, dquot of %s %d\n",
648 dquot->dq_sb->s_id,
649 quotatypes[dquot->dq_type],
650 dquot->dq_id);
651 BUG();
652 }
653#endif
654
655 spin_lock(&dq_list_lock);
656 dqstats.drops++;
657 spin_unlock(&dq_list_lock);
658we_slept:
659 spin_lock(&dq_list_lock);
660 if (atomic_read(&dquot->dq_count) > 1) {
661 /* We have more than one user... nothing to do */
662 atomic_dec(&dquot->dq_count);
Jan Kara6362e4d2006-03-23 03:00:17 -0800663 /* Releasing dquot during quotaoff phase? */
Jan Karaf55abc02008-08-20 17:50:32 +0200664 if (!sb_has_quota_active(dquot->dq_sb, dquot->dq_type) &&
Jan Kara6362e4d2006-03-23 03:00:17 -0800665 atomic_read(&dquot->dq_count) == 1)
666 wake_up(&dquot->dq_wait_unused);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 spin_unlock(&dq_list_lock);
668 return;
669 }
670 /* Need to release dquot? */
671 if (test_bit(DQ_ACTIVE_B, &dquot->dq_flags) && dquot_dirty(dquot)) {
672 spin_unlock(&dq_list_lock);
673 /* Commit dquot before releasing */
Jan Karab48d3802008-07-25 01:46:49 -0700674 ret = dquot->dq_sb->dq_op->write_dquot(dquot);
675 if (ret < 0) {
676 printk(KERN_ERR "VFS: cannot write quota structure on "
677 "device %s (error %d). Quota may get out of "
678 "sync!\n", dquot->dq_sb->s_id, ret);
679 /*
680 * We clear dirty bit anyway, so that we avoid
681 * infinite loop here
682 */
683 spin_lock(&dq_list_lock);
684 clear_dquot_dirty(dquot);
685 spin_unlock(&dq_list_lock);
686 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 goto we_slept;
688 }
689 /* Clear flag in case dquot was inactive (something bad happened) */
690 clear_dquot_dirty(dquot);
691 if (test_bit(DQ_ACTIVE_B, &dquot->dq_flags)) {
692 spin_unlock(&dq_list_lock);
693 dquot->dq_sb->dq_op->release_dquot(dquot);
694 goto we_slept;
695 }
696 atomic_dec(&dquot->dq_count);
697#ifdef __DQUOT_PARANOIA
698 /* sanity check */
Eric Sesterhenn8abf6a42006-04-02 13:36:13 +0200699 BUG_ON(!list_empty(&dquot->dq_free));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700#endif
701 put_dquot_last(dquot);
702 spin_unlock(&dq_list_lock);
703}
Mingming Cao08d03502009-01-14 16:19:32 +0100704EXPORT_SYMBOL(dqput);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705
Jan Kara7d9056b2008-11-25 15:31:32 +0100706struct dquot *dquot_alloc(struct super_block *sb, int type)
Jan Kara74f783a2008-08-19 14:51:22 +0200707{
708 return kmem_cache_zalloc(dquot_cachep, GFP_NOFS);
709}
Jan Kara7d9056b2008-11-25 15:31:32 +0100710EXPORT_SYMBOL(dquot_alloc);
Jan Kara74f783a2008-08-19 14:51:22 +0200711
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712static struct dquot *get_empty_dquot(struct super_block *sb, int type)
713{
714 struct dquot *dquot;
715
Jan Kara74f783a2008-08-19 14:51:22 +0200716 dquot = sb->dq_op->alloc_dquot(sb, type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 if(!dquot)
Jan Karadd6f3c62009-01-26 16:01:43 +0100718 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719
Ingo Molnard3be9152006-03-23 03:00:29 -0800720 mutex_init(&dquot->dq_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 INIT_LIST_HEAD(&dquot->dq_free);
722 INIT_LIST_HEAD(&dquot->dq_inuse);
723 INIT_HLIST_NODE(&dquot->dq_hash);
724 INIT_LIST_HEAD(&dquot->dq_dirty);
Jan Kara6362e4d2006-03-23 03:00:17 -0800725 init_waitqueue_head(&dquot->dq_wait_unused);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 dquot->dq_sb = sb;
727 dquot->dq_type = type;
728 atomic_set(&dquot->dq_count, 1);
729
730 return dquot;
731}
732
733/*
734 * Get reference to dquot
Jan Karacc334122009-01-12 17:23:05 +0100735 *
736 * Locking is slightly tricky here. We are guarded from parallel quotaoff()
737 * destroying our dquot by:
738 * a) checking for quota flags under dq_list_lock and
739 * b) getting a reference to dquot before we release dq_list_lock
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 */
Jan Kara3d9ea252008-10-10 16:12:23 +0200741struct dquot *dqget(struct super_block *sb, unsigned int id, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742{
743 unsigned int hashent = hashfn(sb, id, type);
Jan Karadd6f3c62009-01-26 16:01:43 +0100744 struct dquot *dquot = NULL, *empty = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745
Jan Karaf55abc02008-08-20 17:50:32 +0200746 if (!sb_has_quota_active(sb, type))
Jan Karadd6f3c62009-01-26 16:01:43 +0100747 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748we_slept:
749 spin_lock(&dq_list_lock);
Jan Karacc334122009-01-12 17:23:05 +0100750 spin_lock(&dq_state_lock);
751 if (!sb_has_quota_active(sb, type)) {
752 spin_unlock(&dq_state_lock);
753 spin_unlock(&dq_list_lock);
754 goto out;
755 }
756 spin_unlock(&dq_state_lock);
757
Jan Karadd6f3c62009-01-26 16:01:43 +0100758 dquot = find_dquot(hashent, sb, id, type);
759 if (!dquot) {
760 if (!empty) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 spin_unlock(&dq_list_lock);
Jan Karadd6f3c62009-01-26 16:01:43 +0100762 empty = get_empty_dquot(sb, type);
763 if (!empty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 schedule(); /* Try to wait for a moment... */
765 goto we_slept;
766 }
767 dquot = empty;
Jan Karadd6f3c62009-01-26 16:01:43 +0100768 empty = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 dquot->dq_id = id;
770 /* all dquots go on the inuse_list */
771 put_inuse(dquot);
772 /* hash it first so it can be found */
773 insert_dquot_hash(dquot);
774 dqstats.lookups++;
775 spin_unlock(&dq_list_lock);
776 } else {
777 if (!atomic_read(&dquot->dq_count))
778 remove_free_dquot(dquot);
779 atomic_inc(&dquot->dq_count);
780 dqstats.cache_hits++;
781 dqstats.lookups++;
782 spin_unlock(&dq_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 }
Jan Kara268157b2009-01-27 15:47:22 +0100784 /* Wait for dq_lock - after this we know that either dquot_release() is
785 * already finished or it will be canceled due to dq_count > 1 test */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 wait_on_dquot(dquot);
Jan Kara268157b2009-01-27 15:47:22 +0100787 /* Read the dquot / allocate space in quota file */
788 if (!test_bit(DQ_ACTIVE_B, &dquot->dq_flags) &&
789 sb->dq_op->acquire_dquot(dquot) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 dqput(dquot);
Jan Karadd6f3c62009-01-26 16:01:43 +0100791 dquot = NULL;
Jan Karacc334122009-01-12 17:23:05 +0100792 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 }
794#ifdef __DQUOT_PARANOIA
Eric Sesterhenn8abf6a42006-04-02 13:36:13 +0200795 BUG_ON(!dquot->dq_sb); /* Has somebody invalidated entry under us? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796#endif
Jan Karacc334122009-01-12 17:23:05 +0100797out:
798 if (empty)
799 do_destroy_dquot(empty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800
801 return dquot;
802}
Mingming Cao08d03502009-01-14 16:19:32 +0100803EXPORT_SYMBOL(dqget);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804
805static int dqinit_needed(struct inode *inode, int type)
806{
807 int cnt;
808
809 if (IS_NOQUOTA(inode))
810 return 0;
811 if (type != -1)
Jan Karadd6f3c62009-01-26 16:01:43 +0100812 return !inode->i_dquot[type];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
Jan Karadd6f3c62009-01-26 16:01:43 +0100814 if (!inode->i_dquot[cnt])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 return 1;
816 return 0;
817}
818
Ingo Molnard3be9152006-03-23 03:00:29 -0800819/* This routine is guarded by dqonoff_mutex mutex */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820static void add_dquot_ref(struct super_block *sb, int type)
821{
Jan Kara941d2382008-02-06 01:37:36 -0800822 struct inode *inode, *old_inode = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823
Christoph Hellwigd003fb72007-02-12 00:51:58 -0800824 spin_lock(&inode_lock);
825 list_for_each_entry(inode, &sb->s_inodes, i_sb_list) {
826 if (!atomic_read(&inode->i_writecount))
827 continue;
828 if (!dqinit_needed(inode, type))
829 continue;
830 if (inode->i_state & (I_FREEING|I_WILL_FREE))
831 continue;
832
833 __iget(inode);
834 spin_unlock(&inode_lock);
835
Jan Kara941d2382008-02-06 01:37:36 -0800836 iput(old_inode);
Christoph Hellwigd003fb72007-02-12 00:51:58 -0800837 sb->dq_op->initialize(inode, type);
Jan Kara941d2382008-02-06 01:37:36 -0800838 /* We hold a reference to 'inode' so it couldn't have been
839 * removed from s_inodes list while we dropped the inode_lock.
840 * We cannot iput the inode now as we can be holding the last
841 * reference and we cannot iput it under inode_lock. So we
842 * keep the reference and iput it later. */
843 old_inode = inode;
844 spin_lock(&inode_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 }
Christoph Hellwigd003fb72007-02-12 00:51:58 -0800846 spin_unlock(&inode_lock);
Jan Kara941d2382008-02-06 01:37:36 -0800847 iput(old_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848}
849
Jan Kara268157b2009-01-27 15:47:22 +0100850/*
851 * Return 0 if dqput() won't block.
852 * (note that 1 doesn't necessarily mean blocking)
853 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854static inline int dqput_blocks(struct dquot *dquot)
855{
856 if (atomic_read(&dquot->dq_count) <= 1)
857 return 1;
858 return 0;
859}
860
Jan Kara268157b2009-01-27 15:47:22 +0100861/*
862 * Remove references to dquots from inode and add dquot to list for freeing
863 * if we have the last referece to dquot
864 * We can't race with anybody because we hold dqptr_sem for writing...
865 */
Adrian Bunke5f00f42007-05-08 00:27:22 -0700866static int remove_inode_dquot_ref(struct inode *inode, int type,
867 struct list_head *tofree_head)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868{
869 struct dquot *dquot = inode->i_dquot[type];
870
Jan Karadd6f3c62009-01-26 16:01:43 +0100871 inode->i_dquot[type] = NULL;
872 if (dquot) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 if (dqput_blocks(dquot)) {
874#ifdef __DQUOT_PARANOIA
875 if (atomic_read(&dquot->dq_count) != 1)
876 printk(KERN_WARNING "VFS: Adding dquot with dq_count %d to dispose list.\n", atomic_read(&dquot->dq_count));
877#endif
878 spin_lock(&dq_list_lock);
Jan Kara268157b2009-01-27 15:47:22 +0100879 /* As dquot must have currently users it can't be on
880 * the free list... */
881 list_add(&dquot->dq_free, tofree_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 spin_unlock(&dq_list_lock);
883 return 1;
884 }
885 else
886 dqput(dquot); /* We have guaranteed we won't block */
887 }
888 return 0;
889}
890
Jan Kara268157b2009-01-27 15:47:22 +0100891/*
892 * Free list of dquots
893 * Dquots are removed from inodes and no new references can be got so we are
894 * the only ones holding reference
895 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896static void put_dquot_list(struct list_head *tofree_head)
897{
898 struct list_head *act_head;
899 struct dquot *dquot;
900
901 act_head = tofree_head->next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 while (act_head != tofree_head) {
903 dquot = list_entry(act_head, struct dquot, dq_free);
904 act_head = act_head->next;
Jan Kara268157b2009-01-27 15:47:22 +0100905 /* Remove dquot from the list so we won't have problems... */
906 list_del_init(&dquot->dq_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 dqput(dquot);
908 }
909}
910
Christoph Hellwigfb58b732007-02-12 00:51:57 -0800911static void remove_dquot_ref(struct super_block *sb, int type,
912 struct list_head *tofree_head)
913{
914 struct inode *inode;
915
916 spin_lock(&inode_lock);
917 list_for_each_entry(inode, &sb->s_inodes, i_sb_list) {
918 if (!IS_NOQUOTA(inode))
919 remove_inode_dquot_ref(inode, type, tofree_head);
920 }
921 spin_unlock(&inode_lock);
922}
923
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924/* Gather all references from inodes and drop them */
925static void drop_dquot_ref(struct super_block *sb, int type)
926{
927 LIST_HEAD(tofree_head);
928
Christoph Hellwigfb58b732007-02-12 00:51:57 -0800929 if (sb->dq_op) {
930 down_write(&sb_dqopt(sb)->dqptr_sem);
931 remove_dquot_ref(sb, type, &tofree_head);
932 up_write(&sb_dqopt(sb)->dqptr_sem);
933 put_dquot_list(&tofree_head);
934 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935}
936
Jan Kara12095462008-08-20 14:45:12 +0200937static inline void dquot_incr_inodes(struct dquot *dquot, qsize_t number)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938{
939 dquot->dq_dqb.dqb_curinodes += number;
940}
941
942static inline void dquot_incr_space(struct dquot *dquot, qsize_t number)
943{
944 dquot->dq_dqb.dqb_curspace += number;
945}
946
Mingming Caof18df222009-01-13 16:43:09 +0100947static inline void dquot_resv_space(struct dquot *dquot, qsize_t number)
948{
949 dquot->dq_dqb.dqb_rsvspace += number;
950}
951
Mingming Cao740d9dc2009-01-13 16:43:14 +0100952/*
953 * Claim reserved quota space
954 */
955static void dquot_claim_reserved_space(struct dquot *dquot,
956 qsize_t number)
957{
958 WARN_ON(dquot->dq_dqb.dqb_rsvspace < number);
959 dquot->dq_dqb.dqb_curspace += number;
960 dquot->dq_dqb.dqb_rsvspace -= number;
961}
962
963static inline
964void dquot_free_reserved_space(struct dquot *dquot, qsize_t number)
965{
966 dquot->dq_dqb.dqb_rsvspace -= number;
967}
968
Jan Kara7a2435d2009-01-27 01:47:11 +0100969static void dquot_decr_inodes(struct dquot *dquot, qsize_t number)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970{
Jan Karadb49d2d2008-10-01 18:21:39 +0200971 if (sb_dqopt(dquot->dq_sb)->flags & DQUOT_NEGATIVE_USAGE ||
972 dquot->dq_dqb.dqb_curinodes >= number)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 dquot->dq_dqb.dqb_curinodes -= number;
974 else
975 dquot->dq_dqb.dqb_curinodes = 0;
976 if (dquot->dq_dqb.dqb_curinodes <= dquot->dq_dqb.dqb_isoftlimit)
977 dquot->dq_dqb.dqb_itime = (time_t) 0;
978 clear_bit(DQ_INODES_B, &dquot->dq_flags);
979}
980
Jan Kara7a2435d2009-01-27 01:47:11 +0100981static void dquot_decr_space(struct dquot *dquot, qsize_t number)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982{
Jan Karadb49d2d2008-10-01 18:21:39 +0200983 if (sb_dqopt(dquot->dq_sb)->flags & DQUOT_NEGATIVE_USAGE ||
984 dquot->dq_dqb.dqb_curspace >= number)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 dquot->dq_dqb.dqb_curspace -= number;
986 else
987 dquot->dq_dqb.dqb_curspace = 0;
Jan Kara12095462008-08-20 14:45:12 +0200988 if (dquot->dq_dqb.dqb_curspace <= dquot->dq_dqb.dqb_bsoftlimit)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 dquot->dq_dqb.dqb_btime = (time_t) 0;
990 clear_bit(DQ_BLKS_B, &dquot->dq_flags);
991}
992
Jan Karac5254602007-12-22 14:03:25 -0800993static int warning_issued(struct dquot *dquot, const int warntype)
994{
995 int flag = (warntype == QUOTA_NL_BHARDWARN ||
996 warntype == QUOTA_NL_BSOFTLONGWARN) ? DQ_BLKS_B :
997 ((warntype == QUOTA_NL_IHARDWARN ||
998 warntype == QUOTA_NL_ISOFTLONGWARN) ? DQ_INODES_B : 0);
999
1000 if (!flag)
1001 return 0;
1002 return test_and_set_bit(flag, &dquot->dq_flags);
1003}
1004
Jan Kara8e893462007-10-16 23:29:31 -07001005#ifdef CONFIG_PRINT_QUOTA_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006static int flag_print_warnings = 1;
1007
Jan Kara7a2435d2009-01-27 01:47:11 +01001008static int need_print_warning(struct dquot *dquot)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009{
1010 if (!flag_print_warnings)
1011 return 0;
1012
1013 switch (dquot->dq_type) {
1014 case USRQUOTA:
David Howellsda9592e2008-11-14 10:39:05 +11001015 return current_fsuid() == dquot->dq_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 case GRPQUOTA:
1017 return in_group_p(dquot->dq_id);
1018 }
1019 return 0;
1020}
1021
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022/* Print warning to user which exceeded quota */
Jan Karac5254602007-12-22 14:03:25 -08001023static void print_warning(struct dquot *dquot, const int warntype)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024{
1025 char *msg = NULL;
Peter Zijlstra24ec8392006-12-08 02:36:04 -08001026 struct tty_struct *tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027
Jan Kara657d3bf2008-07-25 01:46:52 -07001028 if (warntype == QUOTA_NL_IHARDBELOW ||
1029 warntype == QUOTA_NL_ISOFTBELOW ||
1030 warntype == QUOTA_NL_BHARDBELOW ||
1031 warntype == QUOTA_NL_BSOFTBELOW || !need_print_warning(dquot))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 return;
1033
Peter Zijlstra24ec8392006-12-08 02:36:04 -08001034 tty = get_current_tty();
1035 if (!tty)
Alan Cox452a00d2008-10-13 10:39:13 +01001036 return;
Peter Zijlstra24ec8392006-12-08 02:36:04 -08001037 tty_write_message(tty, dquot->dq_sb->s_id);
Jan Kara8e893462007-10-16 23:29:31 -07001038 if (warntype == QUOTA_NL_ISOFTWARN || warntype == QUOTA_NL_BSOFTWARN)
Peter Zijlstra24ec8392006-12-08 02:36:04 -08001039 tty_write_message(tty, ": warning, ");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 else
Peter Zijlstra24ec8392006-12-08 02:36:04 -08001041 tty_write_message(tty, ": write failed, ");
1042 tty_write_message(tty, quotatypes[dquot->dq_type]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 switch (warntype) {
Jan Kara8e893462007-10-16 23:29:31 -07001044 case QUOTA_NL_IHARDWARN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 msg = " file limit reached.\r\n";
1046 break;
Jan Kara8e893462007-10-16 23:29:31 -07001047 case QUOTA_NL_ISOFTLONGWARN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 msg = " file quota exceeded too long.\r\n";
1049 break;
Jan Kara8e893462007-10-16 23:29:31 -07001050 case QUOTA_NL_ISOFTWARN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 msg = " file quota exceeded.\r\n";
1052 break;
Jan Kara8e893462007-10-16 23:29:31 -07001053 case QUOTA_NL_BHARDWARN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 msg = " block limit reached.\r\n";
1055 break;
Jan Kara8e893462007-10-16 23:29:31 -07001056 case QUOTA_NL_BSOFTLONGWARN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 msg = " block quota exceeded too long.\r\n";
1058 break;
Jan Kara8e893462007-10-16 23:29:31 -07001059 case QUOTA_NL_BSOFTWARN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 msg = " block quota exceeded.\r\n";
1061 break;
1062 }
Peter Zijlstra24ec8392006-12-08 02:36:04 -08001063 tty_write_message(tty, msg);
Alan Cox452a00d2008-10-13 10:39:13 +01001064 tty_kref_put(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065}
Jan Kara8e893462007-10-16 23:29:31 -07001066#endif
1067
1068#ifdef CONFIG_QUOTA_NETLINK_INTERFACE
1069
Jan Kara8e893462007-10-16 23:29:31 -07001070/* Netlink family structure for quota */
1071static struct genl_family quota_genl_family = {
1072 .id = GENL_ID_GENERATE,
1073 .hdrsize = 0,
1074 .name = "VFS_DQUOT",
1075 .version = 1,
1076 .maxattr = QUOTA_NL_A_MAX,
1077};
1078
1079/* Send warning to userspace about user which exceeded quota */
1080static void send_warning(const struct dquot *dquot, const char warntype)
1081{
1082 static atomic_t seq;
1083 struct sk_buff *skb;
1084 void *msg_head;
1085 int ret;
Jan Kara22dd4832007-12-22 14:03:25 -08001086 int msg_size = 4 * nla_total_size(sizeof(u32)) +
1087 2 * nla_total_size(sizeof(u64));
Jan Kara8e893462007-10-16 23:29:31 -07001088
1089 /* We have to allocate using GFP_NOFS as we are called from a
1090 * filesystem performing write and thus further recursion into
1091 * the fs to free some data could cause deadlocks. */
Jan Kara22dd4832007-12-22 14:03:25 -08001092 skb = genlmsg_new(msg_size, GFP_NOFS);
Jan Kara8e893462007-10-16 23:29:31 -07001093 if (!skb) {
1094 printk(KERN_ERR
1095 "VFS: Not enough memory to send quota warning.\n");
1096 return;
1097 }
1098 msg_head = genlmsg_put(skb, 0, atomic_add_return(1, &seq),
1099 &quota_genl_family, 0, QUOTA_NL_C_WARNING);
1100 if (!msg_head) {
1101 printk(KERN_ERR
1102 "VFS: Cannot store netlink header in quota warning.\n");
1103 goto err_out;
1104 }
1105 ret = nla_put_u32(skb, QUOTA_NL_A_QTYPE, dquot->dq_type);
1106 if (ret)
1107 goto attr_err_out;
1108 ret = nla_put_u64(skb, QUOTA_NL_A_EXCESS_ID, dquot->dq_id);
1109 if (ret)
1110 goto attr_err_out;
1111 ret = nla_put_u32(skb, QUOTA_NL_A_WARNING, warntype);
1112 if (ret)
1113 goto attr_err_out;
1114 ret = nla_put_u32(skb, QUOTA_NL_A_DEV_MAJOR,
1115 MAJOR(dquot->dq_sb->s_dev));
1116 if (ret)
1117 goto attr_err_out;
1118 ret = nla_put_u32(skb, QUOTA_NL_A_DEV_MINOR,
1119 MINOR(dquot->dq_sb->s_dev));
1120 if (ret)
1121 goto attr_err_out;
David Howellsda9592e2008-11-14 10:39:05 +11001122 ret = nla_put_u64(skb, QUOTA_NL_A_CAUSED_ID, current_uid());
Jan Kara8e893462007-10-16 23:29:31 -07001123 if (ret)
1124 goto attr_err_out;
1125 genlmsg_end(skb, msg_head);
1126
1127 ret = genlmsg_multicast(skb, 0, quota_genl_family.id, GFP_NOFS);
1128 if (ret < 0 && ret != -ESRCH)
1129 printk(KERN_ERR
1130 "VFS: Failed to send notification message: %d\n", ret);
1131 return;
1132attr_err_out:
Jan Kara22dd4832007-12-22 14:03:25 -08001133 printk(KERN_ERR "VFS: Not enough space to compose quota message!\n");
Jan Kara8e893462007-10-16 23:29:31 -07001134err_out:
1135 kfree_skb(skb);
1136}
1137#endif
Mingming Caof18df222009-01-13 16:43:09 +01001138/*
1139 * Write warnings to the console and send warning messages over netlink.
1140 *
1141 * Note that this function can sleep.
1142 */
Jan Kara7a2435d2009-01-27 01:47:11 +01001143static void flush_warnings(struct dquot *const *dquots, char *warntype)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144{
1145 int i;
1146
1147 for (i = 0; i < MAXQUOTAS; i++)
Jan Karadd6f3c62009-01-26 16:01:43 +01001148 if (dquots[i] && warntype[i] != QUOTA_NL_NOWARN &&
Jan Karac5254602007-12-22 14:03:25 -08001149 !warning_issued(dquots[i], warntype[i])) {
Jan Kara8e893462007-10-16 23:29:31 -07001150#ifdef CONFIG_PRINT_QUOTA_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 print_warning(dquots[i], warntype[i]);
Jan Kara8e893462007-10-16 23:29:31 -07001152#endif
1153#ifdef CONFIG_QUOTA_NETLINK_INTERFACE
1154 send_warning(dquots[i], warntype[i]);
1155#endif
1156 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157}
1158
Jan Kara7a2435d2009-01-27 01:47:11 +01001159static int ignore_hardlimit(struct dquot *dquot)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160{
1161 struct mem_dqinfo *info = &sb_dqopt(dquot->dq_sb)->info[dquot->dq_type];
1162
1163 return capable(CAP_SYS_RESOURCE) &&
Jan Kara268157b2009-01-27 15:47:22 +01001164 (info->dqi_format->qf_fmt_id != QFMT_VFS_OLD ||
1165 !(info->dqi_flags & V1_DQF_RSQUASH));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166}
1167
1168/* needs dq_data_lock */
Jan Kara12095462008-08-20 14:45:12 +02001169static int check_idq(struct dquot *dquot, qsize_t inodes, char *warntype)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170{
Jan Kara268157b2009-01-27 15:47:22 +01001171 qsize_t newinodes = dquot->dq_dqb.dqb_curinodes + inodes;
1172
Jan Kara8e893462007-10-16 23:29:31 -07001173 *warntype = QUOTA_NL_NOWARN;
Jan Karaf55abc02008-08-20 17:50:32 +02001174 if (!sb_has_quota_limits_enabled(dquot->dq_sb, dquot->dq_type) ||
1175 test_bit(DQ_FAKE_B, &dquot->dq_flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 return QUOTA_OK;
1177
1178 if (dquot->dq_dqb.dqb_ihardlimit &&
Jan Kara268157b2009-01-27 15:47:22 +01001179 newinodes > dquot->dq_dqb.dqb_ihardlimit &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 !ignore_hardlimit(dquot)) {
Jan Kara8e893462007-10-16 23:29:31 -07001181 *warntype = QUOTA_NL_IHARDWARN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 return NO_QUOTA;
1183 }
1184
1185 if (dquot->dq_dqb.dqb_isoftlimit &&
Jan Kara268157b2009-01-27 15:47:22 +01001186 newinodes > dquot->dq_dqb.dqb_isoftlimit &&
1187 dquot->dq_dqb.dqb_itime &&
1188 get_seconds() >= dquot->dq_dqb.dqb_itime &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 !ignore_hardlimit(dquot)) {
Jan Kara8e893462007-10-16 23:29:31 -07001190 *warntype = QUOTA_NL_ISOFTLONGWARN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 return NO_QUOTA;
1192 }
1193
1194 if (dquot->dq_dqb.dqb_isoftlimit &&
Jan Kara268157b2009-01-27 15:47:22 +01001195 newinodes > dquot->dq_dqb.dqb_isoftlimit &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 dquot->dq_dqb.dqb_itime == 0) {
Jan Kara8e893462007-10-16 23:29:31 -07001197 *warntype = QUOTA_NL_ISOFTWARN;
Jan Kara268157b2009-01-27 15:47:22 +01001198 dquot->dq_dqb.dqb_itime = get_seconds() +
1199 sb_dqopt(dquot->dq_sb)->info[dquot->dq_type].dqi_igrace;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 }
1201
1202 return QUOTA_OK;
1203}
1204
1205/* needs dq_data_lock */
1206static int check_bdq(struct dquot *dquot, qsize_t space, int prealloc, char *warntype)
1207{
Mingming Caof18df222009-01-13 16:43:09 +01001208 qsize_t tspace;
Jan Kara268157b2009-01-27 15:47:22 +01001209 struct super_block *sb = dquot->dq_sb;
Mingming Caof18df222009-01-13 16:43:09 +01001210
Jan Kara8e893462007-10-16 23:29:31 -07001211 *warntype = QUOTA_NL_NOWARN;
Jan Kara268157b2009-01-27 15:47:22 +01001212 if (!sb_has_quota_limits_enabled(sb, dquot->dq_type) ||
Jan Karaf55abc02008-08-20 17:50:32 +02001213 test_bit(DQ_FAKE_B, &dquot->dq_flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 return QUOTA_OK;
1215
Mingming Caof18df222009-01-13 16:43:09 +01001216 tspace = dquot->dq_dqb.dqb_curspace + dquot->dq_dqb.dqb_rsvspace
1217 + space;
1218
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 if (dquot->dq_dqb.dqb_bhardlimit &&
Mingming Caof18df222009-01-13 16:43:09 +01001220 tspace > dquot->dq_dqb.dqb_bhardlimit &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 !ignore_hardlimit(dquot)) {
1222 if (!prealloc)
Jan Kara8e893462007-10-16 23:29:31 -07001223 *warntype = QUOTA_NL_BHARDWARN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 return NO_QUOTA;
1225 }
1226
1227 if (dquot->dq_dqb.dqb_bsoftlimit &&
Mingming Caof18df222009-01-13 16:43:09 +01001228 tspace > dquot->dq_dqb.dqb_bsoftlimit &&
Jan Kara268157b2009-01-27 15:47:22 +01001229 dquot->dq_dqb.dqb_btime &&
1230 get_seconds() >= dquot->dq_dqb.dqb_btime &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 !ignore_hardlimit(dquot)) {
1232 if (!prealloc)
Jan Kara8e893462007-10-16 23:29:31 -07001233 *warntype = QUOTA_NL_BSOFTLONGWARN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 return NO_QUOTA;
1235 }
1236
1237 if (dquot->dq_dqb.dqb_bsoftlimit &&
Mingming Caof18df222009-01-13 16:43:09 +01001238 tspace > dquot->dq_dqb.dqb_bsoftlimit &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 dquot->dq_dqb.dqb_btime == 0) {
1240 if (!prealloc) {
Jan Kara8e893462007-10-16 23:29:31 -07001241 *warntype = QUOTA_NL_BSOFTWARN;
Jan Kara268157b2009-01-27 15:47:22 +01001242 dquot->dq_dqb.dqb_btime = get_seconds() +
1243 sb_dqopt(sb)->info[dquot->dq_type].dqi_bgrace;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 }
1245 else
1246 /*
1247 * We don't allow preallocation to exceed softlimit so exceeding will
1248 * be always printed
1249 */
1250 return NO_QUOTA;
1251 }
1252
1253 return QUOTA_OK;
1254}
1255
Jan Kara12095462008-08-20 14:45:12 +02001256static int info_idq_free(struct dquot *dquot, qsize_t inodes)
Jan Kara657d3bf2008-07-25 01:46:52 -07001257{
Jan Kara268157b2009-01-27 15:47:22 +01001258 qsize_t newinodes;
1259
Jan Kara657d3bf2008-07-25 01:46:52 -07001260 if (test_bit(DQ_FAKE_B, &dquot->dq_flags) ||
Jan Karaf55abc02008-08-20 17:50:32 +02001261 dquot->dq_dqb.dqb_curinodes <= dquot->dq_dqb.dqb_isoftlimit ||
1262 !sb_has_quota_limits_enabled(dquot->dq_sb, dquot->dq_type))
Jan Kara657d3bf2008-07-25 01:46:52 -07001263 return QUOTA_NL_NOWARN;
1264
Jan Kara268157b2009-01-27 15:47:22 +01001265 newinodes = dquot->dq_dqb.dqb_curinodes - inodes;
1266 if (newinodes <= dquot->dq_dqb.dqb_isoftlimit)
Jan Kara657d3bf2008-07-25 01:46:52 -07001267 return QUOTA_NL_ISOFTBELOW;
1268 if (dquot->dq_dqb.dqb_curinodes >= dquot->dq_dqb.dqb_ihardlimit &&
Jan Kara268157b2009-01-27 15:47:22 +01001269 newinodes < dquot->dq_dqb.dqb_ihardlimit)
Jan Kara657d3bf2008-07-25 01:46:52 -07001270 return QUOTA_NL_IHARDBELOW;
1271 return QUOTA_NL_NOWARN;
1272}
1273
1274static int info_bdq_free(struct dquot *dquot, qsize_t space)
1275{
1276 if (test_bit(DQ_FAKE_B, &dquot->dq_flags) ||
Jan Kara12095462008-08-20 14:45:12 +02001277 dquot->dq_dqb.dqb_curspace <= dquot->dq_dqb.dqb_bsoftlimit)
Jan Kara657d3bf2008-07-25 01:46:52 -07001278 return QUOTA_NL_NOWARN;
1279
Jan Kara12095462008-08-20 14:45:12 +02001280 if (dquot->dq_dqb.dqb_curspace - space <= dquot->dq_dqb.dqb_bsoftlimit)
Jan Kara657d3bf2008-07-25 01:46:52 -07001281 return QUOTA_NL_BSOFTBELOW;
Jan Kara12095462008-08-20 14:45:12 +02001282 if (dquot->dq_dqb.dqb_curspace >= dquot->dq_dqb.dqb_bhardlimit &&
1283 dquot->dq_dqb.dqb_curspace - space < dquot->dq_dqb.dqb_bhardlimit)
Jan Kara657d3bf2008-07-25 01:46:52 -07001284 return QUOTA_NL_BHARDBELOW;
1285 return QUOTA_NL_NOWARN;
1286}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287/*
1288 * Initialize quota pointers in inode
Jan Karacc334122009-01-12 17:23:05 +01001289 * We do things in a bit complicated way but by that we avoid calling
1290 * dqget() and thus filesystem callbacks under dqptr_sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 */
1292int dquot_initialize(struct inode *inode, int type)
1293{
1294 unsigned int id = 0;
1295 int cnt, ret = 0;
Jan Karadd6f3c62009-01-26 16:01:43 +01001296 struct dquot *got[MAXQUOTAS] = { NULL, NULL };
Jan Karacc334122009-01-12 17:23:05 +01001297 struct super_block *sb = inode->i_sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298
Ingo Molnard3be9152006-03-23 03:00:29 -08001299 /* First test before acquiring mutex - solves deadlocks when we
1300 * re-enter the quota code and are already holding the mutex */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 if (IS_NOQUOTA(inode))
1302 return 0;
Jan Karacc334122009-01-12 17:23:05 +01001303
1304 /* First get references to structures we might need. */
1305 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1306 if (type != -1 && cnt != type)
1307 continue;
1308 switch (cnt) {
1309 case USRQUOTA:
1310 id = inode->i_uid;
1311 break;
1312 case GRPQUOTA:
1313 id = inode->i_gid;
1314 break;
1315 }
1316 got[cnt] = dqget(sb, id, cnt);
1317 }
1318
1319 down_write(&sb_dqopt(sb)->dqptr_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320 /* Having dqptr_sem we know NOQUOTA flags can't be altered... */
1321 if (IS_NOQUOTA(inode))
1322 goto out_err;
1323 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1324 if (type != -1 && cnt != type)
1325 continue;
Jan Karacc334122009-01-12 17:23:05 +01001326 /* Avoid races with quotaoff() */
1327 if (!sb_has_quota_active(sb, cnt))
1328 continue;
Jan Karadd6f3c62009-01-26 16:01:43 +01001329 if (!inode->i_dquot[cnt]) {
Jan Karacc334122009-01-12 17:23:05 +01001330 inode->i_dquot[cnt] = got[cnt];
Jan Karadd6f3c62009-01-26 16:01:43 +01001331 got[cnt] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 }
1333 }
1334out_err:
Jan Karacc334122009-01-12 17:23:05 +01001335 up_write(&sb_dqopt(sb)->dqptr_sem);
1336 /* Drop unused references */
1337 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1338 dqput(got[cnt]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 return ret;
1340}
Mingming Cao08d03502009-01-14 16:19:32 +01001341EXPORT_SYMBOL(dquot_initialize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342
1343/*
1344 * Release all quotas referenced by inode
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 */
Jan Kara3d9ea252008-10-10 16:12:23 +02001346int dquot_drop(struct inode *inode)
1347{
Jan Karacc334122009-01-12 17:23:05 +01001348 int cnt;
1349 struct dquot *put[MAXQUOTAS];
1350
Jan Kara3d9ea252008-10-10 16:12:23 +02001351 down_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
Jan Karacc334122009-01-12 17:23:05 +01001352 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1353 put[cnt] = inode->i_dquot[cnt];
Jan Karadd6f3c62009-01-26 16:01:43 +01001354 inode->i_dquot[cnt] = NULL;
Jan Karacc334122009-01-12 17:23:05 +01001355 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 up_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
Jan Karacc334122009-01-12 17:23:05 +01001357
1358 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1359 dqput(put[cnt]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 return 0;
1361}
Mingming Cao08d03502009-01-14 16:19:32 +01001362EXPORT_SYMBOL(dquot_drop);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363
Jan Karab85f4b82008-07-25 01:46:50 -07001364/* Wrapper to remove references to quota structures from inode */
1365void vfs_dq_drop(struct inode *inode)
1366{
1367 /* Here we can get arbitrary inode from clear_inode() so we have
1368 * to be careful. OTOH we don't need locking as quota operations
1369 * are allowed to change only at mount time */
1370 if (!IS_NOQUOTA(inode) && inode->i_sb && inode->i_sb->dq_op
1371 && inode->i_sb->dq_op->drop) {
1372 int cnt;
1373 /* Test before calling to rule out calls from proc and such
1374 * where we are not allowed to block. Note that this is
1375 * actually reliable test even without the lock - the caller
1376 * must assure that nobody can come after the DQUOT_DROP and
1377 * add quota pointers back anyway */
1378 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
Jan Karadd6f3c62009-01-26 16:01:43 +01001379 if (inode->i_dquot[cnt])
Jan Karab85f4b82008-07-25 01:46:50 -07001380 break;
1381 if (cnt < MAXQUOTAS)
1382 inode->i_sb->dq_op->drop(inode);
1383 }
1384}
Mingming Cao08d03502009-01-14 16:19:32 +01001385EXPORT_SYMBOL(vfs_dq_drop);
Jan Karab85f4b82008-07-25 01:46:50 -07001386
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387/*
1388 * Following four functions update i_blocks+i_bytes fields and
1389 * quota information (together with appropriate checks)
1390 * NOTE: We absolutely rely on the fact that caller dirties
1391 * the inode (usually macros in quotaops.h care about this) and
1392 * holds a handle for the current transaction so that dquot write and
1393 * inode write go into the same transaction.
1394 */
1395
1396/*
1397 * This operation can block, but only after everything is updated
1398 */
Mingming Caof18df222009-01-13 16:43:09 +01001399int __dquot_alloc_space(struct inode *inode, qsize_t number,
1400 int warn, int reserve)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401{
Mingming Caof18df222009-01-13 16:43:09 +01001402 int cnt, ret = QUOTA_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403 char warntype[MAXQUOTAS];
1404
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
Jan Kara8e893462007-10-16 23:29:31 -07001406 warntype[cnt] = QUOTA_NL_NOWARN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408 spin_lock(&dq_data_lock);
1409 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
Jan Karadd6f3c62009-01-26 16:01:43 +01001410 if (!inode->i_dquot[cnt])
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411 continue;
Mingming Caof18df222009-01-13 16:43:09 +01001412 if (check_bdq(inode->i_dquot[cnt], number, warn, warntype+cnt)
1413 == NO_QUOTA) {
1414 ret = NO_QUOTA;
1415 goto out_unlock;
1416 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 }
1418 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
Jan Karadd6f3c62009-01-26 16:01:43 +01001419 if (!inode->i_dquot[cnt])
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 continue;
Mingming Caof18df222009-01-13 16:43:09 +01001421 if (reserve)
1422 dquot_resv_space(inode->i_dquot[cnt], number);
1423 else
1424 dquot_incr_space(inode->i_dquot[cnt], number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 }
Mingming Caof18df222009-01-13 16:43:09 +01001426 if (!reserve)
1427 inode_add_bytes(inode, number);
1428out_unlock:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 spin_unlock(&dq_data_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 flush_warnings(inode->i_dquot, warntype);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431 return ret;
1432}
1433
Mingming Caof18df222009-01-13 16:43:09 +01001434int dquot_alloc_space(struct inode *inode, qsize_t number, int warn)
1435{
1436 int cnt, ret = QUOTA_OK;
1437
1438 /*
1439 * First test before acquiring mutex - solves deadlocks when we
1440 * re-enter the quota code and are already holding the mutex
1441 */
1442 if (IS_NOQUOTA(inode)) {
1443 inode_add_bytes(inode, number);
1444 goto out;
1445 }
1446
1447 down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1448 if (IS_NOQUOTA(inode)) {
1449 inode_add_bytes(inode, number);
1450 goto out_unlock;
1451 }
1452
1453 ret = __dquot_alloc_space(inode, number, warn, 0);
1454 if (ret == NO_QUOTA)
1455 goto out_unlock;
1456
1457 /* Dirtify all the dquots - this can block when journalling */
1458 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1459 if (inode->i_dquot[cnt])
1460 mark_dquot_dirty(inode->i_dquot[cnt]);
1461out_unlock:
1462 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1463out:
1464 return ret;
1465}
Mingming Cao08d03502009-01-14 16:19:32 +01001466EXPORT_SYMBOL(dquot_alloc_space);
Mingming Caof18df222009-01-13 16:43:09 +01001467
1468int dquot_reserve_space(struct inode *inode, qsize_t number, int warn)
1469{
1470 int ret = QUOTA_OK;
1471
1472 if (IS_NOQUOTA(inode))
1473 goto out;
1474
1475 down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1476 if (IS_NOQUOTA(inode))
1477 goto out_unlock;
1478
1479 ret = __dquot_alloc_space(inode, number, warn, 1);
1480out_unlock:
1481 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1482out:
1483 return ret;
1484}
1485EXPORT_SYMBOL(dquot_reserve_space);
1486
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487/*
1488 * This operation can block, but only after everything is updated
1489 */
Jan Kara12095462008-08-20 14:45:12 +02001490int dquot_alloc_inode(const struct inode *inode, qsize_t number)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491{
1492 int cnt, ret = NO_QUOTA;
1493 char warntype[MAXQUOTAS];
1494
Ingo Molnard3be9152006-03-23 03:00:29 -08001495 /* First test before acquiring mutex - solves deadlocks when we
1496 * re-enter the quota code and are already holding the mutex */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 if (IS_NOQUOTA(inode))
1498 return QUOTA_OK;
1499 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
Jan Kara8e893462007-10-16 23:29:31 -07001500 warntype[cnt] = QUOTA_NL_NOWARN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501 down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1502 if (IS_NOQUOTA(inode)) {
1503 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1504 return QUOTA_OK;
1505 }
1506 spin_lock(&dq_data_lock);
1507 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
Jan Karadd6f3c62009-01-26 16:01:43 +01001508 if (!inode->i_dquot[cnt])
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509 continue;
Jan Kara268157b2009-01-27 15:47:22 +01001510 if (check_idq(inode->i_dquot[cnt], number, warntype+cnt)
1511 == NO_QUOTA)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512 goto warn_put_all;
1513 }
1514
1515 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
Jan Karadd6f3c62009-01-26 16:01:43 +01001516 if (!inode->i_dquot[cnt])
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517 continue;
1518 dquot_incr_inodes(inode->i_dquot[cnt], number);
1519 }
1520 ret = QUOTA_OK;
1521warn_put_all:
1522 spin_unlock(&dq_data_lock);
1523 if (ret == QUOTA_OK)
1524 /* Dirtify all the dquots - this can block when journalling */
1525 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1526 if (inode->i_dquot[cnt])
1527 mark_dquot_dirty(inode->i_dquot[cnt]);
Jan Kara087ee8d2007-12-17 16:20:26 -08001528 flush_warnings(inode->i_dquot, warntype);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1530 return ret;
1531}
Mingming Cao08d03502009-01-14 16:19:32 +01001532EXPORT_SYMBOL(dquot_alloc_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533
Mingming Cao740d9dc2009-01-13 16:43:14 +01001534int dquot_claim_space(struct inode *inode, qsize_t number)
1535{
1536 int cnt;
1537 int ret = QUOTA_OK;
1538
1539 if (IS_NOQUOTA(inode)) {
1540 inode_add_bytes(inode, number);
1541 goto out;
1542 }
1543
1544 down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1545 if (IS_NOQUOTA(inode)) {
1546 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1547 inode_add_bytes(inode, number);
1548 goto out;
1549 }
1550
1551 spin_lock(&dq_data_lock);
1552 /* Claim reserved quotas to allocated quotas */
1553 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
Jan Karadd6f3c62009-01-26 16:01:43 +01001554 if (inode->i_dquot[cnt])
Mingming Cao740d9dc2009-01-13 16:43:14 +01001555 dquot_claim_reserved_space(inode->i_dquot[cnt],
1556 number);
1557 }
1558 /* Update inode bytes */
1559 inode_add_bytes(inode, number);
1560 spin_unlock(&dq_data_lock);
1561 /* Dirtify all the dquots - this can block when journalling */
1562 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1563 if (inode->i_dquot[cnt])
1564 mark_dquot_dirty(inode->i_dquot[cnt]);
1565 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1566out:
1567 return ret;
1568}
1569EXPORT_SYMBOL(dquot_claim_space);
1570
1571/*
1572 * Release reserved quota space
1573 */
1574void dquot_release_reserved_space(struct inode *inode, qsize_t number)
1575{
1576 int cnt;
1577
1578 if (IS_NOQUOTA(inode))
1579 goto out;
1580
1581 down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1582 if (IS_NOQUOTA(inode))
1583 goto out_unlock;
1584
1585 spin_lock(&dq_data_lock);
1586 /* Release reserved dquots */
1587 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
Jan Karadd6f3c62009-01-26 16:01:43 +01001588 if (inode->i_dquot[cnt])
Mingming Cao740d9dc2009-01-13 16:43:14 +01001589 dquot_free_reserved_space(inode->i_dquot[cnt], number);
1590 }
1591 spin_unlock(&dq_data_lock);
1592
1593out_unlock:
1594 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1595out:
1596 return;
1597}
1598EXPORT_SYMBOL(dquot_release_reserved_space);
1599
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600/*
1601 * This operation can block, but only after everything is updated
1602 */
1603int dquot_free_space(struct inode *inode, qsize_t number)
1604{
1605 unsigned int cnt;
Jan Kara657d3bf2008-07-25 01:46:52 -07001606 char warntype[MAXQUOTAS];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607
Ingo Molnard3be9152006-03-23 03:00:29 -08001608 /* First test before acquiring mutex - solves deadlocks when we
1609 * re-enter the quota code and are already holding the mutex */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610 if (IS_NOQUOTA(inode)) {
1611out_sub:
1612 inode_sub_bytes(inode, number);
1613 return QUOTA_OK;
1614 }
Jan Kara657d3bf2008-07-25 01:46:52 -07001615
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616 down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1617 /* Now recheck reliably when holding dqptr_sem */
1618 if (IS_NOQUOTA(inode)) {
1619 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1620 goto out_sub;
1621 }
1622 spin_lock(&dq_data_lock);
1623 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
Jan Karadd6f3c62009-01-26 16:01:43 +01001624 if (!inode->i_dquot[cnt])
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625 continue;
Jan Kara657d3bf2008-07-25 01:46:52 -07001626 warntype[cnt] = info_bdq_free(inode->i_dquot[cnt], number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627 dquot_decr_space(inode->i_dquot[cnt], number);
1628 }
1629 inode_sub_bytes(inode, number);
1630 spin_unlock(&dq_data_lock);
1631 /* Dirtify all the dquots - this can block when journalling */
1632 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1633 if (inode->i_dquot[cnt])
1634 mark_dquot_dirty(inode->i_dquot[cnt]);
Jan Kara657d3bf2008-07-25 01:46:52 -07001635 flush_warnings(inode->i_dquot, warntype);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1637 return QUOTA_OK;
1638}
Mingming Cao08d03502009-01-14 16:19:32 +01001639EXPORT_SYMBOL(dquot_free_space);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640
1641/*
1642 * This operation can block, but only after everything is updated
1643 */
Jan Kara12095462008-08-20 14:45:12 +02001644int dquot_free_inode(const struct inode *inode, qsize_t number)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645{
1646 unsigned int cnt;
Jan Kara657d3bf2008-07-25 01:46:52 -07001647 char warntype[MAXQUOTAS];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648
Ingo Molnard3be9152006-03-23 03:00:29 -08001649 /* First test before acquiring mutex - solves deadlocks when we
1650 * re-enter the quota code and are already holding the mutex */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651 if (IS_NOQUOTA(inode))
1652 return QUOTA_OK;
Jan Kara657d3bf2008-07-25 01:46:52 -07001653
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654 down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1655 /* Now recheck reliably when holding dqptr_sem */
1656 if (IS_NOQUOTA(inode)) {
1657 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1658 return QUOTA_OK;
1659 }
1660 spin_lock(&dq_data_lock);
1661 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
Jan Karadd6f3c62009-01-26 16:01:43 +01001662 if (!inode->i_dquot[cnt])
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663 continue;
Jan Kara657d3bf2008-07-25 01:46:52 -07001664 warntype[cnt] = info_idq_free(inode->i_dquot[cnt], number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665 dquot_decr_inodes(inode->i_dquot[cnt], number);
1666 }
1667 spin_unlock(&dq_data_lock);
1668 /* Dirtify all the dquots - this can block when journalling */
1669 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1670 if (inode->i_dquot[cnt])
1671 mark_dquot_dirty(inode->i_dquot[cnt]);
Jan Kara657d3bf2008-07-25 01:46:52 -07001672 flush_warnings(inode->i_dquot, warntype);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1674 return QUOTA_OK;
1675}
Mingming Cao08d03502009-01-14 16:19:32 +01001676EXPORT_SYMBOL(dquot_free_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677
1678/*
Mingming Cao740d9dc2009-01-13 16:43:14 +01001679 * call back function, get reserved quota space from underlying fs
1680 */
1681qsize_t dquot_get_reserved_space(struct inode *inode)
1682{
1683 qsize_t reserved_space = 0;
1684
1685 if (sb_any_quota_active(inode->i_sb) &&
1686 inode->i_sb->dq_op->get_reserved_space)
1687 reserved_space = inode->i_sb->dq_op->get_reserved_space(inode);
1688 return reserved_space;
1689}
1690
1691/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692 * Transfer the number of inode and blocks from one diskquota to an other.
1693 *
1694 * This operation can block, but only after everything is updated
1695 * A transaction must be started when entering this function.
1696 */
1697int dquot_transfer(struct inode *inode, struct iattr *iattr)
1698{
Mingming Cao740d9dc2009-01-13 16:43:14 +01001699 qsize_t space, cur_space;
1700 qsize_t rsv_space = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701 struct dquot *transfer_from[MAXQUOTAS];
1702 struct dquot *transfer_to[MAXQUOTAS];
Jan Karacc334122009-01-12 17:23:05 +01001703 int cnt, ret = QUOTA_OK;
1704 int chuid = iattr->ia_valid & ATTR_UID && inode->i_uid != iattr->ia_uid,
1705 chgid = iattr->ia_valid & ATTR_GID && inode->i_gid != iattr->ia_gid;
Jan Kara657d3bf2008-07-25 01:46:52 -07001706 char warntype_to[MAXQUOTAS];
1707 char warntype_from_inodes[MAXQUOTAS], warntype_from_space[MAXQUOTAS];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708
Ingo Molnard3be9152006-03-23 03:00:29 -08001709 /* First test before acquiring mutex - solves deadlocks when we
1710 * re-enter the quota code and are already holding the mutex */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711 if (IS_NOQUOTA(inode))
1712 return QUOTA_OK;
Jan Karacc334122009-01-12 17:23:05 +01001713 /* Initialize the arrays */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
Jan Karadd6f3c62009-01-26 16:01:43 +01001715 transfer_from[cnt] = NULL;
1716 transfer_to[cnt] = NULL;
Jan Kara657d3bf2008-07-25 01:46:52 -07001717 warntype_to[cnt] = QUOTA_NL_NOWARN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718 }
Jan Kara268157b2009-01-27 15:47:22 +01001719 if (chuid)
1720 transfer_to[USRQUOTA] = dqget(inode->i_sb, iattr->ia_uid,
1721 USRQUOTA);
1722 if (chgid)
1723 transfer_to[GRPQUOTA] = dqget(inode->i_sb, iattr->ia_gid,
1724 GRPQUOTA);
Jan Karacc334122009-01-12 17:23:05 +01001725
1726 down_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
1727 /* Now recheck reliably when holding dqptr_sem */
1728 if (IS_NOQUOTA(inode)) { /* File without quota accounting? */
1729 up_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
1730 goto put_all;
1731 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732 spin_lock(&dq_data_lock);
Mingming Cao740d9dc2009-01-13 16:43:14 +01001733 cur_space = inode_get_bytes(inode);
1734 rsv_space = dquot_get_reserved_space(inode);
1735 space = cur_space + rsv_space;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736 /* Build the transfer_from list and check the limits */
1737 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
Jan Karadd6f3c62009-01-26 16:01:43 +01001738 if (!transfer_to[cnt])
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739 continue;
1740 transfer_from[cnt] = inode->i_dquot[cnt];
Jan Kara657d3bf2008-07-25 01:46:52 -07001741 if (check_idq(transfer_to[cnt], 1, warntype_to + cnt) ==
1742 NO_QUOTA || check_bdq(transfer_to[cnt], space, 0,
1743 warntype_to + cnt) == NO_QUOTA)
Jan Karacc334122009-01-12 17:23:05 +01001744 goto over_quota;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745 }
1746
1747 /*
1748 * Finally perform the needed transfer from transfer_from to transfer_to
1749 */
1750 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1751 /*
1752 * Skip changes for same uid or gid or for turned off quota-type.
1753 */
Jan Karadd6f3c62009-01-26 16:01:43 +01001754 if (!transfer_to[cnt])
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755 continue;
1756
1757 /* Due to IO error we might not have transfer_from[] structure */
1758 if (transfer_from[cnt]) {
Jan Kara657d3bf2008-07-25 01:46:52 -07001759 warntype_from_inodes[cnt] =
1760 info_idq_free(transfer_from[cnt], 1);
1761 warntype_from_space[cnt] =
1762 info_bdq_free(transfer_from[cnt], space);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763 dquot_decr_inodes(transfer_from[cnt], 1);
Mingming Cao740d9dc2009-01-13 16:43:14 +01001764 dquot_decr_space(transfer_from[cnt], cur_space);
1765 dquot_free_reserved_space(transfer_from[cnt],
1766 rsv_space);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767 }
1768
1769 dquot_incr_inodes(transfer_to[cnt], 1);
Mingming Cao740d9dc2009-01-13 16:43:14 +01001770 dquot_incr_space(transfer_to[cnt], cur_space);
1771 dquot_resv_space(transfer_to[cnt], rsv_space);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772
1773 inode->i_dquot[cnt] = transfer_to[cnt];
1774 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775 spin_unlock(&dq_data_lock);
Jan Karacc334122009-01-12 17:23:05 +01001776 up_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
1777
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778 /* Dirtify all the dquots - this can block when journalling */
1779 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1780 if (transfer_from[cnt])
1781 mark_dquot_dirty(transfer_from[cnt]);
Jan Karacc334122009-01-12 17:23:05 +01001782 if (transfer_to[cnt]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783 mark_dquot_dirty(transfer_to[cnt]);
Jan Karacc334122009-01-12 17:23:05 +01001784 /* The reference we got is transferred to the inode */
Jan Karadd6f3c62009-01-26 16:01:43 +01001785 transfer_to[cnt] = NULL;
Jan Karacc334122009-01-12 17:23:05 +01001786 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787 }
Jan Karacc334122009-01-12 17:23:05 +01001788warn_put_all:
Jan Kara657d3bf2008-07-25 01:46:52 -07001789 flush_warnings(transfer_to, warntype_to);
1790 flush_warnings(transfer_from, warntype_from_inodes);
1791 flush_warnings(transfer_from, warntype_from_space);
Jan Karacc334122009-01-12 17:23:05 +01001792put_all:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
Jan Karacc334122009-01-12 17:23:05 +01001794 dqput(transfer_from[cnt]);
1795 dqput(transfer_to[cnt]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797 return ret;
Jan Karacc334122009-01-12 17:23:05 +01001798over_quota:
1799 spin_unlock(&dq_data_lock);
1800 up_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
1801 /* Clear dquot pointers we don't want to dqput() */
1802 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
Jan Karadd6f3c62009-01-26 16:01:43 +01001803 transfer_from[cnt] = NULL;
Jan Karacc334122009-01-12 17:23:05 +01001804 ret = NO_QUOTA;
1805 goto warn_put_all;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806}
Mingming Cao08d03502009-01-14 16:19:32 +01001807EXPORT_SYMBOL(dquot_transfer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808
Jan Karab85f4b82008-07-25 01:46:50 -07001809/* Wrapper for transferring ownership of an inode */
1810int vfs_dq_transfer(struct inode *inode, struct iattr *iattr)
1811{
Jan Karaf55abc02008-08-20 17:50:32 +02001812 if (sb_any_quota_active(inode->i_sb) && !IS_NOQUOTA(inode)) {
Jan Karab85f4b82008-07-25 01:46:50 -07001813 vfs_dq_init(inode);
1814 if (inode->i_sb->dq_op->transfer(inode, iattr) == NO_QUOTA)
1815 return 1;
1816 }
1817 return 0;
1818}
Mingming Cao08d03502009-01-14 16:19:32 +01001819EXPORT_SYMBOL(vfs_dq_transfer);
Jan Karab85f4b82008-07-25 01:46:50 -07001820
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821/*
1822 * Write info of quota file to disk
1823 */
1824int dquot_commit_info(struct super_block *sb, int type)
1825{
1826 int ret;
1827 struct quota_info *dqopt = sb_dqopt(sb);
1828
Ingo Molnard3be9152006-03-23 03:00:29 -08001829 mutex_lock(&dqopt->dqio_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830 ret = dqopt->ops[type]->write_file_info(sb, type);
Ingo Molnard3be9152006-03-23 03:00:29 -08001831 mutex_unlock(&dqopt->dqio_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832 return ret;
1833}
Mingming Cao08d03502009-01-14 16:19:32 +01001834EXPORT_SYMBOL(dquot_commit_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835
1836/*
1837 * Definitions of diskquota operations.
1838 */
1839struct dquot_operations dquot_operations = {
1840 .initialize = dquot_initialize,
1841 .drop = dquot_drop,
1842 .alloc_space = dquot_alloc_space,
1843 .alloc_inode = dquot_alloc_inode,
1844 .free_space = dquot_free_space,
1845 .free_inode = dquot_free_inode,
1846 .transfer = dquot_transfer,
1847 .write_dquot = dquot_commit,
1848 .acquire_dquot = dquot_acquire,
1849 .release_dquot = dquot_release,
1850 .mark_dirty = dquot_mark_dquot_dirty,
Jan Kara74f783a2008-08-19 14:51:22 +02001851 .write_info = dquot_commit_info,
1852 .alloc_dquot = dquot_alloc,
1853 .destroy_dquot = dquot_destroy,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854};
1855
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856/*
1857 * Turn quota off on a device. type == -1 ==> quotaoff for all types (umount)
1858 */
Jan Karaf55abc02008-08-20 17:50:32 +02001859int vfs_quota_disable(struct super_block *sb, int type, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860{
Jan Kara0ff5af82008-04-28 02:14:33 -07001861 int cnt, ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862 struct quota_info *dqopt = sb_dqopt(sb);
1863 struct inode *toputinode[MAXQUOTAS];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864
Jan Karaf55abc02008-08-20 17:50:32 +02001865 /* Cannot turn off usage accounting without turning off limits, or
1866 * suspend quotas and simultaneously turn quotas off. */
1867 if ((flags & DQUOT_USAGE_ENABLED && !(flags & DQUOT_LIMITS_ENABLED))
1868 || (flags & DQUOT_SUSPENDED && flags & (DQUOT_LIMITS_ENABLED |
1869 DQUOT_USAGE_ENABLED)))
1870 return -EINVAL;
1871
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872 /* We need to serialize quota_off() for device */
Ingo Molnard3be9152006-03-23 03:00:29 -08001873 mutex_lock(&dqopt->dqonoff_mutex);
Jan Kara9377abd2008-05-12 14:02:08 -07001874
1875 /*
1876 * Skip everything if there's nothing to do. We have to do this because
1877 * sometimes we are called when fill_super() failed and calling
1878 * sync_fs() in such cases does no good.
1879 */
Jan Karaf55abc02008-08-20 17:50:32 +02001880 if (!sb_any_quota_loaded(sb)) {
Jan Kara9377abd2008-05-12 14:02:08 -07001881 mutex_unlock(&dqopt->dqonoff_mutex);
1882 return 0;
1883 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1885 toputinode[cnt] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886 if (type != -1 && cnt != type)
1887 continue;
Jan Karaf55abc02008-08-20 17:50:32 +02001888 if (!sb_has_quota_loaded(sb, cnt))
Jan Kara0ff5af82008-04-28 02:14:33 -07001889 continue;
Jan Karaf55abc02008-08-20 17:50:32 +02001890
1891 if (flags & DQUOT_SUSPENDED) {
Jan Karacc334122009-01-12 17:23:05 +01001892 spin_lock(&dq_state_lock);
Jan Karaf55abc02008-08-20 17:50:32 +02001893 dqopt->flags |=
1894 dquot_state_flag(DQUOT_SUSPENDED, cnt);
Jan Karacc334122009-01-12 17:23:05 +01001895 spin_unlock(&dq_state_lock);
Jan Karaf55abc02008-08-20 17:50:32 +02001896 } else {
Jan Karacc334122009-01-12 17:23:05 +01001897 spin_lock(&dq_state_lock);
Jan Karaf55abc02008-08-20 17:50:32 +02001898 dqopt->flags &= ~dquot_state_flag(flags, cnt);
1899 /* Turning off suspended quotas? */
1900 if (!sb_has_quota_loaded(sb, cnt) &&
1901 sb_has_quota_suspended(sb, cnt)) {
1902 dqopt->flags &= ~dquot_state_flag(
1903 DQUOT_SUSPENDED, cnt);
Jan Karacc334122009-01-12 17:23:05 +01001904 spin_unlock(&dq_state_lock);
Jan Karaf55abc02008-08-20 17:50:32 +02001905 iput(dqopt->files[cnt]);
1906 dqopt->files[cnt] = NULL;
1907 continue;
1908 }
Jan Karacc334122009-01-12 17:23:05 +01001909 spin_unlock(&dq_state_lock);
Jan Kara0ff5af82008-04-28 02:14:33 -07001910 }
Jan Karaf55abc02008-08-20 17:50:32 +02001911
1912 /* We still have to keep quota loaded? */
1913 if (sb_has_quota_loaded(sb, cnt) && !(flags & DQUOT_SUSPENDED))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915
1916 /* Note: these are blocking operations */
1917 drop_dquot_ref(sb, cnt);
1918 invalidate_dquots(sb, cnt);
1919 /*
Jan Kara268157b2009-01-27 15:47:22 +01001920 * Now all dquots should be invalidated, all writes done so we
1921 * should be only users of the info. No locks needed.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922 */
1923 if (info_dirty(&dqopt->info[cnt]))
1924 sb->dq_op->write_info(sb, cnt);
1925 if (dqopt->ops[cnt]->free_file_info)
1926 dqopt->ops[cnt]->free_file_info(sb, cnt);
1927 put_quota_format(dqopt->info[cnt].dqi_format);
1928
1929 toputinode[cnt] = dqopt->files[cnt];
Jan Karaf55abc02008-08-20 17:50:32 +02001930 if (!sb_has_quota_loaded(sb, cnt))
Jan Kara0ff5af82008-04-28 02:14:33 -07001931 dqopt->files[cnt] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932 dqopt->info[cnt].dqi_flags = 0;
1933 dqopt->info[cnt].dqi_igrace = 0;
1934 dqopt->info[cnt].dqi_bgrace = 0;
1935 dqopt->ops[cnt] = NULL;
1936 }
Ingo Molnard3be9152006-03-23 03:00:29 -08001937 mutex_unlock(&dqopt->dqonoff_mutex);
Jan Karaca785ec2008-09-30 17:53:37 +02001938
1939 /* Skip syncing and setting flags if quota files are hidden */
1940 if (dqopt->flags & DQUOT_QUOTA_SYS_FILE)
1941 goto put_inodes;
1942
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943 /* Sync the superblock so that buffers with quota data are written to
Al Viro7b7b1ac2005-11-07 17:13:39 -05001944 * disk (and so userspace sees correct data afterwards). */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945 if (sb->s_op->sync_fs)
1946 sb->s_op->sync_fs(sb, 1);
1947 sync_blockdev(sb->s_bdev);
1948 /* Now the quota files are just ordinary files and we can set the
1949 * inode flags back. Moreover we discard the pagecache so that
1950 * userspace sees the writes we did bypassing the pagecache. We
1951 * must also discard the blockdev buffers so that we see the
1952 * changes done by userspace on the next quotaon() */
1953 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1954 if (toputinode[cnt]) {
Ingo Molnard3be9152006-03-23 03:00:29 -08001955 mutex_lock(&dqopt->dqonoff_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956 /* If quota was reenabled in the meantime, we have
1957 * nothing to do */
Jan Karaf55abc02008-08-20 17:50:32 +02001958 if (!sb_has_quota_loaded(sb, cnt)) {
Jan Kara268157b2009-01-27 15:47:22 +01001959 mutex_lock_nested(&toputinode[cnt]->i_mutex,
1960 I_MUTEX_QUOTA);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961 toputinode[cnt]->i_flags &= ~(S_IMMUTABLE |
1962 S_NOATIME | S_NOQUOTA);
Jan Kara268157b2009-01-27 15:47:22 +01001963 truncate_inode_pages(&toputinode[cnt]->i_data,
1964 0);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001965 mutex_unlock(&toputinode[cnt]->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966 mark_inode_dirty(toputinode[cnt]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967 }
Ingo Molnard3be9152006-03-23 03:00:29 -08001968 mutex_unlock(&dqopt->dqonoff_mutex);
Jan Karaca785ec2008-09-30 17:53:37 +02001969 }
1970 if (sb->s_bdev)
1971 invalidate_bdev(sb->s_bdev);
1972put_inodes:
1973 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1974 if (toputinode[cnt]) {
Jan Kara0ff5af82008-04-28 02:14:33 -07001975 /* On remount RO, we keep the inode pointer so that we
Jan Karaf55abc02008-08-20 17:50:32 +02001976 * can reenable quota on the subsequent remount RW. We
1977 * have to check 'flags' variable and not use sb_has_
1978 * function because another quotaon / quotaoff could
1979 * change global state before we got here. We refuse
1980 * to suspend quotas when there is pending delete on
1981 * the quota file... */
1982 if (!(flags & DQUOT_SUSPENDED))
Jan Kara0ff5af82008-04-28 02:14:33 -07001983 iput(toputinode[cnt]);
1984 else if (!toputinode[cnt]->i_nlink)
1985 ret = -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986 }
Jan Kara0ff5af82008-04-28 02:14:33 -07001987 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988}
Mingming Cao08d03502009-01-14 16:19:32 +01001989EXPORT_SYMBOL(vfs_quota_disable);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990
Jan Karaf55abc02008-08-20 17:50:32 +02001991int vfs_quota_off(struct super_block *sb, int type, int remount)
1992{
1993 return vfs_quota_disable(sb, type, remount ? DQUOT_SUSPENDED :
1994 (DQUOT_USAGE_ENABLED | DQUOT_LIMITS_ENABLED));
1995}
Mingming Cao08d03502009-01-14 16:19:32 +01001996EXPORT_SYMBOL(vfs_quota_off);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997/*
1998 * Turn quotas on on a device
1999 */
2000
Jan Karaf55abc02008-08-20 17:50:32 +02002001/*
2002 * Helper function to turn quotas on when we already have the inode of
2003 * quota file and no quota information is loaded.
2004 */
2005static int vfs_load_quota_inode(struct inode *inode, int type, int format_id,
2006 unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007{
2008 struct quota_format_type *fmt = find_quota_format(format_id);
2009 struct super_block *sb = inode->i_sb;
2010 struct quota_info *dqopt = sb_dqopt(sb);
2011 int error;
2012 int oldflags = -1;
2013
2014 if (!fmt)
2015 return -ESRCH;
2016 if (!S_ISREG(inode->i_mode)) {
2017 error = -EACCES;
2018 goto out_fmt;
2019 }
2020 if (IS_RDONLY(inode)) {
2021 error = -EROFS;
2022 goto out_fmt;
2023 }
2024 if (!sb->s_op->quota_write || !sb->s_op->quota_read) {
2025 error = -EINVAL;
2026 goto out_fmt;
2027 }
Jan Karaf55abc02008-08-20 17:50:32 +02002028 /* Usage always has to be set... */
2029 if (!(flags & DQUOT_USAGE_ENABLED)) {
2030 error = -EINVAL;
2031 goto out_fmt;
2032 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033
Jan Karaca785ec2008-09-30 17:53:37 +02002034 if (!(dqopt->flags & DQUOT_QUOTA_SYS_FILE)) {
2035 /* As we bypass the pagecache we must now flush the inode so
2036 * that we see all the changes from userspace... */
2037 write_inode_now(inode, 1);
2038 /* And now flush the block cache so that kernel sees the
2039 * changes */
2040 invalidate_bdev(sb->s_bdev);
2041 }
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002042 mutex_lock(&inode->i_mutex);
Ingo Molnard3be9152006-03-23 03:00:29 -08002043 mutex_lock(&dqopt->dqonoff_mutex);
Jan Karaf55abc02008-08-20 17:50:32 +02002044 if (sb_has_quota_loaded(sb, type)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045 error = -EBUSY;
2046 goto out_lock;
2047 }
Jan Karaca785ec2008-09-30 17:53:37 +02002048
2049 if (!(dqopt->flags & DQUOT_QUOTA_SYS_FILE)) {
2050 /* We don't want quota and atime on quota files (deadlocks
2051 * possible) Also nobody should write to the file - we use
2052 * special IO operations which ignore the immutable bit. */
2053 down_write(&dqopt->dqptr_sem);
Jan Kara268157b2009-01-27 15:47:22 +01002054 oldflags = inode->i_flags & (S_NOATIME | S_IMMUTABLE |
2055 S_NOQUOTA);
Jan Karaca785ec2008-09-30 17:53:37 +02002056 inode->i_flags |= S_NOQUOTA | S_NOATIME | S_IMMUTABLE;
2057 up_write(&dqopt->dqptr_sem);
2058 sb->dq_op->drop(inode);
2059 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060
2061 error = -EIO;
2062 dqopt->files[type] = igrab(inode);
2063 if (!dqopt->files[type])
2064 goto out_lock;
2065 error = -EINVAL;
2066 if (!fmt->qf_ops->check_quota_file(sb, type))
2067 goto out_file_init;
2068
2069 dqopt->ops[type] = fmt->qf_ops;
2070 dqopt->info[type].dqi_format = fmt;
Jan Kara0ff5af82008-04-28 02:14:33 -07002071 dqopt->info[type].dqi_fmt_id = format_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072 INIT_LIST_HEAD(&dqopt->info[type].dqi_dirty_list);
Ingo Molnard3be9152006-03-23 03:00:29 -08002073 mutex_lock(&dqopt->dqio_mutex);
Jan Kara268157b2009-01-27 15:47:22 +01002074 error = dqopt->ops[type]->read_file_info(sb, type);
2075 if (error < 0) {
Ingo Molnard3be9152006-03-23 03:00:29 -08002076 mutex_unlock(&dqopt->dqio_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077 goto out_file_init;
2078 }
Ingo Molnard3be9152006-03-23 03:00:29 -08002079 mutex_unlock(&dqopt->dqio_mutex);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002080 mutex_unlock(&inode->i_mutex);
Jan Karacc334122009-01-12 17:23:05 +01002081 spin_lock(&dq_state_lock);
Jan Karaf55abc02008-08-20 17:50:32 +02002082 dqopt->flags |= dquot_state_flag(flags, type);
Jan Karacc334122009-01-12 17:23:05 +01002083 spin_unlock(&dq_state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084
2085 add_dquot_ref(sb, type);
Ingo Molnard3be9152006-03-23 03:00:29 -08002086 mutex_unlock(&dqopt->dqonoff_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087
2088 return 0;
2089
2090out_file_init:
2091 dqopt->files[type] = NULL;
2092 iput(inode);
2093out_lock:
Ingo Molnard3be9152006-03-23 03:00:29 -08002094 mutex_unlock(&dqopt->dqonoff_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095 if (oldflags != -1) {
2096 down_write(&dqopt->dqptr_sem);
2097 /* Set the flags back (in the case of accidental quotaon()
2098 * on a wrong file we don't want to mess up the flags) */
2099 inode->i_flags &= ~(S_NOATIME | S_NOQUOTA | S_IMMUTABLE);
2100 inode->i_flags |= oldflags;
2101 up_write(&dqopt->dqptr_sem);
2102 }
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002103 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104out_fmt:
2105 put_quota_format(fmt);
2106
2107 return error;
2108}
2109
Jan Kara0ff5af82008-04-28 02:14:33 -07002110/* Reenable quotas on remount RW */
2111static int vfs_quota_on_remount(struct super_block *sb, int type)
2112{
2113 struct quota_info *dqopt = sb_dqopt(sb);
2114 struct inode *inode;
2115 int ret;
Jan Karaf55abc02008-08-20 17:50:32 +02002116 unsigned int flags;
Jan Kara0ff5af82008-04-28 02:14:33 -07002117
2118 mutex_lock(&dqopt->dqonoff_mutex);
2119 if (!sb_has_quota_suspended(sb, type)) {
2120 mutex_unlock(&dqopt->dqonoff_mutex);
2121 return 0;
2122 }
Jan Kara0ff5af82008-04-28 02:14:33 -07002123 inode = dqopt->files[type];
2124 dqopt->files[type] = NULL;
Jan Karacc334122009-01-12 17:23:05 +01002125 spin_lock(&dq_state_lock);
Jan Karaf55abc02008-08-20 17:50:32 +02002126 flags = dqopt->flags & dquot_state_flag(DQUOT_USAGE_ENABLED |
2127 DQUOT_LIMITS_ENABLED, type);
2128 dqopt->flags &= ~dquot_state_flag(DQUOT_STATE_FLAGS, type);
Jan Karacc334122009-01-12 17:23:05 +01002129 spin_unlock(&dq_state_lock);
Jan Kara0ff5af82008-04-28 02:14:33 -07002130 mutex_unlock(&dqopt->dqonoff_mutex);
2131
Jan Karaf55abc02008-08-20 17:50:32 +02002132 flags = dquot_generic_flag(flags, type);
2133 ret = vfs_load_quota_inode(inode, type, dqopt->info[type].dqi_fmt_id,
2134 flags);
Jan Kara0ff5af82008-04-28 02:14:33 -07002135 iput(inode);
2136
2137 return ret;
2138}
2139
Al Viro77e69da2008-08-01 04:29:18 -04002140int vfs_quota_on_path(struct super_block *sb, int type, int format_id,
2141 struct path *path)
2142{
2143 int error = security_quota_on(path->dentry);
2144 if (error)
2145 return error;
2146 /* Quota file not on the same filesystem? */
2147 if (path->mnt->mnt_sb != sb)
2148 error = -EXDEV;
2149 else
Jan Karaf55abc02008-08-20 17:50:32 +02002150 error = vfs_load_quota_inode(path->dentry->d_inode, type,
2151 format_id, DQUOT_USAGE_ENABLED |
2152 DQUOT_LIMITS_ENABLED);
Al Viro77e69da2008-08-01 04:29:18 -04002153 return error;
2154}
Mingming Cao08d03502009-01-14 16:19:32 +01002155EXPORT_SYMBOL(vfs_quota_on_path);
Al Viro77e69da2008-08-01 04:29:18 -04002156
Al Viro82646132008-08-02 00:57:06 -04002157int vfs_quota_on(struct super_block *sb, int type, int format_id, char *name,
Jan Kara0ff5af82008-04-28 02:14:33 -07002158 int remount)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159{
Al Viro82646132008-08-02 00:57:06 -04002160 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161 int error;
2162
Jan Kara0ff5af82008-04-28 02:14:33 -07002163 if (remount)
2164 return vfs_quota_on_remount(sb, type);
2165
Al Viro82646132008-08-02 00:57:06 -04002166 error = kern_path(name, LOOKUP_FOLLOW, &path);
Al Viro77e69da2008-08-01 04:29:18 -04002167 if (!error) {
Al Viro82646132008-08-02 00:57:06 -04002168 error = vfs_quota_on_path(sb, type, format_id, &path);
2169 path_put(&path);
Al Viro77e69da2008-08-01 04:29:18 -04002170 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171 return error;
2172}
Mingming Cao08d03502009-01-14 16:19:32 +01002173EXPORT_SYMBOL(vfs_quota_on);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174
2175/*
Jan Karaf55abc02008-08-20 17:50:32 +02002176 * More powerful function for turning on quotas allowing setting
2177 * of individual quota flags
2178 */
2179int vfs_quota_enable(struct inode *inode, int type, int format_id,
2180 unsigned int flags)
2181{
2182 int ret = 0;
2183 struct super_block *sb = inode->i_sb;
2184 struct quota_info *dqopt = sb_dqopt(sb);
2185
2186 /* Just unsuspend quotas? */
2187 if (flags & DQUOT_SUSPENDED)
2188 return vfs_quota_on_remount(sb, type);
2189 if (!flags)
2190 return 0;
2191 /* Just updating flags needed? */
2192 if (sb_has_quota_loaded(sb, type)) {
2193 mutex_lock(&dqopt->dqonoff_mutex);
2194 /* Now do a reliable test... */
2195 if (!sb_has_quota_loaded(sb, type)) {
2196 mutex_unlock(&dqopt->dqonoff_mutex);
2197 goto load_quota;
2198 }
2199 if (flags & DQUOT_USAGE_ENABLED &&
2200 sb_has_quota_usage_enabled(sb, type)) {
2201 ret = -EBUSY;
2202 goto out_lock;
2203 }
2204 if (flags & DQUOT_LIMITS_ENABLED &&
2205 sb_has_quota_limits_enabled(sb, type)) {
2206 ret = -EBUSY;
2207 goto out_lock;
2208 }
Jan Karacc334122009-01-12 17:23:05 +01002209 spin_lock(&dq_state_lock);
Jan Karaf55abc02008-08-20 17:50:32 +02002210 sb_dqopt(sb)->flags |= dquot_state_flag(flags, type);
Jan Karacc334122009-01-12 17:23:05 +01002211 spin_unlock(&dq_state_lock);
Jan Karaf55abc02008-08-20 17:50:32 +02002212out_lock:
2213 mutex_unlock(&dqopt->dqonoff_mutex);
2214 return ret;
2215 }
2216
2217load_quota:
2218 return vfs_load_quota_inode(inode, type, format_id, flags);
2219}
Mingming Cao08d03502009-01-14 16:19:32 +01002220EXPORT_SYMBOL(vfs_quota_enable);
Jan Karaf55abc02008-08-20 17:50:32 +02002221
2222/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223 * This function is used when filesystem needs to initialize quotas
2224 * during mount time.
2225 */
Christoph Hellwig84de8562005-06-23 00:09:16 -07002226int vfs_quota_on_mount(struct super_block *sb, char *qf_name,
2227 int format_id, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228{
Christoph Hellwig84de8562005-06-23 00:09:16 -07002229 struct dentry *dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230 int error;
2231
Christoph Hellwig2fa389c2005-06-23 00:09:16 -07002232 dentry = lookup_one_len(qf_name, sb->s_root, strlen(qf_name));
Christoph Hellwig84de8562005-06-23 00:09:16 -07002233 if (IS_ERR(dentry))
2234 return PTR_ERR(dentry);
2235
Jan Kara154f4842005-11-28 13:44:14 -08002236 if (!dentry->d_inode) {
2237 error = -ENOENT;
2238 goto out;
2239 }
2240
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241 error = security_quota_on(dentry);
Christoph Hellwig84de8562005-06-23 00:09:16 -07002242 if (!error)
Jan Karaf55abc02008-08-20 17:50:32 +02002243 error = vfs_load_quota_inode(dentry->d_inode, type, format_id,
2244 DQUOT_USAGE_ENABLED | DQUOT_LIMITS_ENABLED);
Christoph Hellwig84de8562005-06-23 00:09:16 -07002245
Jan Kara154f4842005-11-28 13:44:14 -08002246out:
Christoph Hellwig84de8562005-06-23 00:09:16 -07002247 dput(dentry);
2248 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249}
Mingming Cao08d03502009-01-14 16:19:32 +01002250EXPORT_SYMBOL(vfs_quota_on_mount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251
Jan Karab85f4b82008-07-25 01:46:50 -07002252/* Wrapper to turn on quotas when remounting rw */
2253int vfs_dq_quota_on_remount(struct super_block *sb)
2254{
2255 int cnt;
2256 int ret = 0, err;
2257
2258 if (!sb->s_qcop || !sb->s_qcop->quota_on)
2259 return -ENOSYS;
2260 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
2261 err = sb->s_qcop->quota_on(sb, cnt, 0, NULL, 1);
2262 if (err < 0 && !ret)
2263 ret = err;
2264 }
2265 return ret;
2266}
Mingming Cao08d03502009-01-14 16:19:32 +01002267EXPORT_SYMBOL(vfs_dq_quota_on_remount);
Jan Karab85f4b82008-07-25 01:46:50 -07002268
Jan Kara12095462008-08-20 14:45:12 +02002269static inline qsize_t qbtos(qsize_t blocks)
2270{
2271 return blocks << QIF_DQBLKSIZE_BITS;
2272}
2273
2274static inline qsize_t stoqb(qsize_t space)
2275{
2276 return (space + QIF_DQBLKSIZE - 1) >> QIF_DQBLKSIZE_BITS;
2277}
2278
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279/* Generic routine for getting common part of quota structure */
2280static void do_get_dqblk(struct dquot *dquot, struct if_dqblk *di)
2281{
2282 struct mem_dqblk *dm = &dquot->dq_dqb;
2283
2284 spin_lock(&dq_data_lock);
Jan Kara12095462008-08-20 14:45:12 +02002285 di->dqb_bhardlimit = stoqb(dm->dqb_bhardlimit);
2286 di->dqb_bsoftlimit = stoqb(dm->dqb_bsoftlimit);
Mingming Caof18df222009-01-13 16:43:09 +01002287 di->dqb_curspace = dm->dqb_curspace + dm->dqb_rsvspace;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288 di->dqb_ihardlimit = dm->dqb_ihardlimit;
2289 di->dqb_isoftlimit = dm->dqb_isoftlimit;
2290 di->dqb_curinodes = dm->dqb_curinodes;
2291 di->dqb_btime = dm->dqb_btime;
2292 di->dqb_itime = dm->dqb_itime;
2293 di->dqb_valid = QIF_ALL;
2294 spin_unlock(&dq_data_lock);
2295}
2296
Jan Kara268157b2009-01-27 15:47:22 +01002297int vfs_get_dqblk(struct super_block *sb, int type, qid_t id,
2298 struct if_dqblk *di)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299{
2300 struct dquot *dquot;
2301
Jan Karacc334122009-01-12 17:23:05 +01002302 dquot = dqget(sb, id, type);
Jan Karadd6f3c62009-01-26 16:01:43 +01002303 if (!dquot)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304 return -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305 do_get_dqblk(dquot, di);
2306 dqput(dquot);
Jan Karacc334122009-01-12 17:23:05 +01002307
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308 return 0;
2309}
Mingming Cao08d03502009-01-14 16:19:32 +01002310EXPORT_SYMBOL(vfs_get_dqblk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311
2312/* Generic routine for setting common part of quota structure */
Andrew Perepechko338bf9a2008-04-28 02:14:31 -07002313static int do_set_dqblk(struct dquot *dquot, struct if_dqblk *di)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314{
2315 struct mem_dqblk *dm = &dquot->dq_dqb;
2316 int check_blim = 0, check_ilim = 0;
Andrew Perepechko338bf9a2008-04-28 02:14:31 -07002317 struct mem_dqinfo *dqi = &sb_dqopt(dquot->dq_sb)->info[dquot->dq_type];
2318
2319 if ((di->dqb_valid & QIF_BLIMITS &&
2320 (di->dqb_bhardlimit > dqi->dqi_maxblimit ||
2321 di->dqb_bsoftlimit > dqi->dqi_maxblimit)) ||
2322 (di->dqb_valid & QIF_ILIMITS &&
2323 (di->dqb_ihardlimit > dqi->dqi_maxilimit ||
2324 di->dqb_isoftlimit > dqi->dqi_maxilimit)))
2325 return -ERANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002326
2327 spin_lock(&dq_data_lock);
2328 if (di->dqb_valid & QIF_SPACE) {
Mingming Caof18df222009-01-13 16:43:09 +01002329 dm->dqb_curspace = di->dqb_curspace - dm->dqb_rsvspace;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330 check_blim = 1;
Jan Kara4d59bce2008-10-02 16:48:10 +02002331 __set_bit(DQ_LASTSET_B + QIF_SPACE_B, &dquot->dq_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002332 }
2333 if (di->dqb_valid & QIF_BLIMITS) {
Jan Kara12095462008-08-20 14:45:12 +02002334 dm->dqb_bsoftlimit = qbtos(di->dqb_bsoftlimit);
2335 dm->dqb_bhardlimit = qbtos(di->dqb_bhardlimit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336 check_blim = 1;
Jan Kara4d59bce2008-10-02 16:48:10 +02002337 __set_bit(DQ_LASTSET_B + QIF_BLIMITS_B, &dquot->dq_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338 }
2339 if (di->dqb_valid & QIF_INODES) {
2340 dm->dqb_curinodes = di->dqb_curinodes;
2341 check_ilim = 1;
Jan Kara4d59bce2008-10-02 16:48:10 +02002342 __set_bit(DQ_LASTSET_B + QIF_INODES_B, &dquot->dq_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343 }
2344 if (di->dqb_valid & QIF_ILIMITS) {
2345 dm->dqb_isoftlimit = di->dqb_isoftlimit;
2346 dm->dqb_ihardlimit = di->dqb_ihardlimit;
2347 check_ilim = 1;
Jan Kara4d59bce2008-10-02 16:48:10 +02002348 __set_bit(DQ_LASTSET_B + QIF_ILIMITS_B, &dquot->dq_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349 }
Jan Kara4d59bce2008-10-02 16:48:10 +02002350 if (di->dqb_valid & QIF_BTIME) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351 dm->dqb_btime = di->dqb_btime;
Jan Karae04a88a92009-01-07 18:07:29 -08002352 check_blim = 1;
Jan Kara4d59bce2008-10-02 16:48:10 +02002353 __set_bit(DQ_LASTSET_B + QIF_BTIME_B, &dquot->dq_flags);
2354 }
2355 if (di->dqb_valid & QIF_ITIME) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002356 dm->dqb_itime = di->dqb_itime;
Jan Karae04a88a92009-01-07 18:07:29 -08002357 check_ilim = 1;
Jan Kara4d59bce2008-10-02 16:48:10 +02002358 __set_bit(DQ_LASTSET_B + QIF_ITIME_B, &dquot->dq_flags);
2359 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360
2361 if (check_blim) {
Jan Kara268157b2009-01-27 15:47:22 +01002362 if (!dm->dqb_bsoftlimit ||
2363 dm->dqb_curspace < dm->dqb_bsoftlimit) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364 dm->dqb_btime = 0;
2365 clear_bit(DQ_BLKS_B, &dquot->dq_flags);
Jan Kara268157b2009-01-27 15:47:22 +01002366 } else if (!(di->dqb_valid & QIF_BTIME))
2367 /* Set grace only if user hasn't provided his own... */
Andrew Perepechko338bf9a2008-04-28 02:14:31 -07002368 dm->dqb_btime = get_seconds() + dqi->dqi_bgrace;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369 }
2370 if (check_ilim) {
Jan Kara268157b2009-01-27 15:47:22 +01002371 if (!dm->dqb_isoftlimit ||
2372 dm->dqb_curinodes < dm->dqb_isoftlimit) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373 dm->dqb_itime = 0;
2374 clear_bit(DQ_INODES_B, &dquot->dq_flags);
Jan Kara268157b2009-01-27 15:47:22 +01002375 } else if (!(di->dqb_valid & QIF_ITIME))
2376 /* Set grace only if user hasn't provided his own... */
Andrew Perepechko338bf9a2008-04-28 02:14:31 -07002377 dm->dqb_itime = get_seconds() + dqi->dqi_igrace;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002378 }
Jan Kara268157b2009-01-27 15:47:22 +01002379 if (dm->dqb_bhardlimit || dm->dqb_bsoftlimit || dm->dqb_ihardlimit ||
2380 dm->dqb_isoftlimit)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381 clear_bit(DQ_FAKE_B, &dquot->dq_flags);
2382 else
2383 set_bit(DQ_FAKE_B, &dquot->dq_flags);
2384 spin_unlock(&dq_data_lock);
2385 mark_dquot_dirty(dquot);
Andrew Perepechko338bf9a2008-04-28 02:14:31 -07002386
2387 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002388}
2389
Jan Kara268157b2009-01-27 15:47:22 +01002390int vfs_set_dqblk(struct super_block *sb, int type, qid_t id,
2391 struct if_dqblk *di)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392{
2393 struct dquot *dquot;
Andrew Perepechko338bf9a2008-04-28 02:14:31 -07002394 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002395
Jan Karaf55abc02008-08-20 17:50:32 +02002396 dquot = dqget(sb, id, type);
2397 if (!dquot) {
2398 rc = -ESRCH;
2399 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002400 }
Andrew Perepechko338bf9a2008-04-28 02:14:31 -07002401 rc = do_set_dqblk(dquot, di);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002402 dqput(dquot);
Jan Karaf55abc02008-08-20 17:50:32 +02002403out:
Andrew Perepechko338bf9a2008-04-28 02:14:31 -07002404 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002405}
Mingming Cao08d03502009-01-14 16:19:32 +01002406EXPORT_SYMBOL(vfs_set_dqblk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002407
2408/* Generic routine for getting common part of quota file information */
2409int vfs_get_dqinfo(struct super_block *sb, int type, struct if_dqinfo *ii)
2410{
2411 struct mem_dqinfo *mi;
2412
Ingo Molnard3be9152006-03-23 03:00:29 -08002413 mutex_lock(&sb_dqopt(sb)->dqonoff_mutex);
Jan Karaf55abc02008-08-20 17:50:32 +02002414 if (!sb_has_quota_active(sb, type)) {
Ingo Molnard3be9152006-03-23 03:00:29 -08002415 mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002416 return -ESRCH;
2417 }
2418 mi = sb_dqopt(sb)->info + type;
2419 spin_lock(&dq_data_lock);
2420 ii->dqi_bgrace = mi->dqi_bgrace;
2421 ii->dqi_igrace = mi->dqi_igrace;
2422 ii->dqi_flags = mi->dqi_flags & DQF_MASK;
2423 ii->dqi_valid = IIF_ALL;
2424 spin_unlock(&dq_data_lock);
Ingo Molnard3be9152006-03-23 03:00:29 -08002425 mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002426 return 0;
2427}
Mingming Cao08d03502009-01-14 16:19:32 +01002428EXPORT_SYMBOL(vfs_get_dqinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002429
2430/* Generic routine for setting common part of quota file information */
2431int vfs_set_dqinfo(struct super_block *sb, int type, struct if_dqinfo *ii)
2432{
2433 struct mem_dqinfo *mi;
Jan Karaf55abc02008-08-20 17:50:32 +02002434 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002435
Ingo Molnard3be9152006-03-23 03:00:29 -08002436 mutex_lock(&sb_dqopt(sb)->dqonoff_mutex);
Jan Karaf55abc02008-08-20 17:50:32 +02002437 if (!sb_has_quota_active(sb, type)) {
2438 err = -ESRCH;
2439 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002440 }
2441 mi = sb_dqopt(sb)->info + type;
2442 spin_lock(&dq_data_lock);
2443 if (ii->dqi_valid & IIF_BGRACE)
2444 mi->dqi_bgrace = ii->dqi_bgrace;
2445 if (ii->dqi_valid & IIF_IGRACE)
2446 mi->dqi_igrace = ii->dqi_igrace;
2447 if (ii->dqi_valid & IIF_FLAGS)
Jan Kara268157b2009-01-27 15:47:22 +01002448 mi->dqi_flags = (mi->dqi_flags & ~DQF_MASK) |
2449 (ii->dqi_flags & DQF_MASK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450 spin_unlock(&dq_data_lock);
2451 mark_info_dirty(sb, type);
2452 /* Force write to disk */
2453 sb->dq_op->write_info(sb, type);
Jan Karaf55abc02008-08-20 17:50:32 +02002454out:
Ingo Molnard3be9152006-03-23 03:00:29 -08002455 mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
Jan Karaf55abc02008-08-20 17:50:32 +02002456 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002457}
Mingming Cao08d03502009-01-14 16:19:32 +01002458EXPORT_SYMBOL(vfs_set_dqinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459
2460struct quotactl_ops vfs_quotactl_ops = {
2461 .quota_on = vfs_quota_on,
2462 .quota_off = vfs_quota_off,
2463 .quota_sync = vfs_quota_sync,
2464 .get_info = vfs_get_dqinfo,
2465 .set_info = vfs_set_dqinfo,
2466 .get_dqblk = vfs_get_dqblk,
2467 .set_dqblk = vfs_set_dqblk
2468};
2469
2470static ctl_table fs_dqstats_table[] = {
2471 {
2472 .ctl_name = FS_DQ_LOOKUPS,
2473 .procname = "lookups",
2474 .data = &dqstats.lookups,
2475 .maxlen = sizeof(int),
2476 .mode = 0444,
2477 .proc_handler = &proc_dointvec,
2478 },
2479 {
2480 .ctl_name = FS_DQ_DROPS,
2481 .procname = "drops",
2482 .data = &dqstats.drops,
2483 .maxlen = sizeof(int),
2484 .mode = 0444,
2485 .proc_handler = &proc_dointvec,
2486 },
2487 {
2488 .ctl_name = FS_DQ_READS,
2489 .procname = "reads",
2490 .data = &dqstats.reads,
2491 .maxlen = sizeof(int),
2492 .mode = 0444,
2493 .proc_handler = &proc_dointvec,
2494 },
2495 {
2496 .ctl_name = FS_DQ_WRITES,
2497 .procname = "writes",
2498 .data = &dqstats.writes,
2499 .maxlen = sizeof(int),
2500 .mode = 0444,
2501 .proc_handler = &proc_dointvec,
2502 },
2503 {
2504 .ctl_name = FS_DQ_CACHE_HITS,
2505 .procname = "cache_hits",
2506 .data = &dqstats.cache_hits,
2507 .maxlen = sizeof(int),
2508 .mode = 0444,
2509 .proc_handler = &proc_dointvec,
2510 },
2511 {
2512 .ctl_name = FS_DQ_ALLOCATED,
2513 .procname = "allocated_dquots",
2514 .data = &dqstats.allocated_dquots,
2515 .maxlen = sizeof(int),
2516 .mode = 0444,
2517 .proc_handler = &proc_dointvec,
2518 },
2519 {
2520 .ctl_name = FS_DQ_FREE,
2521 .procname = "free_dquots",
2522 .data = &dqstats.free_dquots,
2523 .maxlen = sizeof(int),
2524 .mode = 0444,
2525 .proc_handler = &proc_dointvec,
2526 },
2527 {
2528 .ctl_name = FS_DQ_SYNCS,
2529 .procname = "syncs",
2530 .data = &dqstats.syncs,
2531 .maxlen = sizeof(int),
2532 .mode = 0444,
2533 .proc_handler = &proc_dointvec,
2534 },
Jan Kara8e893462007-10-16 23:29:31 -07002535#ifdef CONFIG_PRINT_QUOTA_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07002536 {
2537 .ctl_name = FS_DQ_WARNINGS,
2538 .procname = "warnings",
2539 .data = &flag_print_warnings,
2540 .maxlen = sizeof(int),
2541 .mode = 0644,
2542 .proc_handler = &proc_dointvec,
2543 },
Jan Kara8e893462007-10-16 23:29:31 -07002544#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002545 { .ctl_name = 0 },
2546};
2547
2548static ctl_table fs_table[] = {
2549 {
2550 .ctl_name = FS_DQSTATS,
2551 .procname = "quota",
2552 .mode = 0555,
2553 .child = fs_dqstats_table,
2554 },
2555 { .ctl_name = 0 },
2556};
2557
2558static ctl_table sys_table[] = {
2559 {
2560 .ctl_name = CTL_FS,
2561 .procname = "fs",
2562 .mode = 0555,
2563 .child = fs_table,
2564 },
2565 { .ctl_name = 0 },
2566};
2567
2568static int __init dquot_init(void)
2569{
2570 int i;
2571 unsigned long nr_hash, order;
2572
2573 printk(KERN_NOTICE "VFS: Disk quotas %s\n", __DQUOT_VERSION__);
2574
Eric W. Biederman0b4d4142007-02-14 00:34:09 -08002575 register_sysctl_table(sys_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002576
Paul Mundt20c2df82007-07-20 10:11:58 +09002577 dquot_cachep = kmem_cache_create("dquot",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002578 sizeof(struct dquot), sizeof(unsigned long) * 4,
Paul Jacksonfffb60f2006-03-24 03:16:06 -08002579 (SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT|
2580 SLAB_MEM_SPREAD|SLAB_PANIC),
Paul Mundt20c2df82007-07-20 10:11:58 +09002581 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002582
2583 order = 0;
2584 dquot_hash = (struct hlist_head *)__get_free_pages(GFP_ATOMIC, order);
2585 if (!dquot_hash)
2586 panic("Cannot create dquot hash table");
2587
2588 /* Find power-of-two hlist_heads which can fit into allocation */
2589 nr_hash = (1UL << order) * PAGE_SIZE / sizeof(struct hlist_head);
2590 dq_hash_bits = 0;
2591 do {
2592 dq_hash_bits++;
2593 } while (nr_hash >> dq_hash_bits);
2594 dq_hash_bits--;
2595
2596 nr_hash = 1UL << dq_hash_bits;
2597 dq_hash_mask = nr_hash - 1;
2598 for (i = 0; i < nr_hash; i++)
2599 INIT_HLIST_HEAD(dquot_hash + i);
2600
2601 printk("Dquot-cache hash table entries: %ld (order %ld, %ld bytes)\n",
2602 nr_hash, order, (PAGE_SIZE << order));
2603
Rusty Russell8e1f9362007-07-17 04:03:17 -07002604 register_shrinker(&dqcache_shrinker);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002605
Jan Kara8e893462007-10-16 23:29:31 -07002606#ifdef CONFIG_QUOTA_NETLINK_INTERFACE
2607 if (genl_register_family(&quota_genl_family) != 0)
Jan Kara268157b2009-01-27 15:47:22 +01002608 printk(KERN_ERR
2609 "VFS: Failed to create quota netlink interface.\n");
Jan Kara8e893462007-10-16 23:29:31 -07002610#endif
2611
Linus Torvalds1da177e2005-04-16 15:20:36 -07002612 return 0;
2613}
2614module_init(dquot_init);