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
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>
79 #include <asm/uaccess.h>
81 #define __DQUOT_PARANOIA
84 * There are two quota SMP locks. dq_list_lock protects all lists with quotas
85 * and quota formats and also dqstats structure containing statistics about the
86 * lists. dq_data_lock protects data from dq_dqb and also mem_dqinfo structures
87 * and 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().
91 * The spinlock ordering is hence: dq_data_lock > dq_list_lock > i_lock
93 * Note that some things (eg. sb pointer, type, id) doesn't change during
94 * the life of the dquot structure and so needn't to be protected by a lock
96 * Any operation working on dquots via inode pointers must hold dqptr_sem. If
97 * operation is just reading pointers from inode (or not using them at all) the
98 * read lock is enough. If pointers are altered function must hold write lock.
99 * If operation is holding reference to dquot in other way (e.g. quotactl ops)
100 * it must be guarded by dqonoff_sem.
101 * This locking assures that:
102 * a) update/access to dquot pointers in inode is serialized
103 * b) everyone is guarded against invalidate_dquots()
105 * Each dquot has its dq_lock semaphore. Locked dquots might not be referenced
106 * from inodes (dquot_alloc_space() and such don't check the dq_lock).
107 * Currently dquot is locked only when it is being read to memory (or space for
108 * it is being allocated) on the first dqget() and when it is being released on
109 * the last dqput(). The allocation and release oparations are serialized by
110 * the dq_lock and by checking the use count in dquot_release(). Write
111 * operations on dquots don't hold dq_lock as they copy data under dq_data_lock
112 * spinlock to internal buffers before writing.
114 * Lock ordering (including journal_lock) is following:
115 * dqonoff_sem > journal_lock > dqptr_sem > dquot->dq_lock > dqio_sem
118 spinlock_t dq_list_lock = SPIN_LOCK_UNLOCKED;
119 spinlock_t dq_data_lock = SPIN_LOCK_UNLOCKED;
121 static char *quotatypes[] = INITQFNAMES;
122 static struct quota_format_type *quota_formats; /* List of registered formats */
123 static struct quota_module_name module_names[] = INIT_QUOTA_MODULE_NAMES;
125 int register_quota_format(struct quota_format_type *fmt)
127 spin_lock(&dq_list_lock);
128 fmt->qf_next = quota_formats;
130 spin_unlock(&dq_list_lock);
134 void unregister_quota_format(struct quota_format_type *fmt)
136 struct quota_format_type **actqf;
138 spin_lock(&dq_list_lock);
139 for (actqf = "a_formats; *actqf && *actqf != fmt; actqf = &(*actqf)->qf_next);
141 *actqf = (*actqf)->qf_next;
142 spin_unlock(&dq_list_lock);
145 static struct quota_format_type *find_quota_format(int id)
147 struct quota_format_type *actqf;
149 spin_lock(&dq_list_lock);
150 for (actqf = quota_formats; actqf && actqf->qf_fmt_id != id; actqf = actqf->qf_next);
151 if (!actqf || !try_module_get(actqf->qf_owner)) {
154 spin_unlock(&dq_list_lock);
156 for (qm = 0; module_names[qm].qm_fmt_id && module_names[qm].qm_fmt_id != id; qm++);
157 if (!module_names[qm].qm_fmt_id || request_module(module_names[qm].qm_mod_name))
160 spin_lock(&dq_list_lock);
161 for (actqf = quota_formats; actqf && actqf->qf_fmt_id != id; actqf = actqf->qf_next);
162 if (actqf && !try_module_get(actqf->qf_owner))
165 spin_unlock(&dq_list_lock);
169 static void put_quota_format(struct quota_format_type *fmt)
171 module_put(fmt->qf_owner);
175 * Dquot List Management:
176 * The quota code uses three lists for dquot management: the inuse_list,
177 * free_dquots, and dquot_hash[] array. A single dquot structure may be
178 * on all three lists, depending on its current state.
180 * All dquots are placed to the end of inuse_list when first created, and this
181 * list is used for the sync and invalidate operations, which must look
184 * Unused dquots (dq_count == 0) are added to the free_dquots list when freed,
185 * and this list is searched whenever we need an available dquot. Dquots are
186 * removed from the list as soon as they are used again, and
187 * dqstats.free_dquots gives the number of dquots on the list. When
188 * dquot is invalidated it's completely released from memory.
190 * Dquots with a specific identity (device, type and id) are placed on
191 * one of the dquot_hash[] hash chains. The provides an efficient search
192 * mechanism to locate a specific dquot.
195 static LIST_HEAD(inuse_list);
196 static LIST_HEAD(free_dquots);
197 unsigned int dq_hash_bits, dq_hash_mask;
198 static struct hlist_head *dquot_hash;
200 struct dqstats dqstats;
202 static void dqput(struct dquot *dquot);
204 static inline int const hashfn(struct super_block *sb, unsigned int id, int type)
206 unsigned long tmp = (((unsigned long)sb>>L1_CACHE_SHIFT) ^ id) * (MAXQUOTAS - type);
207 return (tmp + (tmp >> dq_hash_bits)) & dq_hash_mask;
211 * Following list functions expect dq_list_lock to be held
213 static inline void insert_dquot_hash(struct dquot *dquot)
215 struct hlist_head *head = dquot_hash + hashfn(dquot->dq_sb, dquot->dq_id, dquot->dq_type);
216 hlist_add_head(&dquot->dq_hash, head);
219 static inline void remove_dquot_hash(struct dquot *dquot)
221 hlist_del_init(&dquot->dq_hash);
224 static inline struct dquot *find_dquot(unsigned int hashent, struct super_block *sb, unsigned int id, int type)
226 struct hlist_node *node;
229 hlist_for_each (node, dquot_hash+hashent) {
230 dquot = hlist_entry(node, struct dquot, dq_hash);
231 if (dquot->dq_sb == sb && dquot->dq_id == id && dquot->dq_type == type)
237 /* Add a dquot to the tail of the free list */
238 static inline void put_dquot_last(struct dquot *dquot)
240 list_add(&dquot->dq_free, free_dquots.prev);
241 dqstats.free_dquots++;
244 static inline void remove_free_dquot(struct dquot *dquot)
246 if (list_empty(&dquot->dq_free))
248 list_del_init(&dquot->dq_free);
249 dqstats.free_dquots--;
252 static inline void put_inuse(struct dquot *dquot)
254 /* We add to the back of inuse list so we don't have to restart
255 * when traversing this list and we block */
256 list_add(&dquot->dq_inuse, inuse_list.prev);
257 dqstats.allocated_dquots++;
260 static inline void remove_inuse(struct dquot *dquot)
262 dqstats.allocated_dquots--;
263 list_del(&dquot->dq_inuse);
266 * End of list functions needing dq_list_lock
269 static void wait_on_dquot(struct dquot *dquot)
271 down(&dquot->dq_lock);
275 #define mark_dquot_dirty(dquot) ((dquot)->dq_sb->dq_op->mark_dirty(dquot))
277 int dquot_mark_dquot_dirty(struct dquot *dquot)
279 spin_lock(&dq_list_lock);
280 if (!test_and_set_bit(DQ_MOD_B, &dquot->dq_flags))
281 list_add(&dquot->dq_dirty, &sb_dqopt(dquot->dq_sb)->
282 info[dquot->dq_type].dqi_dirty_list);
283 spin_unlock(&dq_list_lock);
287 /* This function needs dq_list_lock */
288 static inline int clear_dquot_dirty(struct dquot *dquot)
290 if (!test_and_clear_bit(DQ_MOD_B, &dquot->dq_flags))
292 list_del_init(&dquot->dq_dirty);
296 void mark_info_dirty(struct super_block *sb, int type)
298 set_bit(DQF_INFO_DIRTY_B, &sb_dqopt(sb)->info[type].dqi_flags);
300 EXPORT_SYMBOL(mark_info_dirty);
303 * Read dquot from disk and alloc space for it
306 int dquot_acquire(struct dquot *dquot)
309 struct quota_info *dqopt = sb_dqopt(dquot->dq_sb);
311 down(&dquot->dq_lock);
312 down(&dqopt->dqio_sem);
313 if (!test_bit(DQ_READ_B, &dquot->dq_flags))
314 ret = dqopt->ops[dquot->dq_type]->read_dqblk(dquot);
317 set_bit(DQ_READ_B, &dquot->dq_flags);
318 /* Instantiate dquot if needed */
319 if (!test_bit(DQ_ACTIVE_B, &dquot->dq_flags) && !dquot->dq_off) {
320 ret = dqopt->ops[dquot->dq_type]->commit_dqblk(dquot);
324 set_bit(DQ_ACTIVE_B, &dquot->dq_flags);
326 up(&dqopt->dqio_sem);
332 * Write dquot to disk
334 int dquot_commit(struct dquot *dquot)
337 struct quota_info *dqopt = sb_dqopt(dquot->dq_sb);
339 down(&dqopt->dqio_sem);
340 spin_lock(&dq_list_lock);
341 if (!clear_dquot_dirty(dquot)) {
342 spin_unlock(&dq_list_lock);
345 spin_unlock(&dq_list_lock);
346 /* Inactive dquot can be only if there was error during read/init
347 * => we have better not writing it */
348 if (test_bit(DQ_ACTIVE_B, &dquot->dq_flags))
349 ret = dqopt->ops[dquot->dq_type]->commit_dqblk(dquot);
351 up(&dqopt->dqio_sem);
352 if (info_dirty(&dqopt->info[dquot->dq_type]))
353 dquot->dq_sb->dq_op->write_info(dquot->dq_sb, dquot->dq_type);
360 int dquot_release(struct dquot *dquot)
363 struct quota_info *dqopt = sb_dqopt(dquot->dq_sb);
365 down(&dquot->dq_lock);
366 /* Check whether we are not racing with some other dqget() */
367 if (atomic_read(&dquot->dq_count) > 1)
369 down(&dqopt->dqio_sem);
370 ret = dqopt->ops[dquot->dq_type]->release_dqblk(dquot);
371 clear_bit(DQ_ACTIVE_B, &dquot->dq_flags);
372 up(&dqopt->dqio_sem);
378 /* Invalidate all dquots on the list. Note that this function is called after
379 * quota is disabled and pointers from inodes removed so there cannot be new
380 * quota users. Also because we hold dqonoff_sem there can be no quota users
381 * for this sb+type at all. */
382 static void invalidate_dquots(struct super_block *sb, int type)
385 struct list_head *head;
387 spin_lock(&dq_list_lock);
388 for (head = inuse_list.next; head != &inuse_list;) {
389 dquot = list_entry(head, struct dquot, dq_inuse);
391 if (dquot->dq_sb != sb)
393 if (dquot->dq_type != type)
395 #ifdef __DQUOT_PARANOIA
396 if (atomic_read(&dquot->dq_count))
399 /* Quota now has no users and it has been written on last dqput() */
400 remove_dquot_hash(dquot);
401 remove_free_dquot(dquot);
403 kmem_cache_free(dquot_cachep, dquot);
405 spin_unlock(&dq_list_lock);
408 int vfs_quota_sync(struct super_block *sb, int type)
410 struct list_head *dirty;
412 struct quota_info *dqopt = sb_dqopt(sb);
415 down(&dqopt->dqonoff_sem);
416 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
417 if (type != -1 && cnt != type)
419 if (!sb_has_quota_enabled(sb, cnt))
421 spin_lock(&dq_list_lock);
422 dirty = &dqopt->info[cnt].dqi_dirty_list;
423 while (!list_empty(dirty)) {
424 dquot = list_entry(dirty->next, struct dquot, dq_dirty);
425 /* Dirty and inactive can be only bad dquot... */
426 if (!test_bit(DQ_ACTIVE_B, &dquot->dq_flags)) {
427 clear_dquot_dirty(dquot);
430 /* Now we have active dquot from which someone is
431 * holding reference so we can safely just increase
433 atomic_inc(&dquot->dq_count);
435 spin_unlock(&dq_list_lock);
436 sb->dq_op->write_dquot(dquot);
438 spin_lock(&dq_list_lock);
440 spin_unlock(&dq_list_lock);
443 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
444 if ((cnt == type || type == -1) && sb_has_quota_enabled(sb, cnt)
445 && info_dirty(&dqopt->info[cnt]))
446 sb->dq_op->write_info(sb, cnt);
447 spin_lock(&dq_list_lock);
449 spin_unlock(&dq_list_lock);
450 up(&dqopt->dqonoff_sem);
455 /* Free unused dquots from cache */
456 static void prune_dqcache(int count)
458 struct list_head *head;
461 head = free_dquots.prev;
462 while (head != &free_dquots && count) {
463 dquot = list_entry(head, struct dquot, dq_free);
464 remove_dquot_hash(dquot);
465 remove_free_dquot(dquot);
467 kmem_cache_free(dquot_cachep, dquot);
469 head = free_dquots.prev;
474 * This is called from kswapd when we think we need some
478 static int shrink_dqcache_memory(int nr, unsigned int gfp_mask)
482 spin_lock(&dq_list_lock);
485 ret = dqstats.allocated_dquots;
486 spin_unlock(&dq_list_lock);
491 * Put reference to dquot
492 * NOTE: If you change this function please check whether dqput_blocks() works right...
493 * MUST be called with either dqptr_sem or dqonoff_sem held
495 static void dqput(struct dquot *dquot)
499 #ifdef __DQUOT_PARANOIA
500 if (!atomic_read(&dquot->dq_count)) {
501 printk("VFS: dqput: trying to free free dquot\n");
502 printk("VFS: device %s, dquot of %s %d\n",
504 quotatypes[dquot->dq_type],
510 spin_lock(&dq_list_lock);
512 spin_unlock(&dq_list_lock);
514 spin_lock(&dq_list_lock);
515 if (atomic_read(&dquot->dq_count) > 1) {
516 /* We have more than one user... nothing to do */
517 atomic_dec(&dquot->dq_count);
518 spin_unlock(&dq_list_lock);
521 /* Need to release dquot? */
522 if (test_bit(DQ_ACTIVE_B, &dquot->dq_flags) && dquot_dirty(dquot)) {
523 spin_unlock(&dq_list_lock);
524 /* Commit dquot before releasing */
525 dquot->dq_sb->dq_op->write_dquot(dquot);
528 /* Clear flag in case dquot was inactive (something bad happened) */
529 clear_dquot_dirty(dquot);
530 if (test_bit(DQ_ACTIVE_B, &dquot->dq_flags)) {
531 spin_unlock(&dq_list_lock);
532 dquot_release(dquot);
535 atomic_dec(&dquot->dq_count);
536 #ifdef __DQUOT_PARANOIA
538 if (!list_empty(&dquot->dq_free))
541 put_dquot_last(dquot);
542 spin_unlock(&dq_list_lock);
545 static struct dquot *get_empty_dquot(struct super_block *sb, int type)
549 dquot = kmem_cache_alloc(dquot_cachep, SLAB_KERNEL);
553 memset((caddr_t)dquot, 0, sizeof(struct dquot));
554 sema_init(&dquot->dq_lock, 1);
555 INIT_LIST_HEAD(&dquot->dq_free);
556 INIT_LIST_HEAD(&dquot->dq_inuse);
557 INIT_HLIST_NODE(&dquot->dq_hash);
558 INIT_LIST_HEAD(&dquot->dq_dirty);
560 dquot->dq_type = type;
561 atomic_set(&dquot->dq_count, 1);
567 * Get reference to dquot
568 * MUST be called with either dqptr_sem or dqonoff_sem held
570 static struct dquot *dqget(struct super_block *sb, unsigned int id, int type)
572 unsigned int hashent = hashfn(sb, id, type);
573 struct dquot *dquot, *empty = NODQUOT;
575 if (!sb_has_quota_enabled(sb, type))
578 spin_lock(&dq_list_lock);
579 if ((dquot = find_dquot(hashent, sb, id, type)) == NODQUOT) {
580 if (empty == NODQUOT) {
581 spin_unlock(&dq_list_lock);
582 if ((empty = get_empty_dquot(sb, type)) == NODQUOT)
583 schedule(); /* Try to wait for a moment... */
588 /* all dquots go on the inuse_list */
590 /* hash it first so it can be found */
591 insert_dquot_hash(dquot);
593 spin_unlock(&dq_list_lock);
595 if (!atomic_read(&dquot->dq_count))
596 remove_free_dquot(dquot);
597 atomic_inc(&dquot->dq_count);
598 dqstats.cache_hits++;
600 spin_unlock(&dq_list_lock);
602 kmem_cache_free(dquot_cachep, empty);
604 /* Wait for dq_lock - after this we know that either dquot_release() is already
605 * finished or it will be canceled due to dq_count > 1 test */
606 wait_on_dquot(dquot);
607 /* Read the dquot and instantiate it (everything done only if needed) */
608 if (!test_bit(DQ_ACTIVE_B, &dquot->dq_flags) && dquot_acquire(dquot) < 0) {
612 #ifdef __DQUOT_PARANOIA
613 if (!dquot->dq_sb) /* Has somebody invalidated entry under us? */
620 static int dqinit_needed(struct inode *inode, int type)
624 if (IS_NOQUOTA(inode))
627 return inode->i_dquot[type] == NODQUOT;
628 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
629 if (inode->i_dquot[cnt] == NODQUOT)
634 /* This routine is guarded by dqonoff_sem semaphore */
635 static void add_dquot_ref(struct super_block *sb, int type)
641 list_for_each(p, &sb->s_files) {
642 struct file *filp = list_entry(p, struct file, f_list);
643 struct inode *inode = filp->f_dentry->d_inode;
644 if (filp->f_mode & FMODE_WRITE && dqinit_needed(inode, type)) {
645 struct dentry *dentry = dget(filp->f_dentry);
647 sb->dq_op->initialize(inode, type);
649 /* As we may have blocked we had better restart... */
656 /* Return 0 if dqput() won't block (note that 1 doesn't necessarily mean blocking) */
657 static inline int dqput_blocks(struct dquot *dquot)
659 if (atomic_read(&dquot->dq_count) <= 1)
664 /* Remove references to dquots from inode - add dquot to list for freeing if needed */
665 /* We can't race with anybody because we hold dqptr_sem for writing... */
666 int remove_inode_dquot_ref(struct inode *inode, int type, struct list_head *tofree_head)
668 struct dquot *dquot = inode->i_dquot[type];
671 inode->i_dquot[type] = NODQUOT;
672 /* any other quota in use? */
673 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
674 if (inode->i_dquot[cnt] != NODQUOT)
677 inode->i_flags &= ~S_QUOTA;
679 if (dquot != NODQUOT) {
680 if (dqput_blocks(dquot)) {
681 #ifdef __DQUOT_PARANOIA
682 if (atomic_read(&dquot->dq_count) != 1)
683 printk(KERN_WARNING "VFS: Adding dquot with dq_count %d to dispose list.\n", atomic_read(&dquot->dq_count));
685 spin_lock(&dq_list_lock);
686 list_add(&dquot->dq_free, tofree_head); /* As dquot must have currently users it can't be on the free list... */
687 spin_unlock(&dq_list_lock);
691 dqput(dquot); /* We have guaranteed we won't block */
696 /* Free list of dquots - called from inode.c */
697 /* dquots are removed from inodes, no new references can be got so we are the only ones holding reference */
698 static void put_dquot_list(struct list_head *tofree_head)
700 struct list_head *act_head;
703 act_head = tofree_head->next;
704 /* So now we have dquots on the list... Just free them */
705 while (act_head != tofree_head) {
706 dquot = list_entry(act_head, struct dquot, dq_free);
707 act_head = act_head->next;
708 list_del_init(&dquot->dq_free); /* Remove dquot from the list so we won't have problems... */
713 /* Function in inode.c - remove pointers to dquots in icache */
714 extern void remove_dquot_ref(struct super_block *, int, struct list_head *);
716 /* Gather all references from inodes and drop them */
717 static void drop_dquot_ref(struct super_block *sb, int type)
719 LIST_HEAD(tofree_head);
721 down_write(&sb_dqopt(sb)->dqptr_sem);
722 remove_dquot_ref(sb, type, &tofree_head);
723 up_write(&sb_dqopt(sb)->dqptr_sem);
724 put_dquot_list(&tofree_head);
727 static inline void dquot_incr_inodes(struct dquot *dquot, unsigned long number)
729 dquot->dq_dqb.dqb_curinodes += number;
732 static inline void dquot_incr_space(struct dquot *dquot, qsize_t number)
734 dquot->dq_dqb.dqb_curspace += number;
737 static inline void dquot_decr_inodes(struct dquot *dquot, unsigned long number)
739 if (dquot->dq_dqb.dqb_curinodes > number)
740 dquot->dq_dqb.dqb_curinodes -= number;
742 dquot->dq_dqb.dqb_curinodes = 0;
743 if (dquot->dq_dqb.dqb_curinodes < dquot->dq_dqb.dqb_isoftlimit)
744 dquot->dq_dqb.dqb_itime = (time_t) 0;
745 clear_bit(DQ_INODES_B, &dquot->dq_flags);
748 static inline void dquot_decr_space(struct dquot *dquot, qsize_t number)
750 if (dquot->dq_dqb.dqb_curspace > number)
751 dquot->dq_dqb.dqb_curspace -= number;
753 dquot->dq_dqb.dqb_curspace = 0;
754 if (toqb(dquot->dq_dqb.dqb_curspace) < dquot->dq_dqb.dqb_bsoftlimit)
755 dquot->dq_dqb.dqb_btime = (time_t) 0;
756 clear_bit(DQ_BLKS_B, &dquot->dq_flags);
759 static inline int need_print_warning(struct dquot *dquot)
761 switch (dquot->dq_type) {
763 return current->fsuid == dquot->dq_id;
765 return in_group_p(dquot->dq_id);
770 /* Values of warnings */
773 #define ISOFTLONGWARN 2
776 #define BSOFTLONGWARN 5
779 /* Print warning to user which exceeded quota */
780 static void print_warning(struct dquot *dquot, const char warntype)
783 int flag = (warntype == BHARDWARN || warntype == BSOFTLONGWARN) ? DQ_BLKS_B :
784 ((warntype == IHARDWARN || warntype == ISOFTLONGWARN) ? DQ_INODES_B : 0);
786 if (!need_print_warning(dquot) || (flag && test_and_set_bit(flag, &dquot->dq_flags)))
788 tty_write_message(current->signal->tty, dquot->dq_sb->s_id);
789 if (warntype == ISOFTWARN || warntype == BSOFTWARN)
790 tty_write_message(current->signal->tty, ": warning, ");
792 tty_write_message(current->signal->tty, ": write failed, ");
793 tty_write_message(current->signal->tty, quotatypes[dquot->dq_type]);
796 msg = " file limit reached.\n";
799 msg = " file quota exceeded too long.\n";
802 msg = " file quota exceeded.\n";
805 msg = " block limit reached.\n";
808 msg = " block quota exceeded too long.\n";
811 msg = " block quota exceeded.\n";
814 tty_write_message(current->signal->tty, msg);
817 static inline void flush_warnings(struct dquot **dquots, char *warntype)
821 for (i = 0; i < MAXQUOTAS; i++)
822 if (dquots[i] != NODQUOT && warntype[i] != NOWARN)
823 print_warning(dquots[i], warntype[i]);
826 static inline char ignore_hardlimit(struct dquot *dquot)
828 struct mem_dqinfo *info = &sb_dqopt(dquot->dq_sb)->info[dquot->dq_type];
830 return capable(CAP_SYS_RESOURCE) &&
831 (info->dqi_format->qf_fmt_id != QFMT_VFS_OLD || !(info->dqi_flags & V1_DQF_RSQUASH));
834 /* needs dq_data_lock */
835 static int check_idq(struct dquot *dquot, ulong inodes, char *warntype)
838 if (inodes <= 0 || test_bit(DQ_FAKE_B, &dquot->dq_flags))
841 if (dquot->dq_dqb.dqb_ihardlimit &&
842 (dquot->dq_dqb.dqb_curinodes + inodes) > dquot->dq_dqb.dqb_ihardlimit &&
843 !ignore_hardlimit(dquot)) {
844 *warntype = IHARDWARN;
848 if (dquot->dq_dqb.dqb_isoftlimit &&
849 (dquot->dq_dqb.dqb_curinodes + inodes) > dquot->dq_dqb.dqb_isoftlimit &&
850 dquot->dq_dqb.dqb_itime && get_seconds() >= dquot->dq_dqb.dqb_itime &&
851 !ignore_hardlimit(dquot)) {
852 *warntype = ISOFTLONGWARN;
856 if (dquot->dq_dqb.dqb_isoftlimit &&
857 (dquot->dq_dqb.dqb_curinodes + inodes) > dquot->dq_dqb.dqb_isoftlimit &&
858 dquot->dq_dqb.dqb_itime == 0) {
859 *warntype = ISOFTWARN;
860 dquot->dq_dqb.dqb_itime = get_seconds() + sb_dqopt(dquot->dq_sb)->info[dquot->dq_type].dqi_igrace;
866 /* needs dq_data_lock */
867 static int check_bdq(struct dquot *dquot, qsize_t space, int prealloc, char *warntype)
870 if (space <= 0 || test_bit(DQ_FAKE_B, &dquot->dq_flags))
873 if (dquot->dq_dqb.dqb_bhardlimit &&
874 toqb(dquot->dq_dqb.dqb_curspace + space) > dquot->dq_dqb.dqb_bhardlimit &&
875 !ignore_hardlimit(dquot)) {
877 *warntype = BHARDWARN;
881 if (dquot->dq_dqb.dqb_bsoftlimit &&
882 toqb(dquot->dq_dqb.dqb_curspace + space) > dquot->dq_dqb.dqb_bsoftlimit &&
883 dquot->dq_dqb.dqb_btime && get_seconds() >= dquot->dq_dqb.dqb_btime &&
884 !ignore_hardlimit(dquot)) {
886 *warntype = BSOFTLONGWARN;
890 if (dquot->dq_dqb.dqb_bsoftlimit &&
891 toqb(dquot->dq_dqb.dqb_curspace + space) > dquot->dq_dqb.dqb_bsoftlimit &&
892 dquot->dq_dqb.dqb_btime == 0) {
894 *warntype = BSOFTWARN;
895 dquot->dq_dqb.dqb_btime = get_seconds() + sb_dqopt(dquot->dq_sb)->info[dquot->dq_type].dqi_bgrace;
899 * We don't allow preallocation to exceed softlimit so exceeding will
909 * Initialize quota pointers in inode
910 * Transaction must be started at entry
912 int dquot_initialize(struct inode *inode, int type)
917 /* First test before acquiring semaphore - solves deadlocks when we
918 * re-enter the quota code and are already holding the semaphore */
919 if (IS_NOQUOTA(inode))
921 down_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
922 /* Having dqptr_sem we know NOQUOTA flags can't be altered... */
923 if (IS_NOQUOTA(inode))
925 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
926 if (type != -1 && cnt != type)
928 if (inode->i_dquot[cnt] == NODQUOT) {
937 inode->i_dquot[cnt] = dqget(inode->i_sb, id, cnt);
938 if (inode->i_dquot[cnt])
939 inode->i_flags |= S_QUOTA;
943 up_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
948 * Release all quotas referenced by inode
949 * Transaction must be started at an entry
951 int dquot_drop(struct inode *inode)
955 down_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
956 inode->i_flags &= ~S_QUOTA;
957 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
958 if (inode->i_dquot[cnt] != NODQUOT) {
959 dqput(inode->i_dquot[cnt]);
960 inode->i_dquot[cnt] = NODQUOT;
963 up_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
968 * Following four functions update i_blocks+i_bytes fields and
969 * quota information (together with appropriate checks)
970 * NOTE: We absolutely rely on the fact that caller dirties
971 * the inode (usually macros in quotaops.h care about this) and
972 * holds a handle for the current transaction so that dquot write and
973 * inode write go into the same transaction.
977 * This operation can block, but only after everything is updated
979 int dquot_alloc_space(struct inode *inode, qsize_t number, int warn)
981 int cnt, ret = NO_QUOTA;
982 char warntype[MAXQUOTAS];
984 /* First test before acquiring semaphore - solves deadlocks when we
985 * re-enter the quota code and are already holding the semaphore */
986 if (IS_NOQUOTA(inode)) {
988 inode_add_bytes(inode, number);
991 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
992 warntype[cnt] = NOWARN;
994 down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
995 if (IS_NOQUOTA(inode)) { /* Now we can do reliable test... */
996 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
999 spin_lock(&dq_data_lock);
1000 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1001 if (inode->i_dquot[cnt] == NODQUOT)
1003 if (check_bdq(inode->i_dquot[cnt], number, warn, warntype+cnt) == NO_QUOTA)
1006 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1007 if (inode->i_dquot[cnt] == NODQUOT)
1009 dquot_incr_space(inode->i_dquot[cnt], number);
1011 inode_add_bytes(inode, number);
1014 spin_unlock(&dq_data_lock);
1015 if (ret == QUOTA_OK)
1016 /* Dirtify all the dquots - this can block when journalling */
1017 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1018 if (inode->i_dquot[cnt])
1019 mark_dquot_dirty(inode->i_dquot[cnt]);
1020 flush_warnings(inode->i_dquot, warntype);
1021 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1026 * This operation can block, but only after everything is updated
1028 int dquot_alloc_inode(const struct inode *inode, unsigned long number)
1030 int cnt, ret = NO_QUOTA;
1031 char warntype[MAXQUOTAS];
1033 /* First test before acquiring semaphore - solves deadlocks when we
1034 * re-enter the quota code and are already holding the semaphore */
1035 if (IS_NOQUOTA(inode))
1037 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1038 warntype[cnt] = NOWARN;
1039 down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1040 if (IS_NOQUOTA(inode)) {
1041 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1044 spin_lock(&dq_data_lock);
1045 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1046 if (inode->i_dquot[cnt] == NODQUOT)
1048 if (check_idq(inode->i_dquot[cnt], number, warntype+cnt) == NO_QUOTA)
1052 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1053 if (inode->i_dquot[cnt] == NODQUOT)
1055 dquot_incr_inodes(inode->i_dquot[cnt], number);
1059 spin_unlock(&dq_data_lock);
1060 if (ret == QUOTA_OK)
1061 /* Dirtify all the dquots - this can block when journalling */
1062 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1063 if (inode->i_dquot[cnt])
1064 mark_dquot_dirty(inode->i_dquot[cnt]);
1065 flush_warnings((struct dquot **)inode->i_dquot, warntype);
1066 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1071 * This is a non-blocking operation.
1073 int dquot_free_space(struct inode *inode, qsize_t number)
1077 /* First test before acquiring semaphore - solves deadlocks when we
1078 * re-enter the quota code and are already holding the semaphore */
1079 if (IS_NOQUOTA(inode)) {
1081 inode_sub_bytes(inode, number);
1084 down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1085 /* Now recheck reliably when holding dqptr_sem */
1086 if (IS_NOQUOTA(inode)) {
1087 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1090 spin_lock(&dq_data_lock);
1091 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1092 if (inode->i_dquot[cnt] == NODQUOT)
1094 dquot_decr_space(inode->i_dquot[cnt], number);
1096 inode_sub_bytes(inode, number);
1097 spin_unlock(&dq_data_lock);
1098 /* Dirtify all the dquots - this can block when journalling */
1099 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1100 if (inode->i_dquot[cnt])
1101 mark_dquot_dirty(inode->i_dquot[cnt]);
1102 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1107 * This is a non-blocking operation.
1109 int dquot_free_inode(const struct inode *inode, unsigned long number)
1113 /* First test before acquiring semaphore - solves deadlocks when we
1114 * re-enter the quota code and are already holding the semaphore */
1115 if (IS_NOQUOTA(inode))
1117 down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1118 /* Now recheck reliably when holding dqptr_sem */
1119 if (IS_NOQUOTA(inode)) {
1120 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1123 spin_lock(&dq_data_lock);
1124 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1125 if (inode->i_dquot[cnt] == NODQUOT)
1127 dquot_decr_inodes(inode->i_dquot[cnt], number);
1129 spin_unlock(&dq_data_lock);
1130 /* Dirtify all the dquots - this can block when journalling */
1131 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1132 if (inode->i_dquot[cnt])
1133 mark_dquot_dirty(inode->i_dquot[cnt]);
1134 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1139 * Transfer the number of inode and blocks from one diskquota to an other.
1141 * This operation can block, but only after everything is updated
1143 int dquot_transfer(struct inode *inode, struct iattr *iattr)
1146 struct dquot *transfer_from[MAXQUOTAS];
1147 struct dquot *transfer_to[MAXQUOTAS];
1148 int cnt, ret = NO_QUOTA, chuid = (iattr->ia_valid & ATTR_UID) && inode->i_uid != iattr->ia_uid,
1149 chgid = (iattr->ia_valid & ATTR_GID) && inode->i_gid != iattr->ia_gid;
1150 char warntype[MAXQUOTAS];
1152 /* First test before acquiring semaphore - solves deadlocks when we
1153 * re-enter the quota code and are already holding the semaphore */
1154 if (IS_NOQUOTA(inode))
1156 /* Clear the arrays */
1157 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1158 transfer_to[cnt] = transfer_from[cnt] = NODQUOT;
1159 warntype[cnt] = NOWARN;
1161 down_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
1162 /* Now recheck reliably when holding dqptr_sem */
1163 if (IS_NOQUOTA(inode)) { /* File without quota accounting? */
1164 up_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
1167 /* First build the transfer_to list - here we can block on
1168 * reading/instantiating of dquots. We know that the transaction for
1169 * us was already started so we don't violate lock ranking here */
1170 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1175 transfer_to[cnt] = dqget(inode->i_sb, iattr->ia_uid, cnt);
1180 transfer_to[cnt] = dqget(inode->i_sb, iattr->ia_gid, cnt);
1184 spin_lock(&dq_data_lock);
1185 space = inode_get_bytes(inode);
1186 /* Build the transfer_from list and check the limits */
1187 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1188 if (transfer_to[cnt] == NODQUOT)
1190 transfer_from[cnt] = inode->i_dquot[cnt];
1191 if (check_idq(transfer_to[cnt], 1, warntype+cnt) == NO_QUOTA ||
1192 check_bdq(transfer_to[cnt], space, 0, warntype+cnt) == NO_QUOTA)
1197 * Finally perform the needed transfer from transfer_from to transfer_to
1199 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1201 * Skip changes for same uid or gid or for turned off quota-type.
1203 if (transfer_to[cnt] == NODQUOT)
1206 dquot_decr_inodes(transfer_from[cnt], 1);
1207 dquot_decr_space(transfer_from[cnt], space);
1209 dquot_incr_inodes(transfer_to[cnt], 1);
1210 dquot_incr_space(transfer_to[cnt], space);
1212 inode->i_dquot[cnt] = transfer_to[cnt];
1216 spin_unlock(&dq_data_lock);
1217 /* Dirtify all the dquots - this can block when journalling */
1218 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1219 if (transfer_from[cnt])
1220 mark_dquot_dirty(transfer_from[cnt]);
1221 if (transfer_to[cnt])
1222 mark_dquot_dirty(transfer_to[cnt]);
1224 flush_warnings(transfer_to, warntype);
1226 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1227 if (ret == QUOTA_OK && transfer_from[cnt] != NODQUOT)
1228 dqput(transfer_from[cnt]);
1229 if (ret == NO_QUOTA && transfer_to[cnt] != NODQUOT)
1230 dqput(transfer_to[cnt]);
1232 up_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
1237 * Write info of quota file to disk
1239 int dquot_commit_info(struct super_block *sb, int type)
1242 struct quota_info *dqopt = sb_dqopt(sb);
1244 down(&dqopt->dqio_sem);
1245 ret = dqopt->ops[type]->write_file_info(sb, type);
1246 up(&dqopt->dqio_sem);
1251 * Definitions of diskquota operations.
1253 struct dquot_operations dquot_operations = {
1254 .initialize = dquot_initialize,
1256 .alloc_space = dquot_alloc_space,
1257 .alloc_inode = dquot_alloc_inode,
1258 .free_space = dquot_free_space,
1259 .free_inode = dquot_free_inode,
1260 .transfer = dquot_transfer,
1261 .write_dquot = dquot_commit,
1262 .mark_dirty = dquot_mark_dquot_dirty,
1263 .write_info = dquot_commit_info
1266 static inline void set_enable_flags(struct quota_info *dqopt, int type)
1270 dqopt->flags |= DQUOT_USR_ENABLED;
1273 dqopt->flags |= DQUOT_GRP_ENABLED;
1278 static inline void reset_enable_flags(struct quota_info *dqopt, int type)
1282 dqopt->flags &= ~DQUOT_USR_ENABLED;
1285 dqopt->flags &= ~DQUOT_GRP_ENABLED;
1291 * Turn quota off on a device. type == -1 ==> quotaoff for all types (umount)
1293 int vfs_quota_off(struct super_block *sb, int type)
1296 struct quota_info *dqopt = sb_dqopt(sb);
1298 /* We need to serialize quota_off() for device */
1299 down(&dqopt->dqonoff_sem);
1300 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1301 if (type != -1 && cnt != type)
1303 if (!sb_has_quota_enabled(sb, cnt))
1305 reset_enable_flags(dqopt, cnt);
1307 /* Note: these are blocking operations */
1308 drop_dquot_ref(sb, cnt);
1309 invalidate_dquots(sb, cnt);
1311 * Now all dquots should be invalidated, all writes done so we should be only
1312 * users of the info. No locks needed.
1314 if (info_dirty(&dqopt->info[cnt]))
1315 sb->dq_op->write_info(sb, cnt);
1316 if (dqopt->ops[cnt]->free_file_info)
1317 dqopt->ops[cnt]->free_file_info(sb, cnt);
1318 put_quota_format(dqopt->info[cnt].dqi_format);
1320 fput(dqopt->files[cnt]);
1321 dqopt->files[cnt] = NULL;
1322 dqopt->info[cnt].dqi_flags = 0;
1323 dqopt->info[cnt].dqi_igrace = 0;
1324 dqopt->info[cnt].dqi_bgrace = 0;
1325 dqopt->ops[cnt] = NULL;
1327 up(&dqopt->dqonoff_sem);
1332 * Turn quotas on on a device
1335 /* Helper function when we already have file open */
1336 static int vfs_quota_on_file(struct file *f, int type, int format_id)
1338 struct quota_format_type *fmt = find_quota_format(format_id);
1339 struct inode *inode;
1340 struct super_block *sb = f->f_dentry->d_sb;
1341 struct quota_info *dqopt = sb_dqopt(sb);
1342 struct dquot *to_drop[MAXQUOTAS];
1344 unsigned int oldflags;
1349 if (!f->f_op || !f->f_op->read || !f->f_op->write)
1351 inode = f->f_dentry->d_inode;
1353 if (!S_ISREG(inode->i_mode))
1356 down(&dqopt->dqonoff_sem);
1357 if (sb_has_quota_enabled(sb, type)) {
1361 oldflags = inode->i_flags;
1362 dqopt->files[type] = f;
1364 if (!fmt->qf_ops->check_quota_file(sb, type))
1366 /* We don't want quota and atime on quota files (deadlocks possible) */
1367 down_write(&dqopt->dqptr_sem);
1368 inode->i_flags |= S_NOQUOTA | S_NOATIME;
1369 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1370 to_drop[cnt] = inode->i_dquot[cnt];
1371 inode->i_dquot[cnt] = NODQUOT;
1373 inode->i_flags &= ~S_QUOTA;
1374 up_write(&dqopt->dqptr_sem);
1375 /* We must put dquots outside of dqptr_sem because we may need to
1376 * start transaction for dquot_release() */
1377 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1379 dqput(to_drop[cnt]);
1382 dqopt->ops[type] = fmt->qf_ops;
1383 dqopt->info[type].dqi_format = fmt;
1384 INIT_LIST_HEAD(&dqopt->info[type].dqi_dirty_list);
1385 down(&dqopt->dqio_sem);
1386 if ((error = dqopt->ops[type]->read_file_info(sb, type)) < 0) {
1387 up(&dqopt->dqio_sem);
1390 up(&dqopt->dqio_sem);
1391 set_enable_flags(dqopt, type);
1393 add_dquot_ref(sb, type);
1394 up(&dqopt->dqonoff_sem);
1399 inode->i_flags = oldflags;
1400 dqopt->files[type] = NULL;
1402 up_write(&dqopt->dqptr_sem);
1403 up(&dqopt->dqonoff_sem);
1405 put_quota_format(fmt);
1410 /* Actual function called from quotactl() */
1411 int vfs_quota_on(struct super_block *sb, int type, int format_id, char *path)
1416 f = filp_open(path, O_RDWR, 0600);
1419 error = security_quota_on(f);
1422 error = vfs_quota_on_file(f, type, format_id);
1426 filp_close(f, NULL);
1431 * Function used by filesystems when filp_open() would fail (filesystem is
1432 * being mounted now). We will use a private file structure. Caller is
1433 * responsible that it's IO functions won't need vfsmnt structure or
1434 * some dentry tricks...
1436 int vfs_quota_on_mount(int type, int format_id, struct dentry *dentry)
1441 dget(dentry); /* Get a reference for struct file */
1442 f = dentry_open(dentry, NULL, O_RDWR);
1447 error = vfs_quota_on_file(f, type, format_id);
1456 /* Generic routine for getting common part of quota structure */
1457 static void do_get_dqblk(struct dquot *dquot, struct if_dqblk *di)
1459 struct mem_dqblk *dm = &dquot->dq_dqb;
1461 spin_lock(&dq_data_lock);
1462 di->dqb_bhardlimit = dm->dqb_bhardlimit;
1463 di->dqb_bsoftlimit = dm->dqb_bsoftlimit;
1464 di->dqb_curspace = dm->dqb_curspace;
1465 di->dqb_ihardlimit = dm->dqb_ihardlimit;
1466 di->dqb_isoftlimit = dm->dqb_isoftlimit;
1467 di->dqb_curinodes = dm->dqb_curinodes;
1468 di->dqb_btime = dm->dqb_btime;
1469 di->dqb_itime = dm->dqb_itime;
1470 di->dqb_valid = QIF_ALL;
1471 spin_unlock(&dq_data_lock);
1474 int vfs_get_dqblk(struct super_block *sb, int type, qid_t id, struct if_dqblk *di)
1476 struct dquot *dquot;
1478 down(&sb_dqopt(sb)->dqonoff_sem);
1479 if (!(dquot = dqget(sb, id, type))) {
1480 up(&sb_dqopt(sb)->dqonoff_sem);
1483 do_get_dqblk(dquot, di);
1485 up(&sb_dqopt(sb)->dqonoff_sem);
1489 /* Generic routine for setting common part of quota structure */
1490 static void do_set_dqblk(struct dquot *dquot, struct if_dqblk *di)
1492 struct mem_dqblk *dm = &dquot->dq_dqb;
1493 int check_blim = 0, check_ilim = 0;
1495 spin_lock(&dq_data_lock);
1496 if (di->dqb_valid & QIF_SPACE) {
1497 dm->dqb_curspace = di->dqb_curspace;
1500 if (di->dqb_valid & QIF_BLIMITS) {
1501 dm->dqb_bsoftlimit = di->dqb_bsoftlimit;
1502 dm->dqb_bhardlimit = di->dqb_bhardlimit;
1505 if (di->dqb_valid & QIF_INODES) {
1506 dm->dqb_curinodes = di->dqb_curinodes;
1509 if (di->dqb_valid & QIF_ILIMITS) {
1510 dm->dqb_isoftlimit = di->dqb_isoftlimit;
1511 dm->dqb_ihardlimit = di->dqb_ihardlimit;
1514 if (di->dqb_valid & QIF_BTIME)
1515 dm->dqb_btime = di->dqb_btime;
1516 if (di->dqb_valid & QIF_ITIME)
1517 dm->dqb_itime = di->dqb_itime;
1520 if (!dm->dqb_bsoftlimit || toqb(dm->dqb_curspace) < dm->dqb_bsoftlimit) {
1522 clear_bit(DQ_BLKS_B, &dquot->dq_flags);
1524 else if (!(di->dqb_valid & QIF_BTIME)) /* Set grace only if user hasn't provided his own... */
1525 dm->dqb_btime = get_seconds() + sb_dqopt(dquot->dq_sb)->info[dquot->dq_type].dqi_bgrace;
1528 if (!dm->dqb_isoftlimit || dm->dqb_curinodes < dm->dqb_isoftlimit) {
1530 clear_bit(DQ_INODES_B, &dquot->dq_flags);
1532 else if (!(di->dqb_valid & QIF_ITIME)) /* Set grace only if user hasn't provided his own... */
1533 dm->dqb_itime = get_seconds() + sb_dqopt(dquot->dq_sb)->info[dquot->dq_type].dqi_igrace;
1535 if (dm->dqb_bhardlimit || dm->dqb_bsoftlimit || dm->dqb_ihardlimit || dm->dqb_isoftlimit)
1536 clear_bit(DQ_FAKE_B, &dquot->dq_flags);
1538 set_bit(DQ_FAKE_B, &dquot->dq_flags);
1539 spin_unlock(&dq_data_lock);
1540 mark_dquot_dirty(dquot);
1543 int vfs_set_dqblk(struct super_block *sb, int type, qid_t id, struct if_dqblk *di)
1545 struct dquot *dquot;
1547 down(&sb_dqopt(sb)->dqonoff_sem);
1548 if (!(dquot = dqget(sb, id, type))) {
1549 up(&sb_dqopt(sb)->dqonoff_sem);
1552 do_set_dqblk(dquot, di);
1554 up(&sb_dqopt(sb)->dqonoff_sem);
1558 /* Generic routine for getting common part of quota file information */
1559 int vfs_get_dqinfo(struct super_block *sb, int type, struct if_dqinfo *ii)
1561 struct mem_dqinfo *mi;
1563 down(&sb_dqopt(sb)->dqonoff_sem);
1564 if (!sb_has_quota_enabled(sb, type)) {
1565 up(&sb_dqopt(sb)->dqonoff_sem);
1568 mi = sb_dqopt(sb)->info + type;
1569 spin_lock(&dq_data_lock);
1570 ii->dqi_bgrace = mi->dqi_bgrace;
1571 ii->dqi_igrace = mi->dqi_igrace;
1572 ii->dqi_flags = mi->dqi_flags & DQF_MASK;
1573 ii->dqi_valid = IIF_ALL;
1574 spin_unlock(&dq_data_lock);
1575 up(&sb_dqopt(sb)->dqonoff_sem);
1579 /* Generic routine for setting common part of quota file information */
1580 int vfs_set_dqinfo(struct super_block *sb, int type, struct if_dqinfo *ii)
1582 struct mem_dqinfo *mi;
1584 down(&sb_dqopt(sb)->dqonoff_sem);
1585 if (!sb_has_quota_enabled(sb, type)) {
1586 up(&sb_dqopt(sb)->dqonoff_sem);
1589 mi = sb_dqopt(sb)->info + type;
1590 spin_lock(&dq_data_lock);
1591 if (ii->dqi_valid & IIF_BGRACE)
1592 mi->dqi_bgrace = ii->dqi_bgrace;
1593 if (ii->dqi_valid & IIF_IGRACE)
1594 mi->dqi_igrace = ii->dqi_igrace;
1595 if (ii->dqi_valid & IIF_FLAGS)
1596 mi->dqi_flags = (mi->dqi_flags & ~DQF_MASK) | (ii->dqi_flags & DQF_MASK);
1597 spin_unlock(&dq_data_lock);
1598 mark_info_dirty(sb, type);
1599 /* Force write to disk */
1600 sb->dq_op->write_info(sb, type);
1601 up(&sb_dqopt(sb)->dqonoff_sem);
1605 struct quotactl_ops vfs_quotactl_ops = {
1606 .quota_on = vfs_quota_on,
1607 .quota_off = vfs_quota_off,
1608 .quota_sync = vfs_quota_sync,
1609 .get_info = vfs_get_dqinfo,
1610 .set_info = vfs_set_dqinfo,
1611 .get_dqblk = vfs_get_dqblk,
1612 .set_dqblk = vfs_set_dqblk
1615 static ctl_table fs_dqstats_table[] = {
1617 .ctl_name = FS_DQ_LOOKUPS,
1618 .procname = "lookups",
1619 .data = &dqstats.lookups,
1620 .maxlen = sizeof(int),
1622 .proc_handler = &proc_dointvec,
1625 .ctl_name = FS_DQ_DROPS,
1626 .procname = "drops",
1627 .data = &dqstats.drops,
1628 .maxlen = sizeof(int),
1630 .proc_handler = &proc_dointvec,
1633 .ctl_name = FS_DQ_READS,
1634 .procname = "reads",
1635 .data = &dqstats.reads,
1636 .maxlen = sizeof(int),
1638 .proc_handler = &proc_dointvec,
1641 .ctl_name = FS_DQ_WRITES,
1642 .procname = "writes",
1643 .data = &dqstats.writes,
1644 .maxlen = sizeof(int),
1646 .proc_handler = &proc_dointvec,
1649 .ctl_name = FS_DQ_CACHE_HITS,
1650 .procname = "cache_hits",
1651 .data = &dqstats.cache_hits,
1652 .maxlen = sizeof(int),
1654 .proc_handler = &proc_dointvec,
1657 .ctl_name = FS_DQ_ALLOCATED,
1658 .procname = "allocated_dquots",
1659 .data = &dqstats.allocated_dquots,
1660 .maxlen = sizeof(int),
1662 .proc_handler = &proc_dointvec,
1665 .ctl_name = FS_DQ_FREE,
1666 .procname = "free_dquots",
1667 .data = &dqstats.free_dquots,
1668 .maxlen = sizeof(int),
1670 .proc_handler = &proc_dointvec,
1673 .ctl_name = FS_DQ_SYNCS,
1674 .procname = "syncs",
1675 .data = &dqstats.syncs,
1676 .maxlen = sizeof(int),
1678 .proc_handler = &proc_dointvec,
1683 static ctl_table fs_table[] = {
1685 .ctl_name = FS_DQSTATS,
1686 .procname = "quota",
1688 .child = fs_dqstats_table,
1693 static ctl_table sys_table[] = {
1703 /* SLAB cache for dquot structures */
1704 kmem_cache_t *dquot_cachep;
1706 static int __init dquot_init(void)
1709 unsigned long nr_hash, order;
1711 printk(KERN_NOTICE "VFS: Disk quotas %s\n", __DQUOT_VERSION__);
1713 register_sysctl_table(sys_table, 0);
1715 dquot_cachep = kmem_cache_create("dquot",
1716 sizeof(struct dquot), sizeof(unsigned long) * 4,
1717 SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT, NULL, NULL);
1719 panic("Cannot create dquot SLAB cache");
1722 dquot_hash = (struct hlist_head *)__get_free_pages(GFP_ATOMIC, order);
1724 panic("Cannot create dquot hash table");
1726 /* Find power-of-two hlist_heads which can fit into allocation */
1727 nr_hash = (1UL << order) * PAGE_SIZE / sizeof(struct hlist_head);
1731 } while (nr_hash >> dq_hash_bits);
1734 nr_hash = 1UL << dq_hash_bits;
1735 dq_hash_mask = nr_hash - 1;
1736 for (i = 0; i < nr_hash; i++)
1737 INIT_HLIST_HEAD(dquot_hash + i);
1739 printk("Dquot-cache hash table entries: %ld (order %ld, %ld bytes)\n",
1740 nr_hash, order, (PAGE_SIZE << order));
1742 set_shrinker(DEFAULT_SEEKS, shrink_dqcache_memory);
1746 module_init(dquot_init);
1748 EXPORT_SYMBOL(register_quota_format);
1749 EXPORT_SYMBOL(unregister_quota_format);
1750 EXPORT_SYMBOL(dqstats);
1751 EXPORT_SYMBOL(dq_list_lock);
1752 EXPORT_SYMBOL(dq_data_lock);
1753 EXPORT_SYMBOL(vfs_quota_on);
1754 EXPORT_SYMBOL(vfs_quota_on_mount);
1755 EXPORT_SYMBOL(vfs_quota_off);
1756 EXPORT_SYMBOL(vfs_quota_sync);
1757 EXPORT_SYMBOL(vfs_get_dqinfo);
1758 EXPORT_SYMBOL(vfs_set_dqinfo);
1759 EXPORT_SYMBOL(vfs_get_dqblk);
1760 EXPORT_SYMBOL(vfs_set_dqblk);
1761 EXPORT_SYMBOL(dquot_commit);
1762 EXPORT_SYMBOL(dquot_commit_info);
1763 EXPORT_SYMBOL(dquot_mark_dquot_dirty);
1764 EXPORT_SYMBOL(dquot_initialize);
1765 EXPORT_SYMBOL(dquot_drop);
1766 EXPORT_SYMBOL(dquot_alloc_space);
1767 EXPORT_SYMBOL(dquot_alloc_inode);
1768 EXPORT_SYMBOL(dquot_free_space);
1769 EXPORT_SYMBOL(dquot_free_inode);
1770 EXPORT_SYMBOL(dquot_transfer);