sata_nv: don't rely on NV_INT_DEV indication with ADMA
[pv_ops_mirror.git] / fs / dquot.c
blob0952cc474d9a57e08b495315200a73d9c93889e7
1 /*
2 * Implementation of the diskquota system for the LINUX operating system. QUOTA
3 * is implemented using the BSD system call interface as the means of
4 * communication with the user level. This file contains the generic routines
5 * called by the different filesystems on allocation of an inode or block.
6 * These routines take care of the administration needed to have a consistent
7 * diskquota tracking system. The ideas of both user and group quotas are based
8 * on the Melbourne quota system as used on BSD derived systems. The internal
9 * implementation is based on one of the several variants of the LINUX
10 * inode-subsystem with added complexity of the diskquota system.
12 * Version: $Id: dquot.c,v 6.3 1996/11/17 18:35:34 mvw Exp mvw $
14 * Author: Marco van Wieringen <mvw@planets.elm.net>
16 * Fixes: Dmitry Gorodchanin <pgmdsg@ibi.com>, 11 Feb 96
18 * Revised list management to avoid races
19 * -- Bill Hawes, <whawes@star.net>, 9/98
21 * Fixed races in dquot_transfer(), dqget() and dquot_alloc_...().
22 * As the consequence the locking was moved from dquot_decr_...(),
23 * dquot_incr_...() to calling functions.
24 * invalidate_dquots() now writes modified dquots.
25 * Serialized quota_off() and quota_on() for mount point.
26 * Fixed a few bugs in grow_dquots().
27 * Fixed deadlock in write_dquot() - we no longer account quotas on
28 * quota files
29 * remove_dquot_ref() moved to inode.c - it now traverses through inodes
30 * add_dquot_ref() restarts after blocking
31 * Added check for bogus uid and fixed check for group in quotactl.
32 * Jan Kara, <jack@suse.cz>, sponsored by SuSE CR, 10-11/99
34 * Used struct list_head instead of own list struct
35 * Invalidation of referenced dquots is no longer possible
36 * Improved free_dquots list management
37 * Quota and i_blocks are now updated in one place to avoid races
38 * Warnings are now delayed so we won't block in critical section
39 * Write updated not to require dquot lock
40 * Jan Kara, <jack@suse.cz>, 9/2000
42 * Added dynamic quota structure allocation
43 * Jan Kara <jack@suse.cz> 12/2000
45 * Rewritten quota interface. Implemented new quota format and
46 * formats registering.
47 * Jan Kara, <jack@suse.cz>, 2001,2002
49 * New SMP locking.
50 * Jan Kara, <jack@suse.cz>, 10/2002
52 * Added journalled quota support, fix lock inversion problems
53 * Jan Kara, <jack@suse.cz>, 2003,2004
55 * (C) Copyright 1994 - 1997 Marco van Wieringen
58 #include <linux/errno.h>
59 #include <linux/kernel.h>
60 #include <linux/fs.h>
61 #include <linux/mount.h>
62 #include <linux/mm.h>
63 #include <linux/time.h>
64 #include <linux/types.h>
65 #include <linux/string.h>
66 #include <linux/fcntl.h>
67 #include <linux/stat.h>
68 #include <linux/tty.h>
69 #include <linux/file.h>
70 #include <linux/slab.h>
71 #include <linux/sysctl.h>
72 #include <linux/smp_lock.h>
73 #include <linux/init.h>
74 #include <linux/module.h>
75 #include <linux/proc_fs.h>
76 #include <linux/security.h>
77 #include <linux/kmod.h>
78 #include <linux/namei.h>
79 #include <linux/buffer_head.h>
80 #include <linux/capability.h>
81 #include <linux/quotaops.h>
83 #include <asm/uaccess.h>
85 #define __DQUOT_PARANOIA
88 * There are two quota SMP locks. dq_list_lock protects all lists with quotas
89 * and quota formats and also dqstats structure containing statistics about the
90 * lists. dq_data_lock protects data from dq_dqb and also mem_dqinfo structures
91 * and also guards consistency of dquot->dq_dqb with inode->i_blocks, i_bytes.
92 * i_blocks and i_bytes updates itself are guarded by i_lock acquired directly
93 * in inode_add_bytes() and inode_sub_bytes().
95 * The spinlock ordering is hence: dq_data_lock > dq_list_lock > i_lock
97 * Note that some things (eg. sb pointer, type, id) doesn't change during
98 * the life of the dquot structure and so needn't to be protected by a lock
100 * Any operation working on dquots via inode pointers must hold dqptr_sem. If
101 * operation is just reading pointers from inode (or not using them at all) the
102 * read lock is enough. If pointers are altered function must hold write lock
103 * (these locking rules also apply for S_NOQUOTA flag in the inode - note that
104 * for altering the flag i_mutex is also needed). If operation is holding
105 * reference to dquot in other way (e.g. quotactl ops) it must be guarded by
106 * dqonoff_mutex.
107 * This locking assures that:
108 * a) update/access to dquot pointers in inode is serialized
109 * b) everyone is guarded against invalidate_dquots()
111 * Each dquot has its dq_lock mutex. Locked dquots might not be referenced
112 * from inodes (dquot_alloc_space() and such don't check the dq_lock).
113 * Currently dquot is locked only when it is being read to memory (or space for
114 * it is being allocated) on the first dqget() and when it is being released on
115 * the last dqput(). The allocation and release oparations are serialized by
116 * the dq_lock and by checking the use count in dquot_release(). Write
117 * operations on dquots don't hold dq_lock as they copy data under dq_data_lock
118 * spinlock to internal buffers before writing.
120 * Lock ordering (including related VFS locks) is the following:
121 * i_mutex > dqonoff_sem > journal_lock > dqptr_sem > dquot->dq_lock >
122 * dqio_mutex
123 * i_mutex on quota files is special (it's below dqio_mutex)
126 static DEFINE_SPINLOCK(dq_list_lock);
127 DEFINE_SPINLOCK(dq_data_lock);
129 static char *quotatypes[] = INITQFNAMES;
130 static struct quota_format_type *quota_formats; /* List of registered formats */
131 static struct quota_module_name module_names[] = INIT_QUOTA_MODULE_NAMES;
133 /* SLAB cache for dquot structures */
134 static struct kmem_cache *dquot_cachep;
136 int register_quota_format(struct quota_format_type *fmt)
138 spin_lock(&dq_list_lock);
139 fmt->qf_next = quota_formats;
140 quota_formats = fmt;
141 spin_unlock(&dq_list_lock);
142 return 0;
145 void unregister_quota_format(struct quota_format_type *fmt)
147 struct quota_format_type **actqf;
149 spin_lock(&dq_list_lock);
150 for (actqf = &quota_formats; *actqf && *actqf != fmt; actqf = &(*actqf)->qf_next);
151 if (*actqf)
152 *actqf = (*actqf)->qf_next;
153 spin_unlock(&dq_list_lock);
156 static struct quota_format_type *find_quota_format(int id)
158 struct quota_format_type *actqf;
160 spin_lock(&dq_list_lock);
161 for (actqf = quota_formats; actqf && actqf->qf_fmt_id != id; actqf = actqf->qf_next);
162 if (!actqf || !try_module_get(actqf->qf_owner)) {
163 int qm;
165 spin_unlock(&dq_list_lock);
167 for (qm = 0; module_names[qm].qm_fmt_id && module_names[qm].qm_fmt_id != id; qm++);
168 if (!module_names[qm].qm_fmt_id || request_module(module_names[qm].qm_mod_name))
169 return NULL;
171 spin_lock(&dq_list_lock);
172 for (actqf = quota_formats; actqf && actqf->qf_fmt_id != id; actqf = actqf->qf_next);
173 if (actqf && !try_module_get(actqf->qf_owner))
174 actqf = NULL;
176 spin_unlock(&dq_list_lock);
177 return actqf;
180 static void put_quota_format(struct quota_format_type *fmt)
182 module_put(fmt->qf_owner);
186 * Dquot List Management:
187 * The quota code uses three lists for dquot management: the inuse_list,
188 * free_dquots, and dquot_hash[] array. A single dquot structure may be
189 * on all three lists, depending on its current state.
191 * All dquots are placed to the end of inuse_list when first created, and this
192 * list is used for invalidate operation, which must look at every dquot.
194 * Unused dquots (dq_count == 0) are added to the free_dquots list when freed,
195 * and this list is searched whenever we need an available dquot. Dquots are
196 * removed from the list as soon as they are used again, and
197 * dqstats.free_dquots gives the number of dquots on the list. When
198 * dquot is invalidated it's completely released from memory.
200 * Dquots with a specific identity (device, type and id) are placed on
201 * one of the dquot_hash[] hash chains. The provides an efficient search
202 * mechanism to locate a specific dquot.
205 static LIST_HEAD(inuse_list);
206 static LIST_HEAD(free_dquots);
207 static unsigned int dq_hash_bits, dq_hash_mask;
208 static struct hlist_head *dquot_hash;
210 struct dqstats dqstats;
212 static void dqput(struct dquot *dquot);
214 static inline unsigned int
215 hashfn(const struct super_block *sb, unsigned int id, int type)
217 unsigned long tmp;
219 tmp = (((unsigned long)sb>>L1_CACHE_SHIFT) ^ id) * (MAXQUOTAS - type);
220 return (tmp + (tmp >> dq_hash_bits)) & dq_hash_mask;
224 * Following list functions expect dq_list_lock to be held
226 static inline void insert_dquot_hash(struct dquot *dquot)
228 struct hlist_head *head = dquot_hash + hashfn(dquot->dq_sb, dquot->dq_id, dquot->dq_type);
229 hlist_add_head(&dquot->dq_hash, head);
232 static inline void remove_dquot_hash(struct dquot *dquot)
234 hlist_del_init(&dquot->dq_hash);
237 static inline struct dquot *find_dquot(unsigned int hashent, struct super_block *sb, unsigned int id, int type)
239 struct hlist_node *node;
240 struct dquot *dquot;
242 hlist_for_each (node, dquot_hash+hashent) {
243 dquot = hlist_entry(node, struct dquot, dq_hash);
244 if (dquot->dq_sb == sb && dquot->dq_id == id && dquot->dq_type == type)
245 return dquot;
247 return NODQUOT;
250 /* Add a dquot to the tail of the free list */
251 static inline void put_dquot_last(struct dquot *dquot)
253 list_add_tail(&dquot->dq_free, &free_dquots);
254 dqstats.free_dquots++;
257 static inline void remove_free_dquot(struct dquot *dquot)
259 if (list_empty(&dquot->dq_free))
260 return;
261 list_del_init(&dquot->dq_free);
262 dqstats.free_dquots--;
265 static inline void put_inuse(struct dquot *dquot)
267 /* We add to the back of inuse list so we don't have to restart
268 * when traversing this list and we block */
269 list_add_tail(&dquot->dq_inuse, &inuse_list);
270 dqstats.allocated_dquots++;
273 static inline void remove_inuse(struct dquot *dquot)
275 dqstats.allocated_dquots--;
276 list_del(&dquot->dq_inuse);
279 * End of list functions needing dq_list_lock
282 static void wait_on_dquot(struct dquot *dquot)
284 mutex_lock(&dquot->dq_lock);
285 mutex_unlock(&dquot->dq_lock);
288 #define mark_dquot_dirty(dquot) ((dquot)->dq_sb->dq_op->mark_dirty(dquot))
290 int dquot_mark_dquot_dirty(struct dquot *dquot)
292 spin_lock(&dq_list_lock);
293 if (!test_and_set_bit(DQ_MOD_B, &dquot->dq_flags))
294 list_add(&dquot->dq_dirty, &sb_dqopt(dquot->dq_sb)->
295 info[dquot->dq_type].dqi_dirty_list);
296 spin_unlock(&dq_list_lock);
297 return 0;
300 /* This function needs dq_list_lock */
301 static inline int clear_dquot_dirty(struct dquot *dquot)
303 if (!test_and_clear_bit(DQ_MOD_B, &dquot->dq_flags))
304 return 0;
305 list_del_init(&dquot->dq_dirty);
306 return 1;
309 void mark_info_dirty(struct super_block *sb, int type)
311 set_bit(DQF_INFO_DIRTY_B, &sb_dqopt(sb)->info[type].dqi_flags);
313 EXPORT_SYMBOL(mark_info_dirty);
316 * Read dquot from disk and alloc space for it
319 int dquot_acquire(struct dquot *dquot)
321 int ret = 0, ret2 = 0;
322 struct quota_info *dqopt = sb_dqopt(dquot->dq_sb);
324 mutex_lock(&dquot->dq_lock);
325 mutex_lock(&dqopt->dqio_mutex);
326 if (!test_bit(DQ_READ_B, &dquot->dq_flags))
327 ret = dqopt->ops[dquot->dq_type]->read_dqblk(dquot);
328 if (ret < 0)
329 goto out_iolock;
330 set_bit(DQ_READ_B, &dquot->dq_flags);
331 /* Instantiate dquot if needed */
332 if (!test_bit(DQ_ACTIVE_B, &dquot->dq_flags) && !dquot->dq_off) {
333 ret = dqopt->ops[dquot->dq_type]->commit_dqblk(dquot);
334 /* Write the info if needed */
335 if (info_dirty(&dqopt->info[dquot->dq_type]))
336 ret2 = dqopt->ops[dquot->dq_type]->write_file_info(dquot->dq_sb, dquot->dq_type);
337 if (ret < 0)
338 goto out_iolock;
339 if (ret2 < 0) {
340 ret = ret2;
341 goto out_iolock;
344 set_bit(DQ_ACTIVE_B, &dquot->dq_flags);
345 out_iolock:
346 mutex_unlock(&dqopt->dqio_mutex);
347 mutex_unlock(&dquot->dq_lock);
348 return ret;
352 * Write dquot to disk
354 int dquot_commit(struct dquot *dquot)
356 int ret = 0, ret2 = 0;
357 struct quota_info *dqopt = sb_dqopt(dquot->dq_sb);
359 mutex_lock(&dqopt->dqio_mutex);
360 spin_lock(&dq_list_lock);
361 if (!clear_dquot_dirty(dquot)) {
362 spin_unlock(&dq_list_lock);
363 goto out_sem;
365 spin_unlock(&dq_list_lock);
366 /* Inactive dquot can be only if there was error during read/init
367 * => we have better not writing it */
368 if (test_bit(DQ_ACTIVE_B, &dquot->dq_flags)) {
369 ret = dqopt->ops[dquot->dq_type]->commit_dqblk(dquot);
370 if (info_dirty(&dqopt->info[dquot->dq_type]))
371 ret2 = dqopt->ops[dquot->dq_type]->write_file_info(dquot->dq_sb, dquot->dq_type);
372 if (ret >= 0)
373 ret = ret2;
375 out_sem:
376 mutex_unlock(&dqopt->dqio_mutex);
377 return ret;
381 * Release dquot
383 int dquot_release(struct dquot *dquot)
385 int ret = 0, ret2 = 0;
386 struct quota_info *dqopt = sb_dqopt(dquot->dq_sb);
388 mutex_lock(&dquot->dq_lock);
389 /* Check whether we are not racing with some other dqget() */
390 if (atomic_read(&dquot->dq_count) > 1)
391 goto out_dqlock;
392 mutex_lock(&dqopt->dqio_mutex);
393 if (dqopt->ops[dquot->dq_type]->release_dqblk) {
394 ret = dqopt->ops[dquot->dq_type]->release_dqblk(dquot);
395 /* Write the info */
396 if (info_dirty(&dqopt->info[dquot->dq_type]))
397 ret2 = dqopt->ops[dquot->dq_type]->write_file_info(dquot->dq_sb, dquot->dq_type);
398 if (ret >= 0)
399 ret = ret2;
401 clear_bit(DQ_ACTIVE_B, &dquot->dq_flags);
402 mutex_unlock(&dqopt->dqio_mutex);
403 out_dqlock:
404 mutex_unlock(&dquot->dq_lock);
405 return ret;
408 /* Invalidate all dquots on the list. Note that this function is called after
409 * quota is disabled and pointers from inodes removed so there cannot be new
410 * quota users. There can still be some users of quotas due to inodes being
411 * just deleted or pruned by prune_icache() (those are not attached to any
412 * list). We have to wait for such users.
414 static void invalidate_dquots(struct super_block *sb, int type)
416 struct dquot *dquot, *tmp;
418 restart:
419 spin_lock(&dq_list_lock);
420 list_for_each_entry_safe(dquot, tmp, &inuse_list, dq_inuse) {
421 if (dquot->dq_sb != sb)
422 continue;
423 if (dquot->dq_type != type)
424 continue;
425 /* Wait for dquot users */
426 if (atomic_read(&dquot->dq_count)) {
427 DEFINE_WAIT(wait);
429 atomic_inc(&dquot->dq_count);
430 prepare_to_wait(&dquot->dq_wait_unused, &wait,
431 TASK_UNINTERRUPTIBLE);
432 spin_unlock(&dq_list_lock);
433 /* Once dqput() wakes us up, we know it's time to free
434 * the dquot.
435 * IMPORTANT: we rely on the fact that there is always
436 * at most one process waiting for dquot to free.
437 * Otherwise dq_count would be > 1 and we would never
438 * wake up.
440 if (atomic_read(&dquot->dq_count) > 1)
441 schedule();
442 finish_wait(&dquot->dq_wait_unused, &wait);
443 dqput(dquot);
444 /* At this moment dquot() need not exist (it could be
445 * reclaimed by prune_dqcache(). Hence we must
446 * restart. */
447 goto restart;
450 * Quota now has no users and it has been written on last
451 * dqput()
453 remove_dquot_hash(dquot);
454 remove_free_dquot(dquot);
455 remove_inuse(dquot);
456 kmem_cache_free(dquot_cachep, dquot);
458 spin_unlock(&dq_list_lock);
461 int vfs_quota_sync(struct super_block *sb, int type)
463 struct list_head *dirty;
464 struct dquot *dquot;
465 struct quota_info *dqopt = sb_dqopt(sb);
466 int cnt;
468 mutex_lock(&dqopt->dqonoff_mutex);
469 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
470 if (type != -1 && cnt != type)
471 continue;
472 if (!sb_has_quota_enabled(sb, cnt))
473 continue;
474 spin_lock(&dq_list_lock);
475 dirty = &dqopt->info[cnt].dqi_dirty_list;
476 while (!list_empty(dirty)) {
477 dquot = list_entry(dirty->next, struct dquot, dq_dirty);
478 /* Dirty and inactive can be only bad dquot... */
479 if (!test_bit(DQ_ACTIVE_B, &dquot->dq_flags)) {
480 clear_dquot_dirty(dquot);
481 continue;
483 /* Now we have active dquot from which someone is
484 * holding reference so we can safely just increase
485 * use count */
486 atomic_inc(&dquot->dq_count);
487 dqstats.lookups++;
488 spin_unlock(&dq_list_lock);
489 sb->dq_op->write_dquot(dquot);
490 dqput(dquot);
491 spin_lock(&dq_list_lock);
493 spin_unlock(&dq_list_lock);
496 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
497 if ((cnt == type || type == -1) && sb_has_quota_enabled(sb, cnt)
498 && info_dirty(&dqopt->info[cnt]))
499 sb->dq_op->write_info(sb, cnt);
500 spin_lock(&dq_list_lock);
501 dqstats.syncs++;
502 spin_unlock(&dq_list_lock);
503 mutex_unlock(&dqopt->dqonoff_mutex);
505 return 0;
508 /* Free unused dquots from cache */
509 static void prune_dqcache(int count)
511 struct list_head *head;
512 struct dquot *dquot;
514 head = free_dquots.prev;
515 while (head != &free_dquots && count) {
516 dquot = list_entry(head, struct dquot, dq_free);
517 remove_dquot_hash(dquot);
518 remove_free_dquot(dquot);
519 remove_inuse(dquot);
520 kmem_cache_free(dquot_cachep, dquot);
521 count--;
522 head = free_dquots.prev;
527 * This is called from kswapd when we think we need some
528 * more memory
531 static int shrink_dqcache_memory(int nr, gfp_t gfp_mask)
533 if (nr) {
534 spin_lock(&dq_list_lock);
535 prune_dqcache(nr);
536 spin_unlock(&dq_list_lock);
538 return (dqstats.free_dquots / 100) * sysctl_vfs_cache_pressure;
542 * Put reference to dquot
543 * NOTE: If you change this function please check whether dqput_blocks() works right...
544 * MUST be called with either dqptr_sem or dqonoff_mutex held
546 static void dqput(struct dquot *dquot)
548 if (!dquot)
549 return;
550 #ifdef __DQUOT_PARANOIA
551 if (!atomic_read(&dquot->dq_count)) {
552 printk("VFS: dqput: trying to free free dquot\n");
553 printk("VFS: device %s, dquot of %s %d\n",
554 dquot->dq_sb->s_id,
555 quotatypes[dquot->dq_type],
556 dquot->dq_id);
557 BUG();
559 #endif
561 spin_lock(&dq_list_lock);
562 dqstats.drops++;
563 spin_unlock(&dq_list_lock);
564 we_slept:
565 spin_lock(&dq_list_lock);
566 if (atomic_read(&dquot->dq_count) > 1) {
567 /* We have more than one user... nothing to do */
568 atomic_dec(&dquot->dq_count);
569 /* Releasing dquot during quotaoff phase? */
570 if (!sb_has_quota_enabled(dquot->dq_sb, dquot->dq_type) &&
571 atomic_read(&dquot->dq_count) == 1)
572 wake_up(&dquot->dq_wait_unused);
573 spin_unlock(&dq_list_lock);
574 return;
576 /* Need to release dquot? */
577 if (test_bit(DQ_ACTIVE_B, &dquot->dq_flags) && dquot_dirty(dquot)) {
578 spin_unlock(&dq_list_lock);
579 /* Commit dquot before releasing */
580 dquot->dq_sb->dq_op->write_dquot(dquot);
581 goto we_slept;
583 /* Clear flag in case dquot was inactive (something bad happened) */
584 clear_dquot_dirty(dquot);
585 if (test_bit(DQ_ACTIVE_B, &dquot->dq_flags)) {
586 spin_unlock(&dq_list_lock);
587 dquot->dq_sb->dq_op->release_dquot(dquot);
588 goto we_slept;
590 atomic_dec(&dquot->dq_count);
591 #ifdef __DQUOT_PARANOIA
592 /* sanity check */
593 BUG_ON(!list_empty(&dquot->dq_free));
594 #endif
595 put_dquot_last(dquot);
596 spin_unlock(&dq_list_lock);
599 static struct dquot *get_empty_dquot(struct super_block *sb, int type)
601 struct dquot *dquot;
603 dquot = kmem_cache_alloc(dquot_cachep, GFP_NOFS);
604 if(!dquot)
605 return NODQUOT;
607 memset((caddr_t)dquot, 0, sizeof(struct dquot));
608 mutex_init(&dquot->dq_lock);
609 INIT_LIST_HEAD(&dquot->dq_free);
610 INIT_LIST_HEAD(&dquot->dq_inuse);
611 INIT_HLIST_NODE(&dquot->dq_hash);
612 INIT_LIST_HEAD(&dquot->dq_dirty);
613 init_waitqueue_head(&dquot->dq_wait_unused);
614 dquot->dq_sb = sb;
615 dquot->dq_type = type;
616 atomic_set(&dquot->dq_count, 1);
618 return dquot;
622 * Get reference to dquot
623 * MUST be called with either dqptr_sem or dqonoff_mutex held
625 static struct dquot *dqget(struct super_block *sb, unsigned int id, int type)
627 unsigned int hashent = hashfn(sb, id, type);
628 struct dquot *dquot, *empty = NODQUOT;
630 if (!sb_has_quota_enabled(sb, type))
631 return NODQUOT;
632 we_slept:
633 spin_lock(&dq_list_lock);
634 if ((dquot = find_dquot(hashent, sb, id, type)) == NODQUOT) {
635 if (empty == NODQUOT) {
636 spin_unlock(&dq_list_lock);
637 if ((empty = get_empty_dquot(sb, type)) == NODQUOT)
638 schedule(); /* Try to wait for a moment... */
639 goto we_slept;
641 dquot = empty;
642 dquot->dq_id = id;
643 /* all dquots go on the inuse_list */
644 put_inuse(dquot);
645 /* hash it first so it can be found */
646 insert_dquot_hash(dquot);
647 dqstats.lookups++;
648 spin_unlock(&dq_list_lock);
649 } else {
650 if (!atomic_read(&dquot->dq_count))
651 remove_free_dquot(dquot);
652 atomic_inc(&dquot->dq_count);
653 dqstats.cache_hits++;
654 dqstats.lookups++;
655 spin_unlock(&dq_list_lock);
656 if (empty)
657 kmem_cache_free(dquot_cachep, empty);
659 /* Wait for dq_lock - after this we know that either dquot_release() is already
660 * finished or it will be canceled due to dq_count > 1 test */
661 wait_on_dquot(dquot);
662 /* Read the dquot and instantiate it (everything done only if needed) */
663 if (!test_bit(DQ_ACTIVE_B, &dquot->dq_flags) && sb->dq_op->acquire_dquot(dquot) < 0) {
664 dqput(dquot);
665 return NODQUOT;
667 #ifdef __DQUOT_PARANOIA
668 BUG_ON(!dquot->dq_sb); /* Has somebody invalidated entry under us? */
669 #endif
671 return dquot;
674 static int dqinit_needed(struct inode *inode, int type)
676 int cnt;
678 if (IS_NOQUOTA(inode))
679 return 0;
680 if (type != -1)
681 return inode->i_dquot[type] == NODQUOT;
682 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
683 if (inode->i_dquot[cnt] == NODQUOT)
684 return 1;
685 return 0;
688 /* This routine is guarded by dqonoff_mutex mutex */
689 static void add_dquot_ref(struct super_block *sb, int type)
691 struct list_head *p;
693 restart:
694 file_list_lock();
695 list_for_each(p, &sb->s_files) {
696 struct file *filp = list_entry(p, struct file, f_u.fu_list);
697 struct inode *inode = filp->f_path.dentry->d_inode;
698 if (filp->f_mode & FMODE_WRITE && dqinit_needed(inode, type)) {
699 struct dentry *dentry = dget(filp->f_path.dentry);
700 file_list_unlock();
701 sb->dq_op->initialize(inode, type);
702 dput(dentry);
703 /* As we may have blocked we had better restart... */
704 goto restart;
707 file_list_unlock();
710 /* Return 0 if dqput() won't block (note that 1 doesn't necessarily mean blocking) */
711 static inline int dqput_blocks(struct dquot *dquot)
713 if (atomic_read(&dquot->dq_count) <= 1)
714 return 1;
715 return 0;
718 /* Remove references to dquots from inode - add dquot to list for freeing if needed */
719 /* We can't race with anybody because we hold dqptr_sem for writing... */
720 int remove_inode_dquot_ref(struct inode *inode, int type, struct list_head *tofree_head)
722 struct dquot *dquot = inode->i_dquot[type];
724 inode->i_dquot[type] = NODQUOT;
725 if (dquot != NODQUOT) {
726 if (dqput_blocks(dquot)) {
727 #ifdef __DQUOT_PARANOIA
728 if (atomic_read(&dquot->dq_count) != 1)
729 printk(KERN_WARNING "VFS: Adding dquot with dq_count %d to dispose list.\n", atomic_read(&dquot->dq_count));
730 #endif
731 spin_lock(&dq_list_lock);
732 list_add(&dquot->dq_free, tofree_head); /* As dquot must have currently users it can't be on the free list... */
733 spin_unlock(&dq_list_lock);
734 return 1;
736 else
737 dqput(dquot); /* We have guaranteed we won't block */
739 return 0;
742 /* Free list of dquots - called from inode.c */
743 /* dquots are removed from inodes, no new references can be got so we are the only ones holding reference */
744 static void put_dquot_list(struct list_head *tofree_head)
746 struct list_head *act_head;
747 struct dquot *dquot;
749 act_head = tofree_head->next;
750 /* So now we have dquots on the list... Just free them */
751 while (act_head != tofree_head) {
752 dquot = list_entry(act_head, struct dquot, dq_free);
753 act_head = act_head->next;
754 list_del_init(&dquot->dq_free); /* Remove dquot from the list so we won't have problems... */
755 dqput(dquot);
759 /* Gather all references from inodes and drop them */
760 static void drop_dquot_ref(struct super_block *sb, int type)
762 LIST_HEAD(tofree_head);
764 down_write(&sb_dqopt(sb)->dqptr_sem);
765 remove_dquot_ref(sb, type, &tofree_head);
766 up_write(&sb_dqopt(sb)->dqptr_sem);
767 put_dquot_list(&tofree_head);
770 static inline void dquot_incr_inodes(struct dquot *dquot, unsigned long number)
772 dquot->dq_dqb.dqb_curinodes += number;
775 static inline void dquot_incr_space(struct dquot *dquot, qsize_t number)
777 dquot->dq_dqb.dqb_curspace += number;
780 static inline void dquot_decr_inodes(struct dquot *dquot, unsigned long number)
782 if (dquot->dq_dqb.dqb_curinodes > number)
783 dquot->dq_dqb.dqb_curinodes -= number;
784 else
785 dquot->dq_dqb.dqb_curinodes = 0;
786 if (dquot->dq_dqb.dqb_curinodes <= dquot->dq_dqb.dqb_isoftlimit)
787 dquot->dq_dqb.dqb_itime = (time_t) 0;
788 clear_bit(DQ_INODES_B, &dquot->dq_flags);
791 static inline void dquot_decr_space(struct dquot *dquot, qsize_t number)
793 if (dquot->dq_dqb.dqb_curspace > number)
794 dquot->dq_dqb.dqb_curspace -= number;
795 else
796 dquot->dq_dqb.dqb_curspace = 0;
797 if (toqb(dquot->dq_dqb.dqb_curspace) <= dquot->dq_dqb.dqb_bsoftlimit)
798 dquot->dq_dqb.dqb_btime = (time_t) 0;
799 clear_bit(DQ_BLKS_B, &dquot->dq_flags);
802 static int flag_print_warnings = 1;
804 static inline int need_print_warning(struct dquot *dquot)
806 if (!flag_print_warnings)
807 return 0;
809 switch (dquot->dq_type) {
810 case USRQUOTA:
811 return current->fsuid == dquot->dq_id;
812 case GRPQUOTA:
813 return in_group_p(dquot->dq_id);
815 return 0;
818 /* Values of warnings */
819 #define NOWARN 0
820 #define IHARDWARN 1
821 #define ISOFTLONGWARN 2
822 #define ISOFTWARN 3
823 #define BHARDWARN 4
824 #define BSOFTLONGWARN 5
825 #define BSOFTWARN 6
827 /* Print warning to user which exceeded quota */
828 static void print_warning(struct dquot *dquot, const char warntype)
830 char *msg = NULL;
831 struct tty_struct *tty;
832 int flag = (warntype == BHARDWARN || warntype == BSOFTLONGWARN) ? DQ_BLKS_B :
833 ((warntype == IHARDWARN || warntype == ISOFTLONGWARN) ? DQ_INODES_B : 0);
835 if (!need_print_warning(dquot) || (flag && test_and_set_bit(flag, &dquot->dq_flags)))
836 return;
838 mutex_lock(&tty_mutex);
839 tty = get_current_tty();
840 if (!tty)
841 goto out_lock;
842 tty_write_message(tty, dquot->dq_sb->s_id);
843 if (warntype == ISOFTWARN || warntype == BSOFTWARN)
844 tty_write_message(tty, ": warning, ");
845 else
846 tty_write_message(tty, ": write failed, ");
847 tty_write_message(tty, quotatypes[dquot->dq_type]);
848 switch (warntype) {
849 case IHARDWARN:
850 msg = " file limit reached.\r\n";
851 break;
852 case ISOFTLONGWARN:
853 msg = " file quota exceeded too long.\r\n";
854 break;
855 case ISOFTWARN:
856 msg = " file quota exceeded.\r\n";
857 break;
858 case BHARDWARN:
859 msg = " block limit reached.\r\n";
860 break;
861 case BSOFTLONGWARN:
862 msg = " block quota exceeded too long.\r\n";
863 break;
864 case BSOFTWARN:
865 msg = " block quota exceeded.\r\n";
866 break;
868 tty_write_message(tty, msg);
869 out_lock:
870 mutex_unlock(&tty_mutex);
873 static inline void flush_warnings(struct dquot **dquots, char *warntype)
875 int i;
877 for (i = 0; i < MAXQUOTAS; i++)
878 if (dquots[i] != NODQUOT && warntype[i] != NOWARN)
879 print_warning(dquots[i], warntype[i]);
882 static inline char ignore_hardlimit(struct dquot *dquot)
884 struct mem_dqinfo *info = &sb_dqopt(dquot->dq_sb)->info[dquot->dq_type];
886 return capable(CAP_SYS_RESOURCE) &&
887 (info->dqi_format->qf_fmt_id != QFMT_VFS_OLD || !(info->dqi_flags & V1_DQF_RSQUASH));
890 /* needs dq_data_lock */
891 static int check_idq(struct dquot *dquot, ulong inodes, char *warntype)
893 *warntype = NOWARN;
894 if (inodes <= 0 || test_bit(DQ_FAKE_B, &dquot->dq_flags))
895 return QUOTA_OK;
897 if (dquot->dq_dqb.dqb_ihardlimit &&
898 (dquot->dq_dqb.dqb_curinodes + inodes) > dquot->dq_dqb.dqb_ihardlimit &&
899 !ignore_hardlimit(dquot)) {
900 *warntype = IHARDWARN;
901 return NO_QUOTA;
904 if (dquot->dq_dqb.dqb_isoftlimit &&
905 (dquot->dq_dqb.dqb_curinodes + inodes) > dquot->dq_dqb.dqb_isoftlimit &&
906 dquot->dq_dqb.dqb_itime && get_seconds() >= dquot->dq_dqb.dqb_itime &&
907 !ignore_hardlimit(dquot)) {
908 *warntype = ISOFTLONGWARN;
909 return NO_QUOTA;
912 if (dquot->dq_dqb.dqb_isoftlimit &&
913 (dquot->dq_dqb.dqb_curinodes + inodes) > dquot->dq_dqb.dqb_isoftlimit &&
914 dquot->dq_dqb.dqb_itime == 0) {
915 *warntype = ISOFTWARN;
916 dquot->dq_dqb.dqb_itime = get_seconds() + sb_dqopt(dquot->dq_sb)->info[dquot->dq_type].dqi_igrace;
919 return QUOTA_OK;
922 /* needs dq_data_lock */
923 static int check_bdq(struct dquot *dquot, qsize_t space, int prealloc, char *warntype)
925 *warntype = 0;
926 if (space <= 0 || test_bit(DQ_FAKE_B, &dquot->dq_flags))
927 return QUOTA_OK;
929 if (dquot->dq_dqb.dqb_bhardlimit &&
930 toqb(dquot->dq_dqb.dqb_curspace + space) > dquot->dq_dqb.dqb_bhardlimit &&
931 !ignore_hardlimit(dquot)) {
932 if (!prealloc)
933 *warntype = BHARDWARN;
934 return NO_QUOTA;
937 if (dquot->dq_dqb.dqb_bsoftlimit &&
938 toqb(dquot->dq_dqb.dqb_curspace + space) > dquot->dq_dqb.dqb_bsoftlimit &&
939 dquot->dq_dqb.dqb_btime && get_seconds() >= dquot->dq_dqb.dqb_btime &&
940 !ignore_hardlimit(dquot)) {
941 if (!prealloc)
942 *warntype = BSOFTLONGWARN;
943 return NO_QUOTA;
946 if (dquot->dq_dqb.dqb_bsoftlimit &&
947 toqb(dquot->dq_dqb.dqb_curspace + space) > dquot->dq_dqb.dqb_bsoftlimit &&
948 dquot->dq_dqb.dqb_btime == 0) {
949 if (!prealloc) {
950 *warntype = BSOFTWARN;
951 dquot->dq_dqb.dqb_btime = get_seconds() + sb_dqopt(dquot->dq_sb)->info[dquot->dq_type].dqi_bgrace;
953 else
955 * We don't allow preallocation to exceed softlimit so exceeding will
956 * be always printed
958 return NO_QUOTA;
961 return QUOTA_OK;
965 * Initialize quota pointers in inode
966 * Transaction must be started at entry
968 int dquot_initialize(struct inode *inode, int type)
970 unsigned int id = 0;
971 int cnt, ret = 0;
973 /* First test before acquiring mutex - solves deadlocks when we
974 * re-enter the quota code and are already holding the mutex */
975 if (IS_NOQUOTA(inode))
976 return 0;
977 down_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
978 /* Having dqptr_sem we know NOQUOTA flags can't be altered... */
979 if (IS_NOQUOTA(inode))
980 goto out_err;
981 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
982 if (type != -1 && cnt != type)
983 continue;
984 if (inode->i_dquot[cnt] == NODQUOT) {
985 switch (cnt) {
986 case USRQUOTA:
987 id = inode->i_uid;
988 break;
989 case GRPQUOTA:
990 id = inode->i_gid;
991 break;
993 inode->i_dquot[cnt] = dqget(inode->i_sb, id, cnt);
996 out_err:
997 up_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
998 return ret;
1002 * Release all quotas referenced by inode
1003 * Transaction must be started at an entry
1005 int dquot_drop(struct inode *inode)
1007 int cnt;
1009 down_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
1010 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1011 if (inode->i_dquot[cnt] != NODQUOT) {
1012 dqput(inode->i_dquot[cnt]);
1013 inode->i_dquot[cnt] = NODQUOT;
1016 up_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
1017 return 0;
1021 * Following four functions update i_blocks+i_bytes fields and
1022 * quota information (together with appropriate checks)
1023 * NOTE: We absolutely rely on the fact that caller dirties
1024 * the inode (usually macros in quotaops.h care about this) and
1025 * holds a handle for the current transaction so that dquot write and
1026 * inode write go into the same transaction.
1030 * This operation can block, but only after everything is updated
1032 int dquot_alloc_space(struct inode *inode, qsize_t number, int warn)
1034 int cnt, ret = NO_QUOTA;
1035 char warntype[MAXQUOTAS];
1037 /* First test before acquiring mutex - solves deadlocks when we
1038 * re-enter the quota code and are already holding the mutex */
1039 if (IS_NOQUOTA(inode)) {
1040 out_add:
1041 inode_add_bytes(inode, number);
1042 return QUOTA_OK;
1044 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1045 warntype[cnt] = NOWARN;
1047 down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1048 if (IS_NOQUOTA(inode)) { /* Now we can do reliable test... */
1049 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1050 goto out_add;
1052 spin_lock(&dq_data_lock);
1053 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1054 if (inode->i_dquot[cnt] == NODQUOT)
1055 continue;
1056 if (check_bdq(inode->i_dquot[cnt], number, warn, warntype+cnt) == NO_QUOTA)
1057 goto warn_put_all;
1059 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1060 if (inode->i_dquot[cnt] == NODQUOT)
1061 continue;
1062 dquot_incr_space(inode->i_dquot[cnt], number);
1064 inode_add_bytes(inode, number);
1065 ret = QUOTA_OK;
1066 warn_put_all:
1067 spin_unlock(&dq_data_lock);
1068 if (ret == QUOTA_OK)
1069 /* Dirtify all the dquots - this can block when journalling */
1070 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1071 if (inode->i_dquot[cnt])
1072 mark_dquot_dirty(inode->i_dquot[cnt]);
1073 flush_warnings(inode->i_dquot, warntype);
1074 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1075 return ret;
1079 * This operation can block, but only after everything is updated
1081 int dquot_alloc_inode(const struct inode *inode, unsigned long number)
1083 int cnt, ret = NO_QUOTA;
1084 char warntype[MAXQUOTAS];
1086 /* First test before acquiring mutex - solves deadlocks when we
1087 * re-enter the quota code and are already holding the mutex */
1088 if (IS_NOQUOTA(inode))
1089 return QUOTA_OK;
1090 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1091 warntype[cnt] = NOWARN;
1092 down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1093 if (IS_NOQUOTA(inode)) {
1094 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1095 return QUOTA_OK;
1097 spin_lock(&dq_data_lock);
1098 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1099 if (inode->i_dquot[cnt] == NODQUOT)
1100 continue;
1101 if (check_idq(inode->i_dquot[cnt], number, warntype+cnt) == NO_QUOTA)
1102 goto warn_put_all;
1105 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1106 if (inode->i_dquot[cnt] == NODQUOT)
1107 continue;
1108 dquot_incr_inodes(inode->i_dquot[cnt], number);
1110 ret = QUOTA_OK;
1111 warn_put_all:
1112 spin_unlock(&dq_data_lock);
1113 if (ret == QUOTA_OK)
1114 /* Dirtify all the dquots - this can block when journalling */
1115 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1116 if (inode->i_dquot[cnt])
1117 mark_dquot_dirty(inode->i_dquot[cnt]);
1118 flush_warnings((struct dquot **)inode->i_dquot, warntype);
1119 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1120 return ret;
1124 * This operation can block, but only after everything is updated
1126 int dquot_free_space(struct inode *inode, qsize_t number)
1128 unsigned int cnt;
1130 /* First test before acquiring mutex - solves deadlocks when we
1131 * re-enter the quota code and are already holding the mutex */
1132 if (IS_NOQUOTA(inode)) {
1133 out_sub:
1134 inode_sub_bytes(inode, number);
1135 return QUOTA_OK;
1137 down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1138 /* Now recheck reliably when holding dqptr_sem */
1139 if (IS_NOQUOTA(inode)) {
1140 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1141 goto out_sub;
1143 spin_lock(&dq_data_lock);
1144 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1145 if (inode->i_dquot[cnt] == NODQUOT)
1146 continue;
1147 dquot_decr_space(inode->i_dquot[cnt], number);
1149 inode_sub_bytes(inode, number);
1150 spin_unlock(&dq_data_lock);
1151 /* Dirtify all the dquots - this can block when journalling */
1152 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1153 if (inode->i_dquot[cnt])
1154 mark_dquot_dirty(inode->i_dquot[cnt]);
1155 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1156 return QUOTA_OK;
1160 * This operation can block, but only after everything is updated
1162 int dquot_free_inode(const struct inode *inode, unsigned long number)
1164 unsigned int cnt;
1166 /* First test before acquiring mutex - solves deadlocks when we
1167 * re-enter the quota code and are already holding the mutex */
1168 if (IS_NOQUOTA(inode))
1169 return QUOTA_OK;
1170 down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1171 /* Now recheck reliably when holding dqptr_sem */
1172 if (IS_NOQUOTA(inode)) {
1173 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1174 return QUOTA_OK;
1176 spin_lock(&dq_data_lock);
1177 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1178 if (inode->i_dquot[cnt] == NODQUOT)
1179 continue;
1180 dquot_decr_inodes(inode->i_dquot[cnt], number);
1182 spin_unlock(&dq_data_lock);
1183 /* Dirtify all the dquots - this can block when journalling */
1184 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1185 if (inode->i_dquot[cnt])
1186 mark_dquot_dirty(inode->i_dquot[cnt]);
1187 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1188 return QUOTA_OK;
1192 * Transfer the number of inode and blocks from one diskquota to an other.
1194 * This operation can block, but only after everything is updated
1195 * A transaction must be started when entering this function.
1197 int dquot_transfer(struct inode *inode, struct iattr *iattr)
1199 qsize_t space;
1200 struct dquot *transfer_from[MAXQUOTAS];
1201 struct dquot *transfer_to[MAXQUOTAS];
1202 int cnt, ret = NO_QUOTA, chuid = (iattr->ia_valid & ATTR_UID) && inode->i_uid != iattr->ia_uid,
1203 chgid = (iattr->ia_valid & ATTR_GID) && inode->i_gid != iattr->ia_gid;
1204 char warntype[MAXQUOTAS];
1206 /* First test before acquiring mutex - solves deadlocks when we
1207 * re-enter the quota code and are already holding the mutex */
1208 if (IS_NOQUOTA(inode))
1209 return QUOTA_OK;
1210 /* Clear the arrays */
1211 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1212 transfer_to[cnt] = transfer_from[cnt] = NODQUOT;
1213 warntype[cnt] = NOWARN;
1215 down_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
1216 /* Now recheck reliably when holding dqptr_sem */
1217 if (IS_NOQUOTA(inode)) { /* File without quota accounting? */
1218 up_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
1219 return QUOTA_OK;
1221 /* First build the transfer_to list - here we can block on
1222 * reading/instantiating of dquots. We know that the transaction for
1223 * us was already started so we don't violate lock ranking here */
1224 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1225 switch (cnt) {
1226 case USRQUOTA:
1227 if (!chuid)
1228 continue;
1229 transfer_to[cnt] = dqget(inode->i_sb, iattr->ia_uid, cnt);
1230 break;
1231 case GRPQUOTA:
1232 if (!chgid)
1233 continue;
1234 transfer_to[cnt] = dqget(inode->i_sb, iattr->ia_gid, cnt);
1235 break;
1238 spin_lock(&dq_data_lock);
1239 space = inode_get_bytes(inode);
1240 /* Build the transfer_from list and check the limits */
1241 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1242 if (transfer_to[cnt] == NODQUOT)
1243 continue;
1244 transfer_from[cnt] = inode->i_dquot[cnt];
1245 if (check_idq(transfer_to[cnt], 1, warntype+cnt) == NO_QUOTA ||
1246 check_bdq(transfer_to[cnt], space, 0, warntype+cnt) == NO_QUOTA)
1247 goto warn_put_all;
1251 * Finally perform the needed transfer from transfer_from to transfer_to
1253 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1255 * Skip changes for same uid or gid or for turned off quota-type.
1257 if (transfer_to[cnt] == NODQUOT)
1258 continue;
1260 /* Due to IO error we might not have transfer_from[] structure */
1261 if (transfer_from[cnt]) {
1262 dquot_decr_inodes(transfer_from[cnt], 1);
1263 dquot_decr_space(transfer_from[cnt], space);
1266 dquot_incr_inodes(transfer_to[cnt], 1);
1267 dquot_incr_space(transfer_to[cnt], space);
1269 inode->i_dquot[cnt] = transfer_to[cnt];
1271 ret = QUOTA_OK;
1272 warn_put_all:
1273 spin_unlock(&dq_data_lock);
1274 /* Dirtify all the dquots - this can block when journalling */
1275 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1276 if (transfer_from[cnt])
1277 mark_dquot_dirty(transfer_from[cnt]);
1278 if (transfer_to[cnt])
1279 mark_dquot_dirty(transfer_to[cnt]);
1281 flush_warnings(transfer_to, warntype);
1283 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1284 if (ret == QUOTA_OK && transfer_from[cnt] != NODQUOT)
1285 dqput(transfer_from[cnt]);
1286 if (ret == NO_QUOTA && transfer_to[cnt] != NODQUOT)
1287 dqput(transfer_to[cnt]);
1289 up_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
1290 return ret;
1294 * Write info of quota file to disk
1296 int dquot_commit_info(struct super_block *sb, int type)
1298 int ret;
1299 struct quota_info *dqopt = sb_dqopt(sb);
1301 mutex_lock(&dqopt->dqio_mutex);
1302 ret = dqopt->ops[type]->write_file_info(sb, type);
1303 mutex_unlock(&dqopt->dqio_mutex);
1304 return ret;
1308 * Definitions of diskquota operations.
1310 struct dquot_operations dquot_operations = {
1311 .initialize = dquot_initialize,
1312 .drop = dquot_drop,
1313 .alloc_space = dquot_alloc_space,
1314 .alloc_inode = dquot_alloc_inode,
1315 .free_space = dquot_free_space,
1316 .free_inode = dquot_free_inode,
1317 .transfer = dquot_transfer,
1318 .write_dquot = dquot_commit,
1319 .acquire_dquot = dquot_acquire,
1320 .release_dquot = dquot_release,
1321 .mark_dirty = dquot_mark_dquot_dirty,
1322 .write_info = dquot_commit_info
1325 static inline void set_enable_flags(struct quota_info *dqopt, int type)
1327 switch (type) {
1328 case USRQUOTA:
1329 dqopt->flags |= DQUOT_USR_ENABLED;
1330 break;
1331 case GRPQUOTA:
1332 dqopt->flags |= DQUOT_GRP_ENABLED;
1333 break;
1337 static inline void reset_enable_flags(struct quota_info *dqopt, int type)
1339 switch (type) {
1340 case USRQUOTA:
1341 dqopt->flags &= ~DQUOT_USR_ENABLED;
1342 break;
1343 case GRPQUOTA:
1344 dqopt->flags &= ~DQUOT_GRP_ENABLED;
1345 break;
1350 * Turn quota off on a device. type == -1 ==> quotaoff for all types (umount)
1352 int vfs_quota_off(struct super_block *sb, int type)
1354 int cnt;
1355 struct quota_info *dqopt = sb_dqopt(sb);
1356 struct inode *toputinode[MAXQUOTAS];
1358 /* We need to serialize quota_off() for device */
1359 mutex_lock(&dqopt->dqonoff_mutex);
1360 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1361 toputinode[cnt] = NULL;
1362 if (type != -1 && cnt != type)
1363 continue;
1364 if (!sb_has_quota_enabled(sb, cnt))
1365 continue;
1366 reset_enable_flags(dqopt, cnt);
1368 /* Note: these are blocking operations */
1369 drop_dquot_ref(sb, cnt);
1370 invalidate_dquots(sb, cnt);
1372 * Now all dquots should be invalidated, all writes done so we should be only
1373 * users of the info. No locks needed.
1375 if (info_dirty(&dqopt->info[cnt]))
1376 sb->dq_op->write_info(sb, cnt);
1377 if (dqopt->ops[cnt]->free_file_info)
1378 dqopt->ops[cnt]->free_file_info(sb, cnt);
1379 put_quota_format(dqopt->info[cnt].dqi_format);
1381 toputinode[cnt] = dqopt->files[cnt];
1382 dqopt->files[cnt] = NULL;
1383 dqopt->info[cnt].dqi_flags = 0;
1384 dqopt->info[cnt].dqi_igrace = 0;
1385 dqopt->info[cnt].dqi_bgrace = 0;
1386 dqopt->ops[cnt] = NULL;
1388 mutex_unlock(&dqopt->dqonoff_mutex);
1389 /* Sync the superblock so that buffers with quota data are written to
1390 * disk (and so userspace sees correct data afterwards). */
1391 if (sb->s_op->sync_fs)
1392 sb->s_op->sync_fs(sb, 1);
1393 sync_blockdev(sb->s_bdev);
1394 /* Now the quota files are just ordinary files and we can set the
1395 * inode flags back. Moreover we discard the pagecache so that
1396 * userspace sees the writes we did bypassing the pagecache. We
1397 * must also discard the blockdev buffers so that we see the
1398 * changes done by userspace on the next quotaon() */
1399 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1400 if (toputinode[cnt]) {
1401 mutex_lock(&dqopt->dqonoff_mutex);
1402 /* If quota was reenabled in the meantime, we have
1403 * nothing to do */
1404 if (!sb_has_quota_enabled(sb, cnt)) {
1405 mutex_lock(&toputinode[cnt]->i_mutex);
1406 toputinode[cnt]->i_flags &= ~(S_IMMUTABLE |
1407 S_NOATIME | S_NOQUOTA);
1408 truncate_inode_pages(&toputinode[cnt]->i_data, 0);
1409 mutex_unlock(&toputinode[cnt]->i_mutex);
1410 mark_inode_dirty(toputinode[cnt]);
1411 iput(toputinode[cnt]);
1413 mutex_unlock(&dqopt->dqonoff_mutex);
1415 if (sb->s_bdev)
1416 invalidate_bdev(sb->s_bdev, 0);
1417 return 0;
1421 * Turn quotas on on a device
1424 /* Helper function when we already have the inode */
1425 static int vfs_quota_on_inode(struct inode *inode, int type, int format_id)
1427 struct quota_format_type *fmt = find_quota_format(format_id);
1428 struct super_block *sb = inode->i_sb;
1429 struct quota_info *dqopt = sb_dqopt(sb);
1430 int error;
1431 int oldflags = -1;
1433 if (!fmt)
1434 return -ESRCH;
1435 if (!S_ISREG(inode->i_mode)) {
1436 error = -EACCES;
1437 goto out_fmt;
1439 if (IS_RDONLY(inode)) {
1440 error = -EROFS;
1441 goto out_fmt;
1443 if (!sb->s_op->quota_write || !sb->s_op->quota_read) {
1444 error = -EINVAL;
1445 goto out_fmt;
1448 /* As we bypass the pagecache we must now flush the inode so that
1449 * we see all the changes from userspace... */
1450 write_inode_now(inode, 1);
1451 /* And now flush the block cache so that kernel sees the changes */
1452 invalidate_bdev(sb->s_bdev, 0);
1453 mutex_lock(&inode->i_mutex);
1454 mutex_lock(&dqopt->dqonoff_mutex);
1455 if (sb_has_quota_enabled(sb, type)) {
1456 error = -EBUSY;
1457 goto out_lock;
1459 /* We don't want quota and atime on quota files (deadlocks possible)
1460 * Also nobody should write to the file - we use special IO operations
1461 * which ignore the immutable bit. */
1462 down_write(&dqopt->dqptr_sem);
1463 oldflags = inode->i_flags & (S_NOATIME | S_IMMUTABLE | S_NOQUOTA);
1464 inode->i_flags |= S_NOQUOTA | S_NOATIME | S_IMMUTABLE;
1465 up_write(&dqopt->dqptr_sem);
1466 sb->dq_op->drop(inode);
1468 error = -EIO;
1469 dqopt->files[type] = igrab(inode);
1470 if (!dqopt->files[type])
1471 goto out_lock;
1472 error = -EINVAL;
1473 if (!fmt->qf_ops->check_quota_file(sb, type))
1474 goto out_file_init;
1476 dqopt->ops[type] = fmt->qf_ops;
1477 dqopt->info[type].dqi_format = fmt;
1478 INIT_LIST_HEAD(&dqopt->info[type].dqi_dirty_list);
1479 mutex_lock(&dqopt->dqio_mutex);
1480 if ((error = dqopt->ops[type]->read_file_info(sb, type)) < 0) {
1481 mutex_unlock(&dqopt->dqio_mutex);
1482 goto out_file_init;
1484 mutex_unlock(&dqopt->dqio_mutex);
1485 mutex_unlock(&inode->i_mutex);
1486 set_enable_flags(dqopt, type);
1488 add_dquot_ref(sb, type);
1489 mutex_unlock(&dqopt->dqonoff_mutex);
1491 return 0;
1493 out_file_init:
1494 dqopt->files[type] = NULL;
1495 iput(inode);
1496 out_lock:
1497 mutex_unlock(&dqopt->dqonoff_mutex);
1498 if (oldflags != -1) {
1499 down_write(&dqopt->dqptr_sem);
1500 /* Set the flags back (in the case of accidental quotaon()
1501 * on a wrong file we don't want to mess up the flags) */
1502 inode->i_flags &= ~(S_NOATIME | S_NOQUOTA | S_IMMUTABLE);
1503 inode->i_flags |= oldflags;
1504 up_write(&dqopt->dqptr_sem);
1506 mutex_unlock(&inode->i_mutex);
1507 out_fmt:
1508 put_quota_format(fmt);
1510 return error;
1513 /* Actual function called from quotactl() */
1514 int vfs_quota_on(struct super_block *sb, int type, int format_id, char *path)
1516 struct nameidata nd;
1517 int error;
1519 error = path_lookup(path, LOOKUP_FOLLOW, &nd);
1520 if (error < 0)
1521 return error;
1522 error = security_quota_on(nd.dentry);
1523 if (error)
1524 goto out_path;
1525 /* Quota file not on the same filesystem? */
1526 if (nd.mnt->mnt_sb != sb)
1527 error = -EXDEV;
1528 else
1529 error = vfs_quota_on_inode(nd.dentry->d_inode, type, format_id);
1530 out_path:
1531 path_release(&nd);
1532 return error;
1536 * This function is used when filesystem needs to initialize quotas
1537 * during mount time.
1539 int vfs_quota_on_mount(struct super_block *sb, char *qf_name,
1540 int format_id, int type)
1542 struct dentry *dentry;
1543 int error;
1545 dentry = lookup_one_len(qf_name, sb->s_root, strlen(qf_name));
1546 if (IS_ERR(dentry))
1547 return PTR_ERR(dentry);
1549 if (!dentry->d_inode) {
1550 error = -ENOENT;
1551 goto out;
1554 error = security_quota_on(dentry);
1555 if (!error)
1556 error = vfs_quota_on_inode(dentry->d_inode, type, format_id);
1558 out:
1559 dput(dentry);
1560 return error;
1563 /* Generic routine for getting common part of quota structure */
1564 static void do_get_dqblk(struct dquot *dquot, struct if_dqblk *di)
1566 struct mem_dqblk *dm = &dquot->dq_dqb;
1568 spin_lock(&dq_data_lock);
1569 di->dqb_bhardlimit = dm->dqb_bhardlimit;
1570 di->dqb_bsoftlimit = dm->dqb_bsoftlimit;
1571 di->dqb_curspace = dm->dqb_curspace;
1572 di->dqb_ihardlimit = dm->dqb_ihardlimit;
1573 di->dqb_isoftlimit = dm->dqb_isoftlimit;
1574 di->dqb_curinodes = dm->dqb_curinodes;
1575 di->dqb_btime = dm->dqb_btime;
1576 di->dqb_itime = dm->dqb_itime;
1577 di->dqb_valid = QIF_ALL;
1578 spin_unlock(&dq_data_lock);
1581 int vfs_get_dqblk(struct super_block *sb, int type, qid_t id, struct if_dqblk *di)
1583 struct dquot *dquot;
1585 mutex_lock(&sb_dqopt(sb)->dqonoff_mutex);
1586 if (!(dquot = dqget(sb, id, type))) {
1587 mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
1588 return -ESRCH;
1590 do_get_dqblk(dquot, di);
1591 dqput(dquot);
1592 mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
1593 return 0;
1596 /* Generic routine for setting common part of quota structure */
1597 static void do_set_dqblk(struct dquot *dquot, struct if_dqblk *di)
1599 struct mem_dqblk *dm = &dquot->dq_dqb;
1600 int check_blim = 0, check_ilim = 0;
1602 spin_lock(&dq_data_lock);
1603 if (di->dqb_valid & QIF_SPACE) {
1604 dm->dqb_curspace = di->dqb_curspace;
1605 check_blim = 1;
1607 if (di->dqb_valid & QIF_BLIMITS) {
1608 dm->dqb_bsoftlimit = di->dqb_bsoftlimit;
1609 dm->dqb_bhardlimit = di->dqb_bhardlimit;
1610 check_blim = 1;
1612 if (di->dqb_valid & QIF_INODES) {
1613 dm->dqb_curinodes = di->dqb_curinodes;
1614 check_ilim = 1;
1616 if (di->dqb_valid & QIF_ILIMITS) {
1617 dm->dqb_isoftlimit = di->dqb_isoftlimit;
1618 dm->dqb_ihardlimit = di->dqb_ihardlimit;
1619 check_ilim = 1;
1621 if (di->dqb_valid & QIF_BTIME)
1622 dm->dqb_btime = di->dqb_btime;
1623 if (di->dqb_valid & QIF_ITIME)
1624 dm->dqb_itime = di->dqb_itime;
1626 if (check_blim) {
1627 if (!dm->dqb_bsoftlimit || toqb(dm->dqb_curspace) < dm->dqb_bsoftlimit) {
1628 dm->dqb_btime = 0;
1629 clear_bit(DQ_BLKS_B, &dquot->dq_flags);
1631 else if (!(di->dqb_valid & QIF_BTIME)) /* Set grace only if user hasn't provided his own... */
1632 dm->dqb_btime = get_seconds() + sb_dqopt(dquot->dq_sb)->info[dquot->dq_type].dqi_bgrace;
1634 if (check_ilim) {
1635 if (!dm->dqb_isoftlimit || dm->dqb_curinodes < dm->dqb_isoftlimit) {
1636 dm->dqb_itime = 0;
1637 clear_bit(DQ_INODES_B, &dquot->dq_flags);
1639 else if (!(di->dqb_valid & QIF_ITIME)) /* Set grace only if user hasn't provided his own... */
1640 dm->dqb_itime = get_seconds() + sb_dqopt(dquot->dq_sb)->info[dquot->dq_type].dqi_igrace;
1642 if (dm->dqb_bhardlimit || dm->dqb_bsoftlimit || dm->dqb_ihardlimit || dm->dqb_isoftlimit)
1643 clear_bit(DQ_FAKE_B, &dquot->dq_flags);
1644 else
1645 set_bit(DQ_FAKE_B, &dquot->dq_flags);
1646 spin_unlock(&dq_data_lock);
1647 mark_dquot_dirty(dquot);
1650 int vfs_set_dqblk(struct super_block *sb, int type, qid_t id, struct if_dqblk *di)
1652 struct dquot *dquot;
1654 mutex_lock(&sb_dqopt(sb)->dqonoff_mutex);
1655 if (!(dquot = dqget(sb, id, type))) {
1656 mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
1657 return -ESRCH;
1659 do_set_dqblk(dquot, di);
1660 dqput(dquot);
1661 mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
1662 return 0;
1665 /* Generic routine for getting common part of quota file information */
1666 int vfs_get_dqinfo(struct super_block *sb, int type, struct if_dqinfo *ii)
1668 struct mem_dqinfo *mi;
1670 mutex_lock(&sb_dqopt(sb)->dqonoff_mutex);
1671 if (!sb_has_quota_enabled(sb, type)) {
1672 mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
1673 return -ESRCH;
1675 mi = sb_dqopt(sb)->info + type;
1676 spin_lock(&dq_data_lock);
1677 ii->dqi_bgrace = mi->dqi_bgrace;
1678 ii->dqi_igrace = mi->dqi_igrace;
1679 ii->dqi_flags = mi->dqi_flags & DQF_MASK;
1680 ii->dqi_valid = IIF_ALL;
1681 spin_unlock(&dq_data_lock);
1682 mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
1683 return 0;
1686 /* Generic routine for setting common part of quota file information */
1687 int vfs_set_dqinfo(struct super_block *sb, int type, struct if_dqinfo *ii)
1689 struct mem_dqinfo *mi;
1691 mutex_lock(&sb_dqopt(sb)->dqonoff_mutex);
1692 if (!sb_has_quota_enabled(sb, type)) {
1693 mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
1694 return -ESRCH;
1696 mi = sb_dqopt(sb)->info + type;
1697 spin_lock(&dq_data_lock);
1698 if (ii->dqi_valid & IIF_BGRACE)
1699 mi->dqi_bgrace = ii->dqi_bgrace;
1700 if (ii->dqi_valid & IIF_IGRACE)
1701 mi->dqi_igrace = ii->dqi_igrace;
1702 if (ii->dqi_valid & IIF_FLAGS)
1703 mi->dqi_flags = (mi->dqi_flags & ~DQF_MASK) | (ii->dqi_flags & DQF_MASK);
1704 spin_unlock(&dq_data_lock);
1705 mark_info_dirty(sb, type);
1706 /* Force write to disk */
1707 sb->dq_op->write_info(sb, type);
1708 mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
1709 return 0;
1712 struct quotactl_ops vfs_quotactl_ops = {
1713 .quota_on = vfs_quota_on,
1714 .quota_off = vfs_quota_off,
1715 .quota_sync = vfs_quota_sync,
1716 .get_info = vfs_get_dqinfo,
1717 .set_info = vfs_set_dqinfo,
1718 .get_dqblk = vfs_get_dqblk,
1719 .set_dqblk = vfs_set_dqblk
1722 static ctl_table fs_dqstats_table[] = {
1724 .ctl_name = FS_DQ_LOOKUPS,
1725 .procname = "lookups",
1726 .data = &dqstats.lookups,
1727 .maxlen = sizeof(int),
1728 .mode = 0444,
1729 .proc_handler = &proc_dointvec,
1732 .ctl_name = FS_DQ_DROPS,
1733 .procname = "drops",
1734 .data = &dqstats.drops,
1735 .maxlen = sizeof(int),
1736 .mode = 0444,
1737 .proc_handler = &proc_dointvec,
1740 .ctl_name = FS_DQ_READS,
1741 .procname = "reads",
1742 .data = &dqstats.reads,
1743 .maxlen = sizeof(int),
1744 .mode = 0444,
1745 .proc_handler = &proc_dointvec,
1748 .ctl_name = FS_DQ_WRITES,
1749 .procname = "writes",
1750 .data = &dqstats.writes,
1751 .maxlen = sizeof(int),
1752 .mode = 0444,
1753 .proc_handler = &proc_dointvec,
1756 .ctl_name = FS_DQ_CACHE_HITS,
1757 .procname = "cache_hits",
1758 .data = &dqstats.cache_hits,
1759 .maxlen = sizeof(int),
1760 .mode = 0444,
1761 .proc_handler = &proc_dointvec,
1764 .ctl_name = FS_DQ_ALLOCATED,
1765 .procname = "allocated_dquots",
1766 .data = &dqstats.allocated_dquots,
1767 .maxlen = sizeof(int),
1768 .mode = 0444,
1769 .proc_handler = &proc_dointvec,
1772 .ctl_name = FS_DQ_FREE,
1773 .procname = "free_dquots",
1774 .data = &dqstats.free_dquots,
1775 .maxlen = sizeof(int),
1776 .mode = 0444,
1777 .proc_handler = &proc_dointvec,
1780 .ctl_name = FS_DQ_SYNCS,
1781 .procname = "syncs",
1782 .data = &dqstats.syncs,
1783 .maxlen = sizeof(int),
1784 .mode = 0444,
1785 .proc_handler = &proc_dointvec,
1788 .ctl_name = FS_DQ_WARNINGS,
1789 .procname = "warnings",
1790 .data = &flag_print_warnings,
1791 .maxlen = sizeof(int),
1792 .mode = 0644,
1793 .proc_handler = &proc_dointvec,
1795 { .ctl_name = 0 },
1798 static ctl_table fs_table[] = {
1800 .ctl_name = FS_DQSTATS,
1801 .procname = "quota",
1802 .mode = 0555,
1803 .child = fs_dqstats_table,
1805 { .ctl_name = 0 },
1808 static ctl_table sys_table[] = {
1810 .ctl_name = CTL_FS,
1811 .procname = "fs",
1812 .mode = 0555,
1813 .child = fs_table,
1815 { .ctl_name = 0 },
1818 static int __init dquot_init(void)
1820 int i;
1821 unsigned long nr_hash, order;
1823 printk(KERN_NOTICE "VFS: Disk quotas %s\n", __DQUOT_VERSION__);
1825 register_sysctl_table(sys_table, 0);
1827 dquot_cachep = kmem_cache_create("dquot",
1828 sizeof(struct dquot), sizeof(unsigned long) * 4,
1829 (SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT|
1830 SLAB_MEM_SPREAD|SLAB_PANIC),
1831 NULL, NULL);
1833 order = 0;
1834 dquot_hash = (struct hlist_head *)__get_free_pages(GFP_ATOMIC, order);
1835 if (!dquot_hash)
1836 panic("Cannot create dquot hash table");
1838 /* Find power-of-two hlist_heads which can fit into allocation */
1839 nr_hash = (1UL << order) * PAGE_SIZE / sizeof(struct hlist_head);
1840 dq_hash_bits = 0;
1841 do {
1842 dq_hash_bits++;
1843 } while (nr_hash >> dq_hash_bits);
1844 dq_hash_bits--;
1846 nr_hash = 1UL << dq_hash_bits;
1847 dq_hash_mask = nr_hash - 1;
1848 for (i = 0; i < nr_hash; i++)
1849 INIT_HLIST_HEAD(dquot_hash + i);
1851 printk("Dquot-cache hash table entries: %ld (order %ld, %ld bytes)\n",
1852 nr_hash, order, (PAGE_SIZE << order));
1854 set_shrinker(DEFAULT_SEEKS, shrink_dqcache_memory);
1856 return 0;
1858 module_init(dquot_init);
1860 EXPORT_SYMBOL(register_quota_format);
1861 EXPORT_SYMBOL(unregister_quota_format);
1862 EXPORT_SYMBOL(dqstats);
1863 EXPORT_SYMBOL(dq_data_lock);
1864 EXPORT_SYMBOL(vfs_quota_on);
1865 EXPORT_SYMBOL(vfs_quota_on_mount);
1866 EXPORT_SYMBOL(vfs_quota_off);
1867 EXPORT_SYMBOL(vfs_quota_sync);
1868 EXPORT_SYMBOL(vfs_get_dqinfo);
1869 EXPORT_SYMBOL(vfs_set_dqinfo);
1870 EXPORT_SYMBOL(vfs_get_dqblk);
1871 EXPORT_SYMBOL(vfs_set_dqblk);
1872 EXPORT_SYMBOL(dquot_commit);
1873 EXPORT_SYMBOL(dquot_commit_info);
1874 EXPORT_SYMBOL(dquot_acquire);
1875 EXPORT_SYMBOL(dquot_release);
1876 EXPORT_SYMBOL(dquot_mark_dquot_dirty);
1877 EXPORT_SYMBOL(dquot_initialize);
1878 EXPORT_SYMBOL(dquot_drop);
1879 EXPORT_SYMBOL(dquot_alloc_space);
1880 EXPORT_SYMBOL(dquot_alloc_inode);
1881 EXPORT_SYMBOL(dquot_free_space);
1882 EXPORT_SYMBOL(dquot_free_inode);
1883 EXPORT_SYMBOL(dquot_transfer);