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 __rcu * const *dquots)
408 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
409 dquot = srcu_dereference(dquots[cnt], &dquot_srcu);
411 /* Even in case of error we have to continue */
412 ret = mark_dquot_dirty(dquot);
419 static inline void dqput_all(struct dquot **dquot)
423 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
427 static inline int clear_dquot_dirty(struct dquot *dquot)
429 if (sb_dqopt(dquot->dq_sb)->flags & DQUOT_NOLIST_DIRTY)
430 return test_and_clear_bit(DQ_MOD_B, &dquot->dq_flags);
432 spin_lock(&dq_list_lock);
433 if (!test_and_clear_bit(DQ_MOD_B, &dquot->dq_flags)) {
434 spin_unlock(&dq_list_lock);
437 list_del_init(&dquot->dq_dirty);
438 spin_unlock(&dq_list_lock);
442 void mark_info_dirty(struct super_block *sb, int type)
444 spin_lock(&dq_data_lock);
445 sb_dqopt(sb)->info[type].dqi_flags |= DQF_INFO_DIRTY;
446 spin_unlock(&dq_data_lock);
448 EXPORT_SYMBOL(mark_info_dirty);
451 * Read dquot from disk and alloc space for it
454 int dquot_acquire(struct dquot *dquot)
456 int ret = 0, ret2 = 0;
457 unsigned int memalloc;
458 struct quota_info *dqopt = sb_dqopt(dquot->dq_sb);
460 mutex_lock(&dquot->dq_lock);
461 memalloc = memalloc_nofs_save();
462 if (!test_bit(DQ_READ_B, &dquot->dq_flags)) {
463 ret = dqopt->ops[dquot->dq_id.type]->read_dqblk(dquot);
467 /* Make sure flags update is visible after dquot has been filled */
468 smp_mb__before_atomic();
469 set_bit(DQ_READ_B, &dquot->dq_flags);
470 /* Instantiate dquot if needed */
471 if (!dquot_active(dquot) && !dquot->dq_off) {
472 ret = dqopt->ops[dquot->dq_id.type]->commit_dqblk(dquot);
473 /* Write the info if needed */
474 if (info_dirty(&dqopt->info[dquot->dq_id.type])) {
475 ret2 = dqopt->ops[dquot->dq_id.type]->write_file_info(
476 dquot->dq_sb, dquot->dq_id.type);
486 * Make sure flags update is visible after on-disk struct has been
487 * allocated. Paired with smp_rmb() in dqget().
489 smp_mb__before_atomic();
490 set_bit(DQ_ACTIVE_B, &dquot->dq_flags);
492 memalloc_nofs_restore(memalloc);
493 mutex_unlock(&dquot->dq_lock);
496 EXPORT_SYMBOL(dquot_acquire);
499 * Write dquot to disk
501 int dquot_commit(struct dquot *dquot)
504 unsigned int memalloc;
505 struct quota_info *dqopt = sb_dqopt(dquot->dq_sb);
507 mutex_lock(&dquot->dq_lock);
508 memalloc = memalloc_nofs_save();
509 if (!clear_dquot_dirty(dquot))
511 /* Inactive dquot can be only if there was error during read/init
512 * => we have better not writing it */
513 if (dquot_active(dquot))
514 ret = dqopt->ops[dquot->dq_id.type]->commit_dqblk(dquot);
518 memalloc_nofs_restore(memalloc);
519 mutex_unlock(&dquot->dq_lock);
522 EXPORT_SYMBOL(dquot_commit);
527 int dquot_release(struct dquot *dquot)
529 int ret = 0, ret2 = 0;
530 unsigned int memalloc;
531 struct quota_info *dqopt = sb_dqopt(dquot->dq_sb);
533 mutex_lock(&dquot->dq_lock);
534 memalloc = memalloc_nofs_save();
535 /* Check whether we are not racing with some other dqget() */
536 if (dquot_is_busy(dquot))
538 if (dqopt->ops[dquot->dq_id.type]->release_dqblk) {
539 ret = dqopt->ops[dquot->dq_id.type]->release_dqblk(dquot);
541 if (info_dirty(&dqopt->info[dquot->dq_id.type])) {
542 ret2 = dqopt->ops[dquot->dq_id.type]->write_file_info(
543 dquot->dq_sb, dquot->dq_id.type);
548 clear_bit(DQ_ACTIVE_B, &dquot->dq_flags);
550 memalloc_nofs_restore(memalloc);
551 mutex_unlock(&dquot->dq_lock);
554 EXPORT_SYMBOL(dquot_release);
556 void dquot_destroy(struct dquot *dquot)
558 kmem_cache_free(dquot_cachep, dquot);
560 EXPORT_SYMBOL(dquot_destroy);
562 static inline void do_destroy_dquot(struct dquot *dquot)
564 dquot->dq_sb->dq_op->destroy_dquot(dquot);
567 /* Invalidate all dquots on the list. Note that this function is called after
568 * quota is disabled and pointers from inodes removed so there cannot be new
569 * quota users. There can still be some users of quotas due to inodes being
570 * just deleted or pruned by prune_icache() (those are not attached to any
571 * list) or parallel quotactl call. We have to wait for such users.
573 static void invalidate_dquots(struct super_block *sb, int type)
575 struct dquot *dquot, *tmp;
578 flush_delayed_work("a_release_work);
580 spin_lock(&dq_list_lock);
581 list_for_each_entry_safe(dquot, tmp, &inuse_list, dq_inuse) {
582 if (dquot->dq_sb != sb)
584 if (dquot->dq_id.type != type)
586 /* Wait for dquot users */
587 if (atomic_read(&dquot->dq_count)) {
588 atomic_inc(&dquot->dq_count);
589 spin_unlock(&dq_list_lock);
591 * Once dqput() wakes us up, we know it's time to free
593 * IMPORTANT: we rely on the fact that there is always
594 * at most one process waiting for dquot to free.
595 * Otherwise dq_count would be > 1 and we would never
598 wait_event(dquot_ref_wq,
599 atomic_read(&dquot->dq_count) == 1);
601 /* At this moment dquot() need not exist (it could be
602 * reclaimed by prune_dqcache(). Hence we must
607 * The last user already dropped its reference but dquot didn't
608 * get fully cleaned up yet. Restart the scan which flushes the
609 * work cleaning up released dquots.
611 if (test_bit(DQ_RELEASING_B, &dquot->dq_flags)) {
612 spin_unlock(&dq_list_lock);
616 * Quota now has no users and it has been written on last
619 remove_dquot_hash(dquot);
620 remove_free_dquot(dquot);
622 do_destroy_dquot(dquot);
624 spin_unlock(&dq_list_lock);
627 /* Call callback for every active dquot on given filesystem */
628 int dquot_scan_active(struct super_block *sb,
629 int (*fn)(struct dquot *dquot, unsigned long priv),
632 struct dquot *dquot, *old_dquot = NULL;
635 WARN_ON_ONCE(!rwsem_is_locked(&sb->s_umount));
637 spin_lock(&dq_list_lock);
638 list_for_each_entry(dquot, &inuse_list, dq_inuse) {
639 if (!dquot_active(dquot))
641 if (dquot->dq_sb != sb)
643 /* Now we have active dquot so we can just increase use count */
644 atomic_inc(&dquot->dq_count);
645 spin_unlock(&dq_list_lock);
649 * ->release_dquot() can be racing with us. Our reference
650 * protects us from new calls to it so just wait for any
651 * outstanding call and recheck the DQ_ACTIVE_B after that.
653 wait_on_dquot(dquot);
654 if (dquot_active(dquot)) {
655 ret = fn(dquot, priv);
659 spin_lock(&dq_list_lock);
660 /* We are safe to continue now because our dquot could not
661 * be moved out of the inuse list while we hold the reference */
663 spin_unlock(&dq_list_lock);
668 EXPORT_SYMBOL(dquot_scan_active);
670 static inline int dquot_write_dquot(struct dquot *dquot)
672 int ret = dquot->dq_sb->dq_op->write_dquot(dquot);
674 quota_error(dquot->dq_sb, "Can't write quota structure "
675 "(error %d). Quota may get out of sync!", ret);
676 /* Clear dirty bit anyway to avoid infinite loop. */
677 clear_dquot_dirty(dquot);
682 /* Write all dquot structures to quota files */
683 int dquot_writeback_dquots(struct super_block *sb, int type)
685 struct list_head dirty;
687 struct quota_info *dqopt = sb_dqopt(sb);
691 WARN_ON_ONCE(!rwsem_is_locked(&sb->s_umount));
693 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
694 if (type != -1 && cnt != type)
696 if (!sb_has_quota_active(sb, cnt))
698 spin_lock(&dq_list_lock);
699 /* Move list away to avoid livelock. */
700 list_replace_init(&dqopt->info[cnt].dqi_dirty_list, &dirty);
701 while (!list_empty(&dirty)) {
702 dquot = list_first_entry(&dirty, struct dquot,
705 WARN_ON(!dquot_active(dquot));
706 /* If the dquot is releasing we should not touch it */
707 if (test_bit(DQ_RELEASING_B, &dquot->dq_flags)) {
708 spin_unlock(&dq_list_lock);
709 flush_delayed_work("a_release_work);
710 spin_lock(&dq_list_lock);
714 /* Now we have active dquot from which someone is
715 * holding reference so we can safely just increase
718 spin_unlock(&dq_list_lock);
719 err = dquot_write_dquot(dquot);
723 spin_lock(&dq_list_lock);
725 spin_unlock(&dq_list_lock);
728 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
729 if ((cnt == type || type == -1) && sb_has_quota_active(sb, cnt)
730 && info_dirty(&dqopt->info[cnt]))
731 sb->dq_op->write_info(sb, cnt);
732 dqstats_inc(DQST_SYNCS);
736 EXPORT_SYMBOL(dquot_writeback_dquots);
738 /* Write all dquot structures to disk and make them visible from userspace */
739 int dquot_quota_sync(struct super_block *sb, int type)
741 struct quota_info *dqopt = sb_dqopt(sb);
745 ret = dquot_writeback_dquots(sb, type);
748 if (dqopt->flags & DQUOT_QUOTA_SYS_FILE)
751 /* This is not very clever (and fast) but currently I don't know about
752 * any other simple way of getting quota data to disk and we must get
753 * them there for userspace to be visible... */
754 if (sb->s_op->sync_fs) {
755 ret = sb->s_op->sync_fs(sb, 1);
759 ret = sync_blockdev(sb->s_bdev);
764 * Now when everything is written we can discard the pagecache so
765 * that userspace sees the changes.
767 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
768 if (type != -1 && cnt != type)
770 if (!sb_has_quota_active(sb, cnt))
772 inode_lock(dqopt->files[cnt]);
773 truncate_inode_pages(&dqopt->files[cnt]->i_data, 0);
774 inode_unlock(dqopt->files[cnt]);
779 EXPORT_SYMBOL(dquot_quota_sync);
782 dqcache_shrink_scan(struct shrinker *shrink, struct shrink_control *sc)
785 unsigned long freed = 0;
787 spin_lock(&dq_list_lock);
788 while (!list_empty(&free_dquots) && sc->nr_to_scan) {
789 dquot = list_first_entry(&free_dquots, struct dquot, dq_free);
790 remove_dquot_hash(dquot);
791 remove_free_dquot(dquot);
793 do_destroy_dquot(dquot);
797 spin_unlock(&dq_list_lock);
802 dqcache_shrink_count(struct shrinker *shrink, struct shrink_control *sc)
804 return vfs_pressure_ratio(
805 percpu_counter_read_positive(&dqstats.counter[DQST_FREE_DQUOTS]));
809 * Safely release dquot and put reference to dquot.
811 static void quota_release_workfn(struct work_struct *work)
814 struct list_head rls_head;
816 spin_lock(&dq_list_lock);
817 /* Exchange the list head to avoid livelock. */
818 list_replace_init(&releasing_dquots, &rls_head);
819 spin_unlock(&dq_list_lock);
820 synchronize_srcu(&dquot_srcu);
823 spin_lock(&dq_list_lock);
824 while (!list_empty(&rls_head)) {
825 dquot = list_first_entry(&rls_head, struct dquot, dq_free);
826 WARN_ON_ONCE(atomic_read(&dquot->dq_count));
828 * Note that DQ_RELEASING_B protects us from racing with
829 * invalidate_dquots() calls so we are safe to work with the
830 * dquot even after we drop dq_list_lock.
832 if (dquot_dirty(dquot)) {
833 spin_unlock(&dq_list_lock);
834 /* Commit dquot before releasing */
835 dquot_write_dquot(dquot);
838 if (dquot_active(dquot)) {
839 spin_unlock(&dq_list_lock);
840 dquot->dq_sb->dq_op->release_dquot(dquot);
843 /* Dquot is inactive and clean, now move it to free list */
844 remove_free_dquot(dquot);
845 put_dquot_last(dquot);
847 spin_unlock(&dq_list_lock);
851 * Put reference to dquot
853 void dqput(struct dquot *dquot)
857 #ifdef CONFIG_QUOTA_DEBUG
858 if (!atomic_read(&dquot->dq_count)) {
859 quota_error(dquot->dq_sb, "trying to free free dquot of %s %d",
860 quotatypes[dquot->dq_id.type],
861 from_kqid(&init_user_ns, dquot->dq_id));
865 dqstats_inc(DQST_DROPS);
867 spin_lock(&dq_list_lock);
868 if (atomic_read(&dquot->dq_count) > 1) {
869 /* We have more than one user... nothing to do */
870 atomic_dec(&dquot->dq_count);
871 /* Releasing dquot during quotaoff phase? */
872 if (!sb_has_quota_active(dquot->dq_sb, dquot->dq_id.type) &&
873 atomic_read(&dquot->dq_count) == 1)
874 wake_up(&dquot_ref_wq);
875 spin_unlock(&dq_list_lock);
879 /* Need to release dquot? */
880 #ifdef CONFIG_QUOTA_DEBUG
882 BUG_ON(!list_empty(&dquot->dq_free));
884 put_releasing_dquots(dquot);
885 atomic_dec(&dquot->dq_count);
886 spin_unlock(&dq_list_lock);
887 queue_delayed_work(system_unbound_wq, "a_release_work, 1);
889 EXPORT_SYMBOL(dqput);
891 struct dquot *dquot_alloc(struct super_block *sb, int type)
893 return kmem_cache_zalloc(dquot_cachep, GFP_NOFS);
895 EXPORT_SYMBOL(dquot_alloc);
897 static struct dquot *get_empty_dquot(struct super_block *sb, int type)
901 dquot = sb->dq_op->alloc_dquot(sb, type);
905 mutex_init(&dquot->dq_lock);
906 INIT_LIST_HEAD(&dquot->dq_free);
907 INIT_LIST_HEAD(&dquot->dq_inuse);
908 INIT_HLIST_NODE(&dquot->dq_hash);
909 INIT_LIST_HEAD(&dquot->dq_dirty);
911 dquot->dq_id = make_kqid_invalid(type);
912 atomic_set(&dquot->dq_count, 1);
913 spin_lock_init(&dquot->dq_dqb_lock);
919 * Get reference to dquot
921 * Locking is slightly tricky here. We are guarded from parallel quotaoff()
922 * destroying our dquot by:
923 * a) checking for quota flags under dq_list_lock and
924 * b) getting a reference to dquot before we release dq_list_lock
926 struct dquot *dqget(struct super_block *sb, struct kqid qid)
928 unsigned int hashent = hashfn(sb, qid);
929 struct dquot *dquot, *empty = NULL;
931 if (!qid_has_mapping(sb->s_user_ns, qid))
932 return ERR_PTR(-EINVAL);
934 if (!sb_has_quota_active(sb, qid.type))
935 return ERR_PTR(-ESRCH);
937 spin_lock(&dq_list_lock);
938 spin_lock(&dq_state_lock);
939 if (!sb_has_quota_active(sb, qid.type)) {
940 spin_unlock(&dq_state_lock);
941 spin_unlock(&dq_list_lock);
942 dquot = ERR_PTR(-ESRCH);
945 spin_unlock(&dq_state_lock);
947 dquot = find_dquot(hashent, sb, qid);
950 spin_unlock(&dq_list_lock);
951 empty = get_empty_dquot(sb, qid.type);
953 schedule(); /* Try to wait for a moment... */
959 /* all dquots go on the inuse_list */
961 /* hash it first so it can be found */
962 insert_dquot_hash(dquot);
963 spin_unlock(&dq_list_lock);
964 dqstats_inc(DQST_LOOKUPS);
966 if (!atomic_read(&dquot->dq_count))
967 remove_free_dquot(dquot);
968 atomic_inc(&dquot->dq_count);
969 spin_unlock(&dq_list_lock);
970 dqstats_inc(DQST_CACHE_HITS);
971 dqstats_inc(DQST_LOOKUPS);
973 /* Wait for dq_lock - after this we know that either dquot_release() is
974 * already finished or it will be canceled due to dq_count > 0 test */
975 wait_on_dquot(dquot);
976 /* Read the dquot / allocate space in quota file */
977 if (!dquot_active(dquot)) {
980 err = sb->dq_op->acquire_dquot(dquot);
983 dquot = ERR_PTR(err);
988 * Make sure following reads see filled structure - paired with
989 * smp_mb__before_atomic() in dquot_acquire().
992 #ifdef CONFIG_QUOTA_DEBUG
993 BUG_ON(!dquot->dq_sb); /* Has somebody invalidated entry under us? */
997 do_destroy_dquot(empty);
1001 EXPORT_SYMBOL(dqget);
1003 static inline struct dquot __rcu **i_dquot(struct inode *inode)
1005 return inode->i_sb->s_op->get_dquots(inode);
1008 static int dqinit_needed(struct inode *inode, int type)
1010 struct dquot __rcu * const *dquots;
1013 if (IS_NOQUOTA(inode))
1016 dquots = i_dquot(inode);
1018 return !dquots[type];
1019 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1025 /* This routine is guarded by s_umount semaphore */
1026 static int add_dquot_ref(struct super_block *sb, int type)
1028 struct inode *inode, *old_inode = NULL;
1029 #ifdef CONFIG_QUOTA_DEBUG
1034 spin_lock(&sb->s_inode_list_lock);
1035 list_for_each_entry(inode, &sb->s_inodes, i_sb_list) {
1036 spin_lock(&inode->i_lock);
1037 if ((inode->i_state & (I_FREEING|I_WILL_FREE|I_NEW)) ||
1038 !atomic_read(&inode->i_writecount) ||
1039 !dqinit_needed(inode, type)) {
1040 spin_unlock(&inode->i_lock);
1044 spin_unlock(&inode->i_lock);
1045 spin_unlock(&sb->s_inode_list_lock);
1047 #ifdef CONFIG_QUOTA_DEBUG
1048 if (unlikely(inode_get_rsv_space(inode) > 0))
1052 err = __dquot_initialize(inode, type);
1059 * We hold a reference to 'inode' so it couldn't have been
1060 * removed from s_inodes list while we dropped the
1061 * s_inode_list_lock. We cannot iput the inode now as we can be
1062 * holding the last reference and we cannot iput it under
1063 * s_inode_list_lock. So we keep the reference and iput it
1068 spin_lock(&sb->s_inode_list_lock);
1070 spin_unlock(&sb->s_inode_list_lock);
1073 #ifdef CONFIG_QUOTA_DEBUG
1075 quota_error(sb, "Writes happened before quota was turned on "
1076 "thus quota information is probably inconsistent. "
1077 "Please run quotacheck(8)");
1083 static void remove_dquot_ref(struct super_block *sb, int type)
1085 struct inode *inode;
1086 #ifdef CONFIG_QUOTA_DEBUG
1090 spin_lock(&sb->s_inode_list_lock);
1091 list_for_each_entry(inode, &sb->s_inodes, i_sb_list) {
1093 * We have to scan also I_NEW inodes because they can already
1094 * have quota pointer initialized. Luckily, we need to touch
1095 * only quota pointers and these have separate locking
1098 spin_lock(&dq_data_lock);
1099 if (!IS_NOQUOTA(inode)) {
1100 struct dquot __rcu **dquots = i_dquot(inode);
1101 struct dquot *dquot = srcu_dereference_check(
1102 dquots[type], &dquot_srcu,
1103 lockdep_is_held(&dq_data_lock));
1105 #ifdef CONFIG_QUOTA_DEBUG
1106 if (unlikely(inode_get_rsv_space(inode) > 0))
1109 rcu_assign_pointer(dquots[type], NULL);
1113 spin_unlock(&dq_data_lock);
1115 spin_unlock(&sb->s_inode_list_lock);
1116 #ifdef CONFIG_QUOTA_DEBUG
1118 printk(KERN_WARNING "VFS (%s): Writes happened after quota"
1119 " was disabled thus quota information is probably "
1120 "inconsistent. Please run quotacheck(8).\n", sb->s_id);
1125 /* Gather all references from inodes and drop them */
1126 static void drop_dquot_ref(struct super_block *sb, int type)
1129 remove_dquot_ref(sb, type);
1133 void dquot_free_reserved_space(struct dquot *dquot, qsize_t number)
1135 if (dquot->dq_dqb.dqb_rsvspace >= number)
1136 dquot->dq_dqb.dqb_rsvspace -= number;
1139 dquot->dq_dqb.dqb_rsvspace = 0;
1141 if (dquot->dq_dqb.dqb_curspace + dquot->dq_dqb.dqb_rsvspace <=
1142 dquot->dq_dqb.dqb_bsoftlimit)
1143 dquot->dq_dqb.dqb_btime = (time64_t) 0;
1144 clear_bit(DQ_BLKS_B, &dquot->dq_flags);
1147 static void dquot_decr_inodes(struct dquot *dquot, qsize_t number)
1149 if (sb_dqopt(dquot->dq_sb)->flags & DQUOT_NEGATIVE_USAGE ||
1150 dquot->dq_dqb.dqb_curinodes >= number)
1151 dquot->dq_dqb.dqb_curinodes -= number;
1153 dquot->dq_dqb.dqb_curinodes = 0;
1154 if (dquot->dq_dqb.dqb_curinodes <= dquot->dq_dqb.dqb_isoftlimit)
1155 dquot->dq_dqb.dqb_itime = (time64_t) 0;
1156 clear_bit(DQ_INODES_B, &dquot->dq_flags);
1159 static void dquot_decr_space(struct dquot *dquot, qsize_t number)
1161 if (sb_dqopt(dquot->dq_sb)->flags & DQUOT_NEGATIVE_USAGE ||
1162 dquot->dq_dqb.dqb_curspace >= number)
1163 dquot->dq_dqb.dqb_curspace -= number;
1165 dquot->dq_dqb.dqb_curspace = 0;
1166 if (dquot->dq_dqb.dqb_curspace + dquot->dq_dqb.dqb_rsvspace <=
1167 dquot->dq_dqb.dqb_bsoftlimit)
1168 dquot->dq_dqb.dqb_btime = (time64_t) 0;
1169 clear_bit(DQ_BLKS_B, &dquot->dq_flags);
1173 struct super_block *w_sb;
1174 struct kqid w_dq_id;
1178 static int warning_issued(struct dquot *dquot, const int warntype)
1180 int flag = (warntype == QUOTA_NL_BHARDWARN ||
1181 warntype == QUOTA_NL_BSOFTLONGWARN) ? DQ_BLKS_B :
1182 ((warntype == QUOTA_NL_IHARDWARN ||
1183 warntype == QUOTA_NL_ISOFTLONGWARN) ? DQ_INODES_B : 0);
1187 return test_and_set_bit(flag, &dquot->dq_flags);
1190 #ifdef CONFIG_PRINT_QUOTA_WARNING
1191 static int flag_print_warnings = 1;
1193 static int need_print_warning(struct dquot_warn *warn)
1195 if (!flag_print_warnings)
1198 switch (warn->w_dq_id.type) {
1200 return uid_eq(current_fsuid(), warn->w_dq_id.uid);
1202 return in_group_p(warn->w_dq_id.gid);
1209 /* Print warning to user which exceeded quota */
1210 static void print_warning(struct dquot_warn *warn)
1213 struct tty_struct *tty;
1214 int warntype = warn->w_type;
1216 if (warntype == QUOTA_NL_IHARDBELOW ||
1217 warntype == QUOTA_NL_ISOFTBELOW ||
1218 warntype == QUOTA_NL_BHARDBELOW ||
1219 warntype == QUOTA_NL_BSOFTBELOW || !need_print_warning(warn))
1222 tty = get_current_tty();
1225 tty_write_message(tty, warn->w_sb->s_id);
1226 if (warntype == QUOTA_NL_ISOFTWARN || warntype == QUOTA_NL_BSOFTWARN)
1227 tty_write_message(tty, ": warning, ");
1229 tty_write_message(tty, ": write failed, ");
1230 tty_write_message(tty, quotatypes[warn->w_dq_id.type]);
1232 case QUOTA_NL_IHARDWARN:
1233 msg = " file limit reached.\r\n";
1235 case QUOTA_NL_ISOFTLONGWARN:
1236 msg = " file quota exceeded too long.\r\n";
1238 case QUOTA_NL_ISOFTWARN:
1239 msg = " file quota exceeded.\r\n";
1241 case QUOTA_NL_BHARDWARN:
1242 msg = " block limit reached.\r\n";
1244 case QUOTA_NL_BSOFTLONGWARN:
1245 msg = " block quota exceeded too long.\r\n";
1247 case QUOTA_NL_BSOFTWARN:
1248 msg = " block quota exceeded.\r\n";
1251 tty_write_message(tty, msg);
1256 static void prepare_warning(struct dquot_warn *warn, struct dquot *dquot,
1259 if (warning_issued(dquot, warntype))
1261 warn->w_type = warntype;
1262 warn->w_sb = dquot->dq_sb;
1263 warn->w_dq_id = dquot->dq_id;
1267 * Write warnings to the console and send warning messages over netlink.
1269 * Note that this function can call into tty and networking code.
1271 static void flush_warnings(struct dquot_warn *warn)
1275 for (i = 0; i < MAXQUOTAS; i++) {
1276 if (warn[i].w_type == QUOTA_NL_NOWARN)
1278 #ifdef CONFIG_PRINT_QUOTA_WARNING
1279 print_warning(&warn[i]);
1281 quota_send_warning(warn[i].w_dq_id,
1282 warn[i].w_sb->s_dev, warn[i].w_type);
1286 static int ignore_hardlimit(struct dquot *dquot)
1288 struct mem_dqinfo *info = &sb_dqopt(dquot->dq_sb)->info[dquot->dq_id.type];
1290 return capable(CAP_SYS_RESOURCE) &&
1291 (info->dqi_format->qf_fmt_id != QFMT_VFS_OLD ||
1292 !(info->dqi_flags & DQF_ROOT_SQUASH));
1295 static int dquot_add_inodes(struct dquot *dquot, qsize_t inodes,
1296 struct dquot_warn *warn)
1301 spin_lock(&dquot->dq_dqb_lock);
1302 newinodes = dquot->dq_dqb.dqb_curinodes + inodes;
1303 if (!sb_has_quota_limits_enabled(dquot->dq_sb, dquot->dq_id.type) ||
1304 test_bit(DQ_FAKE_B, &dquot->dq_flags))
1307 if (dquot->dq_dqb.dqb_ihardlimit &&
1308 newinodes > dquot->dq_dqb.dqb_ihardlimit &&
1309 !ignore_hardlimit(dquot)) {
1310 prepare_warning(warn, dquot, QUOTA_NL_IHARDWARN);
1315 if (dquot->dq_dqb.dqb_isoftlimit &&
1316 newinodes > dquot->dq_dqb.dqb_isoftlimit &&
1317 dquot->dq_dqb.dqb_itime &&
1318 ktime_get_real_seconds() >= dquot->dq_dqb.dqb_itime &&
1319 !ignore_hardlimit(dquot)) {
1320 prepare_warning(warn, dquot, QUOTA_NL_ISOFTLONGWARN);
1325 if (dquot->dq_dqb.dqb_isoftlimit &&
1326 newinodes > dquot->dq_dqb.dqb_isoftlimit &&
1327 dquot->dq_dqb.dqb_itime == 0) {
1328 prepare_warning(warn, dquot, QUOTA_NL_ISOFTWARN);
1329 dquot->dq_dqb.dqb_itime = ktime_get_real_seconds() +
1330 sb_dqopt(dquot->dq_sb)->info[dquot->dq_id.type].dqi_igrace;
1333 dquot->dq_dqb.dqb_curinodes = newinodes;
1336 spin_unlock(&dquot->dq_dqb_lock);
1340 static int dquot_add_space(struct dquot *dquot, qsize_t space,
1341 qsize_t rsv_space, unsigned int flags,
1342 struct dquot_warn *warn)
1345 struct super_block *sb = dquot->dq_sb;
1348 spin_lock(&dquot->dq_dqb_lock);
1349 if (!sb_has_quota_limits_enabled(sb, dquot->dq_id.type) ||
1350 test_bit(DQ_FAKE_B, &dquot->dq_flags))
1353 tspace = dquot->dq_dqb.dqb_curspace + dquot->dq_dqb.dqb_rsvspace
1354 + space + rsv_space;
1356 if (dquot->dq_dqb.dqb_bhardlimit &&
1357 tspace > dquot->dq_dqb.dqb_bhardlimit &&
1358 !ignore_hardlimit(dquot)) {
1359 if (flags & DQUOT_SPACE_WARN)
1360 prepare_warning(warn, dquot, QUOTA_NL_BHARDWARN);
1365 if (dquot->dq_dqb.dqb_bsoftlimit &&
1366 tspace > dquot->dq_dqb.dqb_bsoftlimit &&
1367 dquot->dq_dqb.dqb_btime &&
1368 ktime_get_real_seconds() >= dquot->dq_dqb.dqb_btime &&
1369 !ignore_hardlimit(dquot)) {
1370 if (flags & DQUOT_SPACE_WARN)
1371 prepare_warning(warn, dquot, QUOTA_NL_BSOFTLONGWARN);
1376 if (dquot->dq_dqb.dqb_bsoftlimit &&
1377 tspace > dquot->dq_dqb.dqb_bsoftlimit &&
1378 dquot->dq_dqb.dqb_btime == 0) {
1379 if (flags & DQUOT_SPACE_WARN) {
1380 prepare_warning(warn, dquot, QUOTA_NL_BSOFTWARN);
1381 dquot->dq_dqb.dqb_btime = ktime_get_real_seconds() +
1382 sb_dqopt(sb)->info[dquot->dq_id.type].dqi_bgrace;
1385 * We don't allow preallocation to exceed softlimit so exceeding will
1394 * We have to be careful and go through warning generation & grace time
1395 * setting even if DQUOT_SPACE_NOFAIL is set. That's why we check it
1398 if (flags & DQUOT_SPACE_NOFAIL)
1401 dquot->dq_dqb.dqb_rsvspace += rsv_space;
1402 dquot->dq_dqb.dqb_curspace += space;
1404 spin_unlock(&dquot->dq_dqb_lock);
1408 static int info_idq_free(struct dquot *dquot, qsize_t inodes)
1412 if (test_bit(DQ_FAKE_B, &dquot->dq_flags) ||
1413 dquot->dq_dqb.dqb_curinodes <= dquot->dq_dqb.dqb_isoftlimit ||
1414 !sb_has_quota_limits_enabled(dquot->dq_sb, dquot->dq_id.type))
1415 return QUOTA_NL_NOWARN;
1417 newinodes = dquot->dq_dqb.dqb_curinodes - inodes;
1418 if (newinodes <= dquot->dq_dqb.dqb_isoftlimit)
1419 return QUOTA_NL_ISOFTBELOW;
1420 if (dquot->dq_dqb.dqb_curinodes >= dquot->dq_dqb.dqb_ihardlimit &&
1421 newinodes < dquot->dq_dqb.dqb_ihardlimit)
1422 return QUOTA_NL_IHARDBELOW;
1423 return QUOTA_NL_NOWARN;
1426 static int info_bdq_free(struct dquot *dquot, qsize_t space)
1430 tspace = dquot->dq_dqb.dqb_curspace + dquot->dq_dqb.dqb_rsvspace;
1432 if (test_bit(DQ_FAKE_B, &dquot->dq_flags) ||
1433 tspace <= dquot->dq_dqb.dqb_bsoftlimit)
1434 return QUOTA_NL_NOWARN;
1436 if (tspace - space <= dquot->dq_dqb.dqb_bsoftlimit)
1437 return QUOTA_NL_BSOFTBELOW;
1438 if (tspace >= dquot->dq_dqb.dqb_bhardlimit &&
1439 tspace - space < dquot->dq_dqb.dqb_bhardlimit)
1440 return QUOTA_NL_BHARDBELOW;
1441 return QUOTA_NL_NOWARN;
1444 static int inode_quota_active(const struct inode *inode)
1446 struct super_block *sb = inode->i_sb;
1448 if (IS_NOQUOTA(inode))
1450 return sb_any_quota_loaded(sb) & ~sb_any_quota_suspended(sb);
1454 * Initialize quota pointers in inode
1456 * It is better to call this function outside of any transaction as it
1457 * might need a lot of space in journal for dquot structure allocation.
1459 static int __dquot_initialize(struct inode *inode, int type)
1461 int cnt, init_needed = 0;
1462 struct dquot __rcu **dquots;
1463 struct dquot *got[MAXQUOTAS] = {};
1464 struct super_block *sb = inode->i_sb;
1468 if (!inode_quota_active(inode))
1471 dquots = i_dquot(inode);
1473 /* First get references to structures we might need. */
1474 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1478 struct dquot *dquot;
1480 if (type != -1 && cnt != type)
1483 * The i_dquot should have been initialized in most cases,
1484 * we check it without locking here to avoid unnecessary
1485 * dqget()/dqput() calls.
1490 if (!sb_has_quota_active(sb, cnt))
1497 qid = make_kqid_uid(inode->i_uid);
1500 qid = make_kqid_gid(inode->i_gid);
1503 rc = inode->i_sb->dq_op->get_projid(inode, &projid);
1506 qid = make_kqid_projid(projid);
1509 dquot = dqget(sb, qid);
1510 if (IS_ERR(dquot)) {
1511 /* We raced with somebody turning quotas off... */
1512 if (PTR_ERR(dquot) != -ESRCH) {
1513 ret = PTR_ERR(dquot);
1521 /* All required i_dquot has been initialized */
1525 spin_lock(&dq_data_lock);
1526 if (IS_NOQUOTA(inode))
1528 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1529 if (type != -1 && cnt != type)
1531 /* Avoid races with quotaoff() */
1532 if (!sb_has_quota_active(sb, cnt))
1534 /* We could race with quotaon or dqget() could have failed */
1538 rcu_assign_pointer(dquots[cnt], got[cnt]);
1541 * Make quota reservation system happy if someone
1542 * did a write before quota was turned on
1544 rsv = inode_get_rsv_space(inode);
1545 if (unlikely(rsv)) {
1546 struct dquot *dquot = srcu_dereference_check(
1547 dquots[cnt], &dquot_srcu,
1548 lockdep_is_held(&dq_data_lock));
1550 spin_lock(&inode->i_lock);
1551 /* Get reservation again under proper lock */
1552 rsv = __inode_get_rsv_space(inode);
1553 spin_lock(&dquot->dq_dqb_lock);
1554 dquot->dq_dqb.dqb_rsvspace += rsv;
1555 spin_unlock(&dquot->dq_dqb_lock);
1556 spin_unlock(&inode->i_lock);
1561 spin_unlock(&dq_data_lock);
1563 /* Drop unused references */
1569 int dquot_initialize(struct inode *inode)
1571 return __dquot_initialize(inode, -1);
1573 EXPORT_SYMBOL(dquot_initialize);
1575 bool dquot_initialize_needed(struct inode *inode)
1577 struct dquot __rcu **dquots;
1580 if (!inode_quota_active(inode))
1583 dquots = i_dquot(inode);
1584 for (i = 0; i < MAXQUOTAS; i++)
1585 if (!dquots[i] && sb_has_quota_active(inode->i_sb, i))
1589 EXPORT_SYMBOL(dquot_initialize_needed);
1592 * Release all quotas referenced by inode.
1594 * This function only be called on inode free or converting
1595 * a file to quota file, no other users for the i_dquot in
1596 * both cases, so we needn't call synchronize_srcu() after
1599 static void __dquot_drop(struct inode *inode)
1602 struct dquot __rcu **dquots = i_dquot(inode);
1603 struct dquot *put[MAXQUOTAS];
1605 spin_lock(&dq_data_lock);
1606 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1607 put[cnt] = srcu_dereference_check(dquots[cnt], &dquot_srcu,
1608 lockdep_is_held(&dq_data_lock));
1609 rcu_assign_pointer(dquots[cnt], NULL);
1611 spin_unlock(&dq_data_lock);
1615 void dquot_drop(struct inode *inode)
1617 struct dquot __rcu * const *dquots;
1620 if (IS_NOQUOTA(inode))
1624 * Test before calling to rule out calls from proc and such
1625 * where we are not allowed to block. Note that this is
1626 * actually reliable test even without the lock - the caller
1627 * must assure that nobody can come after the DQUOT_DROP and
1628 * add quota pointers back anyway.
1630 dquots = i_dquot(inode);
1631 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1636 if (cnt < MAXQUOTAS)
1637 __dquot_drop(inode);
1639 EXPORT_SYMBOL(dquot_drop);
1642 * inode_reserved_space is managed internally by quota, and protected by
1643 * i_lock similar to i_blocks+i_bytes.
1645 static qsize_t *inode_reserved_space(struct inode * inode)
1647 /* Filesystem must explicitly define it's own method in order to use
1648 * quota reservation interface */
1649 BUG_ON(!inode->i_sb->dq_op->get_reserved_space);
1650 return inode->i_sb->dq_op->get_reserved_space(inode);
1653 static qsize_t __inode_get_rsv_space(struct inode *inode)
1655 if (!inode->i_sb->dq_op->get_reserved_space)
1657 return *inode_reserved_space(inode);
1660 static qsize_t inode_get_rsv_space(struct inode *inode)
1664 if (!inode->i_sb->dq_op->get_reserved_space)
1666 spin_lock(&inode->i_lock);
1667 ret = __inode_get_rsv_space(inode);
1668 spin_unlock(&inode->i_lock);
1673 * This functions updates i_blocks+i_bytes fields and quota information
1674 * (together with appropriate checks).
1676 * NOTE: We absolutely rely on the fact that caller dirties the inode
1677 * (usually helpers in quotaops.h care about this) and holds a handle for
1678 * the current transaction so that dquot write and inode write go into the
1683 * This operation can block, but only after everything is updated
1685 int __dquot_alloc_space(struct inode *inode, qsize_t number, int flags)
1687 int cnt, ret = 0, index;
1688 struct dquot_warn warn[MAXQUOTAS];
1689 int reserve = flags & DQUOT_SPACE_RESERVE;
1690 struct dquot __rcu **dquots;
1691 struct dquot *dquot;
1693 if (!inode_quota_active(inode)) {
1695 spin_lock(&inode->i_lock);
1696 *inode_reserved_space(inode) += number;
1697 spin_unlock(&inode->i_lock);
1699 inode_add_bytes(inode, number);
1704 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1705 warn[cnt].w_type = QUOTA_NL_NOWARN;
1707 dquots = i_dquot(inode);
1708 index = srcu_read_lock(&dquot_srcu);
1709 spin_lock(&inode->i_lock);
1710 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1711 dquot = srcu_dereference(dquots[cnt], &dquot_srcu);
1715 ret = dquot_add_space(dquot, 0, number, flags, &warn[cnt]);
1717 ret = dquot_add_space(dquot, number, 0, flags, &warn[cnt]);
1720 /* Back out changes we already did */
1721 for (cnt--; cnt >= 0; cnt--) {
1722 dquot = srcu_dereference(dquots[cnt], &dquot_srcu);
1725 spin_lock(&dquot->dq_dqb_lock);
1727 dquot_free_reserved_space(dquot, number);
1729 dquot_decr_space(dquot, number);
1730 spin_unlock(&dquot->dq_dqb_lock);
1732 spin_unlock(&inode->i_lock);
1733 goto out_flush_warn;
1737 *inode_reserved_space(inode) += number;
1739 __inode_add_bytes(inode, number);
1740 spin_unlock(&inode->i_lock);
1743 goto out_flush_warn;
1744 mark_all_dquot_dirty(dquots);
1746 srcu_read_unlock(&dquot_srcu, index);
1747 flush_warnings(warn);
1751 EXPORT_SYMBOL(__dquot_alloc_space);
1754 * This operation can block, but only after everything is updated
1756 int dquot_alloc_inode(struct inode *inode)
1758 int cnt, ret = 0, index;
1759 struct dquot_warn warn[MAXQUOTAS];
1760 struct dquot __rcu * const *dquots;
1761 struct dquot *dquot;
1763 if (!inode_quota_active(inode))
1765 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1766 warn[cnt].w_type = QUOTA_NL_NOWARN;
1768 dquots = i_dquot(inode);
1769 index = srcu_read_lock(&dquot_srcu);
1770 spin_lock(&inode->i_lock);
1771 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1772 dquot = srcu_dereference(dquots[cnt], &dquot_srcu);
1775 ret = dquot_add_inodes(dquot, 1, &warn[cnt]);
1777 for (cnt--; cnt >= 0; cnt--) {
1778 dquot = srcu_dereference(dquots[cnt], &dquot_srcu);
1781 /* Back out changes we already did */
1782 spin_lock(&dquot->dq_dqb_lock);
1783 dquot_decr_inodes(dquot, 1);
1784 spin_unlock(&dquot->dq_dqb_lock);
1791 spin_unlock(&inode->i_lock);
1793 mark_all_dquot_dirty(dquots);
1794 srcu_read_unlock(&dquot_srcu, index);
1795 flush_warnings(warn);
1798 EXPORT_SYMBOL(dquot_alloc_inode);
1801 * Convert in-memory reserved quotas to real consumed quotas
1803 void dquot_claim_space_nodirty(struct inode *inode, qsize_t number)
1805 struct dquot __rcu **dquots;
1806 struct dquot *dquot;
1809 if (!inode_quota_active(inode)) {
1810 spin_lock(&inode->i_lock);
1811 *inode_reserved_space(inode) -= number;
1812 __inode_add_bytes(inode, number);
1813 spin_unlock(&inode->i_lock);
1817 dquots = i_dquot(inode);
1818 index = srcu_read_lock(&dquot_srcu);
1819 spin_lock(&inode->i_lock);
1820 /* Claim reserved quotas to allocated quotas */
1821 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1822 dquot = srcu_dereference(dquots[cnt], &dquot_srcu);
1824 spin_lock(&dquot->dq_dqb_lock);
1825 if (WARN_ON_ONCE(dquot->dq_dqb.dqb_rsvspace < number))
1826 number = dquot->dq_dqb.dqb_rsvspace;
1827 dquot->dq_dqb.dqb_curspace += number;
1828 dquot->dq_dqb.dqb_rsvspace -= number;
1829 spin_unlock(&dquot->dq_dqb_lock);
1832 /* Update inode bytes */
1833 *inode_reserved_space(inode) -= number;
1834 __inode_add_bytes(inode, number);
1835 spin_unlock(&inode->i_lock);
1836 mark_all_dquot_dirty(dquots);
1837 srcu_read_unlock(&dquot_srcu, index);
1840 EXPORT_SYMBOL(dquot_claim_space_nodirty);
1843 * Convert allocated space back to in-memory reserved quotas
1845 void dquot_reclaim_space_nodirty(struct inode *inode, qsize_t number)
1847 struct dquot __rcu **dquots;
1848 struct dquot *dquot;
1851 if (!inode_quota_active(inode)) {
1852 spin_lock(&inode->i_lock);
1853 *inode_reserved_space(inode) += number;
1854 __inode_sub_bytes(inode, number);
1855 spin_unlock(&inode->i_lock);
1859 dquots = i_dquot(inode);
1860 index = srcu_read_lock(&dquot_srcu);
1861 spin_lock(&inode->i_lock);
1862 /* Claim reserved quotas to allocated quotas */
1863 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1864 dquot = srcu_dereference(dquots[cnt], &dquot_srcu);
1866 spin_lock(&dquot->dq_dqb_lock);
1867 if (WARN_ON_ONCE(dquot->dq_dqb.dqb_curspace < number))
1868 number = dquot->dq_dqb.dqb_curspace;
1869 dquot->dq_dqb.dqb_rsvspace += number;
1870 dquot->dq_dqb.dqb_curspace -= number;
1871 spin_unlock(&dquot->dq_dqb_lock);
1874 /* Update inode bytes */
1875 *inode_reserved_space(inode) += number;
1876 __inode_sub_bytes(inode, number);
1877 spin_unlock(&inode->i_lock);
1878 mark_all_dquot_dirty(dquots);
1879 srcu_read_unlock(&dquot_srcu, index);
1882 EXPORT_SYMBOL(dquot_reclaim_space_nodirty);
1885 * This operation can block, but only after everything is updated
1887 void __dquot_free_space(struct inode *inode, qsize_t number, int flags)
1890 struct dquot_warn warn[MAXQUOTAS];
1891 struct dquot __rcu **dquots;
1892 struct dquot *dquot;
1893 int reserve = flags & DQUOT_SPACE_RESERVE, index;
1895 if (!inode_quota_active(inode)) {
1897 spin_lock(&inode->i_lock);
1898 *inode_reserved_space(inode) -= number;
1899 spin_unlock(&inode->i_lock);
1901 inode_sub_bytes(inode, number);
1906 dquots = i_dquot(inode);
1907 index = srcu_read_lock(&dquot_srcu);
1908 spin_lock(&inode->i_lock);
1909 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1912 warn[cnt].w_type = QUOTA_NL_NOWARN;
1913 dquot = srcu_dereference(dquots[cnt], &dquot_srcu);
1916 spin_lock(&dquot->dq_dqb_lock);
1917 wtype = info_bdq_free(dquot, number);
1918 if (wtype != QUOTA_NL_NOWARN)
1919 prepare_warning(&warn[cnt], dquot, wtype);
1921 dquot_free_reserved_space(dquot, number);
1923 dquot_decr_space(dquot, number);
1924 spin_unlock(&dquot->dq_dqb_lock);
1927 *inode_reserved_space(inode) -= number;
1929 __inode_sub_bytes(inode, number);
1930 spin_unlock(&inode->i_lock);
1934 mark_all_dquot_dirty(dquots);
1936 srcu_read_unlock(&dquot_srcu, index);
1937 flush_warnings(warn);
1939 EXPORT_SYMBOL(__dquot_free_space);
1942 * This operation can block, but only after everything is updated
1944 void dquot_free_inode(struct inode *inode)
1947 struct dquot_warn warn[MAXQUOTAS];
1948 struct dquot __rcu * const *dquots;
1949 struct dquot *dquot;
1952 if (!inode_quota_active(inode))
1955 dquots = i_dquot(inode);
1956 index = srcu_read_lock(&dquot_srcu);
1957 spin_lock(&inode->i_lock);
1958 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1960 warn[cnt].w_type = QUOTA_NL_NOWARN;
1961 dquot = srcu_dereference(dquots[cnt], &dquot_srcu);
1964 spin_lock(&dquot->dq_dqb_lock);
1965 wtype = info_idq_free(dquot, 1);
1966 if (wtype != QUOTA_NL_NOWARN)
1967 prepare_warning(&warn[cnt], dquot, wtype);
1968 dquot_decr_inodes(dquot, 1);
1969 spin_unlock(&dquot->dq_dqb_lock);
1971 spin_unlock(&inode->i_lock);
1972 mark_all_dquot_dirty(dquots);
1973 srcu_read_unlock(&dquot_srcu, index);
1974 flush_warnings(warn);
1976 EXPORT_SYMBOL(dquot_free_inode);
1979 * Transfer the number of inode and blocks from one diskquota to an other.
1980 * On success, dquot references in transfer_to are consumed and references
1981 * to original dquots that need to be released are placed there. On failure,
1982 * references are kept untouched.
1984 * This operation can block, but only after everything is updated
1985 * A transaction must be started when entering this function.
1987 * We are holding reference on transfer_from & transfer_to, no need to
1988 * protect them by srcu_read_lock().
1990 int __dquot_transfer(struct inode *inode, struct dquot **transfer_to)
1993 qsize_t rsv_space = 0;
1994 qsize_t inode_usage = 1;
1995 struct dquot __rcu **dquots;
1996 struct dquot *transfer_from[MAXQUOTAS] = {};
1997 int cnt, index, ret = 0;
1998 char is_valid[MAXQUOTAS] = {};
1999 struct dquot_warn warn_to[MAXQUOTAS];
2000 struct dquot_warn warn_from_inodes[MAXQUOTAS];
2001 struct dquot_warn warn_from_space[MAXQUOTAS];
2003 if (IS_NOQUOTA(inode))
2006 if (inode->i_sb->dq_op->get_inode_usage) {
2007 ret = inode->i_sb->dq_op->get_inode_usage(inode, &inode_usage);
2012 /* Initialize the arrays */
2013 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
2014 warn_to[cnt].w_type = QUOTA_NL_NOWARN;
2015 warn_from_inodes[cnt].w_type = QUOTA_NL_NOWARN;
2016 warn_from_space[cnt].w_type = QUOTA_NL_NOWARN;
2019 spin_lock(&dq_data_lock);
2020 spin_lock(&inode->i_lock);
2021 if (IS_NOQUOTA(inode)) { /* File without quota accounting? */
2022 spin_unlock(&inode->i_lock);
2023 spin_unlock(&dq_data_lock);
2026 cur_space = __inode_get_bytes(inode);
2027 rsv_space = __inode_get_rsv_space(inode);
2028 dquots = i_dquot(inode);
2030 * Build the transfer_from list, check limits, and update usage in
2031 * the target structures.
2033 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
2035 * Skip changes for same uid or gid or for turned off quota-type.
2037 if (!transfer_to[cnt])
2039 /* Avoid races with quotaoff() */
2040 if (!sb_has_quota_active(inode->i_sb, cnt))
2043 transfer_from[cnt] = srcu_dereference_check(dquots[cnt],
2044 &dquot_srcu, lockdep_is_held(&dq_data_lock));
2045 ret = dquot_add_inodes(transfer_to[cnt], inode_usage,
2049 ret = dquot_add_space(transfer_to[cnt], cur_space, rsv_space,
2050 DQUOT_SPACE_WARN, &warn_to[cnt]);
2052 spin_lock(&transfer_to[cnt]->dq_dqb_lock);
2053 dquot_decr_inodes(transfer_to[cnt], inode_usage);
2054 spin_unlock(&transfer_to[cnt]->dq_dqb_lock);
2059 /* Decrease usage for source structures and update quota pointers */
2060 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
2063 /* Due to IO error we might not have transfer_from[] structure */
2064 if (transfer_from[cnt]) {
2067 spin_lock(&transfer_from[cnt]->dq_dqb_lock);
2068 wtype = info_idq_free(transfer_from[cnt], inode_usage);
2069 if (wtype != QUOTA_NL_NOWARN)
2070 prepare_warning(&warn_from_inodes[cnt],
2071 transfer_from[cnt], wtype);
2072 wtype = info_bdq_free(transfer_from[cnt],
2073 cur_space + rsv_space);
2074 if (wtype != QUOTA_NL_NOWARN)
2075 prepare_warning(&warn_from_space[cnt],
2076 transfer_from[cnt], wtype);
2077 dquot_decr_inodes(transfer_from[cnt], inode_usage);
2078 dquot_decr_space(transfer_from[cnt], cur_space);
2079 dquot_free_reserved_space(transfer_from[cnt],
2081 spin_unlock(&transfer_from[cnt]->dq_dqb_lock);
2083 rcu_assign_pointer(dquots[cnt], transfer_to[cnt]);
2085 spin_unlock(&inode->i_lock);
2086 spin_unlock(&dq_data_lock);
2089 * These arrays are local and we hold dquot references so we don't need
2090 * the srcu protection but still take dquot_srcu to avoid warning in
2091 * mark_all_dquot_dirty().
2093 index = srcu_read_lock(&dquot_srcu);
2094 mark_all_dquot_dirty((struct dquot __rcu **)transfer_from);
2095 mark_all_dquot_dirty((struct dquot __rcu **)transfer_to);
2096 srcu_read_unlock(&dquot_srcu, index);
2098 flush_warnings(warn_to);
2099 flush_warnings(warn_from_inodes);
2100 flush_warnings(warn_from_space);
2101 /* Pass back references to put */
2102 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
2104 transfer_to[cnt] = transfer_from[cnt];
2107 /* Back out changes we already did */
2108 for (cnt--; cnt >= 0; cnt--) {
2111 spin_lock(&transfer_to[cnt]->dq_dqb_lock);
2112 dquot_decr_inodes(transfer_to[cnt], inode_usage);
2113 dquot_decr_space(transfer_to[cnt], cur_space);
2114 dquot_free_reserved_space(transfer_to[cnt], rsv_space);
2115 spin_unlock(&transfer_to[cnt]->dq_dqb_lock);
2117 spin_unlock(&inode->i_lock);
2118 spin_unlock(&dq_data_lock);
2119 flush_warnings(warn_to);
2122 EXPORT_SYMBOL(__dquot_transfer);
2124 /* Wrapper for transferring ownership of an inode for uid/gid only
2125 * Called from FSXXX_setattr()
2127 int dquot_transfer(struct mnt_idmap *idmap, struct inode *inode,
2128 struct iattr *iattr)
2130 struct dquot *transfer_to[MAXQUOTAS] = {};
2131 struct dquot *dquot;
2132 struct super_block *sb = inode->i_sb;
2135 if (!inode_quota_active(inode))
2138 if (i_uid_needs_update(idmap, iattr, inode)) {
2139 kuid_t kuid = from_vfsuid(idmap, i_user_ns(inode),
2142 dquot = dqget(sb, make_kqid_uid(kuid));
2143 if (IS_ERR(dquot)) {
2144 if (PTR_ERR(dquot) != -ESRCH) {
2145 ret = PTR_ERR(dquot);
2150 transfer_to[USRQUOTA] = dquot;
2152 if (i_gid_needs_update(idmap, iattr, inode)) {
2153 kgid_t kgid = from_vfsgid(idmap, i_user_ns(inode),
2156 dquot = dqget(sb, make_kqid_gid(kgid));
2157 if (IS_ERR(dquot)) {
2158 if (PTR_ERR(dquot) != -ESRCH) {
2159 ret = PTR_ERR(dquot);
2164 transfer_to[GRPQUOTA] = dquot;
2166 ret = __dquot_transfer(inode, transfer_to);
2168 dqput_all(transfer_to);
2171 EXPORT_SYMBOL(dquot_transfer);
2174 * Write info of quota file to disk
2176 int dquot_commit_info(struct super_block *sb, int type)
2178 struct quota_info *dqopt = sb_dqopt(sb);
2180 return dqopt->ops[type]->write_file_info(sb, type);
2182 EXPORT_SYMBOL(dquot_commit_info);
2184 int dquot_get_next_id(struct super_block *sb, struct kqid *qid)
2186 struct quota_info *dqopt = sb_dqopt(sb);
2188 if (!sb_has_quota_active(sb, qid->type))
2190 if (!dqopt->ops[qid->type]->get_next_id)
2192 return dqopt->ops[qid->type]->get_next_id(sb, qid);
2194 EXPORT_SYMBOL(dquot_get_next_id);
2197 * Definitions of diskquota operations.
2199 const struct dquot_operations dquot_operations = {
2200 .write_dquot = dquot_commit,
2201 .acquire_dquot = dquot_acquire,
2202 .release_dquot = dquot_release,
2203 .mark_dirty = dquot_mark_dquot_dirty,
2204 .write_info = dquot_commit_info,
2205 .alloc_dquot = dquot_alloc,
2206 .destroy_dquot = dquot_destroy,
2207 .get_next_id = dquot_get_next_id,
2209 EXPORT_SYMBOL(dquot_operations);
2212 * Generic helper for ->open on filesystems supporting disk quotas.
2214 int dquot_file_open(struct inode *inode, struct file *file)
2218 error = generic_file_open(inode, file);
2219 if (!error && (file->f_mode & FMODE_WRITE))
2220 error = dquot_initialize(inode);
2223 EXPORT_SYMBOL(dquot_file_open);
2225 static void vfs_cleanup_quota_inode(struct super_block *sb, int type)
2227 struct quota_info *dqopt = sb_dqopt(sb);
2228 struct inode *inode = dqopt->files[type];
2232 if (!(dqopt->flags & DQUOT_QUOTA_SYS_FILE)) {
2234 inode->i_flags &= ~S_NOQUOTA;
2235 inode_unlock(inode);
2237 dqopt->files[type] = NULL;
2242 * Turn quota off on a device. type == -1 ==> quotaoff for all types (umount)
2244 int dquot_disable(struct super_block *sb, int type, unsigned int flags)
2247 struct quota_info *dqopt = sb_dqopt(sb);
2249 /* s_umount should be held in exclusive mode */
2250 if (WARN_ON_ONCE(down_read_trylock(&sb->s_umount)))
2251 up_read(&sb->s_umount);
2253 /* Cannot turn off usage accounting without turning off limits, or
2254 * suspend quotas and simultaneously turn quotas off. */
2255 if ((flags & DQUOT_USAGE_ENABLED && !(flags & DQUOT_LIMITS_ENABLED))
2256 || (flags & DQUOT_SUSPENDED && flags & (DQUOT_LIMITS_ENABLED |
2257 DQUOT_USAGE_ENABLED)))
2261 * Skip everything if there's nothing to do. We have to do this because
2262 * sometimes we are called when fill_super() failed and calling
2263 * sync_fs() in such cases does no good.
2265 if (!sb_any_quota_loaded(sb))
2268 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
2269 if (type != -1 && cnt != type)
2271 if (!sb_has_quota_loaded(sb, cnt))
2274 if (flags & DQUOT_SUSPENDED) {
2275 spin_lock(&dq_state_lock);
2277 dquot_state_flag(DQUOT_SUSPENDED, cnt);
2278 spin_unlock(&dq_state_lock);
2280 spin_lock(&dq_state_lock);
2281 dqopt->flags &= ~dquot_state_flag(flags, cnt);
2282 /* Turning off suspended quotas? */
2283 if (!sb_has_quota_loaded(sb, cnt) &&
2284 sb_has_quota_suspended(sb, cnt)) {
2285 dqopt->flags &= ~dquot_state_flag(
2286 DQUOT_SUSPENDED, cnt);
2287 spin_unlock(&dq_state_lock);
2288 vfs_cleanup_quota_inode(sb, cnt);
2291 spin_unlock(&dq_state_lock);
2294 /* We still have to keep quota loaded? */
2295 if (sb_has_quota_loaded(sb, cnt) && !(flags & DQUOT_SUSPENDED))
2298 /* Note: these are blocking operations */
2299 drop_dquot_ref(sb, cnt);
2300 invalidate_dquots(sb, cnt);
2302 * Now all dquots should be invalidated, all writes done so we
2303 * should be only users of the info. No locks needed.
2305 if (info_dirty(&dqopt->info[cnt]))
2306 sb->dq_op->write_info(sb, cnt);
2307 if (dqopt->ops[cnt]->free_file_info)
2308 dqopt->ops[cnt]->free_file_info(sb, cnt);
2309 put_quota_format(dqopt->info[cnt].dqi_format);
2310 dqopt->info[cnt].dqi_flags = 0;
2311 dqopt->info[cnt].dqi_igrace = 0;
2312 dqopt->info[cnt].dqi_bgrace = 0;
2313 dqopt->ops[cnt] = NULL;
2316 /* Skip syncing and setting flags if quota files are hidden */
2317 if (dqopt->flags & DQUOT_QUOTA_SYS_FILE)
2320 /* Sync the superblock so that buffers with quota data are written to
2321 * disk (and so userspace sees correct data afterwards). */
2322 if (sb->s_op->sync_fs)
2323 sb->s_op->sync_fs(sb, 1);
2324 sync_blockdev(sb->s_bdev);
2325 /* Now the quota files are just ordinary files and we can set the
2326 * inode flags back. Moreover we discard the pagecache so that
2327 * userspace sees the writes we did bypassing the pagecache. We
2328 * must also discard the blockdev buffers so that we see the
2329 * changes done by userspace on the next quotaon() */
2330 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
2331 if (!sb_has_quota_loaded(sb, cnt) && dqopt->files[cnt]) {
2332 inode_lock(dqopt->files[cnt]);
2333 truncate_inode_pages(&dqopt->files[cnt]->i_data, 0);
2334 inode_unlock(dqopt->files[cnt]);
2337 invalidate_bdev(sb->s_bdev);
2339 /* We are done when suspending quotas */
2340 if (flags & DQUOT_SUSPENDED)
2343 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
2344 if (!sb_has_quota_loaded(sb, cnt))
2345 vfs_cleanup_quota_inode(sb, cnt);
2348 EXPORT_SYMBOL(dquot_disable);
2350 int dquot_quota_off(struct super_block *sb, int type)
2352 return dquot_disable(sb, type,
2353 DQUOT_USAGE_ENABLED | DQUOT_LIMITS_ENABLED);
2355 EXPORT_SYMBOL(dquot_quota_off);
2358 * Turn quotas on on a device
2361 static int vfs_setup_quota_inode(struct inode *inode, int type)
2363 struct super_block *sb = inode->i_sb;
2364 struct quota_info *dqopt = sb_dqopt(sb);
2366 if (is_bad_inode(inode))
2368 if (!S_ISREG(inode->i_mode))
2370 if (IS_RDONLY(inode))
2372 if (sb_has_quota_loaded(sb, type))
2376 * Quota files should never be encrypted. They should be thought of as
2377 * filesystem metadata, not user data. New-style internal quota files
2378 * cannot be encrypted by users anyway, but old-style external quota
2379 * files could potentially be incorrectly created in an encrypted
2380 * directory, hence this explicit check. Some reasons why encrypted
2381 * quota files don't work include: (1) some filesystems that support
2382 * encryption don't handle it in their quota_read and quota_write, and
2383 * (2) cleaning up encrypted quota files at unmount would need special
2384 * consideration, as quota files are cleaned up later than user files.
2386 if (IS_ENCRYPTED(inode))
2389 dqopt->files[type] = igrab(inode);
2390 if (!dqopt->files[type])
2392 if (!(dqopt->flags & DQUOT_QUOTA_SYS_FILE)) {
2393 /* We don't want quota and atime on quota files (deadlocks
2394 * possible) Also nobody should write to the file - we use
2395 * special IO operations which ignore the immutable bit. */
2397 inode->i_flags |= S_NOQUOTA;
2398 inode_unlock(inode);
2400 * When S_NOQUOTA is set, remove dquot references as no more
2401 * references can be added
2403 __dquot_drop(inode);
2408 int dquot_load_quota_sb(struct super_block *sb, int type, int format_id,
2411 struct quota_format_type *fmt = find_quota_format(format_id);
2412 struct quota_info *dqopt = sb_dqopt(sb);
2415 lockdep_assert_held_write(&sb->s_umount);
2417 /* Just unsuspend quotas? */
2418 BUG_ON(flags & DQUOT_SUSPENDED);
2422 if (!sb->dq_op || !sb->s_qcop ||
2423 (type == PRJQUOTA && sb->dq_op->get_projid == NULL)) {
2427 /* Filesystems outside of init_user_ns not yet supported */
2428 if (sb->s_user_ns != &init_user_ns) {
2432 /* Usage always has to be set... */
2433 if (!(flags & DQUOT_USAGE_ENABLED)) {
2437 if (sb_has_quota_loaded(sb, type)) {
2442 if (!(dqopt->flags & DQUOT_QUOTA_SYS_FILE)) {
2443 /* As we bypass the pagecache we must now flush all the
2444 * dirty data and invalidate caches so that kernel sees
2445 * changes from userspace. It is not enough to just flush
2446 * the quota file since if blocksize < pagesize, invalidation
2447 * of the cache could fail because of other unrelated dirty
2449 sync_filesystem(sb);
2450 invalidate_bdev(sb->s_bdev);
2454 if (!fmt->qf_ops->check_quota_file(sb, type))
2457 dqopt->ops[type] = fmt->qf_ops;
2458 dqopt->info[type].dqi_format = fmt;
2459 dqopt->info[type].dqi_fmt_id = format_id;
2460 INIT_LIST_HEAD(&dqopt->info[type].dqi_dirty_list);
2461 error = dqopt->ops[type]->read_file_info(sb, type);
2464 if (dqopt->flags & DQUOT_QUOTA_SYS_FILE) {
2465 spin_lock(&dq_data_lock);
2466 dqopt->info[type].dqi_flags |= DQF_SYS_FILE;
2467 spin_unlock(&dq_data_lock);
2469 spin_lock(&dq_state_lock);
2470 dqopt->flags |= dquot_state_flag(flags, type);
2471 spin_unlock(&dq_state_lock);
2473 error = add_dquot_ref(sb, type);
2475 dquot_disable(sb, type,
2476 DQUOT_USAGE_ENABLED | DQUOT_LIMITS_ENABLED);
2480 put_quota_format(fmt);
2484 EXPORT_SYMBOL(dquot_load_quota_sb);
2487 * More powerful function for turning on quotas on given quota inode allowing
2488 * setting of individual quota flags
2490 int dquot_load_quota_inode(struct inode *inode, int type, int format_id,
2495 err = vfs_setup_quota_inode(inode, type);
2498 err = dquot_load_quota_sb(inode->i_sb, type, format_id, flags);
2500 vfs_cleanup_quota_inode(inode->i_sb, type);
2503 EXPORT_SYMBOL(dquot_load_quota_inode);
2505 /* Reenable quotas on remount RW */
2506 int dquot_resume(struct super_block *sb, int type)
2508 struct quota_info *dqopt = sb_dqopt(sb);
2512 /* s_umount should be held in exclusive mode */
2513 if (WARN_ON_ONCE(down_read_trylock(&sb->s_umount)))
2514 up_read(&sb->s_umount);
2516 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
2517 if (type != -1 && cnt != type)
2519 if (!sb_has_quota_suspended(sb, cnt))
2522 spin_lock(&dq_state_lock);
2523 flags = dqopt->flags & dquot_state_flag(DQUOT_USAGE_ENABLED |
2524 DQUOT_LIMITS_ENABLED,
2526 dqopt->flags &= ~dquot_state_flag(DQUOT_STATE_FLAGS, cnt);
2527 spin_unlock(&dq_state_lock);
2529 flags = dquot_generic_flag(flags, cnt);
2530 ret = dquot_load_quota_sb(sb, cnt, dqopt->info[cnt].dqi_fmt_id,
2533 vfs_cleanup_quota_inode(sb, cnt);
2538 EXPORT_SYMBOL(dquot_resume);
2540 int dquot_quota_on(struct super_block *sb, int type, int format_id,
2541 const struct path *path)
2543 int error = security_quota_on(path->dentry);
2546 /* Quota file not on the same filesystem? */
2547 if (path->dentry->d_sb != sb)
2550 error = dquot_load_quota_inode(d_inode(path->dentry), type,
2551 format_id, DQUOT_USAGE_ENABLED |
2552 DQUOT_LIMITS_ENABLED);
2555 EXPORT_SYMBOL(dquot_quota_on);
2558 * This function is used when filesystem needs to initialize quotas
2559 * during mount time.
2561 int dquot_quota_on_mount(struct super_block *sb, char *qf_name,
2562 int format_id, int type)
2564 struct dentry *dentry;
2567 dentry = lookup_positive_unlocked(qf_name, sb->s_root, strlen(qf_name));
2569 return PTR_ERR(dentry);
2571 error = security_quota_on(dentry);
2573 error = dquot_load_quota_inode(d_inode(dentry), type, format_id,
2574 DQUOT_USAGE_ENABLED | DQUOT_LIMITS_ENABLED);
2579 EXPORT_SYMBOL(dquot_quota_on_mount);
2581 static int dquot_quota_enable(struct super_block *sb, unsigned int flags)
2585 struct quota_info *dqopt = sb_dqopt(sb);
2587 if (!(dqopt->flags & DQUOT_QUOTA_SYS_FILE))
2589 /* Accounting cannot be turned on while fs is mounted */
2590 flags &= ~(FS_QUOTA_UDQ_ACCT | FS_QUOTA_GDQ_ACCT | FS_QUOTA_PDQ_ACCT);
2593 for (type = 0; type < MAXQUOTAS; type++) {
2594 if (!(flags & qtype_enforce_flag(type)))
2596 /* Can't enforce without accounting */
2597 if (!sb_has_quota_usage_enabled(sb, type)) {
2601 if (sb_has_quota_limits_enabled(sb, type)) {
2605 spin_lock(&dq_state_lock);
2606 dqopt->flags |= dquot_state_flag(DQUOT_LIMITS_ENABLED, type);
2607 spin_unlock(&dq_state_lock);
2611 /* Backout enforcement enablement we already did */
2612 for (type--; type >= 0; type--) {
2613 if (flags & qtype_enforce_flag(type))
2614 dquot_disable(sb, type, DQUOT_LIMITS_ENABLED);
2616 /* Error code translation for better compatibility with XFS */
2622 static int dquot_quota_disable(struct super_block *sb, unsigned int flags)
2626 struct quota_info *dqopt = sb_dqopt(sb);
2628 if (!(dqopt->flags & DQUOT_QUOTA_SYS_FILE))
2631 * We don't support turning off accounting via quotactl. In principle
2632 * quota infrastructure can do this but filesystems don't expect
2633 * userspace to be able to do it.
2636 (FS_QUOTA_UDQ_ACCT | FS_QUOTA_GDQ_ACCT | FS_QUOTA_PDQ_ACCT))
2639 /* Filter out limits not enabled */
2640 for (type = 0; type < MAXQUOTAS; type++)
2641 if (!sb_has_quota_limits_enabled(sb, type))
2642 flags &= ~qtype_enforce_flag(type);
2646 for (type = 0; type < MAXQUOTAS; type++) {
2647 if (flags & qtype_enforce_flag(type)) {
2648 ret = dquot_disable(sb, type, DQUOT_LIMITS_ENABLED);
2655 /* Backout enforcement disabling we already did */
2656 for (type--; type >= 0; type--) {
2657 if (flags & qtype_enforce_flag(type)) {
2658 spin_lock(&dq_state_lock);
2660 dquot_state_flag(DQUOT_LIMITS_ENABLED, type);
2661 spin_unlock(&dq_state_lock);
2667 /* Generic routine for getting common part of quota structure */
2668 static void do_get_dqblk(struct dquot *dquot, struct qc_dqblk *di)
2670 struct mem_dqblk *dm = &dquot->dq_dqb;
2672 memset(di, 0, sizeof(*di));
2673 spin_lock(&dquot->dq_dqb_lock);
2674 di->d_spc_hardlimit = dm->dqb_bhardlimit;
2675 di->d_spc_softlimit = dm->dqb_bsoftlimit;
2676 di->d_ino_hardlimit = dm->dqb_ihardlimit;
2677 di->d_ino_softlimit = dm->dqb_isoftlimit;
2678 di->d_space = dm->dqb_curspace + dm->dqb_rsvspace;
2679 di->d_ino_count = dm->dqb_curinodes;
2680 di->d_spc_timer = dm->dqb_btime;
2681 di->d_ino_timer = dm->dqb_itime;
2682 spin_unlock(&dquot->dq_dqb_lock);
2685 int dquot_get_dqblk(struct super_block *sb, struct kqid qid,
2686 struct qc_dqblk *di)
2688 struct dquot *dquot;
2690 dquot = dqget(sb, qid);
2692 return PTR_ERR(dquot);
2693 do_get_dqblk(dquot, di);
2698 EXPORT_SYMBOL(dquot_get_dqblk);
2700 int dquot_get_next_dqblk(struct super_block *sb, struct kqid *qid,
2701 struct qc_dqblk *di)
2703 struct dquot *dquot;
2706 if (!sb->dq_op->get_next_id)
2708 err = sb->dq_op->get_next_id(sb, qid);
2711 dquot = dqget(sb, *qid);
2713 return PTR_ERR(dquot);
2714 do_get_dqblk(dquot, di);
2719 EXPORT_SYMBOL(dquot_get_next_dqblk);
2721 #define VFS_QC_MASK \
2722 (QC_SPACE | QC_SPC_SOFT | QC_SPC_HARD | \
2723 QC_INO_COUNT | QC_INO_SOFT | QC_INO_HARD | \
2724 QC_SPC_TIMER | QC_INO_TIMER)
2726 /* Generic routine for setting common part of quota structure */
2727 static int do_set_dqblk(struct dquot *dquot, struct qc_dqblk *di)
2729 struct mem_dqblk *dm = &dquot->dq_dqb;
2730 int check_blim = 0, check_ilim = 0;
2731 struct mem_dqinfo *dqi = &sb_dqopt(dquot->dq_sb)->info[dquot->dq_id.type];
2733 if (di->d_fieldmask & ~VFS_QC_MASK)
2736 if (((di->d_fieldmask & QC_SPC_SOFT) &&
2737 di->d_spc_softlimit > dqi->dqi_max_spc_limit) ||
2738 ((di->d_fieldmask & QC_SPC_HARD) &&
2739 di->d_spc_hardlimit > dqi->dqi_max_spc_limit) ||
2740 ((di->d_fieldmask & QC_INO_SOFT) &&
2741 (di->d_ino_softlimit > dqi->dqi_max_ino_limit)) ||
2742 ((di->d_fieldmask & QC_INO_HARD) &&
2743 (di->d_ino_hardlimit > dqi->dqi_max_ino_limit)))
2746 spin_lock(&dquot->dq_dqb_lock);
2747 if (di->d_fieldmask & QC_SPACE) {
2748 dm->dqb_curspace = di->d_space - dm->dqb_rsvspace;
2750 set_bit(DQ_LASTSET_B + QIF_SPACE_B, &dquot->dq_flags);
2753 if (di->d_fieldmask & QC_SPC_SOFT)
2754 dm->dqb_bsoftlimit = di->d_spc_softlimit;
2755 if (di->d_fieldmask & QC_SPC_HARD)
2756 dm->dqb_bhardlimit = di->d_spc_hardlimit;
2757 if (di->d_fieldmask & (QC_SPC_SOFT | QC_SPC_HARD)) {
2759 set_bit(DQ_LASTSET_B + QIF_BLIMITS_B, &dquot->dq_flags);
2762 if (di->d_fieldmask & QC_INO_COUNT) {
2763 dm->dqb_curinodes = di->d_ino_count;
2765 set_bit(DQ_LASTSET_B + QIF_INODES_B, &dquot->dq_flags);
2768 if (di->d_fieldmask & QC_INO_SOFT)
2769 dm->dqb_isoftlimit = di->d_ino_softlimit;
2770 if (di->d_fieldmask & QC_INO_HARD)
2771 dm->dqb_ihardlimit = di->d_ino_hardlimit;
2772 if (di->d_fieldmask & (QC_INO_SOFT | QC_INO_HARD)) {
2774 set_bit(DQ_LASTSET_B + QIF_ILIMITS_B, &dquot->dq_flags);
2777 if (di->d_fieldmask & QC_SPC_TIMER) {
2778 dm->dqb_btime = di->d_spc_timer;
2780 set_bit(DQ_LASTSET_B + QIF_BTIME_B, &dquot->dq_flags);
2783 if (di->d_fieldmask & QC_INO_TIMER) {
2784 dm->dqb_itime = di->d_ino_timer;
2786 set_bit(DQ_LASTSET_B + QIF_ITIME_B, &dquot->dq_flags);
2790 if (!dm->dqb_bsoftlimit ||
2791 dm->dqb_curspace + dm->dqb_rsvspace <= dm->dqb_bsoftlimit) {
2793 clear_bit(DQ_BLKS_B, &dquot->dq_flags);
2794 } else if (!(di->d_fieldmask & QC_SPC_TIMER))
2795 /* Set grace only if user hasn't provided his own... */
2796 dm->dqb_btime = ktime_get_real_seconds() + dqi->dqi_bgrace;
2799 if (!dm->dqb_isoftlimit ||
2800 dm->dqb_curinodes <= dm->dqb_isoftlimit) {
2802 clear_bit(DQ_INODES_B, &dquot->dq_flags);
2803 } else if (!(di->d_fieldmask & QC_INO_TIMER))
2804 /* Set grace only if user hasn't provided his own... */
2805 dm->dqb_itime = ktime_get_real_seconds() + dqi->dqi_igrace;
2807 if (dm->dqb_bhardlimit || dm->dqb_bsoftlimit || dm->dqb_ihardlimit ||
2809 clear_bit(DQ_FAKE_B, &dquot->dq_flags);
2811 set_bit(DQ_FAKE_B, &dquot->dq_flags);
2812 spin_unlock(&dquot->dq_dqb_lock);
2813 mark_dquot_dirty(dquot);
2818 int dquot_set_dqblk(struct super_block *sb, struct kqid qid,
2819 struct qc_dqblk *di)
2821 struct dquot *dquot;
2824 dquot = dqget(sb, qid);
2825 if (IS_ERR(dquot)) {
2826 rc = PTR_ERR(dquot);
2829 rc = do_set_dqblk(dquot, di);
2834 EXPORT_SYMBOL(dquot_set_dqblk);
2836 /* Generic routine for getting common part of quota file information */
2837 int dquot_get_state(struct super_block *sb, struct qc_state *state)
2839 struct mem_dqinfo *mi;
2840 struct qc_type_state *tstate;
2841 struct quota_info *dqopt = sb_dqopt(sb);
2844 memset(state, 0, sizeof(*state));
2845 for (type = 0; type < MAXQUOTAS; type++) {
2846 if (!sb_has_quota_active(sb, type))
2848 tstate = state->s_state + type;
2849 mi = sb_dqopt(sb)->info + type;
2850 tstate->flags = QCI_ACCT_ENABLED;
2851 spin_lock(&dq_data_lock);
2852 if (mi->dqi_flags & DQF_SYS_FILE)
2853 tstate->flags |= QCI_SYSFILE;
2854 if (mi->dqi_flags & DQF_ROOT_SQUASH)
2855 tstate->flags |= QCI_ROOT_SQUASH;
2856 if (sb_has_quota_limits_enabled(sb, type))
2857 tstate->flags |= QCI_LIMITS_ENFORCED;
2858 tstate->spc_timelimit = mi->dqi_bgrace;
2859 tstate->ino_timelimit = mi->dqi_igrace;
2860 if (dqopt->files[type]) {
2861 tstate->ino = dqopt->files[type]->i_ino;
2862 tstate->blocks = dqopt->files[type]->i_blocks;
2864 tstate->nextents = 1; /* We don't know... */
2865 spin_unlock(&dq_data_lock);
2869 EXPORT_SYMBOL(dquot_get_state);
2871 /* Generic routine for setting common part of quota file information */
2872 int dquot_set_dqinfo(struct super_block *sb, int type, struct qc_info *ii)
2874 struct mem_dqinfo *mi;
2876 if ((ii->i_fieldmask & QC_WARNS_MASK) ||
2877 (ii->i_fieldmask & QC_RT_SPC_TIMER))
2879 if (!sb_has_quota_active(sb, type))
2881 mi = sb_dqopt(sb)->info + type;
2882 if (ii->i_fieldmask & QC_FLAGS) {
2883 if ((ii->i_flags & QCI_ROOT_SQUASH &&
2884 mi->dqi_format->qf_fmt_id != QFMT_VFS_OLD))
2887 spin_lock(&dq_data_lock);
2888 if (ii->i_fieldmask & QC_SPC_TIMER)
2889 mi->dqi_bgrace = ii->i_spc_timelimit;
2890 if (ii->i_fieldmask & QC_INO_TIMER)
2891 mi->dqi_igrace = ii->i_ino_timelimit;
2892 if (ii->i_fieldmask & QC_FLAGS) {
2893 if (ii->i_flags & QCI_ROOT_SQUASH)
2894 mi->dqi_flags |= DQF_ROOT_SQUASH;
2896 mi->dqi_flags &= ~DQF_ROOT_SQUASH;
2898 spin_unlock(&dq_data_lock);
2899 mark_info_dirty(sb, type);
2900 /* Force write to disk */
2901 return sb->dq_op->write_info(sb, type);
2903 EXPORT_SYMBOL(dquot_set_dqinfo);
2905 const struct quotactl_ops dquot_quotactl_sysfile_ops = {
2906 .quota_enable = dquot_quota_enable,
2907 .quota_disable = dquot_quota_disable,
2908 .quota_sync = dquot_quota_sync,
2909 .get_state = dquot_get_state,
2910 .set_info = dquot_set_dqinfo,
2911 .get_dqblk = dquot_get_dqblk,
2912 .get_nextdqblk = dquot_get_next_dqblk,
2913 .set_dqblk = dquot_set_dqblk
2915 EXPORT_SYMBOL(dquot_quotactl_sysfile_ops);
2917 static int do_proc_dqstats(struct ctl_table *table, int write,
2918 void *buffer, size_t *lenp, loff_t *ppos)
2920 unsigned int type = (unsigned long *)table->data - dqstats.stat;
2921 s64 value = percpu_counter_sum(&dqstats.counter[type]);
2923 /* Filter negative values for non-monotonic counters */
2924 if (value < 0 && (type == DQST_ALLOC_DQUOTS ||
2925 type == DQST_FREE_DQUOTS))
2928 /* Update global table */
2929 dqstats.stat[type] = value;
2930 return proc_doulongvec_minmax(table, write, buffer, lenp, ppos);
2933 static struct ctl_table fs_dqstats_table[] = {
2935 .procname = "lookups",
2936 .data = &dqstats.stat[DQST_LOOKUPS],
2937 .maxlen = sizeof(unsigned long),
2939 .proc_handler = do_proc_dqstats,
2942 .procname = "drops",
2943 .data = &dqstats.stat[DQST_DROPS],
2944 .maxlen = sizeof(unsigned long),
2946 .proc_handler = do_proc_dqstats,
2949 .procname = "reads",
2950 .data = &dqstats.stat[DQST_READS],
2951 .maxlen = sizeof(unsigned long),
2953 .proc_handler = do_proc_dqstats,
2956 .procname = "writes",
2957 .data = &dqstats.stat[DQST_WRITES],
2958 .maxlen = sizeof(unsigned long),
2960 .proc_handler = do_proc_dqstats,
2963 .procname = "cache_hits",
2964 .data = &dqstats.stat[DQST_CACHE_HITS],
2965 .maxlen = sizeof(unsigned long),
2967 .proc_handler = do_proc_dqstats,
2970 .procname = "allocated_dquots",
2971 .data = &dqstats.stat[DQST_ALLOC_DQUOTS],
2972 .maxlen = sizeof(unsigned long),
2974 .proc_handler = do_proc_dqstats,
2977 .procname = "free_dquots",
2978 .data = &dqstats.stat[DQST_FREE_DQUOTS],
2979 .maxlen = sizeof(unsigned long),
2981 .proc_handler = do_proc_dqstats,
2984 .procname = "syncs",
2985 .data = &dqstats.stat[DQST_SYNCS],
2986 .maxlen = sizeof(unsigned long),
2988 .proc_handler = do_proc_dqstats,
2990 #ifdef CONFIG_PRINT_QUOTA_WARNING
2992 .procname = "warnings",
2993 .data = &flag_print_warnings,
2994 .maxlen = sizeof(int),
2996 .proc_handler = proc_dointvec,
3001 static int __init dquot_init(void)
3004 unsigned long nr_hash, order;
3005 struct shrinker *dqcache_shrinker;
3007 printk(KERN_NOTICE "VFS: Disk quotas %s\n", __DQUOT_VERSION__);
3009 register_sysctl_init("fs/quota", fs_dqstats_table);
3011 dquot_cachep = kmem_cache_create("dquot",
3012 sizeof(struct dquot), sizeof(unsigned long) * 4,
3013 (SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT|
3014 SLAB_MEM_SPREAD|SLAB_PANIC),
3018 dquot_hash = (struct hlist_head *)__get_free_pages(GFP_KERNEL, order);
3020 panic("Cannot create dquot hash table");
3022 for (i = 0; i < _DQST_DQSTAT_LAST; i++) {
3023 ret = percpu_counter_init(&dqstats.counter[i], 0, GFP_KERNEL);
3025 panic("Cannot create dquot stat counters");
3028 /* Find power-of-two hlist_heads which can fit into allocation */
3029 nr_hash = (1UL << order) * PAGE_SIZE / sizeof(struct hlist_head);
3030 dq_hash_bits = ilog2(nr_hash);
3032 nr_hash = 1UL << dq_hash_bits;
3033 dq_hash_mask = nr_hash - 1;
3034 for (i = 0; i < nr_hash; i++)
3035 INIT_HLIST_HEAD(dquot_hash + i);
3037 pr_info("VFS: Dquot-cache hash table entries: %ld (order %ld,"
3038 " %ld bytes)\n", nr_hash, order, (PAGE_SIZE << order));
3040 dqcache_shrinker = shrinker_alloc(0, "dquota-cache");
3041 if (!dqcache_shrinker)
3042 panic("Cannot allocate dquot shrinker");
3044 dqcache_shrinker->count_objects = dqcache_shrink_count;
3045 dqcache_shrinker->scan_objects = dqcache_shrink_scan;
3047 shrinker_register(dqcache_shrinker);
3051 fs_initcall(dquot_init);