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 DEFINE_SPINLOCK(dq_list_lock);
133 static DEFINE_SPINLOCK(dq_state_lock);
134 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 = NODQUOT, *empty = NODQUOT;
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 if ((dquot = find_dquot(hashent, sb, id, type)) == NODQUOT) {
740 if (empty == NODQUOT) {
741 spin_unlock(&dq_list_lock);
742 if ((empty = get_empty_dquot(sb, type)) == NODQUOT)
743 schedule(); /* Try to wait for a moment... */
749 /* all dquots go on the inuse_list */
751 /* hash it first so it can be found */
752 insert_dquot_hash(dquot);
754 spin_unlock(&dq_list_lock);
756 if (!atomic_read(&dquot->dq_count))
757 remove_free_dquot(dquot);
758 atomic_inc(&dquot->dq_count);
759 dqstats.cache_hits++;
761 spin_unlock(&dq_list_lock);
763 /* Wait for dq_lock - after this we know that either dquot_release() is already
764 * finished or it will be canceled due to dq_count > 1 test */
765 wait_on_dquot(dquot);
766 /* Read the dquot and instantiate it (everything done only if needed) */
767 if (!test_bit(DQ_ACTIVE_B, &dquot->dq_flags) && sb->dq_op->acquire_dquot(dquot) < 0) {
772 #ifdef __DQUOT_PARANOIA
773 BUG_ON(!dquot->dq_sb); /* Has somebody invalidated entry under us? */
777 do_destroy_dquot(empty);
781 EXPORT_SYMBOL(dqget);
783 static int dqinit_needed(struct inode *inode, int type)
787 if (IS_NOQUOTA(inode))
790 return inode->i_dquot[type] == NODQUOT;
791 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
792 if (inode->i_dquot[cnt] == NODQUOT)
797 /* This routine is guarded by dqonoff_mutex mutex */
798 static void add_dquot_ref(struct super_block *sb, int type)
800 struct inode *inode, *old_inode = NULL;
802 spin_lock(&inode_lock);
803 list_for_each_entry(inode, &sb->s_inodes, i_sb_list) {
804 if (!atomic_read(&inode->i_writecount))
806 if (!dqinit_needed(inode, type))
808 if (inode->i_state & (I_FREEING|I_WILL_FREE))
812 spin_unlock(&inode_lock);
815 sb->dq_op->initialize(inode, type);
816 /* We hold a reference to 'inode' so it couldn't have been
817 * removed from s_inodes list while we dropped the inode_lock.
818 * We cannot iput the inode now as we can be holding the last
819 * reference and we cannot iput it under inode_lock. So we
820 * keep the reference and iput it later. */
822 spin_lock(&inode_lock);
824 spin_unlock(&inode_lock);
828 /* Return 0 if dqput() won't block (note that 1 doesn't necessarily mean blocking) */
829 static inline int dqput_blocks(struct dquot *dquot)
831 if (atomic_read(&dquot->dq_count) <= 1)
836 /* Remove references to dquots from inode - add dquot to list for freeing if needed */
837 /* We can't race with anybody because we hold dqptr_sem for writing... */
838 static int remove_inode_dquot_ref(struct inode *inode, int type,
839 struct list_head *tofree_head)
841 struct dquot *dquot = inode->i_dquot[type];
843 inode->i_dquot[type] = NODQUOT;
844 if (dquot != NODQUOT) {
845 if (dqput_blocks(dquot)) {
846 #ifdef __DQUOT_PARANOIA
847 if (atomic_read(&dquot->dq_count) != 1)
848 printk(KERN_WARNING "VFS: Adding dquot with dq_count %d to dispose list.\n", atomic_read(&dquot->dq_count));
850 spin_lock(&dq_list_lock);
851 list_add(&dquot->dq_free, tofree_head); /* As dquot must have currently users it can't be on the free list... */
852 spin_unlock(&dq_list_lock);
856 dqput(dquot); /* We have guaranteed we won't block */
861 /* Free list of dquots - called from inode.c */
862 /* dquots are removed from inodes, no new references can be got so we are the only ones holding reference */
863 static void put_dquot_list(struct list_head *tofree_head)
865 struct list_head *act_head;
868 act_head = tofree_head->next;
869 /* So now we have dquots on the list... Just free them */
870 while (act_head != tofree_head) {
871 dquot = list_entry(act_head, struct dquot, dq_free);
872 act_head = act_head->next;
873 list_del_init(&dquot->dq_free); /* Remove dquot from the list so we won't have problems... */
878 static void remove_dquot_ref(struct super_block *sb, int type,
879 struct list_head *tofree_head)
883 spin_lock(&inode_lock);
884 list_for_each_entry(inode, &sb->s_inodes, i_sb_list) {
885 if (!IS_NOQUOTA(inode))
886 remove_inode_dquot_ref(inode, type, tofree_head);
888 spin_unlock(&inode_lock);
891 /* Gather all references from inodes and drop them */
892 static void drop_dquot_ref(struct super_block *sb, int type)
894 LIST_HEAD(tofree_head);
897 down_write(&sb_dqopt(sb)->dqptr_sem);
898 remove_dquot_ref(sb, type, &tofree_head);
899 up_write(&sb_dqopt(sb)->dqptr_sem);
900 put_dquot_list(&tofree_head);
904 static inline void dquot_incr_inodes(struct dquot *dquot, qsize_t number)
906 dquot->dq_dqb.dqb_curinodes += number;
909 static inline void dquot_incr_space(struct dquot *dquot, qsize_t number)
911 dquot->dq_dqb.dqb_curspace += number;
914 static inline void dquot_resv_space(struct dquot *dquot, qsize_t number)
916 dquot->dq_dqb.dqb_rsvspace += number;
920 * Claim reserved quota space
922 static void dquot_claim_reserved_space(struct dquot *dquot,
925 WARN_ON(dquot->dq_dqb.dqb_rsvspace < number);
926 dquot->dq_dqb.dqb_curspace += number;
927 dquot->dq_dqb.dqb_rsvspace -= number;
931 void dquot_free_reserved_space(struct dquot *dquot, qsize_t number)
933 dquot->dq_dqb.dqb_rsvspace -= number;
936 static inline void dquot_decr_inodes(struct dquot *dquot, qsize_t number)
938 if (sb_dqopt(dquot->dq_sb)->flags & DQUOT_NEGATIVE_USAGE ||
939 dquot->dq_dqb.dqb_curinodes >= number)
940 dquot->dq_dqb.dqb_curinodes -= number;
942 dquot->dq_dqb.dqb_curinodes = 0;
943 if (dquot->dq_dqb.dqb_curinodes <= dquot->dq_dqb.dqb_isoftlimit)
944 dquot->dq_dqb.dqb_itime = (time_t) 0;
945 clear_bit(DQ_INODES_B, &dquot->dq_flags);
948 static inline void dquot_decr_space(struct dquot *dquot, qsize_t number)
950 if (sb_dqopt(dquot->dq_sb)->flags & DQUOT_NEGATIVE_USAGE ||
951 dquot->dq_dqb.dqb_curspace >= number)
952 dquot->dq_dqb.dqb_curspace -= number;
954 dquot->dq_dqb.dqb_curspace = 0;
955 if (dquot->dq_dqb.dqb_curspace <= dquot->dq_dqb.dqb_bsoftlimit)
956 dquot->dq_dqb.dqb_btime = (time_t) 0;
957 clear_bit(DQ_BLKS_B, &dquot->dq_flags);
960 static int warning_issued(struct dquot *dquot, const int warntype)
962 int flag = (warntype == QUOTA_NL_BHARDWARN ||
963 warntype == QUOTA_NL_BSOFTLONGWARN) ? DQ_BLKS_B :
964 ((warntype == QUOTA_NL_IHARDWARN ||
965 warntype == QUOTA_NL_ISOFTLONGWARN) ? DQ_INODES_B : 0);
969 return test_and_set_bit(flag, &dquot->dq_flags);
972 #ifdef CONFIG_PRINT_QUOTA_WARNING
973 static int flag_print_warnings = 1;
975 static inline int need_print_warning(struct dquot *dquot)
977 if (!flag_print_warnings)
980 switch (dquot->dq_type) {
982 return current_fsuid() == dquot->dq_id;
984 return in_group_p(dquot->dq_id);
989 /* Print warning to user which exceeded quota */
990 static void print_warning(struct dquot *dquot, const int warntype)
993 struct tty_struct *tty;
995 if (warntype == QUOTA_NL_IHARDBELOW ||
996 warntype == QUOTA_NL_ISOFTBELOW ||
997 warntype == QUOTA_NL_BHARDBELOW ||
998 warntype == QUOTA_NL_BSOFTBELOW || !need_print_warning(dquot))
1001 tty = get_current_tty();
1004 tty_write_message(tty, dquot->dq_sb->s_id);
1005 if (warntype == QUOTA_NL_ISOFTWARN || warntype == QUOTA_NL_BSOFTWARN)
1006 tty_write_message(tty, ": warning, ");
1008 tty_write_message(tty, ": write failed, ");
1009 tty_write_message(tty, quotatypes[dquot->dq_type]);
1011 case QUOTA_NL_IHARDWARN:
1012 msg = " file limit reached.\r\n";
1014 case QUOTA_NL_ISOFTLONGWARN:
1015 msg = " file quota exceeded too long.\r\n";
1017 case QUOTA_NL_ISOFTWARN:
1018 msg = " file quota exceeded.\r\n";
1020 case QUOTA_NL_BHARDWARN:
1021 msg = " block limit reached.\r\n";
1023 case QUOTA_NL_BSOFTLONGWARN:
1024 msg = " block quota exceeded too long.\r\n";
1026 case QUOTA_NL_BSOFTWARN:
1027 msg = " block quota exceeded.\r\n";
1030 tty_write_message(tty, msg);
1035 #ifdef CONFIG_QUOTA_NETLINK_INTERFACE
1037 /* Netlink family structure for quota */
1038 static struct genl_family quota_genl_family = {
1039 .id = GENL_ID_GENERATE,
1041 .name = "VFS_DQUOT",
1043 .maxattr = QUOTA_NL_A_MAX,
1046 /* Send warning to userspace about user which exceeded quota */
1047 static void send_warning(const struct dquot *dquot, const char warntype)
1049 static atomic_t seq;
1050 struct sk_buff *skb;
1053 int msg_size = 4 * nla_total_size(sizeof(u32)) +
1054 2 * nla_total_size(sizeof(u64));
1056 /* We have to allocate using GFP_NOFS as we are called from a
1057 * filesystem performing write and thus further recursion into
1058 * the fs to free some data could cause deadlocks. */
1059 skb = genlmsg_new(msg_size, GFP_NOFS);
1062 "VFS: Not enough memory to send quota warning.\n");
1065 msg_head = genlmsg_put(skb, 0, atomic_add_return(1, &seq),
1066 "a_genl_family, 0, QUOTA_NL_C_WARNING);
1069 "VFS: Cannot store netlink header in quota warning.\n");
1072 ret = nla_put_u32(skb, QUOTA_NL_A_QTYPE, dquot->dq_type);
1075 ret = nla_put_u64(skb, QUOTA_NL_A_EXCESS_ID, dquot->dq_id);
1078 ret = nla_put_u32(skb, QUOTA_NL_A_WARNING, warntype);
1081 ret = nla_put_u32(skb, QUOTA_NL_A_DEV_MAJOR,
1082 MAJOR(dquot->dq_sb->s_dev));
1085 ret = nla_put_u32(skb, QUOTA_NL_A_DEV_MINOR,
1086 MINOR(dquot->dq_sb->s_dev));
1089 ret = nla_put_u64(skb, QUOTA_NL_A_CAUSED_ID, current_uid());
1092 genlmsg_end(skb, msg_head);
1094 ret = genlmsg_multicast(skb, 0, quota_genl_family.id, GFP_NOFS);
1095 if (ret < 0 && ret != -ESRCH)
1097 "VFS: Failed to send notification message: %d\n", ret);
1100 printk(KERN_ERR "VFS: Not enough space to compose quota message!\n");
1106 * Write warnings to the console and send warning messages over netlink.
1108 * Note that this function can sleep.
1110 static inline void flush_warnings(struct dquot * const *dquots, char *warntype)
1114 for (i = 0; i < MAXQUOTAS; i++)
1115 if (dquots[i] != NODQUOT && warntype[i] != QUOTA_NL_NOWARN &&
1116 !warning_issued(dquots[i], warntype[i])) {
1117 #ifdef CONFIG_PRINT_QUOTA_WARNING
1118 print_warning(dquots[i], warntype[i]);
1120 #ifdef CONFIG_QUOTA_NETLINK_INTERFACE
1121 send_warning(dquots[i], warntype[i]);
1126 static inline char ignore_hardlimit(struct dquot *dquot)
1128 struct mem_dqinfo *info = &sb_dqopt(dquot->dq_sb)->info[dquot->dq_type];
1130 return capable(CAP_SYS_RESOURCE) &&
1131 (info->dqi_format->qf_fmt_id != QFMT_VFS_OLD || !(info->dqi_flags & V1_DQF_RSQUASH));
1134 /* needs dq_data_lock */
1135 static int check_idq(struct dquot *dquot, qsize_t inodes, char *warntype)
1137 *warntype = QUOTA_NL_NOWARN;
1138 if (!sb_has_quota_limits_enabled(dquot->dq_sb, dquot->dq_type) ||
1139 test_bit(DQ_FAKE_B, &dquot->dq_flags))
1142 if (dquot->dq_dqb.dqb_ihardlimit &&
1143 (dquot->dq_dqb.dqb_curinodes + inodes) > dquot->dq_dqb.dqb_ihardlimit &&
1144 !ignore_hardlimit(dquot)) {
1145 *warntype = QUOTA_NL_IHARDWARN;
1149 if (dquot->dq_dqb.dqb_isoftlimit &&
1150 (dquot->dq_dqb.dqb_curinodes + inodes) > dquot->dq_dqb.dqb_isoftlimit &&
1151 dquot->dq_dqb.dqb_itime && get_seconds() >= dquot->dq_dqb.dqb_itime &&
1152 !ignore_hardlimit(dquot)) {
1153 *warntype = QUOTA_NL_ISOFTLONGWARN;
1157 if (dquot->dq_dqb.dqb_isoftlimit &&
1158 (dquot->dq_dqb.dqb_curinodes + inodes) > dquot->dq_dqb.dqb_isoftlimit &&
1159 dquot->dq_dqb.dqb_itime == 0) {
1160 *warntype = QUOTA_NL_ISOFTWARN;
1161 dquot->dq_dqb.dqb_itime = get_seconds() + sb_dqopt(dquot->dq_sb)->info[dquot->dq_type].dqi_igrace;
1167 /* needs dq_data_lock */
1168 static int check_bdq(struct dquot *dquot, qsize_t space, int prealloc, char *warntype)
1172 *warntype = QUOTA_NL_NOWARN;
1173 if (!sb_has_quota_limits_enabled(dquot->dq_sb, dquot->dq_type) ||
1174 test_bit(DQ_FAKE_B, &dquot->dq_flags))
1177 tspace = dquot->dq_dqb.dqb_curspace + dquot->dq_dqb.dqb_rsvspace
1180 if (dquot->dq_dqb.dqb_bhardlimit &&
1181 tspace > dquot->dq_dqb.dqb_bhardlimit &&
1182 !ignore_hardlimit(dquot)) {
1184 *warntype = QUOTA_NL_BHARDWARN;
1188 if (dquot->dq_dqb.dqb_bsoftlimit &&
1189 tspace > dquot->dq_dqb.dqb_bsoftlimit &&
1190 dquot->dq_dqb.dqb_btime && get_seconds() >= dquot->dq_dqb.dqb_btime &&
1191 !ignore_hardlimit(dquot)) {
1193 *warntype = QUOTA_NL_BSOFTLONGWARN;
1197 if (dquot->dq_dqb.dqb_bsoftlimit &&
1198 tspace > dquot->dq_dqb.dqb_bsoftlimit &&
1199 dquot->dq_dqb.dqb_btime == 0) {
1201 *warntype = QUOTA_NL_BSOFTWARN;
1202 dquot->dq_dqb.dqb_btime = get_seconds() + sb_dqopt(dquot->dq_sb)->info[dquot->dq_type].dqi_bgrace;
1206 * We don't allow preallocation to exceed softlimit so exceeding will
1215 static int info_idq_free(struct dquot *dquot, qsize_t inodes)
1217 if (test_bit(DQ_FAKE_B, &dquot->dq_flags) ||
1218 dquot->dq_dqb.dqb_curinodes <= dquot->dq_dqb.dqb_isoftlimit ||
1219 !sb_has_quota_limits_enabled(dquot->dq_sb, dquot->dq_type))
1220 return QUOTA_NL_NOWARN;
1222 if (dquot->dq_dqb.dqb_curinodes - inodes <= dquot->dq_dqb.dqb_isoftlimit)
1223 return QUOTA_NL_ISOFTBELOW;
1224 if (dquot->dq_dqb.dqb_curinodes >= dquot->dq_dqb.dqb_ihardlimit &&
1225 dquot->dq_dqb.dqb_curinodes - inodes < dquot->dq_dqb.dqb_ihardlimit)
1226 return QUOTA_NL_IHARDBELOW;
1227 return QUOTA_NL_NOWARN;
1230 static int info_bdq_free(struct dquot *dquot, qsize_t space)
1232 if (test_bit(DQ_FAKE_B, &dquot->dq_flags) ||
1233 dquot->dq_dqb.dqb_curspace <= dquot->dq_dqb.dqb_bsoftlimit)
1234 return QUOTA_NL_NOWARN;
1236 if (dquot->dq_dqb.dqb_curspace - space <= dquot->dq_dqb.dqb_bsoftlimit)
1237 return QUOTA_NL_BSOFTBELOW;
1238 if (dquot->dq_dqb.dqb_curspace >= dquot->dq_dqb.dqb_bhardlimit &&
1239 dquot->dq_dqb.dqb_curspace - space < dquot->dq_dqb.dqb_bhardlimit)
1240 return QUOTA_NL_BHARDBELOW;
1241 return QUOTA_NL_NOWARN;
1244 * Initialize quota pointers in inode
1245 * We do things in a bit complicated way but by that we avoid calling
1246 * dqget() and thus filesystem callbacks under dqptr_sem.
1248 int dquot_initialize(struct inode *inode, int type)
1250 unsigned int id = 0;
1252 struct dquot *got[MAXQUOTAS] = { NODQUOT, NODQUOT };
1253 struct super_block *sb = inode->i_sb;
1255 /* First test before acquiring mutex - solves deadlocks when we
1256 * re-enter the quota code and are already holding the mutex */
1257 if (IS_NOQUOTA(inode))
1260 /* First get references to structures we might need. */
1261 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1262 if (type != -1 && cnt != type)
1272 got[cnt] = dqget(sb, id, cnt);
1275 down_write(&sb_dqopt(sb)->dqptr_sem);
1276 /* Having dqptr_sem we know NOQUOTA flags can't be altered... */
1277 if (IS_NOQUOTA(inode))
1279 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1280 if (type != -1 && cnt != type)
1282 /* Avoid races with quotaoff() */
1283 if (!sb_has_quota_active(sb, cnt))
1285 if (inode->i_dquot[cnt] == NODQUOT) {
1286 inode->i_dquot[cnt] = got[cnt];
1291 up_write(&sb_dqopt(sb)->dqptr_sem);
1292 /* Drop unused references */
1293 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1297 EXPORT_SYMBOL(dquot_initialize);
1300 * Release all quotas referenced by inode
1302 int dquot_drop(struct inode *inode)
1305 struct dquot *put[MAXQUOTAS];
1307 down_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
1308 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1309 put[cnt] = inode->i_dquot[cnt];
1310 inode->i_dquot[cnt] = NODQUOT;
1312 up_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
1314 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1318 EXPORT_SYMBOL(dquot_drop);
1320 /* Wrapper to remove references to quota structures from inode */
1321 void vfs_dq_drop(struct inode *inode)
1323 /* Here we can get arbitrary inode from clear_inode() so we have
1324 * to be careful. OTOH we don't need locking as quota operations
1325 * are allowed to change only at mount time */
1326 if (!IS_NOQUOTA(inode) && inode->i_sb && inode->i_sb->dq_op
1327 && inode->i_sb->dq_op->drop) {
1329 /* Test before calling to rule out calls from proc and such
1330 * where we are not allowed to block. Note that this is
1331 * actually reliable test even without the lock - the caller
1332 * must assure that nobody can come after the DQUOT_DROP and
1333 * add quota pointers back anyway */
1334 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1335 if (inode->i_dquot[cnt] != NODQUOT)
1337 if (cnt < MAXQUOTAS)
1338 inode->i_sb->dq_op->drop(inode);
1341 EXPORT_SYMBOL(vfs_dq_drop);
1344 * Following four functions update i_blocks+i_bytes fields and
1345 * quota information (together with appropriate checks)
1346 * NOTE: We absolutely rely on the fact that caller dirties
1347 * the inode (usually macros in quotaops.h care about this) and
1348 * holds a handle for the current transaction so that dquot write and
1349 * inode write go into the same transaction.
1353 * This operation can block, but only after everything is updated
1355 int __dquot_alloc_space(struct inode *inode, qsize_t number,
1356 int warn, int reserve)
1358 int cnt, ret = QUOTA_OK;
1359 char warntype[MAXQUOTAS];
1361 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1362 warntype[cnt] = QUOTA_NL_NOWARN;
1364 spin_lock(&dq_data_lock);
1365 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1366 if (inode->i_dquot[cnt] == NODQUOT)
1368 if (check_bdq(inode->i_dquot[cnt], number, warn, warntype+cnt)
1374 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1375 if (inode->i_dquot[cnt] == NODQUOT)
1378 dquot_resv_space(inode->i_dquot[cnt], number);
1380 dquot_incr_space(inode->i_dquot[cnt], number);
1383 inode_add_bytes(inode, number);
1385 spin_unlock(&dq_data_lock);
1386 flush_warnings(inode->i_dquot, warntype);
1390 int dquot_alloc_space(struct inode *inode, qsize_t number, int warn)
1392 int cnt, ret = QUOTA_OK;
1395 * First test before acquiring mutex - solves deadlocks when we
1396 * re-enter the quota code and are already holding the mutex
1398 if (IS_NOQUOTA(inode)) {
1399 inode_add_bytes(inode, number);
1403 down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1404 if (IS_NOQUOTA(inode)) {
1405 inode_add_bytes(inode, number);
1409 ret = __dquot_alloc_space(inode, number, warn, 0);
1410 if (ret == NO_QUOTA)
1413 /* Dirtify all the dquots - this can block when journalling */
1414 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1415 if (inode->i_dquot[cnt])
1416 mark_dquot_dirty(inode->i_dquot[cnt]);
1418 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1422 EXPORT_SYMBOL(dquot_alloc_space);
1424 int dquot_reserve_space(struct inode *inode, qsize_t number, int warn)
1428 if (IS_NOQUOTA(inode))
1431 down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1432 if (IS_NOQUOTA(inode))
1435 ret = __dquot_alloc_space(inode, number, warn, 1);
1437 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1441 EXPORT_SYMBOL(dquot_reserve_space);
1444 * This operation can block, but only after everything is updated
1446 int dquot_alloc_inode(const struct inode *inode, qsize_t number)
1448 int cnt, ret = NO_QUOTA;
1449 char warntype[MAXQUOTAS];
1451 /* First test before acquiring mutex - solves deadlocks when we
1452 * re-enter the quota code and are already holding the mutex */
1453 if (IS_NOQUOTA(inode))
1455 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1456 warntype[cnt] = QUOTA_NL_NOWARN;
1457 down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1458 if (IS_NOQUOTA(inode)) {
1459 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1462 spin_lock(&dq_data_lock);
1463 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1464 if (inode->i_dquot[cnt] == NODQUOT)
1466 if (check_idq(inode->i_dquot[cnt], number, warntype+cnt) == NO_QUOTA)
1470 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1471 if (inode->i_dquot[cnt] == NODQUOT)
1473 dquot_incr_inodes(inode->i_dquot[cnt], number);
1477 spin_unlock(&dq_data_lock);
1478 if (ret == QUOTA_OK)
1479 /* Dirtify all the dquots - this can block when journalling */
1480 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1481 if (inode->i_dquot[cnt])
1482 mark_dquot_dirty(inode->i_dquot[cnt]);
1483 flush_warnings(inode->i_dquot, warntype);
1484 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1487 EXPORT_SYMBOL(dquot_alloc_inode);
1489 int dquot_claim_space(struct inode *inode, qsize_t number)
1494 if (IS_NOQUOTA(inode)) {
1495 inode_add_bytes(inode, number);
1499 down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1500 if (IS_NOQUOTA(inode)) {
1501 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1502 inode_add_bytes(inode, number);
1506 spin_lock(&dq_data_lock);
1507 /* Claim reserved quotas to allocated quotas */
1508 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1509 if (inode->i_dquot[cnt] != NODQUOT)
1510 dquot_claim_reserved_space(inode->i_dquot[cnt],
1513 /* Update inode bytes */
1514 inode_add_bytes(inode, number);
1515 spin_unlock(&dq_data_lock);
1516 /* Dirtify all the dquots - this can block when journalling */
1517 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1518 if (inode->i_dquot[cnt])
1519 mark_dquot_dirty(inode->i_dquot[cnt]);
1520 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1524 EXPORT_SYMBOL(dquot_claim_space);
1527 * Release reserved quota space
1529 void dquot_release_reserved_space(struct inode *inode, qsize_t number)
1533 if (IS_NOQUOTA(inode))
1536 down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1537 if (IS_NOQUOTA(inode))
1540 spin_lock(&dq_data_lock);
1541 /* Release reserved dquots */
1542 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1543 if (inode->i_dquot[cnt] != NODQUOT)
1544 dquot_free_reserved_space(inode->i_dquot[cnt], number);
1546 spin_unlock(&dq_data_lock);
1549 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1553 EXPORT_SYMBOL(dquot_release_reserved_space);
1556 * This operation can block, but only after everything is updated
1558 int dquot_free_space(struct inode *inode, qsize_t number)
1561 char warntype[MAXQUOTAS];
1563 /* First test before acquiring mutex - solves deadlocks when we
1564 * re-enter the quota code and are already holding the mutex */
1565 if (IS_NOQUOTA(inode)) {
1567 inode_sub_bytes(inode, number);
1571 down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1572 /* Now recheck reliably when holding dqptr_sem */
1573 if (IS_NOQUOTA(inode)) {
1574 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1577 spin_lock(&dq_data_lock);
1578 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1579 if (inode->i_dquot[cnt] == NODQUOT)
1581 warntype[cnt] = info_bdq_free(inode->i_dquot[cnt], number);
1582 dquot_decr_space(inode->i_dquot[cnt], number);
1584 inode_sub_bytes(inode, number);
1585 spin_unlock(&dq_data_lock);
1586 /* Dirtify all the dquots - this can block when journalling */
1587 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1588 if (inode->i_dquot[cnt])
1589 mark_dquot_dirty(inode->i_dquot[cnt]);
1590 flush_warnings(inode->i_dquot, warntype);
1591 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1594 EXPORT_SYMBOL(dquot_free_space);
1597 * This operation can block, but only after everything is updated
1599 int dquot_free_inode(const struct inode *inode, qsize_t number)
1602 char warntype[MAXQUOTAS];
1604 /* First test before acquiring mutex - solves deadlocks when we
1605 * re-enter the quota code and are already holding the mutex */
1606 if (IS_NOQUOTA(inode))
1609 down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1610 /* Now recheck reliably when holding dqptr_sem */
1611 if (IS_NOQUOTA(inode)) {
1612 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1615 spin_lock(&dq_data_lock);
1616 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1617 if (inode->i_dquot[cnt] == NODQUOT)
1619 warntype[cnt] = info_idq_free(inode->i_dquot[cnt], number);
1620 dquot_decr_inodes(inode->i_dquot[cnt], number);
1622 spin_unlock(&dq_data_lock);
1623 /* Dirtify all the dquots - this can block when journalling */
1624 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1625 if (inode->i_dquot[cnt])
1626 mark_dquot_dirty(inode->i_dquot[cnt]);
1627 flush_warnings(inode->i_dquot, warntype);
1628 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1631 EXPORT_SYMBOL(dquot_free_inode);
1634 * call back function, get reserved quota space from underlying fs
1636 qsize_t dquot_get_reserved_space(struct inode *inode)
1638 qsize_t reserved_space = 0;
1640 if (sb_any_quota_active(inode->i_sb) &&
1641 inode->i_sb->dq_op->get_reserved_space)
1642 reserved_space = inode->i_sb->dq_op->get_reserved_space(inode);
1643 return reserved_space;
1647 * Transfer the number of inode and blocks from one diskquota to an other.
1649 * This operation can block, but only after everything is updated
1650 * A transaction must be started when entering this function.
1652 int dquot_transfer(struct inode *inode, struct iattr *iattr)
1654 qsize_t space, cur_space;
1655 qsize_t rsv_space = 0;
1656 struct dquot *transfer_from[MAXQUOTAS];
1657 struct dquot *transfer_to[MAXQUOTAS];
1658 int cnt, ret = QUOTA_OK;
1659 int chuid = iattr->ia_valid & ATTR_UID && inode->i_uid != iattr->ia_uid,
1660 chgid = iattr->ia_valid & ATTR_GID && inode->i_gid != iattr->ia_gid;
1661 char warntype_to[MAXQUOTAS];
1662 char warntype_from_inodes[MAXQUOTAS], warntype_from_space[MAXQUOTAS];
1664 /* First test before acquiring mutex - solves deadlocks when we
1665 * re-enter the quota code and are already holding the mutex */
1666 if (IS_NOQUOTA(inode))
1668 /* Initialize the arrays */
1669 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1670 transfer_from[cnt] = NODQUOT;
1671 transfer_to[cnt] = NODQUOT;
1672 warntype_to[cnt] = QUOTA_NL_NOWARN;
1677 transfer_to[cnt] = dqget(inode->i_sb, iattr->ia_uid, cnt);
1682 transfer_to[cnt] = dqget(inode->i_sb, iattr->ia_gid, cnt);
1687 down_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
1688 /* Now recheck reliably when holding dqptr_sem */
1689 if (IS_NOQUOTA(inode)) { /* File without quota accounting? */
1690 up_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
1693 spin_lock(&dq_data_lock);
1694 cur_space = inode_get_bytes(inode);
1695 rsv_space = dquot_get_reserved_space(inode);
1696 space = cur_space + rsv_space;
1697 /* Build the transfer_from list and check the limits */
1698 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1699 if (transfer_to[cnt] == NODQUOT)
1701 transfer_from[cnt] = inode->i_dquot[cnt];
1702 if (check_idq(transfer_to[cnt], 1, warntype_to + cnt) ==
1703 NO_QUOTA || check_bdq(transfer_to[cnt], space, 0,
1704 warntype_to + cnt) == NO_QUOTA)
1709 * Finally perform the needed transfer from transfer_from to transfer_to
1711 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1713 * Skip changes for same uid or gid or for turned off quota-type.
1715 if (transfer_to[cnt] == NODQUOT)
1718 /* Due to IO error we might not have transfer_from[] structure */
1719 if (transfer_from[cnt]) {
1720 warntype_from_inodes[cnt] =
1721 info_idq_free(transfer_from[cnt], 1);
1722 warntype_from_space[cnt] =
1723 info_bdq_free(transfer_from[cnt], space);
1724 dquot_decr_inodes(transfer_from[cnt], 1);
1725 dquot_decr_space(transfer_from[cnt], cur_space);
1726 dquot_free_reserved_space(transfer_from[cnt],
1730 dquot_incr_inodes(transfer_to[cnt], 1);
1731 dquot_incr_space(transfer_to[cnt], cur_space);
1732 dquot_resv_space(transfer_to[cnt], rsv_space);
1734 inode->i_dquot[cnt] = transfer_to[cnt];
1736 spin_unlock(&dq_data_lock);
1737 up_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
1739 /* Dirtify all the dquots - this can block when journalling */
1740 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1741 if (transfer_from[cnt])
1742 mark_dquot_dirty(transfer_from[cnt]);
1743 if (transfer_to[cnt]) {
1744 mark_dquot_dirty(transfer_to[cnt]);
1745 /* The reference we got is transferred to the inode */
1746 transfer_to[cnt] = NODQUOT;
1750 flush_warnings(transfer_to, warntype_to);
1751 flush_warnings(transfer_from, warntype_from_inodes);
1752 flush_warnings(transfer_from, warntype_from_space);
1754 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1755 dqput(transfer_from[cnt]);
1756 dqput(transfer_to[cnt]);
1760 spin_unlock(&dq_data_lock);
1761 up_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
1762 /* Clear dquot pointers we don't want to dqput() */
1763 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1764 transfer_from[cnt] = NODQUOT;
1768 EXPORT_SYMBOL(dquot_transfer);
1770 /* Wrapper for transferring ownership of an inode */
1771 int vfs_dq_transfer(struct inode *inode, struct iattr *iattr)
1773 if (sb_any_quota_active(inode->i_sb) && !IS_NOQUOTA(inode)) {
1775 if (inode->i_sb->dq_op->transfer(inode, iattr) == NO_QUOTA)
1780 EXPORT_SYMBOL(vfs_dq_transfer);
1783 * Write info of quota file to disk
1785 int dquot_commit_info(struct super_block *sb, int type)
1788 struct quota_info *dqopt = sb_dqopt(sb);
1790 mutex_lock(&dqopt->dqio_mutex);
1791 ret = dqopt->ops[type]->write_file_info(sb, type);
1792 mutex_unlock(&dqopt->dqio_mutex);
1795 EXPORT_SYMBOL(dquot_commit_info);
1798 * Definitions of diskquota operations.
1800 struct dquot_operations dquot_operations = {
1801 .initialize = dquot_initialize,
1803 .alloc_space = dquot_alloc_space,
1804 .alloc_inode = dquot_alloc_inode,
1805 .free_space = dquot_free_space,
1806 .free_inode = dquot_free_inode,
1807 .transfer = dquot_transfer,
1808 .write_dquot = dquot_commit,
1809 .acquire_dquot = dquot_acquire,
1810 .release_dquot = dquot_release,
1811 .mark_dirty = dquot_mark_dquot_dirty,
1812 .write_info = dquot_commit_info,
1813 .alloc_dquot = dquot_alloc,
1814 .destroy_dquot = dquot_destroy,
1818 * Turn quota off on a device. type == -1 ==> quotaoff for all types (umount)
1820 int vfs_quota_disable(struct super_block *sb, int type, unsigned int flags)
1823 struct quota_info *dqopt = sb_dqopt(sb);
1824 struct inode *toputinode[MAXQUOTAS];
1826 /* Cannot turn off usage accounting without turning off limits, or
1827 * suspend quotas and simultaneously turn quotas off. */
1828 if ((flags & DQUOT_USAGE_ENABLED && !(flags & DQUOT_LIMITS_ENABLED))
1829 || (flags & DQUOT_SUSPENDED && flags & (DQUOT_LIMITS_ENABLED |
1830 DQUOT_USAGE_ENABLED)))
1833 /* We need to serialize quota_off() for device */
1834 mutex_lock(&dqopt->dqonoff_mutex);
1837 * Skip everything if there's nothing to do. We have to do this because
1838 * sometimes we are called when fill_super() failed and calling
1839 * sync_fs() in such cases does no good.
1841 if (!sb_any_quota_loaded(sb)) {
1842 mutex_unlock(&dqopt->dqonoff_mutex);
1845 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1846 toputinode[cnt] = NULL;
1847 if (type != -1 && cnt != type)
1849 if (!sb_has_quota_loaded(sb, cnt))
1852 if (flags & DQUOT_SUSPENDED) {
1853 spin_lock(&dq_state_lock);
1855 dquot_state_flag(DQUOT_SUSPENDED, cnt);
1856 spin_unlock(&dq_state_lock);
1858 spin_lock(&dq_state_lock);
1859 dqopt->flags &= ~dquot_state_flag(flags, cnt);
1860 /* Turning off suspended quotas? */
1861 if (!sb_has_quota_loaded(sb, cnt) &&
1862 sb_has_quota_suspended(sb, cnt)) {
1863 dqopt->flags &= ~dquot_state_flag(
1864 DQUOT_SUSPENDED, cnt);
1865 spin_unlock(&dq_state_lock);
1866 iput(dqopt->files[cnt]);
1867 dqopt->files[cnt] = NULL;
1870 spin_unlock(&dq_state_lock);
1873 /* We still have to keep quota loaded? */
1874 if (sb_has_quota_loaded(sb, cnt) && !(flags & DQUOT_SUSPENDED))
1877 /* Note: these are blocking operations */
1878 drop_dquot_ref(sb, cnt);
1879 invalidate_dquots(sb, cnt);
1881 * Now all dquots should be invalidated, all writes done so we should be only
1882 * users of the info. No locks needed.
1884 if (info_dirty(&dqopt->info[cnt]))
1885 sb->dq_op->write_info(sb, cnt);
1886 if (dqopt->ops[cnt]->free_file_info)
1887 dqopt->ops[cnt]->free_file_info(sb, cnt);
1888 put_quota_format(dqopt->info[cnt].dqi_format);
1890 toputinode[cnt] = dqopt->files[cnt];
1891 if (!sb_has_quota_loaded(sb, cnt))
1892 dqopt->files[cnt] = NULL;
1893 dqopt->info[cnt].dqi_flags = 0;
1894 dqopt->info[cnt].dqi_igrace = 0;
1895 dqopt->info[cnt].dqi_bgrace = 0;
1896 dqopt->ops[cnt] = NULL;
1898 mutex_unlock(&dqopt->dqonoff_mutex);
1900 /* Skip syncing and setting flags if quota files are hidden */
1901 if (dqopt->flags & DQUOT_QUOTA_SYS_FILE)
1904 /* Sync the superblock so that buffers with quota data are written to
1905 * disk (and so userspace sees correct data afterwards). */
1906 if (sb->s_op->sync_fs)
1907 sb->s_op->sync_fs(sb, 1);
1908 sync_blockdev(sb->s_bdev);
1909 /* Now the quota files are just ordinary files and we can set the
1910 * inode flags back. Moreover we discard the pagecache so that
1911 * userspace sees the writes we did bypassing the pagecache. We
1912 * must also discard the blockdev buffers so that we see the
1913 * changes done by userspace on the next quotaon() */
1914 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1915 if (toputinode[cnt]) {
1916 mutex_lock(&dqopt->dqonoff_mutex);
1917 /* If quota was reenabled in the meantime, we have
1919 if (!sb_has_quota_loaded(sb, cnt)) {
1920 mutex_lock_nested(&toputinode[cnt]->i_mutex, I_MUTEX_QUOTA);
1921 toputinode[cnt]->i_flags &= ~(S_IMMUTABLE |
1922 S_NOATIME | S_NOQUOTA);
1923 truncate_inode_pages(&toputinode[cnt]->i_data, 0);
1924 mutex_unlock(&toputinode[cnt]->i_mutex);
1925 mark_inode_dirty(toputinode[cnt]);
1927 mutex_unlock(&dqopt->dqonoff_mutex);
1930 invalidate_bdev(sb->s_bdev);
1932 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1933 if (toputinode[cnt]) {
1934 /* On remount RO, we keep the inode pointer so that we
1935 * can reenable quota on the subsequent remount RW. We
1936 * have to check 'flags' variable and not use sb_has_
1937 * function because another quotaon / quotaoff could
1938 * change global state before we got here. We refuse
1939 * to suspend quotas when there is pending delete on
1940 * the quota file... */
1941 if (!(flags & DQUOT_SUSPENDED))
1942 iput(toputinode[cnt]);
1943 else if (!toputinode[cnt]->i_nlink)
1948 EXPORT_SYMBOL(vfs_quota_disable);
1950 int vfs_quota_off(struct super_block *sb, int type, int remount)
1952 return vfs_quota_disable(sb, type, remount ? DQUOT_SUSPENDED :
1953 (DQUOT_USAGE_ENABLED | DQUOT_LIMITS_ENABLED));
1955 EXPORT_SYMBOL(vfs_quota_off);
1957 * Turn quotas on on a device
1961 * Helper function to turn quotas on when we already have the inode of
1962 * quota file and no quota information is loaded.
1964 static int vfs_load_quota_inode(struct inode *inode, int type, int format_id,
1967 struct quota_format_type *fmt = find_quota_format(format_id);
1968 struct super_block *sb = inode->i_sb;
1969 struct quota_info *dqopt = sb_dqopt(sb);
1975 if (!S_ISREG(inode->i_mode)) {
1979 if (IS_RDONLY(inode)) {
1983 if (!sb->s_op->quota_write || !sb->s_op->quota_read) {
1987 /* Usage always has to be set... */
1988 if (!(flags & DQUOT_USAGE_ENABLED)) {
1993 if (!(dqopt->flags & DQUOT_QUOTA_SYS_FILE)) {
1994 /* As we bypass the pagecache we must now flush the inode so
1995 * that we see all the changes from userspace... */
1996 write_inode_now(inode, 1);
1997 /* And now flush the block cache so that kernel sees the
1999 invalidate_bdev(sb->s_bdev);
2001 mutex_lock(&inode->i_mutex);
2002 mutex_lock(&dqopt->dqonoff_mutex);
2003 if (sb_has_quota_loaded(sb, type)) {
2008 if (!(dqopt->flags & DQUOT_QUOTA_SYS_FILE)) {
2009 /* We don't want quota and atime on quota files (deadlocks
2010 * possible) Also nobody should write to the file - we use
2011 * special IO operations which ignore the immutable bit. */
2012 down_write(&dqopt->dqptr_sem);
2013 oldflags = inode->i_flags & (S_NOATIME | S_IMMUTABLE | S_NOQUOTA);
2014 inode->i_flags |= S_NOQUOTA | S_NOATIME | S_IMMUTABLE;
2015 up_write(&dqopt->dqptr_sem);
2016 sb->dq_op->drop(inode);
2020 dqopt->files[type] = igrab(inode);
2021 if (!dqopt->files[type])
2024 if (!fmt->qf_ops->check_quota_file(sb, type))
2027 dqopt->ops[type] = fmt->qf_ops;
2028 dqopt->info[type].dqi_format = fmt;
2029 dqopt->info[type].dqi_fmt_id = format_id;
2030 INIT_LIST_HEAD(&dqopt->info[type].dqi_dirty_list);
2031 mutex_lock(&dqopt->dqio_mutex);
2032 if ((error = dqopt->ops[type]->read_file_info(sb, type)) < 0) {
2033 mutex_unlock(&dqopt->dqio_mutex);
2036 mutex_unlock(&dqopt->dqio_mutex);
2037 mutex_unlock(&inode->i_mutex);
2038 spin_lock(&dq_state_lock);
2039 dqopt->flags |= dquot_state_flag(flags, type);
2040 spin_unlock(&dq_state_lock);
2042 add_dquot_ref(sb, type);
2043 mutex_unlock(&dqopt->dqonoff_mutex);
2048 dqopt->files[type] = NULL;
2051 mutex_unlock(&dqopt->dqonoff_mutex);
2052 if (oldflags != -1) {
2053 down_write(&dqopt->dqptr_sem);
2054 /* Set the flags back (in the case of accidental quotaon()
2055 * on a wrong file we don't want to mess up the flags) */
2056 inode->i_flags &= ~(S_NOATIME | S_NOQUOTA | S_IMMUTABLE);
2057 inode->i_flags |= oldflags;
2058 up_write(&dqopt->dqptr_sem);
2060 mutex_unlock(&inode->i_mutex);
2062 put_quota_format(fmt);
2067 /* Reenable quotas on remount RW */
2068 static int vfs_quota_on_remount(struct super_block *sb, int type)
2070 struct quota_info *dqopt = sb_dqopt(sb);
2071 struct inode *inode;
2075 mutex_lock(&dqopt->dqonoff_mutex);
2076 if (!sb_has_quota_suspended(sb, type)) {
2077 mutex_unlock(&dqopt->dqonoff_mutex);
2080 inode = dqopt->files[type];
2081 dqopt->files[type] = NULL;
2082 spin_lock(&dq_state_lock);
2083 flags = dqopt->flags & dquot_state_flag(DQUOT_USAGE_ENABLED |
2084 DQUOT_LIMITS_ENABLED, type);
2085 dqopt->flags &= ~dquot_state_flag(DQUOT_STATE_FLAGS, type);
2086 spin_unlock(&dq_state_lock);
2087 mutex_unlock(&dqopt->dqonoff_mutex);
2089 flags = dquot_generic_flag(flags, type);
2090 ret = vfs_load_quota_inode(inode, type, dqopt->info[type].dqi_fmt_id,
2097 int vfs_quota_on_path(struct super_block *sb, int type, int format_id,
2100 int error = security_quota_on(path->dentry);
2103 /* Quota file not on the same filesystem? */
2104 if (path->mnt->mnt_sb != sb)
2107 error = vfs_load_quota_inode(path->dentry->d_inode, type,
2108 format_id, DQUOT_USAGE_ENABLED |
2109 DQUOT_LIMITS_ENABLED);
2112 EXPORT_SYMBOL(vfs_quota_on_path);
2114 int vfs_quota_on(struct super_block *sb, int type, int format_id, char *name,
2121 return vfs_quota_on_remount(sb, type);
2123 error = kern_path(name, LOOKUP_FOLLOW, &path);
2125 error = vfs_quota_on_path(sb, type, format_id, &path);
2130 EXPORT_SYMBOL(vfs_quota_on);
2133 * More powerful function for turning on quotas allowing setting
2134 * of individual quota flags
2136 int vfs_quota_enable(struct inode *inode, int type, int format_id,
2140 struct super_block *sb = inode->i_sb;
2141 struct quota_info *dqopt = sb_dqopt(sb);
2143 /* Just unsuspend quotas? */
2144 if (flags & DQUOT_SUSPENDED)
2145 return vfs_quota_on_remount(sb, type);
2148 /* Just updating flags needed? */
2149 if (sb_has_quota_loaded(sb, type)) {
2150 mutex_lock(&dqopt->dqonoff_mutex);
2151 /* Now do a reliable test... */
2152 if (!sb_has_quota_loaded(sb, type)) {
2153 mutex_unlock(&dqopt->dqonoff_mutex);
2156 if (flags & DQUOT_USAGE_ENABLED &&
2157 sb_has_quota_usage_enabled(sb, type)) {
2161 if (flags & DQUOT_LIMITS_ENABLED &&
2162 sb_has_quota_limits_enabled(sb, type)) {
2166 spin_lock(&dq_state_lock);
2167 sb_dqopt(sb)->flags |= dquot_state_flag(flags, type);
2168 spin_unlock(&dq_state_lock);
2170 mutex_unlock(&dqopt->dqonoff_mutex);
2175 return vfs_load_quota_inode(inode, type, format_id, flags);
2177 EXPORT_SYMBOL(vfs_quota_enable);
2180 * This function is used when filesystem needs to initialize quotas
2181 * during mount time.
2183 int vfs_quota_on_mount(struct super_block *sb, char *qf_name,
2184 int format_id, int type)
2186 struct dentry *dentry;
2189 dentry = lookup_one_len(qf_name, sb->s_root, strlen(qf_name));
2191 return PTR_ERR(dentry);
2193 if (!dentry->d_inode) {
2198 error = security_quota_on(dentry);
2200 error = vfs_load_quota_inode(dentry->d_inode, type, format_id,
2201 DQUOT_USAGE_ENABLED | DQUOT_LIMITS_ENABLED);
2207 EXPORT_SYMBOL(vfs_quota_on_mount);
2209 /* Wrapper to turn on quotas when remounting rw */
2210 int vfs_dq_quota_on_remount(struct super_block *sb)
2215 if (!sb->s_qcop || !sb->s_qcop->quota_on)
2217 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
2218 err = sb->s_qcop->quota_on(sb, cnt, 0, NULL, 1);
2219 if (err < 0 && !ret)
2224 EXPORT_SYMBOL(vfs_dq_quota_on_remount);
2226 static inline qsize_t qbtos(qsize_t blocks)
2228 return blocks << QIF_DQBLKSIZE_BITS;
2231 static inline qsize_t stoqb(qsize_t space)
2233 return (space + QIF_DQBLKSIZE - 1) >> QIF_DQBLKSIZE_BITS;
2236 /* Generic routine for getting common part of quota structure */
2237 static void do_get_dqblk(struct dquot *dquot, struct if_dqblk *di)
2239 struct mem_dqblk *dm = &dquot->dq_dqb;
2241 spin_lock(&dq_data_lock);
2242 di->dqb_bhardlimit = stoqb(dm->dqb_bhardlimit);
2243 di->dqb_bsoftlimit = stoqb(dm->dqb_bsoftlimit);
2244 di->dqb_curspace = dm->dqb_curspace + dm->dqb_rsvspace;
2245 di->dqb_ihardlimit = dm->dqb_ihardlimit;
2246 di->dqb_isoftlimit = dm->dqb_isoftlimit;
2247 di->dqb_curinodes = dm->dqb_curinodes;
2248 di->dqb_btime = dm->dqb_btime;
2249 di->dqb_itime = dm->dqb_itime;
2250 di->dqb_valid = QIF_ALL;
2251 spin_unlock(&dq_data_lock);
2254 int vfs_get_dqblk(struct super_block *sb, int type, qid_t id, struct if_dqblk *di)
2256 struct dquot *dquot;
2258 dquot = dqget(sb, id, type);
2259 if (dquot == NODQUOT)
2261 do_get_dqblk(dquot, di);
2266 EXPORT_SYMBOL(vfs_get_dqblk);
2268 /* Generic routine for setting common part of quota structure */
2269 static int do_set_dqblk(struct dquot *dquot, struct if_dqblk *di)
2271 struct mem_dqblk *dm = &dquot->dq_dqb;
2272 int check_blim = 0, check_ilim = 0;
2273 struct mem_dqinfo *dqi = &sb_dqopt(dquot->dq_sb)->info[dquot->dq_type];
2275 if ((di->dqb_valid & QIF_BLIMITS &&
2276 (di->dqb_bhardlimit > dqi->dqi_maxblimit ||
2277 di->dqb_bsoftlimit > dqi->dqi_maxblimit)) ||
2278 (di->dqb_valid & QIF_ILIMITS &&
2279 (di->dqb_ihardlimit > dqi->dqi_maxilimit ||
2280 di->dqb_isoftlimit > dqi->dqi_maxilimit)))
2283 spin_lock(&dq_data_lock);
2284 if (di->dqb_valid & QIF_SPACE) {
2285 dm->dqb_curspace = di->dqb_curspace - dm->dqb_rsvspace;
2287 __set_bit(DQ_LASTSET_B + QIF_SPACE_B, &dquot->dq_flags);
2289 if (di->dqb_valid & QIF_BLIMITS) {
2290 dm->dqb_bsoftlimit = qbtos(di->dqb_bsoftlimit);
2291 dm->dqb_bhardlimit = qbtos(di->dqb_bhardlimit);
2293 __set_bit(DQ_LASTSET_B + QIF_BLIMITS_B, &dquot->dq_flags);
2295 if (di->dqb_valid & QIF_INODES) {
2296 dm->dqb_curinodes = di->dqb_curinodes;
2298 __set_bit(DQ_LASTSET_B + QIF_INODES_B, &dquot->dq_flags);
2300 if (di->dqb_valid & QIF_ILIMITS) {
2301 dm->dqb_isoftlimit = di->dqb_isoftlimit;
2302 dm->dqb_ihardlimit = di->dqb_ihardlimit;
2304 __set_bit(DQ_LASTSET_B + QIF_ILIMITS_B, &dquot->dq_flags);
2306 if (di->dqb_valid & QIF_BTIME) {
2307 dm->dqb_btime = di->dqb_btime;
2309 __set_bit(DQ_LASTSET_B + QIF_BTIME_B, &dquot->dq_flags);
2311 if (di->dqb_valid & QIF_ITIME) {
2312 dm->dqb_itime = di->dqb_itime;
2314 __set_bit(DQ_LASTSET_B + QIF_ITIME_B, &dquot->dq_flags);
2318 if (!dm->dqb_bsoftlimit || dm->dqb_curspace < dm->dqb_bsoftlimit) {
2320 clear_bit(DQ_BLKS_B, &dquot->dq_flags);
2322 else if (!(di->dqb_valid & QIF_BTIME)) /* Set grace only if user hasn't provided his own... */
2323 dm->dqb_btime = get_seconds() + dqi->dqi_bgrace;
2326 if (!dm->dqb_isoftlimit || dm->dqb_curinodes < dm->dqb_isoftlimit) {
2328 clear_bit(DQ_INODES_B, &dquot->dq_flags);
2330 else if (!(di->dqb_valid & QIF_ITIME)) /* Set grace only if user hasn't provided his own... */
2331 dm->dqb_itime = get_seconds() + dqi->dqi_igrace;
2333 if (dm->dqb_bhardlimit || dm->dqb_bsoftlimit || dm->dqb_ihardlimit || dm->dqb_isoftlimit)
2334 clear_bit(DQ_FAKE_B, &dquot->dq_flags);
2336 set_bit(DQ_FAKE_B, &dquot->dq_flags);
2337 spin_unlock(&dq_data_lock);
2338 mark_dquot_dirty(dquot);
2343 int vfs_set_dqblk(struct super_block *sb, int type, qid_t id, struct if_dqblk *di)
2345 struct dquot *dquot;
2348 dquot = dqget(sb, id, type);
2353 rc = do_set_dqblk(dquot, di);
2358 EXPORT_SYMBOL(vfs_set_dqblk);
2360 /* Generic routine for getting common part of quota file information */
2361 int vfs_get_dqinfo(struct super_block *sb, int type, struct if_dqinfo *ii)
2363 struct mem_dqinfo *mi;
2365 mutex_lock(&sb_dqopt(sb)->dqonoff_mutex);
2366 if (!sb_has_quota_active(sb, type)) {
2367 mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
2370 mi = sb_dqopt(sb)->info + type;
2371 spin_lock(&dq_data_lock);
2372 ii->dqi_bgrace = mi->dqi_bgrace;
2373 ii->dqi_igrace = mi->dqi_igrace;
2374 ii->dqi_flags = mi->dqi_flags & DQF_MASK;
2375 ii->dqi_valid = IIF_ALL;
2376 spin_unlock(&dq_data_lock);
2377 mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
2380 EXPORT_SYMBOL(vfs_get_dqinfo);
2382 /* Generic routine for setting common part of quota file information */
2383 int vfs_set_dqinfo(struct super_block *sb, int type, struct if_dqinfo *ii)
2385 struct mem_dqinfo *mi;
2388 mutex_lock(&sb_dqopt(sb)->dqonoff_mutex);
2389 if (!sb_has_quota_active(sb, type)) {
2393 mi = sb_dqopt(sb)->info + type;
2394 spin_lock(&dq_data_lock);
2395 if (ii->dqi_valid & IIF_BGRACE)
2396 mi->dqi_bgrace = ii->dqi_bgrace;
2397 if (ii->dqi_valid & IIF_IGRACE)
2398 mi->dqi_igrace = ii->dqi_igrace;
2399 if (ii->dqi_valid & IIF_FLAGS)
2400 mi->dqi_flags = (mi->dqi_flags & ~DQF_MASK) | (ii->dqi_flags & DQF_MASK);
2401 spin_unlock(&dq_data_lock);
2402 mark_info_dirty(sb, type);
2403 /* Force write to disk */
2404 sb->dq_op->write_info(sb, type);
2406 mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
2409 EXPORT_SYMBOL(vfs_set_dqinfo);
2411 struct quotactl_ops vfs_quotactl_ops = {
2412 .quota_on = vfs_quota_on,
2413 .quota_off = vfs_quota_off,
2414 .quota_sync = vfs_quota_sync,
2415 .get_info = vfs_get_dqinfo,
2416 .set_info = vfs_set_dqinfo,
2417 .get_dqblk = vfs_get_dqblk,
2418 .set_dqblk = vfs_set_dqblk
2421 static ctl_table fs_dqstats_table[] = {
2423 .ctl_name = FS_DQ_LOOKUPS,
2424 .procname = "lookups",
2425 .data = &dqstats.lookups,
2426 .maxlen = sizeof(int),
2428 .proc_handler = &proc_dointvec,
2431 .ctl_name = FS_DQ_DROPS,
2432 .procname = "drops",
2433 .data = &dqstats.drops,
2434 .maxlen = sizeof(int),
2436 .proc_handler = &proc_dointvec,
2439 .ctl_name = FS_DQ_READS,
2440 .procname = "reads",
2441 .data = &dqstats.reads,
2442 .maxlen = sizeof(int),
2444 .proc_handler = &proc_dointvec,
2447 .ctl_name = FS_DQ_WRITES,
2448 .procname = "writes",
2449 .data = &dqstats.writes,
2450 .maxlen = sizeof(int),
2452 .proc_handler = &proc_dointvec,
2455 .ctl_name = FS_DQ_CACHE_HITS,
2456 .procname = "cache_hits",
2457 .data = &dqstats.cache_hits,
2458 .maxlen = sizeof(int),
2460 .proc_handler = &proc_dointvec,
2463 .ctl_name = FS_DQ_ALLOCATED,
2464 .procname = "allocated_dquots",
2465 .data = &dqstats.allocated_dquots,
2466 .maxlen = sizeof(int),
2468 .proc_handler = &proc_dointvec,
2471 .ctl_name = FS_DQ_FREE,
2472 .procname = "free_dquots",
2473 .data = &dqstats.free_dquots,
2474 .maxlen = sizeof(int),
2476 .proc_handler = &proc_dointvec,
2479 .ctl_name = FS_DQ_SYNCS,
2480 .procname = "syncs",
2481 .data = &dqstats.syncs,
2482 .maxlen = sizeof(int),
2484 .proc_handler = &proc_dointvec,
2486 #ifdef CONFIG_PRINT_QUOTA_WARNING
2488 .ctl_name = FS_DQ_WARNINGS,
2489 .procname = "warnings",
2490 .data = &flag_print_warnings,
2491 .maxlen = sizeof(int),
2493 .proc_handler = &proc_dointvec,
2499 static ctl_table fs_table[] = {
2501 .ctl_name = FS_DQSTATS,
2502 .procname = "quota",
2504 .child = fs_dqstats_table,
2509 static ctl_table sys_table[] = {
2519 static int __init dquot_init(void)
2522 unsigned long nr_hash, order;
2524 printk(KERN_NOTICE "VFS: Disk quotas %s\n", __DQUOT_VERSION__);
2526 register_sysctl_table(sys_table);
2528 dquot_cachep = kmem_cache_create("dquot",
2529 sizeof(struct dquot), sizeof(unsigned long) * 4,
2530 (SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT|
2531 SLAB_MEM_SPREAD|SLAB_PANIC),
2535 dquot_hash = (struct hlist_head *)__get_free_pages(GFP_ATOMIC, order);
2537 panic("Cannot create dquot hash table");
2539 /* Find power-of-two hlist_heads which can fit into allocation */
2540 nr_hash = (1UL << order) * PAGE_SIZE / sizeof(struct hlist_head);
2544 } while (nr_hash >> dq_hash_bits);
2547 nr_hash = 1UL << dq_hash_bits;
2548 dq_hash_mask = nr_hash - 1;
2549 for (i = 0; i < nr_hash; i++)
2550 INIT_HLIST_HEAD(dquot_hash + i);
2552 printk("Dquot-cache hash table entries: %ld (order %ld, %ld bytes)\n",
2553 nr_hash, order, (PAGE_SIZE << order));
2555 register_shrinker(&dqcache_shrinker);
2557 #ifdef CONFIG_QUOTA_NETLINK_INTERFACE
2558 if (genl_register_family("a_genl_family) != 0)
2559 printk(KERN_ERR "VFS: Failed to create quota netlink interface.\n");
2564 module_init(dquot_init);