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 * Special care needs to be taken about S_NOQUOTA inode flag (marking that
105 * inode is a quota file). Functions adding pointers from inode to dquots have
106 * to check this flag under dqptr_sem and then (if S_NOQUOTA is not set) they
107 * have to do all pointer modifications before dropping dqptr_sem. This makes
108 * sure they cannot race with quotaon which first sets S_NOQUOTA flag and
109 * then drops all pointers to dquots from an inode.
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 * The lock ordering of dqptr_sem imposed by quota code is only dqonoff_sem >
124 * dqptr_sem. But filesystem has to count with the fact that functions such as
125 * dquot_alloc_space() acquire dqptr_sem and they usually have to be called
126 * from inside a transaction to keep filesystem consistency after a crash. Also
127 * filesystems usually want to do some IO on dquot from ->mark_dirty which is
128 * called with dqptr_sem held.
129 * i_mutex on quota files is special (it's below dqio_mutex)
132 static __cacheline_aligned_in_smp
DEFINE_SPINLOCK(dq_list_lock
);
133 static __cacheline_aligned_in_smp
DEFINE_SPINLOCK(dq_state_lock
);
134 __cacheline_aligned_in_smp
DEFINE_SPINLOCK(dq_data_lock
);
135 EXPORT_SYMBOL(dq_data_lock
);
137 static char *quotatypes
[] = INITQFNAMES
;
138 static struct quota_format_type
*quota_formats
; /* List of registered formats */
139 static struct quota_module_name module_names
[] = INIT_QUOTA_MODULE_NAMES
;
141 /* SLAB cache for dquot structures */
142 static struct kmem_cache
*dquot_cachep
;
144 int register_quota_format(struct quota_format_type
*fmt
)
146 spin_lock(&dq_list_lock
);
147 fmt
->qf_next
= quota_formats
;
149 spin_unlock(&dq_list_lock
);
152 EXPORT_SYMBOL(register_quota_format
);
154 void unregister_quota_format(struct quota_format_type
*fmt
)
156 struct quota_format_type
**actqf
;
158 spin_lock(&dq_list_lock
);
159 for (actqf
= "a_formats
; *actqf
&& *actqf
!= fmt
;
160 actqf
= &(*actqf
)->qf_next
)
163 *actqf
= (*actqf
)->qf_next
;
164 spin_unlock(&dq_list_lock
);
166 EXPORT_SYMBOL(unregister_quota_format
);
168 static struct quota_format_type
*find_quota_format(int id
)
170 struct quota_format_type
*actqf
;
172 spin_lock(&dq_list_lock
);
173 for (actqf
= quota_formats
; actqf
&& actqf
->qf_fmt_id
!= id
;
174 actqf
= actqf
->qf_next
)
176 if (!actqf
|| !try_module_get(actqf
->qf_owner
)) {
179 spin_unlock(&dq_list_lock
);
181 for (qm
= 0; module_names
[qm
].qm_fmt_id
&&
182 module_names
[qm
].qm_fmt_id
!= id
; qm
++)
184 if (!module_names
[qm
].qm_fmt_id
||
185 request_module(module_names
[qm
].qm_mod_name
))
188 spin_lock(&dq_list_lock
);
189 for (actqf
= quota_formats
; actqf
&& actqf
->qf_fmt_id
!= id
;
190 actqf
= actqf
->qf_next
)
192 if (actqf
&& !try_module_get(actqf
->qf_owner
))
195 spin_unlock(&dq_list_lock
);
199 static void put_quota_format(struct quota_format_type
*fmt
)
201 module_put(fmt
->qf_owner
);
205 * Dquot List Management:
206 * The quota code uses three lists for dquot management: the inuse_list,
207 * free_dquots, and dquot_hash[] array. A single dquot structure may be
208 * on all three lists, depending on its current state.
210 * All dquots are placed to the end of inuse_list when first created, and this
211 * list is used for invalidate operation, which must look at every dquot.
213 * Unused dquots (dq_count == 0) are added to the free_dquots list when freed,
214 * and this list is searched whenever we need an available dquot. Dquots are
215 * removed from the list as soon as they are used again, and
216 * dqstats.free_dquots gives the number of dquots on the list. When
217 * dquot is invalidated it's completely released from memory.
219 * Dquots with a specific identity (device, type and id) are placed on
220 * one of the dquot_hash[] hash chains. The provides an efficient search
221 * mechanism to locate a specific dquot.
224 static LIST_HEAD(inuse_list
);
225 static LIST_HEAD(free_dquots
);
226 static unsigned int dq_hash_bits
, dq_hash_mask
;
227 static struct hlist_head
*dquot_hash
;
229 struct dqstats dqstats
;
230 EXPORT_SYMBOL(dqstats
);
232 static qsize_t
inode_get_rsv_space(struct inode
*inode
);
234 static inline unsigned int
235 hashfn(const struct super_block
*sb
, unsigned int id
, int type
)
239 tmp
= (((unsigned long)sb
>>L1_CACHE_SHIFT
) ^ id
) * (MAXQUOTAS
- type
);
240 return (tmp
+ (tmp
>> dq_hash_bits
)) & dq_hash_mask
;
244 * Following list functions expect dq_list_lock to be held
246 static inline void insert_dquot_hash(struct dquot
*dquot
)
248 struct hlist_head
*head
;
249 head
= dquot_hash
+ hashfn(dquot
->dq_sb
, dquot
->dq_id
, dquot
->dq_type
);
250 hlist_add_head(&dquot
->dq_hash
, head
);
253 static inline void remove_dquot_hash(struct dquot
*dquot
)
255 hlist_del_init(&dquot
->dq_hash
);
258 static struct dquot
*find_dquot(unsigned int hashent
, struct super_block
*sb
,
259 unsigned int id
, int type
)
261 struct hlist_node
*node
;
264 hlist_for_each (node
, dquot_hash
+hashent
) {
265 dquot
= hlist_entry(node
, struct dquot
, dq_hash
);
266 if (dquot
->dq_sb
== sb
&& dquot
->dq_id
== id
&&
267 dquot
->dq_type
== type
)
273 /* Add a dquot to the tail of the free list */
274 static inline void put_dquot_last(struct dquot
*dquot
)
276 list_add_tail(&dquot
->dq_free
, &free_dquots
);
277 dqstats
.free_dquots
++;
280 static inline void remove_free_dquot(struct dquot
*dquot
)
282 if (list_empty(&dquot
->dq_free
))
284 list_del_init(&dquot
->dq_free
);
285 dqstats
.free_dquots
--;
288 static inline void put_inuse(struct dquot
*dquot
)
290 /* We add to the back of inuse list so we don't have to restart
291 * when traversing this list and we block */
292 list_add_tail(&dquot
->dq_inuse
, &inuse_list
);
293 dqstats
.allocated_dquots
++;
296 static inline void remove_inuse(struct dquot
*dquot
)
298 dqstats
.allocated_dquots
--;
299 list_del(&dquot
->dq_inuse
);
302 * End of list functions needing dq_list_lock
305 static void wait_on_dquot(struct dquot
*dquot
)
307 mutex_lock(&dquot
->dq_lock
);
308 mutex_unlock(&dquot
->dq_lock
);
311 static inline int dquot_dirty(struct dquot
*dquot
)
313 return test_bit(DQ_MOD_B
, &dquot
->dq_flags
);
316 static inline int mark_dquot_dirty(struct dquot
*dquot
)
318 return dquot
->dq_sb
->dq_op
->mark_dirty(dquot
);
321 int dquot_mark_dquot_dirty(struct dquot
*dquot
)
323 spin_lock(&dq_list_lock
);
324 if (!test_and_set_bit(DQ_MOD_B
, &dquot
->dq_flags
))
325 list_add(&dquot
->dq_dirty
, &sb_dqopt(dquot
->dq_sb
)->
326 info
[dquot
->dq_type
].dqi_dirty_list
);
327 spin_unlock(&dq_list_lock
);
330 EXPORT_SYMBOL(dquot_mark_dquot_dirty
);
332 /* Dirtify all the dquots - this can block when journalling */
333 static inline int mark_all_dquot_dirty(struct dquot
* const *dquot
)
338 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++) {
340 /* Even in case of error we have to continue */
341 ret
= mark_dquot_dirty(dquot
[cnt
]);
348 static inline void dqput_all(struct dquot
**dquot
)
352 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++)
356 /* This function needs dq_list_lock */
357 static inline int clear_dquot_dirty(struct dquot
*dquot
)
359 if (!test_and_clear_bit(DQ_MOD_B
, &dquot
->dq_flags
))
361 list_del_init(&dquot
->dq_dirty
);
365 void mark_info_dirty(struct super_block
*sb
, int type
)
367 set_bit(DQF_INFO_DIRTY_B
, &sb_dqopt(sb
)->info
[type
].dqi_flags
);
369 EXPORT_SYMBOL(mark_info_dirty
);
372 * Read dquot from disk and alloc space for it
375 int dquot_acquire(struct dquot
*dquot
)
377 int ret
= 0, ret2
= 0;
378 struct quota_info
*dqopt
= sb_dqopt(dquot
->dq_sb
);
380 mutex_lock(&dquot
->dq_lock
);
381 mutex_lock(&dqopt
->dqio_mutex
);
382 if (!test_bit(DQ_READ_B
, &dquot
->dq_flags
))
383 ret
= dqopt
->ops
[dquot
->dq_type
]->read_dqblk(dquot
);
386 set_bit(DQ_READ_B
, &dquot
->dq_flags
);
387 /* Instantiate dquot if needed */
388 if (!test_bit(DQ_ACTIVE_B
, &dquot
->dq_flags
) && !dquot
->dq_off
) {
389 ret
= dqopt
->ops
[dquot
->dq_type
]->commit_dqblk(dquot
);
390 /* Write the info if needed */
391 if (info_dirty(&dqopt
->info
[dquot
->dq_type
])) {
392 ret2
= dqopt
->ops
[dquot
->dq_type
]->write_file_info(
393 dquot
->dq_sb
, dquot
->dq_type
);
402 set_bit(DQ_ACTIVE_B
, &dquot
->dq_flags
);
404 mutex_unlock(&dqopt
->dqio_mutex
);
405 mutex_unlock(&dquot
->dq_lock
);
408 EXPORT_SYMBOL(dquot_acquire
);
411 * Write dquot to disk
413 int dquot_commit(struct dquot
*dquot
)
415 int ret
= 0, ret2
= 0;
416 struct quota_info
*dqopt
= sb_dqopt(dquot
->dq_sb
);
418 mutex_lock(&dqopt
->dqio_mutex
);
419 spin_lock(&dq_list_lock
);
420 if (!clear_dquot_dirty(dquot
)) {
421 spin_unlock(&dq_list_lock
);
424 spin_unlock(&dq_list_lock
);
425 /* Inactive dquot can be only if there was error during read/init
426 * => we have better not writing it */
427 if (test_bit(DQ_ACTIVE_B
, &dquot
->dq_flags
)) {
428 ret
= dqopt
->ops
[dquot
->dq_type
]->commit_dqblk(dquot
);
429 if (info_dirty(&dqopt
->info
[dquot
->dq_type
])) {
430 ret2
= dqopt
->ops
[dquot
->dq_type
]->write_file_info(
431 dquot
->dq_sb
, dquot
->dq_type
);
437 mutex_unlock(&dqopt
->dqio_mutex
);
440 EXPORT_SYMBOL(dquot_commit
);
445 int dquot_release(struct dquot
*dquot
)
447 int ret
= 0, ret2
= 0;
448 struct quota_info
*dqopt
= sb_dqopt(dquot
->dq_sb
);
450 mutex_lock(&dquot
->dq_lock
);
451 /* Check whether we are not racing with some other dqget() */
452 if (atomic_read(&dquot
->dq_count
) > 1)
454 mutex_lock(&dqopt
->dqio_mutex
);
455 if (dqopt
->ops
[dquot
->dq_type
]->release_dqblk
) {
456 ret
= dqopt
->ops
[dquot
->dq_type
]->release_dqblk(dquot
);
458 if (info_dirty(&dqopt
->info
[dquot
->dq_type
])) {
459 ret2
= dqopt
->ops
[dquot
->dq_type
]->write_file_info(
460 dquot
->dq_sb
, dquot
->dq_type
);
465 clear_bit(DQ_ACTIVE_B
, &dquot
->dq_flags
);
466 mutex_unlock(&dqopt
->dqio_mutex
);
468 mutex_unlock(&dquot
->dq_lock
);
471 EXPORT_SYMBOL(dquot_release
);
473 void dquot_destroy(struct dquot
*dquot
)
475 kmem_cache_free(dquot_cachep
, dquot
);
477 EXPORT_SYMBOL(dquot_destroy
);
479 static inline void do_destroy_dquot(struct dquot
*dquot
)
481 dquot
->dq_sb
->dq_op
->destroy_dquot(dquot
);
484 /* Invalidate all dquots on the list. Note that this function is called after
485 * quota is disabled and pointers from inodes removed so there cannot be new
486 * quota users. There can still be some users of quotas due to inodes being
487 * just deleted or pruned by prune_icache() (those are not attached to any
488 * list) or parallel quotactl call. We have to wait for such users.
490 static void invalidate_dquots(struct super_block
*sb
, int type
)
492 struct dquot
*dquot
, *tmp
;
495 spin_lock(&dq_list_lock
);
496 list_for_each_entry_safe(dquot
, tmp
, &inuse_list
, dq_inuse
) {
497 if (dquot
->dq_sb
!= sb
)
499 if (dquot
->dq_type
!= type
)
501 /* Wait for dquot users */
502 if (atomic_read(&dquot
->dq_count
)) {
505 atomic_inc(&dquot
->dq_count
);
506 prepare_to_wait(&dquot
->dq_wait_unused
, &wait
,
507 TASK_UNINTERRUPTIBLE
);
508 spin_unlock(&dq_list_lock
);
509 /* Once dqput() wakes us up, we know it's time to free
511 * IMPORTANT: we rely on the fact that there is always
512 * at most one process waiting for dquot to free.
513 * Otherwise dq_count would be > 1 and we would never
516 if (atomic_read(&dquot
->dq_count
) > 1)
518 finish_wait(&dquot
->dq_wait_unused
, &wait
);
520 /* At this moment dquot() need not exist (it could be
521 * reclaimed by prune_dqcache(). Hence we must
526 * Quota now has no users and it has been written on last
529 remove_dquot_hash(dquot
);
530 remove_free_dquot(dquot
);
532 do_destroy_dquot(dquot
);
534 spin_unlock(&dq_list_lock
);
537 /* Call callback for every active dquot on given filesystem */
538 int dquot_scan_active(struct super_block
*sb
,
539 int (*fn
)(struct dquot
*dquot
, unsigned long priv
),
542 struct dquot
*dquot
, *old_dquot
= NULL
;
545 mutex_lock(&sb_dqopt(sb
)->dqonoff_mutex
);
546 spin_lock(&dq_list_lock
);
547 list_for_each_entry(dquot
, &inuse_list
, dq_inuse
) {
548 if (!test_bit(DQ_ACTIVE_B
, &dquot
->dq_flags
))
550 if (dquot
->dq_sb
!= sb
)
552 /* Now we have active dquot so we can just increase use count */
553 atomic_inc(&dquot
->dq_count
);
555 spin_unlock(&dq_list_lock
);
558 ret
= fn(dquot
, priv
);
561 spin_lock(&dq_list_lock
);
562 /* We are safe to continue now because our dquot could not
563 * be moved out of the inuse list while we hold the reference */
565 spin_unlock(&dq_list_lock
);
568 mutex_unlock(&sb_dqopt(sb
)->dqonoff_mutex
);
571 EXPORT_SYMBOL(dquot_scan_active
);
573 int vfs_quota_sync(struct super_block
*sb
, int type
)
575 struct list_head
*dirty
;
577 struct quota_info
*dqopt
= sb_dqopt(sb
);
580 mutex_lock(&dqopt
->dqonoff_mutex
);
581 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++) {
582 if (type
!= -1 && cnt
!= type
)
584 if (!sb_has_quota_active(sb
, cnt
))
586 spin_lock(&dq_list_lock
);
587 dirty
= &dqopt
->info
[cnt
].dqi_dirty_list
;
588 while (!list_empty(dirty
)) {
589 dquot
= list_first_entry(dirty
, struct dquot
,
591 /* Dirty and inactive can be only bad dquot... */
592 if (!test_bit(DQ_ACTIVE_B
, &dquot
->dq_flags
)) {
593 clear_dquot_dirty(dquot
);
596 /* Now we have active dquot from which someone is
597 * holding reference so we can safely just increase
599 atomic_inc(&dquot
->dq_count
);
601 spin_unlock(&dq_list_lock
);
602 sb
->dq_op
->write_dquot(dquot
);
604 spin_lock(&dq_list_lock
);
606 spin_unlock(&dq_list_lock
);
609 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++)
610 if ((cnt
== type
|| type
== -1) && sb_has_quota_active(sb
, cnt
)
611 && info_dirty(&dqopt
->info
[cnt
]))
612 sb
->dq_op
->write_info(sb
, cnt
);
613 spin_lock(&dq_list_lock
);
615 spin_unlock(&dq_list_lock
);
616 mutex_unlock(&dqopt
->dqonoff_mutex
);
620 EXPORT_SYMBOL(vfs_quota_sync
);
622 /* Free unused dquots from cache */
623 static void prune_dqcache(int count
)
625 struct list_head
*head
;
628 head
= free_dquots
.prev
;
629 while (head
!= &free_dquots
&& count
) {
630 dquot
= list_entry(head
, struct dquot
, dq_free
);
631 remove_dquot_hash(dquot
);
632 remove_free_dquot(dquot
);
634 do_destroy_dquot(dquot
);
636 head
= free_dquots
.prev
;
641 * This is called from kswapd when we think we need some
645 static int shrink_dqcache_memory(int nr
, gfp_t gfp_mask
)
648 spin_lock(&dq_list_lock
);
650 spin_unlock(&dq_list_lock
);
652 return (dqstats
.free_dquots
/ 100) * sysctl_vfs_cache_pressure
;
655 static struct shrinker dqcache_shrinker
= {
656 .shrink
= shrink_dqcache_memory
,
657 .seeks
= DEFAULT_SEEKS
,
661 * Put reference to dquot
662 * NOTE: If you change this function please check whether dqput_blocks() works right...
664 void dqput(struct dquot
*dquot
)
670 #ifdef __DQUOT_PARANOIA
671 if (!atomic_read(&dquot
->dq_count
)) {
672 printk("VFS: dqput: trying to free free dquot\n");
673 printk("VFS: device %s, dquot of %s %d\n",
675 quotatypes
[dquot
->dq_type
],
681 spin_lock(&dq_list_lock
);
683 spin_unlock(&dq_list_lock
);
685 spin_lock(&dq_list_lock
);
686 if (atomic_read(&dquot
->dq_count
) > 1) {
687 /* We have more than one user... nothing to do */
688 atomic_dec(&dquot
->dq_count
);
689 /* Releasing dquot during quotaoff phase? */
690 if (!sb_has_quota_active(dquot
->dq_sb
, dquot
->dq_type
) &&
691 atomic_read(&dquot
->dq_count
) == 1)
692 wake_up(&dquot
->dq_wait_unused
);
693 spin_unlock(&dq_list_lock
);
696 /* Need to release dquot? */
697 if (test_bit(DQ_ACTIVE_B
, &dquot
->dq_flags
) && dquot_dirty(dquot
)) {
698 spin_unlock(&dq_list_lock
);
699 /* Commit dquot before releasing */
700 ret
= dquot
->dq_sb
->dq_op
->write_dquot(dquot
);
702 printk(KERN_ERR
"VFS: cannot write quota structure on "
703 "device %s (error %d). Quota may get out of "
704 "sync!\n", dquot
->dq_sb
->s_id
, ret
);
706 * We clear dirty bit anyway, so that we avoid
709 spin_lock(&dq_list_lock
);
710 clear_dquot_dirty(dquot
);
711 spin_unlock(&dq_list_lock
);
715 /* Clear flag in case dquot was inactive (something bad happened) */
716 clear_dquot_dirty(dquot
);
717 if (test_bit(DQ_ACTIVE_B
, &dquot
->dq_flags
)) {
718 spin_unlock(&dq_list_lock
);
719 dquot
->dq_sb
->dq_op
->release_dquot(dquot
);
722 atomic_dec(&dquot
->dq_count
);
723 #ifdef __DQUOT_PARANOIA
725 BUG_ON(!list_empty(&dquot
->dq_free
));
727 put_dquot_last(dquot
);
728 spin_unlock(&dq_list_lock
);
730 EXPORT_SYMBOL(dqput
);
732 struct dquot
*dquot_alloc(struct super_block
*sb
, int type
)
734 return kmem_cache_zalloc(dquot_cachep
, GFP_NOFS
);
736 EXPORT_SYMBOL(dquot_alloc
);
738 static struct dquot
*get_empty_dquot(struct super_block
*sb
, int type
)
742 dquot
= sb
->dq_op
->alloc_dquot(sb
, type
);
746 mutex_init(&dquot
->dq_lock
);
747 INIT_LIST_HEAD(&dquot
->dq_free
);
748 INIT_LIST_HEAD(&dquot
->dq_inuse
);
749 INIT_HLIST_NODE(&dquot
->dq_hash
);
750 INIT_LIST_HEAD(&dquot
->dq_dirty
);
751 init_waitqueue_head(&dquot
->dq_wait_unused
);
753 dquot
->dq_type
= type
;
754 atomic_set(&dquot
->dq_count
, 1);
760 * Get reference to dquot
762 * Locking is slightly tricky here. We are guarded from parallel quotaoff()
763 * destroying our dquot by:
764 * a) checking for quota flags under dq_list_lock and
765 * b) getting a reference to dquot before we release dq_list_lock
767 struct dquot
*dqget(struct super_block
*sb
, unsigned int id
, int type
)
769 unsigned int hashent
= hashfn(sb
, id
, type
);
770 struct dquot
*dquot
= NULL
, *empty
= NULL
;
772 if (!sb_has_quota_active(sb
, type
))
775 spin_lock(&dq_list_lock
);
776 spin_lock(&dq_state_lock
);
777 if (!sb_has_quota_active(sb
, type
)) {
778 spin_unlock(&dq_state_lock
);
779 spin_unlock(&dq_list_lock
);
782 spin_unlock(&dq_state_lock
);
784 dquot
= find_dquot(hashent
, sb
, id
, type
);
787 spin_unlock(&dq_list_lock
);
788 empty
= get_empty_dquot(sb
, type
);
790 schedule(); /* Try to wait for a moment... */
796 /* all dquots go on the inuse_list */
798 /* hash it first so it can be found */
799 insert_dquot_hash(dquot
);
801 spin_unlock(&dq_list_lock
);
803 if (!atomic_read(&dquot
->dq_count
))
804 remove_free_dquot(dquot
);
805 atomic_inc(&dquot
->dq_count
);
806 dqstats
.cache_hits
++;
808 spin_unlock(&dq_list_lock
);
810 /* Wait for dq_lock - after this we know that either dquot_release() is
811 * already finished or it will be canceled due to dq_count > 1 test */
812 wait_on_dquot(dquot
);
813 /* Read the dquot / allocate space in quota file */
814 if (!test_bit(DQ_ACTIVE_B
, &dquot
->dq_flags
) &&
815 sb
->dq_op
->acquire_dquot(dquot
) < 0) {
820 #ifdef __DQUOT_PARANOIA
821 BUG_ON(!dquot
->dq_sb
); /* Has somebody invalidated entry under us? */
825 do_destroy_dquot(empty
);
829 EXPORT_SYMBOL(dqget
);
831 static int dqinit_needed(struct inode
*inode
, int type
)
835 if (IS_NOQUOTA(inode
))
838 return !inode
->i_dquot
[type
];
839 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++)
840 if (!inode
->i_dquot
[cnt
])
845 /* This routine is guarded by dqonoff_mutex mutex */
846 static void add_dquot_ref(struct super_block
*sb
, int type
)
848 struct inode
*inode
, *old_inode
= NULL
;
851 spin_lock(&inode_lock
);
852 list_for_each_entry(inode
, &sb
->s_inodes
, i_sb_list
) {
853 if (inode
->i_state
& (I_FREEING
|I_CLEAR
|I_WILL_FREE
|I_NEW
))
855 if (unlikely(inode_get_rsv_space(inode
) > 0))
857 if (!atomic_read(&inode
->i_writecount
))
859 if (!dqinit_needed(inode
, type
))
863 spin_unlock(&inode_lock
);
866 sb
->dq_op
->initialize(inode
, type
);
867 /* We hold a reference to 'inode' so it couldn't have been
868 * removed from s_inodes list while we dropped the inode_lock.
869 * We cannot iput the inode now as we can be holding the last
870 * reference and we cannot iput it under inode_lock. So we
871 * keep the reference and iput it later. */
873 spin_lock(&inode_lock
);
875 spin_unlock(&inode_lock
);
879 printk(KERN_WARNING
"VFS (%s): Writes happened before quota"
880 " was turned on thus quota information is probably "
881 "inconsistent. Please run quotacheck(8).\n", sb
->s_id
);
886 * Return 0 if dqput() won't block.
887 * (note that 1 doesn't necessarily mean blocking)
889 static inline int dqput_blocks(struct dquot
*dquot
)
891 if (atomic_read(&dquot
->dq_count
) <= 1)
897 * Remove references to dquots from inode and add dquot to list for freeing
898 * if we have the last referece to dquot
899 * We can't race with anybody because we hold dqptr_sem for writing...
901 static int remove_inode_dquot_ref(struct inode
*inode
, int type
,
902 struct list_head
*tofree_head
)
904 struct dquot
*dquot
= inode
->i_dquot
[type
];
906 inode
->i_dquot
[type
] = NULL
;
908 if (dqput_blocks(dquot
)) {
909 #ifdef __DQUOT_PARANOIA
910 if (atomic_read(&dquot
->dq_count
) != 1)
911 printk(KERN_WARNING
"VFS: Adding dquot with dq_count %d to dispose list.\n", atomic_read(&dquot
->dq_count
));
913 spin_lock(&dq_list_lock
);
914 /* As dquot must have currently users it can't be on
915 * the free list... */
916 list_add(&dquot
->dq_free
, tofree_head
);
917 spin_unlock(&dq_list_lock
);
921 dqput(dquot
); /* We have guaranteed we won't block */
927 * Free list of dquots
928 * Dquots are removed from inodes and no new references can be got so we are
929 * the only ones holding reference
931 static void put_dquot_list(struct list_head
*tofree_head
)
933 struct list_head
*act_head
;
936 act_head
= tofree_head
->next
;
937 while (act_head
!= tofree_head
) {
938 dquot
= list_entry(act_head
, struct dquot
, dq_free
);
939 act_head
= act_head
->next
;
940 /* Remove dquot from the list so we won't have problems... */
941 list_del_init(&dquot
->dq_free
);
946 static void remove_dquot_ref(struct super_block
*sb
, int type
,
947 struct list_head
*tofree_head
)
951 spin_lock(&inode_lock
);
952 list_for_each_entry(inode
, &sb
->s_inodes
, i_sb_list
) {
954 * We have to scan also I_NEW inodes because they can already
955 * have quota pointer initialized. Luckily, we need to touch
956 * only quota pointers and these have separate locking
959 if (!IS_NOQUOTA(inode
))
960 remove_inode_dquot_ref(inode
, type
, tofree_head
);
962 spin_unlock(&inode_lock
);
965 /* Gather all references from inodes and drop them */
966 static void drop_dquot_ref(struct super_block
*sb
, int type
)
968 LIST_HEAD(tofree_head
);
971 down_write(&sb_dqopt(sb
)->dqptr_sem
);
972 remove_dquot_ref(sb
, type
, &tofree_head
);
973 up_write(&sb_dqopt(sb
)->dqptr_sem
);
974 put_dquot_list(&tofree_head
);
978 static inline void dquot_incr_inodes(struct dquot
*dquot
, qsize_t number
)
980 dquot
->dq_dqb
.dqb_curinodes
+= number
;
983 static inline void dquot_incr_space(struct dquot
*dquot
, qsize_t number
)
985 dquot
->dq_dqb
.dqb_curspace
+= number
;
988 static inline void dquot_resv_space(struct dquot
*dquot
, qsize_t number
)
990 dquot
->dq_dqb
.dqb_rsvspace
+= number
;
994 * Claim reserved quota space
996 static void dquot_claim_reserved_space(struct dquot
*dquot
, qsize_t number
)
998 if (dquot
->dq_dqb
.dqb_rsvspace
< number
) {
1000 number
= dquot
->dq_dqb
.dqb_rsvspace
;
1002 dquot
->dq_dqb
.dqb_curspace
+= number
;
1003 dquot
->dq_dqb
.dqb_rsvspace
-= number
;
1007 void dquot_free_reserved_space(struct dquot
*dquot
, qsize_t number
)
1009 if (dquot
->dq_dqb
.dqb_rsvspace
>= number
)
1010 dquot
->dq_dqb
.dqb_rsvspace
-= number
;
1013 dquot
->dq_dqb
.dqb_rsvspace
= 0;
1017 static void dquot_decr_inodes(struct dquot
*dquot
, qsize_t number
)
1019 if (sb_dqopt(dquot
->dq_sb
)->flags
& DQUOT_NEGATIVE_USAGE
||
1020 dquot
->dq_dqb
.dqb_curinodes
>= number
)
1021 dquot
->dq_dqb
.dqb_curinodes
-= number
;
1023 dquot
->dq_dqb
.dqb_curinodes
= 0;
1024 if (dquot
->dq_dqb
.dqb_curinodes
<= dquot
->dq_dqb
.dqb_isoftlimit
)
1025 dquot
->dq_dqb
.dqb_itime
= (time_t) 0;
1026 clear_bit(DQ_INODES_B
, &dquot
->dq_flags
);
1029 static void dquot_decr_space(struct dquot
*dquot
, qsize_t number
)
1031 if (sb_dqopt(dquot
->dq_sb
)->flags
& DQUOT_NEGATIVE_USAGE
||
1032 dquot
->dq_dqb
.dqb_curspace
>= number
)
1033 dquot
->dq_dqb
.dqb_curspace
-= number
;
1035 dquot
->dq_dqb
.dqb_curspace
= 0;
1036 if (dquot
->dq_dqb
.dqb_curspace
<= dquot
->dq_dqb
.dqb_bsoftlimit
)
1037 dquot
->dq_dqb
.dqb_btime
= (time_t) 0;
1038 clear_bit(DQ_BLKS_B
, &dquot
->dq_flags
);
1041 static int warning_issued(struct dquot
*dquot
, const int warntype
)
1043 int flag
= (warntype
== QUOTA_NL_BHARDWARN
||
1044 warntype
== QUOTA_NL_BSOFTLONGWARN
) ? DQ_BLKS_B
:
1045 ((warntype
== QUOTA_NL_IHARDWARN
||
1046 warntype
== QUOTA_NL_ISOFTLONGWARN
) ? DQ_INODES_B
: 0);
1050 return test_and_set_bit(flag
, &dquot
->dq_flags
);
1053 #ifdef CONFIG_PRINT_QUOTA_WARNING
1054 static int flag_print_warnings
= 1;
1056 static int need_print_warning(struct dquot
*dquot
)
1058 if (!flag_print_warnings
)
1061 switch (dquot
->dq_type
) {
1063 return current_fsuid() == dquot
->dq_id
;
1065 return in_group_p(dquot
->dq_id
);
1070 /* Print warning to user which exceeded quota */
1071 static void print_warning(struct dquot
*dquot
, const int warntype
)
1074 struct tty_struct
*tty
;
1076 if (warntype
== QUOTA_NL_IHARDBELOW
||
1077 warntype
== QUOTA_NL_ISOFTBELOW
||
1078 warntype
== QUOTA_NL_BHARDBELOW
||
1079 warntype
== QUOTA_NL_BSOFTBELOW
|| !need_print_warning(dquot
))
1082 tty
= get_current_tty();
1085 tty_write_message(tty
, dquot
->dq_sb
->s_id
);
1086 if (warntype
== QUOTA_NL_ISOFTWARN
|| warntype
== QUOTA_NL_BSOFTWARN
)
1087 tty_write_message(tty
, ": warning, ");
1089 tty_write_message(tty
, ": write failed, ");
1090 tty_write_message(tty
, quotatypes
[dquot
->dq_type
]);
1092 case QUOTA_NL_IHARDWARN
:
1093 msg
= " file limit reached.\r\n";
1095 case QUOTA_NL_ISOFTLONGWARN
:
1096 msg
= " file quota exceeded too long.\r\n";
1098 case QUOTA_NL_ISOFTWARN
:
1099 msg
= " file quota exceeded.\r\n";
1101 case QUOTA_NL_BHARDWARN
:
1102 msg
= " block limit reached.\r\n";
1104 case QUOTA_NL_BSOFTLONGWARN
:
1105 msg
= " block quota exceeded too long.\r\n";
1107 case QUOTA_NL_BSOFTWARN
:
1108 msg
= " block quota exceeded.\r\n";
1111 tty_write_message(tty
, msg
);
1117 * Write warnings to the console and send warning messages over netlink.
1119 * Note that this function can sleep.
1121 static void flush_warnings(struct dquot
*const *dquots
, char *warntype
)
1126 for (i
= 0; i
< MAXQUOTAS
; i
++) {
1128 if (dq
&& warntype
[i
] != QUOTA_NL_NOWARN
&&
1129 !warning_issued(dq
, warntype
[i
])) {
1130 #ifdef CONFIG_PRINT_QUOTA_WARNING
1131 print_warning(dq
, warntype
[i
]);
1133 quota_send_warning(dq
->dq_type
, dq
->dq_id
,
1134 dq
->dq_sb
->s_dev
, warntype
[i
]);
1139 static int ignore_hardlimit(struct dquot
*dquot
)
1141 struct mem_dqinfo
*info
= &sb_dqopt(dquot
->dq_sb
)->info
[dquot
->dq_type
];
1143 return capable(CAP_SYS_RESOURCE
) &&
1144 (info
->dqi_format
->qf_fmt_id
!= QFMT_VFS_OLD
||
1145 !(info
->dqi_flags
& V1_DQF_RSQUASH
));
1148 /* needs dq_data_lock */
1149 static int check_idq(struct dquot
*dquot
, qsize_t inodes
, char *warntype
)
1151 qsize_t newinodes
= dquot
->dq_dqb
.dqb_curinodes
+ inodes
;
1153 *warntype
= QUOTA_NL_NOWARN
;
1154 if (!sb_has_quota_limits_enabled(dquot
->dq_sb
, dquot
->dq_type
) ||
1155 test_bit(DQ_FAKE_B
, &dquot
->dq_flags
))
1158 if (dquot
->dq_dqb
.dqb_ihardlimit
&&
1159 newinodes
> dquot
->dq_dqb
.dqb_ihardlimit
&&
1160 !ignore_hardlimit(dquot
)) {
1161 *warntype
= QUOTA_NL_IHARDWARN
;
1165 if (dquot
->dq_dqb
.dqb_isoftlimit
&&
1166 newinodes
> dquot
->dq_dqb
.dqb_isoftlimit
&&
1167 dquot
->dq_dqb
.dqb_itime
&&
1168 get_seconds() >= dquot
->dq_dqb
.dqb_itime
&&
1169 !ignore_hardlimit(dquot
)) {
1170 *warntype
= QUOTA_NL_ISOFTLONGWARN
;
1174 if (dquot
->dq_dqb
.dqb_isoftlimit
&&
1175 newinodes
> dquot
->dq_dqb
.dqb_isoftlimit
&&
1176 dquot
->dq_dqb
.dqb_itime
== 0) {
1177 *warntype
= QUOTA_NL_ISOFTWARN
;
1178 dquot
->dq_dqb
.dqb_itime
= get_seconds() +
1179 sb_dqopt(dquot
->dq_sb
)->info
[dquot
->dq_type
].dqi_igrace
;
1185 /* needs dq_data_lock */
1186 static int check_bdq(struct dquot
*dquot
, qsize_t space
, int prealloc
, char *warntype
)
1189 struct super_block
*sb
= dquot
->dq_sb
;
1191 *warntype
= QUOTA_NL_NOWARN
;
1192 if (!sb_has_quota_limits_enabled(sb
, dquot
->dq_type
) ||
1193 test_bit(DQ_FAKE_B
, &dquot
->dq_flags
))
1196 tspace
= dquot
->dq_dqb
.dqb_curspace
+ dquot
->dq_dqb
.dqb_rsvspace
1199 if (dquot
->dq_dqb
.dqb_bhardlimit
&&
1200 tspace
> dquot
->dq_dqb
.dqb_bhardlimit
&&
1201 !ignore_hardlimit(dquot
)) {
1203 *warntype
= QUOTA_NL_BHARDWARN
;
1207 if (dquot
->dq_dqb
.dqb_bsoftlimit
&&
1208 tspace
> dquot
->dq_dqb
.dqb_bsoftlimit
&&
1209 dquot
->dq_dqb
.dqb_btime
&&
1210 get_seconds() >= dquot
->dq_dqb
.dqb_btime
&&
1211 !ignore_hardlimit(dquot
)) {
1213 *warntype
= QUOTA_NL_BSOFTLONGWARN
;
1217 if (dquot
->dq_dqb
.dqb_bsoftlimit
&&
1218 tspace
> dquot
->dq_dqb
.dqb_bsoftlimit
&&
1219 dquot
->dq_dqb
.dqb_btime
== 0) {
1221 *warntype
= QUOTA_NL_BSOFTWARN
;
1222 dquot
->dq_dqb
.dqb_btime
= get_seconds() +
1223 sb_dqopt(sb
)->info
[dquot
->dq_type
].dqi_bgrace
;
1227 * We don't allow preallocation to exceed softlimit so exceeding will
1236 static int info_idq_free(struct dquot
*dquot
, qsize_t inodes
)
1240 if (test_bit(DQ_FAKE_B
, &dquot
->dq_flags
) ||
1241 dquot
->dq_dqb
.dqb_curinodes
<= dquot
->dq_dqb
.dqb_isoftlimit
||
1242 !sb_has_quota_limits_enabled(dquot
->dq_sb
, dquot
->dq_type
))
1243 return QUOTA_NL_NOWARN
;
1245 newinodes
= dquot
->dq_dqb
.dqb_curinodes
- inodes
;
1246 if (newinodes
<= dquot
->dq_dqb
.dqb_isoftlimit
)
1247 return QUOTA_NL_ISOFTBELOW
;
1248 if (dquot
->dq_dqb
.dqb_curinodes
>= dquot
->dq_dqb
.dqb_ihardlimit
&&
1249 newinodes
< dquot
->dq_dqb
.dqb_ihardlimit
)
1250 return QUOTA_NL_IHARDBELOW
;
1251 return QUOTA_NL_NOWARN
;
1254 static int info_bdq_free(struct dquot
*dquot
, qsize_t space
)
1256 if (test_bit(DQ_FAKE_B
, &dquot
->dq_flags
) ||
1257 dquot
->dq_dqb
.dqb_curspace
<= dquot
->dq_dqb
.dqb_bsoftlimit
)
1258 return QUOTA_NL_NOWARN
;
1260 if (dquot
->dq_dqb
.dqb_curspace
- space
<= dquot
->dq_dqb
.dqb_bsoftlimit
)
1261 return QUOTA_NL_BSOFTBELOW
;
1262 if (dquot
->dq_dqb
.dqb_curspace
>= dquot
->dq_dqb
.dqb_bhardlimit
&&
1263 dquot
->dq_dqb
.dqb_curspace
- space
< dquot
->dq_dqb
.dqb_bhardlimit
)
1264 return QUOTA_NL_BHARDBELOW
;
1265 return QUOTA_NL_NOWARN
;
1269 * Initialize quota pointers in inode
1270 * We do things in a bit complicated way but by that we avoid calling
1271 * dqget() and thus filesystem callbacks under dqptr_sem.
1273 int dquot_initialize(struct inode
*inode
, int type
)
1275 unsigned int id
= 0;
1277 struct dquot
*got
[MAXQUOTAS
] = { NULL
, NULL
};
1278 struct super_block
*sb
= inode
->i_sb
;
1281 /* First test before acquiring mutex - solves deadlocks when we
1282 * re-enter the quota code and are already holding the mutex */
1283 if (IS_NOQUOTA(inode
))
1286 /* First get references to structures we might need. */
1287 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++) {
1288 if (type
!= -1 && cnt
!= type
)
1298 got
[cnt
] = dqget(sb
, id
, cnt
);
1301 down_write(&sb_dqopt(sb
)->dqptr_sem
);
1302 if (IS_NOQUOTA(inode
))
1304 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++) {
1305 if (type
!= -1 && cnt
!= type
)
1307 /* Avoid races with quotaoff() */
1308 if (!sb_has_quota_active(sb
, cnt
))
1310 if (!inode
->i_dquot
[cnt
]) {
1311 inode
->i_dquot
[cnt
] = got
[cnt
];
1314 * Make quota reservation system happy if someone
1315 * did a write before quota was turned on
1317 rsv
= inode_get_rsv_space(inode
);
1319 dquot_resv_space(inode
->i_dquot
[cnt
], rsv
);
1323 up_write(&sb_dqopt(sb
)->dqptr_sem
);
1324 /* Drop unused references */
1328 EXPORT_SYMBOL(dquot_initialize
);
1331 * Release all quotas referenced by inode
1333 int dquot_drop(struct inode
*inode
)
1336 struct dquot
*put
[MAXQUOTAS
];
1338 down_write(&sb_dqopt(inode
->i_sb
)->dqptr_sem
);
1339 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++) {
1340 put
[cnt
] = inode
->i_dquot
[cnt
];
1341 inode
->i_dquot
[cnt
] = NULL
;
1343 up_write(&sb_dqopt(inode
->i_sb
)->dqptr_sem
);
1347 EXPORT_SYMBOL(dquot_drop
);
1349 /* Wrapper to remove references to quota structures from inode */
1350 void vfs_dq_drop(struct inode
*inode
)
1352 /* Here we can get arbitrary inode from clear_inode() so we have
1353 * to be careful. OTOH we don't need locking as quota operations
1354 * are allowed to change only at mount time */
1355 if (!IS_NOQUOTA(inode
) && inode
->i_sb
&& inode
->i_sb
->dq_op
1356 && inode
->i_sb
->dq_op
->drop
) {
1358 /* Test before calling to rule out calls from proc and such
1359 * where we are not allowed to block. Note that this is
1360 * actually reliable test even without the lock - the caller
1361 * must assure that nobody can come after the DQUOT_DROP and
1362 * add quota pointers back anyway */
1363 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++)
1364 if (inode
->i_dquot
[cnt
])
1366 if (cnt
< MAXQUOTAS
)
1367 inode
->i_sb
->dq_op
->drop(inode
);
1370 EXPORT_SYMBOL(vfs_dq_drop
);
1373 * inode_reserved_space is managed internally by quota, and protected by
1374 * i_lock similar to i_blocks+i_bytes.
1376 static qsize_t
*inode_reserved_space(struct inode
* inode
)
1378 /* Filesystem must explicitly define it's own method in order to use
1379 * quota reservation interface */
1380 BUG_ON(!inode
->i_sb
->dq_op
->get_reserved_space
);
1381 return inode
->i_sb
->dq_op
->get_reserved_space(inode
);
1384 void inode_add_rsv_space(struct inode
*inode
, qsize_t number
)
1386 spin_lock(&inode
->i_lock
);
1387 *inode_reserved_space(inode
) += number
;
1388 spin_unlock(&inode
->i_lock
);
1390 EXPORT_SYMBOL(inode_add_rsv_space
);
1392 void inode_claim_rsv_space(struct inode
*inode
, qsize_t number
)
1394 spin_lock(&inode
->i_lock
);
1395 *inode_reserved_space(inode
) -= number
;
1396 __inode_add_bytes(inode
, number
);
1397 spin_unlock(&inode
->i_lock
);
1399 EXPORT_SYMBOL(inode_claim_rsv_space
);
1401 void inode_sub_rsv_space(struct inode
*inode
, qsize_t number
)
1403 spin_lock(&inode
->i_lock
);
1404 *inode_reserved_space(inode
) -= number
;
1405 spin_unlock(&inode
->i_lock
);
1407 EXPORT_SYMBOL(inode_sub_rsv_space
);
1409 static qsize_t
inode_get_rsv_space(struct inode
*inode
)
1413 if (!inode
->i_sb
->dq_op
->get_reserved_space
)
1415 spin_lock(&inode
->i_lock
);
1416 ret
= *inode_reserved_space(inode
);
1417 spin_unlock(&inode
->i_lock
);
1421 static void inode_incr_space(struct inode
*inode
, qsize_t number
,
1425 inode_add_rsv_space(inode
, number
);
1427 inode_add_bytes(inode
, number
);
1430 static void inode_decr_space(struct inode
*inode
, qsize_t number
, int reserve
)
1433 inode_sub_rsv_space(inode
, number
);
1435 inode_sub_bytes(inode
, number
);
1439 * Following four functions update i_blocks+i_bytes fields and
1440 * quota information (together with appropriate checks)
1441 * NOTE: We absolutely rely on the fact that caller dirties
1442 * the inode (usually macros in quotaops.h care about this) and
1443 * holds a handle for the current transaction so that dquot write and
1444 * inode write go into the same transaction.
1448 * This operation can block, but only after everything is updated
1450 int __dquot_alloc_space(struct inode
*inode
, qsize_t number
,
1451 int warn
, int reserve
)
1453 int cnt
, ret
= QUOTA_OK
;
1454 char warntype
[MAXQUOTAS
];
1457 * First test before acquiring mutex - solves deadlocks when we
1458 * re-enter the quota code and are already holding the mutex
1460 if (IS_NOQUOTA(inode
)) {
1461 inode_incr_space(inode
, number
, reserve
);
1465 down_read(&sb_dqopt(inode
->i_sb
)->dqptr_sem
);
1466 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++)
1467 warntype
[cnt
] = QUOTA_NL_NOWARN
;
1469 spin_lock(&dq_data_lock
);
1470 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++) {
1471 if (!inode
->i_dquot
[cnt
])
1473 if (check_bdq(inode
->i_dquot
[cnt
], number
, warn
, warntype
+cnt
)
1476 spin_unlock(&dq_data_lock
);
1477 goto out_flush_warn
;
1480 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++) {
1481 if (!inode
->i_dquot
[cnt
])
1484 dquot_resv_space(inode
->i_dquot
[cnt
], number
);
1486 dquot_incr_space(inode
->i_dquot
[cnt
], number
);
1488 inode_incr_space(inode
, number
, reserve
);
1489 spin_unlock(&dq_data_lock
);
1492 goto out_flush_warn
;
1493 mark_all_dquot_dirty(inode
->i_dquot
);
1495 flush_warnings(inode
->i_dquot
, warntype
);
1496 up_read(&sb_dqopt(inode
->i_sb
)->dqptr_sem
);
1501 int dquot_alloc_space(struct inode
*inode
, qsize_t number
, int warn
)
1503 return __dquot_alloc_space(inode
, number
, warn
, 0);
1505 EXPORT_SYMBOL(dquot_alloc_space
);
1507 int dquot_reserve_space(struct inode
*inode
, qsize_t number
, int warn
)
1509 return __dquot_alloc_space(inode
, number
, warn
, 1);
1511 EXPORT_SYMBOL(dquot_reserve_space
);
1514 * This operation can block, but only after everything is updated
1516 int dquot_alloc_inode(const struct inode
*inode
, qsize_t number
)
1518 int cnt
, ret
= NO_QUOTA
;
1519 char warntype
[MAXQUOTAS
];
1521 /* First test before acquiring mutex - solves deadlocks when we
1522 * re-enter the quota code and are already holding the mutex */
1523 if (IS_NOQUOTA(inode
))
1525 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++)
1526 warntype
[cnt
] = QUOTA_NL_NOWARN
;
1527 down_read(&sb_dqopt(inode
->i_sb
)->dqptr_sem
);
1528 spin_lock(&dq_data_lock
);
1529 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++) {
1530 if (!inode
->i_dquot
[cnt
])
1532 if (check_idq(inode
->i_dquot
[cnt
], number
, warntype
+cnt
)
1537 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++) {
1538 if (!inode
->i_dquot
[cnt
])
1540 dquot_incr_inodes(inode
->i_dquot
[cnt
], number
);
1544 spin_unlock(&dq_data_lock
);
1545 if (ret
== QUOTA_OK
)
1546 mark_all_dquot_dirty(inode
->i_dquot
);
1547 flush_warnings(inode
->i_dquot
, warntype
);
1548 up_read(&sb_dqopt(inode
->i_sb
)->dqptr_sem
);
1551 EXPORT_SYMBOL(dquot_alloc_inode
);
1553 int dquot_claim_space(struct inode
*inode
, qsize_t number
)
1558 if (IS_NOQUOTA(inode
)) {
1559 inode_claim_rsv_space(inode
, number
);
1563 down_read(&sb_dqopt(inode
->i_sb
)->dqptr_sem
);
1564 spin_lock(&dq_data_lock
);
1565 /* Claim reserved quotas to allocated quotas */
1566 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++) {
1567 if (inode
->i_dquot
[cnt
])
1568 dquot_claim_reserved_space(inode
->i_dquot
[cnt
],
1571 /* Update inode bytes */
1572 inode_claim_rsv_space(inode
, number
);
1573 spin_unlock(&dq_data_lock
);
1574 mark_all_dquot_dirty(inode
->i_dquot
);
1575 up_read(&sb_dqopt(inode
->i_sb
)->dqptr_sem
);
1579 EXPORT_SYMBOL(dquot_claim_space
);
1582 * This operation can block, but only after everything is updated
1584 int __dquot_free_space(struct inode
*inode
, qsize_t number
, int reserve
)
1587 char warntype
[MAXQUOTAS
];
1589 /* First test before acquiring mutex - solves deadlocks when we
1590 * re-enter the quota code and are already holding the mutex */
1591 if (IS_NOQUOTA(inode
)) {
1592 inode_decr_space(inode
, number
, reserve
);
1596 down_read(&sb_dqopt(inode
->i_sb
)->dqptr_sem
);
1597 spin_lock(&dq_data_lock
);
1598 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++) {
1599 if (!inode
->i_dquot
[cnt
])
1601 warntype
[cnt
] = info_bdq_free(inode
->i_dquot
[cnt
], number
);
1603 dquot_free_reserved_space(inode
->i_dquot
[cnt
], number
);
1605 dquot_decr_space(inode
->i_dquot
[cnt
], number
);
1607 inode_decr_space(inode
, number
, reserve
);
1608 spin_unlock(&dq_data_lock
);
1612 mark_all_dquot_dirty(inode
->i_dquot
);
1614 flush_warnings(inode
->i_dquot
, warntype
);
1615 up_read(&sb_dqopt(inode
->i_sb
)->dqptr_sem
);
1619 int dquot_free_space(struct inode
*inode
, qsize_t number
)
1621 return __dquot_free_space(inode
, number
, 0);
1623 EXPORT_SYMBOL(dquot_free_space
);
1626 * Release reserved quota space
1628 void dquot_release_reserved_space(struct inode
*inode
, qsize_t number
)
1630 __dquot_free_space(inode
, number
, 1);
1633 EXPORT_SYMBOL(dquot_release_reserved_space
);
1636 * This operation can block, but only after everything is updated
1638 int dquot_free_inode(const struct inode
*inode
, qsize_t number
)
1641 char warntype
[MAXQUOTAS
];
1643 /* First test before acquiring mutex - solves deadlocks when we
1644 * re-enter the quota code and are already holding the mutex */
1645 if (IS_NOQUOTA(inode
))
1648 down_read(&sb_dqopt(inode
->i_sb
)->dqptr_sem
);
1649 spin_lock(&dq_data_lock
);
1650 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++) {
1651 if (!inode
->i_dquot
[cnt
])
1653 warntype
[cnt
] = info_idq_free(inode
->i_dquot
[cnt
], number
);
1654 dquot_decr_inodes(inode
->i_dquot
[cnt
], number
);
1656 spin_unlock(&dq_data_lock
);
1657 mark_all_dquot_dirty(inode
->i_dquot
);
1658 flush_warnings(inode
->i_dquot
, warntype
);
1659 up_read(&sb_dqopt(inode
->i_sb
)->dqptr_sem
);
1662 EXPORT_SYMBOL(dquot_free_inode
);
1665 * Transfer the number of inode and blocks from one diskquota to an other.
1667 * This operation can block, but only after everything is updated
1668 * A transaction must be started when entering this function.
1670 int dquot_transfer(struct inode
*inode
, struct iattr
*iattr
)
1672 qsize_t space
, cur_space
;
1673 qsize_t rsv_space
= 0;
1674 struct dquot
*transfer_from
[MAXQUOTAS
];
1675 struct dquot
*transfer_to
[MAXQUOTAS
];
1676 int cnt
, ret
= QUOTA_OK
;
1677 int chuid
= iattr
->ia_valid
& ATTR_UID
&& inode
->i_uid
!= iattr
->ia_uid
,
1678 chgid
= iattr
->ia_valid
& ATTR_GID
&& inode
->i_gid
!= iattr
->ia_gid
;
1679 char warntype_to
[MAXQUOTAS
];
1680 char warntype_from_inodes
[MAXQUOTAS
], warntype_from_space
[MAXQUOTAS
];
1682 /* First test before acquiring mutex - solves deadlocks when we
1683 * re-enter the quota code and are already holding the mutex */
1684 if (IS_NOQUOTA(inode
))
1686 /* Initialize the arrays */
1687 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++) {
1688 transfer_from
[cnt
] = NULL
;
1689 transfer_to
[cnt
] = NULL
;
1690 warntype_to
[cnt
] = QUOTA_NL_NOWARN
;
1693 transfer_to
[USRQUOTA
] = dqget(inode
->i_sb
, iattr
->ia_uid
,
1696 transfer_to
[GRPQUOTA
] = dqget(inode
->i_sb
, iattr
->ia_gid
,
1699 down_write(&sb_dqopt(inode
->i_sb
)->dqptr_sem
);
1700 if (IS_NOQUOTA(inode
)) { /* File without quota accounting? */
1701 up_write(&sb_dqopt(inode
->i_sb
)->dqptr_sem
);
1704 spin_lock(&dq_data_lock
);
1705 cur_space
= inode_get_bytes(inode
);
1706 rsv_space
= inode_get_rsv_space(inode
);
1707 space
= cur_space
+ rsv_space
;
1708 /* Build the transfer_from list and check the limits */
1709 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++) {
1710 if (!transfer_to
[cnt
])
1712 transfer_from
[cnt
] = inode
->i_dquot
[cnt
];
1713 if (check_idq(transfer_to
[cnt
], 1, warntype_to
+ cnt
) ==
1714 NO_QUOTA
|| check_bdq(transfer_to
[cnt
], space
, 0,
1715 warntype_to
+ cnt
) == NO_QUOTA
)
1720 * Finally perform the needed transfer from transfer_from to transfer_to
1722 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++) {
1724 * Skip changes for same uid or gid or for turned off quota-type.
1726 if (!transfer_to
[cnt
])
1729 /* Due to IO error we might not have transfer_from[] structure */
1730 if (transfer_from
[cnt
]) {
1731 warntype_from_inodes
[cnt
] =
1732 info_idq_free(transfer_from
[cnt
], 1);
1733 warntype_from_space
[cnt
] =
1734 info_bdq_free(transfer_from
[cnt
], space
);
1735 dquot_decr_inodes(transfer_from
[cnt
], 1);
1736 dquot_decr_space(transfer_from
[cnt
], cur_space
);
1737 dquot_free_reserved_space(transfer_from
[cnt
],
1741 dquot_incr_inodes(transfer_to
[cnt
], 1);
1742 dquot_incr_space(transfer_to
[cnt
], cur_space
);
1743 dquot_resv_space(transfer_to
[cnt
], rsv_space
);
1745 inode
->i_dquot
[cnt
] = transfer_to
[cnt
];
1747 spin_unlock(&dq_data_lock
);
1748 up_write(&sb_dqopt(inode
->i_sb
)->dqptr_sem
);
1750 mark_all_dquot_dirty(transfer_from
);
1751 mark_all_dquot_dirty(transfer_to
);
1752 /* The reference we got is transferred to the inode */
1753 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++)
1754 transfer_to
[cnt
] = NULL
;
1756 flush_warnings(transfer_to
, warntype_to
);
1757 flush_warnings(transfer_from
, warntype_from_inodes
);
1758 flush_warnings(transfer_from
, warntype_from_space
);
1760 dqput_all(transfer_from
);
1761 dqput_all(transfer_to
);
1764 spin_unlock(&dq_data_lock
);
1765 up_write(&sb_dqopt(inode
->i_sb
)->dqptr_sem
);
1766 /* Clear dquot pointers we don't want to dqput() */
1767 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++)
1768 transfer_from
[cnt
] = NULL
;
1772 EXPORT_SYMBOL(dquot_transfer
);
1774 /* Wrapper for transferring ownership of an inode */
1775 int vfs_dq_transfer(struct inode
*inode
, struct iattr
*iattr
)
1777 if (sb_any_quota_active(inode
->i_sb
) && !IS_NOQUOTA(inode
)) {
1779 if (inode
->i_sb
->dq_op
->transfer(inode
, iattr
) == NO_QUOTA
)
1784 EXPORT_SYMBOL(vfs_dq_transfer
);
1787 * Write info of quota file to disk
1789 int dquot_commit_info(struct super_block
*sb
, int type
)
1792 struct quota_info
*dqopt
= sb_dqopt(sb
);
1794 mutex_lock(&dqopt
->dqio_mutex
);
1795 ret
= dqopt
->ops
[type
]->write_file_info(sb
, type
);
1796 mutex_unlock(&dqopt
->dqio_mutex
);
1799 EXPORT_SYMBOL(dquot_commit_info
);
1802 * Definitions of diskquota operations.
1804 const struct dquot_operations dquot_operations
= {
1805 .initialize
= dquot_initialize
,
1807 .alloc_space
= dquot_alloc_space
,
1808 .alloc_inode
= dquot_alloc_inode
,
1809 .free_space
= dquot_free_space
,
1810 .free_inode
= dquot_free_inode
,
1811 .transfer
= dquot_transfer
,
1812 .write_dquot
= dquot_commit
,
1813 .acquire_dquot
= dquot_acquire
,
1814 .release_dquot
= dquot_release
,
1815 .mark_dirty
= dquot_mark_dquot_dirty
,
1816 .write_info
= dquot_commit_info
,
1817 .alloc_dquot
= dquot_alloc
,
1818 .destroy_dquot
= dquot_destroy
,
1822 * Turn quota off on a device. type == -1 ==> quotaoff for all types (umount)
1824 int vfs_quota_disable(struct super_block
*sb
, int type
, unsigned int flags
)
1827 struct quota_info
*dqopt
= sb_dqopt(sb
);
1828 struct inode
*toputinode
[MAXQUOTAS
];
1830 /* Cannot turn off usage accounting without turning off limits, or
1831 * suspend quotas and simultaneously turn quotas off. */
1832 if ((flags
& DQUOT_USAGE_ENABLED
&& !(flags
& DQUOT_LIMITS_ENABLED
))
1833 || (flags
& DQUOT_SUSPENDED
&& flags
& (DQUOT_LIMITS_ENABLED
|
1834 DQUOT_USAGE_ENABLED
)))
1837 /* We need to serialize quota_off() for device */
1838 mutex_lock(&dqopt
->dqonoff_mutex
);
1841 * Skip everything if there's nothing to do. We have to do this because
1842 * sometimes we are called when fill_super() failed and calling
1843 * sync_fs() in such cases does no good.
1845 if (!sb_any_quota_loaded(sb
)) {
1846 mutex_unlock(&dqopt
->dqonoff_mutex
);
1849 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++) {
1850 toputinode
[cnt
] = NULL
;
1851 if (type
!= -1 && cnt
!= type
)
1853 if (!sb_has_quota_loaded(sb
, cnt
))
1856 if (flags
& DQUOT_SUSPENDED
) {
1857 spin_lock(&dq_state_lock
);
1859 dquot_state_flag(DQUOT_SUSPENDED
, cnt
);
1860 spin_unlock(&dq_state_lock
);
1862 spin_lock(&dq_state_lock
);
1863 dqopt
->flags
&= ~dquot_state_flag(flags
, cnt
);
1864 /* Turning off suspended quotas? */
1865 if (!sb_has_quota_loaded(sb
, cnt
) &&
1866 sb_has_quota_suspended(sb
, cnt
)) {
1867 dqopt
->flags
&= ~dquot_state_flag(
1868 DQUOT_SUSPENDED
, cnt
);
1869 spin_unlock(&dq_state_lock
);
1870 iput(dqopt
->files
[cnt
]);
1871 dqopt
->files
[cnt
] = NULL
;
1874 spin_unlock(&dq_state_lock
);
1877 /* We still have to keep quota loaded? */
1878 if (sb_has_quota_loaded(sb
, cnt
) && !(flags
& DQUOT_SUSPENDED
))
1881 /* Note: these are blocking operations */
1882 drop_dquot_ref(sb
, cnt
);
1883 invalidate_dquots(sb
, cnt
);
1885 * Now all dquots should be invalidated, all writes done so we
1886 * should be only users of the info. No locks needed.
1888 if (info_dirty(&dqopt
->info
[cnt
]))
1889 sb
->dq_op
->write_info(sb
, cnt
);
1890 if (dqopt
->ops
[cnt
]->free_file_info
)
1891 dqopt
->ops
[cnt
]->free_file_info(sb
, cnt
);
1892 put_quota_format(dqopt
->info
[cnt
].dqi_format
);
1894 toputinode
[cnt
] = dqopt
->files
[cnt
];
1895 if (!sb_has_quota_loaded(sb
, cnt
))
1896 dqopt
->files
[cnt
] = NULL
;
1897 dqopt
->info
[cnt
].dqi_flags
= 0;
1898 dqopt
->info
[cnt
].dqi_igrace
= 0;
1899 dqopt
->info
[cnt
].dqi_bgrace
= 0;
1900 dqopt
->ops
[cnt
] = NULL
;
1902 mutex_unlock(&dqopt
->dqonoff_mutex
);
1904 /* Skip syncing and setting flags if quota files are hidden */
1905 if (dqopt
->flags
& DQUOT_QUOTA_SYS_FILE
)
1908 /* Sync the superblock so that buffers with quota data are written to
1909 * disk (and so userspace sees correct data afterwards). */
1910 if (sb
->s_op
->sync_fs
)
1911 sb
->s_op
->sync_fs(sb
, 1);
1912 sync_blockdev(sb
->s_bdev
);
1913 /* Now the quota files are just ordinary files and we can set the
1914 * inode flags back. Moreover we discard the pagecache so that
1915 * userspace sees the writes we did bypassing the pagecache. We
1916 * must also discard the blockdev buffers so that we see the
1917 * changes done by userspace on the next quotaon() */
1918 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++)
1919 if (toputinode
[cnt
]) {
1920 mutex_lock(&dqopt
->dqonoff_mutex
);
1921 /* If quota was reenabled in the meantime, we have
1923 if (!sb_has_quota_loaded(sb
, cnt
)) {
1924 mutex_lock_nested(&toputinode
[cnt
]->i_mutex
,
1926 toputinode
[cnt
]->i_flags
&= ~(S_IMMUTABLE
|
1927 S_NOATIME
| S_NOQUOTA
);
1928 truncate_inode_pages(&toputinode
[cnt
]->i_data
,
1930 mutex_unlock(&toputinode
[cnt
]->i_mutex
);
1931 mark_inode_dirty(toputinode
[cnt
]);
1933 mutex_unlock(&dqopt
->dqonoff_mutex
);
1936 invalidate_bdev(sb
->s_bdev
);
1938 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++)
1939 if (toputinode
[cnt
]) {
1940 /* On remount RO, we keep the inode pointer so that we
1941 * can reenable quota on the subsequent remount RW. We
1942 * have to check 'flags' variable and not use sb_has_
1943 * function because another quotaon / quotaoff could
1944 * change global state before we got here. We refuse
1945 * to suspend quotas when there is pending delete on
1946 * the quota file... */
1947 if (!(flags
& DQUOT_SUSPENDED
))
1948 iput(toputinode
[cnt
]);
1949 else if (!toputinode
[cnt
]->i_nlink
)
1954 EXPORT_SYMBOL(vfs_quota_disable
);
1956 int vfs_quota_off(struct super_block
*sb
, int type
, int remount
)
1958 return vfs_quota_disable(sb
, type
, remount
? DQUOT_SUSPENDED
:
1959 (DQUOT_USAGE_ENABLED
| DQUOT_LIMITS_ENABLED
));
1961 EXPORT_SYMBOL(vfs_quota_off
);
1963 * Turn quotas on on a device
1967 * Helper function to turn quotas on when we already have the inode of
1968 * quota file and no quota information is loaded.
1970 static int vfs_load_quota_inode(struct inode
*inode
, int type
, int format_id
,
1973 struct quota_format_type
*fmt
= find_quota_format(format_id
);
1974 struct super_block
*sb
= inode
->i_sb
;
1975 struct quota_info
*dqopt
= sb_dqopt(sb
);
1981 if (!S_ISREG(inode
->i_mode
)) {
1985 if (IS_RDONLY(inode
)) {
1989 if (!sb
->s_op
->quota_write
|| !sb
->s_op
->quota_read
) {
1993 /* Usage always has to be set... */
1994 if (!(flags
& DQUOT_USAGE_ENABLED
)) {
1999 if (!(dqopt
->flags
& DQUOT_QUOTA_SYS_FILE
)) {
2000 /* As we bypass the pagecache we must now flush the inode so
2001 * that we see all the changes from userspace... */
2002 write_inode_now(inode
, 1);
2003 /* And now flush the block cache so that kernel sees the
2005 invalidate_bdev(sb
->s_bdev
);
2007 mutex_lock(&dqopt
->dqonoff_mutex
);
2008 if (sb_has_quota_loaded(sb
, type
)) {
2013 if (!(dqopt
->flags
& DQUOT_QUOTA_SYS_FILE
)) {
2014 /* We don't want quota and atime on quota files (deadlocks
2015 * possible) Also nobody should write to the file - we use
2016 * special IO operations which ignore the immutable bit. */
2017 mutex_lock_nested(&inode
->i_mutex
, I_MUTEX_QUOTA
);
2018 oldflags
= inode
->i_flags
& (S_NOATIME
| S_IMMUTABLE
|
2020 inode
->i_flags
|= S_NOQUOTA
| S_NOATIME
| S_IMMUTABLE
;
2021 mutex_unlock(&inode
->i_mutex
);
2023 * When S_NOQUOTA is set, remove dquot references as no more
2024 * references can be added
2026 sb
->dq_op
->drop(inode
);
2030 dqopt
->files
[type
] = igrab(inode
);
2031 if (!dqopt
->files
[type
])
2034 if (!fmt
->qf_ops
->check_quota_file(sb
, type
))
2037 dqopt
->ops
[type
] = fmt
->qf_ops
;
2038 dqopt
->info
[type
].dqi_format
= fmt
;
2039 dqopt
->info
[type
].dqi_fmt_id
= format_id
;
2040 INIT_LIST_HEAD(&dqopt
->info
[type
].dqi_dirty_list
);
2041 mutex_lock(&dqopt
->dqio_mutex
);
2042 error
= dqopt
->ops
[type
]->read_file_info(sb
, type
);
2044 mutex_unlock(&dqopt
->dqio_mutex
);
2047 mutex_unlock(&dqopt
->dqio_mutex
);
2048 spin_lock(&dq_state_lock
);
2049 dqopt
->flags
|= dquot_state_flag(flags
, type
);
2050 spin_unlock(&dq_state_lock
);
2052 add_dquot_ref(sb
, type
);
2053 mutex_unlock(&dqopt
->dqonoff_mutex
);
2058 dqopt
->files
[type
] = NULL
;
2061 if (oldflags
!= -1) {
2062 mutex_lock_nested(&inode
->i_mutex
, I_MUTEX_QUOTA
);
2063 /* Set the flags back (in the case of accidental quotaon()
2064 * on a wrong file we don't want to mess up the flags) */
2065 inode
->i_flags
&= ~(S_NOATIME
| S_NOQUOTA
| S_IMMUTABLE
);
2066 inode
->i_flags
|= oldflags
;
2067 mutex_unlock(&inode
->i_mutex
);
2069 mutex_unlock(&dqopt
->dqonoff_mutex
);
2071 put_quota_format(fmt
);
2076 /* Reenable quotas on remount RW */
2077 static int vfs_quota_on_remount(struct super_block
*sb
, int type
)
2079 struct quota_info
*dqopt
= sb_dqopt(sb
);
2080 struct inode
*inode
;
2084 mutex_lock(&dqopt
->dqonoff_mutex
);
2085 if (!sb_has_quota_suspended(sb
, type
)) {
2086 mutex_unlock(&dqopt
->dqonoff_mutex
);
2089 inode
= dqopt
->files
[type
];
2090 dqopt
->files
[type
] = NULL
;
2091 spin_lock(&dq_state_lock
);
2092 flags
= dqopt
->flags
& dquot_state_flag(DQUOT_USAGE_ENABLED
|
2093 DQUOT_LIMITS_ENABLED
, type
);
2094 dqopt
->flags
&= ~dquot_state_flag(DQUOT_STATE_FLAGS
, type
);
2095 spin_unlock(&dq_state_lock
);
2096 mutex_unlock(&dqopt
->dqonoff_mutex
);
2098 flags
= dquot_generic_flag(flags
, type
);
2099 ret
= vfs_load_quota_inode(inode
, type
, dqopt
->info
[type
].dqi_fmt_id
,
2106 int vfs_quota_on_path(struct super_block
*sb
, int type
, int format_id
,
2109 int error
= security_quota_on(path
->dentry
);
2112 /* Quota file not on the same filesystem? */
2113 if (path
->mnt
->mnt_sb
!= sb
)
2116 error
= vfs_load_quota_inode(path
->dentry
->d_inode
, type
,
2117 format_id
, DQUOT_USAGE_ENABLED
|
2118 DQUOT_LIMITS_ENABLED
);
2121 EXPORT_SYMBOL(vfs_quota_on_path
);
2123 int vfs_quota_on(struct super_block
*sb
, int type
, int format_id
, char *name
,
2130 return vfs_quota_on_remount(sb
, type
);
2132 error
= kern_path(name
, LOOKUP_FOLLOW
, &path
);
2134 error
= vfs_quota_on_path(sb
, type
, format_id
, &path
);
2139 EXPORT_SYMBOL(vfs_quota_on
);
2142 * More powerful function for turning on quotas allowing setting
2143 * of individual quota flags
2145 int vfs_quota_enable(struct inode
*inode
, int type
, int format_id
,
2149 struct super_block
*sb
= inode
->i_sb
;
2150 struct quota_info
*dqopt
= sb_dqopt(sb
);
2152 /* Just unsuspend quotas? */
2153 if (flags
& DQUOT_SUSPENDED
)
2154 return vfs_quota_on_remount(sb
, type
);
2157 /* Just updating flags needed? */
2158 if (sb_has_quota_loaded(sb
, type
)) {
2159 mutex_lock(&dqopt
->dqonoff_mutex
);
2160 /* Now do a reliable test... */
2161 if (!sb_has_quota_loaded(sb
, type
)) {
2162 mutex_unlock(&dqopt
->dqonoff_mutex
);
2165 if (flags
& DQUOT_USAGE_ENABLED
&&
2166 sb_has_quota_usage_enabled(sb
, type
)) {
2170 if (flags
& DQUOT_LIMITS_ENABLED
&&
2171 sb_has_quota_limits_enabled(sb
, type
)) {
2175 spin_lock(&dq_state_lock
);
2176 sb_dqopt(sb
)->flags
|= dquot_state_flag(flags
, type
);
2177 spin_unlock(&dq_state_lock
);
2179 mutex_unlock(&dqopt
->dqonoff_mutex
);
2184 return vfs_load_quota_inode(inode
, type
, format_id
, flags
);
2186 EXPORT_SYMBOL(vfs_quota_enable
);
2189 * This function is used when filesystem needs to initialize quotas
2190 * during mount time.
2192 int vfs_quota_on_mount(struct super_block
*sb
, char *qf_name
,
2193 int format_id
, int type
)
2195 struct dentry
*dentry
;
2198 mutex_lock(&sb
->s_root
->d_inode
->i_mutex
);
2199 dentry
= lookup_one_len(qf_name
, sb
->s_root
, strlen(qf_name
));
2200 mutex_unlock(&sb
->s_root
->d_inode
->i_mutex
);
2202 return PTR_ERR(dentry
);
2204 if (!dentry
->d_inode
) {
2209 error
= security_quota_on(dentry
);
2211 error
= vfs_load_quota_inode(dentry
->d_inode
, type
, format_id
,
2212 DQUOT_USAGE_ENABLED
| DQUOT_LIMITS_ENABLED
);
2218 EXPORT_SYMBOL(vfs_quota_on_mount
);
2220 /* Wrapper to turn on quotas when remounting rw */
2221 int vfs_dq_quota_on_remount(struct super_block
*sb
)
2226 if (!sb
->s_qcop
|| !sb
->s_qcop
->quota_on
)
2228 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++) {
2229 err
= sb
->s_qcop
->quota_on(sb
, cnt
, 0, NULL
, 1);
2230 if (err
< 0 && !ret
)
2235 EXPORT_SYMBOL(vfs_dq_quota_on_remount
);
2237 static inline qsize_t
qbtos(qsize_t blocks
)
2239 return blocks
<< QIF_DQBLKSIZE_BITS
;
2242 static inline qsize_t
stoqb(qsize_t space
)
2244 return (space
+ QIF_DQBLKSIZE
- 1) >> QIF_DQBLKSIZE_BITS
;
2247 /* Generic routine for getting common part of quota structure */
2248 static void do_get_dqblk(struct dquot
*dquot
, struct if_dqblk
*di
)
2250 struct mem_dqblk
*dm
= &dquot
->dq_dqb
;
2252 spin_lock(&dq_data_lock
);
2253 di
->dqb_bhardlimit
= stoqb(dm
->dqb_bhardlimit
);
2254 di
->dqb_bsoftlimit
= stoqb(dm
->dqb_bsoftlimit
);
2255 di
->dqb_curspace
= dm
->dqb_curspace
+ dm
->dqb_rsvspace
;
2256 di
->dqb_ihardlimit
= dm
->dqb_ihardlimit
;
2257 di
->dqb_isoftlimit
= dm
->dqb_isoftlimit
;
2258 di
->dqb_curinodes
= dm
->dqb_curinodes
;
2259 di
->dqb_btime
= dm
->dqb_btime
;
2260 di
->dqb_itime
= dm
->dqb_itime
;
2261 di
->dqb_valid
= QIF_ALL
;
2262 spin_unlock(&dq_data_lock
);
2265 int vfs_get_dqblk(struct super_block
*sb
, int type
, qid_t id
,
2266 struct if_dqblk
*di
)
2268 struct dquot
*dquot
;
2270 dquot
= dqget(sb
, id
, type
);
2273 do_get_dqblk(dquot
, di
);
2278 EXPORT_SYMBOL(vfs_get_dqblk
);
2280 /* Generic routine for setting common part of quota structure */
2281 static int do_set_dqblk(struct dquot
*dquot
, struct if_dqblk
*di
)
2283 struct mem_dqblk
*dm
= &dquot
->dq_dqb
;
2284 int check_blim
= 0, check_ilim
= 0;
2285 struct mem_dqinfo
*dqi
= &sb_dqopt(dquot
->dq_sb
)->info
[dquot
->dq_type
];
2287 if ((di
->dqb_valid
& QIF_BLIMITS
&&
2288 (di
->dqb_bhardlimit
> dqi
->dqi_maxblimit
||
2289 di
->dqb_bsoftlimit
> dqi
->dqi_maxblimit
)) ||
2290 (di
->dqb_valid
& QIF_ILIMITS
&&
2291 (di
->dqb_ihardlimit
> dqi
->dqi_maxilimit
||
2292 di
->dqb_isoftlimit
> dqi
->dqi_maxilimit
)))
2295 spin_lock(&dq_data_lock
);
2296 if (di
->dqb_valid
& QIF_SPACE
) {
2297 dm
->dqb_curspace
= di
->dqb_curspace
- dm
->dqb_rsvspace
;
2299 __set_bit(DQ_LASTSET_B
+ QIF_SPACE_B
, &dquot
->dq_flags
);
2301 if (di
->dqb_valid
& QIF_BLIMITS
) {
2302 dm
->dqb_bsoftlimit
= qbtos(di
->dqb_bsoftlimit
);
2303 dm
->dqb_bhardlimit
= qbtos(di
->dqb_bhardlimit
);
2305 __set_bit(DQ_LASTSET_B
+ QIF_BLIMITS_B
, &dquot
->dq_flags
);
2307 if (di
->dqb_valid
& QIF_INODES
) {
2308 dm
->dqb_curinodes
= di
->dqb_curinodes
;
2310 __set_bit(DQ_LASTSET_B
+ QIF_INODES_B
, &dquot
->dq_flags
);
2312 if (di
->dqb_valid
& QIF_ILIMITS
) {
2313 dm
->dqb_isoftlimit
= di
->dqb_isoftlimit
;
2314 dm
->dqb_ihardlimit
= di
->dqb_ihardlimit
;
2316 __set_bit(DQ_LASTSET_B
+ QIF_ILIMITS_B
, &dquot
->dq_flags
);
2318 if (di
->dqb_valid
& QIF_BTIME
) {
2319 dm
->dqb_btime
= di
->dqb_btime
;
2321 __set_bit(DQ_LASTSET_B
+ QIF_BTIME_B
, &dquot
->dq_flags
);
2323 if (di
->dqb_valid
& QIF_ITIME
) {
2324 dm
->dqb_itime
= di
->dqb_itime
;
2326 __set_bit(DQ_LASTSET_B
+ QIF_ITIME_B
, &dquot
->dq_flags
);
2330 if (!dm
->dqb_bsoftlimit
||
2331 dm
->dqb_curspace
< dm
->dqb_bsoftlimit
) {
2333 clear_bit(DQ_BLKS_B
, &dquot
->dq_flags
);
2334 } else if (!(di
->dqb_valid
& QIF_BTIME
))
2335 /* Set grace only if user hasn't provided his own... */
2336 dm
->dqb_btime
= get_seconds() + dqi
->dqi_bgrace
;
2339 if (!dm
->dqb_isoftlimit
||
2340 dm
->dqb_curinodes
< dm
->dqb_isoftlimit
) {
2342 clear_bit(DQ_INODES_B
, &dquot
->dq_flags
);
2343 } else if (!(di
->dqb_valid
& QIF_ITIME
))
2344 /* Set grace only if user hasn't provided his own... */
2345 dm
->dqb_itime
= get_seconds() + dqi
->dqi_igrace
;
2347 if (dm
->dqb_bhardlimit
|| dm
->dqb_bsoftlimit
|| dm
->dqb_ihardlimit
||
2349 clear_bit(DQ_FAKE_B
, &dquot
->dq_flags
);
2351 set_bit(DQ_FAKE_B
, &dquot
->dq_flags
);
2352 spin_unlock(&dq_data_lock
);
2353 mark_dquot_dirty(dquot
);
2358 int vfs_set_dqblk(struct super_block
*sb
, int type
, qid_t id
,
2359 struct if_dqblk
*di
)
2361 struct dquot
*dquot
;
2364 dquot
= dqget(sb
, id
, type
);
2369 rc
= do_set_dqblk(dquot
, di
);
2374 EXPORT_SYMBOL(vfs_set_dqblk
);
2376 /* Generic routine for getting common part of quota file information */
2377 int vfs_get_dqinfo(struct super_block
*sb
, int type
, struct if_dqinfo
*ii
)
2379 struct mem_dqinfo
*mi
;
2381 mutex_lock(&sb_dqopt(sb
)->dqonoff_mutex
);
2382 if (!sb_has_quota_active(sb
, type
)) {
2383 mutex_unlock(&sb_dqopt(sb
)->dqonoff_mutex
);
2386 mi
= sb_dqopt(sb
)->info
+ type
;
2387 spin_lock(&dq_data_lock
);
2388 ii
->dqi_bgrace
= mi
->dqi_bgrace
;
2389 ii
->dqi_igrace
= mi
->dqi_igrace
;
2390 ii
->dqi_flags
= mi
->dqi_flags
& DQF_MASK
;
2391 ii
->dqi_valid
= IIF_ALL
;
2392 spin_unlock(&dq_data_lock
);
2393 mutex_unlock(&sb_dqopt(sb
)->dqonoff_mutex
);
2396 EXPORT_SYMBOL(vfs_get_dqinfo
);
2398 /* Generic routine for setting common part of quota file information */
2399 int vfs_set_dqinfo(struct super_block
*sb
, int type
, struct if_dqinfo
*ii
)
2401 struct mem_dqinfo
*mi
;
2404 mutex_lock(&sb_dqopt(sb
)->dqonoff_mutex
);
2405 if (!sb_has_quota_active(sb
, type
)) {
2409 mi
= sb_dqopt(sb
)->info
+ type
;
2410 spin_lock(&dq_data_lock
);
2411 if (ii
->dqi_valid
& IIF_BGRACE
)
2412 mi
->dqi_bgrace
= ii
->dqi_bgrace
;
2413 if (ii
->dqi_valid
& IIF_IGRACE
)
2414 mi
->dqi_igrace
= ii
->dqi_igrace
;
2415 if (ii
->dqi_valid
& IIF_FLAGS
)
2416 mi
->dqi_flags
= (mi
->dqi_flags
& ~DQF_MASK
) |
2417 (ii
->dqi_flags
& DQF_MASK
);
2418 spin_unlock(&dq_data_lock
);
2419 mark_info_dirty(sb
, type
);
2420 /* Force write to disk */
2421 sb
->dq_op
->write_info(sb
, type
);
2423 mutex_unlock(&sb_dqopt(sb
)->dqonoff_mutex
);
2426 EXPORT_SYMBOL(vfs_set_dqinfo
);
2428 const struct quotactl_ops vfs_quotactl_ops
= {
2429 .quota_on
= vfs_quota_on
,
2430 .quota_off
= vfs_quota_off
,
2431 .quota_sync
= vfs_quota_sync
,
2432 .get_info
= vfs_get_dqinfo
,
2433 .set_info
= vfs_set_dqinfo
,
2434 .get_dqblk
= vfs_get_dqblk
,
2435 .set_dqblk
= vfs_set_dqblk
2438 static ctl_table fs_dqstats_table
[] = {
2440 .procname
= "lookups",
2441 .data
= &dqstats
.lookups
,
2442 .maxlen
= sizeof(int),
2444 .proc_handler
= proc_dointvec
,
2447 .procname
= "drops",
2448 .data
= &dqstats
.drops
,
2449 .maxlen
= sizeof(int),
2451 .proc_handler
= proc_dointvec
,
2454 .procname
= "reads",
2455 .data
= &dqstats
.reads
,
2456 .maxlen
= sizeof(int),
2458 .proc_handler
= proc_dointvec
,
2461 .procname
= "writes",
2462 .data
= &dqstats
.writes
,
2463 .maxlen
= sizeof(int),
2465 .proc_handler
= proc_dointvec
,
2468 .procname
= "cache_hits",
2469 .data
= &dqstats
.cache_hits
,
2470 .maxlen
= sizeof(int),
2472 .proc_handler
= proc_dointvec
,
2475 .procname
= "allocated_dquots",
2476 .data
= &dqstats
.allocated_dquots
,
2477 .maxlen
= sizeof(int),
2479 .proc_handler
= proc_dointvec
,
2482 .procname
= "free_dquots",
2483 .data
= &dqstats
.free_dquots
,
2484 .maxlen
= sizeof(int),
2486 .proc_handler
= proc_dointvec
,
2489 .procname
= "syncs",
2490 .data
= &dqstats
.syncs
,
2491 .maxlen
= sizeof(int),
2493 .proc_handler
= proc_dointvec
,
2495 #ifdef CONFIG_PRINT_QUOTA_WARNING
2497 .procname
= "warnings",
2498 .data
= &flag_print_warnings
,
2499 .maxlen
= sizeof(int),
2501 .proc_handler
= proc_dointvec
,
2507 static ctl_table fs_table
[] = {
2509 .procname
= "quota",
2511 .child
= fs_dqstats_table
,
2516 static ctl_table sys_table
[] = {
2525 static int __init
dquot_init(void)
2528 unsigned long nr_hash
, order
;
2530 printk(KERN_NOTICE
"VFS: Disk quotas %s\n", __DQUOT_VERSION__
);
2532 register_sysctl_table(sys_table
);
2534 dquot_cachep
= kmem_cache_create("dquot",
2535 sizeof(struct dquot
), sizeof(unsigned long) * 4,
2536 (SLAB_HWCACHE_ALIGN
|SLAB_RECLAIM_ACCOUNT
|
2537 SLAB_MEM_SPREAD
|SLAB_PANIC
),
2541 dquot_hash
= (struct hlist_head
*)__get_free_pages(GFP_ATOMIC
, order
);
2543 panic("Cannot create dquot hash table");
2545 /* Find power-of-two hlist_heads which can fit into allocation */
2546 nr_hash
= (1UL << order
) * PAGE_SIZE
/ sizeof(struct hlist_head
);
2550 } while (nr_hash
>> dq_hash_bits
);
2553 nr_hash
= 1UL << dq_hash_bits
;
2554 dq_hash_mask
= nr_hash
- 1;
2555 for (i
= 0; i
< nr_hash
; i
++)
2556 INIT_HLIST_HEAD(dquot_hash
+ i
);
2558 printk("Dquot-cache hash table entries: %ld (order %ld, %ld bytes)\n",
2559 nr_hash
, order
, (PAGE_SIZE
<< order
));
2561 register_shrinker(&dqcache_shrinker
);
2565 module_init(dquot_init
);