blob: 48c0571f831d5fc72d7724f54ffab6ea1d61849a [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/*
90 * There are two quota SMP locks. dq_list_lock protects all lists with quotas
91 * and quota formats and also dqstats structure containing statistics about the
92 * lists. dq_data_lock protects data from dq_dqb and also mem_dqinfo structures
93 * and also guards consistency of dquot->dq_dqb with inode->i_blocks, i_bytes.
94 * i_blocks and i_bytes updates itself are guarded by i_lock acquired directly
95 * in inode_add_bytes() and inode_sub_bytes().
96 *
97 * The spinlock ordering is hence: dq_data_lock > dq_list_lock > i_lock
98 *
99 * Note that some things (eg. sb pointer, type, id) doesn't change during
100 * the life of the dquot structure and so needn't to be protected by a lock
101 *
102 * Any operation working on dquots via inode pointers must hold dqptr_sem. If
103 * operation is just reading pointers from inode (or not using them at all) the
104 * read lock is enough. If pointers are altered function must hold write lock
105 * (these locking rules also apply for S_NOQUOTA flag in the inode - note that
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800106 * for altering the flag i_mutex is also needed). If operation is holding
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 * reference to dquot in other way (e.g. quotactl ops) it must be guarded by
Ingo Molnard3be9152006-03-23 03:00:29 -0800108 * dqonoff_mutex.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 * This locking assures that:
110 * a) update/access to dquot pointers in inode is serialized
111 * b) everyone is guarded against invalidate_dquots()
112 *
Ingo Molnard3be9152006-03-23 03:00:29 -0800113 * Each dquot has its dq_lock mutex. Locked dquots might not be referenced
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 * from inodes (dquot_alloc_space() and such don't check the dq_lock).
115 * Currently dquot is locked only when it is being read to memory (or space for
116 * it is being allocated) on the first dqget() and when it is being released on
117 * the last dqput(). The allocation and release oparations are serialized by
118 * the dq_lock and by checking the use count in dquot_release(). Write
119 * operations on dquots don't hold dq_lock as they copy data under dq_data_lock
120 * spinlock to internal buffers before writing.
121 *
122 * Lock ordering (including related VFS locks) is the following:
Ingo Molnard3be9152006-03-23 03:00:29 -0800123 * i_mutex > dqonoff_sem > journal_lock > dqptr_sem > dquot->dq_lock >
124 * dqio_mutex
125 * i_mutex on quota files is special (it's below dqio_mutex)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 */
127
128static DEFINE_SPINLOCK(dq_list_lock);
129DEFINE_SPINLOCK(dq_data_lock);
130
131static char *quotatypes[] = INITQFNAMES;
132static struct quota_format_type *quota_formats; /* List of registered formats */
133static struct quota_module_name module_names[] = INIT_QUOTA_MODULE_NAMES;
134
135/* SLAB cache for dquot structures */
Christoph Lametere18b8902006-12-06 20:33:20 -0800136static struct kmem_cache *dquot_cachep;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137
138int register_quota_format(struct quota_format_type *fmt)
139{
140 spin_lock(&dq_list_lock);
141 fmt->qf_next = quota_formats;
142 quota_formats = fmt;
143 spin_unlock(&dq_list_lock);
144 return 0;
145}
146
147void unregister_quota_format(struct quota_format_type *fmt)
148{
149 struct quota_format_type **actqf;
150
151 spin_lock(&dq_list_lock);
152 for (actqf = &quota_formats; *actqf && *actqf != fmt; actqf = &(*actqf)->qf_next);
153 if (*actqf)
154 *actqf = (*actqf)->qf_next;
155 spin_unlock(&dq_list_lock);
156}
157
158static struct quota_format_type *find_quota_format(int id)
159{
160 struct quota_format_type *actqf;
161
162 spin_lock(&dq_list_lock);
163 for (actqf = quota_formats; actqf && actqf->qf_fmt_id != id; actqf = actqf->qf_next);
164 if (!actqf || !try_module_get(actqf->qf_owner)) {
165 int qm;
166
167 spin_unlock(&dq_list_lock);
168
169 for (qm = 0; module_names[qm].qm_fmt_id && module_names[qm].qm_fmt_id != id; qm++);
170 if (!module_names[qm].qm_fmt_id || request_module(module_names[qm].qm_mod_name))
171 return NULL;
172
173 spin_lock(&dq_list_lock);
174 for (actqf = quota_formats; actqf && actqf->qf_fmt_id != id; actqf = actqf->qf_next);
175 if (actqf && !try_module_get(actqf->qf_owner))
176 actqf = NULL;
177 }
178 spin_unlock(&dq_list_lock);
179 return actqf;
180}
181
182static void put_quota_format(struct quota_format_type *fmt)
183{
184 module_put(fmt->qf_owner);
185}
186
187/*
188 * Dquot List Management:
189 * The quota code uses three lists for dquot management: the inuse_list,
190 * free_dquots, and dquot_hash[] array. A single dquot structure may be
191 * on all three lists, depending on its current state.
192 *
193 * All dquots are placed to the end of inuse_list when first created, and this
194 * list is used for invalidate operation, which must look at every dquot.
195 *
196 * Unused dquots (dq_count == 0) are added to the free_dquots list when freed,
197 * and this list is searched whenever we need an available dquot. Dquots are
198 * removed from the list as soon as they are used again, and
199 * dqstats.free_dquots gives the number of dquots on the list. When
200 * dquot is invalidated it's completely released from memory.
201 *
202 * Dquots with a specific identity (device, type and id) are placed on
203 * one of the dquot_hash[] hash chains. The provides an efficient search
204 * mechanism to locate a specific dquot.
205 */
206
207static LIST_HEAD(inuse_list);
208static LIST_HEAD(free_dquots);
209static unsigned int dq_hash_bits, dq_hash_mask;
210static struct hlist_head *dquot_hash;
211
212struct dqstats dqstats;
213
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214static inline unsigned int
215hashfn(const struct super_block *sb, unsigned int id, int type)
216{
217 unsigned long tmp;
218
219 tmp = (((unsigned long)sb>>L1_CACHE_SHIFT) ^ id) * (MAXQUOTAS - type);
220 return (tmp + (tmp >> dq_hash_bits)) & dq_hash_mask;
221}
222
223/*
224 * Following list functions expect dq_list_lock to be held
225 */
226static inline void insert_dquot_hash(struct dquot *dquot)
227{
228 struct hlist_head *head = dquot_hash + hashfn(dquot->dq_sb, dquot->dq_id, dquot->dq_type);
229 hlist_add_head(&dquot->dq_hash, head);
230}
231
232static inline void remove_dquot_hash(struct dquot *dquot)
233{
234 hlist_del_init(&dquot->dq_hash);
235}
236
237static inline struct dquot *find_dquot(unsigned int hashent, struct super_block *sb, unsigned int id, int type)
238{
239 struct hlist_node *node;
240 struct dquot *dquot;
241
242 hlist_for_each (node, dquot_hash+hashent) {
243 dquot = hlist_entry(node, struct dquot, dq_hash);
244 if (dquot->dq_sb == sb && dquot->dq_id == id && dquot->dq_type == type)
245 return dquot;
246 }
247 return NODQUOT;
248}
249
250/* Add a dquot to the tail of the free list */
251static inline void put_dquot_last(struct dquot *dquot)
252{
Akinobu Mita8e130592006-06-26 00:24:37 -0700253 list_add_tail(&dquot->dq_free, &free_dquots);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 dqstats.free_dquots++;
255}
256
257static inline void remove_free_dquot(struct dquot *dquot)
258{
259 if (list_empty(&dquot->dq_free))
260 return;
261 list_del_init(&dquot->dq_free);
262 dqstats.free_dquots--;
263}
264
265static inline void put_inuse(struct dquot *dquot)
266{
267 /* We add to the back of inuse list so we don't have to restart
268 * when traversing this list and we block */
Akinobu Mita8e130592006-06-26 00:24:37 -0700269 list_add_tail(&dquot->dq_inuse, &inuse_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 dqstats.allocated_dquots++;
271}
272
273static inline void remove_inuse(struct dquot *dquot)
274{
275 dqstats.allocated_dquots--;
276 list_del(&dquot->dq_inuse);
277}
278/*
279 * End of list functions needing dq_list_lock
280 */
281
282static void wait_on_dquot(struct dquot *dquot)
283{
Ingo Molnard3be9152006-03-23 03:00:29 -0800284 mutex_lock(&dquot->dq_lock);
285 mutex_unlock(&dquot->dq_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286}
287
Jan Kara03f6e922008-04-28 02:14:32 -0700288static inline int dquot_dirty(struct dquot *dquot)
289{
290 return test_bit(DQ_MOD_B, &dquot->dq_flags);
291}
292
293static inline int mark_dquot_dirty(struct dquot *dquot)
294{
295 return dquot->dq_sb->dq_op->mark_dirty(dquot);
296}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297
298int dquot_mark_dquot_dirty(struct dquot *dquot)
299{
300 spin_lock(&dq_list_lock);
301 if (!test_and_set_bit(DQ_MOD_B, &dquot->dq_flags))
302 list_add(&dquot->dq_dirty, &sb_dqopt(dquot->dq_sb)->
303 info[dquot->dq_type].dqi_dirty_list);
304 spin_unlock(&dq_list_lock);
305 return 0;
306}
307
308/* This function needs dq_list_lock */
309static inline int clear_dquot_dirty(struct dquot *dquot)
310{
311 if (!test_and_clear_bit(DQ_MOD_B, &dquot->dq_flags))
312 return 0;
313 list_del_init(&dquot->dq_dirty);
314 return 1;
315}
316
317void mark_info_dirty(struct super_block *sb, int type)
318{
319 set_bit(DQF_INFO_DIRTY_B, &sb_dqopt(sb)->info[type].dqi_flags);
320}
321EXPORT_SYMBOL(mark_info_dirty);
322
323/*
324 * Read dquot from disk and alloc space for it
325 */
326
327int dquot_acquire(struct dquot *dquot)
328{
329 int ret = 0, ret2 = 0;
330 struct quota_info *dqopt = sb_dqopt(dquot->dq_sb);
331
Ingo Molnard3be9152006-03-23 03:00:29 -0800332 mutex_lock(&dquot->dq_lock);
333 mutex_lock(&dqopt->dqio_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 if (!test_bit(DQ_READ_B, &dquot->dq_flags))
335 ret = dqopt->ops[dquot->dq_type]->read_dqblk(dquot);
336 if (ret < 0)
337 goto out_iolock;
338 set_bit(DQ_READ_B, &dquot->dq_flags);
339 /* Instantiate dquot if needed */
340 if (!test_bit(DQ_ACTIVE_B, &dquot->dq_flags) && !dquot->dq_off) {
341 ret = dqopt->ops[dquot->dq_type]->commit_dqblk(dquot);
342 /* Write the info if needed */
343 if (info_dirty(&dqopt->info[dquot->dq_type]))
344 ret2 = dqopt->ops[dquot->dq_type]->write_file_info(dquot->dq_sb, dquot->dq_type);
345 if (ret < 0)
346 goto out_iolock;
347 if (ret2 < 0) {
348 ret = ret2;
349 goto out_iolock;
350 }
351 }
352 set_bit(DQ_ACTIVE_B, &dquot->dq_flags);
353out_iolock:
Ingo Molnard3be9152006-03-23 03:00:29 -0800354 mutex_unlock(&dqopt->dqio_mutex);
355 mutex_unlock(&dquot->dq_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 return ret;
357}
358
359/*
360 * Write dquot to disk
361 */
362int dquot_commit(struct dquot *dquot)
363{
364 int ret = 0, ret2 = 0;
365 struct quota_info *dqopt = sb_dqopt(dquot->dq_sb);
366
Ingo Molnard3be9152006-03-23 03:00:29 -0800367 mutex_lock(&dqopt->dqio_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 spin_lock(&dq_list_lock);
369 if (!clear_dquot_dirty(dquot)) {
370 spin_unlock(&dq_list_lock);
371 goto out_sem;
372 }
373 spin_unlock(&dq_list_lock);
374 /* Inactive dquot can be only if there was error during read/init
375 * => we have better not writing it */
376 if (test_bit(DQ_ACTIVE_B, &dquot->dq_flags)) {
377 ret = dqopt->ops[dquot->dq_type]->commit_dqblk(dquot);
378 if (info_dirty(&dqopt->info[dquot->dq_type]))
379 ret2 = dqopt->ops[dquot->dq_type]->write_file_info(dquot->dq_sb, dquot->dq_type);
380 if (ret >= 0)
381 ret = ret2;
382 }
383out_sem:
Ingo Molnard3be9152006-03-23 03:00:29 -0800384 mutex_unlock(&dqopt->dqio_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 return ret;
386}
387
388/*
389 * Release dquot
390 */
391int dquot_release(struct dquot *dquot)
392{
393 int ret = 0, ret2 = 0;
394 struct quota_info *dqopt = sb_dqopt(dquot->dq_sb);
395
Ingo Molnard3be9152006-03-23 03:00:29 -0800396 mutex_lock(&dquot->dq_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 /* Check whether we are not racing with some other dqget() */
398 if (atomic_read(&dquot->dq_count) > 1)
399 goto out_dqlock;
Ingo Molnard3be9152006-03-23 03:00:29 -0800400 mutex_lock(&dqopt->dqio_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 if (dqopt->ops[dquot->dq_type]->release_dqblk) {
402 ret = dqopt->ops[dquot->dq_type]->release_dqblk(dquot);
403 /* Write the info */
404 if (info_dirty(&dqopt->info[dquot->dq_type]))
405 ret2 = dqopt->ops[dquot->dq_type]->write_file_info(dquot->dq_sb, dquot->dq_type);
406 if (ret >= 0)
407 ret = ret2;
408 }
409 clear_bit(DQ_ACTIVE_B, &dquot->dq_flags);
Ingo Molnard3be9152006-03-23 03:00:29 -0800410 mutex_unlock(&dqopt->dqio_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411out_dqlock:
Ingo Molnard3be9152006-03-23 03:00:29 -0800412 mutex_unlock(&dquot->dq_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 return ret;
414}
415
Jan Kara7d9056b2008-11-25 15:31:32 +0100416void dquot_destroy(struct dquot *dquot)
Jan Kara74f783a2008-08-19 14:51:22 +0200417{
418 kmem_cache_free(dquot_cachep, dquot);
419}
Jan Kara7d9056b2008-11-25 15:31:32 +0100420EXPORT_SYMBOL(dquot_destroy);
Jan Kara74f783a2008-08-19 14:51:22 +0200421
422static inline void do_destroy_dquot(struct dquot *dquot)
423{
424 dquot->dq_sb->dq_op->destroy_dquot(dquot);
425}
426
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427/* Invalidate all dquots on the list. Note that this function is called after
428 * quota is disabled and pointers from inodes removed so there cannot be new
Jan Kara6362e4d2006-03-23 03:00:17 -0800429 * quota users. There can still be some users of quotas due to inodes being
430 * just deleted or pruned by prune_icache() (those are not attached to any
431 * list). We have to wait for such users.
432 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433static void invalidate_dquots(struct super_block *sb, int type)
434{
Domen Puncerc33ed272005-06-25 14:59:36 -0700435 struct dquot *dquot, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436
Jan Kara6362e4d2006-03-23 03:00:17 -0800437restart:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 spin_lock(&dq_list_lock);
Domen Puncerc33ed272005-06-25 14:59:36 -0700439 list_for_each_entry_safe(dquot, tmp, &inuse_list, dq_inuse) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 if (dquot->dq_sb != sb)
441 continue;
442 if (dquot->dq_type != type)
443 continue;
Jan Kara6362e4d2006-03-23 03:00:17 -0800444 /* Wait for dquot users */
445 if (atomic_read(&dquot->dq_count)) {
446 DEFINE_WAIT(wait);
447
448 atomic_inc(&dquot->dq_count);
449 prepare_to_wait(&dquot->dq_wait_unused, &wait,
450 TASK_UNINTERRUPTIBLE);
451 spin_unlock(&dq_list_lock);
452 /* Once dqput() wakes us up, we know it's time to free
453 * the dquot.
454 * IMPORTANT: we rely on the fact that there is always
455 * at most one process waiting for dquot to free.
456 * Otherwise dq_count would be > 1 and we would never
457 * wake up.
458 */
459 if (atomic_read(&dquot->dq_count) > 1)
460 schedule();
461 finish_wait(&dquot->dq_wait_unused, &wait);
462 dqput(dquot);
463 /* At this moment dquot() need not exist (it could be
464 * reclaimed by prune_dqcache(). Hence we must
465 * restart. */
466 goto restart;
467 }
468 /*
469 * Quota now has no users and it has been written on last
470 * dqput()
471 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 remove_dquot_hash(dquot);
473 remove_free_dquot(dquot);
474 remove_inuse(dquot);
Jan Kara74f783a2008-08-19 14:51:22 +0200475 do_destroy_dquot(dquot);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 }
477 spin_unlock(&dq_list_lock);
478}
479
Jan Kara12c77522008-10-20 17:05:00 +0200480/* Call callback for every active dquot on given filesystem */
481int dquot_scan_active(struct super_block *sb,
482 int (*fn)(struct dquot *dquot, unsigned long priv),
483 unsigned long priv)
484{
485 struct dquot *dquot, *old_dquot = NULL;
486 int ret = 0;
487
488 mutex_lock(&sb_dqopt(sb)->dqonoff_mutex);
489 spin_lock(&dq_list_lock);
490 list_for_each_entry(dquot, &inuse_list, dq_inuse) {
491 if (!test_bit(DQ_ACTIVE_B, &dquot->dq_flags))
492 continue;
493 if (dquot->dq_sb != sb)
494 continue;
495 /* Now we have active dquot so we can just increase use count */
496 atomic_inc(&dquot->dq_count);
497 dqstats.lookups++;
498 spin_unlock(&dq_list_lock);
499 dqput(old_dquot);
500 old_dquot = dquot;
501 ret = fn(dquot, priv);
502 if (ret < 0)
503 goto out;
504 spin_lock(&dq_list_lock);
505 /* We are safe to continue now because our dquot could not
506 * be moved out of the inuse list while we hold the reference */
507 }
508 spin_unlock(&dq_list_lock);
509out:
510 dqput(old_dquot);
511 mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
512 return ret;
513}
514
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515int vfs_quota_sync(struct super_block *sb, int type)
516{
517 struct list_head *dirty;
518 struct dquot *dquot;
519 struct quota_info *dqopt = sb_dqopt(sb);
520 int cnt;
521
Ingo Molnard3be9152006-03-23 03:00:29 -0800522 mutex_lock(&dqopt->dqonoff_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
524 if (type != -1 && cnt != type)
525 continue;
Jan Karaf55abc02008-08-20 17:50:32 +0200526 if (!sb_has_quota_active(sb, cnt))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 continue;
528 spin_lock(&dq_list_lock);
529 dirty = &dqopt->info[cnt].dqi_dirty_list;
530 while (!list_empty(dirty)) {
Pavel Emelianovb5e61812007-05-08 00:30:19 -0700531 dquot = list_first_entry(dirty, struct dquot, dq_dirty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 /* Dirty and inactive can be only bad dquot... */
533 if (!test_bit(DQ_ACTIVE_B, &dquot->dq_flags)) {
534 clear_dquot_dirty(dquot);
535 continue;
536 }
537 /* Now we have active dquot from which someone is
538 * holding reference so we can safely just increase
539 * use count */
540 atomic_inc(&dquot->dq_count);
541 dqstats.lookups++;
542 spin_unlock(&dq_list_lock);
543 sb->dq_op->write_dquot(dquot);
544 dqput(dquot);
545 spin_lock(&dq_list_lock);
546 }
547 spin_unlock(&dq_list_lock);
548 }
549
550 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
Jan Karaf55abc02008-08-20 17:50:32 +0200551 if ((cnt == type || type == -1) && sb_has_quota_active(sb, cnt)
552 && info_dirty(&dqopt->info[cnt]))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 sb->dq_op->write_info(sb, cnt);
554 spin_lock(&dq_list_lock);
555 dqstats.syncs++;
556 spin_unlock(&dq_list_lock);
Ingo Molnard3be9152006-03-23 03:00:29 -0800557 mutex_unlock(&dqopt->dqonoff_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558
559 return 0;
560}
561
562/* Free unused dquots from cache */
563static void prune_dqcache(int count)
564{
565 struct list_head *head;
566 struct dquot *dquot;
567
568 head = free_dquots.prev;
569 while (head != &free_dquots && count) {
570 dquot = list_entry(head, struct dquot, dq_free);
571 remove_dquot_hash(dquot);
572 remove_free_dquot(dquot);
573 remove_inuse(dquot);
Jan Kara74f783a2008-08-19 14:51:22 +0200574 do_destroy_dquot(dquot);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 count--;
576 head = free_dquots.prev;
577 }
578}
579
580/*
581 * This is called from kswapd when we think we need some
582 * more memory
583 */
584
Al Viro27496a82005-10-21 03:20:48 -0400585static int shrink_dqcache_memory(int nr, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586{
587 if (nr) {
588 spin_lock(&dq_list_lock);
589 prune_dqcache(nr);
590 spin_unlock(&dq_list_lock);
591 }
592 return (dqstats.free_dquots / 100) * sysctl_vfs_cache_pressure;
593}
594
Rusty Russell8e1f9362007-07-17 04:03:17 -0700595static struct shrinker dqcache_shrinker = {
596 .shrink = shrink_dqcache_memory,
597 .seeks = DEFAULT_SEEKS,
598};
599
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600/*
601 * Put reference to dquot
602 * NOTE: If you change this function please check whether dqput_blocks() works right...
Ingo Molnard3be9152006-03-23 03:00:29 -0800603 * MUST be called with either dqptr_sem or dqonoff_mutex held
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 */
Jan Kara3d9ea252008-10-10 16:12:23 +0200605void dqput(struct dquot *dquot)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606{
Jan Karab48d3802008-07-25 01:46:49 -0700607 int ret;
608
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 if (!dquot)
610 return;
611#ifdef __DQUOT_PARANOIA
612 if (!atomic_read(&dquot->dq_count)) {
613 printk("VFS: dqput: trying to free free dquot\n");
614 printk("VFS: device %s, dquot of %s %d\n",
615 dquot->dq_sb->s_id,
616 quotatypes[dquot->dq_type],
617 dquot->dq_id);
618 BUG();
619 }
620#endif
621
622 spin_lock(&dq_list_lock);
623 dqstats.drops++;
624 spin_unlock(&dq_list_lock);
625we_slept:
626 spin_lock(&dq_list_lock);
627 if (atomic_read(&dquot->dq_count) > 1) {
628 /* We have more than one user... nothing to do */
629 atomic_dec(&dquot->dq_count);
Jan Kara6362e4d2006-03-23 03:00:17 -0800630 /* Releasing dquot during quotaoff phase? */
Jan Karaf55abc02008-08-20 17:50:32 +0200631 if (!sb_has_quota_active(dquot->dq_sb, dquot->dq_type) &&
Jan Kara6362e4d2006-03-23 03:00:17 -0800632 atomic_read(&dquot->dq_count) == 1)
633 wake_up(&dquot->dq_wait_unused);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 spin_unlock(&dq_list_lock);
635 return;
636 }
637 /* Need to release dquot? */
638 if (test_bit(DQ_ACTIVE_B, &dquot->dq_flags) && dquot_dirty(dquot)) {
639 spin_unlock(&dq_list_lock);
640 /* Commit dquot before releasing */
Jan Karab48d3802008-07-25 01:46:49 -0700641 ret = dquot->dq_sb->dq_op->write_dquot(dquot);
642 if (ret < 0) {
643 printk(KERN_ERR "VFS: cannot write quota structure on "
644 "device %s (error %d). Quota may get out of "
645 "sync!\n", dquot->dq_sb->s_id, ret);
646 /*
647 * We clear dirty bit anyway, so that we avoid
648 * infinite loop here
649 */
650 spin_lock(&dq_list_lock);
651 clear_dquot_dirty(dquot);
652 spin_unlock(&dq_list_lock);
653 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 goto we_slept;
655 }
656 /* Clear flag in case dquot was inactive (something bad happened) */
657 clear_dquot_dirty(dquot);
658 if (test_bit(DQ_ACTIVE_B, &dquot->dq_flags)) {
659 spin_unlock(&dq_list_lock);
660 dquot->dq_sb->dq_op->release_dquot(dquot);
661 goto we_slept;
662 }
663 atomic_dec(&dquot->dq_count);
664#ifdef __DQUOT_PARANOIA
665 /* sanity check */
Eric Sesterhenn8abf6a42006-04-02 13:36:13 +0200666 BUG_ON(!list_empty(&dquot->dq_free));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667#endif
668 put_dquot_last(dquot);
669 spin_unlock(&dq_list_lock);
670}
671
Jan Kara7d9056b2008-11-25 15:31:32 +0100672struct dquot *dquot_alloc(struct super_block *sb, int type)
Jan Kara74f783a2008-08-19 14:51:22 +0200673{
674 return kmem_cache_zalloc(dquot_cachep, GFP_NOFS);
675}
Jan Kara7d9056b2008-11-25 15:31:32 +0100676EXPORT_SYMBOL(dquot_alloc);
Jan Kara74f783a2008-08-19 14:51:22 +0200677
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678static struct dquot *get_empty_dquot(struct super_block *sb, int type)
679{
680 struct dquot *dquot;
681
Jan Kara74f783a2008-08-19 14:51:22 +0200682 dquot = sb->dq_op->alloc_dquot(sb, type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 if(!dquot)
684 return NODQUOT;
685
Ingo Molnard3be9152006-03-23 03:00:29 -0800686 mutex_init(&dquot->dq_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 INIT_LIST_HEAD(&dquot->dq_free);
688 INIT_LIST_HEAD(&dquot->dq_inuse);
689 INIT_HLIST_NODE(&dquot->dq_hash);
690 INIT_LIST_HEAD(&dquot->dq_dirty);
Jan Kara6362e4d2006-03-23 03:00:17 -0800691 init_waitqueue_head(&dquot->dq_wait_unused);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 dquot->dq_sb = sb;
693 dquot->dq_type = type;
694 atomic_set(&dquot->dq_count, 1);
695
696 return dquot;
697}
698
699/*
Jan Kara3d9ea252008-10-10 16:12:23 +0200700 * Check whether dquot is in memory.
701 * MUST be called with either dqptr_sem or dqonoff_mutex held
702 */
703int dquot_is_cached(struct super_block *sb, unsigned int id, int type)
704{
705 unsigned int hashent = hashfn(sb, id, type);
706 int ret = 0;
707
708 if (!sb_has_quota_active(sb, type))
709 return 0;
710 spin_lock(&dq_list_lock);
711 if (find_dquot(hashent, sb, id, type) != NODQUOT)
712 ret = 1;
713 spin_unlock(&dq_list_lock);
714 return ret;
715}
716
717/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 * Get reference to dquot
Ingo Molnard3be9152006-03-23 03:00:29 -0800719 * MUST be called with either dqptr_sem or dqonoff_mutex held
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 */
Jan Kara3d9ea252008-10-10 16:12:23 +0200721struct dquot *dqget(struct super_block *sb, unsigned int id, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722{
723 unsigned int hashent = hashfn(sb, id, type);
724 struct dquot *dquot, *empty = NODQUOT;
725
Jan Karaf55abc02008-08-20 17:50:32 +0200726 if (!sb_has_quota_active(sb, type))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 return NODQUOT;
728we_slept:
729 spin_lock(&dq_list_lock);
730 if ((dquot = find_dquot(hashent, sb, id, type)) == NODQUOT) {
731 if (empty == NODQUOT) {
732 spin_unlock(&dq_list_lock);
733 if ((empty = get_empty_dquot(sb, type)) == NODQUOT)
734 schedule(); /* Try to wait for a moment... */
735 goto we_slept;
736 }
737 dquot = empty;
738 dquot->dq_id = id;
739 /* all dquots go on the inuse_list */
740 put_inuse(dquot);
741 /* hash it first so it can be found */
742 insert_dquot_hash(dquot);
743 dqstats.lookups++;
744 spin_unlock(&dq_list_lock);
745 } else {
746 if (!atomic_read(&dquot->dq_count))
747 remove_free_dquot(dquot);
748 atomic_inc(&dquot->dq_count);
749 dqstats.cache_hits++;
750 dqstats.lookups++;
751 spin_unlock(&dq_list_lock);
752 if (empty)
Jan Kara74f783a2008-08-19 14:51:22 +0200753 do_destroy_dquot(empty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 }
755 /* Wait for dq_lock - after this we know that either dquot_release() is already
756 * finished or it will be canceled due to dq_count > 1 test */
757 wait_on_dquot(dquot);
758 /* Read the dquot and instantiate it (everything done only if needed) */
759 if (!test_bit(DQ_ACTIVE_B, &dquot->dq_flags) && sb->dq_op->acquire_dquot(dquot) < 0) {
760 dqput(dquot);
761 return NODQUOT;
762 }
763#ifdef __DQUOT_PARANOIA
Eric Sesterhenn8abf6a42006-04-02 13:36:13 +0200764 BUG_ON(!dquot->dq_sb); /* Has somebody invalidated entry under us? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765#endif
766
767 return dquot;
768}
769
770static int dqinit_needed(struct inode *inode, int type)
771{
772 int cnt;
773
774 if (IS_NOQUOTA(inode))
775 return 0;
776 if (type != -1)
777 return inode->i_dquot[type] == NODQUOT;
778 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
779 if (inode->i_dquot[cnt] == NODQUOT)
780 return 1;
781 return 0;
782}
783
Ingo Molnard3be9152006-03-23 03:00:29 -0800784/* This routine is guarded by dqonoff_mutex mutex */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785static void add_dquot_ref(struct super_block *sb, int type)
786{
Jan Kara941d2382008-02-06 01:37:36 -0800787 struct inode *inode, *old_inode = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788
Christoph Hellwigd003fb72007-02-12 00:51:58 -0800789 spin_lock(&inode_lock);
790 list_for_each_entry(inode, &sb->s_inodes, i_sb_list) {
791 if (!atomic_read(&inode->i_writecount))
792 continue;
793 if (!dqinit_needed(inode, type))
794 continue;
795 if (inode->i_state & (I_FREEING|I_WILL_FREE))
796 continue;
797
798 __iget(inode);
799 spin_unlock(&inode_lock);
800
Jan Kara941d2382008-02-06 01:37:36 -0800801 iput(old_inode);
Christoph Hellwigd003fb72007-02-12 00:51:58 -0800802 sb->dq_op->initialize(inode, type);
Jan Kara941d2382008-02-06 01:37:36 -0800803 /* We hold a reference to 'inode' so it couldn't have been
804 * removed from s_inodes list while we dropped the inode_lock.
805 * We cannot iput the inode now as we can be holding the last
806 * reference and we cannot iput it under inode_lock. So we
807 * keep the reference and iput it later. */
808 old_inode = inode;
809 spin_lock(&inode_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 }
Christoph Hellwigd003fb72007-02-12 00:51:58 -0800811 spin_unlock(&inode_lock);
Jan Kara941d2382008-02-06 01:37:36 -0800812 iput(old_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813}
814
815/* Return 0 if dqput() won't block (note that 1 doesn't necessarily mean blocking) */
816static inline int dqput_blocks(struct dquot *dquot)
817{
818 if (atomic_read(&dquot->dq_count) <= 1)
819 return 1;
820 return 0;
821}
822
823/* Remove references to dquots from inode - add dquot to list for freeing if needed */
824/* We can't race with anybody because we hold dqptr_sem for writing... */
Adrian Bunke5f00f42007-05-08 00:27:22 -0700825static int remove_inode_dquot_ref(struct inode *inode, int type,
826 struct list_head *tofree_head)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827{
828 struct dquot *dquot = inode->i_dquot[type];
829
830 inode->i_dquot[type] = NODQUOT;
831 if (dquot != NODQUOT) {
832 if (dqput_blocks(dquot)) {
833#ifdef __DQUOT_PARANOIA
834 if (atomic_read(&dquot->dq_count) != 1)
835 printk(KERN_WARNING "VFS: Adding dquot with dq_count %d to dispose list.\n", atomic_read(&dquot->dq_count));
836#endif
837 spin_lock(&dq_list_lock);
838 list_add(&dquot->dq_free, tofree_head); /* As dquot must have currently users it can't be on the free list... */
839 spin_unlock(&dq_list_lock);
840 return 1;
841 }
842 else
843 dqput(dquot); /* We have guaranteed we won't block */
844 }
845 return 0;
846}
847
848/* Free list of dquots - called from inode.c */
849/* dquots are removed from inodes, no new references can be got so we are the only ones holding reference */
850static void put_dquot_list(struct list_head *tofree_head)
851{
852 struct list_head *act_head;
853 struct dquot *dquot;
854
855 act_head = tofree_head->next;
856 /* So now we have dquots on the list... Just free them */
857 while (act_head != tofree_head) {
858 dquot = list_entry(act_head, struct dquot, dq_free);
859 act_head = act_head->next;
860 list_del_init(&dquot->dq_free); /* Remove dquot from the list so we won't have problems... */
861 dqput(dquot);
862 }
863}
864
Christoph Hellwigfb58b732007-02-12 00:51:57 -0800865static void remove_dquot_ref(struct super_block *sb, int type,
866 struct list_head *tofree_head)
867{
868 struct inode *inode;
869
870 spin_lock(&inode_lock);
871 list_for_each_entry(inode, &sb->s_inodes, i_sb_list) {
872 if (!IS_NOQUOTA(inode))
873 remove_inode_dquot_ref(inode, type, tofree_head);
874 }
875 spin_unlock(&inode_lock);
876}
877
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878/* Gather all references from inodes and drop them */
879static void drop_dquot_ref(struct super_block *sb, int type)
880{
881 LIST_HEAD(tofree_head);
882
Christoph Hellwigfb58b732007-02-12 00:51:57 -0800883 if (sb->dq_op) {
884 down_write(&sb_dqopt(sb)->dqptr_sem);
885 remove_dquot_ref(sb, type, &tofree_head);
886 up_write(&sb_dqopt(sb)->dqptr_sem);
887 put_dquot_list(&tofree_head);
888 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889}
890
Jan Kara12095462008-08-20 14:45:12 +0200891static inline void dquot_incr_inodes(struct dquot *dquot, qsize_t number)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892{
893 dquot->dq_dqb.dqb_curinodes += number;
894}
895
896static inline void dquot_incr_space(struct dquot *dquot, qsize_t number)
897{
898 dquot->dq_dqb.dqb_curspace += number;
899}
900
Jan Kara12095462008-08-20 14:45:12 +0200901static inline void dquot_decr_inodes(struct dquot *dquot, qsize_t number)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902{
Jan Karadb49d2d2008-10-01 18:21:39 +0200903 if (sb_dqopt(dquot->dq_sb)->flags & DQUOT_NEGATIVE_USAGE ||
904 dquot->dq_dqb.dqb_curinodes >= number)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 dquot->dq_dqb.dqb_curinodes -= number;
906 else
907 dquot->dq_dqb.dqb_curinodes = 0;
908 if (dquot->dq_dqb.dqb_curinodes <= dquot->dq_dqb.dqb_isoftlimit)
909 dquot->dq_dqb.dqb_itime = (time_t) 0;
910 clear_bit(DQ_INODES_B, &dquot->dq_flags);
911}
912
913static inline void dquot_decr_space(struct dquot *dquot, qsize_t number)
914{
Jan Karadb49d2d2008-10-01 18:21:39 +0200915 if (sb_dqopt(dquot->dq_sb)->flags & DQUOT_NEGATIVE_USAGE ||
916 dquot->dq_dqb.dqb_curspace >= number)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 dquot->dq_dqb.dqb_curspace -= number;
918 else
919 dquot->dq_dqb.dqb_curspace = 0;
Jan Kara12095462008-08-20 14:45:12 +0200920 if (dquot->dq_dqb.dqb_curspace <= dquot->dq_dqb.dqb_bsoftlimit)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 dquot->dq_dqb.dqb_btime = (time_t) 0;
922 clear_bit(DQ_BLKS_B, &dquot->dq_flags);
923}
924
Jan Karac5254602007-12-22 14:03:25 -0800925static int warning_issued(struct dquot *dquot, const int warntype)
926{
927 int flag = (warntype == QUOTA_NL_BHARDWARN ||
928 warntype == QUOTA_NL_BSOFTLONGWARN) ? DQ_BLKS_B :
929 ((warntype == QUOTA_NL_IHARDWARN ||
930 warntype == QUOTA_NL_ISOFTLONGWARN) ? DQ_INODES_B : 0);
931
932 if (!flag)
933 return 0;
934 return test_and_set_bit(flag, &dquot->dq_flags);
935}
936
Jan Kara8e893462007-10-16 23:29:31 -0700937#ifdef CONFIG_PRINT_QUOTA_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938static int flag_print_warnings = 1;
939
940static inline int need_print_warning(struct dquot *dquot)
941{
942 if (!flag_print_warnings)
943 return 0;
944
945 switch (dquot->dq_type) {
946 case USRQUOTA:
David Howellsda9592e2008-11-14 10:39:05 +1100947 return current_fsuid() == dquot->dq_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 case GRPQUOTA:
949 return in_group_p(dquot->dq_id);
950 }
951 return 0;
952}
953
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954/* Print warning to user which exceeded quota */
Jan Karac5254602007-12-22 14:03:25 -0800955static void print_warning(struct dquot *dquot, const int warntype)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956{
957 char *msg = NULL;
Peter Zijlstra24ec8392006-12-08 02:36:04 -0800958 struct tty_struct *tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959
Jan Kara657d3bf2008-07-25 01:46:52 -0700960 if (warntype == QUOTA_NL_IHARDBELOW ||
961 warntype == QUOTA_NL_ISOFTBELOW ||
962 warntype == QUOTA_NL_BHARDBELOW ||
963 warntype == QUOTA_NL_BSOFTBELOW || !need_print_warning(dquot))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 return;
965
Peter Zijlstra24ec8392006-12-08 02:36:04 -0800966 tty = get_current_tty();
967 if (!tty)
Alan Cox452a00d2008-10-13 10:39:13 +0100968 return;
Peter Zijlstra24ec8392006-12-08 02:36:04 -0800969 tty_write_message(tty, dquot->dq_sb->s_id);
Jan Kara8e893462007-10-16 23:29:31 -0700970 if (warntype == QUOTA_NL_ISOFTWARN || warntype == QUOTA_NL_BSOFTWARN)
Peter Zijlstra24ec8392006-12-08 02:36:04 -0800971 tty_write_message(tty, ": warning, ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 else
Peter Zijlstra24ec8392006-12-08 02:36:04 -0800973 tty_write_message(tty, ": write failed, ");
974 tty_write_message(tty, quotatypes[dquot->dq_type]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 switch (warntype) {
Jan Kara8e893462007-10-16 23:29:31 -0700976 case QUOTA_NL_IHARDWARN:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 msg = " file limit reached.\r\n";
978 break;
Jan Kara8e893462007-10-16 23:29:31 -0700979 case QUOTA_NL_ISOFTLONGWARN:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 msg = " file quota exceeded too long.\r\n";
981 break;
Jan Kara8e893462007-10-16 23:29:31 -0700982 case QUOTA_NL_ISOFTWARN:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 msg = " file quota exceeded.\r\n";
984 break;
Jan Kara8e893462007-10-16 23:29:31 -0700985 case QUOTA_NL_BHARDWARN:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 msg = " block limit reached.\r\n";
987 break;
Jan Kara8e893462007-10-16 23:29:31 -0700988 case QUOTA_NL_BSOFTLONGWARN:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 msg = " block quota exceeded too long.\r\n";
990 break;
Jan Kara8e893462007-10-16 23:29:31 -0700991 case QUOTA_NL_BSOFTWARN:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 msg = " block quota exceeded.\r\n";
993 break;
994 }
Peter Zijlstra24ec8392006-12-08 02:36:04 -0800995 tty_write_message(tty, msg);
Alan Cox452a00d2008-10-13 10:39:13 +0100996 tty_kref_put(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997}
Jan Kara8e893462007-10-16 23:29:31 -0700998#endif
999
1000#ifdef CONFIG_QUOTA_NETLINK_INTERFACE
1001
Jan Kara8e893462007-10-16 23:29:31 -07001002/* Netlink family structure for quota */
1003static struct genl_family quota_genl_family = {
1004 .id = GENL_ID_GENERATE,
1005 .hdrsize = 0,
1006 .name = "VFS_DQUOT",
1007 .version = 1,
1008 .maxattr = QUOTA_NL_A_MAX,
1009};
1010
1011/* Send warning to userspace about user which exceeded quota */
1012static void send_warning(const struct dquot *dquot, const char warntype)
1013{
1014 static atomic_t seq;
1015 struct sk_buff *skb;
1016 void *msg_head;
1017 int ret;
Jan Kara22dd4832007-12-22 14:03:25 -08001018 int msg_size = 4 * nla_total_size(sizeof(u32)) +
1019 2 * nla_total_size(sizeof(u64));
Jan Kara8e893462007-10-16 23:29:31 -07001020
1021 /* We have to allocate using GFP_NOFS as we are called from a
1022 * filesystem performing write and thus further recursion into
1023 * the fs to free some data could cause deadlocks. */
Jan Kara22dd4832007-12-22 14:03:25 -08001024 skb = genlmsg_new(msg_size, GFP_NOFS);
Jan Kara8e893462007-10-16 23:29:31 -07001025 if (!skb) {
1026 printk(KERN_ERR
1027 "VFS: Not enough memory to send quota warning.\n");
1028 return;
1029 }
1030 msg_head = genlmsg_put(skb, 0, atomic_add_return(1, &seq),
1031 &quota_genl_family, 0, QUOTA_NL_C_WARNING);
1032 if (!msg_head) {
1033 printk(KERN_ERR
1034 "VFS: Cannot store netlink header in quota warning.\n");
1035 goto err_out;
1036 }
1037 ret = nla_put_u32(skb, QUOTA_NL_A_QTYPE, dquot->dq_type);
1038 if (ret)
1039 goto attr_err_out;
1040 ret = nla_put_u64(skb, QUOTA_NL_A_EXCESS_ID, dquot->dq_id);
1041 if (ret)
1042 goto attr_err_out;
1043 ret = nla_put_u32(skb, QUOTA_NL_A_WARNING, warntype);
1044 if (ret)
1045 goto attr_err_out;
1046 ret = nla_put_u32(skb, QUOTA_NL_A_DEV_MAJOR,
1047 MAJOR(dquot->dq_sb->s_dev));
1048 if (ret)
1049 goto attr_err_out;
1050 ret = nla_put_u32(skb, QUOTA_NL_A_DEV_MINOR,
1051 MINOR(dquot->dq_sb->s_dev));
1052 if (ret)
1053 goto attr_err_out;
David Howellsda9592e2008-11-14 10:39:05 +11001054 ret = nla_put_u64(skb, QUOTA_NL_A_CAUSED_ID, current_uid());
Jan Kara8e893462007-10-16 23:29:31 -07001055 if (ret)
1056 goto attr_err_out;
1057 genlmsg_end(skb, msg_head);
1058
1059 ret = genlmsg_multicast(skb, 0, quota_genl_family.id, GFP_NOFS);
1060 if (ret < 0 && ret != -ESRCH)
1061 printk(KERN_ERR
1062 "VFS: Failed to send notification message: %d\n", ret);
1063 return;
1064attr_err_out:
Jan Kara22dd4832007-12-22 14:03:25 -08001065 printk(KERN_ERR "VFS: Not enough space to compose quota message!\n");
Jan Kara8e893462007-10-16 23:29:31 -07001066err_out:
1067 kfree_skb(skb);
1068}
1069#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070
Jan Kara087ee8d2007-12-17 16:20:26 -08001071static inline void flush_warnings(struct dquot * const *dquots, char *warntype)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072{
1073 int i;
1074
1075 for (i = 0; i < MAXQUOTAS; i++)
Jan Karac5254602007-12-22 14:03:25 -08001076 if (dquots[i] != NODQUOT && warntype[i] != QUOTA_NL_NOWARN &&
1077 !warning_issued(dquots[i], warntype[i])) {
Jan Kara8e893462007-10-16 23:29:31 -07001078#ifdef CONFIG_PRINT_QUOTA_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 print_warning(dquots[i], warntype[i]);
Jan Kara8e893462007-10-16 23:29:31 -07001080#endif
1081#ifdef CONFIG_QUOTA_NETLINK_INTERFACE
1082 send_warning(dquots[i], warntype[i]);
1083#endif
1084 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085}
1086
1087static inline char ignore_hardlimit(struct dquot *dquot)
1088{
1089 struct mem_dqinfo *info = &sb_dqopt(dquot->dq_sb)->info[dquot->dq_type];
1090
1091 return capable(CAP_SYS_RESOURCE) &&
1092 (info->dqi_format->qf_fmt_id != QFMT_VFS_OLD || !(info->dqi_flags & V1_DQF_RSQUASH));
1093}
1094
1095/* needs dq_data_lock */
Jan Kara12095462008-08-20 14:45:12 +02001096static int check_idq(struct dquot *dquot, qsize_t inodes, char *warntype)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097{
Jan Kara8e893462007-10-16 23:29:31 -07001098 *warntype = QUOTA_NL_NOWARN;
Jan Karaf55abc02008-08-20 17:50:32 +02001099 if (!sb_has_quota_limits_enabled(dquot->dq_sb, dquot->dq_type) ||
1100 test_bit(DQ_FAKE_B, &dquot->dq_flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 return QUOTA_OK;
1102
1103 if (dquot->dq_dqb.dqb_ihardlimit &&
1104 (dquot->dq_dqb.dqb_curinodes + inodes) > dquot->dq_dqb.dqb_ihardlimit &&
1105 !ignore_hardlimit(dquot)) {
Jan Kara8e893462007-10-16 23:29:31 -07001106 *warntype = QUOTA_NL_IHARDWARN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 return NO_QUOTA;
1108 }
1109
1110 if (dquot->dq_dqb.dqb_isoftlimit &&
1111 (dquot->dq_dqb.dqb_curinodes + inodes) > dquot->dq_dqb.dqb_isoftlimit &&
1112 dquot->dq_dqb.dqb_itime && get_seconds() >= dquot->dq_dqb.dqb_itime &&
1113 !ignore_hardlimit(dquot)) {
Jan Kara8e893462007-10-16 23:29:31 -07001114 *warntype = QUOTA_NL_ISOFTLONGWARN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 return NO_QUOTA;
1116 }
1117
1118 if (dquot->dq_dqb.dqb_isoftlimit &&
1119 (dquot->dq_dqb.dqb_curinodes + inodes) > dquot->dq_dqb.dqb_isoftlimit &&
1120 dquot->dq_dqb.dqb_itime == 0) {
Jan Kara8e893462007-10-16 23:29:31 -07001121 *warntype = QUOTA_NL_ISOFTWARN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 dquot->dq_dqb.dqb_itime = get_seconds() + sb_dqopt(dquot->dq_sb)->info[dquot->dq_type].dqi_igrace;
1123 }
1124
1125 return QUOTA_OK;
1126}
1127
1128/* needs dq_data_lock */
1129static int check_bdq(struct dquot *dquot, qsize_t space, int prealloc, char *warntype)
1130{
Jan Kara8e893462007-10-16 23:29:31 -07001131 *warntype = QUOTA_NL_NOWARN;
Jan Karaf55abc02008-08-20 17:50:32 +02001132 if (!sb_has_quota_limits_enabled(dquot->dq_sb, dquot->dq_type) ||
1133 test_bit(DQ_FAKE_B, &dquot->dq_flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 return QUOTA_OK;
1135
1136 if (dquot->dq_dqb.dqb_bhardlimit &&
Jan Kara12095462008-08-20 14:45:12 +02001137 dquot->dq_dqb.dqb_curspace + space > dquot->dq_dqb.dqb_bhardlimit &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 !ignore_hardlimit(dquot)) {
1139 if (!prealloc)
Jan Kara8e893462007-10-16 23:29:31 -07001140 *warntype = QUOTA_NL_BHARDWARN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 return NO_QUOTA;
1142 }
1143
1144 if (dquot->dq_dqb.dqb_bsoftlimit &&
Jan Kara12095462008-08-20 14:45:12 +02001145 dquot->dq_dqb.dqb_curspace + space > dquot->dq_dqb.dqb_bsoftlimit &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 dquot->dq_dqb.dqb_btime && get_seconds() >= dquot->dq_dqb.dqb_btime &&
1147 !ignore_hardlimit(dquot)) {
1148 if (!prealloc)
Jan Kara8e893462007-10-16 23:29:31 -07001149 *warntype = QUOTA_NL_BSOFTLONGWARN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 return NO_QUOTA;
1151 }
1152
1153 if (dquot->dq_dqb.dqb_bsoftlimit &&
Jan Kara12095462008-08-20 14:45:12 +02001154 dquot->dq_dqb.dqb_curspace + space > dquot->dq_dqb.dqb_bsoftlimit &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 dquot->dq_dqb.dqb_btime == 0) {
1156 if (!prealloc) {
Jan Kara8e893462007-10-16 23:29:31 -07001157 *warntype = QUOTA_NL_BSOFTWARN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 dquot->dq_dqb.dqb_btime = get_seconds() + sb_dqopt(dquot->dq_sb)->info[dquot->dq_type].dqi_bgrace;
1159 }
1160 else
1161 /*
1162 * We don't allow preallocation to exceed softlimit so exceeding will
1163 * be always printed
1164 */
1165 return NO_QUOTA;
1166 }
1167
1168 return QUOTA_OK;
1169}
1170
Jan Kara12095462008-08-20 14:45:12 +02001171static int info_idq_free(struct dquot *dquot, qsize_t inodes)
Jan Kara657d3bf2008-07-25 01:46:52 -07001172{
1173 if (test_bit(DQ_FAKE_B, &dquot->dq_flags) ||
Jan Karaf55abc02008-08-20 17:50:32 +02001174 dquot->dq_dqb.dqb_curinodes <= dquot->dq_dqb.dqb_isoftlimit ||
1175 !sb_has_quota_limits_enabled(dquot->dq_sb, dquot->dq_type))
Jan Kara657d3bf2008-07-25 01:46:52 -07001176 return QUOTA_NL_NOWARN;
1177
1178 if (dquot->dq_dqb.dqb_curinodes - inodes <= dquot->dq_dqb.dqb_isoftlimit)
1179 return QUOTA_NL_ISOFTBELOW;
1180 if (dquot->dq_dqb.dqb_curinodes >= dquot->dq_dqb.dqb_ihardlimit &&
1181 dquot->dq_dqb.dqb_curinodes - inodes < dquot->dq_dqb.dqb_ihardlimit)
1182 return QUOTA_NL_IHARDBELOW;
1183 return QUOTA_NL_NOWARN;
1184}
1185
1186static int info_bdq_free(struct dquot *dquot, qsize_t space)
1187{
1188 if (test_bit(DQ_FAKE_B, &dquot->dq_flags) ||
Jan Kara12095462008-08-20 14:45:12 +02001189 dquot->dq_dqb.dqb_curspace <= dquot->dq_dqb.dqb_bsoftlimit)
Jan Kara657d3bf2008-07-25 01:46:52 -07001190 return QUOTA_NL_NOWARN;
1191
Jan Kara12095462008-08-20 14:45:12 +02001192 if (dquot->dq_dqb.dqb_curspace - space <= dquot->dq_dqb.dqb_bsoftlimit)
Jan Kara657d3bf2008-07-25 01:46:52 -07001193 return QUOTA_NL_BSOFTBELOW;
Jan Kara12095462008-08-20 14:45:12 +02001194 if (dquot->dq_dqb.dqb_curspace >= dquot->dq_dqb.dqb_bhardlimit &&
1195 dquot->dq_dqb.dqb_curspace - space < dquot->dq_dqb.dqb_bhardlimit)
Jan Kara657d3bf2008-07-25 01:46:52 -07001196 return QUOTA_NL_BHARDBELOW;
1197 return QUOTA_NL_NOWARN;
1198}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199/*
1200 * Initialize quota pointers in inode
1201 * Transaction must be started at entry
1202 */
1203int dquot_initialize(struct inode *inode, int type)
1204{
1205 unsigned int id = 0;
1206 int cnt, ret = 0;
1207
Ingo Molnard3be9152006-03-23 03:00:29 -08001208 /* First test before acquiring mutex - solves deadlocks when we
1209 * re-enter the quota code and are already holding the mutex */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 if (IS_NOQUOTA(inode))
1211 return 0;
1212 down_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
1213 /* Having dqptr_sem we know NOQUOTA flags can't be altered... */
1214 if (IS_NOQUOTA(inode))
1215 goto out_err;
1216 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1217 if (type != -1 && cnt != type)
1218 continue;
1219 if (inode->i_dquot[cnt] == NODQUOT) {
1220 switch (cnt) {
1221 case USRQUOTA:
1222 id = inode->i_uid;
1223 break;
1224 case GRPQUOTA:
1225 id = inode->i_gid;
1226 break;
1227 }
1228 inode->i_dquot[cnt] = dqget(inode->i_sb, id, cnt);
1229 }
1230 }
1231out_err:
1232 up_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
1233 return ret;
1234}
1235
1236/*
1237 * Release all quotas referenced by inode
1238 * Transaction must be started at an entry
1239 */
Jan Kara3d9ea252008-10-10 16:12:23 +02001240int dquot_drop_locked(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241{
1242 int cnt;
1243
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1245 if (inode->i_dquot[cnt] != NODQUOT) {
1246 dqput(inode->i_dquot[cnt]);
1247 inode->i_dquot[cnt] = NODQUOT;
1248 }
1249 }
Jan Kara3d9ea252008-10-10 16:12:23 +02001250 return 0;
1251}
1252
1253int dquot_drop(struct inode *inode)
1254{
1255 down_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
1256 dquot_drop_locked(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257 up_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
1258 return 0;
1259}
1260
Jan Karab85f4b82008-07-25 01:46:50 -07001261/* Wrapper to remove references to quota structures from inode */
1262void vfs_dq_drop(struct inode *inode)
1263{
1264 /* Here we can get arbitrary inode from clear_inode() so we have
1265 * to be careful. OTOH we don't need locking as quota operations
1266 * are allowed to change only at mount time */
1267 if (!IS_NOQUOTA(inode) && inode->i_sb && inode->i_sb->dq_op
1268 && inode->i_sb->dq_op->drop) {
1269 int cnt;
1270 /* Test before calling to rule out calls from proc and such
1271 * where we are not allowed to block. Note that this is
1272 * actually reliable test even without the lock - the caller
1273 * must assure that nobody can come after the DQUOT_DROP and
1274 * add quota pointers back anyway */
1275 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1276 if (inode->i_dquot[cnt] != NODQUOT)
1277 break;
1278 if (cnt < MAXQUOTAS)
1279 inode->i_sb->dq_op->drop(inode);
1280 }
1281}
1282
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283/*
1284 * Following four functions update i_blocks+i_bytes fields and
1285 * quota information (together with appropriate checks)
1286 * NOTE: We absolutely rely on the fact that caller dirties
1287 * the inode (usually macros in quotaops.h care about this) and
1288 * holds a handle for the current transaction so that dquot write and
1289 * inode write go into the same transaction.
1290 */
1291
1292/*
1293 * This operation can block, but only after everything is updated
1294 */
1295int dquot_alloc_space(struct inode *inode, qsize_t number, int warn)
1296{
1297 int cnt, ret = NO_QUOTA;
1298 char warntype[MAXQUOTAS];
1299
Ingo Molnard3be9152006-03-23 03:00:29 -08001300 /* First test before acquiring mutex - solves deadlocks when we
1301 * re-enter the quota code and are already holding the mutex */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 if (IS_NOQUOTA(inode)) {
1303out_add:
1304 inode_add_bytes(inode, number);
1305 return QUOTA_OK;
1306 }
1307 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
Jan Kara8e893462007-10-16 23:29:31 -07001308 warntype[cnt] = QUOTA_NL_NOWARN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309
1310 down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1311 if (IS_NOQUOTA(inode)) { /* Now we can do reliable test... */
1312 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1313 goto out_add;
1314 }
1315 spin_lock(&dq_data_lock);
1316 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1317 if (inode->i_dquot[cnt] == NODQUOT)
1318 continue;
1319 if (check_bdq(inode->i_dquot[cnt], number, warn, warntype+cnt) == NO_QUOTA)
1320 goto warn_put_all;
1321 }
1322 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1323 if (inode->i_dquot[cnt] == NODQUOT)
1324 continue;
1325 dquot_incr_space(inode->i_dquot[cnt], number);
1326 }
1327 inode_add_bytes(inode, number);
1328 ret = QUOTA_OK;
1329warn_put_all:
1330 spin_unlock(&dq_data_lock);
1331 if (ret == QUOTA_OK)
1332 /* Dirtify all the dquots - this can block when journalling */
1333 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1334 if (inode->i_dquot[cnt])
1335 mark_dquot_dirty(inode->i_dquot[cnt]);
1336 flush_warnings(inode->i_dquot, warntype);
1337 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1338 return ret;
1339}
1340
1341/*
1342 * This operation can block, but only after everything is updated
1343 */
Jan Kara12095462008-08-20 14:45:12 +02001344int dquot_alloc_inode(const struct inode *inode, qsize_t number)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345{
1346 int cnt, ret = NO_QUOTA;
1347 char warntype[MAXQUOTAS];
1348
Ingo Molnard3be9152006-03-23 03:00:29 -08001349 /* First test before acquiring mutex - solves deadlocks when we
1350 * re-enter the quota code and are already holding the mutex */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 if (IS_NOQUOTA(inode))
1352 return QUOTA_OK;
1353 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
Jan Kara8e893462007-10-16 23:29:31 -07001354 warntype[cnt] = QUOTA_NL_NOWARN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1356 if (IS_NOQUOTA(inode)) {
1357 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1358 return QUOTA_OK;
1359 }
1360 spin_lock(&dq_data_lock);
1361 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1362 if (inode->i_dquot[cnt] == NODQUOT)
1363 continue;
1364 if (check_idq(inode->i_dquot[cnt], number, warntype+cnt) == NO_QUOTA)
1365 goto warn_put_all;
1366 }
1367
1368 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1369 if (inode->i_dquot[cnt] == NODQUOT)
1370 continue;
1371 dquot_incr_inodes(inode->i_dquot[cnt], number);
1372 }
1373 ret = QUOTA_OK;
1374warn_put_all:
1375 spin_unlock(&dq_data_lock);
1376 if (ret == QUOTA_OK)
1377 /* Dirtify all the dquots - this can block when journalling */
1378 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1379 if (inode->i_dquot[cnt])
1380 mark_dquot_dirty(inode->i_dquot[cnt]);
Jan Kara087ee8d2007-12-17 16:20:26 -08001381 flush_warnings(inode->i_dquot, warntype);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1383 return ret;
1384}
1385
1386/*
1387 * This operation can block, but only after everything is updated
1388 */
1389int dquot_free_space(struct inode *inode, qsize_t number)
1390{
1391 unsigned int cnt;
Jan Kara657d3bf2008-07-25 01:46:52 -07001392 char warntype[MAXQUOTAS];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393
Ingo Molnard3be9152006-03-23 03:00:29 -08001394 /* First test before acquiring mutex - solves deadlocks when we
1395 * re-enter the quota code and are already holding the mutex */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396 if (IS_NOQUOTA(inode)) {
1397out_sub:
1398 inode_sub_bytes(inode, number);
1399 return QUOTA_OK;
1400 }
Jan Kara657d3bf2008-07-25 01:46:52 -07001401
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1403 /* Now recheck reliably when holding dqptr_sem */
1404 if (IS_NOQUOTA(inode)) {
1405 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1406 goto out_sub;
1407 }
1408 spin_lock(&dq_data_lock);
1409 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1410 if (inode->i_dquot[cnt] == NODQUOT)
1411 continue;
Jan Kara657d3bf2008-07-25 01:46:52 -07001412 warntype[cnt] = info_bdq_free(inode->i_dquot[cnt], number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 dquot_decr_space(inode->i_dquot[cnt], number);
1414 }
1415 inode_sub_bytes(inode, number);
1416 spin_unlock(&dq_data_lock);
1417 /* Dirtify all the dquots - this can block when journalling */
1418 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1419 if (inode->i_dquot[cnt])
1420 mark_dquot_dirty(inode->i_dquot[cnt]);
Jan Kara657d3bf2008-07-25 01:46:52 -07001421 flush_warnings(inode->i_dquot, warntype);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1423 return QUOTA_OK;
1424}
1425
1426/*
1427 * This operation can block, but only after everything is updated
1428 */
Jan Kara12095462008-08-20 14:45:12 +02001429int dquot_free_inode(const struct inode *inode, qsize_t number)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430{
1431 unsigned int cnt;
Jan Kara657d3bf2008-07-25 01:46:52 -07001432 char warntype[MAXQUOTAS];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433
Ingo Molnard3be9152006-03-23 03:00:29 -08001434 /* First test before acquiring mutex - solves deadlocks when we
1435 * re-enter the quota code and are already holding the mutex */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436 if (IS_NOQUOTA(inode))
1437 return QUOTA_OK;
Jan Kara657d3bf2008-07-25 01:46:52 -07001438
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439 down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1440 /* Now recheck reliably when holding dqptr_sem */
1441 if (IS_NOQUOTA(inode)) {
1442 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1443 return QUOTA_OK;
1444 }
1445 spin_lock(&dq_data_lock);
1446 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1447 if (inode->i_dquot[cnt] == NODQUOT)
1448 continue;
Jan Kara657d3bf2008-07-25 01:46:52 -07001449 warntype[cnt] = info_idq_free(inode->i_dquot[cnt], number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450 dquot_decr_inodes(inode->i_dquot[cnt], number);
1451 }
1452 spin_unlock(&dq_data_lock);
1453 /* Dirtify all the dquots - this can block when journalling */
1454 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1455 if (inode->i_dquot[cnt])
1456 mark_dquot_dirty(inode->i_dquot[cnt]);
Jan Kara657d3bf2008-07-25 01:46:52 -07001457 flush_warnings(inode->i_dquot, warntype);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1459 return QUOTA_OK;
1460}
1461
1462/*
1463 * Transfer the number of inode and blocks from one diskquota to an other.
1464 *
1465 * This operation can block, but only after everything is updated
1466 * A transaction must be started when entering this function.
1467 */
1468int dquot_transfer(struct inode *inode, struct iattr *iattr)
1469{
1470 qsize_t space;
1471 struct dquot *transfer_from[MAXQUOTAS];
1472 struct dquot *transfer_to[MAXQUOTAS];
1473 int cnt, ret = NO_QUOTA, chuid = (iattr->ia_valid & ATTR_UID) && inode->i_uid != iattr->ia_uid,
1474 chgid = (iattr->ia_valid & ATTR_GID) && inode->i_gid != iattr->ia_gid;
Jan Kara657d3bf2008-07-25 01:46:52 -07001475 char warntype_to[MAXQUOTAS];
1476 char warntype_from_inodes[MAXQUOTAS], warntype_from_space[MAXQUOTAS];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477
Ingo Molnard3be9152006-03-23 03:00:29 -08001478 /* First test before acquiring mutex - solves deadlocks when we
1479 * re-enter the quota code and are already holding the mutex */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 if (IS_NOQUOTA(inode))
1481 return QUOTA_OK;
1482 /* Clear the arrays */
1483 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1484 transfer_to[cnt] = transfer_from[cnt] = NODQUOT;
Jan Kara657d3bf2008-07-25 01:46:52 -07001485 warntype_to[cnt] = QUOTA_NL_NOWARN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486 }
1487 down_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
1488 /* Now recheck reliably when holding dqptr_sem */
1489 if (IS_NOQUOTA(inode)) { /* File without quota accounting? */
1490 up_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
1491 return QUOTA_OK;
1492 }
1493 /* First build the transfer_to list - here we can block on
1494 * reading/instantiating of dquots. We know that the transaction for
1495 * us was already started so we don't violate lock ranking here */
1496 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1497 switch (cnt) {
1498 case USRQUOTA:
1499 if (!chuid)
1500 continue;
1501 transfer_to[cnt] = dqget(inode->i_sb, iattr->ia_uid, cnt);
1502 break;
1503 case GRPQUOTA:
1504 if (!chgid)
1505 continue;
1506 transfer_to[cnt] = dqget(inode->i_sb, iattr->ia_gid, cnt);
1507 break;
1508 }
1509 }
1510 spin_lock(&dq_data_lock);
1511 space = inode_get_bytes(inode);
1512 /* Build the transfer_from list and check the limits */
1513 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1514 if (transfer_to[cnt] == NODQUOT)
1515 continue;
1516 transfer_from[cnt] = inode->i_dquot[cnt];
Jan Kara657d3bf2008-07-25 01:46:52 -07001517 if (check_idq(transfer_to[cnt], 1, warntype_to + cnt) ==
1518 NO_QUOTA || check_bdq(transfer_to[cnt], space, 0,
1519 warntype_to + cnt) == NO_QUOTA)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520 goto warn_put_all;
1521 }
1522
1523 /*
1524 * Finally perform the needed transfer from transfer_from to transfer_to
1525 */
1526 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1527 /*
1528 * Skip changes for same uid or gid or for turned off quota-type.
1529 */
1530 if (transfer_to[cnt] == NODQUOT)
1531 continue;
1532
1533 /* Due to IO error we might not have transfer_from[] structure */
1534 if (transfer_from[cnt]) {
Jan Kara657d3bf2008-07-25 01:46:52 -07001535 warntype_from_inodes[cnt] =
1536 info_idq_free(transfer_from[cnt], 1);
1537 warntype_from_space[cnt] =
1538 info_bdq_free(transfer_from[cnt], space);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539 dquot_decr_inodes(transfer_from[cnt], 1);
1540 dquot_decr_space(transfer_from[cnt], space);
1541 }
1542
1543 dquot_incr_inodes(transfer_to[cnt], 1);
1544 dquot_incr_space(transfer_to[cnt], space);
1545
1546 inode->i_dquot[cnt] = transfer_to[cnt];
1547 }
1548 ret = QUOTA_OK;
1549warn_put_all:
1550 spin_unlock(&dq_data_lock);
1551 /* Dirtify all the dquots - this can block when journalling */
1552 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1553 if (transfer_from[cnt])
1554 mark_dquot_dirty(transfer_from[cnt]);
1555 if (transfer_to[cnt])
1556 mark_dquot_dirty(transfer_to[cnt]);
1557 }
Jan Kara657d3bf2008-07-25 01:46:52 -07001558 flush_warnings(transfer_to, warntype_to);
1559 flush_warnings(transfer_from, warntype_from_inodes);
1560 flush_warnings(transfer_from, warntype_from_space);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561
1562 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1563 if (ret == QUOTA_OK && transfer_from[cnt] != NODQUOT)
1564 dqput(transfer_from[cnt]);
1565 if (ret == NO_QUOTA && transfer_to[cnt] != NODQUOT)
1566 dqput(transfer_to[cnt]);
1567 }
1568 up_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
1569 return ret;
1570}
1571
Jan Karab85f4b82008-07-25 01:46:50 -07001572/* Wrapper for transferring ownership of an inode */
1573int vfs_dq_transfer(struct inode *inode, struct iattr *iattr)
1574{
Jan Karaf55abc02008-08-20 17:50:32 +02001575 if (sb_any_quota_active(inode->i_sb) && !IS_NOQUOTA(inode)) {
Jan Karab85f4b82008-07-25 01:46:50 -07001576 vfs_dq_init(inode);
1577 if (inode->i_sb->dq_op->transfer(inode, iattr) == NO_QUOTA)
1578 return 1;
1579 }
1580 return 0;
1581}
1582
1583
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584/*
1585 * Write info of quota file to disk
1586 */
1587int dquot_commit_info(struct super_block *sb, int type)
1588{
1589 int ret;
1590 struct quota_info *dqopt = sb_dqopt(sb);
1591
Ingo Molnard3be9152006-03-23 03:00:29 -08001592 mutex_lock(&dqopt->dqio_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593 ret = dqopt->ops[type]->write_file_info(sb, type);
Ingo Molnard3be9152006-03-23 03:00:29 -08001594 mutex_unlock(&dqopt->dqio_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595 return ret;
1596}
1597
1598/*
1599 * Definitions of diskquota operations.
1600 */
1601struct dquot_operations dquot_operations = {
1602 .initialize = dquot_initialize,
1603 .drop = dquot_drop,
1604 .alloc_space = dquot_alloc_space,
1605 .alloc_inode = dquot_alloc_inode,
1606 .free_space = dquot_free_space,
1607 .free_inode = dquot_free_inode,
1608 .transfer = dquot_transfer,
1609 .write_dquot = dquot_commit,
1610 .acquire_dquot = dquot_acquire,
1611 .release_dquot = dquot_release,
1612 .mark_dirty = dquot_mark_dquot_dirty,
Jan Kara74f783a2008-08-19 14:51:22 +02001613 .write_info = dquot_commit_info,
1614 .alloc_dquot = dquot_alloc,
1615 .destroy_dquot = dquot_destroy,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616};
1617
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618/*
1619 * Turn quota off on a device. type == -1 ==> quotaoff for all types (umount)
1620 */
Jan Karaf55abc02008-08-20 17:50:32 +02001621int vfs_quota_disable(struct super_block *sb, int type, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622{
Jan Kara0ff5af82008-04-28 02:14:33 -07001623 int cnt, ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624 struct quota_info *dqopt = sb_dqopt(sb);
1625 struct inode *toputinode[MAXQUOTAS];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626
Jan Karaf55abc02008-08-20 17:50:32 +02001627 /* Cannot turn off usage accounting without turning off limits, or
1628 * suspend quotas and simultaneously turn quotas off. */
1629 if ((flags & DQUOT_USAGE_ENABLED && !(flags & DQUOT_LIMITS_ENABLED))
1630 || (flags & DQUOT_SUSPENDED && flags & (DQUOT_LIMITS_ENABLED |
1631 DQUOT_USAGE_ENABLED)))
1632 return -EINVAL;
1633
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634 /* We need to serialize quota_off() for device */
Ingo Molnard3be9152006-03-23 03:00:29 -08001635 mutex_lock(&dqopt->dqonoff_mutex);
Jan Kara9377abd2008-05-12 14:02:08 -07001636
1637 /*
1638 * Skip everything if there's nothing to do. We have to do this because
1639 * sometimes we are called when fill_super() failed and calling
1640 * sync_fs() in such cases does no good.
1641 */
Jan Karaf55abc02008-08-20 17:50:32 +02001642 if (!sb_any_quota_loaded(sb)) {
Jan Kara9377abd2008-05-12 14:02:08 -07001643 mutex_unlock(&dqopt->dqonoff_mutex);
1644 return 0;
1645 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1647 toputinode[cnt] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648 if (type != -1 && cnt != type)
1649 continue;
Jan Karaf55abc02008-08-20 17:50:32 +02001650 if (!sb_has_quota_loaded(sb, cnt))
Jan Kara0ff5af82008-04-28 02:14:33 -07001651 continue;
Jan Karaf55abc02008-08-20 17:50:32 +02001652
1653 if (flags & DQUOT_SUSPENDED) {
1654 dqopt->flags |=
1655 dquot_state_flag(DQUOT_SUSPENDED, cnt);
1656 } else {
1657 dqopt->flags &= ~dquot_state_flag(flags, cnt);
1658 /* Turning off suspended quotas? */
1659 if (!sb_has_quota_loaded(sb, cnt) &&
1660 sb_has_quota_suspended(sb, cnt)) {
1661 dqopt->flags &= ~dquot_state_flag(
1662 DQUOT_SUSPENDED, cnt);
1663 iput(dqopt->files[cnt]);
1664 dqopt->files[cnt] = NULL;
1665 continue;
1666 }
Jan Kara0ff5af82008-04-28 02:14:33 -07001667 }
Jan Karaf55abc02008-08-20 17:50:32 +02001668
1669 /* We still have to keep quota loaded? */
1670 if (sb_has_quota_loaded(sb, cnt) && !(flags & DQUOT_SUSPENDED))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672
1673 /* Note: these are blocking operations */
1674 drop_dquot_ref(sb, cnt);
1675 invalidate_dquots(sb, cnt);
1676 /*
1677 * Now all dquots should be invalidated, all writes done so we should be only
1678 * users of the info. No locks needed.
1679 */
1680 if (info_dirty(&dqopt->info[cnt]))
1681 sb->dq_op->write_info(sb, cnt);
1682 if (dqopt->ops[cnt]->free_file_info)
1683 dqopt->ops[cnt]->free_file_info(sb, cnt);
1684 put_quota_format(dqopt->info[cnt].dqi_format);
1685
1686 toputinode[cnt] = dqopt->files[cnt];
Jan Karaf55abc02008-08-20 17:50:32 +02001687 if (!sb_has_quota_loaded(sb, cnt))
Jan Kara0ff5af82008-04-28 02:14:33 -07001688 dqopt->files[cnt] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689 dqopt->info[cnt].dqi_flags = 0;
1690 dqopt->info[cnt].dqi_igrace = 0;
1691 dqopt->info[cnt].dqi_bgrace = 0;
1692 dqopt->ops[cnt] = NULL;
1693 }
Ingo Molnard3be9152006-03-23 03:00:29 -08001694 mutex_unlock(&dqopt->dqonoff_mutex);
Jan Karaca785ec2008-09-30 17:53:37 +02001695
1696 /* Skip syncing and setting flags if quota files are hidden */
1697 if (dqopt->flags & DQUOT_QUOTA_SYS_FILE)
1698 goto put_inodes;
1699
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700 /* Sync the superblock so that buffers with quota data are written to
Al Viro7b7b1ac2005-11-07 17:13:39 -05001701 * disk (and so userspace sees correct data afterwards). */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702 if (sb->s_op->sync_fs)
1703 sb->s_op->sync_fs(sb, 1);
1704 sync_blockdev(sb->s_bdev);
1705 /* Now the quota files are just ordinary files and we can set the
1706 * inode flags back. Moreover we discard the pagecache so that
1707 * userspace sees the writes we did bypassing the pagecache. We
1708 * must also discard the blockdev buffers so that we see the
1709 * changes done by userspace on the next quotaon() */
1710 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1711 if (toputinode[cnt]) {
Ingo Molnard3be9152006-03-23 03:00:29 -08001712 mutex_lock(&dqopt->dqonoff_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713 /* If quota was reenabled in the meantime, we have
1714 * nothing to do */
Jan Karaf55abc02008-08-20 17:50:32 +02001715 if (!sb_has_quota_loaded(sb, cnt)) {
Jan Kara79254092007-05-16 22:11:19 -07001716 mutex_lock_nested(&toputinode[cnt]->i_mutex, I_MUTEX_QUOTA);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717 toputinode[cnt]->i_flags &= ~(S_IMMUTABLE |
1718 S_NOATIME | S_NOQUOTA);
1719 truncate_inode_pages(&toputinode[cnt]->i_data, 0);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001720 mutex_unlock(&toputinode[cnt]->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721 mark_inode_dirty(toputinode[cnt]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722 }
Ingo Molnard3be9152006-03-23 03:00:29 -08001723 mutex_unlock(&dqopt->dqonoff_mutex);
Jan Karaca785ec2008-09-30 17:53:37 +02001724 }
1725 if (sb->s_bdev)
1726 invalidate_bdev(sb->s_bdev);
1727put_inodes:
1728 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1729 if (toputinode[cnt]) {
Jan Kara0ff5af82008-04-28 02:14:33 -07001730 /* On remount RO, we keep the inode pointer so that we
Jan Karaf55abc02008-08-20 17:50:32 +02001731 * can reenable quota on the subsequent remount RW. We
1732 * have to check 'flags' variable and not use sb_has_
1733 * function because another quotaon / quotaoff could
1734 * change global state before we got here. We refuse
1735 * to suspend quotas when there is pending delete on
1736 * the quota file... */
1737 if (!(flags & DQUOT_SUSPENDED))
Jan Kara0ff5af82008-04-28 02:14:33 -07001738 iput(toputinode[cnt]);
1739 else if (!toputinode[cnt]->i_nlink)
1740 ret = -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741 }
Jan Kara0ff5af82008-04-28 02:14:33 -07001742 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743}
1744
Jan Karaf55abc02008-08-20 17:50:32 +02001745int vfs_quota_off(struct super_block *sb, int type, int remount)
1746{
1747 return vfs_quota_disable(sb, type, remount ? DQUOT_SUSPENDED :
1748 (DQUOT_USAGE_ENABLED | DQUOT_LIMITS_ENABLED));
1749}
1750
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751/*
1752 * Turn quotas on on a device
1753 */
1754
Jan Karaf55abc02008-08-20 17:50:32 +02001755/*
1756 * Helper function to turn quotas on when we already have the inode of
1757 * quota file and no quota information is loaded.
1758 */
1759static int vfs_load_quota_inode(struct inode *inode, int type, int format_id,
1760 unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761{
1762 struct quota_format_type *fmt = find_quota_format(format_id);
1763 struct super_block *sb = inode->i_sb;
1764 struct quota_info *dqopt = sb_dqopt(sb);
1765 int error;
1766 int oldflags = -1;
1767
1768 if (!fmt)
1769 return -ESRCH;
1770 if (!S_ISREG(inode->i_mode)) {
1771 error = -EACCES;
1772 goto out_fmt;
1773 }
1774 if (IS_RDONLY(inode)) {
1775 error = -EROFS;
1776 goto out_fmt;
1777 }
1778 if (!sb->s_op->quota_write || !sb->s_op->quota_read) {
1779 error = -EINVAL;
1780 goto out_fmt;
1781 }
Jan Karaf55abc02008-08-20 17:50:32 +02001782 /* Usage always has to be set... */
1783 if (!(flags & DQUOT_USAGE_ENABLED)) {
1784 error = -EINVAL;
1785 goto out_fmt;
1786 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787
Jan Karaca785ec2008-09-30 17:53:37 +02001788 if (!(dqopt->flags & DQUOT_QUOTA_SYS_FILE)) {
1789 /* As we bypass the pagecache we must now flush the inode so
1790 * that we see all the changes from userspace... */
1791 write_inode_now(inode, 1);
1792 /* And now flush the block cache so that kernel sees the
1793 * changes */
1794 invalidate_bdev(sb->s_bdev);
1795 }
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001796 mutex_lock(&inode->i_mutex);
Ingo Molnard3be9152006-03-23 03:00:29 -08001797 mutex_lock(&dqopt->dqonoff_mutex);
Jan Karaf55abc02008-08-20 17:50:32 +02001798 if (sb_has_quota_loaded(sb, type)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799 error = -EBUSY;
1800 goto out_lock;
1801 }
Jan Karaca785ec2008-09-30 17:53:37 +02001802
1803 if (!(dqopt->flags & DQUOT_QUOTA_SYS_FILE)) {
1804 /* We don't want quota and atime on quota files (deadlocks
1805 * possible) Also nobody should write to the file - we use
1806 * special IO operations which ignore the immutable bit. */
1807 down_write(&dqopt->dqptr_sem);
1808 oldflags = inode->i_flags & (S_NOATIME | S_IMMUTABLE | S_NOQUOTA);
1809 inode->i_flags |= S_NOQUOTA | S_NOATIME | S_IMMUTABLE;
1810 up_write(&dqopt->dqptr_sem);
1811 sb->dq_op->drop(inode);
1812 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813
1814 error = -EIO;
1815 dqopt->files[type] = igrab(inode);
1816 if (!dqopt->files[type])
1817 goto out_lock;
1818 error = -EINVAL;
1819 if (!fmt->qf_ops->check_quota_file(sb, type))
1820 goto out_file_init;
1821
1822 dqopt->ops[type] = fmt->qf_ops;
1823 dqopt->info[type].dqi_format = fmt;
Jan Kara0ff5af82008-04-28 02:14:33 -07001824 dqopt->info[type].dqi_fmt_id = format_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825 INIT_LIST_HEAD(&dqopt->info[type].dqi_dirty_list);
Ingo Molnard3be9152006-03-23 03:00:29 -08001826 mutex_lock(&dqopt->dqio_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827 if ((error = dqopt->ops[type]->read_file_info(sb, type)) < 0) {
Ingo Molnard3be9152006-03-23 03:00:29 -08001828 mutex_unlock(&dqopt->dqio_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829 goto out_file_init;
1830 }
Ingo Molnard3be9152006-03-23 03:00:29 -08001831 mutex_unlock(&dqopt->dqio_mutex);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001832 mutex_unlock(&inode->i_mutex);
Jan Karaf55abc02008-08-20 17:50:32 +02001833 dqopt->flags |= dquot_state_flag(flags, type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834
1835 add_dquot_ref(sb, type);
Ingo Molnard3be9152006-03-23 03:00:29 -08001836 mutex_unlock(&dqopt->dqonoff_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837
1838 return 0;
1839
1840out_file_init:
1841 dqopt->files[type] = NULL;
1842 iput(inode);
1843out_lock:
Ingo Molnard3be9152006-03-23 03:00:29 -08001844 mutex_unlock(&dqopt->dqonoff_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845 if (oldflags != -1) {
1846 down_write(&dqopt->dqptr_sem);
1847 /* Set the flags back (in the case of accidental quotaon()
1848 * on a wrong file we don't want to mess up the flags) */
1849 inode->i_flags &= ~(S_NOATIME | S_NOQUOTA | S_IMMUTABLE);
1850 inode->i_flags |= oldflags;
1851 up_write(&dqopt->dqptr_sem);
1852 }
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001853 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854out_fmt:
1855 put_quota_format(fmt);
1856
1857 return error;
1858}
1859
Jan Kara0ff5af82008-04-28 02:14:33 -07001860/* Reenable quotas on remount RW */
1861static int vfs_quota_on_remount(struct super_block *sb, int type)
1862{
1863 struct quota_info *dqopt = sb_dqopt(sb);
1864 struct inode *inode;
1865 int ret;
Jan Karaf55abc02008-08-20 17:50:32 +02001866 unsigned int flags;
Jan Kara0ff5af82008-04-28 02:14:33 -07001867
1868 mutex_lock(&dqopt->dqonoff_mutex);
1869 if (!sb_has_quota_suspended(sb, type)) {
1870 mutex_unlock(&dqopt->dqonoff_mutex);
1871 return 0;
1872 }
Jan Kara0ff5af82008-04-28 02:14:33 -07001873 inode = dqopt->files[type];
1874 dqopt->files[type] = NULL;
Jan Karaf55abc02008-08-20 17:50:32 +02001875 flags = dqopt->flags & dquot_state_flag(DQUOT_USAGE_ENABLED |
1876 DQUOT_LIMITS_ENABLED, type);
1877 dqopt->flags &= ~dquot_state_flag(DQUOT_STATE_FLAGS, type);
Jan Kara0ff5af82008-04-28 02:14:33 -07001878 mutex_unlock(&dqopt->dqonoff_mutex);
1879
Jan Karaf55abc02008-08-20 17:50:32 +02001880 flags = dquot_generic_flag(flags, type);
1881 ret = vfs_load_quota_inode(inode, type, dqopt->info[type].dqi_fmt_id,
1882 flags);
Jan Kara0ff5af82008-04-28 02:14:33 -07001883 iput(inode);
1884
1885 return ret;
1886}
1887
Al Viro77e69da2008-08-01 04:29:18 -04001888int vfs_quota_on_path(struct super_block *sb, int type, int format_id,
1889 struct path *path)
1890{
1891 int error = security_quota_on(path->dentry);
1892 if (error)
1893 return error;
1894 /* Quota file not on the same filesystem? */
1895 if (path->mnt->mnt_sb != sb)
1896 error = -EXDEV;
1897 else
Jan Karaf55abc02008-08-20 17:50:32 +02001898 error = vfs_load_quota_inode(path->dentry->d_inode, type,
1899 format_id, DQUOT_USAGE_ENABLED |
1900 DQUOT_LIMITS_ENABLED);
Al Viro77e69da2008-08-01 04:29:18 -04001901 return error;
1902}
1903
Al Viro82646132008-08-02 00:57:06 -04001904int vfs_quota_on(struct super_block *sb, int type, int format_id, char *name,
Jan Kara0ff5af82008-04-28 02:14:33 -07001905 int remount)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906{
Al Viro82646132008-08-02 00:57:06 -04001907 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908 int error;
1909
Jan Kara0ff5af82008-04-28 02:14:33 -07001910 if (remount)
1911 return vfs_quota_on_remount(sb, type);
1912
Al Viro82646132008-08-02 00:57:06 -04001913 error = kern_path(name, LOOKUP_FOLLOW, &path);
Al Viro77e69da2008-08-01 04:29:18 -04001914 if (!error) {
Al Viro82646132008-08-02 00:57:06 -04001915 error = vfs_quota_on_path(sb, type, format_id, &path);
1916 path_put(&path);
Al Viro77e69da2008-08-01 04:29:18 -04001917 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918 return error;
1919}
1920
1921/*
Jan Karaf55abc02008-08-20 17:50:32 +02001922 * More powerful function for turning on quotas allowing setting
1923 * of individual quota flags
1924 */
1925int vfs_quota_enable(struct inode *inode, int type, int format_id,
1926 unsigned int flags)
1927{
1928 int ret = 0;
1929 struct super_block *sb = inode->i_sb;
1930 struct quota_info *dqopt = sb_dqopt(sb);
1931
1932 /* Just unsuspend quotas? */
1933 if (flags & DQUOT_SUSPENDED)
1934 return vfs_quota_on_remount(sb, type);
1935 if (!flags)
1936 return 0;
1937 /* Just updating flags needed? */
1938 if (sb_has_quota_loaded(sb, type)) {
1939 mutex_lock(&dqopt->dqonoff_mutex);
1940 /* Now do a reliable test... */
1941 if (!sb_has_quota_loaded(sb, type)) {
1942 mutex_unlock(&dqopt->dqonoff_mutex);
1943 goto load_quota;
1944 }
1945 if (flags & DQUOT_USAGE_ENABLED &&
1946 sb_has_quota_usage_enabled(sb, type)) {
1947 ret = -EBUSY;
1948 goto out_lock;
1949 }
1950 if (flags & DQUOT_LIMITS_ENABLED &&
1951 sb_has_quota_limits_enabled(sb, type)) {
1952 ret = -EBUSY;
1953 goto out_lock;
1954 }
1955 sb_dqopt(sb)->flags |= dquot_state_flag(flags, type);
1956out_lock:
1957 mutex_unlock(&dqopt->dqonoff_mutex);
1958 return ret;
1959 }
1960
1961load_quota:
1962 return vfs_load_quota_inode(inode, type, format_id, flags);
1963}
1964
1965/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966 * This function is used when filesystem needs to initialize quotas
1967 * during mount time.
1968 */
Christoph Hellwig84de8562005-06-23 00:09:16 -07001969int vfs_quota_on_mount(struct super_block *sb, char *qf_name,
1970 int format_id, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971{
Christoph Hellwig84de8562005-06-23 00:09:16 -07001972 struct dentry *dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973 int error;
1974
Christoph Hellwig2fa389c2005-06-23 00:09:16 -07001975 dentry = lookup_one_len(qf_name, sb->s_root, strlen(qf_name));
Christoph Hellwig84de8562005-06-23 00:09:16 -07001976 if (IS_ERR(dentry))
1977 return PTR_ERR(dentry);
1978
Jan Kara154f4842005-11-28 13:44:14 -08001979 if (!dentry->d_inode) {
1980 error = -ENOENT;
1981 goto out;
1982 }
1983
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984 error = security_quota_on(dentry);
Christoph Hellwig84de8562005-06-23 00:09:16 -07001985 if (!error)
Jan Karaf55abc02008-08-20 17:50:32 +02001986 error = vfs_load_quota_inode(dentry->d_inode, type, format_id,
1987 DQUOT_USAGE_ENABLED | DQUOT_LIMITS_ENABLED);
Christoph Hellwig84de8562005-06-23 00:09:16 -07001988
Jan Kara154f4842005-11-28 13:44:14 -08001989out:
Christoph Hellwig84de8562005-06-23 00:09:16 -07001990 dput(dentry);
1991 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992}
1993
Jan Karab85f4b82008-07-25 01:46:50 -07001994/* Wrapper to turn on quotas when remounting rw */
1995int vfs_dq_quota_on_remount(struct super_block *sb)
1996{
1997 int cnt;
1998 int ret = 0, err;
1999
2000 if (!sb->s_qcop || !sb->s_qcop->quota_on)
2001 return -ENOSYS;
2002 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
2003 err = sb->s_qcop->quota_on(sb, cnt, 0, NULL, 1);
2004 if (err < 0 && !ret)
2005 ret = err;
2006 }
2007 return ret;
2008}
2009
Jan Kara12095462008-08-20 14:45:12 +02002010static inline qsize_t qbtos(qsize_t blocks)
2011{
2012 return blocks << QIF_DQBLKSIZE_BITS;
2013}
2014
2015static inline qsize_t stoqb(qsize_t space)
2016{
2017 return (space + QIF_DQBLKSIZE - 1) >> QIF_DQBLKSIZE_BITS;
2018}
2019
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020/* Generic routine for getting common part of quota structure */
2021static void do_get_dqblk(struct dquot *dquot, struct if_dqblk *di)
2022{
2023 struct mem_dqblk *dm = &dquot->dq_dqb;
2024
2025 spin_lock(&dq_data_lock);
Jan Kara12095462008-08-20 14:45:12 +02002026 di->dqb_bhardlimit = stoqb(dm->dqb_bhardlimit);
2027 di->dqb_bsoftlimit = stoqb(dm->dqb_bsoftlimit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028 di->dqb_curspace = dm->dqb_curspace;
2029 di->dqb_ihardlimit = dm->dqb_ihardlimit;
2030 di->dqb_isoftlimit = dm->dqb_isoftlimit;
2031 di->dqb_curinodes = dm->dqb_curinodes;
2032 di->dqb_btime = dm->dqb_btime;
2033 di->dqb_itime = dm->dqb_itime;
2034 di->dqb_valid = QIF_ALL;
2035 spin_unlock(&dq_data_lock);
2036}
2037
2038int vfs_get_dqblk(struct super_block *sb, int type, qid_t id, struct if_dqblk *di)
2039{
2040 struct dquot *dquot;
2041
Ingo Molnard3be9152006-03-23 03:00:29 -08002042 mutex_lock(&sb_dqopt(sb)->dqonoff_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043 if (!(dquot = dqget(sb, id, type))) {
Ingo Molnard3be9152006-03-23 03:00:29 -08002044 mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045 return -ESRCH;
2046 }
2047 do_get_dqblk(dquot, di);
2048 dqput(dquot);
Ingo Molnard3be9152006-03-23 03:00:29 -08002049 mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050 return 0;
2051}
2052
2053/* Generic routine for setting common part of quota structure */
Andrew Perepechko338bf9a2008-04-28 02:14:31 -07002054static int do_set_dqblk(struct dquot *dquot, struct if_dqblk *di)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055{
2056 struct mem_dqblk *dm = &dquot->dq_dqb;
2057 int check_blim = 0, check_ilim = 0;
Andrew Perepechko338bf9a2008-04-28 02:14:31 -07002058 struct mem_dqinfo *dqi = &sb_dqopt(dquot->dq_sb)->info[dquot->dq_type];
2059
2060 if ((di->dqb_valid & QIF_BLIMITS &&
2061 (di->dqb_bhardlimit > dqi->dqi_maxblimit ||
2062 di->dqb_bsoftlimit > dqi->dqi_maxblimit)) ||
2063 (di->dqb_valid & QIF_ILIMITS &&
2064 (di->dqb_ihardlimit > dqi->dqi_maxilimit ||
2065 di->dqb_isoftlimit > dqi->dqi_maxilimit)))
2066 return -ERANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067
2068 spin_lock(&dq_data_lock);
2069 if (di->dqb_valid & QIF_SPACE) {
2070 dm->dqb_curspace = di->dqb_curspace;
2071 check_blim = 1;
Jan Kara4d59bce2008-10-02 16:48:10 +02002072 __set_bit(DQ_LASTSET_B + QIF_SPACE_B, &dquot->dq_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073 }
2074 if (di->dqb_valid & QIF_BLIMITS) {
Jan Kara12095462008-08-20 14:45:12 +02002075 dm->dqb_bsoftlimit = qbtos(di->dqb_bsoftlimit);
2076 dm->dqb_bhardlimit = qbtos(di->dqb_bhardlimit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077 check_blim = 1;
Jan Kara4d59bce2008-10-02 16:48:10 +02002078 __set_bit(DQ_LASTSET_B + QIF_BLIMITS_B, &dquot->dq_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079 }
2080 if (di->dqb_valid & QIF_INODES) {
2081 dm->dqb_curinodes = di->dqb_curinodes;
2082 check_ilim = 1;
Jan Kara4d59bce2008-10-02 16:48:10 +02002083 __set_bit(DQ_LASTSET_B + QIF_INODES_B, &dquot->dq_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084 }
2085 if (di->dqb_valid & QIF_ILIMITS) {
2086 dm->dqb_isoftlimit = di->dqb_isoftlimit;
2087 dm->dqb_ihardlimit = di->dqb_ihardlimit;
2088 check_ilim = 1;
Jan Kara4d59bce2008-10-02 16:48:10 +02002089 __set_bit(DQ_LASTSET_B + QIF_ILIMITS_B, &dquot->dq_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090 }
Jan Kara4d59bce2008-10-02 16:48:10 +02002091 if (di->dqb_valid & QIF_BTIME) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092 dm->dqb_btime = di->dqb_btime;
Jan Karae04a88a92009-01-07 18:07:29 -08002093 check_blim = 1;
Jan Kara4d59bce2008-10-02 16:48:10 +02002094 __set_bit(DQ_LASTSET_B + QIF_BTIME_B, &dquot->dq_flags);
2095 }
2096 if (di->dqb_valid & QIF_ITIME) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097 dm->dqb_itime = di->dqb_itime;
Jan Karae04a88a92009-01-07 18:07:29 -08002098 check_ilim = 1;
Jan Kara4d59bce2008-10-02 16:48:10 +02002099 __set_bit(DQ_LASTSET_B + QIF_ITIME_B, &dquot->dq_flags);
2100 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101
2102 if (check_blim) {
Jan Kara12095462008-08-20 14:45:12 +02002103 if (!dm->dqb_bsoftlimit || dm->dqb_curspace < dm->dqb_bsoftlimit) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104 dm->dqb_btime = 0;
2105 clear_bit(DQ_BLKS_B, &dquot->dq_flags);
2106 }
2107 else if (!(di->dqb_valid & QIF_BTIME)) /* Set grace only if user hasn't provided his own... */
Andrew Perepechko338bf9a2008-04-28 02:14:31 -07002108 dm->dqb_btime = get_seconds() + dqi->dqi_bgrace;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109 }
2110 if (check_ilim) {
2111 if (!dm->dqb_isoftlimit || dm->dqb_curinodes < dm->dqb_isoftlimit) {
2112 dm->dqb_itime = 0;
2113 clear_bit(DQ_INODES_B, &dquot->dq_flags);
2114 }
2115 else if (!(di->dqb_valid & QIF_ITIME)) /* Set grace only if user hasn't provided his own... */
Andrew Perepechko338bf9a2008-04-28 02:14:31 -07002116 dm->dqb_itime = get_seconds() + dqi->dqi_igrace;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117 }
2118 if (dm->dqb_bhardlimit || dm->dqb_bsoftlimit || dm->dqb_ihardlimit || dm->dqb_isoftlimit)
2119 clear_bit(DQ_FAKE_B, &dquot->dq_flags);
2120 else
2121 set_bit(DQ_FAKE_B, &dquot->dq_flags);
2122 spin_unlock(&dq_data_lock);
2123 mark_dquot_dirty(dquot);
Andrew Perepechko338bf9a2008-04-28 02:14:31 -07002124
2125 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126}
2127
2128int vfs_set_dqblk(struct super_block *sb, int type, qid_t id, struct if_dqblk *di)
2129{
2130 struct dquot *dquot;
Andrew Perepechko338bf9a2008-04-28 02:14:31 -07002131 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132
Ingo Molnard3be9152006-03-23 03:00:29 -08002133 mutex_lock(&sb_dqopt(sb)->dqonoff_mutex);
Jan Karaf55abc02008-08-20 17:50:32 +02002134 dquot = dqget(sb, id, type);
2135 if (!dquot) {
2136 rc = -ESRCH;
2137 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138 }
Andrew Perepechko338bf9a2008-04-28 02:14:31 -07002139 rc = do_set_dqblk(dquot, di);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140 dqput(dquot);
Jan Karaf55abc02008-08-20 17:50:32 +02002141out:
Ingo Molnard3be9152006-03-23 03:00:29 -08002142 mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
Andrew Perepechko338bf9a2008-04-28 02:14:31 -07002143 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144}
2145
2146/* Generic routine for getting common part of quota file information */
2147int vfs_get_dqinfo(struct super_block *sb, int type, struct if_dqinfo *ii)
2148{
2149 struct mem_dqinfo *mi;
2150
Ingo Molnard3be9152006-03-23 03:00:29 -08002151 mutex_lock(&sb_dqopt(sb)->dqonoff_mutex);
Jan Karaf55abc02008-08-20 17:50:32 +02002152 if (!sb_has_quota_active(sb, type)) {
Ingo Molnard3be9152006-03-23 03:00:29 -08002153 mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154 return -ESRCH;
2155 }
2156 mi = sb_dqopt(sb)->info + type;
2157 spin_lock(&dq_data_lock);
2158 ii->dqi_bgrace = mi->dqi_bgrace;
2159 ii->dqi_igrace = mi->dqi_igrace;
2160 ii->dqi_flags = mi->dqi_flags & DQF_MASK;
2161 ii->dqi_valid = IIF_ALL;
2162 spin_unlock(&dq_data_lock);
Ingo Molnard3be9152006-03-23 03:00:29 -08002163 mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164 return 0;
2165}
2166
2167/* Generic routine for setting common part of quota file information */
2168int vfs_set_dqinfo(struct super_block *sb, int type, struct if_dqinfo *ii)
2169{
2170 struct mem_dqinfo *mi;
Jan Karaf55abc02008-08-20 17:50:32 +02002171 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172
Ingo Molnard3be9152006-03-23 03:00:29 -08002173 mutex_lock(&sb_dqopt(sb)->dqonoff_mutex);
Jan Karaf55abc02008-08-20 17:50:32 +02002174 if (!sb_has_quota_active(sb, type)) {
2175 err = -ESRCH;
2176 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177 }
2178 mi = sb_dqopt(sb)->info + type;
2179 spin_lock(&dq_data_lock);
2180 if (ii->dqi_valid & IIF_BGRACE)
2181 mi->dqi_bgrace = ii->dqi_bgrace;
2182 if (ii->dqi_valid & IIF_IGRACE)
2183 mi->dqi_igrace = ii->dqi_igrace;
2184 if (ii->dqi_valid & IIF_FLAGS)
2185 mi->dqi_flags = (mi->dqi_flags & ~DQF_MASK) | (ii->dqi_flags & DQF_MASK);
2186 spin_unlock(&dq_data_lock);
2187 mark_info_dirty(sb, type);
2188 /* Force write to disk */
2189 sb->dq_op->write_info(sb, type);
Jan Karaf55abc02008-08-20 17:50:32 +02002190out:
Ingo Molnard3be9152006-03-23 03:00:29 -08002191 mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
Jan Karaf55abc02008-08-20 17:50:32 +02002192 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193}
2194
2195struct quotactl_ops vfs_quotactl_ops = {
2196 .quota_on = vfs_quota_on,
2197 .quota_off = vfs_quota_off,
2198 .quota_sync = vfs_quota_sync,
2199 .get_info = vfs_get_dqinfo,
2200 .set_info = vfs_set_dqinfo,
2201 .get_dqblk = vfs_get_dqblk,
2202 .set_dqblk = vfs_set_dqblk
2203};
2204
2205static ctl_table fs_dqstats_table[] = {
2206 {
2207 .ctl_name = FS_DQ_LOOKUPS,
2208 .procname = "lookups",
2209 .data = &dqstats.lookups,
2210 .maxlen = sizeof(int),
2211 .mode = 0444,
2212 .proc_handler = &proc_dointvec,
2213 },
2214 {
2215 .ctl_name = FS_DQ_DROPS,
2216 .procname = "drops",
2217 .data = &dqstats.drops,
2218 .maxlen = sizeof(int),
2219 .mode = 0444,
2220 .proc_handler = &proc_dointvec,
2221 },
2222 {
2223 .ctl_name = FS_DQ_READS,
2224 .procname = "reads",
2225 .data = &dqstats.reads,
2226 .maxlen = sizeof(int),
2227 .mode = 0444,
2228 .proc_handler = &proc_dointvec,
2229 },
2230 {
2231 .ctl_name = FS_DQ_WRITES,
2232 .procname = "writes",
2233 .data = &dqstats.writes,
2234 .maxlen = sizeof(int),
2235 .mode = 0444,
2236 .proc_handler = &proc_dointvec,
2237 },
2238 {
2239 .ctl_name = FS_DQ_CACHE_HITS,
2240 .procname = "cache_hits",
2241 .data = &dqstats.cache_hits,
2242 .maxlen = sizeof(int),
2243 .mode = 0444,
2244 .proc_handler = &proc_dointvec,
2245 },
2246 {
2247 .ctl_name = FS_DQ_ALLOCATED,
2248 .procname = "allocated_dquots",
2249 .data = &dqstats.allocated_dquots,
2250 .maxlen = sizeof(int),
2251 .mode = 0444,
2252 .proc_handler = &proc_dointvec,
2253 },
2254 {
2255 .ctl_name = FS_DQ_FREE,
2256 .procname = "free_dquots",
2257 .data = &dqstats.free_dquots,
2258 .maxlen = sizeof(int),
2259 .mode = 0444,
2260 .proc_handler = &proc_dointvec,
2261 },
2262 {
2263 .ctl_name = FS_DQ_SYNCS,
2264 .procname = "syncs",
2265 .data = &dqstats.syncs,
2266 .maxlen = sizeof(int),
2267 .mode = 0444,
2268 .proc_handler = &proc_dointvec,
2269 },
Jan Kara8e893462007-10-16 23:29:31 -07002270#ifdef CONFIG_PRINT_QUOTA_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271 {
2272 .ctl_name = FS_DQ_WARNINGS,
2273 .procname = "warnings",
2274 .data = &flag_print_warnings,
2275 .maxlen = sizeof(int),
2276 .mode = 0644,
2277 .proc_handler = &proc_dointvec,
2278 },
Jan Kara8e893462007-10-16 23:29:31 -07002279#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280 { .ctl_name = 0 },
2281};
2282
2283static ctl_table fs_table[] = {
2284 {
2285 .ctl_name = FS_DQSTATS,
2286 .procname = "quota",
2287 .mode = 0555,
2288 .child = fs_dqstats_table,
2289 },
2290 { .ctl_name = 0 },
2291};
2292
2293static ctl_table sys_table[] = {
2294 {
2295 .ctl_name = CTL_FS,
2296 .procname = "fs",
2297 .mode = 0555,
2298 .child = fs_table,
2299 },
2300 { .ctl_name = 0 },
2301};
2302
2303static int __init dquot_init(void)
2304{
2305 int i;
2306 unsigned long nr_hash, order;
2307
2308 printk(KERN_NOTICE "VFS: Disk quotas %s\n", __DQUOT_VERSION__);
2309
Eric W. Biederman0b4d4142007-02-14 00:34:09 -08002310 register_sysctl_table(sys_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311
Paul Mundt20c2df82007-07-20 10:11:58 +09002312 dquot_cachep = kmem_cache_create("dquot",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313 sizeof(struct dquot), sizeof(unsigned long) * 4,
Paul Jacksonfffb60f2006-03-24 03:16:06 -08002314 (SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT|
2315 SLAB_MEM_SPREAD|SLAB_PANIC),
Paul Mundt20c2df82007-07-20 10:11:58 +09002316 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317
2318 order = 0;
2319 dquot_hash = (struct hlist_head *)__get_free_pages(GFP_ATOMIC, order);
2320 if (!dquot_hash)
2321 panic("Cannot create dquot hash table");
2322
2323 /* Find power-of-two hlist_heads which can fit into allocation */
2324 nr_hash = (1UL << order) * PAGE_SIZE / sizeof(struct hlist_head);
2325 dq_hash_bits = 0;
2326 do {
2327 dq_hash_bits++;
2328 } while (nr_hash >> dq_hash_bits);
2329 dq_hash_bits--;
2330
2331 nr_hash = 1UL << dq_hash_bits;
2332 dq_hash_mask = nr_hash - 1;
2333 for (i = 0; i < nr_hash; i++)
2334 INIT_HLIST_HEAD(dquot_hash + i);
2335
2336 printk("Dquot-cache hash table entries: %ld (order %ld, %ld bytes)\n",
2337 nr_hash, order, (PAGE_SIZE << order));
2338
Rusty Russell8e1f9362007-07-17 04:03:17 -07002339 register_shrinker(&dqcache_shrinker);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340
Jan Kara8e893462007-10-16 23:29:31 -07002341#ifdef CONFIG_QUOTA_NETLINK_INTERFACE
2342 if (genl_register_family(&quota_genl_family) != 0)
2343 printk(KERN_ERR "VFS: Failed to create quota netlink interface.\n");
2344#endif
2345
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346 return 0;
2347}
2348module_init(dquot_init);
2349
2350EXPORT_SYMBOL(register_quota_format);
2351EXPORT_SYMBOL(unregister_quota_format);
2352EXPORT_SYMBOL(dqstats);
2353EXPORT_SYMBOL(dq_data_lock);
Jan Karaf55abc02008-08-20 17:50:32 +02002354EXPORT_SYMBOL(vfs_quota_enable);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002355EXPORT_SYMBOL(vfs_quota_on);
Al Viro77e69da2008-08-01 04:29:18 -04002356EXPORT_SYMBOL(vfs_quota_on_path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357EXPORT_SYMBOL(vfs_quota_on_mount);
Jan Karaf55abc02008-08-20 17:50:32 +02002358EXPORT_SYMBOL(vfs_quota_disable);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359EXPORT_SYMBOL(vfs_quota_off);
Jan Kara12c77522008-10-20 17:05:00 +02002360EXPORT_SYMBOL(dquot_scan_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361EXPORT_SYMBOL(vfs_quota_sync);
2362EXPORT_SYMBOL(vfs_get_dqinfo);
2363EXPORT_SYMBOL(vfs_set_dqinfo);
2364EXPORT_SYMBOL(vfs_get_dqblk);
2365EXPORT_SYMBOL(vfs_set_dqblk);
2366EXPORT_SYMBOL(dquot_commit);
2367EXPORT_SYMBOL(dquot_commit_info);
2368EXPORT_SYMBOL(dquot_acquire);
2369EXPORT_SYMBOL(dquot_release);
2370EXPORT_SYMBOL(dquot_mark_dquot_dirty);
2371EXPORT_SYMBOL(dquot_initialize);
2372EXPORT_SYMBOL(dquot_drop);
Jan Kara3d9ea252008-10-10 16:12:23 +02002373EXPORT_SYMBOL(dquot_drop_locked);
Jan Karab85f4b82008-07-25 01:46:50 -07002374EXPORT_SYMBOL(vfs_dq_drop);
Jan Kara3d9ea252008-10-10 16:12:23 +02002375EXPORT_SYMBOL(dqget);
2376EXPORT_SYMBOL(dqput);
2377EXPORT_SYMBOL(dquot_is_cached);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002378EXPORT_SYMBOL(dquot_alloc_space);
2379EXPORT_SYMBOL(dquot_alloc_inode);
2380EXPORT_SYMBOL(dquot_free_space);
2381EXPORT_SYMBOL(dquot_free_inode);
2382EXPORT_SYMBOL(dquot_transfer);
Jan Karab85f4b82008-07-25 01:46:50 -07002383EXPORT_SYMBOL(vfs_dq_transfer);
2384EXPORT_SYMBOL(vfs_dq_quota_on_remount);