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