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