[TG3]: Set minimal hw interrupt mitigation.
[linux-2.6/verdex.git] / fs / dquot.c
blob3995ce7907ccbcfb276fa694cfa4bc98ddd8cc69
1 /*
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
28 * quota files
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
49 * New SMP locking.
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>
60 #include <linux/fs.h>
61 #include <linux/mount.h>
62 #include <linux/mm.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
104 * dqonoff_sem.
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;
138 quota_formats = fmt;
139 spin_unlock(&dq_list_lock);
140 return 0;
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 = &quota_formats; *actqf && *actqf != fmt; actqf = &(*actqf)->qf_next);
149 if (*actqf)
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)) {
161 int qm;
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))
167 return NULL;
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))
172 actqf = NULL;
174 spin_unlock(&dq_list_lock);
175 return actqf;
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)
215 unsigned long tmp;
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;
238 struct dquot *dquot;
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)
243 return dquot;
245 return NODQUOT;
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))
258 return;
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);
283 up(&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);
295 return 0;
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))
302 return 0;
303 list_del_init(&dquot->dq_dirty);
304 return 1;
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);
326 if (ret < 0)
327 goto out_iolock;
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);
335 if (ret < 0)
336 goto out_iolock;
337 if (ret2 < 0) {
338 ret = ret2;
339 goto out_iolock;
342 set_bit(DQ_ACTIVE_B, &dquot->dq_flags);
343 out_iolock:
344 up(&dqopt->dqio_sem);
345 up(&dquot->dq_lock);
346 return ret;
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);
361 goto out_sem;
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);
370 if (ret >= 0)
371 ret = ret2;
373 out_sem:
374 up(&dqopt->dqio_sem);
375 return ret;
379 * Release dquot
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)
389 goto out_dqlock;
390 down(&dqopt->dqio_sem);
391 if (dqopt->ops[dquot->dq_type]->release_dqblk) {
392 ret = dqopt->ops[dquot->dq_type]->release_dqblk(dquot);
393 /* Write the info */
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);
396 if (ret >= 0)
397 ret = ret2;
399 clear_bit(DQ_ACTIVE_B, &dquot->dq_flags);
400 up(&dqopt->dqio_sem);
401 out_dqlock:
402 up(&dquot->dq_lock);
403 return ret;
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;
413 struct list_head *head;
415 spin_lock(&dq_list_lock);
416 for (head = inuse_list.next; head != &inuse_list;) {
417 dquot = list_entry(head, struct dquot, dq_inuse);
418 head = head->next;
419 if (dquot->dq_sb != sb)
420 continue;
421 if (dquot->dq_type != type)
422 continue;
423 #ifdef __DQUOT_PARANOIA
424 if (atomic_read(&dquot->dq_count))
425 BUG();
426 #endif
427 /* Quota now has no users and it has been written on last dqput() */
428 remove_dquot_hash(dquot);
429 remove_free_dquot(dquot);
430 remove_inuse(dquot);
431 kmem_cache_free(dquot_cachep, dquot);
433 spin_unlock(&dq_list_lock);
436 int vfs_quota_sync(struct super_block *sb, int type)
438 struct list_head *dirty;
439 struct dquot *dquot;
440 struct quota_info *dqopt = sb_dqopt(sb);
441 int cnt;
443 down(&dqopt->dqonoff_sem);
444 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
445 if (type != -1 && cnt != type)
446 continue;
447 if (!sb_has_quota_enabled(sb, cnt))
448 continue;
449 spin_lock(&dq_list_lock);
450 dirty = &dqopt->info[cnt].dqi_dirty_list;
451 while (!list_empty(dirty)) {
452 dquot = list_entry(dirty->next, struct dquot, dq_dirty);
453 /* Dirty and inactive can be only bad dquot... */
454 if (!test_bit(DQ_ACTIVE_B, &dquot->dq_flags)) {
455 clear_dquot_dirty(dquot);
456 continue;
458 /* Now we have active dquot from which someone is
459 * holding reference so we can safely just increase
460 * use count */
461 atomic_inc(&dquot->dq_count);
462 dqstats.lookups++;
463 spin_unlock(&dq_list_lock);
464 sb->dq_op->write_dquot(dquot);
465 dqput(dquot);
466 spin_lock(&dq_list_lock);
468 spin_unlock(&dq_list_lock);
471 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
472 if ((cnt == type || type == -1) && sb_has_quota_enabled(sb, cnt)
473 && info_dirty(&dqopt->info[cnt]))
474 sb->dq_op->write_info(sb, cnt);
475 spin_lock(&dq_list_lock);
476 dqstats.syncs++;
477 spin_unlock(&dq_list_lock);
478 up(&dqopt->dqonoff_sem);
480 return 0;
483 /* Free unused dquots from cache */
484 static void prune_dqcache(int count)
486 struct list_head *head;
487 struct dquot *dquot;
489 head = free_dquots.prev;
490 while (head != &free_dquots && count) {
491 dquot = list_entry(head, struct dquot, dq_free);
492 remove_dquot_hash(dquot);
493 remove_free_dquot(dquot);
494 remove_inuse(dquot);
495 kmem_cache_free(dquot_cachep, dquot);
496 count--;
497 head = free_dquots.prev;
502 * This is called from kswapd when we think we need some
503 * more memory
506 static int shrink_dqcache_memory(int nr, unsigned int gfp_mask)
508 if (nr) {
509 spin_lock(&dq_list_lock);
510 prune_dqcache(nr);
511 spin_unlock(&dq_list_lock);
513 return (dqstats.free_dquots / 100) * sysctl_vfs_cache_pressure;
517 * Put reference to dquot
518 * NOTE: If you change this function please check whether dqput_blocks() works right...
519 * MUST be called with either dqptr_sem or dqonoff_sem held
521 static void dqput(struct dquot *dquot)
523 if (!dquot)
524 return;
525 #ifdef __DQUOT_PARANOIA
526 if (!atomic_read(&dquot->dq_count)) {
527 printk("VFS: dqput: trying to free free dquot\n");
528 printk("VFS: device %s, dquot of %s %d\n",
529 dquot->dq_sb->s_id,
530 quotatypes[dquot->dq_type],
531 dquot->dq_id);
532 BUG();
534 #endif
536 spin_lock(&dq_list_lock);
537 dqstats.drops++;
538 spin_unlock(&dq_list_lock);
539 we_slept:
540 spin_lock(&dq_list_lock);
541 if (atomic_read(&dquot->dq_count) > 1) {
542 /* We have more than one user... nothing to do */
543 atomic_dec(&dquot->dq_count);
544 spin_unlock(&dq_list_lock);
545 return;
547 /* Need to release dquot? */
548 if (test_bit(DQ_ACTIVE_B, &dquot->dq_flags) && dquot_dirty(dquot)) {
549 spin_unlock(&dq_list_lock);
550 /* Commit dquot before releasing */
551 dquot->dq_sb->dq_op->write_dquot(dquot);
552 goto we_slept;
554 /* Clear flag in case dquot was inactive (something bad happened) */
555 clear_dquot_dirty(dquot);
556 if (test_bit(DQ_ACTIVE_B, &dquot->dq_flags)) {
557 spin_unlock(&dq_list_lock);
558 dquot->dq_sb->dq_op->release_dquot(dquot);
559 goto we_slept;
561 atomic_dec(&dquot->dq_count);
562 #ifdef __DQUOT_PARANOIA
563 /* sanity check */
564 if (!list_empty(&dquot->dq_free))
565 BUG();
566 #endif
567 put_dquot_last(dquot);
568 spin_unlock(&dq_list_lock);
571 static struct dquot *get_empty_dquot(struct super_block *sb, int type)
573 struct dquot *dquot;
575 dquot = kmem_cache_alloc(dquot_cachep, SLAB_NOFS);
576 if(!dquot)
577 return NODQUOT;
579 memset((caddr_t)dquot, 0, sizeof(struct dquot));
580 sema_init(&dquot->dq_lock, 1);
581 INIT_LIST_HEAD(&dquot->dq_free);
582 INIT_LIST_HEAD(&dquot->dq_inuse);
583 INIT_HLIST_NODE(&dquot->dq_hash);
584 INIT_LIST_HEAD(&dquot->dq_dirty);
585 dquot->dq_sb = sb;
586 dquot->dq_type = type;
587 atomic_set(&dquot->dq_count, 1);
589 return dquot;
593 * Get reference to dquot
594 * MUST be called with either dqptr_sem or dqonoff_sem held
596 static struct dquot *dqget(struct super_block *sb, unsigned int id, int type)
598 unsigned int hashent = hashfn(sb, id, type);
599 struct dquot *dquot, *empty = NODQUOT;
601 if (!sb_has_quota_enabled(sb, type))
602 return NODQUOT;
603 we_slept:
604 spin_lock(&dq_list_lock);
605 if ((dquot = find_dquot(hashent, sb, id, type)) == NODQUOT) {
606 if (empty == NODQUOT) {
607 spin_unlock(&dq_list_lock);
608 if ((empty = get_empty_dquot(sb, type)) == NODQUOT)
609 schedule(); /* Try to wait for a moment... */
610 goto we_slept;
612 dquot = empty;
613 dquot->dq_id = id;
614 /* all dquots go on the inuse_list */
615 put_inuse(dquot);
616 /* hash it first so it can be found */
617 insert_dquot_hash(dquot);
618 dqstats.lookups++;
619 spin_unlock(&dq_list_lock);
620 } else {
621 if (!atomic_read(&dquot->dq_count))
622 remove_free_dquot(dquot);
623 atomic_inc(&dquot->dq_count);
624 dqstats.cache_hits++;
625 dqstats.lookups++;
626 spin_unlock(&dq_list_lock);
627 if (empty)
628 kmem_cache_free(dquot_cachep, empty);
630 /* Wait for dq_lock - after this we know that either dquot_release() is already
631 * finished or it will be canceled due to dq_count > 1 test */
632 wait_on_dquot(dquot);
633 /* Read the dquot and instantiate it (everything done only if needed) */
634 if (!test_bit(DQ_ACTIVE_B, &dquot->dq_flags) && sb->dq_op->acquire_dquot(dquot) < 0) {
635 dqput(dquot);
636 return NODQUOT;
638 #ifdef __DQUOT_PARANOIA
639 if (!dquot->dq_sb) /* Has somebody invalidated entry under us? */
640 BUG();
641 #endif
643 return dquot;
646 static int dqinit_needed(struct inode *inode, int type)
648 int cnt;
650 if (IS_NOQUOTA(inode))
651 return 0;
652 if (type != -1)
653 return inode->i_dquot[type] == NODQUOT;
654 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
655 if (inode->i_dquot[cnt] == NODQUOT)
656 return 1;
657 return 0;
660 /* This routine is guarded by dqonoff_sem semaphore */
661 static void add_dquot_ref(struct super_block *sb, int type)
663 struct list_head *p;
665 restart:
666 file_list_lock();
667 list_for_each(p, &sb->s_files) {
668 struct file *filp = list_entry(p, struct file, f_list);
669 struct inode *inode = filp->f_dentry->d_inode;
670 if (filp->f_mode & FMODE_WRITE && dqinit_needed(inode, type)) {
671 struct dentry *dentry = dget(filp->f_dentry);
672 file_list_unlock();
673 sb->dq_op->initialize(inode, type);
674 dput(dentry);
675 /* As we may have blocked we had better restart... */
676 goto restart;
679 file_list_unlock();
682 /* Return 0 if dqput() won't block (note that 1 doesn't necessarily mean blocking) */
683 static inline int dqput_blocks(struct dquot *dquot)
685 if (atomic_read(&dquot->dq_count) <= 1)
686 return 1;
687 return 0;
690 /* Remove references to dquots from inode - add dquot to list for freeing if needed */
691 /* We can't race with anybody because we hold dqptr_sem for writing... */
692 int remove_inode_dquot_ref(struct inode *inode, int type, struct list_head *tofree_head)
694 struct dquot *dquot = inode->i_dquot[type];
696 inode->i_dquot[type] = NODQUOT;
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));
702 #endif
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);
706 return 1;
708 else
709 dqput(dquot); /* We have guaranteed we won't block */
711 return 0;
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;
719 struct dquot *dquot;
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... */
727 dqput(dquot);
731 /* Gather all references from inodes and drop them */
732 static void drop_dquot_ref(struct super_block *sb, int type)
734 LIST_HEAD(tofree_head);
736 /* We need to be guarded against prune_icache to reach all the
737 * inodes - otherwise some can be on the local list of prune_icache */
738 down(&iprune_sem);
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 up(&iprune_sem);
743 put_dquot_list(&tofree_head);
746 static inline void dquot_incr_inodes(struct dquot *dquot, unsigned long number)
748 dquot->dq_dqb.dqb_curinodes += number;
751 static inline void dquot_incr_space(struct dquot *dquot, qsize_t number)
753 dquot->dq_dqb.dqb_curspace += number;
756 static inline void dquot_decr_inodes(struct dquot *dquot, unsigned long number)
758 if (dquot->dq_dqb.dqb_curinodes > number)
759 dquot->dq_dqb.dqb_curinodes -= number;
760 else
761 dquot->dq_dqb.dqb_curinodes = 0;
762 if (dquot->dq_dqb.dqb_curinodes <= dquot->dq_dqb.dqb_isoftlimit)
763 dquot->dq_dqb.dqb_itime = (time_t) 0;
764 clear_bit(DQ_INODES_B, &dquot->dq_flags);
767 static inline void dquot_decr_space(struct dquot *dquot, qsize_t number)
769 if (dquot->dq_dqb.dqb_curspace > number)
770 dquot->dq_dqb.dqb_curspace -= number;
771 else
772 dquot->dq_dqb.dqb_curspace = 0;
773 if (toqb(dquot->dq_dqb.dqb_curspace) <= dquot->dq_dqb.dqb_bsoftlimit)
774 dquot->dq_dqb.dqb_btime = (time_t) 0;
775 clear_bit(DQ_BLKS_B, &dquot->dq_flags);
778 static int flag_print_warnings = 1;
780 static inline int need_print_warning(struct dquot *dquot)
782 if (!flag_print_warnings)
783 return 0;
785 switch (dquot->dq_type) {
786 case USRQUOTA:
787 return current->fsuid == dquot->dq_id;
788 case GRPQUOTA:
789 return in_group_p(dquot->dq_id);
791 return 0;
794 /* Values of warnings */
795 #define NOWARN 0
796 #define IHARDWARN 1
797 #define ISOFTLONGWARN 2
798 #define ISOFTWARN 3
799 #define BHARDWARN 4
800 #define BSOFTLONGWARN 5
801 #define BSOFTWARN 6
803 /* Print warning to user which exceeded quota */
804 static void print_warning(struct dquot *dquot, const char warntype)
806 char *msg = NULL;
807 int flag = (warntype == BHARDWARN || warntype == BSOFTLONGWARN) ? DQ_BLKS_B :
808 ((warntype == IHARDWARN || warntype == ISOFTLONGWARN) ? DQ_INODES_B : 0);
810 if (!need_print_warning(dquot) || (flag && test_and_set_bit(flag, &dquot->dq_flags)))
811 return;
813 tty_write_message(current->signal->tty, dquot->dq_sb->s_id);
814 if (warntype == ISOFTWARN || warntype == BSOFTWARN)
815 tty_write_message(current->signal->tty, ": warning, ");
816 else
817 tty_write_message(current->signal->tty, ": write failed, ");
818 tty_write_message(current->signal->tty, quotatypes[dquot->dq_type]);
819 switch (warntype) {
820 case IHARDWARN:
821 msg = " file limit reached.\r\n";
822 break;
823 case ISOFTLONGWARN:
824 msg = " file quota exceeded too long.\r\n";
825 break;
826 case ISOFTWARN:
827 msg = " file quota exceeded.\r\n";
828 break;
829 case BHARDWARN:
830 msg = " block limit reached.\r\n";
831 break;
832 case BSOFTLONGWARN:
833 msg = " block quota exceeded too long.\r\n";
834 break;
835 case BSOFTWARN:
836 msg = " block quota exceeded.\r\n";
837 break;
839 tty_write_message(current->signal->tty, msg);
842 static inline void flush_warnings(struct dquot **dquots, char *warntype)
844 int i;
846 for (i = 0; i < MAXQUOTAS; i++)
847 if (dquots[i] != NODQUOT && warntype[i] != NOWARN)
848 print_warning(dquots[i], warntype[i]);
851 static inline char ignore_hardlimit(struct dquot *dquot)
853 struct mem_dqinfo *info = &sb_dqopt(dquot->dq_sb)->info[dquot->dq_type];
855 return capable(CAP_SYS_RESOURCE) &&
856 (info->dqi_format->qf_fmt_id != QFMT_VFS_OLD || !(info->dqi_flags & V1_DQF_RSQUASH));
859 /* needs dq_data_lock */
860 static int check_idq(struct dquot *dquot, ulong inodes, char *warntype)
862 *warntype = NOWARN;
863 if (inodes <= 0 || test_bit(DQ_FAKE_B, &dquot->dq_flags))
864 return QUOTA_OK;
866 if (dquot->dq_dqb.dqb_ihardlimit &&
867 (dquot->dq_dqb.dqb_curinodes + inodes) > dquot->dq_dqb.dqb_ihardlimit &&
868 !ignore_hardlimit(dquot)) {
869 *warntype = IHARDWARN;
870 return NO_QUOTA;
873 if (dquot->dq_dqb.dqb_isoftlimit &&
874 (dquot->dq_dqb.dqb_curinodes + inodes) > dquot->dq_dqb.dqb_isoftlimit &&
875 dquot->dq_dqb.dqb_itime && get_seconds() >= dquot->dq_dqb.dqb_itime &&
876 !ignore_hardlimit(dquot)) {
877 *warntype = ISOFTLONGWARN;
878 return NO_QUOTA;
881 if (dquot->dq_dqb.dqb_isoftlimit &&
882 (dquot->dq_dqb.dqb_curinodes + inodes) > dquot->dq_dqb.dqb_isoftlimit &&
883 dquot->dq_dqb.dqb_itime == 0) {
884 *warntype = ISOFTWARN;
885 dquot->dq_dqb.dqb_itime = get_seconds() + sb_dqopt(dquot->dq_sb)->info[dquot->dq_type].dqi_igrace;
888 return QUOTA_OK;
891 /* needs dq_data_lock */
892 static int check_bdq(struct dquot *dquot, qsize_t space, int prealloc, char *warntype)
894 *warntype = 0;
895 if (space <= 0 || test_bit(DQ_FAKE_B, &dquot->dq_flags))
896 return QUOTA_OK;
898 if (dquot->dq_dqb.dqb_bhardlimit &&
899 toqb(dquot->dq_dqb.dqb_curspace + space) > dquot->dq_dqb.dqb_bhardlimit &&
900 !ignore_hardlimit(dquot)) {
901 if (!prealloc)
902 *warntype = BHARDWARN;
903 return NO_QUOTA;
906 if (dquot->dq_dqb.dqb_bsoftlimit &&
907 toqb(dquot->dq_dqb.dqb_curspace + space) > dquot->dq_dqb.dqb_bsoftlimit &&
908 dquot->dq_dqb.dqb_btime && get_seconds() >= dquot->dq_dqb.dqb_btime &&
909 !ignore_hardlimit(dquot)) {
910 if (!prealloc)
911 *warntype = BSOFTLONGWARN;
912 return NO_QUOTA;
915 if (dquot->dq_dqb.dqb_bsoftlimit &&
916 toqb(dquot->dq_dqb.dqb_curspace + space) > dquot->dq_dqb.dqb_bsoftlimit &&
917 dquot->dq_dqb.dqb_btime == 0) {
918 if (!prealloc) {
919 *warntype = BSOFTWARN;
920 dquot->dq_dqb.dqb_btime = get_seconds() + sb_dqopt(dquot->dq_sb)->info[dquot->dq_type].dqi_bgrace;
922 else
924 * We don't allow preallocation to exceed softlimit so exceeding will
925 * be always printed
927 return NO_QUOTA;
930 return QUOTA_OK;
934 * Initialize quota pointers in inode
935 * Transaction must be started at entry
937 int dquot_initialize(struct inode *inode, int type)
939 unsigned int id = 0;
940 int cnt, ret = 0;
942 /* First test before acquiring semaphore - solves deadlocks when we
943 * re-enter the quota code and are already holding the semaphore */
944 if (IS_NOQUOTA(inode))
945 return 0;
946 down_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
947 /* Having dqptr_sem we know NOQUOTA flags can't be altered... */
948 if (IS_NOQUOTA(inode))
949 goto out_err;
950 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
951 if (type != -1 && cnt != type)
952 continue;
953 if (inode->i_dquot[cnt] == NODQUOT) {
954 switch (cnt) {
955 case USRQUOTA:
956 id = inode->i_uid;
957 break;
958 case GRPQUOTA:
959 id = inode->i_gid;
960 break;
962 inode->i_dquot[cnt] = dqget(inode->i_sb, id, cnt);
965 out_err:
966 up_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
967 return ret;
971 * Release all quotas referenced by inode
972 * Transaction must be started at an entry
974 int dquot_drop(struct inode *inode)
976 int cnt;
978 down_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
979 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
980 if (inode->i_dquot[cnt] != NODQUOT) {
981 dqput(inode->i_dquot[cnt]);
982 inode->i_dquot[cnt] = NODQUOT;
985 up_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
986 return 0;
990 * Following four functions update i_blocks+i_bytes fields and
991 * quota information (together with appropriate checks)
992 * NOTE: We absolutely rely on the fact that caller dirties
993 * the inode (usually macros in quotaops.h care about this) and
994 * holds a handle for the current transaction so that dquot write and
995 * inode write go into the same transaction.
999 * This operation can block, but only after everything is updated
1001 int dquot_alloc_space(struct inode *inode, qsize_t number, int warn)
1003 int cnt, ret = NO_QUOTA;
1004 char warntype[MAXQUOTAS];
1006 /* First test before acquiring semaphore - solves deadlocks when we
1007 * re-enter the quota code and are already holding the semaphore */
1008 if (IS_NOQUOTA(inode)) {
1009 out_add:
1010 inode_add_bytes(inode, number);
1011 return QUOTA_OK;
1013 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1014 warntype[cnt] = NOWARN;
1016 down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1017 if (IS_NOQUOTA(inode)) { /* Now we can do reliable test... */
1018 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1019 goto out_add;
1021 spin_lock(&dq_data_lock);
1022 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1023 if (inode->i_dquot[cnt] == NODQUOT)
1024 continue;
1025 if (check_bdq(inode->i_dquot[cnt], number, warn, warntype+cnt) == NO_QUOTA)
1026 goto warn_put_all;
1028 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1029 if (inode->i_dquot[cnt] == NODQUOT)
1030 continue;
1031 dquot_incr_space(inode->i_dquot[cnt], number);
1033 inode_add_bytes(inode, number);
1034 ret = QUOTA_OK;
1035 warn_put_all:
1036 spin_unlock(&dq_data_lock);
1037 if (ret == QUOTA_OK)
1038 /* Dirtify all the dquots - this can block when journalling */
1039 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1040 if (inode->i_dquot[cnt])
1041 mark_dquot_dirty(inode->i_dquot[cnt]);
1042 flush_warnings(inode->i_dquot, warntype);
1043 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1044 return ret;
1048 * This operation can block, but only after everything is updated
1050 int dquot_alloc_inode(const struct inode *inode, unsigned long number)
1052 int cnt, ret = NO_QUOTA;
1053 char warntype[MAXQUOTAS];
1055 /* First test before acquiring semaphore - solves deadlocks when we
1056 * re-enter the quota code and are already holding the semaphore */
1057 if (IS_NOQUOTA(inode))
1058 return QUOTA_OK;
1059 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1060 warntype[cnt] = NOWARN;
1061 down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1062 if (IS_NOQUOTA(inode)) {
1063 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1064 return QUOTA_OK;
1066 spin_lock(&dq_data_lock);
1067 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1068 if (inode->i_dquot[cnt] == NODQUOT)
1069 continue;
1070 if (check_idq(inode->i_dquot[cnt], number, warntype+cnt) == NO_QUOTA)
1071 goto warn_put_all;
1074 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1075 if (inode->i_dquot[cnt] == NODQUOT)
1076 continue;
1077 dquot_incr_inodes(inode->i_dquot[cnt], number);
1079 ret = QUOTA_OK;
1080 warn_put_all:
1081 spin_unlock(&dq_data_lock);
1082 if (ret == QUOTA_OK)
1083 /* Dirtify all the dquots - this can block when journalling */
1084 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1085 if (inode->i_dquot[cnt])
1086 mark_dquot_dirty(inode->i_dquot[cnt]);
1087 flush_warnings((struct dquot **)inode->i_dquot, warntype);
1088 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1089 return ret;
1093 * This operation can block, but only after everything is updated
1095 int dquot_free_space(struct inode *inode, qsize_t number)
1097 unsigned int cnt;
1099 /* First test before acquiring semaphore - solves deadlocks when we
1100 * re-enter the quota code and are already holding the semaphore */
1101 if (IS_NOQUOTA(inode)) {
1102 out_sub:
1103 inode_sub_bytes(inode, number);
1104 return QUOTA_OK;
1106 down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1107 /* Now recheck reliably when holding dqptr_sem */
1108 if (IS_NOQUOTA(inode)) {
1109 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1110 goto out_sub;
1112 spin_lock(&dq_data_lock);
1113 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1114 if (inode->i_dquot[cnt] == NODQUOT)
1115 continue;
1116 dquot_decr_space(inode->i_dquot[cnt], number);
1118 inode_sub_bytes(inode, number);
1119 spin_unlock(&dq_data_lock);
1120 /* Dirtify all the dquots - this can block when journalling */
1121 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1122 if (inode->i_dquot[cnt])
1123 mark_dquot_dirty(inode->i_dquot[cnt]);
1124 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1125 return QUOTA_OK;
1129 * This operation can block, but only after everything is updated
1131 int dquot_free_inode(const struct inode *inode, unsigned long number)
1133 unsigned int cnt;
1135 /* First test before acquiring semaphore - solves deadlocks when we
1136 * re-enter the quota code and are already holding the semaphore */
1137 if (IS_NOQUOTA(inode))
1138 return QUOTA_OK;
1139 down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1140 /* Now recheck reliably when holding dqptr_sem */
1141 if (IS_NOQUOTA(inode)) {
1142 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1143 return QUOTA_OK;
1145 spin_lock(&dq_data_lock);
1146 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1147 if (inode->i_dquot[cnt] == NODQUOT)
1148 continue;
1149 dquot_decr_inodes(inode->i_dquot[cnt], number);
1151 spin_unlock(&dq_data_lock);
1152 /* Dirtify all the dquots - this can block when journalling */
1153 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1154 if (inode->i_dquot[cnt])
1155 mark_dquot_dirty(inode->i_dquot[cnt]);
1156 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1157 return QUOTA_OK;
1161 * Transfer the number of inode and blocks from one diskquota to an other.
1163 * This operation can block, but only after everything is updated
1164 * A transaction must be started when entering this function.
1166 int dquot_transfer(struct inode *inode, struct iattr *iattr)
1168 qsize_t space;
1169 struct dquot *transfer_from[MAXQUOTAS];
1170 struct dquot *transfer_to[MAXQUOTAS];
1171 int cnt, ret = NO_QUOTA, chuid = (iattr->ia_valid & ATTR_UID) && inode->i_uid != iattr->ia_uid,
1172 chgid = (iattr->ia_valid & ATTR_GID) && inode->i_gid != iattr->ia_gid;
1173 char warntype[MAXQUOTAS];
1175 /* First test before acquiring semaphore - solves deadlocks when we
1176 * re-enter the quota code and are already holding the semaphore */
1177 if (IS_NOQUOTA(inode))
1178 return QUOTA_OK;
1179 /* Clear the arrays */
1180 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1181 transfer_to[cnt] = transfer_from[cnt] = NODQUOT;
1182 warntype[cnt] = NOWARN;
1184 down_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
1185 /* Now recheck reliably when holding dqptr_sem */
1186 if (IS_NOQUOTA(inode)) { /* File without quota accounting? */
1187 up_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
1188 return QUOTA_OK;
1190 /* First build the transfer_to list - here we can block on
1191 * reading/instantiating of dquots. We know that the transaction for
1192 * us was already started so we don't violate lock ranking here */
1193 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1194 switch (cnt) {
1195 case USRQUOTA:
1196 if (!chuid)
1197 continue;
1198 transfer_to[cnt] = dqget(inode->i_sb, iattr->ia_uid, cnt);
1199 break;
1200 case GRPQUOTA:
1201 if (!chgid)
1202 continue;
1203 transfer_to[cnt] = dqget(inode->i_sb, iattr->ia_gid, cnt);
1204 break;
1207 spin_lock(&dq_data_lock);
1208 space = inode_get_bytes(inode);
1209 /* Build the transfer_from list and check the limits */
1210 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1211 if (transfer_to[cnt] == NODQUOT)
1212 continue;
1213 transfer_from[cnt] = inode->i_dquot[cnt];
1214 if (check_idq(transfer_to[cnt], 1, warntype+cnt) == NO_QUOTA ||
1215 check_bdq(transfer_to[cnt], space, 0, warntype+cnt) == NO_QUOTA)
1216 goto warn_put_all;
1220 * Finally perform the needed transfer from transfer_from to transfer_to
1222 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1224 * Skip changes for same uid or gid or for turned off quota-type.
1226 if (transfer_to[cnt] == NODQUOT)
1227 continue;
1229 /* Due to IO error we might not have transfer_from[] structure */
1230 if (transfer_from[cnt]) {
1231 dquot_decr_inodes(transfer_from[cnt], 1);
1232 dquot_decr_space(transfer_from[cnt], space);
1235 dquot_incr_inodes(transfer_to[cnt], 1);
1236 dquot_incr_space(transfer_to[cnt], space);
1238 inode->i_dquot[cnt] = transfer_to[cnt];
1240 ret = QUOTA_OK;
1241 warn_put_all:
1242 spin_unlock(&dq_data_lock);
1243 /* Dirtify all the dquots - this can block when journalling */
1244 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1245 if (transfer_from[cnt])
1246 mark_dquot_dirty(transfer_from[cnt]);
1247 if (transfer_to[cnt])
1248 mark_dquot_dirty(transfer_to[cnt]);
1250 flush_warnings(transfer_to, warntype);
1252 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1253 if (ret == QUOTA_OK && transfer_from[cnt] != NODQUOT)
1254 dqput(transfer_from[cnt]);
1255 if (ret == NO_QUOTA && transfer_to[cnt] != NODQUOT)
1256 dqput(transfer_to[cnt]);
1258 up_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
1259 return ret;
1263 * Write info of quota file to disk
1265 int dquot_commit_info(struct super_block *sb, int type)
1267 int ret;
1268 struct quota_info *dqopt = sb_dqopt(sb);
1270 down(&dqopt->dqio_sem);
1271 ret = dqopt->ops[type]->write_file_info(sb, type);
1272 up(&dqopt->dqio_sem);
1273 return ret;
1277 * Definitions of diskquota operations.
1279 struct dquot_operations dquot_operations = {
1280 .initialize = dquot_initialize,
1281 .drop = dquot_drop,
1282 .alloc_space = dquot_alloc_space,
1283 .alloc_inode = dquot_alloc_inode,
1284 .free_space = dquot_free_space,
1285 .free_inode = dquot_free_inode,
1286 .transfer = dquot_transfer,
1287 .write_dquot = dquot_commit,
1288 .acquire_dquot = dquot_acquire,
1289 .release_dquot = dquot_release,
1290 .mark_dirty = dquot_mark_dquot_dirty,
1291 .write_info = dquot_commit_info
1294 static inline void set_enable_flags(struct quota_info *dqopt, int type)
1296 switch (type) {
1297 case USRQUOTA:
1298 dqopt->flags |= DQUOT_USR_ENABLED;
1299 break;
1300 case GRPQUOTA:
1301 dqopt->flags |= DQUOT_GRP_ENABLED;
1302 break;
1306 static inline void reset_enable_flags(struct quota_info *dqopt, int type)
1308 switch (type) {
1309 case USRQUOTA:
1310 dqopt->flags &= ~DQUOT_USR_ENABLED;
1311 break;
1312 case GRPQUOTA:
1313 dqopt->flags &= ~DQUOT_GRP_ENABLED;
1314 break;
1319 * Turn quota off on a device. type == -1 ==> quotaoff for all types (umount)
1321 int vfs_quota_off(struct super_block *sb, int type)
1323 int cnt;
1324 struct quota_info *dqopt = sb_dqopt(sb);
1325 struct inode *toputinode[MAXQUOTAS];
1326 struct vfsmount *toputmnt[MAXQUOTAS];
1328 /* We need to serialize quota_off() for device */
1329 down(&dqopt->dqonoff_sem);
1330 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1331 toputinode[cnt] = NULL;
1332 toputmnt[cnt] = NULL;
1333 if (type != -1 && cnt != type)
1334 continue;
1335 if (!sb_has_quota_enabled(sb, cnt))
1336 continue;
1337 reset_enable_flags(dqopt, cnt);
1339 /* Note: these are blocking operations */
1340 drop_dquot_ref(sb, cnt);
1341 invalidate_dquots(sb, cnt);
1343 * Now all dquots should be invalidated, all writes done so we should be only
1344 * users of the info. No locks needed.
1346 if (info_dirty(&dqopt->info[cnt]))
1347 sb->dq_op->write_info(sb, cnt);
1348 if (dqopt->ops[cnt]->free_file_info)
1349 dqopt->ops[cnt]->free_file_info(sb, cnt);
1350 put_quota_format(dqopt->info[cnt].dqi_format);
1352 toputinode[cnt] = dqopt->files[cnt];
1353 toputmnt[cnt] = dqopt->mnt[cnt];
1354 dqopt->files[cnt] = NULL;
1355 dqopt->mnt[cnt] = NULL;
1356 dqopt->info[cnt].dqi_flags = 0;
1357 dqopt->info[cnt].dqi_igrace = 0;
1358 dqopt->info[cnt].dqi_bgrace = 0;
1359 dqopt->ops[cnt] = NULL;
1361 up(&dqopt->dqonoff_sem);
1362 /* Sync the superblock so that buffers with quota data are written to
1363 * disk (and so userspace sees correct data afterwards).
1364 * The reference to vfsmnt we are still holding protects us from
1365 * umount (we don't have it only when quotas are turned on/off for
1366 * journal replay but in that case we are guarded by the fs anyway). */
1367 if (sb->s_op->sync_fs)
1368 sb->s_op->sync_fs(sb, 1);
1369 sync_blockdev(sb->s_bdev);
1370 /* Now the quota files are just ordinary files and we can set the
1371 * inode flags back. Moreover we discard the pagecache so that
1372 * userspace sees the writes we did bypassing the pagecache. We
1373 * must also discard the blockdev buffers so that we see the
1374 * changes done by userspace on the next quotaon() */
1375 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1376 if (toputinode[cnt]) {
1377 down(&dqopt->dqonoff_sem);
1378 /* If quota was reenabled in the meantime, we have
1379 * nothing to do */
1380 if (!sb_has_quota_enabled(sb, cnt)) {
1381 down(&toputinode[cnt]->i_sem);
1382 toputinode[cnt]->i_flags &= ~(S_IMMUTABLE |
1383 S_NOATIME | S_NOQUOTA);
1384 truncate_inode_pages(&toputinode[cnt]->i_data, 0);
1385 up(&toputinode[cnt]->i_sem);
1386 mark_inode_dirty(toputinode[cnt]);
1387 iput(toputinode[cnt]);
1389 up(&dqopt->dqonoff_sem);
1390 /* We don't hold the reference when we turned on quotas
1391 * just for the journal replay... */
1392 if (toputmnt[cnt])
1393 mntput(toputmnt[cnt]);
1395 if (sb->s_bdev)
1396 invalidate_bdev(sb->s_bdev, 0);
1397 return 0;
1401 * Turn quotas on on a device
1404 /* Helper function when we already have the inode */
1405 static int vfs_quota_on_inode(struct inode *inode, int type, int format_id)
1407 struct quota_format_type *fmt = find_quota_format(format_id);
1408 struct super_block *sb = inode->i_sb;
1409 struct quota_info *dqopt = sb_dqopt(sb);
1410 int error;
1411 int oldflags = -1;
1413 if (!fmt)
1414 return -ESRCH;
1415 if (!S_ISREG(inode->i_mode)) {
1416 error = -EACCES;
1417 goto out_fmt;
1419 if (IS_RDONLY(inode)) {
1420 error = -EROFS;
1421 goto out_fmt;
1423 if (!sb->s_op->quota_write || !sb->s_op->quota_read) {
1424 error = -EINVAL;
1425 goto out_fmt;
1428 /* As we bypass the pagecache we must now flush the inode so that
1429 * we see all the changes from userspace... */
1430 write_inode_now(inode, 1);
1431 /* And now flush the block cache so that kernel sees the changes */
1432 invalidate_bdev(sb->s_bdev, 0);
1433 down(&inode->i_sem);
1434 down(&dqopt->dqonoff_sem);
1435 if (sb_has_quota_enabled(sb, type)) {
1436 error = -EBUSY;
1437 goto out_lock;
1439 /* We don't want quota and atime on quota files (deadlocks possible)
1440 * Also nobody should write to the file - we use special IO operations
1441 * which ignore the immutable bit. */
1442 down_write(&dqopt->dqptr_sem);
1443 oldflags = inode->i_flags & (S_NOATIME | S_IMMUTABLE | S_NOQUOTA);
1444 inode->i_flags |= S_NOQUOTA | S_NOATIME | S_IMMUTABLE;
1445 up_write(&dqopt->dqptr_sem);
1446 sb->dq_op->drop(inode);
1448 error = -EIO;
1449 dqopt->files[type] = igrab(inode);
1450 if (!dqopt->files[type])
1451 goto out_lock;
1452 error = -EINVAL;
1453 if (!fmt->qf_ops->check_quota_file(sb, type))
1454 goto out_file_init;
1456 dqopt->ops[type] = fmt->qf_ops;
1457 dqopt->info[type].dqi_format = fmt;
1458 INIT_LIST_HEAD(&dqopt->info[type].dqi_dirty_list);
1459 down(&dqopt->dqio_sem);
1460 if ((error = dqopt->ops[type]->read_file_info(sb, type)) < 0) {
1461 up(&dqopt->dqio_sem);
1462 goto out_file_init;
1464 up(&dqopt->dqio_sem);
1465 up(&inode->i_sem);
1466 set_enable_flags(dqopt, type);
1468 add_dquot_ref(sb, type);
1469 up(&dqopt->dqonoff_sem);
1471 return 0;
1473 out_file_init:
1474 dqopt->files[type] = NULL;
1475 iput(inode);
1476 out_lock:
1477 up(&dqopt->dqonoff_sem);
1478 if (oldflags != -1) {
1479 down_write(&dqopt->dqptr_sem);
1480 /* Set the flags back (in the case of accidental quotaon()
1481 * on a wrong file we don't want to mess up the flags) */
1482 inode->i_flags &= ~(S_NOATIME | S_NOQUOTA | S_IMMUTABLE);
1483 inode->i_flags |= oldflags;
1484 up_write(&dqopt->dqptr_sem);
1486 up(&inode->i_sem);
1487 out_fmt:
1488 put_quota_format(fmt);
1490 return error;
1493 /* Actual function called from quotactl() */
1494 int vfs_quota_on(struct super_block *sb, int type, int format_id, char *path)
1496 struct nameidata nd;
1497 int error;
1499 error = path_lookup(path, LOOKUP_FOLLOW, &nd);
1500 if (error < 0)
1501 return error;
1502 error = security_quota_on(nd.dentry);
1503 if (error)
1504 goto out_path;
1505 /* Quota file not on the same filesystem? */
1506 if (nd.mnt->mnt_sb != sb)
1507 error = -EXDEV;
1508 else {
1509 error = vfs_quota_on_inode(nd.dentry->d_inode, type, format_id);
1510 if (!error)
1511 sb_dqopt(sb)->mnt[type] = mntget(nd.mnt);
1513 out_path:
1514 path_release(&nd);
1515 return error;
1519 * This function is used when filesystem needs to initialize quotas
1520 * during mount time.
1522 int vfs_quota_on_mount(int type, int format_id, struct dentry *dentry)
1524 int error;
1526 error = security_quota_on(dentry);
1527 if (error)
1528 return error;
1529 return vfs_quota_on_inode(dentry->d_inode, type, format_id);
1532 /* Generic routine for getting common part of quota structure */
1533 static void do_get_dqblk(struct dquot *dquot, struct if_dqblk *di)
1535 struct mem_dqblk *dm = &dquot->dq_dqb;
1537 spin_lock(&dq_data_lock);
1538 di->dqb_bhardlimit = dm->dqb_bhardlimit;
1539 di->dqb_bsoftlimit = dm->dqb_bsoftlimit;
1540 di->dqb_curspace = dm->dqb_curspace;
1541 di->dqb_ihardlimit = dm->dqb_ihardlimit;
1542 di->dqb_isoftlimit = dm->dqb_isoftlimit;
1543 di->dqb_curinodes = dm->dqb_curinodes;
1544 di->dqb_btime = dm->dqb_btime;
1545 di->dqb_itime = dm->dqb_itime;
1546 di->dqb_valid = QIF_ALL;
1547 spin_unlock(&dq_data_lock);
1550 int vfs_get_dqblk(struct super_block *sb, int type, qid_t id, struct if_dqblk *di)
1552 struct dquot *dquot;
1554 down(&sb_dqopt(sb)->dqonoff_sem);
1555 if (!(dquot = dqget(sb, id, type))) {
1556 up(&sb_dqopt(sb)->dqonoff_sem);
1557 return -ESRCH;
1559 do_get_dqblk(dquot, di);
1560 dqput(dquot);
1561 up(&sb_dqopt(sb)->dqonoff_sem);
1562 return 0;
1565 /* Generic routine for setting common part of quota structure */
1566 static void do_set_dqblk(struct dquot *dquot, struct if_dqblk *di)
1568 struct mem_dqblk *dm = &dquot->dq_dqb;
1569 int check_blim = 0, check_ilim = 0;
1571 spin_lock(&dq_data_lock);
1572 if (di->dqb_valid & QIF_SPACE) {
1573 dm->dqb_curspace = di->dqb_curspace;
1574 check_blim = 1;
1576 if (di->dqb_valid & QIF_BLIMITS) {
1577 dm->dqb_bsoftlimit = di->dqb_bsoftlimit;
1578 dm->dqb_bhardlimit = di->dqb_bhardlimit;
1579 check_blim = 1;
1581 if (di->dqb_valid & QIF_INODES) {
1582 dm->dqb_curinodes = di->dqb_curinodes;
1583 check_ilim = 1;
1585 if (di->dqb_valid & QIF_ILIMITS) {
1586 dm->dqb_isoftlimit = di->dqb_isoftlimit;
1587 dm->dqb_ihardlimit = di->dqb_ihardlimit;
1588 check_ilim = 1;
1590 if (di->dqb_valid & QIF_BTIME)
1591 dm->dqb_btime = di->dqb_btime;
1592 if (di->dqb_valid & QIF_ITIME)
1593 dm->dqb_itime = di->dqb_itime;
1595 if (check_blim) {
1596 if (!dm->dqb_bsoftlimit || toqb(dm->dqb_curspace) < dm->dqb_bsoftlimit) {
1597 dm->dqb_btime = 0;
1598 clear_bit(DQ_BLKS_B, &dquot->dq_flags);
1600 else if (!(di->dqb_valid & QIF_BTIME)) /* Set grace only if user hasn't provided his own... */
1601 dm->dqb_btime = get_seconds() + sb_dqopt(dquot->dq_sb)->info[dquot->dq_type].dqi_bgrace;
1603 if (check_ilim) {
1604 if (!dm->dqb_isoftlimit || dm->dqb_curinodes < dm->dqb_isoftlimit) {
1605 dm->dqb_itime = 0;
1606 clear_bit(DQ_INODES_B, &dquot->dq_flags);
1608 else if (!(di->dqb_valid & QIF_ITIME)) /* Set grace only if user hasn't provided his own... */
1609 dm->dqb_itime = get_seconds() + sb_dqopt(dquot->dq_sb)->info[dquot->dq_type].dqi_igrace;
1611 if (dm->dqb_bhardlimit || dm->dqb_bsoftlimit || dm->dqb_ihardlimit || dm->dqb_isoftlimit)
1612 clear_bit(DQ_FAKE_B, &dquot->dq_flags);
1613 else
1614 set_bit(DQ_FAKE_B, &dquot->dq_flags);
1615 spin_unlock(&dq_data_lock);
1616 mark_dquot_dirty(dquot);
1619 int vfs_set_dqblk(struct super_block *sb, int type, qid_t id, struct if_dqblk *di)
1621 struct dquot *dquot;
1623 down(&sb_dqopt(sb)->dqonoff_sem);
1624 if (!(dquot = dqget(sb, id, type))) {
1625 up(&sb_dqopt(sb)->dqonoff_sem);
1626 return -ESRCH;
1628 do_set_dqblk(dquot, di);
1629 dqput(dquot);
1630 up(&sb_dqopt(sb)->dqonoff_sem);
1631 return 0;
1634 /* Generic routine for getting common part of quota file information */
1635 int vfs_get_dqinfo(struct super_block *sb, int type, struct if_dqinfo *ii)
1637 struct mem_dqinfo *mi;
1639 down(&sb_dqopt(sb)->dqonoff_sem);
1640 if (!sb_has_quota_enabled(sb, type)) {
1641 up(&sb_dqopt(sb)->dqonoff_sem);
1642 return -ESRCH;
1644 mi = sb_dqopt(sb)->info + type;
1645 spin_lock(&dq_data_lock);
1646 ii->dqi_bgrace = mi->dqi_bgrace;
1647 ii->dqi_igrace = mi->dqi_igrace;
1648 ii->dqi_flags = mi->dqi_flags & DQF_MASK;
1649 ii->dqi_valid = IIF_ALL;
1650 spin_unlock(&dq_data_lock);
1651 up(&sb_dqopt(sb)->dqonoff_sem);
1652 return 0;
1655 /* Generic routine for setting common part of quota file information */
1656 int vfs_set_dqinfo(struct super_block *sb, int type, struct if_dqinfo *ii)
1658 struct mem_dqinfo *mi;
1660 down(&sb_dqopt(sb)->dqonoff_sem);
1661 if (!sb_has_quota_enabled(sb, type)) {
1662 up(&sb_dqopt(sb)->dqonoff_sem);
1663 return -ESRCH;
1665 mi = sb_dqopt(sb)->info + type;
1666 spin_lock(&dq_data_lock);
1667 if (ii->dqi_valid & IIF_BGRACE)
1668 mi->dqi_bgrace = ii->dqi_bgrace;
1669 if (ii->dqi_valid & IIF_IGRACE)
1670 mi->dqi_igrace = ii->dqi_igrace;
1671 if (ii->dqi_valid & IIF_FLAGS)
1672 mi->dqi_flags = (mi->dqi_flags & ~DQF_MASK) | (ii->dqi_flags & DQF_MASK);
1673 spin_unlock(&dq_data_lock);
1674 mark_info_dirty(sb, type);
1675 /* Force write to disk */
1676 sb->dq_op->write_info(sb, type);
1677 up(&sb_dqopt(sb)->dqonoff_sem);
1678 return 0;
1681 struct quotactl_ops vfs_quotactl_ops = {
1682 .quota_on = vfs_quota_on,
1683 .quota_off = vfs_quota_off,
1684 .quota_sync = vfs_quota_sync,
1685 .get_info = vfs_get_dqinfo,
1686 .set_info = vfs_set_dqinfo,
1687 .get_dqblk = vfs_get_dqblk,
1688 .set_dqblk = vfs_set_dqblk
1691 static ctl_table fs_dqstats_table[] = {
1693 .ctl_name = FS_DQ_LOOKUPS,
1694 .procname = "lookups",
1695 .data = &dqstats.lookups,
1696 .maxlen = sizeof(int),
1697 .mode = 0444,
1698 .proc_handler = &proc_dointvec,
1701 .ctl_name = FS_DQ_DROPS,
1702 .procname = "drops",
1703 .data = &dqstats.drops,
1704 .maxlen = sizeof(int),
1705 .mode = 0444,
1706 .proc_handler = &proc_dointvec,
1709 .ctl_name = FS_DQ_READS,
1710 .procname = "reads",
1711 .data = &dqstats.reads,
1712 .maxlen = sizeof(int),
1713 .mode = 0444,
1714 .proc_handler = &proc_dointvec,
1717 .ctl_name = FS_DQ_WRITES,
1718 .procname = "writes",
1719 .data = &dqstats.writes,
1720 .maxlen = sizeof(int),
1721 .mode = 0444,
1722 .proc_handler = &proc_dointvec,
1725 .ctl_name = FS_DQ_CACHE_HITS,
1726 .procname = "cache_hits",
1727 .data = &dqstats.cache_hits,
1728 .maxlen = sizeof(int),
1729 .mode = 0444,
1730 .proc_handler = &proc_dointvec,
1733 .ctl_name = FS_DQ_ALLOCATED,
1734 .procname = "allocated_dquots",
1735 .data = &dqstats.allocated_dquots,
1736 .maxlen = sizeof(int),
1737 .mode = 0444,
1738 .proc_handler = &proc_dointvec,
1741 .ctl_name = FS_DQ_FREE,
1742 .procname = "free_dquots",
1743 .data = &dqstats.free_dquots,
1744 .maxlen = sizeof(int),
1745 .mode = 0444,
1746 .proc_handler = &proc_dointvec,
1749 .ctl_name = FS_DQ_SYNCS,
1750 .procname = "syncs",
1751 .data = &dqstats.syncs,
1752 .maxlen = sizeof(int),
1753 .mode = 0444,
1754 .proc_handler = &proc_dointvec,
1757 .ctl_name = FS_DQ_WARNINGS,
1758 .procname = "warnings",
1759 .data = &flag_print_warnings,
1760 .maxlen = sizeof(int),
1761 .mode = 0644,
1762 .proc_handler = &proc_dointvec,
1764 { .ctl_name = 0 },
1767 static ctl_table fs_table[] = {
1769 .ctl_name = FS_DQSTATS,
1770 .procname = "quota",
1771 .mode = 0555,
1772 .child = fs_dqstats_table,
1774 { .ctl_name = 0 },
1777 static ctl_table sys_table[] = {
1779 .ctl_name = CTL_FS,
1780 .procname = "fs",
1781 .mode = 0555,
1782 .child = fs_table,
1784 { .ctl_name = 0 },
1787 static int __init dquot_init(void)
1789 int i;
1790 unsigned long nr_hash, order;
1792 printk(KERN_NOTICE "VFS: Disk quotas %s\n", __DQUOT_VERSION__);
1794 register_sysctl_table(sys_table, 0);
1796 dquot_cachep = kmem_cache_create("dquot",
1797 sizeof(struct dquot), sizeof(unsigned long) * 4,
1798 SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT|SLAB_PANIC,
1799 NULL, NULL);
1801 order = 0;
1802 dquot_hash = (struct hlist_head *)__get_free_pages(GFP_ATOMIC, order);
1803 if (!dquot_hash)
1804 panic("Cannot create dquot hash table");
1806 /* Find power-of-two hlist_heads which can fit into allocation */
1807 nr_hash = (1UL << order) * PAGE_SIZE / sizeof(struct hlist_head);
1808 dq_hash_bits = 0;
1809 do {
1810 dq_hash_bits++;
1811 } while (nr_hash >> dq_hash_bits);
1812 dq_hash_bits--;
1814 nr_hash = 1UL << dq_hash_bits;
1815 dq_hash_mask = nr_hash - 1;
1816 for (i = 0; i < nr_hash; i++)
1817 INIT_HLIST_HEAD(dquot_hash + i);
1819 printk("Dquot-cache hash table entries: %ld (order %ld, %ld bytes)\n",
1820 nr_hash, order, (PAGE_SIZE << order));
1822 set_shrinker(DEFAULT_SEEKS, shrink_dqcache_memory);
1824 return 0;
1826 module_init(dquot_init);
1828 EXPORT_SYMBOL(register_quota_format);
1829 EXPORT_SYMBOL(unregister_quota_format);
1830 EXPORT_SYMBOL(dqstats);
1831 EXPORT_SYMBOL(dq_data_lock);
1832 EXPORT_SYMBOL(vfs_quota_on);
1833 EXPORT_SYMBOL(vfs_quota_on_mount);
1834 EXPORT_SYMBOL(vfs_quota_off);
1835 EXPORT_SYMBOL(vfs_quota_sync);
1836 EXPORT_SYMBOL(vfs_get_dqinfo);
1837 EXPORT_SYMBOL(vfs_set_dqinfo);
1838 EXPORT_SYMBOL(vfs_get_dqblk);
1839 EXPORT_SYMBOL(vfs_set_dqblk);
1840 EXPORT_SYMBOL(dquot_commit);
1841 EXPORT_SYMBOL(dquot_commit_info);
1842 EXPORT_SYMBOL(dquot_acquire);
1843 EXPORT_SYMBOL(dquot_release);
1844 EXPORT_SYMBOL(dquot_mark_dquot_dirty);
1845 EXPORT_SYMBOL(dquot_initialize);
1846 EXPORT_SYMBOL(dquot_drop);
1847 EXPORT_SYMBOL(dquot_alloc_space);
1848 EXPORT_SYMBOL(dquot_alloc_inode);
1849 EXPORT_SYMBOL(dquot_free_space);
1850 EXPORT_SYMBOL(dquot_free_inode);
1851 EXPORT_SYMBOL(dquot_transfer);