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