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 two quota SMP locks. dq_list_lock protects all lists with quotas
91 * and quota formats and also dqstats structure containing statistics about the
92 * lists. dq_data_lock protects data from dq_dqb and also mem_dqinfo structures
93 * and 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().
97 * The spinlock ordering is hence: dq_data_lock > dq_list_lock > i_lock
99 * Note that some things (eg. sb pointer, type, id) doesn't change during
100 * the life of the dquot structure and so needn't to be protected by a lock
102 * Any operation working on dquots via inode pointers must hold dqptr_sem. If
103 * operation is just reading pointers from inode (or not using them at all) the
104 * read lock is enough. If pointers are altered function must hold write lock
105 * (these locking rules also apply for S_NOQUOTA flag in the inode - note that
106 * for altering the flag i_mutex is also needed). If operation is holding
107 * reference to dquot in other way (e.g. quotactl ops) it must be guarded by
109 * This locking assures that:
110 * a) update/access to dquot pointers in inode is serialized
111 * b) everyone is guarded against invalidate_dquots()
113 * Each dquot has its dq_lock mutex. Locked dquots might not be referenced
114 * from inodes (dquot_alloc_space() and such don't check the dq_lock).
115 * Currently dquot is locked only when it is being read to memory (or space for
116 * it is being allocated) on the first dqget() and when it is being released on
117 * the last dqput(). The allocation and release oparations are serialized by
118 * the dq_lock and by checking the use count in dquot_release(). Write
119 * operations on dquots don't hold dq_lock as they copy data under dq_data_lock
120 * spinlock to internal buffers before writing.
122 * Lock ordering (including related VFS locks) is the following:
123 * i_mutex > dqonoff_sem > journal_lock > dqptr_sem > dquot->dq_lock >
125 * i_mutex on quota files is special (it's below dqio_mutex)
128 static DEFINE_SPINLOCK(dq_list_lock);
129 DEFINE_SPINLOCK(dq_data_lock);
131 static char *quotatypes[] = INITQFNAMES;
132 static struct quota_format_type *quota_formats; /* List of registered formats */
133 static struct quota_module_name module_names[] = INIT_QUOTA_MODULE_NAMES;
135 /* SLAB cache for dquot structures */
136 static struct kmem_cache *dquot_cachep;
138 int register_quota_format(struct quota_format_type *fmt)
140 spin_lock(&dq_list_lock);
141 fmt->qf_next = quota_formats;
143 spin_unlock(&dq_list_lock);
147 void unregister_quota_format(struct quota_format_type *fmt)
149 struct quota_format_type **actqf;
151 spin_lock(&dq_list_lock);
152 for (actqf = "a_formats; *actqf && *actqf != fmt; actqf = &(*actqf)->qf_next);
154 *actqf = (*actqf)->qf_next;
155 spin_unlock(&dq_list_lock);
158 static struct quota_format_type *find_quota_format(int id)
160 struct quota_format_type *actqf;
162 spin_lock(&dq_list_lock);
163 for (actqf = quota_formats; actqf && actqf->qf_fmt_id != id; actqf = actqf->qf_next);
164 if (!actqf || !try_module_get(actqf->qf_owner)) {
167 spin_unlock(&dq_list_lock);
169 for (qm = 0; module_names[qm].qm_fmt_id && module_names[qm].qm_fmt_id != id; qm++);
170 if (!module_names[qm].qm_fmt_id || request_module(module_names[qm].qm_mod_name))
173 spin_lock(&dq_list_lock);
174 for (actqf = quota_formats; actqf && actqf->qf_fmt_id != id; actqf = actqf->qf_next);
175 if (actqf && !try_module_get(actqf->qf_owner))
178 spin_unlock(&dq_list_lock);
182 static void put_quota_format(struct quota_format_type *fmt)
184 module_put(fmt->qf_owner);
188 * Dquot List Management:
189 * The quota code uses three lists for dquot management: the inuse_list,
190 * free_dquots, and dquot_hash[] array. A single dquot structure may be
191 * on all three lists, depending on its current state.
193 * All dquots are placed to the end of inuse_list when first created, and this
194 * list is used for invalidate operation, which must look at every dquot.
196 * Unused dquots (dq_count == 0) are added to the free_dquots list when freed,
197 * and this list is searched whenever we need an available dquot. Dquots are
198 * removed from the list as soon as they are used again, and
199 * dqstats.free_dquots gives the number of dquots on the list. When
200 * dquot is invalidated it's completely released from memory.
202 * Dquots with a specific identity (device, type and id) are placed on
203 * one of the dquot_hash[] hash chains. The provides an efficient search
204 * mechanism to locate a specific dquot.
207 static LIST_HEAD(inuse_list);
208 static LIST_HEAD(free_dquots);
209 static unsigned int dq_hash_bits, dq_hash_mask;
210 static struct hlist_head *dquot_hash;
212 struct dqstats dqstats;
214 static inline unsigned int
215 hashfn(const struct super_block *sb, unsigned int id, int type)
219 tmp = (((unsigned long)sb>>L1_CACHE_SHIFT) ^ id) * (MAXQUOTAS - type);
220 return (tmp + (tmp >> dq_hash_bits)) & dq_hash_mask;
224 * Following list functions expect dq_list_lock to be held
226 static inline void insert_dquot_hash(struct dquot *dquot)
228 struct hlist_head *head = dquot_hash + hashfn(dquot->dq_sb, dquot->dq_id, dquot->dq_type);
229 hlist_add_head(&dquot->dq_hash, head);
232 static inline void remove_dquot_hash(struct dquot *dquot)
234 hlist_del_init(&dquot->dq_hash);
237 static inline struct dquot *find_dquot(unsigned int hashent, struct super_block *sb, unsigned int id, int type)
239 struct hlist_node *node;
242 hlist_for_each (node, dquot_hash+hashent) {
243 dquot = hlist_entry(node, struct dquot, dq_hash);
244 if (dquot->dq_sb == sb && dquot->dq_id == id && dquot->dq_type == type)
250 /* Add a dquot to the tail of the free list */
251 static inline void put_dquot_last(struct dquot *dquot)
253 list_add_tail(&dquot->dq_free, &free_dquots);
254 dqstats.free_dquots++;
257 static inline void remove_free_dquot(struct dquot *dquot)
259 if (list_empty(&dquot->dq_free))
261 list_del_init(&dquot->dq_free);
262 dqstats.free_dquots--;
265 static inline void put_inuse(struct dquot *dquot)
267 /* We add to the back of inuse list so we don't have to restart
268 * when traversing this list and we block */
269 list_add_tail(&dquot->dq_inuse, &inuse_list);
270 dqstats.allocated_dquots++;
273 static inline void remove_inuse(struct dquot *dquot)
275 dqstats.allocated_dquots--;
276 list_del(&dquot->dq_inuse);
279 * End of list functions needing dq_list_lock
282 static void wait_on_dquot(struct dquot *dquot)
284 mutex_lock(&dquot->dq_lock);
285 mutex_unlock(&dquot->dq_lock);
288 static inline int dquot_dirty(struct dquot *dquot)
290 return test_bit(DQ_MOD_B, &dquot->dq_flags);
293 static inline int mark_dquot_dirty(struct dquot *dquot)
295 return dquot->dq_sb->dq_op->mark_dirty(dquot);
298 int dquot_mark_dquot_dirty(struct dquot *dquot)
300 spin_lock(&dq_list_lock);
301 if (!test_and_set_bit(DQ_MOD_B, &dquot->dq_flags))
302 list_add(&dquot->dq_dirty, &sb_dqopt(dquot->dq_sb)->
303 info[dquot->dq_type].dqi_dirty_list);
304 spin_unlock(&dq_list_lock);
308 /* This function needs dq_list_lock */
309 static inline int clear_dquot_dirty(struct dquot *dquot)
311 if (!test_and_clear_bit(DQ_MOD_B, &dquot->dq_flags))
313 list_del_init(&dquot->dq_dirty);
317 void mark_info_dirty(struct super_block *sb, int type)
319 set_bit(DQF_INFO_DIRTY_B, &sb_dqopt(sb)->info[type].dqi_flags);
321 EXPORT_SYMBOL(mark_info_dirty);
324 * Read dquot from disk and alloc space for it
327 int dquot_acquire(struct dquot *dquot)
329 int ret = 0, ret2 = 0;
330 struct quota_info *dqopt = sb_dqopt(dquot->dq_sb);
332 mutex_lock(&dquot->dq_lock);
333 mutex_lock(&dqopt->dqio_mutex);
334 if (!test_bit(DQ_READ_B, &dquot->dq_flags))
335 ret = dqopt->ops[dquot->dq_type]->read_dqblk(dquot);
338 set_bit(DQ_READ_B, &dquot->dq_flags);
339 /* Instantiate dquot if needed */
340 if (!test_bit(DQ_ACTIVE_B, &dquot->dq_flags) && !dquot->dq_off) {
341 ret = dqopt->ops[dquot->dq_type]->commit_dqblk(dquot);
342 /* Write the info if needed */
343 if (info_dirty(&dqopt->info[dquot->dq_type]))
344 ret2 = dqopt->ops[dquot->dq_type]->write_file_info(dquot->dq_sb, dquot->dq_type);
352 set_bit(DQ_ACTIVE_B, &dquot->dq_flags);
354 mutex_unlock(&dqopt->dqio_mutex);
355 mutex_unlock(&dquot->dq_lock);
360 * Write dquot to disk
362 int dquot_commit(struct dquot *dquot)
364 int ret = 0, ret2 = 0;
365 struct quota_info *dqopt = sb_dqopt(dquot->dq_sb);
367 mutex_lock(&dqopt->dqio_mutex);
368 spin_lock(&dq_list_lock);
369 if (!clear_dquot_dirty(dquot)) {
370 spin_unlock(&dq_list_lock);
373 spin_unlock(&dq_list_lock);
374 /* Inactive dquot can be only if there was error during read/init
375 * => we have better not writing it */
376 if (test_bit(DQ_ACTIVE_B, &dquot->dq_flags)) {
377 ret = dqopt->ops[dquot->dq_type]->commit_dqblk(dquot);
378 if (info_dirty(&dqopt->info[dquot->dq_type]))
379 ret2 = dqopt->ops[dquot->dq_type]->write_file_info(dquot->dq_sb, dquot->dq_type);
384 mutex_unlock(&dqopt->dqio_mutex);
391 int dquot_release(struct dquot *dquot)
393 int ret = 0, ret2 = 0;
394 struct quota_info *dqopt = sb_dqopt(dquot->dq_sb);
396 mutex_lock(&dquot->dq_lock);
397 /* Check whether we are not racing with some other dqget() */
398 if (atomic_read(&dquot->dq_count) > 1)
400 mutex_lock(&dqopt->dqio_mutex);
401 if (dqopt->ops[dquot->dq_type]->release_dqblk) {
402 ret = dqopt->ops[dquot->dq_type]->release_dqblk(dquot);
404 if (info_dirty(&dqopt->info[dquot->dq_type]))
405 ret2 = dqopt->ops[dquot->dq_type]->write_file_info(dquot->dq_sb, dquot->dq_type);
409 clear_bit(DQ_ACTIVE_B, &dquot->dq_flags);
410 mutex_unlock(&dqopt->dqio_mutex);
412 mutex_unlock(&dquot->dq_lock);
416 static void dquot_destroy(struct dquot *dquot)
418 kmem_cache_free(dquot_cachep, dquot);
421 static inline void do_destroy_dquot(struct dquot *dquot)
423 dquot->dq_sb->dq_op->destroy_dquot(dquot);
426 /* Invalidate all dquots on the list. Note that this function is called after
427 * quota is disabled and pointers from inodes removed so there cannot be new
428 * quota users. There can still be some users of quotas due to inodes being
429 * just deleted or pruned by prune_icache() (those are not attached to any
430 * list). We have to wait for such users.
432 static void invalidate_dquots(struct super_block *sb, int type)
434 struct dquot *dquot, *tmp;
437 spin_lock(&dq_list_lock);
438 list_for_each_entry_safe(dquot, tmp, &inuse_list, dq_inuse) {
439 if (dquot->dq_sb != sb)
441 if (dquot->dq_type != type)
443 /* Wait for dquot users */
444 if (atomic_read(&dquot->dq_count)) {
447 atomic_inc(&dquot->dq_count);
448 prepare_to_wait(&dquot->dq_wait_unused, &wait,
449 TASK_UNINTERRUPTIBLE);
450 spin_unlock(&dq_list_lock);
451 /* Once dqput() wakes us up, we know it's time to free
453 * IMPORTANT: we rely on the fact that there is always
454 * at most one process waiting for dquot to free.
455 * Otherwise dq_count would be > 1 and we would never
458 if (atomic_read(&dquot->dq_count) > 1)
460 finish_wait(&dquot->dq_wait_unused, &wait);
462 /* At this moment dquot() need not exist (it could be
463 * reclaimed by prune_dqcache(). Hence we must
468 * Quota now has no users and it has been written on last
471 remove_dquot_hash(dquot);
472 remove_free_dquot(dquot);
474 do_destroy_dquot(dquot);
476 spin_unlock(&dq_list_lock);
479 int vfs_quota_sync(struct super_block *sb, int type)
481 struct list_head *dirty;
483 struct quota_info *dqopt = sb_dqopt(sb);
486 mutex_lock(&dqopt->dqonoff_mutex);
487 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
488 if (type != -1 && cnt != type)
490 if (!sb_has_quota_active(sb, cnt))
492 spin_lock(&dq_list_lock);
493 dirty = &dqopt->info[cnt].dqi_dirty_list;
494 while (!list_empty(dirty)) {
495 dquot = list_first_entry(dirty, struct dquot, dq_dirty);
496 /* Dirty and inactive can be only bad dquot... */
497 if (!test_bit(DQ_ACTIVE_B, &dquot->dq_flags)) {
498 clear_dquot_dirty(dquot);
501 /* Now we have active dquot from which someone is
502 * holding reference so we can safely just increase
504 atomic_inc(&dquot->dq_count);
506 spin_unlock(&dq_list_lock);
507 sb->dq_op->write_dquot(dquot);
509 spin_lock(&dq_list_lock);
511 spin_unlock(&dq_list_lock);
514 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
515 if ((cnt == type || type == -1) && sb_has_quota_active(sb, cnt)
516 && info_dirty(&dqopt->info[cnt]))
517 sb->dq_op->write_info(sb, cnt);
518 spin_lock(&dq_list_lock);
520 spin_unlock(&dq_list_lock);
521 mutex_unlock(&dqopt->dqonoff_mutex);
526 /* Free unused dquots from cache */
527 static void prune_dqcache(int count)
529 struct list_head *head;
532 head = free_dquots.prev;
533 while (head != &free_dquots && count) {
534 dquot = list_entry(head, struct dquot, dq_free);
535 remove_dquot_hash(dquot);
536 remove_free_dquot(dquot);
538 do_destroy_dquot(dquot);
540 head = free_dquots.prev;
545 * This is called from kswapd when we think we need some
549 static int shrink_dqcache_memory(int nr, gfp_t gfp_mask)
552 spin_lock(&dq_list_lock);
554 spin_unlock(&dq_list_lock);
556 return (dqstats.free_dquots / 100) * sysctl_vfs_cache_pressure;
559 static struct shrinker dqcache_shrinker = {
560 .shrink = shrink_dqcache_memory,
561 .seeks = DEFAULT_SEEKS,
565 * Put reference to dquot
566 * NOTE: If you change this function please check whether dqput_blocks() works right...
567 * MUST be called with either dqptr_sem or dqonoff_mutex held
569 void dqput(struct dquot *dquot)
575 #ifdef __DQUOT_PARANOIA
576 if (!atomic_read(&dquot->dq_count)) {
577 printk("VFS: dqput: trying to free free dquot\n");
578 printk("VFS: device %s, dquot of %s %d\n",
580 quotatypes[dquot->dq_type],
586 spin_lock(&dq_list_lock);
588 spin_unlock(&dq_list_lock);
590 spin_lock(&dq_list_lock);
591 if (atomic_read(&dquot->dq_count) > 1) {
592 /* We have more than one user... nothing to do */
593 atomic_dec(&dquot->dq_count);
594 /* Releasing dquot during quotaoff phase? */
595 if (!sb_has_quota_active(dquot->dq_sb, dquot->dq_type) &&
596 atomic_read(&dquot->dq_count) == 1)
597 wake_up(&dquot->dq_wait_unused);
598 spin_unlock(&dq_list_lock);
601 /* Need to release dquot? */
602 if (test_bit(DQ_ACTIVE_B, &dquot->dq_flags) && dquot_dirty(dquot)) {
603 spin_unlock(&dq_list_lock);
604 /* Commit dquot before releasing */
605 ret = dquot->dq_sb->dq_op->write_dquot(dquot);
607 printk(KERN_ERR "VFS: cannot write quota structure on "
608 "device %s (error %d). Quota may get out of "
609 "sync!\n", dquot->dq_sb->s_id, ret);
611 * We clear dirty bit anyway, so that we avoid
614 spin_lock(&dq_list_lock);
615 clear_dquot_dirty(dquot);
616 spin_unlock(&dq_list_lock);
620 /* Clear flag in case dquot was inactive (something bad happened) */
621 clear_dquot_dirty(dquot);
622 if (test_bit(DQ_ACTIVE_B, &dquot->dq_flags)) {
623 spin_unlock(&dq_list_lock);
624 dquot->dq_sb->dq_op->release_dquot(dquot);
627 atomic_dec(&dquot->dq_count);
628 #ifdef __DQUOT_PARANOIA
630 BUG_ON(!list_empty(&dquot->dq_free));
632 put_dquot_last(dquot);
633 spin_unlock(&dq_list_lock);
636 static struct dquot *dquot_alloc(struct super_block *sb, int type)
638 return kmem_cache_zalloc(dquot_cachep, GFP_NOFS);
641 static struct dquot *get_empty_dquot(struct super_block *sb, int type)
645 dquot = sb->dq_op->alloc_dquot(sb, type);
649 mutex_init(&dquot->dq_lock);
650 INIT_LIST_HEAD(&dquot->dq_free);
651 INIT_LIST_HEAD(&dquot->dq_inuse);
652 INIT_HLIST_NODE(&dquot->dq_hash);
653 INIT_LIST_HEAD(&dquot->dq_dirty);
654 init_waitqueue_head(&dquot->dq_wait_unused);
656 dquot->dq_type = type;
657 atomic_set(&dquot->dq_count, 1);
663 * Check whether dquot is in memory.
664 * MUST be called with either dqptr_sem or dqonoff_mutex held
666 int dquot_is_cached(struct super_block *sb, unsigned int id, int type)
668 unsigned int hashent = hashfn(sb, id, type);
671 if (!sb_has_quota_active(sb, type))
673 spin_lock(&dq_list_lock);
674 if (find_dquot(hashent, sb, id, type) != NODQUOT)
676 spin_unlock(&dq_list_lock);
681 * Get reference to dquot
682 * MUST be called with either dqptr_sem or dqonoff_mutex held
684 struct dquot *dqget(struct super_block *sb, unsigned int id, int type)
686 unsigned int hashent = hashfn(sb, id, type);
687 struct dquot *dquot, *empty = NODQUOT;
689 if (!sb_has_quota_active(sb, type))
692 spin_lock(&dq_list_lock);
693 if ((dquot = find_dquot(hashent, sb, id, type)) == NODQUOT) {
694 if (empty == NODQUOT) {
695 spin_unlock(&dq_list_lock);
696 if ((empty = get_empty_dquot(sb, type)) == NODQUOT)
697 schedule(); /* Try to wait for a moment... */
702 /* all dquots go on the inuse_list */
704 /* hash it first so it can be found */
705 insert_dquot_hash(dquot);
707 spin_unlock(&dq_list_lock);
709 if (!atomic_read(&dquot->dq_count))
710 remove_free_dquot(dquot);
711 atomic_inc(&dquot->dq_count);
712 dqstats.cache_hits++;
714 spin_unlock(&dq_list_lock);
716 do_destroy_dquot(empty);
718 /* Wait for dq_lock - after this we know that either dquot_release() is already
719 * finished or it will be canceled due to dq_count > 1 test */
720 wait_on_dquot(dquot);
721 /* Read the dquot and instantiate it (everything done only if needed) */
722 if (!test_bit(DQ_ACTIVE_B, &dquot->dq_flags) && sb->dq_op->acquire_dquot(dquot) < 0) {
726 #ifdef __DQUOT_PARANOIA
727 BUG_ON(!dquot->dq_sb); /* Has somebody invalidated entry under us? */
733 static int dqinit_needed(struct inode *inode, int type)
737 if (IS_NOQUOTA(inode))
740 return inode->i_dquot[type] == NODQUOT;
741 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
742 if (inode->i_dquot[cnt] == NODQUOT)
747 /* This routine is guarded by dqonoff_mutex mutex */
748 static void add_dquot_ref(struct super_block *sb, int type)
750 struct inode *inode, *old_inode = NULL;
752 spin_lock(&inode_lock);
753 list_for_each_entry(inode, &sb->s_inodes, i_sb_list) {
754 if (!atomic_read(&inode->i_writecount))
756 if (!dqinit_needed(inode, type))
758 if (inode->i_state & (I_FREEING|I_WILL_FREE))
762 spin_unlock(&inode_lock);
765 sb->dq_op->initialize(inode, type);
766 /* We hold a reference to 'inode' so it couldn't have been
767 * removed from s_inodes list while we dropped the inode_lock.
768 * We cannot iput the inode now as we can be holding the last
769 * reference and we cannot iput it under inode_lock. So we
770 * keep the reference and iput it later. */
772 spin_lock(&inode_lock);
774 spin_unlock(&inode_lock);
778 /* Return 0 if dqput() won't block (note that 1 doesn't necessarily mean blocking) */
779 static inline int dqput_blocks(struct dquot *dquot)
781 if (atomic_read(&dquot->dq_count) <= 1)
786 /* Remove references to dquots from inode - add dquot to list for freeing if needed */
787 /* We can't race with anybody because we hold dqptr_sem for writing... */
788 static int remove_inode_dquot_ref(struct inode *inode, int type,
789 struct list_head *tofree_head)
791 struct dquot *dquot = inode->i_dquot[type];
793 inode->i_dquot[type] = NODQUOT;
794 if (dquot != NODQUOT) {
795 if (dqput_blocks(dquot)) {
796 #ifdef __DQUOT_PARANOIA
797 if (atomic_read(&dquot->dq_count) != 1)
798 printk(KERN_WARNING "VFS: Adding dquot with dq_count %d to dispose list.\n", atomic_read(&dquot->dq_count));
800 spin_lock(&dq_list_lock);
801 list_add(&dquot->dq_free, tofree_head); /* As dquot must have currently users it can't be on the free list... */
802 spin_unlock(&dq_list_lock);
806 dqput(dquot); /* We have guaranteed we won't block */
811 /* Free list of dquots - called from inode.c */
812 /* dquots are removed from inodes, no new references can be got so we are the only ones holding reference */
813 static void put_dquot_list(struct list_head *tofree_head)
815 struct list_head *act_head;
818 act_head = tofree_head->next;
819 /* So now we have dquots on the list... Just free them */
820 while (act_head != tofree_head) {
821 dquot = list_entry(act_head, struct dquot, dq_free);
822 act_head = act_head->next;
823 list_del_init(&dquot->dq_free); /* Remove dquot from the list so we won't have problems... */
828 static void remove_dquot_ref(struct super_block *sb, int type,
829 struct list_head *tofree_head)
833 spin_lock(&inode_lock);
834 list_for_each_entry(inode, &sb->s_inodes, i_sb_list) {
835 if (!IS_NOQUOTA(inode))
836 remove_inode_dquot_ref(inode, type, tofree_head);
838 spin_unlock(&inode_lock);
841 /* Gather all references from inodes and drop them */
842 static void drop_dquot_ref(struct super_block *sb, int type)
844 LIST_HEAD(tofree_head);
847 down_write(&sb_dqopt(sb)->dqptr_sem);
848 remove_dquot_ref(sb, type, &tofree_head);
849 up_write(&sb_dqopt(sb)->dqptr_sem);
850 put_dquot_list(&tofree_head);
854 static inline void dquot_incr_inodes(struct dquot *dquot, qsize_t number)
856 dquot->dq_dqb.dqb_curinodes += number;
859 static inline void dquot_incr_space(struct dquot *dquot, qsize_t number)
861 dquot->dq_dqb.dqb_curspace += number;
864 static inline void dquot_decr_inodes(struct dquot *dquot, qsize_t number)
866 if (sb_dqopt(dquot->dq_sb)->flags & DQUOT_NEGATIVE_USAGE ||
867 dquot->dq_dqb.dqb_curinodes >= number)
868 dquot->dq_dqb.dqb_curinodes -= number;
870 dquot->dq_dqb.dqb_curinodes = 0;
871 if (dquot->dq_dqb.dqb_curinodes <= dquot->dq_dqb.dqb_isoftlimit)
872 dquot->dq_dqb.dqb_itime = (time_t) 0;
873 clear_bit(DQ_INODES_B, &dquot->dq_flags);
876 static inline void dquot_decr_space(struct dquot *dquot, qsize_t number)
878 if (sb_dqopt(dquot->dq_sb)->flags & DQUOT_NEGATIVE_USAGE ||
879 dquot->dq_dqb.dqb_curspace >= number)
880 dquot->dq_dqb.dqb_curspace -= number;
882 dquot->dq_dqb.dqb_curspace = 0;
883 if (dquot->dq_dqb.dqb_curspace <= dquot->dq_dqb.dqb_bsoftlimit)
884 dquot->dq_dqb.dqb_btime = (time_t) 0;
885 clear_bit(DQ_BLKS_B, &dquot->dq_flags);
888 static int warning_issued(struct dquot *dquot, const int warntype)
890 int flag = (warntype == QUOTA_NL_BHARDWARN ||
891 warntype == QUOTA_NL_BSOFTLONGWARN) ? DQ_BLKS_B :
892 ((warntype == QUOTA_NL_IHARDWARN ||
893 warntype == QUOTA_NL_ISOFTLONGWARN) ? DQ_INODES_B : 0);
897 return test_and_set_bit(flag, &dquot->dq_flags);
900 #ifdef CONFIG_PRINT_QUOTA_WARNING
901 static int flag_print_warnings = 1;
903 static inline int need_print_warning(struct dquot *dquot)
905 if (!flag_print_warnings)
908 switch (dquot->dq_type) {
910 return current_fsuid() == dquot->dq_id;
912 return in_group_p(dquot->dq_id);
917 /* Print warning to user which exceeded quota */
918 static void print_warning(struct dquot *dquot, const int warntype)
921 struct tty_struct *tty;
923 if (warntype == QUOTA_NL_IHARDBELOW ||
924 warntype == QUOTA_NL_ISOFTBELOW ||
925 warntype == QUOTA_NL_BHARDBELOW ||
926 warntype == QUOTA_NL_BSOFTBELOW || !need_print_warning(dquot))
929 tty = get_current_tty();
932 tty_write_message(tty, dquot->dq_sb->s_id);
933 if (warntype == QUOTA_NL_ISOFTWARN || warntype == QUOTA_NL_BSOFTWARN)
934 tty_write_message(tty, ": warning, ");
936 tty_write_message(tty, ": write failed, ");
937 tty_write_message(tty, quotatypes[dquot->dq_type]);
939 case QUOTA_NL_IHARDWARN:
940 msg = " file limit reached.\r\n";
942 case QUOTA_NL_ISOFTLONGWARN:
943 msg = " file quota exceeded too long.\r\n";
945 case QUOTA_NL_ISOFTWARN:
946 msg = " file quota exceeded.\r\n";
948 case QUOTA_NL_BHARDWARN:
949 msg = " block limit reached.\r\n";
951 case QUOTA_NL_BSOFTLONGWARN:
952 msg = " block quota exceeded too long.\r\n";
954 case QUOTA_NL_BSOFTWARN:
955 msg = " block quota exceeded.\r\n";
958 tty_write_message(tty, msg);
963 #ifdef CONFIG_QUOTA_NETLINK_INTERFACE
965 /* Netlink family structure for quota */
966 static struct genl_family quota_genl_family = {
967 .id = GENL_ID_GENERATE,
971 .maxattr = QUOTA_NL_A_MAX,
974 /* Send warning to userspace about user which exceeded quota */
975 static void send_warning(const struct dquot *dquot, const char warntype)
981 int msg_size = 4 * nla_total_size(sizeof(u32)) +
982 2 * nla_total_size(sizeof(u64));
984 /* We have to allocate using GFP_NOFS as we are called from a
985 * filesystem performing write and thus further recursion into
986 * the fs to free some data could cause deadlocks. */
987 skb = genlmsg_new(msg_size, GFP_NOFS);
990 "VFS: Not enough memory to send quota warning.\n");
993 msg_head = genlmsg_put(skb, 0, atomic_add_return(1, &seq),
994 "a_genl_family, 0, QUOTA_NL_C_WARNING);
997 "VFS: Cannot store netlink header in quota warning.\n");
1000 ret = nla_put_u32(skb, QUOTA_NL_A_QTYPE, dquot->dq_type);
1003 ret = nla_put_u64(skb, QUOTA_NL_A_EXCESS_ID, dquot->dq_id);
1006 ret = nla_put_u32(skb, QUOTA_NL_A_WARNING, warntype);
1009 ret = nla_put_u32(skb, QUOTA_NL_A_DEV_MAJOR,
1010 MAJOR(dquot->dq_sb->s_dev));
1013 ret = nla_put_u32(skb, QUOTA_NL_A_DEV_MINOR,
1014 MINOR(dquot->dq_sb->s_dev));
1017 ret = nla_put_u64(skb, QUOTA_NL_A_CAUSED_ID, current_uid());
1020 genlmsg_end(skb, msg_head);
1022 ret = genlmsg_multicast(skb, 0, quota_genl_family.id, GFP_NOFS);
1023 if (ret < 0 && ret != -ESRCH)
1025 "VFS: Failed to send notification message: %d\n", ret);
1028 printk(KERN_ERR "VFS: Not enough space to compose quota message!\n");
1034 static inline void flush_warnings(struct dquot * const *dquots, char *warntype)
1038 for (i = 0; i < MAXQUOTAS; i++)
1039 if (dquots[i] != NODQUOT && warntype[i] != QUOTA_NL_NOWARN &&
1040 !warning_issued(dquots[i], warntype[i])) {
1041 #ifdef CONFIG_PRINT_QUOTA_WARNING
1042 print_warning(dquots[i], warntype[i]);
1044 #ifdef CONFIG_QUOTA_NETLINK_INTERFACE
1045 send_warning(dquots[i], warntype[i]);
1050 static inline char ignore_hardlimit(struct dquot *dquot)
1052 struct mem_dqinfo *info = &sb_dqopt(dquot->dq_sb)->info[dquot->dq_type];
1054 return capable(CAP_SYS_RESOURCE) &&
1055 (info->dqi_format->qf_fmt_id != QFMT_VFS_OLD || !(info->dqi_flags & V1_DQF_RSQUASH));
1058 /* needs dq_data_lock */
1059 static int check_idq(struct dquot *dquot, qsize_t inodes, char *warntype)
1061 *warntype = QUOTA_NL_NOWARN;
1062 if (!sb_has_quota_limits_enabled(dquot->dq_sb, dquot->dq_type) ||
1063 test_bit(DQ_FAKE_B, &dquot->dq_flags))
1066 if (dquot->dq_dqb.dqb_ihardlimit &&
1067 (dquot->dq_dqb.dqb_curinodes + inodes) > dquot->dq_dqb.dqb_ihardlimit &&
1068 !ignore_hardlimit(dquot)) {
1069 *warntype = QUOTA_NL_IHARDWARN;
1073 if (dquot->dq_dqb.dqb_isoftlimit &&
1074 (dquot->dq_dqb.dqb_curinodes + inodes) > dquot->dq_dqb.dqb_isoftlimit &&
1075 dquot->dq_dqb.dqb_itime && get_seconds() >= dquot->dq_dqb.dqb_itime &&
1076 !ignore_hardlimit(dquot)) {
1077 *warntype = QUOTA_NL_ISOFTLONGWARN;
1081 if (dquot->dq_dqb.dqb_isoftlimit &&
1082 (dquot->dq_dqb.dqb_curinodes + inodes) > dquot->dq_dqb.dqb_isoftlimit &&
1083 dquot->dq_dqb.dqb_itime == 0) {
1084 *warntype = QUOTA_NL_ISOFTWARN;
1085 dquot->dq_dqb.dqb_itime = get_seconds() + sb_dqopt(dquot->dq_sb)->info[dquot->dq_type].dqi_igrace;
1091 /* needs dq_data_lock */
1092 static int check_bdq(struct dquot *dquot, qsize_t space, int prealloc, char *warntype)
1094 *warntype = QUOTA_NL_NOWARN;
1095 if (!sb_has_quota_limits_enabled(dquot->dq_sb, dquot->dq_type) ||
1096 test_bit(DQ_FAKE_B, &dquot->dq_flags))
1099 if (dquot->dq_dqb.dqb_bhardlimit &&
1100 dquot->dq_dqb.dqb_curspace + space > dquot->dq_dqb.dqb_bhardlimit &&
1101 !ignore_hardlimit(dquot)) {
1103 *warntype = QUOTA_NL_BHARDWARN;
1107 if (dquot->dq_dqb.dqb_bsoftlimit &&
1108 dquot->dq_dqb.dqb_curspace + space > dquot->dq_dqb.dqb_bsoftlimit &&
1109 dquot->dq_dqb.dqb_btime && get_seconds() >= dquot->dq_dqb.dqb_btime &&
1110 !ignore_hardlimit(dquot)) {
1112 *warntype = QUOTA_NL_BSOFTLONGWARN;
1116 if (dquot->dq_dqb.dqb_bsoftlimit &&
1117 dquot->dq_dqb.dqb_curspace + space > dquot->dq_dqb.dqb_bsoftlimit &&
1118 dquot->dq_dqb.dqb_btime == 0) {
1120 *warntype = QUOTA_NL_BSOFTWARN;
1121 dquot->dq_dqb.dqb_btime = get_seconds() + sb_dqopt(dquot->dq_sb)->info[dquot->dq_type].dqi_bgrace;
1125 * We don't allow preallocation to exceed softlimit so exceeding will
1134 static int info_idq_free(struct dquot *dquot, qsize_t inodes)
1136 if (test_bit(DQ_FAKE_B, &dquot->dq_flags) ||
1137 dquot->dq_dqb.dqb_curinodes <= dquot->dq_dqb.dqb_isoftlimit ||
1138 !sb_has_quota_limits_enabled(dquot->dq_sb, dquot->dq_type))
1139 return QUOTA_NL_NOWARN;
1141 if (dquot->dq_dqb.dqb_curinodes - inodes <= dquot->dq_dqb.dqb_isoftlimit)
1142 return QUOTA_NL_ISOFTBELOW;
1143 if (dquot->dq_dqb.dqb_curinodes >= dquot->dq_dqb.dqb_ihardlimit &&
1144 dquot->dq_dqb.dqb_curinodes - inodes < dquot->dq_dqb.dqb_ihardlimit)
1145 return QUOTA_NL_IHARDBELOW;
1146 return QUOTA_NL_NOWARN;
1149 static int info_bdq_free(struct dquot *dquot, qsize_t space)
1151 if (test_bit(DQ_FAKE_B, &dquot->dq_flags) ||
1152 dquot->dq_dqb.dqb_curspace <= dquot->dq_dqb.dqb_bsoftlimit)
1153 return QUOTA_NL_NOWARN;
1155 if (dquot->dq_dqb.dqb_curspace - space <= dquot->dq_dqb.dqb_bsoftlimit)
1156 return QUOTA_NL_BSOFTBELOW;
1157 if (dquot->dq_dqb.dqb_curspace >= dquot->dq_dqb.dqb_bhardlimit &&
1158 dquot->dq_dqb.dqb_curspace - space < dquot->dq_dqb.dqb_bhardlimit)
1159 return QUOTA_NL_BHARDBELOW;
1160 return QUOTA_NL_NOWARN;
1163 * Initialize quota pointers in inode
1164 * Transaction must be started at entry
1166 int dquot_initialize(struct inode *inode, int type)
1168 unsigned int id = 0;
1171 /* First test before acquiring mutex - solves deadlocks when we
1172 * re-enter the quota code and are already holding the mutex */
1173 if (IS_NOQUOTA(inode))
1175 down_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
1176 /* Having dqptr_sem we know NOQUOTA flags can't be altered... */
1177 if (IS_NOQUOTA(inode))
1179 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1180 if (type != -1 && cnt != type)
1182 if (inode->i_dquot[cnt] == NODQUOT) {
1191 inode->i_dquot[cnt] = dqget(inode->i_sb, id, cnt);
1195 up_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
1200 * Release all quotas referenced by inode
1201 * Transaction must be started at an entry
1203 int dquot_drop_locked(struct inode *inode)
1207 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1208 if (inode->i_dquot[cnt] != NODQUOT) {
1209 dqput(inode->i_dquot[cnt]);
1210 inode->i_dquot[cnt] = NODQUOT;
1216 int dquot_drop(struct inode *inode)
1218 down_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
1219 dquot_drop_locked(inode);
1220 up_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
1224 /* Wrapper to remove references to quota structures from inode */
1225 void vfs_dq_drop(struct inode *inode)
1227 /* Here we can get arbitrary inode from clear_inode() so we have
1228 * to be careful. OTOH we don't need locking as quota operations
1229 * are allowed to change only at mount time */
1230 if (!IS_NOQUOTA(inode) && inode->i_sb && inode->i_sb->dq_op
1231 && inode->i_sb->dq_op->drop) {
1233 /* Test before calling to rule out calls from proc and such
1234 * where we are not allowed to block. Note that this is
1235 * actually reliable test even without the lock - the caller
1236 * must assure that nobody can come after the DQUOT_DROP and
1237 * add quota pointers back anyway */
1238 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1239 if (inode->i_dquot[cnt] != NODQUOT)
1241 if (cnt < MAXQUOTAS)
1242 inode->i_sb->dq_op->drop(inode);
1247 * Following four functions update i_blocks+i_bytes fields and
1248 * quota information (together with appropriate checks)
1249 * NOTE: We absolutely rely on the fact that caller dirties
1250 * the inode (usually macros in quotaops.h care about this) and
1251 * holds a handle for the current transaction so that dquot write and
1252 * inode write go into the same transaction.
1256 * This operation can block, but only after everything is updated
1258 int dquot_alloc_space(struct inode *inode, qsize_t number, int warn)
1260 int cnt, ret = NO_QUOTA;
1261 char warntype[MAXQUOTAS];
1263 /* First test before acquiring mutex - solves deadlocks when we
1264 * re-enter the quota code and are already holding the mutex */
1265 if (IS_NOQUOTA(inode)) {
1267 inode_add_bytes(inode, number);
1270 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1271 warntype[cnt] = QUOTA_NL_NOWARN;
1273 down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1274 if (IS_NOQUOTA(inode)) { /* Now we can do reliable test... */
1275 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1278 spin_lock(&dq_data_lock);
1279 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1280 if (inode->i_dquot[cnt] == NODQUOT)
1282 if (check_bdq(inode->i_dquot[cnt], number, warn, warntype+cnt) == NO_QUOTA)
1285 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1286 if (inode->i_dquot[cnt] == NODQUOT)
1288 dquot_incr_space(inode->i_dquot[cnt], number);
1290 inode_add_bytes(inode, number);
1293 spin_unlock(&dq_data_lock);
1294 if (ret == QUOTA_OK)
1295 /* Dirtify all the dquots - this can block when journalling */
1296 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1297 if (inode->i_dquot[cnt])
1298 mark_dquot_dirty(inode->i_dquot[cnt]);
1299 flush_warnings(inode->i_dquot, warntype);
1300 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1305 * This operation can block, but only after everything is updated
1307 int dquot_alloc_inode(const struct inode *inode, qsize_t number)
1309 int cnt, ret = NO_QUOTA;
1310 char warntype[MAXQUOTAS];
1312 /* First test before acquiring mutex - solves deadlocks when we
1313 * re-enter the quota code and are already holding the mutex */
1314 if (IS_NOQUOTA(inode))
1316 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1317 warntype[cnt] = QUOTA_NL_NOWARN;
1318 down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1319 if (IS_NOQUOTA(inode)) {
1320 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1323 spin_lock(&dq_data_lock);
1324 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1325 if (inode->i_dquot[cnt] == NODQUOT)
1327 if (check_idq(inode->i_dquot[cnt], number, warntype+cnt) == NO_QUOTA)
1331 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1332 if (inode->i_dquot[cnt] == NODQUOT)
1334 dquot_incr_inodes(inode->i_dquot[cnt], number);
1338 spin_unlock(&dq_data_lock);
1339 if (ret == QUOTA_OK)
1340 /* Dirtify all the dquots - this can block when journalling */
1341 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1342 if (inode->i_dquot[cnt])
1343 mark_dquot_dirty(inode->i_dquot[cnt]);
1344 flush_warnings(inode->i_dquot, warntype);
1345 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1350 * This operation can block, but only after everything is updated
1352 int dquot_free_space(struct inode *inode, qsize_t number)
1355 char warntype[MAXQUOTAS];
1357 /* First test before acquiring mutex - solves deadlocks when we
1358 * re-enter the quota code and are already holding the mutex */
1359 if (IS_NOQUOTA(inode)) {
1361 inode_sub_bytes(inode, number);
1365 down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1366 /* Now recheck reliably when holding dqptr_sem */
1367 if (IS_NOQUOTA(inode)) {
1368 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1371 spin_lock(&dq_data_lock);
1372 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1373 if (inode->i_dquot[cnt] == NODQUOT)
1375 warntype[cnt] = info_bdq_free(inode->i_dquot[cnt], number);
1376 dquot_decr_space(inode->i_dquot[cnt], number);
1378 inode_sub_bytes(inode, number);
1379 spin_unlock(&dq_data_lock);
1380 /* Dirtify all the dquots - this can block when journalling */
1381 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1382 if (inode->i_dquot[cnt])
1383 mark_dquot_dirty(inode->i_dquot[cnt]);
1384 flush_warnings(inode->i_dquot, warntype);
1385 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1390 * This operation can block, but only after everything is updated
1392 int dquot_free_inode(const struct inode *inode, qsize_t number)
1395 char warntype[MAXQUOTAS];
1397 /* First test before acquiring mutex - solves deadlocks when we
1398 * re-enter the quota code and are already holding the mutex */
1399 if (IS_NOQUOTA(inode))
1402 down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1403 /* Now recheck reliably when holding dqptr_sem */
1404 if (IS_NOQUOTA(inode)) {
1405 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1408 spin_lock(&dq_data_lock);
1409 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1410 if (inode->i_dquot[cnt] == NODQUOT)
1412 warntype[cnt] = info_idq_free(inode->i_dquot[cnt], number);
1413 dquot_decr_inodes(inode->i_dquot[cnt], number);
1415 spin_unlock(&dq_data_lock);
1416 /* Dirtify all the dquots - this can block when journalling */
1417 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1418 if (inode->i_dquot[cnt])
1419 mark_dquot_dirty(inode->i_dquot[cnt]);
1420 flush_warnings(inode->i_dquot, warntype);
1421 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1426 * Transfer the number of inode and blocks from one diskquota to an other.
1428 * This operation can block, but only after everything is updated
1429 * A transaction must be started when entering this function.
1431 int dquot_transfer(struct inode *inode, struct iattr *iattr)
1434 struct dquot *transfer_from[MAXQUOTAS];
1435 struct dquot *transfer_to[MAXQUOTAS];
1436 int cnt, ret = NO_QUOTA, chuid = (iattr->ia_valid & ATTR_UID) && inode->i_uid != iattr->ia_uid,
1437 chgid = (iattr->ia_valid & ATTR_GID) && inode->i_gid != iattr->ia_gid;
1438 char warntype_to[MAXQUOTAS];
1439 char warntype_from_inodes[MAXQUOTAS], warntype_from_space[MAXQUOTAS];
1441 /* First test before acquiring mutex - solves deadlocks when we
1442 * re-enter the quota code and are already holding the mutex */
1443 if (IS_NOQUOTA(inode))
1445 /* Clear the arrays */
1446 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1447 transfer_to[cnt] = transfer_from[cnt] = NODQUOT;
1448 warntype_to[cnt] = QUOTA_NL_NOWARN;
1450 down_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
1451 /* Now recheck reliably when holding dqptr_sem */
1452 if (IS_NOQUOTA(inode)) { /* File without quota accounting? */
1453 up_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
1456 /* First build the transfer_to list - here we can block on
1457 * reading/instantiating of dquots. We know that the transaction for
1458 * us was already started so we don't violate lock ranking here */
1459 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1464 transfer_to[cnt] = dqget(inode->i_sb, iattr->ia_uid, cnt);
1469 transfer_to[cnt] = dqget(inode->i_sb, iattr->ia_gid, cnt);
1473 spin_lock(&dq_data_lock);
1474 space = inode_get_bytes(inode);
1475 /* Build the transfer_from list and check the limits */
1476 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1477 if (transfer_to[cnt] == NODQUOT)
1479 transfer_from[cnt] = inode->i_dquot[cnt];
1480 if (check_idq(transfer_to[cnt], 1, warntype_to + cnt) ==
1481 NO_QUOTA || check_bdq(transfer_to[cnt], space, 0,
1482 warntype_to + cnt) == NO_QUOTA)
1487 * Finally perform the needed transfer from transfer_from to transfer_to
1489 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1491 * Skip changes for same uid or gid or for turned off quota-type.
1493 if (transfer_to[cnt] == NODQUOT)
1496 /* Due to IO error we might not have transfer_from[] structure */
1497 if (transfer_from[cnt]) {
1498 warntype_from_inodes[cnt] =
1499 info_idq_free(transfer_from[cnt], 1);
1500 warntype_from_space[cnt] =
1501 info_bdq_free(transfer_from[cnt], space);
1502 dquot_decr_inodes(transfer_from[cnt], 1);
1503 dquot_decr_space(transfer_from[cnt], space);
1506 dquot_incr_inodes(transfer_to[cnt], 1);
1507 dquot_incr_space(transfer_to[cnt], space);
1509 inode->i_dquot[cnt] = transfer_to[cnt];
1513 spin_unlock(&dq_data_lock);
1514 /* Dirtify all the dquots - this can block when journalling */
1515 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1516 if (transfer_from[cnt])
1517 mark_dquot_dirty(transfer_from[cnt]);
1518 if (transfer_to[cnt])
1519 mark_dquot_dirty(transfer_to[cnt]);
1521 flush_warnings(transfer_to, warntype_to);
1522 flush_warnings(transfer_from, warntype_from_inodes);
1523 flush_warnings(transfer_from, warntype_from_space);
1525 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1526 if (ret == QUOTA_OK && transfer_from[cnt] != NODQUOT)
1527 dqput(transfer_from[cnt]);
1528 if (ret == NO_QUOTA && transfer_to[cnt] != NODQUOT)
1529 dqput(transfer_to[cnt]);
1531 up_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
1535 /* Wrapper for transferring ownership of an inode */
1536 int vfs_dq_transfer(struct inode *inode, struct iattr *iattr)
1538 if (sb_any_quota_active(inode->i_sb) && !IS_NOQUOTA(inode)) {
1540 if (inode->i_sb->dq_op->transfer(inode, iattr) == NO_QUOTA)
1548 * Write info of quota file to disk
1550 int dquot_commit_info(struct super_block *sb, int type)
1553 struct quota_info *dqopt = sb_dqopt(sb);
1555 mutex_lock(&dqopt->dqio_mutex);
1556 ret = dqopt->ops[type]->write_file_info(sb, type);
1557 mutex_unlock(&dqopt->dqio_mutex);
1562 * Definitions of diskquota operations.
1564 struct dquot_operations dquot_operations = {
1565 .initialize = dquot_initialize,
1567 .alloc_space = dquot_alloc_space,
1568 .alloc_inode = dquot_alloc_inode,
1569 .free_space = dquot_free_space,
1570 .free_inode = dquot_free_inode,
1571 .transfer = dquot_transfer,
1572 .write_dquot = dquot_commit,
1573 .acquire_dquot = dquot_acquire,
1574 .release_dquot = dquot_release,
1575 .mark_dirty = dquot_mark_dquot_dirty,
1576 .write_info = dquot_commit_info,
1577 .alloc_dquot = dquot_alloc,
1578 .destroy_dquot = dquot_destroy,
1582 * Turn quota off on a device. type == -1 ==> quotaoff for all types (umount)
1584 int vfs_quota_disable(struct super_block *sb, int type, unsigned int flags)
1587 struct quota_info *dqopt = sb_dqopt(sb);
1588 struct inode *toputinode[MAXQUOTAS];
1590 /* Cannot turn off usage accounting without turning off limits, or
1591 * suspend quotas and simultaneously turn quotas off. */
1592 if ((flags & DQUOT_USAGE_ENABLED && !(flags & DQUOT_LIMITS_ENABLED))
1593 || (flags & DQUOT_SUSPENDED && flags & (DQUOT_LIMITS_ENABLED |
1594 DQUOT_USAGE_ENABLED)))
1597 /* We need to serialize quota_off() for device */
1598 mutex_lock(&dqopt->dqonoff_mutex);
1601 * Skip everything if there's nothing to do. We have to do this because
1602 * sometimes we are called when fill_super() failed and calling
1603 * sync_fs() in such cases does no good.
1605 if (!sb_any_quota_loaded(sb)) {
1606 mutex_unlock(&dqopt->dqonoff_mutex);
1609 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1610 toputinode[cnt] = NULL;
1611 if (type != -1 && cnt != type)
1613 if (!sb_has_quota_loaded(sb, cnt))
1616 if (flags & DQUOT_SUSPENDED) {
1618 dquot_state_flag(DQUOT_SUSPENDED, cnt);
1620 dqopt->flags &= ~dquot_state_flag(flags, cnt);
1621 /* Turning off suspended quotas? */
1622 if (!sb_has_quota_loaded(sb, cnt) &&
1623 sb_has_quota_suspended(sb, cnt)) {
1624 dqopt->flags &= ~dquot_state_flag(
1625 DQUOT_SUSPENDED, cnt);
1626 iput(dqopt->files[cnt]);
1627 dqopt->files[cnt] = NULL;
1632 /* We still have to keep quota loaded? */
1633 if (sb_has_quota_loaded(sb, cnt) && !(flags & DQUOT_SUSPENDED))
1636 /* Note: these are blocking operations */
1637 drop_dquot_ref(sb, cnt);
1638 invalidate_dquots(sb, cnt);
1640 * Now all dquots should be invalidated, all writes done so we should be only
1641 * users of the info. No locks needed.
1643 if (info_dirty(&dqopt->info[cnt]))
1644 sb->dq_op->write_info(sb, cnt);
1645 if (dqopt->ops[cnt]->free_file_info)
1646 dqopt->ops[cnt]->free_file_info(sb, cnt);
1647 put_quota_format(dqopt->info[cnt].dqi_format);
1649 toputinode[cnt] = dqopt->files[cnt];
1650 if (!sb_has_quota_loaded(sb, cnt))
1651 dqopt->files[cnt] = NULL;
1652 dqopt->info[cnt].dqi_flags = 0;
1653 dqopt->info[cnt].dqi_igrace = 0;
1654 dqopt->info[cnt].dqi_bgrace = 0;
1655 dqopt->ops[cnt] = NULL;
1657 mutex_unlock(&dqopt->dqonoff_mutex);
1659 /* Skip syncing and setting flags if quota files are hidden */
1660 if (dqopt->flags & DQUOT_QUOTA_SYS_FILE)
1663 /* Sync the superblock so that buffers with quota data are written to
1664 * disk (and so userspace sees correct data afterwards). */
1665 if (sb->s_op->sync_fs)
1666 sb->s_op->sync_fs(sb, 1);
1667 sync_blockdev(sb->s_bdev);
1668 /* Now the quota files are just ordinary files and we can set the
1669 * inode flags back. Moreover we discard the pagecache so that
1670 * userspace sees the writes we did bypassing the pagecache. We
1671 * must also discard the blockdev buffers so that we see the
1672 * changes done by userspace on the next quotaon() */
1673 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1674 if (toputinode[cnt]) {
1675 mutex_lock(&dqopt->dqonoff_mutex);
1676 /* If quota was reenabled in the meantime, we have
1678 if (!sb_has_quota_loaded(sb, cnt)) {
1679 mutex_lock_nested(&toputinode[cnt]->i_mutex, I_MUTEX_QUOTA);
1680 toputinode[cnt]->i_flags &= ~(S_IMMUTABLE |
1681 S_NOATIME | S_NOQUOTA);
1682 truncate_inode_pages(&toputinode[cnt]->i_data, 0);
1683 mutex_unlock(&toputinode[cnt]->i_mutex);
1684 mark_inode_dirty(toputinode[cnt]);
1686 mutex_unlock(&dqopt->dqonoff_mutex);
1689 invalidate_bdev(sb->s_bdev);
1691 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1692 if (toputinode[cnt]) {
1693 /* On remount RO, we keep the inode pointer so that we
1694 * can reenable quota on the subsequent remount RW. We
1695 * have to check 'flags' variable and not use sb_has_
1696 * function because another quotaon / quotaoff could
1697 * change global state before we got here. We refuse
1698 * to suspend quotas when there is pending delete on
1699 * the quota file... */
1700 if (!(flags & DQUOT_SUSPENDED))
1701 iput(toputinode[cnt]);
1702 else if (!toputinode[cnt]->i_nlink)
1708 int vfs_quota_off(struct super_block *sb, int type, int remount)
1710 return vfs_quota_disable(sb, type, remount ? DQUOT_SUSPENDED :
1711 (DQUOT_USAGE_ENABLED | DQUOT_LIMITS_ENABLED));
1715 * Turn quotas on on a device
1719 * Helper function to turn quotas on when we already have the inode of
1720 * quota file and no quota information is loaded.
1722 static int vfs_load_quota_inode(struct inode *inode, int type, int format_id,
1725 struct quota_format_type *fmt = find_quota_format(format_id);
1726 struct super_block *sb = inode->i_sb;
1727 struct quota_info *dqopt = sb_dqopt(sb);
1733 if (!S_ISREG(inode->i_mode)) {
1737 if (IS_RDONLY(inode)) {
1741 if (!sb->s_op->quota_write || !sb->s_op->quota_read) {
1745 /* Usage always has to be set... */
1746 if (!(flags & DQUOT_USAGE_ENABLED)) {
1751 if (!(dqopt->flags & DQUOT_QUOTA_SYS_FILE)) {
1752 /* As we bypass the pagecache we must now flush the inode so
1753 * that we see all the changes from userspace... */
1754 write_inode_now(inode, 1);
1755 /* And now flush the block cache so that kernel sees the
1757 invalidate_bdev(sb->s_bdev);
1759 mutex_lock(&inode->i_mutex);
1760 mutex_lock(&dqopt->dqonoff_mutex);
1761 if (sb_has_quota_loaded(sb, type)) {
1766 if (!(dqopt->flags & DQUOT_QUOTA_SYS_FILE)) {
1767 /* We don't want quota and atime on quota files (deadlocks
1768 * possible) Also nobody should write to the file - we use
1769 * special IO operations which ignore the immutable bit. */
1770 down_write(&dqopt->dqptr_sem);
1771 oldflags = inode->i_flags & (S_NOATIME | S_IMMUTABLE | S_NOQUOTA);
1772 inode->i_flags |= S_NOQUOTA | S_NOATIME | S_IMMUTABLE;
1773 up_write(&dqopt->dqptr_sem);
1774 sb->dq_op->drop(inode);
1778 dqopt->files[type] = igrab(inode);
1779 if (!dqopt->files[type])
1782 if (!fmt->qf_ops->check_quota_file(sb, type))
1785 dqopt->ops[type] = fmt->qf_ops;
1786 dqopt->info[type].dqi_format = fmt;
1787 dqopt->info[type].dqi_fmt_id = format_id;
1788 INIT_LIST_HEAD(&dqopt->info[type].dqi_dirty_list);
1789 mutex_lock(&dqopt->dqio_mutex);
1790 if ((error = dqopt->ops[type]->read_file_info(sb, type)) < 0) {
1791 mutex_unlock(&dqopt->dqio_mutex);
1794 mutex_unlock(&dqopt->dqio_mutex);
1795 mutex_unlock(&inode->i_mutex);
1796 dqopt->flags |= dquot_state_flag(flags, type);
1798 add_dquot_ref(sb, type);
1799 mutex_unlock(&dqopt->dqonoff_mutex);
1804 dqopt->files[type] = NULL;
1807 mutex_unlock(&dqopt->dqonoff_mutex);
1808 if (oldflags != -1) {
1809 down_write(&dqopt->dqptr_sem);
1810 /* Set the flags back (in the case of accidental quotaon()
1811 * on a wrong file we don't want to mess up the flags) */
1812 inode->i_flags &= ~(S_NOATIME | S_NOQUOTA | S_IMMUTABLE);
1813 inode->i_flags |= oldflags;
1814 up_write(&dqopt->dqptr_sem);
1816 mutex_unlock(&inode->i_mutex);
1818 put_quota_format(fmt);
1823 /* Reenable quotas on remount RW */
1824 static int vfs_quota_on_remount(struct super_block *sb, int type)
1826 struct quota_info *dqopt = sb_dqopt(sb);
1827 struct inode *inode;
1831 mutex_lock(&dqopt->dqonoff_mutex);
1832 if (!sb_has_quota_suspended(sb, type)) {
1833 mutex_unlock(&dqopt->dqonoff_mutex);
1836 inode = dqopt->files[type];
1837 dqopt->files[type] = NULL;
1838 flags = dqopt->flags & dquot_state_flag(DQUOT_USAGE_ENABLED |
1839 DQUOT_LIMITS_ENABLED, type);
1840 dqopt->flags &= ~dquot_state_flag(DQUOT_STATE_FLAGS, type);
1841 mutex_unlock(&dqopt->dqonoff_mutex);
1843 flags = dquot_generic_flag(flags, type);
1844 ret = vfs_load_quota_inode(inode, type, dqopt->info[type].dqi_fmt_id,
1851 int vfs_quota_on_path(struct super_block *sb, int type, int format_id,
1854 int error = security_quota_on(path->dentry);
1857 /* Quota file not on the same filesystem? */
1858 if (path->mnt->mnt_sb != sb)
1861 error = vfs_load_quota_inode(path->dentry->d_inode, type,
1862 format_id, DQUOT_USAGE_ENABLED |
1863 DQUOT_LIMITS_ENABLED);
1867 int vfs_quota_on(struct super_block *sb, int type, int format_id, char *name,
1874 return vfs_quota_on_remount(sb, type);
1876 error = kern_path(name, LOOKUP_FOLLOW, &path);
1878 error = vfs_quota_on_path(sb, type, format_id, &path);
1885 * More powerful function for turning on quotas allowing setting
1886 * of individual quota flags
1888 int vfs_quota_enable(struct inode *inode, int type, int format_id,
1892 struct super_block *sb = inode->i_sb;
1893 struct quota_info *dqopt = sb_dqopt(sb);
1895 /* Just unsuspend quotas? */
1896 if (flags & DQUOT_SUSPENDED)
1897 return vfs_quota_on_remount(sb, type);
1900 /* Just updating flags needed? */
1901 if (sb_has_quota_loaded(sb, type)) {
1902 mutex_lock(&dqopt->dqonoff_mutex);
1903 /* Now do a reliable test... */
1904 if (!sb_has_quota_loaded(sb, type)) {
1905 mutex_unlock(&dqopt->dqonoff_mutex);
1908 if (flags & DQUOT_USAGE_ENABLED &&
1909 sb_has_quota_usage_enabled(sb, type)) {
1913 if (flags & DQUOT_LIMITS_ENABLED &&
1914 sb_has_quota_limits_enabled(sb, type)) {
1918 sb_dqopt(sb)->flags |= dquot_state_flag(flags, type);
1920 mutex_unlock(&dqopt->dqonoff_mutex);
1925 return vfs_load_quota_inode(inode, type, format_id, flags);
1929 * This function is used when filesystem needs to initialize quotas
1930 * during mount time.
1932 int vfs_quota_on_mount(struct super_block *sb, char *qf_name,
1933 int format_id, int type)
1935 struct dentry *dentry;
1938 dentry = lookup_one_len(qf_name, sb->s_root, strlen(qf_name));
1940 return PTR_ERR(dentry);
1942 if (!dentry->d_inode) {
1947 error = security_quota_on(dentry);
1949 error = vfs_load_quota_inode(dentry->d_inode, type, format_id,
1950 DQUOT_USAGE_ENABLED | DQUOT_LIMITS_ENABLED);
1957 /* Wrapper to turn on quotas when remounting rw */
1958 int vfs_dq_quota_on_remount(struct super_block *sb)
1963 if (!sb->s_qcop || !sb->s_qcop->quota_on)
1965 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1966 err = sb->s_qcop->quota_on(sb, cnt, 0, NULL, 1);
1967 if (err < 0 && !ret)
1973 static inline qsize_t qbtos(qsize_t blocks)
1975 return blocks << QIF_DQBLKSIZE_BITS;
1978 static inline qsize_t stoqb(qsize_t space)
1980 return (space + QIF_DQBLKSIZE - 1) >> QIF_DQBLKSIZE_BITS;
1983 /* Generic routine for getting common part of quota structure */
1984 static void do_get_dqblk(struct dquot *dquot, struct if_dqblk *di)
1986 struct mem_dqblk *dm = &dquot->dq_dqb;
1988 spin_lock(&dq_data_lock);
1989 di->dqb_bhardlimit = stoqb(dm->dqb_bhardlimit);
1990 di->dqb_bsoftlimit = stoqb(dm->dqb_bsoftlimit);
1991 di->dqb_curspace = dm->dqb_curspace;
1992 di->dqb_ihardlimit = dm->dqb_ihardlimit;
1993 di->dqb_isoftlimit = dm->dqb_isoftlimit;
1994 di->dqb_curinodes = dm->dqb_curinodes;
1995 di->dqb_btime = dm->dqb_btime;
1996 di->dqb_itime = dm->dqb_itime;
1997 di->dqb_valid = QIF_ALL;
1998 spin_unlock(&dq_data_lock);
2001 int vfs_get_dqblk(struct super_block *sb, int type, qid_t id, struct if_dqblk *di)
2003 struct dquot *dquot;
2005 mutex_lock(&sb_dqopt(sb)->dqonoff_mutex);
2006 if (!(dquot = dqget(sb, id, type))) {
2007 mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
2010 do_get_dqblk(dquot, di);
2012 mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
2016 /* Generic routine for setting common part of quota structure */
2017 static int do_set_dqblk(struct dquot *dquot, struct if_dqblk *di)
2019 struct mem_dqblk *dm = &dquot->dq_dqb;
2020 int check_blim = 0, check_ilim = 0;
2021 struct mem_dqinfo *dqi = &sb_dqopt(dquot->dq_sb)->info[dquot->dq_type];
2023 if ((di->dqb_valid & QIF_BLIMITS &&
2024 (di->dqb_bhardlimit > dqi->dqi_maxblimit ||
2025 di->dqb_bsoftlimit > dqi->dqi_maxblimit)) ||
2026 (di->dqb_valid & QIF_ILIMITS &&
2027 (di->dqb_ihardlimit > dqi->dqi_maxilimit ||
2028 di->dqb_isoftlimit > dqi->dqi_maxilimit)))
2031 spin_lock(&dq_data_lock);
2032 if (di->dqb_valid & QIF_SPACE) {
2033 dm->dqb_curspace = di->dqb_curspace;
2035 __set_bit(DQ_LASTSET_B + QIF_SPACE_B, &dquot->dq_flags);
2037 if (di->dqb_valid & QIF_BLIMITS) {
2038 dm->dqb_bsoftlimit = qbtos(di->dqb_bsoftlimit);
2039 dm->dqb_bhardlimit = qbtos(di->dqb_bhardlimit);
2041 __set_bit(DQ_LASTSET_B + QIF_BLIMITS_B, &dquot->dq_flags);
2043 if (di->dqb_valid & QIF_INODES) {
2044 dm->dqb_curinodes = di->dqb_curinodes;
2046 __set_bit(DQ_LASTSET_B + QIF_INODES_B, &dquot->dq_flags);
2048 if (di->dqb_valid & QIF_ILIMITS) {
2049 dm->dqb_isoftlimit = di->dqb_isoftlimit;
2050 dm->dqb_ihardlimit = di->dqb_ihardlimit;
2052 __set_bit(DQ_LASTSET_B + QIF_ILIMITS_B, &dquot->dq_flags);
2054 if (di->dqb_valid & QIF_BTIME) {
2055 dm->dqb_btime = di->dqb_btime;
2056 __set_bit(DQ_LASTSET_B + QIF_BTIME_B, &dquot->dq_flags);
2058 if (di->dqb_valid & QIF_ITIME) {
2059 dm->dqb_itime = di->dqb_itime;
2060 __set_bit(DQ_LASTSET_B + QIF_ITIME_B, &dquot->dq_flags);
2064 if (!dm->dqb_bsoftlimit || dm->dqb_curspace < dm->dqb_bsoftlimit) {
2066 clear_bit(DQ_BLKS_B, &dquot->dq_flags);
2068 else if (!(di->dqb_valid & QIF_BTIME)) /* Set grace only if user hasn't provided his own... */
2069 dm->dqb_btime = get_seconds() + dqi->dqi_bgrace;
2072 if (!dm->dqb_isoftlimit || dm->dqb_curinodes < dm->dqb_isoftlimit) {
2074 clear_bit(DQ_INODES_B, &dquot->dq_flags);
2076 else if (!(di->dqb_valid & QIF_ITIME)) /* Set grace only if user hasn't provided his own... */
2077 dm->dqb_itime = get_seconds() + dqi->dqi_igrace;
2079 if (dm->dqb_bhardlimit || dm->dqb_bsoftlimit || dm->dqb_ihardlimit || dm->dqb_isoftlimit)
2080 clear_bit(DQ_FAKE_B, &dquot->dq_flags);
2082 set_bit(DQ_FAKE_B, &dquot->dq_flags);
2083 spin_unlock(&dq_data_lock);
2084 mark_dquot_dirty(dquot);
2089 int vfs_set_dqblk(struct super_block *sb, int type, qid_t id, struct if_dqblk *di)
2091 struct dquot *dquot;
2094 mutex_lock(&sb_dqopt(sb)->dqonoff_mutex);
2095 dquot = dqget(sb, id, type);
2100 rc = do_set_dqblk(dquot, di);
2103 mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
2107 /* Generic routine for getting common part of quota file information */
2108 int vfs_get_dqinfo(struct super_block *sb, int type, struct if_dqinfo *ii)
2110 struct mem_dqinfo *mi;
2112 mutex_lock(&sb_dqopt(sb)->dqonoff_mutex);
2113 if (!sb_has_quota_active(sb, type)) {
2114 mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
2117 mi = sb_dqopt(sb)->info + type;
2118 spin_lock(&dq_data_lock);
2119 ii->dqi_bgrace = mi->dqi_bgrace;
2120 ii->dqi_igrace = mi->dqi_igrace;
2121 ii->dqi_flags = mi->dqi_flags & DQF_MASK;
2122 ii->dqi_valid = IIF_ALL;
2123 spin_unlock(&dq_data_lock);
2124 mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
2128 /* Generic routine for setting common part of quota file information */
2129 int vfs_set_dqinfo(struct super_block *sb, int type, struct if_dqinfo *ii)
2131 struct mem_dqinfo *mi;
2134 mutex_lock(&sb_dqopt(sb)->dqonoff_mutex);
2135 if (!sb_has_quota_active(sb, type)) {
2139 mi = sb_dqopt(sb)->info + type;
2140 spin_lock(&dq_data_lock);
2141 if (ii->dqi_valid & IIF_BGRACE)
2142 mi->dqi_bgrace = ii->dqi_bgrace;
2143 if (ii->dqi_valid & IIF_IGRACE)
2144 mi->dqi_igrace = ii->dqi_igrace;
2145 if (ii->dqi_valid & IIF_FLAGS)
2146 mi->dqi_flags = (mi->dqi_flags & ~DQF_MASK) | (ii->dqi_flags & DQF_MASK);
2147 spin_unlock(&dq_data_lock);
2148 mark_info_dirty(sb, type);
2149 /* Force write to disk */
2150 sb->dq_op->write_info(sb, type);
2152 mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
2156 struct quotactl_ops vfs_quotactl_ops = {
2157 .quota_on = vfs_quota_on,
2158 .quota_off = vfs_quota_off,
2159 .quota_sync = vfs_quota_sync,
2160 .get_info = vfs_get_dqinfo,
2161 .set_info = vfs_set_dqinfo,
2162 .get_dqblk = vfs_get_dqblk,
2163 .set_dqblk = vfs_set_dqblk
2166 static ctl_table fs_dqstats_table[] = {
2168 .ctl_name = FS_DQ_LOOKUPS,
2169 .procname = "lookups",
2170 .data = &dqstats.lookups,
2171 .maxlen = sizeof(int),
2173 .proc_handler = &proc_dointvec,
2176 .ctl_name = FS_DQ_DROPS,
2177 .procname = "drops",
2178 .data = &dqstats.drops,
2179 .maxlen = sizeof(int),
2181 .proc_handler = &proc_dointvec,
2184 .ctl_name = FS_DQ_READS,
2185 .procname = "reads",
2186 .data = &dqstats.reads,
2187 .maxlen = sizeof(int),
2189 .proc_handler = &proc_dointvec,
2192 .ctl_name = FS_DQ_WRITES,
2193 .procname = "writes",
2194 .data = &dqstats.writes,
2195 .maxlen = sizeof(int),
2197 .proc_handler = &proc_dointvec,
2200 .ctl_name = FS_DQ_CACHE_HITS,
2201 .procname = "cache_hits",
2202 .data = &dqstats.cache_hits,
2203 .maxlen = sizeof(int),
2205 .proc_handler = &proc_dointvec,
2208 .ctl_name = FS_DQ_ALLOCATED,
2209 .procname = "allocated_dquots",
2210 .data = &dqstats.allocated_dquots,
2211 .maxlen = sizeof(int),
2213 .proc_handler = &proc_dointvec,
2216 .ctl_name = FS_DQ_FREE,
2217 .procname = "free_dquots",
2218 .data = &dqstats.free_dquots,
2219 .maxlen = sizeof(int),
2221 .proc_handler = &proc_dointvec,
2224 .ctl_name = FS_DQ_SYNCS,
2225 .procname = "syncs",
2226 .data = &dqstats.syncs,
2227 .maxlen = sizeof(int),
2229 .proc_handler = &proc_dointvec,
2231 #ifdef CONFIG_PRINT_QUOTA_WARNING
2233 .ctl_name = FS_DQ_WARNINGS,
2234 .procname = "warnings",
2235 .data = &flag_print_warnings,
2236 .maxlen = sizeof(int),
2238 .proc_handler = &proc_dointvec,
2244 static ctl_table fs_table[] = {
2246 .ctl_name = FS_DQSTATS,
2247 .procname = "quota",
2249 .child = fs_dqstats_table,
2254 static ctl_table sys_table[] = {
2264 static int __init dquot_init(void)
2267 unsigned long nr_hash, order;
2269 printk(KERN_NOTICE "VFS: Disk quotas %s\n", __DQUOT_VERSION__);
2271 register_sysctl_table(sys_table);
2273 dquot_cachep = kmem_cache_create("dquot",
2274 sizeof(struct dquot), sizeof(unsigned long) * 4,
2275 (SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT|
2276 SLAB_MEM_SPREAD|SLAB_PANIC),
2280 dquot_hash = (struct hlist_head *)__get_free_pages(GFP_ATOMIC, order);
2282 panic("Cannot create dquot hash table");
2284 /* Find power-of-two hlist_heads which can fit into allocation */
2285 nr_hash = (1UL << order) * PAGE_SIZE / sizeof(struct hlist_head);
2289 } while (nr_hash >> dq_hash_bits);
2292 nr_hash = 1UL << dq_hash_bits;
2293 dq_hash_mask = nr_hash - 1;
2294 for (i = 0; i < nr_hash; i++)
2295 INIT_HLIST_HEAD(dquot_hash + i);
2297 printk("Dquot-cache hash table entries: %ld (order %ld, %ld bytes)\n",
2298 nr_hash, order, (PAGE_SIZE << order));
2300 register_shrinker(&dqcache_shrinker);
2302 #ifdef CONFIG_QUOTA_NETLINK_INTERFACE
2303 if (genl_register_family("a_genl_family) != 0)
2304 printk(KERN_ERR "VFS: Failed to create quota netlink interface.\n");
2309 module_init(dquot_init);
2311 EXPORT_SYMBOL(register_quota_format);
2312 EXPORT_SYMBOL(unregister_quota_format);
2313 EXPORT_SYMBOL(dqstats);
2314 EXPORT_SYMBOL(dq_data_lock);
2315 EXPORT_SYMBOL(vfs_quota_enable);
2316 EXPORT_SYMBOL(vfs_quota_on);
2317 EXPORT_SYMBOL(vfs_quota_on_path);
2318 EXPORT_SYMBOL(vfs_quota_on_mount);
2319 EXPORT_SYMBOL(vfs_quota_disable);
2320 EXPORT_SYMBOL(vfs_quota_off);
2321 EXPORT_SYMBOL(vfs_quota_sync);
2322 EXPORT_SYMBOL(vfs_get_dqinfo);
2323 EXPORT_SYMBOL(vfs_set_dqinfo);
2324 EXPORT_SYMBOL(vfs_get_dqblk);
2325 EXPORT_SYMBOL(vfs_set_dqblk);
2326 EXPORT_SYMBOL(dquot_commit);
2327 EXPORT_SYMBOL(dquot_commit_info);
2328 EXPORT_SYMBOL(dquot_acquire);
2329 EXPORT_SYMBOL(dquot_release);
2330 EXPORT_SYMBOL(dquot_mark_dquot_dirty);
2331 EXPORT_SYMBOL(dquot_initialize);
2332 EXPORT_SYMBOL(dquot_drop);
2333 EXPORT_SYMBOL(dquot_drop_locked);
2334 EXPORT_SYMBOL(vfs_dq_drop);
2335 EXPORT_SYMBOL(dqget);
2336 EXPORT_SYMBOL(dqput);
2337 EXPORT_SYMBOL(dquot_is_cached);
2338 EXPORT_SYMBOL(dquot_alloc_space);
2339 EXPORT_SYMBOL(dquot_alloc_inode);
2340 EXPORT_SYMBOL(dquot_free_space);
2341 EXPORT_SYMBOL(dquot_free_inode);
2342 EXPORT_SYMBOL(dquot_transfer);
2343 EXPORT_SYMBOL(vfs_dq_transfer);
2344 EXPORT_SYMBOL(vfs_dq_quota_on_remount);