quota: Remove uppercase aliases for quota functions.
[linux-2.6-block.git] / fs / quota / dquot.c
CommitLineData
1da177e4
LT
1/*
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 *
1da177e4
LT
12 * 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>
1da177e4
LT
70#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>
16f7e0fe 77#include <linux/capability.h>
be586bab 78#include <linux/quotaops.h>
fb58b731 79#include <linux/writeback.h> /* for inode_lock, oddly enough.. */
8e893469
JK
80#ifdef CONFIG_QUOTA_NETLINK_INTERFACE
81#include <net/netlink.h>
82#include <net/genetlink.h>
83#endif
1da177e4
LT
84
85#include <asm/uaccess.h>
86
87#define __DQUOT_PARANOIA
88
89/*
cc33412f
JK
90 * There are three quota SMP locks. dq_list_lock protects all lists with quotas
91 * and quota formats, dqstats structure containing statistics about the lists
92 * dq_data_lock protects data from dq_dqb and also mem_dqinfo structures and
93 * also guards consistency of dquot->dq_dqb with inode->i_blocks, i_bytes.
1da177e4 94 * i_blocks and i_bytes updates itself are guarded by i_lock acquired directly
cc33412f
JK
95 * in inode_add_bytes() and inode_sub_bytes(). dq_state_lock protects
96 * modifications of quota state (on quotaon and quotaoff) and readers who care
97 * about latest values take it as well.
1da177e4 98 *
cc33412f
JK
99 * The spinlock ordering is hence: dq_data_lock > dq_list_lock > i_lock,
100 * dq_list_lock > dq_state_lock
1da177e4
LT
101 *
102 * Note that some things (eg. sb pointer, type, id) doesn't change during
103 * the life of the dquot structure and so needn't to be protected by a lock
104 *
105 * Any operation working on dquots via inode pointers must hold dqptr_sem. If
106 * operation is just reading pointers from inode (or not using them at all) the
107 * read lock is enough. If pointers are altered function must hold write lock
108 * (these locking rules also apply for S_NOQUOTA flag in the inode - note that
cc33412f 109 * for altering the flag i_mutex is also needed).
1da177e4 110 *
d3be915f 111 * Each dquot has its dq_lock mutex. Locked dquots might not be referenced
1da177e4
LT
112 * 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:
d3be915f
IM
121 * i_mutex > dqonoff_sem > journal_lock > dqptr_sem > dquot->dq_lock >
122 * dqio_mutex
cc33412f
JK
123 * 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.
d3be915f 129 * i_mutex on quota files is special (it's below dqio_mutex)
1da177e4
LT
130 */
131
c516610c
JK
132static __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);
08d0350c 135EXPORT_SYMBOL(dq_data_lock);
1da177e4
LT
136
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 */
e18b890b 142static struct kmem_cache *dquot_cachep;
1da177e4
LT
143
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}
08d0350c 152EXPORT_SYMBOL(register_quota_format);
1da177e4
LT
153
154void unregister_quota_format(struct quota_format_type *fmt)
155{
156 struct quota_format_type **actqf;
157
158 spin_lock(&dq_list_lock);
159 for (actqf = &quota_formats; *actqf && *actqf != fmt; actqf = &(*actqf)->qf_next);
160 if (*actqf)
161 *actqf = (*actqf)->qf_next;
162 spin_unlock(&dq_list_lock);
163}
08d0350c 164EXPORT_SYMBOL(unregister_quota_format);
1da177e4
LT
165
166static struct quota_format_type *find_quota_format(int id)
167{
168 struct quota_format_type *actqf;
169
170 spin_lock(&dq_list_lock);
171 for (actqf = quota_formats; actqf && actqf->qf_fmt_id != id; actqf = actqf->qf_next);
172 if (!actqf || !try_module_get(actqf->qf_owner)) {
173 int qm;
174
175 spin_unlock(&dq_list_lock);
176
177 for (qm = 0; module_names[qm].qm_fmt_id && module_names[qm].qm_fmt_id != id; qm++);
178 if (!module_names[qm].qm_fmt_id || request_module(module_names[qm].qm_mod_name))
179 return NULL;
180
181 spin_lock(&dq_list_lock);
182 for (actqf = quota_formats; actqf && actqf->qf_fmt_id != id; actqf = actqf->qf_next);
183 if (actqf && !try_module_get(actqf->qf_owner))
184 actqf = NULL;
185 }
186 spin_unlock(&dq_list_lock);
187 return actqf;
188}
189
190static void put_quota_format(struct quota_format_type *fmt)
191{
192 module_put(fmt->qf_owner);
193}
194
195/*
196 * Dquot List Management:
197 * The quota code uses three lists for dquot management: the inuse_list,
198 * free_dquots, and dquot_hash[] array. A single dquot structure may be
199 * on all three lists, depending on its current state.
200 *
201 * All dquots are placed to the end of inuse_list when first created, and this
202 * list is used for invalidate operation, which must look at every dquot.
203 *
204 * Unused dquots (dq_count == 0) are added to the free_dquots list when freed,
205 * and this list is searched whenever we need an available dquot. Dquots are
206 * removed from the list as soon as they are used again, and
207 * dqstats.free_dquots gives the number of dquots on the list. When
208 * dquot is invalidated it's completely released from memory.
209 *
210 * Dquots with a specific identity (device, type and id) are placed on
211 * one of the dquot_hash[] hash chains. The provides an efficient search
212 * mechanism to locate a specific dquot.
213 */
214
215static LIST_HEAD(inuse_list);
216static LIST_HEAD(free_dquots);
217static unsigned int dq_hash_bits, dq_hash_mask;
218static struct hlist_head *dquot_hash;
219
220struct dqstats dqstats;
08d0350c 221EXPORT_SYMBOL(dqstats);
1da177e4 222
1da177e4
LT
223static inline unsigned int
224hashfn(const struct super_block *sb, unsigned int id, int type)
225{
226 unsigned long tmp;
227
228 tmp = (((unsigned long)sb>>L1_CACHE_SHIFT) ^ id) * (MAXQUOTAS - type);
229 return (tmp + (tmp >> dq_hash_bits)) & dq_hash_mask;
230}
231
232/*
233 * Following list functions expect dq_list_lock to be held
234 */
235static inline void insert_dquot_hash(struct dquot *dquot)
236{
237 struct hlist_head *head = dquot_hash + hashfn(dquot->dq_sb, dquot->dq_id, dquot->dq_type);
238 hlist_add_head(&dquot->dq_hash, head);
239}
240
241static inline void remove_dquot_hash(struct dquot *dquot)
242{
243 hlist_del_init(&dquot->dq_hash);
244}
245
246static inline struct dquot *find_dquot(unsigned int hashent, struct super_block *sb, unsigned int id, int type)
247{
248 struct hlist_node *node;
249 struct dquot *dquot;
250
251 hlist_for_each (node, dquot_hash+hashent) {
252 dquot = hlist_entry(node, struct dquot, dq_hash);
253 if (dquot->dq_sb == sb && dquot->dq_id == id && dquot->dq_type == type)
254 return dquot;
255 }
dd6f3c6d 256 return NULL;
1da177e4
LT
257}
258
259/* Add a dquot to the tail of the free list */
260static inline void put_dquot_last(struct dquot *dquot)
261{
8e13059a 262 list_add_tail(&dquot->dq_free, &free_dquots);
1da177e4
LT
263 dqstats.free_dquots++;
264}
265
266static inline void remove_free_dquot(struct dquot *dquot)
267{
268 if (list_empty(&dquot->dq_free))
269 return;
270 list_del_init(&dquot->dq_free);
271 dqstats.free_dquots--;
272}
273
274static inline void put_inuse(struct dquot *dquot)
275{
276 /* We add to the back of inuse list so we don't have to restart
277 * when traversing this list and we block */
8e13059a 278 list_add_tail(&dquot->dq_inuse, &inuse_list);
1da177e4
LT
279 dqstats.allocated_dquots++;
280}
281
282static inline void remove_inuse(struct dquot *dquot)
283{
284 dqstats.allocated_dquots--;
285 list_del(&dquot->dq_inuse);
286}
287/*
288 * End of list functions needing dq_list_lock
289 */
290
291static void wait_on_dquot(struct dquot *dquot)
292{
d3be915f
IM
293 mutex_lock(&dquot->dq_lock);
294 mutex_unlock(&dquot->dq_lock);
1da177e4
LT
295}
296
03f6e92b
JK
297static inline int dquot_dirty(struct dquot *dquot)
298{
299 return test_bit(DQ_MOD_B, &dquot->dq_flags);
300}
301
302static inline int mark_dquot_dirty(struct dquot *dquot)
303{
304 return dquot->dq_sb->dq_op->mark_dirty(dquot);
305}
1da177e4
LT
306
307int dquot_mark_dquot_dirty(struct dquot *dquot)
308{
309 spin_lock(&dq_list_lock);
310 if (!test_and_set_bit(DQ_MOD_B, &dquot->dq_flags))
311 list_add(&dquot->dq_dirty, &sb_dqopt(dquot->dq_sb)->
312 info[dquot->dq_type].dqi_dirty_list);
313 spin_unlock(&dq_list_lock);
314 return 0;
315}
08d0350c 316EXPORT_SYMBOL(dquot_mark_dquot_dirty);
1da177e4
LT
317
318/* This function needs dq_list_lock */
319static inline int clear_dquot_dirty(struct dquot *dquot)
320{
321 if (!test_and_clear_bit(DQ_MOD_B, &dquot->dq_flags))
322 return 0;
323 list_del_init(&dquot->dq_dirty);
324 return 1;
325}
326
327void mark_info_dirty(struct super_block *sb, int type)
328{
329 set_bit(DQF_INFO_DIRTY_B, &sb_dqopt(sb)->info[type].dqi_flags);
330}
331EXPORT_SYMBOL(mark_info_dirty);
332
333/*
334 * Read dquot from disk and alloc space for it
335 */
336
337int dquot_acquire(struct dquot *dquot)
338{
339 int ret = 0, ret2 = 0;
340 struct quota_info *dqopt = sb_dqopt(dquot->dq_sb);
341
d3be915f
IM
342 mutex_lock(&dquot->dq_lock);
343 mutex_lock(&dqopt->dqio_mutex);
1da177e4
LT
344 if (!test_bit(DQ_READ_B, &dquot->dq_flags))
345 ret = dqopt->ops[dquot->dq_type]->read_dqblk(dquot);
346 if (ret < 0)
347 goto out_iolock;
348 set_bit(DQ_READ_B, &dquot->dq_flags);
349 /* Instantiate dquot if needed */
350 if (!test_bit(DQ_ACTIVE_B, &dquot->dq_flags) && !dquot->dq_off) {
351 ret = dqopt->ops[dquot->dq_type]->commit_dqblk(dquot);
352 /* Write the info if needed */
353 if (info_dirty(&dqopt->info[dquot->dq_type]))
354 ret2 = dqopt->ops[dquot->dq_type]->write_file_info(dquot->dq_sb, dquot->dq_type);
355 if (ret < 0)
356 goto out_iolock;
357 if (ret2 < 0) {
358 ret = ret2;
359 goto out_iolock;
360 }
361 }
362 set_bit(DQ_ACTIVE_B, &dquot->dq_flags);
363out_iolock:
d3be915f
IM
364 mutex_unlock(&dqopt->dqio_mutex);
365 mutex_unlock(&dquot->dq_lock);
1da177e4
LT
366 return ret;
367}
08d0350c 368EXPORT_SYMBOL(dquot_acquire);
1da177e4
LT
369
370/*
371 * Write dquot to disk
372 */
373int dquot_commit(struct dquot *dquot)
374{
375 int ret = 0, ret2 = 0;
376 struct quota_info *dqopt = sb_dqopt(dquot->dq_sb);
377
d3be915f 378 mutex_lock(&dqopt->dqio_mutex);
1da177e4
LT
379 spin_lock(&dq_list_lock);
380 if (!clear_dquot_dirty(dquot)) {
381 spin_unlock(&dq_list_lock);
382 goto out_sem;
383 }
384 spin_unlock(&dq_list_lock);
385 /* Inactive dquot can be only if there was error during read/init
386 * => we have better not writing it */
387 if (test_bit(DQ_ACTIVE_B, &dquot->dq_flags)) {
388 ret = dqopt->ops[dquot->dq_type]->commit_dqblk(dquot);
389 if (info_dirty(&dqopt->info[dquot->dq_type]))
390 ret2 = dqopt->ops[dquot->dq_type]->write_file_info(dquot->dq_sb, dquot->dq_type);
391 if (ret >= 0)
392 ret = ret2;
393 }
394out_sem:
d3be915f 395 mutex_unlock(&dqopt->dqio_mutex);
1da177e4
LT
396 return ret;
397}
08d0350c 398EXPORT_SYMBOL(dquot_commit);
1da177e4
LT
399
400/*
401 * Release dquot
402 */
403int dquot_release(struct dquot *dquot)
404{
405 int ret = 0, ret2 = 0;
406 struct quota_info *dqopt = sb_dqopt(dquot->dq_sb);
407
d3be915f 408 mutex_lock(&dquot->dq_lock);
1da177e4
LT
409 /* Check whether we are not racing with some other dqget() */
410 if (atomic_read(&dquot->dq_count) > 1)
411 goto out_dqlock;
d3be915f 412 mutex_lock(&dqopt->dqio_mutex);
1da177e4
LT
413 if (dqopt->ops[dquot->dq_type]->release_dqblk) {
414 ret = dqopt->ops[dquot->dq_type]->release_dqblk(dquot);
415 /* Write the info */
416 if (info_dirty(&dqopt->info[dquot->dq_type]))
417 ret2 = dqopt->ops[dquot->dq_type]->write_file_info(dquot->dq_sb, dquot->dq_type);
418 if (ret >= 0)
419 ret = ret2;
420 }
421 clear_bit(DQ_ACTIVE_B, &dquot->dq_flags);
d3be915f 422 mutex_unlock(&dqopt->dqio_mutex);
1da177e4 423out_dqlock:
d3be915f 424 mutex_unlock(&dquot->dq_lock);
1da177e4
LT
425 return ret;
426}
08d0350c 427EXPORT_SYMBOL(dquot_release);
1da177e4 428
7d9056ba 429void dquot_destroy(struct dquot *dquot)
74f783af
JK
430{
431 kmem_cache_free(dquot_cachep, dquot);
432}
7d9056ba 433EXPORT_SYMBOL(dquot_destroy);
74f783af
JK
434
435static inline void do_destroy_dquot(struct dquot *dquot)
436{
437 dquot->dq_sb->dq_op->destroy_dquot(dquot);
438}
439
1da177e4
LT
440/* Invalidate all dquots on the list. Note that this function is called after
441 * quota is disabled and pointers from inodes removed so there cannot be new
6362e4d4
JK
442 * quota users. There can still be some users of quotas due to inodes being
443 * just deleted or pruned by prune_icache() (those are not attached to any
cc33412f 444 * list) or parallel quotactl call. We have to wait for such users.
6362e4d4 445 */
1da177e4
LT
446static void invalidate_dquots(struct super_block *sb, int type)
447{
c33ed271 448 struct dquot *dquot, *tmp;
1da177e4 449
6362e4d4 450restart:
1da177e4 451 spin_lock(&dq_list_lock);
c33ed271 452 list_for_each_entry_safe(dquot, tmp, &inuse_list, dq_inuse) {
1da177e4
LT
453 if (dquot->dq_sb != sb)
454 continue;
455 if (dquot->dq_type != type)
456 continue;
6362e4d4
JK
457 /* Wait for dquot users */
458 if (atomic_read(&dquot->dq_count)) {
459 DEFINE_WAIT(wait);
460
461 atomic_inc(&dquot->dq_count);
462 prepare_to_wait(&dquot->dq_wait_unused, &wait,
463 TASK_UNINTERRUPTIBLE);
464 spin_unlock(&dq_list_lock);
465 /* Once dqput() wakes us up, we know it's time to free
466 * the dquot.
467 * IMPORTANT: we rely on the fact that there is always
468 * at most one process waiting for dquot to free.
469 * Otherwise dq_count would be > 1 and we would never
470 * wake up.
471 */
472 if (atomic_read(&dquot->dq_count) > 1)
473 schedule();
474 finish_wait(&dquot->dq_wait_unused, &wait);
475 dqput(dquot);
476 /* At this moment dquot() need not exist (it could be
477 * reclaimed by prune_dqcache(). Hence we must
478 * restart. */
479 goto restart;
480 }
481 /*
482 * Quota now has no users and it has been written on last
483 * dqput()
484 */
1da177e4
LT
485 remove_dquot_hash(dquot);
486 remove_free_dquot(dquot);
487 remove_inuse(dquot);
74f783af 488 do_destroy_dquot(dquot);
1da177e4
LT
489 }
490 spin_unlock(&dq_list_lock);
491}
492
12c77527
JK
493/* Call callback for every active dquot on given filesystem */
494int dquot_scan_active(struct super_block *sb,
495 int (*fn)(struct dquot *dquot, unsigned long priv),
496 unsigned long priv)
497{
498 struct dquot *dquot, *old_dquot = NULL;
499 int ret = 0;
500
501 mutex_lock(&sb_dqopt(sb)->dqonoff_mutex);
502 spin_lock(&dq_list_lock);
503 list_for_each_entry(dquot, &inuse_list, dq_inuse) {
504 if (!test_bit(DQ_ACTIVE_B, &dquot->dq_flags))
505 continue;
506 if (dquot->dq_sb != sb)
507 continue;
508 /* Now we have active dquot so we can just increase use count */
509 atomic_inc(&dquot->dq_count);
510 dqstats.lookups++;
511 spin_unlock(&dq_list_lock);
512 dqput(old_dquot);
513 old_dquot = dquot;
514 ret = fn(dquot, priv);
515 if (ret < 0)
516 goto out;
517 spin_lock(&dq_list_lock);
518 /* We are safe to continue now because our dquot could not
519 * be moved out of the inuse list while we hold the reference */
520 }
521 spin_unlock(&dq_list_lock);
522out:
523 dqput(old_dquot);
524 mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
525 return ret;
526}
08d0350c 527EXPORT_SYMBOL(dquot_scan_active);
12c77527 528
1da177e4
LT
529int vfs_quota_sync(struct super_block *sb, int type)
530{
531 struct list_head *dirty;
532 struct dquot *dquot;
533 struct quota_info *dqopt = sb_dqopt(sb);
534 int cnt;
535
d3be915f 536 mutex_lock(&dqopt->dqonoff_mutex);
1da177e4
LT
537 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
538 if (type != -1 && cnt != type)
539 continue;
f55abc0f 540 if (!sb_has_quota_active(sb, cnt))
1da177e4
LT
541 continue;
542 spin_lock(&dq_list_lock);
543 dirty = &dqopt->info[cnt].dqi_dirty_list;
544 while (!list_empty(dirty)) {
b5e61818 545 dquot = list_first_entry(dirty, struct dquot, dq_dirty);
1da177e4
LT
546 /* Dirty and inactive can be only bad dquot... */
547 if (!test_bit(DQ_ACTIVE_B, &dquot->dq_flags)) {
548 clear_dquot_dirty(dquot);
549 continue;
550 }
551 /* Now we have active dquot from which someone is
552 * holding reference so we can safely just increase
553 * use count */
554 atomic_inc(&dquot->dq_count);
555 dqstats.lookups++;
556 spin_unlock(&dq_list_lock);
557 sb->dq_op->write_dquot(dquot);
558 dqput(dquot);
559 spin_lock(&dq_list_lock);
560 }
561 spin_unlock(&dq_list_lock);
562 }
563
564 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
f55abc0f
JK
565 if ((cnt == type || type == -1) && sb_has_quota_active(sb, cnt)
566 && info_dirty(&dqopt->info[cnt]))
1da177e4
LT
567 sb->dq_op->write_info(sb, cnt);
568 spin_lock(&dq_list_lock);
569 dqstats.syncs++;
570 spin_unlock(&dq_list_lock);
d3be915f 571 mutex_unlock(&dqopt->dqonoff_mutex);
1da177e4
LT
572
573 return 0;
574}
08d0350c 575EXPORT_SYMBOL(vfs_quota_sync);
1da177e4
LT
576
577/* Free unused dquots from cache */
578static void prune_dqcache(int count)
579{
580 struct list_head *head;
581 struct dquot *dquot;
582
583 head = free_dquots.prev;
584 while (head != &free_dquots && count) {
585 dquot = list_entry(head, struct dquot, dq_free);
586 remove_dquot_hash(dquot);
587 remove_free_dquot(dquot);
588 remove_inuse(dquot);
74f783af 589 do_destroy_dquot(dquot);
1da177e4
LT
590 count--;
591 head = free_dquots.prev;
592 }
593}
594
595/*
596 * This is called from kswapd when we think we need some
597 * more memory
598 */
599
27496a8c 600static int shrink_dqcache_memory(int nr, gfp_t gfp_mask)
1da177e4
LT
601{
602 if (nr) {
603 spin_lock(&dq_list_lock);
604 prune_dqcache(nr);
605 spin_unlock(&dq_list_lock);
606 }
607 return (dqstats.free_dquots / 100) * sysctl_vfs_cache_pressure;
608}
609
8e1f936b
RR
610static struct shrinker dqcache_shrinker = {
611 .shrink = shrink_dqcache_memory,
612 .seeks = DEFAULT_SEEKS,
613};
614
1da177e4
LT
615/*
616 * Put reference to dquot
617 * NOTE: If you change this function please check whether dqput_blocks() works right...
1da177e4 618 */
3d9ea253 619void dqput(struct dquot *dquot)
1da177e4 620{
b48d3805
JK
621 int ret;
622
1da177e4
LT
623 if (!dquot)
624 return;
625#ifdef __DQUOT_PARANOIA
626 if (!atomic_read(&dquot->dq_count)) {
627 printk("VFS: dqput: trying to free free dquot\n");
628 printk("VFS: device %s, dquot of %s %d\n",
629 dquot->dq_sb->s_id,
630 quotatypes[dquot->dq_type],
631 dquot->dq_id);
632 BUG();
633 }
634#endif
635
636 spin_lock(&dq_list_lock);
637 dqstats.drops++;
638 spin_unlock(&dq_list_lock);
639we_slept:
640 spin_lock(&dq_list_lock);
641 if (atomic_read(&dquot->dq_count) > 1) {
642 /* We have more than one user... nothing to do */
643 atomic_dec(&dquot->dq_count);
6362e4d4 644 /* Releasing dquot during quotaoff phase? */
f55abc0f 645 if (!sb_has_quota_active(dquot->dq_sb, dquot->dq_type) &&
6362e4d4
JK
646 atomic_read(&dquot->dq_count) == 1)
647 wake_up(&dquot->dq_wait_unused);
1da177e4
LT
648 spin_unlock(&dq_list_lock);
649 return;
650 }
651 /* Need to release dquot? */
652 if (test_bit(DQ_ACTIVE_B, &dquot->dq_flags) && dquot_dirty(dquot)) {
653 spin_unlock(&dq_list_lock);
654 /* Commit dquot before releasing */
b48d3805
JK
655 ret = dquot->dq_sb->dq_op->write_dquot(dquot);
656 if (ret < 0) {
657 printk(KERN_ERR "VFS: cannot write quota structure on "
658 "device %s (error %d). Quota may get out of "
659 "sync!\n", dquot->dq_sb->s_id, ret);
660 /*
661 * We clear dirty bit anyway, so that we avoid
662 * infinite loop here
663 */
664 spin_lock(&dq_list_lock);
665 clear_dquot_dirty(dquot);
666 spin_unlock(&dq_list_lock);
667 }
1da177e4
LT
668 goto we_slept;
669 }
670 /* Clear flag in case dquot was inactive (something bad happened) */
671 clear_dquot_dirty(dquot);
672 if (test_bit(DQ_ACTIVE_B, &dquot->dq_flags)) {
673 spin_unlock(&dq_list_lock);
674 dquot->dq_sb->dq_op->release_dquot(dquot);
675 goto we_slept;
676 }
677 atomic_dec(&dquot->dq_count);
678#ifdef __DQUOT_PARANOIA
679 /* sanity check */
8abf6a47 680 BUG_ON(!list_empty(&dquot->dq_free));
1da177e4
LT
681#endif
682 put_dquot_last(dquot);
683 spin_unlock(&dq_list_lock);
684}
08d0350c 685EXPORT_SYMBOL(dqput);
1da177e4 686
7d9056ba 687struct dquot *dquot_alloc(struct super_block *sb, int type)
74f783af
JK
688{
689 return kmem_cache_zalloc(dquot_cachep, GFP_NOFS);
690}
7d9056ba 691EXPORT_SYMBOL(dquot_alloc);
74f783af 692
1da177e4
LT
693static struct dquot *get_empty_dquot(struct super_block *sb, int type)
694{
695 struct dquot *dquot;
696
74f783af 697 dquot = sb->dq_op->alloc_dquot(sb, type);
1da177e4 698 if(!dquot)
dd6f3c6d 699 return NULL;
1da177e4 700
d3be915f 701 mutex_init(&dquot->dq_lock);
1da177e4
LT
702 INIT_LIST_HEAD(&dquot->dq_free);
703 INIT_LIST_HEAD(&dquot->dq_inuse);
704 INIT_HLIST_NODE(&dquot->dq_hash);
705 INIT_LIST_HEAD(&dquot->dq_dirty);
6362e4d4 706 init_waitqueue_head(&dquot->dq_wait_unused);
1da177e4
LT
707 dquot->dq_sb = sb;
708 dquot->dq_type = type;
709 atomic_set(&dquot->dq_count, 1);
710
711 return dquot;
712}
713
714/*
715 * Get reference to dquot
cc33412f
JK
716 *
717 * Locking is slightly tricky here. We are guarded from parallel quotaoff()
718 * destroying our dquot by:
719 * a) checking for quota flags under dq_list_lock and
720 * b) getting a reference to dquot before we release dq_list_lock
1da177e4 721 */
3d9ea253 722struct dquot *dqget(struct super_block *sb, unsigned int id, int type)
1da177e4
LT
723{
724 unsigned int hashent = hashfn(sb, id, type);
dd6f3c6d 725 struct dquot *dquot = NULL, *empty = NULL;
1da177e4 726
f55abc0f 727 if (!sb_has_quota_active(sb, type))
dd6f3c6d 728 return NULL;
1da177e4
LT
729we_slept:
730 spin_lock(&dq_list_lock);
cc33412f
JK
731 spin_lock(&dq_state_lock);
732 if (!sb_has_quota_active(sb, type)) {
733 spin_unlock(&dq_state_lock);
734 spin_unlock(&dq_list_lock);
735 goto out;
736 }
737 spin_unlock(&dq_state_lock);
738
dd6f3c6d
JK
739 dquot = find_dquot(hashent, sb, id, type);
740 if (!dquot) {
741 if (!empty) {
1da177e4 742 spin_unlock(&dq_list_lock);
dd6f3c6d
JK
743 empty = get_empty_dquot(sb, type);
744 if (!empty)
1da177e4
LT
745 schedule(); /* Try to wait for a moment... */
746 goto we_slept;
747 }
748 dquot = empty;
dd6f3c6d 749 empty = NULL;
1da177e4
LT
750 dquot->dq_id = id;
751 /* all dquots go on the inuse_list */
752 put_inuse(dquot);
753 /* hash it first so it can be found */
754 insert_dquot_hash(dquot);
755 dqstats.lookups++;
756 spin_unlock(&dq_list_lock);
757 } else {
758 if (!atomic_read(&dquot->dq_count))
759 remove_free_dquot(dquot);
760 atomic_inc(&dquot->dq_count);
761 dqstats.cache_hits++;
762 dqstats.lookups++;
763 spin_unlock(&dq_list_lock);
1da177e4
LT
764 }
765 /* Wait for dq_lock - after this we know that either dquot_release() is already
766 * finished or it will be canceled due to dq_count > 1 test */
767 wait_on_dquot(dquot);
768 /* Read the dquot and instantiate it (everything done only if needed) */
769 if (!test_bit(DQ_ACTIVE_B, &dquot->dq_flags) && sb->dq_op->acquire_dquot(dquot) < 0) {
770 dqput(dquot);
dd6f3c6d 771 dquot = NULL;
cc33412f 772 goto out;
1da177e4
LT
773 }
774#ifdef __DQUOT_PARANOIA
8abf6a47 775 BUG_ON(!dquot->dq_sb); /* Has somebody invalidated entry under us? */
1da177e4 776#endif
cc33412f
JK
777out:
778 if (empty)
779 do_destroy_dquot(empty);
1da177e4
LT
780
781 return dquot;
782}
08d0350c 783EXPORT_SYMBOL(dqget);
1da177e4
LT
784
785static int dqinit_needed(struct inode *inode, int type)
786{
787 int cnt;
788
789 if (IS_NOQUOTA(inode))
790 return 0;
791 if (type != -1)
dd6f3c6d 792 return !inode->i_dquot[type];
1da177e4 793 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
dd6f3c6d 794 if (!inode->i_dquot[cnt])
1da177e4
LT
795 return 1;
796 return 0;
797}
798
d3be915f 799/* This routine is guarded by dqonoff_mutex mutex */
1da177e4
LT
800static void add_dquot_ref(struct super_block *sb, int type)
801{
941d2380 802 struct inode *inode, *old_inode = NULL;
1da177e4 803
d003fb70
CH
804 spin_lock(&inode_lock);
805 list_for_each_entry(inode, &sb->s_inodes, i_sb_list) {
806 if (!atomic_read(&inode->i_writecount))
807 continue;
808 if (!dqinit_needed(inode, type))
809 continue;
810 if (inode->i_state & (I_FREEING|I_WILL_FREE))
811 continue;
812
813 __iget(inode);
814 spin_unlock(&inode_lock);
815
941d2380 816 iput(old_inode);
d003fb70 817 sb->dq_op->initialize(inode, type);
941d2380
JK
818 /* We hold a reference to 'inode' so it couldn't have been
819 * removed from s_inodes list while we dropped the inode_lock.
820 * We cannot iput the inode now as we can be holding the last
821 * reference and we cannot iput it under inode_lock. So we
822 * keep the reference and iput it later. */
823 old_inode = inode;
824 spin_lock(&inode_lock);
1da177e4 825 }
d003fb70 826 spin_unlock(&inode_lock);
941d2380 827 iput(old_inode);
1da177e4
LT
828}
829
830/* Return 0 if dqput() won't block (note that 1 doesn't necessarily mean blocking) */
831static inline int dqput_blocks(struct dquot *dquot)
832{
833 if (atomic_read(&dquot->dq_count) <= 1)
834 return 1;
835 return 0;
836}
837
838/* Remove references to dquots from inode - add dquot to list for freeing if needed */
839/* We can't race with anybody because we hold dqptr_sem for writing... */
e5f00f42
AB
840static int remove_inode_dquot_ref(struct inode *inode, int type,
841 struct list_head *tofree_head)
1da177e4
LT
842{
843 struct dquot *dquot = inode->i_dquot[type];
844
dd6f3c6d
JK
845 inode->i_dquot[type] = NULL;
846 if (dquot) {
1da177e4
LT
847 if (dqput_blocks(dquot)) {
848#ifdef __DQUOT_PARANOIA
849 if (atomic_read(&dquot->dq_count) != 1)
850 printk(KERN_WARNING "VFS: Adding dquot with dq_count %d to dispose list.\n", atomic_read(&dquot->dq_count));
851#endif
852 spin_lock(&dq_list_lock);
853 list_add(&dquot->dq_free, tofree_head); /* As dquot must have currently users it can't be on the free list... */
854 spin_unlock(&dq_list_lock);
855 return 1;
856 }
857 else
858 dqput(dquot); /* We have guaranteed we won't block */
859 }
860 return 0;
861}
862
863/* Free list of dquots - called from inode.c */
864/* dquots are removed from inodes, no new references can be got so we are the only ones holding reference */
865static void put_dquot_list(struct list_head *tofree_head)
866{
867 struct list_head *act_head;
868 struct dquot *dquot;
869
870 act_head = tofree_head->next;
871 /* So now we have dquots on the list... Just free them */
872 while (act_head != tofree_head) {
873 dquot = list_entry(act_head, struct dquot, dq_free);
874 act_head = act_head->next;
875 list_del_init(&dquot->dq_free); /* Remove dquot from the list so we won't have problems... */
876 dqput(dquot);
877 }
878}
879
fb58b731
CH
880static void remove_dquot_ref(struct super_block *sb, int type,
881 struct list_head *tofree_head)
882{
883 struct inode *inode;
884
885 spin_lock(&inode_lock);
886 list_for_each_entry(inode, &sb->s_inodes, i_sb_list) {
887 if (!IS_NOQUOTA(inode))
888 remove_inode_dquot_ref(inode, type, tofree_head);
889 }
890 spin_unlock(&inode_lock);
891}
892
1da177e4
LT
893/* Gather all references from inodes and drop them */
894static void drop_dquot_ref(struct super_block *sb, int type)
895{
896 LIST_HEAD(tofree_head);
897
fb58b731
CH
898 if (sb->dq_op) {
899 down_write(&sb_dqopt(sb)->dqptr_sem);
900 remove_dquot_ref(sb, type, &tofree_head);
901 up_write(&sb_dqopt(sb)->dqptr_sem);
902 put_dquot_list(&tofree_head);
903 }
1da177e4
LT
904}
905
12095460 906static inline void dquot_incr_inodes(struct dquot *dquot, qsize_t number)
1da177e4
LT
907{
908 dquot->dq_dqb.dqb_curinodes += number;
909}
910
911static inline void dquot_incr_space(struct dquot *dquot, qsize_t number)
912{
913 dquot->dq_dqb.dqb_curspace += number;
914}
915
f18df228
MC
916static inline void dquot_resv_space(struct dquot *dquot, qsize_t number)
917{
918 dquot->dq_dqb.dqb_rsvspace += number;
919}
920
740d9dcd
MC
921/*
922 * Claim reserved quota space
923 */
924static void dquot_claim_reserved_space(struct dquot *dquot,
925 qsize_t number)
926{
927 WARN_ON(dquot->dq_dqb.dqb_rsvspace < number);
928 dquot->dq_dqb.dqb_curspace += number;
929 dquot->dq_dqb.dqb_rsvspace -= number;
930}
931
932static inline
933void dquot_free_reserved_space(struct dquot *dquot, qsize_t number)
934{
935 dquot->dq_dqb.dqb_rsvspace -= number;
936}
937
12095460 938static inline void dquot_decr_inodes(struct dquot *dquot, qsize_t number)
1da177e4 939{
db49d2df
JK
940 if (sb_dqopt(dquot->dq_sb)->flags & DQUOT_NEGATIVE_USAGE ||
941 dquot->dq_dqb.dqb_curinodes >= number)
1da177e4
LT
942 dquot->dq_dqb.dqb_curinodes -= number;
943 else
944 dquot->dq_dqb.dqb_curinodes = 0;
945 if (dquot->dq_dqb.dqb_curinodes <= dquot->dq_dqb.dqb_isoftlimit)
946 dquot->dq_dqb.dqb_itime = (time_t) 0;
947 clear_bit(DQ_INODES_B, &dquot->dq_flags);
948}
949
950static inline void dquot_decr_space(struct dquot *dquot, qsize_t number)
951{
db49d2df
JK
952 if (sb_dqopt(dquot->dq_sb)->flags & DQUOT_NEGATIVE_USAGE ||
953 dquot->dq_dqb.dqb_curspace >= number)
1da177e4
LT
954 dquot->dq_dqb.dqb_curspace -= number;
955 else
956 dquot->dq_dqb.dqb_curspace = 0;
12095460 957 if (dquot->dq_dqb.dqb_curspace <= dquot->dq_dqb.dqb_bsoftlimit)
1da177e4
LT
958 dquot->dq_dqb.dqb_btime = (time_t) 0;
959 clear_bit(DQ_BLKS_B, &dquot->dq_flags);
960}
961
c525460e
JK
962static int warning_issued(struct dquot *dquot, const int warntype)
963{
964 int flag = (warntype == QUOTA_NL_BHARDWARN ||
965 warntype == QUOTA_NL_BSOFTLONGWARN) ? DQ_BLKS_B :
966 ((warntype == QUOTA_NL_IHARDWARN ||
967 warntype == QUOTA_NL_ISOFTLONGWARN) ? DQ_INODES_B : 0);
968
969 if (!flag)
970 return 0;
971 return test_and_set_bit(flag, &dquot->dq_flags);
972}
973
8e893469 974#ifdef CONFIG_PRINT_QUOTA_WARNING
1da177e4
LT
975static int flag_print_warnings = 1;
976
977static inline int need_print_warning(struct dquot *dquot)
978{
979 if (!flag_print_warnings)
980 return 0;
981
982 switch (dquot->dq_type) {
983 case USRQUOTA:
da9592ed 984 return current_fsuid() == dquot->dq_id;
1da177e4
LT
985 case GRPQUOTA:
986 return in_group_p(dquot->dq_id);
987 }
988 return 0;
989}
990
1da177e4 991/* Print warning to user which exceeded quota */
c525460e 992static void print_warning(struct dquot *dquot, const int warntype)
1da177e4
LT
993{
994 char *msg = NULL;
24ec839c 995 struct tty_struct *tty;
1da177e4 996
657d3bfa
JK
997 if (warntype == QUOTA_NL_IHARDBELOW ||
998 warntype == QUOTA_NL_ISOFTBELOW ||
999 warntype == QUOTA_NL_BHARDBELOW ||
1000 warntype == QUOTA_NL_BSOFTBELOW || !need_print_warning(dquot))
1da177e4
LT
1001 return;
1002
24ec839c
PZ
1003 tty = get_current_tty();
1004 if (!tty)
452a00d2 1005 return;
24ec839c 1006 tty_write_message(tty, dquot->dq_sb->s_id);
8e893469 1007 if (warntype == QUOTA_NL_ISOFTWARN || warntype == QUOTA_NL_BSOFTWARN)
24ec839c 1008 tty_write_message(tty, ": warning, ");
1da177e4 1009 else
24ec839c
PZ
1010 tty_write_message(tty, ": write failed, ");
1011 tty_write_message(tty, quotatypes[dquot->dq_type]);
1da177e4 1012 switch (warntype) {
8e893469 1013 case QUOTA_NL_IHARDWARN:
1da177e4
LT
1014 msg = " file limit reached.\r\n";
1015 break;
8e893469 1016 case QUOTA_NL_ISOFTLONGWARN:
1da177e4
LT
1017 msg = " file quota exceeded too long.\r\n";
1018 break;
8e893469 1019 case QUOTA_NL_ISOFTWARN:
1da177e4
LT
1020 msg = " file quota exceeded.\r\n";
1021 break;
8e893469 1022 case QUOTA_NL_BHARDWARN:
1da177e4
LT
1023 msg = " block limit reached.\r\n";
1024 break;
8e893469 1025 case QUOTA_NL_BSOFTLONGWARN:
1da177e4
LT
1026 msg = " block quota exceeded too long.\r\n";
1027 break;
8e893469 1028 case QUOTA_NL_BSOFTWARN:
1da177e4
LT
1029 msg = " block quota exceeded.\r\n";
1030 break;
1031 }
24ec839c 1032 tty_write_message(tty, msg);
452a00d2 1033 tty_kref_put(tty);
1da177e4 1034}
8e893469
JK
1035#endif
1036
1037#ifdef CONFIG_QUOTA_NETLINK_INTERFACE
1038
8e893469
JK
1039/* Netlink family structure for quota */
1040static struct genl_family quota_genl_family = {
1041 .id = GENL_ID_GENERATE,
1042 .hdrsize = 0,
1043 .name = "VFS_DQUOT",
1044 .version = 1,
1045 .maxattr = QUOTA_NL_A_MAX,
1046};
1047
1048/* Send warning to userspace about user which exceeded quota */
1049static void send_warning(const struct dquot *dquot, const char warntype)
1050{
1051 static atomic_t seq;
1052 struct sk_buff *skb;
1053 void *msg_head;
1054 int ret;
22dd4837
JK
1055 int msg_size = 4 * nla_total_size(sizeof(u32)) +
1056 2 * nla_total_size(sizeof(u64));
8e893469
JK
1057
1058 /* We have to allocate using GFP_NOFS as we are called from a
1059 * filesystem performing write and thus further recursion into
1060 * the fs to free some data could cause deadlocks. */
22dd4837 1061 skb = genlmsg_new(msg_size, GFP_NOFS);
8e893469
JK
1062 if (!skb) {
1063 printk(KERN_ERR
1064 "VFS: Not enough memory to send quota warning.\n");
1065 return;
1066 }
1067 msg_head = genlmsg_put(skb, 0, atomic_add_return(1, &seq),
1068 &quota_genl_family, 0, QUOTA_NL_C_WARNING);
1069 if (!msg_head) {
1070 printk(KERN_ERR
1071 "VFS: Cannot store netlink header in quota warning.\n");
1072 goto err_out;
1073 }
1074 ret = nla_put_u32(skb, QUOTA_NL_A_QTYPE, dquot->dq_type);
1075 if (ret)
1076 goto attr_err_out;
1077 ret = nla_put_u64(skb, QUOTA_NL_A_EXCESS_ID, dquot->dq_id);
1078 if (ret)
1079 goto attr_err_out;
1080 ret = nla_put_u32(skb, QUOTA_NL_A_WARNING, warntype);
1081 if (ret)
1082 goto attr_err_out;
1083 ret = nla_put_u32(skb, QUOTA_NL_A_DEV_MAJOR,
1084 MAJOR(dquot->dq_sb->s_dev));
1085 if (ret)
1086 goto attr_err_out;
1087 ret = nla_put_u32(skb, QUOTA_NL_A_DEV_MINOR,
1088 MINOR(dquot->dq_sb->s_dev));
1089 if (ret)
1090 goto attr_err_out;
da9592ed 1091 ret = nla_put_u64(skb, QUOTA_NL_A_CAUSED_ID, current_uid());
8e893469
JK
1092 if (ret)
1093 goto attr_err_out;
1094 genlmsg_end(skb, msg_head);
1095
1096 ret = genlmsg_multicast(skb, 0, quota_genl_family.id, GFP_NOFS);
1097 if (ret < 0 && ret != -ESRCH)
1098 printk(KERN_ERR
1099 "VFS: Failed to send notification message: %d\n", ret);
1100 return;
1101attr_err_out:
22dd4837 1102 printk(KERN_ERR "VFS: Not enough space to compose quota message!\n");
8e893469
JK
1103err_out:
1104 kfree_skb(skb);
1105}
1106#endif
f18df228
MC
1107/*
1108 * Write warnings to the console and send warning messages over netlink.
1109 *
1110 * Note that this function can sleep.
1111 */
087ee8d5 1112static inline void flush_warnings(struct dquot * const *dquots, char *warntype)
1da177e4
LT
1113{
1114 int i;
1115
1116 for (i = 0; i < MAXQUOTAS; i++)
dd6f3c6d 1117 if (dquots[i] && warntype[i] != QUOTA_NL_NOWARN &&
c525460e 1118 !warning_issued(dquots[i], warntype[i])) {
8e893469 1119#ifdef CONFIG_PRINT_QUOTA_WARNING
1da177e4 1120 print_warning(dquots[i], warntype[i]);
8e893469
JK
1121#endif
1122#ifdef CONFIG_QUOTA_NETLINK_INTERFACE
1123 send_warning(dquots[i], warntype[i]);
1124#endif
1125 }
1da177e4
LT
1126}
1127
1128static inline char ignore_hardlimit(struct dquot *dquot)
1129{
1130 struct mem_dqinfo *info = &sb_dqopt(dquot->dq_sb)->info[dquot->dq_type];
1131
1132 return capable(CAP_SYS_RESOURCE) &&
1133 (info->dqi_format->qf_fmt_id != QFMT_VFS_OLD || !(info->dqi_flags & V1_DQF_RSQUASH));
1134}
1135
1136/* needs dq_data_lock */
12095460 1137static int check_idq(struct dquot *dquot, qsize_t inodes, char *warntype)
1da177e4 1138{
8e893469 1139 *warntype = QUOTA_NL_NOWARN;
f55abc0f
JK
1140 if (!sb_has_quota_limits_enabled(dquot->dq_sb, dquot->dq_type) ||
1141 test_bit(DQ_FAKE_B, &dquot->dq_flags))
1da177e4
LT
1142 return QUOTA_OK;
1143
1144 if (dquot->dq_dqb.dqb_ihardlimit &&
1145 (dquot->dq_dqb.dqb_curinodes + inodes) > dquot->dq_dqb.dqb_ihardlimit &&
1146 !ignore_hardlimit(dquot)) {
8e893469 1147 *warntype = QUOTA_NL_IHARDWARN;
1da177e4
LT
1148 return NO_QUOTA;
1149 }
1150
1151 if (dquot->dq_dqb.dqb_isoftlimit &&
1152 (dquot->dq_dqb.dqb_curinodes + inodes) > dquot->dq_dqb.dqb_isoftlimit &&
1153 dquot->dq_dqb.dqb_itime && get_seconds() >= dquot->dq_dqb.dqb_itime &&
1154 !ignore_hardlimit(dquot)) {
8e893469 1155 *warntype = QUOTA_NL_ISOFTLONGWARN;
1da177e4
LT
1156 return NO_QUOTA;
1157 }
1158
1159 if (dquot->dq_dqb.dqb_isoftlimit &&
1160 (dquot->dq_dqb.dqb_curinodes + inodes) > dquot->dq_dqb.dqb_isoftlimit &&
1161 dquot->dq_dqb.dqb_itime == 0) {
8e893469 1162 *warntype = QUOTA_NL_ISOFTWARN;
1da177e4
LT
1163 dquot->dq_dqb.dqb_itime = get_seconds() + sb_dqopt(dquot->dq_sb)->info[dquot->dq_type].dqi_igrace;
1164 }
1165
1166 return QUOTA_OK;
1167}
1168
1169/* needs dq_data_lock */
1170static int check_bdq(struct dquot *dquot, qsize_t space, int prealloc, char *warntype)
1171{
f18df228
MC
1172 qsize_t tspace;
1173
8e893469 1174 *warntype = QUOTA_NL_NOWARN;
f55abc0f
JK
1175 if (!sb_has_quota_limits_enabled(dquot->dq_sb, dquot->dq_type) ||
1176 test_bit(DQ_FAKE_B, &dquot->dq_flags))
1da177e4
LT
1177 return QUOTA_OK;
1178
f18df228
MC
1179 tspace = dquot->dq_dqb.dqb_curspace + dquot->dq_dqb.dqb_rsvspace
1180 + space;
1181
1da177e4 1182 if (dquot->dq_dqb.dqb_bhardlimit &&
f18df228 1183 tspace > dquot->dq_dqb.dqb_bhardlimit &&
1da177e4
LT
1184 !ignore_hardlimit(dquot)) {
1185 if (!prealloc)
8e893469 1186 *warntype = QUOTA_NL_BHARDWARN;
1da177e4
LT
1187 return NO_QUOTA;
1188 }
1189
1190 if (dquot->dq_dqb.dqb_bsoftlimit &&
f18df228 1191 tspace > dquot->dq_dqb.dqb_bsoftlimit &&
1da177e4
LT
1192 dquot->dq_dqb.dqb_btime && get_seconds() >= dquot->dq_dqb.dqb_btime &&
1193 !ignore_hardlimit(dquot)) {
1194 if (!prealloc)
8e893469 1195 *warntype = QUOTA_NL_BSOFTLONGWARN;
1da177e4
LT
1196 return NO_QUOTA;
1197 }
1198
1199 if (dquot->dq_dqb.dqb_bsoftlimit &&
f18df228 1200 tspace > dquot->dq_dqb.dqb_bsoftlimit &&
1da177e4
LT
1201 dquot->dq_dqb.dqb_btime == 0) {
1202 if (!prealloc) {
8e893469 1203 *warntype = QUOTA_NL_BSOFTWARN;
1da177e4
LT
1204 dquot->dq_dqb.dqb_btime = get_seconds() + sb_dqopt(dquot->dq_sb)->info[dquot->dq_type].dqi_bgrace;
1205 }
1206 else
1207 /*
1208 * We don't allow preallocation to exceed softlimit so exceeding will
1209 * be always printed
1210 */
1211 return NO_QUOTA;
1212 }
1213
1214 return QUOTA_OK;
1215}
1216
12095460 1217static int info_idq_free(struct dquot *dquot, qsize_t inodes)
657d3bfa
JK
1218{
1219 if (test_bit(DQ_FAKE_B, &dquot->dq_flags) ||
f55abc0f
JK
1220 dquot->dq_dqb.dqb_curinodes <= dquot->dq_dqb.dqb_isoftlimit ||
1221 !sb_has_quota_limits_enabled(dquot->dq_sb, dquot->dq_type))
657d3bfa
JK
1222 return QUOTA_NL_NOWARN;
1223
1224 if (dquot->dq_dqb.dqb_curinodes - inodes <= dquot->dq_dqb.dqb_isoftlimit)
1225 return QUOTA_NL_ISOFTBELOW;
1226 if (dquot->dq_dqb.dqb_curinodes >= dquot->dq_dqb.dqb_ihardlimit &&
1227 dquot->dq_dqb.dqb_curinodes - inodes < dquot->dq_dqb.dqb_ihardlimit)
1228 return QUOTA_NL_IHARDBELOW;
1229 return QUOTA_NL_NOWARN;
1230}
1231
1232static int info_bdq_free(struct dquot *dquot, qsize_t space)
1233{
1234 if (test_bit(DQ_FAKE_B, &dquot->dq_flags) ||
12095460 1235 dquot->dq_dqb.dqb_curspace <= dquot->dq_dqb.dqb_bsoftlimit)
657d3bfa
JK
1236 return QUOTA_NL_NOWARN;
1237
12095460 1238 if (dquot->dq_dqb.dqb_curspace - space <= dquot->dq_dqb.dqb_bsoftlimit)
657d3bfa 1239 return QUOTA_NL_BSOFTBELOW;
12095460
JK
1240 if (dquot->dq_dqb.dqb_curspace >= dquot->dq_dqb.dqb_bhardlimit &&
1241 dquot->dq_dqb.dqb_curspace - space < dquot->dq_dqb.dqb_bhardlimit)
657d3bfa
JK
1242 return QUOTA_NL_BHARDBELOW;
1243 return QUOTA_NL_NOWARN;
1244}
1da177e4
LT
1245/*
1246 * Initialize quota pointers in inode
cc33412f
JK
1247 * We do things in a bit complicated way but by that we avoid calling
1248 * dqget() and thus filesystem callbacks under dqptr_sem.
1da177e4
LT
1249 */
1250int dquot_initialize(struct inode *inode, int type)
1251{
1252 unsigned int id = 0;
1253 int cnt, ret = 0;
dd6f3c6d 1254 struct dquot *got[MAXQUOTAS] = { NULL, NULL };
cc33412f 1255 struct super_block *sb = inode->i_sb;
1da177e4 1256
d3be915f
IM
1257 /* First test before acquiring mutex - solves deadlocks when we
1258 * re-enter the quota code and are already holding the mutex */
1da177e4
LT
1259 if (IS_NOQUOTA(inode))
1260 return 0;
cc33412f
JK
1261
1262 /* First get references to structures we might need. */
1263 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1264 if (type != -1 && cnt != type)
1265 continue;
1266 switch (cnt) {
1267 case USRQUOTA:
1268 id = inode->i_uid;
1269 break;
1270 case GRPQUOTA:
1271 id = inode->i_gid;
1272 break;
1273 }
1274 got[cnt] = dqget(sb, id, cnt);
1275 }
1276
1277 down_write(&sb_dqopt(sb)->dqptr_sem);
1da177e4
LT
1278 /* Having dqptr_sem we know NOQUOTA flags can't be altered... */
1279 if (IS_NOQUOTA(inode))
1280 goto out_err;
1281 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1282 if (type != -1 && cnt != type)
1283 continue;
cc33412f
JK
1284 /* Avoid races with quotaoff() */
1285 if (!sb_has_quota_active(sb, cnt))
1286 continue;
dd6f3c6d 1287 if (!inode->i_dquot[cnt]) {
cc33412f 1288 inode->i_dquot[cnt] = got[cnt];
dd6f3c6d 1289 got[cnt] = NULL;
1da177e4
LT
1290 }
1291 }
1292out_err:
cc33412f
JK
1293 up_write(&sb_dqopt(sb)->dqptr_sem);
1294 /* Drop unused references */
1295 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1296 dqput(got[cnt]);
1da177e4
LT
1297 return ret;
1298}
08d0350c 1299EXPORT_SYMBOL(dquot_initialize);
1da177e4
LT
1300
1301/*
1302 * Release all quotas referenced by inode
1da177e4 1303 */
cc33412f 1304int dquot_drop(struct inode *inode)
1da177e4
LT
1305{
1306 int cnt;
cc33412f 1307 struct dquot *put[MAXQUOTAS];
1da177e4 1308
cc33412f 1309 down_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
1da177e4 1310 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
cc33412f 1311 put[cnt] = inode->i_dquot[cnt];
dd6f3c6d 1312 inode->i_dquot[cnt] = NULL;
1da177e4
LT
1313 }
1314 up_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
cc33412f
JK
1315
1316 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1317 dqput(put[cnt]);
1da177e4
LT
1318 return 0;
1319}
08d0350c 1320EXPORT_SYMBOL(dquot_drop);
1da177e4 1321
b85f4b87
JK
1322/* 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++)
dd6f3c6d 1337 if (inode->i_dquot[cnt])
b85f4b87
JK
1338 break;
1339 if (cnt < MAXQUOTAS)
1340 inode->i_sb->dq_op->drop(inode);
1341 }
1342}
08d0350c 1343EXPORT_SYMBOL(vfs_dq_drop);
b85f4b87 1344
1da177e4
LT
1345/*
1346 * Following four functions update i_blocks+i_bytes fields and
1347 * quota information (together with appropriate checks)
1348 * NOTE: We absolutely rely on the fact that caller dirties
1349 * the inode (usually macros in quotaops.h care about this) and
1350 * holds a handle for the current transaction so that dquot write and
1351 * inode write go into the same transaction.
1352 */
1353
1354/*
1355 * This operation can block, but only after everything is updated
1356 */
f18df228
MC
1357int __dquot_alloc_space(struct inode *inode, qsize_t number,
1358 int warn, int reserve)
1da177e4 1359{
f18df228 1360 int cnt, ret = QUOTA_OK;
1da177e4
LT
1361 char warntype[MAXQUOTAS];
1362
1da177e4 1363 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
8e893469 1364 warntype[cnt] = QUOTA_NL_NOWARN;
1da177e4 1365
1da177e4
LT
1366 spin_lock(&dq_data_lock);
1367 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
dd6f3c6d 1368 if (!inode->i_dquot[cnt])
1da177e4 1369 continue;
f18df228
MC
1370 if (check_bdq(inode->i_dquot[cnt], number, warn, warntype+cnt)
1371 == NO_QUOTA) {
1372 ret = NO_QUOTA;
1373 goto out_unlock;
1374 }
1da177e4
LT
1375 }
1376 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
dd6f3c6d 1377 if (!inode->i_dquot[cnt])
1da177e4 1378 continue;
f18df228
MC
1379 if (reserve)
1380 dquot_resv_space(inode->i_dquot[cnt], number);
1381 else
1382 dquot_incr_space(inode->i_dquot[cnt], number);
1da177e4 1383 }
f18df228
MC
1384 if (!reserve)
1385 inode_add_bytes(inode, number);
1386out_unlock:
1da177e4 1387 spin_unlock(&dq_data_lock);
1da177e4 1388 flush_warnings(inode->i_dquot, warntype);
f18df228
MC
1389 return ret;
1390}
1391
1392int dquot_alloc_space(struct inode *inode, qsize_t number, int warn)
1393{
1394 int cnt, ret = QUOTA_OK;
1395
1396 /*
1397 * First test before acquiring mutex - solves deadlocks when we
1398 * re-enter the quota code and are already holding the mutex
1399 */
1400 if (IS_NOQUOTA(inode)) {
1401 inode_add_bytes(inode, number);
1402 goto out;
1403 }
1404
1405 down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1406 if (IS_NOQUOTA(inode)) {
1407 inode_add_bytes(inode, number);
1408 goto out_unlock;
1409 }
1410
1411 ret = __dquot_alloc_space(inode, number, warn, 0);
1412 if (ret == NO_QUOTA)
1413 goto out_unlock;
1414
1415 /* Dirtify all the dquots - this can block when journalling */
1416 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1417 if (inode->i_dquot[cnt])
1418 mark_dquot_dirty(inode->i_dquot[cnt]);
1419out_unlock:
1da177e4 1420 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
f18df228
MC
1421out:
1422 return ret;
1423}
08d0350c 1424EXPORT_SYMBOL(dquot_alloc_space);
f18df228
MC
1425
1426int dquot_reserve_space(struct inode *inode, qsize_t number, int warn)
1427{
1428 int ret = QUOTA_OK;
1429
1430 if (IS_NOQUOTA(inode))
1431 goto out;
1432
1433 down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1434 if (IS_NOQUOTA(inode))
1435 goto out_unlock;
1436
1437 ret = __dquot_alloc_space(inode, number, warn, 1);
1438out_unlock:
1439 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1440out:
1da177e4
LT
1441 return ret;
1442}
f18df228 1443EXPORT_SYMBOL(dquot_reserve_space);
1da177e4
LT
1444
1445/*
1446 * This operation can block, but only after everything is updated
1447 */
12095460 1448int dquot_alloc_inode(const struct inode *inode, qsize_t number)
1da177e4
LT
1449{
1450 int cnt, ret = NO_QUOTA;
1451 char warntype[MAXQUOTAS];
1452
d3be915f
IM
1453 /* First test before acquiring mutex - solves deadlocks when we
1454 * re-enter the quota code and are already holding the mutex */
1da177e4
LT
1455 if (IS_NOQUOTA(inode))
1456 return QUOTA_OK;
1457 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
8e893469 1458 warntype[cnt] = QUOTA_NL_NOWARN;
1da177e4
LT
1459 down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1460 if (IS_NOQUOTA(inode)) {
1461 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1462 return QUOTA_OK;
1463 }
1464 spin_lock(&dq_data_lock);
1465 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
dd6f3c6d 1466 if (!inode->i_dquot[cnt])
1da177e4
LT
1467 continue;
1468 if (check_idq(inode->i_dquot[cnt], number, warntype+cnt) == NO_QUOTA)
1469 goto warn_put_all;
1470 }
1471
1472 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
dd6f3c6d 1473 if (!inode->i_dquot[cnt])
1da177e4
LT
1474 continue;
1475 dquot_incr_inodes(inode->i_dquot[cnt], number);
1476 }
1477 ret = QUOTA_OK;
1478warn_put_all:
1479 spin_unlock(&dq_data_lock);
1480 if (ret == QUOTA_OK)
1481 /* Dirtify all the dquots - this can block when journalling */
1482 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1483 if (inode->i_dquot[cnt])
1484 mark_dquot_dirty(inode->i_dquot[cnt]);
087ee8d5 1485 flush_warnings(inode->i_dquot, warntype);
1da177e4
LT
1486 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1487 return ret;
1488}
08d0350c 1489EXPORT_SYMBOL(dquot_alloc_inode);
1da177e4 1490
740d9dcd
MC
1491int dquot_claim_space(struct inode *inode, qsize_t number)
1492{
1493 int cnt;
1494 int ret = QUOTA_OK;
1495
1496 if (IS_NOQUOTA(inode)) {
1497 inode_add_bytes(inode, number);
1498 goto out;
1499 }
1500
1501 down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1502 if (IS_NOQUOTA(inode)) {
1503 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1504 inode_add_bytes(inode, number);
1505 goto out;
1506 }
1507
1508 spin_lock(&dq_data_lock);
1509 /* Claim reserved quotas to allocated quotas */
1510 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
dd6f3c6d 1511 if (inode->i_dquot[cnt])
740d9dcd
MC
1512 dquot_claim_reserved_space(inode->i_dquot[cnt],
1513 number);
1514 }
1515 /* Update inode bytes */
1516 inode_add_bytes(inode, number);
1517 spin_unlock(&dq_data_lock);
1518 /* Dirtify all the dquots - this can block when journalling */
1519 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1520 if (inode->i_dquot[cnt])
1521 mark_dquot_dirty(inode->i_dquot[cnt]);
1522 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1523out:
1524 return ret;
1525}
1526EXPORT_SYMBOL(dquot_claim_space);
1527
1528/*
1529 * Release reserved quota space
1530 */
1531void dquot_release_reserved_space(struct inode *inode, qsize_t number)
1532{
1533 int cnt;
1534
1535 if (IS_NOQUOTA(inode))
1536 goto out;
1537
1538 down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1539 if (IS_NOQUOTA(inode))
1540 goto out_unlock;
1541
1542 spin_lock(&dq_data_lock);
1543 /* Release reserved dquots */
1544 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
dd6f3c6d 1545 if (inode->i_dquot[cnt])
740d9dcd
MC
1546 dquot_free_reserved_space(inode->i_dquot[cnt], number);
1547 }
1548 spin_unlock(&dq_data_lock);
1549
1550out_unlock:
1551 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1552out:
1553 return;
1554}
1555EXPORT_SYMBOL(dquot_release_reserved_space);
1556
1da177e4
LT
1557/*
1558 * This operation can block, but only after everything is updated
1559 */
1560int dquot_free_space(struct inode *inode, qsize_t number)
1561{
1562 unsigned int cnt;
657d3bfa 1563 char warntype[MAXQUOTAS];
1da177e4 1564
d3be915f
IM
1565 /* First test before acquiring mutex - solves deadlocks when we
1566 * re-enter the quota code and are already holding the mutex */
1da177e4
LT
1567 if (IS_NOQUOTA(inode)) {
1568out_sub:
1569 inode_sub_bytes(inode, number);
1570 return QUOTA_OK;
1571 }
657d3bfa 1572
1da177e4
LT
1573 down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1574 /* Now recheck reliably when holding dqptr_sem */
1575 if (IS_NOQUOTA(inode)) {
1576 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1577 goto out_sub;
1578 }
1579 spin_lock(&dq_data_lock);
1580 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
dd6f3c6d 1581 if (!inode->i_dquot[cnt])
1da177e4 1582 continue;
657d3bfa 1583 warntype[cnt] = info_bdq_free(inode->i_dquot[cnt], number);
1da177e4
LT
1584 dquot_decr_space(inode->i_dquot[cnt], number);
1585 }
1586 inode_sub_bytes(inode, number);
1587 spin_unlock(&dq_data_lock);
1588 /* Dirtify all the dquots - this can block when journalling */
1589 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1590 if (inode->i_dquot[cnt])
1591 mark_dquot_dirty(inode->i_dquot[cnt]);
657d3bfa 1592 flush_warnings(inode->i_dquot, warntype);
1da177e4
LT
1593 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1594 return QUOTA_OK;
1595}
08d0350c 1596EXPORT_SYMBOL(dquot_free_space);
1da177e4
LT
1597
1598/*
1599 * This operation can block, but only after everything is updated
1600 */
12095460 1601int dquot_free_inode(const struct inode *inode, qsize_t number)
1da177e4
LT
1602{
1603 unsigned int cnt;
657d3bfa 1604 char warntype[MAXQUOTAS];
1da177e4 1605
d3be915f
IM
1606 /* First test before acquiring mutex - solves deadlocks when we
1607 * re-enter the quota code and are already holding the mutex */
1da177e4
LT
1608 if (IS_NOQUOTA(inode))
1609 return QUOTA_OK;
657d3bfa 1610
1da177e4
LT
1611 down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1612 /* Now recheck reliably when holding dqptr_sem */
1613 if (IS_NOQUOTA(inode)) {
1614 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1615 return QUOTA_OK;
1616 }
1617 spin_lock(&dq_data_lock);
1618 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
dd6f3c6d 1619 if (!inode->i_dquot[cnt])
1da177e4 1620 continue;
657d3bfa 1621 warntype[cnt] = info_idq_free(inode->i_dquot[cnt], number);
1da177e4
LT
1622 dquot_decr_inodes(inode->i_dquot[cnt], number);
1623 }
1624 spin_unlock(&dq_data_lock);
1625 /* Dirtify all the dquots - this can block when journalling */
1626 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1627 if (inode->i_dquot[cnt])
1628 mark_dquot_dirty(inode->i_dquot[cnt]);
657d3bfa 1629 flush_warnings(inode->i_dquot, warntype);
1da177e4
LT
1630 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1631 return QUOTA_OK;
1632}
08d0350c 1633EXPORT_SYMBOL(dquot_free_inode);
1da177e4 1634
740d9dcd
MC
1635/*
1636 * call back function, get reserved quota space from underlying fs
1637 */
1638qsize_t dquot_get_reserved_space(struct inode *inode)
1639{
1640 qsize_t reserved_space = 0;
1641
1642 if (sb_any_quota_active(inode->i_sb) &&
1643 inode->i_sb->dq_op->get_reserved_space)
1644 reserved_space = inode->i_sb->dq_op->get_reserved_space(inode);
1645 return reserved_space;
1646}
1647
1da177e4
LT
1648/*
1649 * Transfer the number of inode and blocks from one diskquota to an other.
1650 *
1651 * This operation can block, but only after everything is updated
1652 * A transaction must be started when entering this function.
1653 */
1654int dquot_transfer(struct inode *inode, struct iattr *iattr)
1655{
740d9dcd
MC
1656 qsize_t space, cur_space;
1657 qsize_t rsv_space = 0;
1da177e4
LT
1658 struct dquot *transfer_from[MAXQUOTAS];
1659 struct dquot *transfer_to[MAXQUOTAS];
cc33412f
JK
1660 int cnt, ret = QUOTA_OK;
1661 int chuid = iattr->ia_valid & ATTR_UID && inode->i_uid != iattr->ia_uid,
1662 chgid = iattr->ia_valid & ATTR_GID && inode->i_gid != iattr->ia_gid;
657d3bfa
JK
1663 char warntype_to[MAXQUOTAS];
1664 char warntype_from_inodes[MAXQUOTAS], warntype_from_space[MAXQUOTAS];
1da177e4 1665
d3be915f
IM
1666 /* First test before acquiring mutex - solves deadlocks when we
1667 * re-enter the quota code and are already holding the mutex */
1da177e4
LT
1668 if (IS_NOQUOTA(inode))
1669 return QUOTA_OK;
cc33412f 1670 /* Initialize the arrays */
1da177e4 1671 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
dd6f3c6d
JK
1672 transfer_from[cnt] = NULL;
1673 transfer_to[cnt] = NULL;
657d3bfa 1674 warntype_to[cnt] = QUOTA_NL_NOWARN;
1da177e4
LT
1675 switch (cnt) {
1676 case USRQUOTA:
1677 if (!chuid)
1678 continue;
1679 transfer_to[cnt] = dqget(inode->i_sb, iattr->ia_uid, cnt);
1680 break;
1681 case GRPQUOTA:
1682 if (!chgid)
1683 continue;
1684 transfer_to[cnt] = dqget(inode->i_sb, iattr->ia_gid, cnt);
1685 break;
1686 }
1687 }
cc33412f
JK
1688
1689 down_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
1690 /* Now recheck reliably when holding dqptr_sem */
1691 if (IS_NOQUOTA(inode)) { /* File without quota accounting? */
1692 up_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
1693 goto put_all;
1694 }
1da177e4 1695 spin_lock(&dq_data_lock);
740d9dcd
MC
1696 cur_space = inode_get_bytes(inode);
1697 rsv_space = dquot_get_reserved_space(inode);
1698 space = cur_space + rsv_space;
1da177e4
LT
1699 /* Build the transfer_from list and check the limits */
1700 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
dd6f3c6d 1701 if (!transfer_to[cnt])
1da177e4
LT
1702 continue;
1703 transfer_from[cnt] = inode->i_dquot[cnt];
657d3bfa
JK
1704 if (check_idq(transfer_to[cnt], 1, warntype_to + cnt) ==
1705 NO_QUOTA || check_bdq(transfer_to[cnt], space, 0,
1706 warntype_to + cnt) == NO_QUOTA)
cc33412f 1707 goto over_quota;
1da177e4
LT
1708 }
1709
1710 /*
1711 * Finally perform the needed transfer from transfer_from to transfer_to
1712 */
1713 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1714 /*
1715 * Skip changes for same uid or gid or for turned off quota-type.
1716 */
dd6f3c6d 1717 if (!transfer_to[cnt])
1da177e4
LT
1718 continue;
1719
1720 /* Due to IO error we might not have transfer_from[] structure */
1721 if (transfer_from[cnt]) {
657d3bfa
JK
1722 warntype_from_inodes[cnt] =
1723 info_idq_free(transfer_from[cnt], 1);
1724 warntype_from_space[cnt] =
1725 info_bdq_free(transfer_from[cnt], space);
1da177e4 1726 dquot_decr_inodes(transfer_from[cnt], 1);
740d9dcd
MC
1727 dquot_decr_space(transfer_from[cnt], cur_space);
1728 dquot_free_reserved_space(transfer_from[cnt],
1729 rsv_space);
1da177e4
LT
1730 }
1731
1732 dquot_incr_inodes(transfer_to[cnt], 1);
740d9dcd
MC
1733 dquot_incr_space(transfer_to[cnt], cur_space);
1734 dquot_resv_space(transfer_to[cnt], rsv_space);
1da177e4
LT
1735
1736 inode->i_dquot[cnt] = transfer_to[cnt];
1737 }
1da177e4 1738 spin_unlock(&dq_data_lock);
cc33412f
JK
1739 up_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
1740
1da177e4
LT
1741 /* Dirtify all the dquots - this can block when journalling */
1742 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1743 if (transfer_from[cnt])
1744 mark_dquot_dirty(transfer_from[cnt]);
cc33412f 1745 if (transfer_to[cnt]) {
1da177e4 1746 mark_dquot_dirty(transfer_to[cnt]);
cc33412f 1747 /* The reference we got is transferred to the inode */
dd6f3c6d 1748 transfer_to[cnt] = NULL;
cc33412f 1749 }
1da177e4 1750 }
cc33412f 1751warn_put_all:
657d3bfa
JK
1752 flush_warnings(transfer_to, warntype_to);
1753 flush_warnings(transfer_from, warntype_from_inodes);
1754 flush_warnings(transfer_from, warntype_from_space);
cc33412f 1755put_all:
1da177e4 1756 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
cc33412f
JK
1757 dqput(transfer_from[cnt]);
1758 dqput(transfer_to[cnt]);
1da177e4 1759 }
1da177e4 1760 return ret;
cc33412f
JK
1761over_quota:
1762 spin_unlock(&dq_data_lock);
1763 up_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
1764 /* Clear dquot pointers we don't want to dqput() */
1765 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
dd6f3c6d 1766 transfer_from[cnt] = NULL;
cc33412f
JK
1767 ret = NO_QUOTA;
1768 goto warn_put_all;
1da177e4 1769}
08d0350c 1770EXPORT_SYMBOL(dquot_transfer);
1da177e4 1771
b85f4b87
JK
1772/* Wrapper for transferring ownership of an inode */
1773int vfs_dq_transfer(struct inode *inode, struct iattr *iattr)
1774{
f55abc0f 1775 if (sb_any_quota_active(inode->i_sb) && !IS_NOQUOTA(inode)) {
b85f4b87
JK
1776 vfs_dq_init(inode);
1777 if (inode->i_sb->dq_op->transfer(inode, iattr) == NO_QUOTA)
1778 return 1;
1779 }
1780 return 0;
1781}
08d0350c 1782EXPORT_SYMBOL(vfs_dq_transfer);
b85f4b87 1783
1da177e4
LT
1784/*
1785 * Write info of quota file to disk
1786 */
1787int dquot_commit_info(struct super_block *sb, int type)
1788{
1789 int ret;
1790 struct quota_info *dqopt = sb_dqopt(sb);
1791
d3be915f 1792 mutex_lock(&dqopt->dqio_mutex);
1da177e4 1793 ret = dqopt->ops[type]->write_file_info(sb, type);
d3be915f 1794 mutex_unlock(&dqopt->dqio_mutex);
1da177e4
LT
1795 return ret;
1796}
08d0350c 1797EXPORT_SYMBOL(dquot_commit_info);
1da177e4
LT
1798
1799/*
1800 * Definitions of diskquota operations.
1801 */
1802struct dquot_operations dquot_operations = {
1803 .initialize = dquot_initialize,
1804 .drop = dquot_drop,
1805 .alloc_space = dquot_alloc_space,
1806 .alloc_inode = dquot_alloc_inode,
1807 .free_space = dquot_free_space,
1808 .free_inode = dquot_free_inode,
1809 .transfer = dquot_transfer,
1810 .write_dquot = dquot_commit,
1811 .acquire_dquot = dquot_acquire,
1812 .release_dquot = dquot_release,
1813 .mark_dirty = dquot_mark_dquot_dirty,
74f783af
JK
1814 .write_info = dquot_commit_info,
1815 .alloc_dquot = dquot_alloc,
1816 .destroy_dquot = dquot_destroy,
1da177e4
LT
1817};
1818
1da177e4
LT
1819/*
1820 * Turn quota off on a device. type == -1 ==> quotaoff for all types (umount)
1821 */
f55abc0f 1822int vfs_quota_disable(struct super_block *sb, int type, unsigned int flags)
1da177e4 1823{
0ff5af83 1824 int cnt, ret = 0;
1da177e4
LT
1825 struct quota_info *dqopt = sb_dqopt(sb);
1826 struct inode *toputinode[MAXQUOTAS];
1da177e4 1827
f55abc0f
JK
1828 /* Cannot turn off usage accounting without turning off limits, or
1829 * suspend quotas and simultaneously turn quotas off. */
1830 if ((flags & DQUOT_USAGE_ENABLED && !(flags & DQUOT_LIMITS_ENABLED))
1831 || (flags & DQUOT_SUSPENDED && flags & (DQUOT_LIMITS_ENABLED |
1832 DQUOT_USAGE_ENABLED)))
1833 return -EINVAL;
1834
1da177e4 1835 /* We need to serialize quota_off() for device */
d3be915f 1836 mutex_lock(&dqopt->dqonoff_mutex);
9377abd0
JK
1837
1838 /*
1839 * Skip everything if there's nothing to do. We have to do this because
1840 * sometimes we are called when fill_super() failed and calling
1841 * sync_fs() in such cases does no good.
1842 */
f55abc0f 1843 if (!sb_any_quota_loaded(sb)) {
9377abd0
JK
1844 mutex_unlock(&dqopt->dqonoff_mutex);
1845 return 0;
1846 }
1da177e4
LT
1847 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1848 toputinode[cnt] = NULL;
1da177e4
LT
1849 if (type != -1 && cnt != type)
1850 continue;
f55abc0f 1851 if (!sb_has_quota_loaded(sb, cnt))
0ff5af83 1852 continue;
f55abc0f
JK
1853
1854 if (flags & DQUOT_SUSPENDED) {
cc33412f 1855 spin_lock(&dq_state_lock);
f55abc0f
JK
1856 dqopt->flags |=
1857 dquot_state_flag(DQUOT_SUSPENDED, cnt);
cc33412f 1858 spin_unlock(&dq_state_lock);
f55abc0f 1859 } else {
cc33412f 1860 spin_lock(&dq_state_lock);
f55abc0f
JK
1861 dqopt->flags &= ~dquot_state_flag(flags, cnt);
1862 /* Turning off suspended quotas? */
1863 if (!sb_has_quota_loaded(sb, cnt) &&
1864 sb_has_quota_suspended(sb, cnt)) {
1865 dqopt->flags &= ~dquot_state_flag(
1866 DQUOT_SUSPENDED, cnt);
cc33412f 1867 spin_unlock(&dq_state_lock);
f55abc0f
JK
1868 iput(dqopt->files[cnt]);
1869 dqopt->files[cnt] = NULL;
1870 continue;
1871 }
cc33412f 1872 spin_unlock(&dq_state_lock);
0ff5af83 1873 }
f55abc0f
JK
1874
1875 /* We still have to keep quota loaded? */
1876 if (sb_has_quota_loaded(sb, cnt) && !(flags & DQUOT_SUSPENDED))
1da177e4 1877 continue;
1da177e4
LT
1878
1879 /* Note: these are blocking operations */
1880 drop_dquot_ref(sb, cnt);
1881 invalidate_dquots(sb, cnt);
1882 /*
1883 * Now all dquots should be invalidated, all writes done so we should be only
1884 * users of the info. No locks needed.
1885 */
1886 if (info_dirty(&dqopt->info[cnt]))
1887 sb->dq_op->write_info(sb, cnt);
1888 if (dqopt->ops[cnt]->free_file_info)
1889 dqopt->ops[cnt]->free_file_info(sb, cnt);
1890 put_quota_format(dqopt->info[cnt].dqi_format);
1891
1892 toputinode[cnt] = dqopt->files[cnt];
f55abc0f 1893 if (!sb_has_quota_loaded(sb, cnt))
0ff5af83 1894 dqopt->files[cnt] = NULL;
1da177e4
LT
1895 dqopt->info[cnt].dqi_flags = 0;
1896 dqopt->info[cnt].dqi_igrace = 0;
1897 dqopt->info[cnt].dqi_bgrace = 0;
1898 dqopt->ops[cnt] = NULL;
1899 }
d3be915f 1900 mutex_unlock(&dqopt->dqonoff_mutex);
ca785ec6
JK
1901
1902 /* Skip syncing and setting flags if quota files are hidden */
1903 if (dqopt->flags & DQUOT_QUOTA_SYS_FILE)
1904 goto put_inodes;
1905
1da177e4 1906 /* Sync the superblock so that buffers with quota data are written to
7b7b1ace 1907 * disk (and so userspace sees correct data afterwards). */
1da177e4
LT
1908 if (sb->s_op->sync_fs)
1909 sb->s_op->sync_fs(sb, 1);
1910 sync_blockdev(sb->s_bdev);
1911 /* Now the quota files are just ordinary files and we can set the
1912 * inode flags back. Moreover we discard the pagecache so that
1913 * userspace sees the writes we did bypassing the pagecache. We
1914 * must also discard the blockdev buffers so that we see the
1915 * changes done by userspace on the next quotaon() */
1916 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1917 if (toputinode[cnt]) {
d3be915f 1918 mutex_lock(&dqopt->dqonoff_mutex);
1da177e4
LT
1919 /* If quota was reenabled in the meantime, we have
1920 * nothing to do */
f55abc0f 1921 if (!sb_has_quota_loaded(sb, cnt)) {
7925409e 1922 mutex_lock_nested(&toputinode[cnt]->i_mutex, I_MUTEX_QUOTA);
1da177e4
LT
1923 toputinode[cnt]->i_flags &= ~(S_IMMUTABLE |
1924 S_NOATIME | S_NOQUOTA);
1925 truncate_inode_pages(&toputinode[cnt]->i_data, 0);
1b1dcc1b 1926 mutex_unlock(&toputinode[cnt]->i_mutex);
1da177e4 1927 mark_inode_dirty(toputinode[cnt]);
1da177e4 1928 }
d3be915f 1929 mutex_unlock(&dqopt->dqonoff_mutex);
ca785ec6
JK
1930 }
1931 if (sb->s_bdev)
1932 invalidate_bdev(sb->s_bdev);
1933put_inodes:
1934 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1935 if (toputinode[cnt]) {
0ff5af83 1936 /* On remount RO, we keep the inode pointer so that we
f55abc0f
JK
1937 * can reenable quota on the subsequent remount RW. We
1938 * have to check 'flags' variable and not use sb_has_
1939 * function because another quotaon / quotaoff could
1940 * change global state before we got here. We refuse
1941 * to suspend quotas when there is pending delete on
1942 * the quota file... */
1943 if (!(flags & DQUOT_SUSPENDED))
0ff5af83
JK
1944 iput(toputinode[cnt]);
1945 else if (!toputinode[cnt]->i_nlink)
1946 ret = -EBUSY;
1da177e4 1947 }
0ff5af83 1948 return ret;
1da177e4 1949}
08d0350c 1950EXPORT_SYMBOL(vfs_quota_disable);
1da177e4 1951
f55abc0f
JK
1952int vfs_quota_off(struct super_block *sb, int type, int remount)
1953{
1954 return vfs_quota_disable(sb, type, remount ? DQUOT_SUSPENDED :
1955 (DQUOT_USAGE_ENABLED | DQUOT_LIMITS_ENABLED));
1956}
08d0350c 1957EXPORT_SYMBOL(vfs_quota_off);
1da177e4
LT
1958/*
1959 * Turn quotas on on a device
1960 */
1961
f55abc0f
JK
1962/*
1963 * Helper function to turn quotas on when we already have the inode of
1964 * quota file and no quota information is loaded.
1965 */
1966static int vfs_load_quota_inode(struct inode *inode, int type, int format_id,
1967 unsigned int flags)
1da177e4
LT
1968{
1969 struct quota_format_type *fmt = find_quota_format(format_id);
1970 struct super_block *sb = inode->i_sb;
1971 struct quota_info *dqopt = sb_dqopt(sb);
1972 int error;
1973 int oldflags = -1;
1974
1975 if (!fmt)
1976 return -ESRCH;
1977 if (!S_ISREG(inode->i_mode)) {
1978 error = -EACCES;
1979 goto out_fmt;
1980 }
1981 if (IS_RDONLY(inode)) {
1982 error = -EROFS;
1983 goto out_fmt;
1984 }
1985 if (!sb->s_op->quota_write || !sb->s_op->quota_read) {
1986 error = -EINVAL;
1987 goto out_fmt;
1988 }
f55abc0f
JK
1989 /* Usage always has to be set... */
1990 if (!(flags & DQUOT_USAGE_ENABLED)) {
1991 error = -EINVAL;
1992 goto out_fmt;
1993 }
1da177e4 1994
ca785ec6
JK
1995 if (!(dqopt->flags & DQUOT_QUOTA_SYS_FILE)) {
1996 /* As we bypass the pagecache we must now flush the inode so
1997 * that we see all the changes from userspace... */
1998 write_inode_now(inode, 1);
1999 /* And now flush the block cache so that kernel sees the
2000 * changes */
2001 invalidate_bdev(sb->s_bdev);
2002 }
1b1dcc1b 2003 mutex_lock(&inode->i_mutex);
d3be915f 2004 mutex_lock(&dqopt->dqonoff_mutex);
f55abc0f 2005 if (sb_has_quota_loaded(sb, type)) {
1da177e4
LT
2006 error = -EBUSY;
2007 goto out_lock;
2008 }
ca785ec6
JK
2009
2010 if (!(dqopt->flags & DQUOT_QUOTA_SYS_FILE)) {
2011 /* We don't want quota and atime on quota files (deadlocks
2012 * possible) Also nobody should write to the file - we use
2013 * special IO operations which ignore the immutable bit. */
2014 down_write(&dqopt->dqptr_sem);
2015 oldflags = inode->i_flags & (S_NOATIME | S_IMMUTABLE | S_NOQUOTA);
2016 inode->i_flags |= S_NOQUOTA | S_NOATIME | S_IMMUTABLE;
2017 up_write(&dqopt->dqptr_sem);
2018 sb->dq_op->drop(inode);
2019 }
1da177e4
LT
2020
2021 error = -EIO;
2022 dqopt->files[type] = igrab(inode);
2023 if (!dqopt->files[type])
2024 goto out_lock;
2025 error = -EINVAL;
2026 if (!fmt->qf_ops->check_quota_file(sb, type))
2027 goto out_file_init;
2028
2029 dqopt->ops[type] = fmt->qf_ops;
2030 dqopt->info[type].dqi_format = fmt;
0ff5af83 2031 dqopt->info[type].dqi_fmt_id = format_id;
1da177e4 2032 INIT_LIST_HEAD(&dqopt->info[type].dqi_dirty_list);
d3be915f 2033 mutex_lock(&dqopt->dqio_mutex);
1da177e4 2034 if ((error = dqopt->ops[type]->read_file_info(sb, type)) < 0) {
d3be915f 2035 mutex_unlock(&dqopt->dqio_mutex);
1da177e4
LT
2036 goto out_file_init;
2037 }
d3be915f 2038 mutex_unlock(&dqopt->dqio_mutex);
1b1dcc1b 2039 mutex_unlock(&inode->i_mutex);
cc33412f 2040 spin_lock(&dq_state_lock);
f55abc0f 2041 dqopt->flags |= dquot_state_flag(flags, type);
cc33412f 2042 spin_unlock(&dq_state_lock);
1da177e4
LT
2043
2044 add_dquot_ref(sb, type);
d3be915f 2045 mutex_unlock(&dqopt->dqonoff_mutex);
1da177e4
LT
2046
2047 return 0;
2048
2049out_file_init:
2050 dqopt->files[type] = NULL;
2051 iput(inode);
2052out_lock:
d3be915f 2053 mutex_unlock(&dqopt->dqonoff_mutex);
1da177e4
LT
2054 if (oldflags != -1) {
2055 down_write(&dqopt->dqptr_sem);
2056 /* Set the flags back (in the case of accidental quotaon()
2057 * on a wrong file we don't want to mess up the flags) */
2058 inode->i_flags &= ~(S_NOATIME | S_NOQUOTA | S_IMMUTABLE);
2059 inode->i_flags |= oldflags;
2060 up_write(&dqopt->dqptr_sem);
2061 }
1b1dcc1b 2062 mutex_unlock(&inode->i_mutex);
1da177e4
LT
2063out_fmt:
2064 put_quota_format(fmt);
2065
2066 return error;
2067}
2068
0ff5af83
JK
2069/* Reenable quotas on remount RW */
2070static int vfs_quota_on_remount(struct super_block *sb, int type)
2071{
2072 struct quota_info *dqopt = sb_dqopt(sb);
2073 struct inode *inode;
2074 int ret;
f55abc0f 2075 unsigned int flags;
0ff5af83
JK
2076
2077 mutex_lock(&dqopt->dqonoff_mutex);
2078 if (!sb_has_quota_suspended(sb, type)) {
2079 mutex_unlock(&dqopt->dqonoff_mutex);
2080 return 0;
2081 }
0ff5af83
JK
2082 inode = dqopt->files[type];
2083 dqopt->files[type] = NULL;
cc33412f 2084 spin_lock(&dq_state_lock);
f55abc0f
JK
2085 flags = dqopt->flags & dquot_state_flag(DQUOT_USAGE_ENABLED |
2086 DQUOT_LIMITS_ENABLED, type);
2087 dqopt->flags &= ~dquot_state_flag(DQUOT_STATE_FLAGS, type);
cc33412f 2088 spin_unlock(&dq_state_lock);
0ff5af83
JK
2089 mutex_unlock(&dqopt->dqonoff_mutex);
2090
f55abc0f
JK
2091 flags = dquot_generic_flag(flags, type);
2092 ret = vfs_load_quota_inode(inode, type, dqopt->info[type].dqi_fmt_id,
2093 flags);
0ff5af83
JK
2094 iput(inode);
2095
2096 return ret;
2097}
2098
77e69dac
AV
2099int vfs_quota_on_path(struct super_block *sb, int type, int format_id,
2100 struct path *path)
2101{
2102 int error = security_quota_on(path->dentry);
2103 if (error)
2104 return error;
2105 /* Quota file not on the same filesystem? */
2106 if (path->mnt->mnt_sb != sb)
2107 error = -EXDEV;
2108 else
f55abc0f
JK
2109 error = vfs_load_quota_inode(path->dentry->d_inode, type,
2110 format_id, DQUOT_USAGE_ENABLED |
2111 DQUOT_LIMITS_ENABLED);
77e69dac
AV
2112 return error;
2113}
08d0350c 2114EXPORT_SYMBOL(vfs_quota_on_path);
77e69dac 2115
8264613d 2116int vfs_quota_on(struct super_block *sb, int type, int format_id, char *name,
0ff5af83 2117 int remount)
1da177e4 2118{
8264613d 2119 struct path path;
1da177e4
LT
2120 int error;
2121
0ff5af83
JK
2122 if (remount)
2123 return vfs_quota_on_remount(sb, type);
2124
8264613d 2125 error = kern_path(name, LOOKUP_FOLLOW, &path);
77e69dac 2126 if (!error) {
8264613d
AV
2127 error = vfs_quota_on_path(sb, type, format_id, &path);
2128 path_put(&path);
77e69dac 2129 }
1da177e4
LT
2130 return error;
2131}
08d0350c 2132EXPORT_SYMBOL(vfs_quota_on);
1da177e4 2133
f55abc0f
JK
2134/*
2135 * More powerful function for turning on quotas allowing setting
2136 * of individual quota flags
2137 */
2138int vfs_quota_enable(struct inode *inode, int type, int format_id,
2139 unsigned int flags)
2140{
2141 int ret = 0;
2142 struct super_block *sb = inode->i_sb;
2143 struct quota_info *dqopt = sb_dqopt(sb);
2144
2145 /* Just unsuspend quotas? */
2146 if (flags & DQUOT_SUSPENDED)
2147 return vfs_quota_on_remount(sb, type);
2148 if (!flags)
2149 return 0;
2150 /* Just updating flags needed? */
2151 if (sb_has_quota_loaded(sb, type)) {
2152 mutex_lock(&dqopt->dqonoff_mutex);
2153 /* Now do a reliable test... */
2154 if (!sb_has_quota_loaded(sb, type)) {
2155 mutex_unlock(&dqopt->dqonoff_mutex);
2156 goto load_quota;
2157 }
2158 if (flags & DQUOT_USAGE_ENABLED &&
2159 sb_has_quota_usage_enabled(sb, type)) {
2160 ret = -EBUSY;
2161 goto out_lock;
2162 }
2163 if (flags & DQUOT_LIMITS_ENABLED &&
2164 sb_has_quota_limits_enabled(sb, type)) {
2165 ret = -EBUSY;
2166 goto out_lock;
2167 }
cc33412f 2168 spin_lock(&dq_state_lock);
f55abc0f 2169 sb_dqopt(sb)->flags |= dquot_state_flag(flags, type);
cc33412f 2170 spin_unlock(&dq_state_lock);
f55abc0f
JK
2171out_lock:
2172 mutex_unlock(&dqopt->dqonoff_mutex);
2173 return ret;
2174 }
2175
2176load_quota:
2177 return vfs_load_quota_inode(inode, type, format_id, flags);
2178}
08d0350c 2179EXPORT_SYMBOL(vfs_quota_enable);
f55abc0f 2180
1da177e4
LT
2181/*
2182 * This function is used when filesystem needs to initialize quotas
2183 * during mount time.
2184 */
84de856e
CH
2185int vfs_quota_on_mount(struct super_block *sb, char *qf_name,
2186 int format_id, int type)
1da177e4 2187{
84de856e 2188 struct dentry *dentry;
1da177e4
LT
2189 int error;
2190
2fa389c5 2191 dentry = lookup_one_len(qf_name, sb->s_root, strlen(qf_name));
84de856e
CH
2192 if (IS_ERR(dentry))
2193 return PTR_ERR(dentry);
2194
154f484b
JK
2195 if (!dentry->d_inode) {
2196 error = -ENOENT;
2197 goto out;
2198 }
2199
1da177e4 2200 error = security_quota_on(dentry);
84de856e 2201 if (!error)
f55abc0f
JK
2202 error = vfs_load_quota_inode(dentry->d_inode, type, format_id,
2203 DQUOT_USAGE_ENABLED | DQUOT_LIMITS_ENABLED);
84de856e 2204
154f484b 2205out:
84de856e
CH
2206 dput(dentry);
2207 return error;
1da177e4 2208}
08d0350c 2209EXPORT_SYMBOL(vfs_quota_on_mount);
1da177e4 2210
b85f4b87
JK
2211/* Wrapper to turn on quotas when remounting rw */
2212int vfs_dq_quota_on_remount(struct super_block *sb)
2213{
2214 int cnt;
2215 int ret = 0, err;
2216
2217 if (!sb->s_qcop || !sb->s_qcop->quota_on)
2218 return -ENOSYS;
2219 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
2220 err = sb->s_qcop->quota_on(sb, cnt, 0, NULL, 1);
2221 if (err < 0 && !ret)
2222 ret = err;
2223 }
2224 return ret;
2225}
08d0350c 2226EXPORT_SYMBOL(vfs_dq_quota_on_remount);
b85f4b87 2227
12095460
JK
2228static inline qsize_t qbtos(qsize_t blocks)
2229{
2230 return blocks << QIF_DQBLKSIZE_BITS;
2231}
2232
2233static inline qsize_t stoqb(qsize_t space)
2234{
2235 return (space + QIF_DQBLKSIZE - 1) >> QIF_DQBLKSIZE_BITS;
2236}
2237
1da177e4
LT
2238/* Generic routine for getting common part of quota structure */
2239static void do_get_dqblk(struct dquot *dquot, struct if_dqblk *di)
2240{
2241 struct mem_dqblk *dm = &dquot->dq_dqb;
2242
2243 spin_lock(&dq_data_lock);
12095460
JK
2244 di->dqb_bhardlimit = stoqb(dm->dqb_bhardlimit);
2245 di->dqb_bsoftlimit = stoqb(dm->dqb_bsoftlimit);
f18df228 2246 di->dqb_curspace = dm->dqb_curspace + dm->dqb_rsvspace;
1da177e4
LT
2247 di->dqb_ihardlimit = dm->dqb_ihardlimit;
2248 di->dqb_isoftlimit = dm->dqb_isoftlimit;
2249 di->dqb_curinodes = dm->dqb_curinodes;
2250 di->dqb_btime = dm->dqb_btime;
2251 di->dqb_itime = dm->dqb_itime;
2252 di->dqb_valid = QIF_ALL;
2253 spin_unlock(&dq_data_lock);
2254}
2255
2256int vfs_get_dqblk(struct super_block *sb, int type, qid_t id, struct if_dqblk *di)
2257{
2258 struct dquot *dquot;
2259
cc33412f 2260 dquot = dqget(sb, id, type);
dd6f3c6d 2261 if (!dquot)
1da177e4 2262 return -ESRCH;
1da177e4
LT
2263 do_get_dqblk(dquot, di);
2264 dqput(dquot);
cc33412f 2265
1da177e4
LT
2266 return 0;
2267}
08d0350c 2268EXPORT_SYMBOL(vfs_get_dqblk);
1da177e4
LT
2269
2270/* Generic routine for setting common part of quota structure */
338bf9af 2271static int do_set_dqblk(struct dquot *dquot, struct if_dqblk *di)
1da177e4
LT
2272{
2273 struct mem_dqblk *dm = &dquot->dq_dqb;
2274 int check_blim = 0, check_ilim = 0;
338bf9af
AP
2275 struct mem_dqinfo *dqi = &sb_dqopt(dquot->dq_sb)->info[dquot->dq_type];
2276
2277 if ((di->dqb_valid & QIF_BLIMITS &&
2278 (di->dqb_bhardlimit > dqi->dqi_maxblimit ||
2279 di->dqb_bsoftlimit > dqi->dqi_maxblimit)) ||
2280 (di->dqb_valid & QIF_ILIMITS &&
2281 (di->dqb_ihardlimit > dqi->dqi_maxilimit ||
2282 di->dqb_isoftlimit > dqi->dqi_maxilimit)))
2283 return -ERANGE;
1da177e4
LT
2284
2285 spin_lock(&dq_data_lock);
2286 if (di->dqb_valid & QIF_SPACE) {
f18df228 2287 dm->dqb_curspace = di->dqb_curspace - dm->dqb_rsvspace;
1da177e4 2288 check_blim = 1;
4d59bce4 2289 __set_bit(DQ_LASTSET_B + QIF_SPACE_B, &dquot->dq_flags);
1da177e4
LT
2290 }
2291 if (di->dqb_valid & QIF_BLIMITS) {
12095460
JK
2292 dm->dqb_bsoftlimit = qbtos(di->dqb_bsoftlimit);
2293 dm->dqb_bhardlimit = qbtos(di->dqb_bhardlimit);
1da177e4 2294 check_blim = 1;
4d59bce4 2295 __set_bit(DQ_LASTSET_B + QIF_BLIMITS_B, &dquot->dq_flags);
1da177e4
LT
2296 }
2297 if (di->dqb_valid & QIF_INODES) {
2298 dm->dqb_curinodes = di->dqb_curinodes;
2299 check_ilim = 1;
4d59bce4 2300 __set_bit(DQ_LASTSET_B + QIF_INODES_B, &dquot->dq_flags);
1da177e4
LT
2301 }
2302 if (di->dqb_valid & QIF_ILIMITS) {
2303 dm->dqb_isoftlimit = di->dqb_isoftlimit;
2304 dm->dqb_ihardlimit = di->dqb_ihardlimit;
2305 check_ilim = 1;
4d59bce4 2306 __set_bit(DQ_LASTSET_B + QIF_ILIMITS_B, &dquot->dq_flags);
1da177e4 2307 }
4d59bce4 2308 if (di->dqb_valid & QIF_BTIME) {
1da177e4 2309 dm->dqb_btime = di->dqb_btime;
e04a88a9 2310 check_blim = 1;
4d59bce4
JK
2311 __set_bit(DQ_LASTSET_B + QIF_BTIME_B, &dquot->dq_flags);
2312 }
2313 if (di->dqb_valid & QIF_ITIME) {
1da177e4 2314 dm->dqb_itime = di->dqb_itime;
e04a88a9 2315 check_ilim = 1;
4d59bce4
JK
2316 __set_bit(DQ_LASTSET_B + QIF_ITIME_B, &dquot->dq_flags);
2317 }
1da177e4
LT
2318
2319 if (check_blim) {
12095460 2320 if (!dm->dqb_bsoftlimit || dm->dqb_curspace < dm->dqb_bsoftlimit) {
1da177e4
LT
2321 dm->dqb_btime = 0;
2322 clear_bit(DQ_BLKS_B, &dquot->dq_flags);
2323 }
2324 else if (!(di->dqb_valid & QIF_BTIME)) /* Set grace only if user hasn't provided his own... */
338bf9af 2325 dm->dqb_btime = get_seconds() + dqi->dqi_bgrace;
1da177e4
LT
2326 }
2327 if (check_ilim) {
2328 if (!dm->dqb_isoftlimit || dm->dqb_curinodes < dm->dqb_isoftlimit) {
2329 dm->dqb_itime = 0;
2330 clear_bit(DQ_INODES_B, &dquot->dq_flags);
2331 }
2332 else if (!(di->dqb_valid & QIF_ITIME)) /* Set grace only if user hasn't provided his own... */
338bf9af 2333 dm->dqb_itime = get_seconds() + dqi->dqi_igrace;
1da177e4
LT
2334 }
2335 if (dm->dqb_bhardlimit || dm->dqb_bsoftlimit || dm->dqb_ihardlimit || dm->dqb_isoftlimit)
2336 clear_bit(DQ_FAKE_B, &dquot->dq_flags);
2337 else
2338 set_bit(DQ_FAKE_B, &dquot->dq_flags);
2339 spin_unlock(&dq_data_lock);
2340 mark_dquot_dirty(dquot);
338bf9af
AP
2341
2342 return 0;
1da177e4
LT
2343}
2344
2345int vfs_set_dqblk(struct super_block *sb, int type, qid_t id, struct if_dqblk *di)
2346{
2347 struct dquot *dquot;
338bf9af 2348 int rc;
1da177e4 2349
f55abc0f
JK
2350 dquot = dqget(sb, id, type);
2351 if (!dquot) {
2352 rc = -ESRCH;
2353 goto out;
1da177e4 2354 }
338bf9af 2355 rc = do_set_dqblk(dquot, di);
1da177e4 2356 dqput(dquot);
f55abc0f 2357out:
338bf9af 2358 return rc;
1da177e4 2359}
08d0350c 2360EXPORT_SYMBOL(vfs_set_dqblk);
1da177e4
LT
2361
2362/* Generic routine for getting common part of quota file information */
2363int vfs_get_dqinfo(struct super_block *sb, int type, struct if_dqinfo *ii)
2364{
2365 struct mem_dqinfo *mi;
2366
d3be915f 2367 mutex_lock(&sb_dqopt(sb)->dqonoff_mutex);
f55abc0f 2368 if (!sb_has_quota_active(sb, type)) {
d3be915f 2369 mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
1da177e4
LT
2370 return -ESRCH;
2371 }
2372 mi = sb_dqopt(sb)->info + type;
2373 spin_lock(&dq_data_lock);
2374 ii->dqi_bgrace = mi->dqi_bgrace;
2375 ii->dqi_igrace = mi->dqi_igrace;
2376 ii->dqi_flags = mi->dqi_flags & DQF_MASK;
2377 ii->dqi_valid = IIF_ALL;
2378 spin_unlock(&dq_data_lock);
d3be915f 2379 mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
1da177e4
LT
2380 return 0;
2381}
08d0350c 2382EXPORT_SYMBOL(vfs_get_dqinfo);
1da177e4
LT
2383
2384/* Generic routine for setting common part of quota file information */
2385int vfs_set_dqinfo(struct super_block *sb, int type, struct if_dqinfo *ii)
2386{
2387 struct mem_dqinfo *mi;
f55abc0f 2388 int err = 0;
1da177e4 2389
d3be915f 2390 mutex_lock(&sb_dqopt(sb)->dqonoff_mutex);
f55abc0f
JK
2391 if (!sb_has_quota_active(sb, type)) {
2392 err = -ESRCH;
2393 goto out;
1da177e4
LT
2394 }
2395 mi = sb_dqopt(sb)->info + type;
2396 spin_lock(&dq_data_lock);
2397 if (ii->dqi_valid & IIF_BGRACE)
2398 mi->dqi_bgrace = ii->dqi_bgrace;
2399 if (ii->dqi_valid & IIF_IGRACE)
2400 mi->dqi_igrace = ii->dqi_igrace;
2401 if (ii->dqi_valid & IIF_FLAGS)
2402 mi->dqi_flags = (mi->dqi_flags & ~DQF_MASK) | (ii->dqi_flags & DQF_MASK);
2403 spin_unlock(&dq_data_lock);
2404 mark_info_dirty(sb, type);
2405 /* Force write to disk */
2406 sb->dq_op->write_info(sb, type);
f55abc0f 2407out:
d3be915f 2408 mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
f55abc0f 2409 return err;
1da177e4 2410}
08d0350c 2411EXPORT_SYMBOL(vfs_set_dqinfo);
1da177e4
LT
2412
2413struct quotactl_ops vfs_quotactl_ops = {
2414 .quota_on = vfs_quota_on,
2415 .quota_off = vfs_quota_off,
2416 .quota_sync = vfs_quota_sync,
2417 .get_info = vfs_get_dqinfo,
2418 .set_info = vfs_set_dqinfo,
2419 .get_dqblk = vfs_get_dqblk,
2420 .set_dqblk = vfs_set_dqblk
2421};
2422
2423static ctl_table fs_dqstats_table[] = {
2424 {
2425 .ctl_name = FS_DQ_LOOKUPS,
2426 .procname = "lookups",
2427 .data = &dqstats.lookups,
2428 .maxlen = sizeof(int),
2429 .mode = 0444,
2430 .proc_handler = &proc_dointvec,
2431 },
2432 {
2433 .ctl_name = FS_DQ_DROPS,
2434 .procname = "drops",
2435 .data = &dqstats.drops,
2436 .maxlen = sizeof(int),
2437 .mode = 0444,
2438 .proc_handler = &proc_dointvec,
2439 },
2440 {
2441 .ctl_name = FS_DQ_READS,
2442 .procname = "reads",
2443 .data = &dqstats.reads,
2444 .maxlen = sizeof(int),
2445 .mode = 0444,
2446 .proc_handler = &proc_dointvec,
2447 },
2448 {
2449 .ctl_name = FS_DQ_WRITES,
2450 .procname = "writes",
2451 .data = &dqstats.writes,
2452 .maxlen = sizeof(int),
2453 .mode = 0444,
2454 .proc_handler = &proc_dointvec,
2455 },
2456 {
2457 .ctl_name = FS_DQ_CACHE_HITS,
2458 .procname = "cache_hits",
2459 .data = &dqstats.cache_hits,
2460 .maxlen = sizeof(int),
2461 .mode = 0444,
2462 .proc_handler = &proc_dointvec,
2463 },
2464 {
2465 .ctl_name = FS_DQ_ALLOCATED,
2466 .procname = "allocated_dquots",
2467 .data = &dqstats.allocated_dquots,
2468 .maxlen = sizeof(int),
2469 .mode = 0444,
2470 .proc_handler = &proc_dointvec,
2471 },
2472 {
2473 .ctl_name = FS_DQ_FREE,
2474 .procname = "free_dquots",
2475 .data = &dqstats.free_dquots,
2476 .maxlen = sizeof(int),
2477 .mode = 0444,
2478 .proc_handler = &proc_dointvec,
2479 },
2480 {
2481 .ctl_name = FS_DQ_SYNCS,
2482 .procname = "syncs",
2483 .data = &dqstats.syncs,
2484 .maxlen = sizeof(int),
2485 .mode = 0444,
2486 .proc_handler = &proc_dointvec,
2487 },
8e893469 2488#ifdef CONFIG_PRINT_QUOTA_WARNING
1da177e4
LT
2489 {
2490 .ctl_name = FS_DQ_WARNINGS,
2491 .procname = "warnings",
2492 .data = &flag_print_warnings,
2493 .maxlen = sizeof(int),
2494 .mode = 0644,
2495 .proc_handler = &proc_dointvec,
2496 },
8e893469 2497#endif
1da177e4
LT
2498 { .ctl_name = 0 },
2499};
2500
2501static ctl_table fs_table[] = {
2502 {
2503 .ctl_name = FS_DQSTATS,
2504 .procname = "quota",
2505 .mode = 0555,
2506 .child = fs_dqstats_table,
2507 },
2508 { .ctl_name = 0 },
2509};
2510
2511static ctl_table sys_table[] = {
2512 {
2513 .ctl_name = CTL_FS,
2514 .procname = "fs",
2515 .mode = 0555,
2516 .child = fs_table,
2517 },
2518 { .ctl_name = 0 },
2519};
2520
2521static int __init dquot_init(void)
2522{
2523 int i;
2524 unsigned long nr_hash, order;
2525
2526 printk(KERN_NOTICE "VFS: Disk quotas %s\n", __DQUOT_VERSION__);
2527
0b4d4147 2528 register_sysctl_table(sys_table);
1da177e4 2529
20c2df83 2530 dquot_cachep = kmem_cache_create("dquot",
1da177e4 2531 sizeof(struct dquot), sizeof(unsigned long) * 4,
fffb60f9
PJ
2532 (SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT|
2533 SLAB_MEM_SPREAD|SLAB_PANIC),
20c2df83 2534 NULL);
1da177e4
LT
2535
2536 order = 0;
2537 dquot_hash = (struct hlist_head *)__get_free_pages(GFP_ATOMIC, order);
2538 if (!dquot_hash)
2539 panic("Cannot create dquot hash table");
2540
2541 /* Find power-of-two hlist_heads which can fit into allocation */
2542 nr_hash = (1UL << order) * PAGE_SIZE / sizeof(struct hlist_head);
2543 dq_hash_bits = 0;
2544 do {
2545 dq_hash_bits++;
2546 } while (nr_hash >> dq_hash_bits);
2547 dq_hash_bits--;
2548
2549 nr_hash = 1UL << dq_hash_bits;
2550 dq_hash_mask = nr_hash - 1;
2551 for (i = 0; i < nr_hash; i++)
2552 INIT_HLIST_HEAD(dquot_hash + i);
2553
2554 printk("Dquot-cache hash table entries: %ld (order %ld, %ld bytes)\n",
2555 nr_hash, order, (PAGE_SIZE << order));
2556
8e1f936b 2557 register_shrinker(&dqcache_shrinker);
1da177e4 2558
8e893469
JK
2559#ifdef CONFIG_QUOTA_NETLINK_INTERFACE
2560 if (genl_register_family(&quota_genl_family) != 0)
2561 printk(KERN_ERR "VFS: Failed to create quota netlink interface.\n");
2562#endif
2563
1da177e4
LT
2564 return 0;
2565}
2566module_init(dquot_init);