Import 2.3.12pre9
[davej-history.git] / fs / dquot.c
blob9dfbac082a679409ed49d833afc2a09215336cd5
1 /*
2 * Implementation of the diskquota system for the LINUX operating
3 * system. QUOTA is implemented using the BSD system call interface as
4 * the means of communication with the user level. Currently only the
5 * ext2 filesystem has support for disk quotas. Other filesystems may
6 * be added in the future. This file contains the generic routines
7 * called by the different filesystems on allocation of an inode or
8 * block. These routines take care of the administration needed to
9 * have a consistent diskquota tracking system. The ideas of both
10 * user and group quotas are based on the Melbourne quota system as
11 * used on BSD derived systems. The internal implementation is
12 * based on one of the several variants of the LINUX inode-subsystem
13 * with added complexity of the diskquota system.
15 * Version: $Id: dquot.c,v 6.3 1996/11/17 18:35:34 mvw Exp mvw $
17 * Author: Marco van Wieringen <mvw@planets.elm.net>
19 * Fixes: Dmitry Gorodchanin <pgmdsg@ibi.com>, 11 Feb 96
21 * Revised list management to avoid races
22 * -- Bill Hawes, <whawes@star.net>, 9/98
24 * (C) Copyright 1994 - 1997 Marco van Wieringen
27 #include <linux/errno.h>
28 #include <linux/kernel.h>
29 #include <linux/sched.h>
31 #include <linux/types.h>
32 #include <linux/string.h>
33 #include <linux/fcntl.h>
34 #include <linux/stat.h>
35 #include <linux/tty.h>
36 #include <linux/file.h>
37 #include <linux/malloc.h>
38 #include <linux/mount.h>
39 #include <linux/smp.h>
40 #include <linux/smp_lock.h>
41 #include <linux/init.h>
42 #include <linux/slab.h>
44 #include <asm/uaccess.h>
46 #define __DQUOT_VERSION__ "dquot_6.4.0"
48 int nr_dquots = 0, nr_free_dquots = 0;
49 int max_dquots = NR_DQUOTS;
51 static char quotamessage[MAX_QUOTA_MESSAGE];
52 static char *quotatypes[] = INITQFNAMES;
54 static kmem_cache_t *dquot_cachep;
57 * Dquot List Management:
58 * The quota code uses three lists for dquot management: the inuse_list,
59 * free_dquots, and dquot_hash[] array. A single dquot structure may be
60 * on all three lists, depending on its current state.
62 * All dquots are placed on the inuse_list when first created, and this
63 * list is used for the sync and invalidate operations, which must look
64 * at every dquot.
66 * Unused dquots (dq_count == 0) are added to the free_dquots list when
67 * freed, and this list is searched whenever we need an available dquot.
68 * Dquots are removed from the list as soon as they are used again, and
69 * nr_free_dquots gives the number of dquots on the list.
71 * Dquots with a specific identity (device, type and id) are placed on
72 * one of the dquot_hash[] hash chains. The provides an efficient search
73 * mechanism to lcoate a specific dquot.
76 static struct dquot *inuse_list = NULL;
77 LIST_HEAD(free_dquots);
78 static struct dquot *dquot_hash[NR_DQHASH];
79 static int dquot_updating[NR_DQHASH];
81 static struct dqstats dqstats;
82 static DECLARE_WAIT_QUEUE_HEAD(dquot_wait);
83 static DECLARE_WAIT_QUEUE_HEAD(update_wait);
85 static inline char is_enabled(struct vfsmount *vfsmnt, short type)
87 switch (type) {
88 case USRQUOTA:
89 return((vfsmnt->mnt_dquot.flags & DQUOT_USR_ENABLED) != 0);
90 case GRPQUOTA:
91 return((vfsmnt->mnt_dquot.flags & DQUOT_GRP_ENABLED) != 0);
93 return(0);
96 static inline char sb_has_quota_enabled(struct super_block *sb, short type)
98 struct vfsmount *vfsmnt;
100 return((vfsmnt = lookup_vfsmnt(sb->s_dev)) != (struct vfsmount *)NULL && is_enabled(vfsmnt, type));
103 static inline char dev_has_quota_enabled(kdev_t dev, short type)
105 struct vfsmount *vfsmnt;
107 return((vfsmnt = lookup_vfsmnt(dev)) != (struct vfsmount *)NULL && is_enabled(vfsmnt, type));
110 static inline int const hashfn(kdev_t dev, unsigned int id, short type)
112 return((HASHDEV(dev) ^ id) * (MAXQUOTAS - type)) % NR_DQHASH;
115 static inline void insert_dquot_hash(struct dquot *dquot)
117 struct dquot **htable;
119 htable = &dquot_hash[hashfn(dquot->dq_dev, dquot->dq_id, dquot->dq_type)];
120 if ((dquot->dq_hash_next = *htable) != NULL)
121 (*htable)->dq_hash_pprev = &dquot->dq_hash_next;
122 *htable = dquot;
123 dquot->dq_hash_pprev = htable;
126 static inline void hash_dquot(struct dquot *dquot)
128 insert_dquot_hash(dquot);
131 static inline void unhash_dquot(struct dquot *dquot)
133 if (dquot->dq_hash_pprev) {
134 if (dquot->dq_hash_next)
135 dquot->dq_hash_next->dq_hash_pprev = dquot->dq_hash_pprev;
136 *(dquot->dq_hash_pprev) = dquot->dq_hash_next;
137 dquot->dq_hash_pprev = NULL;
141 static inline struct dquot *find_dquot(unsigned int hashent, kdev_t dev, unsigned int id, short type)
143 struct dquot *dquot;
145 for (dquot = dquot_hash[hashent]; dquot; dquot = dquot->dq_hash_next)
146 if (dquot->dq_dev == dev && dquot->dq_id == id && dquot->dq_type == type)
147 break;
148 return dquot;
151 /* Add a dquot to the head of the free list */
152 static inline void put_dquot_head(struct dquot *dquot)
154 list_add(&dquot->dq_free, &free_dquots);
155 nr_free_dquots++;
158 /* Add a dquot to the tail of the free list */
159 static inline void put_dquot_last(struct dquot *dquot)
161 list_add(&dquot->dq_free, free_dquots.prev);
162 nr_free_dquots++;
165 static inline void remove_free_dquot(struct dquot *dquot)
167 /* sanity check */
168 if (list_empty(&dquot->dq_free)) {
169 printk("remove_free_dquot: dquot not on free list??\n");
171 list_del(&dquot->dq_free);
172 INIT_LIST_HEAD(&dquot->dq_free);
173 nr_free_dquots--;
176 static inline void put_inuse(struct dquot *dquot)
178 if ((dquot->dq_next = inuse_list) != NULL)
179 inuse_list->dq_pprev = &dquot->dq_next;
180 inuse_list = dquot;
181 dquot->dq_pprev = &inuse_list;
184 #if 0 /* currently not needed */
185 static inline void remove_inuse(struct dquot *dquot)
187 if (dquot->dq_pprev) {
188 if (dquot->dq_next)
189 dquot->dq_next->dq_pprev = dquot->dq_pprev;
190 *dquot->dq_pprev = dquot->dq_next;
191 dquot->dq_pprev = NULL;
194 #endif
196 static void __wait_on_dquot(struct dquot *dquot)
198 DECLARE_WAITQUEUE(wait, current);
200 add_wait_queue(&dquot->dq_wait, &wait);
201 repeat:
202 current->state = TASK_UNINTERRUPTIBLE;
203 if (dquot->dq_flags & DQ_LOCKED) {
204 schedule();
205 goto repeat;
207 remove_wait_queue(&dquot->dq_wait, &wait);
208 current->state = TASK_RUNNING;
211 static inline void wait_on_dquot(struct dquot *dquot)
213 if (dquot->dq_flags & DQ_LOCKED)
214 __wait_on_dquot(dquot);
217 static inline void lock_dquot(struct dquot *dquot)
219 wait_on_dquot(dquot);
220 dquot->dq_flags |= DQ_LOCKED;
223 static inline void unlock_dquot(struct dquot *dquot)
225 dquot->dq_flags &= ~DQ_LOCKED;
226 wake_up(&dquot->dq_wait);
229 static void write_dquot(struct dquot *dquot)
231 short type = dquot->dq_type;
232 struct file *filp = dquot->dq_mnt->mnt_dquot.files[type];
233 mm_segment_t fs;
234 loff_t offset;
235 ssize_t ret;
237 lock_dquot(dquot);
238 down(&dquot->dq_mnt->mnt_dquot.semaphore);
239 offset = dqoff(dquot->dq_id);
240 fs = get_fs();
241 set_fs(KERNEL_DS);
244 * Note: clear the DQ_MOD flag unconditionally,
245 * so we don't loop forever on failure.
247 dquot->dq_flags &= ~DQ_MOD;
248 ret = 0;
249 if (filp)
250 ret = filp->f_op->write(filp, (char *)&dquot->dq_dqb,
251 sizeof(struct dqblk), &offset);
252 if (ret != sizeof(struct dqblk))
253 printk(KERN_WARNING "VFS: dquota write failed on dev %s\n",
254 kdevname(dquot->dq_dev));
256 up(&dquot->dq_mnt->mnt_dquot.semaphore);
257 set_fs(fs);
259 unlock_dquot(dquot);
260 dqstats.writes++;
263 static void read_dquot(struct dquot *dquot)
265 short type;
266 struct file *filp;
267 mm_segment_t fs;
268 loff_t offset;
270 type = dquot->dq_type;
271 filp = dquot->dq_mnt->mnt_dquot.files[type];
273 if (filp == (struct file *)NULL)
274 return;
276 lock_dquot(dquot);
277 down(&dquot->dq_mnt->mnt_dquot.semaphore);
278 offset = dqoff(dquot->dq_id);
279 fs = get_fs();
280 set_fs(KERNEL_DS);
281 filp->f_op->read(filp, (char *)&dquot->dq_dqb, sizeof(struct dqblk), &offset);
282 up(&dquot->dq_mnt->mnt_dquot.semaphore);
283 set_fs(fs);
285 if (dquot->dq_bhardlimit == 0 && dquot->dq_bsoftlimit == 0 &&
286 dquot->dq_ihardlimit == 0 && dquot->dq_isoftlimit == 0)
287 dquot->dq_flags |= DQ_FAKE;
288 unlock_dquot(dquot);
289 dqstats.reads++;
293 * Unhash and selectively clear the dquot structure,
294 * but preserve the use count, list pointers, and
295 * wait queue.
297 void clear_dquot(struct dquot *dquot)
299 /* unhash it first */
300 unhash_dquot(dquot);
301 dquot->dq_mnt = NULL;
302 dquot->dq_flags = 0;
303 dquot->dq_referenced = 0;
304 memset(&dquot->dq_dqb, 0, sizeof(struct dqblk));
307 void invalidate_dquots(kdev_t dev, short type)
309 struct dquot *dquot, *next = inuse_list;
310 int need_restart;
312 restart:
313 need_restart = 0;
314 while ((dquot = next) != NULL) {
315 next = dquot->dq_next;
316 if (dquot->dq_dev != dev)
317 continue;
318 if (dquot->dq_type != type)
319 continue;
320 if (dquot->dq_flags & DQ_LOCKED) {
321 __wait_on_dquot(dquot);
323 /* Set the flag for another pass. */
324 need_restart = 1;
326 * Make sure it's still the same dquot.
328 if (dquot->dq_dev != dev)
329 continue;
330 if (dquot->dq_type != type)
331 continue;
333 clear_dquot(dquot);
336 * If anything blocked, restart the operation
337 * to ensure we don't miss any dquots.
339 if (need_restart)
340 goto restart;
343 int sync_dquots(kdev_t dev, short type)
345 struct dquot *dquot, *next = inuse_list;
346 int need_restart;
348 restart:
349 need_restart = 0;
350 while ((dquot = next) != NULL) {
351 next = dquot->dq_next;
352 if (dev && dquot->dq_dev != dev)
353 continue;
354 if (type != -1 && dquot->dq_type != type)
355 continue;
356 if (!(dquot->dq_flags & (DQ_LOCKED | DQ_MOD)))
357 continue;
359 wait_on_dquot(dquot);
360 if (dquot->dq_flags & DQ_MOD)
361 write_dquot(dquot);
362 /* Set the flag for another pass. */
363 need_restart = 1;
366 * If anything blocked, restart the operation
367 * to ensure we don't miss any dquots.
369 if (need_restart)
370 goto restart;
372 dqstats.syncs++;
373 return(0);
376 void dqput(struct dquot *dquot)
378 if (!dquot)
379 return;
380 if (!dquot->dq_count) {
381 printk("VFS: dqput: trying to free free dquot\n");
382 printk("VFS: device %s, dquot of %s %d\n",
383 kdevname(dquot->dq_dev), quotatypes[dquot->dq_type],
384 dquot->dq_id);
385 return;
389 * If the dq_mnt pointer isn't initialized this entry needs no
390 * checking and doesn't need to be written. It's just an empty
391 * dquot that is put back on to the freelist.
393 if (dquot->dq_mnt != (struct vfsmount *)NULL) {
394 dqstats.drops++;
395 we_slept:
396 wait_on_dquot(dquot);
397 if (dquot->dq_count > 1) {
398 dquot->dq_count--;
399 return;
401 if (dquot->dq_flags & DQ_MOD) {
402 write_dquot(dquot);
403 goto we_slept;
407 /* sanity check */
408 if (!list_empty(&dquot->dq_free)) {
409 printk("dqput: dquot already on free list??\n");
411 if (--dquot->dq_count == 0) {
412 /* Place at end of LRU free queue */
413 put_dquot_last(dquot);
414 wake_up(&dquot_wait);
417 return;
420 static void grow_dquots(void)
422 struct dquot *dquot;
423 int cnt = 32;
425 while (cnt > 0) {
426 dquot = kmem_cache_alloc(dquot_cachep, SLAB_KERNEL);
427 if(!dquot)
428 return;
430 nr_dquots++;
431 memset((caddr_t)dquot, 0, sizeof(struct dquot));
432 init_waitqueue_head(&dquot->dq_wait);
433 /* all dquots go on the inuse_list */
434 put_inuse(dquot);
435 put_dquot_head(dquot);
436 cnt--;
440 static struct dquot *find_best_candidate_weighted(void)
442 struct list_head *tmp = &free_dquots;
443 struct dquot *dquot, *best = NULL;
444 unsigned long myscore, bestscore = ~0U;
445 int limit = (nr_free_dquots > 128) ? nr_free_dquots >> 2 : 32;
447 while ((tmp = tmp->next) != &free_dquots && --limit) {
448 dquot = list_entry(tmp, struct dquot, dq_free);
449 if (dquot->dq_flags & (DQ_LOCKED | DQ_MOD))
450 continue;
451 myscore = dquot->dq_referenced;
452 if (myscore < bestscore) {
453 bestscore = myscore;
454 best = dquot;
457 return best;
460 static inline struct dquot *find_best_free(void)
462 struct list_head *tmp = &free_dquots;
463 struct dquot *dquot;
464 int limit = (nr_free_dquots > 1024) ? nr_free_dquots >> 5 : 32;
466 while ((tmp = tmp->next) != &free_dquots && --limit) {
467 dquot = list_entry(tmp, struct dquot, dq_free);
468 if (dquot->dq_referenced == 0)
469 return dquot;
471 return NULL;
474 struct dquot *get_empty_dquot(void)
476 struct dquot *dquot;
477 int count;
479 repeat:
480 dquot = find_best_free();
481 if (!dquot)
482 goto pressure;
483 got_it:
484 if (dquot->dq_flags & (DQ_LOCKED | DQ_MOD)) {
485 wait_on_dquot(dquot);
486 if (dquot->dq_flags & DQ_MOD)
488 if(dquot->dq_mnt != (struct vfsmount *)NULL)
489 write_dquot(dquot);
492 * The dquot may be back in use now, so we
493 * must recheck the free list.
495 goto repeat;
497 /* sanity check ... */
498 if (dquot->dq_count != 0)
499 printk(KERN_ERR "VFS: free dquot count=%d\n", dquot->dq_count);
501 remove_free_dquot(dquot);
502 dquot->dq_count = 1;
503 /* unhash and selectively clear the structure */
504 clear_dquot(dquot);
505 return dquot;
507 pressure:
508 if (nr_dquots < max_dquots) {
509 grow_dquots();
510 goto repeat;
513 dquot = find_best_candidate_weighted();
514 if (dquot)
515 goto got_it;
517 * Try pruning the dcache to free up some dquots ...
519 count = select_dcache(128, 0);
520 if (count) {
521 printk(KERN_DEBUG "get_empty_dquot: pruning %d\n", count);
522 prune_dcache(count);
523 free_inode_memory(count);
524 goto repeat;
527 printk("VFS: No free dquots, contact mvw@planets.elm.net\n");
528 sleep_on(&dquot_wait);
529 goto repeat;
532 struct dquot *dqget(kdev_t dev, unsigned int id, short type)
534 unsigned int hashent = hashfn(dev, id, type);
535 struct dquot *dquot, *empty = NULL;
536 struct vfsmount *vfsmnt;
538 if ((vfsmnt = lookup_vfsmnt(dev)) == (struct vfsmount *)NULL || is_enabled(vfsmnt, type) == 0)
539 return(NODQUOT);
541 we_slept:
542 if ((dquot = find_dquot(hashent, dev, id, type)) == NULL) {
543 if (empty == NULL) {
544 dquot_updating[hashent]++;
545 empty = get_empty_dquot();
546 if (!--dquot_updating[hashent])
547 wake_up(&update_wait);
548 goto we_slept;
550 dquot = empty;
551 dquot->dq_id = id;
552 dquot->dq_type = type;
553 dquot->dq_dev = dev;
554 dquot->dq_mnt = vfsmnt;
555 /* hash it first so it can be found */
556 hash_dquot(dquot);
557 read_dquot(dquot);
558 } else {
559 if (!dquot->dq_count++) {
560 remove_free_dquot(dquot);
561 } else
562 dqstats.cache_hits++;
563 wait_on_dquot(dquot);
564 if (empty)
565 dqput(empty);
568 while (dquot_updating[hashent])
569 sleep_on(&update_wait);
571 dquot->dq_referenced++;
572 dqstats.lookups++;
574 return dquot;
577 static void add_dquot_ref(kdev_t dev, short type)
579 struct super_block *sb = get_super(dev);
580 struct list_head *p;
581 struct inode *inode;
583 if (!sb || !sb->dq_op)
584 return; /* nothing to do */
586 file_list_lock();
587 for (p = sb->s_files.next; p != &sb->s_files; p = p->next) {
588 struct file *filp = list_entry(p, struct file, f_list);
589 if (!filp->f_dentry)
590 continue;
591 inode = filp->f_dentry->d_inode;
592 if (!inode)
593 continue;
594 /* N.B. race problem -- filp could become unused */
595 if (filp->f_mode & FMODE_WRITE) {
596 file_list_unlock();
597 sb->dq_op->initialize(inode, type);
598 inode->i_flags |= S_QUOTA;
599 file_list_lock();
602 file_list_unlock();
605 static void reset_dquot_ptrs(kdev_t dev, short type)
607 struct super_block *sb = get_super(dev);
608 struct list_head *p;
609 struct inode *inode;
610 struct dquot *dquot;
611 int cnt;
613 if (!sb || !sb->dq_op)
614 return; /* nothing to do */
616 restart:
617 /* free any quota for unused dentries */
618 shrink_dcache_sb(sb);
620 file_list_lock();
621 for (p = sb->s_files.next; p != &sb->s_files; p = p->next) {
622 struct file *filp = list_entry(p, struct file, f_list);
623 if (!filp->f_dentry)
624 continue;
625 inode = filp->f_dentry->d_inode;
626 if (!inode)
627 continue;
629 * Note: we restart after each blocking operation,
630 * as the inuse_filps list may have changed.
632 if (IS_QUOTAINIT(inode)) {
633 dquot = inode->i_dquot[type];
634 inode->i_dquot[type] = NODQUOT;
635 /* any other quota in use? */
636 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
637 if (inode->i_dquot[cnt] != NODQUOT)
638 goto put_it;
640 inode->i_flags &= ~S_QUOTA;
641 put_it:
642 if (dquot != NODQUOT) {
643 file_list_unlock();
644 dqput(dquot);
645 /* we may have blocked ... */
646 goto restart;
650 file_list_unlock();
653 static inline void dquot_incr_inodes(struct dquot *dquot, unsigned long number)
655 lock_dquot(dquot);
656 dquot->dq_curinodes += number;
657 dquot->dq_flags |= DQ_MOD;
658 unlock_dquot(dquot);
661 static inline void dquot_incr_blocks(struct dquot *dquot, unsigned long number)
663 lock_dquot(dquot);
664 dquot->dq_curblocks += number;
665 dquot->dq_flags |= DQ_MOD;
666 unlock_dquot(dquot);
669 static inline void dquot_decr_inodes(struct dquot *dquot, unsigned long number)
671 lock_dquot(dquot);
672 if (dquot->dq_curinodes > number)
673 dquot->dq_curinodes -= number;
674 else
675 dquot->dq_curinodes = 0;
676 if (dquot->dq_curinodes < dquot->dq_isoftlimit)
677 dquot->dq_itime = (time_t) 0;
678 dquot->dq_flags &= ~DQ_INODES;
679 dquot->dq_flags |= DQ_MOD;
680 unlock_dquot(dquot);
683 static inline void dquot_decr_blocks(struct dquot *dquot, unsigned long number)
685 lock_dquot(dquot);
686 if (dquot->dq_curblocks > number)
687 dquot->dq_curblocks -= number;
688 else
689 dquot->dq_curblocks = 0;
690 if (dquot->dq_curblocks < dquot->dq_bsoftlimit)
691 dquot->dq_btime = (time_t) 0;
692 dquot->dq_flags &= ~DQ_BLKS;
693 dquot->dq_flags |= DQ_MOD;
694 unlock_dquot(dquot);
697 static inline char need_print_warning(short type, uid_t initiator, struct dquot *dquot)
699 switch (type) {
700 case USRQUOTA:
701 return(initiator == dquot->dq_id);
702 case GRPQUOTA:
703 return(initiator == dquot->dq_id);
705 return(0);
708 static inline char ignore_hardlimit(struct dquot *dquot, uid_t initiator)
710 return(initiator == 0 && dquot->dq_mnt->mnt_dquot.rsquash[dquot->dq_type] == 0);
713 static int check_idq(struct dquot *dquot, short type, u_long short inodes, uid_t initiator,
714 struct tty_struct *tty)
716 if (inodes <= 0 || dquot->dq_flags & DQ_FAKE)
717 return(QUOTA_OK);
719 if (dquot->dq_ihardlimit &&
720 (dquot->dq_curinodes + inodes) > dquot->dq_ihardlimit &&
721 !ignore_hardlimit(dquot, initiator)) {
722 if ((dquot->dq_flags & DQ_INODES) == 0 &&
723 need_print_warning(type, initiator, dquot)) {
724 sprintf(quotamessage, "%s: write failed, %s file limit reached\n",
725 dquot->dq_mnt->mnt_dirname, quotatypes[type]);
726 tty_write_message(tty, quotamessage);
727 dquot->dq_flags |= DQ_INODES;
729 return(NO_QUOTA);
732 if (dquot->dq_isoftlimit &&
733 (dquot->dq_curinodes + inodes) > dquot->dq_isoftlimit &&
734 dquot->dq_itime && CURRENT_TIME >= dquot->dq_itime &&
735 !ignore_hardlimit(dquot, initiator)) {
736 if (need_print_warning(type, initiator, dquot)) {
737 sprintf(quotamessage, "%s: warning, %s file quota exceeded too long.\n",
738 dquot->dq_mnt->mnt_dirname, quotatypes[type]);
739 tty_write_message(tty, quotamessage);
741 return(NO_QUOTA);
744 if (dquot->dq_isoftlimit &&
745 (dquot->dq_curinodes + inodes) > dquot->dq_isoftlimit &&
746 dquot->dq_itime == 0) {
747 if (need_print_warning(type, initiator, dquot)) {
748 sprintf(quotamessage, "%s: warning, %s file quota exceeded\n",
749 dquot->dq_mnt->mnt_dirname, quotatypes[type]);
750 tty_write_message(tty, quotamessage);
752 dquot->dq_itime = CURRENT_TIME + dquot->dq_mnt->mnt_dquot.inode_expire[type];
755 return(QUOTA_OK);
758 static int check_bdq(struct dquot *dquot, short type, u_long blocks, uid_t initiator,
759 struct tty_struct *tty, char warn)
761 if (blocks <= 0 || dquot->dq_flags & DQ_FAKE)
762 return(QUOTA_OK);
764 if (dquot->dq_bhardlimit &&
765 (dquot->dq_curblocks + blocks) > dquot->dq_bhardlimit &&
766 !ignore_hardlimit(dquot, initiator)) {
767 if (warn && (dquot->dq_flags & DQ_BLKS) == 0 &&
768 need_print_warning(type, initiator, dquot)) {
769 sprintf(quotamessage, "%s: write failed, %s disk limit reached.\n",
770 dquot->dq_mnt->mnt_dirname, quotatypes[type]);
771 tty_write_message(tty, quotamessage);
772 dquot->dq_flags |= DQ_BLKS;
774 return(NO_QUOTA);
777 if (dquot->dq_bsoftlimit &&
778 (dquot->dq_curblocks + blocks) > dquot->dq_bsoftlimit &&
779 dquot->dq_btime && CURRENT_TIME >= dquot->dq_btime &&
780 !ignore_hardlimit(dquot, initiator)) {
781 if (warn && need_print_warning(type, initiator, dquot)) {
782 sprintf(quotamessage, "%s: write failed, %s disk quota exceeded too long.\n",
783 dquot->dq_mnt->mnt_dirname, quotatypes[type]);
784 tty_write_message(tty, quotamessage);
786 return(NO_QUOTA);
789 if (dquot->dq_bsoftlimit &&
790 (dquot->dq_curblocks + blocks) > dquot->dq_bsoftlimit &&
791 dquot->dq_btime == 0) {
792 if (warn && need_print_warning(type, initiator, dquot)) {
793 sprintf(quotamessage, "%s: warning, %s disk quota exceeded\n",
794 dquot->dq_mnt->mnt_dirname, quotatypes[type]);
795 tty_write_message(tty, quotamessage);
797 dquot->dq_btime = CURRENT_TIME + dquot->dq_mnt->mnt_dquot.block_expire[type];
800 return(QUOTA_OK);
804 * Initialize a dquot-struct with new quota info. This is used by the
805 * system call interface functions.
807 static int set_dqblk(kdev_t dev, int id, short type, int flags, struct dqblk *dqblk)
809 struct dquot *dquot;
810 int error = -EFAULT;
811 struct dqblk dq_dqblk;
813 if (dqblk == (struct dqblk *)NULL)
814 return error;
816 if (flags & QUOTA_SYSCALL) {
817 if (copy_from_user(&dq_dqblk, dqblk, sizeof(struct dqblk)))
818 return(error);
819 } else
820 memcpy((caddr_t)&dq_dqblk, (caddr_t)dqblk, sizeof(struct dqblk));
822 if ((dquot = dqget(dev, id, type)) != NODQUOT) {
823 lock_dquot(dquot);
825 if (id > 0 && ((flags & SET_QUOTA) || (flags & SET_QLIMIT))) {
826 dquot->dq_bhardlimit = dq_dqblk.dqb_bhardlimit;
827 dquot->dq_bsoftlimit = dq_dqblk.dqb_bsoftlimit;
828 dquot->dq_ihardlimit = dq_dqblk.dqb_ihardlimit;
829 dquot->dq_isoftlimit = dq_dqblk.dqb_isoftlimit;
832 if ((flags & SET_QUOTA) || (flags & SET_USE)) {
833 if (dquot->dq_isoftlimit &&
834 dquot->dq_curinodes < dquot->dq_isoftlimit &&
835 dq_dqblk.dqb_curinodes >= dquot->dq_isoftlimit)
836 dquot->dq_itime = CURRENT_TIME + dquot->dq_mnt->mnt_dquot.inode_expire[type];
837 dquot->dq_curinodes = dq_dqblk.dqb_curinodes;
838 if (dquot->dq_curinodes < dquot->dq_isoftlimit)
839 dquot->dq_flags &= ~DQ_INODES;
840 if (dquot->dq_bsoftlimit &&
841 dquot->dq_curblocks < dquot->dq_bsoftlimit &&
842 dq_dqblk.dqb_curblocks >= dquot->dq_bsoftlimit)
843 dquot->dq_btime = CURRENT_TIME + dquot->dq_mnt->mnt_dquot.block_expire[type];
844 dquot->dq_curblocks = dq_dqblk.dqb_curblocks;
845 if (dquot->dq_curblocks < dquot->dq_bsoftlimit)
846 dquot->dq_flags &= ~DQ_BLKS;
849 if (id == 0) {
850 dquot->dq_mnt->mnt_dquot.block_expire[type] = dquot->dq_btime = dq_dqblk.dqb_btime;
851 dquot->dq_mnt->mnt_dquot.inode_expire[type] = dquot->dq_itime = dq_dqblk.dqb_itime;
854 if (dq_dqblk.dqb_bhardlimit == 0 && dq_dqblk.dqb_bsoftlimit == 0 &&
855 dq_dqblk.dqb_ihardlimit == 0 && dq_dqblk.dqb_isoftlimit == 0)
856 dquot->dq_flags |= DQ_FAKE;
857 else
858 dquot->dq_flags &= ~DQ_FAKE;
860 dquot->dq_flags |= DQ_MOD;
861 unlock_dquot(dquot);
862 dqput(dquot);
864 return(0);
867 static int get_quota(kdev_t dev, int id, short type, struct dqblk *dqblk)
869 struct dquot *dquot;
870 int error = -ESRCH;
872 if (!dev_has_quota_enabled(dev, type))
873 goto out;
874 dquot = dqget(dev, id, type);
875 if (dquot == NODQUOT)
876 goto out;
878 error = -EFAULT;
879 if (dqblk && !copy_to_user(dqblk, &dquot->dq_dqb, sizeof(struct dqblk)))
880 error = 0;
881 dqput(dquot);
882 out:
883 return error;
886 static int get_stats(caddr_t addr)
888 int error = -EFAULT;
889 struct dqstats stats;
891 dqstats.allocated_dquots = nr_dquots;
892 dqstats.free_dquots = nr_free_dquots;
894 /* make a copy, in case we page-fault in user space */
895 memcpy(&stats, &dqstats, sizeof(struct dqstats));
896 if (!copy_to_user(addr, &stats, sizeof(struct dqstats)))
897 error = 0;
898 return error;
901 static int quota_root_squash(kdev_t dev, short type, int *addr)
903 struct vfsmount *vfsmnt;
904 int new_value, error;
906 if ((vfsmnt = lookup_vfsmnt(dev)) == (struct vfsmount *)NULL)
907 return(-ENODEV);
909 error = -EFAULT;
910 if (!copy_from_user(&new_value, addr, sizeof(int))) {
911 vfsmnt->mnt_dquot.rsquash[type] = new_value;
912 error = 0;
914 return error;
918 * This is a simple algorithm that calculates the size of a file in blocks.
919 * This is only used on filesystems that do not have an i_blocks count.
921 static u_long isize_to_blocks(size_t isize, size_t blksize)
923 u_long blocks;
924 u_long indirect;
926 if (!blksize)
927 blksize = BLOCK_SIZE;
928 blocks = (isize / blksize) + ((isize % blksize) ? 1 : 0);
929 if (blocks > 10) {
930 indirect = ((blocks - 11) >> 8) + 1; /* single indirect blocks */
931 if (blocks > (10 + 256)) {
932 indirect += ((blocks - 267) >> 16) + 1; /* double indirect blocks */
933 if (blocks > (10 + 256 + (256 << 8)))
934 indirect++; /* triple indirect blocks */
936 blocks += indirect;
938 return(blocks);
942 * Externally referenced functions through dquot_operations in inode.
944 * Note: this is a blocking operation.
946 void dquot_initialize(struct inode *inode, short type)
948 struct dquot *dquot;
949 unsigned int id = 0;
950 short cnt;
952 if (S_ISREG(inode->i_mode) ||
953 S_ISDIR(inode->i_mode) ||
954 S_ISLNK(inode->i_mode)) {
955 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
956 if (type != -1 && cnt != type)
957 continue;
959 if (!sb_has_quota_enabled(inode->i_sb, cnt))
960 continue;
962 if (inode->i_dquot[cnt] == NODQUOT) {
963 switch (cnt) {
964 case USRQUOTA:
965 id = inode->i_uid;
966 break;
967 case GRPQUOTA:
968 id = inode->i_gid;
969 break;
971 dquot = dqget(inode->i_dev, id, cnt);
972 if (inode->i_dquot[cnt] != NODQUOT) {
973 dqput(dquot);
974 continue;
976 inode->i_dquot[cnt] = dquot;
977 inode->i_flags |= S_QUOTA;
984 * Release all quota for the specified inode.
986 * Note: this is a blocking operation.
988 void dquot_drop(struct inode *inode)
990 struct dquot *dquot;
991 short cnt;
993 inode->i_flags &= ~S_QUOTA;
994 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
995 if (inode->i_dquot[cnt] == NODQUOT)
996 continue;
997 dquot = inode->i_dquot[cnt];
998 inode->i_dquot[cnt] = NODQUOT;
999 dqput(dquot);
1004 * Note: this is a blocking operation.
1006 int dquot_alloc_block(const struct inode *inode, unsigned long number, uid_t initiator,
1007 char warn)
1009 unsigned short cnt;
1010 struct tty_struct *tty = current->tty;
1012 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1013 if (inode->i_dquot[cnt] == NODQUOT)
1014 continue;
1015 if (check_bdq(inode->i_dquot[cnt], cnt, number, initiator, tty, warn))
1016 return(NO_QUOTA);
1019 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1020 if (inode->i_dquot[cnt] == NODQUOT)
1021 continue;
1022 dquot_incr_blocks(inode->i_dquot[cnt], number);
1025 return(QUOTA_OK);
1029 * Note: this is a blocking operation.
1031 int dquot_alloc_inode(const struct inode *inode, unsigned long number, uid_t initiator)
1033 unsigned short cnt;
1034 struct tty_struct *tty = current->tty;
1036 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1037 if (inode->i_dquot[cnt] == NODQUOT)
1038 continue;
1039 if (check_idq(inode->i_dquot[cnt], cnt, number, initiator, tty))
1040 return(NO_QUOTA);
1043 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1044 if (inode->i_dquot[cnt] == NODQUOT)
1045 continue;
1046 dquot_incr_inodes(inode->i_dquot[cnt], number);
1049 return(QUOTA_OK);
1053 * Note: this is a blocking operation.
1055 void dquot_free_block(const struct inode *inode, unsigned long number)
1057 unsigned short cnt;
1059 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1060 if (inode->i_dquot[cnt] == NODQUOT)
1061 continue;
1062 dquot_decr_blocks(inode->i_dquot[cnt], number);
1067 * Note: this is a blocking operation.
1069 void dquot_free_inode(const struct inode *inode, unsigned long number)
1071 unsigned short cnt;
1073 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1074 if (inode->i_dquot[cnt] == NODQUOT)
1075 continue;
1076 dquot_decr_inodes(inode->i_dquot[cnt], number);
1081 * Transfer the number of inode and blocks from one diskquota to an other.
1083 * Note: this is a blocking operation.
1085 int dquot_transfer(struct inode *inode, struct iattr *iattr, char direction, uid_t initiator)
1087 unsigned long blocks;
1088 struct dquot *transfer_from[MAXQUOTAS];
1089 struct dquot *transfer_to[MAXQUOTAS];
1090 struct tty_struct *tty = current->tty;
1091 short cnt, disc;
1094 * Find out if this filesystem uses i_blocks.
1096 if (inode->i_blksize == 0)
1097 blocks = isize_to_blocks(inode->i_size, BLOCK_SIZE);
1098 else
1099 blocks = (inode->i_blocks / 2);
1102 * Build the transfer_from and transfer_to lists and check quotas to see
1103 * if operation is permitted.
1105 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1106 transfer_from[cnt] = NODQUOT;
1107 transfer_to[cnt] = NODQUOT;
1109 if (!sb_has_quota_enabled(inode->i_sb, cnt))
1110 continue;
1112 switch (cnt) {
1113 case USRQUOTA:
1114 if (inode->i_uid == iattr->ia_uid)
1115 continue;
1116 transfer_from[cnt] = dqget(inode->i_dev, (direction) ? iattr->ia_uid : inode->i_uid, cnt);
1117 transfer_to[cnt] = dqget(inode->i_dev, (direction) ? inode->i_uid : iattr->ia_uid, cnt);
1118 break;
1119 case GRPQUOTA:
1120 if (inode->i_gid == iattr->ia_gid)
1121 continue;
1122 transfer_from[cnt] = dqget(inode->i_dev, (direction) ? iattr->ia_gid : inode->i_gid, cnt);
1123 transfer_to[cnt] = dqget(inode->i_dev, (direction) ? inode->i_gid : iattr->ia_gid, cnt);
1124 break;
1127 if (check_idq(transfer_to[cnt], cnt, 1, initiator, tty) == NO_QUOTA ||
1128 check_bdq(transfer_to[cnt], cnt, blocks, initiator, tty, 0) == NO_QUOTA) {
1129 for (disc = 0; disc <= cnt; disc++) {
1130 dqput(transfer_from[disc]);
1131 dqput(transfer_to[disc]);
1133 return(NO_QUOTA);
1138 * Finally perform the needed transfer from transfer_from to transfer_to,
1139 * and release any pointers to dquots not needed anymore.
1141 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1143 * Skip changes for same uid or gid or for non-existing quota-type.
1145 if (transfer_from[cnt] == NODQUOT && transfer_to[cnt] == NODQUOT)
1146 continue;
1148 if (transfer_from[cnt] != NODQUOT) {
1149 dquot_decr_inodes(transfer_from[cnt], 1);
1150 dquot_decr_blocks(transfer_from[cnt], blocks);
1153 if (transfer_to[cnt] != NODQUOT) {
1154 dquot_incr_inodes(transfer_to[cnt], 1);
1155 dquot_incr_blocks(transfer_to[cnt], blocks);
1158 if (inode->i_dquot[cnt] != NODQUOT) {
1159 struct dquot *temp = inode->i_dquot[cnt];
1160 inode->i_dquot[cnt] = transfer_to[cnt];
1161 dqput(temp);
1162 dqput(transfer_from[cnt]);
1163 } else {
1164 dqput(transfer_from[cnt]);
1165 dqput(transfer_to[cnt]);
1169 return(QUOTA_OK);
1173 void __init dquot_init_hash(void)
1175 printk(KERN_NOTICE "VFS: Diskquotas version %s initialized\n", __DQUOT_VERSION__);
1177 dquot_cachep = kmem_cache_create("dquot", sizeof(struct dquot),
1178 sizeof(unsigned long) * 4,
1179 SLAB_HWCACHE_ALIGN, NULL, NULL);
1181 if (!dquot_cachep)
1182 panic("Cannot create dquot SLAB cache\n");
1184 memset(dquot_hash, 0, sizeof(dquot_hash));
1185 memset((caddr_t)&dqstats, 0, sizeof(dqstats));
1189 * Definitions of diskquota operations.
1191 struct dquot_operations dquot_operations = {
1192 dquot_initialize, /* mandatory */
1193 dquot_drop, /* mandatory */
1194 dquot_alloc_block,
1195 dquot_alloc_inode,
1196 dquot_free_block,
1197 dquot_free_inode,
1198 dquot_transfer
1201 static inline void set_enable_flags(struct vfsmount *vfsmnt, short type)
1203 switch (type) {
1204 case USRQUOTA:
1205 vfsmnt->mnt_dquot.flags |= DQUOT_USR_ENABLED;
1206 break;
1207 case GRPQUOTA:
1208 vfsmnt->mnt_dquot.flags |= DQUOT_GRP_ENABLED;
1209 break;
1213 static inline void reset_enable_flags(struct vfsmount *vfsmnt, short type)
1215 switch (type) {
1216 case USRQUOTA:
1217 vfsmnt->mnt_dquot.flags &= ~DQUOT_USR_ENABLED;
1218 break;
1219 case GRPQUOTA:
1220 vfsmnt->mnt_dquot.flags &= ~DQUOT_GRP_ENABLED;
1221 break;
1226 * Turn quota off on a device. type == -1 ==> quotaoff for all types (umount)
1228 int quota_off(kdev_t dev, short type)
1230 struct vfsmount *vfsmnt;
1231 struct file *filp;
1232 short cnt;
1234 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1235 if (type != -1 && cnt != type)
1236 continue;
1238 vfsmnt = lookup_vfsmnt(dev);
1239 if (!vfsmnt)
1240 goto out;
1241 if (!vfsmnt->mnt_sb)
1242 goto out;
1243 if (!is_enabled(vfsmnt, cnt))
1244 continue;
1245 reset_enable_flags(vfsmnt, cnt);
1247 /* Note: these are blocking operations */
1248 reset_dquot_ptrs(dev, cnt);
1249 invalidate_dquots(dev, cnt);
1251 filp = vfsmnt->mnt_dquot.files[cnt];
1252 vfsmnt->mnt_dquot.files[cnt] = (struct file *)NULL;
1253 vfsmnt->mnt_dquot.inode_expire[cnt] = 0;
1254 vfsmnt->mnt_dquot.block_expire[cnt] = 0;
1255 fput(filp);
1259 * Check whether any quota is still enabled,
1260 * and if not clear the dq_op pointer.
1262 vfsmnt = lookup_vfsmnt(dev);
1263 if (vfsmnt && vfsmnt->mnt_sb) {
1264 int enabled = 0;
1265 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1266 enabled |= is_enabled(vfsmnt, cnt);
1267 if (!enabled)
1268 vfsmnt->mnt_sb->dq_op = NULL;
1271 out:
1272 return(0);
1275 int quota_on(kdev_t dev, short type, char *path)
1277 struct file *f;
1278 struct vfsmount *vfsmnt;
1279 struct inode *inode;
1280 struct dquot *dquot;
1281 struct quota_mount_options *mnt_dquot;
1282 char *tmp;
1283 int error;
1285 vfsmnt = lookup_vfsmnt(dev);
1286 if (vfsmnt == (struct vfsmount *)NULL)
1287 return -ENODEV;
1289 if (is_enabled(vfsmnt, type))
1290 return -EBUSY;
1291 mnt_dquot = &vfsmnt->mnt_dquot;
1293 tmp = getname(path);
1294 error = PTR_ERR(tmp);
1295 if (IS_ERR(tmp))
1296 return error;
1298 f = filp_open(tmp, O_RDWR, 0600);
1299 putname(tmp);
1300 if (IS_ERR(f))
1301 return PTR_ERR(f);
1303 /* sanity checks */
1304 error = -EIO;
1305 if (!f->f_op->read && !f->f_op->write)
1306 goto cleanup;
1307 inode = f->f_dentry->d_inode;
1308 error = -EACCES;
1309 if (!S_ISREG(inode->i_mode))
1310 goto cleanup;
1311 error = -EINVAL;
1312 if (inode->i_size == 0 || (inode->i_size % sizeof(struct dqblk)) != 0)
1313 goto cleanup;
1315 /* OK, there we go */
1316 set_enable_flags(vfsmnt, type);
1317 mnt_dquot->files[type] = f;
1319 dquot = dqget(dev, 0, type);
1320 mnt_dquot->inode_expire[type] = (dquot) ? dquot->dq_itime : MAX_IQ_TIME;
1321 mnt_dquot->block_expire[type] = (dquot) ? dquot->dq_btime : MAX_DQ_TIME;
1322 dqput(dquot);
1324 vfsmnt->mnt_sb->dq_op = &dquot_operations;
1325 add_dquot_ref(dev, type);
1327 return(0);
1329 cleanup:
1330 fput(f);
1331 return error;
1335 * This is the system call interface. This communicates with
1336 * the user-level programs. Currently this only supports diskquota
1337 * calls. Maybe we need to add the process quotas etc. in the future,
1338 * but we probably should use rlimits for that.
1340 asmlinkage int sys_quotactl(int cmd, const char *special, int id, caddr_t addr)
1342 int cmds = 0, type = 0, flags = 0;
1343 kdev_t dev;
1344 int ret = -EINVAL;
1346 lock_kernel();
1347 cmds = cmd >> SUBCMDSHIFT;
1348 type = cmd & SUBCMDMASK;
1350 if ((u_int) type >= MAXQUOTAS)
1351 goto out;
1352 ret = -EPERM;
1353 switch (cmds) {
1354 case Q_SYNC:
1355 case Q_GETSTATS:
1356 break;
1357 case Q_GETQUOTA:
1358 if (((type == USRQUOTA && current->euid != id) ||
1359 (type == GRPQUOTA && current->egid != id)) &&
1360 !capable(CAP_SYS_RESOURCE))
1361 goto out;
1362 break;
1363 default:
1364 if (!capable(CAP_SYS_RESOURCE))
1365 goto out;
1368 ret = -EINVAL;
1369 dev = 0;
1370 if (special != NULL || (cmds != Q_SYNC && cmds != Q_GETSTATS)) {
1371 mode_t mode;
1372 struct dentry * dentry;
1374 dentry = namei(special);
1375 if (IS_ERR(dentry))
1376 goto out;
1378 dev = dentry->d_inode->i_rdev;
1379 mode = dentry->d_inode->i_mode;
1380 dput(dentry);
1382 ret = -ENOTBLK;
1383 if (!S_ISBLK(mode))
1384 goto out;
1387 ret = -EINVAL;
1388 switch (cmds) {
1389 case Q_QUOTAON:
1390 ret = quota_on(dev, type, (char *) addr);
1391 goto out;
1392 case Q_QUOTAOFF:
1393 ret = quota_off(dev, type);
1394 goto out;
1395 case Q_GETQUOTA:
1396 ret = get_quota(dev, id, type, (struct dqblk *) addr);
1397 goto out;
1398 case Q_SETQUOTA:
1399 flags |= SET_QUOTA;
1400 break;
1401 case Q_SETUSE:
1402 flags |= SET_USE;
1403 break;
1404 case Q_SETQLIM:
1405 flags |= SET_QLIMIT;
1406 break;
1407 case Q_SYNC:
1408 ret = sync_dquots(dev, type);
1409 goto out;
1410 case Q_GETSTATS:
1411 ret = get_stats(addr);
1412 goto out;
1413 case Q_RSQUASH:
1414 ret = quota_root_squash(dev, type, (int *) addr);
1415 goto out;
1416 default:
1417 goto out;
1420 flags |= QUOTA_SYSCALL;
1422 ret = -ESRCH;
1423 if (dev_has_quota_enabled(dev, type))
1424 ret = set_dqblk(dev, id, type, flags, (struct dqblk *) addr);
1425 out:
1426 unlock_kernel();
1427 return ret;