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