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 two quota SMP locks. dq_list_lock protects all lists with quotas
91 * and quota formats and also dqstats structure containing statistics about the
92 * lists. dq_data_lock protects data from dq_dqb and also mem_dqinfo structures
93 * and 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().
97 * The spinlock ordering is hence: dq_data_lock > dq_list_lock > i_lock
99 * Note that some things (eg. sb pointer, type, id) doesn't change during
100 * the life of the dquot structure and so needn't to be protected by a lock
102 * Any operation working on dquots via inode pointers must hold dqptr_sem. If
103 * operation is just reading pointers from inode (or not using them at all) the
104 * read lock is enough. If pointers are altered function must hold write lock
105 * (these locking rules also apply for S_NOQUOTA flag in the inode - note that
106 * for altering the flag i_mutex is also needed). If operation is holding
107 * reference to dquot in other way (e.g. quotactl ops) it must be guarded by
109 * This locking assures that:
110 * a) update/access to dquot pointers in inode is serialized
111 * b) everyone is guarded against invalidate_dquots()
113 * Each dquot has its dq_lock mutex. Locked dquots might not be referenced
114 * from inodes (dquot_alloc_space() and such don't check the dq_lock).
115 * Currently dquot is locked only when it is being read to memory (or space for
116 * it is being allocated) on the first dqget() and when it is being released on
117 * the last dqput(). The allocation and release oparations are serialized by
118 * the dq_lock and by checking the use count in dquot_release(). Write
119 * operations on dquots don't hold dq_lock as they copy data under dq_data_lock
120 * spinlock to internal buffers before writing.
122 * Lock ordering (including related VFS locks) is the following:
123 * i_mutex > dqonoff_sem > journal_lock > dqptr_sem > dquot->dq_lock >
125 * i_mutex on quota files is special (it's below dqio_mutex)
128 static DEFINE_SPINLOCK(dq_list_lock
);
129 DEFINE_SPINLOCK(dq_data_lock
);
131 static char *quotatypes
[] = INITQFNAMES
;
132 static struct quota_format_type
*quota_formats
; /* List of registered formats */
133 static struct quota_module_name module_names
[] = INIT_QUOTA_MODULE_NAMES
;
135 /* SLAB cache for dquot structures */
136 static struct kmem_cache
*dquot_cachep
;
138 int register_quota_format(struct quota_format_type
*fmt
)
140 spin_lock(&dq_list_lock
);
141 fmt
->qf_next
= quota_formats
;
143 spin_unlock(&dq_list_lock
);
147 void unregister_quota_format(struct quota_format_type
*fmt
)
149 struct quota_format_type
**actqf
;
151 spin_lock(&dq_list_lock
);
152 for (actqf
= "a_formats
; *actqf
&& *actqf
!= fmt
; actqf
= &(*actqf
)->qf_next
);
154 *actqf
= (*actqf
)->qf_next
;
155 spin_unlock(&dq_list_lock
);
158 static struct quota_format_type
*find_quota_format(int id
)
160 struct quota_format_type
*actqf
;
162 spin_lock(&dq_list_lock
);
163 for (actqf
= quota_formats
; actqf
&& actqf
->qf_fmt_id
!= id
; actqf
= actqf
->qf_next
);
164 if (!actqf
|| !try_module_get(actqf
->qf_owner
)) {
167 spin_unlock(&dq_list_lock
);
169 for (qm
= 0; module_names
[qm
].qm_fmt_id
&& module_names
[qm
].qm_fmt_id
!= id
; qm
++);
170 if (!module_names
[qm
].qm_fmt_id
|| request_module(module_names
[qm
].qm_mod_name
))
173 spin_lock(&dq_list_lock
);
174 for (actqf
= quota_formats
; actqf
&& actqf
->qf_fmt_id
!= id
; actqf
= actqf
->qf_next
);
175 if (actqf
&& !try_module_get(actqf
->qf_owner
))
178 spin_unlock(&dq_list_lock
);
182 static void put_quota_format(struct quota_format_type
*fmt
)
184 module_put(fmt
->qf_owner
);
188 * Dquot List Management:
189 * The quota code uses three lists for dquot management: the inuse_list,
190 * free_dquots, and dquot_hash[] array. A single dquot structure may be
191 * on all three lists, depending on its current state.
193 * All dquots are placed to the end of inuse_list when first created, and this
194 * list is used for invalidate operation, which must look at every dquot.
196 * Unused dquots (dq_count == 0) are added to the free_dquots list when freed,
197 * and this list is searched whenever we need an available dquot. Dquots are
198 * removed from the list as soon as they are used again, and
199 * dqstats.free_dquots gives the number of dquots on the list. When
200 * dquot is invalidated it's completely released from memory.
202 * Dquots with a specific identity (device, type and id) are placed on
203 * one of the dquot_hash[] hash chains. The provides an efficient search
204 * mechanism to locate a specific dquot.
207 static LIST_HEAD(inuse_list
);
208 static LIST_HEAD(free_dquots
);
209 static unsigned int dq_hash_bits
, dq_hash_mask
;
210 static struct hlist_head
*dquot_hash
;
212 struct dqstats dqstats
;
214 static void dqput(struct dquot
*dquot
);
216 static inline unsigned int
217 hashfn(const struct super_block
*sb
, unsigned int id
, int type
)
221 tmp
= (((unsigned long)sb
>>L1_CACHE_SHIFT
) ^ id
) * (MAXQUOTAS
- type
);
222 return (tmp
+ (tmp
>> dq_hash_bits
)) & dq_hash_mask
;
226 * Following list functions expect dq_list_lock to be held
228 static inline void insert_dquot_hash(struct dquot
*dquot
)
230 struct hlist_head
*head
= dquot_hash
+ hashfn(dquot
->dq_sb
, dquot
->dq_id
, dquot
->dq_type
);
231 hlist_add_head(&dquot
->dq_hash
, head
);
234 static inline void remove_dquot_hash(struct dquot
*dquot
)
236 hlist_del_init(&dquot
->dq_hash
);
239 static inline struct dquot
*find_dquot(unsigned int hashent
, struct super_block
*sb
, unsigned int id
, int type
)
241 struct hlist_node
*node
;
244 hlist_for_each (node
, dquot_hash
+hashent
) {
245 dquot
= hlist_entry(node
, struct dquot
, dq_hash
);
246 if (dquot
->dq_sb
== sb
&& dquot
->dq_id
== id
&& dquot
->dq_type
== type
)
252 /* Add a dquot to the tail of the free list */
253 static inline void put_dquot_last(struct dquot
*dquot
)
255 list_add_tail(&dquot
->dq_free
, &free_dquots
);
256 dqstats
.free_dquots
++;
259 static inline void remove_free_dquot(struct dquot
*dquot
)
261 if (list_empty(&dquot
->dq_free
))
263 list_del_init(&dquot
->dq_free
);
264 dqstats
.free_dquots
--;
267 static inline void put_inuse(struct dquot
*dquot
)
269 /* We add to the back of inuse list so we don't have to restart
270 * when traversing this list and we block */
271 list_add_tail(&dquot
->dq_inuse
, &inuse_list
);
272 dqstats
.allocated_dquots
++;
275 static inline void remove_inuse(struct dquot
*dquot
)
277 dqstats
.allocated_dquots
--;
278 list_del(&dquot
->dq_inuse
);
281 * End of list functions needing dq_list_lock
284 static void wait_on_dquot(struct dquot
*dquot
)
286 mutex_lock(&dquot
->dq_lock
);
287 mutex_unlock(&dquot
->dq_lock
);
290 static inline int dquot_dirty(struct dquot
*dquot
)
292 return test_bit(DQ_MOD_B
, &dquot
->dq_flags
);
295 static inline int mark_dquot_dirty(struct dquot
*dquot
)
297 return dquot
->dq_sb
->dq_op
->mark_dirty(dquot
);
300 int dquot_mark_dquot_dirty(struct dquot
*dquot
)
302 spin_lock(&dq_list_lock
);
303 if (!test_and_set_bit(DQ_MOD_B
, &dquot
->dq_flags
))
304 list_add(&dquot
->dq_dirty
, &sb_dqopt(dquot
->dq_sb
)->
305 info
[dquot
->dq_type
].dqi_dirty_list
);
306 spin_unlock(&dq_list_lock
);
310 /* This function needs dq_list_lock */
311 static inline int clear_dquot_dirty(struct dquot
*dquot
)
313 if (!test_and_clear_bit(DQ_MOD_B
, &dquot
->dq_flags
))
315 list_del_init(&dquot
->dq_dirty
);
319 void mark_info_dirty(struct super_block
*sb
, int type
)
321 set_bit(DQF_INFO_DIRTY_B
, &sb_dqopt(sb
)->info
[type
].dqi_flags
);
323 EXPORT_SYMBOL(mark_info_dirty
);
326 * Read dquot from disk and alloc space for it
329 int dquot_acquire(struct dquot
*dquot
)
331 int ret
= 0, ret2
= 0;
332 struct quota_info
*dqopt
= sb_dqopt(dquot
->dq_sb
);
334 mutex_lock(&dquot
->dq_lock
);
335 mutex_lock(&dqopt
->dqio_mutex
);
336 if (!test_bit(DQ_READ_B
, &dquot
->dq_flags
))
337 ret
= dqopt
->ops
[dquot
->dq_type
]->read_dqblk(dquot
);
340 set_bit(DQ_READ_B
, &dquot
->dq_flags
);
341 /* Instantiate dquot if needed */
342 if (!test_bit(DQ_ACTIVE_B
, &dquot
->dq_flags
) && !dquot
->dq_off
) {
343 ret
= dqopt
->ops
[dquot
->dq_type
]->commit_dqblk(dquot
);
344 /* Write the info if needed */
345 if (info_dirty(&dqopt
->info
[dquot
->dq_type
]))
346 ret2
= dqopt
->ops
[dquot
->dq_type
]->write_file_info(dquot
->dq_sb
, dquot
->dq_type
);
354 set_bit(DQ_ACTIVE_B
, &dquot
->dq_flags
);
356 mutex_unlock(&dqopt
->dqio_mutex
);
357 mutex_unlock(&dquot
->dq_lock
);
362 * Write dquot to disk
364 int dquot_commit(struct dquot
*dquot
)
366 int ret
= 0, ret2
= 0;
367 struct quota_info
*dqopt
= sb_dqopt(dquot
->dq_sb
);
369 mutex_lock(&dqopt
->dqio_mutex
);
370 spin_lock(&dq_list_lock
);
371 if (!clear_dquot_dirty(dquot
)) {
372 spin_unlock(&dq_list_lock
);
375 spin_unlock(&dq_list_lock
);
376 /* Inactive dquot can be only if there was error during read/init
377 * => we have better not writing it */
378 if (test_bit(DQ_ACTIVE_B
, &dquot
->dq_flags
)) {
379 ret
= dqopt
->ops
[dquot
->dq_type
]->commit_dqblk(dquot
);
380 if (info_dirty(&dqopt
->info
[dquot
->dq_type
]))
381 ret2
= dqopt
->ops
[dquot
->dq_type
]->write_file_info(dquot
->dq_sb
, dquot
->dq_type
);
386 mutex_unlock(&dqopt
->dqio_mutex
);
393 int dquot_release(struct dquot
*dquot
)
395 int ret
= 0, ret2
= 0;
396 struct quota_info
*dqopt
= sb_dqopt(dquot
->dq_sb
);
398 mutex_lock(&dquot
->dq_lock
);
399 /* Check whether we are not racing with some other dqget() */
400 if (atomic_read(&dquot
->dq_count
) > 1)
402 mutex_lock(&dqopt
->dqio_mutex
);
403 if (dqopt
->ops
[dquot
->dq_type
]->release_dqblk
) {
404 ret
= dqopt
->ops
[dquot
->dq_type
]->release_dqblk(dquot
);
406 if (info_dirty(&dqopt
->info
[dquot
->dq_type
]))
407 ret2
= dqopt
->ops
[dquot
->dq_type
]->write_file_info(dquot
->dq_sb
, dquot
->dq_type
);
411 clear_bit(DQ_ACTIVE_B
, &dquot
->dq_flags
);
412 mutex_unlock(&dqopt
->dqio_mutex
);
414 mutex_unlock(&dquot
->dq_lock
);
418 static void dquot_destroy(struct dquot
*dquot
)
420 kmem_cache_free(dquot_cachep
, dquot
);
423 static inline void do_destroy_dquot(struct dquot
*dquot
)
425 dquot
->dq_sb
->dq_op
->destroy_dquot(dquot
);
428 /* Invalidate all dquots on the list. Note that this function is called after
429 * quota is disabled and pointers from inodes removed so there cannot be new
430 * quota users. There can still be some users of quotas due to inodes being
431 * just deleted or pruned by prune_icache() (those are not attached to any
432 * list). We have to wait for such users.
434 static void invalidate_dquots(struct super_block
*sb
, int type
)
436 struct dquot
*dquot
, *tmp
;
439 spin_lock(&dq_list_lock
);
440 list_for_each_entry_safe(dquot
, tmp
, &inuse_list
, dq_inuse
) {
441 if (dquot
->dq_sb
!= sb
)
443 if (dquot
->dq_type
!= type
)
445 /* Wait for dquot users */
446 if (atomic_read(&dquot
->dq_count
)) {
449 atomic_inc(&dquot
->dq_count
);
450 prepare_to_wait(&dquot
->dq_wait_unused
, &wait
,
451 TASK_UNINTERRUPTIBLE
);
452 spin_unlock(&dq_list_lock
);
453 /* Once dqput() wakes us up, we know it's time to free
455 * IMPORTANT: we rely on the fact that there is always
456 * at most one process waiting for dquot to free.
457 * Otherwise dq_count would be > 1 and we would never
460 if (atomic_read(&dquot
->dq_count
) > 1)
462 finish_wait(&dquot
->dq_wait_unused
, &wait
);
464 /* At this moment dquot() need not exist (it could be
465 * reclaimed by prune_dqcache(). Hence we must
470 * Quota now has no users and it has been written on last
473 remove_dquot_hash(dquot
);
474 remove_free_dquot(dquot
);
476 do_destroy_dquot(dquot
);
478 spin_unlock(&dq_list_lock
);
481 int vfs_quota_sync(struct super_block
*sb
, int type
)
483 struct list_head
*dirty
;
485 struct quota_info
*dqopt
= sb_dqopt(sb
);
488 mutex_lock(&dqopt
->dqonoff_mutex
);
489 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++) {
490 if (type
!= -1 && cnt
!= type
)
492 if (!sb_has_quota_enabled(sb
, cnt
))
494 spin_lock(&dq_list_lock
);
495 dirty
= &dqopt
->info
[cnt
].dqi_dirty_list
;
496 while (!list_empty(dirty
)) {
497 dquot
= list_first_entry(dirty
, struct dquot
, dq_dirty
);
498 /* Dirty and inactive can be only bad dquot... */
499 if (!test_bit(DQ_ACTIVE_B
, &dquot
->dq_flags
)) {
500 clear_dquot_dirty(dquot
);
503 /* Now we have active dquot from which someone is
504 * holding reference so we can safely just increase
506 atomic_inc(&dquot
->dq_count
);
508 spin_unlock(&dq_list_lock
);
509 sb
->dq_op
->write_dquot(dquot
);
511 spin_lock(&dq_list_lock
);
513 spin_unlock(&dq_list_lock
);
516 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++)
517 if ((cnt
== type
|| type
== -1) && sb_has_quota_enabled(sb
, cnt
)
518 && info_dirty(&dqopt
->info
[cnt
]))
519 sb
->dq_op
->write_info(sb
, cnt
);
520 spin_lock(&dq_list_lock
);
522 spin_unlock(&dq_list_lock
);
523 mutex_unlock(&dqopt
->dqonoff_mutex
);
528 /* Free unused dquots from cache */
529 static void prune_dqcache(int count
)
531 struct list_head
*head
;
534 head
= free_dquots
.prev
;
535 while (head
!= &free_dquots
&& count
) {
536 dquot
= list_entry(head
, struct dquot
, dq_free
);
537 remove_dquot_hash(dquot
);
538 remove_free_dquot(dquot
);
540 do_destroy_dquot(dquot
);
542 head
= free_dquots
.prev
;
547 * This is called from kswapd when we think we need some
551 static int shrink_dqcache_memory(int nr
, gfp_t gfp_mask
)
554 spin_lock(&dq_list_lock
);
556 spin_unlock(&dq_list_lock
);
558 return (dqstats
.free_dquots
/ 100) * sysctl_vfs_cache_pressure
;
561 static struct shrinker dqcache_shrinker
= {
562 .shrink
= shrink_dqcache_memory
,
563 .seeks
= DEFAULT_SEEKS
,
567 * Put reference to dquot
568 * NOTE: If you change this function please check whether dqput_blocks() works right...
569 * MUST be called with either dqptr_sem or dqonoff_mutex held
571 static void dqput(struct dquot
*dquot
)
577 #ifdef __DQUOT_PARANOIA
578 if (!atomic_read(&dquot
->dq_count
)) {
579 printk("VFS: dqput: trying to free free dquot\n");
580 printk("VFS: device %s, dquot of %s %d\n",
582 quotatypes
[dquot
->dq_type
],
588 spin_lock(&dq_list_lock
);
590 spin_unlock(&dq_list_lock
);
592 spin_lock(&dq_list_lock
);
593 if (atomic_read(&dquot
->dq_count
) > 1) {
594 /* We have more than one user... nothing to do */
595 atomic_dec(&dquot
->dq_count
);
596 /* Releasing dquot during quotaoff phase? */
597 if (!sb_has_quota_enabled(dquot
->dq_sb
, dquot
->dq_type
) &&
598 atomic_read(&dquot
->dq_count
) == 1)
599 wake_up(&dquot
->dq_wait_unused
);
600 spin_unlock(&dq_list_lock
);
603 /* Need to release dquot? */
604 if (test_bit(DQ_ACTIVE_B
, &dquot
->dq_flags
) && dquot_dirty(dquot
)) {
605 spin_unlock(&dq_list_lock
);
606 /* Commit dquot before releasing */
607 ret
= dquot
->dq_sb
->dq_op
->write_dquot(dquot
);
609 printk(KERN_ERR
"VFS: cannot write quota structure on "
610 "device %s (error %d). Quota may get out of "
611 "sync!\n", dquot
->dq_sb
->s_id
, ret
);
613 * We clear dirty bit anyway, so that we avoid
616 spin_lock(&dq_list_lock
);
617 clear_dquot_dirty(dquot
);
618 spin_unlock(&dq_list_lock
);
622 /* Clear flag in case dquot was inactive (something bad happened) */
623 clear_dquot_dirty(dquot
);
624 if (test_bit(DQ_ACTIVE_B
, &dquot
->dq_flags
)) {
625 spin_unlock(&dq_list_lock
);
626 dquot
->dq_sb
->dq_op
->release_dquot(dquot
);
629 atomic_dec(&dquot
->dq_count
);
630 #ifdef __DQUOT_PARANOIA
632 BUG_ON(!list_empty(&dquot
->dq_free
));
634 put_dquot_last(dquot
);
635 spin_unlock(&dq_list_lock
);
638 static struct dquot
*dquot_alloc(struct super_block
*sb
, int type
)
640 return kmem_cache_zalloc(dquot_cachep
, GFP_NOFS
);
643 static struct dquot
*get_empty_dquot(struct super_block
*sb
, int type
)
647 dquot
= sb
->dq_op
->alloc_dquot(sb
, type
);
651 mutex_init(&dquot
->dq_lock
);
652 INIT_LIST_HEAD(&dquot
->dq_free
);
653 INIT_LIST_HEAD(&dquot
->dq_inuse
);
654 INIT_HLIST_NODE(&dquot
->dq_hash
);
655 INIT_LIST_HEAD(&dquot
->dq_dirty
);
656 init_waitqueue_head(&dquot
->dq_wait_unused
);
658 dquot
->dq_type
= type
;
659 atomic_set(&dquot
->dq_count
, 1);
665 * Get reference to dquot
666 * MUST be called with either dqptr_sem or dqonoff_mutex held
668 static struct dquot
*dqget(struct super_block
*sb
, unsigned int id
, int type
)
670 unsigned int hashent
= hashfn(sb
, id
, type
);
671 struct dquot
*dquot
, *empty
= NODQUOT
;
673 if (!sb_has_quota_enabled(sb
, type
))
676 spin_lock(&dq_list_lock
);
677 if ((dquot
= find_dquot(hashent
, sb
, id
, type
)) == NODQUOT
) {
678 if (empty
== NODQUOT
) {
679 spin_unlock(&dq_list_lock
);
680 if ((empty
= get_empty_dquot(sb
, type
)) == NODQUOT
)
681 schedule(); /* Try to wait for a moment... */
686 /* all dquots go on the inuse_list */
688 /* hash it first so it can be found */
689 insert_dquot_hash(dquot
);
691 spin_unlock(&dq_list_lock
);
693 if (!atomic_read(&dquot
->dq_count
))
694 remove_free_dquot(dquot
);
695 atomic_inc(&dquot
->dq_count
);
696 dqstats
.cache_hits
++;
698 spin_unlock(&dq_list_lock
);
700 do_destroy_dquot(empty
);
702 /* Wait for dq_lock - after this we know that either dquot_release() is already
703 * finished or it will be canceled due to dq_count > 1 test */
704 wait_on_dquot(dquot
);
705 /* Read the dquot and instantiate it (everything done only if needed) */
706 if (!test_bit(DQ_ACTIVE_B
, &dquot
->dq_flags
) && sb
->dq_op
->acquire_dquot(dquot
) < 0) {
710 #ifdef __DQUOT_PARANOIA
711 BUG_ON(!dquot
->dq_sb
); /* Has somebody invalidated entry under us? */
717 static int dqinit_needed(struct inode
*inode
, int type
)
721 if (IS_NOQUOTA(inode
))
724 return inode
->i_dquot
[type
] == NODQUOT
;
725 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++)
726 if (inode
->i_dquot
[cnt
] == NODQUOT
)
731 /* This routine is guarded by dqonoff_mutex mutex */
732 static void add_dquot_ref(struct super_block
*sb
, int type
)
734 struct inode
*inode
, *old_inode
= NULL
;
736 spin_lock(&inode_lock
);
737 list_for_each_entry(inode
, &sb
->s_inodes
, i_sb_list
) {
738 if (!atomic_read(&inode
->i_writecount
))
740 if (!dqinit_needed(inode
, type
))
742 if (inode
->i_state
& (I_FREEING
|I_WILL_FREE
))
746 spin_unlock(&inode_lock
);
749 sb
->dq_op
->initialize(inode
, type
);
750 /* We hold a reference to 'inode' so it couldn't have been
751 * removed from s_inodes list while we dropped the inode_lock.
752 * We cannot iput the inode now as we can be holding the last
753 * reference and we cannot iput it under inode_lock. So we
754 * keep the reference and iput it later. */
756 spin_lock(&inode_lock
);
758 spin_unlock(&inode_lock
);
762 /* Return 0 if dqput() won't block (note that 1 doesn't necessarily mean blocking) */
763 static inline int dqput_blocks(struct dquot
*dquot
)
765 if (atomic_read(&dquot
->dq_count
) <= 1)
770 /* Remove references to dquots from inode - add dquot to list for freeing if needed */
771 /* We can't race with anybody because we hold dqptr_sem for writing... */
772 static int remove_inode_dquot_ref(struct inode
*inode
, int type
,
773 struct list_head
*tofree_head
)
775 struct dquot
*dquot
= inode
->i_dquot
[type
];
777 inode
->i_dquot
[type
] = NODQUOT
;
778 if (dquot
!= NODQUOT
) {
779 if (dqput_blocks(dquot
)) {
780 #ifdef __DQUOT_PARANOIA
781 if (atomic_read(&dquot
->dq_count
) != 1)
782 printk(KERN_WARNING
"VFS: Adding dquot with dq_count %d to dispose list.\n", atomic_read(&dquot
->dq_count
));
784 spin_lock(&dq_list_lock
);
785 list_add(&dquot
->dq_free
, tofree_head
); /* As dquot must have currently users it can't be on the free list... */
786 spin_unlock(&dq_list_lock
);
790 dqput(dquot
); /* We have guaranteed we won't block */
795 /* Free list of dquots - called from inode.c */
796 /* dquots are removed from inodes, no new references can be got so we are the only ones holding reference */
797 static void put_dquot_list(struct list_head
*tofree_head
)
799 struct list_head
*act_head
;
802 act_head
= tofree_head
->next
;
803 /* So now we have dquots on the list... Just free them */
804 while (act_head
!= tofree_head
) {
805 dquot
= list_entry(act_head
, struct dquot
, dq_free
);
806 act_head
= act_head
->next
;
807 list_del_init(&dquot
->dq_free
); /* Remove dquot from the list so we won't have problems... */
812 static void remove_dquot_ref(struct super_block
*sb
, int type
,
813 struct list_head
*tofree_head
)
817 spin_lock(&inode_lock
);
818 list_for_each_entry(inode
, &sb
->s_inodes
, i_sb_list
) {
819 if (!IS_NOQUOTA(inode
))
820 remove_inode_dquot_ref(inode
, type
, tofree_head
);
822 spin_unlock(&inode_lock
);
825 /* Gather all references from inodes and drop them */
826 static void drop_dquot_ref(struct super_block
*sb
, int type
)
828 LIST_HEAD(tofree_head
);
831 down_write(&sb_dqopt(sb
)->dqptr_sem
);
832 remove_dquot_ref(sb
, type
, &tofree_head
);
833 up_write(&sb_dqopt(sb
)->dqptr_sem
);
834 put_dquot_list(&tofree_head
);
838 static inline void dquot_incr_inodes(struct dquot
*dquot
, unsigned long number
)
840 dquot
->dq_dqb
.dqb_curinodes
+= number
;
843 static inline void dquot_incr_space(struct dquot
*dquot
, qsize_t number
)
845 dquot
->dq_dqb
.dqb_curspace
+= number
;
848 static inline void dquot_decr_inodes(struct dquot
*dquot
, unsigned long number
)
850 if (dquot
->dq_dqb
.dqb_curinodes
> number
)
851 dquot
->dq_dqb
.dqb_curinodes
-= number
;
853 dquot
->dq_dqb
.dqb_curinodes
= 0;
854 if (dquot
->dq_dqb
.dqb_curinodes
<= dquot
->dq_dqb
.dqb_isoftlimit
)
855 dquot
->dq_dqb
.dqb_itime
= (time_t) 0;
856 clear_bit(DQ_INODES_B
, &dquot
->dq_flags
);
859 static inline void dquot_decr_space(struct dquot
*dquot
, qsize_t number
)
861 if (dquot
->dq_dqb
.dqb_curspace
> number
)
862 dquot
->dq_dqb
.dqb_curspace
-= number
;
864 dquot
->dq_dqb
.dqb_curspace
= 0;
865 if (toqb(dquot
->dq_dqb
.dqb_curspace
) <= dquot
->dq_dqb
.dqb_bsoftlimit
)
866 dquot
->dq_dqb
.dqb_btime
= (time_t) 0;
867 clear_bit(DQ_BLKS_B
, &dquot
->dq_flags
);
870 static int warning_issued(struct dquot
*dquot
, const int warntype
)
872 int flag
= (warntype
== QUOTA_NL_BHARDWARN
||
873 warntype
== QUOTA_NL_BSOFTLONGWARN
) ? DQ_BLKS_B
:
874 ((warntype
== QUOTA_NL_IHARDWARN
||
875 warntype
== QUOTA_NL_ISOFTLONGWARN
) ? DQ_INODES_B
: 0);
879 return test_and_set_bit(flag
, &dquot
->dq_flags
);
882 #ifdef CONFIG_PRINT_QUOTA_WARNING
883 static int flag_print_warnings
= 1;
885 static inline int need_print_warning(struct dquot
*dquot
)
887 if (!flag_print_warnings
)
890 switch (dquot
->dq_type
) {
892 return current_fsuid() == dquot
->dq_id
;
894 return in_group_p(dquot
->dq_id
);
899 /* Print warning to user which exceeded quota */
900 static void print_warning(struct dquot
*dquot
, const int warntype
)
903 struct tty_struct
*tty
;
905 if (warntype
== QUOTA_NL_IHARDBELOW
||
906 warntype
== QUOTA_NL_ISOFTBELOW
||
907 warntype
== QUOTA_NL_BHARDBELOW
||
908 warntype
== QUOTA_NL_BSOFTBELOW
|| !need_print_warning(dquot
))
911 tty
= get_current_tty();
914 tty_write_message(tty
, dquot
->dq_sb
->s_id
);
915 if (warntype
== QUOTA_NL_ISOFTWARN
|| warntype
== QUOTA_NL_BSOFTWARN
)
916 tty_write_message(tty
, ": warning, ");
918 tty_write_message(tty
, ": write failed, ");
919 tty_write_message(tty
, quotatypes
[dquot
->dq_type
]);
921 case QUOTA_NL_IHARDWARN
:
922 msg
= " file limit reached.\r\n";
924 case QUOTA_NL_ISOFTLONGWARN
:
925 msg
= " file quota exceeded too long.\r\n";
927 case QUOTA_NL_ISOFTWARN
:
928 msg
= " file quota exceeded.\r\n";
930 case QUOTA_NL_BHARDWARN
:
931 msg
= " block limit reached.\r\n";
933 case QUOTA_NL_BSOFTLONGWARN
:
934 msg
= " block quota exceeded too long.\r\n";
936 case QUOTA_NL_BSOFTWARN
:
937 msg
= " block quota exceeded.\r\n";
940 tty_write_message(tty
, msg
);
945 #ifdef CONFIG_QUOTA_NETLINK_INTERFACE
947 /* Netlink family structure for quota */
948 static struct genl_family quota_genl_family
= {
949 .id
= GENL_ID_GENERATE
,
953 .maxattr
= QUOTA_NL_A_MAX
,
956 /* Send warning to userspace about user which exceeded quota */
957 static void send_warning(const struct dquot
*dquot
, const char warntype
)
963 int msg_size
= 4 * nla_total_size(sizeof(u32
)) +
964 2 * nla_total_size(sizeof(u64
));
966 /* We have to allocate using GFP_NOFS as we are called from a
967 * filesystem performing write and thus further recursion into
968 * the fs to free some data could cause deadlocks. */
969 skb
= genlmsg_new(msg_size
, GFP_NOFS
);
972 "VFS: Not enough memory to send quota warning.\n");
975 msg_head
= genlmsg_put(skb
, 0, atomic_add_return(1, &seq
),
976 "a_genl_family
, 0, QUOTA_NL_C_WARNING
);
979 "VFS: Cannot store netlink header in quota warning.\n");
982 ret
= nla_put_u32(skb
, QUOTA_NL_A_QTYPE
, dquot
->dq_type
);
985 ret
= nla_put_u64(skb
, QUOTA_NL_A_EXCESS_ID
, dquot
->dq_id
);
988 ret
= nla_put_u32(skb
, QUOTA_NL_A_WARNING
, warntype
);
991 ret
= nla_put_u32(skb
, QUOTA_NL_A_DEV_MAJOR
,
992 MAJOR(dquot
->dq_sb
->s_dev
));
995 ret
= nla_put_u32(skb
, QUOTA_NL_A_DEV_MINOR
,
996 MINOR(dquot
->dq_sb
->s_dev
));
999 ret
= nla_put_u64(skb
, QUOTA_NL_A_CAUSED_ID
, current_uid());
1002 genlmsg_end(skb
, msg_head
);
1004 ret
= genlmsg_multicast(skb
, 0, quota_genl_family
.id
, GFP_NOFS
);
1005 if (ret
< 0 && ret
!= -ESRCH
)
1007 "VFS: Failed to send notification message: %d\n", ret
);
1010 printk(KERN_ERR
"VFS: Not enough space to compose quota message!\n");
1016 static inline void flush_warnings(struct dquot
* const *dquots
, char *warntype
)
1020 for (i
= 0; i
< MAXQUOTAS
; i
++)
1021 if (dquots
[i
] != NODQUOT
&& warntype
[i
] != QUOTA_NL_NOWARN
&&
1022 !warning_issued(dquots
[i
], warntype
[i
])) {
1023 #ifdef CONFIG_PRINT_QUOTA_WARNING
1024 print_warning(dquots
[i
], warntype
[i
]);
1026 #ifdef CONFIG_QUOTA_NETLINK_INTERFACE
1027 send_warning(dquots
[i
], warntype
[i
]);
1032 static inline char ignore_hardlimit(struct dquot
*dquot
)
1034 struct mem_dqinfo
*info
= &sb_dqopt(dquot
->dq_sb
)->info
[dquot
->dq_type
];
1036 return capable(CAP_SYS_RESOURCE
) &&
1037 (info
->dqi_format
->qf_fmt_id
!= QFMT_VFS_OLD
|| !(info
->dqi_flags
& V1_DQF_RSQUASH
));
1040 /* needs dq_data_lock */
1041 static int check_idq(struct dquot
*dquot
, ulong inodes
, char *warntype
)
1043 *warntype
= QUOTA_NL_NOWARN
;
1044 if (inodes
<= 0 || test_bit(DQ_FAKE_B
, &dquot
->dq_flags
))
1047 if (dquot
->dq_dqb
.dqb_ihardlimit
&&
1048 (dquot
->dq_dqb
.dqb_curinodes
+ inodes
) > dquot
->dq_dqb
.dqb_ihardlimit
&&
1049 !ignore_hardlimit(dquot
)) {
1050 *warntype
= QUOTA_NL_IHARDWARN
;
1054 if (dquot
->dq_dqb
.dqb_isoftlimit
&&
1055 (dquot
->dq_dqb
.dqb_curinodes
+ inodes
) > dquot
->dq_dqb
.dqb_isoftlimit
&&
1056 dquot
->dq_dqb
.dqb_itime
&& get_seconds() >= dquot
->dq_dqb
.dqb_itime
&&
1057 !ignore_hardlimit(dquot
)) {
1058 *warntype
= QUOTA_NL_ISOFTLONGWARN
;
1062 if (dquot
->dq_dqb
.dqb_isoftlimit
&&
1063 (dquot
->dq_dqb
.dqb_curinodes
+ inodes
) > dquot
->dq_dqb
.dqb_isoftlimit
&&
1064 dquot
->dq_dqb
.dqb_itime
== 0) {
1065 *warntype
= QUOTA_NL_ISOFTWARN
;
1066 dquot
->dq_dqb
.dqb_itime
= get_seconds() + sb_dqopt(dquot
->dq_sb
)->info
[dquot
->dq_type
].dqi_igrace
;
1072 /* needs dq_data_lock */
1073 static int check_bdq(struct dquot
*dquot
, qsize_t space
, int prealloc
, char *warntype
)
1075 *warntype
= QUOTA_NL_NOWARN
;
1076 if (space
<= 0 || test_bit(DQ_FAKE_B
, &dquot
->dq_flags
))
1079 if (dquot
->dq_dqb
.dqb_bhardlimit
&&
1080 toqb(dquot
->dq_dqb
.dqb_curspace
+ space
) > dquot
->dq_dqb
.dqb_bhardlimit
&&
1081 !ignore_hardlimit(dquot
)) {
1083 *warntype
= QUOTA_NL_BHARDWARN
;
1087 if (dquot
->dq_dqb
.dqb_bsoftlimit
&&
1088 toqb(dquot
->dq_dqb
.dqb_curspace
+ space
) > dquot
->dq_dqb
.dqb_bsoftlimit
&&
1089 dquot
->dq_dqb
.dqb_btime
&& get_seconds() >= dquot
->dq_dqb
.dqb_btime
&&
1090 !ignore_hardlimit(dquot
)) {
1092 *warntype
= QUOTA_NL_BSOFTLONGWARN
;
1096 if (dquot
->dq_dqb
.dqb_bsoftlimit
&&
1097 toqb(dquot
->dq_dqb
.dqb_curspace
+ space
) > dquot
->dq_dqb
.dqb_bsoftlimit
&&
1098 dquot
->dq_dqb
.dqb_btime
== 0) {
1100 *warntype
= QUOTA_NL_BSOFTWARN
;
1101 dquot
->dq_dqb
.dqb_btime
= get_seconds() + sb_dqopt(dquot
->dq_sb
)->info
[dquot
->dq_type
].dqi_bgrace
;
1105 * We don't allow preallocation to exceed softlimit so exceeding will
1114 static int info_idq_free(struct dquot
*dquot
, ulong inodes
)
1116 if (test_bit(DQ_FAKE_B
, &dquot
->dq_flags
) ||
1117 dquot
->dq_dqb
.dqb_curinodes
<= dquot
->dq_dqb
.dqb_isoftlimit
)
1118 return QUOTA_NL_NOWARN
;
1120 if (dquot
->dq_dqb
.dqb_curinodes
- inodes
<= dquot
->dq_dqb
.dqb_isoftlimit
)
1121 return QUOTA_NL_ISOFTBELOW
;
1122 if (dquot
->dq_dqb
.dqb_curinodes
>= dquot
->dq_dqb
.dqb_ihardlimit
&&
1123 dquot
->dq_dqb
.dqb_curinodes
- inodes
< dquot
->dq_dqb
.dqb_ihardlimit
)
1124 return QUOTA_NL_IHARDBELOW
;
1125 return QUOTA_NL_NOWARN
;
1128 static int info_bdq_free(struct dquot
*dquot
, qsize_t space
)
1130 if (test_bit(DQ_FAKE_B
, &dquot
->dq_flags
) ||
1131 toqb(dquot
->dq_dqb
.dqb_curspace
) <= dquot
->dq_dqb
.dqb_bsoftlimit
)
1132 return QUOTA_NL_NOWARN
;
1134 if (toqb(dquot
->dq_dqb
.dqb_curspace
- space
) <=
1135 dquot
->dq_dqb
.dqb_bsoftlimit
)
1136 return QUOTA_NL_BSOFTBELOW
;
1137 if (toqb(dquot
->dq_dqb
.dqb_curspace
) >= dquot
->dq_dqb
.dqb_bhardlimit
&&
1138 toqb(dquot
->dq_dqb
.dqb_curspace
- space
) <
1139 dquot
->dq_dqb
.dqb_bhardlimit
)
1140 return QUOTA_NL_BHARDBELOW
;
1141 return QUOTA_NL_NOWARN
;
1144 * Initialize quota pointers in inode
1145 * Transaction must be started at entry
1147 int dquot_initialize(struct inode
*inode
, int type
)
1149 unsigned int id
= 0;
1152 /* First test before acquiring mutex - solves deadlocks when we
1153 * re-enter the quota code and are already holding the mutex */
1154 if (IS_NOQUOTA(inode
))
1156 down_write(&sb_dqopt(inode
->i_sb
)->dqptr_sem
);
1157 /* Having dqptr_sem we know NOQUOTA flags can't be altered... */
1158 if (IS_NOQUOTA(inode
))
1160 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++) {
1161 if (type
!= -1 && cnt
!= type
)
1163 if (inode
->i_dquot
[cnt
] == NODQUOT
) {
1172 inode
->i_dquot
[cnt
] = dqget(inode
->i_sb
, id
, cnt
);
1176 up_write(&sb_dqopt(inode
->i_sb
)->dqptr_sem
);
1181 * Release all quotas referenced by inode
1182 * Transaction must be started at an entry
1184 int dquot_drop(struct inode
*inode
)
1188 down_write(&sb_dqopt(inode
->i_sb
)->dqptr_sem
);
1189 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++) {
1190 if (inode
->i_dquot
[cnt
] != NODQUOT
) {
1191 dqput(inode
->i_dquot
[cnt
]);
1192 inode
->i_dquot
[cnt
] = NODQUOT
;
1195 up_write(&sb_dqopt(inode
->i_sb
)->dqptr_sem
);
1199 /* Wrapper to remove references to quota structures from inode */
1200 void vfs_dq_drop(struct inode
*inode
)
1202 /* Here we can get arbitrary inode from clear_inode() so we have
1203 * to be careful. OTOH we don't need locking as quota operations
1204 * are allowed to change only at mount time */
1205 if (!IS_NOQUOTA(inode
) && inode
->i_sb
&& inode
->i_sb
->dq_op
1206 && inode
->i_sb
->dq_op
->drop
) {
1208 /* Test before calling to rule out calls from proc and such
1209 * where we are not allowed to block. Note that this is
1210 * actually reliable test even without the lock - the caller
1211 * must assure that nobody can come after the DQUOT_DROP and
1212 * add quota pointers back anyway */
1213 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++)
1214 if (inode
->i_dquot
[cnt
] != NODQUOT
)
1216 if (cnt
< MAXQUOTAS
)
1217 inode
->i_sb
->dq_op
->drop(inode
);
1222 * Following four functions update i_blocks+i_bytes fields and
1223 * quota information (together with appropriate checks)
1224 * NOTE: We absolutely rely on the fact that caller dirties
1225 * the inode (usually macros in quotaops.h care about this) and
1226 * holds a handle for the current transaction so that dquot write and
1227 * inode write go into the same transaction.
1231 * This operation can block, but only after everything is updated
1233 int dquot_alloc_space(struct inode
*inode
, qsize_t number
, int warn
)
1235 int cnt
, ret
= NO_QUOTA
;
1236 char warntype
[MAXQUOTAS
];
1238 /* First test before acquiring mutex - solves deadlocks when we
1239 * re-enter the quota code and are already holding the mutex */
1240 if (IS_NOQUOTA(inode
)) {
1242 inode_add_bytes(inode
, number
);
1245 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++)
1246 warntype
[cnt
] = QUOTA_NL_NOWARN
;
1248 down_read(&sb_dqopt(inode
->i_sb
)->dqptr_sem
);
1249 if (IS_NOQUOTA(inode
)) { /* Now we can do reliable test... */
1250 up_read(&sb_dqopt(inode
->i_sb
)->dqptr_sem
);
1253 spin_lock(&dq_data_lock
);
1254 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++) {
1255 if (inode
->i_dquot
[cnt
] == NODQUOT
)
1257 if (check_bdq(inode
->i_dquot
[cnt
], number
, warn
, warntype
+cnt
) == NO_QUOTA
)
1260 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++) {
1261 if (inode
->i_dquot
[cnt
] == NODQUOT
)
1263 dquot_incr_space(inode
->i_dquot
[cnt
], number
);
1265 inode_add_bytes(inode
, number
);
1268 spin_unlock(&dq_data_lock
);
1269 if (ret
== QUOTA_OK
)
1270 /* Dirtify all the dquots - this can block when journalling */
1271 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++)
1272 if (inode
->i_dquot
[cnt
])
1273 mark_dquot_dirty(inode
->i_dquot
[cnt
]);
1274 flush_warnings(inode
->i_dquot
, warntype
);
1275 up_read(&sb_dqopt(inode
->i_sb
)->dqptr_sem
);
1280 * This operation can block, but only after everything is updated
1282 int dquot_alloc_inode(const struct inode
*inode
, unsigned long number
)
1284 int cnt
, ret
= NO_QUOTA
;
1285 char warntype
[MAXQUOTAS
];
1287 /* First test before acquiring mutex - solves deadlocks when we
1288 * re-enter the quota code and are already holding the mutex */
1289 if (IS_NOQUOTA(inode
))
1291 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++)
1292 warntype
[cnt
] = QUOTA_NL_NOWARN
;
1293 down_read(&sb_dqopt(inode
->i_sb
)->dqptr_sem
);
1294 if (IS_NOQUOTA(inode
)) {
1295 up_read(&sb_dqopt(inode
->i_sb
)->dqptr_sem
);
1298 spin_lock(&dq_data_lock
);
1299 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++) {
1300 if (inode
->i_dquot
[cnt
] == NODQUOT
)
1302 if (check_idq(inode
->i_dquot
[cnt
], number
, warntype
+cnt
) == NO_QUOTA
)
1306 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++) {
1307 if (inode
->i_dquot
[cnt
] == NODQUOT
)
1309 dquot_incr_inodes(inode
->i_dquot
[cnt
], number
);
1313 spin_unlock(&dq_data_lock
);
1314 if (ret
== QUOTA_OK
)
1315 /* Dirtify all the dquots - this can block when journalling */
1316 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++)
1317 if (inode
->i_dquot
[cnt
])
1318 mark_dquot_dirty(inode
->i_dquot
[cnt
]);
1319 flush_warnings(inode
->i_dquot
, warntype
);
1320 up_read(&sb_dqopt(inode
->i_sb
)->dqptr_sem
);
1325 * This operation can block, but only after everything is updated
1327 int dquot_free_space(struct inode
*inode
, qsize_t number
)
1330 char warntype
[MAXQUOTAS
];
1332 /* First test before acquiring mutex - solves deadlocks when we
1333 * re-enter the quota code and are already holding the mutex */
1334 if (IS_NOQUOTA(inode
)) {
1336 inode_sub_bytes(inode
, number
);
1340 down_read(&sb_dqopt(inode
->i_sb
)->dqptr_sem
);
1341 /* Now recheck reliably when holding dqptr_sem */
1342 if (IS_NOQUOTA(inode
)) {
1343 up_read(&sb_dqopt(inode
->i_sb
)->dqptr_sem
);
1346 spin_lock(&dq_data_lock
);
1347 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++) {
1348 if (inode
->i_dquot
[cnt
] == NODQUOT
)
1350 warntype
[cnt
] = info_bdq_free(inode
->i_dquot
[cnt
], number
);
1351 dquot_decr_space(inode
->i_dquot
[cnt
], number
);
1353 inode_sub_bytes(inode
, number
);
1354 spin_unlock(&dq_data_lock
);
1355 /* Dirtify all the dquots - this can block when journalling */
1356 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++)
1357 if (inode
->i_dquot
[cnt
])
1358 mark_dquot_dirty(inode
->i_dquot
[cnt
]);
1359 flush_warnings(inode
->i_dquot
, warntype
);
1360 up_read(&sb_dqopt(inode
->i_sb
)->dqptr_sem
);
1365 * This operation can block, but only after everything is updated
1367 int dquot_free_inode(const struct inode
*inode
, unsigned long number
)
1370 char warntype
[MAXQUOTAS
];
1372 /* First test before acquiring mutex - solves deadlocks when we
1373 * re-enter the quota code and are already holding the mutex */
1374 if (IS_NOQUOTA(inode
))
1377 down_read(&sb_dqopt(inode
->i_sb
)->dqptr_sem
);
1378 /* Now recheck reliably when holding dqptr_sem */
1379 if (IS_NOQUOTA(inode
)) {
1380 up_read(&sb_dqopt(inode
->i_sb
)->dqptr_sem
);
1383 spin_lock(&dq_data_lock
);
1384 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++) {
1385 if (inode
->i_dquot
[cnt
] == NODQUOT
)
1387 warntype
[cnt
] = info_idq_free(inode
->i_dquot
[cnt
], number
);
1388 dquot_decr_inodes(inode
->i_dquot
[cnt
], number
);
1390 spin_unlock(&dq_data_lock
);
1391 /* Dirtify all the dquots - this can block when journalling */
1392 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++)
1393 if (inode
->i_dquot
[cnt
])
1394 mark_dquot_dirty(inode
->i_dquot
[cnt
]);
1395 flush_warnings(inode
->i_dquot
, warntype
);
1396 up_read(&sb_dqopt(inode
->i_sb
)->dqptr_sem
);
1401 * Transfer the number of inode and blocks from one diskquota to an other.
1403 * This operation can block, but only after everything is updated
1404 * A transaction must be started when entering this function.
1406 int dquot_transfer(struct inode
*inode
, struct iattr
*iattr
)
1409 struct dquot
*transfer_from
[MAXQUOTAS
];
1410 struct dquot
*transfer_to
[MAXQUOTAS
];
1411 int cnt
, ret
= NO_QUOTA
, chuid
= (iattr
->ia_valid
& ATTR_UID
) && inode
->i_uid
!= iattr
->ia_uid
,
1412 chgid
= (iattr
->ia_valid
& ATTR_GID
) && inode
->i_gid
!= iattr
->ia_gid
;
1413 char warntype_to
[MAXQUOTAS
];
1414 char warntype_from_inodes
[MAXQUOTAS
], warntype_from_space
[MAXQUOTAS
];
1416 /* First test before acquiring mutex - solves deadlocks when we
1417 * re-enter the quota code and are already holding the mutex */
1418 if (IS_NOQUOTA(inode
))
1420 /* Clear the arrays */
1421 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++) {
1422 transfer_to
[cnt
] = transfer_from
[cnt
] = NODQUOT
;
1423 warntype_to
[cnt
] = QUOTA_NL_NOWARN
;
1425 down_write(&sb_dqopt(inode
->i_sb
)->dqptr_sem
);
1426 /* Now recheck reliably when holding dqptr_sem */
1427 if (IS_NOQUOTA(inode
)) { /* File without quota accounting? */
1428 up_write(&sb_dqopt(inode
->i_sb
)->dqptr_sem
);
1431 /* First build the transfer_to list - here we can block on
1432 * reading/instantiating of dquots. We know that the transaction for
1433 * us was already started so we don't violate lock ranking here */
1434 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++) {
1439 transfer_to
[cnt
] = dqget(inode
->i_sb
, iattr
->ia_uid
, cnt
);
1444 transfer_to
[cnt
] = dqget(inode
->i_sb
, iattr
->ia_gid
, cnt
);
1448 spin_lock(&dq_data_lock
);
1449 space
= inode_get_bytes(inode
);
1450 /* Build the transfer_from list and check the limits */
1451 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++) {
1452 if (transfer_to
[cnt
] == NODQUOT
)
1454 transfer_from
[cnt
] = inode
->i_dquot
[cnt
];
1455 if (check_idq(transfer_to
[cnt
], 1, warntype_to
+ cnt
) ==
1456 NO_QUOTA
|| check_bdq(transfer_to
[cnt
], space
, 0,
1457 warntype_to
+ cnt
) == NO_QUOTA
)
1462 * Finally perform the needed transfer from transfer_from to transfer_to
1464 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++) {
1466 * Skip changes for same uid or gid or for turned off quota-type.
1468 if (transfer_to
[cnt
] == NODQUOT
)
1471 /* Due to IO error we might not have transfer_from[] structure */
1472 if (transfer_from
[cnt
]) {
1473 warntype_from_inodes
[cnt
] =
1474 info_idq_free(transfer_from
[cnt
], 1);
1475 warntype_from_space
[cnt
] =
1476 info_bdq_free(transfer_from
[cnt
], space
);
1477 dquot_decr_inodes(transfer_from
[cnt
], 1);
1478 dquot_decr_space(transfer_from
[cnt
], space
);
1481 dquot_incr_inodes(transfer_to
[cnt
], 1);
1482 dquot_incr_space(transfer_to
[cnt
], space
);
1484 inode
->i_dquot
[cnt
] = transfer_to
[cnt
];
1488 spin_unlock(&dq_data_lock
);
1489 /* Dirtify all the dquots - this can block when journalling */
1490 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++) {
1491 if (transfer_from
[cnt
])
1492 mark_dquot_dirty(transfer_from
[cnt
]);
1493 if (transfer_to
[cnt
])
1494 mark_dquot_dirty(transfer_to
[cnt
]);
1496 flush_warnings(transfer_to
, warntype_to
);
1497 flush_warnings(transfer_from
, warntype_from_inodes
);
1498 flush_warnings(transfer_from
, warntype_from_space
);
1500 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++) {
1501 if (ret
== QUOTA_OK
&& transfer_from
[cnt
] != NODQUOT
)
1502 dqput(transfer_from
[cnt
]);
1503 if (ret
== NO_QUOTA
&& transfer_to
[cnt
] != NODQUOT
)
1504 dqput(transfer_to
[cnt
]);
1506 up_write(&sb_dqopt(inode
->i_sb
)->dqptr_sem
);
1510 /* Wrapper for transferring ownership of an inode */
1511 int vfs_dq_transfer(struct inode
*inode
, struct iattr
*iattr
)
1513 if (sb_any_quota_enabled(inode
->i_sb
) && !IS_NOQUOTA(inode
)) {
1515 if (inode
->i_sb
->dq_op
->transfer(inode
, iattr
) == NO_QUOTA
)
1523 * Write info of quota file to disk
1525 int dquot_commit_info(struct super_block
*sb
, int type
)
1528 struct quota_info
*dqopt
= sb_dqopt(sb
);
1530 mutex_lock(&dqopt
->dqio_mutex
);
1531 ret
= dqopt
->ops
[type
]->write_file_info(sb
, type
);
1532 mutex_unlock(&dqopt
->dqio_mutex
);
1537 * Definitions of diskquota operations.
1539 struct dquot_operations dquot_operations
= {
1540 .initialize
= dquot_initialize
,
1542 .alloc_space
= dquot_alloc_space
,
1543 .alloc_inode
= dquot_alloc_inode
,
1544 .free_space
= dquot_free_space
,
1545 .free_inode
= dquot_free_inode
,
1546 .transfer
= dquot_transfer
,
1547 .write_dquot
= dquot_commit
,
1548 .acquire_dquot
= dquot_acquire
,
1549 .release_dquot
= dquot_release
,
1550 .mark_dirty
= dquot_mark_dquot_dirty
,
1551 .write_info
= dquot_commit_info
,
1552 .alloc_dquot
= dquot_alloc
,
1553 .destroy_dquot
= dquot_destroy
,
1556 static inline void set_enable_flags(struct quota_info
*dqopt
, int type
)
1560 dqopt
->flags
|= DQUOT_USR_ENABLED
;
1561 dqopt
->flags
&= ~DQUOT_USR_SUSPENDED
;
1564 dqopt
->flags
|= DQUOT_GRP_ENABLED
;
1565 dqopt
->flags
&= ~DQUOT_GRP_SUSPENDED
;
1570 static inline void reset_enable_flags(struct quota_info
*dqopt
, int type
,
1575 dqopt
->flags
&= ~DQUOT_USR_ENABLED
;
1577 dqopt
->flags
|= DQUOT_USR_SUSPENDED
;
1579 dqopt
->flags
&= ~DQUOT_USR_SUSPENDED
;
1582 dqopt
->flags
&= ~DQUOT_GRP_ENABLED
;
1584 dqopt
->flags
|= DQUOT_GRP_SUSPENDED
;
1586 dqopt
->flags
&= ~DQUOT_GRP_SUSPENDED
;
1593 * Turn quota off on a device. type == -1 ==> quotaoff for all types (umount)
1595 int vfs_quota_off(struct super_block
*sb
, int type
, int remount
)
1598 struct quota_info
*dqopt
= sb_dqopt(sb
);
1599 struct inode
*toputinode
[MAXQUOTAS
];
1601 /* We need to serialize quota_off() for device */
1602 mutex_lock(&dqopt
->dqonoff_mutex
);
1605 * Skip everything if there's nothing to do. We have to do this because
1606 * sometimes we are called when fill_super() failed and calling
1607 * sync_fs() in such cases does no good.
1609 if (!sb_any_quota_enabled(sb
) && !sb_any_quota_suspended(sb
)) {
1610 mutex_unlock(&dqopt
->dqonoff_mutex
);
1613 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++) {
1614 toputinode
[cnt
] = NULL
;
1615 if (type
!= -1 && cnt
!= type
)
1617 /* If we keep inodes of quota files after remount and quotaoff
1618 * is called, drop kept inodes. */
1619 if (!remount
&& sb_has_quota_suspended(sb
, cnt
)) {
1620 iput(dqopt
->files
[cnt
]);
1621 dqopt
->files
[cnt
] = NULL
;
1622 reset_enable_flags(dqopt
, cnt
, 0);
1625 if (!sb_has_quota_enabled(sb
, cnt
))
1627 reset_enable_flags(dqopt
, cnt
, remount
);
1629 /* Note: these are blocking operations */
1630 drop_dquot_ref(sb
, cnt
);
1631 invalidate_dquots(sb
, cnt
);
1633 * Now all dquots should be invalidated, all writes done so we should be only
1634 * users of the info. No locks needed.
1636 if (info_dirty(&dqopt
->info
[cnt
]))
1637 sb
->dq_op
->write_info(sb
, cnt
);
1638 if (dqopt
->ops
[cnt
]->free_file_info
)
1639 dqopt
->ops
[cnt
]->free_file_info(sb
, cnt
);
1640 put_quota_format(dqopt
->info
[cnt
].dqi_format
);
1642 toputinode
[cnt
] = dqopt
->files
[cnt
];
1644 dqopt
->files
[cnt
] = NULL
;
1645 dqopt
->info
[cnt
].dqi_flags
= 0;
1646 dqopt
->info
[cnt
].dqi_igrace
= 0;
1647 dqopt
->info
[cnt
].dqi_bgrace
= 0;
1648 dqopt
->ops
[cnt
] = NULL
;
1650 mutex_unlock(&dqopt
->dqonoff_mutex
);
1651 /* Sync the superblock so that buffers with quota data are written to
1652 * disk (and so userspace sees correct data afterwards). */
1653 if (sb
->s_op
->sync_fs
)
1654 sb
->s_op
->sync_fs(sb
, 1);
1655 sync_blockdev(sb
->s_bdev
);
1656 /* Now the quota files are just ordinary files and we can set the
1657 * inode flags back. Moreover we discard the pagecache so that
1658 * userspace sees the writes we did bypassing the pagecache. We
1659 * must also discard the blockdev buffers so that we see the
1660 * changes done by userspace on the next quotaon() */
1661 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++)
1662 if (toputinode
[cnt
]) {
1663 mutex_lock(&dqopt
->dqonoff_mutex
);
1664 /* If quota was reenabled in the meantime, we have
1666 if (!sb_has_quota_enabled(sb
, cnt
)) {
1667 mutex_lock_nested(&toputinode
[cnt
]->i_mutex
, I_MUTEX_QUOTA
);
1668 toputinode
[cnt
]->i_flags
&= ~(S_IMMUTABLE
|
1669 S_NOATIME
| S_NOQUOTA
);
1670 truncate_inode_pages(&toputinode
[cnt
]->i_data
, 0);
1671 mutex_unlock(&toputinode
[cnt
]->i_mutex
);
1672 mark_inode_dirty(toputinode
[cnt
]);
1674 mutex_unlock(&dqopt
->dqonoff_mutex
);
1675 /* On remount RO, we keep the inode pointer so that we
1676 * can reenable quota on the subsequent remount RW.
1677 * But we have better not keep inode pointer when there
1678 * is pending delete on the quota file... */
1680 iput(toputinode
[cnt
]);
1681 else if (!toputinode
[cnt
]->i_nlink
)
1685 invalidate_bdev(sb
->s_bdev
);
1690 * Turn quotas on on a device
1693 /* Helper function when we already have the inode */
1694 static int vfs_quota_on_inode(struct inode
*inode
, int type
, int format_id
)
1696 struct quota_format_type
*fmt
= find_quota_format(format_id
);
1697 struct super_block
*sb
= inode
->i_sb
;
1698 struct quota_info
*dqopt
= sb_dqopt(sb
);
1704 if (!S_ISREG(inode
->i_mode
)) {
1708 if (IS_RDONLY(inode
)) {
1712 if (!sb
->s_op
->quota_write
|| !sb
->s_op
->quota_read
) {
1717 /* As we bypass the pagecache we must now flush the inode so that
1718 * we see all the changes from userspace... */
1719 write_inode_now(inode
, 1);
1720 /* And now flush the block cache so that kernel sees the changes */
1721 invalidate_bdev(sb
->s_bdev
);
1722 mutex_lock(&inode
->i_mutex
);
1723 mutex_lock(&dqopt
->dqonoff_mutex
);
1724 if (sb_has_quota_enabled(sb
, type
) ||
1725 sb_has_quota_suspended(sb
, type
)) {
1729 /* We don't want quota and atime on quota files (deadlocks possible)
1730 * Also nobody should write to the file - we use special IO operations
1731 * which ignore the immutable bit. */
1732 down_write(&dqopt
->dqptr_sem
);
1733 oldflags
= inode
->i_flags
& (S_NOATIME
| S_IMMUTABLE
| S_NOQUOTA
);
1734 inode
->i_flags
|= S_NOQUOTA
| S_NOATIME
| S_IMMUTABLE
;
1735 up_write(&dqopt
->dqptr_sem
);
1736 sb
->dq_op
->drop(inode
);
1739 dqopt
->files
[type
] = igrab(inode
);
1740 if (!dqopt
->files
[type
])
1743 if (!fmt
->qf_ops
->check_quota_file(sb
, type
))
1746 dqopt
->ops
[type
] = fmt
->qf_ops
;
1747 dqopt
->info
[type
].dqi_format
= fmt
;
1748 dqopt
->info
[type
].dqi_fmt_id
= format_id
;
1749 INIT_LIST_HEAD(&dqopt
->info
[type
].dqi_dirty_list
);
1750 mutex_lock(&dqopt
->dqio_mutex
);
1751 if ((error
= dqopt
->ops
[type
]->read_file_info(sb
, type
)) < 0) {
1752 mutex_unlock(&dqopt
->dqio_mutex
);
1755 mutex_unlock(&dqopt
->dqio_mutex
);
1756 mutex_unlock(&inode
->i_mutex
);
1757 set_enable_flags(dqopt
, type
);
1759 add_dquot_ref(sb
, type
);
1760 mutex_unlock(&dqopt
->dqonoff_mutex
);
1765 dqopt
->files
[type
] = NULL
;
1768 mutex_unlock(&dqopt
->dqonoff_mutex
);
1769 if (oldflags
!= -1) {
1770 down_write(&dqopt
->dqptr_sem
);
1771 /* Set the flags back (in the case of accidental quotaon()
1772 * on a wrong file we don't want to mess up the flags) */
1773 inode
->i_flags
&= ~(S_NOATIME
| S_NOQUOTA
| S_IMMUTABLE
);
1774 inode
->i_flags
|= oldflags
;
1775 up_write(&dqopt
->dqptr_sem
);
1777 mutex_unlock(&inode
->i_mutex
);
1779 put_quota_format(fmt
);
1784 /* Reenable quotas on remount RW */
1785 static int vfs_quota_on_remount(struct super_block
*sb
, int type
)
1787 struct quota_info
*dqopt
= sb_dqopt(sb
);
1788 struct inode
*inode
;
1791 mutex_lock(&dqopt
->dqonoff_mutex
);
1792 if (!sb_has_quota_suspended(sb
, type
)) {
1793 mutex_unlock(&dqopt
->dqonoff_mutex
);
1796 BUG_ON(sb_has_quota_enabled(sb
, type
));
1798 inode
= dqopt
->files
[type
];
1799 dqopt
->files
[type
] = NULL
;
1800 reset_enable_flags(dqopt
, type
, 0);
1801 mutex_unlock(&dqopt
->dqonoff_mutex
);
1803 ret
= vfs_quota_on_inode(inode
, type
, dqopt
->info
[type
].dqi_fmt_id
);
1809 int vfs_quota_on_path(struct super_block
*sb
, int type
, int format_id
,
1812 int error
= security_quota_on(path
->dentry
);
1815 /* Quota file not on the same filesystem? */
1816 if (path
->mnt
->mnt_sb
!= sb
)
1819 error
= vfs_quota_on_inode(path
->dentry
->d_inode
, type
,
1824 /* Actual function called from quotactl() */
1825 int vfs_quota_on(struct super_block
*sb
, int type
, int format_id
, char *name
,
1832 return vfs_quota_on_remount(sb
, type
);
1834 error
= kern_path(name
, LOOKUP_FOLLOW
, &path
);
1836 error
= vfs_quota_on_path(sb
, type
, format_id
, &path
);
1843 * This function is used when filesystem needs to initialize quotas
1844 * during mount time.
1846 int vfs_quota_on_mount(struct super_block
*sb
, char *qf_name
,
1847 int format_id
, int type
)
1849 struct dentry
*dentry
;
1852 dentry
= lookup_one_len(qf_name
, sb
->s_root
, strlen(qf_name
));
1854 return PTR_ERR(dentry
);
1856 if (!dentry
->d_inode
) {
1861 error
= security_quota_on(dentry
);
1863 error
= vfs_quota_on_inode(dentry
->d_inode
, type
, format_id
);
1870 /* Wrapper to turn on quotas when remounting rw */
1871 int vfs_dq_quota_on_remount(struct super_block
*sb
)
1876 if (!sb
->s_qcop
|| !sb
->s_qcop
->quota_on
)
1878 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++) {
1879 err
= sb
->s_qcop
->quota_on(sb
, cnt
, 0, NULL
, 1);
1880 if (err
< 0 && !ret
)
1886 /* Generic routine for getting common part of quota structure */
1887 static void do_get_dqblk(struct dquot
*dquot
, struct if_dqblk
*di
)
1889 struct mem_dqblk
*dm
= &dquot
->dq_dqb
;
1891 spin_lock(&dq_data_lock
);
1892 di
->dqb_bhardlimit
= dm
->dqb_bhardlimit
;
1893 di
->dqb_bsoftlimit
= dm
->dqb_bsoftlimit
;
1894 di
->dqb_curspace
= dm
->dqb_curspace
;
1895 di
->dqb_ihardlimit
= dm
->dqb_ihardlimit
;
1896 di
->dqb_isoftlimit
= dm
->dqb_isoftlimit
;
1897 di
->dqb_curinodes
= dm
->dqb_curinodes
;
1898 di
->dqb_btime
= dm
->dqb_btime
;
1899 di
->dqb_itime
= dm
->dqb_itime
;
1900 di
->dqb_valid
= QIF_ALL
;
1901 spin_unlock(&dq_data_lock
);
1904 int vfs_get_dqblk(struct super_block
*sb
, int type
, qid_t id
, struct if_dqblk
*di
)
1906 struct dquot
*dquot
;
1908 mutex_lock(&sb_dqopt(sb
)->dqonoff_mutex
);
1909 if (!(dquot
= dqget(sb
, id
, type
))) {
1910 mutex_unlock(&sb_dqopt(sb
)->dqonoff_mutex
);
1913 do_get_dqblk(dquot
, di
);
1915 mutex_unlock(&sb_dqopt(sb
)->dqonoff_mutex
);
1919 /* Generic routine for setting common part of quota structure */
1920 static int do_set_dqblk(struct dquot
*dquot
, struct if_dqblk
*di
)
1922 struct mem_dqblk
*dm
= &dquot
->dq_dqb
;
1923 int check_blim
= 0, check_ilim
= 0;
1924 struct mem_dqinfo
*dqi
= &sb_dqopt(dquot
->dq_sb
)->info
[dquot
->dq_type
];
1926 if ((di
->dqb_valid
& QIF_BLIMITS
&&
1927 (di
->dqb_bhardlimit
> dqi
->dqi_maxblimit
||
1928 di
->dqb_bsoftlimit
> dqi
->dqi_maxblimit
)) ||
1929 (di
->dqb_valid
& QIF_ILIMITS
&&
1930 (di
->dqb_ihardlimit
> dqi
->dqi_maxilimit
||
1931 di
->dqb_isoftlimit
> dqi
->dqi_maxilimit
)))
1934 spin_lock(&dq_data_lock
);
1935 if (di
->dqb_valid
& QIF_SPACE
) {
1936 dm
->dqb_curspace
= di
->dqb_curspace
;
1939 if (di
->dqb_valid
& QIF_BLIMITS
) {
1940 dm
->dqb_bsoftlimit
= di
->dqb_bsoftlimit
;
1941 dm
->dqb_bhardlimit
= di
->dqb_bhardlimit
;
1944 if (di
->dqb_valid
& QIF_INODES
) {
1945 dm
->dqb_curinodes
= di
->dqb_curinodes
;
1948 if (di
->dqb_valid
& QIF_ILIMITS
) {
1949 dm
->dqb_isoftlimit
= di
->dqb_isoftlimit
;
1950 dm
->dqb_ihardlimit
= di
->dqb_ihardlimit
;
1953 if (di
->dqb_valid
& QIF_BTIME
)
1954 dm
->dqb_btime
= di
->dqb_btime
;
1955 if (di
->dqb_valid
& QIF_ITIME
)
1956 dm
->dqb_itime
= di
->dqb_itime
;
1959 if (!dm
->dqb_bsoftlimit
|| toqb(dm
->dqb_curspace
) < dm
->dqb_bsoftlimit
) {
1961 clear_bit(DQ_BLKS_B
, &dquot
->dq_flags
);
1963 else if (!(di
->dqb_valid
& QIF_BTIME
)) /* Set grace only if user hasn't provided his own... */
1964 dm
->dqb_btime
= get_seconds() + dqi
->dqi_bgrace
;
1967 if (!dm
->dqb_isoftlimit
|| dm
->dqb_curinodes
< dm
->dqb_isoftlimit
) {
1969 clear_bit(DQ_INODES_B
, &dquot
->dq_flags
);
1971 else if (!(di
->dqb_valid
& QIF_ITIME
)) /* Set grace only if user hasn't provided his own... */
1972 dm
->dqb_itime
= get_seconds() + dqi
->dqi_igrace
;
1974 if (dm
->dqb_bhardlimit
|| dm
->dqb_bsoftlimit
|| dm
->dqb_ihardlimit
|| dm
->dqb_isoftlimit
)
1975 clear_bit(DQ_FAKE_B
, &dquot
->dq_flags
);
1977 set_bit(DQ_FAKE_B
, &dquot
->dq_flags
);
1978 spin_unlock(&dq_data_lock
);
1979 mark_dquot_dirty(dquot
);
1984 int vfs_set_dqblk(struct super_block
*sb
, int type
, qid_t id
, struct if_dqblk
*di
)
1986 struct dquot
*dquot
;
1989 mutex_lock(&sb_dqopt(sb
)->dqonoff_mutex
);
1990 if (!(dquot
= dqget(sb
, id
, type
))) {
1991 mutex_unlock(&sb_dqopt(sb
)->dqonoff_mutex
);
1994 rc
= do_set_dqblk(dquot
, di
);
1996 mutex_unlock(&sb_dqopt(sb
)->dqonoff_mutex
);
2000 /* Generic routine for getting common part of quota file information */
2001 int vfs_get_dqinfo(struct super_block
*sb
, int type
, struct if_dqinfo
*ii
)
2003 struct mem_dqinfo
*mi
;
2005 mutex_lock(&sb_dqopt(sb
)->dqonoff_mutex
);
2006 if (!sb_has_quota_enabled(sb
, type
)) {
2007 mutex_unlock(&sb_dqopt(sb
)->dqonoff_mutex
);
2010 mi
= sb_dqopt(sb
)->info
+ type
;
2011 spin_lock(&dq_data_lock
);
2012 ii
->dqi_bgrace
= mi
->dqi_bgrace
;
2013 ii
->dqi_igrace
= mi
->dqi_igrace
;
2014 ii
->dqi_flags
= mi
->dqi_flags
& DQF_MASK
;
2015 ii
->dqi_valid
= IIF_ALL
;
2016 spin_unlock(&dq_data_lock
);
2017 mutex_unlock(&sb_dqopt(sb
)->dqonoff_mutex
);
2021 /* Generic routine for setting common part of quota file information */
2022 int vfs_set_dqinfo(struct super_block
*sb
, int type
, struct if_dqinfo
*ii
)
2024 struct mem_dqinfo
*mi
;
2026 mutex_lock(&sb_dqopt(sb
)->dqonoff_mutex
);
2027 if (!sb_has_quota_enabled(sb
, type
)) {
2028 mutex_unlock(&sb_dqopt(sb
)->dqonoff_mutex
);
2031 mi
= sb_dqopt(sb
)->info
+ type
;
2032 spin_lock(&dq_data_lock
);
2033 if (ii
->dqi_valid
& IIF_BGRACE
)
2034 mi
->dqi_bgrace
= ii
->dqi_bgrace
;
2035 if (ii
->dqi_valid
& IIF_IGRACE
)
2036 mi
->dqi_igrace
= ii
->dqi_igrace
;
2037 if (ii
->dqi_valid
& IIF_FLAGS
)
2038 mi
->dqi_flags
= (mi
->dqi_flags
& ~DQF_MASK
) | (ii
->dqi_flags
& DQF_MASK
);
2039 spin_unlock(&dq_data_lock
);
2040 mark_info_dirty(sb
, type
);
2041 /* Force write to disk */
2042 sb
->dq_op
->write_info(sb
, type
);
2043 mutex_unlock(&sb_dqopt(sb
)->dqonoff_mutex
);
2047 struct quotactl_ops vfs_quotactl_ops
= {
2048 .quota_on
= vfs_quota_on
,
2049 .quota_off
= vfs_quota_off
,
2050 .quota_sync
= vfs_quota_sync
,
2051 .get_info
= vfs_get_dqinfo
,
2052 .set_info
= vfs_set_dqinfo
,
2053 .get_dqblk
= vfs_get_dqblk
,
2054 .set_dqblk
= vfs_set_dqblk
2057 static ctl_table fs_dqstats_table
[] = {
2059 .ctl_name
= FS_DQ_LOOKUPS
,
2060 .procname
= "lookups",
2061 .data
= &dqstats
.lookups
,
2062 .maxlen
= sizeof(int),
2064 .proc_handler
= &proc_dointvec
,
2067 .ctl_name
= FS_DQ_DROPS
,
2068 .procname
= "drops",
2069 .data
= &dqstats
.drops
,
2070 .maxlen
= sizeof(int),
2072 .proc_handler
= &proc_dointvec
,
2075 .ctl_name
= FS_DQ_READS
,
2076 .procname
= "reads",
2077 .data
= &dqstats
.reads
,
2078 .maxlen
= sizeof(int),
2080 .proc_handler
= &proc_dointvec
,
2083 .ctl_name
= FS_DQ_WRITES
,
2084 .procname
= "writes",
2085 .data
= &dqstats
.writes
,
2086 .maxlen
= sizeof(int),
2088 .proc_handler
= &proc_dointvec
,
2091 .ctl_name
= FS_DQ_CACHE_HITS
,
2092 .procname
= "cache_hits",
2093 .data
= &dqstats
.cache_hits
,
2094 .maxlen
= sizeof(int),
2096 .proc_handler
= &proc_dointvec
,
2099 .ctl_name
= FS_DQ_ALLOCATED
,
2100 .procname
= "allocated_dquots",
2101 .data
= &dqstats
.allocated_dquots
,
2102 .maxlen
= sizeof(int),
2104 .proc_handler
= &proc_dointvec
,
2107 .ctl_name
= FS_DQ_FREE
,
2108 .procname
= "free_dquots",
2109 .data
= &dqstats
.free_dquots
,
2110 .maxlen
= sizeof(int),
2112 .proc_handler
= &proc_dointvec
,
2115 .ctl_name
= FS_DQ_SYNCS
,
2116 .procname
= "syncs",
2117 .data
= &dqstats
.syncs
,
2118 .maxlen
= sizeof(int),
2120 .proc_handler
= &proc_dointvec
,
2122 #ifdef CONFIG_PRINT_QUOTA_WARNING
2124 .ctl_name
= FS_DQ_WARNINGS
,
2125 .procname
= "warnings",
2126 .data
= &flag_print_warnings
,
2127 .maxlen
= sizeof(int),
2129 .proc_handler
= &proc_dointvec
,
2135 static ctl_table fs_table
[] = {
2137 .ctl_name
= FS_DQSTATS
,
2138 .procname
= "quota",
2140 .child
= fs_dqstats_table
,
2145 static ctl_table sys_table
[] = {
2155 static int __init
dquot_init(void)
2158 unsigned long nr_hash
, order
;
2160 printk(KERN_NOTICE
"VFS: Disk quotas %s\n", __DQUOT_VERSION__
);
2162 register_sysctl_table(sys_table
);
2164 dquot_cachep
= kmem_cache_create("dquot",
2165 sizeof(struct dquot
), sizeof(unsigned long) * 4,
2166 (SLAB_HWCACHE_ALIGN
|SLAB_RECLAIM_ACCOUNT
|
2167 SLAB_MEM_SPREAD
|SLAB_PANIC
),
2171 dquot_hash
= (struct hlist_head
*)__get_free_pages(GFP_ATOMIC
, order
);
2173 panic("Cannot create dquot hash table");
2175 /* Find power-of-two hlist_heads which can fit into allocation */
2176 nr_hash
= (1UL << order
) * PAGE_SIZE
/ sizeof(struct hlist_head
);
2180 } while (nr_hash
>> dq_hash_bits
);
2183 nr_hash
= 1UL << dq_hash_bits
;
2184 dq_hash_mask
= nr_hash
- 1;
2185 for (i
= 0; i
< nr_hash
; i
++)
2186 INIT_HLIST_HEAD(dquot_hash
+ i
);
2188 printk("Dquot-cache hash table entries: %ld (order %ld, %ld bytes)\n",
2189 nr_hash
, order
, (PAGE_SIZE
<< order
));
2191 register_shrinker(&dqcache_shrinker
);
2193 #ifdef CONFIG_QUOTA_NETLINK_INTERFACE
2194 if (genl_register_family("a_genl_family
) != 0)
2195 printk(KERN_ERR
"VFS: Failed to create quota netlink interface.\n");
2200 module_init(dquot_init
);
2202 EXPORT_SYMBOL(register_quota_format
);
2203 EXPORT_SYMBOL(unregister_quota_format
);
2204 EXPORT_SYMBOL(dqstats
);
2205 EXPORT_SYMBOL(dq_data_lock
);
2206 EXPORT_SYMBOL(vfs_quota_on
);
2207 EXPORT_SYMBOL(vfs_quota_on_path
);
2208 EXPORT_SYMBOL(vfs_quota_on_mount
);
2209 EXPORT_SYMBOL(vfs_quota_off
);
2210 EXPORT_SYMBOL(vfs_quota_sync
);
2211 EXPORT_SYMBOL(vfs_get_dqinfo
);
2212 EXPORT_SYMBOL(vfs_set_dqinfo
);
2213 EXPORT_SYMBOL(vfs_get_dqblk
);
2214 EXPORT_SYMBOL(vfs_set_dqblk
);
2215 EXPORT_SYMBOL(dquot_commit
);
2216 EXPORT_SYMBOL(dquot_commit_info
);
2217 EXPORT_SYMBOL(dquot_acquire
);
2218 EXPORT_SYMBOL(dquot_release
);
2219 EXPORT_SYMBOL(dquot_mark_dquot_dirty
);
2220 EXPORT_SYMBOL(dquot_initialize
);
2221 EXPORT_SYMBOL(dquot_drop
);
2222 EXPORT_SYMBOL(vfs_dq_drop
);
2223 EXPORT_SYMBOL(dquot_alloc_space
);
2224 EXPORT_SYMBOL(dquot_alloc_inode
);
2225 EXPORT_SYMBOL(dquot_free_space
);
2226 EXPORT_SYMBOL(dquot_free_inode
);
2227 EXPORT_SYMBOL(dquot_transfer
);
2228 EXPORT_SYMBOL(vfs_dq_transfer
);
2229 EXPORT_SYMBOL(vfs_dq_quota_on_remount
);