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 four lists for dquot management: the inuse_list,
229 * free_dquots, dqi_dirty_list, and dquot_hash[] array. A single dquot
230 * structure may be on some of those lists, depending on its current state.
232 * All dquots are placed to the end of inuse_list when first created, and this
233 * list is used for invalidate operation, which must look at every dquot.
235 * Unused dquots (dq_count == 0) are added to the free_dquots list when freed,
236 * and this list is searched whenever we need an available dquot. Dquots are
237 * removed from the list as soon as they are used again, and
238 * dqstats.free_dquots gives the number of dquots on the list. When
239 * dquot is invalidated it's completely released from memory.
241 * Dirty dquots are added to the dqi_dirty_list of quota_info when mark
242 * dirtied, and this list is searched when writing dirty dquots back to
243 * quota file. Note that some filesystems do dirty dquot tracking on their
244 * own (e.g. in a journal) and thus don't use dqi_dirty_list.
246 * Dquots with a specific identity (device, type and id) are placed on
247 * one of the dquot_hash[] hash chains. The provides an efficient search
248 * mechanism to locate a specific dquot.
251 static LIST_HEAD(inuse_list);
252 static LIST_HEAD(free_dquots);
253 static unsigned int dq_hash_bits, dq_hash_mask;
254 static struct hlist_head *dquot_hash;
256 struct dqstats dqstats;
257 EXPORT_SYMBOL(dqstats);
259 static qsize_t inode_get_rsv_space(struct inode *inode);
260 static qsize_t __inode_get_rsv_space(struct inode *inode);
261 static int __dquot_initialize(struct inode *inode, int type);
263 static inline unsigned int
264 hashfn(const struct super_block *sb, struct kqid qid)
266 unsigned int id = from_kqid(&init_user_ns, qid);
270 tmp = (((unsigned long)sb>>L1_CACHE_SHIFT) ^ id) * (MAXQUOTAS - type);
271 return (tmp + (tmp >> dq_hash_bits)) & dq_hash_mask;
275 * Following list functions expect dq_list_lock to be held
277 static inline void insert_dquot_hash(struct dquot *dquot)
279 struct hlist_head *head;
280 head = dquot_hash + hashfn(dquot->dq_sb, dquot->dq_id);
281 hlist_add_head(&dquot->dq_hash, head);
284 static inline void remove_dquot_hash(struct dquot *dquot)
286 hlist_del_init(&dquot->dq_hash);
289 static struct dquot *find_dquot(unsigned int hashent, struct super_block *sb,
292 struct hlist_node *node;
295 hlist_for_each (node, dquot_hash+hashent) {
296 dquot = hlist_entry(node, struct dquot, dq_hash);
297 if (dquot->dq_sb == sb && qid_eq(dquot->dq_id, qid))
303 /* Add a dquot to the tail of the free list */
304 static inline void put_dquot_last(struct dquot *dquot)
306 list_add_tail(&dquot->dq_free, &free_dquots);
307 dqstats_inc(DQST_FREE_DQUOTS);
310 static inline void remove_free_dquot(struct dquot *dquot)
312 if (list_empty(&dquot->dq_free))
314 list_del_init(&dquot->dq_free);
315 dqstats_dec(DQST_FREE_DQUOTS);
318 static inline void put_inuse(struct dquot *dquot)
320 /* We add to the back of inuse list so we don't have to restart
321 * when traversing this list and we block */
322 list_add_tail(&dquot->dq_inuse, &inuse_list);
323 dqstats_inc(DQST_ALLOC_DQUOTS);
326 static inline void remove_inuse(struct dquot *dquot)
328 dqstats_dec(DQST_ALLOC_DQUOTS);
329 list_del(&dquot->dq_inuse);
332 * End of list functions needing dq_list_lock
335 static void wait_on_dquot(struct dquot *dquot)
337 mutex_lock(&dquot->dq_lock);
338 mutex_unlock(&dquot->dq_lock);
341 static inline int dquot_dirty(struct dquot *dquot)
343 return test_bit(DQ_MOD_B, &dquot->dq_flags);
346 static inline int mark_dquot_dirty(struct dquot *dquot)
348 return dquot->dq_sb->dq_op->mark_dirty(dquot);
351 /* Mark dquot dirty in atomic manner, and return it's old dirty flag state */
352 int dquot_mark_dquot_dirty(struct dquot *dquot)
356 if (!test_bit(DQ_ACTIVE_B, &dquot->dq_flags))
359 if (sb_dqopt(dquot->dq_sb)->flags & DQUOT_NOLIST_DIRTY)
360 return test_and_set_bit(DQ_MOD_B, &dquot->dq_flags);
362 /* If quota is dirty already, we don't have to acquire dq_list_lock */
363 if (test_bit(DQ_MOD_B, &dquot->dq_flags))
366 spin_lock(&dq_list_lock);
367 if (!test_and_set_bit(DQ_MOD_B, &dquot->dq_flags)) {
368 list_add(&dquot->dq_dirty, &sb_dqopt(dquot->dq_sb)->
369 info[dquot->dq_id.type].dqi_dirty_list);
372 spin_unlock(&dq_list_lock);
375 EXPORT_SYMBOL(dquot_mark_dquot_dirty);
377 /* Dirtify all the dquots - this can block when journalling */
378 static inline int mark_all_dquot_dirty(struct dquot * const *dquot)
383 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
385 /* Even in case of error we have to continue */
386 ret = mark_dquot_dirty(dquot[cnt]);
393 static inline void dqput_all(struct dquot **dquot)
397 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
401 static inline int clear_dquot_dirty(struct dquot *dquot)
403 if (sb_dqopt(dquot->dq_sb)->flags & DQUOT_NOLIST_DIRTY)
404 return test_and_clear_bit(DQ_MOD_B, &dquot->dq_flags);
406 spin_lock(&dq_list_lock);
407 if (!test_and_clear_bit(DQ_MOD_B, &dquot->dq_flags)) {
408 spin_unlock(&dq_list_lock);
411 list_del_init(&dquot->dq_dirty);
412 spin_unlock(&dq_list_lock);
416 void mark_info_dirty(struct super_block *sb, int type)
418 spin_lock(&dq_data_lock);
419 sb_dqopt(sb)->info[type].dqi_flags |= DQF_INFO_DIRTY;
420 spin_unlock(&dq_data_lock);
422 EXPORT_SYMBOL(mark_info_dirty);
425 * Read dquot from disk and alloc space for it
428 int dquot_acquire(struct dquot *dquot)
430 int ret = 0, ret2 = 0;
431 unsigned int memalloc;
432 struct quota_info *dqopt = sb_dqopt(dquot->dq_sb);
434 mutex_lock(&dquot->dq_lock);
435 memalloc = memalloc_nofs_save();
436 if (!test_bit(DQ_READ_B, &dquot->dq_flags)) {
437 ret = dqopt->ops[dquot->dq_id.type]->read_dqblk(dquot);
441 /* Make sure flags update is visible after dquot has been filled */
442 smp_mb__before_atomic();
443 set_bit(DQ_READ_B, &dquot->dq_flags);
444 /* Instantiate dquot if needed */
445 if (!test_bit(DQ_ACTIVE_B, &dquot->dq_flags) && !dquot->dq_off) {
446 ret = dqopt->ops[dquot->dq_id.type]->commit_dqblk(dquot);
447 /* Write the info if needed */
448 if (info_dirty(&dqopt->info[dquot->dq_id.type])) {
449 ret2 = dqopt->ops[dquot->dq_id.type]->write_file_info(
450 dquot->dq_sb, dquot->dq_id.type);
460 * Make sure flags update is visible after on-disk struct has been
461 * allocated. Paired with smp_rmb() in dqget().
463 smp_mb__before_atomic();
464 set_bit(DQ_ACTIVE_B, &dquot->dq_flags);
466 memalloc_nofs_restore(memalloc);
467 mutex_unlock(&dquot->dq_lock);
470 EXPORT_SYMBOL(dquot_acquire);
473 * Write dquot to disk
475 int dquot_commit(struct dquot *dquot)
478 unsigned int memalloc;
479 struct quota_info *dqopt = sb_dqopt(dquot->dq_sb);
481 mutex_lock(&dquot->dq_lock);
482 memalloc = memalloc_nofs_save();
483 if (!clear_dquot_dirty(dquot))
485 /* Inactive dquot can be only if there was error during read/init
486 * => we have better not writing it */
487 if (test_bit(DQ_ACTIVE_B, &dquot->dq_flags))
488 ret = dqopt->ops[dquot->dq_id.type]->commit_dqblk(dquot);
492 memalloc_nofs_restore(memalloc);
493 mutex_unlock(&dquot->dq_lock);
496 EXPORT_SYMBOL(dquot_commit);
501 int dquot_release(struct dquot *dquot)
503 int ret = 0, ret2 = 0;
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 /* Check whether we are not racing with some other dqget() */
510 if (dquot_is_busy(dquot))
512 if (dqopt->ops[dquot->dq_id.type]->release_dqblk) {
513 ret = dqopt->ops[dquot->dq_id.type]->release_dqblk(dquot);
515 if (info_dirty(&dqopt->info[dquot->dq_id.type])) {
516 ret2 = dqopt->ops[dquot->dq_id.type]->write_file_info(
517 dquot->dq_sb, dquot->dq_id.type);
522 clear_bit(DQ_ACTIVE_B, &dquot->dq_flags);
524 memalloc_nofs_restore(memalloc);
525 mutex_unlock(&dquot->dq_lock);
528 EXPORT_SYMBOL(dquot_release);
530 void dquot_destroy(struct dquot *dquot)
532 kmem_cache_free(dquot_cachep, dquot);
534 EXPORT_SYMBOL(dquot_destroy);
536 static inline void do_destroy_dquot(struct dquot *dquot)
538 dquot->dq_sb->dq_op->destroy_dquot(dquot);
541 /* Invalidate all dquots on the list. Note that this function is called after
542 * quota is disabled and pointers from inodes removed so there cannot be new
543 * quota users. There can still be some users of quotas due to inodes being
544 * just deleted or pruned by prune_icache() (those are not attached to any
545 * list) or parallel quotactl call. We have to wait for such users.
547 static void invalidate_dquots(struct super_block *sb, int type)
549 struct dquot *dquot, *tmp;
552 spin_lock(&dq_list_lock);
553 list_for_each_entry_safe(dquot, tmp, &inuse_list, dq_inuse) {
554 if (dquot->dq_sb != sb)
556 if (dquot->dq_id.type != type)
558 /* Wait for dquot users */
559 if (atomic_read(&dquot->dq_count)) {
561 spin_unlock(&dq_list_lock);
563 * Once dqput() wakes us up, we know it's time to free
565 * IMPORTANT: we rely on the fact that there is always
566 * at most one process waiting for dquot to free.
567 * Otherwise dq_count would be > 1 and we would never
570 wait_event(dquot_ref_wq,
571 atomic_read(&dquot->dq_count) == 1);
573 /* At this moment dquot() need not exist (it could be
574 * reclaimed by prune_dqcache(). Hence we must
579 * Quota now has no users and it has been written on last
582 remove_dquot_hash(dquot);
583 remove_free_dquot(dquot);
585 do_destroy_dquot(dquot);
587 spin_unlock(&dq_list_lock);
590 /* Call callback for every active dquot on given filesystem */
591 int dquot_scan_active(struct super_block *sb,
592 int (*fn)(struct dquot *dquot, unsigned long priv),
595 struct dquot *dquot, *old_dquot = NULL;
598 WARN_ON_ONCE(!rwsem_is_locked(&sb->s_umount));
600 spin_lock(&dq_list_lock);
601 list_for_each_entry(dquot, &inuse_list, dq_inuse) {
602 if (!test_bit(DQ_ACTIVE_B, &dquot->dq_flags))
604 if (dquot->dq_sb != sb)
606 /* Now we have active dquot so we can just increase use count */
607 atomic_inc(&dquot->dq_count);
608 spin_unlock(&dq_list_lock);
612 * ->release_dquot() can be racing with us. Our reference
613 * protects us from new calls to it so just wait for any
614 * outstanding call and recheck the DQ_ACTIVE_B after that.
616 wait_on_dquot(dquot);
617 if (test_bit(DQ_ACTIVE_B, &dquot->dq_flags)) {
618 ret = fn(dquot, priv);
622 spin_lock(&dq_list_lock);
623 /* We are safe to continue now because our dquot could not
624 * be moved out of the inuse list while we hold the reference */
626 spin_unlock(&dq_list_lock);
631 EXPORT_SYMBOL(dquot_scan_active);
633 /* Write all dquot structures to quota files */
634 int dquot_writeback_dquots(struct super_block *sb, int type)
636 struct list_head dirty;
638 struct quota_info *dqopt = sb_dqopt(sb);
642 WARN_ON_ONCE(!rwsem_is_locked(&sb->s_umount));
644 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
645 if (type != -1 && cnt != type)
647 if (!sb_has_quota_active(sb, cnt))
649 spin_lock(&dq_list_lock);
650 /* Move list away to avoid livelock. */
651 list_replace_init(&dqopt->info[cnt].dqi_dirty_list, &dirty);
652 while (!list_empty(&dirty)) {
653 dquot = list_first_entry(&dirty, struct dquot,
656 WARN_ON(!test_bit(DQ_ACTIVE_B, &dquot->dq_flags));
658 /* Now we have active dquot from which someone is
659 * holding reference so we can safely just increase
662 spin_unlock(&dq_list_lock);
663 err = sb->dq_op->write_dquot(dquot);
666 * Clear dirty bit anyway to avoid infinite
669 clear_dquot_dirty(dquot);
674 spin_lock(&dq_list_lock);
676 spin_unlock(&dq_list_lock);
679 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
680 if ((cnt == type || type == -1) && sb_has_quota_active(sb, cnt)
681 && info_dirty(&dqopt->info[cnt]))
682 sb->dq_op->write_info(sb, cnt);
683 dqstats_inc(DQST_SYNCS);
687 EXPORT_SYMBOL(dquot_writeback_dquots);
689 /* Write all dquot structures to disk and make them visible from userspace */
690 int dquot_quota_sync(struct super_block *sb, int type)
692 struct quota_info *dqopt = sb_dqopt(sb);
696 ret = dquot_writeback_dquots(sb, type);
699 if (dqopt->flags & DQUOT_QUOTA_SYS_FILE)
702 /* This is not very clever (and fast) but currently I don't know about
703 * any other simple way of getting quota data to disk and we must get
704 * them there for userspace to be visible... */
705 if (sb->s_op->sync_fs) {
706 ret = sb->s_op->sync_fs(sb, 1);
710 ret = sync_blockdev(sb->s_bdev);
715 * Now when everything is written we can discard the pagecache so
716 * that userspace sees the changes.
718 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
719 if (type != -1 && cnt != type)
721 if (!sb_has_quota_active(sb, cnt))
723 inode_lock(dqopt->files[cnt]);
724 truncate_inode_pages(&dqopt->files[cnt]->i_data, 0);
725 inode_unlock(dqopt->files[cnt]);
730 EXPORT_SYMBOL(dquot_quota_sync);
733 dqcache_shrink_scan(struct shrinker *shrink, struct shrink_control *sc)
736 unsigned long freed = 0;
738 spin_lock(&dq_list_lock);
739 while (!list_empty(&free_dquots) && sc->nr_to_scan) {
740 dquot = list_first_entry(&free_dquots, struct dquot, dq_free);
741 remove_dquot_hash(dquot);
742 remove_free_dquot(dquot);
744 do_destroy_dquot(dquot);
748 spin_unlock(&dq_list_lock);
753 dqcache_shrink_count(struct shrinker *shrink, struct shrink_control *sc)
755 return vfs_pressure_ratio(
756 percpu_counter_read_positive(&dqstats.counter[DQST_FREE_DQUOTS]));
759 static struct shrinker dqcache_shrinker = {
760 .count_objects = dqcache_shrink_count,
761 .scan_objects = dqcache_shrink_scan,
762 .seeks = DEFAULT_SEEKS,
766 * Put reference to dquot
768 void dqput(struct dquot *dquot)
774 #ifdef CONFIG_QUOTA_DEBUG
775 if (!atomic_read(&dquot->dq_count)) {
776 quota_error(dquot->dq_sb, "trying to free free dquot of %s %d",
777 quotatypes[dquot->dq_id.type],
778 from_kqid(&init_user_ns, dquot->dq_id));
782 dqstats_inc(DQST_DROPS);
784 spin_lock(&dq_list_lock);
785 if (atomic_read(&dquot->dq_count) > 1) {
786 /* We have more than one user... nothing to do */
787 atomic_dec(&dquot->dq_count);
788 /* Releasing dquot during quotaoff phase? */
789 if (!sb_has_quota_active(dquot->dq_sb, dquot->dq_id.type) &&
790 atomic_read(&dquot->dq_count) == 1)
791 wake_up(&dquot_ref_wq);
792 spin_unlock(&dq_list_lock);
795 /* Need to release dquot? */
796 if (dquot_dirty(dquot)) {
797 spin_unlock(&dq_list_lock);
798 /* Commit dquot before releasing */
799 ret = dquot->dq_sb->dq_op->write_dquot(dquot);
801 quota_error(dquot->dq_sb, "Can't write quota structure"
802 " (error %d). Quota may get out of sync!",
805 * We clear dirty bit anyway, so that we avoid
808 clear_dquot_dirty(dquot);
812 if (test_bit(DQ_ACTIVE_B, &dquot->dq_flags)) {
813 spin_unlock(&dq_list_lock);
814 dquot->dq_sb->dq_op->release_dquot(dquot);
817 atomic_dec(&dquot->dq_count);
818 #ifdef CONFIG_QUOTA_DEBUG
820 BUG_ON(!list_empty(&dquot->dq_free));
822 put_dquot_last(dquot);
823 spin_unlock(&dq_list_lock);
825 EXPORT_SYMBOL(dqput);
827 struct dquot *dquot_alloc(struct super_block *sb, int type)
829 return kmem_cache_zalloc(dquot_cachep, GFP_NOFS);
831 EXPORT_SYMBOL(dquot_alloc);
833 static struct dquot *get_empty_dquot(struct super_block *sb, int type)
837 dquot = sb->dq_op->alloc_dquot(sb, type);
841 mutex_init(&dquot->dq_lock);
842 INIT_LIST_HEAD(&dquot->dq_free);
843 INIT_LIST_HEAD(&dquot->dq_inuse);
844 INIT_HLIST_NODE(&dquot->dq_hash);
845 INIT_LIST_HEAD(&dquot->dq_dirty);
847 dquot->dq_id = make_kqid_invalid(type);
848 atomic_set(&dquot->dq_count, 1);
849 spin_lock_init(&dquot->dq_dqb_lock);
855 * Get reference to dquot
857 * Locking is slightly tricky here. We are guarded from parallel quotaoff()
858 * destroying our dquot by:
859 * a) checking for quota flags under dq_list_lock and
860 * b) getting a reference to dquot before we release dq_list_lock
862 struct dquot *dqget(struct super_block *sb, struct kqid qid)
864 unsigned int hashent = hashfn(sb, qid);
865 struct dquot *dquot, *empty = NULL;
867 if (!qid_has_mapping(sb->s_user_ns, qid))
868 return ERR_PTR(-EINVAL);
870 if (!sb_has_quota_active(sb, qid.type))
871 return ERR_PTR(-ESRCH);
873 spin_lock(&dq_list_lock);
874 spin_lock(&dq_state_lock);
875 if (!sb_has_quota_active(sb, qid.type)) {
876 spin_unlock(&dq_state_lock);
877 spin_unlock(&dq_list_lock);
878 dquot = ERR_PTR(-ESRCH);
881 spin_unlock(&dq_state_lock);
883 dquot = find_dquot(hashent, sb, qid);
886 spin_unlock(&dq_list_lock);
887 empty = get_empty_dquot(sb, qid.type);
889 schedule(); /* Try to wait for a moment... */
895 /* all dquots go on the inuse_list */
897 /* hash it first so it can be found */
898 insert_dquot_hash(dquot);
899 spin_unlock(&dq_list_lock);
900 dqstats_inc(DQST_LOOKUPS);
902 if (!atomic_read(&dquot->dq_count))
903 remove_free_dquot(dquot);
904 atomic_inc(&dquot->dq_count);
905 spin_unlock(&dq_list_lock);
906 dqstats_inc(DQST_CACHE_HITS);
907 dqstats_inc(DQST_LOOKUPS);
909 /* Wait for dq_lock - after this we know that either dquot_release() is
910 * already finished or it will be canceled due to dq_count > 1 test */
911 wait_on_dquot(dquot);
912 /* Read the dquot / allocate space in quota file */
913 if (!test_bit(DQ_ACTIVE_B, &dquot->dq_flags)) {
916 err = sb->dq_op->acquire_dquot(dquot);
919 dquot = ERR_PTR(err);
924 * Make sure following reads see filled structure - paired with
925 * smp_mb__before_atomic() in dquot_acquire().
928 #ifdef CONFIG_QUOTA_DEBUG
929 BUG_ON(!dquot->dq_sb); /* Has somebody invalidated entry under us? */
933 do_destroy_dquot(empty);
937 EXPORT_SYMBOL(dqget);
939 static inline struct dquot **i_dquot(struct inode *inode)
941 return inode->i_sb->s_op->get_dquots(inode);
944 static int dqinit_needed(struct inode *inode, int type)
946 struct dquot * const *dquots;
949 if (IS_NOQUOTA(inode))
952 dquots = i_dquot(inode);
954 return !dquots[type];
955 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
961 /* This routine is guarded by s_umount semaphore */
962 static int add_dquot_ref(struct super_block *sb, int type)
964 struct inode *inode, *old_inode = NULL;
965 #ifdef CONFIG_QUOTA_DEBUG
970 spin_lock(&sb->s_inode_list_lock);
971 list_for_each_entry(inode, &sb->s_inodes, i_sb_list) {
972 spin_lock(&inode->i_lock);
973 if ((inode->i_state & (I_FREEING|I_WILL_FREE|I_NEW)) ||
974 !atomic_read(&inode->i_writecount) ||
975 !dqinit_needed(inode, type)) {
976 spin_unlock(&inode->i_lock);
980 spin_unlock(&inode->i_lock);
981 spin_unlock(&sb->s_inode_list_lock);
983 #ifdef CONFIG_QUOTA_DEBUG
984 if (unlikely(inode_get_rsv_space(inode) > 0))
988 err = __dquot_initialize(inode, type);
995 * We hold a reference to 'inode' so it couldn't have been
996 * removed from s_inodes list while we dropped the
997 * s_inode_list_lock. We cannot iput the inode now as we can be
998 * holding the last reference and we cannot iput it under
999 * s_inode_list_lock. So we keep the reference and iput it
1004 spin_lock(&sb->s_inode_list_lock);
1006 spin_unlock(&sb->s_inode_list_lock);
1009 #ifdef CONFIG_QUOTA_DEBUG
1011 quota_error(sb, "Writes happened before quota was turned on "
1012 "thus quota information is probably inconsistent. "
1013 "Please run quotacheck(8)");
1020 * Remove references to dquots from inode and add dquot to list for freeing
1021 * if we have the last reference to dquot
1023 static void remove_inode_dquot_ref(struct inode *inode, int type,
1024 struct list_head *tofree_head)
1026 struct dquot **dquots = i_dquot(inode);
1027 struct dquot *dquot = dquots[type];
1032 dquots[type] = NULL;
1033 if (list_empty(&dquot->dq_free)) {
1035 * The inode still has reference to dquot so it can't be in the
1038 spin_lock(&dq_list_lock);
1039 list_add(&dquot->dq_free, tofree_head);
1040 spin_unlock(&dq_list_lock);
1043 * Dquot is already in a list to put so we won't drop the last
1051 * Free list of dquots
1052 * Dquots are removed from inodes and no new references can be got so we are
1053 * the only ones holding reference
1055 static void put_dquot_list(struct list_head *tofree_head)
1057 struct list_head *act_head;
1058 struct dquot *dquot;
1060 act_head = tofree_head->next;
1061 while (act_head != tofree_head) {
1062 dquot = list_entry(act_head, struct dquot, dq_free);
1063 act_head = act_head->next;
1064 /* Remove dquot from the list so we won't have problems... */
1065 list_del_init(&dquot->dq_free);
1070 static void remove_dquot_ref(struct super_block *sb, int type,
1071 struct list_head *tofree_head)
1073 struct inode *inode;
1074 #ifdef CONFIG_QUOTA_DEBUG
1078 spin_lock(&sb->s_inode_list_lock);
1079 list_for_each_entry(inode, &sb->s_inodes, i_sb_list) {
1081 * We have to scan also I_NEW inodes because they can already
1082 * have quota pointer initialized. Luckily, we need to touch
1083 * only quota pointers and these have separate locking
1086 spin_lock(&dq_data_lock);
1087 if (!IS_NOQUOTA(inode)) {
1088 #ifdef CONFIG_QUOTA_DEBUG
1089 if (unlikely(inode_get_rsv_space(inode) > 0))
1092 remove_inode_dquot_ref(inode, type, tofree_head);
1094 spin_unlock(&dq_data_lock);
1096 spin_unlock(&sb->s_inode_list_lock);
1097 #ifdef CONFIG_QUOTA_DEBUG
1099 printk(KERN_WARNING "VFS (%s): Writes happened after quota"
1100 " was disabled thus quota information is probably "
1101 "inconsistent. Please run quotacheck(8).\n", sb->s_id);
1106 /* Gather all references from inodes and drop them */
1107 static void drop_dquot_ref(struct super_block *sb, int type)
1109 LIST_HEAD(tofree_head);
1112 remove_dquot_ref(sb, type, &tofree_head);
1113 synchronize_srcu(&dquot_srcu);
1114 put_dquot_list(&tofree_head);
1119 void dquot_free_reserved_space(struct dquot *dquot, qsize_t number)
1121 if (dquot->dq_dqb.dqb_rsvspace >= number)
1122 dquot->dq_dqb.dqb_rsvspace -= number;
1125 dquot->dq_dqb.dqb_rsvspace = 0;
1127 if (dquot->dq_dqb.dqb_curspace + dquot->dq_dqb.dqb_rsvspace <=
1128 dquot->dq_dqb.dqb_bsoftlimit)
1129 dquot->dq_dqb.dqb_btime = (time64_t) 0;
1130 clear_bit(DQ_BLKS_B, &dquot->dq_flags);
1133 static void dquot_decr_inodes(struct dquot *dquot, qsize_t number)
1135 if (sb_dqopt(dquot->dq_sb)->flags & DQUOT_NEGATIVE_USAGE ||
1136 dquot->dq_dqb.dqb_curinodes >= number)
1137 dquot->dq_dqb.dqb_curinodes -= number;
1139 dquot->dq_dqb.dqb_curinodes = 0;
1140 if (dquot->dq_dqb.dqb_curinodes <= dquot->dq_dqb.dqb_isoftlimit)
1141 dquot->dq_dqb.dqb_itime = (time64_t) 0;
1142 clear_bit(DQ_INODES_B, &dquot->dq_flags);
1145 static void dquot_decr_space(struct dquot *dquot, qsize_t number)
1147 if (sb_dqopt(dquot->dq_sb)->flags & DQUOT_NEGATIVE_USAGE ||
1148 dquot->dq_dqb.dqb_curspace >= number)
1149 dquot->dq_dqb.dqb_curspace -= number;
1151 dquot->dq_dqb.dqb_curspace = 0;
1152 if (dquot->dq_dqb.dqb_curspace + dquot->dq_dqb.dqb_rsvspace <=
1153 dquot->dq_dqb.dqb_bsoftlimit)
1154 dquot->dq_dqb.dqb_btime = (time64_t) 0;
1155 clear_bit(DQ_BLKS_B, &dquot->dq_flags);
1159 struct super_block *w_sb;
1160 struct kqid w_dq_id;
1164 static int warning_issued(struct dquot *dquot, const int warntype)
1166 int flag = (warntype == QUOTA_NL_BHARDWARN ||
1167 warntype == QUOTA_NL_BSOFTLONGWARN) ? DQ_BLKS_B :
1168 ((warntype == QUOTA_NL_IHARDWARN ||
1169 warntype == QUOTA_NL_ISOFTLONGWARN) ? DQ_INODES_B : 0);
1173 return test_and_set_bit(flag, &dquot->dq_flags);
1176 #ifdef CONFIG_PRINT_QUOTA_WARNING
1177 static int flag_print_warnings = 1;
1179 static int need_print_warning(struct dquot_warn *warn)
1181 if (!flag_print_warnings)
1184 switch (warn->w_dq_id.type) {
1186 return uid_eq(current_fsuid(), warn->w_dq_id.uid);
1188 return in_group_p(warn->w_dq_id.gid);
1195 /* Print warning to user which exceeded quota */
1196 static void print_warning(struct dquot_warn *warn)
1199 struct tty_struct *tty;
1200 int warntype = warn->w_type;
1202 if (warntype == QUOTA_NL_IHARDBELOW ||
1203 warntype == QUOTA_NL_ISOFTBELOW ||
1204 warntype == QUOTA_NL_BHARDBELOW ||
1205 warntype == QUOTA_NL_BSOFTBELOW || !need_print_warning(warn))
1208 tty = get_current_tty();
1211 tty_write_message(tty, warn->w_sb->s_id);
1212 if (warntype == QUOTA_NL_ISOFTWARN || warntype == QUOTA_NL_BSOFTWARN)
1213 tty_write_message(tty, ": warning, ");
1215 tty_write_message(tty, ": write failed, ");
1216 tty_write_message(tty, quotatypes[warn->w_dq_id.type]);
1218 case QUOTA_NL_IHARDWARN:
1219 msg = " file limit reached.\r\n";
1221 case QUOTA_NL_ISOFTLONGWARN:
1222 msg = " file quota exceeded too long.\r\n";
1224 case QUOTA_NL_ISOFTWARN:
1225 msg = " file quota exceeded.\r\n";
1227 case QUOTA_NL_BHARDWARN:
1228 msg = " block limit reached.\r\n";
1230 case QUOTA_NL_BSOFTLONGWARN:
1231 msg = " block quota exceeded too long.\r\n";
1233 case QUOTA_NL_BSOFTWARN:
1234 msg = " block quota exceeded.\r\n";
1237 tty_write_message(tty, msg);
1242 static void prepare_warning(struct dquot_warn *warn, struct dquot *dquot,
1245 if (warning_issued(dquot, warntype))
1247 warn->w_type = warntype;
1248 warn->w_sb = dquot->dq_sb;
1249 warn->w_dq_id = dquot->dq_id;
1253 * Write warnings to the console and send warning messages over netlink.
1255 * Note that this function can call into tty and networking code.
1257 static void flush_warnings(struct dquot_warn *warn)
1261 for (i = 0; i < MAXQUOTAS; i++) {
1262 if (warn[i].w_type == QUOTA_NL_NOWARN)
1264 #ifdef CONFIG_PRINT_QUOTA_WARNING
1265 print_warning(&warn[i]);
1267 quota_send_warning(warn[i].w_dq_id,
1268 warn[i].w_sb->s_dev, warn[i].w_type);
1272 static int ignore_hardlimit(struct dquot *dquot)
1274 struct mem_dqinfo *info = &sb_dqopt(dquot->dq_sb)->info[dquot->dq_id.type];
1276 return capable(CAP_SYS_RESOURCE) &&
1277 (info->dqi_format->qf_fmt_id != QFMT_VFS_OLD ||
1278 !(info->dqi_flags & DQF_ROOT_SQUASH));
1281 static int dquot_add_inodes(struct dquot *dquot, qsize_t inodes,
1282 struct dquot_warn *warn)
1287 spin_lock(&dquot->dq_dqb_lock);
1288 newinodes = dquot->dq_dqb.dqb_curinodes + inodes;
1289 if (!sb_has_quota_limits_enabled(dquot->dq_sb, dquot->dq_id.type) ||
1290 test_bit(DQ_FAKE_B, &dquot->dq_flags))
1293 if (dquot->dq_dqb.dqb_ihardlimit &&
1294 newinodes > dquot->dq_dqb.dqb_ihardlimit &&
1295 !ignore_hardlimit(dquot)) {
1296 prepare_warning(warn, dquot, QUOTA_NL_IHARDWARN);
1301 if (dquot->dq_dqb.dqb_isoftlimit &&
1302 newinodes > dquot->dq_dqb.dqb_isoftlimit &&
1303 dquot->dq_dqb.dqb_itime &&
1304 ktime_get_real_seconds() >= dquot->dq_dqb.dqb_itime &&
1305 !ignore_hardlimit(dquot)) {
1306 prepare_warning(warn, dquot, QUOTA_NL_ISOFTLONGWARN);
1311 if (dquot->dq_dqb.dqb_isoftlimit &&
1312 newinodes > dquot->dq_dqb.dqb_isoftlimit &&
1313 dquot->dq_dqb.dqb_itime == 0) {
1314 prepare_warning(warn, dquot, QUOTA_NL_ISOFTWARN);
1315 dquot->dq_dqb.dqb_itime = ktime_get_real_seconds() +
1316 sb_dqopt(dquot->dq_sb)->info[dquot->dq_id.type].dqi_igrace;
1319 dquot->dq_dqb.dqb_curinodes = newinodes;
1322 spin_unlock(&dquot->dq_dqb_lock);
1326 static int dquot_add_space(struct dquot *dquot, qsize_t space,
1327 qsize_t rsv_space, unsigned int flags,
1328 struct dquot_warn *warn)
1331 struct super_block *sb = dquot->dq_sb;
1334 spin_lock(&dquot->dq_dqb_lock);
1335 if (!sb_has_quota_limits_enabled(sb, dquot->dq_id.type) ||
1336 test_bit(DQ_FAKE_B, &dquot->dq_flags))
1339 tspace = dquot->dq_dqb.dqb_curspace + dquot->dq_dqb.dqb_rsvspace
1340 + space + rsv_space;
1342 if (dquot->dq_dqb.dqb_bhardlimit &&
1343 tspace > dquot->dq_dqb.dqb_bhardlimit &&
1344 !ignore_hardlimit(dquot)) {
1345 if (flags & DQUOT_SPACE_WARN)
1346 prepare_warning(warn, dquot, QUOTA_NL_BHARDWARN);
1351 if (dquot->dq_dqb.dqb_bsoftlimit &&
1352 tspace > dquot->dq_dqb.dqb_bsoftlimit &&
1353 dquot->dq_dqb.dqb_btime &&
1354 ktime_get_real_seconds() >= dquot->dq_dqb.dqb_btime &&
1355 !ignore_hardlimit(dquot)) {
1356 if (flags & DQUOT_SPACE_WARN)
1357 prepare_warning(warn, dquot, QUOTA_NL_BSOFTLONGWARN);
1362 if (dquot->dq_dqb.dqb_bsoftlimit &&
1363 tspace > dquot->dq_dqb.dqb_bsoftlimit &&
1364 dquot->dq_dqb.dqb_btime == 0) {
1365 if (flags & DQUOT_SPACE_WARN) {
1366 prepare_warning(warn, dquot, QUOTA_NL_BSOFTWARN);
1367 dquot->dq_dqb.dqb_btime = ktime_get_real_seconds() +
1368 sb_dqopt(sb)->info[dquot->dq_id.type].dqi_bgrace;
1371 * We don't allow preallocation to exceed softlimit so exceeding will
1380 * We have to be careful and go through warning generation & grace time
1381 * setting even if DQUOT_SPACE_NOFAIL is set. That's why we check it
1384 if (flags & DQUOT_SPACE_NOFAIL)
1387 dquot->dq_dqb.dqb_rsvspace += rsv_space;
1388 dquot->dq_dqb.dqb_curspace += space;
1390 spin_unlock(&dquot->dq_dqb_lock);
1394 static int info_idq_free(struct dquot *dquot, qsize_t inodes)
1398 if (test_bit(DQ_FAKE_B, &dquot->dq_flags) ||
1399 dquot->dq_dqb.dqb_curinodes <= dquot->dq_dqb.dqb_isoftlimit ||
1400 !sb_has_quota_limits_enabled(dquot->dq_sb, dquot->dq_id.type))
1401 return QUOTA_NL_NOWARN;
1403 newinodes = dquot->dq_dqb.dqb_curinodes - inodes;
1404 if (newinodes <= dquot->dq_dqb.dqb_isoftlimit)
1405 return QUOTA_NL_ISOFTBELOW;
1406 if (dquot->dq_dqb.dqb_curinodes >= dquot->dq_dqb.dqb_ihardlimit &&
1407 newinodes < dquot->dq_dqb.dqb_ihardlimit)
1408 return QUOTA_NL_IHARDBELOW;
1409 return QUOTA_NL_NOWARN;
1412 static int info_bdq_free(struct dquot *dquot, qsize_t space)
1416 tspace = dquot->dq_dqb.dqb_curspace + dquot->dq_dqb.dqb_rsvspace;
1418 if (test_bit(DQ_FAKE_B, &dquot->dq_flags) ||
1419 tspace <= dquot->dq_dqb.dqb_bsoftlimit)
1420 return QUOTA_NL_NOWARN;
1422 if (tspace - space <= dquot->dq_dqb.dqb_bsoftlimit)
1423 return QUOTA_NL_BSOFTBELOW;
1424 if (tspace >= dquot->dq_dqb.dqb_bhardlimit &&
1425 tspace - space < dquot->dq_dqb.dqb_bhardlimit)
1426 return QUOTA_NL_BHARDBELOW;
1427 return QUOTA_NL_NOWARN;
1430 static int dquot_active(const struct inode *inode)
1432 struct super_block *sb = inode->i_sb;
1434 if (IS_NOQUOTA(inode))
1436 return sb_any_quota_loaded(sb) & ~sb_any_quota_suspended(sb);
1440 * Initialize quota pointers in inode
1442 * It is better to call this function outside of any transaction as it
1443 * might need a lot of space in journal for dquot structure allocation.
1445 static int __dquot_initialize(struct inode *inode, int type)
1447 int cnt, init_needed = 0;
1448 struct dquot **dquots, *got[MAXQUOTAS] = {};
1449 struct super_block *sb = inode->i_sb;
1453 if (!dquot_active(inode))
1456 dquots = i_dquot(inode);
1458 /* First get references to structures we might need. */
1459 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1463 struct dquot *dquot;
1465 if (type != -1 && cnt != type)
1468 * The i_dquot should have been initialized in most cases,
1469 * we check it without locking here to avoid unnecessary
1470 * dqget()/dqput() calls.
1475 if (!sb_has_quota_active(sb, cnt))
1482 qid = make_kqid_uid(inode->i_uid);
1485 qid = make_kqid_gid(inode->i_gid);
1488 rc = inode->i_sb->dq_op->get_projid(inode, &projid);
1491 qid = make_kqid_projid(projid);
1494 dquot = dqget(sb, qid);
1495 if (IS_ERR(dquot)) {
1496 /* We raced with somebody turning quotas off... */
1497 if (PTR_ERR(dquot) != -ESRCH) {
1498 ret = PTR_ERR(dquot);
1506 /* All required i_dquot has been initialized */
1510 spin_lock(&dq_data_lock);
1511 if (IS_NOQUOTA(inode))
1513 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1514 if (type != -1 && cnt != type)
1516 /* Avoid races with quotaoff() */
1517 if (!sb_has_quota_active(sb, cnt))
1519 /* We could race with quotaon or dqget() could have failed */
1523 dquots[cnt] = got[cnt];
1526 * Make quota reservation system happy if someone
1527 * did a write before quota was turned on
1529 rsv = inode_get_rsv_space(inode);
1530 if (unlikely(rsv)) {
1531 spin_lock(&inode->i_lock);
1532 /* Get reservation again under proper lock */
1533 rsv = __inode_get_rsv_space(inode);
1534 spin_lock(&dquots[cnt]->dq_dqb_lock);
1535 dquots[cnt]->dq_dqb.dqb_rsvspace += rsv;
1536 spin_unlock(&dquots[cnt]->dq_dqb_lock);
1537 spin_unlock(&inode->i_lock);
1542 spin_unlock(&dq_data_lock);
1544 /* Drop unused references */
1550 int dquot_initialize(struct inode *inode)
1552 return __dquot_initialize(inode, -1);
1554 EXPORT_SYMBOL(dquot_initialize);
1556 bool dquot_initialize_needed(struct inode *inode)
1558 struct dquot **dquots;
1561 if (!dquot_active(inode))
1564 dquots = i_dquot(inode);
1565 for (i = 0; i < MAXQUOTAS; i++)
1566 if (!dquots[i] && sb_has_quota_active(inode->i_sb, i))
1570 EXPORT_SYMBOL(dquot_initialize_needed);
1573 * Release all quotas referenced by inode.
1575 * This function only be called on inode free or converting
1576 * a file to quota file, no other users for the i_dquot in
1577 * both cases, so we needn't call synchronize_srcu() after
1580 static void __dquot_drop(struct inode *inode)
1583 struct dquot **dquots = i_dquot(inode);
1584 struct dquot *put[MAXQUOTAS];
1586 spin_lock(&dq_data_lock);
1587 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1588 put[cnt] = dquots[cnt];
1591 spin_unlock(&dq_data_lock);
1595 void dquot_drop(struct inode *inode)
1597 struct dquot * const *dquots;
1600 if (IS_NOQUOTA(inode))
1604 * Test before calling to rule out calls from proc and such
1605 * where we are not allowed to block. Note that this is
1606 * actually reliable test even without the lock - the caller
1607 * must assure that nobody can come after the DQUOT_DROP and
1608 * add quota pointers back anyway.
1610 dquots = i_dquot(inode);
1611 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1616 if (cnt < MAXQUOTAS)
1617 __dquot_drop(inode);
1619 EXPORT_SYMBOL(dquot_drop);
1622 * inode_reserved_space is managed internally by quota, and protected by
1623 * i_lock similar to i_blocks+i_bytes.
1625 static qsize_t *inode_reserved_space(struct inode * inode)
1627 /* Filesystem must explicitly define it's own method in order to use
1628 * quota reservation interface */
1629 BUG_ON(!inode->i_sb->dq_op->get_reserved_space);
1630 return inode->i_sb->dq_op->get_reserved_space(inode);
1633 static qsize_t __inode_get_rsv_space(struct inode *inode)
1635 if (!inode->i_sb->dq_op->get_reserved_space)
1637 return *inode_reserved_space(inode);
1640 static qsize_t inode_get_rsv_space(struct inode *inode)
1644 if (!inode->i_sb->dq_op->get_reserved_space)
1646 spin_lock(&inode->i_lock);
1647 ret = __inode_get_rsv_space(inode);
1648 spin_unlock(&inode->i_lock);
1653 * This functions updates i_blocks+i_bytes fields and quota information
1654 * (together with appropriate checks).
1656 * NOTE: We absolutely rely on the fact that caller dirties the inode
1657 * (usually helpers in quotaops.h care about this) and holds a handle for
1658 * the current transaction so that dquot write and inode write go into the
1663 * This operation can block, but only after everything is updated
1665 int __dquot_alloc_space(struct inode *inode, qsize_t number, int flags)
1667 int cnt, ret = 0, index;
1668 struct dquot_warn warn[MAXQUOTAS];
1669 int reserve = flags & DQUOT_SPACE_RESERVE;
1670 struct dquot **dquots;
1672 if (!dquot_active(inode)) {
1674 spin_lock(&inode->i_lock);
1675 *inode_reserved_space(inode) += number;
1676 spin_unlock(&inode->i_lock);
1678 inode_add_bytes(inode, number);
1683 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1684 warn[cnt].w_type = QUOTA_NL_NOWARN;
1686 dquots = i_dquot(inode);
1687 index = srcu_read_lock(&dquot_srcu);
1688 spin_lock(&inode->i_lock);
1689 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1693 ret = dquot_add_space(dquots[cnt], 0, number, flags,
1696 ret = dquot_add_space(dquots[cnt], number, 0, flags,
1700 /* Back out changes we already did */
1701 for (cnt--; cnt >= 0; cnt--) {
1704 spin_lock(&dquots[cnt]->dq_dqb_lock);
1706 dquot_free_reserved_space(dquots[cnt],
1709 dquot_decr_space(dquots[cnt], number);
1710 spin_unlock(&dquots[cnt]->dq_dqb_lock);
1712 spin_unlock(&inode->i_lock);
1713 goto out_flush_warn;
1717 *inode_reserved_space(inode) += number;
1719 __inode_add_bytes(inode, number);
1720 spin_unlock(&inode->i_lock);
1723 goto out_flush_warn;
1724 mark_all_dquot_dirty(dquots);
1726 srcu_read_unlock(&dquot_srcu, index);
1727 flush_warnings(warn);
1731 EXPORT_SYMBOL(__dquot_alloc_space);
1734 * This operation can block, but only after everything is updated
1736 int dquot_alloc_inode(struct inode *inode)
1738 int cnt, ret = 0, index;
1739 struct dquot_warn warn[MAXQUOTAS];
1740 struct dquot * const *dquots;
1742 if (!dquot_active(inode))
1744 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1745 warn[cnt].w_type = QUOTA_NL_NOWARN;
1747 dquots = i_dquot(inode);
1748 index = srcu_read_lock(&dquot_srcu);
1749 spin_lock(&inode->i_lock);
1750 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1753 ret = dquot_add_inodes(dquots[cnt], 1, &warn[cnt]);
1755 for (cnt--; cnt >= 0; cnt--) {
1758 /* Back out changes we already did */
1759 spin_lock(&dquots[cnt]->dq_dqb_lock);
1760 dquot_decr_inodes(dquots[cnt], 1);
1761 spin_unlock(&dquots[cnt]->dq_dqb_lock);
1768 spin_unlock(&inode->i_lock);
1770 mark_all_dquot_dirty(dquots);
1771 srcu_read_unlock(&dquot_srcu, index);
1772 flush_warnings(warn);
1775 EXPORT_SYMBOL(dquot_alloc_inode);
1778 * Convert in-memory reserved quotas to real consumed quotas
1780 int dquot_claim_space_nodirty(struct inode *inode, qsize_t number)
1782 struct dquot **dquots;
1785 if (!dquot_active(inode)) {
1786 spin_lock(&inode->i_lock);
1787 *inode_reserved_space(inode) -= number;
1788 __inode_add_bytes(inode, number);
1789 spin_unlock(&inode->i_lock);
1793 dquots = i_dquot(inode);
1794 index = srcu_read_lock(&dquot_srcu);
1795 spin_lock(&inode->i_lock);
1796 /* Claim reserved quotas to allocated quotas */
1797 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1799 struct dquot *dquot = dquots[cnt];
1801 spin_lock(&dquot->dq_dqb_lock);
1802 if (WARN_ON_ONCE(dquot->dq_dqb.dqb_rsvspace < number))
1803 number = dquot->dq_dqb.dqb_rsvspace;
1804 dquot->dq_dqb.dqb_curspace += number;
1805 dquot->dq_dqb.dqb_rsvspace -= number;
1806 spin_unlock(&dquot->dq_dqb_lock);
1809 /* Update inode bytes */
1810 *inode_reserved_space(inode) -= number;
1811 __inode_add_bytes(inode, number);
1812 spin_unlock(&inode->i_lock);
1813 mark_all_dquot_dirty(dquots);
1814 srcu_read_unlock(&dquot_srcu, index);
1817 EXPORT_SYMBOL(dquot_claim_space_nodirty);
1820 * Convert allocated space back to in-memory reserved quotas
1822 void dquot_reclaim_space_nodirty(struct inode *inode, qsize_t number)
1824 struct dquot **dquots;
1827 if (!dquot_active(inode)) {
1828 spin_lock(&inode->i_lock);
1829 *inode_reserved_space(inode) += number;
1830 __inode_sub_bytes(inode, number);
1831 spin_unlock(&inode->i_lock);
1835 dquots = i_dquot(inode);
1836 index = srcu_read_lock(&dquot_srcu);
1837 spin_lock(&inode->i_lock);
1838 /* Claim reserved quotas to allocated quotas */
1839 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1841 struct dquot *dquot = dquots[cnt];
1843 spin_lock(&dquot->dq_dqb_lock);
1844 if (WARN_ON_ONCE(dquot->dq_dqb.dqb_curspace < number))
1845 number = dquot->dq_dqb.dqb_curspace;
1846 dquot->dq_dqb.dqb_rsvspace += number;
1847 dquot->dq_dqb.dqb_curspace -= number;
1848 spin_unlock(&dquot->dq_dqb_lock);
1851 /* Update inode bytes */
1852 *inode_reserved_space(inode) += number;
1853 __inode_sub_bytes(inode, number);
1854 spin_unlock(&inode->i_lock);
1855 mark_all_dquot_dirty(dquots);
1856 srcu_read_unlock(&dquot_srcu, index);
1859 EXPORT_SYMBOL(dquot_reclaim_space_nodirty);
1862 * This operation can block, but only after everything is updated
1864 void __dquot_free_space(struct inode *inode, qsize_t number, int flags)
1867 struct dquot_warn warn[MAXQUOTAS];
1868 struct dquot **dquots;
1869 int reserve = flags & DQUOT_SPACE_RESERVE, index;
1871 if (!dquot_active(inode)) {
1873 spin_lock(&inode->i_lock);
1874 *inode_reserved_space(inode) -= number;
1875 spin_unlock(&inode->i_lock);
1877 inode_sub_bytes(inode, number);
1882 dquots = i_dquot(inode);
1883 index = srcu_read_lock(&dquot_srcu);
1884 spin_lock(&inode->i_lock);
1885 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1888 warn[cnt].w_type = QUOTA_NL_NOWARN;
1891 spin_lock(&dquots[cnt]->dq_dqb_lock);
1892 wtype = info_bdq_free(dquots[cnt], number);
1893 if (wtype != QUOTA_NL_NOWARN)
1894 prepare_warning(&warn[cnt], dquots[cnt], wtype);
1896 dquot_free_reserved_space(dquots[cnt], number);
1898 dquot_decr_space(dquots[cnt], number);
1899 spin_unlock(&dquots[cnt]->dq_dqb_lock);
1902 *inode_reserved_space(inode) -= number;
1904 __inode_sub_bytes(inode, number);
1905 spin_unlock(&inode->i_lock);
1909 mark_all_dquot_dirty(dquots);
1911 srcu_read_unlock(&dquot_srcu, index);
1912 flush_warnings(warn);
1914 EXPORT_SYMBOL(__dquot_free_space);
1917 * This operation can block, but only after everything is updated
1919 void dquot_free_inode(struct inode *inode)
1922 struct dquot_warn warn[MAXQUOTAS];
1923 struct dquot * const *dquots;
1926 if (!dquot_active(inode))
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_idq_free(dquots[cnt], 1);
1940 if (wtype != QUOTA_NL_NOWARN)
1941 prepare_warning(&warn[cnt], dquots[cnt], wtype);
1942 dquot_decr_inodes(dquots[cnt], 1);
1943 spin_unlock(&dquots[cnt]->dq_dqb_lock);
1945 spin_unlock(&inode->i_lock);
1946 mark_all_dquot_dirty(dquots);
1947 srcu_read_unlock(&dquot_srcu, index);
1948 flush_warnings(warn);
1950 EXPORT_SYMBOL(dquot_free_inode);
1953 * Transfer the number of inode and blocks from one diskquota to an other.
1954 * On success, dquot references in transfer_to are consumed and references
1955 * to original dquots that need to be released are placed there. On failure,
1956 * references are kept untouched.
1958 * This operation can block, but only after everything is updated
1959 * A transaction must be started when entering this function.
1961 * We are holding reference on transfer_from & transfer_to, no need to
1962 * protect them by srcu_read_lock().
1964 int __dquot_transfer(struct inode *inode, struct dquot **transfer_to)
1967 qsize_t rsv_space = 0;
1968 qsize_t inode_usage = 1;
1969 struct dquot *transfer_from[MAXQUOTAS] = {};
1971 char is_valid[MAXQUOTAS] = {};
1972 struct dquot_warn warn_to[MAXQUOTAS];
1973 struct dquot_warn warn_from_inodes[MAXQUOTAS];
1974 struct dquot_warn warn_from_space[MAXQUOTAS];
1976 if (IS_NOQUOTA(inode))
1979 if (inode->i_sb->dq_op->get_inode_usage) {
1980 ret = inode->i_sb->dq_op->get_inode_usage(inode, &inode_usage);
1985 /* Initialize the arrays */
1986 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1987 warn_to[cnt].w_type = QUOTA_NL_NOWARN;
1988 warn_from_inodes[cnt].w_type = QUOTA_NL_NOWARN;
1989 warn_from_space[cnt].w_type = QUOTA_NL_NOWARN;
1992 spin_lock(&dq_data_lock);
1993 spin_lock(&inode->i_lock);
1994 if (IS_NOQUOTA(inode)) { /* File without quota accounting? */
1995 spin_unlock(&inode->i_lock);
1996 spin_unlock(&dq_data_lock);
1999 cur_space = __inode_get_bytes(inode);
2000 rsv_space = __inode_get_rsv_space(inode);
2002 * Build the transfer_from list, check limits, and update usage in
2003 * the target structures.
2005 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
2007 * Skip changes for same uid or gid or for turned off quota-type.
2009 if (!transfer_to[cnt])
2011 /* Avoid races with quotaoff() */
2012 if (!sb_has_quota_active(inode->i_sb, cnt))
2015 transfer_from[cnt] = i_dquot(inode)[cnt];
2016 ret = dquot_add_inodes(transfer_to[cnt], inode_usage,
2020 ret = dquot_add_space(transfer_to[cnt], cur_space, rsv_space,
2021 DQUOT_SPACE_WARN, &warn_to[cnt]);
2023 spin_lock(&transfer_to[cnt]->dq_dqb_lock);
2024 dquot_decr_inodes(transfer_to[cnt], inode_usage);
2025 spin_unlock(&transfer_to[cnt]->dq_dqb_lock);
2030 /* Decrease usage for source structures and update quota pointers */
2031 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
2034 /* Due to IO error we might not have transfer_from[] structure */
2035 if (transfer_from[cnt]) {
2038 spin_lock(&transfer_from[cnt]->dq_dqb_lock);
2039 wtype = info_idq_free(transfer_from[cnt], inode_usage);
2040 if (wtype != QUOTA_NL_NOWARN)
2041 prepare_warning(&warn_from_inodes[cnt],
2042 transfer_from[cnt], wtype);
2043 wtype = info_bdq_free(transfer_from[cnt],
2044 cur_space + rsv_space);
2045 if (wtype != QUOTA_NL_NOWARN)
2046 prepare_warning(&warn_from_space[cnt],
2047 transfer_from[cnt], wtype);
2048 dquot_decr_inodes(transfer_from[cnt], inode_usage);
2049 dquot_decr_space(transfer_from[cnt], cur_space);
2050 dquot_free_reserved_space(transfer_from[cnt],
2052 spin_unlock(&transfer_from[cnt]->dq_dqb_lock);
2054 i_dquot(inode)[cnt] = transfer_to[cnt];
2056 spin_unlock(&inode->i_lock);
2057 spin_unlock(&dq_data_lock);
2059 mark_all_dquot_dirty(transfer_from);
2060 mark_all_dquot_dirty(transfer_to);
2061 flush_warnings(warn_to);
2062 flush_warnings(warn_from_inodes);
2063 flush_warnings(warn_from_space);
2064 /* Pass back references to put */
2065 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
2067 transfer_to[cnt] = transfer_from[cnt];
2070 /* Back out changes we already did */
2071 for (cnt--; cnt >= 0; cnt--) {
2074 spin_lock(&transfer_to[cnt]->dq_dqb_lock);
2075 dquot_decr_inodes(transfer_to[cnt], inode_usage);
2076 dquot_decr_space(transfer_to[cnt], cur_space);
2077 dquot_free_reserved_space(transfer_to[cnt], rsv_space);
2078 spin_unlock(&transfer_to[cnt]->dq_dqb_lock);
2080 spin_unlock(&inode->i_lock);
2081 spin_unlock(&dq_data_lock);
2082 flush_warnings(warn_to);
2085 EXPORT_SYMBOL(__dquot_transfer);
2087 /* Wrapper for transferring ownership of an inode for uid/gid only
2088 * Called from FSXXX_setattr()
2090 int dquot_transfer(struct inode *inode, struct iattr *iattr)
2092 struct dquot *transfer_to[MAXQUOTAS] = {};
2093 struct dquot *dquot;
2094 struct super_block *sb = inode->i_sb;
2097 if (!dquot_active(inode))
2100 if (iattr->ia_valid & ATTR_UID && !uid_eq(iattr->ia_uid, inode->i_uid)){
2101 dquot = dqget(sb, make_kqid_uid(iattr->ia_uid));
2102 if (IS_ERR(dquot)) {
2103 if (PTR_ERR(dquot) != -ESRCH) {
2104 ret = PTR_ERR(dquot);
2109 transfer_to[USRQUOTA] = dquot;
2111 if (iattr->ia_valid & ATTR_GID && !gid_eq(iattr->ia_gid, inode->i_gid)){
2112 dquot = dqget(sb, make_kqid_gid(iattr->ia_gid));
2113 if (IS_ERR(dquot)) {
2114 if (PTR_ERR(dquot) != -ESRCH) {
2115 ret = PTR_ERR(dquot);
2120 transfer_to[GRPQUOTA] = dquot;
2122 ret = __dquot_transfer(inode, transfer_to);
2124 dqput_all(transfer_to);
2127 EXPORT_SYMBOL(dquot_transfer);
2130 * Write info of quota file to disk
2132 int dquot_commit_info(struct super_block *sb, int type)
2134 struct quota_info *dqopt = sb_dqopt(sb);
2136 return dqopt->ops[type]->write_file_info(sb, type);
2138 EXPORT_SYMBOL(dquot_commit_info);
2140 int dquot_get_next_id(struct super_block *sb, struct kqid *qid)
2142 struct quota_info *dqopt = sb_dqopt(sb);
2144 if (!sb_has_quota_active(sb, qid->type))
2146 if (!dqopt->ops[qid->type]->get_next_id)
2148 return dqopt->ops[qid->type]->get_next_id(sb, qid);
2150 EXPORT_SYMBOL(dquot_get_next_id);
2153 * Definitions of diskquota operations.
2155 const struct dquot_operations dquot_operations = {
2156 .write_dquot = dquot_commit,
2157 .acquire_dquot = dquot_acquire,
2158 .release_dquot = dquot_release,
2159 .mark_dirty = dquot_mark_dquot_dirty,
2160 .write_info = dquot_commit_info,
2161 .alloc_dquot = dquot_alloc,
2162 .destroy_dquot = dquot_destroy,
2163 .get_next_id = dquot_get_next_id,
2165 EXPORT_SYMBOL(dquot_operations);
2168 * Generic helper for ->open on filesystems supporting disk quotas.
2170 int dquot_file_open(struct inode *inode, struct file *file)
2174 error = generic_file_open(inode, file);
2175 if (!error && (file->f_mode & FMODE_WRITE))
2176 error = dquot_initialize(inode);
2179 EXPORT_SYMBOL(dquot_file_open);
2181 static void vfs_cleanup_quota_inode(struct super_block *sb, int type)
2183 struct quota_info *dqopt = sb_dqopt(sb);
2184 struct inode *inode = dqopt->files[type];
2188 if (!(dqopt->flags & DQUOT_QUOTA_SYS_FILE)) {
2190 inode->i_flags &= ~S_NOQUOTA;
2191 inode_unlock(inode);
2193 dqopt->files[type] = NULL;
2198 * Turn quota off on a device. type == -1 ==> quotaoff for all types (umount)
2200 int dquot_disable(struct super_block *sb, int type, unsigned int flags)
2203 struct quota_info *dqopt = sb_dqopt(sb);
2205 /* s_umount should be held in exclusive mode */
2206 if (WARN_ON_ONCE(down_read_trylock(&sb->s_umount)))
2207 up_read(&sb->s_umount);
2209 /* Cannot turn off usage accounting without turning off limits, or
2210 * suspend quotas and simultaneously turn quotas off. */
2211 if ((flags & DQUOT_USAGE_ENABLED && !(flags & DQUOT_LIMITS_ENABLED))
2212 || (flags & DQUOT_SUSPENDED && flags & (DQUOT_LIMITS_ENABLED |
2213 DQUOT_USAGE_ENABLED)))
2217 * Skip everything if there's nothing to do. We have to do this because
2218 * sometimes we are called when fill_super() failed and calling
2219 * sync_fs() in such cases does no good.
2221 if (!sb_any_quota_loaded(sb))
2224 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
2225 if (type != -1 && cnt != type)
2227 if (!sb_has_quota_loaded(sb, cnt))
2230 if (flags & DQUOT_SUSPENDED) {
2231 spin_lock(&dq_state_lock);
2233 dquot_state_flag(DQUOT_SUSPENDED, cnt);
2234 spin_unlock(&dq_state_lock);
2236 spin_lock(&dq_state_lock);
2237 dqopt->flags &= ~dquot_state_flag(flags, cnt);
2238 /* Turning off suspended quotas? */
2239 if (!sb_has_quota_loaded(sb, cnt) &&
2240 sb_has_quota_suspended(sb, cnt)) {
2241 dqopt->flags &= ~dquot_state_flag(
2242 DQUOT_SUSPENDED, cnt);
2243 spin_unlock(&dq_state_lock);
2244 vfs_cleanup_quota_inode(sb, cnt);
2247 spin_unlock(&dq_state_lock);
2250 /* We still have to keep quota loaded? */
2251 if (sb_has_quota_loaded(sb, cnt) && !(flags & DQUOT_SUSPENDED))
2254 /* Note: these are blocking operations */
2255 drop_dquot_ref(sb, cnt);
2256 invalidate_dquots(sb, cnt);
2258 * Now all dquots should be invalidated, all writes done so we
2259 * should be only users of the info. No locks needed.
2261 if (info_dirty(&dqopt->info[cnt]))
2262 sb->dq_op->write_info(sb, cnt);
2263 if (dqopt->ops[cnt]->free_file_info)
2264 dqopt->ops[cnt]->free_file_info(sb, cnt);
2265 put_quota_format(dqopt->info[cnt].dqi_format);
2266 dqopt->info[cnt].dqi_flags = 0;
2267 dqopt->info[cnt].dqi_igrace = 0;
2268 dqopt->info[cnt].dqi_bgrace = 0;
2269 dqopt->ops[cnt] = NULL;
2272 /* Skip syncing and setting flags if quota files are hidden */
2273 if (dqopt->flags & DQUOT_QUOTA_SYS_FILE)
2276 /* Sync the superblock so that buffers with quota data are written to
2277 * disk (and so userspace sees correct data afterwards). */
2278 if (sb->s_op->sync_fs)
2279 sb->s_op->sync_fs(sb, 1);
2280 sync_blockdev(sb->s_bdev);
2281 /* Now the quota files are just ordinary files and we can set the
2282 * inode flags back. Moreover we discard the pagecache so that
2283 * userspace sees the writes we did bypassing the pagecache. We
2284 * must also discard the blockdev buffers so that we see the
2285 * changes done by userspace on the next quotaon() */
2286 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
2287 if (!sb_has_quota_loaded(sb, cnt) && dqopt->files[cnt]) {
2288 inode_lock(dqopt->files[cnt]);
2289 truncate_inode_pages(&dqopt->files[cnt]->i_data, 0);
2290 inode_unlock(dqopt->files[cnt]);
2293 invalidate_bdev(sb->s_bdev);
2295 /* We are done when suspending quotas */
2296 if (flags & DQUOT_SUSPENDED)
2299 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
2300 if (!sb_has_quota_loaded(sb, cnt))
2301 vfs_cleanup_quota_inode(sb, cnt);
2304 EXPORT_SYMBOL(dquot_disable);
2306 int dquot_quota_off(struct super_block *sb, int type)
2308 return dquot_disable(sb, type,
2309 DQUOT_USAGE_ENABLED | DQUOT_LIMITS_ENABLED);
2311 EXPORT_SYMBOL(dquot_quota_off);
2314 * Turn quotas on on a device
2317 static int vfs_setup_quota_inode(struct inode *inode, int type)
2319 struct super_block *sb = inode->i_sb;
2320 struct quota_info *dqopt = sb_dqopt(sb);
2322 if (!S_ISREG(inode->i_mode))
2324 if (IS_RDONLY(inode))
2326 if (sb_has_quota_loaded(sb, type))
2329 dqopt->files[type] = igrab(inode);
2330 if (!dqopt->files[type])
2332 if (!(dqopt->flags & DQUOT_QUOTA_SYS_FILE)) {
2333 /* We don't want quota and atime on quota files (deadlocks
2334 * possible) Also nobody should write to the file - we use
2335 * special IO operations which ignore the immutable bit. */
2337 inode->i_flags |= S_NOQUOTA;
2338 inode_unlock(inode);
2340 * When S_NOQUOTA is set, remove dquot references as no more
2341 * references can be added
2343 __dquot_drop(inode);
2348 int dquot_load_quota_sb(struct super_block *sb, int type, int format_id,
2351 struct quota_format_type *fmt = find_quota_format(format_id);
2352 struct quota_info *dqopt = sb_dqopt(sb);
2355 /* Just unsuspend quotas? */
2356 BUG_ON(flags & DQUOT_SUSPENDED);
2357 /* s_umount should be held in exclusive mode */
2358 if (WARN_ON_ONCE(down_read_trylock(&sb->s_umount)))
2359 up_read(&sb->s_umount);
2363 if (!sb->s_op->quota_write || !sb->s_op->quota_read ||
2364 (type == PRJQUOTA && sb->dq_op->get_projid == NULL)) {
2368 /* Filesystems outside of init_user_ns not yet supported */
2369 if (sb->s_user_ns != &init_user_ns) {
2373 /* Usage always has to be set... */
2374 if (!(flags & DQUOT_USAGE_ENABLED)) {
2378 if (sb_has_quota_loaded(sb, type)) {
2383 if (!(dqopt->flags & DQUOT_QUOTA_SYS_FILE)) {
2384 /* As we bypass the pagecache we must now flush all the
2385 * dirty data and invalidate caches so that kernel sees
2386 * changes from userspace. It is not enough to just flush
2387 * the quota file since if blocksize < pagesize, invalidation
2388 * of the cache could fail because of other unrelated dirty
2390 sync_filesystem(sb);
2391 invalidate_bdev(sb->s_bdev);
2395 if (!fmt->qf_ops->check_quota_file(sb, type))
2398 dqopt->ops[type] = fmt->qf_ops;
2399 dqopt->info[type].dqi_format = fmt;
2400 dqopt->info[type].dqi_fmt_id = format_id;
2401 INIT_LIST_HEAD(&dqopt->info[type].dqi_dirty_list);
2402 error = dqopt->ops[type]->read_file_info(sb, type);
2405 if (dqopt->flags & DQUOT_QUOTA_SYS_FILE) {
2406 spin_lock(&dq_data_lock);
2407 dqopt->info[type].dqi_flags |= DQF_SYS_FILE;
2408 spin_unlock(&dq_data_lock);
2410 spin_lock(&dq_state_lock);
2411 dqopt->flags |= dquot_state_flag(flags, type);
2412 spin_unlock(&dq_state_lock);
2414 error = add_dquot_ref(sb, type);
2416 dquot_disable(sb, type, flags);
2420 put_quota_format(fmt);
2424 EXPORT_SYMBOL(dquot_load_quota_sb);
2427 * More powerful function for turning on quotas on given quota inode allowing
2428 * setting of individual quota flags
2430 int dquot_load_quota_inode(struct inode *inode, int type, int format_id,
2435 err = vfs_setup_quota_inode(inode, type);
2438 err = dquot_load_quota_sb(inode->i_sb, type, format_id, flags);
2440 vfs_cleanup_quota_inode(inode->i_sb, type);
2443 EXPORT_SYMBOL(dquot_load_quota_inode);
2445 /* Reenable quotas on remount RW */
2446 int dquot_resume(struct super_block *sb, int type)
2448 struct quota_info *dqopt = sb_dqopt(sb);
2452 /* s_umount should be held in exclusive mode */
2453 if (WARN_ON_ONCE(down_read_trylock(&sb->s_umount)))
2454 up_read(&sb->s_umount);
2456 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
2457 if (type != -1 && cnt != type)
2459 if (!sb_has_quota_suspended(sb, cnt))
2462 spin_lock(&dq_state_lock);
2463 flags = dqopt->flags & dquot_state_flag(DQUOT_USAGE_ENABLED |
2464 DQUOT_LIMITS_ENABLED,
2466 dqopt->flags &= ~dquot_state_flag(DQUOT_STATE_FLAGS, cnt);
2467 spin_unlock(&dq_state_lock);
2469 flags = dquot_generic_flag(flags, cnt);
2470 ret = dquot_load_quota_sb(sb, cnt, dqopt->info[cnt].dqi_fmt_id,
2473 vfs_cleanup_quota_inode(sb, cnt);
2478 EXPORT_SYMBOL(dquot_resume);
2480 int dquot_quota_on(struct super_block *sb, int type, int format_id,
2481 const struct path *path)
2483 int error = security_quota_on(path->dentry);
2486 /* Quota file not on the same filesystem? */
2487 if (path->dentry->d_sb != sb)
2490 error = dquot_load_quota_inode(d_inode(path->dentry), type,
2491 format_id, DQUOT_USAGE_ENABLED |
2492 DQUOT_LIMITS_ENABLED);
2495 EXPORT_SYMBOL(dquot_quota_on);
2498 * This function is used when filesystem needs to initialize quotas
2499 * during mount time.
2501 int dquot_quota_on_mount(struct super_block *sb, char *qf_name,
2502 int format_id, int type)
2504 struct dentry *dentry;
2507 dentry = lookup_positive_unlocked(qf_name, sb->s_root, strlen(qf_name));
2509 return PTR_ERR(dentry);
2511 error = security_quota_on(dentry);
2513 error = dquot_load_quota_inode(d_inode(dentry), type, format_id,
2514 DQUOT_USAGE_ENABLED | DQUOT_LIMITS_ENABLED);
2519 EXPORT_SYMBOL(dquot_quota_on_mount);
2521 static int dquot_quota_enable(struct super_block *sb, unsigned int flags)
2525 struct quota_info *dqopt = sb_dqopt(sb);
2527 if (!(dqopt->flags & DQUOT_QUOTA_SYS_FILE))
2529 /* Accounting cannot be turned on while fs is mounted */
2530 flags &= ~(FS_QUOTA_UDQ_ACCT | FS_QUOTA_GDQ_ACCT | FS_QUOTA_PDQ_ACCT);
2533 for (type = 0; type < MAXQUOTAS; type++) {
2534 if (!(flags & qtype_enforce_flag(type)))
2536 /* Can't enforce without accounting */
2537 if (!sb_has_quota_usage_enabled(sb, type)) {
2541 if (sb_has_quota_limits_enabled(sb, type)) {
2545 spin_lock(&dq_state_lock);
2546 dqopt->flags |= dquot_state_flag(DQUOT_LIMITS_ENABLED, type);
2547 spin_unlock(&dq_state_lock);
2551 /* Backout enforcement enablement we already did */
2552 for (type--; type >= 0; type--) {
2553 if (flags & qtype_enforce_flag(type))
2554 dquot_disable(sb, type, DQUOT_LIMITS_ENABLED);
2556 /* Error code translation for better compatibility with XFS */
2562 static int dquot_quota_disable(struct super_block *sb, unsigned int flags)
2566 struct quota_info *dqopt = sb_dqopt(sb);
2568 if (!(dqopt->flags & DQUOT_QUOTA_SYS_FILE))
2571 * We don't support turning off accounting via quotactl. In principle
2572 * quota infrastructure can do this but filesystems don't expect
2573 * userspace to be able to do it.
2576 (FS_QUOTA_UDQ_ACCT | FS_QUOTA_GDQ_ACCT | FS_QUOTA_PDQ_ACCT))
2579 /* Filter out limits not enabled */
2580 for (type = 0; type < MAXQUOTAS; type++)
2581 if (!sb_has_quota_limits_enabled(sb, type))
2582 flags &= ~qtype_enforce_flag(type);
2586 for (type = 0; type < MAXQUOTAS; type++) {
2587 if (flags & qtype_enforce_flag(type)) {
2588 ret = dquot_disable(sb, type, DQUOT_LIMITS_ENABLED);
2595 /* Backout enforcement disabling we already did */
2596 for (type--; type >= 0; type--) {
2597 if (flags & qtype_enforce_flag(type)) {
2598 spin_lock(&dq_state_lock);
2600 dquot_state_flag(DQUOT_LIMITS_ENABLED, type);
2601 spin_unlock(&dq_state_lock);
2607 /* Generic routine for getting common part of quota structure */
2608 static void do_get_dqblk(struct dquot *dquot, struct qc_dqblk *di)
2610 struct mem_dqblk *dm = &dquot->dq_dqb;
2612 memset(di, 0, sizeof(*di));
2613 spin_lock(&dquot->dq_dqb_lock);
2614 di->d_spc_hardlimit = dm->dqb_bhardlimit;
2615 di->d_spc_softlimit = dm->dqb_bsoftlimit;
2616 di->d_ino_hardlimit = dm->dqb_ihardlimit;
2617 di->d_ino_softlimit = dm->dqb_isoftlimit;
2618 di->d_space = dm->dqb_curspace + dm->dqb_rsvspace;
2619 di->d_ino_count = dm->dqb_curinodes;
2620 di->d_spc_timer = dm->dqb_btime;
2621 di->d_ino_timer = dm->dqb_itime;
2622 spin_unlock(&dquot->dq_dqb_lock);
2625 int dquot_get_dqblk(struct super_block *sb, struct kqid qid,
2626 struct qc_dqblk *di)
2628 struct dquot *dquot;
2630 dquot = dqget(sb, qid);
2632 return PTR_ERR(dquot);
2633 do_get_dqblk(dquot, di);
2638 EXPORT_SYMBOL(dquot_get_dqblk);
2640 int dquot_get_next_dqblk(struct super_block *sb, struct kqid *qid,
2641 struct qc_dqblk *di)
2643 struct dquot *dquot;
2646 if (!sb->dq_op->get_next_id)
2648 err = sb->dq_op->get_next_id(sb, qid);
2651 dquot = dqget(sb, *qid);
2653 return PTR_ERR(dquot);
2654 do_get_dqblk(dquot, di);
2659 EXPORT_SYMBOL(dquot_get_next_dqblk);
2661 #define VFS_QC_MASK \
2662 (QC_SPACE | QC_SPC_SOFT | QC_SPC_HARD | \
2663 QC_INO_COUNT | QC_INO_SOFT | QC_INO_HARD | \
2664 QC_SPC_TIMER | QC_INO_TIMER)
2666 /* Generic routine for setting common part of quota structure */
2667 static int do_set_dqblk(struct dquot *dquot, struct qc_dqblk *di)
2669 struct mem_dqblk *dm = &dquot->dq_dqb;
2670 int check_blim = 0, check_ilim = 0;
2671 struct mem_dqinfo *dqi = &sb_dqopt(dquot->dq_sb)->info[dquot->dq_id.type];
2673 if (di->d_fieldmask & ~VFS_QC_MASK)
2676 if (((di->d_fieldmask & QC_SPC_SOFT) &&
2677 di->d_spc_softlimit > dqi->dqi_max_spc_limit) ||
2678 ((di->d_fieldmask & QC_SPC_HARD) &&
2679 di->d_spc_hardlimit > dqi->dqi_max_spc_limit) ||
2680 ((di->d_fieldmask & QC_INO_SOFT) &&
2681 (di->d_ino_softlimit > dqi->dqi_max_ino_limit)) ||
2682 ((di->d_fieldmask & QC_INO_HARD) &&
2683 (di->d_ino_hardlimit > dqi->dqi_max_ino_limit)))
2686 spin_lock(&dquot->dq_dqb_lock);
2687 if (di->d_fieldmask & QC_SPACE) {
2688 dm->dqb_curspace = di->d_space - dm->dqb_rsvspace;
2690 set_bit(DQ_LASTSET_B + QIF_SPACE_B, &dquot->dq_flags);
2693 if (di->d_fieldmask & QC_SPC_SOFT)
2694 dm->dqb_bsoftlimit = di->d_spc_softlimit;
2695 if (di->d_fieldmask & QC_SPC_HARD)
2696 dm->dqb_bhardlimit = di->d_spc_hardlimit;
2697 if (di->d_fieldmask & (QC_SPC_SOFT | QC_SPC_HARD)) {
2699 set_bit(DQ_LASTSET_B + QIF_BLIMITS_B, &dquot->dq_flags);
2702 if (di->d_fieldmask & QC_INO_COUNT) {
2703 dm->dqb_curinodes = di->d_ino_count;
2705 set_bit(DQ_LASTSET_B + QIF_INODES_B, &dquot->dq_flags);
2708 if (di->d_fieldmask & QC_INO_SOFT)
2709 dm->dqb_isoftlimit = di->d_ino_softlimit;
2710 if (di->d_fieldmask & QC_INO_HARD)
2711 dm->dqb_ihardlimit = di->d_ino_hardlimit;
2712 if (di->d_fieldmask & (QC_INO_SOFT | QC_INO_HARD)) {
2714 set_bit(DQ_LASTSET_B + QIF_ILIMITS_B, &dquot->dq_flags);
2717 if (di->d_fieldmask & QC_SPC_TIMER) {
2718 dm->dqb_btime = di->d_spc_timer;
2720 set_bit(DQ_LASTSET_B + QIF_BTIME_B, &dquot->dq_flags);
2723 if (di->d_fieldmask & QC_INO_TIMER) {
2724 dm->dqb_itime = di->d_ino_timer;
2726 set_bit(DQ_LASTSET_B + QIF_ITIME_B, &dquot->dq_flags);
2730 if (!dm->dqb_bsoftlimit ||
2731 dm->dqb_curspace + dm->dqb_rsvspace <= dm->dqb_bsoftlimit) {
2733 clear_bit(DQ_BLKS_B, &dquot->dq_flags);
2734 } else if (!(di->d_fieldmask & QC_SPC_TIMER))
2735 /* Set grace only if user hasn't provided his own... */
2736 dm->dqb_btime = ktime_get_real_seconds() + dqi->dqi_bgrace;
2739 if (!dm->dqb_isoftlimit ||
2740 dm->dqb_curinodes <= dm->dqb_isoftlimit) {
2742 clear_bit(DQ_INODES_B, &dquot->dq_flags);
2743 } else if (!(di->d_fieldmask & QC_INO_TIMER))
2744 /* Set grace only if user hasn't provided his own... */
2745 dm->dqb_itime = ktime_get_real_seconds() + dqi->dqi_igrace;
2747 if (dm->dqb_bhardlimit || dm->dqb_bsoftlimit || dm->dqb_ihardlimit ||
2749 clear_bit(DQ_FAKE_B, &dquot->dq_flags);
2751 set_bit(DQ_FAKE_B, &dquot->dq_flags);
2752 spin_unlock(&dquot->dq_dqb_lock);
2753 mark_dquot_dirty(dquot);
2758 int dquot_set_dqblk(struct super_block *sb, struct kqid qid,
2759 struct qc_dqblk *di)
2761 struct dquot *dquot;
2764 dquot = dqget(sb, qid);
2765 if (IS_ERR(dquot)) {
2766 rc = PTR_ERR(dquot);
2769 rc = do_set_dqblk(dquot, di);
2774 EXPORT_SYMBOL(dquot_set_dqblk);
2776 /* Generic routine for getting common part of quota file information */
2777 int dquot_get_state(struct super_block *sb, struct qc_state *state)
2779 struct mem_dqinfo *mi;
2780 struct qc_type_state *tstate;
2781 struct quota_info *dqopt = sb_dqopt(sb);
2784 memset(state, 0, sizeof(*state));
2785 for (type = 0; type < MAXQUOTAS; type++) {
2786 if (!sb_has_quota_active(sb, type))
2788 tstate = state->s_state + type;
2789 mi = sb_dqopt(sb)->info + type;
2790 tstate->flags = QCI_ACCT_ENABLED;
2791 spin_lock(&dq_data_lock);
2792 if (mi->dqi_flags & DQF_SYS_FILE)
2793 tstate->flags |= QCI_SYSFILE;
2794 if (mi->dqi_flags & DQF_ROOT_SQUASH)
2795 tstate->flags |= QCI_ROOT_SQUASH;
2796 if (sb_has_quota_limits_enabled(sb, type))
2797 tstate->flags |= QCI_LIMITS_ENFORCED;
2798 tstate->spc_timelimit = mi->dqi_bgrace;
2799 tstate->ino_timelimit = mi->dqi_igrace;
2800 if (dqopt->files[type]) {
2801 tstate->ino = dqopt->files[type]->i_ino;
2802 tstate->blocks = dqopt->files[type]->i_blocks;
2804 tstate->nextents = 1; /* We don't know... */
2805 spin_unlock(&dq_data_lock);
2809 EXPORT_SYMBOL(dquot_get_state);
2811 /* Generic routine for setting common part of quota file information */
2812 int dquot_set_dqinfo(struct super_block *sb, int type, struct qc_info *ii)
2814 struct mem_dqinfo *mi;
2817 if ((ii->i_fieldmask & QC_WARNS_MASK) ||
2818 (ii->i_fieldmask & QC_RT_SPC_TIMER))
2820 if (!sb_has_quota_active(sb, type))
2822 mi = sb_dqopt(sb)->info + type;
2823 if (ii->i_fieldmask & QC_FLAGS) {
2824 if ((ii->i_flags & QCI_ROOT_SQUASH &&
2825 mi->dqi_format->qf_fmt_id != QFMT_VFS_OLD))
2828 spin_lock(&dq_data_lock);
2829 if (ii->i_fieldmask & QC_SPC_TIMER)
2830 mi->dqi_bgrace = ii->i_spc_timelimit;
2831 if (ii->i_fieldmask & QC_INO_TIMER)
2832 mi->dqi_igrace = ii->i_ino_timelimit;
2833 if (ii->i_fieldmask & QC_FLAGS) {
2834 if (ii->i_flags & QCI_ROOT_SQUASH)
2835 mi->dqi_flags |= DQF_ROOT_SQUASH;
2837 mi->dqi_flags &= ~DQF_ROOT_SQUASH;
2839 spin_unlock(&dq_data_lock);
2840 mark_info_dirty(sb, type);
2841 /* Force write to disk */
2842 sb->dq_op->write_info(sb, type);
2845 EXPORT_SYMBOL(dquot_set_dqinfo);
2847 const struct quotactl_ops dquot_quotactl_sysfile_ops = {
2848 .quota_enable = dquot_quota_enable,
2849 .quota_disable = dquot_quota_disable,
2850 .quota_sync = dquot_quota_sync,
2851 .get_state = dquot_get_state,
2852 .set_info = dquot_set_dqinfo,
2853 .get_dqblk = dquot_get_dqblk,
2854 .get_nextdqblk = dquot_get_next_dqblk,
2855 .set_dqblk = dquot_set_dqblk
2857 EXPORT_SYMBOL(dquot_quotactl_sysfile_ops);
2859 static int do_proc_dqstats(struct ctl_table *table, int write,
2860 void *buffer, size_t *lenp, loff_t *ppos)
2862 unsigned int type = (unsigned long *)table->data - dqstats.stat;
2863 s64 value = percpu_counter_sum(&dqstats.counter[type]);
2865 /* Filter negative values for non-monotonic counters */
2866 if (value < 0 && (type == DQST_ALLOC_DQUOTS ||
2867 type == DQST_FREE_DQUOTS))
2870 /* Update global table */
2871 dqstats.stat[type] = value;
2872 return proc_doulongvec_minmax(table, write, buffer, lenp, ppos);
2875 static struct ctl_table fs_dqstats_table[] = {
2877 .procname = "lookups",
2878 .data = &dqstats.stat[DQST_LOOKUPS],
2879 .maxlen = sizeof(unsigned long),
2881 .proc_handler = do_proc_dqstats,
2884 .procname = "drops",
2885 .data = &dqstats.stat[DQST_DROPS],
2886 .maxlen = sizeof(unsigned long),
2888 .proc_handler = do_proc_dqstats,
2891 .procname = "reads",
2892 .data = &dqstats.stat[DQST_READS],
2893 .maxlen = sizeof(unsigned long),
2895 .proc_handler = do_proc_dqstats,
2898 .procname = "writes",
2899 .data = &dqstats.stat[DQST_WRITES],
2900 .maxlen = sizeof(unsigned long),
2902 .proc_handler = do_proc_dqstats,
2905 .procname = "cache_hits",
2906 .data = &dqstats.stat[DQST_CACHE_HITS],
2907 .maxlen = sizeof(unsigned long),
2909 .proc_handler = do_proc_dqstats,
2912 .procname = "allocated_dquots",
2913 .data = &dqstats.stat[DQST_ALLOC_DQUOTS],
2914 .maxlen = sizeof(unsigned long),
2916 .proc_handler = do_proc_dqstats,
2919 .procname = "free_dquots",
2920 .data = &dqstats.stat[DQST_FREE_DQUOTS],
2921 .maxlen = sizeof(unsigned long),
2923 .proc_handler = do_proc_dqstats,
2926 .procname = "syncs",
2927 .data = &dqstats.stat[DQST_SYNCS],
2928 .maxlen = sizeof(unsigned long),
2930 .proc_handler = do_proc_dqstats,
2932 #ifdef CONFIG_PRINT_QUOTA_WARNING
2934 .procname = "warnings",
2935 .data = &flag_print_warnings,
2936 .maxlen = sizeof(int),
2938 .proc_handler = proc_dointvec,
2944 static struct ctl_table fs_table[] = {
2946 .procname = "quota",
2948 .child = fs_dqstats_table,
2953 static struct ctl_table sys_table[] = {
2962 static int __init dquot_init(void)
2965 unsigned long nr_hash, order;
2967 printk(KERN_NOTICE "VFS: Disk quotas %s\n", __DQUOT_VERSION__);
2969 register_sysctl_table(sys_table);
2971 dquot_cachep = kmem_cache_create("dquot",
2972 sizeof(struct dquot), sizeof(unsigned long) * 4,
2973 (SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT|
2974 SLAB_MEM_SPREAD|SLAB_PANIC),
2978 dquot_hash = (struct hlist_head *)__get_free_pages(GFP_KERNEL, order);
2980 panic("Cannot create dquot hash table");
2982 for (i = 0; i < _DQST_DQSTAT_LAST; i++) {
2983 ret = percpu_counter_init(&dqstats.counter[i], 0, GFP_KERNEL);
2985 panic("Cannot create dquot stat counters");
2988 /* Find power-of-two hlist_heads which can fit into allocation */
2989 nr_hash = (1UL << order) * PAGE_SIZE / sizeof(struct hlist_head);
2990 dq_hash_bits = ilog2(nr_hash);
2992 nr_hash = 1UL << dq_hash_bits;
2993 dq_hash_mask = nr_hash - 1;
2994 for (i = 0; i < nr_hash; i++)
2995 INIT_HLIST_HEAD(dquot_hash + i);
2997 pr_info("VFS: Dquot-cache hash table entries: %ld (order %ld,"
2998 " %ld bytes)\n", nr_hash, order, (PAGE_SIZE << order));
3000 if (register_shrinker(&dqcache_shrinker))
3001 panic("Cannot register dquot shrinker");
3005 fs_initcall(dquot_init);