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