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.
12 * Author: Marco van Wieringen <mvw@planets.elm.net>
14 * Fixes: Dmitry Gorodchanin <pgmdsg@ibi.com>, 11 Feb 96
16 * Revised list management to avoid races
17 * -- Bill Hawes, <whawes@star.net>, 9/98
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
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
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
40 * Added dynamic quota structure allocation
41 * Jan Kara <jack@suse.cz> 12/2000
43 * Rewritten quota interface. Implemented new quota format and
44 * formats registering.
45 * Jan Kara, <jack@suse.cz>, 2001,2002
48 * Jan Kara, <jack@suse.cz>, 10/2002
50 * Added journalled quota support, fix lock inversion problems
51 * Jan Kara, <jack@suse.cz>, 2003,2004
53 * (C) Copyright 1994 - 1997 Marco van Wieringen
56 #include <linux/errno.h>
57 #include <linux/kernel.h>
59 #include <linux/mount.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>
70 #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>
77 #include <linux/capability.h>
78 #include <linux/quotaops.h>
79 #include <linux/writeback.h> /* for inode_lock, oddly enough.. */
80 #ifdef CONFIG_QUOTA_NETLINK_INTERFACE
81 #include <net/netlink.h>
82 #include <net/genetlink.h>
85 #include <asm/uaccess.h>
87 #define __DQUOT_PARANOIA
90 * 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.
94 * i_blocks and i_bytes updates itself are guarded by i_lock acquired directly
95 * 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.
99 * The spinlock ordering is hence: dq_data_lock > dq_list_lock > i_lock,
100 * dq_list_lock > dq_state_lock
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
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
109 * for altering the flag i_mutex is also needed).
111 * Each dquot has its dq_lock mutex. Locked dquots might not be referenced
112 * 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.
120 * Lock ordering (including related VFS locks) is the following:
121 * i_mutex > dqonoff_sem > journal_lock > dqptr_sem > dquot->dq_lock >
123 * 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.
129 * i_mutex on quota files is special (it's below dqio_mutex)
132 static __cacheline_aligned_in_smp DEFINE_SPINLOCK(dq_list_lock);
133 static __cacheline_aligned_in_smp DEFINE_SPINLOCK(dq_state_lock);
134 __cacheline_aligned_in_smp DEFINE_SPINLOCK(dq_data_lock);
135 EXPORT_SYMBOL(dq_data_lock);
137 static char *quotatypes[] = INITQFNAMES;
138 static struct quota_format_type *quota_formats; /* List of registered formats */
139 static struct quota_module_name module_names[] = INIT_QUOTA_MODULE_NAMES;
141 /* SLAB cache for dquot structures */
142 static struct kmem_cache *dquot_cachep;
144 int register_quota_format(struct quota_format_type *fmt)
146 spin_lock(&dq_list_lock);
147 fmt->qf_next = quota_formats;
149 spin_unlock(&dq_list_lock);
152 EXPORT_SYMBOL(register_quota_format);
154 void unregister_quota_format(struct quota_format_type *fmt)
156 struct quota_format_type **actqf;
158 spin_lock(&dq_list_lock);
159 for (actqf = "a_formats; *actqf && *actqf != fmt; actqf = &(*actqf)->qf_next);
161 *actqf = (*actqf)->qf_next;
162 spin_unlock(&dq_list_lock);
164 EXPORT_SYMBOL(unregister_quota_format);
166 static struct quota_format_type *find_quota_format(int id)
168 struct quota_format_type *actqf;
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)) {
175 spin_unlock(&dq_list_lock);
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))
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))
186 spin_unlock(&dq_list_lock);
190 static void put_quota_format(struct quota_format_type *fmt)
192 module_put(fmt->qf_owner);
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.
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.
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.
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.
215 static LIST_HEAD(inuse_list);
216 static LIST_HEAD(free_dquots);
217 static unsigned int dq_hash_bits, dq_hash_mask;
218 static struct hlist_head *dquot_hash;
220 struct dqstats dqstats;
221 EXPORT_SYMBOL(dqstats);
223 static inline unsigned int
224 hashfn(const struct super_block *sb, unsigned int id, int type)
228 tmp = (((unsigned long)sb>>L1_CACHE_SHIFT) ^ id) * (MAXQUOTAS - type);
229 return (tmp + (tmp >> dq_hash_bits)) & dq_hash_mask;
233 * Following list functions expect dq_list_lock to be held
235 static inline void insert_dquot_hash(struct dquot *dquot)
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);
241 static inline void remove_dquot_hash(struct dquot *dquot)
243 hlist_del_init(&dquot->dq_hash);
246 static struct dquot *find_dquot(unsigned int hashent, struct super_block *sb,
247 unsigned int id, int type)
249 struct hlist_node *node;
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)
260 /* Add a dquot to the tail of the free list */
261 static inline void put_dquot_last(struct dquot *dquot)
263 list_add_tail(&dquot->dq_free, &free_dquots);
264 dqstats.free_dquots++;
267 static inline void remove_free_dquot(struct dquot *dquot)
269 if (list_empty(&dquot->dq_free))
271 list_del_init(&dquot->dq_free);
272 dqstats.free_dquots--;
275 static inline void put_inuse(struct dquot *dquot)
277 /* We add to the back of inuse list so we don't have to restart
278 * when traversing this list and we block */
279 list_add_tail(&dquot->dq_inuse, &inuse_list);
280 dqstats.allocated_dquots++;
283 static inline void remove_inuse(struct dquot *dquot)
285 dqstats.allocated_dquots--;
286 list_del(&dquot->dq_inuse);
289 * End of list functions needing dq_list_lock
292 static void wait_on_dquot(struct dquot *dquot)
294 mutex_lock(&dquot->dq_lock);
295 mutex_unlock(&dquot->dq_lock);
298 static inline int dquot_dirty(struct dquot *dquot)
300 return test_bit(DQ_MOD_B, &dquot->dq_flags);
303 static inline int mark_dquot_dirty(struct dquot *dquot)
305 return dquot->dq_sb->dq_op->mark_dirty(dquot);
308 int dquot_mark_dquot_dirty(struct dquot *dquot)
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);
317 EXPORT_SYMBOL(dquot_mark_dquot_dirty);
319 /* This function needs dq_list_lock */
320 static inline int clear_dquot_dirty(struct dquot *dquot)
322 if (!test_and_clear_bit(DQ_MOD_B, &dquot->dq_flags))
324 list_del_init(&dquot->dq_dirty);
328 void mark_info_dirty(struct super_block *sb, int type)
330 set_bit(DQF_INFO_DIRTY_B, &sb_dqopt(sb)->info[type].dqi_flags);
332 EXPORT_SYMBOL(mark_info_dirty);
335 * Read dquot from disk and alloc space for it
338 int dquot_acquire(struct dquot *dquot)
340 int ret = 0, ret2 = 0;
341 struct quota_info *dqopt = sb_dqopt(dquot->dq_sb);
343 mutex_lock(&dquot->dq_lock);
344 mutex_lock(&dqopt->dqio_mutex);
345 if (!test_bit(DQ_READ_B, &dquot->dq_flags))
346 ret = dqopt->ops[dquot->dq_type]->read_dqblk(dquot);
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);
363 set_bit(DQ_ACTIVE_B, &dquot->dq_flags);
365 mutex_unlock(&dqopt->dqio_mutex);
366 mutex_unlock(&dquot->dq_lock);
369 EXPORT_SYMBOL(dquot_acquire);
372 * Write dquot to disk
374 int dquot_commit(struct dquot *dquot)
376 int ret = 0, ret2 = 0;
377 struct quota_info *dqopt = sb_dqopt(dquot->dq_sb);
379 mutex_lock(&dqopt->dqio_mutex);
380 spin_lock(&dq_list_lock);
381 if (!clear_dquot_dirty(dquot)) {
382 spin_unlock(&dq_list_lock);
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);
396 mutex_unlock(&dqopt->dqio_mutex);
399 EXPORT_SYMBOL(dquot_commit);
404 int dquot_release(struct dquot *dquot)
406 int ret = 0, ret2 = 0;
407 struct quota_info *dqopt = sb_dqopt(dquot->dq_sb);
409 mutex_lock(&dquot->dq_lock);
410 /* Check whether we are not racing with some other dqget() */
411 if (atomic_read(&dquot->dq_count) > 1)
413 mutex_lock(&dqopt->dqio_mutex);
414 if (dqopt->ops[dquot->dq_type]->release_dqblk) {
415 ret = dqopt->ops[dquot->dq_type]->release_dqblk(dquot);
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);
422 clear_bit(DQ_ACTIVE_B, &dquot->dq_flags);
423 mutex_unlock(&dqopt->dqio_mutex);
425 mutex_unlock(&dquot->dq_lock);
428 EXPORT_SYMBOL(dquot_release);
430 void dquot_destroy(struct dquot *dquot)
432 kmem_cache_free(dquot_cachep, dquot);
434 EXPORT_SYMBOL(dquot_destroy);
436 static inline void do_destroy_dquot(struct dquot *dquot)
438 dquot->dq_sb->dq_op->destroy_dquot(dquot);
441 /* 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
443 * 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
445 * list) or parallel quotactl call. We have to wait for such users.
447 static void invalidate_dquots(struct super_block *sb, int type)
449 struct dquot *dquot, *tmp;
452 spin_lock(&dq_list_lock);
453 list_for_each_entry_safe(dquot, tmp, &inuse_list, dq_inuse) {
454 if (dquot->dq_sb != sb)
456 if (dquot->dq_type != type)
458 /* Wait for dquot users */
459 if (atomic_read(&dquot->dq_count)) {
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
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
473 if (atomic_read(&dquot->dq_count) > 1)
475 finish_wait(&dquot->dq_wait_unused, &wait);
477 /* At this moment dquot() need not exist (it could be
478 * reclaimed by prune_dqcache(). Hence we must
483 * Quota now has no users and it has been written on last
486 remove_dquot_hash(dquot);
487 remove_free_dquot(dquot);
489 do_destroy_dquot(dquot);
491 spin_unlock(&dq_list_lock);
494 /* Call callback for every active dquot on given filesystem */
495 int dquot_scan_active(struct super_block *sb,
496 int (*fn)(struct dquot *dquot, unsigned long priv),
499 struct dquot *dquot, *old_dquot = NULL;
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))
507 if (dquot->dq_sb != sb)
509 /* Now we have active dquot so we can just increase use count */
510 atomic_inc(&dquot->dq_count);
512 spin_unlock(&dq_list_lock);
515 ret = fn(dquot, priv);
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 */
522 spin_unlock(&dq_list_lock);
525 mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
528 EXPORT_SYMBOL(dquot_scan_active);
530 int vfs_quota_sync(struct super_block *sb, int type)
532 struct list_head *dirty;
534 struct quota_info *dqopt = sb_dqopt(sb);
537 mutex_lock(&dqopt->dqonoff_mutex);
538 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
539 if (type != -1 && cnt != type)
541 if (!sb_has_quota_active(sb, cnt))
543 spin_lock(&dq_list_lock);
544 dirty = &dqopt->info[cnt].dqi_dirty_list;
545 while (!list_empty(dirty)) {
546 dquot = list_first_entry(dirty, struct dquot, dq_dirty);
547 /* Dirty and inactive can be only bad dquot... */
548 if (!test_bit(DQ_ACTIVE_B, &dquot->dq_flags)) {
549 clear_dquot_dirty(dquot);
552 /* Now we have active dquot from which someone is
553 * holding reference so we can safely just increase
555 atomic_inc(&dquot->dq_count);
557 spin_unlock(&dq_list_lock);
558 sb->dq_op->write_dquot(dquot);
560 spin_lock(&dq_list_lock);
562 spin_unlock(&dq_list_lock);
565 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
566 if ((cnt == type || type == -1) && sb_has_quota_active(sb, cnt)
567 && info_dirty(&dqopt->info[cnt]))
568 sb->dq_op->write_info(sb, cnt);
569 spin_lock(&dq_list_lock);
571 spin_unlock(&dq_list_lock);
572 mutex_unlock(&dqopt->dqonoff_mutex);
576 EXPORT_SYMBOL(vfs_quota_sync);
578 /* Free unused dquots from cache */
579 static void prune_dqcache(int count)
581 struct list_head *head;
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);
590 do_destroy_dquot(dquot);
592 head = free_dquots.prev;
597 * This is called from kswapd when we think we need some
601 static int shrink_dqcache_memory(int nr, gfp_t gfp_mask)
604 spin_lock(&dq_list_lock);
606 spin_unlock(&dq_list_lock);
608 return (dqstats.free_dquots / 100) * sysctl_vfs_cache_pressure;
611 static struct shrinker dqcache_shrinker = {
612 .shrink = shrink_dqcache_memory,
613 .seeks = DEFAULT_SEEKS,
617 * Put reference to dquot
618 * NOTE: If you change this function please check whether dqput_blocks() works right...
620 void dqput(struct dquot *dquot)
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",
631 quotatypes[dquot->dq_type],
637 spin_lock(&dq_list_lock);
639 spin_unlock(&dq_list_lock);
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);
645 /* Releasing dquot during quotaoff phase? */
646 if (!sb_has_quota_active(dquot->dq_sb, dquot->dq_type) &&
647 atomic_read(&dquot->dq_count) == 1)
648 wake_up(&dquot->dq_wait_unused);
649 spin_unlock(&dq_list_lock);
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 */
656 ret = dquot->dq_sb->dq_op->write_dquot(dquot);
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);
662 * We clear dirty bit anyway, so that we avoid
665 spin_lock(&dq_list_lock);
666 clear_dquot_dirty(dquot);
667 spin_unlock(&dq_list_lock);
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);
678 atomic_dec(&dquot->dq_count);
679 #ifdef __DQUOT_PARANOIA
681 BUG_ON(!list_empty(&dquot->dq_free));
683 put_dquot_last(dquot);
684 spin_unlock(&dq_list_lock);
686 EXPORT_SYMBOL(dqput);
688 struct dquot *dquot_alloc(struct super_block *sb, int type)
690 return kmem_cache_zalloc(dquot_cachep, GFP_NOFS);
692 EXPORT_SYMBOL(dquot_alloc);
694 static struct dquot *get_empty_dquot(struct super_block *sb, int type)
698 dquot = sb->dq_op->alloc_dquot(sb, type);
702 mutex_init(&dquot->dq_lock);
703 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);
707 init_waitqueue_head(&dquot->dq_wait_unused);
709 dquot->dq_type = type;
710 atomic_set(&dquot->dq_count, 1);
716 * Get reference to dquot
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
723 struct dquot *dqget(struct super_block *sb, unsigned int id, int type)
725 unsigned int hashent = hashfn(sb, id, type);
726 struct dquot *dquot = NULL, *empty = NULL;
728 if (!sb_has_quota_active(sb, type))
731 spin_lock(&dq_list_lock);
732 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);
738 spin_unlock(&dq_state_lock);
740 dquot = find_dquot(hashent, sb, id, type);
743 spin_unlock(&dq_list_lock);
744 empty = get_empty_dquot(sb, type);
746 schedule(); /* Try to wait for a moment... */
752 /* all dquots go on the inuse_list */
754 /* hash it first so it can be found */
755 insert_dquot_hash(dquot);
757 spin_unlock(&dq_list_lock);
759 if (!atomic_read(&dquot->dq_count))
760 remove_free_dquot(dquot);
761 atomic_inc(&dquot->dq_count);
762 dqstats.cache_hits++;
764 spin_unlock(&dq_list_lock);
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) {
775 #ifdef __DQUOT_PARANOIA
776 BUG_ON(!dquot->dq_sb); /* Has somebody invalidated entry under us? */
780 do_destroy_dquot(empty);
784 EXPORT_SYMBOL(dqget);
786 static int dqinit_needed(struct inode *inode, int type)
790 if (IS_NOQUOTA(inode))
793 return !inode->i_dquot[type];
794 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
795 if (!inode->i_dquot[cnt])
800 /* This routine is guarded by dqonoff_mutex mutex */
801 static void add_dquot_ref(struct super_block *sb, int type)
803 struct inode *inode, *old_inode = NULL;
805 spin_lock(&inode_lock);
806 list_for_each_entry(inode, &sb->s_inodes, i_sb_list) {
807 if (!atomic_read(&inode->i_writecount))
809 if (!dqinit_needed(inode, type))
811 if (inode->i_state & (I_FREEING|I_WILL_FREE))
815 spin_unlock(&inode_lock);
818 sb->dq_op->initialize(inode, type);
819 /* 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. */
825 spin_lock(&inode_lock);
827 spin_unlock(&inode_lock);
831 /* Return 0 if dqput() won't block (note that 1 doesn't necessarily mean blocking) */
832 static inline int dqput_blocks(struct dquot *dquot)
834 if (atomic_read(&dquot->dq_count) <= 1)
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... */
841 static int remove_inode_dquot_ref(struct inode *inode, int type,
842 struct list_head *tofree_head)
844 struct dquot *dquot = inode->i_dquot[type];
846 inode->i_dquot[type] = NULL;
848 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));
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);
859 dqput(dquot); /* We have guaranteed we won't block */
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 */
866 static void put_dquot_list(struct list_head *tofree_head)
868 struct list_head *act_head;
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... */
881 static void remove_dquot_ref(struct super_block *sb, int type,
882 struct list_head *tofree_head)
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);
891 spin_unlock(&inode_lock);
894 /* Gather all references from inodes and drop them */
895 static void drop_dquot_ref(struct super_block *sb, int type)
897 LIST_HEAD(tofree_head);
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);
907 static inline void dquot_incr_inodes(struct dquot *dquot, qsize_t number)
909 dquot->dq_dqb.dqb_curinodes += number;
912 static inline void dquot_incr_space(struct dquot *dquot, qsize_t number)
914 dquot->dq_dqb.dqb_curspace += number;
917 static inline void dquot_resv_space(struct dquot *dquot, qsize_t number)
919 dquot->dq_dqb.dqb_rsvspace += number;
923 * Claim reserved quota space
925 static void dquot_claim_reserved_space(struct dquot *dquot,
928 WARN_ON(dquot->dq_dqb.dqb_rsvspace < number);
929 dquot->dq_dqb.dqb_curspace += number;
930 dquot->dq_dqb.dqb_rsvspace -= number;
934 void dquot_free_reserved_space(struct dquot *dquot, qsize_t number)
936 dquot->dq_dqb.dqb_rsvspace -= number;
939 static void dquot_decr_inodes(struct dquot *dquot, qsize_t number)
941 if (sb_dqopt(dquot->dq_sb)->flags & DQUOT_NEGATIVE_USAGE ||
942 dquot->dq_dqb.dqb_curinodes >= number)
943 dquot->dq_dqb.dqb_curinodes -= number;
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);
951 static void dquot_decr_space(struct dquot *dquot, qsize_t number)
953 if (sb_dqopt(dquot->dq_sb)->flags & DQUOT_NEGATIVE_USAGE ||
954 dquot->dq_dqb.dqb_curspace >= number)
955 dquot->dq_dqb.dqb_curspace -= number;
957 dquot->dq_dqb.dqb_curspace = 0;
958 if (dquot->dq_dqb.dqb_curspace <= dquot->dq_dqb.dqb_bsoftlimit)
959 dquot->dq_dqb.dqb_btime = (time_t) 0;
960 clear_bit(DQ_BLKS_B, &dquot->dq_flags);
963 static int warning_issued(struct dquot *dquot, const int warntype)
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);
972 return test_and_set_bit(flag, &dquot->dq_flags);
975 #ifdef CONFIG_PRINT_QUOTA_WARNING
976 static int flag_print_warnings = 1;
978 static int need_print_warning(struct dquot *dquot)
980 if (!flag_print_warnings)
983 switch (dquot->dq_type) {
985 return current_fsuid() == dquot->dq_id;
987 return in_group_p(dquot->dq_id);
992 /* Print warning to user which exceeded quota */
993 static void print_warning(struct dquot *dquot, const int warntype)
996 struct tty_struct *tty;
998 if (warntype == QUOTA_NL_IHARDBELOW ||
999 warntype == QUOTA_NL_ISOFTBELOW ||
1000 warntype == QUOTA_NL_BHARDBELOW ||
1001 warntype == QUOTA_NL_BSOFTBELOW || !need_print_warning(dquot))
1004 tty = get_current_tty();
1007 tty_write_message(tty, dquot->dq_sb->s_id);
1008 if (warntype == QUOTA_NL_ISOFTWARN || warntype == QUOTA_NL_BSOFTWARN)
1009 tty_write_message(tty, ": warning, ");
1011 tty_write_message(tty, ": write failed, ");
1012 tty_write_message(tty, quotatypes[dquot->dq_type]);
1014 case QUOTA_NL_IHARDWARN:
1015 msg = " file limit reached.\r\n";
1017 case QUOTA_NL_ISOFTLONGWARN:
1018 msg = " file quota exceeded too long.\r\n";
1020 case QUOTA_NL_ISOFTWARN:
1021 msg = " file quota exceeded.\r\n";
1023 case QUOTA_NL_BHARDWARN:
1024 msg = " block limit reached.\r\n";
1026 case QUOTA_NL_BSOFTLONGWARN:
1027 msg = " block quota exceeded too long.\r\n";
1029 case QUOTA_NL_BSOFTWARN:
1030 msg = " block quota exceeded.\r\n";
1033 tty_write_message(tty, msg);
1038 #ifdef CONFIG_QUOTA_NETLINK_INTERFACE
1040 /* Netlink family structure for quota */
1041 static struct genl_family quota_genl_family = {
1042 .id = GENL_ID_GENERATE,
1044 .name = "VFS_DQUOT",
1046 .maxattr = QUOTA_NL_A_MAX,
1049 /* Send warning to userspace about user which exceeded quota */
1050 static void send_warning(const struct dquot *dquot, const char warntype)
1052 static atomic_t seq;
1053 struct sk_buff *skb;
1056 int msg_size = 4 * nla_total_size(sizeof(u32)) +
1057 2 * nla_total_size(sizeof(u64));
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. */
1062 skb = genlmsg_new(msg_size, GFP_NOFS);
1065 "VFS: Not enough memory to send quota warning.\n");
1068 msg_head = genlmsg_put(skb, 0, atomic_add_return(1, &seq),
1069 "a_genl_family, 0, QUOTA_NL_C_WARNING);
1072 "VFS: Cannot store netlink header in quota warning.\n");
1075 ret = nla_put_u32(skb, QUOTA_NL_A_QTYPE, dquot->dq_type);
1078 ret = nla_put_u64(skb, QUOTA_NL_A_EXCESS_ID, dquot->dq_id);
1081 ret = nla_put_u32(skb, QUOTA_NL_A_WARNING, warntype);
1084 ret = nla_put_u32(skb, QUOTA_NL_A_DEV_MAJOR,
1085 MAJOR(dquot->dq_sb->s_dev));
1088 ret = nla_put_u32(skb, QUOTA_NL_A_DEV_MINOR,
1089 MINOR(dquot->dq_sb->s_dev));
1092 ret = nla_put_u64(skb, QUOTA_NL_A_CAUSED_ID, current_uid());
1095 genlmsg_end(skb, msg_head);
1097 ret = genlmsg_multicast(skb, 0, quota_genl_family.id, GFP_NOFS);
1098 if (ret < 0 && ret != -ESRCH)
1100 "VFS: Failed to send notification message: %d\n", ret);
1103 printk(KERN_ERR "VFS: Not enough space to compose quota message!\n");
1109 * Write warnings to the console and send warning messages over netlink.
1111 * Note that this function can sleep.
1113 static void flush_warnings(struct dquot *const *dquots, char *warntype)
1117 for (i = 0; i < MAXQUOTAS; i++)
1118 if (dquots[i] && warntype[i] != QUOTA_NL_NOWARN &&
1119 !warning_issued(dquots[i], warntype[i])) {
1120 #ifdef CONFIG_PRINT_QUOTA_WARNING
1121 print_warning(dquots[i], warntype[i]);
1123 #ifdef CONFIG_QUOTA_NETLINK_INTERFACE
1124 send_warning(dquots[i], warntype[i]);
1129 static int ignore_hardlimit(struct dquot *dquot)
1131 struct mem_dqinfo *info = &sb_dqopt(dquot->dq_sb)->info[dquot->dq_type];
1133 return capable(CAP_SYS_RESOURCE) &&
1134 (info->dqi_format->qf_fmt_id != QFMT_VFS_OLD || !(info->dqi_flags & V1_DQF_RSQUASH));
1137 /* needs dq_data_lock */
1138 static int check_idq(struct dquot *dquot, qsize_t inodes, char *warntype)
1140 *warntype = QUOTA_NL_NOWARN;
1141 if (!sb_has_quota_limits_enabled(dquot->dq_sb, dquot->dq_type) ||
1142 test_bit(DQ_FAKE_B, &dquot->dq_flags))
1145 if (dquot->dq_dqb.dqb_ihardlimit &&
1146 (dquot->dq_dqb.dqb_curinodes + inodes) > dquot->dq_dqb.dqb_ihardlimit &&
1147 !ignore_hardlimit(dquot)) {
1148 *warntype = QUOTA_NL_IHARDWARN;
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)) {
1156 *warntype = QUOTA_NL_ISOFTLONGWARN;
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) {
1163 *warntype = QUOTA_NL_ISOFTWARN;
1164 dquot->dq_dqb.dqb_itime = get_seconds() + sb_dqopt(dquot->dq_sb)->info[dquot->dq_type].dqi_igrace;
1170 /* needs dq_data_lock */
1171 static int check_bdq(struct dquot *dquot, qsize_t space, int prealloc, char *warntype)
1175 *warntype = QUOTA_NL_NOWARN;
1176 if (!sb_has_quota_limits_enabled(dquot->dq_sb, dquot->dq_type) ||
1177 test_bit(DQ_FAKE_B, &dquot->dq_flags))
1180 tspace = dquot->dq_dqb.dqb_curspace + dquot->dq_dqb.dqb_rsvspace
1183 if (dquot->dq_dqb.dqb_bhardlimit &&
1184 tspace > dquot->dq_dqb.dqb_bhardlimit &&
1185 !ignore_hardlimit(dquot)) {
1187 *warntype = QUOTA_NL_BHARDWARN;
1191 if (dquot->dq_dqb.dqb_bsoftlimit &&
1192 tspace > dquot->dq_dqb.dqb_bsoftlimit &&
1193 dquot->dq_dqb.dqb_btime && get_seconds() >= dquot->dq_dqb.dqb_btime &&
1194 !ignore_hardlimit(dquot)) {
1196 *warntype = QUOTA_NL_BSOFTLONGWARN;
1200 if (dquot->dq_dqb.dqb_bsoftlimit &&
1201 tspace > dquot->dq_dqb.dqb_bsoftlimit &&
1202 dquot->dq_dqb.dqb_btime == 0) {
1204 *warntype = QUOTA_NL_BSOFTWARN;
1205 dquot->dq_dqb.dqb_btime = get_seconds() + sb_dqopt(dquot->dq_sb)->info[dquot->dq_type].dqi_bgrace;
1209 * We don't allow preallocation to exceed softlimit so exceeding will
1218 static int info_idq_free(struct dquot *dquot, qsize_t inodes)
1220 if (test_bit(DQ_FAKE_B, &dquot->dq_flags) ||
1221 dquot->dq_dqb.dqb_curinodes <= dquot->dq_dqb.dqb_isoftlimit ||
1222 !sb_has_quota_limits_enabled(dquot->dq_sb, dquot->dq_type))
1223 return QUOTA_NL_NOWARN;
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;
1233 static int info_bdq_free(struct dquot *dquot, qsize_t space)
1235 if (test_bit(DQ_FAKE_B, &dquot->dq_flags) ||
1236 dquot->dq_dqb.dqb_curspace <= dquot->dq_dqb.dqb_bsoftlimit)
1237 return QUOTA_NL_NOWARN;
1239 if (dquot->dq_dqb.dqb_curspace - space <= dquot->dq_dqb.dqb_bsoftlimit)
1240 return QUOTA_NL_BSOFTBELOW;
1241 if (dquot->dq_dqb.dqb_curspace >= dquot->dq_dqb.dqb_bhardlimit &&
1242 dquot->dq_dqb.dqb_curspace - space < dquot->dq_dqb.dqb_bhardlimit)
1243 return QUOTA_NL_BHARDBELOW;
1244 return QUOTA_NL_NOWARN;
1247 * Initialize quota pointers in inode
1248 * We do things in a bit complicated way but by that we avoid calling
1249 * dqget() and thus filesystem callbacks under dqptr_sem.
1251 int dquot_initialize(struct inode *inode, int type)
1253 unsigned int id = 0;
1255 struct dquot *got[MAXQUOTAS] = { NULL, NULL };
1256 struct super_block *sb = inode->i_sb;
1258 /* First test before acquiring mutex - solves deadlocks when we
1259 * re-enter the quota code and are already holding the mutex */
1260 if (IS_NOQUOTA(inode))
1263 /* First get references to structures we might need. */
1264 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1265 if (type != -1 && cnt != type)
1275 got[cnt] = dqget(sb, id, cnt);
1278 down_write(&sb_dqopt(sb)->dqptr_sem);
1279 /* Having dqptr_sem we know NOQUOTA flags can't be altered... */
1280 if (IS_NOQUOTA(inode))
1282 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1283 if (type != -1 && cnt != type)
1285 /* Avoid races with quotaoff() */
1286 if (!sb_has_quota_active(sb, cnt))
1288 if (!inode->i_dquot[cnt]) {
1289 inode->i_dquot[cnt] = got[cnt];
1294 up_write(&sb_dqopt(sb)->dqptr_sem);
1295 /* Drop unused references */
1296 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1300 EXPORT_SYMBOL(dquot_initialize);
1303 * Release all quotas referenced by inode
1305 int dquot_drop(struct inode *inode)
1308 struct dquot *put[MAXQUOTAS];
1310 down_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
1311 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1312 put[cnt] = inode->i_dquot[cnt];
1313 inode->i_dquot[cnt] = NULL;
1315 up_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
1317 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1321 EXPORT_SYMBOL(dquot_drop);
1323 /* Wrapper to remove references to quota structures from inode */
1324 void vfs_dq_drop(struct inode *inode)
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) {
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++)
1338 if (inode->i_dquot[cnt])
1340 if (cnt < MAXQUOTAS)
1341 inode->i_sb->dq_op->drop(inode);
1344 EXPORT_SYMBOL(vfs_dq_drop);
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.
1356 * This operation can block, but only after everything is updated
1358 int __dquot_alloc_space(struct inode *inode, qsize_t number,
1359 int warn, int reserve)
1361 int cnt, ret = QUOTA_OK;
1362 char warntype[MAXQUOTAS];
1364 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1365 warntype[cnt] = QUOTA_NL_NOWARN;
1367 spin_lock(&dq_data_lock);
1368 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1369 if (!inode->i_dquot[cnt])
1371 if (check_bdq(inode->i_dquot[cnt], number, warn, warntype+cnt)
1377 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1378 if (!inode->i_dquot[cnt])
1381 dquot_resv_space(inode->i_dquot[cnt], number);
1383 dquot_incr_space(inode->i_dquot[cnt], number);
1386 inode_add_bytes(inode, number);
1388 spin_unlock(&dq_data_lock);
1389 flush_warnings(inode->i_dquot, warntype);
1393 int dquot_alloc_space(struct inode *inode, qsize_t number, int warn)
1395 int cnt, ret = QUOTA_OK;
1398 * First test before acquiring mutex - solves deadlocks when we
1399 * re-enter the quota code and are already holding the mutex
1401 if (IS_NOQUOTA(inode)) {
1402 inode_add_bytes(inode, number);
1406 down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1407 if (IS_NOQUOTA(inode)) {
1408 inode_add_bytes(inode, number);
1412 ret = __dquot_alloc_space(inode, number, warn, 0);
1413 if (ret == NO_QUOTA)
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]);
1421 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1425 EXPORT_SYMBOL(dquot_alloc_space);
1427 int dquot_reserve_space(struct inode *inode, qsize_t number, int warn)
1431 if (IS_NOQUOTA(inode))
1434 down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1435 if (IS_NOQUOTA(inode))
1438 ret = __dquot_alloc_space(inode, number, warn, 1);
1440 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1444 EXPORT_SYMBOL(dquot_reserve_space);
1447 * This operation can block, but only after everything is updated
1449 int dquot_alloc_inode(const struct inode *inode, qsize_t number)
1451 int cnt, ret = NO_QUOTA;
1452 char warntype[MAXQUOTAS];
1454 /* First test before acquiring mutex - solves deadlocks when we
1455 * re-enter the quota code and are already holding the mutex */
1456 if (IS_NOQUOTA(inode))
1458 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1459 warntype[cnt] = QUOTA_NL_NOWARN;
1460 down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1461 if (IS_NOQUOTA(inode)) {
1462 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1465 spin_lock(&dq_data_lock);
1466 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1467 if (!inode->i_dquot[cnt])
1469 if (check_idq(inode->i_dquot[cnt], number, warntype+cnt) == NO_QUOTA)
1473 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1474 if (!inode->i_dquot[cnt])
1476 dquot_incr_inodes(inode->i_dquot[cnt], number);
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]);
1486 flush_warnings(inode->i_dquot, warntype);
1487 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1490 EXPORT_SYMBOL(dquot_alloc_inode);
1492 int dquot_claim_space(struct inode *inode, qsize_t number)
1497 if (IS_NOQUOTA(inode)) {
1498 inode_add_bytes(inode, number);
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);
1509 spin_lock(&dq_data_lock);
1510 /* Claim reserved quotas to allocated quotas */
1511 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1512 if (inode->i_dquot[cnt])
1513 dquot_claim_reserved_space(inode->i_dquot[cnt],
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);
1527 EXPORT_SYMBOL(dquot_claim_space);
1530 * Release reserved quota space
1532 void dquot_release_reserved_space(struct inode *inode, qsize_t number)
1536 if (IS_NOQUOTA(inode))
1539 down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1540 if (IS_NOQUOTA(inode))
1543 spin_lock(&dq_data_lock);
1544 /* Release reserved dquots */
1545 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1546 if (inode->i_dquot[cnt])
1547 dquot_free_reserved_space(inode->i_dquot[cnt], number);
1549 spin_unlock(&dq_data_lock);
1552 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1556 EXPORT_SYMBOL(dquot_release_reserved_space);
1559 * This operation can block, but only after everything is updated
1561 int dquot_free_space(struct inode *inode, qsize_t number)
1564 char warntype[MAXQUOTAS];
1566 /* First test before acquiring mutex - solves deadlocks when we
1567 * re-enter the quota code and are already holding the mutex */
1568 if (IS_NOQUOTA(inode)) {
1570 inode_sub_bytes(inode, number);
1574 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);
1580 spin_lock(&dq_data_lock);
1581 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1582 if (!inode->i_dquot[cnt])
1584 warntype[cnt] = info_bdq_free(inode->i_dquot[cnt], number);
1585 dquot_decr_space(inode->i_dquot[cnt], number);
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]);
1593 flush_warnings(inode->i_dquot, warntype);
1594 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1597 EXPORT_SYMBOL(dquot_free_space);
1600 * This operation can block, but only after everything is updated
1602 int dquot_free_inode(const struct inode *inode, qsize_t number)
1605 char warntype[MAXQUOTAS];
1607 /* First test before acquiring mutex - solves deadlocks when we
1608 * re-enter the quota code and are already holding the mutex */
1609 if (IS_NOQUOTA(inode))
1612 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);
1618 spin_lock(&dq_data_lock);
1619 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1620 if (!inode->i_dquot[cnt])
1622 warntype[cnt] = info_idq_free(inode->i_dquot[cnt], number);
1623 dquot_decr_inodes(inode->i_dquot[cnt], number);
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]);
1630 flush_warnings(inode->i_dquot, warntype);
1631 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1634 EXPORT_SYMBOL(dquot_free_inode);
1637 * call back function, get reserved quota space from underlying fs
1639 qsize_t dquot_get_reserved_space(struct inode *inode)
1641 qsize_t reserved_space = 0;
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;
1650 * Transfer the number of inode and blocks from one diskquota to an other.
1652 * This operation can block, but only after everything is updated
1653 * A transaction must be started when entering this function.
1655 int dquot_transfer(struct inode *inode, struct iattr *iattr)
1657 qsize_t space, cur_space;
1658 qsize_t rsv_space = 0;
1659 struct dquot *transfer_from[MAXQUOTAS];
1660 struct dquot *transfer_to[MAXQUOTAS];
1661 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;
1664 char warntype_to[MAXQUOTAS];
1665 char warntype_from_inodes[MAXQUOTAS], warntype_from_space[MAXQUOTAS];
1667 /* First test before acquiring mutex - solves deadlocks when we
1668 * re-enter the quota code and are already holding the mutex */
1669 if (IS_NOQUOTA(inode))
1671 /* Initialize the arrays */
1672 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1673 transfer_from[cnt] = NULL;
1674 transfer_to[cnt] = NULL;
1675 warntype_to[cnt] = QUOTA_NL_NOWARN;
1680 transfer_to[cnt] = dqget(inode->i_sb, iattr->ia_uid, cnt);
1685 transfer_to[cnt] = dqget(inode->i_sb, iattr->ia_gid, cnt);
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);
1696 spin_lock(&dq_data_lock);
1697 cur_space = inode_get_bytes(inode);
1698 rsv_space = dquot_get_reserved_space(inode);
1699 space = cur_space + rsv_space;
1700 /* Build the transfer_from list and check the limits */
1701 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1702 if (!transfer_to[cnt])
1704 transfer_from[cnt] = inode->i_dquot[cnt];
1705 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)
1712 * Finally perform the needed transfer from transfer_from to transfer_to
1714 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1716 * Skip changes for same uid or gid or for turned off quota-type.
1718 if (!transfer_to[cnt])
1721 /* Due to IO error we might not have transfer_from[] structure */
1722 if (transfer_from[cnt]) {
1723 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);
1727 dquot_decr_inodes(transfer_from[cnt], 1);
1728 dquot_decr_space(transfer_from[cnt], cur_space);
1729 dquot_free_reserved_space(transfer_from[cnt],
1733 dquot_incr_inodes(transfer_to[cnt], 1);
1734 dquot_incr_space(transfer_to[cnt], cur_space);
1735 dquot_resv_space(transfer_to[cnt], rsv_space);
1737 inode->i_dquot[cnt] = transfer_to[cnt];
1739 spin_unlock(&dq_data_lock);
1740 up_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
1742 /* 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]);
1746 if (transfer_to[cnt]) {
1747 mark_dquot_dirty(transfer_to[cnt]);
1748 /* The reference we got is transferred to the inode */
1749 transfer_to[cnt] = NULL;
1753 flush_warnings(transfer_to, warntype_to);
1754 flush_warnings(transfer_from, warntype_from_inodes);
1755 flush_warnings(transfer_from, warntype_from_space);
1757 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1758 dqput(transfer_from[cnt]);
1759 dqput(transfer_to[cnt]);
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++)
1767 transfer_from[cnt] = NULL;
1771 EXPORT_SYMBOL(dquot_transfer);
1773 /* Wrapper for transferring ownership of an inode */
1774 int vfs_dq_transfer(struct inode *inode, struct iattr *iattr)
1776 if (sb_any_quota_active(inode->i_sb) && !IS_NOQUOTA(inode)) {
1778 if (inode->i_sb->dq_op->transfer(inode, iattr) == NO_QUOTA)
1783 EXPORT_SYMBOL(vfs_dq_transfer);
1786 * Write info of quota file to disk
1788 int dquot_commit_info(struct super_block *sb, int type)
1791 struct quota_info *dqopt = sb_dqopt(sb);
1793 mutex_lock(&dqopt->dqio_mutex);
1794 ret = dqopt->ops[type]->write_file_info(sb, type);
1795 mutex_unlock(&dqopt->dqio_mutex);
1798 EXPORT_SYMBOL(dquot_commit_info);
1801 * Definitions of diskquota operations.
1803 struct dquot_operations dquot_operations = {
1804 .initialize = dquot_initialize,
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,
1815 .write_info = dquot_commit_info,
1816 .alloc_dquot = dquot_alloc,
1817 .destroy_dquot = dquot_destroy,
1821 * Turn quota off on a device. type == -1 ==> quotaoff for all types (umount)
1823 int vfs_quota_disable(struct super_block *sb, int type, unsigned int flags)
1826 struct quota_info *dqopt = sb_dqopt(sb);
1827 struct inode *toputinode[MAXQUOTAS];
1829 /* 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)))
1836 /* We need to serialize quota_off() for device */
1837 mutex_lock(&dqopt->dqonoff_mutex);
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.
1844 if (!sb_any_quota_loaded(sb)) {
1845 mutex_unlock(&dqopt->dqonoff_mutex);
1848 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1849 toputinode[cnt] = NULL;
1850 if (type != -1 && cnt != type)
1852 if (!sb_has_quota_loaded(sb, cnt))
1855 if (flags & DQUOT_SUSPENDED) {
1856 spin_lock(&dq_state_lock);
1858 dquot_state_flag(DQUOT_SUSPENDED, cnt);
1859 spin_unlock(&dq_state_lock);
1861 spin_lock(&dq_state_lock);
1862 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);
1868 spin_unlock(&dq_state_lock);
1869 iput(dqopt->files[cnt]);
1870 dqopt->files[cnt] = NULL;
1873 spin_unlock(&dq_state_lock);
1876 /* We still have to keep quota loaded? */
1877 if (sb_has_quota_loaded(sb, cnt) && !(flags & DQUOT_SUSPENDED))
1880 /* Note: these are blocking operations */
1881 drop_dquot_ref(sb, cnt);
1882 invalidate_dquots(sb, cnt);
1884 * Now all dquots should be invalidated, all writes done so we should be only
1885 * users of the info. No locks needed.
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);
1893 toputinode[cnt] = dqopt->files[cnt];
1894 if (!sb_has_quota_loaded(sb, cnt))
1895 dqopt->files[cnt] = NULL;
1896 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;
1901 mutex_unlock(&dqopt->dqonoff_mutex);
1903 /* Skip syncing and setting flags if quota files are hidden */
1904 if (dqopt->flags & DQUOT_QUOTA_SYS_FILE)
1907 /* Sync the superblock so that buffers with quota data are written to
1908 * disk (and so userspace sees correct data afterwards). */
1909 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]) {
1919 mutex_lock(&dqopt->dqonoff_mutex);
1920 /* If quota was reenabled in the meantime, we have
1922 if (!sb_has_quota_loaded(sb, cnt)) {
1923 mutex_lock_nested(&toputinode[cnt]->i_mutex, I_MUTEX_QUOTA);
1924 toputinode[cnt]->i_flags &= ~(S_IMMUTABLE |
1925 S_NOATIME | S_NOQUOTA);
1926 truncate_inode_pages(&toputinode[cnt]->i_data, 0);
1927 mutex_unlock(&toputinode[cnt]->i_mutex);
1928 mark_inode_dirty(toputinode[cnt]);
1930 mutex_unlock(&dqopt->dqonoff_mutex);
1933 invalidate_bdev(sb->s_bdev);
1935 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1936 if (toputinode[cnt]) {
1937 /* On remount RO, we keep the inode pointer so that we
1938 * 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))
1945 iput(toputinode[cnt]);
1946 else if (!toputinode[cnt]->i_nlink)
1951 EXPORT_SYMBOL(vfs_quota_disable);
1953 int vfs_quota_off(struct super_block *sb, int type, int remount)
1955 return vfs_quota_disable(sb, type, remount ? DQUOT_SUSPENDED :
1956 (DQUOT_USAGE_ENABLED | DQUOT_LIMITS_ENABLED));
1958 EXPORT_SYMBOL(vfs_quota_off);
1960 * Turn quotas on on a device
1964 * Helper function to turn quotas on when we already have the inode of
1965 * quota file and no quota information is loaded.
1967 static int vfs_load_quota_inode(struct inode *inode, int type, int format_id,
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);
1978 if (!S_ISREG(inode->i_mode)) {
1982 if (IS_RDONLY(inode)) {
1986 if (!sb->s_op->quota_write || !sb->s_op->quota_read) {
1990 /* Usage always has to be set... */
1991 if (!(flags & DQUOT_USAGE_ENABLED)) {
1996 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
2002 invalidate_bdev(sb->s_bdev);
2004 mutex_lock(&inode->i_mutex);
2005 mutex_lock(&dqopt->dqonoff_mutex);
2006 if (sb_has_quota_loaded(sb, type)) {
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);
2023 dqopt->files[type] = igrab(inode);
2024 if (!dqopt->files[type])
2027 if (!fmt->qf_ops->check_quota_file(sb, type))
2030 dqopt->ops[type] = fmt->qf_ops;
2031 dqopt->info[type].dqi_format = fmt;
2032 dqopt->info[type].dqi_fmt_id = format_id;
2033 INIT_LIST_HEAD(&dqopt->info[type].dqi_dirty_list);
2034 mutex_lock(&dqopt->dqio_mutex);
2035 if ((error = dqopt->ops[type]->read_file_info(sb, type)) < 0) {
2036 mutex_unlock(&dqopt->dqio_mutex);
2039 mutex_unlock(&dqopt->dqio_mutex);
2040 mutex_unlock(&inode->i_mutex);
2041 spin_lock(&dq_state_lock);
2042 dqopt->flags |= dquot_state_flag(flags, type);
2043 spin_unlock(&dq_state_lock);
2045 add_dquot_ref(sb, type);
2046 mutex_unlock(&dqopt->dqonoff_mutex);
2051 dqopt->files[type] = NULL;
2054 mutex_unlock(&dqopt->dqonoff_mutex);
2055 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);
2063 mutex_unlock(&inode->i_mutex);
2065 put_quota_format(fmt);
2070 /* Reenable quotas on remount RW */
2071 static int vfs_quota_on_remount(struct super_block *sb, int type)
2073 struct quota_info *dqopt = sb_dqopt(sb);
2074 struct inode *inode;
2078 mutex_lock(&dqopt->dqonoff_mutex);
2079 if (!sb_has_quota_suspended(sb, type)) {
2080 mutex_unlock(&dqopt->dqonoff_mutex);
2083 inode = dqopt->files[type];
2084 dqopt->files[type] = NULL;
2085 spin_lock(&dq_state_lock);
2086 flags = dqopt->flags & dquot_state_flag(DQUOT_USAGE_ENABLED |
2087 DQUOT_LIMITS_ENABLED, type);
2088 dqopt->flags &= ~dquot_state_flag(DQUOT_STATE_FLAGS, type);
2089 spin_unlock(&dq_state_lock);
2090 mutex_unlock(&dqopt->dqonoff_mutex);
2092 flags = dquot_generic_flag(flags, type);
2093 ret = vfs_load_quota_inode(inode, type, dqopt->info[type].dqi_fmt_id,
2100 int vfs_quota_on_path(struct super_block *sb, int type, int format_id,
2103 int error = security_quota_on(path->dentry);
2106 /* Quota file not on the same filesystem? */
2107 if (path->mnt->mnt_sb != sb)
2110 error = vfs_load_quota_inode(path->dentry->d_inode, type,
2111 format_id, DQUOT_USAGE_ENABLED |
2112 DQUOT_LIMITS_ENABLED);
2115 EXPORT_SYMBOL(vfs_quota_on_path);
2117 int vfs_quota_on(struct super_block *sb, int type, int format_id, char *name,
2124 return vfs_quota_on_remount(sb, type);
2126 error = kern_path(name, LOOKUP_FOLLOW, &path);
2128 error = vfs_quota_on_path(sb, type, format_id, &path);
2133 EXPORT_SYMBOL(vfs_quota_on);
2136 * More powerful function for turning on quotas allowing setting
2137 * of individual quota flags
2139 int vfs_quota_enable(struct inode *inode, int type, int format_id,
2143 struct super_block *sb = inode->i_sb;
2144 struct quota_info *dqopt = sb_dqopt(sb);
2146 /* Just unsuspend quotas? */
2147 if (flags & DQUOT_SUSPENDED)
2148 return vfs_quota_on_remount(sb, type);
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);
2159 if (flags & DQUOT_USAGE_ENABLED &&
2160 sb_has_quota_usage_enabled(sb, type)) {
2164 if (flags & DQUOT_LIMITS_ENABLED &&
2165 sb_has_quota_limits_enabled(sb, type)) {
2169 spin_lock(&dq_state_lock);
2170 sb_dqopt(sb)->flags |= dquot_state_flag(flags, type);
2171 spin_unlock(&dq_state_lock);
2173 mutex_unlock(&dqopt->dqonoff_mutex);
2178 return vfs_load_quota_inode(inode, type, format_id, flags);
2180 EXPORT_SYMBOL(vfs_quota_enable);
2183 * This function is used when filesystem needs to initialize quotas
2184 * during mount time.
2186 int vfs_quota_on_mount(struct super_block *sb, char *qf_name,
2187 int format_id, int type)
2189 struct dentry *dentry;
2192 dentry = lookup_one_len(qf_name, sb->s_root, strlen(qf_name));
2194 return PTR_ERR(dentry);
2196 if (!dentry->d_inode) {
2201 error = security_quota_on(dentry);
2203 error = vfs_load_quota_inode(dentry->d_inode, type, format_id,
2204 DQUOT_USAGE_ENABLED | DQUOT_LIMITS_ENABLED);
2210 EXPORT_SYMBOL(vfs_quota_on_mount);
2212 /* Wrapper to turn on quotas when remounting rw */
2213 int vfs_dq_quota_on_remount(struct super_block *sb)
2218 if (!sb->s_qcop || !sb->s_qcop->quota_on)
2220 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
2221 err = sb->s_qcop->quota_on(sb, cnt, 0, NULL, 1);
2222 if (err < 0 && !ret)
2227 EXPORT_SYMBOL(vfs_dq_quota_on_remount);
2229 static inline qsize_t qbtos(qsize_t blocks)
2231 return blocks << QIF_DQBLKSIZE_BITS;
2234 static inline qsize_t stoqb(qsize_t space)
2236 return (space + QIF_DQBLKSIZE - 1) >> QIF_DQBLKSIZE_BITS;
2239 /* Generic routine for getting common part of quota structure */
2240 static void do_get_dqblk(struct dquot *dquot, struct if_dqblk *di)
2242 struct mem_dqblk *dm = &dquot->dq_dqb;
2244 spin_lock(&dq_data_lock);
2245 di->dqb_bhardlimit = stoqb(dm->dqb_bhardlimit);
2246 di->dqb_bsoftlimit = stoqb(dm->dqb_bsoftlimit);
2247 di->dqb_curspace = dm->dqb_curspace + dm->dqb_rsvspace;
2248 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);
2257 int vfs_get_dqblk(struct super_block *sb, int type, qid_t id, struct if_dqblk *di)
2259 struct dquot *dquot;
2261 dquot = dqget(sb, id, type);
2264 do_get_dqblk(dquot, di);
2269 EXPORT_SYMBOL(vfs_get_dqblk);
2271 /* Generic routine for setting common part of quota structure */
2272 static int do_set_dqblk(struct dquot *dquot, struct if_dqblk *di)
2274 struct mem_dqblk *dm = &dquot->dq_dqb;
2275 int check_blim = 0, check_ilim = 0;
2276 struct mem_dqinfo *dqi = &sb_dqopt(dquot->dq_sb)->info[dquot->dq_type];
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)))
2286 spin_lock(&dq_data_lock);
2287 if (di->dqb_valid & QIF_SPACE) {
2288 dm->dqb_curspace = di->dqb_curspace - dm->dqb_rsvspace;
2290 __set_bit(DQ_LASTSET_B + QIF_SPACE_B, &dquot->dq_flags);
2292 if (di->dqb_valid & QIF_BLIMITS) {
2293 dm->dqb_bsoftlimit = qbtos(di->dqb_bsoftlimit);
2294 dm->dqb_bhardlimit = qbtos(di->dqb_bhardlimit);
2296 __set_bit(DQ_LASTSET_B + QIF_BLIMITS_B, &dquot->dq_flags);
2298 if (di->dqb_valid & QIF_INODES) {
2299 dm->dqb_curinodes = di->dqb_curinodes;
2301 __set_bit(DQ_LASTSET_B + QIF_INODES_B, &dquot->dq_flags);
2303 if (di->dqb_valid & QIF_ILIMITS) {
2304 dm->dqb_isoftlimit = di->dqb_isoftlimit;
2305 dm->dqb_ihardlimit = di->dqb_ihardlimit;
2307 __set_bit(DQ_LASTSET_B + QIF_ILIMITS_B, &dquot->dq_flags);
2309 if (di->dqb_valid & QIF_BTIME) {
2310 dm->dqb_btime = di->dqb_btime;
2312 __set_bit(DQ_LASTSET_B + QIF_BTIME_B, &dquot->dq_flags);
2314 if (di->dqb_valid & QIF_ITIME) {
2315 dm->dqb_itime = di->dqb_itime;
2317 __set_bit(DQ_LASTSET_B + QIF_ITIME_B, &dquot->dq_flags);
2321 if (!dm->dqb_bsoftlimit || dm->dqb_curspace < dm->dqb_bsoftlimit) {
2323 clear_bit(DQ_BLKS_B, &dquot->dq_flags);
2325 else if (!(di->dqb_valid & QIF_BTIME)) /* Set grace only if user hasn't provided his own... */
2326 dm->dqb_btime = get_seconds() + dqi->dqi_bgrace;
2329 if (!dm->dqb_isoftlimit || dm->dqb_curinodes < dm->dqb_isoftlimit) {
2331 clear_bit(DQ_INODES_B, &dquot->dq_flags);
2333 else if (!(di->dqb_valid & QIF_ITIME)) /* Set grace only if user hasn't provided his own... */
2334 dm->dqb_itime = get_seconds() + dqi->dqi_igrace;
2336 if (dm->dqb_bhardlimit || dm->dqb_bsoftlimit || dm->dqb_ihardlimit || dm->dqb_isoftlimit)
2337 clear_bit(DQ_FAKE_B, &dquot->dq_flags);
2339 set_bit(DQ_FAKE_B, &dquot->dq_flags);
2340 spin_unlock(&dq_data_lock);
2341 mark_dquot_dirty(dquot);
2346 int vfs_set_dqblk(struct super_block *sb, int type, qid_t id, struct if_dqblk *di)
2348 struct dquot *dquot;
2351 dquot = dqget(sb, id, type);
2356 rc = do_set_dqblk(dquot, di);
2361 EXPORT_SYMBOL(vfs_set_dqblk);
2363 /* Generic routine for getting common part of quota file information */
2364 int vfs_get_dqinfo(struct super_block *sb, int type, struct if_dqinfo *ii)
2366 struct mem_dqinfo *mi;
2368 mutex_lock(&sb_dqopt(sb)->dqonoff_mutex);
2369 if (!sb_has_quota_active(sb, type)) {
2370 mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
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);
2380 mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
2383 EXPORT_SYMBOL(vfs_get_dqinfo);
2385 /* Generic routine for setting common part of quota file information */
2386 int vfs_set_dqinfo(struct super_block *sb, int type, struct if_dqinfo *ii)
2388 struct mem_dqinfo *mi;
2391 mutex_lock(&sb_dqopt(sb)->dqonoff_mutex);
2392 if (!sb_has_quota_active(sb, type)) {
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);
2409 mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
2412 EXPORT_SYMBOL(vfs_set_dqinfo);
2414 struct 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
2424 static ctl_table fs_dqstats_table[] = {
2426 .ctl_name = FS_DQ_LOOKUPS,
2427 .procname = "lookups",
2428 .data = &dqstats.lookups,
2429 .maxlen = sizeof(int),
2431 .proc_handler = &proc_dointvec,
2434 .ctl_name = FS_DQ_DROPS,
2435 .procname = "drops",
2436 .data = &dqstats.drops,
2437 .maxlen = sizeof(int),
2439 .proc_handler = &proc_dointvec,
2442 .ctl_name = FS_DQ_READS,
2443 .procname = "reads",
2444 .data = &dqstats.reads,
2445 .maxlen = sizeof(int),
2447 .proc_handler = &proc_dointvec,
2450 .ctl_name = FS_DQ_WRITES,
2451 .procname = "writes",
2452 .data = &dqstats.writes,
2453 .maxlen = sizeof(int),
2455 .proc_handler = &proc_dointvec,
2458 .ctl_name = FS_DQ_CACHE_HITS,
2459 .procname = "cache_hits",
2460 .data = &dqstats.cache_hits,
2461 .maxlen = sizeof(int),
2463 .proc_handler = &proc_dointvec,
2466 .ctl_name = FS_DQ_ALLOCATED,
2467 .procname = "allocated_dquots",
2468 .data = &dqstats.allocated_dquots,
2469 .maxlen = sizeof(int),
2471 .proc_handler = &proc_dointvec,
2474 .ctl_name = FS_DQ_FREE,
2475 .procname = "free_dquots",
2476 .data = &dqstats.free_dquots,
2477 .maxlen = sizeof(int),
2479 .proc_handler = &proc_dointvec,
2482 .ctl_name = FS_DQ_SYNCS,
2483 .procname = "syncs",
2484 .data = &dqstats.syncs,
2485 .maxlen = sizeof(int),
2487 .proc_handler = &proc_dointvec,
2489 #ifdef CONFIG_PRINT_QUOTA_WARNING
2491 .ctl_name = FS_DQ_WARNINGS,
2492 .procname = "warnings",
2493 .data = &flag_print_warnings,
2494 .maxlen = sizeof(int),
2496 .proc_handler = &proc_dointvec,
2502 static ctl_table fs_table[] = {
2504 .ctl_name = FS_DQSTATS,
2505 .procname = "quota",
2507 .child = fs_dqstats_table,
2512 static ctl_table sys_table[] = {
2522 static int __init dquot_init(void)
2525 unsigned long nr_hash, order;
2527 printk(KERN_NOTICE "VFS: Disk quotas %s\n", __DQUOT_VERSION__);
2529 register_sysctl_table(sys_table);
2531 dquot_cachep = kmem_cache_create("dquot",
2532 sizeof(struct dquot), sizeof(unsigned long) * 4,
2533 (SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT|
2534 SLAB_MEM_SPREAD|SLAB_PANIC),
2538 dquot_hash = (struct hlist_head *)__get_free_pages(GFP_ATOMIC, order);
2540 panic("Cannot create dquot hash table");
2542 /* Find power-of-two hlist_heads which can fit into allocation */
2543 nr_hash = (1UL << order) * PAGE_SIZE / sizeof(struct hlist_head);
2547 } while (nr_hash >> dq_hash_bits);
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);
2555 printk("Dquot-cache hash table entries: %ld (order %ld, %ld bytes)\n",
2556 nr_hash, order, (PAGE_SIZE << order));
2558 register_shrinker(&dqcache_shrinker);
2560 #ifdef CONFIG_QUOTA_NETLINK_INTERFACE
2561 if (genl_register_family("a_genl_family) != 0)
2562 printk(KERN_ERR "VFS: Failed to create quota netlink interface.\n");
2567 module_init(dquot_init);