blob: ed131318b8490ce32d8b454f9d7e4a6dc4e77e7e [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070012 * 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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070070#include <linux/init.h>
71#include <linux/module.h>
72#include <linux/proc_fs.h>
73#include <linux/security.h>
74#include <linux/kmod.h>
75#include <linux/namei.h>
76#include <linux/buffer_head.h>
Randy Dunlap16f7e0f2006-01-11 12:17:46 -080077#include <linux/capability.h>
Adrian Bunkbe586ba2005-11-07 00:59:35 -080078#include <linux/quotaops.h>
Christoph Hellwigfb58b732007-02-12 00:51:57 -080079#include <linux/writeback.h> /* for inode_lock, oddly enough.. */
Linus Torvalds1da177e2005-04-16 15:20:36 -070080
81#include <asm/uaccess.h>
82
83#define __DQUOT_PARANOIA
84
85/*
Jan Karacc334122009-01-12 17:23:05 +010086 * There are three quota SMP locks. dq_list_lock protects all lists with quotas
87 * and quota formats, dqstats structure containing statistics about the lists
88 * dq_data_lock protects data from dq_dqb and also mem_dqinfo structures and
89 * also guards consistency of dquot->dq_dqb with inode->i_blocks, i_bytes.
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 * i_blocks and i_bytes updates itself are guarded by i_lock acquired directly
Jan Karacc334122009-01-12 17:23:05 +010091 * in inode_add_bytes() and inode_sub_bytes(). dq_state_lock protects
92 * modifications of quota state (on quotaon and quotaoff) and readers who care
93 * about latest values take it as well.
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 *
Jan Karacc334122009-01-12 17:23:05 +010095 * The spinlock ordering is hence: dq_data_lock > dq_list_lock > i_lock,
96 * dq_list_lock > dq_state_lock
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 *
98 * Note that some things (eg. sb pointer, type, id) doesn't change during
99 * the life of the dquot structure and so needn't to be protected by a lock
100 *
101 * Any operation working on dquots via inode pointers must hold dqptr_sem. If
102 * operation is just reading pointers from inode (or not using them at all) the
Jan Kara26245c92010-01-06 17:20:35 +0100103 * read lock is enough. If pointers are altered function must hold write lock.
104 * Special care needs to be taken about S_NOQUOTA inode flag (marking that
105 * inode is a quota file). Functions adding pointers from inode to dquots have
106 * to check this flag under dqptr_sem and then (if S_NOQUOTA is not set) they
107 * have to do all pointer modifications before dropping dqptr_sem. This makes
108 * sure they cannot race with quotaon which first sets S_NOQUOTA flag and
109 * then drops all pointers to dquots from an inode.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 *
Ingo Molnard3be9152006-03-23 03:00:29 -0800111 * Each dquot has its dq_lock mutex. Locked dquots might not be referenced
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 * from inodes (dquot_alloc_space() and such don't check the dq_lock).
113 * Currently dquot is locked only when it is being read to memory (or space for
114 * it is being allocated) on the first dqget() and when it is being released on
115 * the last dqput(). The allocation and release oparations are serialized by
116 * the dq_lock and by checking the use count in dquot_release(). Write
117 * operations on dquots don't hold dq_lock as they copy data under dq_data_lock
118 * spinlock to internal buffers before writing.
119 *
120 * Lock ordering (including related VFS locks) is the following:
Ingo Molnard3be9152006-03-23 03:00:29 -0800121 * i_mutex > dqonoff_sem > journal_lock > dqptr_sem > dquot->dq_lock >
122 * dqio_mutex
Jan Karacc334122009-01-12 17:23:05 +0100123 * The lock ordering of dqptr_sem imposed by quota code is only dqonoff_sem >
124 * dqptr_sem. But filesystem has to count with the fact that functions such as
125 * dquot_alloc_space() acquire dqptr_sem and they usually have to be called
126 * from inside a transaction to keep filesystem consistency after a crash. Also
127 * filesystems usually want to do some IO on dquot from ->mark_dirty which is
128 * called with dqptr_sem held.
Ingo Molnard3be9152006-03-23 03:00:29 -0800129 * i_mutex on quota files is special (it's below dqio_mutex)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 */
131
Jan Karac5166102009-01-26 15:32:46 +0100132static __cacheline_aligned_in_smp DEFINE_SPINLOCK(dq_list_lock);
133static __cacheline_aligned_in_smp DEFINE_SPINLOCK(dq_state_lock);
134__cacheline_aligned_in_smp DEFINE_SPINLOCK(dq_data_lock);
Mingming Cao08d03502009-01-14 16:19:32 +0100135EXPORT_SYMBOL(dq_data_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136
137static char *quotatypes[] = INITQFNAMES;
138static struct quota_format_type *quota_formats; /* List of registered formats */
139static struct quota_module_name module_names[] = INIT_QUOTA_MODULE_NAMES;
140
141/* SLAB cache for dquot structures */
Christoph Lametere18b8902006-12-06 20:33:20 -0800142static struct kmem_cache *dquot_cachep;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143
144int register_quota_format(struct quota_format_type *fmt)
145{
146 spin_lock(&dq_list_lock);
147 fmt->qf_next = quota_formats;
148 quota_formats = fmt;
149 spin_unlock(&dq_list_lock);
150 return 0;
151}
Mingming Cao08d03502009-01-14 16:19:32 +0100152EXPORT_SYMBOL(register_quota_format);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153
154void unregister_quota_format(struct quota_format_type *fmt)
155{
156 struct quota_format_type **actqf;
157
158 spin_lock(&dq_list_lock);
Jan Kara268157b2009-01-27 15:47:22 +0100159 for (actqf = &quota_formats; *actqf && *actqf != fmt;
160 actqf = &(*actqf)->qf_next)
161 ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 if (*actqf)
163 *actqf = (*actqf)->qf_next;
164 spin_unlock(&dq_list_lock);
165}
Mingming Cao08d03502009-01-14 16:19:32 +0100166EXPORT_SYMBOL(unregister_quota_format);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167
168static struct quota_format_type *find_quota_format(int id)
169{
170 struct quota_format_type *actqf;
171
172 spin_lock(&dq_list_lock);
Jan Kara268157b2009-01-27 15:47:22 +0100173 for (actqf = quota_formats; actqf && actqf->qf_fmt_id != id;
174 actqf = actqf->qf_next)
175 ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 if (!actqf || !try_module_get(actqf->qf_owner)) {
177 int qm;
178
179 spin_unlock(&dq_list_lock);
180
Jan Kara268157b2009-01-27 15:47:22 +0100181 for (qm = 0; module_names[qm].qm_fmt_id &&
182 module_names[qm].qm_fmt_id != id; qm++)
183 ;
184 if (!module_names[qm].qm_fmt_id ||
185 request_module(module_names[qm].qm_mod_name))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 return NULL;
187
188 spin_lock(&dq_list_lock);
Jan Kara268157b2009-01-27 15:47:22 +0100189 for (actqf = quota_formats; actqf && actqf->qf_fmt_id != id;
190 actqf = actqf->qf_next)
191 ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 if (actqf && !try_module_get(actqf->qf_owner))
193 actqf = NULL;
194 }
195 spin_unlock(&dq_list_lock);
196 return actqf;
197}
198
199static void put_quota_format(struct quota_format_type *fmt)
200{
201 module_put(fmt->qf_owner);
202}
203
204/*
205 * Dquot List Management:
206 * The quota code uses three lists for dquot management: the inuse_list,
207 * free_dquots, and dquot_hash[] array. A single dquot structure may be
208 * on all three lists, depending on its current state.
209 *
210 * All dquots are placed to the end of inuse_list when first created, and this
211 * list is used for invalidate operation, which must look at every dquot.
212 *
213 * Unused dquots (dq_count == 0) are added to the free_dquots list when freed,
214 * and this list is searched whenever we need an available dquot. Dquots are
215 * removed from the list as soon as they are used again, and
216 * dqstats.free_dquots gives the number of dquots on the list. When
217 * dquot is invalidated it's completely released from memory.
218 *
219 * Dquots with a specific identity (device, type and id) are placed on
220 * one of the dquot_hash[] hash chains. The provides an efficient search
221 * mechanism to locate a specific dquot.
222 */
223
224static LIST_HEAD(inuse_list);
225static LIST_HEAD(free_dquots);
226static unsigned int dq_hash_bits, dq_hash_mask;
227static struct hlist_head *dquot_hash;
228
229struct dqstats dqstats;
Mingming Cao08d03502009-01-14 16:19:32 +0100230EXPORT_SYMBOL(dqstats);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231
Jan Kara0a5a9c72010-02-09 18:20:39 +0100232static qsize_t inode_get_rsv_space(struct inode *inode);
233
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234static inline unsigned int
235hashfn(const struct super_block *sb, unsigned int id, int type)
236{
237 unsigned long tmp;
238
239 tmp = (((unsigned long)sb>>L1_CACHE_SHIFT) ^ id) * (MAXQUOTAS - type);
240 return (tmp + (tmp >> dq_hash_bits)) & dq_hash_mask;
241}
242
243/*
244 * Following list functions expect dq_list_lock to be held
245 */
246static inline void insert_dquot_hash(struct dquot *dquot)
247{
Jan Kara268157b2009-01-27 15:47:22 +0100248 struct hlist_head *head;
249 head = dquot_hash + hashfn(dquot->dq_sb, dquot->dq_id, dquot->dq_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 hlist_add_head(&dquot->dq_hash, head);
251}
252
253static inline void remove_dquot_hash(struct dquot *dquot)
254{
255 hlist_del_init(&dquot->dq_hash);
256}
257
Jan Kara7a2435d2009-01-27 01:47:11 +0100258static struct dquot *find_dquot(unsigned int hashent, struct super_block *sb,
259 unsigned int id, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260{
261 struct hlist_node *node;
262 struct dquot *dquot;
263
264 hlist_for_each (node, dquot_hash+hashent) {
265 dquot = hlist_entry(node, struct dquot, dq_hash);
Jan Kara268157b2009-01-27 15:47:22 +0100266 if (dquot->dq_sb == sb && dquot->dq_id == id &&
267 dquot->dq_type == type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 return dquot;
269 }
Jan Karadd6f3c62009-01-26 16:01:43 +0100270 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271}
272
273/* Add a dquot to the tail of the free list */
274static inline void put_dquot_last(struct dquot *dquot)
275{
Akinobu Mita8e130592006-06-26 00:24:37 -0700276 list_add_tail(&dquot->dq_free, &free_dquots);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 dqstats.free_dquots++;
278}
279
280static inline void remove_free_dquot(struct dquot *dquot)
281{
282 if (list_empty(&dquot->dq_free))
283 return;
284 list_del_init(&dquot->dq_free);
285 dqstats.free_dquots--;
286}
287
288static inline void put_inuse(struct dquot *dquot)
289{
290 /* We add to the back of inuse list so we don't have to restart
291 * when traversing this list and we block */
Akinobu Mita8e130592006-06-26 00:24:37 -0700292 list_add_tail(&dquot->dq_inuse, &inuse_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 dqstats.allocated_dquots++;
294}
295
296static inline void remove_inuse(struct dquot *dquot)
297{
298 dqstats.allocated_dquots--;
299 list_del(&dquot->dq_inuse);
300}
301/*
302 * End of list functions needing dq_list_lock
303 */
304
305static void wait_on_dquot(struct dquot *dquot)
306{
Ingo Molnard3be9152006-03-23 03:00:29 -0800307 mutex_lock(&dquot->dq_lock);
308 mutex_unlock(&dquot->dq_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309}
310
Jan Kara03f6e922008-04-28 02:14:32 -0700311static inline int dquot_dirty(struct dquot *dquot)
312{
313 return test_bit(DQ_MOD_B, &dquot->dq_flags);
314}
315
316static inline int mark_dquot_dirty(struct dquot *dquot)
317{
318 return dquot->dq_sb->dq_op->mark_dirty(dquot);
319}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320
321int dquot_mark_dquot_dirty(struct dquot *dquot)
322{
323 spin_lock(&dq_list_lock);
324 if (!test_and_set_bit(DQ_MOD_B, &dquot->dq_flags))
325 list_add(&dquot->dq_dirty, &sb_dqopt(dquot->dq_sb)->
326 info[dquot->dq_type].dqi_dirty_list);
327 spin_unlock(&dq_list_lock);
328 return 0;
329}
Mingming Cao08d03502009-01-14 16:19:32 +0100330EXPORT_SYMBOL(dquot_mark_dquot_dirty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331
Dmitry Monakhovdc52dd32009-12-14 15:21:15 +0300332/* Dirtify all the dquots - this can block when journalling */
333static inline int mark_all_dquot_dirty(struct dquot * const *dquot)
334{
335 int ret, err, cnt;
336
337 ret = err = 0;
338 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
339 if (dquot[cnt])
340 /* Even in case of error we have to continue */
341 ret = mark_dquot_dirty(dquot[cnt]);
342 if (!err)
343 err = ret;
344 }
345 return err;
346}
347
348static inline void dqput_all(struct dquot **dquot)
349{
350 unsigned int cnt;
351
352 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
353 dqput(dquot[cnt]);
354}
355
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356/* This function needs dq_list_lock */
357static inline int clear_dquot_dirty(struct dquot *dquot)
358{
359 if (!test_and_clear_bit(DQ_MOD_B, &dquot->dq_flags))
360 return 0;
361 list_del_init(&dquot->dq_dirty);
362 return 1;
363}
364
365void mark_info_dirty(struct super_block *sb, int type)
366{
367 set_bit(DQF_INFO_DIRTY_B, &sb_dqopt(sb)->info[type].dqi_flags);
368}
369EXPORT_SYMBOL(mark_info_dirty);
370
371/*
372 * Read dquot from disk and alloc space for it
373 */
374
375int dquot_acquire(struct dquot *dquot)
376{
377 int ret = 0, ret2 = 0;
378 struct quota_info *dqopt = sb_dqopt(dquot->dq_sb);
379
Ingo Molnard3be9152006-03-23 03:00:29 -0800380 mutex_lock(&dquot->dq_lock);
381 mutex_lock(&dqopt->dqio_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 if (!test_bit(DQ_READ_B, &dquot->dq_flags))
383 ret = dqopt->ops[dquot->dq_type]->read_dqblk(dquot);
384 if (ret < 0)
385 goto out_iolock;
386 set_bit(DQ_READ_B, &dquot->dq_flags);
387 /* Instantiate dquot if needed */
388 if (!test_bit(DQ_ACTIVE_B, &dquot->dq_flags) && !dquot->dq_off) {
389 ret = dqopt->ops[dquot->dq_type]->commit_dqblk(dquot);
390 /* Write the info if needed */
Jan Kara268157b2009-01-27 15:47:22 +0100391 if (info_dirty(&dqopt->info[dquot->dq_type])) {
392 ret2 = dqopt->ops[dquot->dq_type]->write_file_info(
393 dquot->dq_sb, dquot->dq_type);
394 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 if (ret < 0)
396 goto out_iolock;
397 if (ret2 < 0) {
398 ret = ret2;
399 goto out_iolock;
400 }
401 }
402 set_bit(DQ_ACTIVE_B, &dquot->dq_flags);
403out_iolock:
Ingo Molnard3be9152006-03-23 03:00:29 -0800404 mutex_unlock(&dqopt->dqio_mutex);
405 mutex_unlock(&dquot->dq_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 return ret;
407}
Mingming Cao08d03502009-01-14 16:19:32 +0100408EXPORT_SYMBOL(dquot_acquire);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409
410/*
411 * Write dquot to disk
412 */
413int dquot_commit(struct dquot *dquot)
414{
415 int ret = 0, ret2 = 0;
416 struct quota_info *dqopt = sb_dqopt(dquot->dq_sb);
417
Ingo Molnard3be9152006-03-23 03:00:29 -0800418 mutex_lock(&dqopt->dqio_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 spin_lock(&dq_list_lock);
420 if (!clear_dquot_dirty(dquot)) {
421 spin_unlock(&dq_list_lock);
422 goto out_sem;
423 }
424 spin_unlock(&dq_list_lock);
425 /* Inactive dquot can be only if there was error during read/init
426 * => we have better not writing it */
427 if (test_bit(DQ_ACTIVE_B, &dquot->dq_flags)) {
428 ret = dqopt->ops[dquot->dq_type]->commit_dqblk(dquot);
Jan Kara268157b2009-01-27 15:47:22 +0100429 if (info_dirty(&dqopt->info[dquot->dq_type])) {
430 ret2 = dqopt->ops[dquot->dq_type]->write_file_info(
431 dquot->dq_sb, dquot->dq_type);
432 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 if (ret >= 0)
434 ret = ret2;
435 }
436out_sem:
Ingo Molnard3be9152006-03-23 03:00:29 -0800437 mutex_unlock(&dqopt->dqio_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 return ret;
439}
Mingming Cao08d03502009-01-14 16:19:32 +0100440EXPORT_SYMBOL(dquot_commit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441
442/*
443 * Release dquot
444 */
445int dquot_release(struct dquot *dquot)
446{
447 int ret = 0, ret2 = 0;
448 struct quota_info *dqopt = sb_dqopt(dquot->dq_sb);
449
Ingo Molnard3be9152006-03-23 03:00:29 -0800450 mutex_lock(&dquot->dq_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 /* Check whether we are not racing with some other dqget() */
452 if (atomic_read(&dquot->dq_count) > 1)
453 goto out_dqlock;
Ingo Molnard3be9152006-03-23 03:00:29 -0800454 mutex_lock(&dqopt->dqio_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 if (dqopt->ops[dquot->dq_type]->release_dqblk) {
456 ret = dqopt->ops[dquot->dq_type]->release_dqblk(dquot);
457 /* Write the info */
Jan Kara268157b2009-01-27 15:47:22 +0100458 if (info_dirty(&dqopt->info[dquot->dq_type])) {
459 ret2 = dqopt->ops[dquot->dq_type]->write_file_info(
460 dquot->dq_sb, dquot->dq_type);
461 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 if (ret >= 0)
463 ret = ret2;
464 }
465 clear_bit(DQ_ACTIVE_B, &dquot->dq_flags);
Ingo Molnard3be9152006-03-23 03:00:29 -0800466 mutex_unlock(&dqopt->dqio_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467out_dqlock:
Ingo Molnard3be9152006-03-23 03:00:29 -0800468 mutex_unlock(&dquot->dq_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 return ret;
470}
Mingming Cao08d03502009-01-14 16:19:32 +0100471EXPORT_SYMBOL(dquot_release);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472
Jan Kara7d9056b2008-11-25 15:31:32 +0100473void dquot_destroy(struct dquot *dquot)
Jan Kara74f783a2008-08-19 14:51:22 +0200474{
475 kmem_cache_free(dquot_cachep, dquot);
476}
Jan Kara7d9056b2008-11-25 15:31:32 +0100477EXPORT_SYMBOL(dquot_destroy);
Jan Kara74f783a2008-08-19 14:51:22 +0200478
479static inline void do_destroy_dquot(struct dquot *dquot)
480{
481 dquot->dq_sb->dq_op->destroy_dquot(dquot);
482}
483
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484/* Invalidate all dquots on the list. Note that this function is called after
485 * quota is disabled and pointers from inodes removed so there cannot be new
Jan Kara6362e4d2006-03-23 03:00:17 -0800486 * quota users. There can still be some users of quotas due to inodes being
487 * just deleted or pruned by prune_icache() (those are not attached to any
Jan Karacc334122009-01-12 17:23:05 +0100488 * list) or parallel quotactl call. We have to wait for such users.
Jan Kara6362e4d2006-03-23 03:00:17 -0800489 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490static void invalidate_dquots(struct super_block *sb, int type)
491{
Domen Puncerc33ed272005-06-25 14:59:36 -0700492 struct dquot *dquot, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493
Jan Kara6362e4d2006-03-23 03:00:17 -0800494restart:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 spin_lock(&dq_list_lock);
Domen Puncerc33ed272005-06-25 14:59:36 -0700496 list_for_each_entry_safe(dquot, tmp, &inuse_list, dq_inuse) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 if (dquot->dq_sb != sb)
498 continue;
499 if (dquot->dq_type != type)
500 continue;
Jan Kara6362e4d2006-03-23 03:00:17 -0800501 /* Wait for dquot users */
502 if (atomic_read(&dquot->dq_count)) {
503 DEFINE_WAIT(wait);
504
505 atomic_inc(&dquot->dq_count);
506 prepare_to_wait(&dquot->dq_wait_unused, &wait,
507 TASK_UNINTERRUPTIBLE);
508 spin_unlock(&dq_list_lock);
509 /* Once dqput() wakes us up, we know it's time to free
510 * the dquot.
511 * IMPORTANT: we rely on the fact that there is always
512 * at most one process waiting for dquot to free.
513 * Otherwise dq_count would be > 1 and we would never
514 * wake up.
515 */
516 if (atomic_read(&dquot->dq_count) > 1)
517 schedule();
518 finish_wait(&dquot->dq_wait_unused, &wait);
519 dqput(dquot);
520 /* At this moment dquot() need not exist (it could be
521 * reclaimed by prune_dqcache(). Hence we must
522 * restart. */
523 goto restart;
524 }
525 /*
526 * Quota now has no users and it has been written on last
527 * dqput()
528 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 remove_dquot_hash(dquot);
530 remove_free_dquot(dquot);
531 remove_inuse(dquot);
Jan Kara74f783a2008-08-19 14:51:22 +0200532 do_destroy_dquot(dquot);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 }
534 spin_unlock(&dq_list_lock);
535}
536
Jan Kara12c77522008-10-20 17:05:00 +0200537/* Call callback for every active dquot on given filesystem */
538int dquot_scan_active(struct super_block *sb,
539 int (*fn)(struct dquot *dquot, unsigned long priv),
540 unsigned long priv)
541{
542 struct dquot *dquot, *old_dquot = NULL;
543 int ret = 0;
544
545 mutex_lock(&sb_dqopt(sb)->dqonoff_mutex);
546 spin_lock(&dq_list_lock);
547 list_for_each_entry(dquot, &inuse_list, dq_inuse) {
548 if (!test_bit(DQ_ACTIVE_B, &dquot->dq_flags))
549 continue;
550 if (dquot->dq_sb != sb)
551 continue;
552 /* Now we have active dquot so we can just increase use count */
553 atomic_inc(&dquot->dq_count);
554 dqstats.lookups++;
555 spin_unlock(&dq_list_lock);
556 dqput(old_dquot);
557 old_dquot = dquot;
558 ret = fn(dquot, priv);
559 if (ret < 0)
560 goto out;
561 spin_lock(&dq_list_lock);
562 /* We are safe to continue now because our dquot could not
563 * be moved out of the inuse list while we hold the reference */
564 }
565 spin_unlock(&dq_list_lock);
566out:
567 dqput(old_dquot);
568 mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
569 return ret;
570}
Mingming Cao08d03502009-01-14 16:19:32 +0100571EXPORT_SYMBOL(dquot_scan_active);
Jan Kara12c77522008-10-20 17:05:00 +0200572
Christoph Hellwig5fb324a2010-02-16 03:44:52 -0500573int vfs_quota_sync(struct super_block *sb, int type, int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574{
575 struct list_head *dirty;
576 struct dquot *dquot;
577 struct quota_info *dqopt = sb_dqopt(sb);
578 int cnt;
579
Ingo Molnard3be9152006-03-23 03:00:29 -0800580 mutex_lock(&dqopt->dqonoff_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
582 if (type != -1 && cnt != type)
583 continue;
Jan Karaf55abc02008-08-20 17:50:32 +0200584 if (!sb_has_quota_active(sb, cnt))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 continue;
586 spin_lock(&dq_list_lock);
587 dirty = &dqopt->info[cnt].dqi_dirty_list;
588 while (!list_empty(dirty)) {
Jan Kara268157b2009-01-27 15:47:22 +0100589 dquot = list_first_entry(dirty, struct dquot,
590 dq_dirty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 /* Dirty and inactive can be only bad dquot... */
592 if (!test_bit(DQ_ACTIVE_B, &dquot->dq_flags)) {
593 clear_dquot_dirty(dquot);
594 continue;
595 }
596 /* Now we have active dquot from which someone is
597 * holding reference so we can safely just increase
598 * use count */
599 atomic_inc(&dquot->dq_count);
600 dqstats.lookups++;
601 spin_unlock(&dq_list_lock);
602 sb->dq_op->write_dquot(dquot);
603 dqput(dquot);
604 spin_lock(&dq_list_lock);
605 }
606 spin_unlock(&dq_list_lock);
607 }
608
609 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
Jan Karaf55abc02008-08-20 17:50:32 +0200610 if ((cnt == type || type == -1) && sb_has_quota_active(sb, cnt)
611 && info_dirty(&dqopt->info[cnt]))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 sb->dq_op->write_info(sb, cnt);
613 spin_lock(&dq_list_lock);
614 dqstats.syncs++;
615 spin_unlock(&dq_list_lock);
Ingo Molnard3be9152006-03-23 03:00:29 -0800616 mutex_unlock(&dqopt->dqonoff_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617
Christoph Hellwig5fb324a2010-02-16 03:44:52 -0500618 if (!wait || (sb_dqopt(sb)->flags & DQUOT_QUOTA_SYS_FILE))
619 return 0;
620
621 /* This is not very clever (and fast) but currently I don't know about
622 * any other simple way of getting quota data to disk and we must get
623 * them there for userspace to be visible... */
624 if (sb->s_op->sync_fs)
625 sb->s_op->sync_fs(sb, 1);
626 sync_blockdev(sb->s_bdev);
627
628 /*
629 * Now when everything is written we can discard the pagecache so
630 * that userspace sees the changes.
631 */
632 mutex_lock(&sb_dqopt(sb)->dqonoff_mutex);
633 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
634 if (type != -1 && cnt != type)
635 continue;
636 if (!sb_has_quota_active(sb, cnt))
637 continue;
638 mutex_lock_nested(&sb_dqopt(sb)->files[cnt]->i_mutex,
639 I_MUTEX_QUOTA);
640 truncate_inode_pages(&sb_dqopt(sb)->files[cnt]->i_data, 0);
641 mutex_unlock(&sb_dqopt(sb)->files[cnt]->i_mutex);
642 }
643 mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
644
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 return 0;
646}
Mingming Cao08d03502009-01-14 16:19:32 +0100647EXPORT_SYMBOL(vfs_quota_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648
649/* Free unused dquots from cache */
650static void prune_dqcache(int count)
651{
652 struct list_head *head;
653 struct dquot *dquot;
654
655 head = free_dquots.prev;
656 while (head != &free_dquots && count) {
657 dquot = list_entry(head, struct dquot, dq_free);
658 remove_dquot_hash(dquot);
659 remove_free_dquot(dquot);
660 remove_inuse(dquot);
Jan Kara74f783a2008-08-19 14:51:22 +0200661 do_destroy_dquot(dquot);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 count--;
663 head = free_dquots.prev;
664 }
665}
666
667/*
668 * This is called from kswapd when we think we need some
669 * more memory
670 */
671
Al Viro27496a82005-10-21 03:20:48 -0400672static int shrink_dqcache_memory(int nr, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673{
674 if (nr) {
675 spin_lock(&dq_list_lock);
676 prune_dqcache(nr);
677 spin_unlock(&dq_list_lock);
678 }
679 return (dqstats.free_dquots / 100) * sysctl_vfs_cache_pressure;
680}
681
Rusty Russell8e1f9362007-07-17 04:03:17 -0700682static struct shrinker dqcache_shrinker = {
683 .shrink = shrink_dqcache_memory,
684 .seeks = DEFAULT_SEEKS,
685};
686
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687/*
688 * Put reference to dquot
689 * NOTE: If you change this function please check whether dqput_blocks() works right...
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 */
Jan Kara3d9ea252008-10-10 16:12:23 +0200691void dqput(struct dquot *dquot)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692{
Jan Karab48d3802008-07-25 01:46:49 -0700693 int ret;
694
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 if (!dquot)
696 return;
697#ifdef __DQUOT_PARANOIA
698 if (!atomic_read(&dquot->dq_count)) {
699 printk("VFS: dqput: trying to free free dquot\n");
700 printk("VFS: device %s, dquot of %s %d\n",
701 dquot->dq_sb->s_id,
702 quotatypes[dquot->dq_type],
703 dquot->dq_id);
704 BUG();
705 }
706#endif
707
708 spin_lock(&dq_list_lock);
709 dqstats.drops++;
710 spin_unlock(&dq_list_lock);
711we_slept:
712 spin_lock(&dq_list_lock);
713 if (atomic_read(&dquot->dq_count) > 1) {
714 /* We have more than one user... nothing to do */
715 atomic_dec(&dquot->dq_count);
Jan Kara6362e4d2006-03-23 03:00:17 -0800716 /* Releasing dquot during quotaoff phase? */
Jan Karaf55abc02008-08-20 17:50:32 +0200717 if (!sb_has_quota_active(dquot->dq_sb, dquot->dq_type) &&
Jan Kara6362e4d2006-03-23 03:00:17 -0800718 atomic_read(&dquot->dq_count) == 1)
719 wake_up(&dquot->dq_wait_unused);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 spin_unlock(&dq_list_lock);
721 return;
722 }
723 /* Need to release dquot? */
724 if (test_bit(DQ_ACTIVE_B, &dquot->dq_flags) && dquot_dirty(dquot)) {
725 spin_unlock(&dq_list_lock);
726 /* Commit dquot before releasing */
Jan Karab48d3802008-07-25 01:46:49 -0700727 ret = dquot->dq_sb->dq_op->write_dquot(dquot);
728 if (ret < 0) {
729 printk(KERN_ERR "VFS: cannot write quota structure on "
730 "device %s (error %d). Quota may get out of "
731 "sync!\n", dquot->dq_sb->s_id, ret);
732 /*
733 * We clear dirty bit anyway, so that we avoid
734 * infinite loop here
735 */
736 spin_lock(&dq_list_lock);
737 clear_dquot_dirty(dquot);
738 spin_unlock(&dq_list_lock);
739 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 goto we_slept;
741 }
742 /* Clear flag in case dquot was inactive (something bad happened) */
743 clear_dquot_dirty(dquot);
744 if (test_bit(DQ_ACTIVE_B, &dquot->dq_flags)) {
745 spin_unlock(&dq_list_lock);
746 dquot->dq_sb->dq_op->release_dquot(dquot);
747 goto we_slept;
748 }
749 atomic_dec(&dquot->dq_count);
750#ifdef __DQUOT_PARANOIA
751 /* sanity check */
Eric Sesterhenn8abf6a42006-04-02 13:36:13 +0200752 BUG_ON(!list_empty(&dquot->dq_free));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753#endif
754 put_dquot_last(dquot);
755 spin_unlock(&dq_list_lock);
756}
Mingming Cao08d03502009-01-14 16:19:32 +0100757EXPORT_SYMBOL(dqput);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758
Jan Kara7d9056b2008-11-25 15:31:32 +0100759struct dquot *dquot_alloc(struct super_block *sb, int type)
Jan Kara74f783a2008-08-19 14:51:22 +0200760{
761 return kmem_cache_zalloc(dquot_cachep, GFP_NOFS);
762}
Jan Kara7d9056b2008-11-25 15:31:32 +0100763EXPORT_SYMBOL(dquot_alloc);
Jan Kara74f783a2008-08-19 14:51:22 +0200764
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765static struct dquot *get_empty_dquot(struct super_block *sb, int type)
766{
767 struct dquot *dquot;
768
Jan Kara74f783a2008-08-19 14:51:22 +0200769 dquot = sb->dq_op->alloc_dquot(sb, type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 if(!dquot)
Jan Karadd6f3c62009-01-26 16:01:43 +0100771 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772
Ingo Molnard3be9152006-03-23 03:00:29 -0800773 mutex_init(&dquot->dq_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 INIT_LIST_HEAD(&dquot->dq_free);
775 INIT_LIST_HEAD(&dquot->dq_inuse);
776 INIT_HLIST_NODE(&dquot->dq_hash);
777 INIT_LIST_HEAD(&dquot->dq_dirty);
Jan Kara6362e4d2006-03-23 03:00:17 -0800778 init_waitqueue_head(&dquot->dq_wait_unused);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 dquot->dq_sb = sb;
780 dquot->dq_type = type;
781 atomic_set(&dquot->dq_count, 1);
782
783 return dquot;
784}
785
786/*
787 * Get reference to dquot
Jan Karacc334122009-01-12 17:23:05 +0100788 *
789 * Locking is slightly tricky here. We are guarded from parallel quotaoff()
790 * destroying our dquot by:
791 * a) checking for quota flags under dq_list_lock and
792 * b) getting a reference to dquot before we release dq_list_lock
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 */
Jan Kara3d9ea252008-10-10 16:12:23 +0200794struct dquot *dqget(struct super_block *sb, unsigned int id, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795{
796 unsigned int hashent = hashfn(sb, id, type);
Jan Karadd6f3c62009-01-26 16:01:43 +0100797 struct dquot *dquot = NULL, *empty = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798
Jan Karaf55abc02008-08-20 17:50:32 +0200799 if (!sb_has_quota_active(sb, type))
Jan Karadd6f3c62009-01-26 16:01:43 +0100800 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801we_slept:
802 spin_lock(&dq_list_lock);
Jan Karacc334122009-01-12 17:23:05 +0100803 spin_lock(&dq_state_lock);
804 if (!sb_has_quota_active(sb, type)) {
805 spin_unlock(&dq_state_lock);
806 spin_unlock(&dq_list_lock);
807 goto out;
808 }
809 spin_unlock(&dq_state_lock);
810
Jan Karadd6f3c62009-01-26 16:01:43 +0100811 dquot = find_dquot(hashent, sb, id, type);
812 if (!dquot) {
813 if (!empty) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 spin_unlock(&dq_list_lock);
Jan Karadd6f3c62009-01-26 16:01:43 +0100815 empty = get_empty_dquot(sb, type);
816 if (!empty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 schedule(); /* Try to wait for a moment... */
818 goto we_slept;
819 }
820 dquot = empty;
Jan Karadd6f3c62009-01-26 16:01:43 +0100821 empty = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 dquot->dq_id = id;
823 /* all dquots go on the inuse_list */
824 put_inuse(dquot);
825 /* hash it first so it can be found */
826 insert_dquot_hash(dquot);
827 dqstats.lookups++;
828 spin_unlock(&dq_list_lock);
829 } else {
830 if (!atomic_read(&dquot->dq_count))
831 remove_free_dquot(dquot);
832 atomic_inc(&dquot->dq_count);
833 dqstats.cache_hits++;
834 dqstats.lookups++;
835 spin_unlock(&dq_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 }
Jan Kara268157b2009-01-27 15:47:22 +0100837 /* Wait for dq_lock - after this we know that either dquot_release() is
838 * already finished or it will be canceled due to dq_count > 1 test */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 wait_on_dquot(dquot);
Jan Kara268157b2009-01-27 15:47:22 +0100840 /* Read the dquot / allocate space in quota file */
841 if (!test_bit(DQ_ACTIVE_B, &dquot->dq_flags) &&
842 sb->dq_op->acquire_dquot(dquot) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 dqput(dquot);
Jan Karadd6f3c62009-01-26 16:01:43 +0100844 dquot = NULL;
Jan Karacc334122009-01-12 17:23:05 +0100845 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 }
847#ifdef __DQUOT_PARANOIA
Eric Sesterhenn8abf6a42006-04-02 13:36:13 +0200848 BUG_ON(!dquot->dq_sb); /* Has somebody invalidated entry under us? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849#endif
Jan Karacc334122009-01-12 17:23:05 +0100850out:
851 if (empty)
852 do_destroy_dquot(empty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853
854 return dquot;
855}
Mingming Cao08d03502009-01-14 16:19:32 +0100856EXPORT_SYMBOL(dqget);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857
858static int dqinit_needed(struct inode *inode, int type)
859{
860 int cnt;
861
862 if (IS_NOQUOTA(inode))
863 return 0;
864 if (type != -1)
Jan Karadd6f3c62009-01-26 16:01:43 +0100865 return !inode->i_dquot[type];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
Jan Karadd6f3c62009-01-26 16:01:43 +0100867 if (!inode->i_dquot[cnt])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 return 1;
869 return 0;
870}
871
Ingo Molnard3be9152006-03-23 03:00:29 -0800872/* This routine is guarded by dqonoff_mutex mutex */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873static void add_dquot_ref(struct super_block *sb, int type)
874{
Jan Kara941d2382008-02-06 01:37:36 -0800875 struct inode *inode, *old_inode = NULL;
Jan Kara0a5a9c72010-02-09 18:20:39 +0100876 int reserved = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877
Christoph Hellwigd003fb72007-02-12 00:51:58 -0800878 spin_lock(&inode_lock);
879 list_for_each_entry(inode, &sb->s_inodes, i_sb_list) {
Wu Fengguangb6fac632009-04-02 16:56:34 -0700880 if (inode->i_state & (I_FREEING|I_CLEAR|I_WILL_FREE|I_NEW))
Nick Pigginaabb8fd2009-03-11 13:17:36 -0700881 continue;
Jan Kara0a5a9c72010-02-09 18:20:39 +0100882 if (unlikely(inode_get_rsv_space(inode) > 0))
883 reserved = 1;
Christoph Hellwigd003fb72007-02-12 00:51:58 -0800884 if (!atomic_read(&inode->i_writecount))
885 continue;
886 if (!dqinit_needed(inode, type))
887 continue;
Christoph Hellwigd003fb72007-02-12 00:51:58 -0800888
889 __iget(inode);
890 spin_unlock(&inode_lock);
891
Jan Kara941d2382008-02-06 01:37:36 -0800892 iput(old_inode);
Christoph Hellwigd003fb72007-02-12 00:51:58 -0800893 sb->dq_op->initialize(inode, type);
Jan Kara941d2382008-02-06 01:37:36 -0800894 /* We hold a reference to 'inode' so it couldn't have been
895 * removed from s_inodes list while we dropped the inode_lock.
896 * We cannot iput the inode now as we can be holding the last
897 * reference and we cannot iput it under inode_lock. So we
898 * keep the reference and iput it later. */
899 old_inode = inode;
900 spin_lock(&inode_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 }
Christoph Hellwigd003fb72007-02-12 00:51:58 -0800902 spin_unlock(&inode_lock);
Jan Kara941d2382008-02-06 01:37:36 -0800903 iput(old_inode);
Jan Kara0a5a9c72010-02-09 18:20:39 +0100904
905 if (reserved) {
906 printk(KERN_WARNING "VFS (%s): Writes happened before quota"
907 " was turned on thus quota information is probably "
908 "inconsistent. Please run quotacheck(8).\n", sb->s_id);
909 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910}
911
Jan Kara268157b2009-01-27 15:47:22 +0100912/*
913 * Return 0 if dqput() won't block.
914 * (note that 1 doesn't necessarily mean blocking)
915 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916static inline int dqput_blocks(struct dquot *dquot)
917{
918 if (atomic_read(&dquot->dq_count) <= 1)
919 return 1;
920 return 0;
921}
922
Jan Kara268157b2009-01-27 15:47:22 +0100923/*
924 * Remove references to dquots from inode and add dquot to list for freeing
925 * if we have the last referece to dquot
926 * We can't race with anybody because we hold dqptr_sem for writing...
927 */
Adrian Bunke5f00f42007-05-08 00:27:22 -0700928static int remove_inode_dquot_ref(struct inode *inode, int type,
929 struct list_head *tofree_head)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930{
931 struct dquot *dquot = inode->i_dquot[type];
932
Jan Karadd6f3c62009-01-26 16:01:43 +0100933 inode->i_dquot[type] = NULL;
934 if (dquot) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 if (dqput_blocks(dquot)) {
936#ifdef __DQUOT_PARANOIA
937 if (atomic_read(&dquot->dq_count) != 1)
938 printk(KERN_WARNING "VFS: Adding dquot with dq_count %d to dispose list.\n", atomic_read(&dquot->dq_count));
939#endif
940 spin_lock(&dq_list_lock);
Jan Kara268157b2009-01-27 15:47:22 +0100941 /* As dquot must have currently users it can't be on
942 * the free list... */
943 list_add(&dquot->dq_free, tofree_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 spin_unlock(&dq_list_lock);
945 return 1;
946 }
947 else
948 dqput(dquot); /* We have guaranteed we won't block */
949 }
950 return 0;
951}
952
Jan Kara268157b2009-01-27 15:47:22 +0100953/*
954 * Free list of dquots
955 * Dquots are removed from inodes and no new references can be got so we are
956 * the only ones holding reference
957 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958static void put_dquot_list(struct list_head *tofree_head)
959{
960 struct list_head *act_head;
961 struct dquot *dquot;
962
963 act_head = tofree_head->next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 while (act_head != tofree_head) {
965 dquot = list_entry(act_head, struct dquot, dq_free);
966 act_head = act_head->next;
Jan Kara268157b2009-01-27 15:47:22 +0100967 /* Remove dquot from the list so we won't have problems... */
968 list_del_init(&dquot->dq_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 dqput(dquot);
970 }
971}
972
Christoph Hellwigfb58b732007-02-12 00:51:57 -0800973static void remove_dquot_ref(struct super_block *sb, int type,
974 struct list_head *tofree_head)
975{
976 struct inode *inode;
977
978 spin_lock(&inode_lock);
979 list_for_each_entry(inode, &sb->s_inodes, i_sb_list) {
Nick Pigginaabb8fd2009-03-11 13:17:36 -0700980 /*
981 * We have to scan also I_NEW inodes because they can already
982 * have quota pointer initialized. Luckily, we need to touch
983 * only quota pointers and these have separate locking
984 * (dqptr_sem).
985 */
Christoph Hellwigfb58b732007-02-12 00:51:57 -0800986 if (!IS_NOQUOTA(inode))
987 remove_inode_dquot_ref(inode, type, tofree_head);
988 }
989 spin_unlock(&inode_lock);
990}
991
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992/* Gather all references from inodes and drop them */
993static void drop_dquot_ref(struct super_block *sb, int type)
994{
995 LIST_HEAD(tofree_head);
996
Christoph Hellwigfb58b732007-02-12 00:51:57 -0800997 if (sb->dq_op) {
998 down_write(&sb_dqopt(sb)->dqptr_sem);
999 remove_dquot_ref(sb, type, &tofree_head);
1000 up_write(&sb_dqopt(sb)->dqptr_sem);
1001 put_dquot_list(&tofree_head);
1002 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003}
1004
Jan Kara12095462008-08-20 14:45:12 +02001005static inline void dquot_incr_inodes(struct dquot *dquot, qsize_t number)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006{
1007 dquot->dq_dqb.dqb_curinodes += number;
1008}
1009
1010static inline void dquot_incr_space(struct dquot *dquot, qsize_t number)
1011{
1012 dquot->dq_dqb.dqb_curspace += number;
1013}
1014
Mingming Caof18df222009-01-13 16:43:09 +01001015static inline void dquot_resv_space(struct dquot *dquot, qsize_t number)
1016{
1017 dquot->dq_dqb.dqb_rsvspace += number;
1018}
1019
Mingming Cao740d9dc2009-01-13 16:43:14 +01001020/*
1021 * Claim reserved quota space
1022 */
Jan Kara0a5a9c72010-02-09 18:20:39 +01001023static void dquot_claim_reserved_space(struct dquot *dquot, qsize_t number)
Mingming Cao740d9dc2009-01-13 16:43:14 +01001024{
Jan Kara0a5a9c72010-02-09 18:20:39 +01001025 if (dquot->dq_dqb.dqb_rsvspace < number) {
1026 WARN_ON_ONCE(1);
1027 number = dquot->dq_dqb.dqb_rsvspace;
1028 }
Mingming Cao740d9dc2009-01-13 16:43:14 +01001029 dquot->dq_dqb.dqb_curspace += number;
1030 dquot->dq_dqb.dqb_rsvspace -= number;
1031}
1032
1033static inline
1034void dquot_free_reserved_space(struct dquot *dquot, qsize_t number)
1035{
Jan Kara0a5a9c72010-02-09 18:20:39 +01001036 if (dquot->dq_dqb.dqb_rsvspace >= number)
1037 dquot->dq_dqb.dqb_rsvspace -= number;
1038 else {
1039 WARN_ON_ONCE(1);
1040 dquot->dq_dqb.dqb_rsvspace = 0;
1041 }
Mingming Cao740d9dc2009-01-13 16:43:14 +01001042}
1043
Jan Kara7a2435d2009-01-27 01:47:11 +01001044static void dquot_decr_inodes(struct dquot *dquot, qsize_t number)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045{
Jan Karadb49d2d2008-10-01 18:21:39 +02001046 if (sb_dqopt(dquot->dq_sb)->flags & DQUOT_NEGATIVE_USAGE ||
1047 dquot->dq_dqb.dqb_curinodes >= number)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 dquot->dq_dqb.dqb_curinodes -= number;
1049 else
1050 dquot->dq_dqb.dqb_curinodes = 0;
1051 if (dquot->dq_dqb.dqb_curinodes <= dquot->dq_dqb.dqb_isoftlimit)
1052 dquot->dq_dqb.dqb_itime = (time_t) 0;
1053 clear_bit(DQ_INODES_B, &dquot->dq_flags);
1054}
1055
Jan Kara7a2435d2009-01-27 01:47:11 +01001056static void dquot_decr_space(struct dquot *dquot, qsize_t number)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057{
Jan Karadb49d2d2008-10-01 18:21:39 +02001058 if (sb_dqopt(dquot->dq_sb)->flags & DQUOT_NEGATIVE_USAGE ||
1059 dquot->dq_dqb.dqb_curspace >= number)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 dquot->dq_dqb.dqb_curspace -= number;
1061 else
1062 dquot->dq_dqb.dqb_curspace = 0;
Jan Kara12095462008-08-20 14:45:12 +02001063 if (dquot->dq_dqb.dqb_curspace <= dquot->dq_dqb.dqb_bsoftlimit)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 dquot->dq_dqb.dqb_btime = (time_t) 0;
1065 clear_bit(DQ_BLKS_B, &dquot->dq_flags);
1066}
1067
Jan Karac5254602007-12-22 14:03:25 -08001068static int warning_issued(struct dquot *dquot, const int warntype)
1069{
1070 int flag = (warntype == QUOTA_NL_BHARDWARN ||
1071 warntype == QUOTA_NL_BSOFTLONGWARN) ? DQ_BLKS_B :
1072 ((warntype == QUOTA_NL_IHARDWARN ||
1073 warntype == QUOTA_NL_ISOFTLONGWARN) ? DQ_INODES_B : 0);
1074
1075 if (!flag)
1076 return 0;
1077 return test_and_set_bit(flag, &dquot->dq_flags);
1078}
1079
Jan Kara8e893462007-10-16 23:29:31 -07001080#ifdef CONFIG_PRINT_QUOTA_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081static int flag_print_warnings = 1;
1082
Jan Kara7a2435d2009-01-27 01:47:11 +01001083static int need_print_warning(struct dquot *dquot)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084{
1085 if (!flag_print_warnings)
1086 return 0;
1087
1088 switch (dquot->dq_type) {
1089 case USRQUOTA:
David Howellsda9592e2008-11-14 10:39:05 +11001090 return current_fsuid() == dquot->dq_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 case GRPQUOTA:
1092 return in_group_p(dquot->dq_id);
1093 }
1094 return 0;
1095}
1096
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097/* Print warning to user which exceeded quota */
Jan Karac5254602007-12-22 14:03:25 -08001098static void print_warning(struct dquot *dquot, const int warntype)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099{
1100 char *msg = NULL;
Peter Zijlstra24ec8392006-12-08 02:36:04 -08001101 struct tty_struct *tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102
Jan Kara657d3bf2008-07-25 01:46:52 -07001103 if (warntype == QUOTA_NL_IHARDBELOW ||
1104 warntype == QUOTA_NL_ISOFTBELOW ||
1105 warntype == QUOTA_NL_BHARDBELOW ||
1106 warntype == QUOTA_NL_BSOFTBELOW || !need_print_warning(dquot))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 return;
1108
Peter Zijlstra24ec8392006-12-08 02:36:04 -08001109 tty = get_current_tty();
1110 if (!tty)
Alan Cox452a00d2008-10-13 10:39:13 +01001111 return;
Peter Zijlstra24ec8392006-12-08 02:36:04 -08001112 tty_write_message(tty, dquot->dq_sb->s_id);
Jan Kara8e893462007-10-16 23:29:31 -07001113 if (warntype == QUOTA_NL_ISOFTWARN || warntype == QUOTA_NL_BSOFTWARN)
Peter Zijlstra24ec8392006-12-08 02:36:04 -08001114 tty_write_message(tty, ": warning, ");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 else
Peter Zijlstra24ec8392006-12-08 02:36:04 -08001116 tty_write_message(tty, ": write failed, ");
1117 tty_write_message(tty, quotatypes[dquot->dq_type]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 switch (warntype) {
Jan Kara8e893462007-10-16 23:29:31 -07001119 case QUOTA_NL_IHARDWARN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 msg = " file limit reached.\r\n";
1121 break;
Jan Kara8e893462007-10-16 23:29:31 -07001122 case QUOTA_NL_ISOFTLONGWARN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 msg = " file quota exceeded too long.\r\n";
1124 break;
Jan Kara8e893462007-10-16 23:29:31 -07001125 case QUOTA_NL_ISOFTWARN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 msg = " file quota exceeded.\r\n";
1127 break;
Jan Kara8e893462007-10-16 23:29:31 -07001128 case QUOTA_NL_BHARDWARN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 msg = " block limit reached.\r\n";
1130 break;
Jan Kara8e893462007-10-16 23:29:31 -07001131 case QUOTA_NL_BSOFTLONGWARN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 msg = " block quota exceeded too long.\r\n";
1133 break;
Jan Kara8e893462007-10-16 23:29:31 -07001134 case QUOTA_NL_BSOFTWARN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 msg = " block quota exceeded.\r\n";
1136 break;
1137 }
Peter Zijlstra24ec8392006-12-08 02:36:04 -08001138 tty_write_message(tty, msg);
Alan Cox452a00d2008-10-13 10:39:13 +01001139 tty_kref_put(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140}
Jan Kara8e893462007-10-16 23:29:31 -07001141#endif
1142
Mingming Caof18df222009-01-13 16:43:09 +01001143/*
1144 * Write warnings to the console and send warning messages over netlink.
1145 *
1146 * Note that this function can sleep.
1147 */
Jan Kara7a2435d2009-01-27 01:47:11 +01001148static void flush_warnings(struct dquot *const *dquots, char *warntype)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149{
Steven Whitehouse86e931a2009-09-28 12:35:17 +01001150 struct dquot *dq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 int i;
1152
Steven Whitehouse86e931a2009-09-28 12:35:17 +01001153 for (i = 0; i < MAXQUOTAS; i++) {
1154 dq = dquots[i];
1155 if (dq && warntype[i] != QUOTA_NL_NOWARN &&
1156 !warning_issued(dq, warntype[i])) {
Jan Kara8e893462007-10-16 23:29:31 -07001157#ifdef CONFIG_PRINT_QUOTA_WARNING
Steven Whitehouse86e931a2009-09-28 12:35:17 +01001158 print_warning(dq, warntype[i]);
Jan Kara8e893462007-10-16 23:29:31 -07001159#endif
Steven Whitehouse86e931a2009-09-28 12:35:17 +01001160 quota_send_warning(dq->dq_type, dq->dq_id,
1161 dq->dq_sb->s_dev, warntype[i]);
Jan Kara8e893462007-10-16 23:29:31 -07001162 }
Steven Whitehouse86e931a2009-09-28 12:35:17 +01001163 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164}
1165
Jan Kara7a2435d2009-01-27 01:47:11 +01001166static int ignore_hardlimit(struct dquot *dquot)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167{
1168 struct mem_dqinfo *info = &sb_dqopt(dquot->dq_sb)->info[dquot->dq_type];
1169
1170 return capable(CAP_SYS_RESOURCE) &&
Jan Kara268157b2009-01-27 15:47:22 +01001171 (info->dqi_format->qf_fmt_id != QFMT_VFS_OLD ||
1172 !(info->dqi_flags & V1_DQF_RSQUASH));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173}
1174
1175/* needs dq_data_lock */
Jan Kara12095462008-08-20 14:45:12 +02001176static int check_idq(struct dquot *dquot, qsize_t inodes, char *warntype)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177{
Jan Kara268157b2009-01-27 15:47:22 +01001178 qsize_t newinodes = dquot->dq_dqb.dqb_curinodes + inodes;
1179
Jan Kara8e893462007-10-16 23:29:31 -07001180 *warntype = QUOTA_NL_NOWARN;
Jan Karaf55abc02008-08-20 17:50:32 +02001181 if (!sb_has_quota_limits_enabled(dquot->dq_sb, dquot->dq_type) ||
1182 test_bit(DQ_FAKE_B, &dquot->dq_flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 return QUOTA_OK;
1184
1185 if (dquot->dq_dqb.dqb_ihardlimit &&
Jan Kara268157b2009-01-27 15:47:22 +01001186 newinodes > dquot->dq_dqb.dqb_ihardlimit &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 !ignore_hardlimit(dquot)) {
Jan Kara8e893462007-10-16 23:29:31 -07001188 *warntype = QUOTA_NL_IHARDWARN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 return NO_QUOTA;
1190 }
1191
1192 if (dquot->dq_dqb.dqb_isoftlimit &&
Jan Kara268157b2009-01-27 15:47:22 +01001193 newinodes > dquot->dq_dqb.dqb_isoftlimit &&
1194 dquot->dq_dqb.dqb_itime &&
1195 get_seconds() >= dquot->dq_dqb.dqb_itime &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 !ignore_hardlimit(dquot)) {
Jan Kara8e893462007-10-16 23:29:31 -07001197 *warntype = QUOTA_NL_ISOFTLONGWARN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 return NO_QUOTA;
1199 }
1200
1201 if (dquot->dq_dqb.dqb_isoftlimit &&
Jan Kara268157b2009-01-27 15:47:22 +01001202 newinodes > dquot->dq_dqb.dqb_isoftlimit &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 dquot->dq_dqb.dqb_itime == 0) {
Jan Kara8e893462007-10-16 23:29:31 -07001204 *warntype = QUOTA_NL_ISOFTWARN;
Jan Kara268157b2009-01-27 15:47:22 +01001205 dquot->dq_dqb.dqb_itime = get_seconds() +
1206 sb_dqopt(dquot->dq_sb)->info[dquot->dq_type].dqi_igrace;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 }
1208
1209 return QUOTA_OK;
1210}
1211
1212/* needs dq_data_lock */
1213static int check_bdq(struct dquot *dquot, qsize_t space, int prealloc, char *warntype)
1214{
Mingming Caof18df222009-01-13 16:43:09 +01001215 qsize_t tspace;
Jan Kara268157b2009-01-27 15:47:22 +01001216 struct super_block *sb = dquot->dq_sb;
Mingming Caof18df222009-01-13 16:43:09 +01001217
Jan Kara8e893462007-10-16 23:29:31 -07001218 *warntype = QUOTA_NL_NOWARN;
Jan Kara268157b2009-01-27 15:47:22 +01001219 if (!sb_has_quota_limits_enabled(sb, dquot->dq_type) ||
Jan Karaf55abc02008-08-20 17:50:32 +02001220 test_bit(DQ_FAKE_B, &dquot->dq_flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 return QUOTA_OK;
1222
Mingming Caof18df222009-01-13 16:43:09 +01001223 tspace = dquot->dq_dqb.dqb_curspace + dquot->dq_dqb.dqb_rsvspace
1224 + space;
1225
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 if (dquot->dq_dqb.dqb_bhardlimit &&
Mingming Caof18df222009-01-13 16:43:09 +01001227 tspace > dquot->dq_dqb.dqb_bhardlimit &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 !ignore_hardlimit(dquot)) {
1229 if (!prealloc)
Jan Kara8e893462007-10-16 23:29:31 -07001230 *warntype = QUOTA_NL_BHARDWARN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 return NO_QUOTA;
1232 }
1233
1234 if (dquot->dq_dqb.dqb_bsoftlimit &&
Mingming Caof18df222009-01-13 16:43:09 +01001235 tspace > dquot->dq_dqb.dqb_bsoftlimit &&
Jan Kara268157b2009-01-27 15:47:22 +01001236 dquot->dq_dqb.dqb_btime &&
1237 get_seconds() >= dquot->dq_dqb.dqb_btime &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 !ignore_hardlimit(dquot)) {
1239 if (!prealloc)
Jan Kara8e893462007-10-16 23:29:31 -07001240 *warntype = QUOTA_NL_BSOFTLONGWARN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 return NO_QUOTA;
1242 }
1243
1244 if (dquot->dq_dqb.dqb_bsoftlimit &&
Mingming Caof18df222009-01-13 16:43:09 +01001245 tspace > dquot->dq_dqb.dqb_bsoftlimit &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 dquot->dq_dqb.dqb_btime == 0) {
1247 if (!prealloc) {
Jan Kara8e893462007-10-16 23:29:31 -07001248 *warntype = QUOTA_NL_BSOFTWARN;
Jan Kara268157b2009-01-27 15:47:22 +01001249 dquot->dq_dqb.dqb_btime = get_seconds() +
1250 sb_dqopt(sb)->info[dquot->dq_type].dqi_bgrace;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 }
1252 else
1253 /*
1254 * We don't allow preallocation to exceed softlimit so exceeding will
1255 * be always printed
1256 */
1257 return NO_QUOTA;
1258 }
1259
1260 return QUOTA_OK;
1261}
1262
Jan Kara12095462008-08-20 14:45:12 +02001263static int info_idq_free(struct dquot *dquot, qsize_t inodes)
Jan Kara657d3bf2008-07-25 01:46:52 -07001264{
Jan Kara268157b2009-01-27 15:47:22 +01001265 qsize_t newinodes;
1266
Jan Kara657d3bf2008-07-25 01:46:52 -07001267 if (test_bit(DQ_FAKE_B, &dquot->dq_flags) ||
Jan Karaf55abc02008-08-20 17:50:32 +02001268 dquot->dq_dqb.dqb_curinodes <= dquot->dq_dqb.dqb_isoftlimit ||
1269 !sb_has_quota_limits_enabled(dquot->dq_sb, dquot->dq_type))
Jan Kara657d3bf2008-07-25 01:46:52 -07001270 return QUOTA_NL_NOWARN;
1271
Jan Kara268157b2009-01-27 15:47:22 +01001272 newinodes = dquot->dq_dqb.dqb_curinodes - inodes;
1273 if (newinodes <= dquot->dq_dqb.dqb_isoftlimit)
Jan Kara657d3bf2008-07-25 01:46:52 -07001274 return QUOTA_NL_ISOFTBELOW;
1275 if (dquot->dq_dqb.dqb_curinodes >= dquot->dq_dqb.dqb_ihardlimit &&
Jan Kara268157b2009-01-27 15:47:22 +01001276 newinodes < dquot->dq_dqb.dqb_ihardlimit)
Jan Kara657d3bf2008-07-25 01:46:52 -07001277 return QUOTA_NL_IHARDBELOW;
1278 return QUOTA_NL_NOWARN;
1279}
1280
1281static int info_bdq_free(struct dquot *dquot, qsize_t space)
1282{
1283 if (test_bit(DQ_FAKE_B, &dquot->dq_flags) ||
Jan Kara12095462008-08-20 14:45:12 +02001284 dquot->dq_dqb.dqb_curspace <= dquot->dq_dqb.dqb_bsoftlimit)
Jan Kara657d3bf2008-07-25 01:46:52 -07001285 return QUOTA_NL_NOWARN;
1286
Jan Kara12095462008-08-20 14:45:12 +02001287 if (dquot->dq_dqb.dqb_curspace - space <= dquot->dq_dqb.dqb_bsoftlimit)
Jan Kara657d3bf2008-07-25 01:46:52 -07001288 return QUOTA_NL_BSOFTBELOW;
Jan Kara12095462008-08-20 14:45:12 +02001289 if (dquot->dq_dqb.dqb_curspace >= dquot->dq_dqb.dqb_bhardlimit &&
1290 dquot->dq_dqb.dqb_curspace - space < dquot->dq_dqb.dqb_bhardlimit)
Jan Kara657d3bf2008-07-25 01:46:52 -07001291 return QUOTA_NL_BHARDBELOW;
1292 return QUOTA_NL_NOWARN;
1293}
Jan Kara0a5a9c72010-02-09 18:20:39 +01001294
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295/*
1296 * Initialize quota pointers in inode
Jan Karacc334122009-01-12 17:23:05 +01001297 * We do things in a bit complicated way but by that we avoid calling
1298 * dqget() and thus filesystem callbacks under dqptr_sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 */
1300int dquot_initialize(struct inode *inode, int type)
1301{
1302 unsigned int id = 0;
1303 int cnt, ret = 0;
Dmitry Monakhovad1e6e82010-02-16 08:31:49 +03001304 struct dquot *got[MAXQUOTAS];
Jan Karacc334122009-01-12 17:23:05 +01001305 struct super_block *sb = inode->i_sb;
Jan Kara0a5a9c72010-02-09 18:20:39 +01001306 qsize_t rsv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307
Ingo Molnard3be9152006-03-23 03:00:29 -08001308 /* First test before acquiring mutex - solves deadlocks when we
1309 * re-enter the quota code and are already holding the mutex */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 if (IS_NOQUOTA(inode))
1311 return 0;
Jan Karacc334122009-01-12 17:23:05 +01001312
1313 /* First get references to structures we might need. */
1314 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
Dmitry Monakhovad1e6e82010-02-16 08:31:49 +03001315 got[cnt] = NULL;
Jan Karacc334122009-01-12 17:23:05 +01001316 if (type != -1 && cnt != type)
1317 continue;
1318 switch (cnt) {
1319 case USRQUOTA:
1320 id = inode->i_uid;
1321 break;
1322 case GRPQUOTA:
1323 id = inode->i_gid;
1324 break;
1325 }
1326 got[cnt] = dqget(sb, id, cnt);
1327 }
1328
1329 down_write(&sb_dqopt(sb)->dqptr_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 if (IS_NOQUOTA(inode))
1331 goto out_err;
1332 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1333 if (type != -1 && cnt != type)
1334 continue;
Jan Karacc334122009-01-12 17:23:05 +01001335 /* Avoid races with quotaoff() */
1336 if (!sb_has_quota_active(sb, cnt))
1337 continue;
Jan Karadd6f3c62009-01-26 16:01:43 +01001338 if (!inode->i_dquot[cnt]) {
Jan Karacc334122009-01-12 17:23:05 +01001339 inode->i_dquot[cnt] = got[cnt];
Jan Karadd6f3c62009-01-26 16:01:43 +01001340 got[cnt] = NULL;
Jan Kara0a5a9c72010-02-09 18:20:39 +01001341 /*
1342 * Make quota reservation system happy if someone
1343 * did a write before quota was turned on
1344 */
1345 rsv = inode_get_rsv_space(inode);
1346 if (unlikely(rsv))
1347 dquot_resv_space(inode->i_dquot[cnt], rsv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 }
1349 }
1350out_err:
Jan Karacc334122009-01-12 17:23:05 +01001351 up_write(&sb_dqopt(sb)->dqptr_sem);
1352 /* Drop unused references */
Dmitry Monakhovdc52dd32009-12-14 15:21:15 +03001353 dqput_all(got);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 return ret;
1355}
Mingming Cao08d03502009-01-14 16:19:32 +01001356EXPORT_SYMBOL(dquot_initialize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357
1358/*
1359 * Release all quotas referenced by inode
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 */
Jan Kara3d9ea252008-10-10 16:12:23 +02001361int dquot_drop(struct inode *inode)
1362{
Jan Karacc334122009-01-12 17:23:05 +01001363 int cnt;
1364 struct dquot *put[MAXQUOTAS];
1365
Jan Kara3d9ea252008-10-10 16:12:23 +02001366 down_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
Jan Karacc334122009-01-12 17:23:05 +01001367 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1368 put[cnt] = inode->i_dquot[cnt];
Jan Karadd6f3c62009-01-26 16:01:43 +01001369 inode->i_dquot[cnt] = NULL;
Jan Karacc334122009-01-12 17:23:05 +01001370 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 up_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
Dmitry Monakhovdc52dd32009-12-14 15:21:15 +03001372 dqput_all(put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373 return 0;
1374}
Mingming Cao08d03502009-01-14 16:19:32 +01001375EXPORT_SYMBOL(dquot_drop);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376
Jan Karab85f4b82008-07-25 01:46:50 -07001377/* Wrapper to remove references to quota structures from inode */
1378void vfs_dq_drop(struct inode *inode)
1379{
1380 /* Here we can get arbitrary inode from clear_inode() so we have
1381 * to be careful. OTOH we don't need locking as quota operations
1382 * are allowed to change only at mount time */
1383 if (!IS_NOQUOTA(inode) && inode->i_sb && inode->i_sb->dq_op
1384 && inode->i_sb->dq_op->drop) {
1385 int cnt;
1386 /* Test before calling to rule out calls from proc and such
1387 * where we are not allowed to block. Note that this is
1388 * actually reliable test even without the lock - the caller
1389 * must assure that nobody can come after the DQUOT_DROP and
1390 * add quota pointers back anyway */
1391 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
Jan Karadd6f3c62009-01-26 16:01:43 +01001392 if (inode->i_dquot[cnt])
Jan Karab85f4b82008-07-25 01:46:50 -07001393 break;
1394 if (cnt < MAXQUOTAS)
1395 inode->i_sb->dq_op->drop(inode);
1396 }
1397}
Mingming Cao08d03502009-01-14 16:19:32 +01001398EXPORT_SYMBOL(vfs_dq_drop);
Jan Karab85f4b82008-07-25 01:46:50 -07001399
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400/*
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001401 * inode_reserved_space is managed internally by quota, and protected by
1402 * i_lock similar to i_blocks+i_bytes.
1403 */
1404static qsize_t *inode_reserved_space(struct inode * inode)
1405{
1406 /* Filesystem must explicitly define it's own method in order to use
1407 * quota reservation interface */
1408 BUG_ON(!inode->i_sb->dq_op->get_reserved_space);
1409 return inode->i_sb->dq_op->get_reserved_space(inode);
1410}
1411
Dmitry Monakhovc4690702010-02-09 17:53:36 +01001412void inode_add_rsv_space(struct inode *inode, qsize_t number)
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001413{
1414 spin_lock(&inode->i_lock);
1415 *inode_reserved_space(inode) += number;
1416 spin_unlock(&inode->i_lock);
1417}
Dmitry Monakhovc4690702010-02-09 17:53:36 +01001418EXPORT_SYMBOL(inode_add_rsv_space);
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001419
Dmitry Monakhovc4690702010-02-09 17:53:36 +01001420void inode_claim_rsv_space(struct inode *inode, qsize_t number)
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001421{
1422 spin_lock(&inode->i_lock);
1423 *inode_reserved_space(inode) -= number;
1424 __inode_add_bytes(inode, number);
1425 spin_unlock(&inode->i_lock);
1426}
Dmitry Monakhovc4690702010-02-09 17:53:36 +01001427EXPORT_SYMBOL(inode_claim_rsv_space);
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001428
Dmitry Monakhovc4690702010-02-09 17:53:36 +01001429void inode_sub_rsv_space(struct inode *inode, qsize_t number)
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001430{
1431 spin_lock(&inode->i_lock);
1432 *inode_reserved_space(inode) -= number;
1433 spin_unlock(&inode->i_lock);
1434}
Dmitry Monakhovc4690702010-02-09 17:53:36 +01001435EXPORT_SYMBOL(inode_sub_rsv_space);
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001436
1437static qsize_t inode_get_rsv_space(struct inode *inode)
1438{
1439 qsize_t ret;
Jan Kara05b5d892010-01-06 18:03:36 +01001440
1441 if (!inode->i_sb->dq_op->get_reserved_space)
1442 return 0;
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001443 spin_lock(&inode->i_lock);
1444 ret = *inode_reserved_space(inode);
1445 spin_unlock(&inode->i_lock);
1446 return ret;
1447}
1448
1449static void inode_incr_space(struct inode *inode, qsize_t number,
1450 int reserve)
1451{
1452 if (reserve)
1453 inode_add_rsv_space(inode, number);
1454 else
1455 inode_add_bytes(inode, number);
1456}
1457
1458static void inode_decr_space(struct inode *inode, qsize_t number, int reserve)
1459{
1460 if (reserve)
1461 inode_sub_rsv_space(inode, number);
1462 else
1463 inode_sub_bytes(inode, number);
1464}
1465
1466/*
Christoph Hellwig5dd40562010-03-03 09:05:00 -05001467 * This functions updates i_blocks+i_bytes fields and quota information
1468 * (together with appropriate checks).
1469 *
1470 * NOTE: We absolutely rely on the fact that caller dirties the inode
1471 * (usually helpers in quotaops.h care about this) and holds a handle for
1472 * the current transaction so that dquot write and inode write go into the
1473 * same transaction.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 */
1475
1476/*
1477 * This operation can block, but only after everything is updated
1478 */
Mingming Caof18df222009-01-13 16:43:09 +01001479int __dquot_alloc_space(struct inode *inode, qsize_t number,
Christoph Hellwig5dd40562010-03-03 09:05:00 -05001480 int warn, int reserve)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481{
Christoph Hellwig5dd40562010-03-03 09:05:00 -05001482 int cnt, ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483 char warntype[MAXQUOTAS];
1484
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001485 /*
1486 * First test before acquiring mutex - solves deadlocks when we
1487 * re-enter the quota code and are already holding the mutex
1488 */
Christoph Hellwig5dd40562010-03-03 09:05:00 -05001489 if (!sb_any_quota_active(inode->i_sb) || IS_NOQUOTA(inode)) {
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001490 inode_incr_space(inode, number, reserve);
1491 goto out;
1492 }
1493
1494 down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
Jan Kara8e893462007-10-16 23:29:31 -07001496 warntype[cnt] = QUOTA_NL_NOWARN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498 spin_lock(&dq_data_lock);
1499 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
Jan Karadd6f3c62009-01-26 16:01:43 +01001500 if (!inode->i_dquot[cnt])
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501 continue;
Christoph Hellwig5dd40562010-03-03 09:05:00 -05001502 if (check_bdq(inode->i_dquot[cnt], number, !warn, warntype+cnt)
1503 == NO_QUOTA) {
1504 ret = -EDQUOT;
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001505 spin_unlock(&dq_data_lock);
1506 goto out_flush_warn;
Mingming Caof18df222009-01-13 16:43:09 +01001507 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508 }
1509 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
Jan Karadd6f3c62009-01-26 16:01:43 +01001510 if (!inode->i_dquot[cnt])
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 continue;
Mingming Caof18df222009-01-13 16:43:09 +01001512 if (reserve)
1513 dquot_resv_space(inode->i_dquot[cnt], number);
1514 else
1515 dquot_incr_space(inode->i_dquot[cnt], number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516 }
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001517 inode_incr_space(inode, number, reserve);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518 spin_unlock(&dq_data_lock);
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001519
1520 if (reserve)
1521 goto out_flush_warn;
Dmitry Monakhovdc52dd32009-12-14 15:21:15 +03001522 mark_all_dquot_dirty(inode->i_dquot);
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001523out_flush_warn:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524 flush_warnings(inode->i_dquot, warntype);
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001525 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1526out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527 return ret;
1528}
Christoph Hellwig5dd40562010-03-03 09:05:00 -05001529EXPORT_SYMBOL(__dquot_alloc_space);
Mingming Caof18df222009-01-13 16:43:09 +01001530
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531/*
1532 * This operation can block, but only after everything is updated
1533 */
Christoph Hellwig63936dd2010-03-03 09:05:01 -05001534int dquot_alloc_inode(const struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535{
Christoph Hellwig63936dd2010-03-03 09:05:01 -05001536 int cnt, ret = -EDQUOT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537 char warntype[MAXQUOTAS];
1538
Ingo Molnard3be9152006-03-23 03:00:29 -08001539 /* First test before acquiring mutex - solves deadlocks when we
1540 * re-enter the quota code and are already holding the mutex */
Christoph Hellwig63936dd2010-03-03 09:05:01 -05001541 if (!sb_any_quota_active(inode->i_sb) || IS_NOQUOTA(inode))
1542 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
Jan Kara8e893462007-10-16 23:29:31 -07001544 warntype[cnt] = QUOTA_NL_NOWARN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545 down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546 spin_lock(&dq_data_lock);
1547 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
Jan Karadd6f3c62009-01-26 16:01:43 +01001548 if (!inode->i_dquot[cnt])
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549 continue;
Christoph Hellwig63936dd2010-03-03 09:05:01 -05001550 if (check_idq(inode->i_dquot[cnt], 1, warntype+cnt)
Jan Kara268157b2009-01-27 15:47:22 +01001551 == NO_QUOTA)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552 goto warn_put_all;
1553 }
1554
1555 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
Jan Karadd6f3c62009-01-26 16:01:43 +01001556 if (!inode->i_dquot[cnt])
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557 continue;
Christoph Hellwig63936dd2010-03-03 09:05:01 -05001558 dquot_incr_inodes(inode->i_dquot[cnt], 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559 }
Christoph Hellwig63936dd2010-03-03 09:05:01 -05001560 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561warn_put_all:
1562 spin_unlock(&dq_data_lock);
Christoph Hellwig63936dd2010-03-03 09:05:01 -05001563 if (ret == 0)
Dmitry Monakhovdc52dd32009-12-14 15:21:15 +03001564 mark_all_dquot_dirty(inode->i_dquot);
Jan Kara087ee8d2007-12-17 16:20:26 -08001565 flush_warnings(inode->i_dquot, warntype);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1567 return ret;
1568}
Mingming Cao08d03502009-01-14 16:19:32 +01001569EXPORT_SYMBOL(dquot_alloc_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570
Christoph Hellwig5dd40562010-03-03 09:05:00 -05001571/*
1572 * Convert in-memory reserved quotas to real consumed quotas
1573 */
1574int dquot_claim_space_nodirty(struct inode *inode, qsize_t number)
Mingming Cao740d9dc2009-01-13 16:43:14 +01001575{
1576 int cnt;
Mingming Cao740d9dc2009-01-13 16:43:14 +01001577
Christoph Hellwig5dd40562010-03-03 09:05:00 -05001578 if (!sb_any_quota_active(inode->i_sb) || IS_NOQUOTA(inode)) {
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001579 inode_claim_rsv_space(inode, number);
Christoph Hellwig5dd40562010-03-03 09:05:00 -05001580 return 0;
Mingming Cao740d9dc2009-01-13 16:43:14 +01001581 }
1582
1583 down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
Mingming Cao740d9dc2009-01-13 16:43:14 +01001584 spin_lock(&dq_data_lock);
1585 /* Claim reserved quotas to allocated quotas */
1586 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
Jan Karadd6f3c62009-01-26 16:01:43 +01001587 if (inode->i_dquot[cnt])
Mingming Cao740d9dc2009-01-13 16:43:14 +01001588 dquot_claim_reserved_space(inode->i_dquot[cnt],
1589 number);
1590 }
1591 /* Update inode bytes */
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001592 inode_claim_rsv_space(inode, number);
Mingming Cao740d9dc2009-01-13 16:43:14 +01001593 spin_unlock(&dq_data_lock);
Dmitry Monakhovdc52dd32009-12-14 15:21:15 +03001594 mark_all_dquot_dirty(inode->i_dquot);
Mingming Cao740d9dc2009-01-13 16:43:14 +01001595 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
Christoph Hellwig5dd40562010-03-03 09:05:00 -05001596 return 0;
Mingming Cao740d9dc2009-01-13 16:43:14 +01001597}
Christoph Hellwig5dd40562010-03-03 09:05:00 -05001598EXPORT_SYMBOL(dquot_claim_space_nodirty);
Mingming Cao740d9dc2009-01-13 16:43:14 +01001599
1600/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601 * This operation can block, but only after everything is updated
1602 */
Christoph Hellwig5dd40562010-03-03 09:05:00 -05001603void __dquot_free_space(struct inode *inode, qsize_t number, int reserve)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604{
1605 unsigned int cnt;
Jan Kara657d3bf2008-07-25 01:46:52 -07001606 char warntype[MAXQUOTAS];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607
Ingo Molnard3be9152006-03-23 03:00:29 -08001608 /* First test before acquiring mutex - solves deadlocks when we
1609 * re-enter the quota code and are already holding the mutex */
Christoph Hellwig5dd40562010-03-03 09:05:00 -05001610 if (!sb_any_quota_active(inode->i_sb) || IS_NOQUOTA(inode)) {
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001611 inode_decr_space(inode, number, reserve);
Christoph Hellwig5dd40562010-03-03 09:05:00 -05001612 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613 }
Jan Kara657d3bf2008-07-25 01:46:52 -07001614
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615 down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616 spin_lock(&dq_data_lock);
1617 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
Jan Karadd6f3c62009-01-26 16:01:43 +01001618 if (!inode->i_dquot[cnt])
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619 continue;
Jan Kara657d3bf2008-07-25 01:46:52 -07001620 warntype[cnt] = info_bdq_free(inode->i_dquot[cnt], number);
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001621 if (reserve)
1622 dquot_free_reserved_space(inode->i_dquot[cnt], number);
1623 else
1624 dquot_decr_space(inode->i_dquot[cnt], number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625 }
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001626 inode_decr_space(inode, number, reserve);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627 spin_unlock(&dq_data_lock);
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001628
1629 if (reserve)
1630 goto out_unlock;
Dmitry Monakhovdc52dd32009-12-14 15:21:15 +03001631 mark_all_dquot_dirty(inode->i_dquot);
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001632out_unlock:
Jan Kara657d3bf2008-07-25 01:46:52 -07001633 flush_warnings(inode->i_dquot, warntype);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635}
Christoph Hellwig5dd40562010-03-03 09:05:00 -05001636EXPORT_SYMBOL(__dquot_free_space);
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001637
1638/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639 * This operation can block, but only after everything is updated
1640 */
Christoph Hellwig63936dd2010-03-03 09:05:01 -05001641void dquot_free_inode(const struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642{
1643 unsigned int cnt;
Jan Kara657d3bf2008-07-25 01:46:52 -07001644 char warntype[MAXQUOTAS];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645
Ingo Molnard3be9152006-03-23 03:00:29 -08001646 /* First test before acquiring mutex - solves deadlocks when we
1647 * re-enter the quota code and are already holding the mutex */
Christoph Hellwig63936dd2010-03-03 09:05:01 -05001648 if (!sb_any_quota_active(inode->i_sb) || IS_NOQUOTA(inode))
1649 return;
Jan Kara657d3bf2008-07-25 01:46:52 -07001650
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651 down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652 spin_lock(&dq_data_lock);
1653 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
Jan Karadd6f3c62009-01-26 16:01:43 +01001654 if (!inode->i_dquot[cnt])
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655 continue;
Christoph Hellwig63936dd2010-03-03 09:05:01 -05001656 warntype[cnt] = info_idq_free(inode->i_dquot[cnt], 1);
1657 dquot_decr_inodes(inode->i_dquot[cnt], 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658 }
1659 spin_unlock(&dq_data_lock);
Dmitry Monakhovdc52dd32009-12-14 15:21:15 +03001660 mark_all_dquot_dirty(inode->i_dquot);
Jan Kara657d3bf2008-07-25 01:46:52 -07001661 flush_warnings(inode->i_dquot, warntype);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663}
Mingming Cao08d03502009-01-14 16:19:32 +01001664EXPORT_SYMBOL(dquot_free_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665
1666/*
1667 * Transfer the number of inode and blocks from one diskquota to an other.
1668 *
1669 * This operation can block, but only after everything is updated
1670 * A transaction must be started when entering this function.
1671 */
Dmitry Monakhov8ddd69d2010-02-16 08:31:50 +03001672int dquot_transfer(struct inode *inode, qid_t *chid, unsigned long mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673{
Mingming Cao740d9dc2009-01-13 16:43:14 +01001674 qsize_t space, cur_space;
1675 qsize_t rsv_space = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676 struct dquot *transfer_from[MAXQUOTAS];
1677 struct dquot *transfer_to[MAXQUOTAS];
Jan Karacc334122009-01-12 17:23:05 +01001678 int cnt, ret = QUOTA_OK;
Jan Kara657d3bf2008-07-25 01:46:52 -07001679 char warntype_to[MAXQUOTAS];
1680 char warntype_from_inodes[MAXQUOTAS], warntype_from_space[MAXQUOTAS];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681
Ingo Molnard3be9152006-03-23 03:00:29 -08001682 /* First test before acquiring mutex - solves deadlocks when we
1683 * re-enter the quota code and are already holding the mutex */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684 if (IS_NOQUOTA(inode))
1685 return QUOTA_OK;
Jan Karacc334122009-01-12 17:23:05 +01001686 /* Initialize the arrays */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
Jan Karadd6f3c62009-01-26 16:01:43 +01001688 transfer_from[cnt] = NULL;
1689 transfer_to[cnt] = NULL;
Jan Kara657d3bf2008-07-25 01:46:52 -07001690 warntype_to[cnt] = QUOTA_NL_NOWARN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691 }
Dmitry Monakhov8ddd69d2010-02-16 08:31:50 +03001692 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1693 if (mask & (1 << cnt))
1694 transfer_to[cnt] = dqget(inode->i_sb, chid[cnt], cnt);
1695 }
Jan Karacc334122009-01-12 17:23:05 +01001696 down_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
Jan Karacc334122009-01-12 17:23:05 +01001697 if (IS_NOQUOTA(inode)) { /* File without quota accounting? */
1698 up_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
1699 goto put_all;
1700 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701 spin_lock(&dq_data_lock);
Mingming Cao740d9dc2009-01-13 16:43:14 +01001702 cur_space = inode_get_bytes(inode);
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001703 rsv_space = inode_get_rsv_space(inode);
Mingming Cao740d9dc2009-01-13 16:43:14 +01001704 space = cur_space + rsv_space;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705 /* Build the transfer_from list and check the limits */
1706 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
Jan Karadd6f3c62009-01-26 16:01:43 +01001707 if (!transfer_to[cnt])
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708 continue;
1709 transfer_from[cnt] = inode->i_dquot[cnt];
Jan Kara657d3bf2008-07-25 01:46:52 -07001710 if (check_idq(transfer_to[cnt], 1, warntype_to + cnt) ==
1711 NO_QUOTA || check_bdq(transfer_to[cnt], space, 0,
1712 warntype_to + cnt) == NO_QUOTA)
Jan Karacc334122009-01-12 17:23:05 +01001713 goto over_quota;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714 }
1715
1716 /*
1717 * Finally perform the needed transfer from transfer_from to transfer_to
1718 */
1719 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1720 /*
1721 * Skip changes for same uid or gid or for turned off quota-type.
1722 */
Jan Karadd6f3c62009-01-26 16:01:43 +01001723 if (!transfer_to[cnt])
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724 continue;
1725
1726 /* Due to IO error we might not have transfer_from[] structure */
1727 if (transfer_from[cnt]) {
Jan Kara657d3bf2008-07-25 01:46:52 -07001728 warntype_from_inodes[cnt] =
1729 info_idq_free(transfer_from[cnt], 1);
1730 warntype_from_space[cnt] =
1731 info_bdq_free(transfer_from[cnt], space);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732 dquot_decr_inodes(transfer_from[cnt], 1);
Mingming Cao740d9dc2009-01-13 16:43:14 +01001733 dquot_decr_space(transfer_from[cnt], cur_space);
1734 dquot_free_reserved_space(transfer_from[cnt],
1735 rsv_space);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736 }
1737
1738 dquot_incr_inodes(transfer_to[cnt], 1);
Mingming Cao740d9dc2009-01-13 16:43:14 +01001739 dquot_incr_space(transfer_to[cnt], cur_space);
1740 dquot_resv_space(transfer_to[cnt], rsv_space);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741
1742 inode->i_dquot[cnt] = transfer_to[cnt];
1743 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744 spin_unlock(&dq_data_lock);
Jan Karacc334122009-01-12 17:23:05 +01001745 up_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
1746
Dmitry Monakhovdc52dd32009-12-14 15:21:15 +03001747 mark_all_dquot_dirty(transfer_from);
1748 mark_all_dquot_dirty(transfer_to);
1749 /* The reference we got is transferred to the inode */
1750 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1751 transfer_to[cnt] = NULL;
Jan Karacc334122009-01-12 17:23:05 +01001752warn_put_all:
Jan Kara657d3bf2008-07-25 01:46:52 -07001753 flush_warnings(transfer_to, warntype_to);
1754 flush_warnings(transfer_from, warntype_from_inodes);
1755 flush_warnings(transfer_from, warntype_from_space);
Jan Karacc334122009-01-12 17:23:05 +01001756put_all:
Dmitry Monakhovdc52dd32009-12-14 15:21:15 +03001757 dqput_all(transfer_from);
1758 dqput_all(transfer_to);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759 return ret;
Jan Karacc334122009-01-12 17:23:05 +01001760over_quota:
1761 spin_unlock(&dq_data_lock);
1762 up_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
1763 /* Clear dquot pointers we don't want to dqput() */
1764 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
Jan Karadd6f3c62009-01-26 16:01:43 +01001765 transfer_from[cnt] = NULL;
Jan Karacc334122009-01-12 17:23:05 +01001766 ret = NO_QUOTA;
1767 goto warn_put_all;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768}
Mingming Cao08d03502009-01-14 16:19:32 +01001769EXPORT_SYMBOL(dquot_transfer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770
Dmitry Monakhov8ddd69d2010-02-16 08:31:50 +03001771/* Wrapper for transferring ownership of an inode for uid/gid only
1772 * Called from FSXXX_setattr()
1773 */
Jan Karab85f4b82008-07-25 01:46:50 -07001774int vfs_dq_transfer(struct inode *inode, struct iattr *iattr)
1775{
Dmitry Monakhov8ddd69d2010-02-16 08:31:50 +03001776 qid_t chid[MAXQUOTAS];
1777 unsigned long mask = 0;
1778
1779 if (iattr->ia_valid & ATTR_UID && iattr->ia_uid != inode->i_uid) {
1780 mask |= 1 << USRQUOTA;
1781 chid[USRQUOTA] = iattr->ia_uid;
1782 }
1783 if (iattr->ia_valid & ATTR_GID && iattr->ia_gid != inode->i_gid) {
1784 mask |= 1 << GRPQUOTA;
1785 chid[GRPQUOTA] = iattr->ia_gid;
1786 }
Jan Karaf55abc02008-08-20 17:50:32 +02001787 if (sb_any_quota_active(inode->i_sb) && !IS_NOQUOTA(inode)) {
Jan Karab85f4b82008-07-25 01:46:50 -07001788 vfs_dq_init(inode);
Dmitry Monakhov8ddd69d2010-02-16 08:31:50 +03001789 if (inode->i_sb->dq_op->transfer(inode, chid, mask) == NO_QUOTA)
Jan Karab85f4b82008-07-25 01:46:50 -07001790 return 1;
1791 }
1792 return 0;
1793}
Mingming Cao08d03502009-01-14 16:19:32 +01001794EXPORT_SYMBOL(vfs_dq_transfer);
Jan Karab85f4b82008-07-25 01:46:50 -07001795
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796/*
1797 * Write info of quota file to disk
1798 */
1799int dquot_commit_info(struct super_block *sb, int type)
1800{
1801 int ret;
1802 struct quota_info *dqopt = sb_dqopt(sb);
1803
Ingo Molnard3be9152006-03-23 03:00:29 -08001804 mutex_lock(&dqopt->dqio_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805 ret = dqopt->ops[type]->write_file_info(sb, type);
Ingo Molnard3be9152006-03-23 03:00:29 -08001806 mutex_unlock(&dqopt->dqio_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807 return ret;
1808}
Mingming Cao08d03502009-01-14 16:19:32 +01001809EXPORT_SYMBOL(dquot_commit_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810
1811/*
1812 * Definitions of diskquota operations.
1813 */
Alexey Dobriyan61e225d2009-09-21 17:01:08 -07001814const struct dquot_operations dquot_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815 .initialize = dquot_initialize,
1816 .drop = dquot_drop,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817 .transfer = dquot_transfer,
1818 .write_dquot = dquot_commit,
1819 .acquire_dquot = dquot_acquire,
1820 .release_dquot = dquot_release,
1821 .mark_dirty = dquot_mark_dquot_dirty,
Jan Kara74f783a2008-08-19 14:51:22 +02001822 .write_info = dquot_commit_info,
1823 .alloc_dquot = dquot_alloc,
1824 .destroy_dquot = dquot_destroy,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825};
1826
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827/*
1828 * Turn quota off on a device. type == -1 ==> quotaoff for all types (umount)
1829 */
Jan Karaf55abc02008-08-20 17:50:32 +02001830int vfs_quota_disable(struct super_block *sb, int type, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831{
Jan Kara0ff5af82008-04-28 02:14:33 -07001832 int cnt, ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833 struct quota_info *dqopt = sb_dqopt(sb);
1834 struct inode *toputinode[MAXQUOTAS];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835
Jan Karaf55abc02008-08-20 17:50:32 +02001836 /* Cannot turn off usage accounting without turning off limits, or
1837 * suspend quotas and simultaneously turn quotas off. */
1838 if ((flags & DQUOT_USAGE_ENABLED && !(flags & DQUOT_LIMITS_ENABLED))
1839 || (flags & DQUOT_SUSPENDED && flags & (DQUOT_LIMITS_ENABLED |
1840 DQUOT_USAGE_ENABLED)))
1841 return -EINVAL;
1842
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843 /* We need to serialize quota_off() for device */
Ingo Molnard3be9152006-03-23 03:00:29 -08001844 mutex_lock(&dqopt->dqonoff_mutex);
Jan Kara9377abd2008-05-12 14:02:08 -07001845
1846 /*
1847 * Skip everything if there's nothing to do. We have to do this because
1848 * sometimes we are called when fill_super() failed and calling
1849 * sync_fs() in such cases does no good.
1850 */
Jan Karaf55abc02008-08-20 17:50:32 +02001851 if (!sb_any_quota_loaded(sb)) {
Jan Kara9377abd2008-05-12 14:02:08 -07001852 mutex_unlock(&dqopt->dqonoff_mutex);
1853 return 0;
1854 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1856 toputinode[cnt] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857 if (type != -1 && cnt != type)
1858 continue;
Jan Karaf55abc02008-08-20 17:50:32 +02001859 if (!sb_has_quota_loaded(sb, cnt))
Jan Kara0ff5af82008-04-28 02:14:33 -07001860 continue;
Jan Karaf55abc02008-08-20 17:50:32 +02001861
1862 if (flags & DQUOT_SUSPENDED) {
Jan Karacc334122009-01-12 17:23:05 +01001863 spin_lock(&dq_state_lock);
Jan Karaf55abc02008-08-20 17:50:32 +02001864 dqopt->flags |=
1865 dquot_state_flag(DQUOT_SUSPENDED, cnt);
Jan Karacc334122009-01-12 17:23:05 +01001866 spin_unlock(&dq_state_lock);
Jan Karaf55abc02008-08-20 17:50:32 +02001867 } else {
Jan Karacc334122009-01-12 17:23:05 +01001868 spin_lock(&dq_state_lock);
Jan Karaf55abc02008-08-20 17:50:32 +02001869 dqopt->flags &= ~dquot_state_flag(flags, cnt);
1870 /* Turning off suspended quotas? */
1871 if (!sb_has_quota_loaded(sb, cnt) &&
1872 sb_has_quota_suspended(sb, cnt)) {
1873 dqopt->flags &= ~dquot_state_flag(
1874 DQUOT_SUSPENDED, cnt);
Jan Karacc334122009-01-12 17:23:05 +01001875 spin_unlock(&dq_state_lock);
Jan Karaf55abc02008-08-20 17:50:32 +02001876 iput(dqopt->files[cnt]);
1877 dqopt->files[cnt] = NULL;
1878 continue;
1879 }
Jan Karacc334122009-01-12 17:23:05 +01001880 spin_unlock(&dq_state_lock);
Jan Kara0ff5af82008-04-28 02:14:33 -07001881 }
Jan Karaf55abc02008-08-20 17:50:32 +02001882
1883 /* We still have to keep quota loaded? */
1884 if (sb_has_quota_loaded(sb, cnt) && !(flags & DQUOT_SUSPENDED))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886
1887 /* Note: these are blocking operations */
1888 drop_dquot_ref(sb, cnt);
1889 invalidate_dquots(sb, cnt);
1890 /*
Jan Kara268157b2009-01-27 15:47:22 +01001891 * Now all dquots should be invalidated, all writes done so we
1892 * should be only users of the info. No locks needed.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893 */
1894 if (info_dirty(&dqopt->info[cnt]))
1895 sb->dq_op->write_info(sb, cnt);
1896 if (dqopt->ops[cnt]->free_file_info)
1897 dqopt->ops[cnt]->free_file_info(sb, cnt);
1898 put_quota_format(dqopt->info[cnt].dqi_format);
1899
1900 toputinode[cnt] = dqopt->files[cnt];
Jan Karaf55abc02008-08-20 17:50:32 +02001901 if (!sb_has_quota_loaded(sb, cnt))
Jan Kara0ff5af82008-04-28 02:14:33 -07001902 dqopt->files[cnt] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903 dqopt->info[cnt].dqi_flags = 0;
1904 dqopt->info[cnt].dqi_igrace = 0;
1905 dqopt->info[cnt].dqi_bgrace = 0;
1906 dqopt->ops[cnt] = NULL;
1907 }
Ingo Molnard3be9152006-03-23 03:00:29 -08001908 mutex_unlock(&dqopt->dqonoff_mutex);
Jan Karaca785ec2008-09-30 17:53:37 +02001909
1910 /* Skip syncing and setting flags if quota files are hidden */
1911 if (dqopt->flags & DQUOT_QUOTA_SYS_FILE)
1912 goto put_inodes;
1913
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914 /* Sync the superblock so that buffers with quota data are written to
Al Viro7b7b1ac2005-11-07 17:13:39 -05001915 * disk (and so userspace sees correct data afterwards). */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916 if (sb->s_op->sync_fs)
1917 sb->s_op->sync_fs(sb, 1);
1918 sync_blockdev(sb->s_bdev);
1919 /* Now the quota files are just ordinary files and we can set the
1920 * inode flags back. Moreover we discard the pagecache so that
1921 * userspace sees the writes we did bypassing the pagecache. We
1922 * must also discard the blockdev buffers so that we see the
1923 * changes done by userspace on the next quotaon() */
1924 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1925 if (toputinode[cnt]) {
Ingo Molnard3be9152006-03-23 03:00:29 -08001926 mutex_lock(&dqopt->dqonoff_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927 /* If quota was reenabled in the meantime, we have
1928 * nothing to do */
Jan Karaf55abc02008-08-20 17:50:32 +02001929 if (!sb_has_quota_loaded(sb, cnt)) {
Jan Kara268157b2009-01-27 15:47:22 +01001930 mutex_lock_nested(&toputinode[cnt]->i_mutex,
1931 I_MUTEX_QUOTA);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932 toputinode[cnt]->i_flags &= ~(S_IMMUTABLE |
1933 S_NOATIME | S_NOQUOTA);
Jan Kara268157b2009-01-27 15:47:22 +01001934 truncate_inode_pages(&toputinode[cnt]->i_data,
1935 0);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001936 mutex_unlock(&toputinode[cnt]->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937 mark_inode_dirty(toputinode[cnt]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938 }
Ingo Molnard3be9152006-03-23 03:00:29 -08001939 mutex_unlock(&dqopt->dqonoff_mutex);
Jan Karaca785ec2008-09-30 17:53:37 +02001940 }
1941 if (sb->s_bdev)
1942 invalidate_bdev(sb->s_bdev);
1943put_inodes:
1944 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1945 if (toputinode[cnt]) {
Jan Kara0ff5af82008-04-28 02:14:33 -07001946 /* On remount RO, we keep the inode pointer so that we
Jan Karaf55abc02008-08-20 17:50:32 +02001947 * can reenable quota on the subsequent remount RW. We
1948 * have to check 'flags' variable and not use sb_has_
1949 * function because another quotaon / quotaoff could
1950 * change global state before we got here. We refuse
1951 * to suspend quotas when there is pending delete on
1952 * the quota file... */
1953 if (!(flags & DQUOT_SUSPENDED))
Jan Kara0ff5af82008-04-28 02:14:33 -07001954 iput(toputinode[cnt]);
1955 else if (!toputinode[cnt]->i_nlink)
1956 ret = -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957 }
Jan Kara0ff5af82008-04-28 02:14:33 -07001958 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959}
Mingming Cao08d03502009-01-14 16:19:32 +01001960EXPORT_SYMBOL(vfs_quota_disable);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961
Jan Karaf55abc02008-08-20 17:50:32 +02001962int vfs_quota_off(struct super_block *sb, int type, int remount)
1963{
1964 return vfs_quota_disable(sb, type, remount ? DQUOT_SUSPENDED :
1965 (DQUOT_USAGE_ENABLED | DQUOT_LIMITS_ENABLED));
1966}
Mingming Cao08d03502009-01-14 16:19:32 +01001967EXPORT_SYMBOL(vfs_quota_off);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968/*
1969 * Turn quotas on on a device
1970 */
1971
Jan Karaf55abc02008-08-20 17:50:32 +02001972/*
1973 * Helper function to turn quotas on when we already have the inode of
1974 * quota file and no quota information is loaded.
1975 */
1976static int vfs_load_quota_inode(struct inode *inode, int type, int format_id,
1977 unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978{
1979 struct quota_format_type *fmt = find_quota_format(format_id);
1980 struct super_block *sb = inode->i_sb;
1981 struct quota_info *dqopt = sb_dqopt(sb);
1982 int error;
1983 int oldflags = -1;
1984
1985 if (!fmt)
1986 return -ESRCH;
1987 if (!S_ISREG(inode->i_mode)) {
1988 error = -EACCES;
1989 goto out_fmt;
1990 }
1991 if (IS_RDONLY(inode)) {
1992 error = -EROFS;
1993 goto out_fmt;
1994 }
1995 if (!sb->s_op->quota_write || !sb->s_op->quota_read) {
1996 error = -EINVAL;
1997 goto out_fmt;
1998 }
Jan Karaf55abc02008-08-20 17:50:32 +02001999 /* Usage always has to be set... */
2000 if (!(flags & DQUOT_USAGE_ENABLED)) {
2001 error = -EINVAL;
2002 goto out_fmt;
2003 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004
Jan Karaca785ec2008-09-30 17:53:37 +02002005 if (!(dqopt->flags & DQUOT_QUOTA_SYS_FILE)) {
Jan Karaab94c392010-02-22 21:07:17 +01002006 /* As we bypass the pagecache we must now flush all the
2007 * dirty data and invalidate caches so that kernel sees
2008 * changes from userspace. It is not enough to just flush
2009 * the quota file since if blocksize < pagesize, invalidation
2010 * of the cache could fail because of other unrelated dirty
2011 * data */
2012 sync_filesystem(sb);
Jan Karaca785ec2008-09-30 17:53:37 +02002013 invalidate_bdev(sb->s_bdev);
2014 }
Ingo Molnard3be9152006-03-23 03:00:29 -08002015 mutex_lock(&dqopt->dqonoff_mutex);
Jan Karaf55abc02008-08-20 17:50:32 +02002016 if (sb_has_quota_loaded(sb, type)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017 error = -EBUSY;
2018 goto out_lock;
2019 }
Jan Karaca785ec2008-09-30 17:53:37 +02002020
2021 if (!(dqopt->flags & DQUOT_QUOTA_SYS_FILE)) {
2022 /* We don't want quota and atime on quota files (deadlocks
2023 * possible) Also nobody should write to the file - we use
2024 * special IO operations which ignore the immutable bit. */
Jan Karadee86562009-07-22 18:12:17 +02002025 mutex_lock_nested(&inode->i_mutex, I_MUTEX_QUOTA);
Jan Kara268157b2009-01-27 15:47:22 +01002026 oldflags = inode->i_flags & (S_NOATIME | S_IMMUTABLE |
2027 S_NOQUOTA);
Jan Karaca785ec2008-09-30 17:53:37 +02002028 inode->i_flags |= S_NOQUOTA | S_NOATIME | S_IMMUTABLE;
Jan Karadee86562009-07-22 18:12:17 +02002029 mutex_unlock(&inode->i_mutex);
Jan Kara26245c92010-01-06 17:20:35 +01002030 /*
2031 * When S_NOQUOTA is set, remove dquot references as no more
2032 * references can be added
2033 */
Jan Karaca785ec2008-09-30 17:53:37 +02002034 sb->dq_op->drop(inode);
2035 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036
2037 error = -EIO;
2038 dqopt->files[type] = igrab(inode);
2039 if (!dqopt->files[type])
2040 goto out_lock;
2041 error = -EINVAL;
2042 if (!fmt->qf_ops->check_quota_file(sb, type))
2043 goto out_file_init;
2044
2045 dqopt->ops[type] = fmt->qf_ops;
2046 dqopt->info[type].dqi_format = fmt;
Jan Kara0ff5af82008-04-28 02:14:33 -07002047 dqopt->info[type].dqi_fmt_id = format_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048 INIT_LIST_HEAD(&dqopt->info[type].dqi_dirty_list);
Ingo Molnard3be9152006-03-23 03:00:29 -08002049 mutex_lock(&dqopt->dqio_mutex);
Jan Kara268157b2009-01-27 15:47:22 +01002050 error = dqopt->ops[type]->read_file_info(sb, type);
2051 if (error < 0) {
Ingo Molnard3be9152006-03-23 03:00:29 -08002052 mutex_unlock(&dqopt->dqio_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053 goto out_file_init;
2054 }
Ingo Molnard3be9152006-03-23 03:00:29 -08002055 mutex_unlock(&dqopt->dqio_mutex);
Jan Karacc334122009-01-12 17:23:05 +01002056 spin_lock(&dq_state_lock);
Jan Karaf55abc02008-08-20 17:50:32 +02002057 dqopt->flags |= dquot_state_flag(flags, type);
Jan Karacc334122009-01-12 17:23:05 +01002058 spin_unlock(&dq_state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059
2060 add_dquot_ref(sb, type);
Ingo Molnard3be9152006-03-23 03:00:29 -08002061 mutex_unlock(&dqopt->dqonoff_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062
2063 return 0;
2064
2065out_file_init:
2066 dqopt->files[type] = NULL;
2067 iput(inode);
2068out_lock:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069 if (oldflags != -1) {
Jan Karadee86562009-07-22 18:12:17 +02002070 mutex_lock_nested(&inode->i_mutex, I_MUTEX_QUOTA);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071 /* Set the flags back (in the case of accidental quotaon()
2072 * on a wrong file we don't want to mess up the flags) */
2073 inode->i_flags &= ~(S_NOATIME | S_NOQUOTA | S_IMMUTABLE);
2074 inode->i_flags |= oldflags;
Jan Karadee86562009-07-22 18:12:17 +02002075 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076 }
Jiaying Zhangd01730d2009-07-07 18:15:21 +02002077 mutex_unlock(&dqopt->dqonoff_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078out_fmt:
2079 put_quota_format(fmt);
2080
2081 return error;
2082}
2083
Jan Kara0ff5af82008-04-28 02:14:33 -07002084/* Reenable quotas on remount RW */
2085static int vfs_quota_on_remount(struct super_block *sb, int type)
2086{
2087 struct quota_info *dqopt = sb_dqopt(sb);
2088 struct inode *inode;
2089 int ret;
Jan Karaf55abc02008-08-20 17:50:32 +02002090 unsigned int flags;
Jan Kara0ff5af82008-04-28 02:14:33 -07002091
2092 mutex_lock(&dqopt->dqonoff_mutex);
2093 if (!sb_has_quota_suspended(sb, type)) {
2094 mutex_unlock(&dqopt->dqonoff_mutex);
2095 return 0;
2096 }
Jan Kara0ff5af82008-04-28 02:14:33 -07002097 inode = dqopt->files[type];
2098 dqopt->files[type] = NULL;
Jan Karacc334122009-01-12 17:23:05 +01002099 spin_lock(&dq_state_lock);
Jan Karaf55abc02008-08-20 17:50:32 +02002100 flags = dqopt->flags & dquot_state_flag(DQUOT_USAGE_ENABLED |
2101 DQUOT_LIMITS_ENABLED, type);
2102 dqopt->flags &= ~dquot_state_flag(DQUOT_STATE_FLAGS, type);
Jan Karacc334122009-01-12 17:23:05 +01002103 spin_unlock(&dq_state_lock);
Jan Kara0ff5af82008-04-28 02:14:33 -07002104 mutex_unlock(&dqopt->dqonoff_mutex);
2105
Jan Karaf55abc02008-08-20 17:50:32 +02002106 flags = dquot_generic_flag(flags, type);
2107 ret = vfs_load_quota_inode(inode, type, dqopt->info[type].dqi_fmt_id,
2108 flags);
Jan Kara0ff5af82008-04-28 02:14:33 -07002109 iput(inode);
2110
2111 return ret;
2112}
2113
Al Viro77e69da2008-08-01 04:29:18 -04002114int vfs_quota_on_path(struct super_block *sb, int type, int format_id,
2115 struct path *path)
2116{
2117 int error = security_quota_on(path->dentry);
2118 if (error)
2119 return error;
2120 /* Quota file not on the same filesystem? */
2121 if (path->mnt->mnt_sb != sb)
2122 error = -EXDEV;
2123 else
Jan Karaf55abc02008-08-20 17:50:32 +02002124 error = vfs_load_quota_inode(path->dentry->d_inode, type,
2125 format_id, DQUOT_USAGE_ENABLED |
2126 DQUOT_LIMITS_ENABLED);
Al Viro77e69da2008-08-01 04:29:18 -04002127 return error;
2128}
Mingming Cao08d03502009-01-14 16:19:32 +01002129EXPORT_SYMBOL(vfs_quota_on_path);
Al Viro77e69da2008-08-01 04:29:18 -04002130
Al Viro82646132008-08-02 00:57:06 -04002131int vfs_quota_on(struct super_block *sb, int type, int format_id, char *name,
Jan Kara0ff5af82008-04-28 02:14:33 -07002132 int remount)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133{
Al Viro82646132008-08-02 00:57:06 -04002134 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135 int error;
2136
Jan Kara0ff5af82008-04-28 02:14:33 -07002137 if (remount)
2138 return vfs_quota_on_remount(sb, type);
2139
Al Viro82646132008-08-02 00:57:06 -04002140 error = kern_path(name, LOOKUP_FOLLOW, &path);
Al Viro77e69da2008-08-01 04:29:18 -04002141 if (!error) {
Al Viro82646132008-08-02 00:57:06 -04002142 error = vfs_quota_on_path(sb, type, format_id, &path);
2143 path_put(&path);
Al Viro77e69da2008-08-01 04:29:18 -04002144 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145 return error;
2146}
Mingming Cao08d03502009-01-14 16:19:32 +01002147EXPORT_SYMBOL(vfs_quota_on);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148
2149/*
Jan Karaf55abc02008-08-20 17:50:32 +02002150 * More powerful function for turning on quotas allowing setting
2151 * of individual quota flags
2152 */
2153int vfs_quota_enable(struct inode *inode, int type, int format_id,
2154 unsigned int flags)
2155{
2156 int ret = 0;
2157 struct super_block *sb = inode->i_sb;
2158 struct quota_info *dqopt = sb_dqopt(sb);
2159
2160 /* Just unsuspend quotas? */
2161 if (flags & DQUOT_SUSPENDED)
2162 return vfs_quota_on_remount(sb, type);
2163 if (!flags)
2164 return 0;
2165 /* Just updating flags needed? */
2166 if (sb_has_quota_loaded(sb, type)) {
2167 mutex_lock(&dqopt->dqonoff_mutex);
2168 /* Now do a reliable test... */
2169 if (!sb_has_quota_loaded(sb, type)) {
2170 mutex_unlock(&dqopt->dqonoff_mutex);
2171 goto load_quota;
2172 }
2173 if (flags & DQUOT_USAGE_ENABLED &&
2174 sb_has_quota_usage_enabled(sb, type)) {
2175 ret = -EBUSY;
2176 goto out_lock;
2177 }
2178 if (flags & DQUOT_LIMITS_ENABLED &&
2179 sb_has_quota_limits_enabled(sb, type)) {
2180 ret = -EBUSY;
2181 goto out_lock;
2182 }
Jan Karacc334122009-01-12 17:23:05 +01002183 spin_lock(&dq_state_lock);
Jan Karaf55abc02008-08-20 17:50:32 +02002184 sb_dqopt(sb)->flags |= dquot_state_flag(flags, type);
Jan Karacc334122009-01-12 17:23:05 +01002185 spin_unlock(&dq_state_lock);
Jan Karaf55abc02008-08-20 17:50:32 +02002186out_lock:
2187 mutex_unlock(&dqopt->dqonoff_mutex);
2188 return ret;
2189 }
2190
2191load_quota:
2192 return vfs_load_quota_inode(inode, type, format_id, flags);
2193}
Mingming Cao08d03502009-01-14 16:19:32 +01002194EXPORT_SYMBOL(vfs_quota_enable);
Jan Karaf55abc02008-08-20 17:50:32 +02002195
2196/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197 * This function is used when filesystem needs to initialize quotas
2198 * during mount time.
2199 */
Christoph Hellwig84de8562005-06-23 00:09:16 -07002200int vfs_quota_on_mount(struct super_block *sb, char *qf_name,
2201 int format_id, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202{
Christoph Hellwig84de8562005-06-23 00:09:16 -07002203 struct dentry *dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204 int error;
2205
Jan Karac56818d2009-11-12 15:42:08 +01002206 mutex_lock(&sb->s_root->d_inode->i_mutex);
Christoph Hellwig2fa389c2005-06-23 00:09:16 -07002207 dentry = lookup_one_len(qf_name, sb->s_root, strlen(qf_name));
Jan Karac56818d2009-11-12 15:42:08 +01002208 mutex_unlock(&sb->s_root->d_inode->i_mutex);
Christoph Hellwig84de8562005-06-23 00:09:16 -07002209 if (IS_ERR(dentry))
2210 return PTR_ERR(dentry);
2211
Jan Kara154f4842005-11-28 13:44:14 -08002212 if (!dentry->d_inode) {
2213 error = -ENOENT;
2214 goto out;
2215 }
2216
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217 error = security_quota_on(dentry);
Christoph Hellwig84de8562005-06-23 00:09:16 -07002218 if (!error)
Jan Karaf55abc02008-08-20 17:50:32 +02002219 error = vfs_load_quota_inode(dentry->d_inode, type, format_id,
2220 DQUOT_USAGE_ENABLED | DQUOT_LIMITS_ENABLED);
Christoph Hellwig84de8562005-06-23 00:09:16 -07002221
Jan Kara154f4842005-11-28 13:44:14 -08002222out:
Christoph Hellwig84de8562005-06-23 00:09:16 -07002223 dput(dentry);
2224 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225}
Mingming Cao08d03502009-01-14 16:19:32 +01002226EXPORT_SYMBOL(vfs_quota_on_mount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227
Jan Karab85f4b82008-07-25 01:46:50 -07002228/* Wrapper to turn on quotas when remounting rw */
2229int vfs_dq_quota_on_remount(struct super_block *sb)
2230{
2231 int cnt;
2232 int ret = 0, err;
2233
2234 if (!sb->s_qcop || !sb->s_qcop->quota_on)
2235 return -ENOSYS;
2236 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
2237 err = sb->s_qcop->quota_on(sb, cnt, 0, NULL, 1);
2238 if (err < 0 && !ret)
2239 ret = err;
2240 }
2241 return ret;
2242}
Mingming Cao08d03502009-01-14 16:19:32 +01002243EXPORT_SYMBOL(vfs_dq_quota_on_remount);
Jan Karab85f4b82008-07-25 01:46:50 -07002244
Jan Kara12095462008-08-20 14:45:12 +02002245static inline qsize_t qbtos(qsize_t blocks)
2246{
2247 return blocks << QIF_DQBLKSIZE_BITS;
2248}
2249
2250static inline qsize_t stoqb(qsize_t space)
2251{
2252 return (space + QIF_DQBLKSIZE - 1) >> QIF_DQBLKSIZE_BITS;
2253}
2254
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255/* Generic routine for getting common part of quota structure */
2256static void do_get_dqblk(struct dquot *dquot, struct if_dqblk *di)
2257{
2258 struct mem_dqblk *dm = &dquot->dq_dqb;
2259
2260 spin_lock(&dq_data_lock);
Jan Kara12095462008-08-20 14:45:12 +02002261 di->dqb_bhardlimit = stoqb(dm->dqb_bhardlimit);
2262 di->dqb_bsoftlimit = stoqb(dm->dqb_bsoftlimit);
Mingming Caof18df222009-01-13 16:43:09 +01002263 di->dqb_curspace = dm->dqb_curspace + dm->dqb_rsvspace;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264 di->dqb_ihardlimit = dm->dqb_ihardlimit;
2265 di->dqb_isoftlimit = dm->dqb_isoftlimit;
2266 di->dqb_curinodes = dm->dqb_curinodes;
2267 di->dqb_btime = dm->dqb_btime;
2268 di->dqb_itime = dm->dqb_itime;
2269 di->dqb_valid = QIF_ALL;
2270 spin_unlock(&dq_data_lock);
2271}
2272
Jan Kara268157b2009-01-27 15:47:22 +01002273int vfs_get_dqblk(struct super_block *sb, int type, qid_t id,
2274 struct if_dqblk *di)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275{
2276 struct dquot *dquot;
2277
Jan Karacc334122009-01-12 17:23:05 +01002278 dquot = dqget(sb, id, type);
Jan Karadd6f3c62009-01-26 16:01:43 +01002279 if (!dquot)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280 return -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281 do_get_dqblk(dquot, di);
2282 dqput(dquot);
Jan Karacc334122009-01-12 17:23:05 +01002283
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284 return 0;
2285}
Mingming Cao08d03502009-01-14 16:19:32 +01002286EXPORT_SYMBOL(vfs_get_dqblk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287
2288/* Generic routine for setting common part of quota structure */
Andrew Perepechko338bf9a2008-04-28 02:14:31 -07002289static int do_set_dqblk(struct dquot *dquot, struct if_dqblk *di)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290{
2291 struct mem_dqblk *dm = &dquot->dq_dqb;
2292 int check_blim = 0, check_ilim = 0;
Andrew Perepechko338bf9a2008-04-28 02:14:31 -07002293 struct mem_dqinfo *dqi = &sb_dqopt(dquot->dq_sb)->info[dquot->dq_type];
2294
2295 if ((di->dqb_valid & QIF_BLIMITS &&
2296 (di->dqb_bhardlimit > dqi->dqi_maxblimit ||
2297 di->dqb_bsoftlimit > dqi->dqi_maxblimit)) ||
2298 (di->dqb_valid & QIF_ILIMITS &&
2299 (di->dqb_ihardlimit > dqi->dqi_maxilimit ||
2300 di->dqb_isoftlimit > dqi->dqi_maxilimit)))
2301 return -ERANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302
2303 spin_lock(&dq_data_lock);
2304 if (di->dqb_valid & QIF_SPACE) {
Mingming Caof18df222009-01-13 16:43:09 +01002305 dm->dqb_curspace = di->dqb_curspace - dm->dqb_rsvspace;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306 check_blim = 1;
Jan Kara4d59bce2008-10-02 16:48:10 +02002307 __set_bit(DQ_LASTSET_B + QIF_SPACE_B, &dquot->dq_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308 }
2309 if (di->dqb_valid & QIF_BLIMITS) {
Jan Kara12095462008-08-20 14:45:12 +02002310 dm->dqb_bsoftlimit = qbtos(di->dqb_bsoftlimit);
2311 dm->dqb_bhardlimit = qbtos(di->dqb_bhardlimit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312 check_blim = 1;
Jan Kara4d59bce2008-10-02 16:48:10 +02002313 __set_bit(DQ_LASTSET_B + QIF_BLIMITS_B, &dquot->dq_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314 }
2315 if (di->dqb_valid & QIF_INODES) {
2316 dm->dqb_curinodes = di->dqb_curinodes;
2317 check_ilim = 1;
Jan Kara4d59bce2008-10-02 16:48:10 +02002318 __set_bit(DQ_LASTSET_B + QIF_INODES_B, &dquot->dq_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319 }
2320 if (di->dqb_valid & QIF_ILIMITS) {
2321 dm->dqb_isoftlimit = di->dqb_isoftlimit;
2322 dm->dqb_ihardlimit = di->dqb_ihardlimit;
2323 check_ilim = 1;
Jan Kara4d59bce2008-10-02 16:48:10 +02002324 __set_bit(DQ_LASTSET_B + QIF_ILIMITS_B, &dquot->dq_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325 }
Jan Kara4d59bce2008-10-02 16:48:10 +02002326 if (di->dqb_valid & QIF_BTIME) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327 dm->dqb_btime = di->dqb_btime;
Jan Karae04a88a92009-01-07 18:07:29 -08002328 check_blim = 1;
Jan Kara4d59bce2008-10-02 16:48:10 +02002329 __set_bit(DQ_LASTSET_B + QIF_BTIME_B, &dquot->dq_flags);
2330 }
2331 if (di->dqb_valid & QIF_ITIME) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002332 dm->dqb_itime = di->dqb_itime;
Jan Karae04a88a92009-01-07 18:07:29 -08002333 check_ilim = 1;
Jan Kara4d59bce2008-10-02 16:48:10 +02002334 __set_bit(DQ_LASTSET_B + QIF_ITIME_B, &dquot->dq_flags);
2335 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336
2337 if (check_blim) {
Jan Kara268157b2009-01-27 15:47:22 +01002338 if (!dm->dqb_bsoftlimit ||
2339 dm->dqb_curspace < dm->dqb_bsoftlimit) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340 dm->dqb_btime = 0;
2341 clear_bit(DQ_BLKS_B, &dquot->dq_flags);
Jan Kara268157b2009-01-27 15:47:22 +01002342 } else if (!(di->dqb_valid & QIF_BTIME))
2343 /* Set grace only if user hasn't provided his own... */
Andrew Perepechko338bf9a2008-04-28 02:14:31 -07002344 dm->dqb_btime = get_seconds() + dqi->dqi_bgrace;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345 }
2346 if (check_ilim) {
Jan Kara268157b2009-01-27 15:47:22 +01002347 if (!dm->dqb_isoftlimit ||
2348 dm->dqb_curinodes < dm->dqb_isoftlimit) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349 dm->dqb_itime = 0;
2350 clear_bit(DQ_INODES_B, &dquot->dq_flags);
Jan Kara268157b2009-01-27 15:47:22 +01002351 } else if (!(di->dqb_valid & QIF_ITIME))
2352 /* Set grace only if user hasn't provided his own... */
Andrew Perepechko338bf9a2008-04-28 02:14:31 -07002353 dm->dqb_itime = get_seconds() + dqi->dqi_igrace;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002354 }
Jan Kara268157b2009-01-27 15:47:22 +01002355 if (dm->dqb_bhardlimit || dm->dqb_bsoftlimit || dm->dqb_ihardlimit ||
2356 dm->dqb_isoftlimit)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357 clear_bit(DQ_FAKE_B, &dquot->dq_flags);
2358 else
2359 set_bit(DQ_FAKE_B, &dquot->dq_flags);
2360 spin_unlock(&dq_data_lock);
2361 mark_dquot_dirty(dquot);
Andrew Perepechko338bf9a2008-04-28 02:14:31 -07002362
2363 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364}
2365
Jan Kara268157b2009-01-27 15:47:22 +01002366int vfs_set_dqblk(struct super_block *sb, int type, qid_t id,
2367 struct if_dqblk *di)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002368{
2369 struct dquot *dquot;
Andrew Perepechko338bf9a2008-04-28 02:14:31 -07002370 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371
Jan Karaf55abc02008-08-20 17:50:32 +02002372 dquot = dqget(sb, id, type);
2373 if (!dquot) {
2374 rc = -ESRCH;
2375 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376 }
Andrew Perepechko338bf9a2008-04-28 02:14:31 -07002377 rc = do_set_dqblk(dquot, di);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002378 dqput(dquot);
Jan Karaf55abc02008-08-20 17:50:32 +02002379out:
Andrew Perepechko338bf9a2008-04-28 02:14:31 -07002380 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381}
Mingming Cao08d03502009-01-14 16:19:32 +01002382EXPORT_SYMBOL(vfs_set_dqblk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002383
2384/* Generic routine for getting common part of quota file information */
2385int vfs_get_dqinfo(struct super_block *sb, int type, struct if_dqinfo *ii)
2386{
2387 struct mem_dqinfo *mi;
2388
Ingo Molnard3be9152006-03-23 03:00:29 -08002389 mutex_lock(&sb_dqopt(sb)->dqonoff_mutex);
Jan Karaf55abc02008-08-20 17:50:32 +02002390 if (!sb_has_quota_active(sb, type)) {
Ingo Molnard3be9152006-03-23 03:00:29 -08002391 mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392 return -ESRCH;
2393 }
2394 mi = sb_dqopt(sb)->info + type;
2395 spin_lock(&dq_data_lock);
2396 ii->dqi_bgrace = mi->dqi_bgrace;
2397 ii->dqi_igrace = mi->dqi_igrace;
2398 ii->dqi_flags = mi->dqi_flags & DQF_MASK;
2399 ii->dqi_valid = IIF_ALL;
2400 spin_unlock(&dq_data_lock);
Ingo Molnard3be9152006-03-23 03:00:29 -08002401 mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002402 return 0;
2403}
Mingming Cao08d03502009-01-14 16:19:32 +01002404EXPORT_SYMBOL(vfs_get_dqinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002405
2406/* Generic routine for setting common part of quota file information */
2407int vfs_set_dqinfo(struct super_block *sb, int type, struct if_dqinfo *ii)
2408{
2409 struct mem_dqinfo *mi;
Jan Karaf55abc02008-08-20 17:50:32 +02002410 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002411
Ingo Molnard3be9152006-03-23 03:00:29 -08002412 mutex_lock(&sb_dqopt(sb)->dqonoff_mutex);
Jan Karaf55abc02008-08-20 17:50:32 +02002413 if (!sb_has_quota_active(sb, type)) {
2414 err = -ESRCH;
2415 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002416 }
2417 mi = sb_dqopt(sb)->info + type;
2418 spin_lock(&dq_data_lock);
2419 if (ii->dqi_valid & IIF_BGRACE)
2420 mi->dqi_bgrace = ii->dqi_bgrace;
2421 if (ii->dqi_valid & IIF_IGRACE)
2422 mi->dqi_igrace = ii->dqi_igrace;
2423 if (ii->dqi_valid & IIF_FLAGS)
Jan Kara268157b2009-01-27 15:47:22 +01002424 mi->dqi_flags = (mi->dqi_flags & ~DQF_MASK) |
2425 (ii->dqi_flags & DQF_MASK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002426 spin_unlock(&dq_data_lock);
2427 mark_info_dirty(sb, type);
2428 /* Force write to disk */
2429 sb->dq_op->write_info(sb, type);
Jan Karaf55abc02008-08-20 17:50:32 +02002430out:
Ingo Molnard3be9152006-03-23 03:00:29 -08002431 mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
Jan Karaf55abc02008-08-20 17:50:32 +02002432 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002433}
Mingming Cao08d03502009-01-14 16:19:32 +01002434EXPORT_SYMBOL(vfs_set_dqinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002435
Alexey Dobriyan0d54b212009-09-21 17:01:09 -07002436const struct quotactl_ops vfs_quotactl_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002437 .quota_on = vfs_quota_on,
2438 .quota_off = vfs_quota_off,
2439 .quota_sync = vfs_quota_sync,
2440 .get_info = vfs_get_dqinfo,
2441 .set_info = vfs_set_dqinfo,
2442 .get_dqblk = vfs_get_dqblk,
2443 .set_dqblk = vfs_set_dqblk
2444};
2445
2446static ctl_table fs_dqstats_table[] = {
2447 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448 .procname = "lookups",
2449 .data = &dqstats.lookups,
2450 .maxlen = sizeof(int),
2451 .mode = 0444,
Eric W. Biederman6d456112009-11-16 03:11:48 -08002452 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453 },
2454 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002455 .procname = "drops",
2456 .data = &dqstats.drops,
2457 .maxlen = sizeof(int),
2458 .mode = 0444,
Eric W. Biederman6d456112009-11-16 03:11:48 -08002459 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002460 },
2461 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002462 .procname = "reads",
2463 .data = &dqstats.reads,
2464 .maxlen = sizeof(int),
2465 .mode = 0444,
Eric W. Biederman6d456112009-11-16 03:11:48 -08002466 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002467 },
2468 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002469 .procname = "writes",
2470 .data = &dqstats.writes,
2471 .maxlen = sizeof(int),
2472 .mode = 0444,
Eric W. Biederman6d456112009-11-16 03:11:48 -08002473 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002474 },
2475 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002476 .procname = "cache_hits",
2477 .data = &dqstats.cache_hits,
2478 .maxlen = sizeof(int),
2479 .mode = 0444,
Eric W. Biederman6d456112009-11-16 03:11:48 -08002480 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002481 },
2482 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002483 .procname = "allocated_dquots",
2484 .data = &dqstats.allocated_dquots,
2485 .maxlen = sizeof(int),
2486 .mode = 0444,
Eric W. Biederman6d456112009-11-16 03:11:48 -08002487 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002488 },
2489 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002490 .procname = "free_dquots",
2491 .data = &dqstats.free_dquots,
2492 .maxlen = sizeof(int),
2493 .mode = 0444,
Eric W. Biederman6d456112009-11-16 03:11:48 -08002494 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002495 },
2496 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002497 .procname = "syncs",
2498 .data = &dqstats.syncs,
2499 .maxlen = sizeof(int),
2500 .mode = 0444,
Eric W. Biederman6d456112009-11-16 03:11:48 -08002501 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002502 },
Jan Kara8e893462007-10-16 23:29:31 -07002503#ifdef CONFIG_PRINT_QUOTA_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07002504 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002505 .procname = "warnings",
2506 .data = &flag_print_warnings,
2507 .maxlen = sizeof(int),
2508 .mode = 0644,
Eric W. Biederman6d456112009-11-16 03:11:48 -08002509 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002510 },
Jan Kara8e893462007-10-16 23:29:31 -07002511#endif
Eric W. Biedermanab092032009-11-05 14:25:10 -08002512 { },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002513};
2514
2515static ctl_table fs_table[] = {
2516 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002517 .procname = "quota",
2518 .mode = 0555,
2519 .child = fs_dqstats_table,
2520 },
Eric W. Biedermanab092032009-11-05 14:25:10 -08002521 { },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002522};
2523
2524static ctl_table sys_table[] = {
2525 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002526 .procname = "fs",
2527 .mode = 0555,
2528 .child = fs_table,
2529 },
Eric W. Biedermanab092032009-11-05 14:25:10 -08002530 { },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002531};
2532
2533static int __init dquot_init(void)
2534{
2535 int i;
2536 unsigned long nr_hash, order;
2537
2538 printk(KERN_NOTICE "VFS: Disk quotas %s\n", __DQUOT_VERSION__);
2539
Eric W. Biederman0b4d4142007-02-14 00:34:09 -08002540 register_sysctl_table(sys_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002541
Paul Mundt20c2df82007-07-20 10:11:58 +09002542 dquot_cachep = kmem_cache_create("dquot",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002543 sizeof(struct dquot), sizeof(unsigned long) * 4,
Paul Jacksonfffb60f2006-03-24 03:16:06 -08002544 (SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT|
2545 SLAB_MEM_SPREAD|SLAB_PANIC),
Paul Mundt20c2df82007-07-20 10:11:58 +09002546 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002547
2548 order = 0;
2549 dquot_hash = (struct hlist_head *)__get_free_pages(GFP_ATOMIC, order);
2550 if (!dquot_hash)
2551 panic("Cannot create dquot hash table");
2552
2553 /* Find power-of-two hlist_heads which can fit into allocation */
2554 nr_hash = (1UL << order) * PAGE_SIZE / sizeof(struct hlist_head);
2555 dq_hash_bits = 0;
2556 do {
2557 dq_hash_bits++;
2558 } while (nr_hash >> dq_hash_bits);
2559 dq_hash_bits--;
2560
2561 nr_hash = 1UL << dq_hash_bits;
2562 dq_hash_mask = nr_hash - 1;
2563 for (i = 0; i < nr_hash; i++)
2564 INIT_HLIST_HEAD(dquot_hash + i);
2565
2566 printk("Dquot-cache hash table entries: %ld (order %ld, %ld bytes)\n",
2567 nr_hash, order, (PAGE_SIZE << order));
2568
Rusty Russell8e1f9362007-07-17 04:03:17 -07002569 register_shrinker(&dqcache_shrinker);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002570
Linus Torvalds1da177e2005-04-16 15:20:36 -07002571 return 0;
2572}
2573module_init(dquot_init);