2 * Implementation of the diskquota system for the LINUX operating system. QUOTA
3 * is implemented using the BSD system call interface as the means of
4 * communication with the user level. This file contains the generic routines
5 * called by the different filesystems on allocation of an inode or block.
6 * These routines take care of the administration needed to have a consistent
7 * diskquota tracking system. The ideas of both user and group quotas are based
8 * on the Melbourne quota system as used on BSD derived systems. The internal
9 * implementation is based on one of the several variants of the LINUX
10 * inode-subsystem with added complexity of the diskquota system.
12 * Version: $Id: dquot.c,v 6.3 1996/11/17 18:35:34 mvw Exp mvw $
14 * Author: Marco van Wieringen <mvw@planets.elm.net>
16 * Fixes: Dmitry Gorodchanin <pgmdsg@ibi.com>, 11 Feb 96
18 * Revised list management to avoid races
19 * -- Bill Hawes, <whawes@star.net>, 9/98
21 * Fixed races in dquot_transfer(), dqget() and dquot_alloc_...().
22 * As the consequence the locking was moved from dquot_decr_...(),
23 * dquot_incr_...() to calling functions.
24 * invalidate_dquots() now writes modified dquots.
25 * Serialized quota_off() and quota_on() for mount point.
26 * Fixed a few bugs in grow_dquots().
27 * Fixed deadlock in write_dquot() - we no longer account quotas on
29 * remove_dquot_ref() moved to inode.c - it now traverses through inodes
30 * add_dquot_ref() restarts after blocking
31 * Added check for bogus uid and fixed check for group in quotactl.
32 * Jan Kara, <jack@suse.cz>, sponsored by SuSE CR, 10-11/99
34 * Used struct list_head instead of own list struct
35 * Invalidation of referenced dquots is no longer possible
36 * Improved free_dquots list management
37 * Quota and i_blocks are now updated in one place to avoid races
38 * Warnings are now delayed so we won't block in critical section
39 * Write updated not to require dquot lock
40 * Jan Kara, <jack@suse.cz>, 9/2000
42 * Added dynamic quota structure allocation
43 * Jan Kara <jack@suse.cz> 12/2000
45 * Rewritten quota interface. Implemented new quota format and
46 * formats registering.
47 * Jan Kara, <jack@suse.cz>, 2001,2002
50 * Jan Kara, <jack@suse.cz>, 10/2002
52 * Added journalled quota support, fix lock inversion problems
53 * Jan Kara, <jack@suse.cz>, 2003,2004
55 * (C) Copyright 1994 - 1997 Marco van Wieringen
58 #include <linux/errno.h>
59 #include <linux/kernel.h>
61 #include <linux/mount.h>
63 #include <linux/time.h>
64 #include <linux/types.h>
65 #include <linux/string.h>
66 #include <linux/fcntl.h>
67 #include <linux/stat.h>
68 #include <linux/tty.h>
69 #include <linux/file.h>
70 #include <linux/slab.h>
71 #include <linux/sysctl.h>
72 #include <linux/smp_lock.h>
73 #include <linux/init.h>
74 #include <linux/module.h>
75 #include <linux/proc_fs.h>
76 #include <linux/security.h>
77 #include <linux/kmod.h>
78 #include <linux/namei.h>
79 #include <linux/buffer_head.h>
81 #include <asm/uaccess.h>
83 #define __DQUOT_PARANOIA
86 * There are two quota SMP locks. dq_list_lock protects all lists with quotas
87 * and quota formats and also dqstats structure containing statistics about the
88 * lists. dq_data_lock protects data from dq_dqb and also mem_dqinfo structures
89 * and also guards consistency of dquot->dq_dqb with inode->i_blocks, i_bytes.
90 * i_blocks and i_bytes updates itself are guarded by i_lock acquired directly
91 * in inode_add_bytes() and inode_sub_bytes().
93 * The spinlock ordering is hence: dq_data_lock > dq_list_lock > i_lock
95 * Note that some things (eg. sb pointer, type, id) doesn't change during
96 * the life of the dquot structure and so needn't to be protected by a lock
98 * Any operation working on dquots via inode pointers must hold dqptr_sem. If
99 * operation is just reading pointers from inode (or not using them at all) the
100 * read lock is enough. If pointers are altered function must hold write lock
101 * (these locking rules also apply for S_NOQUOTA flag in the inode - note that
102 * for altering the flag i_sem is also needed). If operation is holding
103 * reference to dquot in other way (e.g. quotactl ops) it must be guarded by
105 * This locking assures that:
106 * a) update/access to dquot pointers in inode is serialized
107 * b) everyone is guarded against invalidate_dquots()
109 * Each dquot has its dq_lock semaphore. Locked dquots might not be referenced
110 * from inodes (dquot_alloc_space() and such don't check the dq_lock).
111 * Currently dquot is locked only when it is being read to memory (or space for
112 * it is being allocated) on the first dqget() and when it is being released on
113 * the last dqput(). The allocation and release oparations are serialized by
114 * the dq_lock and by checking the use count in dquot_release(). Write
115 * operations on dquots don't hold dq_lock as they copy data under dq_data_lock
116 * spinlock to internal buffers before writing.
118 * Lock ordering (including related VFS locks) is the following:
119 * i_sem > dqonoff_sem > iprune_sem > journal_lock > dqptr_sem >
120 * > dquot->dq_lock > dqio_sem
121 * i_sem on quota files is special (it's below dqio_sem)
124 static DEFINE_SPINLOCK(dq_list_lock);
125 DEFINE_SPINLOCK(dq_data_lock);
127 static char *quotatypes[] = INITQFNAMES;
128 static struct quota_format_type *quota_formats; /* List of registered formats */
129 static struct quota_module_name module_names[] = INIT_QUOTA_MODULE_NAMES;
131 /* SLAB cache for dquot structures */
132 static kmem_cache_t *dquot_cachep;
134 int register_quota_format(struct quota_format_type *fmt)
136 spin_lock(&dq_list_lock);
137 fmt->qf_next = quota_formats;
139 spin_unlock(&dq_list_lock);
143 void unregister_quota_format(struct quota_format_type *fmt)
145 struct quota_format_type **actqf;
147 spin_lock(&dq_list_lock);
148 for (actqf = "a_formats; *actqf && *actqf != fmt; actqf = &(*actqf)->qf_next);
150 *actqf = (*actqf)->qf_next;
151 spin_unlock(&dq_list_lock);
154 static struct quota_format_type *find_quota_format(int id)
156 struct quota_format_type *actqf;
158 spin_lock(&dq_list_lock);
159 for (actqf = quota_formats; actqf && actqf->qf_fmt_id != id; actqf = actqf->qf_next);
160 if (!actqf || !try_module_get(actqf->qf_owner)) {
163 spin_unlock(&dq_list_lock);
165 for (qm = 0; module_names[qm].qm_fmt_id && module_names[qm].qm_fmt_id != id; qm++);
166 if (!module_names[qm].qm_fmt_id || request_module(module_names[qm].qm_mod_name))
169 spin_lock(&dq_list_lock);
170 for (actqf = quota_formats; actqf && actqf->qf_fmt_id != id; actqf = actqf->qf_next);
171 if (actqf && !try_module_get(actqf->qf_owner))
174 spin_unlock(&dq_list_lock);
178 static void put_quota_format(struct quota_format_type *fmt)
180 module_put(fmt->qf_owner);
184 * Dquot List Management:
185 * The quota code uses three lists for dquot management: the inuse_list,
186 * free_dquots, and dquot_hash[] array. A single dquot structure may be
187 * on all three lists, depending on its current state.
189 * All dquots are placed to the end of inuse_list when first created, and this
190 * list is used for invalidate operation, which must look at every dquot.
192 * Unused dquots (dq_count == 0) are added to the free_dquots list when freed,
193 * and this list is searched whenever we need an available dquot. Dquots are
194 * removed from the list as soon as they are used again, and
195 * dqstats.free_dquots gives the number of dquots on the list. When
196 * dquot is invalidated it's completely released from memory.
198 * Dquots with a specific identity (device, type and id) are placed on
199 * one of the dquot_hash[] hash chains. The provides an efficient search
200 * mechanism to locate a specific dquot.
203 static LIST_HEAD(inuse_list);
204 static LIST_HEAD(free_dquots);
205 static unsigned int dq_hash_bits, dq_hash_mask;
206 static struct hlist_head *dquot_hash;
208 struct dqstats dqstats;
210 static void dqput(struct dquot *dquot);
212 static inline unsigned int
213 hashfn(const struct super_block *sb, unsigned int id, int type)
217 tmp = (((unsigned long)sb>>L1_CACHE_SHIFT) ^ id) * (MAXQUOTAS - type);
218 return (tmp + (tmp >> dq_hash_bits)) & dq_hash_mask;
222 * Following list functions expect dq_list_lock to be held
224 static inline void insert_dquot_hash(struct dquot *dquot)
226 struct hlist_head *head = dquot_hash + hashfn(dquot->dq_sb, dquot->dq_id, dquot->dq_type);
227 hlist_add_head(&dquot->dq_hash, head);
230 static inline void remove_dquot_hash(struct dquot *dquot)
232 hlist_del_init(&dquot->dq_hash);
235 static inline struct dquot *find_dquot(unsigned int hashent, struct super_block *sb, unsigned int id, int type)
237 struct hlist_node *node;
240 hlist_for_each (node, dquot_hash+hashent) {
241 dquot = hlist_entry(node, struct dquot, dq_hash);
242 if (dquot->dq_sb == sb && dquot->dq_id == id && dquot->dq_type == type)
248 /* Add a dquot to the tail of the free list */
249 static inline void put_dquot_last(struct dquot *dquot)
251 list_add(&dquot->dq_free, free_dquots.prev);
252 dqstats.free_dquots++;
255 static inline void remove_free_dquot(struct dquot *dquot)
257 if (list_empty(&dquot->dq_free))
259 list_del_init(&dquot->dq_free);
260 dqstats.free_dquots--;
263 static inline void put_inuse(struct dquot *dquot)
265 /* We add to the back of inuse list so we don't have to restart
266 * when traversing this list and we block */
267 list_add(&dquot->dq_inuse, inuse_list.prev);
268 dqstats.allocated_dquots++;
271 static inline void remove_inuse(struct dquot *dquot)
273 dqstats.allocated_dquots--;
274 list_del(&dquot->dq_inuse);
277 * End of list functions needing dq_list_lock
280 static void wait_on_dquot(struct dquot *dquot)
282 down(&dquot->dq_lock);
286 #define mark_dquot_dirty(dquot) ((dquot)->dq_sb->dq_op->mark_dirty(dquot))
288 int dquot_mark_dquot_dirty(struct dquot *dquot)
290 spin_lock(&dq_list_lock);
291 if (!test_and_set_bit(DQ_MOD_B, &dquot->dq_flags))
292 list_add(&dquot->dq_dirty, &sb_dqopt(dquot->dq_sb)->
293 info[dquot->dq_type].dqi_dirty_list);
294 spin_unlock(&dq_list_lock);
298 /* This function needs dq_list_lock */
299 static inline int clear_dquot_dirty(struct dquot *dquot)
301 if (!test_and_clear_bit(DQ_MOD_B, &dquot->dq_flags))
303 list_del_init(&dquot->dq_dirty);
307 void mark_info_dirty(struct super_block *sb, int type)
309 set_bit(DQF_INFO_DIRTY_B, &sb_dqopt(sb)->info[type].dqi_flags);
311 EXPORT_SYMBOL(mark_info_dirty);
314 * Read dquot from disk and alloc space for it
317 int dquot_acquire(struct dquot *dquot)
319 int ret = 0, ret2 = 0;
320 struct quota_info *dqopt = sb_dqopt(dquot->dq_sb);
322 down(&dquot->dq_lock);
323 down(&dqopt->dqio_sem);
324 if (!test_bit(DQ_READ_B, &dquot->dq_flags))
325 ret = dqopt->ops[dquot->dq_type]->read_dqblk(dquot);
328 set_bit(DQ_READ_B, &dquot->dq_flags);
329 /* Instantiate dquot if needed */
330 if (!test_bit(DQ_ACTIVE_B, &dquot->dq_flags) && !dquot->dq_off) {
331 ret = dqopt->ops[dquot->dq_type]->commit_dqblk(dquot);
332 /* Write the info if needed */
333 if (info_dirty(&dqopt->info[dquot->dq_type]))
334 ret2 = dqopt->ops[dquot->dq_type]->write_file_info(dquot->dq_sb, dquot->dq_type);
342 set_bit(DQ_ACTIVE_B, &dquot->dq_flags);
344 up(&dqopt->dqio_sem);
350 * Write dquot to disk
352 int dquot_commit(struct dquot *dquot)
354 int ret = 0, ret2 = 0;
355 struct quota_info *dqopt = sb_dqopt(dquot->dq_sb);
357 down(&dqopt->dqio_sem);
358 spin_lock(&dq_list_lock);
359 if (!clear_dquot_dirty(dquot)) {
360 spin_unlock(&dq_list_lock);
363 spin_unlock(&dq_list_lock);
364 /* Inactive dquot can be only if there was error during read/init
365 * => we have better not writing it */
366 if (test_bit(DQ_ACTIVE_B, &dquot->dq_flags)) {
367 ret = dqopt->ops[dquot->dq_type]->commit_dqblk(dquot);
368 if (info_dirty(&dqopt->info[dquot->dq_type]))
369 ret2 = dqopt->ops[dquot->dq_type]->write_file_info(dquot->dq_sb, dquot->dq_type);
374 up(&dqopt->dqio_sem);
381 int dquot_release(struct dquot *dquot)
383 int ret = 0, ret2 = 0;
384 struct quota_info *dqopt = sb_dqopt(dquot->dq_sb);
386 down(&dquot->dq_lock);
387 /* Check whether we are not racing with some other dqget() */
388 if (atomic_read(&dquot->dq_count) > 1)
390 down(&dqopt->dqio_sem);
391 if (dqopt->ops[dquot->dq_type]->release_dqblk) {
392 ret = dqopt->ops[dquot->dq_type]->release_dqblk(dquot);
394 if (info_dirty(&dqopt->info[dquot->dq_type]))
395 ret2 = dqopt->ops[dquot->dq_type]->write_file_info(dquot->dq_sb, dquot->dq_type);
399 clear_bit(DQ_ACTIVE_B, &dquot->dq_flags);
400 up(&dqopt->dqio_sem);
406 /* Invalidate all dquots on the list. Note that this function is called after
407 * quota is disabled and pointers from inodes removed so there cannot be new
408 * quota users. Also because we hold dqonoff_sem there can be no quota users
409 * for this sb+type at all. */
410 static void invalidate_dquots(struct super_block *sb, int type)
412 struct dquot *dquot, *tmp;
414 spin_lock(&dq_list_lock);
415 list_for_each_entry_safe(dquot, tmp, &inuse_list, dq_inuse) {
416 if (dquot->dq_sb != sb)
418 if (dquot->dq_type != type)
420 #ifdef __DQUOT_PARANOIA
421 if (atomic_read(&dquot->dq_count))
424 /* Quota now has no users and it has been written on last dqput() */
425 remove_dquot_hash(dquot);
426 remove_free_dquot(dquot);
428 kmem_cache_free(dquot_cachep, dquot);
430 spin_unlock(&dq_list_lock);
433 int vfs_quota_sync(struct super_block *sb, int type)
435 struct list_head *dirty;
437 struct quota_info *dqopt = sb_dqopt(sb);
440 down(&dqopt->dqonoff_sem);
441 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
442 if (type != -1 && cnt != type)
444 if (!sb_has_quota_enabled(sb, cnt))
446 spin_lock(&dq_list_lock);
447 dirty = &dqopt->info[cnt].dqi_dirty_list;
448 while (!list_empty(dirty)) {
449 dquot = list_entry(dirty->next, struct dquot, dq_dirty);
450 /* Dirty and inactive can be only bad dquot... */
451 if (!test_bit(DQ_ACTIVE_B, &dquot->dq_flags)) {
452 clear_dquot_dirty(dquot);
455 /* Now we have active dquot from which someone is
456 * holding reference so we can safely just increase
458 atomic_inc(&dquot->dq_count);
460 spin_unlock(&dq_list_lock);
461 sb->dq_op->write_dquot(dquot);
463 spin_lock(&dq_list_lock);
465 spin_unlock(&dq_list_lock);
468 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
469 if ((cnt == type || type == -1) && sb_has_quota_enabled(sb, cnt)
470 && info_dirty(&dqopt->info[cnt]))
471 sb->dq_op->write_info(sb, cnt);
472 spin_lock(&dq_list_lock);
474 spin_unlock(&dq_list_lock);
475 up(&dqopt->dqonoff_sem);
480 /* Free unused dquots from cache */
481 static void prune_dqcache(int count)
483 struct list_head *head;
486 head = free_dquots.prev;
487 while (head != &free_dquots && count) {
488 dquot = list_entry(head, struct dquot, dq_free);
489 remove_dquot_hash(dquot);
490 remove_free_dquot(dquot);
492 kmem_cache_free(dquot_cachep, dquot);
494 head = free_dquots.prev;
499 * This is called from kswapd when we think we need some
503 static int shrink_dqcache_memory(int nr, gfp_t gfp_mask)
506 spin_lock(&dq_list_lock);
508 spin_unlock(&dq_list_lock);
510 return (dqstats.free_dquots / 100) * sysctl_vfs_cache_pressure;
514 * Put reference to dquot
515 * NOTE: If you change this function please check whether dqput_blocks() works right...
516 * MUST be called with either dqptr_sem or dqonoff_sem held
518 static void dqput(struct dquot *dquot)
522 #ifdef __DQUOT_PARANOIA
523 if (!atomic_read(&dquot->dq_count)) {
524 printk("VFS: dqput: trying to free free dquot\n");
525 printk("VFS: device %s, dquot of %s %d\n",
527 quotatypes[dquot->dq_type],
533 spin_lock(&dq_list_lock);
535 spin_unlock(&dq_list_lock);
537 spin_lock(&dq_list_lock);
538 if (atomic_read(&dquot->dq_count) > 1) {
539 /* We have more than one user... nothing to do */
540 atomic_dec(&dquot->dq_count);
541 spin_unlock(&dq_list_lock);
544 /* Need to release dquot? */
545 if (test_bit(DQ_ACTIVE_B, &dquot->dq_flags) && dquot_dirty(dquot)) {
546 spin_unlock(&dq_list_lock);
547 /* Commit dquot before releasing */
548 dquot->dq_sb->dq_op->write_dquot(dquot);
551 /* Clear flag in case dquot was inactive (something bad happened) */
552 clear_dquot_dirty(dquot);
553 if (test_bit(DQ_ACTIVE_B, &dquot->dq_flags)) {
554 spin_unlock(&dq_list_lock);
555 dquot->dq_sb->dq_op->release_dquot(dquot);
558 atomic_dec(&dquot->dq_count);
559 #ifdef __DQUOT_PARANOIA
561 if (!list_empty(&dquot->dq_free))
564 put_dquot_last(dquot);
565 spin_unlock(&dq_list_lock);
568 static struct dquot *get_empty_dquot(struct super_block *sb, int type)
572 dquot = kmem_cache_alloc(dquot_cachep, SLAB_NOFS);
576 memset((caddr_t)dquot, 0, sizeof(struct dquot));
577 sema_init(&dquot->dq_lock, 1);
578 INIT_LIST_HEAD(&dquot->dq_free);
579 INIT_LIST_HEAD(&dquot->dq_inuse);
580 INIT_HLIST_NODE(&dquot->dq_hash);
581 INIT_LIST_HEAD(&dquot->dq_dirty);
583 dquot->dq_type = type;
584 atomic_set(&dquot->dq_count, 1);
590 * Get reference to dquot
591 * MUST be called with either dqptr_sem or dqonoff_sem held
593 static struct dquot *dqget(struct super_block *sb, unsigned int id, int type)
595 unsigned int hashent = hashfn(sb, id, type);
596 struct dquot *dquot, *empty = NODQUOT;
598 if (!sb_has_quota_enabled(sb, type))
601 spin_lock(&dq_list_lock);
602 if ((dquot = find_dquot(hashent, sb, id, type)) == NODQUOT) {
603 if (empty == NODQUOT) {
604 spin_unlock(&dq_list_lock);
605 if ((empty = get_empty_dquot(sb, type)) == NODQUOT)
606 schedule(); /* Try to wait for a moment... */
611 /* all dquots go on the inuse_list */
613 /* hash it first so it can be found */
614 insert_dquot_hash(dquot);
616 spin_unlock(&dq_list_lock);
618 if (!atomic_read(&dquot->dq_count))
619 remove_free_dquot(dquot);
620 atomic_inc(&dquot->dq_count);
621 dqstats.cache_hits++;
623 spin_unlock(&dq_list_lock);
625 kmem_cache_free(dquot_cachep, empty);
627 /* Wait for dq_lock - after this we know that either dquot_release() is already
628 * finished or it will be canceled due to dq_count > 1 test */
629 wait_on_dquot(dquot);
630 /* Read the dquot and instantiate it (everything done only if needed) */
631 if (!test_bit(DQ_ACTIVE_B, &dquot->dq_flags) && sb->dq_op->acquire_dquot(dquot) < 0) {
635 #ifdef __DQUOT_PARANOIA
636 if (!dquot->dq_sb) /* Has somebody invalidated entry under us? */
643 static int dqinit_needed(struct inode *inode, int type)
647 if (IS_NOQUOTA(inode))
650 return inode->i_dquot[type] == NODQUOT;
651 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
652 if (inode->i_dquot[cnt] == NODQUOT)
657 /* This routine is guarded by dqonoff_sem semaphore */
658 static void add_dquot_ref(struct super_block *sb, int type)
664 list_for_each(p, &sb->s_files) {
665 struct file *filp = list_entry(p, struct file, f_u.fu_list);
666 struct inode *inode = filp->f_dentry->d_inode;
667 if (filp->f_mode & FMODE_WRITE && dqinit_needed(inode, type)) {
668 struct dentry *dentry = dget(filp->f_dentry);
670 sb->dq_op->initialize(inode, type);
672 /* As we may have blocked we had better restart... */
679 /* Return 0 if dqput() won't block (note that 1 doesn't necessarily mean blocking) */
680 static inline int dqput_blocks(struct dquot *dquot)
682 if (atomic_read(&dquot->dq_count) <= 1)
687 /* Remove references to dquots from inode - add dquot to list for freeing if needed */
688 /* We can't race with anybody because we hold dqptr_sem for writing... */
689 int remove_inode_dquot_ref(struct inode *inode, int type, struct list_head *tofree_head)
691 struct dquot *dquot = inode->i_dquot[type];
693 inode->i_dquot[type] = NODQUOT;
694 if (dquot != NODQUOT) {
695 if (dqput_blocks(dquot)) {
696 #ifdef __DQUOT_PARANOIA
697 if (atomic_read(&dquot->dq_count) != 1)
698 printk(KERN_WARNING "VFS: Adding dquot with dq_count %d to dispose list.\n", atomic_read(&dquot->dq_count));
700 spin_lock(&dq_list_lock);
701 list_add(&dquot->dq_free, tofree_head); /* As dquot must have currently users it can't be on the free list... */
702 spin_unlock(&dq_list_lock);
706 dqput(dquot); /* We have guaranteed we won't block */
711 /* Free list of dquots - called from inode.c */
712 /* dquots are removed from inodes, no new references can be got so we are the only ones holding reference */
713 static void put_dquot_list(struct list_head *tofree_head)
715 struct list_head *act_head;
718 act_head = tofree_head->next;
719 /* So now we have dquots on the list... Just free them */
720 while (act_head != tofree_head) {
721 dquot = list_entry(act_head, struct dquot, dq_free);
722 act_head = act_head->next;
723 list_del_init(&dquot->dq_free); /* Remove dquot from the list so we won't have problems... */
728 /* Gather all references from inodes and drop them */
729 static void drop_dquot_ref(struct super_block *sb, int type)
731 LIST_HEAD(tofree_head);
733 /* We need to be guarded against prune_icache to reach all the
734 * inodes - otherwise some can be on the local list of prune_icache */
736 down_write(&sb_dqopt(sb)->dqptr_sem);
737 remove_dquot_ref(sb, type, &tofree_head);
738 up_write(&sb_dqopt(sb)->dqptr_sem);
740 put_dquot_list(&tofree_head);
743 static inline void dquot_incr_inodes(struct dquot *dquot, unsigned long number)
745 dquot->dq_dqb.dqb_curinodes += number;
748 static inline void dquot_incr_space(struct dquot *dquot, qsize_t number)
750 dquot->dq_dqb.dqb_curspace += number;
753 static inline void dquot_decr_inodes(struct dquot *dquot, unsigned long number)
755 if (dquot->dq_dqb.dqb_curinodes > number)
756 dquot->dq_dqb.dqb_curinodes -= number;
758 dquot->dq_dqb.dqb_curinodes = 0;
759 if (dquot->dq_dqb.dqb_curinodes <= dquot->dq_dqb.dqb_isoftlimit)
760 dquot->dq_dqb.dqb_itime = (time_t) 0;
761 clear_bit(DQ_INODES_B, &dquot->dq_flags);
764 static inline void dquot_decr_space(struct dquot *dquot, qsize_t number)
766 if (dquot->dq_dqb.dqb_curspace > number)
767 dquot->dq_dqb.dqb_curspace -= number;
769 dquot->dq_dqb.dqb_curspace = 0;
770 if (toqb(dquot->dq_dqb.dqb_curspace) <= dquot->dq_dqb.dqb_bsoftlimit)
771 dquot->dq_dqb.dqb_btime = (time_t) 0;
772 clear_bit(DQ_BLKS_B, &dquot->dq_flags);
775 static int flag_print_warnings = 1;
777 static inline int need_print_warning(struct dquot *dquot)
779 if (!flag_print_warnings)
782 switch (dquot->dq_type) {
784 return current->fsuid == dquot->dq_id;
786 return in_group_p(dquot->dq_id);
791 /* Values of warnings */
794 #define ISOFTLONGWARN 2
797 #define BSOFTLONGWARN 5
800 /* Print warning to user which exceeded quota */
801 static void print_warning(struct dquot *dquot, const char warntype)
804 int flag = (warntype == BHARDWARN || warntype == BSOFTLONGWARN) ? DQ_BLKS_B :
805 ((warntype == IHARDWARN || warntype == ISOFTLONGWARN) ? DQ_INODES_B : 0);
807 if (!need_print_warning(dquot) || (flag && test_and_set_bit(flag, &dquot->dq_flags)))
810 tty_write_message(current->signal->tty, dquot->dq_sb->s_id);
811 if (warntype == ISOFTWARN || warntype == BSOFTWARN)
812 tty_write_message(current->signal->tty, ": warning, ");
814 tty_write_message(current->signal->tty, ": write failed, ");
815 tty_write_message(current->signal->tty, quotatypes[dquot->dq_type]);
818 msg = " file limit reached.\r\n";
821 msg = " file quota exceeded too long.\r\n";
824 msg = " file quota exceeded.\r\n";
827 msg = " block limit reached.\r\n";
830 msg = " block quota exceeded too long.\r\n";
833 msg = " block quota exceeded.\r\n";
836 tty_write_message(current->signal->tty, msg);
839 static inline void flush_warnings(struct dquot **dquots, char *warntype)
843 for (i = 0; i < MAXQUOTAS; i++)
844 if (dquots[i] != NODQUOT && warntype[i] != NOWARN)
845 print_warning(dquots[i], warntype[i]);
848 static inline char ignore_hardlimit(struct dquot *dquot)
850 struct mem_dqinfo *info = &sb_dqopt(dquot->dq_sb)->info[dquot->dq_type];
852 return capable(CAP_SYS_RESOURCE) &&
853 (info->dqi_format->qf_fmt_id != QFMT_VFS_OLD || !(info->dqi_flags & V1_DQF_RSQUASH));
856 /* needs dq_data_lock */
857 static int check_idq(struct dquot *dquot, ulong inodes, char *warntype)
860 if (inodes <= 0 || test_bit(DQ_FAKE_B, &dquot->dq_flags))
863 if (dquot->dq_dqb.dqb_ihardlimit &&
864 (dquot->dq_dqb.dqb_curinodes + inodes) > dquot->dq_dqb.dqb_ihardlimit &&
865 !ignore_hardlimit(dquot)) {
866 *warntype = IHARDWARN;
870 if (dquot->dq_dqb.dqb_isoftlimit &&
871 (dquot->dq_dqb.dqb_curinodes + inodes) > dquot->dq_dqb.dqb_isoftlimit &&
872 dquot->dq_dqb.dqb_itime && get_seconds() >= dquot->dq_dqb.dqb_itime &&
873 !ignore_hardlimit(dquot)) {
874 *warntype = ISOFTLONGWARN;
878 if (dquot->dq_dqb.dqb_isoftlimit &&
879 (dquot->dq_dqb.dqb_curinodes + inodes) > dquot->dq_dqb.dqb_isoftlimit &&
880 dquot->dq_dqb.dqb_itime == 0) {
881 *warntype = ISOFTWARN;
882 dquot->dq_dqb.dqb_itime = get_seconds() + sb_dqopt(dquot->dq_sb)->info[dquot->dq_type].dqi_igrace;
888 /* needs dq_data_lock */
889 static int check_bdq(struct dquot *dquot, qsize_t space, int prealloc, char *warntype)
892 if (space <= 0 || test_bit(DQ_FAKE_B, &dquot->dq_flags))
895 if (dquot->dq_dqb.dqb_bhardlimit &&
896 toqb(dquot->dq_dqb.dqb_curspace + space) > dquot->dq_dqb.dqb_bhardlimit &&
897 !ignore_hardlimit(dquot)) {
899 *warntype = BHARDWARN;
903 if (dquot->dq_dqb.dqb_bsoftlimit &&
904 toqb(dquot->dq_dqb.dqb_curspace + space) > dquot->dq_dqb.dqb_bsoftlimit &&
905 dquot->dq_dqb.dqb_btime && get_seconds() >= dquot->dq_dqb.dqb_btime &&
906 !ignore_hardlimit(dquot)) {
908 *warntype = BSOFTLONGWARN;
912 if (dquot->dq_dqb.dqb_bsoftlimit &&
913 toqb(dquot->dq_dqb.dqb_curspace + space) > dquot->dq_dqb.dqb_bsoftlimit &&
914 dquot->dq_dqb.dqb_btime == 0) {
916 *warntype = BSOFTWARN;
917 dquot->dq_dqb.dqb_btime = get_seconds() + sb_dqopt(dquot->dq_sb)->info[dquot->dq_type].dqi_bgrace;
921 * We don't allow preallocation to exceed softlimit so exceeding will
931 * Initialize quota pointers in inode
932 * Transaction must be started at entry
934 int dquot_initialize(struct inode *inode, int type)
939 /* First test before acquiring semaphore - solves deadlocks when we
940 * re-enter the quota code and are already holding the semaphore */
941 if (IS_NOQUOTA(inode))
943 down_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
944 /* Having dqptr_sem we know NOQUOTA flags can't be altered... */
945 if (IS_NOQUOTA(inode))
947 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
948 if (type != -1 && cnt != type)
950 if (inode->i_dquot[cnt] == NODQUOT) {
959 inode->i_dquot[cnt] = dqget(inode->i_sb, id, cnt);
963 up_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
968 * Release all quotas referenced by inode
969 * Transaction must be started at an entry
971 int dquot_drop(struct inode *inode)
975 down_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
976 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
977 if (inode->i_dquot[cnt] != NODQUOT) {
978 dqput(inode->i_dquot[cnt]);
979 inode->i_dquot[cnt] = NODQUOT;
982 up_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
987 * Following four functions update i_blocks+i_bytes fields and
988 * quota information (together with appropriate checks)
989 * NOTE: We absolutely rely on the fact that caller dirties
990 * the inode (usually macros in quotaops.h care about this) and
991 * holds a handle for the current transaction so that dquot write and
992 * inode write go into the same transaction.
996 * This operation can block, but only after everything is updated
998 int dquot_alloc_space(struct inode *inode, qsize_t number, int warn)
1000 int cnt, ret = NO_QUOTA;
1001 char warntype[MAXQUOTAS];
1003 /* First test before acquiring semaphore - solves deadlocks when we
1004 * re-enter the quota code and are already holding the semaphore */
1005 if (IS_NOQUOTA(inode)) {
1007 inode_add_bytes(inode, number);
1010 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1011 warntype[cnt] = NOWARN;
1013 down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1014 if (IS_NOQUOTA(inode)) { /* Now we can do reliable test... */
1015 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1018 spin_lock(&dq_data_lock);
1019 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1020 if (inode->i_dquot[cnt] == NODQUOT)
1022 if (check_bdq(inode->i_dquot[cnt], number, warn, warntype+cnt) == NO_QUOTA)
1025 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1026 if (inode->i_dquot[cnt] == NODQUOT)
1028 dquot_incr_space(inode->i_dquot[cnt], number);
1030 inode_add_bytes(inode, number);
1033 spin_unlock(&dq_data_lock);
1034 if (ret == QUOTA_OK)
1035 /* Dirtify all the dquots - this can block when journalling */
1036 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1037 if (inode->i_dquot[cnt])
1038 mark_dquot_dirty(inode->i_dquot[cnt]);
1039 flush_warnings(inode->i_dquot, warntype);
1040 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1045 * This operation can block, but only after everything is updated
1047 int dquot_alloc_inode(const struct inode *inode, unsigned long number)
1049 int cnt, ret = NO_QUOTA;
1050 char warntype[MAXQUOTAS];
1052 /* First test before acquiring semaphore - solves deadlocks when we
1053 * re-enter the quota code and are already holding the semaphore */
1054 if (IS_NOQUOTA(inode))
1056 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1057 warntype[cnt] = NOWARN;
1058 down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1059 if (IS_NOQUOTA(inode)) {
1060 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1063 spin_lock(&dq_data_lock);
1064 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1065 if (inode->i_dquot[cnt] == NODQUOT)
1067 if (check_idq(inode->i_dquot[cnt], number, warntype+cnt) == NO_QUOTA)
1071 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1072 if (inode->i_dquot[cnt] == NODQUOT)
1074 dquot_incr_inodes(inode->i_dquot[cnt], number);
1078 spin_unlock(&dq_data_lock);
1079 if (ret == QUOTA_OK)
1080 /* Dirtify all the dquots - this can block when journalling */
1081 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1082 if (inode->i_dquot[cnt])
1083 mark_dquot_dirty(inode->i_dquot[cnt]);
1084 flush_warnings((struct dquot **)inode->i_dquot, warntype);
1085 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1090 * This operation can block, but only after everything is updated
1092 int dquot_free_space(struct inode *inode, qsize_t number)
1096 /* First test before acquiring semaphore - solves deadlocks when we
1097 * re-enter the quota code and are already holding the semaphore */
1098 if (IS_NOQUOTA(inode)) {
1100 inode_sub_bytes(inode, number);
1103 down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1104 /* Now recheck reliably when holding dqptr_sem */
1105 if (IS_NOQUOTA(inode)) {
1106 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1109 spin_lock(&dq_data_lock);
1110 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1111 if (inode->i_dquot[cnt] == NODQUOT)
1113 dquot_decr_space(inode->i_dquot[cnt], number);
1115 inode_sub_bytes(inode, number);
1116 spin_unlock(&dq_data_lock);
1117 /* Dirtify all the dquots - this can block when journalling */
1118 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1119 if (inode->i_dquot[cnt])
1120 mark_dquot_dirty(inode->i_dquot[cnt]);
1121 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1126 * This operation can block, but only after everything is updated
1128 int dquot_free_inode(const struct inode *inode, unsigned long number)
1132 /* First test before acquiring semaphore - solves deadlocks when we
1133 * re-enter the quota code and are already holding the semaphore */
1134 if (IS_NOQUOTA(inode))
1136 down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1137 /* Now recheck reliably when holding dqptr_sem */
1138 if (IS_NOQUOTA(inode)) {
1139 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1142 spin_lock(&dq_data_lock);
1143 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1144 if (inode->i_dquot[cnt] == NODQUOT)
1146 dquot_decr_inodes(inode->i_dquot[cnt], number);
1148 spin_unlock(&dq_data_lock);
1149 /* Dirtify all the dquots - this can block when journalling */
1150 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1151 if (inode->i_dquot[cnt])
1152 mark_dquot_dirty(inode->i_dquot[cnt]);
1153 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1158 * Transfer the number of inode and blocks from one diskquota to an other.
1160 * This operation can block, but only after everything is updated
1161 * A transaction must be started when entering this function.
1163 int dquot_transfer(struct inode *inode, struct iattr *iattr)
1166 struct dquot *transfer_from[MAXQUOTAS];
1167 struct dquot *transfer_to[MAXQUOTAS];
1168 int cnt, ret = NO_QUOTA, chuid = (iattr->ia_valid & ATTR_UID) && inode->i_uid != iattr->ia_uid,
1169 chgid = (iattr->ia_valid & ATTR_GID) && inode->i_gid != iattr->ia_gid;
1170 char warntype[MAXQUOTAS];
1172 /* First test before acquiring semaphore - solves deadlocks when we
1173 * re-enter the quota code and are already holding the semaphore */
1174 if (IS_NOQUOTA(inode))
1176 /* Clear the arrays */
1177 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1178 transfer_to[cnt] = transfer_from[cnt] = NODQUOT;
1179 warntype[cnt] = NOWARN;
1181 down_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
1182 /* Now recheck reliably when holding dqptr_sem */
1183 if (IS_NOQUOTA(inode)) { /* File without quota accounting? */
1184 up_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
1187 /* First build the transfer_to list - here we can block on
1188 * reading/instantiating of dquots. We know that the transaction for
1189 * us was already started so we don't violate lock ranking here */
1190 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1195 transfer_to[cnt] = dqget(inode->i_sb, iattr->ia_uid, cnt);
1200 transfer_to[cnt] = dqget(inode->i_sb, iattr->ia_gid, cnt);
1204 spin_lock(&dq_data_lock);
1205 space = inode_get_bytes(inode);
1206 /* Build the transfer_from list and check the limits */
1207 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1208 if (transfer_to[cnt] == NODQUOT)
1210 transfer_from[cnt] = inode->i_dquot[cnt];
1211 if (check_idq(transfer_to[cnt], 1, warntype+cnt) == NO_QUOTA ||
1212 check_bdq(transfer_to[cnt], space, 0, warntype+cnt) == NO_QUOTA)
1217 * Finally perform the needed transfer from transfer_from to transfer_to
1219 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1221 * Skip changes for same uid or gid or for turned off quota-type.
1223 if (transfer_to[cnt] == NODQUOT)
1226 /* Due to IO error we might not have transfer_from[] structure */
1227 if (transfer_from[cnt]) {
1228 dquot_decr_inodes(transfer_from[cnt], 1);
1229 dquot_decr_space(transfer_from[cnt], space);
1232 dquot_incr_inodes(transfer_to[cnt], 1);
1233 dquot_incr_space(transfer_to[cnt], space);
1235 inode->i_dquot[cnt] = transfer_to[cnt];
1239 spin_unlock(&dq_data_lock);
1240 /* Dirtify all the dquots - this can block when journalling */
1241 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1242 if (transfer_from[cnt])
1243 mark_dquot_dirty(transfer_from[cnt]);
1244 if (transfer_to[cnt])
1245 mark_dquot_dirty(transfer_to[cnt]);
1247 flush_warnings(transfer_to, warntype);
1249 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1250 if (ret == QUOTA_OK && transfer_from[cnt] != NODQUOT)
1251 dqput(transfer_from[cnt]);
1252 if (ret == NO_QUOTA && transfer_to[cnt] != NODQUOT)
1253 dqput(transfer_to[cnt]);
1255 up_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
1260 * Write info of quota file to disk
1262 int dquot_commit_info(struct super_block *sb, int type)
1265 struct quota_info *dqopt = sb_dqopt(sb);
1267 down(&dqopt->dqio_sem);
1268 ret = dqopt->ops[type]->write_file_info(sb, type);
1269 up(&dqopt->dqio_sem);
1274 * Definitions of diskquota operations.
1276 struct dquot_operations dquot_operations = {
1277 .initialize = dquot_initialize,
1279 .alloc_space = dquot_alloc_space,
1280 .alloc_inode = dquot_alloc_inode,
1281 .free_space = dquot_free_space,
1282 .free_inode = dquot_free_inode,
1283 .transfer = dquot_transfer,
1284 .write_dquot = dquot_commit,
1285 .acquire_dquot = dquot_acquire,
1286 .release_dquot = dquot_release,
1287 .mark_dirty = dquot_mark_dquot_dirty,
1288 .write_info = dquot_commit_info
1291 static inline void set_enable_flags(struct quota_info *dqopt, int type)
1295 dqopt->flags |= DQUOT_USR_ENABLED;
1298 dqopt->flags |= DQUOT_GRP_ENABLED;
1303 static inline void reset_enable_flags(struct quota_info *dqopt, int type)
1307 dqopt->flags &= ~DQUOT_USR_ENABLED;
1310 dqopt->flags &= ~DQUOT_GRP_ENABLED;
1316 * Turn quota off on a device. type == -1 ==> quotaoff for all types (umount)
1318 int vfs_quota_off(struct super_block *sb, int type)
1321 struct quota_info *dqopt = sb_dqopt(sb);
1322 struct inode *toputinode[MAXQUOTAS];
1323 struct vfsmount *toputmnt[MAXQUOTAS];
1325 /* We need to serialize quota_off() for device */
1326 down(&dqopt->dqonoff_sem);
1327 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1328 toputinode[cnt] = NULL;
1329 toputmnt[cnt] = NULL;
1330 if (type != -1 && cnt != type)
1332 if (!sb_has_quota_enabled(sb, cnt))
1334 reset_enable_flags(dqopt, cnt);
1336 /* Note: these are blocking operations */
1337 drop_dquot_ref(sb, cnt);
1338 invalidate_dquots(sb, cnt);
1340 * Now all dquots should be invalidated, all writes done so we should be only
1341 * users of the info. No locks needed.
1343 if (info_dirty(&dqopt->info[cnt]))
1344 sb->dq_op->write_info(sb, cnt);
1345 if (dqopt->ops[cnt]->free_file_info)
1346 dqopt->ops[cnt]->free_file_info(sb, cnt);
1347 put_quota_format(dqopt->info[cnt].dqi_format);
1349 toputinode[cnt] = dqopt->files[cnt];
1350 toputmnt[cnt] = dqopt->mnt[cnt];
1351 dqopt->files[cnt] = NULL;
1352 dqopt->mnt[cnt] = NULL;
1353 dqopt->info[cnt].dqi_flags = 0;
1354 dqopt->info[cnt].dqi_igrace = 0;
1355 dqopt->info[cnt].dqi_bgrace = 0;
1356 dqopt->ops[cnt] = NULL;
1358 up(&dqopt->dqonoff_sem);
1359 /* Sync the superblock so that buffers with quota data are written to
1360 * disk (and so userspace sees correct data afterwards).
1361 * The reference to vfsmnt we are still holding protects us from
1362 * umount (we don't have it only when quotas are turned on/off for
1363 * journal replay but in that case we are guarded by the fs anyway). */
1364 if (sb->s_op->sync_fs)
1365 sb->s_op->sync_fs(sb, 1);
1366 sync_blockdev(sb->s_bdev);
1367 /* Now the quota files are just ordinary files and we can set the
1368 * inode flags back. Moreover we discard the pagecache so that
1369 * userspace sees the writes we did bypassing the pagecache. We
1370 * must also discard the blockdev buffers so that we see the
1371 * changes done by userspace on the next quotaon() */
1372 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1373 if (toputinode[cnt]) {
1374 down(&dqopt->dqonoff_sem);
1375 /* If quota was reenabled in the meantime, we have
1377 if (!sb_has_quota_enabled(sb, cnt)) {
1378 down(&toputinode[cnt]->i_sem);
1379 toputinode[cnt]->i_flags &= ~(S_IMMUTABLE |
1380 S_NOATIME | S_NOQUOTA);
1381 truncate_inode_pages(&toputinode[cnt]->i_data, 0);
1382 up(&toputinode[cnt]->i_sem);
1383 mark_inode_dirty(toputinode[cnt]);
1384 iput(toputinode[cnt]);
1386 up(&dqopt->dqonoff_sem);
1387 /* We don't hold the reference when we turned on quotas
1388 * just for the journal replay... */
1390 mntput(toputmnt[cnt]);
1393 invalidate_bdev(sb->s_bdev, 0);
1398 * Turn quotas on on a device
1401 /* Helper function when we already have the inode */
1402 static int vfs_quota_on_inode(struct inode *inode, int type, int format_id)
1404 struct quota_format_type *fmt = find_quota_format(format_id);
1405 struct super_block *sb = inode->i_sb;
1406 struct quota_info *dqopt = sb_dqopt(sb);
1412 if (!S_ISREG(inode->i_mode)) {
1416 if (IS_RDONLY(inode)) {
1420 if (!sb->s_op->quota_write || !sb->s_op->quota_read) {
1425 /* As we bypass the pagecache we must now flush the inode so that
1426 * we see all the changes from userspace... */
1427 write_inode_now(inode, 1);
1428 /* And now flush the block cache so that kernel sees the changes */
1429 invalidate_bdev(sb->s_bdev, 0);
1430 down(&inode->i_sem);
1431 down(&dqopt->dqonoff_sem);
1432 if (sb_has_quota_enabled(sb, type)) {
1436 /* We don't want quota and atime on quota files (deadlocks possible)
1437 * Also nobody should write to the file - we use special IO operations
1438 * which ignore the immutable bit. */
1439 down_write(&dqopt->dqptr_sem);
1440 oldflags = inode->i_flags & (S_NOATIME | S_IMMUTABLE | S_NOQUOTA);
1441 inode->i_flags |= S_NOQUOTA | S_NOATIME | S_IMMUTABLE;
1442 up_write(&dqopt->dqptr_sem);
1443 sb->dq_op->drop(inode);
1446 dqopt->files[type] = igrab(inode);
1447 if (!dqopt->files[type])
1450 if (!fmt->qf_ops->check_quota_file(sb, type))
1453 dqopt->ops[type] = fmt->qf_ops;
1454 dqopt->info[type].dqi_format = fmt;
1455 INIT_LIST_HEAD(&dqopt->info[type].dqi_dirty_list);
1456 down(&dqopt->dqio_sem);
1457 if ((error = dqopt->ops[type]->read_file_info(sb, type)) < 0) {
1458 up(&dqopt->dqio_sem);
1461 up(&dqopt->dqio_sem);
1463 set_enable_flags(dqopt, type);
1465 add_dquot_ref(sb, type);
1466 up(&dqopt->dqonoff_sem);
1471 dqopt->files[type] = NULL;
1474 up(&dqopt->dqonoff_sem);
1475 if (oldflags != -1) {
1476 down_write(&dqopt->dqptr_sem);
1477 /* Set the flags back (in the case of accidental quotaon()
1478 * on a wrong file we don't want to mess up the flags) */
1479 inode->i_flags &= ~(S_NOATIME | S_NOQUOTA | S_IMMUTABLE);
1480 inode->i_flags |= oldflags;
1481 up_write(&dqopt->dqptr_sem);
1485 put_quota_format(fmt);
1490 /* Actual function called from quotactl() */
1491 int vfs_quota_on(struct super_block *sb, int type, int format_id, char *path)
1493 struct nameidata nd;
1496 error = path_lookup(path, LOOKUP_FOLLOW, &nd);
1499 error = security_quota_on(nd.dentry);
1502 /* Quota file not on the same filesystem? */
1503 if (nd.mnt->mnt_sb != sb)
1506 error = vfs_quota_on_inode(nd.dentry->d_inode, type, format_id);
1508 sb_dqopt(sb)->mnt[type] = mntget(nd.mnt);
1516 * This function is used when filesystem needs to initialize quotas
1517 * during mount time.
1519 int vfs_quota_on_mount(struct super_block *sb, char *qf_name,
1520 int format_id, int type)
1522 struct dentry *dentry;
1525 dentry = lookup_one_len(qf_name, sb->s_root, strlen(qf_name));
1527 return PTR_ERR(dentry);
1529 error = security_quota_on(dentry);
1531 error = vfs_quota_on_inode(dentry->d_inode, type, format_id);
1537 /* Generic routine for getting common part of quota structure */
1538 static void do_get_dqblk(struct dquot *dquot, struct if_dqblk *di)
1540 struct mem_dqblk *dm = &dquot->dq_dqb;
1542 spin_lock(&dq_data_lock);
1543 di->dqb_bhardlimit = dm->dqb_bhardlimit;
1544 di->dqb_bsoftlimit = dm->dqb_bsoftlimit;
1545 di->dqb_curspace = dm->dqb_curspace;
1546 di->dqb_ihardlimit = dm->dqb_ihardlimit;
1547 di->dqb_isoftlimit = dm->dqb_isoftlimit;
1548 di->dqb_curinodes = dm->dqb_curinodes;
1549 di->dqb_btime = dm->dqb_btime;
1550 di->dqb_itime = dm->dqb_itime;
1551 di->dqb_valid = QIF_ALL;
1552 spin_unlock(&dq_data_lock);
1555 int vfs_get_dqblk(struct super_block *sb, int type, qid_t id, struct if_dqblk *di)
1557 struct dquot *dquot;
1559 down(&sb_dqopt(sb)->dqonoff_sem);
1560 if (!(dquot = dqget(sb, id, type))) {
1561 up(&sb_dqopt(sb)->dqonoff_sem);
1564 do_get_dqblk(dquot, di);
1566 up(&sb_dqopt(sb)->dqonoff_sem);
1570 /* Generic routine for setting common part of quota structure */
1571 static void do_set_dqblk(struct dquot *dquot, struct if_dqblk *di)
1573 struct mem_dqblk *dm = &dquot->dq_dqb;
1574 int check_blim = 0, check_ilim = 0;
1576 spin_lock(&dq_data_lock);
1577 if (di->dqb_valid & QIF_SPACE) {
1578 dm->dqb_curspace = di->dqb_curspace;
1581 if (di->dqb_valid & QIF_BLIMITS) {
1582 dm->dqb_bsoftlimit = di->dqb_bsoftlimit;
1583 dm->dqb_bhardlimit = di->dqb_bhardlimit;
1586 if (di->dqb_valid & QIF_INODES) {
1587 dm->dqb_curinodes = di->dqb_curinodes;
1590 if (di->dqb_valid & QIF_ILIMITS) {
1591 dm->dqb_isoftlimit = di->dqb_isoftlimit;
1592 dm->dqb_ihardlimit = di->dqb_ihardlimit;
1595 if (di->dqb_valid & QIF_BTIME)
1596 dm->dqb_btime = di->dqb_btime;
1597 if (di->dqb_valid & QIF_ITIME)
1598 dm->dqb_itime = di->dqb_itime;
1601 if (!dm->dqb_bsoftlimit || toqb(dm->dqb_curspace) < dm->dqb_bsoftlimit) {
1603 clear_bit(DQ_BLKS_B, &dquot->dq_flags);
1605 else if (!(di->dqb_valid & QIF_BTIME)) /* Set grace only if user hasn't provided his own... */
1606 dm->dqb_btime = get_seconds() + sb_dqopt(dquot->dq_sb)->info[dquot->dq_type].dqi_bgrace;
1609 if (!dm->dqb_isoftlimit || dm->dqb_curinodes < dm->dqb_isoftlimit) {
1611 clear_bit(DQ_INODES_B, &dquot->dq_flags);
1613 else if (!(di->dqb_valid & QIF_ITIME)) /* Set grace only if user hasn't provided his own... */
1614 dm->dqb_itime = get_seconds() + sb_dqopt(dquot->dq_sb)->info[dquot->dq_type].dqi_igrace;
1616 if (dm->dqb_bhardlimit || dm->dqb_bsoftlimit || dm->dqb_ihardlimit || dm->dqb_isoftlimit)
1617 clear_bit(DQ_FAKE_B, &dquot->dq_flags);
1619 set_bit(DQ_FAKE_B, &dquot->dq_flags);
1620 spin_unlock(&dq_data_lock);
1621 mark_dquot_dirty(dquot);
1624 int vfs_set_dqblk(struct super_block *sb, int type, qid_t id, struct if_dqblk *di)
1626 struct dquot *dquot;
1628 down(&sb_dqopt(sb)->dqonoff_sem);
1629 if (!(dquot = dqget(sb, id, type))) {
1630 up(&sb_dqopt(sb)->dqonoff_sem);
1633 do_set_dqblk(dquot, di);
1635 up(&sb_dqopt(sb)->dqonoff_sem);
1639 /* Generic routine for getting common part of quota file information */
1640 int vfs_get_dqinfo(struct super_block *sb, int type, struct if_dqinfo *ii)
1642 struct mem_dqinfo *mi;
1644 down(&sb_dqopt(sb)->dqonoff_sem);
1645 if (!sb_has_quota_enabled(sb, type)) {
1646 up(&sb_dqopt(sb)->dqonoff_sem);
1649 mi = sb_dqopt(sb)->info + type;
1650 spin_lock(&dq_data_lock);
1651 ii->dqi_bgrace = mi->dqi_bgrace;
1652 ii->dqi_igrace = mi->dqi_igrace;
1653 ii->dqi_flags = mi->dqi_flags & DQF_MASK;
1654 ii->dqi_valid = IIF_ALL;
1655 spin_unlock(&dq_data_lock);
1656 up(&sb_dqopt(sb)->dqonoff_sem);
1660 /* Generic routine for setting common part of quota file information */
1661 int vfs_set_dqinfo(struct super_block *sb, int type, struct if_dqinfo *ii)
1663 struct mem_dqinfo *mi;
1665 down(&sb_dqopt(sb)->dqonoff_sem);
1666 if (!sb_has_quota_enabled(sb, type)) {
1667 up(&sb_dqopt(sb)->dqonoff_sem);
1670 mi = sb_dqopt(sb)->info + type;
1671 spin_lock(&dq_data_lock);
1672 if (ii->dqi_valid & IIF_BGRACE)
1673 mi->dqi_bgrace = ii->dqi_bgrace;
1674 if (ii->dqi_valid & IIF_IGRACE)
1675 mi->dqi_igrace = ii->dqi_igrace;
1676 if (ii->dqi_valid & IIF_FLAGS)
1677 mi->dqi_flags = (mi->dqi_flags & ~DQF_MASK) | (ii->dqi_flags & DQF_MASK);
1678 spin_unlock(&dq_data_lock);
1679 mark_info_dirty(sb, type);
1680 /* Force write to disk */
1681 sb->dq_op->write_info(sb, type);
1682 up(&sb_dqopt(sb)->dqonoff_sem);
1686 struct quotactl_ops vfs_quotactl_ops = {
1687 .quota_on = vfs_quota_on,
1688 .quota_off = vfs_quota_off,
1689 .quota_sync = vfs_quota_sync,
1690 .get_info = vfs_get_dqinfo,
1691 .set_info = vfs_set_dqinfo,
1692 .get_dqblk = vfs_get_dqblk,
1693 .set_dqblk = vfs_set_dqblk
1696 static ctl_table fs_dqstats_table[] = {
1698 .ctl_name = FS_DQ_LOOKUPS,
1699 .procname = "lookups",
1700 .data = &dqstats.lookups,
1701 .maxlen = sizeof(int),
1703 .proc_handler = &proc_dointvec,
1706 .ctl_name = FS_DQ_DROPS,
1707 .procname = "drops",
1708 .data = &dqstats.drops,
1709 .maxlen = sizeof(int),
1711 .proc_handler = &proc_dointvec,
1714 .ctl_name = FS_DQ_READS,
1715 .procname = "reads",
1716 .data = &dqstats.reads,
1717 .maxlen = sizeof(int),
1719 .proc_handler = &proc_dointvec,
1722 .ctl_name = FS_DQ_WRITES,
1723 .procname = "writes",
1724 .data = &dqstats.writes,
1725 .maxlen = sizeof(int),
1727 .proc_handler = &proc_dointvec,
1730 .ctl_name = FS_DQ_CACHE_HITS,
1731 .procname = "cache_hits",
1732 .data = &dqstats.cache_hits,
1733 .maxlen = sizeof(int),
1735 .proc_handler = &proc_dointvec,
1738 .ctl_name = FS_DQ_ALLOCATED,
1739 .procname = "allocated_dquots",
1740 .data = &dqstats.allocated_dquots,
1741 .maxlen = sizeof(int),
1743 .proc_handler = &proc_dointvec,
1746 .ctl_name = FS_DQ_FREE,
1747 .procname = "free_dquots",
1748 .data = &dqstats.free_dquots,
1749 .maxlen = sizeof(int),
1751 .proc_handler = &proc_dointvec,
1754 .ctl_name = FS_DQ_SYNCS,
1755 .procname = "syncs",
1756 .data = &dqstats.syncs,
1757 .maxlen = sizeof(int),
1759 .proc_handler = &proc_dointvec,
1762 .ctl_name = FS_DQ_WARNINGS,
1763 .procname = "warnings",
1764 .data = &flag_print_warnings,
1765 .maxlen = sizeof(int),
1767 .proc_handler = &proc_dointvec,
1772 static ctl_table fs_table[] = {
1774 .ctl_name = FS_DQSTATS,
1775 .procname = "quota",
1777 .child = fs_dqstats_table,
1782 static ctl_table sys_table[] = {
1792 static int __init dquot_init(void)
1795 unsigned long nr_hash, order;
1797 printk(KERN_NOTICE "VFS: Disk quotas %s\n", __DQUOT_VERSION__);
1799 register_sysctl_table(sys_table, 0);
1801 dquot_cachep = kmem_cache_create("dquot",
1802 sizeof(struct dquot), sizeof(unsigned long) * 4,
1803 SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT|SLAB_PANIC,
1807 dquot_hash = (struct hlist_head *)__get_free_pages(GFP_ATOMIC, order);
1809 panic("Cannot create dquot hash table");
1811 /* Find power-of-two hlist_heads which can fit into allocation */
1812 nr_hash = (1UL << order) * PAGE_SIZE / sizeof(struct hlist_head);
1816 } while (nr_hash >> dq_hash_bits);
1819 nr_hash = 1UL << dq_hash_bits;
1820 dq_hash_mask = nr_hash - 1;
1821 for (i = 0; i < nr_hash; i++)
1822 INIT_HLIST_HEAD(dquot_hash + i);
1824 printk("Dquot-cache hash table entries: %ld (order %ld, %ld bytes)\n",
1825 nr_hash, order, (PAGE_SIZE << order));
1827 set_shrinker(DEFAULT_SEEKS, shrink_dqcache_memory);
1831 module_init(dquot_init);
1833 EXPORT_SYMBOL(register_quota_format);
1834 EXPORT_SYMBOL(unregister_quota_format);
1835 EXPORT_SYMBOL(dqstats);
1836 EXPORT_SYMBOL(dq_data_lock);
1837 EXPORT_SYMBOL(vfs_quota_on);
1838 EXPORT_SYMBOL(vfs_quota_on_mount);
1839 EXPORT_SYMBOL(vfs_quota_off);
1840 EXPORT_SYMBOL(vfs_quota_sync);
1841 EXPORT_SYMBOL(vfs_get_dqinfo);
1842 EXPORT_SYMBOL(vfs_set_dqinfo);
1843 EXPORT_SYMBOL(vfs_get_dqblk);
1844 EXPORT_SYMBOL(vfs_set_dqblk);
1845 EXPORT_SYMBOL(dquot_commit);
1846 EXPORT_SYMBOL(dquot_commit_info);
1847 EXPORT_SYMBOL(dquot_acquire);
1848 EXPORT_SYMBOL(dquot_release);
1849 EXPORT_SYMBOL(dquot_mark_dquot_dirty);
1850 EXPORT_SYMBOL(dquot_initialize);
1851 EXPORT_SYMBOL(dquot_drop);
1852 EXPORT_SYMBOL(dquot_alloc_space);
1853 EXPORT_SYMBOL(dquot_alloc_inode);
1854 EXPORT_SYMBOL(dquot_free_space);
1855 EXPORT_SYMBOL(dquot_free_inode);
1856 EXPORT_SYMBOL(dquot_transfer);