blob: d6add0bf5ad3a9200ced2db449b0d6e1b049d146 [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
Jan Kara12095462008-08-20 14:45:12 +0200902static inline void dquot_decr_inodes(struct dquot *dquot, qsize_t number)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903{
Jan Karadb49d2d2008-10-01 18:21:39 +0200904 if (sb_dqopt(dquot->dq_sb)->flags & DQUOT_NEGATIVE_USAGE ||
905 dquot->dq_dqb.dqb_curinodes >= number)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 dquot->dq_dqb.dqb_curinodes -= number;
907 else
908 dquot->dq_dqb.dqb_curinodes = 0;
909 if (dquot->dq_dqb.dqb_curinodes <= dquot->dq_dqb.dqb_isoftlimit)
910 dquot->dq_dqb.dqb_itime = (time_t) 0;
911 clear_bit(DQ_INODES_B, &dquot->dq_flags);
912}
913
914static inline void dquot_decr_space(struct dquot *dquot, qsize_t number)
915{
Jan Karadb49d2d2008-10-01 18:21:39 +0200916 if (sb_dqopt(dquot->dq_sb)->flags & DQUOT_NEGATIVE_USAGE ||
917 dquot->dq_dqb.dqb_curspace >= number)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 dquot->dq_dqb.dqb_curspace -= number;
919 else
920 dquot->dq_dqb.dqb_curspace = 0;
Jan Kara12095462008-08-20 14:45:12 +0200921 if (dquot->dq_dqb.dqb_curspace <= dquot->dq_dqb.dqb_bsoftlimit)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 dquot->dq_dqb.dqb_btime = (time_t) 0;
923 clear_bit(DQ_BLKS_B, &dquot->dq_flags);
924}
925
Jan Karac5254602007-12-22 14:03:25 -0800926static int warning_issued(struct dquot *dquot, const int warntype)
927{
928 int flag = (warntype == QUOTA_NL_BHARDWARN ||
929 warntype == QUOTA_NL_BSOFTLONGWARN) ? DQ_BLKS_B :
930 ((warntype == QUOTA_NL_IHARDWARN ||
931 warntype == QUOTA_NL_ISOFTLONGWARN) ? DQ_INODES_B : 0);
932
933 if (!flag)
934 return 0;
935 return test_and_set_bit(flag, &dquot->dq_flags);
936}
937
Jan Kara8e893462007-10-16 23:29:31 -0700938#ifdef CONFIG_PRINT_QUOTA_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939static int flag_print_warnings = 1;
940
941static inline int need_print_warning(struct dquot *dquot)
942{
943 if (!flag_print_warnings)
944 return 0;
945
946 switch (dquot->dq_type) {
947 case USRQUOTA:
David Howellsda9592e2008-11-14 10:39:05 +1100948 return current_fsuid() == dquot->dq_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 case GRPQUOTA:
950 return in_group_p(dquot->dq_id);
951 }
952 return 0;
953}
954
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955/* Print warning to user which exceeded quota */
Jan Karac5254602007-12-22 14:03:25 -0800956static void print_warning(struct dquot *dquot, const int warntype)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957{
958 char *msg = NULL;
Peter Zijlstra24ec8392006-12-08 02:36:04 -0800959 struct tty_struct *tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960
Jan Kara657d3bf2008-07-25 01:46:52 -0700961 if (warntype == QUOTA_NL_IHARDBELOW ||
962 warntype == QUOTA_NL_ISOFTBELOW ||
963 warntype == QUOTA_NL_BHARDBELOW ||
964 warntype == QUOTA_NL_BSOFTBELOW || !need_print_warning(dquot))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 return;
966
Peter Zijlstra24ec8392006-12-08 02:36:04 -0800967 tty = get_current_tty();
968 if (!tty)
Alan Cox452a00d2008-10-13 10:39:13 +0100969 return;
Peter Zijlstra24ec8392006-12-08 02:36:04 -0800970 tty_write_message(tty, dquot->dq_sb->s_id);
Jan Kara8e893462007-10-16 23:29:31 -0700971 if (warntype == QUOTA_NL_ISOFTWARN || warntype == QUOTA_NL_BSOFTWARN)
Peter Zijlstra24ec8392006-12-08 02:36:04 -0800972 tty_write_message(tty, ": warning, ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 else
Peter Zijlstra24ec8392006-12-08 02:36:04 -0800974 tty_write_message(tty, ": write failed, ");
975 tty_write_message(tty, quotatypes[dquot->dq_type]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 switch (warntype) {
Jan Kara8e893462007-10-16 23:29:31 -0700977 case QUOTA_NL_IHARDWARN:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 msg = " file limit reached.\r\n";
979 break;
Jan Kara8e893462007-10-16 23:29:31 -0700980 case QUOTA_NL_ISOFTLONGWARN:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 msg = " file quota exceeded too long.\r\n";
982 break;
Jan Kara8e893462007-10-16 23:29:31 -0700983 case QUOTA_NL_ISOFTWARN:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 msg = " file quota exceeded.\r\n";
985 break;
Jan Kara8e893462007-10-16 23:29:31 -0700986 case QUOTA_NL_BHARDWARN:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 msg = " block limit reached.\r\n";
988 break;
Jan Kara8e893462007-10-16 23:29:31 -0700989 case QUOTA_NL_BSOFTLONGWARN:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 msg = " block quota exceeded too long.\r\n";
991 break;
Jan Kara8e893462007-10-16 23:29:31 -0700992 case QUOTA_NL_BSOFTWARN:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 msg = " block quota exceeded.\r\n";
994 break;
995 }
Peter Zijlstra24ec8392006-12-08 02:36:04 -0800996 tty_write_message(tty, msg);
Alan Cox452a00d2008-10-13 10:39:13 +0100997 tty_kref_put(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998}
Jan Kara8e893462007-10-16 23:29:31 -0700999#endif
1000
1001#ifdef CONFIG_QUOTA_NETLINK_INTERFACE
1002
Jan Kara8e893462007-10-16 23:29:31 -07001003/* Netlink family structure for quota */
1004static struct genl_family quota_genl_family = {
1005 .id = GENL_ID_GENERATE,
1006 .hdrsize = 0,
1007 .name = "VFS_DQUOT",
1008 .version = 1,
1009 .maxattr = QUOTA_NL_A_MAX,
1010};
1011
1012/* Send warning to userspace about user which exceeded quota */
1013static void send_warning(const struct dquot *dquot, const char warntype)
1014{
1015 static atomic_t seq;
1016 struct sk_buff *skb;
1017 void *msg_head;
1018 int ret;
Jan Kara22dd4832007-12-22 14:03:25 -08001019 int msg_size = 4 * nla_total_size(sizeof(u32)) +
1020 2 * nla_total_size(sizeof(u64));
Jan Kara8e893462007-10-16 23:29:31 -07001021
1022 /* We have to allocate using GFP_NOFS as we are called from a
1023 * filesystem performing write and thus further recursion into
1024 * the fs to free some data could cause deadlocks. */
Jan Kara22dd4832007-12-22 14:03:25 -08001025 skb = genlmsg_new(msg_size, GFP_NOFS);
Jan Kara8e893462007-10-16 23:29:31 -07001026 if (!skb) {
1027 printk(KERN_ERR
1028 "VFS: Not enough memory to send quota warning.\n");
1029 return;
1030 }
1031 msg_head = genlmsg_put(skb, 0, atomic_add_return(1, &seq),
1032 &quota_genl_family, 0, QUOTA_NL_C_WARNING);
1033 if (!msg_head) {
1034 printk(KERN_ERR
1035 "VFS: Cannot store netlink header in quota warning.\n");
1036 goto err_out;
1037 }
1038 ret = nla_put_u32(skb, QUOTA_NL_A_QTYPE, dquot->dq_type);
1039 if (ret)
1040 goto attr_err_out;
1041 ret = nla_put_u64(skb, QUOTA_NL_A_EXCESS_ID, dquot->dq_id);
1042 if (ret)
1043 goto attr_err_out;
1044 ret = nla_put_u32(skb, QUOTA_NL_A_WARNING, warntype);
1045 if (ret)
1046 goto attr_err_out;
1047 ret = nla_put_u32(skb, QUOTA_NL_A_DEV_MAJOR,
1048 MAJOR(dquot->dq_sb->s_dev));
1049 if (ret)
1050 goto attr_err_out;
1051 ret = nla_put_u32(skb, QUOTA_NL_A_DEV_MINOR,
1052 MINOR(dquot->dq_sb->s_dev));
1053 if (ret)
1054 goto attr_err_out;
David Howellsda9592e2008-11-14 10:39:05 +11001055 ret = nla_put_u64(skb, QUOTA_NL_A_CAUSED_ID, current_uid());
Jan Kara8e893462007-10-16 23:29:31 -07001056 if (ret)
1057 goto attr_err_out;
1058 genlmsg_end(skb, msg_head);
1059
Pablo Neira Ayusoff491a72009-02-05 23:56:36 -08001060 genlmsg_multicast(skb, 0, quota_genl_family.id, GFP_NOFS);
Jan Kara8e893462007-10-16 23:29:31 -07001061 return;
1062attr_err_out:
Jan Kara22dd4832007-12-22 14:03:25 -08001063 printk(KERN_ERR "VFS: Not enough space to compose quota message!\n");
Jan Kara8e893462007-10-16 23:29:31 -07001064err_out:
1065 kfree_skb(skb);
1066}
1067#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068
Jan Kara087ee8d2007-12-17 16:20:26 -08001069static inline void flush_warnings(struct dquot * const *dquots, char *warntype)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070{
1071 int i;
1072
1073 for (i = 0; i < MAXQUOTAS; i++)
Jan Karac5254602007-12-22 14:03:25 -08001074 if (dquots[i] != NODQUOT && warntype[i] != QUOTA_NL_NOWARN &&
1075 !warning_issued(dquots[i], warntype[i])) {
Jan Kara8e893462007-10-16 23:29:31 -07001076#ifdef CONFIG_PRINT_QUOTA_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 print_warning(dquots[i], warntype[i]);
Jan Kara8e893462007-10-16 23:29:31 -07001078#endif
1079#ifdef CONFIG_QUOTA_NETLINK_INTERFACE
1080 send_warning(dquots[i], warntype[i]);
1081#endif
1082 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083}
1084
1085static inline char ignore_hardlimit(struct dquot *dquot)
1086{
1087 struct mem_dqinfo *info = &sb_dqopt(dquot->dq_sb)->info[dquot->dq_type];
1088
1089 return capable(CAP_SYS_RESOURCE) &&
1090 (info->dqi_format->qf_fmt_id != QFMT_VFS_OLD || !(info->dqi_flags & V1_DQF_RSQUASH));
1091}
1092
1093/* needs dq_data_lock */
Jan Kara12095462008-08-20 14:45:12 +02001094static int check_idq(struct dquot *dquot, qsize_t inodes, char *warntype)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095{
Jan Kara8e893462007-10-16 23:29:31 -07001096 *warntype = QUOTA_NL_NOWARN;
Jan Karaf55abc02008-08-20 17:50:32 +02001097 if (!sb_has_quota_limits_enabled(dquot->dq_sb, dquot->dq_type) ||
1098 test_bit(DQ_FAKE_B, &dquot->dq_flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 return QUOTA_OK;
1100
1101 if (dquot->dq_dqb.dqb_ihardlimit &&
1102 (dquot->dq_dqb.dqb_curinodes + inodes) > dquot->dq_dqb.dqb_ihardlimit &&
1103 !ignore_hardlimit(dquot)) {
Jan Kara8e893462007-10-16 23:29:31 -07001104 *warntype = QUOTA_NL_IHARDWARN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 return NO_QUOTA;
1106 }
1107
1108 if (dquot->dq_dqb.dqb_isoftlimit &&
1109 (dquot->dq_dqb.dqb_curinodes + inodes) > dquot->dq_dqb.dqb_isoftlimit &&
1110 dquot->dq_dqb.dqb_itime && get_seconds() >= dquot->dq_dqb.dqb_itime &&
1111 !ignore_hardlimit(dquot)) {
Jan Kara8e893462007-10-16 23:29:31 -07001112 *warntype = QUOTA_NL_ISOFTLONGWARN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 return NO_QUOTA;
1114 }
1115
1116 if (dquot->dq_dqb.dqb_isoftlimit &&
1117 (dquot->dq_dqb.dqb_curinodes + inodes) > dquot->dq_dqb.dqb_isoftlimit &&
1118 dquot->dq_dqb.dqb_itime == 0) {
Jan Kara8e893462007-10-16 23:29:31 -07001119 *warntype = QUOTA_NL_ISOFTWARN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 dquot->dq_dqb.dqb_itime = get_seconds() + sb_dqopt(dquot->dq_sb)->info[dquot->dq_type].dqi_igrace;
1121 }
1122
1123 return QUOTA_OK;
1124}
1125
1126/* needs dq_data_lock */
1127static int check_bdq(struct dquot *dquot, qsize_t space, int prealloc, char *warntype)
1128{
Jan Kara8e893462007-10-16 23:29:31 -07001129 *warntype = QUOTA_NL_NOWARN;
Jan Karaf55abc02008-08-20 17:50:32 +02001130 if (!sb_has_quota_limits_enabled(dquot->dq_sb, dquot->dq_type) ||
1131 test_bit(DQ_FAKE_B, &dquot->dq_flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 return QUOTA_OK;
1133
1134 if (dquot->dq_dqb.dqb_bhardlimit &&
Jan Kara12095462008-08-20 14:45:12 +02001135 dquot->dq_dqb.dqb_curspace + space > dquot->dq_dqb.dqb_bhardlimit &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 !ignore_hardlimit(dquot)) {
1137 if (!prealloc)
Jan Kara8e893462007-10-16 23:29:31 -07001138 *warntype = QUOTA_NL_BHARDWARN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 return NO_QUOTA;
1140 }
1141
1142 if (dquot->dq_dqb.dqb_bsoftlimit &&
Jan Kara12095462008-08-20 14:45:12 +02001143 dquot->dq_dqb.dqb_curspace + space > dquot->dq_dqb.dqb_bsoftlimit &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 dquot->dq_dqb.dqb_btime && get_seconds() >= dquot->dq_dqb.dqb_btime &&
1145 !ignore_hardlimit(dquot)) {
1146 if (!prealloc)
Jan Kara8e893462007-10-16 23:29:31 -07001147 *warntype = QUOTA_NL_BSOFTLONGWARN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 return NO_QUOTA;
1149 }
1150
1151 if (dquot->dq_dqb.dqb_bsoftlimit &&
Jan Kara12095462008-08-20 14:45:12 +02001152 dquot->dq_dqb.dqb_curspace + space > dquot->dq_dqb.dqb_bsoftlimit &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 dquot->dq_dqb.dqb_btime == 0) {
1154 if (!prealloc) {
Jan Kara8e893462007-10-16 23:29:31 -07001155 *warntype = QUOTA_NL_BSOFTWARN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 dquot->dq_dqb.dqb_btime = get_seconds() + sb_dqopt(dquot->dq_sb)->info[dquot->dq_type].dqi_bgrace;
1157 }
1158 else
1159 /*
1160 * We don't allow preallocation to exceed softlimit so exceeding will
1161 * be always printed
1162 */
1163 return NO_QUOTA;
1164 }
1165
1166 return QUOTA_OK;
1167}
1168
Jan Kara12095462008-08-20 14:45:12 +02001169static int info_idq_free(struct dquot *dquot, qsize_t inodes)
Jan Kara657d3bf2008-07-25 01:46:52 -07001170{
1171 if (test_bit(DQ_FAKE_B, &dquot->dq_flags) ||
Jan Karaf55abc02008-08-20 17:50:32 +02001172 dquot->dq_dqb.dqb_curinodes <= dquot->dq_dqb.dqb_isoftlimit ||
1173 !sb_has_quota_limits_enabled(dquot->dq_sb, dquot->dq_type))
Jan Kara657d3bf2008-07-25 01:46:52 -07001174 return QUOTA_NL_NOWARN;
1175
1176 if (dquot->dq_dqb.dqb_curinodes - inodes <= dquot->dq_dqb.dqb_isoftlimit)
1177 return QUOTA_NL_ISOFTBELOW;
1178 if (dquot->dq_dqb.dqb_curinodes >= dquot->dq_dqb.dqb_ihardlimit &&
1179 dquot->dq_dqb.dqb_curinodes - inodes < dquot->dq_dqb.dqb_ihardlimit)
1180 return QUOTA_NL_IHARDBELOW;
1181 return QUOTA_NL_NOWARN;
1182}
1183
1184static int info_bdq_free(struct dquot *dquot, qsize_t space)
1185{
1186 if (test_bit(DQ_FAKE_B, &dquot->dq_flags) ||
Jan Kara12095462008-08-20 14:45:12 +02001187 dquot->dq_dqb.dqb_curspace <= dquot->dq_dqb.dqb_bsoftlimit)
Jan Kara657d3bf2008-07-25 01:46:52 -07001188 return QUOTA_NL_NOWARN;
1189
Jan Kara12095462008-08-20 14:45:12 +02001190 if (dquot->dq_dqb.dqb_curspace - space <= dquot->dq_dqb.dqb_bsoftlimit)
Jan Kara657d3bf2008-07-25 01:46:52 -07001191 return QUOTA_NL_BSOFTBELOW;
Jan Kara12095462008-08-20 14:45:12 +02001192 if (dquot->dq_dqb.dqb_curspace >= dquot->dq_dqb.dqb_bhardlimit &&
1193 dquot->dq_dqb.dqb_curspace - space < dquot->dq_dqb.dqb_bhardlimit)
Jan Kara657d3bf2008-07-25 01:46:52 -07001194 return QUOTA_NL_BHARDBELOW;
1195 return QUOTA_NL_NOWARN;
1196}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197/*
1198 * Initialize quota pointers in inode
Jan Karacc334122009-01-12 17:23:05 +01001199 * We do things in a bit complicated way but by that we avoid calling
1200 * dqget() and thus filesystem callbacks under dqptr_sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 */
1202int dquot_initialize(struct inode *inode, int type)
1203{
1204 unsigned int id = 0;
1205 int cnt, ret = 0;
Jan Karacc334122009-01-12 17:23:05 +01001206 struct dquot *got[MAXQUOTAS] = { NODQUOT, NODQUOT };
1207 struct super_block *sb = inode->i_sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208
Ingo Molnard3be9152006-03-23 03:00:29 -08001209 /* First test before acquiring mutex - solves deadlocks when we
1210 * re-enter the quota code and are already holding the mutex */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 if (IS_NOQUOTA(inode))
1212 return 0;
Jan Karacc334122009-01-12 17:23:05 +01001213
1214 /* First get references to structures we might need. */
1215 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1216 if (type != -1 && cnt != type)
1217 continue;
1218 switch (cnt) {
1219 case USRQUOTA:
1220 id = inode->i_uid;
1221 break;
1222 case GRPQUOTA:
1223 id = inode->i_gid;
1224 break;
1225 }
1226 got[cnt] = dqget(sb, id, cnt);
1227 }
1228
1229 down_write(&sb_dqopt(sb)->dqptr_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 /* Having dqptr_sem we know NOQUOTA flags can't be altered... */
1231 if (IS_NOQUOTA(inode))
1232 goto out_err;
1233 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1234 if (type != -1 && cnt != type)
1235 continue;
Jan Karacc334122009-01-12 17:23:05 +01001236 /* Avoid races with quotaoff() */
1237 if (!sb_has_quota_active(sb, cnt))
1238 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 if (inode->i_dquot[cnt] == NODQUOT) {
Jan Karacc334122009-01-12 17:23:05 +01001240 inode->i_dquot[cnt] = got[cnt];
1241 got[cnt] = NODQUOT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 }
1243 }
1244out_err:
Jan Karacc334122009-01-12 17:23:05 +01001245 up_write(&sb_dqopt(sb)->dqptr_sem);
1246 /* Drop unused references */
1247 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1248 dqput(got[cnt]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 return ret;
1250}
1251
1252/*
1253 * Release all quotas referenced by inode
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 */
Jan Kara3d9ea252008-10-10 16:12:23 +02001255int dquot_drop(struct inode *inode)
1256{
Jan Karacc334122009-01-12 17:23:05 +01001257 int cnt;
1258 struct dquot *put[MAXQUOTAS];
1259
Jan Kara3d9ea252008-10-10 16:12:23 +02001260 down_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
Jan Karacc334122009-01-12 17:23:05 +01001261 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1262 put[cnt] = inode->i_dquot[cnt];
1263 inode->i_dquot[cnt] = NODQUOT;
1264 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 up_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
Jan Karacc334122009-01-12 17:23:05 +01001266
1267 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1268 dqput(put[cnt]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 return 0;
1270}
1271
Jan Karab85f4b82008-07-25 01:46:50 -07001272/* Wrapper to remove references to quota structures from inode */
1273void vfs_dq_drop(struct inode *inode)
1274{
1275 /* Here we can get arbitrary inode from clear_inode() so we have
1276 * to be careful. OTOH we don't need locking as quota operations
1277 * are allowed to change only at mount time */
1278 if (!IS_NOQUOTA(inode) && inode->i_sb && inode->i_sb->dq_op
1279 && inode->i_sb->dq_op->drop) {
1280 int cnt;
1281 /* Test before calling to rule out calls from proc and such
1282 * where we are not allowed to block. Note that this is
1283 * actually reliable test even without the lock - the caller
1284 * must assure that nobody can come after the DQUOT_DROP and
1285 * add quota pointers back anyway */
1286 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1287 if (inode->i_dquot[cnt] != NODQUOT)
1288 break;
1289 if (cnt < MAXQUOTAS)
1290 inode->i_sb->dq_op->drop(inode);
1291 }
1292}
1293
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294/*
1295 * Following four functions update i_blocks+i_bytes fields and
1296 * quota information (together with appropriate checks)
1297 * NOTE: We absolutely rely on the fact that caller dirties
1298 * the inode (usually macros in quotaops.h care about this) and
1299 * holds a handle for the current transaction so that dquot write and
1300 * inode write go into the same transaction.
1301 */
1302
1303/*
1304 * This operation can block, but only after everything is updated
1305 */
1306int dquot_alloc_space(struct inode *inode, qsize_t number, int warn)
1307{
1308 int cnt, ret = NO_QUOTA;
1309 char warntype[MAXQUOTAS];
1310
Ingo Molnard3be9152006-03-23 03:00:29 -08001311 /* First test before acquiring mutex - solves deadlocks when we
1312 * re-enter the quota code and are already holding the mutex */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 if (IS_NOQUOTA(inode)) {
1314out_add:
1315 inode_add_bytes(inode, number);
1316 return QUOTA_OK;
1317 }
1318 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
Jan Kara8e893462007-10-16 23:29:31 -07001319 warntype[cnt] = QUOTA_NL_NOWARN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320
1321 down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1322 if (IS_NOQUOTA(inode)) { /* Now we can do reliable test... */
1323 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1324 goto out_add;
1325 }
1326 spin_lock(&dq_data_lock);
1327 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1328 if (inode->i_dquot[cnt] == NODQUOT)
1329 continue;
1330 if (check_bdq(inode->i_dquot[cnt], number, warn, warntype+cnt) == NO_QUOTA)
1331 goto warn_put_all;
1332 }
1333 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1334 if (inode->i_dquot[cnt] == NODQUOT)
1335 continue;
1336 dquot_incr_space(inode->i_dquot[cnt], number);
1337 }
1338 inode_add_bytes(inode, number);
1339 ret = QUOTA_OK;
1340warn_put_all:
1341 spin_unlock(&dq_data_lock);
1342 if (ret == QUOTA_OK)
1343 /* Dirtify all the dquots - this can block when journalling */
1344 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1345 if (inode->i_dquot[cnt])
1346 mark_dquot_dirty(inode->i_dquot[cnt]);
1347 flush_warnings(inode->i_dquot, warntype);
1348 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1349 return ret;
1350}
1351
1352/*
1353 * This operation can block, but only after everything is updated
1354 */
Jan Kara12095462008-08-20 14:45:12 +02001355int dquot_alloc_inode(const struct inode *inode, qsize_t number)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356{
1357 int cnt, ret = NO_QUOTA;
1358 char warntype[MAXQUOTAS];
1359
Ingo Molnard3be9152006-03-23 03:00:29 -08001360 /* First test before acquiring mutex - solves deadlocks when we
1361 * re-enter the quota code and are already holding the mutex */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 if (IS_NOQUOTA(inode))
1363 return QUOTA_OK;
1364 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
Jan Kara8e893462007-10-16 23:29:31 -07001365 warntype[cnt] = QUOTA_NL_NOWARN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1367 if (IS_NOQUOTA(inode)) {
1368 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1369 return QUOTA_OK;
1370 }
1371 spin_lock(&dq_data_lock);
1372 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1373 if (inode->i_dquot[cnt] == NODQUOT)
1374 continue;
1375 if (check_idq(inode->i_dquot[cnt], number, warntype+cnt) == NO_QUOTA)
1376 goto warn_put_all;
1377 }
1378
1379 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1380 if (inode->i_dquot[cnt] == NODQUOT)
1381 continue;
1382 dquot_incr_inodes(inode->i_dquot[cnt], number);
1383 }
1384 ret = QUOTA_OK;
1385warn_put_all:
1386 spin_unlock(&dq_data_lock);
1387 if (ret == QUOTA_OK)
1388 /* Dirtify all the dquots - this can block when journalling */
1389 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1390 if (inode->i_dquot[cnt])
1391 mark_dquot_dirty(inode->i_dquot[cnt]);
Jan Kara087ee8d2007-12-17 16:20:26 -08001392 flush_warnings(inode->i_dquot, warntype);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1394 return ret;
1395}
1396
1397/*
1398 * This operation can block, but only after everything is updated
1399 */
1400int dquot_free_space(struct inode *inode, qsize_t number)
1401{
1402 unsigned int cnt;
Jan Kara657d3bf2008-07-25 01:46:52 -07001403 char warntype[MAXQUOTAS];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404
Ingo Molnard3be9152006-03-23 03:00:29 -08001405 /* First test before acquiring mutex - solves deadlocks when we
1406 * re-enter the quota code and are already holding the mutex */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407 if (IS_NOQUOTA(inode)) {
1408out_sub:
1409 inode_sub_bytes(inode, number);
1410 return QUOTA_OK;
1411 }
Jan Kara657d3bf2008-07-25 01:46:52 -07001412
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1414 /* Now recheck reliably when holding dqptr_sem */
1415 if (IS_NOQUOTA(inode)) {
1416 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1417 goto out_sub;
1418 }
1419 spin_lock(&dq_data_lock);
1420 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1421 if (inode->i_dquot[cnt] == NODQUOT)
1422 continue;
Jan Kara657d3bf2008-07-25 01:46:52 -07001423 warntype[cnt] = info_bdq_free(inode->i_dquot[cnt], number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 dquot_decr_space(inode->i_dquot[cnt], number);
1425 }
1426 inode_sub_bytes(inode, number);
1427 spin_unlock(&dq_data_lock);
1428 /* Dirtify all the dquots - this can block when journalling */
1429 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1430 if (inode->i_dquot[cnt])
1431 mark_dquot_dirty(inode->i_dquot[cnt]);
Jan Kara657d3bf2008-07-25 01:46:52 -07001432 flush_warnings(inode->i_dquot, warntype);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1434 return QUOTA_OK;
1435}
1436
1437/*
1438 * This operation can block, but only after everything is updated
1439 */
Jan Kara12095462008-08-20 14:45:12 +02001440int dquot_free_inode(const struct inode *inode, qsize_t number)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441{
1442 unsigned int cnt;
Jan Kara657d3bf2008-07-25 01:46:52 -07001443 char warntype[MAXQUOTAS];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444
Ingo Molnard3be9152006-03-23 03:00:29 -08001445 /* First test before acquiring mutex - solves deadlocks when we
1446 * re-enter the quota code and are already holding the mutex */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447 if (IS_NOQUOTA(inode))
1448 return QUOTA_OK;
Jan Kara657d3bf2008-07-25 01:46:52 -07001449
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450 down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1451 /* Now recheck reliably when holding dqptr_sem */
1452 if (IS_NOQUOTA(inode)) {
1453 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1454 return QUOTA_OK;
1455 }
1456 spin_lock(&dq_data_lock);
1457 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1458 if (inode->i_dquot[cnt] == NODQUOT)
1459 continue;
Jan Kara657d3bf2008-07-25 01:46:52 -07001460 warntype[cnt] = info_idq_free(inode->i_dquot[cnt], number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 dquot_decr_inodes(inode->i_dquot[cnt], number);
1462 }
1463 spin_unlock(&dq_data_lock);
1464 /* Dirtify all the dquots - this can block when journalling */
1465 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1466 if (inode->i_dquot[cnt])
1467 mark_dquot_dirty(inode->i_dquot[cnt]);
Jan Kara657d3bf2008-07-25 01:46:52 -07001468 flush_warnings(inode->i_dquot, warntype);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1470 return QUOTA_OK;
1471}
1472
1473/*
1474 * Transfer the number of inode and blocks from one diskquota to an other.
1475 *
1476 * This operation can block, but only after everything is updated
1477 * A transaction must be started when entering this function.
1478 */
1479int dquot_transfer(struct inode *inode, struct iattr *iattr)
1480{
1481 qsize_t space;
1482 struct dquot *transfer_from[MAXQUOTAS];
1483 struct dquot *transfer_to[MAXQUOTAS];
Jan Karacc334122009-01-12 17:23:05 +01001484 int cnt, ret = QUOTA_OK;
1485 int chuid = iattr->ia_valid & ATTR_UID && inode->i_uid != iattr->ia_uid,
1486 chgid = iattr->ia_valid & ATTR_GID && inode->i_gid != iattr->ia_gid;
Jan Kara657d3bf2008-07-25 01:46:52 -07001487 char warntype_to[MAXQUOTAS];
1488 char warntype_from_inodes[MAXQUOTAS], warntype_from_space[MAXQUOTAS];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489
Ingo Molnard3be9152006-03-23 03:00:29 -08001490 /* First test before acquiring mutex - solves deadlocks when we
1491 * re-enter the quota code and are already holding the mutex */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492 if (IS_NOQUOTA(inode))
1493 return QUOTA_OK;
Jan Karacc334122009-01-12 17:23:05 +01001494 /* Initialize the arrays */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
Jan Karacc334122009-01-12 17:23:05 +01001496 transfer_from[cnt] = NODQUOT;
1497 transfer_to[cnt] = NODQUOT;
Jan Kara657d3bf2008-07-25 01:46:52 -07001498 warntype_to[cnt] = QUOTA_NL_NOWARN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 switch (cnt) {
1500 case USRQUOTA:
1501 if (!chuid)
1502 continue;
1503 transfer_to[cnt] = dqget(inode->i_sb, iattr->ia_uid, cnt);
1504 break;
1505 case GRPQUOTA:
1506 if (!chgid)
1507 continue;
1508 transfer_to[cnt] = dqget(inode->i_sb, iattr->ia_gid, cnt);
1509 break;
1510 }
1511 }
Jan Karacc334122009-01-12 17:23:05 +01001512
1513 down_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
1514 /* Now recheck reliably when holding dqptr_sem */
1515 if (IS_NOQUOTA(inode)) { /* File without quota accounting? */
1516 up_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
1517 goto put_all;
1518 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519 spin_lock(&dq_data_lock);
1520 space = inode_get_bytes(inode);
1521 /* Build the transfer_from list and check the limits */
1522 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1523 if (transfer_to[cnt] == NODQUOT)
1524 continue;
1525 transfer_from[cnt] = inode->i_dquot[cnt];
Jan Kara657d3bf2008-07-25 01:46:52 -07001526 if (check_idq(transfer_to[cnt], 1, warntype_to + cnt) ==
1527 NO_QUOTA || check_bdq(transfer_to[cnt], space, 0,
1528 warntype_to + cnt) == NO_QUOTA)
Jan Karacc334122009-01-12 17:23:05 +01001529 goto over_quota;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530 }
1531
1532 /*
1533 * Finally perform the needed transfer from transfer_from to transfer_to
1534 */
1535 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1536 /*
1537 * Skip changes for same uid or gid or for turned off quota-type.
1538 */
1539 if (transfer_to[cnt] == NODQUOT)
1540 continue;
1541
1542 /* Due to IO error we might not have transfer_from[] structure */
1543 if (transfer_from[cnt]) {
Jan Kara657d3bf2008-07-25 01:46:52 -07001544 warntype_from_inodes[cnt] =
1545 info_idq_free(transfer_from[cnt], 1);
1546 warntype_from_space[cnt] =
1547 info_bdq_free(transfer_from[cnt], space);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548 dquot_decr_inodes(transfer_from[cnt], 1);
1549 dquot_decr_space(transfer_from[cnt], space);
1550 }
1551
1552 dquot_incr_inodes(transfer_to[cnt], 1);
1553 dquot_incr_space(transfer_to[cnt], space);
1554
1555 inode->i_dquot[cnt] = transfer_to[cnt];
1556 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557 spin_unlock(&dq_data_lock);
Jan Karacc334122009-01-12 17:23:05 +01001558 up_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
1559
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560 /* Dirtify all the dquots - this can block when journalling */
1561 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1562 if (transfer_from[cnt])
1563 mark_dquot_dirty(transfer_from[cnt]);
Jan Karacc334122009-01-12 17:23:05 +01001564 if (transfer_to[cnt]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565 mark_dquot_dirty(transfer_to[cnt]);
Jan Karacc334122009-01-12 17:23:05 +01001566 /* The reference we got is transferred to the inode */
1567 transfer_to[cnt] = NODQUOT;
1568 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569 }
Jan Karacc334122009-01-12 17:23:05 +01001570warn_put_all:
Jan Kara657d3bf2008-07-25 01:46:52 -07001571 flush_warnings(transfer_to, warntype_to);
1572 flush_warnings(transfer_from, warntype_from_inodes);
1573 flush_warnings(transfer_from, warntype_from_space);
Jan Karacc334122009-01-12 17:23:05 +01001574put_all:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
Jan Karacc334122009-01-12 17:23:05 +01001576 dqput(transfer_from[cnt]);
1577 dqput(transfer_to[cnt]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579 return ret;
Jan Karacc334122009-01-12 17:23:05 +01001580over_quota:
1581 spin_unlock(&dq_data_lock);
1582 up_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
1583 /* Clear dquot pointers we don't want to dqput() */
1584 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1585 transfer_from[cnt] = NODQUOT;
1586 ret = NO_QUOTA;
1587 goto warn_put_all;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588}
1589
Jan Karab85f4b82008-07-25 01:46:50 -07001590/* Wrapper for transferring ownership of an inode */
1591int vfs_dq_transfer(struct inode *inode, struct iattr *iattr)
1592{
Jan Karaf55abc02008-08-20 17:50:32 +02001593 if (sb_any_quota_active(inode->i_sb) && !IS_NOQUOTA(inode)) {
Jan Karab85f4b82008-07-25 01:46:50 -07001594 vfs_dq_init(inode);
1595 if (inode->i_sb->dq_op->transfer(inode, iattr) == NO_QUOTA)
1596 return 1;
1597 }
1598 return 0;
1599}
1600
1601
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602/*
1603 * Write info of quota file to disk
1604 */
1605int dquot_commit_info(struct super_block *sb, int type)
1606{
1607 int ret;
1608 struct quota_info *dqopt = sb_dqopt(sb);
1609
Ingo Molnard3be9152006-03-23 03:00:29 -08001610 mutex_lock(&dqopt->dqio_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611 ret = dqopt->ops[type]->write_file_info(sb, type);
Ingo Molnard3be9152006-03-23 03:00:29 -08001612 mutex_unlock(&dqopt->dqio_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613 return ret;
1614}
1615
1616/*
1617 * Definitions of diskquota operations.
1618 */
1619struct dquot_operations dquot_operations = {
1620 .initialize = dquot_initialize,
1621 .drop = dquot_drop,
1622 .alloc_space = dquot_alloc_space,
1623 .alloc_inode = dquot_alloc_inode,
1624 .free_space = dquot_free_space,
1625 .free_inode = dquot_free_inode,
1626 .transfer = dquot_transfer,
1627 .write_dquot = dquot_commit,
1628 .acquire_dquot = dquot_acquire,
1629 .release_dquot = dquot_release,
1630 .mark_dirty = dquot_mark_dquot_dirty,
Jan Kara74f783a2008-08-19 14:51:22 +02001631 .write_info = dquot_commit_info,
1632 .alloc_dquot = dquot_alloc,
1633 .destroy_dquot = dquot_destroy,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634};
1635
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636/*
1637 * Turn quota off on a device. type == -1 ==> quotaoff for all types (umount)
1638 */
Jan Karaf55abc02008-08-20 17:50:32 +02001639int vfs_quota_disable(struct super_block *sb, int type, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640{
Jan Kara0ff5af82008-04-28 02:14:33 -07001641 int cnt, ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642 struct quota_info *dqopt = sb_dqopt(sb);
1643 struct inode *toputinode[MAXQUOTAS];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644
Jan Karaf55abc02008-08-20 17:50:32 +02001645 /* Cannot turn off usage accounting without turning off limits, or
1646 * suspend quotas and simultaneously turn quotas off. */
1647 if ((flags & DQUOT_USAGE_ENABLED && !(flags & DQUOT_LIMITS_ENABLED))
1648 || (flags & DQUOT_SUSPENDED && flags & (DQUOT_LIMITS_ENABLED |
1649 DQUOT_USAGE_ENABLED)))
1650 return -EINVAL;
1651
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652 /* We need to serialize quota_off() for device */
Ingo Molnard3be9152006-03-23 03:00:29 -08001653 mutex_lock(&dqopt->dqonoff_mutex);
Jan Kara9377abd2008-05-12 14:02:08 -07001654
1655 /*
1656 * Skip everything if there's nothing to do. We have to do this because
1657 * sometimes we are called when fill_super() failed and calling
1658 * sync_fs() in such cases does no good.
1659 */
Jan Karaf55abc02008-08-20 17:50:32 +02001660 if (!sb_any_quota_loaded(sb)) {
Jan Kara9377abd2008-05-12 14:02:08 -07001661 mutex_unlock(&dqopt->dqonoff_mutex);
1662 return 0;
1663 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1665 toputinode[cnt] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666 if (type != -1 && cnt != type)
1667 continue;
Jan Karaf55abc02008-08-20 17:50:32 +02001668 if (!sb_has_quota_loaded(sb, cnt))
Jan Kara0ff5af82008-04-28 02:14:33 -07001669 continue;
Jan Karaf55abc02008-08-20 17:50:32 +02001670
1671 if (flags & DQUOT_SUSPENDED) {
Jan Karacc334122009-01-12 17:23:05 +01001672 spin_lock(&dq_state_lock);
Jan Karaf55abc02008-08-20 17:50:32 +02001673 dqopt->flags |=
1674 dquot_state_flag(DQUOT_SUSPENDED, cnt);
Jan Karacc334122009-01-12 17:23:05 +01001675 spin_unlock(&dq_state_lock);
Jan Karaf55abc02008-08-20 17:50:32 +02001676 } else {
Jan Karacc334122009-01-12 17:23:05 +01001677 spin_lock(&dq_state_lock);
Jan Karaf55abc02008-08-20 17:50:32 +02001678 dqopt->flags &= ~dquot_state_flag(flags, cnt);
1679 /* Turning off suspended quotas? */
1680 if (!sb_has_quota_loaded(sb, cnt) &&
1681 sb_has_quota_suspended(sb, cnt)) {
1682 dqopt->flags &= ~dquot_state_flag(
1683 DQUOT_SUSPENDED, cnt);
Jan Karacc334122009-01-12 17:23:05 +01001684 spin_unlock(&dq_state_lock);
Jan Karaf55abc02008-08-20 17:50:32 +02001685 iput(dqopt->files[cnt]);
1686 dqopt->files[cnt] = NULL;
1687 continue;
1688 }
Jan Karacc334122009-01-12 17:23:05 +01001689 spin_unlock(&dq_state_lock);
Jan Kara0ff5af82008-04-28 02:14:33 -07001690 }
Jan Karaf55abc02008-08-20 17:50:32 +02001691
1692 /* We still have to keep quota loaded? */
1693 if (sb_has_quota_loaded(sb, cnt) && !(flags & DQUOT_SUSPENDED))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695
1696 /* Note: these are blocking operations */
1697 drop_dquot_ref(sb, cnt);
1698 invalidate_dquots(sb, cnt);
1699 /*
1700 * Now all dquots should be invalidated, all writes done so we should be only
1701 * users of the info. No locks needed.
1702 */
1703 if (info_dirty(&dqopt->info[cnt]))
1704 sb->dq_op->write_info(sb, cnt);
1705 if (dqopt->ops[cnt]->free_file_info)
1706 dqopt->ops[cnt]->free_file_info(sb, cnt);
1707 put_quota_format(dqopt->info[cnt].dqi_format);
1708
1709 toputinode[cnt] = dqopt->files[cnt];
Jan Karaf55abc02008-08-20 17:50:32 +02001710 if (!sb_has_quota_loaded(sb, cnt))
Jan Kara0ff5af82008-04-28 02:14:33 -07001711 dqopt->files[cnt] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712 dqopt->info[cnt].dqi_flags = 0;
1713 dqopt->info[cnt].dqi_igrace = 0;
1714 dqopt->info[cnt].dqi_bgrace = 0;
1715 dqopt->ops[cnt] = NULL;
1716 }
Ingo Molnard3be9152006-03-23 03:00:29 -08001717 mutex_unlock(&dqopt->dqonoff_mutex);
Jan Karaca785ec2008-09-30 17:53:37 +02001718
1719 /* Skip syncing and setting flags if quota files are hidden */
1720 if (dqopt->flags & DQUOT_QUOTA_SYS_FILE)
1721 goto put_inodes;
1722
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723 /* Sync the superblock so that buffers with quota data are written to
Al Viro7b7b1ac2005-11-07 17:13:39 -05001724 * disk (and so userspace sees correct data afterwards). */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725 if (sb->s_op->sync_fs)
1726 sb->s_op->sync_fs(sb, 1);
1727 sync_blockdev(sb->s_bdev);
1728 /* Now the quota files are just ordinary files and we can set the
1729 * inode flags back. Moreover we discard the pagecache so that
1730 * userspace sees the writes we did bypassing the pagecache. We
1731 * must also discard the blockdev buffers so that we see the
1732 * changes done by userspace on the next quotaon() */
1733 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1734 if (toputinode[cnt]) {
Ingo Molnard3be9152006-03-23 03:00:29 -08001735 mutex_lock(&dqopt->dqonoff_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736 /* If quota was reenabled in the meantime, we have
1737 * nothing to do */
Jan Karaf55abc02008-08-20 17:50:32 +02001738 if (!sb_has_quota_loaded(sb, cnt)) {
Jan Kara79254092007-05-16 22:11:19 -07001739 mutex_lock_nested(&toputinode[cnt]->i_mutex, I_MUTEX_QUOTA);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740 toputinode[cnt]->i_flags &= ~(S_IMMUTABLE |
1741 S_NOATIME | S_NOQUOTA);
1742 truncate_inode_pages(&toputinode[cnt]->i_data, 0);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001743 mutex_unlock(&toputinode[cnt]->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744 mark_inode_dirty(toputinode[cnt]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745 }
Ingo Molnard3be9152006-03-23 03:00:29 -08001746 mutex_unlock(&dqopt->dqonoff_mutex);
Jan Karaca785ec2008-09-30 17:53:37 +02001747 }
1748 if (sb->s_bdev)
1749 invalidate_bdev(sb->s_bdev);
1750put_inodes:
1751 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1752 if (toputinode[cnt]) {
Jan Kara0ff5af82008-04-28 02:14:33 -07001753 /* On remount RO, we keep the inode pointer so that we
Jan Karaf55abc02008-08-20 17:50:32 +02001754 * can reenable quota on the subsequent remount RW. We
1755 * have to check 'flags' variable and not use sb_has_
1756 * function because another quotaon / quotaoff could
1757 * change global state before we got here. We refuse
1758 * to suspend quotas when there is pending delete on
1759 * the quota file... */
1760 if (!(flags & DQUOT_SUSPENDED))
Jan Kara0ff5af82008-04-28 02:14:33 -07001761 iput(toputinode[cnt]);
1762 else if (!toputinode[cnt]->i_nlink)
1763 ret = -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764 }
Jan Kara0ff5af82008-04-28 02:14:33 -07001765 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766}
1767
Jan Karaf55abc02008-08-20 17:50:32 +02001768int vfs_quota_off(struct super_block *sb, int type, int remount)
1769{
1770 return vfs_quota_disable(sb, type, remount ? DQUOT_SUSPENDED :
1771 (DQUOT_USAGE_ENABLED | DQUOT_LIMITS_ENABLED));
1772}
1773
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774/*
1775 * Turn quotas on on a device
1776 */
1777
Jan Karaf55abc02008-08-20 17:50:32 +02001778/*
1779 * Helper function to turn quotas on when we already have the inode of
1780 * quota file and no quota information is loaded.
1781 */
1782static int vfs_load_quota_inode(struct inode *inode, int type, int format_id,
1783 unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784{
1785 struct quota_format_type *fmt = find_quota_format(format_id);
1786 struct super_block *sb = inode->i_sb;
1787 struct quota_info *dqopt = sb_dqopt(sb);
1788 int error;
1789 int oldflags = -1;
1790
1791 if (!fmt)
1792 return -ESRCH;
1793 if (!S_ISREG(inode->i_mode)) {
1794 error = -EACCES;
1795 goto out_fmt;
1796 }
1797 if (IS_RDONLY(inode)) {
1798 error = -EROFS;
1799 goto out_fmt;
1800 }
1801 if (!sb->s_op->quota_write || !sb->s_op->quota_read) {
1802 error = -EINVAL;
1803 goto out_fmt;
1804 }
Jan Karaf55abc02008-08-20 17:50:32 +02001805 /* Usage always has to be set... */
1806 if (!(flags & DQUOT_USAGE_ENABLED)) {
1807 error = -EINVAL;
1808 goto out_fmt;
1809 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810
Jan Karaca785ec2008-09-30 17:53:37 +02001811 if (!(dqopt->flags & DQUOT_QUOTA_SYS_FILE)) {
1812 /* As we bypass the pagecache we must now flush the inode so
1813 * that we see all the changes from userspace... */
1814 write_inode_now(inode, 1);
1815 /* And now flush the block cache so that kernel sees the
1816 * changes */
1817 invalidate_bdev(sb->s_bdev);
1818 }
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001819 mutex_lock(&inode->i_mutex);
Ingo Molnard3be9152006-03-23 03:00:29 -08001820 mutex_lock(&dqopt->dqonoff_mutex);
Jan Karaf55abc02008-08-20 17:50:32 +02001821 if (sb_has_quota_loaded(sb, type)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822 error = -EBUSY;
1823 goto out_lock;
1824 }
Jan Karaca785ec2008-09-30 17:53:37 +02001825
1826 if (!(dqopt->flags & DQUOT_QUOTA_SYS_FILE)) {
1827 /* We don't want quota and atime on quota files (deadlocks
1828 * possible) Also nobody should write to the file - we use
1829 * special IO operations which ignore the immutable bit. */
1830 down_write(&dqopt->dqptr_sem);
1831 oldflags = inode->i_flags & (S_NOATIME | S_IMMUTABLE | S_NOQUOTA);
1832 inode->i_flags |= S_NOQUOTA | S_NOATIME | S_IMMUTABLE;
1833 up_write(&dqopt->dqptr_sem);
1834 sb->dq_op->drop(inode);
1835 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836
1837 error = -EIO;
1838 dqopt->files[type] = igrab(inode);
1839 if (!dqopt->files[type])
1840 goto out_lock;
1841 error = -EINVAL;
1842 if (!fmt->qf_ops->check_quota_file(sb, type))
1843 goto out_file_init;
1844
1845 dqopt->ops[type] = fmt->qf_ops;
1846 dqopt->info[type].dqi_format = fmt;
Jan Kara0ff5af82008-04-28 02:14:33 -07001847 dqopt->info[type].dqi_fmt_id = format_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848 INIT_LIST_HEAD(&dqopt->info[type].dqi_dirty_list);
Ingo Molnard3be9152006-03-23 03:00:29 -08001849 mutex_lock(&dqopt->dqio_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850 if ((error = dqopt->ops[type]->read_file_info(sb, type)) < 0) {
Ingo Molnard3be9152006-03-23 03:00:29 -08001851 mutex_unlock(&dqopt->dqio_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852 goto out_file_init;
1853 }
Ingo Molnard3be9152006-03-23 03:00:29 -08001854 mutex_unlock(&dqopt->dqio_mutex);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001855 mutex_unlock(&inode->i_mutex);
Jan Karacc334122009-01-12 17:23:05 +01001856 spin_lock(&dq_state_lock);
Jan Karaf55abc02008-08-20 17:50:32 +02001857 dqopt->flags |= dquot_state_flag(flags, type);
Jan Karacc334122009-01-12 17:23:05 +01001858 spin_unlock(&dq_state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859
1860 add_dquot_ref(sb, type);
Ingo Molnard3be9152006-03-23 03:00:29 -08001861 mutex_unlock(&dqopt->dqonoff_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862
1863 return 0;
1864
1865out_file_init:
1866 dqopt->files[type] = NULL;
1867 iput(inode);
1868out_lock:
Ingo Molnard3be9152006-03-23 03:00:29 -08001869 mutex_unlock(&dqopt->dqonoff_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870 if (oldflags != -1) {
1871 down_write(&dqopt->dqptr_sem);
1872 /* Set the flags back (in the case of accidental quotaon()
1873 * on a wrong file we don't want to mess up the flags) */
1874 inode->i_flags &= ~(S_NOATIME | S_NOQUOTA | S_IMMUTABLE);
1875 inode->i_flags |= oldflags;
1876 up_write(&dqopt->dqptr_sem);
1877 }
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001878 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879out_fmt:
1880 put_quota_format(fmt);
1881
1882 return error;
1883}
1884
Jan Kara0ff5af82008-04-28 02:14:33 -07001885/* Reenable quotas on remount RW */
1886static int vfs_quota_on_remount(struct super_block *sb, int type)
1887{
1888 struct quota_info *dqopt = sb_dqopt(sb);
1889 struct inode *inode;
1890 int ret;
Jan Karaf55abc02008-08-20 17:50:32 +02001891 unsigned int flags;
Jan Kara0ff5af82008-04-28 02:14:33 -07001892
1893 mutex_lock(&dqopt->dqonoff_mutex);
1894 if (!sb_has_quota_suspended(sb, type)) {
1895 mutex_unlock(&dqopt->dqonoff_mutex);
1896 return 0;
1897 }
Jan Kara0ff5af82008-04-28 02:14:33 -07001898 inode = dqopt->files[type];
1899 dqopt->files[type] = NULL;
Jan Karacc334122009-01-12 17:23:05 +01001900 spin_lock(&dq_state_lock);
Jan Karaf55abc02008-08-20 17:50:32 +02001901 flags = dqopt->flags & dquot_state_flag(DQUOT_USAGE_ENABLED |
1902 DQUOT_LIMITS_ENABLED, type);
1903 dqopt->flags &= ~dquot_state_flag(DQUOT_STATE_FLAGS, type);
Jan Karacc334122009-01-12 17:23:05 +01001904 spin_unlock(&dq_state_lock);
Jan Kara0ff5af82008-04-28 02:14:33 -07001905 mutex_unlock(&dqopt->dqonoff_mutex);
1906
Jan Karaf55abc02008-08-20 17:50:32 +02001907 flags = dquot_generic_flag(flags, type);
1908 ret = vfs_load_quota_inode(inode, type, dqopt->info[type].dqi_fmt_id,
1909 flags);
Jan Kara0ff5af82008-04-28 02:14:33 -07001910 iput(inode);
1911
1912 return ret;
1913}
1914
Al Viro77e69da2008-08-01 04:29:18 -04001915int vfs_quota_on_path(struct super_block *sb, int type, int format_id,
1916 struct path *path)
1917{
1918 int error = security_quota_on(path->dentry);
1919 if (error)
1920 return error;
1921 /* Quota file not on the same filesystem? */
1922 if (path->mnt->mnt_sb != sb)
1923 error = -EXDEV;
1924 else
Jan Karaf55abc02008-08-20 17:50:32 +02001925 error = vfs_load_quota_inode(path->dentry->d_inode, type,
1926 format_id, DQUOT_USAGE_ENABLED |
1927 DQUOT_LIMITS_ENABLED);
Al Viro77e69da2008-08-01 04:29:18 -04001928 return error;
1929}
1930
Al Viro82646132008-08-02 00:57:06 -04001931int vfs_quota_on(struct super_block *sb, int type, int format_id, char *name,
Jan Kara0ff5af82008-04-28 02:14:33 -07001932 int remount)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933{
Al Viro82646132008-08-02 00:57:06 -04001934 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935 int error;
1936
Jan Kara0ff5af82008-04-28 02:14:33 -07001937 if (remount)
1938 return vfs_quota_on_remount(sb, type);
1939
Al Viro82646132008-08-02 00:57:06 -04001940 error = kern_path(name, LOOKUP_FOLLOW, &path);
Al Viro77e69da2008-08-01 04:29:18 -04001941 if (!error) {
Al Viro82646132008-08-02 00:57:06 -04001942 error = vfs_quota_on_path(sb, type, format_id, &path);
1943 path_put(&path);
Al Viro77e69da2008-08-01 04:29:18 -04001944 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945 return error;
1946}
1947
1948/*
Jan Karaf55abc02008-08-20 17:50:32 +02001949 * More powerful function for turning on quotas allowing setting
1950 * of individual quota flags
1951 */
1952int vfs_quota_enable(struct inode *inode, int type, int format_id,
1953 unsigned int flags)
1954{
1955 int ret = 0;
1956 struct super_block *sb = inode->i_sb;
1957 struct quota_info *dqopt = sb_dqopt(sb);
1958
1959 /* Just unsuspend quotas? */
1960 if (flags & DQUOT_SUSPENDED)
1961 return vfs_quota_on_remount(sb, type);
1962 if (!flags)
1963 return 0;
1964 /* Just updating flags needed? */
1965 if (sb_has_quota_loaded(sb, type)) {
1966 mutex_lock(&dqopt->dqonoff_mutex);
1967 /* Now do a reliable test... */
1968 if (!sb_has_quota_loaded(sb, type)) {
1969 mutex_unlock(&dqopt->dqonoff_mutex);
1970 goto load_quota;
1971 }
1972 if (flags & DQUOT_USAGE_ENABLED &&
1973 sb_has_quota_usage_enabled(sb, type)) {
1974 ret = -EBUSY;
1975 goto out_lock;
1976 }
1977 if (flags & DQUOT_LIMITS_ENABLED &&
1978 sb_has_quota_limits_enabled(sb, type)) {
1979 ret = -EBUSY;
1980 goto out_lock;
1981 }
Jan Karacc334122009-01-12 17:23:05 +01001982 spin_lock(&dq_state_lock);
Jan Karaf55abc02008-08-20 17:50:32 +02001983 sb_dqopt(sb)->flags |= dquot_state_flag(flags, type);
Jan Karacc334122009-01-12 17:23:05 +01001984 spin_unlock(&dq_state_lock);
Jan Karaf55abc02008-08-20 17:50:32 +02001985out_lock:
1986 mutex_unlock(&dqopt->dqonoff_mutex);
1987 return ret;
1988 }
1989
1990load_quota:
1991 return vfs_load_quota_inode(inode, type, format_id, flags);
1992}
1993
1994/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995 * This function is used when filesystem needs to initialize quotas
1996 * during mount time.
1997 */
Christoph Hellwig84de8562005-06-23 00:09:16 -07001998int vfs_quota_on_mount(struct super_block *sb, char *qf_name,
1999 int format_id, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000{
Christoph Hellwig84de8562005-06-23 00:09:16 -07002001 struct dentry *dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002 int error;
2003
Christoph Hellwig2fa389c2005-06-23 00:09:16 -07002004 dentry = lookup_one_len(qf_name, sb->s_root, strlen(qf_name));
Christoph Hellwig84de8562005-06-23 00:09:16 -07002005 if (IS_ERR(dentry))
2006 return PTR_ERR(dentry);
2007
Jan Kara154f4842005-11-28 13:44:14 -08002008 if (!dentry->d_inode) {
2009 error = -ENOENT;
2010 goto out;
2011 }
2012
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013 error = security_quota_on(dentry);
Christoph Hellwig84de8562005-06-23 00:09:16 -07002014 if (!error)
Jan Karaf55abc02008-08-20 17:50:32 +02002015 error = vfs_load_quota_inode(dentry->d_inode, type, format_id,
2016 DQUOT_USAGE_ENABLED | DQUOT_LIMITS_ENABLED);
Christoph Hellwig84de8562005-06-23 00:09:16 -07002017
Jan Kara154f4842005-11-28 13:44:14 -08002018out:
Christoph Hellwig84de8562005-06-23 00:09:16 -07002019 dput(dentry);
2020 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021}
2022
Jan Karab85f4b82008-07-25 01:46:50 -07002023/* Wrapper to turn on quotas when remounting rw */
2024int vfs_dq_quota_on_remount(struct super_block *sb)
2025{
2026 int cnt;
2027 int ret = 0, err;
2028
2029 if (!sb->s_qcop || !sb->s_qcop->quota_on)
2030 return -ENOSYS;
2031 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
2032 err = sb->s_qcop->quota_on(sb, cnt, 0, NULL, 1);
2033 if (err < 0 && !ret)
2034 ret = err;
2035 }
2036 return ret;
2037}
2038
Jan Kara12095462008-08-20 14:45:12 +02002039static inline qsize_t qbtos(qsize_t blocks)
2040{
2041 return blocks << QIF_DQBLKSIZE_BITS;
2042}
2043
2044static inline qsize_t stoqb(qsize_t space)
2045{
2046 return (space + QIF_DQBLKSIZE - 1) >> QIF_DQBLKSIZE_BITS;
2047}
2048
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049/* Generic routine for getting common part of quota structure */
2050static void do_get_dqblk(struct dquot *dquot, struct if_dqblk *di)
2051{
2052 struct mem_dqblk *dm = &dquot->dq_dqb;
2053
2054 spin_lock(&dq_data_lock);
Jan Kara12095462008-08-20 14:45:12 +02002055 di->dqb_bhardlimit = stoqb(dm->dqb_bhardlimit);
2056 di->dqb_bsoftlimit = stoqb(dm->dqb_bsoftlimit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057 di->dqb_curspace = dm->dqb_curspace;
2058 di->dqb_ihardlimit = dm->dqb_ihardlimit;
2059 di->dqb_isoftlimit = dm->dqb_isoftlimit;
2060 di->dqb_curinodes = dm->dqb_curinodes;
2061 di->dqb_btime = dm->dqb_btime;
2062 di->dqb_itime = dm->dqb_itime;
2063 di->dqb_valid = QIF_ALL;
2064 spin_unlock(&dq_data_lock);
2065}
2066
2067int vfs_get_dqblk(struct super_block *sb, int type, qid_t id, struct if_dqblk *di)
2068{
2069 struct dquot *dquot;
2070
Jan Karacc334122009-01-12 17:23:05 +01002071 dquot = dqget(sb, id, type);
2072 if (dquot == NODQUOT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073 return -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074 do_get_dqblk(dquot, di);
2075 dqput(dquot);
Jan Karacc334122009-01-12 17:23:05 +01002076
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077 return 0;
2078}
2079
2080/* Generic routine for setting common part of quota structure */
Andrew Perepechko338bf9a2008-04-28 02:14:31 -07002081static int do_set_dqblk(struct dquot *dquot, struct if_dqblk *di)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082{
2083 struct mem_dqblk *dm = &dquot->dq_dqb;
2084 int check_blim = 0, check_ilim = 0;
Andrew Perepechko338bf9a2008-04-28 02:14:31 -07002085 struct mem_dqinfo *dqi = &sb_dqopt(dquot->dq_sb)->info[dquot->dq_type];
2086
2087 if ((di->dqb_valid & QIF_BLIMITS &&
2088 (di->dqb_bhardlimit > dqi->dqi_maxblimit ||
2089 di->dqb_bsoftlimit > dqi->dqi_maxblimit)) ||
2090 (di->dqb_valid & QIF_ILIMITS &&
2091 (di->dqb_ihardlimit > dqi->dqi_maxilimit ||
2092 di->dqb_isoftlimit > dqi->dqi_maxilimit)))
2093 return -ERANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094
2095 spin_lock(&dq_data_lock);
2096 if (di->dqb_valid & QIF_SPACE) {
2097 dm->dqb_curspace = di->dqb_curspace;
2098 check_blim = 1;
Jan Kara4d59bce2008-10-02 16:48:10 +02002099 __set_bit(DQ_LASTSET_B + QIF_SPACE_B, &dquot->dq_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100 }
2101 if (di->dqb_valid & QIF_BLIMITS) {
Jan Kara12095462008-08-20 14:45:12 +02002102 dm->dqb_bsoftlimit = qbtos(di->dqb_bsoftlimit);
2103 dm->dqb_bhardlimit = qbtos(di->dqb_bhardlimit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104 check_blim = 1;
Jan Kara4d59bce2008-10-02 16:48:10 +02002105 __set_bit(DQ_LASTSET_B + QIF_BLIMITS_B, &dquot->dq_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106 }
2107 if (di->dqb_valid & QIF_INODES) {
2108 dm->dqb_curinodes = di->dqb_curinodes;
2109 check_ilim = 1;
Jan Kara4d59bce2008-10-02 16:48:10 +02002110 __set_bit(DQ_LASTSET_B + QIF_INODES_B, &dquot->dq_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111 }
2112 if (di->dqb_valid & QIF_ILIMITS) {
2113 dm->dqb_isoftlimit = di->dqb_isoftlimit;
2114 dm->dqb_ihardlimit = di->dqb_ihardlimit;
2115 check_ilim = 1;
Jan Kara4d59bce2008-10-02 16:48:10 +02002116 __set_bit(DQ_LASTSET_B + QIF_ILIMITS_B, &dquot->dq_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117 }
Jan Kara4d59bce2008-10-02 16:48:10 +02002118 if (di->dqb_valid & QIF_BTIME) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119 dm->dqb_btime = di->dqb_btime;
Jan Karae04a88a92009-01-07 18:07:29 -08002120 check_blim = 1;
Jan Kara4d59bce2008-10-02 16:48:10 +02002121 __set_bit(DQ_LASTSET_B + QIF_BTIME_B, &dquot->dq_flags);
2122 }
2123 if (di->dqb_valid & QIF_ITIME) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124 dm->dqb_itime = di->dqb_itime;
Jan Karae04a88a92009-01-07 18:07:29 -08002125 check_ilim = 1;
Jan Kara4d59bce2008-10-02 16:48:10 +02002126 __set_bit(DQ_LASTSET_B + QIF_ITIME_B, &dquot->dq_flags);
2127 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128
2129 if (check_blim) {
Jan Kara12095462008-08-20 14:45:12 +02002130 if (!dm->dqb_bsoftlimit || dm->dqb_curspace < dm->dqb_bsoftlimit) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131 dm->dqb_btime = 0;
2132 clear_bit(DQ_BLKS_B, &dquot->dq_flags);
2133 }
2134 else if (!(di->dqb_valid & QIF_BTIME)) /* Set grace only if user hasn't provided his own... */
Andrew Perepechko338bf9a2008-04-28 02:14:31 -07002135 dm->dqb_btime = get_seconds() + dqi->dqi_bgrace;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136 }
2137 if (check_ilim) {
2138 if (!dm->dqb_isoftlimit || dm->dqb_curinodes < dm->dqb_isoftlimit) {
2139 dm->dqb_itime = 0;
2140 clear_bit(DQ_INODES_B, &dquot->dq_flags);
2141 }
2142 else if (!(di->dqb_valid & QIF_ITIME)) /* Set grace only if user hasn't provided his own... */
Andrew Perepechko338bf9a2008-04-28 02:14:31 -07002143 dm->dqb_itime = get_seconds() + dqi->dqi_igrace;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144 }
2145 if (dm->dqb_bhardlimit || dm->dqb_bsoftlimit || dm->dqb_ihardlimit || dm->dqb_isoftlimit)
2146 clear_bit(DQ_FAKE_B, &dquot->dq_flags);
2147 else
2148 set_bit(DQ_FAKE_B, &dquot->dq_flags);
2149 spin_unlock(&dq_data_lock);
2150 mark_dquot_dirty(dquot);
Andrew Perepechko338bf9a2008-04-28 02:14:31 -07002151
2152 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153}
2154
2155int vfs_set_dqblk(struct super_block *sb, int type, qid_t id, struct if_dqblk *di)
2156{
2157 struct dquot *dquot;
Andrew Perepechko338bf9a2008-04-28 02:14:31 -07002158 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159
Jan Karaf55abc02008-08-20 17:50:32 +02002160 dquot = dqget(sb, id, type);
2161 if (!dquot) {
2162 rc = -ESRCH;
2163 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164 }
Andrew Perepechko338bf9a2008-04-28 02:14:31 -07002165 rc = do_set_dqblk(dquot, di);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166 dqput(dquot);
Jan Karaf55abc02008-08-20 17:50:32 +02002167out:
Andrew Perepechko338bf9a2008-04-28 02:14:31 -07002168 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169}
2170
2171/* Generic routine for getting common part of quota file information */
2172int vfs_get_dqinfo(struct super_block *sb, int type, struct if_dqinfo *ii)
2173{
2174 struct mem_dqinfo *mi;
2175
Ingo Molnard3be9152006-03-23 03:00:29 -08002176 mutex_lock(&sb_dqopt(sb)->dqonoff_mutex);
Jan Karaf55abc02008-08-20 17:50:32 +02002177 if (!sb_has_quota_active(sb, type)) {
Ingo Molnard3be9152006-03-23 03:00:29 -08002178 mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179 return -ESRCH;
2180 }
2181 mi = sb_dqopt(sb)->info + type;
2182 spin_lock(&dq_data_lock);
2183 ii->dqi_bgrace = mi->dqi_bgrace;
2184 ii->dqi_igrace = mi->dqi_igrace;
2185 ii->dqi_flags = mi->dqi_flags & DQF_MASK;
2186 ii->dqi_valid = IIF_ALL;
2187 spin_unlock(&dq_data_lock);
Ingo Molnard3be9152006-03-23 03:00:29 -08002188 mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189 return 0;
2190}
2191
2192/* Generic routine for setting common part of quota file information */
2193int vfs_set_dqinfo(struct super_block *sb, int type, struct if_dqinfo *ii)
2194{
2195 struct mem_dqinfo *mi;
Jan Karaf55abc02008-08-20 17:50:32 +02002196 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197
Ingo Molnard3be9152006-03-23 03:00:29 -08002198 mutex_lock(&sb_dqopt(sb)->dqonoff_mutex);
Jan Karaf55abc02008-08-20 17:50:32 +02002199 if (!sb_has_quota_active(sb, type)) {
2200 err = -ESRCH;
2201 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202 }
2203 mi = sb_dqopt(sb)->info + type;
2204 spin_lock(&dq_data_lock);
2205 if (ii->dqi_valid & IIF_BGRACE)
2206 mi->dqi_bgrace = ii->dqi_bgrace;
2207 if (ii->dqi_valid & IIF_IGRACE)
2208 mi->dqi_igrace = ii->dqi_igrace;
2209 if (ii->dqi_valid & IIF_FLAGS)
2210 mi->dqi_flags = (mi->dqi_flags & ~DQF_MASK) | (ii->dqi_flags & DQF_MASK);
2211 spin_unlock(&dq_data_lock);
2212 mark_info_dirty(sb, type);
2213 /* Force write to disk */
2214 sb->dq_op->write_info(sb, type);
Jan Karaf55abc02008-08-20 17:50:32 +02002215out:
Ingo Molnard3be9152006-03-23 03:00:29 -08002216 mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
Jan Karaf55abc02008-08-20 17:50:32 +02002217 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218}
2219
2220struct quotactl_ops vfs_quotactl_ops = {
2221 .quota_on = vfs_quota_on,
2222 .quota_off = vfs_quota_off,
2223 .quota_sync = vfs_quota_sync,
2224 .get_info = vfs_get_dqinfo,
2225 .set_info = vfs_set_dqinfo,
2226 .get_dqblk = vfs_get_dqblk,
2227 .set_dqblk = vfs_set_dqblk
2228};
2229
2230static ctl_table fs_dqstats_table[] = {
2231 {
2232 .ctl_name = FS_DQ_LOOKUPS,
2233 .procname = "lookups",
2234 .data = &dqstats.lookups,
2235 .maxlen = sizeof(int),
2236 .mode = 0444,
2237 .proc_handler = &proc_dointvec,
2238 },
2239 {
2240 .ctl_name = FS_DQ_DROPS,
2241 .procname = "drops",
2242 .data = &dqstats.drops,
2243 .maxlen = sizeof(int),
2244 .mode = 0444,
2245 .proc_handler = &proc_dointvec,
2246 },
2247 {
2248 .ctl_name = FS_DQ_READS,
2249 .procname = "reads",
2250 .data = &dqstats.reads,
2251 .maxlen = sizeof(int),
2252 .mode = 0444,
2253 .proc_handler = &proc_dointvec,
2254 },
2255 {
2256 .ctl_name = FS_DQ_WRITES,
2257 .procname = "writes",
2258 .data = &dqstats.writes,
2259 .maxlen = sizeof(int),
2260 .mode = 0444,
2261 .proc_handler = &proc_dointvec,
2262 },
2263 {
2264 .ctl_name = FS_DQ_CACHE_HITS,
2265 .procname = "cache_hits",
2266 .data = &dqstats.cache_hits,
2267 .maxlen = sizeof(int),
2268 .mode = 0444,
2269 .proc_handler = &proc_dointvec,
2270 },
2271 {
2272 .ctl_name = FS_DQ_ALLOCATED,
2273 .procname = "allocated_dquots",
2274 .data = &dqstats.allocated_dquots,
2275 .maxlen = sizeof(int),
2276 .mode = 0444,
2277 .proc_handler = &proc_dointvec,
2278 },
2279 {
2280 .ctl_name = FS_DQ_FREE,
2281 .procname = "free_dquots",
2282 .data = &dqstats.free_dquots,
2283 .maxlen = sizeof(int),
2284 .mode = 0444,
2285 .proc_handler = &proc_dointvec,
2286 },
2287 {
2288 .ctl_name = FS_DQ_SYNCS,
2289 .procname = "syncs",
2290 .data = &dqstats.syncs,
2291 .maxlen = sizeof(int),
2292 .mode = 0444,
2293 .proc_handler = &proc_dointvec,
2294 },
Jan Kara8e893462007-10-16 23:29:31 -07002295#ifdef CONFIG_PRINT_QUOTA_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296 {
2297 .ctl_name = FS_DQ_WARNINGS,
2298 .procname = "warnings",
2299 .data = &flag_print_warnings,
2300 .maxlen = sizeof(int),
2301 .mode = 0644,
2302 .proc_handler = &proc_dointvec,
2303 },
Jan Kara8e893462007-10-16 23:29:31 -07002304#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305 { .ctl_name = 0 },
2306};
2307
2308static ctl_table fs_table[] = {
2309 {
2310 .ctl_name = FS_DQSTATS,
2311 .procname = "quota",
2312 .mode = 0555,
2313 .child = fs_dqstats_table,
2314 },
2315 { .ctl_name = 0 },
2316};
2317
2318static ctl_table sys_table[] = {
2319 {
2320 .ctl_name = CTL_FS,
2321 .procname = "fs",
2322 .mode = 0555,
2323 .child = fs_table,
2324 },
2325 { .ctl_name = 0 },
2326};
2327
2328static int __init dquot_init(void)
2329{
2330 int i;
2331 unsigned long nr_hash, order;
2332
2333 printk(KERN_NOTICE "VFS: Disk quotas %s\n", __DQUOT_VERSION__);
2334
Eric W. Biederman0b4d4142007-02-14 00:34:09 -08002335 register_sysctl_table(sys_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336
Paul Mundt20c2df82007-07-20 10:11:58 +09002337 dquot_cachep = kmem_cache_create("dquot",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338 sizeof(struct dquot), sizeof(unsigned long) * 4,
Paul Jacksonfffb60f2006-03-24 03:16:06 -08002339 (SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT|
2340 SLAB_MEM_SPREAD|SLAB_PANIC),
Paul Mundt20c2df82007-07-20 10:11:58 +09002341 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342
2343 order = 0;
2344 dquot_hash = (struct hlist_head *)__get_free_pages(GFP_ATOMIC, order);
2345 if (!dquot_hash)
2346 panic("Cannot create dquot hash table");
2347
2348 /* Find power-of-two hlist_heads which can fit into allocation */
2349 nr_hash = (1UL << order) * PAGE_SIZE / sizeof(struct hlist_head);
2350 dq_hash_bits = 0;
2351 do {
2352 dq_hash_bits++;
2353 } while (nr_hash >> dq_hash_bits);
2354 dq_hash_bits--;
2355
2356 nr_hash = 1UL << dq_hash_bits;
2357 dq_hash_mask = nr_hash - 1;
2358 for (i = 0; i < nr_hash; i++)
2359 INIT_HLIST_HEAD(dquot_hash + i);
2360
2361 printk("Dquot-cache hash table entries: %ld (order %ld, %ld bytes)\n",
2362 nr_hash, order, (PAGE_SIZE << order));
2363
Rusty Russell8e1f9362007-07-17 04:03:17 -07002364 register_shrinker(&dqcache_shrinker);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365
Jan Kara8e893462007-10-16 23:29:31 -07002366#ifdef CONFIG_QUOTA_NETLINK_INTERFACE
2367 if (genl_register_family(&quota_genl_family) != 0)
2368 printk(KERN_ERR "VFS: Failed to create quota netlink interface.\n");
2369#endif
2370
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371 return 0;
2372}
2373module_init(dquot_init);
2374
2375EXPORT_SYMBOL(register_quota_format);
2376EXPORT_SYMBOL(unregister_quota_format);
2377EXPORT_SYMBOL(dqstats);
2378EXPORT_SYMBOL(dq_data_lock);
Jan Karaf55abc02008-08-20 17:50:32 +02002379EXPORT_SYMBOL(vfs_quota_enable);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380EXPORT_SYMBOL(vfs_quota_on);
Al Viro77e69da2008-08-01 04:29:18 -04002381EXPORT_SYMBOL(vfs_quota_on_path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382EXPORT_SYMBOL(vfs_quota_on_mount);
Jan Karaf55abc02008-08-20 17:50:32 +02002383EXPORT_SYMBOL(vfs_quota_disable);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002384EXPORT_SYMBOL(vfs_quota_off);
Jan Kara12c77522008-10-20 17:05:00 +02002385EXPORT_SYMBOL(dquot_scan_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002386EXPORT_SYMBOL(vfs_quota_sync);
2387EXPORT_SYMBOL(vfs_get_dqinfo);
2388EXPORT_SYMBOL(vfs_set_dqinfo);
2389EXPORT_SYMBOL(vfs_get_dqblk);
2390EXPORT_SYMBOL(vfs_set_dqblk);
2391EXPORT_SYMBOL(dquot_commit);
2392EXPORT_SYMBOL(dquot_commit_info);
2393EXPORT_SYMBOL(dquot_acquire);
2394EXPORT_SYMBOL(dquot_release);
2395EXPORT_SYMBOL(dquot_mark_dquot_dirty);
2396EXPORT_SYMBOL(dquot_initialize);
2397EXPORT_SYMBOL(dquot_drop);
Jan Karab85f4b82008-07-25 01:46:50 -07002398EXPORT_SYMBOL(vfs_dq_drop);
Jan Kara3d9ea252008-10-10 16:12:23 +02002399EXPORT_SYMBOL(dqget);
2400EXPORT_SYMBOL(dqput);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401EXPORT_SYMBOL(dquot_alloc_space);
2402EXPORT_SYMBOL(dquot_alloc_inode);
2403EXPORT_SYMBOL(dquot_free_space);
2404EXPORT_SYMBOL(dquot_free_inode);
2405EXPORT_SYMBOL(dquot_transfer);
Jan Karab85f4b82008-07-25 01:46:50 -07002406EXPORT_SYMBOL(vfs_dq_transfer);
2407EXPORT_SYMBOL(vfs_dq_quota_on_remount);