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 * Version: $Id: dquot.c,v 6.3 1996/11/17 18:35:34 mvw Exp mvw $
14 * Author: Marco van Wieringen <mvw@planets.elm.net>
16 * Fixes: Dmitry Gorodchanin <pgmdsg@ibi.com>, 11 Feb 96
18 * Revised list management to avoid races
19 * -- Bill Hawes, <whawes@star.net>, 9/98
21 * Fixed races in dquot_transfer(), dqget() and dquot_alloc_...().
22 * As the consequence the locking was moved from dquot_decr_...(),
23 * dquot_incr_...() to calling functions.
24 * invalidate_dquots() now writes modified dquots.
25 * Serialized quota_off() and quota_on() for mount point.
26 * Fixed a few bugs in grow_dquots().
27 * Fixed deadlock in write_dquot() - we no longer account quotas on
29 * remove_dquot_ref() moved to inode.c - it now traverses through inodes
30 * add_dquot_ref() restarts after blocking
31 * Added check for bogus uid and fixed check for group in quotactl.
32 * Jan Kara, <jack@suse.cz>, sponsored by SuSE CR, 10-11/99
34 * Used struct list_head instead of own list struct
35 * Invalidation of referenced dquots is no longer possible
36 * Improved free_dquots list management
37 * Quota and i_blocks are now updated in one place to avoid races
38 * Warnings are now delayed so we won't block in critical section
39 * Write updated not to require dquot lock
40 * Jan Kara, <jack@suse.cz>, 9/2000
42 * Added dynamic quota structure allocation
43 * Jan Kara <jack@suse.cz> 12/2000
45 * Rewritten quota interface. Implemented new quota format and
46 * formats registering.
47 * Jan Kara, <jack@suse.cz>, 2001,2002
50 * Jan Kara, <jack@suse.cz>, 10/2002
52 * Added journalled quota support, fix lock inversion problems
53 * Jan Kara, <jack@suse.cz>, 2003,2004
55 * (C) Copyright 1994 - 1997 Marco van Wieringen
58 #include <linux/errno.h>
59 #include <linux/kernel.h>
61 #include <linux/mount.h>
63 #include <linux/time.h>
64 #include <linux/types.h>
65 #include <linux/string.h>
66 #include <linux/fcntl.h>
67 #include <linux/stat.h>
68 #include <linux/tty.h>
69 #include <linux/file.h>
70 #include <linux/slab.h>
71 #include <linux/sysctl.h>
72 #include <linux/smp_lock.h>
73 #include <linux/init.h>
74 #include <linux/module.h>
75 #include <linux/proc_fs.h>
76 #include <linux/security.h>
77 #include <linux/kmod.h>
78 #include <linux/namei.h>
79 #include <linux/buffer_head.h>
81 #include <asm/uaccess.h>
83 #define __DQUOT_PARANOIA
86 * There are two quota SMP locks. dq_list_lock protects all lists with quotas
87 * and quota formats and also dqstats structure containing statistics about the
88 * lists. dq_data_lock protects data from dq_dqb and also mem_dqinfo structures
89 * and 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().
93 * The spinlock ordering is hence: dq_data_lock > dq_list_lock > i_lock
95 * Note that some things (eg. sb pointer, type, id) doesn't change during
96 * the life of the dquot structure and so needn't to be protected by a lock
98 * Any operation working on dquots via inode pointers must hold dqptr_sem. If
99 * operation is just reading pointers from inode (or not using them at all) the
100 * read lock is enough. If pointers are altered function must hold write lock
101 * (these locking rules also apply for S_NOQUOTA flag in the inode - note that
102 * for altering the flag i_sem is also needed). If operation is holding
103 * reference to dquot in other way (e.g. quotactl ops) it must be guarded by
105 * This locking assures that:
106 * a) update/access to dquot pointers in inode is serialized
107 * b) everyone is guarded against invalidate_dquots()
109 * Each dquot has its dq_lock semaphore. Locked dquots might not be referenced
110 * from inodes (dquot_alloc_space() and such don't check the dq_lock).
111 * Currently dquot is locked only when it is being read to memory (or space for
112 * it is being allocated) on the first dqget() and when it is being released on
113 * the last dqput(). The allocation and release oparations are serialized by
114 * the dq_lock and by checking the use count in dquot_release(). Write
115 * operations on dquots don't hold dq_lock as they copy data under dq_data_lock
116 * spinlock to internal buffers before writing.
118 * Lock ordering (including related VFS locks) is the following:
119 * i_sem > dqonoff_sem > iprune_sem > journal_lock > dqptr_sem >
120 * > dquot->dq_lock > dqio_sem
121 * i_sem on quota files is special (it's below dqio_sem)
124 static DEFINE_SPINLOCK(dq_list_lock
);
125 DEFINE_SPINLOCK(dq_data_lock
);
127 static char *quotatypes
[] = INITQFNAMES
;
128 static struct quota_format_type
*quota_formats
; /* List of registered formats */
129 static struct quota_module_name module_names
[] = INIT_QUOTA_MODULE_NAMES
;
131 /* SLAB cache for dquot structures */
132 static kmem_cache_t
*dquot_cachep
;
134 int register_quota_format(struct quota_format_type
*fmt
)
136 spin_lock(&dq_list_lock
);
137 fmt
->qf_next
= quota_formats
;
139 spin_unlock(&dq_list_lock
);
143 void unregister_quota_format(struct quota_format_type
*fmt
)
145 struct quota_format_type
**actqf
;
147 spin_lock(&dq_list_lock
);
148 for (actqf
= "a_formats
; *actqf
&& *actqf
!= fmt
; actqf
= &(*actqf
)->qf_next
);
150 *actqf
= (*actqf
)->qf_next
;
151 spin_unlock(&dq_list_lock
);
154 static struct quota_format_type
*find_quota_format(int id
)
156 struct quota_format_type
*actqf
;
158 spin_lock(&dq_list_lock
);
159 for (actqf
= quota_formats
; actqf
&& actqf
->qf_fmt_id
!= id
; actqf
= actqf
->qf_next
);
160 if (!actqf
|| !try_module_get(actqf
->qf_owner
)) {
163 spin_unlock(&dq_list_lock
);
165 for (qm
= 0; module_names
[qm
].qm_fmt_id
&& module_names
[qm
].qm_fmt_id
!= id
; qm
++);
166 if (!module_names
[qm
].qm_fmt_id
|| request_module(module_names
[qm
].qm_mod_name
))
169 spin_lock(&dq_list_lock
);
170 for (actqf
= quota_formats
; actqf
&& actqf
->qf_fmt_id
!= id
; actqf
= actqf
->qf_next
);
171 if (actqf
&& !try_module_get(actqf
->qf_owner
))
174 spin_unlock(&dq_list_lock
);
178 static void put_quota_format(struct quota_format_type
*fmt
)
180 module_put(fmt
->qf_owner
);
184 * Dquot List Management:
185 * The quota code uses three lists for dquot management: the inuse_list,
186 * free_dquots, and dquot_hash[] array. A single dquot structure may be
187 * on all three lists, depending on its current state.
189 * All dquots are placed to the end of inuse_list when first created, and this
190 * list is used for invalidate operation, which must look at every dquot.
192 * Unused dquots (dq_count == 0) are added to the free_dquots list when freed,
193 * and this list is searched whenever we need an available dquot. Dquots are
194 * removed from the list as soon as they are used again, and
195 * dqstats.free_dquots gives the number of dquots on the list. When
196 * dquot is invalidated it's completely released from memory.
198 * Dquots with a specific identity (device, type and id) are placed on
199 * one of the dquot_hash[] hash chains. The provides an efficient search
200 * mechanism to locate a specific dquot.
203 static LIST_HEAD(inuse_list
);
204 static LIST_HEAD(free_dquots
);
205 static unsigned int dq_hash_bits
, dq_hash_mask
;
206 static struct hlist_head
*dquot_hash
;
208 struct dqstats dqstats
;
210 static void dqput(struct dquot
*dquot
);
212 static inline unsigned int
213 hashfn(const struct super_block
*sb
, unsigned int id
, int type
)
217 tmp
= (((unsigned long)sb
>>L1_CACHE_SHIFT
) ^ id
) * (MAXQUOTAS
- type
);
218 return (tmp
+ (tmp
>> dq_hash_bits
)) & dq_hash_mask
;
222 * Following list functions expect dq_list_lock to be held
224 static inline void insert_dquot_hash(struct dquot
*dquot
)
226 struct hlist_head
*head
= dquot_hash
+ hashfn(dquot
->dq_sb
, dquot
->dq_id
, dquot
->dq_type
);
227 hlist_add_head(&dquot
->dq_hash
, head
);
230 static inline void remove_dquot_hash(struct dquot
*dquot
)
232 hlist_del_init(&dquot
->dq_hash
);
235 static inline struct dquot
*find_dquot(unsigned int hashent
, struct super_block
*sb
, unsigned int id
, int type
)
237 struct hlist_node
*node
;
240 hlist_for_each (node
, dquot_hash
+hashent
) {
241 dquot
= hlist_entry(node
, struct dquot
, dq_hash
);
242 if (dquot
->dq_sb
== sb
&& dquot
->dq_id
== id
&& dquot
->dq_type
== type
)
248 /* Add a dquot to the tail of the free list */
249 static inline void put_dquot_last(struct dquot
*dquot
)
251 list_add(&dquot
->dq_free
, free_dquots
.prev
);
252 dqstats
.free_dquots
++;
255 static inline void remove_free_dquot(struct dquot
*dquot
)
257 if (list_empty(&dquot
->dq_free
))
259 list_del_init(&dquot
->dq_free
);
260 dqstats
.free_dquots
--;
263 static inline void put_inuse(struct dquot
*dquot
)
265 /* We add to the back of inuse list so we don't have to restart
266 * when traversing this list and we block */
267 list_add(&dquot
->dq_inuse
, inuse_list
.prev
);
268 dqstats
.allocated_dquots
++;
271 static inline void remove_inuse(struct dquot
*dquot
)
273 dqstats
.allocated_dquots
--;
274 list_del(&dquot
->dq_inuse
);
277 * End of list functions needing dq_list_lock
280 static void wait_on_dquot(struct dquot
*dquot
)
282 down(&dquot
->dq_lock
);
286 #define mark_dquot_dirty(dquot) ((dquot)->dq_sb->dq_op->mark_dirty(dquot))
288 int dquot_mark_dquot_dirty(struct dquot
*dquot
)
290 spin_lock(&dq_list_lock
);
291 if (!test_and_set_bit(DQ_MOD_B
, &dquot
->dq_flags
))
292 list_add(&dquot
->dq_dirty
, &sb_dqopt(dquot
->dq_sb
)->
293 info
[dquot
->dq_type
].dqi_dirty_list
);
294 spin_unlock(&dq_list_lock
);
298 /* This function needs dq_list_lock */
299 static inline int clear_dquot_dirty(struct dquot
*dquot
)
301 if (!test_and_clear_bit(DQ_MOD_B
, &dquot
->dq_flags
))
303 list_del_init(&dquot
->dq_dirty
);
307 void mark_info_dirty(struct super_block
*sb
, int type
)
309 set_bit(DQF_INFO_DIRTY_B
, &sb_dqopt(sb
)->info
[type
].dqi_flags
);
311 EXPORT_SYMBOL(mark_info_dirty
);
314 * Read dquot from disk and alloc space for it
317 int dquot_acquire(struct dquot
*dquot
)
319 int ret
= 0, ret2
= 0;
320 struct quota_info
*dqopt
= sb_dqopt(dquot
->dq_sb
);
322 down(&dquot
->dq_lock
);
323 down(&dqopt
->dqio_sem
);
324 if (!test_bit(DQ_READ_B
, &dquot
->dq_flags
))
325 ret
= dqopt
->ops
[dquot
->dq_type
]->read_dqblk(dquot
);
328 set_bit(DQ_READ_B
, &dquot
->dq_flags
);
329 /* Instantiate dquot if needed */
330 if (!test_bit(DQ_ACTIVE_B
, &dquot
->dq_flags
) && !dquot
->dq_off
) {
331 ret
= dqopt
->ops
[dquot
->dq_type
]->commit_dqblk(dquot
);
332 /* Write the info if needed */
333 if (info_dirty(&dqopt
->info
[dquot
->dq_type
]))
334 ret2
= dqopt
->ops
[dquot
->dq_type
]->write_file_info(dquot
->dq_sb
, dquot
->dq_type
);
342 set_bit(DQ_ACTIVE_B
, &dquot
->dq_flags
);
344 up(&dqopt
->dqio_sem
);
350 * Write dquot to disk
352 int dquot_commit(struct dquot
*dquot
)
354 int ret
= 0, ret2
= 0;
355 struct quota_info
*dqopt
= sb_dqopt(dquot
->dq_sb
);
357 down(&dqopt
->dqio_sem
);
358 spin_lock(&dq_list_lock
);
359 if (!clear_dquot_dirty(dquot
)) {
360 spin_unlock(&dq_list_lock
);
363 spin_unlock(&dq_list_lock
);
364 /* Inactive dquot can be only if there was error during read/init
365 * => we have better not writing it */
366 if (test_bit(DQ_ACTIVE_B
, &dquot
->dq_flags
)) {
367 ret
= dqopt
->ops
[dquot
->dq_type
]->commit_dqblk(dquot
);
368 if (info_dirty(&dqopt
->info
[dquot
->dq_type
]))
369 ret2
= dqopt
->ops
[dquot
->dq_type
]->write_file_info(dquot
->dq_sb
, dquot
->dq_type
);
374 up(&dqopt
->dqio_sem
);
381 int dquot_release(struct dquot
*dquot
)
383 int ret
= 0, ret2
= 0;
384 struct quota_info
*dqopt
= sb_dqopt(dquot
->dq_sb
);
386 down(&dquot
->dq_lock
);
387 /* Check whether we are not racing with some other dqget() */
388 if (atomic_read(&dquot
->dq_count
) > 1)
390 down(&dqopt
->dqio_sem
);
391 if (dqopt
->ops
[dquot
->dq_type
]->release_dqblk
) {
392 ret
= dqopt
->ops
[dquot
->dq_type
]->release_dqblk(dquot
);
394 if (info_dirty(&dqopt
->info
[dquot
->dq_type
]))
395 ret2
= dqopt
->ops
[dquot
->dq_type
]->write_file_info(dquot
->dq_sb
, dquot
->dq_type
);
399 clear_bit(DQ_ACTIVE_B
, &dquot
->dq_flags
);
400 up(&dqopt
->dqio_sem
);
406 /* Invalidate all dquots on the list. Note that this function is called after
407 * quota is disabled and pointers from inodes removed so there cannot be new
408 * quota users. Also because we hold dqonoff_sem there can be no quota users
409 * for this sb+type at all. */
410 static void invalidate_dquots(struct super_block
*sb
, int type
)
413 struct list_head
*head
;
415 spin_lock(&dq_list_lock
);
416 for (head
= inuse_list
.next
; head
!= &inuse_list
;) {
417 dquot
= list_entry(head
, struct dquot
, dq_inuse
);
419 if (dquot
->dq_sb
!= sb
)
421 if (dquot
->dq_type
!= type
)
423 #ifdef __DQUOT_PARANOIA
424 if (atomic_read(&dquot
->dq_count
))
427 /* Quota now has no users and it has been written on last dqput() */
428 remove_dquot_hash(dquot
);
429 remove_free_dquot(dquot
);
431 kmem_cache_free(dquot_cachep
, dquot
);
433 spin_unlock(&dq_list_lock
);
436 int vfs_quota_sync(struct super_block
*sb
, int type
)
438 struct list_head
*dirty
;
440 struct quota_info
*dqopt
= sb_dqopt(sb
);
443 down(&dqopt
->dqonoff_sem
);
444 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++) {
445 if (type
!= -1 && cnt
!= type
)
447 if (!sb_has_quota_enabled(sb
, cnt
))
449 spin_lock(&dq_list_lock
);
450 dirty
= &dqopt
->info
[cnt
].dqi_dirty_list
;
451 while (!list_empty(dirty
)) {
452 dquot
= list_entry(dirty
->next
, struct dquot
, dq_dirty
);
453 /* Dirty and inactive can be only bad dquot... */
454 if (!test_bit(DQ_ACTIVE_B
, &dquot
->dq_flags
)) {
455 clear_dquot_dirty(dquot
);
458 /* Now we have active dquot from which someone is
459 * holding reference so we can safely just increase
461 atomic_inc(&dquot
->dq_count
);
463 spin_unlock(&dq_list_lock
);
464 sb
->dq_op
->write_dquot(dquot
);
466 spin_lock(&dq_list_lock
);
468 spin_unlock(&dq_list_lock
);
471 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++)
472 if ((cnt
== type
|| type
== -1) && sb_has_quota_enabled(sb
, cnt
)
473 && info_dirty(&dqopt
->info
[cnt
]))
474 sb
->dq_op
->write_info(sb
, cnt
);
475 spin_lock(&dq_list_lock
);
477 spin_unlock(&dq_list_lock
);
478 up(&dqopt
->dqonoff_sem
);
483 /* Free unused dquots from cache */
484 static void prune_dqcache(int count
)
486 struct list_head
*head
;
489 head
= free_dquots
.prev
;
490 while (head
!= &free_dquots
&& count
) {
491 dquot
= list_entry(head
, struct dquot
, dq_free
);
492 remove_dquot_hash(dquot
);
493 remove_free_dquot(dquot
);
495 kmem_cache_free(dquot_cachep
, dquot
);
497 head
= free_dquots
.prev
;
502 * This is called from kswapd when we think we need some
506 static int shrink_dqcache_memory(int nr
, unsigned int gfp_mask
)
509 spin_lock(&dq_list_lock
);
511 spin_unlock(&dq_list_lock
);
513 return (dqstats
.free_dquots
/ 100) * sysctl_vfs_cache_pressure
;
517 * Put reference to dquot
518 * NOTE: If you change this function please check whether dqput_blocks() works right...
519 * MUST be called with either dqptr_sem or dqonoff_sem held
521 static void dqput(struct dquot
*dquot
)
525 #ifdef __DQUOT_PARANOIA
526 if (!atomic_read(&dquot
->dq_count
)) {
527 printk("VFS: dqput: trying to free free dquot\n");
528 printk("VFS: device %s, dquot of %s %d\n",
530 quotatypes
[dquot
->dq_type
],
536 spin_lock(&dq_list_lock
);
538 spin_unlock(&dq_list_lock
);
540 spin_lock(&dq_list_lock
);
541 if (atomic_read(&dquot
->dq_count
) > 1) {
542 /* We have more than one user... nothing to do */
543 atomic_dec(&dquot
->dq_count
);
544 spin_unlock(&dq_list_lock
);
547 /* Need to release dquot? */
548 if (test_bit(DQ_ACTIVE_B
, &dquot
->dq_flags
) && dquot_dirty(dquot
)) {
549 spin_unlock(&dq_list_lock
);
550 /* Commit dquot before releasing */
551 dquot
->dq_sb
->dq_op
->write_dquot(dquot
);
554 /* Clear flag in case dquot was inactive (something bad happened) */
555 clear_dquot_dirty(dquot
);
556 if (test_bit(DQ_ACTIVE_B
, &dquot
->dq_flags
)) {
557 spin_unlock(&dq_list_lock
);
558 dquot
->dq_sb
->dq_op
->release_dquot(dquot
);
561 atomic_dec(&dquot
->dq_count
);
562 #ifdef __DQUOT_PARANOIA
564 if (!list_empty(&dquot
->dq_free
))
567 put_dquot_last(dquot
);
568 spin_unlock(&dq_list_lock
);
571 static struct dquot
*get_empty_dquot(struct super_block
*sb
, int type
)
575 dquot
= kmem_cache_alloc(dquot_cachep
, SLAB_NOFS
);
579 memset((caddr_t
)dquot
, 0, sizeof(struct dquot
));
580 sema_init(&dquot
->dq_lock
, 1);
581 INIT_LIST_HEAD(&dquot
->dq_free
);
582 INIT_LIST_HEAD(&dquot
->dq_inuse
);
583 INIT_HLIST_NODE(&dquot
->dq_hash
);
584 INIT_LIST_HEAD(&dquot
->dq_dirty
);
586 dquot
->dq_type
= type
;
587 atomic_set(&dquot
->dq_count
, 1);
593 * Get reference to dquot
594 * MUST be called with either dqptr_sem or dqonoff_sem held
596 static struct dquot
*dqget(struct super_block
*sb
, unsigned int id
, int type
)
598 unsigned int hashent
= hashfn(sb
, id
, type
);
599 struct dquot
*dquot
, *empty
= NODQUOT
;
601 if (!sb_has_quota_enabled(sb
, type
))
604 spin_lock(&dq_list_lock
);
605 if ((dquot
= find_dquot(hashent
, sb
, id
, type
)) == NODQUOT
) {
606 if (empty
== NODQUOT
) {
607 spin_unlock(&dq_list_lock
);
608 if ((empty
= get_empty_dquot(sb
, type
)) == NODQUOT
)
609 schedule(); /* Try to wait for a moment... */
614 /* all dquots go on the inuse_list */
616 /* hash it first so it can be found */
617 insert_dquot_hash(dquot
);
619 spin_unlock(&dq_list_lock
);
621 if (!atomic_read(&dquot
->dq_count
))
622 remove_free_dquot(dquot
);
623 atomic_inc(&dquot
->dq_count
);
624 dqstats
.cache_hits
++;
626 spin_unlock(&dq_list_lock
);
628 kmem_cache_free(dquot_cachep
, empty
);
630 /* Wait for dq_lock - after this we know that either dquot_release() is already
631 * finished or it will be canceled due to dq_count > 1 test */
632 wait_on_dquot(dquot
);
633 /* Read the dquot and instantiate it (everything done only if needed) */
634 if (!test_bit(DQ_ACTIVE_B
, &dquot
->dq_flags
) && sb
->dq_op
->acquire_dquot(dquot
) < 0) {
638 #ifdef __DQUOT_PARANOIA
639 if (!dquot
->dq_sb
) /* Has somebody invalidated entry under us? */
646 static int dqinit_needed(struct inode
*inode
, int type
)
650 if (IS_NOQUOTA(inode
))
653 return inode
->i_dquot
[type
] == NODQUOT
;
654 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++)
655 if (inode
->i_dquot
[cnt
] == NODQUOT
)
660 /* This routine is guarded by dqonoff_sem semaphore */
661 static void add_dquot_ref(struct super_block
*sb
, int type
)
667 list_for_each(p
, &sb
->s_files
) {
668 struct file
*filp
= list_entry(p
, struct file
, f_list
);
669 struct inode
*inode
= filp
->f_dentry
->d_inode
;
670 if (filp
->f_mode
& FMODE_WRITE
&& dqinit_needed(inode
, type
)) {
671 struct dentry
*dentry
= dget(filp
->f_dentry
);
673 sb
->dq_op
->initialize(inode
, type
);
675 /* As we may have blocked we had better restart... */
682 /* Return 0 if dqput() won't block (note that 1 doesn't necessarily mean blocking) */
683 static inline int dqput_blocks(struct dquot
*dquot
)
685 if (atomic_read(&dquot
->dq_count
) <= 1)
690 /* Remove references to dquots from inode - add dquot to list for freeing if needed */
691 /* We can't race with anybody because we hold dqptr_sem for writing... */
692 int remove_inode_dquot_ref(struct inode
*inode
, int type
, struct list_head
*tofree_head
)
694 struct dquot
*dquot
= inode
->i_dquot
[type
];
696 inode
->i_dquot
[type
] = NODQUOT
;
697 if (dquot
!= NODQUOT
) {
698 if (dqput_blocks(dquot
)) {
699 #ifdef __DQUOT_PARANOIA
700 if (atomic_read(&dquot
->dq_count
) != 1)
701 printk(KERN_WARNING
"VFS: Adding dquot with dq_count %d to dispose list.\n", atomic_read(&dquot
->dq_count
));
703 spin_lock(&dq_list_lock
);
704 list_add(&dquot
->dq_free
, tofree_head
); /* As dquot must have currently users it can't be on the free list... */
705 spin_unlock(&dq_list_lock
);
709 dqput(dquot
); /* We have guaranteed we won't block */
714 /* Free list of dquots - called from inode.c */
715 /* dquots are removed from inodes, no new references can be got so we are the only ones holding reference */
716 static void put_dquot_list(struct list_head
*tofree_head
)
718 struct list_head
*act_head
;
721 act_head
= tofree_head
->next
;
722 /* So now we have dquots on the list... Just free them */
723 while (act_head
!= tofree_head
) {
724 dquot
= list_entry(act_head
, struct dquot
, dq_free
);
725 act_head
= act_head
->next
;
726 list_del_init(&dquot
->dq_free
); /* Remove dquot from the list so we won't have problems... */
731 /* Gather all references from inodes and drop them */
732 static void drop_dquot_ref(struct super_block
*sb
, int type
)
734 LIST_HEAD(tofree_head
);
736 /* We need to be guarded against prune_icache to reach all the
737 * inodes - otherwise some can be on the local list of prune_icache */
739 down_write(&sb_dqopt(sb
)->dqptr_sem
);
740 remove_dquot_ref(sb
, type
, &tofree_head
);
741 up_write(&sb_dqopt(sb
)->dqptr_sem
);
743 put_dquot_list(&tofree_head
);
746 static inline void dquot_incr_inodes(struct dquot
*dquot
, unsigned long number
)
748 dquot
->dq_dqb
.dqb_curinodes
+= number
;
751 static inline void dquot_incr_space(struct dquot
*dquot
, qsize_t number
)
753 dquot
->dq_dqb
.dqb_curspace
+= number
;
756 static inline void dquot_decr_inodes(struct dquot
*dquot
, unsigned long number
)
758 if (dquot
->dq_dqb
.dqb_curinodes
> number
)
759 dquot
->dq_dqb
.dqb_curinodes
-= number
;
761 dquot
->dq_dqb
.dqb_curinodes
= 0;
762 if (dquot
->dq_dqb
.dqb_curinodes
<= dquot
->dq_dqb
.dqb_isoftlimit
)
763 dquot
->dq_dqb
.dqb_itime
= (time_t) 0;
764 clear_bit(DQ_INODES_B
, &dquot
->dq_flags
);
767 static inline void dquot_decr_space(struct dquot
*dquot
, qsize_t number
)
769 if (dquot
->dq_dqb
.dqb_curspace
> number
)
770 dquot
->dq_dqb
.dqb_curspace
-= number
;
772 dquot
->dq_dqb
.dqb_curspace
= 0;
773 if (toqb(dquot
->dq_dqb
.dqb_curspace
) <= dquot
->dq_dqb
.dqb_bsoftlimit
)
774 dquot
->dq_dqb
.dqb_btime
= (time_t) 0;
775 clear_bit(DQ_BLKS_B
, &dquot
->dq_flags
);
778 static int flag_print_warnings
= 1;
780 static inline int need_print_warning(struct dquot
*dquot
)
782 if (!flag_print_warnings
)
785 switch (dquot
->dq_type
) {
787 return current
->fsuid
== dquot
->dq_id
;
789 return in_group_p(dquot
->dq_id
);
794 /* Values of warnings */
797 #define ISOFTLONGWARN 2
800 #define BSOFTLONGWARN 5
803 /* Print warning to user which exceeded quota */
804 static void print_warning(struct dquot
*dquot
, const char warntype
)
807 int flag
= (warntype
== BHARDWARN
|| warntype
== BSOFTLONGWARN
) ? DQ_BLKS_B
:
808 ((warntype
== IHARDWARN
|| warntype
== ISOFTLONGWARN
) ? DQ_INODES_B
: 0);
810 if (!need_print_warning(dquot
) || (flag
&& test_and_set_bit(flag
, &dquot
->dq_flags
)))
813 tty_write_message(current
->signal
->tty
, dquot
->dq_sb
->s_id
);
814 if (warntype
== ISOFTWARN
|| warntype
== BSOFTWARN
)
815 tty_write_message(current
->signal
->tty
, ": warning, ");
817 tty_write_message(current
->signal
->tty
, ": write failed, ");
818 tty_write_message(current
->signal
->tty
, quotatypes
[dquot
->dq_type
]);
821 msg
= " file limit reached.\r\n";
824 msg
= " file quota exceeded too long.\r\n";
827 msg
= " file quota exceeded.\r\n";
830 msg
= " block limit reached.\r\n";
833 msg
= " block quota exceeded too long.\r\n";
836 msg
= " block quota exceeded.\r\n";
839 tty_write_message(current
->signal
->tty
, msg
);
842 static inline void flush_warnings(struct dquot
**dquots
, char *warntype
)
846 for (i
= 0; i
< MAXQUOTAS
; i
++)
847 if (dquots
[i
] != NODQUOT
&& warntype
[i
] != NOWARN
)
848 print_warning(dquots
[i
], warntype
[i
]);
851 static inline char ignore_hardlimit(struct dquot
*dquot
)
853 struct mem_dqinfo
*info
= &sb_dqopt(dquot
->dq_sb
)->info
[dquot
->dq_type
];
855 return capable(CAP_SYS_RESOURCE
) &&
856 (info
->dqi_format
->qf_fmt_id
!= QFMT_VFS_OLD
|| !(info
->dqi_flags
& V1_DQF_RSQUASH
));
859 /* needs dq_data_lock */
860 static int check_idq(struct dquot
*dquot
, ulong inodes
, char *warntype
)
863 if (inodes
<= 0 || test_bit(DQ_FAKE_B
, &dquot
->dq_flags
))
866 if (dquot
->dq_dqb
.dqb_ihardlimit
&&
867 (dquot
->dq_dqb
.dqb_curinodes
+ inodes
) > dquot
->dq_dqb
.dqb_ihardlimit
&&
868 !ignore_hardlimit(dquot
)) {
869 *warntype
= IHARDWARN
;
873 if (dquot
->dq_dqb
.dqb_isoftlimit
&&
874 (dquot
->dq_dqb
.dqb_curinodes
+ inodes
) > dquot
->dq_dqb
.dqb_isoftlimit
&&
875 dquot
->dq_dqb
.dqb_itime
&& get_seconds() >= dquot
->dq_dqb
.dqb_itime
&&
876 !ignore_hardlimit(dquot
)) {
877 *warntype
= ISOFTLONGWARN
;
881 if (dquot
->dq_dqb
.dqb_isoftlimit
&&
882 (dquot
->dq_dqb
.dqb_curinodes
+ inodes
) > dquot
->dq_dqb
.dqb_isoftlimit
&&
883 dquot
->dq_dqb
.dqb_itime
== 0) {
884 *warntype
= ISOFTWARN
;
885 dquot
->dq_dqb
.dqb_itime
= get_seconds() + sb_dqopt(dquot
->dq_sb
)->info
[dquot
->dq_type
].dqi_igrace
;
891 /* needs dq_data_lock */
892 static int check_bdq(struct dquot
*dquot
, qsize_t space
, int prealloc
, char *warntype
)
895 if (space
<= 0 || test_bit(DQ_FAKE_B
, &dquot
->dq_flags
))
898 if (dquot
->dq_dqb
.dqb_bhardlimit
&&
899 toqb(dquot
->dq_dqb
.dqb_curspace
+ space
) > dquot
->dq_dqb
.dqb_bhardlimit
&&
900 !ignore_hardlimit(dquot
)) {
902 *warntype
= BHARDWARN
;
906 if (dquot
->dq_dqb
.dqb_bsoftlimit
&&
907 toqb(dquot
->dq_dqb
.dqb_curspace
+ space
) > dquot
->dq_dqb
.dqb_bsoftlimit
&&
908 dquot
->dq_dqb
.dqb_btime
&& get_seconds() >= dquot
->dq_dqb
.dqb_btime
&&
909 !ignore_hardlimit(dquot
)) {
911 *warntype
= BSOFTLONGWARN
;
915 if (dquot
->dq_dqb
.dqb_bsoftlimit
&&
916 toqb(dquot
->dq_dqb
.dqb_curspace
+ space
) > dquot
->dq_dqb
.dqb_bsoftlimit
&&
917 dquot
->dq_dqb
.dqb_btime
== 0) {
919 *warntype
= BSOFTWARN
;
920 dquot
->dq_dqb
.dqb_btime
= get_seconds() + sb_dqopt(dquot
->dq_sb
)->info
[dquot
->dq_type
].dqi_bgrace
;
924 * We don't allow preallocation to exceed softlimit so exceeding will
934 * Initialize quota pointers in inode
935 * Transaction must be started at entry
937 int dquot_initialize(struct inode
*inode
, int type
)
942 /* First test before acquiring semaphore - solves deadlocks when we
943 * re-enter the quota code and are already holding the semaphore */
944 if (IS_NOQUOTA(inode
))
946 down_write(&sb_dqopt(inode
->i_sb
)->dqptr_sem
);
947 /* Having dqptr_sem we know NOQUOTA flags can't be altered... */
948 if (IS_NOQUOTA(inode
))
950 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++) {
951 if (type
!= -1 && cnt
!= type
)
953 if (inode
->i_dquot
[cnt
] == NODQUOT
) {
962 inode
->i_dquot
[cnt
] = dqget(inode
->i_sb
, id
, cnt
);
966 up_write(&sb_dqopt(inode
->i_sb
)->dqptr_sem
);
971 * Release all quotas referenced by inode
972 * Transaction must be started at an entry
974 int dquot_drop(struct inode
*inode
)
978 down_write(&sb_dqopt(inode
->i_sb
)->dqptr_sem
);
979 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++) {
980 if (inode
->i_dquot
[cnt
] != NODQUOT
) {
981 dqput(inode
->i_dquot
[cnt
]);
982 inode
->i_dquot
[cnt
] = NODQUOT
;
985 up_write(&sb_dqopt(inode
->i_sb
)->dqptr_sem
);
990 * Following four functions update i_blocks+i_bytes fields and
991 * quota information (together with appropriate checks)
992 * NOTE: We absolutely rely on the fact that caller dirties
993 * the inode (usually macros in quotaops.h care about this) and
994 * holds a handle for the current transaction so that dquot write and
995 * inode write go into the same transaction.
999 * This operation can block, but only after everything is updated
1001 int dquot_alloc_space(struct inode
*inode
, qsize_t number
, int warn
)
1003 int cnt
, ret
= NO_QUOTA
;
1004 char warntype
[MAXQUOTAS
];
1006 /* First test before acquiring semaphore - solves deadlocks when we
1007 * re-enter the quota code and are already holding the semaphore */
1008 if (IS_NOQUOTA(inode
)) {
1010 inode_add_bytes(inode
, number
);
1013 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++)
1014 warntype
[cnt
] = NOWARN
;
1016 down_read(&sb_dqopt(inode
->i_sb
)->dqptr_sem
);
1017 if (IS_NOQUOTA(inode
)) { /* Now we can do reliable test... */
1018 up_read(&sb_dqopt(inode
->i_sb
)->dqptr_sem
);
1021 spin_lock(&dq_data_lock
);
1022 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++) {
1023 if (inode
->i_dquot
[cnt
] == NODQUOT
)
1025 if (check_bdq(inode
->i_dquot
[cnt
], number
, warn
, warntype
+cnt
) == NO_QUOTA
)
1028 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++) {
1029 if (inode
->i_dquot
[cnt
] == NODQUOT
)
1031 dquot_incr_space(inode
->i_dquot
[cnt
], number
);
1033 inode_add_bytes(inode
, number
);
1036 spin_unlock(&dq_data_lock
);
1037 if (ret
== QUOTA_OK
)
1038 /* Dirtify all the dquots - this can block when journalling */
1039 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++)
1040 if (inode
->i_dquot
[cnt
])
1041 mark_dquot_dirty(inode
->i_dquot
[cnt
]);
1042 flush_warnings(inode
->i_dquot
, warntype
);
1043 up_read(&sb_dqopt(inode
->i_sb
)->dqptr_sem
);
1048 * This operation can block, but only after everything is updated
1050 int dquot_alloc_inode(const struct inode
*inode
, unsigned long number
)
1052 int cnt
, ret
= NO_QUOTA
;
1053 char warntype
[MAXQUOTAS
];
1055 /* First test before acquiring semaphore - solves deadlocks when we
1056 * re-enter the quota code and are already holding the semaphore */
1057 if (IS_NOQUOTA(inode
))
1059 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++)
1060 warntype
[cnt
] = NOWARN
;
1061 down_read(&sb_dqopt(inode
->i_sb
)->dqptr_sem
);
1062 if (IS_NOQUOTA(inode
)) {
1063 up_read(&sb_dqopt(inode
->i_sb
)->dqptr_sem
);
1066 spin_lock(&dq_data_lock
);
1067 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++) {
1068 if (inode
->i_dquot
[cnt
] == NODQUOT
)
1070 if (check_idq(inode
->i_dquot
[cnt
], number
, warntype
+cnt
) == NO_QUOTA
)
1074 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++) {
1075 if (inode
->i_dquot
[cnt
] == NODQUOT
)
1077 dquot_incr_inodes(inode
->i_dquot
[cnt
], number
);
1081 spin_unlock(&dq_data_lock
);
1082 if (ret
== QUOTA_OK
)
1083 /* Dirtify all the dquots - this can block when journalling */
1084 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++)
1085 if (inode
->i_dquot
[cnt
])
1086 mark_dquot_dirty(inode
->i_dquot
[cnt
]);
1087 flush_warnings((struct dquot
**)inode
->i_dquot
, warntype
);
1088 up_read(&sb_dqopt(inode
->i_sb
)->dqptr_sem
);
1093 * This operation can block, but only after everything is updated
1095 int dquot_free_space(struct inode
*inode
, qsize_t number
)
1099 /* First test before acquiring semaphore - solves deadlocks when we
1100 * re-enter the quota code and are already holding the semaphore */
1101 if (IS_NOQUOTA(inode
)) {
1103 inode_sub_bytes(inode
, number
);
1106 down_read(&sb_dqopt(inode
->i_sb
)->dqptr_sem
);
1107 /* Now recheck reliably when holding dqptr_sem */
1108 if (IS_NOQUOTA(inode
)) {
1109 up_read(&sb_dqopt(inode
->i_sb
)->dqptr_sem
);
1112 spin_lock(&dq_data_lock
);
1113 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++) {
1114 if (inode
->i_dquot
[cnt
] == NODQUOT
)
1116 dquot_decr_space(inode
->i_dquot
[cnt
], number
);
1118 inode_sub_bytes(inode
, number
);
1119 spin_unlock(&dq_data_lock
);
1120 /* Dirtify all the dquots - this can block when journalling */
1121 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++)
1122 if (inode
->i_dquot
[cnt
])
1123 mark_dquot_dirty(inode
->i_dquot
[cnt
]);
1124 up_read(&sb_dqopt(inode
->i_sb
)->dqptr_sem
);
1129 * This operation can block, but only after everything is updated
1131 int dquot_free_inode(const struct inode
*inode
, unsigned long number
)
1135 /* First test before acquiring semaphore - solves deadlocks when we
1136 * re-enter the quota code and are already holding the semaphore */
1137 if (IS_NOQUOTA(inode
))
1139 down_read(&sb_dqopt(inode
->i_sb
)->dqptr_sem
);
1140 /* Now recheck reliably when holding dqptr_sem */
1141 if (IS_NOQUOTA(inode
)) {
1142 up_read(&sb_dqopt(inode
->i_sb
)->dqptr_sem
);
1145 spin_lock(&dq_data_lock
);
1146 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++) {
1147 if (inode
->i_dquot
[cnt
] == NODQUOT
)
1149 dquot_decr_inodes(inode
->i_dquot
[cnt
], number
);
1151 spin_unlock(&dq_data_lock
);
1152 /* Dirtify all the dquots - this can block when journalling */
1153 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++)
1154 if (inode
->i_dquot
[cnt
])
1155 mark_dquot_dirty(inode
->i_dquot
[cnt
]);
1156 up_read(&sb_dqopt(inode
->i_sb
)->dqptr_sem
);
1161 * Transfer the number of inode and blocks from one diskquota to an other.
1163 * This operation can block, but only after everything is updated
1164 * A transaction must be started when entering this function.
1166 int dquot_transfer(struct inode
*inode
, struct iattr
*iattr
)
1169 struct dquot
*transfer_from
[MAXQUOTAS
];
1170 struct dquot
*transfer_to
[MAXQUOTAS
];
1171 int cnt
, ret
= NO_QUOTA
, chuid
= (iattr
->ia_valid
& ATTR_UID
) && inode
->i_uid
!= iattr
->ia_uid
,
1172 chgid
= (iattr
->ia_valid
& ATTR_GID
) && inode
->i_gid
!= iattr
->ia_gid
;
1173 char warntype
[MAXQUOTAS
];
1175 /* First test before acquiring semaphore - solves deadlocks when we
1176 * re-enter the quota code and are already holding the semaphore */
1177 if (IS_NOQUOTA(inode
))
1179 /* Clear the arrays */
1180 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++) {
1181 transfer_to
[cnt
] = transfer_from
[cnt
] = NODQUOT
;
1182 warntype
[cnt
] = NOWARN
;
1184 down_write(&sb_dqopt(inode
->i_sb
)->dqptr_sem
);
1185 /* Now recheck reliably when holding dqptr_sem */
1186 if (IS_NOQUOTA(inode
)) { /* File without quota accounting? */
1187 up_write(&sb_dqopt(inode
->i_sb
)->dqptr_sem
);
1190 /* First build the transfer_to list - here we can block on
1191 * reading/instantiating of dquots. We know that the transaction for
1192 * us was already started so we don't violate lock ranking here */
1193 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++) {
1198 transfer_to
[cnt
] = dqget(inode
->i_sb
, iattr
->ia_uid
, cnt
);
1203 transfer_to
[cnt
] = dqget(inode
->i_sb
, iattr
->ia_gid
, cnt
);
1207 spin_lock(&dq_data_lock
);
1208 space
= inode_get_bytes(inode
);
1209 /* Build the transfer_from list and check the limits */
1210 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++) {
1211 if (transfer_to
[cnt
] == NODQUOT
)
1213 transfer_from
[cnt
] = inode
->i_dquot
[cnt
];
1214 if (check_idq(transfer_to
[cnt
], 1, warntype
+cnt
) == NO_QUOTA
||
1215 check_bdq(transfer_to
[cnt
], space
, 0, warntype
+cnt
) == NO_QUOTA
)
1220 * Finally perform the needed transfer from transfer_from to transfer_to
1222 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++) {
1224 * Skip changes for same uid or gid or for turned off quota-type.
1226 if (transfer_to
[cnt
] == NODQUOT
)
1229 /* Due to IO error we might not have transfer_from[] structure */
1230 if (transfer_from
[cnt
]) {
1231 dquot_decr_inodes(transfer_from
[cnt
], 1);
1232 dquot_decr_space(transfer_from
[cnt
], space
);
1235 dquot_incr_inodes(transfer_to
[cnt
], 1);
1236 dquot_incr_space(transfer_to
[cnt
], space
);
1238 inode
->i_dquot
[cnt
] = transfer_to
[cnt
];
1242 spin_unlock(&dq_data_lock
);
1243 /* Dirtify all the dquots - this can block when journalling */
1244 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++) {
1245 if (transfer_from
[cnt
])
1246 mark_dquot_dirty(transfer_from
[cnt
]);
1247 if (transfer_to
[cnt
])
1248 mark_dquot_dirty(transfer_to
[cnt
]);
1250 flush_warnings(transfer_to
, warntype
);
1252 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++) {
1253 if (ret
== QUOTA_OK
&& transfer_from
[cnt
] != NODQUOT
)
1254 dqput(transfer_from
[cnt
]);
1255 if (ret
== NO_QUOTA
&& transfer_to
[cnt
] != NODQUOT
)
1256 dqput(transfer_to
[cnt
]);
1258 up_write(&sb_dqopt(inode
->i_sb
)->dqptr_sem
);
1263 * Write info of quota file to disk
1265 int dquot_commit_info(struct super_block
*sb
, int type
)
1268 struct quota_info
*dqopt
= sb_dqopt(sb
);
1270 down(&dqopt
->dqio_sem
);
1271 ret
= dqopt
->ops
[type
]->write_file_info(sb
, type
);
1272 up(&dqopt
->dqio_sem
);
1277 * Definitions of diskquota operations.
1279 struct dquot_operations dquot_operations
= {
1280 .initialize
= dquot_initialize
,
1282 .alloc_space
= dquot_alloc_space
,
1283 .alloc_inode
= dquot_alloc_inode
,
1284 .free_space
= dquot_free_space
,
1285 .free_inode
= dquot_free_inode
,
1286 .transfer
= dquot_transfer
,
1287 .write_dquot
= dquot_commit
,
1288 .acquire_dquot
= dquot_acquire
,
1289 .release_dquot
= dquot_release
,
1290 .mark_dirty
= dquot_mark_dquot_dirty
,
1291 .write_info
= dquot_commit_info
1294 static inline void set_enable_flags(struct quota_info
*dqopt
, int type
)
1298 dqopt
->flags
|= DQUOT_USR_ENABLED
;
1301 dqopt
->flags
|= DQUOT_GRP_ENABLED
;
1306 static inline void reset_enable_flags(struct quota_info
*dqopt
, int type
)
1310 dqopt
->flags
&= ~DQUOT_USR_ENABLED
;
1313 dqopt
->flags
&= ~DQUOT_GRP_ENABLED
;
1319 * Turn quota off on a device. type == -1 ==> quotaoff for all types (umount)
1321 int vfs_quota_off(struct super_block
*sb
, int type
)
1324 struct quota_info
*dqopt
= sb_dqopt(sb
);
1325 struct inode
*toputinode
[MAXQUOTAS
];
1326 struct vfsmount
*toputmnt
[MAXQUOTAS
];
1328 /* We need to serialize quota_off() for device */
1329 down(&dqopt
->dqonoff_sem
);
1330 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++) {
1331 toputinode
[cnt
] = NULL
;
1332 toputmnt
[cnt
] = NULL
;
1333 if (type
!= -1 && cnt
!= type
)
1335 if (!sb_has_quota_enabled(sb
, cnt
))
1337 reset_enable_flags(dqopt
, cnt
);
1339 /* Note: these are blocking operations */
1340 drop_dquot_ref(sb
, cnt
);
1341 invalidate_dquots(sb
, cnt
);
1343 * Now all dquots should be invalidated, all writes done so we should be only
1344 * users of the info. No locks needed.
1346 if (info_dirty(&dqopt
->info
[cnt
]))
1347 sb
->dq_op
->write_info(sb
, cnt
);
1348 if (dqopt
->ops
[cnt
]->free_file_info
)
1349 dqopt
->ops
[cnt
]->free_file_info(sb
, cnt
);
1350 put_quota_format(dqopt
->info
[cnt
].dqi_format
);
1352 toputinode
[cnt
] = dqopt
->files
[cnt
];
1353 toputmnt
[cnt
] = dqopt
->mnt
[cnt
];
1354 dqopt
->files
[cnt
] = NULL
;
1355 dqopt
->mnt
[cnt
] = NULL
;
1356 dqopt
->info
[cnt
].dqi_flags
= 0;
1357 dqopt
->info
[cnt
].dqi_igrace
= 0;
1358 dqopt
->info
[cnt
].dqi_bgrace
= 0;
1359 dqopt
->ops
[cnt
] = NULL
;
1361 up(&dqopt
->dqonoff_sem
);
1362 /* Sync the superblock so that buffers with quota data are written to
1363 * disk (and so userspace sees correct data afterwards).
1364 * The reference to vfsmnt we are still holding protects us from
1365 * umount (we don't have it only when quotas are turned on/off for
1366 * journal replay but in that case we are guarded by the fs anyway). */
1367 if (sb
->s_op
->sync_fs
)
1368 sb
->s_op
->sync_fs(sb
, 1);
1369 sync_blockdev(sb
->s_bdev
);
1370 /* Now the quota files are just ordinary files and we can set the
1371 * inode flags back. Moreover we discard the pagecache so that
1372 * userspace sees the writes we did bypassing the pagecache. We
1373 * must also discard the blockdev buffers so that we see the
1374 * changes done by userspace on the next quotaon() */
1375 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++)
1376 if (toputinode
[cnt
]) {
1377 down(&dqopt
->dqonoff_sem
);
1378 /* If quota was reenabled in the meantime, we have
1380 if (!sb_has_quota_enabled(sb
, cnt
)) {
1381 down(&toputinode
[cnt
]->i_sem
);
1382 toputinode
[cnt
]->i_flags
&= ~(S_IMMUTABLE
|
1383 S_NOATIME
| S_NOQUOTA
);
1384 truncate_inode_pages(&toputinode
[cnt
]->i_data
, 0);
1385 up(&toputinode
[cnt
]->i_sem
);
1386 mark_inode_dirty(toputinode
[cnt
]);
1387 iput(toputinode
[cnt
]);
1389 up(&dqopt
->dqonoff_sem
);
1390 /* We don't hold the reference when we turned on quotas
1391 * just for the journal replay... */
1393 mntput(toputmnt
[cnt
]);
1396 invalidate_bdev(sb
->s_bdev
, 0);
1401 * Turn quotas on on a device
1404 /* Helper function when we already have the inode */
1405 static int vfs_quota_on_inode(struct inode
*inode
, int type
, int format_id
)
1407 struct quota_format_type
*fmt
= find_quota_format(format_id
);
1408 struct super_block
*sb
= inode
->i_sb
;
1409 struct quota_info
*dqopt
= sb_dqopt(sb
);
1415 if (!S_ISREG(inode
->i_mode
)) {
1419 if (IS_RDONLY(inode
)) {
1423 if (!sb
->s_op
->quota_write
|| !sb
->s_op
->quota_read
) {
1428 /* As we bypass the pagecache we must now flush the inode so that
1429 * we see all the changes from userspace... */
1430 write_inode_now(inode
, 1);
1431 /* And now flush the block cache so that kernel sees the changes */
1432 invalidate_bdev(sb
->s_bdev
, 0);
1433 down(&inode
->i_sem
);
1434 down(&dqopt
->dqonoff_sem
);
1435 if (sb_has_quota_enabled(sb
, type
)) {
1439 /* We don't want quota and atime on quota files (deadlocks possible)
1440 * Also nobody should write to the file - we use special IO operations
1441 * which ignore the immutable bit. */
1442 down_write(&dqopt
->dqptr_sem
);
1443 oldflags
= inode
->i_flags
& (S_NOATIME
| S_IMMUTABLE
| S_NOQUOTA
);
1444 inode
->i_flags
|= S_NOQUOTA
| S_NOATIME
| S_IMMUTABLE
;
1445 up_write(&dqopt
->dqptr_sem
);
1446 sb
->dq_op
->drop(inode
);
1449 dqopt
->files
[type
] = igrab(inode
);
1450 if (!dqopt
->files
[type
])
1453 if (!fmt
->qf_ops
->check_quota_file(sb
, type
))
1456 dqopt
->ops
[type
] = fmt
->qf_ops
;
1457 dqopt
->info
[type
].dqi_format
= fmt
;
1458 INIT_LIST_HEAD(&dqopt
->info
[type
].dqi_dirty_list
);
1459 down(&dqopt
->dqio_sem
);
1460 if ((error
= dqopt
->ops
[type
]->read_file_info(sb
, type
)) < 0) {
1461 up(&dqopt
->dqio_sem
);
1464 up(&dqopt
->dqio_sem
);
1466 set_enable_flags(dqopt
, type
);
1468 add_dquot_ref(sb
, type
);
1469 up(&dqopt
->dqonoff_sem
);
1474 dqopt
->files
[type
] = NULL
;
1477 up(&dqopt
->dqonoff_sem
);
1478 if (oldflags
!= -1) {
1479 down_write(&dqopt
->dqptr_sem
);
1480 /* Set the flags back (in the case of accidental quotaon()
1481 * on a wrong file we don't want to mess up the flags) */
1482 inode
->i_flags
&= ~(S_NOATIME
| S_NOQUOTA
| S_IMMUTABLE
);
1483 inode
->i_flags
|= oldflags
;
1484 up_write(&dqopt
->dqptr_sem
);
1488 put_quota_format(fmt
);
1493 /* Actual function called from quotactl() */
1494 int vfs_quota_on(struct super_block
*sb
, int type
, int format_id
, char *path
)
1496 struct nameidata nd
;
1499 error
= path_lookup(path
, LOOKUP_FOLLOW
, &nd
);
1502 error
= security_quota_on(nd
.dentry
);
1505 /* Quota file not on the same filesystem? */
1506 if (nd
.mnt
->mnt_sb
!= sb
)
1509 error
= vfs_quota_on_inode(nd
.dentry
->d_inode
, type
, format_id
);
1511 sb_dqopt(sb
)->mnt
[type
] = mntget(nd
.mnt
);
1519 * This function is used when filesystem needs to initialize quotas
1520 * during mount time.
1522 int vfs_quota_on_mount(struct super_block
*sb
, char *qf_name
,
1523 int format_id
, int type
)
1525 struct dentry
*dentry
;
1528 dentry
= lookup_one_len(qf_name
, sb
->s_root
, strlen(qf_name
));
1530 return PTR_ERR(dentry
);
1532 error
= security_quota_on(dentry
);
1534 error
= vfs_quota_on_inode(dentry
->d_inode
, type
, format_id
);
1540 /* Generic routine for getting common part of quota structure */
1541 static void do_get_dqblk(struct dquot
*dquot
, struct if_dqblk
*di
)
1543 struct mem_dqblk
*dm
= &dquot
->dq_dqb
;
1545 spin_lock(&dq_data_lock
);
1546 di
->dqb_bhardlimit
= dm
->dqb_bhardlimit
;
1547 di
->dqb_bsoftlimit
= dm
->dqb_bsoftlimit
;
1548 di
->dqb_curspace
= dm
->dqb_curspace
;
1549 di
->dqb_ihardlimit
= dm
->dqb_ihardlimit
;
1550 di
->dqb_isoftlimit
= dm
->dqb_isoftlimit
;
1551 di
->dqb_curinodes
= dm
->dqb_curinodes
;
1552 di
->dqb_btime
= dm
->dqb_btime
;
1553 di
->dqb_itime
= dm
->dqb_itime
;
1554 di
->dqb_valid
= QIF_ALL
;
1555 spin_unlock(&dq_data_lock
);
1558 int vfs_get_dqblk(struct super_block
*sb
, int type
, qid_t id
, struct if_dqblk
*di
)
1560 struct dquot
*dquot
;
1562 down(&sb_dqopt(sb
)->dqonoff_sem
);
1563 if (!(dquot
= dqget(sb
, id
, type
))) {
1564 up(&sb_dqopt(sb
)->dqonoff_sem
);
1567 do_get_dqblk(dquot
, di
);
1569 up(&sb_dqopt(sb
)->dqonoff_sem
);
1573 /* Generic routine for setting common part of quota structure */
1574 static void do_set_dqblk(struct dquot
*dquot
, struct if_dqblk
*di
)
1576 struct mem_dqblk
*dm
= &dquot
->dq_dqb
;
1577 int check_blim
= 0, check_ilim
= 0;
1579 spin_lock(&dq_data_lock
);
1580 if (di
->dqb_valid
& QIF_SPACE
) {
1581 dm
->dqb_curspace
= di
->dqb_curspace
;
1584 if (di
->dqb_valid
& QIF_BLIMITS
) {
1585 dm
->dqb_bsoftlimit
= di
->dqb_bsoftlimit
;
1586 dm
->dqb_bhardlimit
= di
->dqb_bhardlimit
;
1589 if (di
->dqb_valid
& QIF_INODES
) {
1590 dm
->dqb_curinodes
= di
->dqb_curinodes
;
1593 if (di
->dqb_valid
& QIF_ILIMITS
) {
1594 dm
->dqb_isoftlimit
= di
->dqb_isoftlimit
;
1595 dm
->dqb_ihardlimit
= di
->dqb_ihardlimit
;
1598 if (di
->dqb_valid
& QIF_BTIME
)
1599 dm
->dqb_btime
= di
->dqb_btime
;
1600 if (di
->dqb_valid
& QIF_ITIME
)
1601 dm
->dqb_itime
= di
->dqb_itime
;
1604 if (!dm
->dqb_bsoftlimit
|| toqb(dm
->dqb_curspace
) < dm
->dqb_bsoftlimit
) {
1606 clear_bit(DQ_BLKS_B
, &dquot
->dq_flags
);
1608 else if (!(di
->dqb_valid
& QIF_BTIME
)) /* Set grace only if user hasn't provided his own... */
1609 dm
->dqb_btime
= get_seconds() + sb_dqopt(dquot
->dq_sb
)->info
[dquot
->dq_type
].dqi_bgrace
;
1612 if (!dm
->dqb_isoftlimit
|| dm
->dqb_curinodes
< dm
->dqb_isoftlimit
) {
1614 clear_bit(DQ_INODES_B
, &dquot
->dq_flags
);
1616 else if (!(di
->dqb_valid
& QIF_ITIME
)) /* Set grace only if user hasn't provided his own... */
1617 dm
->dqb_itime
= get_seconds() + sb_dqopt(dquot
->dq_sb
)->info
[dquot
->dq_type
].dqi_igrace
;
1619 if (dm
->dqb_bhardlimit
|| dm
->dqb_bsoftlimit
|| dm
->dqb_ihardlimit
|| dm
->dqb_isoftlimit
)
1620 clear_bit(DQ_FAKE_B
, &dquot
->dq_flags
);
1622 set_bit(DQ_FAKE_B
, &dquot
->dq_flags
);
1623 spin_unlock(&dq_data_lock
);
1624 mark_dquot_dirty(dquot
);
1627 int vfs_set_dqblk(struct super_block
*sb
, int type
, qid_t id
, struct if_dqblk
*di
)
1629 struct dquot
*dquot
;
1631 down(&sb_dqopt(sb
)->dqonoff_sem
);
1632 if (!(dquot
= dqget(sb
, id
, type
))) {
1633 up(&sb_dqopt(sb
)->dqonoff_sem
);
1636 do_set_dqblk(dquot
, di
);
1638 up(&sb_dqopt(sb
)->dqonoff_sem
);
1642 /* Generic routine for getting common part of quota file information */
1643 int vfs_get_dqinfo(struct super_block
*sb
, int type
, struct if_dqinfo
*ii
)
1645 struct mem_dqinfo
*mi
;
1647 down(&sb_dqopt(sb
)->dqonoff_sem
);
1648 if (!sb_has_quota_enabled(sb
, type
)) {
1649 up(&sb_dqopt(sb
)->dqonoff_sem
);
1652 mi
= sb_dqopt(sb
)->info
+ type
;
1653 spin_lock(&dq_data_lock
);
1654 ii
->dqi_bgrace
= mi
->dqi_bgrace
;
1655 ii
->dqi_igrace
= mi
->dqi_igrace
;
1656 ii
->dqi_flags
= mi
->dqi_flags
& DQF_MASK
;
1657 ii
->dqi_valid
= IIF_ALL
;
1658 spin_unlock(&dq_data_lock
);
1659 up(&sb_dqopt(sb
)->dqonoff_sem
);
1663 /* Generic routine for setting common part of quota file information */
1664 int vfs_set_dqinfo(struct super_block
*sb
, int type
, struct if_dqinfo
*ii
)
1666 struct mem_dqinfo
*mi
;
1668 down(&sb_dqopt(sb
)->dqonoff_sem
);
1669 if (!sb_has_quota_enabled(sb
, type
)) {
1670 up(&sb_dqopt(sb
)->dqonoff_sem
);
1673 mi
= sb_dqopt(sb
)->info
+ type
;
1674 spin_lock(&dq_data_lock
);
1675 if (ii
->dqi_valid
& IIF_BGRACE
)
1676 mi
->dqi_bgrace
= ii
->dqi_bgrace
;
1677 if (ii
->dqi_valid
& IIF_IGRACE
)
1678 mi
->dqi_igrace
= ii
->dqi_igrace
;
1679 if (ii
->dqi_valid
& IIF_FLAGS
)
1680 mi
->dqi_flags
= (mi
->dqi_flags
& ~DQF_MASK
) | (ii
->dqi_flags
& DQF_MASK
);
1681 spin_unlock(&dq_data_lock
);
1682 mark_info_dirty(sb
, type
);
1683 /* Force write to disk */
1684 sb
->dq_op
->write_info(sb
, type
);
1685 up(&sb_dqopt(sb
)->dqonoff_sem
);
1689 struct quotactl_ops vfs_quotactl_ops
= {
1690 .quota_on
= vfs_quota_on
,
1691 .quota_off
= vfs_quota_off
,
1692 .quota_sync
= vfs_quota_sync
,
1693 .get_info
= vfs_get_dqinfo
,
1694 .set_info
= vfs_set_dqinfo
,
1695 .get_dqblk
= vfs_get_dqblk
,
1696 .set_dqblk
= vfs_set_dqblk
1699 static ctl_table fs_dqstats_table
[] = {
1701 .ctl_name
= FS_DQ_LOOKUPS
,
1702 .procname
= "lookups",
1703 .data
= &dqstats
.lookups
,
1704 .maxlen
= sizeof(int),
1706 .proc_handler
= &proc_dointvec
,
1709 .ctl_name
= FS_DQ_DROPS
,
1710 .procname
= "drops",
1711 .data
= &dqstats
.drops
,
1712 .maxlen
= sizeof(int),
1714 .proc_handler
= &proc_dointvec
,
1717 .ctl_name
= FS_DQ_READS
,
1718 .procname
= "reads",
1719 .data
= &dqstats
.reads
,
1720 .maxlen
= sizeof(int),
1722 .proc_handler
= &proc_dointvec
,
1725 .ctl_name
= FS_DQ_WRITES
,
1726 .procname
= "writes",
1727 .data
= &dqstats
.writes
,
1728 .maxlen
= sizeof(int),
1730 .proc_handler
= &proc_dointvec
,
1733 .ctl_name
= FS_DQ_CACHE_HITS
,
1734 .procname
= "cache_hits",
1735 .data
= &dqstats
.cache_hits
,
1736 .maxlen
= sizeof(int),
1738 .proc_handler
= &proc_dointvec
,
1741 .ctl_name
= FS_DQ_ALLOCATED
,
1742 .procname
= "allocated_dquots",
1743 .data
= &dqstats
.allocated_dquots
,
1744 .maxlen
= sizeof(int),
1746 .proc_handler
= &proc_dointvec
,
1749 .ctl_name
= FS_DQ_FREE
,
1750 .procname
= "free_dquots",
1751 .data
= &dqstats
.free_dquots
,
1752 .maxlen
= sizeof(int),
1754 .proc_handler
= &proc_dointvec
,
1757 .ctl_name
= FS_DQ_SYNCS
,
1758 .procname
= "syncs",
1759 .data
= &dqstats
.syncs
,
1760 .maxlen
= sizeof(int),
1762 .proc_handler
= &proc_dointvec
,
1765 .ctl_name
= FS_DQ_WARNINGS
,
1766 .procname
= "warnings",
1767 .data
= &flag_print_warnings
,
1768 .maxlen
= sizeof(int),
1770 .proc_handler
= &proc_dointvec
,
1775 static ctl_table fs_table
[] = {
1777 .ctl_name
= FS_DQSTATS
,
1778 .procname
= "quota",
1780 .child
= fs_dqstats_table
,
1785 static ctl_table sys_table
[] = {
1795 static int __init
dquot_init(void)
1798 unsigned long nr_hash
, order
;
1800 printk(KERN_NOTICE
"VFS: Disk quotas %s\n", __DQUOT_VERSION__
);
1802 register_sysctl_table(sys_table
, 0);
1804 dquot_cachep
= kmem_cache_create("dquot",
1805 sizeof(struct dquot
), sizeof(unsigned long) * 4,
1806 SLAB_HWCACHE_ALIGN
|SLAB_RECLAIM_ACCOUNT
|SLAB_PANIC
,
1810 dquot_hash
= (struct hlist_head
*)__get_free_pages(GFP_ATOMIC
, order
);
1812 panic("Cannot create dquot hash table");
1814 /* Find power-of-two hlist_heads which can fit into allocation */
1815 nr_hash
= (1UL << order
) * PAGE_SIZE
/ sizeof(struct hlist_head
);
1819 } while (nr_hash
>> dq_hash_bits
);
1822 nr_hash
= 1UL << dq_hash_bits
;
1823 dq_hash_mask
= nr_hash
- 1;
1824 for (i
= 0; i
< nr_hash
; i
++)
1825 INIT_HLIST_HEAD(dquot_hash
+ i
);
1827 printk("Dquot-cache hash table entries: %ld (order %ld, %ld bytes)\n",
1828 nr_hash
, order
, (PAGE_SIZE
<< order
));
1830 set_shrinker(DEFAULT_SEEKS
, shrink_dqcache_memory
);
1834 module_init(dquot_init
);
1836 EXPORT_SYMBOL(register_quota_format
);
1837 EXPORT_SYMBOL(unregister_quota_format
);
1838 EXPORT_SYMBOL(dqstats
);
1839 EXPORT_SYMBOL(dq_data_lock
);
1840 EXPORT_SYMBOL(vfs_quota_on
);
1841 EXPORT_SYMBOL(vfs_quota_on_mount
);
1842 EXPORT_SYMBOL(vfs_quota_off
);
1843 EXPORT_SYMBOL(vfs_quota_sync
);
1844 EXPORT_SYMBOL(vfs_get_dqinfo
);
1845 EXPORT_SYMBOL(vfs_set_dqinfo
);
1846 EXPORT_SYMBOL(vfs_get_dqblk
);
1847 EXPORT_SYMBOL(vfs_set_dqblk
);
1848 EXPORT_SYMBOL(dquot_commit
);
1849 EXPORT_SYMBOL(dquot_commit_info
);
1850 EXPORT_SYMBOL(dquot_acquire
);
1851 EXPORT_SYMBOL(dquot_release
);
1852 EXPORT_SYMBOL(dquot_mark_dquot_dirty
);
1853 EXPORT_SYMBOL(dquot_initialize
);
1854 EXPORT_SYMBOL(dquot_drop
);
1855 EXPORT_SYMBOL(dquot_alloc_space
);
1856 EXPORT_SYMBOL(dquot_alloc_inode
);
1857 EXPORT_SYMBOL(dquot_free_space
);
1858 EXPORT_SYMBOL(dquot_free_inode
);
1859 EXPORT_SYMBOL(dquot_transfer
);