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