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>
84 * There are three quota SMP locks. dq_list_lock protects all lists with quotas
86 * dq_data_lock protects data from dq_dqb and also mem_dqinfo structures and
87 * also guards consistency of dquot->dq_dqb with inode->i_blocks, i_bytes.
88 * i_blocks and i_bytes updates itself are guarded by i_lock acquired directly
89 * in inode_add_bytes() and inode_sub_bytes(). dq_state_lock protects
90 * modifications of quota state (on quotaon and quotaoff) and readers who care
91 * about latest values take it as well.
93 * The spinlock ordering is hence: dq_data_lock > dq_list_lock > i_lock,
94 * dq_list_lock > dq_state_lock
96 * Note that some things (eg. sb pointer, type, id) doesn't change during
97 * the life of the dquot structure and so needn't to be protected by a lock
99 * Any operation working on dquots via inode pointers must hold dqptr_sem. If
100 * operation is just reading pointers from inode (or not using them at all) the
101 * read lock is enough. If pointers are altered function must hold write lock.
102 * Special care needs to be taken about S_NOQUOTA inode flag (marking that
103 * inode is a quota file). Functions adding pointers from inode to dquots have
104 * to check this flag under dqptr_sem and then (if S_NOQUOTA is not set) they
105 * have to do all pointer modifications before dropping dqptr_sem. This makes
106 * sure they cannot race with quotaon which first sets S_NOQUOTA flag and
107 * then drops all pointers to dquots from an inode.
109 * Each dquot has its dq_lock mutex. Locked dquots might not be referenced
110 * from inodes (dquot_alloc_space() and such don't check the dq_lock).
111 * Currently dquot is locked only when it is being read to memory (or space for
112 * it is being allocated) on the first dqget() and when it is being released on
113 * the last dqput(). The allocation and release oparations are serialized by
114 * the dq_lock and by checking the use count in dquot_release(). Write
115 * operations on dquots don't hold dq_lock as they copy data under dq_data_lock
116 * spinlock to internal buffers before writing.
118 * Lock ordering (including related VFS locks) is the following:
119 * i_mutex > dqonoff_sem > journal_lock > dqptr_sem > dquot->dq_lock >
121 * The lock ordering of dqptr_sem imposed by quota code is only dqonoff_sem >
122 * dqptr_sem. But filesystem has to count with the fact that functions such as
123 * dquot_alloc_space() acquire dqptr_sem and they usually have to be called
124 * from inside a transaction to keep filesystem consistency after a crash. Also
125 * filesystems usually want to do some IO on dquot from ->mark_dirty which is
126 * called with dqptr_sem held.
127 * i_mutex on quota files is special (it's below dqio_mutex)
130 static __cacheline_aligned_in_smp
DEFINE_SPINLOCK(dq_list_lock
);
131 static __cacheline_aligned_in_smp
DEFINE_SPINLOCK(dq_state_lock
);
132 __cacheline_aligned_in_smp
DEFINE_SPINLOCK(dq_data_lock
);
133 EXPORT_SYMBOL(dq_data_lock
);
135 #if defined(CONFIG_QUOTA_DEBUG) || defined(CONFIG_PRINT_QUOTA_WARNING)
136 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
);
233 static void __dquot_initialize(struct inode
*inode
, int type
);
235 static inline unsigned int
236 hashfn(const struct super_block
*sb
, unsigned int id
, int type
)
240 tmp
= (((unsigned long)sb
>>L1_CACHE_SHIFT
) ^ id
) * (MAXQUOTAS
- type
);
241 return (tmp
+ (tmp
>> dq_hash_bits
)) & dq_hash_mask
;
245 * Following list functions expect dq_list_lock to be held
247 static inline void insert_dquot_hash(struct dquot
*dquot
)
249 struct hlist_head
*head
;
250 head
= dquot_hash
+ hashfn(dquot
->dq_sb
, dquot
->dq_id
, dquot
->dq_type
);
251 hlist_add_head(&dquot
->dq_hash
, head
);
254 static inline void remove_dquot_hash(struct dquot
*dquot
)
256 hlist_del_init(&dquot
->dq_hash
);
259 static struct dquot
*find_dquot(unsigned int hashent
, struct super_block
*sb
,
260 unsigned int id
, int type
)
262 struct hlist_node
*node
;
265 hlist_for_each (node
, dquot_hash
+hashent
) {
266 dquot
= hlist_entry(node
, struct dquot
, dq_hash
);
267 if (dquot
->dq_sb
== sb
&& dquot
->dq_id
== id
&&
268 dquot
->dq_type
== type
)
274 /* Add a dquot to the tail of the free list */
275 static inline void put_dquot_last(struct dquot
*dquot
)
277 list_add_tail(&dquot
->dq_free
, &free_dquots
);
278 dqstats_inc(DQST_FREE_DQUOTS
);
281 static inline void remove_free_dquot(struct dquot
*dquot
)
283 if (list_empty(&dquot
->dq_free
))
285 list_del_init(&dquot
->dq_free
);
286 dqstats_dec(DQST_FREE_DQUOTS
);
289 static inline void put_inuse(struct dquot
*dquot
)
291 /* We add to the back of inuse list so we don't have to restart
292 * when traversing this list and we block */
293 list_add_tail(&dquot
->dq_inuse
, &inuse_list
);
294 dqstats_inc(DQST_ALLOC_DQUOTS
);
297 static inline void remove_inuse(struct dquot
*dquot
)
299 dqstats_dec(DQST_ALLOC_DQUOTS
);
300 list_del(&dquot
->dq_inuse
);
303 * End of list functions needing dq_list_lock
306 static void wait_on_dquot(struct dquot
*dquot
)
308 mutex_lock(&dquot
->dq_lock
);
309 mutex_unlock(&dquot
->dq_lock
);
312 static inline int dquot_dirty(struct dquot
*dquot
)
314 return test_bit(DQ_MOD_B
, &dquot
->dq_flags
);
317 static inline int mark_dquot_dirty(struct dquot
*dquot
)
319 return dquot
->dq_sb
->dq_op
->mark_dirty(dquot
);
322 /* Mark dquot dirty in atomic manner, and return it's old dirty flag state */
323 int dquot_mark_dquot_dirty(struct dquot
*dquot
)
327 /* If quota is dirty already, we don't have to acquire dq_list_lock */
328 if (test_bit(DQ_MOD_B
, &dquot
->dq_flags
))
331 spin_lock(&dq_list_lock
);
332 if (!test_and_set_bit(DQ_MOD_B
, &dquot
->dq_flags
)) {
333 list_add(&dquot
->dq_dirty
, &sb_dqopt(dquot
->dq_sb
)->
334 info
[dquot
->dq_type
].dqi_dirty_list
);
337 spin_unlock(&dq_list_lock
);
340 EXPORT_SYMBOL(dquot_mark_dquot_dirty
);
342 /* Dirtify all the dquots - this can block when journalling */
343 static inline int mark_all_dquot_dirty(struct dquot
* const *dquot
)
348 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++) {
350 /* Even in case of error we have to continue */
351 ret
= mark_dquot_dirty(dquot
[cnt
]);
358 static inline void dqput_all(struct dquot
**dquot
)
362 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++)
366 /* This function needs dq_list_lock */
367 static inline int clear_dquot_dirty(struct dquot
*dquot
)
369 if (!test_and_clear_bit(DQ_MOD_B
, &dquot
->dq_flags
))
371 list_del_init(&dquot
->dq_dirty
);
375 void mark_info_dirty(struct super_block
*sb
, int type
)
377 set_bit(DQF_INFO_DIRTY_B
, &sb_dqopt(sb
)->info
[type
].dqi_flags
);
379 EXPORT_SYMBOL(mark_info_dirty
);
382 * Read dquot from disk and alloc space for it
385 int dquot_acquire(struct dquot
*dquot
)
387 int ret
= 0, ret2
= 0;
388 struct quota_info
*dqopt
= sb_dqopt(dquot
->dq_sb
);
390 mutex_lock(&dquot
->dq_lock
);
391 mutex_lock(&dqopt
->dqio_mutex
);
392 if (!test_bit(DQ_READ_B
, &dquot
->dq_flags
))
393 ret
= dqopt
->ops
[dquot
->dq_type
]->read_dqblk(dquot
);
396 set_bit(DQ_READ_B
, &dquot
->dq_flags
);
397 /* Instantiate dquot if needed */
398 if (!test_bit(DQ_ACTIVE_B
, &dquot
->dq_flags
) && !dquot
->dq_off
) {
399 ret
= dqopt
->ops
[dquot
->dq_type
]->commit_dqblk(dquot
);
400 /* Write the info if needed */
401 if (info_dirty(&dqopt
->info
[dquot
->dq_type
])) {
402 ret2
= dqopt
->ops
[dquot
->dq_type
]->write_file_info(
403 dquot
->dq_sb
, dquot
->dq_type
);
412 set_bit(DQ_ACTIVE_B
, &dquot
->dq_flags
);
414 mutex_unlock(&dqopt
->dqio_mutex
);
415 mutex_unlock(&dquot
->dq_lock
);
418 EXPORT_SYMBOL(dquot_acquire
);
421 * Write dquot to disk
423 int dquot_commit(struct dquot
*dquot
)
425 int ret
= 0, ret2
= 0;
426 struct quota_info
*dqopt
= sb_dqopt(dquot
->dq_sb
);
428 mutex_lock(&dqopt
->dqio_mutex
);
429 spin_lock(&dq_list_lock
);
430 if (!clear_dquot_dirty(dquot
)) {
431 spin_unlock(&dq_list_lock
);
434 spin_unlock(&dq_list_lock
);
435 /* Inactive dquot can be only if there was error during read/init
436 * => we have better not writing it */
437 if (test_bit(DQ_ACTIVE_B
, &dquot
->dq_flags
)) {
438 ret
= dqopt
->ops
[dquot
->dq_type
]->commit_dqblk(dquot
);
439 if (info_dirty(&dqopt
->info
[dquot
->dq_type
])) {
440 ret2
= dqopt
->ops
[dquot
->dq_type
]->write_file_info(
441 dquot
->dq_sb
, dquot
->dq_type
);
447 mutex_unlock(&dqopt
->dqio_mutex
);
450 EXPORT_SYMBOL(dquot_commit
);
455 int dquot_release(struct dquot
*dquot
)
457 int ret
= 0, ret2
= 0;
458 struct quota_info
*dqopt
= sb_dqopt(dquot
->dq_sb
);
460 mutex_lock(&dquot
->dq_lock
);
461 /* Check whether we are not racing with some other dqget() */
462 if (atomic_read(&dquot
->dq_count
) > 1)
464 mutex_lock(&dqopt
->dqio_mutex
);
465 if (dqopt
->ops
[dquot
->dq_type
]->release_dqblk
) {
466 ret
= dqopt
->ops
[dquot
->dq_type
]->release_dqblk(dquot
);
468 if (info_dirty(&dqopt
->info
[dquot
->dq_type
])) {
469 ret2
= dqopt
->ops
[dquot
->dq_type
]->write_file_info(
470 dquot
->dq_sb
, dquot
->dq_type
);
475 clear_bit(DQ_ACTIVE_B
, &dquot
->dq_flags
);
476 mutex_unlock(&dqopt
->dqio_mutex
);
478 mutex_unlock(&dquot
->dq_lock
);
481 EXPORT_SYMBOL(dquot_release
);
483 void dquot_destroy(struct dquot
*dquot
)
485 kmem_cache_free(dquot_cachep
, dquot
);
487 EXPORT_SYMBOL(dquot_destroy
);
489 static inline void do_destroy_dquot(struct dquot
*dquot
)
491 dquot
->dq_sb
->dq_op
->destroy_dquot(dquot
);
494 /* Invalidate all dquots on the list. Note that this function is called after
495 * quota is disabled and pointers from inodes removed so there cannot be new
496 * quota users. There can still be some users of quotas due to inodes being
497 * just deleted or pruned by prune_icache() (those are not attached to any
498 * list) or parallel quotactl call. We have to wait for such users.
500 static void invalidate_dquots(struct super_block
*sb
, int type
)
502 struct dquot
*dquot
, *tmp
;
505 spin_lock(&dq_list_lock
);
506 list_for_each_entry_safe(dquot
, tmp
, &inuse_list
, dq_inuse
) {
507 if (dquot
->dq_sb
!= sb
)
509 if (dquot
->dq_type
!= type
)
511 /* Wait for dquot users */
512 if (atomic_read(&dquot
->dq_count
)) {
515 atomic_inc(&dquot
->dq_count
);
516 prepare_to_wait(&dquot
->dq_wait_unused
, &wait
,
517 TASK_UNINTERRUPTIBLE
);
518 spin_unlock(&dq_list_lock
);
519 /* Once dqput() wakes us up, we know it's time to free
521 * IMPORTANT: we rely on the fact that there is always
522 * at most one process waiting for dquot to free.
523 * Otherwise dq_count would be > 1 and we would never
526 if (atomic_read(&dquot
->dq_count
) > 1)
528 finish_wait(&dquot
->dq_wait_unused
, &wait
);
530 /* At this moment dquot() need not exist (it could be
531 * reclaimed by prune_dqcache(). Hence we must
536 * Quota now has no users and it has been written on last
539 remove_dquot_hash(dquot
);
540 remove_free_dquot(dquot
);
542 do_destroy_dquot(dquot
);
544 spin_unlock(&dq_list_lock
);
547 /* Call callback for every active dquot on given filesystem */
548 int dquot_scan_active(struct super_block
*sb
,
549 int (*fn
)(struct dquot
*dquot
, unsigned long priv
),
552 struct dquot
*dquot
, *old_dquot
= NULL
;
555 mutex_lock(&sb_dqopt(sb
)->dqonoff_mutex
);
556 spin_lock(&dq_list_lock
);
557 list_for_each_entry(dquot
, &inuse_list
, dq_inuse
) {
558 if (!test_bit(DQ_ACTIVE_B
, &dquot
->dq_flags
))
560 if (dquot
->dq_sb
!= sb
)
562 /* Now we have active dquot so we can just increase use count */
563 atomic_inc(&dquot
->dq_count
);
564 spin_unlock(&dq_list_lock
);
565 dqstats_inc(DQST_LOOKUPS
);
568 ret
= fn(dquot
, priv
);
571 spin_lock(&dq_list_lock
);
572 /* We are safe to continue now because our dquot could not
573 * be moved out of the inuse list while we hold the reference */
575 spin_unlock(&dq_list_lock
);
578 mutex_unlock(&sb_dqopt(sb
)->dqonoff_mutex
);
581 EXPORT_SYMBOL(dquot_scan_active
);
583 int dquot_quota_sync(struct super_block
*sb
, int type
, int wait
)
585 struct list_head
*dirty
;
587 struct quota_info
*dqopt
= sb_dqopt(sb
);
590 mutex_lock(&dqopt
->dqonoff_mutex
);
591 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++) {
592 if (type
!= -1 && cnt
!= type
)
594 if (!sb_has_quota_active(sb
, cnt
))
596 spin_lock(&dq_list_lock
);
597 dirty
= &dqopt
->info
[cnt
].dqi_dirty_list
;
598 while (!list_empty(dirty
)) {
599 dquot
= list_first_entry(dirty
, struct dquot
,
601 /* Dirty and inactive can be only bad dquot... */
602 if (!test_bit(DQ_ACTIVE_B
, &dquot
->dq_flags
)) {
603 clear_dquot_dirty(dquot
);
606 /* Now we have active dquot from which someone is
607 * holding reference so we can safely just increase
609 atomic_inc(&dquot
->dq_count
);
610 spin_unlock(&dq_list_lock
);
611 dqstats_inc(DQST_LOOKUPS
);
612 sb
->dq_op
->write_dquot(dquot
);
614 spin_lock(&dq_list_lock
);
616 spin_unlock(&dq_list_lock
);
619 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++)
620 if ((cnt
== type
|| type
== -1) && sb_has_quota_active(sb
, cnt
)
621 && info_dirty(&dqopt
->info
[cnt
]))
622 sb
->dq_op
->write_info(sb
, cnt
);
623 dqstats_inc(DQST_SYNCS
);
624 mutex_unlock(&dqopt
->dqonoff_mutex
);
626 if (!wait
|| (sb_dqopt(sb
)->flags
& DQUOT_QUOTA_SYS_FILE
))
629 /* This is not very clever (and fast) but currently I don't know about
630 * any other simple way of getting quota data to disk and we must get
631 * them there for userspace to be visible... */
632 if (sb
->s_op
->sync_fs
)
633 sb
->s_op
->sync_fs(sb
, 1);
634 sync_blockdev(sb
->s_bdev
);
637 * Now when everything is written we can discard the pagecache so
638 * that userspace sees the changes.
640 mutex_lock(&sb_dqopt(sb
)->dqonoff_mutex
);
641 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++) {
642 if (type
!= -1 && cnt
!= type
)
644 if (!sb_has_quota_active(sb
, cnt
))
646 mutex_lock_nested(&sb_dqopt(sb
)->files
[cnt
]->i_mutex
,
648 truncate_inode_pages(&sb_dqopt(sb
)->files
[cnt
]->i_data
, 0);
649 mutex_unlock(&sb_dqopt(sb
)->files
[cnt
]->i_mutex
);
651 mutex_unlock(&sb_dqopt(sb
)->dqonoff_mutex
);
655 EXPORT_SYMBOL(dquot_quota_sync
);
657 /* Free unused dquots from cache */
658 static void prune_dqcache(int count
)
660 struct list_head
*head
;
663 head
= free_dquots
.prev
;
664 while (head
!= &free_dquots
&& count
) {
665 dquot
= list_entry(head
, struct dquot
, dq_free
);
666 remove_dquot_hash(dquot
);
667 remove_free_dquot(dquot
);
669 do_destroy_dquot(dquot
);
671 head
= free_dquots
.prev
;
676 * This is called from kswapd when we think we need some
679 static int shrink_dqcache_memory(int nr
, gfp_t gfp_mask
)
682 spin_lock(&dq_list_lock
);
684 spin_unlock(&dq_list_lock
);
687 percpu_counter_read_positive(&dqstats
.counter
[DQST_FREE_DQUOTS
])
688 /100) * sysctl_vfs_cache_pressure
;
691 static struct shrinker dqcache_shrinker
= {
692 .shrink
= shrink_dqcache_memory
,
693 .seeks
= DEFAULT_SEEKS
,
697 * Put reference to dquot
698 * NOTE: If you change this function please check whether dqput_blocks() works right...
700 void dqput(struct dquot
*dquot
)
706 #ifdef CONFIG_QUOTA_DEBUG
707 if (!atomic_read(&dquot
->dq_count
)) {
708 printk("VFS: dqput: trying to free free dquot\n");
709 printk("VFS: device %s, dquot of %s %d\n",
711 quotatypes
[dquot
->dq_type
],
716 dqstats_inc(DQST_DROPS
);
718 spin_lock(&dq_list_lock
);
719 if (atomic_read(&dquot
->dq_count
) > 1) {
720 /* We have more than one user... nothing to do */
721 atomic_dec(&dquot
->dq_count
);
722 /* Releasing dquot during quotaoff phase? */
723 if (!sb_has_quota_active(dquot
->dq_sb
, dquot
->dq_type
) &&
724 atomic_read(&dquot
->dq_count
) == 1)
725 wake_up(&dquot
->dq_wait_unused
);
726 spin_unlock(&dq_list_lock
);
729 /* Need to release dquot? */
730 if (test_bit(DQ_ACTIVE_B
, &dquot
->dq_flags
) && dquot_dirty(dquot
)) {
731 spin_unlock(&dq_list_lock
);
732 /* Commit dquot before releasing */
733 ret
= dquot
->dq_sb
->dq_op
->write_dquot(dquot
);
735 printk(KERN_ERR
"VFS: cannot write quota structure on "
736 "device %s (error %d). Quota may get out of "
737 "sync!\n", dquot
->dq_sb
->s_id
, ret
);
739 * We clear dirty bit anyway, so that we avoid
742 spin_lock(&dq_list_lock
);
743 clear_dquot_dirty(dquot
);
744 spin_unlock(&dq_list_lock
);
748 /* Clear flag in case dquot was inactive (something bad happened) */
749 clear_dquot_dirty(dquot
);
750 if (test_bit(DQ_ACTIVE_B
, &dquot
->dq_flags
)) {
751 spin_unlock(&dq_list_lock
);
752 dquot
->dq_sb
->dq_op
->release_dquot(dquot
);
755 atomic_dec(&dquot
->dq_count
);
756 #ifdef CONFIG_QUOTA_DEBUG
758 BUG_ON(!list_empty(&dquot
->dq_free
));
760 put_dquot_last(dquot
);
761 spin_unlock(&dq_list_lock
);
763 EXPORT_SYMBOL(dqput
);
765 struct dquot
*dquot_alloc(struct super_block
*sb
, int type
)
767 return kmem_cache_zalloc(dquot_cachep
, GFP_NOFS
);
769 EXPORT_SYMBOL(dquot_alloc
);
771 static struct dquot
*get_empty_dquot(struct super_block
*sb
, int type
)
775 dquot
= sb
->dq_op
->alloc_dquot(sb
, type
);
779 mutex_init(&dquot
->dq_lock
);
780 INIT_LIST_HEAD(&dquot
->dq_free
);
781 INIT_LIST_HEAD(&dquot
->dq_inuse
);
782 INIT_HLIST_NODE(&dquot
->dq_hash
);
783 INIT_LIST_HEAD(&dquot
->dq_dirty
);
784 init_waitqueue_head(&dquot
->dq_wait_unused
);
786 dquot
->dq_type
= type
;
787 atomic_set(&dquot
->dq_count
, 1);
793 * Get reference to dquot
795 * Locking is slightly tricky here. We are guarded from parallel quotaoff()
796 * destroying our dquot by:
797 * a) checking for quota flags under dq_list_lock and
798 * b) getting a reference to dquot before we release dq_list_lock
800 struct dquot
*dqget(struct super_block
*sb
, unsigned int id
, int type
)
802 unsigned int hashent
= hashfn(sb
, id
, type
);
803 struct dquot
*dquot
= NULL
, *empty
= NULL
;
805 if (!sb_has_quota_active(sb
, type
))
808 spin_lock(&dq_list_lock
);
809 spin_lock(&dq_state_lock
);
810 if (!sb_has_quota_active(sb
, type
)) {
811 spin_unlock(&dq_state_lock
);
812 spin_unlock(&dq_list_lock
);
815 spin_unlock(&dq_state_lock
);
817 dquot
= find_dquot(hashent
, sb
, id
, type
);
820 spin_unlock(&dq_list_lock
);
821 empty
= get_empty_dquot(sb
, type
);
823 schedule(); /* Try to wait for a moment... */
829 /* all dquots go on the inuse_list */
831 /* hash it first so it can be found */
832 insert_dquot_hash(dquot
);
833 spin_unlock(&dq_list_lock
);
834 dqstats_inc(DQST_LOOKUPS
);
836 if (!atomic_read(&dquot
->dq_count
))
837 remove_free_dquot(dquot
);
838 atomic_inc(&dquot
->dq_count
);
839 spin_unlock(&dq_list_lock
);
840 dqstats_inc(DQST_CACHE_HITS
);
841 dqstats_inc(DQST_LOOKUPS
);
843 /* Wait for dq_lock - after this we know that either dquot_release() is
844 * already finished or it will be canceled due to dq_count > 1 test */
845 wait_on_dquot(dquot
);
846 /* Read the dquot / allocate space in quota file */
847 if (!test_bit(DQ_ACTIVE_B
, &dquot
->dq_flags
) &&
848 sb
->dq_op
->acquire_dquot(dquot
) < 0) {
853 #ifdef CONFIG_QUOTA_DEBUG
854 BUG_ON(!dquot
->dq_sb
); /* Has somebody invalidated entry under us? */
858 do_destroy_dquot(empty
);
862 EXPORT_SYMBOL(dqget
);
864 static int dqinit_needed(struct inode
*inode
, int type
)
868 if (IS_NOQUOTA(inode
))
871 return !inode
->i_dquot
[type
];
872 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++)
873 if (!inode
->i_dquot
[cnt
])
878 /* This routine is guarded by dqonoff_mutex mutex */
879 static void add_dquot_ref(struct super_block
*sb
, int type
)
881 struct inode
*inode
, *old_inode
= NULL
;
882 #ifdef CONFIG_QUOTA_DEBUG
886 spin_lock(&inode_lock
);
887 list_for_each_entry(inode
, &sb
->s_inodes
, i_sb_list
) {
888 if (inode
->i_state
& (I_FREEING
|I_CLEAR
|I_WILL_FREE
|I_NEW
))
890 #ifdef CONFIG_QUOTA_DEBUG
891 if (unlikely(inode_get_rsv_space(inode
) > 0))
894 if (!atomic_read(&inode
->i_writecount
))
896 if (!dqinit_needed(inode
, type
))
900 spin_unlock(&inode_lock
);
903 __dquot_initialize(inode
, type
);
904 /* We hold a reference to 'inode' so it couldn't have been
905 * removed from s_inodes list while we dropped the inode_lock.
906 * We cannot iput the inode now as we can be holding the last
907 * reference and we cannot iput it under inode_lock. So we
908 * keep the reference and iput it later. */
910 spin_lock(&inode_lock
);
912 spin_unlock(&inode_lock
);
915 #ifdef CONFIG_QUOTA_DEBUG
917 printk(KERN_WARNING
"VFS (%s): Writes happened before quota"
918 " was turned on thus quota information is probably "
919 "inconsistent. Please run quotacheck(8).\n", sb
->s_id
);
925 * Return 0 if dqput() won't block.
926 * (note that 1 doesn't necessarily mean blocking)
928 static inline int dqput_blocks(struct dquot
*dquot
)
930 if (atomic_read(&dquot
->dq_count
) <= 1)
936 * Remove references to dquots from inode and add dquot to list for freeing
937 * if we have the last referece to dquot
938 * We can't race with anybody because we hold dqptr_sem for writing...
940 static int remove_inode_dquot_ref(struct inode
*inode
, int type
,
941 struct list_head
*tofree_head
)
943 struct dquot
*dquot
= inode
->i_dquot
[type
];
945 inode
->i_dquot
[type
] = NULL
;
947 if (dqput_blocks(dquot
)) {
948 #ifdef CONFIG_QUOTA_DEBUG
949 if (atomic_read(&dquot
->dq_count
) != 1)
950 printk(KERN_WARNING
"VFS: Adding dquot with dq_count %d to dispose list.\n", atomic_read(&dquot
->dq_count
));
952 spin_lock(&dq_list_lock
);
953 /* As dquot must have currently users it can't be on
954 * the free list... */
955 list_add(&dquot
->dq_free
, tofree_head
);
956 spin_unlock(&dq_list_lock
);
960 dqput(dquot
); /* We have guaranteed we won't block */
966 * Free list of dquots
967 * Dquots are removed from inodes and no new references can be got so we are
968 * the only ones holding reference
970 static void put_dquot_list(struct list_head
*tofree_head
)
972 struct list_head
*act_head
;
975 act_head
= tofree_head
->next
;
976 while (act_head
!= tofree_head
) {
977 dquot
= list_entry(act_head
, struct dquot
, dq_free
);
978 act_head
= act_head
->next
;
979 /* Remove dquot from the list so we won't have problems... */
980 list_del_init(&dquot
->dq_free
);
985 static void remove_dquot_ref(struct super_block
*sb
, int type
,
986 struct list_head
*tofree_head
)
990 spin_lock(&inode_lock
);
991 list_for_each_entry(inode
, &sb
->s_inodes
, i_sb_list
) {
993 * We have to scan also I_NEW inodes because they can already
994 * have quota pointer initialized. Luckily, we need to touch
995 * only quota pointers and these have separate locking
998 if (!IS_NOQUOTA(inode
))
999 remove_inode_dquot_ref(inode
, type
, tofree_head
);
1001 spin_unlock(&inode_lock
);
1004 /* Gather all references from inodes and drop them */
1005 static void drop_dquot_ref(struct super_block
*sb
, int type
)
1007 LIST_HEAD(tofree_head
);
1010 down_write(&sb_dqopt(sb
)->dqptr_sem
);
1011 remove_dquot_ref(sb
, type
, &tofree_head
);
1012 up_write(&sb_dqopt(sb
)->dqptr_sem
);
1013 put_dquot_list(&tofree_head
);
1017 static inline void dquot_incr_inodes(struct dquot
*dquot
, qsize_t number
)
1019 dquot
->dq_dqb
.dqb_curinodes
+= number
;
1022 static inline void dquot_incr_space(struct dquot
*dquot
, qsize_t number
)
1024 dquot
->dq_dqb
.dqb_curspace
+= number
;
1027 static inline void dquot_resv_space(struct dquot
*dquot
, qsize_t number
)
1029 dquot
->dq_dqb
.dqb_rsvspace
+= number
;
1033 * Claim reserved quota space
1035 static void dquot_claim_reserved_space(struct dquot
*dquot
, qsize_t number
)
1037 if (dquot
->dq_dqb
.dqb_rsvspace
< number
) {
1039 number
= dquot
->dq_dqb
.dqb_rsvspace
;
1041 dquot
->dq_dqb
.dqb_curspace
+= number
;
1042 dquot
->dq_dqb
.dqb_rsvspace
-= number
;
1046 void dquot_free_reserved_space(struct dquot
*dquot
, qsize_t number
)
1048 if (dquot
->dq_dqb
.dqb_rsvspace
>= number
)
1049 dquot
->dq_dqb
.dqb_rsvspace
-= number
;
1052 dquot
->dq_dqb
.dqb_rsvspace
= 0;
1056 static void dquot_decr_inodes(struct dquot
*dquot
, qsize_t number
)
1058 if (sb_dqopt(dquot
->dq_sb
)->flags
& DQUOT_NEGATIVE_USAGE
||
1059 dquot
->dq_dqb
.dqb_curinodes
>= number
)
1060 dquot
->dq_dqb
.dqb_curinodes
-= number
;
1062 dquot
->dq_dqb
.dqb_curinodes
= 0;
1063 if (dquot
->dq_dqb
.dqb_curinodes
<= dquot
->dq_dqb
.dqb_isoftlimit
)
1064 dquot
->dq_dqb
.dqb_itime
= (time_t) 0;
1065 clear_bit(DQ_INODES_B
, &dquot
->dq_flags
);
1068 static void dquot_decr_space(struct dquot
*dquot
, qsize_t number
)
1070 if (sb_dqopt(dquot
->dq_sb
)->flags
& DQUOT_NEGATIVE_USAGE
||
1071 dquot
->dq_dqb
.dqb_curspace
>= number
)
1072 dquot
->dq_dqb
.dqb_curspace
-= number
;
1074 dquot
->dq_dqb
.dqb_curspace
= 0;
1075 if (dquot
->dq_dqb
.dqb_curspace
<= dquot
->dq_dqb
.dqb_bsoftlimit
)
1076 dquot
->dq_dqb
.dqb_btime
= (time_t) 0;
1077 clear_bit(DQ_BLKS_B
, &dquot
->dq_flags
);
1080 static int warning_issued(struct dquot
*dquot
, const int warntype
)
1082 int flag
= (warntype
== QUOTA_NL_BHARDWARN
||
1083 warntype
== QUOTA_NL_BSOFTLONGWARN
) ? DQ_BLKS_B
:
1084 ((warntype
== QUOTA_NL_IHARDWARN
||
1085 warntype
== QUOTA_NL_ISOFTLONGWARN
) ? DQ_INODES_B
: 0);
1089 return test_and_set_bit(flag
, &dquot
->dq_flags
);
1092 #ifdef CONFIG_PRINT_QUOTA_WARNING
1093 static int flag_print_warnings
= 1;
1095 static int need_print_warning(struct dquot
*dquot
)
1097 if (!flag_print_warnings
)
1100 switch (dquot
->dq_type
) {
1102 return current_fsuid() == dquot
->dq_id
;
1104 return in_group_p(dquot
->dq_id
);
1109 /* Print warning to user which exceeded quota */
1110 static void print_warning(struct dquot
*dquot
, const int warntype
)
1113 struct tty_struct
*tty
;
1115 if (warntype
== QUOTA_NL_IHARDBELOW
||
1116 warntype
== QUOTA_NL_ISOFTBELOW
||
1117 warntype
== QUOTA_NL_BHARDBELOW
||
1118 warntype
== QUOTA_NL_BSOFTBELOW
|| !need_print_warning(dquot
))
1121 tty
= get_current_tty();
1124 tty_write_message(tty
, dquot
->dq_sb
->s_id
);
1125 if (warntype
== QUOTA_NL_ISOFTWARN
|| warntype
== QUOTA_NL_BSOFTWARN
)
1126 tty_write_message(tty
, ": warning, ");
1128 tty_write_message(tty
, ": write failed, ");
1129 tty_write_message(tty
, quotatypes
[dquot
->dq_type
]);
1131 case QUOTA_NL_IHARDWARN
:
1132 msg
= " file limit reached.\r\n";
1134 case QUOTA_NL_ISOFTLONGWARN
:
1135 msg
= " file quota exceeded too long.\r\n";
1137 case QUOTA_NL_ISOFTWARN
:
1138 msg
= " file quota exceeded.\r\n";
1140 case QUOTA_NL_BHARDWARN
:
1141 msg
= " block limit reached.\r\n";
1143 case QUOTA_NL_BSOFTLONGWARN
:
1144 msg
= " block quota exceeded too long.\r\n";
1146 case QUOTA_NL_BSOFTWARN
:
1147 msg
= " block quota exceeded.\r\n";
1150 tty_write_message(tty
, msg
);
1156 * Write warnings to the console and send warning messages over netlink.
1158 * Note that this function can sleep.
1160 static void flush_warnings(struct dquot
*const *dquots
, char *warntype
)
1165 for (i
= 0; i
< MAXQUOTAS
; i
++) {
1167 if (dq
&& warntype
[i
] != QUOTA_NL_NOWARN
&&
1168 !warning_issued(dq
, warntype
[i
])) {
1169 #ifdef CONFIG_PRINT_QUOTA_WARNING
1170 print_warning(dq
, warntype
[i
]);
1172 quota_send_warning(dq
->dq_type
, dq
->dq_id
,
1173 dq
->dq_sb
->s_dev
, warntype
[i
]);
1178 static int ignore_hardlimit(struct dquot
*dquot
)
1180 struct mem_dqinfo
*info
= &sb_dqopt(dquot
->dq_sb
)->info
[dquot
->dq_type
];
1182 return capable(CAP_SYS_RESOURCE
) &&
1183 (info
->dqi_format
->qf_fmt_id
!= QFMT_VFS_OLD
||
1184 !(info
->dqi_flags
& V1_DQF_RSQUASH
));
1187 /* needs dq_data_lock */
1188 static int check_idq(struct dquot
*dquot
, qsize_t inodes
, char *warntype
)
1190 qsize_t newinodes
= dquot
->dq_dqb
.dqb_curinodes
+ inodes
;
1192 *warntype
= QUOTA_NL_NOWARN
;
1193 if (!sb_has_quota_limits_enabled(dquot
->dq_sb
, dquot
->dq_type
) ||
1194 test_bit(DQ_FAKE_B
, &dquot
->dq_flags
))
1197 if (dquot
->dq_dqb
.dqb_ihardlimit
&&
1198 newinodes
> dquot
->dq_dqb
.dqb_ihardlimit
&&
1199 !ignore_hardlimit(dquot
)) {
1200 *warntype
= QUOTA_NL_IHARDWARN
;
1204 if (dquot
->dq_dqb
.dqb_isoftlimit
&&
1205 newinodes
> dquot
->dq_dqb
.dqb_isoftlimit
&&
1206 dquot
->dq_dqb
.dqb_itime
&&
1207 get_seconds() >= dquot
->dq_dqb
.dqb_itime
&&
1208 !ignore_hardlimit(dquot
)) {
1209 *warntype
= QUOTA_NL_ISOFTLONGWARN
;
1213 if (dquot
->dq_dqb
.dqb_isoftlimit
&&
1214 newinodes
> dquot
->dq_dqb
.dqb_isoftlimit
&&
1215 dquot
->dq_dqb
.dqb_itime
== 0) {
1216 *warntype
= QUOTA_NL_ISOFTWARN
;
1217 dquot
->dq_dqb
.dqb_itime
= get_seconds() +
1218 sb_dqopt(dquot
->dq_sb
)->info
[dquot
->dq_type
].dqi_igrace
;
1224 /* needs dq_data_lock */
1225 static int check_bdq(struct dquot
*dquot
, qsize_t space
, int prealloc
, char *warntype
)
1228 struct super_block
*sb
= dquot
->dq_sb
;
1230 *warntype
= QUOTA_NL_NOWARN
;
1231 if (!sb_has_quota_limits_enabled(sb
, dquot
->dq_type
) ||
1232 test_bit(DQ_FAKE_B
, &dquot
->dq_flags
))
1235 tspace
= dquot
->dq_dqb
.dqb_curspace
+ dquot
->dq_dqb
.dqb_rsvspace
1238 if (dquot
->dq_dqb
.dqb_bhardlimit
&&
1239 tspace
> dquot
->dq_dqb
.dqb_bhardlimit
&&
1240 !ignore_hardlimit(dquot
)) {
1242 *warntype
= QUOTA_NL_BHARDWARN
;
1246 if (dquot
->dq_dqb
.dqb_bsoftlimit
&&
1247 tspace
> dquot
->dq_dqb
.dqb_bsoftlimit
&&
1248 dquot
->dq_dqb
.dqb_btime
&&
1249 get_seconds() >= dquot
->dq_dqb
.dqb_btime
&&
1250 !ignore_hardlimit(dquot
)) {
1252 *warntype
= QUOTA_NL_BSOFTLONGWARN
;
1256 if (dquot
->dq_dqb
.dqb_bsoftlimit
&&
1257 tspace
> dquot
->dq_dqb
.dqb_bsoftlimit
&&
1258 dquot
->dq_dqb
.dqb_btime
== 0) {
1260 *warntype
= QUOTA_NL_BSOFTWARN
;
1261 dquot
->dq_dqb
.dqb_btime
= get_seconds() +
1262 sb_dqopt(sb
)->info
[dquot
->dq_type
].dqi_bgrace
;
1266 * We don't allow preallocation to exceed softlimit so exceeding will
1275 static int info_idq_free(struct dquot
*dquot
, qsize_t inodes
)
1279 if (test_bit(DQ_FAKE_B
, &dquot
->dq_flags
) ||
1280 dquot
->dq_dqb
.dqb_curinodes
<= dquot
->dq_dqb
.dqb_isoftlimit
||
1281 !sb_has_quota_limits_enabled(dquot
->dq_sb
, dquot
->dq_type
))
1282 return QUOTA_NL_NOWARN
;
1284 newinodes
= dquot
->dq_dqb
.dqb_curinodes
- inodes
;
1285 if (newinodes
<= dquot
->dq_dqb
.dqb_isoftlimit
)
1286 return QUOTA_NL_ISOFTBELOW
;
1287 if (dquot
->dq_dqb
.dqb_curinodes
>= dquot
->dq_dqb
.dqb_ihardlimit
&&
1288 newinodes
< dquot
->dq_dqb
.dqb_ihardlimit
)
1289 return QUOTA_NL_IHARDBELOW
;
1290 return QUOTA_NL_NOWARN
;
1293 static int info_bdq_free(struct dquot
*dquot
, qsize_t space
)
1295 if (test_bit(DQ_FAKE_B
, &dquot
->dq_flags
) ||
1296 dquot
->dq_dqb
.dqb_curspace
<= dquot
->dq_dqb
.dqb_bsoftlimit
)
1297 return QUOTA_NL_NOWARN
;
1299 if (dquot
->dq_dqb
.dqb_curspace
- space
<= dquot
->dq_dqb
.dqb_bsoftlimit
)
1300 return QUOTA_NL_BSOFTBELOW
;
1301 if (dquot
->dq_dqb
.dqb_curspace
>= dquot
->dq_dqb
.dqb_bhardlimit
&&
1302 dquot
->dq_dqb
.dqb_curspace
- space
< dquot
->dq_dqb
.dqb_bhardlimit
)
1303 return QUOTA_NL_BHARDBELOW
;
1304 return QUOTA_NL_NOWARN
;
1308 * Initialize quota pointers in inode
1310 * We do things in a bit complicated way but by that we avoid calling
1311 * dqget() and thus filesystem callbacks under dqptr_sem.
1313 * It is better to call this function outside of any transaction as it
1314 * might need a lot of space in journal for dquot structure allocation.
1316 static void __dquot_initialize(struct inode
*inode
, int type
)
1318 unsigned int id
= 0;
1320 struct dquot
*got
[MAXQUOTAS
];
1321 struct super_block
*sb
= inode
->i_sb
;
1324 /* First test before acquiring mutex - solves deadlocks when we
1325 * re-enter the quota code and are already holding the mutex */
1326 if (!sb_any_quota_active(inode
->i_sb
) || IS_NOQUOTA(inode
))
1329 /* First get references to structures we might need. */
1330 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++) {
1332 if (type
!= -1 && cnt
!= type
)
1342 got
[cnt
] = dqget(sb
, id
, cnt
);
1345 down_write(&sb_dqopt(sb
)->dqptr_sem
);
1346 if (IS_NOQUOTA(inode
))
1348 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++) {
1349 if (type
!= -1 && cnt
!= type
)
1351 /* Avoid races with quotaoff() */
1352 if (!sb_has_quota_active(sb
, cnt
))
1354 if (!inode
->i_dquot
[cnt
]) {
1355 inode
->i_dquot
[cnt
] = got
[cnt
];
1358 * Make quota reservation system happy if someone
1359 * did a write before quota was turned on
1361 rsv
= inode_get_rsv_space(inode
);
1363 dquot_resv_space(inode
->i_dquot
[cnt
], rsv
);
1367 up_write(&sb_dqopt(sb
)->dqptr_sem
);
1368 /* Drop unused references */
1372 void dquot_initialize(struct inode
*inode
)
1374 __dquot_initialize(inode
, -1);
1376 EXPORT_SYMBOL(dquot_initialize
);
1379 * Release all quotas referenced by inode
1381 static void __dquot_drop(struct inode
*inode
)
1384 struct dquot
*put
[MAXQUOTAS
];
1386 down_write(&sb_dqopt(inode
->i_sb
)->dqptr_sem
);
1387 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++) {
1388 put
[cnt
] = inode
->i_dquot
[cnt
];
1389 inode
->i_dquot
[cnt
] = NULL
;
1391 up_write(&sb_dqopt(inode
->i_sb
)->dqptr_sem
);
1395 void dquot_drop(struct inode
*inode
)
1399 if (IS_NOQUOTA(inode
))
1403 * Test before calling to rule out calls from proc and such
1404 * where we are not allowed to block. Note that this is
1405 * actually reliable test even without the lock - the caller
1406 * must assure that nobody can come after the DQUOT_DROP and
1407 * add quota pointers back anyway.
1409 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++) {
1410 if (inode
->i_dquot
[cnt
])
1414 if (cnt
< MAXQUOTAS
)
1415 __dquot_drop(inode
);
1417 EXPORT_SYMBOL(dquot_drop
);
1420 * inode_reserved_space is managed internally by quota, and protected by
1421 * i_lock similar to i_blocks+i_bytes.
1423 static qsize_t
*inode_reserved_space(struct inode
* inode
)
1425 /* Filesystem must explicitly define it's own method in order to use
1426 * quota reservation interface */
1427 BUG_ON(!inode
->i_sb
->dq_op
->get_reserved_space
);
1428 return inode
->i_sb
->dq_op
->get_reserved_space(inode
);
1431 void inode_add_rsv_space(struct inode
*inode
, qsize_t number
)
1433 spin_lock(&inode
->i_lock
);
1434 *inode_reserved_space(inode
) += number
;
1435 spin_unlock(&inode
->i_lock
);
1437 EXPORT_SYMBOL(inode_add_rsv_space
);
1439 void inode_claim_rsv_space(struct inode
*inode
, qsize_t number
)
1441 spin_lock(&inode
->i_lock
);
1442 *inode_reserved_space(inode
) -= number
;
1443 __inode_add_bytes(inode
, number
);
1444 spin_unlock(&inode
->i_lock
);
1446 EXPORT_SYMBOL(inode_claim_rsv_space
);
1448 void inode_sub_rsv_space(struct inode
*inode
, qsize_t number
)
1450 spin_lock(&inode
->i_lock
);
1451 *inode_reserved_space(inode
) -= number
;
1452 spin_unlock(&inode
->i_lock
);
1454 EXPORT_SYMBOL(inode_sub_rsv_space
);
1456 static qsize_t
inode_get_rsv_space(struct inode
*inode
)
1460 if (!inode
->i_sb
->dq_op
->get_reserved_space
)
1462 spin_lock(&inode
->i_lock
);
1463 ret
= *inode_reserved_space(inode
);
1464 spin_unlock(&inode
->i_lock
);
1468 static void inode_incr_space(struct inode
*inode
, qsize_t number
,
1472 inode_add_rsv_space(inode
, number
);
1474 inode_add_bytes(inode
, number
);
1477 static void inode_decr_space(struct inode
*inode
, qsize_t number
, int reserve
)
1480 inode_sub_rsv_space(inode
, number
);
1482 inode_sub_bytes(inode
, number
);
1486 * This functions updates i_blocks+i_bytes fields and quota information
1487 * (together with appropriate checks).
1489 * NOTE: We absolutely rely on the fact that caller dirties the inode
1490 * (usually helpers in quotaops.h care about this) and holds a handle for
1491 * the current transaction so that dquot write and inode write go into the
1496 * This operation can block, but only after everything is updated
1498 int __dquot_alloc_space(struct inode
*inode
, qsize_t number
,
1499 int warn
, int reserve
)
1502 char warntype
[MAXQUOTAS
];
1505 * First test before acquiring mutex - solves deadlocks when we
1506 * re-enter the quota code and are already holding the mutex
1508 if (!sb_any_quota_active(inode
->i_sb
) || IS_NOQUOTA(inode
)) {
1509 inode_incr_space(inode
, number
, reserve
);
1513 down_read(&sb_dqopt(inode
->i_sb
)->dqptr_sem
);
1514 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++)
1515 warntype
[cnt
] = QUOTA_NL_NOWARN
;
1517 spin_lock(&dq_data_lock
);
1518 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++) {
1519 if (!inode
->i_dquot
[cnt
])
1521 ret
= check_bdq(inode
->i_dquot
[cnt
], number
, !warn
,
1524 spin_unlock(&dq_data_lock
);
1525 goto out_flush_warn
;
1528 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++) {
1529 if (!inode
->i_dquot
[cnt
])
1532 dquot_resv_space(inode
->i_dquot
[cnt
], number
);
1534 dquot_incr_space(inode
->i_dquot
[cnt
], number
);
1536 inode_incr_space(inode
, number
, reserve
);
1537 spin_unlock(&dq_data_lock
);
1540 goto out_flush_warn
;
1541 mark_all_dquot_dirty(inode
->i_dquot
);
1543 flush_warnings(inode
->i_dquot
, warntype
);
1544 up_read(&sb_dqopt(inode
->i_sb
)->dqptr_sem
);
1548 EXPORT_SYMBOL(__dquot_alloc_space
);
1551 * This operation can block, but only after everything is updated
1553 int dquot_alloc_inode(const struct inode
*inode
)
1556 char warntype
[MAXQUOTAS
];
1558 /* First test before acquiring mutex - solves deadlocks when we
1559 * re-enter the quota code and are already holding the mutex */
1560 if (!sb_any_quota_active(inode
->i_sb
) || IS_NOQUOTA(inode
))
1562 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++)
1563 warntype
[cnt
] = QUOTA_NL_NOWARN
;
1564 down_read(&sb_dqopt(inode
->i_sb
)->dqptr_sem
);
1565 spin_lock(&dq_data_lock
);
1566 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++) {
1567 if (!inode
->i_dquot
[cnt
])
1569 ret
= check_idq(inode
->i_dquot
[cnt
], 1, warntype
+ cnt
);
1574 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++) {
1575 if (!inode
->i_dquot
[cnt
])
1577 dquot_incr_inodes(inode
->i_dquot
[cnt
], 1);
1581 spin_unlock(&dq_data_lock
);
1583 mark_all_dquot_dirty(inode
->i_dquot
);
1584 flush_warnings(inode
->i_dquot
, warntype
);
1585 up_read(&sb_dqopt(inode
->i_sb
)->dqptr_sem
);
1588 EXPORT_SYMBOL(dquot_alloc_inode
);
1591 * Convert in-memory reserved quotas to real consumed quotas
1593 int dquot_claim_space_nodirty(struct inode
*inode
, qsize_t number
)
1597 if (!sb_any_quota_active(inode
->i_sb
) || IS_NOQUOTA(inode
)) {
1598 inode_claim_rsv_space(inode
, number
);
1602 down_read(&sb_dqopt(inode
->i_sb
)->dqptr_sem
);
1603 spin_lock(&dq_data_lock
);
1604 /* Claim reserved quotas to allocated quotas */
1605 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++) {
1606 if (inode
->i_dquot
[cnt
])
1607 dquot_claim_reserved_space(inode
->i_dquot
[cnt
],
1610 /* Update inode bytes */
1611 inode_claim_rsv_space(inode
, number
);
1612 spin_unlock(&dq_data_lock
);
1613 mark_all_dquot_dirty(inode
->i_dquot
);
1614 up_read(&sb_dqopt(inode
->i_sb
)->dqptr_sem
);
1617 EXPORT_SYMBOL(dquot_claim_space_nodirty
);
1620 * This operation can block, but only after everything is updated
1622 void __dquot_free_space(struct inode
*inode
, qsize_t number
, int reserve
)
1625 char warntype
[MAXQUOTAS
];
1627 /* First test before acquiring mutex - solves deadlocks when we
1628 * re-enter the quota code and are already holding the mutex */
1629 if (!sb_any_quota_active(inode
->i_sb
) || IS_NOQUOTA(inode
)) {
1630 inode_decr_space(inode
, number
, reserve
);
1634 down_read(&sb_dqopt(inode
->i_sb
)->dqptr_sem
);
1635 spin_lock(&dq_data_lock
);
1636 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++) {
1637 if (!inode
->i_dquot
[cnt
])
1639 warntype
[cnt
] = info_bdq_free(inode
->i_dquot
[cnt
], number
);
1641 dquot_free_reserved_space(inode
->i_dquot
[cnt
], number
);
1643 dquot_decr_space(inode
->i_dquot
[cnt
], number
);
1645 inode_decr_space(inode
, number
, reserve
);
1646 spin_unlock(&dq_data_lock
);
1650 mark_all_dquot_dirty(inode
->i_dquot
);
1652 flush_warnings(inode
->i_dquot
, warntype
);
1653 up_read(&sb_dqopt(inode
->i_sb
)->dqptr_sem
);
1655 EXPORT_SYMBOL(__dquot_free_space
);
1658 * This operation can block, but only after everything is updated
1660 void dquot_free_inode(const struct inode
*inode
)
1663 char warntype
[MAXQUOTAS
];
1665 /* First test before acquiring mutex - solves deadlocks when we
1666 * re-enter the quota code and are already holding the mutex */
1667 if (!sb_any_quota_active(inode
->i_sb
) || IS_NOQUOTA(inode
))
1670 down_read(&sb_dqopt(inode
->i_sb
)->dqptr_sem
);
1671 spin_lock(&dq_data_lock
);
1672 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++) {
1673 if (!inode
->i_dquot
[cnt
])
1675 warntype
[cnt
] = info_idq_free(inode
->i_dquot
[cnt
], 1);
1676 dquot_decr_inodes(inode
->i_dquot
[cnt
], 1);
1678 spin_unlock(&dq_data_lock
);
1679 mark_all_dquot_dirty(inode
->i_dquot
);
1680 flush_warnings(inode
->i_dquot
, warntype
);
1681 up_read(&sb_dqopt(inode
->i_sb
)->dqptr_sem
);
1683 EXPORT_SYMBOL(dquot_free_inode
);
1686 * Transfer the number of inode and blocks from one diskquota to an other.
1687 * On success, dquot references in transfer_to are consumed and references
1688 * to original dquots that need to be released are placed there. On failure,
1689 * references are kept untouched.
1691 * This operation can block, but only after everything is updated
1692 * A transaction must be started when entering this function.
1695 int __dquot_transfer(struct inode
*inode
, struct dquot
**transfer_to
)
1697 qsize_t space
, cur_space
;
1698 qsize_t rsv_space
= 0;
1699 struct dquot
*transfer_from
[MAXQUOTAS
] = {};
1701 char warntype_to
[MAXQUOTAS
];
1702 char warntype_from_inodes
[MAXQUOTAS
], warntype_from_space
[MAXQUOTAS
];
1704 /* First test before acquiring mutex - solves deadlocks when we
1705 * re-enter the quota code and are already holding the mutex */
1706 if (IS_NOQUOTA(inode
))
1708 /* Initialize the arrays */
1709 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++)
1710 warntype_to
[cnt
] = QUOTA_NL_NOWARN
;
1711 down_write(&sb_dqopt(inode
->i_sb
)->dqptr_sem
);
1712 if (IS_NOQUOTA(inode
)) { /* File without quota accounting? */
1713 up_write(&sb_dqopt(inode
->i_sb
)->dqptr_sem
);
1716 spin_lock(&dq_data_lock
);
1717 cur_space
= inode_get_bytes(inode
);
1718 rsv_space
= inode_get_rsv_space(inode
);
1719 space
= cur_space
+ rsv_space
;
1720 /* Build the transfer_from list and check the limits */
1721 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++) {
1722 if (!transfer_to
[cnt
])
1724 transfer_from
[cnt
] = inode
->i_dquot
[cnt
];
1725 ret
= check_idq(transfer_to
[cnt
], 1, warntype_to
+ cnt
);
1728 ret
= check_bdq(transfer_to
[cnt
], space
, 0, warntype_to
+ cnt
);
1734 * Finally perform the needed transfer from transfer_from to transfer_to
1736 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++) {
1738 * Skip changes for same uid or gid or for turned off quota-type.
1740 if (!transfer_to
[cnt
])
1743 /* Due to IO error we might not have transfer_from[] structure */
1744 if (transfer_from
[cnt
]) {
1745 warntype_from_inodes
[cnt
] =
1746 info_idq_free(transfer_from
[cnt
], 1);
1747 warntype_from_space
[cnt
] =
1748 info_bdq_free(transfer_from
[cnt
], space
);
1749 dquot_decr_inodes(transfer_from
[cnt
], 1);
1750 dquot_decr_space(transfer_from
[cnt
], cur_space
);
1751 dquot_free_reserved_space(transfer_from
[cnt
],
1755 dquot_incr_inodes(transfer_to
[cnt
], 1);
1756 dquot_incr_space(transfer_to
[cnt
], cur_space
);
1757 dquot_resv_space(transfer_to
[cnt
], rsv_space
);
1759 inode
->i_dquot
[cnt
] = transfer_to
[cnt
];
1761 spin_unlock(&dq_data_lock
);
1762 up_write(&sb_dqopt(inode
->i_sb
)->dqptr_sem
);
1764 mark_all_dquot_dirty(transfer_from
);
1765 mark_all_dquot_dirty(transfer_to
);
1766 /* Pass back references to put */
1767 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++)
1768 transfer_to
[cnt
] = transfer_from
[cnt
];
1770 flush_warnings(transfer_to
, warntype_to
);
1771 flush_warnings(transfer_from
, warntype_from_inodes
);
1772 flush_warnings(transfer_from
, warntype_from_space
);
1775 spin_unlock(&dq_data_lock
);
1776 up_write(&sb_dqopt(inode
->i_sb
)->dqptr_sem
);
1779 EXPORT_SYMBOL(__dquot_transfer
);
1781 /* Wrapper for transferring ownership of an inode for uid/gid only
1782 * Called from FSXXX_setattr()
1784 int dquot_transfer(struct inode
*inode
, struct iattr
*iattr
)
1786 struct dquot
*transfer_to
[MAXQUOTAS
] = {};
1787 struct super_block
*sb
= inode
->i_sb
;
1790 if (!sb_any_quota_active(sb
) || IS_NOQUOTA(inode
))
1793 if (iattr
->ia_valid
& ATTR_UID
&& iattr
->ia_uid
!= inode
->i_uid
)
1794 transfer_to
[USRQUOTA
] = dqget(sb
, iattr
->ia_uid
, USRQUOTA
);
1795 if (iattr
->ia_valid
& ATTR_GID
&& iattr
->ia_gid
!= inode
->i_gid
)
1796 transfer_to
[GRPQUOTA
] = dqget(sb
, iattr
->ia_gid
, GRPQUOTA
);
1798 ret
= __dquot_transfer(inode
, transfer_to
);
1799 dqput_all(transfer_to
);
1802 EXPORT_SYMBOL(dquot_transfer
);
1805 * Write info of quota file to disk
1807 int dquot_commit_info(struct super_block
*sb
, int type
)
1810 struct quota_info
*dqopt
= sb_dqopt(sb
);
1812 mutex_lock(&dqopt
->dqio_mutex
);
1813 ret
= dqopt
->ops
[type
]->write_file_info(sb
, type
);
1814 mutex_unlock(&dqopt
->dqio_mutex
);
1817 EXPORT_SYMBOL(dquot_commit_info
);
1820 * Definitions of diskquota operations.
1822 const struct dquot_operations dquot_operations
= {
1823 .write_dquot
= dquot_commit
,
1824 .acquire_dquot
= dquot_acquire
,
1825 .release_dquot
= dquot_release
,
1826 .mark_dirty
= dquot_mark_dquot_dirty
,
1827 .write_info
= dquot_commit_info
,
1828 .alloc_dquot
= dquot_alloc
,
1829 .destroy_dquot
= dquot_destroy
,
1831 EXPORT_SYMBOL(dquot_operations
);
1834 * Generic helper for ->open on filesystems supporting disk quotas.
1836 int dquot_file_open(struct inode
*inode
, struct file
*file
)
1840 error
= generic_file_open(inode
, file
);
1841 if (!error
&& (file
->f_mode
& FMODE_WRITE
))
1842 dquot_initialize(inode
);
1845 EXPORT_SYMBOL(dquot_file_open
);
1848 * Turn quota off on a device. type == -1 ==> quotaoff for all types (umount)
1850 int dquot_disable(struct super_block
*sb
, int type
, unsigned int flags
)
1853 struct quota_info
*dqopt
= sb_dqopt(sb
);
1854 struct inode
*toputinode
[MAXQUOTAS
];
1856 /* Cannot turn off usage accounting without turning off limits, or
1857 * suspend quotas and simultaneously turn quotas off. */
1858 if ((flags
& DQUOT_USAGE_ENABLED
&& !(flags
& DQUOT_LIMITS_ENABLED
))
1859 || (flags
& DQUOT_SUSPENDED
&& flags
& (DQUOT_LIMITS_ENABLED
|
1860 DQUOT_USAGE_ENABLED
)))
1863 /* We need to serialize quota_off() for device */
1864 mutex_lock(&dqopt
->dqonoff_mutex
);
1867 * Skip everything if there's nothing to do. We have to do this because
1868 * sometimes we are called when fill_super() failed and calling
1869 * sync_fs() in such cases does no good.
1871 if (!sb_any_quota_loaded(sb
)) {
1872 mutex_unlock(&dqopt
->dqonoff_mutex
);
1875 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++) {
1876 toputinode
[cnt
] = NULL
;
1877 if (type
!= -1 && cnt
!= type
)
1879 if (!sb_has_quota_loaded(sb
, cnt
))
1882 if (flags
& DQUOT_SUSPENDED
) {
1883 spin_lock(&dq_state_lock
);
1885 dquot_state_flag(DQUOT_SUSPENDED
, cnt
);
1886 spin_unlock(&dq_state_lock
);
1888 spin_lock(&dq_state_lock
);
1889 dqopt
->flags
&= ~dquot_state_flag(flags
, cnt
);
1890 /* Turning off suspended quotas? */
1891 if (!sb_has_quota_loaded(sb
, cnt
) &&
1892 sb_has_quota_suspended(sb
, cnt
)) {
1893 dqopt
->flags
&= ~dquot_state_flag(
1894 DQUOT_SUSPENDED
, cnt
);
1895 spin_unlock(&dq_state_lock
);
1896 iput(dqopt
->files
[cnt
]);
1897 dqopt
->files
[cnt
] = NULL
;
1900 spin_unlock(&dq_state_lock
);
1903 /* We still have to keep quota loaded? */
1904 if (sb_has_quota_loaded(sb
, cnt
) && !(flags
& DQUOT_SUSPENDED
))
1907 /* Note: these are blocking operations */
1908 drop_dquot_ref(sb
, cnt
);
1909 invalidate_dquots(sb
, cnt
);
1911 * Now all dquots should be invalidated, all writes done so we
1912 * should be only users of the info. No locks needed.
1914 if (info_dirty(&dqopt
->info
[cnt
]))
1915 sb
->dq_op
->write_info(sb
, cnt
);
1916 if (dqopt
->ops
[cnt
]->free_file_info
)
1917 dqopt
->ops
[cnt
]->free_file_info(sb
, cnt
);
1918 put_quota_format(dqopt
->info
[cnt
].dqi_format
);
1920 toputinode
[cnt
] = dqopt
->files
[cnt
];
1921 if (!sb_has_quota_loaded(sb
, cnt
))
1922 dqopt
->files
[cnt
] = NULL
;
1923 dqopt
->info
[cnt
].dqi_flags
= 0;
1924 dqopt
->info
[cnt
].dqi_igrace
= 0;
1925 dqopt
->info
[cnt
].dqi_bgrace
= 0;
1926 dqopt
->ops
[cnt
] = NULL
;
1928 mutex_unlock(&dqopt
->dqonoff_mutex
);
1930 /* Skip syncing and setting flags if quota files are hidden */
1931 if (dqopt
->flags
& DQUOT_QUOTA_SYS_FILE
)
1934 /* Sync the superblock so that buffers with quota data are written to
1935 * disk (and so userspace sees correct data afterwards). */
1936 if (sb
->s_op
->sync_fs
)
1937 sb
->s_op
->sync_fs(sb
, 1);
1938 sync_blockdev(sb
->s_bdev
);
1939 /* Now the quota files are just ordinary files and we can set the
1940 * inode flags back. Moreover we discard the pagecache so that
1941 * userspace sees the writes we did bypassing the pagecache. We
1942 * must also discard the blockdev buffers so that we see the
1943 * changes done by userspace on the next quotaon() */
1944 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++)
1945 if (toputinode
[cnt
]) {
1946 mutex_lock(&dqopt
->dqonoff_mutex
);
1947 /* If quota was reenabled in the meantime, we have
1949 if (!sb_has_quota_loaded(sb
, cnt
)) {
1950 mutex_lock_nested(&toputinode
[cnt
]->i_mutex
,
1952 toputinode
[cnt
]->i_flags
&= ~(S_IMMUTABLE
|
1953 S_NOATIME
| S_NOQUOTA
);
1954 truncate_inode_pages(&toputinode
[cnt
]->i_data
,
1956 mutex_unlock(&toputinode
[cnt
]->i_mutex
);
1957 mark_inode_dirty(toputinode
[cnt
]);
1959 mutex_unlock(&dqopt
->dqonoff_mutex
);
1962 invalidate_bdev(sb
->s_bdev
);
1964 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++)
1965 if (toputinode
[cnt
]) {
1966 /* On remount RO, we keep the inode pointer so that we
1967 * can reenable quota on the subsequent remount RW. We
1968 * have to check 'flags' variable and not use sb_has_
1969 * function because another quotaon / quotaoff could
1970 * change global state before we got here. We refuse
1971 * to suspend quotas when there is pending delete on
1972 * the quota file... */
1973 if (!(flags
& DQUOT_SUSPENDED
))
1974 iput(toputinode
[cnt
]);
1975 else if (!toputinode
[cnt
]->i_nlink
)
1980 EXPORT_SYMBOL(dquot_disable
);
1982 int dquot_quota_off(struct super_block
*sb
, int type
)
1984 return dquot_disable(sb
, type
,
1985 DQUOT_USAGE_ENABLED
| DQUOT_LIMITS_ENABLED
);
1987 EXPORT_SYMBOL(dquot_quota_off
);
1990 * Turn quotas on on a device
1994 * Helper function to turn quotas on when we already have the inode of
1995 * quota file and no quota information is loaded.
1997 static int vfs_load_quota_inode(struct inode
*inode
, int type
, int format_id
,
2000 struct quota_format_type
*fmt
= find_quota_format(format_id
);
2001 struct super_block
*sb
= inode
->i_sb
;
2002 struct quota_info
*dqopt
= sb_dqopt(sb
);
2008 if (!S_ISREG(inode
->i_mode
)) {
2012 if (IS_RDONLY(inode
)) {
2016 if (!sb
->s_op
->quota_write
|| !sb
->s_op
->quota_read
) {
2020 /* Usage always has to be set... */
2021 if (!(flags
& DQUOT_USAGE_ENABLED
)) {
2026 if (!(dqopt
->flags
& DQUOT_QUOTA_SYS_FILE
)) {
2027 /* As we bypass the pagecache we must now flush all the
2028 * dirty data and invalidate caches so that kernel sees
2029 * changes from userspace. It is not enough to just flush
2030 * the quota file since if blocksize < pagesize, invalidation
2031 * of the cache could fail because of other unrelated dirty
2033 sync_filesystem(sb
);
2034 invalidate_bdev(sb
->s_bdev
);
2036 mutex_lock(&dqopt
->dqonoff_mutex
);
2037 if (sb_has_quota_loaded(sb
, type
)) {
2042 if (!(dqopt
->flags
& DQUOT_QUOTA_SYS_FILE
)) {
2043 /* We don't want quota and atime on quota files (deadlocks
2044 * possible) Also nobody should write to the file - we use
2045 * special IO operations which ignore the immutable bit. */
2046 mutex_lock_nested(&inode
->i_mutex
, I_MUTEX_QUOTA
);
2047 oldflags
= inode
->i_flags
& (S_NOATIME
| S_IMMUTABLE
|
2049 inode
->i_flags
|= S_NOQUOTA
| S_NOATIME
| S_IMMUTABLE
;
2050 mutex_unlock(&inode
->i_mutex
);
2052 * When S_NOQUOTA is set, remove dquot references as no more
2053 * references can be added
2055 __dquot_drop(inode
);
2059 dqopt
->files
[type
] = igrab(inode
);
2060 if (!dqopt
->files
[type
])
2063 if (!fmt
->qf_ops
->check_quota_file(sb
, type
))
2066 dqopt
->ops
[type
] = fmt
->qf_ops
;
2067 dqopt
->info
[type
].dqi_format
= fmt
;
2068 dqopt
->info
[type
].dqi_fmt_id
= format_id
;
2069 INIT_LIST_HEAD(&dqopt
->info
[type
].dqi_dirty_list
);
2070 mutex_lock(&dqopt
->dqio_mutex
);
2071 error
= dqopt
->ops
[type
]->read_file_info(sb
, type
);
2073 mutex_unlock(&dqopt
->dqio_mutex
);
2076 mutex_unlock(&dqopt
->dqio_mutex
);
2077 spin_lock(&dq_state_lock
);
2078 dqopt
->flags
|= dquot_state_flag(flags
, type
);
2079 spin_unlock(&dq_state_lock
);
2081 add_dquot_ref(sb
, type
);
2082 mutex_unlock(&dqopt
->dqonoff_mutex
);
2087 dqopt
->files
[type
] = NULL
;
2090 if (oldflags
!= -1) {
2091 mutex_lock_nested(&inode
->i_mutex
, I_MUTEX_QUOTA
);
2092 /* Set the flags back (in the case of accidental quotaon()
2093 * on a wrong file we don't want to mess up the flags) */
2094 inode
->i_flags
&= ~(S_NOATIME
| S_NOQUOTA
| S_IMMUTABLE
);
2095 inode
->i_flags
|= oldflags
;
2096 mutex_unlock(&inode
->i_mutex
);
2098 mutex_unlock(&dqopt
->dqonoff_mutex
);
2100 put_quota_format(fmt
);
2105 /* Reenable quotas on remount RW */
2106 int dquot_resume(struct super_block
*sb
, int type
)
2108 struct quota_info
*dqopt
= sb_dqopt(sb
);
2109 struct inode
*inode
;
2113 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++) {
2114 if (type
!= -1 && cnt
!= type
)
2117 mutex_lock(&dqopt
->dqonoff_mutex
);
2118 if (!sb_has_quota_suspended(sb
, cnt
)) {
2119 mutex_unlock(&dqopt
->dqonoff_mutex
);
2122 inode
= dqopt
->files
[cnt
];
2123 dqopt
->files
[cnt
] = NULL
;
2124 spin_lock(&dq_state_lock
);
2125 flags
= dqopt
->flags
& dquot_state_flag(DQUOT_USAGE_ENABLED
|
2126 DQUOT_LIMITS_ENABLED
,
2128 dqopt
->flags
&= ~dquot_state_flag(DQUOT_STATE_FLAGS
, cnt
);
2129 spin_unlock(&dq_state_lock
);
2130 mutex_unlock(&dqopt
->dqonoff_mutex
);
2132 flags
= dquot_generic_flag(flags
, cnt
);
2133 ret
= vfs_load_quota_inode(inode
, cnt
,
2134 dqopt
->info
[cnt
].dqi_fmt_id
, flags
);
2140 EXPORT_SYMBOL(dquot_resume
);
2142 int dquot_quota_on_path(struct super_block
*sb
, int type
, int format_id
,
2145 int error
= security_quota_on(path
->dentry
);
2148 /* Quota file not on the same filesystem? */
2149 if (path
->mnt
->mnt_sb
!= sb
)
2152 error
= vfs_load_quota_inode(path
->dentry
->d_inode
, type
,
2153 format_id
, DQUOT_USAGE_ENABLED
|
2154 DQUOT_LIMITS_ENABLED
);
2157 EXPORT_SYMBOL(dquot_quota_on_path
);
2159 int dquot_quota_on(struct super_block
*sb
, int type
, int format_id
, char *name
)
2164 error
= kern_path(name
, LOOKUP_FOLLOW
, &path
);
2166 error
= dquot_quota_on_path(sb
, type
, format_id
, &path
);
2171 EXPORT_SYMBOL(dquot_quota_on
);
2174 * More powerful function for turning on quotas allowing setting
2175 * of individual quota flags
2177 int dquot_enable(struct inode
*inode
, int type
, int format_id
,
2181 struct super_block
*sb
= inode
->i_sb
;
2182 struct quota_info
*dqopt
= sb_dqopt(sb
);
2184 /* Just unsuspend quotas? */
2185 BUG_ON(flags
& DQUOT_SUSPENDED
);
2189 /* Just updating flags needed? */
2190 if (sb_has_quota_loaded(sb
, type
)) {
2191 mutex_lock(&dqopt
->dqonoff_mutex
);
2192 /* Now do a reliable test... */
2193 if (!sb_has_quota_loaded(sb
, type
)) {
2194 mutex_unlock(&dqopt
->dqonoff_mutex
);
2197 if (flags
& DQUOT_USAGE_ENABLED
&&
2198 sb_has_quota_usage_enabled(sb
, type
)) {
2202 if (flags
& DQUOT_LIMITS_ENABLED
&&
2203 sb_has_quota_limits_enabled(sb
, type
)) {
2207 spin_lock(&dq_state_lock
);
2208 sb_dqopt(sb
)->flags
|= dquot_state_flag(flags
, type
);
2209 spin_unlock(&dq_state_lock
);
2211 mutex_unlock(&dqopt
->dqonoff_mutex
);
2216 return vfs_load_quota_inode(inode
, type
, format_id
, flags
);
2218 EXPORT_SYMBOL(dquot_enable
);
2221 * This function is used when filesystem needs to initialize quotas
2222 * during mount time.
2224 int dquot_quota_on_mount(struct super_block
*sb
, char *qf_name
,
2225 int format_id
, int type
)
2227 struct dentry
*dentry
;
2230 mutex_lock(&sb
->s_root
->d_inode
->i_mutex
);
2231 dentry
= lookup_one_len(qf_name
, sb
->s_root
, strlen(qf_name
));
2232 mutex_unlock(&sb
->s_root
->d_inode
->i_mutex
);
2234 return PTR_ERR(dentry
);
2236 if (!dentry
->d_inode
) {
2241 error
= security_quota_on(dentry
);
2243 error
= vfs_load_quota_inode(dentry
->d_inode
, type
, format_id
,
2244 DQUOT_USAGE_ENABLED
| DQUOT_LIMITS_ENABLED
);
2250 EXPORT_SYMBOL(dquot_quota_on_mount
);
2252 static inline qsize_t
qbtos(qsize_t blocks
)
2254 return blocks
<< QIF_DQBLKSIZE_BITS
;
2257 static inline qsize_t
stoqb(qsize_t space
)
2259 return (space
+ QIF_DQBLKSIZE
- 1) >> QIF_DQBLKSIZE_BITS
;
2262 /* Generic routine for getting common part of quota structure */
2263 static void do_get_dqblk(struct dquot
*dquot
, struct fs_disk_quota
*di
)
2265 struct mem_dqblk
*dm
= &dquot
->dq_dqb
;
2267 memset(di
, 0, sizeof(*di
));
2268 di
->d_version
= FS_DQUOT_VERSION
;
2269 di
->d_flags
= dquot
->dq_type
== USRQUOTA
?
2270 XFS_USER_QUOTA
: XFS_GROUP_QUOTA
;
2271 di
->d_id
= dquot
->dq_id
;
2273 spin_lock(&dq_data_lock
);
2274 di
->d_blk_hardlimit
= stoqb(dm
->dqb_bhardlimit
);
2275 di
->d_blk_softlimit
= stoqb(dm
->dqb_bsoftlimit
);
2276 di
->d_ino_hardlimit
= dm
->dqb_ihardlimit
;
2277 di
->d_ino_softlimit
= dm
->dqb_isoftlimit
;
2278 di
->d_bcount
= dm
->dqb_curspace
+ dm
->dqb_rsvspace
;
2279 di
->d_icount
= dm
->dqb_curinodes
;
2280 di
->d_btimer
= dm
->dqb_btime
;
2281 di
->d_itimer
= dm
->dqb_itime
;
2282 spin_unlock(&dq_data_lock
);
2285 int dquot_get_dqblk(struct super_block
*sb
, int type
, qid_t id
,
2286 struct fs_disk_quota
*di
)
2288 struct dquot
*dquot
;
2290 dquot
= dqget(sb
, id
, type
);
2293 do_get_dqblk(dquot
, di
);
2298 EXPORT_SYMBOL(dquot_get_dqblk
);
2300 #define VFS_FS_DQ_MASK \
2301 (FS_DQ_BCOUNT | FS_DQ_BSOFT | FS_DQ_BHARD | \
2302 FS_DQ_ICOUNT | FS_DQ_ISOFT | FS_DQ_IHARD | \
2303 FS_DQ_BTIMER | FS_DQ_ITIMER)
2305 /* Generic routine for setting common part of quota structure */
2306 static int do_set_dqblk(struct dquot
*dquot
, struct fs_disk_quota
*di
)
2308 struct mem_dqblk
*dm
= &dquot
->dq_dqb
;
2309 int check_blim
= 0, check_ilim
= 0;
2310 struct mem_dqinfo
*dqi
= &sb_dqopt(dquot
->dq_sb
)->info
[dquot
->dq_type
];
2312 if (di
->d_fieldmask
& ~VFS_FS_DQ_MASK
)
2315 if (((di
->d_fieldmask
& FS_DQ_BSOFT
) &&
2316 (di
->d_blk_softlimit
> dqi
->dqi_maxblimit
)) ||
2317 ((di
->d_fieldmask
& FS_DQ_BHARD
) &&
2318 (di
->d_blk_hardlimit
> dqi
->dqi_maxblimit
)) ||
2319 ((di
->d_fieldmask
& FS_DQ_ISOFT
) &&
2320 (di
->d_ino_softlimit
> dqi
->dqi_maxilimit
)) ||
2321 ((di
->d_fieldmask
& FS_DQ_IHARD
) &&
2322 (di
->d_ino_hardlimit
> dqi
->dqi_maxilimit
)))
2325 spin_lock(&dq_data_lock
);
2326 if (di
->d_fieldmask
& FS_DQ_BCOUNT
) {
2327 dm
->dqb_curspace
= di
->d_bcount
- dm
->dqb_rsvspace
;
2329 set_bit(DQ_LASTSET_B
+ QIF_SPACE_B
, &dquot
->dq_flags
);
2332 if (di
->d_fieldmask
& FS_DQ_BSOFT
)
2333 dm
->dqb_bsoftlimit
= qbtos(di
->d_blk_softlimit
);
2334 if (di
->d_fieldmask
& FS_DQ_BHARD
)
2335 dm
->dqb_bhardlimit
= qbtos(di
->d_blk_hardlimit
);
2336 if (di
->d_fieldmask
& (FS_DQ_BSOFT
| FS_DQ_BHARD
)) {
2338 set_bit(DQ_LASTSET_B
+ QIF_BLIMITS_B
, &dquot
->dq_flags
);
2341 if (di
->d_fieldmask
& FS_DQ_ICOUNT
) {
2342 dm
->dqb_curinodes
= di
->d_icount
;
2344 set_bit(DQ_LASTSET_B
+ QIF_INODES_B
, &dquot
->dq_flags
);
2347 if (di
->d_fieldmask
& FS_DQ_ISOFT
)
2348 dm
->dqb_isoftlimit
= di
->d_ino_softlimit
;
2349 if (di
->d_fieldmask
& FS_DQ_IHARD
)
2350 dm
->dqb_ihardlimit
= di
->d_ino_hardlimit
;
2351 if (di
->d_fieldmask
& (FS_DQ_ISOFT
| FS_DQ_IHARD
)) {
2353 set_bit(DQ_LASTSET_B
+ QIF_ILIMITS_B
, &dquot
->dq_flags
);
2356 if (di
->d_fieldmask
& FS_DQ_BTIMER
) {
2357 dm
->dqb_btime
= di
->d_btimer
;
2359 set_bit(DQ_LASTSET_B
+ QIF_BTIME_B
, &dquot
->dq_flags
);
2362 if (di
->d_fieldmask
& FS_DQ_ITIMER
) {
2363 dm
->dqb_itime
= di
->d_itimer
;
2365 set_bit(DQ_LASTSET_B
+ QIF_ITIME_B
, &dquot
->dq_flags
);
2369 if (!dm
->dqb_bsoftlimit
||
2370 dm
->dqb_curspace
< dm
->dqb_bsoftlimit
) {
2372 clear_bit(DQ_BLKS_B
, &dquot
->dq_flags
);
2373 } else if (!(di
->d_fieldmask
& FS_DQ_BTIMER
))
2374 /* Set grace only if user hasn't provided his own... */
2375 dm
->dqb_btime
= get_seconds() + dqi
->dqi_bgrace
;
2378 if (!dm
->dqb_isoftlimit
||
2379 dm
->dqb_curinodes
< dm
->dqb_isoftlimit
) {
2381 clear_bit(DQ_INODES_B
, &dquot
->dq_flags
);
2382 } else if (!(di
->d_fieldmask
& FS_DQ_ITIMER
))
2383 /* Set grace only if user hasn't provided his own... */
2384 dm
->dqb_itime
= get_seconds() + dqi
->dqi_igrace
;
2386 if (dm
->dqb_bhardlimit
|| dm
->dqb_bsoftlimit
|| dm
->dqb_ihardlimit
||
2388 clear_bit(DQ_FAKE_B
, &dquot
->dq_flags
);
2390 set_bit(DQ_FAKE_B
, &dquot
->dq_flags
);
2391 spin_unlock(&dq_data_lock
);
2392 mark_dquot_dirty(dquot
);
2397 int dquot_set_dqblk(struct super_block
*sb
, int type
, qid_t id
,
2398 struct fs_disk_quota
*di
)
2400 struct dquot
*dquot
;
2403 dquot
= dqget(sb
, id
, type
);
2408 rc
= do_set_dqblk(dquot
, di
);
2413 EXPORT_SYMBOL(dquot_set_dqblk
);
2415 /* Generic routine for getting common part of quota file information */
2416 int dquot_get_dqinfo(struct super_block
*sb
, int type
, struct if_dqinfo
*ii
)
2418 struct mem_dqinfo
*mi
;
2420 mutex_lock(&sb_dqopt(sb
)->dqonoff_mutex
);
2421 if (!sb_has_quota_active(sb
, type
)) {
2422 mutex_unlock(&sb_dqopt(sb
)->dqonoff_mutex
);
2425 mi
= sb_dqopt(sb
)->info
+ type
;
2426 spin_lock(&dq_data_lock
);
2427 ii
->dqi_bgrace
= mi
->dqi_bgrace
;
2428 ii
->dqi_igrace
= mi
->dqi_igrace
;
2429 ii
->dqi_flags
= mi
->dqi_flags
& DQF_MASK
;
2430 ii
->dqi_valid
= IIF_ALL
;
2431 spin_unlock(&dq_data_lock
);
2432 mutex_unlock(&sb_dqopt(sb
)->dqonoff_mutex
);
2435 EXPORT_SYMBOL(dquot_get_dqinfo
);
2437 /* Generic routine for setting common part of quota file information */
2438 int dquot_set_dqinfo(struct super_block
*sb
, int type
, struct if_dqinfo
*ii
)
2440 struct mem_dqinfo
*mi
;
2443 mutex_lock(&sb_dqopt(sb
)->dqonoff_mutex
);
2444 if (!sb_has_quota_active(sb
, type
)) {
2448 mi
= sb_dqopt(sb
)->info
+ type
;
2449 spin_lock(&dq_data_lock
);
2450 if (ii
->dqi_valid
& IIF_BGRACE
)
2451 mi
->dqi_bgrace
= ii
->dqi_bgrace
;
2452 if (ii
->dqi_valid
& IIF_IGRACE
)
2453 mi
->dqi_igrace
= ii
->dqi_igrace
;
2454 if (ii
->dqi_valid
& IIF_FLAGS
)
2455 mi
->dqi_flags
= (mi
->dqi_flags
& ~DQF_MASK
) |
2456 (ii
->dqi_flags
& DQF_MASK
);
2457 spin_unlock(&dq_data_lock
);
2458 mark_info_dirty(sb
, type
);
2459 /* Force write to disk */
2460 sb
->dq_op
->write_info(sb
, type
);
2462 mutex_unlock(&sb_dqopt(sb
)->dqonoff_mutex
);
2465 EXPORT_SYMBOL(dquot_set_dqinfo
);
2467 const struct quotactl_ops dquot_quotactl_ops
= {
2468 .quota_on
= dquot_quota_on
,
2469 .quota_off
= dquot_quota_off
,
2470 .quota_sync
= dquot_quota_sync
,
2471 .get_info
= dquot_get_dqinfo
,
2472 .set_info
= dquot_set_dqinfo
,
2473 .get_dqblk
= dquot_get_dqblk
,
2474 .set_dqblk
= dquot_set_dqblk
2476 EXPORT_SYMBOL(dquot_quotactl_ops
);
2478 static int do_proc_dqstats(struct ctl_table
*table
, int write
,
2479 void __user
*buffer
, size_t *lenp
, loff_t
*ppos
)
2481 unsigned int type
= (int *)table
->data
- dqstats
.stat
;
2483 /* Update global table */
2484 dqstats
.stat
[type
] =
2485 percpu_counter_sum_positive(&dqstats
.counter
[type
]);
2486 return proc_dointvec(table
, write
, buffer
, lenp
, ppos
);
2489 static ctl_table fs_dqstats_table
[] = {
2491 .procname
= "lookups",
2492 .data
= &dqstats
.stat
[DQST_LOOKUPS
],
2493 .maxlen
= sizeof(int),
2495 .proc_handler
= do_proc_dqstats
,
2498 .procname
= "drops",
2499 .data
= &dqstats
.stat
[DQST_DROPS
],
2500 .maxlen
= sizeof(int),
2502 .proc_handler
= do_proc_dqstats
,
2505 .procname
= "reads",
2506 .data
= &dqstats
.stat
[DQST_READS
],
2507 .maxlen
= sizeof(int),
2509 .proc_handler
= do_proc_dqstats
,
2512 .procname
= "writes",
2513 .data
= &dqstats
.stat
[DQST_WRITES
],
2514 .maxlen
= sizeof(int),
2516 .proc_handler
= do_proc_dqstats
,
2519 .procname
= "cache_hits",
2520 .data
= &dqstats
.stat
[DQST_CACHE_HITS
],
2521 .maxlen
= sizeof(int),
2523 .proc_handler
= do_proc_dqstats
,
2526 .procname
= "allocated_dquots",
2527 .data
= &dqstats
.stat
[DQST_ALLOC_DQUOTS
],
2528 .maxlen
= sizeof(int),
2530 .proc_handler
= do_proc_dqstats
,
2533 .procname
= "free_dquots",
2534 .data
= &dqstats
.stat
[DQST_FREE_DQUOTS
],
2535 .maxlen
= sizeof(int),
2537 .proc_handler
= do_proc_dqstats
,
2540 .procname
= "syncs",
2541 .data
= &dqstats
.stat
[DQST_SYNCS
],
2542 .maxlen
= sizeof(int),
2544 .proc_handler
= do_proc_dqstats
,
2546 #ifdef CONFIG_PRINT_QUOTA_WARNING
2548 .procname
= "warnings",
2549 .data
= &flag_print_warnings
,
2550 .maxlen
= sizeof(int),
2552 .proc_handler
= proc_dointvec
,
2558 static ctl_table fs_table
[] = {
2560 .procname
= "quota",
2562 .child
= fs_dqstats_table
,
2567 static ctl_table sys_table
[] = {
2576 static int __init
dquot_init(void)
2579 unsigned long nr_hash
, order
;
2581 printk(KERN_NOTICE
"VFS: Disk quotas %s\n", __DQUOT_VERSION__
);
2583 register_sysctl_table(sys_table
);
2585 dquot_cachep
= kmem_cache_create("dquot",
2586 sizeof(struct dquot
), sizeof(unsigned long) * 4,
2587 (SLAB_HWCACHE_ALIGN
|SLAB_RECLAIM_ACCOUNT
|
2588 SLAB_MEM_SPREAD
|SLAB_PANIC
),
2592 dquot_hash
= (struct hlist_head
*)__get_free_pages(GFP_ATOMIC
, order
);
2594 panic("Cannot create dquot hash table");
2596 for (i
= 0; i
< _DQST_DQSTAT_LAST
; i
++) {
2597 ret
= percpu_counter_init(&dqstats
.counter
[i
], 0);
2599 panic("Cannot create dquot stat counters");
2602 /* Find power-of-two hlist_heads which can fit into allocation */
2603 nr_hash
= (1UL << order
) * PAGE_SIZE
/ sizeof(struct hlist_head
);
2607 } while (nr_hash
>> dq_hash_bits
);
2610 nr_hash
= 1UL << dq_hash_bits
;
2611 dq_hash_mask
= nr_hash
- 1;
2612 for (i
= 0; i
< nr_hash
; i
++)
2613 INIT_HLIST_HEAD(dquot_hash
+ i
);
2615 printk("Dquot-cache hash table entries: %ld (order %ld, %ld bytes)\n",
2616 nr_hash
, order
, (PAGE_SIZE
<< order
));
2618 register_shrinker(&dqcache_shrinker
);
2622 module_init(dquot_init
);