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
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 * 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
45 * Added dynamic quota structure allocation
46 * Jan Kara <jack@suse.cz> 12/2000
48 * (C) Copyright 1994 - 1997 Marco van Wieringen
51 #include <linux/errno.h>
52 #include <linux/kernel.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>
65 #include <asm/uaccess.h>
67 #define __DQUOT_VERSION__ "dquot_6.4.0"
69 int nr_dquots, nr_free_dquots;
71 static char *quotatypes[] = INITQFNAMES;
73 static inline struct quota_mount_options *sb_dqopt(struct super_block *sb)
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.
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
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.
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.
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.
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.
114 static LIST_HEAD(inuse_list);
115 static LIST_HEAD(free_dquots);
116 static struct list_head dquot_hash[NR_DQHASH];
118 static struct dqstats dqstats;
120 static void dqput(struct dquot *);
121 static struct dquot *dqduplicate(struct dquot *);
123 static inline char is_enabled(struct quota_mount_options *dqopt, short type)
127 return((dqopt->flags & DQUOT_USR_ENABLED) != 0);
129 return((dqopt->flags & DQUOT_GRP_ENABLED) != 0);
134 static inline char sb_has_quota_enabled(struct super_block *sb, short type)
136 return is_enabled(sb_dqopt(sb), type);
139 static inline int const hashfn(kdev_t dev, unsigned int id, short type)
141 return((HASHDEV(dev) ^ id) * (MAXQUOTAS - type)) % NR_DQHASH;
144 static inline void insert_dquot_hash(struct dquot *dquot)
146 struct list_head *head = dquot_hash + hashfn(dquot->dq_dev, dquot->dq_id, dquot->dq_type);
147 list_add(&dquot->dq_hash, head);
150 static inline void remove_dquot_hash(struct dquot *dquot)
152 list_del(&dquot->dq_hash);
153 INIT_LIST_HEAD(&dquot->dq_hash);
156 static inline struct dquot *find_dquot(unsigned int hashent, kdev_t dev, unsigned int id, short type)
158 struct list_head *head;
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)
169 /* Add a dquot to the head of the free list */
170 static inline void put_dquot_head(struct dquot *dquot)
172 list_add(&dquot->dq_free, &free_dquots);
176 /* Add a dquot to the tail of the free list */
177 static inline void put_dquot_last(struct dquot *dquot)
179 list_add(&dquot->dq_free, free_dquots.prev);
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)
186 list_del(&dquot->dq_free);
187 list_add(&dquot->dq_free, &free_dquots);
190 static inline void remove_free_dquot(struct dquot *dquot)
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 */
197 list_del(&dquot->dq_free);
198 INIT_LIST_HEAD(&dquot->dq_free);
202 static inline void put_inuse(struct dquot *dquot)
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);
210 static inline void remove_inuse(struct dquot *dquot)
213 list_del(&dquot->dq_inuse);
216 static void __wait_on_dquot(struct dquot *dquot)
218 DECLARE_WAITQUEUE(wait, current);
220 add_wait_queue(&dquot->dq_wait_lock, &wait);
222 set_current_state(TASK_UNINTERRUPTIBLE);
223 if (dquot->dq_flags & DQ_LOCKED) {
227 remove_wait_queue(&dquot->dq_wait_lock, &wait);
228 current->state = TASK_RUNNING;
231 static inline void wait_on_dquot(struct dquot *dquot)
233 if (dquot->dq_flags & DQ_LOCKED)
234 __wait_on_dquot(dquot);
237 static inline void lock_dquot(struct dquot *dquot)
239 wait_on_dquot(dquot);
240 dquot->dq_flags |= DQ_LOCKED;
243 static inline void unlock_dquot(struct dquot *dquot)
245 dquot->dq_flags &= ~DQ_LOCKED;
246 wake_up(&dquot->dq_wait_lock);
249 static void __wait_dquot_unused(struct dquot *dquot)
251 DECLARE_WAITQUEUE(wait, current);
253 add_wait_queue(&dquot->dq_wait_free, &wait);
255 set_current_state(TASK_UNINTERRUPTIBLE);
256 if (dquot->dq_count) {
260 remove_wait_queue(&dquot->dq_wait_free, &wait);
261 current->state = TASK_RUNNING;
265 * We don't have to be afraid of deadlocks as we never have quotas on quota files...
267 static void write_dquot(struct dquot *dquot)
269 short type = dquot->dq_type;
274 struct semaphore *sem = &dquot->dq_sb->s_dquot.dqio_sem;
278 filp = dquot->dq_sb->s_dquot.files[type];
279 offset = dqoff(dquot->dq_id);
284 * Note: clear the DQ_MOD flag unconditionally,
285 * so we don't loop forever on failure.
287 memcpy(&dqbuf, &dquot->dq_dqb, sizeof(struct dqblk));
288 dquot->dq_flags &= ~DQ_MOD;
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));
302 static void read_dquot(struct dquot *dquot)
304 short type = dquot->dq_type;
309 filp = dquot->dq_sb->s_dquot.files[type];
310 if (filp == (struct file *)NULL)
314 if (!dquot->dq_sb) /* Invalidated quota? */
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);
321 filp->f_op->read(filp, (char *)&dquot->dq_dqb, sizeof(struct dqblk), &offset);
322 up(&dquot->dq_sb->s_dquot.dqio_sem);
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;
334 * Unhash and selectively clear the dquot structure,
335 * but preserve the use count, list pointers, and
338 void clear_dquot(struct dquot *dquot)
340 /* unhash it first */
341 remove_dquot_hash(dquot);
344 dquot->dq_dev = NODEV;
347 dquot->dq_referenced = 0;
348 memset(&dquot->dq_dqb, 0, sizeof(struct dqblk));
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)
357 struct list_head *head;
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)
364 if (dquot->dq_type != type)
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.
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);
377 kmem_cache_free(dquot_cachep, dquot);
382 int sync_dquots(kdev_t dev, short type)
384 struct list_head *head;
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)
392 if (type != -1 && dquot->dq_type != type)
394 if (!dquot->dq_sb) /* Invalidated? */
396 if (!(dquot->dq_flags & (DQ_MOD | DQ_LOCKED)))
398 /* Raise use count so quota won't be invalidated. We can't use dqduplicate() as it does too many tests */
400 if (dquot->dq_flags & DQ_LOCKED)
401 wait_on_dquot(dquot);
402 if (dquot->dq_flags & DQ_MOD)
411 /* Free unused dquots from cache */
412 static void prune_dqcache(int count)
414 struct list_head *head;
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);
423 kmem_cache_free(dquot_cachep, dquot);
425 head = free_dquots.prev;
429 int shrink_dqcache_memory(int priority, unsigned int gfp_mask)
431 prune_dqcache(nr_free_dquots / (priority + 1));
432 kmem_cache_shrink(dquot_cachep);
436 /* NOTE: If you change this function please check whether dqput_blocks() works right... */
437 static void dqput(struct dquot *dquot)
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],
451 if (dquot->dq_count > 1) {
452 /* We have more than one user... We can simply decrement use count */
456 if (dquot->dq_flags & DQ_MOD) {
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... */
468 /* Place at end of LRU free queue */
469 put_dquot_last(dquot);
470 wake_up(&dquot->dq_wait_free);
473 struct dquot *get_empty_dquot(void)
477 dquot = kmem_cache_alloc(dquot_cachep, SLAB_KERNEL);
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);
488 /* all dquots go on the inuse_list */
494 static struct dquot *dqget(struct super_block *sb, unsigned int id, short type)
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);
501 if (!is_enabled(dqopt, type)) {
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... */
515 dquot->dq_type = type;
516 dquot->dq_dev = sb->s_dev;
518 /* hash it first so it can be found */
519 insert_dquot_hash(dquot);
522 if (!dquot->dq_count++)
523 remove_free_dquot(dquot);
524 dqstats.cache_hits++;
525 wait_on_dquot(dquot);
530 if (!dquot->dq_sb) { /* Has somebody invalidated entry under us? */
531 printk(KERN_ERR "VFS: dqget(): Quota invalidated in dqget()!\n");
535 dquot->dq_referenced++;
541 static struct dquot *dqduplicate(struct dquot *dquot)
543 if (dquot == NODQUOT)
547 printk(KERN_ERR "VFS: dqduplicate(): Invalidated quota to be duplicated!\n");
551 if (dquot->dq_flags & DQ_LOCKED)
552 printk(KERN_ERR "VFS: dqduplicate(): Locked quota to be duplicated!\n");
553 dquot->dq_referenced++;
558 static int dqinit_needed(struct inode *inode, short type)
562 if (IS_NOQUOTA(inode))
565 return inode->i_dquot[type] == NODQUOT;
566 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
567 if (inode->i_dquot[cnt] == NODQUOT)
572 static void add_dquot_ref(struct super_block *sb, short type)
577 return; /* nothing to do */
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);
588 sb->dq_op->initialize(inode, type);
591 /* As we may have blocked we had better restart... */
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)
601 if (dquot->dq_count == 1)
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)
609 struct dquot *dquot = inode->i_dquot[type];
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)
618 inode->i_flags &= ~S_QUOTA;
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... */
628 dqput(dquot); /* We have guaranteed we won't block */
633 /* Free list of dquots - called from inode.c */
634 void put_dquot_list(struct list_head *tofree_head)
636 struct list_head *act_head = tofree_head->next;
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);
649 static inline void dquot_incr_inodes(struct dquot *dquot, unsigned long number)
651 dquot->dq_curinodes += number;
652 dquot->dq_flags |= DQ_MOD;
655 static inline void dquot_incr_blocks(struct dquot *dquot, unsigned long number)
657 dquot->dq_curblocks += number;
658 dquot->dq_flags |= DQ_MOD;
661 static inline void dquot_decr_inodes(struct dquot *dquot, unsigned long number)
663 if (dquot->dq_curinodes > number)
664 dquot->dq_curinodes -= number;
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;
673 static inline void dquot_decr_blocks(struct dquot *dquot, unsigned long number)
675 if (dquot->dq_curblocks > number)
676 dquot->dq_curblocks -= number;
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;
685 static inline int need_print_warning(struct dquot *dquot, int flag)
687 switch (dquot->dq_type) {
689 return current->fsuid == dquot->dq_id && !(dquot->dq_flags & flag);
691 return in_group_p(dquot->dq_id) && !(dquot->dq_flags & flag);
696 /* Values of warnings */
699 #define ISOFTLONGWARN 2
702 #define BSOFTLONGWARN 5
705 /* Print warning to user which exceeded quota */
706 static void print_warning(struct dquot *dquot, const char warntype)
709 int flag = (warntype == BHARDWARN || warntype == BSOFTLONGWARN) ? DQ_BLKS :
710 ((warntype == IHARDWARN || warntype == ISOFTLONGWARN) ? DQ_INODES : 0);
712 if (!need_print_warning(dquot, flag))
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, ");
719 tty_write_message(current->tty, ": write failed, ");
720 tty_write_message(current->tty, quotatypes[dquot->dq_type]);
723 msg = " file limit reached.\n";
726 msg = " file quota exceeded too long.\n";
729 msg = " file quota exceeded.\n";
732 msg = " block limit reached.\n";
735 msg = " block quota exceeded too long.\n";
738 msg = " block quota exceeded.\n";
741 tty_write_message(current->tty, msg);
744 static inline void flush_warnings(struct dquot **dquots, char *warntype)
748 for (i = 0; i < MAXQUOTAS; i++)
749 if (dquots[i] != NODQUOT && warntype[i] != NOWARN)
750 print_warning(dquots[i], warntype[i]);
753 static inline char ignore_hardlimit(struct dquot *dquot)
755 return capable(CAP_SYS_RESOURCE) && !dquot->dq_sb->s_dquot.rsquash[dquot->dq_type];
758 static int check_idq(struct dquot *dquot, ulong inodes, char *warntype)
761 if (inodes <= 0 || dquot->dq_flags & DQ_FAKE)
764 if (dquot->dq_ihardlimit &&
765 (dquot->dq_curinodes + inodes) > dquot->dq_ihardlimit &&
766 !ignore_hardlimit(dquot)) {
767 *warntype = IHARDWARN;
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;
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];
789 static int check_bdq(struct dquot *dquot, ulong blocks, char prealloc, char *warntype)
792 if (blocks <= 0 || dquot->dq_flags & DQ_FAKE)
795 if (dquot->dq_bhardlimit &&
796 (dquot->dq_curblocks + blocks) > dquot->dq_bhardlimit &&
797 !ignore_hardlimit(dquot)) {
799 *warntype = BHARDWARN;
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)) {
808 *warntype = BSOFTLONGWARN;
812 if (dquot->dq_bsoftlimit &&
813 (dquot->dq_curblocks + blocks) > dquot->dq_bsoftlimit &&
814 dquot->dq_btime == 0) {
816 *warntype = BSOFTWARN;
817 dquot->dq_btime = CURRENT_TIME + dquot->dq_sb->s_dquot.block_expire[dquot->dq_type];
821 * We don't allow preallocation to exceed softlimit so exceeding will
831 * Initialize a dquot-struct with new quota info. This is used by the
832 * system call interface functions.
834 static int set_dqblk(struct super_block *sb, int id, short type, int flags, struct dqblk *dqblk)
838 struct dqblk dq_dqblk;
840 if (copy_from_user(&dq_dqblk, dqblk, sizeof(struct dqblk)))
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;
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;
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;
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;
878 dquot->dq_flags &= ~DQ_FAKE;
880 dquot->dq_flags |= DQ_MOD;
886 static int get_quota(struct super_block *sb, int id, short type, struct dqblk *dqblk)
892 if (!sb || !sb_has_quota_enabled(sb, type))
894 dquot = dqget(sb, id, type);
895 if (dquot == NODQUOT)
898 memcpy(&data, &dquot->dq_dqb, sizeof(struct dqblk)); /* We copy data to preserve them from changing */
901 if (dqblk && !copy_to_user(dqblk, &data, sizeof(struct dqblk)))
907 static int get_stats(caddr_t addr)
910 struct dqstats stats;
912 dqstats.allocated_dquots = nr_dquots;
913 dqstats.free_dquots = nr_free_dquots;
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)))
922 static int quota_root_squash(struct super_block *sb, short type, int *addr)
924 int new_value, error;
930 if (!copy_from_user(&new_value, addr, sizeof(int))) {
931 sb_dqopt(sb)->rsquash[type] = new_value;
937 #if 0 /* We are not going to support filesystems without i_blocks... */
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.
942 static u_long isize_to_blocks(loff_t isize, size_t blksize_bits)
948 blksize_bits = BLOCK_SIZE_BITS;
949 blocks = (isize >> blksize_bits) + ((isize & ~((1 << blksize_bits)-1)) ? 1 : 0);
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 */
964 * Externally referenced functions through dquot_operations in inode.
966 * Note: this is a blocking operation.
968 void dquot_initialize(struct inode *inode, short type)
970 struct dquot *dquot[MAXQUOTAS];
974 if (IS_NOQUOTA(inode))
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)
981 if (!sb_has_quota_enabled(inode->i_sb, cnt))
983 if (inode->i_dquot[cnt] == NODQUOT) {
992 dquot[cnt] = dqget(inode->i_sb, id, cnt);
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)
999 inode->i_dquot[cnt] = dquot[cnt];
1000 dquot[cnt] = NODQUOT;
1001 inode->i_flags |= S_QUOTA;
1004 /* Put quotas which we didn't use */
1005 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1006 if (dquot[cnt] != NODQUOT)
1011 * Release all quota for the specified inode.
1013 * Note: this is a blocking operation.
1015 void dquot_drop(struct inode *inode)
1017 struct dquot *dquot;
1020 inode->i_flags &= ~S_QUOTA;
1021 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1022 if (inode->i_dquot[cnt] == NODQUOT)
1024 dquot = inode->i_dquot[cnt];
1025 inode->i_dquot[cnt] = NODQUOT;
1031 * This operation can block, but only after everything is updated
1033 int dquot_alloc_block(struct inode *inode, unsigned long number, char warn)
1035 int cnt, ret = NO_QUOTA;
1036 struct dquot *dquot[MAXQUOTAS];
1037 char warntype[MAXQUOTAS];
1039 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1040 dquot[cnt] = NODQUOT;
1041 warntype[cnt] = NOWARN;
1044 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1045 dquot[cnt] = dqduplicate(inode->i_dquot[cnt]);
1046 if (dquot[cnt] == NODQUOT)
1048 if (check_bdq(dquot[cnt], number, warn, warntype+cnt) == NO_QUOTA)
1051 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1052 if (dquot[cnt] == NODQUOT)
1054 dquot_incr_blocks(dquot[cnt], number);
1056 inode->i_blocks += number << (BLOCK_SIZE_BITS - 9);
1060 flush_warnings(dquot, warntype);
1061 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1062 if (dquot[cnt] != NODQUOT)
1068 * This operation can block, but only after everything is updated
1070 int dquot_alloc_inode(const struct inode *inode, unsigned long number)
1072 int cnt, ret = NO_QUOTA;
1073 struct dquot *dquot[MAXQUOTAS];
1074 char warntype[MAXQUOTAS];
1076 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1077 dquot[cnt] = NODQUOT;
1078 warntype[cnt] = NOWARN;
1081 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1082 dquot[cnt] = dqduplicate(inode -> i_dquot[cnt]);
1083 if (dquot[cnt] == NODQUOT)
1085 if (check_idq(dquot[cnt], number, warntype+cnt) == NO_QUOTA)
1089 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1090 if (dquot[cnt] == NODQUOT)
1092 dquot_incr_inodes(dquot[cnt], number);
1097 flush_warnings(dquot, warntype);
1098 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1099 if (dquot[cnt] != NODQUOT)
1105 * This is a non-blocking operation.
1107 void dquot_free_block(struct inode *inode, unsigned long number)
1110 struct dquot *dquot;
1113 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1114 dquot = dqduplicate(inode->i_dquot[cnt]);
1115 if (dquot == NODQUOT)
1117 dquot_decr_blocks(dquot, number);
1120 inode->i_blocks -= number << (BLOCK_SIZE_BITS - 9);
1125 * This is a non-blocking operation.
1127 void dquot_free_inode(const struct inode *inode, unsigned long number)
1130 struct dquot *dquot;
1133 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1134 dquot = dqduplicate(inode->i_dquot[cnt]);
1135 if (dquot == NODQUOT)
1137 dquot_decr_inodes(dquot, number);
1144 * Transfer the number of inode and blocks from one diskquota to an other.
1146 * This operation can block, but only after everything is updated
1148 int dquot_transfer(struct inode *inode, struct iattr *iattr)
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];
1157 /* Clear the arrays */
1158 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1159 transfer_to[cnt] = transfer_from[cnt] = NODQUOT;
1160 warntype[cnt] = NOWARN;
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))
1170 transfer_to[cnt] = dqget(inode->i_sb, iattr->ia_uid, cnt);
1175 transfer_to[cnt] = dqget(inode->i_sb, iattr->ia_gid, cnt);
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))
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)... */
1189 if (check_idq(transfer_to[cnt], 1, warntype+cnt) == NO_QUOTA ||
1190 check_bdq(transfer_to[cnt], blocks, 0, warntype+cnt) == NO_QUOTA)
1195 * Finally perform the needed transfer from transfer_from to transfer_to
1197 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1199 * Skip changes for same uid or gid or for non-existing quota-type.
1201 if (transfer_from[cnt] == NODQUOT || transfer_to[cnt] == NODQUOT)
1204 dquot_decr_inodes(transfer_from[cnt], 1);
1205 dquot_decr_blocks(transfer_from[cnt], blocks);
1207 dquot_incr_inodes(transfer_to[cnt], 1);
1208 dquot_incr_blocks(transfer_to[cnt], blocks);
1210 if (inode->i_dquot[cnt] == NODQUOT)
1212 inode->i_dquot[cnt] = transfer_to[cnt];
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
1217 transfer_to[cnt] = transfer_from[cnt];
1219 /* NOBLOCK END. From now on we can block as we wish */
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]);
1232 static int __init dquot_init(void)
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__);
1241 __initcall(dquot_init);
1244 * Definitions of diskquota operations.
1246 struct dquot_operations dquot_operations = {
1247 dquot_initialize, /* mandatory */
1248 dquot_drop, /* mandatory */
1256 static inline void set_enable_flags(struct quota_mount_options *dqopt, short type)
1260 dqopt->flags |= DQUOT_USR_ENABLED;
1263 dqopt->flags |= DQUOT_GRP_ENABLED;
1268 static inline void reset_enable_flags(struct quota_mount_options *dqopt, short type)
1272 dqopt->flags &= ~DQUOT_USR_ENABLED;
1275 dqopt->flags &= ~DQUOT_GRP_ENABLED;
1280 /* Function in inode.c - remove pointers to dquots in icache */
1281 extern void remove_dquot_ref(struct super_block *, short);
1284 * Turn quota off on a device. type == -1 ==> quotaoff for all types (umount)
1286 int quota_off(struct super_block *sb, short type)
1290 struct quota_mount_options *dqopt = sb_dqopt(sb);
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)
1300 if (!is_enabled(dqopt, cnt))
1302 reset_enable_flags(dqopt, cnt);
1304 /* Note: these are blocking operations */
1305 remove_dquot_ref(sb, cnt);
1306 invalidate_dquots(sb, cnt);
1308 filp = dqopt->files[cnt];
1309 dqopt->files[cnt] = (struct file *)NULL;
1310 dqopt->inode_expire[cnt] = 0;
1311 dqopt->block_expire[cnt] = 0;
1314 up(&dqopt->dqoff_sem);
1319 static inline int check_quotafile_size(loff_t size)
1321 ulong blocks = size >> BLOCK_SIZE_BITS;
1322 size_t off = size & (BLOCK_SIZE - 1);
1324 return !(((blocks % sizeof(struct dqblk)) * BLOCK_SIZE + off % sizeof(struct dqblk)) % sizeof(struct dqblk));
1327 static int quota_on(struct super_block *sb, short type, char *path)
1330 struct inode *inode;
1331 struct dquot *dquot;
1332 struct quota_mount_options *dqopt = sb_dqopt(sb);
1336 if (is_enabled(dqopt, type))
1339 down(&dqopt->dqoff_sem);
1340 tmp = getname(path);
1341 error = PTR_ERR(tmp);
1345 f = filp_open(tmp, O_RDWR, 0600);
1352 if (!f->f_op || !f->f_op->read || !f->f_op->write)
1354 inode = f->f_dentry->d_inode;
1356 if (!S_ISREG(inode->i_mode))
1359 if (inode->i_size == 0 || !check_quotafile_size(inode->i_size))
1361 /* We don't want quota on quota files */
1363 inode->i_flags |= S_NOQUOTA;
1365 dqopt->files[type] = f;
1366 set_enable_flags(dqopt, type);
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;
1373 sb->dq_op = &dquot_operations;
1374 add_dquot_ref(sb, type);
1376 up(&dqopt->dqoff_sem);
1380 filp_close(f, NULL);
1382 up(&dqopt->dqoff_sem);
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.
1393 asmlinkage long sys_quotactl(int cmd, const char *special, int id, caddr_t addr)
1395 int cmds = 0, type = 0, flags = 0;
1397 struct super_block *sb = NULL;
1401 cmds = cmd >> SUBCMDSHIFT;
1402 type = cmd & SUBCMDMASK;
1404 if ((u_int) type >= MAXQUOTAS)
1415 if (((type == USRQUOTA && current->euid != id) ||
1416 (type == GRPQUOTA && !in_egroup_p(id))) &&
1417 !capable(CAP_SYS_ADMIN))
1421 if (!capable(CAP_SYS_ADMIN))
1427 if (special != NULL || (cmds != Q_SYNC && cmds != Q_GETSTATS)) {
1429 struct nameidata nd;
1431 ret = user_path_walk(special, &nd);
1435 dev = nd.dentry->d_inode->i_rdev;
1436 mode = nd.dentry->d_inode->i_mode;
1443 sb = get_super(dev);
1451 ret = quota_on(sb, type, (char *) addr);
1454 ret = quota_off(sb, type);
1457 ret = get_quota(sb, id, type, (struct dqblk *) addr);
1466 flags |= SET_QLIMIT;
1469 ret = sync_dquots(dev, type);
1472 ret = get_stats(addr);
1475 ret = quota_root_squash(sb, type, (int *) addr);
1482 if (sb && sb_has_quota_enabled(sb, type))
1483 ret = set_dqblk(sb, id, type, flags, (struct dqblk *) addr);