blob: 2916f91ca40ccbba496b8d242bd758d987484ed2 [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
132static DEFINE_SPINLOCK(dq_list_lock);
Jan Karacc334122009-01-12 17:23:05 +0100133static DEFINE_SPINLOCK(dq_state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134DEFINE_SPINLOCK(dq_data_lock);
135
136static char *quotatypes[] = INITQFNAMES;
137static struct quota_format_type *quota_formats; /* List of registered formats */
138static struct quota_module_name module_names[] = INIT_QUOTA_MODULE_NAMES;
139
140/* SLAB cache for dquot structures */
Christoph Lametere18b8902006-12-06 20:33:20 -0800141static struct kmem_cache *dquot_cachep;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142
143int register_quota_format(struct quota_format_type *fmt)
144{
145 spin_lock(&dq_list_lock);
146 fmt->qf_next = quota_formats;
147 quota_formats = fmt;
148 spin_unlock(&dq_list_lock);
149 return 0;
150}
151
152void unregister_quota_format(struct quota_format_type *fmt)
153{
154 struct quota_format_type **actqf;
155
156 spin_lock(&dq_list_lock);
157 for (actqf = &quota_formats; *actqf && *actqf != fmt; actqf = &(*actqf)->qf_next);
158 if (*actqf)
159 *actqf = (*actqf)->qf_next;
160 spin_unlock(&dq_list_lock);
161}
162
163static struct quota_format_type *find_quota_format(int id)
164{
165 struct quota_format_type *actqf;
166
167 spin_lock(&dq_list_lock);
168 for (actqf = quota_formats; actqf && actqf->qf_fmt_id != id; actqf = actqf->qf_next);
169 if (!actqf || !try_module_get(actqf->qf_owner)) {
170 int qm;
171
172 spin_unlock(&dq_list_lock);
173
174 for (qm = 0; module_names[qm].qm_fmt_id && module_names[qm].qm_fmt_id != id; qm++);
175 if (!module_names[qm].qm_fmt_id || request_module(module_names[qm].qm_mod_name))
176 return NULL;
177
178 spin_lock(&dq_list_lock);
179 for (actqf = quota_formats; actqf && actqf->qf_fmt_id != id; actqf = actqf->qf_next);
180 if (actqf && !try_module_get(actqf->qf_owner))
181 actqf = NULL;
182 }
183 spin_unlock(&dq_list_lock);
184 return actqf;
185}
186
187static void put_quota_format(struct quota_format_type *fmt)
188{
189 module_put(fmt->qf_owner);
190}
191
192/*
193 * Dquot List Management:
194 * The quota code uses three lists for dquot management: the inuse_list,
195 * free_dquots, and dquot_hash[] array. A single dquot structure may be
196 * on all three lists, depending on its current state.
197 *
198 * All dquots are placed to the end of inuse_list when first created, and this
199 * list is used for invalidate operation, which must look at every dquot.
200 *
201 * Unused dquots (dq_count == 0) are added to the free_dquots list when freed,
202 * and this list is searched whenever we need an available dquot. Dquots are
203 * removed from the list as soon as they are used again, and
204 * dqstats.free_dquots gives the number of dquots on the list. When
205 * dquot is invalidated it's completely released from memory.
206 *
207 * Dquots with a specific identity (device, type and id) are placed on
208 * one of the dquot_hash[] hash chains. The provides an efficient search
209 * mechanism to locate a specific dquot.
210 */
211
212static LIST_HEAD(inuse_list);
213static LIST_HEAD(free_dquots);
214static unsigned int dq_hash_bits, dq_hash_mask;
215static struct hlist_head *dquot_hash;
216
217struct dqstats dqstats;
218
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219static inline unsigned int
220hashfn(const struct super_block *sb, unsigned int id, int type)
221{
222 unsigned long tmp;
223
224 tmp = (((unsigned long)sb>>L1_CACHE_SHIFT) ^ id) * (MAXQUOTAS - type);
225 return (tmp + (tmp >> dq_hash_bits)) & dq_hash_mask;
226}
227
228/*
229 * Following list functions expect dq_list_lock to be held
230 */
231static inline void insert_dquot_hash(struct dquot *dquot)
232{
233 struct hlist_head *head = dquot_hash + hashfn(dquot->dq_sb, dquot->dq_id, dquot->dq_type);
234 hlist_add_head(&dquot->dq_hash, head);
235}
236
237static inline void remove_dquot_hash(struct dquot *dquot)
238{
239 hlist_del_init(&dquot->dq_hash);
240}
241
242static inline struct dquot *find_dquot(unsigned int hashent, struct super_block *sb, unsigned int id, int type)
243{
244 struct hlist_node *node;
245 struct dquot *dquot;
246
247 hlist_for_each (node, dquot_hash+hashent) {
248 dquot = hlist_entry(node, struct dquot, dq_hash);
249 if (dquot->dq_sb == sb && dquot->dq_id == id && dquot->dq_type == type)
250 return dquot;
251 }
252 return NODQUOT;
253}
254
255/* Add a dquot to the tail of the free list */
256static inline void put_dquot_last(struct dquot *dquot)
257{
Akinobu Mita8e130592006-06-26 00:24:37 -0700258 list_add_tail(&dquot->dq_free, &free_dquots);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 dqstats.free_dquots++;
260}
261
262static inline void remove_free_dquot(struct dquot *dquot)
263{
264 if (list_empty(&dquot->dq_free))
265 return;
266 list_del_init(&dquot->dq_free);
267 dqstats.free_dquots--;
268}
269
270static inline void put_inuse(struct dquot *dquot)
271{
272 /* We add to the back of inuse list so we don't have to restart
273 * when traversing this list and we block */
Akinobu Mita8e130592006-06-26 00:24:37 -0700274 list_add_tail(&dquot->dq_inuse, &inuse_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 dqstats.allocated_dquots++;
276}
277
278static inline void remove_inuse(struct dquot *dquot)
279{
280 dqstats.allocated_dquots--;
281 list_del(&dquot->dq_inuse);
282}
283/*
284 * End of list functions needing dq_list_lock
285 */
286
287static void wait_on_dquot(struct dquot *dquot)
288{
Ingo Molnard3be9152006-03-23 03:00:29 -0800289 mutex_lock(&dquot->dq_lock);
290 mutex_unlock(&dquot->dq_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291}
292
Jan Kara03f6e922008-04-28 02:14:32 -0700293static inline int dquot_dirty(struct dquot *dquot)
294{
295 return test_bit(DQ_MOD_B, &dquot->dq_flags);
296}
297
298static inline int mark_dquot_dirty(struct dquot *dquot)
299{
300 return dquot->dq_sb->dq_op->mark_dirty(dquot);
301}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302
303int dquot_mark_dquot_dirty(struct dquot *dquot)
304{
305 spin_lock(&dq_list_lock);
306 if (!test_and_set_bit(DQ_MOD_B, &dquot->dq_flags))
307 list_add(&dquot->dq_dirty, &sb_dqopt(dquot->dq_sb)->
308 info[dquot->dq_type].dqi_dirty_list);
309 spin_unlock(&dq_list_lock);
310 return 0;
311}
312
313/* This function needs dq_list_lock */
314static inline int clear_dquot_dirty(struct dquot *dquot)
315{
316 if (!test_and_clear_bit(DQ_MOD_B, &dquot->dq_flags))
317 return 0;
318 list_del_init(&dquot->dq_dirty);
319 return 1;
320}
321
322void mark_info_dirty(struct super_block *sb, int type)
323{
324 set_bit(DQF_INFO_DIRTY_B, &sb_dqopt(sb)->info[type].dqi_flags);
325}
326EXPORT_SYMBOL(mark_info_dirty);
327
328/*
329 * Read dquot from disk and alloc space for it
330 */
331
332int dquot_acquire(struct dquot *dquot)
333{
334 int ret = 0, ret2 = 0;
335 struct quota_info *dqopt = sb_dqopt(dquot->dq_sb);
336
Ingo Molnard3be9152006-03-23 03:00:29 -0800337 mutex_lock(&dquot->dq_lock);
338 mutex_lock(&dqopt->dqio_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 if (!test_bit(DQ_READ_B, &dquot->dq_flags))
340 ret = dqopt->ops[dquot->dq_type]->read_dqblk(dquot);
341 if (ret < 0)
342 goto out_iolock;
343 set_bit(DQ_READ_B, &dquot->dq_flags);
344 /* Instantiate dquot if needed */
345 if (!test_bit(DQ_ACTIVE_B, &dquot->dq_flags) && !dquot->dq_off) {
346 ret = dqopt->ops[dquot->dq_type]->commit_dqblk(dquot);
347 /* Write the info if needed */
348 if (info_dirty(&dqopt->info[dquot->dq_type]))
349 ret2 = dqopt->ops[dquot->dq_type]->write_file_info(dquot->dq_sb, dquot->dq_type);
350 if (ret < 0)
351 goto out_iolock;
352 if (ret2 < 0) {
353 ret = ret2;
354 goto out_iolock;
355 }
356 }
357 set_bit(DQ_ACTIVE_B, &dquot->dq_flags);
358out_iolock:
Ingo Molnard3be9152006-03-23 03:00:29 -0800359 mutex_unlock(&dqopt->dqio_mutex);
360 mutex_unlock(&dquot->dq_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 return ret;
362}
363
364/*
365 * Write dquot to disk
366 */
367int dquot_commit(struct dquot *dquot)
368{
369 int ret = 0, ret2 = 0;
370 struct quota_info *dqopt = sb_dqopt(dquot->dq_sb);
371
Ingo Molnard3be9152006-03-23 03:00:29 -0800372 mutex_lock(&dqopt->dqio_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 spin_lock(&dq_list_lock);
374 if (!clear_dquot_dirty(dquot)) {
375 spin_unlock(&dq_list_lock);
376 goto out_sem;
377 }
378 spin_unlock(&dq_list_lock);
379 /* Inactive dquot can be only if there was error during read/init
380 * => we have better not writing it */
381 if (test_bit(DQ_ACTIVE_B, &dquot->dq_flags)) {
382 ret = dqopt->ops[dquot->dq_type]->commit_dqblk(dquot);
383 if (info_dirty(&dqopt->info[dquot->dq_type]))
384 ret2 = dqopt->ops[dquot->dq_type]->write_file_info(dquot->dq_sb, dquot->dq_type);
385 if (ret >= 0)
386 ret = ret2;
387 }
388out_sem:
Ingo Molnard3be9152006-03-23 03:00:29 -0800389 mutex_unlock(&dqopt->dqio_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 return ret;
391}
392
393/*
394 * Release dquot
395 */
396int dquot_release(struct dquot *dquot)
397{
398 int ret = 0, ret2 = 0;
399 struct quota_info *dqopt = sb_dqopt(dquot->dq_sb);
400
Ingo Molnard3be9152006-03-23 03:00:29 -0800401 mutex_lock(&dquot->dq_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 /* Check whether we are not racing with some other dqget() */
403 if (atomic_read(&dquot->dq_count) > 1)
404 goto out_dqlock;
Ingo Molnard3be9152006-03-23 03:00:29 -0800405 mutex_lock(&dqopt->dqio_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 if (dqopt->ops[dquot->dq_type]->release_dqblk) {
407 ret = dqopt->ops[dquot->dq_type]->release_dqblk(dquot);
408 /* Write the info */
409 if (info_dirty(&dqopt->info[dquot->dq_type]))
410 ret2 = dqopt->ops[dquot->dq_type]->write_file_info(dquot->dq_sb, dquot->dq_type);
411 if (ret >= 0)
412 ret = ret2;
413 }
414 clear_bit(DQ_ACTIVE_B, &dquot->dq_flags);
Ingo Molnard3be9152006-03-23 03:00:29 -0800415 mutex_unlock(&dqopt->dqio_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416out_dqlock:
Ingo Molnard3be9152006-03-23 03:00:29 -0800417 mutex_unlock(&dquot->dq_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 return ret;
419}
420
Jan Kara7d9056b2008-11-25 15:31:32 +0100421void dquot_destroy(struct dquot *dquot)
Jan Kara74f783a2008-08-19 14:51:22 +0200422{
423 kmem_cache_free(dquot_cachep, dquot);
424}
Jan Kara7d9056b2008-11-25 15:31:32 +0100425EXPORT_SYMBOL(dquot_destroy);
Jan Kara74f783a2008-08-19 14:51:22 +0200426
427static inline void do_destroy_dquot(struct dquot *dquot)
428{
429 dquot->dq_sb->dq_op->destroy_dquot(dquot);
430}
431
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432/* Invalidate all dquots on the list. Note that this function is called after
433 * quota is disabled and pointers from inodes removed so there cannot be new
Jan Kara6362e4d2006-03-23 03:00:17 -0800434 * quota users. There can still be some users of quotas due to inodes being
435 * just deleted or pruned by prune_icache() (those are not attached to any
Jan Karacc334122009-01-12 17:23:05 +0100436 * list) or parallel quotactl call. We have to wait for such users.
Jan Kara6362e4d2006-03-23 03:00:17 -0800437 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438static void invalidate_dquots(struct super_block *sb, int type)
439{
Domen Puncerc33ed272005-06-25 14:59:36 -0700440 struct dquot *dquot, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441
Jan Kara6362e4d2006-03-23 03:00:17 -0800442restart:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 spin_lock(&dq_list_lock);
Domen Puncerc33ed272005-06-25 14:59:36 -0700444 list_for_each_entry_safe(dquot, tmp, &inuse_list, dq_inuse) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 if (dquot->dq_sb != sb)
446 continue;
447 if (dquot->dq_type != type)
448 continue;
Jan Kara6362e4d2006-03-23 03:00:17 -0800449 /* Wait for dquot users */
450 if (atomic_read(&dquot->dq_count)) {
451 DEFINE_WAIT(wait);
452
453 atomic_inc(&dquot->dq_count);
454 prepare_to_wait(&dquot->dq_wait_unused, &wait,
455 TASK_UNINTERRUPTIBLE);
456 spin_unlock(&dq_list_lock);
457 /* Once dqput() wakes us up, we know it's time to free
458 * the dquot.
459 * IMPORTANT: we rely on the fact that there is always
460 * at most one process waiting for dquot to free.
461 * Otherwise dq_count would be > 1 and we would never
462 * wake up.
463 */
464 if (atomic_read(&dquot->dq_count) > 1)
465 schedule();
466 finish_wait(&dquot->dq_wait_unused, &wait);
467 dqput(dquot);
468 /* At this moment dquot() need not exist (it could be
469 * reclaimed by prune_dqcache(). Hence we must
470 * restart. */
471 goto restart;
472 }
473 /*
474 * Quota now has no users and it has been written on last
475 * dqput()
476 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 remove_dquot_hash(dquot);
478 remove_free_dquot(dquot);
479 remove_inuse(dquot);
Jan Kara74f783a2008-08-19 14:51:22 +0200480 do_destroy_dquot(dquot);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 }
482 spin_unlock(&dq_list_lock);
483}
484
Jan Kara12c77522008-10-20 17:05:00 +0200485/* Call callback for every active dquot on given filesystem */
486int dquot_scan_active(struct super_block *sb,
487 int (*fn)(struct dquot *dquot, unsigned long priv),
488 unsigned long priv)
489{
490 struct dquot *dquot, *old_dquot = NULL;
491 int ret = 0;
492
493 mutex_lock(&sb_dqopt(sb)->dqonoff_mutex);
494 spin_lock(&dq_list_lock);
495 list_for_each_entry(dquot, &inuse_list, dq_inuse) {
496 if (!test_bit(DQ_ACTIVE_B, &dquot->dq_flags))
497 continue;
498 if (dquot->dq_sb != sb)
499 continue;
500 /* Now we have active dquot so we can just increase use count */
501 atomic_inc(&dquot->dq_count);
502 dqstats.lookups++;
503 spin_unlock(&dq_list_lock);
504 dqput(old_dquot);
505 old_dquot = dquot;
506 ret = fn(dquot, priv);
507 if (ret < 0)
508 goto out;
509 spin_lock(&dq_list_lock);
510 /* We are safe to continue now because our dquot could not
511 * be moved out of the inuse list while we hold the reference */
512 }
513 spin_unlock(&dq_list_lock);
514out:
515 dqput(old_dquot);
516 mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
517 return ret;
518}
519
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520int vfs_quota_sync(struct super_block *sb, int type)
521{
522 struct list_head *dirty;
523 struct dquot *dquot;
524 struct quota_info *dqopt = sb_dqopt(sb);
525 int cnt;
526
Ingo Molnard3be9152006-03-23 03:00:29 -0800527 mutex_lock(&dqopt->dqonoff_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
529 if (type != -1 && cnt != type)
530 continue;
Jan Karaf55abc02008-08-20 17:50:32 +0200531 if (!sb_has_quota_active(sb, cnt))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 continue;
533 spin_lock(&dq_list_lock);
534 dirty = &dqopt->info[cnt].dqi_dirty_list;
535 while (!list_empty(dirty)) {
Pavel Emelianovb5e61812007-05-08 00:30:19 -0700536 dquot = list_first_entry(dirty, struct dquot, dq_dirty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 /* Dirty and inactive can be only bad dquot... */
538 if (!test_bit(DQ_ACTIVE_B, &dquot->dq_flags)) {
539 clear_dquot_dirty(dquot);
540 continue;
541 }
542 /* Now we have active dquot from which someone is
543 * holding reference so we can safely just increase
544 * use count */
545 atomic_inc(&dquot->dq_count);
546 dqstats.lookups++;
547 spin_unlock(&dq_list_lock);
548 sb->dq_op->write_dquot(dquot);
549 dqput(dquot);
550 spin_lock(&dq_list_lock);
551 }
552 spin_unlock(&dq_list_lock);
553 }
554
555 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
Jan Karaf55abc02008-08-20 17:50:32 +0200556 if ((cnt == type || type == -1) && sb_has_quota_active(sb, cnt)
557 && info_dirty(&dqopt->info[cnt]))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 sb->dq_op->write_info(sb, cnt);
559 spin_lock(&dq_list_lock);
560 dqstats.syncs++;
561 spin_unlock(&dq_list_lock);
Ingo Molnard3be9152006-03-23 03:00:29 -0800562 mutex_unlock(&dqopt->dqonoff_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563
564 return 0;
565}
566
567/* Free unused dquots from cache */
568static void prune_dqcache(int count)
569{
570 struct list_head *head;
571 struct dquot *dquot;
572
573 head = free_dquots.prev;
574 while (head != &free_dquots && count) {
575 dquot = list_entry(head, struct dquot, dq_free);
576 remove_dquot_hash(dquot);
577 remove_free_dquot(dquot);
578 remove_inuse(dquot);
Jan Kara74f783a2008-08-19 14:51:22 +0200579 do_destroy_dquot(dquot);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 count--;
581 head = free_dquots.prev;
582 }
583}
584
585/*
586 * This is called from kswapd when we think we need some
587 * more memory
588 */
589
Al Viro27496a82005-10-21 03:20:48 -0400590static int shrink_dqcache_memory(int nr, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591{
592 if (nr) {
593 spin_lock(&dq_list_lock);
594 prune_dqcache(nr);
595 spin_unlock(&dq_list_lock);
596 }
597 return (dqstats.free_dquots / 100) * sysctl_vfs_cache_pressure;
598}
599
Rusty Russell8e1f9362007-07-17 04:03:17 -0700600static struct shrinker dqcache_shrinker = {
601 .shrink = shrink_dqcache_memory,
602 .seeks = DEFAULT_SEEKS,
603};
604
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605/*
606 * Put reference to dquot
607 * NOTE: If you change this function please check whether dqput_blocks() works right...
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 */
Jan Kara3d9ea252008-10-10 16:12:23 +0200609void dqput(struct dquot *dquot)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610{
Jan Karab48d3802008-07-25 01:46:49 -0700611 int ret;
612
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 if (!dquot)
614 return;
615#ifdef __DQUOT_PARANOIA
616 if (!atomic_read(&dquot->dq_count)) {
617 printk("VFS: dqput: trying to free free dquot\n");
618 printk("VFS: device %s, dquot of %s %d\n",
619 dquot->dq_sb->s_id,
620 quotatypes[dquot->dq_type],
621 dquot->dq_id);
622 BUG();
623 }
624#endif
625
626 spin_lock(&dq_list_lock);
627 dqstats.drops++;
628 spin_unlock(&dq_list_lock);
629we_slept:
630 spin_lock(&dq_list_lock);
631 if (atomic_read(&dquot->dq_count) > 1) {
632 /* We have more than one user... nothing to do */
633 atomic_dec(&dquot->dq_count);
Jan Kara6362e4d2006-03-23 03:00:17 -0800634 /* Releasing dquot during quotaoff phase? */
Jan Karaf55abc02008-08-20 17:50:32 +0200635 if (!sb_has_quota_active(dquot->dq_sb, dquot->dq_type) &&
Jan Kara6362e4d2006-03-23 03:00:17 -0800636 atomic_read(&dquot->dq_count) == 1)
637 wake_up(&dquot->dq_wait_unused);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 spin_unlock(&dq_list_lock);
639 return;
640 }
641 /* Need to release dquot? */
642 if (test_bit(DQ_ACTIVE_B, &dquot->dq_flags) && dquot_dirty(dquot)) {
643 spin_unlock(&dq_list_lock);
644 /* Commit dquot before releasing */
Jan Karab48d3802008-07-25 01:46:49 -0700645 ret = dquot->dq_sb->dq_op->write_dquot(dquot);
646 if (ret < 0) {
647 printk(KERN_ERR "VFS: cannot write quota structure on "
648 "device %s (error %d). Quota may get out of "
649 "sync!\n", dquot->dq_sb->s_id, ret);
650 /*
651 * We clear dirty bit anyway, so that we avoid
652 * infinite loop here
653 */
654 spin_lock(&dq_list_lock);
655 clear_dquot_dirty(dquot);
656 spin_unlock(&dq_list_lock);
657 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 goto we_slept;
659 }
660 /* Clear flag in case dquot was inactive (something bad happened) */
661 clear_dquot_dirty(dquot);
662 if (test_bit(DQ_ACTIVE_B, &dquot->dq_flags)) {
663 spin_unlock(&dq_list_lock);
664 dquot->dq_sb->dq_op->release_dquot(dquot);
665 goto we_slept;
666 }
667 atomic_dec(&dquot->dq_count);
668#ifdef __DQUOT_PARANOIA
669 /* sanity check */
Eric Sesterhenn8abf6a42006-04-02 13:36:13 +0200670 BUG_ON(!list_empty(&dquot->dq_free));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671#endif
672 put_dquot_last(dquot);
673 spin_unlock(&dq_list_lock);
674}
675
Jan Kara7d9056b2008-11-25 15:31:32 +0100676struct dquot *dquot_alloc(struct super_block *sb, int type)
Jan Kara74f783a2008-08-19 14:51:22 +0200677{
678 return kmem_cache_zalloc(dquot_cachep, GFP_NOFS);
679}
Jan Kara7d9056b2008-11-25 15:31:32 +0100680EXPORT_SYMBOL(dquot_alloc);
Jan Kara74f783a2008-08-19 14:51:22 +0200681
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682static struct dquot *get_empty_dquot(struct super_block *sb, int type)
683{
684 struct dquot *dquot;
685
Jan Kara74f783a2008-08-19 14:51:22 +0200686 dquot = sb->dq_op->alloc_dquot(sb, type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 if(!dquot)
688 return NODQUOT;
689
Ingo Molnard3be9152006-03-23 03:00:29 -0800690 mutex_init(&dquot->dq_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 INIT_LIST_HEAD(&dquot->dq_free);
692 INIT_LIST_HEAD(&dquot->dq_inuse);
693 INIT_HLIST_NODE(&dquot->dq_hash);
694 INIT_LIST_HEAD(&dquot->dq_dirty);
Jan Kara6362e4d2006-03-23 03:00:17 -0800695 init_waitqueue_head(&dquot->dq_wait_unused);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 dquot->dq_sb = sb;
697 dquot->dq_type = type;
698 atomic_set(&dquot->dq_count, 1);
699
700 return dquot;
701}
702
703/*
704 * Get reference to dquot
Jan Karacc334122009-01-12 17:23:05 +0100705 *
706 * Locking is slightly tricky here. We are guarded from parallel quotaoff()
707 * destroying our dquot by:
708 * a) checking for quota flags under dq_list_lock and
709 * b) getting a reference to dquot before we release dq_list_lock
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 */
Jan Kara3d9ea252008-10-10 16:12:23 +0200711struct dquot *dqget(struct super_block *sb, unsigned int id, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712{
713 unsigned int hashent = hashfn(sb, id, type);
Jan Karacc334122009-01-12 17:23:05 +0100714 struct dquot *dquot = NODQUOT, *empty = NODQUOT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715
Jan Karaf55abc02008-08-20 17:50:32 +0200716 if (!sb_has_quota_active(sb, type))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 return NODQUOT;
718we_slept:
719 spin_lock(&dq_list_lock);
Jan Karacc334122009-01-12 17:23:05 +0100720 spin_lock(&dq_state_lock);
721 if (!sb_has_quota_active(sb, type)) {
722 spin_unlock(&dq_state_lock);
723 spin_unlock(&dq_list_lock);
724 goto out;
725 }
726 spin_unlock(&dq_state_lock);
727
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 if ((dquot = find_dquot(hashent, sb, id, type)) == NODQUOT) {
729 if (empty == NODQUOT) {
730 spin_unlock(&dq_list_lock);
731 if ((empty = get_empty_dquot(sb, type)) == NODQUOT)
732 schedule(); /* Try to wait for a moment... */
733 goto we_slept;
734 }
735 dquot = empty;
Jan Karacc334122009-01-12 17:23:05 +0100736 empty = NODQUOT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 dquot->dq_id = id;
738 /* all dquots go on the inuse_list */
739 put_inuse(dquot);
740 /* hash it first so it can be found */
741 insert_dquot_hash(dquot);
742 dqstats.lookups++;
743 spin_unlock(&dq_list_lock);
744 } else {
745 if (!atomic_read(&dquot->dq_count))
746 remove_free_dquot(dquot);
747 atomic_inc(&dquot->dq_count);
748 dqstats.cache_hits++;
749 dqstats.lookups++;
750 spin_unlock(&dq_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 }
752 /* Wait for dq_lock - after this we know that either dquot_release() is already
753 * finished or it will be canceled due to dq_count > 1 test */
754 wait_on_dquot(dquot);
755 /* Read the dquot and instantiate it (everything done only if needed) */
756 if (!test_bit(DQ_ACTIVE_B, &dquot->dq_flags) && sb->dq_op->acquire_dquot(dquot) < 0) {
757 dqput(dquot);
Jan Karacc334122009-01-12 17:23:05 +0100758 dquot = NODQUOT;
759 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 }
761#ifdef __DQUOT_PARANOIA
Eric Sesterhenn8abf6a42006-04-02 13:36:13 +0200762 BUG_ON(!dquot->dq_sb); /* Has somebody invalidated entry under us? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763#endif
Jan Karacc334122009-01-12 17:23:05 +0100764out:
765 if (empty)
766 do_destroy_dquot(empty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767
768 return dquot;
769}
770
771static int dqinit_needed(struct inode *inode, int type)
772{
773 int cnt;
774
775 if (IS_NOQUOTA(inode))
776 return 0;
777 if (type != -1)
778 return inode->i_dquot[type] == NODQUOT;
779 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
780 if (inode->i_dquot[cnt] == NODQUOT)
781 return 1;
782 return 0;
783}
784
Ingo Molnard3be9152006-03-23 03:00:29 -0800785/* This routine is guarded by dqonoff_mutex mutex */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786static void add_dquot_ref(struct super_block *sb, int type)
787{
Jan Kara941d2382008-02-06 01:37:36 -0800788 struct inode *inode, *old_inode = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789
Christoph Hellwigd003fb72007-02-12 00:51:58 -0800790 spin_lock(&inode_lock);
791 list_for_each_entry(inode, &sb->s_inodes, i_sb_list) {
792 if (!atomic_read(&inode->i_writecount))
793 continue;
794 if (!dqinit_needed(inode, type))
795 continue;
796 if (inode->i_state & (I_FREEING|I_WILL_FREE))
797 continue;
798
799 __iget(inode);
800 spin_unlock(&inode_lock);
801
Jan Kara941d2382008-02-06 01:37:36 -0800802 iput(old_inode);
Christoph Hellwigd003fb72007-02-12 00:51:58 -0800803 sb->dq_op->initialize(inode, type);
Jan Kara941d2382008-02-06 01:37:36 -0800804 /* We hold a reference to 'inode' so it couldn't have been
805 * removed from s_inodes list while we dropped the inode_lock.
806 * We cannot iput the inode now as we can be holding the last
807 * reference and we cannot iput it under inode_lock. So we
808 * keep the reference and iput it later. */
809 old_inode = inode;
810 spin_lock(&inode_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 }
Christoph Hellwigd003fb72007-02-12 00:51:58 -0800812 spin_unlock(&inode_lock);
Jan Kara941d2382008-02-06 01:37:36 -0800813 iput(old_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814}
815
816/* Return 0 if dqput() won't block (note that 1 doesn't necessarily mean blocking) */
817static inline int dqput_blocks(struct dquot *dquot)
818{
819 if (atomic_read(&dquot->dq_count) <= 1)
820 return 1;
821 return 0;
822}
823
824/* Remove references to dquots from inode - add dquot to list for freeing if needed */
825/* We can't race with anybody because we hold dqptr_sem for writing... */
Adrian Bunke5f00f42007-05-08 00:27:22 -0700826static int remove_inode_dquot_ref(struct inode *inode, int type,
827 struct list_head *tofree_head)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828{
829 struct dquot *dquot = inode->i_dquot[type];
830
831 inode->i_dquot[type] = NODQUOT;
832 if (dquot != NODQUOT) {
833 if (dqput_blocks(dquot)) {
834#ifdef __DQUOT_PARANOIA
835 if (atomic_read(&dquot->dq_count) != 1)
836 printk(KERN_WARNING "VFS: Adding dquot with dq_count %d to dispose list.\n", atomic_read(&dquot->dq_count));
837#endif
838 spin_lock(&dq_list_lock);
839 list_add(&dquot->dq_free, tofree_head); /* As dquot must have currently users it can't be on the free list... */
840 spin_unlock(&dq_list_lock);
841 return 1;
842 }
843 else
844 dqput(dquot); /* We have guaranteed we won't block */
845 }
846 return 0;
847}
848
849/* Free list of dquots - called from inode.c */
850/* dquots are removed from inodes, no new references can be got so we are the only ones holding reference */
851static void put_dquot_list(struct list_head *tofree_head)
852{
853 struct list_head *act_head;
854 struct dquot *dquot;
855
856 act_head = tofree_head->next;
857 /* So now we have dquots on the list... Just free them */
858 while (act_head != tofree_head) {
859 dquot = list_entry(act_head, struct dquot, dq_free);
860 act_head = act_head->next;
861 list_del_init(&dquot->dq_free); /* Remove dquot from the list so we won't have problems... */
862 dqput(dquot);
863 }
864}
865
Christoph Hellwigfb58b732007-02-12 00:51:57 -0800866static void remove_dquot_ref(struct super_block *sb, int type,
867 struct list_head *tofree_head)
868{
869 struct inode *inode;
870
871 spin_lock(&inode_lock);
872 list_for_each_entry(inode, &sb->s_inodes, i_sb_list) {
873 if (!IS_NOQUOTA(inode))
874 remove_inode_dquot_ref(inode, type, tofree_head);
875 }
876 spin_unlock(&inode_lock);
877}
878
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879/* Gather all references from inodes and drop them */
880static void drop_dquot_ref(struct super_block *sb, int type)
881{
882 LIST_HEAD(tofree_head);
883
Christoph Hellwigfb58b732007-02-12 00:51:57 -0800884 if (sb->dq_op) {
885 down_write(&sb_dqopt(sb)->dqptr_sem);
886 remove_dquot_ref(sb, type, &tofree_head);
887 up_write(&sb_dqopt(sb)->dqptr_sem);
888 put_dquot_list(&tofree_head);
889 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890}
891
Jan Kara12095462008-08-20 14:45:12 +0200892static inline void dquot_incr_inodes(struct dquot *dquot, qsize_t number)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893{
894 dquot->dq_dqb.dqb_curinodes += number;
895}
896
897static inline void dquot_incr_space(struct dquot *dquot, qsize_t number)
898{
899 dquot->dq_dqb.dqb_curspace += number;
900}
901
Mingming Caof18df222009-01-13 16:43:09 +0100902static inline void dquot_resv_space(struct dquot *dquot, qsize_t number)
903{
904 dquot->dq_dqb.dqb_rsvspace += number;
905}
906
Mingming Cao740d9dc2009-01-13 16:43:14 +0100907/*
908 * Claim reserved quota space
909 */
910static void dquot_claim_reserved_space(struct dquot *dquot,
911 qsize_t number)
912{
913 WARN_ON(dquot->dq_dqb.dqb_rsvspace < number);
914 dquot->dq_dqb.dqb_curspace += number;
915 dquot->dq_dqb.dqb_rsvspace -= number;
916}
917
918static inline
919void dquot_free_reserved_space(struct dquot *dquot, qsize_t number)
920{
921 dquot->dq_dqb.dqb_rsvspace -= number;
922}
923
Jan Kara12095462008-08-20 14:45:12 +0200924static inline void dquot_decr_inodes(struct dquot *dquot, qsize_t number)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925{
Jan Karadb49d2d2008-10-01 18:21:39 +0200926 if (sb_dqopt(dquot->dq_sb)->flags & DQUOT_NEGATIVE_USAGE ||
927 dquot->dq_dqb.dqb_curinodes >= number)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 dquot->dq_dqb.dqb_curinodes -= number;
929 else
930 dquot->dq_dqb.dqb_curinodes = 0;
931 if (dquot->dq_dqb.dqb_curinodes <= dquot->dq_dqb.dqb_isoftlimit)
932 dquot->dq_dqb.dqb_itime = (time_t) 0;
933 clear_bit(DQ_INODES_B, &dquot->dq_flags);
934}
935
936static inline void dquot_decr_space(struct dquot *dquot, qsize_t number)
937{
Jan Karadb49d2d2008-10-01 18:21:39 +0200938 if (sb_dqopt(dquot->dq_sb)->flags & DQUOT_NEGATIVE_USAGE ||
939 dquot->dq_dqb.dqb_curspace >= number)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 dquot->dq_dqb.dqb_curspace -= number;
941 else
942 dquot->dq_dqb.dqb_curspace = 0;
Jan Kara12095462008-08-20 14:45:12 +0200943 if (dquot->dq_dqb.dqb_curspace <= dquot->dq_dqb.dqb_bsoftlimit)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 dquot->dq_dqb.dqb_btime = (time_t) 0;
945 clear_bit(DQ_BLKS_B, &dquot->dq_flags);
946}
947
Jan Karac5254602007-12-22 14:03:25 -0800948static int warning_issued(struct dquot *dquot, const int warntype)
949{
950 int flag = (warntype == QUOTA_NL_BHARDWARN ||
951 warntype == QUOTA_NL_BSOFTLONGWARN) ? DQ_BLKS_B :
952 ((warntype == QUOTA_NL_IHARDWARN ||
953 warntype == QUOTA_NL_ISOFTLONGWARN) ? DQ_INODES_B : 0);
954
955 if (!flag)
956 return 0;
957 return test_and_set_bit(flag, &dquot->dq_flags);
958}
959
Jan Kara8e893462007-10-16 23:29:31 -0700960#ifdef CONFIG_PRINT_QUOTA_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961static int flag_print_warnings = 1;
962
963static inline int need_print_warning(struct dquot *dquot)
964{
965 if (!flag_print_warnings)
966 return 0;
967
968 switch (dquot->dq_type) {
969 case USRQUOTA:
David Howellsda9592e2008-11-14 10:39:05 +1100970 return current_fsuid() == dquot->dq_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 case GRPQUOTA:
972 return in_group_p(dquot->dq_id);
973 }
974 return 0;
975}
976
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977/* Print warning to user which exceeded quota */
Jan Karac5254602007-12-22 14:03:25 -0800978static void print_warning(struct dquot *dquot, const int warntype)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979{
980 char *msg = NULL;
Peter Zijlstra24ec8392006-12-08 02:36:04 -0800981 struct tty_struct *tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982
Jan Kara657d3bf2008-07-25 01:46:52 -0700983 if (warntype == QUOTA_NL_IHARDBELOW ||
984 warntype == QUOTA_NL_ISOFTBELOW ||
985 warntype == QUOTA_NL_BHARDBELOW ||
986 warntype == QUOTA_NL_BSOFTBELOW || !need_print_warning(dquot))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 return;
988
Peter Zijlstra24ec8392006-12-08 02:36:04 -0800989 tty = get_current_tty();
990 if (!tty)
Alan Cox452a00d2008-10-13 10:39:13 +0100991 return;
Peter Zijlstra24ec8392006-12-08 02:36:04 -0800992 tty_write_message(tty, dquot->dq_sb->s_id);
Jan Kara8e893462007-10-16 23:29:31 -0700993 if (warntype == QUOTA_NL_ISOFTWARN || warntype == QUOTA_NL_BSOFTWARN)
Peter Zijlstra24ec8392006-12-08 02:36:04 -0800994 tty_write_message(tty, ": warning, ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 else
Peter Zijlstra24ec8392006-12-08 02:36:04 -0800996 tty_write_message(tty, ": write failed, ");
997 tty_write_message(tty, quotatypes[dquot->dq_type]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 switch (warntype) {
Jan Kara8e893462007-10-16 23:29:31 -0700999 case QUOTA_NL_IHARDWARN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 msg = " file limit reached.\r\n";
1001 break;
Jan Kara8e893462007-10-16 23:29:31 -07001002 case QUOTA_NL_ISOFTLONGWARN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 msg = " file quota exceeded too long.\r\n";
1004 break;
Jan Kara8e893462007-10-16 23:29:31 -07001005 case QUOTA_NL_ISOFTWARN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 msg = " file quota exceeded.\r\n";
1007 break;
Jan Kara8e893462007-10-16 23:29:31 -07001008 case QUOTA_NL_BHARDWARN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 msg = " block limit reached.\r\n";
1010 break;
Jan Kara8e893462007-10-16 23:29:31 -07001011 case QUOTA_NL_BSOFTLONGWARN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 msg = " block quota exceeded too long.\r\n";
1013 break;
Jan Kara8e893462007-10-16 23:29:31 -07001014 case QUOTA_NL_BSOFTWARN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 msg = " block quota exceeded.\r\n";
1016 break;
1017 }
Peter Zijlstra24ec8392006-12-08 02:36:04 -08001018 tty_write_message(tty, msg);
Alan Cox452a00d2008-10-13 10:39:13 +01001019 tty_kref_put(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020}
Jan Kara8e893462007-10-16 23:29:31 -07001021#endif
1022
1023#ifdef CONFIG_QUOTA_NETLINK_INTERFACE
1024
Jan Kara8e893462007-10-16 23:29:31 -07001025/* Netlink family structure for quota */
1026static struct genl_family quota_genl_family = {
1027 .id = GENL_ID_GENERATE,
1028 .hdrsize = 0,
1029 .name = "VFS_DQUOT",
1030 .version = 1,
1031 .maxattr = QUOTA_NL_A_MAX,
1032};
1033
1034/* Send warning to userspace about user which exceeded quota */
1035static void send_warning(const struct dquot *dquot, const char warntype)
1036{
1037 static atomic_t seq;
1038 struct sk_buff *skb;
1039 void *msg_head;
1040 int ret;
Jan Kara22dd4832007-12-22 14:03:25 -08001041 int msg_size = 4 * nla_total_size(sizeof(u32)) +
1042 2 * nla_total_size(sizeof(u64));
Jan Kara8e893462007-10-16 23:29:31 -07001043
1044 /* We have to allocate using GFP_NOFS as we are called from a
1045 * filesystem performing write and thus further recursion into
1046 * the fs to free some data could cause deadlocks. */
Jan Kara22dd4832007-12-22 14:03:25 -08001047 skb = genlmsg_new(msg_size, GFP_NOFS);
Jan Kara8e893462007-10-16 23:29:31 -07001048 if (!skb) {
1049 printk(KERN_ERR
1050 "VFS: Not enough memory to send quota warning.\n");
1051 return;
1052 }
1053 msg_head = genlmsg_put(skb, 0, atomic_add_return(1, &seq),
1054 &quota_genl_family, 0, QUOTA_NL_C_WARNING);
1055 if (!msg_head) {
1056 printk(KERN_ERR
1057 "VFS: Cannot store netlink header in quota warning.\n");
1058 goto err_out;
1059 }
1060 ret = nla_put_u32(skb, QUOTA_NL_A_QTYPE, dquot->dq_type);
1061 if (ret)
1062 goto attr_err_out;
1063 ret = nla_put_u64(skb, QUOTA_NL_A_EXCESS_ID, dquot->dq_id);
1064 if (ret)
1065 goto attr_err_out;
1066 ret = nla_put_u32(skb, QUOTA_NL_A_WARNING, warntype);
1067 if (ret)
1068 goto attr_err_out;
1069 ret = nla_put_u32(skb, QUOTA_NL_A_DEV_MAJOR,
1070 MAJOR(dquot->dq_sb->s_dev));
1071 if (ret)
1072 goto attr_err_out;
1073 ret = nla_put_u32(skb, QUOTA_NL_A_DEV_MINOR,
1074 MINOR(dquot->dq_sb->s_dev));
1075 if (ret)
1076 goto attr_err_out;
David Howellsda9592e2008-11-14 10:39:05 +11001077 ret = nla_put_u64(skb, QUOTA_NL_A_CAUSED_ID, current_uid());
Jan Kara8e893462007-10-16 23:29:31 -07001078 if (ret)
1079 goto attr_err_out;
1080 genlmsg_end(skb, msg_head);
1081
1082 ret = genlmsg_multicast(skb, 0, quota_genl_family.id, GFP_NOFS);
1083 if (ret < 0 && ret != -ESRCH)
1084 printk(KERN_ERR
1085 "VFS: Failed to send notification message: %d\n", ret);
1086 return;
1087attr_err_out:
Jan Kara22dd4832007-12-22 14:03:25 -08001088 printk(KERN_ERR "VFS: Not enough space to compose quota message!\n");
Jan Kara8e893462007-10-16 23:29:31 -07001089err_out:
1090 kfree_skb(skb);
1091}
1092#endif
Mingming Caof18df222009-01-13 16:43:09 +01001093/*
1094 * Write warnings to the console and send warning messages over netlink.
1095 *
1096 * Note that this function can sleep.
1097 */
Jan Kara087ee8d2007-12-17 16:20:26 -08001098static inline void flush_warnings(struct dquot * const *dquots, char *warntype)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099{
1100 int i;
1101
1102 for (i = 0; i < MAXQUOTAS; i++)
Jan Karac5254602007-12-22 14:03:25 -08001103 if (dquots[i] != NODQUOT && warntype[i] != QUOTA_NL_NOWARN &&
1104 !warning_issued(dquots[i], warntype[i])) {
Jan Kara8e893462007-10-16 23:29:31 -07001105#ifdef CONFIG_PRINT_QUOTA_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 print_warning(dquots[i], warntype[i]);
Jan Kara8e893462007-10-16 23:29:31 -07001107#endif
1108#ifdef CONFIG_QUOTA_NETLINK_INTERFACE
1109 send_warning(dquots[i], warntype[i]);
1110#endif
1111 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112}
1113
1114static inline char ignore_hardlimit(struct dquot *dquot)
1115{
1116 struct mem_dqinfo *info = &sb_dqopt(dquot->dq_sb)->info[dquot->dq_type];
1117
1118 return capable(CAP_SYS_RESOURCE) &&
1119 (info->dqi_format->qf_fmt_id != QFMT_VFS_OLD || !(info->dqi_flags & V1_DQF_RSQUASH));
1120}
1121
1122/* needs dq_data_lock */
Jan Kara12095462008-08-20 14:45:12 +02001123static int check_idq(struct dquot *dquot, qsize_t inodes, char *warntype)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124{
Jan Kara8e893462007-10-16 23:29:31 -07001125 *warntype = QUOTA_NL_NOWARN;
Jan Karaf55abc02008-08-20 17:50:32 +02001126 if (!sb_has_quota_limits_enabled(dquot->dq_sb, dquot->dq_type) ||
1127 test_bit(DQ_FAKE_B, &dquot->dq_flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 return QUOTA_OK;
1129
1130 if (dquot->dq_dqb.dqb_ihardlimit &&
1131 (dquot->dq_dqb.dqb_curinodes + inodes) > dquot->dq_dqb.dqb_ihardlimit &&
1132 !ignore_hardlimit(dquot)) {
Jan Kara8e893462007-10-16 23:29:31 -07001133 *warntype = QUOTA_NL_IHARDWARN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 return NO_QUOTA;
1135 }
1136
1137 if (dquot->dq_dqb.dqb_isoftlimit &&
1138 (dquot->dq_dqb.dqb_curinodes + inodes) > dquot->dq_dqb.dqb_isoftlimit &&
1139 dquot->dq_dqb.dqb_itime && get_seconds() >= dquot->dq_dqb.dqb_itime &&
1140 !ignore_hardlimit(dquot)) {
Jan Kara8e893462007-10-16 23:29:31 -07001141 *warntype = QUOTA_NL_ISOFTLONGWARN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 return NO_QUOTA;
1143 }
1144
1145 if (dquot->dq_dqb.dqb_isoftlimit &&
1146 (dquot->dq_dqb.dqb_curinodes + inodes) > dquot->dq_dqb.dqb_isoftlimit &&
1147 dquot->dq_dqb.dqb_itime == 0) {
Jan Kara8e893462007-10-16 23:29:31 -07001148 *warntype = QUOTA_NL_ISOFTWARN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 dquot->dq_dqb.dqb_itime = get_seconds() + sb_dqopt(dquot->dq_sb)->info[dquot->dq_type].dqi_igrace;
1150 }
1151
1152 return QUOTA_OK;
1153}
1154
1155/* needs dq_data_lock */
1156static int check_bdq(struct dquot *dquot, qsize_t space, int prealloc, char *warntype)
1157{
Mingming Caof18df222009-01-13 16:43:09 +01001158 qsize_t tspace;
1159
Jan Kara8e893462007-10-16 23:29:31 -07001160 *warntype = QUOTA_NL_NOWARN;
Jan Karaf55abc02008-08-20 17:50:32 +02001161 if (!sb_has_quota_limits_enabled(dquot->dq_sb, dquot->dq_type) ||
1162 test_bit(DQ_FAKE_B, &dquot->dq_flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 return QUOTA_OK;
1164
Mingming Caof18df222009-01-13 16:43:09 +01001165 tspace = dquot->dq_dqb.dqb_curspace + dquot->dq_dqb.dqb_rsvspace
1166 + space;
1167
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 if (dquot->dq_dqb.dqb_bhardlimit &&
Mingming Caof18df222009-01-13 16:43:09 +01001169 tspace > dquot->dq_dqb.dqb_bhardlimit &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 !ignore_hardlimit(dquot)) {
1171 if (!prealloc)
Jan Kara8e893462007-10-16 23:29:31 -07001172 *warntype = QUOTA_NL_BHARDWARN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 return NO_QUOTA;
1174 }
1175
1176 if (dquot->dq_dqb.dqb_bsoftlimit &&
Mingming Caof18df222009-01-13 16:43:09 +01001177 tspace > dquot->dq_dqb.dqb_bsoftlimit &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 dquot->dq_dqb.dqb_btime && get_seconds() >= dquot->dq_dqb.dqb_btime &&
1179 !ignore_hardlimit(dquot)) {
1180 if (!prealloc)
Jan Kara8e893462007-10-16 23:29:31 -07001181 *warntype = QUOTA_NL_BSOFTLONGWARN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 return NO_QUOTA;
1183 }
1184
1185 if (dquot->dq_dqb.dqb_bsoftlimit &&
Mingming Caof18df222009-01-13 16:43:09 +01001186 tspace > dquot->dq_dqb.dqb_bsoftlimit &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 dquot->dq_dqb.dqb_btime == 0) {
1188 if (!prealloc) {
Jan Kara8e893462007-10-16 23:29:31 -07001189 *warntype = QUOTA_NL_BSOFTWARN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 dquot->dq_dqb.dqb_btime = get_seconds() + sb_dqopt(dquot->dq_sb)->info[dquot->dq_type].dqi_bgrace;
1191 }
1192 else
1193 /*
1194 * We don't allow preallocation to exceed softlimit so exceeding will
1195 * be always printed
1196 */
1197 return NO_QUOTA;
1198 }
1199
1200 return QUOTA_OK;
1201}
1202
Jan Kara12095462008-08-20 14:45:12 +02001203static int info_idq_free(struct dquot *dquot, qsize_t inodes)
Jan Kara657d3bf2008-07-25 01:46:52 -07001204{
1205 if (test_bit(DQ_FAKE_B, &dquot->dq_flags) ||
Jan Karaf55abc02008-08-20 17:50:32 +02001206 dquot->dq_dqb.dqb_curinodes <= dquot->dq_dqb.dqb_isoftlimit ||
1207 !sb_has_quota_limits_enabled(dquot->dq_sb, dquot->dq_type))
Jan Kara657d3bf2008-07-25 01:46:52 -07001208 return QUOTA_NL_NOWARN;
1209
1210 if (dquot->dq_dqb.dqb_curinodes - inodes <= dquot->dq_dqb.dqb_isoftlimit)
1211 return QUOTA_NL_ISOFTBELOW;
1212 if (dquot->dq_dqb.dqb_curinodes >= dquot->dq_dqb.dqb_ihardlimit &&
1213 dquot->dq_dqb.dqb_curinodes - inodes < dquot->dq_dqb.dqb_ihardlimit)
1214 return QUOTA_NL_IHARDBELOW;
1215 return QUOTA_NL_NOWARN;
1216}
1217
1218static int info_bdq_free(struct dquot *dquot, qsize_t space)
1219{
1220 if (test_bit(DQ_FAKE_B, &dquot->dq_flags) ||
Jan Kara12095462008-08-20 14:45:12 +02001221 dquot->dq_dqb.dqb_curspace <= dquot->dq_dqb.dqb_bsoftlimit)
Jan Kara657d3bf2008-07-25 01:46:52 -07001222 return QUOTA_NL_NOWARN;
1223
Jan Kara12095462008-08-20 14:45:12 +02001224 if (dquot->dq_dqb.dqb_curspace - space <= dquot->dq_dqb.dqb_bsoftlimit)
Jan Kara657d3bf2008-07-25 01:46:52 -07001225 return QUOTA_NL_BSOFTBELOW;
Jan Kara12095462008-08-20 14:45:12 +02001226 if (dquot->dq_dqb.dqb_curspace >= dquot->dq_dqb.dqb_bhardlimit &&
1227 dquot->dq_dqb.dqb_curspace - space < dquot->dq_dqb.dqb_bhardlimit)
Jan Kara657d3bf2008-07-25 01:46:52 -07001228 return QUOTA_NL_BHARDBELOW;
1229 return QUOTA_NL_NOWARN;
1230}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231/*
1232 * Initialize quota pointers in inode
Jan Karacc334122009-01-12 17:23:05 +01001233 * We do things in a bit complicated way but by that we avoid calling
1234 * dqget() and thus filesystem callbacks under dqptr_sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 */
1236int dquot_initialize(struct inode *inode, int type)
1237{
1238 unsigned int id = 0;
1239 int cnt, ret = 0;
Jan Karacc334122009-01-12 17:23:05 +01001240 struct dquot *got[MAXQUOTAS] = { NODQUOT, NODQUOT };
1241 struct super_block *sb = inode->i_sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242
Ingo Molnard3be9152006-03-23 03:00:29 -08001243 /* First test before acquiring mutex - solves deadlocks when we
1244 * re-enter the quota code and are already holding the mutex */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 if (IS_NOQUOTA(inode))
1246 return 0;
Jan Karacc334122009-01-12 17:23:05 +01001247
1248 /* First get references to structures we might need. */
1249 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1250 if (type != -1 && cnt != type)
1251 continue;
1252 switch (cnt) {
1253 case USRQUOTA:
1254 id = inode->i_uid;
1255 break;
1256 case GRPQUOTA:
1257 id = inode->i_gid;
1258 break;
1259 }
1260 got[cnt] = dqget(sb, id, cnt);
1261 }
1262
1263 down_write(&sb_dqopt(sb)->dqptr_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 /* Having dqptr_sem we know NOQUOTA flags can't be altered... */
1265 if (IS_NOQUOTA(inode))
1266 goto out_err;
1267 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1268 if (type != -1 && cnt != type)
1269 continue;
Jan Karacc334122009-01-12 17:23:05 +01001270 /* Avoid races with quotaoff() */
1271 if (!sb_has_quota_active(sb, cnt))
1272 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 if (inode->i_dquot[cnt] == NODQUOT) {
Jan Karacc334122009-01-12 17:23:05 +01001274 inode->i_dquot[cnt] = got[cnt];
1275 got[cnt] = NODQUOT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 }
1277 }
1278out_err:
Jan Karacc334122009-01-12 17:23:05 +01001279 up_write(&sb_dqopt(sb)->dqptr_sem);
1280 /* Drop unused references */
1281 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1282 dqput(got[cnt]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 return ret;
1284}
1285
1286/*
1287 * Release all quotas referenced by inode
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 */
Jan Kara3d9ea252008-10-10 16:12:23 +02001289int dquot_drop(struct inode *inode)
1290{
Jan Karacc334122009-01-12 17:23:05 +01001291 int cnt;
1292 struct dquot *put[MAXQUOTAS];
1293
Jan Kara3d9ea252008-10-10 16:12:23 +02001294 down_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
Jan Karacc334122009-01-12 17:23:05 +01001295 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1296 put[cnt] = inode->i_dquot[cnt];
1297 inode->i_dquot[cnt] = NODQUOT;
1298 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 up_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
Jan Karacc334122009-01-12 17:23:05 +01001300
1301 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1302 dqput(put[cnt]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 return 0;
1304}
1305
Jan Karab85f4b82008-07-25 01:46:50 -07001306/* Wrapper to remove references to quota structures from inode */
1307void vfs_dq_drop(struct inode *inode)
1308{
1309 /* Here we can get arbitrary inode from clear_inode() so we have
1310 * to be careful. OTOH we don't need locking as quota operations
1311 * are allowed to change only at mount time */
1312 if (!IS_NOQUOTA(inode) && inode->i_sb && inode->i_sb->dq_op
1313 && inode->i_sb->dq_op->drop) {
1314 int cnt;
1315 /* Test before calling to rule out calls from proc and such
1316 * where we are not allowed to block. Note that this is
1317 * actually reliable test even without the lock - the caller
1318 * must assure that nobody can come after the DQUOT_DROP and
1319 * add quota pointers back anyway */
1320 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1321 if (inode->i_dquot[cnt] != NODQUOT)
1322 break;
1323 if (cnt < MAXQUOTAS)
1324 inode->i_sb->dq_op->drop(inode);
1325 }
1326}
1327
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328/*
1329 * Following four functions update i_blocks+i_bytes fields and
1330 * quota information (together with appropriate checks)
1331 * NOTE: We absolutely rely on the fact that caller dirties
1332 * the inode (usually macros in quotaops.h care about this) and
1333 * holds a handle for the current transaction so that dquot write and
1334 * inode write go into the same transaction.
1335 */
1336
1337/*
1338 * This operation can block, but only after everything is updated
1339 */
Mingming Caof18df222009-01-13 16:43:09 +01001340int __dquot_alloc_space(struct inode *inode, qsize_t number,
1341 int warn, int reserve)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342{
Mingming Caof18df222009-01-13 16:43:09 +01001343 int cnt, ret = QUOTA_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 char warntype[MAXQUOTAS];
1345
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
Jan Kara8e893462007-10-16 23:29:31 -07001347 warntype[cnt] = QUOTA_NL_NOWARN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 spin_lock(&dq_data_lock);
1350 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1351 if (inode->i_dquot[cnt] == NODQUOT)
1352 continue;
Mingming Caof18df222009-01-13 16:43:09 +01001353 if (check_bdq(inode->i_dquot[cnt], number, warn, warntype+cnt)
1354 == NO_QUOTA) {
1355 ret = NO_QUOTA;
1356 goto out_unlock;
1357 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 }
1359 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1360 if (inode->i_dquot[cnt] == NODQUOT)
1361 continue;
Mingming Caof18df222009-01-13 16:43:09 +01001362 if (reserve)
1363 dquot_resv_space(inode->i_dquot[cnt], number);
1364 else
1365 dquot_incr_space(inode->i_dquot[cnt], number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 }
Mingming Caof18df222009-01-13 16:43:09 +01001367 if (!reserve)
1368 inode_add_bytes(inode, number);
1369out_unlock:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370 spin_unlock(&dq_data_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 flush_warnings(inode->i_dquot, warntype);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372 return ret;
1373}
1374
Mingming Caof18df222009-01-13 16:43:09 +01001375int dquot_alloc_space(struct inode *inode, qsize_t number, int warn)
1376{
1377 int cnt, ret = QUOTA_OK;
1378
1379 /*
1380 * First test before acquiring mutex - solves deadlocks when we
1381 * re-enter the quota code and are already holding the mutex
1382 */
1383 if (IS_NOQUOTA(inode)) {
1384 inode_add_bytes(inode, number);
1385 goto out;
1386 }
1387
1388 down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1389 if (IS_NOQUOTA(inode)) {
1390 inode_add_bytes(inode, number);
1391 goto out_unlock;
1392 }
1393
1394 ret = __dquot_alloc_space(inode, number, warn, 0);
1395 if (ret == NO_QUOTA)
1396 goto out_unlock;
1397
1398 /* Dirtify all the dquots - this can block when journalling */
1399 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1400 if (inode->i_dquot[cnt])
1401 mark_dquot_dirty(inode->i_dquot[cnt]);
1402out_unlock:
1403 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1404out:
1405 return ret;
1406}
1407
1408int dquot_reserve_space(struct inode *inode, qsize_t number, int warn)
1409{
1410 int ret = QUOTA_OK;
1411
1412 if (IS_NOQUOTA(inode))
1413 goto out;
1414
1415 down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1416 if (IS_NOQUOTA(inode))
1417 goto out_unlock;
1418
1419 ret = __dquot_alloc_space(inode, number, warn, 1);
1420out_unlock:
1421 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1422out:
1423 return ret;
1424}
1425EXPORT_SYMBOL(dquot_reserve_space);
1426
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427/*
1428 * This operation can block, but only after everything is updated
1429 */
Jan Kara12095462008-08-20 14:45:12 +02001430int dquot_alloc_inode(const struct inode *inode, qsize_t number)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431{
1432 int cnt, ret = NO_QUOTA;
1433 char warntype[MAXQUOTAS];
1434
Ingo Molnard3be9152006-03-23 03:00:29 -08001435 /* First test before acquiring mutex - solves deadlocks when we
1436 * re-enter the quota code and are already holding the mutex */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437 if (IS_NOQUOTA(inode))
1438 return QUOTA_OK;
1439 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
Jan Kara8e893462007-10-16 23:29:31 -07001440 warntype[cnt] = QUOTA_NL_NOWARN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1442 if (IS_NOQUOTA(inode)) {
1443 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1444 return QUOTA_OK;
1445 }
1446 spin_lock(&dq_data_lock);
1447 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1448 if (inode->i_dquot[cnt] == NODQUOT)
1449 continue;
1450 if (check_idq(inode->i_dquot[cnt], number, warntype+cnt) == NO_QUOTA)
1451 goto warn_put_all;
1452 }
1453
1454 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1455 if (inode->i_dquot[cnt] == NODQUOT)
1456 continue;
1457 dquot_incr_inodes(inode->i_dquot[cnt], number);
1458 }
1459 ret = QUOTA_OK;
1460warn_put_all:
1461 spin_unlock(&dq_data_lock);
1462 if (ret == QUOTA_OK)
1463 /* Dirtify all the dquots - this can block when journalling */
1464 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1465 if (inode->i_dquot[cnt])
1466 mark_dquot_dirty(inode->i_dquot[cnt]);
Jan Kara087ee8d2007-12-17 16:20:26 -08001467 flush_warnings(inode->i_dquot, warntype);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1469 return ret;
1470}
1471
Mingming Cao740d9dc2009-01-13 16:43:14 +01001472int dquot_claim_space(struct inode *inode, qsize_t number)
1473{
1474 int cnt;
1475 int ret = QUOTA_OK;
1476
1477 if (IS_NOQUOTA(inode)) {
1478 inode_add_bytes(inode, number);
1479 goto out;
1480 }
1481
1482 down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1483 if (IS_NOQUOTA(inode)) {
1484 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1485 inode_add_bytes(inode, number);
1486 goto out;
1487 }
1488
1489 spin_lock(&dq_data_lock);
1490 /* Claim reserved quotas to allocated quotas */
1491 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1492 if (inode->i_dquot[cnt] != NODQUOT)
1493 dquot_claim_reserved_space(inode->i_dquot[cnt],
1494 number);
1495 }
1496 /* Update inode bytes */
1497 inode_add_bytes(inode, number);
1498 spin_unlock(&dq_data_lock);
1499 /* Dirtify all the dquots - this can block when journalling */
1500 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1501 if (inode->i_dquot[cnt])
1502 mark_dquot_dirty(inode->i_dquot[cnt]);
1503 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1504out:
1505 return ret;
1506}
1507EXPORT_SYMBOL(dquot_claim_space);
1508
1509/*
1510 * Release reserved quota space
1511 */
1512void dquot_release_reserved_space(struct inode *inode, qsize_t number)
1513{
1514 int cnt;
1515
1516 if (IS_NOQUOTA(inode))
1517 goto out;
1518
1519 down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1520 if (IS_NOQUOTA(inode))
1521 goto out_unlock;
1522
1523 spin_lock(&dq_data_lock);
1524 /* Release reserved dquots */
1525 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1526 if (inode->i_dquot[cnt] != NODQUOT)
1527 dquot_free_reserved_space(inode->i_dquot[cnt], number);
1528 }
1529 spin_unlock(&dq_data_lock);
1530
1531out_unlock:
1532 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1533out:
1534 return;
1535}
1536EXPORT_SYMBOL(dquot_release_reserved_space);
1537
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538/*
1539 * This operation can block, but only after everything is updated
1540 */
1541int dquot_free_space(struct inode *inode, qsize_t number)
1542{
1543 unsigned int cnt;
Jan Kara657d3bf2008-07-25 01:46:52 -07001544 char warntype[MAXQUOTAS];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545
Ingo Molnard3be9152006-03-23 03:00:29 -08001546 /* First test before acquiring mutex - solves deadlocks when we
1547 * re-enter the quota code and are already holding the mutex */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548 if (IS_NOQUOTA(inode)) {
1549out_sub:
1550 inode_sub_bytes(inode, number);
1551 return QUOTA_OK;
1552 }
Jan Kara657d3bf2008-07-25 01:46:52 -07001553
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554 down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1555 /* Now recheck reliably when holding dqptr_sem */
1556 if (IS_NOQUOTA(inode)) {
1557 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1558 goto out_sub;
1559 }
1560 spin_lock(&dq_data_lock);
1561 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1562 if (inode->i_dquot[cnt] == NODQUOT)
1563 continue;
Jan Kara657d3bf2008-07-25 01:46:52 -07001564 warntype[cnt] = info_bdq_free(inode->i_dquot[cnt], number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565 dquot_decr_space(inode->i_dquot[cnt], number);
1566 }
1567 inode_sub_bytes(inode, number);
1568 spin_unlock(&dq_data_lock);
1569 /* Dirtify all the dquots - this can block when journalling */
1570 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1571 if (inode->i_dquot[cnt])
1572 mark_dquot_dirty(inode->i_dquot[cnt]);
Jan Kara657d3bf2008-07-25 01:46:52 -07001573 flush_warnings(inode->i_dquot, warntype);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1575 return QUOTA_OK;
1576}
1577
1578/*
1579 * This operation can block, but only after everything is updated
1580 */
Jan Kara12095462008-08-20 14:45:12 +02001581int dquot_free_inode(const struct inode *inode, qsize_t number)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582{
1583 unsigned int cnt;
Jan Kara657d3bf2008-07-25 01:46:52 -07001584 char warntype[MAXQUOTAS];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585
Ingo Molnard3be9152006-03-23 03:00:29 -08001586 /* First test before acquiring mutex - solves deadlocks when we
1587 * re-enter the quota code and are already holding the mutex */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588 if (IS_NOQUOTA(inode))
1589 return QUOTA_OK;
Jan Kara657d3bf2008-07-25 01:46:52 -07001590
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591 down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1592 /* Now recheck reliably when holding dqptr_sem */
1593 if (IS_NOQUOTA(inode)) {
1594 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1595 return QUOTA_OK;
1596 }
1597 spin_lock(&dq_data_lock);
1598 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1599 if (inode->i_dquot[cnt] == NODQUOT)
1600 continue;
Jan Kara657d3bf2008-07-25 01:46:52 -07001601 warntype[cnt] = info_idq_free(inode->i_dquot[cnt], number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602 dquot_decr_inodes(inode->i_dquot[cnt], number);
1603 }
1604 spin_unlock(&dq_data_lock);
1605 /* Dirtify all the dquots - this can block when journalling */
1606 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1607 if (inode->i_dquot[cnt])
1608 mark_dquot_dirty(inode->i_dquot[cnt]);
Jan Kara657d3bf2008-07-25 01:46:52 -07001609 flush_warnings(inode->i_dquot, warntype);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1611 return QUOTA_OK;
1612}
1613
1614/*
Mingming Cao740d9dc2009-01-13 16:43:14 +01001615 * call back function, get reserved quota space from underlying fs
1616 */
1617qsize_t dquot_get_reserved_space(struct inode *inode)
1618{
1619 qsize_t reserved_space = 0;
1620
1621 if (sb_any_quota_active(inode->i_sb) &&
1622 inode->i_sb->dq_op->get_reserved_space)
1623 reserved_space = inode->i_sb->dq_op->get_reserved_space(inode);
1624 return reserved_space;
1625}
1626
1627/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628 * Transfer the number of inode and blocks from one diskquota to an other.
1629 *
1630 * This operation can block, but only after everything is updated
1631 * A transaction must be started when entering this function.
1632 */
1633int dquot_transfer(struct inode *inode, struct iattr *iattr)
1634{
Mingming Cao740d9dc2009-01-13 16:43:14 +01001635 qsize_t space, cur_space;
1636 qsize_t rsv_space = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637 struct dquot *transfer_from[MAXQUOTAS];
1638 struct dquot *transfer_to[MAXQUOTAS];
Jan Karacc334122009-01-12 17:23:05 +01001639 int cnt, ret = QUOTA_OK;
1640 int chuid = iattr->ia_valid & ATTR_UID && inode->i_uid != iattr->ia_uid,
1641 chgid = iattr->ia_valid & ATTR_GID && inode->i_gid != iattr->ia_gid;
Jan Kara657d3bf2008-07-25 01:46:52 -07001642 char warntype_to[MAXQUOTAS];
1643 char warntype_from_inodes[MAXQUOTAS], warntype_from_space[MAXQUOTAS];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644
Ingo Molnard3be9152006-03-23 03:00:29 -08001645 /* First test before acquiring mutex - solves deadlocks when we
1646 * re-enter the quota code and are already holding the mutex */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647 if (IS_NOQUOTA(inode))
1648 return QUOTA_OK;
Jan Karacc334122009-01-12 17:23:05 +01001649 /* Initialize the arrays */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
Jan Karacc334122009-01-12 17:23:05 +01001651 transfer_from[cnt] = NODQUOT;
1652 transfer_to[cnt] = NODQUOT;
Jan Kara657d3bf2008-07-25 01:46:52 -07001653 warntype_to[cnt] = QUOTA_NL_NOWARN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654 switch (cnt) {
1655 case USRQUOTA:
1656 if (!chuid)
1657 continue;
1658 transfer_to[cnt] = dqget(inode->i_sb, iattr->ia_uid, cnt);
1659 break;
1660 case GRPQUOTA:
1661 if (!chgid)
1662 continue;
1663 transfer_to[cnt] = dqget(inode->i_sb, iattr->ia_gid, cnt);
1664 break;
1665 }
1666 }
Jan Karacc334122009-01-12 17:23:05 +01001667
1668 down_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
1669 /* Now recheck reliably when holding dqptr_sem */
1670 if (IS_NOQUOTA(inode)) { /* File without quota accounting? */
1671 up_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
1672 goto put_all;
1673 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674 spin_lock(&dq_data_lock);
Mingming Cao740d9dc2009-01-13 16:43:14 +01001675 cur_space = inode_get_bytes(inode);
1676 rsv_space = dquot_get_reserved_space(inode);
1677 space = cur_space + rsv_space;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678 /* Build the transfer_from list and check the limits */
1679 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1680 if (transfer_to[cnt] == NODQUOT)
1681 continue;
1682 transfer_from[cnt] = inode->i_dquot[cnt];
Jan Kara657d3bf2008-07-25 01:46:52 -07001683 if (check_idq(transfer_to[cnt], 1, warntype_to + cnt) ==
1684 NO_QUOTA || check_bdq(transfer_to[cnt], space, 0,
1685 warntype_to + cnt) == NO_QUOTA)
Jan Karacc334122009-01-12 17:23:05 +01001686 goto over_quota;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687 }
1688
1689 /*
1690 * Finally perform the needed transfer from transfer_from to transfer_to
1691 */
1692 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1693 /*
1694 * Skip changes for same uid or gid or for turned off quota-type.
1695 */
1696 if (transfer_to[cnt] == NODQUOT)
1697 continue;
1698
1699 /* Due to IO error we might not have transfer_from[] structure */
1700 if (transfer_from[cnt]) {
Jan Kara657d3bf2008-07-25 01:46:52 -07001701 warntype_from_inodes[cnt] =
1702 info_idq_free(transfer_from[cnt], 1);
1703 warntype_from_space[cnt] =
1704 info_bdq_free(transfer_from[cnt], space);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705 dquot_decr_inodes(transfer_from[cnt], 1);
Mingming Cao740d9dc2009-01-13 16:43:14 +01001706 dquot_decr_space(transfer_from[cnt], cur_space);
1707 dquot_free_reserved_space(transfer_from[cnt],
1708 rsv_space);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709 }
1710
1711 dquot_incr_inodes(transfer_to[cnt], 1);
Mingming Cao740d9dc2009-01-13 16:43:14 +01001712 dquot_incr_space(transfer_to[cnt], cur_space);
1713 dquot_resv_space(transfer_to[cnt], rsv_space);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714
1715 inode->i_dquot[cnt] = transfer_to[cnt];
1716 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717 spin_unlock(&dq_data_lock);
Jan Karacc334122009-01-12 17:23:05 +01001718 up_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
1719
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720 /* Dirtify all the dquots - this can block when journalling */
1721 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1722 if (transfer_from[cnt])
1723 mark_dquot_dirty(transfer_from[cnt]);
Jan Karacc334122009-01-12 17:23:05 +01001724 if (transfer_to[cnt]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725 mark_dquot_dirty(transfer_to[cnt]);
Jan Karacc334122009-01-12 17:23:05 +01001726 /* The reference we got is transferred to the inode */
1727 transfer_to[cnt] = NODQUOT;
1728 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729 }
Jan Karacc334122009-01-12 17:23:05 +01001730warn_put_all:
Jan Kara657d3bf2008-07-25 01:46:52 -07001731 flush_warnings(transfer_to, warntype_to);
1732 flush_warnings(transfer_from, warntype_from_inodes);
1733 flush_warnings(transfer_from, warntype_from_space);
Jan Karacc334122009-01-12 17:23:05 +01001734put_all:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
Jan Karacc334122009-01-12 17:23:05 +01001736 dqput(transfer_from[cnt]);
1737 dqput(transfer_to[cnt]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739 return ret;
Jan Karacc334122009-01-12 17:23:05 +01001740over_quota:
1741 spin_unlock(&dq_data_lock);
1742 up_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
1743 /* Clear dquot pointers we don't want to dqput() */
1744 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1745 transfer_from[cnt] = NODQUOT;
1746 ret = NO_QUOTA;
1747 goto warn_put_all;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748}
1749
Jan Karab85f4b82008-07-25 01:46:50 -07001750/* Wrapper for transferring ownership of an inode */
1751int vfs_dq_transfer(struct inode *inode, struct iattr *iattr)
1752{
Jan Karaf55abc02008-08-20 17:50:32 +02001753 if (sb_any_quota_active(inode->i_sb) && !IS_NOQUOTA(inode)) {
Jan Karab85f4b82008-07-25 01:46:50 -07001754 vfs_dq_init(inode);
1755 if (inode->i_sb->dq_op->transfer(inode, iattr) == NO_QUOTA)
1756 return 1;
1757 }
1758 return 0;
1759}
1760
1761
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762/*
1763 * Write info of quota file to disk
1764 */
1765int dquot_commit_info(struct super_block *sb, int type)
1766{
1767 int ret;
1768 struct quota_info *dqopt = sb_dqopt(sb);
1769
Ingo Molnard3be9152006-03-23 03:00:29 -08001770 mutex_lock(&dqopt->dqio_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771 ret = dqopt->ops[type]->write_file_info(sb, type);
Ingo Molnard3be9152006-03-23 03:00:29 -08001772 mutex_unlock(&dqopt->dqio_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773 return ret;
1774}
1775
1776/*
1777 * Definitions of diskquota operations.
1778 */
1779struct dquot_operations dquot_operations = {
1780 .initialize = dquot_initialize,
1781 .drop = dquot_drop,
1782 .alloc_space = dquot_alloc_space,
1783 .alloc_inode = dquot_alloc_inode,
1784 .free_space = dquot_free_space,
1785 .free_inode = dquot_free_inode,
1786 .transfer = dquot_transfer,
1787 .write_dquot = dquot_commit,
1788 .acquire_dquot = dquot_acquire,
1789 .release_dquot = dquot_release,
1790 .mark_dirty = dquot_mark_dquot_dirty,
Jan Kara74f783a2008-08-19 14:51:22 +02001791 .write_info = dquot_commit_info,
1792 .alloc_dquot = dquot_alloc,
1793 .destroy_dquot = dquot_destroy,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794};
1795
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796/*
1797 * Turn quota off on a device. type == -1 ==> quotaoff for all types (umount)
1798 */
Jan Karaf55abc02008-08-20 17:50:32 +02001799int vfs_quota_disable(struct super_block *sb, int type, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800{
Jan Kara0ff5af82008-04-28 02:14:33 -07001801 int cnt, ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802 struct quota_info *dqopt = sb_dqopt(sb);
1803 struct inode *toputinode[MAXQUOTAS];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804
Jan Karaf55abc02008-08-20 17:50:32 +02001805 /* Cannot turn off usage accounting without turning off limits, or
1806 * suspend quotas and simultaneously turn quotas off. */
1807 if ((flags & DQUOT_USAGE_ENABLED && !(flags & DQUOT_LIMITS_ENABLED))
1808 || (flags & DQUOT_SUSPENDED && flags & (DQUOT_LIMITS_ENABLED |
1809 DQUOT_USAGE_ENABLED)))
1810 return -EINVAL;
1811
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812 /* We need to serialize quota_off() for device */
Ingo Molnard3be9152006-03-23 03:00:29 -08001813 mutex_lock(&dqopt->dqonoff_mutex);
Jan Kara9377abd2008-05-12 14:02:08 -07001814
1815 /*
1816 * Skip everything if there's nothing to do. We have to do this because
1817 * sometimes we are called when fill_super() failed and calling
1818 * sync_fs() in such cases does no good.
1819 */
Jan Karaf55abc02008-08-20 17:50:32 +02001820 if (!sb_any_quota_loaded(sb)) {
Jan Kara9377abd2008-05-12 14:02:08 -07001821 mutex_unlock(&dqopt->dqonoff_mutex);
1822 return 0;
1823 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1825 toputinode[cnt] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826 if (type != -1 && cnt != type)
1827 continue;
Jan Karaf55abc02008-08-20 17:50:32 +02001828 if (!sb_has_quota_loaded(sb, cnt))
Jan Kara0ff5af82008-04-28 02:14:33 -07001829 continue;
Jan Karaf55abc02008-08-20 17:50:32 +02001830
1831 if (flags & DQUOT_SUSPENDED) {
Jan Karacc334122009-01-12 17:23:05 +01001832 spin_lock(&dq_state_lock);
Jan Karaf55abc02008-08-20 17:50:32 +02001833 dqopt->flags |=
1834 dquot_state_flag(DQUOT_SUSPENDED, cnt);
Jan Karacc334122009-01-12 17:23:05 +01001835 spin_unlock(&dq_state_lock);
Jan Karaf55abc02008-08-20 17:50:32 +02001836 } else {
Jan Karacc334122009-01-12 17:23:05 +01001837 spin_lock(&dq_state_lock);
Jan Karaf55abc02008-08-20 17:50:32 +02001838 dqopt->flags &= ~dquot_state_flag(flags, cnt);
1839 /* Turning off suspended quotas? */
1840 if (!sb_has_quota_loaded(sb, cnt) &&
1841 sb_has_quota_suspended(sb, cnt)) {
1842 dqopt->flags &= ~dquot_state_flag(
1843 DQUOT_SUSPENDED, cnt);
Jan Karacc334122009-01-12 17:23:05 +01001844 spin_unlock(&dq_state_lock);
Jan Karaf55abc02008-08-20 17:50:32 +02001845 iput(dqopt->files[cnt]);
1846 dqopt->files[cnt] = NULL;
1847 continue;
1848 }
Jan Karacc334122009-01-12 17:23:05 +01001849 spin_unlock(&dq_state_lock);
Jan Kara0ff5af82008-04-28 02:14:33 -07001850 }
Jan Karaf55abc02008-08-20 17:50:32 +02001851
1852 /* We still have to keep quota loaded? */
1853 if (sb_has_quota_loaded(sb, cnt) && !(flags & DQUOT_SUSPENDED))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855
1856 /* Note: these are blocking operations */
1857 drop_dquot_ref(sb, cnt);
1858 invalidate_dquots(sb, cnt);
1859 /*
1860 * Now all dquots should be invalidated, all writes done so we should be only
1861 * users of the info. No locks needed.
1862 */
1863 if (info_dirty(&dqopt->info[cnt]))
1864 sb->dq_op->write_info(sb, cnt);
1865 if (dqopt->ops[cnt]->free_file_info)
1866 dqopt->ops[cnt]->free_file_info(sb, cnt);
1867 put_quota_format(dqopt->info[cnt].dqi_format);
1868
1869 toputinode[cnt] = dqopt->files[cnt];
Jan Karaf55abc02008-08-20 17:50:32 +02001870 if (!sb_has_quota_loaded(sb, cnt))
Jan Kara0ff5af82008-04-28 02:14:33 -07001871 dqopt->files[cnt] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872 dqopt->info[cnt].dqi_flags = 0;
1873 dqopt->info[cnt].dqi_igrace = 0;
1874 dqopt->info[cnt].dqi_bgrace = 0;
1875 dqopt->ops[cnt] = NULL;
1876 }
Ingo Molnard3be9152006-03-23 03:00:29 -08001877 mutex_unlock(&dqopt->dqonoff_mutex);
Jan Karaca785ec2008-09-30 17:53:37 +02001878
1879 /* Skip syncing and setting flags if quota files are hidden */
1880 if (dqopt->flags & DQUOT_QUOTA_SYS_FILE)
1881 goto put_inodes;
1882
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883 /* Sync the superblock so that buffers with quota data are written to
Al Viro7b7b1ac2005-11-07 17:13:39 -05001884 * disk (and so userspace sees correct data afterwards). */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885 if (sb->s_op->sync_fs)
1886 sb->s_op->sync_fs(sb, 1);
1887 sync_blockdev(sb->s_bdev);
1888 /* Now the quota files are just ordinary files and we can set the
1889 * inode flags back. Moreover we discard the pagecache so that
1890 * userspace sees the writes we did bypassing the pagecache. We
1891 * must also discard the blockdev buffers so that we see the
1892 * changes done by userspace on the next quotaon() */
1893 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1894 if (toputinode[cnt]) {
Ingo Molnard3be9152006-03-23 03:00:29 -08001895 mutex_lock(&dqopt->dqonoff_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896 /* If quota was reenabled in the meantime, we have
1897 * nothing to do */
Jan Karaf55abc02008-08-20 17:50:32 +02001898 if (!sb_has_quota_loaded(sb, cnt)) {
Jan Kara79254092007-05-16 22:11:19 -07001899 mutex_lock_nested(&toputinode[cnt]->i_mutex, I_MUTEX_QUOTA);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900 toputinode[cnt]->i_flags &= ~(S_IMMUTABLE |
1901 S_NOATIME | S_NOQUOTA);
1902 truncate_inode_pages(&toputinode[cnt]->i_data, 0);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001903 mutex_unlock(&toputinode[cnt]->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904 mark_inode_dirty(toputinode[cnt]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905 }
Ingo Molnard3be9152006-03-23 03:00:29 -08001906 mutex_unlock(&dqopt->dqonoff_mutex);
Jan Karaca785ec2008-09-30 17:53:37 +02001907 }
1908 if (sb->s_bdev)
1909 invalidate_bdev(sb->s_bdev);
1910put_inodes:
1911 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1912 if (toputinode[cnt]) {
Jan Kara0ff5af82008-04-28 02:14:33 -07001913 /* On remount RO, we keep the inode pointer so that we
Jan Karaf55abc02008-08-20 17:50:32 +02001914 * can reenable quota on the subsequent remount RW. We
1915 * have to check 'flags' variable and not use sb_has_
1916 * function because another quotaon / quotaoff could
1917 * change global state before we got here. We refuse
1918 * to suspend quotas when there is pending delete on
1919 * the quota file... */
1920 if (!(flags & DQUOT_SUSPENDED))
Jan Kara0ff5af82008-04-28 02:14:33 -07001921 iput(toputinode[cnt]);
1922 else if (!toputinode[cnt]->i_nlink)
1923 ret = -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924 }
Jan Kara0ff5af82008-04-28 02:14:33 -07001925 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926}
1927
Jan Karaf55abc02008-08-20 17:50:32 +02001928int vfs_quota_off(struct super_block *sb, int type, int remount)
1929{
1930 return vfs_quota_disable(sb, type, remount ? DQUOT_SUSPENDED :
1931 (DQUOT_USAGE_ENABLED | DQUOT_LIMITS_ENABLED));
1932}
1933
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934/*
1935 * Turn quotas on on a device
1936 */
1937
Jan Karaf55abc02008-08-20 17:50:32 +02001938/*
1939 * Helper function to turn quotas on when we already have the inode of
1940 * quota file and no quota information is loaded.
1941 */
1942static int vfs_load_quota_inode(struct inode *inode, int type, int format_id,
1943 unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944{
1945 struct quota_format_type *fmt = find_quota_format(format_id);
1946 struct super_block *sb = inode->i_sb;
1947 struct quota_info *dqopt = sb_dqopt(sb);
1948 int error;
1949 int oldflags = -1;
1950
1951 if (!fmt)
1952 return -ESRCH;
1953 if (!S_ISREG(inode->i_mode)) {
1954 error = -EACCES;
1955 goto out_fmt;
1956 }
1957 if (IS_RDONLY(inode)) {
1958 error = -EROFS;
1959 goto out_fmt;
1960 }
1961 if (!sb->s_op->quota_write || !sb->s_op->quota_read) {
1962 error = -EINVAL;
1963 goto out_fmt;
1964 }
Jan Karaf55abc02008-08-20 17:50:32 +02001965 /* Usage always has to be set... */
1966 if (!(flags & DQUOT_USAGE_ENABLED)) {
1967 error = -EINVAL;
1968 goto out_fmt;
1969 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970
Jan Karaca785ec2008-09-30 17:53:37 +02001971 if (!(dqopt->flags & DQUOT_QUOTA_SYS_FILE)) {
1972 /* As we bypass the pagecache we must now flush the inode so
1973 * that we see all the changes from userspace... */
1974 write_inode_now(inode, 1);
1975 /* And now flush the block cache so that kernel sees the
1976 * changes */
1977 invalidate_bdev(sb->s_bdev);
1978 }
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001979 mutex_lock(&inode->i_mutex);
Ingo Molnard3be9152006-03-23 03:00:29 -08001980 mutex_lock(&dqopt->dqonoff_mutex);
Jan Karaf55abc02008-08-20 17:50:32 +02001981 if (sb_has_quota_loaded(sb, type)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982 error = -EBUSY;
1983 goto out_lock;
1984 }
Jan Karaca785ec2008-09-30 17:53:37 +02001985
1986 if (!(dqopt->flags & DQUOT_QUOTA_SYS_FILE)) {
1987 /* We don't want quota and atime on quota files (deadlocks
1988 * possible) Also nobody should write to the file - we use
1989 * special IO operations which ignore the immutable bit. */
1990 down_write(&dqopt->dqptr_sem);
1991 oldflags = inode->i_flags & (S_NOATIME | S_IMMUTABLE | S_NOQUOTA);
1992 inode->i_flags |= S_NOQUOTA | S_NOATIME | S_IMMUTABLE;
1993 up_write(&dqopt->dqptr_sem);
1994 sb->dq_op->drop(inode);
1995 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996
1997 error = -EIO;
1998 dqopt->files[type] = igrab(inode);
1999 if (!dqopt->files[type])
2000 goto out_lock;
2001 error = -EINVAL;
2002 if (!fmt->qf_ops->check_quota_file(sb, type))
2003 goto out_file_init;
2004
2005 dqopt->ops[type] = fmt->qf_ops;
2006 dqopt->info[type].dqi_format = fmt;
Jan Kara0ff5af82008-04-28 02:14:33 -07002007 dqopt->info[type].dqi_fmt_id = format_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008 INIT_LIST_HEAD(&dqopt->info[type].dqi_dirty_list);
Ingo Molnard3be9152006-03-23 03:00:29 -08002009 mutex_lock(&dqopt->dqio_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010 if ((error = dqopt->ops[type]->read_file_info(sb, type)) < 0) {
Ingo Molnard3be9152006-03-23 03:00:29 -08002011 mutex_unlock(&dqopt->dqio_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012 goto out_file_init;
2013 }
Ingo Molnard3be9152006-03-23 03:00:29 -08002014 mutex_unlock(&dqopt->dqio_mutex);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002015 mutex_unlock(&inode->i_mutex);
Jan Karacc334122009-01-12 17:23:05 +01002016 spin_lock(&dq_state_lock);
Jan Karaf55abc02008-08-20 17:50:32 +02002017 dqopt->flags |= dquot_state_flag(flags, type);
Jan Karacc334122009-01-12 17:23:05 +01002018 spin_unlock(&dq_state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019
2020 add_dquot_ref(sb, type);
Ingo Molnard3be9152006-03-23 03:00:29 -08002021 mutex_unlock(&dqopt->dqonoff_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022
2023 return 0;
2024
2025out_file_init:
2026 dqopt->files[type] = NULL;
2027 iput(inode);
2028out_lock:
Ingo Molnard3be9152006-03-23 03:00:29 -08002029 mutex_unlock(&dqopt->dqonoff_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030 if (oldflags != -1) {
2031 down_write(&dqopt->dqptr_sem);
2032 /* Set the flags back (in the case of accidental quotaon()
2033 * on a wrong file we don't want to mess up the flags) */
2034 inode->i_flags &= ~(S_NOATIME | S_NOQUOTA | S_IMMUTABLE);
2035 inode->i_flags |= oldflags;
2036 up_write(&dqopt->dqptr_sem);
2037 }
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002038 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039out_fmt:
2040 put_quota_format(fmt);
2041
2042 return error;
2043}
2044
Jan Kara0ff5af82008-04-28 02:14:33 -07002045/* Reenable quotas on remount RW */
2046static int vfs_quota_on_remount(struct super_block *sb, int type)
2047{
2048 struct quota_info *dqopt = sb_dqopt(sb);
2049 struct inode *inode;
2050 int ret;
Jan Karaf55abc02008-08-20 17:50:32 +02002051 unsigned int flags;
Jan Kara0ff5af82008-04-28 02:14:33 -07002052
2053 mutex_lock(&dqopt->dqonoff_mutex);
2054 if (!sb_has_quota_suspended(sb, type)) {
2055 mutex_unlock(&dqopt->dqonoff_mutex);
2056 return 0;
2057 }
Jan Kara0ff5af82008-04-28 02:14:33 -07002058 inode = dqopt->files[type];
2059 dqopt->files[type] = NULL;
Jan Karacc334122009-01-12 17:23:05 +01002060 spin_lock(&dq_state_lock);
Jan Karaf55abc02008-08-20 17:50:32 +02002061 flags = dqopt->flags & dquot_state_flag(DQUOT_USAGE_ENABLED |
2062 DQUOT_LIMITS_ENABLED, type);
2063 dqopt->flags &= ~dquot_state_flag(DQUOT_STATE_FLAGS, type);
Jan Karacc334122009-01-12 17:23:05 +01002064 spin_unlock(&dq_state_lock);
Jan Kara0ff5af82008-04-28 02:14:33 -07002065 mutex_unlock(&dqopt->dqonoff_mutex);
2066
Jan Karaf55abc02008-08-20 17:50:32 +02002067 flags = dquot_generic_flag(flags, type);
2068 ret = vfs_load_quota_inode(inode, type, dqopt->info[type].dqi_fmt_id,
2069 flags);
Jan Kara0ff5af82008-04-28 02:14:33 -07002070 iput(inode);
2071
2072 return ret;
2073}
2074
Al Viro77e69da2008-08-01 04:29:18 -04002075int vfs_quota_on_path(struct super_block *sb, int type, int format_id,
2076 struct path *path)
2077{
2078 int error = security_quota_on(path->dentry);
2079 if (error)
2080 return error;
2081 /* Quota file not on the same filesystem? */
2082 if (path->mnt->mnt_sb != sb)
2083 error = -EXDEV;
2084 else
Jan Karaf55abc02008-08-20 17:50:32 +02002085 error = vfs_load_quota_inode(path->dentry->d_inode, type,
2086 format_id, DQUOT_USAGE_ENABLED |
2087 DQUOT_LIMITS_ENABLED);
Al Viro77e69da2008-08-01 04:29:18 -04002088 return error;
2089}
2090
Al Viro82646132008-08-02 00:57:06 -04002091int vfs_quota_on(struct super_block *sb, int type, int format_id, char *name,
Jan Kara0ff5af82008-04-28 02:14:33 -07002092 int remount)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093{
Al Viro82646132008-08-02 00:57:06 -04002094 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095 int error;
2096
Jan Kara0ff5af82008-04-28 02:14:33 -07002097 if (remount)
2098 return vfs_quota_on_remount(sb, type);
2099
Al Viro82646132008-08-02 00:57:06 -04002100 error = kern_path(name, LOOKUP_FOLLOW, &path);
Al Viro77e69da2008-08-01 04:29:18 -04002101 if (!error) {
Al Viro82646132008-08-02 00:57:06 -04002102 error = vfs_quota_on_path(sb, type, format_id, &path);
2103 path_put(&path);
Al Viro77e69da2008-08-01 04:29:18 -04002104 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105 return error;
2106}
2107
2108/*
Jan Karaf55abc02008-08-20 17:50:32 +02002109 * More powerful function for turning on quotas allowing setting
2110 * of individual quota flags
2111 */
2112int vfs_quota_enable(struct inode *inode, int type, int format_id,
2113 unsigned int flags)
2114{
2115 int ret = 0;
2116 struct super_block *sb = inode->i_sb;
2117 struct quota_info *dqopt = sb_dqopt(sb);
2118
2119 /* Just unsuspend quotas? */
2120 if (flags & DQUOT_SUSPENDED)
2121 return vfs_quota_on_remount(sb, type);
2122 if (!flags)
2123 return 0;
2124 /* Just updating flags needed? */
2125 if (sb_has_quota_loaded(sb, type)) {
2126 mutex_lock(&dqopt->dqonoff_mutex);
2127 /* Now do a reliable test... */
2128 if (!sb_has_quota_loaded(sb, type)) {
2129 mutex_unlock(&dqopt->dqonoff_mutex);
2130 goto load_quota;
2131 }
2132 if (flags & DQUOT_USAGE_ENABLED &&
2133 sb_has_quota_usage_enabled(sb, type)) {
2134 ret = -EBUSY;
2135 goto out_lock;
2136 }
2137 if (flags & DQUOT_LIMITS_ENABLED &&
2138 sb_has_quota_limits_enabled(sb, type)) {
2139 ret = -EBUSY;
2140 goto out_lock;
2141 }
Jan Karacc334122009-01-12 17:23:05 +01002142 spin_lock(&dq_state_lock);
Jan Karaf55abc02008-08-20 17:50:32 +02002143 sb_dqopt(sb)->flags |= dquot_state_flag(flags, type);
Jan Karacc334122009-01-12 17:23:05 +01002144 spin_unlock(&dq_state_lock);
Jan Karaf55abc02008-08-20 17:50:32 +02002145out_lock:
2146 mutex_unlock(&dqopt->dqonoff_mutex);
2147 return ret;
2148 }
2149
2150load_quota:
2151 return vfs_load_quota_inode(inode, type, format_id, flags);
2152}
2153
2154/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155 * This function is used when filesystem needs to initialize quotas
2156 * during mount time.
2157 */
Christoph Hellwig84de8562005-06-23 00:09:16 -07002158int vfs_quota_on_mount(struct super_block *sb, char *qf_name,
2159 int format_id, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160{
Christoph Hellwig84de8562005-06-23 00:09:16 -07002161 struct dentry *dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162 int error;
2163
Christoph Hellwig2fa389c2005-06-23 00:09:16 -07002164 dentry = lookup_one_len(qf_name, sb->s_root, strlen(qf_name));
Christoph Hellwig84de8562005-06-23 00:09:16 -07002165 if (IS_ERR(dentry))
2166 return PTR_ERR(dentry);
2167
Jan Kara154f4842005-11-28 13:44:14 -08002168 if (!dentry->d_inode) {
2169 error = -ENOENT;
2170 goto out;
2171 }
2172
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173 error = security_quota_on(dentry);
Christoph Hellwig84de8562005-06-23 00:09:16 -07002174 if (!error)
Jan Karaf55abc02008-08-20 17:50:32 +02002175 error = vfs_load_quota_inode(dentry->d_inode, type, format_id,
2176 DQUOT_USAGE_ENABLED | DQUOT_LIMITS_ENABLED);
Christoph Hellwig84de8562005-06-23 00:09:16 -07002177
Jan Kara154f4842005-11-28 13:44:14 -08002178out:
Christoph Hellwig84de8562005-06-23 00:09:16 -07002179 dput(dentry);
2180 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181}
2182
Jan Karab85f4b82008-07-25 01:46:50 -07002183/* Wrapper to turn on quotas when remounting rw */
2184int vfs_dq_quota_on_remount(struct super_block *sb)
2185{
2186 int cnt;
2187 int ret = 0, err;
2188
2189 if (!sb->s_qcop || !sb->s_qcop->quota_on)
2190 return -ENOSYS;
2191 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
2192 err = sb->s_qcop->quota_on(sb, cnt, 0, NULL, 1);
2193 if (err < 0 && !ret)
2194 ret = err;
2195 }
2196 return ret;
2197}
2198
Jan Kara12095462008-08-20 14:45:12 +02002199static inline qsize_t qbtos(qsize_t blocks)
2200{
2201 return blocks << QIF_DQBLKSIZE_BITS;
2202}
2203
2204static inline qsize_t stoqb(qsize_t space)
2205{
2206 return (space + QIF_DQBLKSIZE - 1) >> QIF_DQBLKSIZE_BITS;
2207}
2208
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209/* Generic routine for getting common part of quota structure */
2210static void do_get_dqblk(struct dquot *dquot, struct if_dqblk *di)
2211{
2212 struct mem_dqblk *dm = &dquot->dq_dqb;
2213
2214 spin_lock(&dq_data_lock);
Jan Kara12095462008-08-20 14:45:12 +02002215 di->dqb_bhardlimit = stoqb(dm->dqb_bhardlimit);
2216 di->dqb_bsoftlimit = stoqb(dm->dqb_bsoftlimit);
Mingming Caof18df222009-01-13 16:43:09 +01002217 di->dqb_curspace = dm->dqb_curspace + dm->dqb_rsvspace;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218 di->dqb_ihardlimit = dm->dqb_ihardlimit;
2219 di->dqb_isoftlimit = dm->dqb_isoftlimit;
2220 di->dqb_curinodes = dm->dqb_curinodes;
2221 di->dqb_btime = dm->dqb_btime;
2222 di->dqb_itime = dm->dqb_itime;
2223 di->dqb_valid = QIF_ALL;
2224 spin_unlock(&dq_data_lock);
2225}
2226
2227int vfs_get_dqblk(struct super_block *sb, int type, qid_t id, struct if_dqblk *di)
2228{
2229 struct dquot *dquot;
2230
Jan Karacc334122009-01-12 17:23:05 +01002231 dquot = dqget(sb, id, type);
2232 if (dquot == NODQUOT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233 return -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234 do_get_dqblk(dquot, di);
2235 dqput(dquot);
Jan Karacc334122009-01-12 17:23:05 +01002236
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237 return 0;
2238}
2239
2240/* Generic routine for setting common part of quota structure */
Andrew Perepechko338bf9a2008-04-28 02:14:31 -07002241static int do_set_dqblk(struct dquot *dquot, struct if_dqblk *di)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242{
2243 struct mem_dqblk *dm = &dquot->dq_dqb;
2244 int check_blim = 0, check_ilim = 0;
Andrew Perepechko338bf9a2008-04-28 02:14:31 -07002245 struct mem_dqinfo *dqi = &sb_dqopt(dquot->dq_sb)->info[dquot->dq_type];
2246
2247 if ((di->dqb_valid & QIF_BLIMITS &&
2248 (di->dqb_bhardlimit > dqi->dqi_maxblimit ||
2249 di->dqb_bsoftlimit > dqi->dqi_maxblimit)) ||
2250 (di->dqb_valid & QIF_ILIMITS &&
2251 (di->dqb_ihardlimit > dqi->dqi_maxilimit ||
2252 di->dqb_isoftlimit > dqi->dqi_maxilimit)))
2253 return -ERANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254
2255 spin_lock(&dq_data_lock);
2256 if (di->dqb_valid & QIF_SPACE) {
Mingming Caof18df222009-01-13 16:43:09 +01002257 dm->dqb_curspace = di->dqb_curspace - dm->dqb_rsvspace;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258 check_blim = 1;
Jan Kara4d59bce2008-10-02 16:48:10 +02002259 __set_bit(DQ_LASTSET_B + QIF_SPACE_B, &dquot->dq_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260 }
2261 if (di->dqb_valid & QIF_BLIMITS) {
Jan Kara12095462008-08-20 14:45:12 +02002262 dm->dqb_bsoftlimit = qbtos(di->dqb_bsoftlimit);
2263 dm->dqb_bhardlimit = qbtos(di->dqb_bhardlimit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264 check_blim = 1;
Jan Kara4d59bce2008-10-02 16:48:10 +02002265 __set_bit(DQ_LASTSET_B + QIF_BLIMITS_B, &dquot->dq_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266 }
2267 if (di->dqb_valid & QIF_INODES) {
2268 dm->dqb_curinodes = di->dqb_curinodes;
2269 check_ilim = 1;
Jan Kara4d59bce2008-10-02 16:48:10 +02002270 __set_bit(DQ_LASTSET_B + QIF_INODES_B, &dquot->dq_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271 }
2272 if (di->dqb_valid & QIF_ILIMITS) {
2273 dm->dqb_isoftlimit = di->dqb_isoftlimit;
2274 dm->dqb_ihardlimit = di->dqb_ihardlimit;
2275 check_ilim = 1;
Jan Kara4d59bce2008-10-02 16:48:10 +02002276 __set_bit(DQ_LASTSET_B + QIF_ILIMITS_B, &dquot->dq_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277 }
Jan Kara4d59bce2008-10-02 16:48:10 +02002278 if (di->dqb_valid & QIF_BTIME) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279 dm->dqb_btime = di->dqb_btime;
Jan Karae04a88a92009-01-07 18:07:29 -08002280 check_blim = 1;
Jan Kara4d59bce2008-10-02 16:48:10 +02002281 __set_bit(DQ_LASTSET_B + QIF_BTIME_B, &dquot->dq_flags);
2282 }
2283 if (di->dqb_valid & QIF_ITIME) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284 dm->dqb_itime = di->dqb_itime;
Jan Karae04a88a92009-01-07 18:07:29 -08002285 check_ilim = 1;
Jan Kara4d59bce2008-10-02 16:48:10 +02002286 __set_bit(DQ_LASTSET_B + QIF_ITIME_B, &dquot->dq_flags);
2287 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288
2289 if (check_blim) {
Jan Kara12095462008-08-20 14:45:12 +02002290 if (!dm->dqb_bsoftlimit || dm->dqb_curspace < dm->dqb_bsoftlimit) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291 dm->dqb_btime = 0;
2292 clear_bit(DQ_BLKS_B, &dquot->dq_flags);
2293 }
2294 else if (!(di->dqb_valid & QIF_BTIME)) /* Set grace only if user hasn't provided his own... */
Andrew Perepechko338bf9a2008-04-28 02:14:31 -07002295 dm->dqb_btime = get_seconds() + dqi->dqi_bgrace;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296 }
2297 if (check_ilim) {
2298 if (!dm->dqb_isoftlimit || dm->dqb_curinodes < dm->dqb_isoftlimit) {
2299 dm->dqb_itime = 0;
2300 clear_bit(DQ_INODES_B, &dquot->dq_flags);
2301 }
2302 else if (!(di->dqb_valid & QIF_ITIME)) /* Set grace only if user hasn't provided his own... */
Andrew Perepechko338bf9a2008-04-28 02:14:31 -07002303 dm->dqb_itime = get_seconds() + dqi->dqi_igrace;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304 }
2305 if (dm->dqb_bhardlimit || dm->dqb_bsoftlimit || dm->dqb_ihardlimit || dm->dqb_isoftlimit)
2306 clear_bit(DQ_FAKE_B, &dquot->dq_flags);
2307 else
2308 set_bit(DQ_FAKE_B, &dquot->dq_flags);
2309 spin_unlock(&dq_data_lock);
2310 mark_dquot_dirty(dquot);
Andrew Perepechko338bf9a2008-04-28 02:14:31 -07002311
2312 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313}
2314
2315int vfs_set_dqblk(struct super_block *sb, int type, qid_t id, struct if_dqblk *di)
2316{
2317 struct dquot *dquot;
Andrew Perepechko338bf9a2008-04-28 02:14:31 -07002318 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319
Jan Karaf55abc02008-08-20 17:50:32 +02002320 dquot = dqget(sb, id, type);
2321 if (!dquot) {
2322 rc = -ESRCH;
2323 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324 }
Andrew Perepechko338bf9a2008-04-28 02:14:31 -07002325 rc = do_set_dqblk(dquot, di);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002326 dqput(dquot);
Jan Karaf55abc02008-08-20 17:50:32 +02002327out:
Andrew Perepechko338bf9a2008-04-28 02:14:31 -07002328 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329}
2330
2331/* Generic routine for getting common part of quota file information */
2332int vfs_get_dqinfo(struct super_block *sb, int type, struct if_dqinfo *ii)
2333{
2334 struct mem_dqinfo *mi;
2335
Ingo Molnard3be9152006-03-23 03:00:29 -08002336 mutex_lock(&sb_dqopt(sb)->dqonoff_mutex);
Jan Karaf55abc02008-08-20 17:50:32 +02002337 if (!sb_has_quota_active(sb, type)) {
Ingo Molnard3be9152006-03-23 03:00:29 -08002338 mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339 return -ESRCH;
2340 }
2341 mi = sb_dqopt(sb)->info + type;
2342 spin_lock(&dq_data_lock);
2343 ii->dqi_bgrace = mi->dqi_bgrace;
2344 ii->dqi_igrace = mi->dqi_igrace;
2345 ii->dqi_flags = mi->dqi_flags & DQF_MASK;
2346 ii->dqi_valid = IIF_ALL;
2347 spin_unlock(&dq_data_lock);
Ingo Molnard3be9152006-03-23 03:00:29 -08002348 mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349 return 0;
2350}
2351
2352/* Generic routine for setting common part of quota file information */
2353int vfs_set_dqinfo(struct super_block *sb, int type, struct if_dqinfo *ii)
2354{
2355 struct mem_dqinfo *mi;
Jan Karaf55abc02008-08-20 17:50:32 +02002356 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357
Ingo Molnard3be9152006-03-23 03:00:29 -08002358 mutex_lock(&sb_dqopt(sb)->dqonoff_mutex);
Jan Karaf55abc02008-08-20 17:50:32 +02002359 if (!sb_has_quota_active(sb, type)) {
2360 err = -ESRCH;
2361 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362 }
2363 mi = sb_dqopt(sb)->info + type;
2364 spin_lock(&dq_data_lock);
2365 if (ii->dqi_valid & IIF_BGRACE)
2366 mi->dqi_bgrace = ii->dqi_bgrace;
2367 if (ii->dqi_valid & IIF_IGRACE)
2368 mi->dqi_igrace = ii->dqi_igrace;
2369 if (ii->dqi_valid & IIF_FLAGS)
2370 mi->dqi_flags = (mi->dqi_flags & ~DQF_MASK) | (ii->dqi_flags & DQF_MASK);
2371 spin_unlock(&dq_data_lock);
2372 mark_info_dirty(sb, type);
2373 /* Force write to disk */
2374 sb->dq_op->write_info(sb, type);
Jan Karaf55abc02008-08-20 17:50:32 +02002375out:
Ingo Molnard3be9152006-03-23 03:00:29 -08002376 mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
Jan Karaf55abc02008-08-20 17:50:32 +02002377 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002378}
2379
2380struct quotactl_ops vfs_quotactl_ops = {
2381 .quota_on = vfs_quota_on,
2382 .quota_off = vfs_quota_off,
2383 .quota_sync = vfs_quota_sync,
2384 .get_info = vfs_get_dqinfo,
2385 .set_info = vfs_set_dqinfo,
2386 .get_dqblk = vfs_get_dqblk,
2387 .set_dqblk = vfs_set_dqblk
2388};
2389
2390static ctl_table fs_dqstats_table[] = {
2391 {
2392 .ctl_name = FS_DQ_LOOKUPS,
2393 .procname = "lookups",
2394 .data = &dqstats.lookups,
2395 .maxlen = sizeof(int),
2396 .mode = 0444,
2397 .proc_handler = &proc_dointvec,
2398 },
2399 {
2400 .ctl_name = FS_DQ_DROPS,
2401 .procname = "drops",
2402 .data = &dqstats.drops,
2403 .maxlen = sizeof(int),
2404 .mode = 0444,
2405 .proc_handler = &proc_dointvec,
2406 },
2407 {
2408 .ctl_name = FS_DQ_READS,
2409 .procname = "reads",
2410 .data = &dqstats.reads,
2411 .maxlen = sizeof(int),
2412 .mode = 0444,
2413 .proc_handler = &proc_dointvec,
2414 },
2415 {
2416 .ctl_name = FS_DQ_WRITES,
2417 .procname = "writes",
2418 .data = &dqstats.writes,
2419 .maxlen = sizeof(int),
2420 .mode = 0444,
2421 .proc_handler = &proc_dointvec,
2422 },
2423 {
2424 .ctl_name = FS_DQ_CACHE_HITS,
2425 .procname = "cache_hits",
2426 .data = &dqstats.cache_hits,
2427 .maxlen = sizeof(int),
2428 .mode = 0444,
2429 .proc_handler = &proc_dointvec,
2430 },
2431 {
2432 .ctl_name = FS_DQ_ALLOCATED,
2433 .procname = "allocated_dquots",
2434 .data = &dqstats.allocated_dquots,
2435 .maxlen = sizeof(int),
2436 .mode = 0444,
2437 .proc_handler = &proc_dointvec,
2438 },
2439 {
2440 .ctl_name = FS_DQ_FREE,
2441 .procname = "free_dquots",
2442 .data = &dqstats.free_dquots,
2443 .maxlen = sizeof(int),
2444 .mode = 0444,
2445 .proc_handler = &proc_dointvec,
2446 },
2447 {
2448 .ctl_name = FS_DQ_SYNCS,
2449 .procname = "syncs",
2450 .data = &dqstats.syncs,
2451 .maxlen = sizeof(int),
2452 .mode = 0444,
2453 .proc_handler = &proc_dointvec,
2454 },
Jan Kara8e893462007-10-16 23:29:31 -07002455#ifdef CONFIG_PRINT_QUOTA_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07002456 {
2457 .ctl_name = FS_DQ_WARNINGS,
2458 .procname = "warnings",
2459 .data = &flag_print_warnings,
2460 .maxlen = sizeof(int),
2461 .mode = 0644,
2462 .proc_handler = &proc_dointvec,
2463 },
Jan Kara8e893462007-10-16 23:29:31 -07002464#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002465 { .ctl_name = 0 },
2466};
2467
2468static ctl_table fs_table[] = {
2469 {
2470 .ctl_name = FS_DQSTATS,
2471 .procname = "quota",
2472 .mode = 0555,
2473 .child = fs_dqstats_table,
2474 },
2475 { .ctl_name = 0 },
2476};
2477
2478static ctl_table sys_table[] = {
2479 {
2480 .ctl_name = CTL_FS,
2481 .procname = "fs",
2482 .mode = 0555,
2483 .child = fs_table,
2484 },
2485 { .ctl_name = 0 },
2486};
2487
2488static int __init dquot_init(void)
2489{
2490 int i;
2491 unsigned long nr_hash, order;
2492
2493 printk(KERN_NOTICE "VFS: Disk quotas %s\n", __DQUOT_VERSION__);
2494
Eric W. Biederman0b4d4142007-02-14 00:34:09 -08002495 register_sysctl_table(sys_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002496
Paul Mundt20c2df82007-07-20 10:11:58 +09002497 dquot_cachep = kmem_cache_create("dquot",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002498 sizeof(struct dquot), sizeof(unsigned long) * 4,
Paul Jacksonfffb60f2006-03-24 03:16:06 -08002499 (SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT|
2500 SLAB_MEM_SPREAD|SLAB_PANIC),
Paul Mundt20c2df82007-07-20 10:11:58 +09002501 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002502
2503 order = 0;
2504 dquot_hash = (struct hlist_head *)__get_free_pages(GFP_ATOMIC, order);
2505 if (!dquot_hash)
2506 panic("Cannot create dquot hash table");
2507
2508 /* Find power-of-two hlist_heads which can fit into allocation */
2509 nr_hash = (1UL << order) * PAGE_SIZE / sizeof(struct hlist_head);
2510 dq_hash_bits = 0;
2511 do {
2512 dq_hash_bits++;
2513 } while (nr_hash >> dq_hash_bits);
2514 dq_hash_bits--;
2515
2516 nr_hash = 1UL << dq_hash_bits;
2517 dq_hash_mask = nr_hash - 1;
2518 for (i = 0; i < nr_hash; i++)
2519 INIT_HLIST_HEAD(dquot_hash + i);
2520
2521 printk("Dquot-cache hash table entries: %ld (order %ld, %ld bytes)\n",
2522 nr_hash, order, (PAGE_SIZE << order));
2523
Rusty Russell8e1f9362007-07-17 04:03:17 -07002524 register_shrinker(&dqcache_shrinker);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002525
Jan Kara8e893462007-10-16 23:29:31 -07002526#ifdef CONFIG_QUOTA_NETLINK_INTERFACE
2527 if (genl_register_family(&quota_genl_family) != 0)
2528 printk(KERN_ERR "VFS: Failed to create quota netlink interface.\n");
2529#endif
2530
Linus Torvalds1da177e2005-04-16 15:20:36 -07002531 return 0;
2532}
2533module_init(dquot_init);
2534
2535EXPORT_SYMBOL(register_quota_format);
2536EXPORT_SYMBOL(unregister_quota_format);
2537EXPORT_SYMBOL(dqstats);
2538EXPORT_SYMBOL(dq_data_lock);
Jan Karaf55abc02008-08-20 17:50:32 +02002539EXPORT_SYMBOL(vfs_quota_enable);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002540EXPORT_SYMBOL(vfs_quota_on);
Al Viro77e69da2008-08-01 04:29:18 -04002541EXPORT_SYMBOL(vfs_quota_on_path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002542EXPORT_SYMBOL(vfs_quota_on_mount);
Jan Karaf55abc02008-08-20 17:50:32 +02002543EXPORT_SYMBOL(vfs_quota_disable);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002544EXPORT_SYMBOL(vfs_quota_off);
Jan Kara12c77522008-10-20 17:05:00 +02002545EXPORT_SYMBOL(dquot_scan_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002546EXPORT_SYMBOL(vfs_quota_sync);
2547EXPORT_SYMBOL(vfs_get_dqinfo);
2548EXPORT_SYMBOL(vfs_set_dqinfo);
2549EXPORT_SYMBOL(vfs_get_dqblk);
2550EXPORT_SYMBOL(vfs_set_dqblk);
2551EXPORT_SYMBOL(dquot_commit);
2552EXPORT_SYMBOL(dquot_commit_info);
2553EXPORT_SYMBOL(dquot_acquire);
2554EXPORT_SYMBOL(dquot_release);
2555EXPORT_SYMBOL(dquot_mark_dquot_dirty);
2556EXPORT_SYMBOL(dquot_initialize);
2557EXPORT_SYMBOL(dquot_drop);
Jan Karab85f4b82008-07-25 01:46:50 -07002558EXPORT_SYMBOL(vfs_dq_drop);
Jan Kara3d9ea252008-10-10 16:12:23 +02002559EXPORT_SYMBOL(dqget);
2560EXPORT_SYMBOL(dqput);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002561EXPORT_SYMBOL(dquot_alloc_space);
2562EXPORT_SYMBOL(dquot_alloc_inode);
2563EXPORT_SYMBOL(dquot_free_space);
2564EXPORT_SYMBOL(dquot_free_inode);
2565EXPORT_SYMBOL(dquot_transfer);
Jan Karab85f4b82008-07-25 01:46:50 -07002566EXPORT_SYMBOL(vfs_dq_transfer);
2567EXPORT_SYMBOL(vfs_dq_quota_on_remount);