blob: f6eaf0d8fd6a5fb64a4d28a85a76d8fcd3fc2221 [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.. */
Linus Torvalds1da177e2005-04-16 15:20:36 -070080
81#include <asm/uaccess.h>
82
83#define __DQUOT_PARANOIA
84
85/*
Jan Karacc334122009-01-12 17:23:05 +010086 * There are three quota SMP locks. dq_list_lock protects all lists with quotas
87 * and quota formats, dqstats structure containing statistics about the lists
88 * dq_data_lock protects data from dq_dqb and also mem_dqinfo structures and
89 * also guards consistency of dquot->dq_dqb with inode->i_blocks, i_bytes.
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 * i_blocks and i_bytes updates itself are guarded by i_lock acquired directly
Jan Karacc334122009-01-12 17:23:05 +010091 * in inode_add_bytes() and inode_sub_bytes(). dq_state_lock protects
92 * modifications of quota state (on quotaon and quotaoff) and readers who care
93 * about latest values take it as well.
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 *
Jan Karacc334122009-01-12 17:23:05 +010095 * The spinlock ordering is hence: dq_data_lock > dq_list_lock > i_lock,
96 * dq_list_lock > dq_state_lock
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 *
98 * Note that some things (eg. sb pointer, type, id) doesn't change during
99 * the life of the dquot structure and so needn't to be protected by a lock
100 *
101 * Any operation working on dquots via inode pointers must hold dqptr_sem. If
102 * operation is just reading pointers from inode (or not using them at all) the
Jan Kara26245c92010-01-06 17:20:35 +0100103 * read lock is enough. If pointers are altered function must hold write lock.
104 * Special care needs to be taken about S_NOQUOTA inode flag (marking that
105 * inode is a quota file). Functions adding pointers from inode to dquots have
106 * to check this flag under dqptr_sem and then (if S_NOQUOTA is not set) they
107 * have to do all pointer modifications before dropping dqptr_sem. This makes
108 * sure they cannot race with quotaon which first sets S_NOQUOTA flag and
109 * then drops all pointers to dquots from an inode.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 *
Ingo Molnard3be9152006-03-23 03:00:29 -0800111 * Each dquot has its dq_lock mutex. Locked dquots might not be referenced
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 * from inodes (dquot_alloc_space() and such don't check the dq_lock).
113 * Currently dquot is locked only when it is being read to memory (or space for
114 * it is being allocated) on the first dqget() and when it is being released on
115 * the last dqput(). The allocation and release oparations are serialized by
116 * the dq_lock and by checking the use count in dquot_release(). Write
117 * operations on dquots don't hold dq_lock as they copy data under dq_data_lock
118 * spinlock to internal buffers before writing.
119 *
120 * Lock ordering (including related VFS locks) is the following:
Ingo Molnard3be9152006-03-23 03:00:29 -0800121 * i_mutex > dqonoff_sem > journal_lock > dqptr_sem > dquot->dq_lock >
122 * dqio_mutex
Jan Karacc334122009-01-12 17:23:05 +0100123 * The lock ordering of dqptr_sem imposed by quota code is only dqonoff_sem >
124 * dqptr_sem. But filesystem has to count with the fact that functions such as
125 * dquot_alloc_space() acquire dqptr_sem and they usually have to be called
126 * from inside a transaction to keep filesystem consistency after a crash. Also
127 * filesystems usually want to do some IO on dquot from ->mark_dirty which is
128 * called with dqptr_sem held.
Ingo Molnard3be9152006-03-23 03:00:29 -0800129 * i_mutex on quota files is special (it's below dqio_mutex)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 */
131
Jan Karac5166102009-01-26 15:32:46 +0100132static __cacheline_aligned_in_smp DEFINE_SPINLOCK(dq_list_lock);
133static __cacheline_aligned_in_smp DEFINE_SPINLOCK(dq_state_lock);
134__cacheline_aligned_in_smp DEFINE_SPINLOCK(dq_data_lock);
Mingming Cao08d03502009-01-14 16:19:32 +0100135EXPORT_SYMBOL(dq_data_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136
137static char *quotatypes[] = INITQFNAMES;
138static struct quota_format_type *quota_formats; /* List of registered formats */
139static struct quota_module_name module_names[] = INIT_QUOTA_MODULE_NAMES;
140
141/* SLAB cache for dquot structures */
Christoph Lametere18b8902006-12-06 20:33:20 -0800142static struct kmem_cache *dquot_cachep;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143
144int register_quota_format(struct quota_format_type *fmt)
145{
146 spin_lock(&dq_list_lock);
147 fmt->qf_next = quota_formats;
148 quota_formats = fmt;
149 spin_unlock(&dq_list_lock);
150 return 0;
151}
Mingming Cao08d03502009-01-14 16:19:32 +0100152EXPORT_SYMBOL(register_quota_format);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153
154void unregister_quota_format(struct quota_format_type *fmt)
155{
156 struct quota_format_type **actqf;
157
158 spin_lock(&dq_list_lock);
Jan Kara268157b2009-01-27 15:47:22 +0100159 for (actqf = &quota_formats; *actqf && *actqf != fmt;
160 actqf = &(*actqf)->qf_next)
161 ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 if (*actqf)
163 *actqf = (*actqf)->qf_next;
164 spin_unlock(&dq_list_lock);
165}
Mingming Cao08d03502009-01-14 16:19:32 +0100166EXPORT_SYMBOL(unregister_quota_format);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167
168static struct quota_format_type *find_quota_format(int id)
169{
170 struct quota_format_type *actqf;
171
172 spin_lock(&dq_list_lock);
Jan Kara268157b2009-01-27 15:47:22 +0100173 for (actqf = quota_formats; actqf && actqf->qf_fmt_id != id;
174 actqf = actqf->qf_next)
175 ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 if (!actqf || !try_module_get(actqf->qf_owner)) {
177 int qm;
178
179 spin_unlock(&dq_list_lock);
180
Jan Kara268157b2009-01-27 15:47:22 +0100181 for (qm = 0; module_names[qm].qm_fmt_id &&
182 module_names[qm].qm_fmt_id != id; qm++)
183 ;
184 if (!module_names[qm].qm_fmt_id ||
185 request_module(module_names[qm].qm_mod_name))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 return NULL;
187
188 spin_lock(&dq_list_lock);
Jan Kara268157b2009-01-27 15:47:22 +0100189 for (actqf = quota_formats; actqf && actqf->qf_fmt_id != id;
190 actqf = actqf->qf_next)
191 ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 if (actqf && !try_module_get(actqf->qf_owner))
193 actqf = NULL;
194 }
195 spin_unlock(&dq_list_lock);
196 return actqf;
197}
198
199static void put_quota_format(struct quota_format_type *fmt)
200{
201 module_put(fmt->qf_owner);
202}
203
204/*
205 * Dquot List Management:
206 * The quota code uses three lists for dquot management: the inuse_list,
207 * free_dquots, and dquot_hash[] array. A single dquot structure may be
208 * on all three lists, depending on its current state.
209 *
210 * All dquots are placed to the end of inuse_list when first created, and this
211 * list is used for invalidate operation, which must look at every dquot.
212 *
213 * Unused dquots (dq_count == 0) are added to the free_dquots list when freed,
214 * and this list is searched whenever we need an available dquot. Dquots are
215 * removed from the list as soon as they are used again, and
216 * dqstats.free_dquots gives the number of dquots on the list. When
217 * dquot is invalidated it's completely released from memory.
218 *
219 * Dquots with a specific identity (device, type and id) are placed on
220 * one of the dquot_hash[] hash chains. The provides an efficient search
221 * mechanism to locate a specific dquot.
222 */
223
224static LIST_HEAD(inuse_list);
225static LIST_HEAD(free_dquots);
226static unsigned int dq_hash_bits, dq_hash_mask;
227static struct hlist_head *dquot_hash;
228
229struct dqstats dqstats;
Mingming Cao08d03502009-01-14 16:19:32 +0100230EXPORT_SYMBOL(dqstats);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232static inline unsigned int
233hashfn(const struct super_block *sb, unsigned int id, int type)
234{
235 unsigned long tmp;
236
237 tmp = (((unsigned long)sb>>L1_CACHE_SHIFT) ^ id) * (MAXQUOTAS - type);
238 return (tmp + (tmp >> dq_hash_bits)) & dq_hash_mask;
239}
240
241/*
242 * Following list functions expect dq_list_lock to be held
243 */
244static inline void insert_dquot_hash(struct dquot *dquot)
245{
Jan Kara268157b2009-01-27 15:47:22 +0100246 struct hlist_head *head;
247 head = dquot_hash + hashfn(dquot->dq_sb, dquot->dq_id, dquot->dq_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 hlist_add_head(&dquot->dq_hash, head);
249}
250
251static inline void remove_dquot_hash(struct dquot *dquot)
252{
253 hlist_del_init(&dquot->dq_hash);
254}
255
Jan Kara7a2435d2009-01-27 01:47:11 +0100256static struct dquot *find_dquot(unsigned int hashent, struct super_block *sb,
257 unsigned int id, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258{
259 struct hlist_node *node;
260 struct dquot *dquot;
261
262 hlist_for_each (node, dquot_hash+hashent) {
263 dquot = hlist_entry(node, struct dquot, dq_hash);
Jan Kara268157b2009-01-27 15:47:22 +0100264 if (dquot->dq_sb == sb && dquot->dq_id == id &&
265 dquot->dq_type == type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 return dquot;
267 }
Jan Karadd6f3c62009-01-26 16:01:43 +0100268 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269}
270
271/* Add a dquot to the tail of the free list */
272static inline void put_dquot_last(struct dquot *dquot)
273{
Akinobu Mita8e130592006-06-26 00:24:37 -0700274 list_add_tail(&dquot->dq_free, &free_dquots);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 dqstats.free_dquots++;
276}
277
278static inline void remove_free_dquot(struct dquot *dquot)
279{
280 if (list_empty(&dquot->dq_free))
281 return;
282 list_del_init(&dquot->dq_free);
283 dqstats.free_dquots--;
284}
285
286static inline void put_inuse(struct dquot *dquot)
287{
288 /* We add to the back of inuse list so we don't have to restart
289 * when traversing this list and we block */
Akinobu Mita8e130592006-06-26 00:24:37 -0700290 list_add_tail(&dquot->dq_inuse, &inuse_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 dqstats.allocated_dquots++;
292}
293
294static inline void remove_inuse(struct dquot *dquot)
295{
296 dqstats.allocated_dquots--;
297 list_del(&dquot->dq_inuse);
298}
299/*
300 * End of list functions needing dq_list_lock
301 */
302
303static void wait_on_dquot(struct dquot *dquot)
304{
Ingo Molnard3be9152006-03-23 03:00:29 -0800305 mutex_lock(&dquot->dq_lock);
306 mutex_unlock(&dquot->dq_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307}
308
Jan Kara03f6e922008-04-28 02:14:32 -0700309static inline int dquot_dirty(struct dquot *dquot)
310{
311 return test_bit(DQ_MOD_B, &dquot->dq_flags);
312}
313
314static inline int mark_dquot_dirty(struct dquot *dquot)
315{
316 return dquot->dq_sb->dq_op->mark_dirty(dquot);
317}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318
319int dquot_mark_dquot_dirty(struct dquot *dquot)
320{
321 spin_lock(&dq_list_lock);
322 if (!test_and_set_bit(DQ_MOD_B, &dquot->dq_flags))
323 list_add(&dquot->dq_dirty, &sb_dqopt(dquot->dq_sb)->
324 info[dquot->dq_type].dqi_dirty_list);
325 spin_unlock(&dq_list_lock);
326 return 0;
327}
Mingming Cao08d03502009-01-14 16:19:32 +0100328EXPORT_SYMBOL(dquot_mark_dquot_dirty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329
Dmitry Monakhovdc52dd32009-12-14 15:21:15 +0300330/* Dirtify all the dquots - this can block when journalling */
331static inline int mark_all_dquot_dirty(struct dquot * const *dquot)
332{
333 int ret, err, cnt;
334
335 ret = err = 0;
336 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
337 if (dquot[cnt])
338 /* Even in case of error we have to continue */
339 ret = mark_dquot_dirty(dquot[cnt]);
340 if (!err)
341 err = ret;
342 }
343 return err;
344}
345
346static inline void dqput_all(struct dquot **dquot)
347{
348 unsigned int cnt;
349
350 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
351 dqput(dquot[cnt]);
352}
353
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354/* This function needs dq_list_lock */
355static inline int clear_dquot_dirty(struct dquot *dquot)
356{
357 if (!test_and_clear_bit(DQ_MOD_B, &dquot->dq_flags))
358 return 0;
359 list_del_init(&dquot->dq_dirty);
360 return 1;
361}
362
363void mark_info_dirty(struct super_block *sb, int type)
364{
365 set_bit(DQF_INFO_DIRTY_B, &sb_dqopt(sb)->info[type].dqi_flags);
366}
367EXPORT_SYMBOL(mark_info_dirty);
368
369/*
370 * Read dquot from disk and alloc space for it
371 */
372
373int dquot_acquire(struct dquot *dquot)
374{
375 int ret = 0, ret2 = 0;
376 struct quota_info *dqopt = sb_dqopt(dquot->dq_sb);
377
Ingo Molnard3be9152006-03-23 03:00:29 -0800378 mutex_lock(&dquot->dq_lock);
379 mutex_lock(&dqopt->dqio_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 if (!test_bit(DQ_READ_B, &dquot->dq_flags))
381 ret = dqopt->ops[dquot->dq_type]->read_dqblk(dquot);
382 if (ret < 0)
383 goto out_iolock;
384 set_bit(DQ_READ_B, &dquot->dq_flags);
385 /* Instantiate dquot if needed */
386 if (!test_bit(DQ_ACTIVE_B, &dquot->dq_flags) && !dquot->dq_off) {
387 ret = dqopt->ops[dquot->dq_type]->commit_dqblk(dquot);
388 /* Write the info if needed */
Jan Kara268157b2009-01-27 15:47:22 +0100389 if (info_dirty(&dqopt->info[dquot->dq_type])) {
390 ret2 = dqopt->ops[dquot->dq_type]->write_file_info(
391 dquot->dq_sb, dquot->dq_type);
392 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 if (ret < 0)
394 goto out_iolock;
395 if (ret2 < 0) {
396 ret = ret2;
397 goto out_iolock;
398 }
399 }
400 set_bit(DQ_ACTIVE_B, &dquot->dq_flags);
401out_iolock:
Ingo Molnard3be9152006-03-23 03:00:29 -0800402 mutex_unlock(&dqopt->dqio_mutex);
403 mutex_unlock(&dquot->dq_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 return ret;
405}
Mingming Cao08d03502009-01-14 16:19:32 +0100406EXPORT_SYMBOL(dquot_acquire);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407
408/*
409 * Write dquot to disk
410 */
411int dquot_commit(struct dquot *dquot)
412{
413 int ret = 0, ret2 = 0;
414 struct quota_info *dqopt = sb_dqopt(dquot->dq_sb);
415
Ingo Molnard3be9152006-03-23 03:00:29 -0800416 mutex_lock(&dqopt->dqio_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 spin_lock(&dq_list_lock);
418 if (!clear_dquot_dirty(dquot)) {
419 spin_unlock(&dq_list_lock);
420 goto out_sem;
421 }
422 spin_unlock(&dq_list_lock);
423 /* Inactive dquot can be only if there was error during read/init
424 * => we have better not writing it */
425 if (test_bit(DQ_ACTIVE_B, &dquot->dq_flags)) {
426 ret = dqopt->ops[dquot->dq_type]->commit_dqblk(dquot);
Jan Kara268157b2009-01-27 15:47:22 +0100427 if (info_dirty(&dqopt->info[dquot->dq_type])) {
428 ret2 = dqopt->ops[dquot->dq_type]->write_file_info(
429 dquot->dq_sb, dquot->dq_type);
430 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 if (ret >= 0)
432 ret = ret2;
433 }
434out_sem:
Ingo Molnard3be9152006-03-23 03:00:29 -0800435 mutex_unlock(&dqopt->dqio_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 return ret;
437}
Mingming Cao08d03502009-01-14 16:19:32 +0100438EXPORT_SYMBOL(dquot_commit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439
440/*
441 * Release dquot
442 */
443int dquot_release(struct dquot *dquot)
444{
445 int ret = 0, ret2 = 0;
446 struct quota_info *dqopt = sb_dqopt(dquot->dq_sb);
447
Ingo Molnard3be9152006-03-23 03:00:29 -0800448 mutex_lock(&dquot->dq_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 /* Check whether we are not racing with some other dqget() */
450 if (atomic_read(&dquot->dq_count) > 1)
451 goto out_dqlock;
Ingo Molnard3be9152006-03-23 03:00:29 -0800452 mutex_lock(&dqopt->dqio_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 if (dqopt->ops[dquot->dq_type]->release_dqblk) {
454 ret = dqopt->ops[dquot->dq_type]->release_dqblk(dquot);
455 /* Write the info */
Jan Kara268157b2009-01-27 15:47:22 +0100456 if (info_dirty(&dqopt->info[dquot->dq_type])) {
457 ret2 = dqopt->ops[dquot->dq_type]->write_file_info(
458 dquot->dq_sb, dquot->dq_type);
459 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 if (ret >= 0)
461 ret = ret2;
462 }
463 clear_bit(DQ_ACTIVE_B, &dquot->dq_flags);
Ingo Molnard3be9152006-03-23 03:00:29 -0800464 mutex_unlock(&dqopt->dqio_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465out_dqlock:
Ingo Molnard3be9152006-03-23 03:00:29 -0800466 mutex_unlock(&dquot->dq_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 return ret;
468}
Mingming Cao08d03502009-01-14 16:19:32 +0100469EXPORT_SYMBOL(dquot_release);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470
Jan Kara7d9056b2008-11-25 15:31:32 +0100471void dquot_destroy(struct dquot *dquot)
Jan Kara74f783a2008-08-19 14:51:22 +0200472{
473 kmem_cache_free(dquot_cachep, dquot);
474}
Jan Kara7d9056b2008-11-25 15:31:32 +0100475EXPORT_SYMBOL(dquot_destroy);
Jan Kara74f783a2008-08-19 14:51:22 +0200476
477static inline void do_destroy_dquot(struct dquot *dquot)
478{
479 dquot->dq_sb->dq_op->destroy_dquot(dquot);
480}
481
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482/* Invalidate all dquots on the list. Note that this function is called after
483 * quota is disabled and pointers from inodes removed so there cannot be new
Jan Kara6362e4d2006-03-23 03:00:17 -0800484 * quota users. There can still be some users of quotas due to inodes being
485 * just deleted or pruned by prune_icache() (those are not attached to any
Jan Karacc334122009-01-12 17:23:05 +0100486 * list) or parallel quotactl call. We have to wait for such users.
Jan Kara6362e4d2006-03-23 03:00:17 -0800487 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488static void invalidate_dquots(struct super_block *sb, int type)
489{
Domen Puncerc33ed272005-06-25 14:59:36 -0700490 struct dquot *dquot, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491
Jan Kara6362e4d2006-03-23 03:00:17 -0800492restart:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 spin_lock(&dq_list_lock);
Domen Puncerc33ed272005-06-25 14:59:36 -0700494 list_for_each_entry_safe(dquot, tmp, &inuse_list, dq_inuse) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 if (dquot->dq_sb != sb)
496 continue;
497 if (dquot->dq_type != type)
498 continue;
Jan Kara6362e4d2006-03-23 03:00:17 -0800499 /* Wait for dquot users */
500 if (atomic_read(&dquot->dq_count)) {
501 DEFINE_WAIT(wait);
502
503 atomic_inc(&dquot->dq_count);
504 prepare_to_wait(&dquot->dq_wait_unused, &wait,
505 TASK_UNINTERRUPTIBLE);
506 spin_unlock(&dq_list_lock);
507 /* Once dqput() wakes us up, we know it's time to free
508 * the dquot.
509 * IMPORTANT: we rely on the fact that there is always
510 * at most one process waiting for dquot to free.
511 * Otherwise dq_count would be > 1 and we would never
512 * wake up.
513 */
514 if (atomic_read(&dquot->dq_count) > 1)
515 schedule();
516 finish_wait(&dquot->dq_wait_unused, &wait);
517 dqput(dquot);
518 /* At this moment dquot() need not exist (it could be
519 * reclaimed by prune_dqcache(). Hence we must
520 * restart. */
521 goto restart;
522 }
523 /*
524 * Quota now has no users and it has been written on last
525 * dqput()
526 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 remove_dquot_hash(dquot);
528 remove_free_dquot(dquot);
529 remove_inuse(dquot);
Jan Kara74f783a2008-08-19 14:51:22 +0200530 do_destroy_dquot(dquot);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 }
532 spin_unlock(&dq_list_lock);
533}
534
Jan Kara12c77522008-10-20 17:05:00 +0200535/* Call callback for every active dquot on given filesystem */
536int dquot_scan_active(struct super_block *sb,
537 int (*fn)(struct dquot *dquot, unsigned long priv),
538 unsigned long priv)
539{
540 struct dquot *dquot, *old_dquot = NULL;
541 int ret = 0;
542
543 mutex_lock(&sb_dqopt(sb)->dqonoff_mutex);
544 spin_lock(&dq_list_lock);
545 list_for_each_entry(dquot, &inuse_list, dq_inuse) {
546 if (!test_bit(DQ_ACTIVE_B, &dquot->dq_flags))
547 continue;
548 if (dquot->dq_sb != sb)
549 continue;
550 /* Now we have active dquot so we can just increase use count */
551 atomic_inc(&dquot->dq_count);
552 dqstats.lookups++;
553 spin_unlock(&dq_list_lock);
554 dqput(old_dquot);
555 old_dquot = dquot;
556 ret = fn(dquot, priv);
557 if (ret < 0)
558 goto out;
559 spin_lock(&dq_list_lock);
560 /* We are safe to continue now because our dquot could not
561 * be moved out of the inuse list while we hold the reference */
562 }
563 spin_unlock(&dq_list_lock);
564out:
565 dqput(old_dquot);
566 mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
567 return ret;
568}
Mingming Cao08d03502009-01-14 16:19:32 +0100569EXPORT_SYMBOL(dquot_scan_active);
Jan Kara12c77522008-10-20 17:05:00 +0200570
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571int vfs_quota_sync(struct super_block *sb, int type)
572{
573 struct list_head *dirty;
574 struct dquot *dquot;
575 struct quota_info *dqopt = sb_dqopt(sb);
576 int cnt;
577
Ingo Molnard3be9152006-03-23 03:00:29 -0800578 mutex_lock(&dqopt->dqonoff_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
580 if (type != -1 && cnt != type)
581 continue;
Jan Karaf55abc02008-08-20 17:50:32 +0200582 if (!sb_has_quota_active(sb, cnt))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 continue;
584 spin_lock(&dq_list_lock);
585 dirty = &dqopt->info[cnt].dqi_dirty_list;
586 while (!list_empty(dirty)) {
Jan Kara268157b2009-01-27 15:47:22 +0100587 dquot = list_first_entry(dirty, struct dquot,
588 dq_dirty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 /* Dirty and inactive can be only bad dquot... */
590 if (!test_bit(DQ_ACTIVE_B, &dquot->dq_flags)) {
591 clear_dquot_dirty(dquot);
592 continue;
593 }
594 /* Now we have active dquot from which someone is
595 * holding reference so we can safely just increase
596 * use count */
597 atomic_inc(&dquot->dq_count);
598 dqstats.lookups++;
599 spin_unlock(&dq_list_lock);
600 sb->dq_op->write_dquot(dquot);
601 dqput(dquot);
602 spin_lock(&dq_list_lock);
603 }
604 spin_unlock(&dq_list_lock);
605 }
606
607 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
Jan Karaf55abc02008-08-20 17:50:32 +0200608 if ((cnt == type || type == -1) && sb_has_quota_active(sb, cnt)
609 && info_dirty(&dqopt->info[cnt]))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 sb->dq_op->write_info(sb, cnt);
611 spin_lock(&dq_list_lock);
612 dqstats.syncs++;
613 spin_unlock(&dq_list_lock);
Ingo Molnard3be9152006-03-23 03:00:29 -0800614 mutex_unlock(&dqopt->dqonoff_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615
616 return 0;
617}
Mingming Cao08d03502009-01-14 16:19:32 +0100618EXPORT_SYMBOL(vfs_quota_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619
620/* Free unused dquots from cache */
621static void prune_dqcache(int count)
622{
623 struct list_head *head;
624 struct dquot *dquot;
625
626 head = free_dquots.prev;
627 while (head != &free_dquots && count) {
628 dquot = list_entry(head, struct dquot, dq_free);
629 remove_dquot_hash(dquot);
630 remove_free_dquot(dquot);
631 remove_inuse(dquot);
Jan Kara74f783a2008-08-19 14:51:22 +0200632 do_destroy_dquot(dquot);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 count--;
634 head = free_dquots.prev;
635 }
636}
637
638/*
639 * This is called from kswapd when we think we need some
640 * more memory
641 */
642
Al Viro27496a82005-10-21 03:20:48 -0400643static int shrink_dqcache_memory(int nr, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644{
645 if (nr) {
646 spin_lock(&dq_list_lock);
647 prune_dqcache(nr);
648 spin_unlock(&dq_list_lock);
649 }
650 return (dqstats.free_dquots / 100) * sysctl_vfs_cache_pressure;
651}
652
Rusty Russell8e1f9362007-07-17 04:03:17 -0700653static struct shrinker dqcache_shrinker = {
654 .shrink = shrink_dqcache_memory,
655 .seeks = DEFAULT_SEEKS,
656};
657
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658/*
659 * Put reference to dquot
660 * NOTE: If you change this function please check whether dqput_blocks() works right...
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 */
Jan Kara3d9ea252008-10-10 16:12:23 +0200662void dqput(struct dquot *dquot)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663{
Jan Karab48d3802008-07-25 01:46:49 -0700664 int ret;
665
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 if (!dquot)
667 return;
668#ifdef __DQUOT_PARANOIA
669 if (!atomic_read(&dquot->dq_count)) {
670 printk("VFS: dqput: trying to free free dquot\n");
671 printk("VFS: device %s, dquot of %s %d\n",
672 dquot->dq_sb->s_id,
673 quotatypes[dquot->dq_type],
674 dquot->dq_id);
675 BUG();
676 }
677#endif
678
679 spin_lock(&dq_list_lock);
680 dqstats.drops++;
681 spin_unlock(&dq_list_lock);
682we_slept:
683 spin_lock(&dq_list_lock);
684 if (atomic_read(&dquot->dq_count) > 1) {
685 /* We have more than one user... nothing to do */
686 atomic_dec(&dquot->dq_count);
Jan Kara6362e4d2006-03-23 03:00:17 -0800687 /* Releasing dquot during quotaoff phase? */
Jan Karaf55abc02008-08-20 17:50:32 +0200688 if (!sb_has_quota_active(dquot->dq_sb, dquot->dq_type) &&
Jan Kara6362e4d2006-03-23 03:00:17 -0800689 atomic_read(&dquot->dq_count) == 1)
690 wake_up(&dquot->dq_wait_unused);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 spin_unlock(&dq_list_lock);
692 return;
693 }
694 /* Need to release dquot? */
695 if (test_bit(DQ_ACTIVE_B, &dquot->dq_flags) && dquot_dirty(dquot)) {
696 spin_unlock(&dq_list_lock);
697 /* Commit dquot before releasing */
Jan Karab48d3802008-07-25 01:46:49 -0700698 ret = dquot->dq_sb->dq_op->write_dquot(dquot);
699 if (ret < 0) {
700 printk(KERN_ERR "VFS: cannot write quota structure on "
701 "device %s (error %d). Quota may get out of "
702 "sync!\n", dquot->dq_sb->s_id, ret);
703 /*
704 * We clear dirty bit anyway, so that we avoid
705 * infinite loop here
706 */
707 spin_lock(&dq_list_lock);
708 clear_dquot_dirty(dquot);
709 spin_unlock(&dq_list_lock);
710 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 goto we_slept;
712 }
713 /* Clear flag in case dquot was inactive (something bad happened) */
714 clear_dquot_dirty(dquot);
715 if (test_bit(DQ_ACTIVE_B, &dquot->dq_flags)) {
716 spin_unlock(&dq_list_lock);
717 dquot->dq_sb->dq_op->release_dquot(dquot);
718 goto we_slept;
719 }
720 atomic_dec(&dquot->dq_count);
721#ifdef __DQUOT_PARANOIA
722 /* sanity check */
Eric Sesterhenn8abf6a42006-04-02 13:36:13 +0200723 BUG_ON(!list_empty(&dquot->dq_free));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724#endif
725 put_dquot_last(dquot);
726 spin_unlock(&dq_list_lock);
727}
Mingming Cao08d03502009-01-14 16:19:32 +0100728EXPORT_SYMBOL(dqput);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729
Jan Kara7d9056b2008-11-25 15:31:32 +0100730struct dquot *dquot_alloc(struct super_block *sb, int type)
Jan Kara74f783a2008-08-19 14:51:22 +0200731{
732 return kmem_cache_zalloc(dquot_cachep, GFP_NOFS);
733}
Jan Kara7d9056b2008-11-25 15:31:32 +0100734EXPORT_SYMBOL(dquot_alloc);
Jan Kara74f783a2008-08-19 14:51:22 +0200735
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736static struct dquot *get_empty_dquot(struct super_block *sb, int type)
737{
738 struct dquot *dquot;
739
Jan Kara74f783a2008-08-19 14:51:22 +0200740 dquot = sb->dq_op->alloc_dquot(sb, type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 if(!dquot)
Jan Karadd6f3c62009-01-26 16:01:43 +0100742 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743
Ingo Molnard3be9152006-03-23 03:00:29 -0800744 mutex_init(&dquot->dq_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 INIT_LIST_HEAD(&dquot->dq_free);
746 INIT_LIST_HEAD(&dquot->dq_inuse);
747 INIT_HLIST_NODE(&dquot->dq_hash);
748 INIT_LIST_HEAD(&dquot->dq_dirty);
Jan Kara6362e4d2006-03-23 03:00:17 -0800749 init_waitqueue_head(&dquot->dq_wait_unused);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 dquot->dq_sb = sb;
751 dquot->dq_type = type;
752 atomic_set(&dquot->dq_count, 1);
753
754 return dquot;
755}
756
757/*
758 * Get reference to dquot
Jan Karacc334122009-01-12 17:23:05 +0100759 *
760 * Locking is slightly tricky here. We are guarded from parallel quotaoff()
761 * destroying our dquot by:
762 * a) checking for quota flags under dq_list_lock and
763 * b) getting a reference to dquot before we release dq_list_lock
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 */
Jan Kara3d9ea252008-10-10 16:12:23 +0200765struct dquot *dqget(struct super_block *sb, unsigned int id, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766{
767 unsigned int hashent = hashfn(sb, id, type);
Jan Karadd6f3c62009-01-26 16:01:43 +0100768 struct dquot *dquot = NULL, *empty = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769
Jan Karaf55abc02008-08-20 17:50:32 +0200770 if (!sb_has_quota_active(sb, type))
Jan Karadd6f3c62009-01-26 16:01:43 +0100771 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772we_slept:
773 spin_lock(&dq_list_lock);
Jan Karacc334122009-01-12 17:23:05 +0100774 spin_lock(&dq_state_lock);
775 if (!sb_has_quota_active(sb, type)) {
776 spin_unlock(&dq_state_lock);
777 spin_unlock(&dq_list_lock);
778 goto out;
779 }
780 spin_unlock(&dq_state_lock);
781
Jan Karadd6f3c62009-01-26 16:01:43 +0100782 dquot = find_dquot(hashent, sb, id, type);
783 if (!dquot) {
784 if (!empty) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 spin_unlock(&dq_list_lock);
Jan Karadd6f3c62009-01-26 16:01:43 +0100786 empty = get_empty_dquot(sb, type);
787 if (!empty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 schedule(); /* Try to wait for a moment... */
789 goto we_slept;
790 }
791 dquot = empty;
Jan Karadd6f3c62009-01-26 16:01:43 +0100792 empty = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 dquot->dq_id = id;
794 /* all dquots go on the inuse_list */
795 put_inuse(dquot);
796 /* hash it first so it can be found */
797 insert_dquot_hash(dquot);
798 dqstats.lookups++;
799 spin_unlock(&dq_list_lock);
800 } else {
801 if (!atomic_read(&dquot->dq_count))
802 remove_free_dquot(dquot);
803 atomic_inc(&dquot->dq_count);
804 dqstats.cache_hits++;
805 dqstats.lookups++;
806 spin_unlock(&dq_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 }
Jan Kara268157b2009-01-27 15:47:22 +0100808 /* Wait for dq_lock - after this we know that either dquot_release() is
809 * already finished or it will be canceled due to dq_count > 1 test */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 wait_on_dquot(dquot);
Jan Kara268157b2009-01-27 15:47:22 +0100811 /* Read the dquot / allocate space in quota file */
812 if (!test_bit(DQ_ACTIVE_B, &dquot->dq_flags) &&
813 sb->dq_op->acquire_dquot(dquot) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 dqput(dquot);
Jan Karadd6f3c62009-01-26 16:01:43 +0100815 dquot = NULL;
Jan Karacc334122009-01-12 17:23:05 +0100816 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 }
818#ifdef __DQUOT_PARANOIA
Eric Sesterhenn8abf6a42006-04-02 13:36:13 +0200819 BUG_ON(!dquot->dq_sb); /* Has somebody invalidated entry under us? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820#endif
Jan Karacc334122009-01-12 17:23:05 +0100821out:
822 if (empty)
823 do_destroy_dquot(empty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824
825 return dquot;
826}
Mingming Cao08d03502009-01-14 16:19:32 +0100827EXPORT_SYMBOL(dqget);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828
829static int dqinit_needed(struct inode *inode, int type)
830{
831 int cnt;
832
833 if (IS_NOQUOTA(inode))
834 return 0;
835 if (type != -1)
Jan Karadd6f3c62009-01-26 16:01:43 +0100836 return !inode->i_dquot[type];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
Jan Karadd6f3c62009-01-26 16:01:43 +0100838 if (!inode->i_dquot[cnt])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 return 1;
840 return 0;
841}
842
Ingo Molnard3be9152006-03-23 03:00:29 -0800843/* This routine is guarded by dqonoff_mutex mutex */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844static void add_dquot_ref(struct super_block *sb, int type)
845{
Jan Kara941d2382008-02-06 01:37:36 -0800846 struct inode *inode, *old_inode = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847
Christoph Hellwigd003fb72007-02-12 00:51:58 -0800848 spin_lock(&inode_lock);
849 list_for_each_entry(inode, &sb->s_inodes, i_sb_list) {
Wu Fengguangb6fac632009-04-02 16:56:34 -0700850 if (inode->i_state & (I_FREEING|I_CLEAR|I_WILL_FREE|I_NEW))
Nick Pigginaabb8fd2009-03-11 13:17:36 -0700851 continue;
Christoph Hellwigd003fb72007-02-12 00:51:58 -0800852 if (!atomic_read(&inode->i_writecount))
853 continue;
854 if (!dqinit_needed(inode, type))
855 continue;
Christoph Hellwigd003fb72007-02-12 00:51:58 -0800856
857 __iget(inode);
858 spin_unlock(&inode_lock);
859
Jan Kara941d2382008-02-06 01:37:36 -0800860 iput(old_inode);
Christoph Hellwigd003fb72007-02-12 00:51:58 -0800861 sb->dq_op->initialize(inode, type);
Jan Kara941d2382008-02-06 01:37:36 -0800862 /* We hold a reference to 'inode' so it couldn't have been
863 * removed from s_inodes list while we dropped the inode_lock.
864 * We cannot iput the inode now as we can be holding the last
865 * reference and we cannot iput it under inode_lock. So we
866 * keep the reference and iput it later. */
867 old_inode = inode;
868 spin_lock(&inode_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 }
Christoph Hellwigd003fb72007-02-12 00:51:58 -0800870 spin_unlock(&inode_lock);
Jan Kara941d2382008-02-06 01:37:36 -0800871 iput(old_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872}
873
Jan Kara268157b2009-01-27 15:47:22 +0100874/*
875 * Return 0 if dqput() won't block.
876 * (note that 1 doesn't necessarily mean blocking)
877 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878static inline int dqput_blocks(struct dquot *dquot)
879{
880 if (atomic_read(&dquot->dq_count) <= 1)
881 return 1;
882 return 0;
883}
884
Jan Kara268157b2009-01-27 15:47:22 +0100885/*
886 * Remove references to dquots from inode and add dquot to list for freeing
887 * if we have the last referece to dquot
888 * We can't race with anybody because we hold dqptr_sem for writing...
889 */
Adrian Bunke5f00f42007-05-08 00:27:22 -0700890static int remove_inode_dquot_ref(struct inode *inode, int type,
891 struct list_head *tofree_head)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892{
893 struct dquot *dquot = inode->i_dquot[type];
894
Jan Karadd6f3c62009-01-26 16:01:43 +0100895 inode->i_dquot[type] = NULL;
896 if (dquot) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 if (dqput_blocks(dquot)) {
898#ifdef __DQUOT_PARANOIA
899 if (atomic_read(&dquot->dq_count) != 1)
900 printk(KERN_WARNING "VFS: Adding dquot with dq_count %d to dispose list.\n", atomic_read(&dquot->dq_count));
901#endif
902 spin_lock(&dq_list_lock);
Jan Kara268157b2009-01-27 15:47:22 +0100903 /* As dquot must have currently users it can't be on
904 * the free list... */
905 list_add(&dquot->dq_free, tofree_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 spin_unlock(&dq_list_lock);
907 return 1;
908 }
909 else
910 dqput(dquot); /* We have guaranteed we won't block */
911 }
912 return 0;
913}
914
Jan Kara268157b2009-01-27 15:47:22 +0100915/*
916 * Free list of dquots
917 * Dquots are removed from inodes and no new references can be got so we are
918 * the only ones holding reference
919 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920static void put_dquot_list(struct list_head *tofree_head)
921{
922 struct list_head *act_head;
923 struct dquot *dquot;
924
925 act_head = tofree_head->next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 while (act_head != tofree_head) {
927 dquot = list_entry(act_head, struct dquot, dq_free);
928 act_head = act_head->next;
Jan Kara268157b2009-01-27 15:47:22 +0100929 /* Remove dquot from the list so we won't have problems... */
930 list_del_init(&dquot->dq_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 dqput(dquot);
932 }
933}
934
Christoph Hellwigfb58b732007-02-12 00:51:57 -0800935static void remove_dquot_ref(struct super_block *sb, int type,
936 struct list_head *tofree_head)
937{
938 struct inode *inode;
939
940 spin_lock(&inode_lock);
941 list_for_each_entry(inode, &sb->s_inodes, i_sb_list) {
Nick Pigginaabb8fd2009-03-11 13:17:36 -0700942 /*
943 * We have to scan also I_NEW inodes because they can already
944 * have quota pointer initialized. Luckily, we need to touch
945 * only quota pointers and these have separate locking
946 * (dqptr_sem).
947 */
Christoph Hellwigfb58b732007-02-12 00:51:57 -0800948 if (!IS_NOQUOTA(inode))
949 remove_inode_dquot_ref(inode, type, tofree_head);
950 }
951 spin_unlock(&inode_lock);
952}
953
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954/* Gather all references from inodes and drop them */
955static void drop_dquot_ref(struct super_block *sb, int type)
956{
957 LIST_HEAD(tofree_head);
958
Christoph Hellwigfb58b732007-02-12 00:51:57 -0800959 if (sb->dq_op) {
960 down_write(&sb_dqopt(sb)->dqptr_sem);
961 remove_dquot_ref(sb, type, &tofree_head);
962 up_write(&sb_dqopt(sb)->dqptr_sem);
963 put_dquot_list(&tofree_head);
964 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965}
966
Jan Kara12095462008-08-20 14:45:12 +0200967static inline void dquot_incr_inodes(struct dquot *dquot, qsize_t number)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968{
969 dquot->dq_dqb.dqb_curinodes += number;
970}
971
972static inline void dquot_incr_space(struct dquot *dquot, qsize_t number)
973{
974 dquot->dq_dqb.dqb_curspace += number;
975}
976
Mingming Caof18df222009-01-13 16:43:09 +0100977static inline void dquot_resv_space(struct dquot *dquot, qsize_t number)
978{
979 dquot->dq_dqb.dqb_rsvspace += number;
980}
981
Mingming Cao740d9dc2009-01-13 16:43:14 +0100982/*
983 * Claim reserved quota space
984 */
985static void dquot_claim_reserved_space(struct dquot *dquot,
986 qsize_t number)
987{
988 WARN_ON(dquot->dq_dqb.dqb_rsvspace < number);
989 dquot->dq_dqb.dqb_curspace += number;
990 dquot->dq_dqb.dqb_rsvspace -= number;
991}
992
993static inline
994void dquot_free_reserved_space(struct dquot *dquot, qsize_t number)
995{
996 dquot->dq_dqb.dqb_rsvspace -= number;
997}
998
Jan Kara7a2435d2009-01-27 01:47:11 +0100999static void dquot_decr_inodes(struct dquot *dquot, qsize_t number)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000{
Jan Karadb49d2d2008-10-01 18:21:39 +02001001 if (sb_dqopt(dquot->dq_sb)->flags & DQUOT_NEGATIVE_USAGE ||
1002 dquot->dq_dqb.dqb_curinodes >= number)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 dquot->dq_dqb.dqb_curinodes -= number;
1004 else
1005 dquot->dq_dqb.dqb_curinodes = 0;
1006 if (dquot->dq_dqb.dqb_curinodes <= dquot->dq_dqb.dqb_isoftlimit)
1007 dquot->dq_dqb.dqb_itime = (time_t) 0;
1008 clear_bit(DQ_INODES_B, &dquot->dq_flags);
1009}
1010
Jan Kara7a2435d2009-01-27 01:47:11 +01001011static void dquot_decr_space(struct dquot *dquot, qsize_t number)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012{
Jan Karadb49d2d2008-10-01 18:21:39 +02001013 if (sb_dqopt(dquot->dq_sb)->flags & DQUOT_NEGATIVE_USAGE ||
1014 dquot->dq_dqb.dqb_curspace >= number)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 dquot->dq_dqb.dqb_curspace -= number;
1016 else
1017 dquot->dq_dqb.dqb_curspace = 0;
Jan Kara12095462008-08-20 14:45:12 +02001018 if (dquot->dq_dqb.dqb_curspace <= dquot->dq_dqb.dqb_bsoftlimit)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 dquot->dq_dqb.dqb_btime = (time_t) 0;
1020 clear_bit(DQ_BLKS_B, &dquot->dq_flags);
1021}
1022
Jan Karac5254602007-12-22 14:03:25 -08001023static int warning_issued(struct dquot *dquot, const int warntype)
1024{
1025 int flag = (warntype == QUOTA_NL_BHARDWARN ||
1026 warntype == QUOTA_NL_BSOFTLONGWARN) ? DQ_BLKS_B :
1027 ((warntype == QUOTA_NL_IHARDWARN ||
1028 warntype == QUOTA_NL_ISOFTLONGWARN) ? DQ_INODES_B : 0);
1029
1030 if (!flag)
1031 return 0;
1032 return test_and_set_bit(flag, &dquot->dq_flags);
1033}
1034
Jan Kara8e893462007-10-16 23:29:31 -07001035#ifdef CONFIG_PRINT_QUOTA_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036static int flag_print_warnings = 1;
1037
Jan Kara7a2435d2009-01-27 01:47:11 +01001038static int need_print_warning(struct dquot *dquot)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039{
1040 if (!flag_print_warnings)
1041 return 0;
1042
1043 switch (dquot->dq_type) {
1044 case USRQUOTA:
David Howellsda9592e2008-11-14 10:39:05 +11001045 return current_fsuid() == dquot->dq_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 case GRPQUOTA:
1047 return in_group_p(dquot->dq_id);
1048 }
1049 return 0;
1050}
1051
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052/* Print warning to user which exceeded quota */
Jan Karac5254602007-12-22 14:03:25 -08001053static void print_warning(struct dquot *dquot, const int warntype)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054{
1055 char *msg = NULL;
Peter Zijlstra24ec8392006-12-08 02:36:04 -08001056 struct tty_struct *tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057
Jan Kara657d3bf2008-07-25 01:46:52 -07001058 if (warntype == QUOTA_NL_IHARDBELOW ||
1059 warntype == QUOTA_NL_ISOFTBELOW ||
1060 warntype == QUOTA_NL_BHARDBELOW ||
1061 warntype == QUOTA_NL_BSOFTBELOW || !need_print_warning(dquot))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 return;
1063
Peter Zijlstra24ec8392006-12-08 02:36:04 -08001064 tty = get_current_tty();
1065 if (!tty)
Alan Cox452a00d2008-10-13 10:39:13 +01001066 return;
Peter Zijlstra24ec8392006-12-08 02:36:04 -08001067 tty_write_message(tty, dquot->dq_sb->s_id);
Jan Kara8e893462007-10-16 23:29:31 -07001068 if (warntype == QUOTA_NL_ISOFTWARN || warntype == QUOTA_NL_BSOFTWARN)
Peter Zijlstra24ec8392006-12-08 02:36:04 -08001069 tty_write_message(tty, ": warning, ");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 else
Peter Zijlstra24ec8392006-12-08 02:36:04 -08001071 tty_write_message(tty, ": write failed, ");
1072 tty_write_message(tty, quotatypes[dquot->dq_type]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 switch (warntype) {
Jan Kara8e893462007-10-16 23:29:31 -07001074 case QUOTA_NL_IHARDWARN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 msg = " file limit reached.\r\n";
1076 break;
Jan Kara8e893462007-10-16 23:29:31 -07001077 case QUOTA_NL_ISOFTLONGWARN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 msg = " file quota exceeded too long.\r\n";
1079 break;
Jan Kara8e893462007-10-16 23:29:31 -07001080 case QUOTA_NL_ISOFTWARN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 msg = " file quota exceeded.\r\n";
1082 break;
Jan Kara8e893462007-10-16 23:29:31 -07001083 case QUOTA_NL_BHARDWARN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 msg = " block limit reached.\r\n";
1085 break;
Jan Kara8e893462007-10-16 23:29:31 -07001086 case QUOTA_NL_BSOFTLONGWARN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 msg = " block quota exceeded too long.\r\n";
1088 break;
Jan Kara8e893462007-10-16 23:29:31 -07001089 case QUOTA_NL_BSOFTWARN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 msg = " block quota exceeded.\r\n";
1091 break;
1092 }
Peter Zijlstra24ec8392006-12-08 02:36:04 -08001093 tty_write_message(tty, msg);
Alan Cox452a00d2008-10-13 10:39:13 +01001094 tty_kref_put(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095}
Jan Kara8e893462007-10-16 23:29:31 -07001096#endif
1097
Mingming Caof18df222009-01-13 16:43:09 +01001098/*
1099 * Write warnings to the console and send warning messages over netlink.
1100 *
1101 * Note that this function can sleep.
1102 */
Jan Kara7a2435d2009-01-27 01:47:11 +01001103static void flush_warnings(struct dquot *const *dquots, char *warntype)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104{
Steven Whitehouse86e931a2009-09-28 12:35:17 +01001105 struct dquot *dq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 int i;
1107
Steven Whitehouse86e931a2009-09-28 12:35:17 +01001108 for (i = 0; i < MAXQUOTAS; i++) {
1109 dq = dquots[i];
1110 if (dq && warntype[i] != QUOTA_NL_NOWARN &&
1111 !warning_issued(dq, warntype[i])) {
Jan Kara8e893462007-10-16 23:29:31 -07001112#ifdef CONFIG_PRINT_QUOTA_WARNING
Steven Whitehouse86e931a2009-09-28 12:35:17 +01001113 print_warning(dq, warntype[i]);
Jan Kara8e893462007-10-16 23:29:31 -07001114#endif
Steven Whitehouse86e931a2009-09-28 12:35:17 +01001115 quota_send_warning(dq->dq_type, dq->dq_id,
1116 dq->dq_sb->s_dev, warntype[i]);
Jan Kara8e893462007-10-16 23:29:31 -07001117 }
Steven Whitehouse86e931a2009-09-28 12:35:17 +01001118 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119}
1120
Jan Kara7a2435d2009-01-27 01:47:11 +01001121static int ignore_hardlimit(struct dquot *dquot)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122{
1123 struct mem_dqinfo *info = &sb_dqopt(dquot->dq_sb)->info[dquot->dq_type];
1124
1125 return capable(CAP_SYS_RESOURCE) &&
Jan Kara268157b2009-01-27 15:47:22 +01001126 (info->dqi_format->qf_fmt_id != QFMT_VFS_OLD ||
1127 !(info->dqi_flags & V1_DQF_RSQUASH));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128}
1129
1130/* needs dq_data_lock */
Jan Kara12095462008-08-20 14:45:12 +02001131static int check_idq(struct dquot *dquot, qsize_t inodes, char *warntype)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132{
Jan Kara268157b2009-01-27 15:47:22 +01001133 qsize_t newinodes = dquot->dq_dqb.dqb_curinodes + inodes;
1134
Jan Kara8e893462007-10-16 23:29:31 -07001135 *warntype = QUOTA_NL_NOWARN;
Jan Karaf55abc02008-08-20 17:50:32 +02001136 if (!sb_has_quota_limits_enabled(dquot->dq_sb, dquot->dq_type) ||
1137 test_bit(DQ_FAKE_B, &dquot->dq_flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 return QUOTA_OK;
1139
1140 if (dquot->dq_dqb.dqb_ihardlimit &&
Jan Kara268157b2009-01-27 15:47:22 +01001141 newinodes > dquot->dq_dqb.dqb_ihardlimit &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 !ignore_hardlimit(dquot)) {
Jan Kara8e893462007-10-16 23:29:31 -07001143 *warntype = QUOTA_NL_IHARDWARN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 return NO_QUOTA;
1145 }
1146
1147 if (dquot->dq_dqb.dqb_isoftlimit &&
Jan Kara268157b2009-01-27 15:47:22 +01001148 newinodes > dquot->dq_dqb.dqb_isoftlimit &&
1149 dquot->dq_dqb.dqb_itime &&
1150 get_seconds() >= dquot->dq_dqb.dqb_itime &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 !ignore_hardlimit(dquot)) {
Jan Kara8e893462007-10-16 23:29:31 -07001152 *warntype = QUOTA_NL_ISOFTLONGWARN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 return NO_QUOTA;
1154 }
1155
1156 if (dquot->dq_dqb.dqb_isoftlimit &&
Jan Kara268157b2009-01-27 15:47:22 +01001157 newinodes > dquot->dq_dqb.dqb_isoftlimit &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 dquot->dq_dqb.dqb_itime == 0) {
Jan Kara8e893462007-10-16 23:29:31 -07001159 *warntype = QUOTA_NL_ISOFTWARN;
Jan Kara268157b2009-01-27 15:47:22 +01001160 dquot->dq_dqb.dqb_itime = get_seconds() +
1161 sb_dqopt(dquot->dq_sb)->info[dquot->dq_type].dqi_igrace;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 }
1163
1164 return QUOTA_OK;
1165}
1166
1167/* needs dq_data_lock */
1168static int check_bdq(struct dquot *dquot, qsize_t space, int prealloc, char *warntype)
1169{
Mingming Caof18df222009-01-13 16:43:09 +01001170 qsize_t tspace;
Jan Kara268157b2009-01-27 15:47:22 +01001171 struct super_block *sb = dquot->dq_sb;
Mingming Caof18df222009-01-13 16:43:09 +01001172
Jan Kara8e893462007-10-16 23:29:31 -07001173 *warntype = QUOTA_NL_NOWARN;
Jan Kara268157b2009-01-27 15:47:22 +01001174 if (!sb_has_quota_limits_enabled(sb, dquot->dq_type) ||
Jan Karaf55abc02008-08-20 17:50:32 +02001175 test_bit(DQ_FAKE_B, &dquot->dq_flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 return QUOTA_OK;
1177
Mingming Caof18df222009-01-13 16:43:09 +01001178 tspace = dquot->dq_dqb.dqb_curspace + dquot->dq_dqb.dqb_rsvspace
1179 + space;
1180
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 if (dquot->dq_dqb.dqb_bhardlimit &&
Mingming Caof18df222009-01-13 16:43:09 +01001182 tspace > dquot->dq_dqb.dqb_bhardlimit &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 !ignore_hardlimit(dquot)) {
1184 if (!prealloc)
Jan Kara8e893462007-10-16 23:29:31 -07001185 *warntype = QUOTA_NL_BHARDWARN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 return NO_QUOTA;
1187 }
1188
1189 if (dquot->dq_dqb.dqb_bsoftlimit &&
Mingming Caof18df222009-01-13 16:43:09 +01001190 tspace > dquot->dq_dqb.dqb_bsoftlimit &&
Jan Kara268157b2009-01-27 15:47:22 +01001191 dquot->dq_dqb.dqb_btime &&
1192 get_seconds() >= dquot->dq_dqb.dqb_btime &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 !ignore_hardlimit(dquot)) {
1194 if (!prealloc)
Jan Kara8e893462007-10-16 23:29:31 -07001195 *warntype = QUOTA_NL_BSOFTLONGWARN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 return NO_QUOTA;
1197 }
1198
1199 if (dquot->dq_dqb.dqb_bsoftlimit &&
Mingming Caof18df222009-01-13 16:43:09 +01001200 tspace > dquot->dq_dqb.dqb_bsoftlimit &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 dquot->dq_dqb.dqb_btime == 0) {
1202 if (!prealloc) {
Jan Kara8e893462007-10-16 23:29:31 -07001203 *warntype = QUOTA_NL_BSOFTWARN;
Jan Kara268157b2009-01-27 15:47:22 +01001204 dquot->dq_dqb.dqb_btime = get_seconds() +
1205 sb_dqopt(sb)->info[dquot->dq_type].dqi_bgrace;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 }
1207 else
1208 /*
1209 * We don't allow preallocation to exceed softlimit so exceeding will
1210 * be always printed
1211 */
1212 return NO_QUOTA;
1213 }
1214
1215 return QUOTA_OK;
1216}
1217
Jan Kara12095462008-08-20 14:45:12 +02001218static int info_idq_free(struct dquot *dquot, qsize_t inodes)
Jan Kara657d3bf2008-07-25 01:46:52 -07001219{
Jan Kara268157b2009-01-27 15:47:22 +01001220 qsize_t newinodes;
1221
Jan Kara657d3bf2008-07-25 01:46:52 -07001222 if (test_bit(DQ_FAKE_B, &dquot->dq_flags) ||
Jan Karaf55abc02008-08-20 17:50:32 +02001223 dquot->dq_dqb.dqb_curinodes <= dquot->dq_dqb.dqb_isoftlimit ||
1224 !sb_has_quota_limits_enabled(dquot->dq_sb, dquot->dq_type))
Jan Kara657d3bf2008-07-25 01:46:52 -07001225 return QUOTA_NL_NOWARN;
1226
Jan Kara268157b2009-01-27 15:47:22 +01001227 newinodes = dquot->dq_dqb.dqb_curinodes - inodes;
1228 if (newinodes <= dquot->dq_dqb.dqb_isoftlimit)
Jan Kara657d3bf2008-07-25 01:46:52 -07001229 return QUOTA_NL_ISOFTBELOW;
1230 if (dquot->dq_dqb.dqb_curinodes >= dquot->dq_dqb.dqb_ihardlimit &&
Jan Kara268157b2009-01-27 15:47:22 +01001231 newinodes < dquot->dq_dqb.dqb_ihardlimit)
Jan Kara657d3bf2008-07-25 01:46:52 -07001232 return QUOTA_NL_IHARDBELOW;
1233 return QUOTA_NL_NOWARN;
1234}
1235
1236static int info_bdq_free(struct dquot *dquot, qsize_t space)
1237{
1238 if (test_bit(DQ_FAKE_B, &dquot->dq_flags) ||
Jan Kara12095462008-08-20 14:45:12 +02001239 dquot->dq_dqb.dqb_curspace <= dquot->dq_dqb.dqb_bsoftlimit)
Jan Kara657d3bf2008-07-25 01:46:52 -07001240 return QUOTA_NL_NOWARN;
1241
Jan Kara12095462008-08-20 14:45:12 +02001242 if (dquot->dq_dqb.dqb_curspace - space <= dquot->dq_dqb.dqb_bsoftlimit)
Jan Kara657d3bf2008-07-25 01:46:52 -07001243 return QUOTA_NL_BSOFTBELOW;
Jan Kara12095462008-08-20 14:45:12 +02001244 if (dquot->dq_dqb.dqb_curspace >= dquot->dq_dqb.dqb_bhardlimit &&
1245 dquot->dq_dqb.dqb_curspace - space < dquot->dq_dqb.dqb_bhardlimit)
Jan Kara657d3bf2008-07-25 01:46:52 -07001246 return QUOTA_NL_BHARDBELOW;
1247 return QUOTA_NL_NOWARN;
1248}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249/*
1250 * Initialize quota pointers in inode
Jan Karacc334122009-01-12 17:23:05 +01001251 * We do things in a bit complicated way but by that we avoid calling
1252 * dqget() and thus filesystem callbacks under dqptr_sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 */
1254int dquot_initialize(struct inode *inode, int type)
1255{
1256 unsigned int id = 0;
1257 int cnt, ret = 0;
Jan Karadd6f3c62009-01-26 16:01:43 +01001258 struct dquot *got[MAXQUOTAS] = { NULL, NULL };
Jan Karacc334122009-01-12 17:23:05 +01001259 struct super_block *sb = inode->i_sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260
Ingo Molnard3be9152006-03-23 03:00:29 -08001261 /* First test before acquiring mutex - solves deadlocks when we
1262 * re-enter the quota code and are already holding the mutex */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 if (IS_NOQUOTA(inode))
1264 return 0;
Jan Karacc334122009-01-12 17:23:05 +01001265
1266 /* First get references to structures we might need. */
1267 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1268 if (type != -1 && cnt != type)
1269 continue;
1270 switch (cnt) {
1271 case USRQUOTA:
1272 id = inode->i_uid;
1273 break;
1274 case GRPQUOTA:
1275 id = inode->i_gid;
1276 break;
1277 }
1278 got[cnt] = dqget(sb, id, cnt);
1279 }
1280
1281 down_write(&sb_dqopt(sb)->dqptr_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 if (IS_NOQUOTA(inode))
1283 goto out_err;
1284 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1285 if (type != -1 && cnt != type)
1286 continue;
Jan Karacc334122009-01-12 17:23:05 +01001287 /* Avoid races with quotaoff() */
1288 if (!sb_has_quota_active(sb, cnt))
1289 continue;
Jan Karadd6f3c62009-01-26 16:01:43 +01001290 if (!inode->i_dquot[cnt]) {
Jan Karacc334122009-01-12 17:23:05 +01001291 inode->i_dquot[cnt] = got[cnt];
Jan Karadd6f3c62009-01-26 16:01:43 +01001292 got[cnt] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 }
1294 }
1295out_err:
Jan Karacc334122009-01-12 17:23:05 +01001296 up_write(&sb_dqopt(sb)->dqptr_sem);
1297 /* Drop unused references */
Dmitry Monakhovdc52dd32009-12-14 15:21:15 +03001298 dqput_all(got);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 return ret;
1300}
Mingming Cao08d03502009-01-14 16:19:32 +01001301EXPORT_SYMBOL(dquot_initialize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302
1303/*
1304 * Release all quotas referenced by inode
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 */
Jan Kara3d9ea252008-10-10 16:12:23 +02001306int dquot_drop(struct inode *inode)
1307{
Jan Karacc334122009-01-12 17:23:05 +01001308 int cnt;
1309 struct dquot *put[MAXQUOTAS];
1310
Jan Kara3d9ea252008-10-10 16:12:23 +02001311 down_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
Jan Karacc334122009-01-12 17:23:05 +01001312 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1313 put[cnt] = inode->i_dquot[cnt];
Jan Karadd6f3c62009-01-26 16:01:43 +01001314 inode->i_dquot[cnt] = NULL;
Jan Karacc334122009-01-12 17:23:05 +01001315 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 up_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
Dmitry Monakhovdc52dd32009-12-14 15:21:15 +03001317 dqput_all(put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 return 0;
1319}
Mingming Cao08d03502009-01-14 16:19:32 +01001320EXPORT_SYMBOL(dquot_drop);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321
Jan Karab85f4b82008-07-25 01:46:50 -07001322/* Wrapper to remove references to quota structures from inode */
1323void vfs_dq_drop(struct inode *inode)
1324{
1325 /* Here we can get arbitrary inode from clear_inode() so we have
1326 * to be careful. OTOH we don't need locking as quota operations
1327 * are allowed to change only at mount time */
1328 if (!IS_NOQUOTA(inode) && inode->i_sb && inode->i_sb->dq_op
1329 && inode->i_sb->dq_op->drop) {
1330 int cnt;
1331 /* Test before calling to rule out calls from proc and such
1332 * where we are not allowed to block. Note that this is
1333 * actually reliable test even without the lock - the caller
1334 * must assure that nobody can come after the DQUOT_DROP and
1335 * add quota pointers back anyway */
1336 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
Jan Karadd6f3c62009-01-26 16:01:43 +01001337 if (inode->i_dquot[cnt])
Jan Karab85f4b82008-07-25 01:46:50 -07001338 break;
1339 if (cnt < MAXQUOTAS)
1340 inode->i_sb->dq_op->drop(inode);
1341 }
1342}
Mingming Cao08d03502009-01-14 16:19:32 +01001343EXPORT_SYMBOL(vfs_dq_drop);
Jan Karab85f4b82008-07-25 01:46:50 -07001344
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345/*
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001346 * inode_reserved_space is managed internally by quota, and protected by
1347 * i_lock similar to i_blocks+i_bytes.
1348 */
1349static qsize_t *inode_reserved_space(struct inode * inode)
1350{
1351 /* Filesystem must explicitly define it's own method in order to use
1352 * quota reservation interface */
1353 BUG_ON(!inode->i_sb->dq_op->get_reserved_space);
1354 return inode->i_sb->dq_op->get_reserved_space(inode);
1355}
1356
1357static void inode_add_rsv_space(struct inode *inode, qsize_t number)
1358{
1359 spin_lock(&inode->i_lock);
1360 *inode_reserved_space(inode) += number;
1361 spin_unlock(&inode->i_lock);
1362}
1363
1364
1365static void inode_claim_rsv_space(struct inode *inode, qsize_t number)
1366{
1367 spin_lock(&inode->i_lock);
1368 *inode_reserved_space(inode) -= number;
1369 __inode_add_bytes(inode, number);
1370 spin_unlock(&inode->i_lock);
1371}
1372
1373static void inode_sub_rsv_space(struct inode *inode, qsize_t number)
1374{
1375 spin_lock(&inode->i_lock);
1376 *inode_reserved_space(inode) -= number;
1377 spin_unlock(&inode->i_lock);
1378}
1379
1380static qsize_t inode_get_rsv_space(struct inode *inode)
1381{
1382 qsize_t ret;
Jan Kara05b5d892010-01-06 18:03:36 +01001383
1384 if (!inode->i_sb->dq_op->get_reserved_space)
1385 return 0;
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001386 spin_lock(&inode->i_lock);
1387 ret = *inode_reserved_space(inode);
1388 spin_unlock(&inode->i_lock);
1389 return ret;
1390}
1391
1392static void inode_incr_space(struct inode *inode, qsize_t number,
1393 int reserve)
1394{
1395 if (reserve)
1396 inode_add_rsv_space(inode, number);
1397 else
1398 inode_add_bytes(inode, number);
1399}
1400
1401static void inode_decr_space(struct inode *inode, qsize_t number, int reserve)
1402{
1403 if (reserve)
1404 inode_sub_rsv_space(inode, number);
1405 else
1406 inode_sub_bytes(inode, number);
1407}
1408
1409/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410 * Following four functions update i_blocks+i_bytes fields and
1411 * quota information (together with appropriate checks)
1412 * NOTE: We absolutely rely on the fact that caller dirties
1413 * the inode (usually macros in quotaops.h care about this) and
1414 * holds a handle for the current transaction so that dquot write and
1415 * inode write go into the same transaction.
1416 */
1417
1418/*
1419 * This operation can block, but only after everything is updated
1420 */
Mingming Caof18df222009-01-13 16:43:09 +01001421int __dquot_alloc_space(struct inode *inode, qsize_t number,
1422 int warn, int reserve)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423{
Mingming Caof18df222009-01-13 16:43:09 +01001424 int cnt, ret = QUOTA_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 char warntype[MAXQUOTAS];
1426
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001427 /*
1428 * First test before acquiring mutex - solves deadlocks when we
1429 * re-enter the quota code and are already holding the mutex
1430 */
1431 if (IS_NOQUOTA(inode)) {
1432 inode_incr_space(inode, number, reserve);
1433 goto out;
1434 }
1435
1436 down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
Jan Kara8e893462007-10-16 23:29:31 -07001438 warntype[cnt] = QUOTA_NL_NOWARN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 spin_lock(&dq_data_lock);
1441 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
Jan Karadd6f3c62009-01-26 16:01:43 +01001442 if (!inode->i_dquot[cnt])
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 continue;
Mingming Caof18df222009-01-13 16:43:09 +01001444 if (check_bdq(inode->i_dquot[cnt], number, warn, warntype+cnt)
1445 == NO_QUOTA) {
1446 ret = NO_QUOTA;
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001447 spin_unlock(&dq_data_lock);
1448 goto out_flush_warn;
Mingming Caof18df222009-01-13 16:43:09 +01001449 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450 }
1451 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
Jan Karadd6f3c62009-01-26 16:01:43 +01001452 if (!inode->i_dquot[cnt])
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453 continue;
Mingming Caof18df222009-01-13 16:43:09 +01001454 if (reserve)
1455 dquot_resv_space(inode->i_dquot[cnt], number);
1456 else
1457 dquot_incr_space(inode->i_dquot[cnt], number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458 }
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001459 inode_incr_space(inode, number, reserve);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460 spin_unlock(&dq_data_lock);
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001461
1462 if (reserve)
1463 goto out_flush_warn;
Dmitry Monakhovdc52dd32009-12-14 15:21:15 +03001464 mark_all_dquot_dirty(inode->i_dquot);
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001465out_flush_warn:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 flush_warnings(inode->i_dquot, warntype);
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001467 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1468out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 return ret;
1470}
1471
Mingming Caof18df222009-01-13 16:43:09 +01001472int dquot_alloc_space(struct inode *inode, qsize_t number, int warn)
1473{
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001474 return __dquot_alloc_space(inode, number, warn, 0);
Mingming Caof18df222009-01-13 16:43:09 +01001475}
Mingming Cao08d03502009-01-14 16:19:32 +01001476EXPORT_SYMBOL(dquot_alloc_space);
Mingming Caof18df222009-01-13 16:43:09 +01001477
1478int dquot_reserve_space(struct inode *inode, qsize_t number, int warn)
1479{
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001480 return __dquot_alloc_space(inode, number, warn, 1);
Mingming Caof18df222009-01-13 16:43:09 +01001481}
1482EXPORT_SYMBOL(dquot_reserve_space);
1483
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484/*
1485 * This operation can block, but only after everything is updated
1486 */
Jan Kara12095462008-08-20 14:45:12 +02001487int dquot_alloc_inode(const struct inode *inode, qsize_t number)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488{
1489 int cnt, ret = NO_QUOTA;
1490 char warntype[MAXQUOTAS];
1491
Ingo Molnard3be9152006-03-23 03:00:29 -08001492 /* First test before acquiring mutex - solves deadlocks when we
1493 * re-enter the quota code and are already holding the mutex */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 if (IS_NOQUOTA(inode))
1495 return QUOTA_OK;
1496 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
Jan Kara8e893462007-10-16 23:29:31 -07001497 warntype[cnt] = QUOTA_NL_NOWARN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498 down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 spin_lock(&dq_data_lock);
1500 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
Jan Karadd6f3c62009-01-26 16:01:43 +01001501 if (!inode->i_dquot[cnt])
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502 continue;
Jan Kara268157b2009-01-27 15:47:22 +01001503 if (check_idq(inode->i_dquot[cnt], number, warntype+cnt)
1504 == NO_QUOTA)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505 goto warn_put_all;
1506 }
1507
1508 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
Jan Karadd6f3c62009-01-26 16:01:43 +01001509 if (!inode->i_dquot[cnt])
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510 continue;
1511 dquot_incr_inodes(inode->i_dquot[cnt], number);
1512 }
1513 ret = QUOTA_OK;
1514warn_put_all:
1515 spin_unlock(&dq_data_lock);
1516 if (ret == QUOTA_OK)
Dmitry Monakhovdc52dd32009-12-14 15:21:15 +03001517 mark_all_dquot_dirty(inode->i_dquot);
Jan Kara087ee8d2007-12-17 16:20:26 -08001518 flush_warnings(inode->i_dquot, warntype);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1520 return ret;
1521}
Mingming Cao08d03502009-01-14 16:19:32 +01001522EXPORT_SYMBOL(dquot_alloc_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523
Mingming Cao740d9dc2009-01-13 16:43:14 +01001524int dquot_claim_space(struct inode *inode, qsize_t number)
1525{
1526 int cnt;
1527 int ret = QUOTA_OK;
1528
1529 if (IS_NOQUOTA(inode)) {
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001530 inode_claim_rsv_space(inode, number);
Mingming Cao740d9dc2009-01-13 16:43:14 +01001531 goto out;
1532 }
1533
1534 down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
Mingming Cao740d9dc2009-01-13 16:43:14 +01001535 spin_lock(&dq_data_lock);
1536 /* Claim reserved quotas to allocated quotas */
1537 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
Jan Karadd6f3c62009-01-26 16:01:43 +01001538 if (inode->i_dquot[cnt])
Mingming Cao740d9dc2009-01-13 16:43:14 +01001539 dquot_claim_reserved_space(inode->i_dquot[cnt],
1540 number);
1541 }
1542 /* Update inode bytes */
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001543 inode_claim_rsv_space(inode, number);
Mingming Cao740d9dc2009-01-13 16:43:14 +01001544 spin_unlock(&dq_data_lock);
Dmitry Monakhovdc52dd32009-12-14 15:21:15 +03001545 mark_all_dquot_dirty(inode->i_dquot);
Mingming Cao740d9dc2009-01-13 16:43:14 +01001546 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1547out:
1548 return ret;
1549}
1550EXPORT_SYMBOL(dquot_claim_space);
1551
1552/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553 * This operation can block, but only after everything is updated
1554 */
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001555int __dquot_free_space(struct inode *inode, qsize_t number, int reserve)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556{
1557 unsigned int cnt;
Jan Kara657d3bf2008-07-25 01:46:52 -07001558 char warntype[MAXQUOTAS];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559
Ingo Molnard3be9152006-03-23 03:00:29 -08001560 /* First test before acquiring mutex - solves deadlocks when we
1561 * re-enter the quota code and are already holding the mutex */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562 if (IS_NOQUOTA(inode)) {
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001563 inode_decr_space(inode, number, reserve);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564 return QUOTA_OK;
1565 }
Jan Kara657d3bf2008-07-25 01:46:52 -07001566
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567 down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568 spin_lock(&dq_data_lock);
1569 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
Jan Karadd6f3c62009-01-26 16:01:43 +01001570 if (!inode->i_dquot[cnt])
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571 continue;
Jan Kara657d3bf2008-07-25 01:46:52 -07001572 warntype[cnt] = info_bdq_free(inode->i_dquot[cnt], number);
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001573 if (reserve)
1574 dquot_free_reserved_space(inode->i_dquot[cnt], number);
1575 else
1576 dquot_decr_space(inode->i_dquot[cnt], number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577 }
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001578 inode_decr_space(inode, number, reserve);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579 spin_unlock(&dq_data_lock);
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001580
1581 if (reserve)
1582 goto out_unlock;
Dmitry Monakhovdc52dd32009-12-14 15:21:15 +03001583 mark_all_dquot_dirty(inode->i_dquot);
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001584out_unlock:
Jan Kara657d3bf2008-07-25 01:46:52 -07001585 flush_warnings(inode->i_dquot, warntype);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1587 return QUOTA_OK;
1588}
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001589
1590int dquot_free_space(struct inode *inode, qsize_t number)
1591{
1592 return __dquot_free_space(inode, number, 0);
1593}
Mingming Cao08d03502009-01-14 16:19:32 +01001594EXPORT_SYMBOL(dquot_free_space);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595
1596/*
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001597 * Release reserved quota space
1598 */
1599void dquot_release_reserved_space(struct inode *inode, qsize_t number)
1600{
1601 __dquot_free_space(inode, number, 1);
1602
1603}
1604EXPORT_SYMBOL(dquot_release_reserved_space);
1605
1606/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607 * This operation can block, but only after everything is updated
1608 */
Jan Kara12095462008-08-20 14:45:12 +02001609int dquot_free_inode(const struct inode *inode, qsize_t number)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610{
1611 unsigned int cnt;
Jan Kara657d3bf2008-07-25 01:46:52 -07001612 char warntype[MAXQUOTAS];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613
Ingo Molnard3be9152006-03-23 03:00:29 -08001614 /* First test before acquiring mutex - solves deadlocks when we
1615 * re-enter the quota code and are already holding the mutex */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616 if (IS_NOQUOTA(inode))
1617 return QUOTA_OK;
Jan Kara657d3bf2008-07-25 01:46:52 -07001618
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619 down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620 spin_lock(&dq_data_lock);
1621 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
Jan Karadd6f3c62009-01-26 16:01:43 +01001622 if (!inode->i_dquot[cnt])
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623 continue;
Jan Kara657d3bf2008-07-25 01:46:52 -07001624 warntype[cnt] = info_idq_free(inode->i_dquot[cnt], number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625 dquot_decr_inodes(inode->i_dquot[cnt], number);
1626 }
1627 spin_unlock(&dq_data_lock);
Dmitry Monakhovdc52dd32009-12-14 15:21:15 +03001628 mark_all_dquot_dirty(inode->i_dquot);
Jan Kara657d3bf2008-07-25 01:46:52 -07001629 flush_warnings(inode->i_dquot, warntype);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1631 return QUOTA_OK;
1632}
Mingming Cao08d03502009-01-14 16:19:32 +01001633EXPORT_SYMBOL(dquot_free_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634
1635/*
1636 * Transfer the number of inode and blocks from one diskquota to an other.
1637 *
1638 * This operation can block, but only after everything is updated
1639 * A transaction must be started when entering this function.
1640 */
1641int dquot_transfer(struct inode *inode, struct iattr *iattr)
1642{
Mingming Cao740d9dc2009-01-13 16:43:14 +01001643 qsize_t space, cur_space;
1644 qsize_t rsv_space = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645 struct dquot *transfer_from[MAXQUOTAS];
1646 struct dquot *transfer_to[MAXQUOTAS];
Jan Karacc334122009-01-12 17:23:05 +01001647 int cnt, ret = QUOTA_OK;
1648 int chuid = iattr->ia_valid & ATTR_UID && inode->i_uid != iattr->ia_uid,
1649 chgid = iattr->ia_valid & ATTR_GID && inode->i_gid != iattr->ia_gid;
Jan Kara657d3bf2008-07-25 01:46:52 -07001650 char warntype_to[MAXQUOTAS];
1651 char warntype_from_inodes[MAXQUOTAS], warntype_from_space[MAXQUOTAS];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652
Ingo Molnard3be9152006-03-23 03:00:29 -08001653 /* First test before acquiring mutex - solves deadlocks when we
1654 * re-enter the quota code and are already holding the mutex */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655 if (IS_NOQUOTA(inode))
1656 return QUOTA_OK;
Jan Karacc334122009-01-12 17:23:05 +01001657 /* Initialize the arrays */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
Jan Karadd6f3c62009-01-26 16:01:43 +01001659 transfer_from[cnt] = NULL;
1660 transfer_to[cnt] = NULL;
Jan Kara657d3bf2008-07-25 01:46:52 -07001661 warntype_to[cnt] = QUOTA_NL_NOWARN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662 }
Jan Kara268157b2009-01-27 15:47:22 +01001663 if (chuid)
1664 transfer_to[USRQUOTA] = dqget(inode->i_sb, iattr->ia_uid,
1665 USRQUOTA);
1666 if (chgid)
1667 transfer_to[GRPQUOTA] = dqget(inode->i_sb, iattr->ia_gid,
1668 GRPQUOTA);
Jan Karacc334122009-01-12 17:23:05 +01001669
1670 down_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
Jan Karacc334122009-01-12 17:23:05 +01001671 if (IS_NOQUOTA(inode)) { /* File without quota accounting? */
1672 up_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
1673 goto put_all;
1674 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 spin_lock(&dq_data_lock);
Mingming Cao740d9dc2009-01-13 16:43:14 +01001676 cur_space = inode_get_bytes(inode);
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001677 rsv_space = inode_get_rsv_space(inode);
Mingming Cao740d9dc2009-01-13 16:43:14 +01001678 space = cur_space + rsv_space;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679 /* Build the transfer_from list and check the limits */
1680 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
Jan Karadd6f3c62009-01-26 16:01:43 +01001681 if (!transfer_to[cnt])
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682 continue;
1683 transfer_from[cnt] = inode->i_dquot[cnt];
Jan Kara657d3bf2008-07-25 01:46:52 -07001684 if (check_idq(transfer_to[cnt], 1, warntype_to + cnt) ==
1685 NO_QUOTA || check_bdq(transfer_to[cnt], space, 0,
1686 warntype_to + cnt) == NO_QUOTA)
Jan Karacc334122009-01-12 17:23:05 +01001687 goto over_quota;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688 }
1689
1690 /*
1691 * Finally perform the needed transfer from transfer_from to transfer_to
1692 */
1693 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1694 /*
1695 * Skip changes for same uid or gid or for turned off quota-type.
1696 */
Jan Karadd6f3c62009-01-26 16:01:43 +01001697 if (!transfer_to[cnt])
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698 continue;
1699
1700 /* Due to IO error we might not have transfer_from[] structure */
1701 if (transfer_from[cnt]) {
Jan Kara657d3bf2008-07-25 01:46:52 -07001702 warntype_from_inodes[cnt] =
1703 info_idq_free(transfer_from[cnt], 1);
1704 warntype_from_space[cnt] =
1705 info_bdq_free(transfer_from[cnt], space);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706 dquot_decr_inodes(transfer_from[cnt], 1);
Mingming Cao740d9dc2009-01-13 16:43:14 +01001707 dquot_decr_space(transfer_from[cnt], cur_space);
1708 dquot_free_reserved_space(transfer_from[cnt],
1709 rsv_space);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710 }
1711
1712 dquot_incr_inodes(transfer_to[cnt], 1);
Mingming Cao740d9dc2009-01-13 16:43:14 +01001713 dquot_incr_space(transfer_to[cnt], cur_space);
1714 dquot_resv_space(transfer_to[cnt], rsv_space);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715
1716 inode->i_dquot[cnt] = transfer_to[cnt];
1717 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718 spin_unlock(&dq_data_lock);
Jan Karacc334122009-01-12 17:23:05 +01001719 up_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
1720
Dmitry Monakhovdc52dd32009-12-14 15:21:15 +03001721 mark_all_dquot_dirty(transfer_from);
1722 mark_all_dquot_dirty(transfer_to);
1723 /* The reference we got is transferred to the inode */
1724 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1725 transfer_to[cnt] = NULL;
Jan Karacc334122009-01-12 17:23:05 +01001726warn_put_all:
Jan Kara657d3bf2008-07-25 01:46:52 -07001727 flush_warnings(transfer_to, warntype_to);
1728 flush_warnings(transfer_from, warntype_from_inodes);
1729 flush_warnings(transfer_from, warntype_from_space);
Jan Karacc334122009-01-12 17:23:05 +01001730put_all:
Dmitry Monakhovdc52dd32009-12-14 15:21:15 +03001731 dqput_all(transfer_from);
1732 dqput_all(transfer_to);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733 return ret;
Jan Karacc334122009-01-12 17:23:05 +01001734over_quota:
1735 spin_unlock(&dq_data_lock);
1736 up_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
1737 /* Clear dquot pointers we don't want to dqput() */
1738 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
Jan Karadd6f3c62009-01-26 16:01:43 +01001739 transfer_from[cnt] = NULL;
Jan Karacc334122009-01-12 17:23:05 +01001740 ret = NO_QUOTA;
1741 goto warn_put_all;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742}
Mingming Cao08d03502009-01-14 16:19:32 +01001743EXPORT_SYMBOL(dquot_transfer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744
Jan Karab85f4b82008-07-25 01:46:50 -07001745/* Wrapper for transferring ownership of an inode */
1746int vfs_dq_transfer(struct inode *inode, struct iattr *iattr)
1747{
Jan Karaf55abc02008-08-20 17:50:32 +02001748 if (sb_any_quota_active(inode->i_sb) && !IS_NOQUOTA(inode)) {
Jan Karab85f4b82008-07-25 01:46:50 -07001749 vfs_dq_init(inode);
1750 if (inode->i_sb->dq_op->transfer(inode, iattr) == NO_QUOTA)
1751 return 1;
1752 }
1753 return 0;
1754}
Mingming Cao08d03502009-01-14 16:19:32 +01001755EXPORT_SYMBOL(vfs_dq_transfer);
Jan Karab85f4b82008-07-25 01:46:50 -07001756
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757/*
1758 * Write info of quota file to disk
1759 */
1760int dquot_commit_info(struct super_block *sb, int type)
1761{
1762 int ret;
1763 struct quota_info *dqopt = sb_dqopt(sb);
1764
Ingo Molnard3be9152006-03-23 03:00:29 -08001765 mutex_lock(&dqopt->dqio_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766 ret = dqopt->ops[type]->write_file_info(sb, type);
Ingo Molnard3be9152006-03-23 03:00:29 -08001767 mutex_unlock(&dqopt->dqio_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768 return ret;
1769}
Mingming Cao08d03502009-01-14 16:19:32 +01001770EXPORT_SYMBOL(dquot_commit_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771
1772/*
1773 * Definitions of diskquota operations.
1774 */
Alexey Dobriyan61e225d2009-09-21 17:01:08 -07001775const struct dquot_operations dquot_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776 .initialize = dquot_initialize,
1777 .drop = dquot_drop,
1778 .alloc_space = dquot_alloc_space,
1779 .alloc_inode = dquot_alloc_inode,
1780 .free_space = dquot_free_space,
1781 .free_inode = dquot_free_inode,
1782 .transfer = dquot_transfer,
1783 .write_dquot = dquot_commit,
1784 .acquire_dquot = dquot_acquire,
1785 .release_dquot = dquot_release,
1786 .mark_dirty = dquot_mark_dquot_dirty,
Jan Kara74f783a2008-08-19 14:51:22 +02001787 .write_info = dquot_commit_info,
1788 .alloc_dquot = dquot_alloc,
1789 .destroy_dquot = dquot_destroy,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790};
1791
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792/*
1793 * Turn quota off on a device. type == -1 ==> quotaoff for all types (umount)
1794 */
Jan Karaf55abc02008-08-20 17:50:32 +02001795int vfs_quota_disable(struct super_block *sb, int type, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796{
Jan Kara0ff5af82008-04-28 02:14:33 -07001797 int cnt, ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798 struct quota_info *dqopt = sb_dqopt(sb);
1799 struct inode *toputinode[MAXQUOTAS];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800
Jan Karaf55abc02008-08-20 17:50:32 +02001801 /* Cannot turn off usage accounting without turning off limits, or
1802 * suspend quotas and simultaneously turn quotas off. */
1803 if ((flags & DQUOT_USAGE_ENABLED && !(flags & DQUOT_LIMITS_ENABLED))
1804 || (flags & DQUOT_SUSPENDED && flags & (DQUOT_LIMITS_ENABLED |
1805 DQUOT_USAGE_ENABLED)))
1806 return -EINVAL;
1807
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808 /* We need to serialize quota_off() for device */
Ingo Molnard3be9152006-03-23 03:00:29 -08001809 mutex_lock(&dqopt->dqonoff_mutex);
Jan Kara9377abd2008-05-12 14:02:08 -07001810
1811 /*
1812 * Skip everything if there's nothing to do. We have to do this because
1813 * sometimes we are called when fill_super() failed and calling
1814 * sync_fs() in such cases does no good.
1815 */
Jan Karaf55abc02008-08-20 17:50:32 +02001816 if (!sb_any_quota_loaded(sb)) {
Jan Kara9377abd2008-05-12 14:02:08 -07001817 mutex_unlock(&dqopt->dqonoff_mutex);
1818 return 0;
1819 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1821 toputinode[cnt] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822 if (type != -1 && cnt != type)
1823 continue;
Jan Karaf55abc02008-08-20 17:50:32 +02001824 if (!sb_has_quota_loaded(sb, cnt))
Jan Kara0ff5af82008-04-28 02:14:33 -07001825 continue;
Jan Karaf55abc02008-08-20 17:50:32 +02001826
1827 if (flags & DQUOT_SUSPENDED) {
Jan Karacc334122009-01-12 17:23:05 +01001828 spin_lock(&dq_state_lock);
Jan Karaf55abc02008-08-20 17:50:32 +02001829 dqopt->flags |=
1830 dquot_state_flag(DQUOT_SUSPENDED, cnt);
Jan Karacc334122009-01-12 17:23:05 +01001831 spin_unlock(&dq_state_lock);
Jan Karaf55abc02008-08-20 17:50:32 +02001832 } else {
Jan Karacc334122009-01-12 17:23:05 +01001833 spin_lock(&dq_state_lock);
Jan Karaf55abc02008-08-20 17:50:32 +02001834 dqopt->flags &= ~dquot_state_flag(flags, cnt);
1835 /* Turning off suspended quotas? */
1836 if (!sb_has_quota_loaded(sb, cnt) &&
1837 sb_has_quota_suspended(sb, cnt)) {
1838 dqopt->flags &= ~dquot_state_flag(
1839 DQUOT_SUSPENDED, cnt);
Jan Karacc334122009-01-12 17:23:05 +01001840 spin_unlock(&dq_state_lock);
Jan Karaf55abc02008-08-20 17:50:32 +02001841 iput(dqopt->files[cnt]);
1842 dqopt->files[cnt] = NULL;
1843 continue;
1844 }
Jan Karacc334122009-01-12 17:23:05 +01001845 spin_unlock(&dq_state_lock);
Jan Kara0ff5af82008-04-28 02:14:33 -07001846 }
Jan Karaf55abc02008-08-20 17:50:32 +02001847
1848 /* We still have to keep quota loaded? */
1849 if (sb_has_quota_loaded(sb, cnt) && !(flags & DQUOT_SUSPENDED))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851
1852 /* Note: these are blocking operations */
1853 drop_dquot_ref(sb, cnt);
1854 invalidate_dquots(sb, cnt);
1855 /*
Jan Kara268157b2009-01-27 15:47:22 +01001856 * Now all dquots should be invalidated, all writes done so we
1857 * should be only users of the info. No locks needed.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858 */
1859 if (info_dirty(&dqopt->info[cnt]))
1860 sb->dq_op->write_info(sb, cnt);
1861 if (dqopt->ops[cnt]->free_file_info)
1862 dqopt->ops[cnt]->free_file_info(sb, cnt);
1863 put_quota_format(dqopt->info[cnt].dqi_format);
1864
1865 toputinode[cnt] = dqopt->files[cnt];
Jan Karaf55abc02008-08-20 17:50:32 +02001866 if (!sb_has_quota_loaded(sb, cnt))
Jan Kara0ff5af82008-04-28 02:14:33 -07001867 dqopt->files[cnt] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868 dqopt->info[cnt].dqi_flags = 0;
1869 dqopt->info[cnt].dqi_igrace = 0;
1870 dqopt->info[cnt].dqi_bgrace = 0;
1871 dqopt->ops[cnt] = NULL;
1872 }
Ingo Molnard3be9152006-03-23 03:00:29 -08001873 mutex_unlock(&dqopt->dqonoff_mutex);
Jan Karaca785ec2008-09-30 17:53:37 +02001874
1875 /* Skip syncing and setting flags if quota files are hidden */
1876 if (dqopt->flags & DQUOT_QUOTA_SYS_FILE)
1877 goto put_inodes;
1878
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879 /* Sync the superblock so that buffers with quota data are written to
Al Viro7b7b1ac2005-11-07 17:13:39 -05001880 * disk (and so userspace sees correct data afterwards). */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881 if (sb->s_op->sync_fs)
1882 sb->s_op->sync_fs(sb, 1);
1883 sync_blockdev(sb->s_bdev);
1884 /* Now the quota files are just ordinary files and we can set the
1885 * inode flags back. Moreover we discard the pagecache so that
1886 * userspace sees the writes we did bypassing the pagecache. We
1887 * must also discard the blockdev buffers so that we see the
1888 * changes done by userspace on the next quotaon() */
1889 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1890 if (toputinode[cnt]) {
Ingo Molnard3be9152006-03-23 03:00:29 -08001891 mutex_lock(&dqopt->dqonoff_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892 /* If quota was reenabled in the meantime, we have
1893 * nothing to do */
Jan Karaf55abc02008-08-20 17:50:32 +02001894 if (!sb_has_quota_loaded(sb, cnt)) {
Jan Kara268157b2009-01-27 15:47:22 +01001895 mutex_lock_nested(&toputinode[cnt]->i_mutex,
1896 I_MUTEX_QUOTA);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897 toputinode[cnt]->i_flags &= ~(S_IMMUTABLE |
1898 S_NOATIME | S_NOQUOTA);
Jan Kara268157b2009-01-27 15:47:22 +01001899 truncate_inode_pages(&toputinode[cnt]->i_data,
1900 0);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001901 mutex_unlock(&toputinode[cnt]->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902 mark_inode_dirty(toputinode[cnt]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903 }
Ingo Molnard3be9152006-03-23 03:00:29 -08001904 mutex_unlock(&dqopt->dqonoff_mutex);
Jan Karaca785ec2008-09-30 17:53:37 +02001905 }
1906 if (sb->s_bdev)
1907 invalidate_bdev(sb->s_bdev);
1908put_inodes:
1909 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1910 if (toputinode[cnt]) {
Jan Kara0ff5af82008-04-28 02:14:33 -07001911 /* On remount RO, we keep the inode pointer so that we
Jan Karaf55abc02008-08-20 17:50:32 +02001912 * can reenable quota on the subsequent remount RW. We
1913 * have to check 'flags' variable and not use sb_has_
1914 * function because another quotaon / quotaoff could
1915 * change global state before we got here. We refuse
1916 * to suspend quotas when there is pending delete on
1917 * the quota file... */
1918 if (!(flags & DQUOT_SUSPENDED))
Jan Kara0ff5af82008-04-28 02:14:33 -07001919 iput(toputinode[cnt]);
1920 else if (!toputinode[cnt]->i_nlink)
1921 ret = -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922 }
Jan Kara0ff5af82008-04-28 02:14:33 -07001923 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924}
Mingming Cao08d03502009-01-14 16:19:32 +01001925EXPORT_SYMBOL(vfs_quota_disable);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926
Jan Karaf55abc02008-08-20 17:50:32 +02001927int vfs_quota_off(struct super_block *sb, int type, int remount)
1928{
1929 return vfs_quota_disable(sb, type, remount ? DQUOT_SUSPENDED :
1930 (DQUOT_USAGE_ENABLED | DQUOT_LIMITS_ENABLED));
1931}
Mingming Cao08d03502009-01-14 16:19:32 +01001932EXPORT_SYMBOL(vfs_quota_off);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933/*
1934 * Turn quotas on on a device
1935 */
1936
Jan Karaf55abc02008-08-20 17:50:32 +02001937/*
1938 * Helper function to turn quotas on when we already have the inode of
1939 * quota file and no quota information is loaded.
1940 */
1941static int vfs_load_quota_inode(struct inode *inode, int type, int format_id,
1942 unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943{
1944 struct quota_format_type *fmt = find_quota_format(format_id);
1945 struct super_block *sb = inode->i_sb;
1946 struct quota_info *dqopt = sb_dqopt(sb);
1947 int error;
1948 int oldflags = -1;
1949
1950 if (!fmt)
1951 return -ESRCH;
1952 if (!S_ISREG(inode->i_mode)) {
1953 error = -EACCES;
1954 goto out_fmt;
1955 }
1956 if (IS_RDONLY(inode)) {
1957 error = -EROFS;
1958 goto out_fmt;
1959 }
1960 if (!sb->s_op->quota_write || !sb->s_op->quota_read) {
1961 error = -EINVAL;
1962 goto out_fmt;
1963 }
Jan Karaf55abc02008-08-20 17:50:32 +02001964 /* Usage always has to be set... */
1965 if (!(flags & DQUOT_USAGE_ENABLED)) {
1966 error = -EINVAL;
1967 goto out_fmt;
1968 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969
Jan Karaca785ec2008-09-30 17:53:37 +02001970 if (!(dqopt->flags & DQUOT_QUOTA_SYS_FILE)) {
1971 /* As we bypass the pagecache we must now flush the inode so
1972 * that we see all the changes from userspace... */
1973 write_inode_now(inode, 1);
1974 /* And now flush the block cache so that kernel sees the
1975 * changes */
1976 invalidate_bdev(sb->s_bdev);
1977 }
Ingo Molnard3be9152006-03-23 03:00:29 -08001978 mutex_lock(&dqopt->dqonoff_mutex);
Jan Karaf55abc02008-08-20 17:50:32 +02001979 if (sb_has_quota_loaded(sb, type)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980 error = -EBUSY;
1981 goto out_lock;
1982 }
Jan Karaca785ec2008-09-30 17:53:37 +02001983
1984 if (!(dqopt->flags & DQUOT_QUOTA_SYS_FILE)) {
1985 /* We don't want quota and atime on quota files (deadlocks
1986 * possible) Also nobody should write to the file - we use
1987 * special IO operations which ignore the immutable bit. */
Jan Karadee86562009-07-22 18:12:17 +02001988 mutex_lock_nested(&inode->i_mutex, I_MUTEX_QUOTA);
Jan Kara268157b2009-01-27 15:47:22 +01001989 oldflags = inode->i_flags & (S_NOATIME | S_IMMUTABLE |
1990 S_NOQUOTA);
Jan Karaca785ec2008-09-30 17:53:37 +02001991 inode->i_flags |= S_NOQUOTA | S_NOATIME | S_IMMUTABLE;
Jan Karadee86562009-07-22 18:12:17 +02001992 mutex_unlock(&inode->i_mutex);
Jan Kara26245c92010-01-06 17:20:35 +01001993 /*
1994 * When S_NOQUOTA is set, remove dquot references as no more
1995 * references can be added
1996 */
Jan Karaca785ec2008-09-30 17:53:37 +02001997 sb->dq_op->drop(inode);
1998 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999
2000 error = -EIO;
2001 dqopt->files[type] = igrab(inode);
2002 if (!dqopt->files[type])
2003 goto out_lock;
2004 error = -EINVAL;
2005 if (!fmt->qf_ops->check_quota_file(sb, type))
2006 goto out_file_init;
2007
2008 dqopt->ops[type] = fmt->qf_ops;
2009 dqopt->info[type].dqi_format = fmt;
Jan Kara0ff5af82008-04-28 02:14:33 -07002010 dqopt->info[type].dqi_fmt_id = format_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011 INIT_LIST_HEAD(&dqopt->info[type].dqi_dirty_list);
Ingo Molnard3be9152006-03-23 03:00:29 -08002012 mutex_lock(&dqopt->dqio_mutex);
Jan Kara268157b2009-01-27 15:47:22 +01002013 error = dqopt->ops[type]->read_file_info(sb, type);
2014 if (error < 0) {
Ingo Molnard3be9152006-03-23 03:00:29 -08002015 mutex_unlock(&dqopt->dqio_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016 goto out_file_init;
2017 }
Ingo Molnard3be9152006-03-23 03:00:29 -08002018 mutex_unlock(&dqopt->dqio_mutex);
Jan Karacc334122009-01-12 17:23:05 +01002019 spin_lock(&dq_state_lock);
Jan Karaf55abc02008-08-20 17:50:32 +02002020 dqopt->flags |= dquot_state_flag(flags, type);
Jan Karacc334122009-01-12 17:23:05 +01002021 spin_unlock(&dq_state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022
2023 add_dquot_ref(sb, type);
Ingo Molnard3be9152006-03-23 03:00:29 -08002024 mutex_unlock(&dqopt->dqonoff_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025
2026 return 0;
2027
2028out_file_init:
2029 dqopt->files[type] = NULL;
2030 iput(inode);
2031out_lock:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032 if (oldflags != -1) {
Jan Karadee86562009-07-22 18:12:17 +02002033 mutex_lock_nested(&inode->i_mutex, I_MUTEX_QUOTA);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034 /* Set the flags back (in the case of accidental quotaon()
2035 * on a wrong file we don't want to mess up the flags) */
2036 inode->i_flags &= ~(S_NOATIME | S_NOQUOTA | S_IMMUTABLE);
2037 inode->i_flags |= oldflags;
Jan Karadee86562009-07-22 18:12:17 +02002038 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039 }
Jiaying Zhangd01730d2009-07-07 18:15:21 +02002040 mutex_unlock(&dqopt->dqonoff_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041out_fmt:
2042 put_quota_format(fmt);
2043
2044 return error;
2045}
2046
Jan Kara0ff5af82008-04-28 02:14:33 -07002047/* Reenable quotas on remount RW */
2048static int vfs_quota_on_remount(struct super_block *sb, int type)
2049{
2050 struct quota_info *dqopt = sb_dqopt(sb);
2051 struct inode *inode;
2052 int ret;
Jan Karaf55abc02008-08-20 17:50:32 +02002053 unsigned int flags;
Jan Kara0ff5af82008-04-28 02:14:33 -07002054
2055 mutex_lock(&dqopt->dqonoff_mutex);
2056 if (!sb_has_quota_suspended(sb, type)) {
2057 mutex_unlock(&dqopt->dqonoff_mutex);
2058 return 0;
2059 }
Jan Kara0ff5af82008-04-28 02:14:33 -07002060 inode = dqopt->files[type];
2061 dqopt->files[type] = NULL;
Jan Karacc334122009-01-12 17:23:05 +01002062 spin_lock(&dq_state_lock);
Jan Karaf55abc02008-08-20 17:50:32 +02002063 flags = dqopt->flags & dquot_state_flag(DQUOT_USAGE_ENABLED |
2064 DQUOT_LIMITS_ENABLED, type);
2065 dqopt->flags &= ~dquot_state_flag(DQUOT_STATE_FLAGS, type);
Jan Karacc334122009-01-12 17:23:05 +01002066 spin_unlock(&dq_state_lock);
Jan Kara0ff5af82008-04-28 02:14:33 -07002067 mutex_unlock(&dqopt->dqonoff_mutex);
2068
Jan Karaf55abc02008-08-20 17:50:32 +02002069 flags = dquot_generic_flag(flags, type);
2070 ret = vfs_load_quota_inode(inode, type, dqopt->info[type].dqi_fmt_id,
2071 flags);
Jan Kara0ff5af82008-04-28 02:14:33 -07002072 iput(inode);
2073
2074 return ret;
2075}
2076
Al Viro77e69da2008-08-01 04:29:18 -04002077int vfs_quota_on_path(struct super_block *sb, int type, int format_id,
2078 struct path *path)
2079{
2080 int error = security_quota_on(path->dentry);
2081 if (error)
2082 return error;
2083 /* Quota file not on the same filesystem? */
2084 if (path->mnt->mnt_sb != sb)
2085 error = -EXDEV;
2086 else
Jan Karaf55abc02008-08-20 17:50:32 +02002087 error = vfs_load_quota_inode(path->dentry->d_inode, type,
2088 format_id, DQUOT_USAGE_ENABLED |
2089 DQUOT_LIMITS_ENABLED);
Al Viro77e69da2008-08-01 04:29:18 -04002090 return error;
2091}
Mingming Cao08d03502009-01-14 16:19:32 +01002092EXPORT_SYMBOL(vfs_quota_on_path);
Al Viro77e69da2008-08-01 04:29:18 -04002093
Al Viro82646132008-08-02 00:57:06 -04002094int vfs_quota_on(struct super_block *sb, int type, int format_id, char *name,
Jan Kara0ff5af82008-04-28 02:14:33 -07002095 int remount)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096{
Al Viro82646132008-08-02 00:57:06 -04002097 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098 int error;
2099
Jan Kara0ff5af82008-04-28 02:14:33 -07002100 if (remount)
2101 return vfs_quota_on_remount(sb, type);
2102
Al Viro82646132008-08-02 00:57:06 -04002103 error = kern_path(name, LOOKUP_FOLLOW, &path);
Al Viro77e69da2008-08-01 04:29:18 -04002104 if (!error) {
Al Viro82646132008-08-02 00:57:06 -04002105 error = vfs_quota_on_path(sb, type, format_id, &path);
2106 path_put(&path);
Al Viro77e69da2008-08-01 04:29:18 -04002107 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108 return error;
2109}
Mingming Cao08d03502009-01-14 16:19:32 +01002110EXPORT_SYMBOL(vfs_quota_on);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111
2112/*
Jan Karaf55abc02008-08-20 17:50:32 +02002113 * More powerful function for turning on quotas allowing setting
2114 * of individual quota flags
2115 */
2116int vfs_quota_enable(struct inode *inode, int type, int format_id,
2117 unsigned int flags)
2118{
2119 int ret = 0;
2120 struct super_block *sb = inode->i_sb;
2121 struct quota_info *dqopt = sb_dqopt(sb);
2122
2123 /* Just unsuspend quotas? */
2124 if (flags & DQUOT_SUSPENDED)
2125 return vfs_quota_on_remount(sb, type);
2126 if (!flags)
2127 return 0;
2128 /* Just updating flags needed? */
2129 if (sb_has_quota_loaded(sb, type)) {
2130 mutex_lock(&dqopt->dqonoff_mutex);
2131 /* Now do a reliable test... */
2132 if (!sb_has_quota_loaded(sb, type)) {
2133 mutex_unlock(&dqopt->dqonoff_mutex);
2134 goto load_quota;
2135 }
2136 if (flags & DQUOT_USAGE_ENABLED &&
2137 sb_has_quota_usage_enabled(sb, type)) {
2138 ret = -EBUSY;
2139 goto out_lock;
2140 }
2141 if (flags & DQUOT_LIMITS_ENABLED &&
2142 sb_has_quota_limits_enabled(sb, type)) {
2143 ret = -EBUSY;
2144 goto out_lock;
2145 }
Jan Karacc334122009-01-12 17:23:05 +01002146 spin_lock(&dq_state_lock);
Jan Karaf55abc02008-08-20 17:50:32 +02002147 sb_dqopt(sb)->flags |= dquot_state_flag(flags, type);
Jan Karacc334122009-01-12 17:23:05 +01002148 spin_unlock(&dq_state_lock);
Jan Karaf55abc02008-08-20 17:50:32 +02002149out_lock:
2150 mutex_unlock(&dqopt->dqonoff_mutex);
2151 return ret;
2152 }
2153
2154load_quota:
2155 return vfs_load_quota_inode(inode, type, format_id, flags);
2156}
Mingming Cao08d03502009-01-14 16:19:32 +01002157EXPORT_SYMBOL(vfs_quota_enable);
Jan Karaf55abc02008-08-20 17:50:32 +02002158
2159/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160 * This function is used when filesystem needs to initialize quotas
2161 * during mount time.
2162 */
Christoph Hellwig84de8562005-06-23 00:09:16 -07002163int vfs_quota_on_mount(struct super_block *sb, char *qf_name,
2164 int format_id, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165{
Christoph Hellwig84de8562005-06-23 00:09:16 -07002166 struct dentry *dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167 int error;
2168
Jan Karac56818d2009-11-12 15:42:08 +01002169 mutex_lock(&sb->s_root->d_inode->i_mutex);
Christoph Hellwig2fa389c2005-06-23 00:09:16 -07002170 dentry = lookup_one_len(qf_name, sb->s_root, strlen(qf_name));
Jan Karac56818d2009-11-12 15:42:08 +01002171 mutex_unlock(&sb->s_root->d_inode->i_mutex);
Christoph Hellwig84de8562005-06-23 00:09:16 -07002172 if (IS_ERR(dentry))
2173 return PTR_ERR(dentry);
2174
Jan Kara154f4842005-11-28 13:44:14 -08002175 if (!dentry->d_inode) {
2176 error = -ENOENT;
2177 goto out;
2178 }
2179
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180 error = security_quota_on(dentry);
Christoph Hellwig84de8562005-06-23 00:09:16 -07002181 if (!error)
Jan Karaf55abc02008-08-20 17:50:32 +02002182 error = vfs_load_quota_inode(dentry->d_inode, type, format_id,
2183 DQUOT_USAGE_ENABLED | DQUOT_LIMITS_ENABLED);
Christoph Hellwig84de8562005-06-23 00:09:16 -07002184
Jan Kara154f4842005-11-28 13:44:14 -08002185out:
Christoph Hellwig84de8562005-06-23 00:09:16 -07002186 dput(dentry);
2187 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188}
Mingming Cao08d03502009-01-14 16:19:32 +01002189EXPORT_SYMBOL(vfs_quota_on_mount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190
Jan Karab85f4b82008-07-25 01:46:50 -07002191/* Wrapper to turn on quotas when remounting rw */
2192int vfs_dq_quota_on_remount(struct super_block *sb)
2193{
2194 int cnt;
2195 int ret = 0, err;
2196
2197 if (!sb->s_qcop || !sb->s_qcop->quota_on)
2198 return -ENOSYS;
2199 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
2200 err = sb->s_qcop->quota_on(sb, cnt, 0, NULL, 1);
2201 if (err < 0 && !ret)
2202 ret = err;
2203 }
2204 return ret;
2205}
Mingming Cao08d03502009-01-14 16:19:32 +01002206EXPORT_SYMBOL(vfs_dq_quota_on_remount);
Jan Karab85f4b82008-07-25 01:46:50 -07002207
Jan Kara12095462008-08-20 14:45:12 +02002208static inline qsize_t qbtos(qsize_t blocks)
2209{
2210 return blocks << QIF_DQBLKSIZE_BITS;
2211}
2212
2213static inline qsize_t stoqb(qsize_t space)
2214{
2215 return (space + QIF_DQBLKSIZE - 1) >> QIF_DQBLKSIZE_BITS;
2216}
2217
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218/* Generic routine for getting common part of quota structure */
2219static void do_get_dqblk(struct dquot *dquot, struct if_dqblk *di)
2220{
2221 struct mem_dqblk *dm = &dquot->dq_dqb;
2222
2223 spin_lock(&dq_data_lock);
Jan Kara12095462008-08-20 14:45:12 +02002224 di->dqb_bhardlimit = stoqb(dm->dqb_bhardlimit);
2225 di->dqb_bsoftlimit = stoqb(dm->dqb_bsoftlimit);
Mingming Caof18df222009-01-13 16:43:09 +01002226 di->dqb_curspace = dm->dqb_curspace + dm->dqb_rsvspace;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227 di->dqb_ihardlimit = dm->dqb_ihardlimit;
2228 di->dqb_isoftlimit = dm->dqb_isoftlimit;
2229 di->dqb_curinodes = dm->dqb_curinodes;
2230 di->dqb_btime = dm->dqb_btime;
2231 di->dqb_itime = dm->dqb_itime;
2232 di->dqb_valid = QIF_ALL;
2233 spin_unlock(&dq_data_lock);
2234}
2235
Jan Kara268157b2009-01-27 15:47:22 +01002236int vfs_get_dqblk(struct super_block *sb, int type, qid_t id,
2237 struct if_dqblk *di)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238{
2239 struct dquot *dquot;
2240
Jan Karacc334122009-01-12 17:23:05 +01002241 dquot = dqget(sb, id, type);
Jan Karadd6f3c62009-01-26 16:01:43 +01002242 if (!dquot)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243 return -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244 do_get_dqblk(dquot, di);
2245 dqput(dquot);
Jan Karacc334122009-01-12 17:23:05 +01002246
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247 return 0;
2248}
Mingming Cao08d03502009-01-14 16:19:32 +01002249EXPORT_SYMBOL(vfs_get_dqblk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250
2251/* Generic routine for setting common part of quota structure */
Andrew Perepechko338bf9a2008-04-28 02:14:31 -07002252static int do_set_dqblk(struct dquot *dquot, struct if_dqblk *di)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253{
2254 struct mem_dqblk *dm = &dquot->dq_dqb;
2255 int check_blim = 0, check_ilim = 0;
Andrew Perepechko338bf9a2008-04-28 02:14:31 -07002256 struct mem_dqinfo *dqi = &sb_dqopt(dquot->dq_sb)->info[dquot->dq_type];
2257
2258 if ((di->dqb_valid & QIF_BLIMITS &&
2259 (di->dqb_bhardlimit > dqi->dqi_maxblimit ||
2260 di->dqb_bsoftlimit > dqi->dqi_maxblimit)) ||
2261 (di->dqb_valid & QIF_ILIMITS &&
2262 (di->dqb_ihardlimit > dqi->dqi_maxilimit ||
2263 di->dqb_isoftlimit > dqi->dqi_maxilimit)))
2264 return -ERANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265
2266 spin_lock(&dq_data_lock);
2267 if (di->dqb_valid & QIF_SPACE) {
Mingming Caof18df222009-01-13 16:43:09 +01002268 dm->dqb_curspace = di->dqb_curspace - dm->dqb_rsvspace;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269 check_blim = 1;
Jan Kara4d59bce2008-10-02 16:48:10 +02002270 __set_bit(DQ_LASTSET_B + QIF_SPACE_B, &dquot->dq_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271 }
2272 if (di->dqb_valid & QIF_BLIMITS) {
Jan Kara12095462008-08-20 14:45:12 +02002273 dm->dqb_bsoftlimit = qbtos(di->dqb_bsoftlimit);
2274 dm->dqb_bhardlimit = qbtos(di->dqb_bhardlimit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275 check_blim = 1;
Jan Kara4d59bce2008-10-02 16:48:10 +02002276 __set_bit(DQ_LASTSET_B + QIF_BLIMITS_B, &dquot->dq_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277 }
2278 if (di->dqb_valid & QIF_INODES) {
2279 dm->dqb_curinodes = di->dqb_curinodes;
2280 check_ilim = 1;
Jan Kara4d59bce2008-10-02 16:48:10 +02002281 __set_bit(DQ_LASTSET_B + QIF_INODES_B, &dquot->dq_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282 }
2283 if (di->dqb_valid & QIF_ILIMITS) {
2284 dm->dqb_isoftlimit = di->dqb_isoftlimit;
2285 dm->dqb_ihardlimit = di->dqb_ihardlimit;
2286 check_ilim = 1;
Jan Kara4d59bce2008-10-02 16:48:10 +02002287 __set_bit(DQ_LASTSET_B + QIF_ILIMITS_B, &dquot->dq_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288 }
Jan Kara4d59bce2008-10-02 16:48:10 +02002289 if (di->dqb_valid & QIF_BTIME) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290 dm->dqb_btime = di->dqb_btime;
Jan Karae04a88a92009-01-07 18:07:29 -08002291 check_blim = 1;
Jan Kara4d59bce2008-10-02 16:48:10 +02002292 __set_bit(DQ_LASTSET_B + QIF_BTIME_B, &dquot->dq_flags);
2293 }
2294 if (di->dqb_valid & QIF_ITIME) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295 dm->dqb_itime = di->dqb_itime;
Jan Karae04a88a92009-01-07 18:07:29 -08002296 check_ilim = 1;
Jan Kara4d59bce2008-10-02 16:48:10 +02002297 __set_bit(DQ_LASTSET_B + QIF_ITIME_B, &dquot->dq_flags);
2298 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299
2300 if (check_blim) {
Jan Kara268157b2009-01-27 15:47:22 +01002301 if (!dm->dqb_bsoftlimit ||
2302 dm->dqb_curspace < dm->dqb_bsoftlimit) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303 dm->dqb_btime = 0;
2304 clear_bit(DQ_BLKS_B, &dquot->dq_flags);
Jan Kara268157b2009-01-27 15:47:22 +01002305 } else if (!(di->dqb_valid & QIF_BTIME))
2306 /* Set grace only if user hasn't provided his own... */
Andrew Perepechko338bf9a2008-04-28 02:14:31 -07002307 dm->dqb_btime = get_seconds() + dqi->dqi_bgrace;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308 }
2309 if (check_ilim) {
Jan Kara268157b2009-01-27 15:47:22 +01002310 if (!dm->dqb_isoftlimit ||
2311 dm->dqb_curinodes < dm->dqb_isoftlimit) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312 dm->dqb_itime = 0;
2313 clear_bit(DQ_INODES_B, &dquot->dq_flags);
Jan Kara268157b2009-01-27 15:47:22 +01002314 } else if (!(di->dqb_valid & QIF_ITIME))
2315 /* Set grace only if user hasn't provided his own... */
Andrew Perepechko338bf9a2008-04-28 02:14:31 -07002316 dm->dqb_itime = get_seconds() + dqi->dqi_igrace;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317 }
Jan Kara268157b2009-01-27 15:47:22 +01002318 if (dm->dqb_bhardlimit || dm->dqb_bsoftlimit || dm->dqb_ihardlimit ||
2319 dm->dqb_isoftlimit)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320 clear_bit(DQ_FAKE_B, &dquot->dq_flags);
2321 else
2322 set_bit(DQ_FAKE_B, &dquot->dq_flags);
2323 spin_unlock(&dq_data_lock);
2324 mark_dquot_dirty(dquot);
Andrew Perepechko338bf9a2008-04-28 02:14:31 -07002325
2326 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327}
2328
Jan Kara268157b2009-01-27 15:47:22 +01002329int vfs_set_dqblk(struct super_block *sb, int type, qid_t id,
2330 struct if_dqblk *di)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331{
2332 struct dquot *dquot;
Andrew Perepechko338bf9a2008-04-28 02:14:31 -07002333 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334
Jan Karaf55abc02008-08-20 17:50:32 +02002335 dquot = dqget(sb, id, type);
2336 if (!dquot) {
2337 rc = -ESRCH;
2338 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339 }
Andrew Perepechko338bf9a2008-04-28 02:14:31 -07002340 rc = do_set_dqblk(dquot, di);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341 dqput(dquot);
Jan Karaf55abc02008-08-20 17:50:32 +02002342out:
Andrew Perepechko338bf9a2008-04-28 02:14:31 -07002343 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344}
Mingming Cao08d03502009-01-14 16:19:32 +01002345EXPORT_SYMBOL(vfs_set_dqblk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346
2347/* Generic routine for getting common part of quota file information */
2348int vfs_get_dqinfo(struct super_block *sb, int type, struct if_dqinfo *ii)
2349{
2350 struct mem_dqinfo *mi;
2351
Ingo Molnard3be9152006-03-23 03:00:29 -08002352 mutex_lock(&sb_dqopt(sb)->dqonoff_mutex);
Jan Karaf55abc02008-08-20 17:50:32 +02002353 if (!sb_has_quota_active(sb, type)) {
Ingo Molnard3be9152006-03-23 03:00:29 -08002354 mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002355 return -ESRCH;
2356 }
2357 mi = sb_dqopt(sb)->info + type;
2358 spin_lock(&dq_data_lock);
2359 ii->dqi_bgrace = mi->dqi_bgrace;
2360 ii->dqi_igrace = mi->dqi_igrace;
2361 ii->dqi_flags = mi->dqi_flags & DQF_MASK;
2362 ii->dqi_valid = IIF_ALL;
2363 spin_unlock(&dq_data_lock);
Ingo Molnard3be9152006-03-23 03:00:29 -08002364 mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365 return 0;
2366}
Mingming Cao08d03502009-01-14 16:19:32 +01002367EXPORT_SYMBOL(vfs_get_dqinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002368
2369/* Generic routine for setting common part of quota file information */
2370int vfs_set_dqinfo(struct super_block *sb, int type, struct if_dqinfo *ii)
2371{
2372 struct mem_dqinfo *mi;
Jan Karaf55abc02008-08-20 17:50:32 +02002373 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374
Ingo Molnard3be9152006-03-23 03:00:29 -08002375 mutex_lock(&sb_dqopt(sb)->dqonoff_mutex);
Jan Karaf55abc02008-08-20 17:50:32 +02002376 if (!sb_has_quota_active(sb, type)) {
2377 err = -ESRCH;
2378 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002379 }
2380 mi = sb_dqopt(sb)->info + type;
2381 spin_lock(&dq_data_lock);
2382 if (ii->dqi_valid & IIF_BGRACE)
2383 mi->dqi_bgrace = ii->dqi_bgrace;
2384 if (ii->dqi_valid & IIF_IGRACE)
2385 mi->dqi_igrace = ii->dqi_igrace;
2386 if (ii->dqi_valid & IIF_FLAGS)
Jan Kara268157b2009-01-27 15:47:22 +01002387 mi->dqi_flags = (mi->dqi_flags & ~DQF_MASK) |
2388 (ii->dqi_flags & DQF_MASK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002389 spin_unlock(&dq_data_lock);
2390 mark_info_dirty(sb, type);
2391 /* Force write to disk */
2392 sb->dq_op->write_info(sb, type);
Jan Karaf55abc02008-08-20 17:50:32 +02002393out:
Ingo Molnard3be9152006-03-23 03:00:29 -08002394 mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
Jan Karaf55abc02008-08-20 17:50:32 +02002395 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002396}
Mingming Cao08d03502009-01-14 16:19:32 +01002397EXPORT_SYMBOL(vfs_set_dqinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398
Alexey Dobriyan0d54b212009-09-21 17:01:09 -07002399const struct quotactl_ops vfs_quotactl_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002400 .quota_on = vfs_quota_on,
2401 .quota_off = vfs_quota_off,
2402 .quota_sync = vfs_quota_sync,
2403 .get_info = vfs_get_dqinfo,
2404 .set_info = vfs_set_dqinfo,
2405 .get_dqblk = vfs_get_dqblk,
2406 .set_dqblk = vfs_set_dqblk
2407};
2408
2409static ctl_table fs_dqstats_table[] = {
2410 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002411 .procname = "lookups",
2412 .data = &dqstats.lookups,
2413 .maxlen = sizeof(int),
2414 .mode = 0444,
Eric W. Biederman6d456112009-11-16 03:11:48 -08002415 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002416 },
2417 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002418 .procname = "drops",
2419 .data = &dqstats.drops,
2420 .maxlen = sizeof(int),
2421 .mode = 0444,
Eric W. Biederman6d456112009-11-16 03:11:48 -08002422 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002423 },
2424 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002425 .procname = "reads",
2426 .data = &dqstats.reads,
2427 .maxlen = sizeof(int),
2428 .mode = 0444,
Eric W. Biederman6d456112009-11-16 03:11:48 -08002429 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002430 },
2431 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002432 .procname = "writes",
2433 .data = &dqstats.writes,
2434 .maxlen = sizeof(int),
2435 .mode = 0444,
Eric W. Biederman6d456112009-11-16 03:11:48 -08002436 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002437 },
2438 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439 .procname = "cache_hits",
2440 .data = &dqstats.cache_hits,
2441 .maxlen = sizeof(int),
2442 .mode = 0444,
Eric W. Biederman6d456112009-11-16 03:11:48 -08002443 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444 },
2445 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002446 .procname = "allocated_dquots",
2447 .data = &dqstats.allocated_dquots,
2448 .maxlen = sizeof(int),
2449 .mode = 0444,
Eric W. Biederman6d456112009-11-16 03:11:48 -08002450 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451 },
2452 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453 .procname = "free_dquots",
2454 .data = &dqstats.free_dquots,
2455 .maxlen = sizeof(int),
2456 .mode = 0444,
Eric W. Biederman6d456112009-11-16 03:11:48 -08002457 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458 },
2459 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002460 .procname = "syncs",
2461 .data = &dqstats.syncs,
2462 .maxlen = sizeof(int),
2463 .mode = 0444,
Eric W. Biederman6d456112009-11-16 03:11:48 -08002464 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002465 },
Jan Kara8e893462007-10-16 23:29:31 -07002466#ifdef CONFIG_PRINT_QUOTA_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07002467 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468 .procname = "warnings",
2469 .data = &flag_print_warnings,
2470 .maxlen = sizeof(int),
2471 .mode = 0644,
Eric W. Biederman6d456112009-11-16 03:11:48 -08002472 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473 },
Jan Kara8e893462007-10-16 23:29:31 -07002474#endif
Eric W. Biedermanab092032009-11-05 14:25:10 -08002475 { },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002476};
2477
2478static ctl_table fs_table[] = {
2479 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002480 .procname = "quota",
2481 .mode = 0555,
2482 .child = fs_dqstats_table,
2483 },
Eric W. Biedermanab092032009-11-05 14:25:10 -08002484 { },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002485};
2486
2487static ctl_table sys_table[] = {
2488 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002489 .procname = "fs",
2490 .mode = 0555,
2491 .child = fs_table,
2492 },
Eric W. Biedermanab092032009-11-05 14:25:10 -08002493 { },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002494};
2495
2496static int __init dquot_init(void)
2497{
2498 int i;
2499 unsigned long nr_hash, order;
2500
2501 printk(KERN_NOTICE "VFS: Disk quotas %s\n", __DQUOT_VERSION__);
2502
Eric W. Biederman0b4d4142007-02-14 00:34:09 -08002503 register_sysctl_table(sys_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002504
Paul Mundt20c2df82007-07-20 10:11:58 +09002505 dquot_cachep = kmem_cache_create("dquot",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002506 sizeof(struct dquot), sizeof(unsigned long) * 4,
Paul Jacksonfffb60f2006-03-24 03:16:06 -08002507 (SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT|
2508 SLAB_MEM_SPREAD|SLAB_PANIC),
Paul Mundt20c2df82007-07-20 10:11:58 +09002509 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002510
2511 order = 0;
2512 dquot_hash = (struct hlist_head *)__get_free_pages(GFP_ATOMIC, order);
2513 if (!dquot_hash)
2514 panic("Cannot create dquot hash table");
2515
2516 /* Find power-of-two hlist_heads which can fit into allocation */
2517 nr_hash = (1UL << order) * PAGE_SIZE / sizeof(struct hlist_head);
2518 dq_hash_bits = 0;
2519 do {
2520 dq_hash_bits++;
2521 } while (nr_hash >> dq_hash_bits);
2522 dq_hash_bits--;
2523
2524 nr_hash = 1UL << dq_hash_bits;
2525 dq_hash_mask = nr_hash - 1;
2526 for (i = 0; i < nr_hash; i++)
2527 INIT_HLIST_HEAD(dquot_hash + i);
2528
2529 printk("Dquot-cache hash table entries: %ld (order %ld, %ld bytes)\n",
2530 nr_hash, order, (PAGE_SIZE << order));
2531
Rusty Russell8e1f9362007-07-17 04:03:17 -07002532 register_shrinker(&dqcache_shrinker);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002533
Linus Torvalds1da177e2005-04-16 15:20:36 -07002534 return 0;
2535}
2536module_init(dquot_init);