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