1 // SPDX-License-Identifier: GPL-2.0
3 * Implementation of the diskquota system for the LINUX operating system. QUOTA
4 * is implemented using the BSD system call interface as the means of
5 * communication with the user level. This file contains the generic routines
6 * called by the different filesystems on allocation of an inode or block.
7 * These routines take care of the administration needed to have a consistent
8 * diskquota tracking system. The ideas of both user and group quotas are based
9 * on the Melbourne quota system as used on BSD derived systems. The internal
10 * implementation is based on one of the several variants of the LINUX
11 * inode-subsystem with added complexity of the diskquota system.
13 * Author: Marco van Wieringen <mvw@planets.elm.net>
15 * Fixes: Dmitry Gorodchanin <pgmdsg@ibi.com>, 11 Feb 96
17 * Revised list management to avoid races
18 * -- Bill Hawes, <whawes@star.net>, 9/98
20 * Fixed races in dquot_transfer(), dqget() and dquot_alloc_...().
21 * As the consequence the locking was moved from dquot_decr_...(),
22 * dquot_incr_...() to calling functions.
23 * invalidate_dquots() now writes modified dquots.
24 * Serialized quota_off() and quota_on() for mount point.
25 * Fixed a few bugs in grow_dquots().
26 * Fixed deadlock in write_dquot() - we no longer account quotas on
28 * remove_dquot_ref() moved to inode.c - it now traverses through inodes
29 * add_dquot_ref() restarts after blocking
30 * Added check for bogus uid and fixed check for group in quotactl.
31 * Jan Kara, <jack@suse.cz>, sponsored by SuSE CR, 10-11/99
33 * Used struct list_head instead of own list struct
34 * Invalidation of referenced dquots is no longer possible
35 * Improved free_dquots list management
36 * Quota and i_blocks are now updated in one place to avoid races
37 * Warnings are now delayed so we won't block in critical section
38 * Write updated not to require dquot lock
39 * Jan Kara, <jack@suse.cz>, 9/2000
41 * Added dynamic quota structure allocation
42 * Jan Kara <jack@suse.cz> 12/2000
44 * Rewritten quota interface. Implemented new quota format and
45 * formats registering.
46 * Jan Kara, <jack@suse.cz>, 2001,2002
49 * Jan Kara, <jack@suse.cz>, 10/2002
51 * Added journalled quota support, fix lock inversion problems
52 * Jan Kara, <jack@suse.cz>, 2003,2004
54 * (C) Copyright 1994 - 1997 Marco van Wieringen
57 #include <linux/errno.h>
58 #include <linux/kernel.h>
60 #include <linux/mount.h>
62 #include <linux/time.h>
63 #include <linux/types.h>
64 #include <linux/string.h>
65 #include <linux/fcntl.h>
66 #include <linux/stat.h>
67 #include <linux/tty.h>
68 #include <linux/file.h>
69 #include <linux/slab.h>
70 #include <linux/sysctl.h>
71 #include <linux/init.h>
72 #include <linux/module.h>
73 #include <linux/proc_fs.h>
74 #include <linux/security.h>
75 #include <linux/sched.h>
76 #include <linux/cred.h>
77 #include <linux/kmod.h>
78 #include <linux/namei.h>
79 #include <linux/capability.h>
80 #include <linux/quotaops.h>
81 #include <linux/blkdev.h>
82 #include <linux/sched/mm.h>
83 #include "../internal.h" /* ugh */
85 #include <linux/uaccess.h>
88 * There are five quota SMP locks:
89 * * dq_list_lock protects all lists with quotas and quota formats.
90 * * dquot->dq_dqb_lock protects data from dq_dqb
91 * * inode->i_lock protects inode->i_blocks, i_bytes and also guards
92 * consistency of dquot->dq_dqb with inode->i_blocks, i_bytes so that
93 * dquot_transfer() can stabilize amount it transfers
94 * * dq_data_lock protects mem_dqinfo structures and modifications of dquot
95 * pointers in the inode
96 * * dq_state_lock protects modifications of quota state (on quotaon and
97 * quotaoff) and readers who care about latest values take it as well.
99 * The spinlock ordering is hence:
100 * dq_data_lock > dq_list_lock > i_lock > dquot->dq_dqb_lock,
101 * dq_list_lock > dq_state_lock
103 * Note that some things (eg. sb pointer, type, id) doesn't change during
104 * the life of the dquot structure and so needn't to be protected by a lock
106 * Operation accessing dquots via inode pointers are protected by dquot_srcu.
107 * Operation of reading pointer needs srcu_read_lock(&dquot_srcu), and
108 * synchronize_srcu(&dquot_srcu) is called after clearing pointers from
109 * inode and before dropping dquot references to avoid use of dquots after
110 * they are freed. dq_data_lock is used to serialize the pointer setting and
111 * clearing operations.
112 * Special care needs to be taken about S_NOQUOTA inode flag (marking that
113 * inode is a quota file). Functions adding pointers from inode to dquots have
114 * to check this flag under dq_data_lock and then (if S_NOQUOTA is not set) they
115 * have to do all pointer modifications before dropping dq_data_lock. This makes
116 * sure they cannot race with quotaon which first sets S_NOQUOTA flag and
117 * then drops all pointers to dquots from an inode.
119 * Each dquot has its dq_lock mutex. Dquot is locked when it is being read to
120 * memory (or space for it is being allocated) on the first dqget(), when it is
121 * being written out, and when it is being released on the last dqput(). The
122 * allocation and release operations are serialized by the dq_lock and by
123 * checking the use count in dquot_release().
125 * Lock ordering (including related VFS locks) is the following:
126 * s_umount > i_mutex > journal_lock > dquot->dq_lock > dqio_sem
129 static __cacheline_aligned_in_smp DEFINE_SPINLOCK(dq_list_lock);
130 static __cacheline_aligned_in_smp DEFINE_SPINLOCK(dq_state_lock);
131 __cacheline_aligned_in_smp DEFINE_SPINLOCK(dq_data_lock);
132 EXPORT_SYMBOL(dq_data_lock);
133 DEFINE_STATIC_SRCU(dquot_srcu);
135 static DECLARE_WAIT_QUEUE_HEAD(dquot_ref_wq);
137 void __quota_error(struct super_block *sb, const char *func,
138 const char *fmt, ...)
140 if (printk_ratelimit()) {
142 struct va_format vaf;
149 printk(KERN_ERR "Quota error (device %s): %s: %pV\n",
150 sb->s_id, func, &vaf);
155 EXPORT_SYMBOL(__quota_error);
157 #if defined(CONFIG_QUOTA_DEBUG) || defined(CONFIG_PRINT_QUOTA_WARNING)
158 static char *quotatypes[] = INITQFNAMES;
160 static struct quota_format_type *quota_formats; /* List of registered formats */
161 static struct quota_module_name module_names[] = INIT_QUOTA_MODULE_NAMES;
163 /* SLAB cache for dquot structures */
164 static struct kmem_cache *dquot_cachep;
166 int register_quota_format(struct quota_format_type *fmt)
168 spin_lock(&dq_list_lock);
169 fmt->qf_next = quota_formats;
171 spin_unlock(&dq_list_lock);
174 EXPORT_SYMBOL(register_quota_format);
176 void unregister_quota_format(struct quota_format_type *fmt)
178 struct quota_format_type **actqf;
180 spin_lock(&dq_list_lock);
181 for (actqf = "a_formats; *actqf && *actqf != fmt;
182 actqf = &(*actqf)->qf_next)
185 *actqf = (*actqf)->qf_next;
186 spin_unlock(&dq_list_lock);
188 EXPORT_SYMBOL(unregister_quota_format);
190 static struct quota_format_type *find_quota_format(int id)
192 struct quota_format_type *actqf;
194 spin_lock(&dq_list_lock);
195 for (actqf = quota_formats; actqf && actqf->qf_fmt_id != id;
196 actqf = actqf->qf_next)
198 if (!actqf || !try_module_get(actqf->qf_owner)) {
201 spin_unlock(&dq_list_lock);
203 for (qm = 0; module_names[qm].qm_fmt_id &&
204 module_names[qm].qm_fmt_id != id; qm++)
206 if (!module_names[qm].qm_fmt_id ||
207 request_module(module_names[qm].qm_mod_name))
210 spin_lock(&dq_list_lock);
211 for (actqf = quota_formats; actqf && actqf->qf_fmt_id != id;
212 actqf = actqf->qf_next)
214 if (actqf && !try_module_get(actqf->qf_owner))
217 spin_unlock(&dq_list_lock);
221 static void put_quota_format(struct quota_format_type *fmt)
223 module_put(fmt->qf_owner);
227 * Dquot List Management:
228 * The quota code uses five lists for dquot management: the inuse_list,
229 * releasing_dquots, free_dquots, dqi_dirty_list, and dquot_hash[] array.
230 * A single dquot structure may be on some of those lists, depending on
233 * All dquots are placed to the end of inuse_list when first created, and this
234 * list is used for invalidate operation, which must look at every dquot.
236 * When the last reference of a dquot is dropped, the dquot is added to
237 * releasing_dquots. We'll then queue work item which will call
238 * synchronize_srcu() and after that perform the final cleanup of all the
239 * dquots on the list. Each cleaned up dquot is moved to free_dquots list.
240 * Both releasing_dquots and free_dquots use the dq_free list_head in the dquot
243 * Unused and cleaned up dquots are in the free_dquots list and this list is
244 * searched whenever we need an available dquot. Dquots are removed from the
245 * list as soon as they are used again and dqstats.free_dquots gives the number
246 * of dquots on the list. When dquot is invalidated it's completely released
249 * Dirty dquots are added to the dqi_dirty_list of quota_info when mark
250 * dirtied, and this list is searched when writing dirty dquots back to
251 * quota file. Note that some filesystems do dirty dquot tracking on their
252 * own (e.g. in a journal) and thus don't use dqi_dirty_list.
254 * Dquots with a specific identity (device, type and id) are placed on
255 * one of the dquot_hash[] hash chains. The provides an efficient search
256 * mechanism to locate a specific dquot.
259 static LIST_HEAD(inuse_list);
260 static LIST_HEAD(free_dquots);
261 static LIST_HEAD(releasing_dquots);
262 static unsigned int dq_hash_bits, dq_hash_mask;
263 static struct hlist_head *dquot_hash;
265 struct dqstats dqstats;
266 EXPORT_SYMBOL(dqstats);
268 static qsize_t inode_get_rsv_space(struct inode *inode);
269 static qsize_t __inode_get_rsv_space(struct inode *inode);
270 static int __dquot_initialize(struct inode *inode, int type);
272 static void quota_release_workfn(struct work_struct *work);
273 static DECLARE_DELAYED_WORK(quota_release_work, quota_release_workfn);
275 static inline unsigned int
276 hashfn(const struct super_block *sb, struct kqid qid)
278 unsigned int id = from_kqid(&init_user_ns, qid);
282 tmp = (((unsigned long)sb>>L1_CACHE_SHIFT) ^ id) * (MAXQUOTAS - type);
283 return (tmp + (tmp >> dq_hash_bits)) & dq_hash_mask;
287 * Following list functions expect dq_list_lock to be held
289 static inline void insert_dquot_hash(struct dquot *dquot)
291 struct hlist_head *head;
292 head = dquot_hash + hashfn(dquot->dq_sb, dquot->dq_id);
293 hlist_add_head(&dquot->dq_hash, head);
296 static inline void remove_dquot_hash(struct dquot *dquot)
298 hlist_del_init(&dquot->dq_hash);
301 static struct dquot *find_dquot(unsigned int hashent, struct super_block *sb,
306 hlist_for_each_entry(dquot, dquot_hash+hashent, dq_hash)
307 if (dquot->dq_sb == sb && qid_eq(dquot->dq_id, qid))
313 /* Add a dquot to the tail of the free list */
314 static inline void put_dquot_last(struct dquot *dquot)
316 list_add_tail(&dquot->dq_free, &free_dquots);
317 dqstats_inc(DQST_FREE_DQUOTS);
320 static inline void put_releasing_dquots(struct dquot *dquot)
322 list_add_tail(&dquot->dq_free, &releasing_dquots);
323 set_bit(DQ_RELEASING_B, &dquot->dq_flags);
326 static inline void remove_free_dquot(struct dquot *dquot)
328 if (list_empty(&dquot->dq_free))
330 list_del_init(&dquot->dq_free);
331 if (!test_bit(DQ_RELEASING_B, &dquot->dq_flags))
332 dqstats_dec(DQST_FREE_DQUOTS);
334 clear_bit(DQ_RELEASING_B, &dquot->dq_flags);
337 static inline void put_inuse(struct dquot *dquot)
339 /* We add to the back of inuse list so we don't have to restart
340 * when traversing this list and we block */
341 list_add_tail(&dquot->dq_inuse, &inuse_list);
342 dqstats_inc(DQST_ALLOC_DQUOTS);
345 static inline void remove_inuse(struct dquot *dquot)
347 dqstats_dec(DQST_ALLOC_DQUOTS);
348 list_del(&dquot->dq_inuse);
351 * End of list functions needing dq_list_lock
354 static void wait_on_dquot(struct dquot *dquot)
356 mutex_lock(&dquot->dq_lock);
357 mutex_unlock(&dquot->dq_lock);
360 static inline int dquot_active(struct dquot *dquot)
362 return test_bit(DQ_ACTIVE_B, &dquot->dq_flags);
365 static inline int dquot_dirty(struct dquot *dquot)
367 return test_bit(DQ_MOD_B, &dquot->dq_flags);
370 static inline int mark_dquot_dirty(struct dquot *dquot)
372 return dquot->dq_sb->dq_op->mark_dirty(dquot);
375 /* Mark dquot dirty in atomic manner, and return it's old dirty flag state */
376 int dquot_mark_dquot_dirty(struct dquot *dquot)
380 if (!dquot_active(dquot))
383 if (sb_dqopt(dquot->dq_sb)->flags & DQUOT_NOLIST_DIRTY)
384 return test_and_set_bit(DQ_MOD_B, &dquot->dq_flags);
386 /* If quota is dirty already, we don't have to acquire dq_list_lock */
387 if (dquot_dirty(dquot))
390 spin_lock(&dq_list_lock);
391 if (!test_and_set_bit(DQ_MOD_B, &dquot->dq_flags)) {
392 list_add(&dquot->dq_dirty, &sb_dqopt(dquot->dq_sb)->
393 info[dquot->dq_id.type].dqi_dirty_list);
396 spin_unlock(&dq_list_lock);
399 EXPORT_SYMBOL(dquot_mark_dquot_dirty);
401 /* Dirtify all the dquots - this can block when journalling */
402 static inline int mark_all_dquot_dirty(struct dquot * const *dquot)
407 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
409 /* Even in case of error we have to continue */
410 ret = mark_dquot_dirty(dquot[cnt]);
417 static inline void dqput_all(struct dquot **dquot)
421 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
425 static inline int clear_dquot_dirty(struct dquot *dquot)
427 if (sb_dqopt(dquot->dq_sb)->flags & DQUOT_NOLIST_DIRTY)
428 return test_and_clear_bit(DQ_MOD_B, &dquot->dq_flags);
430 spin_lock(&dq_list_lock);
431 if (!test_and_clear_bit(DQ_MOD_B, &dquot->dq_flags)) {
432 spin_unlock(&dq_list_lock);
435 list_del_init(&dquot->dq_dirty);
436 spin_unlock(&dq_list_lock);
440 void mark_info_dirty(struct super_block *sb, int type)
442 spin_lock(&dq_data_lock);
443 sb_dqopt(sb)->info[type].dqi_flags |= DQF_INFO_DIRTY;
444 spin_unlock(&dq_data_lock);
446 EXPORT_SYMBOL(mark_info_dirty);
449 * Read dquot from disk and alloc space for it
452 int dquot_acquire(struct dquot *dquot)
454 int ret = 0, ret2 = 0;
455 unsigned int memalloc;
456 struct quota_info *dqopt = sb_dqopt(dquot->dq_sb);
458 mutex_lock(&dquot->dq_lock);
459 memalloc = memalloc_nofs_save();
460 if (!test_bit(DQ_READ_B, &dquot->dq_flags)) {
461 ret = dqopt->ops[dquot->dq_id.type]->read_dqblk(dquot);
465 /* Make sure flags update is visible after dquot has been filled */
466 smp_mb__before_atomic();
467 set_bit(DQ_READ_B, &dquot->dq_flags);
468 /* Instantiate dquot if needed */
469 if (!dquot_active(dquot) && !dquot->dq_off) {
470 ret = dqopt->ops[dquot->dq_id.type]->commit_dqblk(dquot);
471 /* Write the info if needed */
472 if (info_dirty(&dqopt->info[dquot->dq_id.type])) {
473 ret2 = dqopt->ops[dquot->dq_id.type]->write_file_info(
474 dquot->dq_sb, dquot->dq_id.type);
484 * Make sure flags update is visible after on-disk struct has been
485 * allocated. Paired with smp_rmb() in dqget().
487 smp_mb__before_atomic();
488 set_bit(DQ_ACTIVE_B, &dquot->dq_flags);
490 memalloc_nofs_restore(memalloc);
491 mutex_unlock(&dquot->dq_lock);
494 EXPORT_SYMBOL(dquot_acquire);
497 * Write dquot to disk
499 int dquot_commit(struct dquot *dquot)
502 unsigned int memalloc;
503 struct quota_info *dqopt = sb_dqopt(dquot->dq_sb);
505 mutex_lock(&dquot->dq_lock);
506 memalloc = memalloc_nofs_save();
507 if (!clear_dquot_dirty(dquot))
509 /* Inactive dquot can be only if there was error during read/init
510 * => we have better not writing it */
511 if (dquot_active(dquot))
512 ret = dqopt->ops[dquot->dq_id.type]->commit_dqblk(dquot);
516 memalloc_nofs_restore(memalloc);
517 mutex_unlock(&dquot->dq_lock);
520 EXPORT_SYMBOL(dquot_commit);
525 int dquot_release(struct dquot *dquot)
527 int ret = 0, ret2 = 0;
528 unsigned int memalloc;
529 struct quota_info *dqopt = sb_dqopt(dquot->dq_sb);
531 mutex_lock(&dquot->dq_lock);
532 memalloc = memalloc_nofs_save();
533 /* Check whether we are not racing with some other dqget() */
534 if (dquot_is_busy(dquot))
536 if (dqopt->ops[dquot->dq_id.type]->release_dqblk) {
537 ret = dqopt->ops[dquot->dq_id.type]->release_dqblk(dquot);
539 if (info_dirty(&dqopt->info[dquot->dq_id.type])) {
540 ret2 = dqopt->ops[dquot->dq_id.type]->write_file_info(
541 dquot->dq_sb, dquot->dq_id.type);
546 clear_bit(DQ_ACTIVE_B, &dquot->dq_flags);
548 memalloc_nofs_restore(memalloc);
549 mutex_unlock(&dquot->dq_lock);
552 EXPORT_SYMBOL(dquot_release);
554 void dquot_destroy(struct dquot *dquot)
556 kmem_cache_free(dquot_cachep, dquot);
558 EXPORT_SYMBOL(dquot_destroy);
560 static inline void do_destroy_dquot(struct dquot *dquot)
562 dquot->dq_sb->dq_op->destroy_dquot(dquot);
565 /* Invalidate all dquots on the list. Note that this function is called after
566 * quota is disabled and pointers from inodes removed so there cannot be new
567 * quota users. There can still be some users of quotas due to inodes being
568 * just deleted or pruned by prune_icache() (those are not attached to any
569 * list) or parallel quotactl call. We have to wait for such users.
571 static void invalidate_dquots(struct super_block *sb, int type)
573 struct dquot *dquot, *tmp;
576 flush_delayed_work("a_release_work);
578 spin_lock(&dq_list_lock);
579 list_for_each_entry_safe(dquot, tmp, &inuse_list, dq_inuse) {
580 if (dquot->dq_sb != sb)
582 if (dquot->dq_id.type != type)
584 /* Wait for dquot users */
585 if (atomic_read(&dquot->dq_count)) {
586 atomic_inc(&dquot->dq_count);
587 spin_unlock(&dq_list_lock);
589 * Once dqput() wakes us up, we know it's time to free
591 * IMPORTANT: we rely on the fact that there is always
592 * at most one process waiting for dquot to free.
593 * Otherwise dq_count would be > 1 and we would never
596 wait_event(dquot_ref_wq,
597 atomic_read(&dquot->dq_count) == 1);
599 /* At this moment dquot() need not exist (it could be
600 * reclaimed by prune_dqcache(). Hence we must
605 * The last user already dropped its reference but dquot didn't
606 * get fully cleaned up yet. Restart the scan which flushes the
607 * work cleaning up released dquots.
609 if (test_bit(DQ_RELEASING_B, &dquot->dq_flags)) {
610 spin_unlock(&dq_list_lock);
614 * Quota now has no users and it has been written on last
617 remove_dquot_hash(dquot);
618 remove_free_dquot(dquot);
620 do_destroy_dquot(dquot);
622 spin_unlock(&dq_list_lock);
625 /* Call callback for every active dquot on given filesystem */
626 int dquot_scan_active(struct super_block *sb,
627 int (*fn)(struct dquot *dquot, unsigned long priv),
630 struct dquot *dquot, *old_dquot = NULL;
633 WARN_ON_ONCE(!rwsem_is_locked(&sb->s_umount));
635 spin_lock(&dq_list_lock);
636 list_for_each_entry(dquot, &inuse_list, dq_inuse) {
637 if (!dquot_active(dquot))
639 if (dquot->dq_sb != sb)
641 /* Now we have active dquot so we can just increase use count */
642 atomic_inc(&dquot->dq_count);
643 spin_unlock(&dq_list_lock);
647 * ->release_dquot() can be racing with us. Our reference
648 * protects us from new calls to it so just wait for any
649 * outstanding call and recheck the DQ_ACTIVE_B after that.
651 wait_on_dquot(dquot);
652 if (dquot_active(dquot)) {
653 ret = fn(dquot, priv);
657 spin_lock(&dq_list_lock);
658 /* We are safe to continue now because our dquot could not
659 * be moved out of the inuse list while we hold the reference */
661 spin_unlock(&dq_list_lock);
666 EXPORT_SYMBOL(dquot_scan_active);
668 static inline int dquot_write_dquot(struct dquot *dquot)
670 int ret = dquot->dq_sb->dq_op->write_dquot(dquot);
672 quota_error(dquot->dq_sb, "Can't write quota structure "
673 "(error %d). Quota may get out of sync!", ret);
674 /* Clear dirty bit anyway to avoid infinite loop. */
675 clear_dquot_dirty(dquot);
680 /* Write all dquot structures to quota files */
681 int dquot_writeback_dquots(struct super_block *sb, int type)
683 struct list_head dirty;
685 struct quota_info *dqopt = sb_dqopt(sb);
689 WARN_ON_ONCE(!rwsem_is_locked(&sb->s_umount));
691 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
692 if (type != -1 && cnt != type)
694 if (!sb_has_quota_active(sb, cnt))
696 spin_lock(&dq_list_lock);
697 /* Move list away to avoid livelock. */
698 list_replace_init(&dqopt->info[cnt].dqi_dirty_list, &dirty);
699 while (!list_empty(&dirty)) {
700 dquot = list_first_entry(&dirty, struct dquot,
703 WARN_ON(!dquot_active(dquot));
704 /* If the dquot is releasing we should not touch it */
705 if (test_bit(DQ_RELEASING_B, &dquot->dq_flags)) {
706 spin_unlock(&dq_list_lock);
707 flush_delayed_work("a_release_work);
708 spin_lock(&dq_list_lock);
712 /* Now we have active dquot from which someone is
713 * holding reference so we can safely just increase
716 spin_unlock(&dq_list_lock);
717 err = dquot_write_dquot(dquot);
721 spin_lock(&dq_list_lock);
723 spin_unlock(&dq_list_lock);
726 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
727 if ((cnt == type || type == -1) && sb_has_quota_active(sb, cnt)
728 && info_dirty(&dqopt->info[cnt]))
729 sb->dq_op->write_info(sb, cnt);
730 dqstats_inc(DQST_SYNCS);
734 EXPORT_SYMBOL(dquot_writeback_dquots);
736 /* Write all dquot structures to disk and make them visible from userspace */
737 int dquot_quota_sync(struct super_block *sb, int type)
739 struct quota_info *dqopt = sb_dqopt(sb);
743 ret = dquot_writeback_dquots(sb, type);
746 if (dqopt->flags & DQUOT_QUOTA_SYS_FILE)
749 /* This is not very clever (and fast) but currently I don't know about
750 * any other simple way of getting quota data to disk and we must get
751 * them there for userspace to be visible... */
752 if (sb->s_op->sync_fs) {
753 ret = sb->s_op->sync_fs(sb, 1);
757 ret = sync_blockdev(sb->s_bdev);
762 * Now when everything is written we can discard the pagecache so
763 * that userspace sees the changes.
765 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
766 if (type != -1 && cnt != type)
768 if (!sb_has_quota_active(sb, cnt))
770 inode_lock(dqopt->files[cnt]);
771 truncate_inode_pages(&dqopt->files[cnt]->i_data, 0);
772 inode_unlock(dqopt->files[cnt]);
777 EXPORT_SYMBOL(dquot_quota_sync);
780 dqcache_shrink_scan(struct shrinker *shrink, struct shrink_control *sc)
783 unsigned long freed = 0;
785 spin_lock(&dq_list_lock);
786 while (!list_empty(&free_dquots) && sc->nr_to_scan) {
787 dquot = list_first_entry(&free_dquots, struct dquot, dq_free);
788 remove_dquot_hash(dquot);
789 remove_free_dquot(dquot);
791 do_destroy_dquot(dquot);
795 spin_unlock(&dq_list_lock);
800 dqcache_shrink_count(struct shrinker *shrink, struct shrink_control *sc)
802 return vfs_pressure_ratio(
803 percpu_counter_read_positive(&dqstats.counter[DQST_FREE_DQUOTS]));
806 static struct shrinker dqcache_shrinker = {
807 .count_objects = dqcache_shrink_count,
808 .scan_objects = dqcache_shrink_scan,
809 .seeks = DEFAULT_SEEKS,
813 * Safely release dquot and put reference to dquot.
815 static void quota_release_workfn(struct work_struct *work)
818 struct list_head rls_head;
820 spin_lock(&dq_list_lock);
821 /* Exchange the list head to avoid livelock. */
822 list_replace_init(&releasing_dquots, &rls_head);
823 spin_unlock(&dq_list_lock);
824 synchronize_srcu(&dquot_srcu);
827 spin_lock(&dq_list_lock);
828 while (!list_empty(&rls_head)) {
829 dquot = list_first_entry(&rls_head, struct dquot, dq_free);
830 WARN_ON_ONCE(atomic_read(&dquot->dq_count));
832 * Note that DQ_RELEASING_B protects us from racing with
833 * invalidate_dquots() calls so we are safe to work with the
834 * dquot even after we drop dq_list_lock.
836 if (dquot_dirty(dquot)) {
837 spin_unlock(&dq_list_lock);
838 /* Commit dquot before releasing */
839 dquot_write_dquot(dquot);
842 if (dquot_active(dquot)) {
843 spin_unlock(&dq_list_lock);
844 dquot->dq_sb->dq_op->release_dquot(dquot);
847 /* Dquot is inactive and clean, now move it to free list */
848 remove_free_dquot(dquot);
849 put_dquot_last(dquot);
851 spin_unlock(&dq_list_lock);
855 * Put reference to dquot
857 void dqput(struct dquot *dquot)
861 #ifdef CONFIG_QUOTA_DEBUG
862 if (!atomic_read(&dquot->dq_count)) {
863 quota_error(dquot->dq_sb, "trying to free free dquot of %s %d",
864 quotatypes[dquot->dq_id.type],
865 from_kqid(&init_user_ns, dquot->dq_id));
869 dqstats_inc(DQST_DROPS);
871 spin_lock(&dq_list_lock);
872 if (atomic_read(&dquot->dq_count) > 1) {
873 /* We have more than one user... nothing to do */
874 atomic_dec(&dquot->dq_count);
875 /* Releasing dquot during quotaoff phase? */
876 if (!sb_has_quota_active(dquot->dq_sb, dquot->dq_id.type) &&
877 atomic_read(&dquot->dq_count) == 1)
878 wake_up(&dquot_ref_wq);
879 spin_unlock(&dq_list_lock);
883 /* Need to release dquot? */
884 #ifdef CONFIG_QUOTA_DEBUG
886 BUG_ON(!list_empty(&dquot->dq_free));
888 put_releasing_dquots(dquot);
889 atomic_dec(&dquot->dq_count);
890 spin_unlock(&dq_list_lock);
891 queue_delayed_work(system_unbound_wq, "a_release_work, 1);
893 EXPORT_SYMBOL(dqput);
895 struct dquot *dquot_alloc(struct super_block *sb, int type)
897 return kmem_cache_zalloc(dquot_cachep, GFP_NOFS);
899 EXPORT_SYMBOL(dquot_alloc);
901 static struct dquot *get_empty_dquot(struct super_block *sb, int type)
905 dquot = sb->dq_op->alloc_dquot(sb, type);
909 mutex_init(&dquot->dq_lock);
910 INIT_LIST_HEAD(&dquot->dq_free);
911 INIT_LIST_HEAD(&dquot->dq_inuse);
912 INIT_HLIST_NODE(&dquot->dq_hash);
913 INIT_LIST_HEAD(&dquot->dq_dirty);
915 dquot->dq_id = make_kqid_invalid(type);
916 atomic_set(&dquot->dq_count, 1);
917 spin_lock_init(&dquot->dq_dqb_lock);
923 * Get reference to dquot
925 * Locking is slightly tricky here. We are guarded from parallel quotaoff()
926 * destroying our dquot by:
927 * a) checking for quota flags under dq_list_lock and
928 * b) getting a reference to dquot before we release dq_list_lock
930 struct dquot *dqget(struct super_block *sb, struct kqid qid)
932 unsigned int hashent = hashfn(sb, qid);
933 struct dquot *dquot, *empty = NULL;
935 if (!qid_has_mapping(sb->s_user_ns, qid))
936 return ERR_PTR(-EINVAL);
938 if (!sb_has_quota_active(sb, qid.type))
939 return ERR_PTR(-ESRCH);
941 spin_lock(&dq_list_lock);
942 spin_lock(&dq_state_lock);
943 if (!sb_has_quota_active(sb, qid.type)) {
944 spin_unlock(&dq_state_lock);
945 spin_unlock(&dq_list_lock);
946 dquot = ERR_PTR(-ESRCH);
949 spin_unlock(&dq_state_lock);
951 dquot = find_dquot(hashent, sb, qid);
954 spin_unlock(&dq_list_lock);
955 empty = get_empty_dquot(sb, qid.type);
957 schedule(); /* Try to wait for a moment... */
963 /* all dquots go on the inuse_list */
965 /* hash it first so it can be found */
966 insert_dquot_hash(dquot);
967 spin_unlock(&dq_list_lock);
968 dqstats_inc(DQST_LOOKUPS);
970 if (!atomic_read(&dquot->dq_count))
971 remove_free_dquot(dquot);
972 atomic_inc(&dquot->dq_count);
973 spin_unlock(&dq_list_lock);
974 dqstats_inc(DQST_CACHE_HITS);
975 dqstats_inc(DQST_LOOKUPS);
977 /* Wait for dq_lock - after this we know that either dquot_release() is
978 * already finished or it will be canceled due to dq_count > 0 test */
979 wait_on_dquot(dquot);
980 /* Read the dquot / allocate space in quota file */
981 if (!dquot_active(dquot)) {
984 err = sb->dq_op->acquire_dquot(dquot);
987 dquot = ERR_PTR(err);
992 * Make sure following reads see filled structure - paired with
993 * smp_mb__before_atomic() in dquot_acquire().
996 #ifdef CONFIG_QUOTA_DEBUG
997 BUG_ON(!dquot->dq_sb); /* Has somebody invalidated entry under us? */
1001 do_destroy_dquot(empty);
1005 EXPORT_SYMBOL(dqget);
1007 static inline struct dquot **i_dquot(struct inode *inode)
1009 return inode->i_sb->s_op->get_dquots(inode);
1012 static int dqinit_needed(struct inode *inode, int type)
1014 struct dquot * const *dquots;
1017 if (IS_NOQUOTA(inode))
1020 dquots = i_dquot(inode);
1022 return !dquots[type];
1023 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1029 /* This routine is guarded by s_umount semaphore */
1030 static int add_dquot_ref(struct super_block *sb, int type)
1032 struct inode *inode, *old_inode = NULL;
1033 #ifdef CONFIG_QUOTA_DEBUG
1038 spin_lock(&sb->s_inode_list_lock);
1039 list_for_each_entry(inode, &sb->s_inodes, i_sb_list) {
1040 spin_lock(&inode->i_lock);
1041 if ((inode->i_state & (I_FREEING|I_WILL_FREE|I_NEW)) ||
1042 !atomic_read(&inode->i_writecount) ||
1043 !dqinit_needed(inode, type)) {
1044 spin_unlock(&inode->i_lock);
1048 spin_unlock(&inode->i_lock);
1049 spin_unlock(&sb->s_inode_list_lock);
1051 #ifdef CONFIG_QUOTA_DEBUG
1052 if (unlikely(inode_get_rsv_space(inode) > 0))
1056 err = __dquot_initialize(inode, type);
1063 * We hold a reference to 'inode' so it couldn't have been
1064 * removed from s_inodes list while we dropped the
1065 * s_inode_list_lock. We cannot iput the inode now as we can be
1066 * holding the last reference and we cannot iput it under
1067 * s_inode_list_lock. So we keep the reference and iput it
1072 spin_lock(&sb->s_inode_list_lock);
1074 spin_unlock(&sb->s_inode_list_lock);
1077 #ifdef CONFIG_QUOTA_DEBUG
1079 quota_error(sb, "Writes happened before quota was turned on "
1080 "thus quota information is probably inconsistent. "
1081 "Please run quotacheck(8)");
1088 * Remove references to dquots from inode and add dquot to list for freeing
1089 * if we have the last reference to dquot
1091 static void remove_inode_dquot_ref(struct inode *inode, int type,
1092 struct list_head *tofree_head)
1094 struct dquot **dquots = i_dquot(inode);
1095 struct dquot *dquot = dquots[type];
1100 dquots[type] = NULL;
1101 if (list_empty(&dquot->dq_free)) {
1103 * The inode still has reference to dquot so it can't be in the
1106 spin_lock(&dq_list_lock);
1107 list_add(&dquot->dq_free, tofree_head);
1108 spin_unlock(&dq_list_lock);
1111 * Dquot is already in a list to put so we won't drop the last
1119 * Free list of dquots
1120 * Dquots are removed from inodes and no new references can be got so we are
1121 * the only ones holding reference
1123 static void put_dquot_list(struct list_head *tofree_head)
1125 struct list_head *act_head;
1126 struct dquot *dquot;
1128 act_head = tofree_head->next;
1129 while (act_head != tofree_head) {
1130 dquot = list_entry(act_head, struct dquot, dq_free);
1131 act_head = act_head->next;
1132 /* Remove dquot from the list so we won't have problems... */
1133 list_del_init(&dquot->dq_free);
1138 static void remove_dquot_ref(struct super_block *sb, int type,
1139 struct list_head *tofree_head)
1141 struct inode *inode;
1142 #ifdef CONFIG_QUOTA_DEBUG
1146 spin_lock(&sb->s_inode_list_lock);
1147 list_for_each_entry(inode, &sb->s_inodes, i_sb_list) {
1149 * We have to scan also I_NEW inodes because they can already
1150 * have quota pointer initialized. Luckily, we need to touch
1151 * only quota pointers and these have separate locking
1154 spin_lock(&dq_data_lock);
1155 if (!IS_NOQUOTA(inode)) {
1156 #ifdef CONFIG_QUOTA_DEBUG
1157 if (unlikely(inode_get_rsv_space(inode) > 0))
1160 remove_inode_dquot_ref(inode, type, tofree_head);
1162 spin_unlock(&dq_data_lock);
1164 spin_unlock(&sb->s_inode_list_lock);
1165 #ifdef CONFIG_QUOTA_DEBUG
1167 printk(KERN_WARNING "VFS (%s): Writes happened after quota"
1168 " was disabled thus quota information is probably "
1169 "inconsistent. Please run quotacheck(8).\n", sb->s_id);
1174 /* Gather all references from inodes and drop them */
1175 static void drop_dquot_ref(struct super_block *sb, int type)
1177 LIST_HEAD(tofree_head);
1180 remove_dquot_ref(sb, type, &tofree_head);
1181 synchronize_srcu(&dquot_srcu);
1182 put_dquot_list(&tofree_head);
1187 void dquot_free_reserved_space(struct dquot *dquot, qsize_t number)
1189 if (dquot->dq_dqb.dqb_rsvspace >= number)
1190 dquot->dq_dqb.dqb_rsvspace -= number;
1193 dquot->dq_dqb.dqb_rsvspace = 0;
1195 if (dquot->dq_dqb.dqb_curspace + dquot->dq_dqb.dqb_rsvspace <=
1196 dquot->dq_dqb.dqb_bsoftlimit)
1197 dquot->dq_dqb.dqb_btime = (time64_t) 0;
1198 clear_bit(DQ_BLKS_B, &dquot->dq_flags);
1201 static void dquot_decr_inodes(struct dquot *dquot, qsize_t number)
1203 if (sb_dqopt(dquot->dq_sb)->flags & DQUOT_NEGATIVE_USAGE ||
1204 dquot->dq_dqb.dqb_curinodes >= number)
1205 dquot->dq_dqb.dqb_curinodes -= number;
1207 dquot->dq_dqb.dqb_curinodes = 0;
1208 if (dquot->dq_dqb.dqb_curinodes <= dquot->dq_dqb.dqb_isoftlimit)
1209 dquot->dq_dqb.dqb_itime = (time64_t) 0;
1210 clear_bit(DQ_INODES_B, &dquot->dq_flags);
1213 static void dquot_decr_space(struct dquot *dquot, qsize_t number)
1215 if (sb_dqopt(dquot->dq_sb)->flags & DQUOT_NEGATIVE_USAGE ||
1216 dquot->dq_dqb.dqb_curspace >= number)
1217 dquot->dq_dqb.dqb_curspace -= number;
1219 dquot->dq_dqb.dqb_curspace = 0;
1220 if (dquot->dq_dqb.dqb_curspace + dquot->dq_dqb.dqb_rsvspace <=
1221 dquot->dq_dqb.dqb_bsoftlimit)
1222 dquot->dq_dqb.dqb_btime = (time64_t) 0;
1223 clear_bit(DQ_BLKS_B, &dquot->dq_flags);
1227 struct super_block *w_sb;
1228 struct kqid w_dq_id;
1232 static int warning_issued(struct dquot *dquot, const int warntype)
1234 int flag = (warntype == QUOTA_NL_BHARDWARN ||
1235 warntype == QUOTA_NL_BSOFTLONGWARN) ? DQ_BLKS_B :
1236 ((warntype == QUOTA_NL_IHARDWARN ||
1237 warntype == QUOTA_NL_ISOFTLONGWARN) ? DQ_INODES_B : 0);
1241 return test_and_set_bit(flag, &dquot->dq_flags);
1244 #ifdef CONFIG_PRINT_QUOTA_WARNING
1245 static int flag_print_warnings = 1;
1247 static int need_print_warning(struct dquot_warn *warn)
1249 if (!flag_print_warnings)
1252 switch (warn->w_dq_id.type) {
1254 return uid_eq(current_fsuid(), warn->w_dq_id.uid);
1256 return in_group_p(warn->w_dq_id.gid);
1263 /* Print warning to user which exceeded quota */
1264 static void print_warning(struct dquot_warn *warn)
1267 struct tty_struct *tty;
1268 int warntype = warn->w_type;
1270 if (warntype == QUOTA_NL_IHARDBELOW ||
1271 warntype == QUOTA_NL_ISOFTBELOW ||
1272 warntype == QUOTA_NL_BHARDBELOW ||
1273 warntype == QUOTA_NL_BSOFTBELOW || !need_print_warning(warn))
1276 tty = get_current_tty();
1279 tty_write_message(tty, warn->w_sb->s_id);
1280 if (warntype == QUOTA_NL_ISOFTWARN || warntype == QUOTA_NL_BSOFTWARN)
1281 tty_write_message(tty, ": warning, ");
1283 tty_write_message(tty, ": write failed, ");
1284 tty_write_message(tty, quotatypes[warn->w_dq_id.type]);
1286 case QUOTA_NL_IHARDWARN:
1287 msg = " file limit reached.\r\n";
1289 case QUOTA_NL_ISOFTLONGWARN:
1290 msg = " file quota exceeded too long.\r\n";
1292 case QUOTA_NL_ISOFTWARN:
1293 msg = " file quota exceeded.\r\n";
1295 case QUOTA_NL_BHARDWARN:
1296 msg = " block limit reached.\r\n";
1298 case QUOTA_NL_BSOFTLONGWARN:
1299 msg = " block quota exceeded too long.\r\n";
1301 case QUOTA_NL_BSOFTWARN:
1302 msg = " block quota exceeded.\r\n";
1305 tty_write_message(tty, msg);
1310 static void prepare_warning(struct dquot_warn *warn, struct dquot *dquot,
1313 if (warning_issued(dquot, warntype))
1315 warn->w_type = warntype;
1316 warn->w_sb = dquot->dq_sb;
1317 warn->w_dq_id = dquot->dq_id;
1321 * Write warnings to the console and send warning messages over netlink.
1323 * Note that this function can call into tty and networking code.
1325 static void flush_warnings(struct dquot_warn *warn)
1329 for (i = 0; i < MAXQUOTAS; i++) {
1330 if (warn[i].w_type == QUOTA_NL_NOWARN)
1332 #ifdef CONFIG_PRINT_QUOTA_WARNING
1333 print_warning(&warn[i]);
1335 quota_send_warning(warn[i].w_dq_id,
1336 warn[i].w_sb->s_dev, warn[i].w_type);
1340 static int ignore_hardlimit(struct dquot *dquot)
1342 struct mem_dqinfo *info = &sb_dqopt(dquot->dq_sb)->info[dquot->dq_id.type];
1344 return capable(CAP_SYS_RESOURCE) &&
1345 (info->dqi_format->qf_fmt_id != QFMT_VFS_OLD ||
1346 !(info->dqi_flags & DQF_ROOT_SQUASH));
1349 static int dquot_add_inodes(struct dquot *dquot, qsize_t inodes,
1350 struct dquot_warn *warn)
1355 spin_lock(&dquot->dq_dqb_lock);
1356 newinodes = dquot->dq_dqb.dqb_curinodes + inodes;
1357 if (!sb_has_quota_limits_enabled(dquot->dq_sb, dquot->dq_id.type) ||
1358 test_bit(DQ_FAKE_B, &dquot->dq_flags))
1361 if (dquot->dq_dqb.dqb_ihardlimit &&
1362 newinodes > dquot->dq_dqb.dqb_ihardlimit &&
1363 !ignore_hardlimit(dquot)) {
1364 prepare_warning(warn, dquot, QUOTA_NL_IHARDWARN);
1369 if (dquot->dq_dqb.dqb_isoftlimit &&
1370 newinodes > dquot->dq_dqb.dqb_isoftlimit &&
1371 dquot->dq_dqb.dqb_itime &&
1372 ktime_get_real_seconds() >= dquot->dq_dqb.dqb_itime &&
1373 !ignore_hardlimit(dquot)) {
1374 prepare_warning(warn, dquot, QUOTA_NL_ISOFTLONGWARN);
1379 if (dquot->dq_dqb.dqb_isoftlimit &&
1380 newinodes > dquot->dq_dqb.dqb_isoftlimit &&
1381 dquot->dq_dqb.dqb_itime == 0) {
1382 prepare_warning(warn, dquot, QUOTA_NL_ISOFTWARN);
1383 dquot->dq_dqb.dqb_itime = ktime_get_real_seconds() +
1384 sb_dqopt(dquot->dq_sb)->info[dquot->dq_id.type].dqi_igrace;
1387 dquot->dq_dqb.dqb_curinodes = newinodes;
1390 spin_unlock(&dquot->dq_dqb_lock);
1394 static int dquot_add_space(struct dquot *dquot, qsize_t space,
1395 qsize_t rsv_space, unsigned int flags,
1396 struct dquot_warn *warn)
1399 struct super_block *sb = dquot->dq_sb;
1402 spin_lock(&dquot->dq_dqb_lock);
1403 if (!sb_has_quota_limits_enabled(sb, dquot->dq_id.type) ||
1404 test_bit(DQ_FAKE_B, &dquot->dq_flags))
1407 tspace = dquot->dq_dqb.dqb_curspace + dquot->dq_dqb.dqb_rsvspace
1408 + space + rsv_space;
1410 if (dquot->dq_dqb.dqb_bhardlimit &&
1411 tspace > dquot->dq_dqb.dqb_bhardlimit &&
1412 !ignore_hardlimit(dquot)) {
1413 if (flags & DQUOT_SPACE_WARN)
1414 prepare_warning(warn, dquot, QUOTA_NL_BHARDWARN);
1419 if (dquot->dq_dqb.dqb_bsoftlimit &&
1420 tspace > dquot->dq_dqb.dqb_bsoftlimit &&
1421 dquot->dq_dqb.dqb_btime &&
1422 ktime_get_real_seconds() >= dquot->dq_dqb.dqb_btime &&
1423 !ignore_hardlimit(dquot)) {
1424 if (flags & DQUOT_SPACE_WARN)
1425 prepare_warning(warn, dquot, QUOTA_NL_BSOFTLONGWARN);
1430 if (dquot->dq_dqb.dqb_bsoftlimit &&
1431 tspace > dquot->dq_dqb.dqb_bsoftlimit &&
1432 dquot->dq_dqb.dqb_btime == 0) {
1433 if (flags & DQUOT_SPACE_WARN) {
1434 prepare_warning(warn, dquot, QUOTA_NL_BSOFTWARN);
1435 dquot->dq_dqb.dqb_btime = ktime_get_real_seconds() +
1436 sb_dqopt(sb)->info[dquot->dq_id.type].dqi_bgrace;
1439 * We don't allow preallocation to exceed softlimit so exceeding will
1448 * We have to be careful and go through warning generation & grace time
1449 * setting even if DQUOT_SPACE_NOFAIL is set. That's why we check it
1452 if (flags & DQUOT_SPACE_NOFAIL)
1455 dquot->dq_dqb.dqb_rsvspace += rsv_space;
1456 dquot->dq_dqb.dqb_curspace += space;
1458 spin_unlock(&dquot->dq_dqb_lock);
1462 static int info_idq_free(struct dquot *dquot, qsize_t inodes)
1466 if (test_bit(DQ_FAKE_B, &dquot->dq_flags) ||
1467 dquot->dq_dqb.dqb_curinodes <= dquot->dq_dqb.dqb_isoftlimit ||
1468 !sb_has_quota_limits_enabled(dquot->dq_sb, dquot->dq_id.type))
1469 return QUOTA_NL_NOWARN;
1471 newinodes = dquot->dq_dqb.dqb_curinodes - inodes;
1472 if (newinodes <= dquot->dq_dqb.dqb_isoftlimit)
1473 return QUOTA_NL_ISOFTBELOW;
1474 if (dquot->dq_dqb.dqb_curinodes >= dquot->dq_dqb.dqb_ihardlimit &&
1475 newinodes < dquot->dq_dqb.dqb_ihardlimit)
1476 return QUOTA_NL_IHARDBELOW;
1477 return QUOTA_NL_NOWARN;
1480 static int info_bdq_free(struct dquot *dquot, qsize_t space)
1484 tspace = dquot->dq_dqb.dqb_curspace + dquot->dq_dqb.dqb_rsvspace;
1486 if (test_bit(DQ_FAKE_B, &dquot->dq_flags) ||
1487 tspace <= dquot->dq_dqb.dqb_bsoftlimit)
1488 return QUOTA_NL_NOWARN;
1490 if (tspace - space <= dquot->dq_dqb.dqb_bsoftlimit)
1491 return QUOTA_NL_BSOFTBELOW;
1492 if (tspace >= dquot->dq_dqb.dqb_bhardlimit &&
1493 tspace - space < dquot->dq_dqb.dqb_bhardlimit)
1494 return QUOTA_NL_BHARDBELOW;
1495 return QUOTA_NL_NOWARN;
1498 static int inode_quota_active(const struct inode *inode)
1500 struct super_block *sb = inode->i_sb;
1502 if (IS_NOQUOTA(inode))
1504 return sb_any_quota_loaded(sb) & ~sb_any_quota_suspended(sb);
1508 * Initialize quota pointers in inode
1510 * It is better to call this function outside of any transaction as it
1511 * might need a lot of space in journal for dquot structure allocation.
1513 static int __dquot_initialize(struct inode *inode, int type)
1515 int cnt, init_needed = 0;
1516 struct dquot **dquots, *got[MAXQUOTAS] = {};
1517 struct super_block *sb = inode->i_sb;
1521 if (!inode_quota_active(inode))
1524 dquots = i_dquot(inode);
1526 /* First get references to structures we might need. */
1527 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1531 struct dquot *dquot;
1533 if (type != -1 && cnt != type)
1536 * The i_dquot should have been initialized in most cases,
1537 * we check it without locking here to avoid unnecessary
1538 * dqget()/dqput() calls.
1543 if (!sb_has_quota_active(sb, cnt))
1550 qid = make_kqid_uid(inode->i_uid);
1553 qid = make_kqid_gid(inode->i_gid);
1556 rc = inode->i_sb->dq_op->get_projid(inode, &projid);
1559 qid = make_kqid_projid(projid);
1562 dquot = dqget(sb, qid);
1563 if (IS_ERR(dquot)) {
1564 /* We raced with somebody turning quotas off... */
1565 if (PTR_ERR(dquot) != -ESRCH) {
1566 ret = PTR_ERR(dquot);
1574 /* All required i_dquot has been initialized */
1578 spin_lock(&dq_data_lock);
1579 if (IS_NOQUOTA(inode))
1581 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1582 if (type != -1 && cnt != type)
1584 /* Avoid races with quotaoff() */
1585 if (!sb_has_quota_active(sb, cnt))
1587 /* We could race with quotaon or dqget() could have failed */
1591 dquots[cnt] = got[cnt];
1594 * Make quota reservation system happy if someone
1595 * did a write before quota was turned on
1597 rsv = inode_get_rsv_space(inode);
1598 if (unlikely(rsv)) {
1599 spin_lock(&inode->i_lock);
1600 /* Get reservation again under proper lock */
1601 rsv = __inode_get_rsv_space(inode);
1602 spin_lock(&dquots[cnt]->dq_dqb_lock);
1603 dquots[cnt]->dq_dqb.dqb_rsvspace += rsv;
1604 spin_unlock(&dquots[cnt]->dq_dqb_lock);
1605 spin_unlock(&inode->i_lock);
1610 spin_unlock(&dq_data_lock);
1612 /* Drop unused references */
1618 int dquot_initialize(struct inode *inode)
1620 return __dquot_initialize(inode, -1);
1622 EXPORT_SYMBOL(dquot_initialize);
1624 bool dquot_initialize_needed(struct inode *inode)
1626 struct dquot **dquots;
1629 if (!inode_quota_active(inode))
1632 dquots = i_dquot(inode);
1633 for (i = 0; i < MAXQUOTAS; i++)
1634 if (!dquots[i] && sb_has_quota_active(inode->i_sb, i))
1638 EXPORT_SYMBOL(dquot_initialize_needed);
1641 * Release all quotas referenced by inode.
1643 * This function only be called on inode free or converting
1644 * a file to quota file, no other users for the i_dquot in
1645 * both cases, so we needn't call synchronize_srcu() after
1648 static void __dquot_drop(struct inode *inode)
1651 struct dquot **dquots = i_dquot(inode);
1652 struct dquot *put[MAXQUOTAS];
1654 spin_lock(&dq_data_lock);
1655 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1656 put[cnt] = dquots[cnt];
1659 spin_unlock(&dq_data_lock);
1663 void dquot_drop(struct inode *inode)
1665 struct dquot * const *dquots;
1668 if (IS_NOQUOTA(inode))
1672 * Test before calling to rule out calls from proc and such
1673 * where we are not allowed to block. Note that this is
1674 * actually reliable test even without the lock - the caller
1675 * must assure that nobody can come after the DQUOT_DROP and
1676 * add quota pointers back anyway.
1678 dquots = i_dquot(inode);
1679 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1684 if (cnt < MAXQUOTAS)
1685 __dquot_drop(inode);
1687 EXPORT_SYMBOL(dquot_drop);
1690 * inode_reserved_space is managed internally by quota, and protected by
1691 * i_lock similar to i_blocks+i_bytes.
1693 static qsize_t *inode_reserved_space(struct inode * inode)
1695 /* Filesystem must explicitly define it's own method in order to use
1696 * quota reservation interface */
1697 BUG_ON(!inode->i_sb->dq_op->get_reserved_space);
1698 return inode->i_sb->dq_op->get_reserved_space(inode);
1701 static qsize_t __inode_get_rsv_space(struct inode *inode)
1703 if (!inode->i_sb->dq_op->get_reserved_space)
1705 return *inode_reserved_space(inode);
1708 static qsize_t inode_get_rsv_space(struct inode *inode)
1712 if (!inode->i_sb->dq_op->get_reserved_space)
1714 spin_lock(&inode->i_lock);
1715 ret = __inode_get_rsv_space(inode);
1716 spin_unlock(&inode->i_lock);
1721 * This functions updates i_blocks+i_bytes fields and quota information
1722 * (together with appropriate checks).
1724 * NOTE: We absolutely rely on the fact that caller dirties the inode
1725 * (usually helpers in quotaops.h care about this) and holds a handle for
1726 * the current transaction so that dquot write and inode write go into the
1731 * This operation can block, but only after everything is updated
1733 int __dquot_alloc_space(struct inode *inode, qsize_t number, int flags)
1735 int cnt, ret = 0, index;
1736 struct dquot_warn warn[MAXQUOTAS];
1737 int reserve = flags & DQUOT_SPACE_RESERVE;
1738 struct dquot **dquots;
1740 if (!inode_quota_active(inode)) {
1742 spin_lock(&inode->i_lock);
1743 *inode_reserved_space(inode) += number;
1744 spin_unlock(&inode->i_lock);
1746 inode_add_bytes(inode, number);
1751 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1752 warn[cnt].w_type = QUOTA_NL_NOWARN;
1754 dquots = i_dquot(inode);
1755 index = srcu_read_lock(&dquot_srcu);
1756 spin_lock(&inode->i_lock);
1757 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1761 ret = dquot_add_space(dquots[cnt], 0, number, flags,
1764 ret = dquot_add_space(dquots[cnt], number, 0, flags,
1768 /* Back out changes we already did */
1769 for (cnt--; cnt >= 0; cnt--) {
1772 spin_lock(&dquots[cnt]->dq_dqb_lock);
1774 dquot_free_reserved_space(dquots[cnt],
1777 dquot_decr_space(dquots[cnt], number);
1778 spin_unlock(&dquots[cnt]->dq_dqb_lock);
1780 spin_unlock(&inode->i_lock);
1781 goto out_flush_warn;
1785 *inode_reserved_space(inode) += number;
1787 __inode_add_bytes(inode, number);
1788 spin_unlock(&inode->i_lock);
1791 goto out_flush_warn;
1792 mark_all_dquot_dirty(dquots);
1794 srcu_read_unlock(&dquot_srcu, index);
1795 flush_warnings(warn);
1799 EXPORT_SYMBOL(__dquot_alloc_space);
1802 * This operation can block, but only after everything is updated
1804 int dquot_alloc_inode(struct inode *inode)
1806 int cnt, ret = 0, index;
1807 struct dquot_warn warn[MAXQUOTAS];
1808 struct dquot * const *dquots;
1810 if (!inode_quota_active(inode))
1812 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1813 warn[cnt].w_type = QUOTA_NL_NOWARN;
1815 dquots = i_dquot(inode);
1816 index = srcu_read_lock(&dquot_srcu);
1817 spin_lock(&inode->i_lock);
1818 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1821 ret = dquot_add_inodes(dquots[cnt], 1, &warn[cnt]);
1823 for (cnt--; cnt >= 0; cnt--) {
1826 /* Back out changes we already did */
1827 spin_lock(&dquots[cnt]->dq_dqb_lock);
1828 dquot_decr_inodes(dquots[cnt], 1);
1829 spin_unlock(&dquots[cnt]->dq_dqb_lock);
1836 spin_unlock(&inode->i_lock);
1838 mark_all_dquot_dirty(dquots);
1839 srcu_read_unlock(&dquot_srcu, index);
1840 flush_warnings(warn);
1843 EXPORT_SYMBOL(dquot_alloc_inode);
1846 * Convert in-memory reserved quotas to real consumed quotas
1848 int dquot_claim_space_nodirty(struct inode *inode, qsize_t number)
1850 struct dquot **dquots;
1853 if (!inode_quota_active(inode)) {
1854 spin_lock(&inode->i_lock);
1855 *inode_reserved_space(inode) -= number;
1856 __inode_add_bytes(inode, number);
1857 spin_unlock(&inode->i_lock);
1861 dquots = i_dquot(inode);
1862 index = srcu_read_lock(&dquot_srcu);
1863 spin_lock(&inode->i_lock);
1864 /* Claim reserved quotas to allocated quotas */
1865 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1867 struct dquot *dquot = dquots[cnt];
1869 spin_lock(&dquot->dq_dqb_lock);
1870 if (WARN_ON_ONCE(dquot->dq_dqb.dqb_rsvspace < number))
1871 number = dquot->dq_dqb.dqb_rsvspace;
1872 dquot->dq_dqb.dqb_curspace += number;
1873 dquot->dq_dqb.dqb_rsvspace -= number;
1874 spin_unlock(&dquot->dq_dqb_lock);
1877 /* Update inode bytes */
1878 *inode_reserved_space(inode) -= number;
1879 __inode_add_bytes(inode, number);
1880 spin_unlock(&inode->i_lock);
1881 mark_all_dquot_dirty(dquots);
1882 srcu_read_unlock(&dquot_srcu, index);
1885 EXPORT_SYMBOL(dquot_claim_space_nodirty);
1888 * Convert allocated space back to in-memory reserved quotas
1890 void dquot_reclaim_space_nodirty(struct inode *inode, qsize_t number)
1892 struct dquot **dquots;
1895 if (!inode_quota_active(inode)) {
1896 spin_lock(&inode->i_lock);
1897 *inode_reserved_space(inode) += number;
1898 __inode_sub_bytes(inode, number);
1899 spin_unlock(&inode->i_lock);
1903 dquots = i_dquot(inode);
1904 index = srcu_read_lock(&dquot_srcu);
1905 spin_lock(&inode->i_lock);
1906 /* Claim reserved quotas to allocated quotas */
1907 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1909 struct dquot *dquot = dquots[cnt];
1911 spin_lock(&dquot->dq_dqb_lock);
1912 if (WARN_ON_ONCE(dquot->dq_dqb.dqb_curspace < number))
1913 number = dquot->dq_dqb.dqb_curspace;
1914 dquot->dq_dqb.dqb_rsvspace += number;
1915 dquot->dq_dqb.dqb_curspace -= number;
1916 spin_unlock(&dquot->dq_dqb_lock);
1919 /* Update inode bytes */
1920 *inode_reserved_space(inode) += number;
1921 __inode_sub_bytes(inode, number);
1922 spin_unlock(&inode->i_lock);
1923 mark_all_dquot_dirty(dquots);
1924 srcu_read_unlock(&dquot_srcu, index);
1927 EXPORT_SYMBOL(dquot_reclaim_space_nodirty);
1930 * This operation can block, but only after everything is updated
1932 void __dquot_free_space(struct inode *inode, qsize_t number, int flags)
1935 struct dquot_warn warn[MAXQUOTAS];
1936 struct dquot **dquots;
1937 int reserve = flags & DQUOT_SPACE_RESERVE, index;
1939 if (!inode_quota_active(inode)) {
1941 spin_lock(&inode->i_lock);
1942 *inode_reserved_space(inode) -= number;
1943 spin_unlock(&inode->i_lock);
1945 inode_sub_bytes(inode, number);
1950 dquots = i_dquot(inode);
1951 index = srcu_read_lock(&dquot_srcu);
1952 spin_lock(&inode->i_lock);
1953 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1956 warn[cnt].w_type = QUOTA_NL_NOWARN;
1959 spin_lock(&dquots[cnt]->dq_dqb_lock);
1960 wtype = info_bdq_free(dquots[cnt], number);
1961 if (wtype != QUOTA_NL_NOWARN)
1962 prepare_warning(&warn[cnt], dquots[cnt], wtype);
1964 dquot_free_reserved_space(dquots[cnt], number);
1966 dquot_decr_space(dquots[cnt], number);
1967 spin_unlock(&dquots[cnt]->dq_dqb_lock);
1970 *inode_reserved_space(inode) -= number;
1972 __inode_sub_bytes(inode, number);
1973 spin_unlock(&inode->i_lock);
1977 mark_all_dquot_dirty(dquots);
1979 srcu_read_unlock(&dquot_srcu, index);
1980 flush_warnings(warn);
1982 EXPORT_SYMBOL(__dquot_free_space);
1985 * This operation can block, but only after everything is updated
1987 void dquot_free_inode(struct inode *inode)
1990 struct dquot_warn warn[MAXQUOTAS];
1991 struct dquot * const *dquots;
1994 if (!inode_quota_active(inode))
1997 dquots = i_dquot(inode);
1998 index = srcu_read_lock(&dquot_srcu);
1999 spin_lock(&inode->i_lock);
2000 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
2003 warn[cnt].w_type = QUOTA_NL_NOWARN;
2006 spin_lock(&dquots[cnt]->dq_dqb_lock);
2007 wtype = info_idq_free(dquots[cnt], 1);
2008 if (wtype != QUOTA_NL_NOWARN)
2009 prepare_warning(&warn[cnt], dquots[cnt], wtype);
2010 dquot_decr_inodes(dquots[cnt], 1);
2011 spin_unlock(&dquots[cnt]->dq_dqb_lock);
2013 spin_unlock(&inode->i_lock);
2014 mark_all_dquot_dirty(dquots);
2015 srcu_read_unlock(&dquot_srcu, index);
2016 flush_warnings(warn);
2018 EXPORT_SYMBOL(dquot_free_inode);
2021 * Transfer the number of inode and blocks from one diskquota to an other.
2022 * On success, dquot references in transfer_to are consumed and references
2023 * to original dquots that need to be released are placed there. On failure,
2024 * references are kept untouched.
2026 * This operation can block, but only after everything is updated
2027 * A transaction must be started when entering this function.
2029 * We are holding reference on transfer_from & transfer_to, no need to
2030 * protect them by srcu_read_lock().
2032 int __dquot_transfer(struct inode *inode, struct dquot **transfer_to)
2035 qsize_t rsv_space = 0;
2036 qsize_t inode_usage = 1;
2037 struct dquot *transfer_from[MAXQUOTAS] = {};
2039 char is_valid[MAXQUOTAS] = {};
2040 struct dquot_warn warn_to[MAXQUOTAS];
2041 struct dquot_warn warn_from_inodes[MAXQUOTAS];
2042 struct dquot_warn warn_from_space[MAXQUOTAS];
2044 if (IS_NOQUOTA(inode))
2047 if (inode->i_sb->dq_op->get_inode_usage) {
2048 ret = inode->i_sb->dq_op->get_inode_usage(inode, &inode_usage);
2053 /* Initialize the arrays */
2054 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
2055 warn_to[cnt].w_type = QUOTA_NL_NOWARN;
2056 warn_from_inodes[cnt].w_type = QUOTA_NL_NOWARN;
2057 warn_from_space[cnt].w_type = QUOTA_NL_NOWARN;
2060 spin_lock(&dq_data_lock);
2061 spin_lock(&inode->i_lock);
2062 if (IS_NOQUOTA(inode)) { /* File without quota accounting? */
2063 spin_unlock(&inode->i_lock);
2064 spin_unlock(&dq_data_lock);
2067 cur_space = __inode_get_bytes(inode);
2068 rsv_space = __inode_get_rsv_space(inode);
2070 * Build the transfer_from list, check limits, and update usage in
2071 * the target structures.
2073 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
2075 * Skip changes for same uid or gid or for turned off quota-type.
2077 if (!transfer_to[cnt])
2079 /* Avoid races with quotaoff() */
2080 if (!sb_has_quota_active(inode->i_sb, cnt))
2083 transfer_from[cnt] = i_dquot(inode)[cnt];
2084 ret = dquot_add_inodes(transfer_to[cnt], inode_usage,
2088 ret = dquot_add_space(transfer_to[cnt], cur_space, rsv_space,
2089 DQUOT_SPACE_WARN, &warn_to[cnt]);
2091 spin_lock(&transfer_to[cnt]->dq_dqb_lock);
2092 dquot_decr_inodes(transfer_to[cnt], inode_usage);
2093 spin_unlock(&transfer_to[cnt]->dq_dqb_lock);
2098 /* Decrease usage for source structures and update quota pointers */
2099 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
2102 /* Due to IO error we might not have transfer_from[] structure */
2103 if (transfer_from[cnt]) {
2106 spin_lock(&transfer_from[cnt]->dq_dqb_lock);
2107 wtype = info_idq_free(transfer_from[cnt], inode_usage);
2108 if (wtype != QUOTA_NL_NOWARN)
2109 prepare_warning(&warn_from_inodes[cnt],
2110 transfer_from[cnt], wtype);
2111 wtype = info_bdq_free(transfer_from[cnt],
2112 cur_space + rsv_space);
2113 if (wtype != QUOTA_NL_NOWARN)
2114 prepare_warning(&warn_from_space[cnt],
2115 transfer_from[cnt], wtype);
2116 dquot_decr_inodes(transfer_from[cnt], inode_usage);
2117 dquot_decr_space(transfer_from[cnt], cur_space);
2118 dquot_free_reserved_space(transfer_from[cnt],
2120 spin_unlock(&transfer_from[cnt]->dq_dqb_lock);
2122 i_dquot(inode)[cnt] = transfer_to[cnt];
2124 spin_unlock(&inode->i_lock);
2125 spin_unlock(&dq_data_lock);
2127 mark_all_dquot_dirty(transfer_from);
2128 mark_all_dquot_dirty(transfer_to);
2129 flush_warnings(warn_to);
2130 flush_warnings(warn_from_inodes);
2131 flush_warnings(warn_from_space);
2132 /* Pass back references to put */
2133 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
2135 transfer_to[cnt] = transfer_from[cnt];
2138 /* Back out changes we already did */
2139 for (cnt--; cnt >= 0; cnt--) {
2142 spin_lock(&transfer_to[cnt]->dq_dqb_lock);
2143 dquot_decr_inodes(transfer_to[cnt], inode_usage);
2144 dquot_decr_space(transfer_to[cnt], cur_space);
2145 dquot_free_reserved_space(transfer_to[cnt], rsv_space);
2146 spin_unlock(&transfer_to[cnt]->dq_dqb_lock);
2148 spin_unlock(&inode->i_lock);
2149 spin_unlock(&dq_data_lock);
2150 flush_warnings(warn_to);
2153 EXPORT_SYMBOL(__dquot_transfer);
2155 /* Wrapper for transferring ownership of an inode for uid/gid only
2156 * Called from FSXXX_setattr()
2158 int dquot_transfer(struct inode *inode, struct iattr *iattr)
2160 struct dquot *transfer_to[MAXQUOTAS] = {};
2161 struct dquot *dquot;
2162 struct super_block *sb = inode->i_sb;
2165 if (!inode_quota_active(inode))
2168 if (iattr->ia_valid & ATTR_UID && !uid_eq(iattr->ia_uid, inode->i_uid)){
2169 dquot = dqget(sb, make_kqid_uid(iattr->ia_uid));
2170 if (IS_ERR(dquot)) {
2171 if (PTR_ERR(dquot) != -ESRCH) {
2172 ret = PTR_ERR(dquot);
2177 transfer_to[USRQUOTA] = dquot;
2179 if (iattr->ia_valid & ATTR_GID && !gid_eq(iattr->ia_gid, inode->i_gid)){
2180 dquot = dqget(sb, make_kqid_gid(iattr->ia_gid));
2181 if (IS_ERR(dquot)) {
2182 if (PTR_ERR(dquot) != -ESRCH) {
2183 ret = PTR_ERR(dquot);
2188 transfer_to[GRPQUOTA] = dquot;
2190 ret = __dquot_transfer(inode, transfer_to);
2192 dqput_all(transfer_to);
2195 EXPORT_SYMBOL(dquot_transfer);
2198 * Write info of quota file to disk
2200 int dquot_commit_info(struct super_block *sb, int type)
2202 struct quota_info *dqopt = sb_dqopt(sb);
2204 return dqopt->ops[type]->write_file_info(sb, type);
2206 EXPORT_SYMBOL(dquot_commit_info);
2208 int dquot_get_next_id(struct super_block *sb, struct kqid *qid)
2210 struct quota_info *dqopt = sb_dqopt(sb);
2212 if (!sb_has_quota_active(sb, qid->type))
2214 if (!dqopt->ops[qid->type]->get_next_id)
2216 return dqopt->ops[qid->type]->get_next_id(sb, qid);
2218 EXPORT_SYMBOL(dquot_get_next_id);
2221 * Definitions of diskquota operations.
2223 const struct dquot_operations dquot_operations = {
2224 .write_dquot = dquot_commit,
2225 .acquire_dquot = dquot_acquire,
2226 .release_dquot = dquot_release,
2227 .mark_dirty = dquot_mark_dquot_dirty,
2228 .write_info = dquot_commit_info,
2229 .alloc_dquot = dquot_alloc,
2230 .destroy_dquot = dquot_destroy,
2231 .get_next_id = dquot_get_next_id,
2233 EXPORT_SYMBOL(dquot_operations);
2236 * Generic helper for ->open on filesystems supporting disk quotas.
2238 int dquot_file_open(struct inode *inode, struct file *file)
2242 error = generic_file_open(inode, file);
2243 if (!error && (file->f_mode & FMODE_WRITE))
2244 error = dquot_initialize(inode);
2247 EXPORT_SYMBOL(dquot_file_open);
2249 static void vfs_cleanup_quota_inode(struct super_block *sb, int type)
2251 struct quota_info *dqopt = sb_dqopt(sb);
2252 struct inode *inode = dqopt->files[type];
2256 if (!(dqopt->flags & DQUOT_QUOTA_SYS_FILE)) {
2258 inode->i_flags &= ~S_NOQUOTA;
2259 inode_unlock(inode);
2261 dqopt->files[type] = NULL;
2266 * Turn quota off on a device. type == -1 ==> quotaoff for all types (umount)
2268 int dquot_disable(struct super_block *sb, int type, unsigned int flags)
2271 struct quota_info *dqopt = sb_dqopt(sb);
2273 /* s_umount should be held in exclusive mode */
2274 if (WARN_ON_ONCE(down_read_trylock(&sb->s_umount)))
2275 up_read(&sb->s_umount);
2277 /* Cannot turn off usage accounting without turning off limits, or
2278 * suspend quotas and simultaneously turn quotas off. */
2279 if ((flags & DQUOT_USAGE_ENABLED && !(flags & DQUOT_LIMITS_ENABLED))
2280 || (flags & DQUOT_SUSPENDED && flags & (DQUOT_LIMITS_ENABLED |
2281 DQUOT_USAGE_ENABLED)))
2285 * Skip everything if there's nothing to do. We have to do this because
2286 * sometimes we are called when fill_super() failed and calling
2287 * sync_fs() in such cases does no good.
2289 if (!sb_any_quota_loaded(sb))
2292 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
2293 if (type != -1 && cnt != type)
2295 if (!sb_has_quota_loaded(sb, cnt))
2298 if (flags & DQUOT_SUSPENDED) {
2299 spin_lock(&dq_state_lock);
2301 dquot_state_flag(DQUOT_SUSPENDED, cnt);
2302 spin_unlock(&dq_state_lock);
2304 spin_lock(&dq_state_lock);
2305 dqopt->flags &= ~dquot_state_flag(flags, cnt);
2306 /* Turning off suspended quotas? */
2307 if (!sb_has_quota_loaded(sb, cnt) &&
2308 sb_has_quota_suspended(sb, cnt)) {
2309 dqopt->flags &= ~dquot_state_flag(
2310 DQUOT_SUSPENDED, cnt);
2311 spin_unlock(&dq_state_lock);
2312 vfs_cleanup_quota_inode(sb, cnt);
2315 spin_unlock(&dq_state_lock);
2318 /* We still have to keep quota loaded? */
2319 if (sb_has_quota_loaded(sb, cnt) && !(flags & DQUOT_SUSPENDED))
2322 /* Note: these are blocking operations */
2323 drop_dquot_ref(sb, cnt);
2324 invalidate_dquots(sb, cnt);
2326 * Now all dquots should be invalidated, all writes done so we
2327 * should be only users of the info. No locks needed.
2329 if (info_dirty(&dqopt->info[cnt]))
2330 sb->dq_op->write_info(sb, cnt);
2331 if (dqopt->ops[cnt]->free_file_info)
2332 dqopt->ops[cnt]->free_file_info(sb, cnt);
2333 put_quota_format(dqopt->info[cnt].dqi_format);
2334 dqopt->info[cnt].dqi_flags = 0;
2335 dqopt->info[cnt].dqi_igrace = 0;
2336 dqopt->info[cnt].dqi_bgrace = 0;
2337 dqopt->ops[cnt] = NULL;
2340 /* Skip syncing and setting flags if quota files are hidden */
2341 if (dqopt->flags & DQUOT_QUOTA_SYS_FILE)
2344 /* Sync the superblock so that buffers with quota data are written to
2345 * disk (and so userspace sees correct data afterwards). */
2346 if (sb->s_op->sync_fs)
2347 sb->s_op->sync_fs(sb, 1);
2348 sync_blockdev(sb->s_bdev);
2349 /* Now the quota files are just ordinary files and we can set the
2350 * inode flags back. Moreover we discard the pagecache so that
2351 * userspace sees the writes we did bypassing the pagecache. We
2352 * must also discard the blockdev buffers so that we see the
2353 * changes done by userspace on the next quotaon() */
2354 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
2355 if (!sb_has_quota_loaded(sb, cnt) && dqopt->files[cnt]) {
2356 inode_lock(dqopt->files[cnt]);
2357 truncate_inode_pages(&dqopt->files[cnt]->i_data, 0);
2358 inode_unlock(dqopt->files[cnt]);
2361 invalidate_bdev(sb->s_bdev);
2363 /* We are done when suspending quotas */
2364 if (flags & DQUOT_SUSPENDED)
2367 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
2368 if (!sb_has_quota_loaded(sb, cnt))
2369 vfs_cleanup_quota_inode(sb, cnt);
2372 EXPORT_SYMBOL(dquot_disable);
2374 int dquot_quota_off(struct super_block *sb, int type)
2376 return dquot_disable(sb, type,
2377 DQUOT_USAGE_ENABLED | DQUOT_LIMITS_ENABLED);
2379 EXPORT_SYMBOL(dquot_quota_off);
2382 * Turn quotas on on a device
2385 static int vfs_setup_quota_inode(struct inode *inode, int type)
2387 struct super_block *sb = inode->i_sb;
2388 struct quota_info *dqopt = sb_dqopt(sb);
2390 if (is_bad_inode(inode))
2392 if (!S_ISREG(inode->i_mode))
2394 if (IS_RDONLY(inode))
2396 if (sb_has_quota_loaded(sb, type))
2399 dqopt->files[type] = igrab(inode);
2400 if (!dqopt->files[type])
2402 if (!(dqopt->flags & DQUOT_QUOTA_SYS_FILE)) {
2403 /* We don't want quota and atime on quota files (deadlocks
2404 * possible) Also nobody should write to the file - we use
2405 * special IO operations which ignore the immutable bit. */
2407 inode->i_flags |= S_NOQUOTA;
2408 inode_unlock(inode);
2410 * When S_NOQUOTA is set, remove dquot references as no more
2411 * references can be added
2413 __dquot_drop(inode);
2418 int dquot_load_quota_sb(struct super_block *sb, int type, int format_id,
2421 struct quota_format_type *fmt = find_quota_format(format_id);
2422 struct quota_info *dqopt = sb_dqopt(sb);
2425 /* Just unsuspend quotas? */
2426 BUG_ON(flags & DQUOT_SUSPENDED);
2427 /* s_umount should be held in exclusive mode */
2428 if (WARN_ON_ONCE(down_read_trylock(&sb->s_umount)))
2429 up_read(&sb->s_umount);
2433 if (!sb->s_op->quota_write || !sb->s_op->quota_read ||
2434 (type == PRJQUOTA && sb->dq_op->get_projid == NULL)) {
2438 /* Filesystems outside of init_user_ns not yet supported */
2439 if (sb->s_user_ns != &init_user_ns) {
2443 /* Usage always has to be set... */
2444 if (!(flags & DQUOT_USAGE_ENABLED)) {
2448 if (sb_has_quota_loaded(sb, type)) {
2453 if (!(dqopt->flags & DQUOT_QUOTA_SYS_FILE)) {
2454 /* As we bypass the pagecache we must now flush all the
2455 * dirty data and invalidate caches so that kernel sees
2456 * changes from userspace. It is not enough to just flush
2457 * the quota file since if blocksize < pagesize, invalidation
2458 * of the cache could fail because of other unrelated dirty
2460 sync_filesystem(sb);
2461 invalidate_bdev(sb->s_bdev);
2465 if (!fmt->qf_ops->check_quota_file(sb, type))
2468 dqopt->ops[type] = fmt->qf_ops;
2469 dqopt->info[type].dqi_format = fmt;
2470 dqopt->info[type].dqi_fmt_id = format_id;
2471 INIT_LIST_HEAD(&dqopt->info[type].dqi_dirty_list);
2472 error = dqopt->ops[type]->read_file_info(sb, type);
2475 if (dqopt->flags & DQUOT_QUOTA_SYS_FILE) {
2476 spin_lock(&dq_data_lock);
2477 dqopt->info[type].dqi_flags |= DQF_SYS_FILE;
2478 spin_unlock(&dq_data_lock);
2480 spin_lock(&dq_state_lock);
2481 dqopt->flags |= dquot_state_flag(flags, type);
2482 spin_unlock(&dq_state_lock);
2484 error = add_dquot_ref(sb, type);
2486 dquot_disable(sb, type,
2487 DQUOT_USAGE_ENABLED | DQUOT_LIMITS_ENABLED);
2491 put_quota_format(fmt);
2495 EXPORT_SYMBOL(dquot_load_quota_sb);
2498 * More powerful function for turning on quotas on given quota inode allowing
2499 * setting of individual quota flags
2501 int dquot_load_quota_inode(struct inode *inode, int type, int format_id,
2506 err = vfs_setup_quota_inode(inode, type);
2509 err = dquot_load_quota_sb(inode->i_sb, type, format_id, flags);
2511 vfs_cleanup_quota_inode(inode->i_sb, type);
2514 EXPORT_SYMBOL(dquot_load_quota_inode);
2516 /* Reenable quotas on remount RW */
2517 int dquot_resume(struct super_block *sb, int type)
2519 struct quota_info *dqopt = sb_dqopt(sb);
2523 /* s_umount should be held in exclusive mode */
2524 if (WARN_ON_ONCE(down_read_trylock(&sb->s_umount)))
2525 up_read(&sb->s_umount);
2527 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
2528 if (type != -1 && cnt != type)
2530 if (!sb_has_quota_suspended(sb, cnt))
2533 spin_lock(&dq_state_lock);
2534 flags = dqopt->flags & dquot_state_flag(DQUOT_USAGE_ENABLED |
2535 DQUOT_LIMITS_ENABLED,
2537 dqopt->flags &= ~dquot_state_flag(DQUOT_STATE_FLAGS, cnt);
2538 spin_unlock(&dq_state_lock);
2540 flags = dquot_generic_flag(flags, cnt);
2541 ret = dquot_load_quota_sb(sb, cnt, dqopt->info[cnt].dqi_fmt_id,
2544 vfs_cleanup_quota_inode(sb, cnt);
2549 EXPORT_SYMBOL(dquot_resume);
2551 int dquot_quota_on(struct super_block *sb, int type, int format_id,
2552 const struct path *path)
2554 int error = security_quota_on(path->dentry);
2557 /* Quota file not on the same filesystem? */
2558 if (path->dentry->d_sb != sb)
2561 error = dquot_load_quota_inode(d_inode(path->dentry), type,
2562 format_id, DQUOT_USAGE_ENABLED |
2563 DQUOT_LIMITS_ENABLED);
2566 EXPORT_SYMBOL(dquot_quota_on);
2569 * This function is used when filesystem needs to initialize quotas
2570 * during mount time.
2572 int dquot_quota_on_mount(struct super_block *sb, char *qf_name,
2573 int format_id, int type)
2575 struct dentry *dentry;
2578 dentry = lookup_positive_unlocked(qf_name, sb->s_root, strlen(qf_name));
2580 return PTR_ERR(dentry);
2582 error = security_quota_on(dentry);
2584 error = dquot_load_quota_inode(d_inode(dentry), type, format_id,
2585 DQUOT_USAGE_ENABLED | DQUOT_LIMITS_ENABLED);
2590 EXPORT_SYMBOL(dquot_quota_on_mount);
2592 static int dquot_quota_enable(struct super_block *sb, unsigned int flags)
2596 struct quota_info *dqopt = sb_dqopt(sb);
2598 if (!(dqopt->flags & DQUOT_QUOTA_SYS_FILE))
2600 /* Accounting cannot be turned on while fs is mounted */
2601 flags &= ~(FS_QUOTA_UDQ_ACCT | FS_QUOTA_GDQ_ACCT | FS_QUOTA_PDQ_ACCT);
2604 for (type = 0; type < MAXQUOTAS; type++) {
2605 if (!(flags & qtype_enforce_flag(type)))
2607 /* Can't enforce without accounting */
2608 if (!sb_has_quota_usage_enabled(sb, type)) {
2612 if (sb_has_quota_limits_enabled(sb, type)) {
2616 spin_lock(&dq_state_lock);
2617 dqopt->flags |= dquot_state_flag(DQUOT_LIMITS_ENABLED, type);
2618 spin_unlock(&dq_state_lock);
2622 /* Backout enforcement enablement we already did */
2623 for (type--; type >= 0; type--) {
2624 if (flags & qtype_enforce_flag(type))
2625 dquot_disable(sb, type, DQUOT_LIMITS_ENABLED);
2627 /* Error code translation for better compatibility with XFS */
2633 static int dquot_quota_disable(struct super_block *sb, unsigned int flags)
2637 struct quota_info *dqopt = sb_dqopt(sb);
2639 if (!(dqopt->flags & DQUOT_QUOTA_SYS_FILE))
2642 * We don't support turning off accounting via quotactl. In principle
2643 * quota infrastructure can do this but filesystems don't expect
2644 * userspace to be able to do it.
2647 (FS_QUOTA_UDQ_ACCT | FS_QUOTA_GDQ_ACCT | FS_QUOTA_PDQ_ACCT))
2650 /* Filter out limits not enabled */
2651 for (type = 0; type < MAXQUOTAS; type++)
2652 if (!sb_has_quota_limits_enabled(sb, type))
2653 flags &= ~qtype_enforce_flag(type);
2657 for (type = 0; type < MAXQUOTAS; type++) {
2658 if (flags & qtype_enforce_flag(type)) {
2659 ret = dquot_disable(sb, type, DQUOT_LIMITS_ENABLED);
2666 /* Backout enforcement disabling we already did */
2667 for (type--; type >= 0; type--) {
2668 if (flags & qtype_enforce_flag(type)) {
2669 spin_lock(&dq_state_lock);
2671 dquot_state_flag(DQUOT_LIMITS_ENABLED, type);
2672 spin_unlock(&dq_state_lock);
2678 /* Generic routine for getting common part of quota structure */
2679 static void do_get_dqblk(struct dquot *dquot, struct qc_dqblk *di)
2681 struct mem_dqblk *dm = &dquot->dq_dqb;
2683 memset(di, 0, sizeof(*di));
2684 spin_lock(&dquot->dq_dqb_lock);
2685 di->d_spc_hardlimit = dm->dqb_bhardlimit;
2686 di->d_spc_softlimit = dm->dqb_bsoftlimit;
2687 di->d_ino_hardlimit = dm->dqb_ihardlimit;
2688 di->d_ino_softlimit = dm->dqb_isoftlimit;
2689 di->d_space = dm->dqb_curspace + dm->dqb_rsvspace;
2690 di->d_ino_count = dm->dqb_curinodes;
2691 di->d_spc_timer = dm->dqb_btime;
2692 di->d_ino_timer = dm->dqb_itime;
2693 spin_unlock(&dquot->dq_dqb_lock);
2696 int dquot_get_dqblk(struct super_block *sb, struct kqid qid,
2697 struct qc_dqblk *di)
2699 struct dquot *dquot;
2701 dquot = dqget(sb, qid);
2703 return PTR_ERR(dquot);
2704 do_get_dqblk(dquot, di);
2709 EXPORT_SYMBOL(dquot_get_dqblk);
2711 int dquot_get_next_dqblk(struct super_block *sb, struct kqid *qid,
2712 struct qc_dqblk *di)
2714 struct dquot *dquot;
2717 if (!sb->dq_op->get_next_id)
2719 err = sb->dq_op->get_next_id(sb, qid);
2722 dquot = dqget(sb, *qid);
2724 return PTR_ERR(dquot);
2725 do_get_dqblk(dquot, di);
2730 EXPORT_SYMBOL(dquot_get_next_dqblk);
2732 #define VFS_QC_MASK \
2733 (QC_SPACE | QC_SPC_SOFT | QC_SPC_HARD | \
2734 QC_INO_COUNT | QC_INO_SOFT | QC_INO_HARD | \
2735 QC_SPC_TIMER | QC_INO_TIMER)
2737 /* Generic routine for setting common part of quota structure */
2738 static int do_set_dqblk(struct dquot *dquot, struct qc_dqblk *di)
2740 struct mem_dqblk *dm = &dquot->dq_dqb;
2741 int check_blim = 0, check_ilim = 0;
2742 struct mem_dqinfo *dqi = &sb_dqopt(dquot->dq_sb)->info[dquot->dq_id.type];
2744 if (di->d_fieldmask & ~VFS_QC_MASK)
2747 if (((di->d_fieldmask & QC_SPC_SOFT) &&
2748 di->d_spc_softlimit > dqi->dqi_max_spc_limit) ||
2749 ((di->d_fieldmask & QC_SPC_HARD) &&
2750 di->d_spc_hardlimit > dqi->dqi_max_spc_limit) ||
2751 ((di->d_fieldmask & QC_INO_SOFT) &&
2752 (di->d_ino_softlimit > dqi->dqi_max_ino_limit)) ||
2753 ((di->d_fieldmask & QC_INO_HARD) &&
2754 (di->d_ino_hardlimit > dqi->dqi_max_ino_limit)))
2757 spin_lock(&dquot->dq_dqb_lock);
2758 if (di->d_fieldmask & QC_SPACE) {
2759 dm->dqb_curspace = di->d_space - dm->dqb_rsvspace;
2761 set_bit(DQ_LASTSET_B + QIF_SPACE_B, &dquot->dq_flags);
2764 if (di->d_fieldmask & QC_SPC_SOFT)
2765 dm->dqb_bsoftlimit = di->d_spc_softlimit;
2766 if (di->d_fieldmask & QC_SPC_HARD)
2767 dm->dqb_bhardlimit = di->d_spc_hardlimit;
2768 if (di->d_fieldmask & (QC_SPC_SOFT | QC_SPC_HARD)) {
2770 set_bit(DQ_LASTSET_B + QIF_BLIMITS_B, &dquot->dq_flags);
2773 if (di->d_fieldmask & QC_INO_COUNT) {
2774 dm->dqb_curinodes = di->d_ino_count;
2776 set_bit(DQ_LASTSET_B + QIF_INODES_B, &dquot->dq_flags);
2779 if (di->d_fieldmask & QC_INO_SOFT)
2780 dm->dqb_isoftlimit = di->d_ino_softlimit;
2781 if (di->d_fieldmask & QC_INO_HARD)
2782 dm->dqb_ihardlimit = di->d_ino_hardlimit;
2783 if (di->d_fieldmask & (QC_INO_SOFT | QC_INO_HARD)) {
2785 set_bit(DQ_LASTSET_B + QIF_ILIMITS_B, &dquot->dq_flags);
2788 if (di->d_fieldmask & QC_SPC_TIMER) {
2789 dm->dqb_btime = di->d_spc_timer;
2791 set_bit(DQ_LASTSET_B + QIF_BTIME_B, &dquot->dq_flags);
2794 if (di->d_fieldmask & QC_INO_TIMER) {
2795 dm->dqb_itime = di->d_ino_timer;
2797 set_bit(DQ_LASTSET_B + QIF_ITIME_B, &dquot->dq_flags);
2801 if (!dm->dqb_bsoftlimit ||
2802 dm->dqb_curspace + dm->dqb_rsvspace <= dm->dqb_bsoftlimit) {
2804 clear_bit(DQ_BLKS_B, &dquot->dq_flags);
2805 } else if (!(di->d_fieldmask & QC_SPC_TIMER))
2806 /* Set grace only if user hasn't provided his own... */
2807 dm->dqb_btime = ktime_get_real_seconds() + dqi->dqi_bgrace;
2810 if (!dm->dqb_isoftlimit ||
2811 dm->dqb_curinodes <= dm->dqb_isoftlimit) {
2813 clear_bit(DQ_INODES_B, &dquot->dq_flags);
2814 } else if (!(di->d_fieldmask & QC_INO_TIMER))
2815 /* Set grace only if user hasn't provided his own... */
2816 dm->dqb_itime = ktime_get_real_seconds() + dqi->dqi_igrace;
2818 if (dm->dqb_bhardlimit || dm->dqb_bsoftlimit || dm->dqb_ihardlimit ||
2820 clear_bit(DQ_FAKE_B, &dquot->dq_flags);
2822 set_bit(DQ_FAKE_B, &dquot->dq_flags);
2823 spin_unlock(&dquot->dq_dqb_lock);
2824 mark_dquot_dirty(dquot);
2829 int dquot_set_dqblk(struct super_block *sb, struct kqid qid,
2830 struct qc_dqblk *di)
2832 struct dquot *dquot;
2835 dquot = dqget(sb, qid);
2836 if (IS_ERR(dquot)) {
2837 rc = PTR_ERR(dquot);
2840 rc = do_set_dqblk(dquot, di);
2845 EXPORT_SYMBOL(dquot_set_dqblk);
2847 /* Generic routine for getting common part of quota file information */
2848 int dquot_get_state(struct super_block *sb, struct qc_state *state)
2850 struct mem_dqinfo *mi;
2851 struct qc_type_state *tstate;
2852 struct quota_info *dqopt = sb_dqopt(sb);
2855 memset(state, 0, sizeof(*state));
2856 for (type = 0; type < MAXQUOTAS; type++) {
2857 if (!sb_has_quota_active(sb, type))
2859 tstate = state->s_state + type;
2860 mi = sb_dqopt(sb)->info + type;
2861 tstate->flags = QCI_ACCT_ENABLED;
2862 spin_lock(&dq_data_lock);
2863 if (mi->dqi_flags & DQF_SYS_FILE)
2864 tstate->flags |= QCI_SYSFILE;
2865 if (mi->dqi_flags & DQF_ROOT_SQUASH)
2866 tstate->flags |= QCI_ROOT_SQUASH;
2867 if (sb_has_quota_limits_enabled(sb, type))
2868 tstate->flags |= QCI_LIMITS_ENFORCED;
2869 tstate->spc_timelimit = mi->dqi_bgrace;
2870 tstate->ino_timelimit = mi->dqi_igrace;
2871 if (dqopt->files[type]) {
2872 tstate->ino = dqopt->files[type]->i_ino;
2873 tstate->blocks = dqopt->files[type]->i_blocks;
2875 tstate->nextents = 1; /* We don't know... */
2876 spin_unlock(&dq_data_lock);
2880 EXPORT_SYMBOL(dquot_get_state);
2882 /* Generic routine for setting common part of quota file information */
2883 int dquot_set_dqinfo(struct super_block *sb, int type, struct qc_info *ii)
2885 struct mem_dqinfo *mi;
2888 if ((ii->i_fieldmask & QC_WARNS_MASK) ||
2889 (ii->i_fieldmask & QC_RT_SPC_TIMER))
2891 if (!sb_has_quota_active(sb, type))
2893 mi = sb_dqopt(sb)->info + type;
2894 if (ii->i_fieldmask & QC_FLAGS) {
2895 if ((ii->i_flags & QCI_ROOT_SQUASH &&
2896 mi->dqi_format->qf_fmt_id != QFMT_VFS_OLD))
2899 spin_lock(&dq_data_lock);
2900 if (ii->i_fieldmask & QC_SPC_TIMER)
2901 mi->dqi_bgrace = ii->i_spc_timelimit;
2902 if (ii->i_fieldmask & QC_INO_TIMER)
2903 mi->dqi_igrace = ii->i_ino_timelimit;
2904 if (ii->i_fieldmask & QC_FLAGS) {
2905 if (ii->i_flags & QCI_ROOT_SQUASH)
2906 mi->dqi_flags |= DQF_ROOT_SQUASH;
2908 mi->dqi_flags &= ~DQF_ROOT_SQUASH;
2910 spin_unlock(&dq_data_lock);
2911 mark_info_dirty(sb, type);
2912 /* Force write to disk */
2913 sb->dq_op->write_info(sb, type);
2916 EXPORT_SYMBOL(dquot_set_dqinfo);
2918 const struct quotactl_ops dquot_quotactl_sysfile_ops = {
2919 .quota_enable = dquot_quota_enable,
2920 .quota_disable = dquot_quota_disable,
2921 .quota_sync = dquot_quota_sync,
2922 .get_state = dquot_get_state,
2923 .set_info = dquot_set_dqinfo,
2924 .get_dqblk = dquot_get_dqblk,
2925 .get_nextdqblk = dquot_get_next_dqblk,
2926 .set_dqblk = dquot_set_dqblk
2928 EXPORT_SYMBOL(dquot_quotactl_sysfile_ops);
2930 static int do_proc_dqstats(struct ctl_table *table, int write,
2931 void *buffer, size_t *lenp, loff_t *ppos)
2933 unsigned int type = (unsigned long *)table->data - dqstats.stat;
2934 s64 value = percpu_counter_sum(&dqstats.counter[type]);
2936 /* Filter negative values for non-monotonic counters */
2937 if (value < 0 && (type == DQST_ALLOC_DQUOTS ||
2938 type == DQST_FREE_DQUOTS))
2941 /* Update global table */
2942 dqstats.stat[type] = value;
2943 return proc_doulongvec_minmax(table, write, buffer, lenp, ppos);
2946 static struct ctl_table fs_dqstats_table[] = {
2948 .procname = "lookups",
2949 .data = &dqstats.stat[DQST_LOOKUPS],
2950 .maxlen = sizeof(unsigned long),
2952 .proc_handler = do_proc_dqstats,
2955 .procname = "drops",
2956 .data = &dqstats.stat[DQST_DROPS],
2957 .maxlen = sizeof(unsigned long),
2959 .proc_handler = do_proc_dqstats,
2962 .procname = "reads",
2963 .data = &dqstats.stat[DQST_READS],
2964 .maxlen = sizeof(unsigned long),
2966 .proc_handler = do_proc_dqstats,
2969 .procname = "writes",
2970 .data = &dqstats.stat[DQST_WRITES],
2971 .maxlen = sizeof(unsigned long),
2973 .proc_handler = do_proc_dqstats,
2976 .procname = "cache_hits",
2977 .data = &dqstats.stat[DQST_CACHE_HITS],
2978 .maxlen = sizeof(unsigned long),
2980 .proc_handler = do_proc_dqstats,
2983 .procname = "allocated_dquots",
2984 .data = &dqstats.stat[DQST_ALLOC_DQUOTS],
2985 .maxlen = sizeof(unsigned long),
2987 .proc_handler = do_proc_dqstats,
2990 .procname = "free_dquots",
2991 .data = &dqstats.stat[DQST_FREE_DQUOTS],
2992 .maxlen = sizeof(unsigned long),
2994 .proc_handler = do_proc_dqstats,
2997 .procname = "syncs",
2998 .data = &dqstats.stat[DQST_SYNCS],
2999 .maxlen = sizeof(unsigned long),
3001 .proc_handler = do_proc_dqstats,
3003 #ifdef CONFIG_PRINT_QUOTA_WARNING
3005 .procname = "warnings",
3006 .data = &flag_print_warnings,
3007 .maxlen = sizeof(int),
3009 .proc_handler = proc_dointvec,
3015 static struct ctl_table fs_table[] = {
3017 .procname = "quota",
3019 .child = fs_dqstats_table,
3024 static struct ctl_table sys_table[] = {
3033 static int __init dquot_init(void)
3036 unsigned long nr_hash, order;
3038 printk(KERN_NOTICE "VFS: Disk quotas %s\n", __DQUOT_VERSION__);
3040 register_sysctl_table(sys_table);
3042 dquot_cachep = kmem_cache_create("dquot",
3043 sizeof(struct dquot), sizeof(unsigned long) * 4,
3044 (SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT|
3045 SLAB_MEM_SPREAD|SLAB_PANIC),
3049 dquot_hash = (struct hlist_head *)__get_free_pages(GFP_KERNEL, order);
3051 panic("Cannot create dquot hash table");
3053 for (i = 0; i < _DQST_DQSTAT_LAST; i++) {
3054 ret = percpu_counter_init(&dqstats.counter[i], 0, GFP_KERNEL);
3056 panic("Cannot create dquot stat counters");
3059 /* Find power-of-two hlist_heads which can fit into allocation */
3060 nr_hash = (1UL << order) * PAGE_SIZE / sizeof(struct hlist_head);
3061 dq_hash_bits = ilog2(nr_hash);
3063 nr_hash = 1UL << dq_hash_bits;
3064 dq_hash_mask = nr_hash - 1;
3065 for (i = 0; i < nr_hash; i++)
3066 INIT_HLIST_HEAD(dquot_hash + i);
3068 pr_info("VFS: Dquot-cache hash table entries: %ld (order %ld,"
3069 " %ld bytes)\n", nr_hash, order, (PAGE_SIZE << order));
3071 if (register_shrinker(&dqcache_shrinker))
3072 panic("Cannot register dquot shrinker");
3076 fs_initcall(dquot_init);