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