2 * Implementation of the diskquota system for the LINUX operating system. QUOTA
3 * is implemented using the BSD system call interface as the means of
4 * communication with the user level. This file contains the generic routines
5 * called by the different filesystems on allocation of an inode or block.
6 * These routines take care of the administration needed to have a consistent
7 * diskquota tracking system. The ideas of both user and group quotas are based
8 * on the Melbourne quota system as used on BSD derived systems. The internal
9 * implementation is based on one of the several variants of the LINUX
10 * inode-subsystem with added complexity of the diskquota system.
12 * Author: Marco van Wieringen <mvw@planets.elm.net>
14 * Fixes: Dmitry Gorodchanin <pgmdsg@ibi.com>, 11 Feb 96
16 * Revised list management to avoid races
17 * -- Bill Hawes, <whawes@star.net>, 9/98
19 * Fixed races in dquot_transfer(), dqget() and dquot_alloc_...().
20 * As the consequence the locking was moved from dquot_decr_...(),
21 * dquot_incr_...() to calling functions.
22 * invalidate_dquots() now writes modified dquots.
23 * Serialized quota_off() and quota_on() for mount point.
24 * Fixed a few bugs in grow_dquots().
25 * Fixed deadlock in write_dquot() - we no longer account quotas on
27 * remove_dquot_ref() moved to inode.c - it now traverses through inodes
28 * add_dquot_ref() restarts after blocking
29 * Added check for bogus uid and fixed check for group in quotactl.
30 * Jan Kara, <jack@suse.cz>, sponsored by SuSE CR, 10-11/99
32 * Used struct list_head instead of own list struct
33 * Invalidation of referenced dquots is no longer possible
34 * Improved free_dquots list management
35 * Quota and i_blocks are now updated in one place to avoid races
36 * Warnings are now delayed so we won't block in critical section
37 * Write updated not to require dquot lock
38 * Jan Kara, <jack@suse.cz>, 9/2000
40 * Added dynamic quota structure allocation
41 * Jan Kara <jack@suse.cz> 12/2000
43 * Rewritten quota interface. Implemented new quota format and
44 * formats registering.
45 * Jan Kara, <jack@suse.cz>, 2001,2002
48 * Jan Kara, <jack@suse.cz>, 10/2002
50 * Added journalled quota support, fix lock inversion problems
51 * Jan Kara, <jack@suse.cz>, 2003,2004
53 * (C) Copyright 1994 - 1997 Marco van Wieringen
56 #include <linux/errno.h>
57 #include <linux/kernel.h>
59 #include <linux/mount.h>
61 #include <linux/time.h>
62 #include <linux/types.h>
63 #include <linux/string.h>
64 #include <linux/fcntl.h>
65 #include <linux/stat.h>
66 #include <linux/tty.h>
67 #include <linux/file.h>
68 #include <linux/slab.h>
69 #include <linux/sysctl.h>
70 #include <linux/init.h>
71 #include <linux/module.h>
72 #include <linux/proc_fs.h>
73 #include <linux/security.h>
74 #include <linux/kmod.h>
75 #include <linux/namei.h>
76 #include <linux/buffer_head.h>
77 #include <linux/capability.h>
78 #include <linux/quotaops.h>
79 #include <linux/writeback.h> /* for inode_lock, oddly enough.. */
81 #include <asm/uaccess.h>
83 #define __DQUOT_PARANOIA
86 * There are three quota SMP locks. dq_list_lock protects all lists with quotas
87 * and quota formats, dqstats structure containing statistics about the lists
88 * dq_data_lock protects data from dq_dqb and also mem_dqinfo structures and
89 * also guards consistency of dquot->dq_dqb with inode->i_blocks, i_bytes.
90 * i_blocks and i_bytes updates itself are guarded by i_lock acquired directly
91 * in inode_add_bytes() and inode_sub_bytes(). dq_state_lock protects
92 * modifications of quota state (on quotaon and quotaoff) and readers who care
93 * about latest values take it as well.
95 * The spinlock ordering is hence: dq_data_lock > dq_list_lock > i_lock,
96 * dq_list_lock > dq_state_lock
98 * Note that some things (eg. sb pointer, type, id) doesn't change during
99 * the life of the dquot structure and so needn't to be protected by a lock
101 * Any operation working on dquots via inode pointers must hold dqptr_sem. If
102 * operation is just reading pointers from inode (or not using them at all) the
103 * read lock is enough. If pointers are altered function must hold write lock
104 * (these locking rules also apply for S_NOQUOTA flag in the inode - note that
105 * for altering the flag i_mutex is also needed).
107 * Each dquot has its dq_lock mutex. Locked dquots might not be referenced
108 * from inodes (dquot_alloc_space() and such don't check the dq_lock).
109 * Currently dquot is locked only when it is being read to memory (or space for
110 * it is being allocated) on the first dqget() and when it is being released on
111 * the last dqput(). The allocation and release oparations are serialized by
112 * the dq_lock and by checking the use count in dquot_release(). Write
113 * operations on dquots don't hold dq_lock as they copy data under dq_data_lock
114 * spinlock to internal buffers before writing.
116 * Lock ordering (including related VFS locks) is the following:
117 * i_mutex > dqonoff_sem > journal_lock > dqptr_sem > dquot->dq_lock >
119 * The lock ordering of dqptr_sem imposed by quota code is only dqonoff_sem >
120 * dqptr_sem. But filesystem has to count with the fact that functions such as
121 * dquot_alloc_space() acquire dqptr_sem and they usually have to be called
122 * from inside a transaction to keep filesystem consistency after a crash. Also
123 * filesystems usually want to do some IO on dquot from ->mark_dirty which is
124 * called with dqptr_sem held.
125 * i_mutex on quota files is special (it's below dqio_mutex)
128 static __cacheline_aligned_in_smp
DEFINE_SPINLOCK(dq_list_lock
);
129 static __cacheline_aligned_in_smp
DEFINE_SPINLOCK(dq_state_lock
);
130 __cacheline_aligned_in_smp
DEFINE_SPINLOCK(dq_data_lock
);
131 EXPORT_SYMBOL(dq_data_lock
);
133 static char *quotatypes
[] = INITQFNAMES
;
134 static struct quota_format_type
*quota_formats
; /* List of registered formats */
135 static struct quota_module_name module_names
[] = INIT_QUOTA_MODULE_NAMES
;
137 /* SLAB cache for dquot structures */
138 static struct kmem_cache
*dquot_cachep
;
140 int register_quota_format(struct quota_format_type
*fmt
)
142 spin_lock(&dq_list_lock
);
143 fmt
->qf_next
= quota_formats
;
145 spin_unlock(&dq_list_lock
);
148 EXPORT_SYMBOL(register_quota_format
);
150 void unregister_quota_format(struct quota_format_type
*fmt
)
152 struct quota_format_type
**actqf
;
154 spin_lock(&dq_list_lock
);
155 for (actqf
= "a_formats
; *actqf
&& *actqf
!= fmt
;
156 actqf
= &(*actqf
)->qf_next
)
159 *actqf
= (*actqf
)->qf_next
;
160 spin_unlock(&dq_list_lock
);
162 EXPORT_SYMBOL(unregister_quota_format
);
164 static struct quota_format_type
*find_quota_format(int id
)
166 struct quota_format_type
*actqf
;
168 spin_lock(&dq_list_lock
);
169 for (actqf
= quota_formats
; actqf
&& actqf
->qf_fmt_id
!= id
;
170 actqf
= actqf
->qf_next
)
172 if (!actqf
|| !try_module_get(actqf
->qf_owner
)) {
175 spin_unlock(&dq_list_lock
);
177 for (qm
= 0; module_names
[qm
].qm_fmt_id
&&
178 module_names
[qm
].qm_fmt_id
!= id
; qm
++)
180 if (!module_names
[qm
].qm_fmt_id
||
181 request_module(module_names
[qm
].qm_mod_name
))
184 spin_lock(&dq_list_lock
);
185 for (actqf
= quota_formats
; actqf
&& actqf
->qf_fmt_id
!= id
;
186 actqf
= actqf
->qf_next
)
188 if (actqf
&& !try_module_get(actqf
->qf_owner
))
191 spin_unlock(&dq_list_lock
);
195 static void put_quota_format(struct quota_format_type
*fmt
)
197 module_put(fmt
->qf_owner
);
201 * Dquot List Management:
202 * The quota code uses three lists for dquot management: the inuse_list,
203 * free_dquots, and dquot_hash[] array. A single dquot structure may be
204 * on all three lists, depending on its current state.
206 * All dquots are placed to the end of inuse_list when first created, and this
207 * list is used for invalidate operation, which must look at every dquot.
209 * Unused dquots (dq_count == 0) are added to the free_dquots list when freed,
210 * and this list is searched whenever we need an available dquot. Dquots are
211 * removed from the list as soon as they are used again, and
212 * dqstats.free_dquots gives the number of dquots on the list. When
213 * dquot is invalidated it's completely released from memory.
215 * Dquots with a specific identity (device, type and id) are placed on
216 * one of the dquot_hash[] hash chains. The provides an efficient search
217 * mechanism to locate a specific dquot.
220 static LIST_HEAD(inuse_list
);
221 static LIST_HEAD(free_dquots
);
222 static unsigned int dq_hash_bits
, dq_hash_mask
;
223 static struct hlist_head
*dquot_hash
;
225 struct dqstats dqstats
;
226 EXPORT_SYMBOL(dqstats
);
228 static inline unsigned int
229 hashfn(const struct super_block
*sb
, unsigned int id
, int type
)
233 tmp
= (((unsigned long)sb
>>L1_CACHE_SHIFT
) ^ id
) * (MAXQUOTAS
- type
);
234 return (tmp
+ (tmp
>> dq_hash_bits
)) & dq_hash_mask
;
238 * Following list functions expect dq_list_lock to be held
240 static inline void insert_dquot_hash(struct dquot
*dquot
)
242 struct hlist_head
*head
;
243 head
= dquot_hash
+ hashfn(dquot
->dq_sb
, dquot
->dq_id
, dquot
->dq_type
);
244 hlist_add_head(&dquot
->dq_hash
, head
);
247 static inline void remove_dquot_hash(struct dquot
*dquot
)
249 hlist_del_init(&dquot
->dq_hash
);
252 static struct dquot
*find_dquot(unsigned int hashent
, struct super_block
*sb
,
253 unsigned int id
, int type
)
255 struct hlist_node
*node
;
258 hlist_for_each (node
, dquot_hash
+hashent
) {
259 dquot
= hlist_entry(node
, struct dquot
, dq_hash
);
260 if (dquot
->dq_sb
== sb
&& dquot
->dq_id
== id
&&
261 dquot
->dq_type
== type
)
267 /* Add a dquot to the tail of the free list */
268 static inline void put_dquot_last(struct dquot
*dquot
)
270 list_add_tail(&dquot
->dq_free
, &free_dquots
);
271 dqstats
.free_dquots
++;
274 static inline void remove_free_dquot(struct dquot
*dquot
)
276 if (list_empty(&dquot
->dq_free
))
278 list_del_init(&dquot
->dq_free
);
279 dqstats
.free_dquots
--;
282 static inline void put_inuse(struct dquot
*dquot
)
284 /* We add to the back of inuse list so we don't have to restart
285 * when traversing this list and we block */
286 list_add_tail(&dquot
->dq_inuse
, &inuse_list
);
287 dqstats
.allocated_dquots
++;
290 static inline void remove_inuse(struct dquot
*dquot
)
292 dqstats
.allocated_dquots
--;
293 list_del(&dquot
->dq_inuse
);
296 * End of list functions needing dq_list_lock
299 static void wait_on_dquot(struct dquot
*dquot
)
301 mutex_lock(&dquot
->dq_lock
);
302 mutex_unlock(&dquot
->dq_lock
);
305 static inline int dquot_dirty(struct dquot
*dquot
)
307 return test_bit(DQ_MOD_B
, &dquot
->dq_flags
);
310 static inline int mark_dquot_dirty(struct dquot
*dquot
)
312 return dquot
->dq_sb
->dq_op
->mark_dirty(dquot
);
315 int dquot_mark_dquot_dirty(struct dquot
*dquot
)
317 spin_lock(&dq_list_lock
);
318 if (!test_and_set_bit(DQ_MOD_B
, &dquot
->dq_flags
))
319 list_add(&dquot
->dq_dirty
, &sb_dqopt(dquot
->dq_sb
)->
320 info
[dquot
->dq_type
].dqi_dirty_list
);
321 spin_unlock(&dq_list_lock
);
324 EXPORT_SYMBOL(dquot_mark_dquot_dirty
);
326 /* Dirtify all the dquots - this can block when journalling */
327 static inline int mark_all_dquot_dirty(struct dquot
* const *dquot
)
332 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++) {
334 /* Even in case of error we have to continue */
335 ret
= mark_dquot_dirty(dquot
[cnt
]);
342 static inline void dqput_all(struct dquot
**dquot
)
346 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++)
350 /* This function needs dq_list_lock */
351 static inline int clear_dquot_dirty(struct dquot
*dquot
)
353 if (!test_and_clear_bit(DQ_MOD_B
, &dquot
->dq_flags
))
355 list_del_init(&dquot
->dq_dirty
);
359 void mark_info_dirty(struct super_block
*sb
, int type
)
361 set_bit(DQF_INFO_DIRTY_B
, &sb_dqopt(sb
)->info
[type
].dqi_flags
);
363 EXPORT_SYMBOL(mark_info_dirty
);
366 * Read dquot from disk and alloc space for it
369 int dquot_acquire(struct dquot
*dquot
)
371 int ret
= 0, ret2
= 0;
372 struct quota_info
*dqopt
= sb_dqopt(dquot
->dq_sb
);
374 mutex_lock(&dquot
->dq_lock
);
375 mutex_lock(&dqopt
->dqio_mutex
);
376 if (!test_bit(DQ_READ_B
, &dquot
->dq_flags
))
377 ret
= dqopt
->ops
[dquot
->dq_type
]->read_dqblk(dquot
);
380 set_bit(DQ_READ_B
, &dquot
->dq_flags
);
381 /* Instantiate dquot if needed */
382 if (!test_bit(DQ_ACTIVE_B
, &dquot
->dq_flags
) && !dquot
->dq_off
) {
383 ret
= dqopt
->ops
[dquot
->dq_type
]->commit_dqblk(dquot
);
384 /* Write the info if needed */
385 if (info_dirty(&dqopt
->info
[dquot
->dq_type
])) {
386 ret2
= dqopt
->ops
[dquot
->dq_type
]->write_file_info(
387 dquot
->dq_sb
, dquot
->dq_type
);
396 set_bit(DQ_ACTIVE_B
, &dquot
->dq_flags
);
398 mutex_unlock(&dqopt
->dqio_mutex
);
399 mutex_unlock(&dquot
->dq_lock
);
402 EXPORT_SYMBOL(dquot_acquire
);
405 * Write dquot to disk
407 int dquot_commit(struct dquot
*dquot
)
409 int ret
= 0, ret2
= 0;
410 struct quota_info
*dqopt
= sb_dqopt(dquot
->dq_sb
);
412 mutex_lock(&dqopt
->dqio_mutex
);
413 spin_lock(&dq_list_lock
);
414 if (!clear_dquot_dirty(dquot
)) {
415 spin_unlock(&dq_list_lock
);
418 spin_unlock(&dq_list_lock
);
419 /* Inactive dquot can be only if there was error during read/init
420 * => we have better not writing it */
421 if (test_bit(DQ_ACTIVE_B
, &dquot
->dq_flags
)) {
422 ret
= dqopt
->ops
[dquot
->dq_type
]->commit_dqblk(dquot
);
423 if (info_dirty(&dqopt
->info
[dquot
->dq_type
])) {
424 ret2
= dqopt
->ops
[dquot
->dq_type
]->write_file_info(
425 dquot
->dq_sb
, dquot
->dq_type
);
431 mutex_unlock(&dqopt
->dqio_mutex
);
434 EXPORT_SYMBOL(dquot_commit
);
439 int dquot_release(struct dquot
*dquot
)
441 int ret
= 0, ret2
= 0;
442 struct quota_info
*dqopt
= sb_dqopt(dquot
->dq_sb
);
444 mutex_lock(&dquot
->dq_lock
);
445 /* Check whether we are not racing with some other dqget() */
446 if (atomic_read(&dquot
->dq_count
) > 1)
448 mutex_lock(&dqopt
->dqio_mutex
);
449 if (dqopt
->ops
[dquot
->dq_type
]->release_dqblk
) {
450 ret
= dqopt
->ops
[dquot
->dq_type
]->release_dqblk(dquot
);
452 if (info_dirty(&dqopt
->info
[dquot
->dq_type
])) {
453 ret2
= dqopt
->ops
[dquot
->dq_type
]->write_file_info(
454 dquot
->dq_sb
, dquot
->dq_type
);
459 clear_bit(DQ_ACTIVE_B
, &dquot
->dq_flags
);
460 mutex_unlock(&dqopt
->dqio_mutex
);
462 mutex_unlock(&dquot
->dq_lock
);
465 EXPORT_SYMBOL(dquot_release
);
467 void dquot_destroy(struct dquot
*dquot
)
469 kmem_cache_free(dquot_cachep
, dquot
);
471 EXPORT_SYMBOL(dquot_destroy
);
473 static inline void do_destroy_dquot(struct dquot
*dquot
)
475 dquot
->dq_sb
->dq_op
->destroy_dquot(dquot
);
478 /* Invalidate all dquots on the list. Note that this function is called after
479 * quota is disabled and pointers from inodes removed so there cannot be new
480 * quota users. There can still be some users of quotas due to inodes being
481 * just deleted or pruned by prune_icache() (those are not attached to any
482 * list) or parallel quotactl call. We have to wait for such users.
484 static void invalidate_dquots(struct super_block
*sb
, int type
)
486 struct dquot
*dquot
, *tmp
;
489 spin_lock(&dq_list_lock
);
490 list_for_each_entry_safe(dquot
, tmp
, &inuse_list
, dq_inuse
) {
491 if (dquot
->dq_sb
!= sb
)
493 if (dquot
->dq_type
!= type
)
495 /* Wait for dquot users */
496 if (atomic_read(&dquot
->dq_count
)) {
499 atomic_inc(&dquot
->dq_count
);
500 prepare_to_wait(&dquot
->dq_wait_unused
, &wait
,
501 TASK_UNINTERRUPTIBLE
);
502 spin_unlock(&dq_list_lock
);
503 /* Once dqput() wakes us up, we know it's time to free
505 * IMPORTANT: we rely on the fact that there is always
506 * at most one process waiting for dquot to free.
507 * Otherwise dq_count would be > 1 and we would never
510 if (atomic_read(&dquot
->dq_count
) > 1)
512 finish_wait(&dquot
->dq_wait_unused
, &wait
);
514 /* At this moment dquot() need not exist (it could be
515 * reclaimed by prune_dqcache(). Hence we must
520 * Quota now has no users and it has been written on last
523 remove_dquot_hash(dquot
);
524 remove_free_dquot(dquot
);
526 do_destroy_dquot(dquot
);
528 spin_unlock(&dq_list_lock
);
531 /* Call callback for every active dquot on given filesystem */
532 int dquot_scan_active(struct super_block
*sb
,
533 int (*fn
)(struct dquot
*dquot
, unsigned long priv
),
536 struct dquot
*dquot
, *old_dquot
= NULL
;
539 mutex_lock(&sb_dqopt(sb
)->dqonoff_mutex
);
540 spin_lock(&dq_list_lock
);
541 list_for_each_entry(dquot
, &inuse_list
, dq_inuse
) {
542 if (!test_bit(DQ_ACTIVE_B
, &dquot
->dq_flags
))
544 if (dquot
->dq_sb
!= sb
)
546 /* Now we have active dquot so we can just increase use count */
547 atomic_inc(&dquot
->dq_count
);
549 spin_unlock(&dq_list_lock
);
552 ret
= fn(dquot
, priv
);
555 spin_lock(&dq_list_lock
);
556 /* We are safe to continue now because our dquot could not
557 * be moved out of the inuse list while we hold the reference */
559 spin_unlock(&dq_list_lock
);
562 mutex_unlock(&sb_dqopt(sb
)->dqonoff_mutex
);
565 EXPORT_SYMBOL(dquot_scan_active
);
567 int vfs_quota_sync(struct super_block
*sb
, int type
)
569 struct list_head
*dirty
;
571 struct quota_info
*dqopt
= sb_dqopt(sb
);
574 mutex_lock(&dqopt
->dqonoff_mutex
);
575 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++) {
576 if (type
!= -1 && cnt
!= type
)
578 if (!sb_has_quota_active(sb
, cnt
))
580 spin_lock(&dq_list_lock
);
581 dirty
= &dqopt
->info
[cnt
].dqi_dirty_list
;
582 while (!list_empty(dirty
)) {
583 dquot
= list_first_entry(dirty
, struct dquot
,
585 /* Dirty and inactive can be only bad dquot... */
586 if (!test_bit(DQ_ACTIVE_B
, &dquot
->dq_flags
)) {
587 clear_dquot_dirty(dquot
);
590 /* Now we have active dquot from which someone is
591 * holding reference so we can safely just increase
593 atomic_inc(&dquot
->dq_count
);
595 spin_unlock(&dq_list_lock
);
596 sb
->dq_op
->write_dquot(dquot
);
598 spin_lock(&dq_list_lock
);
600 spin_unlock(&dq_list_lock
);
603 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++)
604 if ((cnt
== type
|| type
== -1) && sb_has_quota_active(sb
, cnt
)
605 && info_dirty(&dqopt
->info
[cnt
]))
606 sb
->dq_op
->write_info(sb
, cnt
);
607 spin_lock(&dq_list_lock
);
609 spin_unlock(&dq_list_lock
);
610 mutex_unlock(&dqopt
->dqonoff_mutex
);
614 EXPORT_SYMBOL(vfs_quota_sync
);
616 /* Free unused dquots from cache */
617 static void prune_dqcache(int count
)
619 struct list_head
*head
;
622 head
= free_dquots
.prev
;
623 while (head
!= &free_dquots
&& count
) {
624 dquot
= list_entry(head
, struct dquot
, dq_free
);
625 remove_dquot_hash(dquot
);
626 remove_free_dquot(dquot
);
628 do_destroy_dquot(dquot
);
630 head
= free_dquots
.prev
;
635 * This is called from kswapd when we think we need some
639 static int shrink_dqcache_memory(int nr
, gfp_t gfp_mask
)
642 spin_lock(&dq_list_lock
);
644 spin_unlock(&dq_list_lock
);
646 return (dqstats
.free_dquots
/ 100) * sysctl_vfs_cache_pressure
;
649 static struct shrinker dqcache_shrinker
= {
650 .shrink
= shrink_dqcache_memory
,
651 .seeks
= DEFAULT_SEEKS
,
655 * Put reference to dquot
656 * NOTE: If you change this function please check whether dqput_blocks() works right...
658 void dqput(struct dquot
*dquot
)
664 #ifdef __DQUOT_PARANOIA
665 if (!atomic_read(&dquot
->dq_count
)) {
666 printk("VFS: dqput: trying to free free dquot\n");
667 printk("VFS: device %s, dquot of %s %d\n",
669 quotatypes
[dquot
->dq_type
],
675 spin_lock(&dq_list_lock
);
677 spin_unlock(&dq_list_lock
);
679 spin_lock(&dq_list_lock
);
680 if (atomic_read(&dquot
->dq_count
) > 1) {
681 /* We have more than one user... nothing to do */
682 atomic_dec(&dquot
->dq_count
);
683 /* Releasing dquot during quotaoff phase? */
684 if (!sb_has_quota_active(dquot
->dq_sb
, dquot
->dq_type
) &&
685 atomic_read(&dquot
->dq_count
) == 1)
686 wake_up(&dquot
->dq_wait_unused
);
687 spin_unlock(&dq_list_lock
);
690 /* Need to release dquot? */
691 if (test_bit(DQ_ACTIVE_B
, &dquot
->dq_flags
) && dquot_dirty(dquot
)) {
692 spin_unlock(&dq_list_lock
);
693 /* Commit dquot before releasing */
694 ret
= dquot
->dq_sb
->dq_op
->write_dquot(dquot
);
696 printk(KERN_ERR
"VFS: cannot write quota structure on "
697 "device %s (error %d). Quota may get out of "
698 "sync!\n", dquot
->dq_sb
->s_id
, ret
);
700 * We clear dirty bit anyway, so that we avoid
703 spin_lock(&dq_list_lock
);
704 clear_dquot_dirty(dquot
);
705 spin_unlock(&dq_list_lock
);
709 /* Clear flag in case dquot was inactive (something bad happened) */
710 clear_dquot_dirty(dquot
);
711 if (test_bit(DQ_ACTIVE_B
, &dquot
->dq_flags
)) {
712 spin_unlock(&dq_list_lock
);
713 dquot
->dq_sb
->dq_op
->release_dquot(dquot
);
716 atomic_dec(&dquot
->dq_count
);
717 #ifdef __DQUOT_PARANOIA
719 BUG_ON(!list_empty(&dquot
->dq_free
));
721 put_dquot_last(dquot
);
722 spin_unlock(&dq_list_lock
);
724 EXPORT_SYMBOL(dqput
);
726 struct dquot
*dquot_alloc(struct super_block
*sb
, int type
)
728 return kmem_cache_zalloc(dquot_cachep
, GFP_NOFS
);
730 EXPORT_SYMBOL(dquot_alloc
);
732 static struct dquot
*get_empty_dquot(struct super_block
*sb
, int type
)
736 dquot
= sb
->dq_op
->alloc_dquot(sb
, type
);
740 mutex_init(&dquot
->dq_lock
);
741 INIT_LIST_HEAD(&dquot
->dq_free
);
742 INIT_LIST_HEAD(&dquot
->dq_inuse
);
743 INIT_HLIST_NODE(&dquot
->dq_hash
);
744 INIT_LIST_HEAD(&dquot
->dq_dirty
);
745 init_waitqueue_head(&dquot
->dq_wait_unused
);
747 dquot
->dq_type
= type
;
748 atomic_set(&dquot
->dq_count
, 1);
754 * Get reference to dquot
756 * Locking is slightly tricky here. We are guarded from parallel quotaoff()
757 * destroying our dquot by:
758 * a) checking for quota flags under dq_list_lock and
759 * b) getting a reference to dquot before we release dq_list_lock
761 struct dquot
*dqget(struct super_block
*sb
, unsigned int id
, int type
)
763 unsigned int hashent
= hashfn(sb
, id
, type
);
764 struct dquot
*dquot
= NULL
, *empty
= NULL
;
766 if (!sb_has_quota_active(sb
, type
))
769 spin_lock(&dq_list_lock
);
770 spin_lock(&dq_state_lock
);
771 if (!sb_has_quota_active(sb
, type
)) {
772 spin_unlock(&dq_state_lock
);
773 spin_unlock(&dq_list_lock
);
776 spin_unlock(&dq_state_lock
);
778 dquot
= find_dquot(hashent
, sb
, id
, type
);
781 spin_unlock(&dq_list_lock
);
782 empty
= get_empty_dquot(sb
, type
);
784 schedule(); /* Try to wait for a moment... */
790 /* all dquots go on the inuse_list */
792 /* hash it first so it can be found */
793 insert_dquot_hash(dquot
);
795 spin_unlock(&dq_list_lock
);
797 if (!atomic_read(&dquot
->dq_count
))
798 remove_free_dquot(dquot
);
799 atomic_inc(&dquot
->dq_count
);
800 dqstats
.cache_hits
++;
802 spin_unlock(&dq_list_lock
);
804 /* Wait for dq_lock - after this we know that either dquot_release() is
805 * already finished or it will be canceled due to dq_count > 1 test */
806 wait_on_dquot(dquot
);
807 /* Read the dquot / allocate space in quota file */
808 if (!test_bit(DQ_ACTIVE_B
, &dquot
->dq_flags
) &&
809 sb
->dq_op
->acquire_dquot(dquot
) < 0) {
814 #ifdef __DQUOT_PARANOIA
815 BUG_ON(!dquot
->dq_sb
); /* Has somebody invalidated entry under us? */
819 do_destroy_dquot(empty
);
823 EXPORT_SYMBOL(dqget
);
825 static int dqinit_needed(struct inode
*inode
, int type
)
829 if (IS_NOQUOTA(inode
))
832 return !inode
->i_dquot
[type
];
833 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++)
834 if (!inode
->i_dquot
[cnt
])
839 /* This routine is guarded by dqonoff_mutex mutex */
840 static void add_dquot_ref(struct super_block
*sb
, int type
)
842 struct inode
*inode
, *old_inode
= NULL
;
844 spin_lock(&inode_lock
);
845 list_for_each_entry(inode
, &sb
->s_inodes
, i_sb_list
) {
846 if (inode
->i_state
& (I_FREEING
|I_CLEAR
|I_WILL_FREE
|I_NEW
))
848 if (!atomic_read(&inode
->i_writecount
))
850 if (!dqinit_needed(inode
, type
))
854 spin_unlock(&inode_lock
);
857 sb
->dq_op
->initialize(inode
, type
);
858 /* We hold a reference to 'inode' so it couldn't have been
859 * removed from s_inodes list while we dropped the inode_lock.
860 * We cannot iput the inode now as we can be holding the last
861 * reference and we cannot iput it under inode_lock. So we
862 * keep the reference and iput it later. */
864 spin_lock(&inode_lock
);
866 spin_unlock(&inode_lock
);
871 * Return 0 if dqput() won't block.
872 * (note that 1 doesn't necessarily mean blocking)
874 static inline int dqput_blocks(struct dquot
*dquot
)
876 if (atomic_read(&dquot
->dq_count
) <= 1)
882 * Remove references to dquots from inode and add dquot to list for freeing
883 * if we have the last referece to dquot
884 * We can't race with anybody because we hold dqptr_sem for writing...
886 static int remove_inode_dquot_ref(struct inode
*inode
, int type
,
887 struct list_head
*tofree_head
)
889 struct dquot
*dquot
= inode
->i_dquot
[type
];
891 inode
->i_dquot
[type
] = NULL
;
893 if (dqput_blocks(dquot
)) {
894 #ifdef __DQUOT_PARANOIA
895 if (atomic_read(&dquot
->dq_count
) != 1)
896 printk(KERN_WARNING
"VFS: Adding dquot with dq_count %d to dispose list.\n", atomic_read(&dquot
->dq_count
));
898 spin_lock(&dq_list_lock
);
899 /* As dquot must have currently users it can't be on
900 * the free list... */
901 list_add(&dquot
->dq_free
, tofree_head
);
902 spin_unlock(&dq_list_lock
);
906 dqput(dquot
); /* We have guaranteed we won't block */
912 * Free list of dquots
913 * Dquots are removed from inodes and no new references can be got so we are
914 * the only ones holding reference
916 static void put_dquot_list(struct list_head
*tofree_head
)
918 struct list_head
*act_head
;
921 act_head
= tofree_head
->next
;
922 while (act_head
!= tofree_head
) {
923 dquot
= list_entry(act_head
, struct dquot
, dq_free
);
924 act_head
= act_head
->next
;
925 /* Remove dquot from the list so we won't have problems... */
926 list_del_init(&dquot
->dq_free
);
931 static void remove_dquot_ref(struct super_block
*sb
, int type
,
932 struct list_head
*tofree_head
)
936 spin_lock(&inode_lock
);
937 list_for_each_entry(inode
, &sb
->s_inodes
, i_sb_list
) {
939 * We have to scan also I_NEW inodes because they can already
940 * have quota pointer initialized. Luckily, we need to touch
941 * only quota pointers and these have separate locking
944 if (!IS_NOQUOTA(inode
))
945 remove_inode_dquot_ref(inode
, type
, tofree_head
);
947 spin_unlock(&inode_lock
);
950 /* Gather all references from inodes and drop them */
951 static void drop_dquot_ref(struct super_block
*sb
, int type
)
953 LIST_HEAD(tofree_head
);
956 down_write(&sb_dqopt(sb
)->dqptr_sem
);
957 remove_dquot_ref(sb
, type
, &tofree_head
);
958 up_write(&sb_dqopt(sb
)->dqptr_sem
);
959 put_dquot_list(&tofree_head
);
963 static inline void dquot_incr_inodes(struct dquot
*dquot
, qsize_t number
)
965 dquot
->dq_dqb
.dqb_curinodes
+= number
;
968 static inline void dquot_incr_space(struct dquot
*dquot
, qsize_t number
)
970 dquot
->dq_dqb
.dqb_curspace
+= number
;
973 static inline void dquot_resv_space(struct dquot
*dquot
, qsize_t number
)
975 dquot
->dq_dqb
.dqb_rsvspace
+= number
;
979 * Claim reserved quota space
981 static void dquot_claim_reserved_space(struct dquot
*dquot
,
984 WARN_ON(dquot
->dq_dqb
.dqb_rsvspace
< number
);
985 dquot
->dq_dqb
.dqb_curspace
+= number
;
986 dquot
->dq_dqb
.dqb_rsvspace
-= number
;
990 void dquot_free_reserved_space(struct dquot
*dquot
, qsize_t number
)
992 dquot
->dq_dqb
.dqb_rsvspace
-= number
;
995 static void dquot_decr_inodes(struct dquot
*dquot
, qsize_t number
)
997 if (sb_dqopt(dquot
->dq_sb
)->flags
& DQUOT_NEGATIVE_USAGE
||
998 dquot
->dq_dqb
.dqb_curinodes
>= number
)
999 dquot
->dq_dqb
.dqb_curinodes
-= number
;
1001 dquot
->dq_dqb
.dqb_curinodes
= 0;
1002 if (dquot
->dq_dqb
.dqb_curinodes
<= dquot
->dq_dqb
.dqb_isoftlimit
)
1003 dquot
->dq_dqb
.dqb_itime
= (time_t) 0;
1004 clear_bit(DQ_INODES_B
, &dquot
->dq_flags
);
1007 static void dquot_decr_space(struct dquot
*dquot
, qsize_t number
)
1009 if (sb_dqopt(dquot
->dq_sb
)->flags
& DQUOT_NEGATIVE_USAGE
||
1010 dquot
->dq_dqb
.dqb_curspace
>= number
)
1011 dquot
->dq_dqb
.dqb_curspace
-= number
;
1013 dquot
->dq_dqb
.dqb_curspace
= 0;
1014 if (dquot
->dq_dqb
.dqb_curspace
<= dquot
->dq_dqb
.dqb_bsoftlimit
)
1015 dquot
->dq_dqb
.dqb_btime
= (time_t) 0;
1016 clear_bit(DQ_BLKS_B
, &dquot
->dq_flags
);
1019 static int warning_issued(struct dquot
*dquot
, const int warntype
)
1021 int flag
= (warntype
== QUOTA_NL_BHARDWARN
||
1022 warntype
== QUOTA_NL_BSOFTLONGWARN
) ? DQ_BLKS_B
:
1023 ((warntype
== QUOTA_NL_IHARDWARN
||
1024 warntype
== QUOTA_NL_ISOFTLONGWARN
) ? DQ_INODES_B
: 0);
1028 return test_and_set_bit(flag
, &dquot
->dq_flags
);
1031 #ifdef CONFIG_PRINT_QUOTA_WARNING
1032 static int flag_print_warnings
= 1;
1034 static int need_print_warning(struct dquot
*dquot
)
1036 if (!flag_print_warnings
)
1039 switch (dquot
->dq_type
) {
1041 return current_fsuid() == dquot
->dq_id
;
1043 return in_group_p(dquot
->dq_id
);
1048 /* Print warning to user which exceeded quota */
1049 static void print_warning(struct dquot
*dquot
, const int warntype
)
1052 struct tty_struct
*tty
;
1054 if (warntype
== QUOTA_NL_IHARDBELOW
||
1055 warntype
== QUOTA_NL_ISOFTBELOW
||
1056 warntype
== QUOTA_NL_BHARDBELOW
||
1057 warntype
== QUOTA_NL_BSOFTBELOW
|| !need_print_warning(dquot
))
1060 tty
= get_current_tty();
1063 tty_write_message(tty
, dquot
->dq_sb
->s_id
);
1064 if (warntype
== QUOTA_NL_ISOFTWARN
|| warntype
== QUOTA_NL_BSOFTWARN
)
1065 tty_write_message(tty
, ": warning, ");
1067 tty_write_message(tty
, ": write failed, ");
1068 tty_write_message(tty
, quotatypes
[dquot
->dq_type
]);
1070 case QUOTA_NL_IHARDWARN
:
1071 msg
= " file limit reached.\r\n";
1073 case QUOTA_NL_ISOFTLONGWARN
:
1074 msg
= " file quota exceeded too long.\r\n";
1076 case QUOTA_NL_ISOFTWARN
:
1077 msg
= " file quota exceeded.\r\n";
1079 case QUOTA_NL_BHARDWARN
:
1080 msg
= " block limit reached.\r\n";
1082 case QUOTA_NL_BSOFTLONGWARN
:
1083 msg
= " block quota exceeded too long.\r\n";
1085 case QUOTA_NL_BSOFTWARN
:
1086 msg
= " block quota exceeded.\r\n";
1089 tty_write_message(tty
, msg
);
1095 * Write warnings to the console and send warning messages over netlink.
1097 * Note that this function can sleep.
1099 static void flush_warnings(struct dquot
*const *dquots
, char *warntype
)
1104 for (i
= 0; i
< MAXQUOTAS
; i
++) {
1106 if (dq
&& warntype
[i
] != QUOTA_NL_NOWARN
&&
1107 !warning_issued(dq
, warntype
[i
])) {
1108 #ifdef CONFIG_PRINT_QUOTA_WARNING
1109 print_warning(dq
, warntype
[i
]);
1111 quota_send_warning(dq
->dq_type
, dq
->dq_id
,
1112 dq
->dq_sb
->s_dev
, warntype
[i
]);
1117 static int ignore_hardlimit(struct dquot
*dquot
)
1119 struct mem_dqinfo
*info
= &sb_dqopt(dquot
->dq_sb
)->info
[dquot
->dq_type
];
1121 return capable(CAP_SYS_RESOURCE
) &&
1122 (info
->dqi_format
->qf_fmt_id
!= QFMT_VFS_OLD
||
1123 !(info
->dqi_flags
& V1_DQF_RSQUASH
));
1126 /* needs dq_data_lock */
1127 static int check_idq(struct dquot
*dquot
, qsize_t inodes
, char *warntype
)
1129 qsize_t newinodes
= dquot
->dq_dqb
.dqb_curinodes
+ inodes
;
1131 *warntype
= QUOTA_NL_NOWARN
;
1132 if (!sb_has_quota_limits_enabled(dquot
->dq_sb
, dquot
->dq_type
) ||
1133 test_bit(DQ_FAKE_B
, &dquot
->dq_flags
))
1136 if (dquot
->dq_dqb
.dqb_ihardlimit
&&
1137 newinodes
> dquot
->dq_dqb
.dqb_ihardlimit
&&
1138 !ignore_hardlimit(dquot
)) {
1139 *warntype
= QUOTA_NL_IHARDWARN
;
1143 if (dquot
->dq_dqb
.dqb_isoftlimit
&&
1144 newinodes
> dquot
->dq_dqb
.dqb_isoftlimit
&&
1145 dquot
->dq_dqb
.dqb_itime
&&
1146 get_seconds() >= dquot
->dq_dqb
.dqb_itime
&&
1147 !ignore_hardlimit(dquot
)) {
1148 *warntype
= QUOTA_NL_ISOFTLONGWARN
;
1152 if (dquot
->dq_dqb
.dqb_isoftlimit
&&
1153 newinodes
> dquot
->dq_dqb
.dqb_isoftlimit
&&
1154 dquot
->dq_dqb
.dqb_itime
== 0) {
1155 *warntype
= QUOTA_NL_ISOFTWARN
;
1156 dquot
->dq_dqb
.dqb_itime
= get_seconds() +
1157 sb_dqopt(dquot
->dq_sb
)->info
[dquot
->dq_type
].dqi_igrace
;
1163 /* needs dq_data_lock */
1164 static int check_bdq(struct dquot
*dquot
, qsize_t space
, int prealloc
, char *warntype
)
1167 struct super_block
*sb
= dquot
->dq_sb
;
1169 *warntype
= QUOTA_NL_NOWARN
;
1170 if (!sb_has_quota_limits_enabled(sb
, dquot
->dq_type
) ||
1171 test_bit(DQ_FAKE_B
, &dquot
->dq_flags
))
1174 tspace
= dquot
->dq_dqb
.dqb_curspace
+ dquot
->dq_dqb
.dqb_rsvspace
1177 if (dquot
->dq_dqb
.dqb_bhardlimit
&&
1178 tspace
> dquot
->dq_dqb
.dqb_bhardlimit
&&
1179 !ignore_hardlimit(dquot
)) {
1181 *warntype
= QUOTA_NL_BHARDWARN
;
1185 if (dquot
->dq_dqb
.dqb_bsoftlimit
&&
1186 tspace
> dquot
->dq_dqb
.dqb_bsoftlimit
&&
1187 dquot
->dq_dqb
.dqb_btime
&&
1188 get_seconds() >= dquot
->dq_dqb
.dqb_btime
&&
1189 !ignore_hardlimit(dquot
)) {
1191 *warntype
= QUOTA_NL_BSOFTLONGWARN
;
1195 if (dquot
->dq_dqb
.dqb_bsoftlimit
&&
1196 tspace
> dquot
->dq_dqb
.dqb_bsoftlimit
&&
1197 dquot
->dq_dqb
.dqb_btime
== 0) {
1199 *warntype
= QUOTA_NL_BSOFTWARN
;
1200 dquot
->dq_dqb
.dqb_btime
= get_seconds() +
1201 sb_dqopt(sb
)->info
[dquot
->dq_type
].dqi_bgrace
;
1205 * We don't allow preallocation to exceed softlimit so exceeding will
1214 static int info_idq_free(struct dquot
*dquot
, qsize_t inodes
)
1218 if (test_bit(DQ_FAKE_B
, &dquot
->dq_flags
) ||
1219 dquot
->dq_dqb
.dqb_curinodes
<= dquot
->dq_dqb
.dqb_isoftlimit
||
1220 !sb_has_quota_limits_enabled(dquot
->dq_sb
, dquot
->dq_type
))
1221 return QUOTA_NL_NOWARN
;
1223 newinodes
= dquot
->dq_dqb
.dqb_curinodes
- inodes
;
1224 if (newinodes
<= dquot
->dq_dqb
.dqb_isoftlimit
)
1225 return QUOTA_NL_ISOFTBELOW
;
1226 if (dquot
->dq_dqb
.dqb_curinodes
>= dquot
->dq_dqb
.dqb_ihardlimit
&&
1227 newinodes
< dquot
->dq_dqb
.dqb_ihardlimit
)
1228 return QUOTA_NL_IHARDBELOW
;
1229 return QUOTA_NL_NOWARN
;
1232 static int info_bdq_free(struct dquot
*dquot
, qsize_t space
)
1234 if (test_bit(DQ_FAKE_B
, &dquot
->dq_flags
) ||
1235 dquot
->dq_dqb
.dqb_curspace
<= dquot
->dq_dqb
.dqb_bsoftlimit
)
1236 return QUOTA_NL_NOWARN
;
1238 if (dquot
->dq_dqb
.dqb_curspace
- space
<= dquot
->dq_dqb
.dqb_bsoftlimit
)
1239 return QUOTA_NL_BSOFTBELOW
;
1240 if (dquot
->dq_dqb
.dqb_curspace
>= dquot
->dq_dqb
.dqb_bhardlimit
&&
1241 dquot
->dq_dqb
.dqb_curspace
- space
< dquot
->dq_dqb
.dqb_bhardlimit
)
1242 return QUOTA_NL_BHARDBELOW
;
1243 return QUOTA_NL_NOWARN
;
1246 * Initialize quota pointers in inode
1247 * We do things in a bit complicated way but by that we avoid calling
1248 * dqget() and thus filesystem callbacks under dqptr_sem.
1250 int dquot_initialize(struct inode
*inode
, int type
)
1252 unsigned int id
= 0;
1254 struct dquot
*got
[MAXQUOTAS
] = { NULL
, NULL
};
1255 struct super_block
*sb
= inode
->i_sb
;
1257 /* First test before acquiring mutex - solves deadlocks when we
1258 * re-enter the quota code and are already holding the mutex */
1259 if (IS_NOQUOTA(inode
))
1262 /* First get references to structures we might need. */
1263 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++) {
1264 if (type
!= -1 && cnt
!= type
)
1274 got
[cnt
] = dqget(sb
, id
, cnt
);
1277 down_write(&sb_dqopt(sb
)->dqptr_sem
);
1278 /* Having dqptr_sem we know NOQUOTA flags can't be altered... */
1279 if (IS_NOQUOTA(inode
))
1281 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++) {
1282 if (type
!= -1 && cnt
!= type
)
1284 /* Avoid races with quotaoff() */
1285 if (!sb_has_quota_active(sb
, cnt
))
1287 if (!inode
->i_dquot
[cnt
]) {
1288 inode
->i_dquot
[cnt
] = got
[cnt
];
1293 up_write(&sb_dqopt(sb
)->dqptr_sem
);
1294 /* Drop unused references */
1298 EXPORT_SYMBOL(dquot_initialize
);
1301 * Release all quotas referenced by inode
1303 int dquot_drop(struct inode
*inode
)
1306 struct dquot
*put
[MAXQUOTAS
];
1308 down_write(&sb_dqopt(inode
->i_sb
)->dqptr_sem
);
1309 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++) {
1310 put
[cnt
] = inode
->i_dquot
[cnt
];
1311 inode
->i_dquot
[cnt
] = NULL
;
1313 up_write(&sb_dqopt(inode
->i_sb
)->dqptr_sem
);
1317 EXPORT_SYMBOL(dquot_drop
);
1319 /* Wrapper to remove references to quota structures from inode */
1320 void vfs_dq_drop(struct inode
*inode
)
1322 /* Here we can get arbitrary inode from clear_inode() so we have
1323 * to be careful. OTOH we don't need locking as quota operations
1324 * are allowed to change only at mount time */
1325 if (!IS_NOQUOTA(inode
) && inode
->i_sb
&& inode
->i_sb
->dq_op
1326 && inode
->i_sb
->dq_op
->drop
) {
1328 /* Test before calling to rule out calls from proc and such
1329 * where we are not allowed to block. Note that this is
1330 * actually reliable test even without the lock - the caller
1331 * must assure that nobody can come after the DQUOT_DROP and
1332 * add quota pointers back anyway */
1333 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++)
1334 if (inode
->i_dquot
[cnt
])
1336 if (cnt
< MAXQUOTAS
)
1337 inode
->i_sb
->dq_op
->drop(inode
);
1340 EXPORT_SYMBOL(vfs_dq_drop
);
1343 * inode_reserved_space is managed internally by quota, and protected by
1344 * i_lock similar to i_blocks+i_bytes.
1346 static qsize_t
*inode_reserved_space(struct inode
* inode
)
1348 /* Filesystem must explicitly define it's own method in order to use
1349 * quota reservation interface */
1350 BUG_ON(!inode
->i_sb
->dq_op
->get_reserved_space
);
1351 return inode
->i_sb
->dq_op
->get_reserved_space(inode
);
1354 static void inode_add_rsv_space(struct inode
*inode
, qsize_t number
)
1356 spin_lock(&inode
->i_lock
);
1357 *inode_reserved_space(inode
) += number
;
1358 spin_unlock(&inode
->i_lock
);
1362 static void inode_claim_rsv_space(struct inode
*inode
, qsize_t number
)
1364 spin_lock(&inode
->i_lock
);
1365 *inode_reserved_space(inode
) -= number
;
1366 __inode_add_bytes(inode
, number
);
1367 spin_unlock(&inode
->i_lock
);
1370 static void inode_sub_rsv_space(struct inode
*inode
, qsize_t number
)
1372 spin_lock(&inode
->i_lock
);
1373 *inode_reserved_space(inode
) -= number
;
1374 spin_unlock(&inode
->i_lock
);
1377 static qsize_t
inode_get_rsv_space(struct inode
*inode
)
1380 spin_lock(&inode
->i_lock
);
1381 ret
= *inode_reserved_space(inode
);
1382 spin_unlock(&inode
->i_lock
);
1386 static void inode_incr_space(struct inode
*inode
, qsize_t number
,
1390 inode_add_rsv_space(inode
, number
);
1392 inode_add_bytes(inode
, number
);
1395 static void inode_decr_space(struct inode
*inode
, qsize_t number
, int reserve
)
1398 inode_sub_rsv_space(inode
, number
);
1400 inode_sub_bytes(inode
, number
);
1404 * Following four functions update i_blocks+i_bytes fields and
1405 * quota information (together with appropriate checks)
1406 * NOTE: We absolutely rely on the fact that caller dirties
1407 * the inode (usually macros in quotaops.h care about this) and
1408 * holds a handle for the current transaction so that dquot write and
1409 * inode write go into the same transaction.
1413 * This operation can block, but only after everything is updated
1415 int __dquot_alloc_space(struct inode
*inode
, qsize_t number
,
1416 int warn
, int reserve
)
1418 int cnt
, ret
= QUOTA_OK
;
1419 char warntype
[MAXQUOTAS
];
1422 * First test before acquiring mutex - solves deadlocks when we
1423 * re-enter the quota code and are already holding the mutex
1425 if (IS_NOQUOTA(inode
)) {
1426 inode_incr_space(inode
, number
, reserve
);
1430 down_read(&sb_dqopt(inode
->i_sb
)->dqptr_sem
);
1431 if (IS_NOQUOTA(inode
)) {
1432 inode_incr_space(inode
, number
, reserve
);
1436 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++)
1437 warntype
[cnt
] = QUOTA_NL_NOWARN
;
1439 spin_lock(&dq_data_lock
);
1440 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++) {
1441 if (!inode
->i_dquot
[cnt
])
1443 if (check_bdq(inode
->i_dquot
[cnt
], number
, warn
, warntype
+cnt
)
1446 spin_unlock(&dq_data_lock
);
1447 goto out_flush_warn
;
1450 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++) {
1451 if (!inode
->i_dquot
[cnt
])
1454 dquot_resv_space(inode
->i_dquot
[cnt
], number
);
1456 dquot_incr_space(inode
->i_dquot
[cnt
], number
);
1458 inode_incr_space(inode
, number
, reserve
);
1459 spin_unlock(&dq_data_lock
);
1462 goto out_flush_warn
;
1463 mark_all_dquot_dirty(inode
->i_dquot
);
1465 flush_warnings(inode
->i_dquot
, warntype
);
1467 up_read(&sb_dqopt(inode
->i_sb
)->dqptr_sem
);
1472 int dquot_alloc_space(struct inode
*inode
, qsize_t number
, int warn
)
1474 return __dquot_alloc_space(inode
, number
, warn
, 0);
1476 EXPORT_SYMBOL(dquot_alloc_space
);
1478 int dquot_reserve_space(struct inode
*inode
, qsize_t number
, int warn
)
1480 return __dquot_alloc_space(inode
, number
, warn
, 1);
1482 EXPORT_SYMBOL(dquot_reserve_space
);
1485 * This operation can block, but only after everything is updated
1487 int dquot_alloc_inode(const struct inode
*inode
, qsize_t number
)
1489 int cnt
, ret
= NO_QUOTA
;
1490 char warntype
[MAXQUOTAS
];
1492 /* First test before acquiring mutex - solves deadlocks when we
1493 * re-enter the quota code and are already holding the mutex */
1494 if (IS_NOQUOTA(inode
))
1496 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++)
1497 warntype
[cnt
] = QUOTA_NL_NOWARN
;
1498 down_read(&sb_dqopt(inode
->i_sb
)->dqptr_sem
);
1499 if (IS_NOQUOTA(inode
)) {
1500 up_read(&sb_dqopt(inode
->i_sb
)->dqptr_sem
);
1503 spin_lock(&dq_data_lock
);
1504 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++) {
1505 if (!inode
->i_dquot
[cnt
])
1507 if (check_idq(inode
->i_dquot
[cnt
], number
, warntype
+cnt
)
1512 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++) {
1513 if (!inode
->i_dquot
[cnt
])
1515 dquot_incr_inodes(inode
->i_dquot
[cnt
], number
);
1519 spin_unlock(&dq_data_lock
);
1520 if (ret
== QUOTA_OK
)
1521 mark_all_dquot_dirty(inode
->i_dquot
);
1522 flush_warnings(inode
->i_dquot
, warntype
);
1523 up_read(&sb_dqopt(inode
->i_sb
)->dqptr_sem
);
1526 EXPORT_SYMBOL(dquot_alloc_inode
);
1528 int dquot_claim_space(struct inode
*inode
, qsize_t number
)
1533 if (IS_NOQUOTA(inode
)) {
1534 inode_claim_rsv_space(inode
, number
);
1538 down_read(&sb_dqopt(inode
->i_sb
)->dqptr_sem
);
1539 if (IS_NOQUOTA(inode
)) {
1540 up_read(&sb_dqopt(inode
->i_sb
)->dqptr_sem
);
1541 inode_claim_rsv_space(inode
, number
);
1545 spin_lock(&dq_data_lock
);
1546 /* Claim reserved quotas to allocated quotas */
1547 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++) {
1548 if (inode
->i_dquot
[cnt
])
1549 dquot_claim_reserved_space(inode
->i_dquot
[cnt
],
1552 /* Update inode bytes */
1553 inode_claim_rsv_space(inode
, number
);
1554 spin_unlock(&dq_data_lock
);
1555 mark_all_dquot_dirty(inode
->i_dquot
);
1556 up_read(&sb_dqopt(inode
->i_sb
)->dqptr_sem
);
1560 EXPORT_SYMBOL(dquot_claim_space
);
1563 * This operation can block, but only after everything is updated
1565 int __dquot_free_space(struct inode
*inode
, qsize_t number
, int reserve
)
1568 char warntype
[MAXQUOTAS
];
1570 /* First test before acquiring mutex - solves deadlocks when we
1571 * re-enter the quota code and are already holding the mutex */
1572 if (IS_NOQUOTA(inode
)) {
1574 inode_decr_space(inode
, number
, reserve
);
1578 down_read(&sb_dqopt(inode
->i_sb
)->dqptr_sem
);
1579 /* Now recheck reliably when holding dqptr_sem */
1580 if (IS_NOQUOTA(inode
)) {
1581 up_read(&sb_dqopt(inode
->i_sb
)->dqptr_sem
);
1584 spin_lock(&dq_data_lock
);
1585 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++) {
1586 if (!inode
->i_dquot
[cnt
])
1588 warntype
[cnt
] = info_bdq_free(inode
->i_dquot
[cnt
], number
);
1590 dquot_free_reserved_space(inode
->i_dquot
[cnt
], number
);
1592 dquot_decr_space(inode
->i_dquot
[cnt
], number
);
1594 inode_decr_space(inode
, number
, reserve
);
1595 spin_unlock(&dq_data_lock
);
1599 mark_all_dquot_dirty(inode
->i_dquot
);
1601 flush_warnings(inode
->i_dquot
, warntype
);
1602 up_read(&sb_dqopt(inode
->i_sb
)->dqptr_sem
);
1606 int dquot_free_space(struct inode
*inode
, qsize_t number
)
1608 return __dquot_free_space(inode
, number
, 0);
1610 EXPORT_SYMBOL(dquot_free_space
);
1613 * Release reserved quota space
1615 void dquot_release_reserved_space(struct inode
*inode
, qsize_t number
)
1617 __dquot_free_space(inode
, number
, 1);
1620 EXPORT_SYMBOL(dquot_release_reserved_space
);
1623 * This operation can block, but only after everything is updated
1625 int dquot_free_inode(const struct inode
*inode
, qsize_t number
)
1628 char warntype
[MAXQUOTAS
];
1630 /* First test before acquiring mutex - solves deadlocks when we
1631 * re-enter the quota code and are already holding the mutex */
1632 if (IS_NOQUOTA(inode
))
1635 down_read(&sb_dqopt(inode
->i_sb
)->dqptr_sem
);
1636 /* Now recheck reliably when holding dqptr_sem */
1637 if (IS_NOQUOTA(inode
)) {
1638 up_read(&sb_dqopt(inode
->i_sb
)->dqptr_sem
);
1641 spin_lock(&dq_data_lock
);
1642 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++) {
1643 if (!inode
->i_dquot
[cnt
])
1645 warntype
[cnt
] = info_idq_free(inode
->i_dquot
[cnt
], number
);
1646 dquot_decr_inodes(inode
->i_dquot
[cnt
], number
);
1648 spin_unlock(&dq_data_lock
);
1649 mark_all_dquot_dirty(inode
->i_dquot
);
1650 flush_warnings(inode
->i_dquot
, warntype
);
1651 up_read(&sb_dqopt(inode
->i_sb
)->dqptr_sem
);
1654 EXPORT_SYMBOL(dquot_free_inode
);
1657 * Transfer the number of inode and blocks from one diskquota to an other.
1659 * This operation can block, but only after everything is updated
1660 * A transaction must be started when entering this function.
1662 int dquot_transfer(struct inode
*inode
, struct iattr
*iattr
)
1664 qsize_t space
, cur_space
;
1665 qsize_t rsv_space
= 0;
1666 struct dquot
*transfer_from
[MAXQUOTAS
];
1667 struct dquot
*transfer_to
[MAXQUOTAS
];
1668 int cnt
, ret
= QUOTA_OK
;
1669 int chuid
= iattr
->ia_valid
& ATTR_UID
&& inode
->i_uid
!= iattr
->ia_uid
,
1670 chgid
= iattr
->ia_valid
& ATTR_GID
&& inode
->i_gid
!= iattr
->ia_gid
;
1671 char warntype_to
[MAXQUOTAS
];
1672 char warntype_from_inodes
[MAXQUOTAS
], warntype_from_space
[MAXQUOTAS
];
1674 /* First test before acquiring mutex - solves deadlocks when we
1675 * re-enter the quota code and are already holding the mutex */
1676 if (IS_NOQUOTA(inode
))
1678 /* Initialize the arrays */
1679 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++) {
1680 transfer_from
[cnt
] = NULL
;
1681 transfer_to
[cnt
] = NULL
;
1682 warntype_to
[cnt
] = QUOTA_NL_NOWARN
;
1685 transfer_to
[USRQUOTA
] = dqget(inode
->i_sb
, iattr
->ia_uid
,
1688 transfer_to
[GRPQUOTA
] = dqget(inode
->i_sb
, iattr
->ia_gid
,
1691 down_write(&sb_dqopt(inode
->i_sb
)->dqptr_sem
);
1692 /* Now recheck reliably when holding dqptr_sem */
1693 if (IS_NOQUOTA(inode
)) { /* File without quota accounting? */
1694 up_write(&sb_dqopt(inode
->i_sb
)->dqptr_sem
);
1697 spin_lock(&dq_data_lock
);
1698 cur_space
= inode_get_bytes(inode
);
1699 rsv_space
= inode_get_rsv_space(inode
);
1700 space
= cur_space
+ rsv_space
;
1701 /* Build the transfer_from list and check the limits */
1702 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++) {
1703 if (!transfer_to
[cnt
])
1705 transfer_from
[cnt
] = inode
->i_dquot
[cnt
];
1706 if (check_idq(transfer_to
[cnt
], 1, warntype_to
+ cnt
) ==
1707 NO_QUOTA
|| check_bdq(transfer_to
[cnt
], space
, 0,
1708 warntype_to
+ cnt
) == NO_QUOTA
)
1713 * Finally perform the needed transfer from transfer_from to transfer_to
1715 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++) {
1717 * Skip changes for same uid or gid or for turned off quota-type.
1719 if (!transfer_to
[cnt
])
1722 /* Due to IO error we might not have transfer_from[] structure */
1723 if (transfer_from
[cnt
]) {
1724 warntype_from_inodes
[cnt
] =
1725 info_idq_free(transfer_from
[cnt
], 1);
1726 warntype_from_space
[cnt
] =
1727 info_bdq_free(transfer_from
[cnt
], space
);
1728 dquot_decr_inodes(transfer_from
[cnt
], 1);
1729 dquot_decr_space(transfer_from
[cnt
], cur_space
);
1730 dquot_free_reserved_space(transfer_from
[cnt
],
1734 dquot_incr_inodes(transfer_to
[cnt
], 1);
1735 dquot_incr_space(transfer_to
[cnt
], cur_space
);
1736 dquot_resv_space(transfer_to
[cnt
], rsv_space
);
1738 inode
->i_dquot
[cnt
] = transfer_to
[cnt
];
1740 spin_unlock(&dq_data_lock
);
1741 up_write(&sb_dqopt(inode
->i_sb
)->dqptr_sem
);
1743 mark_all_dquot_dirty(transfer_from
);
1744 mark_all_dquot_dirty(transfer_to
);
1745 /* The reference we got is transferred to the inode */
1746 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++)
1747 transfer_to
[cnt
] = NULL
;
1749 flush_warnings(transfer_to
, warntype_to
);
1750 flush_warnings(transfer_from
, warntype_from_inodes
);
1751 flush_warnings(transfer_from
, warntype_from_space
);
1753 dqput_all(transfer_from
);
1754 dqput_all(transfer_to
);
1757 spin_unlock(&dq_data_lock
);
1758 up_write(&sb_dqopt(inode
->i_sb
)->dqptr_sem
);
1759 /* Clear dquot pointers we don't want to dqput() */
1760 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++)
1761 transfer_from
[cnt
] = NULL
;
1765 EXPORT_SYMBOL(dquot_transfer
);
1767 /* Wrapper for transferring ownership of an inode */
1768 int vfs_dq_transfer(struct inode
*inode
, struct iattr
*iattr
)
1770 if (sb_any_quota_active(inode
->i_sb
) && !IS_NOQUOTA(inode
)) {
1772 if (inode
->i_sb
->dq_op
->transfer(inode
, iattr
) == NO_QUOTA
)
1777 EXPORT_SYMBOL(vfs_dq_transfer
);
1780 * Write info of quota file to disk
1782 int dquot_commit_info(struct super_block
*sb
, int type
)
1785 struct quota_info
*dqopt
= sb_dqopt(sb
);
1787 mutex_lock(&dqopt
->dqio_mutex
);
1788 ret
= dqopt
->ops
[type
]->write_file_info(sb
, type
);
1789 mutex_unlock(&dqopt
->dqio_mutex
);
1792 EXPORT_SYMBOL(dquot_commit_info
);
1795 * Definitions of diskquota operations.
1797 const struct dquot_operations dquot_operations
= {
1798 .initialize
= dquot_initialize
,
1800 .alloc_space
= dquot_alloc_space
,
1801 .alloc_inode
= dquot_alloc_inode
,
1802 .free_space
= dquot_free_space
,
1803 .free_inode
= dquot_free_inode
,
1804 .transfer
= dquot_transfer
,
1805 .write_dquot
= dquot_commit
,
1806 .acquire_dquot
= dquot_acquire
,
1807 .release_dquot
= dquot_release
,
1808 .mark_dirty
= dquot_mark_dquot_dirty
,
1809 .write_info
= dquot_commit_info
,
1810 .alloc_dquot
= dquot_alloc
,
1811 .destroy_dquot
= dquot_destroy
,
1815 * Turn quota off on a device. type == -1 ==> quotaoff for all types (umount)
1817 int vfs_quota_disable(struct super_block
*sb
, int type
, unsigned int flags
)
1820 struct quota_info
*dqopt
= sb_dqopt(sb
);
1821 struct inode
*toputinode
[MAXQUOTAS
];
1823 /* Cannot turn off usage accounting without turning off limits, or
1824 * suspend quotas and simultaneously turn quotas off. */
1825 if ((flags
& DQUOT_USAGE_ENABLED
&& !(flags
& DQUOT_LIMITS_ENABLED
))
1826 || (flags
& DQUOT_SUSPENDED
&& flags
& (DQUOT_LIMITS_ENABLED
|
1827 DQUOT_USAGE_ENABLED
)))
1830 /* We need to serialize quota_off() for device */
1831 mutex_lock(&dqopt
->dqonoff_mutex
);
1834 * Skip everything if there's nothing to do. We have to do this because
1835 * sometimes we are called when fill_super() failed and calling
1836 * sync_fs() in such cases does no good.
1838 if (!sb_any_quota_loaded(sb
)) {
1839 mutex_unlock(&dqopt
->dqonoff_mutex
);
1842 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++) {
1843 toputinode
[cnt
] = NULL
;
1844 if (type
!= -1 && cnt
!= type
)
1846 if (!sb_has_quota_loaded(sb
, cnt
))
1849 if (flags
& DQUOT_SUSPENDED
) {
1850 spin_lock(&dq_state_lock
);
1852 dquot_state_flag(DQUOT_SUSPENDED
, cnt
);
1853 spin_unlock(&dq_state_lock
);
1855 spin_lock(&dq_state_lock
);
1856 dqopt
->flags
&= ~dquot_state_flag(flags
, cnt
);
1857 /* Turning off suspended quotas? */
1858 if (!sb_has_quota_loaded(sb
, cnt
) &&
1859 sb_has_quota_suspended(sb
, cnt
)) {
1860 dqopt
->flags
&= ~dquot_state_flag(
1861 DQUOT_SUSPENDED
, cnt
);
1862 spin_unlock(&dq_state_lock
);
1863 iput(dqopt
->files
[cnt
]);
1864 dqopt
->files
[cnt
] = NULL
;
1867 spin_unlock(&dq_state_lock
);
1870 /* We still have to keep quota loaded? */
1871 if (sb_has_quota_loaded(sb
, cnt
) && !(flags
& DQUOT_SUSPENDED
))
1874 /* Note: these are blocking operations */
1875 drop_dquot_ref(sb
, cnt
);
1876 invalidate_dquots(sb
, cnt
);
1878 * Now all dquots should be invalidated, all writes done so we
1879 * should be only users of the info. No locks needed.
1881 if (info_dirty(&dqopt
->info
[cnt
]))
1882 sb
->dq_op
->write_info(sb
, cnt
);
1883 if (dqopt
->ops
[cnt
]->free_file_info
)
1884 dqopt
->ops
[cnt
]->free_file_info(sb
, cnt
);
1885 put_quota_format(dqopt
->info
[cnt
].dqi_format
);
1887 toputinode
[cnt
] = dqopt
->files
[cnt
];
1888 if (!sb_has_quota_loaded(sb
, cnt
))
1889 dqopt
->files
[cnt
] = NULL
;
1890 dqopt
->info
[cnt
].dqi_flags
= 0;
1891 dqopt
->info
[cnt
].dqi_igrace
= 0;
1892 dqopt
->info
[cnt
].dqi_bgrace
= 0;
1893 dqopt
->ops
[cnt
] = NULL
;
1895 mutex_unlock(&dqopt
->dqonoff_mutex
);
1897 /* Skip syncing and setting flags if quota files are hidden */
1898 if (dqopt
->flags
& DQUOT_QUOTA_SYS_FILE
)
1901 /* Sync the superblock so that buffers with quota data are written to
1902 * disk (and so userspace sees correct data afterwards). */
1903 if (sb
->s_op
->sync_fs
)
1904 sb
->s_op
->sync_fs(sb
, 1);
1905 sync_blockdev(sb
->s_bdev
);
1906 /* Now the quota files are just ordinary files and we can set the
1907 * inode flags back. Moreover we discard the pagecache so that
1908 * userspace sees the writes we did bypassing the pagecache. We
1909 * must also discard the blockdev buffers so that we see the
1910 * changes done by userspace on the next quotaon() */
1911 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++)
1912 if (toputinode
[cnt
]) {
1913 mutex_lock(&dqopt
->dqonoff_mutex
);
1914 /* If quota was reenabled in the meantime, we have
1916 if (!sb_has_quota_loaded(sb
, cnt
)) {
1917 mutex_lock_nested(&toputinode
[cnt
]->i_mutex
,
1919 toputinode
[cnt
]->i_flags
&= ~(S_IMMUTABLE
|
1920 S_NOATIME
| S_NOQUOTA
);
1921 truncate_inode_pages(&toputinode
[cnt
]->i_data
,
1923 mutex_unlock(&toputinode
[cnt
]->i_mutex
);
1924 mark_inode_dirty(toputinode
[cnt
]);
1926 mutex_unlock(&dqopt
->dqonoff_mutex
);
1929 invalidate_bdev(sb
->s_bdev
);
1931 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++)
1932 if (toputinode
[cnt
]) {
1933 /* On remount RO, we keep the inode pointer so that we
1934 * can reenable quota on the subsequent remount RW. We
1935 * have to check 'flags' variable and not use sb_has_
1936 * function because another quotaon / quotaoff could
1937 * change global state before we got here. We refuse
1938 * to suspend quotas when there is pending delete on
1939 * the quota file... */
1940 if (!(flags
& DQUOT_SUSPENDED
))
1941 iput(toputinode
[cnt
]);
1942 else if (!toputinode
[cnt
]->i_nlink
)
1947 EXPORT_SYMBOL(vfs_quota_disable
);
1949 int vfs_quota_off(struct super_block
*sb
, int type
, int remount
)
1951 return vfs_quota_disable(sb
, type
, remount
? DQUOT_SUSPENDED
:
1952 (DQUOT_USAGE_ENABLED
| DQUOT_LIMITS_ENABLED
));
1954 EXPORT_SYMBOL(vfs_quota_off
);
1956 * Turn quotas on on a device
1960 * Helper function to turn quotas on when we already have the inode of
1961 * quota file and no quota information is loaded.
1963 static int vfs_load_quota_inode(struct inode
*inode
, int type
, int format_id
,
1966 struct quota_format_type
*fmt
= find_quota_format(format_id
);
1967 struct super_block
*sb
= inode
->i_sb
;
1968 struct quota_info
*dqopt
= sb_dqopt(sb
);
1974 if (!S_ISREG(inode
->i_mode
)) {
1978 if (IS_RDONLY(inode
)) {
1982 if (!sb
->s_op
->quota_write
|| !sb
->s_op
->quota_read
) {
1986 /* Usage always has to be set... */
1987 if (!(flags
& DQUOT_USAGE_ENABLED
)) {
1992 if (!(dqopt
->flags
& DQUOT_QUOTA_SYS_FILE
)) {
1993 /* As we bypass the pagecache we must now flush the inode so
1994 * that we see all the changes from userspace... */
1995 write_inode_now(inode
, 1);
1996 /* And now flush the block cache so that kernel sees the
1998 invalidate_bdev(sb
->s_bdev
);
2000 mutex_lock(&dqopt
->dqonoff_mutex
);
2001 if (sb_has_quota_loaded(sb
, type
)) {
2006 if (!(dqopt
->flags
& DQUOT_QUOTA_SYS_FILE
)) {
2007 /* We don't want quota and atime on quota files (deadlocks
2008 * possible) Also nobody should write to the file - we use
2009 * special IO operations which ignore the immutable bit. */
2010 down_write(&dqopt
->dqptr_sem
);
2011 mutex_lock_nested(&inode
->i_mutex
, I_MUTEX_QUOTA
);
2012 oldflags
= inode
->i_flags
& (S_NOATIME
| S_IMMUTABLE
|
2014 inode
->i_flags
|= S_NOQUOTA
| S_NOATIME
| S_IMMUTABLE
;
2015 mutex_unlock(&inode
->i_mutex
);
2016 up_write(&dqopt
->dqptr_sem
);
2017 sb
->dq_op
->drop(inode
);
2021 dqopt
->files
[type
] = igrab(inode
);
2022 if (!dqopt
->files
[type
])
2025 if (!fmt
->qf_ops
->check_quota_file(sb
, type
))
2028 dqopt
->ops
[type
] = fmt
->qf_ops
;
2029 dqopt
->info
[type
].dqi_format
= fmt
;
2030 dqopt
->info
[type
].dqi_fmt_id
= format_id
;
2031 INIT_LIST_HEAD(&dqopt
->info
[type
].dqi_dirty_list
);
2032 mutex_lock(&dqopt
->dqio_mutex
);
2033 error
= dqopt
->ops
[type
]->read_file_info(sb
, type
);
2035 mutex_unlock(&dqopt
->dqio_mutex
);
2038 mutex_unlock(&dqopt
->dqio_mutex
);
2039 spin_lock(&dq_state_lock
);
2040 dqopt
->flags
|= dquot_state_flag(flags
, type
);
2041 spin_unlock(&dq_state_lock
);
2043 add_dquot_ref(sb
, type
);
2044 mutex_unlock(&dqopt
->dqonoff_mutex
);
2049 dqopt
->files
[type
] = NULL
;
2052 if (oldflags
!= -1) {
2053 down_write(&dqopt
->dqptr_sem
);
2054 mutex_lock_nested(&inode
->i_mutex
, I_MUTEX_QUOTA
);
2055 /* Set the flags back (in the case of accidental quotaon()
2056 * on a wrong file we don't want to mess up the flags) */
2057 inode
->i_flags
&= ~(S_NOATIME
| S_NOQUOTA
| S_IMMUTABLE
);
2058 inode
->i_flags
|= oldflags
;
2059 mutex_unlock(&inode
->i_mutex
);
2060 up_write(&dqopt
->dqptr_sem
);
2062 mutex_unlock(&dqopt
->dqonoff_mutex
);
2064 put_quota_format(fmt
);
2069 /* Reenable quotas on remount RW */
2070 static int vfs_quota_on_remount(struct super_block
*sb
, int type
)
2072 struct quota_info
*dqopt
= sb_dqopt(sb
);
2073 struct inode
*inode
;
2077 mutex_lock(&dqopt
->dqonoff_mutex
);
2078 if (!sb_has_quota_suspended(sb
, type
)) {
2079 mutex_unlock(&dqopt
->dqonoff_mutex
);
2082 inode
= dqopt
->files
[type
];
2083 dqopt
->files
[type
] = NULL
;
2084 spin_lock(&dq_state_lock
);
2085 flags
= dqopt
->flags
& dquot_state_flag(DQUOT_USAGE_ENABLED
|
2086 DQUOT_LIMITS_ENABLED
, type
);
2087 dqopt
->flags
&= ~dquot_state_flag(DQUOT_STATE_FLAGS
, type
);
2088 spin_unlock(&dq_state_lock
);
2089 mutex_unlock(&dqopt
->dqonoff_mutex
);
2091 flags
= dquot_generic_flag(flags
, type
);
2092 ret
= vfs_load_quota_inode(inode
, type
, dqopt
->info
[type
].dqi_fmt_id
,
2099 int vfs_quota_on_path(struct super_block
*sb
, int type
, int format_id
,
2102 int error
= security_quota_on(path
->dentry
);
2105 /* Quota file not on the same filesystem? */
2106 if (path
->mnt
->mnt_sb
!= sb
)
2109 error
= vfs_load_quota_inode(path
->dentry
->d_inode
, type
,
2110 format_id
, DQUOT_USAGE_ENABLED
|
2111 DQUOT_LIMITS_ENABLED
);
2114 EXPORT_SYMBOL(vfs_quota_on_path
);
2116 int vfs_quota_on(struct super_block
*sb
, int type
, int format_id
, char *name
,
2123 return vfs_quota_on_remount(sb
, type
);
2125 error
= kern_path(name
, LOOKUP_FOLLOW
, &path
);
2127 error
= vfs_quota_on_path(sb
, type
, format_id
, &path
);
2132 EXPORT_SYMBOL(vfs_quota_on
);
2135 * More powerful function for turning on quotas allowing setting
2136 * of individual quota flags
2138 int vfs_quota_enable(struct inode
*inode
, int type
, int format_id
,
2142 struct super_block
*sb
= inode
->i_sb
;
2143 struct quota_info
*dqopt
= sb_dqopt(sb
);
2145 /* Just unsuspend quotas? */
2146 if (flags
& DQUOT_SUSPENDED
)
2147 return vfs_quota_on_remount(sb
, type
);
2150 /* Just updating flags needed? */
2151 if (sb_has_quota_loaded(sb
, type
)) {
2152 mutex_lock(&dqopt
->dqonoff_mutex
);
2153 /* Now do a reliable test... */
2154 if (!sb_has_quota_loaded(sb
, type
)) {
2155 mutex_unlock(&dqopt
->dqonoff_mutex
);
2158 if (flags
& DQUOT_USAGE_ENABLED
&&
2159 sb_has_quota_usage_enabled(sb
, type
)) {
2163 if (flags
& DQUOT_LIMITS_ENABLED
&&
2164 sb_has_quota_limits_enabled(sb
, type
)) {
2168 spin_lock(&dq_state_lock
);
2169 sb_dqopt(sb
)->flags
|= dquot_state_flag(flags
, type
);
2170 spin_unlock(&dq_state_lock
);
2172 mutex_unlock(&dqopt
->dqonoff_mutex
);
2177 return vfs_load_quota_inode(inode
, type
, format_id
, flags
);
2179 EXPORT_SYMBOL(vfs_quota_enable
);
2182 * This function is used when filesystem needs to initialize quotas
2183 * during mount time.
2185 int vfs_quota_on_mount(struct super_block
*sb
, char *qf_name
,
2186 int format_id
, int type
)
2188 struct dentry
*dentry
;
2191 mutex_lock(&sb
->s_root
->d_inode
->i_mutex
);
2192 dentry
= lookup_one_len(qf_name
, sb
->s_root
, strlen(qf_name
));
2193 mutex_unlock(&sb
->s_root
->d_inode
->i_mutex
);
2195 return PTR_ERR(dentry
);
2197 if (!dentry
->d_inode
) {
2202 error
= security_quota_on(dentry
);
2204 error
= vfs_load_quota_inode(dentry
->d_inode
, type
, format_id
,
2205 DQUOT_USAGE_ENABLED
| DQUOT_LIMITS_ENABLED
);
2211 EXPORT_SYMBOL(vfs_quota_on_mount
);
2213 /* Wrapper to turn on quotas when remounting rw */
2214 int vfs_dq_quota_on_remount(struct super_block
*sb
)
2219 if (!sb
->s_qcop
|| !sb
->s_qcop
->quota_on
)
2221 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++) {
2222 err
= sb
->s_qcop
->quota_on(sb
, cnt
, 0, NULL
, 1);
2223 if (err
< 0 && !ret
)
2228 EXPORT_SYMBOL(vfs_dq_quota_on_remount
);
2230 static inline qsize_t
qbtos(qsize_t blocks
)
2232 return blocks
<< QIF_DQBLKSIZE_BITS
;
2235 static inline qsize_t
stoqb(qsize_t space
)
2237 return (space
+ QIF_DQBLKSIZE
- 1) >> QIF_DQBLKSIZE_BITS
;
2240 /* Generic routine for getting common part of quota structure */
2241 static void do_get_dqblk(struct dquot
*dquot
, struct if_dqblk
*di
)
2243 struct mem_dqblk
*dm
= &dquot
->dq_dqb
;
2245 spin_lock(&dq_data_lock
);
2246 di
->dqb_bhardlimit
= stoqb(dm
->dqb_bhardlimit
);
2247 di
->dqb_bsoftlimit
= stoqb(dm
->dqb_bsoftlimit
);
2248 di
->dqb_curspace
= dm
->dqb_curspace
+ dm
->dqb_rsvspace
;
2249 di
->dqb_ihardlimit
= dm
->dqb_ihardlimit
;
2250 di
->dqb_isoftlimit
= dm
->dqb_isoftlimit
;
2251 di
->dqb_curinodes
= dm
->dqb_curinodes
;
2252 di
->dqb_btime
= dm
->dqb_btime
;
2253 di
->dqb_itime
= dm
->dqb_itime
;
2254 di
->dqb_valid
= QIF_ALL
;
2255 spin_unlock(&dq_data_lock
);
2258 int vfs_get_dqblk(struct super_block
*sb
, int type
, qid_t id
,
2259 struct if_dqblk
*di
)
2261 struct dquot
*dquot
;
2263 dquot
= dqget(sb
, id
, type
);
2266 do_get_dqblk(dquot
, di
);
2271 EXPORT_SYMBOL(vfs_get_dqblk
);
2273 /* Generic routine for setting common part of quota structure */
2274 static int do_set_dqblk(struct dquot
*dquot
, struct if_dqblk
*di
)
2276 struct mem_dqblk
*dm
= &dquot
->dq_dqb
;
2277 int check_blim
= 0, check_ilim
= 0;
2278 struct mem_dqinfo
*dqi
= &sb_dqopt(dquot
->dq_sb
)->info
[dquot
->dq_type
];
2280 if ((di
->dqb_valid
& QIF_BLIMITS
&&
2281 (di
->dqb_bhardlimit
> dqi
->dqi_maxblimit
||
2282 di
->dqb_bsoftlimit
> dqi
->dqi_maxblimit
)) ||
2283 (di
->dqb_valid
& QIF_ILIMITS
&&
2284 (di
->dqb_ihardlimit
> dqi
->dqi_maxilimit
||
2285 di
->dqb_isoftlimit
> dqi
->dqi_maxilimit
)))
2288 spin_lock(&dq_data_lock
);
2289 if (di
->dqb_valid
& QIF_SPACE
) {
2290 dm
->dqb_curspace
= di
->dqb_curspace
- dm
->dqb_rsvspace
;
2292 __set_bit(DQ_LASTSET_B
+ QIF_SPACE_B
, &dquot
->dq_flags
);
2294 if (di
->dqb_valid
& QIF_BLIMITS
) {
2295 dm
->dqb_bsoftlimit
= qbtos(di
->dqb_bsoftlimit
);
2296 dm
->dqb_bhardlimit
= qbtos(di
->dqb_bhardlimit
);
2298 __set_bit(DQ_LASTSET_B
+ QIF_BLIMITS_B
, &dquot
->dq_flags
);
2300 if (di
->dqb_valid
& QIF_INODES
) {
2301 dm
->dqb_curinodes
= di
->dqb_curinodes
;
2303 __set_bit(DQ_LASTSET_B
+ QIF_INODES_B
, &dquot
->dq_flags
);
2305 if (di
->dqb_valid
& QIF_ILIMITS
) {
2306 dm
->dqb_isoftlimit
= di
->dqb_isoftlimit
;
2307 dm
->dqb_ihardlimit
= di
->dqb_ihardlimit
;
2309 __set_bit(DQ_LASTSET_B
+ QIF_ILIMITS_B
, &dquot
->dq_flags
);
2311 if (di
->dqb_valid
& QIF_BTIME
) {
2312 dm
->dqb_btime
= di
->dqb_btime
;
2314 __set_bit(DQ_LASTSET_B
+ QIF_BTIME_B
, &dquot
->dq_flags
);
2316 if (di
->dqb_valid
& QIF_ITIME
) {
2317 dm
->dqb_itime
= di
->dqb_itime
;
2319 __set_bit(DQ_LASTSET_B
+ QIF_ITIME_B
, &dquot
->dq_flags
);
2323 if (!dm
->dqb_bsoftlimit
||
2324 dm
->dqb_curspace
< dm
->dqb_bsoftlimit
) {
2326 clear_bit(DQ_BLKS_B
, &dquot
->dq_flags
);
2327 } else if (!(di
->dqb_valid
& QIF_BTIME
))
2328 /* Set grace only if user hasn't provided his own... */
2329 dm
->dqb_btime
= get_seconds() + dqi
->dqi_bgrace
;
2332 if (!dm
->dqb_isoftlimit
||
2333 dm
->dqb_curinodes
< dm
->dqb_isoftlimit
) {
2335 clear_bit(DQ_INODES_B
, &dquot
->dq_flags
);
2336 } else if (!(di
->dqb_valid
& QIF_ITIME
))
2337 /* Set grace only if user hasn't provided his own... */
2338 dm
->dqb_itime
= get_seconds() + dqi
->dqi_igrace
;
2340 if (dm
->dqb_bhardlimit
|| dm
->dqb_bsoftlimit
|| dm
->dqb_ihardlimit
||
2342 clear_bit(DQ_FAKE_B
, &dquot
->dq_flags
);
2344 set_bit(DQ_FAKE_B
, &dquot
->dq_flags
);
2345 spin_unlock(&dq_data_lock
);
2346 mark_dquot_dirty(dquot
);
2351 int vfs_set_dqblk(struct super_block
*sb
, int type
, qid_t id
,
2352 struct if_dqblk
*di
)
2354 struct dquot
*dquot
;
2357 dquot
= dqget(sb
, id
, type
);
2362 rc
= do_set_dqblk(dquot
, di
);
2367 EXPORT_SYMBOL(vfs_set_dqblk
);
2369 /* Generic routine for getting common part of quota file information */
2370 int vfs_get_dqinfo(struct super_block
*sb
, int type
, struct if_dqinfo
*ii
)
2372 struct mem_dqinfo
*mi
;
2374 mutex_lock(&sb_dqopt(sb
)->dqonoff_mutex
);
2375 if (!sb_has_quota_active(sb
, type
)) {
2376 mutex_unlock(&sb_dqopt(sb
)->dqonoff_mutex
);
2379 mi
= sb_dqopt(sb
)->info
+ type
;
2380 spin_lock(&dq_data_lock
);
2381 ii
->dqi_bgrace
= mi
->dqi_bgrace
;
2382 ii
->dqi_igrace
= mi
->dqi_igrace
;
2383 ii
->dqi_flags
= mi
->dqi_flags
& DQF_MASK
;
2384 ii
->dqi_valid
= IIF_ALL
;
2385 spin_unlock(&dq_data_lock
);
2386 mutex_unlock(&sb_dqopt(sb
)->dqonoff_mutex
);
2389 EXPORT_SYMBOL(vfs_get_dqinfo
);
2391 /* Generic routine for setting common part of quota file information */
2392 int vfs_set_dqinfo(struct super_block
*sb
, int type
, struct if_dqinfo
*ii
)
2394 struct mem_dqinfo
*mi
;
2397 mutex_lock(&sb_dqopt(sb
)->dqonoff_mutex
);
2398 if (!sb_has_quota_active(sb
, type
)) {
2402 mi
= sb_dqopt(sb
)->info
+ type
;
2403 spin_lock(&dq_data_lock
);
2404 if (ii
->dqi_valid
& IIF_BGRACE
)
2405 mi
->dqi_bgrace
= ii
->dqi_bgrace
;
2406 if (ii
->dqi_valid
& IIF_IGRACE
)
2407 mi
->dqi_igrace
= ii
->dqi_igrace
;
2408 if (ii
->dqi_valid
& IIF_FLAGS
)
2409 mi
->dqi_flags
= (mi
->dqi_flags
& ~DQF_MASK
) |
2410 (ii
->dqi_flags
& DQF_MASK
);
2411 spin_unlock(&dq_data_lock
);
2412 mark_info_dirty(sb
, type
);
2413 /* Force write to disk */
2414 sb
->dq_op
->write_info(sb
, type
);
2416 mutex_unlock(&sb_dqopt(sb
)->dqonoff_mutex
);
2419 EXPORT_SYMBOL(vfs_set_dqinfo
);
2421 const struct quotactl_ops vfs_quotactl_ops
= {
2422 .quota_on
= vfs_quota_on
,
2423 .quota_off
= vfs_quota_off
,
2424 .quota_sync
= vfs_quota_sync
,
2425 .get_info
= vfs_get_dqinfo
,
2426 .set_info
= vfs_set_dqinfo
,
2427 .get_dqblk
= vfs_get_dqblk
,
2428 .set_dqblk
= vfs_set_dqblk
2431 static ctl_table fs_dqstats_table
[] = {
2433 .procname
= "lookups",
2434 .data
= &dqstats
.lookups
,
2435 .maxlen
= sizeof(int),
2437 .proc_handler
= proc_dointvec
,
2440 .procname
= "drops",
2441 .data
= &dqstats
.drops
,
2442 .maxlen
= sizeof(int),
2444 .proc_handler
= proc_dointvec
,
2447 .procname
= "reads",
2448 .data
= &dqstats
.reads
,
2449 .maxlen
= sizeof(int),
2451 .proc_handler
= proc_dointvec
,
2454 .procname
= "writes",
2455 .data
= &dqstats
.writes
,
2456 .maxlen
= sizeof(int),
2458 .proc_handler
= proc_dointvec
,
2461 .procname
= "cache_hits",
2462 .data
= &dqstats
.cache_hits
,
2463 .maxlen
= sizeof(int),
2465 .proc_handler
= proc_dointvec
,
2468 .procname
= "allocated_dquots",
2469 .data
= &dqstats
.allocated_dquots
,
2470 .maxlen
= sizeof(int),
2472 .proc_handler
= proc_dointvec
,
2475 .procname
= "free_dquots",
2476 .data
= &dqstats
.free_dquots
,
2477 .maxlen
= sizeof(int),
2479 .proc_handler
= proc_dointvec
,
2482 .procname
= "syncs",
2483 .data
= &dqstats
.syncs
,
2484 .maxlen
= sizeof(int),
2486 .proc_handler
= proc_dointvec
,
2488 #ifdef CONFIG_PRINT_QUOTA_WARNING
2490 .procname
= "warnings",
2491 .data
= &flag_print_warnings
,
2492 .maxlen
= sizeof(int),
2494 .proc_handler
= proc_dointvec
,
2500 static ctl_table fs_table
[] = {
2502 .procname
= "quota",
2504 .child
= fs_dqstats_table
,
2509 static ctl_table sys_table
[] = {
2518 static int __init
dquot_init(void)
2521 unsigned long nr_hash
, order
;
2523 printk(KERN_NOTICE
"VFS: Disk quotas %s\n", __DQUOT_VERSION__
);
2525 register_sysctl_table(sys_table
);
2527 dquot_cachep
= kmem_cache_create("dquot",
2528 sizeof(struct dquot
), sizeof(unsigned long) * 4,
2529 (SLAB_HWCACHE_ALIGN
|SLAB_RECLAIM_ACCOUNT
|
2530 SLAB_MEM_SPREAD
|SLAB_PANIC
),
2534 dquot_hash
= (struct hlist_head
*)__get_free_pages(GFP_ATOMIC
, order
);
2536 panic("Cannot create dquot hash table");
2538 /* Find power-of-two hlist_heads which can fit into allocation */
2539 nr_hash
= (1UL << order
) * PAGE_SIZE
/ sizeof(struct hlist_head
);
2543 } while (nr_hash
>> dq_hash_bits
);
2546 nr_hash
= 1UL << dq_hash_bits
;
2547 dq_hash_mask
= nr_hash
- 1;
2548 for (i
= 0; i
< nr_hash
; i
++)
2549 INIT_HLIST_HEAD(dquot_hash
+ i
);
2551 printk("Dquot-cache hash table entries: %ld (order %ld, %ld bytes)\n",
2552 nr_hash
, order
, (PAGE_SIZE
<< order
));
2554 register_shrinker(&dqcache_shrinker
);
2558 module_init(dquot_init
);