2 * Implementation of the diskquota system for the LINUX operating system. QUOTA
3 * is implemented using the BSD system call interface as the means of
4 * communication with the user level. This file contains the generic routines
5 * called by the different filesystems on allocation of an inode or block.
6 * These routines take care of the administration needed to have a consistent
7 * diskquota tracking system. The ideas of both user and group quotas are based
8 * on the Melbourne quota system as used on BSD derived systems. The internal
9 * implementation is based on one of the several variants of the LINUX
10 * inode-subsystem with added complexity of the diskquota system.
12 * Author: Marco van Wieringen <mvw@planets.elm.net>
14 * Fixes: Dmitry Gorodchanin <pgmdsg@ibi.com>, 11 Feb 96
16 * Revised list management to avoid races
17 * -- Bill Hawes, <whawes@star.net>, 9/98
19 * Fixed races in dquot_transfer(), dqget() and dquot_alloc_...().
20 * As the consequence the locking was moved from dquot_decr_...(),
21 * dquot_incr_...() to calling functions.
22 * invalidate_dquots() now writes modified dquots.
23 * Serialized quota_off() and quota_on() for mount point.
24 * Fixed a few bugs in grow_dquots().
25 * Fixed deadlock in write_dquot() - we no longer account quotas on
27 * remove_dquot_ref() moved to inode.c - it now traverses through inodes
28 * add_dquot_ref() restarts after blocking
29 * Added check for bogus uid and fixed check for group in quotactl.
30 * Jan Kara, <jack@suse.cz>, sponsored by SuSE CR, 10-11/99
32 * Used struct list_head instead of own list struct
33 * Invalidation of referenced dquots is no longer possible
34 * Improved free_dquots list management
35 * Quota and i_blocks are now updated in one place to avoid races
36 * Warnings are now delayed so we won't block in critical section
37 * Write updated not to require dquot lock
38 * Jan Kara, <jack@suse.cz>, 9/2000
40 * Added dynamic quota structure allocation
41 * Jan Kara <jack@suse.cz> 12/2000
43 * Rewritten quota interface. Implemented new quota format and
44 * formats registering.
45 * Jan Kara, <jack@suse.cz>, 2001,2002
48 * Jan Kara, <jack@suse.cz>, 10/2002
50 * Added journalled quota support, fix lock inversion problems
51 * Jan Kara, <jack@suse.cz>, 2003,2004
53 * (C) Copyright 1994 - 1997 Marco van Wieringen
56 #include <linux/errno.h>
57 #include <linux/kernel.h>
59 #include <linux/mount.h>
61 #include <linux/time.h>
62 #include <linux/types.h>
63 #include <linux/string.h>
64 #include <linux/fcntl.h>
65 #include <linux/stat.h>
66 #include <linux/tty.h>
67 #include <linux/file.h>
68 #include <linux/slab.h>
69 #include <linux/sysctl.h>
70 #include <linux/init.h>
71 #include <linux/module.h>
72 #include <linux/proc_fs.h>
73 #include <linux/security.h>
74 #include <linux/kmod.h>
75 #include <linux/namei.h>
76 #include <linux/buffer_head.h>
77 #include <linux/capability.h>
78 #include <linux/quotaops.h>
79 #include <linux/writeback.h> /* for inode_lock, oddly enough.. */
81 #include <asm/uaccess.h>
83 #define __DQUOT_PARANOIA
86 * There are three quota SMP locks. dq_list_lock protects all lists with quotas
87 * and quota formats, dqstats structure containing statistics about the lists
88 * dq_data_lock protects data from dq_dqb and also mem_dqinfo structures and
89 * also guards consistency of dquot->dq_dqb with inode->i_blocks, i_bytes.
90 * i_blocks and i_bytes updates itself are guarded by i_lock acquired directly
91 * in inode_add_bytes() and inode_sub_bytes(). dq_state_lock protects
92 * modifications of quota state (on quotaon and quotaoff) and readers who care
93 * about latest values take it as well.
95 * The spinlock ordering is hence: dq_data_lock > dq_list_lock > i_lock,
96 * dq_list_lock > dq_state_lock
98 * Note that some things (eg. sb pointer, type, id) doesn't change during
99 * the life of the dquot structure and so needn't to be protected by a lock
101 * Any operation working on dquots via inode pointers must hold dqptr_sem. If
102 * operation is just reading pointers from inode (or not using them at all) the
103 * read lock is enough. If pointers are altered function must hold write lock
104 * (these locking rules also apply for S_NOQUOTA flag in the inode - note that
105 * for altering the flag i_mutex is also needed).
107 * Each dquot has its dq_lock mutex. Locked dquots might not be referenced
108 * from inodes (dquot_alloc_space() and such don't check the dq_lock).
109 * Currently dquot is locked only when it is being read to memory (or space for
110 * it is being allocated) on the first dqget() and when it is being released on
111 * the last dqput(). The allocation and release oparations are serialized by
112 * the dq_lock and by checking the use count in dquot_release(). Write
113 * operations on dquots don't hold dq_lock as they copy data under dq_data_lock
114 * spinlock to internal buffers before writing.
116 * Lock ordering (including related VFS locks) is the following:
117 * i_mutex > dqonoff_sem > journal_lock > dqptr_sem > dquot->dq_lock >
119 * The lock ordering of dqptr_sem imposed by quota code is only dqonoff_sem >
120 * dqptr_sem. But filesystem has to count with the fact that functions such as
121 * dquot_alloc_space() acquire dqptr_sem and they usually have to be called
122 * from inside a transaction to keep filesystem consistency after a crash. Also
123 * filesystems usually want to do some IO on dquot from ->mark_dirty which is
124 * called with dqptr_sem held.
125 * i_mutex on quota files is special (it's below dqio_mutex)
128 static __cacheline_aligned_in_smp
DEFINE_SPINLOCK(dq_list_lock
);
129 static __cacheline_aligned_in_smp
DEFINE_SPINLOCK(dq_state_lock
);
130 __cacheline_aligned_in_smp
DEFINE_SPINLOCK(dq_data_lock
);
131 EXPORT_SYMBOL(dq_data_lock
);
133 static char *quotatypes
[] = INITQFNAMES
;
134 static struct quota_format_type
*quota_formats
; /* List of registered formats */
135 static struct quota_module_name module_names
[] = INIT_QUOTA_MODULE_NAMES
;
137 /* SLAB cache for dquot structures */
138 static struct kmem_cache
*dquot_cachep
;
140 int register_quota_format(struct quota_format_type
*fmt
)
142 spin_lock(&dq_list_lock
);
143 fmt
->qf_next
= quota_formats
;
145 spin_unlock(&dq_list_lock
);
148 EXPORT_SYMBOL(register_quota_format
);
150 void unregister_quota_format(struct quota_format_type
*fmt
)
152 struct quota_format_type
**actqf
;
154 spin_lock(&dq_list_lock
);
155 for (actqf
= "a_formats
; *actqf
&& *actqf
!= fmt
;
156 actqf
= &(*actqf
)->qf_next
)
159 *actqf
= (*actqf
)->qf_next
;
160 spin_unlock(&dq_list_lock
);
162 EXPORT_SYMBOL(unregister_quota_format
);
164 static struct quota_format_type
*find_quota_format(int id
)
166 struct quota_format_type
*actqf
;
168 spin_lock(&dq_list_lock
);
169 for (actqf
= quota_formats
; actqf
&& actqf
->qf_fmt_id
!= id
;
170 actqf
= actqf
->qf_next
)
172 if (!actqf
|| !try_module_get(actqf
->qf_owner
)) {
175 spin_unlock(&dq_list_lock
);
177 for (qm
= 0; module_names
[qm
].qm_fmt_id
&&
178 module_names
[qm
].qm_fmt_id
!= id
; qm
++)
180 if (!module_names
[qm
].qm_fmt_id
||
181 request_module(module_names
[qm
].qm_mod_name
))
184 spin_lock(&dq_list_lock
);
185 for (actqf
= quota_formats
; actqf
&& actqf
->qf_fmt_id
!= id
;
186 actqf
= actqf
->qf_next
)
188 if (actqf
&& !try_module_get(actqf
->qf_owner
))
191 spin_unlock(&dq_list_lock
);
195 static void put_quota_format(struct quota_format_type
*fmt
)
197 module_put(fmt
->qf_owner
);
201 * Dquot List Management:
202 * The quota code uses three lists for dquot management: the inuse_list,
203 * free_dquots, and dquot_hash[] array. A single dquot structure may be
204 * on all three lists, depending on its current state.
206 * All dquots are placed to the end of inuse_list when first created, and this
207 * list is used for invalidate operation, which must look at every dquot.
209 * Unused dquots (dq_count == 0) are added to the free_dquots list when freed,
210 * and this list is searched whenever we need an available dquot. Dquots are
211 * removed from the list as soon as they are used again, and
212 * dqstats.free_dquots gives the number of dquots on the list. When
213 * dquot is invalidated it's completely released from memory.
215 * Dquots with a specific identity (device, type and id) are placed on
216 * one of the dquot_hash[] hash chains. The provides an efficient search
217 * mechanism to locate a specific dquot.
220 static LIST_HEAD(inuse_list
);
221 static LIST_HEAD(free_dquots
);
222 static unsigned int dq_hash_bits
, dq_hash_mask
;
223 static struct hlist_head
*dquot_hash
;
225 struct dqstats dqstats
;
226 EXPORT_SYMBOL(dqstats
);
228 static inline unsigned int
229 hashfn(const struct super_block
*sb
, unsigned int id
, int type
)
233 tmp
= (((unsigned long)sb
>>L1_CACHE_SHIFT
) ^ id
) * (MAXQUOTAS
- type
);
234 return (tmp
+ (tmp
>> dq_hash_bits
)) & dq_hash_mask
;
238 * Following list functions expect dq_list_lock to be held
240 static inline void insert_dquot_hash(struct dquot
*dquot
)
242 struct hlist_head
*head
;
243 head
= dquot_hash
+ hashfn(dquot
->dq_sb
, dquot
->dq_id
, dquot
->dq_type
);
244 hlist_add_head(&dquot
->dq_hash
, head
);
247 static inline void remove_dquot_hash(struct dquot
*dquot
)
249 hlist_del_init(&dquot
->dq_hash
);
252 static struct dquot
*find_dquot(unsigned int hashent
, struct super_block
*sb
,
253 unsigned int id
, int type
)
255 struct hlist_node
*node
;
258 hlist_for_each (node
, dquot_hash
+hashent
) {
259 dquot
= hlist_entry(node
, struct dquot
, dq_hash
);
260 if (dquot
->dq_sb
== sb
&& dquot
->dq_id
== id
&&
261 dquot
->dq_type
== type
)
267 /* Add a dquot to the tail of the free list */
268 static inline void put_dquot_last(struct dquot
*dquot
)
270 list_add_tail(&dquot
->dq_free
, &free_dquots
);
271 dqstats
.free_dquots
++;
274 static inline void remove_free_dquot(struct dquot
*dquot
)
276 if (list_empty(&dquot
->dq_free
))
278 list_del_init(&dquot
->dq_free
);
279 dqstats
.free_dquots
--;
282 static inline void put_inuse(struct dquot
*dquot
)
284 /* We add to the back of inuse list so we don't have to restart
285 * when traversing this list and we block */
286 list_add_tail(&dquot
->dq_inuse
, &inuse_list
);
287 dqstats
.allocated_dquots
++;
290 static inline void remove_inuse(struct dquot
*dquot
)
292 dqstats
.allocated_dquots
--;
293 list_del(&dquot
->dq_inuse
);
296 * End of list functions needing dq_list_lock
299 static void wait_on_dquot(struct dquot
*dquot
)
301 mutex_lock(&dquot
->dq_lock
);
302 mutex_unlock(&dquot
->dq_lock
);
305 static inline int dquot_dirty(struct dquot
*dquot
)
307 return test_bit(DQ_MOD_B
, &dquot
->dq_flags
);
310 static inline int mark_dquot_dirty(struct dquot
*dquot
)
312 return dquot
->dq_sb
->dq_op
->mark_dirty(dquot
);
315 int dquot_mark_dquot_dirty(struct dquot
*dquot
)
317 spin_lock(&dq_list_lock
);
318 if (!test_and_set_bit(DQ_MOD_B
, &dquot
->dq_flags
))
319 list_add(&dquot
->dq_dirty
, &sb_dqopt(dquot
->dq_sb
)->
320 info
[dquot
->dq_type
].dqi_dirty_list
);
321 spin_unlock(&dq_list_lock
);
324 EXPORT_SYMBOL(dquot_mark_dquot_dirty
);
326 /* This function needs dq_list_lock */
327 static inline int clear_dquot_dirty(struct dquot
*dquot
)
329 if (!test_and_clear_bit(DQ_MOD_B
, &dquot
->dq_flags
))
331 list_del_init(&dquot
->dq_dirty
);
335 void mark_info_dirty(struct super_block
*sb
, int type
)
337 set_bit(DQF_INFO_DIRTY_B
, &sb_dqopt(sb
)->info
[type
].dqi_flags
);
339 EXPORT_SYMBOL(mark_info_dirty
);
342 * Read dquot from disk and alloc space for it
345 int dquot_acquire(struct dquot
*dquot
)
347 int ret
= 0, ret2
= 0;
348 struct quota_info
*dqopt
= sb_dqopt(dquot
->dq_sb
);
350 mutex_lock(&dquot
->dq_lock
);
351 mutex_lock(&dqopt
->dqio_mutex
);
352 if (!test_bit(DQ_READ_B
, &dquot
->dq_flags
))
353 ret
= dqopt
->ops
[dquot
->dq_type
]->read_dqblk(dquot
);
356 set_bit(DQ_READ_B
, &dquot
->dq_flags
);
357 /* Instantiate dquot if needed */
358 if (!test_bit(DQ_ACTIVE_B
, &dquot
->dq_flags
) && !dquot
->dq_off
) {
359 ret
= dqopt
->ops
[dquot
->dq_type
]->commit_dqblk(dquot
);
360 /* Write the info if needed */
361 if (info_dirty(&dqopt
->info
[dquot
->dq_type
])) {
362 ret2
= dqopt
->ops
[dquot
->dq_type
]->write_file_info(
363 dquot
->dq_sb
, dquot
->dq_type
);
372 set_bit(DQ_ACTIVE_B
, &dquot
->dq_flags
);
374 mutex_unlock(&dqopt
->dqio_mutex
);
375 mutex_unlock(&dquot
->dq_lock
);
378 EXPORT_SYMBOL(dquot_acquire
);
381 * Write dquot to disk
383 int dquot_commit(struct dquot
*dquot
)
385 int ret
= 0, ret2
= 0;
386 struct quota_info
*dqopt
= sb_dqopt(dquot
->dq_sb
);
388 mutex_lock(&dqopt
->dqio_mutex
);
389 spin_lock(&dq_list_lock
);
390 if (!clear_dquot_dirty(dquot
)) {
391 spin_unlock(&dq_list_lock
);
394 spin_unlock(&dq_list_lock
);
395 /* Inactive dquot can be only if there was error during read/init
396 * => we have better not writing it */
397 if (test_bit(DQ_ACTIVE_B
, &dquot
->dq_flags
)) {
398 ret
= dqopt
->ops
[dquot
->dq_type
]->commit_dqblk(dquot
);
399 if (info_dirty(&dqopt
->info
[dquot
->dq_type
])) {
400 ret2
= dqopt
->ops
[dquot
->dq_type
]->write_file_info(
401 dquot
->dq_sb
, dquot
->dq_type
);
407 mutex_unlock(&dqopt
->dqio_mutex
);
410 EXPORT_SYMBOL(dquot_commit
);
415 int dquot_release(struct dquot
*dquot
)
417 int ret
= 0, ret2
= 0;
418 struct quota_info
*dqopt
= sb_dqopt(dquot
->dq_sb
);
420 mutex_lock(&dquot
->dq_lock
);
421 /* Check whether we are not racing with some other dqget() */
422 if (atomic_read(&dquot
->dq_count
) > 1)
424 mutex_lock(&dqopt
->dqio_mutex
);
425 if (dqopt
->ops
[dquot
->dq_type
]->release_dqblk
) {
426 ret
= dqopt
->ops
[dquot
->dq_type
]->release_dqblk(dquot
);
428 if (info_dirty(&dqopt
->info
[dquot
->dq_type
])) {
429 ret2
= dqopt
->ops
[dquot
->dq_type
]->write_file_info(
430 dquot
->dq_sb
, dquot
->dq_type
);
435 clear_bit(DQ_ACTIVE_B
, &dquot
->dq_flags
);
436 mutex_unlock(&dqopt
->dqio_mutex
);
438 mutex_unlock(&dquot
->dq_lock
);
441 EXPORT_SYMBOL(dquot_release
);
443 void dquot_destroy(struct dquot
*dquot
)
445 kmem_cache_free(dquot_cachep
, dquot
);
447 EXPORT_SYMBOL(dquot_destroy
);
449 static inline void do_destroy_dquot(struct dquot
*dquot
)
451 dquot
->dq_sb
->dq_op
->destroy_dquot(dquot
);
454 /* Invalidate all dquots on the list. Note that this function is called after
455 * quota is disabled and pointers from inodes removed so there cannot be new
456 * quota users. There can still be some users of quotas due to inodes being
457 * just deleted or pruned by prune_icache() (those are not attached to any
458 * list) or parallel quotactl call. We have to wait for such users.
460 static void invalidate_dquots(struct super_block
*sb
, int type
)
462 struct dquot
*dquot
, *tmp
;
465 spin_lock(&dq_list_lock
);
466 list_for_each_entry_safe(dquot
, tmp
, &inuse_list
, dq_inuse
) {
467 if (dquot
->dq_sb
!= sb
)
469 if (dquot
->dq_type
!= type
)
471 /* Wait for dquot users */
472 if (atomic_read(&dquot
->dq_count
)) {
475 atomic_inc(&dquot
->dq_count
);
476 prepare_to_wait(&dquot
->dq_wait_unused
, &wait
,
477 TASK_UNINTERRUPTIBLE
);
478 spin_unlock(&dq_list_lock
);
479 /* Once dqput() wakes us up, we know it's time to free
481 * IMPORTANT: we rely on the fact that there is always
482 * at most one process waiting for dquot to free.
483 * Otherwise dq_count would be > 1 and we would never
486 if (atomic_read(&dquot
->dq_count
) > 1)
488 finish_wait(&dquot
->dq_wait_unused
, &wait
);
490 /* At this moment dquot() need not exist (it could be
491 * reclaimed by prune_dqcache(). Hence we must
496 * Quota now has no users and it has been written on last
499 remove_dquot_hash(dquot
);
500 remove_free_dquot(dquot
);
502 do_destroy_dquot(dquot
);
504 spin_unlock(&dq_list_lock
);
507 /* Call callback for every active dquot on given filesystem */
508 int dquot_scan_active(struct super_block
*sb
,
509 int (*fn
)(struct dquot
*dquot
, unsigned long priv
),
512 struct dquot
*dquot
, *old_dquot
= NULL
;
515 mutex_lock(&sb_dqopt(sb
)->dqonoff_mutex
);
516 spin_lock(&dq_list_lock
);
517 list_for_each_entry(dquot
, &inuse_list
, dq_inuse
) {
518 if (!test_bit(DQ_ACTIVE_B
, &dquot
->dq_flags
))
520 if (dquot
->dq_sb
!= sb
)
522 /* Now we have active dquot so we can just increase use count */
523 atomic_inc(&dquot
->dq_count
);
525 spin_unlock(&dq_list_lock
);
528 ret
= fn(dquot
, priv
);
531 spin_lock(&dq_list_lock
);
532 /* We are safe to continue now because our dquot could not
533 * be moved out of the inuse list while we hold the reference */
535 spin_unlock(&dq_list_lock
);
538 mutex_unlock(&sb_dqopt(sb
)->dqonoff_mutex
);
541 EXPORT_SYMBOL(dquot_scan_active
);
543 int vfs_quota_sync(struct super_block
*sb
, int type
)
545 struct list_head
*dirty
;
547 struct quota_info
*dqopt
= sb_dqopt(sb
);
550 mutex_lock(&dqopt
->dqonoff_mutex
);
551 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++) {
552 if (type
!= -1 && cnt
!= type
)
554 if (!sb_has_quota_active(sb
, cnt
))
556 spin_lock(&dq_list_lock
);
557 dirty
= &dqopt
->info
[cnt
].dqi_dirty_list
;
558 while (!list_empty(dirty
)) {
559 dquot
= list_first_entry(dirty
, struct dquot
,
561 /* Dirty and inactive can be only bad dquot... */
562 if (!test_bit(DQ_ACTIVE_B
, &dquot
->dq_flags
)) {
563 clear_dquot_dirty(dquot
);
566 /* Now we have active dquot from which someone is
567 * holding reference so we can safely just increase
569 atomic_inc(&dquot
->dq_count
);
571 spin_unlock(&dq_list_lock
);
572 sb
->dq_op
->write_dquot(dquot
);
574 spin_lock(&dq_list_lock
);
576 spin_unlock(&dq_list_lock
);
579 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++)
580 if ((cnt
== type
|| type
== -1) && sb_has_quota_active(sb
, cnt
)
581 && info_dirty(&dqopt
->info
[cnt
]))
582 sb
->dq_op
->write_info(sb
, cnt
);
583 spin_lock(&dq_list_lock
);
585 spin_unlock(&dq_list_lock
);
586 mutex_unlock(&dqopt
->dqonoff_mutex
);
590 EXPORT_SYMBOL(vfs_quota_sync
);
592 /* Free unused dquots from cache */
593 static void prune_dqcache(int count
)
595 struct list_head
*head
;
598 head
= free_dquots
.prev
;
599 while (head
!= &free_dquots
&& count
) {
600 dquot
= list_entry(head
, struct dquot
, dq_free
);
601 remove_dquot_hash(dquot
);
602 remove_free_dquot(dquot
);
604 do_destroy_dquot(dquot
);
606 head
= free_dquots
.prev
;
611 * This is called from kswapd when we think we need some
615 static int shrink_dqcache_memory(int nr
, gfp_t gfp_mask
)
618 spin_lock(&dq_list_lock
);
620 spin_unlock(&dq_list_lock
);
622 return (dqstats
.free_dquots
/ 100) * sysctl_vfs_cache_pressure
;
625 static struct shrinker dqcache_shrinker
= {
626 .shrink
= shrink_dqcache_memory
,
627 .seeks
= DEFAULT_SEEKS
,
631 * Put reference to dquot
632 * NOTE: If you change this function please check whether dqput_blocks() works right...
634 void dqput(struct dquot
*dquot
)
640 #ifdef __DQUOT_PARANOIA
641 if (!atomic_read(&dquot
->dq_count
)) {
642 printk("VFS: dqput: trying to free free dquot\n");
643 printk("VFS: device %s, dquot of %s %d\n",
645 quotatypes
[dquot
->dq_type
],
651 spin_lock(&dq_list_lock
);
653 spin_unlock(&dq_list_lock
);
655 spin_lock(&dq_list_lock
);
656 if (atomic_read(&dquot
->dq_count
) > 1) {
657 /* We have more than one user... nothing to do */
658 atomic_dec(&dquot
->dq_count
);
659 /* Releasing dquot during quotaoff phase? */
660 if (!sb_has_quota_active(dquot
->dq_sb
, dquot
->dq_type
) &&
661 atomic_read(&dquot
->dq_count
) == 1)
662 wake_up(&dquot
->dq_wait_unused
);
663 spin_unlock(&dq_list_lock
);
666 /* Need to release dquot? */
667 if (test_bit(DQ_ACTIVE_B
, &dquot
->dq_flags
) && dquot_dirty(dquot
)) {
668 spin_unlock(&dq_list_lock
);
669 /* Commit dquot before releasing */
670 ret
= dquot
->dq_sb
->dq_op
->write_dquot(dquot
);
672 printk(KERN_ERR
"VFS: cannot write quota structure on "
673 "device %s (error %d). Quota may get out of "
674 "sync!\n", dquot
->dq_sb
->s_id
, ret
);
676 * We clear dirty bit anyway, so that we avoid
679 spin_lock(&dq_list_lock
);
680 clear_dquot_dirty(dquot
);
681 spin_unlock(&dq_list_lock
);
685 /* Clear flag in case dquot was inactive (something bad happened) */
686 clear_dquot_dirty(dquot
);
687 if (test_bit(DQ_ACTIVE_B
, &dquot
->dq_flags
)) {
688 spin_unlock(&dq_list_lock
);
689 dquot
->dq_sb
->dq_op
->release_dquot(dquot
);
692 atomic_dec(&dquot
->dq_count
);
693 #ifdef __DQUOT_PARANOIA
695 BUG_ON(!list_empty(&dquot
->dq_free
));
697 put_dquot_last(dquot
);
698 spin_unlock(&dq_list_lock
);
700 EXPORT_SYMBOL(dqput
);
702 struct dquot
*dquot_alloc(struct super_block
*sb
, int type
)
704 return kmem_cache_zalloc(dquot_cachep
, GFP_NOFS
);
706 EXPORT_SYMBOL(dquot_alloc
);
708 static struct dquot
*get_empty_dquot(struct super_block
*sb
, int type
)
712 dquot
= sb
->dq_op
->alloc_dquot(sb
, type
);
716 mutex_init(&dquot
->dq_lock
);
717 INIT_LIST_HEAD(&dquot
->dq_free
);
718 INIT_LIST_HEAD(&dquot
->dq_inuse
);
719 INIT_HLIST_NODE(&dquot
->dq_hash
);
720 INIT_LIST_HEAD(&dquot
->dq_dirty
);
721 init_waitqueue_head(&dquot
->dq_wait_unused
);
723 dquot
->dq_type
= type
;
724 atomic_set(&dquot
->dq_count
, 1);
730 * Get reference to dquot
732 * Locking is slightly tricky here. We are guarded from parallel quotaoff()
733 * destroying our dquot by:
734 * a) checking for quota flags under dq_list_lock and
735 * b) getting a reference to dquot before we release dq_list_lock
737 struct dquot
*dqget(struct super_block
*sb
, unsigned int id
, int type
)
739 unsigned int hashent
= hashfn(sb
, id
, type
);
740 struct dquot
*dquot
= NULL
, *empty
= NULL
;
742 if (!sb_has_quota_active(sb
, type
))
745 spin_lock(&dq_list_lock
);
746 spin_lock(&dq_state_lock
);
747 if (!sb_has_quota_active(sb
, type
)) {
748 spin_unlock(&dq_state_lock
);
749 spin_unlock(&dq_list_lock
);
752 spin_unlock(&dq_state_lock
);
754 dquot
= find_dquot(hashent
, sb
, id
, type
);
757 spin_unlock(&dq_list_lock
);
758 empty
= get_empty_dquot(sb
, type
);
760 schedule(); /* Try to wait for a moment... */
766 /* all dquots go on the inuse_list */
768 /* hash it first so it can be found */
769 insert_dquot_hash(dquot
);
771 spin_unlock(&dq_list_lock
);
773 if (!atomic_read(&dquot
->dq_count
))
774 remove_free_dquot(dquot
);
775 atomic_inc(&dquot
->dq_count
);
776 dqstats
.cache_hits
++;
778 spin_unlock(&dq_list_lock
);
780 /* Wait for dq_lock - after this we know that either dquot_release() is
781 * already finished or it will be canceled due to dq_count > 1 test */
782 wait_on_dquot(dquot
);
783 /* Read the dquot / allocate space in quota file */
784 if (!test_bit(DQ_ACTIVE_B
, &dquot
->dq_flags
) &&
785 sb
->dq_op
->acquire_dquot(dquot
) < 0) {
790 #ifdef __DQUOT_PARANOIA
791 BUG_ON(!dquot
->dq_sb
); /* Has somebody invalidated entry under us? */
795 do_destroy_dquot(empty
);
799 EXPORT_SYMBOL(dqget
);
801 static int dqinit_needed(struct inode
*inode
, int type
)
805 if (IS_NOQUOTA(inode
))
808 return !inode
->i_dquot
[type
];
809 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++)
810 if (!inode
->i_dquot
[cnt
])
815 /* This routine is guarded by dqonoff_mutex mutex */
816 static void add_dquot_ref(struct super_block
*sb
, int type
)
818 struct inode
*inode
, *old_inode
= NULL
;
820 spin_lock(&inode_lock
);
821 list_for_each_entry(inode
, &sb
->s_inodes
, i_sb_list
) {
822 if (inode
->i_state
& (I_FREEING
|I_CLEAR
|I_WILL_FREE
|I_NEW
))
824 if (!atomic_read(&inode
->i_writecount
))
826 if (!dqinit_needed(inode
, type
))
830 spin_unlock(&inode_lock
);
833 sb
->dq_op
->initialize(inode
, type
);
834 /* We hold a reference to 'inode' so it couldn't have been
835 * removed from s_inodes list while we dropped the inode_lock.
836 * We cannot iput the inode now as we can be holding the last
837 * reference and we cannot iput it under inode_lock. So we
838 * keep the reference and iput it later. */
840 spin_lock(&inode_lock
);
842 spin_unlock(&inode_lock
);
847 * Return 0 if dqput() won't block.
848 * (note that 1 doesn't necessarily mean blocking)
850 static inline int dqput_blocks(struct dquot
*dquot
)
852 if (atomic_read(&dquot
->dq_count
) <= 1)
858 * Remove references to dquots from inode and add dquot to list for freeing
859 * if we have the last referece to dquot
860 * We can't race with anybody because we hold dqptr_sem for writing...
862 static int remove_inode_dquot_ref(struct inode
*inode
, int type
,
863 struct list_head
*tofree_head
)
865 struct dquot
*dquot
= inode
->i_dquot
[type
];
867 inode
->i_dquot
[type
] = NULL
;
869 if (dqput_blocks(dquot
)) {
870 #ifdef __DQUOT_PARANOIA
871 if (atomic_read(&dquot
->dq_count
) != 1)
872 printk(KERN_WARNING
"VFS: Adding dquot with dq_count %d to dispose list.\n", atomic_read(&dquot
->dq_count
));
874 spin_lock(&dq_list_lock
);
875 /* As dquot must have currently users it can't be on
876 * the free list... */
877 list_add(&dquot
->dq_free
, tofree_head
);
878 spin_unlock(&dq_list_lock
);
882 dqput(dquot
); /* We have guaranteed we won't block */
888 * Free list of dquots
889 * Dquots are removed from inodes and no new references can be got so we are
890 * the only ones holding reference
892 static void put_dquot_list(struct list_head
*tofree_head
)
894 struct list_head
*act_head
;
897 act_head
= tofree_head
->next
;
898 while (act_head
!= tofree_head
) {
899 dquot
= list_entry(act_head
, struct dquot
, dq_free
);
900 act_head
= act_head
->next
;
901 /* Remove dquot from the list so we won't have problems... */
902 list_del_init(&dquot
->dq_free
);
907 static void remove_dquot_ref(struct super_block
*sb
, int type
,
908 struct list_head
*tofree_head
)
912 spin_lock(&inode_lock
);
913 list_for_each_entry(inode
, &sb
->s_inodes
, i_sb_list
) {
915 * We have to scan also I_NEW inodes because they can already
916 * have quota pointer initialized. Luckily, we need to touch
917 * only quota pointers and these have separate locking
920 if (!IS_NOQUOTA(inode
))
921 remove_inode_dquot_ref(inode
, type
, tofree_head
);
923 spin_unlock(&inode_lock
);
926 /* Gather all references from inodes and drop them */
927 static void drop_dquot_ref(struct super_block
*sb
, int type
)
929 LIST_HEAD(tofree_head
);
932 down_write(&sb_dqopt(sb
)->dqptr_sem
);
933 remove_dquot_ref(sb
, type
, &tofree_head
);
934 up_write(&sb_dqopt(sb
)->dqptr_sem
);
935 put_dquot_list(&tofree_head
);
939 static inline void dquot_incr_inodes(struct dquot
*dquot
, qsize_t number
)
941 dquot
->dq_dqb
.dqb_curinodes
+= number
;
944 static inline void dquot_incr_space(struct dquot
*dquot
, qsize_t number
)
946 dquot
->dq_dqb
.dqb_curspace
+= number
;
949 static inline void dquot_resv_space(struct dquot
*dquot
, qsize_t number
)
951 dquot
->dq_dqb
.dqb_rsvspace
+= number
;
955 * Claim reserved quota space
957 static void dquot_claim_reserved_space(struct dquot
*dquot
,
960 WARN_ON(dquot
->dq_dqb
.dqb_rsvspace
< number
);
961 dquot
->dq_dqb
.dqb_curspace
+= number
;
962 dquot
->dq_dqb
.dqb_rsvspace
-= number
;
966 void dquot_free_reserved_space(struct dquot
*dquot
, qsize_t number
)
968 dquot
->dq_dqb
.dqb_rsvspace
-= number
;
971 static void dquot_decr_inodes(struct dquot
*dquot
, qsize_t number
)
973 if (sb_dqopt(dquot
->dq_sb
)->flags
& DQUOT_NEGATIVE_USAGE
||
974 dquot
->dq_dqb
.dqb_curinodes
>= number
)
975 dquot
->dq_dqb
.dqb_curinodes
-= number
;
977 dquot
->dq_dqb
.dqb_curinodes
= 0;
978 if (dquot
->dq_dqb
.dqb_curinodes
<= dquot
->dq_dqb
.dqb_isoftlimit
)
979 dquot
->dq_dqb
.dqb_itime
= (time_t) 0;
980 clear_bit(DQ_INODES_B
, &dquot
->dq_flags
);
983 static void dquot_decr_space(struct dquot
*dquot
, qsize_t number
)
985 if (sb_dqopt(dquot
->dq_sb
)->flags
& DQUOT_NEGATIVE_USAGE
||
986 dquot
->dq_dqb
.dqb_curspace
>= number
)
987 dquot
->dq_dqb
.dqb_curspace
-= number
;
989 dquot
->dq_dqb
.dqb_curspace
= 0;
990 if (dquot
->dq_dqb
.dqb_curspace
<= dquot
->dq_dqb
.dqb_bsoftlimit
)
991 dquot
->dq_dqb
.dqb_btime
= (time_t) 0;
992 clear_bit(DQ_BLKS_B
, &dquot
->dq_flags
);
995 static int warning_issued(struct dquot
*dquot
, const int warntype
)
997 int flag
= (warntype
== QUOTA_NL_BHARDWARN
||
998 warntype
== QUOTA_NL_BSOFTLONGWARN
) ? DQ_BLKS_B
:
999 ((warntype
== QUOTA_NL_IHARDWARN
||
1000 warntype
== QUOTA_NL_ISOFTLONGWARN
) ? DQ_INODES_B
: 0);
1004 return test_and_set_bit(flag
, &dquot
->dq_flags
);
1007 #ifdef CONFIG_PRINT_QUOTA_WARNING
1008 static int flag_print_warnings
= 1;
1010 static int need_print_warning(struct dquot
*dquot
)
1012 if (!flag_print_warnings
)
1015 switch (dquot
->dq_type
) {
1017 return current_fsuid() == dquot
->dq_id
;
1019 return in_group_p(dquot
->dq_id
);
1024 /* Print warning to user which exceeded quota */
1025 static void print_warning(struct dquot
*dquot
, const int warntype
)
1028 struct tty_struct
*tty
;
1030 if (warntype
== QUOTA_NL_IHARDBELOW
||
1031 warntype
== QUOTA_NL_ISOFTBELOW
||
1032 warntype
== QUOTA_NL_BHARDBELOW
||
1033 warntype
== QUOTA_NL_BSOFTBELOW
|| !need_print_warning(dquot
))
1036 tty
= get_current_tty();
1039 tty_write_message(tty
, dquot
->dq_sb
->s_id
);
1040 if (warntype
== QUOTA_NL_ISOFTWARN
|| warntype
== QUOTA_NL_BSOFTWARN
)
1041 tty_write_message(tty
, ": warning, ");
1043 tty_write_message(tty
, ": write failed, ");
1044 tty_write_message(tty
, quotatypes
[dquot
->dq_type
]);
1046 case QUOTA_NL_IHARDWARN
:
1047 msg
= " file limit reached.\r\n";
1049 case QUOTA_NL_ISOFTLONGWARN
:
1050 msg
= " file quota exceeded too long.\r\n";
1052 case QUOTA_NL_ISOFTWARN
:
1053 msg
= " file quota exceeded.\r\n";
1055 case QUOTA_NL_BHARDWARN
:
1056 msg
= " block limit reached.\r\n";
1058 case QUOTA_NL_BSOFTLONGWARN
:
1059 msg
= " block quota exceeded too long.\r\n";
1061 case QUOTA_NL_BSOFTWARN
:
1062 msg
= " block quota exceeded.\r\n";
1065 tty_write_message(tty
, msg
);
1071 * Write warnings to the console and send warning messages over netlink.
1073 * Note that this function can sleep.
1075 static void flush_warnings(struct dquot
*const *dquots
, char *warntype
)
1080 for (i
= 0; i
< MAXQUOTAS
; i
++) {
1082 if (dq
&& warntype
[i
] != QUOTA_NL_NOWARN
&&
1083 !warning_issued(dq
, warntype
[i
])) {
1084 #ifdef CONFIG_PRINT_QUOTA_WARNING
1085 print_warning(dq
, warntype
[i
]);
1087 quota_send_warning(dq
->dq_type
, dq
->dq_id
,
1088 dq
->dq_sb
->s_dev
, warntype
[i
]);
1093 static int ignore_hardlimit(struct dquot
*dquot
)
1095 struct mem_dqinfo
*info
= &sb_dqopt(dquot
->dq_sb
)->info
[dquot
->dq_type
];
1097 return capable(CAP_SYS_RESOURCE
) &&
1098 (info
->dqi_format
->qf_fmt_id
!= QFMT_VFS_OLD
||
1099 !(info
->dqi_flags
& V1_DQF_RSQUASH
));
1102 /* needs dq_data_lock */
1103 static int check_idq(struct dquot
*dquot
, qsize_t inodes
, char *warntype
)
1105 qsize_t newinodes
= dquot
->dq_dqb
.dqb_curinodes
+ inodes
;
1107 *warntype
= QUOTA_NL_NOWARN
;
1108 if (!sb_has_quota_limits_enabled(dquot
->dq_sb
, dquot
->dq_type
) ||
1109 test_bit(DQ_FAKE_B
, &dquot
->dq_flags
))
1112 if (dquot
->dq_dqb
.dqb_ihardlimit
&&
1113 newinodes
> dquot
->dq_dqb
.dqb_ihardlimit
&&
1114 !ignore_hardlimit(dquot
)) {
1115 *warntype
= QUOTA_NL_IHARDWARN
;
1119 if (dquot
->dq_dqb
.dqb_isoftlimit
&&
1120 newinodes
> dquot
->dq_dqb
.dqb_isoftlimit
&&
1121 dquot
->dq_dqb
.dqb_itime
&&
1122 get_seconds() >= dquot
->dq_dqb
.dqb_itime
&&
1123 !ignore_hardlimit(dquot
)) {
1124 *warntype
= QUOTA_NL_ISOFTLONGWARN
;
1128 if (dquot
->dq_dqb
.dqb_isoftlimit
&&
1129 newinodes
> dquot
->dq_dqb
.dqb_isoftlimit
&&
1130 dquot
->dq_dqb
.dqb_itime
== 0) {
1131 *warntype
= QUOTA_NL_ISOFTWARN
;
1132 dquot
->dq_dqb
.dqb_itime
= get_seconds() +
1133 sb_dqopt(dquot
->dq_sb
)->info
[dquot
->dq_type
].dqi_igrace
;
1139 /* needs dq_data_lock */
1140 static int check_bdq(struct dquot
*dquot
, qsize_t space
, int prealloc
, char *warntype
)
1143 struct super_block
*sb
= dquot
->dq_sb
;
1145 *warntype
= QUOTA_NL_NOWARN
;
1146 if (!sb_has_quota_limits_enabled(sb
, dquot
->dq_type
) ||
1147 test_bit(DQ_FAKE_B
, &dquot
->dq_flags
))
1150 tspace
= dquot
->dq_dqb
.dqb_curspace
+ dquot
->dq_dqb
.dqb_rsvspace
1153 if (dquot
->dq_dqb
.dqb_bhardlimit
&&
1154 tspace
> dquot
->dq_dqb
.dqb_bhardlimit
&&
1155 !ignore_hardlimit(dquot
)) {
1157 *warntype
= QUOTA_NL_BHARDWARN
;
1161 if (dquot
->dq_dqb
.dqb_bsoftlimit
&&
1162 tspace
> dquot
->dq_dqb
.dqb_bsoftlimit
&&
1163 dquot
->dq_dqb
.dqb_btime
&&
1164 get_seconds() >= dquot
->dq_dqb
.dqb_btime
&&
1165 !ignore_hardlimit(dquot
)) {
1167 *warntype
= QUOTA_NL_BSOFTLONGWARN
;
1171 if (dquot
->dq_dqb
.dqb_bsoftlimit
&&
1172 tspace
> dquot
->dq_dqb
.dqb_bsoftlimit
&&
1173 dquot
->dq_dqb
.dqb_btime
== 0) {
1175 *warntype
= QUOTA_NL_BSOFTWARN
;
1176 dquot
->dq_dqb
.dqb_btime
= get_seconds() +
1177 sb_dqopt(sb
)->info
[dquot
->dq_type
].dqi_bgrace
;
1181 * We don't allow preallocation to exceed softlimit so exceeding will
1190 static int info_idq_free(struct dquot
*dquot
, qsize_t inodes
)
1194 if (test_bit(DQ_FAKE_B
, &dquot
->dq_flags
) ||
1195 dquot
->dq_dqb
.dqb_curinodes
<= dquot
->dq_dqb
.dqb_isoftlimit
||
1196 !sb_has_quota_limits_enabled(dquot
->dq_sb
, dquot
->dq_type
))
1197 return QUOTA_NL_NOWARN
;
1199 newinodes
= dquot
->dq_dqb
.dqb_curinodes
- inodes
;
1200 if (newinodes
<= dquot
->dq_dqb
.dqb_isoftlimit
)
1201 return QUOTA_NL_ISOFTBELOW
;
1202 if (dquot
->dq_dqb
.dqb_curinodes
>= dquot
->dq_dqb
.dqb_ihardlimit
&&
1203 newinodes
< dquot
->dq_dqb
.dqb_ihardlimit
)
1204 return QUOTA_NL_IHARDBELOW
;
1205 return QUOTA_NL_NOWARN
;
1208 static int info_bdq_free(struct dquot
*dquot
, qsize_t space
)
1210 if (test_bit(DQ_FAKE_B
, &dquot
->dq_flags
) ||
1211 dquot
->dq_dqb
.dqb_curspace
<= dquot
->dq_dqb
.dqb_bsoftlimit
)
1212 return QUOTA_NL_NOWARN
;
1214 if (dquot
->dq_dqb
.dqb_curspace
- space
<= dquot
->dq_dqb
.dqb_bsoftlimit
)
1215 return QUOTA_NL_BSOFTBELOW
;
1216 if (dquot
->dq_dqb
.dqb_curspace
>= dquot
->dq_dqb
.dqb_bhardlimit
&&
1217 dquot
->dq_dqb
.dqb_curspace
- space
< dquot
->dq_dqb
.dqb_bhardlimit
)
1218 return QUOTA_NL_BHARDBELOW
;
1219 return QUOTA_NL_NOWARN
;
1222 * Initialize quota pointers in inode
1223 * We do things in a bit complicated way but by that we avoid calling
1224 * dqget() and thus filesystem callbacks under dqptr_sem.
1226 int dquot_initialize(struct inode
*inode
, int type
)
1228 unsigned int id
= 0;
1230 struct dquot
*got
[MAXQUOTAS
] = { NULL
, NULL
};
1231 struct super_block
*sb
= inode
->i_sb
;
1233 /* First test before acquiring mutex - solves deadlocks when we
1234 * re-enter the quota code and are already holding the mutex */
1235 if (IS_NOQUOTA(inode
))
1238 /* First get references to structures we might need. */
1239 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++) {
1240 if (type
!= -1 && cnt
!= type
)
1250 got
[cnt
] = dqget(sb
, id
, cnt
);
1253 down_write(&sb_dqopt(sb
)->dqptr_sem
);
1254 /* Having dqptr_sem we know NOQUOTA flags can't be altered... */
1255 if (IS_NOQUOTA(inode
))
1257 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++) {
1258 if (type
!= -1 && cnt
!= type
)
1260 /* Avoid races with quotaoff() */
1261 if (!sb_has_quota_active(sb
, cnt
))
1263 if (!inode
->i_dquot
[cnt
]) {
1264 inode
->i_dquot
[cnt
] = got
[cnt
];
1269 up_write(&sb_dqopt(sb
)->dqptr_sem
);
1270 /* Drop unused references */
1271 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++)
1275 EXPORT_SYMBOL(dquot_initialize
);
1278 * Release all quotas referenced by inode
1280 int dquot_drop(struct inode
*inode
)
1283 struct dquot
*put
[MAXQUOTAS
];
1285 down_write(&sb_dqopt(inode
->i_sb
)->dqptr_sem
);
1286 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++) {
1287 put
[cnt
] = inode
->i_dquot
[cnt
];
1288 inode
->i_dquot
[cnt
] = NULL
;
1290 up_write(&sb_dqopt(inode
->i_sb
)->dqptr_sem
);
1292 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++)
1296 EXPORT_SYMBOL(dquot_drop
);
1298 /* Wrapper to remove references to quota structures from inode */
1299 void vfs_dq_drop(struct inode
*inode
)
1301 /* Here we can get arbitrary inode from clear_inode() so we have
1302 * to be careful. OTOH we don't need locking as quota operations
1303 * are allowed to change only at mount time */
1304 if (!IS_NOQUOTA(inode
) && inode
->i_sb
&& inode
->i_sb
->dq_op
1305 && inode
->i_sb
->dq_op
->drop
) {
1307 /* Test before calling to rule out calls from proc and such
1308 * where we are not allowed to block. Note that this is
1309 * actually reliable test even without the lock - the caller
1310 * must assure that nobody can come after the DQUOT_DROP and
1311 * add quota pointers back anyway */
1312 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++)
1313 if (inode
->i_dquot
[cnt
])
1315 if (cnt
< MAXQUOTAS
)
1316 inode
->i_sb
->dq_op
->drop(inode
);
1319 EXPORT_SYMBOL(vfs_dq_drop
);
1322 * Following four functions update i_blocks+i_bytes fields and
1323 * quota information (together with appropriate checks)
1324 * NOTE: We absolutely rely on the fact that caller dirties
1325 * the inode (usually macros in quotaops.h care about this) and
1326 * holds a handle for the current transaction so that dquot write and
1327 * inode write go into the same transaction.
1331 * This operation can block, but only after everything is updated
1333 int __dquot_alloc_space(struct inode
*inode
, qsize_t number
,
1334 int warn
, int reserve
)
1336 int cnt
, ret
= QUOTA_OK
;
1337 char warntype
[MAXQUOTAS
];
1339 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++)
1340 warntype
[cnt
] = QUOTA_NL_NOWARN
;
1342 spin_lock(&dq_data_lock
);
1343 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++) {
1344 if (!inode
->i_dquot
[cnt
])
1346 if (check_bdq(inode
->i_dquot
[cnt
], number
, warn
, warntype
+cnt
)
1352 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++) {
1353 if (!inode
->i_dquot
[cnt
])
1356 dquot_resv_space(inode
->i_dquot
[cnt
], number
);
1358 dquot_incr_space(inode
->i_dquot
[cnt
], number
);
1361 inode_add_bytes(inode
, number
);
1363 spin_unlock(&dq_data_lock
);
1364 flush_warnings(inode
->i_dquot
, warntype
);
1368 int dquot_alloc_space(struct inode
*inode
, qsize_t number
, int warn
)
1370 int cnt
, ret
= QUOTA_OK
;
1373 * First test before acquiring mutex - solves deadlocks when we
1374 * re-enter the quota code and are already holding the mutex
1376 if (IS_NOQUOTA(inode
)) {
1377 inode_add_bytes(inode
, number
);
1381 down_read(&sb_dqopt(inode
->i_sb
)->dqptr_sem
);
1382 if (IS_NOQUOTA(inode
)) {
1383 inode_add_bytes(inode
, number
);
1387 ret
= __dquot_alloc_space(inode
, number
, warn
, 0);
1388 if (ret
== NO_QUOTA
)
1391 /* Dirtify all the dquots - this can block when journalling */
1392 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++)
1393 if (inode
->i_dquot
[cnt
])
1394 mark_dquot_dirty(inode
->i_dquot
[cnt
]);
1396 up_read(&sb_dqopt(inode
->i_sb
)->dqptr_sem
);
1400 EXPORT_SYMBOL(dquot_alloc_space
);
1402 int dquot_reserve_space(struct inode
*inode
, qsize_t number
, int warn
)
1406 if (IS_NOQUOTA(inode
))
1409 down_read(&sb_dqopt(inode
->i_sb
)->dqptr_sem
);
1410 if (IS_NOQUOTA(inode
))
1413 ret
= __dquot_alloc_space(inode
, number
, warn
, 1);
1415 up_read(&sb_dqopt(inode
->i_sb
)->dqptr_sem
);
1419 EXPORT_SYMBOL(dquot_reserve_space
);
1422 * This operation can block, but only after everything is updated
1424 int dquot_alloc_inode(const struct inode
*inode
, qsize_t number
)
1426 int cnt
, ret
= NO_QUOTA
;
1427 char warntype
[MAXQUOTAS
];
1429 /* First test before acquiring mutex - solves deadlocks when we
1430 * re-enter the quota code and are already holding the mutex */
1431 if (IS_NOQUOTA(inode
))
1433 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++)
1434 warntype
[cnt
] = QUOTA_NL_NOWARN
;
1435 down_read(&sb_dqopt(inode
->i_sb
)->dqptr_sem
);
1436 if (IS_NOQUOTA(inode
)) {
1437 up_read(&sb_dqopt(inode
->i_sb
)->dqptr_sem
);
1440 spin_lock(&dq_data_lock
);
1441 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++) {
1442 if (!inode
->i_dquot
[cnt
])
1444 if (check_idq(inode
->i_dquot
[cnt
], number
, warntype
+cnt
)
1449 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++) {
1450 if (!inode
->i_dquot
[cnt
])
1452 dquot_incr_inodes(inode
->i_dquot
[cnt
], number
);
1456 spin_unlock(&dq_data_lock
);
1457 if (ret
== QUOTA_OK
)
1458 /* Dirtify all the dquots - this can block when journalling */
1459 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++)
1460 if (inode
->i_dquot
[cnt
])
1461 mark_dquot_dirty(inode
->i_dquot
[cnt
]);
1462 flush_warnings(inode
->i_dquot
, warntype
);
1463 up_read(&sb_dqopt(inode
->i_sb
)->dqptr_sem
);
1466 EXPORT_SYMBOL(dquot_alloc_inode
);
1468 int dquot_claim_space(struct inode
*inode
, qsize_t number
)
1473 if (IS_NOQUOTA(inode
)) {
1474 inode_add_bytes(inode
, number
);
1478 down_read(&sb_dqopt(inode
->i_sb
)->dqptr_sem
);
1479 if (IS_NOQUOTA(inode
)) {
1480 up_read(&sb_dqopt(inode
->i_sb
)->dqptr_sem
);
1481 inode_add_bytes(inode
, number
);
1485 spin_lock(&dq_data_lock
);
1486 /* Claim reserved quotas to allocated quotas */
1487 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++) {
1488 if (inode
->i_dquot
[cnt
])
1489 dquot_claim_reserved_space(inode
->i_dquot
[cnt
],
1492 /* Update inode bytes */
1493 inode_add_bytes(inode
, number
);
1494 spin_unlock(&dq_data_lock
);
1495 /* Dirtify all the dquots - this can block when journalling */
1496 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++)
1497 if (inode
->i_dquot
[cnt
])
1498 mark_dquot_dirty(inode
->i_dquot
[cnt
]);
1499 up_read(&sb_dqopt(inode
->i_sb
)->dqptr_sem
);
1503 EXPORT_SYMBOL(dquot_claim_space
);
1506 * Release reserved quota space
1508 void dquot_release_reserved_space(struct inode
*inode
, qsize_t number
)
1512 if (IS_NOQUOTA(inode
))
1515 down_read(&sb_dqopt(inode
->i_sb
)->dqptr_sem
);
1516 if (IS_NOQUOTA(inode
))
1519 spin_lock(&dq_data_lock
);
1520 /* Release reserved dquots */
1521 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++) {
1522 if (inode
->i_dquot
[cnt
])
1523 dquot_free_reserved_space(inode
->i_dquot
[cnt
], number
);
1525 spin_unlock(&dq_data_lock
);
1528 up_read(&sb_dqopt(inode
->i_sb
)->dqptr_sem
);
1532 EXPORT_SYMBOL(dquot_release_reserved_space
);
1535 * This operation can block, but only after everything is updated
1537 int dquot_free_space(struct inode
*inode
, qsize_t number
)
1540 char warntype
[MAXQUOTAS
];
1542 /* First test before acquiring mutex - solves deadlocks when we
1543 * re-enter the quota code and are already holding the mutex */
1544 if (IS_NOQUOTA(inode
)) {
1546 inode_sub_bytes(inode
, number
);
1550 down_read(&sb_dqopt(inode
->i_sb
)->dqptr_sem
);
1551 /* Now recheck reliably when holding dqptr_sem */
1552 if (IS_NOQUOTA(inode
)) {
1553 up_read(&sb_dqopt(inode
->i_sb
)->dqptr_sem
);
1556 spin_lock(&dq_data_lock
);
1557 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++) {
1558 if (!inode
->i_dquot
[cnt
])
1560 warntype
[cnt
] = info_bdq_free(inode
->i_dquot
[cnt
], number
);
1561 dquot_decr_space(inode
->i_dquot
[cnt
], number
);
1563 inode_sub_bytes(inode
, number
);
1564 spin_unlock(&dq_data_lock
);
1565 /* Dirtify all the dquots - this can block when journalling */
1566 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++)
1567 if (inode
->i_dquot
[cnt
])
1568 mark_dquot_dirty(inode
->i_dquot
[cnt
]);
1569 flush_warnings(inode
->i_dquot
, warntype
);
1570 up_read(&sb_dqopt(inode
->i_sb
)->dqptr_sem
);
1573 EXPORT_SYMBOL(dquot_free_space
);
1576 * This operation can block, but only after everything is updated
1578 int dquot_free_inode(const struct inode
*inode
, qsize_t number
)
1581 char warntype
[MAXQUOTAS
];
1583 /* First test before acquiring mutex - solves deadlocks when we
1584 * re-enter the quota code and are already holding the mutex */
1585 if (IS_NOQUOTA(inode
))
1588 down_read(&sb_dqopt(inode
->i_sb
)->dqptr_sem
);
1589 /* Now recheck reliably when holding dqptr_sem */
1590 if (IS_NOQUOTA(inode
)) {
1591 up_read(&sb_dqopt(inode
->i_sb
)->dqptr_sem
);
1594 spin_lock(&dq_data_lock
);
1595 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++) {
1596 if (!inode
->i_dquot
[cnt
])
1598 warntype
[cnt
] = info_idq_free(inode
->i_dquot
[cnt
], number
);
1599 dquot_decr_inodes(inode
->i_dquot
[cnt
], number
);
1601 spin_unlock(&dq_data_lock
);
1602 /* Dirtify all the dquots - this can block when journalling */
1603 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++)
1604 if (inode
->i_dquot
[cnt
])
1605 mark_dquot_dirty(inode
->i_dquot
[cnt
]);
1606 flush_warnings(inode
->i_dquot
, warntype
);
1607 up_read(&sb_dqopt(inode
->i_sb
)->dqptr_sem
);
1610 EXPORT_SYMBOL(dquot_free_inode
);
1613 * call back function, get reserved quota space from underlying fs
1615 qsize_t
dquot_get_reserved_space(struct inode
*inode
)
1617 qsize_t reserved_space
= 0;
1619 if (sb_any_quota_active(inode
->i_sb
) &&
1620 inode
->i_sb
->dq_op
->get_reserved_space
)
1621 reserved_space
= inode
->i_sb
->dq_op
->get_reserved_space(inode
);
1622 return reserved_space
;
1626 * Transfer the number of inode and blocks from one diskquota to an other.
1628 * This operation can block, but only after everything is updated
1629 * A transaction must be started when entering this function.
1631 int dquot_transfer(struct inode
*inode
, struct iattr
*iattr
)
1633 qsize_t space
, cur_space
;
1634 qsize_t rsv_space
= 0;
1635 struct dquot
*transfer_from
[MAXQUOTAS
];
1636 struct dquot
*transfer_to
[MAXQUOTAS
];
1637 int cnt
, ret
= QUOTA_OK
;
1638 int chuid
= iattr
->ia_valid
& ATTR_UID
&& inode
->i_uid
!= iattr
->ia_uid
,
1639 chgid
= iattr
->ia_valid
& ATTR_GID
&& inode
->i_gid
!= iattr
->ia_gid
;
1640 char warntype_to
[MAXQUOTAS
];
1641 char warntype_from_inodes
[MAXQUOTAS
], warntype_from_space
[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
))
1647 /* Initialize the arrays */
1648 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++) {
1649 transfer_from
[cnt
] = NULL
;
1650 transfer_to
[cnt
] = NULL
;
1651 warntype_to
[cnt
] = QUOTA_NL_NOWARN
;
1654 transfer_to
[USRQUOTA
] = dqget(inode
->i_sb
, iattr
->ia_uid
,
1657 transfer_to
[GRPQUOTA
] = dqget(inode
->i_sb
, iattr
->ia_gid
,
1660 down_write(&sb_dqopt(inode
->i_sb
)->dqptr_sem
);
1661 /* Now recheck reliably when holding dqptr_sem */
1662 if (IS_NOQUOTA(inode
)) { /* File without quota accounting? */
1663 up_write(&sb_dqopt(inode
->i_sb
)->dqptr_sem
);
1666 spin_lock(&dq_data_lock
);
1667 cur_space
= inode_get_bytes(inode
);
1668 rsv_space
= dquot_get_reserved_space(inode
);
1669 space
= cur_space
+ rsv_space
;
1670 /* Build the transfer_from list and check the limits */
1671 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++) {
1672 if (!transfer_to
[cnt
])
1674 transfer_from
[cnt
] = inode
->i_dquot
[cnt
];
1675 if (check_idq(transfer_to
[cnt
], 1, warntype_to
+ cnt
) ==
1676 NO_QUOTA
|| check_bdq(transfer_to
[cnt
], space
, 0,
1677 warntype_to
+ cnt
) == NO_QUOTA
)
1682 * Finally perform the needed transfer from transfer_from to transfer_to
1684 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++) {
1686 * Skip changes for same uid or gid or for turned off quota-type.
1688 if (!transfer_to
[cnt
])
1691 /* Due to IO error we might not have transfer_from[] structure */
1692 if (transfer_from
[cnt
]) {
1693 warntype_from_inodes
[cnt
] =
1694 info_idq_free(transfer_from
[cnt
], 1);
1695 warntype_from_space
[cnt
] =
1696 info_bdq_free(transfer_from
[cnt
], space
);
1697 dquot_decr_inodes(transfer_from
[cnt
], 1);
1698 dquot_decr_space(transfer_from
[cnt
], cur_space
);
1699 dquot_free_reserved_space(transfer_from
[cnt
],
1703 dquot_incr_inodes(transfer_to
[cnt
], 1);
1704 dquot_incr_space(transfer_to
[cnt
], cur_space
);
1705 dquot_resv_space(transfer_to
[cnt
], rsv_space
);
1707 inode
->i_dquot
[cnt
] = transfer_to
[cnt
];
1709 spin_unlock(&dq_data_lock
);
1710 up_write(&sb_dqopt(inode
->i_sb
)->dqptr_sem
);
1712 /* Dirtify all the dquots - this can block when journalling */
1713 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++) {
1714 if (transfer_from
[cnt
])
1715 mark_dquot_dirty(transfer_from
[cnt
]);
1716 if (transfer_to
[cnt
]) {
1717 mark_dquot_dirty(transfer_to
[cnt
]);
1718 /* The reference we got is transferred to the inode */
1719 transfer_to
[cnt
] = NULL
;
1723 flush_warnings(transfer_to
, warntype_to
);
1724 flush_warnings(transfer_from
, warntype_from_inodes
);
1725 flush_warnings(transfer_from
, warntype_from_space
);
1727 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++) {
1728 dqput(transfer_from
[cnt
]);
1729 dqput(transfer_to
[cnt
]);
1733 spin_unlock(&dq_data_lock
);
1734 up_write(&sb_dqopt(inode
->i_sb
)->dqptr_sem
);
1735 /* Clear dquot pointers we don't want to dqput() */
1736 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++)
1737 transfer_from
[cnt
] = NULL
;
1741 EXPORT_SYMBOL(dquot_transfer
);
1743 /* Wrapper for transferring ownership of an inode */
1744 int vfs_dq_transfer(struct inode
*inode
, struct iattr
*iattr
)
1746 if (sb_any_quota_active(inode
->i_sb
) && !IS_NOQUOTA(inode
)) {
1748 if (inode
->i_sb
->dq_op
->transfer(inode
, iattr
) == NO_QUOTA
)
1753 EXPORT_SYMBOL(vfs_dq_transfer
);
1756 * Write info of quota file to disk
1758 int dquot_commit_info(struct super_block
*sb
, int type
)
1761 struct quota_info
*dqopt
= sb_dqopt(sb
);
1763 mutex_lock(&dqopt
->dqio_mutex
);
1764 ret
= dqopt
->ops
[type
]->write_file_info(sb
, type
);
1765 mutex_unlock(&dqopt
->dqio_mutex
);
1768 EXPORT_SYMBOL(dquot_commit_info
);
1771 * Definitions of diskquota operations.
1773 const struct dquot_operations dquot_operations
= {
1774 .initialize
= dquot_initialize
,
1776 .alloc_space
= dquot_alloc_space
,
1777 .alloc_inode
= dquot_alloc_inode
,
1778 .free_space
= dquot_free_space
,
1779 .free_inode
= dquot_free_inode
,
1780 .transfer
= dquot_transfer
,
1781 .write_dquot
= dquot_commit
,
1782 .acquire_dquot
= dquot_acquire
,
1783 .release_dquot
= dquot_release
,
1784 .mark_dirty
= dquot_mark_dquot_dirty
,
1785 .write_info
= dquot_commit_info
,
1786 .alloc_dquot
= dquot_alloc
,
1787 .destroy_dquot
= dquot_destroy
,
1791 * Turn quota off on a device. type == -1 ==> quotaoff for all types (umount)
1793 int vfs_quota_disable(struct super_block
*sb
, int type
, unsigned int flags
)
1796 struct quota_info
*dqopt
= sb_dqopt(sb
);
1797 struct inode
*toputinode
[MAXQUOTAS
];
1799 /* Cannot turn off usage accounting without turning off limits, or
1800 * suspend quotas and simultaneously turn quotas off. */
1801 if ((flags
& DQUOT_USAGE_ENABLED
&& !(flags
& DQUOT_LIMITS_ENABLED
))
1802 || (flags
& DQUOT_SUSPENDED
&& flags
& (DQUOT_LIMITS_ENABLED
|
1803 DQUOT_USAGE_ENABLED
)))
1806 /* We need to serialize quota_off() for device */
1807 mutex_lock(&dqopt
->dqonoff_mutex
);
1810 * Skip everything if there's nothing to do. We have to do this because
1811 * sometimes we are called when fill_super() failed and calling
1812 * sync_fs() in such cases does no good.
1814 if (!sb_any_quota_loaded(sb
)) {
1815 mutex_unlock(&dqopt
->dqonoff_mutex
);
1818 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++) {
1819 toputinode
[cnt
] = NULL
;
1820 if (type
!= -1 && cnt
!= type
)
1822 if (!sb_has_quota_loaded(sb
, cnt
))
1825 if (flags
& DQUOT_SUSPENDED
) {
1826 spin_lock(&dq_state_lock
);
1828 dquot_state_flag(DQUOT_SUSPENDED
, cnt
);
1829 spin_unlock(&dq_state_lock
);
1831 spin_lock(&dq_state_lock
);
1832 dqopt
->flags
&= ~dquot_state_flag(flags
, cnt
);
1833 /* Turning off suspended quotas? */
1834 if (!sb_has_quota_loaded(sb
, cnt
) &&
1835 sb_has_quota_suspended(sb
, cnt
)) {
1836 dqopt
->flags
&= ~dquot_state_flag(
1837 DQUOT_SUSPENDED
, cnt
);
1838 spin_unlock(&dq_state_lock
);
1839 iput(dqopt
->files
[cnt
]);
1840 dqopt
->files
[cnt
] = NULL
;
1843 spin_unlock(&dq_state_lock
);
1846 /* We still have to keep quota loaded? */
1847 if (sb_has_quota_loaded(sb
, cnt
) && !(flags
& DQUOT_SUSPENDED
))
1850 /* Note: these are blocking operations */
1851 drop_dquot_ref(sb
, cnt
);
1852 invalidate_dquots(sb
, cnt
);
1854 * Now all dquots should be invalidated, all writes done so we
1855 * should be only users of the info. No locks needed.
1857 if (info_dirty(&dqopt
->info
[cnt
]))
1858 sb
->dq_op
->write_info(sb
, cnt
);
1859 if (dqopt
->ops
[cnt
]->free_file_info
)
1860 dqopt
->ops
[cnt
]->free_file_info(sb
, cnt
);
1861 put_quota_format(dqopt
->info
[cnt
].dqi_format
);
1863 toputinode
[cnt
] = dqopt
->files
[cnt
];
1864 if (!sb_has_quota_loaded(sb
, cnt
))
1865 dqopt
->files
[cnt
] = NULL
;
1866 dqopt
->info
[cnt
].dqi_flags
= 0;
1867 dqopt
->info
[cnt
].dqi_igrace
= 0;
1868 dqopt
->info
[cnt
].dqi_bgrace
= 0;
1869 dqopt
->ops
[cnt
] = NULL
;
1871 mutex_unlock(&dqopt
->dqonoff_mutex
);
1873 /* Skip syncing and setting flags if quota files are hidden */
1874 if (dqopt
->flags
& DQUOT_QUOTA_SYS_FILE
)
1877 /* Sync the superblock so that buffers with quota data are written to
1878 * disk (and so userspace sees correct data afterwards). */
1879 if (sb
->s_op
->sync_fs
)
1880 sb
->s_op
->sync_fs(sb
, 1);
1881 sync_blockdev(sb
->s_bdev
);
1882 /* Now the quota files are just ordinary files and we can set the
1883 * inode flags back. Moreover we discard the pagecache so that
1884 * userspace sees the writes we did bypassing the pagecache. We
1885 * must also discard the blockdev buffers so that we see the
1886 * changes done by userspace on the next quotaon() */
1887 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++)
1888 if (toputinode
[cnt
]) {
1889 mutex_lock(&dqopt
->dqonoff_mutex
);
1890 /* If quota was reenabled in the meantime, we have
1892 if (!sb_has_quota_loaded(sb
, cnt
)) {
1893 mutex_lock_nested(&toputinode
[cnt
]->i_mutex
,
1895 toputinode
[cnt
]->i_flags
&= ~(S_IMMUTABLE
|
1896 S_NOATIME
| S_NOQUOTA
);
1897 truncate_inode_pages(&toputinode
[cnt
]->i_data
,
1899 mutex_unlock(&toputinode
[cnt
]->i_mutex
);
1900 mark_inode_dirty(toputinode
[cnt
]);
1902 mutex_unlock(&dqopt
->dqonoff_mutex
);
1905 invalidate_bdev(sb
->s_bdev
);
1907 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++)
1908 if (toputinode
[cnt
]) {
1909 /* On remount RO, we keep the inode pointer so that we
1910 * can reenable quota on the subsequent remount RW. We
1911 * have to check 'flags' variable and not use sb_has_
1912 * function because another quotaon / quotaoff could
1913 * change global state before we got here. We refuse
1914 * to suspend quotas when there is pending delete on
1915 * the quota file... */
1916 if (!(flags
& DQUOT_SUSPENDED
))
1917 iput(toputinode
[cnt
]);
1918 else if (!toputinode
[cnt
]->i_nlink
)
1923 EXPORT_SYMBOL(vfs_quota_disable
);
1925 int vfs_quota_off(struct super_block
*sb
, int type
, int remount
)
1927 return vfs_quota_disable(sb
, type
, remount
? DQUOT_SUSPENDED
:
1928 (DQUOT_USAGE_ENABLED
| DQUOT_LIMITS_ENABLED
));
1930 EXPORT_SYMBOL(vfs_quota_off
);
1932 * Turn quotas on on a device
1936 * Helper function to turn quotas on when we already have the inode of
1937 * quota file and no quota information is loaded.
1939 static int vfs_load_quota_inode(struct inode
*inode
, int type
, int format_id
,
1942 struct quota_format_type
*fmt
= find_quota_format(format_id
);
1943 struct super_block
*sb
= inode
->i_sb
;
1944 struct quota_info
*dqopt
= sb_dqopt(sb
);
1950 if (!S_ISREG(inode
->i_mode
)) {
1954 if (IS_RDONLY(inode
)) {
1958 if (!sb
->s_op
->quota_write
|| !sb
->s_op
->quota_read
) {
1962 /* Usage always has to be set... */
1963 if (!(flags
& DQUOT_USAGE_ENABLED
)) {
1968 if (!(dqopt
->flags
& DQUOT_QUOTA_SYS_FILE
)) {
1969 /* As we bypass the pagecache we must now flush the inode so
1970 * that we see all the changes from userspace... */
1971 write_inode_now(inode
, 1);
1972 /* And now flush the block cache so that kernel sees the
1974 invalidate_bdev(sb
->s_bdev
);
1976 mutex_lock(&dqopt
->dqonoff_mutex
);
1977 if (sb_has_quota_loaded(sb
, type
)) {
1982 if (!(dqopt
->flags
& DQUOT_QUOTA_SYS_FILE
)) {
1983 /* We don't want quota and atime on quota files (deadlocks
1984 * possible) Also nobody should write to the file - we use
1985 * special IO operations which ignore the immutable bit. */
1986 down_write(&dqopt
->dqptr_sem
);
1987 mutex_lock_nested(&inode
->i_mutex
, I_MUTEX_QUOTA
);
1988 oldflags
= inode
->i_flags
& (S_NOATIME
| S_IMMUTABLE
|
1990 inode
->i_flags
|= S_NOQUOTA
| S_NOATIME
| S_IMMUTABLE
;
1991 mutex_unlock(&inode
->i_mutex
);
1992 up_write(&dqopt
->dqptr_sem
);
1993 sb
->dq_op
->drop(inode
);
1997 dqopt
->files
[type
] = igrab(inode
);
1998 if (!dqopt
->files
[type
])
2001 if (!fmt
->qf_ops
->check_quota_file(sb
, type
))
2004 dqopt
->ops
[type
] = fmt
->qf_ops
;
2005 dqopt
->info
[type
].dqi_format
= fmt
;
2006 dqopt
->info
[type
].dqi_fmt_id
= format_id
;
2007 INIT_LIST_HEAD(&dqopt
->info
[type
].dqi_dirty_list
);
2008 mutex_lock(&dqopt
->dqio_mutex
);
2009 error
= dqopt
->ops
[type
]->read_file_info(sb
, type
);
2011 mutex_unlock(&dqopt
->dqio_mutex
);
2014 mutex_unlock(&dqopt
->dqio_mutex
);
2015 spin_lock(&dq_state_lock
);
2016 dqopt
->flags
|= dquot_state_flag(flags
, type
);
2017 spin_unlock(&dq_state_lock
);
2019 add_dquot_ref(sb
, type
);
2020 mutex_unlock(&dqopt
->dqonoff_mutex
);
2025 dqopt
->files
[type
] = NULL
;
2028 if (oldflags
!= -1) {
2029 down_write(&dqopt
->dqptr_sem
);
2030 mutex_lock_nested(&inode
->i_mutex
, I_MUTEX_QUOTA
);
2031 /* Set the flags back (in the case of accidental quotaon()
2032 * on a wrong file we don't want to mess up the flags) */
2033 inode
->i_flags
&= ~(S_NOATIME
| S_NOQUOTA
| S_IMMUTABLE
);
2034 inode
->i_flags
|= oldflags
;
2035 mutex_unlock(&inode
->i_mutex
);
2036 up_write(&dqopt
->dqptr_sem
);
2038 mutex_unlock(&dqopt
->dqonoff_mutex
);
2040 put_quota_format(fmt
);
2045 /* Reenable quotas on remount RW */
2046 static int vfs_quota_on_remount(struct super_block
*sb
, int type
)
2048 struct quota_info
*dqopt
= sb_dqopt(sb
);
2049 struct inode
*inode
;
2053 mutex_lock(&dqopt
->dqonoff_mutex
);
2054 if (!sb_has_quota_suspended(sb
, type
)) {
2055 mutex_unlock(&dqopt
->dqonoff_mutex
);
2058 inode
= dqopt
->files
[type
];
2059 dqopt
->files
[type
] = NULL
;
2060 spin_lock(&dq_state_lock
);
2061 flags
= dqopt
->flags
& dquot_state_flag(DQUOT_USAGE_ENABLED
|
2062 DQUOT_LIMITS_ENABLED
, type
);
2063 dqopt
->flags
&= ~dquot_state_flag(DQUOT_STATE_FLAGS
, type
);
2064 spin_unlock(&dq_state_lock
);
2065 mutex_unlock(&dqopt
->dqonoff_mutex
);
2067 flags
= dquot_generic_flag(flags
, type
);
2068 ret
= vfs_load_quota_inode(inode
, type
, dqopt
->info
[type
].dqi_fmt_id
,
2075 int vfs_quota_on_path(struct super_block
*sb
, int type
, int format_id
,
2078 int error
= security_quota_on(path
->dentry
);
2081 /* Quota file not on the same filesystem? */
2082 if (path
->mnt
->mnt_sb
!= sb
)
2085 error
= vfs_load_quota_inode(path
->dentry
->d_inode
, type
,
2086 format_id
, DQUOT_USAGE_ENABLED
|
2087 DQUOT_LIMITS_ENABLED
);
2090 EXPORT_SYMBOL(vfs_quota_on_path
);
2092 int vfs_quota_on(struct super_block
*sb
, int type
, int format_id
, char *name
,
2099 return vfs_quota_on_remount(sb
, type
);
2101 error
= kern_path(name
, LOOKUP_FOLLOW
, &path
);
2103 error
= vfs_quota_on_path(sb
, type
, format_id
, &path
);
2108 EXPORT_SYMBOL(vfs_quota_on
);
2111 * More powerful function for turning on quotas allowing setting
2112 * of individual quota flags
2114 int vfs_quota_enable(struct inode
*inode
, int type
, int format_id
,
2118 struct super_block
*sb
= inode
->i_sb
;
2119 struct quota_info
*dqopt
= sb_dqopt(sb
);
2121 /* Just unsuspend quotas? */
2122 if (flags
& DQUOT_SUSPENDED
)
2123 return vfs_quota_on_remount(sb
, type
);
2126 /* Just updating flags needed? */
2127 if (sb_has_quota_loaded(sb
, type
)) {
2128 mutex_lock(&dqopt
->dqonoff_mutex
);
2129 /* Now do a reliable test... */
2130 if (!sb_has_quota_loaded(sb
, type
)) {
2131 mutex_unlock(&dqopt
->dqonoff_mutex
);
2134 if (flags
& DQUOT_USAGE_ENABLED
&&
2135 sb_has_quota_usage_enabled(sb
, type
)) {
2139 if (flags
& DQUOT_LIMITS_ENABLED
&&
2140 sb_has_quota_limits_enabled(sb
, type
)) {
2144 spin_lock(&dq_state_lock
);
2145 sb_dqopt(sb
)->flags
|= dquot_state_flag(flags
, type
);
2146 spin_unlock(&dq_state_lock
);
2148 mutex_unlock(&dqopt
->dqonoff_mutex
);
2153 return vfs_load_quota_inode(inode
, type
, format_id
, flags
);
2155 EXPORT_SYMBOL(vfs_quota_enable
);
2158 * This function is used when filesystem needs to initialize quotas
2159 * during mount time.
2161 int vfs_quota_on_mount(struct super_block
*sb
, char *qf_name
,
2162 int format_id
, int type
)
2164 struct dentry
*dentry
;
2167 mutex_lock(&sb
->s_root
->d_inode
->i_mutex
);
2168 dentry
= lookup_one_len(qf_name
, sb
->s_root
, strlen(qf_name
));
2169 mutex_unlock(&sb
->s_root
->d_inode
->i_mutex
);
2171 return PTR_ERR(dentry
);
2173 if (!dentry
->d_inode
) {
2178 error
= security_quota_on(dentry
);
2180 error
= vfs_load_quota_inode(dentry
->d_inode
, type
, format_id
,
2181 DQUOT_USAGE_ENABLED
| DQUOT_LIMITS_ENABLED
);
2187 EXPORT_SYMBOL(vfs_quota_on_mount
);
2189 /* Wrapper to turn on quotas when remounting rw */
2190 int vfs_dq_quota_on_remount(struct super_block
*sb
)
2195 if (!sb
->s_qcop
|| !sb
->s_qcop
->quota_on
)
2197 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++) {
2198 err
= sb
->s_qcop
->quota_on(sb
, cnt
, 0, NULL
, 1);
2199 if (err
< 0 && !ret
)
2204 EXPORT_SYMBOL(vfs_dq_quota_on_remount
);
2206 static inline qsize_t
qbtos(qsize_t blocks
)
2208 return blocks
<< QIF_DQBLKSIZE_BITS
;
2211 static inline qsize_t
stoqb(qsize_t space
)
2213 return (space
+ QIF_DQBLKSIZE
- 1) >> QIF_DQBLKSIZE_BITS
;
2216 /* Generic routine for getting common part of quota structure */
2217 static void do_get_dqblk(struct dquot
*dquot
, struct if_dqblk
*di
)
2219 struct mem_dqblk
*dm
= &dquot
->dq_dqb
;
2221 spin_lock(&dq_data_lock
);
2222 di
->dqb_bhardlimit
= stoqb(dm
->dqb_bhardlimit
);
2223 di
->dqb_bsoftlimit
= stoqb(dm
->dqb_bsoftlimit
);
2224 di
->dqb_curspace
= dm
->dqb_curspace
+ dm
->dqb_rsvspace
;
2225 di
->dqb_ihardlimit
= dm
->dqb_ihardlimit
;
2226 di
->dqb_isoftlimit
= dm
->dqb_isoftlimit
;
2227 di
->dqb_curinodes
= dm
->dqb_curinodes
;
2228 di
->dqb_btime
= dm
->dqb_btime
;
2229 di
->dqb_itime
= dm
->dqb_itime
;
2230 di
->dqb_valid
= QIF_ALL
;
2231 spin_unlock(&dq_data_lock
);
2234 int vfs_get_dqblk(struct super_block
*sb
, int type
, qid_t id
,
2235 struct if_dqblk
*di
)
2237 struct dquot
*dquot
;
2239 dquot
= dqget(sb
, id
, type
);
2242 do_get_dqblk(dquot
, di
);
2247 EXPORT_SYMBOL(vfs_get_dqblk
);
2249 /* Generic routine for setting common part of quota structure */
2250 static int do_set_dqblk(struct dquot
*dquot
, struct if_dqblk
*di
)
2252 struct mem_dqblk
*dm
= &dquot
->dq_dqb
;
2253 int check_blim
= 0, check_ilim
= 0;
2254 struct mem_dqinfo
*dqi
= &sb_dqopt(dquot
->dq_sb
)->info
[dquot
->dq_type
];
2256 if ((di
->dqb_valid
& QIF_BLIMITS
&&
2257 (di
->dqb_bhardlimit
> dqi
->dqi_maxblimit
||
2258 di
->dqb_bsoftlimit
> dqi
->dqi_maxblimit
)) ||
2259 (di
->dqb_valid
& QIF_ILIMITS
&&
2260 (di
->dqb_ihardlimit
> dqi
->dqi_maxilimit
||
2261 di
->dqb_isoftlimit
> dqi
->dqi_maxilimit
)))
2264 spin_lock(&dq_data_lock
);
2265 if (di
->dqb_valid
& QIF_SPACE
) {
2266 dm
->dqb_curspace
= di
->dqb_curspace
- dm
->dqb_rsvspace
;
2268 __set_bit(DQ_LASTSET_B
+ QIF_SPACE_B
, &dquot
->dq_flags
);
2270 if (di
->dqb_valid
& QIF_BLIMITS
) {
2271 dm
->dqb_bsoftlimit
= qbtos(di
->dqb_bsoftlimit
);
2272 dm
->dqb_bhardlimit
= qbtos(di
->dqb_bhardlimit
);
2274 __set_bit(DQ_LASTSET_B
+ QIF_BLIMITS_B
, &dquot
->dq_flags
);
2276 if (di
->dqb_valid
& QIF_INODES
) {
2277 dm
->dqb_curinodes
= di
->dqb_curinodes
;
2279 __set_bit(DQ_LASTSET_B
+ QIF_INODES_B
, &dquot
->dq_flags
);
2281 if (di
->dqb_valid
& QIF_ILIMITS
) {
2282 dm
->dqb_isoftlimit
= di
->dqb_isoftlimit
;
2283 dm
->dqb_ihardlimit
= di
->dqb_ihardlimit
;
2285 __set_bit(DQ_LASTSET_B
+ QIF_ILIMITS_B
, &dquot
->dq_flags
);
2287 if (di
->dqb_valid
& QIF_BTIME
) {
2288 dm
->dqb_btime
= di
->dqb_btime
;
2290 __set_bit(DQ_LASTSET_B
+ QIF_BTIME_B
, &dquot
->dq_flags
);
2292 if (di
->dqb_valid
& QIF_ITIME
) {
2293 dm
->dqb_itime
= di
->dqb_itime
;
2295 __set_bit(DQ_LASTSET_B
+ QIF_ITIME_B
, &dquot
->dq_flags
);
2299 if (!dm
->dqb_bsoftlimit
||
2300 dm
->dqb_curspace
< dm
->dqb_bsoftlimit
) {
2302 clear_bit(DQ_BLKS_B
, &dquot
->dq_flags
);
2303 } else if (!(di
->dqb_valid
& QIF_BTIME
))
2304 /* Set grace only if user hasn't provided his own... */
2305 dm
->dqb_btime
= get_seconds() + dqi
->dqi_bgrace
;
2308 if (!dm
->dqb_isoftlimit
||
2309 dm
->dqb_curinodes
< dm
->dqb_isoftlimit
) {
2311 clear_bit(DQ_INODES_B
, &dquot
->dq_flags
);
2312 } else if (!(di
->dqb_valid
& QIF_ITIME
))
2313 /* Set grace only if user hasn't provided his own... */
2314 dm
->dqb_itime
= get_seconds() + dqi
->dqi_igrace
;
2316 if (dm
->dqb_bhardlimit
|| dm
->dqb_bsoftlimit
|| dm
->dqb_ihardlimit
||
2318 clear_bit(DQ_FAKE_B
, &dquot
->dq_flags
);
2320 set_bit(DQ_FAKE_B
, &dquot
->dq_flags
);
2321 spin_unlock(&dq_data_lock
);
2322 mark_dquot_dirty(dquot
);
2327 int vfs_set_dqblk(struct super_block
*sb
, int type
, qid_t id
,
2328 struct if_dqblk
*di
)
2330 struct dquot
*dquot
;
2333 dquot
= dqget(sb
, id
, type
);
2338 rc
= do_set_dqblk(dquot
, di
);
2343 EXPORT_SYMBOL(vfs_set_dqblk
);
2345 /* Generic routine for getting common part of quota file information */
2346 int vfs_get_dqinfo(struct super_block
*sb
, int type
, struct if_dqinfo
*ii
)
2348 struct mem_dqinfo
*mi
;
2350 mutex_lock(&sb_dqopt(sb
)->dqonoff_mutex
);
2351 if (!sb_has_quota_active(sb
, type
)) {
2352 mutex_unlock(&sb_dqopt(sb
)->dqonoff_mutex
);
2355 mi
= sb_dqopt(sb
)->info
+ type
;
2356 spin_lock(&dq_data_lock
);
2357 ii
->dqi_bgrace
= mi
->dqi_bgrace
;
2358 ii
->dqi_igrace
= mi
->dqi_igrace
;
2359 ii
->dqi_flags
= mi
->dqi_flags
& DQF_MASK
;
2360 ii
->dqi_valid
= IIF_ALL
;
2361 spin_unlock(&dq_data_lock
);
2362 mutex_unlock(&sb_dqopt(sb
)->dqonoff_mutex
);
2365 EXPORT_SYMBOL(vfs_get_dqinfo
);
2367 /* Generic routine for setting common part of quota file information */
2368 int vfs_set_dqinfo(struct super_block
*sb
, int type
, struct if_dqinfo
*ii
)
2370 struct mem_dqinfo
*mi
;
2373 mutex_lock(&sb_dqopt(sb
)->dqonoff_mutex
);
2374 if (!sb_has_quota_active(sb
, type
)) {
2378 mi
= sb_dqopt(sb
)->info
+ type
;
2379 spin_lock(&dq_data_lock
);
2380 if (ii
->dqi_valid
& IIF_BGRACE
)
2381 mi
->dqi_bgrace
= ii
->dqi_bgrace
;
2382 if (ii
->dqi_valid
& IIF_IGRACE
)
2383 mi
->dqi_igrace
= ii
->dqi_igrace
;
2384 if (ii
->dqi_valid
& IIF_FLAGS
)
2385 mi
->dqi_flags
= (mi
->dqi_flags
& ~DQF_MASK
) |
2386 (ii
->dqi_flags
& DQF_MASK
);
2387 spin_unlock(&dq_data_lock
);
2388 mark_info_dirty(sb
, type
);
2389 /* Force write to disk */
2390 sb
->dq_op
->write_info(sb
, type
);
2392 mutex_unlock(&sb_dqopt(sb
)->dqonoff_mutex
);
2395 EXPORT_SYMBOL(vfs_set_dqinfo
);
2397 const struct quotactl_ops vfs_quotactl_ops
= {
2398 .quota_on
= vfs_quota_on
,
2399 .quota_off
= vfs_quota_off
,
2400 .quota_sync
= vfs_quota_sync
,
2401 .get_info
= vfs_get_dqinfo
,
2402 .set_info
= vfs_set_dqinfo
,
2403 .get_dqblk
= vfs_get_dqblk
,
2404 .set_dqblk
= vfs_set_dqblk
2407 static ctl_table fs_dqstats_table
[] = {
2409 .procname
= "lookups",
2410 .data
= &dqstats
.lookups
,
2411 .maxlen
= sizeof(int),
2413 .proc_handler
= proc_dointvec
,
2416 .procname
= "drops",
2417 .data
= &dqstats
.drops
,
2418 .maxlen
= sizeof(int),
2420 .proc_handler
= proc_dointvec
,
2423 .procname
= "reads",
2424 .data
= &dqstats
.reads
,
2425 .maxlen
= sizeof(int),
2427 .proc_handler
= proc_dointvec
,
2430 .procname
= "writes",
2431 .data
= &dqstats
.writes
,
2432 .maxlen
= sizeof(int),
2434 .proc_handler
= proc_dointvec
,
2437 .procname
= "cache_hits",
2438 .data
= &dqstats
.cache_hits
,
2439 .maxlen
= sizeof(int),
2441 .proc_handler
= proc_dointvec
,
2444 .procname
= "allocated_dquots",
2445 .data
= &dqstats
.allocated_dquots
,
2446 .maxlen
= sizeof(int),
2448 .proc_handler
= proc_dointvec
,
2451 .procname
= "free_dquots",
2452 .data
= &dqstats
.free_dquots
,
2453 .maxlen
= sizeof(int),
2455 .proc_handler
= proc_dointvec
,
2458 .procname
= "syncs",
2459 .data
= &dqstats
.syncs
,
2460 .maxlen
= sizeof(int),
2462 .proc_handler
= proc_dointvec
,
2464 #ifdef CONFIG_PRINT_QUOTA_WARNING
2466 .procname
= "warnings",
2467 .data
= &flag_print_warnings
,
2468 .maxlen
= sizeof(int),
2470 .proc_handler
= proc_dointvec
,
2476 static ctl_table fs_table
[] = {
2478 .procname
= "quota",
2480 .child
= fs_dqstats_table
,
2485 static ctl_table sys_table
[] = {
2494 static int __init
dquot_init(void)
2497 unsigned long nr_hash
, order
;
2499 printk(KERN_NOTICE
"VFS: Disk quotas %s\n", __DQUOT_VERSION__
);
2501 register_sysctl_table(sys_table
);
2503 dquot_cachep
= kmem_cache_create("dquot",
2504 sizeof(struct dquot
), sizeof(unsigned long) * 4,
2505 (SLAB_HWCACHE_ALIGN
|SLAB_RECLAIM_ACCOUNT
|
2506 SLAB_MEM_SPREAD
|SLAB_PANIC
),
2510 dquot_hash
= (struct hlist_head
*)__get_free_pages(GFP_ATOMIC
, order
);
2512 panic("Cannot create dquot hash table");
2514 /* Find power-of-two hlist_heads which can fit into allocation */
2515 nr_hash
= (1UL << order
) * PAGE_SIZE
/ sizeof(struct hlist_head
);
2519 } while (nr_hash
>> dq_hash_bits
);
2522 nr_hash
= 1UL << dq_hash_bits
;
2523 dq_hash_mask
= nr_hash
- 1;
2524 for (i
= 0; i
< nr_hash
; i
++)
2525 INIT_HLIST_HEAD(dquot_hash
+ i
);
2527 printk("Dquot-cache hash table entries: %ld (order %ld, %ld bytes)\n",
2528 nr_hash
, order
, (PAGE_SIZE
<< order
));
2530 register_shrinker(&dqcache_shrinker
);
2534 module_init(dquot_init
);