blob: dea86abdf2e7fe8ca6589dc0ba22210357a28d01 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Implementation of the diskquota system for the LINUX operating system. QUOTA
3 * is implemented using the BSD system call interface as the means of
4 * communication with the user level. This file contains the generic routines
5 * called by the different filesystems on allocation of an inode or block.
6 * These routines take care of the administration needed to have a consistent
7 * diskquota tracking system. The ideas of both user and group quotas are based
8 * on the Melbourne quota system as used on BSD derived systems. The internal
9 * implementation is based on one of the several variants of the LINUX
10 * inode-subsystem with added complexity of the diskquota system.
11 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070012 * Author: Marco van Wieringen <mvw@planets.elm.net>
13 *
14 * Fixes: Dmitry Gorodchanin <pgmdsg@ibi.com>, 11 Feb 96
15 *
16 * Revised list management to avoid races
17 * -- Bill Hawes, <whawes@star.net>, 9/98
18 *
19 * Fixed races in dquot_transfer(), dqget() and dquot_alloc_...().
20 * As the consequence the locking was moved from dquot_decr_...(),
21 * dquot_incr_...() to calling functions.
22 * invalidate_dquots() now writes modified dquots.
23 * Serialized quota_off() and quota_on() for mount point.
24 * Fixed a few bugs in grow_dquots().
25 * Fixed deadlock in write_dquot() - we no longer account quotas on
26 * quota files
27 * remove_dquot_ref() moved to inode.c - it now traverses through inodes
28 * add_dquot_ref() restarts after blocking
29 * Added check for bogus uid and fixed check for group in quotactl.
30 * Jan Kara, <jack@suse.cz>, sponsored by SuSE CR, 10-11/99
31 *
32 * Used struct list_head instead of own list struct
33 * Invalidation of referenced dquots is no longer possible
34 * Improved free_dquots list management
35 * Quota and i_blocks are now updated in one place to avoid races
36 * Warnings are now delayed so we won't block in critical section
37 * Write updated not to require dquot lock
38 * Jan Kara, <jack@suse.cz>, 9/2000
39 *
40 * Added dynamic quota structure allocation
41 * Jan Kara <jack@suse.cz> 12/2000
42 *
43 * Rewritten quota interface. Implemented new quota format and
44 * formats registering.
45 * Jan Kara, <jack@suse.cz>, 2001,2002
46 *
47 * New SMP locking.
48 * Jan Kara, <jack@suse.cz>, 10/2002
49 *
50 * Added journalled quota support, fix lock inversion problems
51 * Jan Kara, <jack@suse.cz>, 2003,2004
52 *
53 * (C) Copyright 1994 - 1997 Marco van Wieringen
54 */
55
56#include <linux/errno.h>
57#include <linux/kernel.h>
58#include <linux/fs.h>
59#include <linux/mount.h>
60#include <linux/mm.h>
61#include <linux/time.h>
62#include <linux/types.h>
63#include <linux/string.h>
64#include <linux/fcntl.h>
65#include <linux/stat.h>
66#include <linux/tty.h>
67#include <linux/file.h>
68#include <linux/slab.h>
69#include <linux/sysctl.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070070#include <linux/init.h>
71#include <linux/module.h>
72#include <linux/proc_fs.h>
73#include <linux/security.h>
74#include <linux/kmod.h>
75#include <linux/namei.h>
76#include <linux/buffer_head.h>
Randy Dunlap16f7e0f2006-01-11 12:17:46 -080077#include <linux/capability.h>
Adrian Bunkbe586ba2005-11-07 00:59:35 -080078#include <linux/quotaops.h>
Christoph Hellwigfb58b732007-02-12 00:51:57 -080079#include <linux/writeback.h> /* for inode_lock, oddly enough.. */
Linus Torvalds1da177e2005-04-16 15:20:36 -070080
81#include <asm/uaccess.h>
82
83#define __DQUOT_PARANOIA
84
85/*
Jan Karacc334122009-01-12 17:23:05 +010086 * There are three quota SMP locks. dq_list_lock protects all lists with quotas
87 * and quota formats, dqstats structure containing statistics about the lists
88 * dq_data_lock protects data from dq_dqb and also mem_dqinfo structures and
89 * also guards consistency of dquot->dq_dqb with inode->i_blocks, i_bytes.
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 * i_blocks and i_bytes updates itself are guarded by i_lock acquired directly
Jan Karacc334122009-01-12 17:23:05 +010091 * in inode_add_bytes() and inode_sub_bytes(). dq_state_lock protects
92 * modifications of quota state (on quotaon and quotaoff) and readers who care
93 * about latest values take it as well.
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 *
Jan Karacc334122009-01-12 17:23:05 +010095 * The spinlock ordering is hence: dq_data_lock > dq_list_lock > i_lock,
96 * dq_list_lock > dq_state_lock
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 *
98 * Note that some things (eg. sb pointer, type, id) doesn't change during
99 * the life of the dquot structure and so needn't to be protected by a lock
100 *
101 * Any operation working on dquots via inode pointers must hold dqptr_sem. If
102 * operation is just reading pointers from inode (or not using them at all) the
103 * read lock is enough. If pointers are altered function must hold write lock
104 * (these locking rules also apply for S_NOQUOTA flag in the inode - note that
Jan Karacc334122009-01-12 17:23:05 +0100105 * for altering the flag i_mutex is also needed).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 *
Ingo Molnard3be9152006-03-23 03:00:29 -0800107 * Each dquot has its dq_lock mutex. Locked dquots might not be referenced
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 * from inodes (dquot_alloc_space() and such don't check the dq_lock).
109 * Currently dquot is locked only when it is being read to memory (or space for
110 * it is being allocated) on the first dqget() and when it is being released on
111 * the last dqput(). The allocation and release oparations are serialized by
112 * the dq_lock and by checking the use count in dquot_release(). Write
113 * operations on dquots don't hold dq_lock as they copy data under dq_data_lock
114 * spinlock to internal buffers before writing.
115 *
116 * Lock ordering (including related VFS locks) is the following:
Ingo Molnard3be9152006-03-23 03:00:29 -0800117 * i_mutex > dqonoff_sem > journal_lock > dqptr_sem > dquot->dq_lock >
118 * dqio_mutex
Jan Karacc334122009-01-12 17:23:05 +0100119 * The lock ordering of dqptr_sem imposed by quota code is only dqonoff_sem >
120 * dqptr_sem. But filesystem has to count with the fact that functions such as
121 * dquot_alloc_space() acquire dqptr_sem and they usually have to be called
122 * from inside a transaction to keep filesystem consistency after a crash. Also
123 * filesystems usually want to do some IO on dquot from ->mark_dirty which is
124 * called with dqptr_sem held.
Ingo Molnard3be9152006-03-23 03:00:29 -0800125 * i_mutex on quota files is special (it's below dqio_mutex)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 */
127
Jan Karac5166102009-01-26 15:32:46 +0100128static __cacheline_aligned_in_smp DEFINE_SPINLOCK(dq_list_lock);
129static __cacheline_aligned_in_smp DEFINE_SPINLOCK(dq_state_lock);
130__cacheline_aligned_in_smp DEFINE_SPINLOCK(dq_data_lock);
Mingming Cao08d03502009-01-14 16:19:32 +0100131EXPORT_SYMBOL(dq_data_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132
133static char *quotatypes[] = INITQFNAMES;
134static struct quota_format_type *quota_formats; /* List of registered formats */
135static struct quota_module_name module_names[] = INIT_QUOTA_MODULE_NAMES;
136
137/* SLAB cache for dquot structures */
Christoph Lametere18b8902006-12-06 20:33:20 -0800138static struct kmem_cache *dquot_cachep;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139
140int register_quota_format(struct quota_format_type *fmt)
141{
142 spin_lock(&dq_list_lock);
143 fmt->qf_next = quota_formats;
144 quota_formats = fmt;
145 spin_unlock(&dq_list_lock);
146 return 0;
147}
Mingming Cao08d03502009-01-14 16:19:32 +0100148EXPORT_SYMBOL(register_quota_format);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149
150void unregister_quota_format(struct quota_format_type *fmt)
151{
152 struct quota_format_type **actqf;
153
154 spin_lock(&dq_list_lock);
Jan Kara268157b2009-01-27 15:47:22 +0100155 for (actqf = &quota_formats; *actqf && *actqf != fmt;
156 actqf = &(*actqf)->qf_next)
157 ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 if (*actqf)
159 *actqf = (*actqf)->qf_next;
160 spin_unlock(&dq_list_lock);
161}
Mingming Cao08d03502009-01-14 16:19:32 +0100162EXPORT_SYMBOL(unregister_quota_format);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163
164static struct quota_format_type *find_quota_format(int id)
165{
166 struct quota_format_type *actqf;
167
168 spin_lock(&dq_list_lock);
Jan Kara268157b2009-01-27 15:47:22 +0100169 for (actqf = quota_formats; actqf && actqf->qf_fmt_id != id;
170 actqf = actqf->qf_next)
171 ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 if (!actqf || !try_module_get(actqf->qf_owner)) {
173 int qm;
174
175 spin_unlock(&dq_list_lock);
176
Jan Kara268157b2009-01-27 15:47:22 +0100177 for (qm = 0; module_names[qm].qm_fmt_id &&
178 module_names[qm].qm_fmt_id != id; qm++)
179 ;
180 if (!module_names[qm].qm_fmt_id ||
181 request_module(module_names[qm].qm_mod_name))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 return NULL;
183
184 spin_lock(&dq_list_lock);
Jan Kara268157b2009-01-27 15:47:22 +0100185 for (actqf = quota_formats; actqf && actqf->qf_fmt_id != id;
186 actqf = actqf->qf_next)
187 ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 if (actqf && !try_module_get(actqf->qf_owner))
189 actqf = NULL;
190 }
191 spin_unlock(&dq_list_lock);
192 return actqf;
193}
194
195static void put_quota_format(struct quota_format_type *fmt)
196{
197 module_put(fmt->qf_owner);
198}
199
200/*
201 * Dquot List Management:
202 * The quota code uses three lists for dquot management: the inuse_list,
203 * free_dquots, and dquot_hash[] array. A single dquot structure may be
204 * on all three lists, depending on its current state.
205 *
206 * All dquots are placed to the end of inuse_list when first created, and this
207 * list is used for invalidate operation, which must look at every dquot.
208 *
209 * Unused dquots (dq_count == 0) are added to the free_dquots list when freed,
210 * and this list is searched whenever we need an available dquot. Dquots are
211 * removed from the list as soon as they are used again, and
212 * dqstats.free_dquots gives the number of dquots on the list. When
213 * dquot is invalidated it's completely released from memory.
214 *
215 * Dquots with a specific identity (device, type and id) are placed on
216 * one of the dquot_hash[] hash chains. The provides an efficient search
217 * mechanism to locate a specific dquot.
218 */
219
220static LIST_HEAD(inuse_list);
221static LIST_HEAD(free_dquots);
222static unsigned int dq_hash_bits, dq_hash_mask;
223static struct hlist_head *dquot_hash;
224
225struct dqstats dqstats;
Mingming Cao08d03502009-01-14 16:19:32 +0100226EXPORT_SYMBOL(dqstats);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228static inline unsigned int
229hashfn(const struct super_block *sb, unsigned int id, int type)
230{
231 unsigned long tmp;
232
233 tmp = (((unsigned long)sb>>L1_CACHE_SHIFT) ^ id) * (MAXQUOTAS - type);
234 return (tmp + (tmp >> dq_hash_bits)) & dq_hash_mask;
235}
236
237/*
238 * Following list functions expect dq_list_lock to be held
239 */
240static inline void insert_dquot_hash(struct dquot *dquot)
241{
Jan Kara268157b2009-01-27 15:47:22 +0100242 struct hlist_head *head;
243 head = dquot_hash + hashfn(dquot->dq_sb, dquot->dq_id, dquot->dq_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 hlist_add_head(&dquot->dq_hash, head);
245}
246
247static inline void remove_dquot_hash(struct dquot *dquot)
248{
249 hlist_del_init(&dquot->dq_hash);
250}
251
Jan Kara7a2435d2009-01-27 01:47:11 +0100252static struct dquot *find_dquot(unsigned int hashent, struct super_block *sb,
253 unsigned int id, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254{
255 struct hlist_node *node;
256 struct dquot *dquot;
257
258 hlist_for_each (node, dquot_hash+hashent) {
259 dquot = hlist_entry(node, struct dquot, dq_hash);
Jan Kara268157b2009-01-27 15:47:22 +0100260 if (dquot->dq_sb == sb && dquot->dq_id == id &&
261 dquot->dq_type == type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 return dquot;
263 }
Jan Karadd6f3c62009-01-26 16:01:43 +0100264 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265}
266
267/* Add a dquot to the tail of the free list */
268static inline void put_dquot_last(struct dquot *dquot)
269{
Akinobu Mita8e130592006-06-26 00:24:37 -0700270 list_add_tail(&dquot->dq_free, &free_dquots);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 dqstats.free_dquots++;
272}
273
274static inline void remove_free_dquot(struct dquot *dquot)
275{
276 if (list_empty(&dquot->dq_free))
277 return;
278 list_del_init(&dquot->dq_free);
279 dqstats.free_dquots--;
280}
281
282static inline void put_inuse(struct dquot *dquot)
283{
284 /* We add to the back of inuse list so we don't have to restart
285 * when traversing this list and we block */
Akinobu Mita8e130592006-06-26 00:24:37 -0700286 list_add_tail(&dquot->dq_inuse, &inuse_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 dqstats.allocated_dquots++;
288}
289
290static inline void remove_inuse(struct dquot *dquot)
291{
292 dqstats.allocated_dquots--;
293 list_del(&dquot->dq_inuse);
294}
295/*
296 * End of list functions needing dq_list_lock
297 */
298
299static void wait_on_dquot(struct dquot *dquot)
300{
Ingo Molnard3be9152006-03-23 03:00:29 -0800301 mutex_lock(&dquot->dq_lock);
302 mutex_unlock(&dquot->dq_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303}
304
Jan Kara03f6e922008-04-28 02:14:32 -0700305static inline int dquot_dirty(struct dquot *dquot)
306{
307 return test_bit(DQ_MOD_B, &dquot->dq_flags);
308}
309
310static inline int mark_dquot_dirty(struct dquot *dquot)
311{
312 return dquot->dq_sb->dq_op->mark_dirty(dquot);
313}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314
315int dquot_mark_dquot_dirty(struct dquot *dquot)
316{
317 spin_lock(&dq_list_lock);
318 if (!test_and_set_bit(DQ_MOD_B, &dquot->dq_flags))
319 list_add(&dquot->dq_dirty, &sb_dqopt(dquot->dq_sb)->
320 info[dquot->dq_type].dqi_dirty_list);
321 spin_unlock(&dq_list_lock);
322 return 0;
323}
Mingming Cao08d03502009-01-14 16:19:32 +0100324EXPORT_SYMBOL(dquot_mark_dquot_dirty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325
Dmitry Monakhovdc52dd32009-12-14 15:21:15 +0300326/* Dirtify all the dquots - this can block when journalling */
327static inline int mark_all_dquot_dirty(struct dquot * const *dquot)
328{
329 int ret, err, cnt;
330
331 ret = err = 0;
332 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
333 if (dquot[cnt])
334 /* Even in case of error we have to continue */
335 ret = mark_dquot_dirty(dquot[cnt]);
336 if (!err)
337 err = ret;
338 }
339 return err;
340}
341
342static inline void dqput_all(struct dquot **dquot)
343{
344 unsigned int cnt;
345
346 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
347 dqput(dquot[cnt]);
348}
349
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350/* This function needs dq_list_lock */
351static inline int clear_dquot_dirty(struct dquot *dquot)
352{
353 if (!test_and_clear_bit(DQ_MOD_B, &dquot->dq_flags))
354 return 0;
355 list_del_init(&dquot->dq_dirty);
356 return 1;
357}
358
359void mark_info_dirty(struct super_block *sb, int type)
360{
361 set_bit(DQF_INFO_DIRTY_B, &sb_dqopt(sb)->info[type].dqi_flags);
362}
363EXPORT_SYMBOL(mark_info_dirty);
364
365/*
366 * Read dquot from disk and alloc space for it
367 */
368
369int dquot_acquire(struct dquot *dquot)
370{
371 int ret = 0, ret2 = 0;
372 struct quota_info *dqopt = sb_dqopt(dquot->dq_sb);
373
Ingo Molnard3be9152006-03-23 03:00:29 -0800374 mutex_lock(&dquot->dq_lock);
375 mutex_lock(&dqopt->dqio_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 if (!test_bit(DQ_READ_B, &dquot->dq_flags))
377 ret = dqopt->ops[dquot->dq_type]->read_dqblk(dquot);
378 if (ret < 0)
379 goto out_iolock;
380 set_bit(DQ_READ_B, &dquot->dq_flags);
381 /* Instantiate dquot if needed */
382 if (!test_bit(DQ_ACTIVE_B, &dquot->dq_flags) && !dquot->dq_off) {
383 ret = dqopt->ops[dquot->dq_type]->commit_dqblk(dquot);
384 /* Write the info if needed */
Jan Kara268157b2009-01-27 15:47:22 +0100385 if (info_dirty(&dqopt->info[dquot->dq_type])) {
386 ret2 = dqopt->ops[dquot->dq_type]->write_file_info(
387 dquot->dq_sb, dquot->dq_type);
388 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 if (ret < 0)
390 goto out_iolock;
391 if (ret2 < 0) {
392 ret = ret2;
393 goto out_iolock;
394 }
395 }
396 set_bit(DQ_ACTIVE_B, &dquot->dq_flags);
397out_iolock:
Ingo Molnard3be9152006-03-23 03:00:29 -0800398 mutex_unlock(&dqopt->dqio_mutex);
399 mutex_unlock(&dquot->dq_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 return ret;
401}
Mingming Cao08d03502009-01-14 16:19:32 +0100402EXPORT_SYMBOL(dquot_acquire);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403
404/*
405 * Write dquot to disk
406 */
407int dquot_commit(struct dquot *dquot)
408{
409 int ret = 0, ret2 = 0;
410 struct quota_info *dqopt = sb_dqopt(dquot->dq_sb);
411
Ingo Molnard3be9152006-03-23 03:00:29 -0800412 mutex_lock(&dqopt->dqio_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 spin_lock(&dq_list_lock);
414 if (!clear_dquot_dirty(dquot)) {
415 spin_unlock(&dq_list_lock);
416 goto out_sem;
417 }
418 spin_unlock(&dq_list_lock);
419 /* Inactive dquot can be only if there was error during read/init
420 * => we have better not writing it */
421 if (test_bit(DQ_ACTIVE_B, &dquot->dq_flags)) {
422 ret = dqopt->ops[dquot->dq_type]->commit_dqblk(dquot);
Jan Kara268157b2009-01-27 15:47:22 +0100423 if (info_dirty(&dqopt->info[dquot->dq_type])) {
424 ret2 = dqopt->ops[dquot->dq_type]->write_file_info(
425 dquot->dq_sb, dquot->dq_type);
426 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 if (ret >= 0)
428 ret = ret2;
429 }
430out_sem:
Ingo Molnard3be9152006-03-23 03:00:29 -0800431 mutex_unlock(&dqopt->dqio_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 return ret;
433}
Mingming Cao08d03502009-01-14 16:19:32 +0100434EXPORT_SYMBOL(dquot_commit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435
436/*
437 * Release dquot
438 */
439int dquot_release(struct dquot *dquot)
440{
441 int ret = 0, ret2 = 0;
442 struct quota_info *dqopt = sb_dqopt(dquot->dq_sb);
443
Ingo Molnard3be9152006-03-23 03:00:29 -0800444 mutex_lock(&dquot->dq_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 /* Check whether we are not racing with some other dqget() */
446 if (atomic_read(&dquot->dq_count) > 1)
447 goto out_dqlock;
Ingo Molnard3be9152006-03-23 03:00:29 -0800448 mutex_lock(&dqopt->dqio_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 if (dqopt->ops[dquot->dq_type]->release_dqblk) {
450 ret = dqopt->ops[dquot->dq_type]->release_dqblk(dquot);
451 /* Write the info */
Jan Kara268157b2009-01-27 15:47:22 +0100452 if (info_dirty(&dqopt->info[dquot->dq_type])) {
453 ret2 = dqopt->ops[dquot->dq_type]->write_file_info(
454 dquot->dq_sb, dquot->dq_type);
455 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 if (ret >= 0)
457 ret = ret2;
458 }
459 clear_bit(DQ_ACTIVE_B, &dquot->dq_flags);
Ingo Molnard3be9152006-03-23 03:00:29 -0800460 mutex_unlock(&dqopt->dqio_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461out_dqlock:
Ingo Molnard3be9152006-03-23 03:00:29 -0800462 mutex_unlock(&dquot->dq_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 return ret;
464}
Mingming Cao08d03502009-01-14 16:19:32 +0100465EXPORT_SYMBOL(dquot_release);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466
Jan Kara7d9056b2008-11-25 15:31:32 +0100467void dquot_destroy(struct dquot *dquot)
Jan Kara74f783a2008-08-19 14:51:22 +0200468{
469 kmem_cache_free(dquot_cachep, dquot);
470}
Jan Kara7d9056b2008-11-25 15:31:32 +0100471EXPORT_SYMBOL(dquot_destroy);
Jan Kara74f783a2008-08-19 14:51:22 +0200472
473static inline void do_destroy_dquot(struct dquot *dquot)
474{
475 dquot->dq_sb->dq_op->destroy_dquot(dquot);
476}
477
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478/* Invalidate all dquots on the list. Note that this function is called after
479 * quota is disabled and pointers from inodes removed so there cannot be new
Jan Kara6362e4d2006-03-23 03:00:17 -0800480 * quota users. There can still be some users of quotas due to inodes being
481 * just deleted or pruned by prune_icache() (those are not attached to any
Jan Karacc334122009-01-12 17:23:05 +0100482 * list) or parallel quotactl call. We have to wait for such users.
Jan Kara6362e4d2006-03-23 03:00:17 -0800483 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484static void invalidate_dquots(struct super_block *sb, int type)
485{
Domen Puncerc33ed272005-06-25 14:59:36 -0700486 struct dquot *dquot, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487
Jan Kara6362e4d2006-03-23 03:00:17 -0800488restart:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 spin_lock(&dq_list_lock);
Domen Puncerc33ed272005-06-25 14:59:36 -0700490 list_for_each_entry_safe(dquot, tmp, &inuse_list, dq_inuse) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 if (dquot->dq_sb != sb)
492 continue;
493 if (dquot->dq_type != type)
494 continue;
Jan Kara6362e4d2006-03-23 03:00:17 -0800495 /* Wait for dquot users */
496 if (atomic_read(&dquot->dq_count)) {
497 DEFINE_WAIT(wait);
498
499 atomic_inc(&dquot->dq_count);
500 prepare_to_wait(&dquot->dq_wait_unused, &wait,
501 TASK_UNINTERRUPTIBLE);
502 spin_unlock(&dq_list_lock);
503 /* Once dqput() wakes us up, we know it's time to free
504 * the dquot.
505 * IMPORTANT: we rely on the fact that there is always
506 * at most one process waiting for dquot to free.
507 * Otherwise dq_count would be > 1 and we would never
508 * wake up.
509 */
510 if (atomic_read(&dquot->dq_count) > 1)
511 schedule();
512 finish_wait(&dquot->dq_wait_unused, &wait);
513 dqput(dquot);
514 /* At this moment dquot() need not exist (it could be
515 * reclaimed by prune_dqcache(). Hence we must
516 * restart. */
517 goto restart;
518 }
519 /*
520 * Quota now has no users and it has been written on last
521 * dqput()
522 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 remove_dquot_hash(dquot);
524 remove_free_dquot(dquot);
525 remove_inuse(dquot);
Jan Kara74f783a2008-08-19 14:51:22 +0200526 do_destroy_dquot(dquot);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 }
528 spin_unlock(&dq_list_lock);
529}
530
Jan Kara12c77522008-10-20 17:05:00 +0200531/* Call callback for every active dquot on given filesystem */
532int dquot_scan_active(struct super_block *sb,
533 int (*fn)(struct dquot *dquot, unsigned long priv),
534 unsigned long priv)
535{
536 struct dquot *dquot, *old_dquot = NULL;
537 int ret = 0;
538
539 mutex_lock(&sb_dqopt(sb)->dqonoff_mutex);
540 spin_lock(&dq_list_lock);
541 list_for_each_entry(dquot, &inuse_list, dq_inuse) {
542 if (!test_bit(DQ_ACTIVE_B, &dquot->dq_flags))
543 continue;
544 if (dquot->dq_sb != sb)
545 continue;
546 /* Now we have active dquot so we can just increase use count */
547 atomic_inc(&dquot->dq_count);
548 dqstats.lookups++;
549 spin_unlock(&dq_list_lock);
550 dqput(old_dquot);
551 old_dquot = dquot;
552 ret = fn(dquot, priv);
553 if (ret < 0)
554 goto out;
555 spin_lock(&dq_list_lock);
556 /* We are safe to continue now because our dquot could not
557 * be moved out of the inuse list while we hold the reference */
558 }
559 spin_unlock(&dq_list_lock);
560out:
561 dqput(old_dquot);
562 mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
563 return ret;
564}
Mingming Cao08d03502009-01-14 16:19:32 +0100565EXPORT_SYMBOL(dquot_scan_active);
Jan Kara12c77522008-10-20 17:05:00 +0200566
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567int vfs_quota_sync(struct super_block *sb, int type)
568{
569 struct list_head *dirty;
570 struct dquot *dquot;
571 struct quota_info *dqopt = sb_dqopt(sb);
572 int cnt;
573
Ingo Molnard3be9152006-03-23 03:00:29 -0800574 mutex_lock(&dqopt->dqonoff_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
576 if (type != -1 && cnt != type)
577 continue;
Jan Karaf55abc02008-08-20 17:50:32 +0200578 if (!sb_has_quota_active(sb, cnt))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 continue;
580 spin_lock(&dq_list_lock);
581 dirty = &dqopt->info[cnt].dqi_dirty_list;
582 while (!list_empty(dirty)) {
Jan Kara268157b2009-01-27 15:47:22 +0100583 dquot = list_first_entry(dirty, struct dquot,
584 dq_dirty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 /* Dirty and inactive can be only bad dquot... */
586 if (!test_bit(DQ_ACTIVE_B, &dquot->dq_flags)) {
587 clear_dquot_dirty(dquot);
588 continue;
589 }
590 /* Now we have active dquot from which someone is
591 * holding reference so we can safely just increase
592 * use count */
593 atomic_inc(&dquot->dq_count);
594 dqstats.lookups++;
595 spin_unlock(&dq_list_lock);
596 sb->dq_op->write_dquot(dquot);
597 dqput(dquot);
598 spin_lock(&dq_list_lock);
599 }
600 spin_unlock(&dq_list_lock);
601 }
602
603 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
Jan Karaf55abc02008-08-20 17:50:32 +0200604 if ((cnt == type || type == -1) && sb_has_quota_active(sb, cnt)
605 && info_dirty(&dqopt->info[cnt]))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 sb->dq_op->write_info(sb, cnt);
607 spin_lock(&dq_list_lock);
608 dqstats.syncs++;
609 spin_unlock(&dq_list_lock);
Ingo Molnard3be9152006-03-23 03:00:29 -0800610 mutex_unlock(&dqopt->dqonoff_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611
612 return 0;
613}
Mingming Cao08d03502009-01-14 16:19:32 +0100614EXPORT_SYMBOL(vfs_quota_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615
616/* Free unused dquots from cache */
617static void prune_dqcache(int count)
618{
619 struct list_head *head;
620 struct dquot *dquot;
621
622 head = free_dquots.prev;
623 while (head != &free_dquots && count) {
624 dquot = list_entry(head, struct dquot, dq_free);
625 remove_dquot_hash(dquot);
626 remove_free_dquot(dquot);
627 remove_inuse(dquot);
Jan Kara74f783a2008-08-19 14:51:22 +0200628 do_destroy_dquot(dquot);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 count--;
630 head = free_dquots.prev;
631 }
632}
633
634/*
635 * This is called from kswapd when we think we need some
636 * more memory
637 */
638
Al Viro27496a82005-10-21 03:20:48 -0400639static int shrink_dqcache_memory(int nr, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640{
641 if (nr) {
642 spin_lock(&dq_list_lock);
643 prune_dqcache(nr);
644 spin_unlock(&dq_list_lock);
645 }
646 return (dqstats.free_dquots / 100) * sysctl_vfs_cache_pressure;
647}
648
Rusty Russell8e1f9362007-07-17 04:03:17 -0700649static struct shrinker dqcache_shrinker = {
650 .shrink = shrink_dqcache_memory,
651 .seeks = DEFAULT_SEEKS,
652};
653
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654/*
655 * Put reference to dquot
656 * NOTE: If you change this function please check whether dqput_blocks() works right...
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 */
Jan Kara3d9ea252008-10-10 16:12:23 +0200658void dqput(struct dquot *dquot)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659{
Jan Karab48d3802008-07-25 01:46:49 -0700660 int ret;
661
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 if (!dquot)
663 return;
664#ifdef __DQUOT_PARANOIA
665 if (!atomic_read(&dquot->dq_count)) {
666 printk("VFS: dqput: trying to free free dquot\n");
667 printk("VFS: device %s, dquot of %s %d\n",
668 dquot->dq_sb->s_id,
669 quotatypes[dquot->dq_type],
670 dquot->dq_id);
671 BUG();
672 }
673#endif
674
675 spin_lock(&dq_list_lock);
676 dqstats.drops++;
677 spin_unlock(&dq_list_lock);
678we_slept:
679 spin_lock(&dq_list_lock);
680 if (atomic_read(&dquot->dq_count) > 1) {
681 /* We have more than one user... nothing to do */
682 atomic_dec(&dquot->dq_count);
Jan Kara6362e4d2006-03-23 03:00:17 -0800683 /* Releasing dquot during quotaoff phase? */
Jan Karaf55abc02008-08-20 17:50:32 +0200684 if (!sb_has_quota_active(dquot->dq_sb, dquot->dq_type) &&
Jan Kara6362e4d2006-03-23 03:00:17 -0800685 atomic_read(&dquot->dq_count) == 1)
686 wake_up(&dquot->dq_wait_unused);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 spin_unlock(&dq_list_lock);
688 return;
689 }
690 /* Need to release dquot? */
691 if (test_bit(DQ_ACTIVE_B, &dquot->dq_flags) && dquot_dirty(dquot)) {
692 spin_unlock(&dq_list_lock);
693 /* Commit dquot before releasing */
Jan Karab48d3802008-07-25 01:46:49 -0700694 ret = dquot->dq_sb->dq_op->write_dquot(dquot);
695 if (ret < 0) {
696 printk(KERN_ERR "VFS: cannot write quota structure on "
697 "device %s (error %d). Quota may get out of "
698 "sync!\n", dquot->dq_sb->s_id, ret);
699 /*
700 * We clear dirty bit anyway, so that we avoid
701 * infinite loop here
702 */
703 spin_lock(&dq_list_lock);
704 clear_dquot_dirty(dquot);
705 spin_unlock(&dq_list_lock);
706 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 goto we_slept;
708 }
709 /* Clear flag in case dquot was inactive (something bad happened) */
710 clear_dquot_dirty(dquot);
711 if (test_bit(DQ_ACTIVE_B, &dquot->dq_flags)) {
712 spin_unlock(&dq_list_lock);
713 dquot->dq_sb->dq_op->release_dquot(dquot);
714 goto we_slept;
715 }
716 atomic_dec(&dquot->dq_count);
717#ifdef __DQUOT_PARANOIA
718 /* sanity check */
Eric Sesterhenn8abf6a42006-04-02 13:36:13 +0200719 BUG_ON(!list_empty(&dquot->dq_free));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720#endif
721 put_dquot_last(dquot);
722 spin_unlock(&dq_list_lock);
723}
Mingming Cao08d03502009-01-14 16:19:32 +0100724EXPORT_SYMBOL(dqput);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725
Jan Kara7d9056b2008-11-25 15:31:32 +0100726struct dquot *dquot_alloc(struct super_block *sb, int type)
Jan Kara74f783a2008-08-19 14:51:22 +0200727{
728 return kmem_cache_zalloc(dquot_cachep, GFP_NOFS);
729}
Jan Kara7d9056b2008-11-25 15:31:32 +0100730EXPORT_SYMBOL(dquot_alloc);
Jan Kara74f783a2008-08-19 14:51:22 +0200731
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732static struct dquot *get_empty_dquot(struct super_block *sb, int type)
733{
734 struct dquot *dquot;
735
Jan Kara74f783a2008-08-19 14:51:22 +0200736 dquot = sb->dq_op->alloc_dquot(sb, type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 if(!dquot)
Jan Karadd6f3c62009-01-26 16:01:43 +0100738 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739
Ingo Molnard3be9152006-03-23 03:00:29 -0800740 mutex_init(&dquot->dq_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 INIT_LIST_HEAD(&dquot->dq_free);
742 INIT_LIST_HEAD(&dquot->dq_inuse);
743 INIT_HLIST_NODE(&dquot->dq_hash);
744 INIT_LIST_HEAD(&dquot->dq_dirty);
Jan Kara6362e4d2006-03-23 03:00:17 -0800745 init_waitqueue_head(&dquot->dq_wait_unused);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 dquot->dq_sb = sb;
747 dquot->dq_type = type;
748 atomic_set(&dquot->dq_count, 1);
749
750 return dquot;
751}
752
753/*
754 * Get reference to dquot
Jan Karacc334122009-01-12 17:23:05 +0100755 *
756 * Locking is slightly tricky here. We are guarded from parallel quotaoff()
757 * destroying our dquot by:
758 * a) checking for quota flags under dq_list_lock and
759 * b) getting a reference to dquot before we release dq_list_lock
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 */
Jan Kara3d9ea252008-10-10 16:12:23 +0200761struct dquot *dqget(struct super_block *sb, unsigned int id, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762{
763 unsigned int hashent = hashfn(sb, id, type);
Jan Karadd6f3c62009-01-26 16:01:43 +0100764 struct dquot *dquot = NULL, *empty = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765
Jan Karaf55abc02008-08-20 17:50:32 +0200766 if (!sb_has_quota_active(sb, type))
Jan Karadd6f3c62009-01-26 16:01:43 +0100767 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768we_slept:
769 spin_lock(&dq_list_lock);
Jan Karacc334122009-01-12 17:23:05 +0100770 spin_lock(&dq_state_lock);
771 if (!sb_has_quota_active(sb, type)) {
772 spin_unlock(&dq_state_lock);
773 spin_unlock(&dq_list_lock);
774 goto out;
775 }
776 spin_unlock(&dq_state_lock);
777
Jan Karadd6f3c62009-01-26 16:01:43 +0100778 dquot = find_dquot(hashent, sb, id, type);
779 if (!dquot) {
780 if (!empty) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 spin_unlock(&dq_list_lock);
Jan Karadd6f3c62009-01-26 16:01:43 +0100782 empty = get_empty_dquot(sb, type);
783 if (!empty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 schedule(); /* Try to wait for a moment... */
785 goto we_slept;
786 }
787 dquot = empty;
Jan Karadd6f3c62009-01-26 16:01:43 +0100788 empty = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 dquot->dq_id = id;
790 /* all dquots go on the inuse_list */
791 put_inuse(dquot);
792 /* hash it first so it can be found */
793 insert_dquot_hash(dquot);
794 dqstats.lookups++;
795 spin_unlock(&dq_list_lock);
796 } else {
797 if (!atomic_read(&dquot->dq_count))
798 remove_free_dquot(dquot);
799 atomic_inc(&dquot->dq_count);
800 dqstats.cache_hits++;
801 dqstats.lookups++;
802 spin_unlock(&dq_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 }
Jan Kara268157b2009-01-27 15:47:22 +0100804 /* Wait for dq_lock - after this we know that either dquot_release() is
805 * already finished or it will be canceled due to dq_count > 1 test */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 wait_on_dquot(dquot);
Jan Kara268157b2009-01-27 15:47:22 +0100807 /* Read the dquot / allocate space in quota file */
808 if (!test_bit(DQ_ACTIVE_B, &dquot->dq_flags) &&
809 sb->dq_op->acquire_dquot(dquot) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 dqput(dquot);
Jan Karadd6f3c62009-01-26 16:01:43 +0100811 dquot = NULL;
Jan Karacc334122009-01-12 17:23:05 +0100812 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 }
814#ifdef __DQUOT_PARANOIA
Eric Sesterhenn8abf6a42006-04-02 13:36:13 +0200815 BUG_ON(!dquot->dq_sb); /* Has somebody invalidated entry under us? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816#endif
Jan Karacc334122009-01-12 17:23:05 +0100817out:
818 if (empty)
819 do_destroy_dquot(empty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820
821 return dquot;
822}
Mingming Cao08d03502009-01-14 16:19:32 +0100823EXPORT_SYMBOL(dqget);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824
825static int dqinit_needed(struct inode *inode, int type)
826{
827 int cnt;
828
829 if (IS_NOQUOTA(inode))
830 return 0;
831 if (type != -1)
Jan Karadd6f3c62009-01-26 16:01:43 +0100832 return !inode->i_dquot[type];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
Jan Karadd6f3c62009-01-26 16:01:43 +0100834 if (!inode->i_dquot[cnt])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 return 1;
836 return 0;
837}
838
Ingo Molnard3be9152006-03-23 03:00:29 -0800839/* This routine is guarded by dqonoff_mutex mutex */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840static void add_dquot_ref(struct super_block *sb, int type)
841{
Jan Kara941d2382008-02-06 01:37:36 -0800842 struct inode *inode, *old_inode = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843
Christoph Hellwigd003fb72007-02-12 00:51:58 -0800844 spin_lock(&inode_lock);
845 list_for_each_entry(inode, &sb->s_inodes, i_sb_list) {
Wu Fengguangb6fac632009-04-02 16:56:34 -0700846 if (inode->i_state & (I_FREEING|I_CLEAR|I_WILL_FREE|I_NEW))
Nick Pigginaabb8fd2009-03-11 13:17:36 -0700847 continue;
Christoph Hellwigd003fb72007-02-12 00:51:58 -0800848 if (!atomic_read(&inode->i_writecount))
849 continue;
850 if (!dqinit_needed(inode, type))
851 continue;
Christoph Hellwigd003fb72007-02-12 00:51:58 -0800852
853 __iget(inode);
854 spin_unlock(&inode_lock);
855
Jan Kara941d2382008-02-06 01:37:36 -0800856 iput(old_inode);
Christoph Hellwigd003fb72007-02-12 00:51:58 -0800857 sb->dq_op->initialize(inode, type);
Jan Kara941d2382008-02-06 01:37:36 -0800858 /* We hold a reference to 'inode' so it couldn't have been
859 * removed from s_inodes list while we dropped the inode_lock.
860 * We cannot iput the inode now as we can be holding the last
861 * reference and we cannot iput it under inode_lock. So we
862 * keep the reference and iput it later. */
863 old_inode = inode;
864 spin_lock(&inode_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 }
Christoph Hellwigd003fb72007-02-12 00:51:58 -0800866 spin_unlock(&inode_lock);
Jan Kara941d2382008-02-06 01:37:36 -0800867 iput(old_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868}
869
Jan Kara268157b2009-01-27 15:47:22 +0100870/*
871 * Return 0 if dqput() won't block.
872 * (note that 1 doesn't necessarily mean blocking)
873 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874static inline int dqput_blocks(struct dquot *dquot)
875{
876 if (atomic_read(&dquot->dq_count) <= 1)
877 return 1;
878 return 0;
879}
880
Jan Kara268157b2009-01-27 15:47:22 +0100881/*
882 * Remove references to dquots from inode and add dquot to list for freeing
883 * if we have the last referece to dquot
884 * We can't race with anybody because we hold dqptr_sem for writing...
885 */
Adrian Bunke5f00f42007-05-08 00:27:22 -0700886static int remove_inode_dquot_ref(struct inode *inode, int type,
887 struct list_head *tofree_head)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888{
889 struct dquot *dquot = inode->i_dquot[type];
890
Jan Karadd6f3c62009-01-26 16:01:43 +0100891 inode->i_dquot[type] = NULL;
892 if (dquot) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 if (dqput_blocks(dquot)) {
894#ifdef __DQUOT_PARANOIA
895 if (atomic_read(&dquot->dq_count) != 1)
896 printk(KERN_WARNING "VFS: Adding dquot with dq_count %d to dispose list.\n", atomic_read(&dquot->dq_count));
897#endif
898 spin_lock(&dq_list_lock);
Jan Kara268157b2009-01-27 15:47:22 +0100899 /* As dquot must have currently users it can't be on
900 * the free list... */
901 list_add(&dquot->dq_free, tofree_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 spin_unlock(&dq_list_lock);
903 return 1;
904 }
905 else
906 dqput(dquot); /* We have guaranteed we won't block */
907 }
908 return 0;
909}
910
Jan Kara268157b2009-01-27 15:47:22 +0100911/*
912 * Free list of dquots
913 * Dquots are removed from inodes and no new references can be got so we are
914 * the only ones holding reference
915 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916static void put_dquot_list(struct list_head *tofree_head)
917{
918 struct list_head *act_head;
919 struct dquot *dquot;
920
921 act_head = tofree_head->next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 while (act_head != tofree_head) {
923 dquot = list_entry(act_head, struct dquot, dq_free);
924 act_head = act_head->next;
Jan Kara268157b2009-01-27 15:47:22 +0100925 /* Remove dquot from the list so we won't have problems... */
926 list_del_init(&dquot->dq_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 dqput(dquot);
928 }
929}
930
Christoph Hellwigfb58b732007-02-12 00:51:57 -0800931static void remove_dquot_ref(struct super_block *sb, int type,
932 struct list_head *tofree_head)
933{
934 struct inode *inode;
935
936 spin_lock(&inode_lock);
937 list_for_each_entry(inode, &sb->s_inodes, i_sb_list) {
Nick Pigginaabb8fd2009-03-11 13:17:36 -0700938 /*
939 * We have to scan also I_NEW inodes because they can already
940 * have quota pointer initialized. Luckily, we need to touch
941 * only quota pointers and these have separate locking
942 * (dqptr_sem).
943 */
Christoph Hellwigfb58b732007-02-12 00:51:57 -0800944 if (!IS_NOQUOTA(inode))
945 remove_inode_dquot_ref(inode, type, tofree_head);
946 }
947 spin_unlock(&inode_lock);
948}
949
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950/* Gather all references from inodes and drop them */
951static void drop_dquot_ref(struct super_block *sb, int type)
952{
953 LIST_HEAD(tofree_head);
954
Christoph Hellwigfb58b732007-02-12 00:51:57 -0800955 if (sb->dq_op) {
956 down_write(&sb_dqopt(sb)->dqptr_sem);
957 remove_dquot_ref(sb, type, &tofree_head);
958 up_write(&sb_dqopt(sb)->dqptr_sem);
959 put_dquot_list(&tofree_head);
960 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961}
962
Jan Kara12095462008-08-20 14:45:12 +0200963static inline void dquot_incr_inodes(struct dquot *dquot, qsize_t number)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964{
965 dquot->dq_dqb.dqb_curinodes += number;
966}
967
968static inline void dquot_incr_space(struct dquot *dquot, qsize_t number)
969{
970 dquot->dq_dqb.dqb_curspace += number;
971}
972
Mingming Caof18df222009-01-13 16:43:09 +0100973static inline void dquot_resv_space(struct dquot *dquot, qsize_t number)
974{
975 dquot->dq_dqb.dqb_rsvspace += number;
976}
977
Mingming Cao740d9dc2009-01-13 16:43:14 +0100978/*
979 * Claim reserved quota space
980 */
981static void dquot_claim_reserved_space(struct dquot *dquot,
982 qsize_t number)
983{
984 WARN_ON(dquot->dq_dqb.dqb_rsvspace < number);
985 dquot->dq_dqb.dqb_curspace += number;
986 dquot->dq_dqb.dqb_rsvspace -= number;
987}
988
989static inline
990void dquot_free_reserved_space(struct dquot *dquot, qsize_t number)
991{
992 dquot->dq_dqb.dqb_rsvspace -= number;
993}
994
Jan Kara7a2435d2009-01-27 01:47:11 +0100995static void dquot_decr_inodes(struct dquot *dquot, qsize_t number)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996{
Jan Karadb49d2d2008-10-01 18:21:39 +0200997 if (sb_dqopt(dquot->dq_sb)->flags & DQUOT_NEGATIVE_USAGE ||
998 dquot->dq_dqb.dqb_curinodes >= number)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 dquot->dq_dqb.dqb_curinodes -= number;
1000 else
1001 dquot->dq_dqb.dqb_curinodes = 0;
1002 if (dquot->dq_dqb.dqb_curinodes <= dquot->dq_dqb.dqb_isoftlimit)
1003 dquot->dq_dqb.dqb_itime = (time_t) 0;
1004 clear_bit(DQ_INODES_B, &dquot->dq_flags);
1005}
1006
Jan Kara7a2435d2009-01-27 01:47:11 +01001007static void dquot_decr_space(struct dquot *dquot, qsize_t number)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008{
Jan Karadb49d2d2008-10-01 18:21:39 +02001009 if (sb_dqopt(dquot->dq_sb)->flags & DQUOT_NEGATIVE_USAGE ||
1010 dquot->dq_dqb.dqb_curspace >= number)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 dquot->dq_dqb.dqb_curspace -= number;
1012 else
1013 dquot->dq_dqb.dqb_curspace = 0;
Jan Kara12095462008-08-20 14:45:12 +02001014 if (dquot->dq_dqb.dqb_curspace <= dquot->dq_dqb.dqb_bsoftlimit)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 dquot->dq_dqb.dqb_btime = (time_t) 0;
1016 clear_bit(DQ_BLKS_B, &dquot->dq_flags);
1017}
1018
Jan Karac5254602007-12-22 14:03:25 -08001019static int warning_issued(struct dquot *dquot, const int warntype)
1020{
1021 int flag = (warntype == QUOTA_NL_BHARDWARN ||
1022 warntype == QUOTA_NL_BSOFTLONGWARN) ? DQ_BLKS_B :
1023 ((warntype == QUOTA_NL_IHARDWARN ||
1024 warntype == QUOTA_NL_ISOFTLONGWARN) ? DQ_INODES_B : 0);
1025
1026 if (!flag)
1027 return 0;
1028 return test_and_set_bit(flag, &dquot->dq_flags);
1029}
1030
Jan Kara8e893462007-10-16 23:29:31 -07001031#ifdef CONFIG_PRINT_QUOTA_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032static int flag_print_warnings = 1;
1033
Jan Kara7a2435d2009-01-27 01:47:11 +01001034static int need_print_warning(struct dquot *dquot)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035{
1036 if (!flag_print_warnings)
1037 return 0;
1038
1039 switch (dquot->dq_type) {
1040 case USRQUOTA:
David Howellsda9592e2008-11-14 10:39:05 +11001041 return current_fsuid() == dquot->dq_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 case GRPQUOTA:
1043 return in_group_p(dquot->dq_id);
1044 }
1045 return 0;
1046}
1047
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048/* Print warning to user which exceeded quota */
Jan Karac5254602007-12-22 14:03:25 -08001049static void print_warning(struct dquot *dquot, const int warntype)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050{
1051 char *msg = NULL;
Peter Zijlstra24ec8392006-12-08 02:36:04 -08001052 struct tty_struct *tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053
Jan Kara657d3bf2008-07-25 01:46:52 -07001054 if (warntype == QUOTA_NL_IHARDBELOW ||
1055 warntype == QUOTA_NL_ISOFTBELOW ||
1056 warntype == QUOTA_NL_BHARDBELOW ||
1057 warntype == QUOTA_NL_BSOFTBELOW || !need_print_warning(dquot))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 return;
1059
Peter Zijlstra24ec8392006-12-08 02:36:04 -08001060 tty = get_current_tty();
1061 if (!tty)
Alan Cox452a00d2008-10-13 10:39:13 +01001062 return;
Peter Zijlstra24ec8392006-12-08 02:36:04 -08001063 tty_write_message(tty, dquot->dq_sb->s_id);
Jan Kara8e893462007-10-16 23:29:31 -07001064 if (warntype == QUOTA_NL_ISOFTWARN || warntype == QUOTA_NL_BSOFTWARN)
Peter Zijlstra24ec8392006-12-08 02:36:04 -08001065 tty_write_message(tty, ": warning, ");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 else
Peter Zijlstra24ec8392006-12-08 02:36:04 -08001067 tty_write_message(tty, ": write failed, ");
1068 tty_write_message(tty, quotatypes[dquot->dq_type]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 switch (warntype) {
Jan Kara8e893462007-10-16 23:29:31 -07001070 case QUOTA_NL_IHARDWARN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 msg = " file limit reached.\r\n";
1072 break;
Jan Kara8e893462007-10-16 23:29:31 -07001073 case QUOTA_NL_ISOFTLONGWARN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 msg = " file quota exceeded too long.\r\n";
1075 break;
Jan Kara8e893462007-10-16 23:29:31 -07001076 case QUOTA_NL_ISOFTWARN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 msg = " file quota exceeded.\r\n";
1078 break;
Jan Kara8e893462007-10-16 23:29:31 -07001079 case QUOTA_NL_BHARDWARN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 msg = " block limit reached.\r\n";
1081 break;
Jan Kara8e893462007-10-16 23:29:31 -07001082 case QUOTA_NL_BSOFTLONGWARN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 msg = " block quota exceeded too long.\r\n";
1084 break;
Jan Kara8e893462007-10-16 23:29:31 -07001085 case QUOTA_NL_BSOFTWARN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 msg = " block quota exceeded.\r\n";
1087 break;
1088 }
Peter Zijlstra24ec8392006-12-08 02:36:04 -08001089 tty_write_message(tty, msg);
Alan Cox452a00d2008-10-13 10:39:13 +01001090 tty_kref_put(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091}
Jan Kara8e893462007-10-16 23:29:31 -07001092#endif
1093
Mingming Caof18df222009-01-13 16:43:09 +01001094/*
1095 * Write warnings to the console and send warning messages over netlink.
1096 *
1097 * Note that this function can sleep.
1098 */
Jan Kara7a2435d2009-01-27 01:47:11 +01001099static void flush_warnings(struct dquot *const *dquots, char *warntype)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100{
Steven Whitehouse86e931a2009-09-28 12:35:17 +01001101 struct dquot *dq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 int i;
1103
Steven Whitehouse86e931a2009-09-28 12:35:17 +01001104 for (i = 0; i < MAXQUOTAS; i++) {
1105 dq = dquots[i];
1106 if (dq && warntype[i] != QUOTA_NL_NOWARN &&
1107 !warning_issued(dq, warntype[i])) {
Jan Kara8e893462007-10-16 23:29:31 -07001108#ifdef CONFIG_PRINT_QUOTA_WARNING
Steven Whitehouse86e931a2009-09-28 12:35:17 +01001109 print_warning(dq, warntype[i]);
Jan Kara8e893462007-10-16 23:29:31 -07001110#endif
Steven Whitehouse86e931a2009-09-28 12:35:17 +01001111 quota_send_warning(dq->dq_type, dq->dq_id,
1112 dq->dq_sb->s_dev, warntype[i]);
Jan Kara8e893462007-10-16 23:29:31 -07001113 }
Steven Whitehouse86e931a2009-09-28 12:35:17 +01001114 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115}
1116
Jan Kara7a2435d2009-01-27 01:47:11 +01001117static int ignore_hardlimit(struct dquot *dquot)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118{
1119 struct mem_dqinfo *info = &sb_dqopt(dquot->dq_sb)->info[dquot->dq_type];
1120
1121 return capable(CAP_SYS_RESOURCE) &&
Jan Kara268157b2009-01-27 15:47:22 +01001122 (info->dqi_format->qf_fmt_id != QFMT_VFS_OLD ||
1123 !(info->dqi_flags & V1_DQF_RSQUASH));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124}
1125
1126/* needs dq_data_lock */
Jan Kara12095462008-08-20 14:45:12 +02001127static int check_idq(struct dquot *dquot, qsize_t inodes, char *warntype)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128{
Jan Kara268157b2009-01-27 15:47:22 +01001129 qsize_t newinodes = dquot->dq_dqb.dqb_curinodes + inodes;
1130
Jan Kara8e893462007-10-16 23:29:31 -07001131 *warntype = QUOTA_NL_NOWARN;
Jan Karaf55abc02008-08-20 17:50:32 +02001132 if (!sb_has_quota_limits_enabled(dquot->dq_sb, dquot->dq_type) ||
1133 test_bit(DQ_FAKE_B, &dquot->dq_flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 return QUOTA_OK;
1135
1136 if (dquot->dq_dqb.dqb_ihardlimit &&
Jan Kara268157b2009-01-27 15:47:22 +01001137 newinodes > dquot->dq_dqb.dqb_ihardlimit &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 !ignore_hardlimit(dquot)) {
Jan Kara8e893462007-10-16 23:29:31 -07001139 *warntype = QUOTA_NL_IHARDWARN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 return NO_QUOTA;
1141 }
1142
1143 if (dquot->dq_dqb.dqb_isoftlimit &&
Jan Kara268157b2009-01-27 15:47:22 +01001144 newinodes > dquot->dq_dqb.dqb_isoftlimit &&
1145 dquot->dq_dqb.dqb_itime &&
1146 get_seconds() >= dquot->dq_dqb.dqb_itime &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 !ignore_hardlimit(dquot)) {
Jan Kara8e893462007-10-16 23:29:31 -07001148 *warntype = QUOTA_NL_ISOFTLONGWARN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 return NO_QUOTA;
1150 }
1151
1152 if (dquot->dq_dqb.dqb_isoftlimit &&
Jan Kara268157b2009-01-27 15:47:22 +01001153 newinodes > dquot->dq_dqb.dqb_isoftlimit &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 dquot->dq_dqb.dqb_itime == 0) {
Jan Kara8e893462007-10-16 23:29:31 -07001155 *warntype = QUOTA_NL_ISOFTWARN;
Jan Kara268157b2009-01-27 15:47:22 +01001156 dquot->dq_dqb.dqb_itime = get_seconds() +
1157 sb_dqopt(dquot->dq_sb)->info[dquot->dq_type].dqi_igrace;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 }
1159
1160 return QUOTA_OK;
1161}
1162
1163/* needs dq_data_lock */
1164static int check_bdq(struct dquot *dquot, qsize_t space, int prealloc, char *warntype)
1165{
Mingming Caof18df222009-01-13 16:43:09 +01001166 qsize_t tspace;
Jan Kara268157b2009-01-27 15:47:22 +01001167 struct super_block *sb = dquot->dq_sb;
Mingming Caof18df222009-01-13 16:43:09 +01001168
Jan Kara8e893462007-10-16 23:29:31 -07001169 *warntype = QUOTA_NL_NOWARN;
Jan Kara268157b2009-01-27 15:47:22 +01001170 if (!sb_has_quota_limits_enabled(sb, dquot->dq_type) ||
Jan Karaf55abc02008-08-20 17:50:32 +02001171 test_bit(DQ_FAKE_B, &dquot->dq_flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 return QUOTA_OK;
1173
Mingming Caof18df222009-01-13 16:43:09 +01001174 tspace = dquot->dq_dqb.dqb_curspace + dquot->dq_dqb.dqb_rsvspace
1175 + space;
1176
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 if (dquot->dq_dqb.dqb_bhardlimit &&
Mingming Caof18df222009-01-13 16:43:09 +01001178 tspace > dquot->dq_dqb.dqb_bhardlimit &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 !ignore_hardlimit(dquot)) {
1180 if (!prealloc)
Jan Kara8e893462007-10-16 23:29:31 -07001181 *warntype = QUOTA_NL_BHARDWARN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 return NO_QUOTA;
1183 }
1184
1185 if (dquot->dq_dqb.dqb_bsoftlimit &&
Mingming Caof18df222009-01-13 16:43:09 +01001186 tspace > dquot->dq_dqb.dqb_bsoftlimit &&
Jan Kara268157b2009-01-27 15:47:22 +01001187 dquot->dq_dqb.dqb_btime &&
1188 get_seconds() >= dquot->dq_dqb.dqb_btime &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 !ignore_hardlimit(dquot)) {
1190 if (!prealloc)
Jan Kara8e893462007-10-16 23:29:31 -07001191 *warntype = QUOTA_NL_BSOFTLONGWARN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 return NO_QUOTA;
1193 }
1194
1195 if (dquot->dq_dqb.dqb_bsoftlimit &&
Mingming Caof18df222009-01-13 16:43:09 +01001196 tspace > dquot->dq_dqb.dqb_bsoftlimit &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 dquot->dq_dqb.dqb_btime == 0) {
1198 if (!prealloc) {
Jan Kara8e893462007-10-16 23:29:31 -07001199 *warntype = QUOTA_NL_BSOFTWARN;
Jan Kara268157b2009-01-27 15:47:22 +01001200 dquot->dq_dqb.dqb_btime = get_seconds() +
1201 sb_dqopt(sb)->info[dquot->dq_type].dqi_bgrace;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 }
1203 else
1204 /*
1205 * We don't allow preallocation to exceed softlimit so exceeding will
1206 * be always printed
1207 */
1208 return NO_QUOTA;
1209 }
1210
1211 return QUOTA_OK;
1212}
1213
Jan Kara12095462008-08-20 14:45:12 +02001214static int info_idq_free(struct dquot *dquot, qsize_t inodes)
Jan Kara657d3bf2008-07-25 01:46:52 -07001215{
Jan Kara268157b2009-01-27 15:47:22 +01001216 qsize_t newinodes;
1217
Jan Kara657d3bf2008-07-25 01:46:52 -07001218 if (test_bit(DQ_FAKE_B, &dquot->dq_flags) ||
Jan Karaf55abc02008-08-20 17:50:32 +02001219 dquot->dq_dqb.dqb_curinodes <= dquot->dq_dqb.dqb_isoftlimit ||
1220 !sb_has_quota_limits_enabled(dquot->dq_sb, dquot->dq_type))
Jan Kara657d3bf2008-07-25 01:46:52 -07001221 return QUOTA_NL_NOWARN;
1222
Jan Kara268157b2009-01-27 15:47:22 +01001223 newinodes = dquot->dq_dqb.dqb_curinodes - inodes;
1224 if (newinodes <= dquot->dq_dqb.dqb_isoftlimit)
Jan Kara657d3bf2008-07-25 01:46:52 -07001225 return QUOTA_NL_ISOFTBELOW;
1226 if (dquot->dq_dqb.dqb_curinodes >= dquot->dq_dqb.dqb_ihardlimit &&
Jan Kara268157b2009-01-27 15:47:22 +01001227 newinodes < dquot->dq_dqb.dqb_ihardlimit)
Jan Kara657d3bf2008-07-25 01:46:52 -07001228 return QUOTA_NL_IHARDBELOW;
1229 return QUOTA_NL_NOWARN;
1230}
1231
1232static int info_bdq_free(struct dquot *dquot, qsize_t space)
1233{
1234 if (test_bit(DQ_FAKE_B, &dquot->dq_flags) ||
Jan Kara12095462008-08-20 14:45:12 +02001235 dquot->dq_dqb.dqb_curspace <= dquot->dq_dqb.dqb_bsoftlimit)
Jan Kara657d3bf2008-07-25 01:46:52 -07001236 return QUOTA_NL_NOWARN;
1237
Jan Kara12095462008-08-20 14:45:12 +02001238 if (dquot->dq_dqb.dqb_curspace - space <= dquot->dq_dqb.dqb_bsoftlimit)
Jan Kara657d3bf2008-07-25 01:46:52 -07001239 return QUOTA_NL_BSOFTBELOW;
Jan Kara12095462008-08-20 14:45:12 +02001240 if (dquot->dq_dqb.dqb_curspace >= dquot->dq_dqb.dqb_bhardlimit &&
1241 dquot->dq_dqb.dqb_curspace - space < dquot->dq_dqb.dqb_bhardlimit)
Jan Kara657d3bf2008-07-25 01:46:52 -07001242 return QUOTA_NL_BHARDBELOW;
1243 return QUOTA_NL_NOWARN;
1244}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245/*
1246 * Initialize quota pointers in inode
Jan Karacc334122009-01-12 17:23:05 +01001247 * We do things in a bit complicated way but by that we avoid calling
1248 * dqget() and thus filesystem callbacks under dqptr_sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 */
1250int dquot_initialize(struct inode *inode, int type)
1251{
1252 unsigned int id = 0;
1253 int cnt, ret = 0;
Jan Karadd6f3c62009-01-26 16:01:43 +01001254 struct dquot *got[MAXQUOTAS] = { NULL, NULL };
Jan Karacc334122009-01-12 17:23:05 +01001255 struct super_block *sb = inode->i_sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256
Ingo Molnard3be9152006-03-23 03:00:29 -08001257 /* First test before acquiring mutex - solves deadlocks when we
1258 * re-enter the quota code and are already holding the mutex */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 if (IS_NOQUOTA(inode))
1260 return 0;
Jan Karacc334122009-01-12 17:23:05 +01001261
1262 /* First get references to structures we might need. */
1263 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1264 if (type != -1 && cnt != type)
1265 continue;
1266 switch (cnt) {
1267 case USRQUOTA:
1268 id = inode->i_uid;
1269 break;
1270 case GRPQUOTA:
1271 id = inode->i_gid;
1272 break;
1273 }
1274 got[cnt] = dqget(sb, id, cnt);
1275 }
1276
1277 down_write(&sb_dqopt(sb)->dqptr_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 /* Having dqptr_sem we know NOQUOTA flags can't be altered... */
1279 if (IS_NOQUOTA(inode))
1280 goto out_err;
1281 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1282 if (type != -1 && cnt != type)
1283 continue;
Jan Karacc334122009-01-12 17:23:05 +01001284 /* Avoid races with quotaoff() */
1285 if (!sb_has_quota_active(sb, cnt))
1286 continue;
Jan Karadd6f3c62009-01-26 16:01:43 +01001287 if (!inode->i_dquot[cnt]) {
Jan Karacc334122009-01-12 17:23:05 +01001288 inode->i_dquot[cnt] = got[cnt];
Jan Karadd6f3c62009-01-26 16:01:43 +01001289 got[cnt] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 }
1291 }
1292out_err:
Jan Karacc334122009-01-12 17:23:05 +01001293 up_write(&sb_dqopt(sb)->dqptr_sem);
1294 /* Drop unused references */
Dmitry Monakhovdc52dd32009-12-14 15:21:15 +03001295 dqput_all(got);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 return ret;
1297}
Mingming Cao08d03502009-01-14 16:19:32 +01001298EXPORT_SYMBOL(dquot_initialize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299
1300/*
1301 * Release all quotas referenced by inode
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 */
Jan Kara3d9ea252008-10-10 16:12:23 +02001303int dquot_drop(struct inode *inode)
1304{
Jan Karacc334122009-01-12 17:23:05 +01001305 int cnt;
1306 struct dquot *put[MAXQUOTAS];
1307
Jan Kara3d9ea252008-10-10 16:12:23 +02001308 down_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
Jan Karacc334122009-01-12 17:23:05 +01001309 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1310 put[cnt] = inode->i_dquot[cnt];
Jan Karadd6f3c62009-01-26 16:01:43 +01001311 inode->i_dquot[cnt] = NULL;
Jan Karacc334122009-01-12 17:23:05 +01001312 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 up_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
Dmitry Monakhovdc52dd32009-12-14 15:21:15 +03001314 dqput_all(put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 return 0;
1316}
Mingming Cao08d03502009-01-14 16:19:32 +01001317EXPORT_SYMBOL(dquot_drop);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318
Jan Karab85f4b82008-07-25 01:46:50 -07001319/* Wrapper to remove references to quota structures from inode */
1320void vfs_dq_drop(struct inode *inode)
1321{
1322 /* Here we can get arbitrary inode from clear_inode() so we have
1323 * to be careful. OTOH we don't need locking as quota operations
1324 * are allowed to change only at mount time */
1325 if (!IS_NOQUOTA(inode) && inode->i_sb && inode->i_sb->dq_op
1326 && inode->i_sb->dq_op->drop) {
1327 int cnt;
1328 /* Test before calling to rule out calls from proc and such
1329 * where we are not allowed to block. Note that this is
1330 * actually reliable test even without the lock - the caller
1331 * must assure that nobody can come after the DQUOT_DROP and
1332 * add quota pointers back anyway */
1333 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
Jan Karadd6f3c62009-01-26 16:01:43 +01001334 if (inode->i_dquot[cnt])
Jan Karab85f4b82008-07-25 01:46:50 -07001335 break;
1336 if (cnt < MAXQUOTAS)
1337 inode->i_sb->dq_op->drop(inode);
1338 }
1339}
Mingming Cao08d03502009-01-14 16:19:32 +01001340EXPORT_SYMBOL(vfs_dq_drop);
Jan Karab85f4b82008-07-25 01:46:50 -07001341
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342/*
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001343 * inode_reserved_space is managed internally by quota, and protected by
1344 * i_lock similar to i_blocks+i_bytes.
1345 */
1346static qsize_t *inode_reserved_space(struct inode * inode)
1347{
1348 /* Filesystem must explicitly define it's own method in order to use
1349 * quota reservation interface */
1350 BUG_ON(!inode->i_sb->dq_op->get_reserved_space);
1351 return inode->i_sb->dq_op->get_reserved_space(inode);
1352}
1353
1354static void inode_add_rsv_space(struct inode *inode, qsize_t number)
1355{
1356 spin_lock(&inode->i_lock);
1357 *inode_reserved_space(inode) += number;
1358 spin_unlock(&inode->i_lock);
1359}
1360
1361
1362static void inode_claim_rsv_space(struct inode *inode, qsize_t number)
1363{
1364 spin_lock(&inode->i_lock);
1365 *inode_reserved_space(inode) -= number;
1366 __inode_add_bytes(inode, number);
1367 spin_unlock(&inode->i_lock);
1368}
1369
1370static void inode_sub_rsv_space(struct inode *inode, qsize_t number)
1371{
1372 spin_lock(&inode->i_lock);
1373 *inode_reserved_space(inode) -= number;
1374 spin_unlock(&inode->i_lock);
1375}
1376
1377static qsize_t inode_get_rsv_space(struct inode *inode)
1378{
1379 qsize_t ret;
1380 spin_lock(&inode->i_lock);
1381 ret = *inode_reserved_space(inode);
1382 spin_unlock(&inode->i_lock);
1383 return ret;
1384}
1385
1386static void inode_incr_space(struct inode *inode, qsize_t number,
1387 int reserve)
1388{
1389 if (reserve)
1390 inode_add_rsv_space(inode, number);
1391 else
1392 inode_add_bytes(inode, number);
1393}
1394
1395static void inode_decr_space(struct inode *inode, qsize_t number, int reserve)
1396{
1397 if (reserve)
1398 inode_sub_rsv_space(inode, number);
1399 else
1400 inode_sub_bytes(inode, number);
1401}
1402
1403/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 * Following four functions update i_blocks+i_bytes fields and
1405 * quota information (together with appropriate checks)
1406 * NOTE: We absolutely rely on the fact that caller dirties
1407 * the inode (usually macros in quotaops.h care about this) and
1408 * holds a handle for the current transaction so that dquot write and
1409 * inode write go into the same transaction.
1410 */
1411
1412/*
1413 * This operation can block, but only after everything is updated
1414 */
Mingming Caof18df222009-01-13 16:43:09 +01001415int __dquot_alloc_space(struct inode *inode, qsize_t number,
1416 int warn, int reserve)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417{
Mingming Caof18df222009-01-13 16:43:09 +01001418 int cnt, ret = QUOTA_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 char warntype[MAXQUOTAS];
1420
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001421 /*
1422 * First test before acquiring mutex - solves deadlocks when we
1423 * re-enter the quota code and are already holding the mutex
1424 */
1425 if (IS_NOQUOTA(inode)) {
1426 inode_incr_space(inode, number, reserve);
1427 goto out;
1428 }
1429
1430 down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1431 if (IS_NOQUOTA(inode)) {
1432 inode_incr_space(inode, number, reserve);
1433 goto out_unlock;
1434 }
1435
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
Jan Kara8e893462007-10-16 23:29:31 -07001437 warntype[cnt] = QUOTA_NL_NOWARN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439 spin_lock(&dq_data_lock);
1440 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
Jan Karadd6f3c62009-01-26 16:01:43 +01001441 if (!inode->i_dquot[cnt])
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 continue;
Mingming Caof18df222009-01-13 16:43:09 +01001443 if (check_bdq(inode->i_dquot[cnt], number, warn, warntype+cnt)
1444 == NO_QUOTA) {
1445 ret = NO_QUOTA;
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001446 spin_unlock(&dq_data_lock);
1447 goto out_flush_warn;
Mingming Caof18df222009-01-13 16:43:09 +01001448 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449 }
1450 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
Jan Karadd6f3c62009-01-26 16:01:43 +01001451 if (!inode->i_dquot[cnt])
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 continue;
Mingming Caof18df222009-01-13 16:43:09 +01001453 if (reserve)
1454 dquot_resv_space(inode->i_dquot[cnt], number);
1455 else
1456 dquot_incr_space(inode->i_dquot[cnt], number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 }
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001458 inode_incr_space(inode, number, reserve);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459 spin_unlock(&dq_data_lock);
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001460
1461 if (reserve)
1462 goto out_flush_warn;
Dmitry Monakhovdc52dd32009-12-14 15:21:15 +03001463 mark_all_dquot_dirty(inode->i_dquot);
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001464out_flush_warn:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465 flush_warnings(inode->i_dquot, warntype);
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001466out_unlock:
1467 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1468out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 return ret;
1470}
1471
Mingming Caof18df222009-01-13 16:43:09 +01001472int dquot_alloc_space(struct inode *inode, qsize_t number, int warn)
1473{
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001474 return __dquot_alloc_space(inode, number, warn, 0);
Mingming Caof18df222009-01-13 16:43:09 +01001475}
Mingming Cao08d03502009-01-14 16:19:32 +01001476EXPORT_SYMBOL(dquot_alloc_space);
Mingming Caof18df222009-01-13 16:43:09 +01001477
1478int dquot_reserve_space(struct inode *inode, qsize_t number, int warn)
1479{
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001480 return __dquot_alloc_space(inode, number, warn, 1);
Mingming Caof18df222009-01-13 16:43:09 +01001481}
1482EXPORT_SYMBOL(dquot_reserve_space);
1483
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484/*
1485 * This operation can block, but only after everything is updated
1486 */
Jan Kara12095462008-08-20 14:45:12 +02001487int dquot_alloc_inode(const struct inode *inode, qsize_t number)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488{
1489 int cnt, ret = NO_QUOTA;
1490 char warntype[MAXQUOTAS];
1491
Ingo Molnard3be9152006-03-23 03:00:29 -08001492 /* First test before acquiring mutex - solves deadlocks when we
1493 * re-enter the quota code and are already holding the mutex */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 if (IS_NOQUOTA(inode))
1495 return QUOTA_OK;
1496 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
Jan Kara8e893462007-10-16 23:29:31 -07001497 warntype[cnt] = QUOTA_NL_NOWARN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498 down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1499 if (IS_NOQUOTA(inode)) {
1500 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1501 return QUOTA_OK;
1502 }
1503 spin_lock(&dq_data_lock);
1504 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
Jan Karadd6f3c62009-01-26 16:01:43 +01001505 if (!inode->i_dquot[cnt])
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506 continue;
Jan Kara268157b2009-01-27 15:47:22 +01001507 if (check_idq(inode->i_dquot[cnt], number, warntype+cnt)
1508 == NO_QUOTA)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509 goto warn_put_all;
1510 }
1511
1512 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
Jan Karadd6f3c62009-01-26 16:01:43 +01001513 if (!inode->i_dquot[cnt])
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514 continue;
1515 dquot_incr_inodes(inode->i_dquot[cnt], number);
1516 }
1517 ret = QUOTA_OK;
1518warn_put_all:
1519 spin_unlock(&dq_data_lock);
1520 if (ret == QUOTA_OK)
Dmitry Monakhovdc52dd32009-12-14 15:21:15 +03001521 mark_all_dquot_dirty(inode->i_dquot);
Jan Kara087ee8d2007-12-17 16:20:26 -08001522 flush_warnings(inode->i_dquot, warntype);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1524 return ret;
1525}
Mingming Cao08d03502009-01-14 16:19:32 +01001526EXPORT_SYMBOL(dquot_alloc_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527
Mingming Cao740d9dc2009-01-13 16:43:14 +01001528int dquot_claim_space(struct inode *inode, qsize_t number)
1529{
1530 int cnt;
1531 int ret = QUOTA_OK;
1532
1533 if (IS_NOQUOTA(inode)) {
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001534 inode_claim_rsv_space(inode, number);
Mingming Cao740d9dc2009-01-13 16:43:14 +01001535 goto out;
1536 }
1537
1538 down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1539 if (IS_NOQUOTA(inode)) {
1540 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001541 inode_claim_rsv_space(inode, number);
Mingming Cao740d9dc2009-01-13 16:43:14 +01001542 goto out;
1543 }
1544
1545 spin_lock(&dq_data_lock);
1546 /* Claim reserved quotas to allocated quotas */
1547 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
Jan Karadd6f3c62009-01-26 16:01:43 +01001548 if (inode->i_dquot[cnt])
Mingming Cao740d9dc2009-01-13 16:43:14 +01001549 dquot_claim_reserved_space(inode->i_dquot[cnt],
1550 number);
1551 }
1552 /* Update inode bytes */
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001553 inode_claim_rsv_space(inode, number);
Mingming Cao740d9dc2009-01-13 16:43:14 +01001554 spin_unlock(&dq_data_lock);
Dmitry Monakhovdc52dd32009-12-14 15:21:15 +03001555 mark_all_dquot_dirty(inode->i_dquot);
Mingming Cao740d9dc2009-01-13 16:43:14 +01001556 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1557out:
1558 return ret;
1559}
1560EXPORT_SYMBOL(dquot_claim_space);
1561
1562/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563 * This operation can block, but only after everything is updated
1564 */
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001565int __dquot_free_space(struct inode *inode, qsize_t number, int reserve)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566{
1567 unsigned int cnt;
Jan Kara657d3bf2008-07-25 01:46:52 -07001568 char warntype[MAXQUOTAS];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569
Ingo Molnard3be9152006-03-23 03:00:29 -08001570 /* First test before acquiring mutex - solves deadlocks when we
1571 * re-enter the quota code and are already holding the mutex */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572 if (IS_NOQUOTA(inode)) {
1573out_sub:
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001574 inode_decr_space(inode, number, reserve);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575 return QUOTA_OK;
1576 }
Jan Kara657d3bf2008-07-25 01:46:52 -07001577
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578 down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1579 /* Now recheck reliably when holding dqptr_sem */
1580 if (IS_NOQUOTA(inode)) {
1581 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1582 goto out_sub;
1583 }
1584 spin_lock(&dq_data_lock);
1585 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
Jan Karadd6f3c62009-01-26 16:01:43 +01001586 if (!inode->i_dquot[cnt])
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587 continue;
Jan Kara657d3bf2008-07-25 01:46:52 -07001588 warntype[cnt] = info_bdq_free(inode->i_dquot[cnt], number);
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001589 if (reserve)
1590 dquot_free_reserved_space(inode->i_dquot[cnt], number);
1591 else
1592 dquot_decr_space(inode->i_dquot[cnt], number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593 }
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001594 inode_decr_space(inode, number, reserve);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595 spin_unlock(&dq_data_lock);
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001596
1597 if (reserve)
1598 goto out_unlock;
Dmitry Monakhovdc52dd32009-12-14 15:21:15 +03001599 mark_all_dquot_dirty(inode->i_dquot);
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001600out_unlock:
Jan Kara657d3bf2008-07-25 01:46:52 -07001601 flush_warnings(inode->i_dquot, warntype);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1603 return QUOTA_OK;
1604}
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001605
1606int dquot_free_space(struct inode *inode, qsize_t number)
1607{
1608 return __dquot_free_space(inode, number, 0);
1609}
Mingming Cao08d03502009-01-14 16:19:32 +01001610EXPORT_SYMBOL(dquot_free_space);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611
1612/*
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001613 * Release reserved quota space
1614 */
1615void dquot_release_reserved_space(struct inode *inode, qsize_t number)
1616{
1617 __dquot_free_space(inode, number, 1);
1618
1619}
1620EXPORT_SYMBOL(dquot_release_reserved_space);
1621
1622/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623 * This operation can block, but only after everything is updated
1624 */
Jan Kara12095462008-08-20 14:45:12 +02001625int dquot_free_inode(const struct inode *inode, qsize_t number)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626{
1627 unsigned int cnt;
Jan Kara657d3bf2008-07-25 01:46:52 -07001628 char warntype[MAXQUOTAS];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629
Ingo Molnard3be9152006-03-23 03:00:29 -08001630 /* First test before acquiring mutex - solves deadlocks when we
1631 * re-enter the quota code and are already holding the mutex */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632 if (IS_NOQUOTA(inode))
1633 return QUOTA_OK;
Jan Kara657d3bf2008-07-25 01:46:52 -07001634
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635 down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1636 /* Now recheck reliably when holding dqptr_sem */
1637 if (IS_NOQUOTA(inode)) {
1638 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1639 return QUOTA_OK;
1640 }
1641 spin_lock(&dq_data_lock);
1642 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
Jan Karadd6f3c62009-01-26 16:01:43 +01001643 if (!inode->i_dquot[cnt])
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644 continue;
Jan Kara657d3bf2008-07-25 01:46:52 -07001645 warntype[cnt] = info_idq_free(inode->i_dquot[cnt], number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646 dquot_decr_inodes(inode->i_dquot[cnt], number);
1647 }
1648 spin_unlock(&dq_data_lock);
Dmitry Monakhovdc52dd32009-12-14 15:21:15 +03001649 mark_all_dquot_dirty(inode->i_dquot);
Jan Kara657d3bf2008-07-25 01:46:52 -07001650 flush_warnings(inode->i_dquot, warntype);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1652 return QUOTA_OK;
1653}
Mingming Cao08d03502009-01-14 16:19:32 +01001654EXPORT_SYMBOL(dquot_free_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655
1656/*
1657 * Transfer the number of inode and blocks from one diskquota to an other.
1658 *
1659 * This operation can block, but only after everything is updated
1660 * A transaction must be started when entering this function.
1661 */
1662int dquot_transfer(struct inode *inode, struct iattr *iattr)
1663{
Mingming Cao740d9dc2009-01-13 16:43:14 +01001664 qsize_t space, cur_space;
1665 qsize_t rsv_space = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666 struct dquot *transfer_from[MAXQUOTAS];
1667 struct dquot *transfer_to[MAXQUOTAS];
Jan Karacc334122009-01-12 17:23:05 +01001668 int cnt, ret = QUOTA_OK;
1669 int chuid = iattr->ia_valid & ATTR_UID && inode->i_uid != iattr->ia_uid,
1670 chgid = iattr->ia_valid & ATTR_GID && inode->i_gid != iattr->ia_gid;
Jan Kara657d3bf2008-07-25 01:46:52 -07001671 char warntype_to[MAXQUOTAS];
1672 char warntype_from_inodes[MAXQUOTAS], warntype_from_space[MAXQUOTAS];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673
Ingo Molnard3be9152006-03-23 03:00:29 -08001674 /* First test before acquiring mutex - solves deadlocks when we
1675 * re-enter the quota code and are already holding the mutex */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676 if (IS_NOQUOTA(inode))
1677 return QUOTA_OK;
Jan Karacc334122009-01-12 17:23:05 +01001678 /* Initialize the arrays */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
Jan Karadd6f3c62009-01-26 16:01:43 +01001680 transfer_from[cnt] = NULL;
1681 transfer_to[cnt] = NULL;
Jan Kara657d3bf2008-07-25 01:46:52 -07001682 warntype_to[cnt] = QUOTA_NL_NOWARN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683 }
Jan Kara268157b2009-01-27 15:47:22 +01001684 if (chuid)
1685 transfer_to[USRQUOTA] = dqget(inode->i_sb, iattr->ia_uid,
1686 USRQUOTA);
1687 if (chgid)
1688 transfer_to[GRPQUOTA] = dqget(inode->i_sb, iattr->ia_gid,
1689 GRPQUOTA);
Jan Karacc334122009-01-12 17:23:05 +01001690
1691 down_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
1692 /* Now recheck reliably when holding dqptr_sem */
1693 if (IS_NOQUOTA(inode)) { /* File without quota accounting? */
1694 up_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
1695 goto put_all;
1696 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697 spin_lock(&dq_data_lock);
Mingming Cao740d9dc2009-01-13 16:43:14 +01001698 cur_space = inode_get_bytes(inode);
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001699 rsv_space = inode_get_rsv_space(inode);
Mingming Cao740d9dc2009-01-13 16:43:14 +01001700 space = cur_space + rsv_space;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701 /* Build the transfer_from list and check the limits */
1702 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
Jan Karadd6f3c62009-01-26 16:01:43 +01001703 if (!transfer_to[cnt])
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704 continue;
1705 transfer_from[cnt] = inode->i_dquot[cnt];
Jan Kara657d3bf2008-07-25 01:46:52 -07001706 if (check_idq(transfer_to[cnt], 1, warntype_to + cnt) ==
1707 NO_QUOTA || check_bdq(transfer_to[cnt], space, 0,
1708 warntype_to + cnt) == NO_QUOTA)
Jan Karacc334122009-01-12 17:23:05 +01001709 goto over_quota;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710 }
1711
1712 /*
1713 * Finally perform the needed transfer from transfer_from to transfer_to
1714 */
1715 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1716 /*
1717 * Skip changes for same uid or gid or for turned off quota-type.
1718 */
Jan Karadd6f3c62009-01-26 16:01:43 +01001719 if (!transfer_to[cnt])
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720 continue;
1721
1722 /* Due to IO error we might not have transfer_from[] structure */
1723 if (transfer_from[cnt]) {
Jan Kara657d3bf2008-07-25 01:46:52 -07001724 warntype_from_inodes[cnt] =
1725 info_idq_free(transfer_from[cnt], 1);
1726 warntype_from_space[cnt] =
1727 info_bdq_free(transfer_from[cnt], space);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728 dquot_decr_inodes(transfer_from[cnt], 1);
Mingming Cao740d9dc2009-01-13 16:43:14 +01001729 dquot_decr_space(transfer_from[cnt], cur_space);
1730 dquot_free_reserved_space(transfer_from[cnt],
1731 rsv_space);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732 }
1733
1734 dquot_incr_inodes(transfer_to[cnt], 1);
Mingming Cao740d9dc2009-01-13 16:43:14 +01001735 dquot_incr_space(transfer_to[cnt], cur_space);
1736 dquot_resv_space(transfer_to[cnt], rsv_space);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737
1738 inode->i_dquot[cnt] = transfer_to[cnt];
1739 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740 spin_unlock(&dq_data_lock);
Jan Karacc334122009-01-12 17:23:05 +01001741 up_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
1742
Dmitry Monakhovdc52dd32009-12-14 15:21:15 +03001743 mark_all_dquot_dirty(transfer_from);
1744 mark_all_dquot_dirty(transfer_to);
1745 /* The reference we got is transferred to the inode */
1746 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1747 transfer_to[cnt] = NULL;
Jan Karacc334122009-01-12 17:23:05 +01001748warn_put_all:
Jan Kara657d3bf2008-07-25 01:46:52 -07001749 flush_warnings(transfer_to, warntype_to);
1750 flush_warnings(transfer_from, warntype_from_inodes);
1751 flush_warnings(transfer_from, warntype_from_space);
Jan Karacc334122009-01-12 17:23:05 +01001752put_all:
Dmitry Monakhovdc52dd32009-12-14 15:21:15 +03001753 dqput_all(transfer_from);
1754 dqput_all(transfer_to);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755 return ret;
Jan Karacc334122009-01-12 17:23:05 +01001756over_quota:
1757 spin_unlock(&dq_data_lock);
1758 up_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
1759 /* Clear dquot pointers we don't want to dqput() */
1760 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
Jan Karadd6f3c62009-01-26 16:01:43 +01001761 transfer_from[cnt] = NULL;
Jan Karacc334122009-01-12 17:23:05 +01001762 ret = NO_QUOTA;
1763 goto warn_put_all;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764}
Mingming Cao08d03502009-01-14 16:19:32 +01001765EXPORT_SYMBOL(dquot_transfer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766
Jan Karab85f4b82008-07-25 01:46:50 -07001767/* Wrapper for transferring ownership of an inode */
1768int vfs_dq_transfer(struct inode *inode, struct iattr *iattr)
1769{
Jan Karaf55abc02008-08-20 17:50:32 +02001770 if (sb_any_quota_active(inode->i_sb) && !IS_NOQUOTA(inode)) {
Jan Karab85f4b82008-07-25 01:46:50 -07001771 vfs_dq_init(inode);
1772 if (inode->i_sb->dq_op->transfer(inode, iattr) == NO_QUOTA)
1773 return 1;
1774 }
1775 return 0;
1776}
Mingming Cao08d03502009-01-14 16:19:32 +01001777EXPORT_SYMBOL(vfs_dq_transfer);
Jan Karab85f4b82008-07-25 01:46:50 -07001778
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779/*
1780 * Write info of quota file to disk
1781 */
1782int dquot_commit_info(struct super_block *sb, int type)
1783{
1784 int ret;
1785 struct quota_info *dqopt = sb_dqopt(sb);
1786
Ingo Molnard3be9152006-03-23 03:00:29 -08001787 mutex_lock(&dqopt->dqio_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788 ret = dqopt->ops[type]->write_file_info(sb, type);
Ingo Molnard3be9152006-03-23 03:00:29 -08001789 mutex_unlock(&dqopt->dqio_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790 return ret;
1791}
Mingming Cao08d03502009-01-14 16:19:32 +01001792EXPORT_SYMBOL(dquot_commit_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793
1794/*
1795 * Definitions of diskquota operations.
1796 */
Alexey Dobriyan61e225d2009-09-21 17:01:08 -07001797const struct dquot_operations dquot_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798 .initialize = dquot_initialize,
1799 .drop = dquot_drop,
1800 .alloc_space = dquot_alloc_space,
1801 .alloc_inode = dquot_alloc_inode,
1802 .free_space = dquot_free_space,
1803 .free_inode = dquot_free_inode,
1804 .transfer = dquot_transfer,
1805 .write_dquot = dquot_commit,
1806 .acquire_dquot = dquot_acquire,
1807 .release_dquot = dquot_release,
1808 .mark_dirty = dquot_mark_dquot_dirty,
Jan Kara74f783a2008-08-19 14:51:22 +02001809 .write_info = dquot_commit_info,
1810 .alloc_dquot = dquot_alloc,
1811 .destroy_dquot = dquot_destroy,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812};
1813
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814/*
1815 * Turn quota off on a device. type == -1 ==> quotaoff for all types (umount)
1816 */
Jan Karaf55abc02008-08-20 17:50:32 +02001817int vfs_quota_disable(struct super_block *sb, int type, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818{
Jan Kara0ff5af82008-04-28 02:14:33 -07001819 int cnt, ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820 struct quota_info *dqopt = sb_dqopt(sb);
1821 struct inode *toputinode[MAXQUOTAS];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822
Jan Karaf55abc02008-08-20 17:50:32 +02001823 /* Cannot turn off usage accounting without turning off limits, or
1824 * suspend quotas and simultaneously turn quotas off. */
1825 if ((flags & DQUOT_USAGE_ENABLED && !(flags & DQUOT_LIMITS_ENABLED))
1826 || (flags & DQUOT_SUSPENDED && flags & (DQUOT_LIMITS_ENABLED |
1827 DQUOT_USAGE_ENABLED)))
1828 return -EINVAL;
1829
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830 /* We need to serialize quota_off() for device */
Ingo Molnard3be9152006-03-23 03:00:29 -08001831 mutex_lock(&dqopt->dqonoff_mutex);
Jan Kara9377abd2008-05-12 14:02:08 -07001832
1833 /*
1834 * Skip everything if there's nothing to do. We have to do this because
1835 * sometimes we are called when fill_super() failed and calling
1836 * sync_fs() in such cases does no good.
1837 */
Jan Karaf55abc02008-08-20 17:50:32 +02001838 if (!sb_any_quota_loaded(sb)) {
Jan Kara9377abd2008-05-12 14:02:08 -07001839 mutex_unlock(&dqopt->dqonoff_mutex);
1840 return 0;
1841 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1843 toputinode[cnt] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844 if (type != -1 && cnt != type)
1845 continue;
Jan Karaf55abc02008-08-20 17:50:32 +02001846 if (!sb_has_quota_loaded(sb, cnt))
Jan Kara0ff5af82008-04-28 02:14:33 -07001847 continue;
Jan Karaf55abc02008-08-20 17:50:32 +02001848
1849 if (flags & DQUOT_SUSPENDED) {
Jan Karacc334122009-01-12 17:23:05 +01001850 spin_lock(&dq_state_lock);
Jan Karaf55abc02008-08-20 17:50:32 +02001851 dqopt->flags |=
1852 dquot_state_flag(DQUOT_SUSPENDED, cnt);
Jan Karacc334122009-01-12 17:23:05 +01001853 spin_unlock(&dq_state_lock);
Jan Karaf55abc02008-08-20 17:50:32 +02001854 } else {
Jan Karacc334122009-01-12 17:23:05 +01001855 spin_lock(&dq_state_lock);
Jan Karaf55abc02008-08-20 17:50:32 +02001856 dqopt->flags &= ~dquot_state_flag(flags, cnt);
1857 /* Turning off suspended quotas? */
1858 if (!sb_has_quota_loaded(sb, cnt) &&
1859 sb_has_quota_suspended(sb, cnt)) {
1860 dqopt->flags &= ~dquot_state_flag(
1861 DQUOT_SUSPENDED, cnt);
Jan Karacc334122009-01-12 17:23:05 +01001862 spin_unlock(&dq_state_lock);
Jan Karaf55abc02008-08-20 17:50:32 +02001863 iput(dqopt->files[cnt]);
1864 dqopt->files[cnt] = NULL;
1865 continue;
1866 }
Jan Karacc334122009-01-12 17:23:05 +01001867 spin_unlock(&dq_state_lock);
Jan Kara0ff5af82008-04-28 02:14:33 -07001868 }
Jan Karaf55abc02008-08-20 17:50:32 +02001869
1870 /* We still have to keep quota loaded? */
1871 if (sb_has_quota_loaded(sb, cnt) && !(flags & DQUOT_SUSPENDED))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873
1874 /* Note: these are blocking operations */
1875 drop_dquot_ref(sb, cnt);
1876 invalidate_dquots(sb, cnt);
1877 /*
Jan Kara268157b2009-01-27 15:47:22 +01001878 * Now all dquots should be invalidated, all writes done so we
1879 * should be only users of the info. No locks needed.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880 */
1881 if (info_dirty(&dqopt->info[cnt]))
1882 sb->dq_op->write_info(sb, cnt);
1883 if (dqopt->ops[cnt]->free_file_info)
1884 dqopt->ops[cnt]->free_file_info(sb, cnt);
1885 put_quota_format(dqopt->info[cnt].dqi_format);
1886
1887 toputinode[cnt] = dqopt->files[cnt];
Jan Karaf55abc02008-08-20 17:50:32 +02001888 if (!sb_has_quota_loaded(sb, cnt))
Jan Kara0ff5af82008-04-28 02:14:33 -07001889 dqopt->files[cnt] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890 dqopt->info[cnt].dqi_flags = 0;
1891 dqopt->info[cnt].dqi_igrace = 0;
1892 dqopt->info[cnt].dqi_bgrace = 0;
1893 dqopt->ops[cnt] = NULL;
1894 }
Ingo Molnard3be9152006-03-23 03:00:29 -08001895 mutex_unlock(&dqopt->dqonoff_mutex);
Jan Karaca785ec2008-09-30 17:53:37 +02001896
1897 /* Skip syncing and setting flags if quota files are hidden */
1898 if (dqopt->flags & DQUOT_QUOTA_SYS_FILE)
1899 goto put_inodes;
1900
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901 /* Sync the superblock so that buffers with quota data are written to
Al Viro7b7b1ac2005-11-07 17:13:39 -05001902 * disk (and so userspace sees correct data afterwards). */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903 if (sb->s_op->sync_fs)
1904 sb->s_op->sync_fs(sb, 1);
1905 sync_blockdev(sb->s_bdev);
1906 /* Now the quota files are just ordinary files and we can set the
1907 * inode flags back. Moreover we discard the pagecache so that
1908 * userspace sees the writes we did bypassing the pagecache. We
1909 * must also discard the blockdev buffers so that we see the
1910 * changes done by userspace on the next quotaon() */
1911 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1912 if (toputinode[cnt]) {
Ingo Molnard3be9152006-03-23 03:00:29 -08001913 mutex_lock(&dqopt->dqonoff_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914 /* If quota was reenabled in the meantime, we have
1915 * nothing to do */
Jan Karaf55abc02008-08-20 17:50:32 +02001916 if (!sb_has_quota_loaded(sb, cnt)) {
Jan Kara268157b2009-01-27 15:47:22 +01001917 mutex_lock_nested(&toputinode[cnt]->i_mutex,
1918 I_MUTEX_QUOTA);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919 toputinode[cnt]->i_flags &= ~(S_IMMUTABLE |
1920 S_NOATIME | S_NOQUOTA);
Jan Kara268157b2009-01-27 15:47:22 +01001921 truncate_inode_pages(&toputinode[cnt]->i_data,
1922 0);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001923 mutex_unlock(&toputinode[cnt]->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924 mark_inode_dirty(toputinode[cnt]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925 }
Ingo Molnard3be9152006-03-23 03:00:29 -08001926 mutex_unlock(&dqopt->dqonoff_mutex);
Jan Karaca785ec2008-09-30 17:53:37 +02001927 }
1928 if (sb->s_bdev)
1929 invalidate_bdev(sb->s_bdev);
1930put_inodes:
1931 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1932 if (toputinode[cnt]) {
Jan Kara0ff5af82008-04-28 02:14:33 -07001933 /* On remount RO, we keep the inode pointer so that we
Jan Karaf55abc02008-08-20 17:50:32 +02001934 * can reenable quota on the subsequent remount RW. We
1935 * have to check 'flags' variable and not use sb_has_
1936 * function because another quotaon / quotaoff could
1937 * change global state before we got here. We refuse
1938 * to suspend quotas when there is pending delete on
1939 * the quota file... */
1940 if (!(flags & DQUOT_SUSPENDED))
Jan Kara0ff5af82008-04-28 02:14:33 -07001941 iput(toputinode[cnt]);
1942 else if (!toputinode[cnt]->i_nlink)
1943 ret = -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944 }
Jan Kara0ff5af82008-04-28 02:14:33 -07001945 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946}
Mingming Cao08d03502009-01-14 16:19:32 +01001947EXPORT_SYMBOL(vfs_quota_disable);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948
Jan Karaf55abc02008-08-20 17:50:32 +02001949int vfs_quota_off(struct super_block *sb, int type, int remount)
1950{
1951 return vfs_quota_disable(sb, type, remount ? DQUOT_SUSPENDED :
1952 (DQUOT_USAGE_ENABLED | DQUOT_LIMITS_ENABLED));
1953}
Mingming Cao08d03502009-01-14 16:19:32 +01001954EXPORT_SYMBOL(vfs_quota_off);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955/*
1956 * Turn quotas on on a device
1957 */
1958
Jan Karaf55abc02008-08-20 17:50:32 +02001959/*
1960 * Helper function to turn quotas on when we already have the inode of
1961 * quota file and no quota information is loaded.
1962 */
1963static int vfs_load_quota_inode(struct inode *inode, int type, int format_id,
1964 unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965{
1966 struct quota_format_type *fmt = find_quota_format(format_id);
1967 struct super_block *sb = inode->i_sb;
1968 struct quota_info *dqopt = sb_dqopt(sb);
1969 int error;
1970 int oldflags = -1;
1971
1972 if (!fmt)
1973 return -ESRCH;
1974 if (!S_ISREG(inode->i_mode)) {
1975 error = -EACCES;
1976 goto out_fmt;
1977 }
1978 if (IS_RDONLY(inode)) {
1979 error = -EROFS;
1980 goto out_fmt;
1981 }
1982 if (!sb->s_op->quota_write || !sb->s_op->quota_read) {
1983 error = -EINVAL;
1984 goto out_fmt;
1985 }
Jan Karaf55abc02008-08-20 17:50:32 +02001986 /* Usage always has to be set... */
1987 if (!(flags & DQUOT_USAGE_ENABLED)) {
1988 error = -EINVAL;
1989 goto out_fmt;
1990 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991
Jan Karaca785ec2008-09-30 17:53:37 +02001992 if (!(dqopt->flags & DQUOT_QUOTA_SYS_FILE)) {
1993 /* As we bypass the pagecache we must now flush the inode so
1994 * that we see all the changes from userspace... */
1995 write_inode_now(inode, 1);
1996 /* And now flush the block cache so that kernel sees the
1997 * changes */
1998 invalidate_bdev(sb->s_bdev);
1999 }
Ingo Molnard3be9152006-03-23 03:00:29 -08002000 mutex_lock(&dqopt->dqonoff_mutex);
Jan Karaf55abc02008-08-20 17:50:32 +02002001 if (sb_has_quota_loaded(sb, type)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002 error = -EBUSY;
2003 goto out_lock;
2004 }
Jan Karaca785ec2008-09-30 17:53:37 +02002005
2006 if (!(dqopt->flags & DQUOT_QUOTA_SYS_FILE)) {
2007 /* We don't want quota and atime on quota files (deadlocks
2008 * possible) Also nobody should write to the file - we use
2009 * special IO operations which ignore the immutable bit. */
2010 down_write(&dqopt->dqptr_sem);
Jan Karadee86562009-07-22 18:12:17 +02002011 mutex_lock_nested(&inode->i_mutex, I_MUTEX_QUOTA);
Jan Kara268157b2009-01-27 15:47:22 +01002012 oldflags = inode->i_flags & (S_NOATIME | S_IMMUTABLE |
2013 S_NOQUOTA);
Jan Karaca785ec2008-09-30 17:53:37 +02002014 inode->i_flags |= S_NOQUOTA | S_NOATIME | S_IMMUTABLE;
Jan Karadee86562009-07-22 18:12:17 +02002015 mutex_unlock(&inode->i_mutex);
Jan Karaca785ec2008-09-30 17:53:37 +02002016 up_write(&dqopt->dqptr_sem);
2017 sb->dq_op->drop(inode);
2018 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019
2020 error = -EIO;
2021 dqopt->files[type] = igrab(inode);
2022 if (!dqopt->files[type])
2023 goto out_lock;
2024 error = -EINVAL;
2025 if (!fmt->qf_ops->check_quota_file(sb, type))
2026 goto out_file_init;
2027
2028 dqopt->ops[type] = fmt->qf_ops;
2029 dqopt->info[type].dqi_format = fmt;
Jan Kara0ff5af82008-04-28 02:14:33 -07002030 dqopt->info[type].dqi_fmt_id = format_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031 INIT_LIST_HEAD(&dqopt->info[type].dqi_dirty_list);
Ingo Molnard3be9152006-03-23 03:00:29 -08002032 mutex_lock(&dqopt->dqio_mutex);
Jan Kara268157b2009-01-27 15:47:22 +01002033 error = dqopt->ops[type]->read_file_info(sb, type);
2034 if (error < 0) {
Ingo Molnard3be9152006-03-23 03:00:29 -08002035 mutex_unlock(&dqopt->dqio_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036 goto out_file_init;
2037 }
Ingo Molnard3be9152006-03-23 03:00:29 -08002038 mutex_unlock(&dqopt->dqio_mutex);
Jan Karacc334122009-01-12 17:23:05 +01002039 spin_lock(&dq_state_lock);
Jan Karaf55abc02008-08-20 17:50:32 +02002040 dqopt->flags |= dquot_state_flag(flags, type);
Jan Karacc334122009-01-12 17:23:05 +01002041 spin_unlock(&dq_state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042
2043 add_dquot_ref(sb, type);
Ingo Molnard3be9152006-03-23 03:00:29 -08002044 mutex_unlock(&dqopt->dqonoff_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045
2046 return 0;
2047
2048out_file_init:
2049 dqopt->files[type] = NULL;
2050 iput(inode);
2051out_lock:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052 if (oldflags != -1) {
2053 down_write(&dqopt->dqptr_sem);
Jan Karadee86562009-07-22 18:12:17 +02002054 mutex_lock_nested(&inode->i_mutex, I_MUTEX_QUOTA);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055 /* Set the flags back (in the case of accidental quotaon()
2056 * on a wrong file we don't want to mess up the flags) */
2057 inode->i_flags &= ~(S_NOATIME | S_NOQUOTA | S_IMMUTABLE);
2058 inode->i_flags |= oldflags;
Jan Karadee86562009-07-22 18:12:17 +02002059 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060 up_write(&dqopt->dqptr_sem);
2061 }
Jiaying Zhangd01730d2009-07-07 18:15:21 +02002062 mutex_unlock(&dqopt->dqonoff_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063out_fmt:
2064 put_quota_format(fmt);
2065
2066 return error;
2067}
2068
Jan Kara0ff5af82008-04-28 02:14:33 -07002069/* Reenable quotas on remount RW */
2070static int vfs_quota_on_remount(struct super_block *sb, int type)
2071{
2072 struct quota_info *dqopt = sb_dqopt(sb);
2073 struct inode *inode;
2074 int ret;
Jan Karaf55abc02008-08-20 17:50:32 +02002075 unsigned int flags;
Jan Kara0ff5af82008-04-28 02:14:33 -07002076
2077 mutex_lock(&dqopt->dqonoff_mutex);
2078 if (!sb_has_quota_suspended(sb, type)) {
2079 mutex_unlock(&dqopt->dqonoff_mutex);
2080 return 0;
2081 }
Jan Kara0ff5af82008-04-28 02:14:33 -07002082 inode = dqopt->files[type];
2083 dqopt->files[type] = NULL;
Jan Karacc334122009-01-12 17:23:05 +01002084 spin_lock(&dq_state_lock);
Jan Karaf55abc02008-08-20 17:50:32 +02002085 flags = dqopt->flags & dquot_state_flag(DQUOT_USAGE_ENABLED |
2086 DQUOT_LIMITS_ENABLED, type);
2087 dqopt->flags &= ~dquot_state_flag(DQUOT_STATE_FLAGS, type);
Jan Karacc334122009-01-12 17:23:05 +01002088 spin_unlock(&dq_state_lock);
Jan Kara0ff5af82008-04-28 02:14:33 -07002089 mutex_unlock(&dqopt->dqonoff_mutex);
2090
Jan Karaf55abc02008-08-20 17:50:32 +02002091 flags = dquot_generic_flag(flags, type);
2092 ret = vfs_load_quota_inode(inode, type, dqopt->info[type].dqi_fmt_id,
2093 flags);
Jan Kara0ff5af82008-04-28 02:14:33 -07002094 iput(inode);
2095
2096 return ret;
2097}
2098
Al Viro77e69da2008-08-01 04:29:18 -04002099int vfs_quota_on_path(struct super_block *sb, int type, int format_id,
2100 struct path *path)
2101{
2102 int error = security_quota_on(path->dentry);
2103 if (error)
2104 return error;
2105 /* Quota file not on the same filesystem? */
2106 if (path->mnt->mnt_sb != sb)
2107 error = -EXDEV;
2108 else
Jan Karaf55abc02008-08-20 17:50:32 +02002109 error = vfs_load_quota_inode(path->dentry->d_inode, type,
2110 format_id, DQUOT_USAGE_ENABLED |
2111 DQUOT_LIMITS_ENABLED);
Al Viro77e69da2008-08-01 04:29:18 -04002112 return error;
2113}
Mingming Cao08d03502009-01-14 16:19:32 +01002114EXPORT_SYMBOL(vfs_quota_on_path);
Al Viro77e69da2008-08-01 04:29:18 -04002115
Al Viro82646132008-08-02 00:57:06 -04002116int vfs_quota_on(struct super_block *sb, int type, int format_id, char *name,
Jan Kara0ff5af82008-04-28 02:14:33 -07002117 int remount)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118{
Al Viro82646132008-08-02 00:57:06 -04002119 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120 int error;
2121
Jan Kara0ff5af82008-04-28 02:14:33 -07002122 if (remount)
2123 return vfs_quota_on_remount(sb, type);
2124
Al Viro82646132008-08-02 00:57:06 -04002125 error = kern_path(name, LOOKUP_FOLLOW, &path);
Al Viro77e69da2008-08-01 04:29:18 -04002126 if (!error) {
Al Viro82646132008-08-02 00:57:06 -04002127 error = vfs_quota_on_path(sb, type, format_id, &path);
2128 path_put(&path);
Al Viro77e69da2008-08-01 04:29:18 -04002129 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130 return error;
2131}
Mingming Cao08d03502009-01-14 16:19:32 +01002132EXPORT_SYMBOL(vfs_quota_on);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133
2134/*
Jan Karaf55abc02008-08-20 17:50:32 +02002135 * More powerful function for turning on quotas allowing setting
2136 * of individual quota flags
2137 */
2138int vfs_quota_enable(struct inode *inode, int type, int format_id,
2139 unsigned int flags)
2140{
2141 int ret = 0;
2142 struct super_block *sb = inode->i_sb;
2143 struct quota_info *dqopt = sb_dqopt(sb);
2144
2145 /* Just unsuspend quotas? */
2146 if (flags & DQUOT_SUSPENDED)
2147 return vfs_quota_on_remount(sb, type);
2148 if (!flags)
2149 return 0;
2150 /* Just updating flags needed? */
2151 if (sb_has_quota_loaded(sb, type)) {
2152 mutex_lock(&dqopt->dqonoff_mutex);
2153 /* Now do a reliable test... */
2154 if (!sb_has_quota_loaded(sb, type)) {
2155 mutex_unlock(&dqopt->dqonoff_mutex);
2156 goto load_quota;
2157 }
2158 if (flags & DQUOT_USAGE_ENABLED &&
2159 sb_has_quota_usage_enabled(sb, type)) {
2160 ret = -EBUSY;
2161 goto out_lock;
2162 }
2163 if (flags & DQUOT_LIMITS_ENABLED &&
2164 sb_has_quota_limits_enabled(sb, type)) {
2165 ret = -EBUSY;
2166 goto out_lock;
2167 }
Jan Karacc334122009-01-12 17:23:05 +01002168 spin_lock(&dq_state_lock);
Jan Karaf55abc02008-08-20 17:50:32 +02002169 sb_dqopt(sb)->flags |= dquot_state_flag(flags, type);
Jan Karacc334122009-01-12 17:23:05 +01002170 spin_unlock(&dq_state_lock);
Jan Karaf55abc02008-08-20 17:50:32 +02002171out_lock:
2172 mutex_unlock(&dqopt->dqonoff_mutex);
2173 return ret;
2174 }
2175
2176load_quota:
2177 return vfs_load_quota_inode(inode, type, format_id, flags);
2178}
Mingming Cao08d03502009-01-14 16:19:32 +01002179EXPORT_SYMBOL(vfs_quota_enable);
Jan Karaf55abc02008-08-20 17:50:32 +02002180
2181/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182 * This function is used when filesystem needs to initialize quotas
2183 * during mount time.
2184 */
Christoph Hellwig84de8562005-06-23 00:09:16 -07002185int vfs_quota_on_mount(struct super_block *sb, char *qf_name,
2186 int format_id, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187{
Christoph Hellwig84de8562005-06-23 00:09:16 -07002188 struct dentry *dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189 int error;
2190
Jan Karac56818d2009-11-12 15:42:08 +01002191 mutex_lock(&sb->s_root->d_inode->i_mutex);
Christoph Hellwig2fa389c2005-06-23 00:09:16 -07002192 dentry = lookup_one_len(qf_name, sb->s_root, strlen(qf_name));
Jan Karac56818d2009-11-12 15:42:08 +01002193 mutex_unlock(&sb->s_root->d_inode->i_mutex);
Christoph Hellwig84de8562005-06-23 00:09:16 -07002194 if (IS_ERR(dentry))
2195 return PTR_ERR(dentry);
2196
Jan Kara154f4842005-11-28 13:44:14 -08002197 if (!dentry->d_inode) {
2198 error = -ENOENT;
2199 goto out;
2200 }
2201
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202 error = security_quota_on(dentry);
Christoph Hellwig84de8562005-06-23 00:09:16 -07002203 if (!error)
Jan Karaf55abc02008-08-20 17:50:32 +02002204 error = vfs_load_quota_inode(dentry->d_inode, type, format_id,
2205 DQUOT_USAGE_ENABLED | DQUOT_LIMITS_ENABLED);
Christoph Hellwig84de8562005-06-23 00:09:16 -07002206
Jan Kara154f4842005-11-28 13:44:14 -08002207out:
Christoph Hellwig84de8562005-06-23 00:09:16 -07002208 dput(dentry);
2209 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210}
Mingming Cao08d03502009-01-14 16:19:32 +01002211EXPORT_SYMBOL(vfs_quota_on_mount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212
Jan Karab85f4b82008-07-25 01:46:50 -07002213/* Wrapper to turn on quotas when remounting rw */
2214int vfs_dq_quota_on_remount(struct super_block *sb)
2215{
2216 int cnt;
2217 int ret = 0, err;
2218
2219 if (!sb->s_qcop || !sb->s_qcop->quota_on)
2220 return -ENOSYS;
2221 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
2222 err = sb->s_qcop->quota_on(sb, cnt, 0, NULL, 1);
2223 if (err < 0 && !ret)
2224 ret = err;
2225 }
2226 return ret;
2227}
Mingming Cao08d03502009-01-14 16:19:32 +01002228EXPORT_SYMBOL(vfs_dq_quota_on_remount);
Jan Karab85f4b82008-07-25 01:46:50 -07002229
Jan Kara12095462008-08-20 14:45:12 +02002230static inline qsize_t qbtos(qsize_t blocks)
2231{
2232 return blocks << QIF_DQBLKSIZE_BITS;
2233}
2234
2235static inline qsize_t stoqb(qsize_t space)
2236{
2237 return (space + QIF_DQBLKSIZE - 1) >> QIF_DQBLKSIZE_BITS;
2238}
2239
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240/* Generic routine for getting common part of quota structure */
2241static void do_get_dqblk(struct dquot *dquot, struct if_dqblk *di)
2242{
2243 struct mem_dqblk *dm = &dquot->dq_dqb;
2244
2245 spin_lock(&dq_data_lock);
Jan Kara12095462008-08-20 14:45:12 +02002246 di->dqb_bhardlimit = stoqb(dm->dqb_bhardlimit);
2247 di->dqb_bsoftlimit = stoqb(dm->dqb_bsoftlimit);
Mingming Caof18df222009-01-13 16:43:09 +01002248 di->dqb_curspace = dm->dqb_curspace + dm->dqb_rsvspace;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249 di->dqb_ihardlimit = dm->dqb_ihardlimit;
2250 di->dqb_isoftlimit = dm->dqb_isoftlimit;
2251 di->dqb_curinodes = dm->dqb_curinodes;
2252 di->dqb_btime = dm->dqb_btime;
2253 di->dqb_itime = dm->dqb_itime;
2254 di->dqb_valid = QIF_ALL;
2255 spin_unlock(&dq_data_lock);
2256}
2257
Jan Kara268157b2009-01-27 15:47:22 +01002258int vfs_get_dqblk(struct super_block *sb, int type, qid_t id,
2259 struct if_dqblk *di)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260{
2261 struct dquot *dquot;
2262
Jan Karacc334122009-01-12 17:23:05 +01002263 dquot = dqget(sb, id, type);
Jan Karadd6f3c62009-01-26 16:01:43 +01002264 if (!dquot)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265 return -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266 do_get_dqblk(dquot, di);
2267 dqput(dquot);
Jan Karacc334122009-01-12 17:23:05 +01002268
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269 return 0;
2270}
Mingming Cao08d03502009-01-14 16:19:32 +01002271EXPORT_SYMBOL(vfs_get_dqblk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272
2273/* Generic routine for setting common part of quota structure */
Andrew Perepechko338bf9a2008-04-28 02:14:31 -07002274static int do_set_dqblk(struct dquot *dquot, struct if_dqblk *di)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275{
2276 struct mem_dqblk *dm = &dquot->dq_dqb;
2277 int check_blim = 0, check_ilim = 0;
Andrew Perepechko338bf9a2008-04-28 02:14:31 -07002278 struct mem_dqinfo *dqi = &sb_dqopt(dquot->dq_sb)->info[dquot->dq_type];
2279
2280 if ((di->dqb_valid & QIF_BLIMITS &&
2281 (di->dqb_bhardlimit > dqi->dqi_maxblimit ||
2282 di->dqb_bsoftlimit > dqi->dqi_maxblimit)) ||
2283 (di->dqb_valid & QIF_ILIMITS &&
2284 (di->dqb_ihardlimit > dqi->dqi_maxilimit ||
2285 di->dqb_isoftlimit > dqi->dqi_maxilimit)))
2286 return -ERANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287
2288 spin_lock(&dq_data_lock);
2289 if (di->dqb_valid & QIF_SPACE) {
Mingming Caof18df222009-01-13 16:43:09 +01002290 dm->dqb_curspace = di->dqb_curspace - dm->dqb_rsvspace;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291 check_blim = 1;
Jan Kara4d59bce2008-10-02 16:48:10 +02002292 __set_bit(DQ_LASTSET_B + QIF_SPACE_B, &dquot->dq_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293 }
2294 if (di->dqb_valid & QIF_BLIMITS) {
Jan Kara12095462008-08-20 14:45:12 +02002295 dm->dqb_bsoftlimit = qbtos(di->dqb_bsoftlimit);
2296 dm->dqb_bhardlimit = qbtos(di->dqb_bhardlimit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297 check_blim = 1;
Jan Kara4d59bce2008-10-02 16:48:10 +02002298 __set_bit(DQ_LASTSET_B + QIF_BLIMITS_B, &dquot->dq_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299 }
2300 if (di->dqb_valid & QIF_INODES) {
2301 dm->dqb_curinodes = di->dqb_curinodes;
2302 check_ilim = 1;
Jan Kara4d59bce2008-10-02 16:48:10 +02002303 __set_bit(DQ_LASTSET_B + QIF_INODES_B, &dquot->dq_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304 }
2305 if (di->dqb_valid & QIF_ILIMITS) {
2306 dm->dqb_isoftlimit = di->dqb_isoftlimit;
2307 dm->dqb_ihardlimit = di->dqb_ihardlimit;
2308 check_ilim = 1;
Jan Kara4d59bce2008-10-02 16:48:10 +02002309 __set_bit(DQ_LASTSET_B + QIF_ILIMITS_B, &dquot->dq_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310 }
Jan Kara4d59bce2008-10-02 16:48:10 +02002311 if (di->dqb_valid & QIF_BTIME) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312 dm->dqb_btime = di->dqb_btime;
Jan Karae04a88a92009-01-07 18:07:29 -08002313 check_blim = 1;
Jan Kara4d59bce2008-10-02 16:48:10 +02002314 __set_bit(DQ_LASTSET_B + QIF_BTIME_B, &dquot->dq_flags);
2315 }
2316 if (di->dqb_valid & QIF_ITIME) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317 dm->dqb_itime = di->dqb_itime;
Jan Karae04a88a92009-01-07 18:07:29 -08002318 check_ilim = 1;
Jan Kara4d59bce2008-10-02 16:48:10 +02002319 __set_bit(DQ_LASTSET_B + QIF_ITIME_B, &dquot->dq_flags);
2320 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002321
2322 if (check_blim) {
Jan Kara268157b2009-01-27 15:47:22 +01002323 if (!dm->dqb_bsoftlimit ||
2324 dm->dqb_curspace < dm->dqb_bsoftlimit) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325 dm->dqb_btime = 0;
2326 clear_bit(DQ_BLKS_B, &dquot->dq_flags);
Jan Kara268157b2009-01-27 15:47:22 +01002327 } else if (!(di->dqb_valid & QIF_BTIME))
2328 /* Set grace only if user hasn't provided his own... */
Andrew Perepechko338bf9a2008-04-28 02:14:31 -07002329 dm->dqb_btime = get_seconds() + dqi->dqi_bgrace;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330 }
2331 if (check_ilim) {
Jan Kara268157b2009-01-27 15:47:22 +01002332 if (!dm->dqb_isoftlimit ||
2333 dm->dqb_curinodes < dm->dqb_isoftlimit) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334 dm->dqb_itime = 0;
2335 clear_bit(DQ_INODES_B, &dquot->dq_flags);
Jan Kara268157b2009-01-27 15:47:22 +01002336 } else if (!(di->dqb_valid & QIF_ITIME))
2337 /* Set grace only if user hasn't provided his own... */
Andrew Perepechko338bf9a2008-04-28 02:14:31 -07002338 dm->dqb_itime = get_seconds() + dqi->dqi_igrace;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339 }
Jan Kara268157b2009-01-27 15:47:22 +01002340 if (dm->dqb_bhardlimit || dm->dqb_bsoftlimit || dm->dqb_ihardlimit ||
2341 dm->dqb_isoftlimit)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342 clear_bit(DQ_FAKE_B, &dquot->dq_flags);
2343 else
2344 set_bit(DQ_FAKE_B, &dquot->dq_flags);
2345 spin_unlock(&dq_data_lock);
2346 mark_dquot_dirty(dquot);
Andrew Perepechko338bf9a2008-04-28 02:14:31 -07002347
2348 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349}
2350
Jan Kara268157b2009-01-27 15:47:22 +01002351int vfs_set_dqblk(struct super_block *sb, int type, qid_t id,
2352 struct if_dqblk *di)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002353{
2354 struct dquot *dquot;
Andrew Perepechko338bf9a2008-04-28 02:14:31 -07002355 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002356
Jan Karaf55abc02008-08-20 17:50:32 +02002357 dquot = dqget(sb, id, type);
2358 if (!dquot) {
2359 rc = -ESRCH;
2360 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361 }
Andrew Perepechko338bf9a2008-04-28 02:14:31 -07002362 rc = do_set_dqblk(dquot, di);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363 dqput(dquot);
Jan Karaf55abc02008-08-20 17:50:32 +02002364out:
Andrew Perepechko338bf9a2008-04-28 02:14:31 -07002365 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366}
Mingming Cao08d03502009-01-14 16:19:32 +01002367EXPORT_SYMBOL(vfs_set_dqblk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002368
2369/* Generic routine for getting common part of quota file information */
2370int vfs_get_dqinfo(struct super_block *sb, int type, struct if_dqinfo *ii)
2371{
2372 struct mem_dqinfo *mi;
2373
Ingo Molnard3be9152006-03-23 03:00:29 -08002374 mutex_lock(&sb_dqopt(sb)->dqonoff_mutex);
Jan Karaf55abc02008-08-20 17:50:32 +02002375 if (!sb_has_quota_active(sb, type)) {
Ingo Molnard3be9152006-03-23 03:00:29 -08002376 mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377 return -ESRCH;
2378 }
2379 mi = sb_dqopt(sb)->info + type;
2380 spin_lock(&dq_data_lock);
2381 ii->dqi_bgrace = mi->dqi_bgrace;
2382 ii->dqi_igrace = mi->dqi_igrace;
2383 ii->dqi_flags = mi->dqi_flags & DQF_MASK;
2384 ii->dqi_valid = IIF_ALL;
2385 spin_unlock(&dq_data_lock);
Ingo Molnard3be9152006-03-23 03:00:29 -08002386 mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002387 return 0;
2388}
Mingming Cao08d03502009-01-14 16:19:32 +01002389EXPORT_SYMBOL(vfs_get_dqinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002390
2391/* Generic routine for setting common part of quota file information */
2392int vfs_set_dqinfo(struct super_block *sb, int type, struct if_dqinfo *ii)
2393{
2394 struct mem_dqinfo *mi;
Jan Karaf55abc02008-08-20 17:50:32 +02002395 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002396
Ingo Molnard3be9152006-03-23 03:00:29 -08002397 mutex_lock(&sb_dqopt(sb)->dqonoff_mutex);
Jan Karaf55abc02008-08-20 17:50:32 +02002398 if (!sb_has_quota_active(sb, type)) {
2399 err = -ESRCH;
2400 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401 }
2402 mi = sb_dqopt(sb)->info + type;
2403 spin_lock(&dq_data_lock);
2404 if (ii->dqi_valid & IIF_BGRACE)
2405 mi->dqi_bgrace = ii->dqi_bgrace;
2406 if (ii->dqi_valid & IIF_IGRACE)
2407 mi->dqi_igrace = ii->dqi_igrace;
2408 if (ii->dqi_valid & IIF_FLAGS)
Jan Kara268157b2009-01-27 15:47:22 +01002409 mi->dqi_flags = (mi->dqi_flags & ~DQF_MASK) |
2410 (ii->dqi_flags & DQF_MASK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002411 spin_unlock(&dq_data_lock);
2412 mark_info_dirty(sb, type);
2413 /* Force write to disk */
2414 sb->dq_op->write_info(sb, type);
Jan Karaf55abc02008-08-20 17:50:32 +02002415out:
Ingo Molnard3be9152006-03-23 03:00:29 -08002416 mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
Jan Karaf55abc02008-08-20 17:50:32 +02002417 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002418}
Mingming Cao08d03502009-01-14 16:19:32 +01002419EXPORT_SYMBOL(vfs_set_dqinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002420
Alexey Dobriyan0d54b212009-09-21 17:01:09 -07002421const struct quotactl_ops vfs_quotactl_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002422 .quota_on = vfs_quota_on,
2423 .quota_off = vfs_quota_off,
2424 .quota_sync = vfs_quota_sync,
2425 .get_info = vfs_get_dqinfo,
2426 .set_info = vfs_set_dqinfo,
2427 .get_dqblk = vfs_get_dqblk,
2428 .set_dqblk = vfs_set_dqblk
2429};
2430
2431static ctl_table fs_dqstats_table[] = {
2432 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002433 .procname = "lookups",
2434 .data = &dqstats.lookups,
2435 .maxlen = sizeof(int),
2436 .mode = 0444,
Eric W. Biederman6d456112009-11-16 03:11:48 -08002437 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002438 },
2439 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002440 .procname = "drops",
2441 .data = &dqstats.drops,
2442 .maxlen = sizeof(int),
2443 .mode = 0444,
Eric W. Biederman6d456112009-11-16 03:11:48 -08002444 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002445 },
2446 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002447 .procname = "reads",
2448 .data = &dqstats.reads,
2449 .maxlen = sizeof(int),
2450 .mode = 0444,
Eric W. Biederman6d456112009-11-16 03:11:48 -08002451 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002452 },
2453 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454 .procname = "writes",
2455 .data = &dqstats.writes,
2456 .maxlen = sizeof(int),
2457 .mode = 0444,
Eric W. Biederman6d456112009-11-16 03:11:48 -08002458 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459 },
2460 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002461 .procname = "cache_hits",
2462 .data = &dqstats.cache_hits,
2463 .maxlen = sizeof(int),
2464 .mode = 0444,
Eric W. Biederman6d456112009-11-16 03:11:48 -08002465 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002466 },
2467 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468 .procname = "allocated_dquots",
2469 .data = &dqstats.allocated_dquots,
2470 .maxlen = sizeof(int),
2471 .mode = 0444,
Eric W. Biederman6d456112009-11-16 03:11:48 -08002472 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473 },
2474 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475 .procname = "free_dquots",
2476 .data = &dqstats.free_dquots,
2477 .maxlen = sizeof(int),
2478 .mode = 0444,
Eric W. Biederman6d456112009-11-16 03:11:48 -08002479 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002480 },
2481 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002482 .procname = "syncs",
2483 .data = &dqstats.syncs,
2484 .maxlen = sizeof(int),
2485 .mode = 0444,
Eric W. Biederman6d456112009-11-16 03:11:48 -08002486 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002487 },
Jan Kara8e893462007-10-16 23:29:31 -07002488#ifdef CONFIG_PRINT_QUOTA_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07002489 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002490 .procname = "warnings",
2491 .data = &flag_print_warnings,
2492 .maxlen = sizeof(int),
2493 .mode = 0644,
Eric W. Biederman6d456112009-11-16 03:11:48 -08002494 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002495 },
Jan Kara8e893462007-10-16 23:29:31 -07002496#endif
Eric W. Biedermanab092032009-11-05 14:25:10 -08002497 { },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002498};
2499
2500static ctl_table fs_table[] = {
2501 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002502 .procname = "quota",
2503 .mode = 0555,
2504 .child = fs_dqstats_table,
2505 },
Eric W. Biedermanab092032009-11-05 14:25:10 -08002506 { },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002507};
2508
2509static ctl_table sys_table[] = {
2510 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002511 .procname = "fs",
2512 .mode = 0555,
2513 .child = fs_table,
2514 },
Eric W. Biedermanab092032009-11-05 14:25:10 -08002515 { },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002516};
2517
2518static int __init dquot_init(void)
2519{
2520 int i;
2521 unsigned long nr_hash, order;
2522
2523 printk(KERN_NOTICE "VFS: Disk quotas %s\n", __DQUOT_VERSION__);
2524
Eric W. Biederman0b4d4142007-02-14 00:34:09 -08002525 register_sysctl_table(sys_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002526
Paul Mundt20c2df82007-07-20 10:11:58 +09002527 dquot_cachep = kmem_cache_create("dquot",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002528 sizeof(struct dquot), sizeof(unsigned long) * 4,
Paul Jacksonfffb60f2006-03-24 03:16:06 -08002529 (SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT|
2530 SLAB_MEM_SPREAD|SLAB_PANIC),
Paul Mundt20c2df82007-07-20 10:11:58 +09002531 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002532
2533 order = 0;
2534 dquot_hash = (struct hlist_head *)__get_free_pages(GFP_ATOMIC, order);
2535 if (!dquot_hash)
2536 panic("Cannot create dquot hash table");
2537
2538 /* Find power-of-two hlist_heads which can fit into allocation */
2539 nr_hash = (1UL << order) * PAGE_SIZE / sizeof(struct hlist_head);
2540 dq_hash_bits = 0;
2541 do {
2542 dq_hash_bits++;
2543 } while (nr_hash >> dq_hash_bits);
2544 dq_hash_bits--;
2545
2546 nr_hash = 1UL << dq_hash_bits;
2547 dq_hash_mask = nr_hash - 1;
2548 for (i = 0; i < nr_hash; i++)
2549 INIT_HLIST_HEAD(dquot_hash + i);
2550
2551 printk("Dquot-cache hash table entries: %ld (order %ld, %ld bytes)\n",
2552 nr_hash, order, (PAGE_SIZE << order));
2553
Rusty Russell8e1f9362007-07-17 04:03:17 -07002554 register_shrinker(&dqcache_shrinker);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002555
Linus Torvalds1da177e2005-04-16 15:20:36 -07002556 return 0;
2557}
2558module_init(dquot_init);