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 inline struct dquot *find_dquot(unsigned int hashent, struct super_block *sb, unsigned int id, int type)
248 struct hlist_node *node;
251 hlist_for_each (node, dquot_hash+hashent) {
252 dquot = hlist_entry(node, struct dquot, dq_hash);
253 if (dquot->dq_sb == sb && dquot->dq_id == id && dquot->dq_type == type)
259 /* Add a dquot to the tail of the free list */
260 static inline void put_dquot_last(struct dquot *dquot)
262 list_add_tail(&dquot->dq_free, &free_dquots);
263 dqstats.free_dquots++;
266 static inline void remove_free_dquot(struct dquot *dquot)
268 if (list_empty(&dquot->dq_free))
270 list_del_init(&dquot->dq_free);
271 dqstats.free_dquots--;
274 static inline void put_inuse(struct dquot *dquot)
276 /* We add to the back of inuse list so we don't have to restart
277 * when traversing this list and we block */
278 list_add_tail(&dquot->dq_inuse, &inuse_list);
279 dqstats.allocated_dquots++;
282 static inline void remove_inuse(struct dquot *dquot)
284 dqstats.allocated_dquots--;
285 list_del(&dquot->dq_inuse);
288 * End of list functions needing dq_list_lock
291 static void wait_on_dquot(struct dquot *dquot)
293 mutex_lock(&dquot->dq_lock);
294 mutex_unlock(&dquot->dq_lock);
297 static inline int dquot_dirty(struct dquot *dquot)
299 return test_bit(DQ_MOD_B, &dquot->dq_flags);
302 static inline int mark_dquot_dirty(struct dquot *dquot)
304 return dquot->dq_sb->dq_op->mark_dirty(dquot);
307 int dquot_mark_dquot_dirty(struct dquot *dquot)
309 spin_lock(&dq_list_lock);
310 if (!test_and_set_bit(DQ_MOD_B, &dquot->dq_flags))
311 list_add(&dquot->dq_dirty, &sb_dqopt(dquot->dq_sb)->
312 info[dquot->dq_type].dqi_dirty_list);
313 spin_unlock(&dq_list_lock);
316 EXPORT_SYMBOL(dquot_mark_dquot_dirty);
318 /* This function needs dq_list_lock */
319 static inline int clear_dquot_dirty(struct dquot *dquot)
321 if (!test_and_clear_bit(DQ_MOD_B, &dquot->dq_flags))
323 list_del_init(&dquot->dq_dirty);
327 void mark_info_dirty(struct super_block *sb, int type)
329 set_bit(DQF_INFO_DIRTY_B, &sb_dqopt(sb)->info[type].dqi_flags);
331 EXPORT_SYMBOL(mark_info_dirty);
334 * Read dquot from disk and alloc space for it
337 int dquot_acquire(struct dquot *dquot)
339 int ret = 0, ret2 = 0;
340 struct quota_info *dqopt = sb_dqopt(dquot->dq_sb);
342 mutex_lock(&dquot->dq_lock);
343 mutex_lock(&dqopt->dqio_mutex);
344 if (!test_bit(DQ_READ_B, &dquot->dq_flags))
345 ret = dqopt->ops[dquot->dq_type]->read_dqblk(dquot);
348 set_bit(DQ_READ_B, &dquot->dq_flags);
349 /* Instantiate dquot if needed */
350 if (!test_bit(DQ_ACTIVE_B, &dquot->dq_flags) && !dquot->dq_off) {
351 ret = dqopt->ops[dquot->dq_type]->commit_dqblk(dquot);
352 /* Write the info if needed */
353 if (info_dirty(&dqopt->info[dquot->dq_type]))
354 ret2 = dqopt->ops[dquot->dq_type]->write_file_info(dquot->dq_sb, dquot->dq_type);
362 set_bit(DQ_ACTIVE_B, &dquot->dq_flags);
364 mutex_unlock(&dqopt->dqio_mutex);
365 mutex_unlock(&dquot->dq_lock);
368 EXPORT_SYMBOL(dquot_acquire);
371 * Write dquot to disk
373 int dquot_commit(struct dquot *dquot)
375 int ret = 0, ret2 = 0;
376 struct quota_info *dqopt = sb_dqopt(dquot->dq_sb);
378 mutex_lock(&dqopt->dqio_mutex);
379 spin_lock(&dq_list_lock);
380 if (!clear_dquot_dirty(dquot)) {
381 spin_unlock(&dq_list_lock);
384 spin_unlock(&dq_list_lock);
385 /* Inactive dquot can be only if there was error during read/init
386 * => we have better not writing it */
387 if (test_bit(DQ_ACTIVE_B, &dquot->dq_flags)) {
388 ret = dqopt->ops[dquot->dq_type]->commit_dqblk(dquot);
389 if (info_dirty(&dqopt->info[dquot->dq_type]))
390 ret2 = dqopt->ops[dquot->dq_type]->write_file_info(dquot->dq_sb, dquot->dq_type);
395 mutex_unlock(&dqopt->dqio_mutex);
398 EXPORT_SYMBOL(dquot_commit);
403 int dquot_release(struct dquot *dquot)
405 int ret = 0, ret2 = 0;
406 struct quota_info *dqopt = sb_dqopt(dquot->dq_sb);
408 mutex_lock(&dquot->dq_lock);
409 /* Check whether we are not racing with some other dqget() */
410 if (atomic_read(&dquot->dq_count) > 1)
412 mutex_lock(&dqopt->dqio_mutex);
413 if (dqopt->ops[dquot->dq_type]->release_dqblk) {
414 ret = dqopt->ops[dquot->dq_type]->release_dqblk(dquot);
416 if (info_dirty(&dqopt->info[dquot->dq_type]))
417 ret2 = dqopt->ops[dquot->dq_type]->write_file_info(dquot->dq_sb, dquot->dq_type);
421 clear_bit(DQ_ACTIVE_B, &dquot->dq_flags);
422 mutex_unlock(&dqopt->dqio_mutex);
424 mutex_unlock(&dquot->dq_lock);
427 EXPORT_SYMBOL(dquot_release);
429 void dquot_destroy(struct dquot *dquot)
431 kmem_cache_free(dquot_cachep, dquot);
433 EXPORT_SYMBOL(dquot_destroy);
435 static inline void do_destroy_dquot(struct dquot *dquot)
437 dquot->dq_sb->dq_op->destroy_dquot(dquot);
440 /* Invalidate all dquots on the list. Note that this function is called after
441 * quota is disabled and pointers from inodes removed so there cannot be new
442 * quota users. There can still be some users of quotas due to inodes being
443 * just deleted or pruned by prune_icache() (those are not attached to any
444 * list) or parallel quotactl call. We have to wait for such users.
446 static void invalidate_dquots(struct super_block *sb, int type)
448 struct dquot *dquot, *tmp;
451 spin_lock(&dq_list_lock);
452 list_for_each_entry_safe(dquot, tmp, &inuse_list, dq_inuse) {
453 if (dquot->dq_sb != sb)
455 if (dquot->dq_type != type)
457 /* Wait for dquot users */
458 if (atomic_read(&dquot->dq_count)) {
461 atomic_inc(&dquot->dq_count);
462 prepare_to_wait(&dquot->dq_wait_unused, &wait,
463 TASK_UNINTERRUPTIBLE);
464 spin_unlock(&dq_list_lock);
465 /* Once dqput() wakes us up, we know it's time to free
467 * IMPORTANT: we rely on the fact that there is always
468 * at most one process waiting for dquot to free.
469 * Otherwise dq_count would be > 1 and we would never
472 if (atomic_read(&dquot->dq_count) > 1)
474 finish_wait(&dquot->dq_wait_unused, &wait);
476 /* At this moment dquot() need not exist (it could be
477 * reclaimed by prune_dqcache(). Hence we must
482 * Quota now has no users and it has been written on last
485 remove_dquot_hash(dquot);
486 remove_free_dquot(dquot);
488 do_destroy_dquot(dquot);
490 spin_unlock(&dq_list_lock);
493 /* Call callback for every active dquot on given filesystem */
494 int dquot_scan_active(struct super_block *sb,
495 int (*fn)(struct dquot *dquot, unsigned long priv),
498 struct dquot *dquot, *old_dquot = NULL;
501 mutex_lock(&sb_dqopt(sb)->dqonoff_mutex);
502 spin_lock(&dq_list_lock);
503 list_for_each_entry(dquot, &inuse_list, dq_inuse) {
504 if (!test_bit(DQ_ACTIVE_B, &dquot->dq_flags))
506 if (dquot->dq_sb != sb)
508 /* Now we have active dquot so we can just increase use count */
509 atomic_inc(&dquot->dq_count);
511 spin_unlock(&dq_list_lock);
514 ret = fn(dquot, priv);
517 spin_lock(&dq_list_lock);
518 /* We are safe to continue now because our dquot could not
519 * be moved out of the inuse list while we hold the reference */
521 spin_unlock(&dq_list_lock);
524 mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
527 EXPORT_SYMBOL(dquot_scan_active);
529 int vfs_quota_sync(struct super_block *sb, int type)
531 struct list_head *dirty;
533 struct quota_info *dqopt = sb_dqopt(sb);
536 mutex_lock(&dqopt->dqonoff_mutex);
537 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
538 if (type != -1 && cnt != type)
540 if (!sb_has_quota_active(sb, cnt))
542 spin_lock(&dq_list_lock);
543 dirty = &dqopt->info[cnt].dqi_dirty_list;
544 while (!list_empty(dirty)) {
545 dquot = list_first_entry(dirty, struct dquot, dq_dirty);
546 /* Dirty and inactive can be only bad dquot... */
547 if (!test_bit(DQ_ACTIVE_B, &dquot->dq_flags)) {
548 clear_dquot_dirty(dquot);
551 /* Now we have active dquot from which someone is
552 * holding reference so we can safely just increase
554 atomic_inc(&dquot->dq_count);
556 spin_unlock(&dq_list_lock);
557 sb->dq_op->write_dquot(dquot);
559 spin_lock(&dq_list_lock);
561 spin_unlock(&dq_list_lock);
564 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
565 if ((cnt == type || type == -1) && sb_has_quota_active(sb, cnt)
566 && info_dirty(&dqopt->info[cnt]))
567 sb->dq_op->write_info(sb, cnt);
568 spin_lock(&dq_list_lock);
570 spin_unlock(&dq_list_lock);
571 mutex_unlock(&dqopt->dqonoff_mutex);
575 EXPORT_SYMBOL(vfs_quota_sync);
577 /* Free unused dquots from cache */
578 static void prune_dqcache(int count)
580 struct list_head *head;
583 head = free_dquots.prev;
584 while (head != &free_dquots && count) {
585 dquot = list_entry(head, struct dquot, dq_free);
586 remove_dquot_hash(dquot);
587 remove_free_dquot(dquot);
589 do_destroy_dquot(dquot);
591 head = free_dquots.prev;
596 * This is called from kswapd when we think we need some
600 static int shrink_dqcache_memory(int nr, gfp_t gfp_mask)
603 spin_lock(&dq_list_lock);
605 spin_unlock(&dq_list_lock);
607 return (dqstats.free_dquots / 100) * sysctl_vfs_cache_pressure;
610 static struct shrinker dqcache_shrinker = {
611 .shrink = shrink_dqcache_memory,
612 .seeks = DEFAULT_SEEKS,
616 * Put reference to dquot
617 * NOTE: If you change this function please check whether dqput_blocks() works right...
619 void dqput(struct dquot *dquot)
625 #ifdef __DQUOT_PARANOIA
626 if (!atomic_read(&dquot->dq_count)) {
627 printk("VFS: dqput: trying to free free dquot\n");
628 printk("VFS: device %s, dquot of %s %d\n",
630 quotatypes[dquot->dq_type],
636 spin_lock(&dq_list_lock);
638 spin_unlock(&dq_list_lock);
640 spin_lock(&dq_list_lock);
641 if (atomic_read(&dquot->dq_count) > 1) {
642 /* We have more than one user... nothing to do */
643 atomic_dec(&dquot->dq_count);
644 /* Releasing dquot during quotaoff phase? */
645 if (!sb_has_quota_active(dquot->dq_sb, dquot->dq_type) &&
646 atomic_read(&dquot->dq_count) == 1)
647 wake_up(&dquot->dq_wait_unused);
648 spin_unlock(&dq_list_lock);
651 /* Need to release dquot? */
652 if (test_bit(DQ_ACTIVE_B, &dquot->dq_flags) && dquot_dirty(dquot)) {
653 spin_unlock(&dq_list_lock);
654 /* Commit dquot before releasing */
655 ret = dquot->dq_sb->dq_op->write_dquot(dquot);
657 printk(KERN_ERR "VFS: cannot write quota structure on "
658 "device %s (error %d). Quota may get out of "
659 "sync!\n", dquot->dq_sb->s_id, ret);
661 * We clear dirty bit anyway, so that we avoid
664 spin_lock(&dq_list_lock);
665 clear_dquot_dirty(dquot);
666 spin_unlock(&dq_list_lock);
670 /* Clear flag in case dquot was inactive (something bad happened) */
671 clear_dquot_dirty(dquot);
672 if (test_bit(DQ_ACTIVE_B, &dquot->dq_flags)) {
673 spin_unlock(&dq_list_lock);
674 dquot->dq_sb->dq_op->release_dquot(dquot);
677 atomic_dec(&dquot->dq_count);
678 #ifdef __DQUOT_PARANOIA
680 BUG_ON(!list_empty(&dquot->dq_free));
682 put_dquot_last(dquot);
683 spin_unlock(&dq_list_lock);
685 EXPORT_SYMBOL(dqput);
687 struct dquot *dquot_alloc(struct super_block *sb, int type)
689 return kmem_cache_zalloc(dquot_cachep, GFP_NOFS);
691 EXPORT_SYMBOL(dquot_alloc);
693 static struct dquot *get_empty_dquot(struct super_block *sb, int type)
697 dquot = sb->dq_op->alloc_dquot(sb, type);
701 mutex_init(&dquot->dq_lock);
702 INIT_LIST_HEAD(&dquot->dq_free);
703 INIT_LIST_HEAD(&dquot->dq_inuse);
704 INIT_HLIST_NODE(&dquot->dq_hash);
705 INIT_LIST_HEAD(&dquot->dq_dirty);
706 init_waitqueue_head(&dquot->dq_wait_unused);
708 dquot->dq_type = type;
709 atomic_set(&dquot->dq_count, 1);
715 * Get reference to dquot
717 * Locking is slightly tricky here. We are guarded from parallel quotaoff()
718 * destroying our dquot by:
719 * a) checking for quota flags under dq_list_lock and
720 * b) getting a reference to dquot before we release dq_list_lock
722 struct dquot *dqget(struct super_block *sb, unsigned int id, int type)
724 unsigned int hashent = hashfn(sb, id, type);
725 struct dquot *dquot = NULL, *empty = NULL;
727 if (!sb_has_quota_active(sb, type))
730 spin_lock(&dq_list_lock);
731 spin_lock(&dq_state_lock);
732 if (!sb_has_quota_active(sb, type)) {
733 spin_unlock(&dq_state_lock);
734 spin_unlock(&dq_list_lock);
737 spin_unlock(&dq_state_lock);
739 dquot = find_dquot(hashent, sb, id, type);
742 spin_unlock(&dq_list_lock);
743 empty = get_empty_dquot(sb, type);
745 schedule(); /* Try to wait for a moment... */
751 /* all dquots go on the inuse_list */
753 /* hash it first so it can be found */
754 insert_dquot_hash(dquot);
756 spin_unlock(&dq_list_lock);
758 if (!atomic_read(&dquot->dq_count))
759 remove_free_dquot(dquot);
760 atomic_inc(&dquot->dq_count);
761 dqstats.cache_hits++;
763 spin_unlock(&dq_list_lock);
765 /* Wait for dq_lock - after this we know that either dquot_release() is already
766 * finished or it will be canceled due to dq_count > 1 test */
767 wait_on_dquot(dquot);
768 /* Read the dquot and instantiate it (everything done only if needed) */
769 if (!test_bit(DQ_ACTIVE_B, &dquot->dq_flags) && sb->dq_op->acquire_dquot(dquot) < 0) {
774 #ifdef __DQUOT_PARANOIA
775 BUG_ON(!dquot->dq_sb); /* Has somebody invalidated entry under us? */
779 do_destroy_dquot(empty);
783 EXPORT_SYMBOL(dqget);
785 static int dqinit_needed(struct inode *inode, int type)
789 if (IS_NOQUOTA(inode))
792 return !inode->i_dquot[type];
793 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
794 if (!inode->i_dquot[cnt])
799 /* This routine is guarded by dqonoff_mutex mutex */
800 static void add_dquot_ref(struct super_block *sb, int type)
802 struct inode *inode, *old_inode = NULL;
804 spin_lock(&inode_lock);
805 list_for_each_entry(inode, &sb->s_inodes, i_sb_list) {
806 if (!atomic_read(&inode->i_writecount))
808 if (!dqinit_needed(inode, type))
810 if (inode->i_state & (I_FREEING|I_WILL_FREE))
814 spin_unlock(&inode_lock);
817 sb->dq_op->initialize(inode, type);
818 /* We hold a reference to 'inode' so it couldn't have been
819 * removed from s_inodes list while we dropped the inode_lock.
820 * We cannot iput the inode now as we can be holding the last
821 * reference and we cannot iput it under inode_lock. So we
822 * keep the reference and iput it later. */
824 spin_lock(&inode_lock);
826 spin_unlock(&inode_lock);
830 /* Return 0 if dqput() won't block (note that 1 doesn't necessarily mean blocking) */
831 static inline int dqput_blocks(struct dquot *dquot)
833 if (atomic_read(&dquot->dq_count) <= 1)
838 /* Remove references to dquots from inode - add dquot to list for freeing if needed */
839 /* We can't race with anybody because we hold dqptr_sem for writing... */
840 static int remove_inode_dquot_ref(struct inode *inode, int type,
841 struct list_head *tofree_head)
843 struct dquot *dquot = inode->i_dquot[type];
845 inode->i_dquot[type] = NULL;
847 if (dqput_blocks(dquot)) {
848 #ifdef __DQUOT_PARANOIA
849 if (atomic_read(&dquot->dq_count) != 1)
850 printk(KERN_WARNING "VFS: Adding dquot with dq_count %d to dispose list.\n", atomic_read(&dquot->dq_count));
852 spin_lock(&dq_list_lock);
853 list_add(&dquot->dq_free, tofree_head); /* As dquot must have currently users it can't be on the free list... */
854 spin_unlock(&dq_list_lock);
858 dqput(dquot); /* We have guaranteed we won't block */
863 /* Free list of dquots - called from inode.c */
864 /* dquots are removed from inodes, no new references can be got so we are the only ones holding reference */
865 static void put_dquot_list(struct list_head *tofree_head)
867 struct list_head *act_head;
870 act_head = tofree_head->next;
871 /* So now we have dquots on the list... Just free them */
872 while (act_head != tofree_head) {
873 dquot = list_entry(act_head, struct dquot, dq_free);
874 act_head = act_head->next;
875 list_del_init(&dquot->dq_free); /* Remove dquot from the list so we won't have problems... */
880 static void remove_dquot_ref(struct super_block *sb, int type,
881 struct list_head *tofree_head)
885 spin_lock(&inode_lock);
886 list_for_each_entry(inode, &sb->s_inodes, i_sb_list) {
887 if (!IS_NOQUOTA(inode))
888 remove_inode_dquot_ref(inode, type, tofree_head);
890 spin_unlock(&inode_lock);
893 /* Gather all references from inodes and drop them */
894 static void drop_dquot_ref(struct super_block *sb, int type)
896 LIST_HEAD(tofree_head);
899 down_write(&sb_dqopt(sb)->dqptr_sem);
900 remove_dquot_ref(sb, type, &tofree_head);
901 up_write(&sb_dqopt(sb)->dqptr_sem);
902 put_dquot_list(&tofree_head);
906 static inline void dquot_incr_inodes(struct dquot *dquot, qsize_t number)
908 dquot->dq_dqb.dqb_curinodes += number;
911 static inline void dquot_incr_space(struct dquot *dquot, qsize_t number)
913 dquot->dq_dqb.dqb_curspace += number;
916 static inline void dquot_resv_space(struct dquot *dquot, qsize_t number)
918 dquot->dq_dqb.dqb_rsvspace += number;
922 * Claim reserved quota space
924 static void dquot_claim_reserved_space(struct dquot *dquot,
927 WARN_ON(dquot->dq_dqb.dqb_rsvspace < number);
928 dquot->dq_dqb.dqb_curspace += number;
929 dquot->dq_dqb.dqb_rsvspace -= number;
933 void dquot_free_reserved_space(struct dquot *dquot, qsize_t number)
935 dquot->dq_dqb.dqb_rsvspace -= number;
938 static inline void dquot_decr_inodes(struct dquot *dquot, qsize_t number)
940 if (sb_dqopt(dquot->dq_sb)->flags & DQUOT_NEGATIVE_USAGE ||
941 dquot->dq_dqb.dqb_curinodes >= number)
942 dquot->dq_dqb.dqb_curinodes -= number;
944 dquot->dq_dqb.dqb_curinodes = 0;
945 if (dquot->dq_dqb.dqb_curinodes <= dquot->dq_dqb.dqb_isoftlimit)
946 dquot->dq_dqb.dqb_itime = (time_t) 0;
947 clear_bit(DQ_INODES_B, &dquot->dq_flags);
950 static inline void dquot_decr_space(struct dquot *dquot, qsize_t number)
952 if (sb_dqopt(dquot->dq_sb)->flags & DQUOT_NEGATIVE_USAGE ||
953 dquot->dq_dqb.dqb_curspace >= number)
954 dquot->dq_dqb.dqb_curspace -= number;
956 dquot->dq_dqb.dqb_curspace = 0;
957 if (dquot->dq_dqb.dqb_curspace <= dquot->dq_dqb.dqb_bsoftlimit)
958 dquot->dq_dqb.dqb_btime = (time_t) 0;
959 clear_bit(DQ_BLKS_B, &dquot->dq_flags);
962 static int warning_issued(struct dquot *dquot, const int warntype)
964 int flag = (warntype == QUOTA_NL_BHARDWARN ||
965 warntype == QUOTA_NL_BSOFTLONGWARN) ? DQ_BLKS_B :
966 ((warntype == QUOTA_NL_IHARDWARN ||
967 warntype == QUOTA_NL_ISOFTLONGWARN) ? DQ_INODES_B : 0);
971 return test_and_set_bit(flag, &dquot->dq_flags);
974 #ifdef CONFIG_PRINT_QUOTA_WARNING
975 static int flag_print_warnings = 1;
977 static inline int need_print_warning(struct dquot *dquot)
979 if (!flag_print_warnings)
982 switch (dquot->dq_type) {
984 return current_fsuid() == dquot->dq_id;
986 return in_group_p(dquot->dq_id);
991 /* Print warning to user which exceeded quota */
992 static void print_warning(struct dquot *dquot, const int warntype)
995 struct tty_struct *tty;
997 if (warntype == QUOTA_NL_IHARDBELOW ||
998 warntype == QUOTA_NL_ISOFTBELOW ||
999 warntype == QUOTA_NL_BHARDBELOW ||
1000 warntype == QUOTA_NL_BSOFTBELOW || !need_print_warning(dquot))
1003 tty = get_current_tty();
1006 tty_write_message(tty, dquot->dq_sb->s_id);
1007 if (warntype == QUOTA_NL_ISOFTWARN || warntype == QUOTA_NL_BSOFTWARN)
1008 tty_write_message(tty, ": warning, ");
1010 tty_write_message(tty, ": write failed, ");
1011 tty_write_message(tty, quotatypes[dquot->dq_type]);
1013 case QUOTA_NL_IHARDWARN:
1014 msg = " file limit reached.\r\n";
1016 case QUOTA_NL_ISOFTLONGWARN:
1017 msg = " file quota exceeded too long.\r\n";
1019 case QUOTA_NL_ISOFTWARN:
1020 msg = " file quota exceeded.\r\n";
1022 case QUOTA_NL_BHARDWARN:
1023 msg = " block limit reached.\r\n";
1025 case QUOTA_NL_BSOFTLONGWARN:
1026 msg = " block quota exceeded too long.\r\n";
1028 case QUOTA_NL_BSOFTWARN:
1029 msg = " block quota exceeded.\r\n";
1032 tty_write_message(tty, msg);
1037 #ifdef CONFIG_QUOTA_NETLINK_INTERFACE
1039 /* Netlink family structure for quota */
1040 static struct genl_family quota_genl_family = {
1041 .id = GENL_ID_GENERATE,
1043 .name = "VFS_DQUOT",
1045 .maxattr = QUOTA_NL_A_MAX,
1048 /* Send warning to userspace about user which exceeded quota */
1049 static void send_warning(const struct dquot *dquot, const char warntype)
1051 static atomic_t seq;
1052 struct sk_buff *skb;
1055 int msg_size = 4 * nla_total_size(sizeof(u32)) +
1056 2 * nla_total_size(sizeof(u64));
1058 /* We have to allocate using GFP_NOFS as we are called from a
1059 * filesystem performing write and thus further recursion into
1060 * the fs to free some data could cause deadlocks. */
1061 skb = genlmsg_new(msg_size, GFP_NOFS);
1064 "VFS: Not enough memory to send quota warning.\n");
1067 msg_head = genlmsg_put(skb, 0, atomic_add_return(1, &seq),
1068 "a_genl_family, 0, QUOTA_NL_C_WARNING);
1071 "VFS: Cannot store netlink header in quota warning.\n");
1074 ret = nla_put_u32(skb, QUOTA_NL_A_QTYPE, dquot->dq_type);
1077 ret = nla_put_u64(skb, QUOTA_NL_A_EXCESS_ID, dquot->dq_id);
1080 ret = nla_put_u32(skb, QUOTA_NL_A_WARNING, warntype);
1083 ret = nla_put_u32(skb, QUOTA_NL_A_DEV_MAJOR,
1084 MAJOR(dquot->dq_sb->s_dev));
1087 ret = nla_put_u32(skb, QUOTA_NL_A_DEV_MINOR,
1088 MINOR(dquot->dq_sb->s_dev));
1091 ret = nla_put_u64(skb, QUOTA_NL_A_CAUSED_ID, current_uid());
1094 genlmsg_end(skb, msg_head);
1096 ret = genlmsg_multicast(skb, 0, quota_genl_family.id, GFP_NOFS);
1097 if (ret < 0 && ret != -ESRCH)
1099 "VFS: Failed to send notification message: %d\n", ret);
1102 printk(KERN_ERR "VFS: Not enough space to compose quota message!\n");
1108 * Write warnings to the console and send warning messages over netlink.
1110 * Note that this function can sleep.
1112 static inline void flush_warnings(struct dquot * const *dquots, char *warntype)
1116 for (i = 0; i < MAXQUOTAS; i++)
1117 if (dquots[i] && warntype[i] != QUOTA_NL_NOWARN &&
1118 !warning_issued(dquots[i], warntype[i])) {
1119 #ifdef CONFIG_PRINT_QUOTA_WARNING
1120 print_warning(dquots[i], warntype[i]);
1122 #ifdef CONFIG_QUOTA_NETLINK_INTERFACE
1123 send_warning(dquots[i], warntype[i]);
1128 static inline char ignore_hardlimit(struct dquot *dquot)
1130 struct mem_dqinfo *info = &sb_dqopt(dquot->dq_sb)->info[dquot->dq_type];
1132 return capable(CAP_SYS_RESOURCE) &&
1133 (info->dqi_format->qf_fmt_id != QFMT_VFS_OLD || !(info->dqi_flags & V1_DQF_RSQUASH));
1136 /* needs dq_data_lock */
1137 static int check_idq(struct dquot *dquot, qsize_t inodes, char *warntype)
1139 *warntype = QUOTA_NL_NOWARN;
1140 if (!sb_has_quota_limits_enabled(dquot->dq_sb, dquot->dq_type) ||
1141 test_bit(DQ_FAKE_B, &dquot->dq_flags))
1144 if (dquot->dq_dqb.dqb_ihardlimit &&
1145 (dquot->dq_dqb.dqb_curinodes + inodes) > dquot->dq_dqb.dqb_ihardlimit &&
1146 !ignore_hardlimit(dquot)) {
1147 *warntype = QUOTA_NL_IHARDWARN;
1151 if (dquot->dq_dqb.dqb_isoftlimit &&
1152 (dquot->dq_dqb.dqb_curinodes + inodes) > dquot->dq_dqb.dqb_isoftlimit &&
1153 dquot->dq_dqb.dqb_itime && get_seconds() >= dquot->dq_dqb.dqb_itime &&
1154 !ignore_hardlimit(dquot)) {
1155 *warntype = QUOTA_NL_ISOFTLONGWARN;
1159 if (dquot->dq_dqb.dqb_isoftlimit &&
1160 (dquot->dq_dqb.dqb_curinodes + inodes) > dquot->dq_dqb.dqb_isoftlimit &&
1161 dquot->dq_dqb.dqb_itime == 0) {
1162 *warntype = QUOTA_NL_ISOFTWARN;
1163 dquot->dq_dqb.dqb_itime = get_seconds() + sb_dqopt(dquot->dq_sb)->info[dquot->dq_type].dqi_igrace;
1169 /* needs dq_data_lock */
1170 static int check_bdq(struct dquot *dquot, qsize_t space, int prealloc, char *warntype)
1174 *warntype = QUOTA_NL_NOWARN;
1175 if (!sb_has_quota_limits_enabled(dquot->dq_sb, dquot->dq_type) ||
1176 test_bit(DQ_FAKE_B, &dquot->dq_flags))
1179 tspace = dquot->dq_dqb.dqb_curspace + dquot->dq_dqb.dqb_rsvspace
1182 if (dquot->dq_dqb.dqb_bhardlimit &&
1183 tspace > dquot->dq_dqb.dqb_bhardlimit &&
1184 !ignore_hardlimit(dquot)) {
1186 *warntype = QUOTA_NL_BHARDWARN;
1190 if (dquot->dq_dqb.dqb_bsoftlimit &&
1191 tspace > dquot->dq_dqb.dqb_bsoftlimit &&
1192 dquot->dq_dqb.dqb_btime && get_seconds() >= dquot->dq_dqb.dqb_btime &&
1193 !ignore_hardlimit(dquot)) {
1195 *warntype = QUOTA_NL_BSOFTLONGWARN;
1199 if (dquot->dq_dqb.dqb_bsoftlimit &&
1200 tspace > dquot->dq_dqb.dqb_bsoftlimit &&
1201 dquot->dq_dqb.dqb_btime == 0) {
1203 *warntype = QUOTA_NL_BSOFTWARN;
1204 dquot->dq_dqb.dqb_btime = get_seconds() + sb_dqopt(dquot->dq_sb)->info[dquot->dq_type].dqi_bgrace;
1208 * We don't allow preallocation to exceed softlimit so exceeding will
1217 static int info_idq_free(struct dquot *dquot, qsize_t inodes)
1219 if (test_bit(DQ_FAKE_B, &dquot->dq_flags) ||
1220 dquot->dq_dqb.dqb_curinodes <= dquot->dq_dqb.dqb_isoftlimit ||
1221 !sb_has_quota_limits_enabled(dquot->dq_sb, dquot->dq_type))
1222 return QUOTA_NL_NOWARN;
1224 if (dquot->dq_dqb.dqb_curinodes - inodes <= dquot->dq_dqb.dqb_isoftlimit)
1225 return QUOTA_NL_ISOFTBELOW;
1226 if (dquot->dq_dqb.dqb_curinodes >= dquot->dq_dqb.dqb_ihardlimit &&
1227 dquot->dq_dqb.dqb_curinodes - inodes < dquot->dq_dqb.dqb_ihardlimit)
1228 return QUOTA_NL_IHARDBELOW;
1229 return QUOTA_NL_NOWARN;
1232 static int info_bdq_free(struct dquot *dquot, qsize_t space)
1234 if (test_bit(DQ_FAKE_B, &dquot->dq_flags) ||
1235 dquot->dq_dqb.dqb_curspace <= dquot->dq_dqb.dqb_bsoftlimit)
1236 return QUOTA_NL_NOWARN;
1238 if (dquot->dq_dqb.dqb_curspace - space <= dquot->dq_dqb.dqb_bsoftlimit)
1239 return QUOTA_NL_BSOFTBELOW;
1240 if (dquot->dq_dqb.dqb_curspace >= dquot->dq_dqb.dqb_bhardlimit &&
1241 dquot->dq_dqb.dqb_curspace - space < dquot->dq_dqb.dqb_bhardlimit)
1242 return QUOTA_NL_BHARDBELOW;
1243 return QUOTA_NL_NOWARN;
1246 * Initialize quota pointers in inode
1247 * We do things in a bit complicated way but by that we avoid calling
1248 * dqget() and thus filesystem callbacks under dqptr_sem.
1250 int dquot_initialize(struct inode *inode, int type)
1252 unsigned int id = 0;
1254 struct dquot *got[MAXQUOTAS] = { NULL, NULL };
1255 struct super_block *sb = inode->i_sb;
1257 /* First test before acquiring mutex - solves deadlocks when we
1258 * re-enter the quota code and are already holding the mutex */
1259 if (IS_NOQUOTA(inode))
1262 /* First get references to structures we might need. */
1263 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1264 if (type != -1 && cnt != type)
1274 got[cnt] = dqget(sb, id, cnt);
1277 down_write(&sb_dqopt(sb)->dqptr_sem);
1278 /* Having dqptr_sem we know NOQUOTA flags can't be altered... */
1279 if (IS_NOQUOTA(inode))
1281 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1282 if (type != -1 && cnt != type)
1284 /* Avoid races with quotaoff() */
1285 if (!sb_has_quota_active(sb, cnt))
1287 if (!inode->i_dquot[cnt]) {
1288 inode->i_dquot[cnt] = got[cnt];
1293 up_write(&sb_dqopt(sb)->dqptr_sem);
1294 /* Drop unused references */
1295 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1299 EXPORT_SYMBOL(dquot_initialize);
1302 * Release all quotas referenced by inode
1304 int dquot_drop(struct inode *inode)
1307 struct dquot *put[MAXQUOTAS];
1309 down_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
1310 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1311 put[cnt] = inode->i_dquot[cnt];
1312 inode->i_dquot[cnt] = NULL;
1314 up_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
1316 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1320 EXPORT_SYMBOL(dquot_drop);
1322 /* Wrapper to remove references to quota structures from inode */
1323 void vfs_dq_drop(struct inode *inode)
1325 /* Here we can get arbitrary inode from clear_inode() so we have
1326 * to be careful. OTOH we don't need locking as quota operations
1327 * are allowed to change only at mount time */
1328 if (!IS_NOQUOTA(inode) && inode->i_sb && inode->i_sb->dq_op
1329 && inode->i_sb->dq_op->drop) {
1331 /* Test before calling to rule out calls from proc and such
1332 * where we are not allowed to block. Note that this is
1333 * actually reliable test even without the lock - the caller
1334 * must assure that nobody can come after the DQUOT_DROP and
1335 * add quota pointers back anyway */
1336 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1337 if (inode->i_dquot[cnt])
1339 if (cnt < MAXQUOTAS)
1340 inode->i_sb->dq_op->drop(inode);
1343 EXPORT_SYMBOL(vfs_dq_drop);
1346 * Following four functions update i_blocks+i_bytes fields and
1347 * quota information (together with appropriate checks)
1348 * NOTE: We absolutely rely on the fact that caller dirties
1349 * the inode (usually macros in quotaops.h care about this) and
1350 * holds a handle for the current transaction so that dquot write and
1351 * inode write go into the same transaction.
1355 * This operation can block, but only after everything is updated
1357 int __dquot_alloc_space(struct inode *inode, qsize_t number,
1358 int warn, int reserve)
1360 int cnt, ret = QUOTA_OK;
1361 char warntype[MAXQUOTAS];
1363 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1364 warntype[cnt] = QUOTA_NL_NOWARN;
1366 spin_lock(&dq_data_lock);
1367 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1368 if (!inode->i_dquot[cnt])
1370 if (check_bdq(inode->i_dquot[cnt], number, warn, warntype+cnt)
1376 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1377 if (!inode->i_dquot[cnt])
1380 dquot_resv_space(inode->i_dquot[cnt], number);
1382 dquot_incr_space(inode->i_dquot[cnt], number);
1385 inode_add_bytes(inode, number);
1387 spin_unlock(&dq_data_lock);
1388 flush_warnings(inode->i_dquot, warntype);
1392 int dquot_alloc_space(struct inode *inode, qsize_t number, int warn)
1394 int cnt, ret = QUOTA_OK;
1397 * First test before acquiring mutex - solves deadlocks when we
1398 * re-enter the quota code and are already holding the mutex
1400 if (IS_NOQUOTA(inode)) {
1401 inode_add_bytes(inode, number);
1405 down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1406 if (IS_NOQUOTA(inode)) {
1407 inode_add_bytes(inode, number);
1411 ret = __dquot_alloc_space(inode, number, warn, 0);
1412 if (ret == NO_QUOTA)
1415 /* Dirtify all the dquots - this can block when journalling */
1416 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1417 if (inode->i_dquot[cnt])
1418 mark_dquot_dirty(inode->i_dquot[cnt]);
1420 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1424 EXPORT_SYMBOL(dquot_alloc_space);
1426 int dquot_reserve_space(struct inode *inode, qsize_t number, int warn)
1430 if (IS_NOQUOTA(inode))
1433 down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1434 if (IS_NOQUOTA(inode))
1437 ret = __dquot_alloc_space(inode, number, warn, 1);
1439 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1443 EXPORT_SYMBOL(dquot_reserve_space);
1446 * This operation can block, but only after everything is updated
1448 int dquot_alloc_inode(const struct inode *inode, qsize_t number)
1450 int cnt, ret = NO_QUOTA;
1451 char warntype[MAXQUOTAS];
1453 /* First test before acquiring mutex - solves deadlocks when we
1454 * re-enter the quota code and are already holding the mutex */
1455 if (IS_NOQUOTA(inode))
1457 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1458 warntype[cnt] = QUOTA_NL_NOWARN;
1459 down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1460 if (IS_NOQUOTA(inode)) {
1461 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1464 spin_lock(&dq_data_lock);
1465 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1466 if (!inode->i_dquot[cnt])
1468 if (check_idq(inode->i_dquot[cnt], number, warntype+cnt) == NO_QUOTA)
1472 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1473 if (!inode->i_dquot[cnt])
1475 dquot_incr_inodes(inode->i_dquot[cnt], number);
1479 spin_unlock(&dq_data_lock);
1480 if (ret == QUOTA_OK)
1481 /* Dirtify all the dquots - this can block when journalling */
1482 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1483 if (inode->i_dquot[cnt])
1484 mark_dquot_dirty(inode->i_dquot[cnt]);
1485 flush_warnings(inode->i_dquot, warntype);
1486 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1489 EXPORT_SYMBOL(dquot_alloc_inode);
1491 int dquot_claim_space(struct inode *inode, qsize_t number)
1496 if (IS_NOQUOTA(inode)) {
1497 inode_add_bytes(inode, number);
1501 down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1502 if (IS_NOQUOTA(inode)) {
1503 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1504 inode_add_bytes(inode, number);
1508 spin_lock(&dq_data_lock);
1509 /* Claim reserved quotas to allocated quotas */
1510 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1511 if (inode->i_dquot[cnt])
1512 dquot_claim_reserved_space(inode->i_dquot[cnt],
1515 /* Update inode bytes */
1516 inode_add_bytes(inode, number);
1517 spin_unlock(&dq_data_lock);
1518 /* Dirtify all the dquots - this can block when journalling */
1519 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1520 if (inode->i_dquot[cnt])
1521 mark_dquot_dirty(inode->i_dquot[cnt]);
1522 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1526 EXPORT_SYMBOL(dquot_claim_space);
1529 * Release reserved quota space
1531 void dquot_release_reserved_space(struct inode *inode, qsize_t number)
1535 if (IS_NOQUOTA(inode))
1538 down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1539 if (IS_NOQUOTA(inode))
1542 spin_lock(&dq_data_lock);
1543 /* Release reserved dquots */
1544 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1545 if (inode->i_dquot[cnt])
1546 dquot_free_reserved_space(inode->i_dquot[cnt], number);
1548 spin_unlock(&dq_data_lock);
1551 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1555 EXPORT_SYMBOL(dquot_release_reserved_space);
1558 * This operation can block, but only after everything is updated
1560 int dquot_free_space(struct inode *inode, qsize_t number)
1563 char warntype[MAXQUOTAS];
1565 /* First test before acquiring mutex - solves deadlocks when we
1566 * re-enter the quota code and are already holding the mutex */
1567 if (IS_NOQUOTA(inode)) {
1569 inode_sub_bytes(inode, number);
1573 down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1574 /* Now recheck reliably when holding dqptr_sem */
1575 if (IS_NOQUOTA(inode)) {
1576 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1579 spin_lock(&dq_data_lock);
1580 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1581 if (!inode->i_dquot[cnt])
1583 warntype[cnt] = info_bdq_free(inode->i_dquot[cnt], number);
1584 dquot_decr_space(inode->i_dquot[cnt], number);
1586 inode_sub_bytes(inode, number);
1587 spin_unlock(&dq_data_lock);
1588 /* Dirtify all the dquots - this can block when journalling */
1589 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1590 if (inode->i_dquot[cnt])
1591 mark_dquot_dirty(inode->i_dquot[cnt]);
1592 flush_warnings(inode->i_dquot, warntype);
1593 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1596 EXPORT_SYMBOL(dquot_free_space);
1599 * This operation can block, but only after everything is updated
1601 int dquot_free_inode(const struct inode *inode, qsize_t number)
1604 char warntype[MAXQUOTAS];
1606 /* First test before acquiring mutex - solves deadlocks when we
1607 * re-enter the quota code and are already holding the mutex */
1608 if (IS_NOQUOTA(inode))
1611 down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1612 /* Now recheck reliably when holding dqptr_sem */
1613 if (IS_NOQUOTA(inode)) {
1614 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1617 spin_lock(&dq_data_lock);
1618 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1619 if (!inode->i_dquot[cnt])
1621 warntype[cnt] = info_idq_free(inode->i_dquot[cnt], number);
1622 dquot_decr_inodes(inode->i_dquot[cnt], number);
1624 spin_unlock(&dq_data_lock);
1625 /* Dirtify all the dquots - this can block when journalling */
1626 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1627 if (inode->i_dquot[cnt])
1628 mark_dquot_dirty(inode->i_dquot[cnt]);
1629 flush_warnings(inode->i_dquot, warntype);
1630 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1633 EXPORT_SYMBOL(dquot_free_inode);
1636 * call back function, get reserved quota space from underlying fs
1638 qsize_t dquot_get_reserved_space(struct inode *inode)
1640 qsize_t reserved_space = 0;
1642 if (sb_any_quota_active(inode->i_sb) &&
1643 inode->i_sb->dq_op->get_reserved_space)
1644 reserved_space = inode->i_sb->dq_op->get_reserved_space(inode);
1645 return reserved_space;
1649 * Transfer the number of inode and blocks from one diskquota to an other.
1651 * This operation can block, but only after everything is updated
1652 * A transaction must be started when entering this function.
1654 int dquot_transfer(struct inode *inode, struct iattr *iattr)
1656 qsize_t space, cur_space;
1657 qsize_t rsv_space = 0;
1658 struct dquot *transfer_from[MAXQUOTAS];
1659 struct dquot *transfer_to[MAXQUOTAS];
1660 int cnt, ret = QUOTA_OK;
1661 int chuid = iattr->ia_valid & ATTR_UID && inode->i_uid != iattr->ia_uid,
1662 chgid = iattr->ia_valid & ATTR_GID && inode->i_gid != iattr->ia_gid;
1663 char warntype_to[MAXQUOTAS];
1664 char warntype_from_inodes[MAXQUOTAS], warntype_from_space[MAXQUOTAS];
1666 /* First test before acquiring mutex - solves deadlocks when we
1667 * re-enter the quota code and are already holding the mutex */
1668 if (IS_NOQUOTA(inode))
1670 /* Initialize the arrays */
1671 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1672 transfer_from[cnt] = NULL;
1673 transfer_to[cnt] = NULL;
1674 warntype_to[cnt] = QUOTA_NL_NOWARN;
1679 transfer_to[cnt] = dqget(inode->i_sb, iattr->ia_uid, cnt);
1684 transfer_to[cnt] = dqget(inode->i_sb, iattr->ia_gid, cnt);
1689 down_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
1690 /* Now recheck reliably when holding dqptr_sem */
1691 if (IS_NOQUOTA(inode)) { /* File without quota accounting? */
1692 up_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
1695 spin_lock(&dq_data_lock);
1696 cur_space = inode_get_bytes(inode);
1697 rsv_space = dquot_get_reserved_space(inode);
1698 space = cur_space + rsv_space;
1699 /* Build the transfer_from list and check the limits */
1700 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1701 if (!transfer_to[cnt])
1703 transfer_from[cnt] = inode->i_dquot[cnt];
1704 if (check_idq(transfer_to[cnt], 1, warntype_to + cnt) ==
1705 NO_QUOTA || check_bdq(transfer_to[cnt], space, 0,
1706 warntype_to + cnt) == NO_QUOTA)
1711 * Finally perform the needed transfer from transfer_from to transfer_to
1713 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1715 * Skip changes for same uid or gid or for turned off quota-type.
1717 if (!transfer_to[cnt])
1720 /* Due to IO error we might not have transfer_from[] structure */
1721 if (transfer_from[cnt]) {
1722 warntype_from_inodes[cnt] =
1723 info_idq_free(transfer_from[cnt], 1);
1724 warntype_from_space[cnt] =
1725 info_bdq_free(transfer_from[cnt], space);
1726 dquot_decr_inodes(transfer_from[cnt], 1);
1727 dquot_decr_space(transfer_from[cnt], cur_space);
1728 dquot_free_reserved_space(transfer_from[cnt],
1732 dquot_incr_inodes(transfer_to[cnt], 1);
1733 dquot_incr_space(transfer_to[cnt], cur_space);
1734 dquot_resv_space(transfer_to[cnt], rsv_space);
1736 inode->i_dquot[cnt] = transfer_to[cnt];
1738 spin_unlock(&dq_data_lock);
1739 up_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
1741 /* Dirtify all the dquots - this can block when journalling */
1742 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1743 if (transfer_from[cnt])
1744 mark_dquot_dirty(transfer_from[cnt]);
1745 if (transfer_to[cnt]) {
1746 mark_dquot_dirty(transfer_to[cnt]);
1747 /* The reference we got is transferred to the inode */
1748 transfer_to[cnt] = NULL;
1752 flush_warnings(transfer_to, warntype_to);
1753 flush_warnings(transfer_from, warntype_from_inodes);
1754 flush_warnings(transfer_from, warntype_from_space);
1756 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1757 dqput(transfer_from[cnt]);
1758 dqput(transfer_to[cnt]);
1762 spin_unlock(&dq_data_lock);
1763 up_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
1764 /* Clear dquot pointers we don't want to dqput() */
1765 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1766 transfer_from[cnt] = NULL;
1770 EXPORT_SYMBOL(dquot_transfer);
1772 /* Wrapper for transferring ownership of an inode */
1773 int vfs_dq_transfer(struct inode *inode, struct iattr *iattr)
1775 if (sb_any_quota_active(inode->i_sb) && !IS_NOQUOTA(inode)) {
1777 if (inode->i_sb->dq_op->transfer(inode, iattr) == NO_QUOTA)
1782 EXPORT_SYMBOL(vfs_dq_transfer);
1785 * Write info of quota file to disk
1787 int dquot_commit_info(struct super_block *sb, int type)
1790 struct quota_info *dqopt = sb_dqopt(sb);
1792 mutex_lock(&dqopt->dqio_mutex);
1793 ret = dqopt->ops[type]->write_file_info(sb, type);
1794 mutex_unlock(&dqopt->dqio_mutex);
1797 EXPORT_SYMBOL(dquot_commit_info);
1800 * Definitions of diskquota operations.
1802 struct dquot_operations dquot_operations = {
1803 .initialize = dquot_initialize,
1805 .alloc_space = dquot_alloc_space,
1806 .alloc_inode = dquot_alloc_inode,
1807 .free_space = dquot_free_space,
1808 .free_inode = dquot_free_inode,
1809 .transfer = dquot_transfer,
1810 .write_dquot = dquot_commit,
1811 .acquire_dquot = dquot_acquire,
1812 .release_dquot = dquot_release,
1813 .mark_dirty = dquot_mark_dquot_dirty,
1814 .write_info = dquot_commit_info,
1815 .alloc_dquot = dquot_alloc,
1816 .destroy_dquot = dquot_destroy,
1820 * Turn quota off on a device. type == -1 ==> quotaoff for all types (umount)
1822 int vfs_quota_disable(struct super_block *sb, int type, unsigned int flags)
1825 struct quota_info *dqopt = sb_dqopt(sb);
1826 struct inode *toputinode[MAXQUOTAS];
1828 /* Cannot turn off usage accounting without turning off limits, or
1829 * suspend quotas and simultaneously turn quotas off. */
1830 if ((flags & DQUOT_USAGE_ENABLED && !(flags & DQUOT_LIMITS_ENABLED))
1831 || (flags & DQUOT_SUSPENDED && flags & (DQUOT_LIMITS_ENABLED |
1832 DQUOT_USAGE_ENABLED)))
1835 /* We need to serialize quota_off() for device */
1836 mutex_lock(&dqopt->dqonoff_mutex);
1839 * Skip everything if there's nothing to do. We have to do this because
1840 * sometimes we are called when fill_super() failed and calling
1841 * sync_fs() in such cases does no good.
1843 if (!sb_any_quota_loaded(sb)) {
1844 mutex_unlock(&dqopt->dqonoff_mutex);
1847 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1848 toputinode[cnt] = NULL;
1849 if (type != -1 && cnt != type)
1851 if (!sb_has_quota_loaded(sb, cnt))
1854 if (flags & DQUOT_SUSPENDED) {
1855 spin_lock(&dq_state_lock);
1857 dquot_state_flag(DQUOT_SUSPENDED, cnt);
1858 spin_unlock(&dq_state_lock);
1860 spin_lock(&dq_state_lock);
1861 dqopt->flags &= ~dquot_state_flag(flags, cnt);
1862 /* Turning off suspended quotas? */
1863 if (!sb_has_quota_loaded(sb, cnt) &&
1864 sb_has_quota_suspended(sb, cnt)) {
1865 dqopt->flags &= ~dquot_state_flag(
1866 DQUOT_SUSPENDED, cnt);
1867 spin_unlock(&dq_state_lock);
1868 iput(dqopt->files[cnt]);
1869 dqopt->files[cnt] = NULL;
1872 spin_unlock(&dq_state_lock);
1875 /* We still have to keep quota loaded? */
1876 if (sb_has_quota_loaded(sb, cnt) && !(flags & DQUOT_SUSPENDED))
1879 /* Note: these are blocking operations */
1880 drop_dquot_ref(sb, cnt);
1881 invalidate_dquots(sb, cnt);
1883 * Now all dquots should be invalidated, all writes done so we should be only
1884 * users of the info. No locks needed.
1886 if (info_dirty(&dqopt->info[cnt]))
1887 sb->dq_op->write_info(sb, cnt);
1888 if (dqopt->ops[cnt]->free_file_info)
1889 dqopt->ops[cnt]->free_file_info(sb, cnt);
1890 put_quota_format(dqopt->info[cnt].dqi_format);
1892 toputinode[cnt] = dqopt->files[cnt];
1893 if (!sb_has_quota_loaded(sb, cnt))
1894 dqopt->files[cnt] = NULL;
1895 dqopt->info[cnt].dqi_flags = 0;
1896 dqopt->info[cnt].dqi_igrace = 0;
1897 dqopt->info[cnt].dqi_bgrace = 0;
1898 dqopt->ops[cnt] = NULL;
1900 mutex_unlock(&dqopt->dqonoff_mutex);
1902 /* Skip syncing and setting flags if quota files are hidden */
1903 if (dqopt->flags & DQUOT_QUOTA_SYS_FILE)
1906 /* Sync the superblock so that buffers with quota data are written to
1907 * disk (and so userspace sees correct data afterwards). */
1908 if (sb->s_op->sync_fs)
1909 sb->s_op->sync_fs(sb, 1);
1910 sync_blockdev(sb->s_bdev);
1911 /* Now the quota files are just ordinary files and we can set the
1912 * inode flags back. Moreover we discard the pagecache so that
1913 * userspace sees the writes we did bypassing the pagecache. We
1914 * must also discard the blockdev buffers so that we see the
1915 * changes done by userspace on the next quotaon() */
1916 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1917 if (toputinode[cnt]) {
1918 mutex_lock(&dqopt->dqonoff_mutex);
1919 /* If quota was reenabled in the meantime, we have
1921 if (!sb_has_quota_loaded(sb, cnt)) {
1922 mutex_lock_nested(&toputinode[cnt]->i_mutex, I_MUTEX_QUOTA);
1923 toputinode[cnt]->i_flags &= ~(S_IMMUTABLE |
1924 S_NOATIME | S_NOQUOTA);
1925 truncate_inode_pages(&toputinode[cnt]->i_data, 0);
1926 mutex_unlock(&toputinode[cnt]->i_mutex);
1927 mark_inode_dirty(toputinode[cnt]);
1929 mutex_unlock(&dqopt->dqonoff_mutex);
1932 invalidate_bdev(sb->s_bdev);
1934 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1935 if (toputinode[cnt]) {
1936 /* On remount RO, we keep the inode pointer so that we
1937 * can reenable quota on the subsequent remount RW. We
1938 * have to check 'flags' variable and not use sb_has_
1939 * function because another quotaon / quotaoff could
1940 * change global state before we got here. We refuse
1941 * to suspend quotas when there is pending delete on
1942 * the quota file... */
1943 if (!(flags & DQUOT_SUSPENDED))
1944 iput(toputinode[cnt]);
1945 else if (!toputinode[cnt]->i_nlink)
1950 EXPORT_SYMBOL(vfs_quota_disable);
1952 int vfs_quota_off(struct super_block *sb, int type, int remount)
1954 return vfs_quota_disable(sb, type, remount ? DQUOT_SUSPENDED :
1955 (DQUOT_USAGE_ENABLED | DQUOT_LIMITS_ENABLED));
1957 EXPORT_SYMBOL(vfs_quota_off);
1959 * Turn quotas on on a device
1963 * Helper function to turn quotas on when we already have the inode of
1964 * quota file and no quota information is loaded.
1966 static int vfs_load_quota_inode(struct inode *inode, int type, int format_id,
1969 struct quota_format_type *fmt = find_quota_format(format_id);
1970 struct super_block *sb = inode->i_sb;
1971 struct quota_info *dqopt = sb_dqopt(sb);
1977 if (!S_ISREG(inode->i_mode)) {
1981 if (IS_RDONLY(inode)) {
1985 if (!sb->s_op->quota_write || !sb->s_op->quota_read) {
1989 /* Usage always has to be set... */
1990 if (!(flags & DQUOT_USAGE_ENABLED)) {
1995 if (!(dqopt->flags & DQUOT_QUOTA_SYS_FILE)) {
1996 /* As we bypass the pagecache we must now flush the inode so
1997 * that we see all the changes from userspace... */
1998 write_inode_now(inode, 1);
1999 /* And now flush the block cache so that kernel sees the
2001 invalidate_bdev(sb->s_bdev);
2003 mutex_lock(&inode->i_mutex);
2004 mutex_lock(&dqopt->dqonoff_mutex);
2005 if (sb_has_quota_loaded(sb, type)) {
2010 if (!(dqopt->flags & DQUOT_QUOTA_SYS_FILE)) {
2011 /* We don't want quota and atime on quota files (deadlocks
2012 * possible) Also nobody should write to the file - we use
2013 * special IO operations which ignore the immutable bit. */
2014 down_write(&dqopt->dqptr_sem);
2015 oldflags = inode->i_flags & (S_NOATIME | S_IMMUTABLE | S_NOQUOTA);
2016 inode->i_flags |= S_NOQUOTA | S_NOATIME | S_IMMUTABLE;
2017 up_write(&dqopt->dqptr_sem);
2018 sb->dq_op->drop(inode);
2022 dqopt->files[type] = igrab(inode);
2023 if (!dqopt->files[type])
2026 if (!fmt->qf_ops->check_quota_file(sb, type))
2029 dqopt->ops[type] = fmt->qf_ops;
2030 dqopt->info[type].dqi_format = fmt;
2031 dqopt->info[type].dqi_fmt_id = format_id;
2032 INIT_LIST_HEAD(&dqopt->info[type].dqi_dirty_list);
2033 mutex_lock(&dqopt->dqio_mutex);
2034 if ((error = dqopt->ops[type]->read_file_info(sb, type)) < 0) {
2035 mutex_unlock(&dqopt->dqio_mutex);
2038 mutex_unlock(&dqopt->dqio_mutex);
2039 mutex_unlock(&inode->i_mutex);
2040 spin_lock(&dq_state_lock);
2041 dqopt->flags |= dquot_state_flag(flags, type);
2042 spin_unlock(&dq_state_lock);
2044 add_dquot_ref(sb, type);
2045 mutex_unlock(&dqopt->dqonoff_mutex);
2050 dqopt->files[type] = NULL;
2053 mutex_unlock(&dqopt->dqonoff_mutex);
2054 if (oldflags != -1) {
2055 down_write(&dqopt->dqptr_sem);
2056 /* Set the flags back (in the case of accidental quotaon()
2057 * on a wrong file we don't want to mess up the flags) */
2058 inode->i_flags &= ~(S_NOATIME | S_NOQUOTA | S_IMMUTABLE);
2059 inode->i_flags |= oldflags;
2060 up_write(&dqopt->dqptr_sem);
2062 mutex_unlock(&inode->i_mutex);
2064 put_quota_format(fmt);
2069 /* Reenable quotas on remount RW */
2070 static int vfs_quota_on_remount(struct super_block *sb, int type)
2072 struct quota_info *dqopt = sb_dqopt(sb);
2073 struct inode *inode;
2077 mutex_lock(&dqopt->dqonoff_mutex);
2078 if (!sb_has_quota_suspended(sb, type)) {
2079 mutex_unlock(&dqopt->dqonoff_mutex);
2082 inode = dqopt->files[type];
2083 dqopt->files[type] = NULL;
2084 spin_lock(&dq_state_lock);
2085 flags = dqopt->flags & dquot_state_flag(DQUOT_USAGE_ENABLED |
2086 DQUOT_LIMITS_ENABLED, type);
2087 dqopt->flags &= ~dquot_state_flag(DQUOT_STATE_FLAGS, type);
2088 spin_unlock(&dq_state_lock);
2089 mutex_unlock(&dqopt->dqonoff_mutex);
2091 flags = dquot_generic_flag(flags, type);
2092 ret = vfs_load_quota_inode(inode, type, dqopt->info[type].dqi_fmt_id,
2099 int vfs_quota_on_path(struct super_block *sb, int type, int format_id,
2102 int error = security_quota_on(path->dentry);
2105 /* Quota file not on the same filesystem? */
2106 if (path->mnt->mnt_sb != sb)
2109 error = vfs_load_quota_inode(path->dentry->d_inode, type,
2110 format_id, DQUOT_USAGE_ENABLED |
2111 DQUOT_LIMITS_ENABLED);
2114 EXPORT_SYMBOL(vfs_quota_on_path);
2116 int vfs_quota_on(struct super_block *sb, int type, int format_id, char *name,
2123 return vfs_quota_on_remount(sb, type);
2125 error = kern_path(name, LOOKUP_FOLLOW, &path);
2127 error = vfs_quota_on_path(sb, type, format_id, &path);
2132 EXPORT_SYMBOL(vfs_quota_on);
2135 * More powerful function for turning on quotas allowing setting
2136 * of individual quota flags
2138 int vfs_quota_enable(struct inode *inode, int type, int format_id,
2142 struct super_block *sb = inode->i_sb;
2143 struct quota_info *dqopt = sb_dqopt(sb);
2145 /* Just unsuspend quotas? */
2146 if (flags & DQUOT_SUSPENDED)
2147 return vfs_quota_on_remount(sb, type);
2150 /* Just updating flags needed? */
2151 if (sb_has_quota_loaded(sb, type)) {
2152 mutex_lock(&dqopt->dqonoff_mutex);
2153 /* Now do a reliable test... */
2154 if (!sb_has_quota_loaded(sb, type)) {
2155 mutex_unlock(&dqopt->dqonoff_mutex);
2158 if (flags & DQUOT_USAGE_ENABLED &&
2159 sb_has_quota_usage_enabled(sb, type)) {
2163 if (flags & DQUOT_LIMITS_ENABLED &&
2164 sb_has_quota_limits_enabled(sb, type)) {
2168 spin_lock(&dq_state_lock);
2169 sb_dqopt(sb)->flags |= dquot_state_flag(flags, type);
2170 spin_unlock(&dq_state_lock);
2172 mutex_unlock(&dqopt->dqonoff_mutex);
2177 return vfs_load_quota_inode(inode, type, format_id, flags);
2179 EXPORT_SYMBOL(vfs_quota_enable);
2182 * This function is used when filesystem needs to initialize quotas
2183 * during mount time.
2185 int vfs_quota_on_mount(struct super_block *sb, char *qf_name,
2186 int format_id, int type)
2188 struct dentry *dentry;
2191 dentry = lookup_one_len(qf_name, sb->s_root, strlen(qf_name));
2193 return PTR_ERR(dentry);
2195 if (!dentry->d_inode) {
2200 error = security_quota_on(dentry);
2202 error = vfs_load_quota_inode(dentry->d_inode, type, format_id,
2203 DQUOT_USAGE_ENABLED | DQUOT_LIMITS_ENABLED);
2209 EXPORT_SYMBOL(vfs_quota_on_mount);
2211 /* Wrapper to turn on quotas when remounting rw */
2212 int vfs_dq_quota_on_remount(struct super_block *sb)
2217 if (!sb->s_qcop || !sb->s_qcop->quota_on)
2219 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
2220 err = sb->s_qcop->quota_on(sb, cnt, 0, NULL, 1);
2221 if (err < 0 && !ret)
2226 EXPORT_SYMBOL(vfs_dq_quota_on_remount);
2228 static inline qsize_t qbtos(qsize_t blocks)
2230 return blocks << QIF_DQBLKSIZE_BITS;
2233 static inline qsize_t stoqb(qsize_t space)
2235 return (space + QIF_DQBLKSIZE - 1) >> QIF_DQBLKSIZE_BITS;
2238 /* Generic routine for getting common part of quota structure */
2239 static void do_get_dqblk(struct dquot *dquot, struct if_dqblk *di)
2241 struct mem_dqblk *dm = &dquot->dq_dqb;
2243 spin_lock(&dq_data_lock);
2244 di->dqb_bhardlimit = stoqb(dm->dqb_bhardlimit);
2245 di->dqb_bsoftlimit = stoqb(dm->dqb_bsoftlimit);
2246 di->dqb_curspace = dm->dqb_curspace + dm->dqb_rsvspace;
2247 di->dqb_ihardlimit = dm->dqb_ihardlimit;
2248 di->dqb_isoftlimit = dm->dqb_isoftlimit;
2249 di->dqb_curinodes = dm->dqb_curinodes;
2250 di->dqb_btime = dm->dqb_btime;
2251 di->dqb_itime = dm->dqb_itime;
2252 di->dqb_valid = QIF_ALL;
2253 spin_unlock(&dq_data_lock);
2256 int vfs_get_dqblk(struct super_block *sb, int type, qid_t id, struct if_dqblk *di)
2258 struct dquot *dquot;
2260 dquot = dqget(sb, id, type);
2263 do_get_dqblk(dquot, di);
2268 EXPORT_SYMBOL(vfs_get_dqblk);
2270 /* Generic routine for setting common part of quota structure */
2271 static int do_set_dqblk(struct dquot *dquot, struct if_dqblk *di)
2273 struct mem_dqblk *dm = &dquot->dq_dqb;
2274 int check_blim = 0, check_ilim = 0;
2275 struct mem_dqinfo *dqi = &sb_dqopt(dquot->dq_sb)->info[dquot->dq_type];
2277 if ((di->dqb_valid & QIF_BLIMITS &&
2278 (di->dqb_bhardlimit > dqi->dqi_maxblimit ||
2279 di->dqb_bsoftlimit > dqi->dqi_maxblimit)) ||
2280 (di->dqb_valid & QIF_ILIMITS &&
2281 (di->dqb_ihardlimit > dqi->dqi_maxilimit ||
2282 di->dqb_isoftlimit > dqi->dqi_maxilimit)))
2285 spin_lock(&dq_data_lock);
2286 if (di->dqb_valid & QIF_SPACE) {
2287 dm->dqb_curspace = di->dqb_curspace - dm->dqb_rsvspace;
2289 __set_bit(DQ_LASTSET_B + QIF_SPACE_B, &dquot->dq_flags);
2291 if (di->dqb_valid & QIF_BLIMITS) {
2292 dm->dqb_bsoftlimit = qbtos(di->dqb_bsoftlimit);
2293 dm->dqb_bhardlimit = qbtos(di->dqb_bhardlimit);
2295 __set_bit(DQ_LASTSET_B + QIF_BLIMITS_B, &dquot->dq_flags);
2297 if (di->dqb_valid & QIF_INODES) {
2298 dm->dqb_curinodes = di->dqb_curinodes;
2300 __set_bit(DQ_LASTSET_B + QIF_INODES_B, &dquot->dq_flags);
2302 if (di->dqb_valid & QIF_ILIMITS) {
2303 dm->dqb_isoftlimit = di->dqb_isoftlimit;
2304 dm->dqb_ihardlimit = di->dqb_ihardlimit;
2306 __set_bit(DQ_LASTSET_B + QIF_ILIMITS_B, &dquot->dq_flags);
2308 if (di->dqb_valid & QIF_BTIME) {
2309 dm->dqb_btime = di->dqb_btime;
2311 __set_bit(DQ_LASTSET_B + QIF_BTIME_B, &dquot->dq_flags);
2313 if (di->dqb_valid & QIF_ITIME) {
2314 dm->dqb_itime = di->dqb_itime;
2316 __set_bit(DQ_LASTSET_B + QIF_ITIME_B, &dquot->dq_flags);
2320 if (!dm->dqb_bsoftlimit || dm->dqb_curspace < dm->dqb_bsoftlimit) {
2322 clear_bit(DQ_BLKS_B, &dquot->dq_flags);
2324 else if (!(di->dqb_valid & QIF_BTIME)) /* Set grace only if user hasn't provided his own... */
2325 dm->dqb_btime = get_seconds() + dqi->dqi_bgrace;
2328 if (!dm->dqb_isoftlimit || dm->dqb_curinodes < dm->dqb_isoftlimit) {
2330 clear_bit(DQ_INODES_B, &dquot->dq_flags);
2332 else if (!(di->dqb_valid & QIF_ITIME)) /* Set grace only if user hasn't provided his own... */
2333 dm->dqb_itime = get_seconds() + dqi->dqi_igrace;
2335 if (dm->dqb_bhardlimit || dm->dqb_bsoftlimit || dm->dqb_ihardlimit || dm->dqb_isoftlimit)
2336 clear_bit(DQ_FAKE_B, &dquot->dq_flags);
2338 set_bit(DQ_FAKE_B, &dquot->dq_flags);
2339 spin_unlock(&dq_data_lock);
2340 mark_dquot_dirty(dquot);
2345 int vfs_set_dqblk(struct super_block *sb, int type, qid_t id, struct if_dqblk *di)
2347 struct dquot *dquot;
2350 dquot = dqget(sb, id, type);
2355 rc = do_set_dqblk(dquot, di);
2360 EXPORT_SYMBOL(vfs_set_dqblk);
2362 /* Generic routine for getting common part of quota file information */
2363 int vfs_get_dqinfo(struct super_block *sb, int type, struct if_dqinfo *ii)
2365 struct mem_dqinfo *mi;
2367 mutex_lock(&sb_dqopt(sb)->dqonoff_mutex);
2368 if (!sb_has_quota_active(sb, type)) {
2369 mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
2372 mi = sb_dqopt(sb)->info + type;
2373 spin_lock(&dq_data_lock);
2374 ii->dqi_bgrace = mi->dqi_bgrace;
2375 ii->dqi_igrace = mi->dqi_igrace;
2376 ii->dqi_flags = mi->dqi_flags & DQF_MASK;
2377 ii->dqi_valid = IIF_ALL;
2378 spin_unlock(&dq_data_lock);
2379 mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
2382 EXPORT_SYMBOL(vfs_get_dqinfo);
2384 /* Generic routine for setting common part of quota file information */
2385 int vfs_set_dqinfo(struct super_block *sb, int type, struct if_dqinfo *ii)
2387 struct mem_dqinfo *mi;
2390 mutex_lock(&sb_dqopt(sb)->dqonoff_mutex);
2391 if (!sb_has_quota_active(sb, type)) {
2395 mi = sb_dqopt(sb)->info + type;
2396 spin_lock(&dq_data_lock);
2397 if (ii->dqi_valid & IIF_BGRACE)
2398 mi->dqi_bgrace = ii->dqi_bgrace;
2399 if (ii->dqi_valid & IIF_IGRACE)
2400 mi->dqi_igrace = ii->dqi_igrace;
2401 if (ii->dqi_valid & IIF_FLAGS)
2402 mi->dqi_flags = (mi->dqi_flags & ~DQF_MASK) | (ii->dqi_flags & DQF_MASK);
2403 spin_unlock(&dq_data_lock);
2404 mark_info_dirty(sb, type);
2405 /* Force write to disk */
2406 sb->dq_op->write_info(sb, type);
2408 mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
2411 EXPORT_SYMBOL(vfs_set_dqinfo);
2413 struct quotactl_ops vfs_quotactl_ops = {
2414 .quota_on = vfs_quota_on,
2415 .quota_off = vfs_quota_off,
2416 .quota_sync = vfs_quota_sync,
2417 .get_info = vfs_get_dqinfo,
2418 .set_info = vfs_set_dqinfo,
2419 .get_dqblk = vfs_get_dqblk,
2420 .set_dqblk = vfs_set_dqblk
2423 static ctl_table fs_dqstats_table[] = {
2425 .ctl_name = FS_DQ_LOOKUPS,
2426 .procname = "lookups",
2427 .data = &dqstats.lookups,
2428 .maxlen = sizeof(int),
2430 .proc_handler = &proc_dointvec,
2433 .ctl_name = FS_DQ_DROPS,
2434 .procname = "drops",
2435 .data = &dqstats.drops,
2436 .maxlen = sizeof(int),
2438 .proc_handler = &proc_dointvec,
2441 .ctl_name = FS_DQ_READS,
2442 .procname = "reads",
2443 .data = &dqstats.reads,
2444 .maxlen = sizeof(int),
2446 .proc_handler = &proc_dointvec,
2449 .ctl_name = FS_DQ_WRITES,
2450 .procname = "writes",
2451 .data = &dqstats.writes,
2452 .maxlen = sizeof(int),
2454 .proc_handler = &proc_dointvec,
2457 .ctl_name = FS_DQ_CACHE_HITS,
2458 .procname = "cache_hits",
2459 .data = &dqstats.cache_hits,
2460 .maxlen = sizeof(int),
2462 .proc_handler = &proc_dointvec,
2465 .ctl_name = FS_DQ_ALLOCATED,
2466 .procname = "allocated_dquots",
2467 .data = &dqstats.allocated_dquots,
2468 .maxlen = sizeof(int),
2470 .proc_handler = &proc_dointvec,
2473 .ctl_name = FS_DQ_FREE,
2474 .procname = "free_dquots",
2475 .data = &dqstats.free_dquots,
2476 .maxlen = sizeof(int),
2478 .proc_handler = &proc_dointvec,
2481 .ctl_name = FS_DQ_SYNCS,
2482 .procname = "syncs",
2483 .data = &dqstats.syncs,
2484 .maxlen = sizeof(int),
2486 .proc_handler = &proc_dointvec,
2488 #ifdef CONFIG_PRINT_QUOTA_WARNING
2490 .ctl_name = FS_DQ_WARNINGS,
2491 .procname = "warnings",
2492 .data = &flag_print_warnings,
2493 .maxlen = sizeof(int),
2495 .proc_handler = &proc_dointvec,
2501 static ctl_table fs_table[] = {
2503 .ctl_name = FS_DQSTATS,
2504 .procname = "quota",
2506 .child = fs_dqstats_table,
2511 static ctl_table sys_table[] = {
2521 static int __init dquot_init(void)
2524 unsigned long nr_hash, order;
2526 printk(KERN_NOTICE "VFS: Disk quotas %s\n", __DQUOT_VERSION__);
2528 register_sysctl_table(sys_table);
2530 dquot_cachep = kmem_cache_create("dquot",
2531 sizeof(struct dquot), sizeof(unsigned long) * 4,
2532 (SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT|
2533 SLAB_MEM_SPREAD|SLAB_PANIC),
2537 dquot_hash = (struct hlist_head *)__get_free_pages(GFP_ATOMIC, order);
2539 panic("Cannot create dquot hash table");
2541 /* Find power-of-two hlist_heads which can fit into allocation */
2542 nr_hash = (1UL << order) * PAGE_SIZE / sizeof(struct hlist_head);
2546 } while (nr_hash >> dq_hash_bits);
2549 nr_hash = 1UL << dq_hash_bits;
2550 dq_hash_mask = nr_hash - 1;
2551 for (i = 0; i < nr_hash; i++)
2552 INIT_HLIST_HEAD(dquot_hash + i);
2554 printk("Dquot-cache hash table entries: %ld (order %ld, %ld bytes)\n",
2555 nr_hash, order, (PAGE_SIZE << order));
2557 register_shrinker(&dqcache_shrinker);
2559 #ifdef CONFIG_QUOTA_NETLINK_INTERFACE
2560 if (genl_register_family("a_genl_family) != 0)
2561 printk(KERN_ERR "VFS: Failed to create quota netlink interface.\n");
2566 module_init(dquot_init);