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