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