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