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
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
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>
61 #include <linux/mount.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.. */
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
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 >
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
;
141 spin_unlock(&dq_list_lock
);
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
= "a_formats
; *actqf
&& *actqf
!= fmt
; actqf
= &(*actqf
)->qf_next
);
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
)) {
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
))
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
))
176 spin_unlock(&dq_list_lock
);
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
)
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
;
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
)
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
))
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
);
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
))
305 list_del_init(&dquot
->dq_dirty
);
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
);
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
);
344 set_bit(DQ_ACTIVE_B
, &dquot
->dq_flags
);
346 mutex_unlock(&dqopt
->dqio_mutex
);
347 mutex_unlock(&dquot
->dq_lock
);
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
);
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
);
376 mutex_unlock(&dqopt
->dqio_mutex
);
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)
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
);
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
);
401 clear_bit(DQ_ACTIVE_B
, &dquot
->dq_flags
);
402 mutex_unlock(&dqopt
->dqio_mutex
);
404 mutex_unlock(&dquot
->dq_lock
);
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
;
419 spin_lock(&dq_list_lock
);
420 list_for_each_entry_safe(dquot
, tmp
, &inuse_list
, dq_inuse
) {
421 if (dquot
->dq_sb
!= sb
)
423 if (dquot
->dq_type
!= type
)
425 /* Wait for dquot users */
426 if (atomic_read(&dquot
->dq_count
)) {
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
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
440 if (atomic_read(&dquot
->dq_count
) > 1)
442 finish_wait(&dquot
->dq_wait_unused
, &wait
);
444 /* At this moment dquot() need not exist (it could be
445 * reclaimed by prune_dqcache(). Hence we must
450 * Quota now has no users and it has been written on last
453 remove_dquot_hash(dquot
);
454 remove_free_dquot(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
;
465 struct quota_info
*dqopt
= sb_dqopt(sb
);
468 mutex_lock(&dqopt
->dqonoff_mutex
);
469 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++) {
470 if (type
!= -1 && cnt
!= type
)
472 if (!sb_has_quota_enabled(sb
, cnt
))
474 spin_lock(&dq_list_lock
);
475 dirty
= &dqopt
->info
[cnt
].dqi_dirty_list
;
476 while (!list_empty(dirty
)) {
477 dquot
= list_first_entry(dirty
, 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
);
483 /* Now we have active dquot from which someone is
484 * holding reference so we can safely just increase
486 atomic_inc(&dquot
->dq_count
);
488 spin_unlock(&dq_list_lock
);
489 sb
->dq_op
->write_dquot(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
);
502 spin_unlock(&dq_list_lock
);
503 mutex_unlock(&dqopt
->dqonoff_mutex
);
508 /* Free unused dquots from cache */
509 static void prune_dqcache(int count
)
511 struct list_head
*head
;
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
);
520 kmem_cache_free(dquot_cachep
, dquot
);
522 head
= free_dquots
.prev
;
527 * This is called from kswapd when we think we need some
531 static int shrink_dqcache_memory(int nr
, gfp_t gfp_mask
)
534 spin_lock(&dq_list_lock
);
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
)
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",
555 quotatypes
[dquot
->dq_type
],
561 spin_lock(&dq_list_lock
);
563 spin_unlock(&dq_list_lock
);
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
);
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
);
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
);
590 atomic_dec(&dquot
->dq_count
);
591 #ifdef __DQUOT_PARANOIA
593 BUG_ON(!list_empty(&dquot
->dq_free
));
595 put_dquot_last(dquot
);
596 spin_unlock(&dq_list_lock
);
599 static struct dquot
*get_empty_dquot(struct super_block
*sb
, int type
)
603 dquot
= kmem_cache_zalloc(dquot_cachep
, GFP_NOFS
);
607 mutex_init(&dquot
->dq_lock
);
608 INIT_LIST_HEAD(&dquot
->dq_free
);
609 INIT_LIST_HEAD(&dquot
->dq_inuse
);
610 INIT_HLIST_NODE(&dquot
->dq_hash
);
611 INIT_LIST_HEAD(&dquot
->dq_dirty
);
612 init_waitqueue_head(&dquot
->dq_wait_unused
);
614 dquot
->dq_type
= type
;
615 atomic_set(&dquot
->dq_count
, 1);
621 * Get reference to dquot
622 * MUST be called with either dqptr_sem or dqonoff_mutex held
624 static struct dquot
*dqget(struct super_block
*sb
, unsigned int id
, int type
)
626 unsigned int hashent
= hashfn(sb
, id
, type
);
627 struct dquot
*dquot
, *empty
= NODQUOT
;
629 if (!sb_has_quota_enabled(sb
, type
))
632 spin_lock(&dq_list_lock
);
633 if ((dquot
= find_dquot(hashent
, sb
, id
, type
)) == NODQUOT
) {
634 if (empty
== NODQUOT
) {
635 spin_unlock(&dq_list_lock
);
636 if ((empty
= get_empty_dquot(sb
, type
)) == NODQUOT
)
637 schedule(); /* Try to wait for a moment... */
642 /* all dquots go on the inuse_list */
644 /* hash it first so it can be found */
645 insert_dquot_hash(dquot
);
647 spin_unlock(&dq_list_lock
);
649 if (!atomic_read(&dquot
->dq_count
))
650 remove_free_dquot(dquot
);
651 atomic_inc(&dquot
->dq_count
);
652 dqstats
.cache_hits
++;
654 spin_unlock(&dq_list_lock
);
656 kmem_cache_free(dquot_cachep
, empty
);
658 /* Wait for dq_lock - after this we know that either dquot_release() is already
659 * finished or it will be canceled due to dq_count > 1 test */
660 wait_on_dquot(dquot
);
661 /* Read the dquot and instantiate it (everything done only if needed) */
662 if (!test_bit(DQ_ACTIVE_B
, &dquot
->dq_flags
) && sb
->dq_op
->acquire_dquot(dquot
) < 0) {
666 #ifdef __DQUOT_PARANOIA
667 BUG_ON(!dquot
->dq_sb
); /* Has somebody invalidated entry under us? */
673 static int dqinit_needed(struct inode
*inode
, int type
)
677 if (IS_NOQUOTA(inode
))
680 return inode
->i_dquot
[type
] == NODQUOT
;
681 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++)
682 if (inode
->i_dquot
[cnt
] == NODQUOT
)
687 /* This routine is guarded by dqonoff_mutex mutex */
688 static void add_dquot_ref(struct super_block
*sb
, int type
)
693 spin_lock(&inode_lock
);
694 list_for_each_entry(inode
, &sb
->s_inodes
, i_sb_list
) {
695 if (!atomic_read(&inode
->i_writecount
))
697 if (!dqinit_needed(inode
, type
))
699 if (inode
->i_state
& (I_FREEING
|I_WILL_FREE
))
703 spin_unlock(&inode_lock
);
705 sb
->dq_op
->initialize(inode
, type
);
707 /* As we may have blocked we had better restart... */
710 spin_unlock(&inode_lock
);
713 /* Return 0 if dqput() won't block (note that 1 doesn't necessarily mean blocking) */
714 static inline int dqput_blocks(struct dquot
*dquot
)
716 if (atomic_read(&dquot
->dq_count
) <= 1)
721 /* Remove references to dquots from inode - add dquot to list for freeing if needed */
722 /* We can't race with anybody because we hold dqptr_sem for writing... */
723 static int remove_inode_dquot_ref(struct inode
*inode
, int type
,
724 struct list_head
*tofree_head
)
726 struct dquot
*dquot
= inode
->i_dquot
[type
];
728 inode
->i_dquot
[type
] = NODQUOT
;
729 if (dquot
!= NODQUOT
) {
730 if (dqput_blocks(dquot
)) {
731 #ifdef __DQUOT_PARANOIA
732 if (atomic_read(&dquot
->dq_count
) != 1)
733 printk(KERN_WARNING
"VFS: Adding dquot with dq_count %d to dispose list.\n", atomic_read(&dquot
->dq_count
));
735 spin_lock(&dq_list_lock
);
736 list_add(&dquot
->dq_free
, tofree_head
); /* As dquot must have currently users it can't be on the free list... */
737 spin_unlock(&dq_list_lock
);
741 dqput(dquot
); /* We have guaranteed we won't block */
746 /* Free list of dquots - called from inode.c */
747 /* dquots are removed from inodes, no new references can be got so we are the only ones holding reference */
748 static void put_dquot_list(struct list_head
*tofree_head
)
750 struct list_head
*act_head
;
753 act_head
= tofree_head
->next
;
754 /* So now we have dquots on the list... Just free them */
755 while (act_head
!= tofree_head
) {
756 dquot
= list_entry(act_head
, struct dquot
, dq_free
);
757 act_head
= act_head
->next
;
758 list_del_init(&dquot
->dq_free
); /* Remove dquot from the list so we won't have problems... */
763 static void remove_dquot_ref(struct super_block
*sb
, int type
,
764 struct list_head
*tofree_head
)
768 spin_lock(&inode_lock
);
769 list_for_each_entry(inode
, &sb
->s_inodes
, i_sb_list
) {
770 if (!IS_NOQUOTA(inode
))
771 remove_inode_dquot_ref(inode
, type
, tofree_head
);
773 spin_unlock(&inode_lock
);
776 /* Gather all references from inodes and drop them */
777 static void drop_dquot_ref(struct super_block
*sb
, int type
)
779 LIST_HEAD(tofree_head
);
782 down_write(&sb_dqopt(sb
)->dqptr_sem
);
783 remove_dquot_ref(sb
, type
, &tofree_head
);
784 up_write(&sb_dqopt(sb
)->dqptr_sem
);
785 put_dquot_list(&tofree_head
);
789 static inline void dquot_incr_inodes(struct dquot
*dquot
, unsigned long number
)
791 dquot
->dq_dqb
.dqb_curinodes
+= number
;
794 static inline void dquot_incr_space(struct dquot
*dquot
, qsize_t number
)
796 dquot
->dq_dqb
.dqb_curspace
+= number
;
799 static inline void dquot_decr_inodes(struct dquot
*dquot
, unsigned long number
)
801 if (dquot
->dq_dqb
.dqb_curinodes
> number
)
802 dquot
->dq_dqb
.dqb_curinodes
-= number
;
804 dquot
->dq_dqb
.dqb_curinodes
= 0;
805 if (dquot
->dq_dqb
.dqb_curinodes
<= dquot
->dq_dqb
.dqb_isoftlimit
)
806 dquot
->dq_dqb
.dqb_itime
= (time_t) 0;
807 clear_bit(DQ_INODES_B
, &dquot
->dq_flags
);
810 static inline void dquot_decr_space(struct dquot
*dquot
, qsize_t number
)
812 if (dquot
->dq_dqb
.dqb_curspace
> number
)
813 dquot
->dq_dqb
.dqb_curspace
-= number
;
815 dquot
->dq_dqb
.dqb_curspace
= 0;
816 if (toqb(dquot
->dq_dqb
.dqb_curspace
) <= dquot
->dq_dqb
.dqb_bsoftlimit
)
817 dquot
->dq_dqb
.dqb_btime
= (time_t) 0;
818 clear_bit(DQ_BLKS_B
, &dquot
->dq_flags
);
821 static int flag_print_warnings
= 1;
823 static inline int need_print_warning(struct dquot
*dquot
)
825 if (!flag_print_warnings
)
828 switch (dquot
->dq_type
) {
830 return current
->fsuid
== dquot
->dq_id
;
832 return in_group_p(dquot
->dq_id
);
837 /* Values of warnings */
840 #define ISOFTLONGWARN 2
843 #define BSOFTLONGWARN 5
846 /* Print warning to user which exceeded quota */
847 static void print_warning(struct dquot
*dquot
, const char warntype
)
850 struct tty_struct
*tty
;
851 int flag
= (warntype
== BHARDWARN
|| warntype
== BSOFTLONGWARN
) ? DQ_BLKS_B
:
852 ((warntype
== IHARDWARN
|| warntype
== ISOFTLONGWARN
) ? DQ_INODES_B
: 0);
854 if (!need_print_warning(dquot
) || (flag
&& test_and_set_bit(flag
, &dquot
->dq_flags
)))
857 mutex_lock(&tty_mutex
);
858 tty
= get_current_tty();
861 tty_write_message(tty
, dquot
->dq_sb
->s_id
);
862 if (warntype
== ISOFTWARN
|| warntype
== BSOFTWARN
)
863 tty_write_message(tty
, ": warning, ");
865 tty_write_message(tty
, ": write failed, ");
866 tty_write_message(tty
, quotatypes
[dquot
->dq_type
]);
869 msg
= " file limit reached.\r\n";
872 msg
= " file quota exceeded too long.\r\n";
875 msg
= " file quota exceeded.\r\n";
878 msg
= " block limit reached.\r\n";
881 msg
= " block quota exceeded too long.\r\n";
884 msg
= " block quota exceeded.\r\n";
887 tty_write_message(tty
, msg
);
889 mutex_unlock(&tty_mutex
);
892 static inline void flush_warnings(struct dquot
**dquots
, char *warntype
)
896 for (i
= 0; i
< MAXQUOTAS
; i
++)
897 if (dquots
[i
] != NODQUOT
&& warntype
[i
] != NOWARN
)
898 print_warning(dquots
[i
], warntype
[i
]);
901 static inline char ignore_hardlimit(struct dquot
*dquot
)
903 struct mem_dqinfo
*info
= &sb_dqopt(dquot
->dq_sb
)->info
[dquot
->dq_type
];
905 return capable(CAP_SYS_RESOURCE
) &&
906 (info
->dqi_format
->qf_fmt_id
!= QFMT_VFS_OLD
|| !(info
->dqi_flags
& V1_DQF_RSQUASH
));
909 /* needs dq_data_lock */
910 static int check_idq(struct dquot
*dquot
, ulong inodes
, char *warntype
)
913 if (inodes
<= 0 || test_bit(DQ_FAKE_B
, &dquot
->dq_flags
))
916 if (dquot
->dq_dqb
.dqb_ihardlimit
&&
917 (dquot
->dq_dqb
.dqb_curinodes
+ inodes
) > dquot
->dq_dqb
.dqb_ihardlimit
&&
918 !ignore_hardlimit(dquot
)) {
919 *warntype
= IHARDWARN
;
923 if (dquot
->dq_dqb
.dqb_isoftlimit
&&
924 (dquot
->dq_dqb
.dqb_curinodes
+ inodes
) > dquot
->dq_dqb
.dqb_isoftlimit
&&
925 dquot
->dq_dqb
.dqb_itime
&& get_seconds() >= dquot
->dq_dqb
.dqb_itime
&&
926 !ignore_hardlimit(dquot
)) {
927 *warntype
= ISOFTLONGWARN
;
931 if (dquot
->dq_dqb
.dqb_isoftlimit
&&
932 (dquot
->dq_dqb
.dqb_curinodes
+ inodes
) > dquot
->dq_dqb
.dqb_isoftlimit
&&
933 dquot
->dq_dqb
.dqb_itime
== 0) {
934 *warntype
= ISOFTWARN
;
935 dquot
->dq_dqb
.dqb_itime
= get_seconds() + sb_dqopt(dquot
->dq_sb
)->info
[dquot
->dq_type
].dqi_igrace
;
941 /* needs dq_data_lock */
942 static int check_bdq(struct dquot
*dquot
, qsize_t space
, int prealloc
, char *warntype
)
945 if (space
<= 0 || test_bit(DQ_FAKE_B
, &dquot
->dq_flags
))
948 if (dquot
->dq_dqb
.dqb_bhardlimit
&&
949 toqb(dquot
->dq_dqb
.dqb_curspace
+ space
) > dquot
->dq_dqb
.dqb_bhardlimit
&&
950 !ignore_hardlimit(dquot
)) {
952 *warntype
= BHARDWARN
;
956 if (dquot
->dq_dqb
.dqb_bsoftlimit
&&
957 toqb(dquot
->dq_dqb
.dqb_curspace
+ space
) > dquot
->dq_dqb
.dqb_bsoftlimit
&&
958 dquot
->dq_dqb
.dqb_btime
&& get_seconds() >= dquot
->dq_dqb
.dqb_btime
&&
959 !ignore_hardlimit(dquot
)) {
961 *warntype
= BSOFTLONGWARN
;
965 if (dquot
->dq_dqb
.dqb_bsoftlimit
&&
966 toqb(dquot
->dq_dqb
.dqb_curspace
+ space
) > dquot
->dq_dqb
.dqb_bsoftlimit
&&
967 dquot
->dq_dqb
.dqb_btime
== 0) {
969 *warntype
= BSOFTWARN
;
970 dquot
->dq_dqb
.dqb_btime
= get_seconds() + sb_dqopt(dquot
->dq_sb
)->info
[dquot
->dq_type
].dqi_bgrace
;
974 * We don't allow preallocation to exceed softlimit so exceeding will
984 * Initialize quota pointers in inode
985 * Transaction must be started at entry
987 int dquot_initialize(struct inode
*inode
, int type
)
992 /* First test before acquiring mutex - solves deadlocks when we
993 * re-enter the quota code and are already holding the mutex */
994 if (IS_NOQUOTA(inode
))
996 down_write(&sb_dqopt(inode
->i_sb
)->dqptr_sem
);
997 /* Having dqptr_sem we know NOQUOTA flags can't be altered... */
998 if (IS_NOQUOTA(inode
))
1000 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++) {
1001 if (type
!= -1 && cnt
!= type
)
1003 if (inode
->i_dquot
[cnt
] == NODQUOT
) {
1012 inode
->i_dquot
[cnt
] = dqget(inode
->i_sb
, id
, cnt
);
1016 up_write(&sb_dqopt(inode
->i_sb
)->dqptr_sem
);
1021 * Release all quotas referenced by inode
1022 * Transaction must be started at an entry
1024 int dquot_drop(struct inode
*inode
)
1028 down_write(&sb_dqopt(inode
->i_sb
)->dqptr_sem
);
1029 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++) {
1030 if (inode
->i_dquot
[cnt
] != NODQUOT
) {
1031 dqput(inode
->i_dquot
[cnt
]);
1032 inode
->i_dquot
[cnt
] = NODQUOT
;
1035 up_write(&sb_dqopt(inode
->i_sb
)->dqptr_sem
);
1040 * Following four functions update i_blocks+i_bytes fields and
1041 * quota information (together with appropriate checks)
1042 * NOTE: We absolutely rely on the fact that caller dirties
1043 * the inode (usually macros in quotaops.h care about this) and
1044 * holds a handle for the current transaction so that dquot write and
1045 * inode write go into the same transaction.
1049 * This operation can block, but only after everything is updated
1051 int dquot_alloc_space(struct inode
*inode
, qsize_t number
, int warn
)
1053 int cnt
, ret
= NO_QUOTA
;
1054 char warntype
[MAXQUOTAS
];
1056 /* First test before acquiring mutex - solves deadlocks when we
1057 * re-enter the quota code and are already holding the mutex */
1058 if (IS_NOQUOTA(inode
)) {
1060 inode_add_bytes(inode
, number
);
1063 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++)
1064 warntype
[cnt
] = NOWARN
;
1066 down_read(&sb_dqopt(inode
->i_sb
)->dqptr_sem
);
1067 if (IS_NOQUOTA(inode
)) { /* Now we can do reliable test... */
1068 up_read(&sb_dqopt(inode
->i_sb
)->dqptr_sem
);
1071 spin_lock(&dq_data_lock
);
1072 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++) {
1073 if (inode
->i_dquot
[cnt
] == NODQUOT
)
1075 if (check_bdq(inode
->i_dquot
[cnt
], number
, warn
, warntype
+cnt
) == NO_QUOTA
)
1078 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++) {
1079 if (inode
->i_dquot
[cnt
] == NODQUOT
)
1081 dquot_incr_space(inode
->i_dquot
[cnt
], number
);
1083 inode_add_bytes(inode
, number
);
1086 spin_unlock(&dq_data_lock
);
1087 if (ret
== QUOTA_OK
)
1088 /* Dirtify all the dquots - this can block when journalling */
1089 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++)
1090 if (inode
->i_dquot
[cnt
])
1091 mark_dquot_dirty(inode
->i_dquot
[cnt
]);
1092 flush_warnings(inode
->i_dquot
, warntype
);
1093 up_read(&sb_dqopt(inode
->i_sb
)->dqptr_sem
);
1098 * This operation can block, but only after everything is updated
1100 int dquot_alloc_inode(const struct inode
*inode
, unsigned long number
)
1102 int cnt
, ret
= NO_QUOTA
;
1103 char warntype
[MAXQUOTAS
];
1105 /* First test before acquiring mutex - solves deadlocks when we
1106 * re-enter the quota code and are already holding the mutex */
1107 if (IS_NOQUOTA(inode
))
1109 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++)
1110 warntype
[cnt
] = NOWARN
;
1111 down_read(&sb_dqopt(inode
->i_sb
)->dqptr_sem
);
1112 if (IS_NOQUOTA(inode
)) {
1113 up_read(&sb_dqopt(inode
->i_sb
)->dqptr_sem
);
1116 spin_lock(&dq_data_lock
);
1117 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++) {
1118 if (inode
->i_dquot
[cnt
] == NODQUOT
)
1120 if (check_idq(inode
->i_dquot
[cnt
], number
, warntype
+cnt
) == NO_QUOTA
)
1124 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++) {
1125 if (inode
->i_dquot
[cnt
] == NODQUOT
)
1127 dquot_incr_inodes(inode
->i_dquot
[cnt
], number
);
1131 spin_unlock(&dq_data_lock
);
1132 if (ret
== QUOTA_OK
)
1133 /* Dirtify all the dquots - this can block when journalling */
1134 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++)
1135 if (inode
->i_dquot
[cnt
])
1136 mark_dquot_dirty(inode
->i_dquot
[cnt
]);
1137 flush_warnings((struct dquot
**)inode
->i_dquot
, warntype
);
1138 up_read(&sb_dqopt(inode
->i_sb
)->dqptr_sem
);
1143 * This operation can block, but only after everything is updated
1145 int dquot_free_space(struct inode
*inode
, qsize_t number
)
1149 /* First test before acquiring mutex - solves deadlocks when we
1150 * re-enter the quota code and are already holding the mutex */
1151 if (IS_NOQUOTA(inode
)) {
1153 inode_sub_bytes(inode
, number
);
1156 down_read(&sb_dqopt(inode
->i_sb
)->dqptr_sem
);
1157 /* Now recheck reliably when holding dqptr_sem */
1158 if (IS_NOQUOTA(inode
)) {
1159 up_read(&sb_dqopt(inode
->i_sb
)->dqptr_sem
);
1162 spin_lock(&dq_data_lock
);
1163 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++) {
1164 if (inode
->i_dquot
[cnt
] == NODQUOT
)
1166 dquot_decr_space(inode
->i_dquot
[cnt
], number
);
1168 inode_sub_bytes(inode
, number
);
1169 spin_unlock(&dq_data_lock
);
1170 /* Dirtify all the dquots - this can block when journalling */
1171 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++)
1172 if (inode
->i_dquot
[cnt
])
1173 mark_dquot_dirty(inode
->i_dquot
[cnt
]);
1174 up_read(&sb_dqopt(inode
->i_sb
)->dqptr_sem
);
1179 * This operation can block, but only after everything is updated
1181 int dquot_free_inode(const struct inode
*inode
, unsigned long number
)
1185 /* First test before acquiring mutex - solves deadlocks when we
1186 * re-enter the quota code and are already holding the mutex */
1187 if (IS_NOQUOTA(inode
))
1189 down_read(&sb_dqopt(inode
->i_sb
)->dqptr_sem
);
1190 /* Now recheck reliably when holding dqptr_sem */
1191 if (IS_NOQUOTA(inode
)) {
1192 up_read(&sb_dqopt(inode
->i_sb
)->dqptr_sem
);
1195 spin_lock(&dq_data_lock
);
1196 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++) {
1197 if (inode
->i_dquot
[cnt
] == NODQUOT
)
1199 dquot_decr_inodes(inode
->i_dquot
[cnt
], number
);
1201 spin_unlock(&dq_data_lock
);
1202 /* Dirtify all the dquots - this can block when journalling */
1203 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++)
1204 if (inode
->i_dquot
[cnt
])
1205 mark_dquot_dirty(inode
->i_dquot
[cnt
]);
1206 up_read(&sb_dqopt(inode
->i_sb
)->dqptr_sem
);
1211 * Transfer the number of inode and blocks from one diskquota to an other.
1213 * This operation can block, but only after everything is updated
1214 * A transaction must be started when entering this function.
1216 int dquot_transfer(struct inode
*inode
, struct iattr
*iattr
)
1219 struct dquot
*transfer_from
[MAXQUOTAS
];
1220 struct dquot
*transfer_to
[MAXQUOTAS
];
1221 int cnt
, ret
= NO_QUOTA
, chuid
= (iattr
->ia_valid
& ATTR_UID
) && inode
->i_uid
!= iattr
->ia_uid
,
1222 chgid
= (iattr
->ia_valid
& ATTR_GID
) && inode
->i_gid
!= iattr
->ia_gid
;
1223 char warntype
[MAXQUOTAS
];
1225 /* First test before acquiring mutex - solves deadlocks when we
1226 * re-enter the quota code and are already holding the mutex */
1227 if (IS_NOQUOTA(inode
))
1229 /* Clear the arrays */
1230 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++) {
1231 transfer_to
[cnt
] = transfer_from
[cnt
] = NODQUOT
;
1232 warntype
[cnt
] = NOWARN
;
1234 down_write(&sb_dqopt(inode
->i_sb
)->dqptr_sem
);
1235 /* Now recheck reliably when holding dqptr_sem */
1236 if (IS_NOQUOTA(inode
)) { /* File without quota accounting? */
1237 up_write(&sb_dqopt(inode
->i_sb
)->dqptr_sem
);
1240 /* First build the transfer_to list - here we can block on
1241 * reading/instantiating of dquots. We know that the transaction for
1242 * us was already started so we don't violate lock ranking here */
1243 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++) {
1248 transfer_to
[cnt
] = dqget(inode
->i_sb
, iattr
->ia_uid
, cnt
);
1253 transfer_to
[cnt
] = dqget(inode
->i_sb
, iattr
->ia_gid
, cnt
);
1257 spin_lock(&dq_data_lock
);
1258 space
= inode_get_bytes(inode
);
1259 /* Build the transfer_from list and check the limits */
1260 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++) {
1261 if (transfer_to
[cnt
] == NODQUOT
)
1263 transfer_from
[cnt
] = inode
->i_dquot
[cnt
];
1264 if (check_idq(transfer_to
[cnt
], 1, warntype
+cnt
) == NO_QUOTA
||
1265 check_bdq(transfer_to
[cnt
], space
, 0, warntype
+cnt
) == NO_QUOTA
)
1270 * Finally perform the needed transfer from transfer_from to transfer_to
1272 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++) {
1274 * Skip changes for same uid or gid or for turned off quota-type.
1276 if (transfer_to
[cnt
] == NODQUOT
)
1279 /* Due to IO error we might not have transfer_from[] structure */
1280 if (transfer_from
[cnt
]) {
1281 dquot_decr_inodes(transfer_from
[cnt
], 1);
1282 dquot_decr_space(transfer_from
[cnt
], space
);
1285 dquot_incr_inodes(transfer_to
[cnt
], 1);
1286 dquot_incr_space(transfer_to
[cnt
], space
);
1288 inode
->i_dquot
[cnt
] = transfer_to
[cnt
];
1292 spin_unlock(&dq_data_lock
);
1293 /* Dirtify all the dquots - this can block when journalling */
1294 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++) {
1295 if (transfer_from
[cnt
])
1296 mark_dquot_dirty(transfer_from
[cnt
]);
1297 if (transfer_to
[cnt
])
1298 mark_dquot_dirty(transfer_to
[cnt
]);
1300 flush_warnings(transfer_to
, warntype
);
1302 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++) {
1303 if (ret
== QUOTA_OK
&& transfer_from
[cnt
] != NODQUOT
)
1304 dqput(transfer_from
[cnt
]);
1305 if (ret
== NO_QUOTA
&& transfer_to
[cnt
] != NODQUOT
)
1306 dqput(transfer_to
[cnt
]);
1308 up_write(&sb_dqopt(inode
->i_sb
)->dqptr_sem
);
1313 * Write info of quota file to disk
1315 int dquot_commit_info(struct super_block
*sb
, int type
)
1318 struct quota_info
*dqopt
= sb_dqopt(sb
);
1320 mutex_lock(&dqopt
->dqio_mutex
);
1321 ret
= dqopt
->ops
[type
]->write_file_info(sb
, type
);
1322 mutex_unlock(&dqopt
->dqio_mutex
);
1327 * Definitions of diskquota operations.
1329 struct dquot_operations dquot_operations
= {
1330 .initialize
= dquot_initialize
,
1332 .alloc_space
= dquot_alloc_space
,
1333 .alloc_inode
= dquot_alloc_inode
,
1334 .free_space
= dquot_free_space
,
1335 .free_inode
= dquot_free_inode
,
1336 .transfer
= dquot_transfer
,
1337 .write_dquot
= dquot_commit
,
1338 .acquire_dquot
= dquot_acquire
,
1339 .release_dquot
= dquot_release
,
1340 .mark_dirty
= dquot_mark_dquot_dirty
,
1341 .write_info
= dquot_commit_info
1344 static inline void set_enable_flags(struct quota_info
*dqopt
, int type
)
1348 dqopt
->flags
|= DQUOT_USR_ENABLED
;
1351 dqopt
->flags
|= DQUOT_GRP_ENABLED
;
1356 static inline void reset_enable_flags(struct quota_info
*dqopt
, int type
)
1360 dqopt
->flags
&= ~DQUOT_USR_ENABLED
;
1363 dqopt
->flags
&= ~DQUOT_GRP_ENABLED
;
1369 * Turn quota off on a device. type == -1 ==> quotaoff for all types (umount)
1371 int vfs_quota_off(struct super_block
*sb
, int type
)
1374 struct quota_info
*dqopt
= sb_dqopt(sb
);
1375 struct inode
*toputinode
[MAXQUOTAS
];
1377 /* We need to serialize quota_off() for device */
1378 mutex_lock(&dqopt
->dqonoff_mutex
);
1379 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++) {
1380 toputinode
[cnt
] = NULL
;
1381 if (type
!= -1 && cnt
!= type
)
1383 if (!sb_has_quota_enabled(sb
, cnt
))
1385 reset_enable_flags(dqopt
, cnt
);
1387 /* Note: these are blocking operations */
1388 drop_dquot_ref(sb
, cnt
);
1389 invalidate_dquots(sb
, cnt
);
1391 * Now all dquots should be invalidated, all writes done so we should be only
1392 * users of the info. No locks needed.
1394 if (info_dirty(&dqopt
->info
[cnt
]))
1395 sb
->dq_op
->write_info(sb
, cnt
);
1396 if (dqopt
->ops
[cnt
]->free_file_info
)
1397 dqopt
->ops
[cnt
]->free_file_info(sb
, cnt
);
1398 put_quota_format(dqopt
->info
[cnt
].dqi_format
);
1400 toputinode
[cnt
] = dqopt
->files
[cnt
];
1401 dqopt
->files
[cnt
] = NULL
;
1402 dqopt
->info
[cnt
].dqi_flags
= 0;
1403 dqopt
->info
[cnt
].dqi_igrace
= 0;
1404 dqopt
->info
[cnt
].dqi_bgrace
= 0;
1405 dqopt
->ops
[cnt
] = NULL
;
1407 mutex_unlock(&dqopt
->dqonoff_mutex
);
1408 /* Sync the superblock so that buffers with quota data are written to
1409 * disk (and so userspace sees correct data afterwards). */
1410 if (sb
->s_op
->sync_fs
)
1411 sb
->s_op
->sync_fs(sb
, 1);
1412 sync_blockdev(sb
->s_bdev
);
1413 /* Now the quota files are just ordinary files and we can set the
1414 * inode flags back. Moreover we discard the pagecache so that
1415 * userspace sees the writes we did bypassing the pagecache. We
1416 * must also discard the blockdev buffers so that we see the
1417 * changes done by userspace on the next quotaon() */
1418 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++)
1419 if (toputinode
[cnt
]) {
1420 mutex_lock(&dqopt
->dqonoff_mutex
);
1421 /* If quota was reenabled in the meantime, we have
1423 if (!sb_has_quota_enabled(sb
, cnt
)) {
1424 mutex_lock_nested(&toputinode
[cnt
]->i_mutex
, I_MUTEX_QUOTA
);
1425 toputinode
[cnt
]->i_flags
&= ~(S_IMMUTABLE
|
1426 S_NOATIME
| S_NOQUOTA
);
1427 truncate_inode_pages(&toputinode
[cnt
]->i_data
, 0);
1428 mutex_unlock(&toputinode
[cnt
]->i_mutex
);
1429 mark_inode_dirty(toputinode
[cnt
]);
1430 iput(toputinode
[cnt
]);
1432 mutex_unlock(&dqopt
->dqonoff_mutex
);
1435 invalidate_bdev(sb
->s_bdev
);
1440 * Turn quotas on on a device
1443 /* Helper function when we already have the inode */
1444 static int vfs_quota_on_inode(struct inode
*inode
, int type
, int format_id
)
1446 struct quota_format_type
*fmt
= find_quota_format(format_id
);
1447 struct super_block
*sb
= inode
->i_sb
;
1448 struct quota_info
*dqopt
= sb_dqopt(sb
);
1454 if (!S_ISREG(inode
->i_mode
)) {
1458 if (IS_RDONLY(inode
)) {
1462 if (!sb
->s_op
->quota_write
|| !sb
->s_op
->quota_read
) {
1467 /* As we bypass the pagecache we must now flush the inode so that
1468 * we see all the changes from userspace... */
1469 write_inode_now(inode
, 1);
1470 /* And now flush the block cache so that kernel sees the changes */
1471 invalidate_bdev(sb
->s_bdev
);
1472 mutex_lock(&inode
->i_mutex
);
1473 mutex_lock(&dqopt
->dqonoff_mutex
);
1474 if (sb_has_quota_enabled(sb
, type
)) {
1478 /* We don't want quota and atime on quota files (deadlocks possible)
1479 * Also nobody should write to the file - we use special IO operations
1480 * which ignore the immutable bit. */
1481 down_write(&dqopt
->dqptr_sem
);
1482 oldflags
= inode
->i_flags
& (S_NOATIME
| S_IMMUTABLE
| S_NOQUOTA
);
1483 inode
->i_flags
|= S_NOQUOTA
| S_NOATIME
| S_IMMUTABLE
;
1484 up_write(&dqopt
->dqptr_sem
);
1485 sb
->dq_op
->drop(inode
);
1488 dqopt
->files
[type
] = igrab(inode
);
1489 if (!dqopt
->files
[type
])
1492 if (!fmt
->qf_ops
->check_quota_file(sb
, type
))
1495 dqopt
->ops
[type
] = fmt
->qf_ops
;
1496 dqopt
->info
[type
].dqi_format
= fmt
;
1497 INIT_LIST_HEAD(&dqopt
->info
[type
].dqi_dirty_list
);
1498 mutex_lock(&dqopt
->dqio_mutex
);
1499 if ((error
= dqopt
->ops
[type
]->read_file_info(sb
, type
)) < 0) {
1500 mutex_unlock(&dqopt
->dqio_mutex
);
1503 mutex_unlock(&dqopt
->dqio_mutex
);
1504 mutex_unlock(&inode
->i_mutex
);
1505 set_enable_flags(dqopt
, type
);
1507 add_dquot_ref(sb
, type
);
1508 mutex_unlock(&dqopt
->dqonoff_mutex
);
1513 dqopt
->files
[type
] = NULL
;
1516 mutex_unlock(&dqopt
->dqonoff_mutex
);
1517 if (oldflags
!= -1) {
1518 down_write(&dqopt
->dqptr_sem
);
1519 /* Set the flags back (in the case of accidental quotaon()
1520 * on a wrong file we don't want to mess up the flags) */
1521 inode
->i_flags
&= ~(S_NOATIME
| S_NOQUOTA
| S_IMMUTABLE
);
1522 inode
->i_flags
|= oldflags
;
1523 up_write(&dqopt
->dqptr_sem
);
1525 mutex_unlock(&inode
->i_mutex
);
1527 put_quota_format(fmt
);
1532 /* Actual function called from quotactl() */
1533 int vfs_quota_on(struct super_block
*sb
, int type
, int format_id
, char *path
)
1535 struct nameidata nd
;
1538 error
= path_lookup(path
, LOOKUP_FOLLOW
, &nd
);
1541 error
= security_quota_on(nd
.dentry
);
1544 /* Quota file not on the same filesystem? */
1545 if (nd
.mnt
->mnt_sb
!= sb
)
1548 error
= vfs_quota_on_inode(nd
.dentry
->d_inode
, type
, format_id
);
1555 * This function is used when filesystem needs to initialize quotas
1556 * during mount time.
1558 int vfs_quota_on_mount(struct super_block
*sb
, char *qf_name
,
1559 int format_id
, int type
)
1561 struct dentry
*dentry
;
1564 dentry
= lookup_one_len(qf_name
, sb
->s_root
, strlen(qf_name
));
1566 return PTR_ERR(dentry
);
1568 if (!dentry
->d_inode
) {
1573 error
= security_quota_on(dentry
);
1575 error
= vfs_quota_on_inode(dentry
->d_inode
, type
, format_id
);
1582 /* Generic routine for getting common part of quota structure */
1583 static void do_get_dqblk(struct dquot
*dquot
, struct if_dqblk
*di
)
1585 struct mem_dqblk
*dm
= &dquot
->dq_dqb
;
1587 spin_lock(&dq_data_lock
);
1588 di
->dqb_bhardlimit
= dm
->dqb_bhardlimit
;
1589 di
->dqb_bsoftlimit
= dm
->dqb_bsoftlimit
;
1590 di
->dqb_curspace
= dm
->dqb_curspace
;
1591 di
->dqb_ihardlimit
= dm
->dqb_ihardlimit
;
1592 di
->dqb_isoftlimit
= dm
->dqb_isoftlimit
;
1593 di
->dqb_curinodes
= dm
->dqb_curinodes
;
1594 di
->dqb_btime
= dm
->dqb_btime
;
1595 di
->dqb_itime
= dm
->dqb_itime
;
1596 di
->dqb_valid
= QIF_ALL
;
1597 spin_unlock(&dq_data_lock
);
1600 int vfs_get_dqblk(struct super_block
*sb
, int type
, qid_t id
, struct if_dqblk
*di
)
1602 struct dquot
*dquot
;
1604 mutex_lock(&sb_dqopt(sb
)->dqonoff_mutex
);
1605 if (!(dquot
= dqget(sb
, id
, type
))) {
1606 mutex_unlock(&sb_dqopt(sb
)->dqonoff_mutex
);
1609 do_get_dqblk(dquot
, di
);
1611 mutex_unlock(&sb_dqopt(sb
)->dqonoff_mutex
);
1615 /* Generic routine for setting common part of quota structure */
1616 static void do_set_dqblk(struct dquot
*dquot
, struct if_dqblk
*di
)
1618 struct mem_dqblk
*dm
= &dquot
->dq_dqb
;
1619 int check_blim
= 0, check_ilim
= 0;
1621 spin_lock(&dq_data_lock
);
1622 if (di
->dqb_valid
& QIF_SPACE
) {
1623 dm
->dqb_curspace
= di
->dqb_curspace
;
1626 if (di
->dqb_valid
& QIF_BLIMITS
) {
1627 dm
->dqb_bsoftlimit
= di
->dqb_bsoftlimit
;
1628 dm
->dqb_bhardlimit
= di
->dqb_bhardlimit
;
1631 if (di
->dqb_valid
& QIF_INODES
) {
1632 dm
->dqb_curinodes
= di
->dqb_curinodes
;
1635 if (di
->dqb_valid
& QIF_ILIMITS
) {
1636 dm
->dqb_isoftlimit
= di
->dqb_isoftlimit
;
1637 dm
->dqb_ihardlimit
= di
->dqb_ihardlimit
;
1640 if (di
->dqb_valid
& QIF_BTIME
)
1641 dm
->dqb_btime
= di
->dqb_btime
;
1642 if (di
->dqb_valid
& QIF_ITIME
)
1643 dm
->dqb_itime
= di
->dqb_itime
;
1646 if (!dm
->dqb_bsoftlimit
|| toqb(dm
->dqb_curspace
) < dm
->dqb_bsoftlimit
) {
1648 clear_bit(DQ_BLKS_B
, &dquot
->dq_flags
);
1650 else if (!(di
->dqb_valid
& QIF_BTIME
)) /* Set grace only if user hasn't provided his own... */
1651 dm
->dqb_btime
= get_seconds() + sb_dqopt(dquot
->dq_sb
)->info
[dquot
->dq_type
].dqi_bgrace
;
1654 if (!dm
->dqb_isoftlimit
|| dm
->dqb_curinodes
< dm
->dqb_isoftlimit
) {
1656 clear_bit(DQ_INODES_B
, &dquot
->dq_flags
);
1658 else if (!(di
->dqb_valid
& QIF_ITIME
)) /* Set grace only if user hasn't provided his own... */
1659 dm
->dqb_itime
= get_seconds() + sb_dqopt(dquot
->dq_sb
)->info
[dquot
->dq_type
].dqi_igrace
;
1661 if (dm
->dqb_bhardlimit
|| dm
->dqb_bsoftlimit
|| dm
->dqb_ihardlimit
|| dm
->dqb_isoftlimit
)
1662 clear_bit(DQ_FAKE_B
, &dquot
->dq_flags
);
1664 set_bit(DQ_FAKE_B
, &dquot
->dq_flags
);
1665 spin_unlock(&dq_data_lock
);
1666 mark_dquot_dirty(dquot
);
1669 int vfs_set_dqblk(struct super_block
*sb
, int type
, qid_t id
, struct if_dqblk
*di
)
1671 struct dquot
*dquot
;
1673 mutex_lock(&sb_dqopt(sb
)->dqonoff_mutex
);
1674 if (!(dquot
= dqget(sb
, id
, type
))) {
1675 mutex_unlock(&sb_dqopt(sb
)->dqonoff_mutex
);
1678 do_set_dqblk(dquot
, di
);
1680 mutex_unlock(&sb_dqopt(sb
)->dqonoff_mutex
);
1684 /* Generic routine for getting common part of quota file information */
1685 int vfs_get_dqinfo(struct super_block
*sb
, int type
, struct if_dqinfo
*ii
)
1687 struct mem_dqinfo
*mi
;
1689 mutex_lock(&sb_dqopt(sb
)->dqonoff_mutex
);
1690 if (!sb_has_quota_enabled(sb
, type
)) {
1691 mutex_unlock(&sb_dqopt(sb
)->dqonoff_mutex
);
1694 mi
= sb_dqopt(sb
)->info
+ type
;
1695 spin_lock(&dq_data_lock
);
1696 ii
->dqi_bgrace
= mi
->dqi_bgrace
;
1697 ii
->dqi_igrace
= mi
->dqi_igrace
;
1698 ii
->dqi_flags
= mi
->dqi_flags
& DQF_MASK
;
1699 ii
->dqi_valid
= IIF_ALL
;
1700 spin_unlock(&dq_data_lock
);
1701 mutex_unlock(&sb_dqopt(sb
)->dqonoff_mutex
);
1705 /* Generic routine for setting common part of quota file information */
1706 int vfs_set_dqinfo(struct super_block
*sb
, int type
, struct if_dqinfo
*ii
)
1708 struct mem_dqinfo
*mi
;
1710 mutex_lock(&sb_dqopt(sb
)->dqonoff_mutex
);
1711 if (!sb_has_quota_enabled(sb
, type
)) {
1712 mutex_unlock(&sb_dqopt(sb
)->dqonoff_mutex
);
1715 mi
= sb_dqopt(sb
)->info
+ type
;
1716 spin_lock(&dq_data_lock
);
1717 if (ii
->dqi_valid
& IIF_BGRACE
)
1718 mi
->dqi_bgrace
= ii
->dqi_bgrace
;
1719 if (ii
->dqi_valid
& IIF_IGRACE
)
1720 mi
->dqi_igrace
= ii
->dqi_igrace
;
1721 if (ii
->dqi_valid
& IIF_FLAGS
)
1722 mi
->dqi_flags
= (mi
->dqi_flags
& ~DQF_MASK
) | (ii
->dqi_flags
& DQF_MASK
);
1723 spin_unlock(&dq_data_lock
);
1724 mark_info_dirty(sb
, type
);
1725 /* Force write to disk */
1726 sb
->dq_op
->write_info(sb
, type
);
1727 mutex_unlock(&sb_dqopt(sb
)->dqonoff_mutex
);
1731 struct quotactl_ops vfs_quotactl_ops
= {
1732 .quota_on
= vfs_quota_on
,
1733 .quota_off
= vfs_quota_off
,
1734 .quota_sync
= vfs_quota_sync
,
1735 .get_info
= vfs_get_dqinfo
,
1736 .set_info
= vfs_set_dqinfo
,
1737 .get_dqblk
= vfs_get_dqblk
,
1738 .set_dqblk
= vfs_set_dqblk
1741 static ctl_table fs_dqstats_table
[] = {
1743 .ctl_name
= FS_DQ_LOOKUPS
,
1744 .procname
= "lookups",
1745 .data
= &dqstats
.lookups
,
1746 .maxlen
= sizeof(int),
1748 .proc_handler
= &proc_dointvec
,
1751 .ctl_name
= FS_DQ_DROPS
,
1752 .procname
= "drops",
1753 .data
= &dqstats
.drops
,
1754 .maxlen
= sizeof(int),
1756 .proc_handler
= &proc_dointvec
,
1759 .ctl_name
= FS_DQ_READS
,
1760 .procname
= "reads",
1761 .data
= &dqstats
.reads
,
1762 .maxlen
= sizeof(int),
1764 .proc_handler
= &proc_dointvec
,
1767 .ctl_name
= FS_DQ_WRITES
,
1768 .procname
= "writes",
1769 .data
= &dqstats
.writes
,
1770 .maxlen
= sizeof(int),
1772 .proc_handler
= &proc_dointvec
,
1775 .ctl_name
= FS_DQ_CACHE_HITS
,
1776 .procname
= "cache_hits",
1777 .data
= &dqstats
.cache_hits
,
1778 .maxlen
= sizeof(int),
1780 .proc_handler
= &proc_dointvec
,
1783 .ctl_name
= FS_DQ_ALLOCATED
,
1784 .procname
= "allocated_dquots",
1785 .data
= &dqstats
.allocated_dquots
,
1786 .maxlen
= sizeof(int),
1788 .proc_handler
= &proc_dointvec
,
1791 .ctl_name
= FS_DQ_FREE
,
1792 .procname
= "free_dquots",
1793 .data
= &dqstats
.free_dquots
,
1794 .maxlen
= sizeof(int),
1796 .proc_handler
= &proc_dointvec
,
1799 .ctl_name
= FS_DQ_SYNCS
,
1800 .procname
= "syncs",
1801 .data
= &dqstats
.syncs
,
1802 .maxlen
= sizeof(int),
1804 .proc_handler
= &proc_dointvec
,
1807 .ctl_name
= FS_DQ_WARNINGS
,
1808 .procname
= "warnings",
1809 .data
= &flag_print_warnings
,
1810 .maxlen
= sizeof(int),
1812 .proc_handler
= &proc_dointvec
,
1817 static ctl_table fs_table
[] = {
1819 .ctl_name
= FS_DQSTATS
,
1820 .procname
= "quota",
1822 .child
= fs_dqstats_table
,
1827 static ctl_table sys_table
[] = {
1837 static int __init
dquot_init(void)
1840 unsigned long nr_hash
, order
;
1842 printk(KERN_NOTICE
"VFS: Disk quotas %s\n", __DQUOT_VERSION__
);
1844 register_sysctl_table(sys_table
);
1846 dquot_cachep
= kmem_cache_create("dquot",
1847 sizeof(struct dquot
), sizeof(unsigned long) * 4,
1848 (SLAB_HWCACHE_ALIGN
|SLAB_RECLAIM_ACCOUNT
|
1849 SLAB_MEM_SPREAD
|SLAB_PANIC
),
1853 dquot_hash
= (struct hlist_head
*)__get_free_pages(GFP_ATOMIC
, order
);
1855 panic("Cannot create dquot hash table");
1857 /* Find power-of-two hlist_heads which can fit into allocation */
1858 nr_hash
= (1UL << order
) * PAGE_SIZE
/ sizeof(struct hlist_head
);
1862 } while (nr_hash
>> dq_hash_bits
);
1865 nr_hash
= 1UL << dq_hash_bits
;
1866 dq_hash_mask
= nr_hash
- 1;
1867 for (i
= 0; i
< nr_hash
; i
++)
1868 INIT_HLIST_HEAD(dquot_hash
+ i
);
1870 printk("Dquot-cache hash table entries: %ld (order %ld, %ld bytes)\n",
1871 nr_hash
, order
, (PAGE_SIZE
<< order
));
1873 set_shrinker(DEFAULT_SEEKS
, shrink_dqcache_memory
);
1877 module_init(dquot_init
);
1879 EXPORT_SYMBOL(register_quota_format
);
1880 EXPORT_SYMBOL(unregister_quota_format
);
1881 EXPORT_SYMBOL(dqstats
);
1882 EXPORT_SYMBOL(dq_data_lock
);
1883 EXPORT_SYMBOL(vfs_quota_on
);
1884 EXPORT_SYMBOL(vfs_quota_on_mount
);
1885 EXPORT_SYMBOL(vfs_quota_off
);
1886 EXPORT_SYMBOL(vfs_quota_sync
);
1887 EXPORT_SYMBOL(vfs_get_dqinfo
);
1888 EXPORT_SYMBOL(vfs_set_dqinfo
);
1889 EXPORT_SYMBOL(vfs_get_dqblk
);
1890 EXPORT_SYMBOL(vfs_set_dqblk
);
1891 EXPORT_SYMBOL(dquot_commit
);
1892 EXPORT_SYMBOL(dquot_commit_info
);
1893 EXPORT_SYMBOL(dquot_acquire
);
1894 EXPORT_SYMBOL(dquot_release
);
1895 EXPORT_SYMBOL(dquot_mark_dquot_dirty
);
1896 EXPORT_SYMBOL(dquot_initialize
);
1897 EXPORT_SYMBOL(dquot_drop
);
1898 EXPORT_SYMBOL(dquot_alloc_space
);
1899 EXPORT_SYMBOL(dquot_alloc_inode
);
1900 EXPORT_SYMBOL(dquot_free_space
);
1901 EXPORT_SYMBOL(dquot_free_inode
);
1902 EXPORT_SYMBOL(dquot_transfer
);