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 * (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
)
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
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
)
108 return((dqopt
->flags
& DQUOT_USR_ENABLED
) != 0);
110 return((dqopt
->flags
& DQUOT_GRP_ENABLED
) != 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
;
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
)
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
)
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
);
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
);
175 static inline void remove_free_dquot(struct dquot
*dquot
)
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
);
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
;
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
) {
200 dquot
->dq_next
->dq_pprev
= dquot
->dq_pprev
;
201 *dquot
->dq_pprev
= dquot
->dq_next
;
202 dquot
->dq_pprev
= NULL
;
207 static void __wait_on_dquot(struct dquot
*dquot
)
209 DECLARE_WAITQUEUE(wait
, current
);
211 add_wait_queue(&dquot
->dq_wait
, &wait
);
213 set_current_state(TASK_UNINTERRUPTIBLE
);
214 if (dquot
->dq_flags
& DQ_LOCKED
) {
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
;
250 struct semaphore
*sem
= &dquot
->dq_sb
->s_dquot
.dqio_sem
;
253 if (!dquot
->dq_sb
) { /* Invalidated quota? */
258 filp
= dquot
->dq_sb
->s_dquot
.files
[type
];
259 offset
= dqoff(dquot
->dq_id
);
264 * Note: clear the DQ_MOD flag unconditionally,
265 * so we don't loop forever on failure.
267 dquot
->dq_flags
&= ~DQ_MOD
;
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
));
283 static void read_dquot(struct dquot
*dquot
)
285 short type
= dquot
->dq_type
;
290 filp
= dquot
->dq_sb
->s_dquot
.files
[type
];
291 if (filp
== (struct file
*)NULL
)
295 if (!dquot
->dq_sb
) /* Invalidated quota? */
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
);
302 filp
->f_op
->read(filp
, (char *)&dquot
->dq_dqb
, sizeof(struct dqblk
), &offset
);
303 up(&dquot
->dq_sb
->s_dquot
.dqio_sem
);
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
;
315 * Unhash and selectively clear the dquot structure,
316 * but preserve the use count, list pointers, and
319 void clear_dquot(struct dquot
*dquot
)
321 /* unhash it first */
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
;
335 next
= inuse_list
; /* Here it is better. Otherwise the restart doesn't have any sense ;-) */
337 while ((dquot
= next
) != NULL
) {
338 next
= dquot
->dq_next
;
339 if (dquot
->dq_dev
!= dev
)
341 if (dquot
->dq_type
!= type
)
343 if (!dquot
->dq_sb
) /* Already invalidated entry? */
345 if (dquot
->dq_flags
& DQ_LOCKED
) {
346 __wait_on_dquot(dquot
);
348 /* Set the flag for another pass. */
351 * Make sure it's still the same dquot.
353 if (dquot
->dq_dev
!= dev
)
355 if (dquot
->dq_type
!= type
)
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
)
368 need_restart
= 1; /* We slept on IO */
373 * If anything blocked, restart the operation
374 * to ensure we don't miss any dquots.
380 int sync_dquots(kdev_t dev
, short type
)
382 struct dquot
*dquot
, *next
, *ddquot
;
388 while ((dquot
= next
) != NULL
) {
389 next
= dquot
->dq_next
;
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_LOCKED
| DQ_MOD
)))
399 if ((ddquot
= dqduplicate(dquot
)) == NODQUOT
)
401 if (ddquot
->dq_flags
& DQ_MOD
)
404 /* Set the flag for another pass. */
408 * If anything blocked, restart the operation
409 * to ensure we don't miss any dquots.
418 /* NOTE: If you change this function please check whether dqput_blocks() works right... */
419 static void dqput(struct dquot
*dquot
)
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
],
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.
439 if (dquot
->dq_count
> 1) {
440 /* We have more than one user... We can simply decrement use count */
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
) {
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... */
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)
472 dquot
= kmem_cache_alloc(dquot_cachep
, SLAB_KERNEL
);
477 memset((caddr_t
)dquot
, 0, sizeof(struct dquot
));
478 init_waitqueue_head(&dquot
->dq_wait
);
479 /* all dquots go on the inuse_list */
481 put_dquot_head(dquot
);
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
))
499 myscore
= dquot
->dq_referenced
;
500 if (myscore
< bestscore
) {
508 static inline struct dquot
*find_best_free(void)
510 struct list_head
*tmp
= &free_dquots
;
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)
522 struct dquot
*get_empty_dquot(void)
525 int shrink
= 1; /* Number of times we should try to shrink dcache and icache */
528 dquot
= find_best_free();
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
);
540 /* unhash and selectively clear the structure */
545 if (nr_dquots
< max_dquots
)
549 dquot
= find_best_candidate_weighted();
553 * Try pruning the dcache to free up some dquots ...
556 printk(KERN_DEBUG
"get_empty_dquot: pruning dcache and icache\n");
563 printk("VFS: No free dquots, contact mvw@planets.elm.net\n");
564 sleep_on(&dquot_wait
);
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
))
578 if ((dquot
= find_dquot(hashent
, sb
->s_dev
, id
, type
)) == NULL
) {
580 dquot_updating
[hashent
]++;
581 empty
= get_empty_dquot();
582 if (!--dquot_updating
[hashent
])
583 wake_up(&update_wait
);
588 dquot
->dq_type
= type
;
589 dquot
->dq_dev
= sb
->s_dev
;
591 /* hash it first so it can be found */
595 if (!dquot
->dq_count
++) {
596 remove_free_dquot(dquot
);
598 dqstats
.cache_hits
++;
599 wait_on_dquot(dquot
);
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
614 dquot
->dq_referenced
++;
620 static struct dquot
*dqduplicate(struct dquot
*dquot
)
622 if (dquot
== NODQUOT
|| !dquot
->dq_sb
)
625 wait_on_dquot(dquot
);
630 dquot
->dq_referenced
++;
635 /* Check whether this inode is quota file */
636 static inline int is_quotafile(struct inode
*inode
)
639 struct quota_mount_options
*dqopt
= sb_dqopt(inode
->i_sb
);
644 files
= dqopt
->files
;
645 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++)
646 if (files
[cnt
] && files
[cnt
]->f_dentry
->d_inode
== inode
)
651 static int dqinit_needed(struct inode
*inode
, short type
)
655 if (!(S_ISREG(inode
->i_mode
) || S_ISDIR(inode
->i_mode
) || S_ISLNK(inode
->i_mode
)))
657 if (is_quotafile(inode
))
660 return inode
->i_dquot
[type
] == NODQUOT
;
661 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++)
662 if (inode
->i_dquot
[cnt
] == NODQUOT
)
667 static void add_dquot_ref(struct super_block
*sb
, short type
)
673 return; /* nothing to do */
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
);
681 inode
= filp
->f_dentry
->d_inode
;
684 if (filp
->f_mode
& FMODE_WRITE
&& dqinit_needed(inode
, type
)) {
686 sb
->dq_op
->initialize(inode
, type
);
687 inode
->i_flags
|= S_QUOTA
;
688 /* As we may have blocked we had better restart... */
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)
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
];
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
)
715 inode
->i_flags
&= ~S_QUOTA
;
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... */
724 dqput(dquot
); /* We have guaranteed we won't block */
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
;
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
);
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
;
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
;
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
) {
786 return current
->fsuid
== dquot
->dq_id
&& !(dquot
->dq_flags
& flag
);
788 return in_group_p(dquot
->dq_id
) && !(dquot
->dq_flags
& flag
);
793 static void print_warning(struct dquot
*dquot
, int flag
, const char *fmtstr
)
795 if (!need_print_warning(dquot
, flag
))
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
)
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");
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");
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
];
838 static int check_bdq(struct dquot
*dquot
, u_long blocks
, char prealloc
)
840 if (blocks
<= 0 || dquot
->dq_flags
& DQ_FAKE
)
843 if (dquot
->dq_bhardlimit
&&
844 (dquot
->dq_curblocks
+ blocks
) > dquot
->dq_bhardlimit
&&
845 !ignore_hardlimit(dquot
)) {
847 print_warning(dquot
, DQ_BLKS
, "%s: write failed, %s disk limit reached.\n");
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
)) {
856 print_warning(dquot
, DQ_BLKS
, "%s: write failed, %s disk quota exceeded too long.\n");
860 if (dquot
->dq_bsoftlimit
&&
861 (dquot
->dq_curblocks
+ blocks
) > dquot
->dq_bsoftlimit
&&
862 dquot
->dq_btime
== 0) {
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
];
869 * We don't allow preallocation to exceed softlimit so exceeding will
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
)
886 struct dqblk dq_dqblk
;
888 if (dqblk
== (struct dqblk
*)NULL
)
891 if (flags
& QUOTA_SYSCALL
) {
892 if (copy_from_user(&dq_dqblk
, dqblk
, sizeof(struct dqblk
)))
895 memcpy((caddr_t
)&dq_dqblk
, (caddr_t
)dqblk
, sizeof(struct dqblk
));
897 if (sb
&& (dquot
= dqget(sb
, id
, type
)) != NODQUOT
) {
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
;
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
;
933 dquot
->dq_flags
&= ~DQ_FAKE
;
935 dquot
->dq_flags
|= DQ_MOD
;
942 static int get_quota(struct super_block
*sb
, int id
, short type
, struct dqblk
*dqblk
)
947 if (!sb
|| !sb_has_quota_enabled(sb
, type
))
949 dquot
= dqget(sb
, id
, type
);
950 if (dquot
== NODQUOT
)
953 lock_dquot(dquot
); /* We must protect against invalidating the quota */
955 if (dqblk
&& !copy_to_user(dqblk
, &dquot
->dq_dqb
, sizeof(struct dqblk
)))
963 static int get_stats(caddr_t addr
)
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
)))
978 static int quota_root_squash(struct super_block
*sb
, short type
, int *addr
)
980 int new_value
, error
;
986 if (!copy_from_user(&new_value
, addr
, sizeof(int))) {
987 sb_dqopt(sb
)->rsquash
[type
] = new_value
;
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
)
1003 blksize_bits
= BLOCK_SIZE_BITS
;
1004 blocks
= (isize
>> blksize_bits
) + ((isize
& ~((1 << blksize_bits
)-1)) ? 1 : 0);
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 */
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;
1028 if (!S_ISREG(inode
->i_mode
) && !S_ISDIR(inode
->i_mode
) &&
1029 !S_ISLNK(inode
->i_mode
))
1032 /* We don't want to have quotas on quota files - nasty deadlocks possible */
1033 if (is_quotafile(inode
)) {
1037 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++) {
1038 if (type
!= -1 && cnt
!= type
)
1041 if (!sb_has_quota_enabled(inode
->i_sb
, cnt
))
1044 if (inode
->i_dquot
[cnt
] == NODQUOT
) {
1053 dquot
= dqget(inode
->i_sb
, id
, cnt
);
1054 if (dquot
== NODQUOT
)
1056 if (inode
->i_dquot
[cnt
] != NODQUOT
) {
1060 inode
->i_dquot
[cnt
] = dquot
;
1061 inode
->i_flags
|= S_QUOTA
;
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
;
1078 inode
->i_flags
&= ~S_QUOTA
;
1079 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++) {
1080 if (inode
->i_dquot
[cnt
] == NODQUOT
)
1082 dquot
= inode
->i_dquot
[cnt
];
1083 inode
->i_dquot
[cnt
] = NODQUOT
;
1090 * Note: this is a blocking operation.
1092 int dquot_alloc_block(const struct inode
*inode
, unsigned long number
, char warn
)
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
)
1101 lock_dquot(dquot
[cnt
]);
1102 if (check_bdq(dquot
[cnt
], number
, warn
))
1106 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++) {
1107 if (dquot
[cnt
] == NODQUOT
)
1109 dquot_incr_blocks(dquot
[cnt
], number
);
1110 unlock_dquot(dquot
[cnt
]);
1116 for (; cnt
>= 0; cnt
--) {
1117 if (dquot
[cnt
] == NODQUOT
)
1119 unlock_dquot(dquot
[cnt
]);
1126 * Note: this is a blocking operation.
1128 int dquot_alloc_inode(const struct inode
*inode
, unsigned long number
)
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
)
1137 lock_dquot(dquot
[cnt
]);
1138 if (check_idq(dquot
[cnt
], number
))
1142 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++) {
1143 if (dquot
[cnt
] == NODQUOT
)
1145 dquot_incr_inodes(dquot
[cnt
], number
);
1146 unlock_dquot(dquot
[cnt
]);
1152 for (; cnt
>= 0; cnt
--) {
1153 if (dquot
[cnt
] == NODQUOT
)
1155 unlock_dquot(dquot
[cnt
]);
1162 * Note: this is a blocking operation.
1164 void dquot_free_block(const struct inode
*inode
, unsigned long number
)
1167 struct dquot
*dquot
;
1169 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++) {
1170 dquot
= inode
->i_dquot
[cnt
];
1171 if (dquot
== NODQUOT
)
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
)
1184 struct dquot
*dquot
;
1186 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++) {
1187 dquot
= inode
->i_dquot
[cnt
];
1188 if (dquot
== NODQUOT
)
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
];
1207 int error
= -EDQUOT
;
1211 /* Arguably we could consider that as error, but... no fs - no quota */
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
))
1229 if (inode
->i_uid
== iattr
->ia_uid
)
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
);
1236 if (inode
->i_gid
== iattr
->ia_gid
)
1238 transfer_from
[cnt
] = dqget(inode
->i_sb
, inode
->i_gid
, cnt
);
1239 transfer_to
[cnt
] = dqget(inode
->i_sb
, iattr
->ia_gid
, cnt
);
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
;
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
]);
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
) {
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
);
1287 blocks
= (inode
->i_blocks
>> 1);
1288 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++) {
1289 if (!transfer_to
[cnt
])
1291 if (check_idq(transfer_to
[cnt
], 1) == NO_QUOTA
||
1292 check_bdq(transfer_to
[cnt
], blocks
, 0) == NO_QUOTA
) {
1298 if ((error
= notify_change(dentry
, iattr
)))
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
)
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
]);
1325 unlock_dquot(transfer_to
[cnt
]);
1326 dqput(transfer_to
[cnt
]);
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
]);
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 */
1370 static inline void set_enable_flags(struct quota_mount_options
*dqopt
, short type
)
1374 dqopt
->flags
|= DQUOT_USR_ENABLED
;
1377 dqopt
->flags
|= DQUOT_GRP_ENABLED
;
1382 static inline void reset_enable_flags(struct quota_mount_options
*dqopt
, short type
)
1386 dqopt
->flags
&= ~DQUOT_USR_ENABLED
;
1389 dqopt
->flags
&= ~DQUOT_GRP_ENABLED
;
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
)
1405 struct quota_mount_options
*dqopt
= sb_dqopt(sb
);
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
)
1415 if (!is_enabled(dqopt
, cnt
))
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
);
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
);
1441 up(&dqopt
->dqoff_sem
);
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
)
1457 struct inode
*inode
;
1458 struct dquot
*dquot
;
1459 struct quota_mount_options
*dqopt
= sb_dqopt(sb
);
1463 if (is_enabled(dqopt
, type
))
1466 down(&dqopt
->dqoff_sem
);
1467 tmp
= getname(path
);
1468 error
= PTR_ERR(tmp
);
1472 f
= filp_open(tmp
, O_RDWR
, 0600);
1479 if (!f
->f_op
->read
&& !f
->f_op
->write
)
1481 inode
= f
->f_dentry
->d_inode
;
1483 if (!S_ISREG(inode
->i_mode
))
1486 if (inode
->i_size
== 0 || !check_quotafile_size(inode
->i_size
))
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
;
1498 sb
->dq_op
= &dquot_operations
;
1499 add_dquot_ref(sb
, type
);
1501 up(&dqopt
->dqoff_sem
);
1505 filp_close(f
, NULL
);
1507 up(&dqopt
->dqoff_sem
);
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;
1522 struct super_block
*sb
= NULL
;
1526 cmds
= cmd
>> SUBCMDSHIFT
;
1527 type
= cmd
& SUBCMDMASK
;
1529 if ((u_int
) type
>= MAXQUOTAS
)
1540 if (((type
== USRQUOTA
&& current
->euid
!= id
) ||
1541 (type
== GRPQUOTA
&& in_egroup_p(id
))) &&
1542 !capable(CAP_SYS_RESOURCE
))
1546 if (!capable(CAP_SYS_RESOURCE
))
1552 if (special
!= NULL
|| (cmds
!= Q_SYNC
&& cmds
!= Q_GETSTATS
)) {
1554 struct nameidata nd
;
1556 ret
= user_path_walk(special
, &nd
);
1560 dev
= nd
.dentry
->d_inode
->i_rdev
;
1561 mode
= nd
.dentry
->d_inode
->i_mode
;
1567 sb
= get_super(dev
);
1573 ret
= sb
? quota_on(sb
, type
, (char *) addr
) : -ENODEV
;
1576 ret
= quota_off(sb
, type
);
1579 ret
= get_quota(sb
, id
, type
, (struct dqblk
*) addr
);
1588 flags
|= SET_QLIMIT
;
1591 ret
= sync_dquots(dev
, type
);
1594 ret
= get_stats(addr
);
1597 ret
= quota_root_squash(sb
, type
, (int *) addr
);
1603 flags
|= QUOTA_SYSCALL
;
1606 if (sb
&& sb_has_quota_enabled(sb
, type
))
1607 ret
= set_dqblk(sb
, id
, type
, flags
, (struct dqblk
*) addr
);