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