blob: 05c590e10ac21dce7e1796531dbda1ea7d64eb11 [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
Linus Torvalds1da177e2005-04-16 15:20:36 -070083/*
Jan Karacc334122009-01-12 17:23:05 +010084 * There are three quota SMP locks. dq_list_lock protects all lists with quotas
85 * and quota formats, dqstats structure containing statistics about the lists
86 * dq_data_lock protects data from dq_dqb and also mem_dqinfo structures and
87 * also guards consistency of dquot->dq_dqb with inode->i_blocks, i_bytes.
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 * i_blocks and i_bytes updates itself are guarded by i_lock acquired directly
Jan Karacc334122009-01-12 17:23:05 +010089 * in inode_add_bytes() and inode_sub_bytes(). dq_state_lock protects
90 * modifications of quota state (on quotaon and quotaoff) and readers who care
91 * about latest values take it as well.
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 *
Jan Karacc334122009-01-12 17:23:05 +010093 * The spinlock ordering is hence: dq_data_lock > dq_list_lock > i_lock,
94 * dq_list_lock > dq_state_lock
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 *
96 * Note that some things (eg. sb pointer, type, id) doesn't change during
97 * the life of the dquot structure and so needn't to be protected by a lock
98 *
99 * Any operation working on dquots via inode pointers must hold dqptr_sem. If
100 * operation is just reading pointers from inode (or not using them at all) the
Jan Kara26245c92010-01-06 17:20:35 +0100101 * read lock is enough. If pointers are altered function must hold write lock.
102 * Special care needs to be taken about S_NOQUOTA inode flag (marking that
103 * inode is a quota file). Functions adding pointers from inode to dquots have
104 * to check this flag under dqptr_sem and then (if S_NOQUOTA is not set) they
105 * have to do all pointer modifications before dropping dqptr_sem. This makes
106 * sure they cannot race with quotaon which first sets S_NOQUOTA flag and
107 * then drops all pointers to dquots from an inode.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 *
Ingo Molnard3be9152006-03-23 03:00:29 -0800109 * Each dquot has its dq_lock mutex. Locked dquots might not be referenced
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 * from inodes (dquot_alloc_space() and such don't check the dq_lock).
111 * Currently dquot is locked only when it is being read to memory (or space for
112 * it is being allocated) on the first dqget() and when it is being released on
113 * the last dqput(). The allocation and release oparations are serialized by
114 * the dq_lock and by checking the use count in dquot_release(). Write
115 * operations on dquots don't hold dq_lock as they copy data under dq_data_lock
116 * spinlock to internal buffers before writing.
117 *
118 * Lock ordering (including related VFS locks) is the following:
Ingo Molnard3be9152006-03-23 03:00:29 -0800119 * i_mutex > dqonoff_sem > journal_lock > dqptr_sem > dquot->dq_lock >
120 * dqio_mutex
Jan Karacc334122009-01-12 17:23:05 +0100121 * The lock ordering of dqptr_sem imposed by quota code is only dqonoff_sem >
122 * dqptr_sem. But filesystem has to count with the fact that functions such as
123 * dquot_alloc_space() acquire dqptr_sem and they usually have to be called
124 * from inside a transaction to keep filesystem consistency after a crash. Also
125 * filesystems usually want to do some IO on dquot from ->mark_dirty which is
126 * called with dqptr_sem held.
Ingo Molnard3be9152006-03-23 03:00:29 -0800127 * i_mutex on quota files is special (it's below dqio_mutex)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 */
129
Jan Karac5166102009-01-26 15:32:46 +0100130static __cacheline_aligned_in_smp DEFINE_SPINLOCK(dq_list_lock);
131static __cacheline_aligned_in_smp DEFINE_SPINLOCK(dq_state_lock);
132__cacheline_aligned_in_smp DEFINE_SPINLOCK(dq_data_lock);
Mingming Cao08d03502009-01-14 16:19:32 +0100133EXPORT_SYMBOL(dq_data_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134
135static char *quotatypes[] = INITQFNAMES;
136static struct quota_format_type *quota_formats; /* List of registered formats */
137static struct quota_module_name module_names[] = INIT_QUOTA_MODULE_NAMES;
138
139/* SLAB cache for dquot structures */
Christoph Lametere18b8902006-12-06 20:33:20 -0800140static struct kmem_cache *dquot_cachep;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141
142int register_quota_format(struct quota_format_type *fmt)
143{
144 spin_lock(&dq_list_lock);
145 fmt->qf_next = quota_formats;
146 quota_formats = fmt;
147 spin_unlock(&dq_list_lock);
148 return 0;
149}
Mingming Cao08d03502009-01-14 16:19:32 +0100150EXPORT_SYMBOL(register_quota_format);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151
152void unregister_quota_format(struct quota_format_type *fmt)
153{
154 struct quota_format_type **actqf;
155
156 spin_lock(&dq_list_lock);
Jan Kara268157b2009-01-27 15:47:22 +0100157 for (actqf = &quota_formats; *actqf && *actqf != fmt;
158 actqf = &(*actqf)->qf_next)
159 ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 if (*actqf)
161 *actqf = (*actqf)->qf_next;
162 spin_unlock(&dq_list_lock);
163}
Mingming Cao08d03502009-01-14 16:19:32 +0100164EXPORT_SYMBOL(unregister_quota_format);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165
166static struct quota_format_type *find_quota_format(int id)
167{
168 struct quota_format_type *actqf;
169
170 spin_lock(&dq_list_lock);
Jan Kara268157b2009-01-27 15:47:22 +0100171 for (actqf = quota_formats; actqf && actqf->qf_fmt_id != id;
172 actqf = actqf->qf_next)
173 ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 if (!actqf || !try_module_get(actqf->qf_owner)) {
175 int qm;
176
177 spin_unlock(&dq_list_lock);
178
Jan Kara268157b2009-01-27 15:47:22 +0100179 for (qm = 0; module_names[qm].qm_fmt_id &&
180 module_names[qm].qm_fmt_id != id; qm++)
181 ;
182 if (!module_names[qm].qm_fmt_id ||
183 request_module(module_names[qm].qm_mod_name))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 return NULL;
185
186 spin_lock(&dq_list_lock);
Jan Kara268157b2009-01-27 15:47:22 +0100187 for (actqf = quota_formats; actqf && actqf->qf_fmt_id != id;
188 actqf = actqf->qf_next)
189 ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 if (actqf && !try_module_get(actqf->qf_owner))
191 actqf = NULL;
192 }
193 spin_unlock(&dq_list_lock);
194 return actqf;
195}
196
197static void put_quota_format(struct quota_format_type *fmt)
198{
199 module_put(fmt->qf_owner);
200}
201
202/*
203 * Dquot List Management:
204 * The quota code uses three lists for dquot management: the inuse_list,
205 * free_dquots, and dquot_hash[] array. A single dquot structure may be
206 * on all three lists, depending on its current state.
207 *
208 * All dquots are placed to the end of inuse_list when first created, and this
209 * list is used for invalidate operation, which must look at every dquot.
210 *
211 * Unused dquots (dq_count == 0) are added to the free_dquots list when freed,
212 * and this list is searched whenever we need an available dquot. Dquots are
213 * removed from the list as soon as they are used again, and
214 * dqstats.free_dquots gives the number of dquots on the list. When
215 * dquot is invalidated it's completely released from memory.
216 *
217 * Dquots with a specific identity (device, type and id) are placed on
218 * one of the dquot_hash[] hash chains. The provides an efficient search
219 * mechanism to locate a specific dquot.
220 */
221
222static LIST_HEAD(inuse_list);
223static LIST_HEAD(free_dquots);
224static unsigned int dq_hash_bits, dq_hash_mask;
225static struct hlist_head *dquot_hash;
226
227struct dqstats dqstats;
Mingming Cao08d03502009-01-14 16:19:32 +0100228EXPORT_SYMBOL(dqstats);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229
Jan Kara0a5a9c72010-02-09 18:20:39 +0100230static qsize_t inode_get_rsv_space(struct inode *inode);
Christoph Hellwig871a2932010-03-03 09:05:07 -0500231static void __dquot_initialize(struct inode *inode, int type);
Jan Kara0a5a9c72010-02-09 18:20:39 +0100232
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233static inline unsigned int
234hashfn(const struct super_block *sb, unsigned int id, int type)
235{
236 unsigned long tmp;
237
238 tmp = (((unsigned long)sb>>L1_CACHE_SHIFT) ^ id) * (MAXQUOTAS - type);
239 return (tmp + (tmp >> dq_hash_bits)) & dq_hash_mask;
240}
241
242/*
243 * Following list functions expect dq_list_lock to be held
244 */
245static inline void insert_dquot_hash(struct dquot *dquot)
246{
Jan Kara268157b2009-01-27 15:47:22 +0100247 struct hlist_head *head;
248 head = dquot_hash + hashfn(dquot->dq_sb, dquot->dq_id, dquot->dq_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 hlist_add_head(&dquot->dq_hash, head);
250}
251
252static inline void remove_dquot_hash(struct dquot *dquot)
253{
254 hlist_del_init(&dquot->dq_hash);
255}
256
Jan Kara7a2435d2009-01-27 01:47:11 +0100257static struct dquot *find_dquot(unsigned int hashent, struct super_block *sb,
258 unsigned int id, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259{
260 struct hlist_node *node;
261 struct dquot *dquot;
262
263 hlist_for_each (node, dquot_hash+hashent) {
264 dquot = hlist_entry(node, struct dquot, dq_hash);
Jan Kara268157b2009-01-27 15:47:22 +0100265 if (dquot->dq_sb == sb && dquot->dq_id == id &&
266 dquot->dq_type == type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 return dquot;
268 }
Jan Karadd6f3c62009-01-26 16:01:43 +0100269 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270}
271
272/* Add a dquot to the tail of the free list */
273static inline void put_dquot_last(struct dquot *dquot)
274{
Akinobu Mita8e130592006-06-26 00:24:37 -0700275 list_add_tail(&dquot->dq_free, &free_dquots);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 dqstats.free_dquots++;
277}
278
279static inline void remove_free_dquot(struct dquot *dquot)
280{
281 if (list_empty(&dquot->dq_free))
282 return;
283 list_del_init(&dquot->dq_free);
284 dqstats.free_dquots--;
285}
286
287static inline void put_inuse(struct dquot *dquot)
288{
289 /* We add to the back of inuse list so we don't have to restart
290 * when traversing this list and we block */
Akinobu Mita8e130592006-06-26 00:24:37 -0700291 list_add_tail(&dquot->dq_inuse, &inuse_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 dqstats.allocated_dquots++;
293}
294
295static inline void remove_inuse(struct dquot *dquot)
296{
297 dqstats.allocated_dquots--;
298 list_del(&dquot->dq_inuse);
299}
300/*
301 * End of list functions needing dq_list_lock
302 */
303
304static void wait_on_dquot(struct dquot *dquot)
305{
Ingo Molnard3be9152006-03-23 03:00:29 -0800306 mutex_lock(&dquot->dq_lock);
307 mutex_unlock(&dquot->dq_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308}
309
Jan Kara03f6e922008-04-28 02:14:32 -0700310static inline int dquot_dirty(struct dquot *dquot)
311{
312 return test_bit(DQ_MOD_B, &dquot->dq_flags);
313}
314
315static inline int mark_dquot_dirty(struct dquot *dquot)
316{
317 return dquot->dq_sb->dq_op->mark_dirty(dquot);
318}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319
Dmitry Monakhoveabf2902010-03-27 15:15:38 +0300320/* Mark dquot dirty in atomic manner, and return it's old dirty flag state */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321int dquot_mark_dquot_dirty(struct dquot *dquot)
322{
Dmitry Monakhoveabf2902010-03-27 15:15:38 +0300323 int ret = 1;
324
325 /* If quota is dirty already, we don't have to acquire dq_list_lock */
326 if (test_bit(DQ_MOD_B, &dquot->dq_flags))
327 return 1;
328
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 spin_lock(&dq_list_lock);
Dmitry Monakhoveabf2902010-03-27 15:15:38 +0300330 if (!test_and_set_bit(DQ_MOD_B, &dquot->dq_flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 list_add(&dquot->dq_dirty, &sb_dqopt(dquot->dq_sb)->
332 info[dquot->dq_type].dqi_dirty_list);
Dmitry Monakhoveabf2902010-03-27 15:15:38 +0300333 ret = 0;
334 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 spin_unlock(&dq_list_lock);
Dmitry Monakhoveabf2902010-03-27 15:15:38 +0300336 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337}
Mingming Cao08d03502009-01-14 16:19:32 +0100338EXPORT_SYMBOL(dquot_mark_dquot_dirty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339
Dmitry Monakhovdc52dd32009-12-14 15:21:15 +0300340/* Dirtify all the dquots - this can block when journalling */
341static inline int mark_all_dquot_dirty(struct dquot * const *dquot)
342{
343 int ret, err, cnt;
344
345 ret = err = 0;
346 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
347 if (dquot[cnt])
348 /* Even in case of error we have to continue */
349 ret = mark_dquot_dirty(dquot[cnt]);
350 if (!err)
351 err = ret;
352 }
353 return err;
354}
355
356static inline void dqput_all(struct dquot **dquot)
357{
358 unsigned int cnt;
359
360 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
361 dqput(dquot[cnt]);
362}
363
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364/* This function needs dq_list_lock */
365static inline int clear_dquot_dirty(struct dquot *dquot)
366{
367 if (!test_and_clear_bit(DQ_MOD_B, &dquot->dq_flags))
368 return 0;
369 list_del_init(&dquot->dq_dirty);
370 return 1;
371}
372
373void mark_info_dirty(struct super_block *sb, int type)
374{
375 set_bit(DQF_INFO_DIRTY_B, &sb_dqopt(sb)->info[type].dqi_flags);
376}
377EXPORT_SYMBOL(mark_info_dirty);
378
379/*
380 * Read dquot from disk and alloc space for it
381 */
382
383int dquot_acquire(struct dquot *dquot)
384{
385 int ret = 0, ret2 = 0;
386 struct quota_info *dqopt = sb_dqopt(dquot->dq_sb);
387
Ingo Molnard3be9152006-03-23 03:00:29 -0800388 mutex_lock(&dquot->dq_lock);
389 mutex_lock(&dqopt->dqio_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 if (!test_bit(DQ_READ_B, &dquot->dq_flags))
391 ret = dqopt->ops[dquot->dq_type]->read_dqblk(dquot);
392 if (ret < 0)
393 goto out_iolock;
394 set_bit(DQ_READ_B, &dquot->dq_flags);
395 /* Instantiate dquot if needed */
396 if (!test_bit(DQ_ACTIVE_B, &dquot->dq_flags) && !dquot->dq_off) {
397 ret = dqopt->ops[dquot->dq_type]->commit_dqblk(dquot);
398 /* Write the info if needed */
Jan Kara268157b2009-01-27 15:47:22 +0100399 if (info_dirty(&dqopt->info[dquot->dq_type])) {
400 ret2 = dqopt->ops[dquot->dq_type]->write_file_info(
401 dquot->dq_sb, dquot->dq_type);
402 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 if (ret < 0)
404 goto out_iolock;
405 if (ret2 < 0) {
406 ret = ret2;
407 goto out_iolock;
408 }
409 }
410 set_bit(DQ_ACTIVE_B, &dquot->dq_flags);
411out_iolock:
Ingo Molnard3be9152006-03-23 03:00:29 -0800412 mutex_unlock(&dqopt->dqio_mutex);
413 mutex_unlock(&dquot->dq_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 return ret;
415}
Mingming Cao08d03502009-01-14 16:19:32 +0100416EXPORT_SYMBOL(dquot_acquire);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417
418/*
419 * Write dquot to disk
420 */
421int dquot_commit(struct dquot *dquot)
422{
423 int ret = 0, ret2 = 0;
424 struct quota_info *dqopt = sb_dqopt(dquot->dq_sb);
425
Ingo Molnard3be9152006-03-23 03:00:29 -0800426 mutex_lock(&dqopt->dqio_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 spin_lock(&dq_list_lock);
428 if (!clear_dquot_dirty(dquot)) {
429 spin_unlock(&dq_list_lock);
430 goto out_sem;
431 }
432 spin_unlock(&dq_list_lock);
433 /* Inactive dquot can be only if there was error during read/init
434 * => we have better not writing it */
435 if (test_bit(DQ_ACTIVE_B, &dquot->dq_flags)) {
436 ret = dqopt->ops[dquot->dq_type]->commit_dqblk(dquot);
Jan Kara268157b2009-01-27 15:47:22 +0100437 if (info_dirty(&dqopt->info[dquot->dq_type])) {
438 ret2 = dqopt->ops[dquot->dq_type]->write_file_info(
439 dquot->dq_sb, dquot->dq_type);
440 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 if (ret >= 0)
442 ret = ret2;
443 }
444out_sem:
Ingo Molnard3be9152006-03-23 03:00:29 -0800445 mutex_unlock(&dqopt->dqio_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 return ret;
447}
Mingming Cao08d03502009-01-14 16:19:32 +0100448EXPORT_SYMBOL(dquot_commit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449
450/*
451 * Release dquot
452 */
453int dquot_release(struct dquot *dquot)
454{
455 int ret = 0, ret2 = 0;
456 struct quota_info *dqopt = sb_dqopt(dquot->dq_sb);
457
Ingo Molnard3be9152006-03-23 03:00:29 -0800458 mutex_lock(&dquot->dq_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 /* Check whether we are not racing with some other dqget() */
460 if (atomic_read(&dquot->dq_count) > 1)
461 goto out_dqlock;
Ingo Molnard3be9152006-03-23 03:00:29 -0800462 mutex_lock(&dqopt->dqio_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 if (dqopt->ops[dquot->dq_type]->release_dqblk) {
464 ret = dqopt->ops[dquot->dq_type]->release_dqblk(dquot);
465 /* Write the info */
Jan Kara268157b2009-01-27 15:47:22 +0100466 if (info_dirty(&dqopt->info[dquot->dq_type])) {
467 ret2 = dqopt->ops[dquot->dq_type]->write_file_info(
468 dquot->dq_sb, dquot->dq_type);
469 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 if (ret >= 0)
471 ret = ret2;
472 }
473 clear_bit(DQ_ACTIVE_B, &dquot->dq_flags);
Ingo Molnard3be9152006-03-23 03:00:29 -0800474 mutex_unlock(&dqopt->dqio_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475out_dqlock:
Ingo Molnard3be9152006-03-23 03:00:29 -0800476 mutex_unlock(&dquot->dq_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 return ret;
478}
Mingming Cao08d03502009-01-14 16:19:32 +0100479EXPORT_SYMBOL(dquot_release);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480
Jan Kara7d9056b2008-11-25 15:31:32 +0100481void dquot_destroy(struct dquot *dquot)
Jan Kara74f783a2008-08-19 14:51:22 +0200482{
483 kmem_cache_free(dquot_cachep, dquot);
484}
Jan Kara7d9056b2008-11-25 15:31:32 +0100485EXPORT_SYMBOL(dquot_destroy);
Jan Kara74f783a2008-08-19 14:51:22 +0200486
487static inline void do_destroy_dquot(struct dquot *dquot)
488{
489 dquot->dq_sb->dq_op->destroy_dquot(dquot);
490}
491
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492/* Invalidate all dquots on the list. Note that this function is called after
493 * quota is disabled and pointers from inodes removed so there cannot be new
Jan Kara6362e4d2006-03-23 03:00:17 -0800494 * quota users. There can still be some users of quotas due to inodes being
495 * just deleted or pruned by prune_icache() (those are not attached to any
Jan Karacc334122009-01-12 17:23:05 +0100496 * list) or parallel quotactl call. We have to wait for such users.
Jan Kara6362e4d2006-03-23 03:00:17 -0800497 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498static void invalidate_dquots(struct super_block *sb, int type)
499{
Domen Puncerc33ed272005-06-25 14:59:36 -0700500 struct dquot *dquot, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501
Jan Kara6362e4d2006-03-23 03:00:17 -0800502restart:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 spin_lock(&dq_list_lock);
Domen Puncerc33ed272005-06-25 14:59:36 -0700504 list_for_each_entry_safe(dquot, tmp, &inuse_list, dq_inuse) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 if (dquot->dq_sb != sb)
506 continue;
507 if (dquot->dq_type != type)
508 continue;
Jan Kara6362e4d2006-03-23 03:00:17 -0800509 /* Wait for dquot users */
510 if (atomic_read(&dquot->dq_count)) {
511 DEFINE_WAIT(wait);
512
513 atomic_inc(&dquot->dq_count);
514 prepare_to_wait(&dquot->dq_wait_unused, &wait,
515 TASK_UNINTERRUPTIBLE);
516 spin_unlock(&dq_list_lock);
517 /* Once dqput() wakes us up, we know it's time to free
518 * the dquot.
519 * IMPORTANT: we rely on the fact that there is always
520 * at most one process waiting for dquot to free.
521 * Otherwise dq_count would be > 1 and we would never
522 * wake up.
523 */
524 if (atomic_read(&dquot->dq_count) > 1)
525 schedule();
526 finish_wait(&dquot->dq_wait_unused, &wait);
527 dqput(dquot);
528 /* At this moment dquot() need not exist (it could be
529 * reclaimed by prune_dqcache(). Hence we must
530 * restart. */
531 goto restart;
532 }
533 /*
534 * Quota now has no users and it has been written on last
535 * dqput()
536 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 remove_dquot_hash(dquot);
538 remove_free_dquot(dquot);
539 remove_inuse(dquot);
Jan Kara74f783a2008-08-19 14:51:22 +0200540 do_destroy_dquot(dquot);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 }
542 spin_unlock(&dq_list_lock);
543}
544
Jan Kara12c77522008-10-20 17:05:00 +0200545/* Call callback for every active dquot on given filesystem */
546int dquot_scan_active(struct super_block *sb,
547 int (*fn)(struct dquot *dquot, unsigned long priv),
548 unsigned long priv)
549{
550 struct dquot *dquot, *old_dquot = NULL;
551 int ret = 0;
552
553 mutex_lock(&sb_dqopt(sb)->dqonoff_mutex);
554 spin_lock(&dq_list_lock);
555 list_for_each_entry(dquot, &inuse_list, dq_inuse) {
556 if (!test_bit(DQ_ACTIVE_B, &dquot->dq_flags))
557 continue;
558 if (dquot->dq_sb != sb)
559 continue;
560 /* Now we have active dquot so we can just increase use count */
561 atomic_inc(&dquot->dq_count);
562 dqstats.lookups++;
563 spin_unlock(&dq_list_lock);
564 dqput(old_dquot);
565 old_dquot = dquot;
566 ret = fn(dquot, priv);
567 if (ret < 0)
568 goto out;
569 spin_lock(&dq_list_lock);
570 /* We are safe to continue now because our dquot could not
571 * be moved out of the inuse list while we hold the reference */
572 }
573 spin_unlock(&dq_list_lock);
574out:
575 dqput(old_dquot);
576 mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
577 return ret;
578}
Mingming Cao08d03502009-01-14 16:19:32 +0100579EXPORT_SYMBOL(dquot_scan_active);
Jan Kara12c77522008-10-20 17:05:00 +0200580
Christoph Hellwig5fb324a2010-02-16 03:44:52 -0500581int vfs_quota_sync(struct super_block *sb, int type, int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582{
583 struct list_head *dirty;
584 struct dquot *dquot;
585 struct quota_info *dqopt = sb_dqopt(sb);
586 int cnt;
587
Ingo Molnard3be9152006-03-23 03:00:29 -0800588 mutex_lock(&dqopt->dqonoff_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
590 if (type != -1 && cnt != type)
591 continue;
Jan Karaf55abc02008-08-20 17:50:32 +0200592 if (!sb_has_quota_active(sb, cnt))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 continue;
594 spin_lock(&dq_list_lock);
595 dirty = &dqopt->info[cnt].dqi_dirty_list;
596 while (!list_empty(dirty)) {
Jan Kara268157b2009-01-27 15:47:22 +0100597 dquot = list_first_entry(dirty, struct dquot,
598 dq_dirty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 /* Dirty and inactive can be only bad dquot... */
600 if (!test_bit(DQ_ACTIVE_B, &dquot->dq_flags)) {
601 clear_dquot_dirty(dquot);
602 continue;
603 }
604 /* Now we have active dquot from which someone is
605 * holding reference so we can safely just increase
606 * use count */
607 atomic_inc(&dquot->dq_count);
608 dqstats.lookups++;
609 spin_unlock(&dq_list_lock);
610 sb->dq_op->write_dquot(dquot);
611 dqput(dquot);
612 spin_lock(&dq_list_lock);
613 }
614 spin_unlock(&dq_list_lock);
615 }
616
617 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
Jan Karaf55abc02008-08-20 17:50:32 +0200618 if ((cnt == type || type == -1) && sb_has_quota_active(sb, cnt)
619 && info_dirty(&dqopt->info[cnt]))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 sb->dq_op->write_info(sb, cnt);
621 spin_lock(&dq_list_lock);
622 dqstats.syncs++;
623 spin_unlock(&dq_list_lock);
Ingo Molnard3be9152006-03-23 03:00:29 -0800624 mutex_unlock(&dqopt->dqonoff_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625
Christoph Hellwig5fb324a2010-02-16 03:44:52 -0500626 if (!wait || (sb_dqopt(sb)->flags & DQUOT_QUOTA_SYS_FILE))
627 return 0;
628
629 /* This is not very clever (and fast) but currently I don't know about
630 * any other simple way of getting quota data to disk and we must get
631 * them there for userspace to be visible... */
632 if (sb->s_op->sync_fs)
633 sb->s_op->sync_fs(sb, 1);
634 sync_blockdev(sb->s_bdev);
635
636 /*
637 * Now when everything is written we can discard the pagecache so
638 * that userspace sees the changes.
639 */
640 mutex_lock(&sb_dqopt(sb)->dqonoff_mutex);
641 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
642 if (type != -1 && cnt != type)
643 continue;
644 if (!sb_has_quota_active(sb, cnt))
645 continue;
646 mutex_lock_nested(&sb_dqopt(sb)->files[cnt]->i_mutex,
647 I_MUTEX_QUOTA);
648 truncate_inode_pages(&sb_dqopt(sb)->files[cnt]->i_data, 0);
649 mutex_unlock(&sb_dqopt(sb)->files[cnt]->i_mutex);
650 }
651 mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
652
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 return 0;
654}
Mingming Cao08d03502009-01-14 16:19:32 +0100655EXPORT_SYMBOL(vfs_quota_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656
657/* Free unused dquots from cache */
658static void prune_dqcache(int count)
659{
660 struct list_head *head;
661 struct dquot *dquot;
662
663 head = free_dquots.prev;
664 while (head != &free_dquots && count) {
665 dquot = list_entry(head, struct dquot, dq_free);
666 remove_dquot_hash(dquot);
667 remove_free_dquot(dquot);
668 remove_inuse(dquot);
Jan Kara74f783a2008-08-19 14:51:22 +0200669 do_destroy_dquot(dquot);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 count--;
671 head = free_dquots.prev;
672 }
673}
674
675/*
676 * This is called from kswapd when we think we need some
677 * more memory
678 */
679
Al Viro27496a82005-10-21 03:20:48 -0400680static int shrink_dqcache_memory(int nr, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681{
682 if (nr) {
683 spin_lock(&dq_list_lock);
684 prune_dqcache(nr);
685 spin_unlock(&dq_list_lock);
686 }
687 return (dqstats.free_dquots / 100) * sysctl_vfs_cache_pressure;
688}
689
Rusty Russell8e1f9362007-07-17 04:03:17 -0700690static struct shrinker dqcache_shrinker = {
691 .shrink = shrink_dqcache_memory,
692 .seeks = DEFAULT_SEEKS,
693};
694
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695/*
696 * Put reference to dquot
697 * NOTE: If you change this function please check whether dqput_blocks() works right...
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 */
Jan Kara3d9ea252008-10-10 16:12:23 +0200699void dqput(struct dquot *dquot)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700{
Jan Karab48d3802008-07-25 01:46:49 -0700701 int ret;
702
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 if (!dquot)
704 return;
Jan Kara62af9b52010-04-19 16:47:20 +0200705#ifdef CONFIG_QUOTA_DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 if (!atomic_read(&dquot->dq_count)) {
707 printk("VFS: dqput: trying to free free dquot\n");
708 printk("VFS: device %s, dquot of %s %d\n",
709 dquot->dq_sb->s_id,
710 quotatypes[dquot->dq_type],
711 dquot->dq_id);
712 BUG();
713 }
714#endif
715
716 spin_lock(&dq_list_lock);
717 dqstats.drops++;
718 spin_unlock(&dq_list_lock);
719we_slept:
720 spin_lock(&dq_list_lock);
721 if (atomic_read(&dquot->dq_count) > 1) {
722 /* We have more than one user... nothing to do */
723 atomic_dec(&dquot->dq_count);
Jan Kara6362e4d2006-03-23 03:00:17 -0800724 /* Releasing dquot during quotaoff phase? */
Jan Karaf55abc02008-08-20 17:50:32 +0200725 if (!sb_has_quota_active(dquot->dq_sb, dquot->dq_type) &&
Jan Kara6362e4d2006-03-23 03:00:17 -0800726 atomic_read(&dquot->dq_count) == 1)
727 wake_up(&dquot->dq_wait_unused);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 spin_unlock(&dq_list_lock);
729 return;
730 }
731 /* Need to release dquot? */
732 if (test_bit(DQ_ACTIVE_B, &dquot->dq_flags) && dquot_dirty(dquot)) {
733 spin_unlock(&dq_list_lock);
734 /* Commit dquot before releasing */
Jan Karab48d3802008-07-25 01:46:49 -0700735 ret = dquot->dq_sb->dq_op->write_dquot(dquot);
736 if (ret < 0) {
737 printk(KERN_ERR "VFS: cannot write quota structure on "
738 "device %s (error %d). Quota may get out of "
739 "sync!\n", dquot->dq_sb->s_id, ret);
740 /*
741 * We clear dirty bit anyway, so that we avoid
742 * infinite loop here
743 */
744 spin_lock(&dq_list_lock);
745 clear_dquot_dirty(dquot);
746 spin_unlock(&dq_list_lock);
747 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 goto we_slept;
749 }
750 /* Clear flag in case dquot was inactive (something bad happened) */
751 clear_dquot_dirty(dquot);
752 if (test_bit(DQ_ACTIVE_B, &dquot->dq_flags)) {
753 spin_unlock(&dq_list_lock);
754 dquot->dq_sb->dq_op->release_dquot(dquot);
755 goto we_slept;
756 }
757 atomic_dec(&dquot->dq_count);
Jan Kara62af9b52010-04-19 16:47:20 +0200758#ifdef CONFIG_QUOTA_DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 /* sanity check */
Eric Sesterhenn8abf6a42006-04-02 13:36:13 +0200760 BUG_ON(!list_empty(&dquot->dq_free));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761#endif
762 put_dquot_last(dquot);
763 spin_unlock(&dq_list_lock);
764}
Mingming Cao08d03502009-01-14 16:19:32 +0100765EXPORT_SYMBOL(dqput);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766
Jan Kara7d9056b2008-11-25 15:31:32 +0100767struct dquot *dquot_alloc(struct super_block *sb, int type)
Jan Kara74f783a2008-08-19 14:51:22 +0200768{
769 return kmem_cache_zalloc(dquot_cachep, GFP_NOFS);
770}
Jan Kara7d9056b2008-11-25 15:31:32 +0100771EXPORT_SYMBOL(dquot_alloc);
Jan Kara74f783a2008-08-19 14:51:22 +0200772
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773static struct dquot *get_empty_dquot(struct super_block *sb, int type)
774{
775 struct dquot *dquot;
776
Jan Kara74f783a2008-08-19 14:51:22 +0200777 dquot = sb->dq_op->alloc_dquot(sb, type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 if(!dquot)
Jan Karadd6f3c62009-01-26 16:01:43 +0100779 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780
Ingo Molnard3be9152006-03-23 03:00:29 -0800781 mutex_init(&dquot->dq_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 INIT_LIST_HEAD(&dquot->dq_free);
783 INIT_LIST_HEAD(&dquot->dq_inuse);
784 INIT_HLIST_NODE(&dquot->dq_hash);
785 INIT_LIST_HEAD(&dquot->dq_dirty);
Jan Kara6362e4d2006-03-23 03:00:17 -0800786 init_waitqueue_head(&dquot->dq_wait_unused);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 dquot->dq_sb = sb;
788 dquot->dq_type = type;
789 atomic_set(&dquot->dq_count, 1);
790
791 return dquot;
792}
793
794/*
795 * Get reference to dquot
Jan Karacc334122009-01-12 17:23:05 +0100796 *
797 * Locking is slightly tricky here. We are guarded from parallel quotaoff()
798 * destroying our dquot by:
799 * a) checking for quota flags under dq_list_lock and
800 * b) getting a reference to dquot before we release dq_list_lock
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 */
Jan Kara3d9ea252008-10-10 16:12:23 +0200802struct dquot *dqget(struct super_block *sb, unsigned int id, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803{
804 unsigned int hashent = hashfn(sb, id, type);
Jan Karadd6f3c62009-01-26 16:01:43 +0100805 struct dquot *dquot = NULL, *empty = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806
Jan Karaf55abc02008-08-20 17:50:32 +0200807 if (!sb_has_quota_active(sb, type))
Jan Karadd6f3c62009-01-26 16:01:43 +0100808 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809we_slept:
810 spin_lock(&dq_list_lock);
Jan Karacc334122009-01-12 17:23:05 +0100811 spin_lock(&dq_state_lock);
812 if (!sb_has_quota_active(sb, type)) {
813 spin_unlock(&dq_state_lock);
814 spin_unlock(&dq_list_lock);
815 goto out;
816 }
817 spin_unlock(&dq_state_lock);
818
Jan Karadd6f3c62009-01-26 16:01:43 +0100819 dquot = find_dquot(hashent, sb, id, type);
820 if (!dquot) {
821 if (!empty) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 spin_unlock(&dq_list_lock);
Jan Karadd6f3c62009-01-26 16:01:43 +0100823 empty = get_empty_dquot(sb, type);
824 if (!empty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 schedule(); /* Try to wait for a moment... */
826 goto we_slept;
827 }
828 dquot = empty;
Jan Karadd6f3c62009-01-26 16:01:43 +0100829 empty = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 dquot->dq_id = id;
831 /* all dquots go on the inuse_list */
832 put_inuse(dquot);
833 /* hash it first so it can be found */
834 insert_dquot_hash(dquot);
835 dqstats.lookups++;
836 spin_unlock(&dq_list_lock);
837 } else {
838 if (!atomic_read(&dquot->dq_count))
839 remove_free_dquot(dquot);
840 atomic_inc(&dquot->dq_count);
841 dqstats.cache_hits++;
842 dqstats.lookups++;
843 spin_unlock(&dq_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 }
Jan Kara268157b2009-01-27 15:47:22 +0100845 /* Wait for dq_lock - after this we know that either dquot_release() is
846 * already finished or it will be canceled due to dq_count > 1 test */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 wait_on_dquot(dquot);
Jan Kara268157b2009-01-27 15:47:22 +0100848 /* Read the dquot / allocate space in quota file */
849 if (!test_bit(DQ_ACTIVE_B, &dquot->dq_flags) &&
850 sb->dq_op->acquire_dquot(dquot) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 dqput(dquot);
Jan Karadd6f3c62009-01-26 16:01:43 +0100852 dquot = NULL;
Jan Karacc334122009-01-12 17:23:05 +0100853 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 }
Jan Kara62af9b52010-04-19 16:47:20 +0200855#ifdef CONFIG_QUOTA_DEBUG
Eric Sesterhenn8abf6a42006-04-02 13:36:13 +0200856 BUG_ON(!dquot->dq_sb); /* Has somebody invalidated entry under us? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857#endif
Jan Karacc334122009-01-12 17:23:05 +0100858out:
859 if (empty)
860 do_destroy_dquot(empty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861
862 return dquot;
863}
Mingming Cao08d03502009-01-14 16:19:32 +0100864EXPORT_SYMBOL(dqget);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865
866static int dqinit_needed(struct inode *inode, int type)
867{
868 int cnt;
869
870 if (IS_NOQUOTA(inode))
871 return 0;
872 if (type != -1)
Jan Karadd6f3c62009-01-26 16:01:43 +0100873 return !inode->i_dquot[type];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
Jan Karadd6f3c62009-01-26 16:01:43 +0100875 if (!inode->i_dquot[cnt])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 return 1;
877 return 0;
878}
879
Ingo Molnard3be9152006-03-23 03:00:29 -0800880/* This routine is guarded by dqonoff_mutex mutex */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881static void add_dquot_ref(struct super_block *sb, int type)
882{
Jan Kara941d2382008-02-06 01:37:36 -0800883 struct inode *inode, *old_inode = NULL;
Jan Kara62af9b52010-04-19 16:47:20 +0200884#ifdef CONFIG_QUOTA_DEBUG
Jan Kara0a5a9c72010-02-09 18:20:39 +0100885 int reserved = 0;
Jan Kara4c5e6c02010-04-06 18:52:47 +0200886#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887
Christoph Hellwigd003fb72007-02-12 00:51:58 -0800888 spin_lock(&inode_lock);
889 list_for_each_entry(inode, &sb->s_inodes, i_sb_list) {
Wu Fengguangb6fac632009-04-02 16:56:34 -0700890 if (inode->i_state & (I_FREEING|I_CLEAR|I_WILL_FREE|I_NEW))
Nick Pigginaabb8fd2009-03-11 13:17:36 -0700891 continue;
Jan Kara62af9b52010-04-19 16:47:20 +0200892#ifdef CONFIG_QUOTA_DEBUG
Jan Kara0a5a9c72010-02-09 18:20:39 +0100893 if (unlikely(inode_get_rsv_space(inode) > 0))
894 reserved = 1;
Jan Kara4c5e6c02010-04-06 18:52:47 +0200895#endif
Christoph Hellwigd003fb72007-02-12 00:51:58 -0800896 if (!atomic_read(&inode->i_writecount))
897 continue;
898 if (!dqinit_needed(inode, type))
899 continue;
Christoph Hellwigd003fb72007-02-12 00:51:58 -0800900
901 __iget(inode);
902 spin_unlock(&inode_lock);
903
Jan Kara941d2382008-02-06 01:37:36 -0800904 iput(old_inode);
Christoph Hellwig871a2932010-03-03 09:05:07 -0500905 __dquot_initialize(inode, type);
Jan Kara941d2382008-02-06 01:37:36 -0800906 /* We hold a reference to 'inode' so it couldn't have been
907 * removed from s_inodes list while we dropped the inode_lock.
908 * We cannot iput the inode now as we can be holding the last
909 * reference and we cannot iput it under inode_lock. So we
910 * keep the reference and iput it later. */
911 old_inode = inode;
912 spin_lock(&inode_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 }
Christoph Hellwigd003fb72007-02-12 00:51:58 -0800914 spin_unlock(&inode_lock);
Jan Kara941d2382008-02-06 01:37:36 -0800915 iput(old_inode);
Jan Kara0a5a9c72010-02-09 18:20:39 +0100916
Jan Kara62af9b52010-04-19 16:47:20 +0200917#ifdef CONFIG_QUOTA_DEBUG
Jan Kara0a5a9c72010-02-09 18:20:39 +0100918 if (reserved) {
919 printk(KERN_WARNING "VFS (%s): Writes happened before quota"
920 " was turned on thus quota information is probably "
921 "inconsistent. Please run quotacheck(8).\n", sb->s_id);
922 }
Jan Kara4c5e6c02010-04-06 18:52:47 +0200923#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924}
925
Jan Kara268157b2009-01-27 15:47:22 +0100926/*
927 * Return 0 if dqput() won't block.
928 * (note that 1 doesn't necessarily mean blocking)
929 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930static inline int dqput_blocks(struct dquot *dquot)
931{
932 if (atomic_read(&dquot->dq_count) <= 1)
933 return 1;
934 return 0;
935}
936
Jan Kara268157b2009-01-27 15:47:22 +0100937/*
938 * Remove references to dquots from inode and add dquot to list for freeing
939 * if we have the last referece to dquot
940 * We can't race with anybody because we hold dqptr_sem for writing...
941 */
Adrian Bunke5f00f42007-05-08 00:27:22 -0700942static int remove_inode_dquot_ref(struct inode *inode, int type,
943 struct list_head *tofree_head)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944{
945 struct dquot *dquot = inode->i_dquot[type];
946
Jan Karadd6f3c62009-01-26 16:01:43 +0100947 inode->i_dquot[type] = NULL;
948 if (dquot) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 if (dqput_blocks(dquot)) {
Jan Kara62af9b52010-04-19 16:47:20 +0200950#ifdef CONFIG_QUOTA_DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 if (atomic_read(&dquot->dq_count) != 1)
952 printk(KERN_WARNING "VFS: Adding dquot with dq_count %d to dispose list.\n", atomic_read(&dquot->dq_count));
953#endif
954 spin_lock(&dq_list_lock);
Jan Kara268157b2009-01-27 15:47:22 +0100955 /* As dquot must have currently users it can't be on
956 * the free list... */
957 list_add(&dquot->dq_free, tofree_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 spin_unlock(&dq_list_lock);
959 return 1;
960 }
961 else
962 dqput(dquot); /* We have guaranteed we won't block */
963 }
964 return 0;
965}
966
Jan Kara268157b2009-01-27 15:47:22 +0100967/*
968 * Free list of dquots
969 * Dquots are removed from inodes and no new references can be got so we are
970 * the only ones holding reference
971 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972static void put_dquot_list(struct list_head *tofree_head)
973{
974 struct list_head *act_head;
975 struct dquot *dquot;
976
977 act_head = tofree_head->next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 while (act_head != tofree_head) {
979 dquot = list_entry(act_head, struct dquot, dq_free);
980 act_head = act_head->next;
Jan Kara268157b2009-01-27 15:47:22 +0100981 /* Remove dquot from the list so we won't have problems... */
982 list_del_init(&dquot->dq_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 dqput(dquot);
984 }
985}
986
Christoph Hellwigfb58b732007-02-12 00:51:57 -0800987static void remove_dquot_ref(struct super_block *sb, int type,
988 struct list_head *tofree_head)
989{
990 struct inode *inode;
991
992 spin_lock(&inode_lock);
993 list_for_each_entry(inode, &sb->s_inodes, i_sb_list) {
Nick Pigginaabb8fd2009-03-11 13:17:36 -0700994 /*
995 * We have to scan also I_NEW inodes because they can already
996 * have quota pointer initialized. Luckily, we need to touch
997 * only quota pointers and these have separate locking
998 * (dqptr_sem).
999 */
Christoph Hellwigfb58b732007-02-12 00:51:57 -08001000 if (!IS_NOQUOTA(inode))
1001 remove_inode_dquot_ref(inode, type, tofree_head);
1002 }
1003 spin_unlock(&inode_lock);
1004}
1005
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006/* Gather all references from inodes and drop them */
1007static void drop_dquot_ref(struct super_block *sb, int type)
1008{
1009 LIST_HEAD(tofree_head);
1010
Christoph Hellwigfb58b732007-02-12 00:51:57 -08001011 if (sb->dq_op) {
1012 down_write(&sb_dqopt(sb)->dqptr_sem);
1013 remove_dquot_ref(sb, type, &tofree_head);
1014 up_write(&sb_dqopt(sb)->dqptr_sem);
1015 put_dquot_list(&tofree_head);
1016 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017}
1018
Jan Kara12095462008-08-20 14:45:12 +02001019static inline void dquot_incr_inodes(struct dquot *dquot, qsize_t number)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020{
1021 dquot->dq_dqb.dqb_curinodes += number;
1022}
1023
1024static inline void dquot_incr_space(struct dquot *dquot, qsize_t number)
1025{
1026 dquot->dq_dqb.dqb_curspace += number;
1027}
1028
Mingming Caof18df222009-01-13 16:43:09 +01001029static inline void dquot_resv_space(struct dquot *dquot, qsize_t number)
1030{
1031 dquot->dq_dqb.dqb_rsvspace += number;
1032}
1033
Mingming Cao740d9dc2009-01-13 16:43:14 +01001034/*
1035 * Claim reserved quota space
1036 */
Jan Kara0a5a9c72010-02-09 18:20:39 +01001037static void dquot_claim_reserved_space(struct dquot *dquot, qsize_t number)
Mingming Cao740d9dc2009-01-13 16:43:14 +01001038{
Jan Kara0a5a9c72010-02-09 18:20:39 +01001039 if (dquot->dq_dqb.dqb_rsvspace < number) {
1040 WARN_ON_ONCE(1);
1041 number = dquot->dq_dqb.dqb_rsvspace;
1042 }
Mingming Cao740d9dc2009-01-13 16:43:14 +01001043 dquot->dq_dqb.dqb_curspace += number;
1044 dquot->dq_dqb.dqb_rsvspace -= number;
1045}
1046
1047static inline
1048void dquot_free_reserved_space(struct dquot *dquot, qsize_t number)
1049{
Jan Kara0a5a9c72010-02-09 18:20:39 +01001050 if (dquot->dq_dqb.dqb_rsvspace >= number)
1051 dquot->dq_dqb.dqb_rsvspace -= number;
1052 else {
1053 WARN_ON_ONCE(1);
1054 dquot->dq_dqb.dqb_rsvspace = 0;
1055 }
Mingming Cao740d9dc2009-01-13 16:43:14 +01001056}
1057
Jan Kara7a2435d2009-01-27 01:47:11 +01001058static void dquot_decr_inodes(struct dquot *dquot, qsize_t number)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059{
Jan Karadb49d2d2008-10-01 18:21:39 +02001060 if (sb_dqopt(dquot->dq_sb)->flags & DQUOT_NEGATIVE_USAGE ||
1061 dquot->dq_dqb.dqb_curinodes >= number)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 dquot->dq_dqb.dqb_curinodes -= number;
1063 else
1064 dquot->dq_dqb.dqb_curinodes = 0;
1065 if (dquot->dq_dqb.dqb_curinodes <= dquot->dq_dqb.dqb_isoftlimit)
1066 dquot->dq_dqb.dqb_itime = (time_t) 0;
1067 clear_bit(DQ_INODES_B, &dquot->dq_flags);
1068}
1069
Jan Kara7a2435d2009-01-27 01:47:11 +01001070static void dquot_decr_space(struct dquot *dquot, qsize_t number)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071{
Jan Karadb49d2d2008-10-01 18:21:39 +02001072 if (sb_dqopt(dquot->dq_sb)->flags & DQUOT_NEGATIVE_USAGE ||
1073 dquot->dq_dqb.dqb_curspace >= number)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 dquot->dq_dqb.dqb_curspace -= number;
1075 else
1076 dquot->dq_dqb.dqb_curspace = 0;
Jan Kara12095462008-08-20 14:45:12 +02001077 if (dquot->dq_dqb.dqb_curspace <= dquot->dq_dqb.dqb_bsoftlimit)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 dquot->dq_dqb.dqb_btime = (time_t) 0;
1079 clear_bit(DQ_BLKS_B, &dquot->dq_flags);
1080}
1081
Jan Karac5254602007-12-22 14:03:25 -08001082static int warning_issued(struct dquot *dquot, const int warntype)
1083{
1084 int flag = (warntype == QUOTA_NL_BHARDWARN ||
1085 warntype == QUOTA_NL_BSOFTLONGWARN) ? DQ_BLKS_B :
1086 ((warntype == QUOTA_NL_IHARDWARN ||
1087 warntype == QUOTA_NL_ISOFTLONGWARN) ? DQ_INODES_B : 0);
1088
1089 if (!flag)
1090 return 0;
1091 return test_and_set_bit(flag, &dquot->dq_flags);
1092}
1093
Jan Kara8e893462007-10-16 23:29:31 -07001094#ifdef CONFIG_PRINT_QUOTA_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095static int flag_print_warnings = 1;
1096
Jan Kara7a2435d2009-01-27 01:47:11 +01001097static int need_print_warning(struct dquot *dquot)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098{
1099 if (!flag_print_warnings)
1100 return 0;
1101
1102 switch (dquot->dq_type) {
1103 case USRQUOTA:
David Howellsda9592e2008-11-14 10:39:05 +11001104 return current_fsuid() == dquot->dq_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 case GRPQUOTA:
1106 return in_group_p(dquot->dq_id);
1107 }
1108 return 0;
1109}
1110
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111/* Print warning to user which exceeded quota */
Jan Karac5254602007-12-22 14:03:25 -08001112static void print_warning(struct dquot *dquot, const int warntype)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113{
1114 char *msg = NULL;
Peter Zijlstra24ec8392006-12-08 02:36:04 -08001115 struct tty_struct *tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116
Jan Kara657d3bf2008-07-25 01:46:52 -07001117 if (warntype == QUOTA_NL_IHARDBELOW ||
1118 warntype == QUOTA_NL_ISOFTBELOW ||
1119 warntype == QUOTA_NL_BHARDBELOW ||
1120 warntype == QUOTA_NL_BSOFTBELOW || !need_print_warning(dquot))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 return;
1122
Peter Zijlstra24ec8392006-12-08 02:36:04 -08001123 tty = get_current_tty();
1124 if (!tty)
Alan Cox452a00d2008-10-13 10:39:13 +01001125 return;
Peter Zijlstra24ec8392006-12-08 02:36:04 -08001126 tty_write_message(tty, dquot->dq_sb->s_id);
Jan Kara8e893462007-10-16 23:29:31 -07001127 if (warntype == QUOTA_NL_ISOFTWARN || warntype == QUOTA_NL_BSOFTWARN)
Peter Zijlstra24ec8392006-12-08 02:36:04 -08001128 tty_write_message(tty, ": warning, ");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 else
Peter Zijlstra24ec8392006-12-08 02:36:04 -08001130 tty_write_message(tty, ": write failed, ");
1131 tty_write_message(tty, quotatypes[dquot->dq_type]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 switch (warntype) {
Jan Kara8e893462007-10-16 23:29:31 -07001133 case QUOTA_NL_IHARDWARN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 msg = " file limit reached.\r\n";
1135 break;
Jan Kara8e893462007-10-16 23:29:31 -07001136 case QUOTA_NL_ISOFTLONGWARN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 msg = " file quota exceeded too long.\r\n";
1138 break;
Jan Kara8e893462007-10-16 23:29:31 -07001139 case QUOTA_NL_ISOFTWARN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 msg = " file quota exceeded.\r\n";
1141 break;
Jan Kara8e893462007-10-16 23:29:31 -07001142 case QUOTA_NL_BHARDWARN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 msg = " block limit reached.\r\n";
1144 break;
Jan Kara8e893462007-10-16 23:29:31 -07001145 case QUOTA_NL_BSOFTLONGWARN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 msg = " block quota exceeded too long.\r\n";
1147 break;
Jan Kara8e893462007-10-16 23:29:31 -07001148 case QUOTA_NL_BSOFTWARN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 msg = " block quota exceeded.\r\n";
1150 break;
1151 }
Peter Zijlstra24ec8392006-12-08 02:36:04 -08001152 tty_write_message(tty, msg);
Alan Cox452a00d2008-10-13 10:39:13 +01001153 tty_kref_put(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154}
Jan Kara8e893462007-10-16 23:29:31 -07001155#endif
1156
Mingming Caof18df222009-01-13 16:43:09 +01001157/*
1158 * Write warnings to the console and send warning messages over netlink.
1159 *
1160 * Note that this function can sleep.
1161 */
Jan Kara7a2435d2009-01-27 01:47:11 +01001162static void flush_warnings(struct dquot *const *dquots, char *warntype)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163{
Steven Whitehouse86e931a2009-09-28 12:35:17 +01001164 struct dquot *dq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 int i;
1166
Steven Whitehouse86e931a2009-09-28 12:35:17 +01001167 for (i = 0; i < MAXQUOTAS; i++) {
1168 dq = dquots[i];
1169 if (dq && warntype[i] != QUOTA_NL_NOWARN &&
1170 !warning_issued(dq, warntype[i])) {
Jan Kara8e893462007-10-16 23:29:31 -07001171#ifdef CONFIG_PRINT_QUOTA_WARNING
Steven Whitehouse86e931a2009-09-28 12:35:17 +01001172 print_warning(dq, warntype[i]);
Jan Kara8e893462007-10-16 23:29:31 -07001173#endif
Steven Whitehouse86e931a2009-09-28 12:35:17 +01001174 quota_send_warning(dq->dq_type, dq->dq_id,
1175 dq->dq_sb->s_dev, warntype[i]);
Jan Kara8e893462007-10-16 23:29:31 -07001176 }
Steven Whitehouse86e931a2009-09-28 12:35:17 +01001177 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178}
1179
Jan Kara7a2435d2009-01-27 01:47:11 +01001180static int ignore_hardlimit(struct dquot *dquot)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181{
1182 struct mem_dqinfo *info = &sb_dqopt(dquot->dq_sb)->info[dquot->dq_type];
1183
1184 return capable(CAP_SYS_RESOURCE) &&
Jan Kara268157b2009-01-27 15:47:22 +01001185 (info->dqi_format->qf_fmt_id != QFMT_VFS_OLD ||
1186 !(info->dqi_flags & V1_DQF_RSQUASH));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187}
1188
1189/* needs dq_data_lock */
Jan Kara12095462008-08-20 14:45:12 +02001190static int check_idq(struct dquot *dquot, qsize_t inodes, char *warntype)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191{
Jan Kara268157b2009-01-27 15:47:22 +01001192 qsize_t newinodes = dquot->dq_dqb.dqb_curinodes + inodes;
1193
Jan Kara8e893462007-10-16 23:29:31 -07001194 *warntype = QUOTA_NL_NOWARN;
Jan Karaf55abc02008-08-20 17:50:32 +02001195 if (!sb_has_quota_limits_enabled(dquot->dq_sb, dquot->dq_type) ||
1196 test_bit(DQ_FAKE_B, &dquot->dq_flags))
Christoph Hellwigefd8f0e2010-03-03 09:05:08 -05001197 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198
1199 if (dquot->dq_dqb.dqb_ihardlimit &&
Jan Kara268157b2009-01-27 15:47:22 +01001200 newinodes > dquot->dq_dqb.dqb_ihardlimit &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 !ignore_hardlimit(dquot)) {
Jan Kara8e893462007-10-16 23:29:31 -07001202 *warntype = QUOTA_NL_IHARDWARN;
Christoph Hellwigefd8f0e2010-03-03 09:05:08 -05001203 return -EDQUOT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 }
1205
1206 if (dquot->dq_dqb.dqb_isoftlimit &&
Jan Kara268157b2009-01-27 15:47:22 +01001207 newinodes > dquot->dq_dqb.dqb_isoftlimit &&
1208 dquot->dq_dqb.dqb_itime &&
1209 get_seconds() >= dquot->dq_dqb.dqb_itime &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 !ignore_hardlimit(dquot)) {
Jan Kara8e893462007-10-16 23:29:31 -07001211 *warntype = QUOTA_NL_ISOFTLONGWARN;
Christoph Hellwigefd8f0e2010-03-03 09:05:08 -05001212 return -EDQUOT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213 }
1214
1215 if (dquot->dq_dqb.dqb_isoftlimit &&
Jan Kara268157b2009-01-27 15:47:22 +01001216 newinodes > dquot->dq_dqb.dqb_isoftlimit &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 dquot->dq_dqb.dqb_itime == 0) {
Jan Kara8e893462007-10-16 23:29:31 -07001218 *warntype = QUOTA_NL_ISOFTWARN;
Jan Kara268157b2009-01-27 15:47:22 +01001219 dquot->dq_dqb.dqb_itime = get_seconds() +
1220 sb_dqopt(dquot->dq_sb)->info[dquot->dq_type].dqi_igrace;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 }
1222
Christoph Hellwigefd8f0e2010-03-03 09:05:08 -05001223 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224}
1225
1226/* needs dq_data_lock */
1227static int check_bdq(struct dquot *dquot, qsize_t space, int prealloc, char *warntype)
1228{
Mingming Caof18df222009-01-13 16:43:09 +01001229 qsize_t tspace;
Jan Kara268157b2009-01-27 15:47:22 +01001230 struct super_block *sb = dquot->dq_sb;
Mingming Caof18df222009-01-13 16:43:09 +01001231
Jan Kara8e893462007-10-16 23:29:31 -07001232 *warntype = QUOTA_NL_NOWARN;
Jan Kara268157b2009-01-27 15:47:22 +01001233 if (!sb_has_quota_limits_enabled(sb, dquot->dq_type) ||
Jan Karaf55abc02008-08-20 17:50:32 +02001234 test_bit(DQ_FAKE_B, &dquot->dq_flags))
Christoph Hellwigefd8f0e2010-03-03 09:05:08 -05001235 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236
Mingming Caof18df222009-01-13 16:43:09 +01001237 tspace = dquot->dq_dqb.dqb_curspace + dquot->dq_dqb.dqb_rsvspace
1238 + space;
1239
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 if (dquot->dq_dqb.dqb_bhardlimit &&
Mingming Caof18df222009-01-13 16:43:09 +01001241 tspace > dquot->dq_dqb.dqb_bhardlimit &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 !ignore_hardlimit(dquot)) {
1243 if (!prealloc)
Jan Kara8e893462007-10-16 23:29:31 -07001244 *warntype = QUOTA_NL_BHARDWARN;
Christoph Hellwigefd8f0e2010-03-03 09:05:08 -05001245 return -EDQUOT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 }
1247
1248 if (dquot->dq_dqb.dqb_bsoftlimit &&
Mingming Caof18df222009-01-13 16:43:09 +01001249 tspace > dquot->dq_dqb.dqb_bsoftlimit &&
Jan Kara268157b2009-01-27 15:47:22 +01001250 dquot->dq_dqb.dqb_btime &&
1251 get_seconds() >= dquot->dq_dqb.dqb_btime &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 !ignore_hardlimit(dquot)) {
1253 if (!prealloc)
Jan Kara8e893462007-10-16 23:29:31 -07001254 *warntype = QUOTA_NL_BSOFTLONGWARN;
Christoph Hellwigefd8f0e2010-03-03 09:05:08 -05001255 return -EDQUOT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 }
1257
1258 if (dquot->dq_dqb.dqb_bsoftlimit &&
Mingming Caof18df222009-01-13 16:43:09 +01001259 tspace > dquot->dq_dqb.dqb_bsoftlimit &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 dquot->dq_dqb.dqb_btime == 0) {
1261 if (!prealloc) {
Jan Kara8e893462007-10-16 23:29:31 -07001262 *warntype = QUOTA_NL_BSOFTWARN;
Jan Kara268157b2009-01-27 15:47:22 +01001263 dquot->dq_dqb.dqb_btime = get_seconds() +
1264 sb_dqopt(sb)->info[dquot->dq_type].dqi_bgrace;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 }
1266 else
1267 /*
1268 * We don't allow preallocation to exceed softlimit so exceeding will
1269 * be always printed
1270 */
Christoph Hellwigefd8f0e2010-03-03 09:05:08 -05001271 return -EDQUOT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272 }
1273
Christoph Hellwigefd8f0e2010-03-03 09:05:08 -05001274 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275}
1276
Jan Kara12095462008-08-20 14:45:12 +02001277static int info_idq_free(struct dquot *dquot, qsize_t inodes)
Jan Kara657d3bf2008-07-25 01:46:52 -07001278{
Jan Kara268157b2009-01-27 15:47:22 +01001279 qsize_t newinodes;
1280
Jan Kara657d3bf2008-07-25 01:46:52 -07001281 if (test_bit(DQ_FAKE_B, &dquot->dq_flags) ||
Jan Karaf55abc02008-08-20 17:50:32 +02001282 dquot->dq_dqb.dqb_curinodes <= dquot->dq_dqb.dqb_isoftlimit ||
1283 !sb_has_quota_limits_enabled(dquot->dq_sb, dquot->dq_type))
Jan Kara657d3bf2008-07-25 01:46:52 -07001284 return QUOTA_NL_NOWARN;
1285
Jan Kara268157b2009-01-27 15:47:22 +01001286 newinodes = dquot->dq_dqb.dqb_curinodes - inodes;
1287 if (newinodes <= dquot->dq_dqb.dqb_isoftlimit)
Jan Kara657d3bf2008-07-25 01:46:52 -07001288 return QUOTA_NL_ISOFTBELOW;
1289 if (dquot->dq_dqb.dqb_curinodes >= dquot->dq_dqb.dqb_ihardlimit &&
Jan Kara268157b2009-01-27 15:47:22 +01001290 newinodes < dquot->dq_dqb.dqb_ihardlimit)
Jan Kara657d3bf2008-07-25 01:46:52 -07001291 return QUOTA_NL_IHARDBELOW;
1292 return QUOTA_NL_NOWARN;
1293}
1294
1295static int info_bdq_free(struct dquot *dquot, qsize_t space)
1296{
1297 if (test_bit(DQ_FAKE_B, &dquot->dq_flags) ||
Jan Kara12095462008-08-20 14:45:12 +02001298 dquot->dq_dqb.dqb_curspace <= dquot->dq_dqb.dqb_bsoftlimit)
Jan Kara657d3bf2008-07-25 01:46:52 -07001299 return QUOTA_NL_NOWARN;
1300
Jan Kara12095462008-08-20 14:45:12 +02001301 if (dquot->dq_dqb.dqb_curspace - space <= dquot->dq_dqb.dqb_bsoftlimit)
Jan Kara657d3bf2008-07-25 01:46:52 -07001302 return QUOTA_NL_BSOFTBELOW;
Jan Kara12095462008-08-20 14:45:12 +02001303 if (dquot->dq_dqb.dqb_curspace >= dquot->dq_dqb.dqb_bhardlimit &&
1304 dquot->dq_dqb.dqb_curspace - space < dquot->dq_dqb.dqb_bhardlimit)
Jan Kara657d3bf2008-07-25 01:46:52 -07001305 return QUOTA_NL_BHARDBELOW;
1306 return QUOTA_NL_NOWARN;
1307}
Jan Kara0a5a9c72010-02-09 18:20:39 +01001308
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309/*
Christoph Hellwig871a2932010-03-03 09:05:07 -05001310 * Initialize quota pointers in inode
1311 *
1312 * We do things in a bit complicated way but by that we avoid calling
1313 * dqget() and thus filesystem callbacks under dqptr_sem.
1314 *
1315 * It is better to call this function outside of any transaction as it
1316 * might need a lot of space in journal for dquot structure allocation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317 */
Christoph Hellwig871a2932010-03-03 09:05:07 -05001318static void __dquot_initialize(struct inode *inode, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319{
1320 unsigned int id = 0;
Christoph Hellwig871a2932010-03-03 09:05:07 -05001321 int cnt;
Dmitry Monakhovad1e6e82010-02-16 08:31:49 +03001322 struct dquot *got[MAXQUOTAS];
Jan Karacc334122009-01-12 17:23:05 +01001323 struct super_block *sb = inode->i_sb;
Jan Kara0a5a9c72010-02-09 18:20:39 +01001324 qsize_t rsv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325
Ingo Molnard3be9152006-03-23 03:00:29 -08001326 /* First test before acquiring mutex - solves deadlocks when we
1327 * re-enter the quota code and are already holding the mutex */
Christoph Hellwig871a2932010-03-03 09:05:07 -05001328 if (!sb_any_quota_active(inode->i_sb) || IS_NOQUOTA(inode))
1329 return;
Jan Karacc334122009-01-12 17:23:05 +01001330
1331 /* First get references to structures we might need. */
1332 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
Dmitry Monakhovad1e6e82010-02-16 08:31:49 +03001333 got[cnt] = NULL;
Jan Karacc334122009-01-12 17:23:05 +01001334 if (type != -1 && cnt != type)
1335 continue;
1336 switch (cnt) {
1337 case USRQUOTA:
1338 id = inode->i_uid;
1339 break;
1340 case GRPQUOTA:
1341 id = inode->i_gid;
1342 break;
1343 }
1344 got[cnt] = dqget(sb, id, cnt);
1345 }
1346
1347 down_write(&sb_dqopt(sb)->dqptr_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 if (IS_NOQUOTA(inode))
1349 goto out_err;
1350 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1351 if (type != -1 && cnt != type)
1352 continue;
Jan Karacc334122009-01-12 17:23:05 +01001353 /* Avoid races with quotaoff() */
1354 if (!sb_has_quota_active(sb, cnt))
1355 continue;
Jan Karadd6f3c62009-01-26 16:01:43 +01001356 if (!inode->i_dquot[cnt]) {
Jan Karacc334122009-01-12 17:23:05 +01001357 inode->i_dquot[cnt] = got[cnt];
Jan Karadd6f3c62009-01-26 16:01:43 +01001358 got[cnt] = NULL;
Jan Kara0a5a9c72010-02-09 18:20:39 +01001359 /*
1360 * Make quota reservation system happy if someone
1361 * did a write before quota was turned on
1362 */
1363 rsv = inode_get_rsv_space(inode);
1364 if (unlikely(rsv))
1365 dquot_resv_space(inode->i_dquot[cnt], rsv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 }
1367 }
1368out_err:
Jan Karacc334122009-01-12 17:23:05 +01001369 up_write(&sb_dqopt(sb)->dqptr_sem);
1370 /* Drop unused references */
Dmitry Monakhovdc52dd32009-12-14 15:21:15 +03001371 dqput_all(got);
Christoph Hellwig871a2932010-03-03 09:05:07 -05001372}
1373
1374void dquot_initialize(struct inode *inode)
1375{
1376 __dquot_initialize(inode, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377}
Mingming Cao08d03502009-01-14 16:19:32 +01001378EXPORT_SYMBOL(dquot_initialize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379
1380/*
1381 * Release all quotas referenced by inode
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 */
Christoph Hellwig9f754752010-03-03 09:05:05 -05001383static void __dquot_drop(struct inode *inode)
Jan Kara3d9ea252008-10-10 16:12:23 +02001384{
Jan Karacc334122009-01-12 17:23:05 +01001385 int cnt;
1386 struct dquot *put[MAXQUOTAS];
1387
Jan Kara3d9ea252008-10-10 16:12:23 +02001388 down_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
Jan Karacc334122009-01-12 17:23:05 +01001389 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1390 put[cnt] = inode->i_dquot[cnt];
Jan Karadd6f3c62009-01-26 16:01:43 +01001391 inode->i_dquot[cnt] = NULL;
Jan Karacc334122009-01-12 17:23:05 +01001392 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393 up_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
Dmitry Monakhovdc52dd32009-12-14 15:21:15 +03001394 dqput_all(put);
Christoph Hellwig9f754752010-03-03 09:05:05 -05001395}
1396
1397void dquot_drop(struct inode *inode)
1398{
1399 int cnt;
1400
1401 if (IS_NOQUOTA(inode))
1402 return;
1403
1404 /*
1405 * Test before calling to rule out calls from proc and such
1406 * where we are not allowed to block. Note that this is
1407 * actually reliable test even without the lock - the caller
1408 * must assure that nobody can come after the DQUOT_DROP and
1409 * add quota pointers back anyway.
1410 */
1411 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1412 if (inode->i_dquot[cnt])
1413 break;
1414 }
1415
1416 if (cnt < MAXQUOTAS)
1417 __dquot_drop(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418}
Mingming Cao08d03502009-01-14 16:19:32 +01001419EXPORT_SYMBOL(dquot_drop);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420
1421/*
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001422 * inode_reserved_space is managed internally by quota, and protected by
1423 * i_lock similar to i_blocks+i_bytes.
1424 */
1425static qsize_t *inode_reserved_space(struct inode * inode)
1426{
1427 /* Filesystem must explicitly define it's own method in order to use
1428 * quota reservation interface */
1429 BUG_ON(!inode->i_sb->dq_op->get_reserved_space);
1430 return inode->i_sb->dq_op->get_reserved_space(inode);
1431}
1432
Dmitry Monakhovc4690702010-02-09 17:53:36 +01001433void inode_add_rsv_space(struct inode *inode, qsize_t number)
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001434{
1435 spin_lock(&inode->i_lock);
1436 *inode_reserved_space(inode) += number;
1437 spin_unlock(&inode->i_lock);
1438}
Dmitry Monakhovc4690702010-02-09 17:53:36 +01001439EXPORT_SYMBOL(inode_add_rsv_space);
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001440
Dmitry Monakhovc4690702010-02-09 17:53:36 +01001441void inode_claim_rsv_space(struct inode *inode, qsize_t number)
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001442{
1443 spin_lock(&inode->i_lock);
1444 *inode_reserved_space(inode) -= number;
1445 __inode_add_bytes(inode, number);
1446 spin_unlock(&inode->i_lock);
1447}
Dmitry Monakhovc4690702010-02-09 17:53:36 +01001448EXPORT_SYMBOL(inode_claim_rsv_space);
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001449
Dmitry Monakhovc4690702010-02-09 17:53:36 +01001450void inode_sub_rsv_space(struct inode *inode, qsize_t number)
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001451{
1452 spin_lock(&inode->i_lock);
1453 *inode_reserved_space(inode) -= number;
1454 spin_unlock(&inode->i_lock);
1455}
Dmitry Monakhovc4690702010-02-09 17:53:36 +01001456EXPORT_SYMBOL(inode_sub_rsv_space);
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001457
1458static qsize_t inode_get_rsv_space(struct inode *inode)
1459{
1460 qsize_t ret;
Jan Kara05b5d892010-01-06 18:03:36 +01001461
1462 if (!inode->i_sb->dq_op->get_reserved_space)
1463 return 0;
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001464 spin_lock(&inode->i_lock);
1465 ret = *inode_reserved_space(inode);
1466 spin_unlock(&inode->i_lock);
1467 return ret;
1468}
1469
1470static void inode_incr_space(struct inode *inode, qsize_t number,
1471 int reserve)
1472{
1473 if (reserve)
1474 inode_add_rsv_space(inode, number);
1475 else
1476 inode_add_bytes(inode, number);
1477}
1478
1479static void inode_decr_space(struct inode *inode, qsize_t number, int reserve)
1480{
1481 if (reserve)
1482 inode_sub_rsv_space(inode, number);
1483 else
1484 inode_sub_bytes(inode, number);
1485}
1486
1487/*
Christoph Hellwig5dd40562010-03-03 09:05:00 -05001488 * This functions updates i_blocks+i_bytes fields and quota information
1489 * (together with appropriate checks).
1490 *
1491 * NOTE: We absolutely rely on the fact that caller dirties the inode
1492 * (usually helpers in quotaops.h care about this) and holds a handle for
1493 * the current transaction so that dquot write and inode write go into the
1494 * same transaction.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 */
1496
1497/*
1498 * This operation can block, but only after everything is updated
1499 */
Mingming Caof18df222009-01-13 16:43:09 +01001500int __dquot_alloc_space(struct inode *inode, qsize_t number,
Christoph Hellwig5dd40562010-03-03 09:05:00 -05001501 int warn, int reserve)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502{
Christoph Hellwig5dd40562010-03-03 09:05:00 -05001503 int cnt, ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504 char warntype[MAXQUOTAS];
1505
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001506 /*
1507 * First test before acquiring mutex - solves deadlocks when we
1508 * re-enter the quota code and are already holding the mutex
1509 */
Christoph Hellwig5dd40562010-03-03 09:05:00 -05001510 if (!sb_any_quota_active(inode->i_sb) || IS_NOQUOTA(inode)) {
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001511 inode_incr_space(inode, number, reserve);
1512 goto out;
1513 }
1514
1515 down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
Jan Kara8e893462007-10-16 23:29:31 -07001517 warntype[cnt] = QUOTA_NL_NOWARN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519 spin_lock(&dq_data_lock);
1520 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
Jan Karadd6f3c62009-01-26 16:01:43 +01001521 if (!inode->i_dquot[cnt])
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522 continue;
Christoph Hellwigefd8f0e2010-03-03 09:05:08 -05001523 ret = check_bdq(inode->i_dquot[cnt], number, !warn,
1524 warntype+cnt);
1525 if (ret) {
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001526 spin_unlock(&dq_data_lock);
1527 goto out_flush_warn;
Mingming Caof18df222009-01-13 16:43:09 +01001528 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529 }
1530 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
Jan Karadd6f3c62009-01-26 16:01:43 +01001531 if (!inode->i_dquot[cnt])
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532 continue;
Mingming Caof18df222009-01-13 16:43:09 +01001533 if (reserve)
1534 dquot_resv_space(inode->i_dquot[cnt], number);
1535 else
1536 dquot_incr_space(inode->i_dquot[cnt], number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537 }
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001538 inode_incr_space(inode, number, reserve);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539 spin_unlock(&dq_data_lock);
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001540
1541 if (reserve)
1542 goto out_flush_warn;
Dmitry Monakhovdc52dd32009-12-14 15:21:15 +03001543 mark_all_dquot_dirty(inode->i_dquot);
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001544out_flush_warn:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545 flush_warnings(inode->i_dquot, warntype);
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001546 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1547out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548 return ret;
1549}
Christoph Hellwig5dd40562010-03-03 09:05:00 -05001550EXPORT_SYMBOL(__dquot_alloc_space);
Mingming Caof18df222009-01-13 16:43:09 +01001551
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552/*
1553 * This operation can block, but only after everything is updated
1554 */
Christoph Hellwig63936dd2010-03-03 09:05:01 -05001555int dquot_alloc_inode(const struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556{
Christoph Hellwigefd8f0e2010-03-03 09:05:08 -05001557 int cnt, ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558 char warntype[MAXQUOTAS];
1559
Ingo Molnard3be9152006-03-23 03:00:29 -08001560 /* First test before acquiring mutex - solves deadlocks when we
1561 * re-enter the quota code and are already holding the mutex */
Christoph Hellwig63936dd2010-03-03 09:05:01 -05001562 if (!sb_any_quota_active(inode->i_sb) || IS_NOQUOTA(inode))
1563 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
Jan Kara8e893462007-10-16 23:29:31 -07001565 warntype[cnt] = QUOTA_NL_NOWARN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566 down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567 spin_lock(&dq_data_lock);
1568 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
Jan Karadd6f3c62009-01-26 16:01:43 +01001569 if (!inode->i_dquot[cnt])
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570 continue;
Christoph Hellwigefd8f0e2010-03-03 09:05:08 -05001571 ret = check_idq(inode->i_dquot[cnt], 1, warntype + cnt);
1572 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573 goto warn_put_all;
1574 }
1575
1576 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
Jan Karadd6f3c62009-01-26 16:01:43 +01001577 if (!inode->i_dquot[cnt])
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578 continue;
Christoph Hellwig63936dd2010-03-03 09:05:01 -05001579 dquot_incr_inodes(inode->i_dquot[cnt], 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580 }
Christoph Hellwigefd8f0e2010-03-03 09:05:08 -05001581
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582warn_put_all:
1583 spin_unlock(&dq_data_lock);
Christoph Hellwig63936dd2010-03-03 09:05:01 -05001584 if (ret == 0)
Dmitry Monakhovdc52dd32009-12-14 15:21:15 +03001585 mark_all_dquot_dirty(inode->i_dquot);
Jan Kara087ee8d2007-12-17 16:20:26 -08001586 flush_warnings(inode->i_dquot, warntype);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1588 return ret;
1589}
Mingming Cao08d03502009-01-14 16:19:32 +01001590EXPORT_SYMBOL(dquot_alloc_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591
Christoph Hellwig5dd40562010-03-03 09:05:00 -05001592/*
1593 * Convert in-memory reserved quotas to real consumed quotas
1594 */
1595int dquot_claim_space_nodirty(struct inode *inode, qsize_t number)
Mingming Cao740d9dc2009-01-13 16:43:14 +01001596{
1597 int cnt;
Mingming Cao740d9dc2009-01-13 16:43:14 +01001598
Christoph Hellwig5dd40562010-03-03 09:05:00 -05001599 if (!sb_any_quota_active(inode->i_sb) || IS_NOQUOTA(inode)) {
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001600 inode_claim_rsv_space(inode, number);
Christoph Hellwig5dd40562010-03-03 09:05:00 -05001601 return 0;
Mingming Cao740d9dc2009-01-13 16:43:14 +01001602 }
1603
1604 down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
Mingming Cao740d9dc2009-01-13 16:43:14 +01001605 spin_lock(&dq_data_lock);
1606 /* Claim reserved quotas to allocated quotas */
1607 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
Jan Karadd6f3c62009-01-26 16:01:43 +01001608 if (inode->i_dquot[cnt])
Mingming Cao740d9dc2009-01-13 16:43:14 +01001609 dquot_claim_reserved_space(inode->i_dquot[cnt],
1610 number);
1611 }
1612 /* Update inode bytes */
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001613 inode_claim_rsv_space(inode, number);
Mingming Cao740d9dc2009-01-13 16:43:14 +01001614 spin_unlock(&dq_data_lock);
Dmitry Monakhovdc52dd32009-12-14 15:21:15 +03001615 mark_all_dquot_dirty(inode->i_dquot);
Mingming Cao740d9dc2009-01-13 16:43:14 +01001616 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
Christoph Hellwig5dd40562010-03-03 09:05:00 -05001617 return 0;
Mingming Cao740d9dc2009-01-13 16:43:14 +01001618}
Christoph Hellwig5dd40562010-03-03 09:05:00 -05001619EXPORT_SYMBOL(dquot_claim_space_nodirty);
Mingming Cao740d9dc2009-01-13 16:43:14 +01001620
1621/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622 * This operation can block, but only after everything is updated
1623 */
Christoph Hellwig5dd40562010-03-03 09:05:00 -05001624void __dquot_free_space(struct inode *inode, qsize_t number, int reserve)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625{
1626 unsigned int cnt;
Jan Kara657d3bf2008-07-25 01:46:52 -07001627 char warntype[MAXQUOTAS];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628
Ingo Molnard3be9152006-03-23 03:00:29 -08001629 /* First test before acquiring mutex - solves deadlocks when we
1630 * re-enter the quota code and are already holding the mutex */
Christoph Hellwig5dd40562010-03-03 09:05:00 -05001631 if (!sb_any_quota_active(inode->i_sb) || IS_NOQUOTA(inode)) {
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001632 inode_decr_space(inode, number, reserve);
Christoph Hellwig5dd40562010-03-03 09:05:00 -05001633 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634 }
Jan Kara657d3bf2008-07-25 01:46:52 -07001635
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636 down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637 spin_lock(&dq_data_lock);
1638 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
Jan Karadd6f3c62009-01-26 16:01:43 +01001639 if (!inode->i_dquot[cnt])
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640 continue;
Jan Kara657d3bf2008-07-25 01:46:52 -07001641 warntype[cnt] = info_bdq_free(inode->i_dquot[cnt], number);
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001642 if (reserve)
1643 dquot_free_reserved_space(inode->i_dquot[cnt], number);
1644 else
1645 dquot_decr_space(inode->i_dquot[cnt], number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646 }
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001647 inode_decr_space(inode, number, reserve);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648 spin_unlock(&dq_data_lock);
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001649
1650 if (reserve)
1651 goto out_unlock;
Dmitry Monakhovdc52dd32009-12-14 15:21:15 +03001652 mark_all_dquot_dirty(inode->i_dquot);
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001653out_unlock:
Jan Kara657d3bf2008-07-25 01:46:52 -07001654 flush_warnings(inode->i_dquot, warntype);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656}
Christoph Hellwig5dd40562010-03-03 09:05:00 -05001657EXPORT_SYMBOL(__dquot_free_space);
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001658
1659/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660 * This operation can block, but only after everything is updated
1661 */
Christoph Hellwig63936dd2010-03-03 09:05:01 -05001662void dquot_free_inode(const struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663{
1664 unsigned int cnt;
Jan Kara657d3bf2008-07-25 01:46:52 -07001665 char warntype[MAXQUOTAS];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666
Ingo Molnard3be9152006-03-23 03:00:29 -08001667 /* First test before acquiring mutex - solves deadlocks when we
1668 * re-enter the quota code and are already holding the mutex */
Christoph Hellwig63936dd2010-03-03 09:05:01 -05001669 if (!sb_any_quota_active(inode->i_sb) || IS_NOQUOTA(inode))
1670 return;
Jan Kara657d3bf2008-07-25 01:46:52 -07001671
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672 down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673 spin_lock(&dq_data_lock);
1674 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
Jan Karadd6f3c62009-01-26 16:01:43 +01001675 if (!inode->i_dquot[cnt])
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676 continue;
Christoph Hellwig63936dd2010-03-03 09:05:01 -05001677 warntype[cnt] = info_idq_free(inode->i_dquot[cnt], 1);
1678 dquot_decr_inodes(inode->i_dquot[cnt], 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679 }
1680 spin_unlock(&dq_data_lock);
Dmitry Monakhovdc52dd32009-12-14 15:21:15 +03001681 mark_all_dquot_dirty(inode->i_dquot);
Jan Kara657d3bf2008-07-25 01:46:52 -07001682 flush_warnings(inode->i_dquot, warntype);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684}
Mingming Cao08d03502009-01-14 16:19:32 +01001685EXPORT_SYMBOL(dquot_free_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686
1687/*
1688 * Transfer the number of inode and blocks from one diskquota to an other.
1689 *
1690 * This operation can block, but only after everything is updated
1691 * A transaction must be started when entering this function.
1692 */
Christoph Hellwigb43fa822010-03-03 09:05:03 -05001693static int __dquot_transfer(struct inode *inode, qid_t *chid, unsigned long mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694{
Mingming Cao740d9dc2009-01-13 16:43:14 +01001695 qsize_t space, cur_space;
1696 qsize_t rsv_space = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697 struct dquot *transfer_from[MAXQUOTAS];
1698 struct dquot *transfer_to[MAXQUOTAS];
Christoph Hellwigefd8f0e2010-03-03 09:05:08 -05001699 int cnt, ret = 0;
Jan Kara657d3bf2008-07-25 01:46:52 -07001700 char warntype_to[MAXQUOTAS];
1701 char warntype_from_inodes[MAXQUOTAS], warntype_from_space[MAXQUOTAS];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702
Ingo Molnard3be9152006-03-23 03:00:29 -08001703 /* First test before acquiring mutex - solves deadlocks when we
1704 * re-enter the quota code and are already holding the mutex */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705 if (IS_NOQUOTA(inode))
Christoph Hellwigefd8f0e2010-03-03 09:05:08 -05001706 return 0;
Jan Karacc334122009-01-12 17:23:05 +01001707 /* Initialize the arrays */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
Jan Karadd6f3c62009-01-26 16:01:43 +01001709 transfer_from[cnt] = NULL;
1710 transfer_to[cnt] = NULL;
Jan Kara657d3bf2008-07-25 01:46:52 -07001711 warntype_to[cnt] = QUOTA_NL_NOWARN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712 }
Dmitry Monakhov8ddd69d2010-02-16 08:31:50 +03001713 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1714 if (mask & (1 << cnt))
1715 transfer_to[cnt] = dqget(inode->i_sb, chid[cnt], cnt);
1716 }
Jan Karacc334122009-01-12 17:23:05 +01001717 down_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
Jan Karacc334122009-01-12 17:23:05 +01001718 if (IS_NOQUOTA(inode)) { /* File without quota accounting? */
1719 up_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
1720 goto put_all;
1721 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722 spin_lock(&dq_data_lock);
Mingming Cao740d9dc2009-01-13 16:43:14 +01001723 cur_space = inode_get_bytes(inode);
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001724 rsv_space = inode_get_rsv_space(inode);
Mingming Cao740d9dc2009-01-13 16:43:14 +01001725 space = cur_space + rsv_space;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726 /* Build the transfer_from list and check the limits */
1727 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
Jan Karadd6f3c62009-01-26 16:01:43 +01001728 if (!transfer_to[cnt])
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729 continue;
1730 transfer_from[cnt] = inode->i_dquot[cnt];
Christoph Hellwigefd8f0e2010-03-03 09:05:08 -05001731 ret = check_idq(transfer_to[cnt], 1, warntype_to + cnt);
1732 if (ret)
1733 goto over_quota;
1734 ret = check_bdq(transfer_to[cnt], space, 0, warntype_to + cnt);
1735 if (ret)
Jan Karacc334122009-01-12 17:23:05 +01001736 goto over_quota;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737 }
1738
1739 /*
1740 * Finally perform the needed transfer from transfer_from to transfer_to
1741 */
1742 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1743 /*
1744 * Skip changes for same uid or gid or for turned off quota-type.
1745 */
Jan Karadd6f3c62009-01-26 16:01:43 +01001746 if (!transfer_to[cnt])
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747 continue;
1748
1749 /* Due to IO error we might not have transfer_from[] structure */
1750 if (transfer_from[cnt]) {
Jan Kara657d3bf2008-07-25 01:46:52 -07001751 warntype_from_inodes[cnt] =
1752 info_idq_free(transfer_from[cnt], 1);
1753 warntype_from_space[cnt] =
1754 info_bdq_free(transfer_from[cnt], space);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755 dquot_decr_inodes(transfer_from[cnt], 1);
Mingming Cao740d9dc2009-01-13 16:43:14 +01001756 dquot_decr_space(transfer_from[cnt], cur_space);
1757 dquot_free_reserved_space(transfer_from[cnt],
1758 rsv_space);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759 }
1760
1761 dquot_incr_inodes(transfer_to[cnt], 1);
Mingming Cao740d9dc2009-01-13 16:43:14 +01001762 dquot_incr_space(transfer_to[cnt], cur_space);
1763 dquot_resv_space(transfer_to[cnt], rsv_space);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764
1765 inode->i_dquot[cnt] = transfer_to[cnt];
1766 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767 spin_unlock(&dq_data_lock);
Jan Karacc334122009-01-12 17:23:05 +01001768 up_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
1769
Dmitry Monakhovdc52dd32009-12-14 15:21:15 +03001770 mark_all_dquot_dirty(transfer_from);
1771 mark_all_dquot_dirty(transfer_to);
1772 /* The reference we got is transferred to the inode */
1773 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1774 transfer_to[cnt] = NULL;
Jan Karacc334122009-01-12 17:23:05 +01001775warn_put_all:
Jan Kara657d3bf2008-07-25 01:46:52 -07001776 flush_warnings(transfer_to, warntype_to);
1777 flush_warnings(transfer_from, warntype_from_inodes);
1778 flush_warnings(transfer_from, warntype_from_space);
Jan Karacc334122009-01-12 17:23:05 +01001779put_all:
Dmitry Monakhovdc52dd32009-12-14 15:21:15 +03001780 dqput_all(transfer_from);
1781 dqput_all(transfer_to);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782 return ret;
Jan Karacc334122009-01-12 17:23:05 +01001783over_quota:
1784 spin_unlock(&dq_data_lock);
1785 up_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
1786 /* Clear dquot pointers we don't want to dqput() */
1787 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
Jan Karadd6f3c62009-01-26 16:01:43 +01001788 transfer_from[cnt] = NULL;
Jan Karacc334122009-01-12 17:23:05 +01001789 goto warn_put_all;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790}
1791
Dmitry Monakhov8ddd69d2010-02-16 08:31:50 +03001792/* Wrapper for transferring ownership of an inode for uid/gid only
1793 * Called from FSXXX_setattr()
1794 */
Christoph Hellwigb43fa822010-03-03 09:05:03 -05001795int dquot_transfer(struct inode *inode, struct iattr *iattr)
Jan Karab85f4b82008-07-25 01:46:50 -07001796{
Dmitry Monakhov8ddd69d2010-02-16 08:31:50 +03001797 qid_t chid[MAXQUOTAS];
1798 unsigned long mask = 0;
1799
1800 if (iattr->ia_valid & ATTR_UID && iattr->ia_uid != inode->i_uid) {
1801 mask |= 1 << USRQUOTA;
1802 chid[USRQUOTA] = iattr->ia_uid;
1803 }
1804 if (iattr->ia_valid & ATTR_GID && iattr->ia_gid != inode->i_gid) {
1805 mask |= 1 << GRPQUOTA;
1806 chid[GRPQUOTA] = iattr->ia_gid;
1807 }
Jan Karaf55abc02008-08-20 17:50:32 +02001808 if (sb_any_quota_active(inode->i_sb) && !IS_NOQUOTA(inode)) {
Christoph Hellwig871a2932010-03-03 09:05:07 -05001809 dquot_initialize(inode);
Christoph Hellwigefd8f0e2010-03-03 09:05:08 -05001810 return __dquot_transfer(inode, chid, mask);
Jan Karab85f4b82008-07-25 01:46:50 -07001811 }
1812 return 0;
1813}
Christoph Hellwigb43fa822010-03-03 09:05:03 -05001814EXPORT_SYMBOL(dquot_transfer);
Jan Karab85f4b82008-07-25 01:46:50 -07001815
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816/*
1817 * Write info of quota file to disk
1818 */
1819int dquot_commit_info(struct super_block *sb, int type)
1820{
1821 int ret;
1822 struct quota_info *dqopt = sb_dqopt(sb);
1823
Ingo Molnard3be9152006-03-23 03:00:29 -08001824 mutex_lock(&dqopt->dqio_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825 ret = dqopt->ops[type]->write_file_info(sb, type);
Ingo Molnard3be9152006-03-23 03:00:29 -08001826 mutex_unlock(&dqopt->dqio_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827 return ret;
1828}
Mingming Cao08d03502009-01-14 16:19:32 +01001829EXPORT_SYMBOL(dquot_commit_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830
1831/*
1832 * Definitions of diskquota operations.
1833 */
Alexey Dobriyan61e225d2009-09-21 17:01:08 -07001834const struct dquot_operations dquot_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835 .write_dquot = dquot_commit,
1836 .acquire_dquot = dquot_acquire,
1837 .release_dquot = dquot_release,
1838 .mark_dirty = dquot_mark_dquot_dirty,
Jan Kara74f783a2008-08-19 14:51:22 +02001839 .write_info = dquot_commit_info,
1840 .alloc_dquot = dquot_alloc,
1841 .destroy_dquot = dquot_destroy,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842};
1843
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844/*
Christoph Hellwig907f4552010-03-03 09:05:06 -05001845 * Generic helper for ->open on filesystems supporting disk quotas.
1846 */
1847int dquot_file_open(struct inode *inode, struct file *file)
1848{
1849 int error;
1850
1851 error = generic_file_open(inode, file);
1852 if (!error && (file->f_mode & FMODE_WRITE))
Christoph Hellwig871a2932010-03-03 09:05:07 -05001853 dquot_initialize(inode);
Christoph Hellwig907f4552010-03-03 09:05:06 -05001854 return error;
1855}
1856EXPORT_SYMBOL(dquot_file_open);
1857
1858/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859 * Turn quota off on a device. type == -1 ==> quotaoff for all types (umount)
1860 */
Jan Karaf55abc02008-08-20 17:50:32 +02001861int vfs_quota_disable(struct super_block *sb, int type, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862{
Jan Kara0ff5af82008-04-28 02:14:33 -07001863 int cnt, ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864 struct quota_info *dqopt = sb_dqopt(sb);
1865 struct inode *toputinode[MAXQUOTAS];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866
Jan Karaf55abc02008-08-20 17:50:32 +02001867 /* Cannot turn off usage accounting without turning off limits, or
1868 * suspend quotas and simultaneously turn quotas off. */
1869 if ((flags & DQUOT_USAGE_ENABLED && !(flags & DQUOT_LIMITS_ENABLED))
1870 || (flags & DQUOT_SUSPENDED && flags & (DQUOT_LIMITS_ENABLED |
1871 DQUOT_USAGE_ENABLED)))
1872 return -EINVAL;
1873
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874 /* We need to serialize quota_off() for device */
Ingo Molnard3be9152006-03-23 03:00:29 -08001875 mutex_lock(&dqopt->dqonoff_mutex);
Jan Kara9377abd2008-05-12 14:02:08 -07001876
1877 /*
1878 * Skip everything if there's nothing to do. We have to do this because
1879 * sometimes we are called when fill_super() failed and calling
1880 * sync_fs() in such cases does no good.
1881 */
Jan Karaf55abc02008-08-20 17:50:32 +02001882 if (!sb_any_quota_loaded(sb)) {
Jan Kara9377abd2008-05-12 14:02:08 -07001883 mutex_unlock(&dqopt->dqonoff_mutex);
1884 return 0;
1885 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1887 toputinode[cnt] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888 if (type != -1 && cnt != type)
1889 continue;
Jan Karaf55abc02008-08-20 17:50:32 +02001890 if (!sb_has_quota_loaded(sb, cnt))
Jan Kara0ff5af82008-04-28 02:14:33 -07001891 continue;
Jan Karaf55abc02008-08-20 17:50:32 +02001892
1893 if (flags & DQUOT_SUSPENDED) {
Jan Karacc334122009-01-12 17:23:05 +01001894 spin_lock(&dq_state_lock);
Jan Karaf55abc02008-08-20 17:50:32 +02001895 dqopt->flags |=
1896 dquot_state_flag(DQUOT_SUSPENDED, cnt);
Jan Karacc334122009-01-12 17:23:05 +01001897 spin_unlock(&dq_state_lock);
Jan Karaf55abc02008-08-20 17:50:32 +02001898 } else {
Jan Karacc334122009-01-12 17:23:05 +01001899 spin_lock(&dq_state_lock);
Jan Karaf55abc02008-08-20 17:50:32 +02001900 dqopt->flags &= ~dquot_state_flag(flags, cnt);
1901 /* Turning off suspended quotas? */
1902 if (!sb_has_quota_loaded(sb, cnt) &&
1903 sb_has_quota_suspended(sb, cnt)) {
1904 dqopt->flags &= ~dquot_state_flag(
1905 DQUOT_SUSPENDED, cnt);
Jan Karacc334122009-01-12 17:23:05 +01001906 spin_unlock(&dq_state_lock);
Jan Karaf55abc02008-08-20 17:50:32 +02001907 iput(dqopt->files[cnt]);
1908 dqopt->files[cnt] = NULL;
1909 continue;
1910 }
Jan Karacc334122009-01-12 17:23:05 +01001911 spin_unlock(&dq_state_lock);
Jan Kara0ff5af82008-04-28 02:14:33 -07001912 }
Jan Karaf55abc02008-08-20 17:50:32 +02001913
1914 /* We still have to keep quota loaded? */
1915 if (sb_has_quota_loaded(sb, cnt) && !(flags & DQUOT_SUSPENDED))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917
1918 /* Note: these are blocking operations */
1919 drop_dquot_ref(sb, cnt);
1920 invalidate_dquots(sb, cnt);
1921 /*
Jan Kara268157b2009-01-27 15:47:22 +01001922 * Now all dquots should be invalidated, all writes done so we
1923 * should be only users of the info. No locks needed.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924 */
1925 if (info_dirty(&dqopt->info[cnt]))
1926 sb->dq_op->write_info(sb, cnt);
1927 if (dqopt->ops[cnt]->free_file_info)
1928 dqopt->ops[cnt]->free_file_info(sb, cnt);
1929 put_quota_format(dqopt->info[cnt].dqi_format);
1930
1931 toputinode[cnt] = dqopt->files[cnt];
Jan Karaf55abc02008-08-20 17:50:32 +02001932 if (!sb_has_quota_loaded(sb, cnt))
Jan Kara0ff5af82008-04-28 02:14:33 -07001933 dqopt->files[cnt] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934 dqopt->info[cnt].dqi_flags = 0;
1935 dqopt->info[cnt].dqi_igrace = 0;
1936 dqopt->info[cnt].dqi_bgrace = 0;
1937 dqopt->ops[cnt] = NULL;
1938 }
Ingo Molnard3be9152006-03-23 03:00:29 -08001939 mutex_unlock(&dqopt->dqonoff_mutex);
Jan Karaca785ec2008-09-30 17:53:37 +02001940
1941 /* Skip syncing and setting flags if quota files are hidden */
1942 if (dqopt->flags & DQUOT_QUOTA_SYS_FILE)
1943 goto put_inodes;
1944
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945 /* Sync the superblock so that buffers with quota data are written to
Al Viro7b7b1ac2005-11-07 17:13:39 -05001946 * disk (and so userspace sees correct data afterwards). */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947 if (sb->s_op->sync_fs)
1948 sb->s_op->sync_fs(sb, 1);
1949 sync_blockdev(sb->s_bdev);
1950 /* Now the quota files are just ordinary files and we can set the
1951 * inode flags back. Moreover we discard the pagecache so that
1952 * userspace sees the writes we did bypassing the pagecache. We
1953 * must also discard the blockdev buffers so that we see the
1954 * changes done by userspace on the next quotaon() */
1955 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1956 if (toputinode[cnt]) {
Ingo Molnard3be9152006-03-23 03:00:29 -08001957 mutex_lock(&dqopt->dqonoff_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958 /* If quota was reenabled in the meantime, we have
1959 * nothing to do */
Jan Karaf55abc02008-08-20 17:50:32 +02001960 if (!sb_has_quota_loaded(sb, cnt)) {
Jan Kara268157b2009-01-27 15:47:22 +01001961 mutex_lock_nested(&toputinode[cnt]->i_mutex,
1962 I_MUTEX_QUOTA);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963 toputinode[cnt]->i_flags &= ~(S_IMMUTABLE |
1964 S_NOATIME | S_NOQUOTA);
Jan Kara268157b2009-01-27 15:47:22 +01001965 truncate_inode_pages(&toputinode[cnt]->i_data,
1966 0);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001967 mutex_unlock(&toputinode[cnt]->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968 mark_inode_dirty(toputinode[cnt]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969 }
Ingo Molnard3be9152006-03-23 03:00:29 -08001970 mutex_unlock(&dqopt->dqonoff_mutex);
Jan Karaca785ec2008-09-30 17:53:37 +02001971 }
1972 if (sb->s_bdev)
1973 invalidate_bdev(sb->s_bdev);
1974put_inodes:
1975 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1976 if (toputinode[cnt]) {
Jan Kara0ff5af82008-04-28 02:14:33 -07001977 /* On remount RO, we keep the inode pointer so that we
Jan Karaf55abc02008-08-20 17:50:32 +02001978 * can reenable quota on the subsequent remount RW. We
1979 * have to check 'flags' variable and not use sb_has_
1980 * function because another quotaon / quotaoff could
1981 * change global state before we got here. We refuse
1982 * to suspend quotas when there is pending delete on
1983 * the quota file... */
1984 if (!(flags & DQUOT_SUSPENDED))
Jan Kara0ff5af82008-04-28 02:14:33 -07001985 iput(toputinode[cnt]);
1986 else if (!toputinode[cnt]->i_nlink)
1987 ret = -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988 }
Jan Kara0ff5af82008-04-28 02:14:33 -07001989 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990}
Mingming Cao08d03502009-01-14 16:19:32 +01001991EXPORT_SYMBOL(vfs_quota_disable);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992
Jan Karaf55abc02008-08-20 17:50:32 +02001993int vfs_quota_off(struct super_block *sb, int type, int remount)
1994{
1995 return vfs_quota_disable(sb, type, remount ? DQUOT_SUSPENDED :
1996 (DQUOT_USAGE_ENABLED | DQUOT_LIMITS_ENABLED));
1997}
Mingming Cao08d03502009-01-14 16:19:32 +01001998EXPORT_SYMBOL(vfs_quota_off);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999/*
2000 * Turn quotas on on a device
2001 */
2002
Jan Karaf55abc02008-08-20 17:50:32 +02002003/*
2004 * Helper function to turn quotas on when we already have the inode of
2005 * quota file and no quota information is loaded.
2006 */
2007static int vfs_load_quota_inode(struct inode *inode, int type, int format_id,
2008 unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009{
2010 struct quota_format_type *fmt = find_quota_format(format_id);
2011 struct super_block *sb = inode->i_sb;
2012 struct quota_info *dqopt = sb_dqopt(sb);
2013 int error;
2014 int oldflags = -1;
2015
2016 if (!fmt)
2017 return -ESRCH;
2018 if (!S_ISREG(inode->i_mode)) {
2019 error = -EACCES;
2020 goto out_fmt;
2021 }
2022 if (IS_RDONLY(inode)) {
2023 error = -EROFS;
2024 goto out_fmt;
2025 }
2026 if (!sb->s_op->quota_write || !sb->s_op->quota_read) {
2027 error = -EINVAL;
2028 goto out_fmt;
2029 }
Jan Karaf55abc02008-08-20 17:50:32 +02002030 /* Usage always has to be set... */
2031 if (!(flags & DQUOT_USAGE_ENABLED)) {
2032 error = -EINVAL;
2033 goto out_fmt;
2034 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035
Jan Karaca785ec2008-09-30 17:53:37 +02002036 if (!(dqopt->flags & DQUOT_QUOTA_SYS_FILE)) {
Jan Karaab94c392010-02-22 21:07:17 +01002037 /* As we bypass the pagecache we must now flush all the
2038 * dirty data and invalidate caches so that kernel sees
2039 * changes from userspace. It is not enough to just flush
2040 * the quota file since if blocksize < pagesize, invalidation
2041 * of the cache could fail because of other unrelated dirty
2042 * data */
2043 sync_filesystem(sb);
Jan Karaca785ec2008-09-30 17:53:37 +02002044 invalidate_bdev(sb->s_bdev);
2045 }
Ingo Molnard3be9152006-03-23 03:00:29 -08002046 mutex_lock(&dqopt->dqonoff_mutex);
Jan Karaf55abc02008-08-20 17:50:32 +02002047 if (sb_has_quota_loaded(sb, type)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048 error = -EBUSY;
2049 goto out_lock;
2050 }
Jan Karaca785ec2008-09-30 17:53:37 +02002051
2052 if (!(dqopt->flags & DQUOT_QUOTA_SYS_FILE)) {
2053 /* We don't want quota and atime on quota files (deadlocks
2054 * possible) Also nobody should write to the file - we use
2055 * special IO operations which ignore the immutable bit. */
Jan Karadee86562009-07-22 18:12:17 +02002056 mutex_lock_nested(&inode->i_mutex, I_MUTEX_QUOTA);
Jan Kara268157b2009-01-27 15:47:22 +01002057 oldflags = inode->i_flags & (S_NOATIME | S_IMMUTABLE |
2058 S_NOQUOTA);
Jan Karaca785ec2008-09-30 17:53:37 +02002059 inode->i_flags |= S_NOQUOTA | S_NOATIME | S_IMMUTABLE;
Jan Karadee86562009-07-22 18:12:17 +02002060 mutex_unlock(&inode->i_mutex);
Jan Kara26245c92010-01-06 17:20:35 +01002061 /*
2062 * When S_NOQUOTA is set, remove dquot references as no more
2063 * references can be added
2064 */
Christoph Hellwig9f754752010-03-03 09:05:05 -05002065 __dquot_drop(inode);
Jan Karaca785ec2008-09-30 17:53:37 +02002066 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067
2068 error = -EIO;
2069 dqopt->files[type] = igrab(inode);
2070 if (!dqopt->files[type])
2071 goto out_lock;
2072 error = -EINVAL;
2073 if (!fmt->qf_ops->check_quota_file(sb, type))
2074 goto out_file_init;
2075
2076 dqopt->ops[type] = fmt->qf_ops;
2077 dqopt->info[type].dqi_format = fmt;
Jan Kara0ff5af82008-04-28 02:14:33 -07002078 dqopt->info[type].dqi_fmt_id = format_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079 INIT_LIST_HEAD(&dqopt->info[type].dqi_dirty_list);
Ingo Molnard3be9152006-03-23 03:00:29 -08002080 mutex_lock(&dqopt->dqio_mutex);
Jan Kara268157b2009-01-27 15:47:22 +01002081 error = dqopt->ops[type]->read_file_info(sb, type);
2082 if (error < 0) {
Ingo Molnard3be9152006-03-23 03:00:29 -08002083 mutex_unlock(&dqopt->dqio_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084 goto out_file_init;
2085 }
Ingo Molnard3be9152006-03-23 03:00:29 -08002086 mutex_unlock(&dqopt->dqio_mutex);
Jan Karacc334122009-01-12 17:23:05 +01002087 spin_lock(&dq_state_lock);
Jan Karaf55abc02008-08-20 17:50:32 +02002088 dqopt->flags |= dquot_state_flag(flags, type);
Jan Karacc334122009-01-12 17:23:05 +01002089 spin_unlock(&dq_state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090
2091 add_dquot_ref(sb, type);
Ingo Molnard3be9152006-03-23 03:00:29 -08002092 mutex_unlock(&dqopt->dqonoff_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093
2094 return 0;
2095
2096out_file_init:
2097 dqopt->files[type] = NULL;
2098 iput(inode);
2099out_lock:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100 if (oldflags != -1) {
Jan Karadee86562009-07-22 18:12:17 +02002101 mutex_lock_nested(&inode->i_mutex, I_MUTEX_QUOTA);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102 /* Set the flags back (in the case of accidental quotaon()
2103 * on a wrong file we don't want to mess up the flags) */
2104 inode->i_flags &= ~(S_NOATIME | S_NOQUOTA | S_IMMUTABLE);
2105 inode->i_flags |= oldflags;
Jan Karadee86562009-07-22 18:12:17 +02002106 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107 }
Jiaying Zhangd01730d2009-07-07 18:15:21 +02002108 mutex_unlock(&dqopt->dqonoff_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109out_fmt:
2110 put_quota_format(fmt);
2111
2112 return error;
2113}
2114
Jan Kara0ff5af82008-04-28 02:14:33 -07002115/* Reenable quotas on remount RW */
2116static int vfs_quota_on_remount(struct super_block *sb, int type)
2117{
2118 struct quota_info *dqopt = sb_dqopt(sb);
2119 struct inode *inode;
2120 int ret;
Jan Karaf55abc02008-08-20 17:50:32 +02002121 unsigned int flags;
Jan Kara0ff5af82008-04-28 02:14:33 -07002122
2123 mutex_lock(&dqopt->dqonoff_mutex);
2124 if (!sb_has_quota_suspended(sb, type)) {
2125 mutex_unlock(&dqopt->dqonoff_mutex);
2126 return 0;
2127 }
Jan Kara0ff5af82008-04-28 02:14:33 -07002128 inode = dqopt->files[type];
2129 dqopt->files[type] = NULL;
Jan Karacc334122009-01-12 17:23:05 +01002130 spin_lock(&dq_state_lock);
Jan Karaf55abc02008-08-20 17:50:32 +02002131 flags = dqopt->flags & dquot_state_flag(DQUOT_USAGE_ENABLED |
2132 DQUOT_LIMITS_ENABLED, type);
2133 dqopt->flags &= ~dquot_state_flag(DQUOT_STATE_FLAGS, type);
Jan Karacc334122009-01-12 17:23:05 +01002134 spin_unlock(&dq_state_lock);
Jan Kara0ff5af82008-04-28 02:14:33 -07002135 mutex_unlock(&dqopt->dqonoff_mutex);
2136
Jan Karaf55abc02008-08-20 17:50:32 +02002137 flags = dquot_generic_flag(flags, type);
2138 ret = vfs_load_quota_inode(inode, type, dqopt->info[type].dqi_fmt_id,
2139 flags);
Jan Kara0ff5af82008-04-28 02:14:33 -07002140 iput(inode);
2141
2142 return ret;
2143}
2144
Al Viro77e69da2008-08-01 04:29:18 -04002145int vfs_quota_on_path(struct super_block *sb, int type, int format_id,
2146 struct path *path)
2147{
2148 int error = security_quota_on(path->dentry);
2149 if (error)
2150 return error;
2151 /* Quota file not on the same filesystem? */
2152 if (path->mnt->mnt_sb != sb)
2153 error = -EXDEV;
2154 else
Jan Karaf55abc02008-08-20 17:50:32 +02002155 error = vfs_load_quota_inode(path->dentry->d_inode, type,
2156 format_id, DQUOT_USAGE_ENABLED |
2157 DQUOT_LIMITS_ENABLED);
Al Viro77e69da2008-08-01 04:29:18 -04002158 return error;
2159}
Mingming Cao08d03502009-01-14 16:19:32 +01002160EXPORT_SYMBOL(vfs_quota_on_path);
Al Viro77e69da2008-08-01 04:29:18 -04002161
Al Viro82646132008-08-02 00:57:06 -04002162int vfs_quota_on(struct super_block *sb, int type, int format_id, char *name,
Jan Kara0ff5af82008-04-28 02:14:33 -07002163 int remount)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164{
Al Viro82646132008-08-02 00:57:06 -04002165 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166 int error;
2167
Jan Kara0ff5af82008-04-28 02:14:33 -07002168 if (remount)
2169 return vfs_quota_on_remount(sb, type);
2170
Al Viro82646132008-08-02 00:57:06 -04002171 error = kern_path(name, LOOKUP_FOLLOW, &path);
Al Viro77e69da2008-08-01 04:29:18 -04002172 if (!error) {
Al Viro82646132008-08-02 00:57:06 -04002173 error = vfs_quota_on_path(sb, type, format_id, &path);
2174 path_put(&path);
Al Viro77e69da2008-08-01 04:29:18 -04002175 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176 return error;
2177}
Mingming Cao08d03502009-01-14 16:19:32 +01002178EXPORT_SYMBOL(vfs_quota_on);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179
2180/*
Jan Karaf55abc02008-08-20 17:50:32 +02002181 * More powerful function for turning on quotas allowing setting
2182 * of individual quota flags
2183 */
2184int vfs_quota_enable(struct inode *inode, int type, int format_id,
2185 unsigned int flags)
2186{
2187 int ret = 0;
2188 struct super_block *sb = inode->i_sb;
2189 struct quota_info *dqopt = sb_dqopt(sb);
2190
2191 /* Just unsuspend quotas? */
2192 if (flags & DQUOT_SUSPENDED)
2193 return vfs_quota_on_remount(sb, type);
2194 if (!flags)
2195 return 0;
2196 /* Just updating flags needed? */
2197 if (sb_has_quota_loaded(sb, type)) {
2198 mutex_lock(&dqopt->dqonoff_mutex);
2199 /* Now do a reliable test... */
2200 if (!sb_has_quota_loaded(sb, type)) {
2201 mutex_unlock(&dqopt->dqonoff_mutex);
2202 goto load_quota;
2203 }
2204 if (flags & DQUOT_USAGE_ENABLED &&
2205 sb_has_quota_usage_enabled(sb, type)) {
2206 ret = -EBUSY;
2207 goto out_lock;
2208 }
2209 if (flags & DQUOT_LIMITS_ENABLED &&
2210 sb_has_quota_limits_enabled(sb, type)) {
2211 ret = -EBUSY;
2212 goto out_lock;
2213 }
Jan Karacc334122009-01-12 17:23:05 +01002214 spin_lock(&dq_state_lock);
Jan Karaf55abc02008-08-20 17:50:32 +02002215 sb_dqopt(sb)->flags |= dquot_state_flag(flags, type);
Jan Karacc334122009-01-12 17:23:05 +01002216 spin_unlock(&dq_state_lock);
Jan Karaf55abc02008-08-20 17:50:32 +02002217out_lock:
2218 mutex_unlock(&dqopt->dqonoff_mutex);
2219 return ret;
2220 }
2221
2222load_quota:
2223 return vfs_load_quota_inode(inode, type, format_id, flags);
2224}
Mingming Cao08d03502009-01-14 16:19:32 +01002225EXPORT_SYMBOL(vfs_quota_enable);
Jan Karaf55abc02008-08-20 17:50:32 +02002226
2227/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228 * This function is used when filesystem needs to initialize quotas
2229 * during mount time.
2230 */
Christoph Hellwig84de8562005-06-23 00:09:16 -07002231int vfs_quota_on_mount(struct super_block *sb, char *qf_name,
2232 int format_id, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233{
Christoph Hellwig84de8562005-06-23 00:09:16 -07002234 struct dentry *dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235 int error;
2236
Jan Karac56818d2009-11-12 15:42:08 +01002237 mutex_lock(&sb->s_root->d_inode->i_mutex);
Christoph Hellwig2fa389c2005-06-23 00:09:16 -07002238 dentry = lookup_one_len(qf_name, sb->s_root, strlen(qf_name));
Jan Karac56818d2009-11-12 15:42:08 +01002239 mutex_unlock(&sb->s_root->d_inode->i_mutex);
Christoph Hellwig84de8562005-06-23 00:09:16 -07002240 if (IS_ERR(dentry))
2241 return PTR_ERR(dentry);
2242
Jan Kara154f4842005-11-28 13:44:14 -08002243 if (!dentry->d_inode) {
2244 error = -ENOENT;
2245 goto out;
2246 }
2247
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248 error = security_quota_on(dentry);
Christoph Hellwig84de8562005-06-23 00:09:16 -07002249 if (!error)
Jan Karaf55abc02008-08-20 17:50:32 +02002250 error = vfs_load_quota_inode(dentry->d_inode, type, format_id,
2251 DQUOT_USAGE_ENABLED | DQUOT_LIMITS_ENABLED);
Christoph Hellwig84de8562005-06-23 00:09:16 -07002252
Jan Kara154f4842005-11-28 13:44:14 -08002253out:
Christoph Hellwig84de8562005-06-23 00:09:16 -07002254 dput(dentry);
2255 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256}
Mingming Cao08d03502009-01-14 16:19:32 +01002257EXPORT_SYMBOL(vfs_quota_on_mount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258
Jan Karab85f4b82008-07-25 01:46:50 -07002259/* Wrapper to turn on quotas when remounting rw */
2260int vfs_dq_quota_on_remount(struct super_block *sb)
2261{
2262 int cnt;
2263 int ret = 0, err;
2264
2265 if (!sb->s_qcop || !sb->s_qcop->quota_on)
2266 return -ENOSYS;
2267 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
2268 err = sb->s_qcop->quota_on(sb, cnt, 0, NULL, 1);
2269 if (err < 0 && !ret)
2270 ret = err;
2271 }
2272 return ret;
2273}
Mingming Cao08d03502009-01-14 16:19:32 +01002274EXPORT_SYMBOL(vfs_dq_quota_on_remount);
Jan Karab85f4b82008-07-25 01:46:50 -07002275
Jan Kara12095462008-08-20 14:45:12 +02002276static inline qsize_t qbtos(qsize_t blocks)
2277{
2278 return blocks << QIF_DQBLKSIZE_BITS;
2279}
2280
2281static inline qsize_t stoqb(qsize_t space)
2282{
2283 return (space + QIF_DQBLKSIZE - 1) >> QIF_DQBLKSIZE_BITS;
2284}
2285
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286/* Generic routine for getting common part of quota structure */
2287static void do_get_dqblk(struct dquot *dquot, struct if_dqblk *di)
2288{
2289 struct mem_dqblk *dm = &dquot->dq_dqb;
2290
2291 spin_lock(&dq_data_lock);
Jan Kara12095462008-08-20 14:45:12 +02002292 di->dqb_bhardlimit = stoqb(dm->dqb_bhardlimit);
2293 di->dqb_bsoftlimit = stoqb(dm->dqb_bsoftlimit);
Mingming Caof18df222009-01-13 16:43:09 +01002294 di->dqb_curspace = dm->dqb_curspace + dm->dqb_rsvspace;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295 di->dqb_ihardlimit = dm->dqb_ihardlimit;
2296 di->dqb_isoftlimit = dm->dqb_isoftlimit;
2297 di->dqb_curinodes = dm->dqb_curinodes;
2298 di->dqb_btime = dm->dqb_btime;
2299 di->dqb_itime = dm->dqb_itime;
2300 di->dqb_valid = QIF_ALL;
2301 spin_unlock(&dq_data_lock);
2302}
2303
Jan Kara268157b2009-01-27 15:47:22 +01002304int vfs_get_dqblk(struct super_block *sb, int type, qid_t id,
2305 struct if_dqblk *di)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306{
2307 struct dquot *dquot;
2308
Jan Karacc334122009-01-12 17:23:05 +01002309 dquot = dqget(sb, id, type);
Jan Karadd6f3c62009-01-26 16:01:43 +01002310 if (!dquot)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311 return -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312 do_get_dqblk(dquot, di);
2313 dqput(dquot);
Jan Karacc334122009-01-12 17:23:05 +01002314
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315 return 0;
2316}
Mingming Cao08d03502009-01-14 16:19:32 +01002317EXPORT_SYMBOL(vfs_get_dqblk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318
2319/* Generic routine for setting common part of quota structure */
Andrew Perepechko338bf9a2008-04-28 02:14:31 -07002320static int do_set_dqblk(struct dquot *dquot, struct if_dqblk *di)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002321{
2322 struct mem_dqblk *dm = &dquot->dq_dqb;
2323 int check_blim = 0, check_ilim = 0;
Andrew Perepechko338bf9a2008-04-28 02:14:31 -07002324 struct mem_dqinfo *dqi = &sb_dqopt(dquot->dq_sb)->info[dquot->dq_type];
2325
2326 if ((di->dqb_valid & QIF_BLIMITS &&
2327 (di->dqb_bhardlimit > dqi->dqi_maxblimit ||
2328 di->dqb_bsoftlimit > dqi->dqi_maxblimit)) ||
2329 (di->dqb_valid & QIF_ILIMITS &&
2330 (di->dqb_ihardlimit > dqi->dqi_maxilimit ||
2331 di->dqb_isoftlimit > dqi->dqi_maxilimit)))
2332 return -ERANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002333
2334 spin_lock(&dq_data_lock);
2335 if (di->dqb_valid & QIF_SPACE) {
Mingming Caof18df222009-01-13 16:43:09 +01002336 dm->dqb_curspace = di->dqb_curspace - dm->dqb_rsvspace;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337 check_blim = 1;
Andrew Perepechko08261672010-04-12 22:16:50 +04002338 set_bit(DQ_LASTSET_B + QIF_SPACE_B, &dquot->dq_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339 }
2340 if (di->dqb_valid & QIF_BLIMITS) {
Jan Kara12095462008-08-20 14:45:12 +02002341 dm->dqb_bsoftlimit = qbtos(di->dqb_bsoftlimit);
2342 dm->dqb_bhardlimit = qbtos(di->dqb_bhardlimit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343 check_blim = 1;
Andrew Perepechko08261672010-04-12 22:16:50 +04002344 set_bit(DQ_LASTSET_B + QIF_BLIMITS_B, &dquot->dq_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345 }
2346 if (di->dqb_valid & QIF_INODES) {
2347 dm->dqb_curinodes = di->dqb_curinodes;
2348 check_ilim = 1;
Andrew Perepechko08261672010-04-12 22:16:50 +04002349 set_bit(DQ_LASTSET_B + QIF_INODES_B, &dquot->dq_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350 }
2351 if (di->dqb_valid & QIF_ILIMITS) {
2352 dm->dqb_isoftlimit = di->dqb_isoftlimit;
2353 dm->dqb_ihardlimit = di->dqb_ihardlimit;
2354 check_ilim = 1;
Andrew Perepechko08261672010-04-12 22:16:50 +04002355 set_bit(DQ_LASTSET_B + QIF_ILIMITS_B, &dquot->dq_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002356 }
Jan Kara4d59bce2008-10-02 16:48:10 +02002357 if (di->dqb_valid & QIF_BTIME) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002358 dm->dqb_btime = di->dqb_btime;
Jan Karae04a88a92009-01-07 18:07:29 -08002359 check_blim = 1;
Andrew Perepechko08261672010-04-12 22:16:50 +04002360 set_bit(DQ_LASTSET_B + QIF_BTIME_B, &dquot->dq_flags);
Jan Kara4d59bce2008-10-02 16:48:10 +02002361 }
2362 if (di->dqb_valid & QIF_ITIME) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363 dm->dqb_itime = di->dqb_itime;
Jan Karae04a88a92009-01-07 18:07:29 -08002364 check_ilim = 1;
Andrew Perepechko08261672010-04-12 22:16:50 +04002365 set_bit(DQ_LASTSET_B + QIF_ITIME_B, &dquot->dq_flags);
Jan Kara4d59bce2008-10-02 16:48:10 +02002366 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367
2368 if (check_blim) {
Jan Kara268157b2009-01-27 15:47:22 +01002369 if (!dm->dqb_bsoftlimit ||
2370 dm->dqb_curspace < dm->dqb_bsoftlimit) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371 dm->dqb_btime = 0;
2372 clear_bit(DQ_BLKS_B, &dquot->dq_flags);
Jan Kara268157b2009-01-27 15:47:22 +01002373 } else if (!(di->dqb_valid & QIF_BTIME))
2374 /* Set grace only if user hasn't provided his own... */
Andrew Perepechko338bf9a2008-04-28 02:14:31 -07002375 dm->dqb_btime = get_seconds() + dqi->dqi_bgrace;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376 }
2377 if (check_ilim) {
Jan Kara268157b2009-01-27 15:47:22 +01002378 if (!dm->dqb_isoftlimit ||
2379 dm->dqb_curinodes < dm->dqb_isoftlimit) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380 dm->dqb_itime = 0;
2381 clear_bit(DQ_INODES_B, &dquot->dq_flags);
Jan Kara268157b2009-01-27 15:47:22 +01002382 } else if (!(di->dqb_valid & QIF_ITIME))
2383 /* Set grace only if user hasn't provided his own... */
Andrew Perepechko338bf9a2008-04-28 02:14:31 -07002384 dm->dqb_itime = get_seconds() + dqi->dqi_igrace;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002385 }
Jan Kara268157b2009-01-27 15:47:22 +01002386 if (dm->dqb_bhardlimit || dm->dqb_bsoftlimit || dm->dqb_ihardlimit ||
2387 dm->dqb_isoftlimit)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002388 clear_bit(DQ_FAKE_B, &dquot->dq_flags);
2389 else
2390 set_bit(DQ_FAKE_B, &dquot->dq_flags);
2391 spin_unlock(&dq_data_lock);
2392 mark_dquot_dirty(dquot);
Andrew Perepechko338bf9a2008-04-28 02:14:31 -07002393
2394 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002395}
2396
Jan Kara268157b2009-01-27 15:47:22 +01002397int vfs_set_dqblk(struct super_block *sb, int type, qid_t id,
2398 struct if_dqblk *di)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002399{
2400 struct dquot *dquot;
Andrew Perepechko338bf9a2008-04-28 02:14:31 -07002401 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002402
Jan Karaf55abc02008-08-20 17:50:32 +02002403 dquot = dqget(sb, id, type);
2404 if (!dquot) {
2405 rc = -ESRCH;
2406 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002407 }
Andrew Perepechko338bf9a2008-04-28 02:14:31 -07002408 rc = do_set_dqblk(dquot, di);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002409 dqput(dquot);
Jan Karaf55abc02008-08-20 17:50:32 +02002410out:
Andrew Perepechko338bf9a2008-04-28 02:14:31 -07002411 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002412}
Mingming Cao08d03502009-01-14 16:19:32 +01002413EXPORT_SYMBOL(vfs_set_dqblk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002414
2415/* Generic routine for getting common part of quota file information */
2416int vfs_get_dqinfo(struct super_block *sb, int type, struct if_dqinfo *ii)
2417{
2418 struct mem_dqinfo *mi;
2419
Ingo Molnard3be9152006-03-23 03:00:29 -08002420 mutex_lock(&sb_dqopt(sb)->dqonoff_mutex);
Jan Karaf55abc02008-08-20 17:50:32 +02002421 if (!sb_has_quota_active(sb, type)) {
Ingo Molnard3be9152006-03-23 03:00:29 -08002422 mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002423 return -ESRCH;
2424 }
2425 mi = sb_dqopt(sb)->info + type;
2426 spin_lock(&dq_data_lock);
2427 ii->dqi_bgrace = mi->dqi_bgrace;
2428 ii->dqi_igrace = mi->dqi_igrace;
2429 ii->dqi_flags = mi->dqi_flags & DQF_MASK;
2430 ii->dqi_valid = IIF_ALL;
2431 spin_unlock(&dq_data_lock);
Ingo Molnard3be9152006-03-23 03:00:29 -08002432 mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002433 return 0;
2434}
Mingming Cao08d03502009-01-14 16:19:32 +01002435EXPORT_SYMBOL(vfs_get_dqinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002436
2437/* Generic routine for setting common part of quota file information */
2438int vfs_set_dqinfo(struct super_block *sb, int type, struct if_dqinfo *ii)
2439{
2440 struct mem_dqinfo *mi;
Jan Karaf55abc02008-08-20 17:50:32 +02002441 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442
Ingo Molnard3be9152006-03-23 03:00:29 -08002443 mutex_lock(&sb_dqopt(sb)->dqonoff_mutex);
Jan Karaf55abc02008-08-20 17:50:32 +02002444 if (!sb_has_quota_active(sb, type)) {
2445 err = -ESRCH;
2446 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002447 }
2448 mi = sb_dqopt(sb)->info + type;
2449 spin_lock(&dq_data_lock);
2450 if (ii->dqi_valid & IIF_BGRACE)
2451 mi->dqi_bgrace = ii->dqi_bgrace;
2452 if (ii->dqi_valid & IIF_IGRACE)
2453 mi->dqi_igrace = ii->dqi_igrace;
2454 if (ii->dqi_valid & IIF_FLAGS)
Jan Kara268157b2009-01-27 15:47:22 +01002455 mi->dqi_flags = (mi->dqi_flags & ~DQF_MASK) |
2456 (ii->dqi_flags & DQF_MASK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002457 spin_unlock(&dq_data_lock);
2458 mark_info_dirty(sb, type);
2459 /* Force write to disk */
2460 sb->dq_op->write_info(sb, type);
Jan Karaf55abc02008-08-20 17:50:32 +02002461out:
Ingo Molnard3be9152006-03-23 03:00:29 -08002462 mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
Jan Karaf55abc02008-08-20 17:50:32 +02002463 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002464}
Mingming Cao08d03502009-01-14 16:19:32 +01002465EXPORT_SYMBOL(vfs_set_dqinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002466
Alexey Dobriyan0d54b212009-09-21 17:01:09 -07002467const struct quotactl_ops vfs_quotactl_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468 .quota_on = vfs_quota_on,
2469 .quota_off = vfs_quota_off,
2470 .quota_sync = vfs_quota_sync,
2471 .get_info = vfs_get_dqinfo,
2472 .set_info = vfs_set_dqinfo,
2473 .get_dqblk = vfs_get_dqblk,
2474 .set_dqblk = vfs_set_dqblk
2475};
2476
2477static ctl_table fs_dqstats_table[] = {
2478 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002479 .procname = "lookups",
2480 .data = &dqstats.lookups,
2481 .maxlen = sizeof(int),
2482 .mode = 0444,
Eric W. Biederman6d456112009-11-16 03:11:48 -08002483 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002484 },
2485 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002486 .procname = "drops",
2487 .data = &dqstats.drops,
2488 .maxlen = sizeof(int),
2489 .mode = 0444,
Eric W. Biederman6d456112009-11-16 03:11:48 -08002490 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002491 },
2492 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002493 .procname = "reads",
2494 .data = &dqstats.reads,
2495 .maxlen = sizeof(int),
2496 .mode = 0444,
Eric W. Biederman6d456112009-11-16 03:11:48 -08002497 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002498 },
2499 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002500 .procname = "writes",
2501 .data = &dqstats.writes,
2502 .maxlen = sizeof(int),
2503 .mode = 0444,
Eric W. Biederman6d456112009-11-16 03:11:48 -08002504 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002505 },
2506 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002507 .procname = "cache_hits",
2508 .data = &dqstats.cache_hits,
2509 .maxlen = sizeof(int),
2510 .mode = 0444,
Eric W. Biederman6d456112009-11-16 03:11:48 -08002511 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002512 },
2513 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002514 .procname = "allocated_dquots",
2515 .data = &dqstats.allocated_dquots,
2516 .maxlen = sizeof(int),
2517 .mode = 0444,
Eric W. Biederman6d456112009-11-16 03:11:48 -08002518 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002519 },
2520 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002521 .procname = "free_dquots",
2522 .data = &dqstats.free_dquots,
2523 .maxlen = sizeof(int),
2524 .mode = 0444,
Eric W. Biederman6d456112009-11-16 03:11:48 -08002525 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002526 },
2527 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002528 .procname = "syncs",
2529 .data = &dqstats.syncs,
2530 .maxlen = sizeof(int),
2531 .mode = 0444,
Eric W. Biederman6d456112009-11-16 03:11:48 -08002532 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002533 },
Jan Kara8e893462007-10-16 23:29:31 -07002534#ifdef CONFIG_PRINT_QUOTA_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07002535 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002536 .procname = "warnings",
2537 .data = &flag_print_warnings,
2538 .maxlen = sizeof(int),
2539 .mode = 0644,
Eric W. Biederman6d456112009-11-16 03:11:48 -08002540 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002541 },
Jan Kara8e893462007-10-16 23:29:31 -07002542#endif
Eric W. Biedermanab092032009-11-05 14:25:10 -08002543 { },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002544};
2545
2546static ctl_table fs_table[] = {
2547 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002548 .procname = "quota",
2549 .mode = 0555,
2550 .child = fs_dqstats_table,
2551 },
Eric W. Biedermanab092032009-11-05 14:25:10 -08002552 { },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002553};
2554
2555static ctl_table sys_table[] = {
2556 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002557 .procname = "fs",
2558 .mode = 0555,
2559 .child = fs_table,
2560 },
Eric W. Biedermanab092032009-11-05 14:25:10 -08002561 { },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002562};
2563
2564static int __init dquot_init(void)
2565{
2566 int i;
2567 unsigned long nr_hash, order;
2568
2569 printk(KERN_NOTICE "VFS: Disk quotas %s\n", __DQUOT_VERSION__);
2570
Eric W. Biederman0b4d4142007-02-14 00:34:09 -08002571 register_sysctl_table(sys_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002572
Paul Mundt20c2df82007-07-20 10:11:58 +09002573 dquot_cachep = kmem_cache_create("dquot",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002574 sizeof(struct dquot), sizeof(unsigned long) * 4,
Paul Jacksonfffb60f2006-03-24 03:16:06 -08002575 (SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT|
2576 SLAB_MEM_SPREAD|SLAB_PANIC),
Paul Mundt20c2df82007-07-20 10:11:58 +09002577 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002578
2579 order = 0;
2580 dquot_hash = (struct hlist_head *)__get_free_pages(GFP_ATOMIC, order);
2581 if (!dquot_hash)
2582 panic("Cannot create dquot hash table");
2583
2584 /* Find power-of-two hlist_heads which can fit into allocation */
2585 nr_hash = (1UL << order) * PAGE_SIZE / sizeof(struct hlist_head);
2586 dq_hash_bits = 0;
2587 do {
2588 dq_hash_bits++;
2589 } while (nr_hash >> dq_hash_bits);
2590 dq_hash_bits--;
2591
2592 nr_hash = 1UL << dq_hash_bits;
2593 dq_hash_mask = nr_hash - 1;
2594 for (i = 0; i < nr_hash; i++)
2595 INIT_HLIST_HEAD(dquot_hash + i);
2596
2597 printk("Dquot-cache hash table entries: %ld (order %ld, %ld bytes)\n",
2598 nr_hash, order, (PAGE_SIZE << order));
2599
Rusty Russell8e1f9362007-07-17 04:03:17 -07002600 register_shrinker(&dqcache_shrinker);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002601
Linus Torvalds1da177e2005-04-16 15:20:36 -07002602 return 0;
2603}
2604module_init(dquot_init);