]> bbs.cooldavid.org Git - net-next-2.6.git/blame - fs/dquot.c
[X25]: Trivial, SOCK_DEBUG's in x25_facilities missing newlines
[net-next-2.6.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 *
12 * Version: $Id: dquot.c,v 6.3 1996/11/17 18:35:34 mvw Exp mvw $
13 *
14 * Author: Marco van Wieringen <mvw@planets.elm.net>
15 *
16 * Fixes: Dmitry Gorodchanin <pgmdsg@ibi.com>, 11 Feb 96
17 *
18 * Revised list management to avoid races
19 * -- Bill Hawes, <whawes@star.net>, 9/98
20 *
21 * Fixed races in dquot_transfer(), dqget() and dquot_alloc_...().
22 * As the consequence the locking was moved from dquot_decr_...(),
23 * dquot_incr_...() to calling functions.
24 * invalidate_dquots() now writes modified dquots.
25 * Serialized quota_off() and quota_on() for mount point.
26 * Fixed a few bugs in grow_dquots().
27 * Fixed deadlock in write_dquot() - we no longer account quotas on
28 * quota files
29 * remove_dquot_ref() moved to inode.c - it now traverses through inodes
30 * add_dquot_ref() restarts after blocking
31 * Added check for bogus uid and fixed check for group in quotactl.
32 * Jan Kara, <jack@suse.cz>, sponsored by SuSE CR, 10-11/99
33 *
34 * Used struct list_head instead of own list struct
35 * Invalidation of referenced dquots is no longer possible
36 * Improved free_dquots list management
37 * Quota and i_blocks are now updated in one place to avoid races
38 * Warnings are now delayed so we won't block in critical section
39 * Write updated not to require dquot lock
40 * Jan Kara, <jack@suse.cz>, 9/2000
41 *
42 * Added dynamic quota structure allocation
43 * Jan Kara <jack@suse.cz> 12/2000
44 *
45 * Rewritten quota interface. Implemented new quota format and
46 * formats registering.
47 * Jan Kara, <jack@suse.cz>, 2001,2002
48 *
49 * New SMP locking.
50 * Jan Kara, <jack@suse.cz>, 10/2002
51 *
52 * Added journalled quota support, fix lock inversion problems
53 * Jan Kara, <jack@suse.cz>, 2003,2004
54 *
55 * (C) Copyright 1994 - 1997 Marco van Wieringen
56 */
57
58#include <linux/errno.h>
59#include <linux/kernel.h>
60#include <linux/fs.h>
61#include <linux/mount.h>
62#include <linux/mm.h>
63#include <linux/time.h>
64#include <linux/types.h>
65#include <linux/string.h>
66#include <linux/fcntl.h>
67#include <linux/stat.h>
68#include <linux/tty.h>
69#include <linux/file.h>
70#include <linux/slab.h>
71#include <linux/sysctl.h>
72#include <linux/smp_lock.h>
73#include <linux/init.h>
74#include <linux/module.h>
75#include <linux/proc_fs.h>
76#include <linux/security.h>
77#include <linux/kmod.h>
78#include <linux/namei.h>
79#include <linux/buffer_head.h>
16f7e0fe 80#include <linux/capability.h>
be586bab 81#include <linux/quotaops.h>
1da177e4
LT
82
83#include <asm/uaccess.h>
84
85#define __DQUOT_PARANOIA
86
87/*
88 * There are two quota SMP locks. dq_list_lock protects all lists with quotas
89 * and quota formats and also dqstats structure containing statistics about the
90 * lists. dq_data_lock protects data from dq_dqb and also mem_dqinfo structures
91 * and also guards consistency of dquot->dq_dqb with inode->i_blocks, i_bytes.
92 * i_blocks and i_bytes updates itself are guarded by i_lock acquired directly
93 * in inode_add_bytes() and inode_sub_bytes().
94 *
95 * The spinlock ordering is hence: dq_data_lock > dq_list_lock > i_lock
96 *
97 * Note that some things (eg. sb pointer, type, id) doesn't change during
98 * the life of the dquot structure and so needn't to be protected by a lock
99 *
100 * Any operation working on dquots via inode pointers must hold dqptr_sem. If
101 * operation is just reading pointers from inode (or not using them at all) the
102 * read lock is enough. If pointers are altered function must hold write lock
103 * (these locking rules also apply for S_NOQUOTA flag in the inode - note that
1b1dcc1b 104 * for altering the flag i_mutex is also needed). If operation is holding
1da177e4 105 * reference to dquot in other way (e.g. quotactl ops) it must be guarded by
d3be915f 106 * dqonoff_mutex.
1da177e4
LT
107 * This locking assures that:
108 * a) update/access to dquot pointers in inode is serialized
109 * b) everyone is guarded against invalidate_dquots()
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
123 * i_mutex on quota files is special (it's below dqio_mutex)
1da177e4
LT
124 */
125
126static DEFINE_SPINLOCK(dq_list_lock);
127DEFINE_SPINLOCK(dq_data_lock);
128
129static char *quotatypes[] = INITQFNAMES;
130static struct quota_format_type *quota_formats; /* List of registered formats */
131static struct quota_module_name module_names[] = INIT_QUOTA_MODULE_NAMES;
132
133/* SLAB cache for dquot structures */
e18b890b 134static struct kmem_cache *dquot_cachep;
1da177e4
LT
135
136int register_quota_format(struct quota_format_type *fmt)
137{
138 spin_lock(&dq_list_lock);
139 fmt->qf_next = quota_formats;
140 quota_formats = fmt;
141 spin_unlock(&dq_list_lock);
142 return 0;
143}
144
145void unregister_quota_format(struct quota_format_type *fmt)
146{
147 struct quota_format_type **actqf;
148
149 spin_lock(&dq_list_lock);
150 for (actqf = &quota_formats; *actqf && *actqf != fmt; actqf = &(*actqf)->qf_next);
151 if (*actqf)
152 *actqf = (*actqf)->qf_next;
153 spin_unlock(&dq_list_lock);
154}
155
156static struct quota_format_type *find_quota_format(int id)
157{
158 struct quota_format_type *actqf;
159
160 spin_lock(&dq_list_lock);
161 for (actqf = quota_formats; actqf && actqf->qf_fmt_id != id; actqf = actqf->qf_next);
162 if (!actqf || !try_module_get(actqf->qf_owner)) {
163 int qm;
164
165 spin_unlock(&dq_list_lock);
166
167 for (qm = 0; module_names[qm].qm_fmt_id && module_names[qm].qm_fmt_id != id; qm++);
168 if (!module_names[qm].qm_fmt_id || request_module(module_names[qm].qm_mod_name))
169 return NULL;
170
171 spin_lock(&dq_list_lock);
172 for (actqf = quota_formats; actqf && actqf->qf_fmt_id != id; actqf = actqf->qf_next);
173 if (actqf && !try_module_get(actqf->qf_owner))
174 actqf = NULL;
175 }
176 spin_unlock(&dq_list_lock);
177 return actqf;
178}
179
180static void put_quota_format(struct quota_format_type *fmt)
181{
182 module_put(fmt->qf_owner);
183}
184
185/*
186 * Dquot List Management:
187 * The quota code uses three lists for dquot management: the inuse_list,
188 * free_dquots, and dquot_hash[] array. A single dquot structure may be
189 * on all three lists, depending on its current state.
190 *
191 * All dquots are placed to the end of inuse_list when first created, and this
192 * list is used for invalidate operation, which must look at every dquot.
193 *
194 * Unused dquots (dq_count == 0) are added to the free_dquots list when freed,
195 * and this list is searched whenever we need an available dquot. Dquots are
196 * removed from the list as soon as they are used again, and
197 * dqstats.free_dquots gives the number of dquots on the list. When
198 * dquot is invalidated it's completely released from memory.
199 *
200 * Dquots with a specific identity (device, type and id) are placed on
201 * one of the dquot_hash[] hash chains. The provides an efficient search
202 * mechanism to locate a specific dquot.
203 */
204
205static LIST_HEAD(inuse_list);
206static LIST_HEAD(free_dquots);
207static unsigned int dq_hash_bits, dq_hash_mask;
208static struct hlist_head *dquot_hash;
209
210struct dqstats dqstats;
211
212static void dqput(struct dquot *dquot);
213
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
288#define mark_dquot_dirty(dquot) ((dquot)->dq_sb->dq_op->mark_dirty(dquot))
289
290int dquot_mark_dquot_dirty(struct dquot *dquot)
291{
292 spin_lock(&dq_list_lock);
293 if (!test_and_set_bit(DQ_MOD_B, &dquot->dq_flags))
294 list_add(&dquot->dq_dirty, &sb_dqopt(dquot->dq_sb)->
295 info[dquot->dq_type].dqi_dirty_list);
296 spin_unlock(&dq_list_lock);
297 return 0;
298}
299
300/* This function needs dq_list_lock */
301static inline int clear_dquot_dirty(struct dquot *dquot)
302{
303 if (!test_and_clear_bit(DQ_MOD_B, &dquot->dq_flags))
304 return 0;
305 list_del_init(&dquot->dq_dirty);
306 return 1;
307}
308
309void mark_info_dirty(struct super_block *sb, int type)
310{
311 set_bit(DQF_INFO_DIRTY_B, &sb_dqopt(sb)->info[type].dqi_flags);
312}
313EXPORT_SYMBOL(mark_info_dirty);
314
315/*
316 * Read dquot from disk and alloc space for it
317 */
318
319int dquot_acquire(struct dquot *dquot)
320{
321 int ret = 0, ret2 = 0;
322 struct quota_info *dqopt = sb_dqopt(dquot->dq_sb);
323
d3be915f
IM
324 mutex_lock(&dquot->dq_lock);
325 mutex_lock(&dqopt->dqio_mutex);
1da177e4
LT
326 if (!test_bit(DQ_READ_B, &dquot->dq_flags))
327 ret = dqopt->ops[dquot->dq_type]->read_dqblk(dquot);
328 if (ret < 0)
329 goto out_iolock;
330 set_bit(DQ_READ_B, &dquot->dq_flags);
331 /* Instantiate dquot if needed */
332 if (!test_bit(DQ_ACTIVE_B, &dquot->dq_flags) && !dquot->dq_off) {
333 ret = dqopt->ops[dquot->dq_type]->commit_dqblk(dquot);
334 /* Write the info if needed */
335 if (info_dirty(&dqopt->info[dquot->dq_type]))
336 ret2 = dqopt->ops[dquot->dq_type]->write_file_info(dquot->dq_sb, dquot->dq_type);
337 if (ret < 0)
338 goto out_iolock;
339 if (ret2 < 0) {
340 ret = ret2;
341 goto out_iolock;
342 }
343 }
344 set_bit(DQ_ACTIVE_B, &dquot->dq_flags);
345out_iolock:
d3be915f
IM
346 mutex_unlock(&dqopt->dqio_mutex);
347 mutex_unlock(&dquot->dq_lock);
1da177e4
LT
348 return ret;
349}
350
351/*
352 * Write dquot to disk
353 */
354int dquot_commit(struct dquot *dquot)
355{
356 int ret = 0, ret2 = 0;
357 struct quota_info *dqopt = sb_dqopt(dquot->dq_sb);
358
d3be915f 359 mutex_lock(&dqopt->dqio_mutex);
1da177e4
LT
360 spin_lock(&dq_list_lock);
361 if (!clear_dquot_dirty(dquot)) {
362 spin_unlock(&dq_list_lock);
363 goto out_sem;
364 }
365 spin_unlock(&dq_list_lock);
366 /* Inactive dquot can be only if there was error during read/init
367 * => we have better not writing it */
368 if (test_bit(DQ_ACTIVE_B, &dquot->dq_flags)) {
369 ret = dqopt->ops[dquot->dq_type]->commit_dqblk(dquot);
370 if (info_dirty(&dqopt->info[dquot->dq_type]))
371 ret2 = dqopt->ops[dquot->dq_type]->write_file_info(dquot->dq_sb, dquot->dq_type);
372 if (ret >= 0)
373 ret = ret2;
374 }
375out_sem:
d3be915f 376 mutex_unlock(&dqopt->dqio_mutex);
1da177e4
LT
377 return ret;
378}
379
380/*
381 * Release dquot
382 */
383int dquot_release(struct dquot *dquot)
384{
385 int ret = 0, ret2 = 0;
386 struct quota_info *dqopt = sb_dqopt(dquot->dq_sb);
387
d3be915f 388 mutex_lock(&dquot->dq_lock);
1da177e4
LT
389 /* Check whether we are not racing with some other dqget() */
390 if (atomic_read(&dquot->dq_count) > 1)
391 goto out_dqlock;
d3be915f 392 mutex_lock(&dqopt->dqio_mutex);
1da177e4
LT
393 if (dqopt->ops[dquot->dq_type]->release_dqblk) {
394 ret = dqopt->ops[dquot->dq_type]->release_dqblk(dquot);
395 /* Write the info */
396 if (info_dirty(&dqopt->info[dquot->dq_type]))
397 ret2 = dqopt->ops[dquot->dq_type]->write_file_info(dquot->dq_sb, dquot->dq_type);
398 if (ret >= 0)
399 ret = ret2;
400 }
401 clear_bit(DQ_ACTIVE_B, &dquot->dq_flags);
d3be915f 402 mutex_unlock(&dqopt->dqio_mutex);
1da177e4 403out_dqlock:
d3be915f 404 mutex_unlock(&dquot->dq_lock);
1da177e4
LT
405 return ret;
406}
407
408/* Invalidate all dquots on the list. Note that this function is called after
409 * quota is disabled and pointers from inodes removed so there cannot be new
6362e4d4
JK
410 * quota users. There can still be some users of quotas due to inodes being
411 * just deleted or pruned by prune_icache() (those are not attached to any
412 * list). We have to wait for such users.
413 */
1da177e4
LT
414static void invalidate_dquots(struct super_block *sb, int type)
415{
c33ed271 416 struct dquot *dquot, *tmp;
1da177e4 417
6362e4d4 418restart:
1da177e4 419 spin_lock(&dq_list_lock);
c33ed271 420 list_for_each_entry_safe(dquot, tmp, &inuse_list, dq_inuse) {
1da177e4
LT
421 if (dquot->dq_sb != sb)
422 continue;
423 if (dquot->dq_type != type)
424 continue;
6362e4d4
JK
425 /* Wait for dquot users */
426 if (atomic_read(&dquot->dq_count)) {
427 DEFINE_WAIT(wait);
428
429 atomic_inc(&dquot->dq_count);
430 prepare_to_wait(&dquot->dq_wait_unused, &wait,
431 TASK_UNINTERRUPTIBLE);
432 spin_unlock(&dq_list_lock);
433 /* Once dqput() wakes us up, we know it's time to free
434 * the dquot.
435 * IMPORTANT: we rely on the fact that there is always
436 * at most one process waiting for dquot to free.
437 * Otherwise dq_count would be > 1 and we would never
438 * wake up.
439 */
440 if (atomic_read(&dquot->dq_count) > 1)
441 schedule();
442 finish_wait(&dquot->dq_wait_unused, &wait);
443 dqput(dquot);
444 /* At this moment dquot() need not exist (it could be
445 * reclaimed by prune_dqcache(). Hence we must
446 * restart. */
447 goto restart;
448 }
449 /*
450 * Quota now has no users and it has been written on last
451 * dqput()
452 */
1da177e4
LT
453 remove_dquot_hash(dquot);
454 remove_free_dquot(dquot);
455 remove_inuse(dquot);
456 kmem_cache_free(dquot_cachep, dquot);
457 }
458 spin_unlock(&dq_list_lock);
459}
460
461int vfs_quota_sync(struct super_block *sb, int type)
462{
463 struct list_head *dirty;
464 struct dquot *dquot;
465 struct quota_info *dqopt = sb_dqopt(sb);
466 int cnt;
467
d3be915f 468 mutex_lock(&dqopt->dqonoff_mutex);
1da177e4
LT
469 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
470 if (type != -1 && cnt != type)
471 continue;
472 if (!sb_has_quota_enabled(sb, cnt))
473 continue;
474 spin_lock(&dq_list_lock);
475 dirty = &dqopt->info[cnt].dqi_dirty_list;
476 while (!list_empty(dirty)) {
477 dquot = list_entry(dirty->next, struct dquot, dq_dirty);
478 /* Dirty and inactive can be only bad dquot... */
479 if (!test_bit(DQ_ACTIVE_B, &dquot->dq_flags)) {
480 clear_dquot_dirty(dquot);
481 continue;
482 }
483 /* Now we have active dquot from which someone is
484 * holding reference so we can safely just increase
485 * use count */
486 atomic_inc(&dquot->dq_count);
487 dqstats.lookups++;
488 spin_unlock(&dq_list_lock);
489 sb->dq_op->write_dquot(dquot);
490 dqput(dquot);
491 spin_lock(&dq_list_lock);
492 }
493 spin_unlock(&dq_list_lock);
494 }
495
496 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
497 if ((cnt == type || type == -1) && sb_has_quota_enabled(sb, cnt)
498 && info_dirty(&dqopt->info[cnt]))
499 sb->dq_op->write_info(sb, cnt);
500 spin_lock(&dq_list_lock);
501 dqstats.syncs++;
502 spin_unlock(&dq_list_lock);
d3be915f 503 mutex_unlock(&dqopt->dqonoff_mutex);
1da177e4
LT
504
505 return 0;
506}
507
508/* Free unused dquots from cache */
509static void prune_dqcache(int count)
510{
511 struct list_head *head;
512 struct dquot *dquot;
513
514 head = free_dquots.prev;
515 while (head != &free_dquots && count) {
516 dquot = list_entry(head, struct dquot, dq_free);
517 remove_dquot_hash(dquot);
518 remove_free_dquot(dquot);
519 remove_inuse(dquot);
520 kmem_cache_free(dquot_cachep, dquot);
521 count--;
522 head = free_dquots.prev;
523 }
524}
525
526/*
527 * This is called from kswapd when we think we need some
528 * more memory
529 */
530
27496a8c 531static int shrink_dqcache_memory(int nr, gfp_t gfp_mask)
1da177e4
LT
532{
533 if (nr) {
534 spin_lock(&dq_list_lock);
535 prune_dqcache(nr);
536 spin_unlock(&dq_list_lock);
537 }
538 return (dqstats.free_dquots / 100) * sysctl_vfs_cache_pressure;
539}
540
541/*
542 * Put reference to dquot
543 * NOTE: If you change this function please check whether dqput_blocks() works right...
d3be915f 544 * MUST be called with either dqptr_sem or dqonoff_mutex held
1da177e4
LT
545 */
546static void dqput(struct dquot *dquot)
547{
548 if (!dquot)
549 return;
550#ifdef __DQUOT_PARANOIA
551 if (!atomic_read(&dquot->dq_count)) {
552 printk("VFS: dqput: trying to free free dquot\n");
553 printk("VFS: device %s, dquot of %s %d\n",
554 dquot->dq_sb->s_id,
555 quotatypes[dquot->dq_type],
556 dquot->dq_id);
557 BUG();
558 }
559#endif
560
561 spin_lock(&dq_list_lock);
562 dqstats.drops++;
563 spin_unlock(&dq_list_lock);
564we_slept:
565 spin_lock(&dq_list_lock);
566 if (atomic_read(&dquot->dq_count) > 1) {
567 /* We have more than one user... nothing to do */
568 atomic_dec(&dquot->dq_count);
6362e4d4
JK
569 /* Releasing dquot during quotaoff phase? */
570 if (!sb_has_quota_enabled(dquot->dq_sb, dquot->dq_type) &&
571 atomic_read(&dquot->dq_count) == 1)
572 wake_up(&dquot->dq_wait_unused);
1da177e4
LT
573 spin_unlock(&dq_list_lock);
574 return;
575 }
576 /* Need to release dquot? */
577 if (test_bit(DQ_ACTIVE_B, &dquot->dq_flags) && dquot_dirty(dquot)) {
578 spin_unlock(&dq_list_lock);
579 /* Commit dquot before releasing */
580 dquot->dq_sb->dq_op->write_dquot(dquot);
581 goto we_slept;
582 }
583 /* Clear flag in case dquot was inactive (something bad happened) */
584 clear_dquot_dirty(dquot);
585 if (test_bit(DQ_ACTIVE_B, &dquot->dq_flags)) {
586 spin_unlock(&dq_list_lock);
587 dquot->dq_sb->dq_op->release_dquot(dquot);
588 goto we_slept;
589 }
590 atomic_dec(&dquot->dq_count);
591#ifdef __DQUOT_PARANOIA
592 /* sanity check */
8abf6a47 593 BUG_ON(!list_empty(&dquot->dq_free));
1da177e4
LT
594#endif
595 put_dquot_last(dquot);
596 spin_unlock(&dq_list_lock);
597}
598
599static struct dquot *get_empty_dquot(struct super_block *sb, int type)
600{
601 struct dquot *dquot;
602
e6b4f8da 603 dquot = kmem_cache_alloc(dquot_cachep, GFP_NOFS);
1da177e4
LT
604 if(!dquot)
605 return NODQUOT;
606
607 memset((caddr_t)dquot, 0, sizeof(struct dquot));
d3be915f 608 mutex_init(&dquot->dq_lock);
1da177e4
LT
609 INIT_LIST_HEAD(&dquot->dq_free);
610 INIT_LIST_HEAD(&dquot->dq_inuse);
611 INIT_HLIST_NODE(&dquot->dq_hash);
612 INIT_LIST_HEAD(&dquot->dq_dirty);
6362e4d4 613 init_waitqueue_head(&dquot->dq_wait_unused);
1da177e4
LT
614 dquot->dq_sb = sb;
615 dquot->dq_type = type;
616 atomic_set(&dquot->dq_count, 1);
617
618 return dquot;
619}
620
621/*
622 * Get reference to dquot
d3be915f 623 * MUST be called with either dqptr_sem or dqonoff_mutex held
1da177e4
LT
624 */
625static struct dquot *dqget(struct super_block *sb, unsigned int id, int type)
626{
627 unsigned int hashent = hashfn(sb, id, type);
628 struct dquot *dquot, *empty = NODQUOT;
629
630 if (!sb_has_quota_enabled(sb, type))
631 return NODQUOT;
632we_slept:
633 spin_lock(&dq_list_lock);
634 if ((dquot = find_dquot(hashent, sb, id, type)) == NODQUOT) {
635 if (empty == NODQUOT) {
636 spin_unlock(&dq_list_lock);
637 if ((empty = get_empty_dquot(sb, type)) == NODQUOT)
638 schedule(); /* Try to wait for a moment... */
639 goto we_slept;
640 }
641 dquot = empty;
642 dquot->dq_id = id;
643 /* all dquots go on the inuse_list */
644 put_inuse(dquot);
645 /* hash it first so it can be found */
646 insert_dquot_hash(dquot);
647 dqstats.lookups++;
648 spin_unlock(&dq_list_lock);
649 } else {
650 if (!atomic_read(&dquot->dq_count))
651 remove_free_dquot(dquot);
652 atomic_inc(&dquot->dq_count);
653 dqstats.cache_hits++;
654 dqstats.lookups++;
655 spin_unlock(&dq_list_lock);
656 if (empty)
657 kmem_cache_free(dquot_cachep, empty);
658 }
659 /* Wait for dq_lock - after this we know that either dquot_release() is already
660 * finished or it will be canceled due to dq_count > 1 test */
661 wait_on_dquot(dquot);
662 /* Read the dquot and instantiate it (everything done only if needed) */
663 if (!test_bit(DQ_ACTIVE_B, &dquot->dq_flags) && sb->dq_op->acquire_dquot(dquot) < 0) {
664 dqput(dquot);
665 return NODQUOT;
666 }
667#ifdef __DQUOT_PARANOIA
8abf6a47 668 BUG_ON(!dquot->dq_sb); /* Has somebody invalidated entry under us? */
1da177e4
LT
669#endif
670
671 return dquot;
672}
673
674static int dqinit_needed(struct inode *inode, int type)
675{
676 int cnt;
677
678 if (IS_NOQUOTA(inode))
679 return 0;
680 if (type != -1)
681 return inode->i_dquot[type] == NODQUOT;
682 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
683 if (inode->i_dquot[cnt] == NODQUOT)
684 return 1;
685 return 0;
686}
687
d3be915f 688/* This routine is guarded by dqonoff_mutex mutex */
1da177e4
LT
689static void add_dquot_ref(struct super_block *sb, int type)
690{
691 struct list_head *p;
692
693restart:
694 file_list_lock();
695 list_for_each(p, &sb->s_files) {
2f512016 696 struct file *filp = list_entry(p, struct file, f_u.fu_list);
0f7fc9e4 697 struct inode *inode = filp->f_path.dentry->d_inode;
1da177e4 698 if (filp->f_mode & FMODE_WRITE && dqinit_needed(inode, type)) {
0f7fc9e4 699 struct dentry *dentry = dget(filp->f_path.dentry);
1da177e4
LT
700 file_list_unlock();
701 sb->dq_op->initialize(inode, type);
702 dput(dentry);
703 /* As we may have blocked we had better restart... */
704 goto restart;
705 }
706 }
707 file_list_unlock();
708}
709
710/* Return 0 if dqput() won't block (note that 1 doesn't necessarily mean blocking) */
711static inline int dqput_blocks(struct dquot *dquot)
712{
713 if (atomic_read(&dquot->dq_count) <= 1)
714 return 1;
715 return 0;
716}
717
718/* Remove references to dquots from inode - add dquot to list for freeing if needed */
719/* We can't race with anybody because we hold dqptr_sem for writing... */
720int remove_inode_dquot_ref(struct inode *inode, int type, struct list_head *tofree_head)
721{
722 struct dquot *dquot = inode->i_dquot[type];
723
724 inode->i_dquot[type] = NODQUOT;
725 if (dquot != NODQUOT) {
726 if (dqput_blocks(dquot)) {
727#ifdef __DQUOT_PARANOIA
728 if (atomic_read(&dquot->dq_count) != 1)
729 printk(KERN_WARNING "VFS: Adding dquot with dq_count %d to dispose list.\n", atomic_read(&dquot->dq_count));
730#endif
731 spin_lock(&dq_list_lock);
732 list_add(&dquot->dq_free, tofree_head); /* As dquot must have currently users it can't be on the free list... */
733 spin_unlock(&dq_list_lock);
734 return 1;
735 }
736 else
737 dqput(dquot); /* We have guaranteed we won't block */
738 }
739 return 0;
740}
741
742/* Free list of dquots - called from inode.c */
743/* dquots are removed from inodes, no new references can be got so we are the only ones holding reference */
744static void put_dquot_list(struct list_head *tofree_head)
745{
746 struct list_head *act_head;
747 struct dquot *dquot;
748
749 act_head = tofree_head->next;
750 /* So now we have dquots on the list... Just free them */
751 while (act_head != tofree_head) {
752 dquot = list_entry(act_head, struct dquot, dq_free);
753 act_head = act_head->next;
754 list_del_init(&dquot->dq_free); /* Remove dquot from the list so we won't have problems... */
755 dqput(dquot);
756 }
757}
758
759/* Gather all references from inodes and drop them */
760static void drop_dquot_ref(struct super_block *sb, int type)
761{
762 LIST_HEAD(tofree_head);
763
1da177e4
LT
764 down_write(&sb_dqopt(sb)->dqptr_sem);
765 remove_dquot_ref(sb, type, &tofree_head);
766 up_write(&sb_dqopt(sb)->dqptr_sem);
1da177e4
LT
767 put_dquot_list(&tofree_head);
768}
769
770static inline void dquot_incr_inodes(struct dquot *dquot, unsigned long number)
771{
772 dquot->dq_dqb.dqb_curinodes += number;
773}
774
775static inline void dquot_incr_space(struct dquot *dquot, qsize_t number)
776{
777 dquot->dq_dqb.dqb_curspace += number;
778}
779
780static inline void dquot_decr_inodes(struct dquot *dquot, unsigned long number)
781{
782 if (dquot->dq_dqb.dqb_curinodes > number)
783 dquot->dq_dqb.dqb_curinodes -= number;
784 else
785 dquot->dq_dqb.dqb_curinodes = 0;
786 if (dquot->dq_dqb.dqb_curinodes <= dquot->dq_dqb.dqb_isoftlimit)
787 dquot->dq_dqb.dqb_itime = (time_t) 0;
788 clear_bit(DQ_INODES_B, &dquot->dq_flags);
789}
790
791static inline void dquot_decr_space(struct dquot *dquot, qsize_t number)
792{
793 if (dquot->dq_dqb.dqb_curspace > number)
794 dquot->dq_dqb.dqb_curspace -= number;
795 else
796 dquot->dq_dqb.dqb_curspace = 0;
797 if (toqb(dquot->dq_dqb.dqb_curspace) <= dquot->dq_dqb.dqb_bsoftlimit)
798 dquot->dq_dqb.dqb_btime = (time_t) 0;
799 clear_bit(DQ_BLKS_B, &dquot->dq_flags);
800}
801
802static int flag_print_warnings = 1;
803
804static inline int need_print_warning(struct dquot *dquot)
805{
806 if (!flag_print_warnings)
807 return 0;
808
809 switch (dquot->dq_type) {
810 case USRQUOTA:
811 return current->fsuid == dquot->dq_id;
812 case GRPQUOTA:
813 return in_group_p(dquot->dq_id);
814 }
815 return 0;
816}
817
818/* Values of warnings */
819#define NOWARN 0
820#define IHARDWARN 1
821#define ISOFTLONGWARN 2
822#define ISOFTWARN 3
823#define BHARDWARN 4
824#define BSOFTLONGWARN 5
825#define BSOFTWARN 6
826
827/* Print warning to user which exceeded quota */
828static void print_warning(struct dquot *dquot, const char warntype)
829{
830 char *msg = NULL;
24ec839c 831 struct tty_struct *tty;
1da177e4
LT
832 int flag = (warntype == BHARDWARN || warntype == BSOFTLONGWARN) ? DQ_BLKS_B :
833 ((warntype == IHARDWARN || warntype == ISOFTLONGWARN) ? DQ_INODES_B : 0);
834
835 if (!need_print_warning(dquot) || (flag && test_and_set_bit(flag, &dquot->dq_flags)))
836 return;
837
b525a7e4 838 mutex_lock(&tty_mutex);
24ec839c
PZ
839 tty = get_current_tty();
840 if (!tty)
b525a7e4 841 goto out_lock;
24ec839c 842 tty_write_message(tty, dquot->dq_sb->s_id);
1da177e4 843 if (warntype == ISOFTWARN || warntype == BSOFTWARN)
24ec839c 844 tty_write_message(tty, ": warning, ");
1da177e4 845 else
24ec839c
PZ
846 tty_write_message(tty, ": write failed, ");
847 tty_write_message(tty, quotatypes[dquot->dq_type]);
1da177e4
LT
848 switch (warntype) {
849 case IHARDWARN:
850 msg = " file limit reached.\r\n";
851 break;
852 case ISOFTLONGWARN:
853 msg = " file quota exceeded too long.\r\n";
854 break;
855 case ISOFTWARN:
856 msg = " file quota exceeded.\r\n";
857 break;
858 case BHARDWARN:
859 msg = " block limit reached.\r\n";
860 break;
861 case BSOFTLONGWARN:
862 msg = " block quota exceeded too long.\r\n";
863 break;
864 case BSOFTWARN:
865 msg = " block quota exceeded.\r\n";
866 break;
867 }
24ec839c 868 tty_write_message(tty, msg);
b525a7e4
JK
869out_lock:
870 mutex_unlock(&tty_mutex);
1da177e4
LT
871}
872
873static inline void flush_warnings(struct dquot **dquots, char *warntype)
874{
875 int i;
876
877 for (i = 0; i < MAXQUOTAS; i++)
878 if (dquots[i] != NODQUOT && warntype[i] != NOWARN)
879 print_warning(dquots[i], warntype[i]);
880}
881
882static inline char ignore_hardlimit(struct dquot *dquot)
883{
884 struct mem_dqinfo *info = &sb_dqopt(dquot->dq_sb)->info[dquot->dq_type];
885
886 return capable(CAP_SYS_RESOURCE) &&
887 (info->dqi_format->qf_fmt_id != QFMT_VFS_OLD || !(info->dqi_flags & V1_DQF_RSQUASH));
888}
889
890/* needs dq_data_lock */
891static int check_idq(struct dquot *dquot, ulong inodes, char *warntype)
892{
893 *warntype = NOWARN;
894 if (inodes <= 0 || test_bit(DQ_FAKE_B, &dquot->dq_flags))
895 return QUOTA_OK;
896
897 if (dquot->dq_dqb.dqb_ihardlimit &&
898 (dquot->dq_dqb.dqb_curinodes + inodes) > dquot->dq_dqb.dqb_ihardlimit &&
899 !ignore_hardlimit(dquot)) {
900 *warntype = IHARDWARN;
901 return NO_QUOTA;
902 }
903
904 if (dquot->dq_dqb.dqb_isoftlimit &&
905 (dquot->dq_dqb.dqb_curinodes + inodes) > dquot->dq_dqb.dqb_isoftlimit &&
906 dquot->dq_dqb.dqb_itime && get_seconds() >= dquot->dq_dqb.dqb_itime &&
907 !ignore_hardlimit(dquot)) {
908 *warntype = ISOFTLONGWARN;
909 return NO_QUOTA;
910 }
911
912 if (dquot->dq_dqb.dqb_isoftlimit &&
913 (dquot->dq_dqb.dqb_curinodes + inodes) > dquot->dq_dqb.dqb_isoftlimit &&
914 dquot->dq_dqb.dqb_itime == 0) {
915 *warntype = ISOFTWARN;
916 dquot->dq_dqb.dqb_itime = get_seconds() + sb_dqopt(dquot->dq_sb)->info[dquot->dq_type].dqi_igrace;
917 }
918
919 return QUOTA_OK;
920}
921
922/* needs dq_data_lock */
923static int check_bdq(struct dquot *dquot, qsize_t space, int prealloc, char *warntype)
924{
925 *warntype = 0;
926 if (space <= 0 || test_bit(DQ_FAKE_B, &dquot->dq_flags))
927 return QUOTA_OK;
928
929 if (dquot->dq_dqb.dqb_bhardlimit &&
930 toqb(dquot->dq_dqb.dqb_curspace + space) > dquot->dq_dqb.dqb_bhardlimit &&
931 !ignore_hardlimit(dquot)) {
932 if (!prealloc)
933 *warntype = BHARDWARN;
934 return NO_QUOTA;
935 }
936
937 if (dquot->dq_dqb.dqb_bsoftlimit &&
938 toqb(dquot->dq_dqb.dqb_curspace + space) > dquot->dq_dqb.dqb_bsoftlimit &&
939 dquot->dq_dqb.dqb_btime && get_seconds() >= dquot->dq_dqb.dqb_btime &&
940 !ignore_hardlimit(dquot)) {
941 if (!prealloc)
942 *warntype = BSOFTLONGWARN;
943 return NO_QUOTA;
944 }
945
946 if (dquot->dq_dqb.dqb_bsoftlimit &&
947 toqb(dquot->dq_dqb.dqb_curspace + space) > dquot->dq_dqb.dqb_bsoftlimit &&
948 dquot->dq_dqb.dqb_btime == 0) {
949 if (!prealloc) {
950 *warntype = BSOFTWARN;
951 dquot->dq_dqb.dqb_btime = get_seconds() + sb_dqopt(dquot->dq_sb)->info[dquot->dq_type].dqi_bgrace;
952 }
953 else
954 /*
955 * We don't allow preallocation to exceed softlimit so exceeding will
956 * be always printed
957 */
958 return NO_QUOTA;
959 }
960
961 return QUOTA_OK;
962}
963
964/*
965 * Initialize quota pointers in inode
966 * Transaction must be started at entry
967 */
968int dquot_initialize(struct inode *inode, int type)
969{
970 unsigned int id = 0;
971 int cnt, ret = 0;
972
d3be915f
IM
973 /* First test before acquiring mutex - solves deadlocks when we
974 * re-enter the quota code and are already holding the mutex */
1da177e4
LT
975 if (IS_NOQUOTA(inode))
976 return 0;
977 down_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
978 /* Having dqptr_sem we know NOQUOTA flags can't be altered... */
979 if (IS_NOQUOTA(inode))
980 goto out_err;
981 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
982 if (type != -1 && cnt != type)
983 continue;
984 if (inode->i_dquot[cnt] == NODQUOT) {
985 switch (cnt) {
986 case USRQUOTA:
987 id = inode->i_uid;
988 break;
989 case GRPQUOTA:
990 id = inode->i_gid;
991 break;
992 }
993 inode->i_dquot[cnt] = dqget(inode->i_sb, id, cnt);
994 }
995 }
996out_err:
997 up_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
998 return ret;
999}
1000
1001/*
1002 * Release all quotas referenced by inode
1003 * Transaction must be started at an entry
1004 */
1005int dquot_drop(struct inode *inode)
1006{
1007 int cnt;
1008
1009 down_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
1010 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1011 if (inode->i_dquot[cnt] != NODQUOT) {
1012 dqput(inode->i_dquot[cnt]);
1013 inode->i_dquot[cnt] = NODQUOT;
1014 }
1015 }
1016 up_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
1017 return 0;
1018}
1019
1020/*
1021 * Following four functions update i_blocks+i_bytes fields and
1022 * quota information (together with appropriate checks)
1023 * NOTE: We absolutely rely on the fact that caller dirties
1024 * the inode (usually macros in quotaops.h care about this) and
1025 * holds a handle for the current transaction so that dquot write and
1026 * inode write go into the same transaction.
1027 */
1028
1029/*
1030 * This operation can block, but only after everything is updated
1031 */
1032int dquot_alloc_space(struct inode *inode, qsize_t number, int warn)
1033{
1034 int cnt, ret = NO_QUOTA;
1035 char warntype[MAXQUOTAS];
1036
d3be915f
IM
1037 /* First test before acquiring mutex - solves deadlocks when we
1038 * re-enter the quota code and are already holding the mutex */
1da177e4
LT
1039 if (IS_NOQUOTA(inode)) {
1040out_add:
1041 inode_add_bytes(inode, number);
1042 return QUOTA_OK;
1043 }
1044 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1045 warntype[cnt] = NOWARN;
1046
1047 down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1048 if (IS_NOQUOTA(inode)) { /* Now we can do reliable test... */
1049 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1050 goto out_add;
1051 }
1052 spin_lock(&dq_data_lock);
1053 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1054 if (inode->i_dquot[cnt] == NODQUOT)
1055 continue;
1056 if (check_bdq(inode->i_dquot[cnt], number, warn, warntype+cnt) == NO_QUOTA)
1057 goto warn_put_all;
1058 }
1059 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1060 if (inode->i_dquot[cnt] == NODQUOT)
1061 continue;
1062 dquot_incr_space(inode->i_dquot[cnt], number);
1063 }
1064 inode_add_bytes(inode, number);
1065 ret = QUOTA_OK;
1066warn_put_all:
1067 spin_unlock(&dq_data_lock);
1068 if (ret == QUOTA_OK)
1069 /* Dirtify all the dquots - this can block when journalling */
1070 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1071 if (inode->i_dquot[cnt])
1072 mark_dquot_dirty(inode->i_dquot[cnt]);
1073 flush_warnings(inode->i_dquot, warntype);
1074 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1075 return ret;
1076}
1077
1078/*
1079 * This operation can block, but only after everything is updated
1080 */
1081int dquot_alloc_inode(const struct inode *inode, unsigned long number)
1082{
1083 int cnt, ret = NO_QUOTA;
1084 char warntype[MAXQUOTAS];
1085
d3be915f
IM
1086 /* First test before acquiring mutex - solves deadlocks when we
1087 * re-enter the quota code and are already holding the mutex */
1da177e4
LT
1088 if (IS_NOQUOTA(inode))
1089 return QUOTA_OK;
1090 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1091 warntype[cnt] = NOWARN;
1092 down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1093 if (IS_NOQUOTA(inode)) {
1094 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1095 return QUOTA_OK;
1096 }
1097 spin_lock(&dq_data_lock);
1098 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1099 if (inode->i_dquot[cnt] == NODQUOT)
1100 continue;
1101 if (check_idq(inode->i_dquot[cnt], number, warntype+cnt) == NO_QUOTA)
1102 goto warn_put_all;
1103 }
1104
1105 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1106 if (inode->i_dquot[cnt] == NODQUOT)
1107 continue;
1108 dquot_incr_inodes(inode->i_dquot[cnt], number);
1109 }
1110 ret = QUOTA_OK;
1111warn_put_all:
1112 spin_unlock(&dq_data_lock);
1113 if (ret == QUOTA_OK)
1114 /* Dirtify all the dquots - this can block when journalling */
1115 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1116 if (inode->i_dquot[cnt])
1117 mark_dquot_dirty(inode->i_dquot[cnt]);
1118 flush_warnings((struct dquot **)inode->i_dquot, warntype);
1119 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1120 return ret;
1121}
1122
1123/*
1124 * This operation can block, but only after everything is updated
1125 */
1126int dquot_free_space(struct inode *inode, qsize_t number)
1127{
1128 unsigned int cnt;
1129
d3be915f
IM
1130 /* First test before acquiring mutex - solves deadlocks when we
1131 * re-enter the quota code and are already holding the mutex */
1da177e4
LT
1132 if (IS_NOQUOTA(inode)) {
1133out_sub:
1134 inode_sub_bytes(inode, number);
1135 return QUOTA_OK;
1136 }
1137 down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1138 /* Now recheck reliably when holding dqptr_sem */
1139 if (IS_NOQUOTA(inode)) {
1140 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1141 goto out_sub;
1142 }
1143 spin_lock(&dq_data_lock);
1144 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1145 if (inode->i_dquot[cnt] == NODQUOT)
1146 continue;
1147 dquot_decr_space(inode->i_dquot[cnt], number);
1148 }
1149 inode_sub_bytes(inode, number);
1150 spin_unlock(&dq_data_lock);
1151 /* Dirtify all the dquots - this can block when journalling */
1152 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1153 if (inode->i_dquot[cnt])
1154 mark_dquot_dirty(inode->i_dquot[cnt]);
1155 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1156 return QUOTA_OK;
1157}
1158
1159/*
1160 * This operation can block, but only after everything is updated
1161 */
1162int dquot_free_inode(const struct inode *inode, unsigned long number)
1163{
1164 unsigned int cnt;
1165
d3be915f
IM
1166 /* First test before acquiring mutex - solves deadlocks when we
1167 * re-enter the quota code and are already holding the mutex */
1da177e4
LT
1168 if (IS_NOQUOTA(inode))
1169 return QUOTA_OK;
1170 down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1171 /* Now recheck reliably when holding dqptr_sem */
1172 if (IS_NOQUOTA(inode)) {
1173 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1174 return QUOTA_OK;
1175 }
1176 spin_lock(&dq_data_lock);
1177 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1178 if (inode->i_dquot[cnt] == NODQUOT)
1179 continue;
1180 dquot_decr_inodes(inode->i_dquot[cnt], number);
1181 }
1182 spin_unlock(&dq_data_lock);
1183 /* Dirtify all the dquots - this can block when journalling */
1184 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1185 if (inode->i_dquot[cnt])
1186 mark_dquot_dirty(inode->i_dquot[cnt]);
1187 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1188 return QUOTA_OK;
1189}
1190
1191/*
1192 * Transfer the number of inode and blocks from one diskquota to an other.
1193 *
1194 * This operation can block, but only after everything is updated
1195 * A transaction must be started when entering this function.
1196 */
1197int dquot_transfer(struct inode *inode, struct iattr *iattr)
1198{
1199 qsize_t space;
1200 struct dquot *transfer_from[MAXQUOTAS];
1201 struct dquot *transfer_to[MAXQUOTAS];
1202 int cnt, ret = NO_QUOTA, chuid = (iattr->ia_valid & ATTR_UID) && inode->i_uid != iattr->ia_uid,
1203 chgid = (iattr->ia_valid & ATTR_GID) && inode->i_gid != iattr->ia_gid;
1204 char warntype[MAXQUOTAS];
1205
d3be915f
IM
1206 /* First test before acquiring mutex - solves deadlocks when we
1207 * re-enter the quota code and are already holding the mutex */
1da177e4
LT
1208 if (IS_NOQUOTA(inode))
1209 return QUOTA_OK;
1210 /* Clear the arrays */
1211 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1212 transfer_to[cnt] = transfer_from[cnt] = NODQUOT;
1213 warntype[cnt] = NOWARN;
1214 }
1215 down_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
1216 /* Now recheck reliably when holding dqptr_sem */
1217 if (IS_NOQUOTA(inode)) { /* File without quota accounting? */
1218 up_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
1219 return QUOTA_OK;
1220 }
1221 /* First build the transfer_to list - here we can block on
1222 * reading/instantiating of dquots. We know that the transaction for
1223 * us was already started so we don't violate lock ranking here */
1224 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1225 switch (cnt) {
1226 case USRQUOTA:
1227 if (!chuid)
1228 continue;
1229 transfer_to[cnt] = dqget(inode->i_sb, iattr->ia_uid, cnt);
1230 break;
1231 case GRPQUOTA:
1232 if (!chgid)
1233 continue;
1234 transfer_to[cnt] = dqget(inode->i_sb, iattr->ia_gid, cnt);
1235 break;
1236 }
1237 }
1238 spin_lock(&dq_data_lock);
1239 space = inode_get_bytes(inode);
1240 /* Build the transfer_from list and check the limits */
1241 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1242 if (transfer_to[cnt] == NODQUOT)
1243 continue;
1244 transfer_from[cnt] = inode->i_dquot[cnt];
1245 if (check_idq(transfer_to[cnt], 1, warntype+cnt) == NO_QUOTA ||
1246 check_bdq(transfer_to[cnt], space, 0, warntype+cnt) == NO_QUOTA)
1247 goto warn_put_all;
1248 }
1249
1250 /*
1251 * Finally perform the needed transfer from transfer_from to transfer_to
1252 */
1253 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1254 /*
1255 * Skip changes for same uid or gid or for turned off quota-type.
1256 */
1257 if (transfer_to[cnt] == NODQUOT)
1258 continue;
1259
1260 /* Due to IO error we might not have transfer_from[] structure */
1261 if (transfer_from[cnt]) {
1262 dquot_decr_inodes(transfer_from[cnt], 1);
1263 dquot_decr_space(transfer_from[cnt], space);
1264 }
1265
1266 dquot_incr_inodes(transfer_to[cnt], 1);
1267 dquot_incr_space(transfer_to[cnt], space);
1268
1269 inode->i_dquot[cnt] = transfer_to[cnt];
1270 }
1271 ret = QUOTA_OK;
1272warn_put_all:
1273 spin_unlock(&dq_data_lock);
1274 /* Dirtify all the dquots - this can block when journalling */
1275 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1276 if (transfer_from[cnt])
1277 mark_dquot_dirty(transfer_from[cnt]);
1278 if (transfer_to[cnt])
1279 mark_dquot_dirty(transfer_to[cnt]);
1280 }
1281 flush_warnings(transfer_to, warntype);
1282
1283 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1284 if (ret == QUOTA_OK && transfer_from[cnt] != NODQUOT)
1285 dqput(transfer_from[cnt]);
1286 if (ret == NO_QUOTA && transfer_to[cnt] != NODQUOT)
1287 dqput(transfer_to[cnt]);
1288 }
1289 up_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
1290 return ret;
1291}
1292
1293/*
1294 * Write info of quota file to disk
1295 */
1296int dquot_commit_info(struct super_block *sb, int type)
1297{
1298 int ret;
1299 struct quota_info *dqopt = sb_dqopt(sb);
1300
d3be915f 1301 mutex_lock(&dqopt->dqio_mutex);
1da177e4 1302 ret = dqopt->ops[type]->write_file_info(sb, type);
d3be915f 1303 mutex_unlock(&dqopt->dqio_mutex);
1da177e4
LT
1304 return ret;
1305}
1306
1307/*
1308 * Definitions of diskquota operations.
1309 */
1310struct dquot_operations dquot_operations = {
1311 .initialize = dquot_initialize,
1312 .drop = dquot_drop,
1313 .alloc_space = dquot_alloc_space,
1314 .alloc_inode = dquot_alloc_inode,
1315 .free_space = dquot_free_space,
1316 .free_inode = dquot_free_inode,
1317 .transfer = dquot_transfer,
1318 .write_dquot = dquot_commit,
1319 .acquire_dquot = dquot_acquire,
1320 .release_dquot = dquot_release,
1321 .mark_dirty = dquot_mark_dquot_dirty,
1322 .write_info = dquot_commit_info
1323};
1324
1325static inline void set_enable_flags(struct quota_info *dqopt, int type)
1326{
1327 switch (type) {
1328 case USRQUOTA:
1329 dqopt->flags |= DQUOT_USR_ENABLED;
1330 break;
1331 case GRPQUOTA:
1332 dqopt->flags |= DQUOT_GRP_ENABLED;
1333 break;
1334 }
1335}
1336
1337static inline void reset_enable_flags(struct quota_info *dqopt, int type)
1338{
1339 switch (type) {
1340 case USRQUOTA:
1341 dqopt->flags &= ~DQUOT_USR_ENABLED;
1342 break;
1343 case GRPQUOTA:
1344 dqopt->flags &= ~DQUOT_GRP_ENABLED;
1345 break;
1346 }
1347}
1348
1349/*
1350 * Turn quota off on a device. type == -1 ==> quotaoff for all types (umount)
1351 */
1352int vfs_quota_off(struct super_block *sb, int type)
1353{
1354 int cnt;
1355 struct quota_info *dqopt = sb_dqopt(sb);
1356 struct inode *toputinode[MAXQUOTAS];
1da177e4
LT
1357
1358 /* We need to serialize quota_off() for device */
d3be915f 1359 mutex_lock(&dqopt->dqonoff_mutex);
1da177e4
LT
1360 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1361 toputinode[cnt] = NULL;
1da177e4
LT
1362 if (type != -1 && cnt != type)
1363 continue;
1364 if (!sb_has_quota_enabled(sb, cnt))
1365 continue;
1366 reset_enable_flags(dqopt, cnt);
1367
1368 /* Note: these are blocking operations */
1369 drop_dquot_ref(sb, cnt);
1370 invalidate_dquots(sb, cnt);
1371 /*
1372 * Now all dquots should be invalidated, all writes done so we should be only
1373 * users of the info. No locks needed.
1374 */
1375 if (info_dirty(&dqopt->info[cnt]))
1376 sb->dq_op->write_info(sb, cnt);
1377 if (dqopt->ops[cnt]->free_file_info)
1378 dqopt->ops[cnt]->free_file_info(sb, cnt);
1379 put_quota_format(dqopt->info[cnt].dqi_format);
1380
1381 toputinode[cnt] = dqopt->files[cnt];
1da177e4 1382 dqopt->files[cnt] = NULL;
1da177e4
LT
1383 dqopt->info[cnt].dqi_flags = 0;
1384 dqopt->info[cnt].dqi_igrace = 0;
1385 dqopt->info[cnt].dqi_bgrace = 0;
1386 dqopt->ops[cnt] = NULL;
1387 }
d3be915f 1388 mutex_unlock(&dqopt->dqonoff_mutex);
1da177e4 1389 /* Sync the superblock so that buffers with quota data are written to
7b7b1ace 1390 * disk (and so userspace sees correct data afterwards). */
1da177e4
LT
1391 if (sb->s_op->sync_fs)
1392 sb->s_op->sync_fs(sb, 1);
1393 sync_blockdev(sb->s_bdev);
1394 /* Now the quota files are just ordinary files and we can set the
1395 * inode flags back. Moreover we discard the pagecache so that
1396 * userspace sees the writes we did bypassing the pagecache. We
1397 * must also discard the blockdev buffers so that we see the
1398 * changes done by userspace on the next quotaon() */
1399 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1400 if (toputinode[cnt]) {
d3be915f 1401 mutex_lock(&dqopt->dqonoff_mutex);
1da177e4
LT
1402 /* If quota was reenabled in the meantime, we have
1403 * nothing to do */
1404 if (!sb_has_quota_enabled(sb, cnt)) {
1b1dcc1b 1405 mutex_lock(&toputinode[cnt]->i_mutex);
1da177e4
LT
1406 toputinode[cnt]->i_flags &= ~(S_IMMUTABLE |
1407 S_NOATIME | S_NOQUOTA);
1408 truncate_inode_pages(&toputinode[cnt]->i_data, 0);
1b1dcc1b 1409 mutex_unlock(&toputinode[cnt]->i_mutex);
1da177e4
LT
1410 mark_inode_dirty(toputinode[cnt]);
1411 iput(toputinode[cnt]);
1412 }
d3be915f 1413 mutex_unlock(&dqopt->dqonoff_mutex);
1da177e4
LT
1414 }
1415 if (sb->s_bdev)
1416 invalidate_bdev(sb->s_bdev, 0);
1417 return 0;
1418}
1419
1420/*
1421 * Turn quotas on on a device
1422 */
1423
1424/* Helper function when we already have the inode */
1425static int vfs_quota_on_inode(struct inode *inode, int type, int format_id)
1426{
1427 struct quota_format_type *fmt = find_quota_format(format_id);
1428 struct super_block *sb = inode->i_sb;
1429 struct quota_info *dqopt = sb_dqopt(sb);
1430 int error;
1431 int oldflags = -1;
1432
1433 if (!fmt)
1434 return -ESRCH;
1435 if (!S_ISREG(inode->i_mode)) {
1436 error = -EACCES;
1437 goto out_fmt;
1438 }
1439 if (IS_RDONLY(inode)) {
1440 error = -EROFS;
1441 goto out_fmt;
1442 }
1443 if (!sb->s_op->quota_write || !sb->s_op->quota_read) {
1444 error = -EINVAL;
1445 goto out_fmt;
1446 }
1447
1448 /* As we bypass the pagecache we must now flush the inode so that
1449 * we see all the changes from userspace... */
1450 write_inode_now(inode, 1);
1451 /* And now flush the block cache so that kernel sees the changes */
1452 invalidate_bdev(sb->s_bdev, 0);
1b1dcc1b 1453 mutex_lock(&inode->i_mutex);
d3be915f 1454 mutex_lock(&dqopt->dqonoff_mutex);
1da177e4
LT
1455 if (sb_has_quota_enabled(sb, type)) {
1456 error = -EBUSY;
1457 goto out_lock;
1458 }
1459 /* We don't want quota and atime on quota files (deadlocks possible)
1460 * Also nobody should write to the file - we use special IO operations
1461 * which ignore the immutable bit. */
1462 down_write(&dqopt->dqptr_sem);
1463 oldflags = inode->i_flags & (S_NOATIME | S_IMMUTABLE | S_NOQUOTA);
1464 inode->i_flags |= S_NOQUOTA | S_NOATIME | S_IMMUTABLE;
1465 up_write(&dqopt->dqptr_sem);
31e7ad6a 1466 sb->dq_op->drop(inode);
1da177e4
LT
1467
1468 error = -EIO;
1469 dqopt->files[type] = igrab(inode);
1470 if (!dqopt->files[type])
1471 goto out_lock;
1472 error = -EINVAL;
1473 if (!fmt->qf_ops->check_quota_file(sb, type))
1474 goto out_file_init;
1475
1476 dqopt->ops[type] = fmt->qf_ops;
1477 dqopt->info[type].dqi_format = fmt;
1478 INIT_LIST_HEAD(&dqopt->info[type].dqi_dirty_list);
d3be915f 1479 mutex_lock(&dqopt->dqio_mutex);
1da177e4 1480 if ((error = dqopt->ops[type]->read_file_info(sb, type)) < 0) {
d3be915f 1481 mutex_unlock(&dqopt->dqio_mutex);
1da177e4
LT
1482 goto out_file_init;
1483 }
d3be915f 1484 mutex_unlock(&dqopt->dqio_mutex);
1b1dcc1b 1485 mutex_unlock(&inode->i_mutex);
1da177e4
LT
1486 set_enable_flags(dqopt, type);
1487
1488 add_dquot_ref(sb, type);
d3be915f 1489 mutex_unlock(&dqopt->dqonoff_mutex);
1da177e4
LT
1490
1491 return 0;
1492
1493out_file_init:
1494 dqopt->files[type] = NULL;
1495 iput(inode);
1496out_lock:
d3be915f 1497 mutex_unlock(&dqopt->dqonoff_mutex);
1da177e4
LT
1498 if (oldflags != -1) {
1499 down_write(&dqopt->dqptr_sem);
1500 /* Set the flags back (in the case of accidental quotaon()
1501 * on a wrong file we don't want to mess up the flags) */
1502 inode->i_flags &= ~(S_NOATIME | S_NOQUOTA | S_IMMUTABLE);
1503 inode->i_flags |= oldflags;
1504 up_write(&dqopt->dqptr_sem);
1505 }
1b1dcc1b 1506 mutex_unlock(&inode->i_mutex);
1da177e4
LT
1507out_fmt:
1508 put_quota_format(fmt);
1509
1510 return error;
1511}
1512
1513/* Actual function called from quotactl() */
1514int vfs_quota_on(struct super_block *sb, int type, int format_id, char *path)
1515{
1516 struct nameidata nd;
1517 int error;
1518
1519 error = path_lookup(path, LOOKUP_FOLLOW, &nd);
1520 if (error < 0)
1521 return error;
1522 error = security_quota_on(nd.dentry);
1523 if (error)
1524 goto out_path;
1525 /* Quota file not on the same filesystem? */
1526 if (nd.mnt->mnt_sb != sb)
1527 error = -EXDEV;
7b7b1ace 1528 else
1da177e4 1529 error = vfs_quota_on_inode(nd.dentry->d_inode, type, format_id);
1da177e4
LT
1530out_path:
1531 path_release(&nd);
1532 return error;
1533}
1534
1535/*
1536 * This function is used when filesystem needs to initialize quotas
1537 * during mount time.
1538 */
84de856e
CH
1539int vfs_quota_on_mount(struct super_block *sb, char *qf_name,
1540 int format_id, int type)
1da177e4 1541{
84de856e 1542 struct dentry *dentry;
1da177e4
LT
1543 int error;
1544
2fa389c5 1545 dentry = lookup_one_len(qf_name, sb->s_root, strlen(qf_name));
84de856e
CH
1546 if (IS_ERR(dentry))
1547 return PTR_ERR(dentry);
1548
154f484b
JK
1549 if (!dentry->d_inode) {
1550 error = -ENOENT;
1551 goto out;
1552 }
1553
1da177e4 1554 error = security_quota_on(dentry);
84de856e
CH
1555 if (!error)
1556 error = vfs_quota_on_inode(dentry->d_inode, type, format_id);
1557
154f484b 1558out:
84de856e
CH
1559 dput(dentry);
1560 return error;
1da177e4
LT
1561}
1562
1563/* Generic routine for getting common part of quota structure */
1564static void do_get_dqblk(struct dquot *dquot, struct if_dqblk *di)
1565{
1566 struct mem_dqblk *dm = &dquot->dq_dqb;
1567
1568 spin_lock(&dq_data_lock);
1569 di->dqb_bhardlimit = dm->dqb_bhardlimit;
1570 di->dqb_bsoftlimit = dm->dqb_bsoftlimit;
1571 di->dqb_curspace = dm->dqb_curspace;
1572 di->dqb_ihardlimit = dm->dqb_ihardlimit;
1573 di->dqb_isoftlimit = dm->dqb_isoftlimit;
1574 di->dqb_curinodes = dm->dqb_curinodes;
1575 di->dqb_btime = dm->dqb_btime;
1576 di->dqb_itime = dm->dqb_itime;
1577 di->dqb_valid = QIF_ALL;
1578 spin_unlock(&dq_data_lock);
1579}
1580
1581int vfs_get_dqblk(struct super_block *sb, int type, qid_t id, struct if_dqblk *di)
1582{
1583 struct dquot *dquot;
1584
d3be915f 1585 mutex_lock(&sb_dqopt(sb)->dqonoff_mutex);
1da177e4 1586 if (!(dquot = dqget(sb, id, type))) {
d3be915f 1587 mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
1da177e4
LT
1588 return -ESRCH;
1589 }
1590 do_get_dqblk(dquot, di);
1591 dqput(dquot);
d3be915f 1592 mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
1da177e4
LT
1593 return 0;
1594}
1595
1596/* Generic routine for setting common part of quota structure */
1597static void do_set_dqblk(struct dquot *dquot, struct if_dqblk *di)
1598{
1599 struct mem_dqblk *dm = &dquot->dq_dqb;
1600 int check_blim = 0, check_ilim = 0;
1601
1602 spin_lock(&dq_data_lock);
1603 if (di->dqb_valid & QIF_SPACE) {
1604 dm->dqb_curspace = di->dqb_curspace;
1605 check_blim = 1;
1606 }
1607 if (di->dqb_valid & QIF_BLIMITS) {
1608 dm->dqb_bsoftlimit = di->dqb_bsoftlimit;
1609 dm->dqb_bhardlimit = di->dqb_bhardlimit;
1610 check_blim = 1;
1611 }
1612 if (di->dqb_valid & QIF_INODES) {
1613 dm->dqb_curinodes = di->dqb_curinodes;
1614 check_ilim = 1;
1615 }
1616 if (di->dqb_valid & QIF_ILIMITS) {
1617 dm->dqb_isoftlimit = di->dqb_isoftlimit;
1618 dm->dqb_ihardlimit = di->dqb_ihardlimit;
1619 check_ilim = 1;
1620 }
1621 if (di->dqb_valid & QIF_BTIME)
1622 dm->dqb_btime = di->dqb_btime;
1623 if (di->dqb_valid & QIF_ITIME)
1624 dm->dqb_itime = di->dqb_itime;
1625
1626 if (check_blim) {
1627 if (!dm->dqb_bsoftlimit || toqb(dm->dqb_curspace) < dm->dqb_bsoftlimit) {
1628 dm->dqb_btime = 0;
1629 clear_bit(DQ_BLKS_B, &dquot->dq_flags);
1630 }
1631 else if (!(di->dqb_valid & QIF_BTIME)) /* Set grace only if user hasn't provided his own... */
1632 dm->dqb_btime = get_seconds() + sb_dqopt(dquot->dq_sb)->info[dquot->dq_type].dqi_bgrace;
1633 }
1634 if (check_ilim) {
1635 if (!dm->dqb_isoftlimit || dm->dqb_curinodes < dm->dqb_isoftlimit) {
1636 dm->dqb_itime = 0;
1637 clear_bit(DQ_INODES_B, &dquot->dq_flags);
1638 }
1639 else if (!(di->dqb_valid & QIF_ITIME)) /* Set grace only if user hasn't provided his own... */
1640 dm->dqb_itime = get_seconds() + sb_dqopt(dquot->dq_sb)->info[dquot->dq_type].dqi_igrace;
1641 }
1642 if (dm->dqb_bhardlimit || dm->dqb_bsoftlimit || dm->dqb_ihardlimit || dm->dqb_isoftlimit)
1643 clear_bit(DQ_FAKE_B, &dquot->dq_flags);
1644 else
1645 set_bit(DQ_FAKE_B, &dquot->dq_flags);
1646 spin_unlock(&dq_data_lock);
1647 mark_dquot_dirty(dquot);
1648}
1649
1650int vfs_set_dqblk(struct super_block *sb, int type, qid_t id, struct if_dqblk *di)
1651{
1652 struct dquot *dquot;
1653
d3be915f 1654 mutex_lock(&sb_dqopt(sb)->dqonoff_mutex);
1da177e4 1655 if (!(dquot = dqget(sb, id, type))) {
d3be915f 1656 mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
1da177e4
LT
1657 return -ESRCH;
1658 }
1659 do_set_dqblk(dquot, di);
1660 dqput(dquot);
d3be915f 1661 mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
1da177e4
LT
1662 return 0;
1663}
1664
1665/* Generic routine for getting common part of quota file information */
1666int vfs_get_dqinfo(struct super_block *sb, int type, struct if_dqinfo *ii)
1667{
1668 struct mem_dqinfo *mi;
1669
d3be915f 1670 mutex_lock(&sb_dqopt(sb)->dqonoff_mutex);
1da177e4 1671 if (!sb_has_quota_enabled(sb, type)) {
d3be915f 1672 mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
1da177e4
LT
1673 return -ESRCH;
1674 }
1675 mi = sb_dqopt(sb)->info + type;
1676 spin_lock(&dq_data_lock);
1677 ii->dqi_bgrace = mi->dqi_bgrace;
1678 ii->dqi_igrace = mi->dqi_igrace;
1679 ii->dqi_flags = mi->dqi_flags & DQF_MASK;
1680 ii->dqi_valid = IIF_ALL;
1681 spin_unlock(&dq_data_lock);
d3be915f 1682 mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
1da177e4
LT
1683 return 0;
1684}
1685
1686/* Generic routine for setting common part of quota file information */
1687int vfs_set_dqinfo(struct super_block *sb, int type, struct if_dqinfo *ii)
1688{
1689 struct mem_dqinfo *mi;
1690
d3be915f 1691 mutex_lock(&sb_dqopt(sb)->dqonoff_mutex);
1da177e4 1692 if (!sb_has_quota_enabled(sb, type)) {
d3be915f 1693 mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
1da177e4
LT
1694 return -ESRCH;
1695 }
1696 mi = sb_dqopt(sb)->info + type;
1697 spin_lock(&dq_data_lock);
1698 if (ii->dqi_valid & IIF_BGRACE)
1699 mi->dqi_bgrace = ii->dqi_bgrace;
1700 if (ii->dqi_valid & IIF_IGRACE)
1701 mi->dqi_igrace = ii->dqi_igrace;
1702 if (ii->dqi_valid & IIF_FLAGS)
1703 mi->dqi_flags = (mi->dqi_flags & ~DQF_MASK) | (ii->dqi_flags & DQF_MASK);
1704 spin_unlock(&dq_data_lock);
1705 mark_info_dirty(sb, type);
1706 /* Force write to disk */
1707 sb->dq_op->write_info(sb, type);
d3be915f 1708 mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
1da177e4
LT
1709 return 0;
1710}
1711
1712struct quotactl_ops vfs_quotactl_ops = {
1713 .quota_on = vfs_quota_on,
1714 .quota_off = vfs_quota_off,
1715 .quota_sync = vfs_quota_sync,
1716 .get_info = vfs_get_dqinfo,
1717 .set_info = vfs_set_dqinfo,
1718 .get_dqblk = vfs_get_dqblk,
1719 .set_dqblk = vfs_set_dqblk
1720};
1721
1722static ctl_table fs_dqstats_table[] = {
1723 {
1724 .ctl_name = FS_DQ_LOOKUPS,
1725 .procname = "lookups",
1726 .data = &dqstats.lookups,
1727 .maxlen = sizeof(int),
1728 .mode = 0444,
1729 .proc_handler = &proc_dointvec,
1730 },
1731 {
1732 .ctl_name = FS_DQ_DROPS,
1733 .procname = "drops",
1734 .data = &dqstats.drops,
1735 .maxlen = sizeof(int),
1736 .mode = 0444,
1737 .proc_handler = &proc_dointvec,
1738 },
1739 {
1740 .ctl_name = FS_DQ_READS,
1741 .procname = "reads",
1742 .data = &dqstats.reads,
1743 .maxlen = sizeof(int),
1744 .mode = 0444,
1745 .proc_handler = &proc_dointvec,
1746 },
1747 {
1748 .ctl_name = FS_DQ_WRITES,
1749 .procname = "writes",
1750 .data = &dqstats.writes,
1751 .maxlen = sizeof(int),
1752 .mode = 0444,
1753 .proc_handler = &proc_dointvec,
1754 },
1755 {
1756 .ctl_name = FS_DQ_CACHE_HITS,
1757 .procname = "cache_hits",
1758 .data = &dqstats.cache_hits,
1759 .maxlen = sizeof(int),
1760 .mode = 0444,
1761 .proc_handler = &proc_dointvec,
1762 },
1763 {
1764 .ctl_name = FS_DQ_ALLOCATED,
1765 .procname = "allocated_dquots",
1766 .data = &dqstats.allocated_dquots,
1767 .maxlen = sizeof(int),
1768 .mode = 0444,
1769 .proc_handler = &proc_dointvec,
1770 },
1771 {
1772 .ctl_name = FS_DQ_FREE,
1773 .procname = "free_dquots",
1774 .data = &dqstats.free_dquots,
1775 .maxlen = sizeof(int),
1776 .mode = 0444,
1777 .proc_handler = &proc_dointvec,
1778 },
1779 {
1780 .ctl_name = FS_DQ_SYNCS,
1781 .procname = "syncs",
1782 .data = &dqstats.syncs,
1783 .maxlen = sizeof(int),
1784 .mode = 0444,
1785 .proc_handler = &proc_dointvec,
1786 },
1787 {
1788 .ctl_name = FS_DQ_WARNINGS,
1789 .procname = "warnings",
1790 .data = &flag_print_warnings,
1791 .maxlen = sizeof(int),
1792 .mode = 0644,
1793 .proc_handler = &proc_dointvec,
1794 },
1795 { .ctl_name = 0 },
1796};
1797
1798static ctl_table fs_table[] = {
1799 {
1800 .ctl_name = FS_DQSTATS,
1801 .procname = "quota",
1802 .mode = 0555,
1803 .child = fs_dqstats_table,
1804 },
1805 { .ctl_name = 0 },
1806};
1807
1808static ctl_table sys_table[] = {
1809 {
1810 .ctl_name = CTL_FS,
1811 .procname = "fs",
1812 .mode = 0555,
1813 .child = fs_table,
1814 },
1815 { .ctl_name = 0 },
1816};
1817
1818static int __init dquot_init(void)
1819{
1820 int i;
1821 unsigned long nr_hash, order;
1822
1823 printk(KERN_NOTICE "VFS: Disk quotas %s\n", __DQUOT_VERSION__);
1824
1825 register_sysctl_table(sys_table, 0);
1826
1827 dquot_cachep = kmem_cache_create("dquot",
1828 sizeof(struct dquot), sizeof(unsigned long) * 4,
fffb60f9
PJ
1829 (SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT|
1830 SLAB_MEM_SPREAD|SLAB_PANIC),
1da177e4
LT
1831 NULL, NULL);
1832
1833 order = 0;
1834 dquot_hash = (struct hlist_head *)__get_free_pages(GFP_ATOMIC, order);
1835 if (!dquot_hash)
1836 panic("Cannot create dquot hash table");
1837
1838 /* Find power-of-two hlist_heads which can fit into allocation */
1839 nr_hash = (1UL << order) * PAGE_SIZE / sizeof(struct hlist_head);
1840 dq_hash_bits = 0;
1841 do {
1842 dq_hash_bits++;
1843 } while (nr_hash >> dq_hash_bits);
1844 dq_hash_bits--;
1845
1846 nr_hash = 1UL << dq_hash_bits;
1847 dq_hash_mask = nr_hash - 1;
1848 for (i = 0; i < nr_hash; i++)
1849 INIT_HLIST_HEAD(dquot_hash + i);
1850
1851 printk("Dquot-cache hash table entries: %ld (order %ld, %ld bytes)\n",
1852 nr_hash, order, (PAGE_SIZE << order));
1853
1854 set_shrinker(DEFAULT_SEEKS, shrink_dqcache_memory);
1855
1856 return 0;
1857}
1858module_init(dquot_init);
1859
1860EXPORT_SYMBOL(register_quota_format);
1861EXPORT_SYMBOL(unregister_quota_format);
1862EXPORT_SYMBOL(dqstats);
1863EXPORT_SYMBOL(dq_data_lock);
1864EXPORT_SYMBOL(vfs_quota_on);
1865EXPORT_SYMBOL(vfs_quota_on_mount);
1866EXPORT_SYMBOL(vfs_quota_off);
1867EXPORT_SYMBOL(vfs_quota_sync);
1868EXPORT_SYMBOL(vfs_get_dqinfo);
1869EXPORT_SYMBOL(vfs_set_dqinfo);
1870EXPORT_SYMBOL(vfs_get_dqblk);
1871EXPORT_SYMBOL(vfs_set_dqblk);
1872EXPORT_SYMBOL(dquot_commit);
1873EXPORT_SYMBOL(dquot_commit_info);
1874EXPORT_SYMBOL(dquot_acquire);
1875EXPORT_SYMBOL(dquot_release);
1876EXPORT_SYMBOL(dquot_mark_dquot_dirty);
1877EXPORT_SYMBOL(dquot_initialize);
1878EXPORT_SYMBOL(dquot_drop);
1879EXPORT_SYMBOL(dquot_alloc_space);
1880EXPORT_SYMBOL(dquot_alloc_inode);
1881EXPORT_SYMBOL(dquot_free_space);
1882EXPORT_SYMBOL(dquot_free_inode);
1883EXPORT_SYMBOL(dquot_transfer);