thinkpad-acpi: enhance led support
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / fs / dquot.c
blob38f5f6094af253c2b9b153d94d337e9d9ec7d299
1 /*
2 * Implementation of the diskquota system for the LINUX operating system. QUOTA
3 * is implemented using the BSD system call interface as the means of
4 * communication with the user level. This file contains the generic routines
5 * called by the different filesystems on allocation of an inode or block.
6 * These routines take care of the administration needed to have a consistent
7 * diskquota tracking system. The ideas of both user and group quotas are based
8 * on the Melbourne quota system as used on BSD derived systems. The internal
9 * implementation is based on one of the several variants of the LINUX
10 * inode-subsystem with added complexity of the diskquota system.
12 * Version: $Id: dquot.c,v 6.3 1996/11/17 18:35:34 mvw Exp mvw $
14 * Author: Marco van Wieringen <mvw@planets.elm.net>
16 * Fixes: Dmitry Gorodchanin <pgmdsg@ibi.com>, 11 Feb 96
18 * Revised list management to avoid races
19 * -- Bill Hawes, <whawes@star.net>, 9/98
21 * Fixed races in dquot_transfer(), dqget() and dquot_alloc_...().
22 * As the consequence the locking was moved from dquot_decr_...(),
23 * dquot_incr_...() to calling functions.
24 * invalidate_dquots() now writes modified dquots.
25 * Serialized quota_off() and quota_on() for mount point.
26 * Fixed a few bugs in grow_dquots().
27 * Fixed deadlock in write_dquot() - we no longer account quotas on
28 * quota files
29 * remove_dquot_ref() moved to inode.c - it now traverses through inodes
30 * add_dquot_ref() restarts after blocking
31 * Added check for bogus uid and fixed check for group in quotactl.
32 * Jan Kara, <jack@suse.cz>, sponsored by SuSE CR, 10-11/99
34 * Used struct list_head instead of own list struct
35 * Invalidation of referenced dquots is no longer possible
36 * Improved free_dquots list management
37 * Quota and i_blocks are now updated in one place to avoid races
38 * Warnings are now delayed so we won't block in critical section
39 * Write updated not to require dquot lock
40 * Jan Kara, <jack@suse.cz>, 9/2000
42 * Added dynamic quota structure allocation
43 * Jan Kara <jack@suse.cz> 12/2000
45 * Rewritten quota interface. Implemented new quota format and
46 * formats registering.
47 * Jan Kara, <jack@suse.cz>, 2001,2002
49 * New SMP locking.
50 * Jan Kara, <jack@suse.cz>, 10/2002
52 * Added journalled quota support, fix lock inversion problems
53 * Jan Kara, <jack@suse.cz>, 2003,2004
55 * (C) Copyright 1994 - 1997 Marco van Wieringen
58 #include <linux/errno.h>
59 #include <linux/kernel.h>
60 #include <linux/fs.h>
61 #include <linux/mount.h>
62 #include <linux/mm.h>
63 #include <linux/time.h>
64 #include <linux/types.h>
65 #include <linux/string.h>
66 #include <linux/fcntl.h>
67 #include <linux/stat.h>
68 #include <linux/tty.h>
69 #include <linux/file.h>
70 #include <linux/slab.h>
71 #include <linux/sysctl.h>
72 #include <linux/init.h>
73 #include <linux/module.h>
74 #include <linux/proc_fs.h>
75 #include <linux/security.h>
76 #include <linux/kmod.h>
77 #include <linux/namei.h>
78 #include <linux/buffer_head.h>
79 #include <linux/capability.h>
80 #include <linux/quotaops.h>
81 #include <linux/writeback.h> /* for inode_lock, oddly enough.. */
82 #ifdef CONFIG_QUOTA_NETLINK_INTERFACE
83 #include <net/netlink.h>
84 #include <net/genetlink.h>
85 #endif
87 #include <asm/uaccess.h>
89 #define __DQUOT_PARANOIA
92 * There are two quota SMP locks. dq_list_lock protects all lists with quotas
93 * and quota formats and also dqstats structure containing statistics about the
94 * lists. dq_data_lock protects data from dq_dqb and also mem_dqinfo structures
95 * and also guards consistency of dquot->dq_dqb with inode->i_blocks, i_bytes.
96 * i_blocks and i_bytes updates itself are guarded by i_lock acquired directly
97 * in inode_add_bytes() and inode_sub_bytes().
99 * The spinlock ordering is hence: dq_data_lock > dq_list_lock > i_lock
101 * Note that some things (eg. sb pointer, type, id) doesn't change during
102 * the life of the dquot structure and so needn't to be protected by a lock
104 * Any operation working on dquots via inode pointers must hold dqptr_sem. If
105 * operation is just reading pointers from inode (or not using them at all) the
106 * read lock is enough. If pointers are altered function must hold write lock
107 * (these locking rules also apply for S_NOQUOTA flag in the inode - note that
108 * for altering the flag i_mutex is also needed). If operation is holding
109 * reference to dquot in other way (e.g. quotactl ops) it must be guarded by
110 * dqonoff_mutex.
111 * This locking assures that:
112 * a) update/access to dquot pointers in inode is serialized
113 * b) everyone is guarded against invalidate_dquots()
115 * Each dquot has its dq_lock mutex. Locked dquots might not be referenced
116 * from inodes (dquot_alloc_space() and such don't check the dq_lock).
117 * Currently dquot is locked only when it is being read to memory (or space for
118 * it is being allocated) on the first dqget() and when it is being released on
119 * the last dqput(). The allocation and release oparations are serialized by
120 * the dq_lock and by checking the use count in dquot_release(). Write
121 * operations on dquots don't hold dq_lock as they copy data under dq_data_lock
122 * spinlock to internal buffers before writing.
124 * Lock ordering (including related VFS locks) is the following:
125 * i_mutex > dqonoff_sem > journal_lock > dqptr_sem > dquot->dq_lock >
126 * dqio_mutex
127 * i_mutex on quota files is special (it's below dqio_mutex)
130 static DEFINE_SPINLOCK(dq_list_lock);
131 DEFINE_SPINLOCK(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;
149 void unregister_quota_format(struct quota_format_type *fmt)
151 struct quota_format_type **actqf;
153 spin_lock(&dq_list_lock);
154 for (actqf = &quota_formats; *actqf && *actqf != fmt; actqf = &(*actqf)->qf_next);
155 if (*actqf)
156 *actqf = (*actqf)->qf_next;
157 spin_unlock(&dq_list_lock);
160 static struct quota_format_type *find_quota_format(int id)
162 struct quota_format_type *actqf;
164 spin_lock(&dq_list_lock);
165 for (actqf = quota_formats; actqf && actqf->qf_fmt_id != id; actqf = actqf->qf_next);
166 if (!actqf || !try_module_get(actqf->qf_owner)) {
167 int qm;
169 spin_unlock(&dq_list_lock);
171 for (qm = 0; module_names[qm].qm_fmt_id && module_names[qm].qm_fmt_id != id; qm++);
172 if (!module_names[qm].qm_fmt_id || request_module(module_names[qm].qm_mod_name))
173 return NULL;
175 spin_lock(&dq_list_lock);
176 for (actqf = quota_formats; actqf && actqf->qf_fmt_id != id; actqf = actqf->qf_next);
177 if (actqf && !try_module_get(actqf->qf_owner))
178 actqf = NULL;
180 spin_unlock(&dq_list_lock);
181 return actqf;
184 static void put_quota_format(struct quota_format_type *fmt)
186 module_put(fmt->qf_owner);
190 * Dquot List Management:
191 * The quota code uses three lists for dquot management: the inuse_list,
192 * free_dquots, and dquot_hash[] array. A single dquot structure may be
193 * on all three lists, depending on its current state.
195 * All dquots are placed to the end of inuse_list when first created, and this
196 * list is used for invalidate operation, which must look at every dquot.
198 * Unused dquots (dq_count == 0) are added to the free_dquots list when freed,
199 * and this list is searched whenever we need an available dquot. Dquots are
200 * removed from the list as soon as they are used again, and
201 * dqstats.free_dquots gives the number of dquots on the list. When
202 * dquot is invalidated it's completely released from memory.
204 * Dquots with a specific identity (device, type and id) are placed on
205 * one of the dquot_hash[] hash chains. The provides an efficient search
206 * mechanism to locate a specific dquot.
209 static LIST_HEAD(inuse_list);
210 static LIST_HEAD(free_dquots);
211 static unsigned int dq_hash_bits, dq_hash_mask;
212 static struct hlist_head *dquot_hash;
214 struct dqstats dqstats;
216 static void dqput(struct dquot *dquot);
218 static inline unsigned int
219 hashfn(const struct super_block *sb, unsigned int id, int type)
221 unsigned long tmp;
223 tmp = (((unsigned long)sb>>L1_CACHE_SHIFT) ^ id) * (MAXQUOTAS - type);
224 return (tmp + (tmp >> dq_hash_bits)) & dq_hash_mask;
228 * Following list functions expect dq_list_lock to be held
230 static inline void insert_dquot_hash(struct dquot *dquot)
232 struct hlist_head *head = dquot_hash + hashfn(dquot->dq_sb, dquot->dq_id, dquot->dq_type);
233 hlist_add_head(&dquot->dq_hash, head);
236 static inline void remove_dquot_hash(struct dquot *dquot)
238 hlist_del_init(&dquot->dq_hash);
241 static inline struct dquot *find_dquot(unsigned int hashent, struct super_block *sb, unsigned int id, int type)
243 struct hlist_node *node;
244 struct dquot *dquot;
246 hlist_for_each (node, dquot_hash+hashent) {
247 dquot = hlist_entry(node, struct dquot, dq_hash);
248 if (dquot->dq_sb == sb && dquot->dq_id == id && dquot->dq_type == type)
249 return dquot;
251 return NODQUOT;
254 /* Add a dquot to the tail of the free list */
255 static inline void put_dquot_last(struct dquot *dquot)
257 list_add_tail(&dquot->dq_free, &free_dquots);
258 dqstats.free_dquots++;
261 static inline void remove_free_dquot(struct dquot *dquot)
263 if (list_empty(&dquot->dq_free))
264 return;
265 list_del_init(&dquot->dq_free);
266 dqstats.free_dquots--;
269 static inline void put_inuse(struct dquot *dquot)
271 /* We add to the back of inuse list so we don't have to restart
272 * when traversing this list and we block */
273 list_add_tail(&dquot->dq_inuse, &inuse_list);
274 dqstats.allocated_dquots++;
277 static inline void remove_inuse(struct dquot *dquot)
279 dqstats.allocated_dquots--;
280 list_del(&dquot->dq_inuse);
283 * End of list functions needing dq_list_lock
286 static void wait_on_dquot(struct dquot *dquot)
288 mutex_lock(&dquot->dq_lock);
289 mutex_unlock(&dquot->dq_lock);
292 static inline int dquot_dirty(struct dquot *dquot)
294 return test_bit(DQ_MOD_B, &dquot->dq_flags);
297 static inline int mark_dquot_dirty(struct dquot *dquot)
299 return dquot->dq_sb->dq_op->mark_dirty(dquot);
302 int dquot_mark_dquot_dirty(struct dquot *dquot)
304 spin_lock(&dq_list_lock);
305 if (!test_and_set_bit(DQ_MOD_B, &dquot->dq_flags))
306 list_add(&dquot->dq_dirty, &sb_dqopt(dquot->dq_sb)->
307 info[dquot->dq_type].dqi_dirty_list);
308 spin_unlock(&dq_list_lock);
309 return 0;
312 /* This function needs dq_list_lock */
313 static inline int clear_dquot_dirty(struct dquot *dquot)
315 if (!test_and_clear_bit(DQ_MOD_B, &dquot->dq_flags))
316 return 0;
317 list_del_init(&dquot->dq_dirty);
318 return 1;
321 void mark_info_dirty(struct super_block *sb, int type)
323 set_bit(DQF_INFO_DIRTY_B, &sb_dqopt(sb)->info[type].dqi_flags);
325 EXPORT_SYMBOL(mark_info_dirty);
328 * Read dquot from disk and alloc space for it
331 int dquot_acquire(struct dquot *dquot)
333 int ret = 0, ret2 = 0;
334 struct quota_info *dqopt = sb_dqopt(dquot->dq_sb);
336 mutex_lock(&dquot->dq_lock);
337 mutex_lock(&dqopt->dqio_mutex);
338 if (!test_bit(DQ_READ_B, &dquot->dq_flags))
339 ret = dqopt->ops[dquot->dq_type]->read_dqblk(dquot);
340 if (ret < 0)
341 goto out_iolock;
342 set_bit(DQ_READ_B, &dquot->dq_flags);
343 /* Instantiate dquot if needed */
344 if (!test_bit(DQ_ACTIVE_B, &dquot->dq_flags) && !dquot->dq_off) {
345 ret = dqopt->ops[dquot->dq_type]->commit_dqblk(dquot);
346 /* Write the info if needed */
347 if (info_dirty(&dqopt->info[dquot->dq_type]))
348 ret2 = dqopt->ops[dquot->dq_type]->write_file_info(dquot->dq_sb, dquot->dq_type);
349 if (ret < 0)
350 goto out_iolock;
351 if (ret2 < 0) {
352 ret = ret2;
353 goto out_iolock;
356 set_bit(DQ_ACTIVE_B, &dquot->dq_flags);
357 out_iolock:
358 mutex_unlock(&dqopt->dqio_mutex);
359 mutex_unlock(&dquot->dq_lock);
360 return ret;
364 * Write dquot to disk
366 int dquot_commit(struct dquot *dquot)
368 int ret = 0;
369 struct quota_info *dqopt = sb_dqopt(dquot->dq_sb);
371 mutex_lock(&dqopt->dqio_mutex);
372 spin_lock(&dq_list_lock);
373 if (!clear_dquot_dirty(dquot)) {
374 spin_unlock(&dq_list_lock);
375 goto out_sem;
377 spin_unlock(&dq_list_lock);
378 /* Inactive dquot can be only if there was error during read/init
379 * => we have better not writing it */
380 if (test_bit(DQ_ACTIVE_B, &dquot->dq_flags))
381 ret = dqopt->ops[dquot->dq_type]->commit_dqblk(dquot);
382 else
383 ret = -EIO;
384 out_sem:
385 mutex_unlock(&dqopt->dqio_mutex);
386 return ret;
390 * Release dquot
392 int dquot_release(struct dquot *dquot)
394 int ret = 0, ret2 = 0;
395 struct quota_info *dqopt = sb_dqopt(dquot->dq_sb);
397 mutex_lock(&dquot->dq_lock);
398 /* Check whether we are not racing with some other dqget() */
399 if (atomic_read(&dquot->dq_count) > 1)
400 goto out_dqlock;
401 mutex_lock(&dqopt->dqio_mutex);
402 if (dqopt->ops[dquot->dq_type]->release_dqblk) {
403 ret = dqopt->ops[dquot->dq_type]->release_dqblk(dquot);
404 /* Write the info */
405 if (info_dirty(&dqopt->info[dquot->dq_type]))
406 ret2 = dqopt->ops[dquot->dq_type]->write_file_info(dquot->dq_sb, dquot->dq_type);
407 if (ret >= 0)
408 ret = ret2;
410 clear_bit(DQ_ACTIVE_B, &dquot->dq_flags);
411 mutex_unlock(&dqopt->dqio_mutex);
412 out_dqlock:
413 mutex_unlock(&dquot->dq_lock);
414 return ret;
417 /* Invalidate all dquots on the list. Note that this function is called after
418 * quota is disabled and pointers from inodes removed so there cannot be new
419 * quota users. There can still be some users of quotas due to inodes being
420 * just deleted or pruned by prune_icache() (those are not attached to any
421 * list). We have to wait for such users.
423 static void invalidate_dquots(struct super_block *sb, int type)
425 struct dquot *dquot, *tmp;
427 restart:
428 spin_lock(&dq_list_lock);
429 list_for_each_entry_safe(dquot, tmp, &inuse_list, dq_inuse) {
430 if (dquot->dq_sb != sb)
431 continue;
432 if (dquot->dq_type != type)
433 continue;
434 /* Wait for dquot users */
435 if (atomic_read(&dquot->dq_count)) {
436 DEFINE_WAIT(wait);
438 atomic_inc(&dquot->dq_count);
439 prepare_to_wait(&dquot->dq_wait_unused, &wait,
440 TASK_UNINTERRUPTIBLE);
441 spin_unlock(&dq_list_lock);
442 /* Once dqput() wakes us up, we know it's time to free
443 * the dquot.
444 * IMPORTANT: we rely on the fact that there is always
445 * at most one process waiting for dquot to free.
446 * Otherwise dq_count would be > 1 and we would never
447 * wake up.
449 if (atomic_read(&dquot->dq_count) > 1)
450 schedule();
451 finish_wait(&dquot->dq_wait_unused, &wait);
452 dqput(dquot);
453 /* At this moment dquot() need not exist (it could be
454 * reclaimed by prune_dqcache(). Hence we must
455 * restart. */
456 goto restart;
459 * Quota now has no users and it has been written on last
460 * dqput()
462 remove_dquot_hash(dquot);
463 remove_free_dquot(dquot);
464 remove_inuse(dquot);
465 kmem_cache_free(dquot_cachep, dquot);
467 spin_unlock(&dq_list_lock);
470 int vfs_quota_sync(struct super_block *sb, int type)
472 struct list_head *dirty;
473 struct dquot *dquot;
474 struct quota_info *dqopt = sb_dqopt(sb);
475 int cnt;
477 mutex_lock(&dqopt->dqonoff_mutex);
478 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
479 if (type != -1 && cnt != type)
480 continue;
481 if (!sb_has_quota_enabled(sb, cnt))
482 continue;
483 spin_lock(&dq_list_lock);
484 dirty = &dqopt->info[cnt].dqi_dirty_list;
485 while (!list_empty(dirty)) {
486 dquot = list_first_entry(dirty, struct dquot, dq_dirty);
487 /* Dirty and inactive can be only bad dquot... */
488 if (!test_bit(DQ_ACTIVE_B, &dquot->dq_flags)) {
489 clear_dquot_dirty(dquot);
490 continue;
492 /* Now we have active dquot from which someone is
493 * holding reference so we can safely just increase
494 * use count */
495 atomic_inc(&dquot->dq_count);
496 dqstats.lookups++;
497 spin_unlock(&dq_list_lock);
498 sb->dq_op->write_dquot(dquot);
499 dqput(dquot);
500 spin_lock(&dq_list_lock);
502 spin_unlock(&dq_list_lock);
505 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
506 if ((cnt == type || type == -1) && sb_has_quota_enabled(sb, cnt)
507 && info_dirty(&dqopt->info[cnt]))
508 sb->dq_op->write_info(sb, cnt);
509 spin_lock(&dq_list_lock);
510 dqstats.syncs++;
511 spin_unlock(&dq_list_lock);
512 mutex_unlock(&dqopt->dqonoff_mutex);
514 return 0;
517 /* Free unused dquots from cache */
518 static void prune_dqcache(int count)
520 struct list_head *head;
521 struct dquot *dquot;
523 head = free_dquots.prev;
524 while (head != &free_dquots && count) {
525 dquot = list_entry(head, struct dquot, dq_free);
526 remove_dquot_hash(dquot);
527 remove_free_dquot(dquot);
528 remove_inuse(dquot);
529 kmem_cache_free(dquot_cachep, dquot);
530 count--;
531 head = free_dquots.prev;
536 * This is called from kswapd when we think we need some
537 * more memory
540 static int shrink_dqcache_memory(int nr, gfp_t gfp_mask)
542 if (nr) {
543 spin_lock(&dq_list_lock);
544 prune_dqcache(nr);
545 spin_unlock(&dq_list_lock);
547 return (dqstats.free_dquots / 100) * sysctl_vfs_cache_pressure;
550 static struct shrinker dqcache_shrinker = {
551 .shrink = shrink_dqcache_memory,
552 .seeks = DEFAULT_SEEKS,
556 * Put reference to dquot
557 * NOTE: If you change this function please check whether dqput_blocks() works right...
558 * MUST be called with either dqptr_sem or dqonoff_mutex held
560 static void dqput(struct dquot *dquot)
562 int ret;
564 if (!dquot)
565 return;
566 #ifdef __DQUOT_PARANOIA
567 if (!atomic_read(&dquot->dq_count)) {
568 printk("VFS: dqput: trying to free free dquot\n");
569 printk("VFS: device %s, dquot of %s %d\n",
570 dquot->dq_sb->s_id,
571 quotatypes[dquot->dq_type],
572 dquot->dq_id);
573 BUG();
575 #endif
577 spin_lock(&dq_list_lock);
578 dqstats.drops++;
579 spin_unlock(&dq_list_lock);
580 we_slept:
581 spin_lock(&dq_list_lock);
582 if (atomic_read(&dquot->dq_count) > 1) {
583 /* We have more than one user... nothing to do */
584 atomic_dec(&dquot->dq_count);
585 /* Releasing dquot during quotaoff phase? */
586 if (!sb_has_quota_enabled(dquot->dq_sb, dquot->dq_type) &&
587 atomic_read(&dquot->dq_count) == 1)
588 wake_up(&dquot->dq_wait_unused);
589 spin_unlock(&dq_list_lock);
590 return;
592 /* Need to release dquot? */
593 if (test_bit(DQ_ACTIVE_B, &dquot->dq_flags) && dquot_dirty(dquot)) {
594 spin_unlock(&dq_list_lock);
595 /* Commit dquot before releasing */
596 ret = dquot->dq_sb->dq_op->write_dquot(dquot);
597 if (ret < 0) {
598 printk(KERN_ERR "VFS: cannot write quota structure on "
599 "device %s (error %d). Quota may get out of "
600 "sync!\n", dquot->dq_sb->s_id, ret);
602 * We clear dirty bit anyway, so that we avoid
603 * infinite loop here
605 spin_lock(&dq_list_lock);
606 clear_dquot_dirty(dquot);
607 spin_unlock(&dq_list_lock);
609 goto we_slept;
611 /* Clear flag in case dquot was inactive (something bad happened) */
612 clear_dquot_dirty(dquot);
613 if (test_bit(DQ_ACTIVE_B, &dquot->dq_flags)) {
614 spin_unlock(&dq_list_lock);
615 dquot->dq_sb->dq_op->release_dquot(dquot);
616 goto we_slept;
618 atomic_dec(&dquot->dq_count);
619 #ifdef __DQUOT_PARANOIA
620 /* sanity check */
621 BUG_ON(!list_empty(&dquot->dq_free));
622 #endif
623 put_dquot_last(dquot);
624 spin_unlock(&dq_list_lock);
627 static struct dquot *get_empty_dquot(struct super_block *sb, int type)
629 struct dquot *dquot;
631 dquot = kmem_cache_zalloc(dquot_cachep, GFP_NOFS);
632 if(!dquot)
633 return NODQUOT;
635 mutex_init(&dquot->dq_lock);
636 INIT_LIST_HEAD(&dquot->dq_free);
637 INIT_LIST_HEAD(&dquot->dq_inuse);
638 INIT_HLIST_NODE(&dquot->dq_hash);
639 INIT_LIST_HEAD(&dquot->dq_dirty);
640 init_waitqueue_head(&dquot->dq_wait_unused);
641 dquot->dq_sb = sb;
642 dquot->dq_type = type;
643 atomic_set(&dquot->dq_count, 1);
645 return dquot;
649 * Get reference to dquot
650 * MUST be called with either dqptr_sem or dqonoff_mutex held
652 static struct dquot *dqget(struct super_block *sb, unsigned int id, int type)
654 unsigned int hashent = hashfn(sb, id, type);
655 struct dquot *dquot, *empty = NODQUOT;
657 if (!sb_has_quota_enabled(sb, type))
658 return NODQUOT;
659 we_slept:
660 spin_lock(&dq_list_lock);
661 if ((dquot = find_dquot(hashent, sb, id, type)) == NODQUOT) {
662 if (empty == NODQUOT) {
663 spin_unlock(&dq_list_lock);
664 if ((empty = get_empty_dquot(sb, type)) == NODQUOT)
665 schedule(); /* Try to wait for a moment... */
666 goto we_slept;
668 dquot = empty;
669 dquot->dq_id = id;
670 /* all dquots go on the inuse_list */
671 put_inuse(dquot);
672 /* hash it first so it can be found */
673 insert_dquot_hash(dquot);
674 dqstats.lookups++;
675 spin_unlock(&dq_list_lock);
676 } else {
677 if (!atomic_read(&dquot->dq_count))
678 remove_free_dquot(dquot);
679 atomic_inc(&dquot->dq_count);
680 dqstats.cache_hits++;
681 dqstats.lookups++;
682 spin_unlock(&dq_list_lock);
683 if (empty)
684 kmem_cache_free(dquot_cachep, empty);
686 /* Wait for dq_lock - after this we know that either dquot_release() is already
687 * finished or it will be canceled due to dq_count > 1 test */
688 wait_on_dquot(dquot);
689 /* Read the dquot and instantiate it (everything done only if needed) */
690 if (!test_bit(DQ_ACTIVE_B, &dquot->dq_flags) && sb->dq_op->acquire_dquot(dquot) < 0) {
691 dqput(dquot);
692 return NODQUOT;
694 #ifdef __DQUOT_PARANOIA
695 BUG_ON(!dquot->dq_sb); /* Has somebody invalidated entry under us? */
696 #endif
698 return dquot;
701 static int dqinit_needed(struct inode *inode, int type)
703 int cnt;
705 if (IS_NOQUOTA(inode))
706 return 0;
707 if (type != -1)
708 return inode->i_dquot[type] == NODQUOT;
709 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
710 if (inode->i_dquot[cnt] == NODQUOT)
711 return 1;
712 return 0;
715 /* This routine is guarded by dqonoff_mutex mutex */
716 static void add_dquot_ref(struct super_block *sb, int type)
718 struct inode *inode, *old_inode = NULL;
720 spin_lock(&inode_lock);
721 list_for_each_entry(inode, &sb->s_inodes, i_sb_list) {
722 if (!atomic_read(&inode->i_writecount))
723 continue;
724 if (!dqinit_needed(inode, type))
725 continue;
726 if (inode->i_state & (I_FREEING|I_CLEAR|I_WILL_FREE))
727 continue;
729 __iget(inode);
730 spin_unlock(&inode_lock);
732 iput(old_inode);
733 sb->dq_op->initialize(inode, type);
734 /* We hold a reference to 'inode' so it couldn't have been
735 * removed from s_inodes list while we dropped the inode_lock.
736 * We cannot iput the inode now as we can be holding the last
737 * reference and we cannot iput it under inode_lock. So we
738 * keep the reference and iput it later. */
739 old_inode = inode;
740 spin_lock(&inode_lock);
742 spin_unlock(&inode_lock);
743 iput(old_inode);
746 /* Return 0 if dqput() won't block (note that 1 doesn't necessarily mean blocking) */
747 static inline int dqput_blocks(struct dquot *dquot)
749 if (atomic_read(&dquot->dq_count) <= 1)
750 return 1;
751 return 0;
754 /* Remove references to dquots from inode - add dquot to list for freeing if needed */
755 /* We can't race with anybody because we hold dqptr_sem for writing... */
756 static int remove_inode_dquot_ref(struct inode *inode, int type,
757 struct list_head *tofree_head)
759 struct dquot *dquot = inode->i_dquot[type];
761 inode->i_dquot[type] = NODQUOT;
762 if (dquot != NODQUOT) {
763 if (dqput_blocks(dquot)) {
764 #ifdef __DQUOT_PARANOIA
765 if (atomic_read(&dquot->dq_count) != 1)
766 printk(KERN_WARNING "VFS: Adding dquot with dq_count %d to dispose list.\n", atomic_read(&dquot->dq_count));
767 #endif
768 spin_lock(&dq_list_lock);
769 list_add(&dquot->dq_free, tofree_head); /* As dquot must have currently users it can't be on the free list... */
770 spin_unlock(&dq_list_lock);
771 return 1;
773 else
774 dqput(dquot); /* We have guaranteed we won't block */
776 return 0;
779 /* Free list of dquots - called from inode.c */
780 /* dquots are removed from inodes, no new references can be got so we are the only ones holding reference */
781 static void put_dquot_list(struct list_head *tofree_head)
783 struct list_head *act_head;
784 struct dquot *dquot;
786 act_head = tofree_head->next;
787 /* So now we have dquots on the list... Just free them */
788 while (act_head != tofree_head) {
789 dquot = list_entry(act_head, struct dquot, dq_free);
790 act_head = act_head->next;
791 list_del_init(&dquot->dq_free); /* Remove dquot from the list so we won't have problems... */
792 dqput(dquot);
796 static void remove_dquot_ref(struct super_block *sb, int type,
797 struct list_head *tofree_head)
799 struct inode *inode;
801 spin_lock(&inode_lock);
802 list_for_each_entry(inode, &sb->s_inodes, i_sb_list) {
803 if (!IS_NOQUOTA(inode))
804 remove_inode_dquot_ref(inode, type, tofree_head);
806 spin_unlock(&inode_lock);
809 /* Gather all references from inodes and drop them */
810 static void drop_dquot_ref(struct super_block *sb, int type)
812 LIST_HEAD(tofree_head);
814 if (sb->dq_op) {
815 down_write(&sb_dqopt(sb)->dqptr_sem);
816 remove_dquot_ref(sb, type, &tofree_head);
817 up_write(&sb_dqopt(sb)->dqptr_sem);
818 put_dquot_list(&tofree_head);
822 static inline void dquot_incr_inodes(struct dquot *dquot, unsigned long number)
824 dquot->dq_dqb.dqb_curinodes += number;
827 static inline void dquot_incr_space(struct dquot *dquot, qsize_t number)
829 dquot->dq_dqb.dqb_curspace += number;
832 static inline void dquot_decr_inodes(struct dquot *dquot, unsigned long number)
834 if (dquot->dq_dqb.dqb_curinodes > number)
835 dquot->dq_dqb.dqb_curinodes -= number;
836 else
837 dquot->dq_dqb.dqb_curinodes = 0;
838 if (dquot->dq_dqb.dqb_curinodes <= dquot->dq_dqb.dqb_isoftlimit)
839 dquot->dq_dqb.dqb_itime = (time_t) 0;
840 clear_bit(DQ_INODES_B, &dquot->dq_flags);
843 static inline void dquot_decr_space(struct dquot *dquot, qsize_t number)
845 if (dquot->dq_dqb.dqb_curspace > number)
846 dquot->dq_dqb.dqb_curspace -= number;
847 else
848 dquot->dq_dqb.dqb_curspace = 0;
849 if (toqb(dquot->dq_dqb.dqb_curspace) <= dquot->dq_dqb.dqb_bsoftlimit)
850 dquot->dq_dqb.dqb_btime = (time_t) 0;
851 clear_bit(DQ_BLKS_B, &dquot->dq_flags);
854 static int warning_issued(struct dquot *dquot, const int warntype)
856 int flag = (warntype == QUOTA_NL_BHARDWARN ||
857 warntype == QUOTA_NL_BSOFTLONGWARN) ? DQ_BLKS_B :
858 ((warntype == QUOTA_NL_IHARDWARN ||
859 warntype == QUOTA_NL_ISOFTLONGWARN) ? DQ_INODES_B : 0);
861 if (!flag)
862 return 0;
863 return test_and_set_bit(flag, &dquot->dq_flags);
866 #ifdef CONFIG_PRINT_QUOTA_WARNING
867 static int flag_print_warnings = 1;
869 static inline int need_print_warning(struct dquot *dquot)
871 if (!flag_print_warnings)
872 return 0;
874 switch (dquot->dq_type) {
875 case USRQUOTA:
876 return current->fsuid == dquot->dq_id;
877 case GRPQUOTA:
878 return in_group_p(dquot->dq_id);
880 return 0;
883 /* Print warning to user which exceeded quota */
884 static void print_warning(struct dquot *dquot, const int warntype)
886 char *msg = NULL;
887 struct tty_struct *tty;
889 if (warntype == QUOTA_NL_IHARDBELOW ||
890 warntype == QUOTA_NL_ISOFTBELOW ||
891 warntype == QUOTA_NL_BHARDBELOW ||
892 warntype == QUOTA_NL_BSOFTBELOW || !need_print_warning(dquot))
893 return;
895 mutex_lock(&tty_mutex);
896 tty = get_current_tty();
897 if (!tty)
898 goto out_lock;
899 tty_write_message(tty, dquot->dq_sb->s_id);
900 if (warntype == QUOTA_NL_ISOFTWARN || warntype == QUOTA_NL_BSOFTWARN)
901 tty_write_message(tty, ": warning, ");
902 else
903 tty_write_message(tty, ": write failed, ");
904 tty_write_message(tty, quotatypes[dquot->dq_type]);
905 switch (warntype) {
906 case QUOTA_NL_IHARDWARN:
907 msg = " file limit reached.\r\n";
908 break;
909 case QUOTA_NL_ISOFTLONGWARN:
910 msg = " file quota exceeded too long.\r\n";
911 break;
912 case QUOTA_NL_ISOFTWARN:
913 msg = " file quota exceeded.\r\n";
914 break;
915 case QUOTA_NL_BHARDWARN:
916 msg = " block limit reached.\r\n";
917 break;
918 case QUOTA_NL_BSOFTLONGWARN:
919 msg = " block quota exceeded too long.\r\n";
920 break;
921 case QUOTA_NL_BSOFTWARN:
922 msg = " block quota exceeded.\r\n";
923 break;
925 tty_write_message(tty, msg);
926 out_lock:
927 mutex_unlock(&tty_mutex);
929 #endif
931 #ifdef CONFIG_QUOTA_NETLINK_INTERFACE
933 /* Netlink family structure for quota */
934 static struct genl_family quota_genl_family = {
935 .id = GENL_ID_GENERATE,
936 .hdrsize = 0,
937 .name = "VFS_DQUOT",
938 .version = 1,
939 .maxattr = QUOTA_NL_A_MAX,
942 /* Send warning to userspace about user which exceeded quota */
943 static void send_warning(const struct dquot *dquot, const char warntype)
945 static atomic_t seq;
946 struct sk_buff *skb;
947 void *msg_head;
948 int ret;
949 int msg_size = 4 * nla_total_size(sizeof(u32)) +
950 2 * nla_total_size(sizeof(u64));
952 /* We have to allocate using GFP_NOFS as we are called from a
953 * filesystem performing write and thus further recursion into
954 * the fs to free some data could cause deadlocks. */
955 skb = genlmsg_new(msg_size, GFP_NOFS);
956 if (!skb) {
957 printk(KERN_ERR
958 "VFS: Not enough memory to send quota warning.\n");
959 return;
961 msg_head = genlmsg_put(skb, 0, atomic_add_return(1, &seq),
962 &quota_genl_family, 0, QUOTA_NL_C_WARNING);
963 if (!msg_head) {
964 printk(KERN_ERR
965 "VFS: Cannot store netlink header in quota warning.\n");
966 goto err_out;
968 ret = nla_put_u32(skb, QUOTA_NL_A_QTYPE, dquot->dq_type);
969 if (ret)
970 goto attr_err_out;
971 ret = nla_put_u64(skb, QUOTA_NL_A_EXCESS_ID, dquot->dq_id);
972 if (ret)
973 goto attr_err_out;
974 ret = nla_put_u32(skb, QUOTA_NL_A_WARNING, warntype);
975 if (ret)
976 goto attr_err_out;
977 ret = nla_put_u32(skb, QUOTA_NL_A_DEV_MAJOR,
978 MAJOR(dquot->dq_sb->s_dev));
979 if (ret)
980 goto attr_err_out;
981 ret = nla_put_u32(skb, QUOTA_NL_A_DEV_MINOR,
982 MINOR(dquot->dq_sb->s_dev));
983 if (ret)
984 goto attr_err_out;
985 ret = nla_put_u64(skb, QUOTA_NL_A_CAUSED_ID, current->user->uid);
986 if (ret)
987 goto attr_err_out;
988 genlmsg_end(skb, msg_head);
990 ret = genlmsg_multicast(skb, 0, quota_genl_family.id, GFP_NOFS);
991 if (ret < 0 && ret != -ESRCH)
992 printk(KERN_ERR
993 "VFS: Failed to send notification message: %d\n", ret);
994 return;
995 attr_err_out:
996 printk(KERN_ERR "VFS: Not enough space to compose quota message!\n");
997 err_out:
998 kfree_skb(skb);
1000 #endif
1002 static inline void flush_warnings(struct dquot * const *dquots, char *warntype)
1004 int i;
1006 for (i = 0; i < MAXQUOTAS; i++)
1007 if (dquots[i] != NODQUOT && warntype[i] != QUOTA_NL_NOWARN &&
1008 !warning_issued(dquots[i], warntype[i])) {
1009 #ifdef CONFIG_PRINT_QUOTA_WARNING
1010 print_warning(dquots[i], warntype[i]);
1011 #endif
1012 #ifdef CONFIG_QUOTA_NETLINK_INTERFACE
1013 send_warning(dquots[i], warntype[i]);
1014 #endif
1018 static inline char ignore_hardlimit(struct dquot *dquot)
1020 struct mem_dqinfo *info = &sb_dqopt(dquot->dq_sb)->info[dquot->dq_type];
1022 return capable(CAP_SYS_RESOURCE) &&
1023 (info->dqi_format->qf_fmt_id != QFMT_VFS_OLD || !(info->dqi_flags & V1_DQF_RSQUASH));
1026 /* needs dq_data_lock */
1027 static int check_idq(struct dquot *dquot, ulong inodes, char *warntype)
1029 *warntype = QUOTA_NL_NOWARN;
1030 if (inodes <= 0 || test_bit(DQ_FAKE_B, &dquot->dq_flags))
1031 return QUOTA_OK;
1033 if (dquot->dq_dqb.dqb_ihardlimit &&
1034 (dquot->dq_dqb.dqb_curinodes + inodes) > dquot->dq_dqb.dqb_ihardlimit &&
1035 !ignore_hardlimit(dquot)) {
1036 *warntype = QUOTA_NL_IHARDWARN;
1037 return NO_QUOTA;
1040 if (dquot->dq_dqb.dqb_isoftlimit &&
1041 (dquot->dq_dqb.dqb_curinodes + inodes) > dquot->dq_dqb.dqb_isoftlimit &&
1042 dquot->dq_dqb.dqb_itime && get_seconds() >= dquot->dq_dqb.dqb_itime &&
1043 !ignore_hardlimit(dquot)) {
1044 *warntype = QUOTA_NL_ISOFTLONGWARN;
1045 return NO_QUOTA;
1048 if (dquot->dq_dqb.dqb_isoftlimit &&
1049 (dquot->dq_dqb.dqb_curinodes + inodes) > dquot->dq_dqb.dqb_isoftlimit &&
1050 dquot->dq_dqb.dqb_itime == 0) {
1051 *warntype = QUOTA_NL_ISOFTWARN;
1052 dquot->dq_dqb.dqb_itime = get_seconds() + sb_dqopt(dquot->dq_sb)->info[dquot->dq_type].dqi_igrace;
1055 return QUOTA_OK;
1058 /* needs dq_data_lock */
1059 static int check_bdq(struct dquot *dquot, qsize_t space, int prealloc, char *warntype)
1061 *warntype = QUOTA_NL_NOWARN;
1062 if (space <= 0 || test_bit(DQ_FAKE_B, &dquot->dq_flags))
1063 return QUOTA_OK;
1065 if (dquot->dq_dqb.dqb_bhardlimit &&
1066 toqb(dquot->dq_dqb.dqb_curspace + space) > dquot->dq_dqb.dqb_bhardlimit &&
1067 !ignore_hardlimit(dquot)) {
1068 if (!prealloc)
1069 *warntype = QUOTA_NL_BHARDWARN;
1070 return NO_QUOTA;
1073 if (dquot->dq_dqb.dqb_bsoftlimit &&
1074 toqb(dquot->dq_dqb.dqb_curspace + space) > dquot->dq_dqb.dqb_bsoftlimit &&
1075 dquot->dq_dqb.dqb_btime && get_seconds() >= dquot->dq_dqb.dqb_btime &&
1076 !ignore_hardlimit(dquot)) {
1077 if (!prealloc)
1078 *warntype = QUOTA_NL_BSOFTLONGWARN;
1079 return NO_QUOTA;
1082 if (dquot->dq_dqb.dqb_bsoftlimit &&
1083 toqb(dquot->dq_dqb.dqb_curspace + space) > dquot->dq_dqb.dqb_bsoftlimit &&
1084 dquot->dq_dqb.dqb_btime == 0) {
1085 if (!prealloc) {
1086 *warntype = QUOTA_NL_BSOFTWARN;
1087 dquot->dq_dqb.dqb_btime = get_seconds() + sb_dqopt(dquot->dq_sb)->info[dquot->dq_type].dqi_bgrace;
1089 else
1091 * We don't allow preallocation to exceed softlimit so exceeding will
1092 * be always printed
1094 return NO_QUOTA;
1097 return QUOTA_OK;
1100 static int info_idq_free(struct dquot *dquot, ulong inodes)
1102 if (test_bit(DQ_FAKE_B, &dquot->dq_flags) ||
1103 dquot->dq_dqb.dqb_curinodes <= dquot->dq_dqb.dqb_isoftlimit)
1104 return QUOTA_NL_NOWARN;
1106 if (dquot->dq_dqb.dqb_curinodes - inodes <= dquot->dq_dqb.dqb_isoftlimit)
1107 return QUOTA_NL_ISOFTBELOW;
1108 if (dquot->dq_dqb.dqb_curinodes >= dquot->dq_dqb.dqb_ihardlimit &&
1109 dquot->dq_dqb.dqb_curinodes - inodes < dquot->dq_dqb.dqb_ihardlimit)
1110 return QUOTA_NL_IHARDBELOW;
1111 return QUOTA_NL_NOWARN;
1114 static int info_bdq_free(struct dquot *dquot, qsize_t space)
1116 if (test_bit(DQ_FAKE_B, &dquot->dq_flags) ||
1117 toqb(dquot->dq_dqb.dqb_curspace) <= dquot->dq_dqb.dqb_bsoftlimit)
1118 return QUOTA_NL_NOWARN;
1120 if (toqb(dquot->dq_dqb.dqb_curspace - space) <=
1121 dquot->dq_dqb.dqb_bsoftlimit)
1122 return QUOTA_NL_BSOFTBELOW;
1123 if (toqb(dquot->dq_dqb.dqb_curspace) >= dquot->dq_dqb.dqb_bhardlimit &&
1124 toqb(dquot->dq_dqb.dqb_curspace - space) <
1125 dquot->dq_dqb.dqb_bhardlimit)
1126 return QUOTA_NL_BHARDBELOW;
1127 return QUOTA_NL_NOWARN;
1130 * Initialize quota pointers in inode
1131 * Transaction must be started at entry
1133 int dquot_initialize(struct inode *inode, int type)
1135 unsigned int id = 0;
1136 int cnt, ret = 0;
1138 /* First test before acquiring mutex - solves deadlocks when we
1139 * re-enter the quota code and are already holding the mutex */
1140 if (IS_NOQUOTA(inode))
1141 return 0;
1142 down_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
1143 /* Having dqptr_sem we know NOQUOTA flags can't be altered... */
1144 if (IS_NOQUOTA(inode))
1145 goto out_err;
1146 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1147 if (type != -1 && cnt != type)
1148 continue;
1149 if (inode->i_dquot[cnt] == NODQUOT) {
1150 switch (cnt) {
1151 case USRQUOTA:
1152 id = inode->i_uid;
1153 break;
1154 case GRPQUOTA:
1155 id = inode->i_gid;
1156 break;
1158 inode->i_dquot[cnt] = dqget(inode->i_sb, id, cnt);
1161 out_err:
1162 up_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
1163 return ret;
1167 * Release all quotas referenced by inode
1168 * Transaction must be started at an entry
1170 int dquot_drop(struct inode *inode)
1172 int cnt;
1174 down_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
1175 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1176 if (inode->i_dquot[cnt] != NODQUOT) {
1177 dqput(inode->i_dquot[cnt]);
1178 inode->i_dquot[cnt] = NODQUOT;
1181 up_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
1182 return 0;
1185 /* Wrapper to remove references to quota structures from inode */
1186 void vfs_dq_drop(struct inode *inode)
1188 /* Here we can get arbitrary inode from clear_inode() so we have
1189 * to be careful. OTOH we don't need locking as quota operations
1190 * are allowed to change only at mount time */
1191 if (!IS_NOQUOTA(inode) && inode->i_sb && inode->i_sb->dq_op
1192 && inode->i_sb->dq_op->drop) {
1193 int cnt;
1194 /* Test before calling to rule out calls from proc and such
1195 * where we are not allowed to block. Note that this is
1196 * actually reliable test even without the lock - the caller
1197 * must assure that nobody can come after the DQUOT_DROP and
1198 * add quota pointers back anyway */
1199 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1200 if (inode->i_dquot[cnt] != NODQUOT)
1201 break;
1202 if (cnt < MAXQUOTAS)
1203 inode->i_sb->dq_op->drop(inode);
1208 * Following four functions update i_blocks+i_bytes fields and
1209 * quota information (together with appropriate checks)
1210 * NOTE: We absolutely rely on the fact that caller dirties
1211 * the inode (usually macros in quotaops.h care about this) and
1212 * holds a handle for the current transaction so that dquot write and
1213 * inode write go into the same transaction.
1217 * This operation can block, but only after everything is updated
1219 int dquot_alloc_space(struct inode *inode, qsize_t number, int warn)
1221 int cnt, ret = NO_QUOTA;
1222 char warntype[MAXQUOTAS];
1224 /* First test before acquiring mutex - solves deadlocks when we
1225 * re-enter the quota code and are already holding the mutex */
1226 if (IS_NOQUOTA(inode)) {
1227 out_add:
1228 inode_add_bytes(inode, number);
1229 return QUOTA_OK;
1231 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1232 warntype[cnt] = QUOTA_NL_NOWARN;
1234 down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1235 if (IS_NOQUOTA(inode)) { /* Now we can do reliable test... */
1236 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1237 goto out_add;
1239 spin_lock(&dq_data_lock);
1240 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1241 if (inode->i_dquot[cnt] == NODQUOT)
1242 continue;
1243 if (check_bdq(inode->i_dquot[cnt], number, warn, warntype+cnt) == NO_QUOTA)
1244 goto warn_put_all;
1246 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1247 if (inode->i_dquot[cnt] == NODQUOT)
1248 continue;
1249 dquot_incr_space(inode->i_dquot[cnt], number);
1251 inode_add_bytes(inode, number);
1252 ret = QUOTA_OK;
1253 warn_put_all:
1254 spin_unlock(&dq_data_lock);
1255 if (ret == QUOTA_OK)
1256 /* Dirtify all the dquots - this can block when journalling */
1257 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1258 if (inode->i_dquot[cnt])
1259 mark_dquot_dirty(inode->i_dquot[cnt]);
1260 flush_warnings(inode->i_dquot, warntype);
1261 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1262 return ret;
1266 * This operation can block, but only after everything is updated
1268 int dquot_alloc_inode(const struct inode *inode, unsigned long number)
1270 int cnt, ret = NO_QUOTA;
1271 char warntype[MAXQUOTAS];
1273 /* First test before acquiring mutex - solves deadlocks when we
1274 * re-enter the quota code and are already holding the mutex */
1275 if (IS_NOQUOTA(inode))
1276 return QUOTA_OK;
1277 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1278 warntype[cnt] = QUOTA_NL_NOWARN;
1279 down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1280 if (IS_NOQUOTA(inode)) {
1281 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1282 return QUOTA_OK;
1284 spin_lock(&dq_data_lock);
1285 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1286 if (inode->i_dquot[cnt] == NODQUOT)
1287 continue;
1288 if (check_idq(inode->i_dquot[cnt], number, warntype+cnt) == NO_QUOTA)
1289 goto warn_put_all;
1292 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1293 if (inode->i_dquot[cnt] == NODQUOT)
1294 continue;
1295 dquot_incr_inodes(inode->i_dquot[cnt], number);
1297 ret = QUOTA_OK;
1298 warn_put_all:
1299 spin_unlock(&dq_data_lock);
1300 if (ret == QUOTA_OK)
1301 /* Dirtify all the dquots - this can block when journalling */
1302 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1303 if (inode->i_dquot[cnt])
1304 mark_dquot_dirty(inode->i_dquot[cnt]);
1305 flush_warnings(inode->i_dquot, warntype);
1306 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1307 return ret;
1311 * This operation can block, but only after everything is updated
1313 int dquot_free_space(struct inode *inode, qsize_t number)
1315 unsigned int cnt;
1316 char warntype[MAXQUOTAS];
1318 /* First test before acquiring mutex - solves deadlocks when we
1319 * re-enter the quota code and are already holding the mutex */
1320 if (IS_NOQUOTA(inode)) {
1321 out_sub:
1322 inode_sub_bytes(inode, number);
1323 return QUOTA_OK;
1326 down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1327 /* Now recheck reliably when holding dqptr_sem */
1328 if (IS_NOQUOTA(inode)) {
1329 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1330 goto out_sub;
1332 spin_lock(&dq_data_lock);
1333 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1334 if (inode->i_dquot[cnt] == NODQUOT)
1335 continue;
1336 warntype[cnt] = info_bdq_free(inode->i_dquot[cnt], number);
1337 dquot_decr_space(inode->i_dquot[cnt], number);
1339 inode_sub_bytes(inode, number);
1340 spin_unlock(&dq_data_lock);
1341 /* Dirtify all the dquots - this can block when journalling */
1342 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1343 if (inode->i_dquot[cnt])
1344 mark_dquot_dirty(inode->i_dquot[cnt]);
1345 flush_warnings(inode->i_dquot, warntype);
1346 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1347 return QUOTA_OK;
1351 * This operation can block, but only after everything is updated
1353 int dquot_free_inode(const struct inode *inode, unsigned long number)
1355 unsigned int cnt;
1356 char warntype[MAXQUOTAS];
1358 /* First test before acquiring mutex - solves deadlocks when we
1359 * re-enter the quota code and are already holding the mutex */
1360 if (IS_NOQUOTA(inode))
1361 return QUOTA_OK;
1363 down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1364 /* Now recheck reliably when holding dqptr_sem */
1365 if (IS_NOQUOTA(inode)) {
1366 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1367 return QUOTA_OK;
1369 spin_lock(&dq_data_lock);
1370 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1371 if (inode->i_dquot[cnt] == NODQUOT)
1372 continue;
1373 warntype[cnt] = info_idq_free(inode->i_dquot[cnt], number);
1374 dquot_decr_inodes(inode->i_dquot[cnt], number);
1376 spin_unlock(&dq_data_lock);
1377 /* Dirtify all the dquots - this can block when journalling */
1378 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1379 if (inode->i_dquot[cnt])
1380 mark_dquot_dirty(inode->i_dquot[cnt]);
1381 flush_warnings(inode->i_dquot, warntype);
1382 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1383 return QUOTA_OK;
1387 * Transfer the number of inode and blocks from one diskquota to an other.
1389 * This operation can block, but only after everything is updated
1390 * A transaction must be started when entering this function.
1392 int dquot_transfer(struct inode *inode, struct iattr *iattr)
1394 qsize_t space;
1395 struct dquot *transfer_from[MAXQUOTAS];
1396 struct dquot *transfer_to[MAXQUOTAS];
1397 int cnt, ret = NO_QUOTA, chuid = (iattr->ia_valid & ATTR_UID) && inode->i_uid != iattr->ia_uid,
1398 chgid = (iattr->ia_valid & ATTR_GID) && inode->i_gid != iattr->ia_gid;
1399 char warntype_to[MAXQUOTAS];
1400 char warntype_from_inodes[MAXQUOTAS], warntype_from_space[MAXQUOTAS];
1402 /* First test before acquiring mutex - solves deadlocks when we
1403 * re-enter the quota code and are already holding the mutex */
1404 if (IS_NOQUOTA(inode))
1405 return QUOTA_OK;
1406 /* Clear the arrays */
1407 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1408 transfer_to[cnt] = transfer_from[cnt] = NODQUOT;
1409 warntype_to[cnt] = QUOTA_NL_NOWARN;
1411 down_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
1412 /* Now recheck reliably when holding dqptr_sem */
1413 if (IS_NOQUOTA(inode)) { /* File without quota accounting? */
1414 up_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
1415 return QUOTA_OK;
1417 /* First build the transfer_to list - here we can block on
1418 * reading/instantiating of dquots. We know that the transaction for
1419 * us was already started so we don't violate lock ranking here */
1420 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1421 switch (cnt) {
1422 case USRQUOTA:
1423 if (!chuid)
1424 continue;
1425 transfer_to[cnt] = dqget(inode->i_sb, iattr->ia_uid, cnt);
1426 break;
1427 case GRPQUOTA:
1428 if (!chgid)
1429 continue;
1430 transfer_to[cnt] = dqget(inode->i_sb, iattr->ia_gid, cnt);
1431 break;
1434 spin_lock(&dq_data_lock);
1435 space = inode_get_bytes(inode);
1436 /* Build the transfer_from list and check the limits */
1437 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1438 if (transfer_to[cnt] == NODQUOT)
1439 continue;
1440 transfer_from[cnt] = inode->i_dquot[cnt];
1441 if (check_idq(transfer_to[cnt], 1, warntype_to + cnt) ==
1442 NO_QUOTA || check_bdq(transfer_to[cnt], space, 0,
1443 warntype_to + cnt) == NO_QUOTA)
1444 goto warn_put_all;
1448 * Finally perform the needed transfer from transfer_from to transfer_to
1450 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1452 * Skip changes for same uid or gid or for turned off quota-type.
1454 if (transfer_to[cnt] == NODQUOT)
1455 continue;
1457 /* Due to IO error we might not have transfer_from[] structure */
1458 if (transfer_from[cnt]) {
1459 warntype_from_inodes[cnt] =
1460 info_idq_free(transfer_from[cnt], 1);
1461 warntype_from_space[cnt] =
1462 info_bdq_free(transfer_from[cnt], space);
1463 dquot_decr_inodes(transfer_from[cnt], 1);
1464 dquot_decr_space(transfer_from[cnt], space);
1467 dquot_incr_inodes(transfer_to[cnt], 1);
1468 dquot_incr_space(transfer_to[cnt], space);
1470 inode->i_dquot[cnt] = transfer_to[cnt];
1472 ret = QUOTA_OK;
1473 warn_put_all:
1474 spin_unlock(&dq_data_lock);
1475 /* Dirtify all the dquots - this can block when journalling */
1476 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1477 if (transfer_from[cnt])
1478 mark_dquot_dirty(transfer_from[cnt]);
1479 if (transfer_to[cnt])
1480 mark_dquot_dirty(transfer_to[cnt]);
1482 flush_warnings(transfer_to, warntype_to);
1483 flush_warnings(transfer_from, warntype_from_inodes);
1484 flush_warnings(transfer_from, warntype_from_space);
1486 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1487 if (ret == QUOTA_OK && transfer_from[cnt] != NODQUOT)
1488 dqput(transfer_from[cnt]);
1489 if (ret == NO_QUOTA && transfer_to[cnt] != NODQUOT)
1490 dqput(transfer_to[cnt]);
1492 up_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
1493 return ret;
1496 /* Wrapper for transferring ownership of an inode */
1497 int vfs_dq_transfer(struct inode *inode, struct iattr *iattr)
1499 if (sb_any_quota_enabled(inode->i_sb) && !IS_NOQUOTA(inode)) {
1500 vfs_dq_init(inode);
1501 if (inode->i_sb->dq_op->transfer(inode, iattr) == NO_QUOTA)
1502 return 1;
1504 return 0;
1509 * Write info of quota file to disk
1511 int dquot_commit_info(struct super_block *sb, int type)
1513 int ret;
1514 struct quota_info *dqopt = sb_dqopt(sb);
1516 mutex_lock(&dqopt->dqio_mutex);
1517 ret = dqopt->ops[type]->write_file_info(sb, type);
1518 mutex_unlock(&dqopt->dqio_mutex);
1519 return ret;
1523 * Definitions of diskquota operations.
1525 struct dquot_operations dquot_operations = {
1526 .initialize = dquot_initialize,
1527 .drop = dquot_drop,
1528 .alloc_space = dquot_alloc_space,
1529 .alloc_inode = dquot_alloc_inode,
1530 .free_space = dquot_free_space,
1531 .free_inode = dquot_free_inode,
1532 .transfer = dquot_transfer,
1533 .write_dquot = dquot_commit,
1534 .acquire_dquot = dquot_acquire,
1535 .release_dquot = dquot_release,
1536 .mark_dirty = dquot_mark_dquot_dirty,
1537 .write_info = dquot_commit_info
1540 static inline void set_enable_flags(struct quota_info *dqopt, int type)
1542 switch (type) {
1543 case USRQUOTA:
1544 dqopt->flags |= DQUOT_USR_ENABLED;
1545 dqopt->flags &= ~DQUOT_USR_SUSPENDED;
1546 break;
1547 case GRPQUOTA:
1548 dqopt->flags |= DQUOT_GRP_ENABLED;
1549 dqopt->flags &= ~DQUOT_GRP_SUSPENDED;
1550 break;
1554 static inline void reset_enable_flags(struct quota_info *dqopt, int type,
1555 int remount)
1557 switch (type) {
1558 case USRQUOTA:
1559 dqopt->flags &= ~DQUOT_USR_ENABLED;
1560 if (remount)
1561 dqopt->flags |= DQUOT_USR_SUSPENDED;
1562 else
1563 dqopt->flags &= ~DQUOT_USR_SUSPENDED;
1564 break;
1565 case GRPQUOTA:
1566 dqopt->flags &= ~DQUOT_GRP_ENABLED;
1567 if (remount)
1568 dqopt->flags |= DQUOT_GRP_SUSPENDED;
1569 else
1570 dqopt->flags &= ~DQUOT_GRP_SUSPENDED;
1571 break;
1577 * Turn quota off on a device. type == -1 ==> quotaoff for all types (umount)
1579 int vfs_quota_off(struct super_block *sb, int type, int remount)
1581 int cnt, ret = 0;
1582 struct quota_info *dqopt = sb_dqopt(sb);
1583 struct inode *toputinode[MAXQUOTAS];
1585 /* We need to serialize quota_off() for device */
1586 mutex_lock(&dqopt->dqonoff_mutex);
1589 * Skip everything if there's nothing to do. We have to do this because
1590 * sometimes we are called when fill_super() failed and calling
1591 * sync_fs() in such cases does no good.
1593 if (!sb_any_quota_enabled(sb) && !sb_any_quota_suspended(sb)) {
1594 mutex_unlock(&dqopt->dqonoff_mutex);
1595 return 0;
1597 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1598 toputinode[cnt] = NULL;
1599 if (type != -1 && cnt != type)
1600 continue;
1601 /* If we keep inodes of quota files after remount and quotaoff
1602 * is called, drop kept inodes. */
1603 if (!remount && sb_has_quota_suspended(sb, cnt)) {
1604 iput(dqopt->files[cnt]);
1605 dqopt->files[cnt] = NULL;
1606 reset_enable_flags(dqopt, cnt, 0);
1607 continue;
1609 if (!sb_has_quota_enabled(sb, cnt))
1610 continue;
1611 reset_enable_flags(dqopt, cnt, remount);
1613 /* Note: these are blocking operations */
1614 drop_dquot_ref(sb, cnt);
1615 invalidate_dquots(sb, cnt);
1617 * Now all dquots should be invalidated, all writes done so we should be only
1618 * users of the info. No locks needed.
1620 if (info_dirty(&dqopt->info[cnt]))
1621 sb->dq_op->write_info(sb, cnt);
1622 if (dqopt->ops[cnt]->free_file_info)
1623 dqopt->ops[cnt]->free_file_info(sb, cnt);
1624 put_quota_format(dqopt->info[cnt].dqi_format);
1626 toputinode[cnt] = dqopt->files[cnt];
1627 if (!remount)
1628 dqopt->files[cnt] = NULL;
1629 dqopt->info[cnt].dqi_flags = 0;
1630 dqopt->info[cnt].dqi_igrace = 0;
1631 dqopt->info[cnt].dqi_bgrace = 0;
1632 dqopt->ops[cnt] = NULL;
1634 mutex_unlock(&dqopt->dqonoff_mutex);
1635 /* Sync the superblock so that buffers with quota data are written to
1636 * disk (and so userspace sees correct data afterwards). */
1637 if (sb->s_op->sync_fs)
1638 sb->s_op->sync_fs(sb, 1);
1639 sync_blockdev(sb->s_bdev);
1640 /* Now the quota files are just ordinary files and we can set the
1641 * inode flags back. Moreover we discard the pagecache so that
1642 * userspace sees the writes we did bypassing the pagecache. We
1643 * must also discard the blockdev buffers so that we see the
1644 * changes done by userspace on the next quotaon() */
1645 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1646 if (toputinode[cnt]) {
1647 mutex_lock(&dqopt->dqonoff_mutex);
1648 /* If quota was reenabled in the meantime, we have
1649 * nothing to do */
1650 if (!sb_has_quota_enabled(sb, cnt)) {
1651 mutex_lock_nested(&toputinode[cnt]->i_mutex, I_MUTEX_QUOTA);
1652 toputinode[cnt]->i_flags &= ~(S_IMMUTABLE |
1653 S_NOATIME | S_NOQUOTA);
1654 truncate_inode_pages(&toputinode[cnt]->i_data, 0);
1655 mutex_unlock(&toputinode[cnt]->i_mutex);
1656 mark_inode_dirty(toputinode[cnt]);
1658 mutex_unlock(&dqopt->dqonoff_mutex);
1659 /* On remount RO, we keep the inode pointer so that we
1660 * can reenable quota on the subsequent remount RW.
1661 * But we have better not keep inode pointer when there
1662 * is pending delete on the quota file... */
1663 if (!remount)
1664 iput(toputinode[cnt]);
1665 else if (!toputinode[cnt]->i_nlink)
1666 ret = -EBUSY;
1668 if (sb->s_bdev)
1669 invalidate_bdev(sb->s_bdev);
1670 return ret;
1674 * Turn quotas on on a device
1677 /* Helper function when we already have the inode */
1678 static int vfs_quota_on_inode(struct inode *inode, int type, int format_id)
1680 struct quota_format_type *fmt = find_quota_format(format_id);
1681 struct super_block *sb = inode->i_sb;
1682 struct quota_info *dqopt = sb_dqopt(sb);
1683 int error;
1684 int oldflags = -1;
1686 if (!fmt)
1687 return -ESRCH;
1688 if (!S_ISREG(inode->i_mode)) {
1689 error = -EACCES;
1690 goto out_fmt;
1692 if (IS_RDONLY(inode)) {
1693 error = -EROFS;
1694 goto out_fmt;
1696 if (!sb->s_op->quota_write || !sb->s_op->quota_read) {
1697 error = -EINVAL;
1698 goto out_fmt;
1701 /* As we bypass the pagecache we must now flush the inode so that
1702 * we see all the changes from userspace... */
1703 write_inode_now(inode, 1);
1704 /* And now flush the block cache so that kernel sees the changes */
1705 invalidate_bdev(sb->s_bdev);
1706 mutex_lock(&inode->i_mutex);
1707 mutex_lock(&dqopt->dqonoff_mutex);
1708 if (sb_has_quota_enabled(sb, type) ||
1709 sb_has_quota_suspended(sb, type)) {
1710 error = -EBUSY;
1711 goto out_lock;
1713 /* We don't want quota and atime on quota files (deadlocks possible)
1714 * Also nobody should write to the file - we use special IO operations
1715 * which ignore the immutable bit. */
1716 down_write(&dqopt->dqptr_sem);
1717 oldflags = inode->i_flags & (S_NOATIME | S_IMMUTABLE | S_NOQUOTA);
1718 inode->i_flags |= S_NOQUOTA | S_NOATIME | S_IMMUTABLE;
1719 up_write(&dqopt->dqptr_sem);
1720 sb->dq_op->drop(inode);
1722 error = -EIO;
1723 dqopt->files[type] = igrab(inode);
1724 if (!dqopt->files[type])
1725 goto out_lock;
1726 error = -EINVAL;
1727 if (!fmt->qf_ops->check_quota_file(sb, type))
1728 goto out_file_init;
1730 dqopt->ops[type] = fmt->qf_ops;
1731 dqopt->info[type].dqi_format = fmt;
1732 dqopt->info[type].dqi_fmt_id = format_id;
1733 INIT_LIST_HEAD(&dqopt->info[type].dqi_dirty_list);
1734 mutex_lock(&dqopt->dqio_mutex);
1735 if ((error = dqopt->ops[type]->read_file_info(sb, type)) < 0) {
1736 mutex_unlock(&dqopt->dqio_mutex);
1737 goto out_file_init;
1739 mutex_unlock(&dqopt->dqio_mutex);
1740 mutex_unlock(&inode->i_mutex);
1741 set_enable_flags(dqopt, type);
1743 add_dquot_ref(sb, type);
1744 mutex_unlock(&dqopt->dqonoff_mutex);
1746 return 0;
1748 out_file_init:
1749 dqopt->files[type] = NULL;
1750 iput(inode);
1751 out_lock:
1752 mutex_unlock(&dqopt->dqonoff_mutex);
1753 if (oldflags != -1) {
1754 down_write(&dqopt->dqptr_sem);
1755 /* Set the flags back (in the case of accidental quotaon()
1756 * on a wrong file we don't want to mess up the flags) */
1757 inode->i_flags &= ~(S_NOATIME | S_NOQUOTA | S_IMMUTABLE);
1758 inode->i_flags |= oldflags;
1759 up_write(&dqopt->dqptr_sem);
1761 mutex_unlock(&inode->i_mutex);
1762 out_fmt:
1763 put_quota_format(fmt);
1765 return error;
1768 /* Reenable quotas on remount RW */
1769 static int vfs_quota_on_remount(struct super_block *sb, int type)
1771 struct quota_info *dqopt = sb_dqopt(sb);
1772 struct inode *inode;
1773 int ret;
1775 mutex_lock(&dqopt->dqonoff_mutex);
1776 if (!sb_has_quota_suspended(sb, type)) {
1777 mutex_unlock(&dqopt->dqonoff_mutex);
1778 return 0;
1780 BUG_ON(sb_has_quota_enabled(sb, type));
1782 inode = dqopt->files[type];
1783 dqopt->files[type] = NULL;
1784 reset_enable_flags(dqopt, type, 0);
1785 mutex_unlock(&dqopt->dqonoff_mutex);
1787 ret = vfs_quota_on_inode(inode, type, dqopt->info[type].dqi_fmt_id);
1788 iput(inode);
1790 return ret;
1793 int vfs_quota_on_path(struct super_block *sb, int type, int format_id,
1794 struct path *path)
1796 int error = security_quota_on(path->dentry);
1797 if (error)
1798 return error;
1799 /* Quota file not on the same filesystem? */
1800 if (path->mnt->mnt_sb != sb)
1801 error = -EXDEV;
1802 else
1803 error = vfs_quota_on_inode(path->dentry->d_inode, type,
1804 format_id);
1805 return error;
1808 /* Actual function called from quotactl() */
1809 int vfs_quota_on(struct super_block *sb, int type, int format_id, char *path,
1810 int remount)
1812 struct nameidata nd;
1813 int error;
1815 if (remount)
1816 return vfs_quota_on_remount(sb, type);
1818 error = path_lookup(path, LOOKUP_FOLLOW, &nd);
1819 if (!error) {
1820 error = vfs_quota_on_path(sb, type, format_id, &nd.path);
1821 path_put(&nd.path);
1823 return error;
1827 * This function is used when filesystem needs to initialize quotas
1828 * during mount time.
1830 int vfs_quota_on_mount(struct super_block *sb, char *qf_name,
1831 int format_id, int type)
1833 struct dentry *dentry;
1834 int error;
1836 dentry = lookup_one_len(qf_name, sb->s_root, strlen(qf_name));
1837 if (IS_ERR(dentry))
1838 return PTR_ERR(dentry);
1840 if (!dentry->d_inode) {
1841 error = -ENOENT;
1842 goto out;
1845 error = security_quota_on(dentry);
1846 if (!error)
1847 error = vfs_quota_on_inode(dentry->d_inode, type, format_id);
1849 out:
1850 dput(dentry);
1851 return error;
1854 /* Wrapper to turn on quotas when remounting rw */
1855 int vfs_dq_quota_on_remount(struct super_block *sb)
1857 int cnt;
1858 int ret = 0, err;
1860 if (!sb->s_qcop || !sb->s_qcop->quota_on)
1861 return -ENOSYS;
1862 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1863 err = sb->s_qcop->quota_on(sb, cnt, 0, NULL, 1);
1864 if (err < 0 && !ret)
1865 ret = err;
1867 return ret;
1870 /* Generic routine for getting common part of quota structure */
1871 static void do_get_dqblk(struct dquot *dquot, struct if_dqblk *di)
1873 struct mem_dqblk *dm = &dquot->dq_dqb;
1875 spin_lock(&dq_data_lock);
1876 di->dqb_bhardlimit = dm->dqb_bhardlimit;
1877 di->dqb_bsoftlimit = dm->dqb_bsoftlimit;
1878 di->dqb_curspace = dm->dqb_curspace;
1879 di->dqb_ihardlimit = dm->dqb_ihardlimit;
1880 di->dqb_isoftlimit = dm->dqb_isoftlimit;
1881 di->dqb_curinodes = dm->dqb_curinodes;
1882 di->dqb_btime = dm->dqb_btime;
1883 di->dqb_itime = dm->dqb_itime;
1884 di->dqb_valid = QIF_ALL;
1885 spin_unlock(&dq_data_lock);
1888 int vfs_get_dqblk(struct super_block *sb, int type, qid_t id, struct if_dqblk *di)
1890 struct dquot *dquot;
1892 mutex_lock(&sb_dqopt(sb)->dqonoff_mutex);
1893 if (!(dquot = dqget(sb, id, type))) {
1894 mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
1895 return -ESRCH;
1897 do_get_dqblk(dquot, di);
1898 dqput(dquot);
1899 mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
1900 return 0;
1903 /* Generic routine for setting common part of quota structure */
1904 static int do_set_dqblk(struct dquot *dquot, struct if_dqblk *di)
1906 struct mem_dqblk *dm = &dquot->dq_dqb;
1907 int check_blim = 0, check_ilim = 0;
1908 struct mem_dqinfo *dqi = &sb_dqopt(dquot->dq_sb)->info[dquot->dq_type];
1910 if ((di->dqb_valid & QIF_BLIMITS &&
1911 (di->dqb_bhardlimit > dqi->dqi_maxblimit ||
1912 di->dqb_bsoftlimit > dqi->dqi_maxblimit)) ||
1913 (di->dqb_valid & QIF_ILIMITS &&
1914 (di->dqb_ihardlimit > dqi->dqi_maxilimit ||
1915 di->dqb_isoftlimit > dqi->dqi_maxilimit)))
1916 return -ERANGE;
1918 spin_lock(&dq_data_lock);
1919 if (di->dqb_valid & QIF_SPACE) {
1920 dm->dqb_curspace = di->dqb_curspace;
1921 check_blim = 1;
1923 if (di->dqb_valid & QIF_BLIMITS) {
1924 dm->dqb_bsoftlimit = di->dqb_bsoftlimit;
1925 dm->dqb_bhardlimit = di->dqb_bhardlimit;
1926 check_blim = 1;
1928 if (di->dqb_valid & QIF_INODES) {
1929 dm->dqb_curinodes = di->dqb_curinodes;
1930 check_ilim = 1;
1932 if (di->dqb_valid & QIF_ILIMITS) {
1933 dm->dqb_isoftlimit = di->dqb_isoftlimit;
1934 dm->dqb_ihardlimit = di->dqb_ihardlimit;
1935 check_ilim = 1;
1937 if (di->dqb_valid & QIF_BTIME)
1938 dm->dqb_btime = di->dqb_btime;
1939 if (di->dqb_valid & QIF_ITIME)
1940 dm->dqb_itime = di->dqb_itime;
1942 if (check_blim) {
1943 if (!dm->dqb_bsoftlimit || toqb(dm->dqb_curspace) < dm->dqb_bsoftlimit) {
1944 dm->dqb_btime = 0;
1945 clear_bit(DQ_BLKS_B, &dquot->dq_flags);
1947 else if (!(di->dqb_valid & QIF_BTIME)) /* Set grace only if user hasn't provided his own... */
1948 dm->dqb_btime = get_seconds() + dqi->dqi_bgrace;
1950 if (check_ilim) {
1951 if (!dm->dqb_isoftlimit || dm->dqb_curinodes < dm->dqb_isoftlimit) {
1952 dm->dqb_itime = 0;
1953 clear_bit(DQ_INODES_B, &dquot->dq_flags);
1955 else if (!(di->dqb_valid & QIF_ITIME)) /* Set grace only if user hasn't provided his own... */
1956 dm->dqb_itime = get_seconds() + dqi->dqi_igrace;
1958 if (dm->dqb_bhardlimit || dm->dqb_bsoftlimit || dm->dqb_ihardlimit || dm->dqb_isoftlimit)
1959 clear_bit(DQ_FAKE_B, &dquot->dq_flags);
1960 else
1961 set_bit(DQ_FAKE_B, &dquot->dq_flags);
1962 spin_unlock(&dq_data_lock);
1963 mark_dquot_dirty(dquot);
1965 return 0;
1968 int vfs_set_dqblk(struct super_block *sb, int type, qid_t id, struct if_dqblk *di)
1970 struct dquot *dquot;
1971 int rc;
1973 mutex_lock(&sb_dqopt(sb)->dqonoff_mutex);
1974 if (!(dquot = dqget(sb, id, type))) {
1975 mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
1976 return -ESRCH;
1978 rc = do_set_dqblk(dquot, di);
1979 dqput(dquot);
1980 mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
1981 return rc;
1984 /* Generic routine for getting common part of quota file information */
1985 int vfs_get_dqinfo(struct super_block *sb, int type, struct if_dqinfo *ii)
1987 struct mem_dqinfo *mi;
1989 mutex_lock(&sb_dqopt(sb)->dqonoff_mutex);
1990 if (!sb_has_quota_enabled(sb, type)) {
1991 mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
1992 return -ESRCH;
1994 mi = sb_dqopt(sb)->info + type;
1995 spin_lock(&dq_data_lock);
1996 ii->dqi_bgrace = mi->dqi_bgrace;
1997 ii->dqi_igrace = mi->dqi_igrace;
1998 ii->dqi_flags = mi->dqi_flags & DQF_MASK;
1999 ii->dqi_valid = IIF_ALL;
2000 spin_unlock(&dq_data_lock);
2001 mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
2002 return 0;
2005 /* Generic routine for setting common part of quota file information */
2006 int vfs_set_dqinfo(struct super_block *sb, int type, struct if_dqinfo *ii)
2008 struct mem_dqinfo *mi;
2010 mutex_lock(&sb_dqopt(sb)->dqonoff_mutex);
2011 if (!sb_has_quota_enabled(sb, type)) {
2012 mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
2013 return -ESRCH;
2015 mi = sb_dqopt(sb)->info + type;
2016 spin_lock(&dq_data_lock);
2017 if (ii->dqi_valid & IIF_BGRACE)
2018 mi->dqi_bgrace = ii->dqi_bgrace;
2019 if (ii->dqi_valid & IIF_IGRACE)
2020 mi->dqi_igrace = ii->dqi_igrace;
2021 if (ii->dqi_valid & IIF_FLAGS)
2022 mi->dqi_flags = (mi->dqi_flags & ~DQF_MASK) | (ii->dqi_flags & DQF_MASK);
2023 spin_unlock(&dq_data_lock);
2024 mark_info_dirty(sb, type);
2025 /* Force write to disk */
2026 sb->dq_op->write_info(sb, type);
2027 mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
2028 return 0;
2031 struct quotactl_ops vfs_quotactl_ops = {
2032 .quota_on = vfs_quota_on,
2033 .quota_off = vfs_quota_off,
2034 .quota_sync = vfs_quota_sync,
2035 .get_info = vfs_get_dqinfo,
2036 .set_info = vfs_set_dqinfo,
2037 .get_dqblk = vfs_get_dqblk,
2038 .set_dqblk = vfs_set_dqblk
2041 static ctl_table fs_dqstats_table[] = {
2043 .ctl_name = FS_DQ_LOOKUPS,
2044 .procname = "lookups",
2045 .data = &dqstats.lookups,
2046 .maxlen = sizeof(int),
2047 .mode = 0444,
2048 .proc_handler = &proc_dointvec,
2051 .ctl_name = FS_DQ_DROPS,
2052 .procname = "drops",
2053 .data = &dqstats.drops,
2054 .maxlen = sizeof(int),
2055 .mode = 0444,
2056 .proc_handler = &proc_dointvec,
2059 .ctl_name = FS_DQ_READS,
2060 .procname = "reads",
2061 .data = &dqstats.reads,
2062 .maxlen = sizeof(int),
2063 .mode = 0444,
2064 .proc_handler = &proc_dointvec,
2067 .ctl_name = FS_DQ_WRITES,
2068 .procname = "writes",
2069 .data = &dqstats.writes,
2070 .maxlen = sizeof(int),
2071 .mode = 0444,
2072 .proc_handler = &proc_dointvec,
2075 .ctl_name = FS_DQ_CACHE_HITS,
2076 .procname = "cache_hits",
2077 .data = &dqstats.cache_hits,
2078 .maxlen = sizeof(int),
2079 .mode = 0444,
2080 .proc_handler = &proc_dointvec,
2083 .ctl_name = FS_DQ_ALLOCATED,
2084 .procname = "allocated_dquots",
2085 .data = &dqstats.allocated_dquots,
2086 .maxlen = sizeof(int),
2087 .mode = 0444,
2088 .proc_handler = &proc_dointvec,
2091 .ctl_name = FS_DQ_FREE,
2092 .procname = "free_dquots",
2093 .data = &dqstats.free_dquots,
2094 .maxlen = sizeof(int),
2095 .mode = 0444,
2096 .proc_handler = &proc_dointvec,
2099 .ctl_name = FS_DQ_SYNCS,
2100 .procname = "syncs",
2101 .data = &dqstats.syncs,
2102 .maxlen = sizeof(int),
2103 .mode = 0444,
2104 .proc_handler = &proc_dointvec,
2106 #ifdef CONFIG_PRINT_QUOTA_WARNING
2108 .ctl_name = FS_DQ_WARNINGS,
2109 .procname = "warnings",
2110 .data = &flag_print_warnings,
2111 .maxlen = sizeof(int),
2112 .mode = 0644,
2113 .proc_handler = &proc_dointvec,
2115 #endif
2116 { .ctl_name = 0 },
2119 static ctl_table fs_table[] = {
2121 .ctl_name = FS_DQSTATS,
2122 .procname = "quota",
2123 .mode = 0555,
2124 .child = fs_dqstats_table,
2126 { .ctl_name = 0 },
2129 static ctl_table sys_table[] = {
2131 .ctl_name = CTL_FS,
2132 .procname = "fs",
2133 .mode = 0555,
2134 .child = fs_table,
2136 { .ctl_name = 0 },
2139 static int __init dquot_init(void)
2141 int i;
2142 unsigned long nr_hash, order;
2144 printk(KERN_NOTICE "VFS: Disk quotas %s\n", __DQUOT_VERSION__);
2146 register_sysctl_table(sys_table);
2148 dquot_cachep = kmem_cache_create("dquot",
2149 sizeof(struct dquot), sizeof(unsigned long) * 4,
2150 (SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT|
2151 SLAB_MEM_SPREAD|SLAB_PANIC),
2152 NULL);
2154 order = 0;
2155 dquot_hash = (struct hlist_head *)__get_free_pages(GFP_ATOMIC, order);
2156 if (!dquot_hash)
2157 panic("Cannot create dquot hash table");
2159 /* Find power-of-two hlist_heads which can fit into allocation */
2160 nr_hash = (1UL << order) * PAGE_SIZE / sizeof(struct hlist_head);
2161 dq_hash_bits = 0;
2162 do {
2163 dq_hash_bits++;
2164 } while (nr_hash >> dq_hash_bits);
2165 dq_hash_bits--;
2167 nr_hash = 1UL << dq_hash_bits;
2168 dq_hash_mask = nr_hash - 1;
2169 for (i = 0; i < nr_hash; i++)
2170 INIT_HLIST_HEAD(dquot_hash + i);
2172 printk("Dquot-cache hash table entries: %ld (order %ld, %ld bytes)\n",
2173 nr_hash, order, (PAGE_SIZE << order));
2175 register_shrinker(&dqcache_shrinker);
2177 #ifdef CONFIG_QUOTA_NETLINK_INTERFACE
2178 if (genl_register_family(&quota_genl_family) != 0)
2179 printk(KERN_ERR "VFS: Failed to create quota netlink interface.\n");
2180 #endif
2182 return 0;
2184 module_init(dquot_init);
2186 EXPORT_SYMBOL(register_quota_format);
2187 EXPORT_SYMBOL(unregister_quota_format);
2188 EXPORT_SYMBOL(dqstats);
2189 EXPORT_SYMBOL(dq_data_lock);
2190 EXPORT_SYMBOL(vfs_quota_on);
2191 EXPORT_SYMBOL(vfs_quota_on_path);
2192 EXPORT_SYMBOL(vfs_quota_on_mount);
2193 EXPORT_SYMBOL(vfs_quota_off);
2194 EXPORT_SYMBOL(vfs_quota_sync);
2195 EXPORT_SYMBOL(vfs_get_dqinfo);
2196 EXPORT_SYMBOL(vfs_set_dqinfo);
2197 EXPORT_SYMBOL(vfs_get_dqblk);
2198 EXPORT_SYMBOL(vfs_set_dqblk);
2199 EXPORT_SYMBOL(dquot_commit);
2200 EXPORT_SYMBOL(dquot_commit_info);
2201 EXPORT_SYMBOL(dquot_acquire);
2202 EXPORT_SYMBOL(dquot_release);
2203 EXPORT_SYMBOL(dquot_mark_dquot_dirty);
2204 EXPORT_SYMBOL(dquot_initialize);
2205 EXPORT_SYMBOL(dquot_drop);
2206 EXPORT_SYMBOL(vfs_dq_drop);
2207 EXPORT_SYMBOL(dquot_alloc_space);
2208 EXPORT_SYMBOL(dquot_alloc_inode);
2209 EXPORT_SYMBOL(dquot_free_space);
2210 EXPORT_SYMBOL(dquot_free_inode);
2211 EXPORT_SYMBOL(dquot_transfer);
2212 EXPORT_SYMBOL(vfs_dq_transfer);
2213 EXPORT_SYMBOL(vfs_dq_quota_on_remount);