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>
78 #include <linux/pagemap.h>
80 #include <asm/uaccess.h>
82 #define __DQUOT_PARANOIA
85 * There are two quota SMP locks. dq_list_lock protects all lists with quotas
86 * and quota formats and also dqstats structure containing statistics about the
87 * lists. dq_data_lock protects data from dq_dqb and also mem_dqinfo structures
88 * and also guards consistency of dquot->dq_dqb with inode->i_blocks, i_bytes.
89 * i_blocks and i_bytes updates itself are guarded by i_lock acquired directly
90 * in inode_add_bytes() and inode_sub_bytes().
92 * The spinlock ordering is hence: dq_data_lock > dq_list_lock > i_lock
94 * Note that some things (eg. sb pointer, type, id) doesn't change during
95 * the life of the dquot structure and so needn't to be protected by a lock
97 * Any operation working on dquots via inode pointers must hold dqptr_sem. If
98 * operation is just reading pointers from inode (or not using them at all) the
99 * read lock is enough. If pointers are altered function must hold write lock.
100 * If operation is holding reference to dquot in other way (e.g. quotactl ops)
101 * it must be guarded by dqonoff_sem.
102 * This locking assures that:
103 * a) update/access to dquot pointers in inode is serialized
104 * b) everyone is guarded against invalidate_dquots()
106 * Each dquot has its dq_lock semaphore. Locked dquots might not be referenced
107 * from inodes (dquot_alloc_space() and such don't check the dq_lock).
108 * Currently dquot is locked only when it is being read to memory (or space for
109 * it is being allocated) on the first dqget() and when it is being released on
110 * the last dqput(). The allocation and release oparations are serialized by
111 * the dq_lock and by checking the use count in dquot_release(). Write
112 * operations on dquots don't hold dq_lock as they copy data under dq_data_lock
113 * spinlock to internal buffers before writing.
115 * Lock ordering (including journal_lock) is following:
116 * dqonoff_sem > journal_lock > dqptr_sem > dquot->dq_lock > dqio_sem
119 spinlock_t dq_list_lock = SPIN_LOCK_UNLOCKED;
120 spinlock_t dq_data_lock = SPIN_LOCK_UNLOCKED;
122 static char *quotatypes[] = INITQFNAMES;
123 static struct quota_format_type *quota_formats; /* List of registered formats */
124 static struct quota_module_name module_names[] = INIT_QUOTA_MODULE_NAMES;
126 int register_quota_format(struct quota_format_type *fmt)
128 spin_lock(&dq_list_lock);
129 fmt->qf_next = quota_formats;
131 spin_unlock(&dq_list_lock);
135 void unregister_quota_format(struct quota_format_type *fmt)
137 struct quota_format_type **actqf;
139 spin_lock(&dq_list_lock);
140 for (actqf = "a_formats; *actqf && *actqf != fmt; actqf = &(*actqf)->qf_next);
142 *actqf = (*actqf)->qf_next;
143 spin_unlock(&dq_list_lock);
146 static struct quota_format_type *find_quota_format(int id)
148 struct quota_format_type *actqf;
150 spin_lock(&dq_list_lock);
151 for (actqf = quota_formats; actqf && actqf->qf_fmt_id != id; actqf = actqf->qf_next);
152 if (!actqf || !try_module_get(actqf->qf_owner)) {
155 spin_unlock(&dq_list_lock);
157 for (qm = 0; module_names[qm].qm_fmt_id && module_names[qm].qm_fmt_id != id; qm++);
158 if (!module_names[qm].qm_fmt_id || request_module(module_names[qm].qm_mod_name))
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);
170 static void put_quota_format(struct quota_format_type *fmt)
172 module_put(fmt->qf_owner);
176 * Dquot List Management:
177 * The quota code uses three lists for dquot management: the inuse_list,
178 * free_dquots, and dquot_hash[] array. A single dquot structure may be
179 * on all three lists, depending on its current state.
181 * All dquots are placed to the end of inuse_list when first created, and this
182 * list is used for the sync and invalidate operations, which must look
185 * Unused dquots (dq_count == 0) are added to the free_dquots list when freed,
186 * and this list is searched whenever we need an available dquot. Dquots are
187 * removed from the list as soon as they are used again, and
188 * dqstats.free_dquots gives the number of dquots on the list. When
189 * dquot is invalidated it's completely released from memory.
191 * Dquots with a specific identity (device, type and id) are placed on
192 * one of the dquot_hash[] hash chains. The provides an efficient search
193 * mechanism to locate a specific dquot.
196 static LIST_HEAD(inuse_list);
197 static LIST_HEAD(free_dquots);
198 unsigned int dq_hash_bits, dq_hash_mask;
199 static struct hlist_head *dquot_hash;
201 struct dqstats dqstats;
203 static void dqput(struct dquot *dquot);
205 static inline int const hashfn(struct super_block *sb, unsigned int id, int type)
207 unsigned long tmp = (((unsigned long)sb>>L1_CACHE_SHIFT) ^ id) * (MAXQUOTAS - type);
208 return (tmp + (tmp >> dq_hash_bits)) & dq_hash_mask;
212 * Following list functions expect dq_list_lock to be held
214 static inline void insert_dquot_hash(struct dquot *dquot)
216 struct hlist_head *head = dquot_hash + hashfn(dquot->dq_sb, dquot->dq_id, dquot->dq_type);
217 hlist_add_head(&dquot->dq_hash, head);
220 static inline void remove_dquot_hash(struct dquot *dquot)
222 hlist_del_init(&dquot->dq_hash);
225 static inline struct dquot *find_dquot(unsigned int hashent, struct super_block *sb, unsigned int id, int type)
227 struct hlist_node *node;
230 hlist_for_each (node, dquot_hash+hashent) {
231 dquot = hlist_entry(node, struct dquot, dq_hash);
232 if (dquot->dq_sb == sb && dquot->dq_id == id && dquot->dq_type == type)
238 /* Add a dquot to the tail of the free list */
239 static inline void put_dquot_last(struct dquot *dquot)
241 list_add(&dquot->dq_free, free_dquots.prev);
242 dqstats.free_dquots++;
245 static inline void remove_free_dquot(struct dquot *dquot)
247 if (list_empty(&dquot->dq_free))
249 list_del_init(&dquot->dq_free);
250 dqstats.free_dquots--;
253 static inline void put_inuse(struct dquot *dquot)
255 /* We add to the back of inuse list so we don't have to restart
256 * when traversing this list and we block */
257 list_add(&dquot->dq_inuse, inuse_list.prev);
258 dqstats.allocated_dquots++;
261 static inline void remove_inuse(struct dquot *dquot)
263 dqstats.allocated_dquots--;
264 list_del(&dquot->dq_inuse);
267 * End of list functions needing dq_list_lock
270 static void wait_on_dquot(struct dquot *dquot)
272 down(&dquot->dq_lock);
276 #define mark_dquot_dirty(dquot) ((dquot)->dq_sb->dq_op->mark_dirty(dquot))
278 int dquot_mark_dquot_dirty(struct dquot *dquot)
280 spin_lock(&dq_list_lock);
281 if (!test_and_set_bit(DQ_MOD_B, &dquot->dq_flags))
282 list_add(&dquot->dq_dirty, &sb_dqopt(dquot->dq_sb)->
283 info[dquot->dq_type].dqi_dirty_list);
284 spin_unlock(&dq_list_lock);
288 /* This function needs dq_list_lock */
289 static inline int clear_dquot_dirty(struct dquot *dquot)
291 if (!test_and_clear_bit(DQ_MOD_B, &dquot->dq_flags))
293 list_del_init(&dquot->dq_dirty);
297 void mark_info_dirty(struct super_block *sb, int type)
299 set_bit(DQF_INFO_DIRTY_B, &sb_dqopt(sb)->info[type].dqi_flags);
301 EXPORT_SYMBOL(mark_info_dirty);
304 * Read dquot from disk and alloc space for it
307 int dquot_acquire(struct dquot *dquot)
309 int ret = 0, ret2 = 0;
310 struct quota_info *dqopt = sb_dqopt(dquot->dq_sb);
312 down(&dquot->dq_lock);
313 down(&dqopt->dqio_sem);
314 if (!test_bit(DQ_READ_B, &dquot->dq_flags))
315 ret = dqopt->ops[dquot->dq_type]->read_dqblk(dquot);
318 set_bit(DQ_READ_B, &dquot->dq_flags);
319 /* Instantiate dquot if needed */
320 if (!test_bit(DQ_ACTIVE_B, &dquot->dq_flags) && !dquot->dq_off) {
321 ret = dqopt->ops[dquot->dq_type]->commit_dqblk(dquot);
322 /* Write the info if needed */
323 if (info_dirty(&dqopt->info[dquot->dq_type]))
324 ret2 = dqopt->ops[dquot->dq_type]->write_file_info(dquot->dq_sb, dquot->dq_type);
332 set_bit(DQ_ACTIVE_B, &dquot->dq_flags);
334 up(&dqopt->dqio_sem);
340 * Write dquot to disk
342 int dquot_commit(struct dquot *dquot)
344 int ret = 0, ret2 = 0;
345 struct quota_info *dqopt = sb_dqopt(dquot->dq_sb);
347 down(&dqopt->dqio_sem);
348 spin_lock(&dq_list_lock);
349 if (!clear_dquot_dirty(dquot)) {
350 spin_unlock(&dq_list_lock);
353 spin_unlock(&dq_list_lock);
354 /* Inactive dquot can be only if there was error during read/init
355 * => we have better not writing it */
356 if (test_bit(DQ_ACTIVE_B, &dquot->dq_flags)) {
357 ret = dqopt->ops[dquot->dq_type]->commit_dqblk(dquot);
358 if (info_dirty(&dqopt->info[dquot->dq_type]))
359 ret2 = dqopt->ops[dquot->dq_type]->write_file_info(dquot->dq_sb, dquot->dq_type);
364 up(&dqopt->dqio_sem);
371 int dquot_release(struct dquot *dquot)
373 int ret = 0, ret2 = 0;
374 struct quota_info *dqopt = sb_dqopt(dquot->dq_sb);
376 down(&dquot->dq_lock);
377 /* Check whether we are not racing with some other dqget() */
378 if (atomic_read(&dquot->dq_count) > 1)
380 down(&dqopt->dqio_sem);
381 if (dqopt->ops[dquot->dq_type]->release_dqblk) {
382 ret = dqopt->ops[dquot->dq_type]->release_dqblk(dquot);
384 if (info_dirty(&dqopt->info[dquot->dq_type]))
385 ret2 = dqopt->ops[dquot->dq_type]->write_file_info(dquot->dq_sb, dquot->dq_type);
389 clear_bit(DQ_ACTIVE_B, &dquot->dq_flags);
390 up(&dqopt->dqio_sem);
396 /* Invalidate all dquots on the list. Note that this function is called after
397 * quota is disabled and pointers from inodes removed so there cannot be new
398 * quota users. Also because we hold dqonoff_sem there can be no quota users
399 * for this sb+type at all. */
400 static void invalidate_dquots(struct super_block *sb, int type)
403 struct list_head *head;
405 spin_lock(&dq_list_lock);
406 for (head = inuse_list.next; head != &inuse_list;) {
407 dquot = list_entry(head, struct dquot, dq_inuse);
409 if (dquot->dq_sb != sb)
411 if (dquot->dq_type != type)
413 #ifdef __DQUOT_PARANOIA
414 if (atomic_read(&dquot->dq_count))
417 /* Quota now has no users and it has been written on last dqput() */
418 remove_dquot_hash(dquot);
419 remove_free_dquot(dquot);
421 kmem_cache_free(dquot_cachep, dquot);
423 spin_unlock(&dq_list_lock);
426 int vfs_quota_sync(struct super_block *sb, int type)
428 struct list_head *dirty;
430 struct quota_info *dqopt = sb_dqopt(sb);
433 down(&dqopt->dqonoff_sem);
434 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
435 if (type != -1 && cnt != type)
437 if (!sb_has_quota_enabled(sb, cnt))
439 spin_lock(&dq_list_lock);
440 dirty = &dqopt->info[cnt].dqi_dirty_list;
441 while (!list_empty(dirty)) {
442 dquot = list_entry(dirty->next, struct dquot, dq_dirty);
443 /* Dirty and inactive can be only bad dquot... */
444 if (!test_bit(DQ_ACTIVE_B, &dquot->dq_flags)) {
445 clear_dquot_dirty(dquot);
448 /* Now we have active dquot from which someone is
449 * holding reference so we can safely just increase
451 atomic_inc(&dquot->dq_count);
453 spin_unlock(&dq_list_lock);
454 sb->dq_op->write_dquot(dquot);
456 spin_lock(&dq_list_lock);
458 spin_unlock(&dq_list_lock);
461 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
462 if ((cnt == type || type == -1) && sb_has_quota_enabled(sb, cnt)
463 && info_dirty(&dqopt->info[cnt]))
464 sb->dq_op->write_info(sb, cnt);
465 spin_lock(&dq_list_lock);
467 spin_unlock(&dq_list_lock);
468 up(&dqopt->dqonoff_sem);
473 /* Free unused dquots from cache */
474 static void prune_dqcache(int count)
476 struct list_head *head;
479 head = free_dquots.prev;
480 while (head != &free_dquots && count) {
481 dquot = list_entry(head, struct dquot, dq_free);
482 remove_dquot_hash(dquot);
483 remove_free_dquot(dquot);
485 kmem_cache_free(dquot_cachep, dquot);
487 head = free_dquots.prev;
492 * This is called from kswapd when we think we need some
496 static int shrink_dqcache_memory(int nr, unsigned int gfp_mask)
500 spin_lock(&dq_list_lock);
503 ret = dqstats.allocated_dquots;
504 spin_unlock(&dq_list_lock);
509 * Put reference to dquot
510 * NOTE: If you change this function please check whether dqput_blocks() works right...
511 * MUST be called with either dqptr_sem or dqonoff_sem held
513 static void dqput(struct dquot *dquot)
517 #ifdef __DQUOT_PARANOIA
518 if (!atomic_read(&dquot->dq_count)) {
519 printk("VFS: dqput: trying to free free dquot\n");
520 printk("VFS: device %s, dquot of %s %d\n",
522 quotatypes[dquot->dq_type],
528 spin_lock(&dq_list_lock);
530 spin_unlock(&dq_list_lock);
532 spin_lock(&dq_list_lock);
533 if (atomic_read(&dquot->dq_count) > 1) {
534 /* We have more than one user... nothing to do */
535 atomic_dec(&dquot->dq_count);
536 spin_unlock(&dq_list_lock);
539 /* Need to release dquot? */
540 if (test_bit(DQ_ACTIVE_B, &dquot->dq_flags) && dquot_dirty(dquot)) {
541 spin_unlock(&dq_list_lock);
542 /* Commit dquot before releasing */
543 dquot->dq_sb->dq_op->write_dquot(dquot);
546 /* Clear flag in case dquot was inactive (something bad happened) */
547 clear_dquot_dirty(dquot);
548 if (test_bit(DQ_ACTIVE_B, &dquot->dq_flags)) {
549 spin_unlock(&dq_list_lock);
550 dquot->dq_sb->dq_op->release_dquot(dquot);
553 atomic_dec(&dquot->dq_count);
554 #ifdef __DQUOT_PARANOIA
556 if (!list_empty(&dquot->dq_free))
559 put_dquot_last(dquot);
560 spin_unlock(&dq_list_lock);
563 static struct dquot *get_empty_dquot(struct super_block *sb, int type)
567 dquot = kmem_cache_alloc(dquot_cachep, SLAB_NOFS);
571 memset((caddr_t)dquot, 0, sizeof(struct dquot));
572 sema_init(&dquot->dq_lock, 1);
573 INIT_LIST_HEAD(&dquot->dq_free);
574 INIT_LIST_HEAD(&dquot->dq_inuse);
575 INIT_HLIST_NODE(&dquot->dq_hash);
576 INIT_LIST_HEAD(&dquot->dq_dirty);
578 dquot->dq_type = type;
579 atomic_set(&dquot->dq_count, 1);
585 * Get reference to dquot
586 * MUST be called with either dqptr_sem or dqonoff_sem held
588 static struct dquot *dqget(struct super_block *sb, unsigned int id, int type)
590 unsigned int hashent = hashfn(sb, id, type);
591 struct dquot *dquot, *empty = NODQUOT;
593 if (!sb_has_quota_enabled(sb, type))
596 spin_lock(&dq_list_lock);
597 if ((dquot = find_dquot(hashent, sb, id, type)) == NODQUOT) {
598 if (empty == NODQUOT) {
599 spin_unlock(&dq_list_lock);
600 if ((empty = get_empty_dquot(sb, type)) == NODQUOT)
601 schedule(); /* Try to wait for a moment... */
606 /* all dquots go on the inuse_list */
608 /* hash it first so it can be found */
609 insert_dquot_hash(dquot);
611 spin_unlock(&dq_list_lock);
613 if (!atomic_read(&dquot->dq_count))
614 remove_free_dquot(dquot);
615 atomic_inc(&dquot->dq_count);
616 dqstats.cache_hits++;
618 spin_unlock(&dq_list_lock);
620 kmem_cache_free(dquot_cachep, empty);
622 /* Wait for dq_lock - after this we know that either dquot_release() is already
623 * finished or it will be canceled due to dq_count > 1 test */
624 wait_on_dquot(dquot);
625 /* Read the dquot and instantiate it (everything done only if needed) */
626 if (!test_bit(DQ_ACTIVE_B, &dquot->dq_flags) && sb->dq_op->acquire_dquot(dquot) < 0) {
630 #ifdef __DQUOT_PARANOIA
631 if (!dquot->dq_sb) /* Has somebody invalidated entry under us? */
638 static int dqinit_needed(struct inode *inode, int type)
642 if (IS_NOQUOTA(inode))
645 return inode->i_dquot[type] == NODQUOT;
646 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
647 if (inode->i_dquot[cnt] == NODQUOT)
652 /* This routine is guarded by dqonoff_sem semaphore */
653 static void add_dquot_ref(struct super_block *sb, int type)
659 list_for_each(p, &sb->s_files) {
660 struct file *filp = list_entry(p, struct file, f_list);
661 struct inode *inode = filp->f_dentry->d_inode;
662 if (filp->f_mode & FMODE_WRITE && dqinit_needed(inode, type)) {
663 struct dentry *dentry = dget(filp->f_dentry);
665 sb->dq_op->initialize(inode, type);
667 /* As we may have blocked we had better restart... */
674 /* Return 0 if dqput() won't block (note that 1 doesn't necessarily mean blocking) */
675 static inline int dqput_blocks(struct dquot *dquot)
677 if (atomic_read(&dquot->dq_count) <= 1)
682 /* Remove references to dquots from inode - add dquot to list for freeing if needed */
683 /* We can't race with anybody because we hold dqptr_sem for writing... */
684 int remove_inode_dquot_ref(struct inode *inode, int type, struct list_head *tofree_head)
686 struct dquot *dquot = inode->i_dquot[type];
689 inode->i_dquot[type] = NODQUOT;
690 /* any other quota in use? */
691 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
692 if (inode->i_dquot[cnt] != NODQUOT)
695 inode->i_flags &= ~S_QUOTA;
697 if (dquot != NODQUOT) {
698 if (dqput_blocks(dquot)) {
699 #ifdef __DQUOT_PARANOIA
700 if (atomic_read(&dquot->dq_count) != 1)
701 printk(KERN_WARNING "VFS: Adding dquot with dq_count %d to dispose list.\n", atomic_read(&dquot->dq_count));
703 spin_lock(&dq_list_lock);
704 list_add(&dquot->dq_free, tofree_head); /* As dquot must have currently users it can't be on the free list... */
705 spin_unlock(&dq_list_lock);
709 dqput(dquot); /* We have guaranteed we won't block */
714 /* Free list of dquots - called from inode.c */
715 /* dquots are removed from inodes, no new references can be got so we are the only ones holding reference */
716 static void put_dquot_list(struct list_head *tofree_head)
718 struct list_head *act_head;
721 act_head = tofree_head->next;
722 /* So now we have dquots on the list... Just free them */
723 while (act_head != tofree_head) {
724 dquot = list_entry(act_head, struct dquot, dq_free);
725 act_head = act_head->next;
726 list_del_init(&dquot->dq_free); /* Remove dquot from the list so we won't have problems... */
731 /* Function in inode.c - remove pointers to dquots in icache */
732 extern void remove_dquot_ref(struct super_block *, int, struct list_head *);
734 /* Gather all references from inodes and drop them */
735 static void drop_dquot_ref(struct super_block *sb, int type)
737 LIST_HEAD(tofree_head);
739 down_write(&sb_dqopt(sb)->dqptr_sem);
740 remove_dquot_ref(sb, type, &tofree_head);
741 up_write(&sb_dqopt(sb)->dqptr_sem);
742 put_dquot_list(&tofree_head);
745 static inline void dquot_incr_inodes(struct dquot *dquot, unsigned long number)
747 dquot->dq_dqb.dqb_curinodes += number;
750 static inline void dquot_incr_space(struct dquot *dquot, qsize_t number)
752 dquot->dq_dqb.dqb_curspace += number;
755 static inline void dquot_decr_inodes(struct dquot *dquot, unsigned long number)
757 if (dquot->dq_dqb.dqb_curinodes > number)
758 dquot->dq_dqb.dqb_curinodes -= number;
760 dquot->dq_dqb.dqb_curinodes = 0;
761 if (dquot->dq_dqb.dqb_curinodes < dquot->dq_dqb.dqb_isoftlimit)
762 dquot->dq_dqb.dqb_itime = (time_t) 0;
763 clear_bit(DQ_INODES_B, &dquot->dq_flags);
766 static inline void dquot_decr_space(struct dquot *dquot, qsize_t number)
768 if (dquot->dq_dqb.dqb_curspace > number)
769 dquot->dq_dqb.dqb_curspace -= number;
771 dquot->dq_dqb.dqb_curspace = 0;
772 if (toqb(dquot->dq_dqb.dqb_curspace) < dquot->dq_dqb.dqb_bsoftlimit)
773 dquot->dq_dqb.dqb_btime = (time_t) 0;
774 clear_bit(DQ_BLKS_B, &dquot->dq_flags);
777 static inline int need_print_warning(struct dquot *dquot)
779 switch (dquot->dq_type) {
781 return current->fsuid == dquot->dq_id;
783 return in_group_p(dquot->dq_id);
788 /* Values of warnings */
791 #define ISOFTLONGWARN 2
794 #define BSOFTLONGWARN 5
797 /* Print warning to user which exceeded quota */
798 static void print_warning(struct dquot *dquot, const char warntype)
801 int flag = (warntype == BHARDWARN || warntype == BSOFTLONGWARN) ? DQ_BLKS_B :
802 ((warntype == IHARDWARN || warntype == ISOFTLONGWARN) ? DQ_INODES_B : 0);
804 if (!need_print_warning(dquot) || (flag && test_and_set_bit(flag, &dquot->dq_flags)))
806 tty_write_message(current->signal->tty, dquot->dq_sb->s_id);
807 if (warntype == ISOFTWARN || warntype == BSOFTWARN)
808 tty_write_message(current->signal->tty, ": warning, ");
810 tty_write_message(current->signal->tty, ": write failed, ");
811 tty_write_message(current->signal->tty, quotatypes[dquot->dq_type]);
814 msg = " file limit reached.\n";
817 msg = " file quota exceeded too long.\n";
820 msg = " file quota exceeded.\n";
823 msg = " block limit reached.\n";
826 msg = " block quota exceeded too long.\n";
829 msg = " block quota exceeded.\n";
832 tty_write_message(current->signal->tty, msg);
835 static inline void flush_warnings(struct dquot **dquots, char *warntype)
839 for (i = 0; i < MAXQUOTAS; i++)
840 if (dquots[i] != NODQUOT && warntype[i] != NOWARN)
841 print_warning(dquots[i], warntype[i]);
844 static inline char ignore_hardlimit(struct dquot *dquot)
846 struct mem_dqinfo *info = &sb_dqopt(dquot->dq_sb)->info[dquot->dq_type];
848 return capable(CAP_SYS_RESOURCE) &&
849 (info->dqi_format->qf_fmt_id != QFMT_VFS_OLD || !(info->dqi_flags & V1_DQF_RSQUASH));
852 /* needs dq_data_lock */
853 static int check_idq(struct dquot *dquot, ulong inodes, char *warntype)
856 if (inodes <= 0 || test_bit(DQ_FAKE_B, &dquot->dq_flags))
859 if (dquot->dq_dqb.dqb_ihardlimit &&
860 (dquot->dq_dqb.dqb_curinodes + inodes) > dquot->dq_dqb.dqb_ihardlimit &&
861 !ignore_hardlimit(dquot)) {
862 *warntype = IHARDWARN;
866 if (dquot->dq_dqb.dqb_isoftlimit &&
867 (dquot->dq_dqb.dqb_curinodes + inodes) > dquot->dq_dqb.dqb_isoftlimit &&
868 dquot->dq_dqb.dqb_itime && get_seconds() >= dquot->dq_dqb.dqb_itime &&
869 !ignore_hardlimit(dquot)) {
870 *warntype = ISOFTLONGWARN;
874 if (dquot->dq_dqb.dqb_isoftlimit &&
875 (dquot->dq_dqb.dqb_curinodes + inodes) > dquot->dq_dqb.dqb_isoftlimit &&
876 dquot->dq_dqb.dqb_itime == 0) {
877 *warntype = ISOFTWARN;
878 dquot->dq_dqb.dqb_itime = get_seconds() + sb_dqopt(dquot->dq_sb)->info[dquot->dq_type].dqi_igrace;
884 /* needs dq_data_lock */
885 static int check_bdq(struct dquot *dquot, qsize_t space, int prealloc, char *warntype)
888 if (space <= 0 || test_bit(DQ_FAKE_B, &dquot->dq_flags))
891 if (dquot->dq_dqb.dqb_bhardlimit &&
892 toqb(dquot->dq_dqb.dqb_curspace + space) > dquot->dq_dqb.dqb_bhardlimit &&
893 !ignore_hardlimit(dquot)) {
895 *warntype = BHARDWARN;
899 if (dquot->dq_dqb.dqb_bsoftlimit &&
900 toqb(dquot->dq_dqb.dqb_curspace + space) > dquot->dq_dqb.dqb_bsoftlimit &&
901 dquot->dq_dqb.dqb_btime && get_seconds() >= dquot->dq_dqb.dqb_btime &&
902 !ignore_hardlimit(dquot)) {
904 *warntype = BSOFTLONGWARN;
908 if (dquot->dq_dqb.dqb_bsoftlimit &&
909 toqb(dquot->dq_dqb.dqb_curspace + space) > dquot->dq_dqb.dqb_bsoftlimit &&
910 dquot->dq_dqb.dqb_btime == 0) {
912 *warntype = BSOFTWARN;
913 dquot->dq_dqb.dqb_btime = get_seconds() + sb_dqopt(dquot->dq_sb)->info[dquot->dq_type].dqi_bgrace;
917 * We don't allow preallocation to exceed softlimit so exceeding will
927 * Initialize quota pointers in inode
928 * Transaction must be started at entry
930 int dquot_initialize(struct inode *inode, int type)
935 /* First test before acquiring semaphore - solves deadlocks when we
936 * re-enter the quota code and are already holding the semaphore */
937 if (IS_NOQUOTA(inode))
939 down_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
940 /* Having dqptr_sem we know NOQUOTA flags can't be altered... */
941 if (IS_NOQUOTA(inode))
943 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
944 if (type != -1 && cnt != type)
946 if (inode->i_dquot[cnt] == NODQUOT) {
955 inode->i_dquot[cnt] = dqget(inode->i_sb, id, cnt);
956 if (inode->i_dquot[cnt])
957 inode->i_flags |= S_QUOTA;
961 up_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
966 * Release all quotas referenced by inode
967 * Transaction must be started at an entry
969 int dquot_drop(struct inode *inode)
973 down_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
974 inode->i_flags &= ~S_QUOTA;
975 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
976 if (inode->i_dquot[cnt] != NODQUOT) {
977 dqput(inode->i_dquot[cnt]);
978 inode->i_dquot[cnt] = NODQUOT;
981 up_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
986 * Following four functions update i_blocks+i_bytes fields and
987 * quota information (together with appropriate checks)
988 * NOTE: We absolutely rely on the fact that caller dirties
989 * the inode (usually macros in quotaops.h care about this) and
990 * holds a handle for the current transaction so that dquot write and
991 * inode write go into the same transaction.
995 * This operation can block, but only after everything is updated
997 int dquot_alloc_space(struct inode *inode, qsize_t number, int warn)
999 int cnt, ret = NO_QUOTA;
1000 char warntype[MAXQUOTAS];
1002 /* First test before acquiring semaphore - solves deadlocks when we
1003 * re-enter the quota code and are already holding the semaphore */
1004 if (IS_NOQUOTA(inode)) {
1006 inode_add_bytes(inode, number);
1009 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1010 warntype[cnt] = NOWARN;
1012 down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1013 if (IS_NOQUOTA(inode)) { /* Now we can do reliable test... */
1014 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1017 spin_lock(&dq_data_lock);
1018 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1019 if (inode->i_dquot[cnt] == NODQUOT)
1021 if (check_bdq(inode->i_dquot[cnt], number, warn, warntype+cnt) == NO_QUOTA)
1024 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1025 if (inode->i_dquot[cnt] == NODQUOT)
1027 dquot_incr_space(inode->i_dquot[cnt], number);
1029 inode_add_bytes(inode, number);
1032 spin_unlock(&dq_data_lock);
1033 if (ret == QUOTA_OK)
1034 /* Dirtify all the dquots - this can block when journalling */
1035 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1036 if (inode->i_dquot[cnt])
1037 mark_dquot_dirty(inode->i_dquot[cnt]);
1038 flush_warnings(inode->i_dquot, warntype);
1039 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1044 * This operation can block, but only after everything is updated
1046 int dquot_alloc_inode(const struct inode *inode, unsigned long number)
1048 int cnt, ret = NO_QUOTA;
1049 char warntype[MAXQUOTAS];
1051 /* First test before acquiring semaphore - solves deadlocks when we
1052 * re-enter the quota code and are already holding the semaphore */
1053 if (IS_NOQUOTA(inode))
1055 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1056 warntype[cnt] = NOWARN;
1057 down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1058 if (IS_NOQUOTA(inode)) {
1059 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1062 spin_lock(&dq_data_lock);
1063 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1064 if (inode->i_dquot[cnt] == NODQUOT)
1066 if (check_idq(inode->i_dquot[cnt], number, warntype+cnt) == NO_QUOTA)
1070 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1071 if (inode->i_dquot[cnt] == NODQUOT)
1073 dquot_incr_inodes(inode->i_dquot[cnt], number);
1077 spin_unlock(&dq_data_lock);
1078 if (ret == QUOTA_OK)
1079 /* Dirtify all the dquots - this can block when journalling */
1080 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1081 if (inode->i_dquot[cnt])
1082 mark_dquot_dirty(inode->i_dquot[cnt]);
1083 flush_warnings((struct dquot **)inode->i_dquot, warntype);
1084 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1089 * This is a non-blocking operation.
1091 int dquot_free_space(struct inode *inode, qsize_t number)
1095 /* First test before acquiring semaphore - solves deadlocks when we
1096 * re-enter the quota code and are already holding the semaphore */
1097 if (IS_NOQUOTA(inode)) {
1099 inode_sub_bytes(inode, number);
1102 down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1103 /* Now recheck reliably when holding dqptr_sem */
1104 if (IS_NOQUOTA(inode)) {
1105 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1108 spin_lock(&dq_data_lock);
1109 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1110 if (inode->i_dquot[cnt] == NODQUOT)
1112 dquot_decr_space(inode->i_dquot[cnt], number);
1114 inode_sub_bytes(inode, number);
1115 spin_unlock(&dq_data_lock);
1116 /* Dirtify all the dquots - this can block when journalling */
1117 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1118 if (inode->i_dquot[cnt])
1119 mark_dquot_dirty(inode->i_dquot[cnt]);
1120 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1125 * This is a non-blocking operation.
1127 int dquot_free_inode(const struct inode *inode, unsigned long number)
1131 /* First test before acquiring semaphore - solves deadlocks when we
1132 * re-enter the quota code and are already holding the semaphore */
1133 if (IS_NOQUOTA(inode))
1135 down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1136 /* Now recheck reliably when holding dqptr_sem */
1137 if (IS_NOQUOTA(inode)) {
1138 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1141 spin_lock(&dq_data_lock);
1142 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1143 if (inode->i_dquot[cnt] == NODQUOT)
1145 dquot_decr_inodes(inode->i_dquot[cnt], number);
1147 spin_unlock(&dq_data_lock);
1148 /* Dirtify all the dquots - this can block when journalling */
1149 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1150 if (inode->i_dquot[cnt])
1151 mark_dquot_dirty(inode->i_dquot[cnt]);
1152 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1157 * Transfer the number of inode and blocks from one diskquota to an other.
1159 * This operation can block, but only after everything is updated
1161 int dquot_transfer(struct inode *inode, struct iattr *iattr)
1164 struct dquot *transfer_from[MAXQUOTAS];
1165 struct dquot *transfer_to[MAXQUOTAS];
1166 int cnt, ret = NO_QUOTA, chuid = (iattr->ia_valid & ATTR_UID) && inode->i_uid != iattr->ia_uid,
1167 chgid = (iattr->ia_valid & ATTR_GID) && inode->i_gid != iattr->ia_gid;
1168 char warntype[MAXQUOTAS];
1170 /* First test before acquiring semaphore - solves deadlocks when we
1171 * re-enter the quota code and are already holding the semaphore */
1172 if (IS_NOQUOTA(inode))
1174 /* Clear the arrays */
1175 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1176 transfer_to[cnt] = transfer_from[cnt] = NODQUOT;
1177 warntype[cnt] = NOWARN;
1179 down_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
1180 /* Now recheck reliably when holding dqptr_sem */
1181 if (IS_NOQUOTA(inode)) { /* File without quota accounting? */
1182 up_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
1185 /* First build the transfer_to list - here we can block on
1186 * reading/instantiating of dquots. We know that the transaction for
1187 * us was already started so we don't violate lock ranking here */
1188 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1193 transfer_to[cnt] = dqget(inode->i_sb, iattr->ia_uid, cnt);
1198 transfer_to[cnt] = dqget(inode->i_sb, iattr->ia_gid, cnt);
1202 spin_lock(&dq_data_lock);
1203 space = inode_get_bytes(inode);
1204 /* Build the transfer_from list and check the limits */
1205 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1206 if (transfer_to[cnt] == NODQUOT)
1208 transfer_from[cnt] = inode->i_dquot[cnt];
1209 if (check_idq(transfer_to[cnt], 1, warntype+cnt) == NO_QUOTA ||
1210 check_bdq(transfer_to[cnt], space, 0, warntype+cnt) == NO_QUOTA)
1215 * Finally perform the needed transfer from transfer_from to transfer_to
1217 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1219 * Skip changes for same uid or gid or for turned off quota-type.
1221 if (transfer_to[cnt] == NODQUOT)
1224 /* Due to IO error we might not have transfer_from[] structure */
1225 if (transfer_from[cnt]) {
1226 dquot_decr_inodes(transfer_from[cnt], 1);
1227 dquot_decr_space(transfer_from[cnt], space);
1230 dquot_incr_inodes(transfer_to[cnt], 1);
1231 dquot_incr_space(transfer_to[cnt], space);
1233 inode->i_dquot[cnt] = transfer_to[cnt];
1237 spin_unlock(&dq_data_lock);
1238 /* Dirtify all the dquots - this can block when journalling */
1239 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1240 if (transfer_from[cnt])
1241 mark_dquot_dirty(transfer_from[cnt]);
1242 if (transfer_to[cnt])
1243 mark_dquot_dirty(transfer_to[cnt]);
1245 flush_warnings(transfer_to, warntype);
1247 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1248 if (ret == QUOTA_OK && transfer_from[cnt] != NODQUOT)
1249 dqput(transfer_from[cnt]);
1250 if (ret == NO_QUOTA && transfer_to[cnt] != NODQUOT)
1251 dqput(transfer_to[cnt]);
1253 up_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
1258 * Write info of quota file to disk
1260 int dquot_commit_info(struct super_block *sb, int type)
1263 struct quota_info *dqopt = sb_dqopt(sb);
1265 down(&dqopt->dqio_sem);
1266 ret = dqopt->ops[type]->write_file_info(sb, type);
1267 up(&dqopt->dqio_sem);
1272 * Definitions of diskquota operations.
1274 struct dquot_operations dquot_operations = {
1275 .initialize = dquot_initialize,
1277 .alloc_space = dquot_alloc_space,
1278 .alloc_inode = dquot_alloc_inode,
1279 .free_space = dquot_free_space,
1280 .free_inode = dquot_free_inode,
1281 .transfer = dquot_transfer,
1282 .write_dquot = dquot_commit,
1283 .acquire_dquot = dquot_acquire,
1284 .release_dquot = dquot_release,
1285 .mark_dirty = dquot_mark_dquot_dirty,
1286 .write_info = dquot_commit_info
1289 static inline void set_enable_flags(struct quota_info *dqopt, int type)
1293 dqopt->flags |= DQUOT_USR_ENABLED;
1296 dqopt->flags |= DQUOT_GRP_ENABLED;
1301 static inline void reset_enable_flags(struct quota_info *dqopt, int type)
1305 dqopt->flags &= ~DQUOT_USR_ENABLED;
1308 dqopt->flags &= ~DQUOT_GRP_ENABLED;
1314 * Turn quota off on a device. type == -1 ==> quotaoff for all types (umount)
1316 int vfs_quota_off(struct super_block *sb, int type)
1319 struct quota_info *dqopt = sb_dqopt(sb);
1321 /* We need to serialize quota_off() for device */
1322 down(&dqopt->dqonoff_sem);
1323 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1324 if (type != -1 && cnt != type)
1326 if (!sb_has_quota_enabled(sb, cnt))
1328 reset_enable_flags(dqopt, cnt);
1330 /* Note: these are blocking operations */
1331 drop_dquot_ref(sb, cnt);
1332 invalidate_dquots(sb, cnt);
1334 * Now all dquots should be invalidated, all writes done so we should be only
1335 * users of the info. No locks needed.
1337 if (info_dirty(&dqopt->info[cnt]))
1338 sb->dq_op->write_info(sb, cnt);
1339 if (dqopt->ops[cnt]->free_file_info)
1340 dqopt->ops[cnt]->free_file_info(sb, cnt);
1341 put_quota_format(dqopt->info[cnt].dqi_format);
1343 fput(dqopt->files[cnt]);
1344 dqopt->files[cnt] = NULL;
1345 dqopt->info[cnt].dqi_flags = 0;
1346 dqopt->info[cnt].dqi_igrace = 0;
1347 dqopt->info[cnt].dqi_bgrace = 0;
1348 dqopt->ops[cnt] = NULL;
1350 up(&dqopt->dqonoff_sem);
1355 * Turn quotas on on a device
1358 /* Helper function when we already have file open */
1359 static int vfs_quota_on_file(struct file *f, int type, int format_id)
1361 struct quota_format_type *fmt = find_quota_format(format_id);
1362 struct inode *inode;
1363 struct super_block *sb = f->f_dentry->d_sb;
1364 struct quota_info *dqopt = sb_dqopt(sb);
1365 struct dquot *to_drop[MAXQUOTAS];
1367 unsigned int oldflags;
1372 if (!f->f_op || !f->f_op->read || !f->f_op->write)
1374 inode = f->f_dentry->d_inode;
1376 if (!S_ISREG(inode->i_mode))
1379 down(&dqopt->dqonoff_sem);
1380 if (sb_has_quota_enabled(sb, type)) {
1384 oldflags = inode->i_flags;
1385 dqopt->files[type] = f;
1387 if (!fmt->qf_ops->check_quota_file(sb, type))
1389 /* We don't want quota and atime on quota files (deadlocks possible)
1390 * We also need to set GFP mask differently because we cannot recurse
1391 * into filesystem when allocating page for quota inode */
1392 down_write(&dqopt->dqptr_sem);
1393 inode->i_flags |= S_NOQUOTA | S_NOATIME;
1396 * We write to quota files deep within filesystem code. We don't want
1397 * the VFS to reenter filesystem code when it tries to allocate a
1398 * pagecache page for the quota file write. So clear __GFP_FS in
1399 * the quota file's allocation flags.
1401 mapping_set_gfp_mask(inode->i_mapping,
1402 mapping_gfp_mask(inode->i_mapping) & ~__GFP_FS);
1404 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1405 to_drop[cnt] = inode->i_dquot[cnt];
1406 inode->i_dquot[cnt] = NODQUOT;
1408 inode->i_flags &= ~S_QUOTA;
1409 up_write(&dqopt->dqptr_sem);
1410 /* We must put dquots outside of dqptr_sem because we may need to
1411 * start transaction for dquot_release() */
1412 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1414 dqput(to_drop[cnt]);
1417 dqopt->ops[type] = fmt->qf_ops;
1418 dqopt->info[type].dqi_format = fmt;
1419 INIT_LIST_HEAD(&dqopt->info[type].dqi_dirty_list);
1420 down(&dqopt->dqio_sem);
1421 if ((error = dqopt->ops[type]->read_file_info(sb, type)) < 0) {
1422 up(&dqopt->dqio_sem);
1425 up(&dqopt->dqio_sem);
1426 set_enable_flags(dqopt, type);
1428 add_dquot_ref(sb, type);
1429 up(&dqopt->dqonoff_sem);
1434 inode->i_flags = oldflags;
1435 dqopt->files[type] = NULL;
1437 up_write(&dqopt->dqptr_sem);
1438 up(&dqopt->dqonoff_sem);
1440 put_quota_format(fmt);
1445 /* Actual function called from quotactl() */
1446 int vfs_quota_on(struct super_block *sb, int type, int format_id, char *path)
1451 f = filp_open(path, O_RDWR, 0600);
1454 error = security_quota_on(f);
1457 error = vfs_quota_on_file(f, type, format_id);
1461 filp_close(f, NULL);
1466 * Function used by filesystems when filp_open() would fail (filesystem is
1467 * being mounted now). We will use a private file structure. Caller is
1468 * responsible that it's IO functions won't need vfsmnt structure or
1469 * some dentry tricks...
1471 int vfs_quota_on_mount(int type, int format_id, struct dentry *dentry)
1476 dget(dentry); /* Get a reference for struct file */
1477 f = dentry_open(dentry, NULL, O_RDWR);
1482 error = vfs_quota_on_file(f, type, format_id);
1491 /* Generic routine for getting common part of quota structure */
1492 static void do_get_dqblk(struct dquot *dquot, struct if_dqblk *di)
1494 struct mem_dqblk *dm = &dquot->dq_dqb;
1496 spin_lock(&dq_data_lock);
1497 di->dqb_bhardlimit = dm->dqb_bhardlimit;
1498 di->dqb_bsoftlimit = dm->dqb_bsoftlimit;
1499 di->dqb_curspace = dm->dqb_curspace;
1500 di->dqb_ihardlimit = dm->dqb_ihardlimit;
1501 di->dqb_isoftlimit = dm->dqb_isoftlimit;
1502 di->dqb_curinodes = dm->dqb_curinodes;
1503 di->dqb_btime = dm->dqb_btime;
1504 di->dqb_itime = dm->dqb_itime;
1505 di->dqb_valid = QIF_ALL;
1506 spin_unlock(&dq_data_lock);
1509 int vfs_get_dqblk(struct super_block *sb, int type, qid_t id, struct if_dqblk *di)
1511 struct dquot *dquot;
1513 down(&sb_dqopt(sb)->dqonoff_sem);
1514 if (!(dquot = dqget(sb, id, type))) {
1515 up(&sb_dqopt(sb)->dqonoff_sem);
1518 do_get_dqblk(dquot, di);
1520 up(&sb_dqopt(sb)->dqonoff_sem);
1524 /* Generic routine for setting common part of quota structure */
1525 static void do_set_dqblk(struct dquot *dquot, struct if_dqblk *di)
1527 struct mem_dqblk *dm = &dquot->dq_dqb;
1528 int check_blim = 0, check_ilim = 0;
1530 spin_lock(&dq_data_lock);
1531 if (di->dqb_valid & QIF_SPACE) {
1532 dm->dqb_curspace = di->dqb_curspace;
1535 if (di->dqb_valid & QIF_BLIMITS) {
1536 dm->dqb_bsoftlimit = di->dqb_bsoftlimit;
1537 dm->dqb_bhardlimit = di->dqb_bhardlimit;
1540 if (di->dqb_valid & QIF_INODES) {
1541 dm->dqb_curinodes = di->dqb_curinodes;
1544 if (di->dqb_valid & QIF_ILIMITS) {
1545 dm->dqb_isoftlimit = di->dqb_isoftlimit;
1546 dm->dqb_ihardlimit = di->dqb_ihardlimit;
1549 if (di->dqb_valid & QIF_BTIME)
1550 dm->dqb_btime = di->dqb_btime;
1551 if (di->dqb_valid & QIF_ITIME)
1552 dm->dqb_itime = di->dqb_itime;
1555 if (!dm->dqb_bsoftlimit || toqb(dm->dqb_curspace) < dm->dqb_bsoftlimit) {
1557 clear_bit(DQ_BLKS_B, &dquot->dq_flags);
1559 else if (!(di->dqb_valid & QIF_BTIME)) /* Set grace only if user hasn't provided his own... */
1560 dm->dqb_btime = get_seconds() + sb_dqopt(dquot->dq_sb)->info[dquot->dq_type].dqi_bgrace;
1563 if (!dm->dqb_isoftlimit || dm->dqb_curinodes < dm->dqb_isoftlimit) {
1565 clear_bit(DQ_INODES_B, &dquot->dq_flags);
1567 else if (!(di->dqb_valid & QIF_ITIME)) /* Set grace only if user hasn't provided his own... */
1568 dm->dqb_itime = get_seconds() + sb_dqopt(dquot->dq_sb)->info[dquot->dq_type].dqi_igrace;
1570 if (dm->dqb_bhardlimit || dm->dqb_bsoftlimit || dm->dqb_ihardlimit || dm->dqb_isoftlimit)
1571 clear_bit(DQ_FAKE_B, &dquot->dq_flags);
1573 set_bit(DQ_FAKE_B, &dquot->dq_flags);
1574 spin_unlock(&dq_data_lock);
1575 mark_dquot_dirty(dquot);
1578 int vfs_set_dqblk(struct super_block *sb, int type, qid_t id, struct if_dqblk *di)
1580 struct dquot *dquot;
1582 down(&sb_dqopt(sb)->dqonoff_sem);
1583 if (!(dquot = dqget(sb, id, type))) {
1584 up(&sb_dqopt(sb)->dqonoff_sem);
1587 do_set_dqblk(dquot, di);
1589 up(&sb_dqopt(sb)->dqonoff_sem);
1593 /* Generic routine for getting common part of quota file information */
1594 int vfs_get_dqinfo(struct super_block *sb, int type, struct if_dqinfo *ii)
1596 struct mem_dqinfo *mi;
1598 down(&sb_dqopt(sb)->dqonoff_sem);
1599 if (!sb_has_quota_enabled(sb, type)) {
1600 up(&sb_dqopt(sb)->dqonoff_sem);
1603 mi = sb_dqopt(sb)->info + type;
1604 spin_lock(&dq_data_lock);
1605 ii->dqi_bgrace = mi->dqi_bgrace;
1606 ii->dqi_igrace = mi->dqi_igrace;
1607 ii->dqi_flags = mi->dqi_flags & DQF_MASK;
1608 ii->dqi_valid = IIF_ALL;
1609 spin_unlock(&dq_data_lock);
1610 up(&sb_dqopt(sb)->dqonoff_sem);
1614 /* Generic routine for setting common part of quota file information */
1615 int vfs_set_dqinfo(struct super_block *sb, int type, struct if_dqinfo *ii)
1617 struct mem_dqinfo *mi;
1619 down(&sb_dqopt(sb)->dqonoff_sem);
1620 if (!sb_has_quota_enabled(sb, type)) {
1621 up(&sb_dqopt(sb)->dqonoff_sem);
1624 mi = sb_dqopt(sb)->info + type;
1625 spin_lock(&dq_data_lock);
1626 if (ii->dqi_valid & IIF_BGRACE)
1627 mi->dqi_bgrace = ii->dqi_bgrace;
1628 if (ii->dqi_valid & IIF_IGRACE)
1629 mi->dqi_igrace = ii->dqi_igrace;
1630 if (ii->dqi_valid & IIF_FLAGS)
1631 mi->dqi_flags = (mi->dqi_flags & ~DQF_MASK) | (ii->dqi_flags & DQF_MASK);
1632 spin_unlock(&dq_data_lock);
1633 mark_info_dirty(sb, type);
1634 /* Force write to disk */
1635 sb->dq_op->write_info(sb, type);
1636 up(&sb_dqopt(sb)->dqonoff_sem);
1640 struct quotactl_ops vfs_quotactl_ops = {
1641 .quota_on = vfs_quota_on,
1642 .quota_off = vfs_quota_off,
1643 .quota_sync = vfs_quota_sync,
1644 .get_info = vfs_get_dqinfo,
1645 .set_info = vfs_set_dqinfo,
1646 .get_dqblk = vfs_get_dqblk,
1647 .set_dqblk = vfs_set_dqblk
1650 static ctl_table fs_dqstats_table[] = {
1652 .ctl_name = FS_DQ_LOOKUPS,
1653 .procname = "lookups",
1654 .data = &dqstats.lookups,
1655 .maxlen = sizeof(int),
1657 .proc_handler = &proc_dointvec,
1660 .ctl_name = FS_DQ_DROPS,
1661 .procname = "drops",
1662 .data = &dqstats.drops,
1663 .maxlen = sizeof(int),
1665 .proc_handler = &proc_dointvec,
1668 .ctl_name = FS_DQ_READS,
1669 .procname = "reads",
1670 .data = &dqstats.reads,
1671 .maxlen = sizeof(int),
1673 .proc_handler = &proc_dointvec,
1676 .ctl_name = FS_DQ_WRITES,
1677 .procname = "writes",
1678 .data = &dqstats.writes,
1679 .maxlen = sizeof(int),
1681 .proc_handler = &proc_dointvec,
1684 .ctl_name = FS_DQ_CACHE_HITS,
1685 .procname = "cache_hits",
1686 .data = &dqstats.cache_hits,
1687 .maxlen = sizeof(int),
1689 .proc_handler = &proc_dointvec,
1692 .ctl_name = FS_DQ_ALLOCATED,
1693 .procname = "allocated_dquots",
1694 .data = &dqstats.allocated_dquots,
1695 .maxlen = sizeof(int),
1697 .proc_handler = &proc_dointvec,
1700 .ctl_name = FS_DQ_FREE,
1701 .procname = "free_dquots",
1702 .data = &dqstats.free_dquots,
1703 .maxlen = sizeof(int),
1705 .proc_handler = &proc_dointvec,
1708 .ctl_name = FS_DQ_SYNCS,
1709 .procname = "syncs",
1710 .data = &dqstats.syncs,
1711 .maxlen = sizeof(int),
1713 .proc_handler = &proc_dointvec,
1718 static ctl_table fs_table[] = {
1720 .ctl_name = FS_DQSTATS,
1721 .procname = "quota",
1723 .child = fs_dqstats_table,
1728 static ctl_table sys_table[] = {
1738 /* SLAB cache for dquot structures */
1739 kmem_cache_t *dquot_cachep;
1741 static int __init dquot_init(void)
1744 unsigned long nr_hash, order;
1746 printk(KERN_NOTICE "VFS: Disk quotas %s\n", __DQUOT_VERSION__);
1748 register_sysctl_table(sys_table, 0);
1750 dquot_cachep = kmem_cache_create("dquot",
1751 sizeof(struct dquot), sizeof(unsigned long) * 4,
1752 SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT|SLAB_PANIC,
1756 dquot_hash = (struct hlist_head *)__get_free_pages(GFP_ATOMIC, order);
1758 panic("Cannot create dquot hash table");
1760 /* Find power-of-two hlist_heads which can fit into allocation */
1761 nr_hash = (1UL << order) * PAGE_SIZE / sizeof(struct hlist_head);
1765 } while (nr_hash >> dq_hash_bits);
1768 nr_hash = 1UL << dq_hash_bits;
1769 dq_hash_mask = nr_hash - 1;
1770 for (i = 0; i < nr_hash; i++)
1771 INIT_HLIST_HEAD(dquot_hash + i);
1773 printk("Dquot-cache hash table entries: %ld (order %ld, %ld bytes)\n",
1774 nr_hash, order, (PAGE_SIZE << order));
1776 set_shrinker(DEFAULT_SEEKS, shrink_dqcache_memory);
1780 module_init(dquot_init);
1782 EXPORT_SYMBOL(register_quota_format);
1783 EXPORT_SYMBOL(unregister_quota_format);
1784 EXPORT_SYMBOL(dqstats);
1785 EXPORT_SYMBOL(dq_list_lock);
1786 EXPORT_SYMBOL(dq_data_lock);
1787 EXPORT_SYMBOL(vfs_quota_on);
1788 EXPORT_SYMBOL(vfs_quota_on_mount);
1789 EXPORT_SYMBOL(vfs_quota_off);
1790 EXPORT_SYMBOL(vfs_quota_sync);
1791 EXPORT_SYMBOL(vfs_get_dqinfo);
1792 EXPORT_SYMBOL(vfs_set_dqinfo);
1793 EXPORT_SYMBOL(vfs_get_dqblk);
1794 EXPORT_SYMBOL(vfs_set_dqblk);
1795 EXPORT_SYMBOL(dquot_commit);
1796 EXPORT_SYMBOL(dquot_commit_info);
1797 EXPORT_SYMBOL(dquot_acquire);
1798 EXPORT_SYMBOL(dquot_release);
1799 EXPORT_SYMBOL(dquot_mark_dquot_dirty);
1800 EXPORT_SYMBOL(dquot_initialize);
1801 EXPORT_SYMBOL(dquot_drop);
1802 EXPORT_SYMBOL(dquot_alloc_space);
1803 EXPORT_SYMBOL(dquot_alloc_inode);
1804 EXPORT_SYMBOL(dquot_free_space);
1805 EXPORT_SYMBOL(dquot_free_inode);
1806 EXPORT_SYMBOL(dquot_transfer);