blob: eb5a755718f6a25542938048793d8884ad73d126 [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
326/* This function needs dq_list_lock */
327static inline int clear_dquot_dirty(struct dquot *dquot)
328{
329 if (!test_and_clear_bit(DQ_MOD_B, &dquot->dq_flags))
330 return 0;
331 list_del_init(&dquot->dq_dirty);
332 return 1;
333}
334
335void mark_info_dirty(struct super_block *sb, int type)
336{
337 set_bit(DQF_INFO_DIRTY_B, &sb_dqopt(sb)->info[type].dqi_flags);
338}
339EXPORT_SYMBOL(mark_info_dirty);
340
341/*
342 * Read dquot from disk and alloc space for it
343 */
344
345int dquot_acquire(struct dquot *dquot)
346{
347 int ret = 0, ret2 = 0;
348 struct quota_info *dqopt = sb_dqopt(dquot->dq_sb);
349
Ingo Molnard3be9152006-03-23 03:00:29 -0800350 mutex_lock(&dquot->dq_lock);
351 mutex_lock(&dqopt->dqio_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 if (!test_bit(DQ_READ_B, &dquot->dq_flags))
353 ret = dqopt->ops[dquot->dq_type]->read_dqblk(dquot);
354 if (ret < 0)
355 goto out_iolock;
356 set_bit(DQ_READ_B, &dquot->dq_flags);
357 /* Instantiate dquot if needed */
358 if (!test_bit(DQ_ACTIVE_B, &dquot->dq_flags) && !dquot->dq_off) {
359 ret = dqopt->ops[dquot->dq_type]->commit_dqblk(dquot);
360 /* Write the info if needed */
Jan Kara268157b2009-01-27 15:47:22 +0100361 if (info_dirty(&dqopt->info[dquot->dq_type])) {
362 ret2 = dqopt->ops[dquot->dq_type]->write_file_info(
363 dquot->dq_sb, dquot->dq_type);
364 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 if (ret < 0)
366 goto out_iolock;
367 if (ret2 < 0) {
368 ret = ret2;
369 goto out_iolock;
370 }
371 }
372 set_bit(DQ_ACTIVE_B, &dquot->dq_flags);
373out_iolock:
Ingo Molnard3be9152006-03-23 03:00:29 -0800374 mutex_unlock(&dqopt->dqio_mutex);
375 mutex_unlock(&dquot->dq_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 return ret;
377}
Mingming Cao08d03502009-01-14 16:19:32 +0100378EXPORT_SYMBOL(dquot_acquire);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379
380/*
381 * Write dquot to disk
382 */
383int dquot_commit(struct dquot *dquot)
384{
385 int ret = 0, ret2 = 0;
386 struct quota_info *dqopt = sb_dqopt(dquot->dq_sb);
387
Ingo Molnard3be9152006-03-23 03:00:29 -0800388 mutex_lock(&dqopt->dqio_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 spin_lock(&dq_list_lock);
390 if (!clear_dquot_dirty(dquot)) {
391 spin_unlock(&dq_list_lock);
392 goto out_sem;
393 }
394 spin_unlock(&dq_list_lock);
395 /* Inactive dquot can be only if there was error during read/init
396 * => we have better not writing it */
397 if (test_bit(DQ_ACTIVE_B, &dquot->dq_flags)) {
398 ret = dqopt->ops[dquot->dq_type]->commit_dqblk(dquot);
Jan Kara268157b2009-01-27 15:47:22 +0100399 if (info_dirty(&dqopt->info[dquot->dq_type])) {
400 ret2 = dqopt->ops[dquot->dq_type]->write_file_info(
401 dquot->dq_sb, dquot->dq_type);
402 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 if (ret >= 0)
404 ret = ret2;
405 }
406out_sem:
Ingo Molnard3be9152006-03-23 03:00:29 -0800407 mutex_unlock(&dqopt->dqio_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 return ret;
409}
Mingming Cao08d03502009-01-14 16:19:32 +0100410EXPORT_SYMBOL(dquot_commit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411
412/*
413 * Release dquot
414 */
415int dquot_release(struct dquot *dquot)
416{
417 int ret = 0, ret2 = 0;
418 struct quota_info *dqopt = sb_dqopt(dquot->dq_sb);
419
Ingo Molnard3be9152006-03-23 03:00:29 -0800420 mutex_lock(&dquot->dq_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 /* Check whether we are not racing with some other dqget() */
422 if (atomic_read(&dquot->dq_count) > 1)
423 goto out_dqlock;
Ingo Molnard3be9152006-03-23 03:00:29 -0800424 mutex_lock(&dqopt->dqio_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 if (dqopt->ops[dquot->dq_type]->release_dqblk) {
426 ret = dqopt->ops[dquot->dq_type]->release_dqblk(dquot);
427 /* Write the info */
Jan Kara268157b2009-01-27 15:47:22 +0100428 if (info_dirty(&dqopt->info[dquot->dq_type])) {
429 ret2 = dqopt->ops[dquot->dq_type]->write_file_info(
430 dquot->dq_sb, dquot->dq_type);
431 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 if (ret >= 0)
433 ret = ret2;
434 }
435 clear_bit(DQ_ACTIVE_B, &dquot->dq_flags);
Ingo Molnard3be9152006-03-23 03:00:29 -0800436 mutex_unlock(&dqopt->dqio_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437out_dqlock:
Ingo Molnard3be9152006-03-23 03:00:29 -0800438 mutex_unlock(&dquot->dq_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 return ret;
440}
Mingming Cao08d03502009-01-14 16:19:32 +0100441EXPORT_SYMBOL(dquot_release);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442
Jan Kara7d9056b2008-11-25 15:31:32 +0100443void dquot_destroy(struct dquot *dquot)
Jan Kara74f783a2008-08-19 14:51:22 +0200444{
445 kmem_cache_free(dquot_cachep, dquot);
446}
Jan Kara7d9056b2008-11-25 15:31:32 +0100447EXPORT_SYMBOL(dquot_destroy);
Jan Kara74f783a2008-08-19 14:51:22 +0200448
449static inline void do_destroy_dquot(struct dquot *dquot)
450{
451 dquot->dq_sb->dq_op->destroy_dquot(dquot);
452}
453
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454/* Invalidate all dquots on the list. Note that this function is called after
455 * quota is disabled and pointers from inodes removed so there cannot be new
Jan Kara6362e4d2006-03-23 03:00:17 -0800456 * quota users. There can still be some users of quotas due to inodes being
457 * just deleted or pruned by prune_icache() (those are not attached to any
Jan Karacc334122009-01-12 17:23:05 +0100458 * list) or parallel quotactl call. We have to wait for such users.
Jan Kara6362e4d2006-03-23 03:00:17 -0800459 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460static void invalidate_dquots(struct super_block *sb, int type)
461{
Domen Puncerc33ed272005-06-25 14:59:36 -0700462 struct dquot *dquot, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463
Jan Kara6362e4d2006-03-23 03:00:17 -0800464restart:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 spin_lock(&dq_list_lock);
Domen Puncerc33ed272005-06-25 14:59:36 -0700466 list_for_each_entry_safe(dquot, tmp, &inuse_list, dq_inuse) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 if (dquot->dq_sb != sb)
468 continue;
469 if (dquot->dq_type != type)
470 continue;
Jan Kara6362e4d2006-03-23 03:00:17 -0800471 /* Wait for dquot users */
472 if (atomic_read(&dquot->dq_count)) {
473 DEFINE_WAIT(wait);
474
475 atomic_inc(&dquot->dq_count);
476 prepare_to_wait(&dquot->dq_wait_unused, &wait,
477 TASK_UNINTERRUPTIBLE);
478 spin_unlock(&dq_list_lock);
479 /* Once dqput() wakes us up, we know it's time to free
480 * the dquot.
481 * IMPORTANT: we rely on the fact that there is always
482 * at most one process waiting for dquot to free.
483 * Otherwise dq_count would be > 1 and we would never
484 * wake up.
485 */
486 if (atomic_read(&dquot->dq_count) > 1)
487 schedule();
488 finish_wait(&dquot->dq_wait_unused, &wait);
489 dqput(dquot);
490 /* At this moment dquot() need not exist (it could be
491 * reclaimed by prune_dqcache(). Hence we must
492 * restart. */
493 goto restart;
494 }
495 /*
496 * Quota now has no users and it has been written on last
497 * dqput()
498 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 remove_dquot_hash(dquot);
500 remove_free_dquot(dquot);
501 remove_inuse(dquot);
Jan Kara74f783a2008-08-19 14:51:22 +0200502 do_destroy_dquot(dquot);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 }
504 spin_unlock(&dq_list_lock);
505}
506
Jan Kara12c77522008-10-20 17:05:00 +0200507/* Call callback for every active dquot on given filesystem */
508int dquot_scan_active(struct super_block *sb,
509 int (*fn)(struct dquot *dquot, unsigned long priv),
510 unsigned long priv)
511{
512 struct dquot *dquot, *old_dquot = NULL;
513 int ret = 0;
514
515 mutex_lock(&sb_dqopt(sb)->dqonoff_mutex);
516 spin_lock(&dq_list_lock);
517 list_for_each_entry(dquot, &inuse_list, dq_inuse) {
518 if (!test_bit(DQ_ACTIVE_B, &dquot->dq_flags))
519 continue;
520 if (dquot->dq_sb != sb)
521 continue;
522 /* Now we have active dquot so we can just increase use count */
523 atomic_inc(&dquot->dq_count);
524 dqstats.lookups++;
525 spin_unlock(&dq_list_lock);
526 dqput(old_dquot);
527 old_dquot = dquot;
528 ret = fn(dquot, priv);
529 if (ret < 0)
530 goto out;
531 spin_lock(&dq_list_lock);
532 /* We are safe to continue now because our dquot could not
533 * be moved out of the inuse list while we hold the reference */
534 }
535 spin_unlock(&dq_list_lock);
536out:
537 dqput(old_dquot);
538 mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
539 return ret;
540}
Mingming Cao08d03502009-01-14 16:19:32 +0100541EXPORT_SYMBOL(dquot_scan_active);
Jan Kara12c77522008-10-20 17:05:00 +0200542
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543int vfs_quota_sync(struct super_block *sb, int type)
544{
545 struct list_head *dirty;
546 struct dquot *dquot;
547 struct quota_info *dqopt = sb_dqopt(sb);
548 int cnt;
549
Ingo Molnard3be9152006-03-23 03:00:29 -0800550 mutex_lock(&dqopt->dqonoff_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
552 if (type != -1 && cnt != type)
553 continue;
Jan Karaf55abc02008-08-20 17:50:32 +0200554 if (!sb_has_quota_active(sb, cnt))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 continue;
556 spin_lock(&dq_list_lock);
557 dirty = &dqopt->info[cnt].dqi_dirty_list;
558 while (!list_empty(dirty)) {
Jan Kara268157b2009-01-27 15:47:22 +0100559 dquot = list_first_entry(dirty, struct dquot,
560 dq_dirty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 /* Dirty and inactive can be only bad dquot... */
562 if (!test_bit(DQ_ACTIVE_B, &dquot->dq_flags)) {
563 clear_dquot_dirty(dquot);
564 continue;
565 }
566 /* Now we have active dquot from which someone is
567 * holding reference so we can safely just increase
568 * use count */
569 atomic_inc(&dquot->dq_count);
570 dqstats.lookups++;
571 spin_unlock(&dq_list_lock);
572 sb->dq_op->write_dquot(dquot);
573 dqput(dquot);
574 spin_lock(&dq_list_lock);
575 }
576 spin_unlock(&dq_list_lock);
577 }
578
579 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
Jan Karaf55abc02008-08-20 17:50:32 +0200580 if ((cnt == type || type == -1) && sb_has_quota_active(sb, cnt)
581 && info_dirty(&dqopt->info[cnt]))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 sb->dq_op->write_info(sb, cnt);
583 spin_lock(&dq_list_lock);
584 dqstats.syncs++;
585 spin_unlock(&dq_list_lock);
Ingo Molnard3be9152006-03-23 03:00:29 -0800586 mutex_unlock(&dqopt->dqonoff_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587
588 return 0;
589}
Mingming Cao08d03502009-01-14 16:19:32 +0100590EXPORT_SYMBOL(vfs_quota_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591
592/* Free unused dquots from cache */
593static void prune_dqcache(int count)
594{
595 struct list_head *head;
596 struct dquot *dquot;
597
598 head = free_dquots.prev;
599 while (head != &free_dquots && count) {
600 dquot = list_entry(head, struct dquot, dq_free);
601 remove_dquot_hash(dquot);
602 remove_free_dquot(dquot);
603 remove_inuse(dquot);
Jan Kara74f783a2008-08-19 14:51:22 +0200604 do_destroy_dquot(dquot);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 count--;
606 head = free_dquots.prev;
607 }
608}
609
610/*
611 * This is called from kswapd when we think we need some
612 * more memory
613 */
614
Al Viro27496a82005-10-21 03:20:48 -0400615static int shrink_dqcache_memory(int nr, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616{
617 if (nr) {
618 spin_lock(&dq_list_lock);
619 prune_dqcache(nr);
620 spin_unlock(&dq_list_lock);
621 }
622 return (dqstats.free_dquots / 100) * sysctl_vfs_cache_pressure;
623}
624
Rusty Russell8e1f9362007-07-17 04:03:17 -0700625static struct shrinker dqcache_shrinker = {
626 .shrink = shrink_dqcache_memory,
627 .seeks = DEFAULT_SEEKS,
628};
629
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630/*
631 * Put reference to dquot
632 * NOTE: If you change this function please check whether dqput_blocks() works right...
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 */
Jan Kara3d9ea252008-10-10 16:12:23 +0200634void dqput(struct dquot *dquot)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635{
Jan Karab48d3802008-07-25 01:46:49 -0700636 int ret;
637
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 if (!dquot)
639 return;
640#ifdef __DQUOT_PARANOIA
641 if (!atomic_read(&dquot->dq_count)) {
642 printk("VFS: dqput: trying to free free dquot\n");
643 printk("VFS: device %s, dquot of %s %d\n",
644 dquot->dq_sb->s_id,
645 quotatypes[dquot->dq_type],
646 dquot->dq_id);
647 BUG();
648 }
649#endif
650
651 spin_lock(&dq_list_lock);
652 dqstats.drops++;
653 spin_unlock(&dq_list_lock);
654we_slept:
655 spin_lock(&dq_list_lock);
656 if (atomic_read(&dquot->dq_count) > 1) {
657 /* We have more than one user... nothing to do */
658 atomic_dec(&dquot->dq_count);
Jan Kara6362e4d2006-03-23 03:00:17 -0800659 /* Releasing dquot during quotaoff phase? */
Jan Karaf55abc02008-08-20 17:50:32 +0200660 if (!sb_has_quota_active(dquot->dq_sb, dquot->dq_type) &&
Jan Kara6362e4d2006-03-23 03:00:17 -0800661 atomic_read(&dquot->dq_count) == 1)
662 wake_up(&dquot->dq_wait_unused);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 spin_unlock(&dq_list_lock);
664 return;
665 }
666 /* Need to release dquot? */
667 if (test_bit(DQ_ACTIVE_B, &dquot->dq_flags) && dquot_dirty(dquot)) {
668 spin_unlock(&dq_list_lock);
669 /* Commit dquot before releasing */
Jan Karab48d3802008-07-25 01:46:49 -0700670 ret = dquot->dq_sb->dq_op->write_dquot(dquot);
671 if (ret < 0) {
672 printk(KERN_ERR "VFS: cannot write quota structure on "
673 "device %s (error %d). Quota may get out of "
674 "sync!\n", dquot->dq_sb->s_id, ret);
675 /*
676 * We clear dirty bit anyway, so that we avoid
677 * infinite loop here
678 */
679 spin_lock(&dq_list_lock);
680 clear_dquot_dirty(dquot);
681 spin_unlock(&dq_list_lock);
682 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 goto we_slept;
684 }
685 /* Clear flag in case dquot was inactive (something bad happened) */
686 clear_dquot_dirty(dquot);
687 if (test_bit(DQ_ACTIVE_B, &dquot->dq_flags)) {
688 spin_unlock(&dq_list_lock);
689 dquot->dq_sb->dq_op->release_dquot(dquot);
690 goto we_slept;
691 }
692 atomic_dec(&dquot->dq_count);
693#ifdef __DQUOT_PARANOIA
694 /* sanity check */
Eric Sesterhenn8abf6a42006-04-02 13:36:13 +0200695 BUG_ON(!list_empty(&dquot->dq_free));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696#endif
697 put_dquot_last(dquot);
698 spin_unlock(&dq_list_lock);
699}
Mingming Cao08d03502009-01-14 16:19:32 +0100700EXPORT_SYMBOL(dqput);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701
Jan Kara7d9056b2008-11-25 15:31:32 +0100702struct dquot *dquot_alloc(struct super_block *sb, int type)
Jan Kara74f783a2008-08-19 14:51:22 +0200703{
704 return kmem_cache_zalloc(dquot_cachep, GFP_NOFS);
705}
Jan Kara7d9056b2008-11-25 15:31:32 +0100706EXPORT_SYMBOL(dquot_alloc);
Jan Kara74f783a2008-08-19 14:51:22 +0200707
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708static struct dquot *get_empty_dquot(struct super_block *sb, int type)
709{
710 struct dquot *dquot;
711
Jan Kara74f783a2008-08-19 14:51:22 +0200712 dquot = sb->dq_op->alloc_dquot(sb, type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 if(!dquot)
Jan Karadd6f3c62009-01-26 16:01:43 +0100714 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715
Ingo Molnard3be9152006-03-23 03:00:29 -0800716 mutex_init(&dquot->dq_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 INIT_LIST_HEAD(&dquot->dq_free);
718 INIT_LIST_HEAD(&dquot->dq_inuse);
719 INIT_HLIST_NODE(&dquot->dq_hash);
720 INIT_LIST_HEAD(&dquot->dq_dirty);
Jan Kara6362e4d2006-03-23 03:00:17 -0800721 init_waitqueue_head(&dquot->dq_wait_unused);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 dquot->dq_sb = sb;
723 dquot->dq_type = type;
724 atomic_set(&dquot->dq_count, 1);
725
726 return dquot;
727}
728
729/*
730 * Get reference to dquot
Jan Karacc334122009-01-12 17:23:05 +0100731 *
732 * Locking is slightly tricky here. We are guarded from parallel quotaoff()
733 * destroying our dquot by:
734 * a) checking for quota flags under dq_list_lock and
735 * b) getting a reference to dquot before we release dq_list_lock
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 */
Jan Kara3d9ea252008-10-10 16:12:23 +0200737struct dquot *dqget(struct super_block *sb, unsigned int id, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738{
739 unsigned int hashent = hashfn(sb, id, type);
Jan Karadd6f3c62009-01-26 16:01:43 +0100740 struct dquot *dquot = NULL, *empty = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741
Jan Karaf55abc02008-08-20 17:50:32 +0200742 if (!sb_has_quota_active(sb, type))
Jan Karadd6f3c62009-01-26 16:01:43 +0100743 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744we_slept:
745 spin_lock(&dq_list_lock);
Jan Karacc334122009-01-12 17:23:05 +0100746 spin_lock(&dq_state_lock);
747 if (!sb_has_quota_active(sb, type)) {
748 spin_unlock(&dq_state_lock);
749 spin_unlock(&dq_list_lock);
750 goto out;
751 }
752 spin_unlock(&dq_state_lock);
753
Jan Karadd6f3c62009-01-26 16:01:43 +0100754 dquot = find_dquot(hashent, sb, id, type);
755 if (!dquot) {
756 if (!empty) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 spin_unlock(&dq_list_lock);
Jan Karadd6f3c62009-01-26 16:01:43 +0100758 empty = get_empty_dquot(sb, type);
759 if (!empty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 schedule(); /* Try to wait for a moment... */
761 goto we_slept;
762 }
763 dquot = empty;
Jan Karadd6f3c62009-01-26 16:01:43 +0100764 empty = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 dquot->dq_id = id;
766 /* all dquots go on the inuse_list */
767 put_inuse(dquot);
768 /* hash it first so it can be found */
769 insert_dquot_hash(dquot);
770 dqstats.lookups++;
771 spin_unlock(&dq_list_lock);
772 } else {
773 if (!atomic_read(&dquot->dq_count))
774 remove_free_dquot(dquot);
775 atomic_inc(&dquot->dq_count);
776 dqstats.cache_hits++;
777 dqstats.lookups++;
778 spin_unlock(&dq_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 }
Jan Kara268157b2009-01-27 15:47:22 +0100780 /* Wait for dq_lock - after this we know that either dquot_release() is
781 * already finished or it will be canceled due to dq_count > 1 test */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 wait_on_dquot(dquot);
Jan Kara268157b2009-01-27 15:47:22 +0100783 /* Read the dquot / allocate space in quota file */
784 if (!test_bit(DQ_ACTIVE_B, &dquot->dq_flags) &&
785 sb->dq_op->acquire_dquot(dquot) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 dqput(dquot);
Jan Karadd6f3c62009-01-26 16:01:43 +0100787 dquot = NULL;
Jan Karacc334122009-01-12 17:23:05 +0100788 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 }
790#ifdef __DQUOT_PARANOIA
Eric Sesterhenn8abf6a42006-04-02 13:36:13 +0200791 BUG_ON(!dquot->dq_sb); /* Has somebody invalidated entry under us? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792#endif
Jan Karacc334122009-01-12 17:23:05 +0100793out:
794 if (empty)
795 do_destroy_dquot(empty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796
797 return dquot;
798}
Mingming Cao08d03502009-01-14 16:19:32 +0100799EXPORT_SYMBOL(dqget);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800
801static int dqinit_needed(struct inode *inode, int type)
802{
803 int cnt;
804
805 if (IS_NOQUOTA(inode))
806 return 0;
807 if (type != -1)
Jan Karadd6f3c62009-01-26 16:01:43 +0100808 return !inode->i_dquot[type];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
Jan Karadd6f3c62009-01-26 16:01:43 +0100810 if (!inode->i_dquot[cnt])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 return 1;
812 return 0;
813}
814
Ingo Molnard3be9152006-03-23 03:00:29 -0800815/* This routine is guarded by dqonoff_mutex mutex */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816static void add_dquot_ref(struct super_block *sb, int type)
817{
Jan Kara941d2382008-02-06 01:37:36 -0800818 struct inode *inode, *old_inode = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819
Christoph Hellwigd003fb72007-02-12 00:51:58 -0800820 spin_lock(&inode_lock);
821 list_for_each_entry(inode, &sb->s_inodes, i_sb_list) {
Wu Fengguangb6fac632009-04-02 16:56:34 -0700822 if (inode->i_state & (I_FREEING|I_CLEAR|I_WILL_FREE|I_NEW))
Nick Pigginaabb8fd2009-03-11 13:17:36 -0700823 continue;
Christoph Hellwigd003fb72007-02-12 00:51:58 -0800824 if (!atomic_read(&inode->i_writecount))
825 continue;
826 if (!dqinit_needed(inode, type))
827 continue;
Christoph Hellwigd003fb72007-02-12 00:51:58 -0800828
829 __iget(inode);
830 spin_unlock(&inode_lock);
831
Jan Kara941d2382008-02-06 01:37:36 -0800832 iput(old_inode);
Christoph Hellwigd003fb72007-02-12 00:51:58 -0800833 sb->dq_op->initialize(inode, type);
Jan Kara941d2382008-02-06 01:37:36 -0800834 /* We hold a reference to 'inode' so it couldn't have been
835 * removed from s_inodes list while we dropped the inode_lock.
836 * We cannot iput the inode now as we can be holding the last
837 * reference and we cannot iput it under inode_lock. So we
838 * keep the reference and iput it later. */
839 old_inode = inode;
840 spin_lock(&inode_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 }
Christoph Hellwigd003fb72007-02-12 00:51:58 -0800842 spin_unlock(&inode_lock);
Jan Kara941d2382008-02-06 01:37:36 -0800843 iput(old_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844}
845
Jan Kara268157b2009-01-27 15:47:22 +0100846/*
847 * Return 0 if dqput() won't block.
848 * (note that 1 doesn't necessarily mean blocking)
849 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850static inline int dqput_blocks(struct dquot *dquot)
851{
852 if (atomic_read(&dquot->dq_count) <= 1)
853 return 1;
854 return 0;
855}
856
Jan Kara268157b2009-01-27 15:47:22 +0100857/*
858 * Remove references to dquots from inode and add dquot to list for freeing
859 * if we have the last referece to dquot
860 * We can't race with anybody because we hold dqptr_sem for writing...
861 */
Adrian Bunke5f00f42007-05-08 00:27:22 -0700862static int remove_inode_dquot_ref(struct inode *inode, int type,
863 struct list_head *tofree_head)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864{
865 struct dquot *dquot = inode->i_dquot[type];
866
Jan Karadd6f3c62009-01-26 16:01:43 +0100867 inode->i_dquot[type] = NULL;
868 if (dquot) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 if (dqput_blocks(dquot)) {
870#ifdef __DQUOT_PARANOIA
871 if (atomic_read(&dquot->dq_count) != 1)
872 printk(KERN_WARNING "VFS: Adding dquot with dq_count %d to dispose list.\n", atomic_read(&dquot->dq_count));
873#endif
874 spin_lock(&dq_list_lock);
Jan Kara268157b2009-01-27 15:47:22 +0100875 /* As dquot must have currently users it can't be on
876 * the free list... */
877 list_add(&dquot->dq_free, tofree_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 spin_unlock(&dq_list_lock);
879 return 1;
880 }
881 else
882 dqput(dquot); /* We have guaranteed we won't block */
883 }
884 return 0;
885}
886
Jan Kara268157b2009-01-27 15:47:22 +0100887/*
888 * Free list of dquots
889 * Dquots are removed from inodes and no new references can be got so we are
890 * the only ones holding reference
891 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892static void put_dquot_list(struct list_head *tofree_head)
893{
894 struct list_head *act_head;
895 struct dquot *dquot;
896
897 act_head = tofree_head->next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 while (act_head != tofree_head) {
899 dquot = list_entry(act_head, struct dquot, dq_free);
900 act_head = act_head->next;
Jan Kara268157b2009-01-27 15:47:22 +0100901 /* Remove dquot from the list so we won't have problems... */
902 list_del_init(&dquot->dq_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 dqput(dquot);
904 }
905}
906
Christoph Hellwigfb58b732007-02-12 00:51:57 -0800907static void remove_dquot_ref(struct super_block *sb, int type,
908 struct list_head *tofree_head)
909{
910 struct inode *inode;
911
912 spin_lock(&inode_lock);
913 list_for_each_entry(inode, &sb->s_inodes, i_sb_list) {
Nick Pigginaabb8fd2009-03-11 13:17:36 -0700914 /*
915 * We have to scan also I_NEW inodes because they can already
916 * have quota pointer initialized. Luckily, we need to touch
917 * only quota pointers and these have separate locking
918 * (dqptr_sem).
919 */
Christoph Hellwigfb58b732007-02-12 00:51:57 -0800920 if (!IS_NOQUOTA(inode))
921 remove_inode_dquot_ref(inode, type, tofree_head);
922 }
923 spin_unlock(&inode_lock);
924}
925
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926/* Gather all references from inodes and drop them */
927static void drop_dquot_ref(struct super_block *sb, int type)
928{
929 LIST_HEAD(tofree_head);
930
Christoph Hellwigfb58b732007-02-12 00:51:57 -0800931 if (sb->dq_op) {
932 down_write(&sb_dqopt(sb)->dqptr_sem);
933 remove_dquot_ref(sb, type, &tofree_head);
934 up_write(&sb_dqopt(sb)->dqptr_sem);
935 put_dquot_list(&tofree_head);
936 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937}
938
Jan Kara12095462008-08-20 14:45:12 +0200939static inline void dquot_incr_inodes(struct dquot *dquot, qsize_t number)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940{
941 dquot->dq_dqb.dqb_curinodes += number;
942}
943
944static inline void dquot_incr_space(struct dquot *dquot, qsize_t number)
945{
946 dquot->dq_dqb.dqb_curspace += number;
947}
948
Mingming Caof18df222009-01-13 16:43:09 +0100949static inline void dquot_resv_space(struct dquot *dquot, qsize_t number)
950{
951 dquot->dq_dqb.dqb_rsvspace += number;
952}
953
Mingming Cao740d9dc2009-01-13 16:43:14 +0100954/*
955 * Claim reserved quota space
956 */
957static void dquot_claim_reserved_space(struct dquot *dquot,
958 qsize_t number)
959{
960 WARN_ON(dquot->dq_dqb.dqb_rsvspace < number);
961 dquot->dq_dqb.dqb_curspace += number;
962 dquot->dq_dqb.dqb_rsvspace -= number;
963}
964
965static inline
966void dquot_free_reserved_space(struct dquot *dquot, qsize_t number)
967{
968 dquot->dq_dqb.dqb_rsvspace -= number;
969}
970
Jan Kara7a2435d2009-01-27 01:47:11 +0100971static void dquot_decr_inodes(struct dquot *dquot, qsize_t number)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972{
Jan Karadb49d2d2008-10-01 18:21:39 +0200973 if (sb_dqopt(dquot->dq_sb)->flags & DQUOT_NEGATIVE_USAGE ||
974 dquot->dq_dqb.dqb_curinodes >= number)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 dquot->dq_dqb.dqb_curinodes -= number;
976 else
977 dquot->dq_dqb.dqb_curinodes = 0;
978 if (dquot->dq_dqb.dqb_curinodes <= dquot->dq_dqb.dqb_isoftlimit)
979 dquot->dq_dqb.dqb_itime = (time_t) 0;
980 clear_bit(DQ_INODES_B, &dquot->dq_flags);
981}
982
Jan Kara7a2435d2009-01-27 01:47:11 +0100983static void dquot_decr_space(struct dquot *dquot, qsize_t number)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984{
Jan Karadb49d2d2008-10-01 18:21:39 +0200985 if (sb_dqopt(dquot->dq_sb)->flags & DQUOT_NEGATIVE_USAGE ||
986 dquot->dq_dqb.dqb_curspace >= number)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 dquot->dq_dqb.dqb_curspace -= number;
988 else
989 dquot->dq_dqb.dqb_curspace = 0;
Jan Kara12095462008-08-20 14:45:12 +0200990 if (dquot->dq_dqb.dqb_curspace <= dquot->dq_dqb.dqb_bsoftlimit)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 dquot->dq_dqb.dqb_btime = (time_t) 0;
992 clear_bit(DQ_BLKS_B, &dquot->dq_flags);
993}
994
Jan Karac5254602007-12-22 14:03:25 -0800995static int warning_issued(struct dquot *dquot, const int warntype)
996{
997 int flag = (warntype == QUOTA_NL_BHARDWARN ||
998 warntype == QUOTA_NL_BSOFTLONGWARN) ? DQ_BLKS_B :
999 ((warntype == QUOTA_NL_IHARDWARN ||
1000 warntype == QUOTA_NL_ISOFTLONGWARN) ? DQ_INODES_B : 0);
1001
1002 if (!flag)
1003 return 0;
1004 return test_and_set_bit(flag, &dquot->dq_flags);
1005}
1006
Jan Kara8e893462007-10-16 23:29:31 -07001007#ifdef CONFIG_PRINT_QUOTA_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008static int flag_print_warnings = 1;
1009
Jan Kara7a2435d2009-01-27 01:47:11 +01001010static int need_print_warning(struct dquot *dquot)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011{
1012 if (!flag_print_warnings)
1013 return 0;
1014
1015 switch (dquot->dq_type) {
1016 case USRQUOTA:
David Howellsda9592e2008-11-14 10:39:05 +11001017 return current_fsuid() == dquot->dq_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 case GRPQUOTA:
1019 return in_group_p(dquot->dq_id);
1020 }
1021 return 0;
1022}
1023
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024/* Print warning to user which exceeded quota */
Jan Karac5254602007-12-22 14:03:25 -08001025static void print_warning(struct dquot *dquot, const int warntype)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026{
1027 char *msg = NULL;
Peter Zijlstra24ec8392006-12-08 02:36:04 -08001028 struct tty_struct *tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029
Jan Kara657d3bf2008-07-25 01:46:52 -07001030 if (warntype == QUOTA_NL_IHARDBELOW ||
1031 warntype == QUOTA_NL_ISOFTBELOW ||
1032 warntype == QUOTA_NL_BHARDBELOW ||
1033 warntype == QUOTA_NL_BSOFTBELOW || !need_print_warning(dquot))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 return;
1035
Peter Zijlstra24ec8392006-12-08 02:36:04 -08001036 tty = get_current_tty();
1037 if (!tty)
Alan Cox452a00d2008-10-13 10:39:13 +01001038 return;
Peter Zijlstra24ec8392006-12-08 02:36:04 -08001039 tty_write_message(tty, dquot->dq_sb->s_id);
Jan Kara8e893462007-10-16 23:29:31 -07001040 if (warntype == QUOTA_NL_ISOFTWARN || warntype == QUOTA_NL_BSOFTWARN)
Peter Zijlstra24ec8392006-12-08 02:36:04 -08001041 tty_write_message(tty, ": warning, ");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 else
Peter Zijlstra24ec8392006-12-08 02:36:04 -08001043 tty_write_message(tty, ": write failed, ");
1044 tty_write_message(tty, quotatypes[dquot->dq_type]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 switch (warntype) {
Jan Kara8e893462007-10-16 23:29:31 -07001046 case QUOTA_NL_IHARDWARN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 msg = " file limit reached.\r\n";
1048 break;
Jan Kara8e893462007-10-16 23:29:31 -07001049 case QUOTA_NL_ISOFTLONGWARN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 msg = " file quota exceeded too long.\r\n";
1051 break;
Jan Kara8e893462007-10-16 23:29:31 -07001052 case QUOTA_NL_ISOFTWARN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 msg = " file quota exceeded.\r\n";
1054 break;
Jan Kara8e893462007-10-16 23:29:31 -07001055 case QUOTA_NL_BHARDWARN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 msg = " block limit reached.\r\n";
1057 break;
Jan Kara8e893462007-10-16 23:29:31 -07001058 case QUOTA_NL_BSOFTLONGWARN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 msg = " block quota exceeded too long.\r\n";
1060 break;
Jan Kara8e893462007-10-16 23:29:31 -07001061 case QUOTA_NL_BSOFTWARN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 msg = " block quota exceeded.\r\n";
1063 break;
1064 }
Peter Zijlstra24ec8392006-12-08 02:36:04 -08001065 tty_write_message(tty, msg);
Alan Cox452a00d2008-10-13 10:39:13 +01001066 tty_kref_put(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067}
Jan Kara8e893462007-10-16 23:29:31 -07001068#endif
1069
Mingming Caof18df222009-01-13 16:43:09 +01001070/*
1071 * Write warnings to the console and send warning messages over netlink.
1072 *
1073 * Note that this function can sleep.
1074 */
Jan Kara7a2435d2009-01-27 01:47:11 +01001075static void flush_warnings(struct dquot *const *dquots, char *warntype)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076{
Steven Whitehouse86e931a2009-09-28 12:35:17 +01001077 struct dquot *dq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 int i;
1079
Steven Whitehouse86e931a2009-09-28 12:35:17 +01001080 for (i = 0; i < MAXQUOTAS; i++) {
1081 dq = dquots[i];
1082 if (dq && warntype[i] != QUOTA_NL_NOWARN &&
1083 !warning_issued(dq, warntype[i])) {
Jan Kara8e893462007-10-16 23:29:31 -07001084#ifdef CONFIG_PRINT_QUOTA_WARNING
Steven Whitehouse86e931a2009-09-28 12:35:17 +01001085 print_warning(dq, warntype[i]);
Jan Kara8e893462007-10-16 23:29:31 -07001086#endif
Steven Whitehouse86e931a2009-09-28 12:35:17 +01001087 quota_send_warning(dq->dq_type, dq->dq_id,
1088 dq->dq_sb->s_dev, warntype[i]);
Jan Kara8e893462007-10-16 23:29:31 -07001089 }
Steven Whitehouse86e931a2009-09-28 12:35:17 +01001090 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091}
1092
Jan Kara7a2435d2009-01-27 01:47:11 +01001093static int ignore_hardlimit(struct dquot *dquot)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094{
1095 struct mem_dqinfo *info = &sb_dqopt(dquot->dq_sb)->info[dquot->dq_type];
1096
1097 return capable(CAP_SYS_RESOURCE) &&
Jan Kara268157b2009-01-27 15:47:22 +01001098 (info->dqi_format->qf_fmt_id != QFMT_VFS_OLD ||
1099 !(info->dqi_flags & V1_DQF_RSQUASH));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100}
1101
1102/* needs dq_data_lock */
Jan Kara12095462008-08-20 14:45:12 +02001103static int check_idq(struct dquot *dquot, qsize_t inodes, char *warntype)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104{
Jan Kara268157b2009-01-27 15:47:22 +01001105 qsize_t newinodes = dquot->dq_dqb.dqb_curinodes + inodes;
1106
Jan Kara8e893462007-10-16 23:29:31 -07001107 *warntype = QUOTA_NL_NOWARN;
Jan Karaf55abc02008-08-20 17:50:32 +02001108 if (!sb_has_quota_limits_enabled(dquot->dq_sb, dquot->dq_type) ||
1109 test_bit(DQ_FAKE_B, &dquot->dq_flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 return QUOTA_OK;
1111
1112 if (dquot->dq_dqb.dqb_ihardlimit &&
Jan Kara268157b2009-01-27 15:47:22 +01001113 newinodes > dquot->dq_dqb.dqb_ihardlimit &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 !ignore_hardlimit(dquot)) {
Jan Kara8e893462007-10-16 23:29:31 -07001115 *warntype = QUOTA_NL_IHARDWARN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 return NO_QUOTA;
1117 }
1118
1119 if (dquot->dq_dqb.dqb_isoftlimit &&
Jan Kara268157b2009-01-27 15:47:22 +01001120 newinodes > dquot->dq_dqb.dqb_isoftlimit &&
1121 dquot->dq_dqb.dqb_itime &&
1122 get_seconds() >= dquot->dq_dqb.dqb_itime &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 !ignore_hardlimit(dquot)) {
Jan Kara8e893462007-10-16 23:29:31 -07001124 *warntype = QUOTA_NL_ISOFTLONGWARN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 return NO_QUOTA;
1126 }
1127
1128 if (dquot->dq_dqb.dqb_isoftlimit &&
Jan Kara268157b2009-01-27 15:47:22 +01001129 newinodes > dquot->dq_dqb.dqb_isoftlimit &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 dquot->dq_dqb.dqb_itime == 0) {
Jan Kara8e893462007-10-16 23:29:31 -07001131 *warntype = QUOTA_NL_ISOFTWARN;
Jan Kara268157b2009-01-27 15:47:22 +01001132 dquot->dq_dqb.dqb_itime = get_seconds() +
1133 sb_dqopt(dquot->dq_sb)->info[dquot->dq_type].dqi_igrace;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 }
1135
1136 return QUOTA_OK;
1137}
1138
1139/* needs dq_data_lock */
1140static int check_bdq(struct dquot *dquot, qsize_t space, int prealloc, char *warntype)
1141{
Mingming Caof18df222009-01-13 16:43:09 +01001142 qsize_t tspace;
Jan Kara268157b2009-01-27 15:47:22 +01001143 struct super_block *sb = dquot->dq_sb;
Mingming Caof18df222009-01-13 16:43:09 +01001144
Jan Kara8e893462007-10-16 23:29:31 -07001145 *warntype = QUOTA_NL_NOWARN;
Jan Kara268157b2009-01-27 15:47:22 +01001146 if (!sb_has_quota_limits_enabled(sb, dquot->dq_type) ||
Jan Karaf55abc02008-08-20 17:50:32 +02001147 test_bit(DQ_FAKE_B, &dquot->dq_flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 return QUOTA_OK;
1149
Mingming Caof18df222009-01-13 16:43:09 +01001150 tspace = dquot->dq_dqb.dqb_curspace + dquot->dq_dqb.dqb_rsvspace
1151 + space;
1152
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 if (dquot->dq_dqb.dqb_bhardlimit &&
Mingming Caof18df222009-01-13 16:43:09 +01001154 tspace > dquot->dq_dqb.dqb_bhardlimit &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 !ignore_hardlimit(dquot)) {
1156 if (!prealloc)
Jan Kara8e893462007-10-16 23:29:31 -07001157 *warntype = QUOTA_NL_BHARDWARN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 return NO_QUOTA;
1159 }
1160
1161 if (dquot->dq_dqb.dqb_bsoftlimit &&
Mingming Caof18df222009-01-13 16:43:09 +01001162 tspace > dquot->dq_dqb.dqb_bsoftlimit &&
Jan Kara268157b2009-01-27 15:47:22 +01001163 dquot->dq_dqb.dqb_btime &&
1164 get_seconds() >= dquot->dq_dqb.dqb_btime &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 !ignore_hardlimit(dquot)) {
1166 if (!prealloc)
Jan Kara8e893462007-10-16 23:29:31 -07001167 *warntype = QUOTA_NL_BSOFTLONGWARN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 return NO_QUOTA;
1169 }
1170
1171 if (dquot->dq_dqb.dqb_bsoftlimit &&
Mingming Caof18df222009-01-13 16:43:09 +01001172 tspace > dquot->dq_dqb.dqb_bsoftlimit &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 dquot->dq_dqb.dqb_btime == 0) {
1174 if (!prealloc) {
Jan Kara8e893462007-10-16 23:29:31 -07001175 *warntype = QUOTA_NL_BSOFTWARN;
Jan Kara268157b2009-01-27 15:47:22 +01001176 dquot->dq_dqb.dqb_btime = get_seconds() +
1177 sb_dqopt(sb)->info[dquot->dq_type].dqi_bgrace;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 }
1179 else
1180 /*
1181 * We don't allow preallocation to exceed softlimit so exceeding will
1182 * be always printed
1183 */
1184 return NO_QUOTA;
1185 }
1186
1187 return QUOTA_OK;
1188}
1189
Jan Kara12095462008-08-20 14:45:12 +02001190static int info_idq_free(struct dquot *dquot, qsize_t inodes)
Jan Kara657d3bf2008-07-25 01:46:52 -07001191{
Jan Kara268157b2009-01-27 15:47:22 +01001192 qsize_t newinodes;
1193
Jan Kara657d3bf2008-07-25 01:46:52 -07001194 if (test_bit(DQ_FAKE_B, &dquot->dq_flags) ||
Jan Karaf55abc02008-08-20 17:50:32 +02001195 dquot->dq_dqb.dqb_curinodes <= dquot->dq_dqb.dqb_isoftlimit ||
1196 !sb_has_quota_limits_enabled(dquot->dq_sb, dquot->dq_type))
Jan Kara657d3bf2008-07-25 01:46:52 -07001197 return QUOTA_NL_NOWARN;
1198
Jan Kara268157b2009-01-27 15:47:22 +01001199 newinodes = dquot->dq_dqb.dqb_curinodes - inodes;
1200 if (newinodes <= dquot->dq_dqb.dqb_isoftlimit)
Jan Kara657d3bf2008-07-25 01:46:52 -07001201 return QUOTA_NL_ISOFTBELOW;
1202 if (dquot->dq_dqb.dqb_curinodes >= dquot->dq_dqb.dqb_ihardlimit &&
Jan Kara268157b2009-01-27 15:47:22 +01001203 newinodes < dquot->dq_dqb.dqb_ihardlimit)
Jan Kara657d3bf2008-07-25 01:46:52 -07001204 return QUOTA_NL_IHARDBELOW;
1205 return QUOTA_NL_NOWARN;
1206}
1207
1208static int info_bdq_free(struct dquot *dquot, qsize_t space)
1209{
1210 if (test_bit(DQ_FAKE_B, &dquot->dq_flags) ||
Jan Kara12095462008-08-20 14:45:12 +02001211 dquot->dq_dqb.dqb_curspace <= dquot->dq_dqb.dqb_bsoftlimit)
Jan Kara657d3bf2008-07-25 01:46:52 -07001212 return QUOTA_NL_NOWARN;
1213
Jan Kara12095462008-08-20 14:45:12 +02001214 if (dquot->dq_dqb.dqb_curspace - space <= dquot->dq_dqb.dqb_bsoftlimit)
Jan Kara657d3bf2008-07-25 01:46:52 -07001215 return QUOTA_NL_BSOFTBELOW;
Jan Kara12095462008-08-20 14:45:12 +02001216 if (dquot->dq_dqb.dqb_curspace >= dquot->dq_dqb.dqb_bhardlimit &&
1217 dquot->dq_dqb.dqb_curspace - space < dquot->dq_dqb.dqb_bhardlimit)
Jan Kara657d3bf2008-07-25 01:46:52 -07001218 return QUOTA_NL_BHARDBELOW;
1219 return QUOTA_NL_NOWARN;
1220}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221/*
1222 * Initialize quota pointers in inode
Jan Karacc334122009-01-12 17:23:05 +01001223 * We do things in a bit complicated way but by that we avoid calling
1224 * dqget() and thus filesystem callbacks under dqptr_sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 */
1226int dquot_initialize(struct inode *inode, int type)
1227{
1228 unsigned int id = 0;
1229 int cnt, ret = 0;
Jan Karadd6f3c62009-01-26 16:01:43 +01001230 struct dquot *got[MAXQUOTAS] = { NULL, NULL };
Jan Karacc334122009-01-12 17:23:05 +01001231 struct super_block *sb = inode->i_sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232
Ingo Molnard3be9152006-03-23 03:00:29 -08001233 /* First test before acquiring mutex - solves deadlocks when we
1234 * re-enter the quota code and are already holding the mutex */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 if (IS_NOQUOTA(inode))
1236 return 0;
Jan Karacc334122009-01-12 17:23:05 +01001237
1238 /* First get references to structures we might need. */
1239 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1240 if (type != -1 && cnt != type)
1241 continue;
1242 switch (cnt) {
1243 case USRQUOTA:
1244 id = inode->i_uid;
1245 break;
1246 case GRPQUOTA:
1247 id = inode->i_gid;
1248 break;
1249 }
1250 got[cnt] = dqget(sb, id, cnt);
1251 }
1252
1253 down_write(&sb_dqopt(sb)->dqptr_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 /* Having dqptr_sem we know NOQUOTA flags can't be altered... */
1255 if (IS_NOQUOTA(inode))
1256 goto out_err;
1257 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1258 if (type != -1 && cnt != type)
1259 continue;
Jan Karacc334122009-01-12 17:23:05 +01001260 /* Avoid races with quotaoff() */
1261 if (!sb_has_quota_active(sb, cnt))
1262 continue;
Jan Karadd6f3c62009-01-26 16:01:43 +01001263 if (!inode->i_dquot[cnt]) {
Jan Karacc334122009-01-12 17:23:05 +01001264 inode->i_dquot[cnt] = got[cnt];
Jan Karadd6f3c62009-01-26 16:01:43 +01001265 got[cnt] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 }
1267 }
1268out_err:
Jan Karacc334122009-01-12 17:23:05 +01001269 up_write(&sb_dqopt(sb)->dqptr_sem);
1270 /* Drop unused references */
1271 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1272 dqput(got[cnt]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 return ret;
1274}
Mingming Cao08d03502009-01-14 16:19:32 +01001275EXPORT_SYMBOL(dquot_initialize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276
1277/*
1278 * Release all quotas referenced by inode
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 */
Jan Kara3d9ea252008-10-10 16:12:23 +02001280int dquot_drop(struct inode *inode)
1281{
Jan Karacc334122009-01-12 17:23:05 +01001282 int cnt;
1283 struct dquot *put[MAXQUOTAS];
1284
Jan Kara3d9ea252008-10-10 16:12:23 +02001285 down_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
Jan Karacc334122009-01-12 17:23:05 +01001286 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1287 put[cnt] = inode->i_dquot[cnt];
Jan Karadd6f3c62009-01-26 16:01:43 +01001288 inode->i_dquot[cnt] = NULL;
Jan Karacc334122009-01-12 17:23:05 +01001289 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 up_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
Jan Karacc334122009-01-12 17:23:05 +01001291
1292 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1293 dqput(put[cnt]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294 return 0;
1295}
Mingming Cao08d03502009-01-14 16:19:32 +01001296EXPORT_SYMBOL(dquot_drop);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297
Jan Karab85f4b82008-07-25 01:46:50 -07001298/* Wrapper to remove references to quota structures from inode */
1299void vfs_dq_drop(struct inode *inode)
1300{
1301 /* Here we can get arbitrary inode from clear_inode() so we have
1302 * to be careful. OTOH we don't need locking as quota operations
1303 * are allowed to change only at mount time */
1304 if (!IS_NOQUOTA(inode) && inode->i_sb && inode->i_sb->dq_op
1305 && inode->i_sb->dq_op->drop) {
1306 int cnt;
1307 /* Test before calling to rule out calls from proc and such
1308 * where we are not allowed to block. Note that this is
1309 * actually reliable test even without the lock - the caller
1310 * must assure that nobody can come after the DQUOT_DROP and
1311 * add quota pointers back anyway */
1312 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
Jan Karadd6f3c62009-01-26 16:01:43 +01001313 if (inode->i_dquot[cnt])
Jan Karab85f4b82008-07-25 01:46:50 -07001314 break;
1315 if (cnt < MAXQUOTAS)
1316 inode->i_sb->dq_op->drop(inode);
1317 }
1318}
Mingming Cao08d03502009-01-14 16:19:32 +01001319EXPORT_SYMBOL(vfs_dq_drop);
Jan Karab85f4b82008-07-25 01:46:50 -07001320
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321/*
1322 * Following four functions update i_blocks+i_bytes fields and
1323 * quota information (together with appropriate checks)
1324 * NOTE: We absolutely rely on the fact that caller dirties
1325 * the inode (usually macros in quotaops.h care about this) and
1326 * holds a handle for the current transaction so that dquot write and
1327 * inode write go into the same transaction.
1328 */
1329
1330/*
1331 * This operation can block, but only after everything is updated
1332 */
Mingming Caof18df222009-01-13 16:43:09 +01001333int __dquot_alloc_space(struct inode *inode, qsize_t number,
1334 int warn, int reserve)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335{
Mingming Caof18df222009-01-13 16:43:09 +01001336 int cnt, ret = QUOTA_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 char warntype[MAXQUOTAS];
1338
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
Jan Kara8e893462007-10-16 23:29:31 -07001340 warntype[cnt] = QUOTA_NL_NOWARN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 spin_lock(&dq_data_lock);
1343 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
Jan Karadd6f3c62009-01-26 16:01:43 +01001344 if (!inode->i_dquot[cnt])
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 continue;
Mingming Caof18df222009-01-13 16:43:09 +01001346 if (check_bdq(inode->i_dquot[cnt], number, warn, warntype+cnt)
1347 == NO_QUOTA) {
1348 ret = NO_QUOTA;
1349 goto out_unlock;
1350 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 }
1352 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
Jan Karadd6f3c62009-01-26 16:01:43 +01001353 if (!inode->i_dquot[cnt])
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 continue;
Mingming Caof18df222009-01-13 16:43:09 +01001355 if (reserve)
1356 dquot_resv_space(inode->i_dquot[cnt], number);
1357 else
1358 dquot_incr_space(inode->i_dquot[cnt], number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 }
Mingming Caof18df222009-01-13 16:43:09 +01001360 if (!reserve)
1361 inode_add_bytes(inode, number);
1362out_unlock:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363 spin_unlock(&dq_data_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 flush_warnings(inode->i_dquot, warntype);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 return ret;
1366}
1367
Mingming Caof18df222009-01-13 16:43:09 +01001368int dquot_alloc_space(struct inode *inode, qsize_t number, int warn)
1369{
1370 int cnt, ret = QUOTA_OK;
1371
1372 /*
1373 * First test before acquiring mutex - solves deadlocks when we
1374 * re-enter the quota code and are already holding the mutex
1375 */
1376 if (IS_NOQUOTA(inode)) {
1377 inode_add_bytes(inode, number);
1378 goto out;
1379 }
1380
1381 down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1382 if (IS_NOQUOTA(inode)) {
1383 inode_add_bytes(inode, number);
1384 goto out_unlock;
1385 }
1386
1387 ret = __dquot_alloc_space(inode, number, warn, 0);
1388 if (ret == NO_QUOTA)
1389 goto out_unlock;
1390
1391 /* Dirtify all the dquots - this can block when journalling */
1392 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1393 if (inode->i_dquot[cnt])
1394 mark_dquot_dirty(inode->i_dquot[cnt]);
1395out_unlock:
1396 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1397out:
1398 return ret;
1399}
Mingming Cao08d03502009-01-14 16:19:32 +01001400EXPORT_SYMBOL(dquot_alloc_space);
Mingming Caof18df222009-01-13 16:43:09 +01001401
1402int dquot_reserve_space(struct inode *inode, qsize_t number, int warn)
1403{
1404 int ret = QUOTA_OK;
1405
1406 if (IS_NOQUOTA(inode))
1407 goto out;
1408
1409 down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1410 if (IS_NOQUOTA(inode))
1411 goto out_unlock;
1412
1413 ret = __dquot_alloc_space(inode, number, warn, 1);
1414out_unlock:
1415 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1416out:
1417 return ret;
1418}
1419EXPORT_SYMBOL(dquot_reserve_space);
1420
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421/*
1422 * This operation can block, but only after everything is updated
1423 */
Jan Kara12095462008-08-20 14:45:12 +02001424int dquot_alloc_inode(const struct inode *inode, qsize_t number)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425{
1426 int cnt, ret = NO_QUOTA;
1427 char warntype[MAXQUOTAS];
1428
Ingo Molnard3be9152006-03-23 03:00:29 -08001429 /* First test before acquiring mutex - solves deadlocks when we
1430 * re-enter the quota code and are already holding the mutex */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431 if (IS_NOQUOTA(inode))
1432 return QUOTA_OK;
1433 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
Jan Kara8e893462007-10-16 23:29:31 -07001434 warntype[cnt] = QUOTA_NL_NOWARN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1436 if (IS_NOQUOTA(inode)) {
1437 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1438 return QUOTA_OK;
1439 }
1440 spin_lock(&dq_data_lock);
1441 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
Jan Karadd6f3c62009-01-26 16:01:43 +01001442 if (!inode->i_dquot[cnt])
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 continue;
Jan Kara268157b2009-01-27 15:47:22 +01001444 if (check_idq(inode->i_dquot[cnt], number, warntype+cnt)
1445 == NO_QUOTA)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 goto warn_put_all;
1447 }
1448
1449 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
Jan Karadd6f3c62009-01-26 16:01:43 +01001450 if (!inode->i_dquot[cnt])
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451 continue;
1452 dquot_incr_inodes(inode->i_dquot[cnt], number);
1453 }
1454 ret = QUOTA_OK;
1455warn_put_all:
1456 spin_unlock(&dq_data_lock);
1457 if (ret == QUOTA_OK)
1458 /* Dirtify all the dquots - this can block when journalling */
1459 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1460 if (inode->i_dquot[cnt])
1461 mark_dquot_dirty(inode->i_dquot[cnt]);
Jan Kara087ee8d2007-12-17 16:20:26 -08001462 flush_warnings(inode->i_dquot, warntype);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1464 return ret;
1465}
Mingming Cao08d03502009-01-14 16:19:32 +01001466EXPORT_SYMBOL(dquot_alloc_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467
Mingming Cao740d9dc2009-01-13 16:43:14 +01001468int dquot_claim_space(struct inode *inode, qsize_t number)
1469{
1470 int cnt;
1471 int ret = QUOTA_OK;
1472
1473 if (IS_NOQUOTA(inode)) {
1474 inode_add_bytes(inode, number);
1475 goto out;
1476 }
1477
1478 down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1479 if (IS_NOQUOTA(inode)) {
1480 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1481 inode_add_bytes(inode, number);
1482 goto out;
1483 }
1484
1485 spin_lock(&dq_data_lock);
1486 /* Claim reserved quotas to allocated quotas */
1487 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
Jan Karadd6f3c62009-01-26 16:01:43 +01001488 if (inode->i_dquot[cnt])
Mingming Cao740d9dc2009-01-13 16:43:14 +01001489 dquot_claim_reserved_space(inode->i_dquot[cnt],
1490 number);
1491 }
1492 /* Update inode bytes */
1493 inode_add_bytes(inode, number);
1494 spin_unlock(&dq_data_lock);
1495 /* Dirtify all the dquots - this can block when journalling */
1496 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1497 if (inode->i_dquot[cnt])
1498 mark_dquot_dirty(inode->i_dquot[cnt]);
1499 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1500out:
1501 return ret;
1502}
1503EXPORT_SYMBOL(dquot_claim_space);
1504
1505/*
1506 * Release reserved quota space
1507 */
1508void dquot_release_reserved_space(struct inode *inode, qsize_t number)
1509{
1510 int cnt;
1511
1512 if (IS_NOQUOTA(inode))
1513 goto out;
1514
1515 down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1516 if (IS_NOQUOTA(inode))
1517 goto out_unlock;
1518
1519 spin_lock(&dq_data_lock);
1520 /* Release reserved dquots */
1521 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
Jan Karadd6f3c62009-01-26 16:01:43 +01001522 if (inode->i_dquot[cnt])
Mingming Cao740d9dc2009-01-13 16:43:14 +01001523 dquot_free_reserved_space(inode->i_dquot[cnt], number);
1524 }
1525 spin_unlock(&dq_data_lock);
1526
1527out_unlock:
1528 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1529out:
1530 return;
1531}
1532EXPORT_SYMBOL(dquot_release_reserved_space);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533
1534/*
1535 * This operation can block, but only after everything is updated
1536 */
1537int dquot_free_space(struct inode *inode, qsize_t number)
1538{
1539 unsigned int cnt;
Jan Kara657d3bf2008-07-25 01:46:52 -07001540 char warntype[MAXQUOTAS];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541
Ingo Molnard3be9152006-03-23 03:00:29 -08001542 /* First test before acquiring mutex - solves deadlocks when we
1543 * re-enter the quota code and are already holding the mutex */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544 if (IS_NOQUOTA(inode)) {
1545out_sub:
1546 inode_sub_bytes(inode, number);
1547 return QUOTA_OK;
1548 }
Jan Kara657d3bf2008-07-25 01:46:52 -07001549
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550 down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1551 /* Now recheck reliably when holding dqptr_sem */
1552 if (IS_NOQUOTA(inode)) {
1553 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1554 goto out_sub;
1555 }
1556 spin_lock(&dq_data_lock);
1557 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
Jan Karadd6f3c62009-01-26 16:01:43 +01001558 if (!inode->i_dquot[cnt])
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559 continue;
Jan Kara657d3bf2008-07-25 01:46:52 -07001560 warntype[cnt] = info_bdq_free(inode->i_dquot[cnt], number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561 dquot_decr_space(inode->i_dquot[cnt], number);
1562 }
1563 inode_sub_bytes(inode, number);
1564 spin_unlock(&dq_data_lock);
1565 /* Dirtify all the dquots - this can block when journalling */
1566 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1567 if (inode->i_dquot[cnt])
1568 mark_dquot_dirty(inode->i_dquot[cnt]);
Jan Kara657d3bf2008-07-25 01:46:52 -07001569 flush_warnings(inode->i_dquot, warntype);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1571 return QUOTA_OK;
1572}
Mingming Cao08d03502009-01-14 16:19:32 +01001573EXPORT_SYMBOL(dquot_free_space);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574
1575/*
1576 * This operation can block, but only after everything is updated
1577 */
Jan Kara12095462008-08-20 14:45:12 +02001578int dquot_free_inode(const struct inode *inode, qsize_t number)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579{
1580 unsigned int cnt;
Jan Kara657d3bf2008-07-25 01:46:52 -07001581 char warntype[MAXQUOTAS];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582
Ingo Molnard3be9152006-03-23 03:00:29 -08001583 /* First test before acquiring mutex - solves deadlocks when we
1584 * re-enter the quota code and are already holding the mutex */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585 if (IS_NOQUOTA(inode))
1586 return QUOTA_OK;
Jan Kara657d3bf2008-07-25 01:46:52 -07001587
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588 down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1589 /* Now recheck reliably when holding dqptr_sem */
1590 if (IS_NOQUOTA(inode)) {
1591 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1592 return QUOTA_OK;
1593 }
1594 spin_lock(&dq_data_lock);
1595 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
Jan Karadd6f3c62009-01-26 16:01:43 +01001596 if (!inode->i_dquot[cnt])
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597 continue;
Jan Kara657d3bf2008-07-25 01:46:52 -07001598 warntype[cnt] = info_idq_free(inode->i_dquot[cnt], number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599 dquot_decr_inodes(inode->i_dquot[cnt], number);
1600 }
1601 spin_unlock(&dq_data_lock);
1602 /* Dirtify all the dquots - this can block when journalling */
1603 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1604 if (inode->i_dquot[cnt])
1605 mark_dquot_dirty(inode->i_dquot[cnt]);
Jan Kara657d3bf2008-07-25 01:46:52 -07001606 flush_warnings(inode->i_dquot, warntype);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1608 return QUOTA_OK;
1609}
Mingming Cao08d03502009-01-14 16:19:32 +01001610EXPORT_SYMBOL(dquot_free_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611
1612/*
Mingming Cao740d9dc2009-01-13 16:43:14 +01001613 * call back function, get reserved quota space from underlying fs
1614 */
1615qsize_t dquot_get_reserved_space(struct inode *inode)
1616{
1617 qsize_t reserved_space = 0;
1618
1619 if (sb_any_quota_active(inode->i_sb) &&
1620 inode->i_sb->dq_op->get_reserved_space)
1621 reserved_space = inode->i_sb->dq_op->get_reserved_space(inode);
1622 return reserved_space;
1623}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624
1625/*
1626 * Transfer the number of inode and blocks from one diskquota to an other.
1627 *
1628 * This operation can block, but only after everything is updated
1629 * A transaction must be started when entering this function.
1630 */
1631int dquot_transfer(struct inode *inode, struct iattr *iattr)
1632{
Mingming Cao740d9dc2009-01-13 16:43:14 +01001633 qsize_t space, cur_space;
1634 qsize_t rsv_space = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635 struct dquot *transfer_from[MAXQUOTAS];
1636 struct dquot *transfer_to[MAXQUOTAS];
Jan Karacc334122009-01-12 17:23:05 +01001637 int cnt, ret = QUOTA_OK;
1638 int chuid = iattr->ia_valid & ATTR_UID && inode->i_uid != iattr->ia_uid,
1639 chgid = iattr->ia_valid & ATTR_GID && inode->i_gid != iattr->ia_gid;
Jan Kara657d3bf2008-07-25 01:46:52 -07001640 char warntype_to[MAXQUOTAS];
1641 char warntype_from_inodes[MAXQUOTAS], warntype_from_space[MAXQUOTAS];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642
Ingo Molnard3be9152006-03-23 03:00:29 -08001643 /* First test before acquiring mutex - solves deadlocks when we
1644 * re-enter the quota code and are already holding the mutex */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645 if (IS_NOQUOTA(inode))
1646 return QUOTA_OK;
Jan Karacc334122009-01-12 17:23:05 +01001647 /* Initialize the arrays */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
Jan Karadd6f3c62009-01-26 16:01:43 +01001649 transfer_from[cnt] = NULL;
1650 transfer_to[cnt] = NULL;
Jan Kara657d3bf2008-07-25 01:46:52 -07001651 warntype_to[cnt] = QUOTA_NL_NOWARN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652 }
Jan Kara268157b2009-01-27 15:47:22 +01001653 if (chuid)
1654 transfer_to[USRQUOTA] = dqget(inode->i_sb, iattr->ia_uid,
1655 USRQUOTA);
1656 if (chgid)
1657 transfer_to[GRPQUOTA] = dqget(inode->i_sb, iattr->ia_gid,
1658 GRPQUOTA);
Jan Karacc334122009-01-12 17:23:05 +01001659
1660 down_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
1661 /* Now recheck reliably when holding dqptr_sem */
1662 if (IS_NOQUOTA(inode)) { /* File without quota accounting? */
1663 up_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
1664 goto put_all;
1665 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666 spin_lock(&dq_data_lock);
Mingming Cao740d9dc2009-01-13 16:43:14 +01001667 cur_space = inode_get_bytes(inode);
1668 rsv_space = dquot_get_reserved_space(inode);
1669 space = cur_space + rsv_space;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670 /* Build the transfer_from list and check the limits */
1671 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
Jan Karadd6f3c62009-01-26 16:01:43 +01001672 if (!transfer_to[cnt])
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673 continue;
1674 transfer_from[cnt] = inode->i_dquot[cnt];
Jan Kara657d3bf2008-07-25 01:46:52 -07001675 if (check_idq(transfer_to[cnt], 1, warntype_to + cnt) ==
1676 NO_QUOTA || check_bdq(transfer_to[cnt], space, 0,
1677 warntype_to + cnt) == NO_QUOTA)
Jan Karacc334122009-01-12 17:23:05 +01001678 goto over_quota;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679 }
1680
1681 /*
1682 * Finally perform the needed transfer from transfer_from to transfer_to
1683 */
1684 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1685 /*
1686 * Skip changes for same uid or gid or for turned off quota-type.
1687 */
Jan Karadd6f3c62009-01-26 16:01:43 +01001688 if (!transfer_to[cnt])
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689 continue;
1690
1691 /* Due to IO error we might not have transfer_from[] structure */
1692 if (transfer_from[cnt]) {
Jan Kara657d3bf2008-07-25 01:46:52 -07001693 warntype_from_inodes[cnt] =
1694 info_idq_free(transfer_from[cnt], 1);
1695 warntype_from_space[cnt] =
1696 info_bdq_free(transfer_from[cnt], space);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697 dquot_decr_inodes(transfer_from[cnt], 1);
Mingming Cao740d9dc2009-01-13 16:43:14 +01001698 dquot_decr_space(transfer_from[cnt], cur_space);
1699 dquot_free_reserved_space(transfer_from[cnt],
1700 rsv_space);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701 }
1702
1703 dquot_incr_inodes(transfer_to[cnt], 1);
Mingming Cao740d9dc2009-01-13 16:43:14 +01001704 dquot_incr_space(transfer_to[cnt], cur_space);
1705 dquot_resv_space(transfer_to[cnt], rsv_space);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706
1707 inode->i_dquot[cnt] = transfer_to[cnt];
1708 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709 spin_unlock(&dq_data_lock);
Jan Karacc334122009-01-12 17:23:05 +01001710 up_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
1711
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712 /* Dirtify all the dquots - this can block when journalling */
1713 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1714 if (transfer_from[cnt])
1715 mark_dquot_dirty(transfer_from[cnt]);
Jan Karacc334122009-01-12 17:23:05 +01001716 if (transfer_to[cnt]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717 mark_dquot_dirty(transfer_to[cnt]);
Jan Karacc334122009-01-12 17:23:05 +01001718 /* The reference we got is transferred to the inode */
Jan Karadd6f3c62009-01-26 16:01:43 +01001719 transfer_to[cnt] = NULL;
Jan Karacc334122009-01-12 17:23:05 +01001720 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721 }
Jan Karacc334122009-01-12 17:23:05 +01001722warn_put_all:
Jan Kara657d3bf2008-07-25 01:46:52 -07001723 flush_warnings(transfer_to, warntype_to);
1724 flush_warnings(transfer_from, warntype_from_inodes);
1725 flush_warnings(transfer_from, warntype_from_space);
Jan Karacc334122009-01-12 17:23:05 +01001726put_all:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
Jan Karacc334122009-01-12 17:23:05 +01001728 dqput(transfer_from[cnt]);
1729 dqput(transfer_to[cnt]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731 return ret;
Jan Karacc334122009-01-12 17:23:05 +01001732over_quota:
1733 spin_unlock(&dq_data_lock);
1734 up_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
1735 /* Clear dquot pointers we don't want to dqput() */
1736 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
Jan Karadd6f3c62009-01-26 16:01:43 +01001737 transfer_from[cnt] = NULL;
Jan Karacc334122009-01-12 17:23:05 +01001738 ret = NO_QUOTA;
1739 goto warn_put_all;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740}
Mingming Cao08d03502009-01-14 16:19:32 +01001741EXPORT_SYMBOL(dquot_transfer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742
Jan Karab85f4b82008-07-25 01:46:50 -07001743/* Wrapper for transferring ownership of an inode */
1744int vfs_dq_transfer(struct inode *inode, struct iattr *iattr)
1745{
Jan Karaf55abc02008-08-20 17:50:32 +02001746 if (sb_any_quota_active(inode->i_sb) && !IS_NOQUOTA(inode)) {
Jan Karab85f4b82008-07-25 01:46:50 -07001747 vfs_dq_init(inode);
1748 if (inode->i_sb->dq_op->transfer(inode, iattr) == NO_QUOTA)
1749 return 1;
1750 }
1751 return 0;
1752}
Mingming Cao08d03502009-01-14 16:19:32 +01001753EXPORT_SYMBOL(vfs_dq_transfer);
Jan Karab85f4b82008-07-25 01:46:50 -07001754
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755/*
1756 * Write info of quota file to disk
1757 */
1758int dquot_commit_info(struct super_block *sb, int type)
1759{
1760 int ret;
1761 struct quota_info *dqopt = sb_dqopt(sb);
1762
Ingo Molnard3be9152006-03-23 03:00:29 -08001763 mutex_lock(&dqopt->dqio_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764 ret = dqopt->ops[type]->write_file_info(sb, type);
Ingo Molnard3be9152006-03-23 03:00:29 -08001765 mutex_unlock(&dqopt->dqio_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766 return ret;
1767}
Mingming Cao08d03502009-01-14 16:19:32 +01001768EXPORT_SYMBOL(dquot_commit_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769
1770/*
1771 * Definitions of diskquota operations.
1772 */
Alexey Dobriyan61e225d2009-09-21 17:01:08 -07001773const struct dquot_operations dquot_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774 .initialize = dquot_initialize,
1775 .drop = dquot_drop,
1776 .alloc_space = dquot_alloc_space,
1777 .alloc_inode = dquot_alloc_inode,
1778 .free_space = dquot_free_space,
1779 .free_inode = dquot_free_inode,
1780 .transfer = dquot_transfer,
1781 .write_dquot = dquot_commit,
1782 .acquire_dquot = dquot_acquire,
1783 .release_dquot = dquot_release,
1784 .mark_dirty = dquot_mark_dquot_dirty,
Jan Kara74f783a2008-08-19 14:51:22 +02001785 .write_info = dquot_commit_info,
1786 .alloc_dquot = dquot_alloc,
1787 .destroy_dquot = dquot_destroy,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788};
1789
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790/*
1791 * Turn quota off on a device. type == -1 ==> quotaoff for all types (umount)
1792 */
Jan Karaf55abc02008-08-20 17:50:32 +02001793int vfs_quota_disable(struct super_block *sb, int type, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794{
Jan Kara0ff5af82008-04-28 02:14:33 -07001795 int cnt, ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796 struct quota_info *dqopt = sb_dqopt(sb);
1797 struct inode *toputinode[MAXQUOTAS];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798
Jan Karaf55abc02008-08-20 17:50:32 +02001799 /* Cannot turn off usage accounting without turning off limits, or
1800 * suspend quotas and simultaneously turn quotas off. */
1801 if ((flags & DQUOT_USAGE_ENABLED && !(flags & DQUOT_LIMITS_ENABLED))
1802 || (flags & DQUOT_SUSPENDED && flags & (DQUOT_LIMITS_ENABLED |
1803 DQUOT_USAGE_ENABLED)))
1804 return -EINVAL;
1805
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806 /* We need to serialize quota_off() for device */
Ingo Molnard3be9152006-03-23 03:00:29 -08001807 mutex_lock(&dqopt->dqonoff_mutex);
Jan Kara9377abd2008-05-12 14:02:08 -07001808
1809 /*
1810 * Skip everything if there's nothing to do. We have to do this because
1811 * sometimes we are called when fill_super() failed and calling
1812 * sync_fs() in such cases does no good.
1813 */
Jan Karaf55abc02008-08-20 17:50:32 +02001814 if (!sb_any_quota_loaded(sb)) {
Jan Kara9377abd2008-05-12 14:02:08 -07001815 mutex_unlock(&dqopt->dqonoff_mutex);
1816 return 0;
1817 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1819 toputinode[cnt] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820 if (type != -1 && cnt != type)
1821 continue;
Jan Karaf55abc02008-08-20 17:50:32 +02001822 if (!sb_has_quota_loaded(sb, cnt))
Jan Kara0ff5af82008-04-28 02:14:33 -07001823 continue;
Jan Karaf55abc02008-08-20 17:50:32 +02001824
1825 if (flags & DQUOT_SUSPENDED) {
Jan Karacc334122009-01-12 17:23:05 +01001826 spin_lock(&dq_state_lock);
Jan Karaf55abc02008-08-20 17:50:32 +02001827 dqopt->flags |=
1828 dquot_state_flag(DQUOT_SUSPENDED, cnt);
Jan Karacc334122009-01-12 17:23:05 +01001829 spin_unlock(&dq_state_lock);
Jan Karaf55abc02008-08-20 17:50:32 +02001830 } else {
Jan Karacc334122009-01-12 17:23:05 +01001831 spin_lock(&dq_state_lock);
Jan Karaf55abc02008-08-20 17:50:32 +02001832 dqopt->flags &= ~dquot_state_flag(flags, cnt);
1833 /* Turning off suspended quotas? */
1834 if (!sb_has_quota_loaded(sb, cnt) &&
1835 sb_has_quota_suspended(sb, cnt)) {
1836 dqopt->flags &= ~dquot_state_flag(
1837 DQUOT_SUSPENDED, cnt);
Jan Karacc334122009-01-12 17:23:05 +01001838 spin_unlock(&dq_state_lock);
Jan Karaf55abc02008-08-20 17:50:32 +02001839 iput(dqopt->files[cnt]);
1840 dqopt->files[cnt] = NULL;
1841 continue;
1842 }
Jan Karacc334122009-01-12 17:23:05 +01001843 spin_unlock(&dq_state_lock);
Jan Kara0ff5af82008-04-28 02:14:33 -07001844 }
Jan Karaf55abc02008-08-20 17:50:32 +02001845
1846 /* We still have to keep quota loaded? */
1847 if (sb_has_quota_loaded(sb, cnt) && !(flags & DQUOT_SUSPENDED))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849
1850 /* Note: these are blocking operations */
1851 drop_dquot_ref(sb, cnt);
1852 invalidate_dquots(sb, cnt);
1853 /*
Jan Kara268157b2009-01-27 15:47:22 +01001854 * Now all dquots should be invalidated, all writes done so we
1855 * should be only users of the info. No locks needed.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856 */
1857 if (info_dirty(&dqopt->info[cnt]))
1858 sb->dq_op->write_info(sb, cnt);
1859 if (dqopt->ops[cnt]->free_file_info)
1860 dqopt->ops[cnt]->free_file_info(sb, cnt);
1861 put_quota_format(dqopt->info[cnt].dqi_format);
1862
1863 toputinode[cnt] = dqopt->files[cnt];
Jan Karaf55abc02008-08-20 17:50:32 +02001864 if (!sb_has_quota_loaded(sb, cnt))
Jan Kara0ff5af82008-04-28 02:14:33 -07001865 dqopt->files[cnt] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866 dqopt->info[cnt].dqi_flags = 0;
1867 dqopt->info[cnt].dqi_igrace = 0;
1868 dqopt->info[cnt].dqi_bgrace = 0;
1869 dqopt->ops[cnt] = NULL;
1870 }
Ingo Molnard3be9152006-03-23 03:00:29 -08001871 mutex_unlock(&dqopt->dqonoff_mutex);
Jan Karaca785ec2008-09-30 17:53:37 +02001872
1873 /* Skip syncing and setting flags if quota files are hidden */
1874 if (dqopt->flags & DQUOT_QUOTA_SYS_FILE)
1875 goto put_inodes;
1876
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877 /* Sync the superblock so that buffers with quota data are written to
Al Viro7b7b1ac2005-11-07 17:13:39 -05001878 * disk (and so userspace sees correct data afterwards). */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879 if (sb->s_op->sync_fs)
1880 sb->s_op->sync_fs(sb, 1);
1881 sync_blockdev(sb->s_bdev);
1882 /* Now the quota files are just ordinary files and we can set the
1883 * inode flags back. Moreover we discard the pagecache so that
1884 * userspace sees the writes we did bypassing the pagecache. We
1885 * must also discard the blockdev buffers so that we see the
1886 * changes done by userspace on the next quotaon() */
1887 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1888 if (toputinode[cnt]) {
Ingo Molnard3be9152006-03-23 03:00:29 -08001889 mutex_lock(&dqopt->dqonoff_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890 /* If quota was reenabled in the meantime, we have
1891 * nothing to do */
Jan Karaf55abc02008-08-20 17:50:32 +02001892 if (!sb_has_quota_loaded(sb, cnt)) {
Jan Kara268157b2009-01-27 15:47:22 +01001893 mutex_lock_nested(&toputinode[cnt]->i_mutex,
1894 I_MUTEX_QUOTA);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895 toputinode[cnt]->i_flags &= ~(S_IMMUTABLE |
1896 S_NOATIME | S_NOQUOTA);
Jan Kara268157b2009-01-27 15:47:22 +01001897 truncate_inode_pages(&toputinode[cnt]->i_data,
1898 0);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001899 mutex_unlock(&toputinode[cnt]->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900 mark_inode_dirty(toputinode[cnt]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901 }
Ingo Molnard3be9152006-03-23 03:00:29 -08001902 mutex_unlock(&dqopt->dqonoff_mutex);
Jan Karaca785ec2008-09-30 17:53:37 +02001903 }
1904 if (sb->s_bdev)
1905 invalidate_bdev(sb->s_bdev);
1906put_inodes:
1907 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1908 if (toputinode[cnt]) {
Jan Kara0ff5af82008-04-28 02:14:33 -07001909 /* On remount RO, we keep the inode pointer so that we
Jan Karaf55abc02008-08-20 17:50:32 +02001910 * can reenable quota on the subsequent remount RW. We
1911 * have to check 'flags' variable and not use sb_has_
1912 * function because another quotaon / quotaoff could
1913 * change global state before we got here. We refuse
1914 * to suspend quotas when there is pending delete on
1915 * the quota file... */
1916 if (!(flags & DQUOT_SUSPENDED))
Jan Kara0ff5af82008-04-28 02:14:33 -07001917 iput(toputinode[cnt]);
1918 else if (!toputinode[cnt]->i_nlink)
1919 ret = -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920 }
Jan Kara0ff5af82008-04-28 02:14:33 -07001921 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922}
Mingming Cao08d03502009-01-14 16:19:32 +01001923EXPORT_SYMBOL(vfs_quota_disable);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924
Jan Karaf55abc02008-08-20 17:50:32 +02001925int vfs_quota_off(struct super_block *sb, int type, int remount)
1926{
1927 return vfs_quota_disable(sb, type, remount ? DQUOT_SUSPENDED :
1928 (DQUOT_USAGE_ENABLED | DQUOT_LIMITS_ENABLED));
1929}
Mingming Cao08d03502009-01-14 16:19:32 +01001930EXPORT_SYMBOL(vfs_quota_off);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931/*
1932 * Turn quotas on on a device
1933 */
1934
Jan Karaf55abc02008-08-20 17:50:32 +02001935/*
1936 * Helper function to turn quotas on when we already have the inode of
1937 * quota file and no quota information is loaded.
1938 */
1939static int vfs_load_quota_inode(struct inode *inode, int type, int format_id,
1940 unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941{
1942 struct quota_format_type *fmt = find_quota_format(format_id);
1943 struct super_block *sb = inode->i_sb;
1944 struct quota_info *dqopt = sb_dqopt(sb);
1945 int error;
1946 int oldflags = -1;
1947
1948 if (!fmt)
1949 return -ESRCH;
1950 if (!S_ISREG(inode->i_mode)) {
1951 error = -EACCES;
1952 goto out_fmt;
1953 }
1954 if (IS_RDONLY(inode)) {
1955 error = -EROFS;
1956 goto out_fmt;
1957 }
1958 if (!sb->s_op->quota_write || !sb->s_op->quota_read) {
1959 error = -EINVAL;
1960 goto out_fmt;
1961 }
Jan Karaf55abc02008-08-20 17:50:32 +02001962 /* Usage always has to be set... */
1963 if (!(flags & DQUOT_USAGE_ENABLED)) {
1964 error = -EINVAL;
1965 goto out_fmt;
1966 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967
Jan Karaca785ec2008-09-30 17:53:37 +02001968 if (!(dqopt->flags & DQUOT_QUOTA_SYS_FILE)) {
1969 /* As we bypass the pagecache we must now flush the inode so
1970 * that we see all the changes from userspace... */
1971 write_inode_now(inode, 1);
1972 /* And now flush the block cache so that kernel sees the
1973 * changes */
1974 invalidate_bdev(sb->s_bdev);
1975 }
Ingo Molnard3be9152006-03-23 03:00:29 -08001976 mutex_lock(&dqopt->dqonoff_mutex);
Jan Karaf55abc02008-08-20 17:50:32 +02001977 if (sb_has_quota_loaded(sb, type)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978 error = -EBUSY;
1979 goto out_lock;
1980 }
Jan Karaca785ec2008-09-30 17:53:37 +02001981
1982 if (!(dqopt->flags & DQUOT_QUOTA_SYS_FILE)) {
1983 /* We don't want quota and atime on quota files (deadlocks
1984 * possible) Also nobody should write to the file - we use
1985 * special IO operations which ignore the immutable bit. */
1986 down_write(&dqopt->dqptr_sem);
Jan Karadee86562009-07-22 18:12:17 +02001987 mutex_lock_nested(&inode->i_mutex, I_MUTEX_QUOTA);
Jan Kara268157b2009-01-27 15:47:22 +01001988 oldflags = inode->i_flags & (S_NOATIME | S_IMMUTABLE |
1989 S_NOQUOTA);
Jan Karaca785ec2008-09-30 17:53:37 +02001990 inode->i_flags |= S_NOQUOTA | S_NOATIME | S_IMMUTABLE;
Jan Karadee86562009-07-22 18:12:17 +02001991 mutex_unlock(&inode->i_mutex);
Jan Karaca785ec2008-09-30 17:53:37 +02001992 up_write(&dqopt->dqptr_sem);
1993 sb->dq_op->drop(inode);
1994 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995
1996 error = -EIO;
1997 dqopt->files[type] = igrab(inode);
1998 if (!dqopt->files[type])
1999 goto out_lock;
2000 error = -EINVAL;
2001 if (!fmt->qf_ops->check_quota_file(sb, type))
2002 goto out_file_init;
2003
2004 dqopt->ops[type] = fmt->qf_ops;
2005 dqopt->info[type].dqi_format = fmt;
Jan Kara0ff5af82008-04-28 02:14:33 -07002006 dqopt->info[type].dqi_fmt_id = format_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007 INIT_LIST_HEAD(&dqopt->info[type].dqi_dirty_list);
Ingo Molnard3be9152006-03-23 03:00:29 -08002008 mutex_lock(&dqopt->dqio_mutex);
Jan Kara268157b2009-01-27 15:47:22 +01002009 error = dqopt->ops[type]->read_file_info(sb, type);
2010 if (error < 0) {
Ingo Molnard3be9152006-03-23 03:00:29 -08002011 mutex_unlock(&dqopt->dqio_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012 goto out_file_init;
2013 }
Ingo Molnard3be9152006-03-23 03:00:29 -08002014 mutex_unlock(&dqopt->dqio_mutex);
Jan Karacc334122009-01-12 17:23:05 +01002015 spin_lock(&dq_state_lock);
Jan Karaf55abc02008-08-20 17:50:32 +02002016 dqopt->flags |= dquot_state_flag(flags, type);
Jan Karacc334122009-01-12 17:23:05 +01002017 spin_unlock(&dq_state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018
2019 add_dquot_ref(sb, type);
Ingo Molnard3be9152006-03-23 03:00:29 -08002020 mutex_unlock(&dqopt->dqonoff_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021
2022 return 0;
2023
2024out_file_init:
2025 dqopt->files[type] = NULL;
2026 iput(inode);
2027out_lock:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028 if (oldflags != -1) {
2029 down_write(&dqopt->dqptr_sem);
Jan Karadee86562009-07-22 18:12:17 +02002030 mutex_lock_nested(&inode->i_mutex, I_MUTEX_QUOTA);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031 /* Set the flags back (in the case of accidental quotaon()
2032 * on a wrong file we don't want to mess up the flags) */
2033 inode->i_flags &= ~(S_NOATIME | S_NOQUOTA | S_IMMUTABLE);
2034 inode->i_flags |= oldflags;
Jan Karadee86562009-07-22 18:12:17 +02002035 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036 up_write(&dqopt->dqptr_sem);
2037 }
Jiaying Zhangd01730d2009-07-07 18:15:21 +02002038 mutex_unlock(&dqopt->dqonoff_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039out_fmt:
2040 put_quota_format(fmt);
2041
2042 return error;
2043}
2044
Jan Kara0ff5af82008-04-28 02:14:33 -07002045/* Reenable quotas on remount RW */
2046static int vfs_quota_on_remount(struct super_block *sb, int type)
2047{
2048 struct quota_info *dqopt = sb_dqopt(sb);
2049 struct inode *inode;
2050 int ret;
Jan Karaf55abc02008-08-20 17:50:32 +02002051 unsigned int flags;
Jan Kara0ff5af82008-04-28 02:14:33 -07002052
2053 mutex_lock(&dqopt->dqonoff_mutex);
2054 if (!sb_has_quota_suspended(sb, type)) {
2055 mutex_unlock(&dqopt->dqonoff_mutex);
2056 return 0;
2057 }
Jan Kara0ff5af82008-04-28 02:14:33 -07002058 inode = dqopt->files[type];
2059 dqopt->files[type] = NULL;
Jan Karacc334122009-01-12 17:23:05 +01002060 spin_lock(&dq_state_lock);
Jan Karaf55abc02008-08-20 17:50:32 +02002061 flags = dqopt->flags & dquot_state_flag(DQUOT_USAGE_ENABLED |
2062 DQUOT_LIMITS_ENABLED, type);
2063 dqopt->flags &= ~dquot_state_flag(DQUOT_STATE_FLAGS, type);
Jan Karacc334122009-01-12 17:23:05 +01002064 spin_unlock(&dq_state_lock);
Jan Kara0ff5af82008-04-28 02:14:33 -07002065 mutex_unlock(&dqopt->dqonoff_mutex);
2066
Jan Karaf55abc02008-08-20 17:50:32 +02002067 flags = dquot_generic_flag(flags, type);
2068 ret = vfs_load_quota_inode(inode, type, dqopt->info[type].dqi_fmt_id,
2069 flags);
Jan Kara0ff5af82008-04-28 02:14:33 -07002070 iput(inode);
2071
2072 return ret;
2073}
2074
Al Viro77e69da2008-08-01 04:29:18 -04002075int vfs_quota_on_path(struct super_block *sb, int type, int format_id,
2076 struct path *path)
2077{
2078 int error = security_quota_on(path->dentry);
2079 if (error)
2080 return error;
2081 /* Quota file not on the same filesystem? */
2082 if (path->mnt->mnt_sb != sb)
2083 error = -EXDEV;
2084 else
Jan Karaf55abc02008-08-20 17:50:32 +02002085 error = vfs_load_quota_inode(path->dentry->d_inode, type,
2086 format_id, DQUOT_USAGE_ENABLED |
2087 DQUOT_LIMITS_ENABLED);
Al Viro77e69da2008-08-01 04:29:18 -04002088 return error;
2089}
Mingming Cao08d03502009-01-14 16:19:32 +01002090EXPORT_SYMBOL(vfs_quota_on_path);
Al Viro77e69da2008-08-01 04:29:18 -04002091
Al Viro82646132008-08-02 00:57:06 -04002092int vfs_quota_on(struct super_block *sb, int type, int format_id, char *name,
Jan Kara0ff5af82008-04-28 02:14:33 -07002093 int remount)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094{
Al Viro82646132008-08-02 00:57:06 -04002095 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096 int error;
2097
Jan Kara0ff5af82008-04-28 02:14:33 -07002098 if (remount)
2099 return vfs_quota_on_remount(sb, type);
2100
Al Viro82646132008-08-02 00:57:06 -04002101 error = kern_path(name, LOOKUP_FOLLOW, &path);
Al Viro77e69da2008-08-01 04:29:18 -04002102 if (!error) {
Al Viro82646132008-08-02 00:57:06 -04002103 error = vfs_quota_on_path(sb, type, format_id, &path);
2104 path_put(&path);
Al Viro77e69da2008-08-01 04:29:18 -04002105 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106 return error;
2107}
Mingming Cao08d03502009-01-14 16:19:32 +01002108EXPORT_SYMBOL(vfs_quota_on);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109
2110/*
Jan Karaf55abc02008-08-20 17:50:32 +02002111 * More powerful function for turning on quotas allowing setting
2112 * of individual quota flags
2113 */
2114int vfs_quota_enable(struct inode *inode, int type, int format_id,
2115 unsigned int flags)
2116{
2117 int ret = 0;
2118 struct super_block *sb = inode->i_sb;
2119 struct quota_info *dqopt = sb_dqopt(sb);
2120
2121 /* Just unsuspend quotas? */
2122 if (flags & DQUOT_SUSPENDED)
2123 return vfs_quota_on_remount(sb, type);
2124 if (!flags)
2125 return 0;
2126 /* Just updating flags needed? */
2127 if (sb_has_quota_loaded(sb, type)) {
2128 mutex_lock(&dqopt->dqonoff_mutex);
2129 /* Now do a reliable test... */
2130 if (!sb_has_quota_loaded(sb, type)) {
2131 mutex_unlock(&dqopt->dqonoff_mutex);
2132 goto load_quota;
2133 }
2134 if (flags & DQUOT_USAGE_ENABLED &&
2135 sb_has_quota_usage_enabled(sb, type)) {
2136 ret = -EBUSY;
2137 goto out_lock;
2138 }
2139 if (flags & DQUOT_LIMITS_ENABLED &&
2140 sb_has_quota_limits_enabled(sb, type)) {
2141 ret = -EBUSY;
2142 goto out_lock;
2143 }
Jan Karacc334122009-01-12 17:23:05 +01002144 spin_lock(&dq_state_lock);
Jan Karaf55abc02008-08-20 17:50:32 +02002145 sb_dqopt(sb)->flags |= dquot_state_flag(flags, type);
Jan Karacc334122009-01-12 17:23:05 +01002146 spin_unlock(&dq_state_lock);
Jan Karaf55abc02008-08-20 17:50:32 +02002147out_lock:
2148 mutex_unlock(&dqopt->dqonoff_mutex);
2149 return ret;
2150 }
2151
2152load_quota:
2153 return vfs_load_quota_inode(inode, type, format_id, flags);
2154}
Mingming Cao08d03502009-01-14 16:19:32 +01002155EXPORT_SYMBOL(vfs_quota_enable);
Jan Karaf55abc02008-08-20 17:50:32 +02002156
2157/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158 * This function is used when filesystem needs to initialize quotas
2159 * during mount time.
2160 */
Christoph Hellwig84de8562005-06-23 00:09:16 -07002161int vfs_quota_on_mount(struct super_block *sb, char *qf_name,
2162 int format_id, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163{
Christoph Hellwig84de8562005-06-23 00:09:16 -07002164 struct dentry *dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165 int error;
2166
Christoph Hellwig2fa389c2005-06-23 00:09:16 -07002167 dentry = lookup_one_len(qf_name, sb->s_root, strlen(qf_name));
Christoph Hellwig84de8562005-06-23 00:09:16 -07002168 if (IS_ERR(dentry))
2169 return PTR_ERR(dentry);
2170
Jan Kara154f4842005-11-28 13:44:14 -08002171 if (!dentry->d_inode) {
2172 error = -ENOENT;
2173 goto out;
2174 }
2175
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176 error = security_quota_on(dentry);
Christoph Hellwig84de8562005-06-23 00:09:16 -07002177 if (!error)
Jan Karaf55abc02008-08-20 17:50:32 +02002178 error = vfs_load_quota_inode(dentry->d_inode, type, format_id,
2179 DQUOT_USAGE_ENABLED | DQUOT_LIMITS_ENABLED);
Christoph Hellwig84de8562005-06-23 00:09:16 -07002180
Jan Kara154f4842005-11-28 13:44:14 -08002181out:
Christoph Hellwig84de8562005-06-23 00:09:16 -07002182 dput(dentry);
2183 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184}
Mingming Cao08d03502009-01-14 16:19:32 +01002185EXPORT_SYMBOL(vfs_quota_on_mount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186
Jan Karab85f4b82008-07-25 01:46:50 -07002187/* Wrapper to turn on quotas when remounting rw */
2188int vfs_dq_quota_on_remount(struct super_block *sb)
2189{
2190 int cnt;
2191 int ret = 0, err;
2192
2193 if (!sb->s_qcop || !sb->s_qcop->quota_on)
2194 return -ENOSYS;
2195 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
2196 err = sb->s_qcop->quota_on(sb, cnt, 0, NULL, 1);
2197 if (err < 0 && !ret)
2198 ret = err;
2199 }
2200 return ret;
2201}
Mingming Cao08d03502009-01-14 16:19:32 +01002202EXPORT_SYMBOL(vfs_dq_quota_on_remount);
Jan Karab85f4b82008-07-25 01:46:50 -07002203
Jan Kara12095462008-08-20 14:45:12 +02002204static inline qsize_t qbtos(qsize_t blocks)
2205{
2206 return blocks << QIF_DQBLKSIZE_BITS;
2207}
2208
2209static inline qsize_t stoqb(qsize_t space)
2210{
2211 return (space + QIF_DQBLKSIZE - 1) >> QIF_DQBLKSIZE_BITS;
2212}
2213
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214/* Generic routine for getting common part of quota structure */
2215static void do_get_dqblk(struct dquot *dquot, struct if_dqblk *di)
2216{
2217 struct mem_dqblk *dm = &dquot->dq_dqb;
2218
2219 spin_lock(&dq_data_lock);
Jan Kara12095462008-08-20 14:45:12 +02002220 di->dqb_bhardlimit = stoqb(dm->dqb_bhardlimit);
2221 di->dqb_bsoftlimit = stoqb(dm->dqb_bsoftlimit);
Mingming Caof18df222009-01-13 16:43:09 +01002222 di->dqb_curspace = dm->dqb_curspace + dm->dqb_rsvspace;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223 di->dqb_ihardlimit = dm->dqb_ihardlimit;
2224 di->dqb_isoftlimit = dm->dqb_isoftlimit;
2225 di->dqb_curinodes = dm->dqb_curinodes;
2226 di->dqb_btime = dm->dqb_btime;
2227 di->dqb_itime = dm->dqb_itime;
2228 di->dqb_valid = QIF_ALL;
2229 spin_unlock(&dq_data_lock);
2230}
2231
Jan Kara268157b2009-01-27 15:47:22 +01002232int vfs_get_dqblk(struct super_block *sb, int type, qid_t id,
2233 struct if_dqblk *di)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234{
2235 struct dquot *dquot;
2236
Jan Karacc334122009-01-12 17:23:05 +01002237 dquot = dqget(sb, id, type);
Jan Karadd6f3c62009-01-26 16:01:43 +01002238 if (!dquot)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239 return -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240 do_get_dqblk(dquot, di);
2241 dqput(dquot);
Jan Karacc334122009-01-12 17:23:05 +01002242
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243 return 0;
2244}
Mingming Cao08d03502009-01-14 16:19:32 +01002245EXPORT_SYMBOL(vfs_get_dqblk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246
2247/* Generic routine for setting common part of quota structure */
Andrew Perepechko338bf9a2008-04-28 02:14:31 -07002248static int do_set_dqblk(struct dquot *dquot, struct if_dqblk *di)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249{
2250 struct mem_dqblk *dm = &dquot->dq_dqb;
2251 int check_blim = 0, check_ilim = 0;
Andrew Perepechko338bf9a2008-04-28 02:14:31 -07002252 struct mem_dqinfo *dqi = &sb_dqopt(dquot->dq_sb)->info[dquot->dq_type];
2253
2254 if ((di->dqb_valid & QIF_BLIMITS &&
2255 (di->dqb_bhardlimit > dqi->dqi_maxblimit ||
2256 di->dqb_bsoftlimit > dqi->dqi_maxblimit)) ||
2257 (di->dqb_valid & QIF_ILIMITS &&
2258 (di->dqb_ihardlimit > dqi->dqi_maxilimit ||
2259 di->dqb_isoftlimit > dqi->dqi_maxilimit)))
2260 return -ERANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002261
2262 spin_lock(&dq_data_lock);
2263 if (di->dqb_valid & QIF_SPACE) {
Mingming Caof18df222009-01-13 16:43:09 +01002264 dm->dqb_curspace = di->dqb_curspace - dm->dqb_rsvspace;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265 check_blim = 1;
Jan Kara4d59bce2008-10-02 16:48:10 +02002266 __set_bit(DQ_LASTSET_B + QIF_SPACE_B, &dquot->dq_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267 }
2268 if (di->dqb_valid & QIF_BLIMITS) {
Jan Kara12095462008-08-20 14:45:12 +02002269 dm->dqb_bsoftlimit = qbtos(di->dqb_bsoftlimit);
2270 dm->dqb_bhardlimit = qbtos(di->dqb_bhardlimit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271 check_blim = 1;
Jan Kara4d59bce2008-10-02 16:48:10 +02002272 __set_bit(DQ_LASTSET_B + QIF_BLIMITS_B, &dquot->dq_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273 }
2274 if (di->dqb_valid & QIF_INODES) {
2275 dm->dqb_curinodes = di->dqb_curinodes;
2276 check_ilim = 1;
Jan Kara4d59bce2008-10-02 16:48:10 +02002277 __set_bit(DQ_LASTSET_B + QIF_INODES_B, &dquot->dq_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278 }
2279 if (di->dqb_valid & QIF_ILIMITS) {
2280 dm->dqb_isoftlimit = di->dqb_isoftlimit;
2281 dm->dqb_ihardlimit = di->dqb_ihardlimit;
2282 check_ilim = 1;
Jan Kara4d59bce2008-10-02 16:48:10 +02002283 __set_bit(DQ_LASTSET_B + QIF_ILIMITS_B, &dquot->dq_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284 }
Jan Kara4d59bce2008-10-02 16:48:10 +02002285 if (di->dqb_valid & QIF_BTIME) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286 dm->dqb_btime = di->dqb_btime;
Jan Karae04a88a92009-01-07 18:07:29 -08002287 check_blim = 1;
Jan Kara4d59bce2008-10-02 16:48:10 +02002288 __set_bit(DQ_LASTSET_B + QIF_BTIME_B, &dquot->dq_flags);
2289 }
2290 if (di->dqb_valid & QIF_ITIME) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291 dm->dqb_itime = di->dqb_itime;
Jan Karae04a88a92009-01-07 18:07:29 -08002292 check_ilim = 1;
Jan Kara4d59bce2008-10-02 16:48:10 +02002293 __set_bit(DQ_LASTSET_B + QIF_ITIME_B, &dquot->dq_flags);
2294 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295
2296 if (check_blim) {
Jan Kara268157b2009-01-27 15:47:22 +01002297 if (!dm->dqb_bsoftlimit ||
2298 dm->dqb_curspace < dm->dqb_bsoftlimit) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299 dm->dqb_btime = 0;
2300 clear_bit(DQ_BLKS_B, &dquot->dq_flags);
Jan Kara268157b2009-01-27 15:47:22 +01002301 } else if (!(di->dqb_valid & QIF_BTIME))
2302 /* Set grace only if user hasn't provided his own... */
Andrew Perepechko338bf9a2008-04-28 02:14:31 -07002303 dm->dqb_btime = get_seconds() + dqi->dqi_bgrace;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304 }
2305 if (check_ilim) {
Jan Kara268157b2009-01-27 15:47:22 +01002306 if (!dm->dqb_isoftlimit ||
2307 dm->dqb_curinodes < dm->dqb_isoftlimit) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308 dm->dqb_itime = 0;
2309 clear_bit(DQ_INODES_B, &dquot->dq_flags);
Jan Kara268157b2009-01-27 15:47:22 +01002310 } else if (!(di->dqb_valid & QIF_ITIME))
2311 /* Set grace only if user hasn't provided his own... */
Andrew Perepechko338bf9a2008-04-28 02:14:31 -07002312 dm->dqb_itime = get_seconds() + dqi->dqi_igrace;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313 }
Jan Kara268157b2009-01-27 15:47:22 +01002314 if (dm->dqb_bhardlimit || dm->dqb_bsoftlimit || dm->dqb_ihardlimit ||
2315 dm->dqb_isoftlimit)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316 clear_bit(DQ_FAKE_B, &dquot->dq_flags);
2317 else
2318 set_bit(DQ_FAKE_B, &dquot->dq_flags);
2319 spin_unlock(&dq_data_lock);
2320 mark_dquot_dirty(dquot);
Andrew Perepechko338bf9a2008-04-28 02:14:31 -07002321
2322 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323}
2324
Jan Kara268157b2009-01-27 15:47:22 +01002325int vfs_set_dqblk(struct super_block *sb, int type, qid_t id,
2326 struct if_dqblk *di)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327{
2328 struct dquot *dquot;
Andrew Perepechko338bf9a2008-04-28 02:14:31 -07002329 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330
Jan Karaf55abc02008-08-20 17:50:32 +02002331 dquot = dqget(sb, id, type);
2332 if (!dquot) {
2333 rc = -ESRCH;
2334 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335 }
Andrew Perepechko338bf9a2008-04-28 02:14:31 -07002336 rc = do_set_dqblk(dquot, di);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337 dqput(dquot);
Jan Karaf55abc02008-08-20 17:50:32 +02002338out:
Andrew Perepechko338bf9a2008-04-28 02:14:31 -07002339 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340}
Mingming Cao08d03502009-01-14 16:19:32 +01002341EXPORT_SYMBOL(vfs_set_dqblk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342
2343/* Generic routine for getting common part of quota file information */
2344int vfs_get_dqinfo(struct super_block *sb, int type, struct if_dqinfo *ii)
2345{
2346 struct mem_dqinfo *mi;
2347
Ingo Molnard3be9152006-03-23 03:00:29 -08002348 mutex_lock(&sb_dqopt(sb)->dqonoff_mutex);
Jan Karaf55abc02008-08-20 17:50:32 +02002349 if (!sb_has_quota_active(sb, type)) {
Ingo Molnard3be9152006-03-23 03:00:29 -08002350 mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351 return -ESRCH;
2352 }
2353 mi = sb_dqopt(sb)->info + type;
2354 spin_lock(&dq_data_lock);
2355 ii->dqi_bgrace = mi->dqi_bgrace;
2356 ii->dqi_igrace = mi->dqi_igrace;
2357 ii->dqi_flags = mi->dqi_flags & DQF_MASK;
2358 ii->dqi_valid = IIF_ALL;
2359 spin_unlock(&dq_data_lock);
Ingo Molnard3be9152006-03-23 03:00:29 -08002360 mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361 return 0;
2362}
Mingming Cao08d03502009-01-14 16:19:32 +01002363EXPORT_SYMBOL(vfs_get_dqinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364
2365/* Generic routine for setting common part of quota file information */
2366int vfs_set_dqinfo(struct super_block *sb, int type, struct if_dqinfo *ii)
2367{
2368 struct mem_dqinfo *mi;
Jan Karaf55abc02008-08-20 17:50:32 +02002369 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002370
Ingo Molnard3be9152006-03-23 03:00:29 -08002371 mutex_lock(&sb_dqopt(sb)->dqonoff_mutex);
Jan Karaf55abc02008-08-20 17:50:32 +02002372 if (!sb_has_quota_active(sb, type)) {
2373 err = -ESRCH;
2374 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375 }
2376 mi = sb_dqopt(sb)->info + type;
2377 spin_lock(&dq_data_lock);
2378 if (ii->dqi_valid & IIF_BGRACE)
2379 mi->dqi_bgrace = ii->dqi_bgrace;
2380 if (ii->dqi_valid & IIF_IGRACE)
2381 mi->dqi_igrace = ii->dqi_igrace;
2382 if (ii->dqi_valid & IIF_FLAGS)
Jan Kara268157b2009-01-27 15:47:22 +01002383 mi->dqi_flags = (mi->dqi_flags & ~DQF_MASK) |
2384 (ii->dqi_flags & DQF_MASK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002385 spin_unlock(&dq_data_lock);
2386 mark_info_dirty(sb, type);
2387 /* Force write to disk */
2388 sb->dq_op->write_info(sb, type);
Jan Karaf55abc02008-08-20 17:50:32 +02002389out:
Ingo Molnard3be9152006-03-23 03:00:29 -08002390 mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
Jan Karaf55abc02008-08-20 17:50:32 +02002391 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392}
Mingming Cao08d03502009-01-14 16:19:32 +01002393EXPORT_SYMBOL(vfs_set_dqinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394
Alexey Dobriyan0d54b212009-09-21 17:01:09 -07002395const struct quotactl_ops vfs_quotactl_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002396 .quota_on = vfs_quota_on,
2397 .quota_off = vfs_quota_off,
2398 .quota_sync = vfs_quota_sync,
2399 .get_info = vfs_get_dqinfo,
2400 .set_info = vfs_set_dqinfo,
2401 .get_dqblk = vfs_get_dqblk,
2402 .set_dqblk = vfs_set_dqblk
2403};
2404
2405static ctl_table fs_dqstats_table[] = {
2406 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002407 .procname = "lookups",
2408 .data = &dqstats.lookups,
2409 .maxlen = sizeof(int),
2410 .mode = 0444,
Eric W. Biederman6d456112009-11-16 03:11:48 -08002411 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002412 },
2413 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002414 .procname = "drops",
2415 .data = &dqstats.drops,
2416 .maxlen = sizeof(int),
2417 .mode = 0444,
Eric W. Biederman6d456112009-11-16 03:11:48 -08002418 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002419 },
2420 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002421 .procname = "reads",
2422 .data = &dqstats.reads,
2423 .maxlen = sizeof(int),
2424 .mode = 0444,
Eric W. Biederman6d456112009-11-16 03:11:48 -08002425 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002426 },
2427 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002428 .procname = "writes",
2429 .data = &dqstats.writes,
2430 .maxlen = sizeof(int),
2431 .mode = 0444,
Eric W. Biederman6d456112009-11-16 03:11:48 -08002432 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002433 },
2434 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002435 .procname = "cache_hits",
2436 .data = &dqstats.cache_hits,
2437 .maxlen = sizeof(int),
2438 .mode = 0444,
Eric W. Biederman6d456112009-11-16 03:11:48 -08002439 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002440 },
2441 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442 .procname = "allocated_dquots",
2443 .data = &dqstats.allocated_dquots,
2444 .maxlen = sizeof(int),
2445 .mode = 0444,
Eric W. Biederman6d456112009-11-16 03:11:48 -08002446 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002447 },
2448 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002449 .procname = "free_dquots",
2450 .data = &dqstats.free_dquots,
2451 .maxlen = sizeof(int),
2452 .mode = 0444,
Eric W. Biederman6d456112009-11-16 03:11:48 -08002453 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454 },
2455 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002456 .procname = "syncs",
2457 .data = &dqstats.syncs,
2458 .maxlen = sizeof(int),
2459 .mode = 0444,
Eric W. Biederman6d456112009-11-16 03:11:48 -08002460 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002461 },
Jan Kara8e893462007-10-16 23:29:31 -07002462#ifdef CONFIG_PRINT_QUOTA_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07002463 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002464 .procname = "warnings",
2465 .data = &flag_print_warnings,
2466 .maxlen = sizeof(int),
2467 .mode = 0644,
Eric W. Biederman6d456112009-11-16 03:11:48 -08002468 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002469 },
Jan Kara8e893462007-10-16 23:29:31 -07002470#endif
Eric W. Biedermanab092032009-11-05 14:25:10 -08002471 { },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002472};
2473
2474static ctl_table fs_table[] = {
2475 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002476 .procname = "quota",
2477 .mode = 0555,
2478 .child = fs_dqstats_table,
2479 },
Eric W. Biedermanab092032009-11-05 14:25:10 -08002480 { },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002481};
2482
2483static ctl_table sys_table[] = {
2484 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002485 .procname = "fs",
2486 .mode = 0555,
2487 .child = fs_table,
2488 },
Eric W. Biedermanab092032009-11-05 14:25:10 -08002489 { },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002490};
2491
2492static int __init dquot_init(void)
2493{
2494 int i;
2495 unsigned long nr_hash, order;
2496
2497 printk(KERN_NOTICE "VFS: Disk quotas %s\n", __DQUOT_VERSION__);
2498
Eric W. Biederman0b4d4142007-02-14 00:34:09 -08002499 register_sysctl_table(sys_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002500
Paul Mundt20c2df82007-07-20 10:11:58 +09002501 dquot_cachep = kmem_cache_create("dquot",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002502 sizeof(struct dquot), sizeof(unsigned long) * 4,
Paul Jacksonfffb60f2006-03-24 03:16:06 -08002503 (SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT|
2504 SLAB_MEM_SPREAD|SLAB_PANIC),
Paul Mundt20c2df82007-07-20 10:11:58 +09002505 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002506
2507 order = 0;
2508 dquot_hash = (struct hlist_head *)__get_free_pages(GFP_ATOMIC, order);
2509 if (!dquot_hash)
2510 panic("Cannot create dquot hash table");
2511
2512 /* Find power-of-two hlist_heads which can fit into allocation */
2513 nr_hash = (1UL << order) * PAGE_SIZE / sizeof(struct hlist_head);
2514 dq_hash_bits = 0;
2515 do {
2516 dq_hash_bits++;
2517 } while (nr_hash >> dq_hash_bits);
2518 dq_hash_bits--;
2519
2520 nr_hash = 1UL << dq_hash_bits;
2521 dq_hash_mask = nr_hash - 1;
2522 for (i = 0; i < nr_hash; i++)
2523 INIT_HLIST_HEAD(dquot_hash + i);
2524
2525 printk("Dquot-cache hash table entries: %ld (order %ld, %ld bytes)\n",
2526 nr_hash, order, (PAGE_SIZE << order));
2527
Rusty Russell8e1f9362007-07-17 04:03:17 -07002528 register_shrinker(&dqcache_shrinker);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002529
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530 return 0;
2531}
2532module_init(dquot_init);