blob: df6832ec17c3adcc831cfa100ab80dab4e3d958f [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
320int dquot_mark_dquot_dirty(struct dquot *dquot)
321{
322 spin_lock(&dq_list_lock);
323 if (!test_and_set_bit(DQ_MOD_B, &dquot->dq_flags))
324 list_add(&dquot->dq_dirty, &sb_dqopt(dquot->dq_sb)->
325 info[dquot->dq_type].dqi_dirty_list);
326 spin_unlock(&dq_list_lock);
327 return 0;
328}
Mingming Cao08d03502009-01-14 16:19:32 +0100329EXPORT_SYMBOL(dquot_mark_dquot_dirty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330
Dmitry Monakhovdc52dd32009-12-14 15:21:15 +0300331/* Dirtify all the dquots - this can block when journalling */
332static inline int mark_all_dquot_dirty(struct dquot * const *dquot)
333{
334 int ret, err, cnt;
335
336 ret = err = 0;
337 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
338 if (dquot[cnt])
339 /* Even in case of error we have to continue */
340 ret = mark_dquot_dirty(dquot[cnt]);
341 if (!err)
342 err = ret;
343 }
344 return err;
345}
346
347static inline void dqput_all(struct dquot **dquot)
348{
349 unsigned int cnt;
350
351 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
352 dqput(dquot[cnt]);
353}
354
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355/* This function needs dq_list_lock */
356static inline int clear_dquot_dirty(struct dquot *dquot)
357{
358 if (!test_and_clear_bit(DQ_MOD_B, &dquot->dq_flags))
359 return 0;
360 list_del_init(&dquot->dq_dirty);
361 return 1;
362}
363
364void mark_info_dirty(struct super_block *sb, int type)
365{
366 set_bit(DQF_INFO_DIRTY_B, &sb_dqopt(sb)->info[type].dqi_flags);
367}
368EXPORT_SYMBOL(mark_info_dirty);
369
370/*
371 * Read dquot from disk and alloc space for it
372 */
373
374int dquot_acquire(struct dquot *dquot)
375{
376 int ret = 0, ret2 = 0;
377 struct quota_info *dqopt = sb_dqopt(dquot->dq_sb);
378
Ingo Molnard3be9152006-03-23 03:00:29 -0800379 mutex_lock(&dquot->dq_lock);
380 mutex_lock(&dqopt->dqio_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 if (!test_bit(DQ_READ_B, &dquot->dq_flags))
382 ret = dqopt->ops[dquot->dq_type]->read_dqblk(dquot);
383 if (ret < 0)
384 goto out_iolock;
385 set_bit(DQ_READ_B, &dquot->dq_flags);
386 /* Instantiate dquot if needed */
387 if (!test_bit(DQ_ACTIVE_B, &dquot->dq_flags) && !dquot->dq_off) {
388 ret = dqopt->ops[dquot->dq_type]->commit_dqblk(dquot);
389 /* Write the info if needed */
Jan Kara268157b2009-01-27 15:47:22 +0100390 if (info_dirty(&dqopt->info[dquot->dq_type])) {
391 ret2 = dqopt->ops[dquot->dq_type]->write_file_info(
392 dquot->dq_sb, dquot->dq_type);
393 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 if (ret < 0)
395 goto out_iolock;
396 if (ret2 < 0) {
397 ret = ret2;
398 goto out_iolock;
399 }
400 }
401 set_bit(DQ_ACTIVE_B, &dquot->dq_flags);
402out_iolock:
Ingo Molnard3be9152006-03-23 03:00:29 -0800403 mutex_unlock(&dqopt->dqio_mutex);
404 mutex_unlock(&dquot->dq_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 return ret;
406}
Mingming Cao08d03502009-01-14 16:19:32 +0100407EXPORT_SYMBOL(dquot_acquire);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408
409/*
410 * Write dquot to disk
411 */
412int dquot_commit(struct dquot *dquot)
413{
414 int ret = 0, ret2 = 0;
415 struct quota_info *dqopt = sb_dqopt(dquot->dq_sb);
416
Ingo Molnard3be9152006-03-23 03:00:29 -0800417 mutex_lock(&dqopt->dqio_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 spin_lock(&dq_list_lock);
419 if (!clear_dquot_dirty(dquot)) {
420 spin_unlock(&dq_list_lock);
421 goto out_sem;
422 }
423 spin_unlock(&dq_list_lock);
424 /* Inactive dquot can be only if there was error during read/init
425 * => we have better not writing it */
426 if (test_bit(DQ_ACTIVE_B, &dquot->dq_flags)) {
427 ret = dqopt->ops[dquot->dq_type]->commit_dqblk(dquot);
Jan Kara268157b2009-01-27 15:47:22 +0100428 if (info_dirty(&dqopt->info[dquot->dq_type])) {
429 ret2 = dqopt->ops[dquot->dq_type]->write_file_info(
430 dquot->dq_sb, dquot->dq_type);
431 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 if (ret >= 0)
433 ret = ret2;
434 }
435out_sem:
Ingo Molnard3be9152006-03-23 03:00:29 -0800436 mutex_unlock(&dqopt->dqio_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 return ret;
438}
Mingming Cao08d03502009-01-14 16:19:32 +0100439EXPORT_SYMBOL(dquot_commit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440
441/*
442 * Release dquot
443 */
444int dquot_release(struct dquot *dquot)
445{
446 int ret = 0, ret2 = 0;
447 struct quota_info *dqopt = sb_dqopt(dquot->dq_sb);
448
Ingo Molnard3be9152006-03-23 03:00:29 -0800449 mutex_lock(&dquot->dq_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 /* Check whether we are not racing with some other dqget() */
451 if (atomic_read(&dquot->dq_count) > 1)
452 goto out_dqlock;
Ingo Molnard3be9152006-03-23 03:00:29 -0800453 mutex_lock(&dqopt->dqio_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 if (dqopt->ops[dquot->dq_type]->release_dqblk) {
455 ret = dqopt->ops[dquot->dq_type]->release_dqblk(dquot);
456 /* Write the info */
Jan Kara268157b2009-01-27 15:47:22 +0100457 if (info_dirty(&dqopt->info[dquot->dq_type])) {
458 ret2 = dqopt->ops[dquot->dq_type]->write_file_info(
459 dquot->dq_sb, dquot->dq_type);
460 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 if (ret >= 0)
462 ret = ret2;
463 }
464 clear_bit(DQ_ACTIVE_B, &dquot->dq_flags);
Ingo Molnard3be9152006-03-23 03:00:29 -0800465 mutex_unlock(&dqopt->dqio_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466out_dqlock:
Ingo Molnard3be9152006-03-23 03:00:29 -0800467 mutex_unlock(&dquot->dq_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 return ret;
469}
Mingming Cao08d03502009-01-14 16:19:32 +0100470EXPORT_SYMBOL(dquot_release);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471
Jan Kara7d9056b2008-11-25 15:31:32 +0100472void dquot_destroy(struct dquot *dquot)
Jan Kara74f783a2008-08-19 14:51:22 +0200473{
474 kmem_cache_free(dquot_cachep, dquot);
475}
Jan Kara7d9056b2008-11-25 15:31:32 +0100476EXPORT_SYMBOL(dquot_destroy);
Jan Kara74f783a2008-08-19 14:51:22 +0200477
478static inline void do_destroy_dquot(struct dquot *dquot)
479{
480 dquot->dq_sb->dq_op->destroy_dquot(dquot);
481}
482
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483/* Invalidate all dquots on the list. Note that this function is called after
484 * quota is disabled and pointers from inodes removed so there cannot be new
Jan Kara6362e4d2006-03-23 03:00:17 -0800485 * quota users. There can still be some users of quotas due to inodes being
486 * just deleted or pruned by prune_icache() (those are not attached to any
Jan Karacc334122009-01-12 17:23:05 +0100487 * list) or parallel quotactl call. We have to wait for such users.
Jan Kara6362e4d2006-03-23 03:00:17 -0800488 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489static void invalidate_dquots(struct super_block *sb, int type)
490{
Domen Puncerc33ed272005-06-25 14:59:36 -0700491 struct dquot *dquot, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492
Jan Kara6362e4d2006-03-23 03:00:17 -0800493restart:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 spin_lock(&dq_list_lock);
Domen Puncerc33ed272005-06-25 14:59:36 -0700495 list_for_each_entry_safe(dquot, tmp, &inuse_list, dq_inuse) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 if (dquot->dq_sb != sb)
497 continue;
498 if (dquot->dq_type != type)
499 continue;
Jan Kara6362e4d2006-03-23 03:00:17 -0800500 /* Wait for dquot users */
501 if (atomic_read(&dquot->dq_count)) {
502 DEFINE_WAIT(wait);
503
504 atomic_inc(&dquot->dq_count);
505 prepare_to_wait(&dquot->dq_wait_unused, &wait,
506 TASK_UNINTERRUPTIBLE);
507 spin_unlock(&dq_list_lock);
508 /* Once dqput() wakes us up, we know it's time to free
509 * the dquot.
510 * IMPORTANT: we rely on the fact that there is always
511 * at most one process waiting for dquot to free.
512 * Otherwise dq_count would be > 1 and we would never
513 * wake up.
514 */
515 if (atomic_read(&dquot->dq_count) > 1)
516 schedule();
517 finish_wait(&dquot->dq_wait_unused, &wait);
518 dqput(dquot);
519 /* At this moment dquot() need not exist (it could be
520 * reclaimed by prune_dqcache(). Hence we must
521 * restart. */
522 goto restart;
523 }
524 /*
525 * Quota now has no users and it has been written on last
526 * dqput()
527 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 remove_dquot_hash(dquot);
529 remove_free_dquot(dquot);
530 remove_inuse(dquot);
Jan Kara74f783a2008-08-19 14:51:22 +0200531 do_destroy_dquot(dquot);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 }
533 spin_unlock(&dq_list_lock);
534}
535
Jan Kara12c77522008-10-20 17:05:00 +0200536/* Call callback for every active dquot on given filesystem */
537int dquot_scan_active(struct super_block *sb,
538 int (*fn)(struct dquot *dquot, unsigned long priv),
539 unsigned long priv)
540{
541 struct dquot *dquot, *old_dquot = NULL;
542 int ret = 0;
543
544 mutex_lock(&sb_dqopt(sb)->dqonoff_mutex);
545 spin_lock(&dq_list_lock);
546 list_for_each_entry(dquot, &inuse_list, dq_inuse) {
547 if (!test_bit(DQ_ACTIVE_B, &dquot->dq_flags))
548 continue;
549 if (dquot->dq_sb != sb)
550 continue;
551 /* Now we have active dquot so we can just increase use count */
552 atomic_inc(&dquot->dq_count);
553 dqstats.lookups++;
554 spin_unlock(&dq_list_lock);
555 dqput(old_dquot);
556 old_dquot = dquot;
557 ret = fn(dquot, priv);
558 if (ret < 0)
559 goto out;
560 spin_lock(&dq_list_lock);
561 /* We are safe to continue now because our dquot could not
562 * be moved out of the inuse list while we hold the reference */
563 }
564 spin_unlock(&dq_list_lock);
565out:
566 dqput(old_dquot);
567 mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
568 return ret;
569}
Mingming Cao08d03502009-01-14 16:19:32 +0100570EXPORT_SYMBOL(dquot_scan_active);
Jan Kara12c77522008-10-20 17:05:00 +0200571
Christoph Hellwig5fb324a2010-02-16 03:44:52 -0500572int vfs_quota_sync(struct super_block *sb, int type, int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573{
574 struct list_head *dirty;
575 struct dquot *dquot;
576 struct quota_info *dqopt = sb_dqopt(sb);
577 int cnt;
578
Ingo Molnard3be9152006-03-23 03:00:29 -0800579 mutex_lock(&dqopt->dqonoff_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
581 if (type != -1 && cnt != type)
582 continue;
Jan Karaf55abc02008-08-20 17:50:32 +0200583 if (!sb_has_quota_active(sb, cnt))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 continue;
585 spin_lock(&dq_list_lock);
586 dirty = &dqopt->info[cnt].dqi_dirty_list;
587 while (!list_empty(dirty)) {
Jan Kara268157b2009-01-27 15:47:22 +0100588 dquot = list_first_entry(dirty, struct dquot,
589 dq_dirty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 /* Dirty and inactive can be only bad dquot... */
591 if (!test_bit(DQ_ACTIVE_B, &dquot->dq_flags)) {
592 clear_dquot_dirty(dquot);
593 continue;
594 }
595 /* Now we have active dquot from which someone is
596 * holding reference so we can safely just increase
597 * use count */
598 atomic_inc(&dquot->dq_count);
599 dqstats.lookups++;
600 spin_unlock(&dq_list_lock);
601 sb->dq_op->write_dquot(dquot);
602 dqput(dquot);
603 spin_lock(&dq_list_lock);
604 }
605 spin_unlock(&dq_list_lock);
606 }
607
608 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
Jan Karaf55abc02008-08-20 17:50:32 +0200609 if ((cnt == type || type == -1) && sb_has_quota_active(sb, cnt)
610 && info_dirty(&dqopt->info[cnt]))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 sb->dq_op->write_info(sb, cnt);
612 spin_lock(&dq_list_lock);
613 dqstats.syncs++;
614 spin_unlock(&dq_list_lock);
Ingo Molnard3be9152006-03-23 03:00:29 -0800615 mutex_unlock(&dqopt->dqonoff_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616
Christoph Hellwig5fb324a2010-02-16 03:44:52 -0500617 if (!wait || (sb_dqopt(sb)->flags & DQUOT_QUOTA_SYS_FILE))
618 return 0;
619
620 /* This is not very clever (and fast) but currently I don't know about
621 * any other simple way of getting quota data to disk and we must get
622 * them there for userspace to be visible... */
623 if (sb->s_op->sync_fs)
624 sb->s_op->sync_fs(sb, 1);
625 sync_blockdev(sb->s_bdev);
626
627 /*
628 * Now when everything is written we can discard the pagecache so
629 * that userspace sees the changes.
630 */
631 mutex_lock(&sb_dqopt(sb)->dqonoff_mutex);
632 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
633 if (type != -1 && cnt != type)
634 continue;
635 if (!sb_has_quota_active(sb, cnt))
636 continue;
637 mutex_lock_nested(&sb_dqopt(sb)->files[cnt]->i_mutex,
638 I_MUTEX_QUOTA);
639 truncate_inode_pages(&sb_dqopt(sb)->files[cnt]->i_data, 0);
640 mutex_unlock(&sb_dqopt(sb)->files[cnt]->i_mutex);
641 }
642 mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
643
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 return 0;
645}
Mingming Cao08d03502009-01-14 16:19:32 +0100646EXPORT_SYMBOL(vfs_quota_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647
648/* Free unused dquots from cache */
649static void prune_dqcache(int count)
650{
651 struct list_head *head;
652 struct dquot *dquot;
653
654 head = free_dquots.prev;
655 while (head != &free_dquots && count) {
656 dquot = list_entry(head, struct dquot, dq_free);
657 remove_dquot_hash(dquot);
658 remove_free_dquot(dquot);
659 remove_inuse(dquot);
Jan Kara74f783a2008-08-19 14:51:22 +0200660 do_destroy_dquot(dquot);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 count--;
662 head = free_dquots.prev;
663 }
664}
665
666/*
667 * This is called from kswapd when we think we need some
668 * more memory
669 */
670
Al Viro27496a82005-10-21 03:20:48 -0400671static int shrink_dqcache_memory(int nr, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672{
673 if (nr) {
674 spin_lock(&dq_list_lock);
675 prune_dqcache(nr);
676 spin_unlock(&dq_list_lock);
677 }
678 return (dqstats.free_dquots / 100) * sysctl_vfs_cache_pressure;
679}
680
Rusty Russell8e1f9362007-07-17 04:03:17 -0700681static struct shrinker dqcache_shrinker = {
682 .shrink = shrink_dqcache_memory,
683 .seeks = DEFAULT_SEEKS,
684};
685
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686/*
687 * Put reference to dquot
688 * NOTE: If you change this function please check whether dqput_blocks() works right...
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 */
Jan Kara3d9ea252008-10-10 16:12:23 +0200690void dqput(struct dquot *dquot)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691{
Jan Karab48d3802008-07-25 01:46:49 -0700692 int ret;
693
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 if (!dquot)
695 return;
Jan Kara62af9b52010-04-19 16:47:20 +0200696#ifdef CONFIG_QUOTA_DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 if (!atomic_read(&dquot->dq_count)) {
698 printk("VFS: dqput: trying to free free dquot\n");
699 printk("VFS: device %s, dquot of %s %d\n",
700 dquot->dq_sb->s_id,
701 quotatypes[dquot->dq_type],
702 dquot->dq_id);
703 BUG();
704 }
705#endif
706
707 spin_lock(&dq_list_lock);
708 dqstats.drops++;
709 spin_unlock(&dq_list_lock);
710we_slept:
711 spin_lock(&dq_list_lock);
712 if (atomic_read(&dquot->dq_count) > 1) {
713 /* We have more than one user... nothing to do */
714 atomic_dec(&dquot->dq_count);
Jan Kara6362e4d2006-03-23 03:00:17 -0800715 /* Releasing dquot during quotaoff phase? */
Jan Karaf55abc02008-08-20 17:50:32 +0200716 if (!sb_has_quota_active(dquot->dq_sb, dquot->dq_type) &&
Jan Kara6362e4d2006-03-23 03:00:17 -0800717 atomic_read(&dquot->dq_count) == 1)
718 wake_up(&dquot->dq_wait_unused);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 spin_unlock(&dq_list_lock);
720 return;
721 }
722 /* Need to release dquot? */
723 if (test_bit(DQ_ACTIVE_B, &dquot->dq_flags) && dquot_dirty(dquot)) {
724 spin_unlock(&dq_list_lock);
725 /* Commit dquot before releasing */
Jan Karab48d3802008-07-25 01:46:49 -0700726 ret = dquot->dq_sb->dq_op->write_dquot(dquot);
727 if (ret < 0) {
728 printk(KERN_ERR "VFS: cannot write quota structure on "
729 "device %s (error %d). Quota may get out of "
730 "sync!\n", dquot->dq_sb->s_id, ret);
731 /*
732 * We clear dirty bit anyway, so that we avoid
733 * infinite loop here
734 */
735 spin_lock(&dq_list_lock);
736 clear_dquot_dirty(dquot);
737 spin_unlock(&dq_list_lock);
738 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 goto we_slept;
740 }
741 /* Clear flag in case dquot was inactive (something bad happened) */
742 clear_dquot_dirty(dquot);
743 if (test_bit(DQ_ACTIVE_B, &dquot->dq_flags)) {
744 spin_unlock(&dq_list_lock);
745 dquot->dq_sb->dq_op->release_dquot(dquot);
746 goto we_slept;
747 }
748 atomic_dec(&dquot->dq_count);
Jan Kara62af9b52010-04-19 16:47:20 +0200749#ifdef CONFIG_QUOTA_DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 /* sanity check */
Eric Sesterhenn8abf6a42006-04-02 13:36:13 +0200751 BUG_ON(!list_empty(&dquot->dq_free));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752#endif
753 put_dquot_last(dquot);
754 spin_unlock(&dq_list_lock);
755}
Mingming Cao08d03502009-01-14 16:19:32 +0100756EXPORT_SYMBOL(dqput);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757
Jan Kara7d9056b2008-11-25 15:31:32 +0100758struct dquot *dquot_alloc(struct super_block *sb, int type)
Jan Kara74f783a2008-08-19 14:51:22 +0200759{
760 return kmem_cache_zalloc(dquot_cachep, GFP_NOFS);
761}
Jan Kara7d9056b2008-11-25 15:31:32 +0100762EXPORT_SYMBOL(dquot_alloc);
Jan Kara74f783a2008-08-19 14:51:22 +0200763
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764static struct dquot *get_empty_dquot(struct super_block *sb, int type)
765{
766 struct dquot *dquot;
767
Jan Kara74f783a2008-08-19 14:51:22 +0200768 dquot = sb->dq_op->alloc_dquot(sb, type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 if(!dquot)
Jan Karadd6f3c62009-01-26 16:01:43 +0100770 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771
Ingo Molnard3be9152006-03-23 03:00:29 -0800772 mutex_init(&dquot->dq_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 INIT_LIST_HEAD(&dquot->dq_free);
774 INIT_LIST_HEAD(&dquot->dq_inuse);
775 INIT_HLIST_NODE(&dquot->dq_hash);
776 INIT_LIST_HEAD(&dquot->dq_dirty);
Jan Kara6362e4d2006-03-23 03:00:17 -0800777 init_waitqueue_head(&dquot->dq_wait_unused);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 dquot->dq_sb = sb;
779 dquot->dq_type = type;
780 atomic_set(&dquot->dq_count, 1);
781
782 return dquot;
783}
784
785/*
786 * Get reference to dquot
Jan Karacc334122009-01-12 17:23:05 +0100787 *
788 * Locking is slightly tricky here. We are guarded from parallel quotaoff()
789 * destroying our dquot by:
790 * a) checking for quota flags under dq_list_lock and
791 * b) getting a reference to dquot before we release dq_list_lock
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 */
Jan Kara3d9ea252008-10-10 16:12:23 +0200793struct dquot *dqget(struct super_block *sb, unsigned int id, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794{
795 unsigned int hashent = hashfn(sb, id, type);
Jan Karadd6f3c62009-01-26 16:01:43 +0100796 struct dquot *dquot = NULL, *empty = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797
Jan Karaf55abc02008-08-20 17:50:32 +0200798 if (!sb_has_quota_active(sb, type))
Jan Karadd6f3c62009-01-26 16:01:43 +0100799 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800we_slept:
801 spin_lock(&dq_list_lock);
Jan Karacc334122009-01-12 17:23:05 +0100802 spin_lock(&dq_state_lock);
803 if (!sb_has_quota_active(sb, type)) {
804 spin_unlock(&dq_state_lock);
805 spin_unlock(&dq_list_lock);
806 goto out;
807 }
808 spin_unlock(&dq_state_lock);
809
Jan Karadd6f3c62009-01-26 16:01:43 +0100810 dquot = find_dquot(hashent, sb, id, type);
811 if (!dquot) {
812 if (!empty) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 spin_unlock(&dq_list_lock);
Jan Karadd6f3c62009-01-26 16:01:43 +0100814 empty = get_empty_dquot(sb, type);
815 if (!empty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 schedule(); /* Try to wait for a moment... */
817 goto we_slept;
818 }
819 dquot = empty;
Jan Karadd6f3c62009-01-26 16:01:43 +0100820 empty = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 dquot->dq_id = id;
822 /* all dquots go on the inuse_list */
823 put_inuse(dquot);
824 /* hash it first so it can be found */
825 insert_dquot_hash(dquot);
826 dqstats.lookups++;
827 spin_unlock(&dq_list_lock);
828 } else {
829 if (!atomic_read(&dquot->dq_count))
830 remove_free_dquot(dquot);
831 atomic_inc(&dquot->dq_count);
832 dqstats.cache_hits++;
833 dqstats.lookups++;
834 spin_unlock(&dq_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 }
Jan Kara268157b2009-01-27 15:47:22 +0100836 /* Wait for dq_lock - after this we know that either dquot_release() is
837 * already finished or it will be canceled due to dq_count > 1 test */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 wait_on_dquot(dquot);
Jan Kara268157b2009-01-27 15:47:22 +0100839 /* Read the dquot / allocate space in quota file */
840 if (!test_bit(DQ_ACTIVE_B, &dquot->dq_flags) &&
841 sb->dq_op->acquire_dquot(dquot) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 dqput(dquot);
Jan Karadd6f3c62009-01-26 16:01:43 +0100843 dquot = NULL;
Jan Karacc334122009-01-12 17:23:05 +0100844 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 }
Jan Kara62af9b52010-04-19 16:47:20 +0200846#ifdef CONFIG_QUOTA_DEBUG
Eric Sesterhenn8abf6a42006-04-02 13:36:13 +0200847 BUG_ON(!dquot->dq_sb); /* Has somebody invalidated entry under us? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848#endif
Jan Karacc334122009-01-12 17:23:05 +0100849out:
850 if (empty)
851 do_destroy_dquot(empty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852
853 return dquot;
854}
Mingming Cao08d03502009-01-14 16:19:32 +0100855EXPORT_SYMBOL(dqget);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856
857static int dqinit_needed(struct inode *inode, int type)
858{
859 int cnt;
860
861 if (IS_NOQUOTA(inode))
862 return 0;
863 if (type != -1)
Jan Karadd6f3c62009-01-26 16:01:43 +0100864 return !inode->i_dquot[type];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
Jan Karadd6f3c62009-01-26 16:01:43 +0100866 if (!inode->i_dquot[cnt])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 return 1;
868 return 0;
869}
870
Ingo Molnard3be9152006-03-23 03:00:29 -0800871/* This routine is guarded by dqonoff_mutex mutex */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872static void add_dquot_ref(struct super_block *sb, int type)
873{
Jan Kara941d2382008-02-06 01:37:36 -0800874 struct inode *inode, *old_inode = NULL;
Jan Kara62af9b52010-04-19 16:47:20 +0200875#ifdef CONFIG_QUOTA_DEBUG
Jan Kara0a5a9c72010-02-09 18:20:39 +0100876 int reserved = 0;
Jan Kara4c5e6c02010-04-06 18:52:47 +0200877#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878
Christoph Hellwigd003fb72007-02-12 00:51:58 -0800879 spin_lock(&inode_lock);
880 list_for_each_entry(inode, &sb->s_inodes, i_sb_list) {
Wu Fengguangb6fac632009-04-02 16:56:34 -0700881 if (inode->i_state & (I_FREEING|I_CLEAR|I_WILL_FREE|I_NEW))
Nick Pigginaabb8fd2009-03-11 13:17:36 -0700882 continue;
Jan Kara62af9b52010-04-19 16:47:20 +0200883#ifdef CONFIG_QUOTA_DEBUG
Jan Kara0a5a9c72010-02-09 18:20:39 +0100884 if (unlikely(inode_get_rsv_space(inode) > 0))
885 reserved = 1;
Jan Kara4c5e6c02010-04-06 18:52:47 +0200886#endif
Christoph Hellwigd003fb72007-02-12 00:51:58 -0800887 if (!atomic_read(&inode->i_writecount))
888 continue;
889 if (!dqinit_needed(inode, type))
890 continue;
Christoph Hellwigd003fb72007-02-12 00:51:58 -0800891
892 __iget(inode);
893 spin_unlock(&inode_lock);
894
Jan Kara941d2382008-02-06 01:37:36 -0800895 iput(old_inode);
Christoph Hellwig871a2932010-03-03 09:05:07 -0500896 __dquot_initialize(inode, type);
Jan Kara941d2382008-02-06 01:37:36 -0800897 /* We hold a reference to 'inode' so it couldn't have been
898 * removed from s_inodes list while we dropped the inode_lock.
899 * We cannot iput the inode now as we can be holding the last
900 * reference and we cannot iput it under inode_lock. So we
901 * keep the reference and iput it later. */
902 old_inode = inode;
903 spin_lock(&inode_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 }
Christoph Hellwigd003fb72007-02-12 00:51:58 -0800905 spin_unlock(&inode_lock);
Jan Kara941d2382008-02-06 01:37:36 -0800906 iput(old_inode);
Jan Kara0a5a9c72010-02-09 18:20:39 +0100907
Jan Kara62af9b52010-04-19 16:47:20 +0200908#ifdef CONFIG_QUOTA_DEBUG
Jan Kara0a5a9c72010-02-09 18:20:39 +0100909 if (reserved) {
910 printk(KERN_WARNING "VFS (%s): Writes happened before quota"
911 " was turned on thus quota information is probably "
912 "inconsistent. Please run quotacheck(8).\n", sb->s_id);
913 }
Jan Kara4c5e6c02010-04-06 18:52:47 +0200914#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915}
916
Jan Kara268157b2009-01-27 15:47:22 +0100917/*
918 * Return 0 if dqput() won't block.
919 * (note that 1 doesn't necessarily mean blocking)
920 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921static inline int dqput_blocks(struct dquot *dquot)
922{
923 if (atomic_read(&dquot->dq_count) <= 1)
924 return 1;
925 return 0;
926}
927
Jan Kara268157b2009-01-27 15:47:22 +0100928/*
929 * Remove references to dquots from inode and add dquot to list for freeing
930 * if we have the last referece to dquot
931 * We can't race with anybody because we hold dqptr_sem for writing...
932 */
Adrian Bunke5f00f42007-05-08 00:27:22 -0700933static int remove_inode_dquot_ref(struct inode *inode, int type,
934 struct list_head *tofree_head)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935{
936 struct dquot *dquot = inode->i_dquot[type];
937
Jan Karadd6f3c62009-01-26 16:01:43 +0100938 inode->i_dquot[type] = NULL;
939 if (dquot) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 if (dqput_blocks(dquot)) {
Jan Kara62af9b52010-04-19 16:47:20 +0200941#ifdef CONFIG_QUOTA_DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 if (atomic_read(&dquot->dq_count) != 1)
943 printk(KERN_WARNING "VFS: Adding dquot with dq_count %d to dispose list.\n", atomic_read(&dquot->dq_count));
944#endif
945 spin_lock(&dq_list_lock);
Jan Kara268157b2009-01-27 15:47:22 +0100946 /* As dquot must have currently users it can't be on
947 * the free list... */
948 list_add(&dquot->dq_free, tofree_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 spin_unlock(&dq_list_lock);
950 return 1;
951 }
952 else
953 dqput(dquot); /* We have guaranteed we won't block */
954 }
955 return 0;
956}
957
Jan Kara268157b2009-01-27 15:47:22 +0100958/*
959 * Free list of dquots
960 * Dquots are removed from inodes and no new references can be got so we are
961 * the only ones holding reference
962 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963static void put_dquot_list(struct list_head *tofree_head)
964{
965 struct list_head *act_head;
966 struct dquot *dquot;
967
968 act_head = tofree_head->next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 while (act_head != tofree_head) {
970 dquot = list_entry(act_head, struct dquot, dq_free);
971 act_head = act_head->next;
Jan Kara268157b2009-01-27 15:47:22 +0100972 /* Remove dquot from the list so we won't have problems... */
973 list_del_init(&dquot->dq_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 dqput(dquot);
975 }
976}
977
Christoph Hellwigfb58b732007-02-12 00:51:57 -0800978static void remove_dquot_ref(struct super_block *sb, int type,
979 struct list_head *tofree_head)
980{
981 struct inode *inode;
982
983 spin_lock(&inode_lock);
984 list_for_each_entry(inode, &sb->s_inodes, i_sb_list) {
Nick Pigginaabb8fd2009-03-11 13:17:36 -0700985 /*
986 * We have to scan also I_NEW inodes because they can already
987 * have quota pointer initialized. Luckily, we need to touch
988 * only quota pointers and these have separate locking
989 * (dqptr_sem).
990 */
Christoph Hellwigfb58b732007-02-12 00:51:57 -0800991 if (!IS_NOQUOTA(inode))
992 remove_inode_dquot_ref(inode, type, tofree_head);
993 }
994 spin_unlock(&inode_lock);
995}
996
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997/* Gather all references from inodes and drop them */
998static void drop_dquot_ref(struct super_block *sb, int type)
999{
1000 LIST_HEAD(tofree_head);
1001
Christoph Hellwigfb58b732007-02-12 00:51:57 -08001002 if (sb->dq_op) {
1003 down_write(&sb_dqopt(sb)->dqptr_sem);
1004 remove_dquot_ref(sb, type, &tofree_head);
1005 up_write(&sb_dqopt(sb)->dqptr_sem);
1006 put_dquot_list(&tofree_head);
1007 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008}
1009
Jan Kara12095462008-08-20 14:45:12 +02001010static inline void dquot_incr_inodes(struct dquot *dquot, qsize_t number)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011{
1012 dquot->dq_dqb.dqb_curinodes += number;
1013}
1014
1015static inline void dquot_incr_space(struct dquot *dquot, qsize_t number)
1016{
1017 dquot->dq_dqb.dqb_curspace += number;
1018}
1019
Mingming Caof18df222009-01-13 16:43:09 +01001020static inline void dquot_resv_space(struct dquot *dquot, qsize_t number)
1021{
1022 dquot->dq_dqb.dqb_rsvspace += number;
1023}
1024
Mingming Cao740d9dc2009-01-13 16:43:14 +01001025/*
1026 * Claim reserved quota space
1027 */
Jan Kara0a5a9c72010-02-09 18:20:39 +01001028static void dquot_claim_reserved_space(struct dquot *dquot, qsize_t number)
Mingming Cao740d9dc2009-01-13 16:43:14 +01001029{
Jan Kara0a5a9c72010-02-09 18:20:39 +01001030 if (dquot->dq_dqb.dqb_rsvspace < number) {
1031 WARN_ON_ONCE(1);
1032 number = dquot->dq_dqb.dqb_rsvspace;
1033 }
Mingming Cao740d9dc2009-01-13 16:43:14 +01001034 dquot->dq_dqb.dqb_curspace += number;
1035 dquot->dq_dqb.dqb_rsvspace -= number;
1036}
1037
1038static inline
1039void dquot_free_reserved_space(struct dquot *dquot, qsize_t number)
1040{
Jan Kara0a5a9c72010-02-09 18:20:39 +01001041 if (dquot->dq_dqb.dqb_rsvspace >= number)
1042 dquot->dq_dqb.dqb_rsvspace -= number;
1043 else {
1044 WARN_ON_ONCE(1);
1045 dquot->dq_dqb.dqb_rsvspace = 0;
1046 }
Mingming Cao740d9dc2009-01-13 16:43:14 +01001047}
1048
Jan Kara7a2435d2009-01-27 01:47:11 +01001049static void dquot_decr_inodes(struct dquot *dquot, qsize_t number)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050{
Jan Karadb49d2d2008-10-01 18:21:39 +02001051 if (sb_dqopt(dquot->dq_sb)->flags & DQUOT_NEGATIVE_USAGE ||
1052 dquot->dq_dqb.dqb_curinodes >= number)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 dquot->dq_dqb.dqb_curinodes -= number;
1054 else
1055 dquot->dq_dqb.dqb_curinodes = 0;
1056 if (dquot->dq_dqb.dqb_curinodes <= dquot->dq_dqb.dqb_isoftlimit)
1057 dquot->dq_dqb.dqb_itime = (time_t) 0;
1058 clear_bit(DQ_INODES_B, &dquot->dq_flags);
1059}
1060
Jan Kara7a2435d2009-01-27 01:47:11 +01001061static void dquot_decr_space(struct dquot *dquot, qsize_t number)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062{
Jan Karadb49d2d2008-10-01 18:21:39 +02001063 if (sb_dqopt(dquot->dq_sb)->flags & DQUOT_NEGATIVE_USAGE ||
1064 dquot->dq_dqb.dqb_curspace >= number)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 dquot->dq_dqb.dqb_curspace -= number;
1066 else
1067 dquot->dq_dqb.dqb_curspace = 0;
Jan Kara12095462008-08-20 14:45:12 +02001068 if (dquot->dq_dqb.dqb_curspace <= dquot->dq_dqb.dqb_bsoftlimit)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 dquot->dq_dqb.dqb_btime = (time_t) 0;
1070 clear_bit(DQ_BLKS_B, &dquot->dq_flags);
1071}
1072
Jan Karac5254602007-12-22 14:03:25 -08001073static int warning_issued(struct dquot *dquot, const int warntype)
1074{
1075 int flag = (warntype == QUOTA_NL_BHARDWARN ||
1076 warntype == QUOTA_NL_BSOFTLONGWARN) ? DQ_BLKS_B :
1077 ((warntype == QUOTA_NL_IHARDWARN ||
1078 warntype == QUOTA_NL_ISOFTLONGWARN) ? DQ_INODES_B : 0);
1079
1080 if (!flag)
1081 return 0;
1082 return test_and_set_bit(flag, &dquot->dq_flags);
1083}
1084
Jan Kara8e893462007-10-16 23:29:31 -07001085#ifdef CONFIG_PRINT_QUOTA_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086static int flag_print_warnings = 1;
1087
Jan Kara7a2435d2009-01-27 01:47:11 +01001088static int need_print_warning(struct dquot *dquot)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089{
1090 if (!flag_print_warnings)
1091 return 0;
1092
1093 switch (dquot->dq_type) {
1094 case USRQUOTA:
David Howellsda9592e2008-11-14 10:39:05 +11001095 return current_fsuid() == dquot->dq_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 case GRPQUOTA:
1097 return in_group_p(dquot->dq_id);
1098 }
1099 return 0;
1100}
1101
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102/* Print warning to user which exceeded quota */
Jan Karac5254602007-12-22 14:03:25 -08001103static void print_warning(struct dquot *dquot, const int warntype)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104{
1105 char *msg = NULL;
Peter Zijlstra24ec8392006-12-08 02:36:04 -08001106 struct tty_struct *tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107
Jan Kara657d3bf2008-07-25 01:46:52 -07001108 if (warntype == QUOTA_NL_IHARDBELOW ||
1109 warntype == QUOTA_NL_ISOFTBELOW ||
1110 warntype == QUOTA_NL_BHARDBELOW ||
1111 warntype == QUOTA_NL_BSOFTBELOW || !need_print_warning(dquot))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 return;
1113
Peter Zijlstra24ec8392006-12-08 02:36:04 -08001114 tty = get_current_tty();
1115 if (!tty)
Alan Cox452a00d2008-10-13 10:39:13 +01001116 return;
Peter Zijlstra24ec8392006-12-08 02:36:04 -08001117 tty_write_message(tty, dquot->dq_sb->s_id);
Jan Kara8e893462007-10-16 23:29:31 -07001118 if (warntype == QUOTA_NL_ISOFTWARN || warntype == QUOTA_NL_BSOFTWARN)
Peter Zijlstra24ec8392006-12-08 02:36:04 -08001119 tty_write_message(tty, ": warning, ");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 else
Peter Zijlstra24ec8392006-12-08 02:36:04 -08001121 tty_write_message(tty, ": write failed, ");
1122 tty_write_message(tty, quotatypes[dquot->dq_type]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 switch (warntype) {
Jan Kara8e893462007-10-16 23:29:31 -07001124 case QUOTA_NL_IHARDWARN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 msg = " file limit reached.\r\n";
1126 break;
Jan Kara8e893462007-10-16 23:29:31 -07001127 case QUOTA_NL_ISOFTLONGWARN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 msg = " file quota exceeded too long.\r\n";
1129 break;
Jan Kara8e893462007-10-16 23:29:31 -07001130 case QUOTA_NL_ISOFTWARN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 msg = " file quota exceeded.\r\n";
1132 break;
Jan Kara8e893462007-10-16 23:29:31 -07001133 case QUOTA_NL_BHARDWARN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 msg = " block limit reached.\r\n";
1135 break;
Jan Kara8e893462007-10-16 23:29:31 -07001136 case QUOTA_NL_BSOFTLONGWARN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 msg = " block quota exceeded too long.\r\n";
1138 break;
Jan Kara8e893462007-10-16 23:29:31 -07001139 case QUOTA_NL_BSOFTWARN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 msg = " block quota exceeded.\r\n";
1141 break;
1142 }
Peter Zijlstra24ec8392006-12-08 02:36:04 -08001143 tty_write_message(tty, msg);
Alan Cox452a00d2008-10-13 10:39:13 +01001144 tty_kref_put(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145}
Jan Kara8e893462007-10-16 23:29:31 -07001146#endif
1147
Mingming Caof18df222009-01-13 16:43:09 +01001148/*
1149 * Write warnings to the console and send warning messages over netlink.
1150 *
1151 * Note that this function can sleep.
1152 */
Jan Kara7a2435d2009-01-27 01:47:11 +01001153static void flush_warnings(struct dquot *const *dquots, char *warntype)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154{
Steven Whitehouse86e931a2009-09-28 12:35:17 +01001155 struct dquot *dq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 int i;
1157
Steven Whitehouse86e931a2009-09-28 12:35:17 +01001158 for (i = 0; i < MAXQUOTAS; i++) {
1159 dq = dquots[i];
1160 if (dq && warntype[i] != QUOTA_NL_NOWARN &&
1161 !warning_issued(dq, warntype[i])) {
Jan Kara8e893462007-10-16 23:29:31 -07001162#ifdef CONFIG_PRINT_QUOTA_WARNING
Steven Whitehouse86e931a2009-09-28 12:35:17 +01001163 print_warning(dq, warntype[i]);
Jan Kara8e893462007-10-16 23:29:31 -07001164#endif
Steven Whitehouse86e931a2009-09-28 12:35:17 +01001165 quota_send_warning(dq->dq_type, dq->dq_id,
1166 dq->dq_sb->s_dev, warntype[i]);
Jan Kara8e893462007-10-16 23:29:31 -07001167 }
Steven Whitehouse86e931a2009-09-28 12:35:17 +01001168 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169}
1170
Jan Kara7a2435d2009-01-27 01:47:11 +01001171static int ignore_hardlimit(struct dquot *dquot)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172{
1173 struct mem_dqinfo *info = &sb_dqopt(dquot->dq_sb)->info[dquot->dq_type];
1174
1175 return capable(CAP_SYS_RESOURCE) &&
Jan Kara268157b2009-01-27 15:47:22 +01001176 (info->dqi_format->qf_fmt_id != QFMT_VFS_OLD ||
1177 !(info->dqi_flags & V1_DQF_RSQUASH));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178}
1179
1180/* needs dq_data_lock */
Jan Kara12095462008-08-20 14:45:12 +02001181static int check_idq(struct dquot *dquot, qsize_t inodes, char *warntype)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182{
Jan Kara268157b2009-01-27 15:47:22 +01001183 qsize_t newinodes = dquot->dq_dqb.dqb_curinodes + inodes;
1184
Jan Kara8e893462007-10-16 23:29:31 -07001185 *warntype = QUOTA_NL_NOWARN;
Jan Karaf55abc02008-08-20 17:50:32 +02001186 if (!sb_has_quota_limits_enabled(dquot->dq_sb, dquot->dq_type) ||
1187 test_bit(DQ_FAKE_B, &dquot->dq_flags))
Christoph Hellwigefd8f0e2010-03-03 09:05:08 -05001188 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189
1190 if (dquot->dq_dqb.dqb_ihardlimit &&
Jan Kara268157b2009-01-27 15:47:22 +01001191 newinodes > dquot->dq_dqb.dqb_ihardlimit &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 !ignore_hardlimit(dquot)) {
Jan Kara8e893462007-10-16 23:29:31 -07001193 *warntype = QUOTA_NL_IHARDWARN;
Christoph Hellwigefd8f0e2010-03-03 09:05:08 -05001194 return -EDQUOT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 }
1196
1197 if (dquot->dq_dqb.dqb_isoftlimit &&
Jan Kara268157b2009-01-27 15:47:22 +01001198 newinodes > dquot->dq_dqb.dqb_isoftlimit &&
1199 dquot->dq_dqb.dqb_itime &&
1200 get_seconds() >= dquot->dq_dqb.dqb_itime &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 !ignore_hardlimit(dquot)) {
Jan Kara8e893462007-10-16 23:29:31 -07001202 *warntype = QUOTA_NL_ISOFTLONGWARN;
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 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 dquot->dq_dqb.dqb_itime == 0) {
Jan Kara8e893462007-10-16 23:29:31 -07001209 *warntype = QUOTA_NL_ISOFTWARN;
Jan Kara268157b2009-01-27 15:47:22 +01001210 dquot->dq_dqb.dqb_itime = get_seconds() +
1211 sb_dqopt(dquot->dq_sb)->info[dquot->dq_type].dqi_igrace;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 }
1213
Christoph Hellwigefd8f0e2010-03-03 09:05:08 -05001214 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215}
1216
1217/* needs dq_data_lock */
1218static int check_bdq(struct dquot *dquot, qsize_t space, int prealloc, char *warntype)
1219{
Mingming Caof18df222009-01-13 16:43:09 +01001220 qsize_t tspace;
Jan Kara268157b2009-01-27 15:47:22 +01001221 struct super_block *sb = dquot->dq_sb;
Mingming Caof18df222009-01-13 16:43:09 +01001222
Jan Kara8e893462007-10-16 23:29:31 -07001223 *warntype = QUOTA_NL_NOWARN;
Jan Kara268157b2009-01-27 15:47:22 +01001224 if (!sb_has_quota_limits_enabled(sb, dquot->dq_type) ||
Jan Karaf55abc02008-08-20 17:50:32 +02001225 test_bit(DQ_FAKE_B, &dquot->dq_flags))
Christoph Hellwigefd8f0e2010-03-03 09:05:08 -05001226 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227
Mingming Caof18df222009-01-13 16:43:09 +01001228 tspace = dquot->dq_dqb.dqb_curspace + dquot->dq_dqb.dqb_rsvspace
1229 + space;
1230
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 if (dquot->dq_dqb.dqb_bhardlimit &&
Mingming Caof18df222009-01-13 16:43:09 +01001232 tspace > dquot->dq_dqb.dqb_bhardlimit &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 !ignore_hardlimit(dquot)) {
1234 if (!prealloc)
Jan Kara8e893462007-10-16 23:29:31 -07001235 *warntype = QUOTA_NL_BHARDWARN;
Christoph Hellwigefd8f0e2010-03-03 09:05:08 -05001236 return -EDQUOT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 }
1238
1239 if (dquot->dq_dqb.dqb_bsoftlimit &&
Mingming Caof18df222009-01-13 16:43:09 +01001240 tspace > dquot->dq_dqb.dqb_bsoftlimit &&
Jan Kara268157b2009-01-27 15:47:22 +01001241 dquot->dq_dqb.dqb_btime &&
1242 get_seconds() >= dquot->dq_dqb.dqb_btime &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 !ignore_hardlimit(dquot)) {
1244 if (!prealloc)
Jan Kara8e893462007-10-16 23:29:31 -07001245 *warntype = QUOTA_NL_BSOFTLONGWARN;
Christoph Hellwigefd8f0e2010-03-03 09:05:08 -05001246 return -EDQUOT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 }
1248
1249 if (dquot->dq_dqb.dqb_bsoftlimit &&
Mingming Caof18df222009-01-13 16:43:09 +01001250 tspace > dquot->dq_dqb.dqb_bsoftlimit &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 dquot->dq_dqb.dqb_btime == 0) {
1252 if (!prealloc) {
Jan Kara8e893462007-10-16 23:29:31 -07001253 *warntype = QUOTA_NL_BSOFTWARN;
Jan Kara268157b2009-01-27 15:47:22 +01001254 dquot->dq_dqb.dqb_btime = get_seconds() +
1255 sb_dqopt(sb)->info[dquot->dq_type].dqi_bgrace;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 }
1257 else
1258 /*
1259 * We don't allow preallocation to exceed softlimit so exceeding will
1260 * be always printed
1261 */
Christoph Hellwigefd8f0e2010-03-03 09:05:08 -05001262 return -EDQUOT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 }
1264
Christoph Hellwigefd8f0e2010-03-03 09:05:08 -05001265 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266}
1267
Jan Kara12095462008-08-20 14:45:12 +02001268static int info_idq_free(struct dquot *dquot, qsize_t inodes)
Jan Kara657d3bf2008-07-25 01:46:52 -07001269{
Jan Kara268157b2009-01-27 15:47:22 +01001270 qsize_t newinodes;
1271
Jan Kara657d3bf2008-07-25 01:46:52 -07001272 if (test_bit(DQ_FAKE_B, &dquot->dq_flags) ||
Jan Karaf55abc02008-08-20 17:50:32 +02001273 dquot->dq_dqb.dqb_curinodes <= dquot->dq_dqb.dqb_isoftlimit ||
1274 !sb_has_quota_limits_enabled(dquot->dq_sb, dquot->dq_type))
Jan Kara657d3bf2008-07-25 01:46:52 -07001275 return QUOTA_NL_NOWARN;
1276
Jan Kara268157b2009-01-27 15:47:22 +01001277 newinodes = dquot->dq_dqb.dqb_curinodes - inodes;
1278 if (newinodes <= dquot->dq_dqb.dqb_isoftlimit)
Jan Kara657d3bf2008-07-25 01:46:52 -07001279 return QUOTA_NL_ISOFTBELOW;
1280 if (dquot->dq_dqb.dqb_curinodes >= dquot->dq_dqb.dqb_ihardlimit &&
Jan Kara268157b2009-01-27 15:47:22 +01001281 newinodes < dquot->dq_dqb.dqb_ihardlimit)
Jan Kara657d3bf2008-07-25 01:46:52 -07001282 return QUOTA_NL_IHARDBELOW;
1283 return QUOTA_NL_NOWARN;
1284}
1285
1286static int info_bdq_free(struct dquot *dquot, qsize_t space)
1287{
1288 if (test_bit(DQ_FAKE_B, &dquot->dq_flags) ||
Jan Kara12095462008-08-20 14:45:12 +02001289 dquot->dq_dqb.dqb_curspace <= dquot->dq_dqb.dqb_bsoftlimit)
Jan Kara657d3bf2008-07-25 01:46:52 -07001290 return QUOTA_NL_NOWARN;
1291
Jan Kara12095462008-08-20 14:45:12 +02001292 if (dquot->dq_dqb.dqb_curspace - space <= dquot->dq_dqb.dqb_bsoftlimit)
Jan Kara657d3bf2008-07-25 01:46:52 -07001293 return QUOTA_NL_BSOFTBELOW;
Jan Kara12095462008-08-20 14:45:12 +02001294 if (dquot->dq_dqb.dqb_curspace >= dquot->dq_dqb.dqb_bhardlimit &&
1295 dquot->dq_dqb.dqb_curspace - space < dquot->dq_dqb.dqb_bhardlimit)
Jan Kara657d3bf2008-07-25 01:46:52 -07001296 return QUOTA_NL_BHARDBELOW;
1297 return QUOTA_NL_NOWARN;
1298}
Jan Kara0a5a9c72010-02-09 18:20:39 +01001299
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300/*
Christoph Hellwig871a2932010-03-03 09:05:07 -05001301 * Initialize quota pointers in inode
1302 *
1303 * We do things in a bit complicated way but by that we avoid calling
1304 * dqget() and thus filesystem callbacks under dqptr_sem.
1305 *
1306 * It is better to call this function outside of any transaction as it
1307 * might need a lot of space in journal for dquot structure allocation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 */
Christoph Hellwig871a2932010-03-03 09:05:07 -05001309static void __dquot_initialize(struct inode *inode, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310{
1311 unsigned int id = 0;
Christoph Hellwig871a2932010-03-03 09:05:07 -05001312 int cnt;
Dmitry Monakhovad1e6e82010-02-16 08:31:49 +03001313 struct dquot *got[MAXQUOTAS];
Jan Karacc334122009-01-12 17:23:05 +01001314 struct super_block *sb = inode->i_sb;
Jan Kara0a5a9c72010-02-09 18:20:39 +01001315 qsize_t rsv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316
Ingo Molnard3be9152006-03-23 03:00:29 -08001317 /* First test before acquiring mutex - solves deadlocks when we
1318 * re-enter the quota code and are already holding the mutex */
Christoph Hellwig871a2932010-03-03 09:05:07 -05001319 if (!sb_any_quota_active(inode->i_sb) || IS_NOQUOTA(inode))
1320 return;
Jan Karacc334122009-01-12 17:23:05 +01001321
1322 /* First get references to structures we might need. */
1323 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
Dmitry Monakhovad1e6e82010-02-16 08:31:49 +03001324 got[cnt] = NULL;
Jan Karacc334122009-01-12 17:23:05 +01001325 if (type != -1 && cnt != type)
1326 continue;
1327 switch (cnt) {
1328 case USRQUOTA:
1329 id = inode->i_uid;
1330 break;
1331 case GRPQUOTA:
1332 id = inode->i_gid;
1333 break;
1334 }
1335 got[cnt] = dqget(sb, id, cnt);
1336 }
1337
1338 down_write(&sb_dqopt(sb)->dqptr_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 if (IS_NOQUOTA(inode))
1340 goto out_err;
1341 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1342 if (type != -1 && cnt != type)
1343 continue;
Jan Karacc334122009-01-12 17:23:05 +01001344 /* Avoid races with quotaoff() */
1345 if (!sb_has_quota_active(sb, cnt))
1346 continue;
Jan Karadd6f3c62009-01-26 16:01:43 +01001347 if (!inode->i_dquot[cnt]) {
Jan Karacc334122009-01-12 17:23:05 +01001348 inode->i_dquot[cnt] = got[cnt];
Jan Karadd6f3c62009-01-26 16:01:43 +01001349 got[cnt] = NULL;
Jan Kara0a5a9c72010-02-09 18:20:39 +01001350 /*
1351 * Make quota reservation system happy if someone
1352 * did a write before quota was turned on
1353 */
1354 rsv = inode_get_rsv_space(inode);
1355 if (unlikely(rsv))
1356 dquot_resv_space(inode->i_dquot[cnt], rsv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 }
1358 }
1359out_err:
Jan Karacc334122009-01-12 17:23:05 +01001360 up_write(&sb_dqopt(sb)->dqptr_sem);
1361 /* Drop unused references */
Dmitry Monakhovdc52dd32009-12-14 15:21:15 +03001362 dqput_all(got);
Christoph Hellwig871a2932010-03-03 09:05:07 -05001363}
1364
1365void dquot_initialize(struct inode *inode)
1366{
1367 __dquot_initialize(inode, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368}
Mingming Cao08d03502009-01-14 16:19:32 +01001369EXPORT_SYMBOL(dquot_initialize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370
1371/*
1372 * Release all quotas referenced by inode
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373 */
Christoph Hellwig9f754752010-03-03 09:05:05 -05001374static void __dquot_drop(struct inode *inode)
Jan Kara3d9ea252008-10-10 16:12:23 +02001375{
Jan Karacc334122009-01-12 17:23:05 +01001376 int cnt;
1377 struct dquot *put[MAXQUOTAS];
1378
Jan Kara3d9ea252008-10-10 16:12:23 +02001379 down_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
Jan Karacc334122009-01-12 17:23:05 +01001380 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1381 put[cnt] = inode->i_dquot[cnt];
Jan Karadd6f3c62009-01-26 16:01:43 +01001382 inode->i_dquot[cnt] = NULL;
Jan Karacc334122009-01-12 17:23:05 +01001383 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 up_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
Dmitry Monakhovdc52dd32009-12-14 15:21:15 +03001385 dqput_all(put);
Christoph Hellwig9f754752010-03-03 09:05:05 -05001386}
1387
1388void dquot_drop(struct inode *inode)
1389{
1390 int cnt;
1391
1392 if (IS_NOQUOTA(inode))
1393 return;
1394
1395 /*
1396 * Test before calling to rule out calls from proc and such
1397 * where we are not allowed to block. Note that this is
1398 * actually reliable test even without the lock - the caller
1399 * must assure that nobody can come after the DQUOT_DROP and
1400 * add quota pointers back anyway.
1401 */
1402 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1403 if (inode->i_dquot[cnt])
1404 break;
1405 }
1406
1407 if (cnt < MAXQUOTAS)
1408 __dquot_drop(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409}
Mingming Cao08d03502009-01-14 16:19:32 +01001410EXPORT_SYMBOL(dquot_drop);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411
1412/*
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001413 * inode_reserved_space is managed internally by quota, and protected by
1414 * i_lock similar to i_blocks+i_bytes.
1415 */
1416static qsize_t *inode_reserved_space(struct inode * inode)
1417{
1418 /* Filesystem must explicitly define it's own method in order to use
1419 * quota reservation interface */
1420 BUG_ON(!inode->i_sb->dq_op->get_reserved_space);
1421 return inode->i_sb->dq_op->get_reserved_space(inode);
1422}
1423
Dmitry Monakhovc4690702010-02-09 17:53:36 +01001424void inode_add_rsv_space(struct inode *inode, qsize_t number)
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001425{
1426 spin_lock(&inode->i_lock);
1427 *inode_reserved_space(inode) += number;
1428 spin_unlock(&inode->i_lock);
1429}
Dmitry Monakhovc4690702010-02-09 17:53:36 +01001430EXPORT_SYMBOL(inode_add_rsv_space);
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001431
Dmitry Monakhovc4690702010-02-09 17:53:36 +01001432void inode_claim_rsv_space(struct inode *inode, qsize_t number)
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001433{
1434 spin_lock(&inode->i_lock);
1435 *inode_reserved_space(inode) -= number;
1436 __inode_add_bytes(inode, number);
1437 spin_unlock(&inode->i_lock);
1438}
Dmitry Monakhovc4690702010-02-09 17:53:36 +01001439EXPORT_SYMBOL(inode_claim_rsv_space);
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001440
Dmitry Monakhovc4690702010-02-09 17:53:36 +01001441void inode_sub_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 spin_unlock(&inode->i_lock);
1446}
Dmitry Monakhovc4690702010-02-09 17:53:36 +01001447EXPORT_SYMBOL(inode_sub_rsv_space);
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001448
1449static qsize_t inode_get_rsv_space(struct inode *inode)
1450{
1451 qsize_t ret;
Jan Kara05b5d892010-01-06 18:03:36 +01001452
1453 if (!inode->i_sb->dq_op->get_reserved_space)
1454 return 0;
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001455 spin_lock(&inode->i_lock);
1456 ret = *inode_reserved_space(inode);
1457 spin_unlock(&inode->i_lock);
1458 return ret;
1459}
1460
1461static void inode_incr_space(struct inode *inode, qsize_t number,
1462 int reserve)
1463{
1464 if (reserve)
1465 inode_add_rsv_space(inode, number);
1466 else
1467 inode_add_bytes(inode, number);
1468}
1469
1470static void inode_decr_space(struct inode *inode, qsize_t number, int reserve)
1471{
1472 if (reserve)
1473 inode_sub_rsv_space(inode, number);
1474 else
1475 inode_sub_bytes(inode, number);
1476}
1477
1478/*
Christoph Hellwig5dd40562010-03-03 09:05:00 -05001479 * This functions updates i_blocks+i_bytes fields and quota information
1480 * (together with appropriate checks).
1481 *
1482 * NOTE: We absolutely rely on the fact that caller dirties the inode
1483 * (usually helpers in quotaops.h care about this) and holds a handle for
1484 * the current transaction so that dquot write and inode write go into the
1485 * same transaction.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486 */
1487
1488/*
1489 * This operation can block, but only after everything is updated
1490 */
Eric Sandeen56246f92010-05-16 09:00:00 -04001491int __dquot_alloc_space(struct inode *inode, qsize_t number, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492{
Christoph Hellwig5dd40562010-03-03 09:05:00 -05001493 int cnt, ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 char warntype[MAXQUOTAS];
Eric Sandeen56246f92010-05-16 09:00:00 -04001495 int warn = flags & DQUOT_SPACE_WARN;
1496 int reserve = flags & DQUOT_SPACE_RESERVE;
Eric Sandeen0e058422010-05-16 10:00:00 -04001497 int nofail = flags & DQUOT_SPACE_NOFAIL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001499 /*
1500 * First test before acquiring mutex - solves deadlocks when we
1501 * re-enter the quota code and are already holding the mutex
1502 */
Christoph Hellwig5dd40562010-03-03 09:05:00 -05001503 if (!sb_any_quota_active(inode->i_sb) || IS_NOQUOTA(inode)) {
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001504 inode_incr_space(inode, number, reserve);
1505 goto out;
1506 }
1507
1508 down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
Jan Kara8e893462007-10-16 23:29:31 -07001510 warntype[cnt] = QUOTA_NL_NOWARN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512 spin_lock(&dq_data_lock);
1513 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
Jan Karadd6f3c62009-01-26 16:01:43 +01001514 if (!inode->i_dquot[cnt])
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515 continue;
Christoph Hellwigefd8f0e2010-03-03 09:05:08 -05001516 ret = check_bdq(inode->i_dquot[cnt], number, !warn,
1517 warntype+cnt);
Eric Sandeen0e058422010-05-16 10:00:00 -04001518 if (ret && !nofail) {
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001519 spin_unlock(&dq_data_lock);
1520 goto out_flush_warn;
Mingming Caof18df222009-01-13 16:43:09 +01001521 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522 }
1523 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
Jan Karadd6f3c62009-01-26 16:01:43 +01001524 if (!inode->i_dquot[cnt])
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525 continue;
Mingming Caof18df222009-01-13 16:43:09 +01001526 if (reserve)
1527 dquot_resv_space(inode->i_dquot[cnt], number);
1528 else
1529 dquot_incr_space(inode->i_dquot[cnt], number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530 }
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001531 inode_incr_space(inode, number, reserve);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532 spin_unlock(&dq_data_lock);
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001533
1534 if (reserve)
1535 goto out_flush_warn;
Dmitry Monakhovdc52dd32009-12-14 15:21:15 +03001536 mark_all_dquot_dirty(inode->i_dquot);
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001537out_flush_warn:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538 flush_warnings(inode->i_dquot, warntype);
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001539 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1540out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541 return ret;
1542}
Christoph Hellwig5dd40562010-03-03 09:05:00 -05001543EXPORT_SYMBOL(__dquot_alloc_space);
Mingming Caof18df222009-01-13 16:43:09 +01001544
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545/*
1546 * This operation can block, but only after everything is updated
1547 */
Christoph Hellwig63936dd2010-03-03 09:05:01 -05001548int dquot_alloc_inode(const struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549{
Christoph Hellwigefd8f0e2010-03-03 09:05:08 -05001550 int cnt, ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551 char warntype[MAXQUOTAS];
1552
Ingo Molnard3be9152006-03-23 03:00:29 -08001553 /* First test before acquiring mutex - solves deadlocks when we
1554 * re-enter the quota code and are already holding the mutex */
Christoph Hellwig63936dd2010-03-03 09:05:01 -05001555 if (!sb_any_quota_active(inode->i_sb) || IS_NOQUOTA(inode))
1556 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
Jan Kara8e893462007-10-16 23:29:31 -07001558 warntype[cnt] = QUOTA_NL_NOWARN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559 down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560 spin_lock(&dq_data_lock);
1561 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
Jan Karadd6f3c62009-01-26 16:01:43 +01001562 if (!inode->i_dquot[cnt])
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563 continue;
Christoph Hellwigefd8f0e2010-03-03 09:05:08 -05001564 ret = check_idq(inode->i_dquot[cnt], 1, warntype + cnt);
1565 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566 goto warn_put_all;
1567 }
1568
1569 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
Jan Karadd6f3c62009-01-26 16:01:43 +01001570 if (!inode->i_dquot[cnt])
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571 continue;
Christoph Hellwig63936dd2010-03-03 09:05:01 -05001572 dquot_incr_inodes(inode->i_dquot[cnt], 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573 }
Christoph Hellwigefd8f0e2010-03-03 09:05:08 -05001574
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575warn_put_all:
1576 spin_unlock(&dq_data_lock);
Christoph Hellwig63936dd2010-03-03 09:05:01 -05001577 if (ret == 0)
Dmitry Monakhovdc52dd32009-12-14 15:21:15 +03001578 mark_all_dquot_dirty(inode->i_dquot);
Jan Kara087ee8d2007-12-17 16:20:26 -08001579 flush_warnings(inode->i_dquot, warntype);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1581 return ret;
1582}
Mingming Cao08d03502009-01-14 16:19:32 +01001583EXPORT_SYMBOL(dquot_alloc_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584
Christoph Hellwig5dd40562010-03-03 09:05:00 -05001585/*
1586 * Convert in-memory reserved quotas to real consumed quotas
1587 */
1588int dquot_claim_space_nodirty(struct inode *inode, qsize_t number)
Mingming Cao740d9dc2009-01-13 16:43:14 +01001589{
1590 int cnt;
Mingming Cao740d9dc2009-01-13 16:43:14 +01001591
Christoph Hellwig5dd40562010-03-03 09:05:00 -05001592 if (!sb_any_quota_active(inode->i_sb) || IS_NOQUOTA(inode)) {
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001593 inode_claim_rsv_space(inode, number);
Christoph Hellwig5dd40562010-03-03 09:05:00 -05001594 return 0;
Mingming Cao740d9dc2009-01-13 16:43:14 +01001595 }
1596
1597 down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
Mingming Cao740d9dc2009-01-13 16:43:14 +01001598 spin_lock(&dq_data_lock);
1599 /* Claim reserved quotas to allocated quotas */
1600 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
Jan Karadd6f3c62009-01-26 16:01:43 +01001601 if (inode->i_dquot[cnt])
Mingming Cao740d9dc2009-01-13 16:43:14 +01001602 dquot_claim_reserved_space(inode->i_dquot[cnt],
1603 number);
1604 }
1605 /* Update inode bytes */
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001606 inode_claim_rsv_space(inode, number);
Mingming Cao740d9dc2009-01-13 16:43:14 +01001607 spin_unlock(&dq_data_lock);
Dmitry Monakhovdc52dd32009-12-14 15:21:15 +03001608 mark_all_dquot_dirty(inode->i_dquot);
Mingming Cao740d9dc2009-01-13 16:43:14 +01001609 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
Christoph Hellwig5dd40562010-03-03 09:05:00 -05001610 return 0;
Mingming Cao740d9dc2009-01-13 16:43:14 +01001611}
Christoph Hellwig5dd40562010-03-03 09:05:00 -05001612EXPORT_SYMBOL(dquot_claim_space_nodirty);
Mingming Cao740d9dc2009-01-13 16:43:14 +01001613
1614/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615 * This operation can block, but only after everything is updated
1616 */
Eric Sandeen56246f92010-05-16 09:00:00 -04001617void __dquot_free_space(struct inode *inode, qsize_t number, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618{
1619 unsigned int cnt;
Jan Kara657d3bf2008-07-25 01:46:52 -07001620 char warntype[MAXQUOTAS];
Eric Sandeen56246f92010-05-16 09:00:00 -04001621 int reserve = flags & DQUOT_SPACE_RESERVE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622
Ingo Molnard3be9152006-03-23 03:00:29 -08001623 /* First test before acquiring mutex - solves deadlocks when we
1624 * re-enter the quota code and are already holding the mutex */
Christoph Hellwig5dd40562010-03-03 09:05:00 -05001625 if (!sb_any_quota_active(inode->i_sb) || IS_NOQUOTA(inode)) {
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001626 inode_decr_space(inode, number, reserve);
Christoph Hellwig5dd40562010-03-03 09:05:00 -05001627 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628 }
Jan Kara657d3bf2008-07-25 01:46:52 -07001629
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630 down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631 spin_lock(&dq_data_lock);
1632 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
Jan Karadd6f3c62009-01-26 16:01:43 +01001633 if (!inode->i_dquot[cnt])
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634 continue;
Jan Kara657d3bf2008-07-25 01:46:52 -07001635 warntype[cnt] = info_bdq_free(inode->i_dquot[cnt], number);
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001636 if (reserve)
1637 dquot_free_reserved_space(inode->i_dquot[cnt], number);
1638 else
1639 dquot_decr_space(inode->i_dquot[cnt], number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640 }
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001641 inode_decr_space(inode, number, reserve);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642 spin_unlock(&dq_data_lock);
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001643
1644 if (reserve)
1645 goto out_unlock;
Dmitry Monakhovdc52dd32009-12-14 15:21:15 +03001646 mark_all_dquot_dirty(inode->i_dquot);
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001647out_unlock:
Jan Kara657d3bf2008-07-25 01:46:52 -07001648 flush_warnings(inode->i_dquot, warntype);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650}
Christoph Hellwig5dd40562010-03-03 09:05:00 -05001651EXPORT_SYMBOL(__dquot_free_space);
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001652
1653/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654 * This operation can block, but only after everything is updated
1655 */
Christoph Hellwig63936dd2010-03-03 09:05:01 -05001656void dquot_free_inode(const struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657{
1658 unsigned int cnt;
Jan Kara657d3bf2008-07-25 01:46:52 -07001659 char warntype[MAXQUOTAS];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660
Ingo Molnard3be9152006-03-23 03:00:29 -08001661 /* First test before acquiring mutex - solves deadlocks when we
1662 * re-enter the quota code and are already holding the mutex */
Christoph Hellwig63936dd2010-03-03 09:05:01 -05001663 if (!sb_any_quota_active(inode->i_sb) || IS_NOQUOTA(inode))
1664 return;
Jan Kara657d3bf2008-07-25 01:46:52 -07001665
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666 down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667 spin_lock(&dq_data_lock);
1668 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
Jan Karadd6f3c62009-01-26 16:01:43 +01001669 if (!inode->i_dquot[cnt])
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670 continue;
Christoph Hellwig63936dd2010-03-03 09:05:01 -05001671 warntype[cnt] = info_idq_free(inode->i_dquot[cnt], 1);
1672 dquot_decr_inodes(inode->i_dquot[cnt], 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673 }
1674 spin_unlock(&dq_data_lock);
Dmitry Monakhovdc52dd32009-12-14 15:21:15 +03001675 mark_all_dquot_dirty(inode->i_dquot);
Jan Kara657d3bf2008-07-25 01:46:52 -07001676 flush_warnings(inode->i_dquot, warntype);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678}
Mingming Cao08d03502009-01-14 16:19:32 +01001679EXPORT_SYMBOL(dquot_free_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680
1681/*
1682 * Transfer the number of inode and blocks from one diskquota to an other.
1683 *
1684 * This operation can block, but only after everything is updated
1685 * A transaction must be started when entering this function.
1686 */
Christoph Hellwigb43fa822010-03-03 09:05:03 -05001687static int __dquot_transfer(struct inode *inode, qid_t *chid, unsigned long mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688{
Mingming Cao740d9dc2009-01-13 16:43:14 +01001689 qsize_t space, cur_space;
1690 qsize_t rsv_space = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691 struct dquot *transfer_from[MAXQUOTAS];
1692 struct dquot *transfer_to[MAXQUOTAS];
Christoph Hellwigefd8f0e2010-03-03 09:05:08 -05001693 int cnt, ret = 0;
Jan Kara657d3bf2008-07-25 01:46:52 -07001694 char warntype_to[MAXQUOTAS];
1695 char warntype_from_inodes[MAXQUOTAS], warntype_from_space[MAXQUOTAS];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696
Ingo Molnard3be9152006-03-23 03:00:29 -08001697 /* First test before acquiring mutex - solves deadlocks when we
1698 * re-enter the quota code and are already holding the mutex */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699 if (IS_NOQUOTA(inode))
Christoph Hellwigefd8f0e2010-03-03 09:05:08 -05001700 return 0;
Jan Karacc334122009-01-12 17:23:05 +01001701 /* Initialize the arrays */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
Jan Karadd6f3c62009-01-26 16:01:43 +01001703 transfer_from[cnt] = NULL;
1704 transfer_to[cnt] = NULL;
Jan Kara657d3bf2008-07-25 01:46:52 -07001705 warntype_to[cnt] = QUOTA_NL_NOWARN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706 }
Dmitry Monakhov8ddd69d2010-02-16 08:31:50 +03001707 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1708 if (mask & (1 << cnt))
1709 transfer_to[cnt] = dqget(inode->i_sb, chid[cnt], cnt);
1710 }
Jan Karacc334122009-01-12 17:23:05 +01001711 down_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
Jan Karacc334122009-01-12 17:23:05 +01001712 if (IS_NOQUOTA(inode)) { /* File without quota accounting? */
1713 up_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
1714 goto put_all;
1715 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716 spin_lock(&dq_data_lock);
Mingming Cao740d9dc2009-01-13 16:43:14 +01001717 cur_space = inode_get_bytes(inode);
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001718 rsv_space = inode_get_rsv_space(inode);
Mingming Cao740d9dc2009-01-13 16:43:14 +01001719 space = cur_space + rsv_space;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720 /* Build the transfer_from list and check the limits */
1721 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
Jan Karadd6f3c62009-01-26 16:01:43 +01001722 if (!transfer_to[cnt])
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723 continue;
1724 transfer_from[cnt] = inode->i_dquot[cnt];
Christoph Hellwigefd8f0e2010-03-03 09:05:08 -05001725 ret = check_idq(transfer_to[cnt], 1, warntype_to + cnt);
1726 if (ret)
1727 goto over_quota;
1728 ret = check_bdq(transfer_to[cnt], space, 0, warntype_to + cnt);
1729 if (ret)
Jan Karacc334122009-01-12 17:23:05 +01001730 goto over_quota;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731 }
1732
1733 /*
1734 * Finally perform the needed transfer from transfer_from to transfer_to
1735 */
1736 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1737 /*
1738 * Skip changes for same uid or gid or for turned off quota-type.
1739 */
Jan Karadd6f3c62009-01-26 16:01:43 +01001740 if (!transfer_to[cnt])
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741 continue;
1742
1743 /* Due to IO error we might not have transfer_from[] structure */
1744 if (transfer_from[cnt]) {
Jan Kara657d3bf2008-07-25 01:46:52 -07001745 warntype_from_inodes[cnt] =
1746 info_idq_free(transfer_from[cnt], 1);
1747 warntype_from_space[cnt] =
1748 info_bdq_free(transfer_from[cnt], space);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749 dquot_decr_inodes(transfer_from[cnt], 1);
Mingming Cao740d9dc2009-01-13 16:43:14 +01001750 dquot_decr_space(transfer_from[cnt], cur_space);
1751 dquot_free_reserved_space(transfer_from[cnt],
1752 rsv_space);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753 }
1754
1755 dquot_incr_inodes(transfer_to[cnt], 1);
Mingming Cao740d9dc2009-01-13 16:43:14 +01001756 dquot_incr_space(transfer_to[cnt], cur_space);
1757 dquot_resv_space(transfer_to[cnt], rsv_space);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758
1759 inode->i_dquot[cnt] = transfer_to[cnt];
1760 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761 spin_unlock(&dq_data_lock);
Jan Karacc334122009-01-12 17:23:05 +01001762 up_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
1763
Dmitry Monakhovdc52dd32009-12-14 15:21:15 +03001764 mark_all_dquot_dirty(transfer_from);
1765 mark_all_dquot_dirty(transfer_to);
1766 /* The reference we got is transferred to the inode */
1767 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1768 transfer_to[cnt] = NULL;
Jan Karacc334122009-01-12 17:23:05 +01001769warn_put_all:
Jan Kara657d3bf2008-07-25 01:46:52 -07001770 flush_warnings(transfer_to, warntype_to);
1771 flush_warnings(transfer_from, warntype_from_inodes);
1772 flush_warnings(transfer_from, warntype_from_space);
Jan Karacc334122009-01-12 17:23:05 +01001773put_all:
Dmitry Monakhovdc52dd32009-12-14 15:21:15 +03001774 dqput_all(transfer_from);
1775 dqput_all(transfer_to);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776 return ret;
Jan Karacc334122009-01-12 17:23:05 +01001777over_quota:
1778 spin_unlock(&dq_data_lock);
1779 up_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
1780 /* Clear dquot pointers we don't want to dqput() */
1781 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
Jan Karadd6f3c62009-01-26 16:01:43 +01001782 transfer_from[cnt] = NULL;
Jan Karacc334122009-01-12 17:23:05 +01001783 goto warn_put_all;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784}
1785
Dmitry Monakhov8ddd69d2010-02-16 08:31:50 +03001786/* Wrapper for transferring ownership of an inode for uid/gid only
1787 * Called from FSXXX_setattr()
1788 */
Christoph Hellwigb43fa822010-03-03 09:05:03 -05001789int dquot_transfer(struct inode *inode, struct iattr *iattr)
Jan Karab85f4b82008-07-25 01:46:50 -07001790{
Dmitry Monakhov8ddd69d2010-02-16 08:31:50 +03001791 qid_t chid[MAXQUOTAS];
1792 unsigned long mask = 0;
1793
1794 if (iattr->ia_valid & ATTR_UID && iattr->ia_uid != inode->i_uid) {
1795 mask |= 1 << USRQUOTA;
1796 chid[USRQUOTA] = iattr->ia_uid;
1797 }
1798 if (iattr->ia_valid & ATTR_GID && iattr->ia_gid != inode->i_gid) {
1799 mask |= 1 << GRPQUOTA;
1800 chid[GRPQUOTA] = iattr->ia_gid;
1801 }
Jan Karaf55abc02008-08-20 17:50:32 +02001802 if (sb_any_quota_active(inode->i_sb) && !IS_NOQUOTA(inode)) {
Christoph Hellwig871a2932010-03-03 09:05:07 -05001803 dquot_initialize(inode);
Christoph Hellwigefd8f0e2010-03-03 09:05:08 -05001804 return __dquot_transfer(inode, chid, mask);
Jan Karab85f4b82008-07-25 01:46:50 -07001805 }
1806 return 0;
1807}
Christoph Hellwigb43fa822010-03-03 09:05:03 -05001808EXPORT_SYMBOL(dquot_transfer);
Jan Karab85f4b82008-07-25 01:46:50 -07001809
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810/*
1811 * Write info of quota file to disk
1812 */
1813int dquot_commit_info(struct super_block *sb, int type)
1814{
1815 int ret;
1816 struct quota_info *dqopt = sb_dqopt(sb);
1817
Ingo Molnard3be9152006-03-23 03:00:29 -08001818 mutex_lock(&dqopt->dqio_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819 ret = dqopt->ops[type]->write_file_info(sb, type);
Ingo Molnard3be9152006-03-23 03:00:29 -08001820 mutex_unlock(&dqopt->dqio_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821 return ret;
1822}
Mingming Cao08d03502009-01-14 16:19:32 +01001823EXPORT_SYMBOL(dquot_commit_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824
1825/*
1826 * Definitions of diskquota operations.
1827 */
Alexey Dobriyan61e225d2009-09-21 17:01:08 -07001828const struct dquot_operations dquot_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829 .write_dquot = dquot_commit,
1830 .acquire_dquot = dquot_acquire,
1831 .release_dquot = dquot_release,
1832 .mark_dirty = dquot_mark_dquot_dirty,
Jan Kara74f783a2008-08-19 14:51:22 +02001833 .write_info = dquot_commit_info,
1834 .alloc_dquot = dquot_alloc,
1835 .destroy_dquot = dquot_destroy,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836};
1837
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838/*
Christoph Hellwig907f4552010-03-03 09:05:06 -05001839 * Generic helper for ->open on filesystems supporting disk quotas.
1840 */
1841int dquot_file_open(struct inode *inode, struct file *file)
1842{
1843 int error;
1844
1845 error = generic_file_open(inode, file);
1846 if (!error && (file->f_mode & FMODE_WRITE))
Christoph Hellwig871a2932010-03-03 09:05:07 -05001847 dquot_initialize(inode);
Christoph Hellwig907f4552010-03-03 09:05:06 -05001848 return error;
1849}
1850EXPORT_SYMBOL(dquot_file_open);
1851
1852/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853 * Turn quota off on a device. type == -1 ==> quotaoff for all types (umount)
1854 */
Jan Karaf55abc02008-08-20 17:50:32 +02001855int vfs_quota_disable(struct super_block *sb, int type, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856{
Jan Kara0ff5af82008-04-28 02:14:33 -07001857 int cnt, ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858 struct quota_info *dqopt = sb_dqopt(sb);
1859 struct inode *toputinode[MAXQUOTAS];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860
Jan Karaf55abc02008-08-20 17:50:32 +02001861 /* Cannot turn off usage accounting without turning off limits, or
1862 * suspend quotas and simultaneously turn quotas off. */
1863 if ((flags & DQUOT_USAGE_ENABLED && !(flags & DQUOT_LIMITS_ENABLED))
1864 || (flags & DQUOT_SUSPENDED && flags & (DQUOT_LIMITS_ENABLED |
1865 DQUOT_USAGE_ENABLED)))
1866 return -EINVAL;
1867
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868 /* We need to serialize quota_off() for device */
Ingo Molnard3be9152006-03-23 03:00:29 -08001869 mutex_lock(&dqopt->dqonoff_mutex);
Jan Kara9377abd2008-05-12 14:02:08 -07001870
1871 /*
1872 * Skip everything if there's nothing to do. We have to do this because
1873 * sometimes we are called when fill_super() failed and calling
1874 * sync_fs() in such cases does no good.
1875 */
Jan Karaf55abc02008-08-20 17:50:32 +02001876 if (!sb_any_quota_loaded(sb)) {
Jan Kara9377abd2008-05-12 14:02:08 -07001877 mutex_unlock(&dqopt->dqonoff_mutex);
1878 return 0;
1879 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1881 toputinode[cnt] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882 if (type != -1 && cnt != type)
1883 continue;
Jan Karaf55abc02008-08-20 17:50:32 +02001884 if (!sb_has_quota_loaded(sb, cnt))
Jan Kara0ff5af82008-04-28 02:14:33 -07001885 continue;
Jan Karaf55abc02008-08-20 17:50:32 +02001886
1887 if (flags & DQUOT_SUSPENDED) {
Jan Karacc334122009-01-12 17:23:05 +01001888 spin_lock(&dq_state_lock);
Jan Karaf55abc02008-08-20 17:50:32 +02001889 dqopt->flags |=
1890 dquot_state_flag(DQUOT_SUSPENDED, cnt);
Jan Karacc334122009-01-12 17:23:05 +01001891 spin_unlock(&dq_state_lock);
Jan Karaf55abc02008-08-20 17:50:32 +02001892 } else {
Jan Karacc334122009-01-12 17:23:05 +01001893 spin_lock(&dq_state_lock);
Jan Karaf55abc02008-08-20 17:50:32 +02001894 dqopt->flags &= ~dquot_state_flag(flags, cnt);
1895 /* Turning off suspended quotas? */
1896 if (!sb_has_quota_loaded(sb, cnt) &&
1897 sb_has_quota_suspended(sb, cnt)) {
1898 dqopt->flags &= ~dquot_state_flag(
1899 DQUOT_SUSPENDED, cnt);
Jan Karacc334122009-01-12 17:23:05 +01001900 spin_unlock(&dq_state_lock);
Jan Karaf55abc02008-08-20 17:50:32 +02001901 iput(dqopt->files[cnt]);
1902 dqopt->files[cnt] = NULL;
1903 continue;
1904 }
Jan Karacc334122009-01-12 17:23:05 +01001905 spin_unlock(&dq_state_lock);
Jan Kara0ff5af82008-04-28 02:14:33 -07001906 }
Jan Karaf55abc02008-08-20 17:50:32 +02001907
1908 /* We still have to keep quota loaded? */
1909 if (sb_has_quota_loaded(sb, cnt) && !(flags & DQUOT_SUSPENDED))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911
1912 /* Note: these are blocking operations */
1913 drop_dquot_ref(sb, cnt);
1914 invalidate_dquots(sb, cnt);
1915 /*
Jan Kara268157b2009-01-27 15:47:22 +01001916 * Now all dquots should be invalidated, all writes done so we
1917 * should be only users of the info. No locks needed.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918 */
1919 if (info_dirty(&dqopt->info[cnt]))
1920 sb->dq_op->write_info(sb, cnt);
1921 if (dqopt->ops[cnt]->free_file_info)
1922 dqopt->ops[cnt]->free_file_info(sb, cnt);
1923 put_quota_format(dqopt->info[cnt].dqi_format);
1924
1925 toputinode[cnt] = dqopt->files[cnt];
Jan Karaf55abc02008-08-20 17:50:32 +02001926 if (!sb_has_quota_loaded(sb, cnt))
Jan Kara0ff5af82008-04-28 02:14:33 -07001927 dqopt->files[cnt] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928 dqopt->info[cnt].dqi_flags = 0;
1929 dqopt->info[cnt].dqi_igrace = 0;
1930 dqopt->info[cnt].dqi_bgrace = 0;
1931 dqopt->ops[cnt] = NULL;
1932 }
Ingo Molnard3be9152006-03-23 03:00:29 -08001933 mutex_unlock(&dqopt->dqonoff_mutex);
Jan Karaca785ec2008-09-30 17:53:37 +02001934
1935 /* Skip syncing and setting flags if quota files are hidden */
1936 if (dqopt->flags & DQUOT_QUOTA_SYS_FILE)
1937 goto put_inodes;
1938
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939 /* Sync the superblock so that buffers with quota data are written to
Al Viro7b7b1ac2005-11-07 17:13:39 -05001940 * disk (and so userspace sees correct data afterwards). */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941 if (sb->s_op->sync_fs)
1942 sb->s_op->sync_fs(sb, 1);
1943 sync_blockdev(sb->s_bdev);
1944 /* Now the quota files are just ordinary files and we can set the
1945 * inode flags back. Moreover we discard the pagecache so that
1946 * userspace sees the writes we did bypassing the pagecache. We
1947 * must also discard the blockdev buffers so that we see the
1948 * changes done by userspace on the next quotaon() */
1949 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1950 if (toputinode[cnt]) {
Ingo Molnard3be9152006-03-23 03:00:29 -08001951 mutex_lock(&dqopt->dqonoff_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952 /* If quota was reenabled in the meantime, we have
1953 * nothing to do */
Jan Karaf55abc02008-08-20 17:50:32 +02001954 if (!sb_has_quota_loaded(sb, cnt)) {
Jan Kara268157b2009-01-27 15:47:22 +01001955 mutex_lock_nested(&toputinode[cnt]->i_mutex,
1956 I_MUTEX_QUOTA);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957 toputinode[cnt]->i_flags &= ~(S_IMMUTABLE |
1958 S_NOATIME | S_NOQUOTA);
Jan Kara268157b2009-01-27 15:47:22 +01001959 truncate_inode_pages(&toputinode[cnt]->i_data,
1960 0);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001961 mutex_unlock(&toputinode[cnt]->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962 mark_inode_dirty(toputinode[cnt]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963 }
Ingo Molnard3be9152006-03-23 03:00:29 -08001964 mutex_unlock(&dqopt->dqonoff_mutex);
Jan Karaca785ec2008-09-30 17:53:37 +02001965 }
1966 if (sb->s_bdev)
1967 invalidate_bdev(sb->s_bdev);
1968put_inodes:
1969 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1970 if (toputinode[cnt]) {
Jan Kara0ff5af82008-04-28 02:14:33 -07001971 /* On remount RO, we keep the inode pointer so that we
Jan Karaf55abc02008-08-20 17:50:32 +02001972 * can reenable quota on the subsequent remount RW. We
1973 * have to check 'flags' variable and not use sb_has_
1974 * function because another quotaon / quotaoff could
1975 * change global state before we got here. We refuse
1976 * to suspend quotas when there is pending delete on
1977 * the quota file... */
1978 if (!(flags & DQUOT_SUSPENDED))
Jan Kara0ff5af82008-04-28 02:14:33 -07001979 iput(toputinode[cnt]);
1980 else if (!toputinode[cnt]->i_nlink)
1981 ret = -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982 }
Jan Kara0ff5af82008-04-28 02:14:33 -07001983 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984}
Mingming Cao08d03502009-01-14 16:19:32 +01001985EXPORT_SYMBOL(vfs_quota_disable);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986
Jan Karaf55abc02008-08-20 17:50:32 +02001987int vfs_quota_off(struct super_block *sb, int type, int remount)
1988{
1989 return vfs_quota_disable(sb, type, remount ? DQUOT_SUSPENDED :
1990 (DQUOT_USAGE_ENABLED | DQUOT_LIMITS_ENABLED));
1991}
Mingming Cao08d03502009-01-14 16:19:32 +01001992EXPORT_SYMBOL(vfs_quota_off);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993/*
1994 * Turn quotas on on a device
1995 */
1996
Jan Karaf55abc02008-08-20 17:50:32 +02001997/*
1998 * Helper function to turn quotas on when we already have the inode of
1999 * quota file and no quota information is loaded.
2000 */
2001static int vfs_load_quota_inode(struct inode *inode, int type, int format_id,
2002 unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003{
2004 struct quota_format_type *fmt = find_quota_format(format_id);
2005 struct super_block *sb = inode->i_sb;
2006 struct quota_info *dqopt = sb_dqopt(sb);
2007 int error;
2008 int oldflags = -1;
2009
2010 if (!fmt)
2011 return -ESRCH;
2012 if (!S_ISREG(inode->i_mode)) {
2013 error = -EACCES;
2014 goto out_fmt;
2015 }
2016 if (IS_RDONLY(inode)) {
2017 error = -EROFS;
2018 goto out_fmt;
2019 }
2020 if (!sb->s_op->quota_write || !sb->s_op->quota_read) {
2021 error = -EINVAL;
2022 goto out_fmt;
2023 }
Jan Karaf55abc02008-08-20 17:50:32 +02002024 /* Usage always has to be set... */
2025 if (!(flags & DQUOT_USAGE_ENABLED)) {
2026 error = -EINVAL;
2027 goto out_fmt;
2028 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029
Jan Karaca785ec2008-09-30 17:53:37 +02002030 if (!(dqopt->flags & DQUOT_QUOTA_SYS_FILE)) {
Jan Karaab94c392010-02-22 21:07:17 +01002031 /* As we bypass the pagecache we must now flush all the
2032 * dirty data and invalidate caches so that kernel sees
2033 * changes from userspace. It is not enough to just flush
2034 * the quota file since if blocksize < pagesize, invalidation
2035 * of the cache could fail because of other unrelated dirty
2036 * data */
2037 sync_filesystem(sb);
Jan Karaca785ec2008-09-30 17:53:37 +02002038 invalidate_bdev(sb->s_bdev);
2039 }
Ingo Molnard3be9152006-03-23 03:00:29 -08002040 mutex_lock(&dqopt->dqonoff_mutex);
Jan Karaf55abc02008-08-20 17:50:32 +02002041 if (sb_has_quota_loaded(sb, type)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042 error = -EBUSY;
2043 goto out_lock;
2044 }
Jan Karaca785ec2008-09-30 17:53:37 +02002045
2046 if (!(dqopt->flags & DQUOT_QUOTA_SYS_FILE)) {
2047 /* We don't want quota and atime on quota files (deadlocks
2048 * possible) Also nobody should write to the file - we use
2049 * special IO operations which ignore the immutable bit. */
Jan Karadee86562009-07-22 18:12:17 +02002050 mutex_lock_nested(&inode->i_mutex, I_MUTEX_QUOTA);
Jan Kara268157b2009-01-27 15:47:22 +01002051 oldflags = inode->i_flags & (S_NOATIME | S_IMMUTABLE |
2052 S_NOQUOTA);
Jan Karaca785ec2008-09-30 17:53:37 +02002053 inode->i_flags |= S_NOQUOTA | S_NOATIME | S_IMMUTABLE;
Jan Karadee86562009-07-22 18:12:17 +02002054 mutex_unlock(&inode->i_mutex);
Jan Kara26245c92010-01-06 17:20:35 +01002055 /*
2056 * When S_NOQUOTA is set, remove dquot references as no more
2057 * references can be added
2058 */
Christoph Hellwig9f754752010-03-03 09:05:05 -05002059 __dquot_drop(inode);
Jan Karaca785ec2008-09-30 17:53:37 +02002060 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061
2062 error = -EIO;
2063 dqopt->files[type] = igrab(inode);
2064 if (!dqopt->files[type])
2065 goto out_lock;
2066 error = -EINVAL;
2067 if (!fmt->qf_ops->check_quota_file(sb, type))
2068 goto out_file_init;
2069
2070 dqopt->ops[type] = fmt->qf_ops;
2071 dqopt->info[type].dqi_format = fmt;
Jan Kara0ff5af82008-04-28 02:14:33 -07002072 dqopt->info[type].dqi_fmt_id = format_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073 INIT_LIST_HEAD(&dqopt->info[type].dqi_dirty_list);
Ingo Molnard3be9152006-03-23 03:00:29 -08002074 mutex_lock(&dqopt->dqio_mutex);
Jan Kara268157b2009-01-27 15:47:22 +01002075 error = dqopt->ops[type]->read_file_info(sb, type);
2076 if (error < 0) {
Ingo Molnard3be9152006-03-23 03:00:29 -08002077 mutex_unlock(&dqopt->dqio_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078 goto out_file_init;
2079 }
Ingo Molnard3be9152006-03-23 03:00:29 -08002080 mutex_unlock(&dqopt->dqio_mutex);
Jan Karacc334122009-01-12 17:23:05 +01002081 spin_lock(&dq_state_lock);
Jan Karaf55abc02008-08-20 17:50:32 +02002082 dqopt->flags |= dquot_state_flag(flags, type);
Jan Karacc334122009-01-12 17:23:05 +01002083 spin_unlock(&dq_state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084
2085 add_dquot_ref(sb, type);
Ingo Molnard3be9152006-03-23 03:00:29 -08002086 mutex_unlock(&dqopt->dqonoff_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087
2088 return 0;
2089
2090out_file_init:
2091 dqopt->files[type] = NULL;
2092 iput(inode);
2093out_lock:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094 if (oldflags != -1) {
Jan Karadee86562009-07-22 18:12:17 +02002095 mutex_lock_nested(&inode->i_mutex, I_MUTEX_QUOTA);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096 /* Set the flags back (in the case of accidental quotaon()
2097 * on a wrong file we don't want to mess up the flags) */
2098 inode->i_flags &= ~(S_NOATIME | S_NOQUOTA | S_IMMUTABLE);
2099 inode->i_flags |= oldflags;
Jan Karadee86562009-07-22 18:12:17 +02002100 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101 }
Jiaying Zhangd01730d2009-07-07 18:15:21 +02002102 mutex_unlock(&dqopt->dqonoff_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103out_fmt:
2104 put_quota_format(fmt);
2105
2106 return error;
2107}
2108
Jan Kara0ff5af82008-04-28 02:14:33 -07002109/* Reenable quotas on remount RW */
2110static int vfs_quota_on_remount(struct super_block *sb, int type)
2111{
2112 struct quota_info *dqopt = sb_dqopt(sb);
2113 struct inode *inode;
2114 int ret;
Jan Karaf55abc02008-08-20 17:50:32 +02002115 unsigned int flags;
Jan Kara0ff5af82008-04-28 02:14:33 -07002116
2117 mutex_lock(&dqopt->dqonoff_mutex);
2118 if (!sb_has_quota_suspended(sb, type)) {
2119 mutex_unlock(&dqopt->dqonoff_mutex);
2120 return 0;
2121 }
Jan Kara0ff5af82008-04-28 02:14:33 -07002122 inode = dqopt->files[type];
2123 dqopt->files[type] = NULL;
Jan Karacc334122009-01-12 17:23:05 +01002124 spin_lock(&dq_state_lock);
Jan Karaf55abc02008-08-20 17:50:32 +02002125 flags = dqopt->flags & dquot_state_flag(DQUOT_USAGE_ENABLED |
2126 DQUOT_LIMITS_ENABLED, type);
2127 dqopt->flags &= ~dquot_state_flag(DQUOT_STATE_FLAGS, type);
Jan Karacc334122009-01-12 17:23:05 +01002128 spin_unlock(&dq_state_lock);
Jan Kara0ff5af82008-04-28 02:14:33 -07002129 mutex_unlock(&dqopt->dqonoff_mutex);
2130
Jan Karaf55abc02008-08-20 17:50:32 +02002131 flags = dquot_generic_flag(flags, type);
2132 ret = vfs_load_quota_inode(inode, type, dqopt->info[type].dqi_fmt_id,
2133 flags);
Jan Kara0ff5af82008-04-28 02:14:33 -07002134 iput(inode);
2135
2136 return ret;
2137}
2138
Al Viro77e69da2008-08-01 04:29:18 -04002139int vfs_quota_on_path(struct super_block *sb, int type, int format_id,
2140 struct path *path)
2141{
2142 int error = security_quota_on(path->dentry);
2143 if (error)
2144 return error;
2145 /* Quota file not on the same filesystem? */
2146 if (path->mnt->mnt_sb != sb)
2147 error = -EXDEV;
2148 else
Jan Karaf55abc02008-08-20 17:50:32 +02002149 error = vfs_load_quota_inode(path->dentry->d_inode, type,
2150 format_id, DQUOT_USAGE_ENABLED |
2151 DQUOT_LIMITS_ENABLED);
Al Viro77e69da2008-08-01 04:29:18 -04002152 return error;
2153}
Mingming Cao08d03502009-01-14 16:19:32 +01002154EXPORT_SYMBOL(vfs_quota_on_path);
Al Viro77e69da2008-08-01 04:29:18 -04002155
Al Viro82646132008-08-02 00:57:06 -04002156int vfs_quota_on(struct super_block *sb, int type, int format_id, char *name,
Jan Kara0ff5af82008-04-28 02:14:33 -07002157 int remount)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158{
Al Viro82646132008-08-02 00:57:06 -04002159 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160 int error;
2161
Jan Kara0ff5af82008-04-28 02:14:33 -07002162 if (remount)
2163 return vfs_quota_on_remount(sb, type);
2164
Al Viro82646132008-08-02 00:57:06 -04002165 error = kern_path(name, LOOKUP_FOLLOW, &path);
Al Viro77e69da2008-08-01 04:29:18 -04002166 if (!error) {
Al Viro82646132008-08-02 00:57:06 -04002167 error = vfs_quota_on_path(sb, type, format_id, &path);
2168 path_put(&path);
Al Viro77e69da2008-08-01 04:29:18 -04002169 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170 return error;
2171}
Mingming Cao08d03502009-01-14 16:19:32 +01002172EXPORT_SYMBOL(vfs_quota_on);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173
2174/*
Jan Karaf55abc02008-08-20 17:50:32 +02002175 * More powerful function for turning on quotas allowing setting
2176 * of individual quota flags
2177 */
2178int vfs_quota_enable(struct inode *inode, int type, int format_id,
2179 unsigned int flags)
2180{
2181 int ret = 0;
2182 struct super_block *sb = inode->i_sb;
2183 struct quota_info *dqopt = sb_dqopt(sb);
2184
2185 /* Just unsuspend quotas? */
2186 if (flags & DQUOT_SUSPENDED)
2187 return vfs_quota_on_remount(sb, type);
2188 if (!flags)
2189 return 0;
2190 /* Just updating flags needed? */
2191 if (sb_has_quota_loaded(sb, type)) {
2192 mutex_lock(&dqopt->dqonoff_mutex);
2193 /* Now do a reliable test... */
2194 if (!sb_has_quota_loaded(sb, type)) {
2195 mutex_unlock(&dqopt->dqonoff_mutex);
2196 goto load_quota;
2197 }
2198 if (flags & DQUOT_USAGE_ENABLED &&
2199 sb_has_quota_usage_enabled(sb, type)) {
2200 ret = -EBUSY;
2201 goto out_lock;
2202 }
2203 if (flags & DQUOT_LIMITS_ENABLED &&
2204 sb_has_quota_limits_enabled(sb, type)) {
2205 ret = -EBUSY;
2206 goto out_lock;
2207 }
Jan Karacc334122009-01-12 17:23:05 +01002208 spin_lock(&dq_state_lock);
Jan Karaf55abc02008-08-20 17:50:32 +02002209 sb_dqopt(sb)->flags |= dquot_state_flag(flags, type);
Jan Karacc334122009-01-12 17:23:05 +01002210 spin_unlock(&dq_state_lock);
Jan Karaf55abc02008-08-20 17:50:32 +02002211out_lock:
2212 mutex_unlock(&dqopt->dqonoff_mutex);
2213 return ret;
2214 }
2215
2216load_quota:
2217 return vfs_load_quota_inode(inode, type, format_id, flags);
2218}
Mingming Cao08d03502009-01-14 16:19:32 +01002219EXPORT_SYMBOL(vfs_quota_enable);
Jan Karaf55abc02008-08-20 17:50:32 +02002220
2221/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222 * This function is used when filesystem needs to initialize quotas
2223 * during mount time.
2224 */
Christoph Hellwig84de8562005-06-23 00:09:16 -07002225int vfs_quota_on_mount(struct super_block *sb, char *qf_name,
2226 int format_id, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227{
Christoph Hellwig84de8562005-06-23 00:09:16 -07002228 struct dentry *dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229 int error;
2230
Jan Karac56818d2009-11-12 15:42:08 +01002231 mutex_lock(&sb->s_root->d_inode->i_mutex);
Christoph Hellwig2fa389c2005-06-23 00:09:16 -07002232 dentry = lookup_one_len(qf_name, sb->s_root, strlen(qf_name));
Jan Karac56818d2009-11-12 15:42:08 +01002233 mutex_unlock(&sb->s_root->d_inode->i_mutex);
Christoph Hellwig84de8562005-06-23 00:09:16 -07002234 if (IS_ERR(dentry))
2235 return PTR_ERR(dentry);
2236
Jan Kara154f4842005-11-28 13:44:14 -08002237 if (!dentry->d_inode) {
2238 error = -ENOENT;
2239 goto out;
2240 }
2241
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242 error = security_quota_on(dentry);
Christoph Hellwig84de8562005-06-23 00:09:16 -07002243 if (!error)
Jan Karaf55abc02008-08-20 17:50:32 +02002244 error = vfs_load_quota_inode(dentry->d_inode, type, format_id,
2245 DQUOT_USAGE_ENABLED | DQUOT_LIMITS_ENABLED);
Christoph Hellwig84de8562005-06-23 00:09:16 -07002246
Jan Kara154f4842005-11-28 13:44:14 -08002247out:
Christoph Hellwig84de8562005-06-23 00:09:16 -07002248 dput(dentry);
2249 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250}
Mingming Cao08d03502009-01-14 16:19:32 +01002251EXPORT_SYMBOL(vfs_quota_on_mount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252
Jan Karab85f4b82008-07-25 01:46:50 -07002253/* Wrapper to turn on quotas when remounting rw */
2254int vfs_dq_quota_on_remount(struct super_block *sb)
2255{
2256 int cnt;
2257 int ret = 0, err;
2258
2259 if (!sb->s_qcop || !sb->s_qcop->quota_on)
2260 return -ENOSYS;
2261 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
2262 err = sb->s_qcop->quota_on(sb, cnt, 0, NULL, 1);
2263 if (err < 0 && !ret)
2264 ret = err;
2265 }
2266 return ret;
2267}
Mingming Cao08d03502009-01-14 16:19:32 +01002268EXPORT_SYMBOL(vfs_dq_quota_on_remount);
Jan Karab85f4b82008-07-25 01:46:50 -07002269
Jan Kara12095462008-08-20 14:45:12 +02002270static inline qsize_t qbtos(qsize_t blocks)
2271{
2272 return blocks << QIF_DQBLKSIZE_BITS;
2273}
2274
2275static inline qsize_t stoqb(qsize_t space)
2276{
2277 return (space + QIF_DQBLKSIZE - 1) >> QIF_DQBLKSIZE_BITS;
2278}
2279
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280/* Generic routine for getting common part of quota structure */
2281static void do_get_dqblk(struct dquot *dquot, struct if_dqblk *di)
2282{
2283 struct mem_dqblk *dm = &dquot->dq_dqb;
2284
2285 spin_lock(&dq_data_lock);
Jan Kara12095462008-08-20 14:45:12 +02002286 di->dqb_bhardlimit = stoqb(dm->dqb_bhardlimit);
2287 di->dqb_bsoftlimit = stoqb(dm->dqb_bsoftlimit);
Mingming Caof18df222009-01-13 16:43:09 +01002288 di->dqb_curspace = dm->dqb_curspace + dm->dqb_rsvspace;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289 di->dqb_ihardlimit = dm->dqb_ihardlimit;
2290 di->dqb_isoftlimit = dm->dqb_isoftlimit;
2291 di->dqb_curinodes = dm->dqb_curinodes;
2292 di->dqb_btime = dm->dqb_btime;
2293 di->dqb_itime = dm->dqb_itime;
2294 di->dqb_valid = QIF_ALL;
2295 spin_unlock(&dq_data_lock);
2296}
2297
Jan Kara268157b2009-01-27 15:47:22 +01002298int vfs_get_dqblk(struct super_block *sb, int type, qid_t id,
2299 struct if_dqblk *di)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300{
2301 struct dquot *dquot;
2302
Jan Karacc334122009-01-12 17:23:05 +01002303 dquot = dqget(sb, id, type);
Jan Karadd6f3c62009-01-26 16:01:43 +01002304 if (!dquot)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305 return -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306 do_get_dqblk(dquot, di);
2307 dqput(dquot);
Jan Karacc334122009-01-12 17:23:05 +01002308
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309 return 0;
2310}
Mingming Cao08d03502009-01-14 16:19:32 +01002311EXPORT_SYMBOL(vfs_get_dqblk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312
2313/* Generic routine for setting common part of quota structure */
Andrew Perepechko338bf9a2008-04-28 02:14:31 -07002314static int do_set_dqblk(struct dquot *dquot, struct if_dqblk *di)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315{
2316 struct mem_dqblk *dm = &dquot->dq_dqb;
2317 int check_blim = 0, check_ilim = 0;
Andrew Perepechko338bf9a2008-04-28 02:14:31 -07002318 struct mem_dqinfo *dqi = &sb_dqopt(dquot->dq_sb)->info[dquot->dq_type];
2319
2320 if ((di->dqb_valid & QIF_BLIMITS &&
2321 (di->dqb_bhardlimit > dqi->dqi_maxblimit ||
2322 di->dqb_bsoftlimit > dqi->dqi_maxblimit)) ||
2323 (di->dqb_valid & QIF_ILIMITS &&
2324 (di->dqb_ihardlimit > dqi->dqi_maxilimit ||
2325 di->dqb_isoftlimit > dqi->dqi_maxilimit)))
2326 return -ERANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327
2328 spin_lock(&dq_data_lock);
2329 if (di->dqb_valid & QIF_SPACE) {
Mingming Caof18df222009-01-13 16:43:09 +01002330 dm->dqb_curspace = di->dqb_curspace - dm->dqb_rsvspace;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331 check_blim = 1;
Andrew Perepechko08261672010-04-12 22:16:50 +04002332 set_bit(DQ_LASTSET_B + QIF_SPACE_B, &dquot->dq_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002333 }
2334 if (di->dqb_valid & QIF_BLIMITS) {
Jan Kara12095462008-08-20 14:45:12 +02002335 dm->dqb_bsoftlimit = qbtos(di->dqb_bsoftlimit);
2336 dm->dqb_bhardlimit = qbtos(di->dqb_bhardlimit);
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_BLIMITS_B, &dquot->dq_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339 }
2340 if (di->dqb_valid & QIF_INODES) {
2341 dm->dqb_curinodes = di->dqb_curinodes;
2342 check_ilim = 1;
Andrew Perepechko08261672010-04-12 22:16:50 +04002343 set_bit(DQ_LASTSET_B + QIF_INODES_B, &dquot->dq_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344 }
2345 if (di->dqb_valid & QIF_ILIMITS) {
2346 dm->dqb_isoftlimit = di->dqb_isoftlimit;
2347 dm->dqb_ihardlimit = di->dqb_ihardlimit;
2348 check_ilim = 1;
Andrew Perepechko08261672010-04-12 22:16:50 +04002349 set_bit(DQ_LASTSET_B + QIF_ILIMITS_B, &dquot->dq_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350 }
Jan Kara4d59bce2008-10-02 16:48:10 +02002351 if (di->dqb_valid & QIF_BTIME) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352 dm->dqb_btime = di->dqb_btime;
Jan Karae04a88a92009-01-07 18:07:29 -08002353 check_blim = 1;
Andrew Perepechko08261672010-04-12 22:16:50 +04002354 set_bit(DQ_LASTSET_B + QIF_BTIME_B, &dquot->dq_flags);
Jan Kara4d59bce2008-10-02 16:48:10 +02002355 }
2356 if (di->dqb_valid & QIF_ITIME) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357 dm->dqb_itime = di->dqb_itime;
Jan Karae04a88a92009-01-07 18:07:29 -08002358 check_ilim = 1;
Andrew Perepechko08261672010-04-12 22:16:50 +04002359 set_bit(DQ_LASTSET_B + QIF_ITIME_B, &dquot->dq_flags);
Jan Kara4d59bce2008-10-02 16:48:10 +02002360 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361
2362 if (check_blim) {
Jan Kara268157b2009-01-27 15:47:22 +01002363 if (!dm->dqb_bsoftlimit ||
2364 dm->dqb_curspace < dm->dqb_bsoftlimit) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365 dm->dqb_btime = 0;
2366 clear_bit(DQ_BLKS_B, &dquot->dq_flags);
Jan Kara268157b2009-01-27 15:47:22 +01002367 } else if (!(di->dqb_valid & QIF_BTIME))
2368 /* Set grace only if user hasn't provided his own... */
Andrew Perepechko338bf9a2008-04-28 02:14:31 -07002369 dm->dqb_btime = get_seconds() + dqi->dqi_bgrace;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002370 }
2371 if (check_ilim) {
Jan Kara268157b2009-01-27 15:47:22 +01002372 if (!dm->dqb_isoftlimit ||
2373 dm->dqb_curinodes < dm->dqb_isoftlimit) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374 dm->dqb_itime = 0;
2375 clear_bit(DQ_INODES_B, &dquot->dq_flags);
Jan Kara268157b2009-01-27 15:47:22 +01002376 } else if (!(di->dqb_valid & QIF_ITIME))
2377 /* Set grace only if user hasn't provided his own... */
Andrew Perepechko338bf9a2008-04-28 02:14:31 -07002378 dm->dqb_itime = get_seconds() + dqi->dqi_igrace;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002379 }
Jan Kara268157b2009-01-27 15:47:22 +01002380 if (dm->dqb_bhardlimit || dm->dqb_bsoftlimit || dm->dqb_ihardlimit ||
2381 dm->dqb_isoftlimit)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382 clear_bit(DQ_FAKE_B, &dquot->dq_flags);
2383 else
2384 set_bit(DQ_FAKE_B, &dquot->dq_flags);
2385 spin_unlock(&dq_data_lock);
2386 mark_dquot_dirty(dquot);
Andrew Perepechko338bf9a2008-04-28 02:14:31 -07002387
2388 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002389}
2390
Jan Kara268157b2009-01-27 15:47:22 +01002391int vfs_set_dqblk(struct super_block *sb, int type, qid_t id,
2392 struct if_dqblk *di)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002393{
2394 struct dquot *dquot;
Andrew Perepechko338bf9a2008-04-28 02:14:31 -07002395 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002396
Jan Karaf55abc02008-08-20 17:50:32 +02002397 dquot = dqget(sb, id, type);
2398 if (!dquot) {
2399 rc = -ESRCH;
2400 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401 }
Andrew Perepechko338bf9a2008-04-28 02:14:31 -07002402 rc = do_set_dqblk(dquot, di);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002403 dqput(dquot);
Jan Karaf55abc02008-08-20 17:50:32 +02002404out:
Andrew Perepechko338bf9a2008-04-28 02:14:31 -07002405 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406}
Mingming Cao08d03502009-01-14 16:19:32 +01002407EXPORT_SYMBOL(vfs_set_dqblk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002408
2409/* Generic routine for getting common part of quota file information */
2410int vfs_get_dqinfo(struct super_block *sb, int type, struct if_dqinfo *ii)
2411{
2412 struct mem_dqinfo *mi;
2413
Ingo Molnard3be9152006-03-23 03:00:29 -08002414 mutex_lock(&sb_dqopt(sb)->dqonoff_mutex);
Jan Karaf55abc02008-08-20 17:50:32 +02002415 if (!sb_has_quota_active(sb, type)) {
Ingo Molnard3be9152006-03-23 03:00:29 -08002416 mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002417 return -ESRCH;
2418 }
2419 mi = sb_dqopt(sb)->info + type;
2420 spin_lock(&dq_data_lock);
2421 ii->dqi_bgrace = mi->dqi_bgrace;
2422 ii->dqi_igrace = mi->dqi_igrace;
2423 ii->dqi_flags = mi->dqi_flags & DQF_MASK;
2424 ii->dqi_valid = IIF_ALL;
2425 spin_unlock(&dq_data_lock);
Ingo Molnard3be9152006-03-23 03:00:29 -08002426 mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002427 return 0;
2428}
Mingming Cao08d03502009-01-14 16:19:32 +01002429EXPORT_SYMBOL(vfs_get_dqinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002430
2431/* Generic routine for setting common part of quota file information */
2432int vfs_set_dqinfo(struct super_block *sb, int type, struct if_dqinfo *ii)
2433{
2434 struct mem_dqinfo *mi;
Jan Karaf55abc02008-08-20 17:50:32 +02002435 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002436
Ingo Molnard3be9152006-03-23 03:00:29 -08002437 mutex_lock(&sb_dqopt(sb)->dqonoff_mutex);
Jan Karaf55abc02008-08-20 17:50:32 +02002438 if (!sb_has_quota_active(sb, type)) {
2439 err = -ESRCH;
2440 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002441 }
2442 mi = sb_dqopt(sb)->info + type;
2443 spin_lock(&dq_data_lock);
2444 if (ii->dqi_valid & IIF_BGRACE)
2445 mi->dqi_bgrace = ii->dqi_bgrace;
2446 if (ii->dqi_valid & IIF_IGRACE)
2447 mi->dqi_igrace = ii->dqi_igrace;
2448 if (ii->dqi_valid & IIF_FLAGS)
Jan Kara268157b2009-01-27 15:47:22 +01002449 mi->dqi_flags = (mi->dqi_flags & ~DQF_MASK) |
2450 (ii->dqi_flags & DQF_MASK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451 spin_unlock(&dq_data_lock);
2452 mark_info_dirty(sb, type);
2453 /* Force write to disk */
2454 sb->dq_op->write_info(sb, type);
Jan Karaf55abc02008-08-20 17:50:32 +02002455out:
Ingo Molnard3be9152006-03-23 03:00:29 -08002456 mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
Jan Karaf55abc02008-08-20 17:50:32 +02002457 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458}
Mingming Cao08d03502009-01-14 16:19:32 +01002459EXPORT_SYMBOL(vfs_set_dqinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002460
Alexey Dobriyan0d54b212009-09-21 17:01:09 -07002461const struct quotactl_ops vfs_quotactl_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002462 .quota_on = vfs_quota_on,
2463 .quota_off = vfs_quota_off,
2464 .quota_sync = vfs_quota_sync,
2465 .get_info = vfs_get_dqinfo,
2466 .set_info = vfs_set_dqinfo,
2467 .get_dqblk = vfs_get_dqblk,
2468 .set_dqblk = vfs_set_dqblk
2469};
2470
2471static ctl_table fs_dqstats_table[] = {
2472 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473 .procname = "lookups",
2474 .data = &dqstats.lookups,
2475 .maxlen = sizeof(int),
2476 .mode = 0444,
Eric W. Biederman6d456112009-11-16 03:11:48 -08002477 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002478 },
2479 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002480 .procname = "drops",
2481 .data = &dqstats.drops,
2482 .maxlen = sizeof(int),
2483 .mode = 0444,
Eric W. Biederman6d456112009-11-16 03:11:48 -08002484 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002485 },
2486 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002487 .procname = "reads",
2488 .data = &dqstats.reads,
2489 .maxlen = sizeof(int),
2490 .mode = 0444,
Eric W. Biederman6d456112009-11-16 03:11:48 -08002491 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492 },
2493 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002494 .procname = "writes",
2495 .data = &dqstats.writes,
2496 .maxlen = sizeof(int),
2497 .mode = 0444,
Eric W. Biederman6d456112009-11-16 03:11:48 -08002498 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002499 },
2500 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002501 .procname = "cache_hits",
2502 .data = &dqstats.cache_hits,
2503 .maxlen = sizeof(int),
2504 .mode = 0444,
Eric W. Biederman6d456112009-11-16 03:11:48 -08002505 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002506 },
2507 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002508 .procname = "allocated_dquots",
2509 .data = &dqstats.allocated_dquots,
2510 .maxlen = sizeof(int),
2511 .mode = 0444,
Eric W. Biederman6d456112009-11-16 03:11:48 -08002512 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002513 },
2514 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002515 .procname = "free_dquots",
2516 .data = &dqstats.free_dquots,
2517 .maxlen = sizeof(int),
2518 .mode = 0444,
Eric W. Biederman6d456112009-11-16 03:11:48 -08002519 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002520 },
2521 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002522 .procname = "syncs",
2523 .data = &dqstats.syncs,
2524 .maxlen = sizeof(int),
2525 .mode = 0444,
Eric W. Biederman6d456112009-11-16 03:11:48 -08002526 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002527 },
Jan Kara8e893462007-10-16 23:29:31 -07002528#ifdef CONFIG_PRINT_QUOTA_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07002529 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530 .procname = "warnings",
2531 .data = &flag_print_warnings,
2532 .maxlen = sizeof(int),
2533 .mode = 0644,
Eric W. Biederman6d456112009-11-16 03:11:48 -08002534 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002535 },
Jan Kara8e893462007-10-16 23:29:31 -07002536#endif
Eric W. Biedermanab092032009-11-05 14:25:10 -08002537 { },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002538};
2539
2540static ctl_table fs_table[] = {
2541 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002542 .procname = "quota",
2543 .mode = 0555,
2544 .child = fs_dqstats_table,
2545 },
Eric W. Biedermanab092032009-11-05 14:25:10 -08002546 { },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002547};
2548
2549static ctl_table sys_table[] = {
2550 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002551 .procname = "fs",
2552 .mode = 0555,
2553 .child = fs_table,
2554 },
Eric W. Biedermanab092032009-11-05 14:25:10 -08002555 { },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002556};
2557
2558static int __init dquot_init(void)
2559{
2560 int i;
2561 unsigned long nr_hash, order;
2562
2563 printk(KERN_NOTICE "VFS: Disk quotas %s\n", __DQUOT_VERSION__);
2564
Eric W. Biederman0b4d4142007-02-14 00:34:09 -08002565 register_sysctl_table(sys_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002566
Paul Mundt20c2df82007-07-20 10:11:58 +09002567 dquot_cachep = kmem_cache_create("dquot",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002568 sizeof(struct dquot), sizeof(unsigned long) * 4,
Paul Jacksonfffb60f2006-03-24 03:16:06 -08002569 (SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT|
2570 SLAB_MEM_SPREAD|SLAB_PANIC),
Paul Mundt20c2df82007-07-20 10:11:58 +09002571 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002572
2573 order = 0;
2574 dquot_hash = (struct hlist_head *)__get_free_pages(GFP_ATOMIC, order);
2575 if (!dquot_hash)
2576 panic("Cannot create dquot hash table");
2577
2578 /* Find power-of-two hlist_heads which can fit into allocation */
2579 nr_hash = (1UL << order) * PAGE_SIZE / sizeof(struct hlist_head);
2580 dq_hash_bits = 0;
2581 do {
2582 dq_hash_bits++;
2583 } while (nr_hash >> dq_hash_bits);
2584 dq_hash_bits--;
2585
2586 nr_hash = 1UL << dq_hash_bits;
2587 dq_hash_mask = nr_hash - 1;
2588 for (i = 0; i < nr_hash; i++)
2589 INIT_HLIST_HEAD(dquot_hash + i);
2590
2591 printk("Dquot-cache hash table entries: %ld (order %ld, %ld bytes)\n",
2592 nr_hash, order, (PAGE_SIZE << order));
2593
Rusty Russell8e1f9362007-07-17 04:03:17 -07002594 register_shrinker(&dqcache_shrinker);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002595
Linus Torvalds1da177e2005-04-16 15:20:36 -07002596 return 0;
2597}
2598module_init(dquot_init);