blob: e840fa2b112ec7ea15e0030cf2c5eda911be1467 [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.. */
Jan Kara8e893462007-10-16 23:29:31 -070080#ifdef CONFIG_QUOTA_NETLINK_INTERFACE
81#include <net/netlink.h>
82#include <net/genetlink.h>
83#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070084
85#include <asm/uaccess.h>
86
87#define __DQUOT_PARANOIA
88
89/*
Jan Karacc334122009-01-12 17:23:05 +010090 * There are three quota SMP locks. dq_list_lock protects all lists with quotas
91 * and quota formats, dqstats structure containing statistics about the lists
92 * dq_data_lock protects data from dq_dqb and also mem_dqinfo structures and
93 * also guards consistency of dquot->dq_dqb with inode->i_blocks, i_bytes.
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 * i_blocks and i_bytes updates itself are guarded by i_lock acquired directly
Jan Karacc334122009-01-12 17:23:05 +010095 * in inode_add_bytes() and inode_sub_bytes(). dq_state_lock protects
96 * modifications of quota state (on quotaon and quotaoff) and readers who care
97 * about latest values take it as well.
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 *
Jan Karacc334122009-01-12 17:23:05 +010099 * The spinlock ordering is hence: dq_data_lock > dq_list_lock > i_lock,
100 * dq_list_lock > dq_state_lock
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 *
102 * Note that some things (eg. sb pointer, type, id) doesn't change during
103 * the life of the dquot structure and so needn't to be protected by a lock
104 *
105 * Any operation working on dquots via inode pointers must hold dqptr_sem. If
106 * operation is just reading pointers from inode (or not using them at all) the
107 * read lock is enough. If pointers are altered function must hold write lock
108 * (these locking rules also apply for S_NOQUOTA flag in the inode - note that
Jan Karacc334122009-01-12 17:23:05 +0100109 * for altering the flag i_mutex is also needed).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 *
Ingo Molnard3be9152006-03-23 03:00:29 -0800111 * Each dquot has its dq_lock mutex. Locked dquots might not be referenced
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 * from inodes (dquot_alloc_space() and such don't check the dq_lock).
113 * Currently dquot is locked only when it is being read to memory (or space for
114 * it is being allocated) on the first dqget() and when it is being released on
115 * the last dqput(). The allocation and release oparations are serialized by
116 * the dq_lock and by checking the use count in dquot_release(). Write
117 * operations on dquots don't hold dq_lock as they copy data under dq_data_lock
118 * spinlock to internal buffers before writing.
119 *
120 * Lock ordering (including related VFS locks) is the following:
Ingo Molnard3be9152006-03-23 03:00:29 -0800121 * i_mutex > dqonoff_sem > journal_lock > dqptr_sem > dquot->dq_lock >
122 * dqio_mutex
Jan Karacc334122009-01-12 17:23:05 +0100123 * The lock ordering of dqptr_sem imposed by quota code is only dqonoff_sem >
124 * dqptr_sem. But filesystem has to count with the fact that functions such as
125 * dquot_alloc_space() acquire dqptr_sem and they usually have to be called
126 * from inside a transaction to keep filesystem consistency after a crash. Also
127 * filesystems usually want to do some IO on dquot from ->mark_dirty which is
128 * called with dqptr_sem held.
Ingo Molnard3be9152006-03-23 03:00:29 -0800129 * i_mutex on quota files is special (it's below dqio_mutex)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 */
131
Jan Karac5166102009-01-26 15:32:46 +0100132static __cacheline_aligned_in_smp DEFINE_SPINLOCK(dq_list_lock);
133static __cacheline_aligned_in_smp DEFINE_SPINLOCK(dq_state_lock);
134__cacheline_aligned_in_smp DEFINE_SPINLOCK(dq_data_lock);
Mingming Cao08d03502009-01-14 16:19:32 +0100135EXPORT_SYMBOL(dq_data_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136
137static char *quotatypes[] = INITQFNAMES;
138static struct quota_format_type *quota_formats; /* List of registered formats */
139static struct quota_module_name module_names[] = INIT_QUOTA_MODULE_NAMES;
140
141/* SLAB cache for dquot structures */
Christoph Lametere18b8902006-12-06 20:33:20 -0800142static struct kmem_cache *dquot_cachep;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143
144int register_quota_format(struct quota_format_type *fmt)
145{
146 spin_lock(&dq_list_lock);
147 fmt->qf_next = quota_formats;
148 quota_formats = fmt;
149 spin_unlock(&dq_list_lock);
150 return 0;
151}
Mingming Cao08d03502009-01-14 16:19:32 +0100152EXPORT_SYMBOL(register_quota_format);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153
154void unregister_quota_format(struct quota_format_type *fmt)
155{
156 struct quota_format_type **actqf;
157
158 spin_lock(&dq_list_lock);
159 for (actqf = &quota_formats; *actqf && *actqf != fmt; actqf = &(*actqf)->qf_next);
160 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);
171 for (actqf = quota_formats; actqf && actqf->qf_fmt_id != id; actqf = actqf->qf_next);
172 if (!actqf || !try_module_get(actqf->qf_owner)) {
173 int qm;
174
175 spin_unlock(&dq_list_lock);
176
177 for (qm = 0; module_names[qm].qm_fmt_id && module_names[qm].qm_fmt_id != id; qm++);
178 if (!module_names[qm].qm_fmt_id || request_module(module_names[qm].qm_mod_name))
179 return NULL;
180
181 spin_lock(&dq_list_lock);
182 for (actqf = quota_formats; actqf && actqf->qf_fmt_id != id; actqf = actqf->qf_next);
183 if (actqf && !try_module_get(actqf->qf_owner))
184 actqf = NULL;
185 }
186 spin_unlock(&dq_list_lock);
187 return actqf;
188}
189
190static void put_quota_format(struct quota_format_type *fmt)
191{
192 module_put(fmt->qf_owner);
193}
194
195/*
196 * Dquot List Management:
197 * The quota code uses three lists for dquot management: the inuse_list,
198 * free_dquots, and dquot_hash[] array. A single dquot structure may be
199 * on all three lists, depending on its current state.
200 *
201 * All dquots are placed to the end of inuse_list when first created, and this
202 * list is used for invalidate operation, which must look at every dquot.
203 *
204 * Unused dquots (dq_count == 0) are added to the free_dquots list when freed,
205 * and this list is searched whenever we need an available dquot. Dquots are
206 * removed from the list as soon as they are used again, and
207 * dqstats.free_dquots gives the number of dquots on the list. When
208 * dquot is invalidated it's completely released from memory.
209 *
210 * Dquots with a specific identity (device, type and id) are placed on
211 * one of the dquot_hash[] hash chains. The provides an efficient search
212 * mechanism to locate a specific dquot.
213 */
214
215static LIST_HEAD(inuse_list);
216static LIST_HEAD(free_dquots);
217static unsigned int dq_hash_bits, dq_hash_mask;
218static struct hlist_head *dquot_hash;
219
220struct dqstats dqstats;
Mingming Cao08d03502009-01-14 16:19:32 +0100221EXPORT_SYMBOL(dqstats);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223static inline unsigned int
224hashfn(const struct super_block *sb, unsigned int id, int type)
225{
226 unsigned long tmp;
227
228 tmp = (((unsigned long)sb>>L1_CACHE_SHIFT) ^ id) * (MAXQUOTAS - type);
229 return (tmp + (tmp >> dq_hash_bits)) & dq_hash_mask;
230}
231
232/*
233 * Following list functions expect dq_list_lock to be held
234 */
235static inline void insert_dquot_hash(struct dquot *dquot)
236{
237 struct hlist_head *head = dquot_hash + hashfn(dquot->dq_sb, dquot->dq_id, dquot->dq_type);
238 hlist_add_head(&dquot->dq_hash, head);
239}
240
241static inline void remove_dquot_hash(struct dquot *dquot)
242{
243 hlist_del_init(&dquot->dq_hash);
244}
245
246static inline struct dquot *find_dquot(unsigned int hashent, struct super_block *sb, unsigned int id, int type)
247{
248 struct hlist_node *node;
249 struct dquot *dquot;
250
251 hlist_for_each (node, dquot_hash+hashent) {
252 dquot = hlist_entry(node, struct dquot, dq_hash);
253 if (dquot->dq_sb == sb && dquot->dq_id == id && dquot->dq_type == type)
254 return dquot;
255 }
256 return NODQUOT;
257}
258
259/* Add a dquot to the tail of the free list */
260static inline void put_dquot_last(struct dquot *dquot)
261{
Akinobu Mita8e130592006-06-26 00:24:37 -0700262 list_add_tail(&dquot->dq_free, &free_dquots);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 dqstats.free_dquots++;
264}
265
266static inline void remove_free_dquot(struct dquot *dquot)
267{
268 if (list_empty(&dquot->dq_free))
269 return;
270 list_del_init(&dquot->dq_free);
271 dqstats.free_dquots--;
272}
273
274static inline void put_inuse(struct dquot *dquot)
275{
276 /* We add to the back of inuse list so we don't have to restart
277 * when traversing this list and we block */
Akinobu Mita8e130592006-06-26 00:24:37 -0700278 list_add_tail(&dquot->dq_inuse, &inuse_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 dqstats.allocated_dquots++;
280}
281
282static inline void remove_inuse(struct dquot *dquot)
283{
284 dqstats.allocated_dquots--;
285 list_del(&dquot->dq_inuse);
286}
287/*
288 * End of list functions needing dq_list_lock
289 */
290
291static void wait_on_dquot(struct dquot *dquot)
292{
Ingo Molnard3be9152006-03-23 03:00:29 -0800293 mutex_lock(&dquot->dq_lock);
294 mutex_unlock(&dquot->dq_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295}
296
Jan Kara03f6e922008-04-28 02:14:32 -0700297static inline int dquot_dirty(struct dquot *dquot)
298{
299 return test_bit(DQ_MOD_B, &dquot->dq_flags);
300}
301
302static inline int mark_dquot_dirty(struct dquot *dquot)
303{
304 return dquot->dq_sb->dq_op->mark_dirty(dquot);
305}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306
307int dquot_mark_dquot_dirty(struct dquot *dquot)
308{
309 spin_lock(&dq_list_lock);
310 if (!test_and_set_bit(DQ_MOD_B, &dquot->dq_flags))
311 list_add(&dquot->dq_dirty, &sb_dqopt(dquot->dq_sb)->
312 info[dquot->dq_type].dqi_dirty_list);
313 spin_unlock(&dq_list_lock);
314 return 0;
315}
Mingming Cao08d03502009-01-14 16:19:32 +0100316EXPORT_SYMBOL(dquot_mark_dquot_dirty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317
318/* This function needs dq_list_lock */
319static inline int clear_dquot_dirty(struct dquot *dquot)
320{
321 if (!test_and_clear_bit(DQ_MOD_B, &dquot->dq_flags))
322 return 0;
323 list_del_init(&dquot->dq_dirty);
324 return 1;
325}
326
327void mark_info_dirty(struct super_block *sb, int type)
328{
329 set_bit(DQF_INFO_DIRTY_B, &sb_dqopt(sb)->info[type].dqi_flags);
330}
331EXPORT_SYMBOL(mark_info_dirty);
332
333/*
334 * Read dquot from disk and alloc space for it
335 */
336
337int dquot_acquire(struct dquot *dquot)
338{
339 int ret = 0, ret2 = 0;
340 struct quota_info *dqopt = sb_dqopt(dquot->dq_sb);
341
Ingo Molnard3be9152006-03-23 03:00:29 -0800342 mutex_lock(&dquot->dq_lock);
343 mutex_lock(&dqopt->dqio_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 if (!test_bit(DQ_READ_B, &dquot->dq_flags))
345 ret = dqopt->ops[dquot->dq_type]->read_dqblk(dquot);
346 if (ret < 0)
347 goto out_iolock;
348 set_bit(DQ_READ_B, &dquot->dq_flags);
349 /* Instantiate dquot if needed */
350 if (!test_bit(DQ_ACTIVE_B, &dquot->dq_flags) && !dquot->dq_off) {
351 ret = dqopt->ops[dquot->dq_type]->commit_dqblk(dquot);
352 /* Write the info if needed */
353 if (info_dirty(&dqopt->info[dquot->dq_type]))
354 ret2 = dqopt->ops[dquot->dq_type]->write_file_info(dquot->dq_sb, dquot->dq_type);
355 if (ret < 0)
356 goto out_iolock;
357 if (ret2 < 0) {
358 ret = ret2;
359 goto out_iolock;
360 }
361 }
362 set_bit(DQ_ACTIVE_B, &dquot->dq_flags);
363out_iolock:
Ingo Molnard3be9152006-03-23 03:00:29 -0800364 mutex_unlock(&dqopt->dqio_mutex);
365 mutex_unlock(&dquot->dq_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 return ret;
367}
Mingming Cao08d03502009-01-14 16:19:32 +0100368EXPORT_SYMBOL(dquot_acquire);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369
370/*
371 * Write dquot to disk
372 */
373int dquot_commit(struct dquot *dquot)
374{
375 int ret = 0, ret2 = 0;
376 struct quota_info *dqopt = sb_dqopt(dquot->dq_sb);
377
Ingo Molnard3be9152006-03-23 03:00:29 -0800378 mutex_lock(&dqopt->dqio_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 spin_lock(&dq_list_lock);
380 if (!clear_dquot_dirty(dquot)) {
381 spin_unlock(&dq_list_lock);
382 goto out_sem;
383 }
384 spin_unlock(&dq_list_lock);
385 /* Inactive dquot can be only if there was error during read/init
386 * => we have better not writing it */
387 if (test_bit(DQ_ACTIVE_B, &dquot->dq_flags)) {
388 ret = dqopt->ops[dquot->dq_type]->commit_dqblk(dquot);
389 if (info_dirty(&dqopt->info[dquot->dq_type]))
390 ret2 = dqopt->ops[dquot->dq_type]->write_file_info(dquot->dq_sb, dquot->dq_type);
391 if (ret >= 0)
392 ret = ret2;
393 }
394out_sem:
Ingo Molnard3be9152006-03-23 03:00:29 -0800395 mutex_unlock(&dqopt->dqio_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 return ret;
397}
Mingming Cao08d03502009-01-14 16:19:32 +0100398EXPORT_SYMBOL(dquot_commit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399
400/*
401 * Release dquot
402 */
403int dquot_release(struct dquot *dquot)
404{
405 int ret = 0, ret2 = 0;
406 struct quota_info *dqopt = sb_dqopt(dquot->dq_sb);
407
Ingo Molnard3be9152006-03-23 03:00:29 -0800408 mutex_lock(&dquot->dq_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 /* Check whether we are not racing with some other dqget() */
410 if (atomic_read(&dquot->dq_count) > 1)
411 goto out_dqlock;
Ingo Molnard3be9152006-03-23 03:00:29 -0800412 mutex_lock(&dqopt->dqio_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 if (dqopt->ops[dquot->dq_type]->release_dqblk) {
414 ret = dqopt->ops[dquot->dq_type]->release_dqblk(dquot);
415 /* Write the info */
416 if (info_dirty(&dqopt->info[dquot->dq_type]))
417 ret2 = dqopt->ops[dquot->dq_type]->write_file_info(dquot->dq_sb, dquot->dq_type);
418 if (ret >= 0)
419 ret = ret2;
420 }
421 clear_bit(DQ_ACTIVE_B, &dquot->dq_flags);
Ingo Molnard3be9152006-03-23 03:00:29 -0800422 mutex_unlock(&dqopt->dqio_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423out_dqlock:
Ingo Molnard3be9152006-03-23 03:00:29 -0800424 mutex_unlock(&dquot->dq_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 return ret;
426}
Mingming Cao08d03502009-01-14 16:19:32 +0100427EXPORT_SYMBOL(dquot_release);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428
Jan Kara7d9056b2008-11-25 15:31:32 +0100429void dquot_destroy(struct dquot *dquot)
Jan Kara74f783a2008-08-19 14:51:22 +0200430{
431 kmem_cache_free(dquot_cachep, dquot);
432}
Jan Kara7d9056b2008-11-25 15:31:32 +0100433EXPORT_SYMBOL(dquot_destroy);
Jan Kara74f783a2008-08-19 14:51:22 +0200434
435static inline void do_destroy_dquot(struct dquot *dquot)
436{
437 dquot->dq_sb->dq_op->destroy_dquot(dquot);
438}
439
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440/* Invalidate all dquots on the list. Note that this function is called after
441 * quota is disabled and pointers from inodes removed so there cannot be new
Jan Kara6362e4d2006-03-23 03:00:17 -0800442 * quota users. There can still be some users of quotas due to inodes being
443 * just deleted or pruned by prune_icache() (those are not attached to any
Jan Karacc334122009-01-12 17:23:05 +0100444 * list) or parallel quotactl call. We have to wait for such users.
Jan Kara6362e4d2006-03-23 03:00:17 -0800445 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446static void invalidate_dquots(struct super_block *sb, int type)
447{
Domen Puncerc33ed272005-06-25 14:59:36 -0700448 struct dquot *dquot, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449
Jan Kara6362e4d2006-03-23 03:00:17 -0800450restart:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 spin_lock(&dq_list_lock);
Domen Puncerc33ed272005-06-25 14:59:36 -0700452 list_for_each_entry_safe(dquot, tmp, &inuse_list, dq_inuse) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 if (dquot->dq_sb != sb)
454 continue;
455 if (dquot->dq_type != type)
456 continue;
Jan Kara6362e4d2006-03-23 03:00:17 -0800457 /* Wait for dquot users */
458 if (atomic_read(&dquot->dq_count)) {
459 DEFINE_WAIT(wait);
460
461 atomic_inc(&dquot->dq_count);
462 prepare_to_wait(&dquot->dq_wait_unused, &wait,
463 TASK_UNINTERRUPTIBLE);
464 spin_unlock(&dq_list_lock);
465 /* Once dqput() wakes us up, we know it's time to free
466 * the dquot.
467 * IMPORTANT: we rely on the fact that there is always
468 * at most one process waiting for dquot to free.
469 * Otherwise dq_count would be > 1 and we would never
470 * wake up.
471 */
472 if (atomic_read(&dquot->dq_count) > 1)
473 schedule();
474 finish_wait(&dquot->dq_wait_unused, &wait);
475 dqput(dquot);
476 /* At this moment dquot() need not exist (it could be
477 * reclaimed by prune_dqcache(). Hence we must
478 * restart. */
479 goto restart;
480 }
481 /*
482 * Quota now has no users and it has been written on last
483 * dqput()
484 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 remove_dquot_hash(dquot);
486 remove_free_dquot(dquot);
487 remove_inuse(dquot);
Jan Kara74f783a2008-08-19 14:51:22 +0200488 do_destroy_dquot(dquot);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 }
490 spin_unlock(&dq_list_lock);
491}
492
Jan Kara12c77522008-10-20 17:05:00 +0200493/* Call callback for every active dquot on given filesystem */
494int dquot_scan_active(struct super_block *sb,
495 int (*fn)(struct dquot *dquot, unsigned long priv),
496 unsigned long priv)
497{
498 struct dquot *dquot, *old_dquot = NULL;
499 int ret = 0;
500
501 mutex_lock(&sb_dqopt(sb)->dqonoff_mutex);
502 spin_lock(&dq_list_lock);
503 list_for_each_entry(dquot, &inuse_list, dq_inuse) {
504 if (!test_bit(DQ_ACTIVE_B, &dquot->dq_flags))
505 continue;
506 if (dquot->dq_sb != sb)
507 continue;
508 /* Now we have active dquot so we can just increase use count */
509 atomic_inc(&dquot->dq_count);
510 dqstats.lookups++;
511 spin_unlock(&dq_list_lock);
512 dqput(old_dquot);
513 old_dquot = dquot;
514 ret = fn(dquot, priv);
515 if (ret < 0)
516 goto out;
517 spin_lock(&dq_list_lock);
518 /* We are safe to continue now because our dquot could not
519 * be moved out of the inuse list while we hold the reference */
520 }
521 spin_unlock(&dq_list_lock);
522out:
523 dqput(old_dquot);
524 mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
525 return ret;
526}
Mingming Cao08d03502009-01-14 16:19:32 +0100527EXPORT_SYMBOL(dquot_scan_active);
Jan Kara12c77522008-10-20 17:05:00 +0200528
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529int vfs_quota_sync(struct super_block *sb, int type)
530{
531 struct list_head *dirty;
532 struct dquot *dquot;
533 struct quota_info *dqopt = sb_dqopt(sb);
534 int cnt;
535
Ingo Molnard3be9152006-03-23 03:00:29 -0800536 mutex_lock(&dqopt->dqonoff_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
538 if (type != -1 && cnt != type)
539 continue;
Jan Karaf55abc02008-08-20 17:50:32 +0200540 if (!sb_has_quota_active(sb, cnt))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 continue;
542 spin_lock(&dq_list_lock);
543 dirty = &dqopt->info[cnt].dqi_dirty_list;
544 while (!list_empty(dirty)) {
Pavel Emelianovb5e61812007-05-08 00:30:19 -0700545 dquot = list_first_entry(dirty, struct dquot, dq_dirty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 /* Dirty and inactive can be only bad dquot... */
547 if (!test_bit(DQ_ACTIVE_B, &dquot->dq_flags)) {
548 clear_dquot_dirty(dquot);
549 continue;
550 }
551 /* Now we have active dquot from which someone is
552 * holding reference so we can safely just increase
553 * use count */
554 atomic_inc(&dquot->dq_count);
555 dqstats.lookups++;
556 spin_unlock(&dq_list_lock);
557 sb->dq_op->write_dquot(dquot);
558 dqput(dquot);
559 spin_lock(&dq_list_lock);
560 }
561 spin_unlock(&dq_list_lock);
562 }
563
564 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
Jan Karaf55abc02008-08-20 17:50:32 +0200565 if ((cnt == type || type == -1) && sb_has_quota_active(sb, cnt)
566 && info_dirty(&dqopt->info[cnt]))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 sb->dq_op->write_info(sb, cnt);
568 spin_lock(&dq_list_lock);
569 dqstats.syncs++;
570 spin_unlock(&dq_list_lock);
Ingo Molnard3be9152006-03-23 03:00:29 -0800571 mutex_unlock(&dqopt->dqonoff_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572
573 return 0;
574}
Mingming Cao08d03502009-01-14 16:19:32 +0100575EXPORT_SYMBOL(vfs_quota_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576
577/* Free unused dquots from cache */
578static void prune_dqcache(int count)
579{
580 struct list_head *head;
581 struct dquot *dquot;
582
583 head = free_dquots.prev;
584 while (head != &free_dquots && count) {
585 dquot = list_entry(head, struct dquot, dq_free);
586 remove_dquot_hash(dquot);
587 remove_free_dquot(dquot);
588 remove_inuse(dquot);
Jan Kara74f783a2008-08-19 14:51:22 +0200589 do_destroy_dquot(dquot);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 count--;
591 head = free_dquots.prev;
592 }
593}
594
595/*
596 * This is called from kswapd when we think we need some
597 * more memory
598 */
599
Al Viro27496a82005-10-21 03:20:48 -0400600static int shrink_dqcache_memory(int nr, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601{
602 if (nr) {
603 spin_lock(&dq_list_lock);
604 prune_dqcache(nr);
605 spin_unlock(&dq_list_lock);
606 }
607 return (dqstats.free_dquots / 100) * sysctl_vfs_cache_pressure;
608}
609
Rusty Russell8e1f9362007-07-17 04:03:17 -0700610static struct shrinker dqcache_shrinker = {
611 .shrink = shrink_dqcache_memory,
612 .seeks = DEFAULT_SEEKS,
613};
614
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615/*
616 * Put reference to dquot
617 * NOTE: If you change this function please check whether dqput_blocks() works right...
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 */
Jan Kara3d9ea252008-10-10 16:12:23 +0200619void dqput(struct dquot *dquot)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620{
Jan Karab48d3802008-07-25 01:46:49 -0700621 int ret;
622
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 if (!dquot)
624 return;
625#ifdef __DQUOT_PARANOIA
626 if (!atomic_read(&dquot->dq_count)) {
627 printk("VFS: dqput: trying to free free dquot\n");
628 printk("VFS: device %s, dquot of %s %d\n",
629 dquot->dq_sb->s_id,
630 quotatypes[dquot->dq_type],
631 dquot->dq_id);
632 BUG();
633 }
634#endif
635
636 spin_lock(&dq_list_lock);
637 dqstats.drops++;
638 spin_unlock(&dq_list_lock);
639we_slept:
640 spin_lock(&dq_list_lock);
641 if (atomic_read(&dquot->dq_count) > 1) {
642 /* We have more than one user... nothing to do */
643 atomic_dec(&dquot->dq_count);
Jan Kara6362e4d2006-03-23 03:00:17 -0800644 /* Releasing dquot during quotaoff phase? */
Jan Karaf55abc02008-08-20 17:50:32 +0200645 if (!sb_has_quota_active(dquot->dq_sb, dquot->dq_type) &&
Jan Kara6362e4d2006-03-23 03:00:17 -0800646 atomic_read(&dquot->dq_count) == 1)
647 wake_up(&dquot->dq_wait_unused);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 spin_unlock(&dq_list_lock);
649 return;
650 }
651 /* Need to release dquot? */
652 if (test_bit(DQ_ACTIVE_B, &dquot->dq_flags) && dquot_dirty(dquot)) {
653 spin_unlock(&dq_list_lock);
654 /* Commit dquot before releasing */
Jan Karab48d3802008-07-25 01:46:49 -0700655 ret = dquot->dq_sb->dq_op->write_dquot(dquot);
656 if (ret < 0) {
657 printk(KERN_ERR "VFS: cannot write quota structure on "
658 "device %s (error %d). Quota may get out of "
659 "sync!\n", dquot->dq_sb->s_id, ret);
660 /*
661 * We clear dirty bit anyway, so that we avoid
662 * infinite loop here
663 */
664 spin_lock(&dq_list_lock);
665 clear_dquot_dirty(dquot);
666 spin_unlock(&dq_list_lock);
667 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 goto we_slept;
669 }
670 /* Clear flag in case dquot was inactive (something bad happened) */
671 clear_dquot_dirty(dquot);
672 if (test_bit(DQ_ACTIVE_B, &dquot->dq_flags)) {
673 spin_unlock(&dq_list_lock);
674 dquot->dq_sb->dq_op->release_dquot(dquot);
675 goto we_slept;
676 }
677 atomic_dec(&dquot->dq_count);
678#ifdef __DQUOT_PARANOIA
679 /* sanity check */
Eric Sesterhenn8abf6a42006-04-02 13:36:13 +0200680 BUG_ON(!list_empty(&dquot->dq_free));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681#endif
682 put_dquot_last(dquot);
683 spin_unlock(&dq_list_lock);
684}
Mingming Cao08d03502009-01-14 16:19:32 +0100685EXPORT_SYMBOL(dqput);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686
Jan Kara7d9056b2008-11-25 15:31:32 +0100687struct dquot *dquot_alloc(struct super_block *sb, int type)
Jan Kara74f783a2008-08-19 14:51:22 +0200688{
689 return kmem_cache_zalloc(dquot_cachep, GFP_NOFS);
690}
Jan Kara7d9056b2008-11-25 15:31:32 +0100691EXPORT_SYMBOL(dquot_alloc);
Jan Kara74f783a2008-08-19 14:51:22 +0200692
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693static struct dquot *get_empty_dquot(struct super_block *sb, int type)
694{
695 struct dquot *dquot;
696
Jan Kara74f783a2008-08-19 14:51:22 +0200697 dquot = sb->dq_op->alloc_dquot(sb, type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 if(!dquot)
699 return NODQUOT;
700
Ingo Molnard3be9152006-03-23 03:00:29 -0800701 mutex_init(&dquot->dq_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 INIT_LIST_HEAD(&dquot->dq_free);
703 INIT_LIST_HEAD(&dquot->dq_inuse);
704 INIT_HLIST_NODE(&dquot->dq_hash);
705 INIT_LIST_HEAD(&dquot->dq_dirty);
Jan Kara6362e4d2006-03-23 03:00:17 -0800706 init_waitqueue_head(&dquot->dq_wait_unused);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 dquot->dq_sb = sb;
708 dquot->dq_type = type;
709 atomic_set(&dquot->dq_count, 1);
710
711 return dquot;
712}
713
714/*
715 * Get reference to dquot
Jan Karacc334122009-01-12 17:23:05 +0100716 *
717 * Locking is slightly tricky here. We are guarded from parallel quotaoff()
718 * destroying our dquot by:
719 * a) checking for quota flags under dq_list_lock and
720 * b) getting a reference to dquot before we release dq_list_lock
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 */
Jan Kara3d9ea252008-10-10 16:12:23 +0200722struct dquot *dqget(struct super_block *sb, unsigned int id, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723{
724 unsigned int hashent = hashfn(sb, id, type);
Jan Karacc334122009-01-12 17:23:05 +0100725 struct dquot *dquot = NODQUOT, *empty = NODQUOT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726
Jan Karaf55abc02008-08-20 17:50:32 +0200727 if (!sb_has_quota_active(sb, type))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 return NODQUOT;
729we_slept:
730 spin_lock(&dq_list_lock);
Jan Karacc334122009-01-12 17:23:05 +0100731 spin_lock(&dq_state_lock);
732 if (!sb_has_quota_active(sb, type)) {
733 spin_unlock(&dq_state_lock);
734 spin_unlock(&dq_list_lock);
735 goto out;
736 }
737 spin_unlock(&dq_state_lock);
738
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 if ((dquot = find_dquot(hashent, sb, id, type)) == NODQUOT) {
740 if (empty == NODQUOT) {
741 spin_unlock(&dq_list_lock);
742 if ((empty = get_empty_dquot(sb, type)) == NODQUOT)
743 schedule(); /* Try to wait for a moment... */
744 goto we_slept;
745 }
746 dquot = empty;
Jan Karacc334122009-01-12 17:23:05 +0100747 empty = NODQUOT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 dquot->dq_id = id;
749 /* all dquots go on the inuse_list */
750 put_inuse(dquot);
751 /* hash it first so it can be found */
752 insert_dquot_hash(dquot);
753 dqstats.lookups++;
754 spin_unlock(&dq_list_lock);
755 } else {
756 if (!atomic_read(&dquot->dq_count))
757 remove_free_dquot(dquot);
758 atomic_inc(&dquot->dq_count);
759 dqstats.cache_hits++;
760 dqstats.lookups++;
761 spin_unlock(&dq_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 }
763 /* Wait for dq_lock - after this we know that either dquot_release() is already
764 * finished or it will be canceled due to dq_count > 1 test */
765 wait_on_dquot(dquot);
766 /* Read the dquot and instantiate it (everything done only if needed) */
767 if (!test_bit(DQ_ACTIVE_B, &dquot->dq_flags) && sb->dq_op->acquire_dquot(dquot) < 0) {
768 dqput(dquot);
Jan Karacc334122009-01-12 17:23:05 +0100769 dquot = NODQUOT;
770 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 }
772#ifdef __DQUOT_PARANOIA
Eric Sesterhenn8abf6a42006-04-02 13:36:13 +0200773 BUG_ON(!dquot->dq_sb); /* Has somebody invalidated entry under us? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774#endif
Jan Karacc334122009-01-12 17:23:05 +0100775out:
776 if (empty)
777 do_destroy_dquot(empty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778
779 return dquot;
780}
Mingming Cao08d03502009-01-14 16:19:32 +0100781EXPORT_SYMBOL(dqget);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782
783static int dqinit_needed(struct inode *inode, int type)
784{
785 int cnt;
786
787 if (IS_NOQUOTA(inode))
788 return 0;
789 if (type != -1)
790 return inode->i_dquot[type] == NODQUOT;
791 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
792 if (inode->i_dquot[cnt] == NODQUOT)
793 return 1;
794 return 0;
795}
796
Ingo Molnard3be9152006-03-23 03:00:29 -0800797/* This routine is guarded by dqonoff_mutex mutex */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798static void add_dquot_ref(struct super_block *sb, int type)
799{
Jan Kara941d2382008-02-06 01:37:36 -0800800 struct inode *inode, *old_inode = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801
Christoph Hellwigd003fb72007-02-12 00:51:58 -0800802 spin_lock(&inode_lock);
803 list_for_each_entry(inode, &sb->s_inodes, i_sb_list) {
804 if (!atomic_read(&inode->i_writecount))
805 continue;
806 if (!dqinit_needed(inode, type))
807 continue;
808 if (inode->i_state & (I_FREEING|I_WILL_FREE))
809 continue;
810
811 __iget(inode);
812 spin_unlock(&inode_lock);
813
Jan Kara941d2382008-02-06 01:37:36 -0800814 iput(old_inode);
Christoph Hellwigd003fb72007-02-12 00:51:58 -0800815 sb->dq_op->initialize(inode, type);
Jan Kara941d2382008-02-06 01:37:36 -0800816 /* We hold a reference to 'inode' so it couldn't have been
817 * removed from s_inodes list while we dropped the inode_lock.
818 * We cannot iput the inode now as we can be holding the last
819 * reference and we cannot iput it under inode_lock. So we
820 * keep the reference and iput it later. */
821 old_inode = inode;
822 spin_lock(&inode_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 }
Christoph Hellwigd003fb72007-02-12 00:51:58 -0800824 spin_unlock(&inode_lock);
Jan Kara941d2382008-02-06 01:37:36 -0800825 iput(old_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826}
827
828/* Return 0 if dqput() won't block (note that 1 doesn't necessarily mean blocking) */
829static inline int dqput_blocks(struct dquot *dquot)
830{
831 if (atomic_read(&dquot->dq_count) <= 1)
832 return 1;
833 return 0;
834}
835
836/* Remove references to dquots from inode - add dquot to list for freeing if needed */
837/* We can't race with anybody because we hold dqptr_sem for writing... */
Adrian Bunke5f00f42007-05-08 00:27:22 -0700838static int remove_inode_dquot_ref(struct inode *inode, int type,
839 struct list_head *tofree_head)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840{
841 struct dquot *dquot = inode->i_dquot[type];
842
843 inode->i_dquot[type] = NODQUOT;
844 if (dquot != NODQUOT) {
845 if (dqput_blocks(dquot)) {
846#ifdef __DQUOT_PARANOIA
847 if (atomic_read(&dquot->dq_count) != 1)
848 printk(KERN_WARNING "VFS: Adding dquot with dq_count %d to dispose list.\n", atomic_read(&dquot->dq_count));
849#endif
850 spin_lock(&dq_list_lock);
851 list_add(&dquot->dq_free, tofree_head); /* As dquot must have currently users it can't be on the free list... */
852 spin_unlock(&dq_list_lock);
853 return 1;
854 }
855 else
856 dqput(dquot); /* We have guaranteed we won't block */
857 }
858 return 0;
859}
860
861/* Free list of dquots - called from inode.c */
862/* dquots are removed from inodes, no new references can be got so we are the only ones holding reference */
863static void put_dquot_list(struct list_head *tofree_head)
864{
865 struct list_head *act_head;
866 struct dquot *dquot;
867
868 act_head = tofree_head->next;
869 /* So now we have dquots on the list... Just free them */
870 while (act_head != tofree_head) {
871 dquot = list_entry(act_head, struct dquot, dq_free);
872 act_head = act_head->next;
873 list_del_init(&dquot->dq_free); /* Remove dquot from the list so we won't have problems... */
874 dqput(dquot);
875 }
876}
877
Christoph Hellwigfb58b732007-02-12 00:51:57 -0800878static void remove_dquot_ref(struct super_block *sb, int type,
879 struct list_head *tofree_head)
880{
881 struct inode *inode;
882
883 spin_lock(&inode_lock);
884 list_for_each_entry(inode, &sb->s_inodes, i_sb_list) {
885 if (!IS_NOQUOTA(inode))
886 remove_inode_dquot_ref(inode, type, tofree_head);
887 }
888 spin_unlock(&inode_lock);
889}
890
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891/* Gather all references from inodes and drop them */
892static void drop_dquot_ref(struct super_block *sb, int type)
893{
894 LIST_HEAD(tofree_head);
895
Christoph Hellwigfb58b732007-02-12 00:51:57 -0800896 if (sb->dq_op) {
897 down_write(&sb_dqopt(sb)->dqptr_sem);
898 remove_dquot_ref(sb, type, &tofree_head);
899 up_write(&sb_dqopt(sb)->dqptr_sem);
900 put_dquot_list(&tofree_head);
901 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902}
903
Jan Kara12095462008-08-20 14:45:12 +0200904static inline void dquot_incr_inodes(struct dquot *dquot, qsize_t number)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905{
906 dquot->dq_dqb.dqb_curinodes += number;
907}
908
909static inline void dquot_incr_space(struct dquot *dquot, qsize_t number)
910{
911 dquot->dq_dqb.dqb_curspace += number;
912}
913
Mingming Caof18df222009-01-13 16:43:09 +0100914static inline void dquot_resv_space(struct dquot *dquot, qsize_t number)
915{
916 dquot->dq_dqb.dqb_rsvspace += number;
917}
918
Mingming Cao740d9dc2009-01-13 16:43:14 +0100919/*
920 * Claim reserved quota space
921 */
922static void dquot_claim_reserved_space(struct dquot *dquot,
923 qsize_t number)
924{
925 WARN_ON(dquot->dq_dqb.dqb_rsvspace < number);
926 dquot->dq_dqb.dqb_curspace += number;
927 dquot->dq_dqb.dqb_rsvspace -= number;
928}
929
930static inline
931void dquot_free_reserved_space(struct dquot *dquot, qsize_t number)
932{
933 dquot->dq_dqb.dqb_rsvspace -= number;
934}
935
Jan Kara12095462008-08-20 14:45:12 +0200936static inline void dquot_decr_inodes(struct dquot *dquot, qsize_t number)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937{
Jan Karadb49d2d2008-10-01 18:21:39 +0200938 if (sb_dqopt(dquot->dq_sb)->flags & DQUOT_NEGATIVE_USAGE ||
939 dquot->dq_dqb.dqb_curinodes >= number)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 dquot->dq_dqb.dqb_curinodes -= number;
941 else
942 dquot->dq_dqb.dqb_curinodes = 0;
943 if (dquot->dq_dqb.dqb_curinodes <= dquot->dq_dqb.dqb_isoftlimit)
944 dquot->dq_dqb.dqb_itime = (time_t) 0;
945 clear_bit(DQ_INODES_B, &dquot->dq_flags);
946}
947
948static inline void dquot_decr_space(struct dquot *dquot, qsize_t number)
949{
Jan Karadb49d2d2008-10-01 18:21:39 +0200950 if (sb_dqopt(dquot->dq_sb)->flags & DQUOT_NEGATIVE_USAGE ||
951 dquot->dq_dqb.dqb_curspace >= number)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 dquot->dq_dqb.dqb_curspace -= number;
953 else
954 dquot->dq_dqb.dqb_curspace = 0;
Jan Kara12095462008-08-20 14:45:12 +0200955 if (dquot->dq_dqb.dqb_curspace <= dquot->dq_dqb.dqb_bsoftlimit)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 dquot->dq_dqb.dqb_btime = (time_t) 0;
957 clear_bit(DQ_BLKS_B, &dquot->dq_flags);
958}
959
Jan Karac5254602007-12-22 14:03:25 -0800960static int warning_issued(struct dquot *dquot, const int warntype)
961{
962 int flag = (warntype == QUOTA_NL_BHARDWARN ||
963 warntype == QUOTA_NL_BSOFTLONGWARN) ? DQ_BLKS_B :
964 ((warntype == QUOTA_NL_IHARDWARN ||
965 warntype == QUOTA_NL_ISOFTLONGWARN) ? DQ_INODES_B : 0);
966
967 if (!flag)
968 return 0;
969 return test_and_set_bit(flag, &dquot->dq_flags);
970}
971
Jan Kara8e893462007-10-16 23:29:31 -0700972#ifdef CONFIG_PRINT_QUOTA_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973static int flag_print_warnings = 1;
974
975static inline int need_print_warning(struct dquot *dquot)
976{
977 if (!flag_print_warnings)
978 return 0;
979
980 switch (dquot->dq_type) {
981 case USRQUOTA:
David Howellsda9592e2008-11-14 10:39:05 +1100982 return current_fsuid() == dquot->dq_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 case GRPQUOTA:
984 return in_group_p(dquot->dq_id);
985 }
986 return 0;
987}
988
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989/* Print warning to user which exceeded quota */
Jan Karac5254602007-12-22 14:03:25 -0800990static void print_warning(struct dquot *dquot, const int warntype)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991{
992 char *msg = NULL;
Peter Zijlstra24ec8392006-12-08 02:36:04 -0800993 struct tty_struct *tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994
Jan Kara657d3bf2008-07-25 01:46:52 -0700995 if (warntype == QUOTA_NL_IHARDBELOW ||
996 warntype == QUOTA_NL_ISOFTBELOW ||
997 warntype == QUOTA_NL_BHARDBELOW ||
998 warntype == QUOTA_NL_BSOFTBELOW || !need_print_warning(dquot))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 return;
1000
Peter Zijlstra24ec8392006-12-08 02:36:04 -08001001 tty = get_current_tty();
1002 if (!tty)
Alan Cox452a00d2008-10-13 10:39:13 +01001003 return;
Peter Zijlstra24ec8392006-12-08 02:36:04 -08001004 tty_write_message(tty, dquot->dq_sb->s_id);
Jan Kara8e893462007-10-16 23:29:31 -07001005 if (warntype == QUOTA_NL_ISOFTWARN || warntype == QUOTA_NL_BSOFTWARN)
Peter Zijlstra24ec8392006-12-08 02:36:04 -08001006 tty_write_message(tty, ": warning, ");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 else
Peter Zijlstra24ec8392006-12-08 02:36:04 -08001008 tty_write_message(tty, ": write failed, ");
1009 tty_write_message(tty, quotatypes[dquot->dq_type]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 switch (warntype) {
Jan Kara8e893462007-10-16 23:29:31 -07001011 case QUOTA_NL_IHARDWARN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 msg = " file limit reached.\r\n";
1013 break;
Jan Kara8e893462007-10-16 23:29:31 -07001014 case QUOTA_NL_ISOFTLONGWARN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 msg = " file quota exceeded too long.\r\n";
1016 break;
Jan Kara8e893462007-10-16 23:29:31 -07001017 case QUOTA_NL_ISOFTWARN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 msg = " file quota exceeded.\r\n";
1019 break;
Jan Kara8e893462007-10-16 23:29:31 -07001020 case QUOTA_NL_BHARDWARN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 msg = " block limit reached.\r\n";
1022 break;
Jan Kara8e893462007-10-16 23:29:31 -07001023 case QUOTA_NL_BSOFTLONGWARN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 msg = " block quota exceeded too long.\r\n";
1025 break;
Jan Kara8e893462007-10-16 23:29:31 -07001026 case QUOTA_NL_BSOFTWARN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 msg = " block quota exceeded.\r\n";
1028 break;
1029 }
Peter Zijlstra24ec8392006-12-08 02:36:04 -08001030 tty_write_message(tty, msg);
Alan Cox452a00d2008-10-13 10:39:13 +01001031 tty_kref_put(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032}
Jan Kara8e893462007-10-16 23:29:31 -07001033#endif
1034
1035#ifdef CONFIG_QUOTA_NETLINK_INTERFACE
1036
Jan Kara8e893462007-10-16 23:29:31 -07001037/* Netlink family structure for quota */
1038static struct genl_family quota_genl_family = {
1039 .id = GENL_ID_GENERATE,
1040 .hdrsize = 0,
1041 .name = "VFS_DQUOT",
1042 .version = 1,
1043 .maxattr = QUOTA_NL_A_MAX,
1044};
1045
1046/* Send warning to userspace about user which exceeded quota */
1047static void send_warning(const struct dquot *dquot, const char warntype)
1048{
1049 static atomic_t seq;
1050 struct sk_buff *skb;
1051 void *msg_head;
1052 int ret;
Jan Kara22dd4832007-12-22 14:03:25 -08001053 int msg_size = 4 * nla_total_size(sizeof(u32)) +
1054 2 * nla_total_size(sizeof(u64));
Jan Kara8e893462007-10-16 23:29:31 -07001055
1056 /* We have to allocate using GFP_NOFS as we are called from a
1057 * filesystem performing write and thus further recursion into
1058 * the fs to free some data could cause deadlocks. */
Jan Kara22dd4832007-12-22 14:03:25 -08001059 skb = genlmsg_new(msg_size, GFP_NOFS);
Jan Kara8e893462007-10-16 23:29:31 -07001060 if (!skb) {
1061 printk(KERN_ERR
1062 "VFS: Not enough memory to send quota warning.\n");
1063 return;
1064 }
1065 msg_head = genlmsg_put(skb, 0, atomic_add_return(1, &seq),
1066 &quota_genl_family, 0, QUOTA_NL_C_WARNING);
1067 if (!msg_head) {
1068 printk(KERN_ERR
1069 "VFS: Cannot store netlink header in quota warning.\n");
1070 goto err_out;
1071 }
1072 ret = nla_put_u32(skb, QUOTA_NL_A_QTYPE, dquot->dq_type);
1073 if (ret)
1074 goto attr_err_out;
1075 ret = nla_put_u64(skb, QUOTA_NL_A_EXCESS_ID, dquot->dq_id);
1076 if (ret)
1077 goto attr_err_out;
1078 ret = nla_put_u32(skb, QUOTA_NL_A_WARNING, warntype);
1079 if (ret)
1080 goto attr_err_out;
1081 ret = nla_put_u32(skb, QUOTA_NL_A_DEV_MAJOR,
1082 MAJOR(dquot->dq_sb->s_dev));
1083 if (ret)
1084 goto attr_err_out;
1085 ret = nla_put_u32(skb, QUOTA_NL_A_DEV_MINOR,
1086 MINOR(dquot->dq_sb->s_dev));
1087 if (ret)
1088 goto attr_err_out;
David Howellsda9592e2008-11-14 10:39:05 +11001089 ret = nla_put_u64(skb, QUOTA_NL_A_CAUSED_ID, current_uid());
Jan Kara8e893462007-10-16 23:29:31 -07001090 if (ret)
1091 goto attr_err_out;
1092 genlmsg_end(skb, msg_head);
1093
1094 ret = genlmsg_multicast(skb, 0, quota_genl_family.id, GFP_NOFS);
1095 if (ret < 0 && ret != -ESRCH)
1096 printk(KERN_ERR
1097 "VFS: Failed to send notification message: %d\n", ret);
1098 return;
1099attr_err_out:
Jan Kara22dd4832007-12-22 14:03:25 -08001100 printk(KERN_ERR "VFS: Not enough space to compose quota message!\n");
Jan Kara8e893462007-10-16 23:29:31 -07001101err_out:
1102 kfree_skb(skb);
1103}
1104#endif
Mingming Caof18df222009-01-13 16:43:09 +01001105/*
1106 * Write warnings to the console and send warning messages over netlink.
1107 *
1108 * Note that this function can sleep.
1109 */
Jan Kara087ee8d2007-12-17 16:20:26 -08001110static inline void flush_warnings(struct dquot * const *dquots, char *warntype)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111{
1112 int i;
1113
1114 for (i = 0; i < MAXQUOTAS; i++)
Jan Karac5254602007-12-22 14:03:25 -08001115 if (dquots[i] != NODQUOT && warntype[i] != QUOTA_NL_NOWARN &&
1116 !warning_issued(dquots[i], warntype[i])) {
Jan Kara8e893462007-10-16 23:29:31 -07001117#ifdef CONFIG_PRINT_QUOTA_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 print_warning(dquots[i], warntype[i]);
Jan Kara8e893462007-10-16 23:29:31 -07001119#endif
1120#ifdef CONFIG_QUOTA_NETLINK_INTERFACE
1121 send_warning(dquots[i], warntype[i]);
1122#endif
1123 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124}
1125
1126static inline char ignore_hardlimit(struct dquot *dquot)
1127{
1128 struct mem_dqinfo *info = &sb_dqopt(dquot->dq_sb)->info[dquot->dq_type];
1129
1130 return capable(CAP_SYS_RESOURCE) &&
1131 (info->dqi_format->qf_fmt_id != QFMT_VFS_OLD || !(info->dqi_flags & V1_DQF_RSQUASH));
1132}
1133
1134/* needs dq_data_lock */
Jan Kara12095462008-08-20 14:45:12 +02001135static int check_idq(struct dquot *dquot, qsize_t inodes, char *warntype)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136{
Jan Kara8e893462007-10-16 23:29:31 -07001137 *warntype = QUOTA_NL_NOWARN;
Jan Karaf55abc02008-08-20 17:50:32 +02001138 if (!sb_has_quota_limits_enabled(dquot->dq_sb, dquot->dq_type) ||
1139 test_bit(DQ_FAKE_B, &dquot->dq_flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 return QUOTA_OK;
1141
1142 if (dquot->dq_dqb.dqb_ihardlimit &&
1143 (dquot->dq_dqb.dqb_curinodes + inodes) > dquot->dq_dqb.dqb_ihardlimit &&
1144 !ignore_hardlimit(dquot)) {
Jan Kara8e893462007-10-16 23:29:31 -07001145 *warntype = QUOTA_NL_IHARDWARN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 return NO_QUOTA;
1147 }
1148
1149 if (dquot->dq_dqb.dqb_isoftlimit &&
1150 (dquot->dq_dqb.dqb_curinodes + inodes) > dquot->dq_dqb.dqb_isoftlimit &&
1151 dquot->dq_dqb.dqb_itime && get_seconds() >= dquot->dq_dqb.dqb_itime &&
1152 !ignore_hardlimit(dquot)) {
Jan Kara8e893462007-10-16 23:29:31 -07001153 *warntype = QUOTA_NL_ISOFTLONGWARN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 return NO_QUOTA;
1155 }
1156
1157 if (dquot->dq_dqb.dqb_isoftlimit &&
1158 (dquot->dq_dqb.dqb_curinodes + inodes) > dquot->dq_dqb.dqb_isoftlimit &&
1159 dquot->dq_dqb.dqb_itime == 0) {
Jan Kara8e893462007-10-16 23:29:31 -07001160 *warntype = QUOTA_NL_ISOFTWARN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 dquot->dq_dqb.dqb_itime = get_seconds() + sb_dqopt(dquot->dq_sb)->info[dquot->dq_type].dqi_igrace;
1162 }
1163
1164 return QUOTA_OK;
1165}
1166
1167/* needs dq_data_lock */
1168static int check_bdq(struct dquot *dquot, qsize_t space, int prealloc, char *warntype)
1169{
Mingming Caof18df222009-01-13 16:43:09 +01001170 qsize_t tspace;
1171
Jan Kara8e893462007-10-16 23:29:31 -07001172 *warntype = QUOTA_NL_NOWARN;
Jan Karaf55abc02008-08-20 17:50:32 +02001173 if (!sb_has_quota_limits_enabled(dquot->dq_sb, dquot->dq_type) ||
1174 test_bit(DQ_FAKE_B, &dquot->dq_flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 return QUOTA_OK;
1176
Mingming Caof18df222009-01-13 16:43:09 +01001177 tspace = dquot->dq_dqb.dqb_curspace + dquot->dq_dqb.dqb_rsvspace
1178 + space;
1179
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 if (dquot->dq_dqb.dqb_bhardlimit &&
Mingming Caof18df222009-01-13 16:43:09 +01001181 tspace > dquot->dq_dqb.dqb_bhardlimit &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 !ignore_hardlimit(dquot)) {
1183 if (!prealloc)
Jan Kara8e893462007-10-16 23:29:31 -07001184 *warntype = QUOTA_NL_BHARDWARN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 return NO_QUOTA;
1186 }
1187
1188 if (dquot->dq_dqb.dqb_bsoftlimit &&
Mingming Caof18df222009-01-13 16:43:09 +01001189 tspace > dquot->dq_dqb.dqb_bsoftlimit &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 dquot->dq_dqb.dqb_btime && get_seconds() >= dquot->dq_dqb.dqb_btime &&
1191 !ignore_hardlimit(dquot)) {
1192 if (!prealloc)
Jan Kara8e893462007-10-16 23:29:31 -07001193 *warntype = QUOTA_NL_BSOFTLONGWARN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 return NO_QUOTA;
1195 }
1196
1197 if (dquot->dq_dqb.dqb_bsoftlimit &&
Mingming Caof18df222009-01-13 16:43:09 +01001198 tspace > dquot->dq_dqb.dqb_bsoftlimit &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 dquot->dq_dqb.dqb_btime == 0) {
1200 if (!prealloc) {
Jan Kara8e893462007-10-16 23:29:31 -07001201 *warntype = QUOTA_NL_BSOFTWARN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 dquot->dq_dqb.dqb_btime = get_seconds() + sb_dqopt(dquot->dq_sb)->info[dquot->dq_type].dqi_bgrace;
1203 }
1204 else
1205 /*
1206 * We don't allow preallocation to exceed softlimit so exceeding will
1207 * be always printed
1208 */
1209 return NO_QUOTA;
1210 }
1211
1212 return QUOTA_OK;
1213}
1214
Jan Kara12095462008-08-20 14:45:12 +02001215static int info_idq_free(struct dquot *dquot, qsize_t inodes)
Jan Kara657d3bf2008-07-25 01:46:52 -07001216{
1217 if (test_bit(DQ_FAKE_B, &dquot->dq_flags) ||
Jan Karaf55abc02008-08-20 17:50:32 +02001218 dquot->dq_dqb.dqb_curinodes <= dquot->dq_dqb.dqb_isoftlimit ||
1219 !sb_has_quota_limits_enabled(dquot->dq_sb, dquot->dq_type))
Jan Kara657d3bf2008-07-25 01:46:52 -07001220 return QUOTA_NL_NOWARN;
1221
1222 if (dquot->dq_dqb.dqb_curinodes - inodes <= dquot->dq_dqb.dqb_isoftlimit)
1223 return QUOTA_NL_ISOFTBELOW;
1224 if (dquot->dq_dqb.dqb_curinodes >= dquot->dq_dqb.dqb_ihardlimit &&
1225 dquot->dq_dqb.dqb_curinodes - inodes < dquot->dq_dqb.dqb_ihardlimit)
1226 return QUOTA_NL_IHARDBELOW;
1227 return QUOTA_NL_NOWARN;
1228}
1229
1230static int info_bdq_free(struct dquot *dquot, qsize_t space)
1231{
1232 if (test_bit(DQ_FAKE_B, &dquot->dq_flags) ||
Jan Kara12095462008-08-20 14:45:12 +02001233 dquot->dq_dqb.dqb_curspace <= dquot->dq_dqb.dqb_bsoftlimit)
Jan Kara657d3bf2008-07-25 01:46:52 -07001234 return QUOTA_NL_NOWARN;
1235
Jan Kara12095462008-08-20 14:45:12 +02001236 if (dquot->dq_dqb.dqb_curspace - space <= dquot->dq_dqb.dqb_bsoftlimit)
Jan Kara657d3bf2008-07-25 01:46:52 -07001237 return QUOTA_NL_BSOFTBELOW;
Jan Kara12095462008-08-20 14:45:12 +02001238 if (dquot->dq_dqb.dqb_curspace >= dquot->dq_dqb.dqb_bhardlimit &&
1239 dquot->dq_dqb.dqb_curspace - space < dquot->dq_dqb.dqb_bhardlimit)
Jan Kara657d3bf2008-07-25 01:46:52 -07001240 return QUOTA_NL_BHARDBELOW;
1241 return QUOTA_NL_NOWARN;
1242}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243/*
1244 * Initialize quota pointers in inode
Jan Karacc334122009-01-12 17:23:05 +01001245 * We do things in a bit complicated way but by that we avoid calling
1246 * dqget() and thus filesystem callbacks under dqptr_sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 */
1248int dquot_initialize(struct inode *inode, int type)
1249{
1250 unsigned int id = 0;
1251 int cnt, ret = 0;
Jan Karacc334122009-01-12 17:23:05 +01001252 struct dquot *got[MAXQUOTAS] = { NODQUOT, NODQUOT };
1253 struct super_block *sb = inode->i_sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254
Ingo Molnard3be9152006-03-23 03:00:29 -08001255 /* First test before acquiring mutex - solves deadlocks when we
1256 * re-enter the quota code and are already holding the mutex */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257 if (IS_NOQUOTA(inode))
1258 return 0;
Jan Karacc334122009-01-12 17:23:05 +01001259
1260 /* First get references to structures we might need. */
1261 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1262 if (type != -1 && cnt != type)
1263 continue;
1264 switch (cnt) {
1265 case USRQUOTA:
1266 id = inode->i_uid;
1267 break;
1268 case GRPQUOTA:
1269 id = inode->i_gid;
1270 break;
1271 }
1272 got[cnt] = dqget(sb, id, cnt);
1273 }
1274
1275 down_write(&sb_dqopt(sb)->dqptr_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 /* Having dqptr_sem we know NOQUOTA flags can't be altered... */
1277 if (IS_NOQUOTA(inode))
1278 goto out_err;
1279 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1280 if (type != -1 && cnt != type)
1281 continue;
Jan Karacc334122009-01-12 17:23:05 +01001282 /* Avoid races with quotaoff() */
1283 if (!sb_has_quota_active(sb, cnt))
1284 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 if (inode->i_dquot[cnt] == NODQUOT) {
Jan Karacc334122009-01-12 17:23:05 +01001286 inode->i_dquot[cnt] = got[cnt];
1287 got[cnt] = NODQUOT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 }
1289 }
1290out_err:
Jan Karacc334122009-01-12 17:23:05 +01001291 up_write(&sb_dqopt(sb)->dqptr_sem);
1292 /* Drop unused references */
1293 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1294 dqput(got[cnt]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 return ret;
1296}
Mingming Cao08d03502009-01-14 16:19:32 +01001297EXPORT_SYMBOL(dquot_initialize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298
1299/*
1300 * Release all quotas referenced by inode
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 */
Jan Kara3d9ea252008-10-10 16:12:23 +02001302int dquot_drop(struct inode *inode)
1303{
Jan Karacc334122009-01-12 17:23:05 +01001304 int cnt;
1305 struct dquot *put[MAXQUOTAS];
1306
Jan Kara3d9ea252008-10-10 16:12:23 +02001307 down_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
Jan Karacc334122009-01-12 17:23:05 +01001308 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1309 put[cnt] = inode->i_dquot[cnt];
1310 inode->i_dquot[cnt] = NODQUOT;
1311 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 up_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
Jan Karacc334122009-01-12 17:23:05 +01001313
1314 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1315 dqput(put[cnt]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 return 0;
1317}
Mingming Cao08d03502009-01-14 16:19:32 +01001318EXPORT_SYMBOL(dquot_drop);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319
Jan Karab85f4b82008-07-25 01:46:50 -07001320/* Wrapper to remove references to quota structures from inode */
1321void vfs_dq_drop(struct inode *inode)
1322{
1323 /* Here we can get arbitrary inode from clear_inode() so we have
1324 * to be careful. OTOH we don't need locking as quota operations
1325 * are allowed to change only at mount time */
1326 if (!IS_NOQUOTA(inode) && inode->i_sb && inode->i_sb->dq_op
1327 && inode->i_sb->dq_op->drop) {
1328 int cnt;
1329 /* Test before calling to rule out calls from proc and such
1330 * where we are not allowed to block. Note that this is
1331 * actually reliable test even without the lock - the caller
1332 * must assure that nobody can come after the DQUOT_DROP and
1333 * add quota pointers back anyway */
1334 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1335 if (inode->i_dquot[cnt] != NODQUOT)
1336 break;
1337 if (cnt < MAXQUOTAS)
1338 inode->i_sb->dq_op->drop(inode);
1339 }
1340}
Mingming Cao08d03502009-01-14 16:19:32 +01001341EXPORT_SYMBOL(vfs_dq_drop);
Jan Karab85f4b82008-07-25 01:46:50 -07001342
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343/*
1344 * Following four functions update i_blocks+i_bytes fields and
1345 * quota information (together with appropriate checks)
1346 * NOTE: We absolutely rely on the fact that caller dirties
1347 * the inode (usually macros in quotaops.h care about this) and
1348 * holds a handle for the current transaction so that dquot write and
1349 * inode write go into the same transaction.
1350 */
1351
1352/*
1353 * This operation can block, but only after everything is updated
1354 */
Mingming Caof18df222009-01-13 16:43:09 +01001355int __dquot_alloc_space(struct inode *inode, qsize_t number,
1356 int warn, int reserve)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357{
Mingming Caof18df222009-01-13 16:43:09 +01001358 int cnt, ret = QUOTA_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 char warntype[MAXQUOTAS];
1360
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
Jan Kara8e893462007-10-16 23:29:31 -07001362 warntype[cnt] = QUOTA_NL_NOWARN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 spin_lock(&dq_data_lock);
1365 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1366 if (inode->i_dquot[cnt] == NODQUOT)
1367 continue;
Mingming Caof18df222009-01-13 16:43:09 +01001368 if (check_bdq(inode->i_dquot[cnt], number, warn, warntype+cnt)
1369 == NO_QUOTA) {
1370 ret = NO_QUOTA;
1371 goto out_unlock;
1372 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373 }
1374 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1375 if (inode->i_dquot[cnt] == NODQUOT)
1376 continue;
Mingming Caof18df222009-01-13 16:43:09 +01001377 if (reserve)
1378 dquot_resv_space(inode->i_dquot[cnt], number);
1379 else
1380 dquot_incr_space(inode->i_dquot[cnt], number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 }
Mingming Caof18df222009-01-13 16:43:09 +01001382 if (!reserve)
1383 inode_add_bytes(inode, number);
1384out_unlock:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 spin_unlock(&dq_data_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 flush_warnings(inode->i_dquot, warntype);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 return ret;
1388}
1389
Mingming Caof18df222009-01-13 16:43:09 +01001390int dquot_alloc_space(struct inode *inode, qsize_t number, int warn)
1391{
1392 int cnt, ret = QUOTA_OK;
1393
1394 /*
1395 * First test before acquiring mutex - solves deadlocks when we
1396 * re-enter the quota code and are already holding the mutex
1397 */
1398 if (IS_NOQUOTA(inode)) {
1399 inode_add_bytes(inode, number);
1400 goto out;
1401 }
1402
1403 down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1404 if (IS_NOQUOTA(inode)) {
1405 inode_add_bytes(inode, number);
1406 goto out_unlock;
1407 }
1408
1409 ret = __dquot_alloc_space(inode, number, warn, 0);
1410 if (ret == NO_QUOTA)
1411 goto out_unlock;
1412
1413 /* Dirtify all the dquots - this can block when journalling */
1414 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1415 if (inode->i_dquot[cnt])
1416 mark_dquot_dirty(inode->i_dquot[cnt]);
1417out_unlock:
1418 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1419out:
1420 return ret;
1421}
Mingming Cao08d03502009-01-14 16:19:32 +01001422EXPORT_SYMBOL(dquot_alloc_space);
Mingming Caof18df222009-01-13 16:43:09 +01001423
1424int dquot_reserve_space(struct inode *inode, qsize_t number, int warn)
1425{
1426 int ret = QUOTA_OK;
1427
1428 if (IS_NOQUOTA(inode))
1429 goto out;
1430
1431 down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1432 if (IS_NOQUOTA(inode))
1433 goto out_unlock;
1434
1435 ret = __dquot_alloc_space(inode, number, warn, 1);
1436out_unlock:
1437 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1438out:
1439 return ret;
1440}
1441EXPORT_SYMBOL(dquot_reserve_space);
1442
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443/*
1444 * This operation can block, but only after everything is updated
1445 */
Jan Kara12095462008-08-20 14:45:12 +02001446int dquot_alloc_inode(const struct inode *inode, qsize_t number)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447{
1448 int cnt, ret = NO_QUOTA;
1449 char warntype[MAXQUOTAS];
1450
Ingo Molnard3be9152006-03-23 03:00:29 -08001451 /* First test before acquiring mutex - solves deadlocks when we
1452 * re-enter the quota code and are already holding the mutex */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453 if (IS_NOQUOTA(inode))
1454 return QUOTA_OK;
1455 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
Jan Kara8e893462007-10-16 23:29:31 -07001456 warntype[cnt] = QUOTA_NL_NOWARN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1458 if (IS_NOQUOTA(inode)) {
1459 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1460 return QUOTA_OK;
1461 }
1462 spin_lock(&dq_data_lock);
1463 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1464 if (inode->i_dquot[cnt] == NODQUOT)
1465 continue;
1466 if (check_idq(inode->i_dquot[cnt], number, warntype+cnt) == NO_QUOTA)
1467 goto warn_put_all;
1468 }
1469
1470 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1471 if (inode->i_dquot[cnt] == NODQUOT)
1472 continue;
1473 dquot_incr_inodes(inode->i_dquot[cnt], number);
1474 }
1475 ret = QUOTA_OK;
1476warn_put_all:
1477 spin_unlock(&dq_data_lock);
1478 if (ret == QUOTA_OK)
1479 /* Dirtify all the dquots - this can block when journalling */
1480 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1481 if (inode->i_dquot[cnt])
1482 mark_dquot_dirty(inode->i_dquot[cnt]);
Jan Kara087ee8d2007-12-17 16:20:26 -08001483 flush_warnings(inode->i_dquot, warntype);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1485 return ret;
1486}
Mingming Cao08d03502009-01-14 16:19:32 +01001487EXPORT_SYMBOL(dquot_alloc_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488
Mingming Cao740d9dc2009-01-13 16:43:14 +01001489int dquot_claim_space(struct inode *inode, qsize_t number)
1490{
1491 int cnt;
1492 int ret = QUOTA_OK;
1493
1494 if (IS_NOQUOTA(inode)) {
1495 inode_add_bytes(inode, number);
1496 goto out;
1497 }
1498
1499 down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1500 if (IS_NOQUOTA(inode)) {
1501 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1502 inode_add_bytes(inode, number);
1503 goto out;
1504 }
1505
1506 spin_lock(&dq_data_lock);
1507 /* Claim reserved quotas to allocated quotas */
1508 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1509 if (inode->i_dquot[cnt] != NODQUOT)
1510 dquot_claim_reserved_space(inode->i_dquot[cnt],
1511 number);
1512 }
1513 /* Update inode bytes */
1514 inode_add_bytes(inode, number);
1515 spin_unlock(&dq_data_lock);
1516 /* Dirtify all the dquots - this can block when journalling */
1517 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1518 if (inode->i_dquot[cnt])
1519 mark_dquot_dirty(inode->i_dquot[cnt]);
1520 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1521out:
1522 return ret;
1523}
1524EXPORT_SYMBOL(dquot_claim_space);
1525
1526/*
1527 * Release reserved quota space
1528 */
1529void dquot_release_reserved_space(struct inode *inode, qsize_t number)
1530{
1531 int cnt;
1532
1533 if (IS_NOQUOTA(inode))
1534 goto out;
1535
1536 down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1537 if (IS_NOQUOTA(inode))
1538 goto out_unlock;
1539
1540 spin_lock(&dq_data_lock);
1541 /* Release reserved dquots */
1542 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1543 if (inode->i_dquot[cnt] != NODQUOT)
1544 dquot_free_reserved_space(inode->i_dquot[cnt], number);
1545 }
1546 spin_unlock(&dq_data_lock);
1547
1548out_unlock:
1549 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1550out:
1551 return;
1552}
1553EXPORT_SYMBOL(dquot_release_reserved_space);
1554
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555/*
1556 * This operation can block, but only after everything is updated
1557 */
1558int dquot_free_space(struct inode *inode, qsize_t number)
1559{
1560 unsigned int cnt;
Jan Kara657d3bf2008-07-25 01:46:52 -07001561 char warntype[MAXQUOTAS];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562
Ingo Molnard3be9152006-03-23 03:00:29 -08001563 /* First test before acquiring mutex - solves deadlocks when we
1564 * re-enter the quota code and are already holding the mutex */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565 if (IS_NOQUOTA(inode)) {
1566out_sub:
1567 inode_sub_bytes(inode, number);
1568 return QUOTA_OK;
1569 }
Jan Kara657d3bf2008-07-25 01:46:52 -07001570
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571 down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1572 /* Now recheck reliably when holding dqptr_sem */
1573 if (IS_NOQUOTA(inode)) {
1574 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1575 goto out_sub;
1576 }
1577 spin_lock(&dq_data_lock);
1578 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1579 if (inode->i_dquot[cnt] == NODQUOT)
1580 continue;
Jan Kara657d3bf2008-07-25 01:46:52 -07001581 warntype[cnt] = info_bdq_free(inode->i_dquot[cnt], number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582 dquot_decr_space(inode->i_dquot[cnt], number);
1583 }
1584 inode_sub_bytes(inode, number);
1585 spin_unlock(&dq_data_lock);
1586 /* Dirtify all the dquots - this can block when journalling */
1587 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1588 if (inode->i_dquot[cnt])
1589 mark_dquot_dirty(inode->i_dquot[cnt]);
Jan Kara657d3bf2008-07-25 01:46:52 -07001590 flush_warnings(inode->i_dquot, warntype);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1592 return QUOTA_OK;
1593}
Mingming Cao08d03502009-01-14 16:19:32 +01001594EXPORT_SYMBOL(dquot_free_space);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595
1596/*
1597 * This operation can block, but only after everything is updated
1598 */
Jan Kara12095462008-08-20 14:45:12 +02001599int dquot_free_inode(const struct inode *inode, qsize_t number)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600{
1601 unsigned int cnt;
Jan Kara657d3bf2008-07-25 01:46:52 -07001602 char warntype[MAXQUOTAS];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603
Ingo Molnard3be9152006-03-23 03:00:29 -08001604 /* First test before acquiring mutex - solves deadlocks when we
1605 * re-enter the quota code and are already holding the mutex */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606 if (IS_NOQUOTA(inode))
1607 return QUOTA_OK;
Jan Kara657d3bf2008-07-25 01:46:52 -07001608
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609 down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1610 /* Now recheck reliably when holding dqptr_sem */
1611 if (IS_NOQUOTA(inode)) {
1612 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1613 return QUOTA_OK;
1614 }
1615 spin_lock(&dq_data_lock);
1616 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1617 if (inode->i_dquot[cnt] == NODQUOT)
1618 continue;
Jan Kara657d3bf2008-07-25 01:46:52 -07001619 warntype[cnt] = info_idq_free(inode->i_dquot[cnt], number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620 dquot_decr_inodes(inode->i_dquot[cnt], number);
1621 }
1622 spin_unlock(&dq_data_lock);
1623 /* Dirtify all the dquots - this can block when journalling */
1624 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1625 if (inode->i_dquot[cnt])
1626 mark_dquot_dirty(inode->i_dquot[cnt]);
Jan Kara657d3bf2008-07-25 01:46:52 -07001627 flush_warnings(inode->i_dquot, warntype);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1629 return QUOTA_OK;
1630}
Mingming Cao08d03502009-01-14 16:19:32 +01001631EXPORT_SYMBOL(dquot_free_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632
1633/*
Mingming Cao740d9dc2009-01-13 16:43:14 +01001634 * call back function, get reserved quota space from underlying fs
1635 */
1636qsize_t dquot_get_reserved_space(struct inode *inode)
1637{
1638 qsize_t reserved_space = 0;
1639
1640 if (sb_any_quota_active(inode->i_sb) &&
1641 inode->i_sb->dq_op->get_reserved_space)
1642 reserved_space = inode->i_sb->dq_op->get_reserved_space(inode);
1643 return reserved_space;
1644}
1645
1646/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647 * Transfer the number of inode and blocks from one diskquota to an other.
1648 *
1649 * This operation can block, but only after everything is updated
1650 * A transaction must be started when entering this function.
1651 */
1652int dquot_transfer(struct inode *inode, struct iattr *iattr)
1653{
Mingming Cao740d9dc2009-01-13 16:43:14 +01001654 qsize_t space, cur_space;
1655 qsize_t rsv_space = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656 struct dquot *transfer_from[MAXQUOTAS];
1657 struct dquot *transfer_to[MAXQUOTAS];
Jan Karacc334122009-01-12 17:23:05 +01001658 int cnt, ret = QUOTA_OK;
1659 int chuid = iattr->ia_valid & ATTR_UID && inode->i_uid != iattr->ia_uid,
1660 chgid = iattr->ia_valid & ATTR_GID && inode->i_gid != iattr->ia_gid;
Jan Kara657d3bf2008-07-25 01:46:52 -07001661 char warntype_to[MAXQUOTAS];
1662 char warntype_from_inodes[MAXQUOTAS], warntype_from_space[MAXQUOTAS];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663
Ingo Molnard3be9152006-03-23 03:00:29 -08001664 /* First test before acquiring mutex - solves deadlocks when we
1665 * re-enter the quota code and are already holding the mutex */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666 if (IS_NOQUOTA(inode))
1667 return QUOTA_OK;
Jan Karacc334122009-01-12 17:23:05 +01001668 /* Initialize the arrays */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
Jan Karacc334122009-01-12 17:23:05 +01001670 transfer_from[cnt] = NODQUOT;
1671 transfer_to[cnt] = NODQUOT;
Jan Kara657d3bf2008-07-25 01:46:52 -07001672 warntype_to[cnt] = QUOTA_NL_NOWARN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673 switch (cnt) {
1674 case USRQUOTA:
1675 if (!chuid)
1676 continue;
1677 transfer_to[cnt] = dqget(inode->i_sb, iattr->ia_uid, cnt);
1678 break;
1679 case GRPQUOTA:
1680 if (!chgid)
1681 continue;
1682 transfer_to[cnt] = dqget(inode->i_sb, iattr->ia_gid, cnt);
1683 break;
1684 }
1685 }
Jan Karacc334122009-01-12 17:23:05 +01001686
1687 down_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
1688 /* Now recheck reliably when holding dqptr_sem */
1689 if (IS_NOQUOTA(inode)) { /* File without quota accounting? */
1690 up_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
1691 goto put_all;
1692 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693 spin_lock(&dq_data_lock);
Mingming Cao740d9dc2009-01-13 16:43:14 +01001694 cur_space = inode_get_bytes(inode);
1695 rsv_space = dquot_get_reserved_space(inode);
1696 space = cur_space + rsv_space;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697 /* Build the transfer_from list and check the limits */
1698 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1699 if (transfer_to[cnt] == NODQUOT)
1700 continue;
1701 transfer_from[cnt] = inode->i_dquot[cnt];
Jan Kara657d3bf2008-07-25 01:46:52 -07001702 if (check_idq(transfer_to[cnt], 1, warntype_to + cnt) ==
1703 NO_QUOTA || check_bdq(transfer_to[cnt], space, 0,
1704 warntype_to + cnt) == NO_QUOTA)
Jan Karacc334122009-01-12 17:23:05 +01001705 goto over_quota;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706 }
1707
1708 /*
1709 * Finally perform the needed transfer from transfer_from to transfer_to
1710 */
1711 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1712 /*
1713 * Skip changes for same uid or gid or for turned off quota-type.
1714 */
1715 if (transfer_to[cnt] == NODQUOT)
1716 continue;
1717
1718 /* Due to IO error we might not have transfer_from[] structure */
1719 if (transfer_from[cnt]) {
Jan Kara657d3bf2008-07-25 01:46:52 -07001720 warntype_from_inodes[cnt] =
1721 info_idq_free(transfer_from[cnt], 1);
1722 warntype_from_space[cnt] =
1723 info_bdq_free(transfer_from[cnt], space);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724 dquot_decr_inodes(transfer_from[cnt], 1);
Mingming Cao740d9dc2009-01-13 16:43:14 +01001725 dquot_decr_space(transfer_from[cnt], cur_space);
1726 dquot_free_reserved_space(transfer_from[cnt],
1727 rsv_space);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728 }
1729
1730 dquot_incr_inodes(transfer_to[cnt], 1);
Mingming Cao740d9dc2009-01-13 16:43:14 +01001731 dquot_incr_space(transfer_to[cnt], cur_space);
1732 dquot_resv_space(transfer_to[cnt], rsv_space);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733
1734 inode->i_dquot[cnt] = transfer_to[cnt];
1735 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736 spin_unlock(&dq_data_lock);
Jan Karacc334122009-01-12 17:23:05 +01001737 up_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
1738
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739 /* Dirtify all the dquots - this can block when journalling */
1740 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1741 if (transfer_from[cnt])
1742 mark_dquot_dirty(transfer_from[cnt]);
Jan Karacc334122009-01-12 17:23:05 +01001743 if (transfer_to[cnt]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744 mark_dquot_dirty(transfer_to[cnt]);
Jan Karacc334122009-01-12 17:23:05 +01001745 /* The reference we got is transferred to the inode */
1746 transfer_to[cnt] = NODQUOT;
1747 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748 }
Jan Karacc334122009-01-12 17:23:05 +01001749warn_put_all:
Jan Kara657d3bf2008-07-25 01:46:52 -07001750 flush_warnings(transfer_to, warntype_to);
1751 flush_warnings(transfer_from, warntype_from_inodes);
1752 flush_warnings(transfer_from, warntype_from_space);
Jan Karacc334122009-01-12 17:23:05 +01001753put_all:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
Jan Karacc334122009-01-12 17:23:05 +01001755 dqput(transfer_from[cnt]);
1756 dqput(transfer_to[cnt]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758 return ret;
Jan Karacc334122009-01-12 17:23:05 +01001759over_quota:
1760 spin_unlock(&dq_data_lock);
1761 up_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
1762 /* Clear dquot pointers we don't want to dqput() */
1763 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1764 transfer_from[cnt] = NODQUOT;
1765 ret = NO_QUOTA;
1766 goto warn_put_all;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767}
Mingming Cao08d03502009-01-14 16:19:32 +01001768EXPORT_SYMBOL(dquot_transfer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769
Jan Karab85f4b82008-07-25 01:46:50 -07001770/* Wrapper for transferring ownership of an inode */
1771int vfs_dq_transfer(struct inode *inode, struct iattr *iattr)
1772{
Jan Karaf55abc02008-08-20 17:50:32 +02001773 if (sb_any_quota_active(inode->i_sb) && !IS_NOQUOTA(inode)) {
Jan Karab85f4b82008-07-25 01:46:50 -07001774 vfs_dq_init(inode);
1775 if (inode->i_sb->dq_op->transfer(inode, iattr) == NO_QUOTA)
1776 return 1;
1777 }
1778 return 0;
1779}
Mingming Cao08d03502009-01-14 16:19:32 +01001780EXPORT_SYMBOL(vfs_dq_transfer);
Jan Karab85f4b82008-07-25 01:46:50 -07001781
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782/*
1783 * Write info of quota file to disk
1784 */
1785int dquot_commit_info(struct super_block *sb, int type)
1786{
1787 int ret;
1788 struct quota_info *dqopt = sb_dqopt(sb);
1789
Ingo Molnard3be9152006-03-23 03:00:29 -08001790 mutex_lock(&dqopt->dqio_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791 ret = dqopt->ops[type]->write_file_info(sb, type);
Ingo Molnard3be9152006-03-23 03:00:29 -08001792 mutex_unlock(&dqopt->dqio_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793 return ret;
1794}
Mingming Cao08d03502009-01-14 16:19:32 +01001795EXPORT_SYMBOL(dquot_commit_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796
1797/*
1798 * Definitions of diskquota operations.
1799 */
1800struct dquot_operations dquot_operations = {
1801 .initialize = dquot_initialize,
1802 .drop = dquot_drop,
1803 .alloc_space = dquot_alloc_space,
1804 .alloc_inode = dquot_alloc_inode,
1805 .free_space = dquot_free_space,
1806 .free_inode = dquot_free_inode,
1807 .transfer = dquot_transfer,
1808 .write_dquot = dquot_commit,
1809 .acquire_dquot = dquot_acquire,
1810 .release_dquot = dquot_release,
1811 .mark_dirty = dquot_mark_dquot_dirty,
Jan Kara74f783a2008-08-19 14:51:22 +02001812 .write_info = dquot_commit_info,
1813 .alloc_dquot = dquot_alloc,
1814 .destroy_dquot = dquot_destroy,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815};
1816
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817/*
1818 * Turn quota off on a device. type == -1 ==> quotaoff for all types (umount)
1819 */
Jan Karaf55abc02008-08-20 17:50:32 +02001820int vfs_quota_disable(struct super_block *sb, int type, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821{
Jan Kara0ff5af82008-04-28 02:14:33 -07001822 int cnt, ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823 struct quota_info *dqopt = sb_dqopt(sb);
1824 struct inode *toputinode[MAXQUOTAS];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825
Jan Karaf55abc02008-08-20 17:50:32 +02001826 /* Cannot turn off usage accounting without turning off limits, or
1827 * suspend quotas and simultaneously turn quotas off. */
1828 if ((flags & DQUOT_USAGE_ENABLED && !(flags & DQUOT_LIMITS_ENABLED))
1829 || (flags & DQUOT_SUSPENDED && flags & (DQUOT_LIMITS_ENABLED |
1830 DQUOT_USAGE_ENABLED)))
1831 return -EINVAL;
1832
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833 /* We need to serialize quota_off() for device */
Ingo Molnard3be9152006-03-23 03:00:29 -08001834 mutex_lock(&dqopt->dqonoff_mutex);
Jan Kara9377abd2008-05-12 14:02:08 -07001835
1836 /*
1837 * Skip everything if there's nothing to do. We have to do this because
1838 * sometimes we are called when fill_super() failed and calling
1839 * sync_fs() in such cases does no good.
1840 */
Jan Karaf55abc02008-08-20 17:50:32 +02001841 if (!sb_any_quota_loaded(sb)) {
Jan Kara9377abd2008-05-12 14:02:08 -07001842 mutex_unlock(&dqopt->dqonoff_mutex);
1843 return 0;
1844 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1846 toputinode[cnt] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847 if (type != -1 && cnt != type)
1848 continue;
Jan Karaf55abc02008-08-20 17:50:32 +02001849 if (!sb_has_quota_loaded(sb, cnt))
Jan Kara0ff5af82008-04-28 02:14:33 -07001850 continue;
Jan Karaf55abc02008-08-20 17:50:32 +02001851
1852 if (flags & DQUOT_SUSPENDED) {
Jan Karacc334122009-01-12 17:23:05 +01001853 spin_lock(&dq_state_lock);
Jan Karaf55abc02008-08-20 17:50:32 +02001854 dqopt->flags |=
1855 dquot_state_flag(DQUOT_SUSPENDED, cnt);
Jan Karacc334122009-01-12 17:23:05 +01001856 spin_unlock(&dq_state_lock);
Jan Karaf55abc02008-08-20 17:50:32 +02001857 } else {
Jan Karacc334122009-01-12 17:23:05 +01001858 spin_lock(&dq_state_lock);
Jan Karaf55abc02008-08-20 17:50:32 +02001859 dqopt->flags &= ~dquot_state_flag(flags, cnt);
1860 /* Turning off suspended quotas? */
1861 if (!sb_has_quota_loaded(sb, cnt) &&
1862 sb_has_quota_suspended(sb, cnt)) {
1863 dqopt->flags &= ~dquot_state_flag(
1864 DQUOT_SUSPENDED, cnt);
Jan Karacc334122009-01-12 17:23:05 +01001865 spin_unlock(&dq_state_lock);
Jan Karaf55abc02008-08-20 17:50:32 +02001866 iput(dqopt->files[cnt]);
1867 dqopt->files[cnt] = NULL;
1868 continue;
1869 }
Jan Karacc334122009-01-12 17:23:05 +01001870 spin_unlock(&dq_state_lock);
Jan Kara0ff5af82008-04-28 02:14:33 -07001871 }
Jan Karaf55abc02008-08-20 17:50:32 +02001872
1873 /* We still have to keep quota loaded? */
1874 if (sb_has_quota_loaded(sb, cnt) && !(flags & DQUOT_SUSPENDED))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876
1877 /* Note: these are blocking operations */
1878 drop_dquot_ref(sb, cnt);
1879 invalidate_dquots(sb, cnt);
1880 /*
1881 * Now all dquots should be invalidated, all writes done so we should be only
1882 * users of the info. No locks needed.
1883 */
1884 if (info_dirty(&dqopt->info[cnt]))
1885 sb->dq_op->write_info(sb, cnt);
1886 if (dqopt->ops[cnt]->free_file_info)
1887 dqopt->ops[cnt]->free_file_info(sb, cnt);
1888 put_quota_format(dqopt->info[cnt].dqi_format);
1889
1890 toputinode[cnt] = dqopt->files[cnt];
Jan Karaf55abc02008-08-20 17:50:32 +02001891 if (!sb_has_quota_loaded(sb, cnt))
Jan Kara0ff5af82008-04-28 02:14:33 -07001892 dqopt->files[cnt] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893 dqopt->info[cnt].dqi_flags = 0;
1894 dqopt->info[cnt].dqi_igrace = 0;
1895 dqopt->info[cnt].dqi_bgrace = 0;
1896 dqopt->ops[cnt] = NULL;
1897 }
Ingo Molnard3be9152006-03-23 03:00:29 -08001898 mutex_unlock(&dqopt->dqonoff_mutex);
Jan Karaca785ec2008-09-30 17:53:37 +02001899
1900 /* Skip syncing and setting flags if quota files are hidden */
1901 if (dqopt->flags & DQUOT_QUOTA_SYS_FILE)
1902 goto put_inodes;
1903
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904 /* Sync the superblock so that buffers with quota data are written to
Al Viro7b7b1ac2005-11-07 17:13:39 -05001905 * disk (and so userspace sees correct data afterwards). */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906 if (sb->s_op->sync_fs)
1907 sb->s_op->sync_fs(sb, 1);
1908 sync_blockdev(sb->s_bdev);
1909 /* Now the quota files are just ordinary files and we can set the
1910 * inode flags back. Moreover we discard the pagecache so that
1911 * userspace sees the writes we did bypassing the pagecache. We
1912 * must also discard the blockdev buffers so that we see the
1913 * changes done by userspace on the next quotaon() */
1914 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1915 if (toputinode[cnt]) {
Ingo Molnard3be9152006-03-23 03:00:29 -08001916 mutex_lock(&dqopt->dqonoff_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917 /* If quota was reenabled in the meantime, we have
1918 * nothing to do */
Jan Karaf55abc02008-08-20 17:50:32 +02001919 if (!sb_has_quota_loaded(sb, cnt)) {
Jan Kara79254092007-05-16 22:11:19 -07001920 mutex_lock_nested(&toputinode[cnt]->i_mutex, I_MUTEX_QUOTA);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921 toputinode[cnt]->i_flags &= ~(S_IMMUTABLE |
1922 S_NOATIME | S_NOQUOTA);
1923 truncate_inode_pages(&toputinode[cnt]->i_data, 0);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001924 mutex_unlock(&toputinode[cnt]->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925 mark_inode_dirty(toputinode[cnt]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926 }
Ingo Molnard3be9152006-03-23 03:00:29 -08001927 mutex_unlock(&dqopt->dqonoff_mutex);
Jan Karaca785ec2008-09-30 17:53:37 +02001928 }
1929 if (sb->s_bdev)
1930 invalidate_bdev(sb->s_bdev);
1931put_inodes:
1932 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1933 if (toputinode[cnt]) {
Jan Kara0ff5af82008-04-28 02:14:33 -07001934 /* On remount RO, we keep the inode pointer so that we
Jan Karaf55abc02008-08-20 17:50:32 +02001935 * can reenable quota on the subsequent remount RW. We
1936 * have to check 'flags' variable and not use sb_has_
1937 * function because another quotaon / quotaoff could
1938 * change global state before we got here. We refuse
1939 * to suspend quotas when there is pending delete on
1940 * the quota file... */
1941 if (!(flags & DQUOT_SUSPENDED))
Jan Kara0ff5af82008-04-28 02:14:33 -07001942 iput(toputinode[cnt]);
1943 else if (!toputinode[cnt]->i_nlink)
1944 ret = -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945 }
Jan Kara0ff5af82008-04-28 02:14:33 -07001946 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947}
Mingming Cao08d03502009-01-14 16:19:32 +01001948EXPORT_SYMBOL(vfs_quota_disable);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949
Jan Karaf55abc02008-08-20 17:50:32 +02001950int vfs_quota_off(struct super_block *sb, int type, int remount)
1951{
1952 return vfs_quota_disable(sb, type, remount ? DQUOT_SUSPENDED :
1953 (DQUOT_USAGE_ENABLED | DQUOT_LIMITS_ENABLED));
1954}
Mingming Cao08d03502009-01-14 16:19:32 +01001955EXPORT_SYMBOL(vfs_quota_off);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956/*
1957 * Turn quotas on on a device
1958 */
1959
Jan Karaf55abc02008-08-20 17:50:32 +02001960/*
1961 * Helper function to turn quotas on when we already have the inode of
1962 * quota file and no quota information is loaded.
1963 */
1964static int vfs_load_quota_inode(struct inode *inode, int type, int format_id,
1965 unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966{
1967 struct quota_format_type *fmt = find_quota_format(format_id);
1968 struct super_block *sb = inode->i_sb;
1969 struct quota_info *dqopt = sb_dqopt(sb);
1970 int error;
1971 int oldflags = -1;
1972
1973 if (!fmt)
1974 return -ESRCH;
1975 if (!S_ISREG(inode->i_mode)) {
1976 error = -EACCES;
1977 goto out_fmt;
1978 }
1979 if (IS_RDONLY(inode)) {
1980 error = -EROFS;
1981 goto out_fmt;
1982 }
1983 if (!sb->s_op->quota_write || !sb->s_op->quota_read) {
1984 error = -EINVAL;
1985 goto out_fmt;
1986 }
Jan Karaf55abc02008-08-20 17:50:32 +02001987 /* Usage always has to be set... */
1988 if (!(flags & DQUOT_USAGE_ENABLED)) {
1989 error = -EINVAL;
1990 goto out_fmt;
1991 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992
Jan Karaca785ec2008-09-30 17:53:37 +02001993 if (!(dqopt->flags & DQUOT_QUOTA_SYS_FILE)) {
1994 /* As we bypass the pagecache we must now flush the inode so
1995 * that we see all the changes from userspace... */
1996 write_inode_now(inode, 1);
1997 /* And now flush the block cache so that kernel sees the
1998 * changes */
1999 invalidate_bdev(sb->s_bdev);
2000 }
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002001 mutex_lock(&inode->i_mutex);
Ingo Molnard3be9152006-03-23 03:00:29 -08002002 mutex_lock(&dqopt->dqonoff_mutex);
Jan Karaf55abc02008-08-20 17:50:32 +02002003 if (sb_has_quota_loaded(sb, type)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004 error = -EBUSY;
2005 goto out_lock;
2006 }
Jan Karaca785ec2008-09-30 17:53:37 +02002007
2008 if (!(dqopt->flags & DQUOT_QUOTA_SYS_FILE)) {
2009 /* We don't want quota and atime on quota files (deadlocks
2010 * possible) Also nobody should write to the file - we use
2011 * special IO operations which ignore the immutable bit. */
2012 down_write(&dqopt->dqptr_sem);
2013 oldflags = inode->i_flags & (S_NOATIME | S_IMMUTABLE | S_NOQUOTA);
2014 inode->i_flags |= S_NOQUOTA | S_NOATIME | S_IMMUTABLE;
2015 up_write(&dqopt->dqptr_sem);
2016 sb->dq_op->drop(inode);
2017 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018
2019 error = -EIO;
2020 dqopt->files[type] = igrab(inode);
2021 if (!dqopt->files[type])
2022 goto out_lock;
2023 error = -EINVAL;
2024 if (!fmt->qf_ops->check_quota_file(sb, type))
2025 goto out_file_init;
2026
2027 dqopt->ops[type] = fmt->qf_ops;
2028 dqopt->info[type].dqi_format = fmt;
Jan Kara0ff5af82008-04-28 02:14:33 -07002029 dqopt->info[type].dqi_fmt_id = format_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030 INIT_LIST_HEAD(&dqopt->info[type].dqi_dirty_list);
Ingo Molnard3be9152006-03-23 03:00:29 -08002031 mutex_lock(&dqopt->dqio_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032 if ((error = dqopt->ops[type]->read_file_info(sb, type)) < 0) {
Ingo Molnard3be9152006-03-23 03:00:29 -08002033 mutex_unlock(&dqopt->dqio_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034 goto out_file_init;
2035 }
Ingo Molnard3be9152006-03-23 03:00:29 -08002036 mutex_unlock(&dqopt->dqio_mutex);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002037 mutex_unlock(&inode->i_mutex);
Jan Karacc334122009-01-12 17:23:05 +01002038 spin_lock(&dq_state_lock);
Jan Karaf55abc02008-08-20 17:50:32 +02002039 dqopt->flags |= dquot_state_flag(flags, type);
Jan Karacc334122009-01-12 17:23:05 +01002040 spin_unlock(&dq_state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041
2042 add_dquot_ref(sb, type);
Ingo Molnard3be9152006-03-23 03:00:29 -08002043 mutex_unlock(&dqopt->dqonoff_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044
2045 return 0;
2046
2047out_file_init:
2048 dqopt->files[type] = NULL;
2049 iput(inode);
2050out_lock:
Ingo Molnard3be9152006-03-23 03:00:29 -08002051 mutex_unlock(&dqopt->dqonoff_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052 if (oldflags != -1) {
2053 down_write(&dqopt->dqptr_sem);
2054 /* Set the flags back (in the case of accidental quotaon()
2055 * on a wrong file we don't want to mess up the flags) */
2056 inode->i_flags &= ~(S_NOATIME | S_NOQUOTA | S_IMMUTABLE);
2057 inode->i_flags |= oldflags;
2058 up_write(&dqopt->dqptr_sem);
2059 }
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002060 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061out_fmt:
2062 put_quota_format(fmt);
2063
2064 return error;
2065}
2066
Jan Kara0ff5af82008-04-28 02:14:33 -07002067/* Reenable quotas on remount RW */
2068static int vfs_quota_on_remount(struct super_block *sb, int type)
2069{
2070 struct quota_info *dqopt = sb_dqopt(sb);
2071 struct inode *inode;
2072 int ret;
Jan Karaf55abc02008-08-20 17:50:32 +02002073 unsigned int flags;
Jan Kara0ff5af82008-04-28 02:14:33 -07002074
2075 mutex_lock(&dqopt->dqonoff_mutex);
2076 if (!sb_has_quota_suspended(sb, type)) {
2077 mutex_unlock(&dqopt->dqonoff_mutex);
2078 return 0;
2079 }
Jan Kara0ff5af82008-04-28 02:14:33 -07002080 inode = dqopt->files[type];
2081 dqopt->files[type] = NULL;
Jan Karacc334122009-01-12 17:23:05 +01002082 spin_lock(&dq_state_lock);
Jan Karaf55abc02008-08-20 17:50:32 +02002083 flags = dqopt->flags & dquot_state_flag(DQUOT_USAGE_ENABLED |
2084 DQUOT_LIMITS_ENABLED, type);
2085 dqopt->flags &= ~dquot_state_flag(DQUOT_STATE_FLAGS, type);
Jan Karacc334122009-01-12 17:23:05 +01002086 spin_unlock(&dq_state_lock);
Jan Kara0ff5af82008-04-28 02:14:33 -07002087 mutex_unlock(&dqopt->dqonoff_mutex);
2088
Jan Karaf55abc02008-08-20 17:50:32 +02002089 flags = dquot_generic_flag(flags, type);
2090 ret = vfs_load_quota_inode(inode, type, dqopt->info[type].dqi_fmt_id,
2091 flags);
Jan Kara0ff5af82008-04-28 02:14:33 -07002092 iput(inode);
2093
2094 return ret;
2095}
2096
Al Viro77e69da2008-08-01 04:29:18 -04002097int vfs_quota_on_path(struct super_block *sb, int type, int format_id,
2098 struct path *path)
2099{
2100 int error = security_quota_on(path->dentry);
2101 if (error)
2102 return error;
2103 /* Quota file not on the same filesystem? */
2104 if (path->mnt->mnt_sb != sb)
2105 error = -EXDEV;
2106 else
Jan Karaf55abc02008-08-20 17:50:32 +02002107 error = vfs_load_quota_inode(path->dentry->d_inode, type,
2108 format_id, DQUOT_USAGE_ENABLED |
2109 DQUOT_LIMITS_ENABLED);
Al Viro77e69da2008-08-01 04:29:18 -04002110 return error;
2111}
Mingming Cao08d03502009-01-14 16:19:32 +01002112EXPORT_SYMBOL(vfs_quota_on_path);
Al Viro77e69da2008-08-01 04:29:18 -04002113
Al Viro82646132008-08-02 00:57:06 -04002114int vfs_quota_on(struct super_block *sb, int type, int format_id, char *name,
Jan Kara0ff5af82008-04-28 02:14:33 -07002115 int remount)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116{
Al Viro82646132008-08-02 00:57:06 -04002117 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118 int error;
2119
Jan Kara0ff5af82008-04-28 02:14:33 -07002120 if (remount)
2121 return vfs_quota_on_remount(sb, type);
2122
Al Viro82646132008-08-02 00:57:06 -04002123 error = kern_path(name, LOOKUP_FOLLOW, &path);
Al Viro77e69da2008-08-01 04:29:18 -04002124 if (!error) {
Al Viro82646132008-08-02 00:57:06 -04002125 error = vfs_quota_on_path(sb, type, format_id, &path);
2126 path_put(&path);
Al Viro77e69da2008-08-01 04:29:18 -04002127 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128 return error;
2129}
Mingming Cao08d03502009-01-14 16:19:32 +01002130EXPORT_SYMBOL(vfs_quota_on);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131
2132/*
Jan Karaf55abc02008-08-20 17:50:32 +02002133 * More powerful function for turning on quotas allowing setting
2134 * of individual quota flags
2135 */
2136int vfs_quota_enable(struct inode *inode, int type, int format_id,
2137 unsigned int flags)
2138{
2139 int ret = 0;
2140 struct super_block *sb = inode->i_sb;
2141 struct quota_info *dqopt = sb_dqopt(sb);
2142
2143 /* Just unsuspend quotas? */
2144 if (flags & DQUOT_SUSPENDED)
2145 return vfs_quota_on_remount(sb, type);
2146 if (!flags)
2147 return 0;
2148 /* Just updating flags needed? */
2149 if (sb_has_quota_loaded(sb, type)) {
2150 mutex_lock(&dqopt->dqonoff_mutex);
2151 /* Now do a reliable test... */
2152 if (!sb_has_quota_loaded(sb, type)) {
2153 mutex_unlock(&dqopt->dqonoff_mutex);
2154 goto load_quota;
2155 }
2156 if (flags & DQUOT_USAGE_ENABLED &&
2157 sb_has_quota_usage_enabled(sb, type)) {
2158 ret = -EBUSY;
2159 goto out_lock;
2160 }
2161 if (flags & DQUOT_LIMITS_ENABLED &&
2162 sb_has_quota_limits_enabled(sb, type)) {
2163 ret = -EBUSY;
2164 goto out_lock;
2165 }
Jan Karacc334122009-01-12 17:23:05 +01002166 spin_lock(&dq_state_lock);
Jan Karaf55abc02008-08-20 17:50:32 +02002167 sb_dqopt(sb)->flags |= dquot_state_flag(flags, type);
Jan Karacc334122009-01-12 17:23:05 +01002168 spin_unlock(&dq_state_lock);
Jan Karaf55abc02008-08-20 17:50:32 +02002169out_lock:
2170 mutex_unlock(&dqopt->dqonoff_mutex);
2171 return ret;
2172 }
2173
2174load_quota:
2175 return vfs_load_quota_inode(inode, type, format_id, flags);
2176}
Mingming Cao08d03502009-01-14 16:19:32 +01002177EXPORT_SYMBOL(vfs_quota_enable);
Jan Karaf55abc02008-08-20 17:50:32 +02002178
2179/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180 * This function is used when filesystem needs to initialize quotas
2181 * during mount time.
2182 */
Christoph Hellwig84de8562005-06-23 00:09:16 -07002183int vfs_quota_on_mount(struct super_block *sb, char *qf_name,
2184 int format_id, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185{
Christoph Hellwig84de8562005-06-23 00:09:16 -07002186 struct dentry *dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187 int error;
2188
Christoph Hellwig2fa389c2005-06-23 00:09:16 -07002189 dentry = lookup_one_len(qf_name, sb->s_root, strlen(qf_name));
Christoph Hellwig84de8562005-06-23 00:09:16 -07002190 if (IS_ERR(dentry))
2191 return PTR_ERR(dentry);
2192
Jan Kara154f4842005-11-28 13:44:14 -08002193 if (!dentry->d_inode) {
2194 error = -ENOENT;
2195 goto out;
2196 }
2197
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198 error = security_quota_on(dentry);
Christoph Hellwig84de8562005-06-23 00:09:16 -07002199 if (!error)
Jan Karaf55abc02008-08-20 17:50:32 +02002200 error = vfs_load_quota_inode(dentry->d_inode, type, format_id,
2201 DQUOT_USAGE_ENABLED | DQUOT_LIMITS_ENABLED);
Christoph Hellwig84de8562005-06-23 00:09:16 -07002202
Jan Kara154f4842005-11-28 13:44:14 -08002203out:
Christoph Hellwig84de8562005-06-23 00:09:16 -07002204 dput(dentry);
2205 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206}
Mingming Cao08d03502009-01-14 16:19:32 +01002207EXPORT_SYMBOL(vfs_quota_on_mount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208
Jan Karab85f4b82008-07-25 01:46:50 -07002209/* Wrapper to turn on quotas when remounting rw */
2210int vfs_dq_quota_on_remount(struct super_block *sb)
2211{
2212 int cnt;
2213 int ret = 0, err;
2214
2215 if (!sb->s_qcop || !sb->s_qcop->quota_on)
2216 return -ENOSYS;
2217 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
2218 err = sb->s_qcop->quota_on(sb, cnt, 0, NULL, 1);
2219 if (err < 0 && !ret)
2220 ret = err;
2221 }
2222 return ret;
2223}
Mingming Cao08d03502009-01-14 16:19:32 +01002224EXPORT_SYMBOL(vfs_dq_quota_on_remount);
Jan Karab85f4b82008-07-25 01:46:50 -07002225
Jan Kara12095462008-08-20 14:45:12 +02002226static inline qsize_t qbtos(qsize_t blocks)
2227{
2228 return blocks << QIF_DQBLKSIZE_BITS;
2229}
2230
2231static inline qsize_t stoqb(qsize_t space)
2232{
2233 return (space + QIF_DQBLKSIZE - 1) >> QIF_DQBLKSIZE_BITS;
2234}
2235
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236/* Generic routine for getting common part of quota structure */
2237static void do_get_dqblk(struct dquot *dquot, struct if_dqblk *di)
2238{
2239 struct mem_dqblk *dm = &dquot->dq_dqb;
2240
2241 spin_lock(&dq_data_lock);
Jan Kara12095462008-08-20 14:45:12 +02002242 di->dqb_bhardlimit = stoqb(dm->dqb_bhardlimit);
2243 di->dqb_bsoftlimit = stoqb(dm->dqb_bsoftlimit);
Mingming Caof18df222009-01-13 16:43:09 +01002244 di->dqb_curspace = dm->dqb_curspace + dm->dqb_rsvspace;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245 di->dqb_ihardlimit = dm->dqb_ihardlimit;
2246 di->dqb_isoftlimit = dm->dqb_isoftlimit;
2247 di->dqb_curinodes = dm->dqb_curinodes;
2248 di->dqb_btime = dm->dqb_btime;
2249 di->dqb_itime = dm->dqb_itime;
2250 di->dqb_valid = QIF_ALL;
2251 spin_unlock(&dq_data_lock);
2252}
2253
2254int vfs_get_dqblk(struct super_block *sb, int type, qid_t id, struct if_dqblk *di)
2255{
2256 struct dquot *dquot;
2257
Jan Karacc334122009-01-12 17:23:05 +01002258 dquot = dqget(sb, id, type);
2259 if (dquot == NODQUOT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260 return -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002261 do_get_dqblk(dquot, di);
2262 dqput(dquot);
Jan Karacc334122009-01-12 17:23:05 +01002263
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264 return 0;
2265}
Mingming Cao08d03502009-01-14 16:19:32 +01002266EXPORT_SYMBOL(vfs_get_dqblk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267
2268/* Generic routine for setting common part of quota structure */
Andrew Perepechko338bf9a2008-04-28 02:14:31 -07002269static int do_set_dqblk(struct dquot *dquot, struct if_dqblk *di)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270{
2271 struct mem_dqblk *dm = &dquot->dq_dqb;
2272 int check_blim = 0, check_ilim = 0;
Andrew Perepechko338bf9a2008-04-28 02:14:31 -07002273 struct mem_dqinfo *dqi = &sb_dqopt(dquot->dq_sb)->info[dquot->dq_type];
2274
2275 if ((di->dqb_valid & QIF_BLIMITS &&
2276 (di->dqb_bhardlimit > dqi->dqi_maxblimit ||
2277 di->dqb_bsoftlimit > dqi->dqi_maxblimit)) ||
2278 (di->dqb_valid & QIF_ILIMITS &&
2279 (di->dqb_ihardlimit > dqi->dqi_maxilimit ||
2280 di->dqb_isoftlimit > dqi->dqi_maxilimit)))
2281 return -ERANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282
2283 spin_lock(&dq_data_lock);
2284 if (di->dqb_valid & QIF_SPACE) {
Mingming Caof18df222009-01-13 16:43:09 +01002285 dm->dqb_curspace = di->dqb_curspace - dm->dqb_rsvspace;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286 check_blim = 1;
Jan Kara4d59bce2008-10-02 16:48:10 +02002287 __set_bit(DQ_LASTSET_B + QIF_SPACE_B, &dquot->dq_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288 }
2289 if (di->dqb_valid & QIF_BLIMITS) {
Jan Kara12095462008-08-20 14:45:12 +02002290 dm->dqb_bsoftlimit = qbtos(di->dqb_bsoftlimit);
2291 dm->dqb_bhardlimit = qbtos(di->dqb_bhardlimit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292 check_blim = 1;
Jan Kara4d59bce2008-10-02 16:48:10 +02002293 __set_bit(DQ_LASTSET_B + QIF_BLIMITS_B, &dquot->dq_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294 }
2295 if (di->dqb_valid & QIF_INODES) {
2296 dm->dqb_curinodes = di->dqb_curinodes;
2297 check_ilim = 1;
Jan Kara4d59bce2008-10-02 16:48:10 +02002298 __set_bit(DQ_LASTSET_B + QIF_INODES_B, &dquot->dq_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299 }
2300 if (di->dqb_valid & QIF_ILIMITS) {
2301 dm->dqb_isoftlimit = di->dqb_isoftlimit;
2302 dm->dqb_ihardlimit = di->dqb_ihardlimit;
2303 check_ilim = 1;
Jan Kara4d59bce2008-10-02 16:48:10 +02002304 __set_bit(DQ_LASTSET_B + QIF_ILIMITS_B, &dquot->dq_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305 }
Jan Kara4d59bce2008-10-02 16:48:10 +02002306 if (di->dqb_valid & QIF_BTIME) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307 dm->dqb_btime = di->dqb_btime;
Jan Karae04a88a92009-01-07 18:07:29 -08002308 check_blim = 1;
Jan Kara4d59bce2008-10-02 16:48:10 +02002309 __set_bit(DQ_LASTSET_B + QIF_BTIME_B, &dquot->dq_flags);
2310 }
2311 if (di->dqb_valid & QIF_ITIME) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312 dm->dqb_itime = di->dqb_itime;
Jan Karae04a88a92009-01-07 18:07:29 -08002313 check_ilim = 1;
Jan Kara4d59bce2008-10-02 16:48:10 +02002314 __set_bit(DQ_LASTSET_B + QIF_ITIME_B, &dquot->dq_flags);
2315 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316
2317 if (check_blim) {
Jan Kara12095462008-08-20 14:45:12 +02002318 if (!dm->dqb_bsoftlimit || dm->dqb_curspace < dm->dqb_bsoftlimit) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319 dm->dqb_btime = 0;
2320 clear_bit(DQ_BLKS_B, &dquot->dq_flags);
2321 }
2322 else if (!(di->dqb_valid & QIF_BTIME)) /* Set grace only if user hasn't provided his own... */
Andrew Perepechko338bf9a2008-04-28 02:14:31 -07002323 dm->dqb_btime = get_seconds() + dqi->dqi_bgrace;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324 }
2325 if (check_ilim) {
2326 if (!dm->dqb_isoftlimit || dm->dqb_curinodes < dm->dqb_isoftlimit) {
2327 dm->dqb_itime = 0;
2328 clear_bit(DQ_INODES_B, &dquot->dq_flags);
2329 }
2330 else if (!(di->dqb_valid & QIF_ITIME)) /* Set grace only if user hasn't provided his own... */
Andrew Perepechko338bf9a2008-04-28 02:14:31 -07002331 dm->dqb_itime = get_seconds() + dqi->dqi_igrace;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002332 }
2333 if (dm->dqb_bhardlimit || dm->dqb_bsoftlimit || dm->dqb_ihardlimit || dm->dqb_isoftlimit)
2334 clear_bit(DQ_FAKE_B, &dquot->dq_flags);
2335 else
2336 set_bit(DQ_FAKE_B, &dquot->dq_flags);
2337 spin_unlock(&dq_data_lock);
2338 mark_dquot_dirty(dquot);
Andrew Perepechko338bf9a2008-04-28 02:14:31 -07002339
2340 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341}
2342
2343int vfs_set_dqblk(struct super_block *sb, int type, qid_t id, struct if_dqblk *di)
2344{
2345 struct dquot *dquot;
Andrew Perepechko338bf9a2008-04-28 02:14:31 -07002346 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002347
Jan Karaf55abc02008-08-20 17:50:32 +02002348 dquot = dqget(sb, id, type);
2349 if (!dquot) {
2350 rc = -ESRCH;
2351 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352 }
Andrew Perepechko338bf9a2008-04-28 02:14:31 -07002353 rc = do_set_dqblk(dquot, di);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002354 dqput(dquot);
Jan Karaf55abc02008-08-20 17:50:32 +02002355out:
Andrew Perepechko338bf9a2008-04-28 02:14:31 -07002356 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357}
Mingming Cao08d03502009-01-14 16:19:32 +01002358EXPORT_SYMBOL(vfs_set_dqblk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359
2360/* Generic routine for getting common part of quota file information */
2361int vfs_get_dqinfo(struct super_block *sb, int type, struct if_dqinfo *ii)
2362{
2363 struct mem_dqinfo *mi;
2364
Ingo Molnard3be9152006-03-23 03:00:29 -08002365 mutex_lock(&sb_dqopt(sb)->dqonoff_mutex);
Jan Karaf55abc02008-08-20 17:50:32 +02002366 if (!sb_has_quota_active(sb, type)) {
Ingo Molnard3be9152006-03-23 03:00:29 -08002367 mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002368 return -ESRCH;
2369 }
2370 mi = sb_dqopt(sb)->info + type;
2371 spin_lock(&dq_data_lock);
2372 ii->dqi_bgrace = mi->dqi_bgrace;
2373 ii->dqi_igrace = mi->dqi_igrace;
2374 ii->dqi_flags = mi->dqi_flags & DQF_MASK;
2375 ii->dqi_valid = IIF_ALL;
2376 spin_unlock(&dq_data_lock);
Ingo Molnard3be9152006-03-23 03:00:29 -08002377 mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002378 return 0;
2379}
Mingming Cao08d03502009-01-14 16:19:32 +01002380EXPORT_SYMBOL(vfs_get_dqinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381
2382/* Generic routine for setting common part of quota file information */
2383int vfs_set_dqinfo(struct super_block *sb, int type, struct if_dqinfo *ii)
2384{
2385 struct mem_dqinfo *mi;
Jan Karaf55abc02008-08-20 17:50:32 +02002386 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002387
Ingo Molnard3be9152006-03-23 03:00:29 -08002388 mutex_lock(&sb_dqopt(sb)->dqonoff_mutex);
Jan Karaf55abc02008-08-20 17:50:32 +02002389 if (!sb_has_quota_active(sb, type)) {
2390 err = -ESRCH;
2391 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392 }
2393 mi = sb_dqopt(sb)->info + type;
2394 spin_lock(&dq_data_lock);
2395 if (ii->dqi_valid & IIF_BGRACE)
2396 mi->dqi_bgrace = ii->dqi_bgrace;
2397 if (ii->dqi_valid & IIF_IGRACE)
2398 mi->dqi_igrace = ii->dqi_igrace;
2399 if (ii->dqi_valid & IIF_FLAGS)
2400 mi->dqi_flags = (mi->dqi_flags & ~DQF_MASK) | (ii->dqi_flags & DQF_MASK);
2401 spin_unlock(&dq_data_lock);
2402 mark_info_dirty(sb, type);
2403 /* Force write to disk */
2404 sb->dq_op->write_info(sb, type);
Jan Karaf55abc02008-08-20 17:50:32 +02002405out:
Ingo Molnard3be9152006-03-23 03:00:29 -08002406 mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
Jan Karaf55abc02008-08-20 17:50:32 +02002407 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002408}
Mingming Cao08d03502009-01-14 16:19:32 +01002409EXPORT_SYMBOL(vfs_set_dqinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002410
2411struct quotactl_ops vfs_quotactl_ops = {
2412 .quota_on = vfs_quota_on,
2413 .quota_off = vfs_quota_off,
2414 .quota_sync = vfs_quota_sync,
2415 .get_info = vfs_get_dqinfo,
2416 .set_info = vfs_set_dqinfo,
2417 .get_dqblk = vfs_get_dqblk,
2418 .set_dqblk = vfs_set_dqblk
2419};
2420
2421static ctl_table fs_dqstats_table[] = {
2422 {
2423 .ctl_name = FS_DQ_LOOKUPS,
2424 .procname = "lookups",
2425 .data = &dqstats.lookups,
2426 .maxlen = sizeof(int),
2427 .mode = 0444,
2428 .proc_handler = &proc_dointvec,
2429 },
2430 {
2431 .ctl_name = FS_DQ_DROPS,
2432 .procname = "drops",
2433 .data = &dqstats.drops,
2434 .maxlen = sizeof(int),
2435 .mode = 0444,
2436 .proc_handler = &proc_dointvec,
2437 },
2438 {
2439 .ctl_name = FS_DQ_READS,
2440 .procname = "reads",
2441 .data = &dqstats.reads,
2442 .maxlen = sizeof(int),
2443 .mode = 0444,
2444 .proc_handler = &proc_dointvec,
2445 },
2446 {
2447 .ctl_name = FS_DQ_WRITES,
2448 .procname = "writes",
2449 .data = &dqstats.writes,
2450 .maxlen = sizeof(int),
2451 .mode = 0444,
2452 .proc_handler = &proc_dointvec,
2453 },
2454 {
2455 .ctl_name = FS_DQ_CACHE_HITS,
2456 .procname = "cache_hits",
2457 .data = &dqstats.cache_hits,
2458 .maxlen = sizeof(int),
2459 .mode = 0444,
2460 .proc_handler = &proc_dointvec,
2461 },
2462 {
2463 .ctl_name = FS_DQ_ALLOCATED,
2464 .procname = "allocated_dquots",
2465 .data = &dqstats.allocated_dquots,
2466 .maxlen = sizeof(int),
2467 .mode = 0444,
2468 .proc_handler = &proc_dointvec,
2469 },
2470 {
2471 .ctl_name = FS_DQ_FREE,
2472 .procname = "free_dquots",
2473 .data = &dqstats.free_dquots,
2474 .maxlen = sizeof(int),
2475 .mode = 0444,
2476 .proc_handler = &proc_dointvec,
2477 },
2478 {
2479 .ctl_name = FS_DQ_SYNCS,
2480 .procname = "syncs",
2481 .data = &dqstats.syncs,
2482 .maxlen = sizeof(int),
2483 .mode = 0444,
2484 .proc_handler = &proc_dointvec,
2485 },
Jan Kara8e893462007-10-16 23:29:31 -07002486#ifdef CONFIG_PRINT_QUOTA_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07002487 {
2488 .ctl_name = FS_DQ_WARNINGS,
2489 .procname = "warnings",
2490 .data = &flag_print_warnings,
2491 .maxlen = sizeof(int),
2492 .mode = 0644,
2493 .proc_handler = &proc_dointvec,
2494 },
Jan Kara8e893462007-10-16 23:29:31 -07002495#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002496 { .ctl_name = 0 },
2497};
2498
2499static ctl_table fs_table[] = {
2500 {
2501 .ctl_name = FS_DQSTATS,
2502 .procname = "quota",
2503 .mode = 0555,
2504 .child = fs_dqstats_table,
2505 },
2506 { .ctl_name = 0 },
2507};
2508
2509static ctl_table sys_table[] = {
2510 {
2511 .ctl_name = CTL_FS,
2512 .procname = "fs",
2513 .mode = 0555,
2514 .child = fs_table,
2515 },
2516 { .ctl_name = 0 },
2517};
2518
2519static int __init dquot_init(void)
2520{
2521 int i;
2522 unsigned long nr_hash, order;
2523
2524 printk(KERN_NOTICE "VFS: Disk quotas %s\n", __DQUOT_VERSION__);
2525
Eric W. Biederman0b4d4142007-02-14 00:34:09 -08002526 register_sysctl_table(sys_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002527
Paul Mundt20c2df82007-07-20 10:11:58 +09002528 dquot_cachep = kmem_cache_create("dquot",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002529 sizeof(struct dquot), sizeof(unsigned long) * 4,
Paul Jacksonfffb60f2006-03-24 03:16:06 -08002530 (SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT|
2531 SLAB_MEM_SPREAD|SLAB_PANIC),
Paul Mundt20c2df82007-07-20 10:11:58 +09002532 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002533
2534 order = 0;
2535 dquot_hash = (struct hlist_head *)__get_free_pages(GFP_ATOMIC, order);
2536 if (!dquot_hash)
2537 panic("Cannot create dquot hash table");
2538
2539 /* Find power-of-two hlist_heads which can fit into allocation */
2540 nr_hash = (1UL << order) * PAGE_SIZE / sizeof(struct hlist_head);
2541 dq_hash_bits = 0;
2542 do {
2543 dq_hash_bits++;
2544 } while (nr_hash >> dq_hash_bits);
2545 dq_hash_bits--;
2546
2547 nr_hash = 1UL << dq_hash_bits;
2548 dq_hash_mask = nr_hash - 1;
2549 for (i = 0; i < nr_hash; i++)
2550 INIT_HLIST_HEAD(dquot_hash + i);
2551
2552 printk("Dquot-cache hash table entries: %ld (order %ld, %ld bytes)\n",
2553 nr_hash, order, (PAGE_SIZE << order));
2554
Rusty Russell8e1f9362007-07-17 04:03:17 -07002555 register_shrinker(&dqcache_shrinker);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002556
Jan Kara8e893462007-10-16 23:29:31 -07002557#ifdef CONFIG_QUOTA_NETLINK_INTERFACE
2558 if (genl_register_family(&quota_genl_family) != 0)
2559 printk(KERN_ERR "VFS: Failed to create quota netlink interface.\n");
2560#endif
2561
Linus Torvalds1da177e2005-04-16 15:20:36 -07002562 return 0;
2563}
2564module_init(dquot_init);