kbuild: Add target debug_kallsyms
[linux-2.6/mini2440.git] / fs / dquot.c
blobb9732335bcdcd6006733a9561e9ef8ceb8769117
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, *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)
417 continue;
418 if (dquot->dq_type != type)
419 continue;
420 #ifdef __DQUOT_PARANOIA
421 if (atomic_read(&dquot->dq_count))
422 BUG();
423 #endif
424 /* Quota now has no users and it has been written on last dqput() */
425 remove_dquot_hash(dquot);
426 remove_free_dquot(dquot);
427 remove_inuse(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;
436 struct dquot *dquot;
437 struct quota_info *dqopt = sb_dqopt(sb);
438 int cnt;
440 down(&dqopt->dqonoff_sem);
441 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
442 if (type != -1 && cnt != type)
443 continue;
444 if (!sb_has_quota_enabled(sb, cnt))
445 continue;
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);
453 continue;
455 /* Now we have active dquot from which someone is
456 * holding reference so we can safely just increase
457 * use count */
458 atomic_inc(&dquot->dq_count);
459 dqstats.lookups++;
460 spin_unlock(&dq_list_lock);
461 sb->dq_op->write_dquot(dquot);
462 dqput(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);
473 dqstats.syncs++;
474 spin_unlock(&dq_list_lock);
475 up(&dqopt->dqonoff_sem);
477 return 0;
480 /* Free unused dquots from cache */
481 static void prune_dqcache(int count)
483 struct list_head *head;
484 struct dquot *dquot;
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);
491 remove_inuse(dquot);
492 kmem_cache_free(dquot_cachep, dquot);
493 count--;
494 head = free_dquots.prev;
499 * This is called from kswapd when we think we need some
500 * more memory
503 static int shrink_dqcache_memory(int nr, unsigned int gfp_mask)
505 if (nr) {
506 spin_lock(&dq_list_lock);
507 prune_dqcache(nr);
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)
520 if (!dquot)
521 return;
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",
526 dquot->dq_sb->s_id,
527 quotatypes[dquot->dq_type],
528 dquot->dq_id);
529 BUG();
531 #endif
533 spin_lock(&dq_list_lock);
534 dqstats.drops++;
535 spin_unlock(&dq_list_lock);
536 we_slept:
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);
542 return;
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);
549 goto we_slept;
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);
556 goto we_slept;
558 atomic_dec(&dquot->dq_count);
559 #ifdef __DQUOT_PARANOIA
560 /* sanity check */
561 if (!list_empty(&dquot->dq_free))
562 BUG();
563 #endif
564 put_dquot_last(dquot);
565 spin_unlock(&dq_list_lock);
568 static struct dquot *get_empty_dquot(struct super_block *sb, int type)
570 struct dquot *dquot;
572 dquot = kmem_cache_alloc(dquot_cachep, SLAB_NOFS);
573 if(!dquot)
574 return NODQUOT;
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);
582 dquot->dq_sb = sb;
583 dquot->dq_type = type;
584 atomic_set(&dquot->dq_count, 1);
586 return dquot;
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))
599 return NODQUOT;
600 we_slept:
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... */
607 goto we_slept;
609 dquot = empty;
610 dquot->dq_id = id;
611 /* all dquots go on the inuse_list */
612 put_inuse(dquot);
613 /* hash it first so it can be found */
614 insert_dquot_hash(dquot);
615 dqstats.lookups++;
616 spin_unlock(&dq_list_lock);
617 } else {
618 if (!atomic_read(&dquot->dq_count))
619 remove_free_dquot(dquot);
620 atomic_inc(&dquot->dq_count);
621 dqstats.cache_hits++;
622 dqstats.lookups++;
623 spin_unlock(&dq_list_lock);
624 if (empty)
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) {
632 dqput(dquot);
633 return NODQUOT;
635 #ifdef __DQUOT_PARANOIA
636 if (!dquot->dq_sb) /* Has somebody invalidated entry under us? */
637 BUG();
638 #endif
640 return dquot;
643 static int dqinit_needed(struct inode *inode, int type)
645 int cnt;
647 if (IS_NOQUOTA(inode))
648 return 0;
649 if (type != -1)
650 return inode->i_dquot[type] == NODQUOT;
651 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
652 if (inode->i_dquot[cnt] == NODQUOT)
653 return 1;
654 return 0;
657 /* This routine is guarded by dqonoff_sem semaphore */
658 static void add_dquot_ref(struct super_block *sb, int type)
660 struct list_head *p;
662 restart:
663 file_list_lock();
664 list_for_each(p, &sb->s_files) {
665 struct file *filp = list_entry(p, struct file, f_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);
669 file_list_unlock();
670 sb->dq_op->initialize(inode, type);
671 dput(dentry);
672 /* As we may have blocked we had better restart... */
673 goto restart;
676 file_list_unlock();
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)
683 return 1;
684 return 0;
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));
699 #endif
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);
703 return 1;
705 else
706 dqput(dquot); /* We have guaranteed we won't block */
708 return 0;
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;
716 struct dquot *dquot;
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... */
724 dqput(dquot);
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 */
735 down(&iprune_sem);
736 down_write(&sb_dqopt(sb)->dqptr_sem);
737 remove_dquot_ref(sb, type, &tofree_head);
738 up_write(&sb_dqopt(sb)->dqptr_sem);
739 up(&iprune_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;
757 else
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;
768 else
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)
780 return 0;
782 switch (dquot->dq_type) {
783 case USRQUOTA:
784 return current->fsuid == dquot->dq_id;
785 case GRPQUOTA:
786 return in_group_p(dquot->dq_id);
788 return 0;
791 /* Values of warnings */
792 #define NOWARN 0
793 #define IHARDWARN 1
794 #define ISOFTLONGWARN 2
795 #define ISOFTWARN 3
796 #define BHARDWARN 4
797 #define BSOFTLONGWARN 5
798 #define BSOFTWARN 6
800 /* Print warning to user which exceeded quota */
801 static void print_warning(struct dquot *dquot, const char warntype)
803 char *msg = NULL;
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)))
808 return;
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, ");
813 else
814 tty_write_message(current->signal->tty, ": write failed, ");
815 tty_write_message(current->signal->tty, quotatypes[dquot->dq_type]);
816 switch (warntype) {
817 case IHARDWARN:
818 msg = " file limit reached.\r\n";
819 break;
820 case ISOFTLONGWARN:
821 msg = " file quota exceeded too long.\r\n";
822 break;
823 case ISOFTWARN:
824 msg = " file quota exceeded.\r\n";
825 break;
826 case BHARDWARN:
827 msg = " block limit reached.\r\n";
828 break;
829 case BSOFTLONGWARN:
830 msg = " block quota exceeded too long.\r\n";
831 break;
832 case BSOFTWARN:
833 msg = " block quota exceeded.\r\n";
834 break;
836 tty_write_message(current->signal->tty, msg);
839 static inline void flush_warnings(struct dquot **dquots, char *warntype)
841 int i;
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)
859 *warntype = NOWARN;
860 if (inodes <= 0 || test_bit(DQ_FAKE_B, &dquot->dq_flags))
861 return QUOTA_OK;
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;
867 return NO_QUOTA;
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;
875 return NO_QUOTA;
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;
885 return QUOTA_OK;
888 /* needs dq_data_lock */
889 static int check_bdq(struct dquot *dquot, qsize_t space, int prealloc, char *warntype)
891 *warntype = 0;
892 if (space <= 0 || test_bit(DQ_FAKE_B, &dquot->dq_flags))
893 return QUOTA_OK;
895 if (dquot->dq_dqb.dqb_bhardlimit &&
896 toqb(dquot->dq_dqb.dqb_curspace + space) > dquot->dq_dqb.dqb_bhardlimit &&
897 !ignore_hardlimit(dquot)) {
898 if (!prealloc)
899 *warntype = BHARDWARN;
900 return NO_QUOTA;
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)) {
907 if (!prealloc)
908 *warntype = BSOFTLONGWARN;
909 return NO_QUOTA;
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) {
915 if (!prealloc) {
916 *warntype = BSOFTWARN;
917 dquot->dq_dqb.dqb_btime = get_seconds() + sb_dqopt(dquot->dq_sb)->info[dquot->dq_type].dqi_bgrace;
919 else
921 * We don't allow preallocation to exceed softlimit so exceeding will
922 * be always printed
924 return NO_QUOTA;
927 return QUOTA_OK;
931 * Initialize quota pointers in inode
932 * Transaction must be started at entry
934 int dquot_initialize(struct inode *inode, int type)
936 unsigned int id = 0;
937 int cnt, ret = 0;
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))
942 return 0;
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))
946 goto out_err;
947 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
948 if (type != -1 && cnt != type)
949 continue;
950 if (inode->i_dquot[cnt] == NODQUOT) {
951 switch (cnt) {
952 case USRQUOTA:
953 id = inode->i_uid;
954 break;
955 case GRPQUOTA:
956 id = inode->i_gid;
957 break;
959 inode->i_dquot[cnt] = dqget(inode->i_sb, id, cnt);
962 out_err:
963 up_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
964 return ret;
968 * Release all quotas referenced by inode
969 * Transaction must be started at an entry
971 int dquot_drop(struct inode *inode)
973 int cnt;
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);
983 return 0;
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)) {
1006 out_add:
1007 inode_add_bytes(inode, number);
1008 return QUOTA_OK;
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);
1016 goto out_add;
1018 spin_lock(&dq_data_lock);
1019 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1020 if (inode->i_dquot[cnt] == NODQUOT)
1021 continue;
1022 if (check_bdq(inode->i_dquot[cnt], number, warn, warntype+cnt) == NO_QUOTA)
1023 goto warn_put_all;
1025 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1026 if (inode->i_dquot[cnt] == NODQUOT)
1027 continue;
1028 dquot_incr_space(inode->i_dquot[cnt], number);
1030 inode_add_bytes(inode, number);
1031 ret = QUOTA_OK;
1032 warn_put_all:
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);
1041 return ret;
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))
1055 return QUOTA_OK;
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);
1061 return QUOTA_OK;
1063 spin_lock(&dq_data_lock);
1064 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1065 if (inode->i_dquot[cnt] == NODQUOT)
1066 continue;
1067 if (check_idq(inode->i_dquot[cnt], number, warntype+cnt) == NO_QUOTA)
1068 goto warn_put_all;
1071 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1072 if (inode->i_dquot[cnt] == NODQUOT)
1073 continue;
1074 dquot_incr_inodes(inode->i_dquot[cnt], number);
1076 ret = QUOTA_OK;
1077 warn_put_all:
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);
1086 return ret;
1090 * This operation can block, but only after everything is updated
1092 int dquot_free_space(struct inode *inode, qsize_t number)
1094 unsigned int cnt;
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)) {
1099 out_sub:
1100 inode_sub_bytes(inode, number);
1101 return QUOTA_OK;
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);
1107 goto out_sub;
1109 spin_lock(&dq_data_lock);
1110 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1111 if (inode->i_dquot[cnt] == NODQUOT)
1112 continue;
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);
1122 return QUOTA_OK;
1126 * This operation can block, but only after everything is updated
1128 int dquot_free_inode(const struct inode *inode, unsigned long number)
1130 unsigned int cnt;
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))
1135 return QUOTA_OK;
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);
1140 return QUOTA_OK;
1142 spin_lock(&dq_data_lock);
1143 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1144 if (inode->i_dquot[cnt] == NODQUOT)
1145 continue;
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);
1154 return QUOTA_OK;
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)
1165 qsize_t space;
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))
1175 return QUOTA_OK;
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);
1185 return QUOTA_OK;
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++) {
1191 switch (cnt) {
1192 case USRQUOTA:
1193 if (!chuid)
1194 continue;
1195 transfer_to[cnt] = dqget(inode->i_sb, iattr->ia_uid, cnt);
1196 break;
1197 case GRPQUOTA:
1198 if (!chgid)
1199 continue;
1200 transfer_to[cnt] = dqget(inode->i_sb, iattr->ia_gid, cnt);
1201 break;
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)
1209 continue;
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)
1213 goto warn_put_all;
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)
1224 continue;
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];
1237 ret = QUOTA_OK;
1238 warn_put_all:
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);
1256 return ret;
1260 * Write info of quota file to disk
1262 int dquot_commit_info(struct super_block *sb, int type)
1264 int ret;
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);
1270 return ret;
1274 * Definitions of diskquota operations.
1276 struct dquot_operations dquot_operations = {
1277 .initialize = dquot_initialize,
1278 .drop = dquot_drop,
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)
1293 switch (type) {
1294 case USRQUOTA:
1295 dqopt->flags |= DQUOT_USR_ENABLED;
1296 break;
1297 case GRPQUOTA:
1298 dqopt->flags |= DQUOT_GRP_ENABLED;
1299 break;
1303 static inline void reset_enable_flags(struct quota_info *dqopt, int type)
1305 switch (type) {
1306 case USRQUOTA:
1307 dqopt->flags &= ~DQUOT_USR_ENABLED;
1308 break;
1309 case GRPQUOTA:
1310 dqopt->flags &= ~DQUOT_GRP_ENABLED;
1311 break;
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)
1320 int cnt;
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)
1331 continue;
1332 if (!sb_has_quota_enabled(sb, cnt))
1333 continue;
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
1376 * nothing to do */
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... */
1389 if (toputmnt[cnt])
1390 mntput(toputmnt[cnt]);
1392 if (sb->s_bdev)
1393 invalidate_bdev(sb->s_bdev, 0);
1394 return 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);
1407 int error;
1408 int oldflags = -1;
1410 if (!fmt)
1411 return -ESRCH;
1412 if (!S_ISREG(inode->i_mode)) {
1413 error = -EACCES;
1414 goto out_fmt;
1416 if (IS_RDONLY(inode)) {
1417 error = -EROFS;
1418 goto out_fmt;
1420 if (!sb->s_op->quota_write || !sb->s_op->quota_read) {
1421 error = -EINVAL;
1422 goto out_fmt;
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)) {
1433 error = -EBUSY;
1434 goto out_lock;
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);
1445 error = -EIO;
1446 dqopt->files[type] = igrab(inode);
1447 if (!dqopt->files[type])
1448 goto out_lock;
1449 error = -EINVAL;
1450 if (!fmt->qf_ops->check_quota_file(sb, type))
1451 goto out_file_init;
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);
1459 goto out_file_init;
1461 up(&dqopt->dqio_sem);
1462 up(&inode->i_sem);
1463 set_enable_flags(dqopt, type);
1465 add_dquot_ref(sb, type);
1466 up(&dqopt->dqonoff_sem);
1468 return 0;
1470 out_file_init:
1471 dqopt->files[type] = NULL;
1472 iput(inode);
1473 out_lock:
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);
1483 up(&inode->i_sem);
1484 out_fmt:
1485 put_quota_format(fmt);
1487 return error;
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;
1494 int error;
1496 error = path_lookup(path, LOOKUP_FOLLOW, &nd);
1497 if (error < 0)
1498 return error;
1499 error = security_quota_on(nd.dentry);
1500 if (error)
1501 goto out_path;
1502 /* Quota file not on the same filesystem? */
1503 if (nd.mnt->mnt_sb != sb)
1504 error = -EXDEV;
1505 else {
1506 error = vfs_quota_on_inode(nd.dentry->d_inode, type, format_id);
1507 if (!error)
1508 sb_dqopt(sb)->mnt[type] = mntget(nd.mnt);
1510 out_path:
1511 path_release(&nd);
1512 return error;
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;
1523 int error;
1525 dentry = lookup_one_len(qf_name, sb->s_root, strlen(qf_name));
1526 if (IS_ERR(dentry))
1527 return PTR_ERR(dentry);
1529 error = security_quota_on(dentry);
1530 if (!error)
1531 error = vfs_quota_on_inode(dentry->d_inode, type, format_id);
1533 dput(dentry);
1534 return error;
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);
1562 return -ESRCH;
1564 do_get_dqblk(dquot, di);
1565 dqput(dquot);
1566 up(&sb_dqopt(sb)->dqonoff_sem);
1567 return 0;
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;
1579 check_blim = 1;
1581 if (di->dqb_valid & QIF_BLIMITS) {
1582 dm->dqb_bsoftlimit = di->dqb_bsoftlimit;
1583 dm->dqb_bhardlimit = di->dqb_bhardlimit;
1584 check_blim = 1;
1586 if (di->dqb_valid & QIF_INODES) {
1587 dm->dqb_curinodes = di->dqb_curinodes;
1588 check_ilim = 1;
1590 if (di->dqb_valid & QIF_ILIMITS) {
1591 dm->dqb_isoftlimit = di->dqb_isoftlimit;
1592 dm->dqb_ihardlimit = di->dqb_ihardlimit;
1593 check_ilim = 1;
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;
1600 if (check_blim) {
1601 if (!dm->dqb_bsoftlimit || toqb(dm->dqb_curspace) < dm->dqb_bsoftlimit) {
1602 dm->dqb_btime = 0;
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;
1608 if (check_ilim) {
1609 if (!dm->dqb_isoftlimit || dm->dqb_curinodes < dm->dqb_isoftlimit) {
1610 dm->dqb_itime = 0;
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);
1618 else
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);
1631 return -ESRCH;
1633 do_set_dqblk(dquot, di);
1634 dqput(dquot);
1635 up(&sb_dqopt(sb)->dqonoff_sem);
1636 return 0;
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);
1647 return -ESRCH;
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);
1657 return 0;
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);
1668 return -ESRCH;
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);
1683 return 0;
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),
1702 .mode = 0444,
1703 .proc_handler = &proc_dointvec,
1706 .ctl_name = FS_DQ_DROPS,
1707 .procname = "drops",
1708 .data = &dqstats.drops,
1709 .maxlen = sizeof(int),
1710 .mode = 0444,
1711 .proc_handler = &proc_dointvec,
1714 .ctl_name = FS_DQ_READS,
1715 .procname = "reads",
1716 .data = &dqstats.reads,
1717 .maxlen = sizeof(int),
1718 .mode = 0444,
1719 .proc_handler = &proc_dointvec,
1722 .ctl_name = FS_DQ_WRITES,
1723 .procname = "writes",
1724 .data = &dqstats.writes,
1725 .maxlen = sizeof(int),
1726 .mode = 0444,
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),
1734 .mode = 0444,
1735 .proc_handler = &proc_dointvec,
1738 .ctl_name = FS_DQ_ALLOCATED,
1739 .procname = "allocated_dquots",
1740 .data = &dqstats.allocated_dquots,
1741 .maxlen = sizeof(int),
1742 .mode = 0444,
1743 .proc_handler = &proc_dointvec,
1746 .ctl_name = FS_DQ_FREE,
1747 .procname = "free_dquots",
1748 .data = &dqstats.free_dquots,
1749 .maxlen = sizeof(int),
1750 .mode = 0444,
1751 .proc_handler = &proc_dointvec,
1754 .ctl_name = FS_DQ_SYNCS,
1755 .procname = "syncs",
1756 .data = &dqstats.syncs,
1757 .maxlen = sizeof(int),
1758 .mode = 0444,
1759 .proc_handler = &proc_dointvec,
1762 .ctl_name = FS_DQ_WARNINGS,
1763 .procname = "warnings",
1764 .data = &flag_print_warnings,
1765 .maxlen = sizeof(int),
1766 .mode = 0644,
1767 .proc_handler = &proc_dointvec,
1769 { .ctl_name = 0 },
1772 static ctl_table fs_table[] = {
1774 .ctl_name = FS_DQSTATS,
1775 .procname = "quota",
1776 .mode = 0555,
1777 .child = fs_dqstats_table,
1779 { .ctl_name = 0 },
1782 static ctl_table sys_table[] = {
1784 .ctl_name = CTL_FS,
1785 .procname = "fs",
1786 .mode = 0555,
1787 .child = fs_table,
1789 { .ctl_name = 0 },
1792 static int __init dquot_init(void)
1794 int i;
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,
1804 NULL, NULL);
1806 order = 0;
1807 dquot_hash = (struct hlist_head *)__get_free_pages(GFP_ATOMIC, order);
1808 if (!dquot_hash)
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);
1813 dq_hash_bits = 0;
1814 do {
1815 dq_hash_bits++;
1816 } while (nr_hash >> dq_hash_bits);
1817 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);
1829 return 0;
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);