xref: /openbmc/linux/fs/quota/dquot.c (revision c469070a)
1884d179dSJan Kara /*
2884d179dSJan Kara  * Implementation of the diskquota system for the LINUX operating system. QUOTA
3884d179dSJan Kara  * is implemented using the BSD system call interface as the means of
4884d179dSJan Kara  * communication with the user level. This file contains the generic routines
5884d179dSJan Kara  * called by the different filesystems on allocation of an inode or block.
6884d179dSJan Kara  * These routines take care of the administration needed to have a consistent
7884d179dSJan Kara  * diskquota tracking system. The ideas of both user and group quotas are based
8884d179dSJan Kara  * on the Melbourne quota system as used on BSD derived systems. The internal
9884d179dSJan Kara  * implementation is based on one of the several variants of the LINUX
10884d179dSJan Kara  * inode-subsystem with added complexity of the diskquota system.
11884d179dSJan Kara  *
12884d179dSJan Kara  * Author:	Marco van Wieringen <mvw@planets.elm.net>
13884d179dSJan Kara  *
14884d179dSJan Kara  * Fixes:   Dmitry Gorodchanin <pgmdsg@ibi.com>, 11 Feb 96
15884d179dSJan Kara  *
16884d179dSJan Kara  *		Revised list management to avoid races
17884d179dSJan Kara  *		-- Bill Hawes, <whawes@star.net>, 9/98
18884d179dSJan Kara  *
19884d179dSJan Kara  *		Fixed races in dquot_transfer(), dqget() and dquot_alloc_...().
20884d179dSJan Kara  *		As the consequence the locking was moved from dquot_decr_...(),
21884d179dSJan Kara  *		dquot_incr_...() to calling functions.
22884d179dSJan Kara  *		invalidate_dquots() now writes modified dquots.
23884d179dSJan Kara  *		Serialized quota_off() and quota_on() for mount point.
24884d179dSJan Kara  *		Fixed a few bugs in grow_dquots().
25884d179dSJan Kara  *		Fixed deadlock in write_dquot() - we no longer account quotas on
26884d179dSJan Kara  *		quota files
27884d179dSJan Kara  *		remove_dquot_ref() moved to inode.c - it now traverses through inodes
28884d179dSJan Kara  *		add_dquot_ref() restarts after blocking
29884d179dSJan Kara  *		Added check for bogus uid and fixed check for group in quotactl.
30884d179dSJan Kara  *		Jan Kara, <jack@suse.cz>, sponsored by SuSE CR, 10-11/99
31884d179dSJan Kara  *
32884d179dSJan Kara  *		Used struct list_head instead of own list struct
33884d179dSJan Kara  *		Invalidation of referenced dquots is no longer possible
34884d179dSJan Kara  *		Improved free_dquots list management
35884d179dSJan Kara  *		Quota and i_blocks are now updated in one place to avoid races
36884d179dSJan Kara  *		Warnings are now delayed so we won't block in critical section
37884d179dSJan Kara  *		Write updated not to require dquot lock
38884d179dSJan Kara  *		Jan Kara, <jack@suse.cz>, 9/2000
39884d179dSJan Kara  *
40884d179dSJan Kara  *		Added dynamic quota structure allocation
41884d179dSJan Kara  *		Jan Kara <jack@suse.cz> 12/2000
42884d179dSJan Kara  *
43884d179dSJan Kara  *		Rewritten quota interface. Implemented new quota format and
44884d179dSJan Kara  *		formats registering.
45884d179dSJan Kara  *		Jan Kara, <jack@suse.cz>, 2001,2002
46884d179dSJan Kara  *
47884d179dSJan Kara  *		New SMP locking.
48884d179dSJan Kara  *		Jan Kara, <jack@suse.cz>, 10/2002
49884d179dSJan Kara  *
50884d179dSJan Kara  *		Added journalled quota support, fix lock inversion problems
51884d179dSJan Kara  *		Jan Kara, <jack@suse.cz>, 2003,2004
52884d179dSJan Kara  *
53884d179dSJan Kara  * (C) Copyright 1994 - 1997 Marco van Wieringen
54884d179dSJan Kara  */
55884d179dSJan Kara 
56884d179dSJan Kara #include <linux/errno.h>
57884d179dSJan Kara #include <linux/kernel.h>
58884d179dSJan Kara #include <linux/fs.h>
59884d179dSJan Kara #include <linux/mount.h>
60884d179dSJan Kara #include <linux/mm.h>
61884d179dSJan Kara #include <linux/time.h>
62884d179dSJan Kara #include <linux/types.h>
63884d179dSJan Kara #include <linux/string.h>
64884d179dSJan Kara #include <linux/fcntl.h>
65884d179dSJan Kara #include <linux/stat.h>
66884d179dSJan Kara #include <linux/tty.h>
67884d179dSJan Kara #include <linux/file.h>
68884d179dSJan Kara #include <linux/slab.h>
69884d179dSJan Kara #include <linux/sysctl.h>
70884d179dSJan Kara #include <linux/init.h>
71884d179dSJan Kara #include <linux/module.h>
72884d179dSJan Kara #include <linux/proc_fs.h>
73884d179dSJan Kara #include <linux/security.h>
74884d179dSJan Kara #include <linux/kmod.h>
75884d179dSJan Kara #include <linux/namei.h>
76884d179dSJan Kara #include <linux/buffer_head.h>
77884d179dSJan Kara #include <linux/capability.h>
78884d179dSJan Kara #include <linux/quotaops.h>
79884d179dSJan Kara #include <linux/writeback.h> /* for inode_lock, oddly enough.. */
80884d179dSJan Kara 
81884d179dSJan Kara #include <asm/uaccess.h>
82884d179dSJan Kara 
83884d179dSJan Kara #define __DQUOT_PARANOIA
84884d179dSJan Kara 
85884d179dSJan Kara /*
86884d179dSJan Kara  * There are three quota SMP locks. dq_list_lock protects all lists with quotas
87884d179dSJan Kara  * and quota formats, dqstats structure containing statistics about the lists
88884d179dSJan Kara  * dq_data_lock protects data from dq_dqb and also mem_dqinfo structures and
89884d179dSJan Kara  * also guards consistency of dquot->dq_dqb with inode->i_blocks, i_bytes.
90884d179dSJan Kara  * i_blocks and i_bytes updates itself are guarded by i_lock acquired directly
91884d179dSJan Kara  * in inode_add_bytes() and inode_sub_bytes(). dq_state_lock protects
92884d179dSJan Kara  * modifications of quota state (on quotaon and quotaoff) and readers who care
93884d179dSJan Kara  * about latest values take it as well.
94884d179dSJan Kara  *
95884d179dSJan Kara  * The spinlock ordering is hence: dq_data_lock > dq_list_lock > i_lock,
96884d179dSJan Kara  *   dq_list_lock > dq_state_lock
97884d179dSJan Kara  *
98884d179dSJan Kara  * Note that some things (eg. sb pointer, type, id) doesn't change during
99884d179dSJan Kara  * the life of the dquot structure and so needn't to be protected by a lock
100884d179dSJan Kara  *
101884d179dSJan Kara  * Any operation working on dquots via inode pointers must hold dqptr_sem.  If
102884d179dSJan Kara  * operation is just reading pointers from inode (or not using them at all) the
10326245c94SJan Kara  * read lock is enough. If pointers are altered function must hold write lock.
10426245c94SJan Kara  * Special care needs to be taken about S_NOQUOTA inode flag (marking that
10526245c94SJan Kara  * inode is a quota file). Functions adding pointers from inode to dquots have
10626245c94SJan Kara  * to check this flag under dqptr_sem and then (if S_NOQUOTA is not set) they
10726245c94SJan Kara  * have to do all pointer modifications before dropping dqptr_sem. This makes
10826245c94SJan Kara  * sure they cannot race with quotaon which first sets S_NOQUOTA flag and
10926245c94SJan Kara  * then drops all pointers to dquots from an inode.
110884d179dSJan Kara  *
111884d179dSJan Kara  * Each dquot has its dq_lock mutex. Locked dquots might not be referenced
112884d179dSJan Kara  * from inodes (dquot_alloc_space() and such don't check the dq_lock).
113884d179dSJan Kara  * Currently dquot is locked only when it is being read to memory (or space for
114884d179dSJan Kara  * it is being allocated) on the first dqget() and when it is being released on
115884d179dSJan Kara  * the last dqput(). The allocation and release oparations are serialized by
116884d179dSJan Kara  * the dq_lock and by checking the use count in dquot_release().  Write
117884d179dSJan Kara  * operations on dquots don't hold dq_lock as they copy data under dq_data_lock
118884d179dSJan Kara  * spinlock to internal buffers before writing.
119884d179dSJan Kara  *
120884d179dSJan Kara  * Lock ordering (including related VFS locks) is the following:
121884d179dSJan Kara  *   i_mutex > dqonoff_sem > journal_lock > dqptr_sem > dquot->dq_lock >
122884d179dSJan Kara  *   dqio_mutex
123884d179dSJan Kara  * The lock ordering of dqptr_sem imposed by quota code is only dqonoff_sem >
124884d179dSJan Kara  * dqptr_sem. But filesystem has to count with the fact that functions such as
125884d179dSJan Kara  * dquot_alloc_space() acquire dqptr_sem and they usually have to be called
126884d179dSJan Kara  * from inside a transaction to keep filesystem consistency after a crash. Also
127884d179dSJan Kara  * filesystems usually want to do some IO on dquot from ->mark_dirty which is
128884d179dSJan Kara  * called with dqptr_sem held.
129884d179dSJan Kara  * i_mutex on quota files is special (it's below dqio_mutex)
130884d179dSJan Kara  */
131884d179dSJan Kara 
132c516610cSJan Kara static __cacheline_aligned_in_smp DEFINE_SPINLOCK(dq_list_lock);
133c516610cSJan Kara static __cacheline_aligned_in_smp DEFINE_SPINLOCK(dq_state_lock);
134c516610cSJan Kara __cacheline_aligned_in_smp DEFINE_SPINLOCK(dq_data_lock);
135884d179dSJan Kara EXPORT_SYMBOL(dq_data_lock);
136884d179dSJan Kara 
137884d179dSJan Kara static char *quotatypes[] = INITQFNAMES;
138884d179dSJan Kara static struct quota_format_type *quota_formats;	/* List of registered formats */
139884d179dSJan Kara static struct quota_module_name module_names[] = INIT_QUOTA_MODULE_NAMES;
140884d179dSJan Kara 
141884d179dSJan Kara /* SLAB cache for dquot structures */
142884d179dSJan Kara static struct kmem_cache *dquot_cachep;
143884d179dSJan Kara 
144884d179dSJan Kara int register_quota_format(struct quota_format_type *fmt)
145884d179dSJan Kara {
146884d179dSJan Kara 	spin_lock(&dq_list_lock);
147884d179dSJan Kara 	fmt->qf_next = quota_formats;
148884d179dSJan Kara 	quota_formats = fmt;
149884d179dSJan Kara 	spin_unlock(&dq_list_lock);
150884d179dSJan Kara 	return 0;
151884d179dSJan Kara }
152884d179dSJan Kara EXPORT_SYMBOL(register_quota_format);
153884d179dSJan Kara 
154884d179dSJan Kara void unregister_quota_format(struct quota_format_type *fmt)
155884d179dSJan Kara {
156884d179dSJan Kara 	struct quota_format_type **actqf;
157884d179dSJan Kara 
158884d179dSJan Kara 	spin_lock(&dq_list_lock);
159268157baSJan Kara 	for (actqf = &quota_formats; *actqf && *actqf != fmt;
160268157baSJan Kara 	     actqf = &(*actqf)->qf_next)
161268157baSJan Kara 		;
162884d179dSJan Kara 	if (*actqf)
163884d179dSJan Kara 		*actqf = (*actqf)->qf_next;
164884d179dSJan Kara 	spin_unlock(&dq_list_lock);
165884d179dSJan Kara }
166884d179dSJan Kara EXPORT_SYMBOL(unregister_quota_format);
167884d179dSJan Kara 
168884d179dSJan Kara static struct quota_format_type *find_quota_format(int id)
169884d179dSJan Kara {
170884d179dSJan Kara 	struct quota_format_type *actqf;
171884d179dSJan Kara 
172884d179dSJan Kara 	spin_lock(&dq_list_lock);
173268157baSJan Kara 	for (actqf = quota_formats; actqf && actqf->qf_fmt_id != id;
174268157baSJan Kara 	     actqf = actqf->qf_next)
175268157baSJan Kara 		;
176884d179dSJan Kara 	if (!actqf || !try_module_get(actqf->qf_owner)) {
177884d179dSJan Kara 		int qm;
178884d179dSJan Kara 
179884d179dSJan Kara 		spin_unlock(&dq_list_lock);
180884d179dSJan Kara 
181268157baSJan Kara 		for (qm = 0; module_names[qm].qm_fmt_id &&
182268157baSJan Kara 			     module_names[qm].qm_fmt_id != id; qm++)
183268157baSJan Kara 			;
184268157baSJan Kara 		if (!module_names[qm].qm_fmt_id ||
185268157baSJan Kara 		    request_module(module_names[qm].qm_mod_name))
186884d179dSJan Kara 			return NULL;
187884d179dSJan Kara 
188884d179dSJan Kara 		spin_lock(&dq_list_lock);
189268157baSJan Kara 		for (actqf = quota_formats; actqf && actqf->qf_fmt_id != id;
190268157baSJan Kara 		     actqf = actqf->qf_next)
191268157baSJan Kara 			;
192884d179dSJan Kara 		if (actqf && !try_module_get(actqf->qf_owner))
193884d179dSJan Kara 			actqf = NULL;
194884d179dSJan Kara 	}
195884d179dSJan Kara 	spin_unlock(&dq_list_lock);
196884d179dSJan Kara 	return actqf;
197884d179dSJan Kara }
198884d179dSJan Kara 
199884d179dSJan Kara static void put_quota_format(struct quota_format_type *fmt)
200884d179dSJan Kara {
201884d179dSJan Kara 	module_put(fmt->qf_owner);
202884d179dSJan Kara }
203884d179dSJan Kara 
204884d179dSJan Kara /*
205884d179dSJan Kara  * Dquot List Management:
206884d179dSJan Kara  * The quota code uses three lists for dquot management: the inuse_list,
207884d179dSJan Kara  * free_dquots, and dquot_hash[] array. A single dquot structure may be
208884d179dSJan Kara  * on all three lists, depending on its current state.
209884d179dSJan Kara  *
210884d179dSJan Kara  * All dquots are placed to the end of inuse_list when first created, and this
211884d179dSJan Kara  * list is used for invalidate operation, which must look at every dquot.
212884d179dSJan Kara  *
213884d179dSJan Kara  * Unused dquots (dq_count == 0) are added to the free_dquots list when freed,
214884d179dSJan Kara  * and this list is searched whenever we need an available dquot.  Dquots are
215884d179dSJan Kara  * removed from the list as soon as they are used again, and
216884d179dSJan Kara  * dqstats.free_dquots gives the number of dquots on the list. When
217884d179dSJan Kara  * dquot is invalidated it's completely released from memory.
218884d179dSJan Kara  *
219884d179dSJan Kara  * Dquots with a specific identity (device, type and id) are placed on
220884d179dSJan Kara  * one of the dquot_hash[] hash chains. The provides an efficient search
221884d179dSJan Kara  * mechanism to locate a specific dquot.
222884d179dSJan Kara  */
223884d179dSJan Kara 
224884d179dSJan Kara static LIST_HEAD(inuse_list);
225884d179dSJan Kara static LIST_HEAD(free_dquots);
226884d179dSJan Kara static unsigned int dq_hash_bits, dq_hash_mask;
227884d179dSJan Kara static struct hlist_head *dquot_hash;
228884d179dSJan Kara 
229884d179dSJan Kara struct dqstats dqstats;
230884d179dSJan Kara EXPORT_SYMBOL(dqstats);
231884d179dSJan Kara 
232884d179dSJan Kara static inline unsigned int
233884d179dSJan Kara hashfn(const struct super_block *sb, unsigned int id, int type)
234884d179dSJan Kara {
235884d179dSJan Kara 	unsigned long tmp;
236884d179dSJan Kara 
237884d179dSJan Kara 	tmp = (((unsigned long)sb>>L1_CACHE_SHIFT) ^ id) * (MAXQUOTAS - type);
238884d179dSJan Kara 	return (tmp + (tmp >> dq_hash_bits)) & dq_hash_mask;
239884d179dSJan Kara }
240884d179dSJan Kara 
241884d179dSJan Kara /*
242884d179dSJan Kara  * Following list functions expect dq_list_lock to be held
243884d179dSJan Kara  */
244884d179dSJan Kara static inline void insert_dquot_hash(struct dquot *dquot)
245884d179dSJan Kara {
246268157baSJan Kara 	struct hlist_head *head;
247268157baSJan Kara 	head = dquot_hash + hashfn(dquot->dq_sb, dquot->dq_id, dquot->dq_type);
248884d179dSJan Kara 	hlist_add_head(&dquot->dq_hash, head);
249884d179dSJan Kara }
250884d179dSJan Kara 
251884d179dSJan Kara static inline void remove_dquot_hash(struct dquot *dquot)
252884d179dSJan Kara {
253884d179dSJan Kara 	hlist_del_init(&dquot->dq_hash);
254884d179dSJan Kara }
255884d179dSJan Kara 
2567a2435d8SJan Kara static struct dquot *find_dquot(unsigned int hashent, struct super_block *sb,
2577a2435d8SJan Kara 				unsigned int id, int type)
258884d179dSJan Kara {
259884d179dSJan Kara 	struct hlist_node *node;
260884d179dSJan Kara 	struct dquot *dquot;
261884d179dSJan Kara 
262884d179dSJan Kara 	hlist_for_each (node, dquot_hash+hashent) {
263884d179dSJan Kara 		dquot = hlist_entry(node, struct dquot, dq_hash);
264268157baSJan Kara 		if (dquot->dq_sb == sb && dquot->dq_id == id &&
265268157baSJan Kara 		    dquot->dq_type == type)
266884d179dSJan Kara 			return dquot;
267884d179dSJan Kara 	}
268dd6f3c6dSJan Kara 	return NULL;
269884d179dSJan Kara }
270884d179dSJan Kara 
271884d179dSJan Kara /* Add a dquot to the tail of the free list */
272884d179dSJan Kara static inline void put_dquot_last(struct dquot *dquot)
273884d179dSJan Kara {
274884d179dSJan Kara 	list_add_tail(&dquot->dq_free, &free_dquots);
275884d179dSJan Kara 	dqstats.free_dquots++;
276884d179dSJan Kara }
277884d179dSJan Kara 
278884d179dSJan Kara static inline void remove_free_dquot(struct dquot *dquot)
279884d179dSJan Kara {
280884d179dSJan Kara 	if (list_empty(&dquot->dq_free))
281884d179dSJan Kara 		return;
282884d179dSJan Kara 	list_del_init(&dquot->dq_free);
283884d179dSJan Kara 	dqstats.free_dquots--;
284884d179dSJan Kara }
285884d179dSJan Kara 
286884d179dSJan Kara static inline void put_inuse(struct dquot *dquot)
287884d179dSJan Kara {
288884d179dSJan Kara 	/* We add to the back of inuse list so we don't have to restart
289884d179dSJan Kara 	 * when traversing this list and we block */
290884d179dSJan Kara 	list_add_tail(&dquot->dq_inuse, &inuse_list);
291884d179dSJan Kara 	dqstats.allocated_dquots++;
292884d179dSJan Kara }
293884d179dSJan Kara 
294884d179dSJan Kara static inline void remove_inuse(struct dquot *dquot)
295884d179dSJan Kara {
296884d179dSJan Kara 	dqstats.allocated_dquots--;
297884d179dSJan Kara 	list_del(&dquot->dq_inuse);
298884d179dSJan Kara }
299884d179dSJan Kara /*
300884d179dSJan Kara  * End of list functions needing dq_list_lock
301884d179dSJan Kara  */
302884d179dSJan Kara 
303884d179dSJan Kara static void wait_on_dquot(struct dquot *dquot)
304884d179dSJan Kara {
305884d179dSJan Kara 	mutex_lock(&dquot->dq_lock);
306884d179dSJan Kara 	mutex_unlock(&dquot->dq_lock);
307884d179dSJan Kara }
308884d179dSJan Kara 
309884d179dSJan Kara static inline int dquot_dirty(struct dquot *dquot)
310884d179dSJan Kara {
311884d179dSJan Kara 	return test_bit(DQ_MOD_B, &dquot->dq_flags);
312884d179dSJan Kara }
313884d179dSJan Kara 
314884d179dSJan Kara static inline int mark_dquot_dirty(struct dquot *dquot)
315884d179dSJan Kara {
316884d179dSJan Kara 	return dquot->dq_sb->dq_op->mark_dirty(dquot);
317884d179dSJan Kara }
318884d179dSJan Kara 
319884d179dSJan Kara int dquot_mark_dquot_dirty(struct dquot *dquot)
320884d179dSJan Kara {
321884d179dSJan Kara 	spin_lock(&dq_list_lock);
322884d179dSJan Kara 	if (!test_and_set_bit(DQ_MOD_B, &dquot->dq_flags))
323884d179dSJan Kara 		list_add(&dquot->dq_dirty, &sb_dqopt(dquot->dq_sb)->
324884d179dSJan Kara 				info[dquot->dq_type].dqi_dirty_list);
325884d179dSJan Kara 	spin_unlock(&dq_list_lock);
326884d179dSJan Kara 	return 0;
327884d179dSJan Kara }
328884d179dSJan Kara EXPORT_SYMBOL(dquot_mark_dquot_dirty);
329884d179dSJan Kara 
330dc52dd3aSDmitry Monakhov /* Dirtify all the dquots - this can block when journalling */
331dc52dd3aSDmitry Monakhov static inline int mark_all_dquot_dirty(struct dquot * const *dquot)
332dc52dd3aSDmitry Monakhov {
333dc52dd3aSDmitry Monakhov 	int ret, err, cnt;
334dc52dd3aSDmitry Monakhov 
335dc52dd3aSDmitry Monakhov 	ret = err = 0;
336dc52dd3aSDmitry Monakhov 	for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
337dc52dd3aSDmitry Monakhov 		if (dquot[cnt])
338dc52dd3aSDmitry Monakhov 			/* Even in case of error we have to continue */
339dc52dd3aSDmitry Monakhov 			ret = mark_dquot_dirty(dquot[cnt]);
340dc52dd3aSDmitry Monakhov 		if (!err)
341dc52dd3aSDmitry Monakhov 			err = ret;
342dc52dd3aSDmitry Monakhov 	}
343dc52dd3aSDmitry Monakhov 	return err;
344dc52dd3aSDmitry Monakhov }
345dc52dd3aSDmitry Monakhov 
346dc52dd3aSDmitry Monakhov static inline void dqput_all(struct dquot **dquot)
347dc52dd3aSDmitry Monakhov {
348dc52dd3aSDmitry Monakhov 	unsigned int cnt;
349dc52dd3aSDmitry Monakhov 
350dc52dd3aSDmitry Monakhov 	for (cnt = 0; cnt < MAXQUOTAS; cnt++)
351dc52dd3aSDmitry Monakhov 		dqput(dquot[cnt]);
352dc52dd3aSDmitry Monakhov }
353dc52dd3aSDmitry Monakhov 
354884d179dSJan Kara /* This function needs dq_list_lock */
355884d179dSJan Kara static inline int clear_dquot_dirty(struct dquot *dquot)
356884d179dSJan Kara {
357884d179dSJan Kara 	if (!test_and_clear_bit(DQ_MOD_B, &dquot->dq_flags))
358884d179dSJan Kara 		return 0;
359884d179dSJan Kara 	list_del_init(&dquot->dq_dirty);
360884d179dSJan Kara 	return 1;
361884d179dSJan Kara }
362884d179dSJan Kara 
363884d179dSJan Kara void mark_info_dirty(struct super_block *sb, int type)
364884d179dSJan Kara {
365884d179dSJan Kara 	set_bit(DQF_INFO_DIRTY_B, &sb_dqopt(sb)->info[type].dqi_flags);
366884d179dSJan Kara }
367884d179dSJan Kara EXPORT_SYMBOL(mark_info_dirty);
368884d179dSJan Kara 
369884d179dSJan Kara /*
370884d179dSJan Kara  *	Read dquot from disk and alloc space for it
371884d179dSJan Kara  */
372884d179dSJan Kara 
373884d179dSJan Kara int dquot_acquire(struct dquot *dquot)
374884d179dSJan Kara {
375884d179dSJan Kara 	int ret = 0, ret2 = 0;
376884d179dSJan Kara 	struct quota_info *dqopt = sb_dqopt(dquot->dq_sb);
377884d179dSJan Kara 
378884d179dSJan Kara 	mutex_lock(&dquot->dq_lock);
379884d179dSJan Kara 	mutex_lock(&dqopt->dqio_mutex);
380884d179dSJan Kara 	if (!test_bit(DQ_READ_B, &dquot->dq_flags))
381884d179dSJan Kara 		ret = dqopt->ops[dquot->dq_type]->read_dqblk(dquot);
382884d179dSJan Kara 	if (ret < 0)
383884d179dSJan Kara 		goto out_iolock;
384884d179dSJan Kara 	set_bit(DQ_READ_B, &dquot->dq_flags);
385884d179dSJan Kara 	/* Instantiate dquot if needed */
386884d179dSJan Kara 	if (!test_bit(DQ_ACTIVE_B, &dquot->dq_flags) && !dquot->dq_off) {
387884d179dSJan Kara 		ret = dqopt->ops[dquot->dq_type]->commit_dqblk(dquot);
388884d179dSJan Kara 		/* Write the info if needed */
389268157baSJan Kara 		if (info_dirty(&dqopt->info[dquot->dq_type])) {
390268157baSJan Kara 			ret2 = dqopt->ops[dquot->dq_type]->write_file_info(
391268157baSJan Kara 						dquot->dq_sb, dquot->dq_type);
392268157baSJan Kara 		}
393884d179dSJan Kara 		if (ret < 0)
394884d179dSJan Kara 			goto out_iolock;
395884d179dSJan Kara 		if (ret2 < 0) {
396884d179dSJan Kara 			ret = ret2;
397884d179dSJan Kara 			goto out_iolock;
398884d179dSJan Kara 		}
399884d179dSJan Kara 	}
400884d179dSJan Kara 	set_bit(DQ_ACTIVE_B, &dquot->dq_flags);
401884d179dSJan Kara out_iolock:
402884d179dSJan Kara 	mutex_unlock(&dqopt->dqio_mutex);
403884d179dSJan Kara 	mutex_unlock(&dquot->dq_lock);
404884d179dSJan Kara 	return ret;
405884d179dSJan Kara }
406884d179dSJan Kara EXPORT_SYMBOL(dquot_acquire);
407884d179dSJan Kara 
408884d179dSJan Kara /*
409884d179dSJan Kara  *	Write dquot to disk
410884d179dSJan Kara  */
411884d179dSJan Kara int dquot_commit(struct dquot *dquot)
412884d179dSJan Kara {
413884d179dSJan Kara 	int ret = 0, ret2 = 0;
414884d179dSJan Kara 	struct quota_info *dqopt = sb_dqopt(dquot->dq_sb);
415884d179dSJan Kara 
416884d179dSJan Kara 	mutex_lock(&dqopt->dqio_mutex);
417884d179dSJan Kara 	spin_lock(&dq_list_lock);
418884d179dSJan Kara 	if (!clear_dquot_dirty(dquot)) {
419884d179dSJan Kara 		spin_unlock(&dq_list_lock);
420884d179dSJan Kara 		goto out_sem;
421884d179dSJan Kara 	}
422884d179dSJan Kara 	spin_unlock(&dq_list_lock);
423884d179dSJan Kara 	/* Inactive dquot can be only if there was error during read/init
424884d179dSJan Kara 	 * => we have better not writing it */
425884d179dSJan Kara 	if (test_bit(DQ_ACTIVE_B, &dquot->dq_flags)) {
426884d179dSJan Kara 		ret = dqopt->ops[dquot->dq_type]->commit_dqblk(dquot);
427268157baSJan Kara 		if (info_dirty(&dqopt->info[dquot->dq_type])) {
428268157baSJan Kara 			ret2 = dqopt->ops[dquot->dq_type]->write_file_info(
429268157baSJan Kara 						dquot->dq_sb, dquot->dq_type);
430268157baSJan Kara 		}
431884d179dSJan Kara 		if (ret >= 0)
432884d179dSJan Kara 			ret = ret2;
433884d179dSJan Kara 	}
434884d179dSJan Kara out_sem:
435884d179dSJan Kara 	mutex_unlock(&dqopt->dqio_mutex);
436884d179dSJan Kara 	return ret;
437884d179dSJan Kara }
438884d179dSJan Kara EXPORT_SYMBOL(dquot_commit);
439884d179dSJan Kara 
440884d179dSJan Kara /*
441884d179dSJan Kara  *	Release dquot
442884d179dSJan Kara  */
443884d179dSJan Kara int dquot_release(struct dquot *dquot)
444884d179dSJan Kara {
445884d179dSJan Kara 	int ret = 0, ret2 = 0;
446884d179dSJan Kara 	struct quota_info *dqopt = sb_dqopt(dquot->dq_sb);
447884d179dSJan Kara 
448884d179dSJan Kara 	mutex_lock(&dquot->dq_lock);
449884d179dSJan Kara 	/* Check whether we are not racing with some other dqget() */
450884d179dSJan Kara 	if (atomic_read(&dquot->dq_count) > 1)
451884d179dSJan Kara 		goto out_dqlock;
452884d179dSJan Kara 	mutex_lock(&dqopt->dqio_mutex);
453884d179dSJan Kara 	if (dqopt->ops[dquot->dq_type]->release_dqblk) {
454884d179dSJan Kara 		ret = dqopt->ops[dquot->dq_type]->release_dqblk(dquot);
455884d179dSJan Kara 		/* Write the info */
456268157baSJan Kara 		if (info_dirty(&dqopt->info[dquot->dq_type])) {
457268157baSJan Kara 			ret2 = dqopt->ops[dquot->dq_type]->write_file_info(
458268157baSJan Kara 						dquot->dq_sb, dquot->dq_type);
459268157baSJan Kara 		}
460884d179dSJan Kara 		if (ret >= 0)
461884d179dSJan Kara 			ret = ret2;
462884d179dSJan Kara 	}
463884d179dSJan Kara 	clear_bit(DQ_ACTIVE_B, &dquot->dq_flags);
464884d179dSJan Kara 	mutex_unlock(&dqopt->dqio_mutex);
465884d179dSJan Kara out_dqlock:
466884d179dSJan Kara 	mutex_unlock(&dquot->dq_lock);
467884d179dSJan Kara 	return ret;
468884d179dSJan Kara }
469884d179dSJan Kara EXPORT_SYMBOL(dquot_release);
470884d179dSJan Kara 
471884d179dSJan Kara void dquot_destroy(struct dquot *dquot)
472884d179dSJan Kara {
473884d179dSJan Kara 	kmem_cache_free(dquot_cachep, dquot);
474884d179dSJan Kara }
475884d179dSJan Kara EXPORT_SYMBOL(dquot_destroy);
476884d179dSJan Kara 
477884d179dSJan Kara static inline void do_destroy_dquot(struct dquot *dquot)
478884d179dSJan Kara {
479884d179dSJan Kara 	dquot->dq_sb->dq_op->destroy_dquot(dquot);
480884d179dSJan Kara }
481884d179dSJan Kara 
482884d179dSJan Kara /* Invalidate all dquots on the list. Note that this function is called after
483884d179dSJan Kara  * quota is disabled and pointers from inodes removed so there cannot be new
484884d179dSJan Kara  * quota users. There can still be some users of quotas due to inodes being
485884d179dSJan Kara  * just deleted or pruned by prune_icache() (those are not attached to any
486884d179dSJan Kara  * list) or parallel quotactl call. We have to wait for such users.
487884d179dSJan Kara  */
488884d179dSJan Kara static void invalidate_dquots(struct super_block *sb, int type)
489884d179dSJan Kara {
490884d179dSJan Kara 	struct dquot *dquot, *tmp;
491884d179dSJan Kara 
492884d179dSJan Kara restart:
493884d179dSJan Kara 	spin_lock(&dq_list_lock);
494884d179dSJan Kara 	list_for_each_entry_safe(dquot, tmp, &inuse_list, dq_inuse) {
495884d179dSJan Kara 		if (dquot->dq_sb != sb)
496884d179dSJan Kara 			continue;
497884d179dSJan Kara 		if (dquot->dq_type != type)
498884d179dSJan Kara 			continue;
499884d179dSJan Kara 		/* Wait for dquot users */
500884d179dSJan Kara 		if (atomic_read(&dquot->dq_count)) {
501884d179dSJan Kara 			DEFINE_WAIT(wait);
502884d179dSJan Kara 
503884d179dSJan Kara 			atomic_inc(&dquot->dq_count);
504884d179dSJan Kara 			prepare_to_wait(&dquot->dq_wait_unused, &wait,
505884d179dSJan Kara 					TASK_UNINTERRUPTIBLE);
506884d179dSJan Kara 			spin_unlock(&dq_list_lock);
507884d179dSJan Kara 			/* Once dqput() wakes us up, we know it's time to free
508884d179dSJan Kara 			 * the dquot.
509884d179dSJan Kara 			 * IMPORTANT: we rely on the fact that there is always
510884d179dSJan Kara 			 * at most one process waiting for dquot to free.
511884d179dSJan Kara 			 * Otherwise dq_count would be > 1 and we would never
512884d179dSJan Kara 			 * wake up.
513884d179dSJan Kara 			 */
514884d179dSJan Kara 			if (atomic_read(&dquot->dq_count) > 1)
515884d179dSJan Kara 				schedule();
516884d179dSJan Kara 			finish_wait(&dquot->dq_wait_unused, &wait);
517884d179dSJan Kara 			dqput(dquot);
518884d179dSJan Kara 			/* At this moment dquot() need not exist (it could be
519884d179dSJan Kara 			 * reclaimed by prune_dqcache(). Hence we must
520884d179dSJan Kara 			 * restart. */
521884d179dSJan Kara 			goto restart;
522884d179dSJan Kara 		}
523884d179dSJan Kara 		/*
524884d179dSJan Kara 		 * Quota now has no users and it has been written on last
525884d179dSJan Kara 		 * dqput()
526884d179dSJan Kara 		 */
527884d179dSJan Kara 		remove_dquot_hash(dquot);
528884d179dSJan Kara 		remove_free_dquot(dquot);
529884d179dSJan Kara 		remove_inuse(dquot);
530884d179dSJan Kara 		do_destroy_dquot(dquot);
531884d179dSJan Kara 	}
532884d179dSJan Kara 	spin_unlock(&dq_list_lock);
533884d179dSJan Kara }
534884d179dSJan Kara 
535884d179dSJan Kara /* Call callback for every active dquot on given filesystem */
536884d179dSJan Kara int dquot_scan_active(struct super_block *sb,
537884d179dSJan Kara 		      int (*fn)(struct dquot *dquot, unsigned long priv),
538884d179dSJan Kara 		      unsigned long priv)
539884d179dSJan Kara {
540884d179dSJan Kara 	struct dquot *dquot, *old_dquot = NULL;
541884d179dSJan Kara 	int ret = 0;
542884d179dSJan Kara 
543884d179dSJan Kara 	mutex_lock(&sb_dqopt(sb)->dqonoff_mutex);
544884d179dSJan Kara 	spin_lock(&dq_list_lock);
545884d179dSJan Kara 	list_for_each_entry(dquot, &inuse_list, dq_inuse) {
546884d179dSJan Kara 		if (!test_bit(DQ_ACTIVE_B, &dquot->dq_flags))
547884d179dSJan Kara 			continue;
548884d179dSJan Kara 		if (dquot->dq_sb != sb)
549884d179dSJan Kara 			continue;
550884d179dSJan Kara 		/* Now we have active dquot so we can just increase use count */
551884d179dSJan Kara 		atomic_inc(&dquot->dq_count);
552884d179dSJan Kara 		dqstats.lookups++;
553884d179dSJan Kara 		spin_unlock(&dq_list_lock);
554884d179dSJan Kara 		dqput(old_dquot);
555884d179dSJan Kara 		old_dquot = dquot;
556884d179dSJan Kara 		ret = fn(dquot, priv);
557884d179dSJan Kara 		if (ret < 0)
558884d179dSJan Kara 			goto out;
559884d179dSJan Kara 		spin_lock(&dq_list_lock);
560884d179dSJan Kara 		/* We are safe to continue now because our dquot could not
561884d179dSJan Kara 		 * be moved out of the inuse list while we hold the reference */
562884d179dSJan Kara 	}
563884d179dSJan Kara 	spin_unlock(&dq_list_lock);
564884d179dSJan Kara out:
565884d179dSJan Kara 	dqput(old_dquot);
566884d179dSJan Kara 	mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
567884d179dSJan Kara 	return ret;
568884d179dSJan Kara }
569884d179dSJan Kara EXPORT_SYMBOL(dquot_scan_active);
570884d179dSJan Kara 
571884d179dSJan Kara int vfs_quota_sync(struct super_block *sb, int type)
572884d179dSJan Kara {
573884d179dSJan Kara 	struct list_head *dirty;
574884d179dSJan Kara 	struct dquot *dquot;
575884d179dSJan Kara 	struct quota_info *dqopt = sb_dqopt(sb);
576884d179dSJan Kara 	int cnt;
577884d179dSJan Kara 
578884d179dSJan Kara 	mutex_lock(&dqopt->dqonoff_mutex);
579884d179dSJan Kara 	for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
580884d179dSJan Kara 		if (type != -1 && cnt != type)
581884d179dSJan Kara 			continue;
582884d179dSJan Kara 		if (!sb_has_quota_active(sb, cnt))
583884d179dSJan Kara 			continue;
584884d179dSJan Kara 		spin_lock(&dq_list_lock);
585884d179dSJan Kara 		dirty = &dqopt->info[cnt].dqi_dirty_list;
586884d179dSJan Kara 		while (!list_empty(dirty)) {
587268157baSJan Kara 			dquot = list_first_entry(dirty, struct dquot,
588268157baSJan Kara 						 dq_dirty);
589884d179dSJan Kara 			/* Dirty and inactive can be only bad dquot... */
590884d179dSJan Kara 			if (!test_bit(DQ_ACTIVE_B, &dquot->dq_flags)) {
591884d179dSJan Kara 				clear_dquot_dirty(dquot);
592884d179dSJan Kara 				continue;
593884d179dSJan Kara 			}
594884d179dSJan Kara 			/* Now we have active dquot from which someone is
595884d179dSJan Kara  			 * holding reference so we can safely just increase
596884d179dSJan Kara 			 * use count */
597884d179dSJan Kara 			atomic_inc(&dquot->dq_count);
598884d179dSJan Kara 			dqstats.lookups++;
599884d179dSJan Kara 			spin_unlock(&dq_list_lock);
600884d179dSJan Kara 			sb->dq_op->write_dquot(dquot);
601884d179dSJan Kara 			dqput(dquot);
602884d179dSJan Kara 			spin_lock(&dq_list_lock);
603884d179dSJan Kara 		}
604884d179dSJan Kara 		spin_unlock(&dq_list_lock);
605884d179dSJan Kara 	}
606884d179dSJan Kara 
607884d179dSJan Kara 	for (cnt = 0; cnt < MAXQUOTAS; cnt++)
608884d179dSJan Kara 		if ((cnt == type || type == -1) && sb_has_quota_active(sb, cnt)
609884d179dSJan Kara 		    && info_dirty(&dqopt->info[cnt]))
610884d179dSJan Kara 			sb->dq_op->write_info(sb, cnt);
611884d179dSJan Kara 	spin_lock(&dq_list_lock);
612884d179dSJan Kara 	dqstats.syncs++;
613884d179dSJan Kara 	spin_unlock(&dq_list_lock);
614884d179dSJan Kara 	mutex_unlock(&dqopt->dqonoff_mutex);
615884d179dSJan Kara 
616884d179dSJan Kara 	return 0;
617884d179dSJan Kara }
618884d179dSJan Kara EXPORT_SYMBOL(vfs_quota_sync);
619884d179dSJan Kara 
620884d179dSJan Kara /* Free unused dquots from cache */
621884d179dSJan Kara static void prune_dqcache(int count)
622884d179dSJan Kara {
623884d179dSJan Kara 	struct list_head *head;
624884d179dSJan Kara 	struct dquot *dquot;
625884d179dSJan Kara 
626884d179dSJan Kara 	head = free_dquots.prev;
627884d179dSJan Kara 	while (head != &free_dquots && count) {
628884d179dSJan Kara 		dquot = list_entry(head, struct dquot, dq_free);
629884d179dSJan Kara 		remove_dquot_hash(dquot);
630884d179dSJan Kara 		remove_free_dquot(dquot);
631884d179dSJan Kara 		remove_inuse(dquot);
632884d179dSJan Kara 		do_destroy_dquot(dquot);
633884d179dSJan Kara 		count--;
634884d179dSJan Kara 		head = free_dquots.prev;
635884d179dSJan Kara 	}
636884d179dSJan Kara }
637884d179dSJan Kara 
638884d179dSJan Kara /*
639884d179dSJan Kara  * This is called from kswapd when we think we need some
640884d179dSJan Kara  * more memory
641884d179dSJan Kara  */
642884d179dSJan Kara 
643884d179dSJan Kara static int shrink_dqcache_memory(int nr, gfp_t gfp_mask)
644884d179dSJan Kara {
645884d179dSJan Kara 	if (nr) {
646884d179dSJan Kara 		spin_lock(&dq_list_lock);
647884d179dSJan Kara 		prune_dqcache(nr);
648884d179dSJan Kara 		spin_unlock(&dq_list_lock);
649884d179dSJan Kara 	}
650884d179dSJan Kara 	return (dqstats.free_dquots / 100) * sysctl_vfs_cache_pressure;
651884d179dSJan Kara }
652884d179dSJan Kara 
653884d179dSJan Kara static struct shrinker dqcache_shrinker = {
654884d179dSJan Kara 	.shrink = shrink_dqcache_memory,
655884d179dSJan Kara 	.seeks = DEFAULT_SEEKS,
656884d179dSJan Kara };
657884d179dSJan Kara 
658884d179dSJan Kara /*
659884d179dSJan Kara  * Put reference to dquot
660884d179dSJan Kara  * NOTE: If you change this function please check whether dqput_blocks() works right...
661884d179dSJan Kara  */
662884d179dSJan Kara void dqput(struct dquot *dquot)
663884d179dSJan Kara {
664884d179dSJan Kara 	int ret;
665884d179dSJan Kara 
666884d179dSJan Kara 	if (!dquot)
667884d179dSJan Kara 		return;
668884d179dSJan Kara #ifdef __DQUOT_PARANOIA
669884d179dSJan Kara 	if (!atomic_read(&dquot->dq_count)) {
670884d179dSJan Kara 		printk("VFS: dqput: trying to free free dquot\n");
671884d179dSJan Kara 		printk("VFS: device %s, dquot of %s %d\n",
672884d179dSJan Kara 			dquot->dq_sb->s_id,
673884d179dSJan Kara 			quotatypes[dquot->dq_type],
674884d179dSJan Kara 			dquot->dq_id);
675884d179dSJan Kara 		BUG();
676884d179dSJan Kara 	}
677884d179dSJan Kara #endif
678884d179dSJan Kara 
679884d179dSJan Kara 	spin_lock(&dq_list_lock);
680884d179dSJan Kara 	dqstats.drops++;
681884d179dSJan Kara 	spin_unlock(&dq_list_lock);
682884d179dSJan Kara we_slept:
683884d179dSJan Kara 	spin_lock(&dq_list_lock);
684884d179dSJan Kara 	if (atomic_read(&dquot->dq_count) > 1) {
685884d179dSJan Kara 		/* We have more than one user... nothing to do */
686884d179dSJan Kara 		atomic_dec(&dquot->dq_count);
687884d179dSJan Kara 		/* Releasing dquot during quotaoff phase? */
688884d179dSJan Kara 		if (!sb_has_quota_active(dquot->dq_sb, dquot->dq_type) &&
689884d179dSJan Kara 		    atomic_read(&dquot->dq_count) == 1)
690884d179dSJan Kara 			wake_up(&dquot->dq_wait_unused);
691884d179dSJan Kara 		spin_unlock(&dq_list_lock);
692884d179dSJan Kara 		return;
693884d179dSJan Kara 	}
694884d179dSJan Kara 	/* Need to release dquot? */
695884d179dSJan Kara 	if (test_bit(DQ_ACTIVE_B, &dquot->dq_flags) && dquot_dirty(dquot)) {
696884d179dSJan Kara 		spin_unlock(&dq_list_lock);
697884d179dSJan Kara 		/* Commit dquot before releasing */
698884d179dSJan Kara 		ret = dquot->dq_sb->dq_op->write_dquot(dquot);
699884d179dSJan Kara 		if (ret < 0) {
700884d179dSJan Kara 			printk(KERN_ERR "VFS: cannot write quota structure on "
701884d179dSJan Kara 				"device %s (error %d). Quota may get out of "
702884d179dSJan Kara 				"sync!\n", dquot->dq_sb->s_id, ret);
703884d179dSJan Kara 			/*
704884d179dSJan Kara 			 * We clear dirty bit anyway, so that we avoid
705884d179dSJan Kara 			 * infinite loop here
706884d179dSJan Kara 			 */
707884d179dSJan Kara 			spin_lock(&dq_list_lock);
708884d179dSJan Kara 			clear_dquot_dirty(dquot);
709884d179dSJan Kara 			spin_unlock(&dq_list_lock);
710884d179dSJan Kara 		}
711884d179dSJan Kara 		goto we_slept;
712884d179dSJan Kara 	}
713884d179dSJan Kara 	/* Clear flag in case dquot was inactive (something bad happened) */
714884d179dSJan Kara 	clear_dquot_dirty(dquot);
715884d179dSJan Kara 	if (test_bit(DQ_ACTIVE_B, &dquot->dq_flags)) {
716884d179dSJan Kara 		spin_unlock(&dq_list_lock);
717884d179dSJan Kara 		dquot->dq_sb->dq_op->release_dquot(dquot);
718884d179dSJan Kara 		goto we_slept;
719884d179dSJan Kara 	}
720884d179dSJan Kara 	atomic_dec(&dquot->dq_count);
721884d179dSJan Kara #ifdef __DQUOT_PARANOIA
722884d179dSJan Kara 	/* sanity check */
723884d179dSJan Kara 	BUG_ON(!list_empty(&dquot->dq_free));
724884d179dSJan Kara #endif
725884d179dSJan Kara 	put_dquot_last(dquot);
726884d179dSJan Kara 	spin_unlock(&dq_list_lock);
727884d179dSJan Kara }
728884d179dSJan Kara EXPORT_SYMBOL(dqput);
729884d179dSJan Kara 
730884d179dSJan Kara struct dquot *dquot_alloc(struct super_block *sb, int type)
731884d179dSJan Kara {
732884d179dSJan Kara 	return kmem_cache_zalloc(dquot_cachep, GFP_NOFS);
733884d179dSJan Kara }
734884d179dSJan Kara EXPORT_SYMBOL(dquot_alloc);
735884d179dSJan Kara 
736884d179dSJan Kara static struct dquot *get_empty_dquot(struct super_block *sb, int type)
737884d179dSJan Kara {
738884d179dSJan Kara 	struct dquot *dquot;
739884d179dSJan Kara 
740884d179dSJan Kara 	dquot = sb->dq_op->alloc_dquot(sb, type);
741884d179dSJan Kara 	if(!dquot)
742dd6f3c6dSJan Kara 		return NULL;
743884d179dSJan Kara 
744884d179dSJan Kara 	mutex_init(&dquot->dq_lock);
745884d179dSJan Kara 	INIT_LIST_HEAD(&dquot->dq_free);
746884d179dSJan Kara 	INIT_LIST_HEAD(&dquot->dq_inuse);
747884d179dSJan Kara 	INIT_HLIST_NODE(&dquot->dq_hash);
748884d179dSJan Kara 	INIT_LIST_HEAD(&dquot->dq_dirty);
749884d179dSJan Kara 	init_waitqueue_head(&dquot->dq_wait_unused);
750884d179dSJan Kara 	dquot->dq_sb = sb;
751884d179dSJan Kara 	dquot->dq_type = type;
752884d179dSJan Kara 	atomic_set(&dquot->dq_count, 1);
753884d179dSJan Kara 
754884d179dSJan Kara 	return dquot;
755884d179dSJan Kara }
756884d179dSJan Kara 
757884d179dSJan Kara /*
758884d179dSJan Kara  * Get reference to dquot
759884d179dSJan Kara  *
760884d179dSJan Kara  * Locking is slightly tricky here. We are guarded from parallel quotaoff()
761884d179dSJan Kara  * destroying our dquot by:
762884d179dSJan Kara  *   a) checking for quota flags under dq_list_lock and
763884d179dSJan Kara  *   b) getting a reference to dquot before we release dq_list_lock
764884d179dSJan Kara  */
765884d179dSJan Kara struct dquot *dqget(struct super_block *sb, unsigned int id, int type)
766884d179dSJan Kara {
767884d179dSJan Kara 	unsigned int hashent = hashfn(sb, id, type);
768dd6f3c6dSJan Kara 	struct dquot *dquot = NULL, *empty = NULL;
769884d179dSJan Kara 
770884d179dSJan Kara         if (!sb_has_quota_active(sb, type))
771dd6f3c6dSJan Kara 		return NULL;
772884d179dSJan Kara we_slept:
773884d179dSJan Kara 	spin_lock(&dq_list_lock);
774884d179dSJan Kara 	spin_lock(&dq_state_lock);
775884d179dSJan Kara 	if (!sb_has_quota_active(sb, type)) {
776884d179dSJan Kara 		spin_unlock(&dq_state_lock);
777884d179dSJan Kara 		spin_unlock(&dq_list_lock);
778884d179dSJan Kara 		goto out;
779884d179dSJan Kara 	}
780884d179dSJan Kara 	spin_unlock(&dq_state_lock);
781884d179dSJan Kara 
782dd6f3c6dSJan Kara 	dquot = find_dquot(hashent, sb, id, type);
783dd6f3c6dSJan Kara 	if (!dquot) {
784dd6f3c6dSJan Kara 		if (!empty) {
785884d179dSJan Kara 			spin_unlock(&dq_list_lock);
786dd6f3c6dSJan Kara 			empty = get_empty_dquot(sb, type);
787dd6f3c6dSJan Kara 			if (!empty)
788884d179dSJan Kara 				schedule();	/* Try to wait for a moment... */
789884d179dSJan Kara 			goto we_slept;
790884d179dSJan Kara 		}
791884d179dSJan Kara 		dquot = empty;
792dd6f3c6dSJan Kara 		empty = NULL;
793884d179dSJan Kara 		dquot->dq_id = id;
794884d179dSJan Kara 		/* all dquots go on the inuse_list */
795884d179dSJan Kara 		put_inuse(dquot);
796884d179dSJan Kara 		/* hash it first so it can be found */
797884d179dSJan Kara 		insert_dquot_hash(dquot);
798884d179dSJan Kara 		dqstats.lookups++;
799884d179dSJan Kara 		spin_unlock(&dq_list_lock);
800884d179dSJan Kara 	} else {
801884d179dSJan Kara 		if (!atomic_read(&dquot->dq_count))
802884d179dSJan Kara 			remove_free_dquot(dquot);
803884d179dSJan Kara 		atomic_inc(&dquot->dq_count);
804884d179dSJan Kara 		dqstats.cache_hits++;
805884d179dSJan Kara 		dqstats.lookups++;
806884d179dSJan Kara 		spin_unlock(&dq_list_lock);
807884d179dSJan Kara 	}
808268157baSJan Kara 	/* Wait for dq_lock - after this we know that either dquot_release() is
809268157baSJan Kara 	 * already finished or it will be canceled due to dq_count > 1 test */
810884d179dSJan Kara 	wait_on_dquot(dquot);
811268157baSJan Kara 	/* Read the dquot / allocate space in quota file */
812268157baSJan Kara 	if (!test_bit(DQ_ACTIVE_B, &dquot->dq_flags) &&
813268157baSJan Kara 	    sb->dq_op->acquire_dquot(dquot) < 0) {
814884d179dSJan Kara 		dqput(dquot);
815dd6f3c6dSJan Kara 		dquot = NULL;
816884d179dSJan Kara 		goto out;
817884d179dSJan Kara 	}
818884d179dSJan Kara #ifdef __DQUOT_PARANOIA
819884d179dSJan Kara 	BUG_ON(!dquot->dq_sb);	/* Has somebody invalidated entry under us? */
820884d179dSJan Kara #endif
821884d179dSJan Kara out:
822884d179dSJan Kara 	if (empty)
823884d179dSJan Kara 		do_destroy_dquot(empty);
824884d179dSJan Kara 
825884d179dSJan Kara 	return dquot;
826884d179dSJan Kara }
827884d179dSJan Kara EXPORT_SYMBOL(dqget);
828884d179dSJan Kara 
829884d179dSJan Kara static int dqinit_needed(struct inode *inode, int type)
830884d179dSJan Kara {
831884d179dSJan Kara 	int cnt;
832884d179dSJan Kara 
833884d179dSJan Kara 	if (IS_NOQUOTA(inode))
834884d179dSJan Kara 		return 0;
835884d179dSJan Kara 	if (type != -1)
836dd6f3c6dSJan Kara 		return !inode->i_dquot[type];
837884d179dSJan Kara 	for (cnt = 0; cnt < MAXQUOTAS; cnt++)
838dd6f3c6dSJan Kara 		if (!inode->i_dquot[cnt])
839884d179dSJan Kara 			return 1;
840884d179dSJan Kara 	return 0;
841884d179dSJan Kara }
842884d179dSJan Kara 
843884d179dSJan Kara /* This routine is guarded by dqonoff_mutex mutex */
844884d179dSJan Kara static void add_dquot_ref(struct super_block *sb, int type)
845884d179dSJan Kara {
846884d179dSJan Kara 	struct inode *inode, *old_inode = NULL;
847884d179dSJan Kara 
848884d179dSJan Kara 	spin_lock(&inode_lock);
849884d179dSJan Kara 	list_for_each_entry(inode, &sb->s_inodes, i_sb_list) {
850b6fac63cSWu Fengguang 		if (inode->i_state & (I_FREEING|I_CLEAR|I_WILL_FREE|I_NEW))
8513ae5080fSLinus Torvalds 			continue;
852884d179dSJan Kara 		if (!atomic_read(&inode->i_writecount))
853884d179dSJan Kara 			continue;
854884d179dSJan Kara 		if (!dqinit_needed(inode, type))
855884d179dSJan Kara 			continue;
856884d179dSJan Kara 
857884d179dSJan Kara 		__iget(inode);
858884d179dSJan Kara 		spin_unlock(&inode_lock);
859884d179dSJan Kara 
860884d179dSJan Kara 		iput(old_inode);
861884d179dSJan Kara 		sb->dq_op->initialize(inode, type);
862884d179dSJan Kara 		/* We hold a reference to 'inode' so it couldn't have been
863884d179dSJan Kara 		 * removed from s_inodes list while we dropped the inode_lock.
864884d179dSJan Kara 		 * We cannot iput the inode now as we can be holding the last
865884d179dSJan Kara 		 * reference and we cannot iput it under inode_lock. So we
866884d179dSJan Kara 		 * keep the reference and iput it later. */
867884d179dSJan Kara 		old_inode = inode;
868884d179dSJan Kara 		spin_lock(&inode_lock);
869884d179dSJan Kara 	}
870884d179dSJan Kara 	spin_unlock(&inode_lock);
871884d179dSJan Kara 	iput(old_inode);
872884d179dSJan Kara }
873884d179dSJan Kara 
874268157baSJan Kara /*
875268157baSJan Kara  * Return 0 if dqput() won't block.
876268157baSJan Kara  * (note that 1 doesn't necessarily mean blocking)
877268157baSJan Kara  */
878884d179dSJan Kara static inline int dqput_blocks(struct dquot *dquot)
879884d179dSJan Kara {
880884d179dSJan Kara 	if (atomic_read(&dquot->dq_count) <= 1)
881884d179dSJan Kara 		return 1;
882884d179dSJan Kara 	return 0;
883884d179dSJan Kara }
884884d179dSJan Kara 
885268157baSJan Kara /*
886268157baSJan Kara  * Remove references to dquots from inode and add dquot to list for freeing
887268157baSJan Kara  * if we have the last referece to dquot
888268157baSJan Kara  * We can't race with anybody because we hold dqptr_sem for writing...
889268157baSJan Kara  */
890884d179dSJan Kara static int remove_inode_dquot_ref(struct inode *inode, int type,
891884d179dSJan Kara 				  struct list_head *tofree_head)
892884d179dSJan Kara {
893884d179dSJan Kara 	struct dquot *dquot = inode->i_dquot[type];
894884d179dSJan Kara 
895dd6f3c6dSJan Kara 	inode->i_dquot[type] = NULL;
896dd6f3c6dSJan Kara 	if (dquot) {
897884d179dSJan Kara 		if (dqput_blocks(dquot)) {
898884d179dSJan Kara #ifdef __DQUOT_PARANOIA
899884d179dSJan Kara 			if (atomic_read(&dquot->dq_count) != 1)
900884d179dSJan Kara 				printk(KERN_WARNING "VFS: Adding dquot with dq_count %d to dispose list.\n", atomic_read(&dquot->dq_count));
901884d179dSJan Kara #endif
902884d179dSJan Kara 			spin_lock(&dq_list_lock);
903268157baSJan Kara 			/* As dquot must have currently users it can't be on
904268157baSJan Kara 			 * the free list... */
905268157baSJan Kara 			list_add(&dquot->dq_free, tofree_head);
906884d179dSJan Kara 			spin_unlock(&dq_list_lock);
907884d179dSJan Kara 			return 1;
908884d179dSJan Kara 		}
909884d179dSJan Kara 		else
910884d179dSJan Kara 			dqput(dquot);   /* We have guaranteed we won't block */
911884d179dSJan Kara 	}
912884d179dSJan Kara 	return 0;
913884d179dSJan Kara }
914884d179dSJan Kara 
915268157baSJan Kara /*
916268157baSJan Kara  * Free list of dquots
917268157baSJan Kara  * Dquots are removed from inodes and no new references can be got so we are
918268157baSJan Kara  * the only ones holding reference
919268157baSJan Kara  */
920884d179dSJan Kara static void put_dquot_list(struct list_head *tofree_head)
921884d179dSJan Kara {
922884d179dSJan Kara 	struct list_head *act_head;
923884d179dSJan Kara 	struct dquot *dquot;
924884d179dSJan Kara 
925884d179dSJan Kara 	act_head = tofree_head->next;
926884d179dSJan Kara 	while (act_head != tofree_head) {
927884d179dSJan Kara 		dquot = list_entry(act_head, struct dquot, dq_free);
928884d179dSJan Kara 		act_head = act_head->next;
929268157baSJan Kara 		/* Remove dquot from the list so we won't have problems... */
930268157baSJan Kara 		list_del_init(&dquot->dq_free);
931884d179dSJan Kara 		dqput(dquot);
932884d179dSJan Kara 	}
933884d179dSJan Kara }
934884d179dSJan Kara 
935884d179dSJan Kara static void remove_dquot_ref(struct super_block *sb, int type,
936884d179dSJan Kara 		struct list_head *tofree_head)
937884d179dSJan Kara {
938884d179dSJan Kara 	struct inode *inode;
939884d179dSJan Kara 
940884d179dSJan Kara 	spin_lock(&inode_lock);
941884d179dSJan Kara 	list_for_each_entry(inode, &sb->s_inodes, i_sb_list) {
9423ae5080fSLinus Torvalds 		/*
9433ae5080fSLinus Torvalds 		 *  We have to scan also I_NEW inodes because they can already
9443ae5080fSLinus Torvalds 		 *  have quota pointer initialized. Luckily, we need to touch
9453ae5080fSLinus Torvalds 		 *  only quota pointers and these have separate locking
9463ae5080fSLinus Torvalds 		 *  (dqptr_sem).
9473ae5080fSLinus Torvalds 		 */
948884d179dSJan Kara 		if (!IS_NOQUOTA(inode))
949884d179dSJan Kara 			remove_inode_dquot_ref(inode, type, tofree_head);
950884d179dSJan Kara 	}
951884d179dSJan Kara 	spin_unlock(&inode_lock);
952884d179dSJan Kara }
953884d179dSJan Kara 
954884d179dSJan Kara /* Gather all references from inodes and drop them */
955884d179dSJan Kara static void drop_dquot_ref(struct super_block *sb, int type)
956884d179dSJan Kara {
957884d179dSJan Kara 	LIST_HEAD(tofree_head);
958884d179dSJan Kara 
959884d179dSJan Kara 	if (sb->dq_op) {
960884d179dSJan Kara 		down_write(&sb_dqopt(sb)->dqptr_sem);
961884d179dSJan Kara 		remove_dquot_ref(sb, type, &tofree_head);
962884d179dSJan Kara 		up_write(&sb_dqopt(sb)->dqptr_sem);
963884d179dSJan Kara 		put_dquot_list(&tofree_head);
964884d179dSJan Kara 	}
965884d179dSJan Kara }
966884d179dSJan Kara 
967884d179dSJan Kara static inline void dquot_incr_inodes(struct dquot *dquot, qsize_t number)
968884d179dSJan Kara {
969884d179dSJan Kara 	dquot->dq_dqb.dqb_curinodes += number;
970884d179dSJan Kara }
971884d179dSJan Kara 
972884d179dSJan Kara static inline void dquot_incr_space(struct dquot *dquot, qsize_t number)
973884d179dSJan Kara {
974884d179dSJan Kara 	dquot->dq_dqb.dqb_curspace += number;
975884d179dSJan Kara }
976884d179dSJan Kara 
977884d179dSJan Kara static inline void dquot_resv_space(struct dquot *dquot, qsize_t number)
978884d179dSJan Kara {
979884d179dSJan Kara 	dquot->dq_dqb.dqb_rsvspace += number;
980884d179dSJan Kara }
981884d179dSJan Kara 
982884d179dSJan Kara /*
983884d179dSJan Kara  * Claim reserved quota space
984884d179dSJan Kara  */
985884d179dSJan Kara static void dquot_claim_reserved_space(struct dquot *dquot,
986884d179dSJan Kara 						qsize_t number)
987884d179dSJan Kara {
988884d179dSJan Kara 	WARN_ON(dquot->dq_dqb.dqb_rsvspace < number);
989884d179dSJan Kara 	dquot->dq_dqb.dqb_curspace += number;
990884d179dSJan Kara 	dquot->dq_dqb.dqb_rsvspace -= number;
991884d179dSJan Kara }
992884d179dSJan Kara 
993884d179dSJan Kara static inline
994884d179dSJan Kara void dquot_free_reserved_space(struct dquot *dquot, qsize_t number)
995884d179dSJan Kara {
996884d179dSJan Kara 	dquot->dq_dqb.dqb_rsvspace -= number;
997884d179dSJan Kara }
998884d179dSJan Kara 
9997a2435d8SJan Kara static void dquot_decr_inodes(struct dquot *dquot, qsize_t number)
1000884d179dSJan Kara {
1001884d179dSJan Kara 	if (sb_dqopt(dquot->dq_sb)->flags & DQUOT_NEGATIVE_USAGE ||
1002884d179dSJan Kara 	    dquot->dq_dqb.dqb_curinodes >= number)
1003884d179dSJan Kara 		dquot->dq_dqb.dqb_curinodes -= number;
1004884d179dSJan Kara 	else
1005884d179dSJan Kara 		dquot->dq_dqb.dqb_curinodes = 0;
1006884d179dSJan Kara 	if (dquot->dq_dqb.dqb_curinodes <= dquot->dq_dqb.dqb_isoftlimit)
1007884d179dSJan Kara 		dquot->dq_dqb.dqb_itime = (time_t) 0;
1008884d179dSJan Kara 	clear_bit(DQ_INODES_B, &dquot->dq_flags);
1009884d179dSJan Kara }
1010884d179dSJan Kara 
10117a2435d8SJan Kara static void dquot_decr_space(struct dquot *dquot, qsize_t number)
1012884d179dSJan Kara {
1013884d179dSJan Kara 	if (sb_dqopt(dquot->dq_sb)->flags & DQUOT_NEGATIVE_USAGE ||
1014884d179dSJan Kara 	    dquot->dq_dqb.dqb_curspace >= number)
1015884d179dSJan Kara 		dquot->dq_dqb.dqb_curspace -= number;
1016884d179dSJan Kara 	else
1017884d179dSJan Kara 		dquot->dq_dqb.dqb_curspace = 0;
1018884d179dSJan Kara 	if (dquot->dq_dqb.dqb_curspace <= dquot->dq_dqb.dqb_bsoftlimit)
1019884d179dSJan Kara 		dquot->dq_dqb.dqb_btime = (time_t) 0;
1020884d179dSJan Kara 	clear_bit(DQ_BLKS_B, &dquot->dq_flags);
1021884d179dSJan Kara }
1022884d179dSJan Kara 
1023884d179dSJan Kara static int warning_issued(struct dquot *dquot, const int warntype)
1024884d179dSJan Kara {
1025884d179dSJan Kara 	int flag = (warntype == QUOTA_NL_BHARDWARN ||
1026884d179dSJan Kara 		warntype == QUOTA_NL_BSOFTLONGWARN) ? DQ_BLKS_B :
1027884d179dSJan Kara 		((warntype == QUOTA_NL_IHARDWARN ||
1028884d179dSJan Kara 		warntype == QUOTA_NL_ISOFTLONGWARN) ? DQ_INODES_B : 0);
1029884d179dSJan Kara 
1030884d179dSJan Kara 	if (!flag)
1031884d179dSJan Kara 		return 0;
1032884d179dSJan Kara 	return test_and_set_bit(flag, &dquot->dq_flags);
1033884d179dSJan Kara }
1034884d179dSJan Kara 
1035884d179dSJan Kara #ifdef CONFIG_PRINT_QUOTA_WARNING
1036884d179dSJan Kara static int flag_print_warnings = 1;
1037884d179dSJan Kara 
10387a2435d8SJan Kara static int need_print_warning(struct dquot *dquot)
1039884d179dSJan Kara {
1040884d179dSJan Kara 	if (!flag_print_warnings)
1041884d179dSJan Kara 		return 0;
1042884d179dSJan Kara 
1043884d179dSJan Kara 	switch (dquot->dq_type) {
1044884d179dSJan Kara 		case USRQUOTA:
1045884d179dSJan Kara 			return current_fsuid() == dquot->dq_id;
1046884d179dSJan Kara 		case GRPQUOTA:
1047884d179dSJan Kara 			return in_group_p(dquot->dq_id);
1048884d179dSJan Kara 	}
1049884d179dSJan Kara 	return 0;
1050884d179dSJan Kara }
1051884d179dSJan Kara 
1052884d179dSJan Kara /* Print warning to user which exceeded quota */
1053884d179dSJan Kara static void print_warning(struct dquot *dquot, const int warntype)
1054884d179dSJan Kara {
1055884d179dSJan Kara 	char *msg = NULL;
1056884d179dSJan Kara 	struct tty_struct *tty;
1057884d179dSJan Kara 
1058884d179dSJan Kara 	if (warntype == QUOTA_NL_IHARDBELOW ||
1059884d179dSJan Kara 	    warntype == QUOTA_NL_ISOFTBELOW ||
1060884d179dSJan Kara 	    warntype == QUOTA_NL_BHARDBELOW ||
1061884d179dSJan Kara 	    warntype == QUOTA_NL_BSOFTBELOW || !need_print_warning(dquot))
1062884d179dSJan Kara 		return;
1063884d179dSJan Kara 
1064884d179dSJan Kara 	tty = get_current_tty();
1065884d179dSJan Kara 	if (!tty)
1066884d179dSJan Kara 		return;
1067884d179dSJan Kara 	tty_write_message(tty, dquot->dq_sb->s_id);
1068884d179dSJan Kara 	if (warntype == QUOTA_NL_ISOFTWARN || warntype == QUOTA_NL_BSOFTWARN)
1069884d179dSJan Kara 		tty_write_message(tty, ": warning, ");
1070884d179dSJan Kara 	else
1071884d179dSJan Kara 		tty_write_message(tty, ": write failed, ");
1072884d179dSJan Kara 	tty_write_message(tty, quotatypes[dquot->dq_type]);
1073884d179dSJan Kara 	switch (warntype) {
1074884d179dSJan Kara 		case QUOTA_NL_IHARDWARN:
1075884d179dSJan Kara 			msg = " file limit reached.\r\n";
1076884d179dSJan Kara 			break;
1077884d179dSJan Kara 		case QUOTA_NL_ISOFTLONGWARN:
1078884d179dSJan Kara 			msg = " file quota exceeded too long.\r\n";
1079884d179dSJan Kara 			break;
1080884d179dSJan Kara 		case QUOTA_NL_ISOFTWARN:
1081884d179dSJan Kara 			msg = " file quota exceeded.\r\n";
1082884d179dSJan Kara 			break;
1083884d179dSJan Kara 		case QUOTA_NL_BHARDWARN:
1084884d179dSJan Kara 			msg = " block limit reached.\r\n";
1085884d179dSJan Kara 			break;
1086884d179dSJan Kara 		case QUOTA_NL_BSOFTLONGWARN:
1087884d179dSJan Kara 			msg = " block quota exceeded too long.\r\n";
1088884d179dSJan Kara 			break;
1089884d179dSJan Kara 		case QUOTA_NL_BSOFTWARN:
1090884d179dSJan Kara 			msg = " block quota exceeded.\r\n";
1091884d179dSJan Kara 			break;
1092884d179dSJan Kara 	}
1093884d179dSJan Kara 	tty_write_message(tty, msg);
1094884d179dSJan Kara 	tty_kref_put(tty);
1095884d179dSJan Kara }
1096884d179dSJan Kara #endif
1097884d179dSJan Kara 
1098884d179dSJan Kara /*
1099884d179dSJan Kara  * Write warnings to the console and send warning messages over netlink.
1100884d179dSJan Kara  *
1101884d179dSJan Kara  * Note that this function can sleep.
1102884d179dSJan Kara  */
11037a2435d8SJan Kara static void flush_warnings(struct dquot *const *dquots, char *warntype)
1104884d179dSJan Kara {
110586e931a3SSteven Whitehouse 	struct dquot *dq;
1106884d179dSJan Kara 	int i;
1107884d179dSJan Kara 
110886e931a3SSteven Whitehouse 	for (i = 0; i < MAXQUOTAS; i++) {
110986e931a3SSteven Whitehouse 		dq = dquots[i];
111086e931a3SSteven Whitehouse 		if (dq && warntype[i] != QUOTA_NL_NOWARN &&
111186e931a3SSteven Whitehouse 		    !warning_issued(dq, warntype[i])) {
1112884d179dSJan Kara #ifdef CONFIG_PRINT_QUOTA_WARNING
111386e931a3SSteven Whitehouse 			print_warning(dq, warntype[i]);
1114884d179dSJan Kara #endif
111586e931a3SSteven Whitehouse 			quota_send_warning(dq->dq_type, dq->dq_id,
111686e931a3SSteven Whitehouse 					   dq->dq_sb->s_dev, warntype[i]);
111786e931a3SSteven Whitehouse 		}
1118884d179dSJan Kara 	}
1119884d179dSJan Kara }
1120884d179dSJan Kara 
11217a2435d8SJan Kara static int ignore_hardlimit(struct dquot *dquot)
1122884d179dSJan Kara {
1123884d179dSJan Kara 	struct mem_dqinfo *info = &sb_dqopt(dquot->dq_sb)->info[dquot->dq_type];
1124884d179dSJan Kara 
1125884d179dSJan Kara 	return capable(CAP_SYS_RESOURCE) &&
1126268157baSJan Kara 	       (info->dqi_format->qf_fmt_id != QFMT_VFS_OLD ||
1127268157baSJan Kara 		!(info->dqi_flags & V1_DQF_RSQUASH));
1128884d179dSJan Kara }
1129884d179dSJan Kara 
1130884d179dSJan Kara /* needs dq_data_lock */
1131884d179dSJan Kara static int check_idq(struct dquot *dquot, qsize_t inodes, char *warntype)
1132884d179dSJan Kara {
1133268157baSJan Kara 	qsize_t newinodes = dquot->dq_dqb.dqb_curinodes + inodes;
1134268157baSJan Kara 
1135884d179dSJan Kara 	*warntype = QUOTA_NL_NOWARN;
1136884d179dSJan Kara 	if (!sb_has_quota_limits_enabled(dquot->dq_sb, dquot->dq_type) ||
1137884d179dSJan Kara 	    test_bit(DQ_FAKE_B, &dquot->dq_flags))
1138884d179dSJan Kara 		return QUOTA_OK;
1139884d179dSJan Kara 
1140884d179dSJan Kara 	if (dquot->dq_dqb.dqb_ihardlimit &&
1141268157baSJan Kara 	    newinodes > dquot->dq_dqb.dqb_ihardlimit &&
1142884d179dSJan Kara             !ignore_hardlimit(dquot)) {
1143884d179dSJan Kara 		*warntype = QUOTA_NL_IHARDWARN;
1144884d179dSJan Kara 		return NO_QUOTA;
1145884d179dSJan Kara 	}
1146884d179dSJan Kara 
1147884d179dSJan Kara 	if (dquot->dq_dqb.dqb_isoftlimit &&
1148268157baSJan Kara 	    newinodes > dquot->dq_dqb.dqb_isoftlimit &&
1149268157baSJan Kara 	    dquot->dq_dqb.dqb_itime &&
1150268157baSJan Kara 	    get_seconds() >= dquot->dq_dqb.dqb_itime &&
1151884d179dSJan Kara             !ignore_hardlimit(dquot)) {
1152884d179dSJan Kara 		*warntype = QUOTA_NL_ISOFTLONGWARN;
1153884d179dSJan Kara 		return NO_QUOTA;
1154884d179dSJan Kara 	}
1155884d179dSJan Kara 
1156884d179dSJan Kara 	if (dquot->dq_dqb.dqb_isoftlimit &&
1157268157baSJan Kara 	    newinodes > dquot->dq_dqb.dqb_isoftlimit &&
1158884d179dSJan Kara 	    dquot->dq_dqb.dqb_itime == 0) {
1159884d179dSJan Kara 		*warntype = QUOTA_NL_ISOFTWARN;
1160268157baSJan Kara 		dquot->dq_dqb.dqb_itime = get_seconds() +
1161268157baSJan Kara 		    sb_dqopt(dquot->dq_sb)->info[dquot->dq_type].dqi_igrace;
1162884d179dSJan Kara 	}
1163884d179dSJan Kara 
1164884d179dSJan Kara 	return QUOTA_OK;
1165884d179dSJan Kara }
1166884d179dSJan Kara 
1167884d179dSJan Kara /* needs dq_data_lock */
1168884d179dSJan Kara static int check_bdq(struct dquot *dquot, qsize_t space, int prealloc, char *warntype)
1169884d179dSJan Kara {
1170884d179dSJan Kara 	qsize_t tspace;
1171268157baSJan Kara 	struct super_block *sb = dquot->dq_sb;
1172884d179dSJan Kara 
1173884d179dSJan Kara 	*warntype = QUOTA_NL_NOWARN;
1174268157baSJan Kara 	if (!sb_has_quota_limits_enabled(sb, dquot->dq_type) ||
1175884d179dSJan Kara 	    test_bit(DQ_FAKE_B, &dquot->dq_flags))
1176884d179dSJan Kara 		return QUOTA_OK;
1177884d179dSJan Kara 
1178884d179dSJan Kara 	tspace = dquot->dq_dqb.dqb_curspace + dquot->dq_dqb.dqb_rsvspace
1179884d179dSJan Kara 		+ space;
1180884d179dSJan Kara 
1181884d179dSJan Kara 	if (dquot->dq_dqb.dqb_bhardlimit &&
1182884d179dSJan Kara 	    tspace > dquot->dq_dqb.dqb_bhardlimit &&
1183884d179dSJan Kara             !ignore_hardlimit(dquot)) {
1184884d179dSJan Kara 		if (!prealloc)
1185884d179dSJan Kara 			*warntype = QUOTA_NL_BHARDWARN;
1186884d179dSJan Kara 		return NO_QUOTA;
1187884d179dSJan Kara 	}
1188884d179dSJan Kara 
1189884d179dSJan Kara 	if (dquot->dq_dqb.dqb_bsoftlimit &&
1190884d179dSJan Kara 	    tspace > dquot->dq_dqb.dqb_bsoftlimit &&
1191268157baSJan Kara 	    dquot->dq_dqb.dqb_btime &&
1192268157baSJan Kara 	    get_seconds() >= dquot->dq_dqb.dqb_btime &&
1193884d179dSJan Kara             !ignore_hardlimit(dquot)) {
1194884d179dSJan Kara 		if (!prealloc)
1195884d179dSJan Kara 			*warntype = QUOTA_NL_BSOFTLONGWARN;
1196884d179dSJan Kara 		return NO_QUOTA;
1197884d179dSJan Kara 	}
1198884d179dSJan Kara 
1199884d179dSJan Kara 	if (dquot->dq_dqb.dqb_bsoftlimit &&
1200884d179dSJan Kara 	    tspace > dquot->dq_dqb.dqb_bsoftlimit &&
1201884d179dSJan Kara 	    dquot->dq_dqb.dqb_btime == 0) {
1202884d179dSJan Kara 		if (!prealloc) {
1203884d179dSJan Kara 			*warntype = QUOTA_NL_BSOFTWARN;
1204268157baSJan Kara 			dquot->dq_dqb.dqb_btime = get_seconds() +
1205268157baSJan Kara 			    sb_dqopt(sb)->info[dquot->dq_type].dqi_bgrace;
1206884d179dSJan Kara 		}
1207884d179dSJan Kara 		else
1208884d179dSJan Kara 			/*
1209884d179dSJan Kara 			 * We don't allow preallocation to exceed softlimit so exceeding will
1210884d179dSJan Kara 			 * be always printed
1211884d179dSJan Kara 			 */
1212884d179dSJan Kara 			return NO_QUOTA;
1213884d179dSJan Kara 	}
1214884d179dSJan Kara 
1215884d179dSJan Kara 	return QUOTA_OK;
1216884d179dSJan Kara }
1217884d179dSJan Kara 
1218884d179dSJan Kara static int info_idq_free(struct dquot *dquot, qsize_t inodes)
1219884d179dSJan Kara {
1220268157baSJan Kara 	qsize_t newinodes;
1221268157baSJan Kara 
1222884d179dSJan Kara 	if (test_bit(DQ_FAKE_B, &dquot->dq_flags) ||
1223884d179dSJan Kara 	    dquot->dq_dqb.dqb_curinodes <= dquot->dq_dqb.dqb_isoftlimit ||
1224884d179dSJan Kara 	    !sb_has_quota_limits_enabled(dquot->dq_sb, dquot->dq_type))
1225884d179dSJan Kara 		return QUOTA_NL_NOWARN;
1226884d179dSJan Kara 
1227268157baSJan Kara 	newinodes = dquot->dq_dqb.dqb_curinodes - inodes;
1228268157baSJan Kara 	if (newinodes <= dquot->dq_dqb.dqb_isoftlimit)
1229884d179dSJan Kara 		return QUOTA_NL_ISOFTBELOW;
1230884d179dSJan Kara 	if (dquot->dq_dqb.dqb_curinodes >= dquot->dq_dqb.dqb_ihardlimit &&
1231268157baSJan Kara 	    newinodes < dquot->dq_dqb.dqb_ihardlimit)
1232884d179dSJan Kara 		return QUOTA_NL_IHARDBELOW;
1233884d179dSJan Kara 	return QUOTA_NL_NOWARN;
1234884d179dSJan Kara }
1235884d179dSJan Kara 
1236884d179dSJan Kara static int info_bdq_free(struct dquot *dquot, qsize_t space)
1237884d179dSJan Kara {
1238884d179dSJan Kara 	if (test_bit(DQ_FAKE_B, &dquot->dq_flags) ||
1239884d179dSJan Kara 	    dquot->dq_dqb.dqb_curspace <= dquot->dq_dqb.dqb_bsoftlimit)
1240884d179dSJan Kara 		return QUOTA_NL_NOWARN;
1241884d179dSJan Kara 
1242884d179dSJan Kara 	if (dquot->dq_dqb.dqb_curspace - space <= dquot->dq_dqb.dqb_bsoftlimit)
1243884d179dSJan Kara 		return QUOTA_NL_BSOFTBELOW;
1244884d179dSJan Kara 	if (dquot->dq_dqb.dqb_curspace >= dquot->dq_dqb.dqb_bhardlimit &&
1245884d179dSJan Kara 	    dquot->dq_dqb.dqb_curspace - space < dquot->dq_dqb.dqb_bhardlimit)
1246884d179dSJan Kara 		return QUOTA_NL_BHARDBELOW;
1247884d179dSJan Kara 	return QUOTA_NL_NOWARN;
1248884d179dSJan Kara }
1249884d179dSJan Kara /*
1250884d179dSJan Kara  *	Initialize quota pointers in inode
1251884d179dSJan Kara  *	We do things in a bit complicated way but by that we avoid calling
1252884d179dSJan Kara  *	dqget() and thus filesystem callbacks under dqptr_sem.
1253884d179dSJan Kara  */
1254884d179dSJan Kara int dquot_initialize(struct inode *inode, int type)
1255884d179dSJan Kara {
1256884d179dSJan Kara 	unsigned int id = 0;
1257884d179dSJan Kara 	int cnt, ret = 0;
1258dd6f3c6dSJan Kara 	struct dquot *got[MAXQUOTAS] = { NULL, NULL };
1259884d179dSJan Kara 	struct super_block *sb = inode->i_sb;
1260884d179dSJan Kara 
1261884d179dSJan Kara 	/* First test before acquiring mutex - solves deadlocks when we
1262884d179dSJan Kara          * re-enter the quota code and are already holding the mutex */
1263884d179dSJan Kara 	if (IS_NOQUOTA(inode))
1264884d179dSJan Kara 		return 0;
1265884d179dSJan Kara 
1266884d179dSJan Kara 	/* First get references to structures we might need. */
1267884d179dSJan Kara 	for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1268884d179dSJan Kara 		if (type != -1 && cnt != type)
1269884d179dSJan Kara 			continue;
1270884d179dSJan Kara 		switch (cnt) {
1271884d179dSJan Kara 		case USRQUOTA:
1272884d179dSJan Kara 			id = inode->i_uid;
1273884d179dSJan Kara 			break;
1274884d179dSJan Kara 		case GRPQUOTA:
1275884d179dSJan Kara 			id = inode->i_gid;
1276884d179dSJan Kara 			break;
1277884d179dSJan Kara 		}
1278884d179dSJan Kara 		got[cnt] = dqget(sb, id, cnt);
1279884d179dSJan Kara 	}
1280884d179dSJan Kara 
1281884d179dSJan Kara 	down_write(&sb_dqopt(sb)->dqptr_sem);
1282884d179dSJan Kara 	if (IS_NOQUOTA(inode))
1283884d179dSJan Kara 		goto out_err;
1284884d179dSJan Kara 	for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1285884d179dSJan Kara 		if (type != -1 && cnt != type)
1286884d179dSJan Kara 			continue;
1287884d179dSJan Kara 		/* Avoid races with quotaoff() */
1288884d179dSJan Kara 		if (!sb_has_quota_active(sb, cnt))
1289884d179dSJan Kara 			continue;
1290dd6f3c6dSJan Kara 		if (!inode->i_dquot[cnt]) {
1291884d179dSJan Kara 			inode->i_dquot[cnt] = got[cnt];
1292dd6f3c6dSJan Kara 			got[cnt] = NULL;
1293884d179dSJan Kara 		}
1294884d179dSJan Kara 	}
1295884d179dSJan Kara out_err:
1296884d179dSJan Kara 	up_write(&sb_dqopt(sb)->dqptr_sem);
1297884d179dSJan Kara 	/* Drop unused references */
1298dc52dd3aSDmitry Monakhov 	dqput_all(got);
1299884d179dSJan Kara 	return ret;
1300884d179dSJan Kara }
1301884d179dSJan Kara EXPORT_SYMBOL(dquot_initialize);
1302884d179dSJan Kara 
1303884d179dSJan Kara /*
1304884d179dSJan Kara  * 	Release all quotas referenced by inode
1305884d179dSJan Kara  */
1306884d179dSJan Kara int dquot_drop(struct inode *inode)
1307884d179dSJan Kara {
1308884d179dSJan Kara 	int cnt;
1309884d179dSJan Kara 	struct dquot *put[MAXQUOTAS];
1310884d179dSJan Kara 
1311884d179dSJan Kara 	down_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
1312884d179dSJan Kara 	for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1313884d179dSJan Kara 		put[cnt] = inode->i_dquot[cnt];
1314dd6f3c6dSJan Kara 		inode->i_dquot[cnt] = NULL;
1315884d179dSJan Kara 	}
1316884d179dSJan Kara 	up_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
1317dc52dd3aSDmitry Monakhov 	dqput_all(put);
1318884d179dSJan Kara 	return 0;
1319884d179dSJan Kara }
1320884d179dSJan Kara EXPORT_SYMBOL(dquot_drop);
1321884d179dSJan Kara 
1322884d179dSJan Kara /* Wrapper to remove references to quota structures from inode */
1323884d179dSJan Kara void vfs_dq_drop(struct inode *inode)
1324884d179dSJan Kara {
1325884d179dSJan Kara 	/* Here we can get arbitrary inode from clear_inode() so we have
1326884d179dSJan Kara 	 * to be careful. OTOH we don't need locking as quota operations
1327884d179dSJan Kara 	 * are allowed to change only at mount time */
1328884d179dSJan Kara 	if (!IS_NOQUOTA(inode) && inode->i_sb && inode->i_sb->dq_op
1329884d179dSJan Kara 	    && inode->i_sb->dq_op->drop) {
1330884d179dSJan Kara 		int cnt;
1331884d179dSJan Kara 		/* Test before calling to rule out calls from proc and such
1332884d179dSJan Kara                  * where we are not allowed to block. Note that this is
1333884d179dSJan Kara 		 * actually reliable test even without the lock - the caller
1334884d179dSJan Kara 		 * must assure that nobody can come after the DQUOT_DROP and
1335884d179dSJan Kara 		 * add quota pointers back anyway */
1336884d179dSJan Kara 		for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1337dd6f3c6dSJan Kara 			if (inode->i_dquot[cnt])
1338884d179dSJan Kara 				break;
1339884d179dSJan Kara 		if (cnt < MAXQUOTAS)
1340884d179dSJan Kara 			inode->i_sb->dq_op->drop(inode);
1341884d179dSJan Kara 	}
1342884d179dSJan Kara }
1343884d179dSJan Kara EXPORT_SYMBOL(vfs_dq_drop);
1344884d179dSJan Kara 
1345884d179dSJan Kara /*
1346fd8fbfc1SDmitry Monakhov  * inode_reserved_space is managed internally by quota, and protected by
1347fd8fbfc1SDmitry Monakhov  * i_lock similar to i_blocks+i_bytes.
1348fd8fbfc1SDmitry Monakhov  */
1349fd8fbfc1SDmitry Monakhov static qsize_t *inode_reserved_space(struct inode * inode)
1350fd8fbfc1SDmitry Monakhov {
1351fd8fbfc1SDmitry Monakhov 	/* Filesystem must explicitly define it's own method in order to use
1352fd8fbfc1SDmitry Monakhov 	 * quota reservation interface */
1353fd8fbfc1SDmitry Monakhov 	BUG_ON(!inode->i_sb->dq_op->get_reserved_space);
1354fd8fbfc1SDmitry Monakhov 	return inode->i_sb->dq_op->get_reserved_space(inode);
1355fd8fbfc1SDmitry Monakhov }
1356fd8fbfc1SDmitry Monakhov 
1357*c469070aSDmitry Monakhov void inode_add_rsv_space(struct inode *inode, qsize_t number)
1358fd8fbfc1SDmitry Monakhov {
1359fd8fbfc1SDmitry Monakhov 	spin_lock(&inode->i_lock);
1360fd8fbfc1SDmitry Monakhov 	*inode_reserved_space(inode) += number;
1361fd8fbfc1SDmitry Monakhov 	spin_unlock(&inode->i_lock);
1362fd8fbfc1SDmitry Monakhov }
1363*c469070aSDmitry Monakhov EXPORT_SYMBOL(inode_add_rsv_space);
1364fd8fbfc1SDmitry Monakhov 
1365*c469070aSDmitry Monakhov void inode_claim_rsv_space(struct inode *inode, qsize_t number)
1366fd8fbfc1SDmitry Monakhov {
1367fd8fbfc1SDmitry Monakhov 	spin_lock(&inode->i_lock);
1368fd8fbfc1SDmitry Monakhov 	*inode_reserved_space(inode) -= number;
1369fd8fbfc1SDmitry Monakhov 	__inode_add_bytes(inode, number);
1370fd8fbfc1SDmitry Monakhov 	spin_unlock(&inode->i_lock);
1371fd8fbfc1SDmitry Monakhov }
1372*c469070aSDmitry Monakhov EXPORT_SYMBOL(inode_claim_rsv_space);
1373fd8fbfc1SDmitry Monakhov 
1374*c469070aSDmitry Monakhov void inode_sub_rsv_space(struct inode *inode, qsize_t number)
1375fd8fbfc1SDmitry Monakhov {
1376fd8fbfc1SDmitry Monakhov 	spin_lock(&inode->i_lock);
1377fd8fbfc1SDmitry Monakhov 	*inode_reserved_space(inode) -= number;
1378fd8fbfc1SDmitry Monakhov 	spin_unlock(&inode->i_lock);
1379fd8fbfc1SDmitry Monakhov }
1380*c469070aSDmitry Monakhov EXPORT_SYMBOL(inode_sub_rsv_space);
1381fd8fbfc1SDmitry Monakhov 
1382fd8fbfc1SDmitry Monakhov static qsize_t inode_get_rsv_space(struct inode *inode)
1383fd8fbfc1SDmitry Monakhov {
1384fd8fbfc1SDmitry Monakhov 	qsize_t ret;
138505b5d898SJan Kara 
138605b5d898SJan Kara 	if (!inode->i_sb->dq_op->get_reserved_space)
138705b5d898SJan Kara 		return 0;
1388fd8fbfc1SDmitry Monakhov 	spin_lock(&inode->i_lock);
1389fd8fbfc1SDmitry Monakhov 	ret = *inode_reserved_space(inode);
1390fd8fbfc1SDmitry Monakhov 	spin_unlock(&inode->i_lock);
1391fd8fbfc1SDmitry Monakhov 	return ret;
1392fd8fbfc1SDmitry Monakhov }
1393fd8fbfc1SDmitry Monakhov 
1394fd8fbfc1SDmitry Monakhov static void inode_incr_space(struct inode *inode, qsize_t number,
1395fd8fbfc1SDmitry Monakhov 				int reserve)
1396fd8fbfc1SDmitry Monakhov {
1397fd8fbfc1SDmitry Monakhov 	if (reserve)
1398fd8fbfc1SDmitry Monakhov 		inode_add_rsv_space(inode, number);
1399fd8fbfc1SDmitry Monakhov 	else
1400fd8fbfc1SDmitry Monakhov 		inode_add_bytes(inode, number);
1401fd8fbfc1SDmitry Monakhov }
1402fd8fbfc1SDmitry Monakhov 
1403fd8fbfc1SDmitry Monakhov static void inode_decr_space(struct inode *inode, qsize_t number, int reserve)
1404fd8fbfc1SDmitry Monakhov {
1405fd8fbfc1SDmitry Monakhov 	if (reserve)
1406fd8fbfc1SDmitry Monakhov 		inode_sub_rsv_space(inode, number);
1407fd8fbfc1SDmitry Monakhov 	else
1408fd8fbfc1SDmitry Monakhov 		inode_sub_bytes(inode, number);
1409fd8fbfc1SDmitry Monakhov }
1410fd8fbfc1SDmitry Monakhov 
1411fd8fbfc1SDmitry Monakhov /*
1412884d179dSJan Kara  * Following four functions update i_blocks+i_bytes fields and
1413884d179dSJan Kara  * quota information (together with appropriate checks)
1414884d179dSJan Kara  * NOTE: We absolutely rely on the fact that caller dirties
1415884d179dSJan Kara  * the inode (usually macros in quotaops.h care about this) and
1416884d179dSJan Kara  * holds a handle for the current transaction so that dquot write and
1417884d179dSJan Kara  * inode write go into the same transaction.
1418884d179dSJan Kara  */
1419884d179dSJan Kara 
1420884d179dSJan Kara /*
1421884d179dSJan Kara  * This operation can block, but only after everything is updated
1422884d179dSJan Kara  */
1423884d179dSJan Kara int __dquot_alloc_space(struct inode *inode, qsize_t number,
1424884d179dSJan Kara 			int warn, int reserve)
1425884d179dSJan Kara {
1426884d179dSJan Kara 	int cnt, ret = QUOTA_OK;
1427884d179dSJan Kara 	char warntype[MAXQUOTAS];
1428884d179dSJan Kara 
1429fd8fbfc1SDmitry Monakhov 	/*
1430fd8fbfc1SDmitry Monakhov 	 * First test before acquiring mutex - solves deadlocks when we
1431fd8fbfc1SDmitry Monakhov 	 * re-enter the quota code and are already holding the mutex
1432fd8fbfc1SDmitry Monakhov 	 */
1433fd8fbfc1SDmitry Monakhov 	if (IS_NOQUOTA(inode)) {
1434fd8fbfc1SDmitry Monakhov 		inode_incr_space(inode, number, reserve);
1435fd8fbfc1SDmitry Monakhov 		goto out;
1436fd8fbfc1SDmitry Monakhov 	}
1437fd8fbfc1SDmitry Monakhov 
1438fd8fbfc1SDmitry Monakhov 	down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1439884d179dSJan Kara 	for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1440884d179dSJan Kara 		warntype[cnt] = QUOTA_NL_NOWARN;
1441884d179dSJan Kara 
1442884d179dSJan Kara 	spin_lock(&dq_data_lock);
1443884d179dSJan Kara 	for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1444dd6f3c6dSJan Kara 		if (!inode->i_dquot[cnt])
1445884d179dSJan Kara 			continue;
1446884d179dSJan Kara 		if (check_bdq(inode->i_dquot[cnt], number, warn, warntype+cnt)
1447884d179dSJan Kara 		    == NO_QUOTA) {
1448884d179dSJan Kara 			ret = NO_QUOTA;
1449fd8fbfc1SDmitry Monakhov 			spin_unlock(&dq_data_lock);
1450fd8fbfc1SDmitry Monakhov 			goto out_flush_warn;
1451884d179dSJan Kara 		}
1452884d179dSJan Kara 	}
1453884d179dSJan Kara 	for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1454dd6f3c6dSJan Kara 		if (!inode->i_dquot[cnt])
1455884d179dSJan Kara 			continue;
1456884d179dSJan Kara 		if (reserve)
1457884d179dSJan Kara 			dquot_resv_space(inode->i_dquot[cnt], number);
1458884d179dSJan Kara 		else
1459884d179dSJan Kara 			dquot_incr_space(inode->i_dquot[cnt], number);
1460884d179dSJan Kara 	}
1461fd8fbfc1SDmitry Monakhov 	inode_incr_space(inode, number, reserve);
1462884d179dSJan Kara 	spin_unlock(&dq_data_lock);
1463fd8fbfc1SDmitry Monakhov 
1464fd8fbfc1SDmitry Monakhov 	if (reserve)
1465fd8fbfc1SDmitry Monakhov 		goto out_flush_warn;
1466dc52dd3aSDmitry Monakhov 	mark_all_dquot_dirty(inode->i_dquot);
1467fd8fbfc1SDmitry Monakhov out_flush_warn:
1468884d179dSJan Kara 	flush_warnings(inode->i_dquot, warntype);
1469fd8fbfc1SDmitry Monakhov 	up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1470fd8fbfc1SDmitry Monakhov out:
1471884d179dSJan Kara 	return ret;
1472884d179dSJan Kara }
1473884d179dSJan Kara 
1474884d179dSJan Kara int dquot_alloc_space(struct inode *inode, qsize_t number, int warn)
1475884d179dSJan Kara {
1476fd8fbfc1SDmitry Monakhov 	return __dquot_alloc_space(inode, number, warn, 0);
1477884d179dSJan Kara }
1478884d179dSJan Kara EXPORT_SYMBOL(dquot_alloc_space);
1479884d179dSJan Kara 
1480884d179dSJan Kara int dquot_reserve_space(struct inode *inode, qsize_t number, int warn)
1481884d179dSJan Kara {
1482fd8fbfc1SDmitry Monakhov 	return __dquot_alloc_space(inode, number, warn, 1);
1483884d179dSJan Kara }
1484884d179dSJan Kara EXPORT_SYMBOL(dquot_reserve_space);
1485884d179dSJan Kara 
1486884d179dSJan Kara /*
1487884d179dSJan Kara  * This operation can block, but only after everything is updated
1488884d179dSJan Kara  */
1489884d179dSJan Kara int dquot_alloc_inode(const struct inode *inode, qsize_t number)
1490884d179dSJan Kara {
1491884d179dSJan Kara 	int cnt, ret = NO_QUOTA;
1492884d179dSJan Kara 	char warntype[MAXQUOTAS];
1493884d179dSJan Kara 
1494884d179dSJan Kara 	/* First test before acquiring mutex - solves deadlocks when we
1495884d179dSJan Kara          * re-enter the quota code and are already holding the mutex */
1496884d179dSJan Kara 	if (IS_NOQUOTA(inode))
1497884d179dSJan Kara 		return QUOTA_OK;
1498884d179dSJan Kara 	for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1499884d179dSJan Kara 		warntype[cnt] = QUOTA_NL_NOWARN;
1500884d179dSJan Kara 	down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1501884d179dSJan Kara 	spin_lock(&dq_data_lock);
1502884d179dSJan Kara 	for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1503dd6f3c6dSJan Kara 		if (!inode->i_dquot[cnt])
1504884d179dSJan Kara 			continue;
1505268157baSJan Kara 		if (check_idq(inode->i_dquot[cnt], number, warntype+cnt)
1506268157baSJan Kara 		    == NO_QUOTA)
1507884d179dSJan Kara 			goto warn_put_all;
1508884d179dSJan Kara 	}
1509884d179dSJan Kara 
1510884d179dSJan Kara 	for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1511dd6f3c6dSJan Kara 		if (!inode->i_dquot[cnt])
1512884d179dSJan Kara 			continue;
1513884d179dSJan Kara 		dquot_incr_inodes(inode->i_dquot[cnt], number);
1514884d179dSJan Kara 	}
1515884d179dSJan Kara 	ret = QUOTA_OK;
1516884d179dSJan Kara warn_put_all:
1517884d179dSJan Kara 	spin_unlock(&dq_data_lock);
1518884d179dSJan Kara 	if (ret == QUOTA_OK)
1519dc52dd3aSDmitry Monakhov 		mark_all_dquot_dirty(inode->i_dquot);
1520884d179dSJan Kara 	flush_warnings(inode->i_dquot, warntype);
1521884d179dSJan Kara 	up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1522884d179dSJan Kara 	return ret;
1523884d179dSJan Kara }
1524884d179dSJan Kara EXPORT_SYMBOL(dquot_alloc_inode);
1525884d179dSJan Kara 
1526884d179dSJan Kara int dquot_claim_space(struct inode *inode, qsize_t number)
1527884d179dSJan Kara {
1528884d179dSJan Kara 	int cnt;
1529884d179dSJan Kara 	int ret = QUOTA_OK;
1530884d179dSJan Kara 
1531884d179dSJan Kara 	if (IS_NOQUOTA(inode)) {
1532fd8fbfc1SDmitry Monakhov 		inode_claim_rsv_space(inode, number);
1533884d179dSJan Kara 		goto out;
1534884d179dSJan Kara 	}
1535884d179dSJan Kara 
1536884d179dSJan Kara 	down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1537884d179dSJan Kara 	spin_lock(&dq_data_lock);
1538884d179dSJan Kara 	/* Claim reserved quotas to allocated quotas */
1539884d179dSJan Kara 	for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1540dd6f3c6dSJan Kara 		if (inode->i_dquot[cnt])
1541884d179dSJan Kara 			dquot_claim_reserved_space(inode->i_dquot[cnt],
1542884d179dSJan Kara 							number);
1543884d179dSJan Kara 	}
1544884d179dSJan Kara 	/* Update inode bytes */
1545fd8fbfc1SDmitry Monakhov 	inode_claim_rsv_space(inode, number);
1546884d179dSJan Kara 	spin_unlock(&dq_data_lock);
1547dc52dd3aSDmitry Monakhov 	mark_all_dquot_dirty(inode->i_dquot);
1548884d179dSJan Kara 	up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1549884d179dSJan Kara out:
1550884d179dSJan Kara 	return ret;
1551884d179dSJan Kara }
1552884d179dSJan Kara EXPORT_SYMBOL(dquot_claim_space);
1553884d179dSJan Kara 
1554884d179dSJan Kara /*
1555884d179dSJan Kara  * This operation can block, but only after everything is updated
1556884d179dSJan Kara  */
1557fd8fbfc1SDmitry Monakhov int __dquot_free_space(struct inode *inode, qsize_t number, int reserve)
1558884d179dSJan Kara {
1559884d179dSJan Kara 	unsigned int cnt;
1560884d179dSJan Kara 	char warntype[MAXQUOTAS];
1561884d179dSJan Kara 
1562884d179dSJan Kara 	/* First test before acquiring mutex - solves deadlocks when we
1563884d179dSJan Kara          * re-enter the quota code and are already holding the mutex */
1564884d179dSJan Kara 	if (IS_NOQUOTA(inode)) {
1565fd8fbfc1SDmitry Monakhov 		inode_decr_space(inode, number, reserve);
1566884d179dSJan Kara 		return QUOTA_OK;
1567884d179dSJan Kara 	}
1568884d179dSJan Kara 
1569884d179dSJan Kara 	down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1570884d179dSJan Kara 	spin_lock(&dq_data_lock);
1571884d179dSJan Kara 	for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1572dd6f3c6dSJan Kara 		if (!inode->i_dquot[cnt])
1573884d179dSJan Kara 			continue;
1574884d179dSJan Kara 		warntype[cnt] = info_bdq_free(inode->i_dquot[cnt], number);
1575fd8fbfc1SDmitry Monakhov 		if (reserve)
1576fd8fbfc1SDmitry Monakhov 			dquot_free_reserved_space(inode->i_dquot[cnt], number);
1577fd8fbfc1SDmitry Monakhov 		else
1578884d179dSJan Kara 			dquot_decr_space(inode->i_dquot[cnt], number);
1579884d179dSJan Kara 	}
1580fd8fbfc1SDmitry Monakhov 	inode_decr_space(inode, number, reserve);
1581884d179dSJan Kara 	spin_unlock(&dq_data_lock);
1582fd8fbfc1SDmitry Monakhov 
1583fd8fbfc1SDmitry Monakhov 	if (reserve)
1584fd8fbfc1SDmitry Monakhov 		goto out_unlock;
1585dc52dd3aSDmitry Monakhov 	mark_all_dquot_dirty(inode->i_dquot);
1586fd8fbfc1SDmitry Monakhov out_unlock:
1587884d179dSJan Kara 	flush_warnings(inode->i_dquot, warntype);
1588884d179dSJan Kara 	up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1589884d179dSJan Kara 	return QUOTA_OK;
1590884d179dSJan Kara }
1591fd8fbfc1SDmitry Monakhov 
1592fd8fbfc1SDmitry Monakhov int dquot_free_space(struct inode *inode, qsize_t number)
1593fd8fbfc1SDmitry Monakhov {
1594fd8fbfc1SDmitry Monakhov 	return  __dquot_free_space(inode, number, 0);
1595fd8fbfc1SDmitry Monakhov }
1596884d179dSJan Kara EXPORT_SYMBOL(dquot_free_space);
1597884d179dSJan Kara 
1598884d179dSJan Kara /*
1599fd8fbfc1SDmitry Monakhov  * Release reserved quota space
1600fd8fbfc1SDmitry Monakhov  */
1601fd8fbfc1SDmitry Monakhov void dquot_release_reserved_space(struct inode *inode, qsize_t number)
1602fd8fbfc1SDmitry Monakhov {
1603fd8fbfc1SDmitry Monakhov 	__dquot_free_space(inode, number, 1);
1604fd8fbfc1SDmitry Monakhov 
1605fd8fbfc1SDmitry Monakhov }
1606fd8fbfc1SDmitry Monakhov EXPORT_SYMBOL(dquot_release_reserved_space);
1607fd8fbfc1SDmitry Monakhov 
1608fd8fbfc1SDmitry Monakhov /*
1609884d179dSJan Kara  * This operation can block, but only after everything is updated
1610884d179dSJan Kara  */
1611884d179dSJan Kara int dquot_free_inode(const struct inode *inode, qsize_t number)
1612884d179dSJan Kara {
1613884d179dSJan Kara 	unsigned int cnt;
1614884d179dSJan Kara 	char warntype[MAXQUOTAS];
1615884d179dSJan Kara 
1616884d179dSJan Kara 	/* First test before acquiring mutex - solves deadlocks when we
1617884d179dSJan Kara          * re-enter the quota code and are already holding the mutex */
1618884d179dSJan Kara 	if (IS_NOQUOTA(inode))
1619884d179dSJan Kara 		return QUOTA_OK;
1620884d179dSJan Kara 
1621884d179dSJan Kara 	down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1622884d179dSJan Kara 	spin_lock(&dq_data_lock);
1623884d179dSJan Kara 	for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1624dd6f3c6dSJan Kara 		if (!inode->i_dquot[cnt])
1625884d179dSJan Kara 			continue;
1626884d179dSJan Kara 		warntype[cnt] = info_idq_free(inode->i_dquot[cnt], number);
1627884d179dSJan Kara 		dquot_decr_inodes(inode->i_dquot[cnt], number);
1628884d179dSJan Kara 	}
1629884d179dSJan Kara 	spin_unlock(&dq_data_lock);
1630dc52dd3aSDmitry Monakhov 	mark_all_dquot_dirty(inode->i_dquot);
1631884d179dSJan Kara 	flush_warnings(inode->i_dquot, warntype);
1632884d179dSJan Kara 	up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1633884d179dSJan Kara 	return QUOTA_OK;
1634884d179dSJan Kara }
1635884d179dSJan Kara EXPORT_SYMBOL(dquot_free_inode);
1636884d179dSJan Kara 
1637884d179dSJan Kara /*
1638884d179dSJan Kara  * Transfer the number of inode and blocks from one diskquota to an other.
1639884d179dSJan Kara  *
1640884d179dSJan Kara  * This operation can block, but only after everything is updated
1641884d179dSJan Kara  * A transaction must be started when entering this function.
1642884d179dSJan Kara  */
1643884d179dSJan Kara int dquot_transfer(struct inode *inode, struct iattr *iattr)
1644884d179dSJan Kara {
1645884d179dSJan Kara 	qsize_t space, cur_space;
1646884d179dSJan Kara 	qsize_t rsv_space = 0;
1647884d179dSJan Kara 	struct dquot *transfer_from[MAXQUOTAS];
1648884d179dSJan Kara 	struct dquot *transfer_to[MAXQUOTAS];
1649884d179dSJan Kara 	int cnt, ret = QUOTA_OK;
1650884d179dSJan Kara 	int chuid = iattr->ia_valid & ATTR_UID && inode->i_uid != iattr->ia_uid,
1651884d179dSJan Kara 	    chgid = iattr->ia_valid & ATTR_GID && inode->i_gid != iattr->ia_gid;
1652884d179dSJan Kara 	char warntype_to[MAXQUOTAS];
1653884d179dSJan Kara 	char warntype_from_inodes[MAXQUOTAS], warntype_from_space[MAXQUOTAS];
1654884d179dSJan Kara 
1655884d179dSJan Kara 	/* First test before acquiring mutex - solves deadlocks when we
1656884d179dSJan Kara          * re-enter the quota code and are already holding the mutex */
1657884d179dSJan Kara 	if (IS_NOQUOTA(inode))
1658884d179dSJan Kara 		return QUOTA_OK;
1659884d179dSJan Kara 	/* Initialize the arrays */
1660884d179dSJan Kara 	for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1661dd6f3c6dSJan Kara 		transfer_from[cnt] = NULL;
1662dd6f3c6dSJan Kara 		transfer_to[cnt] = NULL;
1663884d179dSJan Kara 		warntype_to[cnt] = QUOTA_NL_NOWARN;
1664884d179dSJan Kara 	}
1665268157baSJan Kara 	if (chuid)
1666268157baSJan Kara 		transfer_to[USRQUOTA] = dqget(inode->i_sb, iattr->ia_uid,
1667268157baSJan Kara 					      USRQUOTA);
1668268157baSJan Kara 	if (chgid)
1669268157baSJan Kara 		transfer_to[GRPQUOTA] = dqget(inode->i_sb, iattr->ia_gid,
1670268157baSJan Kara 					      GRPQUOTA);
1671884d179dSJan Kara 
1672884d179dSJan Kara 	down_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
1673884d179dSJan Kara 	if (IS_NOQUOTA(inode)) {	/* File without quota accounting? */
1674884d179dSJan Kara 		up_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
1675884d179dSJan Kara 		goto put_all;
1676884d179dSJan Kara 	}
1677884d179dSJan Kara 	spin_lock(&dq_data_lock);
1678884d179dSJan Kara 	cur_space = inode_get_bytes(inode);
1679fd8fbfc1SDmitry Monakhov 	rsv_space = inode_get_rsv_space(inode);
1680884d179dSJan Kara 	space = cur_space + rsv_space;
1681884d179dSJan Kara 	/* Build the transfer_from list and check the limits */
1682884d179dSJan Kara 	for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1683dd6f3c6dSJan Kara 		if (!transfer_to[cnt])
1684884d179dSJan Kara 			continue;
1685884d179dSJan Kara 		transfer_from[cnt] = inode->i_dquot[cnt];
1686884d179dSJan Kara 		if (check_idq(transfer_to[cnt], 1, warntype_to + cnt) ==
1687884d179dSJan Kara 		    NO_QUOTA || check_bdq(transfer_to[cnt], space, 0,
1688884d179dSJan Kara 		    warntype_to + cnt) == NO_QUOTA)
1689884d179dSJan Kara 			goto over_quota;
1690884d179dSJan Kara 	}
1691884d179dSJan Kara 
1692884d179dSJan Kara 	/*
1693884d179dSJan Kara 	 * Finally perform the needed transfer from transfer_from to transfer_to
1694884d179dSJan Kara 	 */
1695884d179dSJan Kara 	for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1696884d179dSJan Kara 		/*
1697884d179dSJan Kara 		 * Skip changes for same uid or gid or for turned off quota-type.
1698884d179dSJan Kara 		 */
1699dd6f3c6dSJan Kara 		if (!transfer_to[cnt])
1700884d179dSJan Kara 			continue;
1701884d179dSJan Kara 
1702884d179dSJan Kara 		/* Due to IO error we might not have transfer_from[] structure */
1703884d179dSJan Kara 		if (transfer_from[cnt]) {
1704884d179dSJan Kara 			warntype_from_inodes[cnt] =
1705884d179dSJan Kara 				info_idq_free(transfer_from[cnt], 1);
1706884d179dSJan Kara 			warntype_from_space[cnt] =
1707884d179dSJan Kara 				info_bdq_free(transfer_from[cnt], space);
1708884d179dSJan Kara 			dquot_decr_inodes(transfer_from[cnt], 1);
1709884d179dSJan Kara 			dquot_decr_space(transfer_from[cnt], cur_space);
1710884d179dSJan Kara 			dquot_free_reserved_space(transfer_from[cnt],
1711884d179dSJan Kara 						  rsv_space);
1712884d179dSJan Kara 		}
1713884d179dSJan Kara 
1714884d179dSJan Kara 		dquot_incr_inodes(transfer_to[cnt], 1);
1715884d179dSJan Kara 		dquot_incr_space(transfer_to[cnt], cur_space);
1716884d179dSJan Kara 		dquot_resv_space(transfer_to[cnt], rsv_space);
1717884d179dSJan Kara 
1718884d179dSJan Kara 		inode->i_dquot[cnt] = transfer_to[cnt];
1719884d179dSJan Kara 	}
1720884d179dSJan Kara 	spin_unlock(&dq_data_lock);
1721884d179dSJan Kara 	up_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
1722884d179dSJan Kara 
1723dc52dd3aSDmitry Monakhov 	mark_all_dquot_dirty(transfer_from);
1724dc52dd3aSDmitry Monakhov 	mark_all_dquot_dirty(transfer_to);
1725884d179dSJan Kara 	/* The reference we got is transferred to the inode */
1726dc52dd3aSDmitry Monakhov 	for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1727dd6f3c6dSJan Kara 		transfer_to[cnt] = NULL;
1728884d179dSJan Kara warn_put_all:
1729884d179dSJan Kara 	flush_warnings(transfer_to, warntype_to);
1730884d179dSJan Kara 	flush_warnings(transfer_from, warntype_from_inodes);
1731884d179dSJan Kara 	flush_warnings(transfer_from, warntype_from_space);
1732884d179dSJan Kara put_all:
1733dc52dd3aSDmitry Monakhov 	dqput_all(transfer_from);
1734dc52dd3aSDmitry Monakhov 	dqput_all(transfer_to);
1735884d179dSJan Kara 	return ret;
1736884d179dSJan Kara over_quota:
1737884d179dSJan Kara 	spin_unlock(&dq_data_lock);
1738884d179dSJan Kara 	up_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
1739884d179dSJan Kara 	/* Clear dquot pointers we don't want to dqput() */
1740884d179dSJan Kara 	for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1741dd6f3c6dSJan Kara 		transfer_from[cnt] = NULL;
1742884d179dSJan Kara 	ret = NO_QUOTA;
1743884d179dSJan Kara 	goto warn_put_all;
1744884d179dSJan Kara }
1745884d179dSJan Kara EXPORT_SYMBOL(dquot_transfer);
1746884d179dSJan Kara 
1747884d179dSJan Kara /* Wrapper for transferring ownership of an inode */
1748884d179dSJan Kara int vfs_dq_transfer(struct inode *inode, struct iattr *iattr)
1749884d179dSJan Kara {
1750884d179dSJan Kara 	if (sb_any_quota_active(inode->i_sb) && !IS_NOQUOTA(inode)) {
1751884d179dSJan Kara 		vfs_dq_init(inode);
1752884d179dSJan Kara 		if (inode->i_sb->dq_op->transfer(inode, iattr) == NO_QUOTA)
1753884d179dSJan Kara 			return 1;
1754884d179dSJan Kara 	}
1755884d179dSJan Kara 	return 0;
1756884d179dSJan Kara }
1757884d179dSJan Kara EXPORT_SYMBOL(vfs_dq_transfer);
1758884d179dSJan Kara 
1759884d179dSJan Kara /*
1760884d179dSJan Kara  * Write info of quota file to disk
1761884d179dSJan Kara  */
1762884d179dSJan Kara int dquot_commit_info(struct super_block *sb, int type)
1763884d179dSJan Kara {
1764884d179dSJan Kara 	int ret;
1765884d179dSJan Kara 	struct quota_info *dqopt = sb_dqopt(sb);
1766884d179dSJan Kara 
1767884d179dSJan Kara 	mutex_lock(&dqopt->dqio_mutex);
1768884d179dSJan Kara 	ret = dqopt->ops[type]->write_file_info(sb, type);
1769884d179dSJan Kara 	mutex_unlock(&dqopt->dqio_mutex);
1770884d179dSJan Kara 	return ret;
1771884d179dSJan Kara }
1772884d179dSJan Kara EXPORT_SYMBOL(dquot_commit_info);
1773884d179dSJan Kara 
1774884d179dSJan Kara /*
1775884d179dSJan Kara  * Definitions of diskquota operations.
1776884d179dSJan Kara  */
177761e225dcSAlexey Dobriyan const struct dquot_operations dquot_operations = {
1778884d179dSJan Kara 	.initialize	= dquot_initialize,
1779884d179dSJan Kara 	.drop		= dquot_drop,
1780884d179dSJan Kara 	.alloc_space	= dquot_alloc_space,
1781884d179dSJan Kara 	.alloc_inode	= dquot_alloc_inode,
1782884d179dSJan Kara 	.free_space	= dquot_free_space,
1783884d179dSJan Kara 	.free_inode	= dquot_free_inode,
1784884d179dSJan Kara 	.transfer	= dquot_transfer,
1785884d179dSJan Kara 	.write_dquot	= dquot_commit,
1786884d179dSJan Kara 	.acquire_dquot	= dquot_acquire,
1787884d179dSJan Kara 	.release_dquot	= dquot_release,
1788884d179dSJan Kara 	.mark_dirty	= dquot_mark_dquot_dirty,
1789884d179dSJan Kara 	.write_info	= dquot_commit_info,
1790884d179dSJan Kara 	.alloc_dquot	= dquot_alloc,
1791884d179dSJan Kara 	.destroy_dquot	= dquot_destroy,
1792884d179dSJan Kara };
1793884d179dSJan Kara 
1794884d179dSJan Kara /*
1795884d179dSJan Kara  * Turn quota off on a device. type == -1 ==> quotaoff for all types (umount)
1796884d179dSJan Kara  */
1797884d179dSJan Kara int vfs_quota_disable(struct super_block *sb, int type, unsigned int flags)
1798884d179dSJan Kara {
1799884d179dSJan Kara 	int cnt, ret = 0;
1800884d179dSJan Kara 	struct quota_info *dqopt = sb_dqopt(sb);
1801884d179dSJan Kara 	struct inode *toputinode[MAXQUOTAS];
1802884d179dSJan Kara 
1803884d179dSJan Kara 	/* Cannot turn off usage accounting without turning off limits, or
1804884d179dSJan Kara 	 * suspend quotas and simultaneously turn quotas off. */
1805884d179dSJan Kara 	if ((flags & DQUOT_USAGE_ENABLED && !(flags & DQUOT_LIMITS_ENABLED))
1806884d179dSJan Kara 	    || (flags & DQUOT_SUSPENDED && flags & (DQUOT_LIMITS_ENABLED |
1807884d179dSJan Kara 	    DQUOT_USAGE_ENABLED)))
1808884d179dSJan Kara 		return -EINVAL;
1809884d179dSJan Kara 
1810884d179dSJan Kara 	/* We need to serialize quota_off() for device */
1811884d179dSJan Kara 	mutex_lock(&dqopt->dqonoff_mutex);
1812884d179dSJan Kara 
1813884d179dSJan Kara 	/*
1814884d179dSJan Kara 	 * Skip everything if there's nothing to do. We have to do this because
1815884d179dSJan Kara 	 * sometimes we are called when fill_super() failed and calling
1816884d179dSJan Kara 	 * sync_fs() in such cases does no good.
1817884d179dSJan Kara 	 */
1818884d179dSJan Kara 	if (!sb_any_quota_loaded(sb)) {
1819884d179dSJan Kara 		mutex_unlock(&dqopt->dqonoff_mutex);
1820884d179dSJan Kara 		return 0;
1821884d179dSJan Kara 	}
1822884d179dSJan Kara 	for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1823884d179dSJan Kara 		toputinode[cnt] = NULL;
1824884d179dSJan Kara 		if (type != -1 && cnt != type)
1825884d179dSJan Kara 			continue;
1826884d179dSJan Kara 		if (!sb_has_quota_loaded(sb, cnt))
1827884d179dSJan Kara 			continue;
1828884d179dSJan Kara 
1829884d179dSJan Kara 		if (flags & DQUOT_SUSPENDED) {
1830884d179dSJan Kara 			spin_lock(&dq_state_lock);
1831884d179dSJan Kara 			dqopt->flags |=
1832884d179dSJan Kara 				dquot_state_flag(DQUOT_SUSPENDED, cnt);
1833884d179dSJan Kara 			spin_unlock(&dq_state_lock);
1834884d179dSJan Kara 		} else {
1835884d179dSJan Kara 			spin_lock(&dq_state_lock);
1836884d179dSJan Kara 			dqopt->flags &= ~dquot_state_flag(flags, cnt);
1837884d179dSJan Kara 			/* Turning off suspended quotas? */
1838884d179dSJan Kara 			if (!sb_has_quota_loaded(sb, cnt) &&
1839884d179dSJan Kara 			    sb_has_quota_suspended(sb, cnt)) {
1840884d179dSJan Kara 				dqopt->flags &=	~dquot_state_flag(
1841884d179dSJan Kara 							DQUOT_SUSPENDED, cnt);
1842884d179dSJan Kara 				spin_unlock(&dq_state_lock);
1843884d179dSJan Kara 				iput(dqopt->files[cnt]);
1844884d179dSJan Kara 				dqopt->files[cnt] = NULL;
1845884d179dSJan Kara 				continue;
1846884d179dSJan Kara 			}
1847884d179dSJan Kara 			spin_unlock(&dq_state_lock);
1848884d179dSJan Kara 		}
1849884d179dSJan Kara 
1850884d179dSJan Kara 		/* We still have to keep quota loaded? */
1851884d179dSJan Kara 		if (sb_has_quota_loaded(sb, cnt) && !(flags & DQUOT_SUSPENDED))
1852884d179dSJan Kara 			continue;
1853884d179dSJan Kara 
1854884d179dSJan Kara 		/* Note: these are blocking operations */
1855884d179dSJan Kara 		drop_dquot_ref(sb, cnt);
1856884d179dSJan Kara 		invalidate_dquots(sb, cnt);
1857884d179dSJan Kara 		/*
1858268157baSJan Kara 		 * Now all dquots should be invalidated, all writes done so we
1859268157baSJan Kara 		 * should be only users of the info. No locks needed.
1860884d179dSJan Kara 		 */
1861884d179dSJan Kara 		if (info_dirty(&dqopt->info[cnt]))
1862884d179dSJan Kara 			sb->dq_op->write_info(sb, cnt);
1863884d179dSJan Kara 		if (dqopt->ops[cnt]->free_file_info)
1864884d179dSJan Kara 			dqopt->ops[cnt]->free_file_info(sb, cnt);
1865884d179dSJan Kara 		put_quota_format(dqopt->info[cnt].dqi_format);
1866884d179dSJan Kara 
1867884d179dSJan Kara 		toputinode[cnt] = dqopt->files[cnt];
1868884d179dSJan Kara 		if (!sb_has_quota_loaded(sb, cnt))
1869884d179dSJan Kara 			dqopt->files[cnt] = NULL;
1870884d179dSJan Kara 		dqopt->info[cnt].dqi_flags = 0;
1871884d179dSJan Kara 		dqopt->info[cnt].dqi_igrace = 0;
1872884d179dSJan Kara 		dqopt->info[cnt].dqi_bgrace = 0;
1873884d179dSJan Kara 		dqopt->ops[cnt] = NULL;
1874884d179dSJan Kara 	}
1875884d179dSJan Kara 	mutex_unlock(&dqopt->dqonoff_mutex);
1876884d179dSJan Kara 
1877884d179dSJan Kara 	/* Skip syncing and setting flags if quota files are hidden */
1878884d179dSJan Kara 	if (dqopt->flags & DQUOT_QUOTA_SYS_FILE)
1879884d179dSJan Kara 		goto put_inodes;
1880884d179dSJan Kara 
1881884d179dSJan Kara 	/* Sync the superblock so that buffers with quota data are written to
1882884d179dSJan Kara 	 * disk (and so userspace sees correct data afterwards). */
1883884d179dSJan Kara 	if (sb->s_op->sync_fs)
1884884d179dSJan Kara 		sb->s_op->sync_fs(sb, 1);
1885884d179dSJan Kara 	sync_blockdev(sb->s_bdev);
1886884d179dSJan Kara 	/* Now the quota files are just ordinary files and we can set the
1887884d179dSJan Kara 	 * inode flags back. Moreover we discard the pagecache so that
1888884d179dSJan Kara 	 * userspace sees the writes we did bypassing the pagecache. We
1889884d179dSJan Kara 	 * must also discard the blockdev buffers so that we see the
1890884d179dSJan Kara 	 * changes done by userspace on the next quotaon() */
1891884d179dSJan Kara 	for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1892884d179dSJan Kara 		if (toputinode[cnt]) {
1893884d179dSJan Kara 			mutex_lock(&dqopt->dqonoff_mutex);
1894884d179dSJan Kara 			/* If quota was reenabled in the meantime, we have
1895884d179dSJan Kara 			 * nothing to do */
1896884d179dSJan Kara 			if (!sb_has_quota_loaded(sb, cnt)) {
1897268157baSJan Kara 				mutex_lock_nested(&toputinode[cnt]->i_mutex,
1898268157baSJan Kara 						  I_MUTEX_QUOTA);
1899884d179dSJan Kara 				toputinode[cnt]->i_flags &= ~(S_IMMUTABLE |
1900884d179dSJan Kara 				  S_NOATIME | S_NOQUOTA);
1901268157baSJan Kara 				truncate_inode_pages(&toputinode[cnt]->i_data,
1902268157baSJan Kara 						     0);
1903884d179dSJan Kara 				mutex_unlock(&toputinode[cnt]->i_mutex);
1904884d179dSJan Kara 				mark_inode_dirty(toputinode[cnt]);
1905884d179dSJan Kara 			}
1906884d179dSJan Kara 			mutex_unlock(&dqopt->dqonoff_mutex);
1907884d179dSJan Kara 		}
1908884d179dSJan Kara 	if (sb->s_bdev)
1909884d179dSJan Kara 		invalidate_bdev(sb->s_bdev);
1910884d179dSJan Kara put_inodes:
1911884d179dSJan Kara 	for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1912884d179dSJan Kara 		if (toputinode[cnt]) {
1913884d179dSJan Kara 			/* On remount RO, we keep the inode pointer so that we
1914884d179dSJan Kara 			 * can reenable quota on the subsequent remount RW. We
1915884d179dSJan Kara 			 * have to check 'flags' variable and not use sb_has_
1916884d179dSJan Kara 			 * function because another quotaon / quotaoff could
1917884d179dSJan Kara 			 * change global state before we got here. We refuse
1918884d179dSJan Kara 			 * to suspend quotas when there is pending delete on
1919884d179dSJan Kara 			 * the quota file... */
1920884d179dSJan Kara 			if (!(flags & DQUOT_SUSPENDED))
1921884d179dSJan Kara 				iput(toputinode[cnt]);
1922884d179dSJan Kara 			else if (!toputinode[cnt]->i_nlink)
1923884d179dSJan Kara 				ret = -EBUSY;
1924884d179dSJan Kara 		}
1925884d179dSJan Kara 	return ret;
1926884d179dSJan Kara }
1927884d179dSJan Kara EXPORT_SYMBOL(vfs_quota_disable);
1928884d179dSJan Kara 
1929884d179dSJan Kara int vfs_quota_off(struct super_block *sb, int type, int remount)
1930884d179dSJan Kara {
1931884d179dSJan Kara 	return vfs_quota_disable(sb, type, remount ? DQUOT_SUSPENDED :
1932884d179dSJan Kara 				 (DQUOT_USAGE_ENABLED | DQUOT_LIMITS_ENABLED));
1933884d179dSJan Kara }
1934884d179dSJan Kara EXPORT_SYMBOL(vfs_quota_off);
1935884d179dSJan Kara /*
1936884d179dSJan Kara  *	Turn quotas on on a device
1937884d179dSJan Kara  */
1938884d179dSJan Kara 
1939884d179dSJan Kara /*
1940884d179dSJan Kara  * Helper function to turn quotas on when we already have the inode of
1941884d179dSJan Kara  * quota file and no quota information is loaded.
1942884d179dSJan Kara  */
1943884d179dSJan Kara static int vfs_load_quota_inode(struct inode *inode, int type, int format_id,
1944884d179dSJan Kara 	unsigned int flags)
1945884d179dSJan Kara {
1946884d179dSJan Kara 	struct quota_format_type *fmt = find_quota_format(format_id);
1947884d179dSJan Kara 	struct super_block *sb = inode->i_sb;
1948884d179dSJan Kara 	struct quota_info *dqopt = sb_dqopt(sb);
1949884d179dSJan Kara 	int error;
1950884d179dSJan Kara 	int oldflags = -1;
1951884d179dSJan Kara 
1952884d179dSJan Kara 	if (!fmt)
1953884d179dSJan Kara 		return -ESRCH;
1954884d179dSJan Kara 	if (!S_ISREG(inode->i_mode)) {
1955884d179dSJan Kara 		error = -EACCES;
1956884d179dSJan Kara 		goto out_fmt;
1957884d179dSJan Kara 	}
1958884d179dSJan Kara 	if (IS_RDONLY(inode)) {
1959884d179dSJan Kara 		error = -EROFS;
1960884d179dSJan Kara 		goto out_fmt;
1961884d179dSJan Kara 	}
1962884d179dSJan Kara 	if (!sb->s_op->quota_write || !sb->s_op->quota_read) {
1963884d179dSJan Kara 		error = -EINVAL;
1964884d179dSJan Kara 		goto out_fmt;
1965884d179dSJan Kara 	}
1966884d179dSJan Kara 	/* Usage always has to be set... */
1967884d179dSJan Kara 	if (!(flags & DQUOT_USAGE_ENABLED)) {
1968884d179dSJan Kara 		error = -EINVAL;
1969884d179dSJan Kara 		goto out_fmt;
1970884d179dSJan Kara 	}
1971884d179dSJan Kara 
1972884d179dSJan Kara 	if (!(dqopt->flags & DQUOT_QUOTA_SYS_FILE)) {
1973884d179dSJan Kara 		/* As we bypass the pagecache we must now flush the inode so
1974884d179dSJan Kara 		 * that we see all the changes from userspace... */
1975884d179dSJan Kara 		write_inode_now(inode, 1);
1976884d179dSJan Kara 		/* And now flush the block cache so that kernel sees the
1977884d179dSJan Kara 		 * changes */
1978884d179dSJan Kara 		invalidate_bdev(sb->s_bdev);
1979884d179dSJan Kara 	}
1980884d179dSJan Kara 	mutex_lock(&dqopt->dqonoff_mutex);
1981884d179dSJan Kara 	if (sb_has_quota_loaded(sb, type)) {
1982884d179dSJan Kara 		error = -EBUSY;
1983884d179dSJan Kara 		goto out_lock;
1984884d179dSJan Kara 	}
1985884d179dSJan Kara 
1986884d179dSJan Kara 	if (!(dqopt->flags & DQUOT_QUOTA_SYS_FILE)) {
1987884d179dSJan Kara 		/* We don't want quota and atime on quota files (deadlocks
1988884d179dSJan Kara 		 * possible) Also nobody should write to the file - we use
1989884d179dSJan Kara 		 * special IO operations which ignore the immutable bit. */
1990dee86565SJan Kara 		mutex_lock_nested(&inode->i_mutex, I_MUTEX_QUOTA);
1991268157baSJan Kara 		oldflags = inode->i_flags & (S_NOATIME | S_IMMUTABLE |
1992268157baSJan Kara 					     S_NOQUOTA);
1993884d179dSJan Kara 		inode->i_flags |= S_NOQUOTA | S_NOATIME | S_IMMUTABLE;
1994dee86565SJan Kara 		mutex_unlock(&inode->i_mutex);
199526245c94SJan Kara 		/*
199626245c94SJan Kara 		 * When S_NOQUOTA is set, remove dquot references as no more
199726245c94SJan Kara 		 * references can be added
199826245c94SJan Kara 		 */
1999884d179dSJan Kara 		sb->dq_op->drop(inode);
2000884d179dSJan Kara 	}
2001884d179dSJan Kara 
2002884d179dSJan Kara 	error = -EIO;
2003884d179dSJan Kara 	dqopt->files[type] = igrab(inode);
2004884d179dSJan Kara 	if (!dqopt->files[type])
2005884d179dSJan Kara 		goto out_lock;
2006884d179dSJan Kara 	error = -EINVAL;
2007884d179dSJan Kara 	if (!fmt->qf_ops->check_quota_file(sb, type))
2008884d179dSJan Kara 		goto out_file_init;
2009884d179dSJan Kara 
2010884d179dSJan Kara 	dqopt->ops[type] = fmt->qf_ops;
2011884d179dSJan Kara 	dqopt->info[type].dqi_format = fmt;
2012884d179dSJan Kara 	dqopt->info[type].dqi_fmt_id = format_id;
2013884d179dSJan Kara 	INIT_LIST_HEAD(&dqopt->info[type].dqi_dirty_list);
2014884d179dSJan Kara 	mutex_lock(&dqopt->dqio_mutex);
2015268157baSJan Kara 	error = dqopt->ops[type]->read_file_info(sb, type);
2016268157baSJan Kara 	if (error < 0) {
2017884d179dSJan Kara 		mutex_unlock(&dqopt->dqio_mutex);
2018884d179dSJan Kara 		goto out_file_init;
2019884d179dSJan Kara 	}
2020884d179dSJan Kara 	mutex_unlock(&dqopt->dqio_mutex);
2021884d179dSJan Kara 	spin_lock(&dq_state_lock);
2022884d179dSJan Kara 	dqopt->flags |= dquot_state_flag(flags, type);
2023884d179dSJan Kara 	spin_unlock(&dq_state_lock);
2024884d179dSJan Kara 
2025884d179dSJan Kara 	add_dquot_ref(sb, type);
2026884d179dSJan Kara 	mutex_unlock(&dqopt->dqonoff_mutex);
2027884d179dSJan Kara 
2028884d179dSJan Kara 	return 0;
2029884d179dSJan Kara 
2030884d179dSJan Kara out_file_init:
2031884d179dSJan Kara 	dqopt->files[type] = NULL;
2032884d179dSJan Kara 	iput(inode);
2033884d179dSJan Kara out_lock:
2034884d179dSJan Kara 	if (oldflags != -1) {
2035dee86565SJan Kara 		mutex_lock_nested(&inode->i_mutex, I_MUTEX_QUOTA);
2036884d179dSJan Kara 		/* Set the flags back (in the case of accidental quotaon()
2037884d179dSJan Kara 		 * on a wrong file we don't want to mess up the flags) */
2038884d179dSJan Kara 		inode->i_flags &= ~(S_NOATIME | S_NOQUOTA | S_IMMUTABLE);
2039884d179dSJan Kara 		inode->i_flags |= oldflags;
2040dee86565SJan Kara 		mutex_unlock(&inode->i_mutex);
2041884d179dSJan Kara 	}
2042d01730d7SJiaying Zhang 	mutex_unlock(&dqopt->dqonoff_mutex);
2043884d179dSJan Kara out_fmt:
2044884d179dSJan Kara 	put_quota_format(fmt);
2045884d179dSJan Kara 
2046884d179dSJan Kara 	return error;
2047884d179dSJan Kara }
2048884d179dSJan Kara 
2049884d179dSJan Kara /* Reenable quotas on remount RW */
2050884d179dSJan Kara static int vfs_quota_on_remount(struct super_block *sb, int type)
2051884d179dSJan Kara {
2052884d179dSJan Kara 	struct quota_info *dqopt = sb_dqopt(sb);
2053884d179dSJan Kara 	struct inode *inode;
2054884d179dSJan Kara 	int ret;
2055884d179dSJan Kara 	unsigned int flags;
2056884d179dSJan Kara 
2057884d179dSJan Kara 	mutex_lock(&dqopt->dqonoff_mutex);
2058884d179dSJan Kara 	if (!sb_has_quota_suspended(sb, type)) {
2059884d179dSJan Kara 		mutex_unlock(&dqopt->dqonoff_mutex);
2060884d179dSJan Kara 		return 0;
2061884d179dSJan Kara 	}
2062884d179dSJan Kara 	inode = dqopt->files[type];
2063884d179dSJan Kara 	dqopt->files[type] = NULL;
2064884d179dSJan Kara 	spin_lock(&dq_state_lock);
2065884d179dSJan Kara 	flags = dqopt->flags & dquot_state_flag(DQUOT_USAGE_ENABLED |
2066884d179dSJan Kara 						DQUOT_LIMITS_ENABLED, type);
2067884d179dSJan Kara 	dqopt->flags &= ~dquot_state_flag(DQUOT_STATE_FLAGS, type);
2068884d179dSJan Kara 	spin_unlock(&dq_state_lock);
2069884d179dSJan Kara 	mutex_unlock(&dqopt->dqonoff_mutex);
2070884d179dSJan Kara 
2071884d179dSJan Kara 	flags = dquot_generic_flag(flags, type);
2072884d179dSJan Kara 	ret = vfs_load_quota_inode(inode, type, dqopt->info[type].dqi_fmt_id,
2073884d179dSJan Kara 				   flags);
2074884d179dSJan Kara 	iput(inode);
2075884d179dSJan Kara 
2076884d179dSJan Kara 	return ret;
2077884d179dSJan Kara }
2078884d179dSJan Kara 
2079884d179dSJan Kara int vfs_quota_on_path(struct super_block *sb, int type, int format_id,
2080884d179dSJan Kara 		      struct path *path)
2081884d179dSJan Kara {
2082884d179dSJan Kara 	int error = security_quota_on(path->dentry);
2083884d179dSJan Kara 	if (error)
2084884d179dSJan Kara 		return error;
2085884d179dSJan Kara 	/* Quota file not on the same filesystem? */
2086884d179dSJan Kara 	if (path->mnt->mnt_sb != sb)
2087884d179dSJan Kara 		error = -EXDEV;
2088884d179dSJan Kara 	else
2089884d179dSJan Kara 		error = vfs_load_quota_inode(path->dentry->d_inode, type,
2090884d179dSJan Kara 					     format_id, DQUOT_USAGE_ENABLED |
2091884d179dSJan Kara 					     DQUOT_LIMITS_ENABLED);
2092884d179dSJan Kara 	return error;
2093884d179dSJan Kara }
2094884d179dSJan Kara EXPORT_SYMBOL(vfs_quota_on_path);
2095884d179dSJan Kara 
2096884d179dSJan Kara int vfs_quota_on(struct super_block *sb, int type, int format_id, char *name,
2097884d179dSJan Kara 		 int remount)
2098884d179dSJan Kara {
2099884d179dSJan Kara 	struct path path;
2100884d179dSJan Kara 	int error;
2101884d179dSJan Kara 
2102884d179dSJan Kara 	if (remount)
2103884d179dSJan Kara 		return vfs_quota_on_remount(sb, type);
2104884d179dSJan Kara 
2105884d179dSJan Kara 	error = kern_path(name, LOOKUP_FOLLOW, &path);
2106884d179dSJan Kara 	if (!error) {
2107884d179dSJan Kara 		error = vfs_quota_on_path(sb, type, format_id, &path);
2108884d179dSJan Kara 		path_put(&path);
2109884d179dSJan Kara 	}
2110884d179dSJan Kara 	return error;
2111884d179dSJan Kara }
2112884d179dSJan Kara EXPORT_SYMBOL(vfs_quota_on);
2113884d179dSJan Kara 
2114884d179dSJan Kara /*
2115884d179dSJan Kara  * More powerful function for turning on quotas allowing setting
2116884d179dSJan Kara  * of individual quota flags
2117884d179dSJan Kara  */
2118884d179dSJan Kara int vfs_quota_enable(struct inode *inode, int type, int format_id,
2119884d179dSJan Kara 		unsigned int flags)
2120884d179dSJan Kara {
2121884d179dSJan Kara 	int ret = 0;
2122884d179dSJan Kara 	struct super_block *sb = inode->i_sb;
2123884d179dSJan Kara 	struct quota_info *dqopt = sb_dqopt(sb);
2124884d179dSJan Kara 
2125884d179dSJan Kara 	/* Just unsuspend quotas? */
2126884d179dSJan Kara 	if (flags & DQUOT_SUSPENDED)
2127884d179dSJan Kara 		return vfs_quota_on_remount(sb, type);
2128884d179dSJan Kara 	if (!flags)
2129884d179dSJan Kara 		return 0;
2130884d179dSJan Kara 	/* Just updating flags needed? */
2131884d179dSJan Kara 	if (sb_has_quota_loaded(sb, type)) {
2132884d179dSJan Kara 		mutex_lock(&dqopt->dqonoff_mutex);
2133884d179dSJan Kara 		/* Now do a reliable test... */
2134884d179dSJan Kara 		if (!sb_has_quota_loaded(sb, type)) {
2135884d179dSJan Kara 			mutex_unlock(&dqopt->dqonoff_mutex);
2136884d179dSJan Kara 			goto load_quota;
2137884d179dSJan Kara 		}
2138884d179dSJan Kara 		if (flags & DQUOT_USAGE_ENABLED &&
2139884d179dSJan Kara 		    sb_has_quota_usage_enabled(sb, type)) {
2140884d179dSJan Kara 			ret = -EBUSY;
2141884d179dSJan Kara 			goto out_lock;
2142884d179dSJan Kara 		}
2143884d179dSJan Kara 		if (flags & DQUOT_LIMITS_ENABLED &&
2144884d179dSJan Kara 		    sb_has_quota_limits_enabled(sb, type)) {
2145884d179dSJan Kara 			ret = -EBUSY;
2146884d179dSJan Kara 			goto out_lock;
2147884d179dSJan Kara 		}
2148884d179dSJan Kara 		spin_lock(&dq_state_lock);
2149884d179dSJan Kara 		sb_dqopt(sb)->flags |= dquot_state_flag(flags, type);
2150884d179dSJan Kara 		spin_unlock(&dq_state_lock);
2151884d179dSJan Kara out_lock:
2152884d179dSJan Kara 		mutex_unlock(&dqopt->dqonoff_mutex);
2153884d179dSJan Kara 		return ret;
2154884d179dSJan Kara 	}
2155884d179dSJan Kara 
2156884d179dSJan Kara load_quota:
2157884d179dSJan Kara 	return vfs_load_quota_inode(inode, type, format_id, flags);
2158884d179dSJan Kara }
2159884d179dSJan Kara EXPORT_SYMBOL(vfs_quota_enable);
2160884d179dSJan Kara 
2161884d179dSJan Kara /*
2162884d179dSJan Kara  * This function is used when filesystem needs to initialize quotas
2163884d179dSJan Kara  * during mount time.
2164884d179dSJan Kara  */
2165884d179dSJan Kara int vfs_quota_on_mount(struct super_block *sb, char *qf_name,
2166884d179dSJan Kara 		int format_id, int type)
2167884d179dSJan Kara {
2168884d179dSJan Kara 	struct dentry *dentry;
2169884d179dSJan Kara 	int error;
2170884d179dSJan Kara 
2171c56818d7SJan Kara 	mutex_lock(&sb->s_root->d_inode->i_mutex);
2172884d179dSJan Kara 	dentry = lookup_one_len(qf_name, sb->s_root, strlen(qf_name));
2173c56818d7SJan Kara 	mutex_unlock(&sb->s_root->d_inode->i_mutex);
2174884d179dSJan Kara 	if (IS_ERR(dentry))
2175884d179dSJan Kara 		return PTR_ERR(dentry);
2176884d179dSJan Kara 
2177884d179dSJan Kara 	if (!dentry->d_inode) {
2178884d179dSJan Kara 		error = -ENOENT;
2179884d179dSJan Kara 		goto out;
2180884d179dSJan Kara 	}
2181884d179dSJan Kara 
2182884d179dSJan Kara 	error = security_quota_on(dentry);
2183884d179dSJan Kara 	if (!error)
2184884d179dSJan Kara 		error = vfs_load_quota_inode(dentry->d_inode, type, format_id,
2185884d179dSJan Kara 				DQUOT_USAGE_ENABLED | DQUOT_LIMITS_ENABLED);
2186884d179dSJan Kara 
2187884d179dSJan Kara out:
2188884d179dSJan Kara 	dput(dentry);
2189884d179dSJan Kara 	return error;
2190884d179dSJan Kara }
2191884d179dSJan Kara EXPORT_SYMBOL(vfs_quota_on_mount);
2192884d179dSJan Kara 
2193884d179dSJan Kara /* Wrapper to turn on quotas when remounting rw */
2194884d179dSJan Kara int vfs_dq_quota_on_remount(struct super_block *sb)
2195884d179dSJan Kara {
2196884d179dSJan Kara 	int cnt;
2197884d179dSJan Kara 	int ret = 0, err;
2198884d179dSJan Kara 
2199884d179dSJan Kara 	if (!sb->s_qcop || !sb->s_qcop->quota_on)
2200884d179dSJan Kara 		return -ENOSYS;
2201884d179dSJan Kara 	for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
2202884d179dSJan Kara 		err = sb->s_qcop->quota_on(sb, cnt, 0, NULL, 1);
2203884d179dSJan Kara 		if (err < 0 && !ret)
2204884d179dSJan Kara 			ret = err;
2205884d179dSJan Kara 	}
2206884d179dSJan Kara 	return ret;
2207884d179dSJan Kara }
2208884d179dSJan Kara EXPORT_SYMBOL(vfs_dq_quota_on_remount);
2209884d179dSJan Kara 
2210884d179dSJan Kara static inline qsize_t qbtos(qsize_t blocks)
2211884d179dSJan Kara {
2212884d179dSJan Kara 	return blocks << QIF_DQBLKSIZE_BITS;
2213884d179dSJan Kara }
2214884d179dSJan Kara 
2215884d179dSJan Kara static inline qsize_t stoqb(qsize_t space)
2216884d179dSJan Kara {
2217884d179dSJan Kara 	return (space + QIF_DQBLKSIZE - 1) >> QIF_DQBLKSIZE_BITS;
2218884d179dSJan Kara }
2219884d179dSJan Kara 
2220884d179dSJan Kara /* Generic routine for getting common part of quota structure */
2221884d179dSJan Kara static void do_get_dqblk(struct dquot *dquot, struct if_dqblk *di)
2222884d179dSJan Kara {
2223884d179dSJan Kara 	struct mem_dqblk *dm = &dquot->dq_dqb;
2224884d179dSJan Kara 
2225884d179dSJan Kara 	spin_lock(&dq_data_lock);
2226884d179dSJan Kara 	di->dqb_bhardlimit = stoqb(dm->dqb_bhardlimit);
2227884d179dSJan Kara 	di->dqb_bsoftlimit = stoqb(dm->dqb_bsoftlimit);
2228884d179dSJan Kara 	di->dqb_curspace = dm->dqb_curspace + dm->dqb_rsvspace;
2229884d179dSJan Kara 	di->dqb_ihardlimit = dm->dqb_ihardlimit;
2230884d179dSJan Kara 	di->dqb_isoftlimit = dm->dqb_isoftlimit;
2231884d179dSJan Kara 	di->dqb_curinodes = dm->dqb_curinodes;
2232884d179dSJan Kara 	di->dqb_btime = dm->dqb_btime;
2233884d179dSJan Kara 	di->dqb_itime = dm->dqb_itime;
2234884d179dSJan Kara 	di->dqb_valid = QIF_ALL;
2235884d179dSJan Kara 	spin_unlock(&dq_data_lock);
2236884d179dSJan Kara }
2237884d179dSJan Kara 
2238268157baSJan Kara int vfs_get_dqblk(struct super_block *sb, int type, qid_t id,
2239268157baSJan Kara 		  struct if_dqblk *di)
2240884d179dSJan Kara {
2241884d179dSJan Kara 	struct dquot *dquot;
2242884d179dSJan Kara 
2243884d179dSJan Kara 	dquot = dqget(sb, id, type);
2244dd6f3c6dSJan Kara 	if (!dquot)
2245884d179dSJan Kara 		return -ESRCH;
2246884d179dSJan Kara 	do_get_dqblk(dquot, di);
2247884d179dSJan Kara 	dqput(dquot);
2248884d179dSJan Kara 
2249884d179dSJan Kara 	return 0;
2250884d179dSJan Kara }
2251884d179dSJan Kara EXPORT_SYMBOL(vfs_get_dqblk);
2252884d179dSJan Kara 
2253884d179dSJan Kara /* Generic routine for setting common part of quota structure */
2254884d179dSJan Kara static int do_set_dqblk(struct dquot *dquot, struct if_dqblk *di)
2255884d179dSJan Kara {
2256884d179dSJan Kara 	struct mem_dqblk *dm = &dquot->dq_dqb;
2257884d179dSJan Kara 	int check_blim = 0, check_ilim = 0;
2258884d179dSJan Kara 	struct mem_dqinfo *dqi = &sb_dqopt(dquot->dq_sb)->info[dquot->dq_type];
2259884d179dSJan Kara 
2260884d179dSJan Kara 	if ((di->dqb_valid & QIF_BLIMITS &&
2261884d179dSJan Kara 	     (di->dqb_bhardlimit > dqi->dqi_maxblimit ||
2262884d179dSJan Kara 	      di->dqb_bsoftlimit > dqi->dqi_maxblimit)) ||
2263884d179dSJan Kara 	    (di->dqb_valid & QIF_ILIMITS &&
2264884d179dSJan Kara 	     (di->dqb_ihardlimit > dqi->dqi_maxilimit ||
2265884d179dSJan Kara 	      di->dqb_isoftlimit > dqi->dqi_maxilimit)))
2266884d179dSJan Kara 		return -ERANGE;
2267884d179dSJan Kara 
2268884d179dSJan Kara 	spin_lock(&dq_data_lock);
2269884d179dSJan Kara 	if (di->dqb_valid & QIF_SPACE) {
2270884d179dSJan Kara 		dm->dqb_curspace = di->dqb_curspace - dm->dqb_rsvspace;
2271884d179dSJan Kara 		check_blim = 1;
2272884d179dSJan Kara 		__set_bit(DQ_LASTSET_B + QIF_SPACE_B, &dquot->dq_flags);
2273884d179dSJan Kara 	}
2274884d179dSJan Kara 	if (di->dqb_valid & QIF_BLIMITS) {
2275884d179dSJan Kara 		dm->dqb_bsoftlimit = qbtos(di->dqb_bsoftlimit);
2276884d179dSJan Kara 		dm->dqb_bhardlimit = qbtos(di->dqb_bhardlimit);
2277884d179dSJan Kara 		check_blim = 1;
2278884d179dSJan Kara 		__set_bit(DQ_LASTSET_B + QIF_BLIMITS_B, &dquot->dq_flags);
2279884d179dSJan Kara 	}
2280884d179dSJan Kara 	if (di->dqb_valid & QIF_INODES) {
2281884d179dSJan Kara 		dm->dqb_curinodes = di->dqb_curinodes;
2282884d179dSJan Kara 		check_ilim = 1;
2283884d179dSJan Kara 		__set_bit(DQ_LASTSET_B + QIF_INODES_B, &dquot->dq_flags);
2284884d179dSJan Kara 	}
2285884d179dSJan Kara 	if (di->dqb_valid & QIF_ILIMITS) {
2286884d179dSJan Kara 		dm->dqb_isoftlimit = di->dqb_isoftlimit;
2287884d179dSJan Kara 		dm->dqb_ihardlimit = di->dqb_ihardlimit;
2288884d179dSJan Kara 		check_ilim = 1;
2289884d179dSJan Kara 		__set_bit(DQ_LASTSET_B + QIF_ILIMITS_B, &dquot->dq_flags);
2290884d179dSJan Kara 	}
2291884d179dSJan Kara 	if (di->dqb_valid & QIF_BTIME) {
2292884d179dSJan Kara 		dm->dqb_btime = di->dqb_btime;
2293884d179dSJan Kara 		check_blim = 1;
2294884d179dSJan Kara 		__set_bit(DQ_LASTSET_B + QIF_BTIME_B, &dquot->dq_flags);
2295884d179dSJan Kara 	}
2296884d179dSJan Kara 	if (di->dqb_valid & QIF_ITIME) {
2297884d179dSJan Kara 		dm->dqb_itime = di->dqb_itime;
2298884d179dSJan Kara 		check_ilim = 1;
2299884d179dSJan Kara 		__set_bit(DQ_LASTSET_B + QIF_ITIME_B, &dquot->dq_flags);
2300884d179dSJan Kara 	}
2301884d179dSJan Kara 
2302884d179dSJan Kara 	if (check_blim) {
2303268157baSJan Kara 		if (!dm->dqb_bsoftlimit ||
2304268157baSJan Kara 		    dm->dqb_curspace < dm->dqb_bsoftlimit) {
2305884d179dSJan Kara 			dm->dqb_btime = 0;
2306884d179dSJan Kara 			clear_bit(DQ_BLKS_B, &dquot->dq_flags);
2307268157baSJan Kara 		} else if (!(di->dqb_valid & QIF_BTIME))
2308268157baSJan Kara 			/* Set grace only if user hasn't provided his own... */
2309884d179dSJan Kara 			dm->dqb_btime = get_seconds() + dqi->dqi_bgrace;
2310884d179dSJan Kara 	}
2311884d179dSJan Kara 	if (check_ilim) {
2312268157baSJan Kara 		if (!dm->dqb_isoftlimit ||
2313268157baSJan Kara 		    dm->dqb_curinodes < dm->dqb_isoftlimit) {
2314884d179dSJan Kara 			dm->dqb_itime = 0;
2315884d179dSJan Kara 			clear_bit(DQ_INODES_B, &dquot->dq_flags);
2316268157baSJan Kara 		} else if (!(di->dqb_valid & QIF_ITIME))
2317268157baSJan Kara 			/* Set grace only if user hasn't provided his own... */
2318884d179dSJan Kara 			dm->dqb_itime = get_seconds() + dqi->dqi_igrace;
2319884d179dSJan Kara 	}
2320268157baSJan Kara 	if (dm->dqb_bhardlimit || dm->dqb_bsoftlimit || dm->dqb_ihardlimit ||
2321268157baSJan Kara 	    dm->dqb_isoftlimit)
2322884d179dSJan Kara 		clear_bit(DQ_FAKE_B, &dquot->dq_flags);
2323884d179dSJan Kara 	else
2324884d179dSJan Kara 		set_bit(DQ_FAKE_B, &dquot->dq_flags);
2325884d179dSJan Kara 	spin_unlock(&dq_data_lock);
2326884d179dSJan Kara 	mark_dquot_dirty(dquot);
2327884d179dSJan Kara 
2328884d179dSJan Kara 	return 0;
2329884d179dSJan Kara }
2330884d179dSJan Kara 
2331268157baSJan Kara int vfs_set_dqblk(struct super_block *sb, int type, qid_t id,
2332268157baSJan Kara 		  struct if_dqblk *di)
2333884d179dSJan Kara {
2334884d179dSJan Kara 	struct dquot *dquot;
2335884d179dSJan Kara 	int rc;
2336884d179dSJan Kara 
2337884d179dSJan Kara 	dquot = dqget(sb, id, type);
2338884d179dSJan Kara 	if (!dquot) {
2339884d179dSJan Kara 		rc = -ESRCH;
2340884d179dSJan Kara 		goto out;
2341884d179dSJan Kara 	}
2342884d179dSJan Kara 	rc = do_set_dqblk(dquot, di);
2343884d179dSJan Kara 	dqput(dquot);
2344884d179dSJan Kara out:
2345884d179dSJan Kara 	return rc;
2346884d179dSJan Kara }
2347884d179dSJan Kara EXPORT_SYMBOL(vfs_set_dqblk);
2348884d179dSJan Kara 
2349884d179dSJan Kara /* Generic routine for getting common part of quota file information */
2350884d179dSJan Kara int vfs_get_dqinfo(struct super_block *sb, int type, struct if_dqinfo *ii)
2351884d179dSJan Kara {
2352884d179dSJan Kara 	struct mem_dqinfo *mi;
2353884d179dSJan Kara 
2354884d179dSJan Kara 	mutex_lock(&sb_dqopt(sb)->dqonoff_mutex);
2355884d179dSJan Kara 	if (!sb_has_quota_active(sb, type)) {
2356884d179dSJan Kara 		mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
2357884d179dSJan Kara 		return -ESRCH;
2358884d179dSJan Kara 	}
2359884d179dSJan Kara 	mi = sb_dqopt(sb)->info + type;
2360884d179dSJan Kara 	spin_lock(&dq_data_lock);
2361884d179dSJan Kara 	ii->dqi_bgrace = mi->dqi_bgrace;
2362884d179dSJan Kara 	ii->dqi_igrace = mi->dqi_igrace;
2363884d179dSJan Kara 	ii->dqi_flags = mi->dqi_flags & DQF_MASK;
2364884d179dSJan Kara 	ii->dqi_valid = IIF_ALL;
2365884d179dSJan Kara 	spin_unlock(&dq_data_lock);
2366884d179dSJan Kara 	mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
2367884d179dSJan Kara 	return 0;
2368884d179dSJan Kara }
2369884d179dSJan Kara EXPORT_SYMBOL(vfs_get_dqinfo);
2370884d179dSJan Kara 
2371884d179dSJan Kara /* Generic routine for setting common part of quota file information */
2372884d179dSJan Kara int vfs_set_dqinfo(struct super_block *sb, int type, struct if_dqinfo *ii)
2373884d179dSJan Kara {
2374884d179dSJan Kara 	struct mem_dqinfo *mi;
2375884d179dSJan Kara 	int err = 0;
2376884d179dSJan Kara 
2377884d179dSJan Kara 	mutex_lock(&sb_dqopt(sb)->dqonoff_mutex);
2378884d179dSJan Kara 	if (!sb_has_quota_active(sb, type)) {
2379884d179dSJan Kara 		err = -ESRCH;
2380884d179dSJan Kara 		goto out;
2381884d179dSJan Kara 	}
2382884d179dSJan Kara 	mi = sb_dqopt(sb)->info + type;
2383884d179dSJan Kara 	spin_lock(&dq_data_lock);
2384884d179dSJan Kara 	if (ii->dqi_valid & IIF_BGRACE)
2385884d179dSJan Kara 		mi->dqi_bgrace = ii->dqi_bgrace;
2386884d179dSJan Kara 	if (ii->dqi_valid & IIF_IGRACE)
2387884d179dSJan Kara 		mi->dqi_igrace = ii->dqi_igrace;
2388884d179dSJan Kara 	if (ii->dqi_valid & IIF_FLAGS)
2389268157baSJan Kara 		mi->dqi_flags = (mi->dqi_flags & ~DQF_MASK) |
2390268157baSJan Kara 				(ii->dqi_flags & DQF_MASK);
2391884d179dSJan Kara 	spin_unlock(&dq_data_lock);
2392884d179dSJan Kara 	mark_info_dirty(sb, type);
2393884d179dSJan Kara 	/* Force write to disk */
2394884d179dSJan Kara 	sb->dq_op->write_info(sb, type);
2395884d179dSJan Kara out:
2396884d179dSJan Kara 	mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
2397884d179dSJan Kara 	return err;
2398884d179dSJan Kara }
2399884d179dSJan Kara EXPORT_SYMBOL(vfs_set_dqinfo);
2400884d179dSJan Kara 
24010d54b217SAlexey Dobriyan const struct quotactl_ops vfs_quotactl_ops = {
2402884d179dSJan Kara 	.quota_on	= vfs_quota_on,
2403884d179dSJan Kara 	.quota_off	= vfs_quota_off,
2404884d179dSJan Kara 	.quota_sync	= vfs_quota_sync,
2405884d179dSJan Kara 	.get_info	= vfs_get_dqinfo,
2406884d179dSJan Kara 	.set_info	= vfs_set_dqinfo,
2407884d179dSJan Kara 	.get_dqblk	= vfs_get_dqblk,
2408884d179dSJan Kara 	.set_dqblk	= vfs_set_dqblk
2409884d179dSJan Kara };
2410884d179dSJan Kara 
2411884d179dSJan Kara static ctl_table fs_dqstats_table[] = {
2412884d179dSJan Kara 	{
2413884d179dSJan Kara 		.procname	= "lookups",
2414884d179dSJan Kara 		.data		= &dqstats.lookups,
2415884d179dSJan Kara 		.maxlen		= sizeof(int),
2416884d179dSJan Kara 		.mode		= 0444,
24176d456111SEric W. Biederman 		.proc_handler	= proc_dointvec,
2418884d179dSJan Kara 	},
2419884d179dSJan Kara 	{
2420884d179dSJan Kara 		.procname	= "drops",
2421884d179dSJan Kara 		.data		= &dqstats.drops,
2422884d179dSJan Kara 		.maxlen		= sizeof(int),
2423884d179dSJan Kara 		.mode		= 0444,
24246d456111SEric W. Biederman 		.proc_handler	= proc_dointvec,
2425884d179dSJan Kara 	},
2426884d179dSJan Kara 	{
2427884d179dSJan Kara 		.procname	= "reads",
2428884d179dSJan Kara 		.data		= &dqstats.reads,
2429884d179dSJan Kara 		.maxlen		= sizeof(int),
2430884d179dSJan Kara 		.mode		= 0444,
24316d456111SEric W. Biederman 		.proc_handler	= proc_dointvec,
2432884d179dSJan Kara 	},
2433884d179dSJan Kara 	{
2434884d179dSJan Kara 		.procname	= "writes",
2435884d179dSJan Kara 		.data		= &dqstats.writes,
2436884d179dSJan Kara 		.maxlen		= sizeof(int),
2437884d179dSJan Kara 		.mode		= 0444,
24386d456111SEric W. Biederman 		.proc_handler	= proc_dointvec,
2439884d179dSJan Kara 	},
2440884d179dSJan Kara 	{
2441884d179dSJan Kara 		.procname	= "cache_hits",
2442884d179dSJan Kara 		.data		= &dqstats.cache_hits,
2443884d179dSJan Kara 		.maxlen		= sizeof(int),
2444884d179dSJan Kara 		.mode		= 0444,
24456d456111SEric W. Biederman 		.proc_handler	= proc_dointvec,
2446884d179dSJan Kara 	},
2447884d179dSJan Kara 	{
2448884d179dSJan Kara 		.procname	= "allocated_dquots",
2449884d179dSJan Kara 		.data		= &dqstats.allocated_dquots,
2450884d179dSJan Kara 		.maxlen		= sizeof(int),
2451884d179dSJan Kara 		.mode		= 0444,
24526d456111SEric W. Biederman 		.proc_handler	= proc_dointvec,
2453884d179dSJan Kara 	},
2454884d179dSJan Kara 	{
2455884d179dSJan Kara 		.procname	= "free_dquots",
2456884d179dSJan Kara 		.data		= &dqstats.free_dquots,
2457884d179dSJan Kara 		.maxlen		= sizeof(int),
2458884d179dSJan Kara 		.mode		= 0444,
24596d456111SEric W. Biederman 		.proc_handler	= proc_dointvec,
2460884d179dSJan Kara 	},
2461884d179dSJan Kara 	{
2462884d179dSJan Kara 		.procname	= "syncs",
2463884d179dSJan Kara 		.data		= &dqstats.syncs,
2464884d179dSJan Kara 		.maxlen		= sizeof(int),
2465884d179dSJan Kara 		.mode		= 0444,
24666d456111SEric W. Biederman 		.proc_handler	= proc_dointvec,
2467884d179dSJan Kara 	},
2468884d179dSJan Kara #ifdef CONFIG_PRINT_QUOTA_WARNING
2469884d179dSJan Kara 	{
2470884d179dSJan Kara 		.procname	= "warnings",
2471884d179dSJan Kara 		.data		= &flag_print_warnings,
2472884d179dSJan Kara 		.maxlen		= sizeof(int),
2473884d179dSJan Kara 		.mode		= 0644,
24746d456111SEric W. Biederman 		.proc_handler	= proc_dointvec,
2475884d179dSJan Kara 	},
2476884d179dSJan Kara #endif
2477ab09203eSEric W. Biederman 	{ },
2478884d179dSJan Kara };
2479884d179dSJan Kara 
2480884d179dSJan Kara static ctl_table fs_table[] = {
2481884d179dSJan Kara 	{
2482884d179dSJan Kara 		.procname	= "quota",
2483884d179dSJan Kara 		.mode		= 0555,
2484884d179dSJan Kara 		.child		= fs_dqstats_table,
2485884d179dSJan Kara 	},
2486ab09203eSEric W. Biederman 	{ },
2487884d179dSJan Kara };
2488884d179dSJan Kara 
2489884d179dSJan Kara static ctl_table sys_table[] = {
2490884d179dSJan Kara 	{
2491884d179dSJan Kara 		.procname	= "fs",
2492884d179dSJan Kara 		.mode		= 0555,
2493884d179dSJan Kara 		.child		= fs_table,
2494884d179dSJan Kara 	},
2495ab09203eSEric W. Biederman 	{ },
2496884d179dSJan Kara };
2497884d179dSJan Kara 
2498884d179dSJan Kara static int __init dquot_init(void)
2499884d179dSJan Kara {
2500884d179dSJan Kara 	int i;
2501884d179dSJan Kara 	unsigned long nr_hash, order;
2502884d179dSJan Kara 
2503884d179dSJan Kara 	printk(KERN_NOTICE "VFS: Disk quotas %s\n", __DQUOT_VERSION__);
2504884d179dSJan Kara 
2505884d179dSJan Kara 	register_sysctl_table(sys_table);
2506884d179dSJan Kara 
2507884d179dSJan Kara 	dquot_cachep = kmem_cache_create("dquot",
2508884d179dSJan Kara 			sizeof(struct dquot), sizeof(unsigned long) * 4,
2509884d179dSJan Kara 			(SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT|
2510884d179dSJan Kara 				SLAB_MEM_SPREAD|SLAB_PANIC),
2511884d179dSJan Kara 			NULL);
2512884d179dSJan Kara 
2513884d179dSJan Kara 	order = 0;
2514884d179dSJan Kara 	dquot_hash = (struct hlist_head *)__get_free_pages(GFP_ATOMIC, order);
2515884d179dSJan Kara 	if (!dquot_hash)
2516884d179dSJan Kara 		panic("Cannot create dquot hash table");
2517884d179dSJan Kara 
2518884d179dSJan Kara 	/* Find power-of-two hlist_heads which can fit into allocation */
2519884d179dSJan Kara 	nr_hash = (1UL << order) * PAGE_SIZE / sizeof(struct hlist_head);
2520884d179dSJan Kara 	dq_hash_bits = 0;
2521884d179dSJan Kara 	do {
2522884d179dSJan Kara 		dq_hash_bits++;
2523884d179dSJan Kara 	} while (nr_hash >> dq_hash_bits);
2524884d179dSJan Kara 	dq_hash_bits--;
2525884d179dSJan Kara 
2526884d179dSJan Kara 	nr_hash = 1UL << dq_hash_bits;
2527884d179dSJan Kara 	dq_hash_mask = nr_hash - 1;
2528884d179dSJan Kara 	for (i = 0; i < nr_hash; i++)
2529884d179dSJan Kara 		INIT_HLIST_HEAD(dquot_hash + i);
2530884d179dSJan Kara 
2531884d179dSJan Kara 	printk("Dquot-cache hash table entries: %ld (order %ld, %ld bytes)\n",
2532884d179dSJan Kara 			nr_hash, order, (PAGE_SIZE << order));
2533884d179dSJan Kara 
2534884d179dSJan Kara 	register_shrinker(&dqcache_shrinker);
2535884d179dSJan Kara 
2536884d179dSJan Kara 	return 0;
2537884d179dSJan Kara }
2538884d179dSJan Kara module_init(dquot_init);
2539