xref: /openbmc/linux/fs/quota/dquot.c (revision b7d6e724)
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 	/* Has somebody invalidated entry under us? */
999 	WARN_ON_ONCE(hlist_unhashed(&dquot->dq_hash));
1000 out:
1001 	if (empty)
1002 		do_destroy_dquot(empty);
1003 
1004 	return dquot;
1005 }
1006 EXPORT_SYMBOL(dqget);
1007 
1008 static inline struct dquot __rcu **i_dquot(struct inode *inode)
1009 {
1010 	return inode->i_sb->s_op->get_dquots(inode);
1011 }
1012 
1013 static int dqinit_needed(struct inode *inode, int type)
1014 {
1015 	struct dquot __rcu * const *dquots;
1016 	int cnt;
1017 
1018 	if (IS_NOQUOTA(inode))
1019 		return 0;
1020 
1021 	dquots = i_dquot(inode);
1022 	if (type != -1)
1023 		return !dquots[type];
1024 	for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1025 		if (!dquots[cnt])
1026 			return 1;
1027 	return 0;
1028 }
1029 
1030 /* This routine is guarded by s_umount semaphore */
1031 static int add_dquot_ref(struct super_block *sb, int type)
1032 {
1033 	struct inode *inode, *old_inode = NULL;
1034 #ifdef CONFIG_QUOTA_DEBUG
1035 	int reserved = 0;
1036 #endif
1037 	int err = 0;
1038 
1039 	spin_lock(&sb->s_inode_list_lock);
1040 	list_for_each_entry(inode, &sb->s_inodes, i_sb_list) {
1041 		spin_lock(&inode->i_lock);
1042 		if ((inode->i_state & (I_FREEING|I_WILL_FREE|I_NEW)) ||
1043 		    !atomic_read(&inode->i_writecount) ||
1044 		    !dqinit_needed(inode, type)) {
1045 			spin_unlock(&inode->i_lock);
1046 			continue;
1047 		}
1048 		__iget(inode);
1049 		spin_unlock(&inode->i_lock);
1050 		spin_unlock(&sb->s_inode_list_lock);
1051 
1052 #ifdef CONFIG_QUOTA_DEBUG
1053 		if (unlikely(inode_get_rsv_space(inode) > 0))
1054 			reserved = 1;
1055 #endif
1056 		iput(old_inode);
1057 		err = __dquot_initialize(inode, type);
1058 		if (err) {
1059 			iput(inode);
1060 			goto out;
1061 		}
1062 
1063 		/*
1064 		 * We hold a reference to 'inode' so it couldn't have been
1065 		 * removed from s_inodes list while we dropped the
1066 		 * s_inode_list_lock. We cannot iput the inode now as we can be
1067 		 * holding the last reference and we cannot iput it under
1068 		 * s_inode_list_lock. So we keep the reference and iput it
1069 		 * later.
1070 		 */
1071 		old_inode = inode;
1072 		cond_resched();
1073 		spin_lock(&sb->s_inode_list_lock);
1074 	}
1075 	spin_unlock(&sb->s_inode_list_lock);
1076 	iput(old_inode);
1077 out:
1078 #ifdef CONFIG_QUOTA_DEBUG
1079 	if (reserved) {
1080 		quota_error(sb, "Writes happened before quota was turned on "
1081 			"thus quota information is probably inconsistent. "
1082 			"Please run quotacheck(8)");
1083 	}
1084 #endif
1085 	return err;
1086 }
1087 
1088 static void remove_dquot_ref(struct super_block *sb, int type)
1089 {
1090 	struct inode *inode;
1091 #ifdef CONFIG_QUOTA_DEBUG
1092 	int reserved = 0;
1093 #endif
1094 
1095 	spin_lock(&sb->s_inode_list_lock);
1096 	list_for_each_entry(inode, &sb->s_inodes, i_sb_list) {
1097 		/*
1098 		 *  We have to scan also I_NEW inodes because they can already
1099 		 *  have quota pointer initialized. Luckily, we need to touch
1100 		 *  only quota pointers and these have separate locking
1101 		 *  (dq_data_lock).
1102 		 */
1103 		spin_lock(&dq_data_lock);
1104 		if (!IS_NOQUOTA(inode)) {
1105 			struct dquot __rcu **dquots = i_dquot(inode);
1106 			struct dquot *dquot = srcu_dereference_check(
1107 				dquots[type], &dquot_srcu,
1108 				lockdep_is_held(&dq_data_lock));
1109 
1110 #ifdef CONFIG_QUOTA_DEBUG
1111 			if (unlikely(inode_get_rsv_space(inode) > 0))
1112 				reserved = 1;
1113 #endif
1114 			rcu_assign_pointer(dquots[type], NULL);
1115 			if (dquot)
1116 				dqput(dquot);
1117 		}
1118 		spin_unlock(&dq_data_lock);
1119 	}
1120 	spin_unlock(&sb->s_inode_list_lock);
1121 #ifdef CONFIG_QUOTA_DEBUG
1122 	if (reserved) {
1123 		printk(KERN_WARNING "VFS (%s): Writes happened after quota"
1124 			" was disabled thus quota information is probably "
1125 			"inconsistent. Please run quotacheck(8).\n", sb->s_id);
1126 	}
1127 #endif
1128 }
1129 
1130 /* Gather all references from inodes and drop them */
1131 static void drop_dquot_ref(struct super_block *sb, int type)
1132 {
1133 	if (sb->dq_op)
1134 		remove_dquot_ref(sb, type);
1135 }
1136 
1137 static inline
1138 void dquot_free_reserved_space(struct dquot *dquot, qsize_t number)
1139 {
1140 	if (dquot->dq_dqb.dqb_rsvspace >= number)
1141 		dquot->dq_dqb.dqb_rsvspace -= number;
1142 	else {
1143 		WARN_ON_ONCE(1);
1144 		dquot->dq_dqb.dqb_rsvspace = 0;
1145 	}
1146 	if (dquot->dq_dqb.dqb_curspace + dquot->dq_dqb.dqb_rsvspace <=
1147 	    dquot->dq_dqb.dqb_bsoftlimit)
1148 		dquot->dq_dqb.dqb_btime = (time64_t) 0;
1149 	clear_bit(DQ_BLKS_B, &dquot->dq_flags);
1150 }
1151 
1152 static void dquot_decr_inodes(struct dquot *dquot, qsize_t number)
1153 {
1154 	if (sb_dqopt(dquot->dq_sb)->flags & DQUOT_NEGATIVE_USAGE ||
1155 	    dquot->dq_dqb.dqb_curinodes >= number)
1156 		dquot->dq_dqb.dqb_curinodes -= number;
1157 	else
1158 		dquot->dq_dqb.dqb_curinodes = 0;
1159 	if (dquot->dq_dqb.dqb_curinodes <= dquot->dq_dqb.dqb_isoftlimit)
1160 		dquot->dq_dqb.dqb_itime = (time64_t) 0;
1161 	clear_bit(DQ_INODES_B, &dquot->dq_flags);
1162 }
1163 
1164 static void dquot_decr_space(struct dquot *dquot, qsize_t number)
1165 {
1166 	if (sb_dqopt(dquot->dq_sb)->flags & DQUOT_NEGATIVE_USAGE ||
1167 	    dquot->dq_dqb.dqb_curspace >= number)
1168 		dquot->dq_dqb.dqb_curspace -= number;
1169 	else
1170 		dquot->dq_dqb.dqb_curspace = 0;
1171 	if (dquot->dq_dqb.dqb_curspace + dquot->dq_dqb.dqb_rsvspace <=
1172 	    dquot->dq_dqb.dqb_bsoftlimit)
1173 		dquot->dq_dqb.dqb_btime = (time64_t) 0;
1174 	clear_bit(DQ_BLKS_B, &dquot->dq_flags);
1175 }
1176 
1177 struct dquot_warn {
1178 	struct super_block *w_sb;
1179 	struct kqid w_dq_id;
1180 	short w_type;
1181 };
1182 
1183 static int warning_issued(struct dquot *dquot, const int warntype)
1184 {
1185 	int flag = (warntype == QUOTA_NL_BHARDWARN ||
1186 		warntype == QUOTA_NL_BSOFTLONGWARN) ? DQ_BLKS_B :
1187 		((warntype == QUOTA_NL_IHARDWARN ||
1188 		warntype == QUOTA_NL_ISOFTLONGWARN) ? DQ_INODES_B : 0);
1189 
1190 	if (!flag)
1191 		return 0;
1192 	return test_and_set_bit(flag, &dquot->dq_flags);
1193 }
1194 
1195 #ifdef CONFIG_PRINT_QUOTA_WARNING
1196 static int flag_print_warnings = 1;
1197 
1198 static int need_print_warning(struct dquot_warn *warn)
1199 {
1200 	if (!flag_print_warnings)
1201 		return 0;
1202 
1203 	switch (warn->w_dq_id.type) {
1204 		case USRQUOTA:
1205 			return uid_eq(current_fsuid(), warn->w_dq_id.uid);
1206 		case GRPQUOTA:
1207 			return in_group_p(warn->w_dq_id.gid);
1208 		case PRJQUOTA:
1209 			return 1;
1210 	}
1211 	return 0;
1212 }
1213 
1214 /* Print warning to user which exceeded quota */
1215 static void print_warning(struct dquot_warn *warn)
1216 {
1217 	char *msg = NULL;
1218 	struct tty_struct *tty;
1219 	int warntype = warn->w_type;
1220 
1221 	if (warntype == QUOTA_NL_IHARDBELOW ||
1222 	    warntype == QUOTA_NL_ISOFTBELOW ||
1223 	    warntype == QUOTA_NL_BHARDBELOW ||
1224 	    warntype == QUOTA_NL_BSOFTBELOW || !need_print_warning(warn))
1225 		return;
1226 
1227 	tty = get_current_tty();
1228 	if (!tty)
1229 		return;
1230 	tty_write_message(tty, warn->w_sb->s_id);
1231 	if (warntype == QUOTA_NL_ISOFTWARN || warntype == QUOTA_NL_BSOFTWARN)
1232 		tty_write_message(tty, ": warning, ");
1233 	else
1234 		tty_write_message(tty, ": write failed, ");
1235 	tty_write_message(tty, quotatypes[warn->w_dq_id.type]);
1236 	switch (warntype) {
1237 		case QUOTA_NL_IHARDWARN:
1238 			msg = " file limit reached.\r\n";
1239 			break;
1240 		case QUOTA_NL_ISOFTLONGWARN:
1241 			msg = " file quota exceeded too long.\r\n";
1242 			break;
1243 		case QUOTA_NL_ISOFTWARN:
1244 			msg = " file quota exceeded.\r\n";
1245 			break;
1246 		case QUOTA_NL_BHARDWARN:
1247 			msg = " block limit reached.\r\n";
1248 			break;
1249 		case QUOTA_NL_BSOFTLONGWARN:
1250 			msg = " block quota exceeded too long.\r\n";
1251 			break;
1252 		case QUOTA_NL_BSOFTWARN:
1253 			msg = " block quota exceeded.\r\n";
1254 			break;
1255 	}
1256 	tty_write_message(tty, msg);
1257 	tty_kref_put(tty);
1258 }
1259 #endif
1260 
1261 static void prepare_warning(struct dquot_warn *warn, struct dquot *dquot,
1262 			    int warntype)
1263 {
1264 	if (warning_issued(dquot, warntype))
1265 		return;
1266 	warn->w_type = warntype;
1267 	warn->w_sb = dquot->dq_sb;
1268 	warn->w_dq_id = dquot->dq_id;
1269 }
1270 
1271 /*
1272  * Write warnings to the console and send warning messages over netlink.
1273  *
1274  * Note that this function can call into tty and networking code.
1275  */
1276 static void flush_warnings(struct dquot_warn *warn)
1277 {
1278 	int i;
1279 
1280 	for (i = 0; i < MAXQUOTAS; i++) {
1281 		if (warn[i].w_type == QUOTA_NL_NOWARN)
1282 			continue;
1283 #ifdef CONFIG_PRINT_QUOTA_WARNING
1284 		print_warning(&warn[i]);
1285 #endif
1286 		quota_send_warning(warn[i].w_dq_id,
1287 				   warn[i].w_sb->s_dev, warn[i].w_type);
1288 	}
1289 }
1290 
1291 static int ignore_hardlimit(struct dquot *dquot)
1292 {
1293 	struct mem_dqinfo *info = &sb_dqopt(dquot->dq_sb)->info[dquot->dq_id.type];
1294 
1295 	return capable(CAP_SYS_RESOURCE) &&
1296 	       (info->dqi_format->qf_fmt_id != QFMT_VFS_OLD ||
1297 		!(info->dqi_flags & DQF_ROOT_SQUASH));
1298 }
1299 
1300 static int dquot_add_inodes(struct dquot *dquot, qsize_t inodes,
1301 			    struct dquot_warn *warn)
1302 {
1303 	qsize_t newinodes;
1304 	int ret = 0;
1305 
1306 	spin_lock(&dquot->dq_dqb_lock);
1307 	newinodes = dquot->dq_dqb.dqb_curinodes + inodes;
1308 	if (!sb_has_quota_limits_enabled(dquot->dq_sb, dquot->dq_id.type) ||
1309 	    test_bit(DQ_FAKE_B, &dquot->dq_flags))
1310 		goto add;
1311 
1312 	if (dquot->dq_dqb.dqb_ihardlimit &&
1313 	    newinodes > dquot->dq_dqb.dqb_ihardlimit &&
1314             !ignore_hardlimit(dquot)) {
1315 		prepare_warning(warn, dquot, QUOTA_NL_IHARDWARN);
1316 		ret = -EDQUOT;
1317 		goto out;
1318 	}
1319 
1320 	if (dquot->dq_dqb.dqb_isoftlimit &&
1321 	    newinodes > dquot->dq_dqb.dqb_isoftlimit &&
1322 	    dquot->dq_dqb.dqb_itime &&
1323 	    ktime_get_real_seconds() >= dquot->dq_dqb.dqb_itime &&
1324             !ignore_hardlimit(dquot)) {
1325 		prepare_warning(warn, dquot, QUOTA_NL_ISOFTLONGWARN);
1326 		ret = -EDQUOT;
1327 		goto out;
1328 	}
1329 
1330 	if (dquot->dq_dqb.dqb_isoftlimit &&
1331 	    newinodes > dquot->dq_dqb.dqb_isoftlimit &&
1332 	    dquot->dq_dqb.dqb_itime == 0) {
1333 		prepare_warning(warn, dquot, QUOTA_NL_ISOFTWARN);
1334 		dquot->dq_dqb.dqb_itime = ktime_get_real_seconds() +
1335 		    sb_dqopt(dquot->dq_sb)->info[dquot->dq_id.type].dqi_igrace;
1336 	}
1337 add:
1338 	dquot->dq_dqb.dqb_curinodes = newinodes;
1339 
1340 out:
1341 	spin_unlock(&dquot->dq_dqb_lock);
1342 	return ret;
1343 }
1344 
1345 static int dquot_add_space(struct dquot *dquot, qsize_t space,
1346 			   qsize_t rsv_space, unsigned int flags,
1347 			   struct dquot_warn *warn)
1348 {
1349 	qsize_t tspace;
1350 	struct super_block *sb = dquot->dq_sb;
1351 	int ret = 0;
1352 
1353 	spin_lock(&dquot->dq_dqb_lock);
1354 	if (!sb_has_quota_limits_enabled(sb, dquot->dq_id.type) ||
1355 	    test_bit(DQ_FAKE_B, &dquot->dq_flags))
1356 		goto finish;
1357 
1358 	tspace = dquot->dq_dqb.dqb_curspace + dquot->dq_dqb.dqb_rsvspace
1359 		+ space + rsv_space;
1360 
1361 	if (dquot->dq_dqb.dqb_bhardlimit &&
1362 	    tspace > dquot->dq_dqb.dqb_bhardlimit &&
1363             !ignore_hardlimit(dquot)) {
1364 		if (flags & DQUOT_SPACE_WARN)
1365 			prepare_warning(warn, dquot, QUOTA_NL_BHARDWARN);
1366 		ret = -EDQUOT;
1367 		goto finish;
1368 	}
1369 
1370 	if (dquot->dq_dqb.dqb_bsoftlimit &&
1371 	    tspace > dquot->dq_dqb.dqb_bsoftlimit &&
1372 	    dquot->dq_dqb.dqb_btime &&
1373 	    ktime_get_real_seconds() >= dquot->dq_dqb.dqb_btime &&
1374             !ignore_hardlimit(dquot)) {
1375 		if (flags & DQUOT_SPACE_WARN)
1376 			prepare_warning(warn, dquot, QUOTA_NL_BSOFTLONGWARN);
1377 		ret = -EDQUOT;
1378 		goto finish;
1379 	}
1380 
1381 	if (dquot->dq_dqb.dqb_bsoftlimit &&
1382 	    tspace > dquot->dq_dqb.dqb_bsoftlimit &&
1383 	    dquot->dq_dqb.dqb_btime == 0) {
1384 		if (flags & DQUOT_SPACE_WARN) {
1385 			prepare_warning(warn, dquot, QUOTA_NL_BSOFTWARN);
1386 			dquot->dq_dqb.dqb_btime = ktime_get_real_seconds() +
1387 			    sb_dqopt(sb)->info[dquot->dq_id.type].dqi_bgrace;
1388 		} else {
1389 			/*
1390 			 * We don't allow preallocation to exceed softlimit so exceeding will
1391 			 * be always printed
1392 			 */
1393 			ret = -EDQUOT;
1394 			goto finish;
1395 		}
1396 	}
1397 finish:
1398 	/*
1399 	 * We have to be careful and go through warning generation & grace time
1400 	 * setting even if DQUOT_SPACE_NOFAIL is set. That's why we check it
1401 	 * only here...
1402 	 */
1403 	if (flags & DQUOT_SPACE_NOFAIL)
1404 		ret = 0;
1405 	if (!ret) {
1406 		dquot->dq_dqb.dqb_rsvspace += rsv_space;
1407 		dquot->dq_dqb.dqb_curspace += space;
1408 	}
1409 	spin_unlock(&dquot->dq_dqb_lock);
1410 	return ret;
1411 }
1412 
1413 static int info_idq_free(struct dquot *dquot, qsize_t inodes)
1414 {
1415 	qsize_t newinodes;
1416 
1417 	if (test_bit(DQ_FAKE_B, &dquot->dq_flags) ||
1418 	    dquot->dq_dqb.dqb_curinodes <= dquot->dq_dqb.dqb_isoftlimit ||
1419 	    !sb_has_quota_limits_enabled(dquot->dq_sb, dquot->dq_id.type))
1420 		return QUOTA_NL_NOWARN;
1421 
1422 	newinodes = dquot->dq_dqb.dqb_curinodes - inodes;
1423 	if (newinodes <= dquot->dq_dqb.dqb_isoftlimit)
1424 		return QUOTA_NL_ISOFTBELOW;
1425 	if (dquot->dq_dqb.dqb_curinodes >= dquot->dq_dqb.dqb_ihardlimit &&
1426 	    newinodes < dquot->dq_dqb.dqb_ihardlimit)
1427 		return QUOTA_NL_IHARDBELOW;
1428 	return QUOTA_NL_NOWARN;
1429 }
1430 
1431 static int info_bdq_free(struct dquot *dquot, qsize_t space)
1432 {
1433 	qsize_t tspace;
1434 
1435 	tspace = dquot->dq_dqb.dqb_curspace + dquot->dq_dqb.dqb_rsvspace;
1436 
1437 	if (test_bit(DQ_FAKE_B, &dquot->dq_flags) ||
1438 	    tspace <= dquot->dq_dqb.dqb_bsoftlimit)
1439 		return QUOTA_NL_NOWARN;
1440 
1441 	if (tspace - space <= dquot->dq_dqb.dqb_bsoftlimit)
1442 		return QUOTA_NL_BSOFTBELOW;
1443 	if (tspace >= dquot->dq_dqb.dqb_bhardlimit &&
1444 	    tspace - space < dquot->dq_dqb.dqb_bhardlimit)
1445 		return QUOTA_NL_BHARDBELOW;
1446 	return QUOTA_NL_NOWARN;
1447 }
1448 
1449 static int inode_quota_active(const struct inode *inode)
1450 {
1451 	struct super_block *sb = inode->i_sb;
1452 
1453 	if (IS_NOQUOTA(inode))
1454 		return 0;
1455 	return sb_any_quota_loaded(sb) & ~sb_any_quota_suspended(sb);
1456 }
1457 
1458 /*
1459  * Initialize quota pointers in inode
1460  *
1461  * It is better to call this function outside of any transaction as it
1462  * might need a lot of space in journal for dquot structure allocation.
1463  */
1464 static int __dquot_initialize(struct inode *inode, int type)
1465 {
1466 	int cnt, init_needed = 0;
1467 	struct dquot __rcu **dquots;
1468 	struct dquot *got[MAXQUOTAS] = {};
1469 	struct super_block *sb = inode->i_sb;
1470 	qsize_t rsv;
1471 	int ret = 0;
1472 
1473 	if (!inode_quota_active(inode))
1474 		return 0;
1475 
1476 	dquots = i_dquot(inode);
1477 
1478 	/* First get references to structures we might need. */
1479 	for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1480 		struct kqid qid;
1481 		kprojid_t projid;
1482 		int rc;
1483 		struct dquot *dquot;
1484 
1485 		if (type != -1 && cnt != type)
1486 			continue;
1487 		/*
1488 		 * The i_dquot should have been initialized in most cases,
1489 		 * we check it without locking here to avoid unnecessary
1490 		 * dqget()/dqput() calls.
1491 		 */
1492 		if (dquots[cnt])
1493 			continue;
1494 
1495 		if (!sb_has_quota_active(sb, cnt))
1496 			continue;
1497 
1498 		init_needed = 1;
1499 
1500 		switch (cnt) {
1501 		case USRQUOTA:
1502 			qid = make_kqid_uid(inode->i_uid);
1503 			break;
1504 		case GRPQUOTA:
1505 			qid = make_kqid_gid(inode->i_gid);
1506 			break;
1507 		case PRJQUOTA:
1508 			rc = inode->i_sb->dq_op->get_projid(inode, &projid);
1509 			if (rc)
1510 				continue;
1511 			qid = make_kqid_projid(projid);
1512 			break;
1513 		}
1514 		dquot = dqget(sb, qid);
1515 		if (IS_ERR(dquot)) {
1516 			/* We raced with somebody turning quotas off... */
1517 			if (PTR_ERR(dquot) != -ESRCH) {
1518 				ret = PTR_ERR(dquot);
1519 				goto out_put;
1520 			}
1521 			dquot = NULL;
1522 		}
1523 		got[cnt] = dquot;
1524 	}
1525 
1526 	/* All required i_dquot has been initialized */
1527 	if (!init_needed)
1528 		return 0;
1529 
1530 	spin_lock(&dq_data_lock);
1531 	if (IS_NOQUOTA(inode))
1532 		goto out_lock;
1533 	for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1534 		if (type != -1 && cnt != type)
1535 			continue;
1536 		/* Avoid races with quotaoff() */
1537 		if (!sb_has_quota_active(sb, cnt))
1538 			continue;
1539 		/* We could race with quotaon or dqget() could have failed */
1540 		if (!got[cnt])
1541 			continue;
1542 		if (!dquots[cnt]) {
1543 			rcu_assign_pointer(dquots[cnt], got[cnt]);
1544 			got[cnt] = NULL;
1545 			/*
1546 			 * Make quota reservation system happy if someone
1547 			 * did a write before quota was turned on
1548 			 */
1549 			rsv = inode_get_rsv_space(inode);
1550 			if (unlikely(rsv)) {
1551 				struct dquot *dquot = srcu_dereference_check(
1552 					dquots[cnt], &dquot_srcu,
1553 					lockdep_is_held(&dq_data_lock));
1554 
1555 				spin_lock(&inode->i_lock);
1556 				/* Get reservation again under proper lock */
1557 				rsv = __inode_get_rsv_space(inode);
1558 				spin_lock(&dquot->dq_dqb_lock);
1559 				dquot->dq_dqb.dqb_rsvspace += rsv;
1560 				spin_unlock(&dquot->dq_dqb_lock);
1561 				spin_unlock(&inode->i_lock);
1562 			}
1563 		}
1564 	}
1565 out_lock:
1566 	spin_unlock(&dq_data_lock);
1567 out_put:
1568 	/* Drop unused references */
1569 	dqput_all(got);
1570 
1571 	return ret;
1572 }
1573 
1574 int dquot_initialize(struct inode *inode)
1575 {
1576 	return __dquot_initialize(inode, -1);
1577 }
1578 EXPORT_SYMBOL(dquot_initialize);
1579 
1580 bool dquot_initialize_needed(struct inode *inode)
1581 {
1582 	struct dquot __rcu **dquots;
1583 	int i;
1584 
1585 	if (!inode_quota_active(inode))
1586 		return false;
1587 
1588 	dquots = i_dquot(inode);
1589 	for (i = 0; i < MAXQUOTAS; i++)
1590 		if (!dquots[i] && sb_has_quota_active(inode->i_sb, i))
1591 			return true;
1592 	return false;
1593 }
1594 EXPORT_SYMBOL(dquot_initialize_needed);
1595 
1596 /*
1597  * Release all quotas referenced by inode.
1598  *
1599  * This function only be called on inode free or converting
1600  * a file to quota file, no other users for the i_dquot in
1601  * both cases, so we needn't call synchronize_srcu() after
1602  * clearing i_dquot.
1603  */
1604 static void __dquot_drop(struct inode *inode)
1605 {
1606 	int cnt;
1607 	struct dquot __rcu **dquots = i_dquot(inode);
1608 	struct dquot *put[MAXQUOTAS];
1609 
1610 	spin_lock(&dq_data_lock);
1611 	for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1612 		put[cnt] = srcu_dereference_check(dquots[cnt], &dquot_srcu,
1613 					lockdep_is_held(&dq_data_lock));
1614 		rcu_assign_pointer(dquots[cnt], NULL);
1615 	}
1616 	spin_unlock(&dq_data_lock);
1617 	dqput_all(put);
1618 }
1619 
1620 void dquot_drop(struct inode *inode)
1621 {
1622 	struct dquot __rcu * const *dquots;
1623 	int cnt;
1624 
1625 	if (IS_NOQUOTA(inode))
1626 		return;
1627 
1628 	/*
1629 	 * Test before calling to rule out calls from proc and such
1630 	 * where we are not allowed to block. Note that this is
1631 	 * actually reliable test even without the lock - the caller
1632 	 * must assure that nobody can come after the DQUOT_DROP and
1633 	 * add quota pointers back anyway.
1634 	 */
1635 	dquots = i_dquot(inode);
1636 	for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1637 		if (dquots[cnt])
1638 			break;
1639 	}
1640 
1641 	if (cnt < MAXQUOTAS)
1642 		__dquot_drop(inode);
1643 }
1644 EXPORT_SYMBOL(dquot_drop);
1645 
1646 /*
1647  * inode_reserved_space is managed internally by quota, and protected by
1648  * i_lock similar to i_blocks+i_bytes.
1649  */
1650 static qsize_t *inode_reserved_space(struct inode * inode)
1651 {
1652 	/* Filesystem must explicitly define it's own method in order to use
1653 	 * quota reservation interface */
1654 	BUG_ON(!inode->i_sb->dq_op->get_reserved_space);
1655 	return inode->i_sb->dq_op->get_reserved_space(inode);
1656 }
1657 
1658 static qsize_t __inode_get_rsv_space(struct inode *inode)
1659 {
1660 	if (!inode->i_sb->dq_op->get_reserved_space)
1661 		return 0;
1662 	return *inode_reserved_space(inode);
1663 }
1664 
1665 static qsize_t inode_get_rsv_space(struct inode *inode)
1666 {
1667 	qsize_t ret;
1668 
1669 	if (!inode->i_sb->dq_op->get_reserved_space)
1670 		return 0;
1671 	spin_lock(&inode->i_lock);
1672 	ret = __inode_get_rsv_space(inode);
1673 	spin_unlock(&inode->i_lock);
1674 	return ret;
1675 }
1676 
1677 /*
1678  * This functions updates i_blocks+i_bytes fields and quota information
1679  * (together with appropriate checks).
1680  *
1681  * NOTE: We absolutely rely on the fact that caller dirties the inode
1682  * (usually helpers in quotaops.h care about this) and holds a handle for
1683  * the current transaction so that dquot write and inode write go into the
1684  * same transaction.
1685  */
1686 
1687 /*
1688  * This operation can block, but only after everything is updated
1689  */
1690 int __dquot_alloc_space(struct inode *inode, qsize_t number, int flags)
1691 {
1692 	int cnt, ret = 0, index;
1693 	struct dquot_warn warn[MAXQUOTAS];
1694 	int reserve = flags & DQUOT_SPACE_RESERVE;
1695 	struct dquot __rcu **dquots;
1696 	struct dquot *dquot;
1697 
1698 	if (!inode_quota_active(inode)) {
1699 		if (reserve) {
1700 			spin_lock(&inode->i_lock);
1701 			*inode_reserved_space(inode) += number;
1702 			spin_unlock(&inode->i_lock);
1703 		} else {
1704 			inode_add_bytes(inode, number);
1705 		}
1706 		goto out;
1707 	}
1708 
1709 	for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1710 		warn[cnt].w_type = QUOTA_NL_NOWARN;
1711 
1712 	dquots = i_dquot(inode);
1713 	index = srcu_read_lock(&dquot_srcu);
1714 	spin_lock(&inode->i_lock);
1715 	for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1716 		dquot = srcu_dereference(dquots[cnt], &dquot_srcu);
1717 		if (!dquot)
1718 			continue;
1719 		if (reserve) {
1720 			ret = dquot_add_space(dquot, 0, number, flags, &warn[cnt]);
1721 		} else {
1722 			ret = dquot_add_space(dquot, number, 0, flags, &warn[cnt]);
1723 		}
1724 		if (ret) {
1725 			/* Back out changes we already did */
1726 			for (cnt--; cnt >= 0; cnt--) {
1727 				dquot = srcu_dereference(dquots[cnt], &dquot_srcu);
1728 				if (!dquot)
1729 					continue;
1730 				spin_lock(&dquot->dq_dqb_lock);
1731 				if (reserve)
1732 					dquot_free_reserved_space(dquot, number);
1733 				else
1734 					dquot_decr_space(dquot, number);
1735 				spin_unlock(&dquot->dq_dqb_lock);
1736 			}
1737 			spin_unlock(&inode->i_lock);
1738 			goto out_flush_warn;
1739 		}
1740 	}
1741 	if (reserve)
1742 		*inode_reserved_space(inode) += number;
1743 	else
1744 		__inode_add_bytes(inode, number);
1745 	spin_unlock(&inode->i_lock);
1746 
1747 	if (reserve)
1748 		goto out_flush_warn;
1749 	mark_all_dquot_dirty(dquots);
1750 out_flush_warn:
1751 	srcu_read_unlock(&dquot_srcu, index);
1752 	flush_warnings(warn);
1753 out:
1754 	return ret;
1755 }
1756 EXPORT_SYMBOL(__dquot_alloc_space);
1757 
1758 /*
1759  * This operation can block, but only after everything is updated
1760  */
1761 int dquot_alloc_inode(struct inode *inode)
1762 {
1763 	int cnt, ret = 0, index;
1764 	struct dquot_warn warn[MAXQUOTAS];
1765 	struct dquot __rcu * const *dquots;
1766 	struct dquot *dquot;
1767 
1768 	if (!inode_quota_active(inode))
1769 		return 0;
1770 	for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1771 		warn[cnt].w_type = QUOTA_NL_NOWARN;
1772 
1773 	dquots = i_dquot(inode);
1774 	index = srcu_read_lock(&dquot_srcu);
1775 	spin_lock(&inode->i_lock);
1776 	for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1777 		dquot = srcu_dereference(dquots[cnt], &dquot_srcu);
1778 		if (!dquot)
1779 			continue;
1780 		ret = dquot_add_inodes(dquot, 1, &warn[cnt]);
1781 		if (ret) {
1782 			for (cnt--; cnt >= 0; cnt--) {
1783 				dquot = srcu_dereference(dquots[cnt], &dquot_srcu);
1784 				if (!dquot)
1785 					continue;
1786 				/* Back out changes we already did */
1787 				spin_lock(&dquot->dq_dqb_lock);
1788 				dquot_decr_inodes(dquot, 1);
1789 				spin_unlock(&dquot->dq_dqb_lock);
1790 			}
1791 			goto warn_put_all;
1792 		}
1793 	}
1794 
1795 warn_put_all:
1796 	spin_unlock(&inode->i_lock);
1797 	if (ret == 0)
1798 		mark_all_dquot_dirty(dquots);
1799 	srcu_read_unlock(&dquot_srcu, index);
1800 	flush_warnings(warn);
1801 	return ret;
1802 }
1803 EXPORT_SYMBOL(dquot_alloc_inode);
1804 
1805 /*
1806  * Convert in-memory reserved quotas to real consumed quotas
1807  */
1808 int dquot_claim_space_nodirty(struct inode *inode, qsize_t number)
1809 {
1810 	struct dquot __rcu **dquots;
1811 	struct dquot *dquot;
1812 	int cnt, index;
1813 
1814 	if (!inode_quota_active(inode)) {
1815 		spin_lock(&inode->i_lock);
1816 		*inode_reserved_space(inode) -= number;
1817 		__inode_add_bytes(inode, number);
1818 		spin_unlock(&inode->i_lock);
1819 		return 0;
1820 	}
1821 
1822 	dquots = i_dquot(inode);
1823 	index = srcu_read_lock(&dquot_srcu);
1824 	spin_lock(&inode->i_lock);
1825 	/* Claim reserved quotas to allocated quotas */
1826 	for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1827 		dquot = srcu_dereference(dquots[cnt], &dquot_srcu);
1828 		if (dquot) {
1829 			spin_lock(&dquot->dq_dqb_lock);
1830 			if (WARN_ON_ONCE(dquot->dq_dqb.dqb_rsvspace < number))
1831 				number = dquot->dq_dqb.dqb_rsvspace;
1832 			dquot->dq_dqb.dqb_curspace += number;
1833 			dquot->dq_dqb.dqb_rsvspace -= number;
1834 			spin_unlock(&dquot->dq_dqb_lock);
1835 		}
1836 	}
1837 	/* Update inode bytes */
1838 	*inode_reserved_space(inode) -= number;
1839 	__inode_add_bytes(inode, number);
1840 	spin_unlock(&inode->i_lock);
1841 	mark_all_dquot_dirty(dquots);
1842 	srcu_read_unlock(&dquot_srcu, index);
1843 	return 0;
1844 }
1845 EXPORT_SYMBOL(dquot_claim_space_nodirty);
1846 
1847 /*
1848  * Convert allocated space back to in-memory reserved quotas
1849  */
1850 void dquot_reclaim_space_nodirty(struct inode *inode, qsize_t number)
1851 {
1852 	struct dquot __rcu **dquots;
1853 	struct dquot *dquot;
1854 	int cnt, index;
1855 
1856 	if (!inode_quota_active(inode)) {
1857 		spin_lock(&inode->i_lock);
1858 		*inode_reserved_space(inode) += number;
1859 		__inode_sub_bytes(inode, number);
1860 		spin_unlock(&inode->i_lock);
1861 		return;
1862 	}
1863 
1864 	dquots = i_dquot(inode);
1865 	index = srcu_read_lock(&dquot_srcu);
1866 	spin_lock(&inode->i_lock);
1867 	/* Claim reserved quotas to allocated quotas */
1868 	for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1869 		dquot = srcu_dereference(dquots[cnt], &dquot_srcu);
1870 		if (dquot) {
1871 			spin_lock(&dquot->dq_dqb_lock);
1872 			if (WARN_ON_ONCE(dquot->dq_dqb.dqb_curspace < number))
1873 				number = dquot->dq_dqb.dqb_curspace;
1874 			dquot->dq_dqb.dqb_rsvspace += number;
1875 			dquot->dq_dqb.dqb_curspace -= number;
1876 			spin_unlock(&dquot->dq_dqb_lock);
1877 		}
1878 	}
1879 	/* Update inode bytes */
1880 	*inode_reserved_space(inode) += number;
1881 	__inode_sub_bytes(inode, number);
1882 	spin_unlock(&inode->i_lock);
1883 	mark_all_dquot_dirty(dquots);
1884 	srcu_read_unlock(&dquot_srcu, index);
1885 	return;
1886 }
1887 EXPORT_SYMBOL(dquot_reclaim_space_nodirty);
1888 
1889 /*
1890  * This operation can block, but only after everything is updated
1891  */
1892 void __dquot_free_space(struct inode *inode, qsize_t number, int flags)
1893 {
1894 	unsigned int cnt;
1895 	struct dquot_warn warn[MAXQUOTAS];
1896 	struct dquot __rcu **dquots;
1897 	struct dquot *dquot;
1898 	int reserve = flags & DQUOT_SPACE_RESERVE, index;
1899 
1900 	if (!inode_quota_active(inode)) {
1901 		if (reserve) {
1902 			spin_lock(&inode->i_lock);
1903 			*inode_reserved_space(inode) -= number;
1904 			spin_unlock(&inode->i_lock);
1905 		} else {
1906 			inode_sub_bytes(inode, number);
1907 		}
1908 		return;
1909 	}
1910 
1911 	dquots = i_dquot(inode);
1912 	index = srcu_read_lock(&dquot_srcu);
1913 	spin_lock(&inode->i_lock);
1914 	for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1915 		int wtype;
1916 
1917 		warn[cnt].w_type = QUOTA_NL_NOWARN;
1918 		dquot = srcu_dereference(dquots[cnt], &dquot_srcu);
1919 		if (!dquot)
1920 			continue;
1921 		spin_lock(&dquot->dq_dqb_lock);
1922 		wtype = info_bdq_free(dquot, number);
1923 		if (wtype != QUOTA_NL_NOWARN)
1924 			prepare_warning(&warn[cnt], dquot, wtype);
1925 		if (reserve)
1926 			dquot_free_reserved_space(dquot, number);
1927 		else
1928 			dquot_decr_space(dquot, number);
1929 		spin_unlock(&dquot->dq_dqb_lock);
1930 	}
1931 	if (reserve)
1932 		*inode_reserved_space(inode) -= number;
1933 	else
1934 		__inode_sub_bytes(inode, number);
1935 	spin_unlock(&inode->i_lock);
1936 
1937 	if (reserve)
1938 		goto out_unlock;
1939 	mark_all_dquot_dirty(dquots);
1940 out_unlock:
1941 	srcu_read_unlock(&dquot_srcu, index);
1942 	flush_warnings(warn);
1943 }
1944 EXPORT_SYMBOL(__dquot_free_space);
1945 
1946 /*
1947  * This operation can block, but only after everything is updated
1948  */
1949 void dquot_free_inode(struct inode *inode)
1950 {
1951 	unsigned int cnt;
1952 	struct dquot_warn warn[MAXQUOTAS];
1953 	struct dquot __rcu * const *dquots;
1954 	struct dquot *dquot;
1955 	int index;
1956 
1957 	if (!inode_quota_active(inode))
1958 		return;
1959 
1960 	dquots = i_dquot(inode);
1961 	index = srcu_read_lock(&dquot_srcu);
1962 	spin_lock(&inode->i_lock);
1963 	for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1964 		int wtype;
1965 		warn[cnt].w_type = QUOTA_NL_NOWARN;
1966 		dquot = srcu_dereference(dquots[cnt], &dquot_srcu);
1967 		if (!dquot)
1968 			continue;
1969 		spin_lock(&dquot->dq_dqb_lock);
1970 		wtype = info_idq_free(dquot, 1);
1971 		if (wtype != QUOTA_NL_NOWARN)
1972 			prepare_warning(&warn[cnt], dquot, wtype);
1973 		dquot_decr_inodes(dquot, 1);
1974 		spin_unlock(&dquot->dq_dqb_lock);
1975 	}
1976 	spin_unlock(&inode->i_lock);
1977 	mark_all_dquot_dirty(dquots);
1978 	srcu_read_unlock(&dquot_srcu, index);
1979 	flush_warnings(warn);
1980 }
1981 EXPORT_SYMBOL(dquot_free_inode);
1982 
1983 /*
1984  * Transfer the number of inode and blocks from one diskquota to an other.
1985  * On success, dquot references in transfer_to are consumed and references
1986  * to original dquots that need to be released are placed there. On failure,
1987  * references are kept untouched.
1988  *
1989  * This operation can block, but only after everything is updated
1990  * A transaction must be started when entering this function.
1991  *
1992  * We are holding reference on transfer_from & transfer_to, no need to
1993  * protect them by srcu_read_lock().
1994  */
1995 int __dquot_transfer(struct inode *inode, struct dquot **transfer_to)
1996 {
1997 	qsize_t cur_space;
1998 	qsize_t rsv_space = 0;
1999 	qsize_t inode_usage = 1;
2000 	struct dquot __rcu **dquots;
2001 	struct dquot *transfer_from[MAXQUOTAS] = {};
2002 	int cnt, index, ret = 0;
2003 	char is_valid[MAXQUOTAS] = {};
2004 	struct dquot_warn warn_to[MAXQUOTAS];
2005 	struct dquot_warn warn_from_inodes[MAXQUOTAS];
2006 	struct dquot_warn warn_from_space[MAXQUOTAS];
2007 
2008 	if (IS_NOQUOTA(inode))
2009 		return 0;
2010 
2011 	if (inode->i_sb->dq_op->get_inode_usage) {
2012 		ret = inode->i_sb->dq_op->get_inode_usage(inode, &inode_usage);
2013 		if (ret)
2014 			return ret;
2015 	}
2016 
2017 	/* Initialize the arrays */
2018 	for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
2019 		warn_to[cnt].w_type = QUOTA_NL_NOWARN;
2020 		warn_from_inodes[cnt].w_type = QUOTA_NL_NOWARN;
2021 		warn_from_space[cnt].w_type = QUOTA_NL_NOWARN;
2022 	}
2023 
2024 	spin_lock(&dq_data_lock);
2025 	spin_lock(&inode->i_lock);
2026 	if (IS_NOQUOTA(inode)) {	/* File without quota accounting? */
2027 		spin_unlock(&inode->i_lock);
2028 		spin_unlock(&dq_data_lock);
2029 		return 0;
2030 	}
2031 	cur_space = __inode_get_bytes(inode);
2032 	rsv_space = __inode_get_rsv_space(inode);
2033 	dquots = i_dquot(inode);
2034 	/*
2035 	 * Build the transfer_from list, check limits, and update usage in
2036 	 * the target structures.
2037 	 */
2038 	for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
2039 		/*
2040 		 * Skip changes for same uid or gid or for turned off quota-type.
2041 		 */
2042 		if (!transfer_to[cnt])
2043 			continue;
2044 		/* Avoid races with quotaoff() */
2045 		if (!sb_has_quota_active(inode->i_sb, cnt))
2046 			continue;
2047 		is_valid[cnt] = 1;
2048 		transfer_from[cnt] = srcu_dereference_check(dquots[cnt],
2049 				&dquot_srcu, lockdep_is_held(&dq_data_lock));
2050 		ret = dquot_add_inodes(transfer_to[cnt], inode_usage,
2051 				       &warn_to[cnt]);
2052 		if (ret)
2053 			goto over_quota;
2054 		ret = dquot_add_space(transfer_to[cnt], cur_space, rsv_space,
2055 				      DQUOT_SPACE_WARN, &warn_to[cnt]);
2056 		if (ret) {
2057 			spin_lock(&transfer_to[cnt]->dq_dqb_lock);
2058 			dquot_decr_inodes(transfer_to[cnt], inode_usage);
2059 			spin_unlock(&transfer_to[cnt]->dq_dqb_lock);
2060 			goto over_quota;
2061 		}
2062 	}
2063 
2064 	/* Decrease usage for source structures and update quota pointers */
2065 	for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
2066 		if (!is_valid[cnt])
2067 			continue;
2068 		/* Due to IO error we might not have transfer_from[] structure */
2069 		if (transfer_from[cnt]) {
2070 			int wtype;
2071 
2072 			spin_lock(&transfer_from[cnt]->dq_dqb_lock);
2073 			wtype = info_idq_free(transfer_from[cnt], inode_usage);
2074 			if (wtype != QUOTA_NL_NOWARN)
2075 				prepare_warning(&warn_from_inodes[cnt],
2076 						transfer_from[cnt], wtype);
2077 			wtype = info_bdq_free(transfer_from[cnt],
2078 					      cur_space + rsv_space);
2079 			if (wtype != QUOTA_NL_NOWARN)
2080 				prepare_warning(&warn_from_space[cnt],
2081 						transfer_from[cnt], wtype);
2082 			dquot_decr_inodes(transfer_from[cnt], inode_usage);
2083 			dquot_decr_space(transfer_from[cnt], cur_space);
2084 			dquot_free_reserved_space(transfer_from[cnt],
2085 						  rsv_space);
2086 			spin_unlock(&transfer_from[cnt]->dq_dqb_lock);
2087 		}
2088 		rcu_assign_pointer(dquots[cnt], transfer_to[cnt]);
2089 	}
2090 	spin_unlock(&inode->i_lock);
2091 	spin_unlock(&dq_data_lock);
2092 
2093 	/*
2094 	 * These arrays are local and we hold dquot references so we don't need
2095 	 * the srcu protection but still take dquot_srcu to avoid warning in
2096 	 * mark_all_dquot_dirty().
2097 	 */
2098 	index = srcu_read_lock(&dquot_srcu);
2099 	mark_all_dquot_dirty((struct dquot __rcu **)transfer_from);
2100 	mark_all_dquot_dirty((struct dquot __rcu **)transfer_to);
2101 	srcu_read_unlock(&dquot_srcu, index);
2102 
2103 	flush_warnings(warn_to);
2104 	flush_warnings(warn_from_inodes);
2105 	flush_warnings(warn_from_space);
2106 	/* Pass back references to put */
2107 	for (cnt = 0; cnt < MAXQUOTAS; cnt++)
2108 		if (is_valid[cnt])
2109 			transfer_to[cnt] = transfer_from[cnt];
2110 	return 0;
2111 over_quota:
2112 	/* Back out changes we already did */
2113 	for (cnt--; cnt >= 0; cnt--) {
2114 		if (!is_valid[cnt])
2115 			continue;
2116 		spin_lock(&transfer_to[cnt]->dq_dqb_lock);
2117 		dquot_decr_inodes(transfer_to[cnt], inode_usage);
2118 		dquot_decr_space(transfer_to[cnt], cur_space);
2119 		dquot_free_reserved_space(transfer_to[cnt], rsv_space);
2120 		spin_unlock(&transfer_to[cnt]->dq_dqb_lock);
2121 	}
2122 	spin_unlock(&inode->i_lock);
2123 	spin_unlock(&dq_data_lock);
2124 	flush_warnings(warn_to);
2125 	return ret;
2126 }
2127 EXPORT_SYMBOL(__dquot_transfer);
2128 
2129 /* Wrapper for transferring ownership of an inode for uid/gid only
2130  * Called from FSXXX_setattr()
2131  */
2132 int dquot_transfer(struct mnt_idmap *idmap, struct inode *inode,
2133 		   struct iattr *iattr)
2134 {
2135 	struct dquot *transfer_to[MAXQUOTAS] = {};
2136 	struct dquot *dquot;
2137 	struct super_block *sb = inode->i_sb;
2138 	int ret;
2139 
2140 	if (!inode_quota_active(inode))
2141 		return 0;
2142 
2143 	if (i_uid_needs_update(idmap, iattr, inode)) {
2144 		kuid_t kuid = from_vfsuid(idmap, i_user_ns(inode),
2145 					  iattr->ia_vfsuid);
2146 
2147 		dquot = dqget(sb, make_kqid_uid(kuid));
2148 		if (IS_ERR(dquot)) {
2149 			if (PTR_ERR(dquot) != -ESRCH) {
2150 				ret = PTR_ERR(dquot);
2151 				goto out_put;
2152 			}
2153 			dquot = NULL;
2154 		}
2155 		transfer_to[USRQUOTA] = dquot;
2156 	}
2157 	if (i_gid_needs_update(idmap, iattr, inode)) {
2158 		kgid_t kgid = from_vfsgid(idmap, i_user_ns(inode),
2159 					  iattr->ia_vfsgid);
2160 
2161 		dquot = dqget(sb, make_kqid_gid(kgid));
2162 		if (IS_ERR(dquot)) {
2163 			if (PTR_ERR(dquot) != -ESRCH) {
2164 				ret = PTR_ERR(dquot);
2165 				goto out_put;
2166 			}
2167 			dquot = NULL;
2168 		}
2169 		transfer_to[GRPQUOTA] = dquot;
2170 	}
2171 	ret = __dquot_transfer(inode, transfer_to);
2172 out_put:
2173 	dqput_all(transfer_to);
2174 	return ret;
2175 }
2176 EXPORT_SYMBOL(dquot_transfer);
2177 
2178 /*
2179  * Write info of quota file to disk
2180  */
2181 int dquot_commit_info(struct super_block *sb, int type)
2182 {
2183 	struct quota_info *dqopt = sb_dqopt(sb);
2184 
2185 	return dqopt->ops[type]->write_file_info(sb, type);
2186 }
2187 EXPORT_SYMBOL(dquot_commit_info);
2188 
2189 int dquot_get_next_id(struct super_block *sb, struct kqid *qid)
2190 {
2191 	struct quota_info *dqopt = sb_dqopt(sb);
2192 
2193 	if (!sb_has_quota_active(sb, qid->type))
2194 		return -ESRCH;
2195 	if (!dqopt->ops[qid->type]->get_next_id)
2196 		return -ENOSYS;
2197 	return dqopt->ops[qid->type]->get_next_id(sb, qid);
2198 }
2199 EXPORT_SYMBOL(dquot_get_next_id);
2200 
2201 /*
2202  * Definitions of diskquota operations.
2203  */
2204 const struct dquot_operations dquot_operations = {
2205 	.write_dquot	= dquot_commit,
2206 	.acquire_dquot	= dquot_acquire,
2207 	.release_dquot	= dquot_release,
2208 	.mark_dirty	= dquot_mark_dquot_dirty,
2209 	.write_info	= dquot_commit_info,
2210 	.alloc_dquot	= dquot_alloc,
2211 	.destroy_dquot	= dquot_destroy,
2212 	.get_next_id	= dquot_get_next_id,
2213 };
2214 EXPORT_SYMBOL(dquot_operations);
2215 
2216 /*
2217  * Generic helper for ->open on filesystems supporting disk quotas.
2218  */
2219 int dquot_file_open(struct inode *inode, struct file *file)
2220 {
2221 	int error;
2222 
2223 	error = generic_file_open(inode, file);
2224 	if (!error && (file->f_mode & FMODE_WRITE))
2225 		error = dquot_initialize(inode);
2226 	return error;
2227 }
2228 EXPORT_SYMBOL(dquot_file_open);
2229 
2230 static void vfs_cleanup_quota_inode(struct super_block *sb, int type)
2231 {
2232 	struct quota_info *dqopt = sb_dqopt(sb);
2233 	struct inode *inode = dqopt->files[type];
2234 
2235 	if (!inode)
2236 		return;
2237 	if (!(dqopt->flags & DQUOT_QUOTA_SYS_FILE)) {
2238 		inode_lock(inode);
2239 		inode->i_flags &= ~S_NOQUOTA;
2240 		inode_unlock(inode);
2241 	}
2242 	dqopt->files[type] = NULL;
2243 	iput(inode);
2244 }
2245 
2246 /*
2247  * Turn quota off on a device. type == -1 ==> quotaoff for all types (umount)
2248  */
2249 int dquot_disable(struct super_block *sb, int type, unsigned int flags)
2250 {
2251 	int cnt;
2252 	struct quota_info *dqopt = sb_dqopt(sb);
2253 
2254 	/* s_umount should be held in exclusive mode */
2255 	if (WARN_ON_ONCE(down_read_trylock(&sb->s_umount)))
2256 		up_read(&sb->s_umount);
2257 
2258 	/* Cannot turn off usage accounting without turning off limits, or
2259 	 * suspend quotas and simultaneously turn quotas off. */
2260 	if ((flags & DQUOT_USAGE_ENABLED && !(flags & DQUOT_LIMITS_ENABLED))
2261 	    || (flags & DQUOT_SUSPENDED && flags & (DQUOT_LIMITS_ENABLED |
2262 	    DQUOT_USAGE_ENABLED)))
2263 		return -EINVAL;
2264 
2265 	/*
2266 	 * Skip everything if there's nothing to do. We have to do this because
2267 	 * sometimes we are called when fill_super() failed and calling
2268 	 * sync_fs() in such cases does no good.
2269 	 */
2270 	if (!sb_any_quota_loaded(sb))
2271 		return 0;
2272 
2273 	for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
2274 		if (type != -1 && cnt != type)
2275 			continue;
2276 		if (!sb_has_quota_loaded(sb, cnt))
2277 			continue;
2278 
2279 		if (flags & DQUOT_SUSPENDED) {
2280 			spin_lock(&dq_state_lock);
2281 			dqopt->flags |=
2282 				dquot_state_flag(DQUOT_SUSPENDED, cnt);
2283 			spin_unlock(&dq_state_lock);
2284 		} else {
2285 			spin_lock(&dq_state_lock);
2286 			dqopt->flags &= ~dquot_state_flag(flags, cnt);
2287 			/* Turning off suspended quotas? */
2288 			if (!sb_has_quota_loaded(sb, cnt) &&
2289 			    sb_has_quota_suspended(sb, cnt)) {
2290 				dqopt->flags &=	~dquot_state_flag(
2291 							DQUOT_SUSPENDED, cnt);
2292 				spin_unlock(&dq_state_lock);
2293 				vfs_cleanup_quota_inode(sb, cnt);
2294 				continue;
2295 			}
2296 			spin_unlock(&dq_state_lock);
2297 		}
2298 
2299 		/* We still have to keep quota loaded? */
2300 		if (sb_has_quota_loaded(sb, cnt) && !(flags & DQUOT_SUSPENDED))
2301 			continue;
2302 
2303 		/* Note: these are blocking operations */
2304 		drop_dquot_ref(sb, cnt);
2305 		invalidate_dquots(sb, cnt);
2306 		/*
2307 		 * Now all dquots should be invalidated, all writes done so we
2308 		 * should be only users of the info. No locks needed.
2309 		 */
2310 		if (info_dirty(&dqopt->info[cnt]))
2311 			sb->dq_op->write_info(sb, cnt);
2312 		if (dqopt->ops[cnt]->free_file_info)
2313 			dqopt->ops[cnt]->free_file_info(sb, cnt);
2314 		put_quota_format(dqopt->info[cnt].dqi_format);
2315 		dqopt->info[cnt].dqi_flags = 0;
2316 		dqopt->info[cnt].dqi_igrace = 0;
2317 		dqopt->info[cnt].dqi_bgrace = 0;
2318 		dqopt->ops[cnt] = NULL;
2319 	}
2320 
2321 	/* Skip syncing and setting flags if quota files are hidden */
2322 	if (dqopt->flags & DQUOT_QUOTA_SYS_FILE)
2323 		goto put_inodes;
2324 
2325 	/* Sync the superblock so that buffers with quota data are written to
2326 	 * disk (and so userspace sees correct data afterwards). */
2327 	if (sb->s_op->sync_fs)
2328 		sb->s_op->sync_fs(sb, 1);
2329 	sync_blockdev(sb->s_bdev);
2330 	/* Now the quota files are just ordinary files and we can set the
2331 	 * inode flags back. Moreover we discard the pagecache so that
2332 	 * userspace sees the writes we did bypassing the pagecache. We
2333 	 * must also discard the blockdev buffers so that we see the
2334 	 * changes done by userspace on the next quotaon() */
2335 	for (cnt = 0; cnt < MAXQUOTAS; cnt++)
2336 		if (!sb_has_quota_loaded(sb, cnt) && dqopt->files[cnt]) {
2337 			inode_lock(dqopt->files[cnt]);
2338 			truncate_inode_pages(&dqopt->files[cnt]->i_data, 0);
2339 			inode_unlock(dqopt->files[cnt]);
2340 		}
2341 	if (sb->s_bdev)
2342 		invalidate_bdev(sb->s_bdev);
2343 put_inodes:
2344 	/* We are done when suspending quotas */
2345 	if (flags & DQUOT_SUSPENDED)
2346 		return 0;
2347 
2348 	for (cnt = 0; cnt < MAXQUOTAS; cnt++)
2349 		if (!sb_has_quota_loaded(sb, cnt))
2350 			vfs_cleanup_quota_inode(sb, cnt);
2351 	return 0;
2352 }
2353 EXPORT_SYMBOL(dquot_disable);
2354 
2355 int dquot_quota_off(struct super_block *sb, int type)
2356 {
2357 	return dquot_disable(sb, type,
2358 			     DQUOT_USAGE_ENABLED | DQUOT_LIMITS_ENABLED);
2359 }
2360 EXPORT_SYMBOL(dquot_quota_off);
2361 
2362 /*
2363  *	Turn quotas on on a device
2364  */
2365 
2366 static int vfs_setup_quota_inode(struct inode *inode, int type)
2367 {
2368 	struct super_block *sb = inode->i_sb;
2369 	struct quota_info *dqopt = sb_dqopt(sb);
2370 
2371 	if (is_bad_inode(inode))
2372 		return -EUCLEAN;
2373 	if (!S_ISREG(inode->i_mode))
2374 		return -EACCES;
2375 	if (IS_RDONLY(inode))
2376 		return -EROFS;
2377 	if (sb_has_quota_loaded(sb, type))
2378 		return -EBUSY;
2379 
2380 	/*
2381 	 * Quota files should never be encrypted.  They should be thought of as
2382 	 * filesystem metadata, not user data.  New-style internal quota files
2383 	 * cannot be encrypted by users anyway, but old-style external quota
2384 	 * files could potentially be incorrectly created in an encrypted
2385 	 * directory, hence this explicit check.  Some reasons why encrypted
2386 	 * quota files don't work include: (1) some filesystems that support
2387 	 * encryption don't handle it in their quota_read and quota_write, and
2388 	 * (2) cleaning up encrypted quota files at unmount would need special
2389 	 * consideration, as quota files are cleaned up later than user files.
2390 	 */
2391 	if (IS_ENCRYPTED(inode))
2392 		return -EINVAL;
2393 
2394 	dqopt->files[type] = igrab(inode);
2395 	if (!dqopt->files[type])
2396 		return -EIO;
2397 	if (!(dqopt->flags & DQUOT_QUOTA_SYS_FILE)) {
2398 		/* We don't want quota and atime on quota files (deadlocks
2399 		 * possible) Also nobody should write to the file - we use
2400 		 * special IO operations which ignore the immutable bit. */
2401 		inode_lock(inode);
2402 		inode->i_flags |= S_NOQUOTA;
2403 		inode_unlock(inode);
2404 		/*
2405 		 * When S_NOQUOTA is set, remove dquot references as no more
2406 		 * references can be added
2407 		 */
2408 		__dquot_drop(inode);
2409 	}
2410 	return 0;
2411 }
2412 
2413 int dquot_load_quota_sb(struct super_block *sb, int type, int format_id,
2414 	unsigned int flags)
2415 {
2416 	struct quota_format_type *fmt = find_quota_format(format_id);
2417 	struct quota_info *dqopt = sb_dqopt(sb);
2418 	int error;
2419 
2420 	lockdep_assert_held_write(&sb->s_umount);
2421 
2422 	/* Just unsuspend quotas? */
2423 	BUG_ON(flags & DQUOT_SUSPENDED);
2424 
2425 	if (!fmt)
2426 		return -ESRCH;
2427 	if (!sb->dq_op || !sb->s_qcop ||
2428 	    (type == PRJQUOTA && sb->dq_op->get_projid == NULL)) {
2429 		error = -EINVAL;
2430 		goto out_fmt;
2431 	}
2432 	/* Filesystems outside of init_user_ns not yet supported */
2433 	if (sb->s_user_ns != &init_user_ns) {
2434 		error = -EINVAL;
2435 		goto out_fmt;
2436 	}
2437 	/* Usage always has to be set... */
2438 	if (!(flags & DQUOT_USAGE_ENABLED)) {
2439 		error = -EINVAL;
2440 		goto out_fmt;
2441 	}
2442 	if (sb_has_quota_loaded(sb, type)) {
2443 		error = -EBUSY;
2444 		goto out_fmt;
2445 	}
2446 
2447 	if (!(dqopt->flags & DQUOT_QUOTA_SYS_FILE)) {
2448 		/* As we bypass the pagecache we must now flush all the
2449 		 * dirty data and invalidate caches so that kernel sees
2450 		 * changes from userspace. It is not enough to just flush
2451 		 * the quota file since if blocksize < pagesize, invalidation
2452 		 * of the cache could fail because of other unrelated dirty
2453 		 * data */
2454 		sync_filesystem(sb);
2455 		invalidate_bdev(sb->s_bdev);
2456 	}
2457 
2458 	error = -EINVAL;
2459 	if (!fmt->qf_ops->check_quota_file(sb, type))
2460 		goto out_fmt;
2461 
2462 	dqopt->ops[type] = fmt->qf_ops;
2463 	dqopt->info[type].dqi_format = fmt;
2464 	dqopt->info[type].dqi_fmt_id = format_id;
2465 	INIT_LIST_HEAD(&dqopt->info[type].dqi_dirty_list);
2466 	error = dqopt->ops[type]->read_file_info(sb, type);
2467 	if (error < 0)
2468 		goto out_fmt;
2469 	if (dqopt->flags & DQUOT_QUOTA_SYS_FILE) {
2470 		spin_lock(&dq_data_lock);
2471 		dqopt->info[type].dqi_flags |= DQF_SYS_FILE;
2472 		spin_unlock(&dq_data_lock);
2473 	}
2474 	spin_lock(&dq_state_lock);
2475 	dqopt->flags |= dquot_state_flag(flags, type);
2476 	spin_unlock(&dq_state_lock);
2477 
2478 	error = add_dquot_ref(sb, type);
2479 	if (error)
2480 		dquot_disable(sb, type,
2481 			      DQUOT_USAGE_ENABLED | DQUOT_LIMITS_ENABLED);
2482 
2483 	return error;
2484 out_fmt:
2485 	put_quota_format(fmt);
2486 
2487 	return error;
2488 }
2489 EXPORT_SYMBOL(dquot_load_quota_sb);
2490 
2491 /*
2492  * More powerful function for turning on quotas on given quota inode allowing
2493  * setting of individual quota flags
2494  */
2495 int dquot_load_quota_inode(struct inode *inode, int type, int format_id,
2496 	unsigned int flags)
2497 {
2498 	int err;
2499 
2500 	err = vfs_setup_quota_inode(inode, type);
2501 	if (err < 0)
2502 		return err;
2503 	err = dquot_load_quota_sb(inode->i_sb, type, format_id, flags);
2504 	if (err < 0)
2505 		vfs_cleanup_quota_inode(inode->i_sb, type);
2506 	return err;
2507 }
2508 EXPORT_SYMBOL(dquot_load_quota_inode);
2509 
2510 /* Reenable quotas on remount RW */
2511 int dquot_resume(struct super_block *sb, int type)
2512 {
2513 	struct quota_info *dqopt = sb_dqopt(sb);
2514 	int ret = 0, cnt;
2515 	unsigned int flags;
2516 
2517 	/* s_umount should be held in exclusive mode */
2518 	if (WARN_ON_ONCE(down_read_trylock(&sb->s_umount)))
2519 		up_read(&sb->s_umount);
2520 
2521 	for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
2522 		if (type != -1 && cnt != type)
2523 			continue;
2524 		if (!sb_has_quota_suspended(sb, cnt))
2525 			continue;
2526 
2527 		spin_lock(&dq_state_lock);
2528 		flags = dqopt->flags & dquot_state_flag(DQUOT_USAGE_ENABLED |
2529 							DQUOT_LIMITS_ENABLED,
2530 							cnt);
2531 		dqopt->flags &= ~dquot_state_flag(DQUOT_STATE_FLAGS, cnt);
2532 		spin_unlock(&dq_state_lock);
2533 
2534 		flags = dquot_generic_flag(flags, cnt);
2535 		ret = dquot_load_quota_sb(sb, cnt, dqopt->info[cnt].dqi_fmt_id,
2536 					  flags);
2537 		if (ret < 0)
2538 			vfs_cleanup_quota_inode(sb, cnt);
2539 	}
2540 
2541 	return ret;
2542 }
2543 EXPORT_SYMBOL(dquot_resume);
2544 
2545 int dquot_quota_on(struct super_block *sb, int type, int format_id,
2546 		   const struct path *path)
2547 {
2548 	int error = security_quota_on(path->dentry);
2549 	if (error)
2550 		return error;
2551 	/* Quota file not on the same filesystem? */
2552 	if (path->dentry->d_sb != sb)
2553 		error = -EXDEV;
2554 	else
2555 		error = dquot_load_quota_inode(d_inode(path->dentry), type,
2556 					     format_id, DQUOT_USAGE_ENABLED |
2557 					     DQUOT_LIMITS_ENABLED);
2558 	return error;
2559 }
2560 EXPORT_SYMBOL(dquot_quota_on);
2561 
2562 /*
2563  * This function is used when filesystem needs to initialize quotas
2564  * during mount time.
2565  */
2566 int dquot_quota_on_mount(struct super_block *sb, char *qf_name,
2567 		int format_id, int type)
2568 {
2569 	struct dentry *dentry;
2570 	int error;
2571 
2572 	dentry = lookup_positive_unlocked(qf_name, sb->s_root, strlen(qf_name));
2573 	if (IS_ERR(dentry))
2574 		return PTR_ERR(dentry);
2575 
2576 	error = security_quota_on(dentry);
2577 	if (!error)
2578 		error = dquot_load_quota_inode(d_inode(dentry), type, format_id,
2579 				DQUOT_USAGE_ENABLED | DQUOT_LIMITS_ENABLED);
2580 
2581 	dput(dentry);
2582 	return error;
2583 }
2584 EXPORT_SYMBOL(dquot_quota_on_mount);
2585 
2586 static int dquot_quota_enable(struct super_block *sb, unsigned int flags)
2587 {
2588 	int ret;
2589 	int type;
2590 	struct quota_info *dqopt = sb_dqopt(sb);
2591 
2592 	if (!(dqopt->flags & DQUOT_QUOTA_SYS_FILE))
2593 		return -ENOSYS;
2594 	/* Accounting cannot be turned on while fs is mounted */
2595 	flags &= ~(FS_QUOTA_UDQ_ACCT | FS_QUOTA_GDQ_ACCT | FS_QUOTA_PDQ_ACCT);
2596 	if (!flags)
2597 		return -EINVAL;
2598 	for (type = 0; type < MAXQUOTAS; type++) {
2599 		if (!(flags & qtype_enforce_flag(type)))
2600 			continue;
2601 		/* Can't enforce without accounting */
2602 		if (!sb_has_quota_usage_enabled(sb, type)) {
2603 			ret = -EINVAL;
2604 			goto out_err;
2605 		}
2606 		if (sb_has_quota_limits_enabled(sb, type)) {
2607 			ret = -EBUSY;
2608 			goto out_err;
2609 		}
2610 		spin_lock(&dq_state_lock);
2611 		dqopt->flags |= dquot_state_flag(DQUOT_LIMITS_ENABLED, type);
2612 		spin_unlock(&dq_state_lock);
2613 	}
2614 	return 0;
2615 out_err:
2616 	/* Backout enforcement enablement we already did */
2617 	for (type--; type >= 0; type--)  {
2618 		if (flags & qtype_enforce_flag(type))
2619 			dquot_disable(sb, type, DQUOT_LIMITS_ENABLED);
2620 	}
2621 	/* Error code translation for better compatibility with XFS */
2622 	if (ret == -EBUSY)
2623 		ret = -EEXIST;
2624 	return ret;
2625 }
2626 
2627 static int dquot_quota_disable(struct super_block *sb, unsigned int flags)
2628 {
2629 	int ret;
2630 	int type;
2631 	struct quota_info *dqopt = sb_dqopt(sb);
2632 
2633 	if (!(dqopt->flags & DQUOT_QUOTA_SYS_FILE))
2634 		return -ENOSYS;
2635 	/*
2636 	 * We don't support turning off accounting via quotactl. In principle
2637 	 * quota infrastructure can do this but filesystems don't expect
2638 	 * userspace to be able to do it.
2639 	 */
2640 	if (flags &
2641 		  (FS_QUOTA_UDQ_ACCT | FS_QUOTA_GDQ_ACCT | FS_QUOTA_PDQ_ACCT))
2642 		return -EOPNOTSUPP;
2643 
2644 	/* Filter out limits not enabled */
2645 	for (type = 0; type < MAXQUOTAS; type++)
2646 		if (!sb_has_quota_limits_enabled(sb, type))
2647 			flags &= ~qtype_enforce_flag(type);
2648 	/* Nothing left? */
2649 	if (!flags)
2650 		return -EEXIST;
2651 	for (type = 0; type < MAXQUOTAS; type++) {
2652 		if (flags & qtype_enforce_flag(type)) {
2653 			ret = dquot_disable(sb, type, DQUOT_LIMITS_ENABLED);
2654 			if (ret < 0)
2655 				goto out_err;
2656 		}
2657 	}
2658 	return 0;
2659 out_err:
2660 	/* Backout enforcement disabling we already did */
2661 	for (type--; type >= 0; type--)  {
2662 		if (flags & qtype_enforce_flag(type)) {
2663 			spin_lock(&dq_state_lock);
2664 			dqopt->flags |=
2665 				dquot_state_flag(DQUOT_LIMITS_ENABLED, type);
2666 			spin_unlock(&dq_state_lock);
2667 		}
2668 	}
2669 	return ret;
2670 }
2671 
2672 /* Generic routine for getting common part of quota structure */
2673 static void do_get_dqblk(struct dquot *dquot, struct qc_dqblk *di)
2674 {
2675 	struct mem_dqblk *dm = &dquot->dq_dqb;
2676 
2677 	memset(di, 0, sizeof(*di));
2678 	spin_lock(&dquot->dq_dqb_lock);
2679 	di->d_spc_hardlimit = dm->dqb_bhardlimit;
2680 	di->d_spc_softlimit = dm->dqb_bsoftlimit;
2681 	di->d_ino_hardlimit = dm->dqb_ihardlimit;
2682 	di->d_ino_softlimit = dm->dqb_isoftlimit;
2683 	di->d_space = dm->dqb_curspace + dm->dqb_rsvspace;
2684 	di->d_ino_count = dm->dqb_curinodes;
2685 	di->d_spc_timer = dm->dqb_btime;
2686 	di->d_ino_timer = dm->dqb_itime;
2687 	spin_unlock(&dquot->dq_dqb_lock);
2688 }
2689 
2690 int dquot_get_dqblk(struct super_block *sb, struct kqid qid,
2691 		    struct qc_dqblk *di)
2692 {
2693 	struct dquot *dquot;
2694 
2695 	dquot = dqget(sb, qid);
2696 	if (IS_ERR(dquot))
2697 		return PTR_ERR(dquot);
2698 	do_get_dqblk(dquot, di);
2699 	dqput(dquot);
2700 
2701 	return 0;
2702 }
2703 EXPORT_SYMBOL(dquot_get_dqblk);
2704 
2705 int dquot_get_next_dqblk(struct super_block *sb, struct kqid *qid,
2706 			 struct qc_dqblk *di)
2707 {
2708 	struct dquot *dquot;
2709 	int err;
2710 
2711 	if (!sb->dq_op->get_next_id)
2712 		return -ENOSYS;
2713 	err = sb->dq_op->get_next_id(sb, qid);
2714 	if (err < 0)
2715 		return err;
2716 	dquot = dqget(sb, *qid);
2717 	if (IS_ERR(dquot))
2718 		return PTR_ERR(dquot);
2719 	do_get_dqblk(dquot, di);
2720 	dqput(dquot);
2721 
2722 	return 0;
2723 }
2724 EXPORT_SYMBOL(dquot_get_next_dqblk);
2725 
2726 #define VFS_QC_MASK \
2727 	(QC_SPACE | QC_SPC_SOFT | QC_SPC_HARD | \
2728 	 QC_INO_COUNT | QC_INO_SOFT | QC_INO_HARD | \
2729 	 QC_SPC_TIMER | QC_INO_TIMER)
2730 
2731 /* Generic routine for setting common part of quota structure */
2732 static int do_set_dqblk(struct dquot *dquot, struct qc_dqblk *di)
2733 {
2734 	struct mem_dqblk *dm = &dquot->dq_dqb;
2735 	int check_blim = 0, check_ilim = 0;
2736 	struct mem_dqinfo *dqi = &sb_dqopt(dquot->dq_sb)->info[dquot->dq_id.type];
2737 
2738 	if (di->d_fieldmask & ~VFS_QC_MASK)
2739 		return -EINVAL;
2740 
2741 	if (((di->d_fieldmask & QC_SPC_SOFT) &&
2742 	     di->d_spc_softlimit > dqi->dqi_max_spc_limit) ||
2743 	    ((di->d_fieldmask & QC_SPC_HARD) &&
2744 	     di->d_spc_hardlimit > dqi->dqi_max_spc_limit) ||
2745 	    ((di->d_fieldmask & QC_INO_SOFT) &&
2746 	     (di->d_ino_softlimit > dqi->dqi_max_ino_limit)) ||
2747 	    ((di->d_fieldmask & QC_INO_HARD) &&
2748 	     (di->d_ino_hardlimit > dqi->dqi_max_ino_limit)))
2749 		return -ERANGE;
2750 
2751 	spin_lock(&dquot->dq_dqb_lock);
2752 	if (di->d_fieldmask & QC_SPACE) {
2753 		dm->dqb_curspace = di->d_space - dm->dqb_rsvspace;
2754 		check_blim = 1;
2755 		set_bit(DQ_LASTSET_B + QIF_SPACE_B, &dquot->dq_flags);
2756 	}
2757 
2758 	if (di->d_fieldmask & QC_SPC_SOFT)
2759 		dm->dqb_bsoftlimit = di->d_spc_softlimit;
2760 	if (di->d_fieldmask & QC_SPC_HARD)
2761 		dm->dqb_bhardlimit = di->d_spc_hardlimit;
2762 	if (di->d_fieldmask & (QC_SPC_SOFT | QC_SPC_HARD)) {
2763 		check_blim = 1;
2764 		set_bit(DQ_LASTSET_B + QIF_BLIMITS_B, &dquot->dq_flags);
2765 	}
2766 
2767 	if (di->d_fieldmask & QC_INO_COUNT) {
2768 		dm->dqb_curinodes = di->d_ino_count;
2769 		check_ilim = 1;
2770 		set_bit(DQ_LASTSET_B + QIF_INODES_B, &dquot->dq_flags);
2771 	}
2772 
2773 	if (di->d_fieldmask & QC_INO_SOFT)
2774 		dm->dqb_isoftlimit = di->d_ino_softlimit;
2775 	if (di->d_fieldmask & QC_INO_HARD)
2776 		dm->dqb_ihardlimit = di->d_ino_hardlimit;
2777 	if (di->d_fieldmask & (QC_INO_SOFT | QC_INO_HARD)) {
2778 		check_ilim = 1;
2779 		set_bit(DQ_LASTSET_B + QIF_ILIMITS_B, &dquot->dq_flags);
2780 	}
2781 
2782 	if (di->d_fieldmask & QC_SPC_TIMER) {
2783 		dm->dqb_btime = di->d_spc_timer;
2784 		check_blim = 1;
2785 		set_bit(DQ_LASTSET_B + QIF_BTIME_B, &dquot->dq_flags);
2786 	}
2787 
2788 	if (di->d_fieldmask & QC_INO_TIMER) {
2789 		dm->dqb_itime = di->d_ino_timer;
2790 		check_ilim = 1;
2791 		set_bit(DQ_LASTSET_B + QIF_ITIME_B, &dquot->dq_flags);
2792 	}
2793 
2794 	if (check_blim) {
2795 		if (!dm->dqb_bsoftlimit ||
2796 		    dm->dqb_curspace + dm->dqb_rsvspace <= dm->dqb_bsoftlimit) {
2797 			dm->dqb_btime = 0;
2798 			clear_bit(DQ_BLKS_B, &dquot->dq_flags);
2799 		} else if (!(di->d_fieldmask & QC_SPC_TIMER))
2800 			/* Set grace only if user hasn't provided his own... */
2801 			dm->dqb_btime = ktime_get_real_seconds() + dqi->dqi_bgrace;
2802 	}
2803 	if (check_ilim) {
2804 		if (!dm->dqb_isoftlimit ||
2805 		    dm->dqb_curinodes <= dm->dqb_isoftlimit) {
2806 			dm->dqb_itime = 0;
2807 			clear_bit(DQ_INODES_B, &dquot->dq_flags);
2808 		} else if (!(di->d_fieldmask & QC_INO_TIMER))
2809 			/* Set grace only if user hasn't provided his own... */
2810 			dm->dqb_itime = ktime_get_real_seconds() + dqi->dqi_igrace;
2811 	}
2812 	if (dm->dqb_bhardlimit || dm->dqb_bsoftlimit || dm->dqb_ihardlimit ||
2813 	    dm->dqb_isoftlimit)
2814 		clear_bit(DQ_FAKE_B, &dquot->dq_flags);
2815 	else
2816 		set_bit(DQ_FAKE_B, &dquot->dq_flags);
2817 	spin_unlock(&dquot->dq_dqb_lock);
2818 	mark_dquot_dirty(dquot);
2819 
2820 	return 0;
2821 }
2822 
2823 int dquot_set_dqblk(struct super_block *sb, struct kqid qid,
2824 		  struct qc_dqblk *di)
2825 {
2826 	struct dquot *dquot;
2827 	int rc;
2828 
2829 	dquot = dqget(sb, qid);
2830 	if (IS_ERR(dquot)) {
2831 		rc = PTR_ERR(dquot);
2832 		goto out;
2833 	}
2834 	rc = do_set_dqblk(dquot, di);
2835 	dqput(dquot);
2836 out:
2837 	return rc;
2838 }
2839 EXPORT_SYMBOL(dquot_set_dqblk);
2840 
2841 /* Generic routine for getting common part of quota file information */
2842 int dquot_get_state(struct super_block *sb, struct qc_state *state)
2843 {
2844 	struct mem_dqinfo *mi;
2845 	struct qc_type_state *tstate;
2846 	struct quota_info *dqopt = sb_dqopt(sb);
2847 	int type;
2848 
2849 	memset(state, 0, sizeof(*state));
2850 	for (type = 0; type < MAXQUOTAS; type++) {
2851 		if (!sb_has_quota_active(sb, type))
2852 			continue;
2853 		tstate = state->s_state + type;
2854 		mi = sb_dqopt(sb)->info + type;
2855 		tstate->flags = QCI_ACCT_ENABLED;
2856 		spin_lock(&dq_data_lock);
2857 		if (mi->dqi_flags & DQF_SYS_FILE)
2858 			tstate->flags |= QCI_SYSFILE;
2859 		if (mi->dqi_flags & DQF_ROOT_SQUASH)
2860 			tstate->flags |= QCI_ROOT_SQUASH;
2861 		if (sb_has_quota_limits_enabled(sb, type))
2862 			tstate->flags |= QCI_LIMITS_ENFORCED;
2863 		tstate->spc_timelimit = mi->dqi_bgrace;
2864 		tstate->ino_timelimit = mi->dqi_igrace;
2865 		if (dqopt->files[type]) {
2866 			tstate->ino = dqopt->files[type]->i_ino;
2867 			tstate->blocks = dqopt->files[type]->i_blocks;
2868 		}
2869 		tstate->nextents = 1;	/* We don't know... */
2870 		spin_unlock(&dq_data_lock);
2871 	}
2872 	return 0;
2873 }
2874 EXPORT_SYMBOL(dquot_get_state);
2875 
2876 /* Generic routine for setting common part of quota file information */
2877 int dquot_set_dqinfo(struct super_block *sb, int type, struct qc_info *ii)
2878 {
2879 	struct mem_dqinfo *mi;
2880 
2881 	if ((ii->i_fieldmask & QC_WARNS_MASK) ||
2882 	    (ii->i_fieldmask & QC_RT_SPC_TIMER))
2883 		return -EINVAL;
2884 	if (!sb_has_quota_active(sb, type))
2885 		return -ESRCH;
2886 	mi = sb_dqopt(sb)->info + type;
2887 	if (ii->i_fieldmask & QC_FLAGS) {
2888 		if ((ii->i_flags & QCI_ROOT_SQUASH &&
2889 		     mi->dqi_format->qf_fmt_id != QFMT_VFS_OLD))
2890 			return -EINVAL;
2891 	}
2892 	spin_lock(&dq_data_lock);
2893 	if (ii->i_fieldmask & QC_SPC_TIMER)
2894 		mi->dqi_bgrace = ii->i_spc_timelimit;
2895 	if (ii->i_fieldmask & QC_INO_TIMER)
2896 		mi->dqi_igrace = ii->i_ino_timelimit;
2897 	if (ii->i_fieldmask & QC_FLAGS) {
2898 		if (ii->i_flags & QCI_ROOT_SQUASH)
2899 			mi->dqi_flags |= DQF_ROOT_SQUASH;
2900 		else
2901 			mi->dqi_flags &= ~DQF_ROOT_SQUASH;
2902 	}
2903 	spin_unlock(&dq_data_lock);
2904 	mark_info_dirty(sb, type);
2905 	/* Force write to disk */
2906 	return sb->dq_op->write_info(sb, type);
2907 }
2908 EXPORT_SYMBOL(dquot_set_dqinfo);
2909 
2910 const struct quotactl_ops dquot_quotactl_sysfile_ops = {
2911 	.quota_enable	= dquot_quota_enable,
2912 	.quota_disable	= dquot_quota_disable,
2913 	.quota_sync	= dquot_quota_sync,
2914 	.get_state	= dquot_get_state,
2915 	.set_info	= dquot_set_dqinfo,
2916 	.get_dqblk	= dquot_get_dqblk,
2917 	.get_nextdqblk	= dquot_get_next_dqblk,
2918 	.set_dqblk	= dquot_set_dqblk
2919 };
2920 EXPORT_SYMBOL(dquot_quotactl_sysfile_ops);
2921 
2922 static int do_proc_dqstats(struct ctl_table *table, int write,
2923 		     void *buffer, size_t *lenp, loff_t *ppos)
2924 {
2925 	unsigned int type = (unsigned long *)table->data - dqstats.stat;
2926 	s64 value = percpu_counter_sum(&dqstats.counter[type]);
2927 
2928 	/* Filter negative values for non-monotonic counters */
2929 	if (value < 0 && (type == DQST_ALLOC_DQUOTS ||
2930 			  type == DQST_FREE_DQUOTS))
2931 		value = 0;
2932 
2933 	/* Update global table */
2934 	dqstats.stat[type] = value;
2935 	return proc_doulongvec_minmax(table, write, buffer, lenp, ppos);
2936 }
2937 
2938 static struct ctl_table fs_dqstats_table[] = {
2939 	{
2940 		.procname	= "lookups",
2941 		.data		= &dqstats.stat[DQST_LOOKUPS],
2942 		.maxlen		= sizeof(unsigned long),
2943 		.mode		= 0444,
2944 		.proc_handler	= do_proc_dqstats,
2945 	},
2946 	{
2947 		.procname	= "drops",
2948 		.data		= &dqstats.stat[DQST_DROPS],
2949 		.maxlen		= sizeof(unsigned long),
2950 		.mode		= 0444,
2951 		.proc_handler	= do_proc_dqstats,
2952 	},
2953 	{
2954 		.procname	= "reads",
2955 		.data		= &dqstats.stat[DQST_READS],
2956 		.maxlen		= sizeof(unsigned long),
2957 		.mode		= 0444,
2958 		.proc_handler	= do_proc_dqstats,
2959 	},
2960 	{
2961 		.procname	= "writes",
2962 		.data		= &dqstats.stat[DQST_WRITES],
2963 		.maxlen		= sizeof(unsigned long),
2964 		.mode		= 0444,
2965 		.proc_handler	= do_proc_dqstats,
2966 	},
2967 	{
2968 		.procname	= "cache_hits",
2969 		.data		= &dqstats.stat[DQST_CACHE_HITS],
2970 		.maxlen		= sizeof(unsigned long),
2971 		.mode		= 0444,
2972 		.proc_handler	= do_proc_dqstats,
2973 	},
2974 	{
2975 		.procname	= "allocated_dquots",
2976 		.data		= &dqstats.stat[DQST_ALLOC_DQUOTS],
2977 		.maxlen		= sizeof(unsigned long),
2978 		.mode		= 0444,
2979 		.proc_handler	= do_proc_dqstats,
2980 	},
2981 	{
2982 		.procname	= "free_dquots",
2983 		.data		= &dqstats.stat[DQST_FREE_DQUOTS],
2984 		.maxlen		= sizeof(unsigned long),
2985 		.mode		= 0444,
2986 		.proc_handler	= do_proc_dqstats,
2987 	},
2988 	{
2989 		.procname	= "syncs",
2990 		.data		= &dqstats.stat[DQST_SYNCS],
2991 		.maxlen		= sizeof(unsigned long),
2992 		.mode		= 0444,
2993 		.proc_handler	= do_proc_dqstats,
2994 	},
2995 #ifdef CONFIG_PRINT_QUOTA_WARNING
2996 	{
2997 		.procname	= "warnings",
2998 		.data		= &flag_print_warnings,
2999 		.maxlen		= sizeof(int),
3000 		.mode		= 0644,
3001 		.proc_handler	= proc_dointvec,
3002 	},
3003 #endif
3004 	{ },
3005 };
3006 
3007 static int __init dquot_init(void)
3008 {
3009 	int i, ret;
3010 	unsigned long nr_hash, order;
3011 
3012 	printk(KERN_NOTICE "VFS: Disk quotas %s\n", __DQUOT_VERSION__);
3013 
3014 	register_sysctl_init("fs/quota", fs_dqstats_table);
3015 
3016 	dquot_cachep = kmem_cache_create("dquot",
3017 			sizeof(struct dquot), sizeof(unsigned long) * 4,
3018 			(SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT|
3019 				SLAB_MEM_SPREAD|SLAB_PANIC),
3020 			NULL);
3021 
3022 	order = 0;
3023 	dquot_hash = (struct hlist_head *)__get_free_pages(GFP_KERNEL, order);
3024 	if (!dquot_hash)
3025 		panic("Cannot create dquot hash table");
3026 
3027 	for (i = 0; i < _DQST_DQSTAT_LAST; i++) {
3028 		ret = percpu_counter_init(&dqstats.counter[i], 0, GFP_KERNEL);
3029 		if (ret)
3030 			panic("Cannot create dquot stat counters");
3031 	}
3032 
3033 	/* Find power-of-two hlist_heads which can fit into allocation */
3034 	nr_hash = (1UL << order) * PAGE_SIZE / sizeof(struct hlist_head);
3035 	dq_hash_bits = ilog2(nr_hash);
3036 
3037 	nr_hash = 1UL << dq_hash_bits;
3038 	dq_hash_mask = nr_hash - 1;
3039 	for (i = 0; i < nr_hash; i++)
3040 		INIT_HLIST_HEAD(dquot_hash + i);
3041 
3042 	pr_info("VFS: Dquot-cache hash table entries: %ld (order %ld,"
3043 		" %ld bytes)\n", nr_hash, order, (PAGE_SIZE << order));
3044 
3045 	if (register_shrinker(&dqcache_shrinker, "dquota-cache"))
3046 		panic("Cannot register dquot shrinker");
3047 
3048 	return 0;
3049 }
3050 fs_initcall(dquot_init);
3051