2 * Implementation of the diskquota system for the LINUX operating system. QUOTA
3 * is implemented using the BSD system call interface as the means of
4 * communication with the user level. This file contains the generic routines
5 * called by the different filesystems on allocation of an inode or block.
6 * These routines take care of the administration needed to have a consistent
7 * diskquota tracking system. The ideas of both user and group quotas are based
8 * on the Melbourne quota system as used on BSD derived systems. The internal
9 * implementation is based on one of the several variants of the LINUX
10 * inode-subsystem with added complexity of the diskquota system.
12 * Author: Marco van Wieringen <mvw@planets.elm.net>
14 * Fixes: Dmitry Gorodchanin <pgmdsg@ibi.com>, 11 Feb 96
16 * Revised list management to avoid races
17 * -- Bill Hawes, <whawes@star.net>, 9/98
19 * Fixed races in dquot_transfer(), dqget() and dquot_alloc_...().
20 * As the consequence the locking was moved from dquot_decr_...(),
21 * dquot_incr_...() to calling functions.
22 * invalidate_dquots() now writes modified dquots.
23 * Serialized quota_off() and quota_on() for mount point.
24 * Fixed a few bugs in grow_dquots().
25 * Fixed deadlock in write_dquot() - we no longer account quotas on
27 * remove_dquot_ref() moved to inode.c - it now traverses through inodes
28 * add_dquot_ref() restarts after blocking
29 * Added check for bogus uid and fixed check for group in quotactl.
30 * Jan Kara, <jack@suse.cz>, sponsored by SuSE CR, 10-11/99
32 * Used struct list_head instead of own list struct
33 * Invalidation of referenced dquots is no longer possible
34 * Improved free_dquots list management
35 * Quota and i_blocks are now updated in one place to avoid races
36 * Warnings are now delayed so we won't block in critical section
37 * Write updated not to require dquot lock
38 * Jan Kara, <jack@suse.cz>, 9/2000
40 * Added dynamic quota structure allocation
41 * Jan Kara <jack@suse.cz> 12/2000
43 * Rewritten quota interface. Implemented new quota format and
44 * formats registering.
45 * Jan Kara, <jack@suse.cz>, 2001,2002
48 * Jan Kara, <jack@suse.cz>, 10/2002
50 * Added journalled quota support, fix lock inversion problems
51 * Jan Kara, <jack@suse.cz>, 2003,2004
53 * (C) Copyright 1994 - 1997 Marco van Wieringen
56 #include <linux/errno.h>
57 #include <linux/kernel.h>
59 #include <linux/mount.h>
61 #include <linux/time.h>
62 #include <linux/types.h>
63 #include <linux/string.h>
64 #include <linux/fcntl.h>
65 #include <linux/stat.h>
66 #include <linux/tty.h>
67 #include <linux/file.h>
68 #include <linux/slab.h>
69 #include <linux/sysctl.h>
70 #include <linux/init.h>
71 #include <linux/module.h>
72 #include <linux/proc_fs.h>
73 #include <linux/security.h>
74 #include <linux/kmod.h>
75 #include <linux/namei.h>
76 #include <linux/buffer_head.h>
77 #include <linux/capability.h>
78 #include <linux/quotaops.h>
79 #include <linux/writeback.h> /* for inode_lock, oddly enough.. */
81 #include <asm/uaccess.h>
83 #define __DQUOT_PARANOIA
86 * There are three quota SMP locks. dq_list_lock protects all lists with quotas
87 * and quota formats, dqstats structure containing statistics about the lists
88 * dq_data_lock protects data from dq_dqb and also mem_dqinfo structures and
89 * also guards consistency of dquot->dq_dqb with inode->i_blocks, i_bytes.
90 * i_blocks and i_bytes updates itself are guarded by i_lock acquired directly
91 * in inode_add_bytes() and inode_sub_bytes(). dq_state_lock protects
92 * modifications of quota state (on quotaon and quotaoff) and readers who care
93 * about latest values take it as well.
95 * The spinlock ordering is hence: dq_data_lock > dq_list_lock > i_lock,
96 * dq_list_lock > dq_state_lock
98 * Note that some things (eg. sb pointer, type, id) doesn't change during
99 * the life of the dquot structure and so needn't to be protected by a lock
101 * Any operation working on dquots via inode pointers must hold dqptr_sem. If
102 * operation is just reading pointers from inode (or not using them at all) the
103 * read lock is enough. If pointers are altered function must hold write lock.
104 * Special care needs to be taken about S_NOQUOTA inode flag (marking that
105 * inode is a quota file). Functions adding pointers from inode to dquots have
106 * to check this flag under dqptr_sem and then (if S_NOQUOTA is not set) they
107 * have to do all pointer modifications before dropping dqptr_sem. This makes
108 * sure they cannot race with quotaon which first sets S_NOQUOTA flag and
109 * then drops all pointers to dquots from an inode.
111 * Each dquot has its dq_lock mutex. Locked dquots might not be referenced
112 * from inodes (dquot_alloc_space() and such don't check the dq_lock).
113 * Currently dquot is locked only when it is being read to memory (or space for
114 * it is being allocated) on the first dqget() and when it is being released on
115 * the last dqput(). The allocation and release oparations are serialized by
116 * the dq_lock and by checking the use count in dquot_release(). Write
117 * operations on dquots don't hold dq_lock as they copy data under dq_data_lock
118 * spinlock to internal buffers before writing.
120 * Lock ordering (including related VFS locks) is the following:
121 * i_mutex > dqonoff_sem > journal_lock > dqptr_sem > dquot->dq_lock >
123 * The lock ordering of dqptr_sem imposed by quota code is only dqonoff_sem >
124 * dqptr_sem. But filesystem has to count with the fact that functions such as
125 * dquot_alloc_space() acquire dqptr_sem and they usually have to be called
126 * from inside a transaction to keep filesystem consistency after a crash. Also
127 * filesystems usually want to do some IO on dquot from ->mark_dirty which is
128 * called with dqptr_sem held.
129 * i_mutex on quota files is special (it's below dqio_mutex)
132 static __cacheline_aligned_in_smp
DEFINE_SPINLOCK(dq_list_lock
);
133 static __cacheline_aligned_in_smp
DEFINE_SPINLOCK(dq_state_lock
);
134 __cacheline_aligned_in_smp
DEFINE_SPINLOCK(dq_data_lock
);
135 EXPORT_SYMBOL(dq_data_lock
);
137 static char *quotatypes
[] = INITQFNAMES
;
138 static struct quota_format_type
*quota_formats
; /* List of registered formats */
139 static struct quota_module_name module_names
[] = INIT_QUOTA_MODULE_NAMES
;
141 /* SLAB cache for dquot structures */
142 static struct kmem_cache
*dquot_cachep
;
144 int register_quota_format(struct quota_format_type
*fmt
)
146 spin_lock(&dq_list_lock
);
147 fmt
->qf_next
= quota_formats
;
149 spin_unlock(&dq_list_lock
);
152 EXPORT_SYMBOL(register_quota_format
);
154 void unregister_quota_format(struct quota_format_type
*fmt
)
156 struct quota_format_type
**actqf
;
158 spin_lock(&dq_list_lock
);
159 for (actqf
= "a_formats
; *actqf
&& *actqf
!= fmt
;
160 actqf
= &(*actqf
)->qf_next
)
163 *actqf
= (*actqf
)->qf_next
;
164 spin_unlock(&dq_list_lock
);
166 EXPORT_SYMBOL(unregister_quota_format
);
168 static struct quota_format_type
*find_quota_format(int id
)
170 struct quota_format_type
*actqf
;
172 spin_lock(&dq_list_lock
);
173 for (actqf
= quota_formats
; actqf
&& actqf
->qf_fmt_id
!= id
;
174 actqf
= actqf
->qf_next
)
176 if (!actqf
|| !try_module_get(actqf
->qf_owner
)) {
179 spin_unlock(&dq_list_lock
);
181 for (qm
= 0; module_names
[qm
].qm_fmt_id
&&
182 module_names
[qm
].qm_fmt_id
!= id
; qm
++)
184 if (!module_names
[qm
].qm_fmt_id
||
185 request_module(module_names
[qm
].qm_mod_name
))
188 spin_lock(&dq_list_lock
);
189 for (actqf
= quota_formats
; actqf
&& actqf
->qf_fmt_id
!= id
;
190 actqf
= actqf
->qf_next
)
192 if (actqf
&& !try_module_get(actqf
->qf_owner
))
195 spin_unlock(&dq_list_lock
);
199 static void put_quota_format(struct quota_format_type
*fmt
)
201 module_put(fmt
->qf_owner
);
205 * Dquot List Management:
206 * The quota code uses three lists for dquot management: the inuse_list,
207 * free_dquots, and dquot_hash[] array. A single dquot structure may be
208 * on all three lists, depending on its current state.
210 * All dquots are placed to the end of inuse_list when first created, and this
211 * list is used for invalidate operation, which must look at every dquot.
213 * Unused dquots (dq_count == 0) are added to the free_dquots list when freed,
214 * and this list is searched whenever we need an available dquot. Dquots are
215 * removed from the list as soon as they are used again, and
216 * dqstats.free_dquots gives the number of dquots on the list. When
217 * dquot is invalidated it's completely released from memory.
219 * Dquots with a specific identity (device, type and id) are placed on
220 * one of the dquot_hash[] hash chains. The provides an efficient search
221 * mechanism to locate a specific dquot.
224 static LIST_HEAD(inuse_list
);
225 static LIST_HEAD(free_dquots
);
226 static unsigned int dq_hash_bits
, dq_hash_mask
;
227 static struct hlist_head
*dquot_hash
;
229 struct dqstats dqstats
;
230 EXPORT_SYMBOL(dqstats
);
232 static qsize_t
inode_get_rsv_space(struct inode
*inode
);
233 static void __dquot_initialize(struct inode
*inode
, int type
);
235 static inline unsigned int
236 hashfn(const struct super_block
*sb
, unsigned int id
, int type
)
240 tmp
= (((unsigned long)sb
>>L1_CACHE_SHIFT
) ^ id
) * (MAXQUOTAS
- type
);
241 return (tmp
+ (tmp
>> dq_hash_bits
)) & dq_hash_mask
;
245 * Following list functions expect dq_list_lock to be held
247 static inline void insert_dquot_hash(struct dquot
*dquot
)
249 struct hlist_head
*head
;
250 head
= dquot_hash
+ hashfn(dquot
->dq_sb
, dquot
->dq_id
, dquot
->dq_type
);
251 hlist_add_head(&dquot
->dq_hash
, head
);
254 static inline void remove_dquot_hash(struct dquot
*dquot
)
256 hlist_del_init(&dquot
->dq_hash
);
259 static struct dquot
*find_dquot(unsigned int hashent
, struct super_block
*sb
,
260 unsigned int id
, int type
)
262 struct hlist_node
*node
;
265 hlist_for_each (node
, dquot_hash
+hashent
) {
266 dquot
= hlist_entry(node
, struct dquot
, dq_hash
);
267 if (dquot
->dq_sb
== sb
&& dquot
->dq_id
== id
&&
268 dquot
->dq_type
== type
)
274 /* Add a dquot to the tail of the free list */
275 static inline void put_dquot_last(struct dquot
*dquot
)
277 list_add_tail(&dquot
->dq_free
, &free_dquots
);
278 dqstats
.free_dquots
++;
281 static inline void remove_free_dquot(struct dquot
*dquot
)
283 if (list_empty(&dquot
->dq_free
))
285 list_del_init(&dquot
->dq_free
);
286 dqstats
.free_dquots
--;
289 static inline void put_inuse(struct dquot
*dquot
)
291 /* We add to the back of inuse list so we don't have to restart
292 * when traversing this list and we block */
293 list_add_tail(&dquot
->dq_inuse
, &inuse_list
);
294 dqstats
.allocated_dquots
++;
297 static inline void remove_inuse(struct dquot
*dquot
)
299 dqstats
.allocated_dquots
--;
300 list_del(&dquot
->dq_inuse
);
303 * End of list functions needing dq_list_lock
306 static void wait_on_dquot(struct dquot
*dquot
)
308 mutex_lock(&dquot
->dq_lock
);
309 mutex_unlock(&dquot
->dq_lock
);
312 static inline int dquot_dirty(struct dquot
*dquot
)
314 return test_bit(DQ_MOD_B
, &dquot
->dq_flags
);
317 static inline int mark_dquot_dirty(struct dquot
*dquot
)
319 return dquot
->dq_sb
->dq_op
->mark_dirty(dquot
);
322 int dquot_mark_dquot_dirty(struct dquot
*dquot
)
324 spin_lock(&dq_list_lock
);
325 if (!test_and_set_bit(DQ_MOD_B
, &dquot
->dq_flags
))
326 list_add(&dquot
->dq_dirty
, &sb_dqopt(dquot
->dq_sb
)->
327 info
[dquot
->dq_type
].dqi_dirty_list
);
328 spin_unlock(&dq_list_lock
);
331 EXPORT_SYMBOL(dquot_mark_dquot_dirty
);
333 /* Dirtify all the dquots - this can block when journalling */
334 static inline int mark_all_dquot_dirty(struct dquot
* const *dquot
)
339 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++) {
341 /* Even in case of error we have to continue */
342 ret
= mark_dquot_dirty(dquot
[cnt
]);
349 static inline void dqput_all(struct dquot
**dquot
)
353 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++)
357 /* This function needs dq_list_lock */
358 static inline int clear_dquot_dirty(struct dquot
*dquot
)
360 if (!test_and_clear_bit(DQ_MOD_B
, &dquot
->dq_flags
))
362 list_del_init(&dquot
->dq_dirty
);
366 void mark_info_dirty(struct super_block
*sb
, int type
)
368 set_bit(DQF_INFO_DIRTY_B
, &sb_dqopt(sb
)->info
[type
].dqi_flags
);
370 EXPORT_SYMBOL(mark_info_dirty
);
373 * Read dquot from disk and alloc space for it
376 int dquot_acquire(struct dquot
*dquot
)
378 int ret
= 0, ret2
= 0;
379 struct quota_info
*dqopt
= sb_dqopt(dquot
->dq_sb
);
381 mutex_lock(&dquot
->dq_lock
);
382 mutex_lock(&dqopt
->dqio_mutex
);
383 if (!test_bit(DQ_READ_B
, &dquot
->dq_flags
))
384 ret
= dqopt
->ops
[dquot
->dq_type
]->read_dqblk(dquot
);
387 set_bit(DQ_READ_B
, &dquot
->dq_flags
);
388 /* Instantiate dquot if needed */
389 if (!test_bit(DQ_ACTIVE_B
, &dquot
->dq_flags
) && !dquot
->dq_off
) {
390 ret
= dqopt
->ops
[dquot
->dq_type
]->commit_dqblk(dquot
);
391 /* Write the info if needed */
392 if (info_dirty(&dqopt
->info
[dquot
->dq_type
])) {
393 ret2
= dqopt
->ops
[dquot
->dq_type
]->write_file_info(
394 dquot
->dq_sb
, dquot
->dq_type
);
403 set_bit(DQ_ACTIVE_B
, &dquot
->dq_flags
);
405 mutex_unlock(&dqopt
->dqio_mutex
);
406 mutex_unlock(&dquot
->dq_lock
);
409 EXPORT_SYMBOL(dquot_acquire
);
412 * Write dquot to disk
414 int dquot_commit(struct dquot
*dquot
)
416 int ret
= 0, ret2
= 0;
417 struct quota_info
*dqopt
= sb_dqopt(dquot
->dq_sb
);
419 mutex_lock(&dqopt
->dqio_mutex
);
420 spin_lock(&dq_list_lock
);
421 if (!clear_dquot_dirty(dquot
)) {
422 spin_unlock(&dq_list_lock
);
425 spin_unlock(&dq_list_lock
);
426 /* Inactive dquot can be only if there was error during read/init
427 * => we have better not writing it */
428 if (test_bit(DQ_ACTIVE_B
, &dquot
->dq_flags
)) {
429 ret
= dqopt
->ops
[dquot
->dq_type
]->commit_dqblk(dquot
);
430 if (info_dirty(&dqopt
->info
[dquot
->dq_type
])) {
431 ret2
= dqopt
->ops
[dquot
->dq_type
]->write_file_info(
432 dquot
->dq_sb
, dquot
->dq_type
);
438 mutex_unlock(&dqopt
->dqio_mutex
);
441 EXPORT_SYMBOL(dquot_commit
);
446 int dquot_release(struct dquot
*dquot
)
448 int ret
= 0, ret2
= 0;
449 struct quota_info
*dqopt
= sb_dqopt(dquot
->dq_sb
);
451 mutex_lock(&dquot
->dq_lock
);
452 /* Check whether we are not racing with some other dqget() */
453 if (atomic_read(&dquot
->dq_count
) > 1)
455 mutex_lock(&dqopt
->dqio_mutex
);
456 if (dqopt
->ops
[dquot
->dq_type
]->release_dqblk
) {
457 ret
= dqopt
->ops
[dquot
->dq_type
]->release_dqblk(dquot
);
459 if (info_dirty(&dqopt
->info
[dquot
->dq_type
])) {
460 ret2
= dqopt
->ops
[dquot
->dq_type
]->write_file_info(
461 dquot
->dq_sb
, dquot
->dq_type
);
466 clear_bit(DQ_ACTIVE_B
, &dquot
->dq_flags
);
467 mutex_unlock(&dqopt
->dqio_mutex
);
469 mutex_unlock(&dquot
->dq_lock
);
472 EXPORT_SYMBOL(dquot_release
);
474 void dquot_destroy(struct dquot
*dquot
)
476 kmem_cache_free(dquot_cachep
, dquot
);
478 EXPORT_SYMBOL(dquot_destroy
);
480 static inline void do_destroy_dquot(struct dquot
*dquot
)
482 dquot
->dq_sb
->dq_op
->destroy_dquot(dquot
);
485 /* Invalidate all dquots on the list. Note that this function is called after
486 * quota is disabled and pointers from inodes removed so there cannot be new
487 * quota users. There can still be some users of quotas due to inodes being
488 * just deleted or pruned by prune_icache() (those are not attached to any
489 * list) or parallel quotactl call. We have to wait for such users.
491 static void invalidate_dquots(struct super_block
*sb
, int type
)
493 struct dquot
*dquot
, *tmp
;
496 spin_lock(&dq_list_lock
);
497 list_for_each_entry_safe(dquot
, tmp
, &inuse_list
, dq_inuse
) {
498 if (dquot
->dq_sb
!= sb
)
500 if (dquot
->dq_type
!= type
)
502 /* Wait for dquot users */
503 if (atomic_read(&dquot
->dq_count
)) {
506 atomic_inc(&dquot
->dq_count
);
507 prepare_to_wait(&dquot
->dq_wait_unused
, &wait
,
508 TASK_UNINTERRUPTIBLE
);
509 spin_unlock(&dq_list_lock
);
510 /* Once dqput() wakes us up, we know it's time to free
512 * IMPORTANT: we rely on the fact that there is always
513 * at most one process waiting for dquot to free.
514 * Otherwise dq_count would be > 1 and we would never
517 if (atomic_read(&dquot
->dq_count
) > 1)
519 finish_wait(&dquot
->dq_wait_unused
, &wait
);
521 /* At this moment dquot() need not exist (it could be
522 * reclaimed by prune_dqcache(). Hence we must
527 * Quota now has no users and it has been written on last
530 remove_dquot_hash(dquot
);
531 remove_free_dquot(dquot
);
533 do_destroy_dquot(dquot
);
535 spin_unlock(&dq_list_lock
);
538 /* Call callback for every active dquot on given filesystem */
539 int dquot_scan_active(struct super_block
*sb
,
540 int (*fn
)(struct dquot
*dquot
, unsigned long priv
),
543 struct dquot
*dquot
, *old_dquot
= NULL
;
546 mutex_lock(&sb_dqopt(sb
)->dqonoff_mutex
);
547 spin_lock(&dq_list_lock
);
548 list_for_each_entry(dquot
, &inuse_list
, dq_inuse
) {
549 if (!test_bit(DQ_ACTIVE_B
, &dquot
->dq_flags
))
551 if (dquot
->dq_sb
!= sb
)
553 /* Now we have active dquot so we can just increase use count */
554 atomic_inc(&dquot
->dq_count
);
556 spin_unlock(&dq_list_lock
);
559 ret
= fn(dquot
, priv
);
562 spin_lock(&dq_list_lock
);
563 /* We are safe to continue now because our dquot could not
564 * be moved out of the inuse list while we hold the reference */
566 spin_unlock(&dq_list_lock
);
569 mutex_unlock(&sb_dqopt(sb
)->dqonoff_mutex
);
572 EXPORT_SYMBOL(dquot_scan_active
);
574 int vfs_quota_sync(struct super_block
*sb
, int type
, int wait
)
576 struct list_head
*dirty
;
578 struct quota_info
*dqopt
= sb_dqopt(sb
);
581 mutex_lock(&dqopt
->dqonoff_mutex
);
582 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++) {
583 if (type
!= -1 && cnt
!= type
)
585 if (!sb_has_quota_active(sb
, cnt
))
587 spin_lock(&dq_list_lock
);
588 dirty
= &dqopt
->info
[cnt
].dqi_dirty_list
;
589 while (!list_empty(dirty
)) {
590 dquot
= list_first_entry(dirty
, struct dquot
,
592 /* Dirty and inactive can be only bad dquot... */
593 if (!test_bit(DQ_ACTIVE_B
, &dquot
->dq_flags
)) {
594 clear_dquot_dirty(dquot
);
597 /* Now we have active dquot from which someone is
598 * holding reference so we can safely just increase
600 atomic_inc(&dquot
->dq_count
);
602 spin_unlock(&dq_list_lock
);
603 sb
->dq_op
->write_dquot(dquot
);
605 spin_lock(&dq_list_lock
);
607 spin_unlock(&dq_list_lock
);
610 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++)
611 if ((cnt
== type
|| type
== -1) && sb_has_quota_active(sb
, cnt
)
612 && info_dirty(&dqopt
->info
[cnt
]))
613 sb
->dq_op
->write_info(sb
, cnt
);
614 spin_lock(&dq_list_lock
);
616 spin_unlock(&dq_list_lock
);
617 mutex_unlock(&dqopt
->dqonoff_mutex
);
619 if (!wait
|| (sb_dqopt(sb
)->flags
& DQUOT_QUOTA_SYS_FILE
))
622 /* This is not very clever (and fast) but currently I don't know about
623 * any other simple way of getting quota data to disk and we must get
624 * them there for userspace to be visible... */
625 if (sb
->s_op
->sync_fs
)
626 sb
->s_op
->sync_fs(sb
, 1);
627 sync_blockdev(sb
->s_bdev
);
630 * Now when everything is written we can discard the pagecache so
631 * that userspace sees the changes.
633 mutex_lock(&sb_dqopt(sb
)->dqonoff_mutex
);
634 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++) {
635 if (type
!= -1 && cnt
!= type
)
637 if (!sb_has_quota_active(sb
, cnt
))
639 mutex_lock_nested(&sb_dqopt(sb
)->files
[cnt
]->i_mutex
,
641 truncate_inode_pages(&sb_dqopt(sb
)->files
[cnt
]->i_data
, 0);
642 mutex_unlock(&sb_dqopt(sb
)->files
[cnt
]->i_mutex
);
644 mutex_unlock(&sb_dqopt(sb
)->dqonoff_mutex
);
648 EXPORT_SYMBOL(vfs_quota_sync
);
650 /* Free unused dquots from cache */
651 static void prune_dqcache(int count
)
653 struct list_head
*head
;
656 head
= free_dquots
.prev
;
657 while (head
!= &free_dquots
&& count
) {
658 dquot
= list_entry(head
, struct dquot
, dq_free
);
659 remove_dquot_hash(dquot
);
660 remove_free_dquot(dquot
);
662 do_destroy_dquot(dquot
);
664 head
= free_dquots
.prev
;
669 * This is called from kswapd when we think we need some
673 static int shrink_dqcache_memory(int nr
, gfp_t gfp_mask
)
676 spin_lock(&dq_list_lock
);
678 spin_unlock(&dq_list_lock
);
680 return (dqstats
.free_dquots
/ 100) * sysctl_vfs_cache_pressure
;
683 static struct shrinker dqcache_shrinker
= {
684 .shrink
= shrink_dqcache_memory
,
685 .seeks
= DEFAULT_SEEKS
,
689 * Put reference to dquot
690 * NOTE: If you change this function please check whether dqput_blocks() works right...
692 void dqput(struct dquot
*dquot
)
698 #ifdef __DQUOT_PARANOIA
699 if (!atomic_read(&dquot
->dq_count
)) {
700 printk("VFS: dqput: trying to free free dquot\n");
701 printk("VFS: device %s, dquot of %s %d\n",
703 quotatypes
[dquot
->dq_type
],
709 spin_lock(&dq_list_lock
);
711 spin_unlock(&dq_list_lock
);
713 spin_lock(&dq_list_lock
);
714 if (atomic_read(&dquot
->dq_count
) > 1) {
715 /* We have more than one user... nothing to do */
716 atomic_dec(&dquot
->dq_count
);
717 /* Releasing dquot during quotaoff phase? */
718 if (!sb_has_quota_active(dquot
->dq_sb
, dquot
->dq_type
) &&
719 atomic_read(&dquot
->dq_count
) == 1)
720 wake_up(&dquot
->dq_wait_unused
);
721 spin_unlock(&dq_list_lock
);
724 /* Need to release dquot? */
725 if (test_bit(DQ_ACTIVE_B
, &dquot
->dq_flags
) && dquot_dirty(dquot
)) {
726 spin_unlock(&dq_list_lock
);
727 /* Commit dquot before releasing */
728 ret
= dquot
->dq_sb
->dq_op
->write_dquot(dquot
);
730 printk(KERN_ERR
"VFS: cannot write quota structure on "
731 "device %s (error %d). Quota may get out of "
732 "sync!\n", dquot
->dq_sb
->s_id
, ret
);
734 * We clear dirty bit anyway, so that we avoid
737 spin_lock(&dq_list_lock
);
738 clear_dquot_dirty(dquot
);
739 spin_unlock(&dq_list_lock
);
743 /* Clear flag in case dquot was inactive (something bad happened) */
744 clear_dquot_dirty(dquot
);
745 if (test_bit(DQ_ACTIVE_B
, &dquot
->dq_flags
)) {
746 spin_unlock(&dq_list_lock
);
747 dquot
->dq_sb
->dq_op
->release_dquot(dquot
);
750 atomic_dec(&dquot
->dq_count
);
751 #ifdef __DQUOT_PARANOIA
753 BUG_ON(!list_empty(&dquot
->dq_free
));
755 put_dquot_last(dquot
);
756 spin_unlock(&dq_list_lock
);
758 EXPORT_SYMBOL(dqput
);
760 struct dquot
*dquot_alloc(struct super_block
*sb
, int type
)
762 return kmem_cache_zalloc(dquot_cachep
, GFP_NOFS
);
764 EXPORT_SYMBOL(dquot_alloc
);
766 static struct dquot
*get_empty_dquot(struct super_block
*sb
, int type
)
770 dquot
= sb
->dq_op
->alloc_dquot(sb
, type
);
774 mutex_init(&dquot
->dq_lock
);
775 INIT_LIST_HEAD(&dquot
->dq_free
);
776 INIT_LIST_HEAD(&dquot
->dq_inuse
);
777 INIT_HLIST_NODE(&dquot
->dq_hash
);
778 INIT_LIST_HEAD(&dquot
->dq_dirty
);
779 init_waitqueue_head(&dquot
->dq_wait_unused
);
781 dquot
->dq_type
= type
;
782 atomic_set(&dquot
->dq_count
, 1);
788 * Get reference to dquot
790 * Locking is slightly tricky here. We are guarded from parallel quotaoff()
791 * destroying our dquot by:
792 * a) checking for quota flags under dq_list_lock and
793 * b) getting a reference to dquot before we release dq_list_lock
795 struct dquot
*dqget(struct super_block
*sb
, unsigned int id
, int type
)
797 unsigned int hashent
= hashfn(sb
, id
, type
);
798 struct dquot
*dquot
= NULL
, *empty
= NULL
;
800 if (!sb_has_quota_active(sb
, type
))
803 spin_lock(&dq_list_lock
);
804 spin_lock(&dq_state_lock
);
805 if (!sb_has_quota_active(sb
, type
)) {
806 spin_unlock(&dq_state_lock
);
807 spin_unlock(&dq_list_lock
);
810 spin_unlock(&dq_state_lock
);
812 dquot
= find_dquot(hashent
, sb
, id
, type
);
815 spin_unlock(&dq_list_lock
);
816 empty
= get_empty_dquot(sb
, type
);
818 schedule(); /* Try to wait for a moment... */
824 /* all dquots go on the inuse_list */
826 /* hash it first so it can be found */
827 insert_dquot_hash(dquot
);
829 spin_unlock(&dq_list_lock
);
831 if (!atomic_read(&dquot
->dq_count
))
832 remove_free_dquot(dquot
);
833 atomic_inc(&dquot
->dq_count
);
834 dqstats
.cache_hits
++;
836 spin_unlock(&dq_list_lock
);
838 /* Wait for dq_lock - after this we know that either dquot_release() is
839 * already finished or it will be canceled due to dq_count > 1 test */
840 wait_on_dquot(dquot
);
841 /* Read the dquot / allocate space in quota file */
842 if (!test_bit(DQ_ACTIVE_B
, &dquot
->dq_flags
) &&
843 sb
->dq_op
->acquire_dquot(dquot
) < 0) {
848 #ifdef __DQUOT_PARANOIA
849 BUG_ON(!dquot
->dq_sb
); /* Has somebody invalidated entry under us? */
853 do_destroy_dquot(empty
);
857 EXPORT_SYMBOL(dqget
);
859 static int dqinit_needed(struct inode
*inode
, int type
)
863 if (IS_NOQUOTA(inode
))
866 return !inode
->i_dquot
[type
];
867 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++)
868 if (!inode
->i_dquot
[cnt
])
873 /* This routine is guarded by dqonoff_mutex mutex */
874 static void add_dquot_ref(struct super_block
*sb
, int type
)
876 struct inode
*inode
, *old_inode
= NULL
;
879 spin_lock(&inode_lock
);
880 list_for_each_entry(inode
, &sb
->s_inodes
, i_sb_list
) {
881 if (inode
->i_state
& (I_FREEING
|I_CLEAR
|I_WILL_FREE
|I_NEW
))
883 if (unlikely(inode_get_rsv_space(inode
) > 0))
885 if (!atomic_read(&inode
->i_writecount
))
887 if (!dqinit_needed(inode
, type
))
891 spin_unlock(&inode_lock
);
894 __dquot_initialize(inode
, type
);
895 /* We hold a reference to 'inode' so it couldn't have been
896 * removed from s_inodes list while we dropped the inode_lock.
897 * We cannot iput the inode now as we can be holding the last
898 * reference and we cannot iput it under inode_lock. So we
899 * keep the reference and iput it later. */
901 spin_lock(&inode_lock
);
903 spin_unlock(&inode_lock
);
907 printk(KERN_WARNING
"VFS (%s): Writes happened before quota"
908 " was turned on thus quota information is probably "
909 "inconsistent. Please run quotacheck(8).\n", sb
->s_id
);
914 * Return 0 if dqput() won't block.
915 * (note that 1 doesn't necessarily mean blocking)
917 static inline int dqput_blocks(struct dquot
*dquot
)
919 if (atomic_read(&dquot
->dq_count
) <= 1)
925 * Remove references to dquots from inode and add dquot to list for freeing
926 * if we have the last referece to dquot
927 * We can't race with anybody because we hold dqptr_sem for writing...
929 static int remove_inode_dquot_ref(struct inode
*inode
, int type
,
930 struct list_head
*tofree_head
)
932 struct dquot
*dquot
= inode
->i_dquot
[type
];
934 inode
->i_dquot
[type
] = NULL
;
936 if (dqput_blocks(dquot
)) {
937 #ifdef __DQUOT_PARANOIA
938 if (atomic_read(&dquot
->dq_count
) != 1)
939 printk(KERN_WARNING
"VFS: Adding dquot with dq_count %d to dispose list.\n", atomic_read(&dquot
->dq_count
));
941 spin_lock(&dq_list_lock
);
942 /* As dquot must have currently users it can't be on
943 * the free list... */
944 list_add(&dquot
->dq_free
, tofree_head
);
945 spin_unlock(&dq_list_lock
);
949 dqput(dquot
); /* We have guaranteed we won't block */
955 * Free list of dquots
956 * Dquots are removed from inodes and no new references can be got so we are
957 * the only ones holding reference
959 static void put_dquot_list(struct list_head
*tofree_head
)
961 struct list_head
*act_head
;
964 act_head
= tofree_head
->next
;
965 while (act_head
!= tofree_head
) {
966 dquot
= list_entry(act_head
, struct dquot
, dq_free
);
967 act_head
= act_head
->next
;
968 /* Remove dquot from the list so we won't have problems... */
969 list_del_init(&dquot
->dq_free
);
974 static void remove_dquot_ref(struct super_block
*sb
, int type
,
975 struct list_head
*tofree_head
)
979 spin_lock(&inode_lock
);
980 list_for_each_entry(inode
, &sb
->s_inodes
, i_sb_list
) {
982 * We have to scan also I_NEW inodes because they can already
983 * have quota pointer initialized. Luckily, we need to touch
984 * only quota pointers and these have separate locking
987 if (!IS_NOQUOTA(inode
))
988 remove_inode_dquot_ref(inode
, type
, tofree_head
);
990 spin_unlock(&inode_lock
);
993 /* Gather all references from inodes and drop them */
994 static void drop_dquot_ref(struct super_block
*sb
, int type
)
996 LIST_HEAD(tofree_head
);
999 down_write(&sb_dqopt(sb
)->dqptr_sem
);
1000 remove_dquot_ref(sb
, type
, &tofree_head
);
1001 up_write(&sb_dqopt(sb
)->dqptr_sem
);
1002 put_dquot_list(&tofree_head
);
1006 static inline void dquot_incr_inodes(struct dquot
*dquot
, qsize_t number
)
1008 dquot
->dq_dqb
.dqb_curinodes
+= number
;
1011 static inline void dquot_incr_space(struct dquot
*dquot
, qsize_t number
)
1013 dquot
->dq_dqb
.dqb_curspace
+= number
;
1016 static inline void dquot_resv_space(struct dquot
*dquot
, qsize_t number
)
1018 dquot
->dq_dqb
.dqb_rsvspace
+= number
;
1022 * Claim reserved quota space
1024 static void dquot_claim_reserved_space(struct dquot
*dquot
, qsize_t number
)
1026 if (dquot
->dq_dqb
.dqb_rsvspace
< number
) {
1028 number
= dquot
->dq_dqb
.dqb_rsvspace
;
1030 dquot
->dq_dqb
.dqb_curspace
+= number
;
1031 dquot
->dq_dqb
.dqb_rsvspace
-= number
;
1035 void dquot_free_reserved_space(struct dquot
*dquot
, qsize_t number
)
1037 if (dquot
->dq_dqb
.dqb_rsvspace
>= number
)
1038 dquot
->dq_dqb
.dqb_rsvspace
-= number
;
1041 dquot
->dq_dqb
.dqb_rsvspace
= 0;
1045 static void dquot_decr_inodes(struct dquot
*dquot
, qsize_t number
)
1047 if (sb_dqopt(dquot
->dq_sb
)->flags
& DQUOT_NEGATIVE_USAGE
||
1048 dquot
->dq_dqb
.dqb_curinodes
>= number
)
1049 dquot
->dq_dqb
.dqb_curinodes
-= number
;
1051 dquot
->dq_dqb
.dqb_curinodes
= 0;
1052 if (dquot
->dq_dqb
.dqb_curinodes
<= dquot
->dq_dqb
.dqb_isoftlimit
)
1053 dquot
->dq_dqb
.dqb_itime
= (time_t) 0;
1054 clear_bit(DQ_INODES_B
, &dquot
->dq_flags
);
1057 static void dquot_decr_space(struct dquot
*dquot
, qsize_t number
)
1059 if (sb_dqopt(dquot
->dq_sb
)->flags
& DQUOT_NEGATIVE_USAGE
||
1060 dquot
->dq_dqb
.dqb_curspace
>= number
)
1061 dquot
->dq_dqb
.dqb_curspace
-= number
;
1063 dquot
->dq_dqb
.dqb_curspace
= 0;
1064 if (dquot
->dq_dqb
.dqb_curspace
<= dquot
->dq_dqb
.dqb_bsoftlimit
)
1065 dquot
->dq_dqb
.dqb_btime
= (time_t) 0;
1066 clear_bit(DQ_BLKS_B
, &dquot
->dq_flags
);
1069 static int warning_issued(struct dquot
*dquot
, const int warntype
)
1071 int flag
= (warntype
== QUOTA_NL_BHARDWARN
||
1072 warntype
== QUOTA_NL_BSOFTLONGWARN
) ? DQ_BLKS_B
:
1073 ((warntype
== QUOTA_NL_IHARDWARN
||
1074 warntype
== QUOTA_NL_ISOFTLONGWARN
) ? DQ_INODES_B
: 0);
1078 return test_and_set_bit(flag
, &dquot
->dq_flags
);
1081 #ifdef CONFIG_PRINT_QUOTA_WARNING
1082 static int flag_print_warnings
= 1;
1084 static int need_print_warning(struct dquot
*dquot
)
1086 if (!flag_print_warnings
)
1089 switch (dquot
->dq_type
) {
1091 return current_fsuid() == dquot
->dq_id
;
1093 return in_group_p(dquot
->dq_id
);
1098 /* Print warning to user which exceeded quota */
1099 static void print_warning(struct dquot
*dquot
, const int warntype
)
1102 struct tty_struct
*tty
;
1104 if (warntype
== QUOTA_NL_IHARDBELOW
||
1105 warntype
== QUOTA_NL_ISOFTBELOW
||
1106 warntype
== QUOTA_NL_BHARDBELOW
||
1107 warntype
== QUOTA_NL_BSOFTBELOW
|| !need_print_warning(dquot
))
1110 tty
= get_current_tty();
1113 tty_write_message(tty
, dquot
->dq_sb
->s_id
);
1114 if (warntype
== QUOTA_NL_ISOFTWARN
|| warntype
== QUOTA_NL_BSOFTWARN
)
1115 tty_write_message(tty
, ": warning, ");
1117 tty_write_message(tty
, ": write failed, ");
1118 tty_write_message(tty
, quotatypes
[dquot
->dq_type
]);
1120 case QUOTA_NL_IHARDWARN
:
1121 msg
= " file limit reached.\r\n";
1123 case QUOTA_NL_ISOFTLONGWARN
:
1124 msg
= " file quota exceeded too long.\r\n";
1126 case QUOTA_NL_ISOFTWARN
:
1127 msg
= " file quota exceeded.\r\n";
1129 case QUOTA_NL_BHARDWARN
:
1130 msg
= " block limit reached.\r\n";
1132 case QUOTA_NL_BSOFTLONGWARN
:
1133 msg
= " block quota exceeded too long.\r\n";
1135 case QUOTA_NL_BSOFTWARN
:
1136 msg
= " block quota exceeded.\r\n";
1139 tty_write_message(tty
, msg
);
1145 * Write warnings to the console and send warning messages over netlink.
1147 * Note that this function can sleep.
1149 static void flush_warnings(struct dquot
*const *dquots
, char *warntype
)
1154 for (i
= 0; i
< MAXQUOTAS
; i
++) {
1156 if (dq
&& warntype
[i
] != QUOTA_NL_NOWARN
&&
1157 !warning_issued(dq
, warntype
[i
])) {
1158 #ifdef CONFIG_PRINT_QUOTA_WARNING
1159 print_warning(dq
, warntype
[i
]);
1161 quota_send_warning(dq
->dq_type
, dq
->dq_id
,
1162 dq
->dq_sb
->s_dev
, warntype
[i
]);
1167 static int ignore_hardlimit(struct dquot
*dquot
)
1169 struct mem_dqinfo
*info
= &sb_dqopt(dquot
->dq_sb
)->info
[dquot
->dq_type
];
1171 return capable(CAP_SYS_RESOURCE
) &&
1172 (info
->dqi_format
->qf_fmt_id
!= QFMT_VFS_OLD
||
1173 !(info
->dqi_flags
& V1_DQF_RSQUASH
));
1176 /* needs dq_data_lock */
1177 static int check_idq(struct dquot
*dquot
, qsize_t inodes
, char *warntype
)
1179 qsize_t newinodes
= dquot
->dq_dqb
.dqb_curinodes
+ inodes
;
1181 *warntype
= QUOTA_NL_NOWARN
;
1182 if (!sb_has_quota_limits_enabled(dquot
->dq_sb
, dquot
->dq_type
) ||
1183 test_bit(DQ_FAKE_B
, &dquot
->dq_flags
))
1186 if (dquot
->dq_dqb
.dqb_ihardlimit
&&
1187 newinodes
> dquot
->dq_dqb
.dqb_ihardlimit
&&
1188 !ignore_hardlimit(dquot
)) {
1189 *warntype
= QUOTA_NL_IHARDWARN
;
1193 if (dquot
->dq_dqb
.dqb_isoftlimit
&&
1194 newinodes
> dquot
->dq_dqb
.dqb_isoftlimit
&&
1195 dquot
->dq_dqb
.dqb_itime
&&
1196 get_seconds() >= dquot
->dq_dqb
.dqb_itime
&&
1197 !ignore_hardlimit(dquot
)) {
1198 *warntype
= QUOTA_NL_ISOFTLONGWARN
;
1202 if (dquot
->dq_dqb
.dqb_isoftlimit
&&
1203 newinodes
> dquot
->dq_dqb
.dqb_isoftlimit
&&
1204 dquot
->dq_dqb
.dqb_itime
== 0) {
1205 *warntype
= QUOTA_NL_ISOFTWARN
;
1206 dquot
->dq_dqb
.dqb_itime
= get_seconds() +
1207 sb_dqopt(dquot
->dq_sb
)->info
[dquot
->dq_type
].dqi_igrace
;
1213 /* needs dq_data_lock */
1214 static int check_bdq(struct dquot
*dquot
, qsize_t space
, int prealloc
, char *warntype
)
1217 struct super_block
*sb
= dquot
->dq_sb
;
1219 *warntype
= QUOTA_NL_NOWARN
;
1220 if (!sb_has_quota_limits_enabled(sb
, dquot
->dq_type
) ||
1221 test_bit(DQ_FAKE_B
, &dquot
->dq_flags
))
1224 tspace
= dquot
->dq_dqb
.dqb_curspace
+ dquot
->dq_dqb
.dqb_rsvspace
1227 if (dquot
->dq_dqb
.dqb_bhardlimit
&&
1228 tspace
> dquot
->dq_dqb
.dqb_bhardlimit
&&
1229 !ignore_hardlimit(dquot
)) {
1231 *warntype
= QUOTA_NL_BHARDWARN
;
1235 if (dquot
->dq_dqb
.dqb_bsoftlimit
&&
1236 tspace
> dquot
->dq_dqb
.dqb_bsoftlimit
&&
1237 dquot
->dq_dqb
.dqb_btime
&&
1238 get_seconds() >= dquot
->dq_dqb
.dqb_btime
&&
1239 !ignore_hardlimit(dquot
)) {
1241 *warntype
= QUOTA_NL_BSOFTLONGWARN
;
1245 if (dquot
->dq_dqb
.dqb_bsoftlimit
&&
1246 tspace
> dquot
->dq_dqb
.dqb_bsoftlimit
&&
1247 dquot
->dq_dqb
.dqb_btime
== 0) {
1249 *warntype
= QUOTA_NL_BSOFTWARN
;
1250 dquot
->dq_dqb
.dqb_btime
= get_seconds() +
1251 sb_dqopt(sb
)->info
[dquot
->dq_type
].dqi_bgrace
;
1255 * We don't allow preallocation to exceed softlimit so exceeding will
1264 static int info_idq_free(struct dquot
*dquot
, qsize_t inodes
)
1268 if (test_bit(DQ_FAKE_B
, &dquot
->dq_flags
) ||
1269 dquot
->dq_dqb
.dqb_curinodes
<= dquot
->dq_dqb
.dqb_isoftlimit
||
1270 !sb_has_quota_limits_enabled(dquot
->dq_sb
, dquot
->dq_type
))
1271 return QUOTA_NL_NOWARN
;
1273 newinodes
= dquot
->dq_dqb
.dqb_curinodes
- inodes
;
1274 if (newinodes
<= dquot
->dq_dqb
.dqb_isoftlimit
)
1275 return QUOTA_NL_ISOFTBELOW
;
1276 if (dquot
->dq_dqb
.dqb_curinodes
>= dquot
->dq_dqb
.dqb_ihardlimit
&&
1277 newinodes
< dquot
->dq_dqb
.dqb_ihardlimit
)
1278 return QUOTA_NL_IHARDBELOW
;
1279 return QUOTA_NL_NOWARN
;
1282 static int info_bdq_free(struct dquot
*dquot
, qsize_t space
)
1284 if (test_bit(DQ_FAKE_B
, &dquot
->dq_flags
) ||
1285 dquot
->dq_dqb
.dqb_curspace
<= dquot
->dq_dqb
.dqb_bsoftlimit
)
1286 return QUOTA_NL_NOWARN
;
1288 if (dquot
->dq_dqb
.dqb_curspace
- space
<= dquot
->dq_dqb
.dqb_bsoftlimit
)
1289 return QUOTA_NL_BSOFTBELOW
;
1290 if (dquot
->dq_dqb
.dqb_curspace
>= dquot
->dq_dqb
.dqb_bhardlimit
&&
1291 dquot
->dq_dqb
.dqb_curspace
- space
< dquot
->dq_dqb
.dqb_bhardlimit
)
1292 return QUOTA_NL_BHARDBELOW
;
1293 return QUOTA_NL_NOWARN
;
1297 * Initialize quota pointers in inode
1299 * We do things in a bit complicated way but by that we avoid calling
1300 * dqget() and thus filesystem callbacks under dqptr_sem.
1302 * It is better to call this function outside of any transaction as it
1303 * might need a lot of space in journal for dquot structure allocation.
1305 static void __dquot_initialize(struct inode
*inode
, int type
)
1307 unsigned int id
= 0;
1309 struct dquot
*got
[MAXQUOTAS
];
1310 struct super_block
*sb
= inode
->i_sb
;
1313 /* First test before acquiring mutex - solves deadlocks when we
1314 * re-enter the quota code and are already holding the mutex */
1315 if (!sb_any_quota_active(inode
->i_sb
) || IS_NOQUOTA(inode
))
1318 /* First get references to structures we might need. */
1319 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++) {
1321 if (type
!= -1 && cnt
!= type
)
1331 got
[cnt
] = dqget(sb
, id
, cnt
);
1334 down_write(&sb_dqopt(sb
)->dqptr_sem
);
1335 if (IS_NOQUOTA(inode
))
1337 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++) {
1338 if (type
!= -1 && cnt
!= type
)
1340 /* Avoid races with quotaoff() */
1341 if (!sb_has_quota_active(sb
, cnt
))
1343 if (!inode
->i_dquot
[cnt
]) {
1344 inode
->i_dquot
[cnt
] = got
[cnt
];
1347 * Make quota reservation system happy if someone
1348 * did a write before quota was turned on
1350 rsv
= inode_get_rsv_space(inode
);
1352 dquot_resv_space(inode
->i_dquot
[cnt
], rsv
);
1356 up_write(&sb_dqopt(sb
)->dqptr_sem
);
1357 /* Drop unused references */
1361 void dquot_initialize(struct inode
*inode
)
1363 __dquot_initialize(inode
, -1);
1365 EXPORT_SYMBOL(dquot_initialize
);
1368 * Release all quotas referenced by inode
1370 static void __dquot_drop(struct inode
*inode
)
1373 struct dquot
*put
[MAXQUOTAS
];
1375 down_write(&sb_dqopt(inode
->i_sb
)->dqptr_sem
);
1376 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++) {
1377 put
[cnt
] = inode
->i_dquot
[cnt
];
1378 inode
->i_dquot
[cnt
] = NULL
;
1380 up_write(&sb_dqopt(inode
->i_sb
)->dqptr_sem
);
1384 void dquot_drop(struct inode
*inode
)
1388 if (IS_NOQUOTA(inode
))
1392 * Test before calling to rule out calls from proc and such
1393 * where we are not allowed to block. Note that this is
1394 * actually reliable test even without the lock - the caller
1395 * must assure that nobody can come after the DQUOT_DROP and
1396 * add quota pointers back anyway.
1398 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++) {
1399 if (inode
->i_dquot
[cnt
])
1403 if (cnt
< MAXQUOTAS
)
1404 __dquot_drop(inode
);
1406 EXPORT_SYMBOL(dquot_drop
);
1409 * inode_reserved_space is managed internally by quota, and protected by
1410 * i_lock similar to i_blocks+i_bytes.
1412 static qsize_t
*inode_reserved_space(struct inode
* inode
)
1414 /* Filesystem must explicitly define it's own method in order to use
1415 * quota reservation interface */
1416 BUG_ON(!inode
->i_sb
->dq_op
->get_reserved_space
);
1417 return inode
->i_sb
->dq_op
->get_reserved_space(inode
);
1420 void inode_add_rsv_space(struct inode
*inode
, qsize_t number
)
1422 spin_lock(&inode
->i_lock
);
1423 *inode_reserved_space(inode
) += number
;
1424 spin_unlock(&inode
->i_lock
);
1426 EXPORT_SYMBOL(inode_add_rsv_space
);
1428 void inode_claim_rsv_space(struct inode
*inode
, qsize_t number
)
1430 spin_lock(&inode
->i_lock
);
1431 *inode_reserved_space(inode
) -= number
;
1432 __inode_add_bytes(inode
, number
);
1433 spin_unlock(&inode
->i_lock
);
1435 EXPORT_SYMBOL(inode_claim_rsv_space
);
1437 void inode_sub_rsv_space(struct inode
*inode
, qsize_t number
)
1439 spin_lock(&inode
->i_lock
);
1440 *inode_reserved_space(inode
) -= number
;
1441 spin_unlock(&inode
->i_lock
);
1443 EXPORT_SYMBOL(inode_sub_rsv_space
);
1445 static qsize_t
inode_get_rsv_space(struct inode
*inode
)
1449 if (!inode
->i_sb
->dq_op
->get_reserved_space
)
1451 spin_lock(&inode
->i_lock
);
1452 ret
= *inode_reserved_space(inode
);
1453 spin_unlock(&inode
->i_lock
);
1457 static void inode_incr_space(struct inode
*inode
, qsize_t number
,
1461 inode_add_rsv_space(inode
, number
);
1463 inode_add_bytes(inode
, number
);
1466 static void inode_decr_space(struct inode
*inode
, qsize_t number
, int reserve
)
1469 inode_sub_rsv_space(inode
, number
);
1471 inode_sub_bytes(inode
, number
);
1475 * This functions updates i_blocks+i_bytes fields and quota information
1476 * (together with appropriate checks).
1478 * NOTE: We absolutely rely on the fact that caller dirties the inode
1479 * (usually helpers in quotaops.h care about this) and holds a handle for
1480 * the current transaction so that dquot write and inode write go into the
1485 * This operation can block, but only after everything is updated
1487 int __dquot_alloc_space(struct inode
*inode
, qsize_t number
,
1488 int warn
, int reserve
)
1491 char warntype
[MAXQUOTAS
];
1494 * First test before acquiring mutex - solves deadlocks when we
1495 * re-enter the quota code and are already holding the mutex
1497 if (!sb_any_quota_active(inode
->i_sb
) || IS_NOQUOTA(inode
)) {
1498 inode_incr_space(inode
, number
, reserve
);
1502 down_read(&sb_dqopt(inode
->i_sb
)->dqptr_sem
);
1503 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++)
1504 warntype
[cnt
] = QUOTA_NL_NOWARN
;
1506 spin_lock(&dq_data_lock
);
1507 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++) {
1508 if (!inode
->i_dquot
[cnt
])
1510 ret
= check_bdq(inode
->i_dquot
[cnt
], number
, !warn
,
1513 spin_unlock(&dq_data_lock
);
1514 goto out_flush_warn
;
1517 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++) {
1518 if (!inode
->i_dquot
[cnt
])
1521 dquot_resv_space(inode
->i_dquot
[cnt
], number
);
1523 dquot_incr_space(inode
->i_dquot
[cnt
], number
);
1525 inode_incr_space(inode
, number
, reserve
);
1526 spin_unlock(&dq_data_lock
);
1529 goto out_flush_warn
;
1530 mark_all_dquot_dirty(inode
->i_dquot
);
1532 flush_warnings(inode
->i_dquot
, warntype
);
1533 up_read(&sb_dqopt(inode
->i_sb
)->dqptr_sem
);
1537 EXPORT_SYMBOL(__dquot_alloc_space
);
1540 * This operation can block, but only after everything is updated
1542 int dquot_alloc_inode(const struct inode
*inode
)
1545 char warntype
[MAXQUOTAS
];
1547 /* First test before acquiring mutex - solves deadlocks when we
1548 * re-enter the quota code and are already holding the mutex */
1549 if (!sb_any_quota_active(inode
->i_sb
) || IS_NOQUOTA(inode
))
1551 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++)
1552 warntype
[cnt
] = QUOTA_NL_NOWARN
;
1553 down_read(&sb_dqopt(inode
->i_sb
)->dqptr_sem
);
1554 spin_lock(&dq_data_lock
);
1555 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++) {
1556 if (!inode
->i_dquot
[cnt
])
1558 ret
= check_idq(inode
->i_dquot
[cnt
], 1, warntype
+ cnt
);
1563 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++) {
1564 if (!inode
->i_dquot
[cnt
])
1566 dquot_incr_inodes(inode
->i_dquot
[cnt
], 1);
1570 spin_unlock(&dq_data_lock
);
1572 mark_all_dquot_dirty(inode
->i_dquot
);
1573 flush_warnings(inode
->i_dquot
, warntype
);
1574 up_read(&sb_dqopt(inode
->i_sb
)->dqptr_sem
);
1577 EXPORT_SYMBOL(dquot_alloc_inode
);
1580 * Convert in-memory reserved quotas to real consumed quotas
1582 int dquot_claim_space_nodirty(struct inode
*inode
, qsize_t number
)
1586 if (!sb_any_quota_active(inode
->i_sb
) || IS_NOQUOTA(inode
)) {
1587 inode_claim_rsv_space(inode
, number
);
1591 down_read(&sb_dqopt(inode
->i_sb
)->dqptr_sem
);
1592 spin_lock(&dq_data_lock
);
1593 /* Claim reserved quotas to allocated quotas */
1594 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++) {
1595 if (inode
->i_dquot
[cnt
])
1596 dquot_claim_reserved_space(inode
->i_dquot
[cnt
],
1599 /* Update inode bytes */
1600 inode_claim_rsv_space(inode
, number
);
1601 spin_unlock(&dq_data_lock
);
1602 mark_all_dquot_dirty(inode
->i_dquot
);
1603 up_read(&sb_dqopt(inode
->i_sb
)->dqptr_sem
);
1606 EXPORT_SYMBOL(dquot_claim_space_nodirty
);
1609 * This operation can block, but only after everything is updated
1611 void __dquot_free_space(struct inode
*inode
, qsize_t number
, int reserve
)
1614 char warntype
[MAXQUOTAS
];
1616 /* First test before acquiring mutex - solves deadlocks when we
1617 * re-enter the quota code and are already holding the mutex */
1618 if (!sb_any_quota_active(inode
->i_sb
) || IS_NOQUOTA(inode
)) {
1619 inode_decr_space(inode
, number
, reserve
);
1623 down_read(&sb_dqopt(inode
->i_sb
)->dqptr_sem
);
1624 spin_lock(&dq_data_lock
);
1625 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++) {
1626 if (!inode
->i_dquot
[cnt
])
1628 warntype
[cnt
] = info_bdq_free(inode
->i_dquot
[cnt
], number
);
1630 dquot_free_reserved_space(inode
->i_dquot
[cnt
], number
);
1632 dquot_decr_space(inode
->i_dquot
[cnt
], number
);
1634 inode_decr_space(inode
, number
, reserve
);
1635 spin_unlock(&dq_data_lock
);
1639 mark_all_dquot_dirty(inode
->i_dquot
);
1641 flush_warnings(inode
->i_dquot
, warntype
);
1642 up_read(&sb_dqopt(inode
->i_sb
)->dqptr_sem
);
1644 EXPORT_SYMBOL(__dquot_free_space
);
1647 * This operation can block, but only after everything is updated
1649 void dquot_free_inode(const struct inode
*inode
)
1652 char warntype
[MAXQUOTAS
];
1654 /* First test before acquiring mutex - solves deadlocks when we
1655 * re-enter the quota code and are already holding the mutex */
1656 if (!sb_any_quota_active(inode
->i_sb
) || IS_NOQUOTA(inode
))
1659 down_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
], 1);
1665 dquot_decr_inodes(inode
->i_dquot
[cnt
], 1);
1667 spin_unlock(&dq_data_lock
);
1668 mark_all_dquot_dirty(inode
->i_dquot
);
1669 flush_warnings(inode
->i_dquot
, warntype
);
1670 up_read(&sb_dqopt(inode
->i_sb
)->dqptr_sem
);
1672 EXPORT_SYMBOL(dquot_free_inode
);
1675 * Transfer the number of inode and blocks from one diskquota to an other.
1677 * This operation can block, but only after everything is updated
1678 * A transaction must be started when entering this function.
1680 static int __dquot_transfer(struct inode
*inode
, qid_t
*chid
, unsigned long mask
)
1682 qsize_t space
, cur_space
;
1683 qsize_t rsv_space
= 0;
1684 struct dquot
*transfer_from
[MAXQUOTAS
];
1685 struct dquot
*transfer_to
[MAXQUOTAS
];
1687 char warntype_to
[MAXQUOTAS
];
1688 char warntype_from_inodes
[MAXQUOTAS
], warntype_from_space
[MAXQUOTAS
];
1690 /* First test before acquiring mutex - solves deadlocks when we
1691 * re-enter the quota code and are already holding the mutex */
1692 if (IS_NOQUOTA(inode
))
1694 /* Initialize the arrays */
1695 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++) {
1696 transfer_from
[cnt
] = NULL
;
1697 transfer_to
[cnt
] = NULL
;
1698 warntype_to
[cnt
] = QUOTA_NL_NOWARN
;
1700 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++) {
1701 if (mask
& (1 << cnt
))
1702 transfer_to
[cnt
] = dqget(inode
->i_sb
, chid
[cnt
], cnt
);
1704 down_write(&sb_dqopt(inode
->i_sb
)->dqptr_sem
);
1705 if (IS_NOQUOTA(inode
)) { /* File without quota accounting? */
1706 up_write(&sb_dqopt(inode
->i_sb
)->dqptr_sem
);
1709 spin_lock(&dq_data_lock
);
1710 cur_space
= inode_get_bytes(inode
);
1711 rsv_space
= inode_get_rsv_space(inode
);
1712 space
= cur_space
+ rsv_space
;
1713 /* Build the transfer_from list and check the limits */
1714 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++) {
1715 if (!transfer_to
[cnt
])
1717 transfer_from
[cnt
] = inode
->i_dquot
[cnt
];
1718 ret
= check_idq(transfer_to
[cnt
], 1, warntype_to
+ cnt
);
1721 ret
= check_bdq(transfer_to
[cnt
], space
, 0, warntype_to
+ cnt
);
1727 * Finally perform the needed transfer from transfer_from to transfer_to
1729 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++) {
1731 * Skip changes for same uid or gid or for turned off quota-type.
1733 if (!transfer_to
[cnt
])
1736 /* Due to IO error we might not have transfer_from[] structure */
1737 if (transfer_from
[cnt
]) {
1738 warntype_from_inodes
[cnt
] =
1739 info_idq_free(transfer_from
[cnt
], 1);
1740 warntype_from_space
[cnt
] =
1741 info_bdq_free(transfer_from
[cnt
], space
);
1742 dquot_decr_inodes(transfer_from
[cnt
], 1);
1743 dquot_decr_space(transfer_from
[cnt
], cur_space
);
1744 dquot_free_reserved_space(transfer_from
[cnt
],
1748 dquot_incr_inodes(transfer_to
[cnt
], 1);
1749 dquot_incr_space(transfer_to
[cnt
], cur_space
);
1750 dquot_resv_space(transfer_to
[cnt
], rsv_space
);
1752 inode
->i_dquot
[cnt
] = transfer_to
[cnt
];
1754 spin_unlock(&dq_data_lock
);
1755 up_write(&sb_dqopt(inode
->i_sb
)->dqptr_sem
);
1757 mark_all_dquot_dirty(transfer_from
);
1758 mark_all_dquot_dirty(transfer_to
);
1759 /* The reference we got is transferred to the inode */
1760 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++)
1761 transfer_to
[cnt
] = NULL
;
1763 flush_warnings(transfer_to
, warntype_to
);
1764 flush_warnings(transfer_from
, warntype_from_inodes
);
1765 flush_warnings(transfer_from
, warntype_from_space
);
1767 dqput_all(transfer_from
);
1768 dqput_all(transfer_to
);
1771 spin_unlock(&dq_data_lock
);
1772 up_write(&sb_dqopt(inode
->i_sb
)->dqptr_sem
);
1773 /* Clear dquot pointers we don't want to dqput() */
1774 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++)
1775 transfer_from
[cnt
] = NULL
;
1779 /* Wrapper for transferring ownership of an inode for uid/gid only
1780 * Called from FSXXX_setattr()
1782 int dquot_transfer(struct inode
*inode
, struct iattr
*iattr
)
1784 qid_t chid
[MAXQUOTAS
];
1785 unsigned long mask
= 0;
1787 if (iattr
->ia_valid
& ATTR_UID
&& iattr
->ia_uid
!= inode
->i_uid
) {
1788 mask
|= 1 << USRQUOTA
;
1789 chid
[USRQUOTA
] = iattr
->ia_uid
;
1791 if (iattr
->ia_valid
& ATTR_GID
&& iattr
->ia_gid
!= inode
->i_gid
) {
1792 mask
|= 1 << GRPQUOTA
;
1793 chid
[GRPQUOTA
] = iattr
->ia_gid
;
1795 if (sb_any_quota_active(inode
->i_sb
) && !IS_NOQUOTA(inode
)) {
1796 dquot_initialize(inode
);
1797 return __dquot_transfer(inode
, chid
, mask
);
1801 EXPORT_SYMBOL(dquot_transfer
);
1804 * Write info of quota file to disk
1806 int dquot_commit_info(struct super_block
*sb
, int type
)
1809 struct quota_info
*dqopt
= sb_dqopt(sb
);
1811 mutex_lock(&dqopt
->dqio_mutex
);
1812 ret
= dqopt
->ops
[type
]->write_file_info(sb
, type
);
1813 mutex_unlock(&dqopt
->dqio_mutex
);
1816 EXPORT_SYMBOL(dquot_commit_info
);
1819 * Definitions of diskquota operations.
1821 const struct dquot_operations dquot_operations
= {
1822 .write_dquot
= dquot_commit
,
1823 .acquire_dquot
= dquot_acquire
,
1824 .release_dquot
= dquot_release
,
1825 .mark_dirty
= dquot_mark_dquot_dirty
,
1826 .write_info
= dquot_commit_info
,
1827 .alloc_dquot
= dquot_alloc
,
1828 .destroy_dquot
= dquot_destroy
,
1832 * Generic helper for ->open on filesystems supporting disk quotas.
1834 int dquot_file_open(struct inode
*inode
, struct file
*file
)
1838 error
= generic_file_open(inode
, file
);
1839 if (!error
&& (file
->f_mode
& FMODE_WRITE
))
1840 dquot_initialize(inode
);
1843 EXPORT_SYMBOL(dquot_file_open
);
1846 * Turn quota off on a device. type == -1 ==> quotaoff for all types (umount)
1848 int vfs_quota_disable(struct super_block
*sb
, int type
, unsigned int flags
)
1851 struct quota_info
*dqopt
= sb_dqopt(sb
);
1852 struct inode
*toputinode
[MAXQUOTAS
];
1854 /* Cannot turn off usage accounting without turning off limits, or
1855 * suspend quotas and simultaneously turn quotas off. */
1856 if ((flags
& DQUOT_USAGE_ENABLED
&& !(flags
& DQUOT_LIMITS_ENABLED
))
1857 || (flags
& DQUOT_SUSPENDED
&& flags
& (DQUOT_LIMITS_ENABLED
|
1858 DQUOT_USAGE_ENABLED
)))
1861 /* We need to serialize quota_off() for device */
1862 mutex_lock(&dqopt
->dqonoff_mutex
);
1865 * Skip everything if there's nothing to do. We have to do this because
1866 * sometimes we are called when fill_super() failed and calling
1867 * sync_fs() in such cases does no good.
1869 if (!sb_any_quota_loaded(sb
)) {
1870 mutex_unlock(&dqopt
->dqonoff_mutex
);
1873 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++) {
1874 toputinode
[cnt
] = NULL
;
1875 if (type
!= -1 && cnt
!= type
)
1877 if (!sb_has_quota_loaded(sb
, cnt
))
1880 if (flags
& DQUOT_SUSPENDED
) {
1881 spin_lock(&dq_state_lock
);
1883 dquot_state_flag(DQUOT_SUSPENDED
, cnt
);
1884 spin_unlock(&dq_state_lock
);
1886 spin_lock(&dq_state_lock
);
1887 dqopt
->flags
&= ~dquot_state_flag(flags
, cnt
);
1888 /* Turning off suspended quotas? */
1889 if (!sb_has_quota_loaded(sb
, cnt
) &&
1890 sb_has_quota_suspended(sb
, cnt
)) {
1891 dqopt
->flags
&= ~dquot_state_flag(
1892 DQUOT_SUSPENDED
, cnt
);
1893 spin_unlock(&dq_state_lock
);
1894 iput(dqopt
->files
[cnt
]);
1895 dqopt
->files
[cnt
] = NULL
;
1898 spin_unlock(&dq_state_lock
);
1901 /* We still have to keep quota loaded? */
1902 if (sb_has_quota_loaded(sb
, cnt
) && !(flags
& DQUOT_SUSPENDED
))
1905 /* Note: these are blocking operations */
1906 drop_dquot_ref(sb
, cnt
);
1907 invalidate_dquots(sb
, cnt
);
1909 * Now all dquots should be invalidated, all writes done so we
1910 * should be only users of the info. No locks needed.
1912 if (info_dirty(&dqopt
->info
[cnt
]))
1913 sb
->dq_op
->write_info(sb
, cnt
);
1914 if (dqopt
->ops
[cnt
]->free_file_info
)
1915 dqopt
->ops
[cnt
]->free_file_info(sb
, cnt
);
1916 put_quota_format(dqopt
->info
[cnt
].dqi_format
);
1918 toputinode
[cnt
] = dqopt
->files
[cnt
];
1919 if (!sb_has_quota_loaded(sb
, cnt
))
1920 dqopt
->files
[cnt
] = NULL
;
1921 dqopt
->info
[cnt
].dqi_flags
= 0;
1922 dqopt
->info
[cnt
].dqi_igrace
= 0;
1923 dqopt
->info
[cnt
].dqi_bgrace
= 0;
1924 dqopt
->ops
[cnt
] = NULL
;
1926 mutex_unlock(&dqopt
->dqonoff_mutex
);
1928 /* Skip syncing and setting flags if quota files are hidden */
1929 if (dqopt
->flags
& DQUOT_QUOTA_SYS_FILE
)
1932 /* Sync the superblock so that buffers with quota data are written to
1933 * disk (and so userspace sees correct data afterwards). */
1934 if (sb
->s_op
->sync_fs
)
1935 sb
->s_op
->sync_fs(sb
, 1);
1936 sync_blockdev(sb
->s_bdev
);
1937 /* Now the quota files are just ordinary files and we can set the
1938 * inode flags back. Moreover we discard the pagecache so that
1939 * userspace sees the writes we did bypassing the pagecache. We
1940 * must also discard the blockdev buffers so that we see the
1941 * changes done by userspace on the next quotaon() */
1942 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++)
1943 if (toputinode
[cnt
]) {
1944 mutex_lock(&dqopt
->dqonoff_mutex
);
1945 /* If quota was reenabled in the meantime, we have
1947 if (!sb_has_quota_loaded(sb
, cnt
)) {
1948 mutex_lock_nested(&toputinode
[cnt
]->i_mutex
,
1950 toputinode
[cnt
]->i_flags
&= ~(S_IMMUTABLE
|
1951 S_NOATIME
| S_NOQUOTA
);
1952 truncate_inode_pages(&toputinode
[cnt
]->i_data
,
1954 mutex_unlock(&toputinode
[cnt
]->i_mutex
);
1955 mark_inode_dirty(toputinode
[cnt
]);
1957 mutex_unlock(&dqopt
->dqonoff_mutex
);
1960 invalidate_bdev(sb
->s_bdev
);
1962 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++)
1963 if (toputinode
[cnt
]) {
1964 /* On remount RO, we keep the inode pointer so that we
1965 * can reenable quota on the subsequent remount RW. We
1966 * have to check 'flags' variable and not use sb_has_
1967 * function because another quotaon / quotaoff could
1968 * change global state before we got here. We refuse
1969 * to suspend quotas when there is pending delete on
1970 * the quota file... */
1971 if (!(flags
& DQUOT_SUSPENDED
))
1972 iput(toputinode
[cnt
]);
1973 else if (!toputinode
[cnt
]->i_nlink
)
1978 EXPORT_SYMBOL(vfs_quota_disable
);
1980 int vfs_quota_off(struct super_block
*sb
, int type
, int remount
)
1982 return vfs_quota_disable(sb
, type
, remount
? DQUOT_SUSPENDED
:
1983 (DQUOT_USAGE_ENABLED
| DQUOT_LIMITS_ENABLED
));
1985 EXPORT_SYMBOL(vfs_quota_off
);
1987 * Turn quotas on on a device
1991 * Helper function to turn quotas on when we already have the inode of
1992 * quota file and no quota information is loaded.
1994 static int vfs_load_quota_inode(struct inode
*inode
, int type
, int format_id
,
1997 struct quota_format_type
*fmt
= find_quota_format(format_id
);
1998 struct super_block
*sb
= inode
->i_sb
;
1999 struct quota_info
*dqopt
= sb_dqopt(sb
);
2005 if (!S_ISREG(inode
->i_mode
)) {
2009 if (IS_RDONLY(inode
)) {
2013 if (!sb
->s_op
->quota_write
|| !sb
->s_op
->quota_read
) {
2017 /* Usage always has to be set... */
2018 if (!(flags
& DQUOT_USAGE_ENABLED
)) {
2023 if (!(dqopt
->flags
& DQUOT_QUOTA_SYS_FILE
)) {
2024 /* As we bypass the pagecache we must now flush all the
2025 * dirty data and invalidate caches so that kernel sees
2026 * changes from userspace. It is not enough to just flush
2027 * the quota file since if blocksize < pagesize, invalidation
2028 * of the cache could fail because of other unrelated dirty
2030 sync_filesystem(sb
);
2031 invalidate_bdev(sb
->s_bdev
);
2033 mutex_lock(&dqopt
->dqonoff_mutex
);
2034 if (sb_has_quota_loaded(sb
, type
)) {
2039 if (!(dqopt
->flags
& DQUOT_QUOTA_SYS_FILE
)) {
2040 /* We don't want quota and atime on quota files (deadlocks
2041 * possible) Also nobody should write to the file - we use
2042 * special IO operations which ignore the immutable bit. */
2043 mutex_lock_nested(&inode
->i_mutex
, I_MUTEX_QUOTA
);
2044 oldflags
= inode
->i_flags
& (S_NOATIME
| S_IMMUTABLE
|
2046 inode
->i_flags
|= S_NOQUOTA
| S_NOATIME
| S_IMMUTABLE
;
2047 mutex_unlock(&inode
->i_mutex
);
2049 * When S_NOQUOTA is set, remove dquot references as no more
2050 * references can be added
2052 __dquot_drop(inode
);
2056 dqopt
->files
[type
] = igrab(inode
);
2057 if (!dqopt
->files
[type
])
2060 if (!fmt
->qf_ops
->check_quota_file(sb
, type
))
2063 dqopt
->ops
[type
] = fmt
->qf_ops
;
2064 dqopt
->info
[type
].dqi_format
= fmt
;
2065 dqopt
->info
[type
].dqi_fmt_id
= format_id
;
2066 INIT_LIST_HEAD(&dqopt
->info
[type
].dqi_dirty_list
);
2067 mutex_lock(&dqopt
->dqio_mutex
);
2068 error
= dqopt
->ops
[type
]->read_file_info(sb
, type
);
2070 mutex_unlock(&dqopt
->dqio_mutex
);
2073 mutex_unlock(&dqopt
->dqio_mutex
);
2074 spin_lock(&dq_state_lock
);
2075 dqopt
->flags
|= dquot_state_flag(flags
, type
);
2076 spin_unlock(&dq_state_lock
);
2078 add_dquot_ref(sb
, type
);
2079 mutex_unlock(&dqopt
->dqonoff_mutex
);
2084 dqopt
->files
[type
] = NULL
;
2087 if (oldflags
!= -1) {
2088 mutex_lock_nested(&inode
->i_mutex
, I_MUTEX_QUOTA
);
2089 /* Set the flags back (in the case of accidental quotaon()
2090 * on a wrong file we don't want to mess up the flags) */
2091 inode
->i_flags
&= ~(S_NOATIME
| S_NOQUOTA
| S_IMMUTABLE
);
2092 inode
->i_flags
|= oldflags
;
2093 mutex_unlock(&inode
->i_mutex
);
2095 mutex_unlock(&dqopt
->dqonoff_mutex
);
2097 put_quota_format(fmt
);
2102 /* Reenable quotas on remount RW */
2103 static int vfs_quota_on_remount(struct super_block
*sb
, int type
)
2105 struct quota_info
*dqopt
= sb_dqopt(sb
);
2106 struct inode
*inode
;
2110 mutex_lock(&dqopt
->dqonoff_mutex
);
2111 if (!sb_has_quota_suspended(sb
, type
)) {
2112 mutex_unlock(&dqopt
->dqonoff_mutex
);
2115 inode
= dqopt
->files
[type
];
2116 dqopt
->files
[type
] = NULL
;
2117 spin_lock(&dq_state_lock
);
2118 flags
= dqopt
->flags
& dquot_state_flag(DQUOT_USAGE_ENABLED
|
2119 DQUOT_LIMITS_ENABLED
, type
);
2120 dqopt
->flags
&= ~dquot_state_flag(DQUOT_STATE_FLAGS
, type
);
2121 spin_unlock(&dq_state_lock
);
2122 mutex_unlock(&dqopt
->dqonoff_mutex
);
2124 flags
= dquot_generic_flag(flags
, type
);
2125 ret
= vfs_load_quota_inode(inode
, type
, dqopt
->info
[type
].dqi_fmt_id
,
2132 int vfs_quota_on_path(struct super_block
*sb
, int type
, int format_id
,
2135 int error
= security_quota_on(path
->dentry
);
2138 /* Quota file not on the same filesystem? */
2139 if (path
->mnt
->mnt_sb
!= sb
)
2142 error
= vfs_load_quota_inode(path
->dentry
->d_inode
, type
,
2143 format_id
, DQUOT_USAGE_ENABLED
|
2144 DQUOT_LIMITS_ENABLED
);
2147 EXPORT_SYMBOL(vfs_quota_on_path
);
2149 int vfs_quota_on(struct super_block
*sb
, int type
, int format_id
, char *name
,
2156 return vfs_quota_on_remount(sb
, type
);
2158 error
= kern_path(name
, LOOKUP_FOLLOW
, &path
);
2160 error
= vfs_quota_on_path(sb
, type
, format_id
, &path
);
2165 EXPORT_SYMBOL(vfs_quota_on
);
2168 * More powerful function for turning on quotas allowing setting
2169 * of individual quota flags
2171 int vfs_quota_enable(struct inode
*inode
, int type
, int format_id
,
2175 struct super_block
*sb
= inode
->i_sb
;
2176 struct quota_info
*dqopt
= sb_dqopt(sb
);
2178 /* Just unsuspend quotas? */
2179 if (flags
& DQUOT_SUSPENDED
)
2180 return vfs_quota_on_remount(sb
, type
);
2183 /* Just updating flags needed? */
2184 if (sb_has_quota_loaded(sb
, type
)) {
2185 mutex_lock(&dqopt
->dqonoff_mutex
);
2186 /* Now do a reliable test... */
2187 if (!sb_has_quota_loaded(sb
, type
)) {
2188 mutex_unlock(&dqopt
->dqonoff_mutex
);
2191 if (flags
& DQUOT_USAGE_ENABLED
&&
2192 sb_has_quota_usage_enabled(sb
, type
)) {
2196 if (flags
& DQUOT_LIMITS_ENABLED
&&
2197 sb_has_quota_limits_enabled(sb
, type
)) {
2201 spin_lock(&dq_state_lock
);
2202 sb_dqopt(sb
)->flags
|= dquot_state_flag(flags
, type
);
2203 spin_unlock(&dq_state_lock
);
2205 mutex_unlock(&dqopt
->dqonoff_mutex
);
2210 return vfs_load_quota_inode(inode
, type
, format_id
, flags
);
2212 EXPORT_SYMBOL(vfs_quota_enable
);
2215 * This function is used when filesystem needs to initialize quotas
2216 * during mount time.
2218 int vfs_quota_on_mount(struct super_block
*sb
, char *qf_name
,
2219 int format_id
, int type
)
2221 struct dentry
*dentry
;
2224 mutex_lock(&sb
->s_root
->d_inode
->i_mutex
);
2225 dentry
= lookup_one_len(qf_name
, sb
->s_root
, strlen(qf_name
));
2226 mutex_unlock(&sb
->s_root
->d_inode
->i_mutex
);
2228 return PTR_ERR(dentry
);
2230 if (!dentry
->d_inode
) {
2235 error
= security_quota_on(dentry
);
2237 error
= vfs_load_quota_inode(dentry
->d_inode
, type
, format_id
,
2238 DQUOT_USAGE_ENABLED
| DQUOT_LIMITS_ENABLED
);
2244 EXPORT_SYMBOL(vfs_quota_on_mount
);
2246 /* Wrapper to turn on quotas when remounting rw */
2247 int vfs_dq_quota_on_remount(struct super_block
*sb
)
2252 if (!sb
->s_qcop
|| !sb
->s_qcop
->quota_on
)
2254 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++) {
2255 err
= sb
->s_qcop
->quota_on(sb
, cnt
, 0, NULL
, 1);
2256 if (err
< 0 && !ret
)
2261 EXPORT_SYMBOL(vfs_dq_quota_on_remount
);
2263 static inline qsize_t
qbtos(qsize_t blocks
)
2265 return blocks
<< QIF_DQBLKSIZE_BITS
;
2268 static inline qsize_t
stoqb(qsize_t space
)
2270 return (space
+ QIF_DQBLKSIZE
- 1) >> QIF_DQBLKSIZE_BITS
;
2273 /* Generic routine for getting common part of quota structure */
2274 static void do_get_dqblk(struct dquot
*dquot
, struct if_dqblk
*di
)
2276 struct mem_dqblk
*dm
= &dquot
->dq_dqb
;
2278 spin_lock(&dq_data_lock
);
2279 di
->dqb_bhardlimit
= stoqb(dm
->dqb_bhardlimit
);
2280 di
->dqb_bsoftlimit
= stoqb(dm
->dqb_bsoftlimit
);
2281 di
->dqb_curspace
= dm
->dqb_curspace
+ dm
->dqb_rsvspace
;
2282 di
->dqb_ihardlimit
= dm
->dqb_ihardlimit
;
2283 di
->dqb_isoftlimit
= dm
->dqb_isoftlimit
;
2284 di
->dqb_curinodes
= dm
->dqb_curinodes
;
2285 di
->dqb_btime
= dm
->dqb_btime
;
2286 di
->dqb_itime
= dm
->dqb_itime
;
2287 di
->dqb_valid
= QIF_ALL
;
2288 spin_unlock(&dq_data_lock
);
2291 int vfs_get_dqblk(struct super_block
*sb
, int type
, qid_t id
,
2292 struct if_dqblk
*di
)
2294 struct dquot
*dquot
;
2296 dquot
= dqget(sb
, id
, type
);
2299 do_get_dqblk(dquot
, di
);
2304 EXPORT_SYMBOL(vfs_get_dqblk
);
2306 /* Generic routine for setting common part of quota structure */
2307 static int do_set_dqblk(struct dquot
*dquot
, struct if_dqblk
*di
)
2309 struct mem_dqblk
*dm
= &dquot
->dq_dqb
;
2310 int check_blim
= 0, check_ilim
= 0;
2311 struct mem_dqinfo
*dqi
= &sb_dqopt(dquot
->dq_sb
)->info
[dquot
->dq_type
];
2313 if ((di
->dqb_valid
& QIF_BLIMITS
&&
2314 (di
->dqb_bhardlimit
> dqi
->dqi_maxblimit
||
2315 di
->dqb_bsoftlimit
> dqi
->dqi_maxblimit
)) ||
2316 (di
->dqb_valid
& QIF_ILIMITS
&&
2317 (di
->dqb_ihardlimit
> dqi
->dqi_maxilimit
||
2318 di
->dqb_isoftlimit
> dqi
->dqi_maxilimit
)))
2321 spin_lock(&dq_data_lock
);
2322 if (di
->dqb_valid
& QIF_SPACE
) {
2323 dm
->dqb_curspace
= di
->dqb_curspace
- dm
->dqb_rsvspace
;
2325 __set_bit(DQ_LASTSET_B
+ QIF_SPACE_B
, &dquot
->dq_flags
);
2327 if (di
->dqb_valid
& QIF_BLIMITS
) {
2328 dm
->dqb_bsoftlimit
= qbtos(di
->dqb_bsoftlimit
);
2329 dm
->dqb_bhardlimit
= qbtos(di
->dqb_bhardlimit
);
2331 __set_bit(DQ_LASTSET_B
+ QIF_BLIMITS_B
, &dquot
->dq_flags
);
2333 if (di
->dqb_valid
& QIF_INODES
) {
2334 dm
->dqb_curinodes
= di
->dqb_curinodes
;
2336 __set_bit(DQ_LASTSET_B
+ QIF_INODES_B
, &dquot
->dq_flags
);
2338 if (di
->dqb_valid
& QIF_ILIMITS
) {
2339 dm
->dqb_isoftlimit
= di
->dqb_isoftlimit
;
2340 dm
->dqb_ihardlimit
= di
->dqb_ihardlimit
;
2342 __set_bit(DQ_LASTSET_B
+ QIF_ILIMITS_B
, &dquot
->dq_flags
);
2344 if (di
->dqb_valid
& QIF_BTIME
) {
2345 dm
->dqb_btime
= di
->dqb_btime
;
2347 __set_bit(DQ_LASTSET_B
+ QIF_BTIME_B
, &dquot
->dq_flags
);
2349 if (di
->dqb_valid
& QIF_ITIME
) {
2350 dm
->dqb_itime
= di
->dqb_itime
;
2352 __set_bit(DQ_LASTSET_B
+ QIF_ITIME_B
, &dquot
->dq_flags
);
2356 if (!dm
->dqb_bsoftlimit
||
2357 dm
->dqb_curspace
< dm
->dqb_bsoftlimit
) {
2359 clear_bit(DQ_BLKS_B
, &dquot
->dq_flags
);
2360 } else if (!(di
->dqb_valid
& QIF_BTIME
))
2361 /* Set grace only if user hasn't provided his own... */
2362 dm
->dqb_btime
= get_seconds() + dqi
->dqi_bgrace
;
2365 if (!dm
->dqb_isoftlimit
||
2366 dm
->dqb_curinodes
< dm
->dqb_isoftlimit
) {
2368 clear_bit(DQ_INODES_B
, &dquot
->dq_flags
);
2369 } else if (!(di
->dqb_valid
& QIF_ITIME
))
2370 /* Set grace only if user hasn't provided his own... */
2371 dm
->dqb_itime
= get_seconds() + dqi
->dqi_igrace
;
2373 if (dm
->dqb_bhardlimit
|| dm
->dqb_bsoftlimit
|| dm
->dqb_ihardlimit
||
2375 clear_bit(DQ_FAKE_B
, &dquot
->dq_flags
);
2377 set_bit(DQ_FAKE_B
, &dquot
->dq_flags
);
2378 spin_unlock(&dq_data_lock
);
2379 mark_dquot_dirty(dquot
);
2384 int vfs_set_dqblk(struct super_block
*sb
, int type
, qid_t id
,
2385 struct if_dqblk
*di
)
2387 struct dquot
*dquot
;
2390 dquot
= dqget(sb
, id
, type
);
2395 rc
= do_set_dqblk(dquot
, di
);
2400 EXPORT_SYMBOL(vfs_set_dqblk
);
2402 /* Generic routine for getting common part of quota file information */
2403 int vfs_get_dqinfo(struct super_block
*sb
, int type
, struct if_dqinfo
*ii
)
2405 struct mem_dqinfo
*mi
;
2407 mutex_lock(&sb_dqopt(sb
)->dqonoff_mutex
);
2408 if (!sb_has_quota_active(sb
, type
)) {
2409 mutex_unlock(&sb_dqopt(sb
)->dqonoff_mutex
);
2412 mi
= sb_dqopt(sb
)->info
+ type
;
2413 spin_lock(&dq_data_lock
);
2414 ii
->dqi_bgrace
= mi
->dqi_bgrace
;
2415 ii
->dqi_igrace
= mi
->dqi_igrace
;
2416 ii
->dqi_flags
= mi
->dqi_flags
& DQF_MASK
;
2417 ii
->dqi_valid
= IIF_ALL
;
2418 spin_unlock(&dq_data_lock
);
2419 mutex_unlock(&sb_dqopt(sb
)->dqonoff_mutex
);
2422 EXPORT_SYMBOL(vfs_get_dqinfo
);
2424 /* Generic routine for setting common part of quota file information */
2425 int vfs_set_dqinfo(struct super_block
*sb
, int type
, struct if_dqinfo
*ii
)
2427 struct mem_dqinfo
*mi
;
2430 mutex_lock(&sb_dqopt(sb
)->dqonoff_mutex
);
2431 if (!sb_has_quota_active(sb
, type
)) {
2435 mi
= sb_dqopt(sb
)->info
+ type
;
2436 spin_lock(&dq_data_lock
);
2437 if (ii
->dqi_valid
& IIF_BGRACE
)
2438 mi
->dqi_bgrace
= ii
->dqi_bgrace
;
2439 if (ii
->dqi_valid
& IIF_IGRACE
)
2440 mi
->dqi_igrace
= ii
->dqi_igrace
;
2441 if (ii
->dqi_valid
& IIF_FLAGS
)
2442 mi
->dqi_flags
= (mi
->dqi_flags
& ~DQF_MASK
) |
2443 (ii
->dqi_flags
& DQF_MASK
);
2444 spin_unlock(&dq_data_lock
);
2445 mark_info_dirty(sb
, type
);
2446 /* Force write to disk */
2447 sb
->dq_op
->write_info(sb
, type
);
2449 mutex_unlock(&sb_dqopt(sb
)->dqonoff_mutex
);
2452 EXPORT_SYMBOL(vfs_set_dqinfo
);
2454 const struct quotactl_ops vfs_quotactl_ops
= {
2455 .quota_on
= vfs_quota_on
,
2456 .quota_off
= vfs_quota_off
,
2457 .quota_sync
= vfs_quota_sync
,
2458 .get_info
= vfs_get_dqinfo
,
2459 .set_info
= vfs_set_dqinfo
,
2460 .get_dqblk
= vfs_get_dqblk
,
2461 .set_dqblk
= vfs_set_dqblk
2464 static ctl_table fs_dqstats_table
[] = {
2466 .procname
= "lookups",
2467 .data
= &dqstats
.lookups
,
2468 .maxlen
= sizeof(int),
2470 .proc_handler
= proc_dointvec
,
2473 .procname
= "drops",
2474 .data
= &dqstats
.drops
,
2475 .maxlen
= sizeof(int),
2477 .proc_handler
= proc_dointvec
,
2480 .procname
= "reads",
2481 .data
= &dqstats
.reads
,
2482 .maxlen
= sizeof(int),
2484 .proc_handler
= proc_dointvec
,
2487 .procname
= "writes",
2488 .data
= &dqstats
.writes
,
2489 .maxlen
= sizeof(int),
2491 .proc_handler
= proc_dointvec
,
2494 .procname
= "cache_hits",
2495 .data
= &dqstats
.cache_hits
,
2496 .maxlen
= sizeof(int),
2498 .proc_handler
= proc_dointvec
,
2501 .procname
= "allocated_dquots",
2502 .data
= &dqstats
.allocated_dquots
,
2503 .maxlen
= sizeof(int),
2505 .proc_handler
= proc_dointvec
,
2508 .procname
= "free_dquots",
2509 .data
= &dqstats
.free_dquots
,
2510 .maxlen
= sizeof(int),
2512 .proc_handler
= proc_dointvec
,
2515 .procname
= "syncs",
2516 .data
= &dqstats
.syncs
,
2517 .maxlen
= sizeof(int),
2519 .proc_handler
= proc_dointvec
,
2521 #ifdef CONFIG_PRINT_QUOTA_WARNING
2523 .procname
= "warnings",
2524 .data
= &flag_print_warnings
,
2525 .maxlen
= sizeof(int),
2527 .proc_handler
= proc_dointvec
,
2533 static ctl_table fs_table
[] = {
2535 .procname
= "quota",
2537 .child
= fs_dqstats_table
,
2542 static ctl_table sys_table
[] = {
2551 static int __init
dquot_init(void)
2554 unsigned long nr_hash
, order
;
2556 printk(KERN_NOTICE
"VFS: Disk quotas %s\n", __DQUOT_VERSION__
);
2558 register_sysctl_table(sys_table
);
2560 dquot_cachep
= kmem_cache_create("dquot",
2561 sizeof(struct dquot
), sizeof(unsigned long) * 4,
2562 (SLAB_HWCACHE_ALIGN
|SLAB_RECLAIM_ACCOUNT
|
2563 SLAB_MEM_SPREAD
|SLAB_PANIC
),
2567 dquot_hash
= (struct hlist_head
*)__get_free_pages(GFP_ATOMIC
, order
);
2569 panic("Cannot create dquot hash table");
2571 /* Find power-of-two hlist_heads which can fit into allocation */
2572 nr_hash
= (1UL << order
) * PAGE_SIZE
/ sizeof(struct hlist_head
);
2576 } while (nr_hash
>> dq_hash_bits
);
2579 nr_hash
= 1UL << dq_hash_bits
;
2580 dq_hash_mask
= nr_hash
- 1;
2581 for (i
= 0; i
< nr_hash
; i
++)
2582 INIT_HLIST_HEAD(dquot_hash
+ i
);
2584 printk("Dquot-cache hash table entries: %ld (order %ld, %ld bytes)\n",
2585 nr_hash
, order
, (PAGE_SIZE
<< order
));
2587 register_shrinker(&dqcache_shrinker
);
2591 module_init(dquot_init
);