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