Fix gcc 4.5.1 miscompiling drivers/char/i8k.c (again)
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / fs / quota / dquot.c
blob395d14f868b2aaa95bdb7a6f66313d00696c2702
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 * Author: Marco van Wieringen <mvw@planets.elm.net>
14 * Fixes: Dmitry Gorodchanin <pgmdsg@ibi.com>, 11 Feb 96
16 * Revised list management to avoid races
17 * -- Bill Hawes, <whawes@star.net>, 9/98
19 * Fixed races in dquot_transfer(), dqget() and dquot_alloc_...().
20 * As the consequence the locking was moved from dquot_decr_...(),
21 * dquot_incr_...() to calling functions.
22 * invalidate_dquots() now writes modified dquots.
23 * Serialized quota_off() and quota_on() for mount point.
24 * Fixed a few bugs in grow_dquots().
25 * Fixed deadlock in write_dquot() - we no longer account quotas on
26 * quota files
27 * remove_dquot_ref() moved to inode.c - it now traverses through inodes
28 * add_dquot_ref() restarts after blocking
29 * Added check for bogus uid and fixed check for group in quotactl.
30 * Jan Kara, <jack@suse.cz>, sponsored by SuSE CR, 10-11/99
32 * Used struct list_head instead of own list struct
33 * Invalidation of referenced dquots is no longer possible
34 * Improved free_dquots list management
35 * Quota and i_blocks are now updated in one place to avoid races
36 * Warnings are now delayed so we won't block in critical section
37 * Write updated not to require dquot lock
38 * Jan Kara, <jack@suse.cz>, 9/2000
40 * Added dynamic quota structure allocation
41 * Jan Kara <jack@suse.cz> 12/2000
43 * Rewritten quota interface. Implemented new quota format and
44 * formats registering.
45 * Jan Kara, <jack@suse.cz>, 2001,2002
47 * New SMP locking.
48 * Jan Kara, <jack@suse.cz>, 10/2002
50 * Added journalled quota support, fix lock inversion problems
51 * Jan Kara, <jack@suse.cz>, 2003,2004
53 * (C) Copyright 1994 - 1997 Marco van Wieringen
56 #include <linux/errno.h>
57 #include <linux/kernel.h>
58 #include <linux/fs.h>
59 #include <linux/mount.h>
60 #include <linux/mm.h>
61 #include <linux/time.h>
62 #include <linux/types.h>
63 #include <linux/string.h>
64 #include <linux/fcntl.h>
65 #include <linux/stat.h>
66 #include <linux/tty.h>
67 #include <linux/file.h>
68 #include <linux/slab.h>
69 #include <linux/sysctl.h>
70 #include <linux/init.h>
71 #include <linux/module.h>
72 #include <linux/proc_fs.h>
73 #include <linux/security.h>
74 #include <linux/kmod.h>
75 #include <linux/namei.h>
76 #include <linux/buffer_head.h>
77 #include <linux/capability.h>
78 #include <linux/quotaops.h>
79 #include <linux/writeback.h> /* for inode_lock, oddly enough.. */
81 #include <asm/uaccess.h>
83 #define __DQUOT_PARANOIA
86 * There are three quota SMP locks. dq_list_lock protects all lists with quotas
87 * and quota formats, dqstats structure containing statistics about the lists
88 * dq_data_lock protects data from dq_dqb and also mem_dqinfo structures and
89 * 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(). dq_state_lock protects
92 * modifications of quota state (on quotaon and quotaoff) and readers who care
93 * about latest values take it as well.
95 * The spinlock ordering is hence: dq_data_lock > dq_list_lock > i_lock,
96 * dq_list_lock > dq_state_lock
98 * Note that some things (eg. sb pointer, type, id) doesn't change during
99 * the life of the dquot structure and so needn't to be protected by a lock
101 * Any operation working on dquots via inode pointers must hold dqptr_sem. If
102 * operation is just reading pointers from inode (or not using them at all) the
103 * read lock is enough. If pointers are altered function must hold write lock
104 * (these locking rules also apply for S_NOQUOTA flag in the inode - note that
105 * for altering the flag i_mutex is also needed).
107 * Each dquot has its dq_lock mutex. Locked dquots might not be referenced
108 * from inodes (dquot_alloc_space() and such don't check the dq_lock).
109 * Currently dquot is locked only when it is being read to memory (or space for
110 * it is being allocated) on the first dqget() and when it is being released on
111 * the last dqput(). The allocation and release oparations are serialized by
112 * the dq_lock and by checking the use count in dquot_release(). Write
113 * operations on dquots don't hold dq_lock as they copy data under dq_data_lock
114 * spinlock to internal buffers before writing.
116 * Lock ordering (including related VFS locks) is the following:
117 * i_mutex > dqonoff_sem > journal_lock > dqptr_sem > dquot->dq_lock >
118 * dqio_mutex
119 * The lock ordering of dqptr_sem imposed by quota code is only dqonoff_sem >
120 * dqptr_sem. But filesystem has to count with the fact that functions such as
121 * dquot_alloc_space() acquire dqptr_sem and they usually have to be called
122 * from inside a transaction to keep filesystem consistency after a crash. Also
123 * filesystems usually want to do some IO on dquot from ->mark_dirty which is
124 * called with dqptr_sem held.
125 * i_mutex on quota files is special (it's below dqio_mutex)
128 static __cacheline_aligned_in_smp DEFINE_SPINLOCK(dq_list_lock);
129 static __cacheline_aligned_in_smp DEFINE_SPINLOCK(dq_state_lock);
130 __cacheline_aligned_in_smp DEFINE_SPINLOCK(dq_data_lock);
131 EXPORT_SYMBOL(dq_data_lock);
133 static char *quotatypes[] = INITQFNAMES;
134 static struct quota_format_type *quota_formats; /* List of registered formats */
135 static struct quota_module_name module_names[] = INIT_QUOTA_MODULE_NAMES;
137 /* SLAB cache for dquot structures */
138 static struct kmem_cache *dquot_cachep;
140 int register_quota_format(struct quota_format_type *fmt)
142 spin_lock(&dq_list_lock);
143 fmt->qf_next = quota_formats;
144 quota_formats = fmt;
145 spin_unlock(&dq_list_lock);
146 return 0;
148 EXPORT_SYMBOL(register_quota_format);
150 void unregister_quota_format(struct quota_format_type *fmt)
152 struct quota_format_type **actqf;
154 spin_lock(&dq_list_lock);
155 for (actqf = &quota_formats; *actqf && *actqf != fmt;
156 actqf = &(*actqf)->qf_next)
158 if (*actqf)
159 *actqf = (*actqf)->qf_next;
160 spin_unlock(&dq_list_lock);
162 EXPORT_SYMBOL(unregister_quota_format);
164 static struct quota_format_type *find_quota_format(int id)
166 struct quota_format_type *actqf;
168 spin_lock(&dq_list_lock);
169 for (actqf = quota_formats; actqf && actqf->qf_fmt_id != id;
170 actqf = actqf->qf_next)
172 if (!actqf || !try_module_get(actqf->qf_owner)) {
173 int qm;
175 spin_unlock(&dq_list_lock);
177 for (qm = 0; module_names[qm].qm_fmt_id &&
178 module_names[qm].qm_fmt_id != id; qm++)
180 if (!module_names[qm].qm_fmt_id ||
181 request_module(module_names[qm].qm_mod_name))
182 return NULL;
184 spin_lock(&dq_list_lock);
185 for (actqf = quota_formats; actqf && actqf->qf_fmt_id != id;
186 actqf = actqf->qf_next)
188 if (actqf && !try_module_get(actqf->qf_owner))
189 actqf = NULL;
191 spin_unlock(&dq_list_lock);
192 return actqf;
195 static void put_quota_format(struct quota_format_type *fmt)
197 module_put(fmt->qf_owner);
201 * Dquot List Management:
202 * The quota code uses three lists for dquot management: the inuse_list,
203 * free_dquots, and dquot_hash[] array. A single dquot structure may be
204 * on all three lists, depending on its current state.
206 * All dquots are placed to the end of inuse_list when first created, and this
207 * list is used for invalidate operation, which must look at every dquot.
209 * Unused dquots (dq_count == 0) are added to the free_dquots list when freed,
210 * and this list is searched whenever we need an available dquot. Dquots are
211 * removed from the list as soon as they are used again, and
212 * dqstats.free_dquots gives the number of dquots on the list. When
213 * dquot is invalidated it's completely released from memory.
215 * Dquots with a specific identity (device, type and id) are placed on
216 * one of the dquot_hash[] hash chains. The provides an efficient search
217 * mechanism to locate a specific dquot.
220 static LIST_HEAD(inuse_list);
221 static LIST_HEAD(free_dquots);
222 static unsigned int dq_hash_bits, dq_hash_mask;
223 static struct hlist_head *dquot_hash;
225 struct dqstats dqstats;
226 EXPORT_SYMBOL(dqstats);
228 static qsize_t inode_get_rsv_space(struct inode *inode);
230 static inline unsigned int
231 hashfn(const struct super_block *sb, unsigned int id, int type)
233 unsigned long tmp;
235 tmp = (((unsigned long)sb>>L1_CACHE_SHIFT) ^ id) * (MAXQUOTAS - type);
236 return (tmp + (tmp >> dq_hash_bits)) & dq_hash_mask;
240 * Following list functions expect dq_list_lock to be held
242 static inline void insert_dquot_hash(struct dquot *dquot)
244 struct hlist_head *head;
245 head = dquot_hash + hashfn(dquot->dq_sb, dquot->dq_id, dquot->dq_type);
246 hlist_add_head(&dquot->dq_hash, head);
249 static inline void remove_dquot_hash(struct dquot *dquot)
251 hlist_del_init(&dquot->dq_hash);
254 static struct dquot *find_dquot(unsigned int hashent, struct super_block *sb,
255 unsigned int id, int type)
257 struct hlist_node *node;
258 struct dquot *dquot;
260 hlist_for_each (node, dquot_hash+hashent) {
261 dquot = hlist_entry(node, struct dquot, dq_hash);
262 if (dquot->dq_sb == sb && dquot->dq_id == id &&
263 dquot->dq_type == type)
264 return dquot;
266 return NULL;
269 /* Add a dquot to the tail of the free list */
270 static inline void put_dquot_last(struct dquot *dquot)
272 list_add_tail(&dquot->dq_free, &free_dquots);
273 dqstats.free_dquots++;
276 static inline void remove_free_dquot(struct dquot *dquot)
278 if (list_empty(&dquot->dq_free))
279 return;
280 list_del_init(&dquot->dq_free);
281 dqstats.free_dquots--;
284 static inline void put_inuse(struct dquot *dquot)
286 /* We add to the back of inuse list so we don't have to restart
287 * when traversing this list and we block */
288 list_add_tail(&dquot->dq_inuse, &inuse_list);
289 dqstats.allocated_dquots++;
292 static inline void remove_inuse(struct dquot *dquot)
294 dqstats.allocated_dquots--;
295 list_del(&dquot->dq_inuse);
298 * End of list functions needing dq_list_lock
301 static void wait_on_dquot(struct dquot *dquot)
303 mutex_lock(&dquot->dq_lock);
304 mutex_unlock(&dquot->dq_lock);
307 static inline int dquot_dirty(struct dquot *dquot)
309 return test_bit(DQ_MOD_B, &dquot->dq_flags);
312 static inline int mark_dquot_dirty(struct dquot *dquot)
314 return dquot->dq_sb->dq_op->mark_dirty(dquot);
317 int dquot_mark_dquot_dirty(struct dquot *dquot)
319 spin_lock(&dq_list_lock);
320 if (!test_and_set_bit(DQ_MOD_B, &dquot->dq_flags))
321 list_add(&dquot->dq_dirty, &sb_dqopt(dquot->dq_sb)->
322 info[dquot->dq_type].dqi_dirty_list);
323 spin_unlock(&dq_list_lock);
324 return 0;
326 EXPORT_SYMBOL(dquot_mark_dquot_dirty);
328 /* Dirtify all the dquots - this can block when journalling */
329 static inline int mark_all_dquot_dirty(struct dquot * const *dquot)
331 int ret, err, cnt;
333 ret = err = 0;
334 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
335 if (dquot[cnt])
336 /* Even in case of error we have to continue */
337 ret = mark_dquot_dirty(dquot[cnt]);
338 if (!err)
339 err = ret;
341 return err;
344 static inline void dqput_all(struct dquot **dquot)
346 unsigned int cnt;
348 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
349 dqput(dquot[cnt]);
352 /* This function needs dq_list_lock */
353 static inline int clear_dquot_dirty(struct dquot *dquot)
355 if (!test_and_clear_bit(DQ_MOD_B, &dquot->dq_flags))
356 return 0;
357 list_del_init(&dquot->dq_dirty);
358 return 1;
361 void mark_info_dirty(struct super_block *sb, int type)
363 set_bit(DQF_INFO_DIRTY_B, &sb_dqopt(sb)->info[type].dqi_flags);
365 EXPORT_SYMBOL(mark_info_dirty);
368 * Read dquot from disk and alloc space for it
371 int dquot_acquire(struct dquot *dquot)
373 int ret = 0, ret2 = 0;
374 struct quota_info *dqopt = sb_dqopt(dquot->dq_sb);
376 mutex_lock(&dquot->dq_lock);
377 mutex_lock(&dqopt->dqio_mutex);
378 if (!test_bit(DQ_READ_B, &dquot->dq_flags))
379 ret = dqopt->ops[dquot->dq_type]->read_dqblk(dquot);
380 if (ret < 0)
381 goto out_iolock;
382 set_bit(DQ_READ_B, &dquot->dq_flags);
383 /* Instantiate dquot if needed */
384 if (!test_bit(DQ_ACTIVE_B, &dquot->dq_flags) && !dquot->dq_off) {
385 ret = dqopt->ops[dquot->dq_type]->commit_dqblk(dquot);
386 /* Write the info if needed */
387 if (info_dirty(&dqopt->info[dquot->dq_type])) {
388 ret2 = dqopt->ops[dquot->dq_type]->write_file_info(
389 dquot->dq_sb, dquot->dq_type);
391 if (ret < 0)
392 goto out_iolock;
393 if (ret2 < 0) {
394 ret = ret2;
395 goto out_iolock;
398 set_bit(DQ_ACTIVE_B, &dquot->dq_flags);
399 out_iolock:
400 mutex_unlock(&dqopt->dqio_mutex);
401 mutex_unlock(&dquot->dq_lock);
402 return ret;
404 EXPORT_SYMBOL(dquot_acquire);
407 * Write dquot to disk
409 int dquot_commit(struct dquot *dquot)
411 int ret = 0;
412 struct quota_info *dqopt = sb_dqopt(dquot->dq_sb);
414 mutex_lock(&dqopt->dqio_mutex);
415 spin_lock(&dq_list_lock);
416 if (!clear_dquot_dirty(dquot)) {
417 spin_unlock(&dq_list_lock);
418 goto out_sem;
420 spin_unlock(&dq_list_lock);
421 /* Inactive dquot can be only if there was error during read/init
422 * => we have better not writing it */
423 if (test_bit(DQ_ACTIVE_B, &dquot->dq_flags))
424 ret = dqopt->ops[dquot->dq_type]->commit_dqblk(dquot);
425 else
426 ret = -EIO;
427 out_sem:
428 mutex_unlock(&dqopt->dqio_mutex);
429 return ret;
431 EXPORT_SYMBOL(dquot_commit);
434 * Release dquot
436 int dquot_release(struct dquot *dquot)
438 int ret = 0, ret2 = 0;
439 struct quota_info *dqopt = sb_dqopt(dquot->dq_sb);
441 mutex_lock(&dquot->dq_lock);
442 /* Check whether we are not racing with some other dqget() */
443 if (atomic_read(&dquot->dq_count) > 1)
444 goto out_dqlock;
445 mutex_lock(&dqopt->dqio_mutex);
446 if (dqopt->ops[dquot->dq_type]->release_dqblk) {
447 ret = dqopt->ops[dquot->dq_type]->release_dqblk(dquot);
448 /* Write the info */
449 if (info_dirty(&dqopt->info[dquot->dq_type])) {
450 ret2 = dqopt->ops[dquot->dq_type]->write_file_info(
451 dquot->dq_sb, dquot->dq_type);
453 if (ret >= 0)
454 ret = ret2;
456 clear_bit(DQ_ACTIVE_B, &dquot->dq_flags);
457 mutex_unlock(&dqopt->dqio_mutex);
458 out_dqlock:
459 mutex_unlock(&dquot->dq_lock);
460 return ret;
462 EXPORT_SYMBOL(dquot_release);
464 void dquot_destroy(struct dquot *dquot)
466 kmem_cache_free(dquot_cachep, dquot);
468 EXPORT_SYMBOL(dquot_destroy);
470 static inline void do_destroy_dquot(struct dquot *dquot)
472 dquot->dq_sb->dq_op->destroy_dquot(dquot);
475 /* Invalidate all dquots on the list. Note that this function is called after
476 * quota is disabled and pointers from inodes removed so there cannot be new
477 * quota users. There can still be some users of quotas due to inodes being
478 * just deleted or pruned by prune_icache() (those are not attached to any
479 * list) or parallel quotactl call. We have to wait for such users.
481 static void invalidate_dquots(struct super_block *sb, int type)
483 struct dquot *dquot, *tmp;
485 restart:
486 spin_lock(&dq_list_lock);
487 list_for_each_entry_safe(dquot, tmp, &inuse_list, dq_inuse) {
488 if (dquot->dq_sb != sb)
489 continue;
490 if (dquot->dq_type != type)
491 continue;
492 /* Wait for dquot users */
493 if (atomic_read(&dquot->dq_count)) {
494 DEFINE_WAIT(wait);
496 atomic_inc(&dquot->dq_count);
497 prepare_to_wait(&dquot->dq_wait_unused, &wait,
498 TASK_UNINTERRUPTIBLE);
499 spin_unlock(&dq_list_lock);
500 /* Once dqput() wakes us up, we know it's time to free
501 * the dquot.
502 * IMPORTANT: we rely on the fact that there is always
503 * at most one process waiting for dquot to free.
504 * Otherwise dq_count would be > 1 and we would never
505 * wake up.
507 if (atomic_read(&dquot->dq_count) > 1)
508 schedule();
509 finish_wait(&dquot->dq_wait_unused, &wait);
510 dqput(dquot);
511 /* At this moment dquot() need not exist (it could be
512 * reclaimed by prune_dqcache(). Hence we must
513 * restart. */
514 goto restart;
517 * Quota now has no users and it has been written on last
518 * dqput()
520 remove_dquot_hash(dquot);
521 remove_free_dquot(dquot);
522 remove_inuse(dquot);
523 do_destroy_dquot(dquot);
525 spin_unlock(&dq_list_lock);
528 /* Call callback for every active dquot on given filesystem */
529 int dquot_scan_active(struct super_block *sb,
530 int (*fn)(struct dquot *dquot, unsigned long priv),
531 unsigned long priv)
533 struct dquot *dquot, *old_dquot = NULL;
534 int ret = 0;
536 mutex_lock(&sb_dqopt(sb)->dqonoff_mutex);
537 spin_lock(&dq_list_lock);
538 list_for_each_entry(dquot, &inuse_list, dq_inuse) {
539 if (!test_bit(DQ_ACTIVE_B, &dquot->dq_flags))
540 continue;
541 if (dquot->dq_sb != sb)
542 continue;
543 /* Now we have active dquot so we can just increase use count */
544 atomic_inc(&dquot->dq_count);
545 dqstats.lookups++;
546 spin_unlock(&dq_list_lock);
547 dqput(old_dquot);
548 old_dquot = dquot;
549 ret = fn(dquot, priv);
550 if (ret < 0)
551 goto out;
552 spin_lock(&dq_list_lock);
553 /* We are safe to continue now because our dquot could not
554 * be moved out of the inuse list while we hold the reference */
556 spin_unlock(&dq_list_lock);
557 out:
558 dqput(old_dquot);
559 mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
560 return ret;
562 EXPORT_SYMBOL(dquot_scan_active);
564 int vfs_quota_sync(struct super_block *sb, int type)
566 struct list_head *dirty;
567 struct dquot *dquot;
568 struct quota_info *dqopt = sb_dqopt(sb);
569 int cnt;
571 mutex_lock(&dqopt->dqonoff_mutex);
572 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
573 if (type != -1 && cnt != type)
574 continue;
575 if (!sb_has_quota_active(sb, cnt))
576 continue;
577 spin_lock(&dq_list_lock);
578 dirty = &dqopt->info[cnt].dqi_dirty_list;
579 while (!list_empty(dirty)) {
580 dquot = list_first_entry(dirty, struct dquot,
581 dq_dirty);
582 /* Dirty and inactive can be only bad dquot... */
583 if (!test_bit(DQ_ACTIVE_B, &dquot->dq_flags)) {
584 clear_dquot_dirty(dquot);
585 continue;
587 /* Now we have active dquot from which someone is
588 * holding reference so we can safely just increase
589 * use count */
590 atomic_inc(&dquot->dq_count);
591 dqstats.lookups++;
592 spin_unlock(&dq_list_lock);
593 sb->dq_op->write_dquot(dquot);
594 dqput(dquot);
595 spin_lock(&dq_list_lock);
597 spin_unlock(&dq_list_lock);
600 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
601 if ((cnt == type || type == -1) && sb_has_quota_active(sb, cnt)
602 && info_dirty(&dqopt->info[cnt]))
603 sb->dq_op->write_info(sb, cnt);
604 spin_lock(&dq_list_lock);
605 dqstats.syncs++;
606 spin_unlock(&dq_list_lock);
607 mutex_unlock(&dqopt->dqonoff_mutex);
609 return 0;
611 EXPORT_SYMBOL(vfs_quota_sync);
613 /* Free unused dquots from cache */
614 static void prune_dqcache(int count)
616 struct list_head *head;
617 struct dquot *dquot;
619 head = free_dquots.prev;
620 while (head != &free_dquots && count) {
621 dquot = list_entry(head, struct dquot, dq_free);
622 remove_dquot_hash(dquot);
623 remove_free_dquot(dquot);
624 remove_inuse(dquot);
625 do_destroy_dquot(dquot);
626 count--;
627 head = free_dquots.prev;
632 * This is called from kswapd when we think we need some
633 * more memory
636 static int shrink_dqcache_memory(int nr, gfp_t gfp_mask)
638 if (nr) {
639 spin_lock(&dq_list_lock);
640 prune_dqcache(nr);
641 spin_unlock(&dq_list_lock);
643 return (dqstats.free_dquots / 100) * sysctl_vfs_cache_pressure;
646 static struct shrinker dqcache_shrinker = {
647 .shrink = shrink_dqcache_memory,
648 .seeks = DEFAULT_SEEKS,
652 * Put reference to dquot
653 * NOTE: If you change this function please check whether dqput_blocks() works right...
655 void dqput(struct dquot *dquot)
657 int ret;
659 if (!dquot)
660 return;
661 #ifdef __DQUOT_PARANOIA
662 if (!atomic_read(&dquot->dq_count)) {
663 printk("VFS: dqput: trying to free free dquot\n");
664 printk("VFS: device %s, dquot of %s %d\n",
665 dquot->dq_sb->s_id,
666 quotatypes[dquot->dq_type],
667 dquot->dq_id);
668 BUG();
670 #endif
672 spin_lock(&dq_list_lock);
673 dqstats.drops++;
674 spin_unlock(&dq_list_lock);
675 we_slept:
676 spin_lock(&dq_list_lock);
677 if (atomic_read(&dquot->dq_count) > 1) {
678 /* We have more than one user... nothing to do */
679 atomic_dec(&dquot->dq_count);
680 /* Releasing dquot during quotaoff phase? */
681 if (!sb_has_quota_active(dquot->dq_sb, dquot->dq_type) &&
682 atomic_read(&dquot->dq_count) == 1)
683 wake_up(&dquot->dq_wait_unused);
684 spin_unlock(&dq_list_lock);
685 return;
687 /* Need to release dquot? */
688 if (test_bit(DQ_ACTIVE_B, &dquot->dq_flags) && dquot_dirty(dquot)) {
689 spin_unlock(&dq_list_lock);
690 /* Commit dquot before releasing */
691 ret = dquot->dq_sb->dq_op->write_dquot(dquot);
692 if (ret < 0) {
693 printk(KERN_ERR "VFS: cannot write quota structure on "
694 "device %s (error %d). Quota may get out of "
695 "sync!\n", dquot->dq_sb->s_id, ret);
697 * We clear dirty bit anyway, so that we avoid
698 * infinite loop here
700 spin_lock(&dq_list_lock);
701 clear_dquot_dirty(dquot);
702 spin_unlock(&dq_list_lock);
704 goto we_slept;
706 /* Clear flag in case dquot was inactive (something bad happened) */
707 clear_dquot_dirty(dquot);
708 if (test_bit(DQ_ACTIVE_B, &dquot->dq_flags)) {
709 spin_unlock(&dq_list_lock);
710 dquot->dq_sb->dq_op->release_dquot(dquot);
711 goto we_slept;
713 atomic_dec(&dquot->dq_count);
714 #ifdef __DQUOT_PARANOIA
715 /* sanity check */
716 BUG_ON(!list_empty(&dquot->dq_free));
717 #endif
718 put_dquot_last(dquot);
719 spin_unlock(&dq_list_lock);
721 EXPORT_SYMBOL(dqput);
723 struct dquot *dquot_alloc(struct super_block *sb, int type)
725 return kmem_cache_zalloc(dquot_cachep, GFP_NOFS);
727 EXPORT_SYMBOL(dquot_alloc);
729 static struct dquot *get_empty_dquot(struct super_block *sb, int type)
731 struct dquot *dquot;
733 dquot = sb->dq_op->alloc_dquot(sb, type);
734 if(!dquot)
735 return NULL;
737 mutex_init(&dquot->dq_lock);
738 INIT_LIST_HEAD(&dquot->dq_free);
739 INIT_LIST_HEAD(&dquot->dq_inuse);
740 INIT_HLIST_NODE(&dquot->dq_hash);
741 INIT_LIST_HEAD(&dquot->dq_dirty);
742 init_waitqueue_head(&dquot->dq_wait_unused);
743 dquot->dq_sb = sb;
744 dquot->dq_type = type;
745 atomic_set(&dquot->dq_count, 1);
747 return dquot;
751 * Get reference to dquot
753 * Locking is slightly tricky here. We are guarded from parallel quotaoff()
754 * destroying our dquot by:
755 * a) checking for quota flags under dq_list_lock and
756 * b) getting a reference to dquot before we release dq_list_lock
758 struct dquot *dqget(struct super_block *sb, unsigned int id, int type)
760 unsigned int hashent = hashfn(sb, id, type);
761 struct dquot *dquot = NULL, *empty = NULL;
763 if (!sb_has_quota_active(sb, type))
764 return NULL;
765 we_slept:
766 spin_lock(&dq_list_lock);
767 spin_lock(&dq_state_lock);
768 if (!sb_has_quota_active(sb, type)) {
769 spin_unlock(&dq_state_lock);
770 spin_unlock(&dq_list_lock);
771 goto out;
773 spin_unlock(&dq_state_lock);
775 dquot = find_dquot(hashent, sb, id, type);
776 if (!dquot) {
777 if (!empty) {
778 spin_unlock(&dq_list_lock);
779 empty = get_empty_dquot(sb, type);
780 if (!empty)
781 schedule(); /* Try to wait for a moment... */
782 goto we_slept;
784 dquot = empty;
785 empty = NULL;
786 dquot->dq_id = id;
787 /* all dquots go on the inuse_list */
788 put_inuse(dquot);
789 /* hash it first so it can be found */
790 insert_dquot_hash(dquot);
791 dqstats.lookups++;
792 spin_unlock(&dq_list_lock);
793 } else {
794 if (!atomic_read(&dquot->dq_count))
795 remove_free_dquot(dquot);
796 atomic_inc(&dquot->dq_count);
797 dqstats.cache_hits++;
798 dqstats.lookups++;
799 spin_unlock(&dq_list_lock);
801 /* Wait for dq_lock - after this we know that either dquot_release() is
802 * already finished or it will be canceled due to dq_count > 1 test */
803 wait_on_dquot(dquot);
804 /* Read the dquot / allocate space in quota file */
805 if (!test_bit(DQ_ACTIVE_B, &dquot->dq_flags) &&
806 sb->dq_op->acquire_dquot(dquot) < 0) {
807 dqput(dquot);
808 dquot = NULL;
809 goto out;
811 #ifdef __DQUOT_PARANOIA
812 BUG_ON(!dquot->dq_sb); /* Has somebody invalidated entry under us? */
813 #endif
814 out:
815 if (empty)
816 do_destroy_dquot(empty);
818 return dquot;
820 EXPORT_SYMBOL(dqget);
822 static int dqinit_needed(struct inode *inode, int type)
824 int cnt;
826 if (IS_NOQUOTA(inode))
827 return 0;
828 if (type != -1)
829 return !inode->i_dquot[type];
830 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
831 if (!inode->i_dquot[cnt])
832 return 1;
833 return 0;
836 /* This routine is guarded by dqonoff_mutex mutex */
837 static void add_dquot_ref(struct super_block *sb, int type)
839 struct inode *inode, *old_inode = NULL;
840 int reserved = 0;
842 spin_lock(&inode_lock);
843 list_for_each_entry(inode, &sb->s_inodes, i_sb_list) {
844 if (inode->i_state & (I_FREEING|I_CLEAR|I_WILL_FREE|I_NEW))
845 continue;
846 if (unlikely(inode_get_rsv_space(inode) > 0))
847 reserved = 1;
848 if (!atomic_read(&inode->i_writecount))
849 continue;
850 if (!dqinit_needed(inode, type))
851 continue;
853 __iget(inode);
854 spin_unlock(&inode_lock);
856 iput(old_inode);
857 sb->dq_op->initialize(inode, type);
858 /* We hold a reference to 'inode' so it couldn't have been
859 * removed from s_inodes list while we dropped the inode_lock.
860 * We cannot iput the inode now as we can be holding the last
861 * reference and we cannot iput it under inode_lock. So we
862 * keep the reference and iput it later. */
863 old_inode = inode;
864 spin_lock(&inode_lock);
866 spin_unlock(&inode_lock);
867 iput(old_inode);
869 if (reserved) {
870 printk(KERN_WARNING "VFS (%s): Writes happened before quota"
871 " was turned on thus quota information is probably "
872 "inconsistent. Please run quotacheck(8).\n", sb->s_id);
877 * Return 0 if dqput() won't block.
878 * (note that 1 doesn't necessarily mean blocking)
880 static inline int dqput_blocks(struct dquot *dquot)
882 if (atomic_read(&dquot->dq_count) <= 1)
883 return 1;
884 return 0;
888 * Remove references to dquots from inode and add dquot to list for freeing
889 * if we have the last referece to dquot
890 * We can't race with anybody because we hold dqptr_sem for writing...
892 static int remove_inode_dquot_ref(struct inode *inode, int type,
893 struct list_head *tofree_head)
895 struct dquot *dquot = inode->i_dquot[type];
897 inode->i_dquot[type] = NULL;
898 if (dquot) {
899 if (dqput_blocks(dquot)) {
900 #ifdef __DQUOT_PARANOIA
901 if (atomic_read(&dquot->dq_count) != 1)
902 printk(KERN_WARNING "VFS: Adding dquot with dq_count %d to dispose list.\n", atomic_read(&dquot->dq_count));
903 #endif
904 spin_lock(&dq_list_lock);
905 /* As dquot must have currently users it can't be on
906 * the free list... */
907 list_add(&dquot->dq_free, tofree_head);
908 spin_unlock(&dq_list_lock);
909 return 1;
911 else
912 dqput(dquot); /* We have guaranteed we won't block */
914 return 0;
918 * Free list of dquots
919 * Dquots are removed from inodes and no new references can be got so we are
920 * the only ones holding reference
922 static void put_dquot_list(struct list_head *tofree_head)
924 struct list_head *act_head;
925 struct dquot *dquot;
927 act_head = tofree_head->next;
928 while (act_head != tofree_head) {
929 dquot = list_entry(act_head, struct dquot, dq_free);
930 act_head = act_head->next;
931 /* Remove dquot from the list so we won't have problems... */
932 list_del_init(&dquot->dq_free);
933 dqput(dquot);
937 static void remove_dquot_ref(struct super_block *sb, int type,
938 struct list_head *tofree_head)
940 struct inode *inode;
942 spin_lock(&inode_lock);
943 list_for_each_entry(inode, &sb->s_inodes, i_sb_list) {
945 * We have to scan also I_NEW inodes because they can already
946 * have quota pointer initialized. Luckily, we need to touch
947 * only quota pointers and these have separate locking
948 * (dqptr_sem).
950 if (!IS_NOQUOTA(inode))
951 remove_inode_dquot_ref(inode, type, tofree_head);
953 spin_unlock(&inode_lock);
956 /* Gather all references from inodes and drop them */
957 static void drop_dquot_ref(struct super_block *sb, int type)
959 LIST_HEAD(tofree_head);
961 if (sb->dq_op) {
962 down_write(&sb_dqopt(sb)->dqptr_sem);
963 remove_dquot_ref(sb, type, &tofree_head);
964 up_write(&sb_dqopt(sb)->dqptr_sem);
965 put_dquot_list(&tofree_head);
969 static inline void dquot_incr_inodes(struct dquot *dquot, qsize_t number)
971 dquot->dq_dqb.dqb_curinodes += number;
974 static inline void dquot_incr_space(struct dquot *dquot, qsize_t number)
976 dquot->dq_dqb.dqb_curspace += number;
979 static inline void dquot_resv_space(struct dquot *dquot, qsize_t number)
981 dquot->dq_dqb.dqb_rsvspace += number;
985 * Claim reserved quota space
987 static void dquot_claim_reserved_space(struct dquot *dquot, qsize_t number)
989 if (dquot->dq_dqb.dqb_rsvspace < number) {
990 WARN_ON_ONCE(1);
991 number = dquot->dq_dqb.dqb_rsvspace;
993 dquot->dq_dqb.dqb_curspace += number;
994 dquot->dq_dqb.dqb_rsvspace -= number;
997 static inline
998 void dquot_free_reserved_space(struct dquot *dquot, qsize_t number)
1000 if (dquot->dq_dqb.dqb_rsvspace >= number)
1001 dquot->dq_dqb.dqb_rsvspace -= number;
1002 else {
1003 WARN_ON_ONCE(1);
1004 dquot->dq_dqb.dqb_rsvspace = 0;
1008 static void dquot_decr_inodes(struct dquot *dquot, qsize_t number)
1010 if (sb_dqopt(dquot->dq_sb)->flags & DQUOT_NEGATIVE_USAGE ||
1011 dquot->dq_dqb.dqb_curinodes >= number)
1012 dquot->dq_dqb.dqb_curinodes -= number;
1013 else
1014 dquot->dq_dqb.dqb_curinodes = 0;
1015 if (dquot->dq_dqb.dqb_curinodes <= dquot->dq_dqb.dqb_isoftlimit)
1016 dquot->dq_dqb.dqb_itime = (time_t) 0;
1017 clear_bit(DQ_INODES_B, &dquot->dq_flags);
1020 static void dquot_decr_space(struct dquot *dquot, qsize_t number)
1022 if (sb_dqopt(dquot->dq_sb)->flags & DQUOT_NEGATIVE_USAGE ||
1023 dquot->dq_dqb.dqb_curspace >= number)
1024 dquot->dq_dqb.dqb_curspace -= number;
1025 else
1026 dquot->dq_dqb.dqb_curspace = 0;
1027 if (dquot->dq_dqb.dqb_curspace <= dquot->dq_dqb.dqb_bsoftlimit)
1028 dquot->dq_dqb.dqb_btime = (time_t) 0;
1029 clear_bit(DQ_BLKS_B, &dquot->dq_flags);
1032 static int warning_issued(struct dquot *dquot, const int warntype)
1034 int flag = (warntype == QUOTA_NL_BHARDWARN ||
1035 warntype == QUOTA_NL_BSOFTLONGWARN) ? DQ_BLKS_B :
1036 ((warntype == QUOTA_NL_IHARDWARN ||
1037 warntype == QUOTA_NL_ISOFTLONGWARN) ? DQ_INODES_B : 0);
1039 if (!flag)
1040 return 0;
1041 return test_and_set_bit(flag, &dquot->dq_flags);
1044 #ifdef CONFIG_PRINT_QUOTA_WARNING
1045 static int flag_print_warnings = 1;
1047 static int need_print_warning(struct dquot *dquot)
1049 if (!flag_print_warnings)
1050 return 0;
1052 switch (dquot->dq_type) {
1053 case USRQUOTA:
1054 return current_fsuid() == dquot->dq_id;
1055 case GRPQUOTA:
1056 return in_group_p(dquot->dq_id);
1058 return 0;
1061 /* Print warning to user which exceeded quota */
1062 static void print_warning(struct dquot *dquot, const int warntype)
1064 char *msg = NULL;
1065 struct tty_struct *tty;
1067 if (warntype == QUOTA_NL_IHARDBELOW ||
1068 warntype == QUOTA_NL_ISOFTBELOW ||
1069 warntype == QUOTA_NL_BHARDBELOW ||
1070 warntype == QUOTA_NL_BSOFTBELOW || !need_print_warning(dquot))
1071 return;
1073 tty = get_current_tty();
1074 if (!tty)
1075 return;
1076 tty_write_message(tty, dquot->dq_sb->s_id);
1077 if (warntype == QUOTA_NL_ISOFTWARN || warntype == QUOTA_NL_BSOFTWARN)
1078 tty_write_message(tty, ": warning, ");
1079 else
1080 tty_write_message(tty, ": write failed, ");
1081 tty_write_message(tty, quotatypes[dquot->dq_type]);
1082 switch (warntype) {
1083 case QUOTA_NL_IHARDWARN:
1084 msg = " file limit reached.\r\n";
1085 break;
1086 case QUOTA_NL_ISOFTLONGWARN:
1087 msg = " file quota exceeded too long.\r\n";
1088 break;
1089 case QUOTA_NL_ISOFTWARN:
1090 msg = " file quota exceeded.\r\n";
1091 break;
1092 case QUOTA_NL_BHARDWARN:
1093 msg = " block limit reached.\r\n";
1094 break;
1095 case QUOTA_NL_BSOFTLONGWARN:
1096 msg = " block quota exceeded too long.\r\n";
1097 break;
1098 case QUOTA_NL_BSOFTWARN:
1099 msg = " block quota exceeded.\r\n";
1100 break;
1102 tty_write_message(tty, msg);
1103 tty_kref_put(tty);
1105 #endif
1108 * Write warnings to the console and send warning messages over netlink.
1110 * Note that this function can sleep.
1112 static void flush_warnings(struct dquot *const *dquots, char *warntype)
1114 struct dquot *dq;
1115 int i;
1117 for (i = 0; i < MAXQUOTAS; i++) {
1118 dq = dquots[i];
1119 if (dq && warntype[i] != QUOTA_NL_NOWARN &&
1120 !warning_issued(dq, warntype[i])) {
1121 #ifdef CONFIG_PRINT_QUOTA_WARNING
1122 print_warning(dq, warntype[i]);
1123 #endif
1124 quota_send_warning(dq->dq_type, dq->dq_id,
1125 dq->dq_sb->s_dev, warntype[i]);
1130 static int ignore_hardlimit(struct dquot *dquot)
1132 struct mem_dqinfo *info = &sb_dqopt(dquot->dq_sb)->info[dquot->dq_type];
1134 return capable(CAP_SYS_RESOURCE) &&
1135 (info->dqi_format->qf_fmt_id != QFMT_VFS_OLD ||
1136 !(info->dqi_flags & V1_DQF_RSQUASH));
1139 /* needs dq_data_lock */
1140 static int check_idq(struct dquot *dquot, qsize_t inodes, char *warntype)
1142 qsize_t newinodes = dquot->dq_dqb.dqb_curinodes + inodes;
1144 *warntype = QUOTA_NL_NOWARN;
1145 if (!sb_has_quota_limits_enabled(dquot->dq_sb, dquot->dq_type) ||
1146 test_bit(DQ_FAKE_B, &dquot->dq_flags))
1147 return QUOTA_OK;
1149 if (dquot->dq_dqb.dqb_ihardlimit &&
1150 newinodes > dquot->dq_dqb.dqb_ihardlimit &&
1151 !ignore_hardlimit(dquot)) {
1152 *warntype = QUOTA_NL_IHARDWARN;
1153 return NO_QUOTA;
1156 if (dquot->dq_dqb.dqb_isoftlimit &&
1157 newinodes > dquot->dq_dqb.dqb_isoftlimit &&
1158 dquot->dq_dqb.dqb_itime &&
1159 get_seconds() >= dquot->dq_dqb.dqb_itime &&
1160 !ignore_hardlimit(dquot)) {
1161 *warntype = QUOTA_NL_ISOFTLONGWARN;
1162 return NO_QUOTA;
1165 if (dquot->dq_dqb.dqb_isoftlimit &&
1166 newinodes > dquot->dq_dqb.dqb_isoftlimit &&
1167 dquot->dq_dqb.dqb_itime == 0) {
1168 *warntype = QUOTA_NL_ISOFTWARN;
1169 dquot->dq_dqb.dqb_itime = get_seconds() +
1170 sb_dqopt(dquot->dq_sb)->info[dquot->dq_type].dqi_igrace;
1173 return QUOTA_OK;
1176 /* needs dq_data_lock */
1177 static int check_bdq(struct dquot *dquot, qsize_t space, int prealloc, char *warntype)
1179 qsize_t tspace;
1180 struct super_block *sb = dquot->dq_sb;
1182 *warntype = QUOTA_NL_NOWARN;
1183 if (!sb_has_quota_limits_enabled(sb, dquot->dq_type) ||
1184 test_bit(DQ_FAKE_B, &dquot->dq_flags))
1185 return QUOTA_OK;
1187 tspace = dquot->dq_dqb.dqb_curspace + dquot->dq_dqb.dqb_rsvspace
1188 + space;
1190 if (dquot->dq_dqb.dqb_bhardlimit &&
1191 tspace > dquot->dq_dqb.dqb_bhardlimit &&
1192 !ignore_hardlimit(dquot)) {
1193 if (!prealloc)
1194 *warntype = QUOTA_NL_BHARDWARN;
1195 return NO_QUOTA;
1198 if (dquot->dq_dqb.dqb_bsoftlimit &&
1199 tspace > dquot->dq_dqb.dqb_bsoftlimit &&
1200 dquot->dq_dqb.dqb_btime &&
1201 get_seconds() >= dquot->dq_dqb.dqb_btime &&
1202 !ignore_hardlimit(dquot)) {
1203 if (!prealloc)
1204 *warntype = QUOTA_NL_BSOFTLONGWARN;
1205 return NO_QUOTA;
1208 if (dquot->dq_dqb.dqb_bsoftlimit &&
1209 tspace > dquot->dq_dqb.dqb_bsoftlimit &&
1210 dquot->dq_dqb.dqb_btime == 0) {
1211 if (!prealloc) {
1212 *warntype = QUOTA_NL_BSOFTWARN;
1213 dquot->dq_dqb.dqb_btime = get_seconds() +
1214 sb_dqopt(sb)->info[dquot->dq_type].dqi_bgrace;
1216 else
1218 * We don't allow preallocation to exceed softlimit so exceeding will
1219 * be always printed
1221 return NO_QUOTA;
1224 return QUOTA_OK;
1227 static int info_idq_free(struct dquot *dquot, qsize_t inodes)
1229 qsize_t newinodes;
1231 if (test_bit(DQ_FAKE_B, &dquot->dq_flags) ||
1232 dquot->dq_dqb.dqb_curinodes <= dquot->dq_dqb.dqb_isoftlimit ||
1233 !sb_has_quota_limits_enabled(dquot->dq_sb, dquot->dq_type))
1234 return QUOTA_NL_NOWARN;
1236 newinodes = dquot->dq_dqb.dqb_curinodes - inodes;
1237 if (newinodes <= dquot->dq_dqb.dqb_isoftlimit)
1238 return QUOTA_NL_ISOFTBELOW;
1239 if (dquot->dq_dqb.dqb_curinodes >= dquot->dq_dqb.dqb_ihardlimit &&
1240 newinodes < dquot->dq_dqb.dqb_ihardlimit)
1241 return QUOTA_NL_IHARDBELOW;
1242 return QUOTA_NL_NOWARN;
1245 static int info_bdq_free(struct dquot *dquot, qsize_t space)
1247 if (test_bit(DQ_FAKE_B, &dquot->dq_flags) ||
1248 dquot->dq_dqb.dqb_curspace <= dquot->dq_dqb.dqb_bsoftlimit)
1249 return QUOTA_NL_NOWARN;
1251 if (dquot->dq_dqb.dqb_curspace - space <= dquot->dq_dqb.dqb_bsoftlimit)
1252 return QUOTA_NL_BSOFTBELOW;
1253 if (dquot->dq_dqb.dqb_curspace >= dquot->dq_dqb.dqb_bhardlimit &&
1254 dquot->dq_dqb.dqb_curspace - space < dquot->dq_dqb.dqb_bhardlimit)
1255 return QUOTA_NL_BHARDBELOW;
1256 return QUOTA_NL_NOWARN;
1260 * Initialize quota pointers in inode
1261 * We do things in a bit complicated way but by that we avoid calling
1262 * dqget() and thus filesystem callbacks under dqptr_sem.
1264 int dquot_initialize(struct inode *inode, int type)
1266 unsigned int id = 0;
1267 int cnt, ret = 0;
1268 struct dquot *got[MAXQUOTAS] = { NULL, NULL };
1269 struct super_block *sb = inode->i_sb;
1270 qsize_t rsv;
1272 /* First test before acquiring mutex - solves deadlocks when we
1273 * re-enter the quota code and are already holding the mutex */
1274 if (IS_NOQUOTA(inode))
1275 return 0;
1277 /* First get references to structures we might need. */
1278 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1279 if (type != -1 && cnt != type)
1280 continue;
1281 switch (cnt) {
1282 case USRQUOTA:
1283 id = inode->i_uid;
1284 break;
1285 case GRPQUOTA:
1286 id = inode->i_gid;
1287 break;
1289 got[cnt] = dqget(sb, id, cnt);
1292 down_write(&sb_dqopt(sb)->dqptr_sem);
1293 /* Having dqptr_sem we know NOQUOTA flags can't be altered... */
1294 if (IS_NOQUOTA(inode))
1295 goto out_err;
1296 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1297 if (type != -1 && cnt != type)
1298 continue;
1299 /* Avoid races with quotaoff() */
1300 if (!sb_has_quota_active(sb, cnt))
1301 continue;
1302 if (!inode->i_dquot[cnt]) {
1303 inode->i_dquot[cnt] = got[cnt];
1304 got[cnt] = NULL;
1306 * Make quota reservation system happy if someone
1307 * did a write before quota was turned on
1309 rsv = inode_get_rsv_space(inode);
1310 if (unlikely(rsv))
1311 dquot_resv_space(inode->i_dquot[cnt], rsv);
1314 out_err:
1315 up_write(&sb_dqopt(sb)->dqptr_sem);
1316 /* Drop unused references */
1317 dqput_all(got);
1318 return ret;
1320 EXPORT_SYMBOL(dquot_initialize);
1323 * Release all quotas referenced by inode
1325 int dquot_drop(struct inode *inode)
1327 int cnt;
1328 struct dquot *put[MAXQUOTAS];
1330 down_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
1331 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1332 put[cnt] = inode->i_dquot[cnt];
1333 inode->i_dquot[cnt] = NULL;
1335 up_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
1336 dqput_all(put);
1337 return 0;
1339 EXPORT_SYMBOL(dquot_drop);
1341 /* Wrapper to remove references to quota structures from inode */
1342 void vfs_dq_drop(struct inode *inode)
1344 /* Here we can get arbitrary inode from clear_inode() so we have
1345 * to be careful. OTOH we don't need locking as quota operations
1346 * are allowed to change only at mount time */
1347 if (!IS_NOQUOTA(inode) && inode->i_sb && inode->i_sb->dq_op
1348 && inode->i_sb->dq_op->drop) {
1349 int cnt;
1350 /* Test before calling to rule out calls from proc and such
1351 * where we are not allowed to block. Note that this is
1352 * actually reliable test even without the lock - the caller
1353 * must assure that nobody can come after the DQUOT_DROP and
1354 * add quota pointers back anyway */
1355 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1356 if (inode->i_dquot[cnt])
1357 break;
1358 if (cnt < MAXQUOTAS)
1359 inode->i_sb->dq_op->drop(inode);
1362 EXPORT_SYMBOL(vfs_dq_drop);
1365 * inode_reserved_space is managed internally by quota, and protected by
1366 * i_lock similar to i_blocks+i_bytes.
1368 static qsize_t *inode_reserved_space(struct inode * inode)
1370 /* Filesystem must explicitly define it's own method in order to use
1371 * quota reservation interface */
1372 BUG_ON(!inode->i_sb->dq_op->get_reserved_space);
1373 return inode->i_sb->dq_op->get_reserved_space(inode);
1376 void inode_add_rsv_space(struct inode *inode, qsize_t number)
1378 spin_lock(&inode->i_lock);
1379 *inode_reserved_space(inode) += number;
1380 spin_unlock(&inode->i_lock);
1382 EXPORT_SYMBOL(inode_add_rsv_space);
1384 void inode_claim_rsv_space(struct inode *inode, qsize_t number)
1386 spin_lock(&inode->i_lock);
1387 *inode_reserved_space(inode) -= number;
1388 __inode_add_bytes(inode, number);
1389 spin_unlock(&inode->i_lock);
1391 EXPORT_SYMBOL(inode_claim_rsv_space);
1393 void inode_sub_rsv_space(struct inode *inode, qsize_t number)
1395 spin_lock(&inode->i_lock);
1396 *inode_reserved_space(inode) -= number;
1397 spin_unlock(&inode->i_lock);
1399 EXPORT_SYMBOL(inode_sub_rsv_space);
1401 static qsize_t inode_get_rsv_space(struct inode *inode)
1403 qsize_t ret;
1405 if (!inode->i_sb->dq_op->get_reserved_space)
1406 return 0;
1407 spin_lock(&inode->i_lock);
1408 ret = *inode_reserved_space(inode);
1409 spin_unlock(&inode->i_lock);
1410 return ret;
1413 static void inode_incr_space(struct inode *inode, qsize_t number,
1414 int reserve)
1416 if (reserve)
1417 inode_add_rsv_space(inode, number);
1418 else
1419 inode_add_bytes(inode, number);
1422 static void inode_decr_space(struct inode *inode, qsize_t number, int reserve)
1424 if (reserve)
1425 inode_sub_rsv_space(inode, number);
1426 else
1427 inode_sub_bytes(inode, number);
1431 * Following four functions update i_blocks+i_bytes fields and
1432 * quota information (together with appropriate checks)
1433 * NOTE: We absolutely rely on the fact that caller dirties
1434 * the inode (usually macros in quotaops.h care about this) and
1435 * holds a handle for the current transaction so that dquot write and
1436 * inode write go into the same transaction.
1440 * This operation can block, but only after everything is updated
1442 int __dquot_alloc_space(struct inode *inode, qsize_t number,
1443 int warn, int reserve)
1445 int cnt, ret = QUOTA_OK;
1446 char warntype[MAXQUOTAS];
1449 * First test before acquiring mutex - solves deadlocks when we
1450 * re-enter the quota code and are already holding the mutex
1452 if (IS_NOQUOTA(inode)) {
1453 inode_incr_space(inode, number, reserve);
1454 goto out;
1457 down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1458 if (IS_NOQUOTA(inode)) {
1459 inode_incr_space(inode, number, reserve);
1460 goto out_unlock;
1463 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1464 warntype[cnt] = QUOTA_NL_NOWARN;
1466 spin_lock(&dq_data_lock);
1467 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1468 if (!inode->i_dquot[cnt])
1469 continue;
1470 if (check_bdq(inode->i_dquot[cnt], number, warn, warntype+cnt)
1471 == NO_QUOTA) {
1472 ret = NO_QUOTA;
1473 spin_unlock(&dq_data_lock);
1474 goto out_flush_warn;
1477 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1478 if (!inode->i_dquot[cnt])
1479 continue;
1480 if (reserve)
1481 dquot_resv_space(inode->i_dquot[cnt], number);
1482 else
1483 dquot_incr_space(inode->i_dquot[cnt], number);
1485 inode_incr_space(inode, number, reserve);
1486 spin_unlock(&dq_data_lock);
1488 if (reserve)
1489 goto out_flush_warn;
1490 mark_all_dquot_dirty(inode->i_dquot);
1491 out_flush_warn:
1492 flush_warnings(inode->i_dquot, warntype);
1493 out_unlock:
1494 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1495 out:
1496 return ret;
1499 int dquot_alloc_space(struct inode *inode, qsize_t number, int warn)
1501 return __dquot_alloc_space(inode, number, warn, 0);
1503 EXPORT_SYMBOL(dquot_alloc_space);
1505 int dquot_reserve_space(struct inode *inode, qsize_t number, int warn)
1507 return __dquot_alloc_space(inode, number, warn, 1);
1509 EXPORT_SYMBOL(dquot_reserve_space);
1512 * This operation can block, but only after everything is updated
1514 int dquot_alloc_inode(const struct inode *inode, qsize_t number)
1516 int cnt, ret = NO_QUOTA;
1517 char warntype[MAXQUOTAS];
1519 /* First test before acquiring mutex - solves deadlocks when we
1520 * re-enter the quota code and are already holding the mutex */
1521 if (IS_NOQUOTA(inode))
1522 return QUOTA_OK;
1523 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1524 warntype[cnt] = QUOTA_NL_NOWARN;
1525 down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1526 if (IS_NOQUOTA(inode)) {
1527 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1528 return QUOTA_OK;
1530 spin_lock(&dq_data_lock);
1531 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1532 if (!inode->i_dquot[cnt])
1533 continue;
1534 if (check_idq(inode->i_dquot[cnt], number, warntype+cnt)
1535 == NO_QUOTA)
1536 goto warn_put_all;
1539 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1540 if (!inode->i_dquot[cnt])
1541 continue;
1542 dquot_incr_inodes(inode->i_dquot[cnt], number);
1544 ret = QUOTA_OK;
1545 warn_put_all:
1546 spin_unlock(&dq_data_lock);
1547 if (ret == QUOTA_OK)
1548 mark_all_dquot_dirty(inode->i_dquot);
1549 flush_warnings(inode->i_dquot, warntype);
1550 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1551 return ret;
1553 EXPORT_SYMBOL(dquot_alloc_inode);
1555 int dquot_claim_space(struct inode *inode, qsize_t number)
1557 int cnt;
1558 int ret = QUOTA_OK;
1560 if (IS_NOQUOTA(inode)) {
1561 inode_claim_rsv_space(inode, number);
1562 goto out;
1565 down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1566 if (IS_NOQUOTA(inode)) {
1567 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1568 inode_claim_rsv_space(inode, number);
1569 goto out;
1572 spin_lock(&dq_data_lock);
1573 /* Claim reserved quotas to allocated quotas */
1574 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1575 if (inode->i_dquot[cnt])
1576 dquot_claim_reserved_space(inode->i_dquot[cnt],
1577 number);
1579 /* Update inode bytes */
1580 inode_claim_rsv_space(inode, number);
1581 spin_unlock(&dq_data_lock);
1582 mark_all_dquot_dirty(inode->i_dquot);
1583 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1584 out:
1585 return ret;
1587 EXPORT_SYMBOL(dquot_claim_space);
1590 * This operation can block, but only after everything is updated
1592 int __dquot_free_space(struct inode *inode, qsize_t number, int reserve)
1594 unsigned int cnt;
1595 char warntype[MAXQUOTAS];
1597 /* First test before acquiring mutex - solves deadlocks when we
1598 * re-enter the quota code and are already holding the mutex */
1599 if (IS_NOQUOTA(inode)) {
1600 out_sub:
1601 inode_decr_space(inode, number, reserve);
1602 return QUOTA_OK;
1605 down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1606 /* Now recheck reliably when holding dqptr_sem */
1607 if (IS_NOQUOTA(inode)) {
1608 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1609 goto out_sub;
1611 spin_lock(&dq_data_lock);
1612 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1613 if (!inode->i_dquot[cnt])
1614 continue;
1615 warntype[cnt] = info_bdq_free(inode->i_dquot[cnt], number);
1616 if (reserve)
1617 dquot_free_reserved_space(inode->i_dquot[cnt], number);
1618 else
1619 dquot_decr_space(inode->i_dquot[cnt], number);
1621 inode_decr_space(inode, number, reserve);
1622 spin_unlock(&dq_data_lock);
1624 if (reserve)
1625 goto out_unlock;
1626 mark_all_dquot_dirty(inode->i_dquot);
1627 out_unlock:
1628 flush_warnings(inode->i_dquot, warntype);
1629 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1630 return QUOTA_OK;
1633 int dquot_free_space(struct inode *inode, qsize_t number)
1635 return __dquot_free_space(inode, number, 0);
1637 EXPORT_SYMBOL(dquot_free_space);
1640 * Release reserved quota space
1642 void dquot_release_reserved_space(struct inode *inode, qsize_t number)
1644 __dquot_free_space(inode, number, 1);
1647 EXPORT_SYMBOL(dquot_release_reserved_space);
1650 * This operation can block, but only after everything is updated
1652 int dquot_free_inode(const struct inode *inode, qsize_t number)
1654 unsigned int cnt;
1655 char warntype[MAXQUOTAS];
1657 /* First test before acquiring mutex - solves deadlocks when we
1658 * re-enter the quota code and are already holding the mutex */
1659 if (IS_NOQUOTA(inode))
1660 return QUOTA_OK;
1662 down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1663 /* Now recheck reliably when holding dqptr_sem */
1664 if (IS_NOQUOTA(inode)) {
1665 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1666 return QUOTA_OK;
1668 spin_lock(&dq_data_lock);
1669 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1670 if (!inode->i_dquot[cnt])
1671 continue;
1672 warntype[cnt] = info_idq_free(inode->i_dquot[cnt], number);
1673 dquot_decr_inodes(inode->i_dquot[cnt], number);
1675 spin_unlock(&dq_data_lock);
1676 mark_all_dquot_dirty(inode->i_dquot);
1677 flush_warnings(inode->i_dquot, warntype);
1678 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1679 return QUOTA_OK;
1681 EXPORT_SYMBOL(dquot_free_inode);
1684 * Transfer the number of inode and blocks from one diskquota to an other.
1686 * This operation can block, but only after everything is updated
1687 * A transaction must be started when entering this function.
1689 int dquot_transfer(struct inode *inode, struct iattr *iattr)
1691 qsize_t space, cur_space;
1692 qsize_t rsv_space = 0;
1693 struct dquot *transfer_from[MAXQUOTAS];
1694 struct dquot *transfer_to[MAXQUOTAS];
1695 int cnt, ret = QUOTA_OK;
1696 int chuid = iattr->ia_valid & ATTR_UID && inode->i_uid != iattr->ia_uid,
1697 chgid = iattr->ia_valid & ATTR_GID && inode->i_gid != iattr->ia_gid;
1698 char warntype_to[MAXQUOTAS];
1699 char warntype_from_inodes[MAXQUOTAS], warntype_from_space[MAXQUOTAS];
1701 /* First test before acquiring mutex - solves deadlocks when we
1702 * re-enter the quota code and are already holding the mutex */
1703 if (IS_NOQUOTA(inode))
1704 return QUOTA_OK;
1705 /* Initialize the arrays */
1706 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1707 transfer_from[cnt] = NULL;
1708 transfer_to[cnt] = NULL;
1709 warntype_to[cnt] = QUOTA_NL_NOWARN;
1711 if (chuid)
1712 transfer_to[USRQUOTA] = dqget(inode->i_sb, iattr->ia_uid,
1713 USRQUOTA);
1714 if (chgid)
1715 transfer_to[GRPQUOTA] = dqget(inode->i_sb, iattr->ia_gid,
1716 GRPQUOTA);
1718 down_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
1719 /* Now recheck reliably when holding dqptr_sem */
1720 if (IS_NOQUOTA(inode)) { /* File without quota accounting? */
1721 up_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
1722 goto put_all;
1724 spin_lock(&dq_data_lock);
1725 cur_space = inode_get_bytes(inode);
1726 rsv_space = inode_get_rsv_space(inode);
1727 space = cur_space + rsv_space;
1728 /* Build the transfer_from list and check the limits */
1729 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1730 if (!transfer_to[cnt])
1731 continue;
1732 transfer_from[cnt] = inode->i_dquot[cnt];
1733 if (check_idq(transfer_to[cnt], 1, warntype_to + cnt) ==
1734 NO_QUOTA || check_bdq(transfer_to[cnt], space, 0,
1735 warntype_to + cnt) == NO_QUOTA)
1736 goto over_quota;
1740 * Finally perform the needed transfer from transfer_from to transfer_to
1742 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1744 * Skip changes for same uid or gid or for turned off quota-type.
1746 if (!transfer_to[cnt])
1747 continue;
1749 /* Due to IO error we might not have transfer_from[] structure */
1750 if (transfer_from[cnt]) {
1751 warntype_from_inodes[cnt] =
1752 info_idq_free(transfer_from[cnt], 1);
1753 warntype_from_space[cnt] =
1754 info_bdq_free(transfer_from[cnt], space);
1755 dquot_decr_inodes(transfer_from[cnt], 1);
1756 dquot_decr_space(transfer_from[cnt], cur_space);
1757 dquot_free_reserved_space(transfer_from[cnt],
1758 rsv_space);
1761 dquot_incr_inodes(transfer_to[cnt], 1);
1762 dquot_incr_space(transfer_to[cnt], cur_space);
1763 dquot_resv_space(transfer_to[cnt], rsv_space);
1765 inode->i_dquot[cnt] = transfer_to[cnt];
1767 spin_unlock(&dq_data_lock);
1768 up_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
1770 mark_all_dquot_dirty(transfer_from);
1771 mark_all_dquot_dirty(transfer_to);
1772 /* The reference we got is transferred to the inode */
1773 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1774 transfer_to[cnt] = NULL;
1775 warn_put_all:
1776 flush_warnings(transfer_to, warntype_to);
1777 flush_warnings(transfer_from, warntype_from_inodes);
1778 flush_warnings(transfer_from, warntype_from_space);
1779 put_all:
1780 dqput_all(transfer_from);
1781 dqput_all(transfer_to);
1782 return ret;
1783 over_quota:
1784 spin_unlock(&dq_data_lock);
1785 up_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
1786 /* Clear dquot pointers we don't want to dqput() */
1787 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1788 transfer_from[cnt] = NULL;
1789 ret = NO_QUOTA;
1790 goto warn_put_all;
1792 EXPORT_SYMBOL(dquot_transfer);
1794 /* Wrapper for transferring ownership of an inode */
1795 int vfs_dq_transfer(struct inode *inode, struct iattr *iattr)
1797 if (sb_any_quota_active(inode->i_sb) && !IS_NOQUOTA(inode)) {
1798 vfs_dq_init(inode);
1799 if (inode->i_sb->dq_op->transfer(inode, iattr) == NO_QUOTA)
1800 return 1;
1802 return 0;
1804 EXPORT_SYMBOL(vfs_dq_transfer);
1807 * Write info of quota file to disk
1809 int dquot_commit_info(struct super_block *sb, int type)
1811 int ret;
1812 struct quota_info *dqopt = sb_dqopt(sb);
1814 mutex_lock(&dqopt->dqio_mutex);
1815 ret = dqopt->ops[type]->write_file_info(sb, type);
1816 mutex_unlock(&dqopt->dqio_mutex);
1817 return ret;
1819 EXPORT_SYMBOL(dquot_commit_info);
1822 * Definitions of diskquota operations.
1824 const struct dquot_operations dquot_operations = {
1825 .initialize = dquot_initialize,
1826 .drop = dquot_drop,
1827 .alloc_space = dquot_alloc_space,
1828 .alloc_inode = dquot_alloc_inode,
1829 .free_space = dquot_free_space,
1830 .free_inode = dquot_free_inode,
1831 .transfer = dquot_transfer,
1832 .write_dquot = dquot_commit,
1833 .acquire_dquot = dquot_acquire,
1834 .release_dquot = dquot_release,
1835 .mark_dirty = dquot_mark_dquot_dirty,
1836 .write_info = dquot_commit_info,
1837 .alloc_dquot = dquot_alloc,
1838 .destroy_dquot = dquot_destroy,
1842 * Turn quota off on a device. type == -1 ==> quotaoff for all types (umount)
1844 int vfs_quota_disable(struct super_block *sb, int type, unsigned int flags)
1846 int cnt, ret = 0;
1847 struct quota_info *dqopt = sb_dqopt(sb);
1848 struct inode *toputinode[MAXQUOTAS];
1850 /* Cannot turn off usage accounting without turning off limits, or
1851 * suspend quotas and simultaneously turn quotas off. */
1852 if ((flags & DQUOT_USAGE_ENABLED && !(flags & DQUOT_LIMITS_ENABLED))
1853 || (flags & DQUOT_SUSPENDED && flags & (DQUOT_LIMITS_ENABLED |
1854 DQUOT_USAGE_ENABLED)))
1855 return -EINVAL;
1857 /* We need to serialize quota_off() for device */
1858 mutex_lock(&dqopt->dqonoff_mutex);
1861 * Skip everything if there's nothing to do. We have to do this because
1862 * sometimes we are called when fill_super() failed and calling
1863 * sync_fs() in such cases does no good.
1865 if (!sb_any_quota_loaded(sb)) {
1866 mutex_unlock(&dqopt->dqonoff_mutex);
1867 return 0;
1869 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1870 toputinode[cnt] = NULL;
1871 if (type != -1 && cnt != type)
1872 continue;
1873 if (!sb_has_quota_loaded(sb, cnt))
1874 continue;
1876 if (flags & DQUOT_SUSPENDED) {
1877 spin_lock(&dq_state_lock);
1878 dqopt->flags |=
1879 dquot_state_flag(DQUOT_SUSPENDED, cnt);
1880 spin_unlock(&dq_state_lock);
1881 } else {
1882 spin_lock(&dq_state_lock);
1883 dqopt->flags &= ~dquot_state_flag(flags, cnt);
1884 /* Turning off suspended quotas? */
1885 if (!sb_has_quota_loaded(sb, cnt) &&
1886 sb_has_quota_suspended(sb, cnt)) {
1887 dqopt->flags &= ~dquot_state_flag(
1888 DQUOT_SUSPENDED, cnt);
1889 spin_unlock(&dq_state_lock);
1890 iput(dqopt->files[cnt]);
1891 dqopt->files[cnt] = NULL;
1892 continue;
1894 spin_unlock(&dq_state_lock);
1897 /* We still have to keep quota loaded? */
1898 if (sb_has_quota_loaded(sb, cnt) && !(flags & DQUOT_SUSPENDED))
1899 continue;
1901 /* Note: these are blocking operations */
1902 drop_dquot_ref(sb, cnt);
1903 invalidate_dquots(sb, cnt);
1905 * Now all dquots should be invalidated, all writes done so we
1906 * should be only users of the info. No locks needed.
1908 if (info_dirty(&dqopt->info[cnt]))
1909 sb->dq_op->write_info(sb, cnt);
1910 if (dqopt->ops[cnt]->free_file_info)
1911 dqopt->ops[cnt]->free_file_info(sb, cnt);
1912 put_quota_format(dqopt->info[cnt].dqi_format);
1914 toputinode[cnt] = dqopt->files[cnt];
1915 if (!sb_has_quota_loaded(sb, cnt))
1916 dqopt->files[cnt] = NULL;
1917 dqopt->info[cnt].dqi_flags = 0;
1918 dqopt->info[cnt].dqi_igrace = 0;
1919 dqopt->info[cnt].dqi_bgrace = 0;
1920 dqopt->ops[cnt] = NULL;
1922 mutex_unlock(&dqopt->dqonoff_mutex);
1924 /* Skip syncing and setting flags if quota files are hidden */
1925 if (dqopt->flags & DQUOT_QUOTA_SYS_FILE)
1926 goto put_inodes;
1928 /* Sync the superblock so that buffers with quota data are written to
1929 * disk (and so userspace sees correct data afterwards). */
1930 if (sb->s_op->sync_fs)
1931 sb->s_op->sync_fs(sb, 1);
1932 sync_blockdev(sb->s_bdev);
1933 /* Now the quota files are just ordinary files and we can set the
1934 * inode flags back. Moreover we discard the pagecache so that
1935 * userspace sees the writes we did bypassing the pagecache. We
1936 * must also discard the blockdev buffers so that we see the
1937 * changes done by userspace on the next quotaon() */
1938 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1939 if (toputinode[cnt]) {
1940 mutex_lock(&dqopt->dqonoff_mutex);
1941 /* If quota was reenabled in the meantime, we have
1942 * nothing to do */
1943 if (!sb_has_quota_loaded(sb, cnt)) {
1944 mutex_lock_nested(&toputinode[cnt]->i_mutex,
1945 I_MUTEX_QUOTA);
1946 toputinode[cnt]->i_flags &= ~(S_IMMUTABLE |
1947 S_NOATIME | S_NOQUOTA);
1948 truncate_inode_pages(&toputinode[cnt]->i_data,
1950 mutex_unlock(&toputinode[cnt]->i_mutex);
1951 mark_inode_dirty(toputinode[cnt]);
1953 mutex_unlock(&dqopt->dqonoff_mutex);
1955 if (sb->s_bdev)
1956 invalidate_bdev(sb->s_bdev);
1957 put_inodes:
1958 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1959 if (toputinode[cnt]) {
1960 /* On remount RO, we keep the inode pointer so that we
1961 * can reenable quota on the subsequent remount RW. We
1962 * have to check 'flags' variable and not use sb_has_
1963 * function because another quotaon / quotaoff could
1964 * change global state before we got here. We refuse
1965 * to suspend quotas when there is pending delete on
1966 * the quota file... */
1967 if (!(flags & DQUOT_SUSPENDED))
1968 iput(toputinode[cnt]);
1969 else if (!toputinode[cnt]->i_nlink)
1970 ret = -EBUSY;
1972 return ret;
1974 EXPORT_SYMBOL(vfs_quota_disable);
1976 int vfs_quota_off(struct super_block *sb, int type, int remount)
1978 return vfs_quota_disable(sb, type, remount ? DQUOT_SUSPENDED :
1979 (DQUOT_USAGE_ENABLED | DQUOT_LIMITS_ENABLED));
1981 EXPORT_SYMBOL(vfs_quota_off);
1983 * Turn quotas on on a device
1987 * Helper function to turn quotas on when we already have the inode of
1988 * quota file and no quota information is loaded.
1990 static int vfs_load_quota_inode(struct inode *inode, int type, int format_id,
1991 unsigned int flags)
1993 struct quota_format_type *fmt = find_quota_format(format_id);
1994 struct super_block *sb = inode->i_sb;
1995 struct quota_info *dqopt = sb_dqopt(sb);
1996 int error;
1997 int oldflags = -1;
1999 if (!fmt)
2000 return -ESRCH;
2001 if (!S_ISREG(inode->i_mode)) {
2002 error = -EACCES;
2003 goto out_fmt;
2005 if (IS_RDONLY(inode)) {
2006 error = -EROFS;
2007 goto out_fmt;
2009 if (!sb->s_op->quota_write || !sb->s_op->quota_read) {
2010 error = -EINVAL;
2011 goto out_fmt;
2013 /* Usage always has to be set... */
2014 if (!(flags & DQUOT_USAGE_ENABLED)) {
2015 error = -EINVAL;
2016 goto out_fmt;
2019 if (!(dqopt->flags & DQUOT_QUOTA_SYS_FILE)) {
2020 /* As we bypass the pagecache we must now flush the inode so
2021 * that we see all the changes from userspace... */
2022 write_inode_now(inode, 1);
2023 /* And now flush the block cache so that kernel sees the
2024 * changes */
2025 invalidate_bdev(sb->s_bdev);
2027 mutex_lock(&dqopt->dqonoff_mutex);
2028 if (sb_has_quota_loaded(sb, type)) {
2029 error = -EBUSY;
2030 goto out_lock;
2033 if (!(dqopt->flags & DQUOT_QUOTA_SYS_FILE)) {
2034 /* We don't want quota and atime on quota files (deadlocks
2035 * possible) Also nobody should write to the file - we use
2036 * special IO operations which ignore the immutable bit. */
2037 down_write(&dqopt->dqptr_sem);
2038 mutex_lock_nested(&inode->i_mutex, I_MUTEX_QUOTA);
2039 oldflags = inode->i_flags & (S_NOATIME | S_IMMUTABLE |
2040 S_NOQUOTA);
2041 inode->i_flags |= S_NOQUOTA | S_NOATIME | S_IMMUTABLE;
2042 mutex_unlock(&inode->i_mutex);
2043 up_write(&dqopt->dqptr_sem);
2044 sb->dq_op->drop(inode);
2047 error = -EIO;
2048 dqopt->files[type] = igrab(inode);
2049 if (!dqopt->files[type])
2050 goto out_lock;
2051 error = -EINVAL;
2052 if (!fmt->qf_ops->check_quota_file(sb, type))
2053 goto out_file_init;
2055 dqopt->ops[type] = fmt->qf_ops;
2056 dqopt->info[type].dqi_format = fmt;
2057 dqopt->info[type].dqi_fmt_id = format_id;
2058 INIT_LIST_HEAD(&dqopt->info[type].dqi_dirty_list);
2059 mutex_lock(&dqopt->dqio_mutex);
2060 error = dqopt->ops[type]->read_file_info(sb, type);
2061 if (error < 0) {
2062 mutex_unlock(&dqopt->dqio_mutex);
2063 goto out_file_init;
2065 mutex_unlock(&dqopt->dqio_mutex);
2066 spin_lock(&dq_state_lock);
2067 dqopt->flags |= dquot_state_flag(flags, type);
2068 spin_unlock(&dq_state_lock);
2070 add_dquot_ref(sb, type);
2071 mutex_unlock(&dqopt->dqonoff_mutex);
2073 return 0;
2075 out_file_init:
2076 dqopt->files[type] = NULL;
2077 iput(inode);
2078 out_lock:
2079 if (oldflags != -1) {
2080 down_write(&dqopt->dqptr_sem);
2081 mutex_lock_nested(&inode->i_mutex, I_MUTEX_QUOTA);
2082 /* Set the flags back (in the case of accidental quotaon()
2083 * on a wrong file we don't want to mess up the flags) */
2084 inode->i_flags &= ~(S_NOATIME | S_NOQUOTA | S_IMMUTABLE);
2085 inode->i_flags |= oldflags;
2086 mutex_unlock(&inode->i_mutex);
2087 up_write(&dqopt->dqptr_sem);
2089 mutex_unlock(&dqopt->dqonoff_mutex);
2090 out_fmt:
2091 put_quota_format(fmt);
2093 return error;
2096 /* Reenable quotas on remount RW */
2097 static int vfs_quota_on_remount(struct super_block *sb, int type)
2099 struct quota_info *dqopt = sb_dqopt(sb);
2100 struct inode *inode;
2101 int ret;
2102 unsigned int flags;
2104 mutex_lock(&dqopt->dqonoff_mutex);
2105 if (!sb_has_quota_suspended(sb, type)) {
2106 mutex_unlock(&dqopt->dqonoff_mutex);
2107 return 0;
2109 inode = dqopt->files[type];
2110 dqopt->files[type] = NULL;
2111 spin_lock(&dq_state_lock);
2112 flags = dqopt->flags & dquot_state_flag(DQUOT_USAGE_ENABLED |
2113 DQUOT_LIMITS_ENABLED, type);
2114 dqopt->flags &= ~dquot_state_flag(DQUOT_STATE_FLAGS, type);
2115 spin_unlock(&dq_state_lock);
2116 mutex_unlock(&dqopt->dqonoff_mutex);
2118 flags = dquot_generic_flag(flags, type);
2119 ret = vfs_load_quota_inode(inode, type, dqopt->info[type].dqi_fmt_id,
2120 flags);
2121 iput(inode);
2123 return ret;
2126 int vfs_quota_on_path(struct super_block *sb, int type, int format_id,
2127 struct path *path)
2129 int error = security_quota_on(path->dentry);
2130 if (error)
2131 return error;
2132 /* Quota file not on the same filesystem? */
2133 if (path->mnt->mnt_sb != sb)
2134 error = -EXDEV;
2135 else
2136 error = vfs_load_quota_inode(path->dentry->d_inode, type,
2137 format_id, DQUOT_USAGE_ENABLED |
2138 DQUOT_LIMITS_ENABLED);
2139 return error;
2141 EXPORT_SYMBOL(vfs_quota_on_path);
2143 int vfs_quota_on(struct super_block *sb, int type, int format_id, char *name,
2144 int remount)
2146 struct path path;
2147 int error;
2149 if (remount)
2150 return vfs_quota_on_remount(sb, type);
2152 error = kern_path(name, LOOKUP_FOLLOW, &path);
2153 if (!error) {
2154 error = vfs_quota_on_path(sb, type, format_id, &path);
2155 path_put(&path);
2157 return error;
2159 EXPORT_SYMBOL(vfs_quota_on);
2162 * More powerful function for turning on quotas allowing setting
2163 * of individual quota flags
2165 int vfs_quota_enable(struct inode *inode, int type, int format_id,
2166 unsigned int flags)
2168 int ret = 0;
2169 struct super_block *sb = inode->i_sb;
2170 struct quota_info *dqopt = sb_dqopt(sb);
2172 /* Just unsuspend quotas? */
2173 if (flags & DQUOT_SUSPENDED)
2174 return vfs_quota_on_remount(sb, type);
2175 if (!flags)
2176 return 0;
2177 /* Just updating flags needed? */
2178 if (sb_has_quota_loaded(sb, type)) {
2179 mutex_lock(&dqopt->dqonoff_mutex);
2180 /* Now do a reliable test... */
2181 if (!sb_has_quota_loaded(sb, type)) {
2182 mutex_unlock(&dqopt->dqonoff_mutex);
2183 goto load_quota;
2185 if (flags & DQUOT_USAGE_ENABLED &&
2186 sb_has_quota_usage_enabled(sb, type)) {
2187 ret = -EBUSY;
2188 goto out_lock;
2190 if (flags & DQUOT_LIMITS_ENABLED &&
2191 sb_has_quota_limits_enabled(sb, type)) {
2192 ret = -EBUSY;
2193 goto out_lock;
2195 spin_lock(&dq_state_lock);
2196 sb_dqopt(sb)->flags |= dquot_state_flag(flags, type);
2197 spin_unlock(&dq_state_lock);
2198 out_lock:
2199 mutex_unlock(&dqopt->dqonoff_mutex);
2200 return ret;
2203 load_quota:
2204 return vfs_load_quota_inode(inode, type, format_id, flags);
2206 EXPORT_SYMBOL(vfs_quota_enable);
2209 * This function is used when filesystem needs to initialize quotas
2210 * during mount time.
2212 int vfs_quota_on_mount(struct super_block *sb, char *qf_name,
2213 int format_id, int type)
2215 struct dentry *dentry;
2216 int error;
2218 mutex_lock(&sb->s_root->d_inode->i_mutex);
2219 dentry = lookup_one_len(qf_name, sb->s_root, strlen(qf_name));
2220 mutex_unlock(&sb->s_root->d_inode->i_mutex);
2221 if (IS_ERR(dentry))
2222 return PTR_ERR(dentry);
2224 if (!dentry->d_inode) {
2225 error = -ENOENT;
2226 goto out;
2229 error = security_quota_on(dentry);
2230 if (!error)
2231 error = vfs_load_quota_inode(dentry->d_inode, type, format_id,
2232 DQUOT_USAGE_ENABLED | DQUOT_LIMITS_ENABLED);
2234 out:
2235 dput(dentry);
2236 return error;
2238 EXPORT_SYMBOL(vfs_quota_on_mount);
2240 /* Wrapper to turn on quotas when remounting rw */
2241 int vfs_dq_quota_on_remount(struct super_block *sb)
2243 int cnt;
2244 int ret = 0, err;
2246 if (!sb->s_qcop || !sb->s_qcop->quota_on)
2247 return -ENOSYS;
2248 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
2249 err = sb->s_qcop->quota_on(sb, cnt, 0, NULL, 1);
2250 if (err < 0 && !ret)
2251 ret = err;
2253 return ret;
2255 EXPORT_SYMBOL(vfs_dq_quota_on_remount);
2257 static inline qsize_t qbtos(qsize_t blocks)
2259 return blocks << QIF_DQBLKSIZE_BITS;
2262 static inline qsize_t stoqb(qsize_t space)
2264 return (space + QIF_DQBLKSIZE - 1) >> QIF_DQBLKSIZE_BITS;
2267 /* Generic routine for getting common part of quota structure */
2268 static void do_get_dqblk(struct dquot *dquot, struct if_dqblk *di)
2270 struct mem_dqblk *dm = &dquot->dq_dqb;
2272 spin_lock(&dq_data_lock);
2273 di->dqb_bhardlimit = stoqb(dm->dqb_bhardlimit);
2274 di->dqb_bsoftlimit = stoqb(dm->dqb_bsoftlimit);
2275 di->dqb_curspace = dm->dqb_curspace + dm->dqb_rsvspace;
2276 di->dqb_ihardlimit = dm->dqb_ihardlimit;
2277 di->dqb_isoftlimit = dm->dqb_isoftlimit;
2278 di->dqb_curinodes = dm->dqb_curinodes;
2279 di->dqb_btime = dm->dqb_btime;
2280 di->dqb_itime = dm->dqb_itime;
2281 di->dqb_valid = QIF_ALL;
2282 spin_unlock(&dq_data_lock);
2285 int vfs_get_dqblk(struct super_block *sb, int type, qid_t id,
2286 struct if_dqblk *di)
2288 struct dquot *dquot;
2290 dquot = dqget(sb, id, type);
2291 if (!dquot)
2292 return -ESRCH;
2293 do_get_dqblk(dquot, di);
2294 dqput(dquot);
2296 return 0;
2298 EXPORT_SYMBOL(vfs_get_dqblk);
2300 /* Generic routine for setting common part of quota structure */
2301 static int do_set_dqblk(struct dquot *dquot, struct if_dqblk *di)
2303 struct mem_dqblk *dm = &dquot->dq_dqb;
2304 int check_blim = 0, check_ilim = 0;
2305 struct mem_dqinfo *dqi = &sb_dqopt(dquot->dq_sb)->info[dquot->dq_type];
2307 if ((di->dqb_valid & QIF_BLIMITS &&
2308 (di->dqb_bhardlimit > dqi->dqi_maxblimit ||
2309 di->dqb_bsoftlimit > dqi->dqi_maxblimit)) ||
2310 (di->dqb_valid & QIF_ILIMITS &&
2311 (di->dqb_ihardlimit > dqi->dqi_maxilimit ||
2312 di->dqb_isoftlimit > dqi->dqi_maxilimit)))
2313 return -ERANGE;
2315 spin_lock(&dq_data_lock);
2316 if (di->dqb_valid & QIF_SPACE) {
2317 dm->dqb_curspace = di->dqb_curspace - dm->dqb_rsvspace;
2318 check_blim = 1;
2319 set_bit(DQ_LASTSET_B + QIF_SPACE_B, &dquot->dq_flags);
2321 if (di->dqb_valid & QIF_BLIMITS) {
2322 dm->dqb_bsoftlimit = qbtos(di->dqb_bsoftlimit);
2323 dm->dqb_bhardlimit = qbtos(di->dqb_bhardlimit);
2324 check_blim = 1;
2325 set_bit(DQ_LASTSET_B + QIF_BLIMITS_B, &dquot->dq_flags);
2327 if (di->dqb_valid & QIF_INODES) {
2328 dm->dqb_curinodes = di->dqb_curinodes;
2329 check_ilim = 1;
2330 set_bit(DQ_LASTSET_B + QIF_INODES_B, &dquot->dq_flags);
2332 if (di->dqb_valid & QIF_ILIMITS) {
2333 dm->dqb_isoftlimit = di->dqb_isoftlimit;
2334 dm->dqb_ihardlimit = di->dqb_ihardlimit;
2335 check_ilim = 1;
2336 set_bit(DQ_LASTSET_B + QIF_ILIMITS_B, &dquot->dq_flags);
2338 if (di->dqb_valid & QIF_BTIME) {
2339 dm->dqb_btime = di->dqb_btime;
2340 check_blim = 1;
2341 set_bit(DQ_LASTSET_B + QIF_BTIME_B, &dquot->dq_flags);
2343 if (di->dqb_valid & QIF_ITIME) {
2344 dm->dqb_itime = di->dqb_itime;
2345 check_ilim = 1;
2346 set_bit(DQ_LASTSET_B + QIF_ITIME_B, &dquot->dq_flags);
2349 if (check_blim) {
2350 if (!dm->dqb_bsoftlimit ||
2351 dm->dqb_curspace < dm->dqb_bsoftlimit) {
2352 dm->dqb_btime = 0;
2353 clear_bit(DQ_BLKS_B, &dquot->dq_flags);
2354 } else if (!(di->dqb_valid & QIF_BTIME))
2355 /* Set grace only if user hasn't provided his own... */
2356 dm->dqb_btime = get_seconds() + dqi->dqi_bgrace;
2358 if (check_ilim) {
2359 if (!dm->dqb_isoftlimit ||
2360 dm->dqb_curinodes < dm->dqb_isoftlimit) {
2361 dm->dqb_itime = 0;
2362 clear_bit(DQ_INODES_B, &dquot->dq_flags);
2363 } else if (!(di->dqb_valid & QIF_ITIME))
2364 /* Set grace only if user hasn't provided his own... */
2365 dm->dqb_itime = get_seconds() + dqi->dqi_igrace;
2367 if (dm->dqb_bhardlimit || dm->dqb_bsoftlimit || dm->dqb_ihardlimit ||
2368 dm->dqb_isoftlimit)
2369 clear_bit(DQ_FAKE_B, &dquot->dq_flags);
2370 else
2371 set_bit(DQ_FAKE_B, &dquot->dq_flags);
2372 spin_unlock(&dq_data_lock);
2373 mark_dquot_dirty(dquot);
2375 return 0;
2378 int vfs_set_dqblk(struct super_block *sb, int type, qid_t id,
2379 struct if_dqblk *di)
2381 struct dquot *dquot;
2382 int rc;
2384 dquot = dqget(sb, id, type);
2385 if (!dquot) {
2386 rc = -ESRCH;
2387 goto out;
2389 rc = do_set_dqblk(dquot, di);
2390 dqput(dquot);
2391 out:
2392 return rc;
2394 EXPORT_SYMBOL(vfs_set_dqblk);
2396 /* Generic routine for getting common part of quota file information */
2397 int vfs_get_dqinfo(struct super_block *sb, int type, struct if_dqinfo *ii)
2399 struct mem_dqinfo *mi;
2401 mutex_lock(&sb_dqopt(sb)->dqonoff_mutex);
2402 if (!sb_has_quota_active(sb, type)) {
2403 mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
2404 return -ESRCH;
2406 mi = sb_dqopt(sb)->info + type;
2407 spin_lock(&dq_data_lock);
2408 ii->dqi_bgrace = mi->dqi_bgrace;
2409 ii->dqi_igrace = mi->dqi_igrace;
2410 ii->dqi_flags = mi->dqi_flags & DQF_MASK;
2411 ii->dqi_valid = IIF_ALL;
2412 spin_unlock(&dq_data_lock);
2413 mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
2414 return 0;
2416 EXPORT_SYMBOL(vfs_get_dqinfo);
2418 /* Generic routine for setting common part of quota file information */
2419 int vfs_set_dqinfo(struct super_block *sb, int type, struct if_dqinfo *ii)
2421 struct mem_dqinfo *mi;
2422 int err = 0;
2424 mutex_lock(&sb_dqopt(sb)->dqonoff_mutex);
2425 if (!sb_has_quota_active(sb, type)) {
2426 err = -ESRCH;
2427 goto out;
2429 mi = sb_dqopt(sb)->info + type;
2430 spin_lock(&dq_data_lock);
2431 if (ii->dqi_valid & IIF_BGRACE)
2432 mi->dqi_bgrace = ii->dqi_bgrace;
2433 if (ii->dqi_valid & IIF_IGRACE)
2434 mi->dqi_igrace = ii->dqi_igrace;
2435 if (ii->dqi_valid & IIF_FLAGS)
2436 mi->dqi_flags = (mi->dqi_flags & ~DQF_MASK) |
2437 (ii->dqi_flags & DQF_MASK);
2438 spin_unlock(&dq_data_lock);
2439 mark_info_dirty(sb, type);
2440 /* Force write to disk */
2441 sb->dq_op->write_info(sb, type);
2442 out:
2443 mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
2444 return err;
2446 EXPORT_SYMBOL(vfs_set_dqinfo);
2448 const struct quotactl_ops vfs_quotactl_ops = {
2449 .quota_on = vfs_quota_on,
2450 .quota_off = vfs_quota_off,
2451 .quota_sync = vfs_quota_sync,
2452 .get_info = vfs_get_dqinfo,
2453 .set_info = vfs_set_dqinfo,
2454 .get_dqblk = vfs_get_dqblk,
2455 .set_dqblk = vfs_set_dqblk
2458 static ctl_table fs_dqstats_table[] = {
2460 .procname = "lookups",
2461 .data = &dqstats.lookups,
2462 .maxlen = sizeof(int),
2463 .mode = 0444,
2464 .proc_handler = proc_dointvec,
2467 .procname = "drops",
2468 .data = &dqstats.drops,
2469 .maxlen = sizeof(int),
2470 .mode = 0444,
2471 .proc_handler = proc_dointvec,
2474 .procname = "reads",
2475 .data = &dqstats.reads,
2476 .maxlen = sizeof(int),
2477 .mode = 0444,
2478 .proc_handler = proc_dointvec,
2481 .procname = "writes",
2482 .data = &dqstats.writes,
2483 .maxlen = sizeof(int),
2484 .mode = 0444,
2485 .proc_handler = proc_dointvec,
2488 .procname = "cache_hits",
2489 .data = &dqstats.cache_hits,
2490 .maxlen = sizeof(int),
2491 .mode = 0444,
2492 .proc_handler = proc_dointvec,
2495 .procname = "allocated_dquots",
2496 .data = &dqstats.allocated_dquots,
2497 .maxlen = sizeof(int),
2498 .mode = 0444,
2499 .proc_handler = proc_dointvec,
2502 .procname = "free_dquots",
2503 .data = &dqstats.free_dquots,
2504 .maxlen = sizeof(int),
2505 .mode = 0444,
2506 .proc_handler = proc_dointvec,
2509 .procname = "syncs",
2510 .data = &dqstats.syncs,
2511 .maxlen = sizeof(int),
2512 .mode = 0444,
2513 .proc_handler = proc_dointvec,
2515 #ifdef CONFIG_PRINT_QUOTA_WARNING
2517 .procname = "warnings",
2518 .data = &flag_print_warnings,
2519 .maxlen = sizeof(int),
2520 .mode = 0644,
2521 .proc_handler = proc_dointvec,
2523 #endif
2524 { },
2527 static ctl_table fs_table[] = {
2529 .procname = "quota",
2530 .mode = 0555,
2531 .child = fs_dqstats_table,
2533 { },
2536 static ctl_table sys_table[] = {
2538 .procname = "fs",
2539 .mode = 0555,
2540 .child = fs_table,
2542 { },
2545 static int __init dquot_init(void)
2547 int i;
2548 unsigned long nr_hash, order;
2550 printk(KERN_NOTICE "VFS: Disk quotas %s\n", __DQUOT_VERSION__);
2552 register_sysctl_table(sys_table);
2554 dquot_cachep = kmem_cache_create("dquot",
2555 sizeof(struct dquot), sizeof(unsigned long) * 4,
2556 (SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT|
2557 SLAB_MEM_SPREAD|SLAB_PANIC),
2558 NULL);
2560 order = 0;
2561 dquot_hash = (struct hlist_head *)__get_free_pages(GFP_ATOMIC, order);
2562 if (!dquot_hash)
2563 panic("Cannot create dquot hash table");
2565 /* Find power-of-two hlist_heads which can fit into allocation */
2566 nr_hash = (1UL << order) * PAGE_SIZE / sizeof(struct hlist_head);
2567 dq_hash_bits = 0;
2568 do {
2569 dq_hash_bits++;
2570 } while (nr_hash >> dq_hash_bits);
2571 dq_hash_bits--;
2573 nr_hash = 1UL << dq_hash_bits;
2574 dq_hash_mask = nr_hash - 1;
2575 for (i = 0; i < nr_hash; i++)
2576 INIT_HLIST_HEAD(dquot_hash + i);
2578 printk("Dquot-cache hash table entries: %ld (order %ld, %ld bytes)\n",
2579 nr_hash, order, (PAGE_SIZE << order));
2581 register_shrinker(&dqcache_shrinker);
2583 return 0;
2585 module_init(dquot_init);