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>
83 #define __DQUOT_PARANOIA
86 * There are three quota SMP locks. dq_list_lock protects all lists with quotas
87 * and quota formats, dqstats structure containing statistics about the lists
88 * dq_data_lock protects data from dq_dqb and also mem_dqinfo structures and
89 * also guards consistency of dquot->dq_dqb with inode->i_blocks, i_bytes.
90 * i_blocks and i_bytes updates itself are guarded by i_lock acquired directly
91 * in inode_add_bytes() and inode_sub_bytes(). dq_state_lock protects
92 * modifications of quota state (on quotaon and quotaoff) and readers who care
93 * about latest values take it as well.
95 * The spinlock ordering is hence: dq_data_lock > dq_list_lock > i_lock,
96 * dq_list_lock > dq_state_lock
98 * Note that some things (eg. sb pointer, type, id) doesn't change during
99 * the life of the dquot structure and so needn't to be protected by a lock
101 * Any operation working on dquots via inode pointers must hold dqptr_sem. If
102 * operation is just reading pointers from inode (or not using them at all) the
103 * read lock is enough. If pointers are altered function must hold write lock.
104 * Special care needs to be taken about S_NOQUOTA inode flag (marking that
105 * inode is a quota file). Functions adding pointers from inode to dquots have
106 * to check this flag under dqptr_sem and then (if S_NOQUOTA is not set) they
107 * have to do all pointer modifications before dropping dqptr_sem. This makes
108 * sure they cannot race with quotaon which first sets S_NOQUOTA flag and
109 * then drops all pointers to dquots from an inode.
111 * Each dquot has its dq_lock mutex. Locked dquots might not be referenced
112 * from inodes (dquot_alloc_space() and such don't check the dq_lock).
113 * Currently dquot is locked only when it is being read to memory (or space for
114 * it is being allocated) on the first dqget() and when it is being released on
115 * the last dqput(). The allocation and release oparations are serialized by
116 * the dq_lock and by checking the use count in dquot_release(). Write
117 * operations on dquots don't hold dq_lock as they copy data under dq_data_lock
118 * spinlock to internal buffers before writing.
120 * Lock ordering (including related VFS locks) is the following:
121 * i_mutex > dqonoff_sem > journal_lock > dqptr_sem > dquot->dq_lock >
123 * The lock ordering of dqptr_sem imposed by quota code is only dqonoff_sem >
124 * dqptr_sem. But filesystem has to count with the fact that functions such as
125 * dquot_alloc_space() acquire dqptr_sem and they usually have to be called
126 * from inside a transaction to keep filesystem consistency after a crash. Also
127 * filesystems usually want to do some IO on dquot from ->mark_dirty which is
128 * called with dqptr_sem held.
129 * i_mutex on quota files is special (it's below dqio_mutex)
132 static __cacheline_aligned_in_smp DEFINE_SPINLOCK(dq_list_lock);
133 static __cacheline_aligned_in_smp DEFINE_SPINLOCK(dq_state_lock);
134 __cacheline_aligned_in_smp DEFINE_SPINLOCK(dq_data_lock);
135 EXPORT_SYMBOL(dq_data_lock);
137 static char *quotatypes[] = INITQFNAMES;
138 static struct quota_format_type *quota_formats; /* List of registered formats */
139 static struct quota_module_name module_names[] = INIT_QUOTA_MODULE_NAMES;
141 /* SLAB cache for dquot structures */
142 static struct kmem_cache *dquot_cachep;
144 int register_quota_format(struct quota_format_type *fmt)
146 spin_lock(&dq_list_lock);
147 fmt->qf_next = quota_formats;
149 spin_unlock(&dq_list_lock);
152 EXPORT_SYMBOL(register_quota_format);
154 void unregister_quota_format(struct quota_format_type *fmt)
156 struct quota_format_type **actqf;
158 spin_lock(&dq_list_lock);
159 for (actqf = "a_formats; *actqf && *actqf != fmt;
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 static inline unsigned int
233 hashfn(const struct super_block *sb, unsigned int id, int type)
237 tmp = (((unsigned long)sb>>L1_CACHE_SHIFT) ^ id) * (MAXQUOTAS - type);
238 return (tmp + (tmp >> dq_hash_bits)) & dq_hash_mask;
242 * Following list functions expect dq_list_lock to be held
244 static inline void insert_dquot_hash(struct dquot *dquot)
246 struct hlist_head *head;
247 head = dquot_hash + hashfn(dquot->dq_sb, dquot->dq_id, dquot->dq_type);
248 hlist_add_head(&dquot->dq_hash, head);
251 static inline void remove_dquot_hash(struct dquot *dquot)
253 hlist_del_init(&dquot->dq_hash);
256 static struct dquot *find_dquot(unsigned int hashent, struct super_block *sb,
257 unsigned int id, int type)
259 struct hlist_node *node;
262 hlist_for_each (node, dquot_hash+hashent) {
263 dquot = hlist_entry(node, struct dquot, dq_hash);
264 if (dquot->dq_sb == sb && dquot->dq_id == id &&
265 dquot->dq_type == type)
271 /* Add a dquot to the tail of the free list */
272 static inline void put_dquot_last(struct dquot *dquot)
274 list_add_tail(&dquot->dq_free, &free_dquots);
275 dqstats.free_dquots++;
278 static inline void remove_free_dquot(struct dquot *dquot)
280 if (list_empty(&dquot->dq_free))
282 list_del_init(&dquot->dq_free);
283 dqstats.free_dquots--;
286 static inline void put_inuse(struct dquot *dquot)
288 /* We add to the back of inuse list so we don't have to restart
289 * when traversing this list and we block */
290 list_add_tail(&dquot->dq_inuse, &inuse_list);
291 dqstats.allocated_dquots++;
294 static inline void remove_inuse(struct dquot *dquot)
296 dqstats.allocated_dquots--;
297 list_del(&dquot->dq_inuse);
300 * End of list functions needing dq_list_lock
303 static void wait_on_dquot(struct dquot *dquot)
305 mutex_lock(&dquot->dq_lock);
306 mutex_unlock(&dquot->dq_lock);
309 static inline int dquot_dirty(struct dquot *dquot)
311 return test_bit(DQ_MOD_B, &dquot->dq_flags);
314 static inline int mark_dquot_dirty(struct dquot *dquot)
316 return dquot->dq_sb->dq_op->mark_dirty(dquot);
319 int dquot_mark_dquot_dirty(struct dquot *dquot)
321 spin_lock(&dq_list_lock);
322 if (!test_and_set_bit(DQ_MOD_B, &dquot->dq_flags))
323 list_add(&dquot->dq_dirty, &sb_dqopt(dquot->dq_sb)->
324 info[dquot->dq_type].dqi_dirty_list);
325 spin_unlock(&dq_list_lock);
328 EXPORT_SYMBOL(dquot_mark_dquot_dirty);
330 /* Dirtify all the dquots - this can block when journalling */
331 static inline int mark_all_dquot_dirty(struct dquot * const *dquot)
336 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
338 /* Even in case of error we have to continue */
339 ret = mark_dquot_dirty(dquot[cnt]);
346 static inline void dqput_all(struct dquot **dquot)
350 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
354 /* This function needs dq_list_lock */
355 static inline int clear_dquot_dirty(struct dquot *dquot)
357 if (!test_and_clear_bit(DQ_MOD_B, &dquot->dq_flags))
359 list_del_init(&dquot->dq_dirty);
363 void mark_info_dirty(struct super_block *sb, int type)
365 set_bit(DQF_INFO_DIRTY_B, &sb_dqopt(sb)->info[type].dqi_flags);
367 EXPORT_SYMBOL(mark_info_dirty);
370 * Read dquot from disk and alloc space for it
373 int dquot_acquire(struct dquot *dquot)
375 int ret = 0, ret2 = 0;
376 struct quota_info *dqopt = sb_dqopt(dquot->dq_sb);
378 mutex_lock(&dquot->dq_lock);
379 mutex_lock(&dqopt->dqio_mutex);
380 if (!test_bit(DQ_READ_B, &dquot->dq_flags))
381 ret = dqopt->ops[dquot->dq_type]->read_dqblk(dquot);
384 set_bit(DQ_READ_B, &dquot->dq_flags);
385 /* Instantiate dquot if needed */
386 if (!test_bit(DQ_ACTIVE_B, &dquot->dq_flags) && !dquot->dq_off) {
387 ret = dqopt->ops[dquot->dq_type]->commit_dqblk(dquot);
388 /* Write the info if needed */
389 if (info_dirty(&dqopt->info[dquot->dq_type])) {
390 ret2 = dqopt->ops[dquot->dq_type]->write_file_info(
391 dquot->dq_sb, dquot->dq_type);
400 set_bit(DQ_ACTIVE_B, &dquot->dq_flags);
402 mutex_unlock(&dqopt->dqio_mutex);
403 mutex_unlock(&dquot->dq_lock);
406 EXPORT_SYMBOL(dquot_acquire);
409 * Write dquot to disk
411 int dquot_commit(struct dquot *dquot)
413 int ret = 0, ret2 = 0;
414 struct quota_info *dqopt = sb_dqopt(dquot->dq_sb);
416 mutex_lock(&dqopt->dqio_mutex);
417 spin_lock(&dq_list_lock);
418 if (!clear_dquot_dirty(dquot)) {
419 spin_unlock(&dq_list_lock);
422 spin_unlock(&dq_list_lock);
423 /* Inactive dquot can be only if there was error during read/init
424 * => we have better not writing it */
425 if (test_bit(DQ_ACTIVE_B, &dquot->dq_flags)) {
426 ret = dqopt->ops[dquot->dq_type]->commit_dqblk(dquot);
427 if (info_dirty(&dqopt->info[dquot->dq_type])) {
428 ret2 = dqopt->ops[dquot->dq_type]->write_file_info(
429 dquot->dq_sb, dquot->dq_type);
435 mutex_unlock(&dqopt->dqio_mutex);
438 EXPORT_SYMBOL(dquot_commit);
443 int dquot_release(struct dquot *dquot)
445 int ret = 0, ret2 = 0;
446 struct quota_info *dqopt = sb_dqopt(dquot->dq_sb);
448 mutex_lock(&dquot->dq_lock);
449 /* Check whether we are not racing with some other dqget() */
450 if (atomic_read(&dquot->dq_count) > 1)
452 mutex_lock(&dqopt->dqio_mutex);
453 if (dqopt->ops[dquot->dq_type]->release_dqblk) {
454 ret = dqopt->ops[dquot->dq_type]->release_dqblk(dquot);
456 if (info_dirty(&dqopt->info[dquot->dq_type])) {
457 ret2 = dqopt->ops[dquot->dq_type]->write_file_info(
458 dquot->dq_sb, dquot->dq_type);
463 clear_bit(DQ_ACTIVE_B, &dquot->dq_flags);
464 mutex_unlock(&dqopt->dqio_mutex);
466 mutex_unlock(&dquot->dq_lock);
469 EXPORT_SYMBOL(dquot_release);
471 void dquot_destroy(struct dquot *dquot)
473 kmem_cache_free(dquot_cachep, dquot);
475 EXPORT_SYMBOL(dquot_destroy);
477 static inline void do_destroy_dquot(struct dquot *dquot)
479 dquot->dq_sb->dq_op->destroy_dquot(dquot);
482 /* Invalidate all dquots on the list. Note that this function is called after
483 * quota is disabled and pointers from inodes removed so there cannot be new
484 * quota users. There can still be some users of quotas due to inodes being
485 * just deleted or pruned by prune_icache() (those are not attached to any
486 * list) or parallel quotactl call. We have to wait for such users.
488 static void invalidate_dquots(struct super_block *sb, int type)
490 struct dquot *dquot, *tmp;
493 spin_lock(&dq_list_lock);
494 list_for_each_entry_safe(dquot, tmp, &inuse_list, dq_inuse) {
495 if (dquot->dq_sb != sb)
497 if (dquot->dq_type != type)
499 /* Wait for dquot users */
500 if (atomic_read(&dquot->dq_count)) {
503 atomic_inc(&dquot->dq_count);
504 prepare_to_wait(&dquot->dq_wait_unused, &wait,
505 TASK_UNINTERRUPTIBLE);
506 spin_unlock(&dq_list_lock);
507 /* Once dqput() wakes us up, we know it's time to free
509 * IMPORTANT: we rely on the fact that there is always
510 * at most one process waiting for dquot to free.
511 * Otherwise dq_count would be > 1 and we would never
514 if (atomic_read(&dquot->dq_count) > 1)
516 finish_wait(&dquot->dq_wait_unused, &wait);
518 /* At this moment dquot() need not exist (it could be
519 * reclaimed by prune_dqcache(). Hence we must
524 * Quota now has no users and it has been written on last
527 remove_dquot_hash(dquot);
528 remove_free_dquot(dquot);
530 do_destroy_dquot(dquot);
532 spin_unlock(&dq_list_lock);
535 /* Call callback for every active dquot on given filesystem */
536 int dquot_scan_active(struct super_block *sb,
537 int (*fn)(struct dquot *dquot, unsigned long priv),
540 struct dquot *dquot, *old_dquot = NULL;
543 mutex_lock(&sb_dqopt(sb)->dqonoff_mutex);
544 spin_lock(&dq_list_lock);
545 list_for_each_entry(dquot, &inuse_list, dq_inuse) {
546 if (!test_bit(DQ_ACTIVE_B, &dquot->dq_flags))
548 if (dquot->dq_sb != sb)
550 /* Now we have active dquot so we can just increase use count */
551 atomic_inc(&dquot->dq_count);
553 spin_unlock(&dq_list_lock);
556 ret = fn(dquot, priv);
559 spin_lock(&dq_list_lock);
560 /* We are safe to continue now because our dquot could not
561 * be moved out of the inuse list while we hold the reference */
563 spin_unlock(&dq_list_lock);
566 mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
569 EXPORT_SYMBOL(dquot_scan_active);
571 int vfs_quota_sync(struct super_block *sb, int type)
573 struct list_head *dirty;
575 struct quota_info *dqopt = sb_dqopt(sb);
578 mutex_lock(&dqopt->dqonoff_mutex);
579 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
580 if (type != -1 && cnt != type)
582 if (!sb_has_quota_active(sb, cnt))
584 spin_lock(&dq_list_lock);
585 dirty = &dqopt->info[cnt].dqi_dirty_list;
586 while (!list_empty(dirty)) {
587 dquot = list_first_entry(dirty, struct dquot,
589 /* Dirty and inactive can be only bad dquot... */
590 if (!test_bit(DQ_ACTIVE_B, &dquot->dq_flags)) {
591 clear_dquot_dirty(dquot);
594 /* Now we have active dquot from which someone is
595 * holding reference so we can safely just increase
597 atomic_inc(&dquot->dq_count);
599 spin_unlock(&dq_list_lock);
600 sb->dq_op->write_dquot(dquot);
602 spin_lock(&dq_list_lock);
604 spin_unlock(&dq_list_lock);
607 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
608 if ((cnt == type || type == -1) && sb_has_quota_active(sb, cnt)
609 && info_dirty(&dqopt->info[cnt]))
610 sb->dq_op->write_info(sb, cnt);
611 spin_lock(&dq_list_lock);
613 spin_unlock(&dq_list_lock);
614 mutex_unlock(&dqopt->dqonoff_mutex);
618 EXPORT_SYMBOL(vfs_quota_sync);
620 /* Free unused dquots from cache */
621 static void prune_dqcache(int count)
623 struct list_head *head;
626 head = free_dquots.prev;
627 while (head != &free_dquots && count) {
628 dquot = list_entry(head, struct dquot, dq_free);
629 remove_dquot_hash(dquot);
630 remove_free_dquot(dquot);
632 do_destroy_dquot(dquot);
634 head = free_dquots.prev;
639 * This is called from kswapd when we think we need some
643 static int shrink_dqcache_memory(int nr, gfp_t gfp_mask)
646 spin_lock(&dq_list_lock);
648 spin_unlock(&dq_list_lock);
650 return (dqstats.free_dquots / 100) * sysctl_vfs_cache_pressure;
653 static struct shrinker dqcache_shrinker = {
654 .shrink = shrink_dqcache_memory,
655 .seeks = DEFAULT_SEEKS,
659 * Put reference to dquot
660 * NOTE: If you change this function please check whether dqput_blocks() works right...
662 void dqput(struct dquot *dquot)
668 #ifdef __DQUOT_PARANOIA
669 if (!atomic_read(&dquot->dq_count)) {
670 printk("VFS: dqput: trying to free free dquot\n");
671 printk("VFS: device %s, dquot of %s %d\n",
673 quotatypes[dquot->dq_type],
679 spin_lock(&dq_list_lock);
681 spin_unlock(&dq_list_lock);
683 spin_lock(&dq_list_lock);
684 if (atomic_read(&dquot->dq_count) > 1) {
685 /* We have more than one user... nothing to do */
686 atomic_dec(&dquot->dq_count);
687 /* Releasing dquot during quotaoff phase? */
688 if (!sb_has_quota_active(dquot->dq_sb, dquot->dq_type) &&
689 atomic_read(&dquot->dq_count) == 1)
690 wake_up(&dquot->dq_wait_unused);
691 spin_unlock(&dq_list_lock);
694 /* Need to release dquot? */
695 if (test_bit(DQ_ACTIVE_B, &dquot->dq_flags) && dquot_dirty(dquot)) {
696 spin_unlock(&dq_list_lock);
697 /* Commit dquot before releasing */
698 ret = dquot->dq_sb->dq_op->write_dquot(dquot);
700 printk(KERN_ERR "VFS: cannot write quota structure on "
701 "device %s (error %d). Quota may get out of "
702 "sync!\n", dquot->dq_sb->s_id, ret);
704 * We clear dirty bit anyway, so that we avoid
707 spin_lock(&dq_list_lock);
708 clear_dquot_dirty(dquot);
709 spin_unlock(&dq_list_lock);
713 /* Clear flag in case dquot was inactive (something bad happened) */
714 clear_dquot_dirty(dquot);
715 if (test_bit(DQ_ACTIVE_B, &dquot->dq_flags)) {
716 spin_unlock(&dq_list_lock);
717 dquot->dq_sb->dq_op->release_dquot(dquot);
720 atomic_dec(&dquot->dq_count);
721 #ifdef __DQUOT_PARANOIA
723 BUG_ON(!list_empty(&dquot->dq_free));
725 put_dquot_last(dquot);
726 spin_unlock(&dq_list_lock);
728 EXPORT_SYMBOL(dqput);
730 struct dquot *dquot_alloc(struct super_block *sb, int type)
732 return kmem_cache_zalloc(dquot_cachep, GFP_NOFS);
734 EXPORT_SYMBOL(dquot_alloc);
736 static struct dquot *get_empty_dquot(struct super_block *sb, int type)
740 dquot = sb->dq_op->alloc_dquot(sb, type);
744 mutex_init(&dquot->dq_lock);
745 INIT_LIST_HEAD(&dquot->dq_free);
746 INIT_LIST_HEAD(&dquot->dq_inuse);
747 INIT_HLIST_NODE(&dquot->dq_hash);
748 INIT_LIST_HEAD(&dquot->dq_dirty);
749 init_waitqueue_head(&dquot->dq_wait_unused);
751 dquot->dq_type = type;
752 atomic_set(&dquot->dq_count, 1);
758 * Get reference to dquot
760 * Locking is slightly tricky here. We are guarded from parallel quotaoff()
761 * destroying our dquot by:
762 * a) checking for quota flags under dq_list_lock and
763 * b) getting a reference to dquot before we release dq_list_lock
765 struct dquot *dqget(struct super_block *sb, unsigned int id, int type)
767 unsigned int hashent = hashfn(sb, id, type);
768 struct dquot *dquot = NULL, *empty = NULL;
770 if (!sb_has_quota_active(sb, type))
773 spin_lock(&dq_list_lock);
774 spin_lock(&dq_state_lock);
775 if (!sb_has_quota_active(sb, type)) {
776 spin_unlock(&dq_state_lock);
777 spin_unlock(&dq_list_lock);
780 spin_unlock(&dq_state_lock);
782 dquot = find_dquot(hashent, sb, id, type);
785 spin_unlock(&dq_list_lock);
786 empty = get_empty_dquot(sb, type);
788 schedule(); /* Try to wait for a moment... */
794 /* all dquots go on the inuse_list */
796 /* hash it first so it can be found */
797 insert_dquot_hash(dquot);
799 spin_unlock(&dq_list_lock);
801 if (!atomic_read(&dquot->dq_count))
802 remove_free_dquot(dquot);
803 atomic_inc(&dquot->dq_count);
804 dqstats.cache_hits++;
806 spin_unlock(&dq_list_lock);
808 /* Wait for dq_lock - after this we know that either dquot_release() is
809 * already finished or it will be canceled due to dq_count > 1 test */
810 wait_on_dquot(dquot);
811 /* Read the dquot / allocate space in quota file */
812 if (!test_bit(DQ_ACTIVE_B, &dquot->dq_flags) &&
813 sb->dq_op->acquire_dquot(dquot) < 0) {
818 #ifdef __DQUOT_PARANOIA
819 BUG_ON(!dquot->dq_sb); /* Has somebody invalidated entry under us? */
823 do_destroy_dquot(empty);
827 EXPORT_SYMBOL(dqget);
829 static int dqinit_needed(struct inode *inode, int type)
833 if (IS_NOQUOTA(inode))
836 return !inode->i_dquot[type];
837 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
838 if (!inode->i_dquot[cnt])
843 /* This routine is guarded by dqonoff_mutex mutex */
844 static void add_dquot_ref(struct super_block *sb, int type)
846 struct inode *inode, *old_inode = NULL;
848 spin_lock(&inode_lock);
849 list_for_each_entry(inode, &sb->s_inodes, i_sb_list) {
850 if (inode->i_state & (I_FREEING|I_CLEAR|I_WILL_FREE|I_NEW))
852 if (!atomic_read(&inode->i_writecount))
854 if (!dqinit_needed(inode, type))
858 spin_unlock(&inode_lock);
861 sb->dq_op->initialize(inode, type);
862 /* We hold a reference to 'inode' so it couldn't have been
863 * removed from s_inodes list while we dropped the inode_lock.
864 * We cannot iput the inode now as we can be holding the last
865 * reference and we cannot iput it under inode_lock. So we
866 * keep the reference and iput it later. */
868 spin_lock(&inode_lock);
870 spin_unlock(&inode_lock);
875 * Return 0 if dqput() won't block.
876 * (note that 1 doesn't necessarily mean blocking)
878 static inline int dqput_blocks(struct dquot *dquot)
880 if (atomic_read(&dquot->dq_count) <= 1)
886 * Remove references to dquots from inode and add dquot to list for freeing
887 * if we have the last referece to dquot
888 * We can't race with anybody because we hold dqptr_sem for writing...
890 static int remove_inode_dquot_ref(struct inode *inode, int type,
891 struct list_head *tofree_head)
893 struct dquot *dquot = inode->i_dquot[type];
895 inode->i_dquot[type] = NULL;
897 if (dqput_blocks(dquot)) {
898 #ifdef __DQUOT_PARANOIA
899 if (atomic_read(&dquot->dq_count) != 1)
900 printk(KERN_WARNING "VFS: Adding dquot with dq_count %d to dispose list.\n", atomic_read(&dquot->dq_count));
902 spin_lock(&dq_list_lock);
903 /* As dquot must have currently users it can't be on
904 * the free list... */
905 list_add(&dquot->dq_free, tofree_head);
906 spin_unlock(&dq_list_lock);
910 dqput(dquot); /* We have guaranteed we won't block */
916 * Free list of dquots
917 * Dquots are removed from inodes and no new references can be got so we are
918 * the only ones holding reference
920 static void put_dquot_list(struct list_head *tofree_head)
922 struct list_head *act_head;
925 act_head = tofree_head->next;
926 while (act_head != tofree_head) {
927 dquot = list_entry(act_head, struct dquot, dq_free);
928 act_head = act_head->next;
929 /* Remove dquot from the list so we won't have problems... */
930 list_del_init(&dquot->dq_free);
935 static void remove_dquot_ref(struct super_block *sb, int type,
936 struct list_head *tofree_head)
940 spin_lock(&inode_lock);
941 list_for_each_entry(inode, &sb->s_inodes, i_sb_list) {
943 * We have to scan also I_NEW inodes because they can already
944 * have quota pointer initialized. Luckily, we need to touch
945 * only quota pointers and these have separate locking
948 if (!IS_NOQUOTA(inode))
949 remove_inode_dquot_ref(inode, type, tofree_head);
951 spin_unlock(&inode_lock);
954 /* Gather all references from inodes and drop them */
955 static void drop_dquot_ref(struct super_block *sb, int type)
957 LIST_HEAD(tofree_head);
960 down_write(&sb_dqopt(sb)->dqptr_sem);
961 remove_dquot_ref(sb, type, &tofree_head);
962 up_write(&sb_dqopt(sb)->dqptr_sem);
963 put_dquot_list(&tofree_head);
967 static inline void dquot_incr_inodes(struct dquot *dquot, qsize_t number)
969 dquot->dq_dqb.dqb_curinodes += number;
972 static inline void dquot_incr_space(struct dquot *dquot, qsize_t number)
974 dquot->dq_dqb.dqb_curspace += number;
977 static inline void dquot_resv_space(struct dquot *dquot, qsize_t number)
979 dquot->dq_dqb.dqb_rsvspace += number;
983 * Claim reserved quota space
985 static void dquot_claim_reserved_space(struct dquot *dquot,
988 WARN_ON(dquot->dq_dqb.dqb_rsvspace < number);
989 dquot->dq_dqb.dqb_curspace += number;
990 dquot->dq_dqb.dqb_rsvspace -= number;
994 void dquot_free_reserved_space(struct dquot *dquot, qsize_t number)
996 dquot->dq_dqb.dqb_rsvspace -= number;
999 static void dquot_decr_inodes(struct dquot *dquot, qsize_t number)
1001 if (sb_dqopt(dquot->dq_sb)->flags & DQUOT_NEGATIVE_USAGE ||
1002 dquot->dq_dqb.dqb_curinodes >= number)
1003 dquot->dq_dqb.dqb_curinodes -= number;
1005 dquot->dq_dqb.dqb_curinodes = 0;
1006 if (dquot->dq_dqb.dqb_curinodes <= dquot->dq_dqb.dqb_isoftlimit)
1007 dquot->dq_dqb.dqb_itime = (time_t) 0;
1008 clear_bit(DQ_INODES_B, &dquot->dq_flags);
1011 static void dquot_decr_space(struct dquot *dquot, qsize_t number)
1013 if (sb_dqopt(dquot->dq_sb)->flags & DQUOT_NEGATIVE_USAGE ||
1014 dquot->dq_dqb.dqb_curspace >= number)
1015 dquot->dq_dqb.dqb_curspace -= number;
1017 dquot->dq_dqb.dqb_curspace = 0;
1018 if (dquot->dq_dqb.dqb_curspace <= dquot->dq_dqb.dqb_bsoftlimit)
1019 dquot->dq_dqb.dqb_btime = (time_t) 0;
1020 clear_bit(DQ_BLKS_B, &dquot->dq_flags);
1023 static int warning_issued(struct dquot *dquot, const int warntype)
1025 int flag = (warntype == QUOTA_NL_BHARDWARN ||
1026 warntype == QUOTA_NL_BSOFTLONGWARN) ? DQ_BLKS_B :
1027 ((warntype == QUOTA_NL_IHARDWARN ||
1028 warntype == QUOTA_NL_ISOFTLONGWARN) ? DQ_INODES_B : 0);
1032 return test_and_set_bit(flag, &dquot->dq_flags);
1035 #ifdef CONFIG_PRINT_QUOTA_WARNING
1036 static int flag_print_warnings = 1;
1038 static int need_print_warning(struct dquot *dquot)
1040 if (!flag_print_warnings)
1043 switch (dquot->dq_type) {
1045 return current_fsuid() == dquot->dq_id;
1047 return in_group_p(dquot->dq_id);
1052 /* Print warning to user which exceeded quota */
1053 static void print_warning(struct dquot *dquot, const int warntype)
1056 struct tty_struct *tty;
1058 if (warntype == QUOTA_NL_IHARDBELOW ||
1059 warntype == QUOTA_NL_ISOFTBELOW ||
1060 warntype == QUOTA_NL_BHARDBELOW ||
1061 warntype == QUOTA_NL_BSOFTBELOW || !need_print_warning(dquot))
1064 tty = get_current_tty();
1067 tty_write_message(tty, dquot->dq_sb->s_id);
1068 if (warntype == QUOTA_NL_ISOFTWARN || warntype == QUOTA_NL_BSOFTWARN)
1069 tty_write_message(tty, ": warning, ");
1071 tty_write_message(tty, ": write failed, ");
1072 tty_write_message(tty, quotatypes[dquot->dq_type]);
1074 case QUOTA_NL_IHARDWARN:
1075 msg = " file limit reached.\r\n";
1077 case QUOTA_NL_ISOFTLONGWARN:
1078 msg = " file quota exceeded too long.\r\n";
1080 case QUOTA_NL_ISOFTWARN:
1081 msg = " file quota exceeded.\r\n";
1083 case QUOTA_NL_BHARDWARN:
1084 msg = " block limit reached.\r\n";
1086 case QUOTA_NL_BSOFTLONGWARN:
1087 msg = " block quota exceeded too long.\r\n";
1089 case QUOTA_NL_BSOFTWARN:
1090 msg = " block quota exceeded.\r\n";
1093 tty_write_message(tty, msg);
1099 * Write warnings to the console and send warning messages over netlink.
1101 * Note that this function can sleep.
1103 static void flush_warnings(struct dquot *const *dquots, char *warntype)
1108 for (i = 0; i < MAXQUOTAS; i++) {
1110 if (dq && warntype[i] != QUOTA_NL_NOWARN &&
1111 !warning_issued(dq, warntype[i])) {
1112 #ifdef CONFIG_PRINT_QUOTA_WARNING
1113 print_warning(dq, warntype[i]);
1115 quota_send_warning(dq->dq_type, dq->dq_id,
1116 dq->dq_sb->s_dev, warntype[i]);
1121 static int ignore_hardlimit(struct dquot *dquot)
1123 struct mem_dqinfo *info = &sb_dqopt(dquot->dq_sb)->info[dquot->dq_type];
1125 return capable(CAP_SYS_RESOURCE) &&
1126 (info->dqi_format->qf_fmt_id != QFMT_VFS_OLD ||
1127 !(info->dqi_flags & V1_DQF_RSQUASH));
1130 /* needs dq_data_lock */
1131 static int check_idq(struct dquot *dquot, qsize_t inodes, char *warntype)
1133 qsize_t newinodes = dquot->dq_dqb.dqb_curinodes + inodes;
1135 *warntype = QUOTA_NL_NOWARN;
1136 if (!sb_has_quota_limits_enabled(dquot->dq_sb, dquot->dq_type) ||
1137 test_bit(DQ_FAKE_B, &dquot->dq_flags))
1140 if (dquot->dq_dqb.dqb_ihardlimit &&
1141 newinodes > dquot->dq_dqb.dqb_ihardlimit &&
1142 !ignore_hardlimit(dquot)) {
1143 *warntype = QUOTA_NL_IHARDWARN;
1147 if (dquot->dq_dqb.dqb_isoftlimit &&
1148 newinodes > dquot->dq_dqb.dqb_isoftlimit &&
1149 dquot->dq_dqb.dqb_itime &&
1150 get_seconds() >= dquot->dq_dqb.dqb_itime &&
1151 !ignore_hardlimit(dquot)) {
1152 *warntype = QUOTA_NL_ISOFTLONGWARN;
1156 if (dquot->dq_dqb.dqb_isoftlimit &&
1157 newinodes > dquot->dq_dqb.dqb_isoftlimit &&
1158 dquot->dq_dqb.dqb_itime == 0) {
1159 *warntype = QUOTA_NL_ISOFTWARN;
1160 dquot->dq_dqb.dqb_itime = get_seconds() +
1161 sb_dqopt(dquot->dq_sb)->info[dquot->dq_type].dqi_igrace;
1167 /* needs dq_data_lock */
1168 static int check_bdq(struct dquot *dquot, qsize_t space, int prealloc, char *warntype)
1171 struct super_block *sb = dquot->dq_sb;
1173 *warntype = QUOTA_NL_NOWARN;
1174 if (!sb_has_quota_limits_enabled(sb, dquot->dq_type) ||
1175 test_bit(DQ_FAKE_B, &dquot->dq_flags))
1178 tspace = dquot->dq_dqb.dqb_curspace + dquot->dq_dqb.dqb_rsvspace
1181 if (dquot->dq_dqb.dqb_bhardlimit &&
1182 tspace > dquot->dq_dqb.dqb_bhardlimit &&
1183 !ignore_hardlimit(dquot)) {
1185 *warntype = QUOTA_NL_BHARDWARN;
1189 if (dquot->dq_dqb.dqb_bsoftlimit &&
1190 tspace > dquot->dq_dqb.dqb_bsoftlimit &&
1191 dquot->dq_dqb.dqb_btime &&
1192 get_seconds() >= dquot->dq_dqb.dqb_btime &&
1193 !ignore_hardlimit(dquot)) {
1195 *warntype = QUOTA_NL_BSOFTLONGWARN;
1199 if (dquot->dq_dqb.dqb_bsoftlimit &&
1200 tspace > dquot->dq_dqb.dqb_bsoftlimit &&
1201 dquot->dq_dqb.dqb_btime == 0) {
1203 *warntype = QUOTA_NL_BSOFTWARN;
1204 dquot->dq_dqb.dqb_btime = get_seconds() +
1205 sb_dqopt(sb)->info[dquot->dq_type].dqi_bgrace;
1209 * We don't allow preallocation to exceed softlimit so exceeding will
1218 static int info_idq_free(struct dquot *dquot, qsize_t inodes)
1222 if (test_bit(DQ_FAKE_B, &dquot->dq_flags) ||
1223 dquot->dq_dqb.dqb_curinodes <= dquot->dq_dqb.dqb_isoftlimit ||
1224 !sb_has_quota_limits_enabled(dquot->dq_sb, dquot->dq_type))
1225 return QUOTA_NL_NOWARN;
1227 newinodes = dquot->dq_dqb.dqb_curinodes - inodes;
1228 if (newinodes <= dquot->dq_dqb.dqb_isoftlimit)
1229 return QUOTA_NL_ISOFTBELOW;
1230 if (dquot->dq_dqb.dqb_curinodes >= dquot->dq_dqb.dqb_ihardlimit &&
1231 newinodes < dquot->dq_dqb.dqb_ihardlimit)
1232 return QUOTA_NL_IHARDBELOW;
1233 return QUOTA_NL_NOWARN;
1236 static int info_bdq_free(struct dquot *dquot, qsize_t space)
1238 if (test_bit(DQ_FAKE_B, &dquot->dq_flags) ||
1239 dquot->dq_dqb.dqb_curspace <= dquot->dq_dqb.dqb_bsoftlimit)
1240 return QUOTA_NL_NOWARN;
1242 if (dquot->dq_dqb.dqb_curspace - space <= dquot->dq_dqb.dqb_bsoftlimit)
1243 return QUOTA_NL_BSOFTBELOW;
1244 if (dquot->dq_dqb.dqb_curspace >= dquot->dq_dqb.dqb_bhardlimit &&
1245 dquot->dq_dqb.dqb_curspace - space < dquot->dq_dqb.dqb_bhardlimit)
1246 return QUOTA_NL_BHARDBELOW;
1247 return QUOTA_NL_NOWARN;
1250 * Initialize quota pointers in inode
1251 * We do things in a bit complicated way but by that we avoid calling
1252 * dqget() and thus filesystem callbacks under dqptr_sem.
1254 int dquot_initialize(struct inode *inode, int type)
1256 unsigned int id = 0;
1258 struct dquot *got[MAXQUOTAS] = { NULL, NULL };
1259 struct super_block *sb = inode->i_sb;
1261 /* First test before acquiring mutex - solves deadlocks when we
1262 * re-enter the quota code and are already holding the mutex */
1263 if (IS_NOQUOTA(inode))
1266 /* First get references to structures we might need. */
1267 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1268 if (type != -1 && cnt != type)
1278 got[cnt] = dqget(sb, id, cnt);
1281 down_write(&sb_dqopt(sb)->dqptr_sem);
1282 if (IS_NOQUOTA(inode))
1284 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1285 if (type != -1 && cnt != type)
1287 /* Avoid races with quotaoff() */
1288 if (!sb_has_quota_active(sb, cnt))
1290 if (!inode->i_dquot[cnt]) {
1291 inode->i_dquot[cnt] = got[cnt];
1296 up_write(&sb_dqopt(sb)->dqptr_sem);
1297 /* Drop unused references */
1301 EXPORT_SYMBOL(dquot_initialize);
1304 * Release all quotas referenced by inode
1306 int dquot_drop(struct inode *inode)
1309 struct dquot *put[MAXQUOTAS];
1311 down_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
1312 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1313 put[cnt] = inode->i_dquot[cnt];
1314 inode->i_dquot[cnt] = NULL;
1316 up_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
1320 EXPORT_SYMBOL(dquot_drop);
1322 /* Wrapper to remove references to quota structures from inode */
1323 void vfs_dq_drop(struct inode *inode)
1325 /* Here we can get arbitrary inode from clear_inode() so we have
1326 * to be careful. OTOH we don't need locking as quota operations
1327 * are allowed to change only at mount time */
1328 if (!IS_NOQUOTA(inode) && inode->i_sb && inode->i_sb->dq_op
1329 && inode->i_sb->dq_op->drop) {
1331 /* Test before calling to rule out calls from proc and such
1332 * where we are not allowed to block. Note that this is
1333 * actually reliable test even without the lock - the caller
1334 * must assure that nobody can come after the DQUOT_DROP and
1335 * add quota pointers back anyway */
1336 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1337 if (inode->i_dquot[cnt])
1339 if (cnt < MAXQUOTAS)
1340 inode->i_sb->dq_op->drop(inode);
1343 EXPORT_SYMBOL(vfs_dq_drop);
1346 * inode_reserved_space is managed internally by quota, and protected by
1347 * i_lock similar to i_blocks+i_bytes.
1349 static qsize_t *inode_reserved_space(struct inode * inode)
1351 /* Filesystem must explicitly define it's own method in order to use
1352 * quota reservation interface */
1353 BUG_ON(!inode->i_sb->dq_op->get_reserved_space);
1354 return inode->i_sb->dq_op->get_reserved_space(inode);
1357 static void inode_add_rsv_space(struct inode *inode, qsize_t number)
1359 spin_lock(&inode->i_lock);
1360 *inode_reserved_space(inode) += number;
1361 spin_unlock(&inode->i_lock);
1365 static void inode_claim_rsv_space(struct inode *inode, qsize_t number)
1367 spin_lock(&inode->i_lock);
1368 *inode_reserved_space(inode) -= number;
1369 __inode_add_bytes(inode, number);
1370 spin_unlock(&inode->i_lock);
1373 static void inode_sub_rsv_space(struct inode *inode, qsize_t number)
1375 spin_lock(&inode->i_lock);
1376 *inode_reserved_space(inode) -= number;
1377 spin_unlock(&inode->i_lock);
1380 static qsize_t inode_get_rsv_space(struct inode *inode)
1384 if (!inode->i_sb->dq_op->get_reserved_space)
1386 spin_lock(&inode->i_lock);
1387 ret = *inode_reserved_space(inode);
1388 spin_unlock(&inode->i_lock);
1392 static void inode_incr_space(struct inode *inode, qsize_t number,
1396 inode_add_rsv_space(inode, number);
1398 inode_add_bytes(inode, number);
1401 static void inode_decr_space(struct inode *inode, qsize_t number, int reserve)
1404 inode_sub_rsv_space(inode, number);
1406 inode_sub_bytes(inode, number);
1410 * Following four functions update i_blocks+i_bytes fields and
1411 * quota information (together with appropriate checks)
1412 * NOTE: We absolutely rely on the fact that caller dirties
1413 * the inode (usually macros in quotaops.h care about this) and
1414 * holds a handle for the current transaction so that dquot write and
1415 * inode write go into the same transaction.
1419 * This operation can block, but only after everything is updated
1421 int __dquot_alloc_space(struct inode *inode, qsize_t number,
1422 int warn, int reserve)
1424 int cnt, ret = QUOTA_OK;
1425 char warntype[MAXQUOTAS];
1428 * First test before acquiring mutex - solves deadlocks when we
1429 * re-enter the quota code and are already holding the mutex
1431 if (IS_NOQUOTA(inode)) {
1432 inode_incr_space(inode, number, reserve);
1436 down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1437 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1438 warntype[cnt] = QUOTA_NL_NOWARN;
1440 spin_lock(&dq_data_lock);
1441 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1442 if (!inode->i_dquot[cnt])
1444 if (check_bdq(inode->i_dquot[cnt], number, warn, warntype+cnt)
1447 spin_unlock(&dq_data_lock);
1448 goto out_flush_warn;
1451 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1452 if (!inode->i_dquot[cnt])
1455 dquot_resv_space(inode->i_dquot[cnt], number);
1457 dquot_incr_space(inode->i_dquot[cnt], number);
1459 inode_incr_space(inode, number, reserve);
1460 spin_unlock(&dq_data_lock);
1463 goto out_flush_warn;
1464 mark_all_dquot_dirty(inode->i_dquot);
1466 flush_warnings(inode->i_dquot, warntype);
1467 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1472 int dquot_alloc_space(struct inode *inode, qsize_t number, int warn)
1474 return __dquot_alloc_space(inode, number, warn, 0);
1476 EXPORT_SYMBOL(dquot_alloc_space);
1478 int dquot_reserve_space(struct inode *inode, qsize_t number, int warn)
1480 return __dquot_alloc_space(inode, number, warn, 1);
1482 EXPORT_SYMBOL(dquot_reserve_space);
1485 * This operation can block, but only after everything is updated
1487 int dquot_alloc_inode(const struct inode *inode, qsize_t number)
1489 int cnt, ret = NO_QUOTA;
1490 char warntype[MAXQUOTAS];
1492 /* First test before acquiring mutex - solves deadlocks when we
1493 * re-enter the quota code and are already holding the mutex */
1494 if (IS_NOQUOTA(inode))
1496 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1497 warntype[cnt] = QUOTA_NL_NOWARN;
1498 down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1499 spin_lock(&dq_data_lock);
1500 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1501 if (!inode->i_dquot[cnt])
1503 if (check_idq(inode->i_dquot[cnt], number, warntype+cnt)
1508 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1509 if (!inode->i_dquot[cnt])
1511 dquot_incr_inodes(inode->i_dquot[cnt], number);
1515 spin_unlock(&dq_data_lock);
1516 if (ret == QUOTA_OK)
1517 mark_all_dquot_dirty(inode->i_dquot);
1518 flush_warnings(inode->i_dquot, warntype);
1519 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1522 EXPORT_SYMBOL(dquot_alloc_inode);
1524 int dquot_claim_space(struct inode *inode, qsize_t number)
1529 if (IS_NOQUOTA(inode)) {
1530 inode_claim_rsv_space(inode, number);
1534 down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1535 spin_lock(&dq_data_lock);
1536 /* Claim reserved quotas to allocated quotas */
1537 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1538 if (inode->i_dquot[cnt])
1539 dquot_claim_reserved_space(inode->i_dquot[cnt],
1542 /* Update inode bytes */
1543 inode_claim_rsv_space(inode, number);
1544 spin_unlock(&dq_data_lock);
1545 mark_all_dquot_dirty(inode->i_dquot);
1546 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1550 EXPORT_SYMBOL(dquot_claim_space);
1553 * This operation can block, but only after everything is updated
1555 int __dquot_free_space(struct inode *inode, qsize_t number, int reserve)
1558 char warntype[MAXQUOTAS];
1560 /* First test before acquiring mutex - solves deadlocks when we
1561 * re-enter the quota code and are already holding the mutex */
1562 if (IS_NOQUOTA(inode)) {
1563 inode_decr_space(inode, number, reserve);
1567 down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1568 spin_lock(&dq_data_lock);
1569 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1570 if (!inode->i_dquot[cnt])
1572 warntype[cnt] = info_bdq_free(inode->i_dquot[cnt], number);
1574 dquot_free_reserved_space(inode->i_dquot[cnt], number);
1576 dquot_decr_space(inode->i_dquot[cnt], number);
1578 inode_decr_space(inode, number, reserve);
1579 spin_unlock(&dq_data_lock);
1583 mark_all_dquot_dirty(inode->i_dquot);
1585 flush_warnings(inode->i_dquot, warntype);
1586 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1590 int dquot_free_space(struct inode *inode, qsize_t number)
1592 return __dquot_free_space(inode, number, 0);
1594 EXPORT_SYMBOL(dquot_free_space);
1597 * Release reserved quota space
1599 void dquot_release_reserved_space(struct inode *inode, qsize_t number)
1601 __dquot_free_space(inode, number, 1);
1604 EXPORT_SYMBOL(dquot_release_reserved_space);
1607 * This operation can block, but only after everything is updated
1609 int dquot_free_inode(const struct inode *inode, qsize_t number)
1612 char warntype[MAXQUOTAS];
1614 /* First test before acquiring mutex - solves deadlocks when we
1615 * re-enter the quota code and are already holding the mutex */
1616 if (IS_NOQUOTA(inode))
1619 down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1620 spin_lock(&dq_data_lock);
1621 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1622 if (!inode->i_dquot[cnt])
1624 warntype[cnt] = info_idq_free(inode->i_dquot[cnt], number);
1625 dquot_decr_inodes(inode->i_dquot[cnt], number);
1627 spin_unlock(&dq_data_lock);
1628 mark_all_dquot_dirty(inode->i_dquot);
1629 flush_warnings(inode->i_dquot, warntype);
1630 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1633 EXPORT_SYMBOL(dquot_free_inode);
1636 * Transfer the number of inode and blocks from one diskquota to an other.
1638 * This operation can block, but only after everything is updated
1639 * A transaction must be started when entering this function.
1641 int dquot_transfer(struct inode *inode, struct iattr *iattr)
1643 qsize_t space, cur_space;
1644 qsize_t rsv_space = 0;
1645 struct dquot *transfer_from[MAXQUOTAS];
1646 struct dquot *transfer_to[MAXQUOTAS];
1647 int cnt, ret = QUOTA_OK;
1648 int chuid = iattr->ia_valid & ATTR_UID && inode->i_uid != iattr->ia_uid,
1649 chgid = iattr->ia_valid & ATTR_GID && inode->i_gid != iattr->ia_gid;
1650 char warntype_to[MAXQUOTAS];
1651 char warntype_from_inodes[MAXQUOTAS], warntype_from_space[MAXQUOTAS];
1653 /* First test before acquiring mutex - solves deadlocks when we
1654 * re-enter the quota code and are already holding the mutex */
1655 if (IS_NOQUOTA(inode))
1657 /* Initialize the arrays */
1658 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1659 transfer_from[cnt] = NULL;
1660 transfer_to[cnt] = NULL;
1661 warntype_to[cnt] = QUOTA_NL_NOWARN;
1664 transfer_to[USRQUOTA] = dqget(inode->i_sb, iattr->ia_uid,
1667 transfer_to[GRPQUOTA] = dqget(inode->i_sb, iattr->ia_gid,
1670 down_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
1671 if (IS_NOQUOTA(inode)) { /* File without quota accounting? */
1672 up_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
1675 spin_lock(&dq_data_lock);
1676 cur_space = inode_get_bytes(inode);
1677 rsv_space = inode_get_rsv_space(inode);
1678 space = cur_space + rsv_space;
1679 /* Build the transfer_from list and check the limits */
1680 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1681 if (!transfer_to[cnt])
1683 transfer_from[cnt] = inode->i_dquot[cnt];
1684 if (check_idq(transfer_to[cnt], 1, warntype_to + cnt) ==
1685 NO_QUOTA || check_bdq(transfer_to[cnt], space, 0,
1686 warntype_to + cnt) == NO_QUOTA)
1691 * Finally perform the needed transfer from transfer_from to transfer_to
1693 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1695 * Skip changes for same uid or gid or for turned off quota-type.
1697 if (!transfer_to[cnt])
1700 /* Due to IO error we might not have transfer_from[] structure */
1701 if (transfer_from[cnt]) {
1702 warntype_from_inodes[cnt] =
1703 info_idq_free(transfer_from[cnt], 1);
1704 warntype_from_space[cnt] =
1705 info_bdq_free(transfer_from[cnt], space);
1706 dquot_decr_inodes(transfer_from[cnt], 1);
1707 dquot_decr_space(transfer_from[cnt], cur_space);
1708 dquot_free_reserved_space(transfer_from[cnt],
1712 dquot_incr_inodes(transfer_to[cnt], 1);
1713 dquot_incr_space(transfer_to[cnt], cur_space);
1714 dquot_resv_space(transfer_to[cnt], rsv_space);
1716 inode->i_dquot[cnt] = transfer_to[cnt];
1718 spin_unlock(&dq_data_lock);
1719 up_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
1721 mark_all_dquot_dirty(transfer_from);
1722 mark_all_dquot_dirty(transfer_to);
1723 /* The reference we got is transferred to the inode */
1724 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1725 transfer_to[cnt] = NULL;
1727 flush_warnings(transfer_to, warntype_to);
1728 flush_warnings(transfer_from, warntype_from_inodes);
1729 flush_warnings(transfer_from, warntype_from_space);
1731 dqput_all(transfer_from);
1732 dqput_all(transfer_to);
1735 spin_unlock(&dq_data_lock);
1736 up_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
1737 /* Clear dquot pointers we don't want to dqput() */
1738 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1739 transfer_from[cnt] = NULL;
1743 EXPORT_SYMBOL(dquot_transfer);
1745 /* Wrapper for transferring ownership of an inode */
1746 int vfs_dq_transfer(struct inode *inode, struct iattr *iattr)
1748 if (sb_any_quota_active(inode->i_sb) && !IS_NOQUOTA(inode)) {
1750 if (inode->i_sb->dq_op->transfer(inode, iattr) == NO_QUOTA)
1755 EXPORT_SYMBOL(vfs_dq_transfer);
1758 * Write info of quota file to disk
1760 int dquot_commit_info(struct super_block *sb, int type)
1763 struct quota_info *dqopt = sb_dqopt(sb);
1765 mutex_lock(&dqopt->dqio_mutex);
1766 ret = dqopt->ops[type]->write_file_info(sb, type);
1767 mutex_unlock(&dqopt->dqio_mutex);
1770 EXPORT_SYMBOL(dquot_commit_info);
1773 * Definitions of diskquota operations.
1775 const struct dquot_operations dquot_operations = {
1776 .initialize = dquot_initialize,
1778 .alloc_space = dquot_alloc_space,
1779 .alloc_inode = dquot_alloc_inode,
1780 .free_space = dquot_free_space,
1781 .free_inode = dquot_free_inode,
1782 .transfer = dquot_transfer,
1783 .write_dquot = dquot_commit,
1784 .acquire_dquot = dquot_acquire,
1785 .release_dquot = dquot_release,
1786 .mark_dirty = dquot_mark_dquot_dirty,
1787 .write_info = dquot_commit_info,
1788 .alloc_dquot = dquot_alloc,
1789 .destroy_dquot = dquot_destroy,
1793 * Turn quota off on a device. type == -1 ==> quotaoff for all types (umount)
1795 int vfs_quota_disable(struct super_block *sb, int type, unsigned int flags)
1798 struct quota_info *dqopt = sb_dqopt(sb);
1799 struct inode *toputinode[MAXQUOTAS];
1801 /* Cannot turn off usage accounting without turning off limits, or
1802 * suspend quotas and simultaneously turn quotas off. */
1803 if ((flags & DQUOT_USAGE_ENABLED && !(flags & DQUOT_LIMITS_ENABLED))
1804 || (flags & DQUOT_SUSPENDED && flags & (DQUOT_LIMITS_ENABLED |
1805 DQUOT_USAGE_ENABLED)))
1808 /* We need to serialize quota_off() for device */
1809 mutex_lock(&dqopt->dqonoff_mutex);
1812 * Skip everything if there's nothing to do. We have to do this because
1813 * sometimes we are called when fill_super() failed and calling
1814 * sync_fs() in such cases does no good.
1816 if (!sb_any_quota_loaded(sb)) {
1817 mutex_unlock(&dqopt->dqonoff_mutex);
1820 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1821 toputinode[cnt] = NULL;
1822 if (type != -1 && cnt != type)
1824 if (!sb_has_quota_loaded(sb, cnt))
1827 if (flags & DQUOT_SUSPENDED) {
1828 spin_lock(&dq_state_lock);
1830 dquot_state_flag(DQUOT_SUSPENDED, cnt);
1831 spin_unlock(&dq_state_lock);
1833 spin_lock(&dq_state_lock);
1834 dqopt->flags &= ~dquot_state_flag(flags, cnt);
1835 /* Turning off suspended quotas? */
1836 if (!sb_has_quota_loaded(sb, cnt) &&
1837 sb_has_quota_suspended(sb, cnt)) {
1838 dqopt->flags &= ~dquot_state_flag(
1839 DQUOT_SUSPENDED, cnt);
1840 spin_unlock(&dq_state_lock);
1841 iput(dqopt->files[cnt]);
1842 dqopt->files[cnt] = NULL;
1845 spin_unlock(&dq_state_lock);
1848 /* We still have to keep quota loaded? */
1849 if (sb_has_quota_loaded(sb, cnt) && !(flags & DQUOT_SUSPENDED))
1852 /* Note: these are blocking operations */
1853 drop_dquot_ref(sb, cnt);
1854 invalidate_dquots(sb, cnt);
1856 * Now all dquots should be invalidated, all writes done so we
1857 * should be only users of the info. No locks needed.
1859 if (info_dirty(&dqopt->info[cnt]))
1860 sb->dq_op->write_info(sb, cnt);
1861 if (dqopt->ops[cnt]->free_file_info)
1862 dqopt->ops[cnt]->free_file_info(sb, cnt);
1863 put_quota_format(dqopt->info[cnt].dqi_format);
1865 toputinode[cnt] = dqopt->files[cnt];
1866 if (!sb_has_quota_loaded(sb, cnt))
1867 dqopt->files[cnt] = NULL;
1868 dqopt->info[cnt].dqi_flags = 0;
1869 dqopt->info[cnt].dqi_igrace = 0;
1870 dqopt->info[cnt].dqi_bgrace = 0;
1871 dqopt->ops[cnt] = NULL;
1873 mutex_unlock(&dqopt->dqonoff_mutex);
1875 /* Skip syncing and setting flags if quota files are hidden */
1876 if (dqopt->flags & DQUOT_QUOTA_SYS_FILE)
1879 /* Sync the superblock so that buffers with quota data are written to
1880 * disk (and so userspace sees correct data afterwards). */
1881 if (sb->s_op->sync_fs)
1882 sb->s_op->sync_fs(sb, 1);
1883 sync_blockdev(sb->s_bdev);
1884 /* Now the quota files are just ordinary files and we can set the
1885 * inode flags back. Moreover we discard the pagecache so that
1886 * userspace sees the writes we did bypassing the pagecache. We
1887 * must also discard the blockdev buffers so that we see the
1888 * changes done by userspace on the next quotaon() */
1889 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1890 if (toputinode[cnt]) {
1891 mutex_lock(&dqopt->dqonoff_mutex);
1892 /* If quota was reenabled in the meantime, we have
1894 if (!sb_has_quota_loaded(sb, cnt)) {
1895 mutex_lock_nested(&toputinode[cnt]->i_mutex,
1897 toputinode[cnt]->i_flags &= ~(S_IMMUTABLE |
1898 S_NOATIME | S_NOQUOTA);
1899 truncate_inode_pages(&toputinode[cnt]->i_data,
1901 mutex_unlock(&toputinode[cnt]->i_mutex);
1902 mark_inode_dirty(toputinode[cnt]);
1904 mutex_unlock(&dqopt->dqonoff_mutex);
1907 invalidate_bdev(sb->s_bdev);
1909 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1910 if (toputinode[cnt]) {
1911 /* On remount RO, we keep the inode pointer so that we
1912 * can reenable quota on the subsequent remount RW. We
1913 * have to check 'flags' variable and not use sb_has_
1914 * function because another quotaon / quotaoff could
1915 * change global state before we got here. We refuse
1916 * to suspend quotas when there is pending delete on
1917 * the quota file... */
1918 if (!(flags & DQUOT_SUSPENDED))
1919 iput(toputinode[cnt]);
1920 else if (!toputinode[cnt]->i_nlink)
1925 EXPORT_SYMBOL(vfs_quota_disable);
1927 int vfs_quota_off(struct super_block *sb, int type, int remount)
1929 return vfs_quota_disable(sb, type, remount ? DQUOT_SUSPENDED :
1930 (DQUOT_USAGE_ENABLED | DQUOT_LIMITS_ENABLED));
1932 EXPORT_SYMBOL(vfs_quota_off);
1934 * Turn quotas on on a device
1938 * Helper function to turn quotas on when we already have the inode of
1939 * quota file and no quota information is loaded.
1941 static int vfs_load_quota_inode(struct inode *inode, int type, int format_id,
1944 struct quota_format_type *fmt = find_quota_format(format_id);
1945 struct super_block *sb = inode->i_sb;
1946 struct quota_info *dqopt = sb_dqopt(sb);
1952 if (!S_ISREG(inode->i_mode)) {
1956 if (IS_RDONLY(inode)) {
1960 if (!sb->s_op->quota_write || !sb->s_op->quota_read) {
1964 /* Usage always has to be set... */
1965 if (!(flags & DQUOT_USAGE_ENABLED)) {
1970 if (!(dqopt->flags & DQUOT_QUOTA_SYS_FILE)) {
1971 /* As we bypass the pagecache we must now flush the inode so
1972 * that we see all the changes from userspace... */
1973 write_inode_now(inode, 1);
1974 /* And now flush the block cache so that kernel sees the
1976 invalidate_bdev(sb->s_bdev);
1978 mutex_lock(&dqopt->dqonoff_mutex);
1979 if (sb_has_quota_loaded(sb, type)) {
1984 if (!(dqopt->flags & DQUOT_QUOTA_SYS_FILE)) {
1985 /* We don't want quota and atime on quota files (deadlocks
1986 * possible) Also nobody should write to the file - we use
1987 * special IO operations which ignore the immutable bit. */
1988 mutex_lock_nested(&inode->i_mutex, I_MUTEX_QUOTA);
1989 oldflags = inode->i_flags & (S_NOATIME | S_IMMUTABLE |
1991 inode->i_flags |= S_NOQUOTA | S_NOATIME | S_IMMUTABLE;
1992 mutex_unlock(&inode->i_mutex);
1994 * When S_NOQUOTA is set, remove dquot references as no more
1995 * references can be added
1997 sb->dq_op->drop(inode);
2001 dqopt->files[type] = igrab(inode);
2002 if (!dqopt->files[type])
2005 if (!fmt->qf_ops->check_quota_file(sb, type))
2008 dqopt->ops[type] = fmt->qf_ops;
2009 dqopt->info[type].dqi_format = fmt;
2010 dqopt->info[type].dqi_fmt_id = format_id;
2011 INIT_LIST_HEAD(&dqopt->info[type].dqi_dirty_list);
2012 mutex_lock(&dqopt->dqio_mutex);
2013 error = dqopt->ops[type]->read_file_info(sb, type);
2015 mutex_unlock(&dqopt->dqio_mutex);
2018 mutex_unlock(&dqopt->dqio_mutex);
2019 spin_lock(&dq_state_lock);
2020 dqopt->flags |= dquot_state_flag(flags, type);
2021 spin_unlock(&dq_state_lock);
2023 add_dquot_ref(sb, type);
2024 mutex_unlock(&dqopt->dqonoff_mutex);
2029 dqopt->files[type] = NULL;
2032 if (oldflags != -1) {
2033 mutex_lock_nested(&inode->i_mutex, I_MUTEX_QUOTA);
2034 /* Set the flags back (in the case of accidental quotaon()
2035 * on a wrong file we don't want to mess up the flags) */
2036 inode->i_flags &= ~(S_NOATIME | S_NOQUOTA | S_IMMUTABLE);
2037 inode->i_flags |= oldflags;
2038 mutex_unlock(&inode->i_mutex);
2040 mutex_unlock(&dqopt->dqonoff_mutex);
2042 put_quota_format(fmt);
2047 /* Reenable quotas on remount RW */
2048 static int vfs_quota_on_remount(struct super_block *sb, int type)
2050 struct quota_info *dqopt = sb_dqopt(sb);
2051 struct inode *inode;
2055 mutex_lock(&dqopt->dqonoff_mutex);
2056 if (!sb_has_quota_suspended(sb, type)) {
2057 mutex_unlock(&dqopt->dqonoff_mutex);
2060 inode = dqopt->files[type];
2061 dqopt->files[type] = NULL;
2062 spin_lock(&dq_state_lock);
2063 flags = dqopt->flags & dquot_state_flag(DQUOT_USAGE_ENABLED |
2064 DQUOT_LIMITS_ENABLED, type);
2065 dqopt->flags &= ~dquot_state_flag(DQUOT_STATE_FLAGS, type);
2066 spin_unlock(&dq_state_lock);
2067 mutex_unlock(&dqopt->dqonoff_mutex);
2069 flags = dquot_generic_flag(flags, type);
2070 ret = vfs_load_quota_inode(inode, type, dqopt->info[type].dqi_fmt_id,
2077 int vfs_quota_on_path(struct super_block *sb, int type, int format_id,
2080 int error = security_quota_on(path->dentry);
2083 /* Quota file not on the same filesystem? */
2084 if (path->mnt->mnt_sb != sb)
2087 error = vfs_load_quota_inode(path->dentry->d_inode, type,
2088 format_id, DQUOT_USAGE_ENABLED |
2089 DQUOT_LIMITS_ENABLED);
2092 EXPORT_SYMBOL(vfs_quota_on_path);
2094 int vfs_quota_on(struct super_block *sb, int type, int format_id, char *name,
2101 return vfs_quota_on_remount(sb, type);
2103 error = kern_path(name, LOOKUP_FOLLOW, &path);
2105 error = vfs_quota_on_path(sb, type, format_id, &path);
2110 EXPORT_SYMBOL(vfs_quota_on);
2113 * More powerful function for turning on quotas allowing setting
2114 * of individual quota flags
2116 int vfs_quota_enable(struct inode *inode, int type, int format_id,
2120 struct super_block *sb = inode->i_sb;
2121 struct quota_info *dqopt = sb_dqopt(sb);
2123 /* Just unsuspend quotas? */
2124 if (flags & DQUOT_SUSPENDED)
2125 return vfs_quota_on_remount(sb, type);
2128 /* Just updating flags needed? */
2129 if (sb_has_quota_loaded(sb, type)) {
2130 mutex_lock(&dqopt->dqonoff_mutex);
2131 /* Now do a reliable test... */
2132 if (!sb_has_quota_loaded(sb, type)) {
2133 mutex_unlock(&dqopt->dqonoff_mutex);
2136 if (flags & DQUOT_USAGE_ENABLED &&
2137 sb_has_quota_usage_enabled(sb, type)) {
2141 if (flags & DQUOT_LIMITS_ENABLED &&
2142 sb_has_quota_limits_enabled(sb, type)) {
2146 spin_lock(&dq_state_lock);
2147 sb_dqopt(sb)->flags |= dquot_state_flag(flags, type);
2148 spin_unlock(&dq_state_lock);
2150 mutex_unlock(&dqopt->dqonoff_mutex);
2155 return vfs_load_quota_inode(inode, type, format_id, flags);
2157 EXPORT_SYMBOL(vfs_quota_enable);
2160 * This function is used when filesystem needs to initialize quotas
2161 * during mount time.
2163 int vfs_quota_on_mount(struct super_block *sb, char *qf_name,
2164 int format_id, int type)
2166 struct dentry *dentry;
2169 mutex_lock(&sb->s_root->d_inode->i_mutex);
2170 dentry = lookup_one_len(qf_name, sb->s_root, strlen(qf_name));
2171 mutex_unlock(&sb->s_root->d_inode->i_mutex);
2173 return PTR_ERR(dentry);
2175 if (!dentry->d_inode) {
2180 error = security_quota_on(dentry);
2182 error = vfs_load_quota_inode(dentry->d_inode, type, format_id,
2183 DQUOT_USAGE_ENABLED | DQUOT_LIMITS_ENABLED);
2189 EXPORT_SYMBOL(vfs_quota_on_mount);
2191 /* Wrapper to turn on quotas when remounting rw */
2192 int vfs_dq_quota_on_remount(struct super_block *sb)
2197 if (!sb->s_qcop || !sb->s_qcop->quota_on)
2199 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
2200 err = sb->s_qcop->quota_on(sb, cnt, 0, NULL, 1);
2201 if (err < 0 && !ret)
2206 EXPORT_SYMBOL(vfs_dq_quota_on_remount);
2208 static inline qsize_t qbtos(qsize_t blocks)
2210 return blocks << QIF_DQBLKSIZE_BITS;
2213 static inline qsize_t stoqb(qsize_t space)
2215 return (space + QIF_DQBLKSIZE - 1) >> QIF_DQBLKSIZE_BITS;
2218 /* Generic routine for getting common part of quota structure */
2219 static void do_get_dqblk(struct dquot *dquot, struct if_dqblk *di)
2221 struct mem_dqblk *dm = &dquot->dq_dqb;
2223 spin_lock(&dq_data_lock);
2224 di->dqb_bhardlimit = stoqb(dm->dqb_bhardlimit);
2225 di->dqb_bsoftlimit = stoqb(dm->dqb_bsoftlimit);
2226 di->dqb_curspace = dm->dqb_curspace + dm->dqb_rsvspace;
2227 di->dqb_ihardlimit = dm->dqb_ihardlimit;
2228 di->dqb_isoftlimit = dm->dqb_isoftlimit;
2229 di->dqb_curinodes = dm->dqb_curinodes;
2230 di->dqb_btime = dm->dqb_btime;
2231 di->dqb_itime = dm->dqb_itime;
2232 di->dqb_valid = QIF_ALL;
2233 spin_unlock(&dq_data_lock);
2236 int vfs_get_dqblk(struct super_block *sb, int type, qid_t id,
2237 struct if_dqblk *di)
2239 struct dquot *dquot;
2241 dquot = dqget(sb, id, type);
2244 do_get_dqblk(dquot, di);
2249 EXPORT_SYMBOL(vfs_get_dqblk);
2251 /* Generic routine for setting common part of quota structure */
2252 static int do_set_dqblk(struct dquot *dquot, struct if_dqblk *di)
2254 struct mem_dqblk *dm = &dquot->dq_dqb;
2255 int check_blim = 0, check_ilim = 0;
2256 struct mem_dqinfo *dqi = &sb_dqopt(dquot->dq_sb)->info[dquot->dq_type];
2258 if ((di->dqb_valid & QIF_BLIMITS &&
2259 (di->dqb_bhardlimit > dqi->dqi_maxblimit ||
2260 di->dqb_bsoftlimit > dqi->dqi_maxblimit)) ||
2261 (di->dqb_valid & QIF_ILIMITS &&
2262 (di->dqb_ihardlimit > dqi->dqi_maxilimit ||
2263 di->dqb_isoftlimit > dqi->dqi_maxilimit)))
2266 spin_lock(&dq_data_lock);
2267 if (di->dqb_valid & QIF_SPACE) {
2268 dm->dqb_curspace = di->dqb_curspace - dm->dqb_rsvspace;
2270 __set_bit(DQ_LASTSET_B + QIF_SPACE_B, &dquot->dq_flags);
2272 if (di->dqb_valid & QIF_BLIMITS) {
2273 dm->dqb_bsoftlimit = qbtos(di->dqb_bsoftlimit);
2274 dm->dqb_bhardlimit = qbtos(di->dqb_bhardlimit);
2276 __set_bit(DQ_LASTSET_B + QIF_BLIMITS_B, &dquot->dq_flags);
2278 if (di->dqb_valid & QIF_INODES) {
2279 dm->dqb_curinodes = di->dqb_curinodes;
2281 __set_bit(DQ_LASTSET_B + QIF_INODES_B, &dquot->dq_flags);
2283 if (di->dqb_valid & QIF_ILIMITS) {
2284 dm->dqb_isoftlimit = di->dqb_isoftlimit;
2285 dm->dqb_ihardlimit = di->dqb_ihardlimit;
2287 __set_bit(DQ_LASTSET_B + QIF_ILIMITS_B, &dquot->dq_flags);
2289 if (di->dqb_valid & QIF_BTIME) {
2290 dm->dqb_btime = di->dqb_btime;
2292 __set_bit(DQ_LASTSET_B + QIF_BTIME_B, &dquot->dq_flags);
2294 if (di->dqb_valid & QIF_ITIME) {
2295 dm->dqb_itime = di->dqb_itime;
2297 __set_bit(DQ_LASTSET_B + QIF_ITIME_B, &dquot->dq_flags);
2301 if (!dm->dqb_bsoftlimit ||
2302 dm->dqb_curspace < dm->dqb_bsoftlimit) {
2304 clear_bit(DQ_BLKS_B, &dquot->dq_flags);
2305 } else if (!(di->dqb_valid & QIF_BTIME))
2306 /* Set grace only if user hasn't provided his own... */
2307 dm->dqb_btime = get_seconds() + dqi->dqi_bgrace;
2310 if (!dm->dqb_isoftlimit ||
2311 dm->dqb_curinodes < dm->dqb_isoftlimit) {
2313 clear_bit(DQ_INODES_B, &dquot->dq_flags);
2314 } else if (!(di->dqb_valid & QIF_ITIME))
2315 /* Set grace only if user hasn't provided his own... */
2316 dm->dqb_itime = get_seconds() + dqi->dqi_igrace;
2318 if (dm->dqb_bhardlimit || dm->dqb_bsoftlimit || dm->dqb_ihardlimit ||
2320 clear_bit(DQ_FAKE_B, &dquot->dq_flags);
2322 set_bit(DQ_FAKE_B, &dquot->dq_flags);
2323 spin_unlock(&dq_data_lock);
2324 mark_dquot_dirty(dquot);
2329 int vfs_set_dqblk(struct super_block *sb, int type, qid_t id,
2330 struct if_dqblk *di)
2332 struct dquot *dquot;
2335 dquot = dqget(sb, id, type);
2340 rc = do_set_dqblk(dquot, di);
2345 EXPORT_SYMBOL(vfs_set_dqblk);
2347 /* Generic routine for getting common part of quota file information */
2348 int vfs_get_dqinfo(struct super_block *sb, int type, struct if_dqinfo *ii)
2350 struct mem_dqinfo *mi;
2352 mutex_lock(&sb_dqopt(sb)->dqonoff_mutex);
2353 if (!sb_has_quota_active(sb, type)) {
2354 mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
2357 mi = sb_dqopt(sb)->info + type;
2358 spin_lock(&dq_data_lock);
2359 ii->dqi_bgrace = mi->dqi_bgrace;
2360 ii->dqi_igrace = mi->dqi_igrace;
2361 ii->dqi_flags = mi->dqi_flags & DQF_MASK;
2362 ii->dqi_valid = IIF_ALL;
2363 spin_unlock(&dq_data_lock);
2364 mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
2367 EXPORT_SYMBOL(vfs_get_dqinfo);
2369 /* Generic routine for setting common part of quota file information */
2370 int vfs_set_dqinfo(struct super_block *sb, int type, struct if_dqinfo *ii)
2372 struct mem_dqinfo *mi;
2375 mutex_lock(&sb_dqopt(sb)->dqonoff_mutex);
2376 if (!sb_has_quota_active(sb, type)) {
2380 mi = sb_dqopt(sb)->info + type;
2381 spin_lock(&dq_data_lock);
2382 if (ii->dqi_valid & IIF_BGRACE)
2383 mi->dqi_bgrace = ii->dqi_bgrace;
2384 if (ii->dqi_valid & IIF_IGRACE)
2385 mi->dqi_igrace = ii->dqi_igrace;
2386 if (ii->dqi_valid & IIF_FLAGS)
2387 mi->dqi_flags = (mi->dqi_flags & ~DQF_MASK) |
2388 (ii->dqi_flags & DQF_MASK);
2389 spin_unlock(&dq_data_lock);
2390 mark_info_dirty(sb, type);
2391 /* Force write to disk */
2392 sb->dq_op->write_info(sb, type);
2394 mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
2397 EXPORT_SYMBOL(vfs_set_dqinfo);
2399 const struct quotactl_ops vfs_quotactl_ops = {
2400 .quota_on = vfs_quota_on,
2401 .quota_off = vfs_quota_off,
2402 .quota_sync = vfs_quota_sync,
2403 .get_info = vfs_get_dqinfo,
2404 .set_info = vfs_set_dqinfo,
2405 .get_dqblk = vfs_get_dqblk,
2406 .set_dqblk = vfs_set_dqblk
2409 static ctl_table fs_dqstats_table[] = {
2411 .procname = "lookups",
2412 .data = &dqstats.lookups,
2413 .maxlen = sizeof(int),
2415 .proc_handler = proc_dointvec,
2418 .procname = "drops",
2419 .data = &dqstats.drops,
2420 .maxlen = sizeof(int),
2422 .proc_handler = proc_dointvec,
2425 .procname = "reads",
2426 .data = &dqstats.reads,
2427 .maxlen = sizeof(int),
2429 .proc_handler = proc_dointvec,
2432 .procname = "writes",
2433 .data = &dqstats.writes,
2434 .maxlen = sizeof(int),
2436 .proc_handler = proc_dointvec,
2439 .procname = "cache_hits",
2440 .data = &dqstats.cache_hits,
2441 .maxlen = sizeof(int),
2443 .proc_handler = proc_dointvec,
2446 .procname = "allocated_dquots",
2447 .data = &dqstats.allocated_dquots,
2448 .maxlen = sizeof(int),
2450 .proc_handler = proc_dointvec,
2453 .procname = "free_dquots",
2454 .data = &dqstats.free_dquots,
2455 .maxlen = sizeof(int),
2457 .proc_handler = proc_dointvec,
2460 .procname = "syncs",
2461 .data = &dqstats.syncs,
2462 .maxlen = sizeof(int),
2464 .proc_handler = proc_dointvec,
2466 #ifdef CONFIG_PRINT_QUOTA_WARNING
2468 .procname = "warnings",
2469 .data = &flag_print_warnings,
2470 .maxlen = sizeof(int),
2472 .proc_handler = proc_dointvec,
2478 static ctl_table fs_table[] = {
2480 .procname = "quota",
2482 .child = fs_dqstats_table,
2487 static ctl_table sys_table[] = {
2496 static int __init dquot_init(void)
2499 unsigned long nr_hash, order;
2501 printk(KERN_NOTICE "VFS: Disk quotas %s\n", __DQUOT_VERSION__);
2503 register_sysctl_table(sys_table);
2505 dquot_cachep = kmem_cache_create("dquot",
2506 sizeof(struct dquot), sizeof(unsigned long) * 4,
2507 (SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT|
2508 SLAB_MEM_SPREAD|SLAB_PANIC),
2512 dquot_hash = (struct hlist_head *)__get_free_pages(GFP_ATOMIC, order);
2514 panic("Cannot create dquot hash table");
2516 /* Find power-of-two hlist_heads which can fit into allocation */
2517 nr_hash = (1UL << order) * PAGE_SIZE / sizeof(struct hlist_head);
2521 } while (nr_hash >> dq_hash_bits);
2524 nr_hash = 1UL << dq_hash_bits;
2525 dq_hash_mask = nr_hash - 1;
2526 for (i = 0; i < nr_hash; i++)
2527 INIT_HLIST_HEAD(dquot_hash + i);
2529 printk("Dquot-cache hash table entries: %ld (order %ld, %ld bytes)\n",
2530 nr_hash, order, (PAGE_SIZE << order));
2532 register_shrinker(&dqcache_shrinker);
2536 module_init(dquot_init);