Linux 2.4.0-test9pre8
[davej-history.git] / fs / dquot.c
blobef1f15016f3622f3bd2f05c98970a48885e2f6b7
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.
15 * Version: $Id: dquot.c,v 6.3 1996/11/17 18:35:34 mvw Exp mvw $
17 * Author: Marco van Wieringen <mvw@planets.elm.net>
19 * Fixes: Dmitry Gorodchanin <pgmdsg@ibi.com>, 11 Feb 96
21 * Revised list management to avoid races
22 * -- Bill Hawes, <whawes@star.net>, 9/98
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
37 * (C) Copyright 1994 - 1997 Marco van Wieringen
40 #include <linux/errno.h>
41 #include <linux/kernel.h>
42 #include <linux/sched.h>
44 #include <linux/types.h>
45 #include <linux/string.h>
46 #include <linux/fcntl.h>
47 #include <linux/stat.h>
48 #include <linux/tty.h>
49 #include <linux/file.h>
50 #include <linux/malloc.h>
51 #include <linux/mount.h>
52 #include <linux/smp.h>
53 #include <linux/smp_lock.h>
54 #include <linux/init.h>
55 #include <linux/slab.h>
57 #include <asm/uaccess.h>
59 #define __DQUOT_VERSION__ "dquot_6.4.0"
61 int nr_dquots, nr_free_dquots;
62 int max_dquots = NR_DQUOTS;
64 static char quotamessage[MAX_QUOTA_MESSAGE];
65 static char *quotatypes[] = INITQFNAMES;
67 static inline struct quota_mount_options *sb_dqopt(struct super_block *sb)
69 return &sb->s_dquot;
73 * Dquot List Management:
74 * The quota code uses three lists for dquot management: the inuse_list,
75 * free_dquots, and dquot_hash[] array. A single dquot structure may be
76 * on all three lists, depending on its current state.
78 * All dquots are placed on the inuse_list when first created, and this
79 * list is used for the sync and invalidate operations, which must look
80 * at every dquot.
82 * Unused dquots (dq_count == 0) are added to the free_dquots list when
83 * freed, and this list is searched whenever we need an available dquot.
84 * Dquots are removed from the list as soon as they are used again, and
85 * nr_free_dquots gives the number of dquots on the list.
87 * Dquots with a specific identity (device, type and id) are placed on
88 * one of the dquot_hash[] hash chains. The provides an efficient search
89 * mechanism to lcoate a specific dquot.
92 static struct dquot *inuse_list;
93 static LIST_HEAD(free_dquots);
94 static struct dquot *dquot_hash[NR_DQHASH];
95 static int dquot_updating[NR_DQHASH];
97 static struct dqstats dqstats;
98 static DECLARE_WAIT_QUEUE_HEAD(dquot_wait);
99 static DECLARE_WAIT_QUEUE_HEAD(update_wait);
101 static void dqput(struct dquot *);
102 static struct dquot *dqduplicate(struct dquot *);
104 static inline char is_enabled(struct quota_mount_options *dqopt, short type)
106 switch (type) {
107 case USRQUOTA:
108 return((dqopt->flags & DQUOT_USR_ENABLED) != 0);
109 case GRPQUOTA:
110 return((dqopt->flags & DQUOT_GRP_ENABLED) != 0);
112 return(0);
115 static inline char sb_has_quota_enabled(struct super_block *sb, short type)
117 return is_enabled(sb_dqopt(sb), type);
120 static inline int const hashfn(kdev_t dev, unsigned int id, short type)
122 return((HASHDEV(dev) ^ id) * (MAXQUOTAS - type)) % NR_DQHASH;
125 static inline void insert_dquot_hash(struct dquot *dquot)
127 struct dquot **htable;
129 htable = &dquot_hash[hashfn(dquot->dq_dev, dquot->dq_id, dquot->dq_type)];
130 if ((dquot->dq_hash_next = *htable) != NULL)
131 (*htable)->dq_hash_pprev = &dquot->dq_hash_next;
132 *htable = dquot;
133 dquot->dq_hash_pprev = htable;
136 static inline void hash_dquot(struct dquot *dquot)
138 insert_dquot_hash(dquot);
141 static inline void unhash_dquot(struct dquot *dquot)
143 if (dquot->dq_hash_pprev) {
144 if (dquot->dq_hash_next)
145 dquot->dq_hash_next->dq_hash_pprev = dquot->dq_hash_pprev;
146 *(dquot->dq_hash_pprev) = dquot->dq_hash_next;
147 dquot->dq_hash_pprev = NULL;
151 static inline struct dquot *find_dquot(unsigned int hashent, kdev_t dev, unsigned int id, short type)
153 struct dquot *dquot;
155 for (dquot = dquot_hash[hashent]; dquot; dquot = dquot->dq_hash_next)
156 if (dquot->dq_dev == dev && dquot->dq_id == id && dquot->dq_type == type)
157 break;
158 return dquot;
161 /* Add a dquot to the head of the free list */
162 static inline void put_dquot_head(struct dquot *dquot)
164 list_add(&dquot->dq_free, &free_dquots);
165 nr_free_dquots++;
168 /* Add a dquot to the tail of the free list */
169 static inline void put_dquot_last(struct dquot *dquot)
171 list_add(&dquot->dq_free, free_dquots.prev);
172 nr_free_dquots++;
175 static inline void remove_free_dquot(struct dquot *dquot)
177 /* sanity check */
178 if (list_empty(&dquot->dq_free)) {
179 printk("remove_free_dquot: dquot not on the free list??\n");
180 return; /* J.K. Just don't do anything */
182 list_del(&dquot->dq_free);
183 INIT_LIST_HEAD(&dquot->dq_free);
184 nr_free_dquots--;
187 static inline void put_inuse(struct dquot *dquot)
189 if ((dquot->dq_next = inuse_list) != NULL)
190 inuse_list->dq_pprev = &dquot->dq_next;
191 inuse_list = dquot;
192 dquot->dq_pprev = &inuse_list;
195 #if 0 /* currently not needed */
196 static inline void remove_inuse(struct dquot *dquot)
198 if (dquot->dq_pprev) {
199 if (dquot->dq_next)
200 dquot->dq_next->dq_pprev = dquot->dq_pprev;
201 *dquot->dq_pprev = dquot->dq_next;
202 dquot->dq_pprev = NULL;
205 #endif
207 static void __wait_on_dquot(struct dquot *dquot)
209 DECLARE_WAITQUEUE(wait, current);
211 add_wait_queue(&dquot->dq_wait, &wait);
212 repeat:
213 set_current_state(TASK_UNINTERRUPTIBLE);
214 if (dquot->dq_flags & DQ_LOCKED) {
215 schedule();
216 goto repeat;
218 remove_wait_queue(&dquot->dq_wait, &wait);
219 current->state = TASK_RUNNING;
222 static inline void wait_on_dquot(struct dquot *dquot)
224 if (dquot->dq_flags & DQ_LOCKED)
225 __wait_on_dquot(dquot);
228 static inline void lock_dquot(struct dquot *dquot)
230 wait_on_dquot(dquot);
231 dquot->dq_flags |= DQ_LOCKED;
234 static inline void unlock_dquot(struct dquot *dquot)
236 dquot->dq_flags &= ~DQ_LOCKED;
237 wake_up(&dquot->dq_wait);
241 * We don't have to be afraid of deadlocks as we never have quotas on quota files...
243 static void write_dquot(struct dquot *dquot)
245 short type = dquot->dq_type;
246 struct file *filp;
247 mm_segment_t fs;
248 loff_t offset;
249 ssize_t ret;
250 struct semaphore *sem = &dquot->dq_sb->s_dquot.dqio_sem;
252 lock_dquot(dquot);
253 if (!dquot->dq_sb) { /* Invalidated quota? */
254 unlock_dquot(dquot);
255 return;
257 down(sem);
258 filp = dquot->dq_sb->s_dquot.files[type];
259 offset = dqoff(dquot->dq_id);
260 fs = get_fs();
261 set_fs(KERNEL_DS);
264 * Note: clear the DQ_MOD flag unconditionally,
265 * so we don't loop forever on failure.
267 dquot->dq_flags &= ~DQ_MOD;
268 ret = 0;
269 if (filp)
270 ret = filp->f_op->write(filp, (char *)&dquot->dq_dqb,
271 sizeof(struct dqblk), &offset);
272 if (ret != sizeof(struct dqblk))
273 printk(KERN_WARNING "VFS: dquota write failed on dev %s\n",
274 kdevname(dquot->dq_dev));
276 set_fs(fs);
277 up(sem);
278 unlock_dquot(dquot);
280 dqstats.writes++;
283 static void read_dquot(struct dquot *dquot)
285 short type = dquot->dq_type;
286 struct file *filp;
287 mm_segment_t fs;
288 loff_t offset;
290 filp = dquot->dq_sb->s_dquot.files[type];
291 if (filp == (struct file *)NULL)
292 return;
294 lock_dquot(dquot);
295 if (!dquot->dq_sb) /* Invalidated quota? */
296 goto out_lock;
297 /* Now we are sure filp is valid - the dquot isn't invalidated */
298 down(&dquot->dq_sb->s_dquot.dqio_sem);
299 offset = dqoff(dquot->dq_id);
300 fs = get_fs();
301 set_fs(KERNEL_DS);
302 filp->f_op->read(filp, (char *)&dquot->dq_dqb, sizeof(struct dqblk), &offset);
303 up(&dquot->dq_sb->s_dquot.dqio_sem);
304 set_fs(fs);
306 if (dquot->dq_bhardlimit == 0 && dquot->dq_bsoftlimit == 0 &&
307 dquot->dq_ihardlimit == 0 && dquot->dq_isoftlimit == 0)
308 dquot->dq_flags |= DQ_FAKE;
309 dqstats.reads++;
310 out_lock:
311 unlock_dquot(dquot);
315 * Unhash and selectively clear the dquot structure,
316 * but preserve the use count, list pointers, and
317 * wait queue.
319 void clear_dquot(struct dquot *dquot)
321 /* unhash it first */
322 unhash_dquot(dquot);
323 dquot->dq_sb = NULL;
324 dquot->dq_flags = 0;
325 dquot->dq_referenced = 0;
326 memset(&dquot->dq_dqb, 0, sizeof(struct dqblk));
329 void invalidate_dquots(kdev_t dev, short type)
331 struct dquot *dquot, *next;
332 int need_restart;
334 restart:
335 next = inuse_list; /* Here it is better. Otherwise the restart doesn't have any sense ;-) */
336 need_restart = 0;
337 while ((dquot = next) != NULL) {
338 next = dquot->dq_next;
339 if (dquot->dq_dev != dev)
340 continue;
341 if (dquot->dq_type != type)
342 continue;
343 if (!dquot->dq_sb) /* Already invalidated entry? */
344 continue;
345 if (dquot->dq_flags & DQ_LOCKED) {
346 __wait_on_dquot(dquot);
348 /* Set the flag for another pass. */
349 need_restart = 1;
351 * Make sure it's still the same dquot.
353 if (dquot->dq_dev != dev)
354 continue;
355 if (dquot->dq_type != type)
356 continue;
357 if (!dquot->dq_sb)
358 continue;
361 * Because inodes needn't to be the only holders of dquot
362 * the quota needn't to be written to disk. So we write it
363 * ourselves before discarding the data just for sure...
365 if (dquot->dq_flags & DQ_MOD && dquot->dq_sb)
367 write_dquot(dquot);
368 need_restart = 1; /* We slept on IO */
370 clear_dquot(dquot);
373 * If anything blocked, restart the operation
374 * to ensure we don't miss any dquots.
376 if (need_restart)
377 goto restart;
380 int sync_dquots(kdev_t dev, short type)
382 struct dquot *dquot, *next, *ddquot;
383 int need_restart;
385 restart:
386 next = inuse_list;
387 need_restart = 0;
388 while ((dquot = next) != NULL) {
389 next = dquot->dq_next;
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_LOCKED | DQ_MOD)))
397 continue;
399 if ((ddquot = dqduplicate(dquot)) == NODQUOT)
400 continue;
401 if (ddquot->dq_flags & DQ_MOD)
402 write_dquot(ddquot);
403 dqput(ddquot);
404 /* Set the flag for another pass. */
405 need_restart = 1;
408 * If anything blocked, restart the operation
409 * to ensure we don't miss any dquots.
411 if (need_restart)
412 goto restart;
414 dqstats.syncs++;
415 return(0);
418 /* NOTE: If you change this function please check whether dqput_blocks() works right... */
419 static void dqput(struct dquot *dquot)
421 if (!dquot)
422 return;
423 if (!dquot->dq_count) {
424 printk("VFS: dqput: trying to free free dquot\n");
425 printk("VFS: device %s, dquot of %s %d\n",
426 kdevname(dquot->dq_dev), quotatypes[dquot->dq_type],
427 dquot->dq_id);
428 return;
432 * If the dq_sb pointer isn't initialized this entry needs no
433 * checking and doesn't need to be written. It's just an empty
434 * dquot that is put back on to the freelist.
436 if (dquot->dq_sb)
437 dqstats.drops++;
438 we_slept:
439 if (dquot->dq_count > 1) {
440 /* We have more than one user... We can simply decrement use count */
441 dquot->dq_count--;
442 return;
444 if (dquot->dq_flags & DQ_LOCKED) {
445 printk(KERN_ERR "VFS: Locked quota to be put on the free list.\n");
446 dquot->dq_flags &= ~DQ_LOCKED;
448 if (dquot->dq_sb && dquot->dq_flags & DQ_MOD) {
449 write_dquot(dquot);
450 goto we_slept;
453 /* sanity check */
454 if (!list_empty(&dquot->dq_free)) {
455 printk(KERN_ERR "dqput: dquot already on free list??\n");
456 dquot->dq_count--; /* J.K. Just decrementing use count seems safer... */
457 return;
459 dquot->dq_count--;
460 dquot->dq_flags &= ~DQ_MOD; /* Modified flag has no sense on free list */
461 /* Place at end of LRU free queue */
462 put_dquot_last(dquot);
463 wake_up(&dquot_wait);
466 static int grow_dquots(void)
468 struct dquot *dquot;
469 int cnt = 0;
471 while (cnt < 32) {
472 dquot = kmem_cache_alloc(dquot_cachep, SLAB_KERNEL);
473 if(!dquot)
474 return cnt;
476 nr_dquots++;
477 memset((caddr_t)dquot, 0, sizeof(struct dquot));
478 init_waitqueue_head(&dquot->dq_wait);
479 /* all dquots go on the inuse_list */
480 put_inuse(dquot);
481 put_dquot_head(dquot);
482 cnt++;
484 return cnt;
487 static struct dquot *find_best_candidate_weighted(void)
489 struct list_head *tmp = &free_dquots;
490 struct dquot *dquot, *best = NULL;
491 unsigned long myscore, bestscore = ~0U;
492 int limit = (nr_free_dquots > 128) ? nr_free_dquots >> 2 : 32;
494 while ((tmp = tmp->next) != &free_dquots && --limit) {
495 dquot = list_entry(tmp, struct dquot, dq_free);
496 /* This should never happen... */
497 if (dquot->dq_flags & (DQ_LOCKED | DQ_MOD))
498 continue;
499 myscore = dquot->dq_referenced;
500 if (myscore < bestscore) {
501 bestscore = myscore;
502 best = dquot;
505 return best;
508 static inline struct dquot *find_best_free(void)
510 struct list_head *tmp = &free_dquots;
511 struct dquot *dquot;
512 int limit = (nr_free_dquots > 1024) ? nr_free_dquots >> 5 : 32;
514 while ((tmp = tmp->next) != &free_dquots && --limit) {
515 dquot = list_entry(tmp, struct dquot, dq_free);
516 if (dquot->dq_referenced == 0)
517 return dquot;
519 return NULL;
522 struct dquot *get_empty_dquot(void)
524 struct dquot *dquot;
525 int shrink = 1; /* Number of times we should try to shrink dcache and icache */
527 repeat:
528 dquot = find_best_free();
529 if (!dquot)
530 goto pressure;
531 got_it:
532 /* Sanity checks */
533 if (dquot->dq_flags & DQ_LOCKED)
534 printk(KERN_ERR "VFS: Locked dquot on the free list\n");
535 if (dquot->dq_count != 0)
536 printk(KERN_ERR "VFS: free dquot count=%d\n", dquot->dq_count);
538 remove_free_dquot(dquot);
539 dquot->dq_count = 1;
540 /* unhash and selectively clear the structure */
541 clear_dquot(dquot);
542 return dquot;
544 pressure:
545 if (nr_dquots < max_dquots)
546 if (grow_dquots())
547 goto repeat;
549 dquot = find_best_candidate_weighted();
550 if (dquot)
551 goto got_it;
553 * Try pruning the dcache to free up some dquots ...
555 if (shrink) {
556 printk(KERN_DEBUG "get_empty_dquot: pruning dcache and icache\n");
557 prune_dcache(128);
558 prune_icache(128);
559 shrink--;
560 goto repeat;
563 printk("VFS: No free dquots, contact mvw@planets.elm.net\n");
564 sleep_on(&dquot_wait);
565 goto repeat;
568 static struct dquot *dqget(struct super_block *sb, unsigned int id, short type)
570 unsigned int hashent = hashfn(sb->s_dev, id, type);
571 struct dquot *dquot, *empty = NULL;
572 struct quota_mount_options *dqopt = sb_dqopt(sb);
574 if (!is_enabled(dqopt, type))
575 return(NODQUOT);
577 we_slept:
578 if ((dquot = find_dquot(hashent, sb->s_dev, id, type)) == NULL) {
579 if (empty == NULL) {
580 dquot_updating[hashent]++;
581 empty = get_empty_dquot();
582 if (!--dquot_updating[hashent])
583 wake_up(&update_wait);
584 goto we_slept;
586 dquot = empty;
587 dquot->dq_id = id;
588 dquot->dq_type = type;
589 dquot->dq_dev = sb->s_dev;
590 dquot->dq_sb = sb;
591 /* hash it first so it can be found */
592 hash_dquot(dquot);
593 read_dquot(dquot);
594 } else {
595 if (!dquot->dq_count++) {
596 remove_free_dquot(dquot);
597 } else
598 dqstats.cache_hits++;
599 wait_on_dquot(dquot);
600 if (empty)
601 dqput(empty);
604 while (dquot_updating[hashent])
605 sleep_on(&update_wait);
607 if (!dquot->dq_sb) { /* Has somebody invalidated entry under us? */
609 * Do it as if the quota was invalidated before we started
611 dqput(dquot);
612 return NODQUOT;
614 dquot->dq_referenced++;
615 dqstats.lookups++;
617 return dquot;
620 static struct dquot *dqduplicate(struct dquot *dquot)
622 if (dquot == NODQUOT || !dquot->dq_sb)
623 return NODQUOT;
624 dquot->dq_count++;
625 wait_on_dquot(dquot);
626 if (!dquot->dq_sb) {
627 dquot->dq_count--;
628 return NODQUOT;
630 dquot->dq_referenced++;
631 dqstats.lookups++;
632 return dquot;
635 /* Check whether this inode is quota file */
636 static inline int is_quotafile(struct inode *inode)
638 int cnt;
639 struct quota_mount_options *dqopt = sb_dqopt(inode->i_sb);
640 struct file **files;
642 if (!dqopt)
643 return 0;
644 files = dqopt->files;
645 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
646 if (files[cnt] && files[cnt]->f_dentry->d_inode == inode)
647 return 1;
648 return 0;
651 static int dqinit_needed(struct inode *inode, short type)
653 int cnt;
655 if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode)))
656 return 0;
657 if (is_quotafile(inode))
658 return 0;
659 if (type != -1)
660 return inode->i_dquot[type] == NODQUOT;
661 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
662 if (inode->i_dquot[cnt] == NODQUOT)
663 return 1;
664 return 0;
667 static void add_dquot_ref(struct super_block *sb, short type)
669 struct list_head *p;
670 struct inode *inode;
672 if (!sb->dq_op)
673 return; /* nothing to do */
675 restart:
676 file_list_lock();
677 for (p = sb->s_files.next; p != &sb->s_files; p = p->next) {
678 struct file *filp = list_entry(p, struct file, f_list);
679 if (!filp->f_dentry)
680 continue;
681 inode = filp->f_dentry->d_inode;
682 if (!inode)
683 continue;
684 if (filp->f_mode & FMODE_WRITE && dqinit_needed(inode, type)) {
685 file_list_unlock();
686 sb->dq_op->initialize(inode, type);
687 inode->i_flags |= S_QUOTA;
688 /* As we may have blocked we had better restart... */
689 goto restart;
692 file_list_unlock();
695 /* Return 0 if dqput() won't block (note that 1 doesn't necessarily mean blocking) */
696 static inline int dqput_blocks(struct dquot *dquot)
698 if (dquot->dq_count == 1)
699 return 1;
700 return 0;
703 /* Remove references to dquots from inode - add dquot to list for freeing if needed */
704 int remove_inode_dquot_ref(struct inode *inode, short type, struct list_head *tofree_head)
706 struct dquot *dquot = inode->i_dquot[type];
707 int cnt;
709 inode->i_dquot[type] = NODQUOT;
710 /* any other quota in use? */
711 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
712 if (inode->i_dquot[cnt] != NODQUOT)
713 goto put_it;
715 inode->i_flags &= ~S_QUOTA;
716 put_it:
717 if (dquot != NODQUOT) {
718 if (dqput_blocks(dquot)) {
719 if (dquot->dq_count != 1)
720 printk(KERN_WARNING "VFS: Adding dquot with dq_count %d to dispose list.\n", dquot->dq_count);
721 list_add(&dquot->dq_free, tofree_head); /* As dquot must have currently users it can't be on the free list... */
722 return 1;
723 } else {
724 dqput(dquot); /* We have guaranteed we won't block */
727 return 0;
730 /* Free list of dquots - called from inode.c */
731 void put_dquot_list(struct list_head *tofree_head)
733 struct list_head *act_head = tofree_head->next;
734 struct dquot *dquot;
736 /* So now we have dquots on the list... Just free them */
737 while (act_head != tofree_head) {
738 dquot = list_entry(act_head, struct dquot, dq_free);
739 act_head = act_head->next;
740 list_del(&dquot->dq_free); /* Remove dquot from the list so we won't have problems... */
741 INIT_LIST_HEAD(&dquot->dq_free);
742 dqput(dquot);
746 static inline void dquot_incr_inodes(struct dquot *dquot, unsigned long number)
748 dquot->dq_curinodes += number;
749 dquot->dq_flags |= DQ_MOD;
752 static inline void dquot_incr_blocks(struct dquot *dquot, unsigned long number)
754 dquot->dq_curblocks += number;
755 dquot->dq_flags |= DQ_MOD;
758 static inline void dquot_decr_inodes(struct dquot *dquot, unsigned long number)
760 if (dquot->dq_curinodes > number)
761 dquot->dq_curinodes -= number;
762 else
763 dquot->dq_curinodes = 0;
764 if (dquot->dq_curinodes < dquot->dq_isoftlimit)
765 dquot->dq_itime = (time_t) 0;
766 dquot->dq_flags &= ~DQ_INODES;
767 dquot->dq_flags |= DQ_MOD;
770 static inline void dquot_decr_blocks(struct dquot *dquot, unsigned long number)
772 if (dquot->dq_curblocks > number)
773 dquot->dq_curblocks -= number;
774 else
775 dquot->dq_curblocks = 0;
776 if (dquot->dq_curblocks < dquot->dq_bsoftlimit)
777 dquot->dq_btime = (time_t) 0;
778 dquot->dq_flags &= ~DQ_BLKS;
779 dquot->dq_flags |= DQ_MOD;
782 static inline int need_print_warning(struct dquot *dquot, int flag)
784 switch (dquot->dq_type) {
785 case USRQUOTA:
786 return current->fsuid == dquot->dq_id && !(dquot->dq_flags & flag);
787 case GRPQUOTA:
788 return in_group_p(dquot->dq_id) && !(dquot->dq_flags & flag);
790 return 0;
793 static void print_warning(struct dquot *dquot, int flag, const char *fmtstr)
795 if (!need_print_warning(dquot, flag))
796 return;
797 sprintf(quotamessage, fmtstr,
798 bdevname(dquot->dq_sb->s_dev), quotatypes[dquot->dq_type]);
799 tty_write_message(current->tty, quotamessage);
800 dquot->dq_flags |= flag;
803 static inline char ignore_hardlimit(struct dquot *dquot)
805 return capable(CAP_SYS_RESOURCE) && !dquot->dq_sb->s_dquot.rsquash[dquot->dq_type];
808 static int check_idq(struct dquot *dquot, u_long inodes)
810 if (inodes <= 0 || dquot->dq_flags & DQ_FAKE)
811 return QUOTA_OK;
813 if (dquot->dq_ihardlimit &&
814 (dquot->dq_curinodes + inodes) > dquot->dq_ihardlimit &&
815 !ignore_hardlimit(dquot)) {
816 print_warning(dquot, DQ_INODES, "%s: write failed, %s file limit reached\n");
817 return NO_QUOTA;
820 if (dquot->dq_isoftlimit &&
821 (dquot->dq_curinodes + inodes) > dquot->dq_isoftlimit &&
822 dquot->dq_itime && CURRENT_TIME >= dquot->dq_itime &&
823 !ignore_hardlimit(dquot)) {
824 print_warning(dquot, DQ_INODES, "%s: warning, %s file quota exceeded too long.\n");
825 return NO_QUOTA;
828 if (dquot->dq_isoftlimit &&
829 (dquot->dq_curinodes + inodes) > dquot->dq_isoftlimit &&
830 dquot->dq_itime == 0) {
831 print_warning(dquot, 0, "%s: warning, %s file quota exceeded\n");
832 dquot->dq_itime = CURRENT_TIME + dquot->dq_sb->s_dquot.inode_expire[dquot->dq_type];
835 return QUOTA_OK;
838 static int check_bdq(struct dquot *dquot, u_long blocks, char prealloc)
840 if (blocks <= 0 || dquot->dq_flags & DQ_FAKE)
841 return QUOTA_OK;
843 if (dquot->dq_bhardlimit &&
844 (dquot->dq_curblocks + blocks) > dquot->dq_bhardlimit &&
845 !ignore_hardlimit(dquot)) {
846 if (!prealloc)
847 print_warning(dquot, DQ_BLKS, "%s: write failed, %s disk limit reached.\n");
848 return NO_QUOTA;
851 if (dquot->dq_bsoftlimit &&
852 (dquot->dq_curblocks + blocks) > dquot->dq_bsoftlimit &&
853 dquot->dq_btime && CURRENT_TIME >= dquot->dq_btime &&
854 !ignore_hardlimit(dquot)) {
855 if (!prealloc)
856 print_warning(dquot, DQ_BLKS, "%s: write failed, %s disk quota exceeded too long.\n");
857 return NO_QUOTA;
860 if (dquot->dq_bsoftlimit &&
861 (dquot->dq_curblocks + blocks) > dquot->dq_bsoftlimit &&
862 dquot->dq_btime == 0) {
863 if (!prealloc) {
864 print_warning(dquot, 0, "%s: warning, %s disk quota exceeded\n");
865 dquot->dq_btime = CURRENT_TIME + dquot->dq_sb->s_dquot.block_expire[dquot->dq_type];
867 else
869 * We don't allow preallocation to exceed softlimit so exceeding will
870 * be always printed
872 return NO_QUOTA;
875 return QUOTA_OK;
879 * Initialize a dquot-struct with new quota info. This is used by the
880 * system call interface functions.
882 static int set_dqblk(struct super_block *sb, int id, short type, int flags, struct dqblk *dqblk)
884 struct dquot *dquot;
885 int error = -EFAULT;
886 struct dqblk dq_dqblk;
888 if (dqblk == (struct dqblk *)NULL)
889 return error;
891 if (flags & QUOTA_SYSCALL) {
892 if (copy_from_user(&dq_dqblk, dqblk, sizeof(struct dqblk)))
893 return(error);
894 } else
895 memcpy((caddr_t)&dq_dqblk, (caddr_t)dqblk, sizeof(struct dqblk));
897 if (sb && (dquot = dqget(sb, id, type)) != NODQUOT) {
898 lock_dquot(dquot);
900 if (id > 0 && ((flags & SET_QUOTA) || (flags & SET_QLIMIT))) {
901 dquot->dq_bhardlimit = dq_dqblk.dqb_bhardlimit;
902 dquot->dq_bsoftlimit = dq_dqblk.dqb_bsoftlimit;
903 dquot->dq_ihardlimit = dq_dqblk.dqb_ihardlimit;
904 dquot->dq_isoftlimit = dq_dqblk.dqb_isoftlimit;
907 if ((flags & SET_QUOTA) || (flags & SET_USE)) {
908 if (dquot->dq_isoftlimit &&
909 dquot->dq_curinodes < dquot->dq_isoftlimit &&
910 dq_dqblk.dqb_curinodes >= dquot->dq_isoftlimit)
911 dquot->dq_itime = CURRENT_TIME + dquot->dq_sb->s_dquot.inode_expire[type];
912 dquot->dq_curinodes = dq_dqblk.dqb_curinodes;
913 if (dquot->dq_curinodes < dquot->dq_isoftlimit)
914 dquot->dq_flags &= ~DQ_INODES;
915 if (dquot->dq_bsoftlimit &&
916 dquot->dq_curblocks < dquot->dq_bsoftlimit &&
917 dq_dqblk.dqb_curblocks >= dquot->dq_bsoftlimit)
918 dquot->dq_btime = CURRENT_TIME + dquot->dq_sb->s_dquot.block_expire[type];
919 dquot->dq_curblocks = dq_dqblk.dqb_curblocks;
920 if (dquot->dq_curblocks < dquot->dq_bsoftlimit)
921 dquot->dq_flags &= ~DQ_BLKS;
924 if (id == 0) {
925 dquot->dq_sb->s_dquot.block_expire[type] = dquot->dq_btime = dq_dqblk.dqb_btime;
926 dquot->dq_sb->s_dquot.inode_expire[type] = dquot->dq_itime = dq_dqblk.dqb_itime;
929 if (dq_dqblk.dqb_bhardlimit == 0 && dq_dqblk.dqb_bsoftlimit == 0 &&
930 dq_dqblk.dqb_ihardlimit == 0 && dq_dqblk.dqb_isoftlimit == 0)
931 dquot->dq_flags |= DQ_FAKE;
932 else
933 dquot->dq_flags &= ~DQ_FAKE;
935 dquot->dq_flags |= DQ_MOD;
936 unlock_dquot(dquot);
937 dqput(dquot);
939 return(0);
942 static int get_quota(struct super_block *sb, int id, short type, struct dqblk *dqblk)
944 struct dquot *dquot;
945 int error = -ESRCH;
947 if (!sb || !sb_has_quota_enabled(sb, type))
948 goto out;
949 dquot = dqget(sb, id, type);
950 if (dquot == NODQUOT)
951 goto out;
953 lock_dquot(dquot); /* We must protect against invalidating the quota */
954 error = -EFAULT;
955 if (dqblk && !copy_to_user(dqblk, &dquot->dq_dqb, sizeof(struct dqblk)))
956 error = 0;
957 unlock_dquot(dquot);
958 dqput(dquot);
959 out:
960 return error;
963 static int get_stats(caddr_t addr)
965 int error = -EFAULT;
966 struct dqstats stats;
968 dqstats.allocated_dquots = nr_dquots;
969 dqstats.free_dquots = nr_free_dquots;
971 /* make a copy, in case we page-fault in user space */
972 memcpy(&stats, &dqstats, sizeof(struct dqstats));
973 if (!copy_to_user(addr, &stats, sizeof(struct dqstats)))
974 error = 0;
975 return error;
978 static int quota_root_squash(struct super_block *sb, short type, int *addr)
980 int new_value, error;
982 if (!sb)
983 return(-ENODEV);
985 error = -EFAULT;
986 if (!copy_from_user(&new_value, addr, sizeof(int))) {
987 sb_dqopt(sb)->rsquash[type] = new_value;
988 error = 0;
990 return error;
994 * This is a simple algorithm that calculates the size of a file in blocks.
995 * This is only used on filesystems that do not have an i_blocks count.
997 static u_long isize_to_blocks(loff_t isize, size_t blksize_bits)
999 u_long blocks;
1000 u_long indirect;
1002 if (!blksize_bits)
1003 blksize_bits = BLOCK_SIZE_BITS;
1004 blocks = (isize >> blksize_bits) + ((isize & ~((1 << blksize_bits)-1)) ? 1 : 0);
1005 if (blocks > 10) {
1006 indirect = ((blocks - 11) >> 8) + 1; /* single indirect blocks */
1007 if (blocks > (10 + 256)) {
1008 indirect += ((blocks - 267) >> 16) + 1; /* double indirect blocks */
1009 if (blocks > (10 + 256 + (256 << 8)))
1010 indirect++; /* triple indirect blocks */
1012 blocks += indirect;
1014 return blocks;
1018 * Externally referenced functions through dquot_operations in inode.
1020 * Note: this is a blocking operation.
1022 void dquot_initialize(struct inode *inode, short type)
1024 struct dquot *dquot;
1025 unsigned int id = 0;
1026 short cnt;
1028 if (!S_ISREG(inode->i_mode) && !S_ISDIR(inode->i_mode) &&
1029 !S_ISLNK(inode->i_mode))
1030 return;
1031 lock_kernel();
1032 /* We don't want to have quotas on quota files - nasty deadlocks possible */
1033 if (is_quotafile(inode)) {
1034 unlock_kernel();
1035 return;
1037 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1038 if (type != -1 && cnt != type)
1039 continue;
1041 if (!sb_has_quota_enabled(inode->i_sb, cnt))
1042 continue;
1044 if (inode->i_dquot[cnt] == NODQUOT) {
1045 switch (cnt) {
1046 case USRQUOTA:
1047 id = inode->i_uid;
1048 break;
1049 case GRPQUOTA:
1050 id = inode->i_gid;
1051 break;
1053 dquot = dqget(inode->i_sb, id, cnt);
1054 if (dquot == NODQUOT)
1055 continue;
1056 if (inode->i_dquot[cnt] != NODQUOT) {
1057 dqput(dquot);
1058 continue;
1060 inode->i_dquot[cnt] = dquot;
1061 inode->i_flags |= S_QUOTA;
1064 unlock_kernel();
1068 * Release all quota for the specified inode.
1070 * Note: this is a blocking operation.
1072 void dquot_drop(struct inode *inode)
1074 struct dquot *dquot;
1075 short cnt;
1077 lock_kernel();
1078 inode->i_flags &= ~S_QUOTA;
1079 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1080 if (inode->i_dquot[cnt] == NODQUOT)
1081 continue;
1082 dquot = inode->i_dquot[cnt];
1083 inode->i_dquot[cnt] = NODQUOT;
1084 dqput(dquot);
1086 unlock_kernel();
1090 * Note: this is a blocking operation.
1092 int dquot_alloc_block(const struct inode *inode, unsigned long number, char warn)
1094 int cnt;
1095 struct dquot *dquot[MAXQUOTAS];
1097 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1098 dquot[cnt] = dqduplicate(inode->i_dquot[cnt]);
1099 if (dquot[cnt] == NODQUOT)
1100 continue;
1101 lock_dquot(dquot[cnt]);
1102 if (check_bdq(dquot[cnt], number, warn))
1103 goto put_all;
1106 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1107 if (dquot[cnt] == NODQUOT)
1108 continue;
1109 dquot_incr_blocks(dquot[cnt], number);
1110 unlock_dquot(dquot[cnt]);
1111 dqput(dquot[cnt]);
1114 return QUOTA_OK;
1115 put_all:
1116 for (; cnt >= 0; cnt--) {
1117 if (dquot[cnt] == NODQUOT)
1118 continue;
1119 unlock_dquot(dquot[cnt]);
1120 dqput(dquot[cnt]);
1122 return NO_QUOTA;
1126 * Note: this is a blocking operation.
1128 int dquot_alloc_inode(const struct inode *inode, unsigned long number)
1130 int cnt;
1131 struct dquot *dquot[MAXQUOTAS];
1133 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1134 dquot[cnt] = dqduplicate(inode -> i_dquot[cnt]);
1135 if (dquot[cnt] == NODQUOT)
1136 continue;
1137 lock_dquot(dquot[cnt]);
1138 if (check_idq(dquot[cnt], number))
1139 goto put_all;
1142 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1143 if (dquot[cnt] == NODQUOT)
1144 continue;
1145 dquot_incr_inodes(dquot[cnt], number);
1146 unlock_dquot(dquot[cnt]);
1147 dqput(dquot[cnt]);
1150 return QUOTA_OK;
1151 put_all:
1152 for (; cnt >= 0; cnt--) {
1153 if (dquot[cnt] == NODQUOT)
1154 continue;
1155 unlock_dquot(dquot[cnt]);
1156 dqput(dquot[cnt]);
1158 return NO_QUOTA;
1162 * Note: this is a blocking operation.
1164 void dquot_free_block(const struct inode *inode, unsigned long number)
1166 unsigned short cnt;
1167 struct dquot *dquot;
1169 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1170 dquot = inode->i_dquot[cnt];
1171 if (dquot == NODQUOT)
1172 continue;
1173 wait_on_dquot(dquot);
1174 dquot_decr_blocks(dquot, number);
1179 * Note: this is a blocking operation.
1181 void dquot_free_inode(const struct inode *inode, unsigned long number)
1183 unsigned short cnt;
1184 struct dquot *dquot;
1186 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1187 dquot = inode->i_dquot[cnt];
1188 if (dquot == NODQUOT)
1189 continue;
1190 wait_on_dquot(dquot);
1191 dquot_decr_inodes(dquot, number);
1196 * Transfer the number of inode and blocks from one diskquota to an other.
1198 * Note: this is a blocking operation.
1200 int dquot_transfer(struct dentry *dentry, struct iattr *iattr)
1202 struct inode *inode = dentry -> d_inode;
1203 unsigned long blocks;
1204 struct dquot *transfer_from[MAXQUOTAS];
1205 struct dquot *transfer_to[MAXQUOTAS];
1206 short cnt, disc;
1207 int error = -EDQUOT;
1209 if (!inode)
1210 return -ENOENT;
1211 /* Arguably we could consider that as error, but... no fs - no quota */
1212 if (!inode->i_sb)
1213 return 0;
1215 lock_kernel();
1217 * Build the transfer_from and transfer_to lists and check quotas to see
1218 * if operation is permitted.
1220 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1221 transfer_from[cnt] = NODQUOT;
1222 transfer_to[cnt] = NODQUOT;
1224 if (!sb_has_quota_enabled(inode->i_sb, cnt))
1225 continue;
1227 switch (cnt) {
1228 case USRQUOTA:
1229 if (inode->i_uid == iattr->ia_uid)
1230 continue;
1231 /* We can get transfer_from from inode, can't we? */
1232 transfer_from[cnt] = dqget(inode->i_sb, inode->i_uid, cnt);
1233 transfer_to[cnt] = dqget(inode->i_sb, iattr->ia_uid, cnt);
1234 break;
1235 case GRPQUOTA:
1236 if (inode->i_gid == iattr->ia_gid)
1237 continue;
1238 transfer_from[cnt] = dqget(inode->i_sb, inode->i_gid, cnt);
1239 transfer_to[cnt] = dqget(inode->i_sb, iattr->ia_gid, cnt);
1240 break;
1243 /* Something bad (eg. quotaoff) happened while we were sleeping? */
1244 if (transfer_from[cnt] == NODQUOT || transfer_to[cnt] == NODQUOT)
1246 if (transfer_from[cnt] != NODQUOT) {
1247 dqput(transfer_from[cnt]);
1248 transfer_from[cnt] = NODQUOT;
1250 if (transfer_to[cnt] != NODQUOT) {
1251 dqput(transfer_to[cnt]);
1252 transfer_to[cnt] = NODQUOT;
1254 continue;
1257 * We have to lock the quotas to prevent races...
1259 if (transfer_from[cnt] < transfer_to[cnt])
1261 lock_dquot(transfer_from[cnt]);
1262 lock_dquot(transfer_to[cnt]);
1264 else
1266 lock_dquot(transfer_to[cnt]);
1267 lock_dquot(transfer_from[cnt]);
1271 * The entries might got invalidated while locking. The second
1272 * dqget() could block and so the first structure might got
1273 * invalidated or locked...
1275 if (!transfer_to[cnt]->dq_sb || !transfer_from[cnt]->dq_sb) {
1276 cnt++;
1277 goto put_all;
1282 * Find out if this filesystem uses i_blocks.
1284 if (!inode->i_sb->s_blocksize)
1285 blocks = isize_to_blocks(inode->i_size, BLOCK_SIZE_BITS);
1286 else
1287 blocks = (inode->i_blocks >> 1);
1288 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1289 if (!transfer_to[cnt])
1290 continue;
1291 if (check_idq(transfer_to[cnt], 1) == NO_QUOTA ||
1292 check_bdq(transfer_to[cnt], blocks, 0) == NO_QUOTA) {
1293 cnt = MAXQUOTAS;
1294 goto put_all;
1298 if ((error = notify_change(dentry, iattr)))
1299 goto put_all;
1301 * Finally perform the needed transfer from transfer_from to transfer_to,
1302 * and release any pointers to dquots not needed anymore.
1304 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1306 * Skip changes for same uid or gid or for non-existing quota-type.
1308 if (transfer_from[cnt] == NODQUOT && transfer_to[cnt] == NODQUOT)
1309 continue;
1311 dquot_decr_inodes(transfer_from[cnt], 1);
1312 dquot_decr_blocks(transfer_from[cnt], blocks);
1314 dquot_incr_inodes(transfer_to[cnt], 1);
1315 dquot_incr_blocks(transfer_to[cnt], blocks);
1317 unlock_dquot(transfer_from[cnt]);
1318 dqput(transfer_from[cnt]);
1319 if (inode->i_dquot[cnt] != NODQUOT) {
1320 struct dquot *temp = inode->i_dquot[cnt];
1321 inode->i_dquot[cnt] = transfer_to[cnt];
1322 unlock_dquot(transfer_to[cnt]);
1323 dqput(temp);
1324 } else {
1325 unlock_dquot(transfer_to[cnt]);
1326 dqput(transfer_to[cnt]);
1330 unlock_kernel();
1331 return 0;
1332 put_all:
1333 for (disc = 0; disc < cnt; disc++) {
1334 /* There should be none or both pointers set but... */
1335 if (transfer_to[disc] != NODQUOT) {
1336 unlock_dquot(transfer_to[disc]);
1337 dqput(transfer_to[disc]);
1339 if (transfer_from[disc] != NODQUOT) {
1340 unlock_dquot(transfer_from[disc]);
1341 dqput(transfer_from[disc]);
1344 unlock_kernel();
1345 return error;
1349 void __init dquot_init_hash(void)
1351 printk(KERN_NOTICE "VFS: Diskquotas version %s initialized\n", __DQUOT_VERSION__);
1353 memset(dquot_hash, 0, sizeof(dquot_hash));
1354 memset((caddr_t)&dqstats, 0, sizeof(dqstats));
1358 * Definitions of diskquota operations.
1360 struct dquot_operations dquot_operations = {
1361 dquot_initialize, /* mandatory */
1362 dquot_drop, /* mandatory */
1363 dquot_alloc_block,
1364 dquot_alloc_inode,
1365 dquot_free_block,
1366 dquot_free_inode,
1367 dquot_transfer
1370 static inline void set_enable_flags(struct quota_mount_options *dqopt, short type)
1372 switch (type) {
1373 case USRQUOTA:
1374 dqopt->flags |= DQUOT_USR_ENABLED;
1375 break;
1376 case GRPQUOTA:
1377 dqopt->flags |= DQUOT_GRP_ENABLED;
1378 break;
1382 static inline void reset_enable_flags(struct quota_mount_options *dqopt, short type)
1384 switch (type) {
1385 case USRQUOTA:
1386 dqopt->flags &= ~DQUOT_USR_ENABLED;
1387 break;
1388 case GRPQUOTA:
1389 dqopt->flags &= ~DQUOT_GRP_ENABLED;
1390 break;
1394 /* Function in inode.c - remove pointers to dquots in icache */
1395 extern void remove_dquot_ref(kdev_t, short);
1398 * Turn quota off on a device. type == -1 ==> quotaoff for all types (umount)
1400 int quota_off(struct super_block *sb, short type)
1402 struct file *filp;
1403 short cnt;
1404 int enabled = 0;
1405 struct quota_mount_options *dqopt = sb_dqopt(sb);
1407 if (!sb)
1408 goto out;
1410 /* We need to serialize quota_off() for device */
1411 down(&dqopt->dqoff_sem);
1412 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1413 if (type != -1 && cnt != type)
1414 continue;
1415 if (!is_enabled(dqopt, cnt))
1416 continue;
1417 reset_enable_flags(dqopt, cnt);
1419 /* Note: these are blocking operations */
1420 remove_dquot_ref(sb->s_dev, cnt);
1421 invalidate_dquots(sb->s_dev, cnt);
1423 /* Wait for any pending IO - remove me as soon as invalidate is more polite */
1424 down(&dqopt->dqio_sem);
1425 filp = dqopt->files[cnt];
1426 dqopt->files[cnt] = (struct file *)NULL;
1427 dqopt->inode_expire[cnt] = 0;
1428 dqopt->block_expire[cnt] = 0;
1429 up(&dqopt->dqio_sem);
1430 fput(filp);
1434 * Check whether any quota is still enabled,
1435 * and if not clear the dq_op pointer.
1437 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1438 enabled |= is_enabled(dqopt, cnt);
1439 if (!enabled)
1440 sb->dq_op = NULL;
1441 up(&dqopt->dqoff_sem);
1442 out:
1443 return(0);
1446 static inline int check_quotafile_size(loff_t size)
1448 ulong blocks = size >> BLOCK_SIZE_BITS;
1449 size_t off = size & (BLOCK_SIZE - 1);
1451 return !(((blocks % sizeof(struct dqblk)) * BLOCK_SIZE + off % sizeof(struct dqblk)) % sizeof(struct dqblk));
1454 static int quota_on(struct super_block *sb, short type, char *path)
1456 struct file *f;
1457 struct inode *inode;
1458 struct dquot *dquot;
1459 struct quota_mount_options *dqopt = sb_dqopt(sb);
1460 char *tmp;
1461 int error;
1463 if (is_enabled(dqopt, type))
1464 return -EBUSY;
1466 down(&dqopt->dqoff_sem);
1467 tmp = getname(path);
1468 error = PTR_ERR(tmp);
1469 if (IS_ERR(tmp))
1470 goto out_lock;
1472 f = filp_open(tmp, O_RDWR, 0600);
1473 putname(tmp);
1475 error = PTR_ERR(f);
1476 if (IS_ERR(f))
1477 goto out_lock;
1478 error = -EIO;
1479 if (!f->f_op->read && !f->f_op->write)
1480 goto out_f;
1481 inode = f->f_dentry->d_inode;
1482 error = -EACCES;
1483 if (!S_ISREG(inode->i_mode))
1484 goto out_f;
1485 error = -EINVAL;
1486 if (inode->i_size == 0 || !check_quotafile_size(inode->i_size))
1487 goto out_f;
1488 dquot_drop(inode); /* We don't want quota on quota files */
1490 set_enable_flags(dqopt, type);
1491 dqopt->files[type] = f;
1493 dquot = dqget(sb, 0, type);
1494 dqopt->inode_expire[type] = (dquot != NODQUOT) ? dquot->dq_itime : MAX_IQ_TIME;
1495 dqopt->block_expire[type] = (dquot != NODQUOT) ? dquot->dq_btime : MAX_DQ_TIME;
1496 dqput(dquot);
1498 sb->dq_op = &dquot_operations;
1499 add_dquot_ref(sb, type);
1501 up(&dqopt->dqoff_sem);
1502 return 0;
1504 out_f:
1505 filp_close(f, NULL);
1506 out_lock:
1507 up(&dqopt->dqoff_sem);
1509 return error;
1513 * This is the system call interface. This communicates with
1514 * the user-level programs. Currently this only supports diskquota
1515 * calls. Maybe we need to add the process quotas etc. in the future,
1516 * but we probably should use rlimits for that.
1518 asmlinkage long sys_quotactl(int cmd, const char *special, int id, caddr_t addr)
1520 int cmds = 0, type = 0, flags = 0;
1521 kdev_t dev;
1522 struct super_block *sb = NULL;
1523 int ret = -EINVAL;
1525 lock_kernel();
1526 cmds = cmd >> SUBCMDSHIFT;
1527 type = cmd & SUBCMDMASK;
1529 if ((u_int) type >= MAXQUOTAS)
1530 goto out;
1531 if (id & ~0xFFFF)
1532 goto out;
1534 ret = -EPERM;
1535 switch (cmds) {
1536 case Q_SYNC:
1537 case Q_GETSTATS:
1538 break;
1539 case Q_GETQUOTA:
1540 if (((type == USRQUOTA && current->euid != id) ||
1541 (type == GRPQUOTA && in_egroup_p(id))) &&
1542 !capable(CAP_SYS_RESOURCE))
1543 goto out;
1544 break;
1545 default:
1546 if (!capable(CAP_SYS_RESOURCE))
1547 goto out;
1550 ret = -EINVAL;
1551 dev = NODEV;
1552 if (special != NULL || (cmds != Q_SYNC && cmds != Q_GETSTATS)) {
1553 mode_t mode;
1554 struct nameidata nd;
1556 ret = user_path_walk(special, &nd);
1557 if (ret)
1558 goto out;
1560 dev = nd.dentry->d_inode->i_rdev;
1561 mode = nd.dentry->d_inode->i_mode;
1562 path_release(&nd);
1564 ret = -ENOTBLK;
1565 if (!S_ISBLK(mode))
1566 goto out;
1567 sb = get_super(dev);
1570 ret = -EINVAL;
1571 switch (cmds) {
1572 case Q_QUOTAON:
1573 ret = sb ? quota_on(sb, type, (char *) addr) : -ENODEV;
1574 goto out;
1575 case Q_QUOTAOFF:
1576 ret = quota_off(sb, type);
1577 goto out;
1578 case Q_GETQUOTA:
1579 ret = get_quota(sb, id, type, (struct dqblk *) addr);
1580 goto out;
1581 case Q_SETQUOTA:
1582 flags |= SET_QUOTA;
1583 break;
1584 case Q_SETUSE:
1585 flags |= SET_USE;
1586 break;
1587 case Q_SETQLIM:
1588 flags |= SET_QLIMIT;
1589 break;
1590 case Q_SYNC:
1591 ret = sync_dquots(dev, type);
1592 goto out;
1593 case Q_GETSTATS:
1594 ret = get_stats(addr);
1595 goto out;
1596 case Q_RSQUASH:
1597 ret = quota_root_squash(sb, type, (int *) addr);
1598 goto out;
1599 default:
1600 goto out;
1603 flags |= QUOTA_SYSCALL;
1605 ret = -ESRCH;
1606 if (sb && sb_has_quota_enabled(sb, type))
1607 ret = set_dqblk(sb, id, type, flags, (struct dqblk *) addr);
1608 out:
1609 unlock_kernel();
1610 return ret;