blob: 361a2a6f13e102b67b7aca3cc099ad0c7aa28a4c [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Implementation of the diskquota system for the LINUX operating system. QUOTA
3 * is implemented using the BSD system call interface as the means of
4 * communication with the user level. This file contains the generic routines
5 * called by the different filesystems on allocation of an inode or block.
6 * These routines take care of the administration needed to have a consistent
7 * diskquota tracking system. The ideas of both user and group quotas are based
8 * on the Melbourne quota system as used on BSD derived systems. The internal
9 * implementation is based on one of the several variants of the LINUX
10 * inode-subsystem with added complexity of the diskquota system.
11 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070012 * Author: Marco van Wieringen <mvw@planets.elm.net>
13 *
14 * Fixes: Dmitry Gorodchanin <pgmdsg@ibi.com>, 11 Feb 96
15 *
16 * Revised list management to avoid races
17 * -- Bill Hawes, <whawes@star.net>, 9/98
18 *
19 * Fixed races in dquot_transfer(), dqget() and dquot_alloc_...().
20 * As the consequence the locking was moved from dquot_decr_...(),
21 * dquot_incr_...() to calling functions.
22 * invalidate_dquots() now writes modified dquots.
23 * Serialized quota_off() and quota_on() for mount point.
24 * Fixed a few bugs in grow_dquots().
25 * Fixed deadlock in write_dquot() - we no longer account quotas on
26 * quota files
27 * remove_dquot_ref() moved to inode.c - it now traverses through inodes
28 * add_dquot_ref() restarts after blocking
29 * Added check for bogus uid and fixed check for group in quotactl.
30 * Jan Kara, <jack@suse.cz>, sponsored by SuSE CR, 10-11/99
31 *
32 * Used struct list_head instead of own list struct
33 * Invalidation of referenced dquots is no longer possible
34 * Improved free_dquots list management
35 * Quota and i_blocks are now updated in one place to avoid races
36 * Warnings are now delayed so we won't block in critical section
37 * Write updated not to require dquot lock
38 * Jan Kara, <jack@suse.cz>, 9/2000
39 *
40 * Added dynamic quota structure allocation
41 * Jan Kara <jack@suse.cz> 12/2000
42 *
43 * Rewritten quota interface. Implemented new quota format and
44 * formats registering.
45 * Jan Kara, <jack@suse.cz>, 2001,2002
46 *
47 * New SMP locking.
48 * Jan Kara, <jack@suse.cz>, 10/2002
49 *
50 * Added journalled quota support, fix lock inversion problems
51 * Jan Kara, <jack@suse.cz>, 2003,2004
52 *
53 * (C) Copyright 1994 - 1997 Marco van Wieringen
54 */
55
56#include <linux/errno.h>
57#include <linux/kernel.h>
58#include <linux/fs.h>
59#include <linux/mount.h>
60#include <linux/mm.h>
61#include <linux/time.h>
62#include <linux/types.h>
63#include <linux/string.h>
64#include <linux/fcntl.h>
65#include <linux/stat.h>
66#include <linux/tty.h>
67#include <linux/file.h>
68#include <linux/slab.h>
69#include <linux/sysctl.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070070#include <linux/init.h>
71#include <linux/module.h>
72#include <linux/proc_fs.h>
73#include <linux/security.h>
Al Viro40401532012-02-13 03:58:52 +000074#include <linux/sched.h>
Ingo Molnar5b825c32017-02-02 17:54:15 +010075#include <linux/cred.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070076#include <linux/kmod.h>
77#include <linux/namei.h>
Randy Dunlap16f7e0f2006-01-11 12:17:46 -080078#include <linux/capability.h>
Adrian Bunkbe586ba2005-11-07 00:59:35 -080079#include <linux/quotaops.h>
Dave Chinner55fa6092011-03-22 22:23:40 +110080#include "../internal.h" /* ugh */
Linus Torvalds1da177e2005-04-16 15:20:36 -070081
Jeff Liuf3da9312012-05-28 23:40:17 +080082#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070083
Linus Torvalds1da177e2005-04-16 15:20:36 -070084/*
Jan Karacc334122009-01-12 17:23:05 +010085 * There are three quota SMP locks. dq_list_lock protects all lists with quotas
Dmitry Monakhovdde95882010-04-26 20:03:33 +040086 * and quota formats.
Jan Karacc334122009-01-12 17:23:05 +010087 * dq_data_lock protects data from dq_dqb and also mem_dqinfo structures and
88 * also guards consistency of dquot->dq_dqb with inode->i_blocks, i_bytes.
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 * i_blocks and i_bytes updates itself are guarded by i_lock acquired directly
Jan Karacc334122009-01-12 17:23:05 +010090 * in inode_add_bytes() and inode_sub_bytes(). dq_state_lock protects
91 * modifications of quota state (on quotaon and quotaoff) and readers who care
92 * about latest values take it as well.
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 *
Jan Karacc334122009-01-12 17:23:05 +010094 * The spinlock ordering is hence: dq_data_lock > dq_list_lock > i_lock,
95 * dq_list_lock > dq_state_lock
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 *
97 * Note that some things (eg. sb pointer, type, id) doesn't change during
98 * the life of the dquot structure and so needn't to be protected by a lock
99 *
Niu Yaweib9ba6f92014-06-04 12:23:19 +0800100 * Operation accessing dquots via inode pointers are protected by dquot_srcu.
101 * Operation of reading pointer needs srcu_read_lock(&dquot_srcu), and
102 * synchronize_srcu(&dquot_srcu) is called after clearing pointers from
103 * inode and before dropping dquot references to avoid use of dquots after
104 * they are freed. dq_data_lock is used to serialize the pointer setting and
105 * clearing operations.
Jan Kara26245c92010-01-06 17:20:35 +0100106 * Special care needs to be taken about S_NOQUOTA inode flag (marking that
107 * inode is a quota file). Functions adding pointers from inode to dquots have
Niu Yaweib9ba6f92014-06-04 12:23:19 +0800108 * to check this flag under dq_data_lock and then (if S_NOQUOTA is not set) they
109 * have to do all pointer modifications before dropping dq_data_lock. This makes
Jan Kara26245c92010-01-06 17:20:35 +0100110 * sure they cannot race with quotaon which first sets S_NOQUOTA flag and
111 * then drops all pointers to dquots from an inode.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 *
Jan Kara5e8cb9b2017-06-08 15:16:41 +0200113 * Each dquot has its dq_lock mutex. Dquot is locked when it is being read to
114 * memory (or space for it is being allocated) on the first dqget(), when it is
115 * being written out, and when it is being released on the last dqput(). The
116 * allocation and release operations are serialized by the dq_lock and by
117 * checking the use count in dquot_release().
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 *
119 * Lock ordering (including related VFS locks) is the following:
Jan Karabc8230e2017-06-08 14:39:48 +0200120 * s_umount > i_mutex > journal_lock > dquot->dq_lock > dqio_sem
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 */
122
Jan Karac5166102009-01-26 15:32:46 +0100123static __cacheline_aligned_in_smp DEFINE_SPINLOCK(dq_list_lock);
124static __cacheline_aligned_in_smp DEFINE_SPINLOCK(dq_state_lock);
125__cacheline_aligned_in_smp DEFINE_SPINLOCK(dq_data_lock);
Mingming Cao08d03502009-01-14 16:19:32 +0100126EXPORT_SYMBOL(dq_data_lock);
Niu Yaweib9ba6f92014-06-04 12:23:19 +0800127DEFINE_STATIC_SRCU(dquot_srcu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128
Jan Kara503330f2017-08-02 17:18:50 +0200129static DECLARE_WAIT_QUEUE_HEAD(dquot_ref_wq);
130
Jiaying Zhangfb5ffb02010-07-20 16:54:43 +0200131void __quota_error(struct super_block *sb, const char *func,
Joe Perches055adcb2010-11-23 18:49:54 -0800132 const char *fmt, ...)
Jiaying Zhangfb5ffb02010-07-20 16:54:43 +0200133{
Jiaying Zhangfb5ffb02010-07-20 16:54:43 +0200134 if (printk_ratelimit()) {
Joe Perches055adcb2010-11-23 18:49:54 -0800135 va_list args;
136 struct va_format vaf;
137
Jiaying Zhangfb5ffb02010-07-20 16:54:43 +0200138 va_start(args, fmt);
Joe Perches055adcb2010-11-23 18:49:54 -0800139
140 vaf.fmt = fmt;
141 vaf.va = &args;
142
143 printk(KERN_ERR "Quota error (device %s): %s: %pV\n",
144 sb->s_id, func, &vaf);
145
Jiaying Zhangfb5ffb02010-07-20 16:54:43 +0200146 va_end(args);
147 }
148}
149EXPORT_SYMBOL(__quota_error);
150
Sergey Senozhatskyda8d1ba2010-04-26 12:09:26 +0200151#if defined(CONFIG_QUOTA_DEBUG) || defined(CONFIG_PRINT_QUOTA_WARNING)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152static char *quotatypes[] = INITQFNAMES;
Sergey Senozhatskyda8d1ba2010-04-26 12:09:26 +0200153#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154static struct quota_format_type *quota_formats; /* List of registered formats */
155static struct quota_module_name module_names[] = INIT_QUOTA_MODULE_NAMES;
156
157/* SLAB cache for dquot structures */
Christoph Lametere18b8902006-12-06 20:33:20 -0800158static struct kmem_cache *dquot_cachep;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159
160int register_quota_format(struct quota_format_type *fmt)
161{
162 spin_lock(&dq_list_lock);
163 fmt->qf_next = quota_formats;
164 quota_formats = fmt;
165 spin_unlock(&dq_list_lock);
166 return 0;
167}
Mingming Cao08d03502009-01-14 16:19:32 +0100168EXPORT_SYMBOL(register_quota_format);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169
170void unregister_quota_format(struct quota_format_type *fmt)
171{
172 struct quota_format_type **actqf;
173
174 spin_lock(&dq_list_lock);
Jan Kara268157b2009-01-27 15:47:22 +0100175 for (actqf = &quota_formats; *actqf && *actqf != fmt;
176 actqf = &(*actqf)->qf_next)
177 ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 if (*actqf)
179 *actqf = (*actqf)->qf_next;
180 spin_unlock(&dq_list_lock);
181}
Mingming Cao08d03502009-01-14 16:19:32 +0100182EXPORT_SYMBOL(unregister_quota_format);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183
184static struct quota_format_type *find_quota_format(int id)
185{
186 struct quota_format_type *actqf;
187
188 spin_lock(&dq_list_lock);
Jan Kara268157b2009-01-27 15:47:22 +0100189 for (actqf = quota_formats; actqf && actqf->qf_fmt_id != id;
190 actqf = actqf->qf_next)
191 ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 if (!actqf || !try_module_get(actqf->qf_owner)) {
193 int qm;
194
195 spin_unlock(&dq_list_lock);
196
Jan Kara268157b2009-01-27 15:47:22 +0100197 for (qm = 0; module_names[qm].qm_fmt_id &&
198 module_names[qm].qm_fmt_id != id; qm++)
199 ;
200 if (!module_names[qm].qm_fmt_id ||
201 request_module(module_names[qm].qm_mod_name))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 return NULL;
203
204 spin_lock(&dq_list_lock);
Jan Kara268157b2009-01-27 15:47:22 +0100205 for (actqf = quota_formats; actqf && actqf->qf_fmt_id != id;
206 actqf = actqf->qf_next)
207 ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 if (actqf && !try_module_get(actqf->qf_owner))
209 actqf = NULL;
210 }
211 spin_unlock(&dq_list_lock);
212 return actqf;
213}
214
215static void put_quota_format(struct quota_format_type *fmt)
216{
217 module_put(fmt->qf_owner);
218}
219
220/*
221 * Dquot List Management:
222 * The quota code uses three lists for dquot management: the inuse_list,
223 * free_dquots, and dquot_hash[] array. A single dquot structure may be
224 * on all three lists, depending on its current state.
225 *
226 * All dquots are placed to the end of inuse_list when first created, and this
227 * list is used for invalidate operation, which must look at every dquot.
228 *
229 * Unused dquots (dq_count == 0) are added to the free_dquots list when freed,
230 * and this list is searched whenever we need an available dquot. Dquots are
231 * removed from the list as soon as they are used again, and
232 * dqstats.free_dquots gives the number of dquots on the list. When
233 * dquot is invalidated it's completely released from memory.
234 *
235 * Dquots with a specific identity (device, type and id) are placed on
236 * one of the dquot_hash[] hash chains. The provides an efficient search
237 * mechanism to locate a specific dquot.
238 */
239
240static LIST_HEAD(inuse_list);
241static LIST_HEAD(free_dquots);
242static unsigned int dq_hash_bits, dq_hash_mask;
243static struct hlist_head *dquot_hash;
244
245struct dqstats dqstats;
Mingming Cao08d03502009-01-14 16:19:32 +0100246EXPORT_SYMBOL(dqstats);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247
Jan Kara0a5a9c72010-02-09 18:20:39 +0100248static qsize_t inode_get_rsv_space(struct inode *inode);
Jan Kara6184fc02015-06-24 18:07:02 +0200249static int __dquot_initialize(struct inode *inode, int type);
Jan Kara0a5a9c72010-02-09 18:20:39 +0100250
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251static inline unsigned int
Eric W. Biederman1a06d422012-09-16 05:45:30 -0700252hashfn(const struct super_block *sb, struct kqid qid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253{
Eric W. Biederman1a06d422012-09-16 05:45:30 -0700254 unsigned int id = from_kqid(&init_user_ns, qid);
255 int type = qid.type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 unsigned long tmp;
257
258 tmp = (((unsigned long)sb>>L1_CACHE_SHIFT) ^ id) * (MAXQUOTAS - type);
259 return (tmp + (tmp >> dq_hash_bits)) & dq_hash_mask;
260}
261
262/*
263 * Following list functions expect dq_list_lock to be held
264 */
265static inline void insert_dquot_hash(struct dquot *dquot)
266{
Jan Kara268157b2009-01-27 15:47:22 +0100267 struct hlist_head *head;
Eric W. Biederman1a06d422012-09-16 05:45:30 -0700268 head = dquot_hash + hashfn(dquot->dq_sb, dquot->dq_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 hlist_add_head(&dquot->dq_hash, head);
270}
271
272static inline void remove_dquot_hash(struct dquot *dquot)
273{
274 hlist_del_init(&dquot->dq_hash);
275}
276
Jan Kara7a2435d2009-01-27 01:47:11 +0100277static struct dquot *find_dquot(unsigned int hashent, struct super_block *sb,
Eric W. Biederman1a06d422012-09-16 05:45:30 -0700278 struct kqid qid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279{
280 struct hlist_node *node;
281 struct dquot *dquot;
282
283 hlist_for_each (node, dquot_hash+hashent) {
284 dquot = hlist_entry(node, struct dquot, dq_hash);
Eric W. Biederman4c376dc2012-09-16 03:56:19 -0700285 if (dquot->dq_sb == sb && qid_eq(dquot->dq_id, qid))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 return dquot;
287 }
Jan Karadd6f3c62009-01-26 16:01:43 +0100288 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289}
290
291/* Add a dquot to the tail of the free list */
292static inline void put_dquot_last(struct dquot *dquot)
293{
Akinobu Mita8e130592006-06-26 00:24:37 -0700294 list_add_tail(&dquot->dq_free, &free_dquots);
Dmitry Monakhovdde95882010-04-26 20:03:33 +0400295 dqstats_inc(DQST_FREE_DQUOTS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296}
297
298static inline void remove_free_dquot(struct dquot *dquot)
299{
300 if (list_empty(&dquot->dq_free))
301 return;
302 list_del_init(&dquot->dq_free);
Dmitry Monakhovdde95882010-04-26 20:03:33 +0400303 dqstats_dec(DQST_FREE_DQUOTS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304}
305
306static inline void put_inuse(struct dquot *dquot)
307{
308 /* We add to the back of inuse list so we don't have to restart
309 * when traversing this list and we block */
Akinobu Mita8e130592006-06-26 00:24:37 -0700310 list_add_tail(&dquot->dq_inuse, &inuse_list);
Dmitry Monakhovdde95882010-04-26 20:03:33 +0400311 dqstats_inc(DQST_ALLOC_DQUOTS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312}
313
314static inline void remove_inuse(struct dquot *dquot)
315{
Dmitry Monakhovdde95882010-04-26 20:03:33 +0400316 dqstats_dec(DQST_ALLOC_DQUOTS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 list_del(&dquot->dq_inuse);
318}
319/*
320 * End of list functions needing dq_list_lock
321 */
322
323static void wait_on_dquot(struct dquot *dquot)
324{
Ingo Molnard3be9152006-03-23 03:00:29 -0800325 mutex_lock(&dquot->dq_lock);
326 mutex_unlock(&dquot->dq_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327}
328
Jan Kara03f6e922008-04-28 02:14:32 -0700329static inline int dquot_dirty(struct dquot *dquot)
330{
331 return test_bit(DQ_MOD_B, &dquot->dq_flags);
332}
333
334static inline int mark_dquot_dirty(struct dquot *dquot)
335{
336 return dquot->dq_sb->dq_op->mark_dirty(dquot);
337}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338
Dmitry Monakhoveabf2902010-03-27 15:15:38 +0300339/* Mark dquot dirty in atomic manner, and return it's old dirty flag state */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340int dquot_mark_dquot_dirty(struct dquot *dquot)
341{
Dmitry Monakhoveabf2902010-03-27 15:15:38 +0300342 int ret = 1;
343
Jan Kara4580b302017-08-02 11:44:38 +0200344 if (!test_bit(DQ_ACTIVE_B, &dquot->dq_flags))
345 return 0;
346
Dmitry Monakhoveabf2902010-03-27 15:15:38 +0300347 /* If quota is dirty already, we don't have to acquire dq_list_lock */
348 if (test_bit(DQ_MOD_B, &dquot->dq_flags))
349 return 1;
350
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 spin_lock(&dq_list_lock);
Dmitry Monakhoveabf2902010-03-27 15:15:38 +0300352 if (!test_and_set_bit(DQ_MOD_B, &dquot->dq_flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 list_add(&dquot->dq_dirty, &sb_dqopt(dquot->dq_sb)->
Eric W. Biederman4c376dc2012-09-16 03:56:19 -0700354 info[dquot->dq_id.type].dqi_dirty_list);
Dmitry Monakhoveabf2902010-03-27 15:15:38 +0300355 ret = 0;
356 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 spin_unlock(&dq_list_lock);
Dmitry Monakhoveabf2902010-03-27 15:15:38 +0300358 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359}
Mingming Cao08d03502009-01-14 16:19:32 +0100360EXPORT_SYMBOL(dquot_mark_dquot_dirty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361
Dmitry Monakhovdc52dd32009-12-14 15:21:15 +0300362/* Dirtify all the dquots - this can block when journalling */
363static inline int mark_all_dquot_dirty(struct dquot * const *dquot)
364{
365 int ret, err, cnt;
366
367 ret = err = 0;
368 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
369 if (dquot[cnt])
370 /* Even in case of error we have to continue */
371 ret = mark_dquot_dirty(dquot[cnt]);
372 if (!err)
373 err = ret;
374 }
375 return err;
376}
377
378static inline void dqput_all(struct dquot **dquot)
379{
380 unsigned int cnt;
381
382 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
383 dqput(dquot[cnt]);
384}
385
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386static inline int clear_dquot_dirty(struct dquot *dquot)
387{
Jan Kara1e0b7cb2017-08-02 11:54:26 +0200388 spin_lock(&dq_list_lock);
389 if (!test_and_clear_bit(DQ_MOD_B, &dquot->dq_flags)) {
390 spin_unlock(&dq_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 return 0;
Jan Kara1e0b7cb2017-08-02 11:54:26 +0200392 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 list_del_init(&dquot->dq_dirty);
Jan Kara1e0b7cb2017-08-02 11:54:26 +0200394 spin_unlock(&dq_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 return 1;
396}
397
398void mark_info_dirty(struct super_block *sb, int type)
399{
Jan Kara15512372017-06-09 11:56:06 +0200400 spin_lock(&dq_data_lock);
401 sb_dqopt(sb)->info[type].dqi_flags |= DQF_INFO_DIRTY;
402 spin_unlock(&dq_data_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403}
404EXPORT_SYMBOL(mark_info_dirty);
405
406/*
407 * Read dquot from disk and alloc space for it
408 */
409
410int dquot_acquire(struct dquot *dquot)
411{
412 int ret = 0, ret2 = 0;
413 struct quota_info *dqopt = sb_dqopt(dquot->dq_sb);
414
Ingo Molnard3be9152006-03-23 03:00:29 -0800415 mutex_lock(&dquot->dq_lock);
Jan Karae342e382017-06-08 15:30:45 +0200416 if (!test_bit(DQ_READ_B, &dquot->dq_flags))
Eric W. Biederman4c376dc2012-09-16 03:56:19 -0700417 ret = dqopt->ops[dquot->dq_id.type]->read_dqblk(dquot);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 if (ret < 0)
419 goto out_iolock;
Jan Kara044c9b62016-02-18 13:20:20 +0100420 /* Make sure flags update is visible after dquot has been filled */
421 smp_mb__before_atomic();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 set_bit(DQ_READ_B, &dquot->dq_flags);
423 /* Instantiate dquot if needed */
424 if (!test_bit(DQ_ACTIVE_B, &dquot->dq_flags) && !dquot->dq_off) {
Eric W. Biederman4c376dc2012-09-16 03:56:19 -0700425 ret = dqopt->ops[dquot->dq_id.type]->commit_dqblk(dquot);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 /* Write the info if needed */
Eric W. Biederman4c376dc2012-09-16 03:56:19 -0700427 if (info_dirty(&dqopt->info[dquot->dq_id.type])) {
428 ret2 = dqopt->ops[dquot->dq_id.type]->write_file_info(
429 dquot->dq_sb, dquot->dq_id.type);
Jan Kara268157b2009-01-27 15:47:22 +0100430 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 if (ret < 0)
432 goto out_iolock;
433 if (ret2 < 0) {
434 ret = ret2;
435 goto out_iolock;
436 }
437 }
Jan Kara044c9b62016-02-18 13:20:20 +0100438 /*
439 * Make sure flags update is visible after on-disk struct has been
440 * allocated. Paired with smp_rmb() in dqget().
441 */
442 smp_mb__before_atomic();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 set_bit(DQ_ACTIVE_B, &dquot->dq_flags);
444out_iolock:
Ingo Molnard3be9152006-03-23 03:00:29 -0800445 mutex_unlock(&dquot->dq_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 return ret;
447}
Mingming Cao08d03502009-01-14 16:19:32 +0100448EXPORT_SYMBOL(dquot_acquire);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449
450/*
451 * Write dquot to disk
452 */
453int dquot_commit(struct dquot *dquot)
454{
Jan Karab03f2452011-03-31 18:36:52 +0200455 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 struct quota_info *dqopt = sb_dqopt(dquot->dq_sb);
457
Jan Kara5e8cb9b2017-06-08 15:16:41 +0200458 mutex_lock(&dquot->dq_lock);
Jan Kara1e0b7cb2017-08-02 11:54:26 +0200459 if (!clear_dquot_dirty(dquot))
Jan Kara5e8cb9b2017-06-08 15:16:41 +0200460 goto out_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 /* Inactive dquot can be only if there was error during read/init
462 * => we have better not writing it */
Jan Kara8fc32c22017-06-08 15:48:16 +0200463 if (test_bit(DQ_ACTIVE_B, &dquot->dq_flags))
Eric W. Biederman4c376dc2012-09-16 03:56:19 -0700464 ret = dqopt->ops[dquot->dq_id.type]->commit_dqblk(dquot);
Jan Kara8fc32c22017-06-08 15:48:16 +0200465 else
Jan Karab03f2452011-03-31 18:36:52 +0200466 ret = -EIO;
Jan Kara5e8cb9b2017-06-08 15:16:41 +0200467out_lock:
468 mutex_unlock(&dquot->dq_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 return ret;
470}
Mingming Cao08d03502009-01-14 16:19:32 +0100471EXPORT_SYMBOL(dquot_commit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472
473/*
474 * Release dquot
475 */
476int dquot_release(struct dquot *dquot)
477{
478 int ret = 0, ret2 = 0;
479 struct quota_info *dqopt = sb_dqopt(dquot->dq_sb);
480
Ingo Molnard3be9152006-03-23 03:00:29 -0800481 mutex_lock(&dquot->dq_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 /* Check whether we are not racing with some other dqget() */
483 if (atomic_read(&dquot->dq_count) > 1)
484 goto out_dqlock;
Eric W. Biederman4c376dc2012-09-16 03:56:19 -0700485 if (dqopt->ops[dquot->dq_id.type]->release_dqblk) {
486 ret = dqopt->ops[dquot->dq_id.type]->release_dqblk(dquot);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 /* Write the info */
Eric W. Biederman4c376dc2012-09-16 03:56:19 -0700488 if (info_dirty(&dqopt->info[dquot->dq_id.type])) {
489 ret2 = dqopt->ops[dquot->dq_id.type]->write_file_info(
490 dquot->dq_sb, dquot->dq_id.type);
Jan Kara268157b2009-01-27 15:47:22 +0100491 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 if (ret >= 0)
493 ret = ret2;
494 }
495 clear_bit(DQ_ACTIVE_B, &dquot->dq_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496out_dqlock:
Ingo Molnard3be9152006-03-23 03:00:29 -0800497 mutex_unlock(&dquot->dq_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 return ret;
499}
Mingming Cao08d03502009-01-14 16:19:32 +0100500EXPORT_SYMBOL(dquot_release);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501
Jan Kara7d9056b2008-11-25 15:31:32 +0100502void dquot_destroy(struct dquot *dquot)
Jan Kara74f783a2008-08-19 14:51:22 +0200503{
504 kmem_cache_free(dquot_cachep, dquot);
505}
Jan Kara7d9056b2008-11-25 15:31:32 +0100506EXPORT_SYMBOL(dquot_destroy);
Jan Kara74f783a2008-08-19 14:51:22 +0200507
508static inline void do_destroy_dquot(struct dquot *dquot)
509{
510 dquot->dq_sb->dq_op->destroy_dquot(dquot);
511}
512
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513/* Invalidate all dquots on the list. Note that this function is called after
514 * quota is disabled and pointers from inodes removed so there cannot be new
Jan Kara6362e4d2006-03-23 03:00:17 -0800515 * quota users. There can still be some users of quotas due to inodes being
516 * just deleted or pruned by prune_icache() (those are not attached to any
Jan Karacc334122009-01-12 17:23:05 +0100517 * list) or parallel quotactl call. We have to wait for such users.
Jan Kara6362e4d2006-03-23 03:00:17 -0800518 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519static void invalidate_dquots(struct super_block *sb, int type)
520{
Domen Puncerc33ed272005-06-25 14:59:36 -0700521 struct dquot *dquot, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522
Jan Kara6362e4d2006-03-23 03:00:17 -0800523restart:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 spin_lock(&dq_list_lock);
Domen Puncerc33ed272005-06-25 14:59:36 -0700525 list_for_each_entry_safe(dquot, tmp, &inuse_list, dq_inuse) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 if (dquot->dq_sb != sb)
527 continue;
Eric W. Biederman4c376dc2012-09-16 03:56:19 -0700528 if (dquot->dq_id.type != type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 continue;
Jan Kara6362e4d2006-03-23 03:00:17 -0800530 /* Wait for dquot users */
531 if (atomic_read(&dquot->dq_count)) {
Jan Kara9f985cb2014-04-03 14:46:55 -0700532 dqgrab(dquot);
Jan Kara6362e4d2006-03-23 03:00:17 -0800533 spin_unlock(&dq_list_lock);
Jan Kara503330f2017-08-02 17:18:50 +0200534 /*
535 * Once dqput() wakes us up, we know it's time to free
Jan Kara6362e4d2006-03-23 03:00:17 -0800536 * the dquot.
537 * IMPORTANT: we rely on the fact that there is always
538 * at most one process waiting for dquot to free.
539 * Otherwise dq_count would be > 1 and we would never
540 * wake up.
541 */
Jan Kara503330f2017-08-02 17:18:50 +0200542 wait_event(dquot_ref_wq,
543 atomic_read(&dquot->dq_count) == 1);
Jan Kara6362e4d2006-03-23 03:00:17 -0800544 dqput(dquot);
545 /* At this moment dquot() need not exist (it could be
546 * reclaimed by prune_dqcache(). Hence we must
547 * restart. */
548 goto restart;
549 }
550 /*
551 * Quota now has no users and it has been written on last
552 * dqput()
553 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 remove_dquot_hash(dquot);
555 remove_free_dquot(dquot);
556 remove_inuse(dquot);
Jan Kara74f783a2008-08-19 14:51:22 +0200557 do_destroy_dquot(dquot);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 }
559 spin_unlock(&dq_list_lock);
560}
561
Jan Kara12c77522008-10-20 17:05:00 +0200562/* Call callback for every active dquot on given filesystem */
563int dquot_scan_active(struct super_block *sb,
564 int (*fn)(struct dquot *dquot, unsigned long priv),
565 unsigned long priv)
566{
567 struct dquot *dquot, *old_dquot = NULL;
568 int ret = 0;
569
Jan Karaee1ac542016-11-23 13:52:19 +0100570 WARN_ON_ONCE(!rwsem_is_locked(&sb->s_umount));
571
Jan Kara12c77522008-10-20 17:05:00 +0200572 spin_lock(&dq_list_lock);
573 list_for_each_entry(dquot, &inuse_list, dq_inuse) {
574 if (!test_bit(DQ_ACTIVE_B, &dquot->dq_flags))
575 continue;
576 if (dquot->dq_sb != sb)
577 continue;
578 /* Now we have active dquot so we can just increase use count */
579 atomic_inc(&dquot->dq_count);
Jan Kara12c77522008-10-20 17:05:00 +0200580 spin_unlock(&dq_list_lock);
Dmitry Monakhovdde95882010-04-26 20:03:33 +0400581 dqstats_inc(DQST_LOOKUPS);
Jan Kara12c77522008-10-20 17:05:00 +0200582 dqput(old_dquot);
583 old_dquot = dquot;
Jan Kara1362f4e2014-02-20 17:02:27 +0100584 /*
585 * ->release_dquot() can be racing with us. Our reference
586 * protects us from new calls to it so just wait for any
587 * outstanding call and recheck the DQ_ACTIVE_B after that.
588 */
589 wait_on_dquot(dquot);
590 if (test_bit(DQ_ACTIVE_B, &dquot->dq_flags)) {
591 ret = fn(dquot, priv);
592 if (ret < 0)
593 goto out;
594 }
Jan Kara12c77522008-10-20 17:05:00 +0200595 spin_lock(&dq_list_lock);
596 /* We are safe to continue now because our dquot could not
597 * be moved out of the inuse list while we hold the reference */
598 }
599 spin_unlock(&dq_list_lock);
600out:
601 dqput(old_dquot);
Jan Kara12c77522008-10-20 17:05:00 +0200602 return ret;
603}
Mingming Cao08d03502009-01-14 16:19:32 +0100604EXPORT_SYMBOL(dquot_scan_active);
Jan Kara12c77522008-10-20 17:05:00 +0200605
Jan Karaceed1722012-07-03 16:45:28 +0200606/* Write all dquot structures to quota files */
607int dquot_writeback_dquots(struct super_block *sb, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608{
609 struct list_head *dirty;
610 struct dquot *dquot;
611 struct quota_info *dqopt = sb_dqopt(sb);
612 int cnt;
Jan Karaceed1722012-07-03 16:45:28 +0200613 int err, ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614
Jan Kara9d1ccbe2016-11-23 13:35:14 +0100615 WARN_ON_ONCE(!rwsem_is_locked(&sb->s_umount));
616
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
618 if (type != -1 && cnt != type)
619 continue;
Jan Karaf55abc02008-08-20 17:50:32 +0200620 if (!sb_has_quota_active(sb, cnt))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 continue;
622 spin_lock(&dq_list_lock);
623 dirty = &dqopt->info[cnt].dqi_dirty_list;
624 while (!list_empty(dirty)) {
Jan Kara268157b2009-01-27 15:47:22 +0100625 dquot = list_first_entry(dirty, struct dquot,
626 dq_dirty);
Jan Kara4580b302017-08-02 11:44:38 +0200627
628 WARN_ON(!test_bit(DQ_ACTIVE_B, &dquot->dq_flags));
629
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 /* Now we have active dquot from which someone is
631 * holding reference so we can safely just increase
632 * use count */
Jan Kara9f985cb2014-04-03 14:46:55 -0700633 dqgrab(dquot);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 spin_unlock(&dq_list_lock);
Dmitry Monakhovdde95882010-04-26 20:03:33 +0400635 dqstats_inc(DQST_LOOKUPS);
Jan Karaceed1722012-07-03 16:45:28 +0200636 err = sb->dq_op->write_dquot(dquot);
637 if (!ret && err)
Jan Kara474d2602014-10-22 09:06:49 +0200638 ret = err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 dqput(dquot);
640 spin_lock(&dq_list_lock);
641 }
642 spin_unlock(&dq_list_lock);
643 }
644
645 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
Jan Karaf55abc02008-08-20 17:50:32 +0200646 if ((cnt == type || type == -1) && sb_has_quota_active(sb, cnt)
647 && info_dirty(&dqopt->info[cnt]))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 sb->dq_op->write_info(sb, cnt);
Dmitry Monakhovdde95882010-04-26 20:03:33 +0400649 dqstats_inc(DQST_SYNCS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650
Jan Karaceed1722012-07-03 16:45:28 +0200651 return ret;
652}
653EXPORT_SYMBOL(dquot_writeback_dquots);
654
655/* Write all dquot structures to disk and make them visible from userspace */
656int dquot_quota_sync(struct super_block *sb, int type)
657{
658 struct quota_info *dqopt = sb_dqopt(sb);
659 int cnt;
660 int ret;
661
662 ret = dquot_writeback_dquots(sb, type);
663 if (ret)
664 return ret;
665 if (dqopt->flags & DQUOT_QUOTA_SYS_FILE)
Christoph Hellwig5fb324a2010-02-16 03:44:52 -0500666 return 0;
667
668 /* This is not very clever (and fast) but currently I don't know about
669 * any other simple way of getting quota data to disk and we must get
670 * them there for userspace to be visible... */
671 if (sb->s_op->sync_fs)
672 sb->s_op->sync_fs(sb, 1);
673 sync_blockdev(sb->s_bdev);
674
675 /*
676 * Now when everything is written we can discard the pagecache so
677 * that userspace sees the changes.
678 */
Christoph Hellwig5fb324a2010-02-16 03:44:52 -0500679 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
680 if (type != -1 && cnt != type)
681 continue;
682 if (!sb_has_quota_active(sb, cnt))
683 continue;
Al Viro59551022016-01-22 15:40:57 -0500684 inode_lock(dqopt->files[cnt]);
Jan Karaf9ef1782012-04-25 21:10:31 +0200685 truncate_inode_pages(&dqopt->files[cnt]->i_data, 0);
Al Viro59551022016-01-22 15:40:57 -0500686 inode_unlock(dqopt->files[cnt]);
Christoph Hellwig5fb324a2010-02-16 03:44:52 -0500687 }
Christoph Hellwig5fb324a2010-02-16 03:44:52 -0500688
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 return 0;
690}
Christoph Hellwig287a8092010-05-19 07:16:45 -0400691EXPORT_SYMBOL(dquot_quota_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692
Dave Chinner1ab6c492013-08-28 10:18:09 +1000693static unsigned long
694dqcache_shrink_scan(struct shrinker *shrink, struct shrink_control *sc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695{
696 struct list_head *head;
697 struct dquot *dquot;
Dave Chinner1ab6c492013-08-28 10:18:09 +1000698 unsigned long freed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699
Niu Yaweid68aab6b2014-06-04 12:22:13 +0800700 spin_lock(&dq_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 head = free_dquots.prev;
Dave Chinner1ab6c492013-08-28 10:18:09 +1000702 while (head != &free_dquots && sc->nr_to_scan) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 dquot = list_entry(head, struct dquot, dq_free);
704 remove_dquot_hash(dquot);
705 remove_free_dquot(dquot);
706 remove_inuse(dquot);
Jan Kara74f783a2008-08-19 14:51:22 +0200707 do_destroy_dquot(dquot);
Dave Chinner1ab6c492013-08-28 10:18:09 +1000708 sc->nr_to_scan--;
709 freed++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 head = free_dquots.prev;
711 }
Niu Yaweid68aab6b2014-06-04 12:22:13 +0800712 spin_unlock(&dq_list_lock);
Dave Chinner1ab6c492013-08-28 10:18:09 +1000713 return freed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714}
715
Dave Chinner1ab6c492013-08-28 10:18:09 +1000716static unsigned long
717dqcache_shrink_count(struct shrinker *shrink, struct shrink_control *sc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718{
Glauber Costa55f841c2013-08-28 10:17:53 +1000719 return vfs_pressure_ratio(
720 percpu_counter_read_positive(&dqstats.counter[DQST_FREE_DQUOTS]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721}
722
Rusty Russell8e1f9362007-07-17 04:03:17 -0700723static struct shrinker dqcache_shrinker = {
Dave Chinner1ab6c492013-08-28 10:18:09 +1000724 .count_objects = dqcache_shrink_count,
725 .scan_objects = dqcache_shrink_scan,
Rusty Russell8e1f9362007-07-17 04:03:17 -0700726 .seeks = DEFAULT_SEEKS,
727};
728
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729/*
730 * Put reference to dquot
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 */
Jan Kara3d9ea252008-10-10 16:12:23 +0200732void dqput(struct dquot *dquot)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733{
Jan Karab48d3802008-07-25 01:46:49 -0700734 int ret;
735
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 if (!dquot)
737 return;
Jan Kara62af9b52010-04-19 16:47:20 +0200738#ifdef CONFIG_QUOTA_DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 if (!atomic_read(&dquot->dq_count)) {
Jiaying Zhangfb5ffb02010-07-20 16:54:43 +0200740 quota_error(dquot->dq_sb, "trying to free free dquot of %s %d",
Eric W. Biederman4c376dc2012-09-16 03:56:19 -0700741 quotatypes[dquot->dq_id.type],
742 from_kqid(&init_user_ns, dquot->dq_id));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 BUG();
744 }
745#endif
Dmitry Monakhovdde95882010-04-26 20:03:33 +0400746 dqstats_inc(DQST_DROPS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747we_slept:
748 spin_lock(&dq_list_lock);
749 if (atomic_read(&dquot->dq_count) > 1) {
750 /* We have more than one user... nothing to do */
751 atomic_dec(&dquot->dq_count);
Jan Kara6362e4d2006-03-23 03:00:17 -0800752 /* Releasing dquot during quotaoff phase? */
Eric W. Biederman4c376dc2012-09-16 03:56:19 -0700753 if (!sb_has_quota_active(dquot->dq_sb, dquot->dq_id.type) &&
Jan Kara6362e4d2006-03-23 03:00:17 -0800754 atomic_read(&dquot->dq_count) == 1)
Jan Kara503330f2017-08-02 17:18:50 +0200755 wake_up(&dquot_ref_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 spin_unlock(&dq_list_lock);
757 return;
758 }
759 /* Need to release dquot? */
Jan Kara4580b302017-08-02 11:44:38 +0200760 if (dquot_dirty(dquot)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 spin_unlock(&dq_list_lock);
762 /* Commit dquot before releasing */
Jan Karab48d3802008-07-25 01:46:49 -0700763 ret = dquot->dq_sb->dq_op->write_dquot(dquot);
764 if (ret < 0) {
Jiaying Zhangfb5ffb02010-07-20 16:54:43 +0200765 quota_error(dquot->dq_sb, "Can't write quota structure"
766 " (error %d). Quota may get out of sync!",
767 ret);
Jan Karab48d3802008-07-25 01:46:49 -0700768 /*
769 * We clear dirty bit anyway, so that we avoid
770 * infinite loop here
771 */
Jan Karab48d3802008-07-25 01:46:49 -0700772 clear_dquot_dirty(dquot);
Jan Karab48d3802008-07-25 01:46:49 -0700773 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 goto we_slept;
775 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 if (test_bit(DQ_ACTIVE_B, &dquot->dq_flags)) {
777 spin_unlock(&dq_list_lock);
778 dquot->dq_sb->dq_op->release_dquot(dquot);
779 goto we_slept;
780 }
781 atomic_dec(&dquot->dq_count);
Jan Kara62af9b52010-04-19 16:47:20 +0200782#ifdef CONFIG_QUOTA_DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 /* sanity check */
Eric Sesterhenn8abf6a42006-04-02 13:36:13 +0200784 BUG_ON(!list_empty(&dquot->dq_free));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785#endif
786 put_dquot_last(dquot);
787 spin_unlock(&dq_list_lock);
788}
Mingming Cao08d03502009-01-14 16:19:32 +0100789EXPORT_SYMBOL(dqput);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790
Jan Kara7d9056b2008-11-25 15:31:32 +0100791struct dquot *dquot_alloc(struct super_block *sb, int type)
Jan Kara74f783a2008-08-19 14:51:22 +0200792{
793 return kmem_cache_zalloc(dquot_cachep, GFP_NOFS);
794}
Jan Kara7d9056b2008-11-25 15:31:32 +0100795EXPORT_SYMBOL(dquot_alloc);
Jan Kara74f783a2008-08-19 14:51:22 +0200796
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797static struct dquot *get_empty_dquot(struct super_block *sb, int type)
798{
799 struct dquot *dquot;
800
Jan Kara74f783a2008-08-19 14:51:22 +0200801 dquot = sb->dq_op->alloc_dquot(sb, type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 if(!dquot)
Jan Karadd6f3c62009-01-26 16:01:43 +0100803 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804
Ingo Molnard3be9152006-03-23 03:00:29 -0800805 mutex_init(&dquot->dq_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 INIT_LIST_HEAD(&dquot->dq_free);
807 INIT_LIST_HEAD(&dquot->dq_inuse);
808 INIT_HLIST_NODE(&dquot->dq_hash);
809 INIT_LIST_HEAD(&dquot->dq_dirty);
810 dquot->dq_sb = sb;
Eric W. Biederman1a06d422012-09-16 05:45:30 -0700811 dquot->dq_id = make_kqid_invalid(type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 atomic_set(&dquot->dq_count, 1);
813
814 return dquot;
815}
816
817/*
818 * Get reference to dquot
Jan Karacc334122009-01-12 17:23:05 +0100819 *
820 * Locking is slightly tricky here. We are guarded from parallel quotaoff()
821 * destroying our dquot by:
822 * a) checking for quota flags under dq_list_lock and
823 * b) getting a reference to dquot before we release dq_list_lock
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 */
Eric W. Biedermanaca645a2012-09-16 03:11:50 -0700825struct dquot *dqget(struct super_block *sb, struct kqid qid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826{
Eric W. Biederman1a06d422012-09-16 05:45:30 -0700827 unsigned int hashent = hashfn(sb, qid);
Jan Kara6184fc02015-06-24 18:07:02 +0200828 struct dquot *dquot, *empty = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829
Eric W. Biedermand49d3762016-06-30 16:31:01 -0500830 if (!qid_has_mapping(sb->s_user_ns, qid))
831 return ERR_PTR(-EINVAL);
832
Eric W. Biederman1a06d422012-09-16 05:45:30 -0700833 if (!sb_has_quota_active(sb, qid.type))
Jan Kara6184fc02015-06-24 18:07:02 +0200834 return ERR_PTR(-ESRCH);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835we_slept:
836 spin_lock(&dq_list_lock);
Jan Karacc334122009-01-12 17:23:05 +0100837 spin_lock(&dq_state_lock);
Eric W. Biederman1a06d422012-09-16 05:45:30 -0700838 if (!sb_has_quota_active(sb, qid.type)) {
Jan Karacc334122009-01-12 17:23:05 +0100839 spin_unlock(&dq_state_lock);
840 spin_unlock(&dq_list_lock);
Jan Kara6184fc02015-06-24 18:07:02 +0200841 dquot = ERR_PTR(-ESRCH);
Jan Karacc334122009-01-12 17:23:05 +0100842 goto out;
843 }
844 spin_unlock(&dq_state_lock);
845
Eric W. Biederman1a06d422012-09-16 05:45:30 -0700846 dquot = find_dquot(hashent, sb, qid);
Jan Karadd6f3c62009-01-26 16:01:43 +0100847 if (!dquot) {
848 if (!empty) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 spin_unlock(&dq_list_lock);
Eric W. Biederman1a06d422012-09-16 05:45:30 -0700850 empty = get_empty_dquot(sb, qid.type);
Jan Karadd6f3c62009-01-26 16:01:43 +0100851 if (!empty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 schedule(); /* Try to wait for a moment... */
853 goto we_slept;
854 }
855 dquot = empty;
Jan Karadd6f3c62009-01-26 16:01:43 +0100856 empty = NULL;
Eric W. Biederman4c376dc2012-09-16 03:56:19 -0700857 dquot->dq_id = qid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 /* all dquots go on the inuse_list */
859 put_inuse(dquot);
860 /* hash it first so it can be found */
861 insert_dquot_hash(dquot);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 spin_unlock(&dq_list_lock);
Dmitry Monakhovdde95882010-04-26 20:03:33 +0400863 dqstats_inc(DQST_LOOKUPS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 } else {
865 if (!atomic_read(&dquot->dq_count))
866 remove_free_dquot(dquot);
867 atomic_inc(&dquot->dq_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 spin_unlock(&dq_list_lock);
Dmitry Monakhovdde95882010-04-26 20:03:33 +0400869 dqstats_inc(DQST_CACHE_HITS);
870 dqstats_inc(DQST_LOOKUPS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 }
Jan Kara268157b2009-01-27 15:47:22 +0100872 /* Wait for dq_lock - after this we know that either dquot_release() is
873 * already finished or it will be canceled due to dq_count > 1 test */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 wait_on_dquot(dquot);
Jan Kara268157b2009-01-27 15:47:22 +0100875 /* Read the dquot / allocate space in quota file */
Jan Kara6184fc02015-06-24 18:07:02 +0200876 if (!test_bit(DQ_ACTIVE_B, &dquot->dq_flags)) {
877 int err;
878
879 err = sb->dq_op->acquire_dquot(dquot);
880 if (err < 0) {
881 dqput(dquot);
882 dquot = ERR_PTR(err);
883 goto out;
884 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 }
Jan Kara044c9b62016-02-18 13:20:20 +0100886 /*
887 * Make sure following reads see filled structure - paired with
888 * smp_mb__before_atomic() in dquot_acquire().
889 */
890 smp_rmb();
Jan Kara62af9b52010-04-19 16:47:20 +0200891#ifdef CONFIG_QUOTA_DEBUG
Eric Sesterhenn8abf6a42006-04-02 13:36:13 +0200892 BUG_ON(!dquot->dq_sb); /* Has somebody invalidated entry under us? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893#endif
Jan Karacc334122009-01-12 17:23:05 +0100894out:
895 if (empty)
896 do_destroy_dquot(empty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897
898 return dquot;
899}
Mingming Cao08d03502009-01-14 16:19:32 +0100900EXPORT_SYMBOL(dqget);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901
Jan Kara2d0fa462014-09-25 16:36:14 +0200902static inline struct dquot **i_dquot(struct inode *inode)
903{
Jan Kara2d0fa462014-09-25 16:36:14 +0200904 return inode->i_sb->s_op->get_dquots(inode);
905}
906
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907static int dqinit_needed(struct inode *inode, int type)
908{
Konstantin Khlebnikov5bcd3b62015-02-12 12:36:37 +0300909 struct dquot * const *dquots;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 int cnt;
911
912 if (IS_NOQUOTA(inode))
913 return 0;
Konstantin Khlebnikov5bcd3b62015-02-12 12:36:37 +0300914
915 dquots = i_dquot(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 if (type != -1)
Konstantin Khlebnikov5bcd3b62015-02-12 12:36:37 +0300917 return !dquots[type];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
Konstantin Khlebnikov5bcd3b62015-02-12 12:36:37 +0300919 if (!dquots[cnt])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 return 1;
921 return 0;
922}
923
Jan Karac3b00442016-11-23 14:04:55 +0100924/* This routine is guarded by s_umount semaphore */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925static void add_dquot_ref(struct super_block *sb, int type)
926{
Jan Kara941d2382008-02-06 01:37:36 -0800927 struct inode *inode, *old_inode = NULL;
Jan Kara62af9b52010-04-19 16:47:20 +0200928#ifdef CONFIG_QUOTA_DEBUG
Jan Kara0a5a9c72010-02-09 18:20:39 +0100929 int reserved = 0;
Jan Kara4c5e6c02010-04-06 18:52:47 +0200930#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931
Dave Chinner74278da2015-03-04 12:37:22 -0500932 spin_lock(&sb->s_inode_list_lock);
Christoph Hellwigd003fb72007-02-12 00:51:58 -0800933 list_for_each_entry(inode, &sb->s_inodes, i_sb_list) {
Dave Chinner250df6e2011-03-22 22:23:36 +1100934 spin_lock(&inode->i_lock);
935 if ((inode->i_state & (I_FREEING|I_WILL_FREE|I_NEW)) ||
936 !atomic_read(&inode->i_writecount) ||
937 !dqinit_needed(inode, type)) {
938 spin_unlock(&inode->i_lock);
Nick Pigginaabb8fd2009-03-11 13:17:36 -0700939 continue;
Dave Chinner250df6e2011-03-22 22:23:36 +1100940 }
Christoph Hellwigd003fb72007-02-12 00:51:58 -0800941 __iget(inode);
Dave Chinner250df6e2011-03-22 22:23:36 +1100942 spin_unlock(&inode->i_lock);
Dave Chinner74278da2015-03-04 12:37:22 -0500943 spin_unlock(&sb->s_inode_list_lock);
Christoph Hellwigd003fb72007-02-12 00:51:58 -0800944
Jan Karad7e97112012-04-24 17:08:41 +0200945#ifdef CONFIG_QUOTA_DEBUG
946 if (unlikely(inode_get_rsv_space(inode) > 0))
947 reserved = 1;
948#endif
Jan Kara941d2382008-02-06 01:37:36 -0800949 iput(old_inode);
Christoph Hellwig871a2932010-03-03 09:05:07 -0500950 __dquot_initialize(inode, type);
Dave Chinner55fa6092011-03-22 22:23:40 +1100951
952 /*
953 * We hold a reference to 'inode' so it couldn't have been
954 * removed from s_inodes list while we dropped the
Dave Chinner74278da2015-03-04 12:37:22 -0500955 * s_inode_list_lock. We cannot iput the inode now as we can be
Dave Chinner55fa6092011-03-22 22:23:40 +1100956 * holding the last reference and we cannot iput it under
Dave Chinner74278da2015-03-04 12:37:22 -0500957 * s_inode_list_lock. So we keep the reference and iput it
Dave Chinner55fa6092011-03-22 22:23:40 +1100958 * later.
959 */
Jan Kara941d2382008-02-06 01:37:36 -0800960 old_inode = inode;
Dave Chinner74278da2015-03-04 12:37:22 -0500961 spin_lock(&sb->s_inode_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 }
Dave Chinner74278da2015-03-04 12:37:22 -0500963 spin_unlock(&sb->s_inode_list_lock);
Jan Kara941d2382008-02-06 01:37:36 -0800964 iput(old_inode);
Jan Kara0a5a9c72010-02-09 18:20:39 +0100965
Jan Kara62af9b52010-04-19 16:47:20 +0200966#ifdef CONFIG_QUOTA_DEBUG
Jan Kara0a5a9c72010-02-09 18:20:39 +0100967 if (reserved) {
Jiaying Zhangfb5ffb02010-07-20 16:54:43 +0200968 quota_error(sb, "Writes happened before quota was turned on "
969 "thus quota information is probably inconsistent. "
970 "Please run quotacheck(8)");
Jan Kara0a5a9c72010-02-09 18:20:39 +0100971 }
Jan Kara4c5e6c02010-04-06 18:52:47 +0200972#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973}
974
Jan Kara268157b2009-01-27 15:47:22 +0100975/*
Jan Kara268157b2009-01-27 15:47:22 +0100976 * Remove references to dquots from inode and add dquot to list for freeing
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300977 * if we have the last reference to dquot
Jan Kara268157b2009-01-27 15:47:22 +0100978 */
Niu Yawei9eb64632014-06-04 12:21:30 +0800979static void remove_inode_dquot_ref(struct inode *inode, int type,
980 struct list_head *tofree_head)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981{
Konstantin Khlebnikov5bcd3b62015-02-12 12:36:37 +0300982 struct dquot **dquots = i_dquot(inode);
983 struct dquot *dquot = dquots[type];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984
Niu Yawei9eb64632014-06-04 12:21:30 +0800985 if (!dquot)
986 return;
987
Konstantin Khlebnikov5bcd3b62015-02-12 12:36:37 +0300988 dquots[type] = NULL;
Niu Yawei9eb64632014-06-04 12:21:30 +0800989 if (list_empty(&dquot->dq_free)) {
990 /*
991 * The inode still has reference to dquot so it can't be in the
992 * free list
993 */
994 spin_lock(&dq_list_lock);
995 list_add(&dquot->dq_free, tofree_head);
996 spin_unlock(&dq_list_lock);
997 } else {
998 /*
999 * Dquot is already in a list to put so we won't drop the last
1000 * reference here.
1001 */
1002 dqput(dquot);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004}
1005
Jan Kara268157b2009-01-27 15:47:22 +01001006/*
1007 * Free list of dquots
1008 * Dquots are removed from inodes and no new references can be got so we are
1009 * the only ones holding reference
1010 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011static void put_dquot_list(struct list_head *tofree_head)
1012{
1013 struct list_head *act_head;
1014 struct dquot *dquot;
1015
1016 act_head = tofree_head->next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 while (act_head != tofree_head) {
1018 dquot = list_entry(act_head, struct dquot, dq_free);
1019 act_head = act_head->next;
Jan Kara268157b2009-01-27 15:47:22 +01001020 /* Remove dquot from the list so we won't have problems... */
1021 list_del_init(&dquot->dq_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 dqput(dquot);
1023 }
1024}
1025
Christoph Hellwigfb58b732007-02-12 00:51:57 -08001026static void remove_dquot_ref(struct super_block *sb, int type,
1027 struct list_head *tofree_head)
1028{
1029 struct inode *inode;
Dmitry Monakhov7af9cce2010-06-01 11:39:48 +04001030 int reserved = 0;
Christoph Hellwigfb58b732007-02-12 00:51:57 -08001031
Dave Chinner74278da2015-03-04 12:37:22 -05001032 spin_lock(&sb->s_inode_list_lock);
Christoph Hellwigfb58b732007-02-12 00:51:57 -08001033 list_for_each_entry(inode, &sb->s_inodes, i_sb_list) {
Nick Pigginaabb8fd2009-03-11 13:17:36 -07001034 /*
1035 * We have to scan also I_NEW inodes because they can already
1036 * have quota pointer initialized. Luckily, we need to touch
1037 * only quota pointers and these have separate locking
Niu Yaweib9ba6f92014-06-04 12:23:19 +08001038 * (dq_data_lock).
Nick Pigginaabb8fd2009-03-11 13:17:36 -07001039 */
Niu Yaweib9ba6f92014-06-04 12:23:19 +08001040 spin_lock(&dq_data_lock);
Dmitry Monakhov7af9cce2010-06-01 11:39:48 +04001041 if (!IS_NOQUOTA(inode)) {
1042 if (unlikely(inode_get_rsv_space(inode) > 0))
1043 reserved = 1;
Christoph Hellwigfb58b732007-02-12 00:51:57 -08001044 remove_inode_dquot_ref(inode, type, tofree_head);
Dmitry Monakhov7af9cce2010-06-01 11:39:48 +04001045 }
Niu Yaweib9ba6f92014-06-04 12:23:19 +08001046 spin_unlock(&dq_data_lock);
Christoph Hellwigfb58b732007-02-12 00:51:57 -08001047 }
Dave Chinner74278da2015-03-04 12:37:22 -05001048 spin_unlock(&sb->s_inode_list_lock);
Dmitry Monakhov7af9cce2010-06-01 11:39:48 +04001049#ifdef CONFIG_QUOTA_DEBUG
1050 if (reserved) {
1051 printk(KERN_WARNING "VFS (%s): Writes happened after quota"
1052 " was disabled thus quota information is probably "
1053 "inconsistent. Please run quotacheck(8).\n", sb->s_id);
1054 }
1055#endif
Christoph Hellwigfb58b732007-02-12 00:51:57 -08001056}
1057
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058/* Gather all references from inodes and drop them */
1059static void drop_dquot_ref(struct super_block *sb, int type)
1060{
1061 LIST_HEAD(tofree_head);
1062
Christoph Hellwigfb58b732007-02-12 00:51:57 -08001063 if (sb->dq_op) {
Christoph Hellwigfb58b732007-02-12 00:51:57 -08001064 remove_dquot_ref(sb, type, &tofree_head);
Niu Yaweib9ba6f92014-06-04 12:23:19 +08001065 synchronize_srcu(&dquot_srcu);
Christoph Hellwigfb58b732007-02-12 00:51:57 -08001066 put_dquot_list(&tofree_head);
1067 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068}
1069
Jan Kara12095462008-08-20 14:45:12 +02001070static inline void dquot_incr_inodes(struct dquot *dquot, qsize_t number)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071{
1072 dquot->dq_dqb.dqb_curinodes += number;
1073}
1074
1075static inline void dquot_incr_space(struct dquot *dquot, qsize_t number)
1076{
1077 dquot->dq_dqb.dqb_curspace += number;
1078}
1079
Mingming Caof18df222009-01-13 16:43:09 +01001080static inline void dquot_resv_space(struct dquot *dquot, qsize_t number)
1081{
1082 dquot->dq_dqb.dqb_rsvspace += number;
1083}
1084
Mingming Cao740d9dc2009-01-13 16:43:14 +01001085/*
1086 * Claim reserved quota space
1087 */
Jan Kara0a5a9c72010-02-09 18:20:39 +01001088static void dquot_claim_reserved_space(struct dquot *dquot, qsize_t number)
Mingming Cao740d9dc2009-01-13 16:43:14 +01001089{
Jan Kara0a5a9c72010-02-09 18:20:39 +01001090 if (dquot->dq_dqb.dqb_rsvspace < number) {
1091 WARN_ON_ONCE(1);
1092 number = dquot->dq_dqb.dqb_rsvspace;
1093 }
Mingming Cao740d9dc2009-01-13 16:43:14 +01001094 dquot->dq_dqb.dqb_curspace += number;
1095 dquot->dq_dqb.dqb_rsvspace -= number;
1096}
1097
Jan Kara1c8924e2013-08-17 09:32:32 -04001098static void dquot_reclaim_reserved_space(struct dquot *dquot, qsize_t number)
1099{
1100 if (WARN_ON_ONCE(dquot->dq_dqb.dqb_curspace < number))
1101 number = dquot->dq_dqb.dqb_curspace;
1102 dquot->dq_dqb.dqb_rsvspace += number;
1103 dquot->dq_dqb.dqb_curspace -= number;
1104}
1105
Mingming Cao740d9dc2009-01-13 16:43:14 +01001106static inline
1107void dquot_free_reserved_space(struct dquot *dquot, qsize_t number)
1108{
Jan Kara0a5a9c72010-02-09 18:20:39 +01001109 if (dquot->dq_dqb.dqb_rsvspace >= number)
1110 dquot->dq_dqb.dqb_rsvspace -= number;
1111 else {
1112 WARN_ON_ONCE(1);
1113 dquot->dq_dqb.dqb_rsvspace = 0;
1114 }
Mingming Cao740d9dc2009-01-13 16:43:14 +01001115}
1116
Jan Kara7a2435d2009-01-27 01:47:11 +01001117static void dquot_decr_inodes(struct dquot *dquot, qsize_t number)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118{
Jan Karadb49d2d2008-10-01 18:21:39 +02001119 if (sb_dqopt(dquot->dq_sb)->flags & DQUOT_NEGATIVE_USAGE ||
1120 dquot->dq_dqb.dqb_curinodes >= number)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 dquot->dq_dqb.dqb_curinodes -= number;
1122 else
1123 dquot->dq_dqb.dqb_curinodes = 0;
1124 if (dquot->dq_dqb.dqb_curinodes <= dquot->dq_dqb.dqb_isoftlimit)
Arnd Bergmanne008bb62016-06-17 22:03:16 +02001125 dquot->dq_dqb.dqb_itime = (time64_t) 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 clear_bit(DQ_INODES_B, &dquot->dq_flags);
1127}
1128
Jan Kara7a2435d2009-01-27 01:47:11 +01001129static void dquot_decr_space(struct dquot *dquot, qsize_t number)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130{
Jan Karadb49d2d2008-10-01 18:21:39 +02001131 if (sb_dqopt(dquot->dq_sb)->flags & DQUOT_NEGATIVE_USAGE ||
1132 dquot->dq_dqb.dqb_curspace >= number)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 dquot->dq_dqb.dqb_curspace -= number;
1134 else
1135 dquot->dq_dqb.dqb_curspace = 0;
Jan Kara12095462008-08-20 14:45:12 +02001136 if (dquot->dq_dqb.dqb_curspace <= dquot->dq_dqb.dqb_bsoftlimit)
Arnd Bergmanne008bb62016-06-17 22:03:16 +02001137 dquot->dq_dqb.dqb_btime = (time64_t) 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 clear_bit(DQ_BLKS_B, &dquot->dq_flags);
1139}
1140
Jan Karabf097aa2012-02-14 13:28:01 +01001141struct dquot_warn {
1142 struct super_block *w_sb;
Eric W. Biederman7b9c7322012-09-16 04:05:34 -07001143 struct kqid w_dq_id;
Jan Karabf097aa2012-02-14 13:28:01 +01001144 short w_type;
1145};
1146
Jan Karac5254602007-12-22 14:03:25 -08001147static int warning_issued(struct dquot *dquot, const int warntype)
1148{
1149 int flag = (warntype == QUOTA_NL_BHARDWARN ||
1150 warntype == QUOTA_NL_BSOFTLONGWARN) ? DQ_BLKS_B :
1151 ((warntype == QUOTA_NL_IHARDWARN ||
1152 warntype == QUOTA_NL_ISOFTLONGWARN) ? DQ_INODES_B : 0);
1153
1154 if (!flag)
1155 return 0;
1156 return test_and_set_bit(flag, &dquot->dq_flags);
1157}
1158
Jan Kara8e893462007-10-16 23:29:31 -07001159#ifdef CONFIG_PRINT_QUOTA_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160static int flag_print_warnings = 1;
1161
Jan Karabf097aa2012-02-14 13:28:01 +01001162static int need_print_warning(struct dquot_warn *warn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163{
1164 if (!flag_print_warnings)
1165 return 0;
1166
Eric W. Biederman7b9c7322012-09-16 04:05:34 -07001167 switch (warn->w_dq_id.type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 case USRQUOTA:
Eric W. Biederman1a06d422012-09-16 05:45:30 -07001169 return uid_eq(current_fsuid(), warn->w_dq_id.uid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 case GRPQUOTA:
Eric W. Biederman7b9c7322012-09-16 04:05:34 -07001171 return in_group_p(warn->w_dq_id.gid);
Li Xi847aac62015-03-19 04:04:53 +09001172 case PRJQUOTA:
1173 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 }
1175 return 0;
1176}
1177
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178/* Print warning to user which exceeded quota */
Jan Karabf097aa2012-02-14 13:28:01 +01001179static void print_warning(struct dquot_warn *warn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180{
1181 char *msg = NULL;
Peter Zijlstra24ec8392006-12-08 02:36:04 -08001182 struct tty_struct *tty;
Jan Karabf097aa2012-02-14 13:28:01 +01001183 int warntype = warn->w_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184
Jan Kara657d3bf2008-07-25 01:46:52 -07001185 if (warntype == QUOTA_NL_IHARDBELOW ||
1186 warntype == QUOTA_NL_ISOFTBELOW ||
1187 warntype == QUOTA_NL_BHARDBELOW ||
Jan Karabf097aa2012-02-14 13:28:01 +01001188 warntype == QUOTA_NL_BSOFTBELOW || !need_print_warning(warn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 return;
1190
Peter Zijlstra24ec8392006-12-08 02:36:04 -08001191 tty = get_current_tty();
1192 if (!tty)
Alan Cox452a00d2008-10-13 10:39:13 +01001193 return;
Jan Karabf097aa2012-02-14 13:28:01 +01001194 tty_write_message(tty, warn->w_sb->s_id);
Jan Kara8e893462007-10-16 23:29:31 -07001195 if (warntype == QUOTA_NL_ISOFTWARN || warntype == QUOTA_NL_BSOFTWARN)
Peter Zijlstra24ec8392006-12-08 02:36:04 -08001196 tty_write_message(tty, ": warning, ");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 else
Peter Zijlstra24ec8392006-12-08 02:36:04 -08001198 tty_write_message(tty, ": write failed, ");
Eric W. Biederman7b9c7322012-09-16 04:05:34 -07001199 tty_write_message(tty, quotatypes[warn->w_dq_id.type]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 switch (warntype) {
Jan Kara8e893462007-10-16 23:29:31 -07001201 case QUOTA_NL_IHARDWARN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 msg = " file limit reached.\r\n";
1203 break;
Jan Kara8e893462007-10-16 23:29:31 -07001204 case QUOTA_NL_ISOFTLONGWARN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 msg = " file quota exceeded too long.\r\n";
1206 break;
Jan Kara8e893462007-10-16 23:29:31 -07001207 case QUOTA_NL_ISOFTWARN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 msg = " file quota exceeded.\r\n";
1209 break;
Jan Kara8e893462007-10-16 23:29:31 -07001210 case QUOTA_NL_BHARDWARN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 msg = " block limit reached.\r\n";
1212 break;
Jan Kara8e893462007-10-16 23:29:31 -07001213 case QUOTA_NL_BSOFTLONGWARN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 msg = " block quota exceeded too long.\r\n";
1215 break;
Jan Kara8e893462007-10-16 23:29:31 -07001216 case QUOTA_NL_BSOFTWARN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 msg = " block quota exceeded.\r\n";
1218 break;
1219 }
Peter Zijlstra24ec8392006-12-08 02:36:04 -08001220 tty_write_message(tty, msg);
Alan Cox452a00d2008-10-13 10:39:13 +01001221 tty_kref_put(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222}
Jan Kara8e893462007-10-16 23:29:31 -07001223#endif
1224
Jan Karabf097aa2012-02-14 13:28:01 +01001225static void prepare_warning(struct dquot_warn *warn, struct dquot *dquot,
1226 int warntype)
1227{
1228 if (warning_issued(dquot, warntype))
1229 return;
1230 warn->w_type = warntype;
1231 warn->w_sb = dquot->dq_sb;
1232 warn->w_dq_id = dquot->dq_id;
Jan Karabf097aa2012-02-14 13:28:01 +01001233}
1234
Mingming Caof18df222009-01-13 16:43:09 +01001235/*
1236 * Write warnings to the console and send warning messages over netlink.
1237 *
Jan Karabf097aa2012-02-14 13:28:01 +01001238 * Note that this function can call into tty and networking code.
Mingming Caof18df222009-01-13 16:43:09 +01001239 */
Jan Karabf097aa2012-02-14 13:28:01 +01001240static void flush_warnings(struct dquot_warn *warn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241{
1242 int i;
1243
Steven Whitehouse86e931a2009-09-28 12:35:17 +01001244 for (i = 0; i < MAXQUOTAS; i++) {
Jan Karabf097aa2012-02-14 13:28:01 +01001245 if (warn[i].w_type == QUOTA_NL_NOWARN)
1246 continue;
Jan Kara8e893462007-10-16 23:29:31 -07001247#ifdef CONFIG_PRINT_QUOTA_WARNING
Jan Karabf097aa2012-02-14 13:28:01 +01001248 print_warning(&warn[i]);
Jan Kara8e893462007-10-16 23:29:31 -07001249#endif
Eric W. Biederman7b9c7322012-09-16 04:05:34 -07001250 quota_send_warning(warn[i].w_dq_id,
Jan Karabf097aa2012-02-14 13:28:01 +01001251 warn[i].w_sb->s_dev, warn[i].w_type);
Steven Whitehouse86e931a2009-09-28 12:35:17 +01001252 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253}
1254
Jan Kara7a2435d2009-01-27 01:47:11 +01001255static int ignore_hardlimit(struct dquot *dquot)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256{
Eric W. Biederman4c376dc2012-09-16 03:56:19 -07001257 struct mem_dqinfo *info = &sb_dqopt(dquot->dq_sb)->info[dquot->dq_id.type];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258
1259 return capable(CAP_SYS_RESOURCE) &&
Jan Kara268157b2009-01-27 15:47:22 +01001260 (info->dqi_format->qf_fmt_id != QFMT_VFS_OLD ||
Jan Kara9c451012014-11-19 09:21:58 +01001261 !(info->dqi_flags & DQF_ROOT_SQUASH));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262}
1263
1264/* needs dq_data_lock */
Jan Karabf097aa2012-02-14 13:28:01 +01001265static int check_idq(struct dquot *dquot, qsize_t inodes,
1266 struct dquot_warn *warn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267{
Jan Kara268157b2009-01-27 15:47:22 +01001268 qsize_t newinodes = dquot->dq_dqb.dqb_curinodes + inodes;
1269
Eric W. Biederman4c376dc2012-09-16 03:56:19 -07001270 if (!sb_has_quota_limits_enabled(dquot->dq_sb, dquot->dq_id.type) ||
Jan Karaf55abc02008-08-20 17:50:32 +02001271 test_bit(DQ_FAKE_B, &dquot->dq_flags))
Christoph Hellwigefd8f0e2010-03-03 09:05:08 -05001272 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273
1274 if (dquot->dq_dqb.dqb_ihardlimit &&
Jan Kara268157b2009-01-27 15:47:22 +01001275 newinodes > dquot->dq_dqb.dqb_ihardlimit &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 !ignore_hardlimit(dquot)) {
Jan Karabf097aa2012-02-14 13:28:01 +01001277 prepare_warning(warn, dquot, QUOTA_NL_IHARDWARN);
Christoph Hellwigefd8f0e2010-03-03 09:05:08 -05001278 return -EDQUOT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 }
1280
1281 if (dquot->dq_dqb.dqb_isoftlimit &&
Jan Kara268157b2009-01-27 15:47:22 +01001282 newinodes > dquot->dq_dqb.dqb_isoftlimit &&
1283 dquot->dq_dqb.dqb_itime &&
Arnd Bergmanne008bb62016-06-17 22:03:16 +02001284 ktime_get_real_seconds() >= dquot->dq_dqb.dqb_itime &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 !ignore_hardlimit(dquot)) {
Jan Karabf097aa2012-02-14 13:28:01 +01001286 prepare_warning(warn, dquot, QUOTA_NL_ISOFTLONGWARN);
Christoph Hellwigefd8f0e2010-03-03 09:05:08 -05001287 return -EDQUOT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 }
1289
1290 if (dquot->dq_dqb.dqb_isoftlimit &&
Jan Kara268157b2009-01-27 15:47:22 +01001291 newinodes > dquot->dq_dqb.dqb_isoftlimit &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 dquot->dq_dqb.dqb_itime == 0) {
Jan Karabf097aa2012-02-14 13:28:01 +01001293 prepare_warning(warn, dquot, QUOTA_NL_ISOFTWARN);
Arnd Bergmanne008bb62016-06-17 22:03:16 +02001294 dquot->dq_dqb.dqb_itime = ktime_get_real_seconds() +
Eric W. Biederman4c376dc2012-09-16 03:56:19 -07001295 sb_dqopt(dquot->dq_sb)->info[dquot->dq_id.type].dqi_igrace;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 }
1297
Christoph Hellwigefd8f0e2010-03-03 09:05:08 -05001298 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299}
1300
1301/* needs dq_data_lock */
Jan Karabf097aa2012-02-14 13:28:01 +01001302static int check_bdq(struct dquot *dquot, qsize_t space, int prealloc,
1303 struct dquot_warn *warn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304{
Mingming Caof18df222009-01-13 16:43:09 +01001305 qsize_t tspace;
Jan Kara268157b2009-01-27 15:47:22 +01001306 struct super_block *sb = dquot->dq_sb;
Mingming Caof18df222009-01-13 16:43:09 +01001307
Eric W. Biederman4c376dc2012-09-16 03:56:19 -07001308 if (!sb_has_quota_limits_enabled(sb, dquot->dq_id.type) ||
Jan Karaf55abc02008-08-20 17:50:32 +02001309 test_bit(DQ_FAKE_B, &dquot->dq_flags))
Christoph Hellwigefd8f0e2010-03-03 09:05:08 -05001310 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311
Mingming Caof18df222009-01-13 16:43:09 +01001312 tspace = dquot->dq_dqb.dqb_curspace + dquot->dq_dqb.dqb_rsvspace
1313 + space;
1314
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 if (dquot->dq_dqb.dqb_bhardlimit &&
Mingming Caof18df222009-01-13 16:43:09 +01001316 tspace > dquot->dq_dqb.dqb_bhardlimit &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317 !ignore_hardlimit(dquot)) {
1318 if (!prealloc)
Jan Karabf097aa2012-02-14 13:28:01 +01001319 prepare_warning(warn, dquot, QUOTA_NL_BHARDWARN);
Christoph Hellwigefd8f0e2010-03-03 09:05:08 -05001320 return -EDQUOT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 }
1322
1323 if (dquot->dq_dqb.dqb_bsoftlimit &&
Mingming Caof18df222009-01-13 16:43:09 +01001324 tspace > dquot->dq_dqb.dqb_bsoftlimit &&
Jan Kara268157b2009-01-27 15:47:22 +01001325 dquot->dq_dqb.dqb_btime &&
Arnd Bergmanne008bb62016-06-17 22:03:16 +02001326 ktime_get_real_seconds() >= dquot->dq_dqb.dqb_btime &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 !ignore_hardlimit(dquot)) {
1328 if (!prealloc)
Jan Karabf097aa2012-02-14 13:28:01 +01001329 prepare_warning(warn, dquot, QUOTA_NL_BSOFTLONGWARN);
Christoph Hellwigefd8f0e2010-03-03 09:05:08 -05001330 return -EDQUOT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 }
1332
1333 if (dquot->dq_dqb.dqb_bsoftlimit &&
Mingming Caof18df222009-01-13 16:43:09 +01001334 tspace > dquot->dq_dqb.dqb_bsoftlimit &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335 dquot->dq_dqb.dqb_btime == 0) {
1336 if (!prealloc) {
Jan Karabf097aa2012-02-14 13:28:01 +01001337 prepare_warning(warn, dquot, QUOTA_NL_BSOFTWARN);
Arnd Bergmanne008bb62016-06-17 22:03:16 +02001338 dquot->dq_dqb.dqb_btime = ktime_get_real_seconds() +
Eric W. Biederman4c376dc2012-09-16 03:56:19 -07001339 sb_dqopt(sb)->info[dquot->dq_id.type].dqi_bgrace;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 }
1341 else
1342 /*
1343 * We don't allow preallocation to exceed softlimit so exceeding will
1344 * be always printed
1345 */
Christoph Hellwigefd8f0e2010-03-03 09:05:08 -05001346 return -EDQUOT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 }
1348
Christoph Hellwigefd8f0e2010-03-03 09:05:08 -05001349 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350}
1351
Jan Kara12095462008-08-20 14:45:12 +02001352static int info_idq_free(struct dquot *dquot, qsize_t inodes)
Jan Kara657d3bf2008-07-25 01:46:52 -07001353{
Jan Kara268157b2009-01-27 15:47:22 +01001354 qsize_t newinodes;
1355
Jan Kara657d3bf2008-07-25 01:46:52 -07001356 if (test_bit(DQ_FAKE_B, &dquot->dq_flags) ||
Jan Karaf55abc02008-08-20 17:50:32 +02001357 dquot->dq_dqb.dqb_curinodes <= dquot->dq_dqb.dqb_isoftlimit ||
Eric W. Biederman4c376dc2012-09-16 03:56:19 -07001358 !sb_has_quota_limits_enabled(dquot->dq_sb, dquot->dq_id.type))
Jan Kara657d3bf2008-07-25 01:46:52 -07001359 return QUOTA_NL_NOWARN;
1360
Jan Kara268157b2009-01-27 15:47:22 +01001361 newinodes = dquot->dq_dqb.dqb_curinodes - inodes;
1362 if (newinodes <= dquot->dq_dqb.dqb_isoftlimit)
Jan Kara657d3bf2008-07-25 01:46:52 -07001363 return QUOTA_NL_ISOFTBELOW;
1364 if (dquot->dq_dqb.dqb_curinodes >= dquot->dq_dqb.dqb_ihardlimit &&
Jan Kara268157b2009-01-27 15:47:22 +01001365 newinodes < dquot->dq_dqb.dqb_ihardlimit)
Jan Kara657d3bf2008-07-25 01:46:52 -07001366 return QUOTA_NL_IHARDBELOW;
1367 return QUOTA_NL_NOWARN;
1368}
1369
1370static int info_bdq_free(struct dquot *dquot, qsize_t space)
1371{
1372 if (test_bit(DQ_FAKE_B, &dquot->dq_flags) ||
Jan Kara12095462008-08-20 14:45:12 +02001373 dquot->dq_dqb.dqb_curspace <= dquot->dq_dqb.dqb_bsoftlimit)
Jan Kara657d3bf2008-07-25 01:46:52 -07001374 return QUOTA_NL_NOWARN;
1375
Jan Kara12095462008-08-20 14:45:12 +02001376 if (dquot->dq_dqb.dqb_curspace - space <= dquot->dq_dqb.dqb_bsoftlimit)
Jan Kara657d3bf2008-07-25 01:46:52 -07001377 return QUOTA_NL_BSOFTBELOW;
Jan Kara12095462008-08-20 14:45:12 +02001378 if (dquot->dq_dqb.dqb_curspace >= dquot->dq_dqb.dqb_bhardlimit &&
1379 dquot->dq_dqb.dqb_curspace - space < dquot->dq_dqb.dqb_bhardlimit)
Jan Kara657d3bf2008-07-25 01:46:52 -07001380 return QUOTA_NL_BHARDBELOW;
1381 return QUOTA_NL_NOWARN;
1382}
Jan Kara0a5a9c72010-02-09 18:20:39 +01001383
Christoph Hellwig189eef52010-06-04 10:56:29 +02001384static int dquot_active(const struct inode *inode)
1385{
1386 struct super_block *sb = inode->i_sb;
1387
1388 if (IS_NOQUOTA(inode))
1389 return 0;
1390 return sb_any_quota_loaded(sb) & ~sb_any_quota_suspended(sb);
1391}
1392
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393/*
Christoph Hellwig871a2932010-03-03 09:05:07 -05001394 * Initialize quota pointers in inode
1395 *
Christoph Hellwig871a2932010-03-03 09:05:07 -05001396 * It is better to call this function outside of any transaction as it
1397 * might need a lot of space in journal for dquot structure allocation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 */
Jan Kara6184fc02015-06-24 18:07:02 +02001399static int __dquot_initialize(struct inode *inode, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400{
Niu Yawei1ea06be2014-06-04 12:20:30 +08001401 int cnt, init_needed = 0;
Nikolay Borisovab73ef42016-03-03 10:54:57 +01001402 struct dquot **dquots, *got[MAXQUOTAS] = {};
Jan Karacc334122009-01-12 17:23:05 +01001403 struct super_block *sb = inode->i_sb;
Jan Kara0a5a9c72010-02-09 18:20:39 +01001404 qsize_t rsv;
Jan Kara6184fc02015-06-24 18:07:02 +02001405 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406
Christoph Hellwig189eef52010-06-04 10:56:29 +02001407 if (!dquot_active(inode))
Jan Kara6184fc02015-06-24 18:07:02 +02001408 return 0;
Jan Karacc334122009-01-12 17:23:05 +01001409
Konstantin Khlebnikov5bcd3b62015-02-12 12:36:37 +03001410 dquots = i_dquot(inode);
1411
Jan Karacc334122009-01-12 17:23:05 +01001412 /* First get references to structures we might need. */
1413 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
Eric W. Biedermanaca645a2012-09-16 03:11:50 -07001414 struct kqid qid;
Li Xi847aac62015-03-19 04:04:53 +09001415 kprojid_t projid;
1416 int rc;
Jan Kara6184fc02015-06-24 18:07:02 +02001417 struct dquot *dquot;
Li Xi847aac62015-03-19 04:04:53 +09001418
Jan Karacc334122009-01-12 17:23:05 +01001419 if (type != -1 && cnt != type)
1420 continue;
Niu Yawei1ea06be2014-06-04 12:20:30 +08001421 /*
1422 * The i_dquot should have been initialized in most cases,
1423 * we check it without locking here to avoid unnecessary
1424 * dqget()/dqput() calls.
1425 */
Konstantin Khlebnikov5bcd3b62015-02-12 12:36:37 +03001426 if (dquots[cnt])
Niu Yawei1ea06be2014-06-04 12:20:30 +08001427 continue;
Li Xi847aac62015-03-19 04:04:53 +09001428
1429 if (!sb_has_quota_active(sb, cnt))
1430 continue;
1431
Niu Yawei1ea06be2014-06-04 12:20:30 +08001432 init_needed = 1;
1433
Jan Karacc334122009-01-12 17:23:05 +01001434 switch (cnt) {
1435 case USRQUOTA:
Eric W. Biedermanaca645a2012-09-16 03:11:50 -07001436 qid = make_kqid_uid(inode->i_uid);
Jan Karacc334122009-01-12 17:23:05 +01001437 break;
1438 case GRPQUOTA:
Eric W. Biedermanaca645a2012-09-16 03:11:50 -07001439 qid = make_kqid_gid(inode->i_gid);
Jan Karacc334122009-01-12 17:23:05 +01001440 break;
Li Xi847aac62015-03-19 04:04:53 +09001441 case PRJQUOTA:
1442 rc = inode->i_sb->dq_op->get_projid(inode, &projid);
1443 if (rc)
1444 continue;
1445 qid = make_kqid_projid(projid);
1446 break;
Jan Karacc334122009-01-12 17:23:05 +01001447 }
Jan Kara6184fc02015-06-24 18:07:02 +02001448 dquot = dqget(sb, qid);
1449 if (IS_ERR(dquot)) {
1450 /* We raced with somebody turning quotas off... */
1451 if (PTR_ERR(dquot) != -ESRCH) {
1452 ret = PTR_ERR(dquot);
1453 goto out_put;
1454 }
1455 dquot = NULL;
1456 }
1457 got[cnt] = dquot;
Jan Karacc334122009-01-12 17:23:05 +01001458 }
1459
Niu Yawei1ea06be2014-06-04 12:20:30 +08001460 /* All required i_dquot has been initialized */
1461 if (!init_needed)
Jan Kara6184fc02015-06-24 18:07:02 +02001462 return 0;
Niu Yawei1ea06be2014-06-04 12:20:30 +08001463
Niu Yaweib9ba6f92014-06-04 12:23:19 +08001464 spin_lock(&dq_data_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465 if (IS_NOQUOTA(inode))
Jan Kara6184fc02015-06-24 18:07:02 +02001466 goto out_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1468 if (type != -1 && cnt != type)
1469 continue;
Jan Karacc334122009-01-12 17:23:05 +01001470 /* Avoid races with quotaoff() */
1471 if (!sb_has_quota_active(sb, cnt))
1472 continue;
Jan Kara4408ea42010-10-19 00:24:21 +02001473 /* We could race with quotaon or dqget() could have failed */
1474 if (!got[cnt])
1475 continue;
Konstantin Khlebnikov5bcd3b62015-02-12 12:36:37 +03001476 if (!dquots[cnt]) {
1477 dquots[cnt] = got[cnt];
Jan Karadd6f3c62009-01-26 16:01:43 +01001478 got[cnt] = NULL;
Jan Kara0a5a9c72010-02-09 18:20:39 +01001479 /*
1480 * Make quota reservation system happy if someone
1481 * did a write before quota was turned on
1482 */
1483 rsv = inode_get_rsv_space(inode);
Niu Yaweib9ba6f92014-06-04 12:23:19 +08001484 if (unlikely(rsv))
Konstantin Khlebnikov5bcd3b62015-02-12 12:36:37 +03001485 dquot_resv_space(dquots[cnt], rsv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486 }
1487 }
Jan Kara6184fc02015-06-24 18:07:02 +02001488out_lock:
Niu Yaweib9ba6f92014-06-04 12:23:19 +08001489 spin_unlock(&dq_data_lock);
Jan Kara6184fc02015-06-24 18:07:02 +02001490out_put:
Jan Karacc334122009-01-12 17:23:05 +01001491 /* Drop unused references */
Dmitry Monakhovdc52dd32009-12-14 15:21:15 +03001492 dqput_all(got);
Jan Kara6184fc02015-06-24 18:07:02 +02001493
1494 return ret;
Christoph Hellwig871a2932010-03-03 09:05:07 -05001495}
1496
Jan Kara6184fc02015-06-24 18:07:02 +02001497int dquot_initialize(struct inode *inode)
Christoph Hellwig871a2932010-03-03 09:05:07 -05001498{
Jan Kara6184fc02015-06-24 18:07:02 +02001499 return __dquot_initialize(inode, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500}
Mingming Cao08d03502009-01-14 16:19:32 +01001501EXPORT_SYMBOL(dquot_initialize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502
Tahsin Erdoganb8cb5a52017-05-24 18:24:07 -04001503bool dquot_initialize_needed(struct inode *inode)
1504{
1505 struct dquot **dquots;
1506 int i;
1507
1508 if (!dquot_active(inode))
1509 return false;
1510
1511 dquots = i_dquot(inode);
1512 for (i = 0; i < MAXQUOTAS; i++)
1513 if (!dquots[i] && sb_has_quota_active(inode->i_sb, i))
1514 return true;
1515 return false;
1516}
1517EXPORT_SYMBOL(dquot_initialize_needed);
1518
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519/*
Niu Yaweib9ba6f92014-06-04 12:23:19 +08001520 * Release all quotas referenced by inode.
1521 *
1522 * This function only be called on inode free or converting
1523 * a file to quota file, no other users for the i_dquot in
1524 * both cases, so we needn't call synchronize_srcu() after
1525 * clearing i_dquot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526 */
Christoph Hellwig9f754752010-03-03 09:05:05 -05001527static void __dquot_drop(struct inode *inode)
Jan Kara3d9ea252008-10-10 16:12:23 +02001528{
Jan Karacc334122009-01-12 17:23:05 +01001529 int cnt;
Konstantin Khlebnikov5bcd3b62015-02-12 12:36:37 +03001530 struct dquot **dquots = i_dquot(inode);
Jan Karacc334122009-01-12 17:23:05 +01001531 struct dquot *put[MAXQUOTAS];
1532
Niu Yaweib9ba6f92014-06-04 12:23:19 +08001533 spin_lock(&dq_data_lock);
Jan Karacc334122009-01-12 17:23:05 +01001534 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
Konstantin Khlebnikov5bcd3b62015-02-12 12:36:37 +03001535 put[cnt] = dquots[cnt];
1536 dquots[cnt] = NULL;
Jan Karacc334122009-01-12 17:23:05 +01001537 }
Niu Yaweib9ba6f92014-06-04 12:23:19 +08001538 spin_unlock(&dq_data_lock);
Dmitry Monakhovdc52dd32009-12-14 15:21:15 +03001539 dqput_all(put);
Christoph Hellwig9f754752010-03-03 09:05:05 -05001540}
1541
1542void dquot_drop(struct inode *inode)
1543{
Konstantin Khlebnikov5bcd3b62015-02-12 12:36:37 +03001544 struct dquot * const *dquots;
Christoph Hellwig9f754752010-03-03 09:05:05 -05001545 int cnt;
1546
1547 if (IS_NOQUOTA(inode))
1548 return;
1549
1550 /*
1551 * Test before calling to rule out calls from proc and such
1552 * where we are not allowed to block. Note that this is
1553 * actually reliable test even without the lock - the caller
1554 * must assure that nobody can come after the DQUOT_DROP and
1555 * add quota pointers back anyway.
1556 */
Konstantin Khlebnikov5bcd3b62015-02-12 12:36:37 +03001557 dquots = i_dquot(inode);
Christoph Hellwig9f754752010-03-03 09:05:05 -05001558 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
Konstantin Khlebnikov5bcd3b62015-02-12 12:36:37 +03001559 if (dquots[cnt])
Christoph Hellwig9f754752010-03-03 09:05:05 -05001560 break;
1561 }
1562
1563 if (cnt < MAXQUOTAS)
1564 __dquot_drop(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565}
Mingming Cao08d03502009-01-14 16:19:32 +01001566EXPORT_SYMBOL(dquot_drop);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567
1568/*
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001569 * inode_reserved_space is managed internally by quota, and protected by
1570 * i_lock similar to i_blocks+i_bytes.
1571 */
1572static qsize_t *inode_reserved_space(struct inode * inode)
1573{
1574 /* Filesystem must explicitly define it's own method in order to use
1575 * quota reservation interface */
1576 BUG_ON(!inode->i_sb->dq_op->get_reserved_space);
1577 return inode->i_sb->dq_op->get_reserved_space(inode);
1578}
1579
Dmitry Monakhovc4690702010-02-09 17:53:36 +01001580void inode_add_rsv_space(struct inode *inode, qsize_t number)
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001581{
1582 spin_lock(&inode->i_lock);
1583 *inode_reserved_space(inode) += number;
1584 spin_unlock(&inode->i_lock);
1585}
Dmitry Monakhovc4690702010-02-09 17:53:36 +01001586EXPORT_SYMBOL(inode_add_rsv_space);
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001587
Dmitry Monakhovc4690702010-02-09 17:53:36 +01001588void inode_claim_rsv_space(struct inode *inode, qsize_t number)
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001589{
1590 spin_lock(&inode->i_lock);
1591 *inode_reserved_space(inode) -= number;
1592 __inode_add_bytes(inode, number);
1593 spin_unlock(&inode->i_lock);
1594}
Dmitry Monakhovc4690702010-02-09 17:53:36 +01001595EXPORT_SYMBOL(inode_claim_rsv_space);
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001596
Jan Kara1c8924e2013-08-17 09:32:32 -04001597void inode_reclaim_rsv_space(struct inode *inode, qsize_t number)
1598{
1599 spin_lock(&inode->i_lock);
1600 *inode_reserved_space(inode) += number;
1601 __inode_sub_bytes(inode, number);
1602 spin_unlock(&inode->i_lock);
1603}
1604EXPORT_SYMBOL(inode_reclaim_rsv_space);
1605
Dmitry Monakhovc4690702010-02-09 17:53:36 +01001606void inode_sub_rsv_space(struct inode *inode, qsize_t number)
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001607{
1608 spin_lock(&inode->i_lock);
1609 *inode_reserved_space(inode) -= number;
1610 spin_unlock(&inode->i_lock);
1611}
Dmitry Monakhovc4690702010-02-09 17:53:36 +01001612EXPORT_SYMBOL(inode_sub_rsv_space);
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001613
1614static qsize_t inode_get_rsv_space(struct inode *inode)
1615{
1616 qsize_t ret;
Jan Kara05b5d892010-01-06 18:03:36 +01001617
1618 if (!inode->i_sb->dq_op->get_reserved_space)
1619 return 0;
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001620 spin_lock(&inode->i_lock);
1621 ret = *inode_reserved_space(inode);
1622 spin_unlock(&inode->i_lock);
1623 return ret;
1624}
1625
1626static void inode_incr_space(struct inode *inode, qsize_t number,
1627 int reserve)
1628{
1629 if (reserve)
1630 inode_add_rsv_space(inode, number);
1631 else
1632 inode_add_bytes(inode, number);
1633}
1634
1635static void inode_decr_space(struct inode *inode, qsize_t number, int reserve)
1636{
1637 if (reserve)
1638 inode_sub_rsv_space(inode, number);
1639 else
1640 inode_sub_bytes(inode, number);
1641}
1642
1643/*
Christoph Hellwig5dd40562010-03-03 09:05:00 -05001644 * This functions updates i_blocks+i_bytes fields and quota information
1645 * (together with appropriate checks).
1646 *
1647 * NOTE: We absolutely rely on the fact that caller dirties the inode
1648 * (usually helpers in quotaops.h care about this) and holds a handle for
1649 * the current transaction so that dquot write and inode write go into the
1650 * same transaction.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651 */
1652
1653/*
1654 * This operation can block, but only after everything is updated
1655 */
Eric Sandeen56246f92010-05-16 09:00:00 -04001656int __dquot_alloc_space(struct inode *inode, qsize_t number, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657{
Niu Yaweib9ba6f92014-06-04 12:23:19 +08001658 int cnt, ret = 0, index;
Jan Karabf097aa2012-02-14 13:28:01 +01001659 struct dquot_warn warn[MAXQUOTAS];
Eric Sandeen56246f92010-05-16 09:00:00 -04001660 int reserve = flags & DQUOT_SPACE_RESERVE;
Konstantin Khlebnikov5bcd3b62015-02-12 12:36:37 +03001661 struct dquot **dquots;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662
Christoph Hellwig189eef52010-06-04 10:56:29 +02001663 if (!dquot_active(inode)) {
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001664 inode_incr_space(inode, number, reserve);
1665 goto out;
1666 }
1667
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
Jan Karabf097aa2012-02-14 13:28:01 +01001669 warn[cnt].w_type = QUOTA_NL_NOWARN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670
Konstantin Khlebnikov5bcd3b62015-02-12 12:36:37 +03001671 dquots = i_dquot(inode);
Niu Yaweib9ba6f92014-06-04 12:23:19 +08001672 index = srcu_read_lock(&dquot_srcu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673 spin_lock(&dq_data_lock);
1674 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
Jan Karabf097aa2012-02-14 13:28:01 +01001675 if (!dquots[cnt])
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676 continue;
Jan Karabf097aa2012-02-14 13:28:01 +01001677 ret = check_bdq(dquots[cnt], number,
1678 !(flags & DQUOT_SPACE_WARN), &warn[cnt]);
1679 if (ret && !(flags & DQUOT_SPACE_NOFAIL)) {
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001680 spin_unlock(&dq_data_lock);
1681 goto out_flush_warn;
Mingming Caof18df222009-01-13 16:43:09 +01001682 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683 }
1684 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
Jan Karabf097aa2012-02-14 13:28:01 +01001685 if (!dquots[cnt])
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686 continue;
Mingming Caof18df222009-01-13 16:43:09 +01001687 if (reserve)
Jan Karabf097aa2012-02-14 13:28:01 +01001688 dquot_resv_space(dquots[cnt], number);
Mingming Caof18df222009-01-13 16:43:09 +01001689 else
Jan Karabf097aa2012-02-14 13:28:01 +01001690 dquot_incr_space(dquots[cnt], number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691 }
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001692 inode_incr_space(inode, number, reserve);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693 spin_unlock(&dq_data_lock);
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001694
1695 if (reserve)
1696 goto out_flush_warn;
Jan Karabf097aa2012-02-14 13:28:01 +01001697 mark_all_dquot_dirty(dquots);
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001698out_flush_warn:
Niu Yaweib9ba6f92014-06-04 12:23:19 +08001699 srcu_read_unlock(&dquot_srcu, index);
Jan Karabf097aa2012-02-14 13:28:01 +01001700 flush_warnings(warn);
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001701out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702 return ret;
1703}
Christoph Hellwig5dd40562010-03-03 09:05:00 -05001704EXPORT_SYMBOL(__dquot_alloc_space);
Mingming Caof18df222009-01-13 16:43:09 +01001705
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706/*
1707 * This operation can block, but only after everything is updated
1708 */
Jan Kara6bab3592014-10-20 16:01:33 +02001709int dquot_alloc_inode(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710{
Niu Yaweib9ba6f92014-06-04 12:23:19 +08001711 int cnt, ret = 0, index;
Jan Karabf097aa2012-02-14 13:28:01 +01001712 struct dquot_warn warn[MAXQUOTAS];
Konstantin Khlebnikov5bcd3b62015-02-12 12:36:37 +03001713 struct dquot * const *dquots;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714
Christoph Hellwig189eef52010-06-04 10:56:29 +02001715 if (!dquot_active(inode))
Christoph Hellwig63936dd2010-03-03 09:05:01 -05001716 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
Jan Karabf097aa2012-02-14 13:28:01 +01001718 warn[cnt].w_type = QUOTA_NL_NOWARN;
Niu Yaweib9ba6f92014-06-04 12:23:19 +08001719
Konstantin Khlebnikov5bcd3b62015-02-12 12:36:37 +03001720 dquots = i_dquot(inode);
Niu Yaweib9ba6f92014-06-04 12:23:19 +08001721 index = srcu_read_lock(&dquot_srcu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722 spin_lock(&dq_data_lock);
1723 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
Jan Karabf097aa2012-02-14 13:28:01 +01001724 if (!dquots[cnt])
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725 continue;
Jan Karabf097aa2012-02-14 13:28:01 +01001726 ret = check_idq(dquots[cnt], 1, &warn[cnt]);
Christoph Hellwigefd8f0e2010-03-03 09:05:08 -05001727 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728 goto warn_put_all;
1729 }
1730
1731 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
Jan Karabf097aa2012-02-14 13:28:01 +01001732 if (!dquots[cnt])
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733 continue;
Jan Karabf097aa2012-02-14 13:28:01 +01001734 dquot_incr_inodes(dquots[cnt], 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735 }
Christoph Hellwigefd8f0e2010-03-03 09:05:08 -05001736
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737warn_put_all:
1738 spin_unlock(&dq_data_lock);
Christoph Hellwig63936dd2010-03-03 09:05:01 -05001739 if (ret == 0)
Jan Karabf097aa2012-02-14 13:28:01 +01001740 mark_all_dquot_dirty(dquots);
Niu Yaweib9ba6f92014-06-04 12:23:19 +08001741 srcu_read_unlock(&dquot_srcu, index);
Jan Karabf097aa2012-02-14 13:28:01 +01001742 flush_warnings(warn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743 return ret;
1744}
Mingming Cao08d03502009-01-14 16:19:32 +01001745EXPORT_SYMBOL(dquot_alloc_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746
Christoph Hellwig5dd40562010-03-03 09:05:00 -05001747/*
1748 * Convert in-memory reserved quotas to real consumed quotas
1749 */
1750int dquot_claim_space_nodirty(struct inode *inode, qsize_t number)
Mingming Cao740d9dc2009-01-13 16:43:14 +01001751{
Konstantin Khlebnikov5bcd3b62015-02-12 12:36:37 +03001752 struct dquot **dquots;
Niu Yaweib9ba6f92014-06-04 12:23:19 +08001753 int cnt, index;
Mingming Cao740d9dc2009-01-13 16:43:14 +01001754
Christoph Hellwig189eef52010-06-04 10:56:29 +02001755 if (!dquot_active(inode)) {
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001756 inode_claim_rsv_space(inode, number);
Christoph Hellwig5dd40562010-03-03 09:05:00 -05001757 return 0;
Mingming Cao740d9dc2009-01-13 16:43:14 +01001758 }
1759
Konstantin Khlebnikov5bcd3b62015-02-12 12:36:37 +03001760 dquots = i_dquot(inode);
Niu Yaweib9ba6f92014-06-04 12:23:19 +08001761 index = srcu_read_lock(&dquot_srcu);
Mingming Cao740d9dc2009-01-13 16:43:14 +01001762 spin_lock(&dq_data_lock);
1763 /* Claim reserved quotas to allocated quotas */
1764 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
Konstantin Khlebnikov5bcd3b62015-02-12 12:36:37 +03001765 if (dquots[cnt])
1766 dquot_claim_reserved_space(dquots[cnt], number);
Mingming Cao740d9dc2009-01-13 16:43:14 +01001767 }
1768 /* Update inode bytes */
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001769 inode_claim_rsv_space(inode, number);
Mingming Cao740d9dc2009-01-13 16:43:14 +01001770 spin_unlock(&dq_data_lock);
Konstantin Khlebnikov5bcd3b62015-02-12 12:36:37 +03001771 mark_all_dquot_dirty(dquots);
Niu Yaweib9ba6f92014-06-04 12:23:19 +08001772 srcu_read_unlock(&dquot_srcu, index);
Christoph Hellwig5dd40562010-03-03 09:05:00 -05001773 return 0;
Mingming Cao740d9dc2009-01-13 16:43:14 +01001774}
Christoph Hellwig5dd40562010-03-03 09:05:00 -05001775EXPORT_SYMBOL(dquot_claim_space_nodirty);
Mingming Cao740d9dc2009-01-13 16:43:14 +01001776
1777/*
Jan Kara1c8924e2013-08-17 09:32:32 -04001778 * Convert allocated space back to in-memory reserved quotas
1779 */
1780void dquot_reclaim_space_nodirty(struct inode *inode, qsize_t number)
1781{
Konstantin Khlebnikov5bcd3b62015-02-12 12:36:37 +03001782 struct dquot **dquots;
Niu Yaweib9ba6f92014-06-04 12:23:19 +08001783 int cnt, index;
Jan Kara1c8924e2013-08-17 09:32:32 -04001784
1785 if (!dquot_active(inode)) {
1786 inode_reclaim_rsv_space(inode, number);
1787 return;
1788 }
1789
Konstantin Khlebnikov5bcd3b62015-02-12 12:36:37 +03001790 dquots = i_dquot(inode);
Niu Yaweib9ba6f92014-06-04 12:23:19 +08001791 index = srcu_read_lock(&dquot_srcu);
Jan Kara1c8924e2013-08-17 09:32:32 -04001792 spin_lock(&dq_data_lock);
1793 /* Claim reserved quotas to allocated quotas */
1794 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
Konstantin Khlebnikov5bcd3b62015-02-12 12:36:37 +03001795 if (dquots[cnt])
1796 dquot_reclaim_reserved_space(dquots[cnt], number);
Jan Kara1c8924e2013-08-17 09:32:32 -04001797 }
1798 /* Update inode bytes */
1799 inode_reclaim_rsv_space(inode, number);
1800 spin_unlock(&dq_data_lock);
Konstantin Khlebnikov5bcd3b62015-02-12 12:36:37 +03001801 mark_all_dquot_dirty(dquots);
Niu Yaweib9ba6f92014-06-04 12:23:19 +08001802 srcu_read_unlock(&dquot_srcu, index);
Jan Kara1c8924e2013-08-17 09:32:32 -04001803 return;
1804}
1805EXPORT_SYMBOL(dquot_reclaim_space_nodirty);
1806
1807/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808 * This operation can block, but only after everything is updated
1809 */
Eric Sandeen56246f92010-05-16 09:00:00 -04001810void __dquot_free_space(struct inode *inode, qsize_t number, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811{
1812 unsigned int cnt;
Jan Karabf097aa2012-02-14 13:28:01 +01001813 struct dquot_warn warn[MAXQUOTAS];
Konstantin Khlebnikov5bcd3b62015-02-12 12:36:37 +03001814 struct dquot **dquots;
Niu Yaweib9ba6f92014-06-04 12:23:19 +08001815 int reserve = flags & DQUOT_SPACE_RESERVE, index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816
Christoph Hellwig189eef52010-06-04 10:56:29 +02001817 if (!dquot_active(inode)) {
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001818 inode_decr_space(inode, number, reserve);
Christoph Hellwig5dd40562010-03-03 09:05:00 -05001819 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820 }
Jan Kara657d3bf2008-07-25 01:46:52 -07001821
Konstantin Khlebnikov5bcd3b62015-02-12 12:36:37 +03001822 dquots = i_dquot(inode);
Niu Yaweib9ba6f92014-06-04 12:23:19 +08001823 index = srcu_read_lock(&dquot_srcu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824 spin_lock(&dq_data_lock);
1825 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
Jan Karabf097aa2012-02-14 13:28:01 +01001826 int wtype;
1827
1828 warn[cnt].w_type = QUOTA_NL_NOWARN;
1829 if (!dquots[cnt])
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830 continue;
Jan Karabf097aa2012-02-14 13:28:01 +01001831 wtype = info_bdq_free(dquots[cnt], number);
1832 if (wtype != QUOTA_NL_NOWARN)
1833 prepare_warning(&warn[cnt], dquots[cnt], wtype);
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001834 if (reserve)
Jan Karabf097aa2012-02-14 13:28:01 +01001835 dquot_free_reserved_space(dquots[cnt], number);
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001836 else
Jan Karabf097aa2012-02-14 13:28:01 +01001837 dquot_decr_space(dquots[cnt], number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838 }
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001839 inode_decr_space(inode, number, reserve);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840 spin_unlock(&dq_data_lock);
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001841
1842 if (reserve)
1843 goto out_unlock;
Jan Karabf097aa2012-02-14 13:28:01 +01001844 mark_all_dquot_dirty(dquots);
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001845out_unlock:
Niu Yaweib9ba6f92014-06-04 12:23:19 +08001846 srcu_read_unlock(&dquot_srcu, index);
Jan Karabf097aa2012-02-14 13:28:01 +01001847 flush_warnings(warn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848}
Christoph Hellwig5dd40562010-03-03 09:05:00 -05001849EXPORT_SYMBOL(__dquot_free_space);
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001850
1851/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852 * This operation can block, but only after everything is updated
1853 */
Jan Kara6bab3592014-10-20 16:01:33 +02001854void dquot_free_inode(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855{
1856 unsigned int cnt;
Jan Karabf097aa2012-02-14 13:28:01 +01001857 struct dquot_warn warn[MAXQUOTAS];
Konstantin Khlebnikov5bcd3b62015-02-12 12:36:37 +03001858 struct dquot * const *dquots;
Niu Yaweib9ba6f92014-06-04 12:23:19 +08001859 int index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860
Christoph Hellwig189eef52010-06-04 10:56:29 +02001861 if (!dquot_active(inode))
Christoph Hellwig63936dd2010-03-03 09:05:01 -05001862 return;
Jan Kara657d3bf2008-07-25 01:46:52 -07001863
Konstantin Khlebnikov5bcd3b62015-02-12 12:36:37 +03001864 dquots = i_dquot(inode);
Niu Yaweib9ba6f92014-06-04 12:23:19 +08001865 index = srcu_read_lock(&dquot_srcu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866 spin_lock(&dq_data_lock);
1867 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
Jan Karabf097aa2012-02-14 13:28:01 +01001868 int wtype;
1869
1870 warn[cnt].w_type = QUOTA_NL_NOWARN;
1871 if (!dquots[cnt])
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872 continue;
Jan Karabf097aa2012-02-14 13:28:01 +01001873 wtype = info_idq_free(dquots[cnt], 1);
1874 if (wtype != QUOTA_NL_NOWARN)
1875 prepare_warning(&warn[cnt], dquots[cnt], wtype);
1876 dquot_decr_inodes(dquots[cnt], 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877 }
1878 spin_unlock(&dq_data_lock);
Jan Karabf097aa2012-02-14 13:28:01 +01001879 mark_all_dquot_dirty(dquots);
Niu Yaweib9ba6f92014-06-04 12:23:19 +08001880 srcu_read_unlock(&dquot_srcu, index);
Jan Karabf097aa2012-02-14 13:28:01 +01001881 flush_warnings(warn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882}
Mingming Cao08d03502009-01-14 16:19:32 +01001883EXPORT_SYMBOL(dquot_free_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884
1885/*
1886 * Transfer the number of inode and blocks from one diskquota to an other.
Jan Karabc8e5f02010-05-13 19:58:50 +02001887 * On success, dquot references in transfer_to are consumed and references
1888 * to original dquots that need to be released are placed there. On failure,
1889 * references are kept untouched.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890 *
1891 * This operation can block, but only after everything is updated
1892 * A transaction must be started when entering this function.
Jan Karabc8e5f02010-05-13 19:58:50 +02001893 *
Niu Yaweib9ba6f92014-06-04 12:23:19 +08001894 * We are holding reference on transfer_from & transfer_to, no need to
1895 * protect them by srcu_read_lock().
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896 */
Jan Karabc8e5f02010-05-13 19:58:50 +02001897int __dquot_transfer(struct inode *inode, struct dquot **transfer_to)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898{
Mingming Cao740d9dc2009-01-13 16:43:14 +01001899 qsize_t space, cur_space;
1900 qsize_t rsv_space = 0;
Tahsin Erdogan7a9ca532017-06-22 11:46:48 -04001901 qsize_t inode_usage = 1;
Jan Karabc8e5f02010-05-13 19:58:50 +02001902 struct dquot *transfer_from[MAXQUOTAS] = {};
Christoph Hellwigefd8f0e2010-03-03 09:05:08 -05001903 int cnt, ret = 0;
Dmitry9e327842010-10-09 23:15:30 +04001904 char is_valid[MAXQUOTAS] = {};
Jan Karabf097aa2012-02-14 13:28:01 +01001905 struct dquot_warn warn_to[MAXQUOTAS];
1906 struct dquot_warn warn_from_inodes[MAXQUOTAS];
1907 struct dquot_warn warn_from_space[MAXQUOTAS];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909 if (IS_NOQUOTA(inode))
Christoph Hellwigefd8f0e2010-03-03 09:05:08 -05001910 return 0;
Tahsin Erdogan7a9ca532017-06-22 11:46:48 -04001911
1912 if (inode->i_sb->dq_op->get_inode_usage) {
1913 ret = inode->i_sb->dq_op->get_inode_usage(inode, &inode_usage);
1914 if (ret)
1915 return ret;
1916 }
1917
Jan Karacc334122009-01-12 17:23:05 +01001918 /* Initialize the arrays */
Jan Karabf097aa2012-02-14 13:28:01 +01001919 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1920 warn_to[cnt].w_type = QUOTA_NL_NOWARN;
1921 warn_from_inodes[cnt].w_type = QUOTA_NL_NOWARN;
1922 warn_from_space[cnt].w_type = QUOTA_NL_NOWARN;
1923 }
Niu Yaweib9ba6f92014-06-04 12:23:19 +08001924
1925 spin_lock(&dq_data_lock);
Jan Karacc334122009-01-12 17:23:05 +01001926 if (IS_NOQUOTA(inode)) { /* File without quota accounting? */
Niu Yaweib9ba6f92014-06-04 12:23:19 +08001927 spin_unlock(&dq_data_lock);
Jan Karabc8e5f02010-05-13 19:58:50 +02001928 return 0;
Jan Karacc334122009-01-12 17:23:05 +01001929 }
Mingming Cao740d9dc2009-01-13 16:43:14 +01001930 cur_space = inode_get_bytes(inode);
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001931 rsv_space = inode_get_rsv_space(inode);
Mingming Cao740d9dc2009-01-13 16:43:14 +01001932 space = cur_space + rsv_space;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933 /* Build the transfer_from list and check the limits */
1934 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
Dmitry9e327842010-10-09 23:15:30 +04001935 /*
1936 * Skip changes for same uid or gid or for turned off quota-type.
1937 */
Jan Karadd6f3c62009-01-26 16:01:43 +01001938 if (!transfer_to[cnt])
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939 continue;
Dmitry9e327842010-10-09 23:15:30 +04001940 /* Avoid races with quotaoff() */
1941 if (!sb_has_quota_active(inode->i_sb, cnt))
1942 continue;
1943 is_valid[cnt] = 1;
Jan Kara2d0fa462014-09-25 16:36:14 +02001944 transfer_from[cnt] = i_dquot(inode)[cnt];
Tahsin Erdogan7a9ca532017-06-22 11:46:48 -04001945 ret = check_idq(transfer_to[cnt], inode_usage, &warn_to[cnt]);
Christoph Hellwigefd8f0e2010-03-03 09:05:08 -05001946 if (ret)
1947 goto over_quota;
Jan Karabf097aa2012-02-14 13:28:01 +01001948 ret = check_bdq(transfer_to[cnt], space, 0, &warn_to[cnt]);
Christoph Hellwigefd8f0e2010-03-03 09:05:08 -05001949 if (ret)
Jan Karacc334122009-01-12 17:23:05 +01001950 goto over_quota;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951 }
1952
1953 /*
1954 * Finally perform the needed transfer from transfer_from to transfer_to
1955 */
1956 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
Dmitry9e327842010-10-09 23:15:30 +04001957 if (!is_valid[cnt])
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959 /* Due to IO error we might not have transfer_from[] structure */
1960 if (transfer_from[cnt]) {
Jan Karabf097aa2012-02-14 13:28:01 +01001961 int wtype;
Tahsin Erdogan7a9ca532017-06-22 11:46:48 -04001962 wtype = info_idq_free(transfer_from[cnt], inode_usage);
Jan Karabf097aa2012-02-14 13:28:01 +01001963 if (wtype != QUOTA_NL_NOWARN)
1964 prepare_warning(&warn_from_inodes[cnt],
1965 transfer_from[cnt], wtype);
1966 wtype = info_bdq_free(transfer_from[cnt], space);
1967 if (wtype != QUOTA_NL_NOWARN)
1968 prepare_warning(&warn_from_space[cnt],
1969 transfer_from[cnt], wtype);
Tahsin Erdogan7a9ca532017-06-22 11:46:48 -04001970 dquot_decr_inodes(transfer_from[cnt], inode_usage);
Mingming Cao740d9dc2009-01-13 16:43:14 +01001971 dquot_decr_space(transfer_from[cnt], cur_space);
1972 dquot_free_reserved_space(transfer_from[cnt],
1973 rsv_space);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974 }
1975
Tahsin Erdogan7a9ca532017-06-22 11:46:48 -04001976 dquot_incr_inodes(transfer_to[cnt], inode_usage);
Mingming Cao740d9dc2009-01-13 16:43:14 +01001977 dquot_incr_space(transfer_to[cnt], cur_space);
1978 dquot_resv_space(transfer_to[cnt], rsv_space);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979
Jan Kara2d0fa462014-09-25 16:36:14 +02001980 i_dquot(inode)[cnt] = transfer_to[cnt];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982 spin_unlock(&dq_data_lock);
Jan Karacc334122009-01-12 17:23:05 +01001983
Dmitry Monakhovdc52dd32009-12-14 15:21:15 +03001984 mark_all_dquot_dirty(transfer_from);
1985 mark_all_dquot_dirty(transfer_to);
Jan Karabf097aa2012-02-14 13:28:01 +01001986 flush_warnings(warn_to);
1987 flush_warnings(warn_from_inodes);
1988 flush_warnings(warn_from_space);
Jan Karabc8e5f02010-05-13 19:58:50 +02001989 /* Pass back references to put */
Dmitry Monakhovdc52dd32009-12-14 15:21:15 +03001990 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
Dmitry9e327842010-10-09 23:15:30 +04001991 if (is_valid[cnt])
1992 transfer_to[cnt] = transfer_from[cnt];
Jan Kara86f3cbe2010-10-11 15:22:21 +02001993 return 0;
Jan Karacc334122009-01-12 17:23:05 +01001994over_quota:
1995 spin_unlock(&dq_data_lock);
Jan Karabf097aa2012-02-14 13:28:01 +01001996 flush_warnings(warn_to);
Jan Kara86f3cbe2010-10-11 15:22:21 +02001997 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998}
Jan Karabc8e5f02010-05-13 19:58:50 +02001999EXPORT_SYMBOL(__dquot_transfer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000
Dmitry Monakhov8ddd69d2010-02-16 08:31:50 +03002001/* Wrapper for transferring ownership of an inode for uid/gid only
2002 * Called from FSXXX_setattr()
2003 */
Christoph Hellwigb43fa822010-03-03 09:05:03 -05002004int dquot_transfer(struct inode *inode, struct iattr *iattr)
Jan Karab85f4b82008-07-25 01:46:50 -07002005{
Jan Karabc8e5f02010-05-13 19:58:50 +02002006 struct dquot *transfer_to[MAXQUOTAS] = {};
Jan Kara6184fc02015-06-24 18:07:02 +02002007 struct dquot *dquot;
Jan Karabc8e5f02010-05-13 19:58:50 +02002008 struct super_block *sb = inode->i_sb;
2009 int ret;
Dmitry Monakhov8ddd69d2010-02-16 08:31:50 +03002010
Christoph Hellwig189eef52010-06-04 10:56:29 +02002011 if (!dquot_active(inode))
Jan Karabc8e5f02010-05-13 19:58:50 +02002012 return 0;
Dmitry Monakhov12755622010-04-08 22:04:20 +04002013
Jan Kara6184fc02015-06-24 18:07:02 +02002014 if (iattr->ia_valid & ATTR_UID && !uid_eq(iattr->ia_uid, inode->i_uid)){
2015 dquot = dqget(sb, make_kqid_uid(iattr->ia_uid));
2016 if (IS_ERR(dquot)) {
2017 if (PTR_ERR(dquot) != -ESRCH) {
2018 ret = PTR_ERR(dquot);
2019 goto out_put;
2020 }
2021 dquot = NULL;
2022 }
2023 transfer_to[USRQUOTA] = dquot;
2024 }
2025 if (iattr->ia_valid & ATTR_GID && !gid_eq(iattr->ia_gid, inode->i_gid)){
2026 dquot = dqget(sb, make_kqid_gid(iattr->ia_gid));
2027 if (IS_ERR(dquot)) {
2028 if (PTR_ERR(dquot) != -ESRCH) {
2029 ret = PTR_ERR(dquot);
2030 goto out_put;
2031 }
2032 dquot = NULL;
2033 }
2034 transfer_to[GRPQUOTA] = dquot;
2035 }
Jan Karabc8e5f02010-05-13 19:58:50 +02002036 ret = __dquot_transfer(inode, transfer_to);
Jan Kara6184fc02015-06-24 18:07:02 +02002037out_put:
Jan Karabc8e5f02010-05-13 19:58:50 +02002038 dqput_all(transfer_to);
2039 return ret;
Jan Karab85f4b82008-07-25 01:46:50 -07002040}
Christoph Hellwigb43fa822010-03-03 09:05:03 -05002041EXPORT_SYMBOL(dquot_transfer);
Jan Karab85f4b82008-07-25 01:46:50 -07002042
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043/*
2044 * Write info of quota file to disk
2045 */
2046int dquot_commit_info(struct super_block *sb, int type)
2047{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048 struct quota_info *dqopt = sb_dqopt(sb);
2049
Jan Kara9a8ae302017-06-09 08:45:43 +02002050 return dqopt->ops[type]->write_file_info(sb, type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051}
Mingming Cao08d03502009-01-14 16:19:32 +01002052EXPORT_SYMBOL(dquot_commit_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053
Jan Karabe6257b2016-01-25 19:24:50 +01002054int dquot_get_next_id(struct super_block *sb, struct kqid *qid)
2055{
2056 struct quota_info *dqopt = sb_dqopt(sb);
Jan Karabe6257b2016-01-25 19:24:50 +01002057
Jan Kara9d1ccbe2016-11-23 13:35:14 +01002058 if (!sb_has_quota_active(sb, qid->type))
2059 return -ESRCH;
2060 if (!dqopt->ops[qid->type]->get_next_id)
2061 return -ENOSYS;
Jan Karaf14618c2017-06-09 08:36:16 +02002062 return dqopt->ops[qid->type]->get_next_id(sb, qid);
Jan Karabe6257b2016-01-25 19:24:50 +01002063}
2064EXPORT_SYMBOL(dquot_get_next_id);
2065
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066/*
2067 * Definitions of diskquota operations.
2068 */
Alexey Dobriyan61e225d2009-09-21 17:01:08 -07002069const struct dquot_operations dquot_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070 .write_dquot = dquot_commit,
2071 .acquire_dquot = dquot_acquire,
2072 .release_dquot = dquot_release,
2073 .mark_dirty = dquot_mark_dquot_dirty,
Jan Kara74f783a2008-08-19 14:51:22 +02002074 .write_info = dquot_commit_info,
2075 .alloc_dquot = dquot_alloc,
2076 .destroy_dquot = dquot_destroy,
Jan Karabe6257b2016-01-25 19:24:50 +01002077 .get_next_id = dquot_get_next_id,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078};
Christoph Hellwig123e9ca2010-05-19 07:16:44 -04002079EXPORT_SYMBOL(dquot_operations);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081/*
Christoph Hellwig907f4552010-03-03 09:05:06 -05002082 * Generic helper for ->open on filesystems supporting disk quotas.
2083 */
2084int dquot_file_open(struct inode *inode, struct file *file)
2085{
2086 int error;
2087
2088 error = generic_file_open(inode, file);
2089 if (!error && (file->f_mode & FMODE_WRITE))
Christoph Hellwig871a2932010-03-03 09:05:07 -05002090 dquot_initialize(inode);
Christoph Hellwig907f4552010-03-03 09:05:06 -05002091 return error;
2092}
2093EXPORT_SYMBOL(dquot_file_open);
2094
2095/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096 * Turn quota off on a device. type == -1 ==> quotaoff for all types (umount)
2097 */
Christoph Hellwig0f0dd622010-05-19 07:16:41 -04002098int dquot_disable(struct super_block *sb, int type, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099{
Jan Kara0ff5af82008-04-28 02:14:33 -07002100 int cnt, ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101 struct quota_info *dqopt = sb_dqopt(sb);
2102 struct inode *toputinode[MAXQUOTAS];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103
Jan Kara7d6cd732016-11-23 13:16:10 +01002104 /* s_umount should be held in exclusive mode */
2105 if (WARN_ON_ONCE(down_read_trylock(&sb->s_umount)))
2106 up_read(&sb->s_umount);
2107
Jan Karaf55abc02008-08-20 17:50:32 +02002108 /* Cannot turn off usage accounting without turning off limits, or
2109 * suspend quotas and simultaneously turn quotas off. */
2110 if ((flags & DQUOT_USAGE_ENABLED && !(flags & DQUOT_LIMITS_ENABLED))
2111 || (flags & DQUOT_SUSPENDED && flags & (DQUOT_LIMITS_ENABLED |
2112 DQUOT_USAGE_ENABLED)))
2113 return -EINVAL;
2114
Jan Kara9377abd2008-05-12 14:02:08 -07002115 /*
2116 * Skip everything if there's nothing to do. We have to do this because
2117 * sometimes we are called when fill_super() failed and calling
2118 * sync_fs() in such cases does no good.
2119 */
Jan Karac3b00442016-11-23 14:04:55 +01002120 if (!sb_any_quota_loaded(sb))
Jan Kara9377abd2008-05-12 14:02:08 -07002121 return 0;
Jan Karac3b00442016-11-23 14:04:55 +01002122
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
2124 toputinode[cnt] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125 if (type != -1 && cnt != type)
2126 continue;
Jan Karaf55abc02008-08-20 17:50:32 +02002127 if (!sb_has_quota_loaded(sb, cnt))
Jan Kara0ff5af82008-04-28 02:14:33 -07002128 continue;
Jan Karaf55abc02008-08-20 17:50:32 +02002129
2130 if (flags & DQUOT_SUSPENDED) {
Jan Karacc334122009-01-12 17:23:05 +01002131 spin_lock(&dq_state_lock);
Jan Karaf55abc02008-08-20 17:50:32 +02002132 dqopt->flags |=
2133 dquot_state_flag(DQUOT_SUSPENDED, cnt);
Jan Karacc334122009-01-12 17:23:05 +01002134 spin_unlock(&dq_state_lock);
Jan Karaf55abc02008-08-20 17:50:32 +02002135 } else {
Jan Karacc334122009-01-12 17:23:05 +01002136 spin_lock(&dq_state_lock);
Jan Karaf55abc02008-08-20 17:50:32 +02002137 dqopt->flags &= ~dquot_state_flag(flags, cnt);
2138 /* Turning off suspended quotas? */
2139 if (!sb_has_quota_loaded(sb, cnt) &&
2140 sb_has_quota_suspended(sb, cnt)) {
2141 dqopt->flags &= ~dquot_state_flag(
2142 DQUOT_SUSPENDED, cnt);
Jan Karacc334122009-01-12 17:23:05 +01002143 spin_unlock(&dq_state_lock);
Jan Karaf55abc02008-08-20 17:50:32 +02002144 iput(dqopt->files[cnt]);
2145 dqopt->files[cnt] = NULL;
2146 continue;
2147 }
Jan Karacc334122009-01-12 17:23:05 +01002148 spin_unlock(&dq_state_lock);
Jan Kara0ff5af82008-04-28 02:14:33 -07002149 }
Jan Karaf55abc02008-08-20 17:50:32 +02002150
2151 /* We still have to keep quota loaded? */
2152 if (sb_has_quota_loaded(sb, cnt) && !(flags & DQUOT_SUSPENDED))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154
2155 /* Note: these are blocking operations */
2156 drop_dquot_ref(sb, cnt);
2157 invalidate_dquots(sb, cnt);
2158 /*
Jan Kara268157b2009-01-27 15:47:22 +01002159 * Now all dquots should be invalidated, all writes done so we
2160 * should be only users of the info. No locks needed.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161 */
2162 if (info_dirty(&dqopt->info[cnt]))
2163 sb->dq_op->write_info(sb, cnt);
2164 if (dqopt->ops[cnt]->free_file_info)
2165 dqopt->ops[cnt]->free_file_info(sb, cnt);
2166 put_quota_format(dqopt->info[cnt].dqi_format);
2167
2168 toputinode[cnt] = dqopt->files[cnt];
Jan Karaf55abc02008-08-20 17:50:32 +02002169 if (!sb_has_quota_loaded(sb, cnt))
Jan Kara0ff5af82008-04-28 02:14:33 -07002170 dqopt->files[cnt] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171 dqopt->info[cnt].dqi_flags = 0;
2172 dqopt->info[cnt].dqi_igrace = 0;
2173 dqopt->info[cnt].dqi_bgrace = 0;
2174 dqopt->ops[cnt] = NULL;
2175 }
Jan Karaca785ec2008-09-30 17:53:37 +02002176
2177 /* Skip syncing and setting flags if quota files are hidden */
2178 if (dqopt->flags & DQUOT_QUOTA_SYS_FILE)
2179 goto put_inodes;
2180
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181 /* Sync the superblock so that buffers with quota data are written to
Al Viro7b7b1ac2005-11-07 17:13:39 -05002182 * disk (and so userspace sees correct data afterwards). */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183 if (sb->s_op->sync_fs)
2184 sb->s_op->sync_fs(sb, 1);
2185 sync_blockdev(sb->s_bdev);
2186 /* Now the quota files are just ordinary files and we can set the
2187 * inode flags back. Moreover we discard the pagecache so that
2188 * userspace sees the writes we did bypassing the pagecache. We
2189 * must also discard the blockdev buffers so that we see the
2190 * changes done by userspace on the next quotaon() */
2191 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
Jan Kara2700e602016-12-19 14:01:39 +01002192 /* This can happen when suspending quotas on remount-ro... */
2193 if (toputinode[cnt] && !sb_has_quota_loaded(sb, cnt)) {
Jan Karac3b00442016-11-23 14:04:55 +01002194 inode_lock(toputinode[cnt]);
Jan Karaaad6cde2017-04-11 16:29:12 +02002195 toputinode[cnt]->i_flags &= ~S_NOQUOTA;
Jan Karac3b00442016-11-23 14:04:55 +01002196 truncate_inode_pages(&toputinode[cnt]->i_data, 0);
2197 inode_unlock(toputinode[cnt]);
2198 mark_inode_dirty_sync(toputinode[cnt]);
Jan Karaca785ec2008-09-30 17:53:37 +02002199 }
2200 if (sb->s_bdev)
2201 invalidate_bdev(sb->s_bdev);
2202put_inodes:
2203 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
2204 if (toputinode[cnt]) {
Jan Kara0ff5af82008-04-28 02:14:33 -07002205 /* On remount RO, we keep the inode pointer so that we
Jan Karaf55abc02008-08-20 17:50:32 +02002206 * can reenable quota on the subsequent remount RW. We
2207 * have to check 'flags' variable and not use sb_has_
2208 * function because another quotaon / quotaoff could
2209 * change global state before we got here. We refuse
2210 * to suspend quotas when there is pending delete on
2211 * the quota file... */
2212 if (!(flags & DQUOT_SUSPENDED))
Jan Kara0ff5af82008-04-28 02:14:33 -07002213 iput(toputinode[cnt]);
2214 else if (!toputinode[cnt]->i_nlink)
2215 ret = -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216 }
Jan Kara0ff5af82008-04-28 02:14:33 -07002217 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218}
Christoph Hellwig0f0dd622010-05-19 07:16:41 -04002219EXPORT_SYMBOL(dquot_disable);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220
Christoph Hellwig287a8092010-05-19 07:16:45 -04002221int dquot_quota_off(struct super_block *sb, int type)
Jan Karaf55abc02008-08-20 17:50:32 +02002222{
Christoph Hellwig0f0dd622010-05-19 07:16:41 -04002223 return dquot_disable(sb, type,
2224 DQUOT_USAGE_ENABLED | DQUOT_LIMITS_ENABLED);
Jan Karaf55abc02008-08-20 17:50:32 +02002225}
Christoph Hellwig287a8092010-05-19 07:16:45 -04002226EXPORT_SYMBOL(dquot_quota_off);
Christoph Hellwig0f0dd622010-05-19 07:16:41 -04002227
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228/*
2229 * Turn quotas on on a device
2230 */
2231
Jan Karaf55abc02008-08-20 17:50:32 +02002232/*
2233 * Helper function to turn quotas on when we already have the inode of
2234 * quota file and no quota information is loaded.
2235 */
2236static int vfs_load_quota_inode(struct inode *inode, int type, int format_id,
2237 unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238{
2239 struct quota_format_type *fmt = find_quota_format(format_id);
2240 struct super_block *sb = inode->i_sb;
2241 struct quota_info *dqopt = sb_dqopt(sb);
2242 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243
2244 if (!fmt)
2245 return -ESRCH;
2246 if (!S_ISREG(inode->i_mode)) {
2247 error = -EACCES;
2248 goto out_fmt;
2249 }
2250 if (IS_RDONLY(inode)) {
2251 error = -EROFS;
2252 goto out_fmt;
2253 }
Li Xi847aac62015-03-19 04:04:53 +09002254 if (!sb->s_op->quota_write || !sb->s_op->quota_read ||
2255 (type == PRJQUOTA && sb->dq_op->get_projid == NULL)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256 error = -EINVAL;
2257 goto out_fmt;
2258 }
Eric W. Biederman5c004822016-07-05 10:41:57 -05002259 /* Filesystems outside of init_user_ns not yet supported */
2260 if (sb->s_user_ns != &init_user_ns) {
2261 error = -EINVAL;
2262 goto out_fmt;
2263 }
Jan Karaf55abc02008-08-20 17:50:32 +02002264 /* Usage always has to be set... */
2265 if (!(flags & DQUOT_USAGE_ENABLED)) {
2266 error = -EINVAL;
2267 goto out_fmt;
2268 }
Jan Karac3b00442016-11-23 14:04:55 +01002269 if (sb_has_quota_loaded(sb, type)) {
2270 error = -EBUSY;
2271 goto out_fmt;
2272 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273
Jan Karaca785ec2008-09-30 17:53:37 +02002274 if (!(dqopt->flags & DQUOT_QUOTA_SYS_FILE)) {
Jan Karaab94c392010-02-22 21:07:17 +01002275 /* As we bypass the pagecache we must now flush all the
2276 * dirty data and invalidate caches so that kernel sees
2277 * changes from userspace. It is not enough to just flush
2278 * the quota file since if blocksize < pagesize, invalidation
2279 * of the cache could fail because of other unrelated dirty
2280 * data */
2281 sync_filesystem(sb);
Jan Karaca785ec2008-09-30 17:53:37 +02002282 invalidate_bdev(sb->s_bdev);
2283 }
Jan Karaca785ec2008-09-30 17:53:37 +02002284
2285 if (!(dqopt->flags & DQUOT_QUOTA_SYS_FILE)) {
2286 /* We don't want quota and atime on quota files (deadlocks
2287 * possible) Also nobody should write to the file - we use
2288 * special IO operations which ignore the immutable bit. */
Al Viro59551022016-01-22 15:40:57 -05002289 inode_lock(inode);
Jan Karaaad6cde2017-04-11 16:29:12 +02002290 inode->i_flags |= S_NOQUOTA;
Al Viro59551022016-01-22 15:40:57 -05002291 inode_unlock(inode);
Jan Kara26245c92010-01-06 17:20:35 +01002292 /*
2293 * When S_NOQUOTA is set, remove dquot references as no more
2294 * references can be added
2295 */
Christoph Hellwig9f754752010-03-03 09:05:05 -05002296 __dquot_drop(inode);
Jan Karaca785ec2008-09-30 17:53:37 +02002297 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298
2299 error = -EIO;
2300 dqopt->files[type] = igrab(inode);
2301 if (!dqopt->files[type])
Jan Karac3b00442016-11-23 14:04:55 +01002302 goto out_file_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303 error = -EINVAL;
2304 if (!fmt->qf_ops->check_quota_file(sb, type))
2305 goto out_file_init;
2306
2307 dqopt->ops[type] = fmt->qf_ops;
2308 dqopt->info[type].dqi_format = fmt;
Jan Kara0ff5af82008-04-28 02:14:33 -07002309 dqopt->info[type].dqi_fmt_id = format_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310 INIT_LIST_HEAD(&dqopt->info[type].dqi_dirty_list);
Jan Kara268157b2009-01-27 15:47:22 +01002311 error = dqopt->ops[type]->read_file_info(sb, type);
Jan Kara42fdb852017-06-09 08:59:46 +02002312 if (error < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313 goto out_file_init;
Jan Kara15512372017-06-09 11:56:06 +02002314 if (dqopt->flags & DQUOT_QUOTA_SYS_FILE) {
2315 spin_lock(&dq_data_lock);
Jan Kara46fe44c2011-11-16 15:03:59 +01002316 dqopt->info[type].dqi_flags |= DQF_SYS_FILE;
Jan Kara15512372017-06-09 11:56:06 +02002317 spin_unlock(&dq_data_lock);
2318 }
Jan Karacc334122009-01-12 17:23:05 +01002319 spin_lock(&dq_state_lock);
Jan Karaf55abc02008-08-20 17:50:32 +02002320 dqopt->flags |= dquot_state_flag(flags, type);
Jan Karacc334122009-01-12 17:23:05 +01002321 spin_unlock(&dq_state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322
2323 add_dquot_ref(sb, type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324
2325 return 0;
2326
2327out_file_init:
2328 dqopt->files[type] = NULL;
2329 iput(inode);
Jan Karac3b00442016-11-23 14:04:55 +01002330out_file_flags:
Jan Karaaad6cde2017-04-11 16:29:12 +02002331 inode_lock(inode);
2332 inode->i_flags &= ~S_NOQUOTA;
2333 inode_unlock(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334out_fmt:
2335 put_quota_format(fmt);
2336
2337 return error;
2338}
2339
Jan Kara0ff5af82008-04-28 02:14:33 -07002340/* Reenable quotas on remount RW */
Christoph Hellwig0f0dd622010-05-19 07:16:41 -04002341int dquot_resume(struct super_block *sb, int type)
Jan Kara0ff5af82008-04-28 02:14:33 -07002342{
2343 struct quota_info *dqopt = sb_dqopt(sb);
2344 struct inode *inode;
Christoph Hellwig0f0dd622010-05-19 07:16:41 -04002345 int ret = 0, cnt;
Jan Karaf55abc02008-08-20 17:50:32 +02002346 unsigned int flags;
Jan Kara0ff5af82008-04-28 02:14:33 -07002347
Jan Kara7d6cd732016-11-23 13:16:10 +01002348 /* s_umount should be held in exclusive mode */
2349 if (WARN_ON_ONCE(down_read_trylock(&sb->s_umount)))
2350 up_read(&sb->s_umount);
2351
Christoph Hellwig0f0dd622010-05-19 07:16:41 -04002352 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
2353 if (type != -1 && cnt != type)
2354 continue;
Jan Karac3b00442016-11-23 14:04:55 +01002355 if (!sb_has_quota_suspended(sb, cnt))
Christoph Hellwig0f0dd622010-05-19 07:16:41 -04002356 continue;
Jan Karac3b00442016-11-23 14:04:55 +01002357
Christoph Hellwig0f0dd622010-05-19 07:16:41 -04002358 inode = dqopt->files[cnt];
2359 dqopt->files[cnt] = NULL;
2360 spin_lock(&dq_state_lock);
2361 flags = dqopt->flags & dquot_state_flag(DQUOT_USAGE_ENABLED |
2362 DQUOT_LIMITS_ENABLED,
2363 cnt);
2364 dqopt->flags &= ~dquot_state_flag(DQUOT_STATE_FLAGS, cnt);
2365 spin_unlock(&dq_state_lock);
Christoph Hellwig0f0dd622010-05-19 07:16:41 -04002366
2367 flags = dquot_generic_flag(flags, cnt);
2368 ret = vfs_load_quota_inode(inode, cnt,
2369 dqopt->info[cnt].dqi_fmt_id, flags);
2370 iput(inode);
2371 }
Jan Kara0ff5af82008-04-28 02:14:33 -07002372
2373 return ret;
2374}
Christoph Hellwig0f0dd622010-05-19 07:16:41 -04002375EXPORT_SYMBOL(dquot_resume);
Jan Kara0ff5af82008-04-28 02:14:33 -07002376
Jan Karaf00c9e42010-09-15 17:38:58 +02002377int dquot_quota_on(struct super_block *sb, int type, int format_id,
Al Viro8c54ca92016-11-20 19:49:34 -05002378 const struct path *path)
Al Viro77e69da2008-08-01 04:29:18 -04002379{
2380 int error = security_quota_on(path->dentry);
2381 if (error)
2382 return error;
2383 /* Quota file not on the same filesystem? */
Al Virod8c95842011-12-07 18:16:57 -05002384 if (path->dentry->d_sb != sb)
Al Viro77e69da2008-08-01 04:29:18 -04002385 error = -EXDEV;
2386 else
David Howellsdea655c2015-03-17 22:26:15 +00002387 error = vfs_load_quota_inode(d_inode(path->dentry), type,
Jan Karaf55abc02008-08-20 17:50:32 +02002388 format_id, DQUOT_USAGE_ENABLED |
2389 DQUOT_LIMITS_ENABLED);
Al Viro77e69da2008-08-01 04:29:18 -04002390 return error;
2391}
Christoph Hellwig287a8092010-05-19 07:16:45 -04002392EXPORT_SYMBOL(dquot_quota_on);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002393
2394/*
Jan Karaf55abc02008-08-20 17:50:32 +02002395 * More powerful function for turning on quotas allowing setting
2396 * of individual quota flags
2397 */
Christoph Hellwig287a8092010-05-19 07:16:45 -04002398int dquot_enable(struct inode *inode, int type, int format_id,
2399 unsigned int flags)
Jan Karaf55abc02008-08-20 17:50:32 +02002400{
Jan Karaf55abc02008-08-20 17:50:32 +02002401 struct super_block *sb = inode->i_sb;
Jan Karaf55abc02008-08-20 17:50:32 +02002402
2403 /* Just unsuspend quotas? */
Christoph Hellwig0f0dd622010-05-19 07:16:41 -04002404 BUG_ON(flags & DQUOT_SUSPENDED);
Jan Kara7d6cd732016-11-23 13:16:10 +01002405 /* s_umount should be held in exclusive mode */
2406 if (WARN_ON_ONCE(down_read_trylock(&sb->s_umount)))
2407 up_read(&sb->s_umount);
Christoph Hellwig0f0dd622010-05-19 07:16:41 -04002408
Jan Karaf55abc02008-08-20 17:50:32 +02002409 if (!flags)
2410 return 0;
2411 /* Just updating flags needed? */
2412 if (sb_has_quota_loaded(sb, type)) {
Jan Karaf55abc02008-08-20 17:50:32 +02002413 if (flags & DQUOT_USAGE_ENABLED &&
Jan Karac3b00442016-11-23 14:04:55 +01002414 sb_has_quota_usage_enabled(sb, type))
2415 return -EBUSY;
Jan Karaf55abc02008-08-20 17:50:32 +02002416 if (flags & DQUOT_LIMITS_ENABLED &&
Jan Karac3b00442016-11-23 14:04:55 +01002417 sb_has_quota_limits_enabled(sb, type))
2418 return -EBUSY;
Jan Karacc334122009-01-12 17:23:05 +01002419 spin_lock(&dq_state_lock);
Jan Karaf55abc02008-08-20 17:50:32 +02002420 sb_dqopt(sb)->flags |= dquot_state_flag(flags, type);
Jan Karacc334122009-01-12 17:23:05 +01002421 spin_unlock(&dq_state_lock);
Jan Karac3b00442016-11-23 14:04:55 +01002422 return 0;
Jan Karaf55abc02008-08-20 17:50:32 +02002423 }
2424
Jan Karaf55abc02008-08-20 17:50:32 +02002425 return vfs_load_quota_inode(inode, type, format_id, flags);
2426}
Christoph Hellwig287a8092010-05-19 07:16:45 -04002427EXPORT_SYMBOL(dquot_enable);
Jan Karaf55abc02008-08-20 17:50:32 +02002428
2429/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002430 * This function is used when filesystem needs to initialize quotas
2431 * during mount time.
2432 */
Christoph Hellwig287a8092010-05-19 07:16:45 -04002433int dquot_quota_on_mount(struct super_block *sb, char *qf_name,
Christoph Hellwig84de8562005-06-23 00:09:16 -07002434 int format_id, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002435{
Christoph Hellwig84de8562005-06-23 00:09:16 -07002436 struct dentry *dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002437 int error;
2438
Al Viroe12a4e82016-03-07 23:27:22 -05002439 dentry = lookup_one_len_unlocked(qf_name, sb->s_root, strlen(qf_name));
Christoph Hellwig84de8562005-06-23 00:09:16 -07002440 if (IS_ERR(dentry))
2441 return PTR_ERR(dentry);
2442
David Howellsdea655c2015-03-17 22:26:15 +00002443 if (d_really_is_negative(dentry)) {
Jan Kara154f4842005-11-28 13:44:14 -08002444 error = -ENOENT;
2445 goto out;
2446 }
2447
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448 error = security_quota_on(dentry);
Christoph Hellwig84de8562005-06-23 00:09:16 -07002449 if (!error)
David Howellsdea655c2015-03-17 22:26:15 +00002450 error = vfs_load_quota_inode(d_inode(dentry), type, format_id,
Jan Karaf55abc02008-08-20 17:50:32 +02002451 DQUOT_USAGE_ENABLED | DQUOT_LIMITS_ENABLED);
Christoph Hellwig84de8562005-06-23 00:09:16 -07002452
Jan Kara154f4842005-11-28 13:44:14 -08002453out:
Christoph Hellwig84de8562005-06-23 00:09:16 -07002454 dput(dentry);
2455 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002456}
Christoph Hellwig287a8092010-05-19 07:16:45 -04002457EXPORT_SYMBOL(dquot_quota_on_mount);
Jan Karab85f4b82008-07-25 01:46:50 -07002458
Jan Kara3e2af672014-10-06 18:40:51 +02002459static int dquot_quota_enable(struct super_block *sb, unsigned int flags)
2460{
2461 int ret;
2462 int type;
2463 struct quota_info *dqopt = sb_dqopt(sb);
2464
2465 if (!(dqopt->flags & DQUOT_QUOTA_SYS_FILE))
2466 return -ENOSYS;
2467 /* Accounting cannot be turned on while fs is mounted */
2468 flags &= ~(FS_QUOTA_UDQ_ACCT | FS_QUOTA_GDQ_ACCT | FS_QUOTA_PDQ_ACCT);
2469 if (!flags)
2470 return -EINVAL;
2471 for (type = 0; type < MAXQUOTAS; type++) {
2472 if (!(flags & qtype_enforce_flag(type)))
2473 continue;
2474 /* Can't enforce without accounting */
2475 if (!sb_has_quota_usage_enabled(sb, type))
2476 return -EINVAL;
2477 ret = dquot_enable(dqopt->files[type], type,
2478 dqopt->info[type].dqi_fmt_id,
2479 DQUOT_LIMITS_ENABLED);
2480 if (ret < 0)
2481 goto out_err;
2482 }
2483 return 0;
2484out_err:
2485 /* Backout enforcement enablement we already did */
2486 for (type--; type >= 0; type--) {
2487 if (flags & qtype_enforce_flag(type))
2488 dquot_disable(sb, type, DQUOT_LIMITS_ENABLED);
2489 }
2490 /* Error code translation for better compatibility with XFS */
2491 if (ret == -EBUSY)
2492 ret = -EEXIST;
2493 return ret;
2494}
2495
2496static int dquot_quota_disable(struct super_block *sb, unsigned int flags)
2497{
2498 int ret;
2499 int type;
2500 struct quota_info *dqopt = sb_dqopt(sb);
2501
2502 if (!(dqopt->flags & DQUOT_QUOTA_SYS_FILE))
2503 return -ENOSYS;
2504 /*
2505 * We don't support turning off accounting via quotactl. In principle
2506 * quota infrastructure can do this but filesystems don't expect
2507 * userspace to be able to do it.
2508 */
2509 if (flags &
2510 (FS_QUOTA_UDQ_ACCT | FS_QUOTA_GDQ_ACCT | FS_QUOTA_PDQ_ACCT))
2511 return -EOPNOTSUPP;
2512
2513 /* Filter out limits not enabled */
2514 for (type = 0; type < MAXQUOTAS; type++)
2515 if (!sb_has_quota_limits_enabled(sb, type))
2516 flags &= ~qtype_enforce_flag(type);
2517 /* Nothing left? */
2518 if (!flags)
2519 return -EEXIST;
2520 for (type = 0; type < MAXQUOTAS; type++) {
2521 if (flags & qtype_enforce_flag(type)) {
2522 ret = dquot_disable(sb, type, DQUOT_LIMITS_ENABLED);
2523 if (ret < 0)
2524 goto out_err;
2525 }
2526 }
2527 return 0;
2528out_err:
2529 /* Backout enforcement disabling we already did */
2530 for (type--; type >= 0; type--) {
2531 if (flags & qtype_enforce_flag(type))
2532 dquot_enable(dqopt->files[type], type,
2533 dqopt->info[type].dqi_fmt_id,
2534 DQUOT_LIMITS_ENABLED);
2535 }
2536 return ret;
2537}
2538
Linus Torvalds1da177e2005-04-16 15:20:36 -07002539/* Generic routine for getting common part of quota structure */
Jan Kara14bf61f2014-10-09 16:03:13 +02002540static void do_get_dqblk(struct dquot *dquot, struct qc_dqblk *di)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002541{
2542 struct mem_dqblk *dm = &dquot->dq_dqb;
2543
Christoph Hellwigb9b2dd32010-05-06 17:04:58 -04002544 memset(di, 0, sizeof(*di));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002545 spin_lock(&dq_data_lock);
Jan Kara14bf61f2014-10-09 16:03:13 +02002546 di->d_spc_hardlimit = dm->dqb_bhardlimit;
2547 di->d_spc_softlimit = dm->dqb_bsoftlimit;
Christoph Hellwigb9b2dd32010-05-06 17:04:58 -04002548 di->d_ino_hardlimit = dm->dqb_ihardlimit;
2549 di->d_ino_softlimit = dm->dqb_isoftlimit;
Jan Kara14bf61f2014-10-09 16:03:13 +02002550 di->d_space = dm->dqb_curspace + dm->dqb_rsvspace;
2551 di->d_ino_count = dm->dqb_curinodes;
2552 di->d_spc_timer = dm->dqb_btime;
2553 di->d_ino_timer = dm->dqb_itime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002554 spin_unlock(&dq_data_lock);
2555}
2556
Eric W. Biederman74a8a102012-09-16 02:07:49 -07002557int dquot_get_dqblk(struct super_block *sb, struct kqid qid,
Jan Kara14bf61f2014-10-09 16:03:13 +02002558 struct qc_dqblk *di)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002559{
2560 struct dquot *dquot;
2561
Eric W. Biedermanaca645a2012-09-16 03:11:50 -07002562 dquot = dqget(sb, qid);
Jan Kara6184fc02015-06-24 18:07:02 +02002563 if (IS_ERR(dquot))
2564 return PTR_ERR(dquot);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002565 do_get_dqblk(dquot, di);
2566 dqput(dquot);
Jan Karacc334122009-01-12 17:23:05 +01002567
Linus Torvalds1da177e2005-04-16 15:20:36 -07002568 return 0;
2569}
Christoph Hellwig287a8092010-05-19 07:16:45 -04002570EXPORT_SYMBOL(dquot_get_dqblk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002571
Jan Karabe6257b2016-01-25 19:24:50 +01002572int dquot_get_next_dqblk(struct super_block *sb, struct kqid *qid,
2573 struct qc_dqblk *di)
2574{
2575 struct dquot *dquot;
2576 int err;
2577
2578 if (!sb->dq_op->get_next_id)
2579 return -ENOSYS;
2580 err = sb->dq_op->get_next_id(sb, qid);
2581 if (err < 0)
2582 return err;
2583 dquot = dqget(sb, *qid);
2584 if (IS_ERR(dquot))
2585 return PTR_ERR(dquot);
2586 do_get_dqblk(dquot, di);
2587 dqput(dquot);
2588
2589 return 0;
2590}
2591EXPORT_SYMBOL(dquot_get_next_dqblk);
2592
Jan Kara14bf61f2014-10-09 16:03:13 +02002593#define VFS_QC_MASK \
2594 (QC_SPACE | QC_SPC_SOFT | QC_SPC_HARD | \
2595 QC_INO_COUNT | QC_INO_SOFT | QC_INO_HARD | \
2596 QC_SPC_TIMER | QC_INO_TIMER)
Christoph Hellwigc472b432010-05-06 17:05:17 -04002597
Linus Torvalds1da177e2005-04-16 15:20:36 -07002598/* Generic routine for setting common part of quota structure */
Jan Kara14bf61f2014-10-09 16:03:13 +02002599static int do_set_dqblk(struct dquot *dquot, struct qc_dqblk *di)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002600{
2601 struct mem_dqblk *dm = &dquot->dq_dqb;
2602 int check_blim = 0, check_ilim = 0;
Eric W. Biederman4c376dc2012-09-16 03:56:19 -07002603 struct mem_dqinfo *dqi = &sb_dqopt(dquot->dq_sb)->info[dquot->dq_id.type];
Andrew Perepechko338bf9a2008-04-28 02:14:31 -07002604
Jan Kara14bf61f2014-10-09 16:03:13 +02002605 if (di->d_fieldmask & ~VFS_QC_MASK)
Christoph Hellwigc472b432010-05-06 17:05:17 -04002606 return -EINVAL;
2607
Jan Kara14bf61f2014-10-09 16:03:13 +02002608 if (((di->d_fieldmask & QC_SPC_SOFT) &&
Jan Karab10a0812014-10-09 16:54:13 +02002609 di->d_spc_softlimit > dqi->dqi_max_spc_limit) ||
Jan Kara14bf61f2014-10-09 16:03:13 +02002610 ((di->d_fieldmask & QC_SPC_HARD) &&
Jan Karab10a0812014-10-09 16:54:13 +02002611 di->d_spc_hardlimit > dqi->dqi_max_spc_limit) ||
Jan Kara14bf61f2014-10-09 16:03:13 +02002612 ((di->d_fieldmask & QC_INO_SOFT) &&
Jan Karab10a0812014-10-09 16:54:13 +02002613 (di->d_ino_softlimit > dqi->dqi_max_ino_limit)) ||
Jan Kara14bf61f2014-10-09 16:03:13 +02002614 ((di->d_fieldmask & QC_INO_HARD) &&
Jan Karab10a0812014-10-09 16:54:13 +02002615 (di->d_ino_hardlimit > dqi->dqi_max_ino_limit)))
Andrew Perepechko338bf9a2008-04-28 02:14:31 -07002616 return -ERANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002617
2618 spin_lock(&dq_data_lock);
Jan Kara14bf61f2014-10-09 16:03:13 +02002619 if (di->d_fieldmask & QC_SPACE) {
2620 dm->dqb_curspace = di->d_space - dm->dqb_rsvspace;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002621 check_blim = 1;
Andrew Perepechko08261672010-04-12 22:16:50 +04002622 set_bit(DQ_LASTSET_B + QIF_SPACE_B, &dquot->dq_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002623 }
Christoph Hellwigc472b432010-05-06 17:05:17 -04002624
Jan Kara14bf61f2014-10-09 16:03:13 +02002625 if (di->d_fieldmask & QC_SPC_SOFT)
2626 dm->dqb_bsoftlimit = di->d_spc_softlimit;
2627 if (di->d_fieldmask & QC_SPC_HARD)
2628 dm->dqb_bhardlimit = di->d_spc_hardlimit;
2629 if (di->d_fieldmask & (QC_SPC_SOFT | QC_SPC_HARD)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002630 check_blim = 1;
Andrew Perepechko08261672010-04-12 22:16:50 +04002631 set_bit(DQ_LASTSET_B + QIF_BLIMITS_B, &dquot->dq_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002632 }
Christoph Hellwigc472b432010-05-06 17:05:17 -04002633
Jan Kara14bf61f2014-10-09 16:03:13 +02002634 if (di->d_fieldmask & QC_INO_COUNT) {
2635 dm->dqb_curinodes = di->d_ino_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002636 check_ilim = 1;
Andrew Perepechko08261672010-04-12 22:16:50 +04002637 set_bit(DQ_LASTSET_B + QIF_INODES_B, &dquot->dq_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002638 }
Christoph Hellwigc472b432010-05-06 17:05:17 -04002639
Jan Kara14bf61f2014-10-09 16:03:13 +02002640 if (di->d_fieldmask & QC_INO_SOFT)
Christoph Hellwigc472b432010-05-06 17:05:17 -04002641 dm->dqb_isoftlimit = di->d_ino_softlimit;
Jan Kara14bf61f2014-10-09 16:03:13 +02002642 if (di->d_fieldmask & QC_INO_HARD)
Christoph Hellwigc472b432010-05-06 17:05:17 -04002643 dm->dqb_ihardlimit = di->d_ino_hardlimit;
Jan Kara14bf61f2014-10-09 16:03:13 +02002644 if (di->d_fieldmask & (QC_INO_SOFT | QC_INO_HARD)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002645 check_ilim = 1;
Andrew Perepechko08261672010-04-12 22:16:50 +04002646 set_bit(DQ_LASTSET_B + QIF_ILIMITS_B, &dquot->dq_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002647 }
Christoph Hellwigc472b432010-05-06 17:05:17 -04002648
Jan Kara14bf61f2014-10-09 16:03:13 +02002649 if (di->d_fieldmask & QC_SPC_TIMER) {
2650 dm->dqb_btime = di->d_spc_timer;
Jan Karae04a88a92009-01-07 18:07:29 -08002651 check_blim = 1;
Andrew Perepechko08261672010-04-12 22:16:50 +04002652 set_bit(DQ_LASTSET_B + QIF_BTIME_B, &dquot->dq_flags);
Jan Kara4d59bce2008-10-02 16:48:10 +02002653 }
Christoph Hellwigc472b432010-05-06 17:05:17 -04002654
Jan Kara14bf61f2014-10-09 16:03:13 +02002655 if (di->d_fieldmask & QC_INO_TIMER) {
2656 dm->dqb_itime = di->d_ino_timer;
Jan Karae04a88a92009-01-07 18:07:29 -08002657 check_ilim = 1;
Andrew Perepechko08261672010-04-12 22:16:50 +04002658 set_bit(DQ_LASTSET_B + QIF_ITIME_B, &dquot->dq_flags);
Jan Kara4d59bce2008-10-02 16:48:10 +02002659 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002660
2661 if (check_blim) {
Jan Kara268157b2009-01-27 15:47:22 +01002662 if (!dm->dqb_bsoftlimit ||
2663 dm->dqb_curspace < dm->dqb_bsoftlimit) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002664 dm->dqb_btime = 0;
2665 clear_bit(DQ_BLKS_B, &dquot->dq_flags);
Jan Kara14bf61f2014-10-09 16:03:13 +02002666 } else if (!(di->d_fieldmask & QC_SPC_TIMER))
Jan Kara268157b2009-01-27 15:47:22 +01002667 /* Set grace only if user hasn't provided his own... */
Arnd Bergmanne008bb62016-06-17 22:03:16 +02002668 dm->dqb_btime = ktime_get_real_seconds() + dqi->dqi_bgrace;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002669 }
2670 if (check_ilim) {
Jan Kara268157b2009-01-27 15:47:22 +01002671 if (!dm->dqb_isoftlimit ||
2672 dm->dqb_curinodes < dm->dqb_isoftlimit) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002673 dm->dqb_itime = 0;
2674 clear_bit(DQ_INODES_B, &dquot->dq_flags);
Jan Kara14bf61f2014-10-09 16:03:13 +02002675 } else if (!(di->d_fieldmask & QC_INO_TIMER))
Jan Kara268157b2009-01-27 15:47:22 +01002676 /* Set grace only if user hasn't provided his own... */
Arnd Bergmanne008bb62016-06-17 22:03:16 +02002677 dm->dqb_itime = ktime_get_real_seconds() + dqi->dqi_igrace;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002678 }
Jan Kara268157b2009-01-27 15:47:22 +01002679 if (dm->dqb_bhardlimit || dm->dqb_bsoftlimit || dm->dqb_ihardlimit ||
2680 dm->dqb_isoftlimit)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002681 clear_bit(DQ_FAKE_B, &dquot->dq_flags);
2682 else
2683 set_bit(DQ_FAKE_B, &dquot->dq_flags);
2684 spin_unlock(&dq_data_lock);
2685 mark_dquot_dirty(dquot);
Andrew Perepechko338bf9a2008-04-28 02:14:31 -07002686
2687 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002688}
2689
Eric W. Biederman74a8a102012-09-16 02:07:49 -07002690int dquot_set_dqblk(struct super_block *sb, struct kqid qid,
Jan Kara14bf61f2014-10-09 16:03:13 +02002691 struct qc_dqblk *di)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002692{
2693 struct dquot *dquot;
Andrew Perepechko338bf9a2008-04-28 02:14:31 -07002694 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002695
Eric W. Biedermanaca645a2012-09-16 03:11:50 -07002696 dquot = dqget(sb, qid);
Jan Kara6184fc02015-06-24 18:07:02 +02002697 if (IS_ERR(dquot)) {
2698 rc = PTR_ERR(dquot);
Jan Karaf55abc02008-08-20 17:50:32 +02002699 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002700 }
Andrew Perepechko338bf9a2008-04-28 02:14:31 -07002701 rc = do_set_dqblk(dquot, di);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002702 dqput(dquot);
Jan Karaf55abc02008-08-20 17:50:32 +02002703out:
Andrew Perepechko338bf9a2008-04-28 02:14:31 -07002704 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002705}
Christoph Hellwig287a8092010-05-19 07:16:45 -04002706EXPORT_SYMBOL(dquot_set_dqblk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002707
2708/* Generic routine for getting common part of quota file information */
Jan Kara0a2403392014-11-19 00:42:09 +01002709int dquot_get_state(struct super_block *sb, struct qc_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002710{
2711 struct mem_dqinfo *mi;
Jan Kara0a2403392014-11-19 00:42:09 +01002712 struct qc_type_state *tstate;
2713 struct quota_info *dqopt = sb_dqopt(sb);
2714 int type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002715
Jan Kara0a2403392014-11-19 00:42:09 +01002716 memset(state, 0, sizeof(*state));
2717 for (type = 0; type < MAXQUOTAS; type++) {
2718 if (!sb_has_quota_active(sb, type))
2719 continue;
2720 tstate = state->s_state + type;
2721 mi = sb_dqopt(sb)->info + type;
2722 tstate->flags = QCI_ACCT_ENABLED;
2723 spin_lock(&dq_data_lock);
2724 if (mi->dqi_flags & DQF_SYS_FILE)
2725 tstate->flags |= QCI_SYSFILE;
2726 if (mi->dqi_flags & DQF_ROOT_SQUASH)
2727 tstate->flags |= QCI_ROOT_SQUASH;
2728 if (sb_has_quota_limits_enabled(sb, type))
2729 tstate->flags |= QCI_LIMITS_ENFORCED;
2730 tstate->spc_timelimit = mi->dqi_bgrace;
2731 tstate->ino_timelimit = mi->dqi_igrace;
2732 tstate->ino = dqopt->files[type]->i_ino;
2733 tstate->blocks = dqopt->files[type]->i_blocks;
2734 tstate->nextents = 1; /* We don't know... */
2735 spin_unlock(&dq_data_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002736 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002737 return 0;
2738}
Jan Kara0a2403392014-11-19 00:42:09 +01002739EXPORT_SYMBOL(dquot_get_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002740
2741/* Generic routine for setting common part of quota file information */
Jan Kara5eacb2a2014-12-16 12:03:51 +01002742int dquot_set_dqinfo(struct super_block *sb, int type, struct qc_info *ii)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002743{
2744 struct mem_dqinfo *mi;
Jan Karaf55abc02008-08-20 17:50:32 +02002745 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002746
Jan Kara5eacb2a2014-12-16 12:03:51 +01002747 if ((ii->i_fieldmask & QC_WARNS_MASK) ||
2748 (ii->i_fieldmask & QC_RT_SPC_TIMER))
2749 return -EINVAL;
Jan Kara9d1ccbe2016-11-23 13:35:14 +01002750 if (!sb_has_quota_active(sb, type))
2751 return -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002752 mi = sb_dqopt(sb)->info + type;
Jan Kara5eacb2a2014-12-16 12:03:51 +01002753 if (ii->i_fieldmask & QC_FLAGS) {
2754 if ((ii->i_flags & QCI_ROOT_SQUASH &&
Jan Kara9d1ccbe2016-11-23 13:35:14 +01002755 mi->dqi_format->qf_fmt_id != QFMT_VFS_OLD))
2756 return -EINVAL;
Jan Karaca6cb0912014-11-19 09:32:39 +01002757 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002758 spin_lock(&dq_data_lock);
Jan Kara5eacb2a2014-12-16 12:03:51 +01002759 if (ii->i_fieldmask & QC_SPC_TIMER)
2760 mi->dqi_bgrace = ii->i_spc_timelimit;
2761 if (ii->i_fieldmask & QC_INO_TIMER)
2762 mi->dqi_igrace = ii->i_ino_timelimit;
2763 if (ii->i_fieldmask & QC_FLAGS) {
2764 if (ii->i_flags & QCI_ROOT_SQUASH)
2765 mi->dqi_flags |= DQF_ROOT_SQUASH;
2766 else
2767 mi->dqi_flags &= ~DQF_ROOT_SQUASH;
2768 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002769 spin_unlock(&dq_data_lock);
2770 mark_info_dirty(sb, type);
2771 /* Force write to disk */
2772 sb->dq_op->write_info(sb, type);
Jan Karaf55abc02008-08-20 17:50:32 +02002773 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002774}
Christoph Hellwig287a8092010-05-19 07:16:45 -04002775EXPORT_SYMBOL(dquot_set_dqinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002776
Jan Kara3e2af672014-10-06 18:40:51 +02002777const struct quotactl_ops dquot_quotactl_sysfile_ops = {
2778 .quota_enable = dquot_quota_enable,
2779 .quota_disable = dquot_quota_disable,
2780 .quota_sync = dquot_quota_sync,
Jan Kara0a2403392014-11-19 00:42:09 +01002781 .get_state = dquot_get_state,
Jan Kara3e2af672014-10-06 18:40:51 +02002782 .set_info = dquot_set_dqinfo,
2783 .get_dqblk = dquot_get_dqblk,
Jan Karabe6257b2016-01-25 19:24:50 +01002784 .get_nextdqblk = dquot_get_next_dqblk,
Jan Kara3e2af672014-10-06 18:40:51 +02002785 .set_dqblk = dquot_set_dqblk
2786};
2787EXPORT_SYMBOL(dquot_quotactl_sysfile_ops);
2788
Dmitry Monakhovdde95882010-04-26 20:03:33 +04002789static int do_proc_dqstats(struct ctl_table *table, int write,
2790 void __user *buffer, size_t *lenp, loff_t *ppos)
2791{
Dmitry Monakhovdde95882010-04-26 20:03:33 +04002792 unsigned int type = (int *)table->data - dqstats.stat;
Dmitry Monakhovf32764b2010-05-26 23:21:58 +02002793
2794 /* Update global table */
2795 dqstats.stat[type] =
2796 percpu_counter_sum_positive(&dqstats.counter[type]);
Dmitry Monakhovdde95882010-04-26 20:03:33 +04002797 return proc_dointvec(table, write, buffer, lenp, ppos);
2798}
2799
Joe Perchese6287532013-06-13 19:37:49 -07002800static struct ctl_table fs_dqstats_table[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002801 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002802 .procname = "lookups",
Dmitry Monakhovdde95882010-04-26 20:03:33 +04002803 .data = &dqstats.stat[DQST_LOOKUPS],
Linus Torvalds1da177e2005-04-16 15:20:36 -07002804 .maxlen = sizeof(int),
2805 .mode = 0444,
Dmitry Monakhovdde95882010-04-26 20:03:33 +04002806 .proc_handler = do_proc_dqstats,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002807 },
2808 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002809 .procname = "drops",
Dmitry Monakhovdde95882010-04-26 20:03:33 +04002810 .data = &dqstats.stat[DQST_DROPS],
Linus Torvalds1da177e2005-04-16 15:20:36 -07002811 .maxlen = sizeof(int),
2812 .mode = 0444,
Dmitry Monakhovdde95882010-04-26 20:03:33 +04002813 .proc_handler = do_proc_dqstats,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002814 },
2815 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002816 .procname = "reads",
Dmitry Monakhovdde95882010-04-26 20:03:33 +04002817 .data = &dqstats.stat[DQST_READS],
Linus Torvalds1da177e2005-04-16 15:20:36 -07002818 .maxlen = sizeof(int),
2819 .mode = 0444,
Dmitry Monakhovdde95882010-04-26 20:03:33 +04002820 .proc_handler = do_proc_dqstats,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002821 },
2822 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002823 .procname = "writes",
Dmitry Monakhovdde95882010-04-26 20:03:33 +04002824 .data = &dqstats.stat[DQST_WRITES],
Linus Torvalds1da177e2005-04-16 15:20:36 -07002825 .maxlen = sizeof(int),
2826 .mode = 0444,
Dmitry Monakhovdde95882010-04-26 20:03:33 +04002827 .proc_handler = do_proc_dqstats,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002828 },
2829 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002830 .procname = "cache_hits",
Dmitry Monakhovdde95882010-04-26 20:03:33 +04002831 .data = &dqstats.stat[DQST_CACHE_HITS],
Linus Torvalds1da177e2005-04-16 15:20:36 -07002832 .maxlen = sizeof(int),
2833 .mode = 0444,
Dmitry Monakhovdde95882010-04-26 20:03:33 +04002834 .proc_handler = do_proc_dqstats,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002835 },
2836 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002837 .procname = "allocated_dquots",
Dmitry Monakhovdde95882010-04-26 20:03:33 +04002838 .data = &dqstats.stat[DQST_ALLOC_DQUOTS],
Linus Torvalds1da177e2005-04-16 15:20:36 -07002839 .maxlen = sizeof(int),
2840 .mode = 0444,
Dmitry Monakhovdde95882010-04-26 20:03:33 +04002841 .proc_handler = do_proc_dqstats,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002842 },
2843 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002844 .procname = "free_dquots",
Dmitry Monakhovdde95882010-04-26 20:03:33 +04002845 .data = &dqstats.stat[DQST_FREE_DQUOTS],
Linus Torvalds1da177e2005-04-16 15:20:36 -07002846 .maxlen = sizeof(int),
2847 .mode = 0444,
Dmitry Monakhovdde95882010-04-26 20:03:33 +04002848 .proc_handler = do_proc_dqstats,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002849 },
2850 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002851 .procname = "syncs",
Dmitry Monakhovdde95882010-04-26 20:03:33 +04002852 .data = &dqstats.stat[DQST_SYNCS],
Linus Torvalds1da177e2005-04-16 15:20:36 -07002853 .maxlen = sizeof(int),
2854 .mode = 0444,
Dmitry Monakhovdde95882010-04-26 20:03:33 +04002855 .proc_handler = do_proc_dqstats,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002856 },
Jan Kara8e893462007-10-16 23:29:31 -07002857#ifdef CONFIG_PRINT_QUOTA_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07002858 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002859 .procname = "warnings",
2860 .data = &flag_print_warnings,
2861 .maxlen = sizeof(int),
2862 .mode = 0644,
Eric W. Biederman6d456112009-11-16 03:11:48 -08002863 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002864 },
Jan Kara8e893462007-10-16 23:29:31 -07002865#endif
Eric W. Biedermanab092032009-11-05 14:25:10 -08002866 { },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002867};
2868
Joe Perchese6287532013-06-13 19:37:49 -07002869static struct ctl_table fs_table[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002870 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002871 .procname = "quota",
2872 .mode = 0555,
2873 .child = fs_dqstats_table,
2874 },
Eric W. Biedermanab092032009-11-05 14:25:10 -08002875 { },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002876};
2877
Joe Perchese6287532013-06-13 19:37:49 -07002878static struct ctl_table sys_table[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002879 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002880 .procname = "fs",
2881 .mode = 0555,
2882 .child = fs_table,
2883 },
Eric W. Biedermanab092032009-11-05 14:25:10 -08002884 { },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002885};
2886
2887static int __init dquot_init(void)
2888{
Dmitry Monakhovf32764b2010-05-26 23:21:58 +02002889 int i, ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002890 unsigned long nr_hash, order;
2891
2892 printk(KERN_NOTICE "VFS: Disk quotas %s\n", __DQUOT_VERSION__);
2893
Eric W. Biederman0b4d4142007-02-14 00:34:09 -08002894 register_sysctl_table(sys_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002895
Paul Mundt20c2df82007-07-20 10:11:58 +09002896 dquot_cachep = kmem_cache_create("dquot",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002897 sizeof(struct dquot), sizeof(unsigned long) * 4,
Paul Jacksonfffb60f2006-03-24 03:16:06 -08002898 (SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT|
2899 SLAB_MEM_SPREAD|SLAB_PANIC),
Paul Mundt20c2df82007-07-20 10:11:58 +09002900 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002901
2902 order = 0;
2903 dquot_hash = (struct hlist_head *)__get_free_pages(GFP_ATOMIC, order);
2904 if (!dquot_hash)
2905 panic("Cannot create dquot hash table");
2906
Dmitry Monakhovf32764b2010-05-26 23:21:58 +02002907 for (i = 0; i < _DQST_DQSTAT_LAST; i++) {
Tejun Heo908c7f12014-09-08 09:51:29 +09002908 ret = percpu_counter_init(&dqstats.counter[i], 0, GFP_KERNEL);
Dmitry Monakhovf32764b2010-05-26 23:21:58 +02002909 if (ret)
2910 panic("Cannot create dquot stat counters");
2911 }
Dmitry Monakhovdde95882010-04-26 20:03:33 +04002912
Linus Torvalds1da177e2005-04-16 15:20:36 -07002913 /* Find power-of-two hlist_heads which can fit into allocation */
2914 nr_hash = (1UL << order) * PAGE_SIZE / sizeof(struct hlist_head);
2915 dq_hash_bits = 0;
2916 do {
2917 dq_hash_bits++;
2918 } while (nr_hash >> dq_hash_bits);
2919 dq_hash_bits--;
2920
2921 nr_hash = 1UL << dq_hash_bits;
2922 dq_hash_mask = nr_hash - 1;
2923 for (i = 0; i < nr_hash; i++)
2924 INIT_HLIST_HEAD(dquot_hash + i);
2925
Anton Blanchard19858e72014-11-04 12:05:01 +11002926 pr_info("VFS: Dquot-cache hash table entries: %ld (order %ld,"
2927 " %ld bytes)\n", nr_hash, order, (PAGE_SIZE << order));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002928
Rusty Russell8e1f9362007-07-17 04:03:17 -07002929 register_shrinker(&dqcache_shrinker);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002930
Linus Torvalds1da177e2005-04-16 15:20:36 -07002931 return 0;
2932}
Paul Gortmaker331221f2015-12-12 16:30:12 -05002933fs_initcall(dquot_init);