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 "../internal.h" /* ugh */
83 #include <linux/uaccess.h>
86 * There are five quota SMP locks:
87 * * dq_list_lock protects all lists with quotas and quota formats.
88 * * dquot->dq_dqb_lock protects data from dq_dqb
89 * * inode->i_lock protects inode->i_blocks, i_bytes and also guards
90 * consistency of dquot->dq_dqb with inode->i_blocks, i_bytes so that
91 * dquot_transfer() can stabilize amount it transfers
92 * * dq_data_lock protects mem_dqinfo structures and modifications of dquot
93 * pointers in the inode
94 * * dq_state_lock protects modifications of quota state (on quotaon and
95 * quotaoff) and readers who care about latest values take it as well.
97 * The spinlock ordering is hence:
98 * dq_data_lock > dq_list_lock > i_lock > dquot->dq_dqb_lock,
99 * dq_list_lock > dq_state_lock
101 * Note that some things (eg. sb pointer, type, id) doesn't change during
102 * the life of the dquot structure and so needn't to be protected by a lock
104 * Operation accessing dquots via inode pointers are protected by dquot_srcu.
105 * Operation of reading pointer needs srcu_read_lock(&dquot_srcu), and
106 * synchronize_srcu(&dquot_srcu) is called after clearing pointers from
107 * inode and before dropping dquot references to avoid use of dquots after
108 * they are freed. dq_data_lock is used to serialize the pointer setting and
109 * clearing operations.
110 * Special care needs to be taken about S_NOQUOTA inode flag (marking that
111 * inode is a quota file). Functions adding pointers from inode to dquots have
112 * to check this flag under dq_data_lock and then (if S_NOQUOTA is not set) they
113 * have to do all pointer modifications before dropping dq_data_lock. This makes
114 * sure they cannot race with quotaon which first sets S_NOQUOTA flag and
115 * then drops all pointers to dquots from an inode.
117 * Each dquot has its dq_lock mutex. Dquot is locked when it is being read to
118 * memory (or space for it is being allocated) on the first dqget(), when it is
119 * being written out, and when it is being released on the last dqput(). The
120 * allocation and release operations are serialized by the dq_lock and by
121 * checking the use count in dquot_release().
123 * Lock ordering (including related VFS locks) is the following:
124 * s_umount > i_mutex > journal_lock > dquot->dq_lock > dqio_sem
127 static __cacheline_aligned_in_smp DEFINE_SPINLOCK(dq_list_lock);
128 static __cacheline_aligned_in_smp DEFINE_SPINLOCK(dq_state_lock);
129 __cacheline_aligned_in_smp DEFINE_SPINLOCK(dq_data_lock);
130 EXPORT_SYMBOL(dq_data_lock);
131 DEFINE_STATIC_SRCU(dquot_srcu);
133 static DECLARE_WAIT_QUEUE_HEAD(dquot_ref_wq);
135 void __quota_error(struct super_block *sb, const char *func,
136 const char *fmt, ...)
138 if (printk_ratelimit()) {
140 struct va_format vaf;
147 printk(KERN_ERR "Quota error (device %s): %s: %pV\n",
148 sb->s_id, func, &vaf);
153 EXPORT_SYMBOL(__quota_error);
155 #if defined(CONFIG_QUOTA_DEBUG) || defined(CONFIG_PRINT_QUOTA_WARNING)
156 static char *quotatypes[] = INITQFNAMES;
158 static struct quota_format_type *quota_formats; /* List of registered formats */
159 static struct quota_module_name module_names[] = INIT_QUOTA_MODULE_NAMES;
161 /* SLAB cache for dquot structures */
162 static struct kmem_cache *dquot_cachep;
164 int register_quota_format(struct quota_format_type *fmt)
166 spin_lock(&dq_list_lock);
167 fmt->qf_next = quota_formats;
169 spin_unlock(&dq_list_lock);
172 EXPORT_SYMBOL(register_quota_format);
174 void unregister_quota_format(struct quota_format_type *fmt)
176 struct quota_format_type **actqf;
178 spin_lock(&dq_list_lock);
179 for (actqf = "a_formats; *actqf && *actqf != fmt;
180 actqf = &(*actqf)->qf_next)
183 *actqf = (*actqf)->qf_next;
184 spin_unlock(&dq_list_lock);
186 EXPORT_SYMBOL(unregister_quota_format);
188 static struct quota_format_type *find_quota_format(int id)
190 struct quota_format_type *actqf;
192 spin_lock(&dq_list_lock);
193 for (actqf = quota_formats; actqf && actqf->qf_fmt_id != id;
194 actqf = actqf->qf_next)
196 if (!actqf || !try_module_get(actqf->qf_owner)) {
199 spin_unlock(&dq_list_lock);
201 for (qm = 0; module_names[qm].qm_fmt_id &&
202 module_names[qm].qm_fmt_id != id; qm++)
204 if (!module_names[qm].qm_fmt_id ||
205 request_module(module_names[qm].qm_mod_name))
208 spin_lock(&dq_list_lock);
209 for (actqf = quota_formats; actqf && actqf->qf_fmt_id != id;
210 actqf = actqf->qf_next)
212 if (actqf && !try_module_get(actqf->qf_owner))
215 spin_unlock(&dq_list_lock);
219 static void put_quota_format(struct quota_format_type *fmt)
221 module_put(fmt->qf_owner);
225 * Dquot List Management:
226 * The quota code uses five lists for dquot management: the inuse_list,
227 * releasing_dquots, free_dquots, dqi_dirty_list, and dquot_hash[] array.
228 * A single dquot structure may be on some of those lists, depending on
231 * All dquots are placed to the end of inuse_list when first created, and this
232 * list is used for invalidate operation, which must look at every dquot.
234 * When the last reference of a dquot will be dropped, the dquot will be
235 * added to releasing_dquots. We'd then queue work item which would call
236 * synchronize_srcu() and after that perform the final cleanup of all the
237 * dquots on the list. Both releasing_dquots and free_dquots use the
238 * dq_free list_head in the dquot struct. When a dquot is removed from
239 * releasing_dquots, a reference count is always subtracted, and if
240 * dq_count == 0 at that point, the dquot will be added to the free_dquots.
242 * Unused dquots (dq_count == 0) are added to the free_dquots list when freed,
243 * and this list is searched whenever we need an available dquot. Dquots are
244 * removed from the list as soon as they are used again, and
245 * dqstats.free_dquots gives the number of dquots on the list. When
246 * dquot is invalidated it's completely released from memory.
248 * Dirty dquots are added to the dqi_dirty_list of quota_info when mark
249 * dirtied, and this list is searched when writing dirty dquots back to
250 * quota file. Note that some filesystems do dirty dquot tracking on their
251 * own (e.g. in a journal) and thus don't use dqi_dirty_list.
253 * Dquots with a specific identity (device, type and id) are placed on
254 * one of the dquot_hash[] hash chains. The provides an efficient search
255 * mechanism to locate a specific dquot.
258 static LIST_HEAD(inuse_list);
259 static LIST_HEAD(free_dquots);
260 static LIST_HEAD(releasing_dquots);
261 static unsigned int dq_hash_bits, dq_hash_mask;
262 static struct hlist_head *dquot_hash;
264 struct dqstats dqstats;
265 EXPORT_SYMBOL(dqstats);
267 static qsize_t inode_get_rsv_space(struct inode *inode);
268 static qsize_t __inode_get_rsv_space(struct inode *inode);
269 static int __dquot_initialize(struct inode *inode, int type);
271 static void quota_release_workfn(struct work_struct *work);
272 static DECLARE_DELAYED_WORK(quota_release_work, quota_release_workfn);
274 static inline unsigned int
275 hashfn(const struct super_block *sb, struct kqid qid)
277 unsigned int id = from_kqid(&init_user_ns, qid);
281 tmp = (((unsigned long)sb>>L1_CACHE_SHIFT) ^ id) * (MAXQUOTAS - type);
282 return (tmp + (tmp >> dq_hash_bits)) & dq_hash_mask;
286 * Following list functions expect dq_list_lock to be held
288 static inline void insert_dquot_hash(struct dquot *dquot)
290 struct hlist_head *head;
291 head = dquot_hash + hashfn(dquot->dq_sb, dquot->dq_id);
292 hlist_add_head(&dquot->dq_hash, head);
295 static inline void remove_dquot_hash(struct dquot *dquot)
297 hlist_del_init(&dquot->dq_hash);
300 static struct dquot *find_dquot(unsigned int hashent, struct super_block *sb,
303 struct hlist_node *node;
306 hlist_for_each (node, dquot_hash+hashent) {
307 dquot = hlist_entry(node, struct dquot, dq_hash);
308 if (dquot->dq_sb == sb && qid_eq(dquot->dq_id, qid))
314 /* Add a dquot to the tail of the free list */
315 static inline void put_dquot_last(struct dquot *dquot)
317 list_add_tail(&dquot->dq_free, &free_dquots);
318 dqstats_inc(DQST_FREE_DQUOTS);
321 static inline void put_releasing_dquots(struct dquot *dquot)
323 list_add_tail(&dquot->dq_free, &releasing_dquots);
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 (!atomic_read(&dquot->dq_count))
332 dqstats_dec(DQST_FREE_DQUOTS);
335 static inline void put_inuse(struct dquot *dquot)
337 /* We add to the back of inuse list so we don't have to restart
338 * when traversing this list and we block */
339 list_add_tail(&dquot->dq_inuse, &inuse_list);
340 dqstats_inc(DQST_ALLOC_DQUOTS);
343 static inline void remove_inuse(struct dquot *dquot)
345 dqstats_dec(DQST_ALLOC_DQUOTS);
346 list_del(&dquot->dq_inuse);
349 * End of list functions needing dq_list_lock
352 static void wait_on_dquot(struct dquot *dquot)
354 mutex_lock(&dquot->dq_lock);
355 mutex_unlock(&dquot->dq_lock);
358 static inline int dquot_active(struct dquot *dquot)
360 return test_bit(DQ_ACTIVE_B, &dquot->dq_flags);
363 static inline int dquot_dirty(struct dquot *dquot)
365 return test_bit(DQ_MOD_B, &dquot->dq_flags);
368 static inline int mark_dquot_dirty(struct dquot *dquot)
370 return dquot->dq_sb->dq_op->mark_dirty(dquot);
373 /* Mark dquot dirty in atomic manner, and return it's old dirty flag state */
374 int dquot_mark_dquot_dirty(struct dquot *dquot)
378 if (!dquot_active(dquot))
381 if (sb_dqopt(dquot->dq_sb)->flags & DQUOT_NOLIST_DIRTY)
382 return test_and_set_bit(DQ_MOD_B, &dquot->dq_flags);
384 /* If quota is dirty already, we don't have to acquire dq_list_lock */
385 if (dquot_dirty(dquot))
388 spin_lock(&dq_list_lock);
389 if (!test_and_set_bit(DQ_MOD_B, &dquot->dq_flags)) {
390 list_add(&dquot->dq_dirty, &sb_dqopt(dquot->dq_sb)->
391 info[dquot->dq_id.type].dqi_dirty_list);
394 spin_unlock(&dq_list_lock);
397 EXPORT_SYMBOL(dquot_mark_dquot_dirty);
399 /* Dirtify all the dquots - this can block when journalling */
400 static inline int mark_all_dquot_dirty(struct dquot * const *dquot)
405 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
407 /* Even in case of error we have to continue */
408 ret = mark_dquot_dirty(dquot[cnt]);
415 static inline void dqput_all(struct dquot **dquot)
419 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
423 static inline int clear_dquot_dirty(struct dquot *dquot)
425 if (sb_dqopt(dquot->dq_sb)->flags & DQUOT_NOLIST_DIRTY)
426 return test_and_clear_bit(DQ_MOD_B, &dquot->dq_flags);
428 spin_lock(&dq_list_lock);
429 if (!test_and_clear_bit(DQ_MOD_B, &dquot->dq_flags)) {
430 spin_unlock(&dq_list_lock);
433 list_del_init(&dquot->dq_dirty);
434 spin_unlock(&dq_list_lock);
438 void mark_info_dirty(struct super_block *sb, int type)
440 spin_lock(&dq_data_lock);
441 sb_dqopt(sb)->info[type].dqi_flags |= DQF_INFO_DIRTY;
442 spin_unlock(&dq_data_lock);
444 EXPORT_SYMBOL(mark_info_dirty);
447 * Read dquot from disk and alloc space for it
450 int dquot_acquire(struct dquot *dquot)
452 int ret = 0, ret2 = 0;
453 struct quota_info *dqopt = sb_dqopt(dquot->dq_sb);
455 mutex_lock(&dquot->dq_lock);
456 if (!test_bit(DQ_READ_B, &dquot->dq_flags)) {
457 ret = dqopt->ops[dquot->dq_id.type]->read_dqblk(dquot);
461 /* Make sure flags update is visible after dquot has been filled */
462 smp_mb__before_atomic();
463 set_bit(DQ_READ_B, &dquot->dq_flags);
464 /* Instantiate dquot if needed */
465 if (!dquot_active(dquot) && !dquot->dq_off) {
466 ret = dqopt->ops[dquot->dq_id.type]->commit_dqblk(dquot);
467 /* Write the info if needed */
468 if (info_dirty(&dqopt->info[dquot->dq_id.type])) {
469 ret2 = dqopt->ops[dquot->dq_id.type]->write_file_info(
470 dquot->dq_sb, dquot->dq_id.type);
480 * Make sure flags update is visible after on-disk struct has been
481 * allocated. Paired with smp_rmb() in dqget().
483 smp_mb__before_atomic();
484 set_bit(DQ_ACTIVE_B, &dquot->dq_flags);
486 mutex_unlock(&dquot->dq_lock);
489 EXPORT_SYMBOL(dquot_acquire);
492 * Write dquot to disk
494 int dquot_commit(struct dquot *dquot)
497 struct quota_info *dqopt = sb_dqopt(dquot->dq_sb);
499 mutex_lock(&dquot->dq_lock);
500 if (!clear_dquot_dirty(dquot))
502 /* Inactive dquot can be only if there was error during read/init
503 * => we have better not writing it */
504 if (dquot_active(dquot))
505 ret = dqopt->ops[dquot->dq_id.type]->commit_dqblk(dquot);
509 mutex_unlock(&dquot->dq_lock);
512 EXPORT_SYMBOL(dquot_commit);
517 int dquot_release(struct dquot *dquot)
519 int ret = 0, ret2 = 0;
520 struct quota_info *dqopt = sb_dqopt(dquot->dq_sb);
522 mutex_lock(&dquot->dq_lock);
523 /* Check whether we are not racing with some other dqget() */
524 if (dquot_is_busy(dquot))
526 if (dqopt->ops[dquot->dq_id.type]->release_dqblk) {
527 ret = dqopt->ops[dquot->dq_id.type]->release_dqblk(dquot);
529 if (info_dirty(&dqopt->info[dquot->dq_id.type])) {
530 ret2 = dqopt->ops[dquot->dq_id.type]->write_file_info(
531 dquot->dq_sb, dquot->dq_id.type);
536 clear_bit(DQ_ACTIVE_B, &dquot->dq_flags);
538 mutex_unlock(&dquot->dq_lock);
541 EXPORT_SYMBOL(dquot_release);
543 void dquot_destroy(struct dquot *dquot)
545 kmem_cache_free(dquot_cachep, dquot);
547 EXPORT_SYMBOL(dquot_destroy);
549 static inline void do_destroy_dquot(struct dquot *dquot)
551 dquot->dq_sb->dq_op->destroy_dquot(dquot);
554 /* Invalidate all dquots on the list. Note that this function is called after
555 * quota is disabled and pointers from inodes removed so there cannot be new
556 * quota users. There can still be some users of quotas due to inodes being
557 * just deleted or pruned by prune_icache() (those are not attached to any
558 * list) or parallel quotactl call. We have to wait for such users.
560 static void invalidate_dquots(struct super_block *sb, int type)
562 struct dquot *dquot, *tmp;
565 flush_delayed_work("a_release_work);
567 spin_lock(&dq_list_lock);
568 list_for_each_entry_safe(dquot, tmp, &inuse_list, dq_inuse) {
569 if (dquot->dq_sb != sb)
571 if (dquot->dq_id.type != type)
573 /* Wait for dquot users */
574 if (atomic_read(&dquot->dq_count)) {
575 /* dquot in releasing_dquots, flush and retry */
576 if (!list_empty(&dquot->dq_free)) {
577 spin_unlock(&dq_list_lock);
581 atomic_inc(&dquot->dq_count);
582 spin_unlock(&dq_list_lock);
584 * Once dqput() wakes us up, we know it's time to free
586 * IMPORTANT: we rely on the fact that there is always
587 * at most one process waiting for dquot to free.
588 * Otherwise dq_count would be > 1 and we would never
591 wait_event(dquot_ref_wq,
592 atomic_read(&dquot->dq_count) == 1);
594 /* At this moment dquot() need not exist (it could be
595 * reclaimed by prune_dqcache(). Hence we must
600 * Quota now has no users and it has been written on last
603 remove_dquot_hash(dquot);
604 remove_free_dquot(dquot);
606 do_destroy_dquot(dquot);
608 spin_unlock(&dq_list_lock);
611 /* Call callback for every active dquot on given filesystem */
612 int dquot_scan_active(struct super_block *sb,
613 int (*fn)(struct dquot *dquot, unsigned long priv),
616 struct dquot *dquot, *old_dquot = NULL;
619 WARN_ON_ONCE(!rwsem_is_locked(&sb->s_umount));
621 spin_lock(&dq_list_lock);
622 list_for_each_entry(dquot, &inuse_list, dq_inuse) {
623 if (!dquot_active(dquot))
625 if (dquot->dq_sb != sb)
627 /* Now we have active dquot so we can just increase use count */
628 atomic_inc(&dquot->dq_count);
629 spin_unlock(&dq_list_lock);
633 * ->release_dquot() can be racing with us. Our reference
634 * protects us from new calls to it so just wait for any
635 * outstanding call and recheck the DQ_ACTIVE_B after that.
637 wait_on_dquot(dquot);
638 if (dquot_active(dquot)) {
639 ret = fn(dquot, priv);
643 spin_lock(&dq_list_lock);
644 /* We are safe to continue now because our dquot could not
645 * be moved out of the inuse list while we hold the reference */
647 spin_unlock(&dq_list_lock);
652 EXPORT_SYMBOL(dquot_scan_active);
654 static inline int dquot_write_dquot(struct dquot *dquot)
656 int ret = dquot->dq_sb->dq_op->write_dquot(dquot);
658 quota_error(dquot->dq_sb, "Can't write quota structure "
659 "(error %d). Quota may get out of sync!", ret);
660 /* Clear dirty bit anyway to avoid infinite loop. */
661 clear_dquot_dirty(dquot);
666 /* Write all dquot structures to quota files */
667 int dquot_writeback_dquots(struct super_block *sb, int type)
669 struct list_head dirty;
671 struct quota_info *dqopt = sb_dqopt(sb);
675 WARN_ON_ONCE(!rwsem_is_locked(&sb->s_umount));
677 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
678 if (type != -1 && cnt != type)
680 if (!sb_has_quota_active(sb, cnt))
682 spin_lock(&dq_list_lock);
683 /* Move list away to avoid livelock. */
684 list_replace_init(&dqopt->info[cnt].dqi_dirty_list, &dirty);
685 while (!list_empty(&dirty)) {
686 dquot = list_first_entry(&dirty, struct dquot,
689 WARN_ON(!dquot_active(dquot));
691 /* Now we have active dquot from which someone is
692 * holding reference so we can safely just increase
695 spin_unlock(&dq_list_lock);
696 err = dquot_write_dquot(dquot);
700 spin_lock(&dq_list_lock);
702 spin_unlock(&dq_list_lock);
705 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
706 if ((cnt == type || type == -1) && sb_has_quota_active(sb, cnt)
707 && info_dirty(&dqopt->info[cnt]))
708 sb->dq_op->write_info(sb, cnt);
709 dqstats_inc(DQST_SYNCS);
713 EXPORT_SYMBOL(dquot_writeback_dquots);
715 /* Write all dquot structures to disk and make them visible from userspace */
716 int dquot_quota_sync(struct super_block *sb, int type)
718 struct quota_info *dqopt = sb_dqopt(sb);
722 ret = dquot_writeback_dquots(sb, type);
725 if (dqopt->flags & DQUOT_QUOTA_SYS_FILE)
728 /* This is not very clever (and fast) but currently I don't know about
729 * any other simple way of getting quota data to disk and we must get
730 * them there for userspace to be visible... */
731 if (sb->s_op->sync_fs) {
732 ret = sb->s_op->sync_fs(sb, 1);
736 ret = sync_blockdev(sb->s_bdev);
741 * Now when everything is written we can discard the pagecache so
742 * that userspace sees the changes.
744 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
745 if (type != -1 && cnt != type)
747 if (!sb_has_quota_active(sb, cnt))
749 inode_lock(dqopt->files[cnt]);
750 truncate_inode_pages(&dqopt->files[cnt]->i_data, 0);
751 inode_unlock(dqopt->files[cnt]);
756 EXPORT_SYMBOL(dquot_quota_sync);
759 dqcache_shrink_scan(struct shrinker *shrink, struct shrink_control *sc)
762 unsigned long freed = 0;
764 spin_lock(&dq_list_lock);
765 while (!list_empty(&free_dquots) && sc->nr_to_scan) {
766 dquot = list_first_entry(&free_dquots, struct dquot, dq_free);
767 remove_dquot_hash(dquot);
768 remove_free_dquot(dquot);
770 do_destroy_dquot(dquot);
774 spin_unlock(&dq_list_lock);
779 dqcache_shrink_count(struct shrinker *shrink, struct shrink_control *sc)
781 return vfs_pressure_ratio(
782 percpu_counter_read_positive(&dqstats.counter[DQST_FREE_DQUOTS]));
785 static struct shrinker dqcache_shrinker = {
786 .count_objects = dqcache_shrink_count,
787 .scan_objects = dqcache_shrink_scan,
788 .seeks = DEFAULT_SEEKS,
792 * Safely release dquot and put reference to dquot.
794 static void quota_release_workfn(struct work_struct *work)
797 struct list_head rls_head;
799 spin_lock(&dq_list_lock);
800 /* Exchange the list head to avoid livelock. */
801 list_replace_init(&releasing_dquots, &rls_head);
802 spin_unlock(&dq_list_lock);
805 synchronize_srcu(&dquot_srcu);
806 spin_lock(&dq_list_lock);
807 while (!list_empty(&rls_head)) {
808 dquot = list_first_entry(&rls_head, struct dquot, dq_free);
809 /* Dquot got used again? */
810 if (atomic_read(&dquot->dq_count) > 1) {
811 remove_free_dquot(dquot);
812 atomic_dec(&dquot->dq_count);
815 if (dquot_dirty(dquot)) {
816 spin_unlock(&dq_list_lock);
817 /* Commit dquot before releasing */
818 dquot_write_dquot(dquot);
821 if (dquot_active(dquot)) {
822 spin_unlock(&dq_list_lock);
823 dquot->dq_sb->dq_op->release_dquot(dquot);
826 /* Dquot is inactive and clean, now move it to free list */
827 remove_free_dquot(dquot);
828 atomic_dec(&dquot->dq_count);
829 put_dquot_last(dquot);
831 spin_unlock(&dq_list_lock);
835 * Put reference to dquot
837 void dqput(struct dquot *dquot)
841 #ifdef CONFIG_QUOTA_DEBUG
842 if (!atomic_read(&dquot->dq_count)) {
843 quota_error(dquot->dq_sb, "trying to free free dquot of %s %d",
844 quotatypes[dquot->dq_id.type],
845 from_kqid(&init_user_ns, dquot->dq_id));
849 dqstats_inc(DQST_DROPS);
851 spin_lock(&dq_list_lock);
852 if (atomic_read(&dquot->dq_count) > 1) {
853 /* We have more than one user... nothing to do */
854 atomic_dec(&dquot->dq_count);
855 /* Releasing dquot during quotaoff phase? */
856 if (!sb_has_quota_active(dquot->dq_sb, dquot->dq_id.type) &&
857 atomic_read(&dquot->dq_count) == 1)
858 wake_up(&dquot_ref_wq);
859 spin_unlock(&dq_list_lock);
863 /* Need to release dquot? */
864 #ifdef CONFIG_QUOTA_DEBUG
866 BUG_ON(!list_empty(&dquot->dq_free));
868 put_releasing_dquots(dquot);
869 spin_unlock(&dq_list_lock);
870 queue_delayed_work(system_unbound_wq, "a_release_work, 1);
872 EXPORT_SYMBOL(dqput);
874 struct dquot *dquot_alloc(struct super_block *sb, int type)
876 return kmem_cache_zalloc(dquot_cachep, GFP_NOFS);
878 EXPORT_SYMBOL(dquot_alloc);
880 static struct dquot *get_empty_dquot(struct super_block *sb, int type)
884 dquot = sb->dq_op->alloc_dquot(sb, type);
888 mutex_init(&dquot->dq_lock);
889 INIT_LIST_HEAD(&dquot->dq_free);
890 INIT_LIST_HEAD(&dquot->dq_inuse);
891 INIT_HLIST_NODE(&dquot->dq_hash);
892 INIT_LIST_HEAD(&dquot->dq_dirty);
894 dquot->dq_id = make_kqid_invalid(type);
895 atomic_set(&dquot->dq_count, 1);
896 spin_lock_init(&dquot->dq_dqb_lock);
902 * Get reference to dquot
904 * Locking is slightly tricky here. We are guarded from parallel quotaoff()
905 * destroying our dquot by:
906 * a) checking for quota flags under dq_list_lock and
907 * b) getting a reference to dquot before we release dq_list_lock
909 struct dquot *dqget(struct super_block *sb, struct kqid qid)
911 unsigned int hashent = hashfn(sb, qid);
912 struct dquot *dquot, *empty = NULL;
914 if (!qid_has_mapping(sb->s_user_ns, qid))
915 return ERR_PTR(-EINVAL);
917 if (!sb_has_quota_active(sb, qid.type))
918 return ERR_PTR(-ESRCH);
920 spin_lock(&dq_list_lock);
921 spin_lock(&dq_state_lock);
922 if (!sb_has_quota_active(sb, qid.type)) {
923 spin_unlock(&dq_state_lock);
924 spin_unlock(&dq_list_lock);
925 dquot = ERR_PTR(-ESRCH);
928 spin_unlock(&dq_state_lock);
930 dquot = find_dquot(hashent, sb, qid);
933 spin_unlock(&dq_list_lock);
934 empty = get_empty_dquot(sb, qid.type);
936 schedule(); /* Try to wait for a moment... */
942 /* all dquots go on the inuse_list */
944 /* hash it first so it can be found */
945 insert_dquot_hash(dquot);
946 spin_unlock(&dq_list_lock);
947 dqstats_inc(DQST_LOOKUPS);
949 if (!atomic_read(&dquot->dq_count))
950 remove_free_dquot(dquot);
951 atomic_inc(&dquot->dq_count);
952 spin_unlock(&dq_list_lock);
953 dqstats_inc(DQST_CACHE_HITS);
954 dqstats_inc(DQST_LOOKUPS);
956 /* Wait for dq_lock - after this we know that either dquot_release() is
957 * already finished or it will be canceled due to dq_count > 1 test */
958 wait_on_dquot(dquot);
959 /* Read the dquot / allocate space in quota file */
960 if (!dquot_active(dquot)) {
963 err = sb->dq_op->acquire_dquot(dquot);
966 dquot = ERR_PTR(err);
971 * Make sure following reads see filled structure - paired with
972 * smp_mb__before_atomic() in dquot_acquire().
975 #ifdef CONFIG_QUOTA_DEBUG
976 BUG_ON(!dquot->dq_sb); /* Has somebody invalidated entry under us? */
980 do_destroy_dquot(empty);
984 EXPORT_SYMBOL(dqget);
986 static inline struct dquot **i_dquot(struct inode *inode)
988 return inode->i_sb->s_op->get_dquots(inode);
991 static int dqinit_needed(struct inode *inode, int type)
993 struct dquot * const *dquots;
996 if (IS_NOQUOTA(inode))
999 dquots = i_dquot(inode);
1001 return !dquots[type];
1002 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1008 /* This routine is guarded by s_umount semaphore */
1009 static int add_dquot_ref(struct super_block *sb, int type)
1011 struct inode *inode, *old_inode = NULL;
1012 #ifdef CONFIG_QUOTA_DEBUG
1017 spin_lock(&sb->s_inode_list_lock);
1018 list_for_each_entry(inode, &sb->s_inodes, i_sb_list) {
1019 spin_lock(&inode->i_lock);
1020 if ((inode->i_state & (I_FREEING|I_WILL_FREE|I_NEW)) ||
1021 !atomic_read(&inode->i_writecount) ||
1022 !dqinit_needed(inode, type)) {
1023 spin_unlock(&inode->i_lock);
1027 spin_unlock(&inode->i_lock);
1028 spin_unlock(&sb->s_inode_list_lock);
1030 #ifdef CONFIG_QUOTA_DEBUG
1031 if (unlikely(inode_get_rsv_space(inode) > 0))
1035 err = __dquot_initialize(inode, type);
1042 * We hold a reference to 'inode' so it couldn't have been
1043 * removed from s_inodes list while we dropped the
1044 * s_inode_list_lock. We cannot iput the inode now as we can be
1045 * holding the last reference and we cannot iput it under
1046 * s_inode_list_lock. So we keep the reference and iput it
1051 spin_lock(&sb->s_inode_list_lock);
1053 spin_unlock(&sb->s_inode_list_lock);
1056 #ifdef CONFIG_QUOTA_DEBUG
1058 quota_error(sb, "Writes happened before quota was turned on "
1059 "thus quota information is probably inconsistent. "
1060 "Please run quotacheck(8)");
1067 * Remove references to dquots from inode and add dquot to list for freeing
1068 * if we have the last reference to dquot
1070 static void remove_inode_dquot_ref(struct inode *inode, int type,
1071 struct list_head *tofree_head)
1073 struct dquot **dquots = i_dquot(inode);
1074 struct dquot *dquot = dquots[type];
1079 dquots[type] = NULL;
1080 if (list_empty(&dquot->dq_free)) {
1082 * The inode still has reference to dquot so it can't be in the
1085 spin_lock(&dq_list_lock);
1086 list_add(&dquot->dq_free, tofree_head);
1087 spin_unlock(&dq_list_lock);
1090 * Dquot is already in a list to put so we won't drop the last
1098 * Free list of dquots
1099 * Dquots are removed from inodes and no new references can be got so we are
1100 * the only ones holding reference
1102 static void put_dquot_list(struct list_head *tofree_head)
1104 struct list_head *act_head;
1105 struct dquot *dquot;
1107 act_head = tofree_head->next;
1108 while (act_head != tofree_head) {
1109 dquot = list_entry(act_head, struct dquot, dq_free);
1110 act_head = act_head->next;
1111 /* Remove dquot from the list so we won't have problems... */
1112 list_del_init(&dquot->dq_free);
1117 static void remove_dquot_ref(struct super_block *sb, int type,
1118 struct list_head *tofree_head)
1120 struct inode *inode;
1121 #ifdef CONFIG_QUOTA_DEBUG
1125 spin_lock(&sb->s_inode_list_lock);
1126 list_for_each_entry(inode, &sb->s_inodes, i_sb_list) {
1128 * We have to scan also I_NEW inodes because they can already
1129 * have quota pointer initialized. Luckily, we need to touch
1130 * only quota pointers and these have separate locking
1133 spin_lock(&dq_data_lock);
1134 if (!IS_NOQUOTA(inode)) {
1135 #ifdef CONFIG_QUOTA_DEBUG
1136 if (unlikely(inode_get_rsv_space(inode) > 0))
1139 remove_inode_dquot_ref(inode, type, tofree_head);
1141 spin_unlock(&dq_data_lock);
1143 spin_unlock(&sb->s_inode_list_lock);
1144 #ifdef CONFIG_QUOTA_DEBUG
1146 printk(KERN_WARNING "VFS (%s): Writes happened after quota"
1147 " was disabled thus quota information is probably "
1148 "inconsistent. Please run quotacheck(8).\n", sb->s_id);
1153 /* Gather all references from inodes and drop them */
1154 static void drop_dquot_ref(struct super_block *sb, int type)
1156 LIST_HEAD(tofree_head);
1159 remove_dquot_ref(sb, type, &tofree_head);
1160 synchronize_srcu(&dquot_srcu);
1161 put_dquot_list(&tofree_head);
1166 void dquot_free_reserved_space(struct dquot *dquot, qsize_t number)
1168 if (dquot->dq_dqb.dqb_rsvspace >= number)
1169 dquot->dq_dqb.dqb_rsvspace -= number;
1172 dquot->dq_dqb.dqb_rsvspace = 0;
1174 if (dquot->dq_dqb.dqb_curspace + dquot->dq_dqb.dqb_rsvspace <=
1175 dquot->dq_dqb.dqb_bsoftlimit)
1176 dquot->dq_dqb.dqb_btime = (time64_t) 0;
1177 clear_bit(DQ_BLKS_B, &dquot->dq_flags);
1180 static void dquot_decr_inodes(struct dquot *dquot, qsize_t number)
1182 if (sb_dqopt(dquot->dq_sb)->flags & DQUOT_NEGATIVE_USAGE ||
1183 dquot->dq_dqb.dqb_curinodes >= number)
1184 dquot->dq_dqb.dqb_curinodes -= number;
1186 dquot->dq_dqb.dqb_curinodes = 0;
1187 if (dquot->dq_dqb.dqb_curinodes <= dquot->dq_dqb.dqb_isoftlimit)
1188 dquot->dq_dqb.dqb_itime = (time64_t) 0;
1189 clear_bit(DQ_INODES_B, &dquot->dq_flags);
1192 static void dquot_decr_space(struct dquot *dquot, qsize_t number)
1194 if (sb_dqopt(dquot->dq_sb)->flags & DQUOT_NEGATIVE_USAGE ||
1195 dquot->dq_dqb.dqb_curspace >= number)
1196 dquot->dq_dqb.dqb_curspace -= number;
1198 dquot->dq_dqb.dqb_curspace = 0;
1199 if (dquot->dq_dqb.dqb_curspace + dquot->dq_dqb.dqb_rsvspace <=
1200 dquot->dq_dqb.dqb_bsoftlimit)
1201 dquot->dq_dqb.dqb_btime = (time64_t) 0;
1202 clear_bit(DQ_BLKS_B, &dquot->dq_flags);
1206 struct super_block *w_sb;
1207 struct kqid w_dq_id;
1211 static int warning_issued(struct dquot *dquot, const int warntype)
1213 int flag = (warntype == QUOTA_NL_BHARDWARN ||
1214 warntype == QUOTA_NL_BSOFTLONGWARN) ? DQ_BLKS_B :
1215 ((warntype == QUOTA_NL_IHARDWARN ||
1216 warntype == QUOTA_NL_ISOFTLONGWARN) ? DQ_INODES_B : 0);
1220 return test_and_set_bit(flag, &dquot->dq_flags);
1223 #ifdef CONFIG_PRINT_QUOTA_WARNING
1224 static int flag_print_warnings = 1;
1226 static int need_print_warning(struct dquot_warn *warn)
1228 if (!flag_print_warnings)
1231 switch (warn->w_dq_id.type) {
1233 return uid_eq(current_fsuid(), warn->w_dq_id.uid);
1235 return in_group_p(warn->w_dq_id.gid);
1242 /* Print warning to user which exceeded quota */
1243 static void print_warning(struct dquot_warn *warn)
1246 struct tty_struct *tty;
1247 int warntype = warn->w_type;
1249 if (warntype == QUOTA_NL_IHARDBELOW ||
1250 warntype == QUOTA_NL_ISOFTBELOW ||
1251 warntype == QUOTA_NL_BHARDBELOW ||
1252 warntype == QUOTA_NL_BSOFTBELOW || !need_print_warning(warn))
1255 tty = get_current_tty();
1258 tty_write_message(tty, warn->w_sb->s_id);
1259 if (warntype == QUOTA_NL_ISOFTWARN || warntype == QUOTA_NL_BSOFTWARN)
1260 tty_write_message(tty, ": warning, ");
1262 tty_write_message(tty, ": write failed, ");
1263 tty_write_message(tty, quotatypes[warn->w_dq_id.type]);
1265 case QUOTA_NL_IHARDWARN:
1266 msg = " file limit reached.\r\n";
1268 case QUOTA_NL_ISOFTLONGWARN:
1269 msg = " file quota exceeded too long.\r\n";
1271 case QUOTA_NL_ISOFTWARN:
1272 msg = " file quota exceeded.\r\n";
1274 case QUOTA_NL_BHARDWARN:
1275 msg = " block limit reached.\r\n";
1277 case QUOTA_NL_BSOFTLONGWARN:
1278 msg = " block quota exceeded too long.\r\n";
1280 case QUOTA_NL_BSOFTWARN:
1281 msg = " block quota exceeded.\r\n";
1284 tty_write_message(tty, msg);
1289 static void prepare_warning(struct dquot_warn *warn, struct dquot *dquot,
1292 if (warning_issued(dquot, warntype))
1294 warn->w_type = warntype;
1295 warn->w_sb = dquot->dq_sb;
1296 warn->w_dq_id = dquot->dq_id;
1300 * Write warnings to the console and send warning messages over netlink.
1302 * Note that this function can call into tty and networking code.
1304 static void flush_warnings(struct dquot_warn *warn)
1308 for (i = 0; i < MAXQUOTAS; i++) {
1309 if (warn[i].w_type == QUOTA_NL_NOWARN)
1311 #ifdef CONFIG_PRINT_QUOTA_WARNING
1312 print_warning(&warn[i]);
1314 quota_send_warning(warn[i].w_dq_id,
1315 warn[i].w_sb->s_dev, warn[i].w_type);
1319 static int ignore_hardlimit(struct dquot *dquot)
1321 struct mem_dqinfo *info = &sb_dqopt(dquot->dq_sb)->info[dquot->dq_id.type];
1323 return capable(CAP_SYS_RESOURCE) &&
1324 (info->dqi_format->qf_fmt_id != QFMT_VFS_OLD ||
1325 !(info->dqi_flags & DQF_ROOT_SQUASH));
1328 static int dquot_add_inodes(struct dquot *dquot, qsize_t inodes,
1329 struct dquot_warn *warn)
1334 spin_lock(&dquot->dq_dqb_lock);
1335 newinodes = dquot->dq_dqb.dqb_curinodes + inodes;
1336 if (!sb_has_quota_limits_enabled(dquot->dq_sb, dquot->dq_id.type) ||
1337 test_bit(DQ_FAKE_B, &dquot->dq_flags))
1340 if (dquot->dq_dqb.dqb_ihardlimit &&
1341 newinodes > dquot->dq_dqb.dqb_ihardlimit &&
1342 !ignore_hardlimit(dquot)) {
1343 prepare_warning(warn, dquot, QUOTA_NL_IHARDWARN);
1348 if (dquot->dq_dqb.dqb_isoftlimit &&
1349 newinodes > dquot->dq_dqb.dqb_isoftlimit &&
1350 dquot->dq_dqb.dqb_itime &&
1351 ktime_get_real_seconds() >= dquot->dq_dqb.dqb_itime &&
1352 !ignore_hardlimit(dquot)) {
1353 prepare_warning(warn, dquot, QUOTA_NL_ISOFTLONGWARN);
1358 if (dquot->dq_dqb.dqb_isoftlimit &&
1359 newinodes > dquot->dq_dqb.dqb_isoftlimit &&
1360 dquot->dq_dqb.dqb_itime == 0) {
1361 prepare_warning(warn, dquot, QUOTA_NL_ISOFTWARN);
1362 dquot->dq_dqb.dqb_itime = ktime_get_real_seconds() +
1363 sb_dqopt(dquot->dq_sb)->info[dquot->dq_id.type].dqi_igrace;
1366 dquot->dq_dqb.dqb_curinodes = newinodes;
1369 spin_unlock(&dquot->dq_dqb_lock);
1373 static int dquot_add_space(struct dquot *dquot, qsize_t space,
1374 qsize_t rsv_space, unsigned int flags,
1375 struct dquot_warn *warn)
1378 struct super_block *sb = dquot->dq_sb;
1381 spin_lock(&dquot->dq_dqb_lock);
1382 if (!sb_has_quota_limits_enabled(sb, dquot->dq_id.type) ||
1383 test_bit(DQ_FAKE_B, &dquot->dq_flags))
1386 tspace = dquot->dq_dqb.dqb_curspace + dquot->dq_dqb.dqb_rsvspace
1387 + space + rsv_space;
1389 if (dquot->dq_dqb.dqb_bhardlimit &&
1390 tspace > dquot->dq_dqb.dqb_bhardlimit &&
1391 !ignore_hardlimit(dquot)) {
1392 if (flags & DQUOT_SPACE_WARN)
1393 prepare_warning(warn, dquot, QUOTA_NL_BHARDWARN);
1398 if (dquot->dq_dqb.dqb_bsoftlimit &&
1399 tspace > dquot->dq_dqb.dqb_bsoftlimit &&
1400 dquot->dq_dqb.dqb_btime &&
1401 ktime_get_real_seconds() >= dquot->dq_dqb.dqb_btime &&
1402 !ignore_hardlimit(dquot)) {
1403 if (flags & DQUOT_SPACE_WARN)
1404 prepare_warning(warn, dquot, QUOTA_NL_BSOFTLONGWARN);
1409 if (dquot->dq_dqb.dqb_bsoftlimit &&
1410 tspace > dquot->dq_dqb.dqb_bsoftlimit &&
1411 dquot->dq_dqb.dqb_btime == 0) {
1412 if (flags & DQUOT_SPACE_WARN) {
1413 prepare_warning(warn, dquot, QUOTA_NL_BSOFTWARN);
1414 dquot->dq_dqb.dqb_btime = ktime_get_real_seconds() +
1415 sb_dqopt(sb)->info[dquot->dq_id.type].dqi_bgrace;
1418 * We don't allow preallocation to exceed softlimit so exceeding will
1427 * We have to be careful and go through warning generation & grace time
1428 * setting even if DQUOT_SPACE_NOFAIL is set. That's why we check it
1431 if (flags & DQUOT_SPACE_NOFAIL)
1434 dquot->dq_dqb.dqb_rsvspace += rsv_space;
1435 dquot->dq_dqb.dqb_curspace += space;
1437 spin_unlock(&dquot->dq_dqb_lock);
1441 static int info_idq_free(struct dquot *dquot, qsize_t inodes)
1445 if (test_bit(DQ_FAKE_B, &dquot->dq_flags) ||
1446 dquot->dq_dqb.dqb_curinodes <= dquot->dq_dqb.dqb_isoftlimit ||
1447 !sb_has_quota_limits_enabled(dquot->dq_sb, dquot->dq_id.type))
1448 return QUOTA_NL_NOWARN;
1450 newinodes = dquot->dq_dqb.dqb_curinodes - inodes;
1451 if (newinodes <= dquot->dq_dqb.dqb_isoftlimit)
1452 return QUOTA_NL_ISOFTBELOW;
1453 if (dquot->dq_dqb.dqb_curinodes >= dquot->dq_dqb.dqb_ihardlimit &&
1454 newinodes < dquot->dq_dqb.dqb_ihardlimit)
1455 return QUOTA_NL_IHARDBELOW;
1456 return QUOTA_NL_NOWARN;
1459 static int info_bdq_free(struct dquot *dquot, qsize_t space)
1463 tspace = dquot->dq_dqb.dqb_curspace + dquot->dq_dqb.dqb_rsvspace;
1465 if (test_bit(DQ_FAKE_B, &dquot->dq_flags) ||
1466 tspace <= dquot->dq_dqb.dqb_bsoftlimit)
1467 return QUOTA_NL_NOWARN;
1469 if (tspace - space <= dquot->dq_dqb.dqb_bsoftlimit)
1470 return QUOTA_NL_BSOFTBELOW;
1471 if (tspace >= dquot->dq_dqb.dqb_bhardlimit &&
1472 tspace - space < dquot->dq_dqb.dqb_bhardlimit)
1473 return QUOTA_NL_BHARDBELOW;
1474 return QUOTA_NL_NOWARN;
1477 static int inode_quota_active(const struct inode *inode)
1479 struct super_block *sb = inode->i_sb;
1481 if (IS_NOQUOTA(inode))
1483 return sb_any_quota_loaded(sb) & ~sb_any_quota_suspended(sb);
1487 * Initialize quota pointers in inode
1489 * It is better to call this function outside of any transaction as it
1490 * might need a lot of space in journal for dquot structure allocation.
1492 static int __dquot_initialize(struct inode *inode, int type)
1494 int cnt, init_needed = 0;
1495 struct dquot **dquots, *got[MAXQUOTAS] = {};
1496 struct super_block *sb = inode->i_sb;
1500 if (!inode_quota_active(inode))
1503 dquots = i_dquot(inode);
1505 /* First get references to structures we might need. */
1506 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1510 struct dquot *dquot;
1512 if (type != -1 && cnt != type)
1515 * The i_dquot should have been initialized in most cases,
1516 * we check it without locking here to avoid unnecessary
1517 * dqget()/dqput() calls.
1522 if (!sb_has_quota_active(sb, cnt))
1529 qid = make_kqid_uid(inode->i_uid);
1532 qid = make_kqid_gid(inode->i_gid);
1535 rc = inode->i_sb->dq_op->get_projid(inode, &projid);
1538 qid = make_kqid_projid(projid);
1541 dquot = dqget(sb, qid);
1542 if (IS_ERR(dquot)) {
1543 /* We raced with somebody turning quotas off... */
1544 if (PTR_ERR(dquot) != -ESRCH) {
1545 ret = PTR_ERR(dquot);
1553 /* All required i_dquot has been initialized */
1557 spin_lock(&dq_data_lock);
1558 if (IS_NOQUOTA(inode))
1560 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1561 if (type != -1 && cnt != type)
1563 /* Avoid races with quotaoff() */
1564 if (!sb_has_quota_active(sb, cnt))
1566 /* We could race with quotaon or dqget() could have failed */
1570 dquots[cnt] = got[cnt];
1573 * Make quota reservation system happy if someone
1574 * did a write before quota was turned on
1576 rsv = inode_get_rsv_space(inode);
1577 if (unlikely(rsv)) {
1578 spin_lock(&inode->i_lock);
1579 /* Get reservation again under proper lock */
1580 rsv = __inode_get_rsv_space(inode);
1581 spin_lock(&dquots[cnt]->dq_dqb_lock);
1582 dquots[cnt]->dq_dqb.dqb_rsvspace += rsv;
1583 spin_unlock(&dquots[cnt]->dq_dqb_lock);
1584 spin_unlock(&inode->i_lock);
1589 spin_unlock(&dq_data_lock);
1591 /* Drop unused references */
1597 int dquot_initialize(struct inode *inode)
1599 return __dquot_initialize(inode, -1);
1601 EXPORT_SYMBOL(dquot_initialize);
1603 bool dquot_initialize_needed(struct inode *inode)
1605 struct dquot **dquots;
1608 if (!inode_quota_active(inode))
1611 dquots = i_dquot(inode);
1612 for (i = 0; i < MAXQUOTAS; i++)
1613 if (!dquots[i] && sb_has_quota_active(inode->i_sb, i))
1617 EXPORT_SYMBOL(dquot_initialize_needed);
1620 * Release all quotas referenced by inode.
1622 * This function only be called on inode free or converting
1623 * a file to quota file, no other users for the i_dquot in
1624 * both cases, so we needn't call synchronize_srcu() after
1627 static void __dquot_drop(struct inode *inode)
1630 struct dquot **dquots = i_dquot(inode);
1631 struct dquot *put[MAXQUOTAS];
1633 spin_lock(&dq_data_lock);
1634 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1635 put[cnt] = dquots[cnt];
1638 spin_unlock(&dq_data_lock);
1642 void dquot_drop(struct inode *inode)
1644 struct dquot * const *dquots;
1647 if (IS_NOQUOTA(inode))
1651 * Test before calling to rule out calls from proc and such
1652 * where we are not allowed to block. Note that this is
1653 * actually reliable test even without the lock - the caller
1654 * must assure that nobody can come after the DQUOT_DROP and
1655 * add quota pointers back anyway.
1657 dquots = i_dquot(inode);
1658 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1663 if (cnt < MAXQUOTAS)
1664 __dquot_drop(inode);
1666 EXPORT_SYMBOL(dquot_drop);
1669 * inode_reserved_space is managed internally by quota, and protected by
1670 * i_lock similar to i_blocks+i_bytes.
1672 static qsize_t *inode_reserved_space(struct inode * inode)
1674 /* Filesystem must explicitly define it's own method in order to use
1675 * quota reservation interface */
1676 BUG_ON(!inode->i_sb->dq_op->get_reserved_space);
1677 return inode->i_sb->dq_op->get_reserved_space(inode);
1680 static qsize_t __inode_get_rsv_space(struct inode *inode)
1682 if (!inode->i_sb->dq_op->get_reserved_space)
1684 return *inode_reserved_space(inode);
1687 static qsize_t inode_get_rsv_space(struct inode *inode)
1691 if (!inode->i_sb->dq_op->get_reserved_space)
1693 spin_lock(&inode->i_lock);
1694 ret = __inode_get_rsv_space(inode);
1695 spin_unlock(&inode->i_lock);
1700 * This functions updates i_blocks+i_bytes fields and quota information
1701 * (together with appropriate checks).
1703 * NOTE: We absolutely rely on the fact that caller dirties the inode
1704 * (usually helpers in quotaops.h care about this) and holds a handle for
1705 * the current transaction so that dquot write and inode write go into the
1710 * This operation can block, but only after everything is updated
1712 int __dquot_alloc_space(struct inode *inode, qsize_t number, int flags)
1714 int cnt, ret = 0, index;
1715 struct dquot_warn warn[MAXQUOTAS];
1716 int reserve = flags & DQUOT_SPACE_RESERVE;
1717 struct dquot **dquots;
1719 if (!inode_quota_active(inode)) {
1721 spin_lock(&inode->i_lock);
1722 *inode_reserved_space(inode) += number;
1723 spin_unlock(&inode->i_lock);
1725 inode_add_bytes(inode, number);
1730 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1731 warn[cnt].w_type = QUOTA_NL_NOWARN;
1733 dquots = i_dquot(inode);
1734 index = srcu_read_lock(&dquot_srcu);
1735 spin_lock(&inode->i_lock);
1736 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1740 ret = dquot_add_space(dquots[cnt], 0, number, flags,
1743 ret = dquot_add_space(dquots[cnt], number, 0, flags,
1747 /* Back out changes we already did */
1748 for (cnt--; cnt >= 0; cnt--) {
1751 spin_lock(&dquots[cnt]->dq_dqb_lock);
1753 dquot_free_reserved_space(dquots[cnt],
1756 dquot_decr_space(dquots[cnt], number);
1757 spin_unlock(&dquots[cnt]->dq_dqb_lock);
1759 spin_unlock(&inode->i_lock);
1760 goto out_flush_warn;
1764 *inode_reserved_space(inode) += number;
1766 __inode_add_bytes(inode, number);
1767 spin_unlock(&inode->i_lock);
1770 goto out_flush_warn;
1771 mark_all_dquot_dirty(dquots);
1773 srcu_read_unlock(&dquot_srcu, index);
1774 flush_warnings(warn);
1778 EXPORT_SYMBOL(__dquot_alloc_space);
1781 * This operation can block, but only after everything is updated
1783 int dquot_alloc_inode(struct inode *inode)
1785 int cnt, ret = 0, index;
1786 struct dquot_warn warn[MAXQUOTAS];
1787 struct dquot * const *dquots;
1789 if (!inode_quota_active(inode))
1791 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1792 warn[cnt].w_type = QUOTA_NL_NOWARN;
1794 dquots = i_dquot(inode);
1795 index = srcu_read_lock(&dquot_srcu);
1796 spin_lock(&inode->i_lock);
1797 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1800 ret = dquot_add_inodes(dquots[cnt], 1, &warn[cnt]);
1802 for (cnt--; cnt >= 0; cnt--) {
1805 /* Back out changes we already did */
1806 spin_lock(&dquots[cnt]->dq_dqb_lock);
1807 dquot_decr_inodes(dquots[cnt], 1);
1808 spin_unlock(&dquots[cnt]->dq_dqb_lock);
1815 spin_unlock(&inode->i_lock);
1817 mark_all_dquot_dirty(dquots);
1818 srcu_read_unlock(&dquot_srcu, index);
1819 flush_warnings(warn);
1822 EXPORT_SYMBOL(dquot_alloc_inode);
1825 * Convert in-memory reserved quotas to real consumed quotas
1827 int dquot_claim_space_nodirty(struct inode *inode, qsize_t number)
1829 struct dquot **dquots;
1832 if (!inode_quota_active(inode)) {
1833 spin_lock(&inode->i_lock);
1834 *inode_reserved_space(inode) -= number;
1835 __inode_add_bytes(inode, number);
1836 spin_unlock(&inode->i_lock);
1840 dquots = i_dquot(inode);
1841 index = srcu_read_lock(&dquot_srcu);
1842 spin_lock(&inode->i_lock);
1843 /* Claim reserved quotas to allocated quotas */
1844 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1846 struct dquot *dquot = dquots[cnt];
1848 spin_lock(&dquot->dq_dqb_lock);
1849 if (WARN_ON_ONCE(dquot->dq_dqb.dqb_rsvspace < number))
1850 number = dquot->dq_dqb.dqb_rsvspace;
1851 dquot->dq_dqb.dqb_curspace += number;
1852 dquot->dq_dqb.dqb_rsvspace -= number;
1853 spin_unlock(&dquot->dq_dqb_lock);
1856 /* Update inode bytes */
1857 *inode_reserved_space(inode) -= number;
1858 __inode_add_bytes(inode, number);
1859 spin_unlock(&inode->i_lock);
1860 mark_all_dquot_dirty(dquots);
1861 srcu_read_unlock(&dquot_srcu, index);
1864 EXPORT_SYMBOL(dquot_claim_space_nodirty);
1867 * Convert allocated space back to in-memory reserved quotas
1869 void dquot_reclaim_space_nodirty(struct inode *inode, qsize_t number)
1871 struct dquot **dquots;
1874 if (!inode_quota_active(inode)) {
1875 spin_lock(&inode->i_lock);
1876 *inode_reserved_space(inode) += number;
1877 __inode_sub_bytes(inode, number);
1878 spin_unlock(&inode->i_lock);
1882 dquots = i_dquot(inode);
1883 index = srcu_read_lock(&dquot_srcu);
1884 spin_lock(&inode->i_lock);
1885 /* Claim reserved quotas to allocated quotas */
1886 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1888 struct dquot *dquot = dquots[cnt];
1890 spin_lock(&dquot->dq_dqb_lock);
1891 if (WARN_ON_ONCE(dquot->dq_dqb.dqb_curspace < number))
1892 number = dquot->dq_dqb.dqb_curspace;
1893 dquot->dq_dqb.dqb_rsvspace += number;
1894 dquot->dq_dqb.dqb_curspace -= number;
1895 spin_unlock(&dquot->dq_dqb_lock);
1898 /* Update inode bytes */
1899 *inode_reserved_space(inode) += number;
1900 __inode_sub_bytes(inode, number);
1901 spin_unlock(&inode->i_lock);
1902 mark_all_dquot_dirty(dquots);
1903 srcu_read_unlock(&dquot_srcu, index);
1906 EXPORT_SYMBOL(dquot_reclaim_space_nodirty);
1909 * This operation can block, but only after everything is updated
1911 void __dquot_free_space(struct inode *inode, qsize_t number, int flags)
1914 struct dquot_warn warn[MAXQUOTAS];
1915 struct dquot **dquots;
1916 int reserve = flags & DQUOT_SPACE_RESERVE, index;
1918 if (!inode_quota_active(inode)) {
1920 spin_lock(&inode->i_lock);
1921 *inode_reserved_space(inode) -= number;
1922 spin_unlock(&inode->i_lock);
1924 inode_sub_bytes(inode, number);
1929 dquots = i_dquot(inode);
1930 index = srcu_read_lock(&dquot_srcu);
1931 spin_lock(&inode->i_lock);
1932 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1935 warn[cnt].w_type = QUOTA_NL_NOWARN;
1938 spin_lock(&dquots[cnt]->dq_dqb_lock);
1939 wtype = info_bdq_free(dquots[cnt], number);
1940 if (wtype != QUOTA_NL_NOWARN)
1941 prepare_warning(&warn[cnt], dquots[cnt], wtype);
1943 dquot_free_reserved_space(dquots[cnt], number);
1945 dquot_decr_space(dquots[cnt], number);
1946 spin_unlock(&dquots[cnt]->dq_dqb_lock);
1949 *inode_reserved_space(inode) -= number;
1951 __inode_sub_bytes(inode, number);
1952 spin_unlock(&inode->i_lock);
1956 mark_all_dquot_dirty(dquots);
1958 srcu_read_unlock(&dquot_srcu, index);
1959 flush_warnings(warn);
1961 EXPORT_SYMBOL(__dquot_free_space);
1964 * This operation can block, but only after everything is updated
1966 void dquot_free_inode(struct inode *inode)
1969 struct dquot_warn warn[MAXQUOTAS];
1970 struct dquot * const *dquots;
1973 if (!inode_quota_active(inode))
1976 dquots = i_dquot(inode);
1977 index = srcu_read_lock(&dquot_srcu);
1978 spin_lock(&inode->i_lock);
1979 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1982 warn[cnt].w_type = QUOTA_NL_NOWARN;
1985 spin_lock(&dquots[cnt]->dq_dqb_lock);
1986 wtype = info_idq_free(dquots[cnt], 1);
1987 if (wtype != QUOTA_NL_NOWARN)
1988 prepare_warning(&warn[cnt], dquots[cnt], wtype);
1989 dquot_decr_inodes(dquots[cnt], 1);
1990 spin_unlock(&dquots[cnt]->dq_dqb_lock);
1992 spin_unlock(&inode->i_lock);
1993 mark_all_dquot_dirty(dquots);
1994 srcu_read_unlock(&dquot_srcu, index);
1995 flush_warnings(warn);
1997 EXPORT_SYMBOL(dquot_free_inode);
2000 * Transfer the number of inode and blocks from one diskquota to an other.
2001 * On success, dquot references in transfer_to are consumed and references
2002 * to original dquots that need to be released are placed there. On failure,
2003 * references are kept untouched.
2005 * This operation can block, but only after everything is updated
2006 * A transaction must be started when entering this function.
2008 * We are holding reference on transfer_from & transfer_to, no need to
2009 * protect them by srcu_read_lock().
2011 int __dquot_transfer(struct inode *inode, struct dquot **transfer_to)
2014 qsize_t rsv_space = 0;
2015 qsize_t inode_usage = 1;
2016 struct dquot *transfer_from[MAXQUOTAS] = {};
2018 char is_valid[MAXQUOTAS] = {};
2019 struct dquot_warn warn_to[MAXQUOTAS];
2020 struct dquot_warn warn_from_inodes[MAXQUOTAS];
2021 struct dquot_warn warn_from_space[MAXQUOTAS];
2023 if (IS_NOQUOTA(inode))
2026 if (inode->i_sb->dq_op->get_inode_usage) {
2027 ret = inode->i_sb->dq_op->get_inode_usage(inode, &inode_usage);
2032 /* Initialize the arrays */
2033 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
2034 warn_to[cnt].w_type = QUOTA_NL_NOWARN;
2035 warn_from_inodes[cnt].w_type = QUOTA_NL_NOWARN;
2036 warn_from_space[cnt].w_type = QUOTA_NL_NOWARN;
2039 spin_lock(&dq_data_lock);
2040 spin_lock(&inode->i_lock);
2041 if (IS_NOQUOTA(inode)) { /* File without quota accounting? */
2042 spin_unlock(&inode->i_lock);
2043 spin_unlock(&dq_data_lock);
2046 cur_space = __inode_get_bytes(inode);
2047 rsv_space = __inode_get_rsv_space(inode);
2049 * Build the transfer_from list, check limits, and update usage in
2050 * the target structures.
2052 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
2054 * Skip changes for same uid or gid or for turned off quota-type.
2056 if (!transfer_to[cnt])
2058 /* Avoid races with quotaoff() */
2059 if (!sb_has_quota_active(inode->i_sb, cnt))
2062 transfer_from[cnt] = i_dquot(inode)[cnt];
2063 ret = dquot_add_inodes(transfer_to[cnt], inode_usage,
2067 ret = dquot_add_space(transfer_to[cnt], cur_space, rsv_space,
2068 DQUOT_SPACE_WARN, &warn_to[cnt]);
2070 spin_lock(&transfer_to[cnt]->dq_dqb_lock);
2071 dquot_decr_inodes(transfer_to[cnt], inode_usage);
2072 spin_unlock(&transfer_to[cnt]->dq_dqb_lock);
2077 /* Decrease usage for source structures and update quota pointers */
2078 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
2081 /* Due to IO error we might not have transfer_from[] structure */
2082 if (transfer_from[cnt]) {
2085 spin_lock(&transfer_from[cnt]->dq_dqb_lock);
2086 wtype = info_idq_free(transfer_from[cnt], inode_usage);
2087 if (wtype != QUOTA_NL_NOWARN)
2088 prepare_warning(&warn_from_inodes[cnt],
2089 transfer_from[cnt], wtype);
2090 wtype = info_bdq_free(transfer_from[cnt],
2091 cur_space + rsv_space);
2092 if (wtype != QUOTA_NL_NOWARN)
2093 prepare_warning(&warn_from_space[cnt],
2094 transfer_from[cnt], wtype);
2095 dquot_decr_inodes(transfer_from[cnt], inode_usage);
2096 dquot_decr_space(transfer_from[cnt], cur_space);
2097 dquot_free_reserved_space(transfer_from[cnt],
2099 spin_unlock(&transfer_from[cnt]->dq_dqb_lock);
2101 i_dquot(inode)[cnt] = transfer_to[cnt];
2103 spin_unlock(&inode->i_lock);
2104 spin_unlock(&dq_data_lock);
2106 mark_all_dquot_dirty(transfer_from);
2107 mark_all_dquot_dirty(transfer_to);
2108 flush_warnings(warn_to);
2109 flush_warnings(warn_from_inodes);
2110 flush_warnings(warn_from_space);
2111 /* Pass back references to put */
2112 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
2114 transfer_to[cnt] = transfer_from[cnt];
2117 /* Back out changes we already did */
2118 for (cnt--; cnt >= 0; cnt--) {
2121 spin_lock(&transfer_to[cnt]->dq_dqb_lock);
2122 dquot_decr_inodes(transfer_to[cnt], inode_usage);
2123 dquot_decr_space(transfer_to[cnt], cur_space);
2124 dquot_free_reserved_space(transfer_to[cnt], rsv_space);
2125 spin_unlock(&transfer_to[cnt]->dq_dqb_lock);
2127 spin_unlock(&inode->i_lock);
2128 spin_unlock(&dq_data_lock);
2129 flush_warnings(warn_to);
2132 EXPORT_SYMBOL(__dquot_transfer);
2134 /* Wrapper for transferring ownership of an inode for uid/gid only
2135 * Called from FSXXX_setattr()
2137 int dquot_transfer(struct inode *inode, struct iattr *iattr)
2139 struct dquot *transfer_to[MAXQUOTAS] = {};
2140 struct dquot *dquot;
2141 struct super_block *sb = inode->i_sb;
2144 if (!inode_quota_active(inode))
2147 if (iattr->ia_valid & ATTR_UID && !uid_eq(iattr->ia_uid, inode->i_uid)){
2148 dquot = dqget(sb, make_kqid_uid(iattr->ia_uid));
2149 if (IS_ERR(dquot)) {
2150 if (PTR_ERR(dquot) != -ESRCH) {
2151 ret = PTR_ERR(dquot);
2156 transfer_to[USRQUOTA] = dquot;
2158 if (iattr->ia_valid & ATTR_GID && !gid_eq(iattr->ia_gid, inode->i_gid)){
2159 dquot = dqget(sb, make_kqid_gid(iattr->ia_gid));
2160 if (IS_ERR(dquot)) {
2161 if (PTR_ERR(dquot) != -ESRCH) {
2162 ret = PTR_ERR(dquot);
2167 transfer_to[GRPQUOTA] = dquot;
2169 ret = __dquot_transfer(inode, transfer_to);
2171 dqput_all(transfer_to);
2174 EXPORT_SYMBOL(dquot_transfer);
2177 * Write info of quota file to disk
2179 int dquot_commit_info(struct super_block *sb, int type)
2181 struct quota_info *dqopt = sb_dqopt(sb);
2183 return dqopt->ops[type]->write_file_info(sb, type);
2185 EXPORT_SYMBOL(dquot_commit_info);
2187 int dquot_get_next_id(struct super_block *sb, struct kqid *qid)
2189 struct quota_info *dqopt = sb_dqopt(sb);
2191 if (!sb_has_quota_active(sb, qid->type))
2193 if (!dqopt->ops[qid->type]->get_next_id)
2195 return dqopt->ops[qid->type]->get_next_id(sb, qid);
2197 EXPORT_SYMBOL(dquot_get_next_id);
2200 * Definitions of diskquota operations.
2202 const struct dquot_operations dquot_operations = {
2203 .write_dquot = dquot_commit,
2204 .acquire_dquot = dquot_acquire,
2205 .release_dquot = dquot_release,
2206 .mark_dirty = dquot_mark_dquot_dirty,
2207 .write_info = dquot_commit_info,
2208 .alloc_dquot = dquot_alloc,
2209 .destroy_dquot = dquot_destroy,
2210 .get_next_id = dquot_get_next_id,
2212 EXPORT_SYMBOL(dquot_operations);
2215 * Generic helper for ->open on filesystems supporting disk quotas.
2217 int dquot_file_open(struct inode *inode, struct file *file)
2221 error = generic_file_open(inode, file);
2222 if (!error && (file->f_mode & FMODE_WRITE))
2223 error = dquot_initialize(inode);
2226 EXPORT_SYMBOL(dquot_file_open);
2229 * Turn quota off on a device. type == -1 ==> quotaoff for all types (umount)
2231 int dquot_disable(struct super_block *sb, int type, unsigned int flags)
2234 struct quota_info *dqopt = sb_dqopt(sb);
2235 struct inode *toputinode[MAXQUOTAS];
2237 /* s_umount should be held in exclusive mode */
2238 if (WARN_ON_ONCE(down_read_trylock(&sb->s_umount)))
2239 up_read(&sb->s_umount);
2241 /* Cannot turn off usage accounting without turning off limits, or
2242 * suspend quotas and simultaneously turn quotas off. */
2243 if ((flags & DQUOT_USAGE_ENABLED && !(flags & DQUOT_LIMITS_ENABLED))
2244 || (flags & DQUOT_SUSPENDED && flags & (DQUOT_LIMITS_ENABLED |
2245 DQUOT_USAGE_ENABLED)))
2249 * Skip everything if there's nothing to do. We have to do this because
2250 * sometimes we are called when fill_super() failed and calling
2251 * sync_fs() in such cases does no good.
2253 if (!sb_any_quota_loaded(sb))
2256 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
2257 toputinode[cnt] = NULL;
2258 if (type != -1 && cnt != type)
2260 if (!sb_has_quota_loaded(sb, cnt))
2263 if (flags & DQUOT_SUSPENDED) {
2264 spin_lock(&dq_state_lock);
2266 dquot_state_flag(DQUOT_SUSPENDED, cnt);
2267 spin_unlock(&dq_state_lock);
2269 spin_lock(&dq_state_lock);
2270 dqopt->flags &= ~dquot_state_flag(flags, cnt);
2271 /* Turning off suspended quotas? */
2272 if (!sb_has_quota_loaded(sb, cnt) &&
2273 sb_has_quota_suspended(sb, cnt)) {
2274 dqopt->flags &= ~dquot_state_flag(
2275 DQUOT_SUSPENDED, cnt);
2276 spin_unlock(&dq_state_lock);
2277 iput(dqopt->files[cnt]);
2278 dqopt->files[cnt] = NULL;
2281 spin_unlock(&dq_state_lock);
2284 /* We still have to keep quota loaded? */
2285 if (sb_has_quota_loaded(sb, cnt) && !(flags & DQUOT_SUSPENDED))
2288 /* Note: these are blocking operations */
2289 drop_dquot_ref(sb, cnt);
2290 invalidate_dquots(sb, cnt);
2292 * Now all dquots should be invalidated, all writes done so we
2293 * should be only users of the info. No locks needed.
2295 if (info_dirty(&dqopt->info[cnt]))
2296 sb->dq_op->write_info(sb, cnt);
2297 if (dqopt->ops[cnt]->free_file_info)
2298 dqopt->ops[cnt]->free_file_info(sb, cnt);
2299 put_quota_format(dqopt->info[cnt].dqi_format);
2301 toputinode[cnt] = dqopt->files[cnt];
2302 if (!sb_has_quota_loaded(sb, cnt))
2303 dqopt->files[cnt] = NULL;
2304 dqopt->info[cnt].dqi_flags = 0;
2305 dqopt->info[cnt].dqi_igrace = 0;
2306 dqopt->info[cnt].dqi_bgrace = 0;
2307 dqopt->ops[cnt] = NULL;
2310 /* Skip syncing and setting flags if quota files are hidden */
2311 if (dqopt->flags & DQUOT_QUOTA_SYS_FILE)
2314 /* Sync the superblock so that buffers with quota data are written to
2315 * disk (and so userspace sees correct data afterwards). */
2316 if (sb->s_op->sync_fs)
2317 sb->s_op->sync_fs(sb, 1);
2318 sync_blockdev(sb->s_bdev);
2319 /* Now the quota files are just ordinary files and we can set the
2320 * inode flags back. Moreover we discard the pagecache so that
2321 * userspace sees the writes we did bypassing the pagecache. We
2322 * must also discard the blockdev buffers so that we see the
2323 * changes done by userspace on the next quotaon() */
2324 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
2325 /* This can happen when suspending quotas on remount-ro... */
2326 if (toputinode[cnt] && !sb_has_quota_loaded(sb, cnt)) {
2327 inode_lock(toputinode[cnt]);
2328 toputinode[cnt]->i_flags &= ~S_NOQUOTA;
2329 truncate_inode_pages(&toputinode[cnt]->i_data, 0);
2330 inode_unlock(toputinode[cnt]);
2331 mark_inode_dirty_sync(toputinode[cnt]);
2334 invalidate_bdev(sb->s_bdev);
2336 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
2337 if (toputinode[cnt]) {
2338 /* On remount RO, we keep the inode pointer so that we
2339 * can reenable quota on the subsequent remount RW. We
2340 * have to check 'flags' variable and not use sb_has_
2341 * function because another quotaon / quotaoff could
2342 * change global state before we got here. We refuse
2343 * to suspend quotas when there is pending delete on
2344 * the quota file... */
2345 if (!(flags & DQUOT_SUSPENDED))
2346 iput(toputinode[cnt]);
2347 else if (!toputinode[cnt]->i_nlink)
2352 EXPORT_SYMBOL(dquot_disable);
2354 int dquot_quota_off(struct super_block *sb, int type)
2356 return dquot_disable(sb, type,
2357 DQUOT_USAGE_ENABLED | DQUOT_LIMITS_ENABLED);
2359 EXPORT_SYMBOL(dquot_quota_off);
2362 * Turn quotas on on a device
2365 static int vfs_setup_quota_inode(struct inode *inode, int type)
2367 struct super_block *sb = inode->i_sb;
2368 struct quota_info *dqopt = sb_dqopt(sb);
2370 if (is_bad_inode(inode))
2372 if (!S_ISREG(inode->i_mode))
2374 if (IS_RDONLY(inode))
2376 if (sb_has_quota_loaded(sb, type))
2379 dqopt->files[type] = igrab(inode);
2380 if (!dqopt->files[type])
2382 if (!(dqopt->flags & DQUOT_QUOTA_SYS_FILE)) {
2383 /* We don't want quota and atime on quota files (deadlocks
2384 * possible) Also nobody should write to the file - we use
2385 * special IO operations which ignore the immutable bit. */
2387 inode->i_flags |= S_NOQUOTA;
2388 inode_unlock(inode);
2390 * When S_NOQUOTA is set, remove dquot references as no more
2391 * references can be added
2393 __dquot_drop(inode);
2398 static void vfs_cleanup_quota_inode(struct super_block *sb, int type)
2400 struct quota_info *dqopt = sb_dqopt(sb);
2401 struct inode *inode = dqopt->files[type];
2403 if (!(dqopt->flags & DQUOT_QUOTA_SYS_FILE)) {
2405 inode->i_flags &= ~S_NOQUOTA;
2406 inode_unlock(inode);
2408 dqopt->files[type] = NULL;
2412 int dquot_load_quota_sb(struct super_block *sb, int type, int format_id,
2415 struct quota_format_type *fmt = find_quota_format(format_id);
2416 struct quota_info *dqopt = sb_dqopt(sb);
2421 if (!sb->s_op->quota_write || !sb->s_op->quota_read ||
2422 (type == PRJQUOTA && sb->dq_op->get_projid == NULL)) {
2426 /* Filesystems outside of init_user_ns not yet supported */
2427 if (sb->s_user_ns != &init_user_ns) {
2431 /* Usage always has to be set... */
2432 if (!(flags & DQUOT_USAGE_ENABLED)) {
2436 if (sb_has_quota_loaded(sb, type)) {
2441 if (!(dqopt->flags & DQUOT_QUOTA_SYS_FILE)) {
2442 /* As we bypass the pagecache we must now flush all the
2443 * dirty data and invalidate caches so that kernel sees
2444 * changes from userspace. It is not enough to just flush
2445 * the quota file since if blocksize < pagesize, invalidation
2446 * of the cache could fail because of other unrelated dirty
2448 sync_filesystem(sb);
2449 invalidate_bdev(sb->s_bdev);
2453 if (!fmt->qf_ops->check_quota_file(sb, type))
2456 dqopt->ops[type] = fmt->qf_ops;
2457 dqopt->info[type].dqi_format = fmt;
2458 dqopt->info[type].dqi_fmt_id = format_id;
2459 INIT_LIST_HEAD(&dqopt->info[type].dqi_dirty_list);
2460 error = dqopt->ops[type]->read_file_info(sb, type);
2463 if (dqopt->flags & DQUOT_QUOTA_SYS_FILE) {
2464 spin_lock(&dq_data_lock);
2465 dqopt->info[type].dqi_flags |= DQF_SYS_FILE;
2466 spin_unlock(&dq_data_lock);
2468 spin_lock(&dq_state_lock);
2469 dqopt->flags |= dquot_state_flag(flags, type);
2470 spin_unlock(&dq_state_lock);
2472 error = add_dquot_ref(sb, type);
2474 dquot_disable(sb, type,
2475 DQUOT_USAGE_ENABLED | DQUOT_LIMITS_ENABLED);
2479 put_quota_format(fmt);
2483 EXPORT_SYMBOL(dquot_load_quota_sb);
2486 * Helper function to turn quotas on when we already have the inode of
2487 * quota file and no quota information is loaded.
2489 static int vfs_load_quota_inode(struct inode *inode, int type, int format_id,
2494 err = vfs_setup_quota_inode(inode, type);
2497 err = dquot_load_quota_sb(inode->i_sb, type, format_id, flags);
2499 vfs_cleanup_quota_inode(inode->i_sb, type);
2503 /* Reenable quotas on remount RW */
2504 int dquot_resume(struct super_block *sb, int type)
2506 struct quota_info *dqopt = sb_dqopt(sb);
2507 struct inode *inode;
2511 /* s_umount should be held in exclusive mode */
2512 if (WARN_ON_ONCE(down_read_trylock(&sb->s_umount)))
2513 up_read(&sb->s_umount);
2515 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
2516 if (type != -1 && cnt != type)
2518 if (!sb_has_quota_suspended(sb, cnt))
2521 inode = dqopt->files[cnt];
2522 dqopt->files[cnt] = NULL;
2523 spin_lock(&dq_state_lock);
2524 flags = dqopt->flags & dquot_state_flag(DQUOT_USAGE_ENABLED |
2525 DQUOT_LIMITS_ENABLED,
2527 dqopt->flags &= ~dquot_state_flag(DQUOT_STATE_FLAGS, cnt);
2528 spin_unlock(&dq_state_lock);
2530 flags = dquot_generic_flag(flags, cnt);
2531 ret = vfs_load_quota_inode(inode, cnt,
2532 dqopt->info[cnt].dqi_fmt_id, flags);
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 = vfs_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 * More powerful function for turning on quotas allowing setting
2559 * of individual quota flags
2561 int dquot_enable(struct inode *inode, int type, int format_id,
2564 struct super_block *sb = inode->i_sb;
2566 /* Just unsuspend quotas? */
2567 BUG_ON(flags & DQUOT_SUSPENDED);
2568 /* s_umount should be held in exclusive mode */
2569 if (WARN_ON_ONCE(down_read_trylock(&sb->s_umount)))
2570 up_read(&sb->s_umount);
2574 /* Just updating flags needed? */
2575 if (sb_has_quota_loaded(sb, type)) {
2576 if (flags & DQUOT_USAGE_ENABLED &&
2577 sb_has_quota_usage_enabled(sb, type))
2579 if (flags & DQUOT_LIMITS_ENABLED &&
2580 sb_has_quota_limits_enabled(sb, type))
2582 spin_lock(&dq_state_lock);
2583 sb_dqopt(sb)->flags |= dquot_state_flag(flags, type);
2584 spin_unlock(&dq_state_lock);
2588 return vfs_load_quota_inode(inode, type, format_id, flags);
2590 EXPORT_SYMBOL(dquot_enable);
2593 * This function is used when filesystem needs to initialize quotas
2594 * during mount time.
2596 int dquot_quota_on_mount(struct super_block *sb, char *qf_name,
2597 int format_id, int type)
2599 struct dentry *dentry;
2602 dentry = lookup_positive_unlocked(qf_name, sb->s_root, strlen(qf_name));
2604 return PTR_ERR(dentry);
2606 error = security_quota_on(dentry);
2608 error = vfs_load_quota_inode(d_inode(dentry), type, format_id,
2609 DQUOT_USAGE_ENABLED | DQUOT_LIMITS_ENABLED);
2614 EXPORT_SYMBOL(dquot_quota_on_mount);
2616 static int dquot_quota_enable(struct super_block *sb, unsigned int flags)
2620 struct quota_info *dqopt = sb_dqopt(sb);
2622 if (!(dqopt->flags & DQUOT_QUOTA_SYS_FILE))
2624 /* Accounting cannot be turned on while fs is mounted */
2625 flags &= ~(FS_QUOTA_UDQ_ACCT | FS_QUOTA_GDQ_ACCT | FS_QUOTA_PDQ_ACCT);
2628 for (type = 0; type < MAXQUOTAS; type++) {
2629 if (!(flags & qtype_enforce_flag(type)))
2631 /* Can't enforce without accounting */
2632 if (!sb_has_quota_usage_enabled(sb, type))
2634 ret = dquot_enable(dqopt->files[type], type,
2635 dqopt->info[type].dqi_fmt_id,
2636 DQUOT_LIMITS_ENABLED);
2642 /* Backout enforcement enablement we already did */
2643 for (type--; type >= 0; type--) {
2644 if (flags & qtype_enforce_flag(type))
2645 dquot_disable(sb, type, DQUOT_LIMITS_ENABLED);
2647 /* Error code translation for better compatibility with XFS */
2653 static int dquot_quota_disable(struct super_block *sb, unsigned int flags)
2657 struct quota_info *dqopt = sb_dqopt(sb);
2659 if (!(dqopt->flags & DQUOT_QUOTA_SYS_FILE))
2662 * We don't support turning off accounting via quotactl. In principle
2663 * quota infrastructure can do this but filesystems don't expect
2664 * userspace to be able to do it.
2667 (FS_QUOTA_UDQ_ACCT | FS_QUOTA_GDQ_ACCT | FS_QUOTA_PDQ_ACCT))
2670 /* Filter out limits not enabled */
2671 for (type = 0; type < MAXQUOTAS; type++)
2672 if (!sb_has_quota_limits_enabled(sb, type))
2673 flags &= ~qtype_enforce_flag(type);
2677 for (type = 0; type < MAXQUOTAS; type++) {
2678 if (flags & qtype_enforce_flag(type)) {
2679 ret = dquot_disable(sb, type, DQUOT_LIMITS_ENABLED);
2686 /* Backout enforcement disabling we already did */
2687 for (type--; type >= 0; type--) {
2688 if (flags & qtype_enforce_flag(type))
2689 dquot_enable(dqopt->files[type], type,
2690 dqopt->info[type].dqi_fmt_id,
2691 DQUOT_LIMITS_ENABLED);
2696 /* Generic routine for getting common part of quota structure */
2697 static void do_get_dqblk(struct dquot *dquot, struct qc_dqblk *di)
2699 struct mem_dqblk *dm = &dquot->dq_dqb;
2701 memset(di, 0, sizeof(*di));
2702 spin_lock(&dquot->dq_dqb_lock);
2703 di->d_spc_hardlimit = dm->dqb_bhardlimit;
2704 di->d_spc_softlimit = dm->dqb_bsoftlimit;
2705 di->d_ino_hardlimit = dm->dqb_ihardlimit;
2706 di->d_ino_softlimit = dm->dqb_isoftlimit;
2707 di->d_space = dm->dqb_curspace + dm->dqb_rsvspace;
2708 di->d_ino_count = dm->dqb_curinodes;
2709 di->d_spc_timer = dm->dqb_btime;
2710 di->d_ino_timer = dm->dqb_itime;
2711 spin_unlock(&dquot->dq_dqb_lock);
2714 int dquot_get_dqblk(struct super_block *sb, struct kqid qid,
2715 struct qc_dqblk *di)
2717 struct dquot *dquot;
2719 dquot = dqget(sb, qid);
2721 return PTR_ERR(dquot);
2722 do_get_dqblk(dquot, di);
2727 EXPORT_SYMBOL(dquot_get_dqblk);
2729 int dquot_get_next_dqblk(struct super_block *sb, struct kqid *qid,
2730 struct qc_dqblk *di)
2732 struct dquot *dquot;
2735 if (!sb->dq_op->get_next_id)
2737 err = sb->dq_op->get_next_id(sb, qid);
2740 dquot = dqget(sb, *qid);
2742 return PTR_ERR(dquot);
2743 do_get_dqblk(dquot, di);
2748 EXPORT_SYMBOL(dquot_get_next_dqblk);
2750 #define VFS_QC_MASK \
2751 (QC_SPACE | QC_SPC_SOFT | QC_SPC_HARD | \
2752 QC_INO_COUNT | QC_INO_SOFT | QC_INO_HARD | \
2753 QC_SPC_TIMER | QC_INO_TIMER)
2755 /* Generic routine for setting common part of quota structure */
2756 static int do_set_dqblk(struct dquot *dquot, struct qc_dqblk *di)
2758 struct mem_dqblk *dm = &dquot->dq_dqb;
2759 int check_blim = 0, check_ilim = 0;
2760 struct mem_dqinfo *dqi = &sb_dqopt(dquot->dq_sb)->info[dquot->dq_id.type];
2762 if (di->d_fieldmask & ~VFS_QC_MASK)
2765 if (((di->d_fieldmask & QC_SPC_SOFT) &&
2766 di->d_spc_softlimit > dqi->dqi_max_spc_limit) ||
2767 ((di->d_fieldmask & QC_SPC_HARD) &&
2768 di->d_spc_hardlimit > dqi->dqi_max_spc_limit) ||
2769 ((di->d_fieldmask & QC_INO_SOFT) &&
2770 (di->d_ino_softlimit > dqi->dqi_max_ino_limit)) ||
2771 ((di->d_fieldmask & QC_INO_HARD) &&
2772 (di->d_ino_hardlimit > dqi->dqi_max_ino_limit)))
2775 spin_lock(&dquot->dq_dqb_lock);
2776 if (di->d_fieldmask & QC_SPACE) {
2777 dm->dqb_curspace = di->d_space - dm->dqb_rsvspace;
2779 set_bit(DQ_LASTSET_B + QIF_SPACE_B, &dquot->dq_flags);
2782 if (di->d_fieldmask & QC_SPC_SOFT)
2783 dm->dqb_bsoftlimit = di->d_spc_softlimit;
2784 if (di->d_fieldmask & QC_SPC_HARD)
2785 dm->dqb_bhardlimit = di->d_spc_hardlimit;
2786 if (di->d_fieldmask & (QC_SPC_SOFT | QC_SPC_HARD)) {
2788 set_bit(DQ_LASTSET_B + QIF_BLIMITS_B, &dquot->dq_flags);
2791 if (di->d_fieldmask & QC_INO_COUNT) {
2792 dm->dqb_curinodes = di->d_ino_count;
2794 set_bit(DQ_LASTSET_B + QIF_INODES_B, &dquot->dq_flags);
2797 if (di->d_fieldmask & QC_INO_SOFT)
2798 dm->dqb_isoftlimit = di->d_ino_softlimit;
2799 if (di->d_fieldmask & QC_INO_HARD)
2800 dm->dqb_ihardlimit = di->d_ino_hardlimit;
2801 if (di->d_fieldmask & (QC_INO_SOFT | QC_INO_HARD)) {
2803 set_bit(DQ_LASTSET_B + QIF_ILIMITS_B, &dquot->dq_flags);
2806 if (di->d_fieldmask & QC_SPC_TIMER) {
2807 dm->dqb_btime = di->d_spc_timer;
2809 set_bit(DQ_LASTSET_B + QIF_BTIME_B, &dquot->dq_flags);
2812 if (di->d_fieldmask & QC_INO_TIMER) {
2813 dm->dqb_itime = di->d_ino_timer;
2815 set_bit(DQ_LASTSET_B + QIF_ITIME_B, &dquot->dq_flags);
2819 if (!dm->dqb_bsoftlimit ||
2820 dm->dqb_curspace + dm->dqb_rsvspace <= dm->dqb_bsoftlimit) {
2822 clear_bit(DQ_BLKS_B, &dquot->dq_flags);
2823 } else if (!(di->d_fieldmask & QC_SPC_TIMER))
2824 /* Set grace only if user hasn't provided his own... */
2825 dm->dqb_btime = ktime_get_real_seconds() + dqi->dqi_bgrace;
2828 if (!dm->dqb_isoftlimit ||
2829 dm->dqb_curinodes <= dm->dqb_isoftlimit) {
2831 clear_bit(DQ_INODES_B, &dquot->dq_flags);
2832 } else if (!(di->d_fieldmask & QC_INO_TIMER))
2833 /* Set grace only if user hasn't provided his own... */
2834 dm->dqb_itime = ktime_get_real_seconds() + dqi->dqi_igrace;
2836 if (dm->dqb_bhardlimit || dm->dqb_bsoftlimit || dm->dqb_ihardlimit ||
2838 clear_bit(DQ_FAKE_B, &dquot->dq_flags);
2840 set_bit(DQ_FAKE_B, &dquot->dq_flags);
2841 spin_unlock(&dquot->dq_dqb_lock);
2842 mark_dquot_dirty(dquot);
2847 int dquot_set_dqblk(struct super_block *sb, struct kqid qid,
2848 struct qc_dqblk *di)
2850 struct dquot *dquot;
2853 dquot = dqget(sb, qid);
2854 if (IS_ERR(dquot)) {
2855 rc = PTR_ERR(dquot);
2858 rc = do_set_dqblk(dquot, di);
2863 EXPORT_SYMBOL(dquot_set_dqblk);
2865 /* Generic routine for getting common part of quota file information */
2866 int dquot_get_state(struct super_block *sb, struct qc_state *state)
2868 struct mem_dqinfo *mi;
2869 struct qc_type_state *tstate;
2870 struct quota_info *dqopt = sb_dqopt(sb);
2873 memset(state, 0, sizeof(*state));
2874 for (type = 0; type < MAXQUOTAS; type++) {
2875 if (!sb_has_quota_active(sb, type))
2877 tstate = state->s_state + type;
2878 mi = sb_dqopt(sb)->info + type;
2879 tstate->flags = QCI_ACCT_ENABLED;
2880 spin_lock(&dq_data_lock);
2881 if (mi->dqi_flags & DQF_SYS_FILE)
2882 tstate->flags |= QCI_SYSFILE;
2883 if (mi->dqi_flags & DQF_ROOT_SQUASH)
2884 tstate->flags |= QCI_ROOT_SQUASH;
2885 if (sb_has_quota_limits_enabled(sb, type))
2886 tstate->flags |= QCI_LIMITS_ENFORCED;
2887 tstate->spc_timelimit = mi->dqi_bgrace;
2888 tstate->ino_timelimit = mi->dqi_igrace;
2889 tstate->ino = dqopt->files[type]->i_ino;
2890 tstate->blocks = dqopt->files[type]->i_blocks;
2891 tstate->nextents = 1; /* We don't know... */
2892 spin_unlock(&dq_data_lock);
2896 EXPORT_SYMBOL(dquot_get_state);
2898 /* Generic routine for setting common part of quota file information */
2899 int dquot_set_dqinfo(struct super_block *sb, int type, struct qc_info *ii)
2901 struct mem_dqinfo *mi;
2904 if ((ii->i_fieldmask & QC_WARNS_MASK) ||
2905 (ii->i_fieldmask & QC_RT_SPC_TIMER))
2907 if (!sb_has_quota_active(sb, type))
2909 mi = sb_dqopt(sb)->info + type;
2910 if (ii->i_fieldmask & QC_FLAGS) {
2911 if ((ii->i_flags & QCI_ROOT_SQUASH &&
2912 mi->dqi_format->qf_fmt_id != QFMT_VFS_OLD))
2915 spin_lock(&dq_data_lock);
2916 if (ii->i_fieldmask & QC_SPC_TIMER)
2917 mi->dqi_bgrace = ii->i_spc_timelimit;
2918 if (ii->i_fieldmask & QC_INO_TIMER)
2919 mi->dqi_igrace = ii->i_ino_timelimit;
2920 if (ii->i_fieldmask & QC_FLAGS) {
2921 if (ii->i_flags & QCI_ROOT_SQUASH)
2922 mi->dqi_flags |= DQF_ROOT_SQUASH;
2924 mi->dqi_flags &= ~DQF_ROOT_SQUASH;
2926 spin_unlock(&dq_data_lock);
2927 mark_info_dirty(sb, type);
2928 /* Force write to disk */
2929 sb->dq_op->write_info(sb, type);
2932 EXPORT_SYMBOL(dquot_set_dqinfo);
2934 const struct quotactl_ops dquot_quotactl_sysfile_ops = {
2935 .quota_enable = dquot_quota_enable,
2936 .quota_disable = dquot_quota_disable,
2937 .quota_sync = dquot_quota_sync,
2938 .get_state = dquot_get_state,
2939 .set_info = dquot_set_dqinfo,
2940 .get_dqblk = dquot_get_dqblk,
2941 .get_nextdqblk = dquot_get_next_dqblk,
2942 .set_dqblk = dquot_set_dqblk
2944 EXPORT_SYMBOL(dquot_quotactl_sysfile_ops);
2946 static int do_proc_dqstats(struct ctl_table *table, int write,
2947 void __user *buffer, size_t *lenp, loff_t *ppos)
2949 unsigned int type = (unsigned long *)table->data - dqstats.stat;
2950 s64 value = percpu_counter_sum(&dqstats.counter[type]);
2952 /* Filter negative values for non-monotonic counters */
2953 if (value < 0 && (type == DQST_ALLOC_DQUOTS ||
2954 type == DQST_FREE_DQUOTS))
2957 /* Update global table */
2958 dqstats.stat[type] = value;
2959 return proc_doulongvec_minmax(table, write, buffer, lenp, ppos);
2962 static struct ctl_table fs_dqstats_table[] = {
2964 .procname = "lookups",
2965 .data = &dqstats.stat[DQST_LOOKUPS],
2966 .maxlen = sizeof(unsigned long),
2968 .proc_handler = do_proc_dqstats,
2971 .procname = "drops",
2972 .data = &dqstats.stat[DQST_DROPS],
2973 .maxlen = sizeof(unsigned long),
2975 .proc_handler = do_proc_dqstats,
2978 .procname = "reads",
2979 .data = &dqstats.stat[DQST_READS],
2980 .maxlen = sizeof(unsigned long),
2982 .proc_handler = do_proc_dqstats,
2985 .procname = "writes",
2986 .data = &dqstats.stat[DQST_WRITES],
2987 .maxlen = sizeof(unsigned long),
2989 .proc_handler = do_proc_dqstats,
2992 .procname = "cache_hits",
2993 .data = &dqstats.stat[DQST_CACHE_HITS],
2994 .maxlen = sizeof(unsigned long),
2996 .proc_handler = do_proc_dqstats,
2999 .procname = "allocated_dquots",
3000 .data = &dqstats.stat[DQST_ALLOC_DQUOTS],
3001 .maxlen = sizeof(unsigned long),
3003 .proc_handler = do_proc_dqstats,
3006 .procname = "free_dquots",
3007 .data = &dqstats.stat[DQST_FREE_DQUOTS],
3008 .maxlen = sizeof(unsigned long),
3010 .proc_handler = do_proc_dqstats,
3013 .procname = "syncs",
3014 .data = &dqstats.stat[DQST_SYNCS],
3015 .maxlen = sizeof(unsigned long),
3017 .proc_handler = do_proc_dqstats,
3019 #ifdef CONFIG_PRINT_QUOTA_WARNING
3021 .procname = "warnings",
3022 .data = &flag_print_warnings,
3023 .maxlen = sizeof(int),
3025 .proc_handler = proc_dointvec,
3031 static struct ctl_table fs_table[] = {
3033 .procname = "quota",
3035 .child = fs_dqstats_table,
3040 static struct ctl_table sys_table[] = {
3049 static int __init dquot_init(void)
3052 unsigned long nr_hash, order;
3054 printk(KERN_NOTICE "VFS: Disk quotas %s\n", __DQUOT_VERSION__);
3056 register_sysctl_table(sys_table);
3058 dquot_cachep = kmem_cache_create("dquot",
3059 sizeof(struct dquot), sizeof(unsigned long) * 4,
3060 (SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT|
3061 SLAB_MEM_SPREAD|SLAB_PANIC),
3065 dquot_hash = (struct hlist_head *)__get_free_pages(GFP_KERNEL, order);
3067 panic("Cannot create dquot hash table");
3069 for (i = 0; i < _DQST_DQSTAT_LAST; i++) {
3070 ret = percpu_counter_init(&dqstats.counter[i], 0, GFP_KERNEL);
3072 panic("Cannot create dquot stat counters");
3075 /* Find power-of-two hlist_heads which can fit into allocation */
3076 nr_hash = (1UL << order) * PAGE_SIZE / sizeof(struct hlist_head);
3080 } while (nr_hash >> dq_hash_bits);
3083 nr_hash = 1UL << dq_hash_bits;
3084 dq_hash_mask = nr_hash - 1;
3085 for (i = 0; i < nr_hash; i++)
3086 INIT_HLIST_HEAD(dquot_hash + i);
3088 pr_info("VFS: Dquot-cache hash table entries: %ld (order %ld,"
3089 " %ld bytes)\n", nr_hash, order, (PAGE_SIZE << order));
3091 if (register_shrinker(&dqcache_shrinker))
3092 panic("Cannot register dquot shrinker");
3096 fs_initcall(dquot_init);