vxlan: check return value of gro_cells_init()
[linux/fpc-iii.git] / fs / quota / dquot.c
blob1d1d393f4208ded44916a1e7d574564de616c1f3
1 // SPDX-License-Identifier: GPL-2.0
2 /*
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
27 * quota files
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
48 * New SMP locking.
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>
59 #include <linux/fs.h>
60 #include <linux/mount.h>
61 #include <linux/mm.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()) {
139 va_list args;
140 struct va_format vaf;
142 va_start(args, fmt);
144 vaf.fmt = fmt;
145 vaf.va = &args;
147 printk(KERN_ERR "Quota error (device %s): %s: %pV\n",
148 sb->s_id, func, &vaf);
150 va_end(args);
153 EXPORT_SYMBOL(__quota_error);
155 #if defined(CONFIG_QUOTA_DEBUG) || defined(CONFIG_PRINT_QUOTA_WARNING)
156 static char *quotatypes[] = INITQFNAMES;
157 #endif
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;
168 quota_formats = fmt;
169 spin_unlock(&dq_list_lock);
170 return 0;
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 = &quota_formats; *actqf && *actqf != fmt;
180 actqf = &(*actqf)->qf_next)
182 if (*actqf)
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)) {
197 int qm;
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))
206 return NULL;
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))
213 actqf = NULL;
215 spin_unlock(&dq_list_lock);
216 return actqf;
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 three lists for dquot management: the inuse_list,
227 * free_dquots, and dquot_hash[] array. A single dquot structure may be
228 * on all three lists, depending on its current state.
230 * All dquots are placed to the end of inuse_list when first created, and this
231 * list is used for invalidate operation, which must look at every dquot.
233 * Unused dquots (dq_count == 0) are added to the free_dquots list when freed,
234 * and this list is searched whenever we need an available dquot. Dquots are
235 * removed from the list as soon as they are used again, and
236 * dqstats.free_dquots gives the number of dquots on the list. When
237 * dquot is invalidated it's completely released from memory.
239 * Dquots with a specific identity (device, type and id) are placed on
240 * one of the dquot_hash[] hash chains. The provides an efficient search
241 * mechanism to locate a specific dquot.
244 static LIST_HEAD(inuse_list);
245 static LIST_HEAD(free_dquots);
246 static unsigned int dq_hash_bits, dq_hash_mask;
247 static struct hlist_head *dquot_hash;
249 struct dqstats dqstats;
250 EXPORT_SYMBOL(dqstats);
252 static qsize_t inode_get_rsv_space(struct inode *inode);
253 static qsize_t __inode_get_rsv_space(struct inode *inode);
254 static int __dquot_initialize(struct inode *inode, int type);
256 static inline unsigned int
257 hashfn(const struct super_block *sb, struct kqid qid)
259 unsigned int id = from_kqid(&init_user_ns, qid);
260 int type = qid.type;
261 unsigned long tmp;
263 tmp = (((unsigned long)sb>>L1_CACHE_SHIFT) ^ id) * (MAXQUOTAS - type);
264 return (tmp + (tmp >> dq_hash_bits)) & dq_hash_mask;
268 * Following list functions expect dq_list_lock to be held
270 static inline void insert_dquot_hash(struct dquot *dquot)
272 struct hlist_head *head;
273 head = dquot_hash + hashfn(dquot->dq_sb, dquot->dq_id);
274 hlist_add_head(&dquot->dq_hash, head);
277 static inline void remove_dquot_hash(struct dquot *dquot)
279 hlist_del_init(&dquot->dq_hash);
282 static struct dquot *find_dquot(unsigned int hashent, struct super_block *sb,
283 struct kqid qid)
285 struct hlist_node *node;
286 struct dquot *dquot;
288 hlist_for_each (node, dquot_hash+hashent) {
289 dquot = hlist_entry(node, struct dquot, dq_hash);
290 if (dquot->dq_sb == sb && qid_eq(dquot->dq_id, qid))
291 return dquot;
293 return NULL;
296 /* Add a dquot to the tail of the free list */
297 static inline void put_dquot_last(struct dquot *dquot)
299 list_add_tail(&dquot->dq_free, &free_dquots);
300 dqstats_inc(DQST_FREE_DQUOTS);
303 static inline void remove_free_dquot(struct dquot *dquot)
305 if (list_empty(&dquot->dq_free))
306 return;
307 list_del_init(&dquot->dq_free);
308 dqstats_dec(DQST_FREE_DQUOTS);
311 static inline void put_inuse(struct dquot *dquot)
313 /* We add to the back of inuse list so we don't have to restart
314 * when traversing this list and we block */
315 list_add_tail(&dquot->dq_inuse, &inuse_list);
316 dqstats_inc(DQST_ALLOC_DQUOTS);
319 static inline void remove_inuse(struct dquot *dquot)
321 dqstats_dec(DQST_ALLOC_DQUOTS);
322 list_del(&dquot->dq_inuse);
325 * End of list functions needing dq_list_lock
328 static void wait_on_dquot(struct dquot *dquot)
330 mutex_lock(&dquot->dq_lock);
331 mutex_unlock(&dquot->dq_lock);
334 static inline int dquot_dirty(struct dquot *dquot)
336 return test_bit(DQ_MOD_B, &dquot->dq_flags);
339 static inline int mark_dquot_dirty(struct dquot *dquot)
341 return dquot->dq_sb->dq_op->mark_dirty(dquot);
344 /* Mark dquot dirty in atomic manner, and return it's old dirty flag state */
345 int dquot_mark_dquot_dirty(struct dquot *dquot)
347 int ret = 1;
349 if (!test_bit(DQ_ACTIVE_B, &dquot->dq_flags))
350 return 0;
352 if (sb_dqopt(dquot->dq_sb)->flags & DQUOT_NOLIST_DIRTY)
353 return test_and_set_bit(DQ_MOD_B, &dquot->dq_flags);
355 /* If quota is dirty already, we don't have to acquire dq_list_lock */
356 if (test_bit(DQ_MOD_B, &dquot->dq_flags))
357 return 1;
359 spin_lock(&dq_list_lock);
360 if (!test_and_set_bit(DQ_MOD_B, &dquot->dq_flags)) {
361 list_add(&dquot->dq_dirty, &sb_dqopt(dquot->dq_sb)->
362 info[dquot->dq_id.type].dqi_dirty_list);
363 ret = 0;
365 spin_unlock(&dq_list_lock);
366 return ret;
368 EXPORT_SYMBOL(dquot_mark_dquot_dirty);
370 /* Dirtify all the dquots - this can block when journalling */
371 static inline int mark_all_dquot_dirty(struct dquot * const *dquot)
373 int ret, err, cnt;
375 ret = err = 0;
376 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
377 if (dquot[cnt])
378 /* Even in case of error we have to continue */
379 ret = mark_dquot_dirty(dquot[cnt]);
380 if (!err)
381 err = ret;
383 return err;
386 static inline void dqput_all(struct dquot **dquot)
388 unsigned int cnt;
390 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
391 dqput(dquot[cnt]);
394 static inline int clear_dquot_dirty(struct dquot *dquot)
396 if (sb_dqopt(dquot->dq_sb)->flags & DQUOT_NOLIST_DIRTY)
397 return test_and_clear_bit(DQ_MOD_B, &dquot->dq_flags);
399 spin_lock(&dq_list_lock);
400 if (!test_and_clear_bit(DQ_MOD_B, &dquot->dq_flags)) {
401 spin_unlock(&dq_list_lock);
402 return 0;
404 list_del_init(&dquot->dq_dirty);
405 spin_unlock(&dq_list_lock);
406 return 1;
409 void mark_info_dirty(struct super_block *sb, int type)
411 spin_lock(&dq_data_lock);
412 sb_dqopt(sb)->info[type].dqi_flags |= DQF_INFO_DIRTY;
413 spin_unlock(&dq_data_lock);
415 EXPORT_SYMBOL(mark_info_dirty);
418 * Read dquot from disk and alloc space for it
421 int dquot_acquire(struct dquot *dquot)
423 int ret = 0, ret2 = 0;
424 struct quota_info *dqopt = sb_dqopt(dquot->dq_sb);
426 mutex_lock(&dquot->dq_lock);
427 if (!test_bit(DQ_READ_B, &dquot->dq_flags))
428 ret = dqopt->ops[dquot->dq_id.type]->read_dqblk(dquot);
429 if (ret < 0)
430 goto out_iolock;
431 /* Make sure flags update is visible after dquot has been filled */
432 smp_mb__before_atomic();
433 set_bit(DQ_READ_B, &dquot->dq_flags);
434 /* Instantiate dquot if needed */
435 if (!test_bit(DQ_ACTIVE_B, &dquot->dq_flags) && !dquot->dq_off) {
436 ret = dqopt->ops[dquot->dq_id.type]->commit_dqblk(dquot);
437 /* Write the info if needed */
438 if (info_dirty(&dqopt->info[dquot->dq_id.type])) {
439 ret2 = dqopt->ops[dquot->dq_id.type]->write_file_info(
440 dquot->dq_sb, dquot->dq_id.type);
442 if (ret < 0)
443 goto out_iolock;
444 if (ret2 < 0) {
445 ret = ret2;
446 goto out_iolock;
450 * Make sure flags update is visible after on-disk struct has been
451 * allocated. Paired with smp_rmb() in dqget().
453 smp_mb__before_atomic();
454 set_bit(DQ_ACTIVE_B, &dquot->dq_flags);
455 out_iolock:
456 mutex_unlock(&dquot->dq_lock);
457 return ret;
459 EXPORT_SYMBOL(dquot_acquire);
462 * Write dquot to disk
464 int dquot_commit(struct dquot *dquot)
466 int ret = 0;
467 struct quota_info *dqopt = sb_dqopt(dquot->dq_sb);
469 mutex_lock(&dquot->dq_lock);
470 if (!clear_dquot_dirty(dquot))
471 goto out_lock;
472 /* Inactive dquot can be only if there was error during read/init
473 * => we have better not writing it */
474 if (test_bit(DQ_ACTIVE_B, &dquot->dq_flags))
475 ret = dqopt->ops[dquot->dq_id.type]->commit_dqblk(dquot);
476 else
477 ret = -EIO;
478 out_lock:
479 mutex_unlock(&dquot->dq_lock);
480 return ret;
482 EXPORT_SYMBOL(dquot_commit);
485 * Release dquot
487 int dquot_release(struct dquot *dquot)
489 int ret = 0, ret2 = 0;
490 struct quota_info *dqopt = sb_dqopt(dquot->dq_sb);
492 mutex_lock(&dquot->dq_lock);
493 /* Check whether we are not racing with some other dqget() */
494 if (dquot_is_busy(dquot))
495 goto out_dqlock;
496 if (dqopt->ops[dquot->dq_id.type]->release_dqblk) {
497 ret = dqopt->ops[dquot->dq_id.type]->release_dqblk(dquot);
498 /* Write the info */
499 if (info_dirty(&dqopt->info[dquot->dq_id.type])) {
500 ret2 = dqopt->ops[dquot->dq_id.type]->write_file_info(
501 dquot->dq_sb, dquot->dq_id.type);
503 if (ret >= 0)
504 ret = ret2;
506 clear_bit(DQ_ACTIVE_B, &dquot->dq_flags);
507 out_dqlock:
508 mutex_unlock(&dquot->dq_lock);
509 return ret;
511 EXPORT_SYMBOL(dquot_release);
513 void dquot_destroy(struct dquot *dquot)
515 kmem_cache_free(dquot_cachep, dquot);
517 EXPORT_SYMBOL(dquot_destroy);
519 static inline void do_destroy_dquot(struct dquot *dquot)
521 dquot->dq_sb->dq_op->destroy_dquot(dquot);
524 /* Invalidate all dquots on the list. Note that this function is called after
525 * quota is disabled and pointers from inodes removed so there cannot be new
526 * quota users. There can still be some users of quotas due to inodes being
527 * just deleted or pruned by prune_icache() (those are not attached to any
528 * list) or parallel quotactl call. We have to wait for such users.
530 static void invalidate_dquots(struct super_block *sb, int type)
532 struct dquot *dquot, *tmp;
534 restart:
535 spin_lock(&dq_list_lock);
536 list_for_each_entry_safe(dquot, tmp, &inuse_list, dq_inuse) {
537 if (dquot->dq_sb != sb)
538 continue;
539 if (dquot->dq_id.type != type)
540 continue;
541 /* Wait for dquot users */
542 if (atomic_read(&dquot->dq_count)) {
543 dqgrab(dquot);
544 spin_unlock(&dq_list_lock);
546 * Once dqput() wakes us up, we know it's time to free
547 * the dquot.
548 * IMPORTANT: we rely on the fact that there is always
549 * at most one process waiting for dquot to free.
550 * Otherwise dq_count would be > 1 and we would never
551 * wake up.
553 wait_event(dquot_ref_wq,
554 atomic_read(&dquot->dq_count) == 1);
555 dqput(dquot);
556 /* At this moment dquot() need not exist (it could be
557 * reclaimed by prune_dqcache(). Hence we must
558 * restart. */
559 goto restart;
562 * Quota now has no users and it has been written on last
563 * dqput()
565 remove_dquot_hash(dquot);
566 remove_free_dquot(dquot);
567 remove_inuse(dquot);
568 do_destroy_dquot(dquot);
570 spin_unlock(&dq_list_lock);
573 /* Call callback for every active dquot on given filesystem */
574 int dquot_scan_active(struct super_block *sb,
575 int (*fn)(struct dquot *dquot, unsigned long priv),
576 unsigned long priv)
578 struct dquot *dquot, *old_dquot = NULL;
579 int ret = 0;
581 WARN_ON_ONCE(!rwsem_is_locked(&sb->s_umount));
583 spin_lock(&dq_list_lock);
584 list_for_each_entry(dquot, &inuse_list, dq_inuse) {
585 if (!test_bit(DQ_ACTIVE_B, &dquot->dq_flags))
586 continue;
587 if (dquot->dq_sb != sb)
588 continue;
589 /* Now we have active dquot so we can just increase use count */
590 atomic_inc(&dquot->dq_count);
591 spin_unlock(&dq_list_lock);
592 dqstats_inc(DQST_LOOKUPS);
593 dqput(old_dquot);
594 old_dquot = dquot;
596 * ->release_dquot() can be racing with us. Our reference
597 * protects us from new calls to it so just wait for any
598 * outstanding call and recheck the DQ_ACTIVE_B after that.
600 wait_on_dquot(dquot);
601 if (test_bit(DQ_ACTIVE_B, &dquot->dq_flags)) {
602 ret = fn(dquot, priv);
603 if (ret < 0)
604 goto out;
606 spin_lock(&dq_list_lock);
607 /* We are safe to continue now because our dquot could not
608 * be moved out of the inuse list while we hold the reference */
610 spin_unlock(&dq_list_lock);
611 out:
612 dqput(old_dquot);
613 return ret;
615 EXPORT_SYMBOL(dquot_scan_active);
617 /* Write all dquot structures to quota files */
618 int dquot_writeback_dquots(struct super_block *sb, int type)
620 struct list_head dirty;
621 struct dquot *dquot;
622 struct quota_info *dqopt = sb_dqopt(sb);
623 int cnt;
624 int err, ret = 0;
626 WARN_ON_ONCE(!rwsem_is_locked(&sb->s_umount));
628 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
629 if (type != -1 && cnt != type)
630 continue;
631 if (!sb_has_quota_active(sb, cnt))
632 continue;
633 spin_lock(&dq_list_lock);
634 /* Move list away to avoid livelock. */
635 list_replace_init(&dqopt->info[cnt].dqi_dirty_list, &dirty);
636 while (!list_empty(&dirty)) {
637 dquot = list_first_entry(&dirty, struct dquot,
638 dq_dirty);
640 WARN_ON(!test_bit(DQ_ACTIVE_B, &dquot->dq_flags));
642 /* Now we have active dquot from which someone is
643 * holding reference so we can safely just increase
644 * use count */
645 dqgrab(dquot);
646 spin_unlock(&dq_list_lock);
647 dqstats_inc(DQST_LOOKUPS);
648 err = sb->dq_op->write_dquot(dquot);
649 if (err) {
651 * Clear dirty bit anyway to avoid infinite
652 * loop here.
654 clear_dquot_dirty(dquot);
655 if (!ret)
656 ret = err;
658 dqput(dquot);
659 spin_lock(&dq_list_lock);
661 spin_unlock(&dq_list_lock);
664 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
665 if ((cnt == type || type == -1) && sb_has_quota_active(sb, cnt)
666 && info_dirty(&dqopt->info[cnt]))
667 sb->dq_op->write_info(sb, cnt);
668 dqstats_inc(DQST_SYNCS);
670 return ret;
672 EXPORT_SYMBOL(dquot_writeback_dquots);
674 /* Write all dquot structures to disk and make them visible from userspace */
675 int dquot_quota_sync(struct super_block *sb, int type)
677 struct quota_info *dqopt = sb_dqopt(sb);
678 int cnt;
679 int ret;
681 ret = dquot_writeback_dquots(sb, type);
682 if (ret)
683 return ret;
684 if (dqopt->flags & DQUOT_QUOTA_SYS_FILE)
685 return 0;
687 /* This is not very clever (and fast) but currently I don't know about
688 * any other simple way of getting quota data to disk and we must get
689 * them there for userspace to be visible... */
690 if (sb->s_op->sync_fs)
691 sb->s_op->sync_fs(sb, 1);
692 sync_blockdev(sb->s_bdev);
695 * Now when everything is written we can discard the pagecache so
696 * that userspace sees the changes.
698 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
699 if (type != -1 && cnt != type)
700 continue;
701 if (!sb_has_quota_active(sb, cnt))
702 continue;
703 inode_lock(dqopt->files[cnt]);
704 truncate_inode_pages(&dqopt->files[cnt]->i_data, 0);
705 inode_unlock(dqopt->files[cnt]);
708 return 0;
710 EXPORT_SYMBOL(dquot_quota_sync);
712 static unsigned long
713 dqcache_shrink_scan(struct shrinker *shrink, struct shrink_control *sc)
715 struct dquot *dquot;
716 unsigned long freed = 0;
718 spin_lock(&dq_list_lock);
719 while (!list_empty(&free_dquots) && sc->nr_to_scan) {
720 dquot = list_first_entry(&free_dquots, struct dquot, dq_free);
721 remove_dquot_hash(dquot);
722 remove_free_dquot(dquot);
723 remove_inuse(dquot);
724 do_destroy_dquot(dquot);
725 sc->nr_to_scan--;
726 freed++;
728 spin_unlock(&dq_list_lock);
729 return freed;
732 static unsigned long
733 dqcache_shrink_count(struct shrinker *shrink, struct shrink_control *sc)
735 return vfs_pressure_ratio(
736 percpu_counter_read_positive(&dqstats.counter[DQST_FREE_DQUOTS]));
739 static struct shrinker dqcache_shrinker = {
740 .count_objects = dqcache_shrink_count,
741 .scan_objects = dqcache_shrink_scan,
742 .seeks = DEFAULT_SEEKS,
746 * Put reference to dquot
748 void dqput(struct dquot *dquot)
750 int ret;
752 if (!dquot)
753 return;
754 #ifdef CONFIG_QUOTA_DEBUG
755 if (!atomic_read(&dquot->dq_count)) {
756 quota_error(dquot->dq_sb, "trying to free free dquot of %s %d",
757 quotatypes[dquot->dq_id.type],
758 from_kqid(&init_user_ns, dquot->dq_id));
759 BUG();
761 #endif
762 dqstats_inc(DQST_DROPS);
763 we_slept:
764 spin_lock(&dq_list_lock);
765 if (atomic_read(&dquot->dq_count) > 1) {
766 /* We have more than one user... nothing to do */
767 atomic_dec(&dquot->dq_count);
768 /* Releasing dquot during quotaoff phase? */
769 if (!sb_has_quota_active(dquot->dq_sb, dquot->dq_id.type) &&
770 atomic_read(&dquot->dq_count) == 1)
771 wake_up(&dquot_ref_wq);
772 spin_unlock(&dq_list_lock);
773 return;
775 /* Need to release dquot? */
776 if (dquot_dirty(dquot)) {
777 spin_unlock(&dq_list_lock);
778 /* Commit dquot before releasing */
779 ret = dquot->dq_sb->dq_op->write_dquot(dquot);
780 if (ret < 0) {
781 quota_error(dquot->dq_sb, "Can't write quota structure"
782 " (error %d). Quota may get out of sync!",
783 ret);
785 * We clear dirty bit anyway, so that we avoid
786 * infinite loop here
788 clear_dquot_dirty(dquot);
790 goto we_slept;
792 if (test_bit(DQ_ACTIVE_B, &dquot->dq_flags)) {
793 spin_unlock(&dq_list_lock);
794 dquot->dq_sb->dq_op->release_dquot(dquot);
795 goto we_slept;
797 atomic_dec(&dquot->dq_count);
798 #ifdef CONFIG_QUOTA_DEBUG
799 /* sanity check */
800 BUG_ON(!list_empty(&dquot->dq_free));
801 #endif
802 put_dquot_last(dquot);
803 spin_unlock(&dq_list_lock);
805 EXPORT_SYMBOL(dqput);
807 struct dquot *dquot_alloc(struct super_block *sb, int type)
809 return kmem_cache_zalloc(dquot_cachep, GFP_NOFS);
811 EXPORT_SYMBOL(dquot_alloc);
813 static struct dquot *get_empty_dquot(struct super_block *sb, int type)
815 struct dquot *dquot;
817 dquot = sb->dq_op->alloc_dquot(sb, type);
818 if(!dquot)
819 return NULL;
821 mutex_init(&dquot->dq_lock);
822 INIT_LIST_HEAD(&dquot->dq_free);
823 INIT_LIST_HEAD(&dquot->dq_inuse);
824 INIT_HLIST_NODE(&dquot->dq_hash);
825 INIT_LIST_HEAD(&dquot->dq_dirty);
826 dquot->dq_sb = sb;
827 dquot->dq_id = make_kqid_invalid(type);
828 atomic_set(&dquot->dq_count, 1);
829 spin_lock_init(&dquot->dq_dqb_lock);
831 return dquot;
835 * Get reference to dquot
837 * Locking is slightly tricky here. We are guarded from parallel quotaoff()
838 * destroying our dquot by:
839 * a) checking for quota flags under dq_list_lock and
840 * b) getting a reference to dquot before we release dq_list_lock
842 struct dquot *dqget(struct super_block *sb, struct kqid qid)
844 unsigned int hashent = hashfn(sb, qid);
845 struct dquot *dquot, *empty = NULL;
847 if (!qid_has_mapping(sb->s_user_ns, qid))
848 return ERR_PTR(-EINVAL);
850 if (!sb_has_quota_active(sb, qid.type))
851 return ERR_PTR(-ESRCH);
852 we_slept:
853 spin_lock(&dq_list_lock);
854 spin_lock(&dq_state_lock);
855 if (!sb_has_quota_active(sb, qid.type)) {
856 spin_unlock(&dq_state_lock);
857 spin_unlock(&dq_list_lock);
858 dquot = ERR_PTR(-ESRCH);
859 goto out;
861 spin_unlock(&dq_state_lock);
863 dquot = find_dquot(hashent, sb, qid);
864 if (!dquot) {
865 if (!empty) {
866 spin_unlock(&dq_list_lock);
867 empty = get_empty_dquot(sb, qid.type);
868 if (!empty)
869 schedule(); /* Try to wait for a moment... */
870 goto we_slept;
872 dquot = empty;
873 empty = NULL;
874 dquot->dq_id = qid;
875 /* all dquots go on the inuse_list */
876 put_inuse(dquot);
877 /* hash it first so it can be found */
878 insert_dquot_hash(dquot);
879 spin_unlock(&dq_list_lock);
880 dqstats_inc(DQST_LOOKUPS);
881 } else {
882 if (!atomic_read(&dquot->dq_count))
883 remove_free_dquot(dquot);
884 atomic_inc(&dquot->dq_count);
885 spin_unlock(&dq_list_lock);
886 dqstats_inc(DQST_CACHE_HITS);
887 dqstats_inc(DQST_LOOKUPS);
889 /* Wait for dq_lock - after this we know that either dquot_release() is
890 * already finished or it will be canceled due to dq_count > 1 test */
891 wait_on_dquot(dquot);
892 /* Read the dquot / allocate space in quota file */
893 if (!test_bit(DQ_ACTIVE_B, &dquot->dq_flags)) {
894 int err;
896 err = sb->dq_op->acquire_dquot(dquot);
897 if (err < 0) {
898 dqput(dquot);
899 dquot = ERR_PTR(err);
900 goto out;
904 * Make sure following reads see filled structure - paired with
905 * smp_mb__before_atomic() in dquot_acquire().
907 smp_rmb();
908 #ifdef CONFIG_QUOTA_DEBUG
909 BUG_ON(!dquot->dq_sb); /* Has somebody invalidated entry under us? */
910 #endif
911 out:
912 if (empty)
913 do_destroy_dquot(empty);
915 return dquot;
917 EXPORT_SYMBOL(dqget);
919 static inline struct dquot **i_dquot(struct inode *inode)
921 return inode->i_sb->s_op->get_dquots(inode);
924 static int dqinit_needed(struct inode *inode, int type)
926 struct dquot * const *dquots;
927 int cnt;
929 if (IS_NOQUOTA(inode))
930 return 0;
932 dquots = i_dquot(inode);
933 if (type != -1)
934 return !dquots[type];
935 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
936 if (!dquots[cnt])
937 return 1;
938 return 0;
941 /* This routine is guarded by s_umount semaphore */
942 static int add_dquot_ref(struct super_block *sb, int type)
944 struct inode *inode, *old_inode = NULL;
945 #ifdef CONFIG_QUOTA_DEBUG
946 int reserved = 0;
947 #endif
948 int err = 0;
950 spin_lock(&sb->s_inode_list_lock);
951 list_for_each_entry(inode, &sb->s_inodes, i_sb_list) {
952 spin_lock(&inode->i_lock);
953 if ((inode->i_state & (I_FREEING|I_WILL_FREE|I_NEW)) ||
954 !atomic_read(&inode->i_writecount) ||
955 !dqinit_needed(inode, type)) {
956 spin_unlock(&inode->i_lock);
957 continue;
959 __iget(inode);
960 spin_unlock(&inode->i_lock);
961 spin_unlock(&sb->s_inode_list_lock);
963 #ifdef CONFIG_QUOTA_DEBUG
964 if (unlikely(inode_get_rsv_space(inode) > 0))
965 reserved = 1;
966 #endif
967 iput(old_inode);
968 err = __dquot_initialize(inode, type);
969 if (err) {
970 iput(inode);
971 goto out;
975 * We hold a reference to 'inode' so it couldn't have been
976 * removed from s_inodes list while we dropped the
977 * s_inode_list_lock. We cannot iput the inode now as we can be
978 * holding the last reference and we cannot iput it under
979 * s_inode_list_lock. So we keep the reference and iput it
980 * later.
982 old_inode = inode;
983 cond_resched();
984 spin_lock(&sb->s_inode_list_lock);
986 spin_unlock(&sb->s_inode_list_lock);
987 iput(old_inode);
988 out:
989 #ifdef CONFIG_QUOTA_DEBUG
990 if (reserved) {
991 quota_error(sb, "Writes happened before quota was turned on "
992 "thus quota information is probably inconsistent. "
993 "Please run quotacheck(8)");
995 #endif
996 return err;
1000 * Remove references to dquots from inode and add dquot to list for freeing
1001 * if we have the last reference to dquot
1003 static void remove_inode_dquot_ref(struct inode *inode, int type,
1004 struct list_head *tofree_head)
1006 struct dquot **dquots = i_dquot(inode);
1007 struct dquot *dquot = dquots[type];
1009 if (!dquot)
1010 return;
1012 dquots[type] = NULL;
1013 if (list_empty(&dquot->dq_free)) {
1015 * The inode still has reference to dquot so it can't be in the
1016 * free list
1018 spin_lock(&dq_list_lock);
1019 list_add(&dquot->dq_free, tofree_head);
1020 spin_unlock(&dq_list_lock);
1021 } else {
1023 * Dquot is already in a list to put so we won't drop the last
1024 * reference here.
1026 dqput(dquot);
1031 * Free list of dquots
1032 * Dquots are removed from inodes and no new references can be got so we are
1033 * the only ones holding reference
1035 static void put_dquot_list(struct list_head *tofree_head)
1037 struct list_head *act_head;
1038 struct dquot *dquot;
1040 act_head = tofree_head->next;
1041 while (act_head != tofree_head) {
1042 dquot = list_entry(act_head, struct dquot, dq_free);
1043 act_head = act_head->next;
1044 /* Remove dquot from the list so we won't have problems... */
1045 list_del_init(&dquot->dq_free);
1046 dqput(dquot);
1050 static void remove_dquot_ref(struct super_block *sb, int type,
1051 struct list_head *tofree_head)
1053 struct inode *inode;
1054 int reserved = 0;
1056 spin_lock(&sb->s_inode_list_lock);
1057 list_for_each_entry(inode, &sb->s_inodes, i_sb_list) {
1059 * We have to scan also I_NEW inodes because they can already
1060 * have quota pointer initialized. Luckily, we need to touch
1061 * only quota pointers and these have separate locking
1062 * (dq_data_lock).
1064 spin_lock(&dq_data_lock);
1065 if (!IS_NOQUOTA(inode)) {
1066 if (unlikely(inode_get_rsv_space(inode) > 0))
1067 reserved = 1;
1068 remove_inode_dquot_ref(inode, type, tofree_head);
1070 spin_unlock(&dq_data_lock);
1072 spin_unlock(&sb->s_inode_list_lock);
1073 #ifdef CONFIG_QUOTA_DEBUG
1074 if (reserved) {
1075 printk(KERN_WARNING "VFS (%s): Writes happened after quota"
1076 " was disabled thus quota information is probably "
1077 "inconsistent. Please run quotacheck(8).\n", sb->s_id);
1079 #endif
1082 /* Gather all references from inodes and drop them */
1083 static void drop_dquot_ref(struct super_block *sb, int type)
1085 LIST_HEAD(tofree_head);
1087 if (sb->dq_op) {
1088 remove_dquot_ref(sb, type, &tofree_head);
1089 synchronize_srcu(&dquot_srcu);
1090 put_dquot_list(&tofree_head);
1094 static inline
1095 void dquot_free_reserved_space(struct dquot *dquot, qsize_t number)
1097 if (dquot->dq_dqb.dqb_rsvspace >= number)
1098 dquot->dq_dqb.dqb_rsvspace -= number;
1099 else {
1100 WARN_ON_ONCE(1);
1101 dquot->dq_dqb.dqb_rsvspace = 0;
1103 if (dquot->dq_dqb.dqb_curspace + dquot->dq_dqb.dqb_rsvspace <=
1104 dquot->dq_dqb.dqb_bsoftlimit)
1105 dquot->dq_dqb.dqb_btime = (time64_t) 0;
1106 clear_bit(DQ_BLKS_B, &dquot->dq_flags);
1109 static void dquot_decr_inodes(struct dquot *dquot, qsize_t number)
1111 if (sb_dqopt(dquot->dq_sb)->flags & DQUOT_NEGATIVE_USAGE ||
1112 dquot->dq_dqb.dqb_curinodes >= number)
1113 dquot->dq_dqb.dqb_curinodes -= number;
1114 else
1115 dquot->dq_dqb.dqb_curinodes = 0;
1116 if (dquot->dq_dqb.dqb_curinodes <= dquot->dq_dqb.dqb_isoftlimit)
1117 dquot->dq_dqb.dqb_itime = (time64_t) 0;
1118 clear_bit(DQ_INODES_B, &dquot->dq_flags);
1121 static void dquot_decr_space(struct dquot *dquot, qsize_t number)
1123 if (sb_dqopt(dquot->dq_sb)->flags & DQUOT_NEGATIVE_USAGE ||
1124 dquot->dq_dqb.dqb_curspace >= number)
1125 dquot->dq_dqb.dqb_curspace -= number;
1126 else
1127 dquot->dq_dqb.dqb_curspace = 0;
1128 if (dquot->dq_dqb.dqb_curspace + dquot->dq_dqb.dqb_rsvspace <=
1129 dquot->dq_dqb.dqb_bsoftlimit)
1130 dquot->dq_dqb.dqb_btime = (time64_t) 0;
1131 clear_bit(DQ_BLKS_B, &dquot->dq_flags);
1134 struct dquot_warn {
1135 struct super_block *w_sb;
1136 struct kqid w_dq_id;
1137 short w_type;
1140 static int warning_issued(struct dquot *dquot, const int warntype)
1142 int flag = (warntype == QUOTA_NL_BHARDWARN ||
1143 warntype == QUOTA_NL_BSOFTLONGWARN) ? DQ_BLKS_B :
1144 ((warntype == QUOTA_NL_IHARDWARN ||
1145 warntype == QUOTA_NL_ISOFTLONGWARN) ? DQ_INODES_B : 0);
1147 if (!flag)
1148 return 0;
1149 return test_and_set_bit(flag, &dquot->dq_flags);
1152 #ifdef CONFIG_PRINT_QUOTA_WARNING
1153 static int flag_print_warnings = 1;
1155 static int need_print_warning(struct dquot_warn *warn)
1157 if (!flag_print_warnings)
1158 return 0;
1160 switch (warn->w_dq_id.type) {
1161 case USRQUOTA:
1162 return uid_eq(current_fsuid(), warn->w_dq_id.uid);
1163 case GRPQUOTA:
1164 return in_group_p(warn->w_dq_id.gid);
1165 case PRJQUOTA:
1166 return 1;
1168 return 0;
1171 /* Print warning to user which exceeded quota */
1172 static void print_warning(struct dquot_warn *warn)
1174 char *msg = NULL;
1175 struct tty_struct *tty;
1176 int warntype = warn->w_type;
1178 if (warntype == QUOTA_NL_IHARDBELOW ||
1179 warntype == QUOTA_NL_ISOFTBELOW ||
1180 warntype == QUOTA_NL_BHARDBELOW ||
1181 warntype == QUOTA_NL_BSOFTBELOW || !need_print_warning(warn))
1182 return;
1184 tty = get_current_tty();
1185 if (!tty)
1186 return;
1187 tty_write_message(tty, warn->w_sb->s_id);
1188 if (warntype == QUOTA_NL_ISOFTWARN || warntype == QUOTA_NL_BSOFTWARN)
1189 tty_write_message(tty, ": warning, ");
1190 else
1191 tty_write_message(tty, ": write failed, ");
1192 tty_write_message(tty, quotatypes[warn->w_dq_id.type]);
1193 switch (warntype) {
1194 case QUOTA_NL_IHARDWARN:
1195 msg = " file limit reached.\r\n";
1196 break;
1197 case QUOTA_NL_ISOFTLONGWARN:
1198 msg = " file quota exceeded too long.\r\n";
1199 break;
1200 case QUOTA_NL_ISOFTWARN:
1201 msg = " file quota exceeded.\r\n";
1202 break;
1203 case QUOTA_NL_BHARDWARN:
1204 msg = " block limit reached.\r\n";
1205 break;
1206 case QUOTA_NL_BSOFTLONGWARN:
1207 msg = " block quota exceeded too long.\r\n";
1208 break;
1209 case QUOTA_NL_BSOFTWARN:
1210 msg = " block quota exceeded.\r\n";
1211 break;
1213 tty_write_message(tty, msg);
1214 tty_kref_put(tty);
1216 #endif
1218 static void prepare_warning(struct dquot_warn *warn, struct dquot *dquot,
1219 int warntype)
1221 if (warning_issued(dquot, warntype))
1222 return;
1223 warn->w_type = warntype;
1224 warn->w_sb = dquot->dq_sb;
1225 warn->w_dq_id = dquot->dq_id;
1229 * Write warnings to the console and send warning messages over netlink.
1231 * Note that this function can call into tty and networking code.
1233 static void flush_warnings(struct dquot_warn *warn)
1235 int i;
1237 for (i = 0; i < MAXQUOTAS; i++) {
1238 if (warn[i].w_type == QUOTA_NL_NOWARN)
1239 continue;
1240 #ifdef CONFIG_PRINT_QUOTA_WARNING
1241 print_warning(&warn[i]);
1242 #endif
1243 quota_send_warning(warn[i].w_dq_id,
1244 warn[i].w_sb->s_dev, warn[i].w_type);
1248 static int ignore_hardlimit(struct dquot *dquot)
1250 struct mem_dqinfo *info = &sb_dqopt(dquot->dq_sb)->info[dquot->dq_id.type];
1252 return capable(CAP_SYS_RESOURCE) &&
1253 (info->dqi_format->qf_fmt_id != QFMT_VFS_OLD ||
1254 !(info->dqi_flags & DQF_ROOT_SQUASH));
1257 static int dquot_add_inodes(struct dquot *dquot, qsize_t inodes,
1258 struct dquot_warn *warn)
1260 qsize_t newinodes;
1261 int ret = 0;
1263 spin_lock(&dquot->dq_dqb_lock);
1264 newinodes = dquot->dq_dqb.dqb_curinodes + inodes;
1265 if (!sb_has_quota_limits_enabled(dquot->dq_sb, dquot->dq_id.type) ||
1266 test_bit(DQ_FAKE_B, &dquot->dq_flags))
1267 goto add;
1269 if (dquot->dq_dqb.dqb_ihardlimit &&
1270 newinodes > dquot->dq_dqb.dqb_ihardlimit &&
1271 !ignore_hardlimit(dquot)) {
1272 prepare_warning(warn, dquot, QUOTA_NL_IHARDWARN);
1273 ret = -EDQUOT;
1274 goto out;
1277 if (dquot->dq_dqb.dqb_isoftlimit &&
1278 newinodes > dquot->dq_dqb.dqb_isoftlimit &&
1279 dquot->dq_dqb.dqb_itime &&
1280 ktime_get_real_seconds() >= dquot->dq_dqb.dqb_itime &&
1281 !ignore_hardlimit(dquot)) {
1282 prepare_warning(warn, dquot, QUOTA_NL_ISOFTLONGWARN);
1283 ret = -EDQUOT;
1284 goto out;
1287 if (dquot->dq_dqb.dqb_isoftlimit &&
1288 newinodes > dquot->dq_dqb.dqb_isoftlimit &&
1289 dquot->dq_dqb.dqb_itime == 0) {
1290 prepare_warning(warn, dquot, QUOTA_NL_ISOFTWARN);
1291 dquot->dq_dqb.dqb_itime = ktime_get_real_seconds() +
1292 sb_dqopt(dquot->dq_sb)->info[dquot->dq_id.type].dqi_igrace;
1294 add:
1295 dquot->dq_dqb.dqb_curinodes = newinodes;
1297 out:
1298 spin_unlock(&dquot->dq_dqb_lock);
1299 return ret;
1302 static int dquot_add_space(struct dquot *dquot, qsize_t space,
1303 qsize_t rsv_space, unsigned int flags,
1304 struct dquot_warn *warn)
1306 qsize_t tspace;
1307 struct super_block *sb = dquot->dq_sb;
1308 int ret = 0;
1310 spin_lock(&dquot->dq_dqb_lock);
1311 if (!sb_has_quota_limits_enabled(sb, dquot->dq_id.type) ||
1312 test_bit(DQ_FAKE_B, &dquot->dq_flags))
1313 goto finish;
1315 tspace = dquot->dq_dqb.dqb_curspace + dquot->dq_dqb.dqb_rsvspace
1316 + space + rsv_space;
1318 if (dquot->dq_dqb.dqb_bhardlimit &&
1319 tspace > dquot->dq_dqb.dqb_bhardlimit &&
1320 !ignore_hardlimit(dquot)) {
1321 if (flags & DQUOT_SPACE_WARN)
1322 prepare_warning(warn, dquot, QUOTA_NL_BHARDWARN);
1323 ret = -EDQUOT;
1324 goto finish;
1327 if (dquot->dq_dqb.dqb_bsoftlimit &&
1328 tspace > dquot->dq_dqb.dqb_bsoftlimit &&
1329 dquot->dq_dqb.dqb_btime &&
1330 ktime_get_real_seconds() >= dquot->dq_dqb.dqb_btime &&
1331 !ignore_hardlimit(dquot)) {
1332 if (flags & DQUOT_SPACE_WARN)
1333 prepare_warning(warn, dquot, QUOTA_NL_BSOFTLONGWARN);
1334 ret = -EDQUOT;
1335 goto finish;
1338 if (dquot->dq_dqb.dqb_bsoftlimit &&
1339 tspace > dquot->dq_dqb.dqb_bsoftlimit &&
1340 dquot->dq_dqb.dqb_btime == 0) {
1341 if (flags & DQUOT_SPACE_WARN) {
1342 prepare_warning(warn, dquot, QUOTA_NL_BSOFTWARN);
1343 dquot->dq_dqb.dqb_btime = ktime_get_real_seconds() +
1344 sb_dqopt(sb)->info[dquot->dq_id.type].dqi_bgrace;
1345 } else {
1347 * We don't allow preallocation to exceed softlimit so exceeding will
1348 * be always printed
1350 ret = -EDQUOT;
1351 goto finish;
1354 finish:
1356 * We have to be careful and go through warning generation & grace time
1357 * setting even if DQUOT_SPACE_NOFAIL is set. That's why we check it
1358 * only here...
1360 if (flags & DQUOT_SPACE_NOFAIL)
1361 ret = 0;
1362 if (!ret) {
1363 dquot->dq_dqb.dqb_rsvspace += rsv_space;
1364 dquot->dq_dqb.dqb_curspace += space;
1366 spin_unlock(&dquot->dq_dqb_lock);
1367 return ret;
1370 static int info_idq_free(struct dquot *dquot, qsize_t inodes)
1372 qsize_t newinodes;
1374 if (test_bit(DQ_FAKE_B, &dquot->dq_flags) ||
1375 dquot->dq_dqb.dqb_curinodes <= dquot->dq_dqb.dqb_isoftlimit ||
1376 !sb_has_quota_limits_enabled(dquot->dq_sb, dquot->dq_id.type))
1377 return QUOTA_NL_NOWARN;
1379 newinodes = dquot->dq_dqb.dqb_curinodes - inodes;
1380 if (newinodes <= dquot->dq_dqb.dqb_isoftlimit)
1381 return QUOTA_NL_ISOFTBELOW;
1382 if (dquot->dq_dqb.dqb_curinodes >= dquot->dq_dqb.dqb_ihardlimit &&
1383 newinodes < dquot->dq_dqb.dqb_ihardlimit)
1384 return QUOTA_NL_IHARDBELOW;
1385 return QUOTA_NL_NOWARN;
1388 static int info_bdq_free(struct dquot *dquot, qsize_t space)
1390 qsize_t tspace;
1392 tspace = dquot->dq_dqb.dqb_curspace + dquot->dq_dqb.dqb_rsvspace;
1394 if (test_bit(DQ_FAKE_B, &dquot->dq_flags) ||
1395 tspace <= dquot->dq_dqb.dqb_bsoftlimit)
1396 return QUOTA_NL_NOWARN;
1398 if (tspace - space <= dquot->dq_dqb.dqb_bsoftlimit)
1399 return QUOTA_NL_BSOFTBELOW;
1400 if (tspace >= dquot->dq_dqb.dqb_bhardlimit &&
1401 tspace - space < dquot->dq_dqb.dqb_bhardlimit)
1402 return QUOTA_NL_BHARDBELOW;
1403 return QUOTA_NL_NOWARN;
1406 static int dquot_active(const struct inode *inode)
1408 struct super_block *sb = inode->i_sb;
1410 if (IS_NOQUOTA(inode))
1411 return 0;
1412 return sb_any_quota_loaded(sb) & ~sb_any_quota_suspended(sb);
1416 * Initialize quota pointers in inode
1418 * It is better to call this function outside of any transaction as it
1419 * might need a lot of space in journal for dquot structure allocation.
1421 static int __dquot_initialize(struct inode *inode, int type)
1423 int cnt, init_needed = 0;
1424 struct dquot **dquots, *got[MAXQUOTAS] = {};
1425 struct super_block *sb = inode->i_sb;
1426 qsize_t rsv;
1427 int ret = 0;
1429 if (!dquot_active(inode))
1430 return 0;
1432 dquots = i_dquot(inode);
1434 /* First get references to structures we might need. */
1435 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1436 struct kqid qid;
1437 kprojid_t projid;
1438 int rc;
1439 struct dquot *dquot;
1441 if (type != -1 && cnt != type)
1442 continue;
1444 * The i_dquot should have been initialized in most cases,
1445 * we check it without locking here to avoid unnecessary
1446 * dqget()/dqput() calls.
1448 if (dquots[cnt])
1449 continue;
1451 if (!sb_has_quota_active(sb, cnt))
1452 continue;
1454 init_needed = 1;
1456 switch (cnt) {
1457 case USRQUOTA:
1458 qid = make_kqid_uid(inode->i_uid);
1459 break;
1460 case GRPQUOTA:
1461 qid = make_kqid_gid(inode->i_gid);
1462 break;
1463 case PRJQUOTA:
1464 rc = inode->i_sb->dq_op->get_projid(inode, &projid);
1465 if (rc)
1466 continue;
1467 qid = make_kqid_projid(projid);
1468 break;
1470 dquot = dqget(sb, qid);
1471 if (IS_ERR(dquot)) {
1472 /* We raced with somebody turning quotas off... */
1473 if (PTR_ERR(dquot) != -ESRCH) {
1474 ret = PTR_ERR(dquot);
1475 goto out_put;
1477 dquot = NULL;
1479 got[cnt] = dquot;
1482 /* All required i_dquot has been initialized */
1483 if (!init_needed)
1484 return 0;
1486 spin_lock(&dq_data_lock);
1487 if (IS_NOQUOTA(inode))
1488 goto out_lock;
1489 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1490 if (type != -1 && cnt != type)
1491 continue;
1492 /* Avoid races with quotaoff() */
1493 if (!sb_has_quota_active(sb, cnt))
1494 continue;
1495 /* We could race with quotaon or dqget() could have failed */
1496 if (!got[cnt])
1497 continue;
1498 if (!dquots[cnt]) {
1499 dquots[cnt] = got[cnt];
1500 got[cnt] = NULL;
1502 * Make quota reservation system happy if someone
1503 * did a write before quota was turned on
1505 rsv = inode_get_rsv_space(inode);
1506 if (unlikely(rsv)) {
1507 spin_lock(&inode->i_lock);
1508 /* Get reservation again under proper lock */
1509 rsv = __inode_get_rsv_space(inode);
1510 spin_lock(&dquots[cnt]->dq_dqb_lock);
1511 dquots[cnt]->dq_dqb.dqb_rsvspace += rsv;
1512 spin_unlock(&dquots[cnt]->dq_dqb_lock);
1513 spin_unlock(&inode->i_lock);
1517 out_lock:
1518 spin_unlock(&dq_data_lock);
1519 out_put:
1520 /* Drop unused references */
1521 dqput_all(got);
1523 return ret;
1526 int dquot_initialize(struct inode *inode)
1528 return __dquot_initialize(inode, -1);
1530 EXPORT_SYMBOL(dquot_initialize);
1532 bool dquot_initialize_needed(struct inode *inode)
1534 struct dquot **dquots;
1535 int i;
1537 if (!dquot_active(inode))
1538 return false;
1540 dquots = i_dquot(inode);
1541 for (i = 0; i < MAXQUOTAS; i++)
1542 if (!dquots[i] && sb_has_quota_active(inode->i_sb, i))
1543 return true;
1544 return false;
1546 EXPORT_SYMBOL(dquot_initialize_needed);
1549 * Release all quotas referenced by inode.
1551 * This function only be called on inode free or converting
1552 * a file to quota file, no other users for the i_dquot in
1553 * both cases, so we needn't call synchronize_srcu() after
1554 * clearing i_dquot.
1556 static void __dquot_drop(struct inode *inode)
1558 int cnt;
1559 struct dquot **dquots = i_dquot(inode);
1560 struct dquot *put[MAXQUOTAS];
1562 spin_lock(&dq_data_lock);
1563 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1564 put[cnt] = dquots[cnt];
1565 dquots[cnt] = NULL;
1567 spin_unlock(&dq_data_lock);
1568 dqput_all(put);
1571 void dquot_drop(struct inode *inode)
1573 struct dquot * const *dquots;
1574 int cnt;
1576 if (IS_NOQUOTA(inode))
1577 return;
1580 * Test before calling to rule out calls from proc and such
1581 * where we are not allowed to block. Note that this is
1582 * actually reliable test even without the lock - the caller
1583 * must assure that nobody can come after the DQUOT_DROP and
1584 * add quota pointers back anyway.
1586 dquots = i_dquot(inode);
1587 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1588 if (dquots[cnt])
1589 break;
1592 if (cnt < MAXQUOTAS)
1593 __dquot_drop(inode);
1595 EXPORT_SYMBOL(dquot_drop);
1598 * inode_reserved_space is managed internally by quota, and protected by
1599 * i_lock similar to i_blocks+i_bytes.
1601 static qsize_t *inode_reserved_space(struct inode * inode)
1603 /* Filesystem must explicitly define it's own method in order to use
1604 * quota reservation interface */
1605 BUG_ON(!inode->i_sb->dq_op->get_reserved_space);
1606 return inode->i_sb->dq_op->get_reserved_space(inode);
1609 static qsize_t __inode_get_rsv_space(struct inode *inode)
1611 if (!inode->i_sb->dq_op->get_reserved_space)
1612 return 0;
1613 return *inode_reserved_space(inode);
1616 static qsize_t inode_get_rsv_space(struct inode *inode)
1618 qsize_t ret;
1620 if (!inode->i_sb->dq_op->get_reserved_space)
1621 return 0;
1622 spin_lock(&inode->i_lock);
1623 ret = __inode_get_rsv_space(inode);
1624 spin_unlock(&inode->i_lock);
1625 return ret;
1629 * This functions updates i_blocks+i_bytes fields and quota information
1630 * (together with appropriate checks).
1632 * NOTE: We absolutely rely on the fact that caller dirties the inode
1633 * (usually helpers in quotaops.h care about this) and holds a handle for
1634 * the current transaction so that dquot write and inode write go into the
1635 * same transaction.
1639 * This operation can block, but only after everything is updated
1641 int __dquot_alloc_space(struct inode *inode, qsize_t number, int flags)
1643 int cnt, ret = 0, index;
1644 struct dquot_warn warn[MAXQUOTAS];
1645 int reserve = flags & DQUOT_SPACE_RESERVE;
1646 struct dquot **dquots;
1648 if (!dquot_active(inode)) {
1649 if (reserve) {
1650 spin_lock(&inode->i_lock);
1651 *inode_reserved_space(inode) += number;
1652 spin_unlock(&inode->i_lock);
1653 } else {
1654 inode_add_bytes(inode, number);
1656 goto out;
1659 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1660 warn[cnt].w_type = QUOTA_NL_NOWARN;
1662 dquots = i_dquot(inode);
1663 index = srcu_read_lock(&dquot_srcu);
1664 spin_lock(&inode->i_lock);
1665 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1666 if (!dquots[cnt])
1667 continue;
1668 if (flags & DQUOT_SPACE_RESERVE) {
1669 ret = dquot_add_space(dquots[cnt], 0, number, flags,
1670 &warn[cnt]);
1671 } else {
1672 ret = dquot_add_space(dquots[cnt], number, 0, flags,
1673 &warn[cnt]);
1675 if (ret) {
1676 /* Back out changes we already did */
1677 for (cnt--; cnt >= 0; cnt--) {
1678 if (!dquots[cnt])
1679 continue;
1680 spin_lock(&dquots[cnt]->dq_dqb_lock);
1681 if (flags & DQUOT_SPACE_RESERVE) {
1682 dquots[cnt]->dq_dqb.dqb_rsvspace -=
1683 number;
1684 } else {
1685 dquots[cnt]->dq_dqb.dqb_curspace -=
1686 number;
1688 spin_unlock(&dquots[cnt]->dq_dqb_lock);
1690 spin_unlock(&inode->i_lock);
1691 goto out_flush_warn;
1694 if (reserve)
1695 *inode_reserved_space(inode) += number;
1696 else
1697 __inode_add_bytes(inode, number);
1698 spin_unlock(&inode->i_lock);
1700 if (reserve)
1701 goto out_flush_warn;
1702 mark_all_dquot_dirty(dquots);
1703 out_flush_warn:
1704 srcu_read_unlock(&dquot_srcu, index);
1705 flush_warnings(warn);
1706 out:
1707 return ret;
1709 EXPORT_SYMBOL(__dquot_alloc_space);
1712 * This operation can block, but only after everything is updated
1714 int dquot_alloc_inode(struct inode *inode)
1716 int cnt, ret = 0, index;
1717 struct dquot_warn warn[MAXQUOTAS];
1718 struct dquot * const *dquots;
1720 if (!dquot_active(inode))
1721 return 0;
1722 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1723 warn[cnt].w_type = QUOTA_NL_NOWARN;
1725 dquots = i_dquot(inode);
1726 index = srcu_read_lock(&dquot_srcu);
1727 spin_lock(&inode->i_lock);
1728 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1729 if (!dquots[cnt])
1730 continue;
1731 ret = dquot_add_inodes(dquots[cnt], 1, &warn[cnt]);
1732 if (ret) {
1733 for (cnt--; cnt >= 0; cnt--) {
1734 if (!dquots[cnt])
1735 continue;
1736 /* Back out changes we already did */
1737 spin_lock(&dquots[cnt]->dq_dqb_lock);
1738 dquots[cnt]->dq_dqb.dqb_curinodes--;
1739 spin_unlock(&dquots[cnt]->dq_dqb_lock);
1741 goto warn_put_all;
1745 warn_put_all:
1746 spin_unlock(&inode->i_lock);
1747 if (ret == 0)
1748 mark_all_dquot_dirty(dquots);
1749 srcu_read_unlock(&dquot_srcu, index);
1750 flush_warnings(warn);
1751 return ret;
1753 EXPORT_SYMBOL(dquot_alloc_inode);
1756 * Convert in-memory reserved quotas to real consumed quotas
1758 int dquot_claim_space_nodirty(struct inode *inode, qsize_t number)
1760 struct dquot **dquots;
1761 int cnt, index;
1763 if (!dquot_active(inode)) {
1764 spin_lock(&inode->i_lock);
1765 *inode_reserved_space(inode) -= number;
1766 __inode_add_bytes(inode, number);
1767 spin_unlock(&inode->i_lock);
1768 return 0;
1771 dquots = i_dquot(inode);
1772 index = srcu_read_lock(&dquot_srcu);
1773 spin_lock(&inode->i_lock);
1774 /* Claim reserved quotas to allocated quotas */
1775 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1776 if (dquots[cnt]) {
1777 struct dquot *dquot = dquots[cnt];
1779 spin_lock(&dquot->dq_dqb_lock);
1780 if (WARN_ON_ONCE(dquot->dq_dqb.dqb_rsvspace < number))
1781 number = dquot->dq_dqb.dqb_rsvspace;
1782 dquot->dq_dqb.dqb_curspace += number;
1783 dquot->dq_dqb.dqb_rsvspace -= number;
1784 spin_unlock(&dquot->dq_dqb_lock);
1787 /* Update inode bytes */
1788 *inode_reserved_space(inode) -= number;
1789 __inode_add_bytes(inode, number);
1790 spin_unlock(&inode->i_lock);
1791 mark_all_dquot_dirty(dquots);
1792 srcu_read_unlock(&dquot_srcu, index);
1793 return 0;
1795 EXPORT_SYMBOL(dquot_claim_space_nodirty);
1798 * Convert allocated space back to in-memory reserved quotas
1800 void dquot_reclaim_space_nodirty(struct inode *inode, qsize_t number)
1802 struct dquot **dquots;
1803 int cnt, index;
1805 if (!dquot_active(inode)) {
1806 spin_lock(&inode->i_lock);
1807 *inode_reserved_space(inode) += number;
1808 __inode_sub_bytes(inode, number);
1809 spin_unlock(&inode->i_lock);
1810 return;
1813 dquots = i_dquot(inode);
1814 index = srcu_read_lock(&dquot_srcu);
1815 spin_lock(&inode->i_lock);
1816 /* Claim reserved quotas to allocated quotas */
1817 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1818 if (dquots[cnt]) {
1819 struct dquot *dquot = dquots[cnt];
1821 spin_lock(&dquot->dq_dqb_lock);
1822 if (WARN_ON_ONCE(dquot->dq_dqb.dqb_curspace < number))
1823 number = dquot->dq_dqb.dqb_curspace;
1824 dquot->dq_dqb.dqb_rsvspace += number;
1825 dquot->dq_dqb.dqb_curspace -= number;
1826 spin_unlock(&dquot->dq_dqb_lock);
1829 /* Update inode bytes */
1830 *inode_reserved_space(inode) += number;
1831 __inode_sub_bytes(inode, number);
1832 spin_unlock(&inode->i_lock);
1833 mark_all_dquot_dirty(dquots);
1834 srcu_read_unlock(&dquot_srcu, index);
1835 return;
1837 EXPORT_SYMBOL(dquot_reclaim_space_nodirty);
1840 * This operation can block, but only after everything is updated
1842 void __dquot_free_space(struct inode *inode, qsize_t number, int flags)
1844 unsigned int cnt;
1845 struct dquot_warn warn[MAXQUOTAS];
1846 struct dquot **dquots;
1847 int reserve = flags & DQUOT_SPACE_RESERVE, index;
1849 if (!dquot_active(inode)) {
1850 if (reserve) {
1851 spin_lock(&inode->i_lock);
1852 *inode_reserved_space(inode) -= number;
1853 spin_unlock(&inode->i_lock);
1854 } else {
1855 inode_sub_bytes(inode, number);
1857 return;
1860 dquots = i_dquot(inode);
1861 index = srcu_read_lock(&dquot_srcu);
1862 spin_lock(&inode->i_lock);
1863 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1864 int wtype;
1866 warn[cnt].w_type = QUOTA_NL_NOWARN;
1867 if (!dquots[cnt])
1868 continue;
1869 spin_lock(&dquots[cnt]->dq_dqb_lock);
1870 wtype = info_bdq_free(dquots[cnt], number);
1871 if (wtype != QUOTA_NL_NOWARN)
1872 prepare_warning(&warn[cnt], dquots[cnt], wtype);
1873 if (reserve)
1874 dquot_free_reserved_space(dquots[cnt], number);
1875 else
1876 dquot_decr_space(dquots[cnt], number);
1877 spin_unlock(&dquots[cnt]->dq_dqb_lock);
1879 if (reserve)
1880 *inode_reserved_space(inode) -= number;
1881 else
1882 __inode_sub_bytes(inode, number);
1883 spin_unlock(&inode->i_lock);
1885 if (reserve)
1886 goto out_unlock;
1887 mark_all_dquot_dirty(dquots);
1888 out_unlock:
1889 srcu_read_unlock(&dquot_srcu, index);
1890 flush_warnings(warn);
1892 EXPORT_SYMBOL(__dquot_free_space);
1895 * This operation can block, but only after everything is updated
1897 void dquot_free_inode(struct inode *inode)
1899 unsigned int cnt;
1900 struct dquot_warn warn[MAXQUOTAS];
1901 struct dquot * const *dquots;
1902 int index;
1904 if (!dquot_active(inode))
1905 return;
1907 dquots = i_dquot(inode);
1908 index = srcu_read_lock(&dquot_srcu);
1909 spin_lock(&inode->i_lock);
1910 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1911 int wtype;
1913 warn[cnt].w_type = QUOTA_NL_NOWARN;
1914 if (!dquots[cnt])
1915 continue;
1916 spin_lock(&dquots[cnt]->dq_dqb_lock);
1917 wtype = info_idq_free(dquots[cnt], 1);
1918 if (wtype != QUOTA_NL_NOWARN)
1919 prepare_warning(&warn[cnt], dquots[cnt], wtype);
1920 dquot_decr_inodes(dquots[cnt], 1);
1921 spin_unlock(&dquots[cnt]->dq_dqb_lock);
1923 spin_unlock(&inode->i_lock);
1924 mark_all_dquot_dirty(dquots);
1925 srcu_read_unlock(&dquot_srcu, index);
1926 flush_warnings(warn);
1928 EXPORT_SYMBOL(dquot_free_inode);
1931 * Transfer the number of inode and blocks from one diskquota to an other.
1932 * On success, dquot references in transfer_to are consumed and references
1933 * to original dquots that need to be released are placed there. On failure,
1934 * references are kept untouched.
1936 * This operation can block, but only after everything is updated
1937 * A transaction must be started when entering this function.
1939 * We are holding reference on transfer_from & transfer_to, no need to
1940 * protect them by srcu_read_lock().
1942 int __dquot_transfer(struct inode *inode, struct dquot **transfer_to)
1944 qsize_t cur_space;
1945 qsize_t rsv_space = 0;
1946 qsize_t inode_usage = 1;
1947 struct dquot *transfer_from[MAXQUOTAS] = {};
1948 int cnt, ret = 0;
1949 char is_valid[MAXQUOTAS] = {};
1950 struct dquot_warn warn_to[MAXQUOTAS];
1951 struct dquot_warn warn_from_inodes[MAXQUOTAS];
1952 struct dquot_warn warn_from_space[MAXQUOTAS];
1954 if (IS_NOQUOTA(inode))
1955 return 0;
1957 if (inode->i_sb->dq_op->get_inode_usage) {
1958 ret = inode->i_sb->dq_op->get_inode_usage(inode, &inode_usage);
1959 if (ret)
1960 return ret;
1963 /* Initialize the arrays */
1964 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1965 warn_to[cnt].w_type = QUOTA_NL_NOWARN;
1966 warn_from_inodes[cnt].w_type = QUOTA_NL_NOWARN;
1967 warn_from_space[cnt].w_type = QUOTA_NL_NOWARN;
1970 spin_lock(&dq_data_lock);
1971 spin_lock(&inode->i_lock);
1972 if (IS_NOQUOTA(inode)) { /* File without quota accounting? */
1973 spin_unlock(&inode->i_lock);
1974 spin_unlock(&dq_data_lock);
1975 return 0;
1977 cur_space = __inode_get_bytes(inode);
1978 rsv_space = __inode_get_rsv_space(inode);
1980 * Build the transfer_from list, check limits, and update usage in
1981 * the target structures.
1983 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1985 * Skip changes for same uid or gid or for turned off quota-type.
1987 if (!transfer_to[cnt])
1988 continue;
1989 /* Avoid races with quotaoff() */
1990 if (!sb_has_quota_active(inode->i_sb, cnt))
1991 continue;
1992 is_valid[cnt] = 1;
1993 transfer_from[cnt] = i_dquot(inode)[cnt];
1994 ret = dquot_add_inodes(transfer_to[cnt], inode_usage,
1995 &warn_to[cnt]);
1996 if (ret)
1997 goto over_quota;
1998 ret = dquot_add_space(transfer_to[cnt], cur_space, rsv_space,
1999 DQUOT_SPACE_WARN, &warn_to[cnt]);
2000 if (ret) {
2001 spin_lock(&transfer_to[cnt]->dq_dqb_lock);
2002 dquot_decr_inodes(transfer_to[cnt], inode_usage);
2003 spin_unlock(&transfer_to[cnt]->dq_dqb_lock);
2004 goto over_quota;
2008 /* Decrease usage for source structures and update quota pointers */
2009 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
2010 if (!is_valid[cnt])
2011 continue;
2012 /* Due to IO error we might not have transfer_from[] structure */
2013 if (transfer_from[cnt]) {
2014 int wtype;
2016 spin_lock(&transfer_from[cnt]->dq_dqb_lock);
2017 wtype = info_idq_free(transfer_from[cnt], inode_usage);
2018 if (wtype != QUOTA_NL_NOWARN)
2019 prepare_warning(&warn_from_inodes[cnt],
2020 transfer_from[cnt], wtype);
2021 wtype = info_bdq_free(transfer_from[cnt],
2022 cur_space + rsv_space);
2023 if (wtype != QUOTA_NL_NOWARN)
2024 prepare_warning(&warn_from_space[cnt],
2025 transfer_from[cnt], wtype);
2026 dquot_decr_inodes(transfer_from[cnt], inode_usage);
2027 dquot_decr_space(transfer_from[cnt], cur_space);
2028 dquot_free_reserved_space(transfer_from[cnt],
2029 rsv_space);
2030 spin_unlock(&transfer_from[cnt]->dq_dqb_lock);
2032 i_dquot(inode)[cnt] = transfer_to[cnt];
2034 spin_unlock(&inode->i_lock);
2035 spin_unlock(&dq_data_lock);
2037 mark_all_dquot_dirty(transfer_from);
2038 mark_all_dquot_dirty(transfer_to);
2039 flush_warnings(warn_to);
2040 flush_warnings(warn_from_inodes);
2041 flush_warnings(warn_from_space);
2042 /* Pass back references to put */
2043 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
2044 if (is_valid[cnt])
2045 transfer_to[cnt] = transfer_from[cnt];
2046 return 0;
2047 over_quota:
2048 /* Back out changes we already did */
2049 for (cnt--; cnt >= 0; cnt--) {
2050 if (!is_valid[cnt])
2051 continue;
2052 spin_lock(&transfer_to[cnt]->dq_dqb_lock);
2053 dquot_decr_inodes(transfer_to[cnt], inode_usage);
2054 dquot_decr_space(transfer_to[cnt], cur_space);
2055 dquot_free_reserved_space(transfer_to[cnt], rsv_space);
2056 spin_unlock(&transfer_to[cnt]->dq_dqb_lock);
2058 spin_unlock(&inode->i_lock);
2059 spin_unlock(&dq_data_lock);
2060 flush_warnings(warn_to);
2061 return ret;
2063 EXPORT_SYMBOL(__dquot_transfer);
2065 /* Wrapper for transferring ownership of an inode for uid/gid only
2066 * Called from FSXXX_setattr()
2068 int dquot_transfer(struct inode *inode, struct iattr *iattr)
2070 struct dquot *transfer_to[MAXQUOTAS] = {};
2071 struct dquot *dquot;
2072 struct super_block *sb = inode->i_sb;
2073 int ret;
2075 if (!dquot_active(inode))
2076 return 0;
2078 if (iattr->ia_valid & ATTR_UID && !uid_eq(iattr->ia_uid, inode->i_uid)){
2079 dquot = dqget(sb, make_kqid_uid(iattr->ia_uid));
2080 if (IS_ERR(dquot)) {
2081 if (PTR_ERR(dquot) != -ESRCH) {
2082 ret = PTR_ERR(dquot);
2083 goto out_put;
2085 dquot = NULL;
2087 transfer_to[USRQUOTA] = dquot;
2089 if (iattr->ia_valid & ATTR_GID && !gid_eq(iattr->ia_gid, inode->i_gid)){
2090 dquot = dqget(sb, make_kqid_gid(iattr->ia_gid));
2091 if (IS_ERR(dquot)) {
2092 if (PTR_ERR(dquot) != -ESRCH) {
2093 ret = PTR_ERR(dquot);
2094 goto out_put;
2096 dquot = NULL;
2098 transfer_to[GRPQUOTA] = dquot;
2100 ret = __dquot_transfer(inode, transfer_to);
2101 out_put:
2102 dqput_all(transfer_to);
2103 return ret;
2105 EXPORT_SYMBOL(dquot_transfer);
2108 * Write info of quota file to disk
2110 int dquot_commit_info(struct super_block *sb, int type)
2112 struct quota_info *dqopt = sb_dqopt(sb);
2114 return dqopt->ops[type]->write_file_info(sb, type);
2116 EXPORT_SYMBOL(dquot_commit_info);
2118 int dquot_get_next_id(struct super_block *sb, struct kqid *qid)
2120 struct quota_info *dqopt = sb_dqopt(sb);
2122 if (!sb_has_quota_active(sb, qid->type))
2123 return -ESRCH;
2124 if (!dqopt->ops[qid->type]->get_next_id)
2125 return -ENOSYS;
2126 return dqopt->ops[qid->type]->get_next_id(sb, qid);
2128 EXPORT_SYMBOL(dquot_get_next_id);
2131 * Definitions of diskquota operations.
2133 const struct dquot_operations dquot_operations = {
2134 .write_dquot = dquot_commit,
2135 .acquire_dquot = dquot_acquire,
2136 .release_dquot = dquot_release,
2137 .mark_dirty = dquot_mark_dquot_dirty,
2138 .write_info = dquot_commit_info,
2139 .alloc_dquot = dquot_alloc,
2140 .destroy_dquot = dquot_destroy,
2141 .get_next_id = dquot_get_next_id,
2143 EXPORT_SYMBOL(dquot_operations);
2146 * Generic helper for ->open on filesystems supporting disk quotas.
2148 int dquot_file_open(struct inode *inode, struct file *file)
2150 int error;
2152 error = generic_file_open(inode, file);
2153 if (!error && (file->f_mode & FMODE_WRITE))
2154 error = dquot_initialize(inode);
2155 return error;
2157 EXPORT_SYMBOL(dquot_file_open);
2160 * Turn quota off on a device. type == -1 ==> quotaoff for all types (umount)
2162 int dquot_disable(struct super_block *sb, int type, unsigned int flags)
2164 int cnt, ret = 0;
2165 struct quota_info *dqopt = sb_dqopt(sb);
2166 struct inode *toputinode[MAXQUOTAS];
2168 /* s_umount should be held in exclusive mode */
2169 if (WARN_ON_ONCE(down_read_trylock(&sb->s_umount)))
2170 up_read(&sb->s_umount);
2172 /* Cannot turn off usage accounting without turning off limits, or
2173 * suspend quotas and simultaneously turn quotas off. */
2174 if ((flags & DQUOT_USAGE_ENABLED && !(flags & DQUOT_LIMITS_ENABLED))
2175 || (flags & DQUOT_SUSPENDED && flags & (DQUOT_LIMITS_ENABLED |
2176 DQUOT_USAGE_ENABLED)))
2177 return -EINVAL;
2180 * Skip everything if there's nothing to do. We have to do this because
2181 * sometimes we are called when fill_super() failed and calling
2182 * sync_fs() in such cases does no good.
2184 if (!sb_any_quota_loaded(sb))
2185 return 0;
2187 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
2188 toputinode[cnt] = NULL;
2189 if (type != -1 && cnt != type)
2190 continue;
2191 if (!sb_has_quota_loaded(sb, cnt))
2192 continue;
2194 if (flags & DQUOT_SUSPENDED) {
2195 spin_lock(&dq_state_lock);
2196 dqopt->flags |=
2197 dquot_state_flag(DQUOT_SUSPENDED, cnt);
2198 spin_unlock(&dq_state_lock);
2199 } else {
2200 spin_lock(&dq_state_lock);
2201 dqopt->flags &= ~dquot_state_flag(flags, cnt);
2202 /* Turning off suspended quotas? */
2203 if (!sb_has_quota_loaded(sb, cnt) &&
2204 sb_has_quota_suspended(sb, cnt)) {
2205 dqopt->flags &= ~dquot_state_flag(
2206 DQUOT_SUSPENDED, cnt);
2207 spin_unlock(&dq_state_lock);
2208 iput(dqopt->files[cnt]);
2209 dqopt->files[cnt] = NULL;
2210 continue;
2212 spin_unlock(&dq_state_lock);
2215 /* We still have to keep quota loaded? */
2216 if (sb_has_quota_loaded(sb, cnt) && !(flags & DQUOT_SUSPENDED))
2217 continue;
2219 /* Note: these are blocking operations */
2220 drop_dquot_ref(sb, cnt);
2221 invalidate_dquots(sb, cnt);
2223 * Now all dquots should be invalidated, all writes done so we
2224 * should be only users of the info. No locks needed.
2226 if (info_dirty(&dqopt->info[cnt]))
2227 sb->dq_op->write_info(sb, cnt);
2228 if (dqopt->ops[cnt]->free_file_info)
2229 dqopt->ops[cnt]->free_file_info(sb, cnt);
2230 put_quota_format(dqopt->info[cnt].dqi_format);
2232 toputinode[cnt] = dqopt->files[cnt];
2233 if (!sb_has_quota_loaded(sb, cnt))
2234 dqopt->files[cnt] = NULL;
2235 dqopt->info[cnt].dqi_flags = 0;
2236 dqopt->info[cnt].dqi_igrace = 0;
2237 dqopt->info[cnt].dqi_bgrace = 0;
2238 dqopt->ops[cnt] = NULL;
2241 /* Skip syncing and setting flags if quota files are hidden */
2242 if (dqopt->flags & DQUOT_QUOTA_SYS_FILE)
2243 goto put_inodes;
2245 /* Sync the superblock so that buffers with quota data are written to
2246 * disk (and so userspace sees correct data afterwards). */
2247 if (sb->s_op->sync_fs)
2248 sb->s_op->sync_fs(sb, 1);
2249 sync_blockdev(sb->s_bdev);
2250 /* Now the quota files are just ordinary files and we can set the
2251 * inode flags back. Moreover we discard the pagecache so that
2252 * userspace sees the writes we did bypassing the pagecache. We
2253 * must also discard the blockdev buffers so that we see the
2254 * changes done by userspace on the next quotaon() */
2255 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
2256 /* This can happen when suspending quotas on remount-ro... */
2257 if (toputinode[cnt] && !sb_has_quota_loaded(sb, cnt)) {
2258 inode_lock(toputinode[cnt]);
2259 toputinode[cnt]->i_flags &= ~S_NOQUOTA;
2260 truncate_inode_pages(&toputinode[cnt]->i_data, 0);
2261 inode_unlock(toputinode[cnt]);
2262 mark_inode_dirty_sync(toputinode[cnt]);
2264 if (sb->s_bdev)
2265 invalidate_bdev(sb->s_bdev);
2266 put_inodes:
2267 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
2268 if (toputinode[cnt]) {
2269 /* On remount RO, we keep the inode pointer so that we
2270 * can reenable quota on the subsequent remount RW. We
2271 * have to check 'flags' variable and not use sb_has_
2272 * function because another quotaon / quotaoff could
2273 * change global state before we got here. We refuse
2274 * to suspend quotas when there is pending delete on
2275 * the quota file... */
2276 if (!(flags & DQUOT_SUSPENDED))
2277 iput(toputinode[cnt]);
2278 else if (!toputinode[cnt]->i_nlink)
2279 ret = -EBUSY;
2281 return ret;
2283 EXPORT_SYMBOL(dquot_disable);
2285 int dquot_quota_off(struct super_block *sb, int type)
2287 return dquot_disable(sb, type,
2288 DQUOT_USAGE_ENABLED | DQUOT_LIMITS_ENABLED);
2290 EXPORT_SYMBOL(dquot_quota_off);
2293 * Turn quotas on on a device
2297 * Helper function to turn quotas on when we already have the inode of
2298 * quota file and no quota information is loaded.
2300 static int vfs_load_quota_inode(struct inode *inode, int type, int format_id,
2301 unsigned int flags)
2303 struct quota_format_type *fmt = find_quota_format(format_id);
2304 struct super_block *sb = inode->i_sb;
2305 struct quota_info *dqopt = sb_dqopt(sb);
2306 int error;
2308 if (!fmt)
2309 return -ESRCH;
2310 if (!S_ISREG(inode->i_mode)) {
2311 error = -EACCES;
2312 goto out_fmt;
2314 if (IS_RDONLY(inode)) {
2315 error = -EROFS;
2316 goto out_fmt;
2318 if (!sb->s_op->quota_write || !sb->s_op->quota_read ||
2319 (type == PRJQUOTA && sb->dq_op->get_projid == NULL)) {
2320 error = -EINVAL;
2321 goto out_fmt;
2323 /* Filesystems outside of init_user_ns not yet supported */
2324 if (sb->s_user_ns != &init_user_ns) {
2325 error = -EINVAL;
2326 goto out_fmt;
2328 /* Usage always has to be set... */
2329 if (!(flags & DQUOT_USAGE_ENABLED)) {
2330 error = -EINVAL;
2331 goto out_fmt;
2333 if (sb_has_quota_loaded(sb, type)) {
2334 error = -EBUSY;
2335 goto out_fmt;
2338 if (!(dqopt->flags & DQUOT_QUOTA_SYS_FILE)) {
2339 /* As we bypass the pagecache we must now flush all the
2340 * dirty data and invalidate caches so that kernel sees
2341 * changes from userspace. It is not enough to just flush
2342 * the quota file since if blocksize < pagesize, invalidation
2343 * of the cache could fail because of other unrelated dirty
2344 * data */
2345 sync_filesystem(sb);
2346 invalidate_bdev(sb->s_bdev);
2349 if (!(dqopt->flags & DQUOT_QUOTA_SYS_FILE)) {
2350 /* We don't want quota and atime on quota files (deadlocks
2351 * possible) Also nobody should write to the file - we use
2352 * special IO operations which ignore the immutable bit. */
2353 inode_lock(inode);
2354 inode->i_flags |= S_NOQUOTA;
2355 inode_unlock(inode);
2357 * When S_NOQUOTA is set, remove dquot references as no more
2358 * references can be added
2360 __dquot_drop(inode);
2363 error = -EIO;
2364 dqopt->files[type] = igrab(inode);
2365 if (!dqopt->files[type])
2366 goto out_file_flags;
2367 error = -EINVAL;
2368 if (!fmt->qf_ops->check_quota_file(sb, type))
2369 goto out_file_init;
2371 dqopt->ops[type] = fmt->qf_ops;
2372 dqopt->info[type].dqi_format = fmt;
2373 dqopt->info[type].dqi_fmt_id = format_id;
2374 INIT_LIST_HEAD(&dqopt->info[type].dqi_dirty_list);
2375 error = dqopt->ops[type]->read_file_info(sb, type);
2376 if (error < 0)
2377 goto out_file_init;
2378 if (dqopt->flags & DQUOT_QUOTA_SYS_FILE) {
2379 spin_lock(&dq_data_lock);
2380 dqopt->info[type].dqi_flags |= DQF_SYS_FILE;
2381 spin_unlock(&dq_data_lock);
2383 spin_lock(&dq_state_lock);
2384 dqopt->flags |= dquot_state_flag(flags, type);
2385 spin_unlock(&dq_state_lock);
2387 error = add_dquot_ref(sb, type);
2388 if (error)
2389 dquot_disable(sb, type, flags);
2391 return error;
2392 out_file_init:
2393 dqopt->files[type] = NULL;
2394 iput(inode);
2395 out_file_flags:
2396 inode_lock(inode);
2397 inode->i_flags &= ~S_NOQUOTA;
2398 inode_unlock(inode);
2399 out_fmt:
2400 put_quota_format(fmt);
2402 return error;
2405 /* Reenable quotas on remount RW */
2406 int dquot_resume(struct super_block *sb, int type)
2408 struct quota_info *dqopt = sb_dqopt(sb);
2409 struct inode *inode;
2410 int ret = 0, cnt;
2411 unsigned int flags;
2413 /* s_umount should be held in exclusive mode */
2414 if (WARN_ON_ONCE(down_read_trylock(&sb->s_umount)))
2415 up_read(&sb->s_umount);
2417 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
2418 if (type != -1 && cnt != type)
2419 continue;
2420 if (!sb_has_quota_suspended(sb, cnt))
2421 continue;
2423 inode = dqopt->files[cnt];
2424 dqopt->files[cnt] = NULL;
2425 spin_lock(&dq_state_lock);
2426 flags = dqopt->flags & dquot_state_flag(DQUOT_USAGE_ENABLED |
2427 DQUOT_LIMITS_ENABLED,
2428 cnt);
2429 dqopt->flags &= ~dquot_state_flag(DQUOT_STATE_FLAGS, cnt);
2430 spin_unlock(&dq_state_lock);
2432 flags = dquot_generic_flag(flags, cnt);
2433 ret = vfs_load_quota_inode(inode, cnt,
2434 dqopt->info[cnt].dqi_fmt_id, flags);
2435 iput(inode);
2438 return ret;
2440 EXPORT_SYMBOL(dquot_resume);
2442 int dquot_quota_on(struct super_block *sb, int type, int format_id,
2443 const struct path *path)
2445 int error = security_quota_on(path->dentry);
2446 if (error)
2447 return error;
2448 /* Quota file not on the same filesystem? */
2449 if (path->dentry->d_sb != sb)
2450 error = -EXDEV;
2451 else
2452 error = vfs_load_quota_inode(d_inode(path->dentry), type,
2453 format_id, DQUOT_USAGE_ENABLED |
2454 DQUOT_LIMITS_ENABLED);
2455 return error;
2457 EXPORT_SYMBOL(dquot_quota_on);
2460 * More powerful function for turning on quotas allowing setting
2461 * of individual quota flags
2463 int dquot_enable(struct inode *inode, int type, int format_id,
2464 unsigned int flags)
2466 struct super_block *sb = inode->i_sb;
2468 /* Just unsuspend quotas? */
2469 BUG_ON(flags & DQUOT_SUSPENDED);
2470 /* s_umount should be held in exclusive mode */
2471 if (WARN_ON_ONCE(down_read_trylock(&sb->s_umount)))
2472 up_read(&sb->s_umount);
2474 if (!flags)
2475 return 0;
2476 /* Just updating flags needed? */
2477 if (sb_has_quota_loaded(sb, type)) {
2478 if (flags & DQUOT_USAGE_ENABLED &&
2479 sb_has_quota_usage_enabled(sb, type))
2480 return -EBUSY;
2481 if (flags & DQUOT_LIMITS_ENABLED &&
2482 sb_has_quota_limits_enabled(sb, type))
2483 return -EBUSY;
2484 spin_lock(&dq_state_lock);
2485 sb_dqopt(sb)->flags |= dquot_state_flag(flags, type);
2486 spin_unlock(&dq_state_lock);
2487 return 0;
2490 return vfs_load_quota_inode(inode, type, format_id, flags);
2492 EXPORT_SYMBOL(dquot_enable);
2495 * This function is used when filesystem needs to initialize quotas
2496 * during mount time.
2498 int dquot_quota_on_mount(struct super_block *sb, char *qf_name,
2499 int format_id, int type)
2501 struct dentry *dentry;
2502 int error;
2504 dentry = lookup_one_len_unlocked(qf_name, sb->s_root, strlen(qf_name));
2505 if (IS_ERR(dentry))
2506 return PTR_ERR(dentry);
2508 if (d_really_is_negative(dentry)) {
2509 error = -ENOENT;
2510 goto out;
2513 error = security_quota_on(dentry);
2514 if (!error)
2515 error = vfs_load_quota_inode(d_inode(dentry), type, format_id,
2516 DQUOT_USAGE_ENABLED | DQUOT_LIMITS_ENABLED);
2518 out:
2519 dput(dentry);
2520 return error;
2522 EXPORT_SYMBOL(dquot_quota_on_mount);
2524 static int dquot_quota_enable(struct super_block *sb, unsigned int flags)
2526 int ret;
2527 int type;
2528 struct quota_info *dqopt = sb_dqopt(sb);
2530 if (!(dqopt->flags & DQUOT_QUOTA_SYS_FILE))
2531 return -ENOSYS;
2532 /* Accounting cannot be turned on while fs is mounted */
2533 flags &= ~(FS_QUOTA_UDQ_ACCT | FS_QUOTA_GDQ_ACCT | FS_QUOTA_PDQ_ACCT);
2534 if (!flags)
2535 return -EINVAL;
2536 for (type = 0; type < MAXQUOTAS; type++) {
2537 if (!(flags & qtype_enforce_flag(type)))
2538 continue;
2539 /* Can't enforce without accounting */
2540 if (!sb_has_quota_usage_enabled(sb, type))
2541 return -EINVAL;
2542 ret = dquot_enable(dqopt->files[type], type,
2543 dqopt->info[type].dqi_fmt_id,
2544 DQUOT_LIMITS_ENABLED);
2545 if (ret < 0)
2546 goto out_err;
2548 return 0;
2549 out_err:
2550 /* Backout enforcement enablement we already did */
2551 for (type--; type >= 0; type--) {
2552 if (flags & qtype_enforce_flag(type))
2553 dquot_disable(sb, type, DQUOT_LIMITS_ENABLED);
2555 /* Error code translation for better compatibility with XFS */
2556 if (ret == -EBUSY)
2557 ret = -EEXIST;
2558 return ret;
2561 static int dquot_quota_disable(struct super_block *sb, unsigned int flags)
2563 int ret;
2564 int type;
2565 struct quota_info *dqopt = sb_dqopt(sb);
2567 if (!(dqopt->flags & DQUOT_QUOTA_SYS_FILE))
2568 return -ENOSYS;
2570 * We don't support turning off accounting via quotactl. In principle
2571 * quota infrastructure can do this but filesystems don't expect
2572 * userspace to be able to do it.
2574 if (flags &
2575 (FS_QUOTA_UDQ_ACCT | FS_QUOTA_GDQ_ACCT | FS_QUOTA_PDQ_ACCT))
2576 return -EOPNOTSUPP;
2578 /* Filter out limits not enabled */
2579 for (type = 0; type < MAXQUOTAS; type++)
2580 if (!sb_has_quota_limits_enabled(sb, type))
2581 flags &= ~qtype_enforce_flag(type);
2582 /* Nothing left? */
2583 if (!flags)
2584 return -EEXIST;
2585 for (type = 0; type < MAXQUOTAS; type++) {
2586 if (flags & qtype_enforce_flag(type)) {
2587 ret = dquot_disable(sb, type, DQUOT_LIMITS_ENABLED);
2588 if (ret < 0)
2589 goto out_err;
2592 return 0;
2593 out_err:
2594 /* Backout enforcement disabling we already did */
2595 for (type--; type >= 0; type--) {
2596 if (flags & qtype_enforce_flag(type))
2597 dquot_enable(dqopt->files[type], type,
2598 dqopt->info[type].dqi_fmt_id,
2599 DQUOT_LIMITS_ENABLED);
2601 return ret;
2604 /* Generic routine for getting common part of quota structure */
2605 static void do_get_dqblk(struct dquot *dquot, struct qc_dqblk *di)
2607 struct mem_dqblk *dm = &dquot->dq_dqb;
2609 memset(di, 0, sizeof(*di));
2610 spin_lock(&dquot->dq_dqb_lock);
2611 di->d_spc_hardlimit = dm->dqb_bhardlimit;
2612 di->d_spc_softlimit = dm->dqb_bsoftlimit;
2613 di->d_ino_hardlimit = dm->dqb_ihardlimit;
2614 di->d_ino_softlimit = dm->dqb_isoftlimit;
2615 di->d_space = dm->dqb_curspace + dm->dqb_rsvspace;
2616 di->d_ino_count = dm->dqb_curinodes;
2617 di->d_spc_timer = dm->dqb_btime;
2618 di->d_ino_timer = dm->dqb_itime;
2619 spin_unlock(&dquot->dq_dqb_lock);
2622 int dquot_get_dqblk(struct super_block *sb, struct kqid qid,
2623 struct qc_dqblk *di)
2625 struct dquot *dquot;
2627 dquot = dqget(sb, qid);
2628 if (IS_ERR(dquot))
2629 return PTR_ERR(dquot);
2630 do_get_dqblk(dquot, di);
2631 dqput(dquot);
2633 return 0;
2635 EXPORT_SYMBOL(dquot_get_dqblk);
2637 int dquot_get_next_dqblk(struct super_block *sb, struct kqid *qid,
2638 struct qc_dqblk *di)
2640 struct dquot *dquot;
2641 int err;
2643 if (!sb->dq_op->get_next_id)
2644 return -ENOSYS;
2645 err = sb->dq_op->get_next_id(sb, qid);
2646 if (err < 0)
2647 return err;
2648 dquot = dqget(sb, *qid);
2649 if (IS_ERR(dquot))
2650 return PTR_ERR(dquot);
2651 do_get_dqblk(dquot, di);
2652 dqput(dquot);
2654 return 0;
2656 EXPORT_SYMBOL(dquot_get_next_dqblk);
2658 #define VFS_QC_MASK \
2659 (QC_SPACE | QC_SPC_SOFT | QC_SPC_HARD | \
2660 QC_INO_COUNT | QC_INO_SOFT | QC_INO_HARD | \
2661 QC_SPC_TIMER | QC_INO_TIMER)
2663 /* Generic routine for setting common part of quota structure */
2664 static int do_set_dqblk(struct dquot *dquot, struct qc_dqblk *di)
2666 struct mem_dqblk *dm = &dquot->dq_dqb;
2667 int check_blim = 0, check_ilim = 0;
2668 struct mem_dqinfo *dqi = &sb_dqopt(dquot->dq_sb)->info[dquot->dq_id.type];
2670 if (di->d_fieldmask & ~VFS_QC_MASK)
2671 return -EINVAL;
2673 if (((di->d_fieldmask & QC_SPC_SOFT) &&
2674 di->d_spc_softlimit > dqi->dqi_max_spc_limit) ||
2675 ((di->d_fieldmask & QC_SPC_HARD) &&
2676 di->d_spc_hardlimit > dqi->dqi_max_spc_limit) ||
2677 ((di->d_fieldmask & QC_INO_SOFT) &&
2678 (di->d_ino_softlimit > dqi->dqi_max_ino_limit)) ||
2679 ((di->d_fieldmask & QC_INO_HARD) &&
2680 (di->d_ino_hardlimit > dqi->dqi_max_ino_limit)))
2681 return -ERANGE;
2683 spin_lock(&dquot->dq_dqb_lock);
2684 if (di->d_fieldmask & QC_SPACE) {
2685 dm->dqb_curspace = di->d_space - dm->dqb_rsvspace;
2686 check_blim = 1;
2687 set_bit(DQ_LASTSET_B + QIF_SPACE_B, &dquot->dq_flags);
2690 if (di->d_fieldmask & QC_SPC_SOFT)
2691 dm->dqb_bsoftlimit = di->d_spc_softlimit;
2692 if (di->d_fieldmask & QC_SPC_HARD)
2693 dm->dqb_bhardlimit = di->d_spc_hardlimit;
2694 if (di->d_fieldmask & (QC_SPC_SOFT | QC_SPC_HARD)) {
2695 check_blim = 1;
2696 set_bit(DQ_LASTSET_B + QIF_BLIMITS_B, &dquot->dq_flags);
2699 if (di->d_fieldmask & QC_INO_COUNT) {
2700 dm->dqb_curinodes = di->d_ino_count;
2701 check_ilim = 1;
2702 set_bit(DQ_LASTSET_B + QIF_INODES_B, &dquot->dq_flags);
2705 if (di->d_fieldmask & QC_INO_SOFT)
2706 dm->dqb_isoftlimit = di->d_ino_softlimit;
2707 if (di->d_fieldmask & QC_INO_HARD)
2708 dm->dqb_ihardlimit = di->d_ino_hardlimit;
2709 if (di->d_fieldmask & (QC_INO_SOFT | QC_INO_HARD)) {
2710 check_ilim = 1;
2711 set_bit(DQ_LASTSET_B + QIF_ILIMITS_B, &dquot->dq_flags);
2714 if (di->d_fieldmask & QC_SPC_TIMER) {
2715 dm->dqb_btime = di->d_spc_timer;
2716 check_blim = 1;
2717 set_bit(DQ_LASTSET_B + QIF_BTIME_B, &dquot->dq_flags);
2720 if (di->d_fieldmask & QC_INO_TIMER) {
2721 dm->dqb_itime = di->d_ino_timer;
2722 check_ilim = 1;
2723 set_bit(DQ_LASTSET_B + QIF_ITIME_B, &dquot->dq_flags);
2726 if (check_blim) {
2727 if (!dm->dqb_bsoftlimit ||
2728 dm->dqb_curspace + dm->dqb_rsvspace < dm->dqb_bsoftlimit) {
2729 dm->dqb_btime = 0;
2730 clear_bit(DQ_BLKS_B, &dquot->dq_flags);
2731 } else if (!(di->d_fieldmask & QC_SPC_TIMER))
2732 /* Set grace only if user hasn't provided his own... */
2733 dm->dqb_btime = ktime_get_real_seconds() + dqi->dqi_bgrace;
2735 if (check_ilim) {
2736 if (!dm->dqb_isoftlimit ||
2737 dm->dqb_curinodes < dm->dqb_isoftlimit) {
2738 dm->dqb_itime = 0;
2739 clear_bit(DQ_INODES_B, &dquot->dq_flags);
2740 } else if (!(di->d_fieldmask & QC_INO_TIMER))
2741 /* Set grace only if user hasn't provided his own... */
2742 dm->dqb_itime = ktime_get_real_seconds() + dqi->dqi_igrace;
2744 if (dm->dqb_bhardlimit || dm->dqb_bsoftlimit || dm->dqb_ihardlimit ||
2745 dm->dqb_isoftlimit)
2746 clear_bit(DQ_FAKE_B, &dquot->dq_flags);
2747 else
2748 set_bit(DQ_FAKE_B, &dquot->dq_flags);
2749 spin_unlock(&dquot->dq_dqb_lock);
2750 mark_dquot_dirty(dquot);
2752 return 0;
2755 int dquot_set_dqblk(struct super_block *sb, struct kqid qid,
2756 struct qc_dqblk *di)
2758 struct dquot *dquot;
2759 int rc;
2761 dquot = dqget(sb, qid);
2762 if (IS_ERR(dquot)) {
2763 rc = PTR_ERR(dquot);
2764 goto out;
2766 rc = do_set_dqblk(dquot, di);
2767 dqput(dquot);
2768 out:
2769 return rc;
2771 EXPORT_SYMBOL(dquot_set_dqblk);
2773 /* Generic routine for getting common part of quota file information */
2774 int dquot_get_state(struct super_block *sb, struct qc_state *state)
2776 struct mem_dqinfo *mi;
2777 struct qc_type_state *tstate;
2778 struct quota_info *dqopt = sb_dqopt(sb);
2779 int type;
2781 memset(state, 0, sizeof(*state));
2782 for (type = 0; type < MAXQUOTAS; type++) {
2783 if (!sb_has_quota_active(sb, type))
2784 continue;
2785 tstate = state->s_state + type;
2786 mi = sb_dqopt(sb)->info + type;
2787 tstate->flags = QCI_ACCT_ENABLED;
2788 spin_lock(&dq_data_lock);
2789 if (mi->dqi_flags & DQF_SYS_FILE)
2790 tstate->flags |= QCI_SYSFILE;
2791 if (mi->dqi_flags & DQF_ROOT_SQUASH)
2792 tstate->flags |= QCI_ROOT_SQUASH;
2793 if (sb_has_quota_limits_enabled(sb, type))
2794 tstate->flags |= QCI_LIMITS_ENFORCED;
2795 tstate->spc_timelimit = mi->dqi_bgrace;
2796 tstate->ino_timelimit = mi->dqi_igrace;
2797 tstate->ino = dqopt->files[type]->i_ino;
2798 tstate->blocks = dqopt->files[type]->i_blocks;
2799 tstate->nextents = 1; /* We don't know... */
2800 spin_unlock(&dq_data_lock);
2802 return 0;
2804 EXPORT_SYMBOL(dquot_get_state);
2806 /* Generic routine for setting common part of quota file information */
2807 int dquot_set_dqinfo(struct super_block *sb, int type, struct qc_info *ii)
2809 struct mem_dqinfo *mi;
2810 int err = 0;
2812 if ((ii->i_fieldmask & QC_WARNS_MASK) ||
2813 (ii->i_fieldmask & QC_RT_SPC_TIMER))
2814 return -EINVAL;
2815 if (!sb_has_quota_active(sb, type))
2816 return -ESRCH;
2817 mi = sb_dqopt(sb)->info + type;
2818 if (ii->i_fieldmask & QC_FLAGS) {
2819 if ((ii->i_flags & QCI_ROOT_SQUASH &&
2820 mi->dqi_format->qf_fmt_id != QFMT_VFS_OLD))
2821 return -EINVAL;
2823 spin_lock(&dq_data_lock);
2824 if (ii->i_fieldmask & QC_SPC_TIMER)
2825 mi->dqi_bgrace = ii->i_spc_timelimit;
2826 if (ii->i_fieldmask & QC_INO_TIMER)
2827 mi->dqi_igrace = ii->i_ino_timelimit;
2828 if (ii->i_fieldmask & QC_FLAGS) {
2829 if (ii->i_flags & QCI_ROOT_SQUASH)
2830 mi->dqi_flags |= DQF_ROOT_SQUASH;
2831 else
2832 mi->dqi_flags &= ~DQF_ROOT_SQUASH;
2834 spin_unlock(&dq_data_lock);
2835 mark_info_dirty(sb, type);
2836 /* Force write to disk */
2837 sb->dq_op->write_info(sb, type);
2838 return err;
2840 EXPORT_SYMBOL(dquot_set_dqinfo);
2842 const struct quotactl_ops dquot_quotactl_sysfile_ops = {
2843 .quota_enable = dquot_quota_enable,
2844 .quota_disable = dquot_quota_disable,
2845 .quota_sync = dquot_quota_sync,
2846 .get_state = dquot_get_state,
2847 .set_info = dquot_set_dqinfo,
2848 .get_dqblk = dquot_get_dqblk,
2849 .get_nextdqblk = dquot_get_next_dqblk,
2850 .set_dqblk = dquot_set_dqblk
2852 EXPORT_SYMBOL(dquot_quotactl_sysfile_ops);
2854 static int do_proc_dqstats(struct ctl_table *table, int write,
2855 void __user *buffer, size_t *lenp, loff_t *ppos)
2857 unsigned int type = (unsigned long *)table->data - dqstats.stat;
2858 s64 value = percpu_counter_sum(&dqstats.counter[type]);
2860 /* Filter negative values for non-monotonic counters */
2861 if (value < 0 && (type == DQST_ALLOC_DQUOTS ||
2862 type == DQST_FREE_DQUOTS))
2863 value = 0;
2865 /* Update global table */
2866 dqstats.stat[type] = value;
2867 return proc_doulongvec_minmax(table, write, buffer, lenp, ppos);
2870 static struct ctl_table fs_dqstats_table[] = {
2872 .procname = "lookups",
2873 .data = &dqstats.stat[DQST_LOOKUPS],
2874 .maxlen = sizeof(unsigned long),
2875 .mode = 0444,
2876 .proc_handler = do_proc_dqstats,
2879 .procname = "drops",
2880 .data = &dqstats.stat[DQST_DROPS],
2881 .maxlen = sizeof(unsigned long),
2882 .mode = 0444,
2883 .proc_handler = do_proc_dqstats,
2886 .procname = "reads",
2887 .data = &dqstats.stat[DQST_READS],
2888 .maxlen = sizeof(unsigned long),
2889 .mode = 0444,
2890 .proc_handler = do_proc_dqstats,
2893 .procname = "writes",
2894 .data = &dqstats.stat[DQST_WRITES],
2895 .maxlen = sizeof(unsigned long),
2896 .mode = 0444,
2897 .proc_handler = do_proc_dqstats,
2900 .procname = "cache_hits",
2901 .data = &dqstats.stat[DQST_CACHE_HITS],
2902 .maxlen = sizeof(unsigned long),
2903 .mode = 0444,
2904 .proc_handler = do_proc_dqstats,
2907 .procname = "allocated_dquots",
2908 .data = &dqstats.stat[DQST_ALLOC_DQUOTS],
2909 .maxlen = sizeof(unsigned long),
2910 .mode = 0444,
2911 .proc_handler = do_proc_dqstats,
2914 .procname = "free_dquots",
2915 .data = &dqstats.stat[DQST_FREE_DQUOTS],
2916 .maxlen = sizeof(unsigned long),
2917 .mode = 0444,
2918 .proc_handler = do_proc_dqstats,
2921 .procname = "syncs",
2922 .data = &dqstats.stat[DQST_SYNCS],
2923 .maxlen = sizeof(unsigned long),
2924 .mode = 0444,
2925 .proc_handler = do_proc_dqstats,
2927 #ifdef CONFIG_PRINT_QUOTA_WARNING
2929 .procname = "warnings",
2930 .data = &flag_print_warnings,
2931 .maxlen = sizeof(int),
2932 .mode = 0644,
2933 .proc_handler = proc_dointvec,
2935 #endif
2936 { },
2939 static struct ctl_table fs_table[] = {
2941 .procname = "quota",
2942 .mode = 0555,
2943 .child = fs_dqstats_table,
2945 { },
2948 static struct ctl_table sys_table[] = {
2950 .procname = "fs",
2951 .mode = 0555,
2952 .child = fs_table,
2954 { },
2957 static int __init dquot_init(void)
2959 int i, ret;
2960 unsigned long nr_hash, order;
2962 printk(KERN_NOTICE "VFS: Disk quotas %s\n", __DQUOT_VERSION__);
2964 register_sysctl_table(sys_table);
2966 dquot_cachep = kmem_cache_create("dquot",
2967 sizeof(struct dquot), sizeof(unsigned long) * 4,
2968 (SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT|
2969 SLAB_MEM_SPREAD|SLAB_PANIC),
2970 NULL);
2972 order = 0;
2973 dquot_hash = (struct hlist_head *)__get_free_pages(GFP_KERNEL, order);
2974 if (!dquot_hash)
2975 panic("Cannot create dquot hash table");
2977 for (i = 0; i < _DQST_DQSTAT_LAST; i++) {
2978 ret = percpu_counter_init(&dqstats.counter[i], 0, GFP_KERNEL);
2979 if (ret)
2980 panic("Cannot create dquot stat counters");
2983 /* Find power-of-two hlist_heads which can fit into allocation */
2984 nr_hash = (1UL << order) * PAGE_SIZE / sizeof(struct hlist_head);
2985 dq_hash_bits = 0;
2986 do {
2987 dq_hash_bits++;
2988 } while (nr_hash >> dq_hash_bits);
2989 dq_hash_bits--;
2991 nr_hash = 1UL << dq_hash_bits;
2992 dq_hash_mask = nr_hash - 1;
2993 for (i = 0; i < nr_hash; i++)
2994 INIT_HLIST_HEAD(dquot_hash + i);
2996 pr_info("VFS: Dquot-cache hash table entries: %ld (order %ld,"
2997 " %ld bytes)\n", nr_hash, order, (PAGE_SIZE << order));
2999 if (register_shrinker(&dqcache_shrinker))
3000 panic("Cannot register dquot shrinker");
3002 return 0;
3004 fs_initcall(dquot_init);