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