blob: 4a37b8e32b4e2e6162aad57a3025e59b0c0d0f75 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Implementation of the diskquota system for the LINUX operating system. QUOTA
3 * is implemented using the BSD system call interface as the means of
4 * communication with the user level. This file contains the generic routines
5 * called by the different filesystems on allocation of an inode or block.
6 * These routines take care of the administration needed to have a consistent
7 * diskquota tracking system. The ideas of both user and group quotas are based
8 * on the Melbourne quota system as used on BSD derived systems. The internal
9 * implementation is based on one of the several variants of the LINUX
10 * inode-subsystem with added complexity of the diskquota system.
11 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070012 * Author: Marco van Wieringen <mvw@planets.elm.net>
13 *
14 * Fixes: Dmitry Gorodchanin <pgmdsg@ibi.com>, 11 Feb 96
15 *
16 * Revised list management to avoid races
17 * -- Bill Hawes, <whawes@star.net>, 9/98
18 *
19 * Fixed races in dquot_transfer(), dqget() and dquot_alloc_...().
20 * As the consequence the locking was moved from dquot_decr_...(),
21 * dquot_incr_...() to calling functions.
22 * invalidate_dquots() now writes modified dquots.
23 * Serialized quota_off() and quota_on() for mount point.
24 * Fixed a few bugs in grow_dquots().
25 * Fixed deadlock in write_dquot() - we no longer account quotas on
26 * quota files
27 * remove_dquot_ref() moved to inode.c - it now traverses through inodes
28 * add_dquot_ref() restarts after blocking
29 * Added check for bogus uid and fixed check for group in quotactl.
30 * Jan Kara, <jack@suse.cz>, sponsored by SuSE CR, 10-11/99
31 *
32 * Used struct list_head instead of own list struct
33 * Invalidation of referenced dquots is no longer possible
34 * Improved free_dquots list management
35 * Quota and i_blocks are now updated in one place to avoid races
36 * Warnings are now delayed so we won't block in critical section
37 * Write updated not to require dquot lock
38 * Jan Kara, <jack@suse.cz>, 9/2000
39 *
40 * Added dynamic quota structure allocation
41 * Jan Kara <jack@suse.cz> 12/2000
42 *
43 * Rewritten quota interface. Implemented new quota format and
44 * formats registering.
45 * Jan Kara, <jack@suse.cz>, 2001,2002
46 *
47 * New SMP locking.
48 * Jan Kara, <jack@suse.cz>, 10/2002
49 *
50 * Added journalled quota support, fix lock inversion problems
51 * Jan Kara, <jack@suse.cz>, 2003,2004
52 *
53 * (C) Copyright 1994 - 1997 Marco van Wieringen
54 */
55
56#include <linux/errno.h>
57#include <linux/kernel.h>
58#include <linux/fs.h>
59#include <linux/mount.h>
60#include <linux/mm.h>
61#include <linux/time.h>
62#include <linux/types.h>
63#include <linux/string.h>
64#include <linux/fcntl.h>
65#include <linux/stat.h>
66#include <linux/tty.h>
67#include <linux/file.h>
68#include <linux/slab.h>
69#include <linux/sysctl.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070070#include <linux/init.h>
71#include <linux/module.h>
72#include <linux/proc_fs.h>
73#include <linux/security.h>
74#include <linux/kmod.h>
75#include <linux/namei.h>
76#include <linux/buffer_head.h>
Randy Dunlap16f7e0f2006-01-11 12:17:46 -080077#include <linux/capability.h>
Adrian Bunkbe586ba2005-11-07 00:59:35 -080078#include <linux/quotaops.h>
Christoph Hellwigfb58b732007-02-12 00:51:57 -080079#include <linux/writeback.h> /* for inode_lock, oddly enough.. */
Jan Kara8e893462007-10-16 23:29:31 -070080#ifdef CONFIG_QUOTA_NETLINK_INTERFACE
81#include <net/netlink.h>
82#include <net/genetlink.h>
83#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070084
85#include <asm/uaccess.h>
86
87#define __DQUOT_PARANOIA
88
89/*
Jan Karacc334122009-01-12 17:23:05 +010090 * There are three quota SMP locks. dq_list_lock protects all lists with quotas
91 * and quota formats, dqstats structure containing statistics about the lists
92 * dq_data_lock protects data from dq_dqb and also mem_dqinfo structures and
93 * also guards consistency of dquot->dq_dqb with inode->i_blocks, i_bytes.
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 * i_blocks and i_bytes updates itself are guarded by i_lock acquired directly
Jan Karacc334122009-01-12 17:23:05 +010095 * in inode_add_bytes() and inode_sub_bytes(). dq_state_lock protects
96 * modifications of quota state (on quotaon and quotaoff) and readers who care
97 * about latest values take it as well.
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 *
Jan Karacc334122009-01-12 17:23:05 +010099 * The spinlock ordering is hence: dq_data_lock > dq_list_lock > i_lock,
100 * dq_list_lock > dq_state_lock
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 *
102 * Note that some things (eg. sb pointer, type, id) doesn't change during
103 * the life of the dquot structure and so needn't to be protected by a lock
104 *
105 * Any operation working on dquots via inode pointers must hold dqptr_sem. If
106 * operation is just reading pointers from inode (or not using them at all) the
107 * read lock is enough. If pointers are altered function must hold write lock
108 * (these locking rules also apply for S_NOQUOTA flag in the inode - note that
Jan Karacc334122009-01-12 17:23:05 +0100109 * for altering the flag i_mutex is also needed).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 *
Ingo Molnard3be9152006-03-23 03:00:29 -0800111 * Each dquot has its dq_lock mutex. Locked dquots might not be referenced
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 * from inodes (dquot_alloc_space() and such don't check the dq_lock).
113 * Currently dquot is locked only when it is being read to memory (or space for
114 * it is being allocated) on the first dqget() and when it is being released on
115 * the last dqput(). The allocation and release oparations are serialized by
116 * the dq_lock and by checking the use count in dquot_release(). Write
117 * operations on dquots don't hold dq_lock as they copy data under dq_data_lock
118 * spinlock to internal buffers before writing.
119 *
120 * Lock ordering (including related VFS locks) is the following:
Ingo Molnard3be9152006-03-23 03:00:29 -0800121 * i_mutex > dqonoff_sem > journal_lock > dqptr_sem > dquot->dq_lock >
122 * dqio_mutex
Jan Karacc334122009-01-12 17:23:05 +0100123 * The lock ordering of dqptr_sem imposed by quota code is only dqonoff_sem >
124 * dqptr_sem. But filesystem has to count with the fact that functions such as
125 * dquot_alloc_space() acquire dqptr_sem and they usually have to be called
126 * from inside a transaction to keep filesystem consistency after a crash. Also
127 * filesystems usually want to do some IO on dquot from ->mark_dirty which is
128 * called with dqptr_sem held.
Ingo Molnard3be9152006-03-23 03:00:29 -0800129 * i_mutex on quota files is special (it's below dqio_mutex)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 */
131
Jan Karac5166102009-01-26 15:32:46 +0100132static __cacheline_aligned_in_smp DEFINE_SPINLOCK(dq_list_lock);
133static __cacheline_aligned_in_smp DEFINE_SPINLOCK(dq_state_lock);
134__cacheline_aligned_in_smp DEFINE_SPINLOCK(dq_data_lock);
Mingming Cao08d03502009-01-14 16:19:32 +0100135EXPORT_SYMBOL(dq_data_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136
137static char *quotatypes[] = INITQFNAMES;
138static struct quota_format_type *quota_formats; /* List of registered formats */
139static struct quota_module_name module_names[] = INIT_QUOTA_MODULE_NAMES;
140
141/* SLAB cache for dquot structures */
Christoph Lametere18b8902006-12-06 20:33:20 -0800142static struct kmem_cache *dquot_cachep;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143
144int register_quota_format(struct quota_format_type *fmt)
145{
146 spin_lock(&dq_list_lock);
147 fmt->qf_next = quota_formats;
148 quota_formats = fmt;
149 spin_unlock(&dq_list_lock);
150 return 0;
151}
Mingming Cao08d03502009-01-14 16:19:32 +0100152EXPORT_SYMBOL(register_quota_format);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153
154void unregister_quota_format(struct quota_format_type *fmt)
155{
156 struct quota_format_type **actqf;
157
158 spin_lock(&dq_list_lock);
159 for (actqf = &quota_formats; *actqf && *actqf != fmt; actqf = &(*actqf)->qf_next);
160 if (*actqf)
161 *actqf = (*actqf)->qf_next;
162 spin_unlock(&dq_list_lock);
163}
Mingming Cao08d03502009-01-14 16:19:32 +0100164EXPORT_SYMBOL(unregister_quota_format);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165
166static struct quota_format_type *find_quota_format(int id)
167{
168 struct quota_format_type *actqf;
169
170 spin_lock(&dq_list_lock);
171 for (actqf = quota_formats; actqf && actqf->qf_fmt_id != id; actqf = actqf->qf_next);
172 if (!actqf || !try_module_get(actqf->qf_owner)) {
173 int qm;
174
175 spin_unlock(&dq_list_lock);
176
177 for (qm = 0; module_names[qm].qm_fmt_id && module_names[qm].qm_fmt_id != id; qm++);
178 if (!module_names[qm].qm_fmt_id || request_module(module_names[qm].qm_mod_name))
179 return NULL;
180
181 spin_lock(&dq_list_lock);
182 for (actqf = quota_formats; actqf && actqf->qf_fmt_id != id; actqf = actqf->qf_next);
183 if (actqf && !try_module_get(actqf->qf_owner))
184 actqf = NULL;
185 }
186 spin_unlock(&dq_list_lock);
187 return actqf;
188}
189
190static void put_quota_format(struct quota_format_type *fmt)
191{
192 module_put(fmt->qf_owner);
193}
194
195/*
196 * Dquot List Management:
197 * The quota code uses three lists for dquot management: the inuse_list,
198 * free_dquots, and dquot_hash[] array. A single dquot structure may be
199 * on all three lists, depending on its current state.
200 *
201 * All dquots are placed to the end of inuse_list when first created, and this
202 * list is used for invalidate operation, which must look at every dquot.
203 *
204 * Unused dquots (dq_count == 0) are added to the free_dquots list when freed,
205 * and this list is searched whenever we need an available dquot. Dquots are
206 * removed from the list as soon as they are used again, and
207 * dqstats.free_dquots gives the number of dquots on the list. When
208 * dquot is invalidated it's completely released from memory.
209 *
210 * Dquots with a specific identity (device, type and id) are placed on
211 * one of the dquot_hash[] hash chains. The provides an efficient search
212 * mechanism to locate a specific dquot.
213 */
214
215static LIST_HEAD(inuse_list);
216static LIST_HEAD(free_dquots);
217static unsigned int dq_hash_bits, dq_hash_mask;
218static struct hlist_head *dquot_hash;
219
220struct dqstats dqstats;
Mingming Cao08d03502009-01-14 16:19:32 +0100221EXPORT_SYMBOL(dqstats);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223static inline unsigned int
224hashfn(const struct super_block *sb, unsigned int id, int type)
225{
226 unsigned long tmp;
227
228 tmp = (((unsigned long)sb>>L1_CACHE_SHIFT) ^ id) * (MAXQUOTAS - type);
229 return (tmp + (tmp >> dq_hash_bits)) & dq_hash_mask;
230}
231
232/*
233 * Following list functions expect dq_list_lock to be held
234 */
235static inline void insert_dquot_hash(struct dquot *dquot)
236{
237 struct hlist_head *head = dquot_hash + hashfn(dquot->dq_sb, dquot->dq_id, dquot->dq_type);
238 hlist_add_head(&dquot->dq_hash, head);
239}
240
241static inline void remove_dquot_hash(struct dquot *dquot)
242{
243 hlist_del_init(&dquot->dq_hash);
244}
245
Jan Kara7a2435d2009-01-27 01:47:11 +0100246static struct dquot *find_dquot(unsigned int hashent, struct super_block *sb,
247 unsigned int id, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248{
249 struct hlist_node *node;
250 struct dquot *dquot;
251
252 hlist_for_each (node, dquot_hash+hashent) {
253 dquot = hlist_entry(node, struct dquot, dq_hash);
254 if (dquot->dq_sb == sb && dquot->dq_id == id && dquot->dq_type == type)
255 return dquot;
256 }
Jan Karadd6f3c62009-01-26 16:01:43 +0100257 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258}
259
260/* Add a dquot to the tail of the free list */
261static inline void put_dquot_last(struct dquot *dquot)
262{
Akinobu Mita8e130592006-06-26 00:24:37 -0700263 list_add_tail(&dquot->dq_free, &free_dquots);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 dqstats.free_dquots++;
265}
266
267static inline void remove_free_dquot(struct dquot *dquot)
268{
269 if (list_empty(&dquot->dq_free))
270 return;
271 list_del_init(&dquot->dq_free);
272 dqstats.free_dquots--;
273}
274
275static inline void put_inuse(struct dquot *dquot)
276{
277 /* We add to the back of inuse list so we don't have to restart
278 * when traversing this list and we block */
Akinobu Mita8e130592006-06-26 00:24:37 -0700279 list_add_tail(&dquot->dq_inuse, &inuse_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 dqstats.allocated_dquots++;
281}
282
283static inline void remove_inuse(struct dquot *dquot)
284{
285 dqstats.allocated_dquots--;
286 list_del(&dquot->dq_inuse);
287}
288/*
289 * End of list functions needing dq_list_lock
290 */
291
292static void wait_on_dquot(struct dquot *dquot)
293{
Ingo Molnard3be9152006-03-23 03:00:29 -0800294 mutex_lock(&dquot->dq_lock);
295 mutex_unlock(&dquot->dq_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296}
297
Jan Kara03f6e922008-04-28 02:14:32 -0700298static inline int dquot_dirty(struct dquot *dquot)
299{
300 return test_bit(DQ_MOD_B, &dquot->dq_flags);
301}
302
303static inline int mark_dquot_dirty(struct dquot *dquot)
304{
305 return dquot->dq_sb->dq_op->mark_dirty(dquot);
306}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307
308int dquot_mark_dquot_dirty(struct dquot *dquot)
309{
310 spin_lock(&dq_list_lock);
311 if (!test_and_set_bit(DQ_MOD_B, &dquot->dq_flags))
312 list_add(&dquot->dq_dirty, &sb_dqopt(dquot->dq_sb)->
313 info[dquot->dq_type].dqi_dirty_list);
314 spin_unlock(&dq_list_lock);
315 return 0;
316}
Mingming Cao08d03502009-01-14 16:19:32 +0100317EXPORT_SYMBOL(dquot_mark_dquot_dirty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318
319/* This function needs dq_list_lock */
320static inline int clear_dquot_dirty(struct dquot *dquot)
321{
322 if (!test_and_clear_bit(DQ_MOD_B, &dquot->dq_flags))
323 return 0;
324 list_del_init(&dquot->dq_dirty);
325 return 1;
326}
327
328void mark_info_dirty(struct super_block *sb, int type)
329{
330 set_bit(DQF_INFO_DIRTY_B, &sb_dqopt(sb)->info[type].dqi_flags);
331}
332EXPORT_SYMBOL(mark_info_dirty);
333
334/*
335 * Read dquot from disk and alloc space for it
336 */
337
338int dquot_acquire(struct dquot *dquot)
339{
340 int ret = 0, ret2 = 0;
341 struct quota_info *dqopt = sb_dqopt(dquot->dq_sb);
342
Ingo Molnard3be9152006-03-23 03:00:29 -0800343 mutex_lock(&dquot->dq_lock);
344 mutex_lock(&dqopt->dqio_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 if (!test_bit(DQ_READ_B, &dquot->dq_flags))
346 ret = dqopt->ops[dquot->dq_type]->read_dqblk(dquot);
347 if (ret < 0)
348 goto out_iolock;
349 set_bit(DQ_READ_B, &dquot->dq_flags);
350 /* Instantiate dquot if needed */
351 if (!test_bit(DQ_ACTIVE_B, &dquot->dq_flags) && !dquot->dq_off) {
352 ret = dqopt->ops[dquot->dq_type]->commit_dqblk(dquot);
353 /* Write the info if needed */
354 if (info_dirty(&dqopt->info[dquot->dq_type]))
355 ret2 = dqopt->ops[dquot->dq_type]->write_file_info(dquot->dq_sb, dquot->dq_type);
356 if (ret < 0)
357 goto out_iolock;
358 if (ret2 < 0) {
359 ret = ret2;
360 goto out_iolock;
361 }
362 }
363 set_bit(DQ_ACTIVE_B, &dquot->dq_flags);
364out_iolock:
Ingo Molnard3be9152006-03-23 03:00:29 -0800365 mutex_unlock(&dqopt->dqio_mutex);
366 mutex_unlock(&dquot->dq_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 return ret;
368}
Mingming Cao08d03502009-01-14 16:19:32 +0100369EXPORT_SYMBOL(dquot_acquire);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370
371/*
372 * Write dquot to disk
373 */
374int dquot_commit(struct dquot *dquot)
375{
376 int ret = 0, ret2 = 0;
377 struct quota_info *dqopt = sb_dqopt(dquot->dq_sb);
378
Ingo Molnard3be9152006-03-23 03:00:29 -0800379 mutex_lock(&dqopt->dqio_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 spin_lock(&dq_list_lock);
381 if (!clear_dquot_dirty(dquot)) {
382 spin_unlock(&dq_list_lock);
383 goto out_sem;
384 }
385 spin_unlock(&dq_list_lock);
386 /* Inactive dquot can be only if there was error during read/init
387 * => we have better not writing it */
388 if (test_bit(DQ_ACTIVE_B, &dquot->dq_flags)) {
389 ret = dqopt->ops[dquot->dq_type]->commit_dqblk(dquot);
390 if (info_dirty(&dqopt->info[dquot->dq_type]))
391 ret2 = dqopt->ops[dquot->dq_type]->write_file_info(dquot->dq_sb, dquot->dq_type);
392 if (ret >= 0)
393 ret = ret2;
394 }
395out_sem:
Ingo Molnard3be9152006-03-23 03:00:29 -0800396 mutex_unlock(&dqopt->dqio_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 return ret;
398}
Mingming Cao08d03502009-01-14 16:19:32 +0100399EXPORT_SYMBOL(dquot_commit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400
401/*
402 * Release dquot
403 */
404int dquot_release(struct dquot *dquot)
405{
406 int ret = 0, ret2 = 0;
407 struct quota_info *dqopt = sb_dqopt(dquot->dq_sb);
408
Ingo Molnard3be9152006-03-23 03:00:29 -0800409 mutex_lock(&dquot->dq_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 /* Check whether we are not racing with some other dqget() */
411 if (atomic_read(&dquot->dq_count) > 1)
412 goto out_dqlock;
Ingo Molnard3be9152006-03-23 03:00:29 -0800413 mutex_lock(&dqopt->dqio_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 if (dqopt->ops[dquot->dq_type]->release_dqblk) {
415 ret = dqopt->ops[dquot->dq_type]->release_dqblk(dquot);
416 /* Write the info */
417 if (info_dirty(&dqopt->info[dquot->dq_type]))
418 ret2 = dqopt->ops[dquot->dq_type]->write_file_info(dquot->dq_sb, dquot->dq_type);
419 if (ret >= 0)
420 ret = ret2;
421 }
422 clear_bit(DQ_ACTIVE_B, &dquot->dq_flags);
Ingo Molnard3be9152006-03-23 03:00:29 -0800423 mutex_unlock(&dqopt->dqio_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424out_dqlock:
Ingo Molnard3be9152006-03-23 03:00:29 -0800425 mutex_unlock(&dquot->dq_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 return ret;
427}
Mingming Cao08d03502009-01-14 16:19:32 +0100428EXPORT_SYMBOL(dquot_release);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429
Jan Kara7d9056b2008-11-25 15:31:32 +0100430void dquot_destroy(struct dquot *dquot)
Jan Kara74f783a2008-08-19 14:51:22 +0200431{
432 kmem_cache_free(dquot_cachep, dquot);
433}
Jan Kara7d9056b2008-11-25 15:31:32 +0100434EXPORT_SYMBOL(dquot_destroy);
Jan Kara74f783a2008-08-19 14:51:22 +0200435
436static inline void do_destroy_dquot(struct dquot *dquot)
437{
438 dquot->dq_sb->dq_op->destroy_dquot(dquot);
439}
440
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441/* Invalidate all dquots on the list. Note that this function is called after
442 * quota is disabled and pointers from inodes removed so there cannot be new
Jan Kara6362e4d2006-03-23 03:00:17 -0800443 * quota users. There can still be some users of quotas due to inodes being
444 * just deleted or pruned by prune_icache() (those are not attached to any
Jan Karacc334122009-01-12 17:23:05 +0100445 * list) or parallel quotactl call. We have to wait for such users.
Jan Kara6362e4d2006-03-23 03:00:17 -0800446 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447static void invalidate_dquots(struct super_block *sb, int type)
448{
Domen Puncerc33ed272005-06-25 14:59:36 -0700449 struct dquot *dquot, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450
Jan Kara6362e4d2006-03-23 03:00:17 -0800451restart:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 spin_lock(&dq_list_lock);
Domen Puncerc33ed272005-06-25 14:59:36 -0700453 list_for_each_entry_safe(dquot, tmp, &inuse_list, dq_inuse) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 if (dquot->dq_sb != sb)
455 continue;
456 if (dquot->dq_type != type)
457 continue;
Jan Kara6362e4d2006-03-23 03:00:17 -0800458 /* Wait for dquot users */
459 if (atomic_read(&dquot->dq_count)) {
460 DEFINE_WAIT(wait);
461
462 atomic_inc(&dquot->dq_count);
463 prepare_to_wait(&dquot->dq_wait_unused, &wait,
464 TASK_UNINTERRUPTIBLE);
465 spin_unlock(&dq_list_lock);
466 /* Once dqput() wakes us up, we know it's time to free
467 * the dquot.
468 * IMPORTANT: we rely on the fact that there is always
469 * at most one process waiting for dquot to free.
470 * Otherwise dq_count would be > 1 and we would never
471 * wake up.
472 */
473 if (atomic_read(&dquot->dq_count) > 1)
474 schedule();
475 finish_wait(&dquot->dq_wait_unused, &wait);
476 dqput(dquot);
477 /* At this moment dquot() need not exist (it could be
478 * reclaimed by prune_dqcache(). Hence we must
479 * restart. */
480 goto restart;
481 }
482 /*
483 * Quota now has no users and it has been written on last
484 * dqput()
485 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 remove_dquot_hash(dquot);
487 remove_free_dquot(dquot);
488 remove_inuse(dquot);
Jan Kara74f783a2008-08-19 14:51:22 +0200489 do_destroy_dquot(dquot);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 }
491 spin_unlock(&dq_list_lock);
492}
493
Jan Kara12c77522008-10-20 17:05:00 +0200494/* Call callback for every active dquot on given filesystem */
495int dquot_scan_active(struct super_block *sb,
496 int (*fn)(struct dquot *dquot, unsigned long priv),
497 unsigned long priv)
498{
499 struct dquot *dquot, *old_dquot = NULL;
500 int ret = 0;
501
502 mutex_lock(&sb_dqopt(sb)->dqonoff_mutex);
503 spin_lock(&dq_list_lock);
504 list_for_each_entry(dquot, &inuse_list, dq_inuse) {
505 if (!test_bit(DQ_ACTIVE_B, &dquot->dq_flags))
506 continue;
507 if (dquot->dq_sb != sb)
508 continue;
509 /* Now we have active dquot so we can just increase use count */
510 atomic_inc(&dquot->dq_count);
511 dqstats.lookups++;
512 spin_unlock(&dq_list_lock);
513 dqput(old_dquot);
514 old_dquot = dquot;
515 ret = fn(dquot, priv);
516 if (ret < 0)
517 goto out;
518 spin_lock(&dq_list_lock);
519 /* We are safe to continue now because our dquot could not
520 * be moved out of the inuse list while we hold the reference */
521 }
522 spin_unlock(&dq_list_lock);
523out:
524 dqput(old_dquot);
525 mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
526 return ret;
527}
Mingming Cao08d03502009-01-14 16:19:32 +0100528EXPORT_SYMBOL(dquot_scan_active);
Jan Kara12c77522008-10-20 17:05:00 +0200529
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530int vfs_quota_sync(struct super_block *sb, int type)
531{
532 struct list_head *dirty;
533 struct dquot *dquot;
534 struct quota_info *dqopt = sb_dqopt(sb);
535 int cnt;
536
Ingo Molnard3be9152006-03-23 03:00:29 -0800537 mutex_lock(&dqopt->dqonoff_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
539 if (type != -1 && cnt != type)
540 continue;
Jan Karaf55abc02008-08-20 17:50:32 +0200541 if (!sb_has_quota_active(sb, cnt))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 continue;
543 spin_lock(&dq_list_lock);
544 dirty = &dqopt->info[cnt].dqi_dirty_list;
545 while (!list_empty(dirty)) {
Pavel Emelianovb5e61812007-05-08 00:30:19 -0700546 dquot = list_first_entry(dirty, struct dquot, dq_dirty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 /* Dirty and inactive can be only bad dquot... */
548 if (!test_bit(DQ_ACTIVE_B, &dquot->dq_flags)) {
549 clear_dquot_dirty(dquot);
550 continue;
551 }
552 /* Now we have active dquot from which someone is
553 * holding reference so we can safely just increase
554 * use count */
555 atomic_inc(&dquot->dq_count);
556 dqstats.lookups++;
557 spin_unlock(&dq_list_lock);
558 sb->dq_op->write_dquot(dquot);
559 dqput(dquot);
560 spin_lock(&dq_list_lock);
561 }
562 spin_unlock(&dq_list_lock);
563 }
564
565 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
Jan Karaf55abc02008-08-20 17:50:32 +0200566 if ((cnt == type || type == -1) && sb_has_quota_active(sb, cnt)
567 && info_dirty(&dqopt->info[cnt]))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 sb->dq_op->write_info(sb, cnt);
569 spin_lock(&dq_list_lock);
570 dqstats.syncs++;
571 spin_unlock(&dq_list_lock);
Ingo Molnard3be9152006-03-23 03:00:29 -0800572 mutex_unlock(&dqopt->dqonoff_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573
574 return 0;
575}
Mingming Cao08d03502009-01-14 16:19:32 +0100576EXPORT_SYMBOL(vfs_quota_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577
578/* Free unused dquots from cache */
579static void prune_dqcache(int count)
580{
581 struct list_head *head;
582 struct dquot *dquot;
583
584 head = free_dquots.prev;
585 while (head != &free_dquots && count) {
586 dquot = list_entry(head, struct dquot, dq_free);
587 remove_dquot_hash(dquot);
588 remove_free_dquot(dquot);
589 remove_inuse(dquot);
Jan Kara74f783a2008-08-19 14:51:22 +0200590 do_destroy_dquot(dquot);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 count--;
592 head = free_dquots.prev;
593 }
594}
595
596/*
597 * This is called from kswapd when we think we need some
598 * more memory
599 */
600
Al Viro27496a82005-10-21 03:20:48 -0400601static int shrink_dqcache_memory(int nr, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602{
603 if (nr) {
604 spin_lock(&dq_list_lock);
605 prune_dqcache(nr);
606 spin_unlock(&dq_list_lock);
607 }
608 return (dqstats.free_dquots / 100) * sysctl_vfs_cache_pressure;
609}
610
Rusty Russell8e1f9362007-07-17 04:03:17 -0700611static struct shrinker dqcache_shrinker = {
612 .shrink = shrink_dqcache_memory,
613 .seeks = DEFAULT_SEEKS,
614};
615
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616/*
617 * Put reference to dquot
618 * NOTE: If you change this function please check whether dqput_blocks() works right...
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 */
Jan Kara3d9ea252008-10-10 16:12:23 +0200620void dqput(struct dquot *dquot)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621{
Jan Karab48d3802008-07-25 01:46:49 -0700622 int ret;
623
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 if (!dquot)
625 return;
626#ifdef __DQUOT_PARANOIA
627 if (!atomic_read(&dquot->dq_count)) {
628 printk("VFS: dqput: trying to free free dquot\n");
629 printk("VFS: device %s, dquot of %s %d\n",
630 dquot->dq_sb->s_id,
631 quotatypes[dquot->dq_type],
632 dquot->dq_id);
633 BUG();
634 }
635#endif
636
637 spin_lock(&dq_list_lock);
638 dqstats.drops++;
639 spin_unlock(&dq_list_lock);
640we_slept:
641 spin_lock(&dq_list_lock);
642 if (atomic_read(&dquot->dq_count) > 1) {
643 /* We have more than one user... nothing to do */
644 atomic_dec(&dquot->dq_count);
Jan Kara6362e4d2006-03-23 03:00:17 -0800645 /* Releasing dquot during quotaoff phase? */
Jan Karaf55abc02008-08-20 17:50:32 +0200646 if (!sb_has_quota_active(dquot->dq_sb, dquot->dq_type) &&
Jan Kara6362e4d2006-03-23 03:00:17 -0800647 atomic_read(&dquot->dq_count) == 1)
648 wake_up(&dquot->dq_wait_unused);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 spin_unlock(&dq_list_lock);
650 return;
651 }
652 /* Need to release dquot? */
653 if (test_bit(DQ_ACTIVE_B, &dquot->dq_flags) && dquot_dirty(dquot)) {
654 spin_unlock(&dq_list_lock);
655 /* Commit dquot before releasing */
Jan Karab48d3802008-07-25 01:46:49 -0700656 ret = dquot->dq_sb->dq_op->write_dquot(dquot);
657 if (ret < 0) {
658 printk(KERN_ERR "VFS: cannot write quota structure on "
659 "device %s (error %d). Quota may get out of "
660 "sync!\n", dquot->dq_sb->s_id, ret);
661 /*
662 * We clear dirty bit anyway, so that we avoid
663 * infinite loop here
664 */
665 spin_lock(&dq_list_lock);
666 clear_dquot_dirty(dquot);
667 spin_unlock(&dq_list_lock);
668 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 goto we_slept;
670 }
671 /* Clear flag in case dquot was inactive (something bad happened) */
672 clear_dquot_dirty(dquot);
673 if (test_bit(DQ_ACTIVE_B, &dquot->dq_flags)) {
674 spin_unlock(&dq_list_lock);
675 dquot->dq_sb->dq_op->release_dquot(dquot);
676 goto we_slept;
677 }
678 atomic_dec(&dquot->dq_count);
679#ifdef __DQUOT_PARANOIA
680 /* sanity check */
Eric Sesterhenn8abf6a42006-04-02 13:36:13 +0200681 BUG_ON(!list_empty(&dquot->dq_free));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682#endif
683 put_dquot_last(dquot);
684 spin_unlock(&dq_list_lock);
685}
Mingming Cao08d03502009-01-14 16:19:32 +0100686EXPORT_SYMBOL(dqput);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687
Jan Kara7d9056b2008-11-25 15:31:32 +0100688struct dquot *dquot_alloc(struct super_block *sb, int type)
Jan Kara74f783a2008-08-19 14:51:22 +0200689{
690 return kmem_cache_zalloc(dquot_cachep, GFP_NOFS);
691}
Jan Kara7d9056b2008-11-25 15:31:32 +0100692EXPORT_SYMBOL(dquot_alloc);
Jan Kara74f783a2008-08-19 14:51:22 +0200693
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694static struct dquot *get_empty_dquot(struct super_block *sb, int type)
695{
696 struct dquot *dquot;
697
Jan Kara74f783a2008-08-19 14:51:22 +0200698 dquot = sb->dq_op->alloc_dquot(sb, type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 if(!dquot)
Jan Karadd6f3c62009-01-26 16:01:43 +0100700 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701
Ingo Molnard3be9152006-03-23 03:00:29 -0800702 mutex_init(&dquot->dq_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 INIT_LIST_HEAD(&dquot->dq_free);
704 INIT_LIST_HEAD(&dquot->dq_inuse);
705 INIT_HLIST_NODE(&dquot->dq_hash);
706 INIT_LIST_HEAD(&dquot->dq_dirty);
Jan Kara6362e4d2006-03-23 03:00:17 -0800707 init_waitqueue_head(&dquot->dq_wait_unused);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 dquot->dq_sb = sb;
709 dquot->dq_type = type;
710 atomic_set(&dquot->dq_count, 1);
711
712 return dquot;
713}
714
715/*
716 * Get reference to dquot
Jan Karacc334122009-01-12 17:23:05 +0100717 *
718 * Locking is slightly tricky here. We are guarded from parallel quotaoff()
719 * destroying our dquot by:
720 * a) checking for quota flags under dq_list_lock and
721 * b) getting a reference to dquot before we release dq_list_lock
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 */
Jan Kara3d9ea252008-10-10 16:12:23 +0200723struct dquot *dqget(struct super_block *sb, unsigned int id, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724{
725 unsigned int hashent = hashfn(sb, id, type);
Jan Karadd6f3c62009-01-26 16:01:43 +0100726 struct dquot *dquot = NULL, *empty = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727
Jan Karaf55abc02008-08-20 17:50:32 +0200728 if (!sb_has_quota_active(sb, type))
Jan Karadd6f3c62009-01-26 16:01:43 +0100729 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730we_slept:
731 spin_lock(&dq_list_lock);
Jan Karacc334122009-01-12 17:23:05 +0100732 spin_lock(&dq_state_lock);
733 if (!sb_has_quota_active(sb, type)) {
734 spin_unlock(&dq_state_lock);
735 spin_unlock(&dq_list_lock);
736 goto out;
737 }
738 spin_unlock(&dq_state_lock);
739
Jan Karadd6f3c62009-01-26 16:01:43 +0100740 dquot = find_dquot(hashent, sb, id, type);
741 if (!dquot) {
742 if (!empty) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 spin_unlock(&dq_list_lock);
Jan Karadd6f3c62009-01-26 16:01:43 +0100744 empty = get_empty_dquot(sb, type);
745 if (!empty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 schedule(); /* Try to wait for a moment... */
747 goto we_slept;
748 }
749 dquot = empty;
Jan Karadd6f3c62009-01-26 16:01:43 +0100750 empty = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 dquot->dq_id = id;
752 /* all dquots go on the inuse_list */
753 put_inuse(dquot);
754 /* hash it first so it can be found */
755 insert_dquot_hash(dquot);
756 dqstats.lookups++;
757 spin_unlock(&dq_list_lock);
758 } else {
759 if (!atomic_read(&dquot->dq_count))
760 remove_free_dquot(dquot);
761 atomic_inc(&dquot->dq_count);
762 dqstats.cache_hits++;
763 dqstats.lookups++;
764 spin_unlock(&dq_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 }
766 /* Wait for dq_lock - after this we know that either dquot_release() is already
767 * finished or it will be canceled due to dq_count > 1 test */
768 wait_on_dquot(dquot);
769 /* Read the dquot and instantiate it (everything done only if needed) */
770 if (!test_bit(DQ_ACTIVE_B, &dquot->dq_flags) && sb->dq_op->acquire_dquot(dquot) < 0) {
771 dqput(dquot);
Jan Karadd6f3c62009-01-26 16:01:43 +0100772 dquot = NULL;
Jan Karacc334122009-01-12 17:23:05 +0100773 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 }
775#ifdef __DQUOT_PARANOIA
Eric Sesterhenn8abf6a42006-04-02 13:36:13 +0200776 BUG_ON(!dquot->dq_sb); /* Has somebody invalidated entry under us? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777#endif
Jan Karacc334122009-01-12 17:23:05 +0100778out:
779 if (empty)
780 do_destroy_dquot(empty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781
782 return dquot;
783}
Mingming Cao08d03502009-01-14 16:19:32 +0100784EXPORT_SYMBOL(dqget);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785
786static int dqinit_needed(struct inode *inode, int type)
787{
788 int cnt;
789
790 if (IS_NOQUOTA(inode))
791 return 0;
792 if (type != -1)
Jan Karadd6f3c62009-01-26 16:01:43 +0100793 return !inode->i_dquot[type];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
Jan Karadd6f3c62009-01-26 16:01:43 +0100795 if (!inode->i_dquot[cnt])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 return 1;
797 return 0;
798}
799
Ingo Molnard3be9152006-03-23 03:00:29 -0800800/* This routine is guarded by dqonoff_mutex mutex */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801static void add_dquot_ref(struct super_block *sb, int type)
802{
Jan Kara941d2382008-02-06 01:37:36 -0800803 struct inode *inode, *old_inode = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804
Christoph Hellwigd003fb72007-02-12 00:51:58 -0800805 spin_lock(&inode_lock);
806 list_for_each_entry(inode, &sb->s_inodes, i_sb_list) {
807 if (!atomic_read(&inode->i_writecount))
808 continue;
809 if (!dqinit_needed(inode, type))
810 continue;
811 if (inode->i_state & (I_FREEING|I_WILL_FREE))
812 continue;
813
814 __iget(inode);
815 spin_unlock(&inode_lock);
816
Jan Kara941d2382008-02-06 01:37:36 -0800817 iput(old_inode);
Christoph Hellwigd003fb72007-02-12 00:51:58 -0800818 sb->dq_op->initialize(inode, type);
Jan Kara941d2382008-02-06 01:37:36 -0800819 /* We hold a reference to 'inode' so it couldn't have been
820 * removed from s_inodes list while we dropped the inode_lock.
821 * We cannot iput the inode now as we can be holding the last
822 * reference and we cannot iput it under inode_lock. So we
823 * keep the reference and iput it later. */
824 old_inode = inode;
825 spin_lock(&inode_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 }
Christoph Hellwigd003fb72007-02-12 00:51:58 -0800827 spin_unlock(&inode_lock);
Jan Kara941d2382008-02-06 01:37:36 -0800828 iput(old_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829}
830
831/* Return 0 if dqput() won't block (note that 1 doesn't necessarily mean blocking) */
832static inline int dqput_blocks(struct dquot *dquot)
833{
834 if (atomic_read(&dquot->dq_count) <= 1)
835 return 1;
836 return 0;
837}
838
839/* Remove references to dquots from inode - add dquot to list for freeing if needed */
840/* We can't race with anybody because we hold dqptr_sem for writing... */
Adrian Bunke5f00f42007-05-08 00:27:22 -0700841static int remove_inode_dquot_ref(struct inode *inode, int type,
842 struct list_head *tofree_head)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843{
844 struct dquot *dquot = inode->i_dquot[type];
845
Jan Karadd6f3c62009-01-26 16:01:43 +0100846 inode->i_dquot[type] = NULL;
847 if (dquot) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 if (dqput_blocks(dquot)) {
849#ifdef __DQUOT_PARANOIA
850 if (atomic_read(&dquot->dq_count) != 1)
851 printk(KERN_WARNING "VFS: Adding dquot with dq_count %d to dispose list.\n", atomic_read(&dquot->dq_count));
852#endif
853 spin_lock(&dq_list_lock);
854 list_add(&dquot->dq_free, tofree_head); /* As dquot must have currently users it can't be on the free list... */
855 spin_unlock(&dq_list_lock);
856 return 1;
857 }
858 else
859 dqput(dquot); /* We have guaranteed we won't block */
860 }
861 return 0;
862}
863
864/* Free list of dquots - called from inode.c */
865/* dquots are removed from inodes, no new references can be got so we are the only ones holding reference */
866static void put_dquot_list(struct list_head *tofree_head)
867{
868 struct list_head *act_head;
869 struct dquot *dquot;
870
871 act_head = tofree_head->next;
872 /* So now we have dquots on the list... Just free them */
873 while (act_head != tofree_head) {
874 dquot = list_entry(act_head, struct dquot, dq_free);
875 act_head = act_head->next;
876 list_del_init(&dquot->dq_free); /* Remove dquot from the list so we won't have problems... */
877 dqput(dquot);
878 }
879}
880
Christoph Hellwigfb58b732007-02-12 00:51:57 -0800881static void remove_dquot_ref(struct super_block *sb, int type,
882 struct list_head *tofree_head)
883{
884 struct inode *inode;
885
886 spin_lock(&inode_lock);
887 list_for_each_entry(inode, &sb->s_inodes, i_sb_list) {
888 if (!IS_NOQUOTA(inode))
889 remove_inode_dquot_ref(inode, type, tofree_head);
890 }
891 spin_unlock(&inode_lock);
892}
893
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894/* Gather all references from inodes and drop them */
895static void drop_dquot_ref(struct super_block *sb, int type)
896{
897 LIST_HEAD(tofree_head);
898
Christoph Hellwigfb58b732007-02-12 00:51:57 -0800899 if (sb->dq_op) {
900 down_write(&sb_dqopt(sb)->dqptr_sem);
901 remove_dquot_ref(sb, type, &tofree_head);
902 up_write(&sb_dqopt(sb)->dqptr_sem);
903 put_dquot_list(&tofree_head);
904 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905}
906
Jan Kara12095462008-08-20 14:45:12 +0200907static inline void dquot_incr_inodes(struct dquot *dquot, qsize_t number)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908{
909 dquot->dq_dqb.dqb_curinodes += number;
910}
911
912static inline void dquot_incr_space(struct dquot *dquot, qsize_t number)
913{
914 dquot->dq_dqb.dqb_curspace += number;
915}
916
Mingming Caof18df222009-01-13 16:43:09 +0100917static inline void dquot_resv_space(struct dquot *dquot, qsize_t number)
918{
919 dquot->dq_dqb.dqb_rsvspace += number;
920}
921
Mingming Cao740d9dc2009-01-13 16:43:14 +0100922/*
923 * Claim reserved quota space
924 */
925static void dquot_claim_reserved_space(struct dquot *dquot,
926 qsize_t number)
927{
928 WARN_ON(dquot->dq_dqb.dqb_rsvspace < number);
929 dquot->dq_dqb.dqb_curspace += number;
930 dquot->dq_dqb.dqb_rsvspace -= number;
931}
932
933static inline
934void dquot_free_reserved_space(struct dquot *dquot, qsize_t number)
935{
936 dquot->dq_dqb.dqb_rsvspace -= number;
937}
938
Jan Kara7a2435d2009-01-27 01:47:11 +0100939static void dquot_decr_inodes(struct dquot *dquot, qsize_t number)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940{
Jan Karadb49d2d2008-10-01 18:21:39 +0200941 if (sb_dqopt(dquot->dq_sb)->flags & DQUOT_NEGATIVE_USAGE ||
942 dquot->dq_dqb.dqb_curinodes >= number)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 dquot->dq_dqb.dqb_curinodes -= number;
944 else
945 dquot->dq_dqb.dqb_curinodes = 0;
946 if (dquot->dq_dqb.dqb_curinodes <= dquot->dq_dqb.dqb_isoftlimit)
947 dquot->dq_dqb.dqb_itime = (time_t) 0;
948 clear_bit(DQ_INODES_B, &dquot->dq_flags);
949}
950
Jan Kara7a2435d2009-01-27 01:47:11 +0100951static void dquot_decr_space(struct dquot *dquot, qsize_t number)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952{
Jan Karadb49d2d2008-10-01 18:21:39 +0200953 if (sb_dqopt(dquot->dq_sb)->flags & DQUOT_NEGATIVE_USAGE ||
954 dquot->dq_dqb.dqb_curspace >= number)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 dquot->dq_dqb.dqb_curspace -= number;
956 else
957 dquot->dq_dqb.dqb_curspace = 0;
Jan Kara12095462008-08-20 14:45:12 +0200958 if (dquot->dq_dqb.dqb_curspace <= dquot->dq_dqb.dqb_bsoftlimit)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 dquot->dq_dqb.dqb_btime = (time_t) 0;
960 clear_bit(DQ_BLKS_B, &dquot->dq_flags);
961}
962
Jan Karac5254602007-12-22 14:03:25 -0800963static int warning_issued(struct dquot *dquot, const int warntype)
964{
965 int flag = (warntype == QUOTA_NL_BHARDWARN ||
966 warntype == QUOTA_NL_BSOFTLONGWARN) ? DQ_BLKS_B :
967 ((warntype == QUOTA_NL_IHARDWARN ||
968 warntype == QUOTA_NL_ISOFTLONGWARN) ? DQ_INODES_B : 0);
969
970 if (!flag)
971 return 0;
972 return test_and_set_bit(flag, &dquot->dq_flags);
973}
974
Jan Kara8e893462007-10-16 23:29:31 -0700975#ifdef CONFIG_PRINT_QUOTA_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976static int flag_print_warnings = 1;
977
Jan Kara7a2435d2009-01-27 01:47:11 +0100978static int need_print_warning(struct dquot *dquot)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979{
980 if (!flag_print_warnings)
981 return 0;
982
983 switch (dquot->dq_type) {
984 case USRQUOTA:
David Howellsda9592e2008-11-14 10:39:05 +1100985 return current_fsuid() == dquot->dq_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 case GRPQUOTA:
987 return in_group_p(dquot->dq_id);
988 }
989 return 0;
990}
991
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992/* Print warning to user which exceeded quota */
Jan Karac5254602007-12-22 14:03:25 -0800993static void print_warning(struct dquot *dquot, const int warntype)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994{
995 char *msg = NULL;
Peter Zijlstra24ec8392006-12-08 02:36:04 -0800996 struct tty_struct *tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997
Jan Kara657d3bf2008-07-25 01:46:52 -0700998 if (warntype == QUOTA_NL_IHARDBELOW ||
999 warntype == QUOTA_NL_ISOFTBELOW ||
1000 warntype == QUOTA_NL_BHARDBELOW ||
1001 warntype == QUOTA_NL_BSOFTBELOW || !need_print_warning(dquot))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 return;
1003
Peter Zijlstra24ec8392006-12-08 02:36:04 -08001004 tty = get_current_tty();
1005 if (!tty)
Alan Cox452a00d2008-10-13 10:39:13 +01001006 return;
Peter Zijlstra24ec8392006-12-08 02:36:04 -08001007 tty_write_message(tty, dquot->dq_sb->s_id);
Jan Kara8e893462007-10-16 23:29:31 -07001008 if (warntype == QUOTA_NL_ISOFTWARN || warntype == QUOTA_NL_BSOFTWARN)
Peter Zijlstra24ec8392006-12-08 02:36:04 -08001009 tty_write_message(tty, ": warning, ");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 else
Peter Zijlstra24ec8392006-12-08 02:36:04 -08001011 tty_write_message(tty, ": write failed, ");
1012 tty_write_message(tty, quotatypes[dquot->dq_type]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 switch (warntype) {
Jan Kara8e893462007-10-16 23:29:31 -07001014 case QUOTA_NL_IHARDWARN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 msg = " file limit reached.\r\n";
1016 break;
Jan Kara8e893462007-10-16 23:29:31 -07001017 case QUOTA_NL_ISOFTLONGWARN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 msg = " file quota exceeded too long.\r\n";
1019 break;
Jan Kara8e893462007-10-16 23:29:31 -07001020 case QUOTA_NL_ISOFTWARN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 msg = " file quota exceeded.\r\n";
1022 break;
Jan Kara8e893462007-10-16 23:29:31 -07001023 case QUOTA_NL_BHARDWARN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 msg = " block limit reached.\r\n";
1025 break;
Jan Kara8e893462007-10-16 23:29:31 -07001026 case QUOTA_NL_BSOFTLONGWARN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 msg = " block quota exceeded too long.\r\n";
1028 break;
Jan Kara8e893462007-10-16 23:29:31 -07001029 case QUOTA_NL_BSOFTWARN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 msg = " block quota exceeded.\r\n";
1031 break;
1032 }
Peter Zijlstra24ec8392006-12-08 02:36:04 -08001033 tty_write_message(tty, msg);
Alan Cox452a00d2008-10-13 10:39:13 +01001034 tty_kref_put(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035}
Jan Kara8e893462007-10-16 23:29:31 -07001036#endif
1037
1038#ifdef CONFIG_QUOTA_NETLINK_INTERFACE
1039
Jan Kara8e893462007-10-16 23:29:31 -07001040/* Netlink family structure for quota */
1041static struct genl_family quota_genl_family = {
1042 .id = GENL_ID_GENERATE,
1043 .hdrsize = 0,
1044 .name = "VFS_DQUOT",
1045 .version = 1,
1046 .maxattr = QUOTA_NL_A_MAX,
1047};
1048
1049/* Send warning to userspace about user which exceeded quota */
1050static void send_warning(const struct dquot *dquot, const char warntype)
1051{
1052 static atomic_t seq;
1053 struct sk_buff *skb;
1054 void *msg_head;
1055 int ret;
Jan Kara22dd4832007-12-22 14:03:25 -08001056 int msg_size = 4 * nla_total_size(sizeof(u32)) +
1057 2 * nla_total_size(sizeof(u64));
Jan Kara8e893462007-10-16 23:29:31 -07001058
1059 /* We have to allocate using GFP_NOFS as we are called from a
1060 * filesystem performing write and thus further recursion into
1061 * the fs to free some data could cause deadlocks. */
Jan Kara22dd4832007-12-22 14:03:25 -08001062 skb = genlmsg_new(msg_size, GFP_NOFS);
Jan Kara8e893462007-10-16 23:29:31 -07001063 if (!skb) {
1064 printk(KERN_ERR
1065 "VFS: Not enough memory to send quota warning.\n");
1066 return;
1067 }
1068 msg_head = genlmsg_put(skb, 0, atomic_add_return(1, &seq),
1069 &quota_genl_family, 0, QUOTA_NL_C_WARNING);
1070 if (!msg_head) {
1071 printk(KERN_ERR
1072 "VFS: Cannot store netlink header in quota warning.\n");
1073 goto err_out;
1074 }
1075 ret = nla_put_u32(skb, QUOTA_NL_A_QTYPE, dquot->dq_type);
1076 if (ret)
1077 goto attr_err_out;
1078 ret = nla_put_u64(skb, QUOTA_NL_A_EXCESS_ID, dquot->dq_id);
1079 if (ret)
1080 goto attr_err_out;
1081 ret = nla_put_u32(skb, QUOTA_NL_A_WARNING, warntype);
1082 if (ret)
1083 goto attr_err_out;
1084 ret = nla_put_u32(skb, QUOTA_NL_A_DEV_MAJOR,
1085 MAJOR(dquot->dq_sb->s_dev));
1086 if (ret)
1087 goto attr_err_out;
1088 ret = nla_put_u32(skb, QUOTA_NL_A_DEV_MINOR,
1089 MINOR(dquot->dq_sb->s_dev));
1090 if (ret)
1091 goto attr_err_out;
David Howellsda9592e2008-11-14 10:39:05 +11001092 ret = nla_put_u64(skb, QUOTA_NL_A_CAUSED_ID, current_uid());
Jan Kara8e893462007-10-16 23:29:31 -07001093 if (ret)
1094 goto attr_err_out;
1095 genlmsg_end(skb, msg_head);
1096
1097 ret = genlmsg_multicast(skb, 0, quota_genl_family.id, GFP_NOFS);
1098 if (ret < 0 && ret != -ESRCH)
1099 printk(KERN_ERR
1100 "VFS: Failed to send notification message: %d\n", ret);
1101 return;
1102attr_err_out:
Jan Kara22dd4832007-12-22 14:03:25 -08001103 printk(KERN_ERR "VFS: Not enough space to compose quota message!\n");
Jan Kara8e893462007-10-16 23:29:31 -07001104err_out:
1105 kfree_skb(skb);
1106}
1107#endif
Mingming Caof18df222009-01-13 16:43:09 +01001108/*
1109 * Write warnings to the console and send warning messages over netlink.
1110 *
1111 * Note that this function can sleep.
1112 */
Jan Kara7a2435d2009-01-27 01:47:11 +01001113static void flush_warnings(struct dquot *const *dquots, char *warntype)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114{
1115 int i;
1116
1117 for (i = 0; i < MAXQUOTAS; i++)
Jan Karadd6f3c62009-01-26 16:01:43 +01001118 if (dquots[i] && warntype[i] != QUOTA_NL_NOWARN &&
Jan Karac5254602007-12-22 14:03:25 -08001119 !warning_issued(dquots[i], warntype[i])) {
Jan Kara8e893462007-10-16 23:29:31 -07001120#ifdef CONFIG_PRINT_QUOTA_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 print_warning(dquots[i], warntype[i]);
Jan Kara8e893462007-10-16 23:29:31 -07001122#endif
1123#ifdef CONFIG_QUOTA_NETLINK_INTERFACE
1124 send_warning(dquots[i], warntype[i]);
1125#endif
1126 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127}
1128
Jan Kara7a2435d2009-01-27 01:47:11 +01001129static int ignore_hardlimit(struct dquot *dquot)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130{
1131 struct mem_dqinfo *info = &sb_dqopt(dquot->dq_sb)->info[dquot->dq_type];
1132
1133 return capable(CAP_SYS_RESOURCE) &&
1134 (info->dqi_format->qf_fmt_id != QFMT_VFS_OLD || !(info->dqi_flags & V1_DQF_RSQUASH));
1135}
1136
1137/* needs dq_data_lock */
Jan Kara12095462008-08-20 14:45:12 +02001138static int check_idq(struct dquot *dquot, qsize_t inodes, char *warntype)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139{
Jan Kara8e893462007-10-16 23:29:31 -07001140 *warntype = QUOTA_NL_NOWARN;
Jan Karaf55abc02008-08-20 17:50:32 +02001141 if (!sb_has_quota_limits_enabled(dquot->dq_sb, dquot->dq_type) ||
1142 test_bit(DQ_FAKE_B, &dquot->dq_flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 return QUOTA_OK;
1144
1145 if (dquot->dq_dqb.dqb_ihardlimit &&
1146 (dquot->dq_dqb.dqb_curinodes + inodes) > dquot->dq_dqb.dqb_ihardlimit &&
1147 !ignore_hardlimit(dquot)) {
Jan Kara8e893462007-10-16 23:29:31 -07001148 *warntype = QUOTA_NL_IHARDWARN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 return NO_QUOTA;
1150 }
1151
1152 if (dquot->dq_dqb.dqb_isoftlimit &&
1153 (dquot->dq_dqb.dqb_curinodes + inodes) > dquot->dq_dqb.dqb_isoftlimit &&
1154 dquot->dq_dqb.dqb_itime && get_seconds() >= dquot->dq_dqb.dqb_itime &&
1155 !ignore_hardlimit(dquot)) {
Jan Kara8e893462007-10-16 23:29:31 -07001156 *warntype = QUOTA_NL_ISOFTLONGWARN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 return NO_QUOTA;
1158 }
1159
1160 if (dquot->dq_dqb.dqb_isoftlimit &&
1161 (dquot->dq_dqb.dqb_curinodes + inodes) > dquot->dq_dqb.dqb_isoftlimit &&
1162 dquot->dq_dqb.dqb_itime == 0) {
Jan Kara8e893462007-10-16 23:29:31 -07001163 *warntype = QUOTA_NL_ISOFTWARN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 dquot->dq_dqb.dqb_itime = get_seconds() + sb_dqopt(dquot->dq_sb)->info[dquot->dq_type].dqi_igrace;
1165 }
1166
1167 return QUOTA_OK;
1168}
1169
1170/* needs dq_data_lock */
1171static int check_bdq(struct dquot *dquot, qsize_t space, int prealloc, char *warntype)
1172{
Mingming Caof18df222009-01-13 16:43:09 +01001173 qsize_t tspace;
1174
Jan Kara8e893462007-10-16 23:29:31 -07001175 *warntype = QUOTA_NL_NOWARN;
Jan Karaf55abc02008-08-20 17:50:32 +02001176 if (!sb_has_quota_limits_enabled(dquot->dq_sb, dquot->dq_type) ||
1177 test_bit(DQ_FAKE_B, &dquot->dq_flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 return QUOTA_OK;
1179
Mingming Caof18df222009-01-13 16:43:09 +01001180 tspace = dquot->dq_dqb.dqb_curspace + dquot->dq_dqb.dqb_rsvspace
1181 + space;
1182
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 if (dquot->dq_dqb.dqb_bhardlimit &&
Mingming Caof18df222009-01-13 16:43:09 +01001184 tspace > dquot->dq_dqb.dqb_bhardlimit &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 !ignore_hardlimit(dquot)) {
1186 if (!prealloc)
Jan Kara8e893462007-10-16 23:29:31 -07001187 *warntype = QUOTA_NL_BHARDWARN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 return NO_QUOTA;
1189 }
1190
1191 if (dquot->dq_dqb.dqb_bsoftlimit &&
Mingming Caof18df222009-01-13 16:43:09 +01001192 tspace > dquot->dq_dqb.dqb_bsoftlimit &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 dquot->dq_dqb.dqb_btime && get_seconds() >= dquot->dq_dqb.dqb_btime &&
1194 !ignore_hardlimit(dquot)) {
1195 if (!prealloc)
Jan Kara8e893462007-10-16 23:29:31 -07001196 *warntype = QUOTA_NL_BSOFTLONGWARN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 return NO_QUOTA;
1198 }
1199
1200 if (dquot->dq_dqb.dqb_bsoftlimit &&
Mingming Caof18df222009-01-13 16:43:09 +01001201 tspace > dquot->dq_dqb.dqb_bsoftlimit &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 dquot->dq_dqb.dqb_btime == 0) {
1203 if (!prealloc) {
Jan Kara8e893462007-10-16 23:29:31 -07001204 *warntype = QUOTA_NL_BSOFTWARN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 dquot->dq_dqb.dqb_btime = get_seconds() + sb_dqopt(dquot->dq_sb)->info[dquot->dq_type].dqi_bgrace;
1206 }
1207 else
1208 /*
1209 * We don't allow preallocation to exceed softlimit so exceeding will
1210 * be always printed
1211 */
1212 return NO_QUOTA;
1213 }
1214
1215 return QUOTA_OK;
1216}
1217
Jan Kara12095462008-08-20 14:45:12 +02001218static int info_idq_free(struct dquot *dquot, qsize_t inodes)
Jan Kara657d3bf2008-07-25 01:46:52 -07001219{
1220 if (test_bit(DQ_FAKE_B, &dquot->dq_flags) ||
Jan Karaf55abc02008-08-20 17:50:32 +02001221 dquot->dq_dqb.dqb_curinodes <= dquot->dq_dqb.dqb_isoftlimit ||
1222 !sb_has_quota_limits_enabled(dquot->dq_sb, dquot->dq_type))
Jan Kara657d3bf2008-07-25 01:46:52 -07001223 return QUOTA_NL_NOWARN;
1224
1225 if (dquot->dq_dqb.dqb_curinodes - inodes <= dquot->dq_dqb.dqb_isoftlimit)
1226 return QUOTA_NL_ISOFTBELOW;
1227 if (dquot->dq_dqb.dqb_curinodes >= dquot->dq_dqb.dqb_ihardlimit &&
1228 dquot->dq_dqb.dqb_curinodes - inodes < dquot->dq_dqb.dqb_ihardlimit)
1229 return QUOTA_NL_IHARDBELOW;
1230 return QUOTA_NL_NOWARN;
1231}
1232
1233static int info_bdq_free(struct dquot *dquot, qsize_t space)
1234{
1235 if (test_bit(DQ_FAKE_B, &dquot->dq_flags) ||
Jan Kara12095462008-08-20 14:45:12 +02001236 dquot->dq_dqb.dqb_curspace <= dquot->dq_dqb.dqb_bsoftlimit)
Jan Kara657d3bf2008-07-25 01:46:52 -07001237 return QUOTA_NL_NOWARN;
1238
Jan Kara12095462008-08-20 14:45:12 +02001239 if (dquot->dq_dqb.dqb_curspace - space <= dquot->dq_dqb.dqb_bsoftlimit)
Jan Kara657d3bf2008-07-25 01:46:52 -07001240 return QUOTA_NL_BSOFTBELOW;
Jan Kara12095462008-08-20 14:45:12 +02001241 if (dquot->dq_dqb.dqb_curspace >= dquot->dq_dqb.dqb_bhardlimit &&
1242 dquot->dq_dqb.dqb_curspace - space < dquot->dq_dqb.dqb_bhardlimit)
Jan Kara657d3bf2008-07-25 01:46:52 -07001243 return QUOTA_NL_BHARDBELOW;
1244 return QUOTA_NL_NOWARN;
1245}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246/*
1247 * Initialize quota pointers in inode
Jan Karacc334122009-01-12 17:23:05 +01001248 * We do things in a bit complicated way but by that we avoid calling
1249 * dqget() and thus filesystem callbacks under dqptr_sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 */
1251int dquot_initialize(struct inode *inode, int type)
1252{
1253 unsigned int id = 0;
1254 int cnt, ret = 0;
Jan Karadd6f3c62009-01-26 16:01:43 +01001255 struct dquot *got[MAXQUOTAS] = { NULL, NULL };
Jan Karacc334122009-01-12 17:23:05 +01001256 struct super_block *sb = inode->i_sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257
Ingo Molnard3be9152006-03-23 03:00:29 -08001258 /* First test before acquiring mutex - solves deadlocks when we
1259 * re-enter the quota code and are already holding the mutex */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 if (IS_NOQUOTA(inode))
1261 return 0;
Jan Karacc334122009-01-12 17:23:05 +01001262
1263 /* First get references to structures we might need. */
1264 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1265 if (type != -1 && cnt != type)
1266 continue;
1267 switch (cnt) {
1268 case USRQUOTA:
1269 id = inode->i_uid;
1270 break;
1271 case GRPQUOTA:
1272 id = inode->i_gid;
1273 break;
1274 }
1275 got[cnt] = dqget(sb, id, cnt);
1276 }
1277
1278 down_write(&sb_dqopt(sb)->dqptr_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 /* Having dqptr_sem we know NOQUOTA flags can't be altered... */
1280 if (IS_NOQUOTA(inode))
1281 goto out_err;
1282 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1283 if (type != -1 && cnt != type)
1284 continue;
Jan Karacc334122009-01-12 17:23:05 +01001285 /* Avoid races with quotaoff() */
1286 if (!sb_has_quota_active(sb, cnt))
1287 continue;
Jan Karadd6f3c62009-01-26 16:01:43 +01001288 if (!inode->i_dquot[cnt]) {
Jan Karacc334122009-01-12 17:23:05 +01001289 inode->i_dquot[cnt] = got[cnt];
Jan Karadd6f3c62009-01-26 16:01:43 +01001290 got[cnt] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 }
1292 }
1293out_err:
Jan Karacc334122009-01-12 17:23:05 +01001294 up_write(&sb_dqopt(sb)->dqptr_sem);
1295 /* Drop unused references */
1296 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1297 dqput(got[cnt]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 return ret;
1299}
Mingming Cao08d03502009-01-14 16:19:32 +01001300EXPORT_SYMBOL(dquot_initialize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301
1302/*
1303 * Release all quotas referenced by inode
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 */
Jan Kara3d9ea252008-10-10 16:12:23 +02001305int dquot_drop(struct inode *inode)
1306{
Jan Karacc334122009-01-12 17:23:05 +01001307 int cnt;
1308 struct dquot *put[MAXQUOTAS];
1309
Jan Kara3d9ea252008-10-10 16:12:23 +02001310 down_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
Jan Karacc334122009-01-12 17:23:05 +01001311 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1312 put[cnt] = inode->i_dquot[cnt];
Jan Karadd6f3c62009-01-26 16:01:43 +01001313 inode->i_dquot[cnt] = NULL;
Jan Karacc334122009-01-12 17:23:05 +01001314 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 up_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
Jan Karacc334122009-01-12 17:23:05 +01001316
1317 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1318 dqput(put[cnt]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 return 0;
1320}
Mingming Cao08d03502009-01-14 16:19:32 +01001321EXPORT_SYMBOL(dquot_drop);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322
Jan Karab85f4b82008-07-25 01:46:50 -07001323/* Wrapper to remove references to quota structures from inode */
1324void vfs_dq_drop(struct inode *inode)
1325{
1326 /* Here we can get arbitrary inode from clear_inode() so we have
1327 * to be careful. OTOH we don't need locking as quota operations
1328 * are allowed to change only at mount time */
1329 if (!IS_NOQUOTA(inode) && inode->i_sb && inode->i_sb->dq_op
1330 && inode->i_sb->dq_op->drop) {
1331 int cnt;
1332 /* Test before calling to rule out calls from proc and such
1333 * where we are not allowed to block. Note that this is
1334 * actually reliable test even without the lock - the caller
1335 * must assure that nobody can come after the DQUOT_DROP and
1336 * add quota pointers back anyway */
1337 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
Jan Karadd6f3c62009-01-26 16:01:43 +01001338 if (inode->i_dquot[cnt])
Jan Karab85f4b82008-07-25 01:46:50 -07001339 break;
1340 if (cnt < MAXQUOTAS)
1341 inode->i_sb->dq_op->drop(inode);
1342 }
1343}
Mingming Cao08d03502009-01-14 16:19:32 +01001344EXPORT_SYMBOL(vfs_dq_drop);
Jan Karab85f4b82008-07-25 01:46:50 -07001345
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346/*
1347 * Following four functions update i_blocks+i_bytes fields and
1348 * quota information (together with appropriate checks)
1349 * NOTE: We absolutely rely on the fact that caller dirties
1350 * the inode (usually macros in quotaops.h care about this) and
1351 * holds a handle for the current transaction so that dquot write and
1352 * inode write go into the same transaction.
1353 */
1354
1355/*
1356 * This operation can block, but only after everything is updated
1357 */
Mingming Caof18df222009-01-13 16:43:09 +01001358int __dquot_alloc_space(struct inode *inode, qsize_t number,
1359 int warn, int reserve)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360{
Mingming Caof18df222009-01-13 16:43:09 +01001361 int cnt, ret = QUOTA_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 char warntype[MAXQUOTAS];
1363
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
Jan Kara8e893462007-10-16 23:29:31 -07001365 warntype[cnt] = QUOTA_NL_NOWARN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 spin_lock(&dq_data_lock);
1368 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
Jan Karadd6f3c62009-01-26 16:01:43 +01001369 if (!inode->i_dquot[cnt])
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370 continue;
Mingming Caof18df222009-01-13 16:43:09 +01001371 if (check_bdq(inode->i_dquot[cnt], number, warn, warntype+cnt)
1372 == NO_QUOTA) {
1373 ret = NO_QUOTA;
1374 goto out_unlock;
1375 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 }
1377 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
Jan Karadd6f3c62009-01-26 16:01:43 +01001378 if (!inode->i_dquot[cnt])
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 continue;
Mingming Caof18df222009-01-13 16:43:09 +01001380 if (reserve)
1381 dquot_resv_space(inode->i_dquot[cnt], number);
1382 else
1383 dquot_incr_space(inode->i_dquot[cnt], number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 }
Mingming Caof18df222009-01-13 16:43:09 +01001385 if (!reserve)
1386 inode_add_bytes(inode, number);
1387out_unlock:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 spin_unlock(&dq_data_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 flush_warnings(inode->i_dquot, warntype);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 return ret;
1391}
1392
Mingming Caof18df222009-01-13 16:43:09 +01001393int dquot_alloc_space(struct inode *inode, qsize_t number, int warn)
1394{
1395 int cnt, ret = QUOTA_OK;
1396
1397 /*
1398 * First test before acquiring mutex - solves deadlocks when we
1399 * re-enter the quota code and are already holding the mutex
1400 */
1401 if (IS_NOQUOTA(inode)) {
1402 inode_add_bytes(inode, number);
1403 goto out;
1404 }
1405
1406 down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1407 if (IS_NOQUOTA(inode)) {
1408 inode_add_bytes(inode, number);
1409 goto out_unlock;
1410 }
1411
1412 ret = __dquot_alloc_space(inode, number, warn, 0);
1413 if (ret == NO_QUOTA)
1414 goto out_unlock;
1415
1416 /* Dirtify all the dquots - this can block when journalling */
1417 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1418 if (inode->i_dquot[cnt])
1419 mark_dquot_dirty(inode->i_dquot[cnt]);
1420out_unlock:
1421 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1422out:
1423 return ret;
1424}
Mingming Cao08d03502009-01-14 16:19:32 +01001425EXPORT_SYMBOL(dquot_alloc_space);
Mingming Caof18df222009-01-13 16:43:09 +01001426
1427int dquot_reserve_space(struct inode *inode, qsize_t number, int warn)
1428{
1429 int ret = QUOTA_OK;
1430
1431 if (IS_NOQUOTA(inode))
1432 goto out;
1433
1434 down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1435 if (IS_NOQUOTA(inode))
1436 goto out_unlock;
1437
1438 ret = __dquot_alloc_space(inode, number, warn, 1);
1439out_unlock:
1440 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1441out:
1442 return ret;
1443}
1444EXPORT_SYMBOL(dquot_reserve_space);
1445
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446/*
1447 * This operation can block, but only after everything is updated
1448 */
Jan Kara12095462008-08-20 14:45:12 +02001449int dquot_alloc_inode(const struct inode *inode, qsize_t number)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450{
1451 int cnt, ret = NO_QUOTA;
1452 char warntype[MAXQUOTAS];
1453
Ingo Molnard3be9152006-03-23 03:00:29 -08001454 /* First test before acquiring mutex - solves deadlocks when we
1455 * re-enter the quota code and are already holding the mutex */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456 if (IS_NOQUOTA(inode))
1457 return QUOTA_OK;
1458 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
Jan Kara8e893462007-10-16 23:29:31 -07001459 warntype[cnt] = QUOTA_NL_NOWARN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460 down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1461 if (IS_NOQUOTA(inode)) {
1462 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1463 return QUOTA_OK;
1464 }
1465 spin_lock(&dq_data_lock);
1466 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
Jan Karadd6f3c62009-01-26 16:01:43 +01001467 if (!inode->i_dquot[cnt])
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468 continue;
1469 if (check_idq(inode->i_dquot[cnt], number, warntype+cnt) == NO_QUOTA)
1470 goto warn_put_all;
1471 }
1472
1473 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
Jan Karadd6f3c62009-01-26 16:01:43 +01001474 if (!inode->i_dquot[cnt])
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475 continue;
1476 dquot_incr_inodes(inode->i_dquot[cnt], number);
1477 }
1478 ret = QUOTA_OK;
1479warn_put_all:
1480 spin_unlock(&dq_data_lock);
1481 if (ret == QUOTA_OK)
1482 /* Dirtify all the dquots - this can block when journalling */
1483 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1484 if (inode->i_dquot[cnt])
1485 mark_dquot_dirty(inode->i_dquot[cnt]);
Jan Kara087ee8d2007-12-17 16:20:26 -08001486 flush_warnings(inode->i_dquot, warntype);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1488 return ret;
1489}
Mingming Cao08d03502009-01-14 16:19:32 +01001490EXPORT_SYMBOL(dquot_alloc_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491
Mingming Cao740d9dc2009-01-13 16:43:14 +01001492int dquot_claim_space(struct inode *inode, qsize_t number)
1493{
1494 int cnt;
1495 int ret = QUOTA_OK;
1496
1497 if (IS_NOQUOTA(inode)) {
1498 inode_add_bytes(inode, number);
1499 goto out;
1500 }
1501
1502 down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1503 if (IS_NOQUOTA(inode)) {
1504 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1505 inode_add_bytes(inode, number);
1506 goto out;
1507 }
1508
1509 spin_lock(&dq_data_lock);
1510 /* Claim reserved quotas to allocated quotas */
1511 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
Jan Karadd6f3c62009-01-26 16:01:43 +01001512 if (inode->i_dquot[cnt])
Mingming Cao740d9dc2009-01-13 16:43:14 +01001513 dquot_claim_reserved_space(inode->i_dquot[cnt],
1514 number);
1515 }
1516 /* Update inode bytes */
1517 inode_add_bytes(inode, number);
1518 spin_unlock(&dq_data_lock);
1519 /* Dirtify all the dquots - this can block when journalling */
1520 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1521 if (inode->i_dquot[cnt])
1522 mark_dquot_dirty(inode->i_dquot[cnt]);
1523 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1524out:
1525 return ret;
1526}
1527EXPORT_SYMBOL(dquot_claim_space);
1528
1529/*
1530 * Release reserved quota space
1531 */
1532void dquot_release_reserved_space(struct inode *inode, qsize_t number)
1533{
1534 int cnt;
1535
1536 if (IS_NOQUOTA(inode))
1537 goto out;
1538
1539 down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1540 if (IS_NOQUOTA(inode))
1541 goto out_unlock;
1542
1543 spin_lock(&dq_data_lock);
1544 /* Release reserved dquots */
1545 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
Jan Karadd6f3c62009-01-26 16:01:43 +01001546 if (inode->i_dquot[cnt])
Mingming Cao740d9dc2009-01-13 16:43:14 +01001547 dquot_free_reserved_space(inode->i_dquot[cnt], number);
1548 }
1549 spin_unlock(&dq_data_lock);
1550
1551out_unlock:
1552 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1553out:
1554 return;
1555}
1556EXPORT_SYMBOL(dquot_release_reserved_space);
1557
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558/*
1559 * This operation can block, but only after everything is updated
1560 */
1561int dquot_free_space(struct inode *inode, qsize_t number)
1562{
1563 unsigned int cnt;
Jan Kara657d3bf2008-07-25 01:46:52 -07001564 char warntype[MAXQUOTAS];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565
Ingo Molnard3be9152006-03-23 03:00:29 -08001566 /* First test before acquiring mutex - solves deadlocks when we
1567 * re-enter the quota code and are already holding the mutex */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568 if (IS_NOQUOTA(inode)) {
1569out_sub:
1570 inode_sub_bytes(inode, number);
1571 return QUOTA_OK;
1572 }
Jan Kara657d3bf2008-07-25 01:46:52 -07001573
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574 down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1575 /* Now recheck reliably when holding dqptr_sem */
1576 if (IS_NOQUOTA(inode)) {
1577 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1578 goto out_sub;
1579 }
1580 spin_lock(&dq_data_lock);
1581 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
Jan Karadd6f3c62009-01-26 16:01:43 +01001582 if (!inode->i_dquot[cnt])
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583 continue;
Jan Kara657d3bf2008-07-25 01:46:52 -07001584 warntype[cnt] = info_bdq_free(inode->i_dquot[cnt], number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585 dquot_decr_space(inode->i_dquot[cnt], number);
1586 }
1587 inode_sub_bytes(inode, number);
1588 spin_unlock(&dq_data_lock);
1589 /* Dirtify all the dquots - this can block when journalling */
1590 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1591 if (inode->i_dquot[cnt])
1592 mark_dquot_dirty(inode->i_dquot[cnt]);
Jan Kara657d3bf2008-07-25 01:46:52 -07001593 flush_warnings(inode->i_dquot, warntype);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1595 return QUOTA_OK;
1596}
Mingming Cao08d03502009-01-14 16:19:32 +01001597EXPORT_SYMBOL(dquot_free_space);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598
1599/*
1600 * This operation can block, but only after everything is updated
1601 */
Jan Kara12095462008-08-20 14:45:12 +02001602int dquot_free_inode(const struct inode *inode, qsize_t number)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603{
1604 unsigned int cnt;
Jan Kara657d3bf2008-07-25 01:46:52 -07001605 char warntype[MAXQUOTAS];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606
Ingo Molnard3be9152006-03-23 03:00:29 -08001607 /* First test before acquiring mutex - solves deadlocks when we
1608 * re-enter the quota code and are already holding the mutex */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609 if (IS_NOQUOTA(inode))
1610 return QUOTA_OK;
Jan Kara657d3bf2008-07-25 01:46:52 -07001611
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612 down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1613 /* Now recheck reliably when holding dqptr_sem */
1614 if (IS_NOQUOTA(inode)) {
1615 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1616 return QUOTA_OK;
1617 }
1618 spin_lock(&dq_data_lock);
1619 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
Jan Karadd6f3c62009-01-26 16:01:43 +01001620 if (!inode->i_dquot[cnt])
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621 continue;
Jan Kara657d3bf2008-07-25 01:46:52 -07001622 warntype[cnt] = info_idq_free(inode->i_dquot[cnt], number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623 dquot_decr_inodes(inode->i_dquot[cnt], number);
1624 }
1625 spin_unlock(&dq_data_lock);
1626 /* Dirtify all the dquots - this can block when journalling */
1627 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1628 if (inode->i_dquot[cnt])
1629 mark_dquot_dirty(inode->i_dquot[cnt]);
Jan Kara657d3bf2008-07-25 01:46:52 -07001630 flush_warnings(inode->i_dquot, warntype);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1632 return QUOTA_OK;
1633}
Mingming Cao08d03502009-01-14 16:19:32 +01001634EXPORT_SYMBOL(dquot_free_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635
1636/*
Mingming Cao740d9dc2009-01-13 16:43:14 +01001637 * call back function, get reserved quota space from underlying fs
1638 */
1639qsize_t dquot_get_reserved_space(struct inode *inode)
1640{
1641 qsize_t reserved_space = 0;
1642
1643 if (sb_any_quota_active(inode->i_sb) &&
1644 inode->i_sb->dq_op->get_reserved_space)
1645 reserved_space = inode->i_sb->dq_op->get_reserved_space(inode);
1646 return reserved_space;
1647}
1648
1649/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650 * Transfer the number of inode and blocks from one diskquota to an other.
1651 *
1652 * This operation can block, but only after everything is updated
1653 * A transaction must be started when entering this function.
1654 */
1655int dquot_transfer(struct inode *inode, struct iattr *iattr)
1656{
Mingming Cao740d9dc2009-01-13 16:43:14 +01001657 qsize_t space, cur_space;
1658 qsize_t rsv_space = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659 struct dquot *transfer_from[MAXQUOTAS];
1660 struct dquot *transfer_to[MAXQUOTAS];
Jan Karacc334122009-01-12 17:23:05 +01001661 int cnt, ret = QUOTA_OK;
1662 int chuid = iattr->ia_valid & ATTR_UID && inode->i_uid != iattr->ia_uid,
1663 chgid = iattr->ia_valid & ATTR_GID && inode->i_gid != iattr->ia_gid;
Jan Kara657d3bf2008-07-25 01:46:52 -07001664 char warntype_to[MAXQUOTAS];
1665 char warntype_from_inodes[MAXQUOTAS], warntype_from_space[MAXQUOTAS];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666
Ingo Molnard3be9152006-03-23 03:00:29 -08001667 /* First test before acquiring mutex - solves deadlocks when we
1668 * re-enter the quota code and are already holding the mutex */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669 if (IS_NOQUOTA(inode))
1670 return QUOTA_OK;
Jan Karacc334122009-01-12 17:23:05 +01001671 /* Initialize the arrays */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
Jan Karadd6f3c62009-01-26 16:01:43 +01001673 transfer_from[cnt] = NULL;
1674 transfer_to[cnt] = NULL;
Jan Kara657d3bf2008-07-25 01:46:52 -07001675 warntype_to[cnt] = QUOTA_NL_NOWARN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676 switch (cnt) {
1677 case USRQUOTA:
1678 if (!chuid)
1679 continue;
1680 transfer_to[cnt] = dqget(inode->i_sb, iattr->ia_uid, cnt);
1681 break;
1682 case GRPQUOTA:
1683 if (!chgid)
1684 continue;
1685 transfer_to[cnt] = dqget(inode->i_sb, iattr->ia_gid, cnt);
1686 break;
1687 }
1688 }
Jan Karacc334122009-01-12 17:23:05 +01001689
1690 down_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
1691 /* Now recheck reliably when holding dqptr_sem */
1692 if (IS_NOQUOTA(inode)) { /* File without quota accounting? */
1693 up_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
1694 goto put_all;
1695 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696 spin_lock(&dq_data_lock);
Mingming Cao740d9dc2009-01-13 16:43:14 +01001697 cur_space = inode_get_bytes(inode);
1698 rsv_space = dquot_get_reserved_space(inode);
1699 space = cur_space + rsv_space;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700 /* Build the transfer_from list and check the limits */
1701 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
Jan Karadd6f3c62009-01-26 16:01:43 +01001702 if (!transfer_to[cnt])
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703 continue;
1704 transfer_from[cnt] = inode->i_dquot[cnt];
Jan Kara657d3bf2008-07-25 01:46:52 -07001705 if (check_idq(transfer_to[cnt], 1, warntype_to + cnt) ==
1706 NO_QUOTA || check_bdq(transfer_to[cnt], space, 0,
1707 warntype_to + cnt) == NO_QUOTA)
Jan Karacc334122009-01-12 17:23:05 +01001708 goto over_quota;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709 }
1710
1711 /*
1712 * Finally perform the needed transfer from transfer_from to transfer_to
1713 */
1714 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1715 /*
1716 * Skip changes for same uid or gid or for turned off quota-type.
1717 */
Jan Karadd6f3c62009-01-26 16:01:43 +01001718 if (!transfer_to[cnt])
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719 continue;
1720
1721 /* Due to IO error we might not have transfer_from[] structure */
1722 if (transfer_from[cnt]) {
Jan Kara657d3bf2008-07-25 01:46:52 -07001723 warntype_from_inodes[cnt] =
1724 info_idq_free(transfer_from[cnt], 1);
1725 warntype_from_space[cnt] =
1726 info_bdq_free(transfer_from[cnt], space);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727 dquot_decr_inodes(transfer_from[cnt], 1);
Mingming Cao740d9dc2009-01-13 16:43:14 +01001728 dquot_decr_space(transfer_from[cnt], cur_space);
1729 dquot_free_reserved_space(transfer_from[cnt],
1730 rsv_space);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731 }
1732
1733 dquot_incr_inodes(transfer_to[cnt], 1);
Mingming Cao740d9dc2009-01-13 16:43:14 +01001734 dquot_incr_space(transfer_to[cnt], cur_space);
1735 dquot_resv_space(transfer_to[cnt], rsv_space);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736
1737 inode->i_dquot[cnt] = transfer_to[cnt];
1738 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739 spin_unlock(&dq_data_lock);
Jan Karacc334122009-01-12 17:23:05 +01001740 up_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
1741
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742 /* Dirtify all the dquots - this can block when journalling */
1743 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1744 if (transfer_from[cnt])
1745 mark_dquot_dirty(transfer_from[cnt]);
Jan Karacc334122009-01-12 17:23:05 +01001746 if (transfer_to[cnt]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747 mark_dquot_dirty(transfer_to[cnt]);
Jan Karacc334122009-01-12 17:23:05 +01001748 /* The reference we got is transferred to the inode */
Jan Karadd6f3c62009-01-26 16:01:43 +01001749 transfer_to[cnt] = NULL;
Jan Karacc334122009-01-12 17:23:05 +01001750 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751 }
Jan Karacc334122009-01-12 17:23:05 +01001752warn_put_all:
Jan Kara657d3bf2008-07-25 01:46:52 -07001753 flush_warnings(transfer_to, warntype_to);
1754 flush_warnings(transfer_from, warntype_from_inodes);
1755 flush_warnings(transfer_from, warntype_from_space);
Jan Karacc334122009-01-12 17:23:05 +01001756put_all:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
Jan Karacc334122009-01-12 17:23:05 +01001758 dqput(transfer_from[cnt]);
1759 dqput(transfer_to[cnt]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761 return ret;
Jan Karacc334122009-01-12 17:23:05 +01001762over_quota:
1763 spin_unlock(&dq_data_lock);
1764 up_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
1765 /* Clear dquot pointers we don't want to dqput() */
1766 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
Jan Karadd6f3c62009-01-26 16:01:43 +01001767 transfer_from[cnt] = NULL;
Jan Karacc334122009-01-12 17:23:05 +01001768 ret = NO_QUOTA;
1769 goto warn_put_all;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770}
Mingming Cao08d03502009-01-14 16:19:32 +01001771EXPORT_SYMBOL(dquot_transfer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772
Jan Karab85f4b82008-07-25 01:46:50 -07001773/* Wrapper for transferring ownership of an inode */
1774int vfs_dq_transfer(struct inode *inode, struct iattr *iattr)
1775{
Jan Karaf55abc02008-08-20 17:50:32 +02001776 if (sb_any_quota_active(inode->i_sb) && !IS_NOQUOTA(inode)) {
Jan Karab85f4b82008-07-25 01:46:50 -07001777 vfs_dq_init(inode);
1778 if (inode->i_sb->dq_op->transfer(inode, iattr) == NO_QUOTA)
1779 return 1;
1780 }
1781 return 0;
1782}
Mingming Cao08d03502009-01-14 16:19:32 +01001783EXPORT_SYMBOL(vfs_dq_transfer);
Jan Karab85f4b82008-07-25 01:46:50 -07001784
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785/*
1786 * Write info of quota file to disk
1787 */
1788int dquot_commit_info(struct super_block *sb, int type)
1789{
1790 int ret;
1791 struct quota_info *dqopt = sb_dqopt(sb);
1792
Ingo Molnard3be9152006-03-23 03:00:29 -08001793 mutex_lock(&dqopt->dqio_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794 ret = dqopt->ops[type]->write_file_info(sb, type);
Ingo Molnard3be9152006-03-23 03:00:29 -08001795 mutex_unlock(&dqopt->dqio_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796 return ret;
1797}
Mingming Cao08d03502009-01-14 16:19:32 +01001798EXPORT_SYMBOL(dquot_commit_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799
1800/*
1801 * Definitions of diskquota operations.
1802 */
1803struct dquot_operations dquot_operations = {
1804 .initialize = dquot_initialize,
1805 .drop = dquot_drop,
1806 .alloc_space = dquot_alloc_space,
1807 .alloc_inode = dquot_alloc_inode,
1808 .free_space = dquot_free_space,
1809 .free_inode = dquot_free_inode,
1810 .transfer = dquot_transfer,
1811 .write_dquot = dquot_commit,
1812 .acquire_dquot = dquot_acquire,
1813 .release_dquot = dquot_release,
1814 .mark_dirty = dquot_mark_dquot_dirty,
Jan Kara74f783a2008-08-19 14:51:22 +02001815 .write_info = dquot_commit_info,
1816 .alloc_dquot = dquot_alloc,
1817 .destroy_dquot = dquot_destroy,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818};
1819
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820/*
1821 * Turn quota off on a device. type == -1 ==> quotaoff for all types (umount)
1822 */
Jan Karaf55abc02008-08-20 17:50:32 +02001823int vfs_quota_disable(struct super_block *sb, int type, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824{
Jan Kara0ff5af82008-04-28 02:14:33 -07001825 int cnt, ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826 struct quota_info *dqopt = sb_dqopt(sb);
1827 struct inode *toputinode[MAXQUOTAS];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828
Jan Karaf55abc02008-08-20 17:50:32 +02001829 /* Cannot turn off usage accounting without turning off limits, or
1830 * suspend quotas and simultaneously turn quotas off. */
1831 if ((flags & DQUOT_USAGE_ENABLED && !(flags & DQUOT_LIMITS_ENABLED))
1832 || (flags & DQUOT_SUSPENDED && flags & (DQUOT_LIMITS_ENABLED |
1833 DQUOT_USAGE_ENABLED)))
1834 return -EINVAL;
1835
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836 /* We need to serialize quota_off() for device */
Ingo Molnard3be9152006-03-23 03:00:29 -08001837 mutex_lock(&dqopt->dqonoff_mutex);
Jan Kara9377abd2008-05-12 14:02:08 -07001838
1839 /*
1840 * Skip everything if there's nothing to do. We have to do this because
1841 * sometimes we are called when fill_super() failed and calling
1842 * sync_fs() in such cases does no good.
1843 */
Jan Karaf55abc02008-08-20 17:50:32 +02001844 if (!sb_any_quota_loaded(sb)) {
Jan Kara9377abd2008-05-12 14:02:08 -07001845 mutex_unlock(&dqopt->dqonoff_mutex);
1846 return 0;
1847 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1849 toputinode[cnt] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850 if (type != -1 && cnt != type)
1851 continue;
Jan Karaf55abc02008-08-20 17:50:32 +02001852 if (!sb_has_quota_loaded(sb, cnt))
Jan Kara0ff5af82008-04-28 02:14:33 -07001853 continue;
Jan Karaf55abc02008-08-20 17:50:32 +02001854
1855 if (flags & DQUOT_SUSPENDED) {
Jan Karacc334122009-01-12 17:23:05 +01001856 spin_lock(&dq_state_lock);
Jan Karaf55abc02008-08-20 17:50:32 +02001857 dqopt->flags |=
1858 dquot_state_flag(DQUOT_SUSPENDED, cnt);
Jan Karacc334122009-01-12 17:23:05 +01001859 spin_unlock(&dq_state_lock);
Jan Karaf55abc02008-08-20 17:50:32 +02001860 } else {
Jan Karacc334122009-01-12 17:23:05 +01001861 spin_lock(&dq_state_lock);
Jan Karaf55abc02008-08-20 17:50:32 +02001862 dqopt->flags &= ~dquot_state_flag(flags, cnt);
1863 /* Turning off suspended quotas? */
1864 if (!sb_has_quota_loaded(sb, cnt) &&
1865 sb_has_quota_suspended(sb, cnt)) {
1866 dqopt->flags &= ~dquot_state_flag(
1867 DQUOT_SUSPENDED, cnt);
Jan Karacc334122009-01-12 17:23:05 +01001868 spin_unlock(&dq_state_lock);
Jan Karaf55abc02008-08-20 17:50:32 +02001869 iput(dqopt->files[cnt]);
1870 dqopt->files[cnt] = NULL;
1871 continue;
1872 }
Jan Karacc334122009-01-12 17:23:05 +01001873 spin_unlock(&dq_state_lock);
Jan Kara0ff5af82008-04-28 02:14:33 -07001874 }
Jan Karaf55abc02008-08-20 17:50:32 +02001875
1876 /* We still have to keep quota loaded? */
1877 if (sb_has_quota_loaded(sb, cnt) && !(flags & DQUOT_SUSPENDED))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879
1880 /* Note: these are blocking operations */
1881 drop_dquot_ref(sb, cnt);
1882 invalidate_dquots(sb, cnt);
1883 /*
1884 * Now all dquots should be invalidated, all writes done so we should be only
1885 * users of the info. No locks needed.
1886 */
1887 if (info_dirty(&dqopt->info[cnt]))
1888 sb->dq_op->write_info(sb, cnt);
1889 if (dqopt->ops[cnt]->free_file_info)
1890 dqopt->ops[cnt]->free_file_info(sb, cnt);
1891 put_quota_format(dqopt->info[cnt].dqi_format);
1892
1893 toputinode[cnt] = dqopt->files[cnt];
Jan Karaf55abc02008-08-20 17:50:32 +02001894 if (!sb_has_quota_loaded(sb, cnt))
Jan Kara0ff5af82008-04-28 02:14:33 -07001895 dqopt->files[cnt] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896 dqopt->info[cnt].dqi_flags = 0;
1897 dqopt->info[cnt].dqi_igrace = 0;
1898 dqopt->info[cnt].dqi_bgrace = 0;
1899 dqopt->ops[cnt] = NULL;
1900 }
Ingo Molnard3be9152006-03-23 03:00:29 -08001901 mutex_unlock(&dqopt->dqonoff_mutex);
Jan Karaca785ec2008-09-30 17:53:37 +02001902
1903 /* Skip syncing and setting flags if quota files are hidden */
1904 if (dqopt->flags & DQUOT_QUOTA_SYS_FILE)
1905 goto put_inodes;
1906
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907 /* Sync the superblock so that buffers with quota data are written to
Al Viro7b7b1ac2005-11-07 17:13:39 -05001908 * disk (and so userspace sees correct data afterwards). */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909 if (sb->s_op->sync_fs)
1910 sb->s_op->sync_fs(sb, 1);
1911 sync_blockdev(sb->s_bdev);
1912 /* Now the quota files are just ordinary files and we can set the
1913 * inode flags back. Moreover we discard the pagecache so that
1914 * userspace sees the writes we did bypassing the pagecache. We
1915 * must also discard the blockdev buffers so that we see the
1916 * changes done by userspace on the next quotaon() */
1917 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1918 if (toputinode[cnt]) {
Ingo Molnard3be9152006-03-23 03:00:29 -08001919 mutex_lock(&dqopt->dqonoff_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920 /* If quota was reenabled in the meantime, we have
1921 * nothing to do */
Jan Karaf55abc02008-08-20 17:50:32 +02001922 if (!sb_has_quota_loaded(sb, cnt)) {
Jan Kara79254092007-05-16 22:11:19 -07001923 mutex_lock_nested(&toputinode[cnt]->i_mutex, I_MUTEX_QUOTA);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924 toputinode[cnt]->i_flags &= ~(S_IMMUTABLE |
1925 S_NOATIME | S_NOQUOTA);
1926 truncate_inode_pages(&toputinode[cnt]->i_data, 0);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001927 mutex_unlock(&toputinode[cnt]->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928 mark_inode_dirty(toputinode[cnt]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929 }
Ingo Molnard3be9152006-03-23 03:00:29 -08001930 mutex_unlock(&dqopt->dqonoff_mutex);
Jan Karaca785ec2008-09-30 17:53:37 +02001931 }
1932 if (sb->s_bdev)
1933 invalidate_bdev(sb->s_bdev);
1934put_inodes:
1935 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1936 if (toputinode[cnt]) {
Jan Kara0ff5af82008-04-28 02:14:33 -07001937 /* On remount RO, we keep the inode pointer so that we
Jan Karaf55abc02008-08-20 17:50:32 +02001938 * can reenable quota on the subsequent remount RW. We
1939 * have to check 'flags' variable and not use sb_has_
1940 * function because another quotaon / quotaoff could
1941 * change global state before we got here. We refuse
1942 * to suspend quotas when there is pending delete on
1943 * the quota file... */
1944 if (!(flags & DQUOT_SUSPENDED))
Jan Kara0ff5af82008-04-28 02:14:33 -07001945 iput(toputinode[cnt]);
1946 else if (!toputinode[cnt]->i_nlink)
1947 ret = -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948 }
Jan Kara0ff5af82008-04-28 02:14:33 -07001949 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950}
Mingming Cao08d03502009-01-14 16:19:32 +01001951EXPORT_SYMBOL(vfs_quota_disable);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952
Jan Karaf55abc02008-08-20 17:50:32 +02001953int vfs_quota_off(struct super_block *sb, int type, int remount)
1954{
1955 return vfs_quota_disable(sb, type, remount ? DQUOT_SUSPENDED :
1956 (DQUOT_USAGE_ENABLED | DQUOT_LIMITS_ENABLED));
1957}
Mingming Cao08d03502009-01-14 16:19:32 +01001958EXPORT_SYMBOL(vfs_quota_off);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959/*
1960 * Turn quotas on on a device
1961 */
1962
Jan Karaf55abc02008-08-20 17:50:32 +02001963/*
1964 * Helper function to turn quotas on when we already have the inode of
1965 * quota file and no quota information is loaded.
1966 */
1967static int vfs_load_quota_inode(struct inode *inode, int type, int format_id,
1968 unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969{
1970 struct quota_format_type *fmt = find_quota_format(format_id);
1971 struct super_block *sb = inode->i_sb;
1972 struct quota_info *dqopt = sb_dqopt(sb);
1973 int error;
1974 int oldflags = -1;
1975
1976 if (!fmt)
1977 return -ESRCH;
1978 if (!S_ISREG(inode->i_mode)) {
1979 error = -EACCES;
1980 goto out_fmt;
1981 }
1982 if (IS_RDONLY(inode)) {
1983 error = -EROFS;
1984 goto out_fmt;
1985 }
1986 if (!sb->s_op->quota_write || !sb->s_op->quota_read) {
1987 error = -EINVAL;
1988 goto out_fmt;
1989 }
Jan Karaf55abc02008-08-20 17:50:32 +02001990 /* Usage always has to be set... */
1991 if (!(flags & DQUOT_USAGE_ENABLED)) {
1992 error = -EINVAL;
1993 goto out_fmt;
1994 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995
Jan Karaca785ec2008-09-30 17:53:37 +02001996 if (!(dqopt->flags & DQUOT_QUOTA_SYS_FILE)) {
1997 /* As we bypass the pagecache we must now flush the inode so
1998 * that we see all the changes from userspace... */
1999 write_inode_now(inode, 1);
2000 /* And now flush the block cache so that kernel sees the
2001 * changes */
2002 invalidate_bdev(sb->s_bdev);
2003 }
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002004 mutex_lock(&inode->i_mutex);
Ingo Molnard3be9152006-03-23 03:00:29 -08002005 mutex_lock(&dqopt->dqonoff_mutex);
Jan Karaf55abc02008-08-20 17:50:32 +02002006 if (sb_has_quota_loaded(sb, type)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007 error = -EBUSY;
2008 goto out_lock;
2009 }
Jan Karaca785ec2008-09-30 17:53:37 +02002010
2011 if (!(dqopt->flags & DQUOT_QUOTA_SYS_FILE)) {
2012 /* We don't want quota and atime on quota files (deadlocks
2013 * possible) Also nobody should write to the file - we use
2014 * special IO operations which ignore the immutable bit. */
2015 down_write(&dqopt->dqptr_sem);
2016 oldflags = inode->i_flags & (S_NOATIME | S_IMMUTABLE | S_NOQUOTA);
2017 inode->i_flags |= S_NOQUOTA | S_NOATIME | S_IMMUTABLE;
2018 up_write(&dqopt->dqptr_sem);
2019 sb->dq_op->drop(inode);
2020 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021
2022 error = -EIO;
2023 dqopt->files[type] = igrab(inode);
2024 if (!dqopt->files[type])
2025 goto out_lock;
2026 error = -EINVAL;
2027 if (!fmt->qf_ops->check_quota_file(sb, type))
2028 goto out_file_init;
2029
2030 dqopt->ops[type] = fmt->qf_ops;
2031 dqopt->info[type].dqi_format = fmt;
Jan Kara0ff5af82008-04-28 02:14:33 -07002032 dqopt->info[type].dqi_fmt_id = format_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033 INIT_LIST_HEAD(&dqopt->info[type].dqi_dirty_list);
Ingo Molnard3be9152006-03-23 03:00:29 -08002034 mutex_lock(&dqopt->dqio_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035 if ((error = dqopt->ops[type]->read_file_info(sb, type)) < 0) {
Ingo Molnard3be9152006-03-23 03:00:29 -08002036 mutex_unlock(&dqopt->dqio_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037 goto out_file_init;
2038 }
Ingo Molnard3be9152006-03-23 03:00:29 -08002039 mutex_unlock(&dqopt->dqio_mutex);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002040 mutex_unlock(&inode->i_mutex);
Jan Karacc334122009-01-12 17:23:05 +01002041 spin_lock(&dq_state_lock);
Jan Karaf55abc02008-08-20 17:50:32 +02002042 dqopt->flags |= dquot_state_flag(flags, type);
Jan Karacc334122009-01-12 17:23:05 +01002043 spin_unlock(&dq_state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044
2045 add_dquot_ref(sb, type);
Ingo Molnard3be9152006-03-23 03:00:29 -08002046 mutex_unlock(&dqopt->dqonoff_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047
2048 return 0;
2049
2050out_file_init:
2051 dqopt->files[type] = NULL;
2052 iput(inode);
2053out_lock:
Ingo Molnard3be9152006-03-23 03:00:29 -08002054 mutex_unlock(&dqopt->dqonoff_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055 if (oldflags != -1) {
2056 down_write(&dqopt->dqptr_sem);
2057 /* Set the flags back (in the case of accidental quotaon()
2058 * on a wrong file we don't want to mess up the flags) */
2059 inode->i_flags &= ~(S_NOATIME | S_NOQUOTA | S_IMMUTABLE);
2060 inode->i_flags |= oldflags;
2061 up_write(&dqopt->dqptr_sem);
2062 }
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002063 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064out_fmt:
2065 put_quota_format(fmt);
2066
2067 return error;
2068}
2069
Jan Kara0ff5af82008-04-28 02:14:33 -07002070/* Reenable quotas on remount RW */
2071static int vfs_quota_on_remount(struct super_block *sb, int type)
2072{
2073 struct quota_info *dqopt = sb_dqopt(sb);
2074 struct inode *inode;
2075 int ret;
Jan Karaf55abc02008-08-20 17:50:32 +02002076 unsigned int flags;
Jan Kara0ff5af82008-04-28 02:14:33 -07002077
2078 mutex_lock(&dqopt->dqonoff_mutex);
2079 if (!sb_has_quota_suspended(sb, type)) {
2080 mutex_unlock(&dqopt->dqonoff_mutex);
2081 return 0;
2082 }
Jan Kara0ff5af82008-04-28 02:14:33 -07002083 inode = dqopt->files[type];
2084 dqopt->files[type] = NULL;
Jan Karacc334122009-01-12 17:23:05 +01002085 spin_lock(&dq_state_lock);
Jan Karaf55abc02008-08-20 17:50:32 +02002086 flags = dqopt->flags & dquot_state_flag(DQUOT_USAGE_ENABLED |
2087 DQUOT_LIMITS_ENABLED, type);
2088 dqopt->flags &= ~dquot_state_flag(DQUOT_STATE_FLAGS, type);
Jan Karacc334122009-01-12 17:23:05 +01002089 spin_unlock(&dq_state_lock);
Jan Kara0ff5af82008-04-28 02:14:33 -07002090 mutex_unlock(&dqopt->dqonoff_mutex);
2091
Jan Karaf55abc02008-08-20 17:50:32 +02002092 flags = dquot_generic_flag(flags, type);
2093 ret = vfs_load_quota_inode(inode, type, dqopt->info[type].dqi_fmt_id,
2094 flags);
Jan Kara0ff5af82008-04-28 02:14:33 -07002095 iput(inode);
2096
2097 return ret;
2098}
2099
Al Viro77e69da2008-08-01 04:29:18 -04002100int vfs_quota_on_path(struct super_block *sb, int type, int format_id,
2101 struct path *path)
2102{
2103 int error = security_quota_on(path->dentry);
2104 if (error)
2105 return error;
2106 /* Quota file not on the same filesystem? */
2107 if (path->mnt->mnt_sb != sb)
2108 error = -EXDEV;
2109 else
Jan Karaf55abc02008-08-20 17:50:32 +02002110 error = vfs_load_quota_inode(path->dentry->d_inode, type,
2111 format_id, DQUOT_USAGE_ENABLED |
2112 DQUOT_LIMITS_ENABLED);
Al Viro77e69da2008-08-01 04:29:18 -04002113 return error;
2114}
Mingming Cao08d03502009-01-14 16:19:32 +01002115EXPORT_SYMBOL(vfs_quota_on_path);
Al Viro77e69da2008-08-01 04:29:18 -04002116
Al Viro82646132008-08-02 00:57:06 -04002117int vfs_quota_on(struct super_block *sb, int type, int format_id, char *name,
Jan Kara0ff5af82008-04-28 02:14:33 -07002118 int remount)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119{
Al Viro82646132008-08-02 00:57:06 -04002120 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121 int error;
2122
Jan Kara0ff5af82008-04-28 02:14:33 -07002123 if (remount)
2124 return vfs_quota_on_remount(sb, type);
2125
Al Viro82646132008-08-02 00:57:06 -04002126 error = kern_path(name, LOOKUP_FOLLOW, &path);
Al Viro77e69da2008-08-01 04:29:18 -04002127 if (!error) {
Al Viro82646132008-08-02 00:57:06 -04002128 error = vfs_quota_on_path(sb, type, format_id, &path);
2129 path_put(&path);
Al Viro77e69da2008-08-01 04:29:18 -04002130 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131 return error;
2132}
Mingming Cao08d03502009-01-14 16:19:32 +01002133EXPORT_SYMBOL(vfs_quota_on);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134
2135/*
Jan Karaf55abc02008-08-20 17:50:32 +02002136 * More powerful function for turning on quotas allowing setting
2137 * of individual quota flags
2138 */
2139int vfs_quota_enable(struct inode *inode, int type, int format_id,
2140 unsigned int flags)
2141{
2142 int ret = 0;
2143 struct super_block *sb = inode->i_sb;
2144 struct quota_info *dqopt = sb_dqopt(sb);
2145
2146 /* Just unsuspend quotas? */
2147 if (flags & DQUOT_SUSPENDED)
2148 return vfs_quota_on_remount(sb, type);
2149 if (!flags)
2150 return 0;
2151 /* Just updating flags needed? */
2152 if (sb_has_quota_loaded(sb, type)) {
2153 mutex_lock(&dqopt->dqonoff_mutex);
2154 /* Now do a reliable test... */
2155 if (!sb_has_quota_loaded(sb, type)) {
2156 mutex_unlock(&dqopt->dqonoff_mutex);
2157 goto load_quota;
2158 }
2159 if (flags & DQUOT_USAGE_ENABLED &&
2160 sb_has_quota_usage_enabled(sb, type)) {
2161 ret = -EBUSY;
2162 goto out_lock;
2163 }
2164 if (flags & DQUOT_LIMITS_ENABLED &&
2165 sb_has_quota_limits_enabled(sb, type)) {
2166 ret = -EBUSY;
2167 goto out_lock;
2168 }
Jan Karacc334122009-01-12 17:23:05 +01002169 spin_lock(&dq_state_lock);
Jan Karaf55abc02008-08-20 17:50:32 +02002170 sb_dqopt(sb)->flags |= dquot_state_flag(flags, type);
Jan Karacc334122009-01-12 17:23:05 +01002171 spin_unlock(&dq_state_lock);
Jan Karaf55abc02008-08-20 17:50:32 +02002172out_lock:
2173 mutex_unlock(&dqopt->dqonoff_mutex);
2174 return ret;
2175 }
2176
2177load_quota:
2178 return vfs_load_quota_inode(inode, type, format_id, flags);
2179}
Mingming Cao08d03502009-01-14 16:19:32 +01002180EXPORT_SYMBOL(vfs_quota_enable);
Jan Karaf55abc02008-08-20 17:50:32 +02002181
2182/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183 * This function is used when filesystem needs to initialize quotas
2184 * during mount time.
2185 */
Christoph Hellwig84de8562005-06-23 00:09:16 -07002186int vfs_quota_on_mount(struct super_block *sb, char *qf_name,
2187 int format_id, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188{
Christoph Hellwig84de8562005-06-23 00:09:16 -07002189 struct dentry *dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190 int error;
2191
Christoph Hellwig2fa389c2005-06-23 00:09:16 -07002192 dentry = lookup_one_len(qf_name, sb->s_root, strlen(qf_name));
Christoph Hellwig84de8562005-06-23 00:09:16 -07002193 if (IS_ERR(dentry))
2194 return PTR_ERR(dentry);
2195
Jan Kara154f4842005-11-28 13:44:14 -08002196 if (!dentry->d_inode) {
2197 error = -ENOENT;
2198 goto out;
2199 }
2200
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201 error = security_quota_on(dentry);
Christoph Hellwig84de8562005-06-23 00:09:16 -07002202 if (!error)
Jan Karaf55abc02008-08-20 17:50:32 +02002203 error = vfs_load_quota_inode(dentry->d_inode, type, format_id,
2204 DQUOT_USAGE_ENABLED | DQUOT_LIMITS_ENABLED);
Christoph Hellwig84de8562005-06-23 00:09:16 -07002205
Jan Kara154f4842005-11-28 13:44:14 -08002206out:
Christoph Hellwig84de8562005-06-23 00:09:16 -07002207 dput(dentry);
2208 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209}
Mingming Cao08d03502009-01-14 16:19:32 +01002210EXPORT_SYMBOL(vfs_quota_on_mount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211
Jan Karab85f4b82008-07-25 01:46:50 -07002212/* Wrapper to turn on quotas when remounting rw */
2213int vfs_dq_quota_on_remount(struct super_block *sb)
2214{
2215 int cnt;
2216 int ret = 0, err;
2217
2218 if (!sb->s_qcop || !sb->s_qcop->quota_on)
2219 return -ENOSYS;
2220 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
2221 err = sb->s_qcop->quota_on(sb, cnt, 0, NULL, 1);
2222 if (err < 0 && !ret)
2223 ret = err;
2224 }
2225 return ret;
2226}
Mingming Cao08d03502009-01-14 16:19:32 +01002227EXPORT_SYMBOL(vfs_dq_quota_on_remount);
Jan Karab85f4b82008-07-25 01:46:50 -07002228
Jan Kara12095462008-08-20 14:45:12 +02002229static inline qsize_t qbtos(qsize_t blocks)
2230{
2231 return blocks << QIF_DQBLKSIZE_BITS;
2232}
2233
2234static inline qsize_t stoqb(qsize_t space)
2235{
2236 return (space + QIF_DQBLKSIZE - 1) >> QIF_DQBLKSIZE_BITS;
2237}
2238
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239/* Generic routine for getting common part of quota structure */
2240static void do_get_dqblk(struct dquot *dquot, struct if_dqblk *di)
2241{
2242 struct mem_dqblk *dm = &dquot->dq_dqb;
2243
2244 spin_lock(&dq_data_lock);
Jan Kara12095462008-08-20 14:45:12 +02002245 di->dqb_bhardlimit = stoqb(dm->dqb_bhardlimit);
2246 di->dqb_bsoftlimit = stoqb(dm->dqb_bsoftlimit);
Mingming Caof18df222009-01-13 16:43:09 +01002247 di->dqb_curspace = dm->dqb_curspace + dm->dqb_rsvspace;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248 di->dqb_ihardlimit = dm->dqb_ihardlimit;
2249 di->dqb_isoftlimit = dm->dqb_isoftlimit;
2250 di->dqb_curinodes = dm->dqb_curinodes;
2251 di->dqb_btime = dm->dqb_btime;
2252 di->dqb_itime = dm->dqb_itime;
2253 di->dqb_valid = QIF_ALL;
2254 spin_unlock(&dq_data_lock);
2255}
2256
2257int vfs_get_dqblk(struct super_block *sb, int type, qid_t id, struct if_dqblk *di)
2258{
2259 struct dquot *dquot;
2260
Jan Karacc334122009-01-12 17:23:05 +01002261 dquot = dqget(sb, id, type);
Jan Karadd6f3c62009-01-26 16:01:43 +01002262 if (!dquot)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263 return -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264 do_get_dqblk(dquot, di);
2265 dqput(dquot);
Jan Karacc334122009-01-12 17:23:05 +01002266
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267 return 0;
2268}
Mingming Cao08d03502009-01-14 16:19:32 +01002269EXPORT_SYMBOL(vfs_get_dqblk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270
2271/* Generic routine for setting common part of quota structure */
Andrew Perepechko338bf9a2008-04-28 02:14:31 -07002272static int do_set_dqblk(struct dquot *dquot, struct if_dqblk *di)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273{
2274 struct mem_dqblk *dm = &dquot->dq_dqb;
2275 int check_blim = 0, check_ilim = 0;
Andrew Perepechko338bf9a2008-04-28 02:14:31 -07002276 struct mem_dqinfo *dqi = &sb_dqopt(dquot->dq_sb)->info[dquot->dq_type];
2277
2278 if ((di->dqb_valid & QIF_BLIMITS &&
2279 (di->dqb_bhardlimit > dqi->dqi_maxblimit ||
2280 di->dqb_bsoftlimit > dqi->dqi_maxblimit)) ||
2281 (di->dqb_valid & QIF_ILIMITS &&
2282 (di->dqb_ihardlimit > dqi->dqi_maxilimit ||
2283 di->dqb_isoftlimit > dqi->dqi_maxilimit)))
2284 return -ERANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285
2286 spin_lock(&dq_data_lock);
2287 if (di->dqb_valid & QIF_SPACE) {
Mingming Caof18df222009-01-13 16:43:09 +01002288 dm->dqb_curspace = di->dqb_curspace - dm->dqb_rsvspace;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289 check_blim = 1;
Jan Kara4d59bce2008-10-02 16:48:10 +02002290 __set_bit(DQ_LASTSET_B + QIF_SPACE_B, &dquot->dq_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291 }
2292 if (di->dqb_valid & QIF_BLIMITS) {
Jan Kara12095462008-08-20 14:45:12 +02002293 dm->dqb_bsoftlimit = qbtos(di->dqb_bsoftlimit);
2294 dm->dqb_bhardlimit = qbtos(di->dqb_bhardlimit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295 check_blim = 1;
Jan Kara4d59bce2008-10-02 16:48:10 +02002296 __set_bit(DQ_LASTSET_B + QIF_BLIMITS_B, &dquot->dq_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297 }
2298 if (di->dqb_valid & QIF_INODES) {
2299 dm->dqb_curinodes = di->dqb_curinodes;
2300 check_ilim = 1;
Jan Kara4d59bce2008-10-02 16:48:10 +02002301 __set_bit(DQ_LASTSET_B + QIF_INODES_B, &dquot->dq_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302 }
2303 if (di->dqb_valid & QIF_ILIMITS) {
2304 dm->dqb_isoftlimit = di->dqb_isoftlimit;
2305 dm->dqb_ihardlimit = di->dqb_ihardlimit;
2306 check_ilim = 1;
Jan Kara4d59bce2008-10-02 16:48:10 +02002307 __set_bit(DQ_LASTSET_B + QIF_ILIMITS_B, &dquot->dq_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308 }
Jan Kara4d59bce2008-10-02 16:48:10 +02002309 if (di->dqb_valid & QIF_BTIME) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310 dm->dqb_btime = di->dqb_btime;
Jan Karae04a88a92009-01-07 18:07:29 -08002311 check_blim = 1;
Jan Kara4d59bce2008-10-02 16:48:10 +02002312 __set_bit(DQ_LASTSET_B + QIF_BTIME_B, &dquot->dq_flags);
2313 }
2314 if (di->dqb_valid & QIF_ITIME) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315 dm->dqb_itime = di->dqb_itime;
Jan Karae04a88a92009-01-07 18:07:29 -08002316 check_ilim = 1;
Jan Kara4d59bce2008-10-02 16:48:10 +02002317 __set_bit(DQ_LASTSET_B + QIF_ITIME_B, &dquot->dq_flags);
2318 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319
2320 if (check_blim) {
Jan Kara12095462008-08-20 14:45:12 +02002321 if (!dm->dqb_bsoftlimit || dm->dqb_curspace < dm->dqb_bsoftlimit) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322 dm->dqb_btime = 0;
2323 clear_bit(DQ_BLKS_B, &dquot->dq_flags);
2324 }
2325 else if (!(di->dqb_valid & QIF_BTIME)) /* Set grace only if user hasn't provided his own... */
Andrew Perepechko338bf9a2008-04-28 02:14:31 -07002326 dm->dqb_btime = get_seconds() + dqi->dqi_bgrace;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327 }
2328 if (check_ilim) {
2329 if (!dm->dqb_isoftlimit || dm->dqb_curinodes < dm->dqb_isoftlimit) {
2330 dm->dqb_itime = 0;
2331 clear_bit(DQ_INODES_B, &dquot->dq_flags);
2332 }
2333 else if (!(di->dqb_valid & QIF_ITIME)) /* Set grace only if user hasn't provided his own... */
Andrew Perepechko338bf9a2008-04-28 02:14:31 -07002334 dm->dqb_itime = get_seconds() + dqi->dqi_igrace;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335 }
2336 if (dm->dqb_bhardlimit || dm->dqb_bsoftlimit || dm->dqb_ihardlimit || dm->dqb_isoftlimit)
2337 clear_bit(DQ_FAKE_B, &dquot->dq_flags);
2338 else
2339 set_bit(DQ_FAKE_B, &dquot->dq_flags);
2340 spin_unlock(&dq_data_lock);
2341 mark_dquot_dirty(dquot);
Andrew Perepechko338bf9a2008-04-28 02:14:31 -07002342
2343 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344}
2345
2346int vfs_set_dqblk(struct super_block *sb, int type, qid_t id, struct if_dqblk *di)
2347{
2348 struct dquot *dquot;
Andrew Perepechko338bf9a2008-04-28 02:14:31 -07002349 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350
Jan Karaf55abc02008-08-20 17:50:32 +02002351 dquot = dqget(sb, id, type);
2352 if (!dquot) {
2353 rc = -ESRCH;
2354 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002355 }
Andrew Perepechko338bf9a2008-04-28 02:14:31 -07002356 rc = do_set_dqblk(dquot, di);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357 dqput(dquot);
Jan Karaf55abc02008-08-20 17:50:32 +02002358out:
Andrew Perepechko338bf9a2008-04-28 02:14:31 -07002359 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360}
Mingming Cao08d03502009-01-14 16:19:32 +01002361EXPORT_SYMBOL(vfs_set_dqblk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362
2363/* Generic routine for getting common part of quota file information */
2364int vfs_get_dqinfo(struct super_block *sb, int type, struct if_dqinfo *ii)
2365{
2366 struct mem_dqinfo *mi;
2367
Ingo Molnard3be9152006-03-23 03:00:29 -08002368 mutex_lock(&sb_dqopt(sb)->dqonoff_mutex);
Jan Karaf55abc02008-08-20 17:50:32 +02002369 if (!sb_has_quota_active(sb, type)) {
Ingo Molnard3be9152006-03-23 03:00:29 -08002370 mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371 return -ESRCH;
2372 }
2373 mi = sb_dqopt(sb)->info + type;
2374 spin_lock(&dq_data_lock);
2375 ii->dqi_bgrace = mi->dqi_bgrace;
2376 ii->dqi_igrace = mi->dqi_igrace;
2377 ii->dqi_flags = mi->dqi_flags & DQF_MASK;
2378 ii->dqi_valid = IIF_ALL;
2379 spin_unlock(&dq_data_lock);
Ingo Molnard3be9152006-03-23 03:00:29 -08002380 mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381 return 0;
2382}
Mingming Cao08d03502009-01-14 16:19:32 +01002383EXPORT_SYMBOL(vfs_get_dqinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002384
2385/* Generic routine for setting common part of quota file information */
2386int vfs_set_dqinfo(struct super_block *sb, int type, struct if_dqinfo *ii)
2387{
2388 struct mem_dqinfo *mi;
Jan Karaf55abc02008-08-20 17:50:32 +02002389 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002390
Ingo Molnard3be9152006-03-23 03:00:29 -08002391 mutex_lock(&sb_dqopt(sb)->dqonoff_mutex);
Jan Karaf55abc02008-08-20 17:50:32 +02002392 if (!sb_has_quota_active(sb, type)) {
2393 err = -ESRCH;
2394 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002395 }
2396 mi = sb_dqopt(sb)->info + type;
2397 spin_lock(&dq_data_lock);
2398 if (ii->dqi_valid & IIF_BGRACE)
2399 mi->dqi_bgrace = ii->dqi_bgrace;
2400 if (ii->dqi_valid & IIF_IGRACE)
2401 mi->dqi_igrace = ii->dqi_igrace;
2402 if (ii->dqi_valid & IIF_FLAGS)
2403 mi->dqi_flags = (mi->dqi_flags & ~DQF_MASK) | (ii->dqi_flags & DQF_MASK);
2404 spin_unlock(&dq_data_lock);
2405 mark_info_dirty(sb, type);
2406 /* Force write to disk */
2407 sb->dq_op->write_info(sb, type);
Jan Karaf55abc02008-08-20 17:50:32 +02002408out:
Ingo Molnard3be9152006-03-23 03:00:29 -08002409 mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
Jan Karaf55abc02008-08-20 17:50:32 +02002410 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002411}
Mingming Cao08d03502009-01-14 16:19:32 +01002412EXPORT_SYMBOL(vfs_set_dqinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002413
2414struct quotactl_ops vfs_quotactl_ops = {
2415 .quota_on = vfs_quota_on,
2416 .quota_off = vfs_quota_off,
2417 .quota_sync = vfs_quota_sync,
2418 .get_info = vfs_get_dqinfo,
2419 .set_info = vfs_set_dqinfo,
2420 .get_dqblk = vfs_get_dqblk,
2421 .set_dqblk = vfs_set_dqblk
2422};
2423
2424static ctl_table fs_dqstats_table[] = {
2425 {
2426 .ctl_name = FS_DQ_LOOKUPS,
2427 .procname = "lookups",
2428 .data = &dqstats.lookups,
2429 .maxlen = sizeof(int),
2430 .mode = 0444,
2431 .proc_handler = &proc_dointvec,
2432 },
2433 {
2434 .ctl_name = FS_DQ_DROPS,
2435 .procname = "drops",
2436 .data = &dqstats.drops,
2437 .maxlen = sizeof(int),
2438 .mode = 0444,
2439 .proc_handler = &proc_dointvec,
2440 },
2441 {
2442 .ctl_name = FS_DQ_READS,
2443 .procname = "reads",
2444 .data = &dqstats.reads,
2445 .maxlen = sizeof(int),
2446 .mode = 0444,
2447 .proc_handler = &proc_dointvec,
2448 },
2449 {
2450 .ctl_name = FS_DQ_WRITES,
2451 .procname = "writes",
2452 .data = &dqstats.writes,
2453 .maxlen = sizeof(int),
2454 .mode = 0444,
2455 .proc_handler = &proc_dointvec,
2456 },
2457 {
2458 .ctl_name = FS_DQ_CACHE_HITS,
2459 .procname = "cache_hits",
2460 .data = &dqstats.cache_hits,
2461 .maxlen = sizeof(int),
2462 .mode = 0444,
2463 .proc_handler = &proc_dointvec,
2464 },
2465 {
2466 .ctl_name = FS_DQ_ALLOCATED,
2467 .procname = "allocated_dquots",
2468 .data = &dqstats.allocated_dquots,
2469 .maxlen = sizeof(int),
2470 .mode = 0444,
2471 .proc_handler = &proc_dointvec,
2472 },
2473 {
2474 .ctl_name = FS_DQ_FREE,
2475 .procname = "free_dquots",
2476 .data = &dqstats.free_dquots,
2477 .maxlen = sizeof(int),
2478 .mode = 0444,
2479 .proc_handler = &proc_dointvec,
2480 },
2481 {
2482 .ctl_name = FS_DQ_SYNCS,
2483 .procname = "syncs",
2484 .data = &dqstats.syncs,
2485 .maxlen = sizeof(int),
2486 .mode = 0444,
2487 .proc_handler = &proc_dointvec,
2488 },
Jan Kara8e893462007-10-16 23:29:31 -07002489#ifdef CONFIG_PRINT_QUOTA_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07002490 {
2491 .ctl_name = FS_DQ_WARNINGS,
2492 .procname = "warnings",
2493 .data = &flag_print_warnings,
2494 .maxlen = sizeof(int),
2495 .mode = 0644,
2496 .proc_handler = &proc_dointvec,
2497 },
Jan Kara8e893462007-10-16 23:29:31 -07002498#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002499 { .ctl_name = 0 },
2500};
2501
2502static ctl_table fs_table[] = {
2503 {
2504 .ctl_name = FS_DQSTATS,
2505 .procname = "quota",
2506 .mode = 0555,
2507 .child = fs_dqstats_table,
2508 },
2509 { .ctl_name = 0 },
2510};
2511
2512static ctl_table sys_table[] = {
2513 {
2514 .ctl_name = CTL_FS,
2515 .procname = "fs",
2516 .mode = 0555,
2517 .child = fs_table,
2518 },
2519 { .ctl_name = 0 },
2520};
2521
2522static int __init dquot_init(void)
2523{
2524 int i;
2525 unsigned long nr_hash, order;
2526
2527 printk(KERN_NOTICE "VFS: Disk quotas %s\n", __DQUOT_VERSION__);
2528
Eric W. Biederman0b4d4142007-02-14 00:34:09 -08002529 register_sysctl_table(sys_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530
Paul Mundt20c2df82007-07-20 10:11:58 +09002531 dquot_cachep = kmem_cache_create("dquot",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002532 sizeof(struct dquot), sizeof(unsigned long) * 4,
Paul Jacksonfffb60f2006-03-24 03:16:06 -08002533 (SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT|
2534 SLAB_MEM_SPREAD|SLAB_PANIC),
Paul Mundt20c2df82007-07-20 10:11:58 +09002535 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002536
2537 order = 0;
2538 dquot_hash = (struct hlist_head *)__get_free_pages(GFP_ATOMIC, order);
2539 if (!dquot_hash)
2540 panic("Cannot create dquot hash table");
2541
2542 /* Find power-of-two hlist_heads which can fit into allocation */
2543 nr_hash = (1UL << order) * PAGE_SIZE / sizeof(struct hlist_head);
2544 dq_hash_bits = 0;
2545 do {
2546 dq_hash_bits++;
2547 } while (nr_hash >> dq_hash_bits);
2548 dq_hash_bits--;
2549
2550 nr_hash = 1UL << dq_hash_bits;
2551 dq_hash_mask = nr_hash - 1;
2552 for (i = 0; i < nr_hash; i++)
2553 INIT_HLIST_HEAD(dquot_hash + i);
2554
2555 printk("Dquot-cache hash table entries: %ld (order %ld, %ld bytes)\n",
2556 nr_hash, order, (PAGE_SIZE << order));
2557
Rusty Russell8e1f9362007-07-17 04:03:17 -07002558 register_shrinker(&dqcache_shrinker);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002559
Jan Kara8e893462007-10-16 23:29:31 -07002560#ifdef CONFIG_QUOTA_NETLINK_INTERFACE
2561 if (genl_register_family(&quota_genl_family) != 0)
2562 printk(KERN_ERR "VFS: Failed to create quota netlink interface.\n");
2563#endif
2564
Linus Torvalds1da177e2005-04-16 15:20:36 -07002565 return 0;
2566}
2567module_init(dquot_init);