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