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