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 1.1.1.1 2005/04/11 02:50:44 jack Exp $
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)
192 if (list_empty(&dquot->dq_free))
194 list_del(&dquot->dq_free);
195 INIT_LIST_HEAD(&dquot->dq_free);
199 static inline void put_inuse(struct dquot *dquot)
201 /* We add to the back of inuse list so we don't have to restart
202 * when traversing this list and we block */
203 list_add(&dquot->dq_inuse, inuse_list.prev);
207 static inline void remove_inuse(struct dquot *dquot)
210 list_del(&dquot->dq_inuse);
213 static void __wait_on_dquot(struct dquot *dquot)
215 DECLARE_WAITQUEUE(wait, current);
217 add_wait_queue(&dquot->dq_wait_lock, &wait);
219 set_current_state(TASK_UNINTERRUPTIBLE);
220 if (dquot->dq_flags & DQ_LOCKED) {
224 remove_wait_queue(&dquot->dq_wait_lock, &wait);
225 current->state = TASK_RUNNING;
228 static inline void wait_on_dquot(struct dquot *dquot)
230 if (dquot->dq_flags & DQ_LOCKED)
231 __wait_on_dquot(dquot);
234 static inline void lock_dquot(struct dquot *dquot)
236 wait_on_dquot(dquot);
237 dquot->dq_flags |= DQ_LOCKED;
240 static inline void unlock_dquot(struct dquot *dquot)
242 dquot->dq_flags &= ~DQ_LOCKED;
243 wake_up(&dquot->dq_wait_lock);
246 static void __wait_dquot_unused(struct dquot *dquot)
248 DECLARE_WAITQUEUE(wait, current);
250 add_wait_queue(&dquot->dq_wait_free, &wait);
252 set_current_state(TASK_UNINTERRUPTIBLE);
253 if (dquot->dq_count) {
257 remove_wait_queue(&dquot->dq_wait_free, &wait);
258 current->state = TASK_RUNNING;
262 * We don't have to be afraid of deadlocks as we never have quotas on quota files...
264 static void write_dquot(struct dquot *dquot)
266 short type = dquot->dq_type;
271 struct semaphore *sem = &dquot->dq_sb->s_dquot.dqio_sem;
275 filp = dquot->dq_sb->s_dquot.files[type];
276 offset = dqoff(dquot->dq_id);
281 * Note: clear the DQ_MOD flag unconditionally,
282 * so we don't loop forever on failure.
284 memcpy(&dqbuf, &dquot->dq_dqb, sizeof(struct dqblk));
285 dquot->dq_flags &= ~DQ_MOD;
288 ret = filp->f_op->write(filp, (char *)&dqbuf,
289 sizeof(struct dqblk), &offset);
290 if (ret != sizeof(struct dqblk))
291 printk(KERN_WARNING "VFS: dquota write failed on dev %s\n",
292 kdevname(dquot->dq_dev));
299 static void read_dquot(struct dquot *dquot)
301 short type = dquot->dq_type;
306 filp = dquot->dq_sb->s_dquot.files[type];
307 if (filp == (struct file *)NULL)
311 if (!dquot->dq_sb) /* Invalidated quota? */
313 /* Now we are sure filp is valid - the dquot isn't invalidated */
314 down(&dquot->dq_sb->s_dquot.dqio_sem);
315 offset = dqoff(dquot->dq_id);
318 filp->f_op->read(filp, (char *)&dquot->dq_dqb, sizeof(struct dqblk), &offset);
319 up(&dquot->dq_sb->s_dquot.dqio_sem);
322 if (dquot->dq_bhardlimit == 0 && dquot->dq_bsoftlimit == 0 &&
323 dquot->dq_ihardlimit == 0 && dquot->dq_isoftlimit == 0)
324 dquot->dq_flags |= DQ_FAKE;
330 /* Invalidate all dquots on the list, wait for all users. Note that this function is called
331 * after quota is disabled so no new quota might be created. As we only insert to the end of
332 * inuse list, we don't have to restart searching... */
333 static void invalidate_dquots(struct super_block *sb, short type)
336 struct list_head *head;
339 for (head = inuse_list.next; head != &inuse_list; head = head->next) {
340 dquot = list_entry(head, struct dquot, dq_inuse);
341 if (dquot->dq_sb != sb)
343 if (dquot->dq_type != type)
345 dquot->dq_flags |= DQ_INVAL;
348 * Wait for any users of quota. As we have already cleared the flags in
349 * superblock and cleared all pointers from inodes we are assured
350 * that there will be no new users of this quota.
352 __wait_dquot_unused(dquot);
353 /* Quota now have no users and it has been written on last dqput() */
354 remove_dquot_hash(dquot);
355 remove_free_dquot(dquot);
357 kmem_cache_free(dquot_cachep, dquot);
362 int sync_dquots(kdev_t dev, short type)
364 struct list_head *head;
369 for (head = inuse_list.next; head != &inuse_list; head = head->next) {
370 dquot = list_entry(head, struct dquot, dq_inuse);
371 if (dev && dquot->dq_dev != dev)
373 if (type != -1 && dquot->dq_type != type)
375 if (!dquot->dq_sb) /* Invalidated? */
377 if (!(dquot->dq_flags & (DQ_MOD | DQ_LOCKED)))
379 /* Raise use count so quota won't be invalidated. We can't use dqduplicate() as it does too many tests */
381 if (dquot->dq_flags & DQ_LOCKED)
382 wait_on_dquot(dquot);
383 if (dquot->dq_flags & DQ_MOD)
393 /* Free unused dquots from cache */
394 static void prune_dqcache(int count)
396 struct list_head *head;
399 head = free_dquots.prev;
400 while (head != &free_dquots && count) {
401 dquot = list_entry(head, struct dquot, dq_free);
402 remove_dquot_hash(dquot);
403 remove_free_dquot(dquot);
405 kmem_cache_free(dquot_cachep, dquot);
407 head = free_dquots.prev;
411 int shrink_dqcache_memory(int priority, unsigned int gfp_mask)
414 prune_dqcache(nr_free_dquots / (priority + 1));
416 return kmem_cache_shrink(dquot_cachep);
419 /* NOTE: If you change this function please check whether dqput_blocks() works right... */
420 static void dqput(struct dquot *dquot)
424 if (!dquot->dq_count) {
425 printk("VFS: dqput: trying to free free dquot\n");
426 printk("VFS: device %s, dquot of %s %d\n",
427 kdevname(dquot->dq_dev), quotatypes[dquot->dq_type],
434 if (dquot->dq_count > 1) {
435 /* We have more than one user... We can simply decrement use count */
439 if (dquot->dq_flags & DQ_MOD) {
445 if (!list_empty(&dquot->dq_free)) {
446 printk(KERN_ERR "dqput: dquot already on free list??\n");
447 dquot->dq_count--; /* J.K. Just decrementing use count seems safer... */
451 /* If dquot is going to be invalidated invalidate_dquots() is going to free it so */
452 if (!(dquot->dq_flags & DQ_INVAL))
453 put_dquot_last(dquot); /* Place at end of LRU free queue */
454 wake_up(&dquot->dq_wait_free);
457 static struct dquot *get_empty_dquot(void)
461 dquot = kmem_cache_alloc(dquot_cachep, SLAB_KERNEL);
465 memset((caddr_t)dquot, 0, sizeof(struct dquot));
466 init_waitqueue_head(&dquot->dq_wait_free);
467 init_waitqueue_head(&dquot->dq_wait_lock);
468 INIT_LIST_HEAD(&dquot->dq_free);
469 INIT_LIST_HEAD(&dquot->dq_inuse);
470 INIT_LIST_HEAD(&dquot->dq_hash);
472 /* all dquots go on the inuse_list */
478 static struct dquot *dqget(struct super_block *sb, unsigned int id, short type)
480 unsigned int hashent = hashfn(sb->s_dev, id, type);
481 struct dquot *dquot, *empty = NODQUOT;
482 struct quota_mount_options *dqopt = sb_dqopt(sb);
485 if (!is_enabled(dqopt, type)) {
491 if ((dquot = find_dquot(hashent, sb->s_dev, id, type)) == NODQUOT) {
492 if (empty == NODQUOT) {
493 if ((empty = get_empty_dquot()) == NODQUOT)
494 schedule(); /* Try to wait for a moment... */
499 dquot->dq_type = type;
500 dquot->dq_dev = sb->s_dev;
502 /* hash it first so it can be found */
503 insert_dquot_hash(dquot);
506 if (!dquot->dq_count++)
507 remove_free_dquot(dquot);
508 dqstats.cache_hits++;
509 wait_on_dquot(dquot);
514 if (!dquot->dq_sb) { /* Has somebody invalidated entry under us? */
515 printk(KERN_ERR "VFS: dqget(): Quota invalidated in dqget()!\n");
519 dquot->dq_referenced++;
525 static struct dquot *dqduplicate(struct dquot *dquot)
527 if (dquot == NODQUOT)
531 printk(KERN_ERR "VFS: dqduplicate(): Invalidated quota to be duplicated!\n");
535 if (dquot->dq_flags & DQ_LOCKED)
536 printk(KERN_ERR "VFS: dqduplicate(): Locked quota to be duplicated!\n");
537 dquot->dq_referenced++;
542 static int dqinit_needed(struct inode *inode, short type)
546 if (IS_NOQUOTA(inode))
549 return inode->i_dquot[type] == NODQUOT;
550 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
551 if (inode->i_dquot[cnt] == NODQUOT)
556 static void add_dquot_ref(struct super_block *sb, short type)
561 return; /* nothing to do */
565 for (p = sb->s_files.next; p != &sb->s_files; p = p->next) {
566 struct file *filp = list_entry(p, struct file, f_list);
567 struct inode *inode = filp->f_dentry->d_inode;
568 if (filp->f_mode & FMODE_WRITE && dqinit_needed(inode, type)) {
569 struct vfsmount *mnt = mntget(filp->f_vfsmnt);
570 struct dentry *dentry = dget(filp->f_dentry);
572 sb->dq_op->initialize(inode, type);
575 /* As we may have blocked we had better restart... */
582 /* Return 0 if dqput() won't block (note that 1 doesn't necessarily mean blocking) */
583 static inline int dqput_blocks(struct dquot *dquot)
585 if (dquot->dq_count == 1)
590 /* Remove references to dquots from inode - add dquot to list for freeing if needed */
591 int remove_inode_dquot_ref(struct inode *inode, short type, struct list_head *tofree_head)
593 struct dquot *dquot = inode->i_dquot[type];
596 inode->i_dquot[type] = NODQUOT;
597 /* any other quota in use? */
598 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
599 if (inode->i_dquot[cnt] != NODQUOT)
602 inode->i_flags &= ~S_QUOTA;
604 if (dquot != NODQUOT) {
605 if (dqput_blocks(dquot)) {
606 if (dquot->dq_count != 1)
607 printk(KERN_WARNING "VFS: Adding dquot with dq_count %d to dispose list.\n", dquot->dq_count);
608 list_add(&dquot->dq_free, tofree_head); /* As dquot must have currently users it can't be on the free list... */
612 dqput(dquot); /* We have guaranteed we won't block */
617 /* Free list of dquots - called from inode.c */
618 void put_dquot_list(struct list_head *tofree_head)
620 struct list_head *act_head;
624 act_head = tofree_head->next;
625 /* So now we have dquots on the list... Just free them */
626 while (act_head != tofree_head) {
627 dquot = list_entry(act_head, struct dquot, dq_free);
628 act_head = act_head->next;
629 list_del(&dquot->dq_free); /* Remove dquot from the list so we won't have problems... */
630 INIT_LIST_HEAD(&dquot->dq_free);
636 static inline void dquot_incr_inodes(struct dquot *dquot, unsigned long number)
638 dquot->dq_curinodes += number;
639 dquot->dq_flags |= DQ_MOD;
642 static inline void dquot_incr_blocks(struct dquot *dquot, unsigned long number)
644 dquot->dq_curblocks += number;
645 dquot->dq_flags |= DQ_MOD;
648 static inline void dquot_decr_inodes(struct dquot *dquot, unsigned long number)
650 if (dquot->dq_curinodes > number)
651 dquot->dq_curinodes -= number;
653 dquot->dq_curinodes = 0;
654 if (dquot->dq_curinodes < dquot->dq_isoftlimit)
655 dquot->dq_itime = (time_t) 0;
656 dquot->dq_flags &= ~DQ_INODES;
657 dquot->dq_flags |= DQ_MOD;
660 static inline void dquot_decr_blocks(struct dquot *dquot, unsigned long number)
662 if (dquot->dq_curblocks > number)
663 dquot->dq_curblocks -= number;
665 dquot->dq_curblocks = 0;
666 if (dquot->dq_curblocks < dquot->dq_bsoftlimit)
667 dquot->dq_btime = (time_t) 0;
668 dquot->dq_flags &= ~DQ_BLKS;
669 dquot->dq_flags |= DQ_MOD;
672 static inline int need_print_warning(struct dquot *dquot, int flag)
674 switch (dquot->dq_type) {
676 return current->fsuid == dquot->dq_id && !(dquot->dq_flags & flag);
678 return in_group_p(dquot->dq_id) && !(dquot->dq_flags & flag);
683 /* Values of warnings */
686 #define ISOFTLONGWARN 2
689 #define BSOFTLONGWARN 5
692 /* Print warning to user which exceeded quota */
693 static void print_warning(struct dquot *dquot, const char warntype)
696 int flag = (warntype == BHARDWARN || warntype == BSOFTLONGWARN) ? DQ_BLKS :
697 ((warntype == IHARDWARN || warntype == ISOFTLONGWARN) ? DQ_INODES : 0);
699 if (!need_print_warning(dquot, flag))
701 dquot->dq_flags |= flag;
702 tty_write_message(current->tty, (char *)bdevname(dquot->dq_sb->s_dev));
703 if (warntype == ISOFTWARN || warntype == BSOFTWARN)
704 tty_write_message(current->tty, ": warning, ");
706 tty_write_message(current->tty, ": write failed, ");
707 tty_write_message(current->tty, quotatypes[dquot->dq_type]);
710 msg = " file limit reached.\n";
713 msg = " file quota exceeded too long.\n";
716 msg = " file quota exceeded.\n";
719 msg = " block limit reached.\n";
722 msg = " block quota exceeded too long.\n";
725 msg = " block quota exceeded.\n";
728 tty_write_message(current->tty, msg);
731 static inline void flush_warnings(struct dquot **dquots, char *warntype)
735 for (i = 0; i < MAXQUOTAS; i++)
736 if (dquots[i] != NODQUOT && warntype[i] != NOWARN)
737 print_warning(dquots[i], warntype[i]);
740 static inline char ignore_hardlimit(struct dquot *dquot)
742 return capable(CAP_SYS_RESOURCE) && !dquot->dq_sb->s_dquot.rsquash[dquot->dq_type];
745 static int check_idq(struct dquot *dquot, ulong inodes, char *warntype)
748 if (inodes <= 0 || dquot->dq_flags & DQ_FAKE)
751 if (dquot->dq_ihardlimit &&
752 (dquot->dq_curinodes + inodes) > dquot->dq_ihardlimit &&
753 !ignore_hardlimit(dquot)) {
754 *warntype = IHARDWARN;
758 if (dquot->dq_isoftlimit &&
759 (dquot->dq_curinodes + inodes) > dquot->dq_isoftlimit &&
760 dquot->dq_itime && CURRENT_TIME >= dquot->dq_itime &&
761 !ignore_hardlimit(dquot)) {
762 *warntype = ISOFTLONGWARN;
766 if (dquot->dq_isoftlimit &&
767 (dquot->dq_curinodes + inodes) > dquot->dq_isoftlimit &&
768 dquot->dq_itime == 0) {
769 *warntype = ISOFTWARN;
770 dquot->dq_itime = CURRENT_TIME + dquot->dq_sb->s_dquot.inode_expire[dquot->dq_type];
776 static int check_bdq(struct dquot *dquot, ulong blocks, char prealloc, char *warntype)
779 if (blocks <= 0 || dquot->dq_flags & DQ_FAKE)
782 if (dquot->dq_bhardlimit &&
783 (dquot->dq_curblocks + blocks) > dquot->dq_bhardlimit &&
784 !ignore_hardlimit(dquot)) {
786 *warntype = BHARDWARN;
790 if (dquot->dq_bsoftlimit &&
791 (dquot->dq_curblocks + blocks) > dquot->dq_bsoftlimit &&
792 dquot->dq_btime && CURRENT_TIME >= dquot->dq_btime &&
793 !ignore_hardlimit(dquot)) {
795 *warntype = BSOFTLONGWARN;
799 if (dquot->dq_bsoftlimit &&
800 (dquot->dq_curblocks + blocks) > dquot->dq_bsoftlimit &&
801 dquot->dq_btime == 0) {
803 *warntype = BSOFTWARN;
804 dquot->dq_btime = CURRENT_TIME + dquot->dq_sb->s_dquot.block_expire[dquot->dq_type];
808 * We don't allow preallocation to exceed softlimit so exceeding will
818 * Initialize a dquot-struct with new quota info. This is used by the
819 * system call interface functions.
821 static int set_dqblk(struct super_block *sb, int id, short type, int flags, struct dqblk *dqblk)
825 struct dqblk dq_dqblk;
827 if (copy_from_user(&dq_dqblk, dqblk, sizeof(struct dqblk)))
830 if (sb && (dquot = dqget(sb, id, type)) != NODQUOT) {
831 /* We can't block while changing quota structure... */
832 if (id > 0 && ((flags & SET_QUOTA) || (flags & SET_QLIMIT))) {
833 dquot->dq_bhardlimit = dq_dqblk.dqb_bhardlimit;
834 dquot->dq_bsoftlimit = dq_dqblk.dqb_bsoftlimit;
835 dquot->dq_ihardlimit = dq_dqblk.dqb_ihardlimit;
836 dquot->dq_isoftlimit = dq_dqblk.dqb_isoftlimit;
839 if ((flags & SET_QUOTA) || (flags & SET_USE)) {
840 if (dquot->dq_isoftlimit &&
841 dquot->dq_curinodes < dquot->dq_isoftlimit &&
842 dq_dqblk.dqb_curinodes >= dquot->dq_isoftlimit)
843 dquot->dq_itime = CURRENT_TIME + dquot->dq_sb->s_dquot.inode_expire[type];
844 dquot->dq_curinodes = dq_dqblk.dqb_curinodes;
845 if (dquot->dq_curinodes < dquot->dq_isoftlimit)
846 dquot->dq_flags &= ~DQ_INODES;
847 if (dquot->dq_bsoftlimit &&
848 dquot->dq_curblocks < dquot->dq_bsoftlimit &&
849 dq_dqblk.dqb_curblocks >= dquot->dq_bsoftlimit)
850 dquot->dq_btime = CURRENT_TIME + dquot->dq_sb->s_dquot.block_expire[type];
851 dquot->dq_curblocks = dq_dqblk.dqb_curblocks;
852 if (dquot->dq_curblocks < dquot->dq_bsoftlimit)
853 dquot->dq_flags &= ~DQ_BLKS;
857 dquot->dq_sb->s_dquot.block_expire[type] = dquot->dq_btime = dq_dqblk.dqb_btime;
858 dquot->dq_sb->s_dquot.inode_expire[type] = dquot->dq_itime = dq_dqblk.dqb_itime;
861 if (dq_dqblk.dqb_bhardlimit == 0 && dq_dqblk.dqb_bsoftlimit == 0 &&
862 dq_dqblk.dqb_ihardlimit == 0 && dq_dqblk.dqb_isoftlimit == 0)
863 dquot->dq_flags |= DQ_FAKE;
865 dquot->dq_flags &= ~DQ_FAKE;
867 dquot->dq_flags |= DQ_MOD;
873 static int get_quota(struct super_block *sb, int id, short type, struct dqblk *dqblk)
879 if (!sb || !sb_has_quota_enabled(sb, type))
881 dquot = dqget(sb, id, type);
882 if (dquot == NODQUOT)
885 memcpy(&data, &dquot->dq_dqb, sizeof(struct dqblk)); /* We copy data to preserve them from changing */
888 if (dqblk && !copy_to_user(dqblk, &data, sizeof(struct dqblk)))
894 static int get_stats(caddr_t addr)
897 struct dqstats stats;
899 dqstats.allocated_dquots = nr_dquots;
900 dqstats.free_dquots = nr_free_dquots;
902 /* make a copy, in case we page-fault in user space */
903 memcpy(&stats, &dqstats, sizeof(struct dqstats));
904 if (!copy_to_user(addr, &stats, sizeof(struct dqstats)))
909 static int quota_root_squash(struct super_block *sb, short type, int *addr)
911 int new_value, error;
917 if (!copy_from_user(&new_value, addr, sizeof(int))) {
918 sb_dqopt(sb)->rsquash[type] = new_value;
924 #if 0 /* We are not going to support filesystems without i_blocks... */
926 * This is a simple algorithm that calculates the size of a file in blocks.
927 * This is only used on filesystems that do not have an i_blocks count.
929 static u_long isize_to_blocks(loff_t isize, size_t blksize_bits)
935 blksize_bits = BLOCK_SIZE_BITS;
936 blocks = (isize >> blksize_bits) + ((isize & ~((1 << blksize_bits)-1)) ? 1 : 0);
938 indirect = ((blocks - 11) >> 8) + 1; /* single indirect blocks */
939 if (blocks > (10 + 256)) {
940 indirect += ((blocks - 267) >> 16) + 1; /* double indirect blocks */
941 if (blocks > (10 + 256 + (256 << 8)))
942 indirect++; /* triple indirect blocks */
951 * Externally referenced functions through dquot_operations in inode.
953 * Note: this is a blocking operation.
955 void dquot_initialize(struct inode *inode, short type)
957 struct dquot *dquot[MAXQUOTAS];
961 if (IS_NOQUOTA(inode))
963 /* Build list of quotas to initialize... We can block here */
964 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
965 dquot[cnt] = NODQUOT;
966 if (type != -1 && cnt != type)
968 if (!sb_has_quota_enabled(inode->i_sb, cnt))
970 if (inode->i_dquot[cnt] == NODQUOT) {
979 dquot[cnt] = dqget(inode->i_sb, id, cnt);
982 /* NOBLOCK START: Here we shouldn't block */
983 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
984 if (dquot[cnt] == NODQUOT || !sb_has_quota_enabled(inode->i_sb, cnt) || inode->i_dquot[cnt] != NODQUOT)
986 inode->i_dquot[cnt] = dquot[cnt];
987 dquot[cnt] = NODQUOT;
988 inode->i_flags |= S_QUOTA;
991 /* Put quotas which we didn't use */
992 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
993 if (dquot[cnt] != NODQUOT)
998 * Release all quota for the specified inode.
1000 * Note: this is a blocking operation.
1002 void dquot_drop(struct inode *inode)
1004 struct dquot *dquot;
1007 inode->i_flags &= ~S_QUOTA;
1008 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1009 if (inode->i_dquot[cnt] == NODQUOT)
1011 dquot = inode->i_dquot[cnt];
1012 inode->i_dquot[cnt] = NODQUOT;
1018 * This operation can block, but only after everything is updated
1020 int dquot_alloc_block(struct inode *inode, unsigned long number, char warn)
1022 int cnt, ret = NO_QUOTA;
1023 struct dquot *dquot[MAXQUOTAS];
1024 char warntype[MAXQUOTAS];
1026 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1027 dquot[cnt] = NODQUOT;
1028 warntype[cnt] = NOWARN;
1031 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1032 dquot[cnt] = dqduplicate(inode->i_dquot[cnt]);
1033 if (dquot[cnt] == NODQUOT)
1035 if (check_bdq(dquot[cnt], number, warn, warntype+cnt) == NO_QUOTA)
1038 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1039 if (dquot[cnt] == NODQUOT)
1041 dquot_incr_blocks(dquot[cnt], number);
1043 inode->i_blocks += number << (BLOCK_SIZE_BITS - 9);
1047 flush_warnings(dquot, warntype);
1048 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1049 if (dquot[cnt] != NODQUOT)
1055 * This operation can block, but only after everything is updated
1057 int dquot_alloc_inode(const struct inode *inode, unsigned long number)
1059 int cnt, ret = NO_QUOTA;
1060 struct dquot *dquot[MAXQUOTAS];
1061 char warntype[MAXQUOTAS];
1063 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1064 dquot[cnt] = NODQUOT;
1065 warntype[cnt] = NOWARN;
1068 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1069 dquot[cnt] = dqduplicate(inode -> i_dquot[cnt]);
1070 if (dquot[cnt] == NODQUOT)
1072 if (check_idq(dquot[cnt], number, warntype+cnt) == NO_QUOTA)
1076 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1077 if (dquot[cnt] == NODQUOT)
1079 dquot_incr_inodes(dquot[cnt], number);
1084 flush_warnings(dquot, warntype);
1085 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1086 if (dquot[cnt] != NODQUOT)
1092 * This is a non-blocking operation.
1094 void dquot_free_block(struct inode *inode, unsigned long number)
1097 struct dquot *dquot;
1100 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1101 dquot = dqduplicate(inode->i_dquot[cnt]);
1102 if (dquot == NODQUOT)
1104 dquot_decr_blocks(dquot, number);
1107 inode->i_blocks -= number << (BLOCK_SIZE_BITS - 9);
1112 * This is a non-blocking operation.
1114 void dquot_free_inode(const struct inode *inode, unsigned long number)
1117 struct dquot *dquot;
1120 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1121 dquot = dqduplicate(inode->i_dquot[cnt]);
1122 if (dquot == NODQUOT)
1124 dquot_decr_inodes(dquot, number);
1131 * Transfer the number of inode and blocks from one diskquota to an other.
1133 * This operation can block, but only after everything is updated
1135 int dquot_transfer(struct inode *inode, struct iattr *iattr)
1137 unsigned long blocks;
1138 struct dquot *transfer_from[MAXQUOTAS];
1139 struct dquot *transfer_to[MAXQUOTAS];
1140 int cnt, ret = NO_QUOTA, chuid = (iattr->ia_valid & ATTR_UID) && inode->i_uid != iattr->ia_uid,
1141 chgid = (iattr->ia_valid & ATTR_GID) && inode->i_gid != iattr->ia_gid;
1142 char warntype[MAXQUOTAS];
1144 /* Clear the arrays */
1145 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1146 transfer_to[cnt] = transfer_from[cnt] = NODQUOT;
1147 warntype[cnt] = NOWARN;
1149 /* First build the transfer_to list - here we can block on reading of dquots... */
1150 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1151 if (!sb_has_quota_enabled(inode->i_sb, cnt))
1157 transfer_to[cnt] = dqget(inode->i_sb, iattr->ia_uid, cnt);
1162 transfer_to[cnt] = dqget(inode->i_sb, iattr->ia_gid, cnt);
1166 /* NOBLOCK START: From now on we shouldn't block */
1167 blocks = (inode->i_blocks >> 1);
1168 /* Build the transfer_from list and check the limits */
1169 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1170 /* The second test can fail when quotaoff is in progress... */
1171 if (transfer_to[cnt] == NODQUOT || !sb_has_quota_enabled(inode->i_sb, cnt))
1173 transfer_from[cnt] = dqduplicate(inode->i_dquot[cnt]);
1174 if (transfer_from[cnt] == NODQUOT) /* Can happen on quotafiles (quota isn't initialized on them)... */
1176 if (check_idq(transfer_to[cnt], 1, warntype+cnt) == NO_QUOTA ||
1177 check_bdq(transfer_to[cnt], blocks, 0, warntype+cnt) == NO_QUOTA)
1182 * Finally perform the needed transfer from transfer_from to transfer_to
1184 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1186 * Skip changes for same uid or gid or for non-existing quota-type.
1188 if (transfer_from[cnt] == NODQUOT || transfer_to[cnt] == NODQUOT)
1191 dquot_decr_inodes(transfer_from[cnt], 1);
1192 dquot_decr_blocks(transfer_from[cnt], blocks);
1194 dquot_incr_inodes(transfer_to[cnt], 1);
1195 dquot_incr_blocks(transfer_to[cnt], blocks);
1197 if (inode->i_dquot[cnt] == NODQUOT)
1199 inode->i_dquot[cnt] = transfer_to[cnt];
1201 * We've got to release transfer_from[] twice - once for dquot_transfer() and
1202 * once for inode. We don't want to release transfer_to[] as it's now placed in inode
1204 transfer_to[cnt] = transfer_from[cnt];
1206 /* NOBLOCK END. From now on we can block as we wish */
1209 flush_warnings(transfer_to, warntype);
1210 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1211 if (transfer_to[cnt] != NODQUOT)
1212 dqput(transfer_to[cnt]);
1213 if (transfer_from[cnt] != NODQUOT)
1214 dqput(transfer_from[cnt]);
1219 static int __init dquot_init(void)
1223 for (i = 0; i < NR_DQHASH; i++)
1224 INIT_LIST_HEAD(dquot_hash + i);
1225 printk(KERN_NOTICE "VFS: Diskquotas version %s initialized\n", __DQUOT_VERSION__);
1228 __initcall(dquot_init);
1231 * Definitions of diskquota operations.
1233 struct dquot_operations dquot_operations = {
1234 dquot_initialize, /* mandatory */
1235 dquot_drop, /* mandatory */
1243 static inline void set_enable_flags(struct quota_mount_options *dqopt, short type)
1247 dqopt->flags |= DQUOT_USR_ENABLED;
1250 dqopt->flags |= DQUOT_GRP_ENABLED;
1255 static inline void reset_enable_flags(struct quota_mount_options *dqopt, short type)
1259 dqopt->flags &= ~DQUOT_USR_ENABLED;
1262 dqopt->flags &= ~DQUOT_GRP_ENABLED;
1267 /* Function in inode.c - remove pointers to dquots in icache */
1268 extern void remove_dquot_ref(struct super_block *, short);
1271 * Turn quota off on a device. type == -1 ==> quotaoff for all types (umount)
1273 int quota_off(struct super_block *sb, short type)
1277 struct quota_mount_options *dqopt = sb_dqopt(sb);
1283 /* We need to serialize quota_off() for device */
1284 down(&dqopt->dqoff_sem);
1285 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1286 if (type != -1 && cnt != type)
1288 if (!is_enabled(dqopt, cnt))
1290 reset_enable_flags(dqopt, cnt);
1292 /* Note: these are blocking operations */
1293 remove_dquot_ref(sb, cnt);
1294 invalidate_dquots(sb, cnt);
1296 filp = dqopt->files[cnt];
1297 dqopt->files[cnt] = (struct file *)NULL;
1298 dqopt->inode_expire[cnt] = 0;
1299 dqopt->block_expire[cnt] = 0;
1302 up(&dqopt->dqoff_sem);
1308 static inline int check_quotafile_size(loff_t size)
1310 ulong blocks = size >> BLOCK_SIZE_BITS;
1311 size_t off = size & (BLOCK_SIZE - 1);
1313 return !(((blocks % sizeof(struct dqblk)) * BLOCK_SIZE + off % sizeof(struct dqblk)) % sizeof(struct dqblk));
1316 static int quota_on(struct super_block *sb, short type, char *path)
1319 struct inode *inode;
1320 struct dquot *dquot;
1321 struct quota_mount_options *dqopt = sb_dqopt(sb);
1325 if (is_enabled(dqopt, type))
1328 down(&dqopt->dqoff_sem);
1329 tmp = getname(path);
1330 error = PTR_ERR(tmp);
1334 f = filp_open(tmp, O_RDWR, 0600);
1341 if (!f->f_op || !f->f_op->read || !f->f_op->write)
1343 inode = f->f_dentry->d_inode;
1345 if (!S_ISREG(inode->i_mode))
1348 if (inode->i_size == 0 || !check_quotafile_size(inode->i_size))
1350 /* We don't want quota on quota files */
1352 inode->i_flags |= S_NOQUOTA;
1354 dqopt->files[type] = f;
1355 sb->dq_op = &dquot_operations;
1356 set_enable_flags(dqopt, type);
1358 dquot = dqget(sb, 0, type);
1359 dqopt->inode_expire[type] = (dquot != NODQUOT) ? dquot->dq_itime : MAX_IQ_TIME;
1360 dqopt->block_expire[type] = (dquot != NODQUOT) ? dquot->dq_btime : MAX_DQ_TIME;
1363 add_dquot_ref(sb, type);
1365 up(&dqopt->dqoff_sem);
1369 filp_close(f, NULL);
1371 up(&dqopt->dqoff_sem);
1377 * This is the system call interface. This communicates with
1378 * the user-level programs. Currently this only supports diskquota
1379 * calls. Maybe we need to add the process quotas etc. in the future,
1380 * but we probably should use rlimits for that.
1382 asmlinkage long sys_quotactl(int cmd, const char *special, int id, caddr_t addr)
1384 int cmds = 0, type = 0, flags = 0;
1386 struct super_block *sb = NULL;
1390 cmds = cmd >> SUBCMDSHIFT;
1391 type = cmd & SUBCMDMASK;
1393 if ((u_int) type >= MAXQUOTAS)
1404 if (((type == USRQUOTA && current->euid != id) ||
1405 (type == GRPQUOTA && !in_egroup_p(id))) &&
1406 !capable(CAP_SYS_ADMIN))
1410 if (!capable(CAP_SYS_ADMIN))
1416 if (special != NULL || (cmds != Q_SYNC && cmds != Q_GETSTATS)) {
1418 struct nameidata nd;
1420 ret = user_path_walk(special, &nd);
1424 dev = nd.dentry->d_inode->i_rdev;
1425 mode = nd.dentry->d_inode->i_mode;
1432 sb = get_super(dev);
1440 ret = quota_on(sb, type, (char *) addr);
1443 ret = quota_off(sb, type);
1446 ret = get_quota(sb, id, type, (struct dqblk *) addr);
1455 flags |= SET_QLIMIT;
1458 ret = sync_dquots(dev, type);
1461 ret = get_stats(addr);
1464 ret = quota_root_squash(sb, type, (int *) addr);
1471 if (sb && sb_has_quota_enabled(sb, type))
1472 ret = set_dqblk(sb, id, type, flags, (struct dqblk *) addr);