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
85 * and quota formats, dqstats structure containing statistics about the lists
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 static char *quotatypes[] = INITQFNAMES;
136 static struct quota_format_type *quota_formats; /* List of registered formats */
137 static struct quota_module_name module_names[] = INIT_QUOTA_MODULE_NAMES;
139 /* SLAB cache for dquot structures */
140 static struct kmem_cache *dquot_cachep;
142 int register_quota_format(struct quota_format_type *fmt)
144 spin_lock(&dq_list_lock);
145 fmt->qf_next = quota_formats;
147 spin_unlock(&dq_list_lock);
150 EXPORT_SYMBOL(register_quota_format);
152 void unregister_quota_format(struct quota_format_type *fmt)
154 struct quota_format_type **actqf;
156 spin_lock(&dq_list_lock);
157 for (actqf = "a_formats; *actqf && *actqf != fmt;
158 actqf = &(*actqf)->qf_next)
161 *actqf = (*actqf)->qf_next;
162 spin_unlock(&dq_list_lock);
164 EXPORT_SYMBOL(unregister_quota_format);
166 static struct quota_format_type *find_quota_format(int id)
168 struct quota_format_type *actqf;
170 spin_lock(&dq_list_lock);
171 for (actqf = quota_formats; actqf && actqf->qf_fmt_id != id;
172 actqf = actqf->qf_next)
174 if (!actqf || !try_module_get(actqf->qf_owner)) {
177 spin_unlock(&dq_list_lock);
179 for (qm = 0; module_names[qm].qm_fmt_id &&
180 module_names[qm].qm_fmt_id != id; qm++)
182 if (!module_names[qm].qm_fmt_id ||
183 request_module(module_names[qm].qm_mod_name))
186 spin_lock(&dq_list_lock);
187 for (actqf = quota_formats; actqf && actqf->qf_fmt_id != id;
188 actqf = actqf->qf_next)
190 if (actqf && !try_module_get(actqf->qf_owner))
193 spin_unlock(&dq_list_lock);
197 static void put_quota_format(struct quota_format_type *fmt)
199 module_put(fmt->qf_owner);
203 * Dquot List Management:
204 * The quota code uses three lists for dquot management: the inuse_list,
205 * free_dquots, and dquot_hash[] array. A single dquot structure may be
206 * on all three lists, depending on its current state.
208 * All dquots are placed to the end of inuse_list when first created, and this
209 * list is used for invalidate operation, which must look at every dquot.
211 * Unused dquots (dq_count == 0) are added to the free_dquots list when freed,
212 * and this list is searched whenever we need an available dquot. Dquots are
213 * removed from the list as soon as they are used again, and
214 * dqstats.free_dquots gives the number of dquots on the list. When
215 * dquot is invalidated it's completely released from memory.
217 * Dquots with a specific identity (device, type and id) are placed on
218 * one of the dquot_hash[] hash chains. The provides an efficient search
219 * mechanism to locate a specific dquot.
222 static LIST_HEAD(inuse_list);
223 static LIST_HEAD(free_dquots);
224 static unsigned int dq_hash_bits, dq_hash_mask;
225 static struct hlist_head *dquot_hash;
227 struct dqstats dqstats;
228 EXPORT_SYMBOL(dqstats);
230 static qsize_t inode_get_rsv_space(struct inode *inode);
231 static void __dquot_initialize(struct inode *inode, int type);
233 static inline unsigned int
234 hashfn(const struct super_block *sb, unsigned int id, int type)
238 tmp = (((unsigned long)sb>>L1_CACHE_SHIFT) ^ id) * (MAXQUOTAS - type);
239 return (tmp + (tmp >> dq_hash_bits)) & dq_hash_mask;
243 * Following list functions expect dq_list_lock to be held
245 static inline void insert_dquot_hash(struct dquot *dquot)
247 struct hlist_head *head;
248 head = dquot_hash + hashfn(dquot->dq_sb, dquot->dq_id, dquot->dq_type);
249 hlist_add_head(&dquot->dq_hash, head);
252 static inline void remove_dquot_hash(struct dquot *dquot)
254 hlist_del_init(&dquot->dq_hash);
257 static struct dquot *find_dquot(unsigned int hashent, struct super_block *sb,
258 unsigned int id, int type)
260 struct hlist_node *node;
263 hlist_for_each (node, dquot_hash+hashent) {
264 dquot = hlist_entry(node, struct dquot, dq_hash);
265 if (dquot->dq_sb == sb && dquot->dq_id == id &&
266 dquot->dq_type == type)
272 /* Add a dquot to the tail of the free list */
273 static inline void put_dquot_last(struct dquot *dquot)
275 list_add_tail(&dquot->dq_free, &free_dquots);
276 dqstats.free_dquots++;
279 static inline void remove_free_dquot(struct dquot *dquot)
281 if (list_empty(&dquot->dq_free))
283 list_del_init(&dquot->dq_free);
284 dqstats.free_dquots--;
287 static inline void put_inuse(struct dquot *dquot)
289 /* We add to the back of inuse list so we don't have to restart
290 * when traversing this list and we block */
291 list_add_tail(&dquot->dq_inuse, &inuse_list);
292 dqstats.allocated_dquots++;
295 static inline void remove_inuse(struct dquot *dquot)
297 dqstats.allocated_dquots--;
298 list_del(&dquot->dq_inuse);
301 * End of list functions needing dq_list_lock
304 static void wait_on_dquot(struct dquot *dquot)
306 mutex_lock(&dquot->dq_lock);
307 mutex_unlock(&dquot->dq_lock);
310 static inline int dquot_dirty(struct dquot *dquot)
312 return test_bit(DQ_MOD_B, &dquot->dq_flags);
315 static inline int mark_dquot_dirty(struct dquot *dquot)
317 return dquot->dq_sb->dq_op->mark_dirty(dquot);
320 int dquot_mark_dquot_dirty(struct dquot *dquot)
322 spin_lock(&dq_list_lock);
323 if (!test_and_set_bit(DQ_MOD_B, &dquot->dq_flags))
324 list_add(&dquot->dq_dirty, &sb_dqopt(dquot->dq_sb)->
325 info[dquot->dq_type].dqi_dirty_list);
326 spin_unlock(&dq_list_lock);
329 EXPORT_SYMBOL(dquot_mark_dquot_dirty);
331 /* Dirtify all the dquots - this can block when journalling */
332 static inline int mark_all_dquot_dirty(struct dquot * const *dquot)
337 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
339 /* Even in case of error we have to continue */
340 ret = mark_dquot_dirty(dquot[cnt]);
347 static inline void dqput_all(struct dquot **dquot)
351 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
355 /* This function needs dq_list_lock */
356 static inline int clear_dquot_dirty(struct dquot *dquot)
358 if (!test_and_clear_bit(DQ_MOD_B, &dquot->dq_flags))
360 list_del_init(&dquot->dq_dirty);
364 void mark_info_dirty(struct super_block *sb, int type)
366 set_bit(DQF_INFO_DIRTY_B, &sb_dqopt(sb)->info[type].dqi_flags);
368 EXPORT_SYMBOL(mark_info_dirty);
371 * Read dquot from disk and alloc space for it
374 int dquot_acquire(struct dquot *dquot)
376 int ret = 0, ret2 = 0;
377 struct quota_info *dqopt = sb_dqopt(dquot->dq_sb);
379 mutex_lock(&dquot->dq_lock);
380 mutex_lock(&dqopt->dqio_mutex);
381 if (!test_bit(DQ_READ_B, &dquot->dq_flags))
382 ret = dqopt->ops[dquot->dq_type]->read_dqblk(dquot);
385 set_bit(DQ_READ_B, &dquot->dq_flags);
386 /* Instantiate dquot if needed */
387 if (!test_bit(DQ_ACTIVE_B, &dquot->dq_flags) && !dquot->dq_off) {
388 ret = dqopt->ops[dquot->dq_type]->commit_dqblk(dquot);
389 /* Write the info if needed */
390 if (info_dirty(&dqopt->info[dquot->dq_type])) {
391 ret2 = dqopt->ops[dquot->dq_type]->write_file_info(
392 dquot->dq_sb, dquot->dq_type);
401 set_bit(DQ_ACTIVE_B, &dquot->dq_flags);
403 mutex_unlock(&dqopt->dqio_mutex);
404 mutex_unlock(&dquot->dq_lock);
407 EXPORT_SYMBOL(dquot_acquire);
410 * Write dquot to disk
412 int dquot_commit(struct dquot *dquot)
414 int ret = 0, ret2 = 0;
415 struct quota_info *dqopt = sb_dqopt(dquot->dq_sb);
417 mutex_lock(&dqopt->dqio_mutex);
418 spin_lock(&dq_list_lock);
419 if (!clear_dquot_dirty(dquot)) {
420 spin_unlock(&dq_list_lock);
423 spin_unlock(&dq_list_lock);
424 /* Inactive dquot can be only if there was error during read/init
425 * => we have better not writing it */
426 if (test_bit(DQ_ACTIVE_B, &dquot->dq_flags)) {
427 ret = dqopt->ops[dquot->dq_type]->commit_dqblk(dquot);
428 if (info_dirty(&dqopt->info[dquot->dq_type])) {
429 ret2 = dqopt->ops[dquot->dq_type]->write_file_info(
430 dquot->dq_sb, dquot->dq_type);
436 mutex_unlock(&dqopt->dqio_mutex);
439 EXPORT_SYMBOL(dquot_commit);
444 int dquot_release(struct dquot *dquot)
446 int ret = 0, ret2 = 0;
447 struct quota_info *dqopt = sb_dqopt(dquot->dq_sb);
449 mutex_lock(&dquot->dq_lock);
450 /* Check whether we are not racing with some other dqget() */
451 if (atomic_read(&dquot->dq_count) > 1)
453 mutex_lock(&dqopt->dqio_mutex);
454 if (dqopt->ops[dquot->dq_type]->release_dqblk) {
455 ret = dqopt->ops[dquot->dq_type]->release_dqblk(dquot);
457 if (info_dirty(&dqopt->info[dquot->dq_type])) {
458 ret2 = dqopt->ops[dquot->dq_type]->write_file_info(
459 dquot->dq_sb, dquot->dq_type);
464 clear_bit(DQ_ACTIVE_B, &dquot->dq_flags);
465 mutex_unlock(&dqopt->dqio_mutex);
467 mutex_unlock(&dquot->dq_lock);
470 EXPORT_SYMBOL(dquot_release);
472 void dquot_destroy(struct dquot *dquot)
474 kmem_cache_free(dquot_cachep, dquot);
476 EXPORT_SYMBOL(dquot_destroy);
478 static inline void do_destroy_dquot(struct dquot *dquot)
480 dquot->dq_sb->dq_op->destroy_dquot(dquot);
483 /* Invalidate all dquots on the list. Note that this function is called after
484 * quota is disabled and pointers from inodes removed so there cannot be new
485 * quota users. There can still be some users of quotas due to inodes being
486 * just deleted or pruned by prune_icache() (those are not attached to any
487 * list) or parallel quotactl call. We have to wait for such users.
489 static void invalidate_dquots(struct super_block *sb, int type)
491 struct dquot *dquot, *tmp;
494 spin_lock(&dq_list_lock);
495 list_for_each_entry_safe(dquot, tmp, &inuse_list, dq_inuse) {
496 if (dquot->dq_sb != sb)
498 if (dquot->dq_type != type)
500 /* Wait for dquot users */
501 if (atomic_read(&dquot->dq_count)) {
504 atomic_inc(&dquot->dq_count);
505 prepare_to_wait(&dquot->dq_wait_unused, &wait,
506 TASK_UNINTERRUPTIBLE);
507 spin_unlock(&dq_list_lock);
508 /* Once dqput() wakes us up, we know it's time to free
510 * IMPORTANT: we rely on the fact that there is always
511 * at most one process waiting for dquot to free.
512 * Otherwise dq_count would be > 1 and we would never
515 if (atomic_read(&dquot->dq_count) > 1)
517 finish_wait(&dquot->dq_wait_unused, &wait);
519 /* At this moment dquot() need not exist (it could be
520 * reclaimed by prune_dqcache(). Hence we must
525 * Quota now has no users and it has been written on last
528 remove_dquot_hash(dquot);
529 remove_free_dquot(dquot);
531 do_destroy_dquot(dquot);
533 spin_unlock(&dq_list_lock);
536 /* Call callback for every active dquot on given filesystem */
537 int dquot_scan_active(struct super_block *sb,
538 int (*fn)(struct dquot *dquot, unsigned long priv),
541 struct dquot *dquot, *old_dquot = NULL;
544 mutex_lock(&sb_dqopt(sb)->dqonoff_mutex);
545 spin_lock(&dq_list_lock);
546 list_for_each_entry(dquot, &inuse_list, dq_inuse) {
547 if (!test_bit(DQ_ACTIVE_B, &dquot->dq_flags))
549 if (dquot->dq_sb != sb)
551 /* Now we have active dquot so we can just increase use count */
552 atomic_inc(&dquot->dq_count);
554 spin_unlock(&dq_list_lock);
557 ret = fn(dquot, priv);
560 spin_lock(&dq_list_lock);
561 /* We are safe to continue now because our dquot could not
562 * be moved out of the inuse list while we hold the reference */
564 spin_unlock(&dq_list_lock);
567 mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
570 EXPORT_SYMBOL(dquot_scan_active);
572 int vfs_quota_sync(struct super_block *sb, int type, int wait)
574 struct list_head *dirty;
576 struct quota_info *dqopt = sb_dqopt(sb);
579 mutex_lock(&dqopt->dqonoff_mutex);
580 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
581 if (type != -1 && cnt != type)
583 if (!sb_has_quota_active(sb, cnt))
585 spin_lock(&dq_list_lock);
586 dirty = &dqopt->info[cnt].dqi_dirty_list;
587 while (!list_empty(dirty)) {
588 dquot = list_first_entry(dirty, struct dquot,
590 /* Dirty and inactive can be only bad dquot... */
591 if (!test_bit(DQ_ACTIVE_B, &dquot->dq_flags)) {
592 clear_dquot_dirty(dquot);
595 /* Now we have active dquot from which someone is
596 * holding reference so we can safely just increase
598 atomic_inc(&dquot->dq_count);
600 spin_unlock(&dq_list_lock);
601 sb->dq_op->write_dquot(dquot);
603 spin_lock(&dq_list_lock);
605 spin_unlock(&dq_list_lock);
608 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
609 if ((cnt == type || type == -1) && sb_has_quota_active(sb, cnt)
610 && info_dirty(&dqopt->info[cnt]))
611 sb->dq_op->write_info(sb, cnt);
612 spin_lock(&dq_list_lock);
614 spin_unlock(&dq_list_lock);
615 mutex_unlock(&dqopt->dqonoff_mutex);
617 if (!wait || (sb_dqopt(sb)->flags & DQUOT_QUOTA_SYS_FILE))
620 /* This is not very clever (and fast) but currently I don't know about
621 * any other simple way of getting quota data to disk and we must get
622 * them there for userspace to be visible... */
623 if (sb->s_op->sync_fs)
624 sb->s_op->sync_fs(sb, 1);
625 sync_blockdev(sb->s_bdev);
628 * Now when everything is written we can discard the pagecache so
629 * that userspace sees the changes.
631 mutex_lock(&sb_dqopt(sb)->dqonoff_mutex);
632 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
633 if (type != -1 && cnt != type)
635 if (!sb_has_quota_active(sb, cnt))
637 mutex_lock_nested(&sb_dqopt(sb)->files[cnt]->i_mutex,
639 truncate_inode_pages(&sb_dqopt(sb)->files[cnt]->i_data, 0);
640 mutex_unlock(&sb_dqopt(sb)->files[cnt]->i_mutex);
642 mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
646 EXPORT_SYMBOL(vfs_quota_sync);
648 /* Free unused dquots from cache */
649 static void prune_dqcache(int count)
651 struct list_head *head;
654 head = free_dquots.prev;
655 while (head != &free_dquots && count) {
656 dquot = list_entry(head, struct dquot, dq_free);
657 remove_dquot_hash(dquot);
658 remove_free_dquot(dquot);
660 do_destroy_dquot(dquot);
662 head = free_dquots.prev;
667 * This is called from kswapd when we think we need some
671 static int shrink_dqcache_memory(int nr, gfp_t gfp_mask)
674 spin_lock(&dq_list_lock);
676 spin_unlock(&dq_list_lock);
678 return (dqstats.free_dquots / 100) * sysctl_vfs_cache_pressure;
681 static struct shrinker dqcache_shrinker = {
682 .shrink = shrink_dqcache_memory,
683 .seeks = DEFAULT_SEEKS,
687 * Put reference to dquot
688 * NOTE: If you change this function please check whether dqput_blocks() works right...
690 void dqput(struct dquot *dquot)
696 #ifdef CONFIG_QUOTA_DEBUG
697 if (!atomic_read(&dquot->dq_count)) {
698 printk("VFS: dqput: trying to free free dquot\n");
699 printk("VFS: device %s, dquot of %s %d\n",
701 quotatypes[dquot->dq_type],
707 spin_lock(&dq_list_lock);
709 spin_unlock(&dq_list_lock);
711 spin_lock(&dq_list_lock);
712 if (atomic_read(&dquot->dq_count) > 1) {
713 /* We have more than one user... nothing to do */
714 atomic_dec(&dquot->dq_count);
715 /* Releasing dquot during quotaoff phase? */
716 if (!sb_has_quota_active(dquot->dq_sb, dquot->dq_type) &&
717 atomic_read(&dquot->dq_count) == 1)
718 wake_up(&dquot->dq_wait_unused);
719 spin_unlock(&dq_list_lock);
722 /* Need to release dquot? */
723 if (test_bit(DQ_ACTIVE_B, &dquot->dq_flags) && dquot_dirty(dquot)) {
724 spin_unlock(&dq_list_lock);
725 /* Commit dquot before releasing */
726 ret = dquot->dq_sb->dq_op->write_dquot(dquot);
728 printk(KERN_ERR "VFS: cannot write quota structure on "
729 "device %s (error %d). Quota may get out of "
730 "sync!\n", dquot->dq_sb->s_id, ret);
732 * We clear dirty bit anyway, so that we avoid
735 spin_lock(&dq_list_lock);
736 clear_dquot_dirty(dquot);
737 spin_unlock(&dq_list_lock);
741 /* Clear flag in case dquot was inactive (something bad happened) */
742 clear_dquot_dirty(dquot);
743 if (test_bit(DQ_ACTIVE_B, &dquot->dq_flags)) {
744 spin_unlock(&dq_list_lock);
745 dquot->dq_sb->dq_op->release_dquot(dquot);
748 atomic_dec(&dquot->dq_count);
749 #ifdef CONFIG_QUOTA_DEBUG
751 BUG_ON(!list_empty(&dquot->dq_free));
753 put_dquot_last(dquot);
754 spin_unlock(&dq_list_lock);
756 EXPORT_SYMBOL(dqput);
758 struct dquot *dquot_alloc(struct super_block *sb, int type)
760 return kmem_cache_zalloc(dquot_cachep, GFP_NOFS);
762 EXPORT_SYMBOL(dquot_alloc);
764 static struct dquot *get_empty_dquot(struct super_block *sb, int type)
768 dquot = sb->dq_op->alloc_dquot(sb, type);
772 mutex_init(&dquot->dq_lock);
773 INIT_LIST_HEAD(&dquot->dq_free);
774 INIT_LIST_HEAD(&dquot->dq_inuse);
775 INIT_HLIST_NODE(&dquot->dq_hash);
776 INIT_LIST_HEAD(&dquot->dq_dirty);
777 init_waitqueue_head(&dquot->dq_wait_unused);
779 dquot->dq_type = type;
780 atomic_set(&dquot->dq_count, 1);
786 * Get reference to dquot
788 * Locking is slightly tricky here. We are guarded from parallel quotaoff()
789 * destroying our dquot by:
790 * a) checking for quota flags under dq_list_lock and
791 * b) getting a reference to dquot before we release dq_list_lock
793 struct dquot *dqget(struct super_block *sb, unsigned int id, int type)
795 unsigned int hashent = hashfn(sb, id, type);
796 struct dquot *dquot = NULL, *empty = NULL;
798 if (!sb_has_quota_active(sb, type))
801 spin_lock(&dq_list_lock);
802 spin_lock(&dq_state_lock);
803 if (!sb_has_quota_active(sb, type)) {
804 spin_unlock(&dq_state_lock);
805 spin_unlock(&dq_list_lock);
808 spin_unlock(&dq_state_lock);
810 dquot = find_dquot(hashent, sb, id, type);
813 spin_unlock(&dq_list_lock);
814 empty = get_empty_dquot(sb, type);
816 schedule(); /* Try to wait for a moment... */
822 /* all dquots go on the inuse_list */
824 /* hash it first so it can be found */
825 insert_dquot_hash(dquot);
827 spin_unlock(&dq_list_lock);
829 if (!atomic_read(&dquot->dq_count))
830 remove_free_dquot(dquot);
831 atomic_inc(&dquot->dq_count);
832 dqstats.cache_hits++;
834 spin_unlock(&dq_list_lock);
836 /* Wait for dq_lock - after this we know that either dquot_release() is
837 * already finished or it will be canceled due to dq_count > 1 test */
838 wait_on_dquot(dquot);
839 /* Read the dquot / allocate space in quota file */
840 if (!test_bit(DQ_ACTIVE_B, &dquot->dq_flags) &&
841 sb->dq_op->acquire_dquot(dquot) < 0) {
846 #ifdef CONFIG_QUOTA_DEBUG
847 BUG_ON(!dquot->dq_sb); /* Has somebody invalidated entry under us? */
851 do_destroy_dquot(empty);
855 EXPORT_SYMBOL(dqget);
857 static int dqinit_needed(struct inode *inode, int type)
861 if (IS_NOQUOTA(inode))
864 return !inode->i_dquot[type];
865 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
866 if (!inode->i_dquot[cnt])
871 /* This routine is guarded by dqonoff_mutex mutex */
872 static void add_dquot_ref(struct super_block *sb, int type)
874 struct inode *inode, *old_inode = NULL;
875 #ifdef CONFIG_QUOTA_DEBUG
879 spin_lock(&inode_lock);
880 list_for_each_entry(inode, &sb->s_inodes, i_sb_list) {
881 if (inode->i_state & (I_FREEING|I_CLEAR|I_WILL_FREE|I_NEW))
883 #ifdef CONFIG_QUOTA_DEBUG
884 if (unlikely(inode_get_rsv_space(inode) > 0))
887 if (!atomic_read(&inode->i_writecount))
889 if (!dqinit_needed(inode, type))
893 spin_unlock(&inode_lock);
896 __dquot_initialize(inode, type);
897 /* We hold a reference to 'inode' so it couldn't have been
898 * removed from s_inodes list while we dropped the inode_lock.
899 * We cannot iput the inode now as we can be holding the last
900 * reference and we cannot iput it under inode_lock. So we
901 * keep the reference and iput it later. */
903 spin_lock(&inode_lock);
905 spin_unlock(&inode_lock);
908 #ifdef CONFIG_QUOTA_DEBUG
910 printk(KERN_WARNING "VFS (%s): Writes happened before quota"
911 " was turned on thus quota information is probably "
912 "inconsistent. Please run quotacheck(8).\n", sb->s_id);
918 * Return 0 if dqput() won't block.
919 * (note that 1 doesn't necessarily mean blocking)
921 static inline int dqput_blocks(struct dquot *dquot)
923 if (atomic_read(&dquot->dq_count) <= 1)
929 * Remove references to dquots from inode and add dquot to list for freeing
930 * if we have the last referece to dquot
931 * We can't race with anybody because we hold dqptr_sem for writing...
933 static int remove_inode_dquot_ref(struct inode *inode, int type,
934 struct list_head *tofree_head)
936 struct dquot *dquot = inode->i_dquot[type];
938 inode->i_dquot[type] = NULL;
940 if (dqput_blocks(dquot)) {
941 #ifdef CONFIG_QUOTA_DEBUG
942 if (atomic_read(&dquot->dq_count) != 1)
943 printk(KERN_WARNING "VFS: Adding dquot with dq_count %d to dispose list.\n", atomic_read(&dquot->dq_count));
945 spin_lock(&dq_list_lock);
946 /* As dquot must have currently users it can't be on
947 * the free list... */
948 list_add(&dquot->dq_free, tofree_head);
949 spin_unlock(&dq_list_lock);
953 dqput(dquot); /* We have guaranteed we won't block */
959 * Free list of dquots
960 * Dquots are removed from inodes and no new references can be got so we are
961 * the only ones holding reference
963 static void put_dquot_list(struct list_head *tofree_head)
965 struct list_head *act_head;
968 act_head = tofree_head->next;
969 while (act_head != tofree_head) {
970 dquot = list_entry(act_head, struct dquot, dq_free);
971 act_head = act_head->next;
972 /* Remove dquot from the list so we won't have problems... */
973 list_del_init(&dquot->dq_free);
978 static void remove_dquot_ref(struct super_block *sb, int type,
979 struct list_head *tofree_head)
983 spin_lock(&inode_lock);
984 list_for_each_entry(inode, &sb->s_inodes, i_sb_list) {
986 * We have to scan also I_NEW inodes because they can already
987 * have quota pointer initialized. Luckily, we need to touch
988 * only quota pointers and these have separate locking
991 if (!IS_NOQUOTA(inode))
992 remove_inode_dquot_ref(inode, type, tofree_head);
994 spin_unlock(&inode_lock);
997 /* Gather all references from inodes and drop them */
998 static void drop_dquot_ref(struct super_block *sb, int type)
1000 LIST_HEAD(tofree_head);
1003 down_write(&sb_dqopt(sb)->dqptr_sem);
1004 remove_dquot_ref(sb, type, &tofree_head);
1005 up_write(&sb_dqopt(sb)->dqptr_sem);
1006 put_dquot_list(&tofree_head);
1010 static inline void dquot_incr_inodes(struct dquot *dquot, qsize_t number)
1012 dquot->dq_dqb.dqb_curinodes += number;
1015 static inline void dquot_incr_space(struct dquot *dquot, qsize_t number)
1017 dquot->dq_dqb.dqb_curspace += number;
1020 static inline void dquot_resv_space(struct dquot *dquot, qsize_t number)
1022 dquot->dq_dqb.dqb_rsvspace += number;
1026 * Claim reserved quota space
1028 static void dquot_claim_reserved_space(struct dquot *dquot, qsize_t number)
1030 if (dquot->dq_dqb.dqb_rsvspace < number) {
1032 number = dquot->dq_dqb.dqb_rsvspace;
1034 dquot->dq_dqb.dqb_curspace += number;
1035 dquot->dq_dqb.dqb_rsvspace -= number;
1039 void dquot_free_reserved_space(struct dquot *dquot, qsize_t number)
1041 if (dquot->dq_dqb.dqb_rsvspace >= number)
1042 dquot->dq_dqb.dqb_rsvspace -= number;
1045 dquot->dq_dqb.dqb_rsvspace = 0;
1049 static void dquot_decr_inodes(struct dquot *dquot, qsize_t number)
1051 if (sb_dqopt(dquot->dq_sb)->flags & DQUOT_NEGATIVE_USAGE ||
1052 dquot->dq_dqb.dqb_curinodes >= number)
1053 dquot->dq_dqb.dqb_curinodes -= number;
1055 dquot->dq_dqb.dqb_curinodes = 0;
1056 if (dquot->dq_dqb.dqb_curinodes <= dquot->dq_dqb.dqb_isoftlimit)
1057 dquot->dq_dqb.dqb_itime = (time_t) 0;
1058 clear_bit(DQ_INODES_B, &dquot->dq_flags);
1061 static void dquot_decr_space(struct dquot *dquot, qsize_t number)
1063 if (sb_dqopt(dquot->dq_sb)->flags & DQUOT_NEGATIVE_USAGE ||
1064 dquot->dq_dqb.dqb_curspace >= number)
1065 dquot->dq_dqb.dqb_curspace -= number;
1067 dquot->dq_dqb.dqb_curspace = 0;
1068 if (dquot->dq_dqb.dqb_curspace <= dquot->dq_dqb.dqb_bsoftlimit)
1069 dquot->dq_dqb.dqb_btime = (time_t) 0;
1070 clear_bit(DQ_BLKS_B, &dquot->dq_flags);
1073 static int warning_issued(struct dquot *dquot, const int warntype)
1075 int flag = (warntype == QUOTA_NL_BHARDWARN ||
1076 warntype == QUOTA_NL_BSOFTLONGWARN) ? DQ_BLKS_B :
1077 ((warntype == QUOTA_NL_IHARDWARN ||
1078 warntype == QUOTA_NL_ISOFTLONGWARN) ? DQ_INODES_B : 0);
1082 return test_and_set_bit(flag, &dquot->dq_flags);
1085 #ifdef CONFIG_PRINT_QUOTA_WARNING
1086 static int flag_print_warnings = 1;
1088 static int need_print_warning(struct dquot *dquot)
1090 if (!flag_print_warnings)
1093 switch (dquot->dq_type) {
1095 return current_fsuid() == dquot->dq_id;
1097 return in_group_p(dquot->dq_id);
1102 /* Print warning to user which exceeded quota */
1103 static void print_warning(struct dquot *dquot, const int warntype)
1106 struct tty_struct *tty;
1108 if (warntype == QUOTA_NL_IHARDBELOW ||
1109 warntype == QUOTA_NL_ISOFTBELOW ||
1110 warntype == QUOTA_NL_BHARDBELOW ||
1111 warntype == QUOTA_NL_BSOFTBELOW || !need_print_warning(dquot))
1114 tty = get_current_tty();
1117 tty_write_message(tty, dquot->dq_sb->s_id);
1118 if (warntype == QUOTA_NL_ISOFTWARN || warntype == QUOTA_NL_BSOFTWARN)
1119 tty_write_message(tty, ": warning, ");
1121 tty_write_message(tty, ": write failed, ");
1122 tty_write_message(tty, quotatypes[dquot->dq_type]);
1124 case QUOTA_NL_IHARDWARN:
1125 msg = " file limit reached.\r\n";
1127 case QUOTA_NL_ISOFTLONGWARN:
1128 msg = " file quota exceeded too long.\r\n";
1130 case QUOTA_NL_ISOFTWARN:
1131 msg = " file quota exceeded.\r\n";
1133 case QUOTA_NL_BHARDWARN:
1134 msg = " block limit reached.\r\n";
1136 case QUOTA_NL_BSOFTLONGWARN:
1137 msg = " block quota exceeded too long.\r\n";
1139 case QUOTA_NL_BSOFTWARN:
1140 msg = " block quota exceeded.\r\n";
1143 tty_write_message(tty, msg);
1149 * Write warnings to the console and send warning messages over netlink.
1151 * Note that this function can sleep.
1153 static void flush_warnings(struct dquot *const *dquots, char *warntype)
1158 for (i = 0; i < MAXQUOTAS; i++) {
1160 if (dq && warntype[i] != QUOTA_NL_NOWARN &&
1161 !warning_issued(dq, warntype[i])) {
1162 #ifdef CONFIG_PRINT_QUOTA_WARNING
1163 print_warning(dq, warntype[i]);
1165 quota_send_warning(dq->dq_type, dq->dq_id,
1166 dq->dq_sb->s_dev, warntype[i]);
1171 static int ignore_hardlimit(struct dquot *dquot)
1173 struct mem_dqinfo *info = &sb_dqopt(dquot->dq_sb)->info[dquot->dq_type];
1175 return capable(CAP_SYS_RESOURCE) &&
1176 (info->dqi_format->qf_fmt_id != QFMT_VFS_OLD ||
1177 !(info->dqi_flags & V1_DQF_RSQUASH));
1180 /* needs dq_data_lock */
1181 static int check_idq(struct dquot *dquot, qsize_t inodes, char *warntype)
1183 qsize_t newinodes = dquot->dq_dqb.dqb_curinodes + inodes;
1185 *warntype = QUOTA_NL_NOWARN;
1186 if (!sb_has_quota_limits_enabled(dquot->dq_sb, dquot->dq_type) ||
1187 test_bit(DQ_FAKE_B, &dquot->dq_flags))
1190 if (dquot->dq_dqb.dqb_ihardlimit &&
1191 newinodes > dquot->dq_dqb.dqb_ihardlimit &&
1192 !ignore_hardlimit(dquot)) {
1193 *warntype = QUOTA_NL_IHARDWARN;
1197 if (dquot->dq_dqb.dqb_isoftlimit &&
1198 newinodes > dquot->dq_dqb.dqb_isoftlimit &&
1199 dquot->dq_dqb.dqb_itime &&
1200 get_seconds() >= dquot->dq_dqb.dqb_itime &&
1201 !ignore_hardlimit(dquot)) {
1202 *warntype = QUOTA_NL_ISOFTLONGWARN;
1206 if (dquot->dq_dqb.dqb_isoftlimit &&
1207 newinodes > dquot->dq_dqb.dqb_isoftlimit &&
1208 dquot->dq_dqb.dqb_itime == 0) {
1209 *warntype = QUOTA_NL_ISOFTWARN;
1210 dquot->dq_dqb.dqb_itime = get_seconds() +
1211 sb_dqopt(dquot->dq_sb)->info[dquot->dq_type].dqi_igrace;
1217 /* needs dq_data_lock */
1218 static int check_bdq(struct dquot *dquot, qsize_t space, int prealloc, char *warntype)
1221 struct super_block *sb = dquot->dq_sb;
1223 *warntype = QUOTA_NL_NOWARN;
1224 if (!sb_has_quota_limits_enabled(sb, dquot->dq_type) ||
1225 test_bit(DQ_FAKE_B, &dquot->dq_flags))
1228 tspace = dquot->dq_dqb.dqb_curspace + dquot->dq_dqb.dqb_rsvspace
1231 if (dquot->dq_dqb.dqb_bhardlimit &&
1232 tspace > dquot->dq_dqb.dqb_bhardlimit &&
1233 !ignore_hardlimit(dquot)) {
1235 *warntype = QUOTA_NL_BHARDWARN;
1239 if (dquot->dq_dqb.dqb_bsoftlimit &&
1240 tspace > dquot->dq_dqb.dqb_bsoftlimit &&
1241 dquot->dq_dqb.dqb_btime &&
1242 get_seconds() >= dquot->dq_dqb.dqb_btime &&
1243 !ignore_hardlimit(dquot)) {
1245 *warntype = QUOTA_NL_BSOFTLONGWARN;
1249 if (dquot->dq_dqb.dqb_bsoftlimit &&
1250 tspace > dquot->dq_dqb.dqb_bsoftlimit &&
1251 dquot->dq_dqb.dqb_btime == 0) {
1253 *warntype = QUOTA_NL_BSOFTWARN;
1254 dquot->dq_dqb.dqb_btime = get_seconds() +
1255 sb_dqopt(sb)->info[dquot->dq_type].dqi_bgrace;
1259 * We don't allow preallocation to exceed softlimit so exceeding will
1268 static int info_idq_free(struct dquot *dquot, qsize_t inodes)
1272 if (test_bit(DQ_FAKE_B, &dquot->dq_flags) ||
1273 dquot->dq_dqb.dqb_curinodes <= dquot->dq_dqb.dqb_isoftlimit ||
1274 !sb_has_quota_limits_enabled(dquot->dq_sb, dquot->dq_type))
1275 return QUOTA_NL_NOWARN;
1277 newinodes = dquot->dq_dqb.dqb_curinodes - inodes;
1278 if (newinodes <= dquot->dq_dqb.dqb_isoftlimit)
1279 return QUOTA_NL_ISOFTBELOW;
1280 if (dquot->dq_dqb.dqb_curinodes >= dquot->dq_dqb.dqb_ihardlimit &&
1281 newinodes < dquot->dq_dqb.dqb_ihardlimit)
1282 return QUOTA_NL_IHARDBELOW;
1283 return QUOTA_NL_NOWARN;
1286 static int info_bdq_free(struct dquot *dquot, qsize_t space)
1288 if (test_bit(DQ_FAKE_B, &dquot->dq_flags) ||
1289 dquot->dq_dqb.dqb_curspace <= dquot->dq_dqb.dqb_bsoftlimit)
1290 return QUOTA_NL_NOWARN;
1292 if (dquot->dq_dqb.dqb_curspace - space <= dquot->dq_dqb.dqb_bsoftlimit)
1293 return QUOTA_NL_BSOFTBELOW;
1294 if (dquot->dq_dqb.dqb_curspace >= dquot->dq_dqb.dqb_bhardlimit &&
1295 dquot->dq_dqb.dqb_curspace - space < dquot->dq_dqb.dqb_bhardlimit)
1296 return QUOTA_NL_BHARDBELOW;
1297 return QUOTA_NL_NOWARN;
1301 * Initialize quota pointers in inode
1303 * We do things in a bit complicated way but by that we avoid calling
1304 * dqget() and thus filesystem callbacks under dqptr_sem.
1306 * It is better to call this function outside of any transaction as it
1307 * might need a lot of space in journal for dquot structure allocation.
1309 static void __dquot_initialize(struct inode *inode, int type)
1311 unsigned int id = 0;
1313 struct dquot *got[MAXQUOTAS];
1314 struct super_block *sb = inode->i_sb;
1317 /* First test before acquiring mutex - solves deadlocks when we
1318 * re-enter the quota code and are already holding the mutex */
1319 if (!sb_any_quota_active(inode->i_sb) || IS_NOQUOTA(inode))
1322 /* First get references to structures we might need. */
1323 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1325 if (type != -1 && cnt != type)
1335 got[cnt] = dqget(sb, id, cnt);
1338 down_write(&sb_dqopt(sb)->dqptr_sem);
1339 if (IS_NOQUOTA(inode))
1341 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1342 if (type != -1 && cnt != type)
1344 /* Avoid races with quotaoff() */
1345 if (!sb_has_quota_active(sb, cnt))
1347 if (!inode->i_dquot[cnt]) {
1348 inode->i_dquot[cnt] = got[cnt];
1351 * Make quota reservation system happy if someone
1352 * did a write before quota was turned on
1354 rsv = inode_get_rsv_space(inode);
1356 dquot_resv_space(inode->i_dquot[cnt], rsv);
1360 up_write(&sb_dqopt(sb)->dqptr_sem);
1361 /* Drop unused references */
1365 void dquot_initialize(struct inode *inode)
1367 __dquot_initialize(inode, -1);
1369 EXPORT_SYMBOL(dquot_initialize);
1372 * Release all quotas referenced by inode
1374 static void __dquot_drop(struct inode *inode)
1377 struct dquot *put[MAXQUOTAS];
1379 down_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
1380 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1381 put[cnt] = inode->i_dquot[cnt];
1382 inode->i_dquot[cnt] = NULL;
1384 up_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
1388 void dquot_drop(struct inode *inode)
1392 if (IS_NOQUOTA(inode))
1396 * Test before calling to rule out calls from proc and such
1397 * where we are not allowed to block. Note that this is
1398 * actually reliable test even without the lock - the caller
1399 * must assure that nobody can come after the DQUOT_DROP and
1400 * add quota pointers back anyway.
1402 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1403 if (inode->i_dquot[cnt])
1407 if (cnt < MAXQUOTAS)
1408 __dquot_drop(inode);
1410 EXPORT_SYMBOL(dquot_drop);
1413 * inode_reserved_space is managed internally by quota, and protected by
1414 * i_lock similar to i_blocks+i_bytes.
1416 static qsize_t *inode_reserved_space(struct inode * inode)
1418 /* Filesystem must explicitly define it's own method in order to use
1419 * quota reservation interface */
1420 BUG_ON(!inode->i_sb->dq_op->get_reserved_space);
1421 return inode->i_sb->dq_op->get_reserved_space(inode);
1424 void inode_add_rsv_space(struct inode *inode, qsize_t number)
1426 spin_lock(&inode->i_lock);
1427 *inode_reserved_space(inode) += number;
1428 spin_unlock(&inode->i_lock);
1430 EXPORT_SYMBOL(inode_add_rsv_space);
1432 void inode_claim_rsv_space(struct inode *inode, qsize_t number)
1434 spin_lock(&inode->i_lock);
1435 *inode_reserved_space(inode) -= number;
1436 __inode_add_bytes(inode, number);
1437 spin_unlock(&inode->i_lock);
1439 EXPORT_SYMBOL(inode_claim_rsv_space);
1441 void inode_sub_rsv_space(struct inode *inode, qsize_t number)
1443 spin_lock(&inode->i_lock);
1444 *inode_reserved_space(inode) -= number;
1445 spin_unlock(&inode->i_lock);
1447 EXPORT_SYMBOL(inode_sub_rsv_space);
1449 static qsize_t inode_get_rsv_space(struct inode *inode)
1453 if (!inode->i_sb->dq_op->get_reserved_space)
1455 spin_lock(&inode->i_lock);
1456 ret = *inode_reserved_space(inode);
1457 spin_unlock(&inode->i_lock);
1461 static void inode_incr_space(struct inode *inode, qsize_t number,
1465 inode_add_rsv_space(inode, number);
1467 inode_add_bytes(inode, number);
1470 static void inode_decr_space(struct inode *inode, qsize_t number, int reserve)
1473 inode_sub_rsv_space(inode, number);
1475 inode_sub_bytes(inode, number);
1479 * This functions updates i_blocks+i_bytes fields and quota information
1480 * (together with appropriate checks).
1482 * NOTE: We absolutely rely on the fact that caller dirties the inode
1483 * (usually helpers in quotaops.h care about this) and holds a handle for
1484 * the current transaction so that dquot write and inode write go into the
1489 * This operation can block, but only after everything is updated
1491 int __dquot_alloc_space(struct inode *inode, qsize_t number, int flags)
1494 char warntype[MAXQUOTAS];
1495 int warn = flags & DQUOT_SPACE_WARN;
1496 int reserve = flags & DQUOT_SPACE_RESERVE;
1499 * First test before acquiring mutex - solves deadlocks when we
1500 * re-enter the quota code and are already holding the mutex
1502 if (!sb_any_quota_active(inode->i_sb) || IS_NOQUOTA(inode)) {
1503 inode_incr_space(inode, number, reserve);
1507 down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1508 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1509 warntype[cnt] = QUOTA_NL_NOWARN;
1511 spin_lock(&dq_data_lock);
1512 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1513 if (!inode->i_dquot[cnt])
1515 ret = check_bdq(inode->i_dquot[cnt], number, !warn,
1518 spin_unlock(&dq_data_lock);
1519 goto out_flush_warn;
1522 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1523 if (!inode->i_dquot[cnt])
1526 dquot_resv_space(inode->i_dquot[cnt], number);
1528 dquot_incr_space(inode->i_dquot[cnt], number);
1530 inode_incr_space(inode, number, reserve);
1531 spin_unlock(&dq_data_lock);
1534 goto out_flush_warn;
1535 mark_all_dquot_dirty(inode->i_dquot);
1537 flush_warnings(inode->i_dquot, warntype);
1538 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1542 EXPORT_SYMBOL(__dquot_alloc_space);
1545 * This operation can block, but only after everything is updated
1547 int dquot_alloc_inode(const struct inode *inode)
1550 char warntype[MAXQUOTAS];
1552 /* First test before acquiring mutex - solves deadlocks when we
1553 * re-enter the quota code and are already holding the mutex */
1554 if (!sb_any_quota_active(inode->i_sb) || IS_NOQUOTA(inode))
1556 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1557 warntype[cnt] = QUOTA_NL_NOWARN;
1558 down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1559 spin_lock(&dq_data_lock);
1560 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1561 if (!inode->i_dquot[cnt])
1563 ret = check_idq(inode->i_dquot[cnt], 1, warntype + cnt);
1568 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1569 if (!inode->i_dquot[cnt])
1571 dquot_incr_inodes(inode->i_dquot[cnt], 1);
1575 spin_unlock(&dq_data_lock);
1577 mark_all_dquot_dirty(inode->i_dquot);
1578 flush_warnings(inode->i_dquot, warntype);
1579 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1582 EXPORT_SYMBOL(dquot_alloc_inode);
1585 * Convert in-memory reserved quotas to real consumed quotas
1587 int dquot_claim_space_nodirty(struct inode *inode, qsize_t number)
1591 if (!sb_any_quota_active(inode->i_sb) || IS_NOQUOTA(inode)) {
1592 inode_claim_rsv_space(inode, number);
1596 down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1597 spin_lock(&dq_data_lock);
1598 /* Claim reserved quotas to allocated quotas */
1599 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1600 if (inode->i_dquot[cnt])
1601 dquot_claim_reserved_space(inode->i_dquot[cnt],
1604 /* Update inode bytes */
1605 inode_claim_rsv_space(inode, number);
1606 spin_unlock(&dq_data_lock);
1607 mark_all_dquot_dirty(inode->i_dquot);
1608 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1611 EXPORT_SYMBOL(dquot_claim_space_nodirty);
1614 * This operation can block, but only after everything is updated
1616 void __dquot_free_space(struct inode *inode, qsize_t number, int flags)
1619 char warntype[MAXQUOTAS];
1620 int reserve = flags & DQUOT_SPACE_RESERVE;
1622 /* First test before acquiring mutex - solves deadlocks when we
1623 * re-enter the quota code and are already holding the mutex */
1624 if (!sb_any_quota_active(inode->i_sb) || IS_NOQUOTA(inode)) {
1625 inode_decr_space(inode, number, reserve);
1629 down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1630 spin_lock(&dq_data_lock);
1631 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1632 if (!inode->i_dquot[cnt])
1634 warntype[cnt] = info_bdq_free(inode->i_dquot[cnt], number);
1636 dquot_free_reserved_space(inode->i_dquot[cnt], number);
1638 dquot_decr_space(inode->i_dquot[cnt], number);
1640 inode_decr_space(inode, number, reserve);
1641 spin_unlock(&dq_data_lock);
1645 mark_all_dquot_dirty(inode->i_dquot);
1647 flush_warnings(inode->i_dquot, warntype);
1648 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1650 EXPORT_SYMBOL(__dquot_free_space);
1653 * This operation can block, but only after everything is updated
1655 void dquot_free_inode(const struct inode *inode)
1658 char warntype[MAXQUOTAS];
1660 /* First test before acquiring mutex - solves deadlocks when we
1661 * re-enter the quota code and are already holding the mutex */
1662 if (!sb_any_quota_active(inode->i_sb) || IS_NOQUOTA(inode))
1665 down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1666 spin_lock(&dq_data_lock);
1667 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1668 if (!inode->i_dquot[cnt])
1670 warntype[cnt] = info_idq_free(inode->i_dquot[cnt], 1);
1671 dquot_decr_inodes(inode->i_dquot[cnt], 1);
1673 spin_unlock(&dq_data_lock);
1674 mark_all_dquot_dirty(inode->i_dquot);
1675 flush_warnings(inode->i_dquot, warntype);
1676 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1678 EXPORT_SYMBOL(dquot_free_inode);
1681 * Transfer the number of inode and blocks from one diskquota to an other.
1683 * This operation can block, but only after everything is updated
1684 * A transaction must be started when entering this function.
1686 static int __dquot_transfer(struct inode *inode, qid_t *chid, unsigned long mask)
1688 qsize_t space, cur_space;
1689 qsize_t rsv_space = 0;
1690 struct dquot *transfer_from[MAXQUOTAS];
1691 struct dquot *transfer_to[MAXQUOTAS];
1693 char warntype_to[MAXQUOTAS];
1694 char warntype_from_inodes[MAXQUOTAS], warntype_from_space[MAXQUOTAS];
1696 /* First test before acquiring mutex - solves deadlocks when we
1697 * re-enter the quota code and are already holding the mutex */
1698 if (IS_NOQUOTA(inode))
1700 /* Initialize the arrays */
1701 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1702 transfer_from[cnt] = NULL;
1703 transfer_to[cnt] = NULL;
1704 warntype_to[cnt] = QUOTA_NL_NOWARN;
1706 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1707 if (mask & (1 << cnt))
1708 transfer_to[cnt] = dqget(inode->i_sb, chid[cnt], cnt);
1710 down_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
1711 if (IS_NOQUOTA(inode)) { /* File without quota accounting? */
1712 up_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
1715 spin_lock(&dq_data_lock);
1716 cur_space = inode_get_bytes(inode);
1717 rsv_space = inode_get_rsv_space(inode);
1718 space = cur_space + rsv_space;
1719 /* Build the transfer_from list and check the limits */
1720 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1721 if (!transfer_to[cnt])
1723 transfer_from[cnt] = inode->i_dquot[cnt];
1724 ret = check_idq(transfer_to[cnt], 1, warntype_to + cnt);
1727 ret = check_bdq(transfer_to[cnt], space, 0, warntype_to + cnt);
1733 * Finally perform the needed transfer from transfer_from to transfer_to
1735 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1737 * Skip changes for same uid or gid or for turned off quota-type.
1739 if (!transfer_to[cnt])
1742 /* Due to IO error we might not have transfer_from[] structure */
1743 if (transfer_from[cnt]) {
1744 warntype_from_inodes[cnt] =
1745 info_idq_free(transfer_from[cnt], 1);
1746 warntype_from_space[cnt] =
1747 info_bdq_free(transfer_from[cnt], space);
1748 dquot_decr_inodes(transfer_from[cnt], 1);
1749 dquot_decr_space(transfer_from[cnt], cur_space);
1750 dquot_free_reserved_space(transfer_from[cnt],
1754 dquot_incr_inodes(transfer_to[cnt], 1);
1755 dquot_incr_space(transfer_to[cnt], cur_space);
1756 dquot_resv_space(transfer_to[cnt], rsv_space);
1758 inode->i_dquot[cnt] = transfer_to[cnt];
1760 spin_unlock(&dq_data_lock);
1761 up_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
1763 mark_all_dquot_dirty(transfer_from);
1764 mark_all_dquot_dirty(transfer_to);
1765 /* The reference we got is transferred to the inode */
1766 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1767 transfer_to[cnt] = NULL;
1769 flush_warnings(transfer_to, warntype_to);
1770 flush_warnings(transfer_from, warntype_from_inodes);
1771 flush_warnings(transfer_from, warntype_from_space);
1773 dqput_all(transfer_from);
1774 dqput_all(transfer_to);
1777 spin_unlock(&dq_data_lock);
1778 up_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
1779 /* Clear dquot pointers we don't want to dqput() */
1780 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1781 transfer_from[cnt] = NULL;
1785 /* Wrapper for transferring ownership of an inode for uid/gid only
1786 * Called from FSXXX_setattr()
1788 int dquot_transfer(struct inode *inode, struct iattr *iattr)
1790 qid_t chid[MAXQUOTAS];
1791 unsigned long mask = 0;
1793 if (iattr->ia_valid & ATTR_UID && iattr->ia_uid != inode->i_uid) {
1794 mask |= 1 << USRQUOTA;
1795 chid[USRQUOTA] = iattr->ia_uid;
1797 if (iattr->ia_valid & ATTR_GID && iattr->ia_gid != inode->i_gid) {
1798 mask |= 1 << GRPQUOTA;
1799 chid[GRPQUOTA] = iattr->ia_gid;
1801 if (sb_any_quota_active(inode->i_sb) && !IS_NOQUOTA(inode)) {
1802 dquot_initialize(inode);
1803 return __dquot_transfer(inode, chid, mask);
1807 EXPORT_SYMBOL(dquot_transfer);
1810 * Write info of quota file to disk
1812 int dquot_commit_info(struct super_block *sb, int type)
1815 struct quota_info *dqopt = sb_dqopt(sb);
1817 mutex_lock(&dqopt->dqio_mutex);
1818 ret = dqopt->ops[type]->write_file_info(sb, type);
1819 mutex_unlock(&dqopt->dqio_mutex);
1822 EXPORT_SYMBOL(dquot_commit_info);
1825 * Definitions of diskquota operations.
1827 const struct dquot_operations dquot_operations = {
1828 .write_dquot = dquot_commit,
1829 .acquire_dquot = dquot_acquire,
1830 .release_dquot = dquot_release,
1831 .mark_dirty = dquot_mark_dquot_dirty,
1832 .write_info = dquot_commit_info,
1833 .alloc_dquot = dquot_alloc,
1834 .destroy_dquot = dquot_destroy,
1838 * Generic helper for ->open on filesystems supporting disk quotas.
1840 int dquot_file_open(struct inode *inode, struct file *file)
1844 error = generic_file_open(inode, file);
1845 if (!error && (file->f_mode & FMODE_WRITE))
1846 dquot_initialize(inode);
1849 EXPORT_SYMBOL(dquot_file_open);
1852 * Turn quota off on a device. type == -1 ==> quotaoff for all types (umount)
1854 int vfs_quota_disable(struct super_block *sb, int type, unsigned int flags)
1857 struct quota_info *dqopt = sb_dqopt(sb);
1858 struct inode *toputinode[MAXQUOTAS];
1860 /* Cannot turn off usage accounting without turning off limits, or
1861 * suspend quotas and simultaneously turn quotas off. */
1862 if ((flags & DQUOT_USAGE_ENABLED && !(flags & DQUOT_LIMITS_ENABLED))
1863 || (flags & DQUOT_SUSPENDED && flags & (DQUOT_LIMITS_ENABLED |
1864 DQUOT_USAGE_ENABLED)))
1867 /* We need to serialize quota_off() for device */
1868 mutex_lock(&dqopt->dqonoff_mutex);
1871 * Skip everything if there's nothing to do. We have to do this because
1872 * sometimes we are called when fill_super() failed and calling
1873 * sync_fs() in such cases does no good.
1875 if (!sb_any_quota_loaded(sb)) {
1876 mutex_unlock(&dqopt->dqonoff_mutex);
1879 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1880 toputinode[cnt] = NULL;
1881 if (type != -1 && cnt != type)
1883 if (!sb_has_quota_loaded(sb, cnt))
1886 if (flags & DQUOT_SUSPENDED) {
1887 spin_lock(&dq_state_lock);
1889 dquot_state_flag(DQUOT_SUSPENDED, cnt);
1890 spin_unlock(&dq_state_lock);
1892 spin_lock(&dq_state_lock);
1893 dqopt->flags &= ~dquot_state_flag(flags, cnt);
1894 /* Turning off suspended quotas? */
1895 if (!sb_has_quota_loaded(sb, cnt) &&
1896 sb_has_quota_suspended(sb, cnt)) {
1897 dqopt->flags &= ~dquot_state_flag(
1898 DQUOT_SUSPENDED, cnt);
1899 spin_unlock(&dq_state_lock);
1900 iput(dqopt->files[cnt]);
1901 dqopt->files[cnt] = NULL;
1904 spin_unlock(&dq_state_lock);
1907 /* We still have to keep quota loaded? */
1908 if (sb_has_quota_loaded(sb, cnt) && !(flags & DQUOT_SUSPENDED))
1911 /* Note: these are blocking operations */
1912 drop_dquot_ref(sb, cnt);
1913 invalidate_dquots(sb, cnt);
1915 * Now all dquots should be invalidated, all writes done so we
1916 * should be only users of the info. No locks needed.
1918 if (info_dirty(&dqopt->info[cnt]))
1919 sb->dq_op->write_info(sb, cnt);
1920 if (dqopt->ops[cnt]->free_file_info)
1921 dqopt->ops[cnt]->free_file_info(sb, cnt);
1922 put_quota_format(dqopt->info[cnt].dqi_format);
1924 toputinode[cnt] = dqopt->files[cnt];
1925 if (!sb_has_quota_loaded(sb, cnt))
1926 dqopt->files[cnt] = NULL;
1927 dqopt->info[cnt].dqi_flags = 0;
1928 dqopt->info[cnt].dqi_igrace = 0;
1929 dqopt->info[cnt].dqi_bgrace = 0;
1930 dqopt->ops[cnt] = NULL;
1932 mutex_unlock(&dqopt->dqonoff_mutex);
1934 /* Skip syncing and setting flags if quota files are hidden */
1935 if (dqopt->flags & DQUOT_QUOTA_SYS_FILE)
1938 /* Sync the superblock so that buffers with quota data are written to
1939 * disk (and so userspace sees correct data afterwards). */
1940 if (sb->s_op->sync_fs)
1941 sb->s_op->sync_fs(sb, 1);
1942 sync_blockdev(sb->s_bdev);
1943 /* Now the quota files are just ordinary files and we can set the
1944 * inode flags back. Moreover we discard the pagecache so that
1945 * userspace sees the writes we did bypassing the pagecache. We
1946 * must also discard the blockdev buffers so that we see the
1947 * changes done by userspace on the next quotaon() */
1948 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1949 if (toputinode[cnt]) {
1950 mutex_lock(&dqopt->dqonoff_mutex);
1951 /* If quota was reenabled in the meantime, we have
1953 if (!sb_has_quota_loaded(sb, cnt)) {
1954 mutex_lock_nested(&toputinode[cnt]->i_mutex,
1956 toputinode[cnt]->i_flags &= ~(S_IMMUTABLE |
1957 S_NOATIME | S_NOQUOTA);
1958 truncate_inode_pages(&toputinode[cnt]->i_data,
1960 mutex_unlock(&toputinode[cnt]->i_mutex);
1961 mark_inode_dirty(toputinode[cnt]);
1963 mutex_unlock(&dqopt->dqonoff_mutex);
1966 invalidate_bdev(sb->s_bdev);
1968 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1969 if (toputinode[cnt]) {
1970 /* On remount RO, we keep the inode pointer so that we
1971 * can reenable quota on the subsequent remount RW. We
1972 * have to check 'flags' variable and not use sb_has_
1973 * function because another quotaon / quotaoff could
1974 * change global state before we got here. We refuse
1975 * to suspend quotas when there is pending delete on
1976 * the quota file... */
1977 if (!(flags & DQUOT_SUSPENDED))
1978 iput(toputinode[cnt]);
1979 else if (!toputinode[cnt]->i_nlink)
1984 EXPORT_SYMBOL(vfs_quota_disable);
1986 int vfs_quota_off(struct super_block *sb, int type, int remount)
1988 return vfs_quota_disable(sb, type, remount ? DQUOT_SUSPENDED :
1989 (DQUOT_USAGE_ENABLED | DQUOT_LIMITS_ENABLED));
1991 EXPORT_SYMBOL(vfs_quota_off);
1993 * Turn quotas on on a device
1997 * Helper function to turn quotas on when we already have the inode of
1998 * quota file and no quota information is loaded.
2000 static int vfs_load_quota_inode(struct inode *inode, int type, int format_id,
2003 struct quota_format_type *fmt = find_quota_format(format_id);
2004 struct super_block *sb = inode->i_sb;
2005 struct quota_info *dqopt = sb_dqopt(sb);
2011 if (!S_ISREG(inode->i_mode)) {
2015 if (IS_RDONLY(inode)) {
2019 if (!sb->s_op->quota_write || !sb->s_op->quota_read) {
2023 /* Usage always has to be set... */
2024 if (!(flags & DQUOT_USAGE_ENABLED)) {
2029 if (!(dqopt->flags & DQUOT_QUOTA_SYS_FILE)) {
2030 /* As we bypass the pagecache we must now flush all the
2031 * dirty data and invalidate caches so that kernel sees
2032 * changes from userspace. It is not enough to just flush
2033 * the quota file since if blocksize < pagesize, invalidation
2034 * of the cache could fail because of other unrelated dirty
2036 sync_filesystem(sb);
2037 invalidate_bdev(sb->s_bdev);
2039 mutex_lock(&dqopt->dqonoff_mutex);
2040 if (sb_has_quota_loaded(sb, type)) {
2045 if (!(dqopt->flags & DQUOT_QUOTA_SYS_FILE)) {
2046 /* We don't want quota and atime on quota files (deadlocks
2047 * possible) Also nobody should write to the file - we use
2048 * special IO operations which ignore the immutable bit. */
2049 mutex_lock_nested(&inode->i_mutex, I_MUTEX_QUOTA);
2050 oldflags = inode->i_flags & (S_NOATIME | S_IMMUTABLE |
2052 inode->i_flags |= S_NOQUOTA | S_NOATIME | S_IMMUTABLE;
2053 mutex_unlock(&inode->i_mutex);
2055 * When S_NOQUOTA is set, remove dquot references as no more
2056 * references can be added
2058 __dquot_drop(inode);
2062 dqopt->files[type] = igrab(inode);
2063 if (!dqopt->files[type])
2066 if (!fmt->qf_ops->check_quota_file(sb, type))
2069 dqopt->ops[type] = fmt->qf_ops;
2070 dqopt->info[type].dqi_format = fmt;
2071 dqopt->info[type].dqi_fmt_id = format_id;
2072 INIT_LIST_HEAD(&dqopt->info[type].dqi_dirty_list);
2073 mutex_lock(&dqopt->dqio_mutex);
2074 error = dqopt->ops[type]->read_file_info(sb, type);
2076 mutex_unlock(&dqopt->dqio_mutex);
2079 mutex_unlock(&dqopt->dqio_mutex);
2080 spin_lock(&dq_state_lock);
2081 dqopt->flags |= dquot_state_flag(flags, type);
2082 spin_unlock(&dq_state_lock);
2084 add_dquot_ref(sb, type);
2085 mutex_unlock(&dqopt->dqonoff_mutex);
2090 dqopt->files[type] = NULL;
2093 if (oldflags != -1) {
2094 mutex_lock_nested(&inode->i_mutex, I_MUTEX_QUOTA);
2095 /* Set the flags back (in the case of accidental quotaon()
2096 * on a wrong file we don't want to mess up the flags) */
2097 inode->i_flags &= ~(S_NOATIME | S_NOQUOTA | S_IMMUTABLE);
2098 inode->i_flags |= oldflags;
2099 mutex_unlock(&inode->i_mutex);
2101 mutex_unlock(&dqopt->dqonoff_mutex);
2103 put_quota_format(fmt);
2108 /* Reenable quotas on remount RW */
2109 static int vfs_quota_on_remount(struct super_block *sb, int type)
2111 struct quota_info *dqopt = sb_dqopt(sb);
2112 struct inode *inode;
2116 mutex_lock(&dqopt->dqonoff_mutex);
2117 if (!sb_has_quota_suspended(sb, type)) {
2118 mutex_unlock(&dqopt->dqonoff_mutex);
2121 inode = dqopt->files[type];
2122 dqopt->files[type] = NULL;
2123 spin_lock(&dq_state_lock);
2124 flags = dqopt->flags & dquot_state_flag(DQUOT_USAGE_ENABLED |
2125 DQUOT_LIMITS_ENABLED, type);
2126 dqopt->flags &= ~dquot_state_flag(DQUOT_STATE_FLAGS, type);
2127 spin_unlock(&dq_state_lock);
2128 mutex_unlock(&dqopt->dqonoff_mutex);
2130 flags = dquot_generic_flag(flags, type);
2131 ret = vfs_load_quota_inode(inode, type, dqopt->info[type].dqi_fmt_id,
2138 int vfs_quota_on_path(struct super_block *sb, int type, int format_id,
2141 int error = security_quota_on(path->dentry);
2144 /* Quota file not on the same filesystem? */
2145 if (path->mnt->mnt_sb != sb)
2148 error = vfs_load_quota_inode(path->dentry->d_inode, type,
2149 format_id, DQUOT_USAGE_ENABLED |
2150 DQUOT_LIMITS_ENABLED);
2153 EXPORT_SYMBOL(vfs_quota_on_path);
2155 int vfs_quota_on(struct super_block *sb, int type, int format_id, char *name,
2162 return vfs_quota_on_remount(sb, type);
2164 error = kern_path(name, LOOKUP_FOLLOW, &path);
2166 error = vfs_quota_on_path(sb, type, format_id, &path);
2171 EXPORT_SYMBOL(vfs_quota_on);
2174 * More powerful function for turning on quotas allowing setting
2175 * of individual quota flags
2177 int vfs_quota_enable(struct inode *inode, int type, int format_id,
2181 struct super_block *sb = inode->i_sb;
2182 struct quota_info *dqopt = sb_dqopt(sb);
2184 /* Just unsuspend quotas? */
2185 if (flags & DQUOT_SUSPENDED)
2186 return vfs_quota_on_remount(sb, type);
2189 /* Just updating flags needed? */
2190 if (sb_has_quota_loaded(sb, type)) {
2191 mutex_lock(&dqopt->dqonoff_mutex);
2192 /* Now do a reliable test... */
2193 if (!sb_has_quota_loaded(sb, type)) {
2194 mutex_unlock(&dqopt->dqonoff_mutex);
2197 if (flags & DQUOT_USAGE_ENABLED &&
2198 sb_has_quota_usage_enabled(sb, type)) {
2202 if (flags & DQUOT_LIMITS_ENABLED &&
2203 sb_has_quota_limits_enabled(sb, type)) {
2207 spin_lock(&dq_state_lock);
2208 sb_dqopt(sb)->flags |= dquot_state_flag(flags, type);
2209 spin_unlock(&dq_state_lock);
2211 mutex_unlock(&dqopt->dqonoff_mutex);
2216 return vfs_load_quota_inode(inode, type, format_id, flags);
2218 EXPORT_SYMBOL(vfs_quota_enable);
2221 * This function is used when filesystem needs to initialize quotas
2222 * during mount time.
2224 int vfs_quota_on_mount(struct super_block *sb, char *qf_name,
2225 int format_id, int type)
2227 struct dentry *dentry;
2230 mutex_lock(&sb->s_root->d_inode->i_mutex);
2231 dentry = lookup_one_len(qf_name, sb->s_root, strlen(qf_name));
2232 mutex_unlock(&sb->s_root->d_inode->i_mutex);
2234 return PTR_ERR(dentry);
2236 if (!dentry->d_inode) {
2241 error = security_quota_on(dentry);
2243 error = vfs_load_quota_inode(dentry->d_inode, type, format_id,
2244 DQUOT_USAGE_ENABLED | DQUOT_LIMITS_ENABLED);
2250 EXPORT_SYMBOL(vfs_quota_on_mount);
2252 /* Wrapper to turn on quotas when remounting rw */
2253 int vfs_dq_quota_on_remount(struct super_block *sb)
2258 if (!sb->s_qcop || !sb->s_qcop->quota_on)
2260 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
2261 err = sb->s_qcop->quota_on(sb, cnt, 0, NULL, 1);
2262 if (err < 0 && !ret)
2267 EXPORT_SYMBOL(vfs_dq_quota_on_remount);
2269 static inline qsize_t qbtos(qsize_t blocks)
2271 return blocks << QIF_DQBLKSIZE_BITS;
2274 static inline qsize_t stoqb(qsize_t space)
2276 return (space + QIF_DQBLKSIZE - 1) >> QIF_DQBLKSIZE_BITS;
2279 /* Generic routine for getting common part of quota structure */
2280 static void do_get_dqblk(struct dquot *dquot, struct if_dqblk *di)
2282 struct mem_dqblk *dm = &dquot->dq_dqb;
2284 spin_lock(&dq_data_lock);
2285 di->dqb_bhardlimit = stoqb(dm->dqb_bhardlimit);
2286 di->dqb_bsoftlimit = stoqb(dm->dqb_bsoftlimit);
2287 di->dqb_curspace = dm->dqb_curspace + dm->dqb_rsvspace;
2288 di->dqb_ihardlimit = dm->dqb_ihardlimit;
2289 di->dqb_isoftlimit = dm->dqb_isoftlimit;
2290 di->dqb_curinodes = dm->dqb_curinodes;
2291 di->dqb_btime = dm->dqb_btime;
2292 di->dqb_itime = dm->dqb_itime;
2293 di->dqb_valid = QIF_ALL;
2294 spin_unlock(&dq_data_lock);
2297 int vfs_get_dqblk(struct super_block *sb, int type, qid_t id,
2298 struct if_dqblk *di)
2300 struct dquot *dquot;
2302 dquot = dqget(sb, id, type);
2305 do_get_dqblk(dquot, di);
2310 EXPORT_SYMBOL(vfs_get_dqblk);
2312 /* Generic routine for setting common part of quota structure */
2313 static int do_set_dqblk(struct dquot *dquot, struct if_dqblk *di)
2315 struct mem_dqblk *dm = &dquot->dq_dqb;
2316 int check_blim = 0, check_ilim = 0;
2317 struct mem_dqinfo *dqi = &sb_dqopt(dquot->dq_sb)->info[dquot->dq_type];
2319 if ((di->dqb_valid & QIF_BLIMITS &&
2320 (di->dqb_bhardlimit > dqi->dqi_maxblimit ||
2321 di->dqb_bsoftlimit > dqi->dqi_maxblimit)) ||
2322 (di->dqb_valid & QIF_ILIMITS &&
2323 (di->dqb_ihardlimit > dqi->dqi_maxilimit ||
2324 di->dqb_isoftlimit > dqi->dqi_maxilimit)))
2327 spin_lock(&dq_data_lock);
2328 if (di->dqb_valid & QIF_SPACE) {
2329 dm->dqb_curspace = di->dqb_curspace - dm->dqb_rsvspace;
2331 set_bit(DQ_LASTSET_B + QIF_SPACE_B, &dquot->dq_flags);
2333 if (di->dqb_valid & QIF_BLIMITS) {
2334 dm->dqb_bsoftlimit = qbtos(di->dqb_bsoftlimit);
2335 dm->dqb_bhardlimit = qbtos(di->dqb_bhardlimit);
2337 set_bit(DQ_LASTSET_B + QIF_BLIMITS_B, &dquot->dq_flags);
2339 if (di->dqb_valid & QIF_INODES) {
2340 dm->dqb_curinodes = di->dqb_curinodes;
2342 set_bit(DQ_LASTSET_B + QIF_INODES_B, &dquot->dq_flags);
2344 if (di->dqb_valid & QIF_ILIMITS) {
2345 dm->dqb_isoftlimit = di->dqb_isoftlimit;
2346 dm->dqb_ihardlimit = di->dqb_ihardlimit;
2348 set_bit(DQ_LASTSET_B + QIF_ILIMITS_B, &dquot->dq_flags);
2350 if (di->dqb_valid & QIF_BTIME) {
2351 dm->dqb_btime = di->dqb_btime;
2353 set_bit(DQ_LASTSET_B + QIF_BTIME_B, &dquot->dq_flags);
2355 if (di->dqb_valid & QIF_ITIME) {
2356 dm->dqb_itime = di->dqb_itime;
2358 set_bit(DQ_LASTSET_B + QIF_ITIME_B, &dquot->dq_flags);
2362 if (!dm->dqb_bsoftlimit ||
2363 dm->dqb_curspace < dm->dqb_bsoftlimit) {
2365 clear_bit(DQ_BLKS_B, &dquot->dq_flags);
2366 } else if (!(di->dqb_valid & QIF_BTIME))
2367 /* Set grace only if user hasn't provided his own... */
2368 dm->dqb_btime = get_seconds() + dqi->dqi_bgrace;
2371 if (!dm->dqb_isoftlimit ||
2372 dm->dqb_curinodes < dm->dqb_isoftlimit) {
2374 clear_bit(DQ_INODES_B, &dquot->dq_flags);
2375 } else if (!(di->dqb_valid & QIF_ITIME))
2376 /* Set grace only if user hasn't provided his own... */
2377 dm->dqb_itime = get_seconds() + dqi->dqi_igrace;
2379 if (dm->dqb_bhardlimit || dm->dqb_bsoftlimit || dm->dqb_ihardlimit ||
2381 clear_bit(DQ_FAKE_B, &dquot->dq_flags);
2383 set_bit(DQ_FAKE_B, &dquot->dq_flags);
2384 spin_unlock(&dq_data_lock);
2385 mark_dquot_dirty(dquot);
2390 int vfs_set_dqblk(struct super_block *sb, int type, qid_t id,
2391 struct if_dqblk *di)
2393 struct dquot *dquot;
2396 dquot = dqget(sb, id, type);
2401 rc = do_set_dqblk(dquot, di);
2406 EXPORT_SYMBOL(vfs_set_dqblk);
2408 /* Generic routine for getting common part of quota file information */
2409 int vfs_get_dqinfo(struct super_block *sb, int type, struct if_dqinfo *ii)
2411 struct mem_dqinfo *mi;
2413 mutex_lock(&sb_dqopt(sb)->dqonoff_mutex);
2414 if (!sb_has_quota_active(sb, type)) {
2415 mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
2418 mi = sb_dqopt(sb)->info + type;
2419 spin_lock(&dq_data_lock);
2420 ii->dqi_bgrace = mi->dqi_bgrace;
2421 ii->dqi_igrace = mi->dqi_igrace;
2422 ii->dqi_flags = mi->dqi_flags & DQF_MASK;
2423 ii->dqi_valid = IIF_ALL;
2424 spin_unlock(&dq_data_lock);
2425 mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
2428 EXPORT_SYMBOL(vfs_get_dqinfo);
2430 /* Generic routine for setting common part of quota file information */
2431 int vfs_set_dqinfo(struct super_block *sb, int type, struct if_dqinfo *ii)
2433 struct mem_dqinfo *mi;
2436 mutex_lock(&sb_dqopt(sb)->dqonoff_mutex);
2437 if (!sb_has_quota_active(sb, type)) {
2441 mi = sb_dqopt(sb)->info + type;
2442 spin_lock(&dq_data_lock);
2443 if (ii->dqi_valid & IIF_BGRACE)
2444 mi->dqi_bgrace = ii->dqi_bgrace;
2445 if (ii->dqi_valid & IIF_IGRACE)
2446 mi->dqi_igrace = ii->dqi_igrace;
2447 if (ii->dqi_valid & IIF_FLAGS)
2448 mi->dqi_flags = (mi->dqi_flags & ~DQF_MASK) |
2449 (ii->dqi_flags & DQF_MASK);
2450 spin_unlock(&dq_data_lock);
2451 mark_info_dirty(sb, type);
2452 /* Force write to disk */
2453 sb->dq_op->write_info(sb, type);
2455 mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
2458 EXPORT_SYMBOL(vfs_set_dqinfo);
2460 const struct quotactl_ops vfs_quotactl_ops = {
2461 .quota_on = vfs_quota_on,
2462 .quota_off = vfs_quota_off,
2463 .quota_sync = vfs_quota_sync,
2464 .get_info = vfs_get_dqinfo,
2465 .set_info = vfs_set_dqinfo,
2466 .get_dqblk = vfs_get_dqblk,
2467 .set_dqblk = vfs_set_dqblk
2470 static ctl_table fs_dqstats_table[] = {
2472 .procname = "lookups",
2473 .data = &dqstats.lookups,
2474 .maxlen = sizeof(int),
2476 .proc_handler = proc_dointvec,
2479 .procname = "drops",
2480 .data = &dqstats.drops,
2481 .maxlen = sizeof(int),
2483 .proc_handler = proc_dointvec,
2486 .procname = "reads",
2487 .data = &dqstats.reads,
2488 .maxlen = sizeof(int),
2490 .proc_handler = proc_dointvec,
2493 .procname = "writes",
2494 .data = &dqstats.writes,
2495 .maxlen = sizeof(int),
2497 .proc_handler = proc_dointvec,
2500 .procname = "cache_hits",
2501 .data = &dqstats.cache_hits,
2502 .maxlen = sizeof(int),
2504 .proc_handler = proc_dointvec,
2507 .procname = "allocated_dquots",
2508 .data = &dqstats.allocated_dquots,
2509 .maxlen = sizeof(int),
2511 .proc_handler = proc_dointvec,
2514 .procname = "free_dquots",
2515 .data = &dqstats.free_dquots,
2516 .maxlen = sizeof(int),
2518 .proc_handler = proc_dointvec,
2521 .procname = "syncs",
2522 .data = &dqstats.syncs,
2523 .maxlen = sizeof(int),
2525 .proc_handler = proc_dointvec,
2527 #ifdef CONFIG_PRINT_QUOTA_WARNING
2529 .procname = "warnings",
2530 .data = &flag_print_warnings,
2531 .maxlen = sizeof(int),
2533 .proc_handler = proc_dointvec,
2539 static ctl_table fs_table[] = {
2541 .procname = "quota",
2543 .child = fs_dqstats_table,
2548 static ctl_table sys_table[] = {
2557 static int __init dquot_init(void)
2560 unsigned long nr_hash, order;
2562 printk(KERN_NOTICE "VFS: Disk quotas %s\n", __DQUOT_VERSION__);
2564 register_sysctl_table(sys_table);
2566 dquot_cachep = kmem_cache_create("dquot",
2567 sizeof(struct dquot), sizeof(unsigned long) * 4,
2568 (SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT|
2569 SLAB_MEM_SPREAD|SLAB_PANIC),
2573 dquot_hash = (struct hlist_head *)__get_free_pages(GFP_ATOMIC, order);
2575 panic("Cannot create dquot hash table");
2577 /* Find power-of-two hlist_heads which can fit into allocation */
2578 nr_hash = (1UL << order) * PAGE_SIZE / sizeof(struct hlist_head);
2582 } while (nr_hash >> dq_hash_bits);
2585 nr_hash = 1UL << dq_hash_bits;
2586 dq_hash_mask = nr_hash - 1;
2587 for (i = 0; i < nr_hash; i++)
2588 INIT_HLIST_HEAD(dquot_hash + i);
2590 printk("Dquot-cache hash table entries: %ld (order %ld, %ld bytes)\n",
2591 nr_hash, order, (PAGE_SIZE << order));
2593 register_shrinker(&dqcache_shrinker);
2597 module_init(dquot_init);