xref: /OK3568_Linux_fs/kernel/fs/quota/dquot.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * Implementation of the diskquota system for the LINUX operating system. QUOTA
4*4882a593Smuzhiyun  * is implemented using the BSD system call interface as the means of
5*4882a593Smuzhiyun  * communication with the user level. This file contains the generic routines
6*4882a593Smuzhiyun  * called by the different filesystems on allocation of an inode or block.
7*4882a593Smuzhiyun  * These routines take care of the administration needed to have a consistent
8*4882a593Smuzhiyun  * diskquota tracking system. The ideas of both user and group quotas are based
9*4882a593Smuzhiyun  * on the Melbourne quota system as used on BSD derived systems. The internal
10*4882a593Smuzhiyun  * implementation is based on one of the several variants of the LINUX
11*4882a593Smuzhiyun  * inode-subsystem with added complexity of the diskquota system.
12*4882a593Smuzhiyun  *
13*4882a593Smuzhiyun  * Author:	Marco van Wieringen <mvw@planets.elm.net>
14*4882a593Smuzhiyun  *
15*4882a593Smuzhiyun  * Fixes:   Dmitry Gorodchanin <pgmdsg@ibi.com>, 11 Feb 96
16*4882a593Smuzhiyun  *
17*4882a593Smuzhiyun  *		Revised list management to avoid races
18*4882a593Smuzhiyun  *		-- Bill Hawes, <whawes@star.net>, 9/98
19*4882a593Smuzhiyun  *
20*4882a593Smuzhiyun  *		Fixed races in dquot_transfer(), dqget() and dquot_alloc_...().
21*4882a593Smuzhiyun  *		As the consequence the locking was moved from dquot_decr_...(),
22*4882a593Smuzhiyun  *		dquot_incr_...() to calling functions.
23*4882a593Smuzhiyun  *		invalidate_dquots() now writes modified dquots.
24*4882a593Smuzhiyun  *		Serialized quota_off() and quota_on() for mount point.
25*4882a593Smuzhiyun  *		Fixed a few bugs in grow_dquots().
26*4882a593Smuzhiyun  *		Fixed deadlock in write_dquot() - we no longer account quotas on
27*4882a593Smuzhiyun  *		quota files
28*4882a593Smuzhiyun  *		remove_dquot_ref() moved to inode.c - it now traverses through inodes
29*4882a593Smuzhiyun  *		add_dquot_ref() restarts after blocking
30*4882a593Smuzhiyun  *		Added check for bogus uid and fixed check for group in quotactl.
31*4882a593Smuzhiyun  *		Jan Kara, <jack@suse.cz>, sponsored by SuSE CR, 10-11/99
32*4882a593Smuzhiyun  *
33*4882a593Smuzhiyun  *		Used struct list_head instead of own list struct
34*4882a593Smuzhiyun  *		Invalidation of referenced dquots is no longer possible
35*4882a593Smuzhiyun  *		Improved free_dquots list management
36*4882a593Smuzhiyun  *		Quota and i_blocks are now updated in one place to avoid races
37*4882a593Smuzhiyun  *		Warnings are now delayed so we won't block in critical section
38*4882a593Smuzhiyun  *		Write updated not to require dquot lock
39*4882a593Smuzhiyun  *		Jan Kara, <jack@suse.cz>, 9/2000
40*4882a593Smuzhiyun  *
41*4882a593Smuzhiyun  *		Added dynamic quota structure allocation
42*4882a593Smuzhiyun  *		Jan Kara <jack@suse.cz> 12/2000
43*4882a593Smuzhiyun  *
44*4882a593Smuzhiyun  *		Rewritten quota interface. Implemented new quota format and
45*4882a593Smuzhiyun  *		formats registering.
46*4882a593Smuzhiyun  *		Jan Kara, <jack@suse.cz>, 2001,2002
47*4882a593Smuzhiyun  *
48*4882a593Smuzhiyun  *		New SMP locking.
49*4882a593Smuzhiyun  *		Jan Kara, <jack@suse.cz>, 10/2002
50*4882a593Smuzhiyun  *
51*4882a593Smuzhiyun  *		Added journalled quota support, fix lock inversion problems
52*4882a593Smuzhiyun  *		Jan Kara, <jack@suse.cz>, 2003,2004
53*4882a593Smuzhiyun  *
54*4882a593Smuzhiyun  * (C) Copyright 1994 - 1997 Marco van Wieringen
55*4882a593Smuzhiyun  */
56*4882a593Smuzhiyun 
57*4882a593Smuzhiyun #include <linux/errno.h>
58*4882a593Smuzhiyun #include <linux/kernel.h>
59*4882a593Smuzhiyun #include <linux/fs.h>
60*4882a593Smuzhiyun #include <linux/mount.h>
61*4882a593Smuzhiyun #include <linux/mm.h>
62*4882a593Smuzhiyun #include <linux/time.h>
63*4882a593Smuzhiyun #include <linux/types.h>
64*4882a593Smuzhiyun #include <linux/string.h>
65*4882a593Smuzhiyun #include <linux/fcntl.h>
66*4882a593Smuzhiyun #include <linux/stat.h>
67*4882a593Smuzhiyun #include <linux/tty.h>
68*4882a593Smuzhiyun #include <linux/file.h>
69*4882a593Smuzhiyun #include <linux/slab.h>
70*4882a593Smuzhiyun #include <linux/sysctl.h>
71*4882a593Smuzhiyun #include <linux/init.h>
72*4882a593Smuzhiyun #include <linux/module.h>
73*4882a593Smuzhiyun #include <linux/proc_fs.h>
74*4882a593Smuzhiyun #include <linux/security.h>
75*4882a593Smuzhiyun #include <linux/sched.h>
76*4882a593Smuzhiyun #include <linux/cred.h>
77*4882a593Smuzhiyun #include <linux/kmod.h>
78*4882a593Smuzhiyun #include <linux/namei.h>
79*4882a593Smuzhiyun #include <linux/capability.h>
80*4882a593Smuzhiyun #include <linux/quotaops.h>
81*4882a593Smuzhiyun #include <linux/blkdev.h>
82*4882a593Smuzhiyun #include <linux/sched/mm.h>
83*4882a593Smuzhiyun #include "../internal.h" /* ugh */
84*4882a593Smuzhiyun 
85*4882a593Smuzhiyun #include <linux/uaccess.h>
86*4882a593Smuzhiyun 
87*4882a593Smuzhiyun /*
88*4882a593Smuzhiyun  * There are five quota SMP locks:
89*4882a593Smuzhiyun  * * dq_list_lock protects all lists with quotas and quota formats.
90*4882a593Smuzhiyun  * * dquot->dq_dqb_lock protects data from dq_dqb
91*4882a593Smuzhiyun  * * inode->i_lock protects inode->i_blocks, i_bytes and also guards
92*4882a593Smuzhiyun  *   consistency of dquot->dq_dqb with inode->i_blocks, i_bytes so that
93*4882a593Smuzhiyun  *   dquot_transfer() can stabilize amount it transfers
94*4882a593Smuzhiyun  * * dq_data_lock protects mem_dqinfo structures and modifications of dquot
95*4882a593Smuzhiyun  *   pointers in the inode
96*4882a593Smuzhiyun  * * dq_state_lock protects modifications of quota state (on quotaon and
97*4882a593Smuzhiyun  *   quotaoff) and readers who care about latest values take it as well.
98*4882a593Smuzhiyun  *
99*4882a593Smuzhiyun  * The spinlock ordering is hence:
100*4882a593Smuzhiyun  *   dq_data_lock > dq_list_lock > i_lock > dquot->dq_dqb_lock,
101*4882a593Smuzhiyun  *   dq_list_lock > dq_state_lock
102*4882a593Smuzhiyun  *
103*4882a593Smuzhiyun  * Note that some things (eg. sb pointer, type, id) doesn't change during
104*4882a593Smuzhiyun  * the life of the dquot structure and so needn't to be protected by a lock
105*4882a593Smuzhiyun  *
106*4882a593Smuzhiyun  * Operation accessing dquots via inode pointers are protected by dquot_srcu.
107*4882a593Smuzhiyun  * Operation of reading pointer needs srcu_read_lock(&dquot_srcu), and
108*4882a593Smuzhiyun  * synchronize_srcu(&dquot_srcu) is called after clearing pointers from
109*4882a593Smuzhiyun  * inode and before dropping dquot references to avoid use of dquots after
110*4882a593Smuzhiyun  * they are freed. dq_data_lock is used to serialize the pointer setting and
111*4882a593Smuzhiyun  * clearing operations.
112*4882a593Smuzhiyun  * Special care needs to be taken about S_NOQUOTA inode flag (marking that
113*4882a593Smuzhiyun  * inode is a quota file). Functions adding pointers from inode to dquots have
114*4882a593Smuzhiyun  * to check this flag under dq_data_lock and then (if S_NOQUOTA is not set) they
115*4882a593Smuzhiyun  * have to do all pointer modifications before dropping dq_data_lock. This makes
116*4882a593Smuzhiyun  * sure they cannot race with quotaon which first sets S_NOQUOTA flag and
117*4882a593Smuzhiyun  * then drops all pointers to dquots from an inode.
118*4882a593Smuzhiyun  *
119*4882a593Smuzhiyun  * Each dquot has its dq_lock mutex.  Dquot is locked when it is being read to
120*4882a593Smuzhiyun  * memory (or space for it is being allocated) on the first dqget(), when it is
121*4882a593Smuzhiyun  * being written out, and when it is being released on the last dqput(). The
122*4882a593Smuzhiyun  * allocation and release operations are serialized by the dq_lock and by
123*4882a593Smuzhiyun  * checking the use count in dquot_release().
124*4882a593Smuzhiyun  *
125*4882a593Smuzhiyun  * Lock ordering (including related VFS locks) is the following:
126*4882a593Smuzhiyun  *   s_umount > i_mutex > journal_lock > dquot->dq_lock > dqio_sem
127*4882a593Smuzhiyun  */
128*4882a593Smuzhiyun 
129*4882a593Smuzhiyun static __cacheline_aligned_in_smp DEFINE_SPINLOCK(dq_list_lock);
130*4882a593Smuzhiyun static __cacheline_aligned_in_smp DEFINE_SPINLOCK(dq_state_lock);
131*4882a593Smuzhiyun __cacheline_aligned_in_smp DEFINE_SPINLOCK(dq_data_lock);
132*4882a593Smuzhiyun EXPORT_SYMBOL(dq_data_lock);
133*4882a593Smuzhiyun DEFINE_STATIC_SRCU(dquot_srcu);
134*4882a593Smuzhiyun 
135*4882a593Smuzhiyun static DECLARE_WAIT_QUEUE_HEAD(dquot_ref_wq);
136*4882a593Smuzhiyun 
__quota_error(struct super_block * sb,const char * func,const char * fmt,...)137*4882a593Smuzhiyun void __quota_error(struct super_block *sb, const char *func,
138*4882a593Smuzhiyun 		   const char *fmt, ...)
139*4882a593Smuzhiyun {
140*4882a593Smuzhiyun 	if (printk_ratelimit()) {
141*4882a593Smuzhiyun 		va_list args;
142*4882a593Smuzhiyun 		struct va_format vaf;
143*4882a593Smuzhiyun 
144*4882a593Smuzhiyun 		va_start(args, fmt);
145*4882a593Smuzhiyun 
146*4882a593Smuzhiyun 		vaf.fmt = fmt;
147*4882a593Smuzhiyun 		vaf.va = &args;
148*4882a593Smuzhiyun 
149*4882a593Smuzhiyun 		printk(KERN_ERR "Quota error (device %s): %s: %pV\n",
150*4882a593Smuzhiyun 		       sb->s_id, func, &vaf);
151*4882a593Smuzhiyun 
152*4882a593Smuzhiyun 		va_end(args);
153*4882a593Smuzhiyun 	}
154*4882a593Smuzhiyun }
155*4882a593Smuzhiyun EXPORT_SYMBOL(__quota_error);
156*4882a593Smuzhiyun 
157*4882a593Smuzhiyun #if defined(CONFIG_QUOTA_DEBUG) || defined(CONFIG_PRINT_QUOTA_WARNING)
158*4882a593Smuzhiyun static char *quotatypes[] = INITQFNAMES;
159*4882a593Smuzhiyun #endif
160*4882a593Smuzhiyun static struct quota_format_type *quota_formats;	/* List of registered formats */
161*4882a593Smuzhiyun static struct quota_module_name module_names[] = INIT_QUOTA_MODULE_NAMES;
162*4882a593Smuzhiyun 
163*4882a593Smuzhiyun /* SLAB cache for dquot structures */
164*4882a593Smuzhiyun static struct kmem_cache *dquot_cachep;
165*4882a593Smuzhiyun 
register_quota_format(struct quota_format_type * fmt)166*4882a593Smuzhiyun int register_quota_format(struct quota_format_type *fmt)
167*4882a593Smuzhiyun {
168*4882a593Smuzhiyun 	spin_lock(&dq_list_lock);
169*4882a593Smuzhiyun 	fmt->qf_next = quota_formats;
170*4882a593Smuzhiyun 	quota_formats = fmt;
171*4882a593Smuzhiyun 	spin_unlock(&dq_list_lock);
172*4882a593Smuzhiyun 	return 0;
173*4882a593Smuzhiyun }
174*4882a593Smuzhiyun EXPORT_SYMBOL(register_quota_format);
175*4882a593Smuzhiyun 
unregister_quota_format(struct quota_format_type * fmt)176*4882a593Smuzhiyun void unregister_quota_format(struct quota_format_type *fmt)
177*4882a593Smuzhiyun {
178*4882a593Smuzhiyun 	struct quota_format_type **actqf;
179*4882a593Smuzhiyun 
180*4882a593Smuzhiyun 	spin_lock(&dq_list_lock);
181*4882a593Smuzhiyun 	for (actqf = &quota_formats; *actqf && *actqf != fmt;
182*4882a593Smuzhiyun 	     actqf = &(*actqf)->qf_next)
183*4882a593Smuzhiyun 		;
184*4882a593Smuzhiyun 	if (*actqf)
185*4882a593Smuzhiyun 		*actqf = (*actqf)->qf_next;
186*4882a593Smuzhiyun 	spin_unlock(&dq_list_lock);
187*4882a593Smuzhiyun }
188*4882a593Smuzhiyun EXPORT_SYMBOL(unregister_quota_format);
189*4882a593Smuzhiyun 
find_quota_format(int id)190*4882a593Smuzhiyun static struct quota_format_type *find_quota_format(int id)
191*4882a593Smuzhiyun {
192*4882a593Smuzhiyun 	struct quota_format_type *actqf;
193*4882a593Smuzhiyun 
194*4882a593Smuzhiyun 	spin_lock(&dq_list_lock);
195*4882a593Smuzhiyun 	for (actqf = quota_formats; actqf && actqf->qf_fmt_id != id;
196*4882a593Smuzhiyun 	     actqf = actqf->qf_next)
197*4882a593Smuzhiyun 		;
198*4882a593Smuzhiyun 	if (!actqf || !try_module_get(actqf->qf_owner)) {
199*4882a593Smuzhiyun 		int qm;
200*4882a593Smuzhiyun 
201*4882a593Smuzhiyun 		spin_unlock(&dq_list_lock);
202*4882a593Smuzhiyun 
203*4882a593Smuzhiyun 		for (qm = 0; module_names[qm].qm_fmt_id &&
204*4882a593Smuzhiyun 			     module_names[qm].qm_fmt_id != id; qm++)
205*4882a593Smuzhiyun 			;
206*4882a593Smuzhiyun 		if (!module_names[qm].qm_fmt_id ||
207*4882a593Smuzhiyun 		    request_module(module_names[qm].qm_mod_name))
208*4882a593Smuzhiyun 			return NULL;
209*4882a593Smuzhiyun 
210*4882a593Smuzhiyun 		spin_lock(&dq_list_lock);
211*4882a593Smuzhiyun 		for (actqf = quota_formats; actqf && actqf->qf_fmt_id != id;
212*4882a593Smuzhiyun 		     actqf = actqf->qf_next)
213*4882a593Smuzhiyun 			;
214*4882a593Smuzhiyun 		if (actqf && !try_module_get(actqf->qf_owner))
215*4882a593Smuzhiyun 			actqf = NULL;
216*4882a593Smuzhiyun 	}
217*4882a593Smuzhiyun 	spin_unlock(&dq_list_lock);
218*4882a593Smuzhiyun 	return actqf;
219*4882a593Smuzhiyun }
220*4882a593Smuzhiyun 
put_quota_format(struct quota_format_type * fmt)221*4882a593Smuzhiyun static void put_quota_format(struct quota_format_type *fmt)
222*4882a593Smuzhiyun {
223*4882a593Smuzhiyun 	module_put(fmt->qf_owner);
224*4882a593Smuzhiyun }
225*4882a593Smuzhiyun 
226*4882a593Smuzhiyun /*
227*4882a593Smuzhiyun  * Dquot List Management:
228*4882a593Smuzhiyun  * The quota code uses four lists for dquot management: the inuse_list,
229*4882a593Smuzhiyun  * free_dquots, dqi_dirty_list, and dquot_hash[] array. A single dquot
230*4882a593Smuzhiyun  * structure may be on some of those lists, depending on its current state.
231*4882a593Smuzhiyun  *
232*4882a593Smuzhiyun  * All dquots are placed to the end of inuse_list when first created, and this
233*4882a593Smuzhiyun  * list is used for invalidate operation, which must look at every dquot.
234*4882a593Smuzhiyun  *
235*4882a593Smuzhiyun  * Unused dquots (dq_count == 0) are added to the free_dquots list when freed,
236*4882a593Smuzhiyun  * and this list is searched whenever we need an available dquot.  Dquots are
237*4882a593Smuzhiyun  * removed from the list as soon as they are used again, and
238*4882a593Smuzhiyun  * dqstats.free_dquots gives the number of dquots on the list. When
239*4882a593Smuzhiyun  * dquot is invalidated it's completely released from memory.
240*4882a593Smuzhiyun  *
241*4882a593Smuzhiyun  * Dirty dquots are added to the dqi_dirty_list of quota_info when mark
242*4882a593Smuzhiyun  * dirtied, and this list is searched when writing dirty dquots back to
243*4882a593Smuzhiyun  * quota file. Note that some filesystems do dirty dquot tracking on their
244*4882a593Smuzhiyun  * own (e.g. in a journal) and thus don't use dqi_dirty_list.
245*4882a593Smuzhiyun  *
246*4882a593Smuzhiyun  * Dquots with a specific identity (device, type and id) are placed on
247*4882a593Smuzhiyun  * one of the dquot_hash[] hash chains. The provides an efficient search
248*4882a593Smuzhiyun  * mechanism to locate a specific dquot.
249*4882a593Smuzhiyun  */
250*4882a593Smuzhiyun 
251*4882a593Smuzhiyun static LIST_HEAD(inuse_list);
252*4882a593Smuzhiyun static LIST_HEAD(free_dquots);
253*4882a593Smuzhiyun static unsigned int dq_hash_bits, dq_hash_mask;
254*4882a593Smuzhiyun static struct hlist_head *dquot_hash;
255*4882a593Smuzhiyun 
256*4882a593Smuzhiyun struct dqstats dqstats;
257*4882a593Smuzhiyun EXPORT_SYMBOL(dqstats);
258*4882a593Smuzhiyun 
259*4882a593Smuzhiyun static qsize_t inode_get_rsv_space(struct inode *inode);
260*4882a593Smuzhiyun static qsize_t __inode_get_rsv_space(struct inode *inode);
261*4882a593Smuzhiyun static int __dquot_initialize(struct inode *inode, int type);
262*4882a593Smuzhiyun 
263*4882a593Smuzhiyun static inline unsigned int
hashfn(const struct super_block * sb,struct kqid qid)264*4882a593Smuzhiyun hashfn(const struct super_block *sb, struct kqid qid)
265*4882a593Smuzhiyun {
266*4882a593Smuzhiyun 	unsigned int id = from_kqid(&init_user_ns, qid);
267*4882a593Smuzhiyun 	int type = qid.type;
268*4882a593Smuzhiyun 	unsigned long tmp;
269*4882a593Smuzhiyun 
270*4882a593Smuzhiyun 	tmp = (((unsigned long)sb>>L1_CACHE_SHIFT) ^ id) * (MAXQUOTAS - type);
271*4882a593Smuzhiyun 	return (tmp + (tmp >> dq_hash_bits)) & dq_hash_mask;
272*4882a593Smuzhiyun }
273*4882a593Smuzhiyun 
274*4882a593Smuzhiyun /*
275*4882a593Smuzhiyun  * Following list functions expect dq_list_lock to be held
276*4882a593Smuzhiyun  */
insert_dquot_hash(struct dquot * dquot)277*4882a593Smuzhiyun static inline void insert_dquot_hash(struct dquot *dquot)
278*4882a593Smuzhiyun {
279*4882a593Smuzhiyun 	struct hlist_head *head;
280*4882a593Smuzhiyun 	head = dquot_hash + hashfn(dquot->dq_sb, dquot->dq_id);
281*4882a593Smuzhiyun 	hlist_add_head(&dquot->dq_hash, head);
282*4882a593Smuzhiyun }
283*4882a593Smuzhiyun 
remove_dquot_hash(struct dquot * dquot)284*4882a593Smuzhiyun static inline void remove_dquot_hash(struct dquot *dquot)
285*4882a593Smuzhiyun {
286*4882a593Smuzhiyun 	hlist_del_init(&dquot->dq_hash);
287*4882a593Smuzhiyun }
288*4882a593Smuzhiyun 
find_dquot(unsigned int hashent,struct super_block * sb,struct kqid qid)289*4882a593Smuzhiyun static struct dquot *find_dquot(unsigned int hashent, struct super_block *sb,
290*4882a593Smuzhiyun 				struct kqid qid)
291*4882a593Smuzhiyun {
292*4882a593Smuzhiyun 	struct hlist_node *node;
293*4882a593Smuzhiyun 	struct dquot *dquot;
294*4882a593Smuzhiyun 
295*4882a593Smuzhiyun 	hlist_for_each (node, dquot_hash+hashent) {
296*4882a593Smuzhiyun 		dquot = hlist_entry(node, struct dquot, dq_hash);
297*4882a593Smuzhiyun 		if (dquot->dq_sb == sb && qid_eq(dquot->dq_id, qid))
298*4882a593Smuzhiyun 			return dquot;
299*4882a593Smuzhiyun 	}
300*4882a593Smuzhiyun 	return NULL;
301*4882a593Smuzhiyun }
302*4882a593Smuzhiyun 
303*4882a593Smuzhiyun /* Add a dquot to the tail of the free list */
put_dquot_last(struct dquot * dquot)304*4882a593Smuzhiyun static inline void put_dquot_last(struct dquot *dquot)
305*4882a593Smuzhiyun {
306*4882a593Smuzhiyun 	list_add_tail(&dquot->dq_free, &free_dquots);
307*4882a593Smuzhiyun 	dqstats_inc(DQST_FREE_DQUOTS);
308*4882a593Smuzhiyun }
309*4882a593Smuzhiyun 
remove_free_dquot(struct dquot * dquot)310*4882a593Smuzhiyun static inline void remove_free_dquot(struct dquot *dquot)
311*4882a593Smuzhiyun {
312*4882a593Smuzhiyun 	if (list_empty(&dquot->dq_free))
313*4882a593Smuzhiyun 		return;
314*4882a593Smuzhiyun 	list_del_init(&dquot->dq_free);
315*4882a593Smuzhiyun 	dqstats_dec(DQST_FREE_DQUOTS);
316*4882a593Smuzhiyun }
317*4882a593Smuzhiyun 
put_inuse(struct dquot * dquot)318*4882a593Smuzhiyun static inline void put_inuse(struct dquot *dquot)
319*4882a593Smuzhiyun {
320*4882a593Smuzhiyun 	/* We add to the back of inuse list so we don't have to restart
321*4882a593Smuzhiyun 	 * when traversing this list and we block */
322*4882a593Smuzhiyun 	list_add_tail(&dquot->dq_inuse, &inuse_list);
323*4882a593Smuzhiyun 	dqstats_inc(DQST_ALLOC_DQUOTS);
324*4882a593Smuzhiyun }
325*4882a593Smuzhiyun 
remove_inuse(struct dquot * dquot)326*4882a593Smuzhiyun static inline void remove_inuse(struct dquot *dquot)
327*4882a593Smuzhiyun {
328*4882a593Smuzhiyun 	dqstats_dec(DQST_ALLOC_DQUOTS);
329*4882a593Smuzhiyun 	list_del(&dquot->dq_inuse);
330*4882a593Smuzhiyun }
331*4882a593Smuzhiyun /*
332*4882a593Smuzhiyun  * End of list functions needing dq_list_lock
333*4882a593Smuzhiyun  */
334*4882a593Smuzhiyun 
wait_on_dquot(struct dquot * dquot)335*4882a593Smuzhiyun static void wait_on_dquot(struct dquot *dquot)
336*4882a593Smuzhiyun {
337*4882a593Smuzhiyun 	mutex_lock(&dquot->dq_lock);
338*4882a593Smuzhiyun 	mutex_unlock(&dquot->dq_lock);
339*4882a593Smuzhiyun }
340*4882a593Smuzhiyun 
dquot_dirty(struct dquot * dquot)341*4882a593Smuzhiyun static inline int dquot_dirty(struct dquot *dquot)
342*4882a593Smuzhiyun {
343*4882a593Smuzhiyun 	return test_bit(DQ_MOD_B, &dquot->dq_flags);
344*4882a593Smuzhiyun }
345*4882a593Smuzhiyun 
mark_dquot_dirty(struct dquot * dquot)346*4882a593Smuzhiyun static inline int mark_dquot_dirty(struct dquot *dquot)
347*4882a593Smuzhiyun {
348*4882a593Smuzhiyun 	return dquot->dq_sb->dq_op->mark_dirty(dquot);
349*4882a593Smuzhiyun }
350*4882a593Smuzhiyun 
351*4882a593Smuzhiyun /* Mark dquot dirty in atomic manner, and return it's old dirty flag state */
dquot_mark_dquot_dirty(struct dquot * dquot)352*4882a593Smuzhiyun int dquot_mark_dquot_dirty(struct dquot *dquot)
353*4882a593Smuzhiyun {
354*4882a593Smuzhiyun 	int ret = 1;
355*4882a593Smuzhiyun 
356*4882a593Smuzhiyun 	if (!test_bit(DQ_ACTIVE_B, &dquot->dq_flags))
357*4882a593Smuzhiyun 		return 0;
358*4882a593Smuzhiyun 
359*4882a593Smuzhiyun 	if (sb_dqopt(dquot->dq_sb)->flags & DQUOT_NOLIST_DIRTY)
360*4882a593Smuzhiyun 		return test_and_set_bit(DQ_MOD_B, &dquot->dq_flags);
361*4882a593Smuzhiyun 
362*4882a593Smuzhiyun 	/* If quota is dirty already, we don't have to acquire dq_list_lock */
363*4882a593Smuzhiyun 	if (test_bit(DQ_MOD_B, &dquot->dq_flags))
364*4882a593Smuzhiyun 		return 1;
365*4882a593Smuzhiyun 
366*4882a593Smuzhiyun 	spin_lock(&dq_list_lock);
367*4882a593Smuzhiyun 	if (!test_and_set_bit(DQ_MOD_B, &dquot->dq_flags)) {
368*4882a593Smuzhiyun 		list_add(&dquot->dq_dirty, &sb_dqopt(dquot->dq_sb)->
369*4882a593Smuzhiyun 				info[dquot->dq_id.type].dqi_dirty_list);
370*4882a593Smuzhiyun 		ret = 0;
371*4882a593Smuzhiyun 	}
372*4882a593Smuzhiyun 	spin_unlock(&dq_list_lock);
373*4882a593Smuzhiyun 	return ret;
374*4882a593Smuzhiyun }
375*4882a593Smuzhiyun EXPORT_SYMBOL(dquot_mark_dquot_dirty);
376*4882a593Smuzhiyun 
377*4882a593Smuzhiyun /* Dirtify all the dquots - this can block when journalling */
mark_all_dquot_dirty(struct dquot * const * dquot)378*4882a593Smuzhiyun static inline int mark_all_dquot_dirty(struct dquot * const *dquot)
379*4882a593Smuzhiyun {
380*4882a593Smuzhiyun 	int ret, err, cnt;
381*4882a593Smuzhiyun 
382*4882a593Smuzhiyun 	ret = err = 0;
383*4882a593Smuzhiyun 	for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
384*4882a593Smuzhiyun 		if (dquot[cnt])
385*4882a593Smuzhiyun 			/* Even in case of error we have to continue */
386*4882a593Smuzhiyun 			ret = mark_dquot_dirty(dquot[cnt]);
387*4882a593Smuzhiyun 		if (!err)
388*4882a593Smuzhiyun 			err = ret;
389*4882a593Smuzhiyun 	}
390*4882a593Smuzhiyun 	return err;
391*4882a593Smuzhiyun }
392*4882a593Smuzhiyun 
dqput_all(struct dquot ** dquot)393*4882a593Smuzhiyun static inline void dqput_all(struct dquot **dquot)
394*4882a593Smuzhiyun {
395*4882a593Smuzhiyun 	unsigned int cnt;
396*4882a593Smuzhiyun 
397*4882a593Smuzhiyun 	for (cnt = 0; cnt < MAXQUOTAS; cnt++)
398*4882a593Smuzhiyun 		dqput(dquot[cnt]);
399*4882a593Smuzhiyun }
400*4882a593Smuzhiyun 
clear_dquot_dirty(struct dquot * dquot)401*4882a593Smuzhiyun static inline int clear_dquot_dirty(struct dquot *dquot)
402*4882a593Smuzhiyun {
403*4882a593Smuzhiyun 	if (sb_dqopt(dquot->dq_sb)->flags & DQUOT_NOLIST_DIRTY)
404*4882a593Smuzhiyun 		return test_and_clear_bit(DQ_MOD_B, &dquot->dq_flags);
405*4882a593Smuzhiyun 
406*4882a593Smuzhiyun 	spin_lock(&dq_list_lock);
407*4882a593Smuzhiyun 	if (!test_and_clear_bit(DQ_MOD_B, &dquot->dq_flags)) {
408*4882a593Smuzhiyun 		spin_unlock(&dq_list_lock);
409*4882a593Smuzhiyun 		return 0;
410*4882a593Smuzhiyun 	}
411*4882a593Smuzhiyun 	list_del_init(&dquot->dq_dirty);
412*4882a593Smuzhiyun 	spin_unlock(&dq_list_lock);
413*4882a593Smuzhiyun 	return 1;
414*4882a593Smuzhiyun }
415*4882a593Smuzhiyun 
mark_info_dirty(struct super_block * sb,int type)416*4882a593Smuzhiyun void mark_info_dirty(struct super_block *sb, int type)
417*4882a593Smuzhiyun {
418*4882a593Smuzhiyun 	spin_lock(&dq_data_lock);
419*4882a593Smuzhiyun 	sb_dqopt(sb)->info[type].dqi_flags |= DQF_INFO_DIRTY;
420*4882a593Smuzhiyun 	spin_unlock(&dq_data_lock);
421*4882a593Smuzhiyun }
422*4882a593Smuzhiyun EXPORT_SYMBOL(mark_info_dirty);
423*4882a593Smuzhiyun 
424*4882a593Smuzhiyun /*
425*4882a593Smuzhiyun  *	Read dquot from disk and alloc space for it
426*4882a593Smuzhiyun  */
427*4882a593Smuzhiyun 
dquot_acquire(struct dquot * dquot)428*4882a593Smuzhiyun int dquot_acquire(struct dquot *dquot)
429*4882a593Smuzhiyun {
430*4882a593Smuzhiyun 	int ret = 0, ret2 = 0;
431*4882a593Smuzhiyun 	unsigned int memalloc;
432*4882a593Smuzhiyun 	struct quota_info *dqopt = sb_dqopt(dquot->dq_sb);
433*4882a593Smuzhiyun 
434*4882a593Smuzhiyun 	mutex_lock(&dquot->dq_lock);
435*4882a593Smuzhiyun 	memalloc = memalloc_nofs_save();
436*4882a593Smuzhiyun 	if (!test_bit(DQ_READ_B, &dquot->dq_flags)) {
437*4882a593Smuzhiyun 		ret = dqopt->ops[dquot->dq_id.type]->read_dqblk(dquot);
438*4882a593Smuzhiyun 		if (ret < 0)
439*4882a593Smuzhiyun 			goto out_iolock;
440*4882a593Smuzhiyun 	}
441*4882a593Smuzhiyun 	/* Make sure flags update is visible after dquot has been filled */
442*4882a593Smuzhiyun 	smp_mb__before_atomic();
443*4882a593Smuzhiyun 	set_bit(DQ_READ_B, &dquot->dq_flags);
444*4882a593Smuzhiyun 	/* Instantiate dquot if needed */
445*4882a593Smuzhiyun 	if (!test_bit(DQ_ACTIVE_B, &dquot->dq_flags) && !dquot->dq_off) {
446*4882a593Smuzhiyun 		ret = dqopt->ops[dquot->dq_id.type]->commit_dqblk(dquot);
447*4882a593Smuzhiyun 		/* Write the info if needed */
448*4882a593Smuzhiyun 		if (info_dirty(&dqopt->info[dquot->dq_id.type])) {
449*4882a593Smuzhiyun 			ret2 = dqopt->ops[dquot->dq_id.type]->write_file_info(
450*4882a593Smuzhiyun 					dquot->dq_sb, dquot->dq_id.type);
451*4882a593Smuzhiyun 		}
452*4882a593Smuzhiyun 		if (ret < 0)
453*4882a593Smuzhiyun 			goto out_iolock;
454*4882a593Smuzhiyun 		if (ret2 < 0) {
455*4882a593Smuzhiyun 			ret = ret2;
456*4882a593Smuzhiyun 			goto out_iolock;
457*4882a593Smuzhiyun 		}
458*4882a593Smuzhiyun 	}
459*4882a593Smuzhiyun 	/*
460*4882a593Smuzhiyun 	 * Make sure flags update is visible after on-disk struct has been
461*4882a593Smuzhiyun 	 * allocated. Paired with smp_rmb() in dqget().
462*4882a593Smuzhiyun 	 */
463*4882a593Smuzhiyun 	smp_mb__before_atomic();
464*4882a593Smuzhiyun 	set_bit(DQ_ACTIVE_B, &dquot->dq_flags);
465*4882a593Smuzhiyun out_iolock:
466*4882a593Smuzhiyun 	memalloc_nofs_restore(memalloc);
467*4882a593Smuzhiyun 	mutex_unlock(&dquot->dq_lock);
468*4882a593Smuzhiyun 	return ret;
469*4882a593Smuzhiyun }
470*4882a593Smuzhiyun EXPORT_SYMBOL(dquot_acquire);
471*4882a593Smuzhiyun 
472*4882a593Smuzhiyun /*
473*4882a593Smuzhiyun  *	Write dquot to disk
474*4882a593Smuzhiyun  */
dquot_commit(struct dquot * dquot)475*4882a593Smuzhiyun int dquot_commit(struct dquot *dquot)
476*4882a593Smuzhiyun {
477*4882a593Smuzhiyun 	int ret = 0;
478*4882a593Smuzhiyun 	unsigned int memalloc;
479*4882a593Smuzhiyun 	struct quota_info *dqopt = sb_dqopt(dquot->dq_sb);
480*4882a593Smuzhiyun 
481*4882a593Smuzhiyun 	mutex_lock(&dquot->dq_lock);
482*4882a593Smuzhiyun 	memalloc = memalloc_nofs_save();
483*4882a593Smuzhiyun 	if (!clear_dquot_dirty(dquot))
484*4882a593Smuzhiyun 		goto out_lock;
485*4882a593Smuzhiyun 	/* Inactive dquot can be only if there was error during read/init
486*4882a593Smuzhiyun 	 * => we have better not writing it */
487*4882a593Smuzhiyun 	if (test_bit(DQ_ACTIVE_B, &dquot->dq_flags))
488*4882a593Smuzhiyun 		ret = dqopt->ops[dquot->dq_id.type]->commit_dqblk(dquot);
489*4882a593Smuzhiyun 	else
490*4882a593Smuzhiyun 		ret = -EIO;
491*4882a593Smuzhiyun out_lock:
492*4882a593Smuzhiyun 	memalloc_nofs_restore(memalloc);
493*4882a593Smuzhiyun 	mutex_unlock(&dquot->dq_lock);
494*4882a593Smuzhiyun 	return ret;
495*4882a593Smuzhiyun }
496*4882a593Smuzhiyun EXPORT_SYMBOL(dquot_commit);
497*4882a593Smuzhiyun 
498*4882a593Smuzhiyun /*
499*4882a593Smuzhiyun  *	Release dquot
500*4882a593Smuzhiyun  */
dquot_release(struct dquot * dquot)501*4882a593Smuzhiyun int dquot_release(struct dquot *dquot)
502*4882a593Smuzhiyun {
503*4882a593Smuzhiyun 	int ret = 0, ret2 = 0;
504*4882a593Smuzhiyun 	unsigned int memalloc;
505*4882a593Smuzhiyun 	struct quota_info *dqopt = sb_dqopt(dquot->dq_sb);
506*4882a593Smuzhiyun 
507*4882a593Smuzhiyun 	mutex_lock(&dquot->dq_lock);
508*4882a593Smuzhiyun 	memalloc = memalloc_nofs_save();
509*4882a593Smuzhiyun 	/* Check whether we are not racing with some other dqget() */
510*4882a593Smuzhiyun 	if (dquot_is_busy(dquot))
511*4882a593Smuzhiyun 		goto out_dqlock;
512*4882a593Smuzhiyun 	if (dqopt->ops[dquot->dq_id.type]->release_dqblk) {
513*4882a593Smuzhiyun 		ret = dqopt->ops[dquot->dq_id.type]->release_dqblk(dquot);
514*4882a593Smuzhiyun 		/* Write the info */
515*4882a593Smuzhiyun 		if (info_dirty(&dqopt->info[dquot->dq_id.type])) {
516*4882a593Smuzhiyun 			ret2 = dqopt->ops[dquot->dq_id.type]->write_file_info(
517*4882a593Smuzhiyun 						dquot->dq_sb, dquot->dq_id.type);
518*4882a593Smuzhiyun 		}
519*4882a593Smuzhiyun 		if (ret >= 0)
520*4882a593Smuzhiyun 			ret = ret2;
521*4882a593Smuzhiyun 	}
522*4882a593Smuzhiyun 	clear_bit(DQ_ACTIVE_B, &dquot->dq_flags);
523*4882a593Smuzhiyun out_dqlock:
524*4882a593Smuzhiyun 	memalloc_nofs_restore(memalloc);
525*4882a593Smuzhiyun 	mutex_unlock(&dquot->dq_lock);
526*4882a593Smuzhiyun 	return ret;
527*4882a593Smuzhiyun }
528*4882a593Smuzhiyun EXPORT_SYMBOL(dquot_release);
529*4882a593Smuzhiyun 
dquot_destroy(struct dquot * dquot)530*4882a593Smuzhiyun void dquot_destroy(struct dquot *dquot)
531*4882a593Smuzhiyun {
532*4882a593Smuzhiyun 	kmem_cache_free(dquot_cachep, dquot);
533*4882a593Smuzhiyun }
534*4882a593Smuzhiyun EXPORT_SYMBOL(dquot_destroy);
535*4882a593Smuzhiyun 
do_destroy_dquot(struct dquot * dquot)536*4882a593Smuzhiyun static inline void do_destroy_dquot(struct dquot *dquot)
537*4882a593Smuzhiyun {
538*4882a593Smuzhiyun 	dquot->dq_sb->dq_op->destroy_dquot(dquot);
539*4882a593Smuzhiyun }
540*4882a593Smuzhiyun 
541*4882a593Smuzhiyun /* Invalidate all dquots on the list. Note that this function is called after
542*4882a593Smuzhiyun  * quota is disabled and pointers from inodes removed so there cannot be new
543*4882a593Smuzhiyun  * quota users. There can still be some users of quotas due to inodes being
544*4882a593Smuzhiyun  * just deleted or pruned by prune_icache() (those are not attached to any
545*4882a593Smuzhiyun  * list) or parallel quotactl call. We have to wait for such users.
546*4882a593Smuzhiyun  */
invalidate_dquots(struct super_block * sb,int type)547*4882a593Smuzhiyun static void invalidate_dquots(struct super_block *sb, int type)
548*4882a593Smuzhiyun {
549*4882a593Smuzhiyun 	struct dquot *dquot, *tmp;
550*4882a593Smuzhiyun 
551*4882a593Smuzhiyun restart:
552*4882a593Smuzhiyun 	spin_lock(&dq_list_lock);
553*4882a593Smuzhiyun 	list_for_each_entry_safe(dquot, tmp, &inuse_list, dq_inuse) {
554*4882a593Smuzhiyun 		if (dquot->dq_sb != sb)
555*4882a593Smuzhiyun 			continue;
556*4882a593Smuzhiyun 		if (dquot->dq_id.type != type)
557*4882a593Smuzhiyun 			continue;
558*4882a593Smuzhiyun 		/* Wait for dquot users */
559*4882a593Smuzhiyun 		if (atomic_read(&dquot->dq_count)) {
560*4882a593Smuzhiyun 			dqgrab(dquot);
561*4882a593Smuzhiyun 			spin_unlock(&dq_list_lock);
562*4882a593Smuzhiyun 			/*
563*4882a593Smuzhiyun 			 * Once dqput() wakes us up, we know it's time to free
564*4882a593Smuzhiyun 			 * the dquot.
565*4882a593Smuzhiyun 			 * IMPORTANT: we rely on the fact that there is always
566*4882a593Smuzhiyun 			 * at most one process waiting for dquot to free.
567*4882a593Smuzhiyun 			 * Otherwise dq_count would be > 1 and we would never
568*4882a593Smuzhiyun 			 * wake up.
569*4882a593Smuzhiyun 			 */
570*4882a593Smuzhiyun 			wait_event(dquot_ref_wq,
571*4882a593Smuzhiyun 				   atomic_read(&dquot->dq_count) == 1);
572*4882a593Smuzhiyun 			dqput(dquot);
573*4882a593Smuzhiyun 			/* At this moment dquot() need not exist (it could be
574*4882a593Smuzhiyun 			 * reclaimed by prune_dqcache(). Hence we must
575*4882a593Smuzhiyun 			 * restart. */
576*4882a593Smuzhiyun 			goto restart;
577*4882a593Smuzhiyun 		}
578*4882a593Smuzhiyun 		/*
579*4882a593Smuzhiyun 		 * Quota now has no users and it has been written on last
580*4882a593Smuzhiyun 		 * dqput()
581*4882a593Smuzhiyun 		 */
582*4882a593Smuzhiyun 		remove_dquot_hash(dquot);
583*4882a593Smuzhiyun 		remove_free_dquot(dquot);
584*4882a593Smuzhiyun 		remove_inuse(dquot);
585*4882a593Smuzhiyun 		do_destroy_dquot(dquot);
586*4882a593Smuzhiyun 	}
587*4882a593Smuzhiyun 	spin_unlock(&dq_list_lock);
588*4882a593Smuzhiyun }
589*4882a593Smuzhiyun 
590*4882a593Smuzhiyun /* Call callback for every active dquot on given filesystem */
dquot_scan_active(struct super_block * sb,int (* fn)(struct dquot * dquot,unsigned long priv),unsigned long priv)591*4882a593Smuzhiyun int dquot_scan_active(struct super_block *sb,
592*4882a593Smuzhiyun 		      int (*fn)(struct dquot *dquot, unsigned long priv),
593*4882a593Smuzhiyun 		      unsigned long priv)
594*4882a593Smuzhiyun {
595*4882a593Smuzhiyun 	struct dquot *dquot, *old_dquot = NULL;
596*4882a593Smuzhiyun 	int ret = 0;
597*4882a593Smuzhiyun 
598*4882a593Smuzhiyun 	WARN_ON_ONCE(!rwsem_is_locked(&sb->s_umount));
599*4882a593Smuzhiyun 
600*4882a593Smuzhiyun 	spin_lock(&dq_list_lock);
601*4882a593Smuzhiyun 	list_for_each_entry(dquot, &inuse_list, dq_inuse) {
602*4882a593Smuzhiyun 		if (!test_bit(DQ_ACTIVE_B, &dquot->dq_flags))
603*4882a593Smuzhiyun 			continue;
604*4882a593Smuzhiyun 		if (dquot->dq_sb != sb)
605*4882a593Smuzhiyun 			continue;
606*4882a593Smuzhiyun 		/* Now we have active dquot so we can just increase use count */
607*4882a593Smuzhiyun 		atomic_inc(&dquot->dq_count);
608*4882a593Smuzhiyun 		spin_unlock(&dq_list_lock);
609*4882a593Smuzhiyun 		dqput(old_dquot);
610*4882a593Smuzhiyun 		old_dquot = dquot;
611*4882a593Smuzhiyun 		/*
612*4882a593Smuzhiyun 		 * ->release_dquot() can be racing with us. Our reference
613*4882a593Smuzhiyun 		 * protects us from new calls to it so just wait for any
614*4882a593Smuzhiyun 		 * outstanding call and recheck the DQ_ACTIVE_B after that.
615*4882a593Smuzhiyun 		 */
616*4882a593Smuzhiyun 		wait_on_dquot(dquot);
617*4882a593Smuzhiyun 		if (test_bit(DQ_ACTIVE_B, &dquot->dq_flags)) {
618*4882a593Smuzhiyun 			ret = fn(dquot, priv);
619*4882a593Smuzhiyun 			if (ret < 0)
620*4882a593Smuzhiyun 				goto out;
621*4882a593Smuzhiyun 		}
622*4882a593Smuzhiyun 		spin_lock(&dq_list_lock);
623*4882a593Smuzhiyun 		/* We are safe to continue now because our dquot could not
624*4882a593Smuzhiyun 		 * be moved out of the inuse list while we hold the reference */
625*4882a593Smuzhiyun 	}
626*4882a593Smuzhiyun 	spin_unlock(&dq_list_lock);
627*4882a593Smuzhiyun out:
628*4882a593Smuzhiyun 	dqput(old_dquot);
629*4882a593Smuzhiyun 	return ret;
630*4882a593Smuzhiyun }
631*4882a593Smuzhiyun EXPORT_SYMBOL(dquot_scan_active);
632*4882a593Smuzhiyun 
633*4882a593Smuzhiyun /* Write all dquot structures to quota files */
dquot_writeback_dquots(struct super_block * sb,int type)634*4882a593Smuzhiyun int dquot_writeback_dquots(struct super_block *sb, int type)
635*4882a593Smuzhiyun {
636*4882a593Smuzhiyun 	struct list_head dirty;
637*4882a593Smuzhiyun 	struct dquot *dquot;
638*4882a593Smuzhiyun 	struct quota_info *dqopt = sb_dqopt(sb);
639*4882a593Smuzhiyun 	int cnt;
640*4882a593Smuzhiyun 	int err, ret = 0;
641*4882a593Smuzhiyun 
642*4882a593Smuzhiyun 	WARN_ON_ONCE(!rwsem_is_locked(&sb->s_umount));
643*4882a593Smuzhiyun 
644*4882a593Smuzhiyun 	for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
645*4882a593Smuzhiyun 		if (type != -1 && cnt != type)
646*4882a593Smuzhiyun 			continue;
647*4882a593Smuzhiyun 		if (!sb_has_quota_active(sb, cnt))
648*4882a593Smuzhiyun 			continue;
649*4882a593Smuzhiyun 		spin_lock(&dq_list_lock);
650*4882a593Smuzhiyun 		/* Move list away to avoid livelock. */
651*4882a593Smuzhiyun 		list_replace_init(&dqopt->info[cnt].dqi_dirty_list, &dirty);
652*4882a593Smuzhiyun 		while (!list_empty(&dirty)) {
653*4882a593Smuzhiyun 			dquot = list_first_entry(&dirty, struct dquot,
654*4882a593Smuzhiyun 						 dq_dirty);
655*4882a593Smuzhiyun 
656*4882a593Smuzhiyun 			WARN_ON(!test_bit(DQ_ACTIVE_B, &dquot->dq_flags));
657*4882a593Smuzhiyun 
658*4882a593Smuzhiyun 			/* Now we have active dquot from which someone is
659*4882a593Smuzhiyun  			 * holding reference so we can safely just increase
660*4882a593Smuzhiyun 			 * use count */
661*4882a593Smuzhiyun 			dqgrab(dquot);
662*4882a593Smuzhiyun 			spin_unlock(&dq_list_lock);
663*4882a593Smuzhiyun 			err = sb->dq_op->write_dquot(dquot);
664*4882a593Smuzhiyun 			if (err) {
665*4882a593Smuzhiyun 				/*
666*4882a593Smuzhiyun 				 * Clear dirty bit anyway to avoid infinite
667*4882a593Smuzhiyun 				 * loop here.
668*4882a593Smuzhiyun 				 */
669*4882a593Smuzhiyun 				clear_dquot_dirty(dquot);
670*4882a593Smuzhiyun 				if (!ret)
671*4882a593Smuzhiyun 					ret = err;
672*4882a593Smuzhiyun 			}
673*4882a593Smuzhiyun 			dqput(dquot);
674*4882a593Smuzhiyun 			spin_lock(&dq_list_lock);
675*4882a593Smuzhiyun 		}
676*4882a593Smuzhiyun 		spin_unlock(&dq_list_lock);
677*4882a593Smuzhiyun 	}
678*4882a593Smuzhiyun 
679*4882a593Smuzhiyun 	for (cnt = 0; cnt < MAXQUOTAS; cnt++)
680*4882a593Smuzhiyun 		if ((cnt == type || type == -1) && sb_has_quota_active(sb, cnt)
681*4882a593Smuzhiyun 		    && info_dirty(&dqopt->info[cnt]))
682*4882a593Smuzhiyun 			sb->dq_op->write_info(sb, cnt);
683*4882a593Smuzhiyun 	dqstats_inc(DQST_SYNCS);
684*4882a593Smuzhiyun 
685*4882a593Smuzhiyun 	return ret;
686*4882a593Smuzhiyun }
687*4882a593Smuzhiyun EXPORT_SYMBOL(dquot_writeback_dquots);
688*4882a593Smuzhiyun 
689*4882a593Smuzhiyun /* Write all dquot structures to disk and make them visible from userspace */
dquot_quota_sync(struct super_block * sb,int type)690*4882a593Smuzhiyun int dquot_quota_sync(struct super_block *sb, int type)
691*4882a593Smuzhiyun {
692*4882a593Smuzhiyun 	struct quota_info *dqopt = sb_dqopt(sb);
693*4882a593Smuzhiyun 	int cnt;
694*4882a593Smuzhiyun 	int ret;
695*4882a593Smuzhiyun 
696*4882a593Smuzhiyun 	ret = dquot_writeback_dquots(sb, type);
697*4882a593Smuzhiyun 	if (ret)
698*4882a593Smuzhiyun 		return ret;
699*4882a593Smuzhiyun 	if (dqopt->flags & DQUOT_QUOTA_SYS_FILE)
700*4882a593Smuzhiyun 		return 0;
701*4882a593Smuzhiyun 
702*4882a593Smuzhiyun 	/* This is not very clever (and fast) but currently I don't know about
703*4882a593Smuzhiyun 	 * any other simple way of getting quota data to disk and we must get
704*4882a593Smuzhiyun 	 * them there for userspace to be visible... */
705*4882a593Smuzhiyun 	if (sb->s_op->sync_fs) {
706*4882a593Smuzhiyun 		ret = sb->s_op->sync_fs(sb, 1);
707*4882a593Smuzhiyun 		if (ret)
708*4882a593Smuzhiyun 			return ret;
709*4882a593Smuzhiyun 	}
710*4882a593Smuzhiyun 	ret = sync_blockdev(sb->s_bdev);
711*4882a593Smuzhiyun 	if (ret)
712*4882a593Smuzhiyun 		return ret;
713*4882a593Smuzhiyun 
714*4882a593Smuzhiyun 	/*
715*4882a593Smuzhiyun 	 * Now when everything is written we can discard the pagecache so
716*4882a593Smuzhiyun 	 * that userspace sees the changes.
717*4882a593Smuzhiyun 	 */
718*4882a593Smuzhiyun 	for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
719*4882a593Smuzhiyun 		if (type != -1 && cnt != type)
720*4882a593Smuzhiyun 			continue;
721*4882a593Smuzhiyun 		if (!sb_has_quota_active(sb, cnt))
722*4882a593Smuzhiyun 			continue;
723*4882a593Smuzhiyun 		inode_lock(dqopt->files[cnt]);
724*4882a593Smuzhiyun 		truncate_inode_pages(&dqopt->files[cnt]->i_data, 0);
725*4882a593Smuzhiyun 		inode_unlock(dqopt->files[cnt]);
726*4882a593Smuzhiyun 	}
727*4882a593Smuzhiyun 
728*4882a593Smuzhiyun 	return 0;
729*4882a593Smuzhiyun }
730*4882a593Smuzhiyun EXPORT_SYMBOL(dquot_quota_sync);
731*4882a593Smuzhiyun 
732*4882a593Smuzhiyun static unsigned long
dqcache_shrink_scan(struct shrinker * shrink,struct shrink_control * sc)733*4882a593Smuzhiyun dqcache_shrink_scan(struct shrinker *shrink, struct shrink_control *sc)
734*4882a593Smuzhiyun {
735*4882a593Smuzhiyun 	struct dquot *dquot;
736*4882a593Smuzhiyun 	unsigned long freed = 0;
737*4882a593Smuzhiyun 
738*4882a593Smuzhiyun 	spin_lock(&dq_list_lock);
739*4882a593Smuzhiyun 	while (!list_empty(&free_dquots) && sc->nr_to_scan) {
740*4882a593Smuzhiyun 		dquot = list_first_entry(&free_dquots, struct dquot, dq_free);
741*4882a593Smuzhiyun 		remove_dquot_hash(dquot);
742*4882a593Smuzhiyun 		remove_free_dquot(dquot);
743*4882a593Smuzhiyun 		remove_inuse(dquot);
744*4882a593Smuzhiyun 		do_destroy_dquot(dquot);
745*4882a593Smuzhiyun 		sc->nr_to_scan--;
746*4882a593Smuzhiyun 		freed++;
747*4882a593Smuzhiyun 	}
748*4882a593Smuzhiyun 	spin_unlock(&dq_list_lock);
749*4882a593Smuzhiyun 	return freed;
750*4882a593Smuzhiyun }
751*4882a593Smuzhiyun 
752*4882a593Smuzhiyun static unsigned long
dqcache_shrink_count(struct shrinker * shrink,struct shrink_control * sc)753*4882a593Smuzhiyun dqcache_shrink_count(struct shrinker *shrink, struct shrink_control *sc)
754*4882a593Smuzhiyun {
755*4882a593Smuzhiyun 	return vfs_pressure_ratio(
756*4882a593Smuzhiyun 	percpu_counter_read_positive(&dqstats.counter[DQST_FREE_DQUOTS]));
757*4882a593Smuzhiyun }
758*4882a593Smuzhiyun 
759*4882a593Smuzhiyun static struct shrinker dqcache_shrinker = {
760*4882a593Smuzhiyun 	.count_objects = dqcache_shrink_count,
761*4882a593Smuzhiyun 	.scan_objects = dqcache_shrink_scan,
762*4882a593Smuzhiyun 	.seeks = DEFAULT_SEEKS,
763*4882a593Smuzhiyun };
764*4882a593Smuzhiyun 
765*4882a593Smuzhiyun /*
766*4882a593Smuzhiyun  * Put reference to dquot
767*4882a593Smuzhiyun  */
dqput(struct dquot * dquot)768*4882a593Smuzhiyun void dqput(struct dquot *dquot)
769*4882a593Smuzhiyun {
770*4882a593Smuzhiyun 	int ret;
771*4882a593Smuzhiyun 
772*4882a593Smuzhiyun 	if (!dquot)
773*4882a593Smuzhiyun 		return;
774*4882a593Smuzhiyun #ifdef CONFIG_QUOTA_DEBUG
775*4882a593Smuzhiyun 	if (!atomic_read(&dquot->dq_count)) {
776*4882a593Smuzhiyun 		quota_error(dquot->dq_sb, "trying to free free dquot of %s %d",
777*4882a593Smuzhiyun 			    quotatypes[dquot->dq_id.type],
778*4882a593Smuzhiyun 			    from_kqid(&init_user_ns, dquot->dq_id));
779*4882a593Smuzhiyun 		BUG();
780*4882a593Smuzhiyun 	}
781*4882a593Smuzhiyun #endif
782*4882a593Smuzhiyun 	dqstats_inc(DQST_DROPS);
783*4882a593Smuzhiyun we_slept:
784*4882a593Smuzhiyun 	spin_lock(&dq_list_lock);
785*4882a593Smuzhiyun 	if (atomic_read(&dquot->dq_count) > 1) {
786*4882a593Smuzhiyun 		/* We have more than one user... nothing to do */
787*4882a593Smuzhiyun 		atomic_dec(&dquot->dq_count);
788*4882a593Smuzhiyun 		/* Releasing dquot during quotaoff phase? */
789*4882a593Smuzhiyun 		if (!sb_has_quota_active(dquot->dq_sb, dquot->dq_id.type) &&
790*4882a593Smuzhiyun 		    atomic_read(&dquot->dq_count) == 1)
791*4882a593Smuzhiyun 			wake_up(&dquot_ref_wq);
792*4882a593Smuzhiyun 		spin_unlock(&dq_list_lock);
793*4882a593Smuzhiyun 		return;
794*4882a593Smuzhiyun 	}
795*4882a593Smuzhiyun 	/* Need to release dquot? */
796*4882a593Smuzhiyun 	if (dquot_dirty(dquot)) {
797*4882a593Smuzhiyun 		spin_unlock(&dq_list_lock);
798*4882a593Smuzhiyun 		/* Commit dquot before releasing */
799*4882a593Smuzhiyun 		ret = dquot->dq_sb->dq_op->write_dquot(dquot);
800*4882a593Smuzhiyun 		if (ret < 0) {
801*4882a593Smuzhiyun 			quota_error(dquot->dq_sb, "Can't write quota structure"
802*4882a593Smuzhiyun 				    " (error %d). Quota may get out of sync!",
803*4882a593Smuzhiyun 				    ret);
804*4882a593Smuzhiyun 			/*
805*4882a593Smuzhiyun 			 * We clear dirty bit anyway, so that we avoid
806*4882a593Smuzhiyun 			 * infinite loop here
807*4882a593Smuzhiyun 			 */
808*4882a593Smuzhiyun 			clear_dquot_dirty(dquot);
809*4882a593Smuzhiyun 		}
810*4882a593Smuzhiyun 		goto we_slept;
811*4882a593Smuzhiyun 	}
812*4882a593Smuzhiyun 	if (test_bit(DQ_ACTIVE_B, &dquot->dq_flags)) {
813*4882a593Smuzhiyun 		spin_unlock(&dq_list_lock);
814*4882a593Smuzhiyun 		dquot->dq_sb->dq_op->release_dquot(dquot);
815*4882a593Smuzhiyun 		goto we_slept;
816*4882a593Smuzhiyun 	}
817*4882a593Smuzhiyun 	atomic_dec(&dquot->dq_count);
818*4882a593Smuzhiyun #ifdef CONFIG_QUOTA_DEBUG
819*4882a593Smuzhiyun 	/* sanity check */
820*4882a593Smuzhiyun 	BUG_ON(!list_empty(&dquot->dq_free));
821*4882a593Smuzhiyun #endif
822*4882a593Smuzhiyun 	put_dquot_last(dquot);
823*4882a593Smuzhiyun 	spin_unlock(&dq_list_lock);
824*4882a593Smuzhiyun }
825*4882a593Smuzhiyun EXPORT_SYMBOL(dqput);
826*4882a593Smuzhiyun 
dquot_alloc(struct super_block * sb,int type)827*4882a593Smuzhiyun struct dquot *dquot_alloc(struct super_block *sb, int type)
828*4882a593Smuzhiyun {
829*4882a593Smuzhiyun 	return kmem_cache_zalloc(dquot_cachep, GFP_NOFS);
830*4882a593Smuzhiyun }
831*4882a593Smuzhiyun EXPORT_SYMBOL(dquot_alloc);
832*4882a593Smuzhiyun 
get_empty_dquot(struct super_block * sb,int type)833*4882a593Smuzhiyun static struct dquot *get_empty_dquot(struct super_block *sb, int type)
834*4882a593Smuzhiyun {
835*4882a593Smuzhiyun 	struct dquot *dquot;
836*4882a593Smuzhiyun 
837*4882a593Smuzhiyun 	dquot = sb->dq_op->alloc_dquot(sb, type);
838*4882a593Smuzhiyun 	if(!dquot)
839*4882a593Smuzhiyun 		return NULL;
840*4882a593Smuzhiyun 
841*4882a593Smuzhiyun 	mutex_init(&dquot->dq_lock);
842*4882a593Smuzhiyun 	INIT_LIST_HEAD(&dquot->dq_free);
843*4882a593Smuzhiyun 	INIT_LIST_HEAD(&dquot->dq_inuse);
844*4882a593Smuzhiyun 	INIT_HLIST_NODE(&dquot->dq_hash);
845*4882a593Smuzhiyun 	INIT_LIST_HEAD(&dquot->dq_dirty);
846*4882a593Smuzhiyun 	dquot->dq_sb = sb;
847*4882a593Smuzhiyun 	dquot->dq_id = make_kqid_invalid(type);
848*4882a593Smuzhiyun 	atomic_set(&dquot->dq_count, 1);
849*4882a593Smuzhiyun 	spin_lock_init(&dquot->dq_dqb_lock);
850*4882a593Smuzhiyun 
851*4882a593Smuzhiyun 	return dquot;
852*4882a593Smuzhiyun }
853*4882a593Smuzhiyun 
854*4882a593Smuzhiyun /*
855*4882a593Smuzhiyun  * Get reference to dquot
856*4882a593Smuzhiyun  *
857*4882a593Smuzhiyun  * Locking is slightly tricky here. We are guarded from parallel quotaoff()
858*4882a593Smuzhiyun  * destroying our dquot by:
859*4882a593Smuzhiyun  *   a) checking for quota flags under dq_list_lock and
860*4882a593Smuzhiyun  *   b) getting a reference to dquot before we release dq_list_lock
861*4882a593Smuzhiyun  */
dqget(struct super_block * sb,struct kqid qid)862*4882a593Smuzhiyun struct dquot *dqget(struct super_block *sb, struct kqid qid)
863*4882a593Smuzhiyun {
864*4882a593Smuzhiyun 	unsigned int hashent = hashfn(sb, qid);
865*4882a593Smuzhiyun 	struct dquot *dquot, *empty = NULL;
866*4882a593Smuzhiyun 
867*4882a593Smuzhiyun 	if (!qid_has_mapping(sb->s_user_ns, qid))
868*4882a593Smuzhiyun 		return ERR_PTR(-EINVAL);
869*4882a593Smuzhiyun 
870*4882a593Smuzhiyun         if (!sb_has_quota_active(sb, qid.type))
871*4882a593Smuzhiyun 		return ERR_PTR(-ESRCH);
872*4882a593Smuzhiyun we_slept:
873*4882a593Smuzhiyun 	spin_lock(&dq_list_lock);
874*4882a593Smuzhiyun 	spin_lock(&dq_state_lock);
875*4882a593Smuzhiyun 	if (!sb_has_quota_active(sb, qid.type)) {
876*4882a593Smuzhiyun 		spin_unlock(&dq_state_lock);
877*4882a593Smuzhiyun 		spin_unlock(&dq_list_lock);
878*4882a593Smuzhiyun 		dquot = ERR_PTR(-ESRCH);
879*4882a593Smuzhiyun 		goto out;
880*4882a593Smuzhiyun 	}
881*4882a593Smuzhiyun 	spin_unlock(&dq_state_lock);
882*4882a593Smuzhiyun 
883*4882a593Smuzhiyun 	dquot = find_dquot(hashent, sb, qid);
884*4882a593Smuzhiyun 	if (!dquot) {
885*4882a593Smuzhiyun 		if (!empty) {
886*4882a593Smuzhiyun 			spin_unlock(&dq_list_lock);
887*4882a593Smuzhiyun 			empty = get_empty_dquot(sb, qid.type);
888*4882a593Smuzhiyun 			if (!empty)
889*4882a593Smuzhiyun 				schedule();	/* Try to wait for a moment... */
890*4882a593Smuzhiyun 			goto we_slept;
891*4882a593Smuzhiyun 		}
892*4882a593Smuzhiyun 		dquot = empty;
893*4882a593Smuzhiyun 		empty = NULL;
894*4882a593Smuzhiyun 		dquot->dq_id = qid;
895*4882a593Smuzhiyun 		/* all dquots go on the inuse_list */
896*4882a593Smuzhiyun 		put_inuse(dquot);
897*4882a593Smuzhiyun 		/* hash it first so it can be found */
898*4882a593Smuzhiyun 		insert_dquot_hash(dquot);
899*4882a593Smuzhiyun 		spin_unlock(&dq_list_lock);
900*4882a593Smuzhiyun 		dqstats_inc(DQST_LOOKUPS);
901*4882a593Smuzhiyun 	} else {
902*4882a593Smuzhiyun 		if (!atomic_read(&dquot->dq_count))
903*4882a593Smuzhiyun 			remove_free_dquot(dquot);
904*4882a593Smuzhiyun 		atomic_inc(&dquot->dq_count);
905*4882a593Smuzhiyun 		spin_unlock(&dq_list_lock);
906*4882a593Smuzhiyun 		dqstats_inc(DQST_CACHE_HITS);
907*4882a593Smuzhiyun 		dqstats_inc(DQST_LOOKUPS);
908*4882a593Smuzhiyun 	}
909*4882a593Smuzhiyun 	/* Wait for dq_lock - after this we know that either dquot_release() is
910*4882a593Smuzhiyun 	 * already finished or it will be canceled due to dq_count > 1 test */
911*4882a593Smuzhiyun 	wait_on_dquot(dquot);
912*4882a593Smuzhiyun 	/* Read the dquot / allocate space in quota file */
913*4882a593Smuzhiyun 	if (!test_bit(DQ_ACTIVE_B, &dquot->dq_flags)) {
914*4882a593Smuzhiyun 		int err;
915*4882a593Smuzhiyun 
916*4882a593Smuzhiyun 		err = sb->dq_op->acquire_dquot(dquot);
917*4882a593Smuzhiyun 		if (err < 0) {
918*4882a593Smuzhiyun 			dqput(dquot);
919*4882a593Smuzhiyun 			dquot = ERR_PTR(err);
920*4882a593Smuzhiyun 			goto out;
921*4882a593Smuzhiyun 		}
922*4882a593Smuzhiyun 	}
923*4882a593Smuzhiyun 	/*
924*4882a593Smuzhiyun 	 * Make sure following reads see filled structure - paired with
925*4882a593Smuzhiyun 	 * smp_mb__before_atomic() in dquot_acquire().
926*4882a593Smuzhiyun 	 */
927*4882a593Smuzhiyun 	smp_rmb();
928*4882a593Smuzhiyun #ifdef CONFIG_QUOTA_DEBUG
929*4882a593Smuzhiyun 	BUG_ON(!dquot->dq_sb);	/* Has somebody invalidated entry under us? */
930*4882a593Smuzhiyun #endif
931*4882a593Smuzhiyun out:
932*4882a593Smuzhiyun 	if (empty)
933*4882a593Smuzhiyun 		do_destroy_dquot(empty);
934*4882a593Smuzhiyun 
935*4882a593Smuzhiyun 	return dquot;
936*4882a593Smuzhiyun }
937*4882a593Smuzhiyun EXPORT_SYMBOL(dqget);
938*4882a593Smuzhiyun 
i_dquot(struct inode * inode)939*4882a593Smuzhiyun static inline struct dquot **i_dquot(struct inode *inode)
940*4882a593Smuzhiyun {
941*4882a593Smuzhiyun 	return inode->i_sb->s_op->get_dquots(inode);
942*4882a593Smuzhiyun }
943*4882a593Smuzhiyun 
dqinit_needed(struct inode * inode,int type)944*4882a593Smuzhiyun static int dqinit_needed(struct inode *inode, int type)
945*4882a593Smuzhiyun {
946*4882a593Smuzhiyun 	struct dquot * const *dquots;
947*4882a593Smuzhiyun 	int cnt;
948*4882a593Smuzhiyun 
949*4882a593Smuzhiyun 	if (IS_NOQUOTA(inode))
950*4882a593Smuzhiyun 		return 0;
951*4882a593Smuzhiyun 
952*4882a593Smuzhiyun 	dquots = i_dquot(inode);
953*4882a593Smuzhiyun 	if (type != -1)
954*4882a593Smuzhiyun 		return !dquots[type];
955*4882a593Smuzhiyun 	for (cnt = 0; cnt < MAXQUOTAS; cnt++)
956*4882a593Smuzhiyun 		if (!dquots[cnt])
957*4882a593Smuzhiyun 			return 1;
958*4882a593Smuzhiyun 	return 0;
959*4882a593Smuzhiyun }
960*4882a593Smuzhiyun 
961*4882a593Smuzhiyun /* This routine is guarded by s_umount semaphore */
add_dquot_ref(struct super_block * sb,int type)962*4882a593Smuzhiyun static int add_dquot_ref(struct super_block *sb, int type)
963*4882a593Smuzhiyun {
964*4882a593Smuzhiyun 	struct inode *inode, *old_inode = NULL;
965*4882a593Smuzhiyun #ifdef CONFIG_QUOTA_DEBUG
966*4882a593Smuzhiyun 	int reserved = 0;
967*4882a593Smuzhiyun #endif
968*4882a593Smuzhiyun 	int err = 0;
969*4882a593Smuzhiyun 
970*4882a593Smuzhiyun 	spin_lock(&sb->s_inode_list_lock);
971*4882a593Smuzhiyun 	list_for_each_entry(inode, &sb->s_inodes, i_sb_list) {
972*4882a593Smuzhiyun 		spin_lock(&inode->i_lock);
973*4882a593Smuzhiyun 		if ((inode->i_state & (I_FREEING|I_WILL_FREE|I_NEW)) ||
974*4882a593Smuzhiyun 		    !atomic_read(&inode->i_writecount) ||
975*4882a593Smuzhiyun 		    !dqinit_needed(inode, type)) {
976*4882a593Smuzhiyun 			spin_unlock(&inode->i_lock);
977*4882a593Smuzhiyun 			continue;
978*4882a593Smuzhiyun 		}
979*4882a593Smuzhiyun 		__iget(inode);
980*4882a593Smuzhiyun 		spin_unlock(&inode->i_lock);
981*4882a593Smuzhiyun 		spin_unlock(&sb->s_inode_list_lock);
982*4882a593Smuzhiyun 
983*4882a593Smuzhiyun #ifdef CONFIG_QUOTA_DEBUG
984*4882a593Smuzhiyun 		if (unlikely(inode_get_rsv_space(inode) > 0))
985*4882a593Smuzhiyun 			reserved = 1;
986*4882a593Smuzhiyun #endif
987*4882a593Smuzhiyun 		iput(old_inode);
988*4882a593Smuzhiyun 		err = __dquot_initialize(inode, type);
989*4882a593Smuzhiyun 		if (err) {
990*4882a593Smuzhiyun 			iput(inode);
991*4882a593Smuzhiyun 			goto out;
992*4882a593Smuzhiyun 		}
993*4882a593Smuzhiyun 
994*4882a593Smuzhiyun 		/*
995*4882a593Smuzhiyun 		 * We hold a reference to 'inode' so it couldn't have been
996*4882a593Smuzhiyun 		 * removed from s_inodes list while we dropped the
997*4882a593Smuzhiyun 		 * s_inode_list_lock. We cannot iput the inode now as we can be
998*4882a593Smuzhiyun 		 * holding the last reference and we cannot iput it under
999*4882a593Smuzhiyun 		 * s_inode_list_lock. So we keep the reference and iput it
1000*4882a593Smuzhiyun 		 * later.
1001*4882a593Smuzhiyun 		 */
1002*4882a593Smuzhiyun 		old_inode = inode;
1003*4882a593Smuzhiyun 		cond_resched();
1004*4882a593Smuzhiyun 		spin_lock(&sb->s_inode_list_lock);
1005*4882a593Smuzhiyun 	}
1006*4882a593Smuzhiyun 	spin_unlock(&sb->s_inode_list_lock);
1007*4882a593Smuzhiyun 	iput(old_inode);
1008*4882a593Smuzhiyun out:
1009*4882a593Smuzhiyun #ifdef CONFIG_QUOTA_DEBUG
1010*4882a593Smuzhiyun 	if (reserved) {
1011*4882a593Smuzhiyun 		quota_error(sb, "Writes happened before quota was turned on "
1012*4882a593Smuzhiyun 			"thus quota information is probably inconsistent. "
1013*4882a593Smuzhiyun 			"Please run quotacheck(8)");
1014*4882a593Smuzhiyun 	}
1015*4882a593Smuzhiyun #endif
1016*4882a593Smuzhiyun 	return err;
1017*4882a593Smuzhiyun }
1018*4882a593Smuzhiyun 
1019*4882a593Smuzhiyun /*
1020*4882a593Smuzhiyun  * Remove references to dquots from inode and add dquot to list for freeing
1021*4882a593Smuzhiyun  * if we have the last reference to dquot
1022*4882a593Smuzhiyun  */
remove_inode_dquot_ref(struct inode * inode,int type,struct list_head * tofree_head)1023*4882a593Smuzhiyun static void remove_inode_dquot_ref(struct inode *inode, int type,
1024*4882a593Smuzhiyun 				   struct list_head *tofree_head)
1025*4882a593Smuzhiyun {
1026*4882a593Smuzhiyun 	struct dquot **dquots = i_dquot(inode);
1027*4882a593Smuzhiyun 	struct dquot *dquot = dquots[type];
1028*4882a593Smuzhiyun 
1029*4882a593Smuzhiyun 	if (!dquot)
1030*4882a593Smuzhiyun 		return;
1031*4882a593Smuzhiyun 
1032*4882a593Smuzhiyun 	dquots[type] = NULL;
1033*4882a593Smuzhiyun 	if (list_empty(&dquot->dq_free)) {
1034*4882a593Smuzhiyun 		/*
1035*4882a593Smuzhiyun 		 * The inode still has reference to dquot so it can't be in the
1036*4882a593Smuzhiyun 		 * free list
1037*4882a593Smuzhiyun 		 */
1038*4882a593Smuzhiyun 		spin_lock(&dq_list_lock);
1039*4882a593Smuzhiyun 		list_add(&dquot->dq_free, tofree_head);
1040*4882a593Smuzhiyun 		spin_unlock(&dq_list_lock);
1041*4882a593Smuzhiyun 	} else {
1042*4882a593Smuzhiyun 		/*
1043*4882a593Smuzhiyun 		 * Dquot is already in a list to put so we won't drop the last
1044*4882a593Smuzhiyun 		 * reference here.
1045*4882a593Smuzhiyun 		 */
1046*4882a593Smuzhiyun 		dqput(dquot);
1047*4882a593Smuzhiyun 	}
1048*4882a593Smuzhiyun }
1049*4882a593Smuzhiyun 
1050*4882a593Smuzhiyun /*
1051*4882a593Smuzhiyun  * Free list of dquots
1052*4882a593Smuzhiyun  * Dquots are removed from inodes and no new references can be got so we are
1053*4882a593Smuzhiyun  * the only ones holding reference
1054*4882a593Smuzhiyun  */
put_dquot_list(struct list_head * tofree_head)1055*4882a593Smuzhiyun static void put_dquot_list(struct list_head *tofree_head)
1056*4882a593Smuzhiyun {
1057*4882a593Smuzhiyun 	struct list_head *act_head;
1058*4882a593Smuzhiyun 	struct dquot *dquot;
1059*4882a593Smuzhiyun 
1060*4882a593Smuzhiyun 	act_head = tofree_head->next;
1061*4882a593Smuzhiyun 	while (act_head != tofree_head) {
1062*4882a593Smuzhiyun 		dquot = list_entry(act_head, struct dquot, dq_free);
1063*4882a593Smuzhiyun 		act_head = act_head->next;
1064*4882a593Smuzhiyun 		/* Remove dquot from the list so we won't have problems... */
1065*4882a593Smuzhiyun 		list_del_init(&dquot->dq_free);
1066*4882a593Smuzhiyun 		dqput(dquot);
1067*4882a593Smuzhiyun 	}
1068*4882a593Smuzhiyun }
1069*4882a593Smuzhiyun 
remove_dquot_ref(struct super_block * sb,int type,struct list_head * tofree_head)1070*4882a593Smuzhiyun static void remove_dquot_ref(struct super_block *sb, int type,
1071*4882a593Smuzhiyun 		struct list_head *tofree_head)
1072*4882a593Smuzhiyun {
1073*4882a593Smuzhiyun 	struct inode *inode;
1074*4882a593Smuzhiyun #ifdef CONFIG_QUOTA_DEBUG
1075*4882a593Smuzhiyun 	int reserved = 0;
1076*4882a593Smuzhiyun #endif
1077*4882a593Smuzhiyun 
1078*4882a593Smuzhiyun 	spin_lock(&sb->s_inode_list_lock);
1079*4882a593Smuzhiyun 	list_for_each_entry(inode, &sb->s_inodes, i_sb_list) {
1080*4882a593Smuzhiyun 		/*
1081*4882a593Smuzhiyun 		 *  We have to scan also I_NEW inodes because they can already
1082*4882a593Smuzhiyun 		 *  have quota pointer initialized. Luckily, we need to touch
1083*4882a593Smuzhiyun 		 *  only quota pointers and these have separate locking
1084*4882a593Smuzhiyun 		 *  (dq_data_lock).
1085*4882a593Smuzhiyun 		 */
1086*4882a593Smuzhiyun 		spin_lock(&dq_data_lock);
1087*4882a593Smuzhiyun 		if (!IS_NOQUOTA(inode)) {
1088*4882a593Smuzhiyun #ifdef CONFIG_QUOTA_DEBUG
1089*4882a593Smuzhiyun 			if (unlikely(inode_get_rsv_space(inode) > 0))
1090*4882a593Smuzhiyun 				reserved = 1;
1091*4882a593Smuzhiyun #endif
1092*4882a593Smuzhiyun 			remove_inode_dquot_ref(inode, type, tofree_head);
1093*4882a593Smuzhiyun 		}
1094*4882a593Smuzhiyun 		spin_unlock(&dq_data_lock);
1095*4882a593Smuzhiyun 	}
1096*4882a593Smuzhiyun 	spin_unlock(&sb->s_inode_list_lock);
1097*4882a593Smuzhiyun #ifdef CONFIG_QUOTA_DEBUG
1098*4882a593Smuzhiyun 	if (reserved) {
1099*4882a593Smuzhiyun 		printk(KERN_WARNING "VFS (%s): Writes happened after quota"
1100*4882a593Smuzhiyun 			" was disabled thus quota information is probably "
1101*4882a593Smuzhiyun 			"inconsistent. Please run quotacheck(8).\n", sb->s_id);
1102*4882a593Smuzhiyun 	}
1103*4882a593Smuzhiyun #endif
1104*4882a593Smuzhiyun }
1105*4882a593Smuzhiyun 
1106*4882a593Smuzhiyun /* Gather all references from inodes and drop them */
drop_dquot_ref(struct super_block * sb,int type)1107*4882a593Smuzhiyun static void drop_dquot_ref(struct super_block *sb, int type)
1108*4882a593Smuzhiyun {
1109*4882a593Smuzhiyun 	LIST_HEAD(tofree_head);
1110*4882a593Smuzhiyun 
1111*4882a593Smuzhiyun 	if (sb->dq_op) {
1112*4882a593Smuzhiyun 		remove_dquot_ref(sb, type, &tofree_head);
1113*4882a593Smuzhiyun 		synchronize_srcu(&dquot_srcu);
1114*4882a593Smuzhiyun 		put_dquot_list(&tofree_head);
1115*4882a593Smuzhiyun 	}
1116*4882a593Smuzhiyun }
1117*4882a593Smuzhiyun 
1118*4882a593Smuzhiyun static inline
dquot_free_reserved_space(struct dquot * dquot,qsize_t number)1119*4882a593Smuzhiyun void dquot_free_reserved_space(struct dquot *dquot, qsize_t number)
1120*4882a593Smuzhiyun {
1121*4882a593Smuzhiyun 	if (dquot->dq_dqb.dqb_rsvspace >= number)
1122*4882a593Smuzhiyun 		dquot->dq_dqb.dqb_rsvspace -= number;
1123*4882a593Smuzhiyun 	else {
1124*4882a593Smuzhiyun 		WARN_ON_ONCE(1);
1125*4882a593Smuzhiyun 		dquot->dq_dqb.dqb_rsvspace = 0;
1126*4882a593Smuzhiyun 	}
1127*4882a593Smuzhiyun 	if (dquot->dq_dqb.dqb_curspace + dquot->dq_dqb.dqb_rsvspace <=
1128*4882a593Smuzhiyun 	    dquot->dq_dqb.dqb_bsoftlimit)
1129*4882a593Smuzhiyun 		dquot->dq_dqb.dqb_btime = (time64_t) 0;
1130*4882a593Smuzhiyun 	clear_bit(DQ_BLKS_B, &dquot->dq_flags);
1131*4882a593Smuzhiyun }
1132*4882a593Smuzhiyun 
dquot_decr_inodes(struct dquot * dquot,qsize_t number)1133*4882a593Smuzhiyun static void dquot_decr_inodes(struct dquot *dquot, qsize_t number)
1134*4882a593Smuzhiyun {
1135*4882a593Smuzhiyun 	if (sb_dqopt(dquot->dq_sb)->flags & DQUOT_NEGATIVE_USAGE ||
1136*4882a593Smuzhiyun 	    dquot->dq_dqb.dqb_curinodes >= number)
1137*4882a593Smuzhiyun 		dquot->dq_dqb.dqb_curinodes -= number;
1138*4882a593Smuzhiyun 	else
1139*4882a593Smuzhiyun 		dquot->dq_dqb.dqb_curinodes = 0;
1140*4882a593Smuzhiyun 	if (dquot->dq_dqb.dqb_curinodes <= dquot->dq_dqb.dqb_isoftlimit)
1141*4882a593Smuzhiyun 		dquot->dq_dqb.dqb_itime = (time64_t) 0;
1142*4882a593Smuzhiyun 	clear_bit(DQ_INODES_B, &dquot->dq_flags);
1143*4882a593Smuzhiyun }
1144*4882a593Smuzhiyun 
dquot_decr_space(struct dquot * dquot,qsize_t number)1145*4882a593Smuzhiyun static void dquot_decr_space(struct dquot *dquot, qsize_t number)
1146*4882a593Smuzhiyun {
1147*4882a593Smuzhiyun 	if (sb_dqopt(dquot->dq_sb)->flags & DQUOT_NEGATIVE_USAGE ||
1148*4882a593Smuzhiyun 	    dquot->dq_dqb.dqb_curspace >= number)
1149*4882a593Smuzhiyun 		dquot->dq_dqb.dqb_curspace -= number;
1150*4882a593Smuzhiyun 	else
1151*4882a593Smuzhiyun 		dquot->dq_dqb.dqb_curspace = 0;
1152*4882a593Smuzhiyun 	if (dquot->dq_dqb.dqb_curspace + dquot->dq_dqb.dqb_rsvspace <=
1153*4882a593Smuzhiyun 	    dquot->dq_dqb.dqb_bsoftlimit)
1154*4882a593Smuzhiyun 		dquot->dq_dqb.dqb_btime = (time64_t) 0;
1155*4882a593Smuzhiyun 	clear_bit(DQ_BLKS_B, &dquot->dq_flags);
1156*4882a593Smuzhiyun }
1157*4882a593Smuzhiyun 
1158*4882a593Smuzhiyun struct dquot_warn {
1159*4882a593Smuzhiyun 	struct super_block *w_sb;
1160*4882a593Smuzhiyun 	struct kqid w_dq_id;
1161*4882a593Smuzhiyun 	short w_type;
1162*4882a593Smuzhiyun };
1163*4882a593Smuzhiyun 
warning_issued(struct dquot * dquot,const int warntype)1164*4882a593Smuzhiyun static int warning_issued(struct dquot *dquot, const int warntype)
1165*4882a593Smuzhiyun {
1166*4882a593Smuzhiyun 	int flag = (warntype == QUOTA_NL_BHARDWARN ||
1167*4882a593Smuzhiyun 		warntype == QUOTA_NL_BSOFTLONGWARN) ? DQ_BLKS_B :
1168*4882a593Smuzhiyun 		((warntype == QUOTA_NL_IHARDWARN ||
1169*4882a593Smuzhiyun 		warntype == QUOTA_NL_ISOFTLONGWARN) ? DQ_INODES_B : 0);
1170*4882a593Smuzhiyun 
1171*4882a593Smuzhiyun 	if (!flag)
1172*4882a593Smuzhiyun 		return 0;
1173*4882a593Smuzhiyun 	return test_and_set_bit(flag, &dquot->dq_flags);
1174*4882a593Smuzhiyun }
1175*4882a593Smuzhiyun 
1176*4882a593Smuzhiyun #ifdef CONFIG_PRINT_QUOTA_WARNING
1177*4882a593Smuzhiyun static int flag_print_warnings = 1;
1178*4882a593Smuzhiyun 
need_print_warning(struct dquot_warn * warn)1179*4882a593Smuzhiyun static int need_print_warning(struct dquot_warn *warn)
1180*4882a593Smuzhiyun {
1181*4882a593Smuzhiyun 	if (!flag_print_warnings)
1182*4882a593Smuzhiyun 		return 0;
1183*4882a593Smuzhiyun 
1184*4882a593Smuzhiyun 	switch (warn->w_dq_id.type) {
1185*4882a593Smuzhiyun 		case USRQUOTA:
1186*4882a593Smuzhiyun 			return uid_eq(current_fsuid(), warn->w_dq_id.uid);
1187*4882a593Smuzhiyun 		case GRPQUOTA:
1188*4882a593Smuzhiyun 			return in_group_p(warn->w_dq_id.gid);
1189*4882a593Smuzhiyun 		case PRJQUOTA:
1190*4882a593Smuzhiyun 			return 1;
1191*4882a593Smuzhiyun 	}
1192*4882a593Smuzhiyun 	return 0;
1193*4882a593Smuzhiyun }
1194*4882a593Smuzhiyun 
1195*4882a593Smuzhiyun /* Print warning to user which exceeded quota */
print_warning(struct dquot_warn * warn)1196*4882a593Smuzhiyun static void print_warning(struct dquot_warn *warn)
1197*4882a593Smuzhiyun {
1198*4882a593Smuzhiyun 	char *msg = NULL;
1199*4882a593Smuzhiyun 	struct tty_struct *tty;
1200*4882a593Smuzhiyun 	int warntype = warn->w_type;
1201*4882a593Smuzhiyun 
1202*4882a593Smuzhiyun 	if (warntype == QUOTA_NL_IHARDBELOW ||
1203*4882a593Smuzhiyun 	    warntype == QUOTA_NL_ISOFTBELOW ||
1204*4882a593Smuzhiyun 	    warntype == QUOTA_NL_BHARDBELOW ||
1205*4882a593Smuzhiyun 	    warntype == QUOTA_NL_BSOFTBELOW || !need_print_warning(warn))
1206*4882a593Smuzhiyun 		return;
1207*4882a593Smuzhiyun 
1208*4882a593Smuzhiyun 	tty = get_current_tty();
1209*4882a593Smuzhiyun 	if (!tty)
1210*4882a593Smuzhiyun 		return;
1211*4882a593Smuzhiyun 	tty_write_message(tty, warn->w_sb->s_id);
1212*4882a593Smuzhiyun 	if (warntype == QUOTA_NL_ISOFTWARN || warntype == QUOTA_NL_BSOFTWARN)
1213*4882a593Smuzhiyun 		tty_write_message(tty, ": warning, ");
1214*4882a593Smuzhiyun 	else
1215*4882a593Smuzhiyun 		tty_write_message(tty, ": write failed, ");
1216*4882a593Smuzhiyun 	tty_write_message(tty, quotatypes[warn->w_dq_id.type]);
1217*4882a593Smuzhiyun 	switch (warntype) {
1218*4882a593Smuzhiyun 		case QUOTA_NL_IHARDWARN:
1219*4882a593Smuzhiyun 			msg = " file limit reached.\r\n";
1220*4882a593Smuzhiyun 			break;
1221*4882a593Smuzhiyun 		case QUOTA_NL_ISOFTLONGWARN:
1222*4882a593Smuzhiyun 			msg = " file quota exceeded too long.\r\n";
1223*4882a593Smuzhiyun 			break;
1224*4882a593Smuzhiyun 		case QUOTA_NL_ISOFTWARN:
1225*4882a593Smuzhiyun 			msg = " file quota exceeded.\r\n";
1226*4882a593Smuzhiyun 			break;
1227*4882a593Smuzhiyun 		case QUOTA_NL_BHARDWARN:
1228*4882a593Smuzhiyun 			msg = " block limit reached.\r\n";
1229*4882a593Smuzhiyun 			break;
1230*4882a593Smuzhiyun 		case QUOTA_NL_BSOFTLONGWARN:
1231*4882a593Smuzhiyun 			msg = " block quota exceeded too long.\r\n";
1232*4882a593Smuzhiyun 			break;
1233*4882a593Smuzhiyun 		case QUOTA_NL_BSOFTWARN:
1234*4882a593Smuzhiyun 			msg = " block quota exceeded.\r\n";
1235*4882a593Smuzhiyun 			break;
1236*4882a593Smuzhiyun 	}
1237*4882a593Smuzhiyun 	tty_write_message(tty, msg);
1238*4882a593Smuzhiyun 	tty_kref_put(tty);
1239*4882a593Smuzhiyun }
1240*4882a593Smuzhiyun #endif
1241*4882a593Smuzhiyun 
prepare_warning(struct dquot_warn * warn,struct dquot * dquot,int warntype)1242*4882a593Smuzhiyun static void prepare_warning(struct dquot_warn *warn, struct dquot *dquot,
1243*4882a593Smuzhiyun 			    int warntype)
1244*4882a593Smuzhiyun {
1245*4882a593Smuzhiyun 	if (warning_issued(dquot, warntype))
1246*4882a593Smuzhiyun 		return;
1247*4882a593Smuzhiyun 	warn->w_type = warntype;
1248*4882a593Smuzhiyun 	warn->w_sb = dquot->dq_sb;
1249*4882a593Smuzhiyun 	warn->w_dq_id = dquot->dq_id;
1250*4882a593Smuzhiyun }
1251*4882a593Smuzhiyun 
1252*4882a593Smuzhiyun /*
1253*4882a593Smuzhiyun  * Write warnings to the console and send warning messages over netlink.
1254*4882a593Smuzhiyun  *
1255*4882a593Smuzhiyun  * Note that this function can call into tty and networking code.
1256*4882a593Smuzhiyun  */
flush_warnings(struct dquot_warn * warn)1257*4882a593Smuzhiyun static void flush_warnings(struct dquot_warn *warn)
1258*4882a593Smuzhiyun {
1259*4882a593Smuzhiyun 	int i;
1260*4882a593Smuzhiyun 
1261*4882a593Smuzhiyun 	for (i = 0; i < MAXQUOTAS; i++) {
1262*4882a593Smuzhiyun 		if (warn[i].w_type == QUOTA_NL_NOWARN)
1263*4882a593Smuzhiyun 			continue;
1264*4882a593Smuzhiyun #ifdef CONFIG_PRINT_QUOTA_WARNING
1265*4882a593Smuzhiyun 		print_warning(&warn[i]);
1266*4882a593Smuzhiyun #endif
1267*4882a593Smuzhiyun 		quota_send_warning(warn[i].w_dq_id,
1268*4882a593Smuzhiyun 				   warn[i].w_sb->s_dev, warn[i].w_type);
1269*4882a593Smuzhiyun 	}
1270*4882a593Smuzhiyun }
1271*4882a593Smuzhiyun 
ignore_hardlimit(struct dquot * dquot)1272*4882a593Smuzhiyun static int ignore_hardlimit(struct dquot *dquot)
1273*4882a593Smuzhiyun {
1274*4882a593Smuzhiyun 	struct mem_dqinfo *info = &sb_dqopt(dquot->dq_sb)->info[dquot->dq_id.type];
1275*4882a593Smuzhiyun 
1276*4882a593Smuzhiyun 	return capable(CAP_SYS_RESOURCE) &&
1277*4882a593Smuzhiyun 	       (info->dqi_format->qf_fmt_id != QFMT_VFS_OLD ||
1278*4882a593Smuzhiyun 		!(info->dqi_flags & DQF_ROOT_SQUASH));
1279*4882a593Smuzhiyun }
1280*4882a593Smuzhiyun 
dquot_add_inodes(struct dquot * dquot,qsize_t inodes,struct dquot_warn * warn)1281*4882a593Smuzhiyun static int dquot_add_inodes(struct dquot *dquot, qsize_t inodes,
1282*4882a593Smuzhiyun 			    struct dquot_warn *warn)
1283*4882a593Smuzhiyun {
1284*4882a593Smuzhiyun 	qsize_t newinodes;
1285*4882a593Smuzhiyun 	int ret = 0;
1286*4882a593Smuzhiyun 
1287*4882a593Smuzhiyun 	spin_lock(&dquot->dq_dqb_lock);
1288*4882a593Smuzhiyun 	newinodes = dquot->dq_dqb.dqb_curinodes + inodes;
1289*4882a593Smuzhiyun 	if (!sb_has_quota_limits_enabled(dquot->dq_sb, dquot->dq_id.type) ||
1290*4882a593Smuzhiyun 	    test_bit(DQ_FAKE_B, &dquot->dq_flags))
1291*4882a593Smuzhiyun 		goto add;
1292*4882a593Smuzhiyun 
1293*4882a593Smuzhiyun 	if (dquot->dq_dqb.dqb_ihardlimit &&
1294*4882a593Smuzhiyun 	    newinodes > dquot->dq_dqb.dqb_ihardlimit &&
1295*4882a593Smuzhiyun             !ignore_hardlimit(dquot)) {
1296*4882a593Smuzhiyun 		prepare_warning(warn, dquot, QUOTA_NL_IHARDWARN);
1297*4882a593Smuzhiyun 		ret = -EDQUOT;
1298*4882a593Smuzhiyun 		goto out;
1299*4882a593Smuzhiyun 	}
1300*4882a593Smuzhiyun 
1301*4882a593Smuzhiyun 	if (dquot->dq_dqb.dqb_isoftlimit &&
1302*4882a593Smuzhiyun 	    newinodes > dquot->dq_dqb.dqb_isoftlimit &&
1303*4882a593Smuzhiyun 	    dquot->dq_dqb.dqb_itime &&
1304*4882a593Smuzhiyun 	    ktime_get_real_seconds() >= dquot->dq_dqb.dqb_itime &&
1305*4882a593Smuzhiyun             !ignore_hardlimit(dquot)) {
1306*4882a593Smuzhiyun 		prepare_warning(warn, dquot, QUOTA_NL_ISOFTLONGWARN);
1307*4882a593Smuzhiyun 		ret = -EDQUOT;
1308*4882a593Smuzhiyun 		goto out;
1309*4882a593Smuzhiyun 	}
1310*4882a593Smuzhiyun 
1311*4882a593Smuzhiyun 	if (dquot->dq_dqb.dqb_isoftlimit &&
1312*4882a593Smuzhiyun 	    newinodes > dquot->dq_dqb.dqb_isoftlimit &&
1313*4882a593Smuzhiyun 	    dquot->dq_dqb.dqb_itime == 0) {
1314*4882a593Smuzhiyun 		prepare_warning(warn, dquot, QUOTA_NL_ISOFTWARN);
1315*4882a593Smuzhiyun 		dquot->dq_dqb.dqb_itime = ktime_get_real_seconds() +
1316*4882a593Smuzhiyun 		    sb_dqopt(dquot->dq_sb)->info[dquot->dq_id.type].dqi_igrace;
1317*4882a593Smuzhiyun 	}
1318*4882a593Smuzhiyun add:
1319*4882a593Smuzhiyun 	dquot->dq_dqb.dqb_curinodes = newinodes;
1320*4882a593Smuzhiyun 
1321*4882a593Smuzhiyun out:
1322*4882a593Smuzhiyun 	spin_unlock(&dquot->dq_dqb_lock);
1323*4882a593Smuzhiyun 	return ret;
1324*4882a593Smuzhiyun }
1325*4882a593Smuzhiyun 
dquot_add_space(struct dquot * dquot,qsize_t space,qsize_t rsv_space,unsigned int flags,struct dquot_warn * warn)1326*4882a593Smuzhiyun static int dquot_add_space(struct dquot *dquot, qsize_t space,
1327*4882a593Smuzhiyun 			   qsize_t rsv_space, unsigned int flags,
1328*4882a593Smuzhiyun 			   struct dquot_warn *warn)
1329*4882a593Smuzhiyun {
1330*4882a593Smuzhiyun 	qsize_t tspace;
1331*4882a593Smuzhiyun 	struct super_block *sb = dquot->dq_sb;
1332*4882a593Smuzhiyun 	int ret = 0;
1333*4882a593Smuzhiyun 
1334*4882a593Smuzhiyun 	spin_lock(&dquot->dq_dqb_lock);
1335*4882a593Smuzhiyun 	if (!sb_has_quota_limits_enabled(sb, dquot->dq_id.type) ||
1336*4882a593Smuzhiyun 	    test_bit(DQ_FAKE_B, &dquot->dq_flags))
1337*4882a593Smuzhiyun 		goto finish;
1338*4882a593Smuzhiyun 
1339*4882a593Smuzhiyun 	tspace = dquot->dq_dqb.dqb_curspace + dquot->dq_dqb.dqb_rsvspace
1340*4882a593Smuzhiyun 		+ space + rsv_space;
1341*4882a593Smuzhiyun 
1342*4882a593Smuzhiyun 	if (dquot->dq_dqb.dqb_bhardlimit &&
1343*4882a593Smuzhiyun 	    tspace > dquot->dq_dqb.dqb_bhardlimit &&
1344*4882a593Smuzhiyun             !ignore_hardlimit(dquot)) {
1345*4882a593Smuzhiyun 		if (flags & DQUOT_SPACE_WARN)
1346*4882a593Smuzhiyun 			prepare_warning(warn, dquot, QUOTA_NL_BHARDWARN);
1347*4882a593Smuzhiyun 		ret = -EDQUOT;
1348*4882a593Smuzhiyun 		goto finish;
1349*4882a593Smuzhiyun 	}
1350*4882a593Smuzhiyun 
1351*4882a593Smuzhiyun 	if (dquot->dq_dqb.dqb_bsoftlimit &&
1352*4882a593Smuzhiyun 	    tspace > dquot->dq_dqb.dqb_bsoftlimit &&
1353*4882a593Smuzhiyun 	    dquot->dq_dqb.dqb_btime &&
1354*4882a593Smuzhiyun 	    ktime_get_real_seconds() >= dquot->dq_dqb.dqb_btime &&
1355*4882a593Smuzhiyun             !ignore_hardlimit(dquot)) {
1356*4882a593Smuzhiyun 		if (flags & DQUOT_SPACE_WARN)
1357*4882a593Smuzhiyun 			prepare_warning(warn, dquot, QUOTA_NL_BSOFTLONGWARN);
1358*4882a593Smuzhiyun 		ret = -EDQUOT;
1359*4882a593Smuzhiyun 		goto finish;
1360*4882a593Smuzhiyun 	}
1361*4882a593Smuzhiyun 
1362*4882a593Smuzhiyun 	if (dquot->dq_dqb.dqb_bsoftlimit &&
1363*4882a593Smuzhiyun 	    tspace > dquot->dq_dqb.dqb_bsoftlimit &&
1364*4882a593Smuzhiyun 	    dquot->dq_dqb.dqb_btime == 0) {
1365*4882a593Smuzhiyun 		if (flags & DQUOT_SPACE_WARN) {
1366*4882a593Smuzhiyun 			prepare_warning(warn, dquot, QUOTA_NL_BSOFTWARN);
1367*4882a593Smuzhiyun 			dquot->dq_dqb.dqb_btime = ktime_get_real_seconds() +
1368*4882a593Smuzhiyun 			    sb_dqopt(sb)->info[dquot->dq_id.type].dqi_bgrace;
1369*4882a593Smuzhiyun 		} else {
1370*4882a593Smuzhiyun 			/*
1371*4882a593Smuzhiyun 			 * We don't allow preallocation to exceed softlimit so exceeding will
1372*4882a593Smuzhiyun 			 * be always printed
1373*4882a593Smuzhiyun 			 */
1374*4882a593Smuzhiyun 			ret = -EDQUOT;
1375*4882a593Smuzhiyun 			goto finish;
1376*4882a593Smuzhiyun 		}
1377*4882a593Smuzhiyun 	}
1378*4882a593Smuzhiyun finish:
1379*4882a593Smuzhiyun 	/*
1380*4882a593Smuzhiyun 	 * We have to be careful and go through warning generation & grace time
1381*4882a593Smuzhiyun 	 * setting even if DQUOT_SPACE_NOFAIL is set. That's why we check it
1382*4882a593Smuzhiyun 	 * only here...
1383*4882a593Smuzhiyun 	 */
1384*4882a593Smuzhiyun 	if (flags & DQUOT_SPACE_NOFAIL)
1385*4882a593Smuzhiyun 		ret = 0;
1386*4882a593Smuzhiyun 	if (!ret) {
1387*4882a593Smuzhiyun 		dquot->dq_dqb.dqb_rsvspace += rsv_space;
1388*4882a593Smuzhiyun 		dquot->dq_dqb.dqb_curspace += space;
1389*4882a593Smuzhiyun 	}
1390*4882a593Smuzhiyun 	spin_unlock(&dquot->dq_dqb_lock);
1391*4882a593Smuzhiyun 	return ret;
1392*4882a593Smuzhiyun }
1393*4882a593Smuzhiyun 
info_idq_free(struct dquot * dquot,qsize_t inodes)1394*4882a593Smuzhiyun static int info_idq_free(struct dquot *dquot, qsize_t inodes)
1395*4882a593Smuzhiyun {
1396*4882a593Smuzhiyun 	qsize_t newinodes;
1397*4882a593Smuzhiyun 
1398*4882a593Smuzhiyun 	if (test_bit(DQ_FAKE_B, &dquot->dq_flags) ||
1399*4882a593Smuzhiyun 	    dquot->dq_dqb.dqb_curinodes <= dquot->dq_dqb.dqb_isoftlimit ||
1400*4882a593Smuzhiyun 	    !sb_has_quota_limits_enabled(dquot->dq_sb, dquot->dq_id.type))
1401*4882a593Smuzhiyun 		return QUOTA_NL_NOWARN;
1402*4882a593Smuzhiyun 
1403*4882a593Smuzhiyun 	newinodes = dquot->dq_dqb.dqb_curinodes - inodes;
1404*4882a593Smuzhiyun 	if (newinodes <= dquot->dq_dqb.dqb_isoftlimit)
1405*4882a593Smuzhiyun 		return QUOTA_NL_ISOFTBELOW;
1406*4882a593Smuzhiyun 	if (dquot->dq_dqb.dqb_curinodes >= dquot->dq_dqb.dqb_ihardlimit &&
1407*4882a593Smuzhiyun 	    newinodes < dquot->dq_dqb.dqb_ihardlimit)
1408*4882a593Smuzhiyun 		return QUOTA_NL_IHARDBELOW;
1409*4882a593Smuzhiyun 	return QUOTA_NL_NOWARN;
1410*4882a593Smuzhiyun }
1411*4882a593Smuzhiyun 
info_bdq_free(struct dquot * dquot,qsize_t space)1412*4882a593Smuzhiyun static int info_bdq_free(struct dquot *dquot, qsize_t space)
1413*4882a593Smuzhiyun {
1414*4882a593Smuzhiyun 	qsize_t tspace;
1415*4882a593Smuzhiyun 
1416*4882a593Smuzhiyun 	tspace = dquot->dq_dqb.dqb_curspace + dquot->dq_dqb.dqb_rsvspace;
1417*4882a593Smuzhiyun 
1418*4882a593Smuzhiyun 	if (test_bit(DQ_FAKE_B, &dquot->dq_flags) ||
1419*4882a593Smuzhiyun 	    tspace <= dquot->dq_dqb.dqb_bsoftlimit)
1420*4882a593Smuzhiyun 		return QUOTA_NL_NOWARN;
1421*4882a593Smuzhiyun 
1422*4882a593Smuzhiyun 	if (tspace - space <= dquot->dq_dqb.dqb_bsoftlimit)
1423*4882a593Smuzhiyun 		return QUOTA_NL_BSOFTBELOW;
1424*4882a593Smuzhiyun 	if (tspace >= dquot->dq_dqb.dqb_bhardlimit &&
1425*4882a593Smuzhiyun 	    tspace - space < dquot->dq_dqb.dqb_bhardlimit)
1426*4882a593Smuzhiyun 		return QUOTA_NL_BHARDBELOW;
1427*4882a593Smuzhiyun 	return QUOTA_NL_NOWARN;
1428*4882a593Smuzhiyun }
1429*4882a593Smuzhiyun 
dquot_active(const struct inode * inode)1430*4882a593Smuzhiyun static int dquot_active(const struct inode *inode)
1431*4882a593Smuzhiyun {
1432*4882a593Smuzhiyun 	struct super_block *sb = inode->i_sb;
1433*4882a593Smuzhiyun 
1434*4882a593Smuzhiyun 	if (IS_NOQUOTA(inode))
1435*4882a593Smuzhiyun 		return 0;
1436*4882a593Smuzhiyun 	return sb_any_quota_loaded(sb) & ~sb_any_quota_suspended(sb);
1437*4882a593Smuzhiyun }
1438*4882a593Smuzhiyun 
1439*4882a593Smuzhiyun /*
1440*4882a593Smuzhiyun  * Initialize quota pointers in inode
1441*4882a593Smuzhiyun  *
1442*4882a593Smuzhiyun  * It is better to call this function outside of any transaction as it
1443*4882a593Smuzhiyun  * might need a lot of space in journal for dquot structure allocation.
1444*4882a593Smuzhiyun  */
__dquot_initialize(struct inode * inode,int type)1445*4882a593Smuzhiyun static int __dquot_initialize(struct inode *inode, int type)
1446*4882a593Smuzhiyun {
1447*4882a593Smuzhiyun 	int cnt, init_needed = 0;
1448*4882a593Smuzhiyun 	struct dquot **dquots, *got[MAXQUOTAS] = {};
1449*4882a593Smuzhiyun 	struct super_block *sb = inode->i_sb;
1450*4882a593Smuzhiyun 	qsize_t rsv;
1451*4882a593Smuzhiyun 	int ret = 0;
1452*4882a593Smuzhiyun 
1453*4882a593Smuzhiyun 	if (!dquot_active(inode))
1454*4882a593Smuzhiyun 		return 0;
1455*4882a593Smuzhiyun 
1456*4882a593Smuzhiyun 	dquots = i_dquot(inode);
1457*4882a593Smuzhiyun 
1458*4882a593Smuzhiyun 	/* First get references to structures we might need. */
1459*4882a593Smuzhiyun 	for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1460*4882a593Smuzhiyun 		struct kqid qid;
1461*4882a593Smuzhiyun 		kprojid_t projid;
1462*4882a593Smuzhiyun 		int rc;
1463*4882a593Smuzhiyun 		struct dquot *dquot;
1464*4882a593Smuzhiyun 
1465*4882a593Smuzhiyun 		if (type != -1 && cnt != type)
1466*4882a593Smuzhiyun 			continue;
1467*4882a593Smuzhiyun 		/*
1468*4882a593Smuzhiyun 		 * The i_dquot should have been initialized in most cases,
1469*4882a593Smuzhiyun 		 * we check it without locking here to avoid unnecessary
1470*4882a593Smuzhiyun 		 * dqget()/dqput() calls.
1471*4882a593Smuzhiyun 		 */
1472*4882a593Smuzhiyun 		if (dquots[cnt])
1473*4882a593Smuzhiyun 			continue;
1474*4882a593Smuzhiyun 
1475*4882a593Smuzhiyun 		if (!sb_has_quota_active(sb, cnt))
1476*4882a593Smuzhiyun 			continue;
1477*4882a593Smuzhiyun 
1478*4882a593Smuzhiyun 		init_needed = 1;
1479*4882a593Smuzhiyun 
1480*4882a593Smuzhiyun 		switch (cnt) {
1481*4882a593Smuzhiyun 		case USRQUOTA:
1482*4882a593Smuzhiyun 			qid = make_kqid_uid(inode->i_uid);
1483*4882a593Smuzhiyun 			break;
1484*4882a593Smuzhiyun 		case GRPQUOTA:
1485*4882a593Smuzhiyun 			qid = make_kqid_gid(inode->i_gid);
1486*4882a593Smuzhiyun 			break;
1487*4882a593Smuzhiyun 		case PRJQUOTA:
1488*4882a593Smuzhiyun 			rc = inode->i_sb->dq_op->get_projid(inode, &projid);
1489*4882a593Smuzhiyun 			if (rc)
1490*4882a593Smuzhiyun 				continue;
1491*4882a593Smuzhiyun 			qid = make_kqid_projid(projid);
1492*4882a593Smuzhiyun 			break;
1493*4882a593Smuzhiyun 		}
1494*4882a593Smuzhiyun 		dquot = dqget(sb, qid);
1495*4882a593Smuzhiyun 		if (IS_ERR(dquot)) {
1496*4882a593Smuzhiyun 			/* We raced with somebody turning quotas off... */
1497*4882a593Smuzhiyun 			if (PTR_ERR(dquot) != -ESRCH) {
1498*4882a593Smuzhiyun 				ret = PTR_ERR(dquot);
1499*4882a593Smuzhiyun 				goto out_put;
1500*4882a593Smuzhiyun 			}
1501*4882a593Smuzhiyun 			dquot = NULL;
1502*4882a593Smuzhiyun 		}
1503*4882a593Smuzhiyun 		got[cnt] = dquot;
1504*4882a593Smuzhiyun 	}
1505*4882a593Smuzhiyun 
1506*4882a593Smuzhiyun 	/* All required i_dquot has been initialized */
1507*4882a593Smuzhiyun 	if (!init_needed)
1508*4882a593Smuzhiyun 		return 0;
1509*4882a593Smuzhiyun 
1510*4882a593Smuzhiyun 	spin_lock(&dq_data_lock);
1511*4882a593Smuzhiyun 	if (IS_NOQUOTA(inode))
1512*4882a593Smuzhiyun 		goto out_lock;
1513*4882a593Smuzhiyun 	for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1514*4882a593Smuzhiyun 		if (type != -1 && cnt != type)
1515*4882a593Smuzhiyun 			continue;
1516*4882a593Smuzhiyun 		/* Avoid races with quotaoff() */
1517*4882a593Smuzhiyun 		if (!sb_has_quota_active(sb, cnt))
1518*4882a593Smuzhiyun 			continue;
1519*4882a593Smuzhiyun 		/* We could race with quotaon or dqget() could have failed */
1520*4882a593Smuzhiyun 		if (!got[cnt])
1521*4882a593Smuzhiyun 			continue;
1522*4882a593Smuzhiyun 		if (!dquots[cnt]) {
1523*4882a593Smuzhiyun 			dquots[cnt] = got[cnt];
1524*4882a593Smuzhiyun 			got[cnt] = NULL;
1525*4882a593Smuzhiyun 			/*
1526*4882a593Smuzhiyun 			 * Make quota reservation system happy if someone
1527*4882a593Smuzhiyun 			 * did a write before quota was turned on
1528*4882a593Smuzhiyun 			 */
1529*4882a593Smuzhiyun 			rsv = inode_get_rsv_space(inode);
1530*4882a593Smuzhiyun 			if (unlikely(rsv)) {
1531*4882a593Smuzhiyun 				spin_lock(&inode->i_lock);
1532*4882a593Smuzhiyun 				/* Get reservation again under proper lock */
1533*4882a593Smuzhiyun 				rsv = __inode_get_rsv_space(inode);
1534*4882a593Smuzhiyun 				spin_lock(&dquots[cnt]->dq_dqb_lock);
1535*4882a593Smuzhiyun 				dquots[cnt]->dq_dqb.dqb_rsvspace += rsv;
1536*4882a593Smuzhiyun 				spin_unlock(&dquots[cnt]->dq_dqb_lock);
1537*4882a593Smuzhiyun 				spin_unlock(&inode->i_lock);
1538*4882a593Smuzhiyun 			}
1539*4882a593Smuzhiyun 		}
1540*4882a593Smuzhiyun 	}
1541*4882a593Smuzhiyun out_lock:
1542*4882a593Smuzhiyun 	spin_unlock(&dq_data_lock);
1543*4882a593Smuzhiyun out_put:
1544*4882a593Smuzhiyun 	/* Drop unused references */
1545*4882a593Smuzhiyun 	dqput_all(got);
1546*4882a593Smuzhiyun 
1547*4882a593Smuzhiyun 	return ret;
1548*4882a593Smuzhiyun }
1549*4882a593Smuzhiyun 
dquot_initialize(struct inode * inode)1550*4882a593Smuzhiyun int dquot_initialize(struct inode *inode)
1551*4882a593Smuzhiyun {
1552*4882a593Smuzhiyun 	return __dquot_initialize(inode, -1);
1553*4882a593Smuzhiyun }
1554*4882a593Smuzhiyun EXPORT_SYMBOL(dquot_initialize);
1555*4882a593Smuzhiyun 
dquot_initialize_needed(struct inode * inode)1556*4882a593Smuzhiyun bool dquot_initialize_needed(struct inode *inode)
1557*4882a593Smuzhiyun {
1558*4882a593Smuzhiyun 	struct dquot **dquots;
1559*4882a593Smuzhiyun 	int i;
1560*4882a593Smuzhiyun 
1561*4882a593Smuzhiyun 	if (!dquot_active(inode))
1562*4882a593Smuzhiyun 		return false;
1563*4882a593Smuzhiyun 
1564*4882a593Smuzhiyun 	dquots = i_dquot(inode);
1565*4882a593Smuzhiyun 	for (i = 0; i < MAXQUOTAS; i++)
1566*4882a593Smuzhiyun 		if (!dquots[i] && sb_has_quota_active(inode->i_sb, i))
1567*4882a593Smuzhiyun 			return true;
1568*4882a593Smuzhiyun 	return false;
1569*4882a593Smuzhiyun }
1570*4882a593Smuzhiyun EXPORT_SYMBOL(dquot_initialize_needed);
1571*4882a593Smuzhiyun 
1572*4882a593Smuzhiyun /*
1573*4882a593Smuzhiyun  * Release all quotas referenced by inode.
1574*4882a593Smuzhiyun  *
1575*4882a593Smuzhiyun  * This function only be called on inode free or converting
1576*4882a593Smuzhiyun  * a file to quota file, no other users for the i_dquot in
1577*4882a593Smuzhiyun  * both cases, so we needn't call synchronize_srcu() after
1578*4882a593Smuzhiyun  * clearing i_dquot.
1579*4882a593Smuzhiyun  */
__dquot_drop(struct inode * inode)1580*4882a593Smuzhiyun static void __dquot_drop(struct inode *inode)
1581*4882a593Smuzhiyun {
1582*4882a593Smuzhiyun 	int cnt;
1583*4882a593Smuzhiyun 	struct dquot **dquots = i_dquot(inode);
1584*4882a593Smuzhiyun 	struct dquot *put[MAXQUOTAS];
1585*4882a593Smuzhiyun 
1586*4882a593Smuzhiyun 	spin_lock(&dq_data_lock);
1587*4882a593Smuzhiyun 	for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1588*4882a593Smuzhiyun 		put[cnt] = dquots[cnt];
1589*4882a593Smuzhiyun 		dquots[cnt] = NULL;
1590*4882a593Smuzhiyun 	}
1591*4882a593Smuzhiyun 	spin_unlock(&dq_data_lock);
1592*4882a593Smuzhiyun 	dqput_all(put);
1593*4882a593Smuzhiyun }
1594*4882a593Smuzhiyun 
dquot_drop(struct inode * inode)1595*4882a593Smuzhiyun void dquot_drop(struct inode *inode)
1596*4882a593Smuzhiyun {
1597*4882a593Smuzhiyun 	struct dquot * const *dquots;
1598*4882a593Smuzhiyun 	int cnt;
1599*4882a593Smuzhiyun 
1600*4882a593Smuzhiyun 	if (IS_NOQUOTA(inode))
1601*4882a593Smuzhiyun 		return;
1602*4882a593Smuzhiyun 
1603*4882a593Smuzhiyun 	/*
1604*4882a593Smuzhiyun 	 * Test before calling to rule out calls from proc and such
1605*4882a593Smuzhiyun 	 * where we are not allowed to block. Note that this is
1606*4882a593Smuzhiyun 	 * actually reliable test even without the lock - the caller
1607*4882a593Smuzhiyun 	 * must assure that nobody can come after the DQUOT_DROP and
1608*4882a593Smuzhiyun 	 * add quota pointers back anyway.
1609*4882a593Smuzhiyun 	 */
1610*4882a593Smuzhiyun 	dquots = i_dquot(inode);
1611*4882a593Smuzhiyun 	for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1612*4882a593Smuzhiyun 		if (dquots[cnt])
1613*4882a593Smuzhiyun 			break;
1614*4882a593Smuzhiyun 	}
1615*4882a593Smuzhiyun 
1616*4882a593Smuzhiyun 	if (cnt < MAXQUOTAS)
1617*4882a593Smuzhiyun 		__dquot_drop(inode);
1618*4882a593Smuzhiyun }
1619*4882a593Smuzhiyun EXPORT_SYMBOL(dquot_drop);
1620*4882a593Smuzhiyun 
1621*4882a593Smuzhiyun /*
1622*4882a593Smuzhiyun  * inode_reserved_space is managed internally by quota, and protected by
1623*4882a593Smuzhiyun  * i_lock similar to i_blocks+i_bytes.
1624*4882a593Smuzhiyun  */
inode_reserved_space(struct inode * inode)1625*4882a593Smuzhiyun static qsize_t *inode_reserved_space(struct inode * inode)
1626*4882a593Smuzhiyun {
1627*4882a593Smuzhiyun 	/* Filesystem must explicitly define it's own method in order to use
1628*4882a593Smuzhiyun 	 * quota reservation interface */
1629*4882a593Smuzhiyun 	BUG_ON(!inode->i_sb->dq_op->get_reserved_space);
1630*4882a593Smuzhiyun 	return inode->i_sb->dq_op->get_reserved_space(inode);
1631*4882a593Smuzhiyun }
1632*4882a593Smuzhiyun 
__inode_get_rsv_space(struct inode * inode)1633*4882a593Smuzhiyun static qsize_t __inode_get_rsv_space(struct inode *inode)
1634*4882a593Smuzhiyun {
1635*4882a593Smuzhiyun 	if (!inode->i_sb->dq_op->get_reserved_space)
1636*4882a593Smuzhiyun 		return 0;
1637*4882a593Smuzhiyun 	return *inode_reserved_space(inode);
1638*4882a593Smuzhiyun }
1639*4882a593Smuzhiyun 
inode_get_rsv_space(struct inode * inode)1640*4882a593Smuzhiyun static qsize_t inode_get_rsv_space(struct inode *inode)
1641*4882a593Smuzhiyun {
1642*4882a593Smuzhiyun 	qsize_t ret;
1643*4882a593Smuzhiyun 
1644*4882a593Smuzhiyun 	if (!inode->i_sb->dq_op->get_reserved_space)
1645*4882a593Smuzhiyun 		return 0;
1646*4882a593Smuzhiyun 	spin_lock(&inode->i_lock);
1647*4882a593Smuzhiyun 	ret = __inode_get_rsv_space(inode);
1648*4882a593Smuzhiyun 	spin_unlock(&inode->i_lock);
1649*4882a593Smuzhiyun 	return ret;
1650*4882a593Smuzhiyun }
1651*4882a593Smuzhiyun 
1652*4882a593Smuzhiyun /*
1653*4882a593Smuzhiyun  * This functions updates i_blocks+i_bytes fields and quota information
1654*4882a593Smuzhiyun  * (together with appropriate checks).
1655*4882a593Smuzhiyun  *
1656*4882a593Smuzhiyun  * NOTE: We absolutely rely on the fact that caller dirties the inode
1657*4882a593Smuzhiyun  * (usually helpers in quotaops.h care about this) and holds a handle for
1658*4882a593Smuzhiyun  * the current transaction so that dquot write and inode write go into the
1659*4882a593Smuzhiyun  * same transaction.
1660*4882a593Smuzhiyun  */
1661*4882a593Smuzhiyun 
1662*4882a593Smuzhiyun /*
1663*4882a593Smuzhiyun  * This operation can block, but only after everything is updated
1664*4882a593Smuzhiyun  */
__dquot_alloc_space(struct inode * inode,qsize_t number,int flags)1665*4882a593Smuzhiyun int __dquot_alloc_space(struct inode *inode, qsize_t number, int flags)
1666*4882a593Smuzhiyun {
1667*4882a593Smuzhiyun 	int cnt, ret = 0, index;
1668*4882a593Smuzhiyun 	struct dquot_warn warn[MAXQUOTAS];
1669*4882a593Smuzhiyun 	int reserve = flags & DQUOT_SPACE_RESERVE;
1670*4882a593Smuzhiyun 	struct dquot **dquots;
1671*4882a593Smuzhiyun 
1672*4882a593Smuzhiyun 	if (!dquot_active(inode)) {
1673*4882a593Smuzhiyun 		if (reserve) {
1674*4882a593Smuzhiyun 			spin_lock(&inode->i_lock);
1675*4882a593Smuzhiyun 			*inode_reserved_space(inode) += number;
1676*4882a593Smuzhiyun 			spin_unlock(&inode->i_lock);
1677*4882a593Smuzhiyun 		} else {
1678*4882a593Smuzhiyun 			inode_add_bytes(inode, number);
1679*4882a593Smuzhiyun 		}
1680*4882a593Smuzhiyun 		goto out;
1681*4882a593Smuzhiyun 	}
1682*4882a593Smuzhiyun 
1683*4882a593Smuzhiyun 	for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1684*4882a593Smuzhiyun 		warn[cnt].w_type = QUOTA_NL_NOWARN;
1685*4882a593Smuzhiyun 
1686*4882a593Smuzhiyun 	dquots = i_dquot(inode);
1687*4882a593Smuzhiyun 	index = srcu_read_lock(&dquot_srcu);
1688*4882a593Smuzhiyun 	spin_lock(&inode->i_lock);
1689*4882a593Smuzhiyun 	for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1690*4882a593Smuzhiyun 		if (!dquots[cnt])
1691*4882a593Smuzhiyun 			continue;
1692*4882a593Smuzhiyun 		if (reserve) {
1693*4882a593Smuzhiyun 			ret = dquot_add_space(dquots[cnt], 0, number, flags,
1694*4882a593Smuzhiyun 					      &warn[cnt]);
1695*4882a593Smuzhiyun 		} else {
1696*4882a593Smuzhiyun 			ret = dquot_add_space(dquots[cnt], number, 0, flags,
1697*4882a593Smuzhiyun 					      &warn[cnt]);
1698*4882a593Smuzhiyun 		}
1699*4882a593Smuzhiyun 		if (ret) {
1700*4882a593Smuzhiyun 			/* Back out changes we already did */
1701*4882a593Smuzhiyun 			for (cnt--; cnt >= 0; cnt--) {
1702*4882a593Smuzhiyun 				if (!dquots[cnt])
1703*4882a593Smuzhiyun 					continue;
1704*4882a593Smuzhiyun 				spin_lock(&dquots[cnt]->dq_dqb_lock);
1705*4882a593Smuzhiyun 				if (reserve)
1706*4882a593Smuzhiyun 					dquot_free_reserved_space(dquots[cnt],
1707*4882a593Smuzhiyun 								  number);
1708*4882a593Smuzhiyun 				else
1709*4882a593Smuzhiyun 					dquot_decr_space(dquots[cnt], number);
1710*4882a593Smuzhiyun 				spin_unlock(&dquots[cnt]->dq_dqb_lock);
1711*4882a593Smuzhiyun 			}
1712*4882a593Smuzhiyun 			spin_unlock(&inode->i_lock);
1713*4882a593Smuzhiyun 			goto out_flush_warn;
1714*4882a593Smuzhiyun 		}
1715*4882a593Smuzhiyun 	}
1716*4882a593Smuzhiyun 	if (reserve)
1717*4882a593Smuzhiyun 		*inode_reserved_space(inode) += number;
1718*4882a593Smuzhiyun 	else
1719*4882a593Smuzhiyun 		__inode_add_bytes(inode, number);
1720*4882a593Smuzhiyun 	spin_unlock(&inode->i_lock);
1721*4882a593Smuzhiyun 
1722*4882a593Smuzhiyun 	if (reserve)
1723*4882a593Smuzhiyun 		goto out_flush_warn;
1724*4882a593Smuzhiyun 	mark_all_dquot_dirty(dquots);
1725*4882a593Smuzhiyun out_flush_warn:
1726*4882a593Smuzhiyun 	srcu_read_unlock(&dquot_srcu, index);
1727*4882a593Smuzhiyun 	flush_warnings(warn);
1728*4882a593Smuzhiyun out:
1729*4882a593Smuzhiyun 	return ret;
1730*4882a593Smuzhiyun }
1731*4882a593Smuzhiyun EXPORT_SYMBOL(__dquot_alloc_space);
1732*4882a593Smuzhiyun 
1733*4882a593Smuzhiyun /*
1734*4882a593Smuzhiyun  * This operation can block, but only after everything is updated
1735*4882a593Smuzhiyun  */
dquot_alloc_inode(struct inode * inode)1736*4882a593Smuzhiyun int dquot_alloc_inode(struct inode *inode)
1737*4882a593Smuzhiyun {
1738*4882a593Smuzhiyun 	int cnt, ret = 0, index;
1739*4882a593Smuzhiyun 	struct dquot_warn warn[MAXQUOTAS];
1740*4882a593Smuzhiyun 	struct dquot * const *dquots;
1741*4882a593Smuzhiyun 
1742*4882a593Smuzhiyun 	if (!dquot_active(inode))
1743*4882a593Smuzhiyun 		return 0;
1744*4882a593Smuzhiyun 	for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1745*4882a593Smuzhiyun 		warn[cnt].w_type = QUOTA_NL_NOWARN;
1746*4882a593Smuzhiyun 
1747*4882a593Smuzhiyun 	dquots = i_dquot(inode);
1748*4882a593Smuzhiyun 	index = srcu_read_lock(&dquot_srcu);
1749*4882a593Smuzhiyun 	spin_lock(&inode->i_lock);
1750*4882a593Smuzhiyun 	for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1751*4882a593Smuzhiyun 		if (!dquots[cnt])
1752*4882a593Smuzhiyun 			continue;
1753*4882a593Smuzhiyun 		ret = dquot_add_inodes(dquots[cnt], 1, &warn[cnt]);
1754*4882a593Smuzhiyun 		if (ret) {
1755*4882a593Smuzhiyun 			for (cnt--; cnt >= 0; cnt--) {
1756*4882a593Smuzhiyun 				if (!dquots[cnt])
1757*4882a593Smuzhiyun 					continue;
1758*4882a593Smuzhiyun 				/* Back out changes we already did */
1759*4882a593Smuzhiyun 				spin_lock(&dquots[cnt]->dq_dqb_lock);
1760*4882a593Smuzhiyun 				dquot_decr_inodes(dquots[cnt], 1);
1761*4882a593Smuzhiyun 				spin_unlock(&dquots[cnt]->dq_dqb_lock);
1762*4882a593Smuzhiyun 			}
1763*4882a593Smuzhiyun 			goto warn_put_all;
1764*4882a593Smuzhiyun 		}
1765*4882a593Smuzhiyun 	}
1766*4882a593Smuzhiyun 
1767*4882a593Smuzhiyun warn_put_all:
1768*4882a593Smuzhiyun 	spin_unlock(&inode->i_lock);
1769*4882a593Smuzhiyun 	if (ret == 0)
1770*4882a593Smuzhiyun 		mark_all_dquot_dirty(dquots);
1771*4882a593Smuzhiyun 	srcu_read_unlock(&dquot_srcu, index);
1772*4882a593Smuzhiyun 	flush_warnings(warn);
1773*4882a593Smuzhiyun 	return ret;
1774*4882a593Smuzhiyun }
1775*4882a593Smuzhiyun EXPORT_SYMBOL(dquot_alloc_inode);
1776*4882a593Smuzhiyun 
1777*4882a593Smuzhiyun /*
1778*4882a593Smuzhiyun  * Convert in-memory reserved quotas to real consumed quotas
1779*4882a593Smuzhiyun  */
dquot_claim_space_nodirty(struct inode * inode,qsize_t number)1780*4882a593Smuzhiyun int dquot_claim_space_nodirty(struct inode *inode, qsize_t number)
1781*4882a593Smuzhiyun {
1782*4882a593Smuzhiyun 	struct dquot **dquots;
1783*4882a593Smuzhiyun 	int cnt, index;
1784*4882a593Smuzhiyun 
1785*4882a593Smuzhiyun 	if (!dquot_active(inode)) {
1786*4882a593Smuzhiyun 		spin_lock(&inode->i_lock);
1787*4882a593Smuzhiyun 		*inode_reserved_space(inode) -= number;
1788*4882a593Smuzhiyun 		__inode_add_bytes(inode, number);
1789*4882a593Smuzhiyun 		spin_unlock(&inode->i_lock);
1790*4882a593Smuzhiyun 		return 0;
1791*4882a593Smuzhiyun 	}
1792*4882a593Smuzhiyun 
1793*4882a593Smuzhiyun 	dquots = i_dquot(inode);
1794*4882a593Smuzhiyun 	index = srcu_read_lock(&dquot_srcu);
1795*4882a593Smuzhiyun 	spin_lock(&inode->i_lock);
1796*4882a593Smuzhiyun 	/* Claim reserved quotas to allocated quotas */
1797*4882a593Smuzhiyun 	for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1798*4882a593Smuzhiyun 		if (dquots[cnt]) {
1799*4882a593Smuzhiyun 			struct dquot *dquot = dquots[cnt];
1800*4882a593Smuzhiyun 
1801*4882a593Smuzhiyun 			spin_lock(&dquot->dq_dqb_lock);
1802*4882a593Smuzhiyun 			if (WARN_ON_ONCE(dquot->dq_dqb.dqb_rsvspace < number))
1803*4882a593Smuzhiyun 				number = dquot->dq_dqb.dqb_rsvspace;
1804*4882a593Smuzhiyun 			dquot->dq_dqb.dqb_curspace += number;
1805*4882a593Smuzhiyun 			dquot->dq_dqb.dqb_rsvspace -= number;
1806*4882a593Smuzhiyun 			spin_unlock(&dquot->dq_dqb_lock);
1807*4882a593Smuzhiyun 		}
1808*4882a593Smuzhiyun 	}
1809*4882a593Smuzhiyun 	/* Update inode bytes */
1810*4882a593Smuzhiyun 	*inode_reserved_space(inode) -= number;
1811*4882a593Smuzhiyun 	__inode_add_bytes(inode, number);
1812*4882a593Smuzhiyun 	spin_unlock(&inode->i_lock);
1813*4882a593Smuzhiyun 	mark_all_dquot_dirty(dquots);
1814*4882a593Smuzhiyun 	srcu_read_unlock(&dquot_srcu, index);
1815*4882a593Smuzhiyun 	return 0;
1816*4882a593Smuzhiyun }
1817*4882a593Smuzhiyun EXPORT_SYMBOL(dquot_claim_space_nodirty);
1818*4882a593Smuzhiyun 
1819*4882a593Smuzhiyun /*
1820*4882a593Smuzhiyun  * Convert allocated space back to in-memory reserved quotas
1821*4882a593Smuzhiyun  */
dquot_reclaim_space_nodirty(struct inode * inode,qsize_t number)1822*4882a593Smuzhiyun void dquot_reclaim_space_nodirty(struct inode *inode, qsize_t number)
1823*4882a593Smuzhiyun {
1824*4882a593Smuzhiyun 	struct dquot **dquots;
1825*4882a593Smuzhiyun 	int cnt, index;
1826*4882a593Smuzhiyun 
1827*4882a593Smuzhiyun 	if (!dquot_active(inode)) {
1828*4882a593Smuzhiyun 		spin_lock(&inode->i_lock);
1829*4882a593Smuzhiyun 		*inode_reserved_space(inode) += number;
1830*4882a593Smuzhiyun 		__inode_sub_bytes(inode, number);
1831*4882a593Smuzhiyun 		spin_unlock(&inode->i_lock);
1832*4882a593Smuzhiyun 		return;
1833*4882a593Smuzhiyun 	}
1834*4882a593Smuzhiyun 
1835*4882a593Smuzhiyun 	dquots = i_dquot(inode);
1836*4882a593Smuzhiyun 	index = srcu_read_lock(&dquot_srcu);
1837*4882a593Smuzhiyun 	spin_lock(&inode->i_lock);
1838*4882a593Smuzhiyun 	/* Claim reserved quotas to allocated quotas */
1839*4882a593Smuzhiyun 	for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1840*4882a593Smuzhiyun 		if (dquots[cnt]) {
1841*4882a593Smuzhiyun 			struct dquot *dquot = dquots[cnt];
1842*4882a593Smuzhiyun 
1843*4882a593Smuzhiyun 			spin_lock(&dquot->dq_dqb_lock);
1844*4882a593Smuzhiyun 			if (WARN_ON_ONCE(dquot->dq_dqb.dqb_curspace < number))
1845*4882a593Smuzhiyun 				number = dquot->dq_dqb.dqb_curspace;
1846*4882a593Smuzhiyun 			dquot->dq_dqb.dqb_rsvspace += number;
1847*4882a593Smuzhiyun 			dquot->dq_dqb.dqb_curspace -= number;
1848*4882a593Smuzhiyun 			spin_unlock(&dquot->dq_dqb_lock);
1849*4882a593Smuzhiyun 		}
1850*4882a593Smuzhiyun 	}
1851*4882a593Smuzhiyun 	/* Update inode bytes */
1852*4882a593Smuzhiyun 	*inode_reserved_space(inode) += number;
1853*4882a593Smuzhiyun 	__inode_sub_bytes(inode, number);
1854*4882a593Smuzhiyun 	spin_unlock(&inode->i_lock);
1855*4882a593Smuzhiyun 	mark_all_dquot_dirty(dquots);
1856*4882a593Smuzhiyun 	srcu_read_unlock(&dquot_srcu, index);
1857*4882a593Smuzhiyun 	return;
1858*4882a593Smuzhiyun }
1859*4882a593Smuzhiyun EXPORT_SYMBOL(dquot_reclaim_space_nodirty);
1860*4882a593Smuzhiyun 
1861*4882a593Smuzhiyun /*
1862*4882a593Smuzhiyun  * This operation can block, but only after everything is updated
1863*4882a593Smuzhiyun  */
__dquot_free_space(struct inode * inode,qsize_t number,int flags)1864*4882a593Smuzhiyun void __dquot_free_space(struct inode *inode, qsize_t number, int flags)
1865*4882a593Smuzhiyun {
1866*4882a593Smuzhiyun 	unsigned int cnt;
1867*4882a593Smuzhiyun 	struct dquot_warn warn[MAXQUOTAS];
1868*4882a593Smuzhiyun 	struct dquot **dquots;
1869*4882a593Smuzhiyun 	int reserve = flags & DQUOT_SPACE_RESERVE, index;
1870*4882a593Smuzhiyun 
1871*4882a593Smuzhiyun 	if (!dquot_active(inode)) {
1872*4882a593Smuzhiyun 		if (reserve) {
1873*4882a593Smuzhiyun 			spin_lock(&inode->i_lock);
1874*4882a593Smuzhiyun 			*inode_reserved_space(inode) -= number;
1875*4882a593Smuzhiyun 			spin_unlock(&inode->i_lock);
1876*4882a593Smuzhiyun 		} else {
1877*4882a593Smuzhiyun 			inode_sub_bytes(inode, number);
1878*4882a593Smuzhiyun 		}
1879*4882a593Smuzhiyun 		return;
1880*4882a593Smuzhiyun 	}
1881*4882a593Smuzhiyun 
1882*4882a593Smuzhiyun 	dquots = i_dquot(inode);
1883*4882a593Smuzhiyun 	index = srcu_read_lock(&dquot_srcu);
1884*4882a593Smuzhiyun 	spin_lock(&inode->i_lock);
1885*4882a593Smuzhiyun 	for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1886*4882a593Smuzhiyun 		int wtype;
1887*4882a593Smuzhiyun 
1888*4882a593Smuzhiyun 		warn[cnt].w_type = QUOTA_NL_NOWARN;
1889*4882a593Smuzhiyun 		if (!dquots[cnt])
1890*4882a593Smuzhiyun 			continue;
1891*4882a593Smuzhiyun 		spin_lock(&dquots[cnt]->dq_dqb_lock);
1892*4882a593Smuzhiyun 		wtype = info_bdq_free(dquots[cnt], number);
1893*4882a593Smuzhiyun 		if (wtype != QUOTA_NL_NOWARN)
1894*4882a593Smuzhiyun 			prepare_warning(&warn[cnt], dquots[cnt], wtype);
1895*4882a593Smuzhiyun 		if (reserve)
1896*4882a593Smuzhiyun 			dquot_free_reserved_space(dquots[cnt], number);
1897*4882a593Smuzhiyun 		else
1898*4882a593Smuzhiyun 			dquot_decr_space(dquots[cnt], number);
1899*4882a593Smuzhiyun 		spin_unlock(&dquots[cnt]->dq_dqb_lock);
1900*4882a593Smuzhiyun 	}
1901*4882a593Smuzhiyun 	if (reserve)
1902*4882a593Smuzhiyun 		*inode_reserved_space(inode) -= number;
1903*4882a593Smuzhiyun 	else
1904*4882a593Smuzhiyun 		__inode_sub_bytes(inode, number);
1905*4882a593Smuzhiyun 	spin_unlock(&inode->i_lock);
1906*4882a593Smuzhiyun 
1907*4882a593Smuzhiyun 	if (reserve)
1908*4882a593Smuzhiyun 		goto out_unlock;
1909*4882a593Smuzhiyun 	mark_all_dquot_dirty(dquots);
1910*4882a593Smuzhiyun out_unlock:
1911*4882a593Smuzhiyun 	srcu_read_unlock(&dquot_srcu, index);
1912*4882a593Smuzhiyun 	flush_warnings(warn);
1913*4882a593Smuzhiyun }
1914*4882a593Smuzhiyun EXPORT_SYMBOL(__dquot_free_space);
1915*4882a593Smuzhiyun 
1916*4882a593Smuzhiyun /*
1917*4882a593Smuzhiyun  * This operation can block, but only after everything is updated
1918*4882a593Smuzhiyun  */
dquot_free_inode(struct inode * inode)1919*4882a593Smuzhiyun void dquot_free_inode(struct inode *inode)
1920*4882a593Smuzhiyun {
1921*4882a593Smuzhiyun 	unsigned int cnt;
1922*4882a593Smuzhiyun 	struct dquot_warn warn[MAXQUOTAS];
1923*4882a593Smuzhiyun 	struct dquot * const *dquots;
1924*4882a593Smuzhiyun 	int index;
1925*4882a593Smuzhiyun 
1926*4882a593Smuzhiyun 	if (!dquot_active(inode))
1927*4882a593Smuzhiyun 		return;
1928*4882a593Smuzhiyun 
1929*4882a593Smuzhiyun 	dquots = i_dquot(inode);
1930*4882a593Smuzhiyun 	index = srcu_read_lock(&dquot_srcu);
1931*4882a593Smuzhiyun 	spin_lock(&inode->i_lock);
1932*4882a593Smuzhiyun 	for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1933*4882a593Smuzhiyun 		int wtype;
1934*4882a593Smuzhiyun 
1935*4882a593Smuzhiyun 		warn[cnt].w_type = QUOTA_NL_NOWARN;
1936*4882a593Smuzhiyun 		if (!dquots[cnt])
1937*4882a593Smuzhiyun 			continue;
1938*4882a593Smuzhiyun 		spin_lock(&dquots[cnt]->dq_dqb_lock);
1939*4882a593Smuzhiyun 		wtype = info_idq_free(dquots[cnt], 1);
1940*4882a593Smuzhiyun 		if (wtype != QUOTA_NL_NOWARN)
1941*4882a593Smuzhiyun 			prepare_warning(&warn[cnt], dquots[cnt], wtype);
1942*4882a593Smuzhiyun 		dquot_decr_inodes(dquots[cnt], 1);
1943*4882a593Smuzhiyun 		spin_unlock(&dquots[cnt]->dq_dqb_lock);
1944*4882a593Smuzhiyun 	}
1945*4882a593Smuzhiyun 	spin_unlock(&inode->i_lock);
1946*4882a593Smuzhiyun 	mark_all_dquot_dirty(dquots);
1947*4882a593Smuzhiyun 	srcu_read_unlock(&dquot_srcu, index);
1948*4882a593Smuzhiyun 	flush_warnings(warn);
1949*4882a593Smuzhiyun }
1950*4882a593Smuzhiyun EXPORT_SYMBOL(dquot_free_inode);
1951*4882a593Smuzhiyun 
1952*4882a593Smuzhiyun /*
1953*4882a593Smuzhiyun  * Transfer the number of inode and blocks from one diskquota to an other.
1954*4882a593Smuzhiyun  * On success, dquot references in transfer_to are consumed and references
1955*4882a593Smuzhiyun  * to original dquots that need to be released are placed there. On failure,
1956*4882a593Smuzhiyun  * references are kept untouched.
1957*4882a593Smuzhiyun  *
1958*4882a593Smuzhiyun  * This operation can block, but only after everything is updated
1959*4882a593Smuzhiyun  * A transaction must be started when entering this function.
1960*4882a593Smuzhiyun  *
1961*4882a593Smuzhiyun  * We are holding reference on transfer_from & transfer_to, no need to
1962*4882a593Smuzhiyun  * protect them by srcu_read_lock().
1963*4882a593Smuzhiyun  */
__dquot_transfer(struct inode * inode,struct dquot ** transfer_to)1964*4882a593Smuzhiyun int __dquot_transfer(struct inode *inode, struct dquot **transfer_to)
1965*4882a593Smuzhiyun {
1966*4882a593Smuzhiyun 	qsize_t cur_space;
1967*4882a593Smuzhiyun 	qsize_t rsv_space = 0;
1968*4882a593Smuzhiyun 	qsize_t inode_usage = 1;
1969*4882a593Smuzhiyun 	struct dquot *transfer_from[MAXQUOTAS] = {};
1970*4882a593Smuzhiyun 	int cnt, ret = 0;
1971*4882a593Smuzhiyun 	char is_valid[MAXQUOTAS] = {};
1972*4882a593Smuzhiyun 	struct dquot_warn warn_to[MAXQUOTAS];
1973*4882a593Smuzhiyun 	struct dquot_warn warn_from_inodes[MAXQUOTAS];
1974*4882a593Smuzhiyun 	struct dquot_warn warn_from_space[MAXQUOTAS];
1975*4882a593Smuzhiyun 
1976*4882a593Smuzhiyun 	if (IS_NOQUOTA(inode))
1977*4882a593Smuzhiyun 		return 0;
1978*4882a593Smuzhiyun 
1979*4882a593Smuzhiyun 	if (inode->i_sb->dq_op->get_inode_usage) {
1980*4882a593Smuzhiyun 		ret = inode->i_sb->dq_op->get_inode_usage(inode, &inode_usage);
1981*4882a593Smuzhiyun 		if (ret)
1982*4882a593Smuzhiyun 			return ret;
1983*4882a593Smuzhiyun 	}
1984*4882a593Smuzhiyun 
1985*4882a593Smuzhiyun 	/* Initialize the arrays */
1986*4882a593Smuzhiyun 	for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1987*4882a593Smuzhiyun 		warn_to[cnt].w_type = QUOTA_NL_NOWARN;
1988*4882a593Smuzhiyun 		warn_from_inodes[cnt].w_type = QUOTA_NL_NOWARN;
1989*4882a593Smuzhiyun 		warn_from_space[cnt].w_type = QUOTA_NL_NOWARN;
1990*4882a593Smuzhiyun 	}
1991*4882a593Smuzhiyun 
1992*4882a593Smuzhiyun 	spin_lock(&dq_data_lock);
1993*4882a593Smuzhiyun 	spin_lock(&inode->i_lock);
1994*4882a593Smuzhiyun 	if (IS_NOQUOTA(inode)) {	/* File without quota accounting? */
1995*4882a593Smuzhiyun 		spin_unlock(&inode->i_lock);
1996*4882a593Smuzhiyun 		spin_unlock(&dq_data_lock);
1997*4882a593Smuzhiyun 		return 0;
1998*4882a593Smuzhiyun 	}
1999*4882a593Smuzhiyun 	cur_space = __inode_get_bytes(inode);
2000*4882a593Smuzhiyun 	rsv_space = __inode_get_rsv_space(inode);
2001*4882a593Smuzhiyun 	/*
2002*4882a593Smuzhiyun 	 * Build the transfer_from list, check limits, and update usage in
2003*4882a593Smuzhiyun 	 * the target structures.
2004*4882a593Smuzhiyun 	 */
2005*4882a593Smuzhiyun 	for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
2006*4882a593Smuzhiyun 		/*
2007*4882a593Smuzhiyun 		 * Skip changes for same uid or gid or for turned off quota-type.
2008*4882a593Smuzhiyun 		 */
2009*4882a593Smuzhiyun 		if (!transfer_to[cnt])
2010*4882a593Smuzhiyun 			continue;
2011*4882a593Smuzhiyun 		/* Avoid races with quotaoff() */
2012*4882a593Smuzhiyun 		if (!sb_has_quota_active(inode->i_sb, cnt))
2013*4882a593Smuzhiyun 			continue;
2014*4882a593Smuzhiyun 		is_valid[cnt] = 1;
2015*4882a593Smuzhiyun 		transfer_from[cnt] = i_dquot(inode)[cnt];
2016*4882a593Smuzhiyun 		ret = dquot_add_inodes(transfer_to[cnt], inode_usage,
2017*4882a593Smuzhiyun 				       &warn_to[cnt]);
2018*4882a593Smuzhiyun 		if (ret)
2019*4882a593Smuzhiyun 			goto over_quota;
2020*4882a593Smuzhiyun 		ret = dquot_add_space(transfer_to[cnt], cur_space, rsv_space,
2021*4882a593Smuzhiyun 				      DQUOT_SPACE_WARN, &warn_to[cnt]);
2022*4882a593Smuzhiyun 		if (ret) {
2023*4882a593Smuzhiyun 			spin_lock(&transfer_to[cnt]->dq_dqb_lock);
2024*4882a593Smuzhiyun 			dquot_decr_inodes(transfer_to[cnt], inode_usage);
2025*4882a593Smuzhiyun 			spin_unlock(&transfer_to[cnt]->dq_dqb_lock);
2026*4882a593Smuzhiyun 			goto over_quota;
2027*4882a593Smuzhiyun 		}
2028*4882a593Smuzhiyun 	}
2029*4882a593Smuzhiyun 
2030*4882a593Smuzhiyun 	/* Decrease usage for source structures and update quota pointers */
2031*4882a593Smuzhiyun 	for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
2032*4882a593Smuzhiyun 		if (!is_valid[cnt])
2033*4882a593Smuzhiyun 			continue;
2034*4882a593Smuzhiyun 		/* Due to IO error we might not have transfer_from[] structure */
2035*4882a593Smuzhiyun 		if (transfer_from[cnt]) {
2036*4882a593Smuzhiyun 			int wtype;
2037*4882a593Smuzhiyun 
2038*4882a593Smuzhiyun 			spin_lock(&transfer_from[cnt]->dq_dqb_lock);
2039*4882a593Smuzhiyun 			wtype = info_idq_free(transfer_from[cnt], inode_usage);
2040*4882a593Smuzhiyun 			if (wtype != QUOTA_NL_NOWARN)
2041*4882a593Smuzhiyun 				prepare_warning(&warn_from_inodes[cnt],
2042*4882a593Smuzhiyun 						transfer_from[cnt], wtype);
2043*4882a593Smuzhiyun 			wtype = info_bdq_free(transfer_from[cnt],
2044*4882a593Smuzhiyun 					      cur_space + rsv_space);
2045*4882a593Smuzhiyun 			if (wtype != QUOTA_NL_NOWARN)
2046*4882a593Smuzhiyun 				prepare_warning(&warn_from_space[cnt],
2047*4882a593Smuzhiyun 						transfer_from[cnt], wtype);
2048*4882a593Smuzhiyun 			dquot_decr_inodes(transfer_from[cnt], inode_usage);
2049*4882a593Smuzhiyun 			dquot_decr_space(transfer_from[cnt], cur_space);
2050*4882a593Smuzhiyun 			dquot_free_reserved_space(transfer_from[cnt],
2051*4882a593Smuzhiyun 						  rsv_space);
2052*4882a593Smuzhiyun 			spin_unlock(&transfer_from[cnt]->dq_dqb_lock);
2053*4882a593Smuzhiyun 		}
2054*4882a593Smuzhiyun 		i_dquot(inode)[cnt] = transfer_to[cnt];
2055*4882a593Smuzhiyun 	}
2056*4882a593Smuzhiyun 	spin_unlock(&inode->i_lock);
2057*4882a593Smuzhiyun 	spin_unlock(&dq_data_lock);
2058*4882a593Smuzhiyun 
2059*4882a593Smuzhiyun 	mark_all_dquot_dirty(transfer_from);
2060*4882a593Smuzhiyun 	mark_all_dquot_dirty(transfer_to);
2061*4882a593Smuzhiyun 	flush_warnings(warn_to);
2062*4882a593Smuzhiyun 	flush_warnings(warn_from_inodes);
2063*4882a593Smuzhiyun 	flush_warnings(warn_from_space);
2064*4882a593Smuzhiyun 	/* Pass back references to put */
2065*4882a593Smuzhiyun 	for (cnt = 0; cnt < MAXQUOTAS; cnt++)
2066*4882a593Smuzhiyun 		if (is_valid[cnt])
2067*4882a593Smuzhiyun 			transfer_to[cnt] = transfer_from[cnt];
2068*4882a593Smuzhiyun 	return 0;
2069*4882a593Smuzhiyun over_quota:
2070*4882a593Smuzhiyun 	/* Back out changes we already did */
2071*4882a593Smuzhiyun 	for (cnt--; cnt >= 0; cnt--) {
2072*4882a593Smuzhiyun 		if (!is_valid[cnt])
2073*4882a593Smuzhiyun 			continue;
2074*4882a593Smuzhiyun 		spin_lock(&transfer_to[cnt]->dq_dqb_lock);
2075*4882a593Smuzhiyun 		dquot_decr_inodes(transfer_to[cnt], inode_usage);
2076*4882a593Smuzhiyun 		dquot_decr_space(transfer_to[cnt], cur_space);
2077*4882a593Smuzhiyun 		dquot_free_reserved_space(transfer_to[cnt], rsv_space);
2078*4882a593Smuzhiyun 		spin_unlock(&transfer_to[cnt]->dq_dqb_lock);
2079*4882a593Smuzhiyun 	}
2080*4882a593Smuzhiyun 	spin_unlock(&inode->i_lock);
2081*4882a593Smuzhiyun 	spin_unlock(&dq_data_lock);
2082*4882a593Smuzhiyun 	flush_warnings(warn_to);
2083*4882a593Smuzhiyun 	return ret;
2084*4882a593Smuzhiyun }
2085*4882a593Smuzhiyun EXPORT_SYMBOL(__dquot_transfer);
2086*4882a593Smuzhiyun 
2087*4882a593Smuzhiyun /* Wrapper for transferring ownership of an inode for uid/gid only
2088*4882a593Smuzhiyun  * Called from FSXXX_setattr()
2089*4882a593Smuzhiyun  */
dquot_transfer(struct inode * inode,struct iattr * iattr)2090*4882a593Smuzhiyun int dquot_transfer(struct inode *inode, struct iattr *iattr)
2091*4882a593Smuzhiyun {
2092*4882a593Smuzhiyun 	struct dquot *transfer_to[MAXQUOTAS] = {};
2093*4882a593Smuzhiyun 	struct dquot *dquot;
2094*4882a593Smuzhiyun 	struct super_block *sb = inode->i_sb;
2095*4882a593Smuzhiyun 	int ret;
2096*4882a593Smuzhiyun 
2097*4882a593Smuzhiyun 	if (!dquot_active(inode))
2098*4882a593Smuzhiyun 		return 0;
2099*4882a593Smuzhiyun 
2100*4882a593Smuzhiyun 	if (iattr->ia_valid & ATTR_UID && !uid_eq(iattr->ia_uid, inode->i_uid)){
2101*4882a593Smuzhiyun 		dquot = dqget(sb, make_kqid_uid(iattr->ia_uid));
2102*4882a593Smuzhiyun 		if (IS_ERR(dquot)) {
2103*4882a593Smuzhiyun 			if (PTR_ERR(dquot) != -ESRCH) {
2104*4882a593Smuzhiyun 				ret = PTR_ERR(dquot);
2105*4882a593Smuzhiyun 				goto out_put;
2106*4882a593Smuzhiyun 			}
2107*4882a593Smuzhiyun 			dquot = NULL;
2108*4882a593Smuzhiyun 		}
2109*4882a593Smuzhiyun 		transfer_to[USRQUOTA] = dquot;
2110*4882a593Smuzhiyun 	}
2111*4882a593Smuzhiyun 	if (iattr->ia_valid & ATTR_GID && !gid_eq(iattr->ia_gid, inode->i_gid)){
2112*4882a593Smuzhiyun 		dquot = dqget(sb, make_kqid_gid(iattr->ia_gid));
2113*4882a593Smuzhiyun 		if (IS_ERR(dquot)) {
2114*4882a593Smuzhiyun 			if (PTR_ERR(dquot) != -ESRCH) {
2115*4882a593Smuzhiyun 				ret = PTR_ERR(dquot);
2116*4882a593Smuzhiyun 				goto out_put;
2117*4882a593Smuzhiyun 			}
2118*4882a593Smuzhiyun 			dquot = NULL;
2119*4882a593Smuzhiyun 		}
2120*4882a593Smuzhiyun 		transfer_to[GRPQUOTA] = dquot;
2121*4882a593Smuzhiyun 	}
2122*4882a593Smuzhiyun 	ret = __dquot_transfer(inode, transfer_to);
2123*4882a593Smuzhiyun out_put:
2124*4882a593Smuzhiyun 	dqput_all(transfer_to);
2125*4882a593Smuzhiyun 	return ret;
2126*4882a593Smuzhiyun }
2127*4882a593Smuzhiyun EXPORT_SYMBOL(dquot_transfer);
2128*4882a593Smuzhiyun 
2129*4882a593Smuzhiyun /*
2130*4882a593Smuzhiyun  * Write info of quota file to disk
2131*4882a593Smuzhiyun  */
dquot_commit_info(struct super_block * sb,int type)2132*4882a593Smuzhiyun int dquot_commit_info(struct super_block *sb, int type)
2133*4882a593Smuzhiyun {
2134*4882a593Smuzhiyun 	struct quota_info *dqopt = sb_dqopt(sb);
2135*4882a593Smuzhiyun 
2136*4882a593Smuzhiyun 	return dqopt->ops[type]->write_file_info(sb, type);
2137*4882a593Smuzhiyun }
2138*4882a593Smuzhiyun EXPORT_SYMBOL(dquot_commit_info);
2139*4882a593Smuzhiyun 
dquot_get_next_id(struct super_block * sb,struct kqid * qid)2140*4882a593Smuzhiyun int dquot_get_next_id(struct super_block *sb, struct kqid *qid)
2141*4882a593Smuzhiyun {
2142*4882a593Smuzhiyun 	struct quota_info *dqopt = sb_dqopt(sb);
2143*4882a593Smuzhiyun 
2144*4882a593Smuzhiyun 	if (!sb_has_quota_active(sb, qid->type))
2145*4882a593Smuzhiyun 		return -ESRCH;
2146*4882a593Smuzhiyun 	if (!dqopt->ops[qid->type]->get_next_id)
2147*4882a593Smuzhiyun 		return -ENOSYS;
2148*4882a593Smuzhiyun 	return dqopt->ops[qid->type]->get_next_id(sb, qid);
2149*4882a593Smuzhiyun }
2150*4882a593Smuzhiyun EXPORT_SYMBOL(dquot_get_next_id);
2151*4882a593Smuzhiyun 
2152*4882a593Smuzhiyun /*
2153*4882a593Smuzhiyun  * Definitions of diskquota operations.
2154*4882a593Smuzhiyun  */
2155*4882a593Smuzhiyun const struct dquot_operations dquot_operations = {
2156*4882a593Smuzhiyun 	.write_dquot	= dquot_commit,
2157*4882a593Smuzhiyun 	.acquire_dquot	= dquot_acquire,
2158*4882a593Smuzhiyun 	.release_dquot	= dquot_release,
2159*4882a593Smuzhiyun 	.mark_dirty	= dquot_mark_dquot_dirty,
2160*4882a593Smuzhiyun 	.write_info	= dquot_commit_info,
2161*4882a593Smuzhiyun 	.alloc_dquot	= dquot_alloc,
2162*4882a593Smuzhiyun 	.destroy_dquot	= dquot_destroy,
2163*4882a593Smuzhiyun 	.get_next_id	= dquot_get_next_id,
2164*4882a593Smuzhiyun };
2165*4882a593Smuzhiyun EXPORT_SYMBOL(dquot_operations);
2166*4882a593Smuzhiyun 
2167*4882a593Smuzhiyun /*
2168*4882a593Smuzhiyun  * Generic helper for ->open on filesystems supporting disk quotas.
2169*4882a593Smuzhiyun  */
dquot_file_open(struct inode * inode,struct file * file)2170*4882a593Smuzhiyun int dquot_file_open(struct inode *inode, struct file *file)
2171*4882a593Smuzhiyun {
2172*4882a593Smuzhiyun 	int error;
2173*4882a593Smuzhiyun 
2174*4882a593Smuzhiyun 	error = generic_file_open(inode, file);
2175*4882a593Smuzhiyun 	if (!error && (file->f_mode & FMODE_WRITE))
2176*4882a593Smuzhiyun 		error = dquot_initialize(inode);
2177*4882a593Smuzhiyun 	return error;
2178*4882a593Smuzhiyun }
2179*4882a593Smuzhiyun EXPORT_SYMBOL(dquot_file_open);
2180*4882a593Smuzhiyun 
vfs_cleanup_quota_inode(struct super_block * sb,int type)2181*4882a593Smuzhiyun static void vfs_cleanup_quota_inode(struct super_block *sb, int type)
2182*4882a593Smuzhiyun {
2183*4882a593Smuzhiyun 	struct quota_info *dqopt = sb_dqopt(sb);
2184*4882a593Smuzhiyun 	struct inode *inode = dqopt->files[type];
2185*4882a593Smuzhiyun 
2186*4882a593Smuzhiyun 	if (!inode)
2187*4882a593Smuzhiyun 		return;
2188*4882a593Smuzhiyun 	if (!(dqopt->flags & DQUOT_QUOTA_SYS_FILE)) {
2189*4882a593Smuzhiyun 		inode_lock(inode);
2190*4882a593Smuzhiyun 		inode->i_flags &= ~S_NOQUOTA;
2191*4882a593Smuzhiyun 		inode_unlock(inode);
2192*4882a593Smuzhiyun 	}
2193*4882a593Smuzhiyun 	dqopt->files[type] = NULL;
2194*4882a593Smuzhiyun 	iput(inode);
2195*4882a593Smuzhiyun }
2196*4882a593Smuzhiyun 
2197*4882a593Smuzhiyun /*
2198*4882a593Smuzhiyun  * Turn quota off on a device. type == -1 ==> quotaoff for all types (umount)
2199*4882a593Smuzhiyun  */
dquot_disable(struct super_block * sb,int type,unsigned int flags)2200*4882a593Smuzhiyun int dquot_disable(struct super_block *sb, int type, unsigned int flags)
2201*4882a593Smuzhiyun {
2202*4882a593Smuzhiyun 	int cnt;
2203*4882a593Smuzhiyun 	struct quota_info *dqopt = sb_dqopt(sb);
2204*4882a593Smuzhiyun 
2205*4882a593Smuzhiyun 	/* s_umount should be held in exclusive mode */
2206*4882a593Smuzhiyun 	if (WARN_ON_ONCE(down_read_trylock(&sb->s_umount)))
2207*4882a593Smuzhiyun 		up_read(&sb->s_umount);
2208*4882a593Smuzhiyun 
2209*4882a593Smuzhiyun 	/* Cannot turn off usage accounting without turning off limits, or
2210*4882a593Smuzhiyun 	 * suspend quotas and simultaneously turn quotas off. */
2211*4882a593Smuzhiyun 	if ((flags & DQUOT_USAGE_ENABLED && !(flags & DQUOT_LIMITS_ENABLED))
2212*4882a593Smuzhiyun 	    || (flags & DQUOT_SUSPENDED && flags & (DQUOT_LIMITS_ENABLED |
2213*4882a593Smuzhiyun 	    DQUOT_USAGE_ENABLED)))
2214*4882a593Smuzhiyun 		return -EINVAL;
2215*4882a593Smuzhiyun 
2216*4882a593Smuzhiyun 	/*
2217*4882a593Smuzhiyun 	 * Skip everything if there's nothing to do. We have to do this because
2218*4882a593Smuzhiyun 	 * sometimes we are called when fill_super() failed and calling
2219*4882a593Smuzhiyun 	 * sync_fs() in such cases does no good.
2220*4882a593Smuzhiyun 	 */
2221*4882a593Smuzhiyun 	if (!sb_any_quota_loaded(sb))
2222*4882a593Smuzhiyun 		return 0;
2223*4882a593Smuzhiyun 
2224*4882a593Smuzhiyun 	for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
2225*4882a593Smuzhiyun 		if (type != -1 && cnt != type)
2226*4882a593Smuzhiyun 			continue;
2227*4882a593Smuzhiyun 		if (!sb_has_quota_loaded(sb, cnt))
2228*4882a593Smuzhiyun 			continue;
2229*4882a593Smuzhiyun 
2230*4882a593Smuzhiyun 		if (flags & DQUOT_SUSPENDED) {
2231*4882a593Smuzhiyun 			spin_lock(&dq_state_lock);
2232*4882a593Smuzhiyun 			dqopt->flags |=
2233*4882a593Smuzhiyun 				dquot_state_flag(DQUOT_SUSPENDED, cnt);
2234*4882a593Smuzhiyun 			spin_unlock(&dq_state_lock);
2235*4882a593Smuzhiyun 		} else {
2236*4882a593Smuzhiyun 			spin_lock(&dq_state_lock);
2237*4882a593Smuzhiyun 			dqopt->flags &= ~dquot_state_flag(flags, cnt);
2238*4882a593Smuzhiyun 			/* Turning off suspended quotas? */
2239*4882a593Smuzhiyun 			if (!sb_has_quota_loaded(sb, cnt) &&
2240*4882a593Smuzhiyun 			    sb_has_quota_suspended(sb, cnt)) {
2241*4882a593Smuzhiyun 				dqopt->flags &=	~dquot_state_flag(
2242*4882a593Smuzhiyun 							DQUOT_SUSPENDED, cnt);
2243*4882a593Smuzhiyun 				spin_unlock(&dq_state_lock);
2244*4882a593Smuzhiyun 				vfs_cleanup_quota_inode(sb, cnt);
2245*4882a593Smuzhiyun 				continue;
2246*4882a593Smuzhiyun 			}
2247*4882a593Smuzhiyun 			spin_unlock(&dq_state_lock);
2248*4882a593Smuzhiyun 		}
2249*4882a593Smuzhiyun 
2250*4882a593Smuzhiyun 		/* We still have to keep quota loaded? */
2251*4882a593Smuzhiyun 		if (sb_has_quota_loaded(sb, cnt) && !(flags & DQUOT_SUSPENDED))
2252*4882a593Smuzhiyun 			continue;
2253*4882a593Smuzhiyun 
2254*4882a593Smuzhiyun 		/* Note: these are blocking operations */
2255*4882a593Smuzhiyun 		drop_dquot_ref(sb, cnt);
2256*4882a593Smuzhiyun 		invalidate_dquots(sb, cnt);
2257*4882a593Smuzhiyun 		/*
2258*4882a593Smuzhiyun 		 * Now all dquots should be invalidated, all writes done so we
2259*4882a593Smuzhiyun 		 * should be only users of the info. No locks needed.
2260*4882a593Smuzhiyun 		 */
2261*4882a593Smuzhiyun 		if (info_dirty(&dqopt->info[cnt]))
2262*4882a593Smuzhiyun 			sb->dq_op->write_info(sb, cnt);
2263*4882a593Smuzhiyun 		if (dqopt->ops[cnt]->free_file_info)
2264*4882a593Smuzhiyun 			dqopt->ops[cnt]->free_file_info(sb, cnt);
2265*4882a593Smuzhiyun 		put_quota_format(dqopt->info[cnt].dqi_format);
2266*4882a593Smuzhiyun 		dqopt->info[cnt].dqi_flags = 0;
2267*4882a593Smuzhiyun 		dqopt->info[cnt].dqi_igrace = 0;
2268*4882a593Smuzhiyun 		dqopt->info[cnt].dqi_bgrace = 0;
2269*4882a593Smuzhiyun 		dqopt->ops[cnt] = NULL;
2270*4882a593Smuzhiyun 	}
2271*4882a593Smuzhiyun 
2272*4882a593Smuzhiyun 	/* Skip syncing and setting flags if quota files are hidden */
2273*4882a593Smuzhiyun 	if (dqopt->flags & DQUOT_QUOTA_SYS_FILE)
2274*4882a593Smuzhiyun 		goto put_inodes;
2275*4882a593Smuzhiyun 
2276*4882a593Smuzhiyun 	/* Sync the superblock so that buffers with quota data are written to
2277*4882a593Smuzhiyun 	 * disk (and so userspace sees correct data afterwards). */
2278*4882a593Smuzhiyun 	if (sb->s_op->sync_fs)
2279*4882a593Smuzhiyun 		sb->s_op->sync_fs(sb, 1);
2280*4882a593Smuzhiyun 	sync_blockdev(sb->s_bdev);
2281*4882a593Smuzhiyun 	/* Now the quota files are just ordinary files and we can set the
2282*4882a593Smuzhiyun 	 * inode flags back. Moreover we discard the pagecache so that
2283*4882a593Smuzhiyun 	 * userspace sees the writes we did bypassing the pagecache. We
2284*4882a593Smuzhiyun 	 * must also discard the blockdev buffers so that we see the
2285*4882a593Smuzhiyun 	 * changes done by userspace on the next quotaon() */
2286*4882a593Smuzhiyun 	for (cnt = 0; cnt < MAXQUOTAS; cnt++)
2287*4882a593Smuzhiyun 		if (!sb_has_quota_loaded(sb, cnt) && dqopt->files[cnt]) {
2288*4882a593Smuzhiyun 			inode_lock(dqopt->files[cnt]);
2289*4882a593Smuzhiyun 			truncate_inode_pages(&dqopt->files[cnt]->i_data, 0);
2290*4882a593Smuzhiyun 			inode_unlock(dqopt->files[cnt]);
2291*4882a593Smuzhiyun 		}
2292*4882a593Smuzhiyun 	if (sb->s_bdev)
2293*4882a593Smuzhiyun 		invalidate_bdev(sb->s_bdev);
2294*4882a593Smuzhiyun put_inodes:
2295*4882a593Smuzhiyun 	/* We are done when suspending quotas */
2296*4882a593Smuzhiyun 	if (flags & DQUOT_SUSPENDED)
2297*4882a593Smuzhiyun 		return 0;
2298*4882a593Smuzhiyun 
2299*4882a593Smuzhiyun 	for (cnt = 0; cnt < MAXQUOTAS; cnt++)
2300*4882a593Smuzhiyun 		if (!sb_has_quota_loaded(sb, cnt))
2301*4882a593Smuzhiyun 			vfs_cleanup_quota_inode(sb, cnt);
2302*4882a593Smuzhiyun 	return 0;
2303*4882a593Smuzhiyun }
2304*4882a593Smuzhiyun EXPORT_SYMBOL(dquot_disable);
2305*4882a593Smuzhiyun 
dquot_quota_off(struct super_block * sb,int type)2306*4882a593Smuzhiyun int dquot_quota_off(struct super_block *sb, int type)
2307*4882a593Smuzhiyun {
2308*4882a593Smuzhiyun 	return dquot_disable(sb, type,
2309*4882a593Smuzhiyun 			     DQUOT_USAGE_ENABLED | DQUOT_LIMITS_ENABLED);
2310*4882a593Smuzhiyun }
2311*4882a593Smuzhiyun EXPORT_SYMBOL(dquot_quota_off);
2312*4882a593Smuzhiyun 
2313*4882a593Smuzhiyun /*
2314*4882a593Smuzhiyun  *	Turn quotas on on a device
2315*4882a593Smuzhiyun  */
2316*4882a593Smuzhiyun 
vfs_setup_quota_inode(struct inode * inode,int type)2317*4882a593Smuzhiyun static int vfs_setup_quota_inode(struct inode *inode, int type)
2318*4882a593Smuzhiyun {
2319*4882a593Smuzhiyun 	struct super_block *sb = inode->i_sb;
2320*4882a593Smuzhiyun 	struct quota_info *dqopt = sb_dqopt(sb);
2321*4882a593Smuzhiyun 
2322*4882a593Smuzhiyun 	if (!S_ISREG(inode->i_mode))
2323*4882a593Smuzhiyun 		return -EACCES;
2324*4882a593Smuzhiyun 	if (IS_RDONLY(inode))
2325*4882a593Smuzhiyun 		return -EROFS;
2326*4882a593Smuzhiyun 	if (sb_has_quota_loaded(sb, type))
2327*4882a593Smuzhiyun 		return -EBUSY;
2328*4882a593Smuzhiyun 
2329*4882a593Smuzhiyun 	dqopt->files[type] = igrab(inode);
2330*4882a593Smuzhiyun 	if (!dqopt->files[type])
2331*4882a593Smuzhiyun 		return -EIO;
2332*4882a593Smuzhiyun 	if (!(dqopt->flags & DQUOT_QUOTA_SYS_FILE)) {
2333*4882a593Smuzhiyun 		/* We don't want quota and atime on quota files (deadlocks
2334*4882a593Smuzhiyun 		 * possible) Also nobody should write to the file - we use
2335*4882a593Smuzhiyun 		 * special IO operations which ignore the immutable bit. */
2336*4882a593Smuzhiyun 		inode_lock(inode);
2337*4882a593Smuzhiyun 		inode->i_flags |= S_NOQUOTA;
2338*4882a593Smuzhiyun 		inode_unlock(inode);
2339*4882a593Smuzhiyun 		/*
2340*4882a593Smuzhiyun 		 * When S_NOQUOTA is set, remove dquot references as no more
2341*4882a593Smuzhiyun 		 * references can be added
2342*4882a593Smuzhiyun 		 */
2343*4882a593Smuzhiyun 		__dquot_drop(inode);
2344*4882a593Smuzhiyun 	}
2345*4882a593Smuzhiyun 	return 0;
2346*4882a593Smuzhiyun }
2347*4882a593Smuzhiyun 
dquot_load_quota_sb(struct super_block * sb,int type,int format_id,unsigned int flags)2348*4882a593Smuzhiyun int dquot_load_quota_sb(struct super_block *sb, int type, int format_id,
2349*4882a593Smuzhiyun 	unsigned int flags)
2350*4882a593Smuzhiyun {
2351*4882a593Smuzhiyun 	struct quota_format_type *fmt = find_quota_format(format_id);
2352*4882a593Smuzhiyun 	struct quota_info *dqopt = sb_dqopt(sb);
2353*4882a593Smuzhiyun 	int error;
2354*4882a593Smuzhiyun 
2355*4882a593Smuzhiyun 	/* Just unsuspend quotas? */
2356*4882a593Smuzhiyun 	BUG_ON(flags & DQUOT_SUSPENDED);
2357*4882a593Smuzhiyun 	/* s_umount should be held in exclusive mode */
2358*4882a593Smuzhiyun 	if (WARN_ON_ONCE(down_read_trylock(&sb->s_umount)))
2359*4882a593Smuzhiyun 		up_read(&sb->s_umount);
2360*4882a593Smuzhiyun 
2361*4882a593Smuzhiyun 	if (!fmt)
2362*4882a593Smuzhiyun 		return -ESRCH;
2363*4882a593Smuzhiyun 	if (!sb->s_op->quota_write || !sb->s_op->quota_read ||
2364*4882a593Smuzhiyun 	    (type == PRJQUOTA && sb->dq_op->get_projid == NULL)) {
2365*4882a593Smuzhiyun 		error = -EINVAL;
2366*4882a593Smuzhiyun 		goto out_fmt;
2367*4882a593Smuzhiyun 	}
2368*4882a593Smuzhiyun 	/* Filesystems outside of init_user_ns not yet supported */
2369*4882a593Smuzhiyun 	if (sb->s_user_ns != &init_user_ns) {
2370*4882a593Smuzhiyun 		error = -EINVAL;
2371*4882a593Smuzhiyun 		goto out_fmt;
2372*4882a593Smuzhiyun 	}
2373*4882a593Smuzhiyun 	/* Usage always has to be set... */
2374*4882a593Smuzhiyun 	if (!(flags & DQUOT_USAGE_ENABLED)) {
2375*4882a593Smuzhiyun 		error = -EINVAL;
2376*4882a593Smuzhiyun 		goto out_fmt;
2377*4882a593Smuzhiyun 	}
2378*4882a593Smuzhiyun 	if (sb_has_quota_loaded(sb, type)) {
2379*4882a593Smuzhiyun 		error = -EBUSY;
2380*4882a593Smuzhiyun 		goto out_fmt;
2381*4882a593Smuzhiyun 	}
2382*4882a593Smuzhiyun 
2383*4882a593Smuzhiyun 	if (!(dqopt->flags & DQUOT_QUOTA_SYS_FILE)) {
2384*4882a593Smuzhiyun 		/* As we bypass the pagecache we must now flush all the
2385*4882a593Smuzhiyun 		 * dirty data and invalidate caches so that kernel sees
2386*4882a593Smuzhiyun 		 * changes from userspace. It is not enough to just flush
2387*4882a593Smuzhiyun 		 * the quota file since if blocksize < pagesize, invalidation
2388*4882a593Smuzhiyun 		 * of the cache could fail because of other unrelated dirty
2389*4882a593Smuzhiyun 		 * data */
2390*4882a593Smuzhiyun 		sync_filesystem(sb);
2391*4882a593Smuzhiyun 		invalidate_bdev(sb->s_bdev);
2392*4882a593Smuzhiyun 	}
2393*4882a593Smuzhiyun 
2394*4882a593Smuzhiyun 	error = -EINVAL;
2395*4882a593Smuzhiyun 	if (!fmt->qf_ops->check_quota_file(sb, type))
2396*4882a593Smuzhiyun 		goto out_fmt;
2397*4882a593Smuzhiyun 
2398*4882a593Smuzhiyun 	dqopt->ops[type] = fmt->qf_ops;
2399*4882a593Smuzhiyun 	dqopt->info[type].dqi_format = fmt;
2400*4882a593Smuzhiyun 	dqopt->info[type].dqi_fmt_id = format_id;
2401*4882a593Smuzhiyun 	INIT_LIST_HEAD(&dqopt->info[type].dqi_dirty_list);
2402*4882a593Smuzhiyun 	error = dqopt->ops[type]->read_file_info(sb, type);
2403*4882a593Smuzhiyun 	if (error < 0)
2404*4882a593Smuzhiyun 		goto out_fmt;
2405*4882a593Smuzhiyun 	if (dqopt->flags & DQUOT_QUOTA_SYS_FILE) {
2406*4882a593Smuzhiyun 		spin_lock(&dq_data_lock);
2407*4882a593Smuzhiyun 		dqopt->info[type].dqi_flags |= DQF_SYS_FILE;
2408*4882a593Smuzhiyun 		spin_unlock(&dq_data_lock);
2409*4882a593Smuzhiyun 	}
2410*4882a593Smuzhiyun 	spin_lock(&dq_state_lock);
2411*4882a593Smuzhiyun 	dqopt->flags |= dquot_state_flag(flags, type);
2412*4882a593Smuzhiyun 	spin_unlock(&dq_state_lock);
2413*4882a593Smuzhiyun 
2414*4882a593Smuzhiyun 	error = add_dquot_ref(sb, type);
2415*4882a593Smuzhiyun 	if (error)
2416*4882a593Smuzhiyun 		dquot_disable(sb, type, flags);
2417*4882a593Smuzhiyun 
2418*4882a593Smuzhiyun 	return error;
2419*4882a593Smuzhiyun out_fmt:
2420*4882a593Smuzhiyun 	put_quota_format(fmt);
2421*4882a593Smuzhiyun 
2422*4882a593Smuzhiyun 	return error;
2423*4882a593Smuzhiyun }
2424*4882a593Smuzhiyun EXPORT_SYMBOL(dquot_load_quota_sb);
2425*4882a593Smuzhiyun 
2426*4882a593Smuzhiyun /*
2427*4882a593Smuzhiyun  * More powerful function for turning on quotas on given quota inode allowing
2428*4882a593Smuzhiyun  * setting of individual quota flags
2429*4882a593Smuzhiyun  */
dquot_load_quota_inode(struct inode * inode,int type,int format_id,unsigned int flags)2430*4882a593Smuzhiyun int dquot_load_quota_inode(struct inode *inode, int type, int format_id,
2431*4882a593Smuzhiyun 	unsigned int flags)
2432*4882a593Smuzhiyun {
2433*4882a593Smuzhiyun 	int err;
2434*4882a593Smuzhiyun 
2435*4882a593Smuzhiyun 	err = vfs_setup_quota_inode(inode, type);
2436*4882a593Smuzhiyun 	if (err < 0)
2437*4882a593Smuzhiyun 		return err;
2438*4882a593Smuzhiyun 	err = dquot_load_quota_sb(inode->i_sb, type, format_id, flags);
2439*4882a593Smuzhiyun 	if (err < 0)
2440*4882a593Smuzhiyun 		vfs_cleanup_quota_inode(inode->i_sb, type);
2441*4882a593Smuzhiyun 	return err;
2442*4882a593Smuzhiyun }
2443*4882a593Smuzhiyun EXPORT_SYMBOL(dquot_load_quota_inode);
2444*4882a593Smuzhiyun 
2445*4882a593Smuzhiyun /* Reenable quotas on remount RW */
dquot_resume(struct super_block * sb,int type)2446*4882a593Smuzhiyun int dquot_resume(struct super_block *sb, int type)
2447*4882a593Smuzhiyun {
2448*4882a593Smuzhiyun 	struct quota_info *dqopt = sb_dqopt(sb);
2449*4882a593Smuzhiyun 	int ret = 0, cnt;
2450*4882a593Smuzhiyun 	unsigned int flags;
2451*4882a593Smuzhiyun 
2452*4882a593Smuzhiyun 	/* s_umount should be held in exclusive mode */
2453*4882a593Smuzhiyun 	if (WARN_ON_ONCE(down_read_trylock(&sb->s_umount)))
2454*4882a593Smuzhiyun 		up_read(&sb->s_umount);
2455*4882a593Smuzhiyun 
2456*4882a593Smuzhiyun 	for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
2457*4882a593Smuzhiyun 		if (type != -1 && cnt != type)
2458*4882a593Smuzhiyun 			continue;
2459*4882a593Smuzhiyun 		if (!sb_has_quota_suspended(sb, cnt))
2460*4882a593Smuzhiyun 			continue;
2461*4882a593Smuzhiyun 
2462*4882a593Smuzhiyun 		spin_lock(&dq_state_lock);
2463*4882a593Smuzhiyun 		flags = dqopt->flags & dquot_state_flag(DQUOT_USAGE_ENABLED |
2464*4882a593Smuzhiyun 							DQUOT_LIMITS_ENABLED,
2465*4882a593Smuzhiyun 							cnt);
2466*4882a593Smuzhiyun 		dqopt->flags &= ~dquot_state_flag(DQUOT_STATE_FLAGS, cnt);
2467*4882a593Smuzhiyun 		spin_unlock(&dq_state_lock);
2468*4882a593Smuzhiyun 
2469*4882a593Smuzhiyun 		flags = dquot_generic_flag(flags, cnt);
2470*4882a593Smuzhiyun 		ret = dquot_load_quota_sb(sb, cnt, dqopt->info[cnt].dqi_fmt_id,
2471*4882a593Smuzhiyun 					  flags);
2472*4882a593Smuzhiyun 		if (ret < 0)
2473*4882a593Smuzhiyun 			vfs_cleanup_quota_inode(sb, cnt);
2474*4882a593Smuzhiyun 	}
2475*4882a593Smuzhiyun 
2476*4882a593Smuzhiyun 	return ret;
2477*4882a593Smuzhiyun }
2478*4882a593Smuzhiyun EXPORT_SYMBOL(dquot_resume);
2479*4882a593Smuzhiyun 
dquot_quota_on(struct super_block * sb,int type,int format_id,const struct path * path)2480*4882a593Smuzhiyun int dquot_quota_on(struct super_block *sb, int type, int format_id,
2481*4882a593Smuzhiyun 		   const struct path *path)
2482*4882a593Smuzhiyun {
2483*4882a593Smuzhiyun 	int error = security_quota_on(path->dentry);
2484*4882a593Smuzhiyun 	if (error)
2485*4882a593Smuzhiyun 		return error;
2486*4882a593Smuzhiyun 	/* Quota file not on the same filesystem? */
2487*4882a593Smuzhiyun 	if (path->dentry->d_sb != sb)
2488*4882a593Smuzhiyun 		error = -EXDEV;
2489*4882a593Smuzhiyun 	else
2490*4882a593Smuzhiyun 		error = dquot_load_quota_inode(d_inode(path->dentry), type,
2491*4882a593Smuzhiyun 					     format_id, DQUOT_USAGE_ENABLED |
2492*4882a593Smuzhiyun 					     DQUOT_LIMITS_ENABLED);
2493*4882a593Smuzhiyun 	return error;
2494*4882a593Smuzhiyun }
2495*4882a593Smuzhiyun EXPORT_SYMBOL(dquot_quota_on);
2496*4882a593Smuzhiyun 
2497*4882a593Smuzhiyun /*
2498*4882a593Smuzhiyun  * This function is used when filesystem needs to initialize quotas
2499*4882a593Smuzhiyun  * during mount time.
2500*4882a593Smuzhiyun  */
dquot_quota_on_mount(struct super_block * sb,char * qf_name,int format_id,int type)2501*4882a593Smuzhiyun int dquot_quota_on_mount(struct super_block *sb, char *qf_name,
2502*4882a593Smuzhiyun 		int format_id, int type)
2503*4882a593Smuzhiyun {
2504*4882a593Smuzhiyun 	struct dentry *dentry;
2505*4882a593Smuzhiyun 	int error;
2506*4882a593Smuzhiyun 
2507*4882a593Smuzhiyun 	dentry = lookup_positive_unlocked(qf_name, sb->s_root, strlen(qf_name));
2508*4882a593Smuzhiyun 	if (IS_ERR(dentry))
2509*4882a593Smuzhiyun 		return PTR_ERR(dentry);
2510*4882a593Smuzhiyun 
2511*4882a593Smuzhiyun 	error = security_quota_on(dentry);
2512*4882a593Smuzhiyun 	if (!error)
2513*4882a593Smuzhiyun 		error = dquot_load_quota_inode(d_inode(dentry), type, format_id,
2514*4882a593Smuzhiyun 				DQUOT_USAGE_ENABLED | DQUOT_LIMITS_ENABLED);
2515*4882a593Smuzhiyun 
2516*4882a593Smuzhiyun 	dput(dentry);
2517*4882a593Smuzhiyun 	return error;
2518*4882a593Smuzhiyun }
2519*4882a593Smuzhiyun EXPORT_SYMBOL(dquot_quota_on_mount);
2520*4882a593Smuzhiyun 
dquot_quota_enable(struct super_block * sb,unsigned int flags)2521*4882a593Smuzhiyun static int dquot_quota_enable(struct super_block *sb, unsigned int flags)
2522*4882a593Smuzhiyun {
2523*4882a593Smuzhiyun 	int ret;
2524*4882a593Smuzhiyun 	int type;
2525*4882a593Smuzhiyun 	struct quota_info *dqopt = sb_dqopt(sb);
2526*4882a593Smuzhiyun 
2527*4882a593Smuzhiyun 	if (!(dqopt->flags & DQUOT_QUOTA_SYS_FILE))
2528*4882a593Smuzhiyun 		return -ENOSYS;
2529*4882a593Smuzhiyun 	/* Accounting cannot be turned on while fs is mounted */
2530*4882a593Smuzhiyun 	flags &= ~(FS_QUOTA_UDQ_ACCT | FS_QUOTA_GDQ_ACCT | FS_QUOTA_PDQ_ACCT);
2531*4882a593Smuzhiyun 	if (!flags)
2532*4882a593Smuzhiyun 		return -EINVAL;
2533*4882a593Smuzhiyun 	for (type = 0; type < MAXQUOTAS; type++) {
2534*4882a593Smuzhiyun 		if (!(flags & qtype_enforce_flag(type)))
2535*4882a593Smuzhiyun 			continue;
2536*4882a593Smuzhiyun 		/* Can't enforce without accounting */
2537*4882a593Smuzhiyun 		if (!sb_has_quota_usage_enabled(sb, type)) {
2538*4882a593Smuzhiyun 			ret = -EINVAL;
2539*4882a593Smuzhiyun 			goto out_err;
2540*4882a593Smuzhiyun 		}
2541*4882a593Smuzhiyun 		if (sb_has_quota_limits_enabled(sb, type)) {
2542*4882a593Smuzhiyun 			ret = -EBUSY;
2543*4882a593Smuzhiyun 			goto out_err;
2544*4882a593Smuzhiyun 		}
2545*4882a593Smuzhiyun 		spin_lock(&dq_state_lock);
2546*4882a593Smuzhiyun 		dqopt->flags |= dquot_state_flag(DQUOT_LIMITS_ENABLED, type);
2547*4882a593Smuzhiyun 		spin_unlock(&dq_state_lock);
2548*4882a593Smuzhiyun 	}
2549*4882a593Smuzhiyun 	return 0;
2550*4882a593Smuzhiyun out_err:
2551*4882a593Smuzhiyun 	/* Backout enforcement enablement we already did */
2552*4882a593Smuzhiyun 	for (type--; type >= 0; type--)  {
2553*4882a593Smuzhiyun 		if (flags & qtype_enforce_flag(type))
2554*4882a593Smuzhiyun 			dquot_disable(sb, type, DQUOT_LIMITS_ENABLED);
2555*4882a593Smuzhiyun 	}
2556*4882a593Smuzhiyun 	/* Error code translation for better compatibility with XFS */
2557*4882a593Smuzhiyun 	if (ret == -EBUSY)
2558*4882a593Smuzhiyun 		ret = -EEXIST;
2559*4882a593Smuzhiyun 	return ret;
2560*4882a593Smuzhiyun }
2561*4882a593Smuzhiyun 
dquot_quota_disable(struct super_block * sb,unsigned int flags)2562*4882a593Smuzhiyun static int dquot_quota_disable(struct super_block *sb, unsigned int flags)
2563*4882a593Smuzhiyun {
2564*4882a593Smuzhiyun 	int ret;
2565*4882a593Smuzhiyun 	int type;
2566*4882a593Smuzhiyun 	struct quota_info *dqopt = sb_dqopt(sb);
2567*4882a593Smuzhiyun 
2568*4882a593Smuzhiyun 	if (!(dqopt->flags & DQUOT_QUOTA_SYS_FILE))
2569*4882a593Smuzhiyun 		return -ENOSYS;
2570*4882a593Smuzhiyun 	/*
2571*4882a593Smuzhiyun 	 * We don't support turning off accounting via quotactl. In principle
2572*4882a593Smuzhiyun 	 * quota infrastructure can do this but filesystems don't expect
2573*4882a593Smuzhiyun 	 * userspace to be able to do it.
2574*4882a593Smuzhiyun 	 */
2575*4882a593Smuzhiyun 	if (flags &
2576*4882a593Smuzhiyun 		  (FS_QUOTA_UDQ_ACCT | FS_QUOTA_GDQ_ACCT | FS_QUOTA_PDQ_ACCT))
2577*4882a593Smuzhiyun 		return -EOPNOTSUPP;
2578*4882a593Smuzhiyun 
2579*4882a593Smuzhiyun 	/* Filter out limits not enabled */
2580*4882a593Smuzhiyun 	for (type = 0; type < MAXQUOTAS; type++)
2581*4882a593Smuzhiyun 		if (!sb_has_quota_limits_enabled(sb, type))
2582*4882a593Smuzhiyun 			flags &= ~qtype_enforce_flag(type);
2583*4882a593Smuzhiyun 	/* Nothing left? */
2584*4882a593Smuzhiyun 	if (!flags)
2585*4882a593Smuzhiyun 		return -EEXIST;
2586*4882a593Smuzhiyun 	for (type = 0; type < MAXQUOTAS; type++) {
2587*4882a593Smuzhiyun 		if (flags & qtype_enforce_flag(type)) {
2588*4882a593Smuzhiyun 			ret = dquot_disable(sb, type, DQUOT_LIMITS_ENABLED);
2589*4882a593Smuzhiyun 			if (ret < 0)
2590*4882a593Smuzhiyun 				goto out_err;
2591*4882a593Smuzhiyun 		}
2592*4882a593Smuzhiyun 	}
2593*4882a593Smuzhiyun 	return 0;
2594*4882a593Smuzhiyun out_err:
2595*4882a593Smuzhiyun 	/* Backout enforcement disabling we already did */
2596*4882a593Smuzhiyun 	for (type--; type >= 0; type--)  {
2597*4882a593Smuzhiyun 		if (flags & qtype_enforce_flag(type)) {
2598*4882a593Smuzhiyun 			spin_lock(&dq_state_lock);
2599*4882a593Smuzhiyun 			dqopt->flags |=
2600*4882a593Smuzhiyun 				dquot_state_flag(DQUOT_LIMITS_ENABLED, type);
2601*4882a593Smuzhiyun 			spin_unlock(&dq_state_lock);
2602*4882a593Smuzhiyun 		}
2603*4882a593Smuzhiyun 	}
2604*4882a593Smuzhiyun 	return ret;
2605*4882a593Smuzhiyun }
2606*4882a593Smuzhiyun 
2607*4882a593Smuzhiyun /* Generic routine for getting common part of quota structure */
do_get_dqblk(struct dquot * dquot,struct qc_dqblk * di)2608*4882a593Smuzhiyun static void do_get_dqblk(struct dquot *dquot, struct qc_dqblk *di)
2609*4882a593Smuzhiyun {
2610*4882a593Smuzhiyun 	struct mem_dqblk *dm = &dquot->dq_dqb;
2611*4882a593Smuzhiyun 
2612*4882a593Smuzhiyun 	memset(di, 0, sizeof(*di));
2613*4882a593Smuzhiyun 	spin_lock(&dquot->dq_dqb_lock);
2614*4882a593Smuzhiyun 	di->d_spc_hardlimit = dm->dqb_bhardlimit;
2615*4882a593Smuzhiyun 	di->d_spc_softlimit = dm->dqb_bsoftlimit;
2616*4882a593Smuzhiyun 	di->d_ino_hardlimit = dm->dqb_ihardlimit;
2617*4882a593Smuzhiyun 	di->d_ino_softlimit = dm->dqb_isoftlimit;
2618*4882a593Smuzhiyun 	di->d_space = dm->dqb_curspace + dm->dqb_rsvspace;
2619*4882a593Smuzhiyun 	di->d_ino_count = dm->dqb_curinodes;
2620*4882a593Smuzhiyun 	di->d_spc_timer = dm->dqb_btime;
2621*4882a593Smuzhiyun 	di->d_ino_timer = dm->dqb_itime;
2622*4882a593Smuzhiyun 	spin_unlock(&dquot->dq_dqb_lock);
2623*4882a593Smuzhiyun }
2624*4882a593Smuzhiyun 
dquot_get_dqblk(struct super_block * sb,struct kqid qid,struct qc_dqblk * di)2625*4882a593Smuzhiyun int dquot_get_dqblk(struct super_block *sb, struct kqid qid,
2626*4882a593Smuzhiyun 		    struct qc_dqblk *di)
2627*4882a593Smuzhiyun {
2628*4882a593Smuzhiyun 	struct dquot *dquot;
2629*4882a593Smuzhiyun 
2630*4882a593Smuzhiyun 	dquot = dqget(sb, qid);
2631*4882a593Smuzhiyun 	if (IS_ERR(dquot))
2632*4882a593Smuzhiyun 		return PTR_ERR(dquot);
2633*4882a593Smuzhiyun 	do_get_dqblk(dquot, di);
2634*4882a593Smuzhiyun 	dqput(dquot);
2635*4882a593Smuzhiyun 
2636*4882a593Smuzhiyun 	return 0;
2637*4882a593Smuzhiyun }
2638*4882a593Smuzhiyun EXPORT_SYMBOL(dquot_get_dqblk);
2639*4882a593Smuzhiyun 
dquot_get_next_dqblk(struct super_block * sb,struct kqid * qid,struct qc_dqblk * di)2640*4882a593Smuzhiyun int dquot_get_next_dqblk(struct super_block *sb, struct kqid *qid,
2641*4882a593Smuzhiyun 			 struct qc_dqblk *di)
2642*4882a593Smuzhiyun {
2643*4882a593Smuzhiyun 	struct dquot *dquot;
2644*4882a593Smuzhiyun 	int err;
2645*4882a593Smuzhiyun 
2646*4882a593Smuzhiyun 	if (!sb->dq_op->get_next_id)
2647*4882a593Smuzhiyun 		return -ENOSYS;
2648*4882a593Smuzhiyun 	err = sb->dq_op->get_next_id(sb, qid);
2649*4882a593Smuzhiyun 	if (err < 0)
2650*4882a593Smuzhiyun 		return err;
2651*4882a593Smuzhiyun 	dquot = dqget(sb, *qid);
2652*4882a593Smuzhiyun 	if (IS_ERR(dquot))
2653*4882a593Smuzhiyun 		return PTR_ERR(dquot);
2654*4882a593Smuzhiyun 	do_get_dqblk(dquot, di);
2655*4882a593Smuzhiyun 	dqput(dquot);
2656*4882a593Smuzhiyun 
2657*4882a593Smuzhiyun 	return 0;
2658*4882a593Smuzhiyun }
2659*4882a593Smuzhiyun EXPORT_SYMBOL(dquot_get_next_dqblk);
2660*4882a593Smuzhiyun 
2661*4882a593Smuzhiyun #define VFS_QC_MASK \
2662*4882a593Smuzhiyun 	(QC_SPACE | QC_SPC_SOFT | QC_SPC_HARD | \
2663*4882a593Smuzhiyun 	 QC_INO_COUNT | QC_INO_SOFT | QC_INO_HARD | \
2664*4882a593Smuzhiyun 	 QC_SPC_TIMER | QC_INO_TIMER)
2665*4882a593Smuzhiyun 
2666*4882a593Smuzhiyun /* Generic routine for setting common part of quota structure */
do_set_dqblk(struct dquot * dquot,struct qc_dqblk * di)2667*4882a593Smuzhiyun static int do_set_dqblk(struct dquot *dquot, struct qc_dqblk *di)
2668*4882a593Smuzhiyun {
2669*4882a593Smuzhiyun 	struct mem_dqblk *dm = &dquot->dq_dqb;
2670*4882a593Smuzhiyun 	int check_blim = 0, check_ilim = 0;
2671*4882a593Smuzhiyun 	struct mem_dqinfo *dqi = &sb_dqopt(dquot->dq_sb)->info[dquot->dq_id.type];
2672*4882a593Smuzhiyun 
2673*4882a593Smuzhiyun 	if (di->d_fieldmask & ~VFS_QC_MASK)
2674*4882a593Smuzhiyun 		return -EINVAL;
2675*4882a593Smuzhiyun 
2676*4882a593Smuzhiyun 	if (((di->d_fieldmask & QC_SPC_SOFT) &&
2677*4882a593Smuzhiyun 	     di->d_spc_softlimit > dqi->dqi_max_spc_limit) ||
2678*4882a593Smuzhiyun 	    ((di->d_fieldmask & QC_SPC_HARD) &&
2679*4882a593Smuzhiyun 	     di->d_spc_hardlimit > dqi->dqi_max_spc_limit) ||
2680*4882a593Smuzhiyun 	    ((di->d_fieldmask & QC_INO_SOFT) &&
2681*4882a593Smuzhiyun 	     (di->d_ino_softlimit > dqi->dqi_max_ino_limit)) ||
2682*4882a593Smuzhiyun 	    ((di->d_fieldmask & QC_INO_HARD) &&
2683*4882a593Smuzhiyun 	     (di->d_ino_hardlimit > dqi->dqi_max_ino_limit)))
2684*4882a593Smuzhiyun 		return -ERANGE;
2685*4882a593Smuzhiyun 
2686*4882a593Smuzhiyun 	spin_lock(&dquot->dq_dqb_lock);
2687*4882a593Smuzhiyun 	if (di->d_fieldmask & QC_SPACE) {
2688*4882a593Smuzhiyun 		dm->dqb_curspace = di->d_space - dm->dqb_rsvspace;
2689*4882a593Smuzhiyun 		check_blim = 1;
2690*4882a593Smuzhiyun 		set_bit(DQ_LASTSET_B + QIF_SPACE_B, &dquot->dq_flags);
2691*4882a593Smuzhiyun 	}
2692*4882a593Smuzhiyun 
2693*4882a593Smuzhiyun 	if (di->d_fieldmask & QC_SPC_SOFT)
2694*4882a593Smuzhiyun 		dm->dqb_bsoftlimit = di->d_spc_softlimit;
2695*4882a593Smuzhiyun 	if (di->d_fieldmask & QC_SPC_HARD)
2696*4882a593Smuzhiyun 		dm->dqb_bhardlimit = di->d_spc_hardlimit;
2697*4882a593Smuzhiyun 	if (di->d_fieldmask & (QC_SPC_SOFT | QC_SPC_HARD)) {
2698*4882a593Smuzhiyun 		check_blim = 1;
2699*4882a593Smuzhiyun 		set_bit(DQ_LASTSET_B + QIF_BLIMITS_B, &dquot->dq_flags);
2700*4882a593Smuzhiyun 	}
2701*4882a593Smuzhiyun 
2702*4882a593Smuzhiyun 	if (di->d_fieldmask & QC_INO_COUNT) {
2703*4882a593Smuzhiyun 		dm->dqb_curinodes = di->d_ino_count;
2704*4882a593Smuzhiyun 		check_ilim = 1;
2705*4882a593Smuzhiyun 		set_bit(DQ_LASTSET_B + QIF_INODES_B, &dquot->dq_flags);
2706*4882a593Smuzhiyun 	}
2707*4882a593Smuzhiyun 
2708*4882a593Smuzhiyun 	if (di->d_fieldmask & QC_INO_SOFT)
2709*4882a593Smuzhiyun 		dm->dqb_isoftlimit = di->d_ino_softlimit;
2710*4882a593Smuzhiyun 	if (di->d_fieldmask & QC_INO_HARD)
2711*4882a593Smuzhiyun 		dm->dqb_ihardlimit = di->d_ino_hardlimit;
2712*4882a593Smuzhiyun 	if (di->d_fieldmask & (QC_INO_SOFT | QC_INO_HARD)) {
2713*4882a593Smuzhiyun 		check_ilim = 1;
2714*4882a593Smuzhiyun 		set_bit(DQ_LASTSET_B + QIF_ILIMITS_B, &dquot->dq_flags);
2715*4882a593Smuzhiyun 	}
2716*4882a593Smuzhiyun 
2717*4882a593Smuzhiyun 	if (di->d_fieldmask & QC_SPC_TIMER) {
2718*4882a593Smuzhiyun 		dm->dqb_btime = di->d_spc_timer;
2719*4882a593Smuzhiyun 		check_blim = 1;
2720*4882a593Smuzhiyun 		set_bit(DQ_LASTSET_B + QIF_BTIME_B, &dquot->dq_flags);
2721*4882a593Smuzhiyun 	}
2722*4882a593Smuzhiyun 
2723*4882a593Smuzhiyun 	if (di->d_fieldmask & QC_INO_TIMER) {
2724*4882a593Smuzhiyun 		dm->dqb_itime = di->d_ino_timer;
2725*4882a593Smuzhiyun 		check_ilim = 1;
2726*4882a593Smuzhiyun 		set_bit(DQ_LASTSET_B + QIF_ITIME_B, &dquot->dq_flags);
2727*4882a593Smuzhiyun 	}
2728*4882a593Smuzhiyun 
2729*4882a593Smuzhiyun 	if (check_blim) {
2730*4882a593Smuzhiyun 		if (!dm->dqb_bsoftlimit ||
2731*4882a593Smuzhiyun 		    dm->dqb_curspace + dm->dqb_rsvspace <= dm->dqb_bsoftlimit) {
2732*4882a593Smuzhiyun 			dm->dqb_btime = 0;
2733*4882a593Smuzhiyun 			clear_bit(DQ_BLKS_B, &dquot->dq_flags);
2734*4882a593Smuzhiyun 		} else if (!(di->d_fieldmask & QC_SPC_TIMER))
2735*4882a593Smuzhiyun 			/* Set grace only if user hasn't provided his own... */
2736*4882a593Smuzhiyun 			dm->dqb_btime = ktime_get_real_seconds() + dqi->dqi_bgrace;
2737*4882a593Smuzhiyun 	}
2738*4882a593Smuzhiyun 	if (check_ilim) {
2739*4882a593Smuzhiyun 		if (!dm->dqb_isoftlimit ||
2740*4882a593Smuzhiyun 		    dm->dqb_curinodes <= dm->dqb_isoftlimit) {
2741*4882a593Smuzhiyun 			dm->dqb_itime = 0;
2742*4882a593Smuzhiyun 			clear_bit(DQ_INODES_B, &dquot->dq_flags);
2743*4882a593Smuzhiyun 		} else if (!(di->d_fieldmask & QC_INO_TIMER))
2744*4882a593Smuzhiyun 			/* Set grace only if user hasn't provided his own... */
2745*4882a593Smuzhiyun 			dm->dqb_itime = ktime_get_real_seconds() + dqi->dqi_igrace;
2746*4882a593Smuzhiyun 	}
2747*4882a593Smuzhiyun 	if (dm->dqb_bhardlimit || dm->dqb_bsoftlimit || dm->dqb_ihardlimit ||
2748*4882a593Smuzhiyun 	    dm->dqb_isoftlimit)
2749*4882a593Smuzhiyun 		clear_bit(DQ_FAKE_B, &dquot->dq_flags);
2750*4882a593Smuzhiyun 	else
2751*4882a593Smuzhiyun 		set_bit(DQ_FAKE_B, &dquot->dq_flags);
2752*4882a593Smuzhiyun 	spin_unlock(&dquot->dq_dqb_lock);
2753*4882a593Smuzhiyun 	mark_dquot_dirty(dquot);
2754*4882a593Smuzhiyun 
2755*4882a593Smuzhiyun 	return 0;
2756*4882a593Smuzhiyun }
2757*4882a593Smuzhiyun 
dquot_set_dqblk(struct super_block * sb,struct kqid qid,struct qc_dqblk * di)2758*4882a593Smuzhiyun int dquot_set_dqblk(struct super_block *sb, struct kqid qid,
2759*4882a593Smuzhiyun 		  struct qc_dqblk *di)
2760*4882a593Smuzhiyun {
2761*4882a593Smuzhiyun 	struct dquot *dquot;
2762*4882a593Smuzhiyun 	int rc;
2763*4882a593Smuzhiyun 
2764*4882a593Smuzhiyun 	dquot = dqget(sb, qid);
2765*4882a593Smuzhiyun 	if (IS_ERR(dquot)) {
2766*4882a593Smuzhiyun 		rc = PTR_ERR(dquot);
2767*4882a593Smuzhiyun 		goto out;
2768*4882a593Smuzhiyun 	}
2769*4882a593Smuzhiyun 	rc = do_set_dqblk(dquot, di);
2770*4882a593Smuzhiyun 	dqput(dquot);
2771*4882a593Smuzhiyun out:
2772*4882a593Smuzhiyun 	return rc;
2773*4882a593Smuzhiyun }
2774*4882a593Smuzhiyun EXPORT_SYMBOL(dquot_set_dqblk);
2775*4882a593Smuzhiyun 
2776*4882a593Smuzhiyun /* Generic routine for getting common part of quota file information */
dquot_get_state(struct super_block * sb,struct qc_state * state)2777*4882a593Smuzhiyun int dquot_get_state(struct super_block *sb, struct qc_state *state)
2778*4882a593Smuzhiyun {
2779*4882a593Smuzhiyun 	struct mem_dqinfo *mi;
2780*4882a593Smuzhiyun 	struct qc_type_state *tstate;
2781*4882a593Smuzhiyun 	struct quota_info *dqopt = sb_dqopt(sb);
2782*4882a593Smuzhiyun 	int type;
2783*4882a593Smuzhiyun 
2784*4882a593Smuzhiyun 	memset(state, 0, sizeof(*state));
2785*4882a593Smuzhiyun 	for (type = 0; type < MAXQUOTAS; type++) {
2786*4882a593Smuzhiyun 		if (!sb_has_quota_active(sb, type))
2787*4882a593Smuzhiyun 			continue;
2788*4882a593Smuzhiyun 		tstate = state->s_state + type;
2789*4882a593Smuzhiyun 		mi = sb_dqopt(sb)->info + type;
2790*4882a593Smuzhiyun 		tstate->flags = QCI_ACCT_ENABLED;
2791*4882a593Smuzhiyun 		spin_lock(&dq_data_lock);
2792*4882a593Smuzhiyun 		if (mi->dqi_flags & DQF_SYS_FILE)
2793*4882a593Smuzhiyun 			tstate->flags |= QCI_SYSFILE;
2794*4882a593Smuzhiyun 		if (mi->dqi_flags & DQF_ROOT_SQUASH)
2795*4882a593Smuzhiyun 			tstate->flags |= QCI_ROOT_SQUASH;
2796*4882a593Smuzhiyun 		if (sb_has_quota_limits_enabled(sb, type))
2797*4882a593Smuzhiyun 			tstate->flags |= QCI_LIMITS_ENFORCED;
2798*4882a593Smuzhiyun 		tstate->spc_timelimit = mi->dqi_bgrace;
2799*4882a593Smuzhiyun 		tstate->ino_timelimit = mi->dqi_igrace;
2800*4882a593Smuzhiyun 		if (dqopt->files[type]) {
2801*4882a593Smuzhiyun 			tstate->ino = dqopt->files[type]->i_ino;
2802*4882a593Smuzhiyun 			tstate->blocks = dqopt->files[type]->i_blocks;
2803*4882a593Smuzhiyun 		}
2804*4882a593Smuzhiyun 		tstate->nextents = 1;	/* We don't know... */
2805*4882a593Smuzhiyun 		spin_unlock(&dq_data_lock);
2806*4882a593Smuzhiyun 	}
2807*4882a593Smuzhiyun 	return 0;
2808*4882a593Smuzhiyun }
2809*4882a593Smuzhiyun EXPORT_SYMBOL(dquot_get_state);
2810*4882a593Smuzhiyun 
2811*4882a593Smuzhiyun /* Generic routine for setting common part of quota file information */
dquot_set_dqinfo(struct super_block * sb,int type,struct qc_info * ii)2812*4882a593Smuzhiyun int dquot_set_dqinfo(struct super_block *sb, int type, struct qc_info *ii)
2813*4882a593Smuzhiyun {
2814*4882a593Smuzhiyun 	struct mem_dqinfo *mi;
2815*4882a593Smuzhiyun 	int err = 0;
2816*4882a593Smuzhiyun 
2817*4882a593Smuzhiyun 	if ((ii->i_fieldmask & QC_WARNS_MASK) ||
2818*4882a593Smuzhiyun 	    (ii->i_fieldmask & QC_RT_SPC_TIMER))
2819*4882a593Smuzhiyun 		return -EINVAL;
2820*4882a593Smuzhiyun 	if (!sb_has_quota_active(sb, type))
2821*4882a593Smuzhiyun 		return -ESRCH;
2822*4882a593Smuzhiyun 	mi = sb_dqopt(sb)->info + type;
2823*4882a593Smuzhiyun 	if (ii->i_fieldmask & QC_FLAGS) {
2824*4882a593Smuzhiyun 		if ((ii->i_flags & QCI_ROOT_SQUASH &&
2825*4882a593Smuzhiyun 		     mi->dqi_format->qf_fmt_id != QFMT_VFS_OLD))
2826*4882a593Smuzhiyun 			return -EINVAL;
2827*4882a593Smuzhiyun 	}
2828*4882a593Smuzhiyun 	spin_lock(&dq_data_lock);
2829*4882a593Smuzhiyun 	if (ii->i_fieldmask & QC_SPC_TIMER)
2830*4882a593Smuzhiyun 		mi->dqi_bgrace = ii->i_spc_timelimit;
2831*4882a593Smuzhiyun 	if (ii->i_fieldmask & QC_INO_TIMER)
2832*4882a593Smuzhiyun 		mi->dqi_igrace = ii->i_ino_timelimit;
2833*4882a593Smuzhiyun 	if (ii->i_fieldmask & QC_FLAGS) {
2834*4882a593Smuzhiyun 		if (ii->i_flags & QCI_ROOT_SQUASH)
2835*4882a593Smuzhiyun 			mi->dqi_flags |= DQF_ROOT_SQUASH;
2836*4882a593Smuzhiyun 		else
2837*4882a593Smuzhiyun 			mi->dqi_flags &= ~DQF_ROOT_SQUASH;
2838*4882a593Smuzhiyun 	}
2839*4882a593Smuzhiyun 	spin_unlock(&dq_data_lock);
2840*4882a593Smuzhiyun 	mark_info_dirty(sb, type);
2841*4882a593Smuzhiyun 	/* Force write to disk */
2842*4882a593Smuzhiyun 	sb->dq_op->write_info(sb, type);
2843*4882a593Smuzhiyun 	return err;
2844*4882a593Smuzhiyun }
2845*4882a593Smuzhiyun EXPORT_SYMBOL(dquot_set_dqinfo);
2846*4882a593Smuzhiyun 
2847*4882a593Smuzhiyun const struct quotactl_ops dquot_quotactl_sysfile_ops = {
2848*4882a593Smuzhiyun 	.quota_enable	= dquot_quota_enable,
2849*4882a593Smuzhiyun 	.quota_disable	= dquot_quota_disable,
2850*4882a593Smuzhiyun 	.quota_sync	= dquot_quota_sync,
2851*4882a593Smuzhiyun 	.get_state	= dquot_get_state,
2852*4882a593Smuzhiyun 	.set_info	= dquot_set_dqinfo,
2853*4882a593Smuzhiyun 	.get_dqblk	= dquot_get_dqblk,
2854*4882a593Smuzhiyun 	.get_nextdqblk	= dquot_get_next_dqblk,
2855*4882a593Smuzhiyun 	.set_dqblk	= dquot_set_dqblk
2856*4882a593Smuzhiyun };
2857*4882a593Smuzhiyun EXPORT_SYMBOL(dquot_quotactl_sysfile_ops);
2858*4882a593Smuzhiyun 
do_proc_dqstats(struct ctl_table * table,int write,void * buffer,size_t * lenp,loff_t * ppos)2859*4882a593Smuzhiyun static int do_proc_dqstats(struct ctl_table *table, int write,
2860*4882a593Smuzhiyun 		     void *buffer, size_t *lenp, loff_t *ppos)
2861*4882a593Smuzhiyun {
2862*4882a593Smuzhiyun 	unsigned int type = (unsigned long *)table->data - dqstats.stat;
2863*4882a593Smuzhiyun 	s64 value = percpu_counter_sum(&dqstats.counter[type]);
2864*4882a593Smuzhiyun 
2865*4882a593Smuzhiyun 	/* Filter negative values for non-monotonic counters */
2866*4882a593Smuzhiyun 	if (value < 0 && (type == DQST_ALLOC_DQUOTS ||
2867*4882a593Smuzhiyun 			  type == DQST_FREE_DQUOTS))
2868*4882a593Smuzhiyun 		value = 0;
2869*4882a593Smuzhiyun 
2870*4882a593Smuzhiyun 	/* Update global table */
2871*4882a593Smuzhiyun 	dqstats.stat[type] = value;
2872*4882a593Smuzhiyun 	return proc_doulongvec_minmax(table, write, buffer, lenp, ppos);
2873*4882a593Smuzhiyun }
2874*4882a593Smuzhiyun 
2875*4882a593Smuzhiyun static struct ctl_table fs_dqstats_table[] = {
2876*4882a593Smuzhiyun 	{
2877*4882a593Smuzhiyun 		.procname	= "lookups",
2878*4882a593Smuzhiyun 		.data		= &dqstats.stat[DQST_LOOKUPS],
2879*4882a593Smuzhiyun 		.maxlen		= sizeof(unsigned long),
2880*4882a593Smuzhiyun 		.mode		= 0444,
2881*4882a593Smuzhiyun 		.proc_handler	= do_proc_dqstats,
2882*4882a593Smuzhiyun 	},
2883*4882a593Smuzhiyun 	{
2884*4882a593Smuzhiyun 		.procname	= "drops",
2885*4882a593Smuzhiyun 		.data		= &dqstats.stat[DQST_DROPS],
2886*4882a593Smuzhiyun 		.maxlen		= sizeof(unsigned long),
2887*4882a593Smuzhiyun 		.mode		= 0444,
2888*4882a593Smuzhiyun 		.proc_handler	= do_proc_dqstats,
2889*4882a593Smuzhiyun 	},
2890*4882a593Smuzhiyun 	{
2891*4882a593Smuzhiyun 		.procname	= "reads",
2892*4882a593Smuzhiyun 		.data		= &dqstats.stat[DQST_READS],
2893*4882a593Smuzhiyun 		.maxlen		= sizeof(unsigned long),
2894*4882a593Smuzhiyun 		.mode		= 0444,
2895*4882a593Smuzhiyun 		.proc_handler	= do_proc_dqstats,
2896*4882a593Smuzhiyun 	},
2897*4882a593Smuzhiyun 	{
2898*4882a593Smuzhiyun 		.procname	= "writes",
2899*4882a593Smuzhiyun 		.data		= &dqstats.stat[DQST_WRITES],
2900*4882a593Smuzhiyun 		.maxlen		= sizeof(unsigned long),
2901*4882a593Smuzhiyun 		.mode		= 0444,
2902*4882a593Smuzhiyun 		.proc_handler	= do_proc_dqstats,
2903*4882a593Smuzhiyun 	},
2904*4882a593Smuzhiyun 	{
2905*4882a593Smuzhiyun 		.procname	= "cache_hits",
2906*4882a593Smuzhiyun 		.data		= &dqstats.stat[DQST_CACHE_HITS],
2907*4882a593Smuzhiyun 		.maxlen		= sizeof(unsigned long),
2908*4882a593Smuzhiyun 		.mode		= 0444,
2909*4882a593Smuzhiyun 		.proc_handler	= do_proc_dqstats,
2910*4882a593Smuzhiyun 	},
2911*4882a593Smuzhiyun 	{
2912*4882a593Smuzhiyun 		.procname	= "allocated_dquots",
2913*4882a593Smuzhiyun 		.data		= &dqstats.stat[DQST_ALLOC_DQUOTS],
2914*4882a593Smuzhiyun 		.maxlen		= sizeof(unsigned long),
2915*4882a593Smuzhiyun 		.mode		= 0444,
2916*4882a593Smuzhiyun 		.proc_handler	= do_proc_dqstats,
2917*4882a593Smuzhiyun 	},
2918*4882a593Smuzhiyun 	{
2919*4882a593Smuzhiyun 		.procname	= "free_dquots",
2920*4882a593Smuzhiyun 		.data		= &dqstats.stat[DQST_FREE_DQUOTS],
2921*4882a593Smuzhiyun 		.maxlen		= sizeof(unsigned long),
2922*4882a593Smuzhiyun 		.mode		= 0444,
2923*4882a593Smuzhiyun 		.proc_handler	= do_proc_dqstats,
2924*4882a593Smuzhiyun 	},
2925*4882a593Smuzhiyun 	{
2926*4882a593Smuzhiyun 		.procname	= "syncs",
2927*4882a593Smuzhiyun 		.data		= &dqstats.stat[DQST_SYNCS],
2928*4882a593Smuzhiyun 		.maxlen		= sizeof(unsigned long),
2929*4882a593Smuzhiyun 		.mode		= 0444,
2930*4882a593Smuzhiyun 		.proc_handler	= do_proc_dqstats,
2931*4882a593Smuzhiyun 	},
2932*4882a593Smuzhiyun #ifdef CONFIG_PRINT_QUOTA_WARNING
2933*4882a593Smuzhiyun 	{
2934*4882a593Smuzhiyun 		.procname	= "warnings",
2935*4882a593Smuzhiyun 		.data		= &flag_print_warnings,
2936*4882a593Smuzhiyun 		.maxlen		= sizeof(int),
2937*4882a593Smuzhiyun 		.mode		= 0644,
2938*4882a593Smuzhiyun 		.proc_handler	= proc_dointvec,
2939*4882a593Smuzhiyun 	},
2940*4882a593Smuzhiyun #endif
2941*4882a593Smuzhiyun 	{ },
2942*4882a593Smuzhiyun };
2943*4882a593Smuzhiyun 
2944*4882a593Smuzhiyun static struct ctl_table fs_table[] = {
2945*4882a593Smuzhiyun 	{
2946*4882a593Smuzhiyun 		.procname	= "quota",
2947*4882a593Smuzhiyun 		.mode		= 0555,
2948*4882a593Smuzhiyun 		.child		= fs_dqstats_table,
2949*4882a593Smuzhiyun 	},
2950*4882a593Smuzhiyun 	{ },
2951*4882a593Smuzhiyun };
2952*4882a593Smuzhiyun 
2953*4882a593Smuzhiyun static struct ctl_table sys_table[] = {
2954*4882a593Smuzhiyun 	{
2955*4882a593Smuzhiyun 		.procname	= "fs",
2956*4882a593Smuzhiyun 		.mode		= 0555,
2957*4882a593Smuzhiyun 		.child		= fs_table,
2958*4882a593Smuzhiyun 	},
2959*4882a593Smuzhiyun 	{ },
2960*4882a593Smuzhiyun };
2961*4882a593Smuzhiyun 
dquot_init(void)2962*4882a593Smuzhiyun static int __init dquot_init(void)
2963*4882a593Smuzhiyun {
2964*4882a593Smuzhiyun 	int i, ret;
2965*4882a593Smuzhiyun 	unsigned long nr_hash, order;
2966*4882a593Smuzhiyun 
2967*4882a593Smuzhiyun 	printk(KERN_NOTICE "VFS: Disk quotas %s\n", __DQUOT_VERSION__);
2968*4882a593Smuzhiyun 
2969*4882a593Smuzhiyun 	register_sysctl_table(sys_table);
2970*4882a593Smuzhiyun 
2971*4882a593Smuzhiyun 	dquot_cachep = kmem_cache_create("dquot",
2972*4882a593Smuzhiyun 			sizeof(struct dquot), sizeof(unsigned long) * 4,
2973*4882a593Smuzhiyun 			(SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT|
2974*4882a593Smuzhiyun 				SLAB_MEM_SPREAD|SLAB_PANIC),
2975*4882a593Smuzhiyun 			NULL);
2976*4882a593Smuzhiyun 
2977*4882a593Smuzhiyun 	order = 0;
2978*4882a593Smuzhiyun 	dquot_hash = (struct hlist_head *)__get_free_pages(GFP_KERNEL, order);
2979*4882a593Smuzhiyun 	if (!dquot_hash)
2980*4882a593Smuzhiyun 		panic("Cannot create dquot hash table");
2981*4882a593Smuzhiyun 
2982*4882a593Smuzhiyun 	for (i = 0; i < _DQST_DQSTAT_LAST; i++) {
2983*4882a593Smuzhiyun 		ret = percpu_counter_init(&dqstats.counter[i], 0, GFP_KERNEL);
2984*4882a593Smuzhiyun 		if (ret)
2985*4882a593Smuzhiyun 			panic("Cannot create dquot stat counters");
2986*4882a593Smuzhiyun 	}
2987*4882a593Smuzhiyun 
2988*4882a593Smuzhiyun 	/* Find power-of-two hlist_heads which can fit into allocation */
2989*4882a593Smuzhiyun 	nr_hash = (1UL << order) * PAGE_SIZE / sizeof(struct hlist_head);
2990*4882a593Smuzhiyun 	dq_hash_bits = ilog2(nr_hash);
2991*4882a593Smuzhiyun 
2992*4882a593Smuzhiyun 	nr_hash = 1UL << dq_hash_bits;
2993*4882a593Smuzhiyun 	dq_hash_mask = nr_hash - 1;
2994*4882a593Smuzhiyun 	for (i = 0; i < nr_hash; i++)
2995*4882a593Smuzhiyun 		INIT_HLIST_HEAD(dquot_hash + i);
2996*4882a593Smuzhiyun 
2997*4882a593Smuzhiyun 	pr_info("VFS: Dquot-cache hash table entries: %ld (order %ld,"
2998*4882a593Smuzhiyun 		" %ld bytes)\n", nr_hash, order, (PAGE_SIZE << order));
2999*4882a593Smuzhiyun 
3000*4882a593Smuzhiyun 	if (register_shrinker(&dqcache_shrinker))
3001*4882a593Smuzhiyun 		panic("Cannot register dquot shrinker");
3002*4882a593Smuzhiyun 
3003*4882a593Smuzhiyun 	return 0;
3004*4882a593Smuzhiyun }
3005*4882a593Smuzhiyun fs_initcall(dquot_init);
3006