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