v2.4.10.4 -> v2.4.10.5
[linux-flexiantxendom0-3.2.10.git] / fs / dquot.c
1 /*
2  * Implementation of the diskquota system for the LINUX operating
3  * system. QUOTA is implemented using the BSD system call interface as
4  * the means of communication with the user level. Currently only the
5  * ext2 filesystem has support for disk quotas. Other filesystems may
6  * be added in the future. This file contains the generic routines
7  * called by the different filesystems on allocation of an inode or
8  * block. These routines take care of the administration needed to
9  * have a consistent diskquota tracking system. The ideas of both
10  * user and group quotas are based on the Melbourne quota system as
11  * used on BSD derived systems. The internal implementation is 
12  * based on one of the several variants of the LINUX inode-subsystem
13  * with added complexity of the diskquota system.
14  * 
15  * Version: $Id: dquot.c,v 6.3 1996/11/17 18:35:34 mvw Exp mvw $
16  * 
17  * Author:      Marco van Wieringen <mvw@planets.elm.net>
18  *
19  * Fixes:   Dmitry Gorodchanin <pgmdsg@ibi.com>, 11 Feb 96
20  *
21  *              Revised list management to avoid races
22  *              -- Bill Hawes, <whawes@star.net>, 9/98
23  *
24  *              Fixed races in dquot_transfer(), dqget() and dquot_alloc_...().
25  *              As the consequence the locking was moved from dquot_decr_...(),
26  *              dquot_incr_...() to calling functions.
27  *              invalidate_dquots() now writes modified dquots.
28  *              Serialized quota_off() and quota_on() for mount point.
29  *              Fixed a few bugs in grow_dquots().
30  *              Fixed deadlock in write_dquot() - we no longer account quotas on
31  *              quota files
32  *              remove_dquot_ref() moved to inode.c - it now traverses through inodes
33  *              add_dquot_ref() restarts after blocking
34  *              Added check for bogus uid and fixed check for group in quotactl.
35  *              Jan Kara, <jack@suse.cz>, sponsored by SuSE CR, 10-11/99
36  *
37  *              Used struct list_head instead of own list struct
38  *              Invalidation of dquots with dq_count > 0 no longer possible
39  *              Improved free_dquots list management
40  *              Quota and i_blocks are now updated in one place to avoid races
41  *              Warnings are now delayed so we won't block in critical section
42  *              Write updated not to require dquot lock
43  *              Jan Kara, <jack@suse.cz>, 9/2000
44  *
45  *              Added dynamic quota structure allocation
46  *              Jan Kara <jack@suse.cz> 12/2000
47  *
48  * (C) Copyright 1994 - 1997 Marco van Wieringen 
49  */
50
51 #include <linux/errno.h>
52 #include <linux/kernel.h>
53 #include <linux/fs.h>
54 #include <linux/sched.h>
55 #include <linux/types.h>
56 #include <linux/string.h>
57 #include <linux/fcntl.h>
58 #include <linux/stat.h>
59 #include <linux/tty.h>
60 #include <linux/file.h>
61 #include <linux/slab.h>
62 #include <linux/smp_lock.h>
63 #include <linux/init.h>
64
65 #include <asm/uaccess.h>
66
67 #define __DQUOT_VERSION__       "dquot_6.4.0"
68
69 int nr_dquots, nr_free_dquots;
70
71 static char *quotatypes[] = INITQFNAMES;
72
73 static inline struct quota_mount_options *sb_dqopt(struct super_block *sb)
74 {
75         return &sb->s_dquot;
76 }
77
78 /*
79  * Dquot List Management:
80  * The quota code uses three lists for dquot management: the inuse_list,
81  * free_dquots, and dquot_hash[] array. A single dquot structure may be
82  * on all three lists, depending on its current state.
83  *
84  * All dquots are placed to the end of inuse_list when first created, and this
85  * list is used for the sync and invalidate operations, which must look
86  * at every dquot.
87  *
88  * Unused dquots (dq_count == 0) are added to the free_dquots list when
89  * freed, and this list is searched whenever we need an available dquot.
90  * Dquots are removed from the list as soon as they are used again, and
91  * nr_free_dquots gives the number of dquots on the list. When dquot is
92  * invalidated it's completely released from memory.
93  *
94  * Dquots with a specific identity (device, type and id) are placed on
95  * one of the dquot_hash[] hash chains. The provides an efficient search
96  * mechanism to locate a specific dquot.
97  */
98
99 /*
100  * Note that any operation which operates on dquot data (ie. dq_dqb) mustn't
101  * block while it's updating/reading it. Otherwise races would occur.
102  *
103  * Locked dquots might not be referenced in inodes - operations like
104  * add_dquot_space() does dqduplicate() and would complain. Currently
105  * dquot it locked only once in its existence - when it's being read
106  * to memory on first dqget() and at that time it can't be referenced
107  * from inode. Write operations on dquots don't hold dquot lock as they
108  * copy data to internal buffers before writing anyway and copying as well
109  * as any data update should be atomic. Also nobody can change used
110  * entries in dquot structure as this is done only when quota is destroyed
111  * and invalidate_dquots() waits for dquot to have dq_count == 0.
112  */
113
114 static LIST_HEAD(inuse_list);
115 static LIST_HEAD(free_dquots);
116 static struct list_head dquot_hash[NR_DQHASH];
117
118 static struct dqstats dqstats;
119
120 static void dqput(struct dquot *);
121 static struct dquot *dqduplicate(struct dquot *);
122
123 static inline char is_enabled(struct quota_mount_options *dqopt, short type)
124 {
125         switch (type) {
126                 case USRQUOTA:
127                         return((dqopt->flags & DQUOT_USR_ENABLED) != 0);
128                 case GRPQUOTA:
129                         return((dqopt->flags & DQUOT_GRP_ENABLED) != 0);
130         }
131         return(0);
132 }
133
134 static inline char sb_has_quota_enabled(struct super_block *sb, short type)
135 {
136         return is_enabled(sb_dqopt(sb), type);
137 }
138
139 static inline int const hashfn(kdev_t dev, unsigned int id, short type)
140 {
141         return((HASHDEV(dev) ^ id) * (MAXQUOTAS - type)) % NR_DQHASH;
142 }
143
144 static inline void insert_dquot_hash(struct dquot *dquot)
145 {
146         struct list_head *head = dquot_hash + hashfn(dquot->dq_dev, dquot->dq_id, dquot->dq_type);
147         list_add(&dquot->dq_hash, head);
148 }
149
150 static inline void remove_dquot_hash(struct dquot *dquot)
151 {
152         list_del(&dquot->dq_hash);
153         INIT_LIST_HEAD(&dquot->dq_hash);
154 }
155
156 static inline struct dquot *find_dquot(unsigned int hashent, kdev_t dev, unsigned int id, short type)
157 {
158         struct list_head *head;
159         struct dquot *dquot;
160
161         for (head = dquot_hash[hashent].next; head != dquot_hash+hashent; head = head->next) {
162                 dquot = list_entry(head, struct dquot, dq_hash);
163                 if (dquot->dq_dev == dev && dquot->dq_id == id && dquot->dq_type == type)
164                         return dquot;
165         }
166         return NODQUOT;
167 }
168
169 /* Add a dquot to the head of the free list */
170 static inline void put_dquot_head(struct dquot *dquot)
171 {
172         list_add(&dquot->dq_free, &free_dquots);
173         nr_free_dquots++;
174 }
175
176 /* Add a dquot to the tail of the free list */
177 static inline void put_dquot_last(struct dquot *dquot)
178 {
179         list_add(&dquot->dq_free, free_dquots.prev);
180         nr_free_dquots++;
181 }
182
183 /* Move dquot to the head of free list (it must be already on it) */
184 static inline void move_dquot_head(struct dquot *dquot)
185 {
186         list_del(&dquot->dq_free);
187         list_add(&dquot->dq_free, &free_dquots);
188 }
189
190 static inline void remove_free_dquot(struct dquot *dquot)
191 {
192         /* sanity check */
193         if (list_empty(&dquot->dq_free)) {
194                 printk("remove_free_dquot: dquot not on the free list??\n");
195                 return;         /* J.K. Just don't do anything */
196         }
197         list_del(&dquot->dq_free);
198         INIT_LIST_HEAD(&dquot->dq_free);
199         nr_free_dquots--;
200 }
201
202 static inline void put_inuse(struct dquot *dquot)
203 {
204         /* We add to the back of inuse list so we don't have to restart
205          * when traversing this list and we block */
206         list_add(&dquot->dq_inuse, inuse_list.prev);
207         nr_dquots++;
208 }
209
210 static inline void remove_inuse(struct dquot *dquot)
211 {
212         nr_dquots--;
213         list_del(&dquot->dq_inuse);
214 }
215
216 static void __wait_on_dquot(struct dquot *dquot)
217 {
218         DECLARE_WAITQUEUE(wait, current);
219
220         add_wait_queue(&dquot->dq_wait_lock, &wait);
221 repeat:
222         set_current_state(TASK_UNINTERRUPTIBLE);
223         if (dquot->dq_flags & DQ_LOCKED) {
224                 schedule();
225                 goto repeat;
226         }
227         remove_wait_queue(&dquot->dq_wait_lock, &wait);
228         current->state = TASK_RUNNING;
229 }
230
231 static inline void wait_on_dquot(struct dquot *dquot)
232 {
233         if (dquot->dq_flags & DQ_LOCKED)
234                 __wait_on_dquot(dquot);
235 }
236
237 static inline void lock_dquot(struct dquot *dquot)
238 {
239         wait_on_dquot(dquot);
240         dquot->dq_flags |= DQ_LOCKED;
241 }
242
243 static inline void unlock_dquot(struct dquot *dquot)
244 {
245         dquot->dq_flags &= ~DQ_LOCKED;
246         wake_up(&dquot->dq_wait_lock);
247 }
248
249 static void __wait_dquot_unused(struct dquot *dquot)
250 {
251         DECLARE_WAITQUEUE(wait, current);
252
253         add_wait_queue(&dquot->dq_wait_free, &wait);
254 repeat:
255         set_current_state(TASK_UNINTERRUPTIBLE);
256         if (dquot->dq_count) {
257                 schedule();
258                 goto repeat;
259         }
260         remove_wait_queue(&dquot->dq_wait_free, &wait);
261         current->state = TASK_RUNNING;
262 }
263
264 /*
265  *      We don't have to be afraid of deadlocks as we never have quotas on quota files...
266  */
267 static void write_dquot(struct dquot *dquot)
268 {
269         short type = dquot->dq_type;
270         struct file *filp;
271         mm_segment_t fs;
272         loff_t offset;
273         ssize_t ret;
274         struct semaphore *sem = &dquot->dq_sb->s_dquot.dqio_sem;
275         struct dqblk dqbuf;
276
277         down(sem);
278         filp = dquot->dq_sb->s_dquot.files[type];
279         offset = dqoff(dquot->dq_id);
280         fs = get_fs();
281         set_fs(KERNEL_DS);
282
283         /*
284          * Note: clear the DQ_MOD flag unconditionally,
285          * so we don't loop forever on failure.
286          */
287         memcpy(&dqbuf, &dquot->dq_dqb, sizeof(struct dqblk));
288         dquot->dq_flags &= ~DQ_MOD;
289         ret = 0;
290         if (filp)
291                 ret = filp->f_op->write(filp, (char *)&dqbuf, 
292                                         sizeof(struct dqblk), &offset);
293         if (ret != sizeof(struct dqblk))
294                 printk(KERN_WARNING "VFS: dquota write failed on dev %s\n",
295                         kdevname(dquot->dq_dev));
296
297         set_fs(fs);
298         up(sem);
299         dqstats.writes++;
300 }
301
302 static void read_dquot(struct dquot *dquot)
303 {
304         short type = dquot->dq_type;
305         struct file *filp;
306         mm_segment_t fs;
307         loff_t offset;
308
309         filp = dquot->dq_sb->s_dquot.files[type];
310         if (filp == (struct file *)NULL)
311                 return;
312
313         lock_dquot(dquot);
314         if (!dquot->dq_sb)      /* Invalidated quota? */
315                 goto out_lock;
316         /* Now we are sure filp is valid - the dquot isn't invalidated */
317         down(&dquot->dq_sb->s_dquot.dqio_sem);
318         offset = dqoff(dquot->dq_id);
319         fs = get_fs();
320         set_fs(KERNEL_DS);
321         filp->f_op->read(filp, (char *)&dquot->dq_dqb, sizeof(struct dqblk), &offset);
322         up(&dquot->dq_sb->s_dquot.dqio_sem);
323         set_fs(fs);
324
325         if (dquot->dq_bhardlimit == 0 && dquot->dq_bsoftlimit == 0 &&
326             dquot->dq_ihardlimit == 0 && dquot->dq_isoftlimit == 0)
327                 dquot->dq_flags |= DQ_FAKE;
328         dqstats.reads++;
329 out_lock:
330         unlock_dquot(dquot);
331 }
332
333 /*
334  * Unhash and selectively clear the dquot structure,
335  * but preserve the use count, list pointers, and
336  * wait queue.
337  */
338 void clear_dquot(struct dquot *dquot)
339 {
340         /* unhash it first */
341         remove_dquot_hash(dquot);
342         dquot->dq_sb = NULL;
343         dquot->dq_id = 0;
344         dquot->dq_dev = NODEV;
345         dquot->dq_type = -1;
346         dquot->dq_flags = 0;
347         dquot->dq_referenced = 0;
348         memset(&dquot->dq_dqb, 0, sizeof(struct dqblk));
349 }
350
351 /* Invalidate all dquots on the list, wait for all users. Note that this function is called
352  * after quota is disabled so no new quota might be created. As we only insert to the end of
353  * inuse list, we don't have to restart searching... */
354 static void invalidate_dquots(struct super_block *sb, short type)
355 {
356         struct dquot *dquot;
357         struct list_head *head;
358
359 restart:
360         for (head = inuse_list.next; head != &inuse_list; head = head->next) {
361                 dquot = list_entry(head, struct dquot, dq_inuse);
362                 if (dquot->dq_sb != sb)
363                         continue;
364                 if (dquot->dq_type != type)
365                         continue;
366                 if (dquot->dq_count)
367                         /*
368                          *  Wait for any users of quota. As we have already cleared the flags in
369                          *  superblock and cleared all pointers from inodes we are assured
370                          *  that there will be no new users of this quota.
371                          */
372                         __wait_dquot_unused(dquot);
373                 /* Quota now have no users and it has been written on last dqput() */
374                 remove_dquot_hash(dquot);
375                 remove_free_dquot(dquot);
376                 remove_inuse(dquot);
377                 kmem_cache_free(dquot_cachep, dquot);
378                 goto restart;
379         }
380 }
381
382 int sync_dquots(kdev_t dev, short type)
383 {
384         struct list_head *head;
385         struct dquot *dquot;
386
387 restart:
388         for (head = inuse_list.next; head != &inuse_list; head = head->next) {
389                 dquot = list_entry(head, struct dquot, dq_inuse);
390                 if (dev && dquot->dq_dev != dev)
391                         continue;
392                 if (type != -1 && dquot->dq_type != type)
393                         continue;
394                 if (!dquot->dq_sb)      /* Invalidated? */
395                         continue;
396                 if (!(dquot->dq_flags & (DQ_MOD | DQ_LOCKED)))
397                         continue;
398                 /* Raise use count so quota won't be invalidated. We can't use dqduplicate() as it does too many tests */
399                 dquot->dq_count++;
400                 if (dquot->dq_flags & DQ_LOCKED)
401                         wait_on_dquot(dquot);
402                 if (dquot->dq_flags & DQ_MOD)
403                         write_dquot(dquot);
404                 dqput(dquot);
405                 goto restart;
406         }
407         dqstats.syncs++;
408         return 0;
409 }
410
411 /* Free unused dquots from cache */
412 static void prune_dqcache(int count)
413 {
414         struct list_head *head;
415         struct dquot *dquot;
416
417         head = free_dquots.prev;
418         while (head != &free_dquots && count) {
419                 dquot = list_entry(head, struct dquot, dq_free);
420                 remove_dquot_hash(dquot);
421                 remove_free_dquot(dquot);
422                 remove_inuse(dquot);
423                 kmem_cache_free(dquot_cachep, dquot);
424                 count--;
425                 head = free_dquots.prev;
426         }
427 }
428
429 int shrink_dqcache_memory(int priority, unsigned int gfp_mask)
430 {
431         prune_dqcache(nr_free_dquots / (priority + 1));
432         kmem_cache_shrink(dquot_cachep);
433         return 0;
434 }
435
436 /* NOTE: If you change this function please check whether dqput_blocks() works right... */
437 static void dqput(struct dquot *dquot)
438 {
439         if (!dquot)
440                 return;
441         if (!dquot->dq_count) {
442                 printk("VFS: dqput: trying to free free dquot\n");
443                 printk("VFS: device %s, dquot of %s %d\n",
444                         kdevname(dquot->dq_dev), quotatypes[dquot->dq_type],
445                         dquot->dq_id);
446                 return;
447         }
448
449         dqstats.drops++;
450 we_slept:
451         if (dquot->dq_count > 1) {
452                 /* We have more than one user... We can simply decrement use count */
453                 dquot->dq_count--;
454                 return;
455         }
456         if (dquot->dq_flags & DQ_MOD) {
457                 write_dquot(dquot);
458                 goto we_slept;
459         }
460
461         /* sanity check */
462         if (!list_empty(&dquot->dq_free)) {
463                 printk(KERN_ERR "dqput: dquot already on free list??\n");
464                 dquot->dq_count--;      /* J.K. Just decrementing use count seems safer... */
465                 return;
466         }
467         dquot->dq_count--;
468         /* Place at end of LRU free queue */
469         put_dquot_last(dquot);
470         wake_up(&dquot->dq_wait_free);
471 }
472
473 struct dquot *get_empty_dquot(void)
474 {
475         struct dquot *dquot;
476
477         dquot = kmem_cache_alloc(dquot_cachep, SLAB_KERNEL);
478         if(!dquot)
479                 return NODQUOT;
480
481         memset((caddr_t)dquot, 0, sizeof(struct dquot));
482         init_waitqueue_head(&dquot->dq_wait_free);
483         init_waitqueue_head(&dquot->dq_wait_lock);
484         INIT_LIST_HEAD(&dquot->dq_free);
485         INIT_LIST_HEAD(&dquot->dq_inuse);
486         INIT_LIST_HEAD(&dquot->dq_hash);
487         dquot->dq_count = 1;
488         /* all dquots go on the inuse_list */
489         put_inuse(dquot);
490
491         return dquot;
492 }
493
494 static struct dquot *dqget(struct super_block *sb, unsigned int id, short type)
495 {
496         unsigned int hashent = hashfn(sb->s_dev, id, type);
497         struct dquot *dquot, *empty = NODQUOT;
498         struct quota_mount_options *dqopt = sb_dqopt(sb);
499
500 we_slept:
501         if (!is_enabled(dqopt, type)) {
502                 if (empty)
503                         dqput(empty);
504                 return NODQUOT;
505         }
506
507         if ((dquot = find_dquot(hashent, sb->s_dev, id, type)) == NODQUOT) {
508                 if (empty == NODQUOT) {
509                         if ((empty = get_empty_dquot()) == NODQUOT)
510                                 schedule();     /* Try to wait for a moment... */
511                         goto we_slept;
512                 }
513                 dquot = empty;
514                 dquot->dq_id = id;
515                 dquot->dq_type = type;
516                 dquot->dq_dev = sb->s_dev;
517                 dquot->dq_sb = sb;
518                 /* hash it first so it can be found */
519                 insert_dquot_hash(dquot);
520                 read_dquot(dquot);
521         } else {
522                 if (!dquot->dq_count++)
523                         remove_free_dquot(dquot);
524                 dqstats.cache_hits++;
525                 wait_on_dquot(dquot);
526                 if (empty)
527                         dqput(empty);
528         }
529
530         if (!dquot->dq_sb) {    /* Has somebody invalidated entry under us? */
531                 printk(KERN_ERR "VFS: dqget(): Quota invalidated in dqget()!\n");
532                 dqput(dquot);
533                 return NODQUOT;
534         }
535         dquot->dq_referenced++;
536         dqstats.lookups++;
537
538         return dquot;
539 }
540
541 static struct dquot *dqduplicate(struct dquot *dquot)
542 {
543         if (dquot == NODQUOT)
544                 return NODQUOT;
545         dquot->dq_count++;
546         if (!dquot->dq_sb) {
547                 printk(KERN_ERR "VFS: dqduplicate(): Invalidated quota to be duplicated!\n");
548                 dquot->dq_count--;
549                 return NODQUOT;
550         }
551         if (dquot->dq_flags & DQ_LOCKED)
552                 printk(KERN_ERR "VFS: dqduplicate(): Locked quota to be duplicated!\n");
553         dquot->dq_referenced++;
554         dqstats.lookups++;
555         return dquot;
556 }
557
558 static int dqinit_needed(struct inode *inode, short type)
559 {
560         int cnt;
561
562         if (IS_NOQUOTA(inode))
563                 return 0;
564         if (type != -1)
565                 return inode->i_dquot[type] == NODQUOT;
566         for (cnt = 0; cnt < MAXQUOTAS; cnt++)
567                 if (inode->i_dquot[cnt] == NODQUOT)
568                         return 1;
569         return 0;
570 }
571
572 static void add_dquot_ref(struct super_block *sb, short type)
573 {
574         struct list_head *p;
575
576         if (!sb->dq_op)
577                 return; /* nothing to do */
578
579 restart:
580         file_list_lock();
581         for (p = sb->s_files.next; p != &sb->s_files; p = p->next) {
582                 struct file *filp = list_entry(p, struct file, f_list);
583                 struct inode *inode = filp->f_dentry->d_inode;
584                 if (filp->f_mode & FMODE_WRITE && dqinit_needed(inode, type)) {
585                         struct vfsmount *mnt = mntget(filp->f_vfsmnt);
586                         struct dentry *dentry = dget(filp->f_dentry);
587                         file_list_unlock();
588                         sb->dq_op->initialize(inode, type);
589                         dput(dentry);
590                         mntput(mnt);
591                         /* As we may have blocked we had better restart... */
592                         goto restart;
593                 }
594         }
595         file_list_unlock();
596 }
597
598 /* Return 0 if dqput() won't block (note that 1 doesn't necessarily mean blocking) */
599 static inline int dqput_blocks(struct dquot *dquot)
600 {
601         if (dquot->dq_count == 1)
602                 return 1;
603         return 0;
604 }
605
606 /* Remove references to dquots from inode - add dquot to list for freeing if needed */
607 int remove_inode_dquot_ref(struct inode *inode, short type, struct list_head *tofree_head)
608 {
609         struct dquot *dquot = inode->i_dquot[type];
610         int cnt;
611
612         inode->i_dquot[type] = NODQUOT;
613         /* any other quota in use? */
614         for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
615                 if (inode->i_dquot[cnt] != NODQUOT)
616                         goto put_it;
617         }
618         inode->i_flags &= ~S_QUOTA;
619 put_it:
620         if (dquot != NODQUOT) {
621                 if (dqput_blocks(dquot)) {
622                         if (dquot->dq_count != 1)
623                                 printk(KERN_WARNING "VFS: Adding dquot with dq_count %d to dispose list.\n", dquot->dq_count);
624                         list_add(&dquot->dq_free, tofree_head); /* As dquot must have currently users it can't be on the free list... */
625                         return 1;
626                 }
627                 else
628                         dqput(dquot);   /* We have guaranteed we won't block */
629         }
630         return 0;
631 }
632
633 /* Free list of dquots - called from inode.c */
634 void put_dquot_list(struct list_head *tofree_head)
635 {
636         struct list_head *act_head = tofree_head->next;
637         struct dquot *dquot;
638
639         /* So now we have dquots on the list... Just free them */
640         while (act_head != tofree_head) {
641                 dquot = list_entry(act_head, struct dquot, dq_free);
642                 act_head = act_head->next;
643                 list_del(&dquot->dq_free);      /* Remove dquot from the list so we won't have problems... */
644                 INIT_LIST_HEAD(&dquot->dq_free);
645                 dqput(dquot);
646         }
647 }
648
649 static inline void dquot_incr_inodes(struct dquot *dquot, unsigned long number)
650 {
651         dquot->dq_curinodes += number;
652         dquot->dq_flags |= DQ_MOD;
653 }
654
655 static inline void dquot_incr_blocks(struct dquot *dquot, unsigned long number)
656 {
657         dquot->dq_curblocks += number;
658         dquot->dq_flags |= DQ_MOD;
659 }
660
661 static inline void dquot_decr_inodes(struct dquot *dquot, unsigned long number)
662 {
663         if (dquot->dq_curinodes > number)
664                 dquot->dq_curinodes -= number;
665         else
666                 dquot->dq_curinodes = 0;
667         if (dquot->dq_curinodes < dquot->dq_isoftlimit)
668                 dquot->dq_itime = (time_t) 0;
669         dquot->dq_flags &= ~DQ_INODES;
670         dquot->dq_flags |= DQ_MOD;
671 }
672
673 static inline void dquot_decr_blocks(struct dquot *dquot, unsigned long number)
674 {
675         if (dquot->dq_curblocks > number)
676                 dquot->dq_curblocks -= number;
677         else
678                 dquot->dq_curblocks = 0;
679         if (dquot->dq_curblocks < dquot->dq_bsoftlimit)
680                 dquot->dq_btime = (time_t) 0;
681         dquot->dq_flags &= ~DQ_BLKS;
682         dquot->dq_flags |= DQ_MOD;
683 }
684
685 static inline int need_print_warning(struct dquot *dquot, int flag)
686 {
687         switch (dquot->dq_type) {
688                 case USRQUOTA:
689                         return current->fsuid == dquot->dq_id && !(dquot->dq_flags & flag);
690                 case GRPQUOTA:
691                         return in_group_p(dquot->dq_id) && !(dquot->dq_flags & flag);
692         }
693         return 0;
694 }
695
696 /* Values of warnings */
697 #define NOWARN 0
698 #define IHARDWARN 1
699 #define ISOFTLONGWARN 2
700 #define ISOFTWARN 3
701 #define BHARDWARN 4
702 #define BSOFTLONGWARN 5
703 #define BSOFTWARN 6
704
705 /* Print warning to user which exceeded quota */
706 static void print_warning(struct dquot *dquot, const char warntype)
707 {
708         char *msg = NULL;
709         int flag = (warntype == BHARDWARN || warntype == BSOFTLONGWARN) ? DQ_BLKS :
710           ((warntype == IHARDWARN || warntype == ISOFTLONGWARN) ? DQ_INODES : 0);
711
712         if (!need_print_warning(dquot, flag))
713                 return;
714         dquot->dq_flags |= flag;
715         tty_write_message(current->tty, (char *)bdevname(dquot->dq_sb->s_dev));
716         if (warntype == ISOFTWARN || warntype == BSOFTWARN)
717                 tty_write_message(current->tty, ": warning, ");
718         else
719                 tty_write_message(current->tty, ": write failed, ");
720         tty_write_message(current->tty, quotatypes[dquot->dq_type]);
721         switch (warntype) {
722                 case IHARDWARN:
723                         msg = " file limit reached.\n";
724                         break;
725                 case ISOFTLONGWARN:
726                         msg = " file quota exceeded too long.\n";
727                         break;
728                 case ISOFTWARN:
729                         msg = " file quota exceeded.\n";
730                         break;
731                 case BHARDWARN:
732                         msg = " block limit reached.\n";
733                         break;
734                 case BSOFTLONGWARN:
735                         msg = " block quota exceeded too long.\n";
736                         break;
737                 case BSOFTWARN:
738                         msg = " block quota exceeded.\n";
739                         break;
740         }
741         tty_write_message(current->tty, msg);
742 }
743
744 static inline void flush_warnings(struct dquot **dquots, char *warntype)
745 {
746         int i;
747
748         for (i = 0; i < MAXQUOTAS; i++)
749                 if (dquots[i] != NODQUOT && warntype[i] != NOWARN)
750                         print_warning(dquots[i], warntype[i]);
751 }
752
753 static inline char ignore_hardlimit(struct dquot *dquot)
754 {
755         return capable(CAP_SYS_RESOURCE) && !dquot->dq_sb->s_dquot.rsquash[dquot->dq_type];
756 }
757
758 static int check_idq(struct dquot *dquot, ulong inodes, char *warntype)
759 {
760         *warntype = NOWARN;
761         if (inodes <= 0 || dquot->dq_flags & DQ_FAKE)
762                 return QUOTA_OK;
763
764         if (dquot->dq_ihardlimit &&
765            (dquot->dq_curinodes + inodes) > dquot->dq_ihardlimit &&
766             !ignore_hardlimit(dquot)) {
767                 *warntype = IHARDWARN;
768                 return NO_QUOTA;
769         }
770
771         if (dquot->dq_isoftlimit &&
772            (dquot->dq_curinodes + inodes) > dquot->dq_isoftlimit &&
773             dquot->dq_itime && CURRENT_TIME >= dquot->dq_itime &&
774             !ignore_hardlimit(dquot)) {
775                 *warntype = ISOFTLONGWARN;
776                 return NO_QUOTA;
777         }
778
779         if (dquot->dq_isoftlimit &&
780            (dquot->dq_curinodes + inodes) > dquot->dq_isoftlimit &&
781             dquot->dq_itime == 0) {
782                 *warntype = ISOFTWARN;
783                 dquot->dq_itime = CURRENT_TIME + dquot->dq_sb->s_dquot.inode_expire[dquot->dq_type];
784         }
785
786         return QUOTA_OK;
787 }
788
789 static int check_bdq(struct dquot *dquot, ulong blocks, char prealloc, char *warntype)
790 {
791         *warntype = 0;
792         if (blocks <= 0 || dquot->dq_flags & DQ_FAKE)
793                 return QUOTA_OK;
794
795         if (dquot->dq_bhardlimit &&
796            (dquot->dq_curblocks + blocks) > dquot->dq_bhardlimit &&
797             !ignore_hardlimit(dquot)) {
798                 if (!prealloc)
799                         *warntype = BHARDWARN;
800                 return NO_QUOTA;
801         }
802
803         if (dquot->dq_bsoftlimit &&
804            (dquot->dq_curblocks + blocks) > dquot->dq_bsoftlimit &&
805             dquot->dq_btime && CURRENT_TIME >= dquot->dq_btime &&
806             !ignore_hardlimit(dquot)) {
807                 if (!prealloc)
808                         *warntype = BSOFTLONGWARN;
809                 return NO_QUOTA;
810         }
811
812         if (dquot->dq_bsoftlimit &&
813            (dquot->dq_curblocks + blocks) > dquot->dq_bsoftlimit &&
814             dquot->dq_btime == 0) {
815                 if (!prealloc) {
816                         *warntype = BSOFTWARN;
817                         dquot->dq_btime = CURRENT_TIME + dquot->dq_sb->s_dquot.block_expire[dquot->dq_type];
818                 }
819                 else
820                         /*
821                          * We don't allow preallocation to exceed softlimit so exceeding will
822                          * be always printed
823                          */
824                         return NO_QUOTA;
825         }
826
827         return QUOTA_OK;
828 }
829
830 /*
831  * Initialize a dquot-struct with new quota info. This is used by the
832  * system call interface functions.
833  */ 
834 static int set_dqblk(struct super_block *sb, int id, short type, int flags, struct dqblk *dqblk)
835 {
836         struct dquot *dquot;
837         int error = -EFAULT;
838         struct dqblk dq_dqblk;
839
840         if (copy_from_user(&dq_dqblk, dqblk, sizeof(struct dqblk)))
841                 return error;
842
843         if (sb && (dquot = dqget(sb, id, type)) != NODQUOT) {
844                 /* We can't block while changing quota structure... */
845                 if (id > 0 && ((flags & SET_QUOTA) || (flags & SET_QLIMIT))) {
846                         dquot->dq_bhardlimit = dq_dqblk.dqb_bhardlimit;
847                         dquot->dq_bsoftlimit = dq_dqblk.dqb_bsoftlimit;
848                         dquot->dq_ihardlimit = dq_dqblk.dqb_ihardlimit;
849                         dquot->dq_isoftlimit = dq_dqblk.dqb_isoftlimit;
850                 }
851
852                 if ((flags & SET_QUOTA) || (flags & SET_USE)) {
853                         if (dquot->dq_isoftlimit &&
854                             dquot->dq_curinodes < dquot->dq_isoftlimit &&
855                             dq_dqblk.dqb_curinodes >= dquot->dq_isoftlimit)
856                                 dquot->dq_itime = CURRENT_TIME + dquot->dq_sb->s_dquot.inode_expire[type];
857                         dquot->dq_curinodes = dq_dqblk.dqb_curinodes;
858                         if (dquot->dq_curinodes < dquot->dq_isoftlimit)
859                                 dquot->dq_flags &= ~DQ_INODES;
860                         if (dquot->dq_bsoftlimit &&
861                             dquot->dq_curblocks < dquot->dq_bsoftlimit &&
862                             dq_dqblk.dqb_curblocks >= dquot->dq_bsoftlimit)
863                                 dquot->dq_btime = CURRENT_TIME + dquot->dq_sb->s_dquot.block_expire[type];
864                         dquot->dq_curblocks = dq_dqblk.dqb_curblocks;
865                         if (dquot->dq_curblocks < dquot->dq_bsoftlimit)
866                                 dquot->dq_flags &= ~DQ_BLKS;
867                 }
868
869                 if (id == 0) {
870                         dquot->dq_sb->s_dquot.block_expire[type] = dquot->dq_btime = dq_dqblk.dqb_btime;
871                         dquot->dq_sb->s_dquot.inode_expire[type] = dquot->dq_itime = dq_dqblk.dqb_itime;
872                 }
873
874                 if (dq_dqblk.dqb_bhardlimit == 0 && dq_dqblk.dqb_bsoftlimit == 0 &&
875                     dq_dqblk.dqb_ihardlimit == 0 && dq_dqblk.dqb_isoftlimit == 0)
876                         dquot->dq_flags |= DQ_FAKE;
877                 else
878                         dquot->dq_flags &= ~DQ_FAKE;
879
880                 dquot->dq_flags |= DQ_MOD;
881                 dqput(dquot);
882         }
883         return 0;
884 }
885
886 static int get_quota(struct super_block *sb, int id, short type, struct dqblk *dqblk)
887 {
888         struct dquot *dquot;
889         struct dqblk data;
890         int error = -ESRCH;
891
892         if (!sb || !sb_has_quota_enabled(sb, type))
893                 goto out;
894         dquot = dqget(sb, id, type);
895         if (dquot == NODQUOT)
896                 goto out;
897
898         memcpy(&data, &dquot->dq_dqb, sizeof(struct dqblk));        /* We copy data to preserve them from changing */
899         dqput(dquot);
900         error = -EFAULT;
901         if (dqblk && !copy_to_user(dqblk, &data, sizeof(struct dqblk)))
902                 error = 0;
903 out:
904         return error;
905 }
906
907 static int get_stats(caddr_t addr)
908 {
909         int error = -EFAULT;
910         struct dqstats stats;
911
912         dqstats.allocated_dquots = nr_dquots;
913         dqstats.free_dquots = nr_free_dquots;
914
915         /* make a copy, in case we page-fault in user space */
916         memcpy(&stats, &dqstats, sizeof(struct dqstats));
917         if (!copy_to_user(addr, &stats, sizeof(struct dqstats)))
918                 error = 0;
919         return error;
920 }
921
922 static int quota_root_squash(struct super_block *sb, short type, int *addr)
923 {
924         int new_value, error;
925
926         if (!sb)
927                 return(-ENODEV);
928
929         error = -EFAULT;
930         if (!copy_from_user(&new_value, addr, sizeof(int))) {
931                 sb_dqopt(sb)->rsquash[type] = new_value;
932                 error = 0;
933         }
934         return error;
935 }
936
937 #if 0   /* We are not going to support filesystems without i_blocks... */
938 /*
939  * This is a simple algorithm that calculates the size of a file in blocks.
940  * This is only used on filesystems that do not have an i_blocks count.
941  */
942 static u_long isize_to_blocks(loff_t isize, size_t blksize_bits)
943 {
944         u_long blocks;
945         u_long indirect;
946
947         if (!blksize_bits)
948                 blksize_bits = BLOCK_SIZE_BITS;
949         blocks = (isize >> blksize_bits) + ((isize & ~((1 << blksize_bits)-1)) ? 1 : 0);
950         if (blocks > 10) {
951                 indirect = ((blocks - 11) >> 8) + 1; /* single indirect blocks */
952                 if (blocks > (10 + 256)) {
953                         indirect += ((blocks - 267) >> 16) + 1; /* double indirect blocks */
954                         if (blocks > (10 + 256 + (256 << 8)))
955                                 indirect++; /* triple indirect blocks */
956                 }
957                 blocks += indirect;
958         }
959         return blocks;
960 }
961 #endif
962
963 /*
964  * Externally referenced functions through dquot_operations in inode.
965  *
966  * Note: this is a blocking operation.
967  */
968 void dquot_initialize(struct inode *inode, short type)
969 {
970         struct dquot *dquot[MAXQUOTAS];
971         unsigned int id = 0;
972         short cnt;
973
974         if (IS_NOQUOTA(inode))
975                 return;
976         /* Build list of quotas to initialize... We can block here */
977         for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
978                 dquot[cnt] = NODQUOT;
979                 if (type != -1 && cnt != type)
980                         continue;
981                 if (!sb_has_quota_enabled(inode->i_sb, cnt))
982                         continue;
983                 if (inode->i_dquot[cnt] == NODQUOT) {
984                         switch (cnt) {
985                                 case USRQUOTA:
986                                         id = inode->i_uid;
987                                         break;
988                                 case GRPQUOTA:
989                                         id = inode->i_gid;
990                                         break;
991                         }
992                         dquot[cnt] = dqget(inode->i_sb, id, cnt);
993                 }
994         }
995         /* NOBLOCK START: Here we shouldn't block */
996         for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
997                 if (dquot[cnt] == NODQUOT || !sb_has_quota_enabled(inode->i_sb, cnt) || inode->i_dquot[cnt] != NODQUOT)
998                         continue;
999                 inode->i_dquot[cnt] = dquot[cnt];
1000                 dquot[cnt] = NODQUOT;
1001                 inode->i_flags |= S_QUOTA;
1002         }
1003         /* NOBLOCK END */
1004         /* Put quotas which we didn't use */
1005         for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1006                 if (dquot[cnt] != NODQUOT)
1007                         dqput(dquot[cnt]);
1008 }
1009
1010 /*
1011  * Release all quota for the specified inode.
1012  *
1013  * Note: this is a blocking operation.
1014  */
1015 void dquot_drop(struct inode *inode)
1016 {
1017         struct dquot *dquot;
1018         short cnt;
1019
1020         inode->i_flags &= ~S_QUOTA;
1021         for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1022                 if (inode->i_dquot[cnt] == NODQUOT)
1023                         continue;
1024                 dquot = inode->i_dquot[cnt];
1025                 inode->i_dquot[cnt] = NODQUOT;
1026                 dqput(dquot);
1027         }
1028 }
1029
1030 /*
1031  * This operation can block, but only after everything is updated
1032  */
1033 int dquot_alloc_block(struct inode *inode, unsigned long number, char warn)
1034 {
1035         int cnt, ret = NO_QUOTA;
1036         struct dquot *dquot[MAXQUOTAS];
1037         char warntype[MAXQUOTAS];
1038
1039         for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1040                 dquot[cnt] = NODQUOT;
1041                 warntype[cnt] = NOWARN;
1042         }
1043         /* NOBLOCK Start */
1044         for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1045                 dquot[cnt] = dqduplicate(inode->i_dquot[cnt]);
1046                 if (dquot[cnt] == NODQUOT)
1047                         continue;
1048                 if (check_bdq(dquot[cnt], number, warn, warntype+cnt) == NO_QUOTA)
1049                         goto warn_put_all;
1050         }
1051         for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1052                 if (dquot[cnt] == NODQUOT)
1053                         continue;
1054                 dquot_incr_blocks(dquot[cnt], number);
1055         }
1056         inode->i_blocks += number << (BLOCK_SIZE_BITS - 9);
1057         /* NOBLOCK End */
1058         ret = QUOTA_OK;
1059 warn_put_all:
1060         flush_warnings(dquot, warntype);
1061         for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1062                 if (dquot[cnt] != NODQUOT)
1063                         dqput(dquot[cnt]);
1064         return ret;
1065 }
1066
1067 /*
1068  * This operation can block, but only after everything is updated
1069  */
1070 int dquot_alloc_inode(const struct inode *inode, unsigned long number)
1071 {
1072         int cnt, ret = NO_QUOTA;
1073         struct dquot *dquot[MAXQUOTAS];
1074         char warntype[MAXQUOTAS];
1075
1076         for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1077                 dquot[cnt] = NODQUOT;
1078                 warntype[cnt] = NOWARN;
1079         }
1080         /* NOBLOCK Start */
1081         for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1082                 dquot[cnt] = dqduplicate(inode -> i_dquot[cnt]);
1083                 if (dquot[cnt] == NODQUOT)
1084                         continue;
1085                 if (check_idq(dquot[cnt], number, warntype+cnt) == NO_QUOTA)
1086                         goto warn_put_all;
1087         }
1088
1089         for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1090                 if (dquot[cnt] == NODQUOT)
1091                         continue;
1092                 dquot_incr_inodes(dquot[cnt], number);
1093         }
1094         /* NOBLOCK End */
1095         ret = QUOTA_OK;
1096 warn_put_all:
1097         flush_warnings(dquot, warntype);
1098         for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1099                 if (dquot[cnt] != NODQUOT)
1100                         dqput(dquot[cnt]);
1101         return ret;
1102 }
1103
1104 /*
1105  * This is a non-blocking operation.
1106  */
1107 void dquot_free_block(struct inode *inode, unsigned long number)
1108 {
1109         unsigned short cnt;
1110         struct dquot *dquot;
1111
1112         /* NOBLOCK Start */
1113         for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1114                 dquot = dqduplicate(inode->i_dquot[cnt]);
1115                 if (dquot == NODQUOT)
1116                         continue;
1117                 dquot_decr_blocks(dquot, number);
1118                 dqput(dquot);
1119         }
1120         inode->i_blocks -= number << (BLOCK_SIZE_BITS - 9);
1121         /* NOBLOCK End */
1122 }
1123
1124 /*
1125  * This is a non-blocking operation.
1126  */
1127 void dquot_free_inode(const struct inode *inode, unsigned long number)
1128 {
1129         unsigned short cnt;
1130         struct dquot *dquot;
1131
1132         /* NOBLOCK Start */
1133         for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1134                 dquot = dqduplicate(inode->i_dquot[cnt]);
1135                 if (dquot == NODQUOT)
1136                         continue;
1137                 dquot_decr_inodes(dquot, number);
1138                 dqput(dquot);
1139         }
1140         /* NOBLOCK End */
1141 }
1142
1143 /*
1144  * Transfer the number of inode and blocks from one diskquota to an other.
1145  *
1146  * This operation can block, but only after everything is updated
1147  */
1148 int dquot_transfer(struct inode *inode, struct iattr *iattr)
1149 {
1150         unsigned long blocks;
1151         struct dquot *transfer_from[MAXQUOTAS];
1152         struct dquot *transfer_to[MAXQUOTAS];
1153         int cnt, ret = NO_QUOTA, chuid = (iattr->ia_valid & ATTR_UID) && inode->i_uid != iattr->ia_uid,
1154             chgid = (iattr->ia_valid & ATTR_GID) && inode->i_gid != iattr->ia_gid;
1155         char warntype[MAXQUOTAS];
1156
1157         /* Clear the arrays */
1158         for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1159                 transfer_to[cnt] = transfer_from[cnt] = NODQUOT;
1160                 warntype[cnt] = NOWARN;
1161         }
1162         /* First build the transfer_to list - here we can block on reading of dquots... */
1163         for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1164                 if (!sb_has_quota_enabled(inode->i_sb, cnt))
1165                         continue;
1166                 switch (cnt) {
1167                         case USRQUOTA:
1168                                 if (!chuid)
1169                                         continue;
1170                                 transfer_to[cnt] = dqget(inode->i_sb, iattr->ia_uid, cnt);
1171                                 break;
1172                         case GRPQUOTA:
1173                                 if (!chgid)
1174                                         continue;
1175                                 transfer_to[cnt] = dqget(inode->i_sb, iattr->ia_gid, cnt);
1176                                 break;
1177                 }
1178         }
1179         /* NOBLOCK START: From now on we shouldn't block */
1180         blocks = (inode->i_blocks >> 1);
1181         /* Build the transfer_from list and check the limits */
1182         for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1183                 /* The second test can fail when quotaoff is in progress... */
1184                 if (transfer_to[cnt] == NODQUOT || !sb_has_quota_enabled(inode->i_sb, cnt))
1185                         continue;
1186                 transfer_from[cnt] = dqduplicate(inode->i_dquot[cnt]);
1187                 if (transfer_from[cnt] == NODQUOT)      /* Can happen on quotafiles (quota isn't initialized on them)... */
1188                         continue;
1189                 if (check_idq(transfer_to[cnt], 1, warntype+cnt) == NO_QUOTA ||
1190                     check_bdq(transfer_to[cnt], blocks, 0, warntype+cnt) == NO_QUOTA)
1191                         goto warn_put_all;
1192         }
1193
1194         /*
1195          * Finally perform the needed transfer from transfer_from to transfer_to
1196          */
1197         for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1198                 /*
1199                  * Skip changes for same uid or gid or for non-existing quota-type.
1200                  */
1201                 if (transfer_from[cnt] == NODQUOT || transfer_to[cnt] == NODQUOT)
1202                         continue;
1203
1204                 dquot_decr_inodes(transfer_from[cnt], 1);
1205                 dquot_decr_blocks(transfer_from[cnt], blocks);
1206
1207                 dquot_incr_inodes(transfer_to[cnt], 1);
1208                 dquot_incr_blocks(transfer_to[cnt], blocks);
1209
1210                 if (inode->i_dquot[cnt] == NODQUOT)
1211                         BUG();
1212                 inode->i_dquot[cnt] = transfer_to[cnt];
1213                 /*
1214                  * We've got to release transfer_from[] twice - once for dquot_transfer() and
1215                  * once for inode. We don't want to release transfer_to[] as it's now placed in inode
1216                  */
1217                 transfer_to[cnt] = transfer_from[cnt];
1218         }
1219         /* NOBLOCK END. From now on we can block as we wish */
1220         ret = QUOTA_OK;
1221 warn_put_all:
1222         flush_warnings(transfer_to, warntype);
1223         for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1224                 if (transfer_to[cnt] != NODQUOT)
1225                         dqput(transfer_to[cnt]);
1226                 if (transfer_from[cnt] != NODQUOT)
1227                         dqput(transfer_from[cnt]);
1228         }
1229         return ret;
1230 }
1231
1232 static int __init dquot_init(void)
1233 {
1234         int i;
1235
1236         for (i = 0; i < NR_DQHASH; i++)
1237                 INIT_LIST_HEAD(dquot_hash + i);
1238         printk(KERN_NOTICE "VFS: Diskquotas version %s initialized\n", __DQUOT_VERSION__);
1239         return 0;
1240 }
1241 __initcall(dquot_init);
1242
1243 /*
1244  * Definitions of diskquota operations.
1245  */
1246 struct dquot_operations dquot_operations = {
1247         dquot_initialize,               /* mandatory */
1248         dquot_drop,                     /* mandatory */
1249         dquot_alloc_block,
1250         dquot_alloc_inode,
1251         dquot_free_block,
1252         dquot_free_inode,
1253         dquot_transfer
1254 };
1255
1256 static inline void set_enable_flags(struct quota_mount_options *dqopt, short type)
1257 {
1258         switch (type) {
1259                 case USRQUOTA:
1260                         dqopt->flags |= DQUOT_USR_ENABLED;
1261                         break;
1262                 case GRPQUOTA:
1263                         dqopt->flags |= DQUOT_GRP_ENABLED;
1264                         break;
1265         }
1266 }
1267
1268 static inline void reset_enable_flags(struct quota_mount_options *dqopt, short type)
1269 {
1270         switch (type) {
1271                 case USRQUOTA:
1272                         dqopt->flags &= ~DQUOT_USR_ENABLED;
1273                         break;
1274                 case GRPQUOTA:
1275                         dqopt->flags &= ~DQUOT_GRP_ENABLED;
1276                         break;
1277         }
1278 }
1279
1280 /* Function in inode.c - remove pointers to dquots in icache */
1281 extern void remove_dquot_ref(struct super_block *, short);
1282
1283 /*
1284  * Turn quota off on a device. type == -1 ==> quotaoff for all types (umount)
1285  */
1286 int quota_off(struct super_block *sb, short type)
1287 {
1288         struct file *filp;
1289         short cnt;
1290         struct quota_mount_options *dqopt = sb_dqopt(sb);
1291
1292         if (!sb)
1293                 goto out;
1294
1295         /* We need to serialize quota_off() for device */
1296         down(&dqopt->dqoff_sem);
1297         for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1298                 if (type != -1 && cnt != type)
1299                         continue;
1300                 if (!is_enabled(dqopt, cnt))
1301                         continue;
1302                 reset_enable_flags(dqopt, cnt);
1303
1304                 /* Note: these are blocking operations */
1305                 remove_dquot_ref(sb, cnt);
1306                 invalidate_dquots(sb, cnt);
1307
1308                 filp = dqopt->files[cnt];
1309                 dqopt->files[cnt] = (struct file *)NULL;
1310                 dqopt->inode_expire[cnt] = 0;
1311                 dqopt->block_expire[cnt] = 0;
1312                 fput(filp);
1313         }       
1314         up(&dqopt->dqoff_sem);
1315 out:
1316         return 0;
1317 }
1318
1319 static inline int check_quotafile_size(loff_t size)
1320 {
1321         ulong blocks = size >> BLOCK_SIZE_BITS;
1322         size_t off = size & (BLOCK_SIZE - 1);
1323
1324         return !(((blocks % sizeof(struct dqblk)) * BLOCK_SIZE + off % sizeof(struct dqblk)) % sizeof(struct dqblk));
1325 }
1326
1327 static int quota_on(struct super_block *sb, short type, char *path)
1328 {
1329         struct file *f;
1330         struct inode *inode;
1331         struct dquot *dquot;
1332         struct quota_mount_options *dqopt = sb_dqopt(sb);
1333         char *tmp;
1334         int error;
1335
1336         if (is_enabled(dqopt, type))
1337                 return -EBUSY;
1338
1339         down(&dqopt->dqoff_sem);
1340         tmp = getname(path);
1341         error = PTR_ERR(tmp);
1342         if (IS_ERR(tmp))
1343                 goto out_lock;
1344
1345         f = filp_open(tmp, O_RDWR, 0600);
1346         putname(tmp);
1347
1348         error = PTR_ERR(f);
1349         if (IS_ERR(f))
1350                 goto out_lock;
1351         error = -EIO;
1352         if (!f->f_op || !f->f_op->read || !f->f_op->write)
1353                 goto out_f;
1354         inode = f->f_dentry->d_inode;
1355         error = -EACCES;
1356         if (!S_ISREG(inode->i_mode))
1357                 goto out_f;
1358         error = -EINVAL;
1359         if (inode->i_size == 0 || !check_quotafile_size(inode->i_size))
1360                 goto out_f;
1361         /* We don't want quota on quota files */
1362         dquot_drop(inode);
1363         inode->i_flags |= S_NOQUOTA;
1364
1365         dqopt->files[type] = f;
1366         set_enable_flags(dqopt, type);
1367
1368         dquot = dqget(sb, 0, type);
1369         dqopt->inode_expire[type] = (dquot != NODQUOT) ? dquot->dq_itime : MAX_IQ_TIME;
1370         dqopt->block_expire[type] = (dquot != NODQUOT) ? dquot->dq_btime : MAX_DQ_TIME;
1371         dqput(dquot);
1372
1373         sb->dq_op = &dquot_operations;
1374         add_dquot_ref(sb, type);
1375
1376         up(&dqopt->dqoff_sem);
1377         return 0;
1378
1379 out_f:
1380         filp_close(f, NULL);
1381 out_lock:
1382         up(&dqopt->dqoff_sem);
1383
1384         return error; 
1385 }
1386
1387 /*
1388  * This is the system call interface. This communicates with
1389  * the user-level programs. Currently this only supports diskquota
1390  * calls. Maybe we need to add the process quotas etc. in the future,
1391  * but we probably should use rlimits for that.
1392  */
1393 asmlinkage long sys_quotactl(int cmd, const char *special, int id, caddr_t addr)
1394 {
1395         int cmds = 0, type = 0, flags = 0;
1396         kdev_t dev;
1397         struct super_block *sb = NULL;
1398         int ret = -EINVAL;
1399
1400         lock_kernel();
1401         cmds = cmd >> SUBCMDSHIFT;
1402         type = cmd & SUBCMDMASK;
1403
1404         if ((u_int) type >= MAXQUOTAS)
1405                 goto out;
1406         if (id & ~0xFFFF)
1407                 goto out;
1408
1409         ret = -EPERM;
1410         switch (cmds) {
1411                 case Q_SYNC:
1412                 case Q_GETSTATS:
1413                         break;
1414                 case Q_GETQUOTA:
1415                         if (((type == USRQUOTA && current->euid != id) ||
1416                              (type == GRPQUOTA && !in_egroup_p(id))) &&
1417                             !capable(CAP_SYS_ADMIN))
1418                                 goto out;
1419                         break;
1420                 default:
1421                         if (!capable(CAP_SYS_ADMIN))
1422                                 goto out;
1423         }
1424
1425         ret = -EINVAL;
1426         dev = NODEV;
1427         if (special != NULL || (cmds != Q_SYNC && cmds != Q_GETSTATS)) {
1428                 mode_t mode;
1429                 struct nameidata nd;
1430
1431                 ret = user_path_walk(special, &nd);
1432                 if (ret)
1433                         goto out;
1434
1435                 dev = nd.dentry->d_inode->i_rdev;
1436                 mode = nd.dentry->d_inode->i_mode;
1437                 path_release(&nd);
1438
1439                 ret = -ENOTBLK;
1440                 if (!S_ISBLK(mode))
1441                         goto out;
1442                 ret = -ENODEV;
1443                 sb = get_super(dev);
1444                 if (!sb)
1445                         goto out;
1446         }
1447
1448         ret = -EINVAL;
1449         switch (cmds) {
1450                 case Q_QUOTAON:
1451                         ret = quota_on(sb, type, (char *) addr);
1452                         goto out;
1453                 case Q_QUOTAOFF:
1454                         ret = quota_off(sb, type);
1455                         goto out;
1456                 case Q_GETQUOTA:
1457                         ret = get_quota(sb, id, type, (struct dqblk *) addr);
1458                         goto out;
1459                 case Q_SETQUOTA:
1460                         flags |= SET_QUOTA;
1461                         break;
1462                 case Q_SETUSE:
1463                         flags |= SET_USE;
1464                         break;
1465                 case Q_SETQLIM:
1466                         flags |= SET_QLIMIT;
1467                         break;
1468                 case Q_SYNC:
1469                         ret = sync_dquots(dev, type);
1470                         goto out;
1471                 case Q_GETSTATS:
1472                         ret = get_stats(addr);
1473                         goto out;
1474                 case Q_RSQUASH:
1475                         ret = quota_root_squash(sb, type, (int *) addr);
1476                         goto out;
1477                 default:
1478                         goto out;
1479         }
1480
1481         ret = -NODEV;
1482         if (sb && sb_has_quota_enabled(sb, type))
1483                 ret = set_dqblk(sb, id, type, flags, (struct dqblk *) addr);
1484 out:
1485         if (sb)
1486                 drop_super(sb);
1487         unlock_kernel();
1488         return ret;
1489 }