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.. */
81 #include <asm/uaccess.h>
84 * There are three quota SMP locks. dq_list_lock protects all lists with quotas
86 * dq_data_lock protects data from dq_dqb and also mem_dqinfo structures and
87 * also guards consistency of dquot->dq_dqb with inode->i_blocks, i_bytes.
88 * i_blocks and i_bytes updates itself are guarded by i_lock acquired directly
89 * in inode_add_bytes() and inode_sub_bytes(). dq_state_lock protects
90 * modifications of quota state (on quotaon and quotaoff) and readers who care
91 * about latest values take it as well.
93 * The spinlock ordering is hence: dq_data_lock > dq_list_lock > i_lock,
94 * dq_list_lock > dq_state_lock
96 * Note that some things (eg. sb pointer, type, id) doesn't change during
97 * the life of the dquot structure and so needn't to be protected by a lock
99 * Any operation working on dquots via inode pointers must hold dqptr_sem. If
100 * operation is just reading pointers from inode (or not using them at all) the
101 * read lock is enough. If pointers are altered function must hold write lock.
102 * Special care needs to be taken about S_NOQUOTA inode flag (marking that
103 * inode is a quota file). Functions adding pointers from inode to dquots have
104 * to check this flag under dqptr_sem and then (if S_NOQUOTA is not set) they
105 * have to do all pointer modifications before dropping dqptr_sem. This makes
106 * sure they cannot race with quotaon which first sets S_NOQUOTA flag and
107 * then drops all pointers to dquots from an inode.
109 * Each dquot has its dq_lock mutex. Locked dquots might not be referenced
110 * from inodes (dquot_alloc_space() and such don't check the dq_lock).
111 * Currently dquot is locked only when it is being read to memory (or space for
112 * it is being allocated) on the first dqget() and when it is being released on
113 * the last dqput(). The allocation and release oparations are serialized by
114 * the dq_lock and by checking the use count in dquot_release(). Write
115 * operations on dquots don't hold dq_lock as they copy data under dq_data_lock
116 * spinlock to internal buffers before writing.
118 * Lock ordering (including related VFS locks) is the following:
119 * i_mutex > dqonoff_sem > journal_lock > dqptr_sem > dquot->dq_lock >
121 * The lock ordering of dqptr_sem imposed by quota code is only dqonoff_sem >
122 * dqptr_sem. But filesystem has to count with the fact that functions such as
123 * dquot_alloc_space() acquire dqptr_sem and they usually have to be called
124 * from inside a transaction to keep filesystem consistency after a crash. Also
125 * filesystems usually want to do some IO on dquot from ->mark_dirty which is
126 * called with dqptr_sem held.
127 * i_mutex on quota files is special (it's below dqio_mutex)
130 static __cacheline_aligned_in_smp DEFINE_SPINLOCK(dq_list_lock);
131 static __cacheline_aligned_in_smp DEFINE_SPINLOCK(dq_state_lock);
132 __cacheline_aligned_in_smp DEFINE_SPINLOCK(dq_data_lock);
133 EXPORT_SYMBOL(dq_data_lock);
135 #if defined(CONFIG_QUOTA_DEBUG) || defined(CONFIG_PRINT_QUOTA_WARNING)
136 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;
160 actqf = &(*actqf)->qf_next)
163 *actqf = (*actqf)->qf_next;
164 spin_unlock(&dq_list_lock);
166 EXPORT_SYMBOL(unregister_quota_format);
168 static struct quota_format_type *find_quota_format(int id)
170 struct quota_format_type *actqf;
172 spin_lock(&dq_list_lock);
173 for (actqf = quota_formats; actqf && actqf->qf_fmt_id != id;
174 actqf = actqf->qf_next)
176 if (!actqf || !try_module_get(actqf->qf_owner)) {
179 spin_unlock(&dq_list_lock);
181 for (qm = 0; module_names[qm].qm_fmt_id &&
182 module_names[qm].qm_fmt_id != id; qm++)
184 if (!module_names[qm].qm_fmt_id ||
185 request_module(module_names[qm].qm_mod_name))
188 spin_lock(&dq_list_lock);
189 for (actqf = quota_formats; actqf && actqf->qf_fmt_id != id;
190 actqf = actqf->qf_next)
192 if (actqf && !try_module_get(actqf->qf_owner))
195 spin_unlock(&dq_list_lock);
199 static void put_quota_format(struct quota_format_type *fmt)
201 module_put(fmt->qf_owner);
205 * Dquot List Management:
206 * The quota code uses three lists for dquot management: the inuse_list,
207 * free_dquots, and dquot_hash[] array. A single dquot structure may be
208 * on all three lists, depending on its current state.
210 * All dquots are placed to the end of inuse_list when first created, and this
211 * list is used for invalidate operation, which must look at every dquot.
213 * Unused dquots (dq_count == 0) are added to the free_dquots list when freed,
214 * and this list is searched whenever we need an available dquot. Dquots are
215 * removed from the list as soon as they are used again, and
216 * dqstats.free_dquots gives the number of dquots on the list. When
217 * dquot is invalidated it's completely released from memory.
219 * Dquots with a specific identity (device, type and id) are placed on
220 * one of the dquot_hash[] hash chains. The provides an efficient search
221 * mechanism to locate a specific dquot.
224 static LIST_HEAD(inuse_list);
225 static LIST_HEAD(free_dquots);
226 static unsigned int dq_hash_bits, dq_hash_mask;
227 static struct hlist_head *dquot_hash;
229 struct dqstats dqstats;
230 EXPORT_SYMBOL(dqstats);
232 struct dqstats *dqstats_pcpu;
233 EXPORT_SYMBOL(dqstats_pcpu);
236 static qsize_t inode_get_rsv_space(struct inode *inode);
237 static void __dquot_initialize(struct inode *inode, int type);
239 static inline unsigned int
240 hashfn(const struct super_block *sb, unsigned int id, int type)
244 tmp = (((unsigned long)sb>>L1_CACHE_SHIFT) ^ id) * (MAXQUOTAS - type);
245 return (tmp + (tmp >> dq_hash_bits)) & dq_hash_mask;
249 * Following list functions expect dq_list_lock to be held
251 static inline void insert_dquot_hash(struct dquot *dquot)
253 struct hlist_head *head;
254 head = dquot_hash + hashfn(dquot->dq_sb, dquot->dq_id, dquot->dq_type);
255 hlist_add_head(&dquot->dq_hash, head);
258 static inline void remove_dquot_hash(struct dquot *dquot)
260 hlist_del_init(&dquot->dq_hash);
263 static struct dquot *find_dquot(unsigned int hashent, struct super_block *sb,
264 unsigned int id, int type)
266 struct hlist_node *node;
269 hlist_for_each (node, dquot_hash+hashent) {
270 dquot = hlist_entry(node, struct dquot, dq_hash);
271 if (dquot->dq_sb == sb && dquot->dq_id == id &&
272 dquot->dq_type == type)
278 /* Add a dquot to the tail of the free list */
279 static inline void put_dquot_last(struct dquot *dquot)
281 list_add_tail(&dquot->dq_free, &free_dquots);
282 dqstats_inc(DQST_FREE_DQUOTS);
285 static inline void remove_free_dquot(struct dquot *dquot)
287 if (list_empty(&dquot->dq_free))
289 list_del_init(&dquot->dq_free);
290 dqstats_dec(DQST_FREE_DQUOTS);
293 static inline void put_inuse(struct dquot *dquot)
295 /* We add to the back of inuse list so we don't have to restart
296 * when traversing this list and we block */
297 list_add_tail(&dquot->dq_inuse, &inuse_list);
298 dqstats_inc(DQST_ALLOC_DQUOTS);
301 static inline void remove_inuse(struct dquot *dquot)
303 dqstats_dec(DQST_ALLOC_DQUOTS);
304 list_del(&dquot->dq_inuse);
307 * End of list functions needing dq_list_lock
310 static void wait_on_dquot(struct dquot *dquot)
312 mutex_lock(&dquot->dq_lock);
313 mutex_unlock(&dquot->dq_lock);
316 static inline int dquot_dirty(struct dquot *dquot)
318 return test_bit(DQ_MOD_B, &dquot->dq_flags);
321 static inline int mark_dquot_dirty(struct dquot *dquot)
323 return dquot->dq_sb->dq_op->mark_dirty(dquot);
326 /* Mark dquot dirty in atomic manner, and return it's old dirty flag state */
327 int dquot_mark_dquot_dirty(struct dquot *dquot)
331 /* If quota is dirty already, we don't have to acquire dq_list_lock */
332 if (test_bit(DQ_MOD_B, &dquot->dq_flags))
335 spin_lock(&dq_list_lock);
336 if (!test_and_set_bit(DQ_MOD_B, &dquot->dq_flags)) {
337 list_add(&dquot->dq_dirty, &sb_dqopt(dquot->dq_sb)->
338 info[dquot->dq_type].dqi_dirty_list);
341 spin_unlock(&dq_list_lock);
344 EXPORT_SYMBOL(dquot_mark_dquot_dirty);
346 /* Dirtify all the dquots - this can block when journalling */
347 static inline int mark_all_dquot_dirty(struct dquot * const *dquot)
352 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
354 /* Even in case of error we have to continue */
355 ret = mark_dquot_dirty(dquot[cnt]);
362 static inline void dqput_all(struct dquot **dquot)
366 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
370 /* This function needs dq_list_lock */
371 static inline int clear_dquot_dirty(struct dquot *dquot)
373 if (!test_and_clear_bit(DQ_MOD_B, &dquot->dq_flags))
375 list_del_init(&dquot->dq_dirty);
379 void mark_info_dirty(struct super_block *sb, int type)
381 set_bit(DQF_INFO_DIRTY_B, &sb_dqopt(sb)->info[type].dqi_flags);
383 EXPORT_SYMBOL(mark_info_dirty);
386 * Read dquot from disk and alloc space for it
389 int dquot_acquire(struct dquot *dquot)
391 int ret = 0, ret2 = 0;
392 struct quota_info *dqopt = sb_dqopt(dquot->dq_sb);
394 mutex_lock(&dquot->dq_lock);
395 mutex_lock(&dqopt->dqio_mutex);
396 if (!test_bit(DQ_READ_B, &dquot->dq_flags))
397 ret = dqopt->ops[dquot->dq_type]->read_dqblk(dquot);
400 set_bit(DQ_READ_B, &dquot->dq_flags);
401 /* Instantiate dquot if needed */
402 if (!test_bit(DQ_ACTIVE_B, &dquot->dq_flags) && !dquot->dq_off) {
403 ret = dqopt->ops[dquot->dq_type]->commit_dqblk(dquot);
404 /* Write the info if needed */
405 if (info_dirty(&dqopt->info[dquot->dq_type])) {
406 ret2 = dqopt->ops[dquot->dq_type]->write_file_info(
407 dquot->dq_sb, dquot->dq_type);
416 set_bit(DQ_ACTIVE_B, &dquot->dq_flags);
418 mutex_unlock(&dqopt->dqio_mutex);
419 mutex_unlock(&dquot->dq_lock);
422 EXPORT_SYMBOL(dquot_acquire);
425 * Write dquot to disk
427 int dquot_commit(struct dquot *dquot)
429 int ret = 0, ret2 = 0;
430 struct quota_info *dqopt = sb_dqopt(dquot->dq_sb);
432 mutex_lock(&dqopt->dqio_mutex);
433 spin_lock(&dq_list_lock);
434 if (!clear_dquot_dirty(dquot)) {
435 spin_unlock(&dq_list_lock);
438 spin_unlock(&dq_list_lock);
439 /* Inactive dquot can be only if there was error during read/init
440 * => we have better not writing it */
441 if (test_bit(DQ_ACTIVE_B, &dquot->dq_flags)) {
442 ret = dqopt->ops[dquot->dq_type]->commit_dqblk(dquot);
443 if (info_dirty(&dqopt->info[dquot->dq_type])) {
444 ret2 = dqopt->ops[dquot->dq_type]->write_file_info(
445 dquot->dq_sb, dquot->dq_type);
451 mutex_unlock(&dqopt->dqio_mutex);
454 EXPORT_SYMBOL(dquot_commit);
459 int dquot_release(struct dquot *dquot)
461 int ret = 0, ret2 = 0;
462 struct quota_info *dqopt = sb_dqopt(dquot->dq_sb);
464 mutex_lock(&dquot->dq_lock);
465 /* Check whether we are not racing with some other dqget() */
466 if (atomic_read(&dquot->dq_count) > 1)
468 mutex_lock(&dqopt->dqio_mutex);
469 if (dqopt->ops[dquot->dq_type]->release_dqblk) {
470 ret = dqopt->ops[dquot->dq_type]->release_dqblk(dquot);
472 if (info_dirty(&dqopt->info[dquot->dq_type])) {
473 ret2 = dqopt->ops[dquot->dq_type]->write_file_info(
474 dquot->dq_sb, dquot->dq_type);
479 clear_bit(DQ_ACTIVE_B, &dquot->dq_flags);
480 mutex_unlock(&dqopt->dqio_mutex);
482 mutex_unlock(&dquot->dq_lock);
485 EXPORT_SYMBOL(dquot_release);
487 void dquot_destroy(struct dquot *dquot)
489 kmem_cache_free(dquot_cachep, dquot);
491 EXPORT_SYMBOL(dquot_destroy);
493 static inline void do_destroy_dquot(struct dquot *dquot)
495 dquot->dq_sb->dq_op->destroy_dquot(dquot);
498 /* Invalidate all dquots on the list. Note that this function is called after
499 * quota is disabled and pointers from inodes removed so there cannot be new
500 * quota users. There can still be some users of quotas due to inodes being
501 * just deleted or pruned by prune_icache() (those are not attached to any
502 * list) or parallel quotactl call. We have to wait for such users.
504 static void invalidate_dquots(struct super_block *sb, int type)
506 struct dquot *dquot, *tmp;
509 spin_lock(&dq_list_lock);
510 list_for_each_entry_safe(dquot, tmp, &inuse_list, dq_inuse) {
511 if (dquot->dq_sb != sb)
513 if (dquot->dq_type != type)
515 /* Wait for dquot users */
516 if (atomic_read(&dquot->dq_count)) {
519 atomic_inc(&dquot->dq_count);
520 prepare_to_wait(&dquot->dq_wait_unused, &wait,
521 TASK_UNINTERRUPTIBLE);
522 spin_unlock(&dq_list_lock);
523 /* Once dqput() wakes us up, we know it's time to free
525 * IMPORTANT: we rely on the fact that there is always
526 * at most one process waiting for dquot to free.
527 * Otherwise dq_count would be > 1 and we would never
530 if (atomic_read(&dquot->dq_count) > 1)
532 finish_wait(&dquot->dq_wait_unused, &wait);
534 /* At this moment dquot() need not exist (it could be
535 * reclaimed by prune_dqcache(). Hence we must
540 * Quota now has no users and it has been written on last
543 remove_dquot_hash(dquot);
544 remove_free_dquot(dquot);
546 do_destroy_dquot(dquot);
548 spin_unlock(&dq_list_lock);
551 /* Call callback for every active dquot on given filesystem */
552 int dquot_scan_active(struct super_block *sb,
553 int (*fn)(struct dquot *dquot, unsigned long priv),
556 struct dquot *dquot, *old_dquot = NULL;
559 mutex_lock(&sb_dqopt(sb)->dqonoff_mutex);
560 spin_lock(&dq_list_lock);
561 list_for_each_entry(dquot, &inuse_list, dq_inuse) {
562 if (!test_bit(DQ_ACTIVE_B, &dquot->dq_flags))
564 if (dquot->dq_sb != sb)
566 /* Now we have active dquot so we can just increase use count */
567 atomic_inc(&dquot->dq_count);
568 spin_unlock(&dq_list_lock);
569 dqstats_inc(DQST_LOOKUPS);
572 ret = fn(dquot, priv);
575 spin_lock(&dq_list_lock);
576 /* We are safe to continue now because our dquot could not
577 * be moved out of the inuse list while we hold the reference */
579 spin_unlock(&dq_list_lock);
582 mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
585 EXPORT_SYMBOL(dquot_scan_active);
587 int vfs_quota_sync(struct super_block *sb, int type, int wait)
589 struct list_head *dirty;
591 struct quota_info *dqopt = sb_dqopt(sb);
594 mutex_lock(&dqopt->dqonoff_mutex);
595 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
596 if (type != -1 && cnt != type)
598 if (!sb_has_quota_active(sb, cnt))
600 spin_lock(&dq_list_lock);
601 dirty = &dqopt->info[cnt].dqi_dirty_list;
602 while (!list_empty(dirty)) {
603 dquot = list_first_entry(dirty, struct dquot,
605 /* Dirty and inactive can be only bad dquot... */
606 if (!test_bit(DQ_ACTIVE_B, &dquot->dq_flags)) {
607 clear_dquot_dirty(dquot);
610 /* Now we have active dquot from which someone is
611 * holding reference so we can safely just increase
613 atomic_inc(&dquot->dq_count);
614 spin_unlock(&dq_list_lock);
615 dqstats_inc(DQST_LOOKUPS);
616 sb->dq_op->write_dquot(dquot);
618 spin_lock(&dq_list_lock);
620 spin_unlock(&dq_list_lock);
623 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
624 if ((cnt == type || type == -1) && sb_has_quota_active(sb, cnt)
625 && info_dirty(&dqopt->info[cnt]))
626 sb->dq_op->write_info(sb, cnt);
627 dqstats_inc(DQST_SYNCS);
628 mutex_unlock(&dqopt->dqonoff_mutex);
630 if (!wait || (sb_dqopt(sb)->flags & DQUOT_QUOTA_SYS_FILE))
633 /* This is not very clever (and fast) but currently I don't know about
634 * any other simple way of getting quota data to disk and we must get
635 * them there for userspace to be visible... */
636 if (sb->s_op->sync_fs)
637 sb->s_op->sync_fs(sb, 1);
638 sync_blockdev(sb->s_bdev);
641 * Now when everything is written we can discard the pagecache so
642 * that userspace sees the changes.
644 mutex_lock(&sb_dqopt(sb)->dqonoff_mutex);
645 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
646 if (type != -1 && cnt != type)
648 if (!sb_has_quota_active(sb, cnt))
650 mutex_lock_nested(&sb_dqopt(sb)->files[cnt]->i_mutex,
652 truncate_inode_pages(&sb_dqopt(sb)->files[cnt]->i_data, 0);
653 mutex_unlock(&sb_dqopt(sb)->files[cnt]->i_mutex);
655 mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
659 EXPORT_SYMBOL(vfs_quota_sync);
661 /* Free unused dquots from cache */
662 static void prune_dqcache(int count)
664 struct list_head *head;
667 head = free_dquots.prev;
668 while (head != &free_dquots && count) {
669 dquot = list_entry(head, struct dquot, dq_free);
670 remove_dquot_hash(dquot);
671 remove_free_dquot(dquot);
673 do_destroy_dquot(dquot);
675 head = free_dquots.prev;
679 static int dqstats_read(unsigned int type)
684 for_each_possible_cpu(cpu)
685 count += per_cpu_ptr(dqstats_pcpu, cpu)->stat[type];
686 /* Statistics reading is racy, but absolute accuracy isn't required */
690 count = dqstats.stat[type];
696 * This is called from kswapd when we think we need some
700 static int shrink_dqcache_memory(int nr, gfp_t gfp_mask)
703 spin_lock(&dq_list_lock);
705 spin_unlock(&dq_list_lock);
707 return (dqstats_read(DQST_FREE_DQUOTS)/100) * sysctl_vfs_cache_pressure;
710 static struct shrinker dqcache_shrinker = {
711 .shrink = shrink_dqcache_memory,
712 .seeks = DEFAULT_SEEKS,
716 * Put reference to dquot
717 * NOTE: If you change this function please check whether dqput_blocks() works right...
719 void dqput(struct dquot *dquot)
725 #ifdef CONFIG_QUOTA_DEBUG
726 if (!atomic_read(&dquot->dq_count)) {
727 printk("VFS: dqput: trying to free free dquot\n");
728 printk("VFS: device %s, dquot of %s %d\n",
730 quotatypes[dquot->dq_type],
735 dqstats_inc(DQST_DROPS);
737 spin_lock(&dq_list_lock);
738 if (atomic_read(&dquot->dq_count) > 1) {
739 /* We have more than one user... nothing to do */
740 atomic_dec(&dquot->dq_count);
741 /* Releasing dquot during quotaoff phase? */
742 if (!sb_has_quota_active(dquot->dq_sb, dquot->dq_type) &&
743 atomic_read(&dquot->dq_count) == 1)
744 wake_up(&dquot->dq_wait_unused);
745 spin_unlock(&dq_list_lock);
748 /* Need to release dquot? */
749 if (test_bit(DQ_ACTIVE_B, &dquot->dq_flags) && dquot_dirty(dquot)) {
750 spin_unlock(&dq_list_lock);
751 /* Commit dquot before releasing */
752 ret = dquot->dq_sb->dq_op->write_dquot(dquot);
754 printk(KERN_ERR "VFS: cannot write quota structure on "
755 "device %s (error %d). Quota may get out of "
756 "sync!\n", dquot->dq_sb->s_id, ret);
758 * We clear dirty bit anyway, so that we avoid
761 spin_lock(&dq_list_lock);
762 clear_dquot_dirty(dquot);
763 spin_unlock(&dq_list_lock);
767 /* Clear flag in case dquot was inactive (something bad happened) */
768 clear_dquot_dirty(dquot);
769 if (test_bit(DQ_ACTIVE_B, &dquot->dq_flags)) {
770 spin_unlock(&dq_list_lock);
771 dquot->dq_sb->dq_op->release_dquot(dquot);
774 atomic_dec(&dquot->dq_count);
775 #ifdef CONFIG_QUOTA_DEBUG
777 BUG_ON(!list_empty(&dquot->dq_free));
779 put_dquot_last(dquot);
780 spin_unlock(&dq_list_lock);
782 EXPORT_SYMBOL(dqput);
784 struct dquot *dquot_alloc(struct super_block *sb, int type)
786 return kmem_cache_zalloc(dquot_cachep, GFP_NOFS);
788 EXPORT_SYMBOL(dquot_alloc);
790 static struct dquot *get_empty_dquot(struct super_block *sb, int type)
794 dquot = sb->dq_op->alloc_dquot(sb, type);
798 mutex_init(&dquot->dq_lock);
799 INIT_LIST_HEAD(&dquot->dq_free);
800 INIT_LIST_HEAD(&dquot->dq_inuse);
801 INIT_HLIST_NODE(&dquot->dq_hash);
802 INIT_LIST_HEAD(&dquot->dq_dirty);
803 init_waitqueue_head(&dquot->dq_wait_unused);
805 dquot->dq_type = type;
806 atomic_set(&dquot->dq_count, 1);
812 * Get reference to dquot
814 * Locking is slightly tricky here. We are guarded from parallel quotaoff()
815 * destroying our dquot by:
816 * a) checking for quota flags under dq_list_lock and
817 * b) getting a reference to dquot before we release dq_list_lock
819 struct dquot *dqget(struct super_block *sb, unsigned int id, int type)
821 unsigned int hashent = hashfn(sb, id, type);
822 struct dquot *dquot = NULL, *empty = NULL;
824 if (!sb_has_quota_active(sb, type))
827 spin_lock(&dq_list_lock);
828 spin_lock(&dq_state_lock);
829 if (!sb_has_quota_active(sb, type)) {
830 spin_unlock(&dq_state_lock);
831 spin_unlock(&dq_list_lock);
834 spin_unlock(&dq_state_lock);
836 dquot = find_dquot(hashent, sb, id, type);
839 spin_unlock(&dq_list_lock);
840 empty = get_empty_dquot(sb, type);
842 schedule(); /* Try to wait for a moment... */
848 /* all dquots go on the inuse_list */
850 /* hash it first so it can be found */
851 insert_dquot_hash(dquot);
852 spin_unlock(&dq_list_lock);
853 dqstats_inc(DQST_LOOKUPS);
855 if (!atomic_read(&dquot->dq_count))
856 remove_free_dquot(dquot);
857 atomic_inc(&dquot->dq_count);
858 spin_unlock(&dq_list_lock);
859 dqstats_inc(DQST_CACHE_HITS);
860 dqstats_inc(DQST_LOOKUPS);
862 /* Wait for dq_lock - after this we know that either dquot_release() is
863 * already finished or it will be canceled due to dq_count > 1 test */
864 wait_on_dquot(dquot);
865 /* Read the dquot / allocate space in quota file */
866 if (!test_bit(DQ_ACTIVE_B, &dquot->dq_flags) &&
867 sb->dq_op->acquire_dquot(dquot) < 0) {
872 #ifdef CONFIG_QUOTA_DEBUG
873 BUG_ON(!dquot->dq_sb); /* Has somebody invalidated entry under us? */
877 do_destroy_dquot(empty);
881 EXPORT_SYMBOL(dqget);
883 static int dqinit_needed(struct inode *inode, int type)
887 if (IS_NOQUOTA(inode))
890 return !inode->i_dquot[type];
891 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
892 if (!inode->i_dquot[cnt])
897 /* This routine is guarded by dqonoff_mutex mutex */
898 static void add_dquot_ref(struct super_block *sb, int type)
900 struct inode *inode, *old_inode = NULL;
901 #ifdef CONFIG_QUOTA_DEBUG
905 spin_lock(&inode_lock);
906 list_for_each_entry(inode, &sb->s_inodes, i_sb_list) {
907 if (inode->i_state & (I_FREEING|I_CLEAR|I_WILL_FREE|I_NEW))
909 #ifdef CONFIG_QUOTA_DEBUG
910 if (unlikely(inode_get_rsv_space(inode) > 0))
913 if (!atomic_read(&inode->i_writecount))
915 if (!dqinit_needed(inode, type))
919 spin_unlock(&inode_lock);
922 __dquot_initialize(inode, type);
923 /* We hold a reference to 'inode' so it couldn't have been
924 * removed from s_inodes list while we dropped the inode_lock.
925 * We cannot iput the inode now as we can be holding the last
926 * reference and we cannot iput it under inode_lock. So we
927 * keep the reference and iput it later. */
929 spin_lock(&inode_lock);
931 spin_unlock(&inode_lock);
934 #ifdef CONFIG_QUOTA_DEBUG
936 printk(KERN_WARNING "VFS (%s): Writes happened before quota"
937 " was turned on thus quota information is probably "
938 "inconsistent. Please run quotacheck(8).\n", sb->s_id);
944 * Return 0 if dqput() won't block.
945 * (note that 1 doesn't necessarily mean blocking)
947 static inline int dqput_blocks(struct dquot *dquot)
949 if (atomic_read(&dquot->dq_count) <= 1)
955 * Remove references to dquots from inode and add dquot to list for freeing
956 * if we have the last referece to dquot
957 * We can't race with anybody because we hold dqptr_sem for writing...
959 static int remove_inode_dquot_ref(struct inode *inode, int type,
960 struct list_head *tofree_head)
962 struct dquot *dquot = inode->i_dquot[type];
964 inode->i_dquot[type] = NULL;
966 if (dqput_blocks(dquot)) {
967 #ifdef CONFIG_QUOTA_DEBUG
968 if (atomic_read(&dquot->dq_count) != 1)
969 printk(KERN_WARNING "VFS: Adding dquot with dq_count %d to dispose list.\n", atomic_read(&dquot->dq_count));
971 spin_lock(&dq_list_lock);
972 /* As dquot must have currently users it can't be on
973 * the free list... */
974 list_add(&dquot->dq_free, tofree_head);
975 spin_unlock(&dq_list_lock);
979 dqput(dquot); /* We have guaranteed we won't block */
985 * Free list of dquots
986 * Dquots are removed from inodes and no new references can be got so we are
987 * the only ones holding reference
989 static void put_dquot_list(struct list_head *tofree_head)
991 struct list_head *act_head;
994 act_head = tofree_head->next;
995 while (act_head != tofree_head) {
996 dquot = list_entry(act_head, struct dquot, dq_free);
997 act_head = act_head->next;
998 /* Remove dquot from the list so we won't have problems... */
999 list_del_init(&dquot->dq_free);
1004 static void remove_dquot_ref(struct super_block *sb, int type,
1005 struct list_head *tofree_head)
1007 struct inode *inode;
1009 spin_lock(&inode_lock);
1010 list_for_each_entry(inode, &sb->s_inodes, i_sb_list) {
1012 * We have to scan also I_NEW inodes because they can already
1013 * have quota pointer initialized. Luckily, we need to touch
1014 * only quota pointers and these have separate locking
1017 if (!IS_NOQUOTA(inode))
1018 remove_inode_dquot_ref(inode, type, tofree_head);
1020 spin_unlock(&inode_lock);
1023 /* Gather all references from inodes and drop them */
1024 static void drop_dquot_ref(struct super_block *sb, int type)
1026 LIST_HEAD(tofree_head);
1029 down_write(&sb_dqopt(sb)->dqptr_sem);
1030 remove_dquot_ref(sb, type, &tofree_head);
1031 up_write(&sb_dqopt(sb)->dqptr_sem);
1032 put_dquot_list(&tofree_head);
1036 static inline void dquot_incr_inodes(struct dquot *dquot, qsize_t number)
1038 dquot->dq_dqb.dqb_curinodes += number;
1041 static inline void dquot_incr_space(struct dquot *dquot, qsize_t number)
1043 dquot->dq_dqb.dqb_curspace += number;
1046 static inline void dquot_resv_space(struct dquot *dquot, qsize_t number)
1048 dquot->dq_dqb.dqb_rsvspace += number;
1052 * Claim reserved quota space
1054 static void dquot_claim_reserved_space(struct dquot *dquot, qsize_t number)
1056 if (dquot->dq_dqb.dqb_rsvspace < number) {
1058 number = dquot->dq_dqb.dqb_rsvspace;
1060 dquot->dq_dqb.dqb_curspace += number;
1061 dquot->dq_dqb.dqb_rsvspace -= number;
1065 void dquot_free_reserved_space(struct dquot *dquot, qsize_t number)
1067 if (dquot->dq_dqb.dqb_rsvspace >= number)
1068 dquot->dq_dqb.dqb_rsvspace -= number;
1071 dquot->dq_dqb.dqb_rsvspace = 0;
1075 static void dquot_decr_inodes(struct dquot *dquot, qsize_t number)
1077 if (sb_dqopt(dquot->dq_sb)->flags & DQUOT_NEGATIVE_USAGE ||
1078 dquot->dq_dqb.dqb_curinodes >= number)
1079 dquot->dq_dqb.dqb_curinodes -= number;
1081 dquot->dq_dqb.dqb_curinodes = 0;
1082 if (dquot->dq_dqb.dqb_curinodes <= dquot->dq_dqb.dqb_isoftlimit)
1083 dquot->dq_dqb.dqb_itime = (time_t) 0;
1084 clear_bit(DQ_INODES_B, &dquot->dq_flags);
1087 static void dquot_decr_space(struct dquot *dquot, qsize_t number)
1089 if (sb_dqopt(dquot->dq_sb)->flags & DQUOT_NEGATIVE_USAGE ||
1090 dquot->dq_dqb.dqb_curspace >= number)
1091 dquot->dq_dqb.dqb_curspace -= number;
1093 dquot->dq_dqb.dqb_curspace = 0;
1094 if (dquot->dq_dqb.dqb_curspace <= dquot->dq_dqb.dqb_bsoftlimit)
1095 dquot->dq_dqb.dqb_btime = (time_t) 0;
1096 clear_bit(DQ_BLKS_B, &dquot->dq_flags);
1099 static int warning_issued(struct dquot *dquot, const int warntype)
1101 int flag = (warntype == QUOTA_NL_BHARDWARN ||
1102 warntype == QUOTA_NL_BSOFTLONGWARN) ? DQ_BLKS_B :
1103 ((warntype == QUOTA_NL_IHARDWARN ||
1104 warntype == QUOTA_NL_ISOFTLONGWARN) ? DQ_INODES_B : 0);
1108 return test_and_set_bit(flag, &dquot->dq_flags);
1111 #ifdef CONFIG_PRINT_QUOTA_WARNING
1112 static int flag_print_warnings = 1;
1114 static int need_print_warning(struct dquot *dquot)
1116 if (!flag_print_warnings)
1119 switch (dquot->dq_type) {
1121 return current_fsuid() == dquot->dq_id;
1123 return in_group_p(dquot->dq_id);
1128 /* Print warning to user which exceeded quota */
1129 static void print_warning(struct dquot *dquot, const int warntype)
1132 struct tty_struct *tty;
1134 if (warntype == QUOTA_NL_IHARDBELOW ||
1135 warntype == QUOTA_NL_ISOFTBELOW ||
1136 warntype == QUOTA_NL_BHARDBELOW ||
1137 warntype == QUOTA_NL_BSOFTBELOW || !need_print_warning(dquot))
1140 tty = get_current_tty();
1143 tty_write_message(tty, dquot->dq_sb->s_id);
1144 if (warntype == QUOTA_NL_ISOFTWARN || warntype == QUOTA_NL_BSOFTWARN)
1145 tty_write_message(tty, ": warning, ");
1147 tty_write_message(tty, ": write failed, ");
1148 tty_write_message(tty, quotatypes[dquot->dq_type]);
1150 case QUOTA_NL_IHARDWARN:
1151 msg = " file limit reached.\r\n";
1153 case QUOTA_NL_ISOFTLONGWARN:
1154 msg = " file quota exceeded too long.\r\n";
1156 case QUOTA_NL_ISOFTWARN:
1157 msg = " file quota exceeded.\r\n";
1159 case QUOTA_NL_BHARDWARN:
1160 msg = " block limit reached.\r\n";
1162 case QUOTA_NL_BSOFTLONGWARN:
1163 msg = " block quota exceeded too long.\r\n";
1165 case QUOTA_NL_BSOFTWARN:
1166 msg = " block quota exceeded.\r\n";
1169 tty_write_message(tty, msg);
1175 * Write warnings to the console and send warning messages over netlink.
1177 * Note that this function can sleep.
1179 static void flush_warnings(struct dquot *const *dquots, char *warntype)
1184 for (i = 0; i < MAXQUOTAS; i++) {
1186 if (dq && warntype[i] != QUOTA_NL_NOWARN &&
1187 !warning_issued(dq, warntype[i])) {
1188 #ifdef CONFIG_PRINT_QUOTA_WARNING
1189 print_warning(dq, warntype[i]);
1191 quota_send_warning(dq->dq_type, dq->dq_id,
1192 dq->dq_sb->s_dev, warntype[i]);
1197 static int ignore_hardlimit(struct dquot *dquot)
1199 struct mem_dqinfo *info = &sb_dqopt(dquot->dq_sb)->info[dquot->dq_type];
1201 return capable(CAP_SYS_RESOURCE) &&
1202 (info->dqi_format->qf_fmt_id != QFMT_VFS_OLD ||
1203 !(info->dqi_flags & V1_DQF_RSQUASH));
1206 /* needs dq_data_lock */
1207 static int check_idq(struct dquot *dquot, qsize_t inodes, char *warntype)
1209 qsize_t newinodes = dquot->dq_dqb.dqb_curinodes + inodes;
1211 *warntype = QUOTA_NL_NOWARN;
1212 if (!sb_has_quota_limits_enabled(dquot->dq_sb, dquot->dq_type) ||
1213 test_bit(DQ_FAKE_B, &dquot->dq_flags))
1216 if (dquot->dq_dqb.dqb_ihardlimit &&
1217 newinodes > dquot->dq_dqb.dqb_ihardlimit &&
1218 !ignore_hardlimit(dquot)) {
1219 *warntype = QUOTA_NL_IHARDWARN;
1223 if (dquot->dq_dqb.dqb_isoftlimit &&
1224 newinodes > dquot->dq_dqb.dqb_isoftlimit &&
1225 dquot->dq_dqb.dqb_itime &&
1226 get_seconds() >= dquot->dq_dqb.dqb_itime &&
1227 !ignore_hardlimit(dquot)) {
1228 *warntype = QUOTA_NL_ISOFTLONGWARN;
1232 if (dquot->dq_dqb.dqb_isoftlimit &&
1233 newinodes > dquot->dq_dqb.dqb_isoftlimit &&
1234 dquot->dq_dqb.dqb_itime == 0) {
1235 *warntype = QUOTA_NL_ISOFTWARN;
1236 dquot->dq_dqb.dqb_itime = get_seconds() +
1237 sb_dqopt(dquot->dq_sb)->info[dquot->dq_type].dqi_igrace;
1243 /* needs dq_data_lock */
1244 static int check_bdq(struct dquot *dquot, qsize_t space, int prealloc, char *warntype)
1247 struct super_block *sb = dquot->dq_sb;
1249 *warntype = QUOTA_NL_NOWARN;
1250 if (!sb_has_quota_limits_enabled(sb, dquot->dq_type) ||
1251 test_bit(DQ_FAKE_B, &dquot->dq_flags))
1254 tspace = dquot->dq_dqb.dqb_curspace + dquot->dq_dqb.dqb_rsvspace
1257 if (dquot->dq_dqb.dqb_bhardlimit &&
1258 tspace > dquot->dq_dqb.dqb_bhardlimit &&
1259 !ignore_hardlimit(dquot)) {
1261 *warntype = QUOTA_NL_BHARDWARN;
1265 if (dquot->dq_dqb.dqb_bsoftlimit &&
1266 tspace > dquot->dq_dqb.dqb_bsoftlimit &&
1267 dquot->dq_dqb.dqb_btime &&
1268 get_seconds() >= dquot->dq_dqb.dqb_btime &&
1269 !ignore_hardlimit(dquot)) {
1271 *warntype = QUOTA_NL_BSOFTLONGWARN;
1275 if (dquot->dq_dqb.dqb_bsoftlimit &&
1276 tspace > dquot->dq_dqb.dqb_bsoftlimit &&
1277 dquot->dq_dqb.dqb_btime == 0) {
1279 *warntype = QUOTA_NL_BSOFTWARN;
1280 dquot->dq_dqb.dqb_btime = get_seconds() +
1281 sb_dqopt(sb)->info[dquot->dq_type].dqi_bgrace;
1285 * We don't allow preallocation to exceed softlimit so exceeding will
1294 static int info_idq_free(struct dquot *dquot, qsize_t inodes)
1298 if (test_bit(DQ_FAKE_B, &dquot->dq_flags) ||
1299 dquot->dq_dqb.dqb_curinodes <= dquot->dq_dqb.dqb_isoftlimit ||
1300 !sb_has_quota_limits_enabled(dquot->dq_sb, dquot->dq_type))
1301 return QUOTA_NL_NOWARN;
1303 newinodes = dquot->dq_dqb.dqb_curinodes - inodes;
1304 if (newinodes <= dquot->dq_dqb.dqb_isoftlimit)
1305 return QUOTA_NL_ISOFTBELOW;
1306 if (dquot->dq_dqb.dqb_curinodes >= dquot->dq_dqb.dqb_ihardlimit &&
1307 newinodes < dquot->dq_dqb.dqb_ihardlimit)
1308 return QUOTA_NL_IHARDBELOW;
1309 return QUOTA_NL_NOWARN;
1312 static int info_bdq_free(struct dquot *dquot, qsize_t space)
1314 if (test_bit(DQ_FAKE_B, &dquot->dq_flags) ||
1315 dquot->dq_dqb.dqb_curspace <= dquot->dq_dqb.dqb_bsoftlimit)
1316 return QUOTA_NL_NOWARN;
1318 if (dquot->dq_dqb.dqb_curspace - space <= dquot->dq_dqb.dqb_bsoftlimit)
1319 return QUOTA_NL_BSOFTBELOW;
1320 if (dquot->dq_dqb.dqb_curspace >= dquot->dq_dqb.dqb_bhardlimit &&
1321 dquot->dq_dqb.dqb_curspace - space < dquot->dq_dqb.dqb_bhardlimit)
1322 return QUOTA_NL_BHARDBELOW;
1323 return QUOTA_NL_NOWARN;
1327 * Initialize quota pointers in inode
1329 * We do things in a bit complicated way but by that we avoid calling
1330 * dqget() and thus filesystem callbacks under dqptr_sem.
1332 * It is better to call this function outside of any transaction as it
1333 * might need a lot of space in journal for dquot structure allocation.
1335 static void __dquot_initialize(struct inode *inode, int type)
1337 unsigned int id = 0;
1339 struct dquot *got[MAXQUOTAS];
1340 struct super_block *sb = inode->i_sb;
1343 /* First test before acquiring mutex - solves deadlocks when we
1344 * re-enter the quota code and are already holding the mutex */
1345 if (!sb_any_quota_active(inode->i_sb) || IS_NOQUOTA(inode))
1348 /* First get references to structures we might need. */
1349 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1351 if (type != -1 && cnt != type)
1361 got[cnt] = dqget(sb, id, cnt);
1364 down_write(&sb_dqopt(sb)->dqptr_sem);
1365 if (IS_NOQUOTA(inode))
1367 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1368 if (type != -1 && cnt != type)
1370 /* Avoid races with quotaoff() */
1371 if (!sb_has_quota_active(sb, cnt))
1373 if (!inode->i_dquot[cnt]) {
1374 inode->i_dquot[cnt] = got[cnt];
1377 * Make quota reservation system happy if someone
1378 * did a write before quota was turned on
1380 rsv = inode_get_rsv_space(inode);
1382 dquot_resv_space(inode->i_dquot[cnt], rsv);
1386 up_write(&sb_dqopt(sb)->dqptr_sem);
1387 /* Drop unused references */
1391 void dquot_initialize(struct inode *inode)
1393 __dquot_initialize(inode, -1);
1395 EXPORT_SYMBOL(dquot_initialize);
1398 * Release all quotas referenced by inode
1400 static void __dquot_drop(struct inode *inode)
1403 struct dquot *put[MAXQUOTAS];
1405 down_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
1406 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1407 put[cnt] = inode->i_dquot[cnt];
1408 inode->i_dquot[cnt] = NULL;
1410 up_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
1414 void dquot_drop(struct inode *inode)
1418 if (IS_NOQUOTA(inode))
1422 * Test before calling to rule out calls from proc and such
1423 * where we are not allowed to block. Note that this is
1424 * actually reliable test even without the lock - the caller
1425 * must assure that nobody can come after the DQUOT_DROP and
1426 * add quota pointers back anyway.
1428 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1429 if (inode->i_dquot[cnt])
1433 if (cnt < MAXQUOTAS)
1434 __dquot_drop(inode);
1436 EXPORT_SYMBOL(dquot_drop);
1439 * inode_reserved_space is managed internally by quota, and protected by
1440 * i_lock similar to i_blocks+i_bytes.
1442 static qsize_t *inode_reserved_space(struct inode * inode)
1444 /* Filesystem must explicitly define it's own method in order to use
1445 * quota reservation interface */
1446 BUG_ON(!inode->i_sb->dq_op->get_reserved_space);
1447 return inode->i_sb->dq_op->get_reserved_space(inode);
1450 void inode_add_rsv_space(struct inode *inode, qsize_t number)
1452 spin_lock(&inode->i_lock);
1453 *inode_reserved_space(inode) += number;
1454 spin_unlock(&inode->i_lock);
1456 EXPORT_SYMBOL(inode_add_rsv_space);
1458 void inode_claim_rsv_space(struct inode *inode, qsize_t number)
1460 spin_lock(&inode->i_lock);
1461 *inode_reserved_space(inode) -= number;
1462 __inode_add_bytes(inode, number);
1463 spin_unlock(&inode->i_lock);
1465 EXPORT_SYMBOL(inode_claim_rsv_space);
1467 void inode_sub_rsv_space(struct inode *inode, qsize_t number)
1469 spin_lock(&inode->i_lock);
1470 *inode_reserved_space(inode) -= number;
1471 spin_unlock(&inode->i_lock);
1473 EXPORT_SYMBOL(inode_sub_rsv_space);
1475 static qsize_t inode_get_rsv_space(struct inode *inode)
1479 if (!inode->i_sb->dq_op->get_reserved_space)
1481 spin_lock(&inode->i_lock);
1482 ret = *inode_reserved_space(inode);
1483 spin_unlock(&inode->i_lock);
1487 static void inode_incr_space(struct inode *inode, qsize_t number,
1491 inode_add_rsv_space(inode, number);
1493 inode_add_bytes(inode, number);
1496 static void inode_decr_space(struct inode *inode, qsize_t number, int reserve)
1499 inode_sub_rsv_space(inode, number);
1501 inode_sub_bytes(inode, number);
1505 * This functions updates i_blocks+i_bytes fields and quota information
1506 * (together with appropriate checks).
1508 * NOTE: We absolutely rely on the fact that caller dirties the inode
1509 * (usually helpers in quotaops.h care about this) and holds a handle for
1510 * the current transaction so that dquot write and inode write go into the
1515 * This operation can block, but only after everything is updated
1517 int __dquot_alloc_space(struct inode *inode, qsize_t number, int flags)
1520 char warntype[MAXQUOTAS];
1521 int warn = flags & DQUOT_SPACE_WARN;
1522 int reserve = flags & DQUOT_SPACE_RESERVE;
1523 int nofail = flags & DQUOT_SPACE_NOFAIL;
1526 * First test before acquiring mutex - solves deadlocks when we
1527 * re-enter the quota code and are already holding the mutex
1529 if (!sb_any_quota_active(inode->i_sb) || IS_NOQUOTA(inode)) {
1530 inode_incr_space(inode, number, reserve);
1534 down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1535 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1536 warntype[cnt] = QUOTA_NL_NOWARN;
1538 spin_lock(&dq_data_lock);
1539 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1540 if (!inode->i_dquot[cnt])
1542 ret = check_bdq(inode->i_dquot[cnt], number, !warn,
1544 if (ret && !nofail) {
1545 spin_unlock(&dq_data_lock);
1546 goto out_flush_warn;
1549 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1550 if (!inode->i_dquot[cnt])
1553 dquot_resv_space(inode->i_dquot[cnt], number);
1555 dquot_incr_space(inode->i_dquot[cnt], number);
1557 inode_incr_space(inode, number, reserve);
1558 spin_unlock(&dq_data_lock);
1561 goto out_flush_warn;
1562 mark_all_dquot_dirty(inode->i_dquot);
1564 flush_warnings(inode->i_dquot, warntype);
1565 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1569 EXPORT_SYMBOL(__dquot_alloc_space);
1572 * This operation can block, but only after everything is updated
1574 int dquot_alloc_inode(const struct inode *inode)
1577 char warntype[MAXQUOTAS];
1579 /* First test before acquiring mutex - solves deadlocks when we
1580 * re-enter the quota code and are already holding the mutex */
1581 if (!sb_any_quota_active(inode->i_sb) || IS_NOQUOTA(inode))
1583 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1584 warntype[cnt] = QUOTA_NL_NOWARN;
1585 down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1586 spin_lock(&dq_data_lock);
1587 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1588 if (!inode->i_dquot[cnt])
1590 ret = check_idq(inode->i_dquot[cnt], 1, warntype + cnt);
1595 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1596 if (!inode->i_dquot[cnt])
1598 dquot_incr_inodes(inode->i_dquot[cnt], 1);
1602 spin_unlock(&dq_data_lock);
1604 mark_all_dquot_dirty(inode->i_dquot);
1605 flush_warnings(inode->i_dquot, warntype);
1606 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1609 EXPORT_SYMBOL(dquot_alloc_inode);
1612 * Convert in-memory reserved quotas to real consumed quotas
1614 int dquot_claim_space_nodirty(struct inode *inode, qsize_t number)
1618 if (!sb_any_quota_active(inode->i_sb) || IS_NOQUOTA(inode)) {
1619 inode_claim_rsv_space(inode, number);
1623 down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1624 spin_lock(&dq_data_lock);
1625 /* Claim reserved quotas to allocated quotas */
1626 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1627 if (inode->i_dquot[cnt])
1628 dquot_claim_reserved_space(inode->i_dquot[cnt],
1631 /* Update inode bytes */
1632 inode_claim_rsv_space(inode, number);
1633 spin_unlock(&dq_data_lock);
1634 mark_all_dquot_dirty(inode->i_dquot);
1635 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1638 EXPORT_SYMBOL(dquot_claim_space_nodirty);
1641 * This operation can block, but only after everything is updated
1643 void __dquot_free_space(struct inode *inode, qsize_t number, int flags)
1646 char warntype[MAXQUOTAS];
1647 int reserve = flags & DQUOT_SPACE_RESERVE;
1649 /* First test before acquiring mutex - solves deadlocks when we
1650 * re-enter the quota code and are already holding the mutex */
1651 if (!sb_any_quota_active(inode->i_sb) || IS_NOQUOTA(inode)) {
1652 inode_decr_space(inode, number, reserve);
1656 down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1657 spin_lock(&dq_data_lock);
1658 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1659 if (!inode->i_dquot[cnt])
1661 warntype[cnt] = info_bdq_free(inode->i_dquot[cnt], number);
1663 dquot_free_reserved_space(inode->i_dquot[cnt], number);
1665 dquot_decr_space(inode->i_dquot[cnt], number);
1667 inode_decr_space(inode, number, reserve);
1668 spin_unlock(&dq_data_lock);
1672 mark_all_dquot_dirty(inode->i_dquot);
1674 flush_warnings(inode->i_dquot, warntype);
1675 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1677 EXPORT_SYMBOL(__dquot_free_space);
1680 * This operation can block, but only after everything is updated
1682 void dquot_free_inode(const struct inode *inode)
1685 char warntype[MAXQUOTAS];
1687 /* First test before acquiring mutex - solves deadlocks when we
1688 * re-enter the quota code and are already holding the mutex */
1689 if (!sb_any_quota_active(inode->i_sb) || IS_NOQUOTA(inode))
1692 down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1693 spin_lock(&dq_data_lock);
1694 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1695 if (!inode->i_dquot[cnt])
1697 warntype[cnt] = info_idq_free(inode->i_dquot[cnt], 1);
1698 dquot_decr_inodes(inode->i_dquot[cnt], 1);
1700 spin_unlock(&dq_data_lock);
1701 mark_all_dquot_dirty(inode->i_dquot);
1702 flush_warnings(inode->i_dquot, warntype);
1703 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1705 EXPORT_SYMBOL(dquot_free_inode);
1708 * Transfer the number of inode and blocks from one diskquota to an other.
1709 * On success, dquot references in transfer_to are consumed and references
1710 * to original dquots that need to be released are placed there. On failure,
1711 * references are kept untouched.
1713 * This operation can block, but only after everything is updated
1714 * A transaction must be started when entering this function.
1717 int __dquot_transfer(struct inode *inode, struct dquot **transfer_to)
1719 qsize_t space, cur_space;
1720 qsize_t rsv_space = 0;
1721 struct dquot *transfer_from[MAXQUOTAS] = {};
1723 char warntype_to[MAXQUOTAS];
1724 char warntype_from_inodes[MAXQUOTAS], warntype_from_space[MAXQUOTAS];
1726 /* First test before acquiring mutex - solves deadlocks when we
1727 * re-enter the quota code and are already holding the mutex */
1728 if (IS_NOQUOTA(inode))
1730 /* Initialize the arrays */
1731 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1732 warntype_to[cnt] = QUOTA_NL_NOWARN;
1733 down_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
1734 if (IS_NOQUOTA(inode)) { /* File without quota accounting? */
1735 up_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
1738 spin_lock(&dq_data_lock);
1739 cur_space = inode_get_bytes(inode);
1740 rsv_space = inode_get_rsv_space(inode);
1741 space = cur_space + rsv_space;
1742 /* Build the transfer_from list and check the limits */
1743 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1744 if (!transfer_to[cnt])
1746 transfer_from[cnt] = inode->i_dquot[cnt];
1747 ret = check_idq(transfer_to[cnt], 1, warntype_to + cnt);
1750 ret = check_bdq(transfer_to[cnt], space, 0, warntype_to + cnt);
1756 * Finally perform the needed transfer from transfer_from to transfer_to
1758 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1760 * Skip changes for same uid or gid or for turned off quota-type.
1762 if (!transfer_to[cnt])
1765 /* Due to IO error we might not have transfer_from[] structure */
1766 if (transfer_from[cnt]) {
1767 warntype_from_inodes[cnt] =
1768 info_idq_free(transfer_from[cnt], 1);
1769 warntype_from_space[cnt] =
1770 info_bdq_free(transfer_from[cnt], space);
1771 dquot_decr_inodes(transfer_from[cnt], 1);
1772 dquot_decr_space(transfer_from[cnt], cur_space);
1773 dquot_free_reserved_space(transfer_from[cnt],
1777 dquot_incr_inodes(transfer_to[cnt], 1);
1778 dquot_incr_space(transfer_to[cnt], cur_space);
1779 dquot_resv_space(transfer_to[cnt], rsv_space);
1781 inode->i_dquot[cnt] = transfer_to[cnt];
1783 spin_unlock(&dq_data_lock);
1784 up_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
1786 mark_all_dquot_dirty(transfer_from);
1787 mark_all_dquot_dirty(transfer_to);
1788 /* Pass back references to put */
1789 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1790 transfer_to[cnt] = transfer_from[cnt];
1792 flush_warnings(transfer_to, warntype_to);
1793 flush_warnings(transfer_from, warntype_from_inodes);
1794 flush_warnings(transfer_from, warntype_from_space);
1797 spin_unlock(&dq_data_lock);
1798 up_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
1801 EXPORT_SYMBOL(__dquot_transfer);
1803 /* Wrapper for transferring ownership of an inode for uid/gid only
1804 * Called from FSXXX_setattr()
1806 int dquot_transfer(struct inode *inode, struct iattr *iattr)
1808 struct dquot *transfer_to[MAXQUOTAS] = {};
1809 struct super_block *sb = inode->i_sb;
1812 if (!sb_any_quota_active(sb) || IS_NOQUOTA(inode))
1815 if (iattr->ia_valid & ATTR_UID && iattr->ia_uid != inode->i_uid)
1816 transfer_to[USRQUOTA] = dqget(sb, iattr->ia_uid, USRQUOTA);
1817 if (iattr->ia_valid & ATTR_GID && iattr->ia_gid != inode->i_gid)
1818 transfer_to[GRPQUOTA] = dqget(sb, iattr->ia_uid, GRPQUOTA);
1820 ret = __dquot_transfer(inode, transfer_to);
1821 dqput_all(transfer_to);
1824 EXPORT_SYMBOL(dquot_transfer);
1827 * Write info of quota file to disk
1829 int dquot_commit_info(struct super_block *sb, int type)
1832 struct quota_info *dqopt = sb_dqopt(sb);
1834 mutex_lock(&dqopt->dqio_mutex);
1835 ret = dqopt->ops[type]->write_file_info(sb, type);
1836 mutex_unlock(&dqopt->dqio_mutex);
1839 EXPORT_SYMBOL(dquot_commit_info);
1842 * Definitions of diskquota operations.
1844 const struct dquot_operations dquot_operations = {
1845 .write_dquot = dquot_commit,
1846 .acquire_dquot = dquot_acquire,
1847 .release_dquot = dquot_release,
1848 .mark_dirty = dquot_mark_dquot_dirty,
1849 .write_info = dquot_commit_info,
1850 .alloc_dquot = dquot_alloc,
1851 .destroy_dquot = dquot_destroy,
1855 * Generic helper for ->open on filesystems supporting disk quotas.
1857 int dquot_file_open(struct inode *inode, struct file *file)
1861 error = generic_file_open(inode, file);
1862 if (!error && (file->f_mode & FMODE_WRITE))
1863 dquot_initialize(inode);
1866 EXPORT_SYMBOL(dquot_file_open);
1869 * Turn quota off on a device. type == -1 ==> quotaoff for all types (umount)
1871 int vfs_quota_disable(struct super_block *sb, int type, unsigned int flags)
1874 struct quota_info *dqopt = sb_dqopt(sb);
1875 struct inode *toputinode[MAXQUOTAS];
1877 /* Cannot turn off usage accounting without turning off limits, or
1878 * suspend quotas and simultaneously turn quotas off. */
1879 if ((flags & DQUOT_USAGE_ENABLED && !(flags & DQUOT_LIMITS_ENABLED))
1880 || (flags & DQUOT_SUSPENDED && flags & (DQUOT_LIMITS_ENABLED |
1881 DQUOT_USAGE_ENABLED)))
1884 /* We need to serialize quota_off() for device */
1885 mutex_lock(&dqopt->dqonoff_mutex);
1888 * Skip everything if there's nothing to do. We have to do this because
1889 * sometimes we are called when fill_super() failed and calling
1890 * sync_fs() in such cases does no good.
1892 if (!sb_any_quota_loaded(sb)) {
1893 mutex_unlock(&dqopt->dqonoff_mutex);
1896 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1897 toputinode[cnt] = NULL;
1898 if (type != -1 && cnt != type)
1900 if (!sb_has_quota_loaded(sb, cnt))
1903 if (flags & DQUOT_SUSPENDED) {
1904 spin_lock(&dq_state_lock);
1906 dquot_state_flag(DQUOT_SUSPENDED, cnt);
1907 spin_unlock(&dq_state_lock);
1909 spin_lock(&dq_state_lock);
1910 dqopt->flags &= ~dquot_state_flag(flags, cnt);
1911 /* Turning off suspended quotas? */
1912 if (!sb_has_quota_loaded(sb, cnt) &&
1913 sb_has_quota_suspended(sb, cnt)) {
1914 dqopt->flags &= ~dquot_state_flag(
1915 DQUOT_SUSPENDED, cnt);
1916 spin_unlock(&dq_state_lock);
1917 iput(dqopt->files[cnt]);
1918 dqopt->files[cnt] = NULL;
1921 spin_unlock(&dq_state_lock);
1924 /* We still have to keep quota loaded? */
1925 if (sb_has_quota_loaded(sb, cnt) && !(flags & DQUOT_SUSPENDED))
1928 /* Note: these are blocking operations */
1929 drop_dquot_ref(sb, cnt);
1930 invalidate_dquots(sb, cnt);
1932 * Now all dquots should be invalidated, all writes done so we
1933 * should be only users of the info. No locks needed.
1935 if (info_dirty(&dqopt->info[cnt]))
1936 sb->dq_op->write_info(sb, cnt);
1937 if (dqopt->ops[cnt]->free_file_info)
1938 dqopt->ops[cnt]->free_file_info(sb, cnt);
1939 put_quota_format(dqopt->info[cnt].dqi_format);
1941 toputinode[cnt] = dqopt->files[cnt];
1942 if (!sb_has_quota_loaded(sb, cnt))
1943 dqopt->files[cnt] = NULL;
1944 dqopt->info[cnt].dqi_flags = 0;
1945 dqopt->info[cnt].dqi_igrace = 0;
1946 dqopt->info[cnt].dqi_bgrace = 0;
1947 dqopt->ops[cnt] = NULL;
1949 mutex_unlock(&dqopt->dqonoff_mutex);
1951 /* Skip syncing and setting flags if quota files are hidden */
1952 if (dqopt->flags & DQUOT_QUOTA_SYS_FILE)
1955 /* Sync the superblock so that buffers with quota data are written to
1956 * disk (and so userspace sees correct data afterwards). */
1957 if (sb->s_op->sync_fs)
1958 sb->s_op->sync_fs(sb, 1);
1959 sync_blockdev(sb->s_bdev);
1960 /* Now the quota files are just ordinary files and we can set the
1961 * inode flags back. Moreover we discard the pagecache so that
1962 * userspace sees the writes we did bypassing the pagecache. We
1963 * must also discard the blockdev buffers so that we see the
1964 * changes done by userspace on the next quotaon() */
1965 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1966 if (toputinode[cnt]) {
1967 mutex_lock(&dqopt->dqonoff_mutex);
1968 /* If quota was reenabled in the meantime, we have
1970 if (!sb_has_quota_loaded(sb, cnt)) {
1971 mutex_lock_nested(&toputinode[cnt]->i_mutex,
1973 toputinode[cnt]->i_flags &= ~(S_IMMUTABLE |
1974 S_NOATIME | S_NOQUOTA);
1975 truncate_inode_pages(&toputinode[cnt]->i_data,
1977 mutex_unlock(&toputinode[cnt]->i_mutex);
1978 mark_inode_dirty(toputinode[cnt]);
1980 mutex_unlock(&dqopt->dqonoff_mutex);
1983 invalidate_bdev(sb->s_bdev);
1985 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1986 if (toputinode[cnt]) {
1987 /* On remount RO, we keep the inode pointer so that we
1988 * can reenable quota on the subsequent remount RW. We
1989 * have to check 'flags' variable and not use sb_has_
1990 * function because another quotaon / quotaoff could
1991 * change global state before we got here. We refuse
1992 * to suspend quotas when there is pending delete on
1993 * the quota file... */
1994 if (!(flags & DQUOT_SUSPENDED))
1995 iput(toputinode[cnt]);
1996 else if (!toputinode[cnt]->i_nlink)
2001 EXPORT_SYMBOL(vfs_quota_disable);
2003 int vfs_quota_off(struct super_block *sb, int type, int remount)
2005 return vfs_quota_disable(sb, type, remount ? DQUOT_SUSPENDED :
2006 (DQUOT_USAGE_ENABLED | DQUOT_LIMITS_ENABLED));
2008 EXPORT_SYMBOL(vfs_quota_off);
2010 * Turn quotas on on a device
2014 * Helper function to turn quotas on when we already have the inode of
2015 * quota file and no quota information is loaded.
2017 static int vfs_load_quota_inode(struct inode *inode, int type, int format_id,
2020 struct quota_format_type *fmt = find_quota_format(format_id);
2021 struct super_block *sb = inode->i_sb;
2022 struct quota_info *dqopt = sb_dqopt(sb);
2028 if (!S_ISREG(inode->i_mode)) {
2032 if (IS_RDONLY(inode)) {
2036 if (!sb->s_op->quota_write || !sb->s_op->quota_read) {
2040 /* Usage always has to be set... */
2041 if (!(flags & DQUOT_USAGE_ENABLED)) {
2046 if (!(dqopt->flags & DQUOT_QUOTA_SYS_FILE)) {
2047 /* As we bypass the pagecache we must now flush all the
2048 * dirty data and invalidate caches so that kernel sees
2049 * changes from userspace. It is not enough to just flush
2050 * the quota file since if blocksize < pagesize, invalidation
2051 * of the cache could fail because of other unrelated dirty
2053 sync_filesystem(sb);
2054 invalidate_bdev(sb->s_bdev);
2056 mutex_lock(&dqopt->dqonoff_mutex);
2057 if (sb_has_quota_loaded(sb, type)) {
2062 if (!(dqopt->flags & DQUOT_QUOTA_SYS_FILE)) {
2063 /* We don't want quota and atime on quota files (deadlocks
2064 * possible) Also nobody should write to the file - we use
2065 * special IO operations which ignore the immutable bit. */
2066 mutex_lock_nested(&inode->i_mutex, I_MUTEX_QUOTA);
2067 oldflags = inode->i_flags & (S_NOATIME | S_IMMUTABLE |
2069 inode->i_flags |= S_NOQUOTA | S_NOATIME | S_IMMUTABLE;
2070 mutex_unlock(&inode->i_mutex);
2072 * When S_NOQUOTA is set, remove dquot references as no more
2073 * references can be added
2075 __dquot_drop(inode);
2079 dqopt->files[type] = igrab(inode);
2080 if (!dqopt->files[type])
2083 if (!fmt->qf_ops->check_quota_file(sb, type))
2086 dqopt->ops[type] = fmt->qf_ops;
2087 dqopt->info[type].dqi_format = fmt;
2088 dqopt->info[type].dqi_fmt_id = format_id;
2089 INIT_LIST_HEAD(&dqopt->info[type].dqi_dirty_list);
2090 mutex_lock(&dqopt->dqio_mutex);
2091 error = dqopt->ops[type]->read_file_info(sb, type);
2093 mutex_unlock(&dqopt->dqio_mutex);
2096 mutex_unlock(&dqopt->dqio_mutex);
2097 spin_lock(&dq_state_lock);
2098 dqopt->flags |= dquot_state_flag(flags, type);
2099 spin_unlock(&dq_state_lock);
2101 add_dquot_ref(sb, type);
2102 mutex_unlock(&dqopt->dqonoff_mutex);
2107 dqopt->files[type] = NULL;
2110 if (oldflags != -1) {
2111 mutex_lock_nested(&inode->i_mutex, I_MUTEX_QUOTA);
2112 /* Set the flags back (in the case of accidental quotaon()
2113 * on a wrong file we don't want to mess up the flags) */
2114 inode->i_flags &= ~(S_NOATIME | S_NOQUOTA | S_IMMUTABLE);
2115 inode->i_flags |= oldflags;
2116 mutex_unlock(&inode->i_mutex);
2118 mutex_unlock(&dqopt->dqonoff_mutex);
2120 put_quota_format(fmt);
2125 /* Reenable quotas on remount RW */
2126 static int vfs_quota_on_remount(struct super_block *sb, int type)
2128 struct quota_info *dqopt = sb_dqopt(sb);
2129 struct inode *inode;
2133 mutex_lock(&dqopt->dqonoff_mutex);
2134 if (!sb_has_quota_suspended(sb, type)) {
2135 mutex_unlock(&dqopt->dqonoff_mutex);
2138 inode = dqopt->files[type];
2139 dqopt->files[type] = NULL;
2140 spin_lock(&dq_state_lock);
2141 flags = dqopt->flags & dquot_state_flag(DQUOT_USAGE_ENABLED |
2142 DQUOT_LIMITS_ENABLED, type);
2143 dqopt->flags &= ~dquot_state_flag(DQUOT_STATE_FLAGS, type);
2144 spin_unlock(&dq_state_lock);
2145 mutex_unlock(&dqopt->dqonoff_mutex);
2147 flags = dquot_generic_flag(flags, type);
2148 ret = vfs_load_quota_inode(inode, type, dqopt->info[type].dqi_fmt_id,
2155 int vfs_quota_on_path(struct super_block *sb, int type, int format_id,
2158 int error = security_quota_on(path->dentry);
2161 /* Quota file not on the same filesystem? */
2162 if (path->mnt->mnt_sb != sb)
2165 error = vfs_load_quota_inode(path->dentry->d_inode, type,
2166 format_id, DQUOT_USAGE_ENABLED |
2167 DQUOT_LIMITS_ENABLED);
2170 EXPORT_SYMBOL(vfs_quota_on_path);
2172 int vfs_quota_on(struct super_block *sb, int type, int format_id, char *name,
2179 return vfs_quota_on_remount(sb, type);
2181 error = kern_path(name, LOOKUP_FOLLOW, &path);
2183 error = vfs_quota_on_path(sb, type, format_id, &path);
2188 EXPORT_SYMBOL(vfs_quota_on);
2191 * More powerful function for turning on quotas allowing setting
2192 * of individual quota flags
2194 int vfs_quota_enable(struct inode *inode, int type, int format_id,
2198 struct super_block *sb = inode->i_sb;
2199 struct quota_info *dqopt = sb_dqopt(sb);
2201 /* Just unsuspend quotas? */
2202 if (flags & DQUOT_SUSPENDED)
2203 return vfs_quota_on_remount(sb, type);
2206 /* Just updating flags needed? */
2207 if (sb_has_quota_loaded(sb, type)) {
2208 mutex_lock(&dqopt->dqonoff_mutex);
2209 /* Now do a reliable test... */
2210 if (!sb_has_quota_loaded(sb, type)) {
2211 mutex_unlock(&dqopt->dqonoff_mutex);
2214 if (flags & DQUOT_USAGE_ENABLED &&
2215 sb_has_quota_usage_enabled(sb, type)) {
2219 if (flags & DQUOT_LIMITS_ENABLED &&
2220 sb_has_quota_limits_enabled(sb, type)) {
2224 spin_lock(&dq_state_lock);
2225 sb_dqopt(sb)->flags |= dquot_state_flag(flags, type);
2226 spin_unlock(&dq_state_lock);
2228 mutex_unlock(&dqopt->dqonoff_mutex);
2233 return vfs_load_quota_inode(inode, type, format_id, flags);
2235 EXPORT_SYMBOL(vfs_quota_enable);
2238 * This function is used when filesystem needs to initialize quotas
2239 * during mount time.
2241 int vfs_quota_on_mount(struct super_block *sb, char *qf_name,
2242 int format_id, int type)
2244 struct dentry *dentry;
2247 mutex_lock(&sb->s_root->d_inode->i_mutex);
2248 dentry = lookup_one_len(qf_name, sb->s_root, strlen(qf_name));
2249 mutex_unlock(&sb->s_root->d_inode->i_mutex);
2251 return PTR_ERR(dentry);
2253 if (!dentry->d_inode) {
2258 error = security_quota_on(dentry);
2260 error = vfs_load_quota_inode(dentry->d_inode, type, format_id,
2261 DQUOT_USAGE_ENABLED | DQUOT_LIMITS_ENABLED);
2267 EXPORT_SYMBOL(vfs_quota_on_mount);
2269 /* Wrapper to turn on quotas when remounting rw */
2270 int vfs_dq_quota_on_remount(struct super_block *sb)
2275 if (!sb->s_qcop || !sb->s_qcop->quota_on)
2277 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
2278 err = sb->s_qcop->quota_on(sb, cnt, 0, NULL, 1);
2279 if (err < 0 && !ret)
2284 EXPORT_SYMBOL(vfs_dq_quota_on_remount);
2286 static inline qsize_t qbtos(qsize_t blocks)
2288 return blocks << QIF_DQBLKSIZE_BITS;
2291 static inline qsize_t stoqb(qsize_t space)
2293 return (space + QIF_DQBLKSIZE - 1) >> QIF_DQBLKSIZE_BITS;
2296 /* Generic routine for getting common part of quota structure */
2297 static void do_get_dqblk(struct dquot *dquot, struct fs_disk_quota *di)
2299 struct mem_dqblk *dm = &dquot->dq_dqb;
2301 memset(di, 0, sizeof(*di));
2302 di->d_version = FS_DQUOT_VERSION;
2303 di->d_flags = dquot->dq_type == USRQUOTA ?
2304 XFS_USER_QUOTA : XFS_GROUP_QUOTA;
2305 di->d_id = dquot->dq_id;
2307 spin_lock(&dq_data_lock);
2308 di->d_blk_hardlimit = stoqb(dm->dqb_bhardlimit);
2309 di->d_blk_softlimit = stoqb(dm->dqb_bsoftlimit);
2310 di->d_ino_hardlimit = dm->dqb_ihardlimit;
2311 di->d_ino_softlimit = dm->dqb_isoftlimit;
2312 di->d_bcount = dm->dqb_curspace + dm->dqb_rsvspace;
2313 di->d_icount = dm->dqb_curinodes;
2314 di->d_btimer = dm->dqb_btime;
2315 di->d_itimer = dm->dqb_itime;
2316 spin_unlock(&dq_data_lock);
2319 int vfs_get_dqblk(struct super_block *sb, int type, qid_t id,
2320 struct fs_disk_quota *di)
2322 struct dquot *dquot;
2324 dquot = dqget(sb, id, type);
2327 do_get_dqblk(dquot, di);
2332 EXPORT_SYMBOL(vfs_get_dqblk);
2334 #define VFS_FS_DQ_MASK \
2335 (FS_DQ_BCOUNT | FS_DQ_BSOFT | FS_DQ_BHARD | \
2336 FS_DQ_ICOUNT | FS_DQ_ISOFT | FS_DQ_IHARD | \
2337 FS_DQ_BTIMER | FS_DQ_ITIMER)
2339 /* Generic routine for setting common part of quota structure */
2340 static int do_set_dqblk(struct dquot *dquot, struct fs_disk_quota *di)
2342 struct mem_dqblk *dm = &dquot->dq_dqb;
2343 int check_blim = 0, check_ilim = 0;
2344 struct mem_dqinfo *dqi = &sb_dqopt(dquot->dq_sb)->info[dquot->dq_type];
2346 if (di->d_fieldmask & ~VFS_FS_DQ_MASK)
2349 if (((di->d_fieldmask & FS_DQ_BSOFT) &&
2350 (di->d_blk_softlimit > dqi->dqi_maxblimit)) ||
2351 ((di->d_fieldmask & FS_DQ_BHARD) &&
2352 (di->d_blk_hardlimit > dqi->dqi_maxblimit)) ||
2353 ((di->d_fieldmask & FS_DQ_ISOFT) &&
2354 (di->d_ino_softlimit > dqi->dqi_maxilimit)) ||
2355 ((di->d_fieldmask & FS_DQ_IHARD) &&
2356 (di->d_ino_hardlimit > dqi->dqi_maxilimit)))
2359 spin_lock(&dq_data_lock);
2360 if (di->d_fieldmask & FS_DQ_BCOUNT) {
2361 dm->dqb_curspace = di->d_bcount - dm->dqb_rsvspace;
2363 set_bit(DQ_LASTSET_B + QIF_SPACE_B, &dquot->dq_flags);
2366 if (di->d_fieldmask & FS_DQ_BSOFT)
2367 dm->dqb_bsoftlimit = qbtos(di->d_blk_softlimit);
2368 if (di->d_fieldmask & FS_DQ_BHARD)
2369 dm->dqb_bhardlimit = qbtos(di->d_blk_hardlimit);
2370 if (di->d_fieldmask & (FS_DQ_BSOFT | FS_DQ_BHARD)) {
2372 set_bit(DQ_LASTSET_B + QIF_BLIMITS_B, &dquot->dq_flags);
2375 if (di->d_fieldmask & FS_DQ_ICOUNT) {
2376 dm->dqb_curinodes = di->d_icount;
2378 set_bit(DQ_LASTSET_B + QIF_INODES_B, &dquot->dq_flags);
2381 if (di->d_fieldmask & FS_DQ_ISOFT)
2382 dm->dqb_isoftlimit = di->d_ino_softlimit;
2383 if (di->d_fieldmask & FS_DQ_IHARD)
2384 dm->dqb_ihardlimit = di->d_ino_hardlimit;
2385 if (di->d_fieldmask & (FS_DQ_ISOFT | FS_DQ_IHARD)) {
2387 set_bit(DQ_LASTSET_B + QIF_ILIMITS_B, &dquot->dq_flags);
2390 if (di->d_fieldmask & FS_DQ_BTIMER) {
2391 dm->dqb_btime = di->d_btimer;
2393 set_bit(DQ_LASTSET_B + QIF_BTIME_B, &dquot->dq_flags);
2396 if (di->d_fieldmask & FS_DQ_ITIMER) {
2397 dm->dqb_itime = di->d_itimer;
2399 set_bit(DQ_LASTSET_B + QIF_ITIME_B, &dquot->dq_flags);
2403 if (!dm->dqb_bsoftlimit ||
2404 dm->dqb_curspace < dm->dqb_bsoftlimit) {
2406 clear_bit(DQ_BLKS_B, &dquot->dq_flags);
2407 } else if (!(di->d_fieldmask & FS_DQ_BTIMER))
2408 /* Set grace only if user hasn't provided his own... */
2409 dm->dqb_btime = get_seconds() + dqi->dqi_bgrace;
2412 if (!dm->dqb_isoftlimit ||
2413 dm->dqb_curinodes < dm->dqb_isoftlimit) {
2415 clear_bit(DQ_INODES_B, &dquot->dq_flags);
2416 } else if (!(di->d_fieldmask & FS_DQ_ITIMER))
2417 /* Set grace only if user hasn't provided his own... */
2418 dm->dqb_itime = get_seconds() + dqi->dqi_igrace;
2420 if (dm->dqb_bhardlimit || dm->dqb_bsoftlimit || dm->dqb_ihardlimit ||
2422 clear_bit(DQ_FAKE_B, &dquot->dq_flags);
2424 set_bit(DQ_FAKE_B, &dquot->dq_flags);
2425 spin_unlock(&dq_data_lock);
2426 mark_dquot_dirty(dquot);
2431 int vfs_set_dqblk(struct super_block *sb, int type, qid_t id,
2432 struct fs_disk_quota *di)
2434 struct dquot *dquot;
2437 dquot = dqget(sb, id, type);
2442 rc = do_set_dqblk(dquot, di);
2447 EXPORT_SYMBOL(vfs_set_dqblk);
2449 /* Generic routine for getting common part of quota file information */
2450 int vfs_get_dqinfo(struct super_block *sb, int type, struct if_dqinfo *ii)
2452 struct mem_dqinfo *mi;
2454 mutex_lock(&sb_dqopt(sb)->dqonoff_mutex);
2455 if (!sb_has_quota_active(sb, type)) {
2456 mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
2459 mi = sb_dqopt(sb)->info + type;
2460 spin_lock(&dq_data_lock);
2461 ii->dqi_bgrace = mi->dqi_bgrace;
2462 ii->dqi_igrace = mi->dqi_igrace;
2463 ii->dqi_flags = mi->dqi_flags & DQF_MASK;
2464 ii->dqi_valid = IIF_ALL;
2465 spin_unlock(&dq_data_lock);
2466 mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
2469 EXPORT_SYMBOL(vfs_get_dqinfo);
2471 /* Generic routine for setting common part of quota file information */
2472 int vfs_set_dqinfo(struct super_block *sb, int type, struct if_dqinfo *ii)
2474 struct mem_dqinfo *mi;
2477 mutex_lock(&sb_dqopt(sb)->dqonoff_mutex);
2478 if (!sb_has_quota_active(sb, type)) {
2482 mi = sb_dqopt(sb)->info + type;
2483 spin_lock(&dq_data_lock);
2484 if (ii->dqi_valid & IIF_BGRACE)
2485 mi->dqi_bgrace = ii->dqi_bgrace;
2486 if (ii->dqi_valid & IIF_IGRACE)
2487 mi->dqi_igrace = ii->dqi_igrace;
2488 if (ii->dqi_valid & IIF_FLAGS)
2489 mi->dqi_flags = (mi->dqi_flags & ~DQF_MASK) |
2490 (ii->dqi_flags & DQF_MASK);
2491 spin_unlock(&dq_data_lock);
2492 mark_info_dirty(sb, type);
2493 /* Force write to disk */
2494 sb->dq_op->write_info(sb, type);
2496 mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
2499 EXPORT_SYMBOL(vfs_set_dqinfo);
2501 const struct quotactl_ops vfs_quotactl_ops = {
2502 .quota_on = vfs_quota_on,
2503 .quota_off = vfs_quota_off,
2504 .quota_sync = vfs_quota_sync,
2505 .get_info = vfs_get_dqinfo,
2506 .set_info = vfs_set_dqinfo,
2507 .get_dqblk = vfs_get_dqblk,
2508 .set_dqblk = vfs_set_dqblk
2512 static int do_proc_dqstats(struct ctl_table *table, int write,
2513 void __user *buffer, size_t *lenp, loff_t *ppos)
2516 /* Update global table */
2517 unsigned int type = (int *)table->data - dqstats.stat;
2518 dqstats.stat[type] = dqstats_read(type);
2520 return proc_dointvec(table, write, buffer, lenp, ppos);
2523 static ctl_table fs_dqstats_table[] = {
2525 .procname = "lookups",
2526 .data = &dqstats.stat[DQST_LOOKUPS],
2527 .maxlen = sizeof(int),
2529 .proc_handler = do_proc_dqstats,
2532 .procname = "drops",
2533 .data = &dqstats.stat[DQST_DROPS],
2534 .maxlen = sizeof(int),
2536 .proc_handler = do_proc_dqstats,
2539 .procname = "reads",
2540 .data = &dqstats.stat[DQST_READS],
2541 .maxlen = sizeof(int),
2543 .proc_handler = do_proc_dqstats,
2546 .procname = "writes",
2547 .data = &dqstats.stat[DQST_WRITES],
2548 .maxlen = sizeof(int),
2550 .proc_handler = do_proc_dqstats,
2553 .procname = "cache_hits",
2554 .data = &dqstats.stat[DQST_CACHE_HITS],
2555 .maxlen = sizeof(int),
2557 .proc_handler = do_proc_dqstats,
2560 .procname = "allocated_dquots",
2561 .data = &dqstats.stat[DQST_ALLOC_DQUOTS],
2562 .maxlen = sizeof(int),
2564 .proc_handler = do_proc_dqstats,
2567 .procname = "free_dquots",
2568 .data = &dqstats.stat[DQST_FREE_DQUOTS],
2569 .maxlen = sizeof(int),
2571 .proc_handler = do_proc_dqstats,
2574 .procname = "syncs",
2575 .data = &dqstats.stat[DQST_SYNCS],
2576 .maxlen = sizeof(int),
2578 .proc_handler = do_proc_dqstats,
2580 #ifdef CONFIG_PRINT_QUOTA_WARNING
2582 .procname = "warnings",
2583 .data = &flag_print_warnings,
2584 .maxlen = sizeof(int),
2586 .proc_handler = proc_dointvec,
2592 static ctl_table fs_table[] = {
2594 .procname = "quota",
2596 .child = fs_dqstats_table,
2601 static ctl_table sys_table[] = {
2610 static int __init dquot_init(void)
2613 unsigned long nr_hash, order;
2615 printk(KERN_NOTICE "VFS: Disk quotas %s\n", __DQUOT_VERSION__);
2617 register_sysctl_table(sys_table);
2619 dquot_cachep = kmem_cache_create("dquot",
2620 sizeof(struct dquot), sizeof(unsigned long) * 4,
2621 (SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT|
2622 SLAB_MEM_SPREAD|SLAB_PANIC),
2626 dquot_hash = (struct hlist_head *)__get_free_pages(GFP_ATOMIC, order);
2628 panic("Cannot create dquot hash table");
2631 dqstats_pcpu = alloc_percpu(struct dqstats);
2633 panic("Cannot create dquot stats table");
2635 memset(&dqstats, 0, sizeof(struct dqstats));
2637 /* Find power-of-two hlist_heads which can fit into allocation */
2638 nr_hash = (1UL << order) * PAGE_SIZE / sizeof(struct hlist_head);
2642 } while (nr_hash >> dq_hash_bits);
2645 nr_hash = 1UL << dq_hash_bits;
2646 dq_hash_mask = nr_hash - 1;
2647 for (i = 0; i < nr_hash; i++)
2648 INIT_HLIST_HEAD(dquot_hash + i);
2650 printk("Dquot-cache hash table entries: %ld (order %ld, %ld bytes)\n",
2651 nr_hash, order, (PAGE_SIZE << order));
2653 register_shrinker(&dqcache_shrinker);
2657 module_init(dquot_init);