MOXA linux-2.6.x / linux-2.6.9-uc0 from sdlinux-moxaart.tgz
[linux-2.6.9-moxart.git] / fs / dquot.c
blobb035fc4b448c918ae878c9dc668d31478c1da954
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
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/pagemap.h>
80 #include <asm/uaccess.h>
82 #define __DQUOT_PARANOIA
85 * There are two quota SMP locks. dq_list_lock protects all lists with quotas
86 * and quota formats and also dqstats structure containing statistics about the
87 * lists. dq_data_lock protects data from dq_dqb and also mem_dqinfo structures
88 * and also guards consistency of dquot->dq_dqb with inode->i_blocks, i_bytes.
89 * i_blocks and i_bytes updates itself are guarded by i_lock acquired directly
90 * in inode_add_bytes() and inode_sub_bytes().
92 * The spinlock ordering is hence: dq_data_lock > dq_list_lock > i_lock
94 * Note that some things (eg. sb pointer, type, id) doesn't change during
95 * the life of the dquot structure and so needn't to be protected by a lock
97 * Any operation working on dquots via inode pointers must hold dqptr_sem. If
98 * operation is just reading pointers from inode (or not using them at all) the
99 * read lock is enough. If pointers are altered function must hold write lock
100 * (these locking rules also apply for S_NOQUOTA flag in the inode - note that
101 * for altering the flag i_sem is also needed). If operation is holding
102 * reference to dquot in other way (e.g. quotactl ops) it must be guarded by
103 * dqonoff_sem.
104 * This locking assures that:
105 * a) update/access to dquot pointers in inode is serialized
106 * b) everyone is guarded against invalidate_dquots()
108 * Each dquot has its dq_lock semaphore. Locked dquots might not be referenced
109 * from inodes (dquot_alloc_space() and such don't check the dq_lock).
110 * Currently dquot is locked only when it is being read to memory (or space for
111 * it is being allocated) on the first dqget() and when it is being released on
112 * the last dqput(). The allocation and release oparations are serialized by
113 * the dq_lock and by checking the use count in dquot_release(). Write
114 * operations on dquots don't hold dq_lock as they copy data under dq_data_lock
115 * spinlock to internal buffers before writing.
117 * Lock ordering (including related VFS locks) is following:
118 * i_sem > dqonoff_sem > iprune_sem > journal_lock > dqptr_sem >
119 * > dquot->dq_lock > dqio_sem
120 * i_sem on quota files is special (it's below dqio_sem)
123 static spinlock_t dq_list_lock = SPIN_LOCK_UNLOCKED;
124 spinlock_t dq_data_lock = SPIN_LOCK_UNLOCKED;
126 static char *quotatypes[] = INITQFNAMES;
127 static struct quota_format_type *quota_formats; /* List of registered formats */
128 static struct quota_module_name module_names[] = INIT_QUOTA_MODULE_NAMES;
130 int register_quota_format(struct quota_format_type *fmt)
132 spin_lock(&dq_list_lock);
133 fmt->qf_next = quota_formats;
134 quota_formats = fmt;
135 spin_unlock(&dq_list_lock);
136 return 0;
139 void unregister_quota_format(struct quota_format_type *fmt)
141 struct quota_format_type **actqf;
143 spin_lock(&dq_list_lock);
144 for (actqf = &quota_formats; *actqf && *actqf != fmt; actqf = &(*actqf)->qf_next);
145 if (*actqf)
146 *actqf = (*actqf)->qf_next;
147 spin_unlock(&dq_list_lock);
150 static struct quota_format_type *find_quota_format(int id)
152 struct quota_format_type *actqf;
154 spin_lock(&dq_list_lock);
155 for (actqf = quota_formats; actqf && actqf->qf_fmt_id != id; actqf = actqf->qf_next);
156 if (!actqf || !try_module_get(actqf->qf_owner)) {
157 int qm;
159 spin_unlock(&dq_list_lock);
161 for (qm = 0; module_names[qm].qm_fmt_id && module_names[qm].qm_fmt_id != id; qm++);
162 if (!module_names[qm].qm_fmt_id || request_module(module_names[qm].qm_mod_name))
163 return NULL;
165 spin_lock(&dq_list_lock);
166 for (actqf = quota_formats; actqf && actqf->qf_fmt_id != id; actqf = actqf->qf_next);
167 if (actqf && !try_module_get(actqf->qf_owner))
168 actqf = NULL;
170 spin_unlock(&dq_list_lock);
171 return actqf;
174 static void put_quota_format(struct quota_format_type *fmt)
176 module_put(fmt->qf_owner);
180 * Dquot List Management:
181 * The quota code uses three lists for dquot management: the inuse_list,
182 * free_dquots, and dquot_hash[] array. A single dquot structure may be
183 * on all three lists, depending on its current state.
185 * All dquots are placed to the end of inuse_list when first created, and this
186 * list is used for the sync and invalidate operations, which must look
187 * at every dquot.
189 * Unused dquots (dq_count == 0) are added to the free_dquots list when freed,
190 * and this list is searched whenever we need an available dquot. Dquots are
191 * removed from the list as soon as they are used again, and
192 * dqstats.free_dquots gives the number of dquots on the list. When
193 * dquot is invalidated it's completely released from memory.
195 * Dquots with a specific identity (device, type and id) are placed on
196 * one of the dquot_hash[] hash chains. The provides an efficient search
197 * mechanism to locate a specific dquot.
200 static LIST_HEAD(inuse_list);
201 static LIST_HEAD(free_dquots);
202 unsigned int dq_hash_bits, dq_hash_mask;
203 static struct hlist_head *dquot_hash;
205 struct dqstats dqstats;
207 static void dqput(struct dquot *dquot);
209 static inline unsigned int
210 hashfn(const struct super_block *sb, unsigned int id, int type)
212 unsigned long tmp;
214 tmp = (((unsigned long)sb>>L1_CACHE_SHIFT) ^ id) * (MAXQUOTAS - type);
215 return (tmp + (tmp >> dq_hash_bits)) & dq_hash_mask;
219 * Following list functions expect dq_list_lock to be held
221 static inline void insert_dquot_hash(struct dquot *dquot)
223 struct hlist_head *head = dquot_hash + hashfn(dquot->dq_sb, dquot->dq_id, dquot->dq_type);
224 hlist_add_head(&dquot->dq_hash, head);
227 static inline void remove_dquot_hash(struct dquot *dquot)
229 hlist_del_init(&dquot->dq_hash);
232 static inline struct dquot *find_dquot(unsigned int hashent, struct super_block *sb, unsigned int id, int type)
234 struct hlist_node *node;
235 struct dquot *dquot;
237 hlist_for_each (node, dquot_hash+hashent) {
238 dquot = hlist_entry(node, struct dquot, dq_hash);
239 if (dquot->dq_sb == sb && dquot->dq_id == id && dquot->dq_type == type)
240 return dquot;
242 return NODQUOT;
245 /* Add a dquot to the tail of the free list */
246 static inline void put_dquot_last(struct dquot *dquot)
248 list_add(&dquot->dq_free, free_dquots.prev);
249 dqstats.free_dquots++;
252 static inline void remove_free_dquot(struct dquot *dquot)
254 if (list_empty(&dquot->dq_free))
255 return;
256 list_del_init(&dquot->dq_free);
257 dqstats.free_dquots--;
260 static inline void put_inuse(struct dquot *dquot)
262 /* We add to the back of inuse list so we don't have to restart
263 * when traversing this list and we block */
264 list_add(&dquot->dq_inuse, inuse_list.prev);
265 dqstats.allocated_dquots++;
268 static inline void remove_inuse(struct dquot *dquot)
270 dqstats.allocated_dquots--;
271 list_del(&dquot->dq_inuse);
274 * End of list functions needing dq_list_lock
277 static void wait_on_dquot(struct dquot *dquot)
279 down(&dquot->dq_lock);
280 up(&dquot->dq_lock);
283 #define mark_dquot_dirty(dquot) ((dquot)->dq_sb->dq_op->mark_dirty(dquot))
285 int dquot_mark_dquot_dirty(struct dquot *dquot)
287 spin_lock(&dq_list_lock);
288 if (!test_and_set_bit(DQ_MOD_B, &dquot->dq_flags))
289 list_add(&dquot->dq_dirty, &sb_dqopt(dquot->dq_sb)->
290 info[dquot->dq_type].dqi_dirty_list);
291 spin_unlock(&dq_list_lock);
292 return 0;
295 /* This function needs dq_list_lock */
296 static inline int clear_dquot_dirty(struct dquot *dquot)
298 if (!test_and_clear_bit(DQ_MOD_B, &dquot->dq_flags))
299 return 0;
300 list_del_init(&dquot->dq_dirty);
301 return 1;
304 void mark_info_dirty(struct super_block *sb, int type)
306 set_bit(DQF_INFO_DIRTY_B, &sb_dqopt(sb)->info[type].dqi_flags);
308 EXPORT_SYMBOL(mark_info_dirty);
311 * Read dquot from disk and alloc space for it
314 int dquot_acquire(struct dquot *dquot)
316 int ret = 0, ret2 = 0;
317 struct quota_info *dqopt = sb_dqopt(dquot->dq_sb);
319 down(&dquot->dq_lock);
320 down(&dqopt->dqio_sem);
321 if (!test_bit(DQ_READ_B, &dquot->dq_flags))
322 ret = dqopt->ops[dquot->dq_type]->read_dqblk(dquot);
323 if (ret < 0)
324 goto out_iolock;
325 set_bit(DQ_READ_B, &dquot->dq_flags);
326 /* Instantiate dquot if needed */
327 if (!test_bit(DQ_ACTIVE_B, &dquot->dq_flags) && !dquot->dq_off) {
328 ret = dqopt->ops[dquot->dq_type]->commit_dqblk(dquot);
329 /* Write the info if needed */
330 if (info_dirty(&dqopt->info[dquot->dq_type]))
331 ret2 = dqopt->ops[dquot->dq_type]->write_file_info(dquot->dq_sb, dquot->dq_type);
332 if (ret < 0)
333 goto out_iolock;
334 if (ret2 < 0) {
335 ret = ret2;
336 goto out_iolock;
339 set_bit(DQ_ACTIVE_B, &dquot->dq_flags);
340 out_iolock:
341 up(&dqopt->dqio_sem);
342 up(&dquot->dq_lock);
343 return ret;
347 * Write dquot to disk
349 int dquot_commit(struct dquot *dquot)
351 int ret = 0, ret2 = 0;
352 struct quota_info *dqopt = sb_dqopt(dquot->dq_sb);
354 down(&dqopt->dqio_sem);
355 spin_lock(&dq_list_lock);
356 if (!clear_dquot_dirty(dquot)) {
357 spin_unlock(&dq_list_lock);
358 goto out_sem;
360 spin_unlock(&dq_list_lock);
361 /* Inactive dquot can be only if there was error during read/init
362 * => we have better not writing it */
363 if (test_bit(DQ_ACTIVE_B, &dquot->dq_flags)) {
364 ret = dqopt->ops[dquot->dq_type]->commit_dqblk(dquot);
365 if (info_dirty(&dqopt->info[dquot->dq_type]))
366 ret2 = dqopt->ops[dquot->dq_type]->write_file_info(dquot->dq_sb, dquot->dq_type);
367 if (ret >= 0)
368 ret = ret2;
370 out_sem:
371 up(&dqopt->dqio_sem);
372 return ret;
376 * Release dquot
378 int dquot_release(struct dquot *dquot)
380 int ret = 0, ret2 = 0;
381 struct quota_info *dqopt = sb_dqopt(dquot->dq_sb);
383 down(&dquot->dq_lock);
384 /* Check whether we are not racing with some other dqget() */
385 if (atomic_read(&dquot->dq_count) > 1)
386 goto out_dqlock;
387 down(&dqopt->dqio_sem);
388 if (dqopt->ops[dquot->dq_type]->release_dqblk) {
389 ret = dqopt->ops[dquot->dq_type]->release_dqblk(dquot);
390 /* Write the info */
391 if (info_dirty(&dqopt->info[dquot->dq_type]))
392 ret2 = dqopt->ops[dquot->dq_type]->write_file_info(dquot->dq_sb, dquot->dq_type);
393 if (ret >= 0)
394 ret = ret2;
396 clear_bit(DQ_ACTIVE_B, &dquot->dq_flags);
397 up(&dqopt->dqio_sem);
398 out_dqlock:
399 up(&dquot->dq_lock);
400 return ret;
403 /* Invalidate all dquots on the list. Note that this function is called after
404 * quota is disabled and pointers from inodes removed so there cannot be new
405 * quota users. Also because we hold dqonoff_sem there can be no quota users
406 * for this sb+type at all. */
407 static void invalidate_dquots(struct super_block *sb, int type)
409 struct dquot *dquot;
410 struct list_head *head;
412 spin_lock(&dq_list_lock);
413 for (head = inuse_list.next; head != &inuse_list;) {
414 dquot = list_entry(head, struct dquot, dq_inuse);
415 head = head->next;
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 int ret;
507 spin_lock(&dq_list_lock);
508 if (nr)
509 prune_dqcache(nr);
510 ret = dqstats.allocated_dquots;
511 spin_unlock(&dq_list_lock);
512 return ret;
516 * Put reference to dquot
517 * NOTE: If you change this function please check whether dqput_blocks() works right...
518 * MUST be called with either dqptr_sem or dqonoff_sem held
520 static void dqput(struct dquot *dquot)
522 if (!dquot)
523 return;
524 #ifdef __DQUOT_PARANOIA
525 if (!atomic_read(&dquot->dq_count)) {
526 printk("VFS: dqput: trying to free free dquot\n");
527 printk("VFS: device %s, dquot of %s %d\n",
528 dquot->dq_sb->s_id,
529 quotatypes[dquot->dq_type],
530 dquot->dq_id);
531 BUG();
533 #endif
535 spin_lock(&dq_list_lock);
536 dqstats.drops++;
537 spin_unlock(&dq_list_lock);
538 we_slept:
539 spin_lock(&dq_list_lock);
540 if (atomic_read(&dquot->dq_count) > 1) {
541 /* We have more than one user... nothing to do */
542 atomic_dec(&dquot->dq_count);
543 spin_unlock(&dq_list_lock);
544 return;
546 /* Need to release dquot? */
547 if (test_bit(DQ_ACTIVE_B, &dquot->dq_flags) && dquot_dirty(dquot)) {
548 spin_unlock(&dq_list_lock);
549 /* Commit dquot before releasing */
550 dquot->dq_sb->dq_op->write_dquot(dquot);
551 goto we_slept;
553 /* Clear flag in case dquot was inactive (something bad happened) */
554 clear_dquot_dirty(dquot);
555 if (test_bit(DQ_ACTIVE_B, &dquot->dq_flags)) {
556 spin_unlock(&dq_list_lock);
557 dquot->dq_sb->dq_op->release_dquot(dquot);
558 goto we_slept;
560 atomic_dec(&dquot->dq_count);
561 #ifdef __DQUOT_PARANOIA
562 /* sanity check */
563 if (!list_empty(&dquot->dq_free))
564 BUG();
565 #endif
566 put_dquot_last(dquot);
567 spin_unlock(&dq_list_lock);
570 static struct dquot *get_empty_dquot(struct super_block *sb, int type)
572 struct dquot *dquot;
574 dquot = kmem_cache_alloc(dquot_cachep, SLAB_NOFS);
575 if(!dquot)
576 return NODQUOT;
578 memset((caddr_t)dquot, 0, sizeof(struct dquot));
579 sema_init(&dquot->dq_lock, 1);
580 INIT_LIST_HEAD(&dquot->dq_free);
581 INIT_LIST_HEAD(&dquot->dq_inuse);
582 INIT_HLIST_NODE(&dquot->dq_hash);
583 INIT_LIST_HEAD(&dquot->dq_dirty);
584 dquot->dq_sb = sb;
585 dquot->dq_type = type;
586 atomic_set(&dquot->dq_count, 1);
588 return dquot;
592 * Get reference to dquot
593 * MUST be called with either dqptr_sem or dqonoff_sem held
595 static struct dquot *dqget(struct super_block *sb, unsigned int id, int type)
597 unsigned int hashent = hashfn(sb, id, type);
598 struct dquot *dquot, *empty = NODQUOT;
600 if (!sb_has_quota_enabled(sb, type))
601 return NODQUOT;
602 we_slept:
603 spin_lock(&dq_list_lock);
604 if ((dquot = find_dquot(hashent, sb, id, type)) == NODQUOT) {
605 if (empty == NODQUOT) {
606 spin_unlock(&dq_list_lock);
607 if ((empty = get_empty_dquot(sb, type)) == NODQUOT)
608 schedule(); /* Try to wait for a moment... */
609 goto we_slept;
611 dquot = empty;
612 dquot->dq_id = id;
613 /* all dquots go on the inuse_list */
614 put_inuse(dquot);
615 /* hash it first so it can be found */
616 insert_dquot_hash(dquot);
617 dqstats.lookups++;
618 spin_unlock(&dq_list_lock);
619 } else {
620 if (!atomic_read(&dquot->dq_count))
621 remove_free_dquot(dquot);
622 atomic_inc(&dquot->dq_count);
623 dqstats.cache_hits++;
624 dqstats.lookups++;
625 spin_unlock(&dq_list_lock);
626 if (empty)
627 kmem_cache_free(dquot_cachep, empty);
629 /* Wait for dq_lock - after this we know that either dquot_release() is already
630 * finished or it will be canceled due to dq_count > 1 test */
631 wait_on_dquot(dquot);
632 /* Read the dquot and instantiate it (everything done only if needed) */
633 if (!test_bit(DQ_ACTIVE_B, &dquot->dq_flags) && sb->dq_op->acquire_dquot(dquot) < 0) {
634 dqput(dquot);
635 return NODQUOT;
637 #ifdef __DQUOT_PARANOIA
638 if (!dquot->dq_sb) /* Has somebody invalidated entry under us? */
639 BUG();
640 #endif
642 return dquot;
645 static int dqinit_needed(struct inode *inode, int type)
647 int cnt;
649 if (IS_NOQUOTA(inode))
650 return 0;
651 if (type != -1)
652 return inode->i_dquot[type] == NODQUOT;
653 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
654 if (inode->i_dquot[cnt] == NODQUOT)
655 return 1;
656 return 0;
659 /* This routine is guarded by dqonoff_sem semaphore */
660 static void add_dquot_ref(struct super_block *sb, int type)
662 struct list_head *p;
664 restart:
665 file_list_lock();
666 list_for_each(p, &sb->s_files) {
667 struct file *filp = list_entry(p, struct file, f_list);
668 struct inode *inode = filp->f_dentry->d_inode;
669 if (filp->f_mode & FMODE_WRITE && dqinit_needed(inode, type)) {
670 struct dentry *dentry = dget(filp->f_dentry);
671 file_list_unlock();
672 sb->dq_op->initialize(inode, type);
673 dput(dentry);
674 /* As we may have blocked we had better restart... */
675 goto restart;
678 file_list_unlock();
681 /* Return 0 if dqput() won't block (note that 1 doesn't necessarily mean blocking) */
682 static inline int dqput_blocks(struct dquot *dquot)
684 if (atomic_read(&dquot->dq_count) <= 1)
685 return 1;
686 return 0;
689 /* Remove references to dquots from inode - add dquot to list for freeing if needed */
690 /* We can't race with anybody because we hold dqptr_sem for writing... */
691 int remove_inode_dquot_ref(struct inode *inode, int type, struct list_head *tofree_head)
693 struct dquot *dquot = inode->i_dquot[type];
695 inode->i_dquot[type] = NODQUOT;
696 if (dquot != NODQUOT) {
697 if (dqput_blocks(dquot)) {
698 #ifdef __DQUOT_PARANOIA
699 if (atomic_read(&dquot->dq_count) != 1)
700 printk(KERN_WARNING "VFS: Adding dquot with dq_count %d to dispose list.\n", atomic_read(&dquot->dq_count));
701 #endif
702 spin_lock(&dq_list_lock);
703 list_add(&dquot->dq_free, tofree_head); /* As dquot must have currently users it can't be on the free list... */
704 spin_unlock(&dq_list_lock);
705 return 1;
707 else
708 dqput(dquot); /* We have guaranteed we won't block */
710 return 0;
713 /* Free list of dquots - called from inode.c */
714 /* dquots are removed from inodes, no new references can be got so we are the only ones holding reference */
715 static void put_dquot_list(struct list_head *tofree_head)
717 struct list_head *act_head;
718 struct dquot *dquot;
720 act_head = tofree_head->next;
721 /* So now we have dquots on the list... Just free them */
722 while (act_head != tofree_head) {
723 dquot = list_entry(act_head, struct dquot, dq_free);
724 act_head = act_head->next;
725 list_del_init(&dquot->dq_free); /* Remove dquot from the list so we won't have problems... */
726 dqput(dquot);
730 /* Gather all references from inodes and drop them */
731 static void drop_dquot_ref(struct super_block *sb, int type)
733 LIST_HEAD(tofree_head);
735 /* We need to be guarded against prune_icache to reach all the
736 * inodes - otherwise some can be on the local list of prune_icache */
737 down(&iprune_sem);
738 down_write(&sb_dqopt(sb)->dqptr_sem);
739 remove_dquot_ref(sb, type, &tofree_head);
740 up_write(&sb_dqopt(sb)->dqptr_sem);
741 up(&iprune_sem);
742 put_dquot_list(&tofree_head);
745 static inline void dquot_incr_inodes(struct dquot *dquot, unsigned long number)
747 dquot->dq_dqb.dqb_curinodes += number;
750 static inline void dquot_incr_space(struct dquot *dquot, qsize_t number)
752 dquot->dq_dqb.dqb_curspace += number;
755 static inline void dquot_decr_inodes(struct dquot *dquot, unsigned long number)
757 if (dquot->dq_dqb.dqb_curinodes > number)
758 dquot->dq_dqb.dqb_curinodes -= number;
759 else
760 dquot->dq_dqb.dqb_curinodes = 0;
761 if (dquot->dq_dqb.dqb_curinodes < dquot->dq_dqb.dqb_isoftlimit)
762 dquot->dq_dqb.dqb_itime = (time_t) 0;
763 clear_bit(DQ_INODES_B, &dquot->dq_flags);
766 static inline void dquot_decr_space(struct dquot *dquot, qsize_t number)
768 if (dquot->dq_dqb.dqb_curspace > number)
769 dquot->dq_dqb.dqb_curspace -= number;
770 else
771 dquot->dq_dqb.dqb_curspace = 0;
772 if (toqb(dquot->dq_dqb.dqb_curspace) < dquot->dq_dqb.dqb_bsoftlimit)
773 dquot->dq_dqb.dqb_btime = (time_t) 0;
774 clear_bit(DQ_BLKS_B, &dquot->dq_flags);
777 static inline int need_print_warning(struct dquot *dquot)
779 switch (dquot->dq_type) {
780 case USRQUOTA:
781 return current->fsuid == dquot->dq_id;
782 case GRPQUOTA:
783 return in_group_p(dquot->dq_id);
785 return 0;
788 /* Values of warnings */
789 #define NOWARN 0
790 #define IHARDWARN 1
791 #define ISOFTLONGWARN 2
792 #define ISOFTWARN 3
793 #define BHARDWARN 4
794 #define BSOFTLONGWARN 5
795 #define BSOFTWARN 6
797 /* Print warning to user which exceeded quota */
798 static void print_warning(struct dquot *dquot, const char warntype)
800 char *msg = NULL;
801 int flag = (warntype == BHARDWARN || warntype == BSOFTLONGWARN) ? DQ_BLKS_B :
802 ((warntype == IHARDWARN || warntype == ISOFTLONGWARN) ? DQ_INODES_B : 0);
804 if (!need_print_warning(dquot) || (flag && test_and_set_bit(flag, &dquot->dq_flags)))
805 return;
806 tty_write_message(current->signal->tty, dquot->dq_sb->s_id);
807 if (warntype == ISOFTWARN || warntype == BSOFTWARN)
808 tty_write_message(current->signal->tty, ": warning, ");
809 else
810 tty_write_message(current->signal->tty, ": write failed, ");
811 tty_write_message(current->signal->tty, quotatypes[dquot->dq_type]);
812 switch (warntype) {
813 case IHARDWARN:
814 msg = " file limit reached.\n";
815 break;
816 case ISOFTLONGWARN:
817 msg = " file quota exceeded too long.\n";
818 break;
819 case ISOFTWARN:
820 msg = " file quota exceeded.\n";
821 break;
822 case BHARDWARN:
823 msg = " block limit reached.\n";
824 break;
825 case BSOFTLONGWARN:
826 msg = " block quota exceeded too long.\n";
827 break;
828 case BSOFTWARN:
829 msg = " block quota exceeded.\n";
830 break;
832 tty_write_message(current->signal->tty, msg);
835 static inline void flush_warnings(struct dquot **dquots, char *warntype)
837 int i;
839 for (i = 0; i < MAXQUOTAS; i++)
840 if (dquots[i] != NODQUOT && warntype[i] != NOWARN)
841 print_warning(dquots[i], warntype[i]);
844 static inline char ignore_hardlimit(struct dquot *dquot)
846 struct mem_dqinfo *info = &sb_dqopt(dquot->dq_sb)->info[dquot->dq_type];
848 return capable(CAP_SYS_RESOURCE) &&
849 (info->dqi_format->qf_fmt_id != QFMT_VFS_OLD || !(info->dqi_flags & V1_DQF_RSQUASH));
852 /* needs dq_data_lock */
853 static int check_idq(struct dquot *dquot, ulong inodes, char *warntype)
855 *warntype = NOWARN;
856 if (inodes <= 0 || test_bit(DQ_FAKE_B, &dquot->dq_flags))
857 return QUOTA_OK;
859 if (dquot->dq_dqb.dqb_ihardlimit &&
860 (dquot->dq_dqb.dqb_curinodes + inodes) > dquot->dq_dqb.dqb_ihardlimit &&
861 !ignore_hardlimit(dquot)) {
862 *warntype = IHARDWARN;
863 return NO_QUOTA;
866 if (dquot->dq_dqb.dqb_isoftlimit &&
867 (dquot->dq_dqb.dqb_curinodes + inodes) > dquot->dq_dqb.dqb_isoftlimit &&
868 dquot->dq_dqb.dqb_itime && get_seconds() >= dquot->dq_dqb.dqb_itime &&
869 !ignore_hardlimit(dquot)) {
870 *warntype = ISOFTLONGWARN;
871 return NO_QUOTA;
874 if (dquot->dq_dqb.dqb_isoftlimit &&
875 (dquot->dq_dqb.dqb_curinodes + inodes) > dquot->dq_dqb.dqb_isoftlimit &&
876 dquot->dq_dqb.dqb_itime == 0) {
877 *warntype = ISOFTWARN;
878 dquot->dq_dqb.dqb_itime = get_seconds() + sb_dqopt(dquot->dq_sb)->info[dquot->dq_type].dqi_igrace;
881 return QUOTA_OK;
884 /* needs dq_data_lock */
885 static int check_bdq(struct dquot *dquot, qsize_t space, int prealloc, char *warntype)
887 *warntype = 0;
888 if (space <= 0 || test_bit(DQ_FAKE_B, &dquot->dq_flags))
889 return QUOTA_OK;
891 if (dquot->dq_dqb.dqb_bhardlimit &&
892 toqb(dquot->dq_dqb.dqb_curspace + space) > dquot->dq_dqb.dqb_bhardlimit &&
893 !ignore_hardlimit(dquot)) {
894 if (!prealloc)
895 *warntype = BHARDWARN;
896 return NO_QUOTA;
899 if (dquot->dq_dqb.dqb_bsoftlimit &&
900 toqb(dquot->dq_dqb.dqb_curspace + space) > dquot->dq_dqb.dqb_bsoftlimit &&
901 dquot->dq_dqb.dqb_btime && get_seconds() >= dquot->dq_dqb.dqb_btime &&
902 !ignore_hardlimit(dquot)) {
903 if (!prealloc)
904 *warntype = BSOFTLONGWARN;
905 return NO_QUOTA;
908 if (dquot->dq_dqb.dqb_bsoftlimit &&
909 toqb(dquot->dq_dqb.dqb_curspace + space) > dquot->dq_dqb.dqb_bsoftlimit &&
910 dquot->dq_dqb.dqb_btime == 0) {
911 if (!prealloc) {
912 *warntype = BSOFTWARN;
913 dquot->dq_dqb.dqb_btime = get_seconds() + sb_dqopt(dquot->dq_sb)->info[dquot->dq_type].dqi_bgrace;
915 else
917 * We don't allow preallocation to exceed softlimit so exceeding will
918 * be always printed
920 return NO_QUOTA;
923 return QUOTA_OK;
927 * Initialize quota pointers in inode
928 * Transaction must be started at entry
930 int dquot_initialize(struct inode *inode, int type)
932 unsigned int id = 0;
933 int cnt, ret = 0;
935 /* First test before acquiring semaphore - solves deadlocks when we
936 * re-enter the quota code and are already holding the semaphore */
937 if (IS_NOQUOTA(inode))
938 return 0;
939 down_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
940 /* Having dqptr_sem we know NOQUOTA flags can't be altered... */
941 if (IS_NOQUOTA(inode))
942 goto out_err;
943 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
944 if (type != -1 && cnt != type)
945 continue;
946 if (inode->i_dquot[cnt] == NODQUOT) {
947 switch (cnt) {
948 case USRQUOTA:
949 id = inode->i_uid;
950 break;
951 case GRPQUOTA:
952 id = inode->i_gid;
953 break;
955 inode->i_dquot[cnt] = dqget(inode->i_sb, id, cnt);
958 out_err:
959 up_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
960 return ret;
964 * Release all quotas referenced by inode
965 * Transaction must be started at an entry
967 int dquot_drop(struct inode *inode)
969 int cnt;
971 down_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
972 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
973 if (inode->i_dquot[cnt] != NODQUOT) {
974 dqput(inode->i_dquot[cnt]);
975 inode->i_dquot[cnt] = NODQUOT;
978 up_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
979 return 0;
983 * Following four functions update i_blocks+i_bytes fields and
984 * quota information (together with appropriate checks)
985 * NOTE: We absolutely rely on the fact that caller dirties
986 * the inode (usually macros in quotaops.h care about this) and
987 * holds a handle for the current transaction so that dquot write and
988 * inode write go into the same transaction.
992 * This operation can block, but only after everything is updated
994 int dquot_alloc_space(struct inode *inode, qsize_t number, int warn)
996 int cnt, ret = NO_QUOTA;
997 char warntype[MAXQUOTAS];
999 /* First test before acquiring semaphore - solves deadlocks when we
1000 * re-enter the quota code and are already holding the semaphore */
1001 if (IS_NOQUOTA(inode)) {
1002 out_add:
1003 inode_add_bytes(inode, number);
1004 return QUOTA_OK;
1006 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1007 warntype[cnt] = NOWARN;
1009 down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1010 if (IS_NOQUOTA(inode)) { /* Now we can do reliable test... */
1011 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1012 goto out_add;
1014 spin_lock(&dq_data_lock);
1015 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1016 if (inode->i_dquot[cnt] == NODQUOT)
1017 continue;
1018 if (check_bdq(inode->i_dquot[cnt], number, warn, warntype+cnt) == NO_QUOTA)
1019 goto warn_put_all;
1021 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1022 if (inode->i_dquot[cnt] == NODQUOT)
1023 continue;
1024 dquot_incr_space(inode->i_dquot[cnt], number);
1026 inode_add_bytes(inode, number);
1027 ret = QUOTA_OK;
1028 warn_put_all:
1029 spin_unlock(&dq_data_lock);
1030 if (ret == QUOTA_OK)
1031 /* Dirtify all the dquots - this can block when journalling */
1032 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1033 if (inode->i_dquot[cnt])
1034 mark_dquot_dirty(inode->i_dquot[cnt]);
1035 flush_warnings(inode->i_dquot, warntype);
1036 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1037 return ret;
1041 * This operation can block, but only after everything is updated
1043 int dquot_alloc_inode(const struct inode *inode, unsigned long number)
1045 int cnt, ret = NO_QUOTA;
1046 char warntype[MAXQUOTAS];
1048 /* First test before acquiring semaphore - solves deadlocks when we
1049 * re-enter the quota code and are already holding the semaphore */
1050 if (IS_NOQUOTA(inode))
1051 return QUOTA_OK;
1052 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1053 warntype[cnt] = NOWARN;
1054 down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1055 if (IS_NOQUOTA(inode)) {
1056 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1057 return QUOTA_OK;
1059 spin_lock(&dq_data_lock);
1060 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1061 if (inode->i_dquot[cnt] == NODQUOT)
1062 continue;
1063 if (check_idq(inode->i_dquot[cnt], number, warntype+cnt) == NO_QUOTA)
1064 goto warn_put_all;
1067 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1068 if (inode->i_dquot[cnt] == NODQUOT)
1069 continue;
1070 dquot_incr_inodes(inode->i_dquot[cnt], number);
1072 ret = QUOTA_OK;
1073 warn_put_all:
1074 spin_unlock(&dq_data_lock);
1075 if (ret == QUOTA_OK)
1076 /* Dirtify all the dquots - this can block when journalling */
1077 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1078 if (inode->i_dquot[cnt])
1079 mark_dquot_dirty(inode->i_dquot[cnt]);
1080 flush_warnings((struct dquot **)inode->i_dquot, warntype);
1081 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1082 return ret;
1086 * This is a non-blocking operation.
1088 int dquot_free_space(struct inode *inode, qsize_t number)
1090 unsigned int cnt;
1092 /* First test before acquiring semaphore - solves deadlocks when we
1093 * re-enter the quota code and are already holding the semaphore */
1094 if (IS_NOQUOTA(inode)) {
1095 out_sub:
1096 inode_sub_bytes(inode, number);
1097 return QUOTA_OK;
1099 down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1100 /* Now recheck reliably when holding dqptr_sem */
1101 if (IS_NOQUOTA(inode)) {
1102 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1103 goto out_sub;
1105 spin_lock(&dq_data_lock);
1106 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1107 if (inode->i_dquot[cnt] == NODQUOT)
1108 continue;
1109 dquot_decr_space(inode->i_dquot[cnt], number);
1111 inode_sub_bytes(inode, number);
1112 spin_unlock(&dq_data_lock);
1113 /* Dirtify all the dquots - this can block when journalling */
1114 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1115 if (inode->i_dquot[cnt])
1116 mark_dquot_dirty(inode->i_dquot[cnt]);
1117 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1118 return QUOTA_OK;
1122 * This is a non-blocking operation.
1124 int dquot_free_inode(const struct inode *inode, unsigned long number)
1126 unsigned int cnt;
1128 /* First test before acquiring semaphore - solves deadlocks when we
1129 * re-enter the quota code and are already holding the semaphore */
1130 if (IS_NOQUOTA(inode))
1131 return QUOTA_OK;
1132 down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1133 /* Now recheck reliably when holding dqptr_sem */
1134 if (IS_NOQUOTA(inode)) {
1135 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1136 return QUOTA_OK;
1138 spin_lock(&dq_data_lock);
1139 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1140 if (inode->i_dquot[cnt] == NODQUOT)
1141 continue;
1142 dquot_decr_inodes(inode->i_dquot[cnt], number);
1144 spin_unlock(&dq_data_lock);
1145 /* Dirtify all the dquots - this can block when journalling */
1146 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1147 if (inode->i_dquot[cnt])
1148 mark_dquot_dirty(inode->i_dquot[cnt]);
1149 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1150 return QUOTA_OK;
1154 * Transfer the number of inode and blocks from one diskquota to an other.
1156 * This operation can block, but only after everything is updated
1158 int dquot_transfer(struct inode *inode, struct iattr *iattr)
1160 qsize_t space;
1161 struct dquot *transfer_from[MAXQUOTAS];
1162 struct dquot *transfer_to[MAXQUOTAS];
1163 int cnt, ret = NO_QUOTA, chuid = (iattr->ia_valid & ATTR_UID) && inode->i_uid != iattr->ia_uid,
1164 chgid = (iattr->ia_valid & ATTR_GID) && inode->i_gid != iattr->ia_gid;
1165 char warntype[MAXQUOTAS];
1167 /* First test before acquiring semaphore - solves deadlocks when we
1168 * re-enter the quota code and are already holding the semaphore */
1169 if (IS_NOQUOTA(inode))
1170 return QUOTA_OK;
1171 /* Clear the arrays */
1172 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1173 transfer_to[cnt] = transfer_from[cnt] = NODQUOT;
1174 warntype[cnt] = NOWARN;
1176 down_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
1177 /* Now recheck reliably when holding dqptr_sem */
1178 if (IS_NOQUOTA(inode)) { /* File without quota accounting? */
1179 up_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
1180 return QUOTA_OK;
1182 /* First build the transfer_to list - here we can block on
1183 * reading/instantiating of dquots. We know that the transaction for
1184 * us was already started so we don't violate lock ranking here */
1185 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1186 switch (cnt) {
1187 case USRQUOTA:
1188 if (!chuid)
1189 continue;
1190 transfer_to[cnt] = dqget(inode->i_sb, iattr->ia_uid, cnt);
1191 break;
1192 case GRPQUOTA:
1193 if (!chgid)
1194 continue;
1195 transfer_to[cnt] = dqget(inode->i_sb, iattr->ia_gid, cnt);
1196 break;
1199 spin_lock(&dq_data_lock);
1200 space = inode_get_bytes(inode);
1201 /* Build the transfer_from list and check the limits */
1202 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1203 if (transfer_to[cnt] == NODQUOT)
1204 continue;
1205 transfer_from[cnt] = inode->i_dquot[cnt];
1206 if (check_idq(transfer_to[cnt], 1, warntype+cnt) == NO_QUOTA ||
1207 check_bdq(transfer_to[cnt], space, 0, warntype+cnt) == NO_QUOTA)
1208 goto warn_put_all;
1212 * Finally perform the needed transfer from transfer_from to transfer_to
1214 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1216 * Skip changes for same uid or gid or for turned off quota-type.
1218 if (transfer_to[cnt] == NODQUOT)
1219 continue;
1221 /* Due to IO error we might not have transfer_from[] structure */
1222 if (transfer_from[cnt]) {
1223 dquot_decr_inodes(transfer_from[cnt], 1);
1224 dquot_decr_space(transfer_from[cnt], space);
1227 dquot_incr_inodes(transfer_to[cnt], 1);
1228 dquot_incr_space(transfer_to[cnt], space);
1230 inode->i_dquot[cnt] = transfer_to[cnt];
1232 ret = QUOTA_OK;
1233 warn_put_all:
1234 spin_unlock(&dq_data_lock);
1235 /* Dirtify all the dquots - this can block when journalling */
1236 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1237 if (transfer_from[cnt])
1238 mark_dquot_dirty(transfer_from[cnt]);
1239 if (transfer_to[cnt])
1240 mark_dquot_dirty(transfer_to[cnt]);
1242 flush_warnings(transfer_to, warntype);
1244 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1245 if (ret == QUOTA_OK && transfer_from[cnt] != NODQUOT)
1246 dqput(transfer_from[cnt]);
1247 if (ret == NO_QUOTA && transfer_to[cnt] != NODQUOT)
1248 dqput(transfer_to[cnt]);
1250 up_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
1251 return ret;
1255 * Write info of quota file to disk
1257 int dquot_commit_info(struct super_block *sb, int type)
1259 int ret;
1260 struct quota_info *dqopt = sb_dqopt(sb);
1262 down(&dqopt->dqio_sem);
1263 ret = dqopt->ops[type]->write_file_info(sb, type);
1264 up(&dqopt->dqio_sem);
1265 return ret;
1269 * Definitions of diskquota operations.
1271 struct dquot_operations dquot_operations = {
1272 .initialize = dquot_initialize,
1273 .drop = dquot_drop,
1274 .alloc_space = dquot_alloc_space,
1275 .alloc_inode = dquot_alloc_inode,
1276 .free_space = dquot_free_space,
1277 .free_inode = dquot_free_inode,
1278 .transfer = dquot_transfer,
1279 .write_dquot = dquot_commit,
1280 .acquire_dquot = dquot_acquire,
1281 .release_dquot = dquot_release,
1282 .mark_dirty = dquot_mark_dquot_dirty,
1283 .write_info = dquot_commit_info
1286 static inline void set_enable_flags(struct quota_info *dqopt, int type)
1288 switch (type) {
1289 case USRQUOTA:
1290 dqopt->flags |= DQUOT_USR_ENABLED;
1291 break;
1292 case GRPQUOTA:
1293 dqopt->flags |= DQUOT_GRP_ENABLED;
1294 break;
1298 static inline void reset_enable_flags(struct quota_info *dqopt, int type)
1300 switch (type) {
1301 case USRQUOTA:
1302 dqopt->flags &= ~DQUOT_USR_ENABLED;
1303 break;
1304 case GRPQUOTA:
1305 dqopt->flags &= ~DQUOT_GRP_ENABLED;
1306 break;
1311 * Turn quota off on a device. type == -1 ==> quotaoff for all types (umount)
1313 int vfs_quota_off(struct super_block *sb, int type)
1315 int cnt;
1316 struct quota_info *dqopt = sb_dqopt(sb);
1318 /* We need to serialize quota_off() for device */
1319 down(&dqopt->dqonoff_sem);
1320 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1321 if (type != -1 && cnt != type)
1322 continue;
1323 if (!sb_has_quota_enabled(sb, cnt))
1324 continue;
1325 reset_enable_flags(dqopt, cnt);
1327 /* Note: these are blocking operations */
1328 drop_dquot_ref(sb, cnt);
1329 invalidate_dquots(sb, cnt);
1331 * Now all dquots should be invalidated, all writes done so we should be only
1332 * users of the info. No locks needed.
1334 if (info_dirty(&dqopt->info[cnt]))
1335 sb->dq_op->write_info(sb, cnt);
1336 if (dqopt->ops[cnt]->free_file_info)
1337 dqopt->ops[cnt]->free_file_info(sb, cnt);
1338 put_quota_format(dqopt->info[cnt].dqi_format);
1340 fput(dqopt->files[cnt]);
1341 dqopt->files[cnt] = NULL;
1342 dqopt->info[cnt].dqi_flags = 0;
1343 dqopt->info[cnt].dqi_igrace = 0;
1344 dqopt->info[cnt].dqi_bgrace = 0;
1345 dqopt->ops[cnt] = NULL;
1347 up(&dqopt->dqonoff_sem);
1348 return 0;
1352 * Turn quotas on on a device
1355 /* Helper function when we already have file open */
1356 static int vfs_quota_on_file(struct file *f, int type, int format_id)
1358 struct quota_format_type *fmt = find_quota_format(format_id);
1359 struct inode *inode;
1360 struct super_block *sb = f->f_dentry->d_sb;
1361 struct quota_info *dqopt = sb_dqopt(sb);
1362 struct dquot *to_drop[MAXQUOTAS];
1363 int error, cnt;
1364 unsigned int oldflags = -1;
1366 if (!fmt)
1367 return -ESRCH;
1368 error = -EIO;
1369 if (!f->f_op || !f->f_op->read || !f->f_op->write)
1370 goto out_fmt;
1371 inode = f->f_dentry->d_inode;
1372 error = -EACCES;
1373 if (!S_ISREG(inode->i_mode))
1374 goto out_fmt;
1376 down(&inode->i_sem);
1377 down(&dqopt->dqonoff_sem);
1378 if (sb_has_quota_enabled(sb, type)) {
1379 up(&inode->i_sem);
1380 error = -EBUSY;
1381 goto out_lock;
1383 /* We don't want quota and atime on quota files (deadlocks possible)
1384 * We also need to set GFP mask differently because we cannot recurse
1385 * into filesystem when allocating page for quota inode */
1386 down_write(&dqopt->dqptr_sem);
1387 oldflags = inode->i_flags & (S_NOATIME | S_NOQUOTA);
1388 inode->i_flags |= S_NOQUOTA | S_NOATIME;
1389 up_write(&dqopt->dqptr_sem);
1390 up(&inode->i_sem);
1392 dqopt->files[type] = f;
1393 error = -EINVAL;
1394 if (!fmt->qf_ops->check_quota_file(sb, type))
1395 goto out_file_init;
1397 * We write to quota files deep within filesystem code. We don't want
1398 * the VFS to reenter filesystem code when it tries to allocate a
1399 * pagecache page for the quota file write. So clear __GFP_FS in
1400 * the quota file's allocation flags.
1402 mapping_set_gfp_mask(inode->i_mapping,
1403 mapping_gfp_mask(inode->i_mapping) & ~__GFP_FS);
1405 down_write(&dqopt->dqptr_sem);
1406 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1407 to_drop[cnt] = inode->i_dquot[cnt];
1408 inode->i_dquot[cnt] = NODQUOT;
1410 up_write(&dqopt->dqptr_sem);
1411 /* We must put dquots outside of dqptr_sem because we may need to
1412 * start transaction for dquot_release() */
1413 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1414 if (to_drop[cnt])
1415 dqput(to_drop[cnt]);
1418 dqopt->ops[type] = fmt->qf_ops;
1419 dqopt->info[type].dqi_format = fmt;
1420 INIT_LIST_HEAD(&dqopt->info[type].dqi_dirty_list);
1421 down(&dqopt->dqio_sem);
1422 if ((error = dqopt->ops[type]->read_file_info(sb, type)) < 0) {
1423 up(&dqopt->dqio_sem);
1424 goto out_file_init;
1426 up(&dqopt->dqio_sem);
1427 set_enable_flags(dqopt, type);
1429 add_dquot_ref(sb, type);
1430 up(&dqopt->dqonoff_sem);
1432 return 0;
1434 out_file_init:
1435 dqopt->files[type] = NULL;
1436 out_lock:
1437 up(&dqopt->dqonoff_sem);
1438 if (oldflags != -1) {
1439 down(&inode->i_sem);
1440 down_write(&dqopt->dqptr_sem);
1441 /* Reset the NOATIME flag back. I know it could change in the
1442 * mean time but playing with NOATIME flags on a quota file is
1443 * never a good idea */
1444 inode->i_flags &= ~(S_NOATIME | S_NOQUOTA);
1445 inode->i_flags |= oldflags;
1446 up_write(&dqopt->dqptr_sem);
1447 up(&inode->i_sem);
1449 out_fmt:
1450 put_quota_format(fmt);
1452 return error;
1455 /* Actual function called from quotactl() */
1456 int vfs_quota_on(struct super_block *sb, int type, int format_id, char *path)
1458 struct file *f;
1459 int error;
1461 f = filp_open(path, O_RDWR, 0600);
1462 if (IS_ERR(f))
1463 return PTR_ERR(f);
1464 error = security_quota_on(f);
1465 if (error)
1466 goto out_f;
1467 error = vfs_quota_on_file(f, type, format_id);
1468 if (!error)
1469 return 0;
1470 out_f:
1471 filp_close(f, NULL);
1472 return error;
1476 * Function used by filesystems when filp_open() would fail (filesystem is
1477 * being mounted now). We will use a private file structure. Caller is
1478 * responsible that it's IO functions won't need vfsmnt structure or
1479 * some dentry tricks...
1481 int vfs_quota_on_mount(int type, int format_id, struct dentry *dentry)
1483 struct file *f;
1484 int error;
1486 dget(dentry); /* Get a reference for struct file */
1487 f = dentry_open(dentry, NULL, O_RDWR);
1488 if (IS_ERR(f)) {
1489 error = PTR_ERR(f);
1490 goto out_dentry;
1492 error = vfs_quota_on_file(f, type, format_id);
1493 if (!error)
1494 return 0;
1495 fput(f);
1496 out_dentry:
1497 dput(dentry);
1498 return error;
1501 /* Generic routine for getting common part of quota structure */
1502 static void do_get_dqblk(struct dquot *dquot, struct if_dqblk *di)
1504 struct mem_dqblk *dm = &dquot->dq_dqb;
1506 spin_lock(&dq_data_lock);
1507 di->dqb_bhardlimit = dm->dqb_bhardlimit;
1508 di->dqb_bsoftlimit = dm->dqb_bsoftlimit;
1509 di->dqb_curspace = dm->dqb_curspace;
1510 di->dqb_ihardlimit = dm->dqb_ihardlimit;
1511 di->dqb_isoftlimit = dm->dqb_isoftlimit;
1512 di->dqb_curinodes = dm->dqb_curinodes;
1513 di->dqb_btime = dm->dqb_btime;
1514 di->dqb_itime = dm->dqb_itime;
1515 di->dqb_valid = QIF_ALL;
1516 spin_unlock(&dq_data_lock);
1519 int vfs_get_dqblk(struct super_block *sb, int type, qid_t id, struct if_dqblk *di)
1521 struct dquot *dquot;
1523 down(&sb_dqopt(sb)->dqonoff_sem);
1524 if (!(dquot = dqget(sb, id, type))) {
1525 up(&sb_dqopt(sb)->dqonoff_sem);
1526 return -ESRCH;
1528 do_get_dqblk(dquot, di);
1529 dqput(dquot);
1530 up(&sb_dqopt(sb)->dqonoff_sem);
1531 return 0;
1534 /* Generic routine for setting common part of quota structure */
1535 static void do_set_dqblk(struct dquot *dquot, struct if_dqblk *di)
1537 struct mem_dqblk *dm = &dquot->dq_dqb;
1538 int check_blim = 0, check_ilim = 0;
1540 spin_lock(&dq_data_lock);
1541 if (di->dqb_valid & QIF_SPACE) {
1542 dm->dqb_curspace = di->dqb_curspace;
1543 check_blim = 1;
1545 if (di->dqb_valid & QIF_BLIMITS) {
1546 dm->dqb_bsoftlimit = di->dqb_bsoftlimit;
1547 dm->dqb_bhardlimit = di->dqb_bhardlimit;
1548 check_blim = 1;
1550 if (di->dqb_valid & QIF_INODES) {
1551 dm->dqb_curinodes = di->dqb_curinodes;
1552 check_ilim = 1;
1554 if (di->dqb_valid & QIF_ILIMITS) {
1555 dm->dqb_isoftlimit = di->dqb_isoftlimit;
1556 dm->dqb_ihardlimit = di->dqb_ihardlimit;
1557 check_ilim = 1;
1559 if (di->dqb_valid & QIF_BTIME)
1560 dm->dqb_btime = di->dqb_btime;
1561 if (di->dqb_valid & QIF_ITIME)
1562 dm->dqb_itime = di->dqb_itime;
1564 if (check_blim) {
1565 if (!dm->dqb_bsoftlimit || toqb(dm->dqb_curspace) < dm->dqb_bsoftlimit) {
1566 dm->dqb_btime = 0;
1567 clear_bit(DQ_BLKS_B, &dquot->dq_flags);
1569 else if (!(di->dqb_valid & QIF_BTIME)) /* Set grace only if user hasn't provided his own... */
1570 dm->dqb_btime = get_seconds() + sb_dqopt(dquot->dq_sb)->info[dquot->dq_type].dqi_bgrace;
1572 if (check_ilim) {
1573 if (!dm->dqb_isoftlimit || dm->dqb_curinodes < dm->dqb_isoftlimit) {
1574 dm->dqb_itime = 0;
1575 clear_bit(DQ_INODES_B, &dquot->dq_flags);
1577 else if (!(di->dqb_valid & QIF_ITIME)) /* Set grace only if user hasn't provided his own... */
1578 dm->dqb_itime = get_seconds() + sb_dqopt(dquot->dq_sb)->info[dquot->dq_type].dqi_igrace;
1580 if (dm->dqb_bhardlimit || dm->dqb_bsoftlimit || dm->dqb_ihardlimit || dm->dqb_isoftlimit)
1581 clear_bit(DQ_FAKE_B, &dquot->dq_flags);
1582 else
1583 set_bit(DQ_FAKE_B, &dquot->dq_flags);
1584 spin_unlock(&dq_data_lock);
1585 mark_dquot_dirty(dquot);
1588 int vfs_set_dqblk(struct super_block *sb, int type, qid_t id, struct if_dqblk *di)
1590 struct dquot *dquot;
1592 down(&sb_dqopt(sb)->dqonoff_sem);
1593 if (!(dquot = dqget(sb, id, type))) {
1594 up(&sb_dqopt(sb)->dqonoff_sem);
1595 return -ESRCH;
1597 do_set_dqblk(dquot, di);
1598 dqput(dquot);
1599 up(&sb_dqopt(sb)->dqonoff_sem);
1600 return 0;
1603 /* Generic routine for getting common part of quota file information */
1604 int vfs_get_dqinfo(struct super_block *sb, int type, struct if_dqinfo *ii)
1606 struct mem_dqinfo *mi;
1608 down(&sb_dqopt(sb)->dqonoff_sem);
1609 if (!sb_has_quota_enabled(sb, type)) {
1610 up(&sb_dqopt(sb)->dqonoff_sem);
1611 return -ESRCH;
1613 mi = sb_dqopt(sb)->info + type;
1614 spin_lock(&dq_data_lock);
1615 ii->dqi_bgrace = mi->dqi_bgrace;
1616 ii->dqi_igrace = mi->dqi_igrace;
1617 ii->dqi_flags = mi->dqi_flags & DQF_MASK;
1618 ii->dqi_valid = IIF_ALL;
1619 spin_unlock(&dq_data_lock);
1620 up(&sb_dqopt(sb)->dqonoff_sem);
1621 return 0;
1624 /* Generic routine for setting common part of quota file information */
1625 int vfs_set_dqinfo(struct super_block *sb, int type, struct if_dqinfo *ii)
1627 struct mem_dqinfo *mi;
1629 down(&sb_dqopt(sb)->dqonoff_sem);
1630 if (!sb_has_quota_enabled(sb, type)) {
1631 up(&sb_dqopt(sb)->dqonoff_sem);
1632 return -ESRCH;
1634 mi = sb_dqopt(sb)->info + type;
1635 spin_lock(&dq_data_lock);
1636 if (ii->dqi_valid & IIF_BGRACE)
1637 mi->dqi_bgrace = ii->dqi_bgrace;
1638 if (ii->dqi_valid & IIF_IGRACE)
1639 mi->dqi_igrace = ii->dqi_igrace;
1640 if (ii->dqi_valid & IIF_FLAGS)
1641 mi->dqi_flags = (mi->dqi_flags & ~DQF_MASK) | (ii->dqi_flags & DQF_MASK);
1642 spin_unlock(&dq_data_lock);
1643 mark_info_dirty(sb, type);
1644 /* Force write to disk */
1645 sb->dq_op->write_info(sb, type);
1646 up(&sb_dqopt(sb)->dqonoff_sem);
1647 return 0;
1650 struct quotactl_ops vfs_quotactl_ops = {
1651 .quota_on = vfs_quota_on,
1652 .quota_off = vfs_quota_off,
1653 .quota_sync = vfs_quota_sync,
1654 .get_info = vfs_get_dqinfo,
1655 .set_info = vfs_set_dqinfo,
1656 .get_dqblk = vfs_get_dqblk,
1657 .set_dqblk = vfs_set_dqblk
1660 static ctl_table fs_dqstats_table[] = {
1662 .ctl_name = FS_DQ_LOOKUPS,
1663 .procname = "lookups",
1664 .data = &dqstats.lookups,
1665 .maxlen = sizeof(int),
1666 .mode = 0444,
1667 .proc_handler = &proc_dointvec,
1670 .ctl_name = FS_DQ_DROPS,
1671 .procname = "drops",
1672 .data = &dqstats.drops,
1673 .maxlen = sizeof(int),
1674 .mode = 0444,
1675 .proc_handler = &proc_dointvec,
1678 .ctl_name = FS_DQ_READS,
1679 .procname = "reads",
1680 .data = &dqstats.reads,
1681 .maxlen = sizeof(int),
1682 .mode = 0444,
1683 .proc_handler = &proc_dointvec,
1686 .ctl_name = FS_DQ_WRITES,
1687 .procname = "writes",
1688 .data = &dqstats.writes,
1689 .maxlen = sizeof(int),
1690 .mode = 0444,
1691 .proc_handler = &proc_dointvec,
1694 .ctl_name = FS_DQ_CACHE_HITS,
1695 .procname = "cache_hits",
1696 .data = &dqstats.cache_hits,
1697 .maxlen = sizeof(int),
1698 .mode = 0444,
1699 .proc_handler = &proc_dointvec,
1702 .ctl_name = FS_DQ_ALLOCATED,
1703 .procname = "allocated_dquots",
1704 .data = &dqstats.allocated_dquots,
1705 .maxlen = sizeof(int),
1706 .mode = 0444,
1707 .proc_handler = &proc_dointvec,
1710 .ctl_name = FS_DQ_FREE,
1711 .procname = "free_dquots",
1712 .data = &dqstats.free_dquots,
1713 .maxlen = sizeof(int),
1714 .mode = 0444,
1715 .proc_handler = &proc_dointvec,
1718 .ctl_name = FS_DQ_SYNCS,
1719 .procname = "syncs",
1720 .data = &dqstats.syncs,
1721 .maxlen = sizeof(int),
1722 .mode = 0444,
1723 .proc_handler = &proc_dointvec,
1725 { .ctl_name = 0 },
1728 static ctl_table fs_table[] = {
1730 .ctl_name = FS_DQSTATS,
1731 .procname = "quota",
1732 .mode = 0555,
1733 .child = fs_dqstats_table,
1735 { .ctl_name = 0 },
1738 static ctl_table sys_table[] = {
1740 .ctl_name = CTL_FS,
1741 .procname = "fs",
1742 .mode = 0555,
1743 .child = fs_table,
1745 { .ctl_name = 0 },
1748 /* SLAB cache for dquot structures */
1749 kmem_cache_t *dquot_cachep;
1751 static int __init dquot_init(void)
1753 int i;
1754 unsigned long nr_hash, order;
1756 printk(KERN_NOTICE "VFS: Disk quotas %s\n", __DQUOT_VERSION__);
1758 register_sysctl_table(sys_table, 0);
1760 dquot_cachep = kmem_cache_create("dquot",
1761 sizeof(struct dquot), sizeof(unsigned long) * 4,
1762 SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT|SLAB_PANIC,
1763 NULL, NULL);
1765 order = 0;
1766 dquot_hash = (struct hlist_head *)__get_free_pages(GFP_ATOMIC, order);
1767 if (!dquot_hash)
1768 panic("Cannot create dquot hash table");
1770 /* Find power-of-two hlist_heads which can fit into allocation */
1771 nr_hash = (1UL << order) * PAGE_SIZE / sizeof(struct hlist_head);
1772 dq_hash_bits = 0;
1773 do {
1774 dq_hash_bits++;
1775 } while (nr_hash >> dq_hash_bits);
1776 dq_hash_bits--;
1778 nr_hash = 1UL << dq_hash_bits;
1779 dq_hash_mask = nr_hash - 1;
1780 for (i = 0; i < nr_hash; i++)
1781 INIT_HLIST_HEAD(dquot_hash + i);
1783 printk("Dquot-cache hash table entries: %ld (order %ld, %ld bytes)\n",
1784 nr_hash, order, (PAGE_SIZE << order));
1786 set_shrinker(DEFAULT_SEEKS, shrink_dqcache_memory);
1788 return 0;
1790 module_init(dquot_init);
1792 EXPORT_SYMBOL(register_quota_format);
1793 EXPORT_SYMBOL(unregister_quota_format);
1794 EXPORT_SYMBOL(dqstats);
1795 EXPORT_SYMBOL(dq_data_lock);
1796 EXPORT_SYMBOL(vfs_quota_on);
1797 EXPORT_SYMBOL(vfs_quota_on_mount);
1798 EXPORT_SYMBOL(vfs_quota_off);
1799 EXPORT_SYMBOL(vfs_quota_sync);
1800 EXPORT_SYMBOL(vfs_get_dqinfo);
1801 EXPORT_SYMBOL(vfs_set_dqinfo);
1802 EXPORT_SYMBOL(vfs_get_dqblk);
1803 EXPORT_SYMBOL(vfs_set_dqblk);
1804 EXPORT_SYMBOL(dquot_commit);
1805 EXPORT_SYMBOL(dquot_commit_info);
1806 EXPORT_SYMBOL(dquot_acquire);
1807 EXPORT_SYMBOL(dquot_release);
1808 EXPORT_SYMBOL(dquot_mark_dquot_dirty);
1809 EXPORT_SYMBOL(dquot_initialize);
1810 EXPORT_SYMBOL(dquot_drop);
1811 EXPORT_SYMBOL(dquot_alloc_space);
1812 EXPORT_SYMBOL(dquot_alloc_inode);
1813 EXPORT_SYMBOL(dquot_free_space);
1814 EXPORT_SYMBOL(dquot_free_inode);
1815 EXPORT_SYMBOL(dquot_transfer);