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 * Version: $Id: dquot.c,v 6.3 1996/11/17 18:35:34 mvw Exp mvw $
14 * Author: Marco van Wieringen <mvw@planets.elm.net>
16 * Fixes: Dmitry Gorodchanin <pgmdsg@ibi.com>, 11 Feb 96
18 * Revised list management to avoid races
19 * -- Bill Hawes, <whawes@star.net>, 9/98
21 * Fixed races in dquot_transfer(), dqget() and dquot_alloc_...().
22 * As the consequence the locking was moved from dquot_decr_...(),
23 * dquot_incr_...() to calling functions.
24 * invalidate_dquots() now writes modified dquots.
25 * Serialized quota_off() and quota_on() for mount point.
26 * Fixed a few bugs in grow_dquots().
27 * Fixed deadlock in write_dquot() - we no longer account quotas on
29 * remove_dquot_ref() moved to inode.c - it now traverses through inodes
30 * add_dquot_ref() restarts after blocking
31 * Added check for bogus uid and fixed check for group in quotactl.
32 * Jan Kara, <jack@suse.cz>, sponsored by SuSE CR, 10-11/99
34 * Used struct list_head instead of own list struct
35 * Invalidation of referenced dquots is no longer possible
36 * Improved free_dquots list management
37 * Quota and i_blocks are now updated in one place to avoid races
38 * Warnings are now delayed so we won't block in critical section
39 * Write updated not to require dquot lock
40 * Jan Kara, <jack@suse.cz>, 9/2000
42 * Added dynamic quota structure allocation
43 * Jan Kara <jack@suse.cz> 12/2000
45 * Rewritten quota interface. Implemented new quota format and
46 * formats registering.
47 * Jan Kara, <jack@suse.cz>, 2001,2002
50 * Jan Kara, <jack@suse.cz>, 10/2002
52 * Added journalled quota support, fix lock inversion problems
53 * Jan Kara, <jack@suse.cz>, 2003,2004
55 * (C) Copyright 1994 - 1997 Marco van Wieringen
58 #include <linux/errno.h>
59 #include <linux/kernel.h>
61 #include <linux/mount.h>
63 #include <linux/time.h>
64 #include <linux/types.h>
65 #include <linux/string.h>
66 #include <linux/fcntl.h>
67 #include <linux/stat.h>
68 #include <linux/tty.h>
69 #include <linux/file.h>
70 #include <linux/slab.h>
71 #include <linux/sysctl.h>
72 #include <linux/smp_lock.h>
73 #include <linux/init.h>
74 #include <linux/module.h>
75 #include <linux/proc_fs.h>
76 #include <linux/security.h>
77 #include <linux/kmod.h>
78 #include <linux/namei.h>
79 #include <linux/buffer_head.h>
80 #include <linux/capability.h>
81 #include <linux/quotaops.h>
82 #include <linux/writeback.h> /* for inode_lock, oddly enough.. */
84 #include <asm/uaccess.h>
86 #define __DQUOT_PARANOIA
89 * There are two quota SMP locks. dq_list_lock protects all lists with quotas
90 * and quota formats and also dqstats structure containing statistics about the
91 * lists. dq_data_lock protects data from dq_dqb and also mem_dqinfo structures
92 * and also guards consistency of dquot->dq_dqb with inode->i_blocks, i_bytes.
93 * i_blocks and i_bytes updates itself are guarded by i_lock acquired directly
94 * in inode_add_bytes() and inode_sub_bytes().
96 * The spinlock ordering is hence: dq_data_lock > dq_list_lock > i_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 * (these locking rules also apply for S_NOQUOTA flag in the inode - note that
105 * for altering the flag i_mutex is also needed). If operation is holding
106 * reference to dquot in other way (e.g. quotactl ops) it must be guarded by
108 * This locking assures that:
109 * a) update/access to dquot pointers in inode is serialized
110 * b) everyone is guarded against invalidate_dquots()
112 * Each dquot has its dq_lock mutex. Locked dquots might not be referenced
113 * from inodes (dquot_alloc_space() and such don't check the dq_lock).
114 * Currently dquot is locked only when it is being read to memory (or space for
115 * it is being allocated) on the first dqget() and when it is being released on
116 * the last dqput(). The allocation and release oparations are serialized by
117 * the dq_lock and by checking the use count in dquot_release(). Write
118 * operations on dquots don't hold dq_lock as they copy data under dq_data_lock
119 * spinlock to internal buffers before writing.
121 * Lock ordering (including related VFS locks) is the following:
122 * i_mutex > dqonoff_sem > journal_lock > dqptr_sem > dquot->dq_lock >
124 * i_mutex on quota files is special (it's below dqio_mutex)
127 static DEFINE_SPINLOCK(dq_list_lock);
128 DEFINE_SPINLOCK(dq_data_lock);
130 static char *quotatypes[] = INITQFNAMES;
131 static struct quota_format_type *quota_formats; /* List of registered formats */
132 static struct quota_module_name module_names[] = INIT_QUOTA_MODULE_NAMES;
134 /* SLAB cache for dquot structures */
135 static struct kmem_cache *dquot_cachep;
137 int register_quota_format(struct quota_format_type *fmt)
139 spin_lock(&dq_list_lock);
140 fmt->qf_next = quota_formats;
142 spin_unlock(&dq_list_lock);
146 void unregister_quota_format(struct quota_format_type *fmt)
148 struct quota_format_type **actqf;
150 spin_lock(&dq_list_lock);
151 for (actqf = "a_formats; *actqf && *actqf != fmt; actqf = &(*actqf)->qf_next);
153 *actqf = (*actqf)->qf_next;
154 spin_unlock(&dq_list_lock);
157 static struct quota_format_type *find_quota_format(int id)
159 struct quota_format_type *actqf;
161 spin_lock(&dq_list_lock);
162 for (actqf = quota_formats; actqf && actqf->qf_fmt_id != id; actqf = actqf->qf_next);
163 if (!actqf || !try_module_get(actqf->qf_owner)) {
166 spin_unlock(&dq_list_lock);
168 for (qm = 0; module_names[qm].qm_fmt_id && module_names[qm].qm_fmt_id != id; qm++);
169 if (!module_names[qm].qm_fmt_id || request_module(module_names[qm].qm_mod_name))
172 spin_lock(&dq_list_lock);
173 for (actqf = quota_formats; actqf && actqf->qf_fmt_id != id; actqf = actqf->qf_next);
174 if (actqf && !try_module_get(actqf->qf_owner))
177 spin_unlock(&dq_list_lock);
181 static void put_quota_format(struct quota_format_type *fmt)
183 module_put(fmt->qf_owner);
187 * Dquot List Management:
188 * The quota code uses three lists for dquot management: the inuse_list,
189 * free_dquots, and dquot_hash[] array. A single dquot structure may be
190 * on all three lists, depending on its current state.
192 * All dquots are placed to the end of inuse_list when first created, and this
193 * list is used for invalidate operation, which must look at every dquot.
195 * Unused dquots (dq_count == 0) are added to the free_dquots list when freed,
196 * and this list is searched whenever we need an available dquot. Dquots are
197 * removed from the list as soon as they are used again, and
198 * dqstats.free_dquots gives the number of dquots on the list. When
199 * dquot is invalidated it's completely released from memory.
201 * Dquots with a specific identity (device, type and id) are placed on
202 * one of the dquot_hash[] hash chains. The provides an efficient search
203 * mechanism to locate a specific dquot.
206 static LIST_HEAD(inuse_list);
207 static LIST_HEAD(free_dquots);
208 static unsigned int dq_hash_bits, dq_hash_mask;
209 static struct hlist_head *dquot_hash;
211 struct dqstats dqstats;
213 static void dqput(struct dquot *dquot);
215 static inline unsigned int
216 hashfn(const struct super_block *sb, unsigned int id, int type)
220 tmp = (((unsigned long)sb>>L1_CACHE_SHIFT) ^ id) * (MAXQUOTAS - type);
221 return (tmp + (tmp >> dq_hash_bits)) & dq_hash_mask;
225 * Following list functions expect dq_list_lock to be held
227 static inline void insert_dquot_hash(struct dquot *dquot)
229 struct hlist_head *head = dquot_hash + hashfn(dquot->dq_sb, dquot->dq_id, dquot->dq_type);
230 hlist_add_head(&dquot->dq_hash, head);
233 static inline void remove_dquot_hash(struct dquot *dquot)
235 hlist_del_init(&dquot->dq_hash);
238 static inline struct dquot *find_dquot(unsigned int hashent, struct super_block *sb, unsigned int id, int type)
240 struct hlist_node *node;
243 hlist_for_each (node, dquot_hash+hashent) {
244 dquot = hlist_entry(node, struct dquot, dq_hash);
245 if (dquot->dq_sb == sb && dquot->dq_id == id && dquot->dq_type == type)
251 /* Add a dquot to the tail of the free list */
252 static inline void put_dquot_last(struct dquot *dquot)
254 list_add_tail(&dquot->dq_free, &free_dquots);
255 dqstats.free_dquots++;
258 static inline void remove_free_dquot(struct dquot *dquot)
260 if (list_empty(&dquot->dq_free))
262 list_del_init(&dquot->dq_free);
263 dqstats.free_dquots--;
266 static inline void put_inuse(struct dquot *dquot)
268 /* We add to the back of inuse list so we don't have to restart
269 * when traversing this list and we block */
270 list_add_tail(&dquot->dq_inuse, &inuse_list);
271 dqstats.allocated_dquots++;
274 static inline void remove_inuse(struct dquot *dquot)
276 dqstats.allocated_dquots--;
277 list_del(&dquot->dq_inuse);
280 * End of list functions needing dq_list_lock
283 static void wait_on_dquot(struct dquot *dquot)
285 mutex_lock(&dquot->dq_lock);
286 mutex_unlock(&dquot->dq_lock);
289 #define mark_dquot_dirty(dquot) ((dquot)->dq_sb->dq_op->mark_dirty(dquot))
291 int dquot_mark_dquot_dirty(struct dquot *dquot)
293 spin_lock(&dq_list_lock);
294 if (!test_and_set_bit(DQ_MOD_B, &dquot->dq_flags))
295 list_add(&dquot->dq_dirty, &sb_dqopt(dquot->dq_sb)->
296 info[dquot->dq_type].dqi_dirty_list);
297 spin_unlock(&dq_list_lock);
301 /* This function needs dq_list_lock */
302 static inline int clear_dquot_dirty(struct dquot *dquot)
304 if (!test_and_clear_bit(DQ_MOD_B, &dquot->dq_flags))
306 list_del_init(&dquot->dq_dirty);
310 void mark_info_dirty(struct super_block *sb, int type)
312 set_bit(DQF_INFO_DIRTY_B, &sb_dqopt(sb)->info[type].dqi_flags);
314 EXPORT_SYMBOL(mark_info_dirty);
317 * Read dquot from disk and alloc space for it
320 int dquot_acquire(struct dquot *dquot)
322 int ret = 0, ret2 = 0;
323 struct quota_info *dqopt = sb_dqopt(dquot->dq_sb);
325 mutex_lock(&dquot->dq_lock);
326 mutex_lock(&dqopt->dqio_mutex);
327 if (!test_bit(DQ_READ_B, &dquot->dq_flags))
328 ret = dqopt->ops[dquot->dq_type]->read_dqblk(dquot);
331 set_bit(DQ_READ_B, &dquot->dq_flags);
332 /* Instantiate dquot if needed */
333 if (!test_bit(DQ_ACTIVE_B, &dquot->dq_flags) && !dquot->dq_off) {
334 ret = dqopt->ops[dquot->dq_type]->commit_dqblk(dquot);
335 /* Write the info if needed */
336 if (info_dirty(&dqopt->info[dquot->dq_type]))
337 ret2 = dqopt->ops[dquot->dq_type]->write_file_info(dquot->dq_sb, dquot->dq_type);
345 set_bit(DQ_ACTIVE_B, &dquot->dq_flags);
347 mutex_unlock(&dqopt->dqio_mutex);
348 mutex_unlock(&dquot->dq_lock);
353 * Write dquot to disk
355 int dquot_commit(struct dquot *dquot)
357 int ret = 0, ret2 = 0;
358 struct quota_info *dqopt = sb_dqopt(dquot->dq_sb);
360 mutex_lock(&dqopt->dqio_mutex);
361 spin_lock(&dq_list_lock);
362 if (!clear_dquot_dirty(dquot)) {
363 spin_unlock(&dq_list_lock);
366 spin_unlock(&dq_list_lock);
367 /* Inactive dquot can be only if there was error during read/init
368 * => we have better not writing it */
369 if (test_bit(DQ_ACTIVE_B, &dquot->dq_flags)) {
370 ret = dqopt->ops[dquot->dq_type]->commit_dqblk(dquot);
371 if (info_dirty(&dqopt->info[dquot->dq_type]))
372 ret2 = dqopt->ops[dquot->dq_type]->write_file_info(dquot->dq_sb, dquot->dq_type);
377 mutex_unlock(&dqopt->dqio_mutex);
384 int dquot_release(struct dquot *dquot)
386 int ret = 0, ret2 = 0;
387 struct quota_info *dqopt = sb_dqopt(dquot->dq_sb);
389 mutex_lock(&dquot->dq_lock);
390 /* Check whether we are not racing with some other dqget() */
391 if (atomic_read(&dquot->dq_count) > 1)
393 mutex_lock(&dqopt->dqio_mutex);
394 if (dqopt->ops[dquot->dq_type]->release_dqblk) {
395 ret = dqopt->ops[dquot->dq_type]->release_dqblk(dquot);
397 if (info_dirty(&dqopt->info[dquot->dq_type]))
398 ret2 = dqopt->ops[dquot->dq_type]->write_file_info(dquot->dq_sb, dquot->dq_type);
402 clear_bit(DQ_ACTIVE_B, &dquot->dq_flags);
403 mutex_unlock(&dqopt->dqio_mutex);
405 mutex_unlock(&dquot->dq_lock);
409 /* Invalidate all dquots on the list. Note that this function is called after
410 * quota is disabled and pointers from inodes removed so there cannot be new
411 * quota users. There can still be some users of quotas due to inodes being
412 * just deleted or pruned by prune_icache() (those are not attached to any
413 * list). We have to wait for such users.
415 static void invalidate_dquots(struct super_block *sb, int type)
417 struct dquot *dquot, *tmp;
420 spin_lock(&dq_list_lock);
421 list_for_each_entry_safe(dquot, tmp, &inuse_list, dq_inuse) {
422 if (dquot->dq_sb != sb)
424 if (dquot->dq_type != type)
426 /* Wait for dquot users */
427 if (atomic_read(&dquot->dq_count)) {
430 atomic_inc(&dquot->dq_count);
431 prepare_to_wait(&dquot->dq_wait_unused, &wait,
432 TASK_UNINTERRUPTIBLE);
433 spin_unlock(&dq_list_lock);
434 /* Once dqput() wakes us up, we know it's time to free
436 * IMPORTANT: we rely on the fact that there is always
437 * at most one process waiting for dquot to free.
438 * Otherwise dq_count would be > 1 and we would never
441 if (atomic_read(&dquot->dq_count) > 1)
443 finish_wait(&dquot->dq_wait_unused, &wait);
445 /* At this moment dquot() need not exist (it could be
446 * reclaimed by prune_dqcache(). Hence we must
451 * Quota now has no users and it has been written on last
454 remove_dquot_hash(dquot);
455 remove_free_dquot(dquot);
457 kmem_cache_free(dquot_cachep, dquot);
459 spin_unlock(&dq_list_lock);
462 int vfs_quota_sync(struct super_block *sb, int type)
464 struct list_head *dirty;
466 struct quota_info *dqopt = sb_dqopt(sb);
469 mutex_lock(&dqopt->dqonoff_mutex);
470 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
471 if (type != -1 && cnt != type)
473 if (!sb_has_quota_enabled(sb, cnt))
475 spin_lock(&dq_list_lock);
476 dirty = &dqopt->info[cnt].dqi_dirty_list;
477 while (!list_empty(dirty)) {
478 dquot = list_entry(dirty->next, struct dquot, dq_dirty);
479 /* Dirty and inactive can be only bad dquot... */
480 if (!test_bit(DQ_ACTIVE_B, &dquot->dq_flags)) {
481 clear_dquot_dirty(dquot);
484 /* Now we have active dquot from which someone is
485 * holding reference so we can safely just increase
487 atomic_inc(&dquot->dq_count);
489 spin_unlock(&dq_list_lock);
490 sb->dq_op->write_dquot(dquot);
492 spin_lock(&dq_list_lock);
494 spin_unlock(&dq_list_lock);
497 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
498 if ((cnt == type || type == -1) && sb_has_quota_enabled(sb, cnt)
499 && info_dirty(&dqopt->info[cnt]))
500 sb->dq_op->write_info(sb, cnt);
501 spin_lock(&dq_list_lock);
503 spin_unlock(&dq_list_lock);
504 mutex_unlock(&dqopt->dqonoff_mutex);
509 /* Free unused dquots from cache */
510 static void prune_dqcache(int count)
512 struct list_head *head;
515 head = free_dquots.prev;
516 while (head != &free_dquots && count) {
517 dquot = list_entry(head, struct dquot, dq_free);
518 remove_dquot_hash(dquot);
519 remove_free_dquot(dquot);
521 kmem_cache_free(dquot_cachep, dquot);
523 head = free_dquots.prev;
528 * This is called from kswapd when we think we need some
532 static int shrink_dqcache_memory(int nr, gfp_t gfp_mask)
535 spin_lock(&dq_list_lock);
537 spin_unlock(&dq_list_lock);
539 return (dqstats.free_dquots / 100) * sysctl_vfs_cache_pressure;
543 * Put reference to dquot
544 * NOTE: If you change this function please check whether dqput_blocks() works right...
545 * MUST be called with either dqptr_sem or dqonoff_mutex held
547 static void dqput(struct dquot *dquot)
551 #ifdef __DQUOT_PARANOIA
552 if (!atomic_read(&dquot->dq_count)) {
553 printk("VFS: dqput: trying to free free dquot\n");
554 printk("VFS: device %s, dquot of %s %d\n",
556 quotatypes[dquot->dq_type],
562 spin_lock(&dq_list_lock);
564 spin_unlock(&dq_list_lock);
566 spin_lock(&dq_list_lock);
567 if (atomic_read(&dquot->dq_count) > 1) {
568 /* We have more than one user... nothing to do */
569 atomic_dec(&dquot->dq_count);
570 /* Releasing dquot during quotaoff phase? */
571 if (!sb_has_quota_enabled(dquot->dq_sb, dquot->dq_type) &&
572 atomic_read(&dquot->dq_count) == 1)
573 wake_up(&dquot->dq_wait_unused);
574 spin_unlock(&dq_list_lock);
577 /* Need to release dquot? */
578 if (test_bit(DQ_ACTIVE_B, &dquot->dq_flags) && dquot_dirty(dquot)) {
579 spin_unlock(&dq_list_lock);
580 /* Commit dquot before releasing */
581 dquot->dq_sb->dq_op->write_dquot(dquot);
584 /* Clear flag in case dquot was inactive (something bad happened) */
585 clear_dquot_dirty(dquot);
586 if (test_bit(DQ_ACTIVE_B, &dquot->dq_flags)) {
587 spin_unlock(&dq_list_lock);
588 dquot->dq_sb->dq_op->release_dquot(dquot);
591 atomic_dec(&dquot->dq_count);
592 #ifdef __DQUOT_PARANOIA
594 BUG_ON(!list_empty(&dquot->dq_free));
596 put_dquot_last(dquot);
597 spin_unlock(&dq_list_lock);
600 static struct dquot *get_empty_dquot(struct super_block *sb, int type)
604 dquot = kmem_cache_zalloc(dquot_cachep, GFP_NOFS);
608 mutex_init(&dquot->dq_lock);
609 INIT_LIST_HEAD(&dquot->dq_free);
610 INIT_LIST_HEAD(&dquot->dq_inuse);
611 INIT_HLIST_NODE(&dquot->dq_hash);
612 INIT_LIST_HEAD(&dquot->dq_dirty);
613 init_waitqueue_head(&dquot->dq_wait_unused);
615 dquot->dq_type = type;
616 atomic_set(&dquot->dq_count, 1);
622 * Get reference to dquot
623 * MUST be called with either dqptr_sem or dqonoff_mutex held
625 static struct dquot *dqget(struct super_block *sb, unsigned int id, int type)
627 unsigned int hashent = hashfn(sb, id, type);
628 struct dquot *dquot, *empty = NODQUOT;
630 if (!sb_has_quota_enabled(sb, type))
633 spin_lock(&dq_list_lock);
634 if ((dquot = find_dquot(hashent, sb, id, type)) == NODQUOT) {
635 if (empty == NODQUOT) {
636 spin_unlock(&dq_list_lock);
637 if ((empty = get_empty_dquot(sb, type)) == NODQUOT)
638 schedule(); /* Try to wait for a moment... */
643 /* all dquots go on the inuse_list */
645 /* hash it first so it can be found */
646 insert_dquot_hash(dquot);
648 spin_unlock(&dq_list_lock);
650 if (!atomic_read(&dquot->dq_count))
651 remove_free_dquot(dquot);
652 atomic_inc(&dquot->dq_count);
653 dqstats.cache_hits++;
655 spin_unlock(&dq_list_lock);
657 kmem_cache_free(dquot_cachep, empty);
659 /* Wait for dq_lock - after this we know that either dquot_release() is already
660 * finished or it will be canceled due to dq_count > 1 test */
661 wait_on_dquot(dquot);
662 /* Read the dquot and instantiate it (everything done only if needed) */
663 if (!test_bit(DQ_ACTIVE_B, &dquot->dq_flags) && sb->dq_op->acquire_dquot(dquot) < 0) {
667 #ifdef __DQUOT_PARANOIA
668 BUG_ON(!dquot->dq_sb); /* Has somebody invalidated entry under us? */
674 static int dqinit_needed(struct inode *inode, int type)
678 if (IS_NOQUOTA(inode))
681 return inode->i_dquot[type] == NODQUOT;
682 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
683 if (inode->i_dquot[cnt] == NODQUOT)
688 /* This routine is guarded by dqonoff_mutex mutex */
689 static void add_dquot_ref(struct super_block *sb, int type)
694 spin_lock(&inode_lock);
695 list_for_each_entry(inode, &sb->s_inodes, i_sb_list) {
696 if (!atomic_read(&inode->i_writecount))
698 if (!dqinit_needed(inode, type))
700 if (inode->i_state & (I_FREEING|I_WILL_FREE))
704 spin_unlock(&inode_lock);
706 sb->dq_op->initialize(inode, type);
708 /* As we may have blocked we had better restart... */
711 spin_unlock(&inode_lock);
714 /* Return 0 if dqput() won't block (note that 1 doesn't necessarily mean blocking) */
715 static inline int dqput_blocks(struct dquot *dquot)
717 if (atomic_read(&dquot->dq_count) <= 1)
722 /* Remove references to dquots from inode - add dquot to list for freeing if needed */
723 /* We can't race with anybody because we hold dqptr_sem for writing... */
724 static int remove_inode_dquot_ref(struct inode *inode, int type,
725 struct list_head *tofree_head)
727 struct dquot *dquot = inode->i_dquot[type];
729 inode->i_dquot[type] = NODQUOT;
730 if (dquot != NODQUOT) {
731 if (dqput_blocks(dquot)) {
732 #ifdef __DQUOT_PARANOIA
733 if (atomic_read(&dquot->dq_count) != 1)
734 printk(KERN_WARNING "VFS: Adding dquot with dq_count %d to dispose list.\n", atomic_read(&dquot->dq_count));
736 spin_lock(&dq_list_lock);
737 list_add(&dquot->dq_free, tofree_head); /* As dquot must have currently users it can't be on the free list... */
738 spin_unlock(&dq_list_lock);
742 dqput(dquot); /* We have guaranteed we won't block */
747 /* Free list of dquots - called from inode.c */
748 /* dquots are removed from inodes, no new references can be got so we are the only ones holding reference */
749 static void put_dquot_list(struct list_head *tofree_head)
751 struct list_head *act_head;
754 act_head = tofree_head->next;
755 /* So now we have dquots on the list... Just free them */
756 while (act_head != tofree_head) {
757 dquot = list_entry(act_head, struct dquot, dq_free);
758 act_head = act_head->next;
759 list_del_init(&dquot->dq_free); /* Remove dquot from the list so we won't have problems... */
764 static void remove_dquot_ref(struct super_block *sb, int type,
765 struct list_head *tofree_head)
769 spin_lock(&inode_lock);
770 list_for_each_entry(inode, &sb->s_inodes, i_sb_list) {
771 if (!IS_NOQUOTA(inode))
772 remove_inode_dquot_ref(inode, type, tofree_head);
774 spin_unlock(&inode_lock);
777 /* Gather all references from inodes and drop them */
778 static void drop_dquot_ref(struct super_block *sb, int type)
780 LIST_HEAD(tofree_head);
783 down_write(&sb_dqopt(sb)->dqptr_sem);
784 remove_dquot_ref(sb, type, &tofree_head);
785 up_write(&sb_dqopt(sb)->dqptr_sem);
786 put_dquot_list(&tofree_head);
790 static inline void dquot_incr_inodes(struct dquot *dquot, unsigned long number)
792 dquot->dq_dqb.dqb_curinodes += number;
795 static inline void dquot_incr_space(struct dquot *dquot, qsize_t number)
797 dquot->dq_dqb.dqb_curspace += number;
800 static inline void dquot_decr_inodes(struct dquot *dquot, unsigned long number)
802 if (dquot->dq_dqb.dqb_curinodes > number)
803 dquot->dq_dqb.dqb_curinodes -= number;
805 dquot->dq_dqb.dqb_curinodes = 0;
806 if (dquot->dq_dqb.dqb_curinodes <= dquot->dq_dqb.dqb_isoftlimit)
807 dquot->dq_dqb.dqb_itime = (time_t) 0;
808 clear_bit(DQ_INODES_B, &dquot->dq_flags);
811 static inline void dquot_decr_space(struct dquot *dquot, qsize_t number)
813 if (dquot->dq_dqb.dqb_curspace > number)
814 dquot->dq_dqb.dqb_curspace -= number;
816 dquot->dq_dqb.dqb_curspace = 0;
817 if (toqb(dquot->dq_dqb.dqb_curspace) <= dquot->dq_dqb.dqb_bsoftlimit)
818 dquot->dq_dqb.dqb_btime = (time_t) 0;
819 clear_bit(DQ_BLKS_B, &dquot->dq_flags);
822 static int flag_print_warnings = 1;
824 static inline int need_print_warning(struct dquot *dquot)
826 if (!flag_print_warnings)
829 switch (dquot->dq_type) {
831 return current->fsuid == dquot->dq_id;
833 return in_group_p(dquot->dq_id);
838 /* Values of warnings */
841 #define ISOFTLONGWARN 2
844 #define BSOFTLONGWARN 5
847 /* Print warning to user which exceeded quota */
848 static void print_warning(struct dquot *dquot, const char warntype)
851 struct tty_struct *tty;
852 int flag = (warntype == BHARDWARN || warntype == BSOFTLONGWARN) ? DQ_BLKS_B :
853 ((warntype == IHARDWARN || warntype == ISOFTLONGWARN) ? DQ_INODES_B : 0);
855 if (!need_print_warning(dquot) || (flag && test_and_set_bit(flag, &dquot->dq_flags)))
858 mutex_lock(&tty_mutex);
859 tty = get_current_tty();
862 tty_write_message(tty, dquot->dq_sb->s_id);
863 if (warntype == ISOFTWARN || warntype == BSOFTWARN)
864 tty_write_message(tty, ": warning, ");
866 tty_write_message(tty, ": write failed, ");
867 tty_write_message(tty, quotatypes[dquot->dq_type]);
870 msg = " file limit reached.\r\n";
873 msg = " file quota exceeded too long.\r\n";
876 msg = " file quota exceeded.\r\n";
879 msg = " block limit reached.\r\n";
882 msg = " block quota exceeded too long.\r\n";
885 msg = " block quota exceeded.\r\n";
888 tty_write_message(tty, msg);
890 mutex_unlock(&tty_mutex);
893 static inline void flush_warnings(struct dquot **dquots, char *warntype)
897 for (i = 0; i < MAXQUOTAS; i++)
898 if (dquots[i] != NODQUOT && warntype[i] != NOWARN)
899 print_warning(dquots[i], warntype[i]);
902 static inline char ignore_hardlimit(struct dquot *dquot)
904 struct mem_dqinfo *info = &sb_dqopt(dquot->dq_sb)->info[dquot->dq_type];
906 return capable(CAP_SYS_RESOURCE) &&
907 (info->dqi_format->qf_fmt_id != QFMT_VFS_OLD || !(info->dqi_flags & V1_DQF_RSQUASH));
910 /* needs dq_data_lock */
911 static int check_idq(struct dquot *dquot, ulong inodes, char *warntype)
914 if (inodes <= 0 || test_bit(DQ_FAKE_B, &dquot->dq_flags))
917 if (dquot->dq_dqb.dqb_ihardlimit &&
918 (dquot->dq_dqb.dqb_curinodes + inodes) > dquot->dq_dqb.dqb_ihardlimit &&
919 !ignore_hardlimit(dquot)) {
920 *warntype = IHARDWARN;
924 if (dquot->dq_dqb.dqb_isoftlimit &&
925 (dquot->dq_dqb.dqb_curinodes + inodes) > dquot->dq_dqb.dqb_isoftlimit &&
926 dquot->dq_dqb.dqb_itime && get_seconds() >= dquot->dq_dqb.dqb_itime &&
927 !ignore_hardlimit(dquot)) {
928 *warntype = ISOFTLONGWARN;
932 if (dquot->dq_dqb.dqb_isoftlimit &&
933 (dquot->dq_dqb.dqb_curinodes + inodes) > dquot->dq_dqb.dqb_isoftlimit &&
934 dquot->dq_dqb.dqb_itime == 0) {
935 *warntype = ISOFTWARN;
936 dquot->dq_dqb.dqb_itime = get_seconds() + sb_dqopt(dquot->dq_sb)->info[dquot->dq_type].dqi_igrace;
942 /* needs dq_data_lock */
943 static int check_bdq(struct dquot *dquot, qsize_t space, int prealloc, char *warntype)
946 if (space <= 0 || test_bit(DQ_FAKE_B, &dquot->dq_flags))
949 if (dquot->dq_dqb.dqb_bhardlimit &&
950 toqb(dquot->dq_dqb.dqb_curspace + space) > dquot->dq_dqb.dqb_bhardlimit &&
951 !ignore_hardlimit(dquot)) {
953 *warntype = BHARDWARN;
957 if (dquot->dq_dqb.dqb_bsoftlimit &&
958 toqb(dquot->dq_dqb.dqb_curspace + space) > dquot->dq_dqb.dqb_bsoftlimit &&
959 dquot->dq_dqb.dqb_btime && get_seconds() >= dquot->dq_dqb.dqb_btime &&
960 !ignore_hardlimit(dquot)) {
962 *warntype = BSOFTLONGWARN;
966 if (dquot->dq_dqb.dqb_bsoftlimit &&
967 toqb(dquot->dq_dqb.dqb_curspace + space) > dquot->dq_dqb.dqb_bsoftlimit &&
968 dquot->dq_dqb.dqb_btime == 0) {
970 *warntype = BSOFTWARN;
971 dquot->dq_dqb.dqb_btime = get_seconds() + sb_dqopt(dquot->dq_sb)->info[dquot->dq_type].dqi_bgrace;
975 * We don't allow preallocation to exceed softlimit so exceeding will
985 * Initialize quota pointers in inode
986 * Transaction must be started at entry
988 int dquot_initialize(struct inode *inode, int type)
993 /* First test before acquiring mutex - solves deadlocks when we
994 * re-enter the quota code and are already holding the mutex */
995 if (IS_NOQUOTA(inode))
997 down_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
998 /* Having dqptr_sem we know NOQUOTA flags can't be altered... */
999 if (IS_NOQUOTA(inode))
1001 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1002 if (type != -1 && cnt != type)
1004 if (inode->i_dquot[cnt] == NODQUOT) {
1013 inode->i_dquot[cnt] = dqget(inode->i_sb, id, cnt);
1017 up_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
1022 * Release all quotas referenced by inode
1023 * Transaction must be started at an entry
1025 int dquot_drop(struct inode *inode)
1029 down_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
1030 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1031 if (inode->i_dquot[cnt] != NODQUOT) {
1032 dqput(inode->i_dquot[cnt]);
1033 inode->i_dquot[cnt] = NODQUOT;
1036 up_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
1041 * Following four functions update i_blocks+i_bytes fields and
1042 * quota information (together with appropriate checks)
1043 * NOTE: We absolutely rely on the fact that caller dirties
1044 * the inode (usually macros in quotaops.h care about this) and
1045 * holds a handle for the current transaction so that dquot write and
1046 * inode write go into the same transaction.
1050 * This operation can block, but only after everything is updated
1052 int dquot_alloc_space(struct inode *inode, qsize_t number, int warn)
1054 int cnt, ret = NO_QUOTA;
1055 char warntype[MAXQUOTAS];
1057 /* First test before acquiring mutex - solves deadlocks when we
1058 * re-enter the quota code and are already holding the mutex */
1059 if (IS_NOQUOTA(inode)) {
1061 inode_add_bytes(inode, number);
1064 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1065 warntype[cnt] = NOWARN;
1067 down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1068 if (IS_NOQUOTA(inode)) { /* Now we can do reliable test... */
1069 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1072 spin_lock(&dq_data_lock);
1073 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1074 if (inode->i_dquot[cnt] == NODQUOT)
1076 if (check_bdq(inode->i_dquot[cnt], number, warn, warntype+cnt) == NO_QUOTA)
1079 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1080 if (inode->i_dquot[cnt] == NODQUOT)
1082 dquot_incr_space(inode->i_dquot[cnt], number);
1084 inode_add_bytes(inode, number);
1087 spin_unlock(&dq_data_lock);
1088 if (ret == QUOTA_OK)
1089 /* Dirtify all the dquots - this can block when journalling */
1090 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1091 if (inode->i_dquot[cnt])
1092 mark_dquot_dirty(inode->i_dquot[cnt]);
1093 flush_warnings(inode->i_dquot, warntype);
1094 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1099 * This operation can block, but only after everything is updated
1101 int dquot_alloc_inode(const struct inode *inode, unsigned long number)
1103 int cnt, ret = NO_QUOTA;
1104 char warntype[MAXQUOTAS];
1106 /* First test before acquiring mutex - solves deadlocks when we
1107 * re-enter the quota code and are already holding the mutex */
1108 if (IS_NOQUOTA(inode))
1110 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1111 warntype[cnt] = NOWARN;
1112 down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1113 if (IS_NOQUOTA(inode)) {
1114 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1117 spin_lock(&dq_data_lock);
1118 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1119 if (inode->i_dquot[cnt] == NODQUOT)
1121 if (check_idq(inode->i_dquot[cnt], number, warntype+cnt) == NO_QUOTA)
1125 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1126 if (inode->i_dquot[cnt] == NODQUOT)
1128 dquot_incr_inodes(inode->i_dquot[cnt], number);
1132 spin_unlock(&dq_data_lock);
1133 if (ret == QUOTA_OK)
1134 /* Dirtify all the dquots - this can block when journalling */
1135 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1136 if (inode->i_dquot[cnt])
1137 mark_dquot_dirty(inode->i_dquot[cnt]);
1138 flush_warnings((struct dquot **)inode->i_dquot, warntype);
1139 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1144 * This operation can block, but only after everything is updated
1146 int dquot_free_space(struct inode *inode, qsize_t number)
1150 /* First test before acquiring mutex - solves deadlocks when we
1151 * re-enter the quota code and are already holding the mutex */
1152 if (IS_NOQUOTA(inode)) {
1154 inode_sub_bytes(inode, number);
1157 down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1158 /* Now recheck reliably when holding dqptr_sem */
1159 if (IS_NOQUOTA(inode)) {
1160 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1163 spin_lock(&dq_data_lock);
1164 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1165 if (inode->i_dquot[cnt] == NODQUOT)
1167 dquot_decr_space(inode->i_dquot[cnt], number);
1169 inode_sub_bytes(inode, number);
1170 spin_unlock(&dq_data_lock);
1171 /* Dirtify all the dquots - this can block when journalling */
1172 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1173 if (inode->i_dquot[cnt])
1174 mark_dquot_dirty(inode->i_dquot[cnt]);
1175 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1180 * This operation can block, but only after everything is updated
1182 int dquot_free_inode(const struct inode *inode, unsigned long number)
1186 /* First test before acquiring mutex - solves deadlocks when we
1187 * re-enter the quota code and are already holding the mutex */
1188 if (IS_NOQUOTA(inode))
1190 down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1191 /* Now recheck reliably when holding dqptr_sem */
1192 if (IS_NOQUOTA(inode)) {
1193 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1196 spin_lock(&dq_data_lock);
1197 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1198 if (inode->i_dquot[cnt] == NODQUOT)
1200 dquot_decr_inodes(inode->i_dquot[cnt], number);
1202 spin_unlock(&dq_data_lock);
1203 /* Dirtify all the dquots - this can block when journalling */
1204 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1205 if (inode->i_dquot[cnt])
1206 mark_dquot_dirty(inode->i_dquot[cnt]);
1207 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1212 * Transfer the number of inode and blocks from one diskquota to an other.
1214 * This operation can block, but only after everything is updated
1215 * A transaction must be started when entering this function.
1217 int dquot_transfer(struct inode *inode, struct iattr *iattr)
1220 struct dquot *transfer_from[MAXQUOTAS];
1221 struct dquot *transfer_to[MAXQUOTAS];
1222 int cnt, ret = NO_QUOTA, chuid = (iattr->ia_valid & ATTR_UID) && inode->i_uid != iattr->ia_uid,
1223 chgid = (iattr->ia_valid & ATTR_GID) && inode->i_gid != iattr->ia_gid;
1224 char warntype[MAXQUOTAS];
1226 /* First test before acquiring mutex - solves deadlocks when we
1227 * re-enter the quota code and are already holding the mutex */
1228 if (IS_NOQUOTA(inode))
1230 /* Clear the arrays */
1231 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1232 transfer_to[cnt] = transfer_from[cnt] = NODQUOT;
1233 warntype[cnt] = NOWARN;
1235 down_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
1236 /* Now recheck reliably when holding dqptr_sem */
1237 if (IS_NOQUOTA(inode)) { /* File without quota accounting? */
1238 up_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
1241 /* First build the transfer_to list - here we can block on
1242 * reading/instantiating of dquots. We know that the transaction for
1243 * us was already started so we don't violate lock ranking here */
1244 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1249 transfer_to[cnt] = dqget(inode->i_sb, iattr->ia_uid, cnt);
1254 transfer_to[cnt] = dqget(inode->i_sb, iattr->ia_gid, cnt);
1258 spin_lock(&dq_data_lock);
1259 space = inode_get_bytes(inode);
1260 /* Build the transfer_from list and check the limits */
1261 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1262 if (transfer_to[cnt] == NODQUOT)
1264 transfer_from[cnt] = inode->i_dquot[cnt];
1265 if (check_idq(transfer_to[cnt], 1, warntype+cnt) == NO_QUOTA ||
1266 check_bdq(transfer_to[cnt], space, 0, warntype+cnt) == NO_QUOTA)
1271 * Finally perform the needed transfer from transfer_from to transfer_to
1273 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1275 * Skip changes for same uid or gid or for turned off quota-type.
1277 if (transfer_to[cnt] == NODQUOT)
1280 /* Due to IO error we might not have transfer_from[] structure */
1281 if (transfer_from[cnt]) {
1282 dquot_decr_inodes(transfer_from[cnt], 1);
1283 dquot_decr_space(transfer_from[cnt], space);
1286 dquot_incr_inodes(transfer_to[cnt], 1);
1287 dquot_incr_space(transfer_to[cnt], space);
1289 inode->i_dquot[cnt] = transfer_to[cnt];
1293 spin_unlock(&dq_data_lock);
1294 /* Dirtify all the dquots - this can block when journalling */
1295 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1296 if (transfer_from[cnt])
1297 mark_dquot_dirty(transfer_from[cnt]);
1298 if (transfer_to[cnt])
1299 mark_dquot_dirty(transfer_to[cnt]);
1301 flush_warnings(transfer_to, warntype);
1303 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1304 if (ret == QUOTA_OK && transfer_from[cnt] != NODQUOT)
1305 dqput(transfer_from[cnt]);
1306 if (ret == NO_QUOTA && transfer_to[cnt] != NODQUOT)
1307 dqput(transfer_to[cnt]);
1309 up_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
1314 * Write info of quota file to disk
1316 int dquot_commit_info(struct super_block *sb, int type)
1319 struct quota_info *dqopt = sb_dqopt(sb);
1321 mutex_lock(&dqopt->dqio_mutex);
1322 ret = dqopt->ops[type]->write_file_info(sb, type);
1323 mutex_unlock(&dqopt->dqio_mutex);
1328 * Definitions of diskquota operations.
1330 struct dquot_operations dquot_operations = {
1331 .initialize = dquot_initialize,
1333 .alloc_space = dquot_alloc_space,
1334 .alloc_inode = dquot_alloc_inode,
1335 .free_space = dquot_free_space,
1336 .free_inode = dquot_free_inode,
1337 .transfer = dquot_transfer,
1338 .write_dquot = dquot_commit,
1339 .acquire_dquot = dquot_acquire,
1340 .release_dquot = dquot_release,
1341 .mark_dirty = dquot_mark_dquot_dirty,
1342 .write_info = dquot_commit_info
1345 static inline void set_enable_flags(struct quota_info *dqopt, int type)
1349 dqopt->flags |= DQUOT_USR_ENABLED;
1352 dqopt->flags |= DQUOT_GRP_ENABLED;
1357 static inline void reset_enable_flags(struct quota_info *dqopt, int type)
1361 dqopt->flags &= ~DQUOT_USR_ENABLED;
1364 dqopt->flags &= ~DQUOT_GRP_ENABLED;
1370 * Turn quota off on a device. type == -1 ==> quotaoff for all types (umount)
1372 int vfs_quota_off(struct super_block *sb, int type)
1375 struct quota_info *dqopt = sb_dqopt(sb);
1376 struct inode *toputinode[MAXQUOTAS];
1378 /* We need to serialize quota_off() for device */
1379 mutex_lock(&dqopt->dqonoff_mutex);
1380 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1381 toputinode[cnt] = NULL;
1382 if (type != -1 && cnt != type)
1384 if (!sb_has_quota_enabled(sb, cnt))
1386 reset_enable_flags(dqopt, cnt);
1388 /* Note: these are blocking operations */
1389 drop_dquot_ref(sb, cnt);
1390 invalidate_dquots(sb, cnt);
1392 * Now all dquots should be invalidated, all writes done so we should be only
1393 * users of the info. No locks needed.
1395 if (info_dirty(&dqopt->info[cnt]))
1396 sb->dq_op->write_info(sb, cnt);
1397 if (dqopt->ops[cnt]->free_file_info)
1398 dqopt->ops[cnt]->free_file_info(sb, cnt);
1399 put_quota_format(dqopt->info[cnt].dqi_format);
1401 toputinode[cnt] = dqopt->files[cnt];
1402 dqopt->files[cnt] = NULL;
1403 dqopt->info[cnt].dqi_flags = 0;
1404 dqopt->info[cnt].dqi_igrace = 0;
1405 dqopt->info[cnt].dqi_bgrace = 0;
1406 dqopt->ops[cnt] = NULL;
1408 mutex_unlock(&dqopt->dqonoff_mutex);
1409 /* Sync the superblock so that buffers with quota data are written to
1410 * disk (and so userspace sees correct data afterwards). */
1411 if (sb->s_op->sync_fs)
1412 sb->s_op->sync_fs(sb, 1);
1413 sync_blockdev(sb->s_bdev);
1414 /* Now the quota files are just ordinary files and we can set the
1415 * inode flags back. Moreover we discard the pagecache so that
1416 * userspace sees the writes we did bypassing the pagecache. We
1417 * must also discard the blockdev buffers so that we see the
1418 * changes done by userspace on the next quotaon() */
1419 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1420 if (toputinode[cnt]) {
1421 mutex_lock(&dqopt->dqonoff_mutex);
1422 /* If quota was reenabled in the meantime, we have
1424 if (!sb_has_quota_enabled(sb, cnt)) {
1425 mutex_lock(&toputinode[cnt]->i_mutex);
1426 toputinode[cnt]->i_flags &= ~(S_IMMUTABLE |
1427 S_NOATIME | S_NOQUOTA);
1428 truncate_inode_pages(&toputinode[cnt]->i_data, 0);
1429 mutex_unlock(&toputinode[cnt]->i_mutex);
1430 mark_inode_dirty(toputinode[cnt]);
1431 iput(toputinode[cnt]);
1433 mutex_unlock(&dqopt->dqonoff_mutex);
1436 invalidate_bdev(sb->s_bdev);
1441 * Turn quotas on on a device
1444 /* Helper function when we already have the inode */
1445 static int vfs_quota_on_inode(struct inode *inode, int type, int format_id)
1447 struct quota_format_type *fmt = find_quota_format(format_id);
1448 struct super_block *sb = inode->i_sb;
1449 struct quota_info *dqopt = sb_dqopt(sb);
1455 if (!S_ISREG(inode->i_mode)) {
1459 if (IS_RDONLY(inode)) {
1463 if (!sb->s_op->quota_write || !sb->s_op->quota_read) {
1468 /* As we bypass the pagecache we must now flush the inode so that
1469 * we see all the changes from userspace... */
1470 write_inode_now(inode, 1);
1471 /* And now flush the block cache so that kernel sees the changes */
1472 invalidate_bdev(sb->s_bdev);
1473 mutex_lock(&inode->i_mutex);
1474 mutex_lock(&dqopt->dqonoff_mutex);
1475 if (sb_has_quota_enabled(sb, type)) {
1479 /* We don't want quota and atime on quota files (deadlocks possible)
1480 * Also nobody should write to the file - we use special IO operations
1481 * which ignore the immutable bit. */
1482 down_write(&dqopt->dqptr_sem);
1483 oldflags = inode->i_flags & (S_NOATIME | S_IMMUTABLE | S_NOQUOTA);
1484 inode->i_flags |= S_NOQUOTA | S_NOATIME | S_IMMUTABLE;
1485 up_write(&dqopt->dqptr_sem);
1486 sb->dq_op->drop(inode);
1489 dqopt->files[type] = igrab(inode);
1490 if (!dqopt->files[type])
1493 if (!fmt->qf_ops->check_quota_file(sb, type))
1496 dqopt->ops[type] = fmt->qf_ops;
1497 dqopt->info[type].dqi_format = fmt;
1498 INIT_LIST_HEAD(&dqopt->info[type].dqi_dirty_list);
1499 mutex_lock(&dqopt->dqio_mutex);
1500 if ((error = dqopt->ops[type]->read_file_info(sb, type)) < 0) {
1501 mutex_unlock(&dqopt->dqio_mutex);
1504 mutex_unlock(&dqopt->dqio_mutex);
1505 mutex_unlock(&inode->i_mutex);
1506 set_enable_flags(dqopt, type);
1508 add_dquot_ref(sb, type);
1509 mutex_unlock(&dqopt->dqonoff_mutex);
1514 dqopt->files[type] = NULL;
1517 mutex_unlock(&dqopt->dqonoff_mutex);
1518 if (oldflags != -1) {
1519 down_write(&dqopt->dqptr_sem);
1520 /* Set the flags back (in the case of accidental quotaon()
1521 * on a wrong file we don't want to mess up the flags) */
1522 inode->i_flags &= ~(S_NOATIME | S_NOQUOTA | S_IMMUTABLE);
1523 inode->i_flags |= oldflags;
1524 up_write(&dqopt->dqptr_sem);
1526 mutex_unlock(&inode->i_mutex);
1528 put_quota_format(fmt);
1533 /* Actual function called from quotactl() */
1534 int vfs_quota_on(struct super_block *sb, int type, int format_id, char *path)
1536 struct nameidata nd;
1539 error = path_lookup(path, LOOKUP_FOLLOW, &nd);
1542 error = security_quota_on(nd.dentry);
1545 /* Quota file not on the same filesystem? */
1546 if (nd.mnt->mnt_sb != sb)
1549 error = vfs_quota_on_inode(nd.dentry->d_inode, type, format_id);
1556 * This function is used when filesystem needs to initialize quotas
1557 * during mount time.
1559 int vfs_quota_on_mount(struct super_block *sb, char *qf_name,
1560 int format_id, int type)
1562 struct dentry *dentry;
1565 dentry = lookup_one_len(qf_name, sb->s_root, strlen(qf_name));
1567 return PTR_ERR(dentry);
1569 if (!dentry->d_inode) {
1574 error = security_quota_on(dentry);
1576 error = vfs_quota_on_inode(dentry->d_inode, type, format_id);
1583 /* Generic routine for getting common part of quota structure */
1584 static void do_get_dqblk(struct dquot *dquot, struct if_dqblk *di)
1586 struct mem_dqblk *dm = &dquot->dq_dqb;
1588 spin_lock(&dq_data_lock);
1589 di->dqb_bhardlimit = dm->dqb_bhardlimit;
1590 di->dqb_bsoftlimit = dm->dqb_bsoftlimit;
1591 di->dqb_curspace = dm->dqb_curspace;
1592 di->dqb_ihardlimit = dm->dqb_ihardlimit;
1593 di->dqb_isoftlimit = dm->dqb_isoftlimit;
1594 di->dqb_curinodes = dm->dqb_curinodes;
1595 di->dqb_btime = dm->dqb_btime;
1596 di->dqb_itime = dm->dqb_itime;
1597 di->dqb_valid = QIF_ALL;
1598 spin_unlock(&dq_data_lock);
1601 int vfs_get_dqblk(struct super_block *sb, int type, qid_t id, struct if_dqblk *di)
1603 struct dquot *dquot;
1605 mutex_lock(&sb_dqopt(sb)->dqonoff_mutex);
1606 if (!(dquot = dqget(sb, id, type))) {
1607 mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
1610 do_get_dqblk(dquot, di);
1612 mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
1616 /* Generic routine for setting common part of quota structure */
1617 static void do_set_dqblk(struct dquot *dquot, struct if_dqblk *di)
1619 struct mem_dqblk *dm = &dquot->dq_dqb;
1620 int check_blim = 0, check_ilim = 0;
1622 spin_lock(&dq_data_lock);
1623 if (di->dqb_valid & QIF_SPACE) {
1624 dm->dqb_curspace = di->dqb_curspace;
1627 if (di->dqb_valid & QIF_BLIMITS) {
1628 dm->dqb_bsoftlimit = di->dqb_bsoftlimit;
1629 dm->dqb_bhardlimit = di->dqb_bhardlimit;
1632 if (di->dqb_valid & QIF_INODES) {
1633 dm->dqb_curinodes = di->dqb_curinodes;
1636 if (di->dqb_valid & QIF_ILIMITS) {
1637 dm->dqb_isoftlimit = di->dqb_isoftlimit;
1638 dm->dqb_ihardlimit = di->dqb_ihardlimit;
1641 if (di->dqb_valid & QIF_BTIME)
1642 dm->dqb_btime = di->dqb_btime;
1643 if (di->dqb_valid & QIF_ITIME)
1644 dm->dqb_itime = di->dqb_itime;
1647 if (!dm->dqb_bsoftlimit || toqb(dm->dqb_curspace) < dm->dqb_bsoftlimit) {
1649 clear_bit(DQ_BLKS_B, &dquot->dq_flags);
1651 else if (!(di->dqb_valid & QIF_BTIME)) /* Set grace only if user hasn't provided his own... */
1652 dm->dqb_btime = get_seconds() + sb_dqopt(dquot->dq_sb)->info[dquot->dq_type].dqi_bgrace;
1655 if (!dm->dqb_isoftlimit || dm->dqb_curinodes < dm->dqb_isoftlimit) {
1657 clear_bit(DQ_INODES_B, &dquot->dq_flags);
1659 else if (!(di->dqb_valid & QIF_ITIME)) /* Set grace only if user hasn't provided his own... */
1660 dm->dqb_itime = get_seconds() + sb_dqopt(dquot->dq_sb)->info[dquot->dq_type].dqi_igrace;
1662 if (dm->dqb_bhardlimit || dm->dqb_bsoftlimit || dm->dqb_ihardlimit || dm->dqb_isoftlimit)
1663 clear_bit(DQ_FAKE_B, &dquot->dq_flags);
1665 set_bit(DQ_FAKE_B, &dquot->dq_flags);
1666 spin_unlock(&dq_data_lock);
1667 mark_dquot_dirty(dquot);
1670 int vfs_set_dqblk(struct super_block *sb, int type, qid_t id, struct if_dqblk *di)
1672 struct dquot *dquot;
1674 mutex_lock(&sb_dqopt(sb)->dqonoff_mutex);
1675 if (!(dquot = dqget(sb, id, type))) {
1676 mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
1679 do_set_dqblk(dquot, di);
1681 mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
1685 /* Generic routine for getting common part of quota file information */
1686 int vfs_get_dqinfo(struct super_block *sb, int type, struct if_dqinfo *ii)
1688 struct mem_dqinfo *mi;
1690 mutex_lock(&sb_dqopt(sb)->dqonoff_mutex);
1691 if (!sb_has_quota_enabled(sb, type)) {
1692 mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
1695 mi = sb_dqopt(sb)->info + type;
1696 spin_lock(&dq_data_lock);
1697 ii->dqi_bgrace = mi->dqi_bgrace;
1698 ii->dqi_igrace = mi->dqi_igrace;
1699 ii->dqi_flags = mi->dqi_flags & DQF_MASK;
1700 ii->dqi_valid = IIF_ALL;
1701 spin_unlock(&dq_data_lock);
1702 mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
1706 /* Generic routine for setting common part of quota file information */
1707 int vfs_set_dqinfo(struct super_block *sb, int type, struct if_dqinfo *ii)
1709 struct mem_dqinfo *mi;
1711 mutex_lock(&sb_dqopt(sb)->dqonoff_mutex);
1712 if (!sb_has_quota_enabled(sb, type)) {
1713 mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
1716 mi = sb_dqopt(sb)->info + type;
1717 spin_lock(&dq_data_lock);
1718 if (ii->dqi_valid & IIF_BGRACE)
1719 mi->dqi_bgrace = ii->dqi_bgrace;
1720 if (ii->dqi_valid & IIF_IGRACE)
1721 mi->dqi_igrace = ii->dqi_igrace;
1722 if (ii->dqi_valid & IIF_FLAGS)
1723 mi->dqi_flags = (mi->dqi_flags & ~DQF_MASK) | (ii->dqi_flags & DQF_MASK);
1724 spin_unlock(&dq_data_lock);
1725 mark_info_dirty(sb, type);
1726 /* Force write to disk */
1727 sb->dq_op->write_info(sb, type);
1728 mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
1732 struct quotactl_ops vfs_quotactl_ops = {
1733 .quota_on = vfs_quota_on,
1734 .quota_off = vfs_quota_off,
1735 .quota_sync = vfs_quota_sync,
1736 .get_info = vfs_get_dqinfo,
1737 .set_info = vfs_set_dqinfo,
1738 .get_dqblk = vfs_get_dqblk,
1739 .set_dqblk = vfs_set_dqblk
1742 static ctl_table fs_dqstats_table[] = {
1744 .ctl_name = FS_DQ_LOOKUPS,
1745 .procname = "lookups",
1746 .data = &dqstats.lookups,
1747 .maxlen = sizeof(int),
1749 .proc_handler = &proc_dointvec,
1752 .ctl_name = FS_DQ_DROPS,
1753 .procname = "drops",
1754 .data = &dqstats.drops,
1755 .maxlen = sizeof(int),
1757 .proc_handler = &proc_dointvec,
1760 .ctl_name = FS_DQ_READS,
1761 .procname = "reads",
1762 .data = &dqstats.reads,
1763 .maxlen = sizeof(int),
1765 .proc_handler = &proc_dointvec,
1768 .ctl_name = FS_DQ_WRITES,
1769 .procname = "writes",
1770 .data = &dqstats.writes,
1771 .maxlen = sizeof(int),
1773 .proc_handler = &proc_dointvec,
1776 .ctl_name = FS_DQ_CACHE_HITS,
1777 .procname = "cache_hits",
1778 .data = &dqstats.cache_hits,
1779 .maxlen = sizeof(int),
1781 .proc_handler = &proc_dointvec,
1784 .ctl_name = FS_DQ_ALLOCATED,
1785 .procname = "allocated_dquots",
1786 .data = &dqstats.allocated_dquots,
1787 .maxlen = sizeof(int),
1789 .proc_handler = &proc_dointvec,
1792 .ctl_name = FS_DQ_FREE,
1793 .procname = "free_dquots",
1794 .data = &dqstats.free_dquots,
1795 .maxlen = sizeof(int),
1797 .proc_handler = &proc_dointvec,
1800 .ctl_name = FS_DQ_SYNCS,
1801 .procname = "syncs",
1802 .data = &dqstats.syncs,
1803 .maxlen = sizeof(int),
1805 .proc_handler = &proc_dointvec,
1808 .ctl_name = FS_DQ_WARNINGS,
1809 .procname = "warnings",
1810 .data = &flag_print_warnings,
1811 .maxlen = sizeof(int),
1813 .proc_handler = &proc_dointvec,
1818 static ctl_table fs_table[] = {
1820 .ctl_name = FS_DQSTATS,
1821 .procname = "quota",
1823 .child = fs_dqstats_table,
1828 static ctl_table sys_table[] = {
1838 static int __init dquot_init(void)
1841 unsigned long nr_hash, order;
1843 printk(KERN_NOTICE "VFS: Disk quotas %s\n", __DQUOT_VERSION__);
1845 register_sysctl_table(sys_table);
1847 dquot_cachep = kmem_cache_create("dquot",
1848 sizeof(struct dquot), sizeof(unsigned long) * 4,
1849 (SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT|
1850 SLAB_MEM_SPREAD|SLAB_PANIC),
1854 dquot_hash = (struct hlist_head *)__get_free_pages(GFP_ATOMIC, order);
1856 panic("Cannot create dquot hash table");
1858 /* Find power-of-two hlist_heads which can fit into allocation */
1859 nr_hash = (1UL << order) * PAGE_SIZE / sizeof(struct hlist_head);
1863 } while (nr_hash >> dq_hash_bits);
1866 nr_hash = 1UL << dq_hash_bits;
1867 dq_hash_mask = nr_hash - 1;
1868 for (i = 0; i < nr_hash; i++)
1869 INIT_HLIST_HEAD(dquot_hash + i);
1871 printk("Dquot-cache hash table entries: %ld (order %ld, %ld bytes)\n",
1872 nr_hash, order, (PAGE_SIZE << order));
1874 set_shrinker(DEFAULT_SEEKS, shrink_dqcache_memory);
1878 module_init(dquot_init);
1880 EXPORT_SYMBOL(register_quota_format);
1881 EXPORT_SYMBOL(unregister_quota_format);
1882 EXPORT_SYMBOL(dqstats);
1883 EXPORT_SYMBOL(dq_data_lock);
1884 EXPORT_SYMBOL(vfs_quota_on);
1885 EXPORT_SYMBOL(vfs_quota_on_mount);
1886 EXPORT_SYMBOL(vfs_quota_off);
1887 EXPORT_SYMBOL(vfs_quota_sync);
1888 EXPORT_SYMBOL(vfs_get_dqinfo);
1889 EXPORT_SYMBOL(vfs_set_dqinfo);
1890 EXPORT_SYMBOL(vfs_get_dqblk);
1891 EXPORT_SYMBOL(vfs_set_dqblk);
1892 EXPORT_SYMBOL(dquot_commit);
1893 EXPORT_SYMBOL(dquot_commit_info);
1894 EXPORT_SYMBOL(dquot_acquire);
1895 EXPORT_SYMBOL(dquot_release);
1896 EXPORT_SYMBOL(dquot_mark_dquot_dirty);
1897 EXPORT_SYMBOL(dquot_initialize);
1898 EXPORT_SYMBOL(dquot_drop);
1899 EXPORT_SYMBOL(dquot_alloc_space);
1900 EXPORT_SYMBOL(dquot_alloc_inode);
1901 EXPORT_SYMBOL(dquot_free_space);
1902 EXPORT_SYMBOL(dquot_free_inode);
1903 EXPORT_SYMBOL(dquot_transfer);