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.. */
80 #ifdef CONFIG_QUOTA_NETLINK_INTERFACE
81 #include <net/netlink.h>
82 #include <net/genetlink.h>
85 #include <asm/uaccess.h>
87 #define __DQUOT_PARANOIA
90 * There are three quota SMP locks. dq_list_lock protects all lists with quotas
91 * and quota formats, dqstats structure containing statistics about the lists
92 * dq_data_lock protects data from dq_dqb and also mem_dqinfo structures and
93 * also guards consistency of dquot->dq_dqb with inode->i_blocks, i_bytes.
94 * i_blocks and i_bytes updates itself are guarded by i_lock acquired directly
95 * in inode_add_bytes() and inode_sub_bytes(). dq_state_lock protects
96 * modifications of quota state (on quotaon and quotaoff) and readers who care
97 * about latest values take it as well.
99 * The spinlock ordering is hence: dq_data_lock > dq_list_lock > i_lock,
100 * dq_list_lock > dq_state_lock
102 * Note that some things (eg. sb pointer, type, id) doesn't change during
103 * the life of the dquot structure and so needn't to be protected by a lock
105 * Any operation working on dquots via inode pointers must hold dqptr_sem. If
106 * operation is just reading pointers from inode (or not using them at all) the
107 * read lock is enough. If pointers are altered function must hold write lock
108 * (these locking rules also apply for S_NOQUOTA flag in the inode - note that
109 * for altering the flag i_mutex is also needed).
111 * Each dquot has its dq_lock mutex. Locked dquots might not be referenced
112 * from inodes (dquot_alloc_space() and such don't check the dq_lock).
113 * Currently dquot is locked only when it is being read to memory (or space for
114 * it is being allocated) on the first dqget() and when it is being released on
115 * the last dqput(). The allocation and release oparations are serialized by
116 * the dq_lock and by checking the use count in dquot_release(). Write
117 * operations on dquots don't hold dq_lock as they copy data under dq_data_lock
118 * spinlock to internal buffers before writing.
120 * Lock ordering (including related VFS locks) is the following:
121 * i_mutex > dqonoff_sem > journal_lock > dqptr_sem > dquot->dq_lock >
123 * The lock ordering of dqptr_sem imposed by quota code is only dqonoff_sem >
124 * dqptr_sem. But filesystem has to count with the fact that functions such as
125 * dquot_alloc_space() acquire dqptr_sem and they usually have to be called
126 * from inside a transaction to keep filesystem consistency after a crash. Also
127 * filesystems usually want to do some IO on dquot from ->mark_dirty which is
128 * called with dqptr_sem held.
129 * i_mutex on quota files is special (it's below dqio_mutex)
132 static __cacheline_aligned_in_smp
DEFINE_SPINLOCK(dq_list_lock
);
133 static __cacheline_aligned_in_smp
DEFINE_SPINLOCK(dq_state_lock
);
134 __cacheline_aligned_in_smp
DEFINE_SPINLOCK(dq_data_lock
);
135 EXPORT_SYMBOL(dq_data_lock
);
137 static char *quotatypes
[] = INITQFNAMES
;
138 static struct quota_format_type
*quota_formats
; /* List of registered formats */
139 static struct quota_module_name module_names
[] = INIT_QUOTA_MODULE_NAMES
;
141 /* SLAB cache for dquot structures */
142 static struct kmem_cache
*dquot_cachep
;
144 int register_quota_format(struct quota_format_type
*fmt
)
146 spin_lock(&dq_list_lock
);
147 fmt
->qf_next
= quota_formats
;
149 spin_unlock(&dq_list_lock
);
152 EXPORT_SYMBOL(register_quota_format
);
154 void unregister_quota_format(struct quota_format_type
*fmt
)
156 struct quota_format_type
**actqf
;
158 spin_lock(&dq_list_lock
);
159 for (actqf
= "a_formats
; *actqf
&& *actqf
!= fmt
;
160 actqf
= &(*actqf
)->qf_next
)
163 *actqf
= (*actqf
)->qf_next
;
164 spin_unlock(&dq_list_lock
);
166 EXPORT_SYMBOL(unregister_quota_format
);
168 static struct quota_format_type
*find_quota_format(int id
)
170 struct quota_format_type
*actqf
;
172 spin_lock(&dq_list_lock
);
173 for (actqf
= quota_formats
; actqf
&& actqf
->qf_fmt_id
!= id
;
174 actqf
= actqf
->qf_next
)
176 if (!actqf
|| !try_module_get(actqf
->qf_owner
)) {
179 spin_unlock(&dq_list_lock
);
181 for (qm
= 0; module_names
[qm
].qm_fmt_id
&&
182 module_names
[qm
].qm_fmt_id
!= id
; qm
++)
184 if (!module_names
[qm
].qm_fmt_id
||
185 request_module(module_names
[qm
].qm_mod_name
))
188 spin_lock(&dq_list_lock
);
189 for (actqf
= quota_formats
; actqf
&& actqf
->qf_fmt_id
!= id
;
190 actqf
= actqf
->qf_next
)
192 if (actqf
&& !try_module_get(actqf
->qf_owner
))
195 spin_unlock(&dq_list_lock
);
199 static void put_quota_format(struct quota_format_type
*fmt
)
201 module_put(fmt
->qf_owner
);
205 * Dquot List Management:
206 * The quota code uses three lists for dquot management: the inuse_list,
207 * free_dquots, and dquot_hash[] array. A single dquot structure may be
208 * on all three lists, depending on its current state.
210 * All dquots are placed to the end of inuse_list when first created, and this
211 * list is used for invalidate operation, which must look at every dquot.
213 * Unused dquots (dq_count == 0) are added to the free_dquots list when freed,
214 * and this list is searched whenever we need an available dquot. Dquots are
215 * removed from the list as soon as they are used again, and
216 * dqstats.free_dquots gives the number of dquots on the list. When
217 * dquot is invalidated it's completely released from memory.
219 * Dquots with a specific identity (device, type and id) are placed on
220 * one of the dquot_hash[] hash chains. The provides an efficient search
221 * mechanism to locate a specific dquot.
224 static LIST_HEAD(inuse_list
);
225 static LIST_HEAD(free_dquots
);
226 static unsigned int dq_hash_bits
, dq_hash_mask
;
227 static struct hlist_head
*dquot_hash
;
229 struct dqstats dqstats
;
230 EXPORT_SYMBOL(dqstats
);
232 static inline unsigned int
233 hashfn(const struct super_block
*sb
, unsigned int id
, int type
)
237 tmp
= (((unsigned long)sb
>>L1_CACHE_SHIFT
) ^ id
) * (MAXQUOTAS
- type
);
238 return (tmp
+ (tmp
>> dq_hash_bits
)) & dq_hash_mask
;
242 * Following list functions expect dq_list_lock to be held
244 static inline void insert_dquot_hash(struct dquot
*dquot
)
246 struct hlist_head
*head
;
247 head
= dquot_hash
+ hashfn(dquot
->dq_sb
, dquot
->dq_id
, dquot
->dq_type
);
248 hlist_add_head(&dquot
->dq_hash
, head
);
251 static inline void remove_dquot_hash(struct dquot
*dquot
)
253 hlist_del_init(&dquot
->dq_hash
);
256 static struct dquot
*find_dquot(unsigned int hashent
, struct super_block
*sb
,
257 unsigned int id
, int type
)
259 struct hlist_node
*node
;
262 hlist_for_each (node
, dquot_hash
+hashent
) {
263 dquot
= hlist_entry(node
, struct dquot
, dq_hash
);
264 if (dquot
->dq_sb
== sb
&& dquot
->dq_id
== id
&&
265 dquot
->dq_type
== type
)
271 /* Add a dquot to the tail of the free list */
272 static inline void put_dquot_last(struct dquot
*dquot
)
274 list_add_tail(&dquot
->dq_free
, &free_dquots
);
275 dqstats
.free_dquots
++;
278 static inline void remove_free_dquot(struct dquot
*dquot
)
280 if (list_empty(&dquot
->dq_free
))
282 list_del_init(&dquot
->dq_free
);
283 dqstats
.free_dquots
--;
286 static inline void put_inuse(struct dquot
*dquot
)
288 /* We add to the back of inuse list so we don't have to restart
289 * when traversing this list and we block */
290 list_add_tail(&dquot
->dq_inuse
, &inuse_list
);
291 dqstats
.allocated_dquots
++;
294 static inline void remove_inuse(struct dquot
*dquot
)
296 dqstats
.allocated_dquots
--;
297 list_del(&dquot
->dq_inuse
);
300 * End of list functions needing dq_list_lock
303 static void wait_on_dquot(struct dquot
*dquot
)
305 mutex_lock(&dquot
->dq_lock
);
306 mutex_unlock(&dquot
->dq_lock
);
309 static inline int dquot_dirty(struct dquot
*dquot
)
311 return test_bit(DQ_MOD_B
, &dquot
->dq_flags
);
314 static inline int mark_dquot_dirty(struct dquot
*dquot
)
316 return dquot
->dq_sb
->dq_op
->mark_dirty(dquot
);
319 int dquot_mark_dquot_dirty(struct dquot
*dquot
)
321 spin_lock(&dq_list_lock
);
322 if (!test_and_set_bit(DQ_MOD_B
, &dquot
->dq_flags
))
323 list_add(&dquot
->dq_dirty
, &sb_dqopt(dquot
->dq_sb
)->
324 info
[dquot
->dq_type
].dqi_dirty_list
);
325 spin_unlock(&dq_list_lock
);
328 EXPORT_SYMBOL(dquot_mark_dquot_dirty
);
330 /* This function needs dq_list_lock */
331 static inline int clear_dquot_dirty(struct dquot
*dquot
)
333 if (!test_and_clear_bit(DQ_MOD_B
, &dquot
->dq_flags
))
335 list_del_init(&dquot
->dq_dirty
);
339 void mark_info_dirty(struct super_block
*sb
, int type
)
341 set_bit(DQF_INFO_DIRTY_B
, &sb_dqopt(sb
)->info
[type
].dqi_flags
);
343 EXPORT_SYMBOL(mark_info_dirty
);
346 * Read dquot from disk and alloc space for it
349 int dquot_acquire(struct dquot
*dquot
)
351 int ret
= 0, ret2
= 0;
352 struct quota_info
*dqopt
= sb_dqopt(dquot
->dq_sb
);
354 mutex_lock(&dquot
->dq_lock
);
355 mutex_lock(&dqopt
->dqio_mutex
);
356 if (!test_bit(DQ_READ_B
, &dquot
->dq_flags
))
357 ret
= dqopt
->ops
[dquot
->dq_type
]->read_dqblk(dquot
);
360 set_bit(DQ_READ_B
, &dquot
->dq_flags
);
361 /* Instantiate dquot if needed */
362 if (!test_bit(DQ_ACTIVE_B
, &dquot
->dq_flags
) && !dquot
->dq_off
) {
363 ret
= dqopt
->ops
[dquot
->dq_type
]->commit_dqblk(dquot
);
364 /* Write the info if needed */
365 if (info_dirty(&dqopt
->info
[dquot
->dq_type
])) {
366 ret2
= dqopt
->ops
[dquot
->dq_type
]->write_file_info(
367 dquot
->dq_sb
, dquot
->dq_type
);
376 set_bit(DQ_ACTIVE_B
, &dquot
->dq_flags
);
378 mutex_unlock(&dqopt
->dqio_mutex
);
379 mutex_unlock(&dquot
->dq_lock
);
382 EXPORT_SYMBOL(dquot_acquire
);
385 * Write dquot to disk
387 int dquot_commit(struct dquot
*dquot
)
389 int ret
= 0, ret2
= 0;
390 struct quota_info
*dqopt
= sb_dqopt(dquot
->dq_sb
);
392 mutex_lock(&dqopt
->dqio_mutex
);
393 spin_lock(&dq_list_lock
);
394 if (!clear_dquot_dirty(dquot
)) {
395 spin_unlock(&dq_list_lock
);
398 spin_unlock(&dq_list_lock
);
399 /* Inactive dquot can be only if there was error during read/init
400 * => we have better not writing it */
401 if (test_bit(DQ_ACTIVE_B
, &dquot
->dq_flags
)) {
402 ret
= dqopt
->ops
[dquot
->dq_type
]->commit_dqblk(dquot
);
403 if (info_dirty(&dqopt
->info
[dquot
->dq_type
])) {
404 ret2
= dqopt
->ops
[dquot
->dq_type
]->write_file_info(
405 dquot
->dq_sb
, dquot
->dq_type
);
411 mutex_unlock(&dqopt
->dqio_mutex
);
414 EXPORT_SYMBOL(dquot_commit
);
419 int dquot_release(struct dquot
*dquot
)
421 int ret
= 0, ret2
= 0;
422 struct quota_info
*dqopt
= sb_dqopt(dquot
->dq_sb
);
424 mutex_lock(&dquot
->dq_lock
);
425 /* Check whether we are not racing with some other dqget() */
426 if (atomic_read(&dquot
->dq_count
) > 1)
428 mutex_lock(&dqopt
->dqio_mutex
);
429 if (dqopt
->ops
[dquot
->dq_type
]->release_dqblk
) {
430 ret
= dqopt
->ops
[dquot
->dq_type
]->release_dqblk(dquot
);
432 if (info_dirty(&dqopt
->info
[dquot
->dq_type
])) {
433 ret2
= dqopt
->ops
[dquot
->dq_type
]->write_file_info(
434 dquot
->dq_sb
, dquot
->dq_type
);
439 clear_bit(DQ_ACTIVE_B
, &dquot
->dq_flags
);
440 mutex_unlock(&dqopt
->dqio_mutex
);
442 mutex_unlock(&dquot
->dq_lock
);
445 EXPORT_SYMBOL(dquot_release
);
447 void dquot_destroy(struct dquot
*dquot
)
449 kmem_cache_free(dquot_cachep
, dquot
);
451 EXPORT_SYMBOL(dquot_destroy
);
453 static inline void do_destroy_dquot(struct dquot
*dquot
)
455 dquot
->dq_sb
->dq_op
->destroy_dquot(dquot
);
458 /* Invalidate all dquots on the list. Note that this function is called after
459 * quota is disabled and pointers from inodes removed so there cannot be new
460 * quota users. There can still be some users of quotas due to inodes being
461 * just deleted or pruned by prune_icache() (those are not attached to any
462 * list) or parallel quotactl call. We have to wait for such users.
464 static void invalidate_dquots(struct super_block
*sb
, int type
)
466 struct dquot
*dquot
, *tmp
;
469 spin_lock(&dq_list_lock
);
470 list_for_each_entry_safe(dquot
, tmp
, &inuse_list
, dq_inuse
) {
471 if (dquot
->dq_sb
!= sb
)
473 if (dquot
->dq_type
!= type
)
475 /* Wait for dquot users */
476 if (atomic_read(&dquot
->dq_count
)) {
479 atomic_inc(&dquot
->dq_count
);
480 prepare_to_wait(&dquot
->dq_wait_unused
, &wait
,
481 TASK_UNINTERRUPTIBLE
);
482 spin_unlock(&dq_list_lock
);
483 /* Once dqput() wakes us up, we know it's time to free
485 * IMPORTANT: we rely on the fact that there is always
486 * at most one process waiting for dquot to free.
487 * Otherwise dq_count would be > 1 and we would never
490 if (atomic_read(&dquot
->dq_count
) > 1)
492 finish_wait(&dquot
->dq_wait_unused
, &wait
);
494 /* At this moment dquot() need not exist (it could be
495 * reclaimed by prune_dqcache(). Hence we must
500 * Quota now has no users and it has been written on last
503 remove_dquot_hash(dquot
);
504 remove_free_dquot(dquot
);
506 do_destroy_dquot(dquot
);
508 spin_unlock(&dq_list_lock
);
511 /* Call callback for every active dquot on given filesystem */
512 int dquot_scan_active(struct super_block
*sb
,
513 int (*fn
)(struct dquot
*dquot
, unsigned long priv
),
516 struct dquot
*dquot
, *old_dquot
= NULL
;
519 mutex_lock(&sb_dqopt(sb
)->dqonoff_mutex
);
520 spin_lock(&dq_list_lock
);
521 list_for_each_entry(dquot
, &inuse_list
, dq_inuse
) {
522 if (!test_bit(DQ_ACTIVE_B
, &dquot
->dq_flags
))
524 if (dquot
->dq_sb
!= sb
)
526 /* Now we have active dquot so we can just increase use count */
527 atomic_inc(&dquot
->dq_count
);
529 spin_unlock(&dq_list_lock
);
532 ret
= fn(dquot
, priv
);
535 spin_lock(&dq_list_lock
);
536 /* We are safe to continue now because our dquot could not
537 * be moved out of the inuse list while we hold the reference */
539 spin_unlock(&dq_list_lock
);
542 mutex_unlock(&sb_dqopt(sb
)->dqonoff_mutex
);
545 EXPORT_SYMBOL(dquot_scan_active
);
547 int vfs_quota_sync(struct super_block
*sb
, int type
)
549 struct list_head
*dirty
;
551 struct quota_info
*dqopt
= sb_dqopt(sb
);
554 mutex_lock(&dqopt
->dqonoff_mutex
);
555 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++) {
556 if (type
!= -1 && cnt
!= type
)
558 if (!sb_has_quota_active(sb
, cnt
))
560 spin_lock(&dq_list_lock
);
561 dirty
= &dqopt
->info
[cnt
].dqi_dirty_list
;
562 while (!list_empty(dirty
)) {
563 dquot
= list_first_entry(dirty
, struct dquot
,
565 /* Dirty and inactive can be only bad dquot... */
566 if (!test_bit(DQ_ACTIVE_B
, &dquot
->dq_flags
)) {
567 clear_dquot_dirty(dquot
);
570 /* Now we have active dquot from which someone is
571 * holding reference so we can safely just increase
573 atomic_inc(&dquot
->dq_count
);
575 spin_unlock(&dq_list_lock
);
576 sb
->dq_op
->write_dquot(dquot
);
578 spin_lock(&dq_list_lock
);
580 spin_unlock(&dq_list_lock
);
583 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++)
584 if ((cnt
== type
|| type
== -1) && sb_has_quota_active(sb
, cnt
)
585 && info_dirty(&dqopt
->info
[cnt
]))
586 sb
->dq_op
->write_info(sb
, cnt
);
587 spin_lock(&dq_list_lock
);
589 spin_unlock(&dq_list_lock
);
590 mutex_unlock(&dqopt
->dqonoff_mutex
);
594 EXPORT_SYMBOL(vfs_quota_sync
);
596 /* Free unused dquots from cache */
597 static void prune_dqcache(int count
)
599 struct list_head
*head
;
602 head
= free_dquots
.prev
;
603 while (head
!= &free_dquots
&& count
) {
604 dquot
= list_entry(head
, struct dquot
, dq_free
);
605 remove_dquot_hash(dquot
);
606 remove_free_dquot(dquot
);
608 do_destroy_dquot(dquot
);
610 head
= free_dquots
.prev
;
615 * This is called from kswapd when we think we need some
619 static int shrink_dqcache_memory(int nr
, gfp_t gfp_mask
)
622 spin_lock(&dq_list_lock
);
624 spin_unlock(&dq_list_lock
);
626 return (dqstats
.free_dquots
/ 100) * sysctl_vfs_cache_pressure
;
629 static struct shrinker dqcache_shrinker
= {
630 .shrink
= shrink_dqcache_memory
,
631 .seeks
= DEFAULT_SEEKS
,
635 * Put reference to dquot
636 * NOTE: If you change this function please check whether dqput_blocks() works right...
638 void dqput(struct dquot
*dquot
)
644 #ifdef __DQUOT_PARANOIA
645 if (!atomic_read(&dquot
->dq_count
)) {
646 printk("VFS: dqput: trying to free free dquot\n");
647 printk("VFS: device %s, dquot of %s %d\n",
649 quotatypes
[dquot
->dq_type
],
655 spin_lock(&dq_list_lock
);
657 spin_unlock(&dq_list_lock
);
659 spin_lock(&dq_list_lock
);
660 if (atomic_read(&dquot
->dq_count
) > 1) {
661 /* We have more than one user... nothing to do */
662 atomic_dec(&dquot
->dq_count
);
663 /* Releasing dquot during quotaoff phase? */
664 if (!sb_has_quota_active(dquot
->dq_sb
, dquot
->dq_type
) &&
665 atomic_read(&dquot
->dq_count
) == 1)
666 wake_up(&dquot
->dq_wait_unused
);
667 spin_unlock(&dq_list_lock
);
670 /* Need to release dquot? */
671 if (test_bit(DQ_ACTIVE_B
, &dquot
->dq_flags
) && dquot_dirty(dquot
)) {
672 spin_unlock(&dq_list_lock
);
673 /* Commit dquot before releasing */
674 ret
= dquot
->dq_sb
->dq_op
->write_dquot(dquot
);
676 printk(KERN_ERR
"VFS: cannot write quota structure on "
677 "device %s (error %d). Quota may get out of "
678 "sync!\n", dquot
->dq_sb
->s_id
, ret
);
680 * We clear dirty bit anyway, so that we avoid
683 spin_lock(&dq_list_lock
);
684 clear_dquot_dirty(dquot
);
685 spin_unlock(&dq_list_lock
);
689 /* Clear flag in case dquot was inactive (something bad happened) */
690 clear_dquot_dirty(dquot
);
691 if (test_bit(DQ_ACTIVE_B
, &dquot
->dq_flags
)) {
692 spin_unlock(&dq_list_lock
);
693 dquot
->dq_sb
->dq_op
->release_dquot(dquot
);
696 atomic_dec(&dquot
->dq_count
);
697 #ifdef __DQUOT_PARANOIA
699 BUG_ON(!list_empty(&dquot
->dq_free
));
701 put_dquot_last(dquot
);
702 spin_unlock(&dq_list_lock
);
704 EXPORT_SYMBOL(dqput
);
706 struct dquot
*dquot_alloc(struct super_block
*sb
, int type
)
708 return kmem_cache_zalloc(dquot_cachep
, GFP_NOFS
);
710 EXPORT_SYMBOL(dquot_alloc
);
712 static struct dquot
*get_empty_dquot(struct super_block
*sb
, int type
)
716 dquot
= sb
->dq_op
->alloc_dquot(sb
, type
);
720 mutex_init(&dquot
->dq_lock
);
721 INIT_LIST_HEAD(&dquot
->dq_free
);
722 INIT_LIST_HEAD(&dquot
->dq_inuse
);
723 INIT_HLIST_NODE(&dquot
->dq_hash
);
724 INIT_LIST_HEAD(&dquot
->dq_dirty
);
725 init_waitqueue_head(&dquot
->dq_wait_unused
);
727 dquot
->dq_type
= type
;
728 atomic_set(&dquot
->dq_count
, 1);
734 * Get reference to dquot
736 * Locking is slightly tricky here. We are guarded from parallel quotaoff()
737 * destroying our dquot by:
738 * a) checking for quota flags under dq_list_lock and
739 * b) getting a reference to dquot before we release dq_list_lock
741 struct dquot
*dqget(struct super_block
*sb
, unsigned int id
, int type
)
743 unsigned int hashent
= hashfn(sb
, id
, type
);
744 struct dquot
*dquot
= NULL
, *empty
= NULL
;
746 if (!sb_has_quota_active(sb
, type
))
749 spin_lock(&dq_list_lock
);
750 spin_lock(&dq_state_lock
);
751 if (!sb_has_quota_active(sb
, type
)) {
752 spin_unlock(&dq_state_lock
);
753 spin_unlock(&dq_list_lock
);
756 spin_unlock(&dq_state_lock
);
758 dquot
= find_dquot(hashent
, sb
, id
, type
);
761 spin_unlock(&dq_list_lock
);
762 empty
= get_empty_dquot(sb
, type
);
764 schedule(); /* Try to wait for a moment... */
770 /* all dquots go on the inuse_list */
772 /* hash it first so it can be found */
773 insert_dquot_hash(dquot
);
775 spin_unlock(&dq_list_lock
);
777 if (!atomic_read(&dquot
->dq_count
))
778 remove_free_dquot(dquot
);
779 atomic_inc(&dquot
->dq_count
);
780 dqstats
.cache_hits
++;
782 spin_unlock(&dq_list_lock
);
784 /* Wait for dq_lock - after this we know that either dquot_release() is
785 * already finished or it will be canceled due to dq_count > 1 test */
786 wait_on_dquot(dquot
);
787 /* Read the dquot / allocate space in quota file */
788 if (!test_bit(DQ_ACTIVE_B
, &dquot
->dq_flags
) &&
789 sb
->dq_op
->acquire_dquot(dquot
) < 0) {
794 #ifdef __DQUOT_PARANOIA
795 BUG_ON(!dquot
->dq_sb
); /* Has somebody invalidated entry under us? */
799 do_destroy_dquot(empty
);
803 EXPORT_SYMBOL(dqget
);
805 static int dqinit_needed(struct inode
*inode
, int type
)
809 if (IS_NOQUOTA(inode
))
812 return !inode
->i_dquot
[type
];
813 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++)
814 if (!inode
->i_dquot
[cnt
])
819 /* This routine is guarded by dqonoff_mutex mutex */
820 static void add_dquot_ref(struct super_block
*sb
, int type
)
822 struct inode
*inode
, *old_inode
= NULL
;
824 spin_lock(&inode_lock
);
825 list_for_each_entry(inode
, &sb
->s_inodes
, i_sb_list
) {
826 if (!atomic_read(&inode
->i_writecount
))
828 if (!dqinit_needed(inode
, type
))
830 if (inode
->i_state
& (I_FREEING
|I_WILL_FREE
))
834 spin_unlock(&inode_lock
);
837 sb
->dq_op
->initialize(inode
, type
);
838 /* We hold a reference to 'inode' so it couldn't have been
839 * removed from s_inodes list while we dropped the inode_lock.
840 * We cannot iput the inode now as we can be holding the last
841 * reference and we cannot iput it under inode_lock. So we
842 * keep the reference and iput it later. */
844 spin_lock(&inode_lock
);
846 spin_unlock(&inode_lock
);
851 * Return 0 if dqput() won't block.
852 * (note that 1 doesn't necessarily mean blocking)
854 static inline int dqput_blocks(struct dquot
*dquot
)
856 if (atomic_read(&dquot
->dq_count
) <= 1)
862 * Remove references to dquots from inode and add dquot to list for freeing
863 * if we have the last referece to dquot
864 * We can't race with anybody because we hold dqptr_sem for writing...
866 static int remove_inode_dquot_ref(struct inode
*inode
, int type
,
867 struct list_head
*tofree_head
)
869 struct dquot
*dquot
= inode
->i_dquot
[type
];
871 inode
->i_dquot
[type
] = NULL
;
873 if (dqput_blocks(dquot
)) {
874 #ifdef __DQUOT_PARANOIA
875 if (atomic_read(&dquot
->dq_count
) != 1)
876 printk(KERN_WARNING
"VFS: Adding dquot with dq_count %d to dispose list.\n", atomic_read(&dquot
->dq_count
));
878 spin_lock(&dq_list_lock
);
879 /* As dquot must have currently users it can't be on
880 * the free list... */
881 list_add(&dquot
->dq_free
, tofree_head
);
882 spin_unlock(&dq_list_lock
);
886 dqput(dquot
); /* We have guaranteed we won't block */
892 * Free list of dquots
893 * Dquots are removed from inodes and no new references can be got so we are
894 * the only ones holding reference
896 static void put_dquot_list(struct list_head
*tofree_head
)
898 struct list_head
*act_head
;
901 act_head
= tofree_head
->next
;
902 while (act_head
!= tofree_head
) {
903 dquot
= list_entry(act_head
, struct dquot
, dq_free
);
904 act_head
= act_head
->next
;
905 /* Remove dquot from the list so we won't have problems... */
906 list_del_init(&dquot
->dq_free
);
911 static void remove_dquot_ref(struct super_block
*sb
, int type
,
912 struct list_head
*tofree_head
)
916 spin_lock(&inode_lock
);
917 list_for_each_entry(inode
, &sb
->s_inodes
, i_sb_list
) {
918 if (!IS_NOQUOTA(inode
))
919 remove_inode_dquot_ref(inode
, type
, tofree_head
);
921 spin_unlock(&inode_lock
);
924 /* Gather all references from inodes and drop them */
925 static void drop_dquot_ref(struct super_block
*sb
, int type
)
927 LIST_HEAD(tofree_head
);
930 down_write(&sb_dqopt(sb
)->dqptr_sem
);
931 remove_dquot_ref(sb
, type
, &tofree_head
);
932 up_write(&sb_dqopt(sb
)->dqptr_sem
);
933 put_dquot_list(&tofree_head
);
937 static inline void dquot_incr_inodes(struct dquot
*dquot
, qsize_t number
)
939 dquot
->dq_dqb
.dqb_curinodes
+= number
;
942 static inline void dquot_incr_space(struct dquot
*dquot
, qsize_t number
)
944 dquot
->dq_dqb
.dqb_curspace
+= number
;
947 static inline void dquot_resv_space(struct dquot
*dquot
, qsize_t number
)
949 dquot
->dq_dqb
.dqb_rsvspace
+= number
;
953 * Claim reserved quota space
955 static void dquot_claim_reserved_space(struct dquot
*dquot
,
958 WARN_ON(dquot
->dq_dqb
.dqb_rsvspace
< number
);
959 dquot
->dq_dqb
.dqb_curspace
+= number
;
960 dquot
->dq_dqb
.dqb_rsvspace
-= number
;
964 void dquot_free_reserved_space(struct dquot
*dquot
, qsize_t number
)
966 dquot
->dq_dqb
.dqb_rsvspace
-= number
;
969 static void dquot_decr_inodes(struct dquot
*dquot
, qsize_t number
)
971 if (sb_dqopt(dquot
->dq_sb
)->flags
& DQUOT_NEGATIVE_USAGE
||
972 dquot
->dq_dqb
.dqb_curinodes
>= number
)
973 dquot
->dq_dqb
.dqb_curinodes
-= number
;
975 dquot
->dq_dqb
.dqb_curinodes
= 0;
976 if (dquot
->dq_dqb
.dqb_curinodes
<= dquot
->dq_dqb
.dqb_isoftlimit
)
977 dquot
->dq_dqb
.dqb_itime
= (time_t) 0;
978 clear_bit(DQ_INODES_B
, &dquot
->dq_flags
);
981 static void dquot_decr_space(struct dquot
*dquot
, qsize_t number
)
983 if (sb_dqopt(dquot
->dq_sb
)->flags
& DQUOT_NEGATIVE_USAGE
||
984 dquot
->dq_dqb
.dqb_curspace
>= number
)
985 dquot
->dq_dqb
.dqb_curspace
-= number
;
987 dquot
->dq_dqb
.dqb_curspace
= 0;
988 if (dquot
->dq_dqb
.dqb_curspace
<= dquot
->dq_dqb
.dqb_bsoftlimit
)
989 dquot
->dq_dqb
.dqb_btime
= (time_t) 0;
990 clear_bit(DQ_BLKS_B
, &dquot
->dq_flags
);
993 static int warning_issued(struct dquot
*dquot
, const int warntype
)
995 int flag
= (warntype
== QUOTA_NL_BHARDWARN
||
996 warntype
== QUOTA_NL_BSOFTLONGWARN
) ? DQ_BLKS_B
:
997 ((warntype
== QUOTA_NL_IHARDWARN
||
998 warntype
== QUOTA_NL_ISOFTLONGWARN
) ? DQ_INODES_B
: 0);
1002 return test_and_set_bit(flag
, &dquot
->dq_flags
);
1005 #ifdef CONFIG_PRINT_QUOTA_WARNING
1006 static int flag_print_warnings
= 1;
1008 static int need_print_warning(struct dquot
*dquot
)
1010 if (!flag_print_warnings
)
1013 switch (dquot
->dq_type
) {
1015 return current_fsuid() == dquot
->dq_id
;
1017 return in_group_p(dquot
->dq_id
);
1022 /* Print warning to user which exceeded quota */
1023 static void print_warning(struct dquot
*dquot
, const int warntype
)
1026 struct tty_struct
*tty
;
1028 if (warntype
== QUOTA_NL_IHARDBELOW
||
1029 warntype
== QUOTA_NL_ISOFTBELOW
||
1030 warntype
== QUOTA_NL_BHARDBELOW
||
1031 warntype
== QUOTA_NL_BSOFTBELOW
|| !need_print_warning(dquot
))
1034 tty
= get_current_tty();
1037 tty_write_message(tty
, dquot
->dq_sb
->s_id
);
1038 if (warntype
== QUOTA_NL_ISOFTWARN
|| warntype
== QUOTA_NL_BSOFTWARN
)
1039 tty_write_message(tty
, ": warning, ");
1041 tty_write_message(tty
, ": write failed, ");
1042 tty_write_message(tty
, quotatypes
[dquot
->dq_type
]);
1044 case QUOTA_NL_IHARDWARN
:
1045 msg
= " file limit reached.\r\n";
1047 case QUOTA_NL_ISOFTLONGWARN
:
1048 msg
= " file quota exceeded too long.\r\n";
1050 case QUOTA_NL_ISOFTWARN
:
1051 msg
= " file quota exceeded.\r\n";
1053 case QUOTA_NL_BHARDWARN
:
1054 msg
= " block limit reached.\r\n";
1056 case QUOTA_NL_BSOFTLONGWARN
:
1057 msg
= " block quota exceeded too long.\r\n";
1059 case QUOTA_NL_BSOFTWARN
:
1060 msg
= " block quota exceeded.\r\n";
1063 tty_write_message(tty
, msg
);
1068 #ifdef CONFIG_QUOTA_NETLINK_INTERFACE
1070 /* Netlink family structure for quota */
1071 static struct genl_family quota_genl_family
= {
1072 .id
= GENL_ID_GENERATE
,
1074 .name
= "VFS_DQUOT",
1076 .maxattr
= QUOTA_NL_A_MAX
,
1079 /* Send warning to userspace about user which exceeded quota */
1080 static void send_warning(const struct dquot
*dquot
, const char warntype
)
1082 static atomic_t seq
;
1083 struct sk_buff
*skb
;
1086 int msg_size
= 4 * nla_total_size(sizeof(u32
)) +
1087 2 * nla_total_size(sizeof(u64
));
1089 /* We have to allocate using GFP_NOFS as we are called from a
1090 * filesystem performing write and thus further recursion into
1091 * the fs to free some data could cause deadlocks. */
1092 skb
= genlmsg_new(msg_size
, GFP_NOFS
);
1095 "VFS: Not enough memory to send quota warning.\n");
1098 msg_head
= genlmsg_put(skb
, 0, atomic_add_return(1, &seq
),
1099 "a_genl_family
, 0, QUOTA_NL_C_WARNING
);
1102 "VFS: Cannot store netlink header in quota warning.\n");
1105 ret
= nla_put_u32(skb
, QUOTA_NL_A_QTYPE
, dquot
->dq_type
);
1108 ret
= nla_put_u64(skb
, QUOTA_NL_A_EXCESS_ID
, dquot
->dq_id
);
1111 ret
= nla_put_u32(skb
, QUOTA_NL_A_WARNING
, warntype
);
1114 ret
= nla_put_u32(skb
, QUOTA_NL_A_DEV_MAJOR
,
1115 MAJOR(dquot
->dq_sb
->s_dev
));
1118 ret
= nla_put_u32(skb
, QUOTA_NL_A_DEV_MINOR
,
1119 MINOR(dquot
->dq_sb
->s_dev
));
1122 ret
= nla_put_u64(skb
, QUOTA_NL_A_CAUSED_ID
, current_uid());
1125 genlmsg_end(skb
, msg_head
);
1127 ret
= genlmsg_multicast(skb
, 0, quota_genl_family
.id
, GFP_NOFS
);
1128 if (ret
< 0 && ret
!= -ESRCH
)
1130 "VFS: Failed to send notification message: %d\n", ret
);
1133 printk(KERN_ERR
"VFS: Not enough space to compose quota message!\n");
1139 * Write warnings to the console and send warning messages over netlink.
1141 * Note that this function can sleep.
1143 static void flush_warnings(struct dquot
*const *dquots
, char *warntype
)
1147 for (i
= 0; i
< MAXQUOTAS
; i
++)
1148 if (dquots
[i
] && warntype
[i
] != QUOTA_NL_NOWARN
&&
1149 !warning_issued(dquots
[i
], warntype
[i
])) {
1150 #ifdef CONFIG_PRINT_QUOTA_WARNING
1151 print_warning(dquots
[i
], warntype
[i
]);
1153 #ifdef CONFIG_QUOTA_NETLINK_INTERFACE
1154 send_warning(dquots
[i
], warntype
[i
]);
1159 static int ignore_hardlimit(struct dquot
*dquot
)
1161 struct mem_dqinfo
*info
= &sb_dqopt(dquot
->dq_sb
)->info
[dquot
->dq_type
];
1163 return capable(CAP_SYS_RESOURCE
) &&
1164 (info
->dqi_format
->qf_fmt_id
!= QFMT_VFS_OLD
||
1165 !(info
->dqi_flags
& V1_DQF_RSQUASH
));
1168 /* needs dq_data_lock */
1169 static int check_idq(struct dquot
*dquot
, qsize_t inodes
, char *warntype
)
1171 qsize_t newinodes
= dquot
->dq_dqb
.dqb_curinodes
+ inodes
;
1173 *warntype
= QUOTA_NL_NOWARN
;
1174 if (!sb_has_quota_limits_enabled(dquot
->dq_sb
, dquot
->dq_type
) ||
1175 test_bit(DQ_FAKE_B
, &dquot
->dq_flags
))
1178 if (dquot
->dq_dqb
.dqb_ihardlimit
&&
1179 newinodes
> dquot
->dq_dqb
.dqb_ihardlimit
&&
1180 !ignore_hardlimit(dquot
)) {
1181 *warntype
= QUOTA_NL_IHARDWARN
;
1185 if (dquot
->dq_dqb
.dqb_isoftlimit
&&
1186 newinodes
> dquot
->dq_dqb
.dqb_isoftlimit
&&
1187 dquot
->dq_dqb
.dqb_itime
&&
1188 get_seconds() >= dquot
->dq_dqb
.dqb_itime
&&
1189 !ignore_hardlimit(dquot
)) {
1190 *warntype
= QUOTA_NL_ISOFTLONGWARN
;
1194 if (dquot
->dq_dqb
.dqb_isoftlimit
&&
1195 newinodes
> dquot
->dq_dqb
.dqb_isoftlimit
&&
1196 dquot
->dq_dqb
.dqb_itime
== 0) {
1197 *warntype
= QUOTA_NL_ISOFTWARN
;
1198 dquot
->dq_dqb
.dqb_itime
= get_seconds() +
1199 sb_dqopt(dquot
->dq_sb
)->info
[dquot
->dq_type
].dqi_igrace
;
1205 /* needs dq_data_lock */
1206 static int check_bdq(struct dquot
*dquot
, qsize_t space
, int prealloc
, char *warntype
)
1209 struct super_block
*sb
= dquot
->dq_sb
;
1211 *warntype
= QUOTA_NL_NOWARN
;
1212 if (!sb_has_quota_limits_enabled(sb
, dquot
->dq_type
) ||
1213 test_bit(DQ_FAKE_B
, &dquot
->dq_flags
))
1216 tspace
= dquot
->dq_dqb
.dqb_curspace
+ dquot
->dq_dqb
.dqb_rsvspace
1219 if (dquot
->dq_dqb
.dqb_bhardlimit
&&
1220 tspace
> dquot
->dq_dqb
.dqb_bhardlimit
&&
1221 !ignore_hardlimit(dquot
)) {
1223 *warntype
= QUOTA_NL_BHARDWARN
;
1227 if (dquot
->dq_dqb
.dqb_bsoftlimit
&&
1228 tspace
> dquot
->dq_dqb
.dqb_bsoftlimit
&&
1229 dquot
->dq_dqb
.dqb_btime
&&
1230 get_seconds() >= dquot
->dq_dqb
.dqb_btime
&&
1231 !ignore_hardlimit(dquot
)) {
1233 *warntype
= QUOTA_NL_BSOFTLONGWARN
;
1237 if (dquot
->dq_dqb
.dqb_bsoftlimit
&&
1238 tspace
> dquot
->dq_dqb
.dqb_bsoftlimit
&&
1239 dquot
->dq_dqb
.dqb_btime
== 0) {
1241 *warntype
= QUOTA_NL_BSOFTWARN
;
1242 dquot
->dq_dqb
.dqb_btime
= get_seconds() +
1243 sb_dqopt(sb
)->info
[dquot
->dq_type
].dqi_bgrace
;
1247 * We don't allow preallocation to exceed softlimit so exceeding will
1256 static int info_idq_free(struct dquot
*dquot
, qsize_t inodes
)
1260 if (test_bit(DQ_FAKE_B
, &dquot
->dq_flags
) ||
1261 dquot
->dq_dqb
.dqb_curinodes
<= dquot
->dq_dqb
.dqb_isoftlimit
||
1262 !sb_has_quota_limits_enabled(dquot
->dq_sb
, dquot
->dq_type
))
1263 return QUOTA_NL_NOWARN
;
1265 newinodes
= dquot
->dq_dqb
.dqb_curinodes
- inodes
;
1266 if (newinodes
<= dquot
->dq_dqb
.dqb_isoftlimit
)
1267 return QUOTA_NL_ISOFTBELOW
;
1268 if (dquot
->dq_dqb
.dqb_curinodes
>= dquot
->dq_dqb
.dqb_ihardlimit
&&
1269 newinodes
< dquot
->dq_dqb
.dqb_ihardlimit
)
1270 return QUOTA_NL_IHARDBELOW
;
1271 return QUOTA_NL_NOWARN
;
1274 static int info_bdq_free(struct dquot
*dquot
, qsize_t space
)
1276 if (test_bit(DQ_FAKE_B
, &dquot
->dq_flags
) ||
1277 dquot
->dq_dqb
.dqb_curspace
<= dquot
->dq_dqb
.dqb_bsoftlimit
)
1278 return QUOTA_NL_NOWARN
;
1280 if (dquot
->dq_dqb
.dqb_curspace
- space
<= dquot
->dq_dqb
.dqb_bsoftlimit
)
1281 return QUOTA_NL_BSOFTBELOW
;
1282 if (dquot
->dq_dqb
.dqb_curspace
>= dquot
->dq_dqb
.dqb_bhardlimit
&&
1283 dquot
->dq_dqb
.dqb_curspace
- space
< dquot
->dq_dqb
.dqb_bhardlimit
)
1284 return QUOTA_NL_BHARDBELOW
;
1285 return QUOTA_NL_NOWARN
;
1288 * Initialize quota pointers in inode
1289 * We do things in a bit complicated way but by that we avoid calling
1290 * dqget() and thus filesystem callbacks under dqptr_sem.
1292 int dquot_initialize(struct inode
*inode
, int type
)
1294 unsigned int id
= 0;
1296 struct dquot
*got
[MAXQUOTAS
] = { NULL
, NULL
};
1297 struct super_block
*sb
= inode
->i_sb
;
1299 /* First test before acquiring mutex - solves deadlocks when we
1300 * re-enter the quota code and are already holding the mutex */
1301 if (IS_NOQUOTA(inode
))
1304 /* First get references to structures we might need. */
1305 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++) {
1306 if (type
!= -1 && cnt
!= type
)
1316 got
[cnt
] = dqget(sb
, id
, cnt
);
1319 down_write(&sb_dqopt(sb
)->dqptr_sem
);
1320 /* Having dqptr_sem we know NOQUOTA flags can't be altered... */
1321 if (IS_NOQUOTA(inode
))
1323 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++) {
1324 if (type
!= -1 && cnt
!= type
)
1326 /* Avoid races with quotaoff() */
1327 if (!sb_has_quota_active(sb
, cnt
))
1329 if (!inode
->i_dquot
[cnt
]) {
1330 inode
->i_dquot
[cnt
] = got
[cnt
];
1335 up_write(&sb_dqopt(sb
)->dqptr_sem
);
1336 /* Drop unused references */
1337 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++)
1341 EXPORT_SYMBOL(dquot_initialize
);
1344 * Release all quotas referenced by inode
1346 int dquot_drop(struct inode
*inode
)
1349 struct dquot
*put
[MAXQUOTAS
];
1351 down_write(&sb_dqopt(inode
->i_sb
)->dqptr_sem
);
1352 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++) {
1353 put
[cnt
] = inode
->i_dquot
[cnt
];
1354 inode
->i_dquot
[cnt
] = NULL
;
1356 up_write(&sb_dqopt(inode
->i_sb
)->dqptr_sem
);
1358 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++)
1362 EXPORT_SYMBOL(dquot_drop
);
1364 /* Wrapper to remove references to quota structures from inode */
1365 void vfs_dq_drop(struct inode
*inode
)
1367 /* Here we can get arbitrary inode from clear_inode() so we have
1368 * to be careful. OTOH we don't need locking as quota operations
1369 * are allowed to change only at mount time */
1370 if (!IS_NOQUOTA(inode
) && inode
->i_sb
&& inode
->i_sb
->dq_op
1371 && inode
->i_sb
->dq_op
->drop
) {
1373 /* Test before calling to rule out calls from proc and such
1374 * where we are not allowed to block. Note that this is
1375 * actually reliable test even without the lock - the caller
1376 * must assure that nobody can come after the DQUOT_DROP and
1377 * add quota pointers back anyway */
1378 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++)
1379 if (inode
->i_dquot
[cnt
])
1381 if (cnt
< MAXQUOTAS
)
1382 inode
->i_sb
->dq_op
->drop(inode
);
1385 EXPORT_SYMBOL(vfs_dq_drop
);
1388 * Following four functions update i_blocks+i_bytes fields and
1389 * quota information (together with appropriate checks)
1390 * NOTE: We absolutely rely on the fact that caller dirties
1391 * the inode (usually macros in quotaops.h care about this) and
1392 * holds a handle for the current transaction so that dquot write and
1393 * inode write go into the same transaction.
1397 * This operation can block, but only after everything is updated
1399 int __dquot_alloc_space(struct inode
*inode
, qsize_t number
,
1400 int warn
, int reserve
)
1402 int cnt
, ret
= QUOTA_OK
;
1403 char warntype
[MAXQUOTAS
];
1405 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++)
1406 warntype
[cnt
] = QUOTA_NL_NOWARN
;
1408 spin_lock(&dq_data_lock
);
1409 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++) {
1410 if (!inode
->i_dquot
[cnt
])
1412 if (check_bdq(inode
->i_dquot
[cnt
], number
, warn
, warntype
+cnt
)
1418 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++) {
1419 if (!inode
->i_dquot
[cnt
])
1422 dquot_resv_space(inode
->i_dquot
[cnt
], number
);
1424 dquot_incr_space(inode
->i_dquot
[cnt
], number
);
1427 inode_add_bytes(inode
, number
);
1429 spin_unlock(&dq_data_lock
);
1430 flush_warnings(inode
->i_dquot
, warntype
);
1434 int dquot_alloc_space(struct inode
*inode
, qsize_t number
, int warn
)
1436 int cnt
, ret
= QUOTA_OK
;
1439 * First test before acquiring mutex - solves deadlocks when we
1440 * re-enter the quota code and are already holding the mutex
1442 if (IS_NOQUOTA(inode
)) {
1443 inode_add_bytes(inode
, number
);
1447 down_read(&sb_dqopt(inode
->i_sb
)->dqptr_sem
);
1448 if (IS_NOQUOTA(inode
)) {
1449 inode_add_bytes(inode
, number
);
1453 ret
= __dquot_alloc_space(inode
, number
, warn
, 0);
1454 if (ret
== NO_QUOTA
)
1457 /* Dirtify all the dquots - this can block when journalling */
1458 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++)
1459 if (inode
->i_dquot
[cnt
])
1460 mark_dquot_dirty(inode
->i_dquot
[cnt
]);
1462 up_read(&sb_dqopt(inode
->i_sb
)->dqptr_sem
);
1466 EXPORT_SYMBOL(dquot_alloc_space
);
1468 int dquot_reserve_space(struct inode
*inode
, qsize_t number
, int warn
)
1472 if (IS_NOQUOTA(inode
))
1475 down_read(&sb_dqopt(inode
->i_sb
)->dqptr_sem
);
1476 if (IS_NOQUOTA(inode
))
1479 ret
= __dquot_alloc_space(inode
, number
, warn
, 1);
1481 up_read(&sb_dqopt(inode
->i_sb
)->dqptr_sem
);
1485 EXPORT_SYMBOL(dquot_reserve_space
);
1488 * This operation can block, but only after everything is updated
1490 int dquot_alloc_inode(const struct inode
*inode
, qsize_t number
)
1492 int cnt
, ret
= NO_QUOTA
;
1493 char warntype
[MAXQUOTAS
];
1495 /* First test before acquiring mutex - solves deadlocks when we
1496 * re-enter the quota code and are already holding the mutex */
1497 if (IS_NOQUOTA(inode
))
1499 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++)
1500 warntype
[cnt
] = QUOTA_NL_NOWARN
;
1501 down_read(&sb_dqopt(inode
->i_sb
)->dqptr_sem
);
1502 if (IS_NOQUOTA(inode
)) {
1503 up_read(&sb_dqopt(inode
->i_sb
)->dqptr_sem
);
1506 spin_lock(&dq_data_lock
);
1507 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++) {
1508 if (!inode
->i_dquot
[cnt
])
1510 if (check_idq(inode
->i_dquot
[cnt
], number
, warntype
+cnt
)
1515 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++) {
1516 if (!inode
->i_dquot
[cnt
])
1518 dquot_incr_inodes(inode
->i_dquot
[cnt
], number
);
1522 spin_unlock(&dq_data_lock
);
1523 if (ret
== QUOTA_OK
)
1524 /* Dirtify all the dquots - this can block when journalling */
1525 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++)
1526 if (inode
->i_dquot
[cnt
])
1527 mark_dquot_dirty(inode
->i_dquot
[cnt
]);
1528 flush_warnings(inode
->i_dquot
, warntype
);
1529 up_read(&sb_dqopt(inode
->i_sb
)->dqptr_sem
);
1532 EXPORT_SYMBOL(dquot_alloc_inode
);
1534 int dquot_claim_space(struct inode
*inode
, qsize_t number
)
1539 if (IS_NOQUOTA(inode
)) {
1540 inode_add_bytes(inode
, number
);
1544 down_read(&sb_dqopt(inode
->i_sb
)->dqptr_sem
);
1545 if (IS_NOQUOTA(inode
)) {
1546 up_read(&sb_dqopt(inode
->i_sb
)->dqptr_sem
);
1547 inode_add_bytes(inode
, number
);
1551 spin_lock(&dq_data_lock
);
1552 /* Claim reserved quotas to allocated quotas */
1553 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++) {
1554 if (inode
->i_dquot
[cnt
])
1555 dquot_claim_reserved_space(inode
->i_dquot
[cnt
],
1558 /* Update inode bytes */
1559 inode_add_bytes(inode
, number
);
1560 spin_unlock(&dq_data_lock
);
1561 /* Dirtify all the dquots - this can block when journalling */
1562 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++)
1563 if (inode
->i_dquot
[cnt
])
1564 mark_dquot_dirty(inode
->i_dquot
[cnt
]);
1565 up_read(&sb_dqopt(inode
->i_sb
)->dqptr_sem
);
1569 EXPORT_SYMBOL(dquot_claim_space
);
1572 * Release reserved quota space
1574 void dquot_release_reserved_space(struct inode
*inode
, qsize_t number
)
1578 if (IS_NOQUOTA(inode
))
1581 down_read(&sb_dqopt(inode
->i_sb
)->dqptr_sem
);
1582 if (IS_NOQUOTA(inode
))
1585 spin_lock(&dq_data_lock
);
1586 /* Release reserved dquots */
1587 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++) {
1588 if (inode
->i_dquot
[cnt
])
1589 dquot_free_reserved_space(inode
->i_dquot
[cnt
], number
);
1591 spin_unlock(&dq_data_lock
);
1594 up_read(&sb_dqopt(inode
->i_sb
)->dqptr_sem
);
1598 EXPORT_SYMBOL(dquot_release_reserved_space
);
1601 * This operation can block, but only after everything is updated
1603 int dquot_free_space(struct inode
*inode
, qsize_t number
)
1606 char warntype
[MAXQUOTAS
];
1608 /* First test before acquiring mutex - solves deadlocks when we
1609 * re-enter the quota code and are already holding the mutex */
1610 if (IS_NOQUOTA(inode
)) {
1612 inode_sub_bytes(inode
, number
);
1616 down_read(&sb_dqopt(inode
->i_sb
)->dqptr_sem
);
1617 /* Now recheck reliably when holding dqptr_sem */
1618 if (IS_NOQUOTA(inode
)) {
1619 up_read(&sb_dqopt(inode
->i_sb
)->dqptr_sem
);
1622 spin_lock(&dq_data_lock
);
1623 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++) {
1624 if (!inode
->i_dquot
[cnt
])
1626 warntype
[cnt
] = info_bdq_free(inode
->i_dquot
[cnt
], number
);
1627 dquot_decr_space(inode
->i_dquot
[cnt
], number
);
1629 inode_sub_bytes(inode
, number
);
1630 spin_unlock(&dq_data_lock
);
1631 /* Dirtify all the dquots - this can block when journalling */
1632 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++)
1633 if (inode
->i_dquot
[cnt
])
1634 mark_dquot_dirty(inode
->i_dquot
[cnt
]);
1635 flush_warnings(inode
->i_dquot
, warntype
);
1636 up_read(&sb_dqopt(inode
->i_sb
)->dqptr_sem
);
1639 EXPORT_SYMBOL(dquot_free_space
);
1642 * This operation can block, but only after everything is updated
1644 int dquot_free_inode(const struct inode
*inode
, qsize_t number
)
1647 char warntype
[MAXQUOTAS
];
1649 /* First test before acquiring mutex - solves deadlocks when we
1650 * re-enter the quota code and are already holding the mutex */
1651 if (IS_NOQUOTA(inode
))
1654 down_read(&sb_dqopt(inode
->i_sb
)->dqptr_sem
);
1655 /* Now recheck reliably when holding dqptr_sem */
1656 if (IS_NOQUOTA(inode
)) {
1657 up_read(&sb_dqopt(inode
->i_sb
)->dqptr_sem
);
1660 spin_lock(&dq_data_lock
);
1661 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++) {
1662 if (!inode
->i_dquot
[cnt
])
1664 warntype
[cnt
] = info_idq_free(inode
->i_dquot
[cnt
], number
);
1665 dquot_decr_inodes(inode
->i_dquot
[cnt
], number
);
1667 spin_unlock(&dq_data_lock
);
1668 /* Dirtify all the dquots - this can block when journalling */
1669 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++)
1670 if (inode
->i_dquot
[cnt
])
1671 mark_dquot_dirty(inode
->i_dquot
[cnt
]);
1672 flush_warnings(inode
->i_dquot
, warntype
);
1673 up_read(&sb_dqopt(inode
->i_sb
)->dqptr_sem
);
1676 EXPORT_SYMBOL(dquot_free_inode
);
1679 * call back function, get reserved quota space from underlying fs
1681 qsize_t
dquot_get_reserved_space(struct inode
*inode
)
1683 qsize_t reserved_space
= 0;
1685 if (sb_any_quota_active(inode
->i_sb
) &&
1686 inode
->i_sb
->dq_op
->get_reserved_space
)
1687 reserved_space
= inode
->i_sb
->dq_op
->get_reserved_space(inode
);
1688 return reserved_space
;
1692 * Transfer the number of inode and blocks from one diskquota to an other.
1694 * This operation can block, but only after everything is updated
1695 * A transaction must be started when entering this function.
1697 int dquot_transfer(struct inode
*inode
, struct iattr
*iattr
)
1699 qsize_t space
, cur_space
;
1700 qsize_t rsv_space
= 0;
1701 struct dquot
*transfer_from
[MAXQUOTAS
];
1702 struct dquot
*transfer_to
[MAXQUOTAS
];
1703 int cnt
, ret
= QUOTA_OK
;
1704 int chuid
= iattr
->ia_valid
& ATTR_UID
&& inode
->i_uid
!= iattr
->ia_uid
,
1705 chgid
= iattr
->ia_valid
& ATTR_GID
&& inode
->i_gid
!= iattr
->ia_gid
;
1706 char warntype_to
[MAXQUOTAS
];
1707 char warntype_from_inodes
[MAXQUOTAS
], warntype_from_space
[MAXQUOTAS
];
1709 /* First test before acquiring mutex - solves deadlocks when we
1710 * re-enter the quota code and are already holding the mutex */
1711 if (IS_NOQUOTA(inode
))
1713 /* Initialize the arrays */
1714 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++) {
1715 transfer_from
[cnt
] = NULL
;
1716 transfer_to
[cnt
] = NULL
;
1717 warntype_to
[cnt
] = QUOTA_NL_NOWARN
;
1720 transfer_to
[USRQUOTA
] = dqget(inode
->i_sb
, iattr
->ia_uid
,
1723 transfer_to
[GRPQUOTA
] = dqget(inode
->i_sb
, iattr
->ia_gid
,
1726 down_write(&sb_dqopt(inode
->i_sb
)->dqptr_sem
);
1727 /* Now recheck reliably when holding dqptr_sem */
1728 if (IS_NOQUOTA(inode
)) { /* File without quota accounting? */
1729 up_write(&sb_dqopt(inode
->i_sb
)->dqptr_sem
);
1732 spin_lock(&dq_data_lock
);
1733 cur_space
= inode_get_bytes(inode
);
1734 rsv_space
= dquot_get_reserved_space(inode
);
1735 space
= cur_space
+ rsv_space
;
1736 /* Build the transfer_from list and check the limits */
1737 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++) {
1738 if (!transfer_to
[cnt
])
1740 transfer_from
[cnt
] = inode
->i_dquot
[cnt
];
1741 if (check_idq(transfer_to
[cnt
], 1, warntype_to
+ cnt
) ==
1742 NO_QUOTA
|| check_bdq(transfer_to
[cnt
], space
, 0,
1743 warntype_to
+ cnt
) == NO_QUOTA
)
1748 * Finally perform the needed transfer from transfer_from to transfer_to
1750 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++) {
1752 * Skip changes for same uid or gid or for turned off quota-type.
1754 if (!transfer_to
[cnt
])
1757 /* Due to IO error we might not have transfer_from[] structure */
1758 if (transfer_from
[cnt
]) {
1759 warntype_from_inodes
[cnt
] =
1760 info_idq_free(transfer_from
[cnt
], 1);
1761 warntype_from_space
[cnt
] =
1762 info_bdq_free(transfer_from
[cnt
], space
);
1763 dquot_decr_inodes(transfer_from
[cnt
], 1);
1764 dquot_decr_space(transfer_from
[cnt
], cur_space
);
1765 dquot_free_reserved_space(transfer_from
[cnt
],
1769 dquot_incr_inodes(transfer_to
[cnt
], 1);
1770 dquot_incr_space(transfer_to
[cnt
], cur_space
);
1771 dquot_resv_space(transfer_to
[cnt
], rsv_space
);
1773 inode
->i_dquot
[cnt
] = transfer_to
[cnt
];
1775 spin_unlock(&dq_data_lock
);
1776 up_write(&sb_dqopt(inode
->i_sb
)->dqptr_sem
);
1778 /* Dirtify all the dquots - this can block when journalling */
1779 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++) {
1780 if (transfer_from
[cnt
])
1781 mark_dquot_dirty(transfer_from
[cnt
]);
1782 if (transfer_to
[cnt
]) {
1783 mark_dquot_dirty(transfer_to
[cnt
]);
1784 /* The reference we got is transferred to the inode */
1785 transfer_to
[cnt
] = NULL
;
1789 flush_warnings(transfer_to
, warntype_to
);
1790 flush_warnings(transfer_from
, warntype_from_inodes
);
1791 flush_warnings(transfer_from
, warntype_from_space
);
1793 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++) {
1794 dqput(transfer_from
[cnt
]);
1795 dqput(transfer_to
[cnt
]);
1799 spin_unlock(&dq_data_lock
);
1800 up_write(&sb_dqopt(inode
->i_sb
)->dqptr_sem
);
1801 /* Clear dquot pointers we don't want to dqput() */
1802 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++)
1803 transfer_from
[cnt
] = NULL
;
1807 EXPORT_SYMBOL(dquot_transfer
);
1809 /* Wrapper for transferring ownership of an inode */
1810 int vfs_dq_transfer(struct inode
*inode
, struct iattr
*iattr
)
1812 if (sb_any_quota_active(inode
->i_sb
) && !IS_NOQUOTA(inode
)) {
1814 if (inode
->i_sb
->dq_op
->transfer(inode
, iattr
) == NO_QUOTA
)
1819 EXPORT_SYMBOL(vfs_dq_transfer
);
1822 * Write info of quota file to disk
1824 int dquot_commit_info(struct super_block
*sb
, int type
)
1827 struct quota_info
*dqopt
= sb_dqopt(sb
);
1829 mutex_lock(&dqopt
->dqio_mutex
);
1830 ret
= dqopt
->ops
[type
]->write_file_info(sb
, type
);
1831 mutex_unlock(&dqopt
->dqio_mutex
);
1834 EXPORT_SYMBOL(dquot_commit_info
);
1837 * Definitions of diskquota operations.
1839 struct dquot_operations dquot_operations
= {
1840 .initialize
= dquot_initialize
,
1842 .alloc_space
= dquot_alloc_space
,
1843 .alloc_inode
= dquot_alloc_inode
,
1844 .free_space
= dquot_free_space
,
1845 .free_inode
= dquot_free_inode
,
1846 .transfer
= dquot_transfer
,
1847 .write_dquot
= dquot_commit
,
1848 .acquire_dquot
= dquot_acquire
,
1849 .release_dquot
= dquot_release
,
1850 .mark_dirty
= dquot_mark_dquot_dirty
,
1851 .write_info
= dquot_commit_info
,
1852 .alloc_dquot
= dquot_alloc
,
1853 .destroy_dquot
= dquot_destroy
,
1857 * Turn quota off on a device. type == -1 ==> quotaoff for all types (umount)
1859 int vfs_quota_disable(struct super_block
*sb
, int type
, unsigned int flags
)
1862 struct quota_info
*dqopt
= sb_dqopt(sb
);
1863 struct inode
*toputinode
[MAXQUOTAS
];
1865 /* Cannot turn off usage accounting without turning off limits, or
1866 * suspend quotas and simultaneously turn quotas off. */
1867 if ((flags
& DQUOT_USAGE_ENABLED
&& !(flags
& DQUOT_LIMITS_ENABLED
))
1868 || (flags
& DQUOT_SUSPENDED
&& flags
& (DQUOT_LIMITS_ENABLED
|
1869 DQUOT_USAGE_ENABLED
)))
1872 /* We need to serialize quota_off() for device */
1873 mutex_lock(&dqopt
->dqonoff_mutex
);
1876 * Skip everything if there's nothing to do. We have to do this because
1877 * sometimes we are called when fill_super() failed and calling
1878 * sync_fs() in such cases does no good.
1880 if (!sb_any_quota_loaded(sb
)) {
1881 mutex_unlock(&dqopt
->dqonoff_mutex
);
1884 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++) {
1885 toputinode
[cnt
] = NULL
;
1886 if (type
!= -1 && cnt
!= type
)
1888 if (!sb_has_quota_loaded(sb
, cnt
))
1891 if (flags
& DQUOT_SUSPENDED
) {
1892 spin_lock(&dq_state_lock
);
1894 dquot_state_flag(DQUOT_SUSPENDED
, cnt
);
1895 spin_unlock(&dq_state_lock
);
1897 spin_lock(&dq_state_lock
);
1898 dqopt
->flags
&= ~dquot_state_flag(flags
, cnt
);
1899 /* Turning off suspended quotas? */
1900 if (!sb_has_quota_loaded(sb
, cnt
) &&
1901 sb_has_quota_suspended(sb
, cnt
)) {
1902 dqopt
->flags
&= ~dquot_state_flag(
1903 DQUOT_SUSPENDED
, cnt
);
1904 spin_unlock(&dq_state_lock
);
1905 iput(dqopt
->files
[cnt
]);
1906 dqopt
->files
[cnt
] = NULL
;
1909 spin_unlock(&dq_state_lock
);
1912 /* We still have to keep quota loaded? */
1913 if (sb_has_quota_loaded(sb
, cnt
) && !(flags
& DQUOT_SUSPENDED
))
1916 /* Note: these are blocking operations */
1917 drop_dquot_ref(sb
, cnt
);
1918 invalidate_dquots(sb
, cnt
);
1920 * Now all dquots should be invalidated, all writes done so we
1921 * should be only users of the info. No locks needed.
1923 if (info_dirty(&dqopt
->info
[cnt
]))
1924 sb
->dq_op
->write_info(sb
, cnt
);
1925 if (dqopt
->ops
[cnt
]->free_file_info
)
1926 dqopt
->ops
[cnt
]->free_file_info(sb
, cnt
);
1927 put_quota_format(dqopt
->info
[cnt
].dqi_format
);
1929 toputinode
[cnt
] = dqopt
->files
[cnt
];
1930 if (!sb_has_quota_loaded(sb
, cnt
))
1931 dqopt
->files
[cnt
] = NULL
;
1932 dqopt
->info
[cnt
].dqi_flags
= 0;
1933 dqopt
->info
[cnt
].dqi_igrace
= 0;
1934 dqopt
->info
[cnt
].dqi_bgrace
= 0;
1935 dqopt
->ops
[cnt
] = NULL
;
1937 mutex_unlock(&dqopt
->dqonoff_mutex
);
1939 /* Skip syncing and setting flags if quota files are hidden */
1940 if (dqopt
->flags
& DQUOT_QUOTA_SYS_FILE
)
1943 /* Sync the superblock so that buffers with quota data are written to
1944 * disk (and so userspace sees correct data afterwards). */
1945 if (sb
->s_op
->sync_fs
)
1946 sb
->s_op
->sync_fs(sb
, 1);
1947 sync_blockdev(sb
->s_bdev
);
1948 /* Now the quota files are just ordinary files and we can set the
1949 * inode flags back. Moreover we discard the pagecache so that
1950 * userspace sees the writes we did bypassing the pagecache. We
1951 * must also discard the blockdev buffers so that we see the
1952 * changes done by userspace on the next quotaon() */
1953 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++)
1954 if (toputinode
[cnt
]) {
1955 mutex_lock(&dqopt
->dqonoff_mutex
);
1956 /* If quota was reenabled in the meantime, we have
1958 if (!sb_has_quota_loaded(sb
, cnt
)) {
1959 mutex_lock_nested(&toputinode
[cnt
]->i_mutex
,
1961 toputinode
[cnt
]->i_flags
&= ~(S_IMMUTABLE
|
1962 S_NOATIME
| S_NOQUOTA
);
1963 truncate_inode_pages(&toputinode
[cnt
]->i_data
,
1965 mutex_unlock(&toputinode
[cnt
]->i_mutex
);
1966 mark_inode_dirty(toputinode
[cnt
]);
1968 mutex_unlock(&dqopt
->dqonoff_mutex
);
1971 invalidate_bdev(sb
->s_bdev
);
1973 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++)
1974 if (toputinode
[cnt
]) {
1975 /* On remount RO, we keep the inode pointer so that we
1976 * can reenable quota on the subsequent remount RW. We
1977 * have to check 'flags' variable and not use sb_has_
1978 * function because another quotaon / quotaoff could
1979 * change global state before we got here. We refuse
1980 * to suspend quotas when there is pending delete on
1981 * the quota file... */
1982 if (!(flags
& DQUOT_SUSPENDED
))
1983 iput(toputinode
[cnt
]);
1984 else if (!toputinode
[cnt
]->i_nlink
)
1989 EXPORT_SYMBOL(vfs_quota_disable
);
1991 int vfs_quota_off(struct super_block
*sb
, int type
, int remount
)
1993 return vfs_quota_disable(sb
, type
, remount
? DQUOT_SUSPENDED
:
1994 (DQUOT_USAGE_ENABLED
| DQUOT_LIMITS_ENABLED
));
1996 EXPORT_SYMBOL(vfs_quota_off
);
1998 * Turn quotas on on a device
2002 * Helper function to turn quotas on when we already have the inode of
2003 * quota file and no quota information is loaded.
2005 static int vfs_load_quota_inode(struct inode
*inode
, int type
, int format_id
,
2008 struct quota_format_type
*fmt
= find_quota_format(format_id
);
2009 struct super_block
*sb
= inode
->i_sb
;
2010 struct quota_info
*dqopt
= sb_dqopt(sb
);
2016 if (!S_ISREG(inode
->i_mode
)) {
2020 if (IS_RDONLY(inode
)) {
2024 if (!sb
->s_op
->quota_write
|| !sb
->s_op
->quota_read
) {
2028 /* Usage always has to be set... */
2029 if (!(flags
& DQUOT_USAGE_ENABLED
)) {
2034 if (!(dqopt
->flags
& DQUOT_QUOTA_SYS_FILE
)) {
2035 /* As we bypass the pagecache we must now flush the inode so
2036 * that we see all the changes from userspace... */
2037 write_inode_now(inode
, 1);
2038 /* And now flush the block cache so that kernel sees the
2040 invalidate_bdev(sb
->s_bdev
);
2042 mutex_lock(&inode
->i_mutex
);
2043 mutex_lock(&dqopt
->dqonoff_mutex
);
2044 if (sb_has_quota_loaded(sb
, type
)) {
2049 if (!(dqopt
->flags
& DQUOT_QUOTA_SYS_FILE
)) {
2050 /* We don't want quota and atime on quota files (deadlocks
2051 * possible) Also nobody should write to the file - we use
2052 * special IO operations which ignore the immutable bit. */
2053 down_write(&dqopt
->dqptr_sem
);
2054 oldflags
= inode
->i_flags
& (S_NOATIME
| S_IMMUTABLE
|
2056 inode
->i_flags
|= S_NOQUOTA
| S_NOATIME
| S_IMMUTABLE
;
2057 up_write(&dqopt
->dqptr_sem
);
2058 sb
->dq_op
->drop(inode
);
2062 dqopt
->files
[type
] = igrab(inode
);
2063 if (!dqopt
->files
[type
])
2066 if (!fmt
->qf_ops
->check_quota_file(sb
, type
))
2069 dqopt
->ops
[type
] = fmt
->qf_ops
;
2070 dqopt
->info
[type
].dqi_format
= fmt
;
2071 dqopt
->info
[type
].dqi_fmt_id
= format_id
;
2072 INIT_LIST_HEAD(&dqopt
->info
[type
].dqi_dirty_list
);
2073 mutex_lock(&dqopt
->dqio_mutex
);
2074 error
= dqopt
->ops
[type
]->read_file_info(sb
, type
);
2076 mutex_unlock(&dqopt
->dqio_mutex
);
2079 mutex_unlock(&dqopt
->dqio_mutex
);
2080 mutex_unlock(&inode
->i_mutex
);
2081 spin_lock(&dq_state_lock
);
2082 dqopt
->flags
|= dquot_state_flag(flags
, type
);
2083 spin_unlock(&dq_state_lock
);
2085 add_dquot_ref(sb
, type
);
2086 mutex_unlock(&dqopt
->dqonoff_mutex
);
2091 dqopt
->files
[type
] = NULL
;
2094 mutex_unlock(&dqopt
->dqonoff_mutex
);
2095 if (oldflags
!= -1) {
2096 down_write(&dqopt
->dqptr_sem
);
2097 /* Set the flags back (in the case of accidental quotaon()
2098 * on a wrong file we don't want to mess up the flags) */
2099 inode
->i_flags
&= ~(S_NOATIME
| S_NOQUOTA
| S_IMMUTABLE
);
2100 inode
->i_flags
|= oldflags
;
2101 up_write(&dqopt
->dqptr_sem
);
2103 mutex_unlock(&inode
->i_mutex
);
2105 put_quota_format(fmt
);
2110 /* Reenable quotas on remount RW */
2111 static int vfs_quota_on_remount(struct super_block
*sb
, int type
)
2113 struct quota_info
*dqopt
= sb_dqopt(sb
);
2114 struct inode
*inode
;
2118 mutex_lock(&dqopt
->dqonoff_mutex
);
2119 if (!sb_has_quota_suspended(sb
, type
)) {
2120 mutex_unlock(&dqopt
->dqonoff_mutex
);
2123 inode
= dqopt
->files
[type
];
2124 dqopt
->files
[type
] = NULL
;
2125 spin_lock(&dq_state_lock
);
2126 flags
= dqopt
->flags
& dquot_state_flag(DQUOT_USAGE_ENABLED
|
2127 DQUOT_LIMITS_ENABLED
, type
);
2128 dqopt
->flags
&= ~dquot_state_flag(DQUOT_STATE_FLAGS
, type
);
2129 spin_unlock(&dq_state_lock
);
2130 mutex_unlock(&dqopt
->dqonoff_mutex
);
2132 flags
= dquot_generic_flag(flags
, type
);
2133 ret
= vfs_load_quota_inode(inode
, type
, dqopt
->info
[type
].dqi_fmt_id
,
2140 int vfs_quota_on_path(struct super_block
*sb
, int type
, int format_id
,
2143 int error
= security_quota_on(path
->dentry
);
2146 /* Quota file not on the same filesystem? */
2147 if (path
->mnt
->mnt_sb
!= sb
)
2150 error
= vfs_load_quota_inode(path
->dentry
->d_inode
, type
,
2151 format_id
, DQUOT_USAGE_ENABLED
|
2152 DQUOT_LIMITS_ENABLED
);
2155 EXPORT_SYMBOL(vfs_quota_on_path
);
2157 int vfs_quota_on(struct super_block
*sb
, int type
, int format_id
, char *name
,
2164 return vfs_quota_on_remount(sb
, type
);
2166 error
= kern_path(name
, LOOKUP_FOLLOW
, &path
);
2168 error
= vfs_quota_on_path(sb
, type
, format_id
, &path
);
2173 EXPORT_SYMBOL(vfs_quota_on
);
2176 * More powerful function for turning on quotas allowing setting
2177 * of individual quota flags
2179 int vfs_quota_enable(struct inode
*inode
, int type
, int format_id
,
2183 struct super_block
*sb
= inode
->i_sb
;
2184 struct quota_info
*dqopt
= sb_dqopt(sb
);
2186 /* Just unsuspend quotas? */
2187 if (flags
& DQUOT_SUSPENDED
)
2188 return vfs_quota_on_remount(sb
, type
);
2191 /* Just updating flags needed? */
2192 if (sb_has_quota_loaded(sb
, type
)) {
2193 mutex_lock(&dqopt
->dqonoff_mutex
);
2194 /* Now do a reliable test... */
2195 if (!sb_has_quota_loaded(sb
, type
)) {
2196 mutex_unlock(&dqopt
->dqonoff_mutex
);
2199 if (flags
& DQUOT_USAGE_ENABLED
&&
2200 sb_has_quota_usage_enabled(sb
, type
)) {
2204 if (flags
& DQUOT_LIMITS_ENABLED
&&
2205 sb_has_quota_limits_enabled(sb
, type
)) {
2209 spin_lock(&dq_state_lock
);
2210 sb_dqopt(sb
)->flags
|= dquot_state_flag(flags
, type
);
2211 spin_unlock(&dq_state_lock
);
2213 mutex_unlock(&dqopt
->dqonoff_mutex
);
2218 return vfs_load_quota_inode(inode
, type
, format_id
, flags
);
2220 EXPORT_SYMBOL(vfs_quota_enable
);
2223 * This function is used when filesystem needs to initialize quotas
2224 * during mount time.
2226 int vfs_quota_on_mount(struct super_block
*sb
, char *qf_name
,
2227 int format_id
, int type
)
2229 struct dentry
*dentry
;
2232 dentry
= lookup_one_len(qf_name
, sb
->s_root
, strlen(qf_name
));
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(vfs_quota_on_mount
);
2252 /* Wrapper to turn on quotas when remounting rw */
2253 int vfs_dq_quota_on_remount(struct super_block
*sb
)
2258 if (!sb
->s_qcop
|| !sb
->s_qcop
->quota_on
)
2260 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++) {
2261 err
= sb
->s_qcop
->quota_on(sb
, cnt
, 0, NULL
, 1);
2262 if (err
< 0 && !ret
)
2267 EXPORT_SYMBOL(vfs_dq_quota_on_remount
);
2269 static inline qsize_t
qbtos(qsize_t blocks
)
2271 return blocks
<< QIF_DQBLKSIZE_BITS
;
2274 static inline qsize_t
stoqb(qsize_t space
)
2276 return (space
+ QIF_DQBLKSIZE
- 1) >> QIF_DQBLKSIZE_BITS
;
2279 /* Generic routine for getting common part of quota structure */
2280 static void do_get_dqblk(struct dquot
*dquot
, struct if_dqblk
*di
)
2282 struct mem_dqblk
*dm
= &dquot
->dq_dqb
;
2284 spin_lock(&dq_data_lock
);
2285 di
->dqb_bhardlimit
= stoqb(dm
->dqb_bhardlimit
);
2286 di
->dqb_bsoftlimit
= stoqb(dm
->dqb_bsoftlimit
);
2287 di
->dqb_curspace
= dm
->dqb_curspace
+ dm
->dqb_rsvspace
;
2288 di
->dqb_ihardlimit
= dm
->dqb_ihardlimit
;
2289 di
->dqb_isoftlimit
= dm
->dqb_isoftlimit
;
2290 di
->dqb_curinodes
= dm
->dqb_curinodes
;
2291 di
->dqb_btime
= dm
->dqb_btime
;
2292 di
->dqb_itime
= dm
->dqb_itime
;
2293 di
->dqb_valid
= QIF_ALL
;
2294 spin_unlock(&dq_data_lock
);
2297 int vfs_get_dqblk(struct super_block
*sb
, int type
, qid_t id
,
2298 struct if_dqblk
*di
)
2300 struct dquot
*dquot
;
2302 dquot
= dqget(sb
, id
, type
);
2305 do_get_dqblk(dquot
, di
);
2310 EXPORT_SYMBOL(vfs_get_dqblk
);
2312 /* Generic routine for setting common part of quota structure */
2313 static int do_set_dqblk(struct dquot
*dquot
, struct if_dqblk
*di
)
2315 struct mem_dqblk
*dm
= &dquot
->dq_dqb
;
2316 int check_blim
= 0, check_ilim
= 0;
2317 struct mem_dqinfo
*dqi
= &sb_dqopt(dquot
->dq_sb
)->info
[dquot
->dq_type
];
2319 if ((di
->dqb_valid
& QIF_BLIMITS
&&
2320 (di
->dqb_bhardlimit
> dqi
->dqi_maxblimit
||
2321 di
->dqb_bsoftlimit
> dqi
->dqi_maxblimit
)) ||
2322 (di
->dqb_valid
& QIF_ILIMITS
&&
2323 (di
->dqb_ihardlimit
> dqi
->dqi_maxilimit
||
2324 di
->dqb_isoftlimit
> dqi
->dqi_maxilimit
)))
2327 spin_lock(&dq_data_lock
);
2328 if (di
->dqb_valid
& QIF_SPACE
) {
2329 dm
->dqb_curspace
= di
->dqb_curspace
- dm
->dqb_rsvspace
;
2331 __set_bit(DQ_LASTSET_B
+ QIF_SPACE_B
, &dquot
->dq_flags
);
2333 if (di
->dqb_valid
& QIF_BLIMITS
) {
2334 dm
->dqb_bsoftlimit
= qbtos(di
->dqb_bsoftlimit
);
2335 dm
->dqb_bhardlimit
= qbtos(di
->dqb_bhardlimit
);
2337 __set_bit(DQ_LASTSET_B
+ QIF_BLIMITS_B
, &dquot
->dq_flags
);
2339 if (di
->dqb_valid
& QIF_INODES
) {
2340 dm
->dqb_curinodes
= di
->dqb_curinodes
;
2342 __set_bit(DQ_LASTSET_B
+ QIF_INODES_B
, &dquot
->dq_flags
);
2344 if (di
->dqb_valid
& QIF_ILIMITS
) {
2345 dm
->dqb_isoftlimit
= di
->dqb_isoftlimit
;
2346 dm
->dqb_ihardlimit
= di
->dqb_ihardlimit
;
2348 __set_bit(DQ_LASTSET_B
+ QIF_ILIMITS_B
, &dquot
->dq_flags
);
2350 if (di
->dqb_valid
& QIF_BTIME
) {
2351 dm
->dqb_btime
= di
->dqb_btime
;
2353 __set_bit(DQ_LASTSET_B
+ QIF_BTIME_B
, &dquot
->dq_flags
);
2355 if (di
->dqb_valid
& QIF_ITIME
) {
2356 dm
->dqb_itime
= di
->dqb_itime
;
2358 __set_bit(DQ_LASTSET_B
+ QIF_ITIME_B
, &dquot
->dq_flags
);
2362 if (!dm
->dqb_bsoftlimit
||
2363 dm
->dqb_curspace
< dm
->dqb_bsoftlimit
) {
2365 clear_bit(DQ_BLKS_B
, &dquot
->dq_flags
);
2366 } else if (!(di
->dqb_valid
& QIF_BTIME
))
2367 /* Set grace only if user hasn't provided his own... */
2368 dm
->dqb_btime
= get_seconds() + dqi
->dqi_bgrace
;
2371 if (!dm
->dqb_isoftlimit
||
2372 dm
->dqb_curinodes
< dm
->dqb_isoftlimit
) {
2374 clear_bit(DQ_INODES_B
, &dquot
->dq_flags
);
2375 } else if (!(di
->dqb_valid
& QIF_ITIME
))
2376 /* Set grace only if user hasn't provided his own... */
2377 dm
->dqb_itime
= get_seconds() + dqi
->dqi_igrace
;
2379 if (dm
->dqb_bhardlimit
|| dm
->dqb_bsoftlimit
|| dm
->dqb_ihardlimit
||
2381 clear_bit(DQ_FAKE_B
, &dquot
->dq_flags
);
2383 set_bit(DQ_FAKE_B
, &dquot
->dq_flags
);
2384 spin_unlock(&dq_data_lock
);
2385 mark_dquot_dirty(dquot
);
2390 int vfs_set_dqblk(struct super_block
*sb
, int type
, qid_t id
,
2391 struct if_dqblk
*di
)
2393 struct dquot
*dquot
;
2396 dquot
= dqget(sb
, id
, type
);
2401 rc
= do_set_dqblk(dquot
, di
);
2406 EXPORT_SYMBOL(vfs_set_dqblk
);
2408 /* Generic routine for getting common part of quota file information */
2409 int vfs_get_dqinfo(struct super_block
*sb
, int type
, struct if_dqinfo
*ii
)
2411 struct mem_dqinfo
*mi
;
2413 mutex_lock(&sb_dqopt(sb
)->dqonoff_mutex
);
2414 if (!sb_has_quota_active(sb
, type
)) {
2415 mutex_unlock(&sb_dqopt(sb
)->dqonoff_mutex
);
2418 mi
= sb_dqopt(sb
)->info
+ type
;
2419 spin_lock(&dq_data_lock
);
2420 ii
->dqi_bgrace
= mi
->dqi_bgrace
;
2421 ii
->dqi_igrace
= mi
->dqi_igrace
;
2422 ii
->dqi_flags
= mi
->dqi_flags
& DQF_MASK
;
2423 ii
->dqi_valid
= IIF_ALL
;
2424 spin_unlock(&dq_data_lock
);
2425 mutex_unlock(&sb_dqopt(sb
)->dqonoff_mutex
);
2428 EXPORT_SYMBOL(vfs_get_dqinfo
);
2430 /* Generic routine for setting common part of quota file information */
2431 int vfs_set_dqinfo(struct super_block
*sb
, int type
, struct if_dqinfo
*ii
)
2433 struct mem_dqinfo
*mi
;
2436 mutex_lock(&sb_dqopt(sb
)->dqonoff_mutex
);
2437 if (!sb_has_quota_active(sb
, type
)) {
2441 mi
= sb_dqopt(sb
)->info
+ type
;
2442 spin_lock(&dq_data_lock
);
2443 if (ii
->dqi_valid
& IIF_BGRACE
)
2444 mi
->dqi_bgrace
= ii
->dqi_bgrace
;
2445 if (ii
->dqi_valid
& IIF_IGRACE
)
2446 mi
->dqi_igrace
= ii
->dqi_igrace
;
2447 if (ii
->dqi_valid
& IIF_FLAGS
)
2448 mi
->dqi_flags
= (mi
->dqi_flags
& ~DQF_MASK
) |
2449 (ii
->dqi_flags
& DQF_MASK
);
2450 spin_unlock(&dq_data_lock
);
2451 mark_info_dirty(sb
, type
);
2452 /* Force write to disk */
2453 sb
->dq_op
->write_info(sb
, type
);
2455 mutex_unlock(&sb_dqopt(sb
)->dqonoff_mutex
);
2458 EXPORT_SYMBOL(vfs_set_dqinfo
);
2460 struct quotactl_ops vfs_quotactl_ops
= {
2461 .quota_on
= vfs_quota_on
,
2462 .quota_off
= vfs_quota_off
,
2463 .quota_sync
= vfs_quota_sync
,
2464 .get_info
= vfs_get_dqinfo
,
2465 .set_info
= vfs_set_dqinfo
,
2466 .get_dqblk
= vfs_get_dqblk
,
2467 .set_dqblk
= vfs_set_dqblk
2470 static ctl_table fs_dqstats_table
[] = {
2472 .ctl_name
= FS_DQ_LOOKUPS
,
2473 .procname
= "lookups",
2474 .data
= &dqstats
.lookups
,
2475 .maxlen
= sizeof(int),
2477 .proc_handler
= &proc_dointvec
,
2480 .ctl_name
= FS_DQ_DROPS
,
2481 .procname
= "drops",
2482 .data
= &dqstats
.drops
,
2483 .maxlen
= sizeof(int),
2485 .proc_handler
= &proc_dointvec
,
2488 .ctl_name
= FS_DQ_READS
,
2489 .procname
= "reads",
2490 .data
= &dqstats
.reads
,
2491 .maxlen
= sizeof(int),
2493 .proc_handler
= &proc_dointvec
,
2496 .ctl_name
= FS_DQ_WRITES
,
2497 .procname
= "writes",
2498 .data
= &dqstats
.writes
,
2499 .maxlen
= sizeof(int),
2501 .proc_handler
= &proc_dointvec
,
2504 .ctl_name
= FS_DQ_CACHE_HITS
,
2505 .procname
= "cache_hits",
2506 .data
= &dqstats
.cache_hits
,
2507 .maxlen
= sizeof(int),
2509 .proc_handler
= &proc_dointvec
,
2512 .ctl_name
= FS_DQ_ALLOCATED
,
2513 .procname
= "allocated_dquots",
2514 .data
= &dqstats
.allocated_dquots
,
2515 .maxlen
= sizeof(int),
2517 .proc_handler
= &proc_dointvec
,
2520 .ctl_name
= FS_DQ_FREE
,
2521 .procname
= "free_dquots",
2522 .data
= &dqstats
.free_dquots
,
2523 .maxlen
= sizeof(int),
2525 .proc_handler
= &proc_dointvec
,
2528 .ctl_name
= FS_DQ_SYNCS
,
2529 .procname
= "syncs",
2530 .data
= &dqstats
.syncs
,
2531 .maxlen
= sizeof(int),
2533 .proc_handler
= &proc_dointvec
,
2535 #ifdef CONFIG_PRINT_QUOTA_WARNING
2537 .ctl_name
= FS_DQ_WARNINGS
,
2538 .procname
= "warnings",
2539 .data
= &flag_print_warnings
,
2540 .maxlen
= sizeof(int),
2542 .proc_handler
= &proc_dointvec
,
2548 static ctl_table fs_table
[] = {
2550 .ctl_name
= FS_DQSTATS
,
2551 .procname
= "quota",
2553 .child
= fs_dqstats_table
,
2558 static ctl_table sys_table
[] = {
2568 static int __init
dquot_init(void)
2571 unsigned long nr_hash
, order
;
2573 printk(KERN_NOTICE
"VFS: Disk quotas %s\n", __DQUOT_VERSION__
);
2575 register_sysctl_table(sys_table
);
2577 dquot_cachep
= kmem_cache_create("dquot",
2578 sizeof(struct dquot
), sizeof(unsigned long) * 4,
2579 (SLAB_HWCACHE_ALIGN
|SLAB_RECLAIM_ACCOUNT
|
2580 SLAB_MEM_SPREAD
|SLAB_PANIC
),
2584 dquot_hash
= (struct hlist_head
*)__get_free_pages(GFP_ATOMIC
, order
);
2586 panic("Cannot create dquot hash table");
2588 /* Find power-of-two hlist_heads which can fit into allocation */
2589 nr_hash
= (1UL << order
) * PAGE_SIZE
/ sizeof(struct hlist_head
);
2593 } while (nr_hash
>> dq_hash_bits
);
2596 nr_hash
= 1UL << dq_hash_bits
;
2597 dq_hash_mask
= nr_hash
- 1;
2598 for (i
= 0; i
< nr_hash
; i
++)
2599 INIT_HLIST_HEAD(dquot_hash
+ i
);
2601 printk("Dquot-cache hash table entries: %ld (order %ld, %ld bytes)\n",
2602 nr_hash
, order
, (PAGE_SIZE
<< order
));
2604 register_shrinker(&dqcache_shrinker
);
2606 #ifdef CONFIG_QUOTA_NETLINK_INTERFACE
2607 if (genl_register_family("a_genl_family
) != 0)
2609 "VFS: Failed to create quota netlink interface.\n");
2614 module_init(dquot_init
);