blob: ae8fd9e645cc5f5c6edabab49fc7111f4ecebfde [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 Kara74f783a2008-08-19 14:51:22 +0200416static void dquot_destroy(struct dquot *dquot)
417{
418 kmem_cache_free(dquot_cachep, dquot);
419}
420
421static inline void do_destroy_dquot(struct dquot *dquot)
422{
423 dquot->dq_sb->dq_op->destroy_dquot(dquot);
424}
425
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426/* Invalidate all dquots on the list. Note that this function is called after
427 * quota is disabled and pointers from inodes removed so there cannot be new
Jan Kara6362e4d2006-03-23 03:00:17 -0800428 * quota users. There can still be some users of quotas due to inodes being
429 * just deleted or pruned by prune_icache() (those are not attached to any
430 * list). We have to wait for such users.
431 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432static void invalidate_dquots(struct super_block *sb, int type)
433{
Domen Puncerc33ed272005-06-25 14:59:36 -0700434 struct dquot *dquot, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435
Jan Kara6362e4d2006-03-23 03:00:17 -0800436restart:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 spin_lock(&dq_list_lock);
Domen Puncerc33ed272005-06-25 14:59:36 -0700438 list_for_each_entry_safe(dquot, tmp, &inuse_list, dq_inuse) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 if (dquot->dq_sb != sb)
440 continue;
441 if (dquot->dq_type != type)
442 continue;
Jan Kara6362e4d2006-03-23 03:00:17 -0800443 /* Wait for dquot users */
444 if (atomic_read(&dquot->dq_count)) {
445 DEFINE_WAIT(wait);
446
447 atomic_inc(&dquot->dq_count);
448 prepare_to_wait(&dquot->dq_wait_unused, &wait,
449 TASK_UNINTERRUPTIBLE);
450 spin_unlock(&dq_list_lock);
451 /* Once dqput() wakes us up, we know it's time to free
452 * the dquot.
453 * IMPORTANT: we rely on the fact that there is always
454 * at most one process waiting for dquot to free.
455 * Otherwise dq_count would be > 1 and we would never
456 * wake up.
457 */
458 if (atomic_read(&dquot->dq_count) > 1)
459 schedule();
460 finish_wait(&dquot->dq_wait_unused, &wait);
461 dqput(dquot);
462 /* At this moment dquot() need not exist (it could be
463 * reclaimed by prune_dqcache(). Hence we must
464 * restart. */
465 goto restart;
466 }
467 /*
468 * Quota now has no users and it has been written on last
469 * dqput()
470 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 remove_dquot_hash(dquot);
472 remove_free_dquot(dquot);
473 remove_inuse(dquot);
Jan Kara74f783a2008-08-19 14:51:22 +0200474 do_destroy_dquot(dquot);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 }
476 spin_unlock(&dq_list_lock);
477}
478
479int vfs_quota_sync(struct super_block *sb, int type)
480{
481 struct list_head *dirty;
482 struct dquot *dquot;
483 struct quota_info *dqopt = sb_dqopt(sb);
484 int cnt;
485
Ingo Molnard3be9152006-03-23 03:00:29 -0800486 mutex_lock(&dqopt->dqonoff_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
488 if (type != -1 && cnt != type)
489 continue;
Jan Karaf55abc02008-08-20 17:50:32 +0200490 if (!sb_has_quota_active(sb, cnt))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 continue;
492 spin_lock(&dq_list_lock);
493 dirty = &dqopt->info[cnt].dqi_dirty_list;
494 while (!list_empty(dirty)) {
Pavel Emelianovb5e61812007-05-08 00:30:19 -0700495 dquot = list_first_entry(dirty, struct dquot, dq_dirty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 /* Dirty and inactive can be only bad dquot... */
497 if (!test_bit(DQ_ACTIVE_B, &dquot->dq_flags)) {
498 clear_dquot_dirty(dquot);
499 continue;
500 }
501 /* Now we have active dquot from which someone is
502 * holding reference so we can safely just increase
503 * use count */
504 atomic_inc(&dquot->dq_count);
505 dqstats.lookups++;
506 spin_unlock(&dq_list_lock);
507 sb->dq_op->write_dquot(dquot);
508 dqput(dquot);
509 spin_lock(&dq_list_lock);
510 }
511 spin_unlock(&dq_list_lock);
512 }
513
514 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
Jan Karaf55abc02008-08-20 17:50:32 +0200515 if ((cnt == type || type == -1) && sb_has_quota_active(sb, cnt)
516 && info_dirty(&dqopt->info[cnt]))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 sb->dq_op->write_info(sb, cnt);
518 spin_lock(&dq_list_lock);
519 dqstats.syncs++;
520 spin_unlock(&dq_list_lock);
Ingo Molnard3be9152006-03-23 03:00:29 -0800521 mutex_unlock(&dqopt->dqonoff_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522
523 return 0;
524}
525
526/* Free unused dquots from cache */
527static void prune_dqcache(int count)
528{
529 struct list_head *head;
530 struct dquot *dquot;
531
532 head = free_dquots.prev;
533 while (head != &free_dquots && count) {
534 dquot = list_entry(head, struct dquot, dq_free);
535 remove_dquot_hash(dquot);
536 remove_free_dquot(dquot);
537 remove_inuse(dquot);
Jan Kara74f783a2008-08-19 14:51:22 +0200538 do_destroy_dquot(dquot);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 count--;
540 head = free_dquots.prev;
541 }
542}
543
544/*
545 * This is called from kswapd when we think we need some
546 * more memory
547 */
548
Al Viro27496a82005-10-21 03:20:48 -0400549static int shrink_dqcache_memory(int nr, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550{
551 if (nr) {
552 spin_lock(&dq_list_lock);
553 prune_dqcache(nr);
554 spin_unlock(&dq_list_lock);
555 }
556 return (dqstats.free_dquots / 100) * sysctl_vfs_cache_pressure;
557}
558
Rusty Russell8e1f9362007-07-17 04:03:17 -0700559static struct shrinker dqcache_shrinker = {
560 .shrink = shrink_dqcache_memory,
561 .seeks = DEFAULT_SEEKS,
562};
563
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564/*
565 * Put reference to dquot
566 * NOTE: If you change this function please check whether dqput_blocks() works right...
Ingo Molnard3be9152006-03-23 03:00:29 -0800567 * MUST be called with either dqptr_sem or dqonoff_mutex held
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 */
Jan Kara3d9ea252008-10-10 16:12:23 +0200569void dqput(struct dquot *dquot)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570{
Jan Karab48d3802008-07-25 01:46:49 -0700571 int ret;
572
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 if (!dquot)
574 return;
575#ifdef __DQUOT_PARANOIA
576 if (!atomic_read(&dquot->dq_count)) {
577 printk("VFS: dqput: trying to free free dquot\n");
578 printk("VFS: device %s, dquot of %s %d\n",
579 dquot->dq_sb->s_id,
580 quotatypes[dquot->dq_type],
581 dquot->dq_id);
582 BUG();
583 }
584#endif
585
586 spin_lock(&dq_list_lock);
587 dqstats.drops++;
588 spin_unlock(&dq_list_lock);
589we_slept:
590 spin_lock(&dq_list_lock);
591 if (atomic_read(&dquot->dq_count) > 1) {
592 /* We have more than one user... nothing to do */
593 atomic_dec(&dquot->dq_count);
Jan Kara6362e4d2006-03-23 03:00:17 -0800594 /* Releasing dquot during quotaoff phase? */
Jan Karaf55abc02008-08-20 17:50:32 +0200595 if (!sb_has_quota_active(dquot->dq_sb, dquot->dq_type) &&
Jan Kara6362e4d2006-03-23 03:00:17 -0800596 atomic_read(&dquot->dq_count) == 1)
597 wake_up(&dquot->dq_wait_unused);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 spin_unlock(&dq_list_lock);
599 return;
600 }
601 /* Need to release dquot? */
602 if (test_bit(DQ_ACTIVE_B, &dquot->dq_flags) && dquot_dirty(dquot)) {
603 spin_unlock(&dq_list_lock);
604 /* Commit dquot before releasing */
Jan Karab48d3802008-07-25 01:46:49 -0700605 ret = dquot->dq_sb->dq_op->write_dquot(dquot);
606 if (ret < 0) {
607 printk(KERN_ERR "VFS: cannot write quota structure on "
608 "device %s (error %d). Quota may get out of "
609 "sync!\n", dquot->dq_sb->s_id, ret);
610 /*
611 * We clear dirty bit anyway, so that we avoid
612 * infinite loop here
613 */
614 spin_lock(&dq_list_lock);
615 clear_dquot_dirty(dquot);
616 spin_unlock(&dq_list_lock);
617 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 goto we_slept;
619 }
620 /* Clear flag in case dquot was inactive (something bad happened) */
621 clear_dquot_dirty(dquot);
622 if (test_bit(DQ_ACTIVE_B, &dquot->dq_flags)) {
623 spin_unlock(&dq_list_lock);
624 dquot->dq_sb->dq_op->release_dquot(dquot);
625 goto we_slept;
626 }
627 atomic_dec(&dquot->dq_count);
628#ifdef __DQUOT_PARANOIA
629 /* sanity check */
Eric Sesterhenn8abf6a42006-04-02 13:36:13 +0200630 BUG_ON(!list_empty(&dquot->dq_free));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631#endif
632 put_dquot_last(dquot);
633 spin_unlock(&dq_list_lock);
634}
635
Jan Kara74f783a2008-08-19 14:51:22 +0200636static struct dquot *dquot_alloc(struct super_block *sb, int type)
637{
638 return kmem_cache_zalloc(dquot_cachep, GFP_NOFS);
639}
640
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641static struct dquot *get_empty_dquot(struct super_block *sb, int type)
642{
643 struct dquot *dquot;
644
Jan Kara74f783a2008-08-19 14:51:22 +0200645 dquot = sb->dq_op->alloc_dquot(sb, type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 if(!dquot)
647 return NODQUOT;
648
Ingo Molnard3be9152006-03-23 03:00:29 -0800649 mutex_init(&dquot->dq_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 INIT_LIST_HEAD(&dquot->dq_free);
651 INIT_LIST_HEAD(&dquot->dq_inuse);
652 INIT_HLIST_NODE(&dquot->dq_hash);
653 INIT_LIST_HEAD(&dquot->dq_dirty);
Jan Kara6362e4d2006-03-23 03:00:17 -0800654 init_waitqueue_head(&dquot->dq_wait_unused);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 dquot->dq_sb = sb;
656 dquot->dq_type = type;
657 atomic_set(&dquot->dq_count, 1);
658
659 return dquot;
660}
661
662/*
Jan Kara3d9ea252008-10-10 16:12:23 +0200663 * Check whether dquot is in memory.
664 * MUST be called with either dqptr_sem or dqonoff_mutex held
665 */
666int dquot_is_cached(struct super_block *sb, unsigned int id, int type)
667{
668 unsigned int hashent = hashfn(sb, id, type);
669 int ret = 0;
670
671 if (!sb_has_quota_active(sb, type))
672 return 0;
673 spin_lock(&dq_list_lock);
674 if (find_dquot(hashent, sb, id, type) != NODQUOT)
675 ret = 1;
676 spin_unlock(&dq_list_lock);
677 return ret;
678}
679
680/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 * Get reference to dquot
Ingo Molnard3be9152006-03-23 03:00:29 -0800682 * MUST be called with either dqptr_sem or dqonoff_mutex held
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 */
Jan Kara3d9ea252008-10-10 16:12:23 +0200684struct dquot *dqget(struct super_block *sb, unsigned int id, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685{
686 unsigned int hashent = hashfn(sb, id, type);
687 struct dquot *dquot, *empty = NODQUOT;
688
Jan Karaf55abc02008-08-20 17:50:32 +0200689 if (!sb_has_quota_active(sb, type))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 return NODQUOT;
691we_slept:
692 spin_lock(&dq_list_lock);
693 if ((dquot = find_dquot(hashent, sb, id, type)) == NODQUOT) {
694 if (empty == NODQUOT) {
695 spin_unlock(&dq_list_lock);
696 if ((empty = get_empty_dquot(sb, type)) == NODQUOT)
697 schedule(); /* Try to wait for a moment... */
698 goto we_slept;
699 }
700 dquot = empty;
701 dquot->dq_id = id;
702 /* all dquots go on the inuse_list */
703 put_inuse(dquot);
704 /* hash it first so it can be found */
705 insert_dquot_hash(dquot);
706 dqstats.lookups++;
707 spin_unlock(&dq_list_lock);
708 } else {
709 if (!atomic_read(&dquot->dq_count))
710 remove_free_dquot(dquot);
711 atomic_inc(&dquot->dq_count);
712 dqstats.cache_hits++;
713 dqstats.lookups++;
714 spin_unlock(&dq_list_lock);
715 if (empty)
Jan Kara74f783a2008-08-19 14:51:22 +0200716 do_destroy_dquot(empty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 }
718 /* Wait for dq_lock - after this we know that either dquot_release() is already
719 * finished or it will be canceled due to dq_count > 1 test */
720 wait_on_dquot(dquot);
721 /* Read the dquot and instantiate it (everything done only if needed) */
722 if (!test_bit(DQ_ACTIVE_B, &dquot->dq_flags) && sb->dq_op->acquire_dquot(dquot) < 0) {
723 dqput(dquot);
724 return NODQUOT;
725 }
726#ifdef __DQUOT_PARANOIA
Eric Sesterhenn8abf6a42006-04-02 13:36:13 +0200727 BUG_ON(!dquot->dq_sb); /* Has somebody invalidated entry under us? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728#endif
729
730 return dquot;
731}
732
733static int dqinit_needed(struct inode *inode, int type)
734{
735 int cnt;
736
737 if (IS_NOQUOTA(inode))
738 return 0;
739 if (type != -1)
740 return inode->i_dquot[type] == NODQUOT;
741 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
742 if (inode->i_dquot[cnt] == NODQUOT)
743 return 1;
744 return 0;
745}
746
Ingo Molnard3be9152006-03-23 03:00:29 -0800747/* This routine is guarded by dqonoff_mutex mutex */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748static void add_dquot_ref(struct super_block *sb, int type)
749{
Jan Kara941d2382008-02-06 01:37:36 -0800750 struct inode *inode, *old_inode = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751
Christoph Hellwigd003fb72007-02-12 00:51:58 -0800752 spin_lock(&inode_lock);
753 list_for_each_entry(inode, &sb->s_inodes, i_sb_list) {
754 if (!atomic_read(&inode->i_writecount))
755 continue;
756 if (!dqinit_needed(inode, type))
757 continue;
758 if (inode->i_state & (I_FREEING|I_WILL_FREE))
759 continue;
760
761 __iget(inode);
762 spin_unlock(&inode_lock);
763
Jan Kara941d2382008-02-06 01:37:36 -0800764 iput(old_inode);
Christoph Hellwigd003fb72007-02-12 00:51:58 -0800765 sb->dq_op->initialize(inode, type);
Jan Kara941d2382008-02-06 01:37:36 -0800766 /* We hold a reference to 'inode' so it couldn't have been
767 * removed from s_inodes list while we dropped the inode_lock.
768 * We cannot iput the inode now as we can be holding the last
769 * reference and we cannot iput it under inode_lock. So we
770 * keep the reference and iput it later. */
771 old_inode = inode;
772 spin_lock(&inode_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 }
Christoph Hellwigd003fb72007-02-12 00:51:58 -0800774 spin_unlock(&inode_lock);
Jan Kara941d2382008-02-06 01:37:36 -0800775 iput(old_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776}
777
778/* Return 0 if dqput() won't block (note that 1 doesn't necessarily mean blocking) */
779static inline int dqput_blocks(struct dquot *dquot)
780{
781 if (atomic_read(&dquot->dq_count) <= 1)
782 return 1;
783 return 0;
784}
785
786/* Remove references to dquots from inode - add dquot to list for freeing if needed */
787/* We can't race with anybody because we hold dqptr_sem for writing... */
Adrian Bunke5f00f42007-05-08 00:27:22 -0700788static int remove_inode_dquot_ref(struct inode *inode, int type,
789 struct list_head *tofree_head)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790{
791 struct dquot *dquot = inode->i_dquot[type];
792
793 inode->i_dquot[type] = NODQUOT;
794 if (dquot != NODQUOT) {
795 if (dqput_blocks(dquot)) {
796#ifdef __DQUOT_PARANOIA
797 if (atomic_read(&dquot->dq_count) != 1)
798 printk(KERN_WARNING "VFS: Adding dquot with dq_count %d to dispose list.\n", atomic_read(&dquot->dq_count));
799#endif
800 spin_lock(&dq_list_lock);
801 list_add(&dquot->dq_free, tofree_head); /* As dquot must have currently users it can't be on the free list... */
802 spin_unlock(&dq_list_lock);
803 return 1;
804 }
805 else
806 dqput(dquot); /* We have guaranteed we won't block */
807 }
808 return 0;
809}
810
811/* Free list of dquots - called from inode.c */
812/* dquots are removed from inodes, no new references can be got so we are the only ones holding reference */
813static void put_dquot_list(struct list_head *tofree_head)
814{
815 struct list_head *act_head;
816 struct dquot *dquot;
817
818 act_head = tofree_head->next;
819 /* So now we have dquots on the list... Just free them */
820 while (act_head != tofree_head) {
821 dquot = list_entry(act_head, struct dquot, dq_free);
822 act_head = act_head->next;
823 list_del_init(&dquot->dq_free); /* Remove dquot from the list so we won't have problems... */
824 dqput(dquot);
825 }
826}
827
Christoph Hellwigfb58b732007-02-12 00:51:57 -0800828static void remove_dquot_ref(struct super_block *sb, int type,
829 struct list_head *tofree_head)
830{
831 struct inode *inode;
832
833 spin_lock(&inode_lock);
834 list_for_each_entry(inode, &sb->s_inodes, i_sb_list) {
835 if (!IS_NOQUOTA(inode))
836 remove_inode_dquot_ref(inode, type, tofree_head);
837 }
838 spin_unlock(&inode_lock);
839}
840
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841/* Gather all references from inodes and drop them */
842static void drop_dquot_ref(struct super_block *sb, int type)
843{
844 LIST_HEAD(tofree_head);
845
Christoph Hellwigfb58b732007-02-12 00:51:57 -0800846 if (sb->dq_op) {
847 down_write(&sb_dqopt(sb)->dqptr_sem);
848 remove_dquot_ref(sb, type, &tofree_head);
849 up_write(&sb_dqopt(sb)->dqptr_sem);
850 put_dquot_list(&tofree_head);
851 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852}
853
Jan Kara12095462008-08-20 14:45:12 +0200854static inline void dquot_incr_inodes(struct dquot *dquot, qsize_t number)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855{
856 dquot->dq_dqb.dqb_curinodes += number;
857}
858
859static inline void dquot_incr_space(struct dquot *dquot, qsize_t number)
860{
861 dquot->dq_dqb.dqb_curspace += number;
862}
863
Jan Kara12095462008-08-20 14:45:12 +0200864static inline void dquot_decr_inodes(struct dquot *dquot, qsize_t number)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865{
Jan Karadb49d2d2008-10-01 18:21:39 +0200866 if (sb_dqopt(dquot->dq_sb)->flags & DQUOT_NEGATIVE_USAGE ||
867 dquot->dq_dqb.dqb_curinodes >= number)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 dquot->dq_dqb.dqb_curinodes -= number;
869 else
870 dquot->dq_dqb.dqb_curinodes = 0;
871 if (dquot->dq_dqb.dqb_curinodes <= dquot->dq_dqb.dqb_isoftlimit)
872 dquot->dq_dqb.dqb_itime = (time_t) 0;
873 clear_bit(DQ_INODES_B, &dquot->dq_flags);
874}
875
876static inline void dquot_decr_space(struct dquot *dquot, qsize_t number)
877{
Jan Karadb49d2d2008-10-01 18:21:39 +0200878 if (sb_dqopt(dquot->dq_sb)->flags & DQUOT_NEGATIVE_USAGE ||
879 dquot->dq_dqb.dqb_curspace >= number)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 dquot->dq_dqb.dqb_curspace -= number;
881 else
882 dquot->dq_dqb.dqb_curspace = 0;
Jan Kara12095462008-08-20 14:45:12 +0200883 if (dquot->dq_dqb.dqb_curspace <= dquot->dq_dqb.dqb_bsoftlimit)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 dquot->dq_dqb.dqb_btime = (time_t) 0;
885 clear_bit(DQ_BLKS_B, &dquot->dq_flags);
886}
887
Jan Karac5254602007-12-22 14:03:25 -0800888static int warning_issued(struct dquot *dquot, const int warntype)
889{
890 int flag = (warntype == QUOTA_NL_BHARDWARN ||
891 warntype == QUOTA_NL_BSOFTLONGWARN) ? DQ_BLKS_B :
892 ((warntype == QUOTA_NL_IHARDWARN ||
893 warntype == QUOTA_NL_ISOFTLONGWARN) ? DQ_INODES_B : 0);
894
895 if (!flag)
896 return 0;
897 return test_and_set_bit(flag, &dquot->dq_flags);
898}
899
Jan Kara8e893462007-10-16 23:29:31 -0700900#ifdef CONFIG_PRINT_QUOTA_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901static int flag_print_warnings = 1;
902
903static inline int need_print_warning(struct dquot *dquot)
904{
905 if (!flag_print_warnings)
906 return 0;
907
908 switch (dquot->dq_type) {
909 case USRQUOTA:
David Howellsda9592e2008-11-14 10:39:05 +1100910 return current_fsuid() == dquot->dq_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 case GRPQUOTA:
912 return in_group_p(dquot->dq_id);
913 }
914 return 0;
915}
916
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917/* Print warning to user which exceeded quota */
Jan Karac5254602007-12-22 14:03:25 -0800918static void print_warning(struct dquot *dquot, const int warntype)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919{
920 char *msg = NULL;
Peter Zijlstra24ec8392006-12-08 02:36:04 -0800921 struct tty_struct *tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922
Jan Kara657d3bf2008-07-25 01:46:52 -0700923 if (warntype == QUOTA_NL_IHARDBELOW ||
924 warntype == QUOTA_NL_ISOFTBELOW ||
925 warntype == QUOTA_NL_BHARDBELOW ||
926 warntype == QUOTA_NL_BSOFTBELOW || !need_print_warning(dquot))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 return;
928
Peter Zijlstra24ec8392006-12-08 02:36:04 -0800929 tty = get_current_tty();
930 if (!tty)
Alan Cox452a00d2008-10-13 10:39:13 +0100931 return;
Peter Zijlstra24ec8392006-12-08 02:36:04 -0800932 tty_write_message(tty, dquot->dq_sb->s_id);
Jan Kara8e893462007-10-16 23:29:31 -0700933 if (warntype == QUOTA_NL_ISOFTWARN || warntype == QUOTA_NL_BSOFTWARN)
Peter Zijlstra24ec8392006-12-08 02:36:04 -0800934 tty_write_message(tty, ": warning, ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 else
Peter Zijlstra24ec8392006-12-08 02:36:04 -0800936 tty_write_message(tty, ": write failed, ");
937 tty_write_message(tty, quotatypes[dquot->dq_type]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 switch (warntype) {
Jan Kara8e893462007-10-16 23:29:31 -0700939 case QUOTA_NL_IHARDWARN:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 msg = " file limit reached.\r\n";
941 break;
Jan Kara8e893462007-10-16 23:29:31 -0700942 case QUOTA_NL_ISOFTLONGWARN:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 msg = " file quota exceeded too long.\r\n";
944 break;
Jan Kara8e893462007-10-16 23:29:31 -0700945 case QUOTA_NL_ISOFTWARN:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 msg = " file quota exceeded.\r\n";
947 break;
Jan Kara8e893462007-10-16 23:29:31 -0700948 case QUOTA_NL_BHARDWARN:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 msg = " block limit reached.\r\n";
950 break;
Jan Kara8e893462007-10-16 23:29:31 -0700951 case QUOTA_NL_BSOFTLONGWARN:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 msg = " block quota exceeded too long.\r\n";
953 break;
Jan Kara8e893462007-10-16 23:29:31 -0700954 case QUOTA_NL_BSOFTWARN:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 msg = " block quota exceeded.\r\n";
956 break;
957 }
Peter Zijlstra24ec8392006-12-08 02:36:04 -0800958 tty_write_message(tty, msg);
Alan Cox452a00d2008-10-13 10:39:13 +0100959 tty_kref_put(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960}
Jan Kara8e893462007-10-16 23:29:31 -0700961#endif
962
963#ifdef CONFIG_QUOTA_NETLINK_INTERFACE
964
Jan Kara8e893462007-10-16 23:29:31 -0700965/* Netlink family structure for quota */
966static struct genl_family quota_genl_family = {
967 .id = GENL_ID_GENERATE,
968 .hdrsize = 0,
969 .name = "VFS_DQUOT",
970 .version = 1,
971 .maxattr = QUOTA_NL_A_MAX,
972};
973
974/* Send warning to userspace about user which exceeded quota */
975static void send_warning(const struct dquot *dquot, const char warntype)
976{
977 static atomic_t seq;
978 struct sk_buff *skb;
979 void *msg_head;
980 int ret;
Jan Kara22dd4832007-12-22 14:03:25 -0800981 int msg_size = 4 * nla_total_size(sizeof(u32)) +
982 2 * nla_total_size(sizeof(u64));
Jan Kara8e893462007-10-16 23:29:31 -0700983
984 /* We have to allocate using GFP_NOFS as we are called from a
985 * filesystem performing write and thus further recursion into
986 * the fs to free some data could cause deadlocks. */
Jan Kara22dd4832007-12-22 14:03:25 -0800987 skb = genlmsg_new(msg_size, GFP_NOFS);
Jan Kara8e893462007-10-16 23:29:31 -0700988 if (!skb) {
989 printk(KERN_ERR
990 "VFS: Not enough memory to send quota warning.\n");
991 return;
992 }
993 msg_head = genlmsg_put(skb, 0, atomic_add_return(1, &seq),
994 &quota_genl_family, 0, QUOTA_NL_C_WARNING);
995 if (!msg_head) {
996 printk(KERN_ERR
997 "VFS: Cannot store netlink header in quota warning.\n");
998 goto err_out;
999 }
1000 ret = nla_put_u32(skb, QUOTA_NL_A_QTYPE, dquot->dq_type);
1001 if (ret)
1002 goto attr_err_out;
1003 ret = nla_put_u64(skb, QUOTA_NL_A_EXCESS_ID, dquot->dq_id);
1004 if (ret)
1005 goto attr_err_out;
1006 ret = nla_put_u32(skb, QUOTA_NL_A_WARNING, warntype);
1007 if (ret)
1008 goto attr_err_out;
1009 ret = nla_put_u32(skb, QUOTA_NL_A_DEV_MAJOR,
1010 MAJOR(dquot->dq_sb->s_dev));
1011 if (ret)
1012 goto attr_err_out;
1013 ret = nla_put_u32(skb, QUOTA_NL_A_DEV_MINOR,
1014 MINOR(dquot->dq_sb->s_dev));
1015 if (ret)
1016 goto attr_err_out;
David Howellsda9592e2008-11-14 10:39:05 +11001017 ret = nla_put_u64(skb, QUOTA_NL_A_CAUSED_ID, current_uid());
Jan Kara8e893462007-10-16 23:29:31 -07001018 if (ret)
1019 goto attr_err_out;
1020 genlmsg_end(skb, msg_head);
1021
1022 ret = genlmsg_multicast(skb, 0, quota_genl_family.id, GFP_NOFS);
1023 if (ret < 0 && ret != -ESRCH)
1024 printk(KERN_ERR
1025 "VFS: Failed to send notification message: %d\n", ret);
1026 return;
1027attr_err_out:
Jan Kara22dd4832007-12-22 14:03:25 -08001028 printk(KERN_ERR "VFS: Not enough space to compose quota message!\n");
Jan Kara8e893462007-10-16 23:29:31 -07001029err_out:
1030 kfree_skb(skb);
1031}
1032#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033
Jan Kara087ee8d2007-12-17 16:20:26 -08001034static inline void flush_warnings(struct dquot * const *dquots, char *warntype)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035{
1036 int i;
1037
1038 for (i = 0; i < MAXQUOTAS; i++)
Jan Karac5254602007-12-22 14:03:25 -08001039 if (dquots[i] != NODQUOT && warntype[i] != QUOTA_NL_NOWARN &&
1040 !warning_issued(dquots[i], warntype[i])) {
Jan Kara8e893462007-10-16 23:29:31 -07001041#ifdef CONFIG_PRINT_QUOTA_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 print_warning(dquots[i], warntype[i]);
Jan Kara8e893462007-10-16 23:29:31 -07001043#endif
1044#ifdef CONFIG_QUOTA_NETLINK_INTERFACE
1045 send_warning(dquots[i], warntype[i]);
1046#endif
1047 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048}
1049
1050static inline char ignore_hardlimit(struct dquot *dquot)
1051{
1052 struct mem_dqinfo *info = &sb_dqopt(dquot->dq_sb)->info[dquot->dq_type];
1053
1054 return capable(CAP_SYS_RESOURCE) &&
1055 (info->dqi_format->qf_fmt_id != QFMT_VFS_OLD || !(info->dqi_flags & V1_DQF_RSQUASH));
1056}
1057
1058/* needs dq_data_lock */
Jan Kara12095462008-08-20 14:45:12 +02001059static int check_idq(struct dquot *dquot, qsize_t inodes, char *warntype)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060{
Jan Kara8e893462007-10-16 23:29:31 -07001061 *warntype = QUOTA_NL_NOWARN;
Jan Karaf55abc02008-08-20 17:50:32 +02001062 if (!sb_has_quota_limits_enabled(dquot->dq_sb, dquot->dq_type) ||
1063 test_bit(DQ_FAKE_B, &dquot->dq_flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 return QUOTA_OK;
1065
1066 if (dquot->dq_dqb.dqb_ihardlimit &&
1067 (dquot->dq_dqb.dqb_curinodes + inodes) > dquot->dq_dqb.dqb_ihardlimit &&
1068 !ignore_hardlimit(dquot)) {
Jan Kara8e893462007-10-16 23:29:31 -07001069 *warntype = QUOTA_NL_IHARDWARN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 return NO_QUOTA;
1071 }
1072
1073 if (dquot->dq_dqb.dqb_isoftlimit &&
1074 (dquot->dq_dqb.dqb_curinodes + inodes) > dquot->dq_dqb.dqb_isoftlimit &&
1075 dquot->dq_dqb.dqb_itime && get_seconds() >= dquot->dq_dqb.dqb_itime &&
1076 !ignore_hardlimit(dquot)) {
Jan Kara8e893462007-10-16 23:29:31 -07001077 *warntype = QUOTA_NL_ISOFTLONGWARN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 return NO_QUOTA;
1079 }
1080
1081 if (dquot->dq_dqb.dqb_isoftlimit &&
1082 (dquot->dq_dqb.dqb_curinodes + inodes) > dquot->dq_dqb.dqb_isoftlimit &&
1083 dquot->dq_dqb.dqb_itime == 0) {
Jan Kara8e893462007-10-16 23:29:31 -07001084 *warntype = QUOTA_NL_ISOFTWARN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 dquot->dq_dqb.dqb_itime = get_seconds() + sb_dqopt(dquot->dq_sb)->info[dquot->dq_type].dqi_igrace;
1086 }
1087
1088 return QUOTA_OK;
1089}
1090
1091/* needs dq_data_lock */
1092static int check_bdq(struct dquot *dquot, qsize_t space, int prealloc, char *warntype)
1093{
Jan Kara8e893462007-10-16 23:29:31 -07001094 *warntype = QUOTA_NL_NOWARN;
Jan Karaf55abc02008-08-20 17:50:32 +02001095 if (!sb_has_quota_limits_enabled(dquot->dq_sb, dquot->dq_type) ||
1096 test_bit(DQ_FAKE_B, &dquot->dq_flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 return QUOTA_OK;
1098
1099 if (dquot->dq_dqb.dqb_bhardlimit &&
Jan Kara12095462008-08-20 14:45:12 +02001100 dquot->dq_dqb.dqb_curspace + space > dquot->dq_dqb.dqb_bhardlimit &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 !ignore_hardlimit(dquot)) {
1102 if (!prealloc)
Jan Kara8e893462007-10-16 23:29:31 -07001103 *warntype = QUOTA_NL_BHARDWARN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 return NO_QUOTA;
1105 }
1106
1107 if (dquot->dq_dqb.dqb_bsoftlimit &&
Jan Kara12095462008-08-20 14:45:12 +02001108 dquot->dq_dqb.dqb_curspace + space > dquot->dq_dqb.dqb_bsoftlimit &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 dquot->dq_dqb.dqb_btime && get_seconds() >= dquot->dq_dqb.dqb_btime &&
1110 !ignore_hardlimit(dquot)) {
1111 if (!prealloc)
Jan Kara8e893462007-10-16 23:29:31 -07001112 *warntype = QUOTA_NL_BSOFTLONGWARN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 return NO_QUOTA;
1114 }
1115
1116 if (dquot->dq_dqb.dqb_bsoftlimit &&
Jan Kara12095462008-08-20 14:45:12 +02001117 dquot->dq_dqb.dqb_curspace + space > dquot->dq_dqb.dqb_bsoftlimit &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 dquot->dq_dqb.dqb_btime == 0) {
1119 if (!prealloc) {
Jan Kara8e893462007-10-16 23:29:31 -07001120 *warntype = QUOTA_NL_BSOFTWARN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 dquot->dq_dqb.dqb_btime = get_seconds() + sb_dqopt(dquot->dq_sb)->info[dquot->dq_type].dqi_bgrace;
1122 }
1123 else
1124 /*
1125 * We don't allow preallocation to exceed softlimit so exceeding will
1126 * be always printed
1127 */
1128 return NO_QUOTA;
1129 }
1130
1131 return QUOTA_OK;
1132}
1133
Jan Kara12095462008-08-20 14:45:12 +02001134static int info_idq_free(struct dquot *dquot, qsize_t inodes)
Jan Kara657d3bf2008-07-25 01:46:52 -07001135{
1136 if (test_bit(DQ_FAKE_B, &dquot->dq_flags) ||
Jan Karaf55abc02008-08-20 17:50:32 +02001137 dquot->dq_dqb.dqb_curinodes <= dquot->dq_dqb.dqb_isoftlimit ||
1138 !sb_has_quota_limits_enabled(dquot->dq_sb, dquot->dq_type))
Jan Kara657d3bf2008-07-25 01:46:52 -07001139 return QUOTA_NL_NOWARN;
1140
1141 if (dquot->dq_dqb.dqb_curinodes - inodes <= dquot->dq_dqb.dqb_isoftlimit)
1142 return QUOTA_NL_ISOFTBELOW;
1143 if (dquot->dq_dqb.dqb_curinodes >= dquot->dq_dqb.dqb_ihardlimit &&
1144 dquot->dq_dqb.dqb_curinodes - inodes < dquot->dq_dqb.dqb_ihardlimit)
1145 return QUOTA_NL_IHARDBELOW;
1146 return QUOTA_NL_NOWARN;
1147}
1148
1149static int info_bdq_free(struct dquot *dquot, qsize_t space)
1150{
1151 if (test_bit(DQ_FAKE_B, &dquot->dq_flags) ||
Jan Kara12095462008-08-20 14:45:12 +02001152 dquot->dq_dqb.dqb_curspace <= dquot->dq_dqb.dqb_bsoftlimit)
Jan Kara657d3bf2008-07-25 01:46:52 -07001153 return QUOTA_NL_NOWARN;
1154
Jan Kara12095462008-08-20 14:45:12 +02001155 if (dquot->dq_dqb.dqb_curspace - space <= dquot->dq_dqb.dqb_bsoftlimit)
Jan Kara657d3bf2008-07-25 01:46:52 -07001156 return QUOTA_NL_BSOFTBELOW;
Jan Kara12095462008-08-20 14:45:12 +02001157 if (dquot->dq_dqb.dqb_curspace >= dquot->dq_dqb.dqb_bhardlimit &&
1158 dquot->dq_dqb.dqb_curspace - space < dquot->dq_dqb.dqb_bhardlimit)
Jan Kara657d3bf2008-07-25 01:46:52 -07001159 return QUOTA_NL_BHARDBELOW;
1160 return QUOTA_NL_NOWARN;
1161}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162/*
1163 * Initialize quota pointers in inode
1164 * Transaction must be started at entry
1165 */
1166int dquot_initialize(struct inode *inode, int type)
1167{
1168 unsigned int id = 0;
1169 int cnt, ret = 0;
1170
Ingo Molnard3be9152006-03-23 03:00:29 -08001171 /* First test before acquiring mutex - solves deadlocks when we
1172 * re-enter the quota code and are already holding the mutex */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 if (IS_NOQUOTA(inode))
1174 return 0;
1175 down_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
1176 /* Having dqptr_sem we know NOQUOTA flags can't be altered... */
1177 if (IS_NOQUOTA(inode))
1178 goto out_err;
1179 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1180 if (type != -1 && cnt != type)
1181 continue;
1182 if (inode->i_dquot[cnt] == NODQUOT) {
1183 switch (cnt) {
1184 case USRQUOTA:
1185 id = inode->i_uid;
1186 break;
1187 case GRPQUOTA:
1188 id = inode->i_gid;
1189 break;
1190 }
1191 inode->i_dquot[cnt] = dqget(inode->i_sb, id, cnt);
1192 }
1193 }
1194out_err:
1195 up_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
1196 return ret;
1197}
1198
1199/*
1200 * Release all quotas referenced by inode
1201 * Transaction must be started at an entry
1202 */
Jan Kara3d9ea252008-10-10 16:12:23 +02001203int dquot_drop_locked(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204{
1205 int cnt;
1206
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1208 if (inode->i_dquot[cnt] != NODQUOT) {
1209 dqput(inode->i_dquot[cnt]);
1210 inode->i_dquot[cnt] = NODQUOT;
1211 }
1212 }
Jan Kara3d9ea252008-10-10 16:12:23 +02001213 return 0;
1214}
1215
1216int dquot_drop(struct inode *inode)
1217{
1218 down_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
1219 dquot_drop_locked(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 up_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
1221 return 0;
1222}
1223
Jan Karab85f4b82008-07-25 01:46:50 -07001224/* Wrapper to remove references to quota structures from inode */
1225void vfs_dq_drop(struct inode *inode)
1226{
1227 /* Here we can get arbitrary inode from clear_inode() so we have
1228 * to be careful. OTOH we don't need locking as quota operations
1229 * are allowed to change only at mount time */
1230 if (!IS_NOQUOTA(inode) && inode->i_sb && inode->i_sb->dq_op
1231 && inode->i_sb->dq_op->drop) {
1232 int cnt;
1233 /* Test before calling to rule out calls from proc and such
1234 * where we are not allowed to block. Note that this is
1235 * actually reliable test even without the lock - the caller
1236 * must assure that nobody can come after the DQUOT_DROP and
1237 * add quota pointers back anyway */
1238 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1239 if (inode->i_dquot[cnt] != NODQUOT)
1240 break;
1241 if (cnt < MAXQUOTAS)
1242 inode->i_sb->dq_op->drop(inode);
1243 }
1244}
1245
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246/*
1247 * Following four functions update i_blocks+i_bytes fields and
1248 * quota information (together with appropriate checks)
1249 * NOTE: We absolutely rely on the fact that caller dirties
1250 * the inode (usually macros in quotaops.h care about this) and
1251 * holds a handle for the current transaction so that dquot write and
1252 * inode write go into the same transaction.
1253 */
1254
1255/*
1256 * This operation can block, but only after everything is updated
1257 */
1258int dquot_alloc_space(struct inode *inode, qsize_t number, int warn)
1259{
1260 int cnt, ret = NO_QUOTA;
1261 char warntype[MAXQUOTAS];
1262
Ingo Molnard3be9152006-03-23 03:00:29 -08001263 /* First test before acquiring mutex - solves deadlocks when we
1264 * re-enter the quota code and are already holding the mutex */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 if (IS_NOQUOTA(inode)) {
1266out_add:
1267 inode_add_bytes(inode, number);
1268 return QUOTA_OK;
1269 }
1270 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
Jan Kara8e893462007-10-16 23:29:31 -07001271 warntype[cnt] = QUOTA_NL_NOWARN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272
1273 down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1274 if (IS_NOQUOTA(inode)) { /* Now we can do reliable test... */
1275 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1276 goto out_add;
1277 }
1278 spin_lock(&dq_data_lock);
1279 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1280 if (inode->i_dquot[cnt] == NODQUOT)
1281 continue;
1282 if (check_bdq(inode->i_dquot[cnt], number, warn, warntype+cnt) == NO_QUOTA)
1283 goto warn_put_all;
1284 }
1285 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1286 if (inode->i_dquot[cnt] == NODQUOT)
1287 continue;
1288 dquot_incr_space(inode->i_dquot[cnt], number);
1289 }
1290 inode_add_bytes(inode, number);
1291 ret = QUOTA_OK;
1292warn_put_all:
1293 spin_unlock(&dq_data_lock);
1294 if (ret == QUOTA_OK)
1295 /* Dirtify all the dquots - this can block when journalling */
1296 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1297 if (inode->i_dquot[cnt])
1298 mark_dquot_dirty(inode->i_dquot[cnt]);
1299 flush_warnings(inode->i_dquot, warntype);
1300 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1301 return ret;
1302}
1303
1304/*
1305 * This operation can block, but only after everything is updated
1306 */
Jan Kara12095462008-08-20 14:45:12 +02001307int dquot_alloc_inode(const struct inode *inode, qsize_t number)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308{
1309 int cnt, ret = NO_QUOTA;
1310 char warntype[MAXQUOTAS];
1311
Ingo Molnard3be9152006-03-23 03:00:29 -08001312 /* First test before acquiring mutex - solves deadlocks when we
1313 * re-enter the quota code and are already holding the mutex */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 if (IS_NOQUOTA(inode))
1315 return QUOTA_OK;
1316 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
Jan Kara8e893462007-10-16 23:29:31 -07001317 warntype[cnt] = QUOTA_NL_NOWARN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1319 if (IS_NOQUOTA(inode)) {
1320 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1321 return QUOTA_OK;
1322 }
1323 spin_lock(&dq_data_lock);
1324 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1325 if (inode->i_dquot[cnt] == NODQUOT)
1326 continue;
1327 if (check_idq(inode->i_dquot[cnt], number, warntype+cnt) == NO_QUOTA)
1328 goto warn_put_all;
1329 }
1330
1331 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1332 if (inode->i_dquot[cnt] == NODQUOT)
1333 continue;
1334 dquot_incr_inodes(inode->i_dquot[cnt], number);
1335 }
1336 ret = QUOTA_OK;
1337warn_put_all:
1338 spin_unlock(&dq_data_lock);
1339 if (ret == QUOTA_OK)
1340 /* Dirtify all the dquots - this can block when journalling */
1341 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1342 if (inode->i_dquot[cnt])
1343 mark_dquot_dirty(inode->i_dquot[cnt]);
Jan Kara087ee8d2007-12-17 16:20:26 -08001344 flush_warnings(inode->i_dquot, warntype);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1346 return ret;
1347}
1348
1349/*
1350 * This operation can block, but only after everything is updated
1351 */
1352int dquot_free_space(struct inode *inode, qsize_t number)
1353{
1354 unsigned int cnt;
Jan Kara657d3bf2008-07-25 01:46:52 -07001355 char warntype[MAXQUOTAS];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356
Ingo Molnard3be9152006-03-23 03:00:29 -08001357 /* First test before acquiring mutex - solves deadlocks when we
1358 * re-enter the quota code and are already holding the mutex */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 if (IS_NOQUOTA(inode)) {
1360out_sub:
1361 inode_sub_bytes(inode, number);
1362 return QUOTA_OK;
1363 }
Jan Kara657d3bf2008-07-25 01:46:52 -07001364
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1366 /* Now recheck reliably when holding dqptr_sem */
1367 if (IS_NOQUOTA(inode)) {
1368 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1369 goto out_sub;
1370 }
1371 spin_lock(&dq_data_lock);
1372 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1373 if (inode->i_dquot[cnt] == NODQUOT)
1374 continue;
Jan Kara657d3bf2008-07-25 01:46:52 -07001375 warntype[cnt] = info_bdq_free(inode->i_dquot[cnt], number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 dquot_decr_space(inode->i_dquot[cnt], number);
1377 }
1378 inode_sub_bytes(inode, number);
1379 spin_unlock(&dq_data_lock);
1380 /* Dirtify all the dquots - this can block when journalling */
1381 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1382 if (inode->i_dquot[cnt])
1383 mark_dquot_dirty(inode->i_dquot[cnt]);
Jan Kara657d3bf2008-07-25 01:46:52 -07001384 flush_warnings(inode->i_dquot, warntype);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1386 return QUOTA_OK;
1387}
1388
1389/*
1390 * This operation can block, but only after everything is updated
1391 */
Jan Kara12095462008-08-20 14:45:12 +02001392int dquot_free_inode(const struct inode *inode, qsize_t number)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393{
1394 unsigned int cnt;
Jan Kara657d3bf2008-07-25 01:46:52 -07001395 char warntype[MAXQUOTAS];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396
Ingo Molnard3be9152006-03-23 03:00:29 -08001397 /* First test before acquiring mutex - solves deadlocks when we
1398 * re-enter the quota code and are already holding the mutex */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399 if (IS_NOQUOTA(inode))
1400 return QUOTA_OK;
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 return QUOTA_OK;
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_idq_free(inode->i_dquot[cnt], number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 dquot_decr_inodes(inode->i_dquot[cnt], number);
1414 }
1415 spin_unlock(&dq_data_lock);
1416 /* Dirtify all the dquots - this can block when journalling */
1417 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1418 if (inode->i_dquot[cnt])
1419 mark_dquot_dirty(inode->i_dquot[cnt]);
Jan Kara657d3bf2008-07-25 01:46:52 -07001420 flush_warnings(inode->i_dquot, warntype);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1422 return QUOTA_OK;
1423}
1424
1425/*
1426 * Transfer the number of inode and blocks from one diskquota to an other.
1427 *
1428 * This operation can block, but only after everything is updated
1429 * A transaction must be started when entering this function.
1430 */
1431int dquot_transfer(struct inode *inode, struct iattr *iattr)
1432{
1433 qsize_t space;
1434 struct dquot *transfer_from[MAXQUOTAS];
1435 struct dquot *transfer_to[MAXQUOTAS];
1436 int cnt, ret = NO_QUOTA, chuid = (iattr->ia_valid & ATTR_UID) && inode->i_uid != iattr->ia_uid,
1437 chgid = (iattr->ia_valid & ATTR_GID) && inode->i_gid != iattr->ia_gid;
Jan Kara657d3bf2008-07-25 01:46:52 -07001438 char warntype_to[MAXQUOTAS];
1439 char warntype_from_inodes[MAXQUOTAS], warntype_from_space[MAXQUOTAS];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440
Ingo Molnard3be9152006-03-23 03:00:29 -08001441 /* First test before acquiring mutex - solves deadlocks when we
1442 * re-enter the quota code and are already holding the mutex */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 if (IS_NOQUOTA(inode))
1444 return QUOTA_OK;
1445 /* Clear the arrays */
1446 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1447 transfer_to[cnt] = transfer_from[cnt] = NODQUOT;
Jan Kara657d3bf2008-07-25 01:46:52 -07001448 warntype_to[cnt] = QUOTA_NL_NOWARN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449 }
1450 down_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
1451 /* Now recheck reliably when holding dqptr_sem */
1452 if (IS_NOQUOTA(inode)) { /* File without quota accounting? */
1453 up_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
1454 return QUOTA_OK;
1455 }
1456 /* First build the transfer_to list - here we can block on
1457 * reading/instantiating of dquots. We know that the transaction for
1458 * us was already started so we don't violate lock ranking here */
1459 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1460 switch (cnt) {
1461 case USRQUOTA:
1462 if (!chuid)
1463 continue;
1464 transfer_to[cnt] = dqget(inode->i_sb, iattr->ia_uid, cnt);
1465 break;
1466 case GRPQUOTA:
1467 if (!chgid)
1468 continue;
1469 transfer_to[cnt] = dqget(inode->i_sb, iattr->ia_gid, cnt);
1470 break;
1471 }
1472 }
1473 spin_lock(&dq_data_lock);
1474 space = inode_get_bytes(inode);
1475 /* Build the transfer_from list and check the limits */
1476 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1477 if (transfer_to[cnt] == NODQUOT)
1478 continue;
1479 transfer_from[cnt] = inode->i_dquot[cnt];
Jan Kara657d3bf2008-07-25 01:46:52 -07001480 if (check_idq(transfer_to[cnt], 1, warntype_to + cnt) ==
1481 NO_QUOTA || check_bdq(transfer_to[cnt], space, 0,
1482 warntype_to + cnt) == NO_QUOTA)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483 goto warn_put_all;
1484 }
1485
1486 /*
1487 * Finally perform the needed transfer from transfer_from to transfer_to
1488 */
1489 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1490 /*
1491 * Skip changes for same uid or gid or for turned off quota-type.
1492 */
1493 if (transfer_to[cnt] == NODQUOT)
1494 continue;
1495
1496 /* Due to IO error we might not have transfer_from[] structure */
1497 if (transfer_from[cnt]) {
Jan Kara657d3bf2008-07-25 01:46:52 -07001498 warntype_from_inodes[cnt] =
1499 info_idq_free(transfer_from[cnt], 1);
1500 warntype_from_space[cnt] =
1501 info_bdq_free(transfer_from[cnt], space);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502 dquot_decr_inodes(transfer_from[cnt], 1);
1503 dquot_decr_space(transfer_from[cnt], space);
1504 }
1505
1506 dquot_incr_inodes(transfer_to[cnt], 1);
1507 dquot_incr_space(transfer_to[cnt], space);
1508
1509 inode->i_dquot[cnt] = transfer_to[cnt];
1510 }
1511 ret = QUOTA_OK;
1512warn_put_all:
1513 spin_unlock(&dq_data_lock);
1514 /* Dirtify all the dquots - this can block when journalling */
1515 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1516 if (transfer_from[cnt])
1517 mark_dquot_dirty(transfer_from[cnt]);
1518 if (transfer_to[cnt])
1519 mark_dquot_dirty(transfer_to[cnt]);
1520 }
Jan Kara657d3bf2008-07-25 01:46:52 -07001521 flush_warnings(transfer_to, warntype_to);
1522 flush_warnings(transfer_from, warntype_from_inodes);
1523 flush_warnings(transfer_from, warntype_from_space);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524
1525 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1526 if (ret == QUOTA_OK && transfer_from[cnt] != NODQUOT)
1527 dqput(transfer_from[cnt]);
1528 if (ret == NO_QUOTA && transfer_to[cnt] != NODQUOT)
1529 dqput(transfer_to[cnt]);
1530 }
1531 up_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
1532 return ret;
1533}
1534
Jan Karab85f4b82008-07-25 01:46:50 -07001535/* Wrapper for transferring ownership of an inode */
1536int vfs_dq_transfer(struct inode *inode, struct iattr *iattr)
1537{
Jan Karaf55abc02008-08-20 17:50:32 +02001538 if (sb_any_quota_active(inode->i_sb) && !IS_NOQUOTA(inode)) {
Jan Karab85f4b82008-07-25 01:46:50 -07001539 vfs_dq_init(inode);
1540 if (inode->i_sb->dq_op->transfer(inode, iattr) == NO_QUOTA)
1541 return 1;
1542 }
1543 return 0;
1544}
1545
1546
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547/*
1548 * Write info of quota file to disk
1549 */
1550int dquot_commit_info(struct super_block *sb, int type)
1551{
1552 int ret;
1553 struct quota_info *dqopt = sb_dqopt(sb);
1554
Ingo Molnard3be9152006-03-23 03:00:29 -08001555 mutex_lock(&dqopt->dqio_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556 ret = dqopt->ops[type]->write_file_info(sb, type);
Ingo Molnard3be9152006-03-23 03:00:29 -08001557 mutex_unlock(&dqopt->dqio_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558 return ret;
1559}
1560
1561/*
1562 * Definitions of diskquota operations.
1563 */
1564struct dquot_operations dquot_operations = {
1565 .initialize = dquot_initialize,
1566 .drop = dquot_drop,
1567 .alloc_space = dquot_alloc_space,
1568 .alloc_inode = dquot_alloc_inode,
1569 .free_space = dquot_free_space,
1570 .free_inode = dquot_free_inode,
1571 .transfer = dquot_transfer,
1572 .write_dquot = dquot_commit,
1573 .acquire_dquot = dquot_acquire,
1574 .release_dquot = dquot_release,
1575 .mark_dirty = dquot_mark_dquot_dirty,
Jan Kara74f783a2008-08-19 14:51:22 +02001576 .write_info = dquot_commit_info,
1577 .alloc_dquot = dquot_alloc,
1578 .destroy_dquot = dquot_destroy,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579};
1580
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581/*
1582 * Turn quota off on a device. type == -1 ==> quotaoff for all types (umount)
1583 */
Jan Karaf55abc02008-08-20 17:50:32 +02001584int vfs_quota_disable(struct super_block *sb, int type, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585{
Jan Kara0ff5af82008-04-28 02:14:33 -07001586 int cnt, ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587 struct quota_info *dqopt = sb_dqopt(sb);
1588 struct inode *toputinode[MAXQUOTAS];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589
Jan Karaf55abc02008-08-20 17:50:32 +02001590 /* Cannot turn off usage accounting without turning off limits, or
1591 * suspend quotas and simultaneously turn quotas off. */
1592 if ((flags & DQUOT_USAGE_ENABLED && !(flags & DQUOT_LIMITS_ENABLED))
1593 || (flags & DQUOT_SUSPENDED && flags & (DQUOT_LIMITS_ENABLED |
1594 DQUOT_USAGE_ENABLED)))
1595 return -EINVAL;
1596
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597 /* We need to serialize quota_off() for device */
Ingo Molnard3be9152006-03-23 03:00:29 -08001598 mutex_lock(&dqopt->dqonoff_mutex);
Jan Kara9377abd2008-05-12 14:02:08 -07001599
1600 /*
1601 * Skip everything if there's nothing to do. We have to do this because
1602 * sometimes we are called when fill_super() failed and calling
1603 * sync_fs() in such cases does no good.
1604 */
Jan Karaf55abc02008-08-20 17:50:32 +02001605 if (!sb_any_quota_loaded(sb)) {
Jan Kara9377abd2008-05-12 14:02:08 -07001606 mutex_unlock(&dqopt->dqonoff_mutex);
1607 return 0;
1608 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1610 toputinode[cnt] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611 if (type != -1 && cnt != type)
1612 continue;
Jan Karaf55abc02008-08-20 17:50:32 +02001613 if (!sb_has_quota_loaded(sb, cnt))
Jan Kara0ff5af82008-04-28 02:14:33 -07001614 continue;
Jan Karaf55abc02008-08-20 17:50:32 +02001615
1616 if (flags & DQUOT_SUSPENDED) {
1617 dqopt->flags |=
1618 dquot_state_flag(DQUOT_SUSPENDED, cnt);
1619 } else {
1620 dqopt->flags &= ~dquot_state_flag(flags, cnt);
1621 /* Turning off suspended quotas? */
1622 if (!sb_has_quota_loaded(sb, cnt) &&
1623 sb_has_quota_suspended(sb, cnt)) {
1624 dqopt->flags &= ~dquot_state_flag(
1625 DQUOT_SUSPENDED, cnt);
1626 iput(dqopt->files[cnt]);
1627 dqopt->files[cnt] = NULL;
1628 continue;
1629 }
Jan Kara0ff5af82008-04-28 02:14:33 -07001630 }
Jan Karaf55abc02008-08-20 17:50:32 +02001631
1632 /* We still have to keep quota loaded? */
1633 if (sb_has_quota_loaded(sb, cnt) && !(flags & DQUOT_SUSPENDED))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635
1636 /* Note: these are blocking operations */
1637 drop_dquot_ref(sb, cnt);
1638 invalidate_dquots(sb, cnt);
1639 /*
1640 * Now all dquots should be invalidated, all writes done so we should be only
1641 * users of the info. No locks needed.
1642 */
1643 if (info_dirty(&dqopt->info[cnt]))
1644 sb->dq_op->write_info(sb, cnt);
1645 if (dqopt->ops[cnt]->free_file_info)
1646 dqopt->ops[cnt]->free_file_info(sb, cnt);
1647 put_quota_format(dqopt->info[cnt].dqi_format);
1648
1649 toputinode[cnt] = dqopt->files[cnt];
Jan Karaf55abc02008-08-20 17:50:32 +02001650 if (!sb_has_quota_loaded(sb, cnt))
Jan Kara0ff5af82008-04-28 02:14:33 -07001651 dqopt->files[cnt] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652 dqopt->info[cnt].dqi_flags = 0;
1653 dqopt->info[cnt].dqi_igrace = 0;
1654 dqopt->info[cnt].dqi_bgrace = 0;
1655 dqopt->ops[cnt] = NULL;
1656 }
Ingo Molnard3be9152006-03-23 03:00:29 -08001657 mutex_unlock(&dqopt->dqonoff_mutex);
Jan Karaca785ec2008-09-30 17:53:37 +02001658
1659 /* Skip syncing and setting flags if quota files are hidden */
1660 if (dqopt->flags & DQUOT_QUOTA_SYS_FILE)
1661 goto put_inodes;
1662
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663 /* Sync the superblock so that buffers with quota data are written to
Al Viro7b7b1ac2005-11-07 17:13:39 -05001664 * disk (and so userspace sees correct data afterwards). */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665 if (sb->s_op->sync_fs)
1666 sb->s_op->sync_fs(sb, 1);
1667 sync_blockdev(sb->s_bdev);
1668 /* Now the quota files are just ordinary files and we can set the
1669 * inode flags back. Moreover we discard the pagecache so that
1670 * userspace sees the writes we did bypassing the pagecache. We
1671 * must also discard the blockdev buffers so that we see the
1672 * changes done by userspace on the next quotaon() */
1673 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1674 if (toputinode[cnt]) {
Ingo Molnard3be9152006-03-23 03:00:29 -08001675 mutex_lock(&dqopt->dqonoff_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676 /* If quota was reenabled in the meantime, we have
1677 * nothing to do */
Jan Karaf55abc02008-08-20 17:50:32 +02001678 if (!sb_has_quota_loaded(sb, cnt)) {
Jan Kara79254092007-05-16 22:11:19 -07001679 mutex_lock_nested(&toputinode[cnt]->i_mutex, I_MUTEX_QUOTA);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680 toputinode[cnt]->i_flags &= ~(S_IMMUTABLE |
1681 S_NOATIME | S_NOQUOTA);
1682 truncate_inode_pages(&toputinode[cnt]->i_data, 0);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001683 mutex_unlock(&toputinode[cnt]->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684 mark_inode_dirty(toputinode[cnt]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685 }
Ingo Molnard3be9152006-03-23 03:00:29 -08001686 mutex_unlock(&dqopt->dqonoff_mutex);
Jan Karaca785ec2008-09-30 17:53:37 +02001687 }
1688 if (sb->s_bdev)
1689 invalidate_bdev(sb->s_bdev);
1690put_inodes:
1691 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1692 if (toputinode[cnt]) {
Jan Kara0ff5af82008-04-28 02:14:33 -07001693 /* On remount RO, we keep the inode pointer so that we
Jan Karaf55abc02008-08-20 17:50:32 +02001694 * can reenable quota on the subsequent remount RW. We
1695 * have to check 'flags' variable and not use sb_has_
1696 * function because another quotaon / quotaoff could
1697 * change global state before we got here. We refuse
1698 * to suspend quotas when there is pending delete on
1699 * the quota file... */
1700 if (!(flags & DQUOT_SUSPENDED))
Jan Kara0ff5af82008-04-28 02:14:33 -07001701 iput(toputinode[cnt]);
1702 else if (!toputinode[cnt]->i_nlink)
1703 ret = -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704 }
Jan Kara0ff5af82008-04-28 02:14:33 -07001705 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706}
1707
Jan Karaf55abc02008-08-20 17:50:32 +02001708int vfs_quota_off(struct super_block *sb, int type, int remount)
1709{
1710 return vfs_quota_disable(sb, type, remount ? DQUOT_SUSPENDED :
1711 (DQUOT_USAGE_ENABLED | DQUOT_LIMITS_ENABLED));
1712}
1713
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714/*
1715 * Turn quotas on on a device
1716 */
1717
Jan Karaf55abc02008-08-20 17:50:32 +02001718/*
1719 * Helper function to turn quotas on when we already have the inode of
1720 * quota file and no quota information is loaded.
1721 */
1722static int vfs_load_quota_inode(struct inode *inode, int type, int format_id,
1723 unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724{
1725 struct quota_format_type *fmt = find_quota_format(format_id);
1726 struct super_block *sb = inode->i_sb;
1727 struct quota_info *dqopt = sb_dqopt(sb);
1728 int error;
1729 int oldflags = -1;
1730
1731 if (!fmt)
1732 return -ESRCH;
1733 if (!S_ISREG(inode->i_mode)) {
1734 error = -EACCES;
1735 goto out_fmt;
1736 }
1737 if (IS_RDONLY(inode)) {
1738 error = -EROFS;
1739 goto out_fmt;
1740 }
1741 if (!sb->s_op->quota_write || !sb->s_op->quota_read) {
1742 error = -EINVAL;
1743 goto out_fmt;
1744 }
Jan Karaf55abc02008-08-20 17:50:32 +02001745 /* Usage always has to be set... */
1746 if (!(flags & DQUOT_USAGE_ENABLED)) {
1747 error = -EINVAL;
1748 goto out_fmt;
1749 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750
Jan Karaca785ec2008-09-30 17:53:37 +02001751 if (!(dqopt->flags & DQUOT_QUOTA_SYS_FILE)) {
1752 /* As we bypass the pagecache we must now flush the inode so
1753 * that we see all the changes from userspace... */
1754 write_inode_now(inode, 1);
1755 /* And now flush the block cache so that kernel sees the
1756 * changes */
1757 invalidate_bdev(sb->s_bdev);
1758 }
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001759 mutex_lock(&inode->i_mutex);
Ingo Molnard3be9152006-03-23 03:00:29 -08001760 mutex_lock(&dqopt->dqonoff_mutex);
Jan Karaf55abc02008-08-20 17:50:32 +02001761 if (sb_has_quota_loaded(sb, type)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762 error = -EBUSY;
1763 goto out_lock;
1764 }
Jan Karaca785ec2008-09-30 17:53:37 +02001765
1766 if (!(dqopt->flags & DQUOT_QUOTA_SYS_FILE)) {
1767 /* We don't want quota and atime on quota files (deadlocks
1768 * possible) Also nobody should write to the file - we use
1769 * special IO operations which ignore the immutable bit. */
1770 down_write(&dqopt->dqptr_sem);
1771 oldflags = inode->i_flags & (S_NOATIME | S_IMMUTABLE | S_NOQUOTA);
1772 inode->i_flags |= S_NOQUOTA | S_NOATIME | S_IMMUTABLE;
1773 up_write(&dqopt->dqptr_sem);
1774 sb->dq_op->drop(inode);
1775 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776
1777 error = -EIO;
1778 dqopt->files[type] = igrab(inode);
1779 if (!dqopt->files[type])
1780 goto out_lock;
1781 error = -EINVAL;
1782 if (!fmt->qf_ops->check_quota_file(sb, type))
1783 goto out_file_init;
1784
1785 dqopt->ops[type] = fmt->qf_ops;
1786 dqopt->info[type].dqi_format = fmt;
Jan Kara0ff5af82008-04-28 02:14:33 -07001787 dqopt->info[type].dqi_fmt_id = format_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788 INIT_LIST_HEAD(&dqopt->info[type].dqi_dirty_list);
Ingo Molnard3be9152006-03-23 03:00:29 -08001789 mutex_lock(&dqopt->dqio_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790 if ((error = dqopt->ops[type]->read_file_info(sb, type)) < 0) {
Ingo Molnard3be9152006-03-23 03:00:29 -08001791 mutex_unlock(&dqopt->dqio_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792 goto out_file_init;
1793 }
Ingo Molnard3be9152006-03-23 03:00:29 -08001794 mutex_unlock(&dqopt->dqio_mutex);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001795 mutex_unlock(&inode->i_mutex);
Jan Karaf55abc02008-08-20 17:50:32 +02001796 dqopt->flags |= dquot_state_flag(flags, type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797
1798 add_dquot_ref(sb, type);
Ingo Molnard3be9152006-03-23 03:00:29 -08001799 mutex_unlock(&dqopt->dqonoff_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800
1801 return 0;
1802
1803out_file_init:
1804 dqopt->files[type] = NULL;
1805 iput(inode);
1806out_lock:
Ingo Molnard3be9152006-03-23 03:00:29 -08001807 mutex_unlock(&dqopt->dqonoff_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808 if (oldflags != -1) {
1809 down_write(&dqopt->dqptr_sem);
1810 /* Set the flags back (in the case of accidental quotaon()
1811 * on a wrong file we don't want to mess up the flags) */
1812 inode->i_flags &= ~(S_NOATIME | S_NOQUOTA | S_IMMUTABLE);
1813 inode->i_flags |= oldflags;
1814 up_write(&dqopt->dqptr_sem);
1815 }
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001816 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817out_fmt:
1818 put_quota_format(fmt);
1819
1820 return error;
1821}
1822
Jan Kara0ff5af82008-04-28 02:14:33 -07001823/* Reenable quotas on remount RW */
1824static int vfs_quota_on_remount(struct super_block *sb, int type)
1825{
1826 struct quota_info *dqopt = sb_dqopt(sb);
1827 struct inode *inode;
1828 int ret;
Jan Karaf55abc02008-08-20 17:50:32 +02001829 unsigned int flags;
Jan Kara0ff5af82008-04-28 02:14:33 -07001830
1831 mutex_lock(&dqopt->dqonoff_mutex);
1832 if (!sb_has_quota_suspended(sb, type)) {
1833 mutex_unlock(&dqopt->dqonoff_mutex);
1834 return 0;
1835 }
Jan Kara0ff5af82008-04-28 02:14:33 -07001836 inode = dqopt->files[type];
1837 dqopt->files[type] = NULL;
Jan Karaf55abc02008-08-20 17:50:32 +02001838 flags = dqopt->flags & dquot_state_flag(DQUOT_USAGE_ENABLED |
1839 DQUOT_LIMITS_ENABLED, type);
1840 dqopt->flags &= ~dquot_state_flag(DQUOT_STATE_FLAGS, type);
Jan Kara0ff5af82008-04-28 02:14:33 -07001841 mutex_unlock(&dqopt->dqonoff_mutex);
1842
Jan Karaf55abc02008-08-20 17:50:32 +02001843 flags = dquot_generic_flag(flags, type);
1844 ret = vfs_load_quota_inode(inode, type, dqopt->info[type].dqi_fmt_id,
1845 flags);
Jan Kara0ff5af82008-04-28 02:14:33 -07001846 iput(inode);
1847
1848 return ret;
1849}
1850
Al Viro77e69da2008-08-01 04:29:18 -04001851int vfs_quota_on_path(struct super_block *sb, int type, int format_id,
1852 struct path *path)
1853{
1854 int error = security_quota_on(path->dentry);
1855 if (error)
1856 return error;
1857 /* Quota file not on the same filesystem? */
1858 if (path->mnt->mnt_sb != sb)
1859 error = -EXDEV;
1860 else
Jan Karaf55abc02008-08-20 17:50:32 +02001861 error = vfs_load_quota_inode(path->dentry->d_inode, type,
1862 format_id, DQUOT_USAGE_ENABLED |
1863 DQUOT_LIMITS_ENABLED);
Al Viro77e69da2008-08-01 04:29:18 -04001864 return error;
1865}
1866
Al Viro82646132008-08-02 00:57:06 -04001867int vfs_quota_on(struct super_block *sb, int type, int format_id, char *name,
Jan Kara0ff5af82008-04-28 02:14:33 -07001868 int remount)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869{
Al Viro82646132008-08-02 00:57:06 -04001870 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871 int error;
1872
Jan Kara0ff5af82008-04-28 02:14:33 -07001873 if (remount)
1874 return vfs_quota_on_remount(sb, type);
1875
Al Viro82646132008-08-02 00:57:06 -04001876 error = kern_path(name, LOOKUP_FOLLOW, &path);
Al Viro77e69da2008-08-01 04:29:18 -04001877 if (!error) {
Al Viro82646132008-08-02 00:57:06 -04001878 error = vfs_quota_on_path(sb, type, format_id, &path);
1879 path_put(&path);
Al Viro77e69da2008-08-01 04:29:18 -04001880 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881 return error;
1882}
1883
1884/*
Jan Karaf55abc02008-08-20 17:50:32 +02001885 * More powerful function for turning on quotas allowing setting
1886 * of individual quota flags
1887 */
1888int vfs_quota_enable(struct inode *inode, int type, int format_id,
1889 unsigned int flags)
1890{
1891 int ret = 0;
1892 struct super_block *sb = inode->i_sb;
1893 struct quota_info *dqopt = sb_dqopt(sb);
1894
1895 /* Just unsuspend quotas? */
1896 if (flags & DQUOT_SUSPENDED)
1897 return vfs_quota_on_remount(sb, type);
1898 if (!flags)
1899 return 0;
1900 /* Just updating flags needed? */
1901 if (sb_has_quota_loaded(sb, type)) {
1902 mutex_lock(&dqopt->dqonoff_mutex);
1903 /* Now do a reliable test... */
1904 if (!sb_has_quota_loaded(sb, type)) {
1905 mutex_unlock(&dqopt->dqonoff_mutex);
1906 goto load_quota;
1907 }
1908 if (flags & DQUOT_USAGE_ENABLED &&
1909 sb_has_quota_usage_enabled(sb, type)) {
1910 ret = -EBUSY;
1911 goto out_lock;
1912 }
1913 if (flags & DQUOT_LIMITS_ENABLED &&
1914 sb_has_quota_limits_enabled(sb, type)) {
1915 ret = -EBUSY;
1916 goto out_lock;
1917 }
1918 sb_dqopt(sb)->flags |= dquot_state_flag(flags, type);
1919out_lock:
1920 mutex_unlock(&dqopt->dqonoff_mutex);
1921 return ret;
1922 }
1923
1924load_quota:
1925 return vfs_load_quota_inode(inode, type, format_id, flags);
1926}
1927
1928/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929 * This function is used when filesystem needs to initialize quotas
1930 * during mount time.
1931 */
Christoph Hellwig84de8562005-06-23 00:09:16 -07001932int vfs_quota_on_mount(struct super_block *sb, char *qf_name,
1933 int format_id, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934{
Christoph Hellwig84de8562005-06-23 00:09:16 -07001935 struct dentry *dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936 int error;
1937
Christoph Hellwig2fa389c2005-06-23 00:09:16 -07001938 dentry = lookup_one_len(qf_name, sb->s_root, strlen(qf_name));
Christoph Hellwig84de8562005-06-23 00:09:16 -07001939 if (IS_ERR(dentry))
1940 return PTR_ERR(dentry);
1941
Jan Kara154f4842005-11-28 13:44:14 -08001942 if (!dentry->d_inode) {
1943 error = -ENOENT;
1944 goto out;
1945 }
1946
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947 error = security_quota_on(dentry);
Christoph Hellwig84de8562005-06-23 00:09:16 -07001948 if (!error)
Jan Karaf55abc02008-08-20 17:50:32 +02001949 error = vfs_load_quota_inode(dentry->d_inode, type, format_id,
1950 DQUOT_USAGE_ENABLED | DQUOT_LIMITS_ENABLED);
Christoph Hellwig84de8562005-06-23 00:09:16 -07001951
Jan Kara154f4842005-11-28 13:44:14 -08001952out:
Christoph Hellwig84de8562005-06-23 00:09:16 -07001953 dput(dentry);
1954 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955}
1956
Jan Karab85f4b82008-07-25 01:46:50 -07001957/* Wrapper to turn on quotas when remounting rw */
1958int vfs_dq_quota_on_remount(struct super_block *sb)
1959{
1960 int cnt;
1961 int ret = 0, err;
1962
1963 if (!sb->s_qcop || !sb->s_qcop->quota_on)
1964 return -ENOSYS;
1965 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1966 err = sb->s_qcop->quota_on(sb, cnt, 0, NULL, 1);
1967 if (err < 0 && !ret)
1968 ret = err;
1969 }
1970 return ret;
1971}
1972
Jan Kara12095462008-08-20 14:45:12 +02001973static inline qsize_t qbtos(qsize_t blocks)
1974{
1975 return blocks << QIF_DQBLKSIZE_BITS;
1976}
1977
1978static inline qsize_t stoqb(qsize_t space)
1979{
1980 return (space + QIF_DQBLKSIZE - 1) >> QIF_DQBLKSIZE_BITS;
1981}
1982
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983/* Generic routine for getting common part of quota structure */
1984static void do_get_dqblk(struct dquot *dquot, struct if_dqblk *di)
1985{
1986 struct mem_dqblk *dm = &dquot->dq_dqb;
1987
1988 spin_lock(&dq_data_lock);
Jan Kara12095462008-08-20 14:45:12 +02001989 di->dqb_bhardlimit = stoqb(dm->dqb_bhardlimit);
1990 di->dqb_bsoftlimit = stoqb(dm->dqb_bsoftlimit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991 di->dqb_curspace = dm->dqb_curspace;
1992 di->dqb_ihardlimit = dm->dqb_ihardlimit;
1993 di->dqb_isoftlimit = dm->dqb_isoftlimit;
1994 di->dqb_curinodes = dm->dqb_curinodes;
1995 di->dqb_btime = dm->dqb_btime;
1996 di->dqb_itime = dm->dqb_itime;
1997 di->dqb_valid = QIF_ALL;
1998 spin_unlock(&dq_data_lock);
1999}
2000
2001int vfs_get_dqblk(struct super_block *sb, int type, qid_t id, struct if_dqblk *di)
2002{
2003 struct dquot *dquot;
2004
Ingo Molnard3be9152006-03-23 03:00:29 -08002005 mutex_lock(&sb_dqopt(sb)->dqonoff_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006 if (!(dquot = dqget(sb, id, type))) {
Ingo Molnard3be9152006-03-23 03:00:29 -08002007 mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008 return -ESRCH;
2009 }
2010 do_get_dqblk(dquot, di);
2011 dqput(dquot);
Ingo Molnard3be9152006-03-23 03:00:29 -08002012 mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013 return 0;
2014}
2015
2016/* Generic routine for setting common part of quota structure */
Andrew Perepechko338bf9a2008-04-28 02:14:31 -07002017static int do_set_dqblk(struct dquot *dquot, struct if_dqblk *di)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018{
2019 struct mem_dqblk *dm = &dquot->dq_dqb;
2020 int check_blim = 0, check_ilim = 0;
Andrew Perepechko338bf9a2008-04-28 02:14:31 -07002021 struct mem_dqinfo *dqi = &sb_dqopt(dquot->dq_sb)->info[dquot->dq_type];
2022
2023 if ((di->dqb_valid & QIF_BLIMITS &&
2024 (di->dqb_bhardlimit > dqi->dqi_maxblimit ||
2025 di->dqb_bsoftlimit > dqi->dqi_maxblimit)) ||
2026 (di->dqb_valid & QIF_ILIMITS &&
2027 (di->dqb_ihardlimit > dqi->dqi_maxilimit ||
2028 di->dqb_isoftlimit > dqi->dqi_maxilimit)))
2029 return -ERANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030
2031 spin_lock(&dq_data_lock);
2032 if (di->dqb_valid & QIF_SPACE) {
2033 dm->dqb_curspace = di->dqb_curspace;
2034 check_blim = 1;
Jan Kara4d59bce2008-10-02 16:48:10 +02002035 __set_bit(DQ_LASTSET_B + QIF_SPACE_B, &dquot->dq_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036 }
2037 if (di->dqb_valid & QIF_BLIMITS) {
Jan Kara12095462008-08-20 14:45:12 +02002038 dm->dqb_bsoftlimit = qbtos(di->dqb_bsoftlimit);
2039 dm->dqb_bhardlimit = qbtos(di->dqb_bhardlimit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040 check_blim = 1;
Jan Kara4d59bce2008-10-02 16:48:10 +02002041 __set_bit(DQ_LASTSET_B + QIF_BLIMITS_B, &dquot->dq_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042 }
2043 if (di->dqb_valid & QIF_INODES) {
2044 dm->dqb_curinodes = di->dqb_curinodes;
2045 check_ilim = 1;
Jan Kara4d59bce2008-10-02 16:48:10 +02002046 __set_bit(DQ_LASTSET_B + QIF_INODES_B, &dquot->dq_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047 }
2048 if (di->dqb_valid & QIF_ILIMITS) {
2049 dm->dqb_isoftlimit = di->dqb_isoftlimit;
2050 dm->dqb_ihardlimit = di->dqb_ihardlimit;
2051 check_ilim = 1;
Jan Kara4d59bce2008-10-02 16:48:10 +02002052 __set_bit(DQ_LASTSET_B + QIF_ILIMITS_B, &dquot->dq_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053 }
Jan Kara4d59bce2008-10-02 16:48:10 +02002054 if (di->dqb_valid & QIF_BTIME) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055 dm->dqb_btime = di->dqb_btime;
Jan Kara4d59bce2008-10-02 16:48:10 +02002056 __set_bit(DQ_LASTSET_B + QIF_BTIME_B, &dquot->dq_flags);
2057 }
2058 if (di->dqb_valid & QIF_ITIME) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059 dm->dqb_itime = di->dqb_itime;
Jan Kara4d59bce2008-10-02 16:48:10 +02002060 __set_bit(DQ_LASTSET_B + QIF_ITIME_B, &dquot->dq_flags);
2061 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062
2063 if (check_blim) {
Jan Kara12095462008-08-20 14:45:12 +02002064 if (!dm->dqb_bsoftlimit || dm->dqb_curspace < dm->dqb_bsoftlimit) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065 dm->dqb_btime = 0;
2066 clear_bit(DQ_BLKS_B, &dquot->dq_flags);
2067 }
2068 else if (!(di->dqb_valid & QIF_BTIME)) /* Set grace only if user hasn't provided his own... */
Andrew Perepechko338bf9a2008-04-28 02:14:31 -07002069 dm->dqb_btime = get_seconds() + dqi->dqi_bgrace;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070 }
2071 if (check_ilim) {
2072 if (!dm->dqb_isoftlimit || dm->dqb_curinodes < dm->dqb_isoftlimit) {
2073 dm->dqb_itime = 0;
2074 clear_bit(DQ_INODES_B, &dquot->dq_flags);
2075 }
2076 else if (!(di->dqb_valid & QIF_ITIME)) /* Set grace only if user hasn't provided his own... */
Andrew Perepechko338bf9a2008-04-28 02:14:31 -07002077 dm->dqb_itime = get_seconds() + dqi->dqi_igrace;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078 }
2079 if (dm->dqb_bhardlimit || dm->dqb_bsoftlimit || dm->dqb_ihardlimit || dm->dqb_isoftlimit)
2080 clear_bit(DQ_FAKE_B, &dquot->dq_flags);
2081 else
2082 set_bit(DQ_FAKE_B, &dquot->dq_flags);
2083 spin_unlock(&dq_data_lock);
2084 mark_dquot_dirty(dquot);
Andrew Perepechko338bf9a2008-04-28 02:14:31 -07002085
2086 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087}
2088
2089int vfs_set_dqblk(struct super_block *sb, int type, qid_t id, struct if_dqblk *di)
2090{
2091 struct dquot *dquot;
Andrew Perepechko338bf9a2008-04-28 02:14:31 -07002092 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093
Ingo Molnard3be9152006-03-23 03:00:29 -08002094 mutex_lock(&sb_dqopt(sb)->dqonoff_mutex);
Jan Karaf55abc02008-08-20 17:50:32 +02002095 dquot = dqget(sb, id, type);
2096 if (!dquot) {
2097 rc = -ESRCH;
2098 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099 }
Andrew Perepechko338bf9a2008-04-28 02:14:31 -07002100 rc = do_set_dqblk(dquot, di);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101 dqput(dquot);
Jan Karaf55abc02008-08-20 17:50:32 +02002102out:
Ingo Molnard3be9152006-03-23 03:00:29 -08002103 mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
Andrew Perepechko338bf9a2008-04-28 02:14:31 -07002104 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105}
2106
2107/* Generic routine for getting common part of quota file information */
2108int vfs_get_dqinfo(struct super_block *sb, int type, struct if_dqinfo *ii)
2109{
2110 struct mem_dqinfo *mi;
2111
Ingo Molnard3be9152006-03-23 03:00:29 -08002112 mutex_lock(&sb_dqopt(sb)->dqonoff_mutex);
Jan Karaf55abc02008-08-20 17:50:32 +02002113 if (!sb_has_quota_active(sb, type)) {
Ingo Molnard3be9152006-03-23 03:00:29 -08002114 mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115 return -ESRCH;
2116 }
2117 mi = sb_dqopt(sb)->info + type;
2118 spin_lock(&dq_data_lock);
2119 ii->dqi_bgrace = mi->dqi_bgrace;
2120 ii->dqi_igrace = mi->dqi_igrace;
2121 ii->dqi_flags = mi->dqi_flags & DQF_MASK;
2122 ii->dqi_valid = IIF_ALL;
2123 spin_unlock(&dq_data_lock);
Ingo Molnard3be9152006-03-23 03:00:29 -08002124 mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125 return 0;
2126}
2127
2128/* Generic routine for setting common part of quota file information */
2129int vfs_set_dqinfo(struct super_block *sb, int type, struct if_dqinfo *ii)
2130{
2131 struct mem_dqinfo *mi;
Jan Karaf55abc02008-08-20 17:50:32 +02002132 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133
Ingo Molnard3be9152006-03-23 03:00:29 -08002134 mutex_lock(&sb_dqopt(sb)->dqonoff_mutex);
Jan Karaf55abc02008-08-20 17:50:32 +02002135 if (!sb_has_quota_active(sb, type)) {
2136 err = -ESRCH;
2137 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138 }
2139 mi = sb_dqopt(sb)->info + type;
2140 spin_lock(&dq_data_lock);
2141 if (ii->dqi_valid & IIF_BGRACE)
2142 mi->dqi_bgrace = ii->dqi_bgrace;
2143 if (ii->dqi_valid & IIF_IGRACE)
2144 mi->dqi_igrace = ii->dqi_igrace;
2145 if (ii->dqi_valid & IIF_FLAGS)
2146 mi->dqi_flags = (mi->dqi_flags & ~DQF_MASK) | (ii->dqi_flags & DQF_MASK);
2147 spin_unlock(&dq_data_lock);
2148 mark_info_dirty(sb, type);
2149 /* Force write to disk */
2150 sb->dq_op->write_info(sb, type);
Jan Karaf55abc02008-08-20 17:50:32 +02002151out:
Ingo Molnard3be9152006-03-23 03:00:29 -08002152 mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
Jan Karaf55abc02008-08-20 17:50:32 +02002153 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154}
2155
2156struct quotactl_ops vfs_quotactl_ops = {
2157 .quota_on = vfs_quota_on,
2158 .quota_off = vfs_quota_off,
2159 .quota_sync = vfs_quota_sync,
2160 .get_info = vfs_get_dqinfo,
2161 .set_info = vfs_set_dqinfo,
2162 .get_dqblk = vfs_get_dqblk,
2163 .set_dqblk = vfs_set_dqblk
2164};
2165
2166static ctl_table fs_dqstats_table[] = {
2167 {
2168 .ctl_name = FS_DQ_LOOKUPS,
2169 .procname = "lookups",
2170 .data = &dqstats.lookups,
2171 .maxlen = sizeof(int),
2172 .mode = 0444,
2173 .proc_handler = &proc_dointvec,
2174 },
2175 {
2176 .ctl_name = FS_DQ_DROPS,
2177 .procname = "drops",
2178 .data = &dqstats.drops,
2179 .maxlen = sizeof(int),
2180 .mode = 0444,
2181 .proc_handler = &proc_dointvec,
2182 },
2183 {
2184 .ctl_name = FS_DQ_READS,
2185 .procname = "reads",
2186 .data = &dqstats.reads,
2187 .maxlen = sizeof(int),
2188 .mode = 0444,
2189 .proc_handler = &proc_dointvec,
2190 },
2191 {
2192 .ctl_name = FS_DQ_WRITES,
2193 .procname = "writes",
2194 .data = &dqstats.writes,
2195 .maxlen = sizeof(int),
2196 .mode = 0444,
2197 .proc_handler = &proc_dointvec,
2198 },
2199 {
2200 .ctl_name = FS_DQ_CACHE_HITS,
2201 .procname = "cache_hits",
2202 .data = &dqstats.cache_hits,
2203 .maxlen = sizeof(int),
2204 .mode = 0444,
2205 .proc_handler = &proc_dointvec,
2206 },
2207 {
2208 .ctl_name = FS_DQ_ALLOCATED,
2209 .procname = "allocated_dquots",
2210 .data = &dqstats.allocated_dquots,
2211 .maxlen = sizeof(int),
2212 .mode = 0444,
2213 .proc_handler = &proc_dointvec,
2214 },
2215 {
2216 .ctl_name = FS_DQ_FREE,
2217 .procname = "free_dquots",
2218 .data = &dqstats.free_dquots,
2219 .maxlen = sizeof(int),
2220 .mode = 0444,
2221 .proc_handler = &proc_dointvec,
2222 },
2223 {
2224 .ctl_name = FS_DQ_SYNCS,
2225 .procname = "syncs",
2226 .data = &dqstats.syncs,
2227 .maxlen = sizeof(int),
2228 .mode = 0444,
2229 .proc_handler = &proc_dointvec,
2230 },
Jan Kara8e893462007-10-16 23:29:31 -07002231#ifdef CONFIG_PRINT_QUOTA_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232 {
2233 .ctl_name = FS_DQ_WARNINGS,
2234 .procname = "warnings",
2235 .data = &flag_print_warnings,
2236 .maxlen = sizeof(int),
2237 .mode = 0644,
2238 .proc_handler = &proc_dointvec,
2239 },
Jan Kara8e893462007-10-16 23:29:31 -07002240#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241 { .ctl_name = 0 },
2242};
2243
2244static ctl_table fs_table[] = {
2245 {
2246 .ctl_name = FS_DQSTATS,
2247 .procname = "quota",
2248 .mode = 0555,
2249 .child = fs_dqstats_table,
2250 },
2251 { .ctl_name = 0 },
2252};
2253
2254static ctl_table sys_table[] = {
2255 {
2256 .ctl_name = CTL_FS,
2257 .procname = "fs",
2258 .mode = 0555,
2259 .child = fs_table,
2260 },
2261 { .ctl_name = 0 },
2262};
2263
2264static int __init dquot_init(void)
2265{
2266 int i;
2267 unsigned long nr_hash, order;
2268
2269 printk(KERN_NOTICE "VFS: Disk quotas %s\n", __DQUOT_VERSION__);
2270
Eric W. Biederman0b4d4142007-02-14 00:34:09 -08002271 register_sysctl_table(sys_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272
Paul Mundt20c2df82007-07-20 10:11:58 +09002273 dquot_cachep = kmem_cache_create("dquot",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274 sizeof(struct dquot), sizeof(unsigned long) * 4,
Paul Jacksonfffb60f2006-03-24 03:16:06 -08002275 (SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT|
2276 SLAB_MEM_SPREAD|SLAB_PANIC),
Paul Mundt20c2df82007-07-20 10:11:58 +09002277 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278
2279 order = 0;
2280 dquot_hash = (struct hlist_head *)__get_free_pages(GFP_ATOMIC, order);
2281 if (!dquot_hash)
2282 panic("Cannot create dquot hash table");
2283
2284 /* Find power-of-two hlist_heads which can fit into allocation */
2285 nr_hash = (1UL << order) * PAGE_SIZE / sizeof(struct hlist_head);
2286 dq_hash_bits = 0;
2287 do {
2288 dq_hash_bits++;
2289 } while (nr_hash >> dq_hash_bits);
2290 dq_hash_bits--;
2291
2292 nr_hash = 1UL << dq_hash_bits;
2293 dq_hash_mask = nr_hash - 1;
2294 for (i = 0; i < nr_hash; i++)
2295 INIT_HLIST_HEAD(dquot_hash + i);
2296
2297 printk("Dquot-cache hash table entries: %ld (order %ld, %ld bytes)\n",
2298 nr_hash, order, (PAGE_SIZE << order));
2299
Rusty Russell8e1f9362007-07-17 04:03:17 -07002300 register_shrinker(&dqcache_shrinker);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301
Jan Kara8e893462007-10-16 23:29:31 -07002302#ifdef CONFIG_QUOTA_NETLINK_INTERFACE
2303 if (genl_register_family(&quota_genl_family) != 0)
2304 printk(KERN_ERR "VFS: Failed to create quota netlink interface.\n");
2305#endif
2306
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307 return 0;
2308}
2309module_init(dquot_init);
2310
2311EXPORT_SYMBOL(register_quota_format);
2312EXPORT_SYMBOL(unregister_quota_format);
2313EXPORT_SYMBOL(dqstats);
2314EXPORT_SYMBOL(dq_data_lock);
Jan Karaf55abc02008-08-20 17:50:32 +02002315EXPORT_SYMBOL(vfs_quota_enable);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316EXPORT_SYMBOL(vfs_quota_on);
Al Viro77e69da2008-08-01 04:29:18 -04002317EXPORT_SYMBOL(vfs_quota_on_path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318EXPORT_SYMBOL(vfs_quota_on_mount);
Jan Karaf55abc02008-08-20 17:50:32 +02002319EXPORT_SYMBOL(vfs_quota_disable);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320EXPORT_SYMBOL(vfs_quota_off);
2321EXPORT_SYMBOL(vfs_quota_sync);
2322EXPORT_SYMBOL(vfs_get_dqinfo);
2323EXPORT_SYMBOL(vfs_set_dqinfo);
2324EXPORT_SYMBOL(vfs_get_dqblk);
2325EXPORT_SYMBOL(vfs_set_dqblk);
2326EXPORT_SYMBOL(dquot_commit);
2327EXPORT_SYMBOL(dquot_commit_info);
2328EXPORT_SYMBOL(dquot_acquire);
2329EXPORT_SYMBOL(dquot_release);
2330EXPORT_SYMBOL(dquot_mark_dquot_dirty);
2331EXPORT_SYMBOL(dquot_initialize);
2332EXPORT_SYMBOL(dquot_drop);
Jan Kara3d9ea252008-10-10 16:12:23 +02002333EXPORT_SYMBOL(dquot_drop_locked);
Jan Karab85f4b82008-07-25 01:46:50 -07002334EXPORT_SYMBOL(vfs_dq_drop);
Jan Kara3d9ea252008-10-10 16:12:23 +02002335EXPORT_SYMBOL(dqget);
2336EXPORT_SYMBOL(dqput);
2337EXPORT_SYMBOL(dquot_is_cached);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338EXPORT_SYMBOL(dquot_alloc_space);
2339EXPORT_SYMBOL(dquot_alloc_inode);
2340EXPORT_SYMBOL(dquot_free_space);
2341EXPORT_SYMBOL(dquot_free_inode);
2342EXPORT_SYMBOL(dquot_transfer);
Jan Karab85f4b82008-07-25 01:46:50 -07002343EXPORT_SYMBOL(vfs_dq_transfer);
2344EXPORT_SYMBOL(vfs_dq_quota_on_remount);