[PATCH] DVB: Documentation and Kconfig updazes
[linux-2.6/history.git] / fs / dquot.c
blobb7b9b5c4427706216f869af961d6629cdd8d5a68
1 /*
2 * Implementation of the diskquota system for the LINUX operating
3 * system. QUOTA is implemented using the BSD system call interface as
4 * the means of communication with the user level. Currently only the
5 * ext2 filesystem has support for disk quotas. Other filesystems may
6 * be added in the future. This file contains the generic routines
7 * called by the different filesystems on allocation of an inode or
8 * block. These routines take care of the administration needed to
9 * have a consistent diskquota tracking system. The ideas of both
10 * user and group quotas are based on the Melbourne quota system as
11 * used on BSD derived systems. The internal implementation is
12 * based on one of the several variants of the LINUX inode-subsystem
13 * with added complexity of the diskquota system.
15 * Version: $Id: dquot.c,v 6.3 1996/11/17 18:35:34 mvw Exp mvw $
17 * Author: Marco van Wieringen <mvw@planets.elm.net>
19 * Fixes: Dmitry Gorodchanin <pgmdsg@ibi.com>, 11 Feb 96
21 * Revised list management to avoid races
22 * -- Bill Hawes, <whawes@star.net>, 9/98
24 * Fixed races in dquot_transfer(), dqget() and dquot_alloc_...().
25 * As the consequence the locking was moved from dquot_decr_...(),
26 * dquot_incr_...() to calling functions.
27 * invalidate_dquots() now writes modified dquots.
28 * Serialized quota_off() and quota_on() for mount point.
29 * Fixed a few bugs in grow_dquots().
30 * Fixed deadlock in write_dquot() - we no longer account quotas on
31 * quota files
32 * remove_dquot_ref() moved to inode.c - it now traverses through inodes
33 * add_dquot_ref() restarts after blocking
34 * Added check for bogus uid and fixed check for group in quotactl.
35 * Jan Kara, <jack@suse.cz>, sponsored by SuSE CR, 10-11/99
37 * Used struct list_head instead of own list struct
38 * Invalidation of referenced dquots is 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 * Rewritten quota interface. Implemented new quota format and
49 * formats registering.
50 * Jan Kara, <jack@suse.cz>, 2001,2002
52 * New SMP locking.
53 * Jan Kara, <jack@suse.cz>, 10/2002
55 * (C) Copyright 1994 - 1997 Marco van Wieringen
58 #include <linux/errno.h>
59 #include <linux/kernel.h>
60 #include <linux/fs.h>
61 #include <linux/mount.h>
62 #include <linux/mm.h>
63 #include <linux/time.h>
64 #include <linux/types.h>
65 #include <linux/string.h>
66 #include <linux/fcntl.h>
67 #include <linux/stat.h>
68 #include <linux/tty.h>
69 #include <linux/file.h>
70 #include <linux/slab.h>
71 #include <linux/sysctl.h>
72 #include <linux/smp_lock.h>
73 #include <linux/init.h>
74 #include <linux/module.h>
75 #include <linux/proc_fs.h>
76 #include <linux/security.h>
77 #include <linux/kmod.h>
79 #include <asm/uaccess.h>
81 #define __DQUOT_PARANOIA
84 * There are two quota SMP locks. dq_list_lock protects all lists with quotas
85 * and quota formats and also dqstats structure containing statistics about the
86 * lists. dq_data_lock protects data from dq_dqb and also mem_dqinfo structures
87 * and also guards consistency of dquot->dq_dqb with inode->i_blocks, i_bytes.
88 * Note that we don't have to do the locking of i_blocks and i_bytes when the
89 * quota is disabled - i_sem should serialize the access. dq_data_lock should
90 * be always grabbed before dq_list_lock.
92 * Note that some things (eg. sb pointer, type, id) doesn't change during
93 * the life of the dquot structure and so needn't to be protected by a lock
95 spinlock_t dq_list_lock = SPIN_LOCK_UNLOCKED;
96 spinlock_t dq_data_lock = SPIN_LOCK_UNLOCKED;
98 static char *quotatypes[] = INITQFNAMES;
99 static struct quota_format_type *quota_formats; /* List of registered formats */
100 static struct quota_module_name module_names[] = INIT_QUOTA_MODULE_NAMES;
102 int register_quota_format(struct quota_format_type *fmt)
104 spin_lock(&dq_list_lock);
105 fmt->qf_next = quota_formats;
106 quota_formats = fmt;
107 spin_unlock(&dq_list_lock);
108 return 0;
111 void unregister_quota_format(struct quota_format_type *fmt)
113 struct quota_format_type **actqf;
115 spin_lock(&dq_list_lock);
116 for (actqf = &quota_formats; *actqf && *actqf != fmt; actqf = &(*actqf)->qf_next);
117 if (*actqf)
118 *actqf = (*actqf)->qf_next;
119 spin_unlock(&dq_list_lock);
122 static struct quota_format_type *find_quota_format(int id)
124 struct quota_format_type *actqf;
126 spin_lock(&dq_list_lock);
127 for (actqf = quota_formats; actqf && actqf->qf_fmt_id != id; actqf = actqf->qf_next);
128 if (!actqf || !try_module_get(actqf->qf_owner)) {
129 int qm;
131 spin_unlock(&dq_list_lock);
133 for (qm = 0; module_names[qm].qm_fmt_id && module_names[qm].qm_fmt_id != id; qm++);
134 if (!module_names[qm].qm_fmt_id || request_module(module_names[qm].qm_mod_name))
135 return NULL;
137 spin_lock(&dq_list_lock);
138 for (actqf = quota_formats; actqf && actqf->qf_fmt_id != id; actqf = actqf->qf_next);
139 if (actqf && !try_module_get(actqf->qf_owner))
140 actqf = NULL;
142 spin_unlock(&dq_list_lock);
143 return actqf;
146 static void put_quota_format(struct quota_format_type *fmt)
148 module_put(fmt->qf_owner);
152 * Dquot List Management:
153 * The quota code uses three lists for dquot management: the inuse_list,
154 * free_dquots, and dquot_hash[] array. A single dquot structure may be
155 * on all three lists, depending on its current state.
157 * All dquots are placed to the end of inuse_list when first created, and this
158 * list is used for the sync and invalidate operations, which must look
159 * at every dquot.
161 * Unused dquots (dq_count == 0) are added to the free_dquots list when freed,
162 * and this list is searched whenever we need an available dquot. Dquots are
163 * removed from the list as soon as they are used again, and
164 * dqstats.free_dquots gives the number of dquots on the list. When
165 * dquot is invalidated it's completely released from memory.
167 * Dquots with a specific identity (device, type and id) are placed on
168 * one of the dquot_hash[] hash chains. The provides an efficient search
169 * mechanism to locate a specific dquot.
173 * Note that any operation which operates on dquot data (ie. dq_dqb) must
174 * hold dq_data_lock.
176 * Any operation working with dquots must hold dqptr_sem. If operation is
177 * just reading pointers from inodes than read lock is enough. If pointers
178 * are altered function must hold write lock.
180 * Locked dquots might not be referenced in inodes. Currently dquot it locked
181 * only once in its existence - when it's being read to memory on first dqget()
182 * and at that time it can't be referenced from inode. Write operations on
183 * dquots don't hold dquot lock as they copy data to internal buffers before
184 * writing anyway and copying as well as any data update should be atomic. Also
185 * nobody can change used entries in dquot structure as this is done only when
186 * quota is destroyed and invalidate_dquots() is called only when dq_count == 0.
189 static LIST_HEAD(inuse_list);
190 static LIST_HEAD(free_dquots);
191 static struct list_head dquot_hash[NR_DQHASH];
193 struct dqstats dqstats;
195 static void dqput(struct dquot *dquot);
197 static inline int const hashfn(struct super_block *sb, unsigned int id, int type)
199 return((((unsigned long)sb>>L1_CACHE_SHIFT) ^ id) * (MAXQUOTAS - type)) % NR_DQHASH;
203 * Following list functions expect dq_list_lock to be held
205 static inline void insert_dquot_hash(struct dquot *dquot)
207 struct list_head *head = dquot_hash + hashfn(dquot->dq_sb, dquot->dq_id, dquot->dq_type);
208 list_add(&dquot->dq_hash, head);
211 static inline void remove_dquot_hash(struct dquot *dquot)
213 list_del_init(&dquot->dq_hash);
216 static inline struct dquot *find_dquot(unsigned int hashent, struct super_block *sb, unsigned int id, int type)
218 struct list_head *head;
219 struct dquot *dquot;
221 for (head = dquot_hash[hashent].next; head != dquot_hash+hashent; head = head->next) {
222 dquot = list_entry(head, struct dquot, dq_hash);
223 if (dquot->dq_sb == sb && dquot->dq_id == id && dquot->dq_type == type)
224 return dquot;
226 return NODQUOT;
229 /* Add a dquot to the tail of the free list */
230 static inline void put_dquot_last(struct dquot *dquot)
232 list_add(&dquot->dq_free, free_dquots.prev);
233 dqstats.free_dquots++;
236 static inline void remove_free_dquot(struct dquot *dquot)
238 if (list_empty(&dquot->dq_free))
239 return;
240 list_del_init(&dquot->dq_free);
241 dqstats.free_dquots--;
244 static inline void put_inuse(struct dquot *dquot)
246 /* We add to the back of inuse list so we don't have to restart
247 * when traversing this list and we block */
248 list_add(&dquot->dq_inuse, inuse_list.prev);
249 dqstats.allocated_dquots++;
252 static inline void remove_inuse(struct dquot *dquot)
254 dqstats.allocated_dquots--;
255 list_del(&dquot->dq_inuse);
258 static void wait_on_dquot(struct dquot *dquot)
260 down(&dquot->dq_lock);
261 up(&dquot->dq_lock);
264 static int read_dqblk(struct dquot *dquot)
266 int ret;
267 struct quota_info *dqopt = sb_dqopt(dquot->dq_sb);
269 down(&dquot->dq_lock);
270 down(&dqopt->dqio_sem);
271 ret = dqopt->ops[dquot->dq_type]->read_dqblk(dquot);
272 up(&dqopt->dqio_sem);
273 up(&dquot->dq_lock);
274 return ret;
277 static int commit_dqblk(struct dquot *dquot)
279 int ret;
280 struct quota_info *dqopt = sb_dqopt(dquot->dq_sb);
282 down(&dqopt->dqio_sem);
283 ret = dqopt->ops[dquot->dq_type]->commit_dqblk(dquot);
284 up(&dqopt->dqio_sem);
285 return ret;
288 /* Invalidate all dquots on the list. Note that this function is called after
289 * quota is disabled so no new quota might be created. Because we hold dqptr_sem
290 * for writing and pointers were already removed from inodes we actually know that
291 * no quota for this sb+type should be held. */
292 static void invalidate_dquots(struct super_block *sb, int type)
294 struct dquot *dquot;
295 struct list_head *head;
297 spin_lock(&dq_list_lock);
298 for (head = inuse_list.next; head != &inuse_list;) {
299 dquot = list_entry(head, struct dquot, dq_inuse);
300 head = head->next;
301 if (dquot->dq_sb != sb)
302 continue;
303 if (dquot->dq_type != type)
304 continue;
305 #ifdef __DQUOT_PARANOIA
306 /* There should be no users of quota - we hold dqptr_sem for writing */
307 if (atomic_read(&dquot->dq_count))
308 BUG();
309 #endif
310 /* Quota now have no users and it has been written on last dqput() */
311 remove_dquot_hash(dquot);
312 remove_free_dquot(dquot);
313 remove_inuse(dquot);
314 kmem_cache_free(dquot_cachep, dquot);
316 spin_unlock(&dq_list_lock);
319 static int vfs_quota_sync(struct super_block *sb, int type)
321 struct list_head *head;
322 struct dquot *dquot;
323 struct quota_info *dqopt = sb_dqopt(sb);
324 int cnt;
326 down_read(&dqopt->dqptr_sem);
327 restart:
328 /* At this point any dirty dquot will definitely be written so we can clear
329 dirty flag from info */
330 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
331 if ((cnt == type || type == -1) && sb_has_quota_enabled(sb, cnt))
332 clear_bit(DQF_ANY_DQUOT_DIRTY_B, &dqopt->info[cnt].dqi_flags);
333 spin_lock(&dq_list_lock);
334 list_for_each(head, &inuse_list) {
335 dquot = list_entry(head, struct dquot, dq_inuse);
336 if (sb && dquot->dq_sb != sb)
337 continue;
338 if (type != -1 && dquot->dq_type != type)
339 continue;
340 if (!dquot->dq_sb) /* Invalidated? */
341 continue;
342 if (!dquot_dirty(dquot))
343 continue;
344 atomic_inc(&dquot->dq_count);
345 dqstats.lookups++;
346 spin_unlock(&dq_list_lock);
347 sb->dq_op->write_dquot(dquot);
348 dqput(dquot);
349 goto restart;
351 spin_unlock(&dq_list_lock);
353 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
354 if ((cnt == type || type == -1) && sb_has_quota_enabled(sb, cnt) && info_dirty(&dqopt->info[cnt])) {
355 down(&dqopt->dqio_sem);
356 dqopt->ops[cnt]->write_file_info(sb, cnt);
357 up(&dqopt->dqio_sem);
359 spin_lock(&dq_list_lock);
360 dqstats.syncs++;
361 spin_unlock(&dq_list_lock);
362 up_read(&dqopt->dqptr_sem);
364 return 0;
367 /* Free unused dquots from cache */
368 static void prune_dqcache(int count)
370 struct list_head *head;
371 struct dquot *dquot;
373 head = free_dquots.prev;
374 while (head != &free_dquots && count) {
375 dquot = list_entry(head, struct dquot, dq_free);
376 remove_dquot_hash(dquot);
377 remove_free_dquot(dquot);
378 remove_inuse(dquot);
379 kmem_cache_free(dquot_cachep, dquot);
380 count--;
381 head = free_dquots.prev;
386 * This is called from kswapd when we think we need some
387 * more memory
390 static int shrink_dqcache_memory(int nr, unsigned int gfp_mask)
392 int ret;
394 spin_lock(&dq_list_lock);
395 if (nr)
396 prune_dqcache(nr);
397 ret = dqstats.allocated_dquots;
398 spin_unlock(&dq_list_lock);
399 return ret;
403 * Put reference to dquot
404 * NOTE: If you change this function please check whether dqput_blocks() works right...
405 * MUST be called with dqptr_sem held
407 static void dqput(struct dquot *dquot)
409 if (!dquot)
410 return;
411 #ifdef __DQUOT_PARANOIA
412 if (!atomic_read(&dquot->dq_count)) {
413 printk("VFS: dqput: trying to free free dquot\n");
414 printk("VFS: device %s, dquot of %s %d\n",
415 dquot->dq_sb->s_id,
416 quotatypes[dquot->dq_type],
417 dquot->dq_id);
418 BUG();
420 #endif
422 spin_lock(&dq_list_lock);
423 dqstats.drops++;
424 spin_unlock(&dq_list_lock);
425 we_slept:
426 spin_lock(&dq_list_lock);
427 if (atomic_read(&dquot->dq_count) > 1) {
428 /* We have more than one user... nothing to do */
429 atomic_dec(&dquot->dq_count);
430 spin_unlock(&dq_list_lock);
431 return;
433 if (dquot_dirty(dquot)) {
434 spin_unlock(&dq_list_lock);
435 dquot->dq_sb->dq_op->write_dquot(dquot);
436 goto we_slept;
438 atomic_dec(&dquot->dq_count);
439 #ifdef __DQUOT_PARANOIA
440 /* sanity check */
441 if (!list_empty(&dquot->dq_free))
442 BUG();
443 #endif
444 put_dquot_last(dquot);
445 spin_unlock(&dq_list_lock);
448 static struct dquot *get_empty_dquot(struct super_block *sb, int type)
450 struct dquot *dquot;
452 dquot = kmem_cache_alloc(dquot_cachep, SLAB_KERNEL);
453 if(!dquot)
454 return NODQUOT;
456 memset((caddr_t)dquot, 0, sizeof(struct dquot));
457 sema_init(&dquot->dq_lock, 1);
458 INIT_LIST_HEAD(&dquot->dq_free);
459 INIT_LIST_HEAD(&dquot->dq_inuse);
460 INIT_LIST_HEAD(&dquot->dq_hash);
461 dquot->dq_sb = sb;
462 dquot->dq_type = type;
463 atomic_set(&dquot->dq_count, 1);
465 return dquot;
469 * Get reference to dquot
470 * MUST be called with dqptr_sem held
472 static struct dquot *dqget(struct super_block *sb, unsigned int id, int type)
474 unsigned int hashent = hashfn(sb, id, type);
475 struct dquot *dquot, *empty = NODQUOT;
477 if (!sb_has_quota_enabled(sb, type))
478 return NODQUOT;
479 we_slept:
480 spin_lock(&dq_list_lock);
481 if ((dquot = find_dquot(hashent, sb, id, type)) == NODQUOT) {
482 if (empty == NODQUOT) {
483 spin_unlock(&dq_list_lock);
484 if ((empty = get_empty_dquot(sb, type)) == NODQUOT)
485 schedule(); /* Try to wait for a moment... */
486 goto we_slept;
488 dquot = empty;
489 dquot->dq_id = id;
490 /* all dquots go on the inuse_list */
491 put_inuse(dquot);
492 /* hash it first so it can be found */
493 insert_dquot_hash(dquot);
494 dqstats.lookups++;
495 spin_unlock(&dq_list_lock);
496 read_dqblk(dquot);
497 } else {
498 if (!atomic_read(&dquot->dq_count))
499 remove_free_dquot(dquot);
500 atomic_inc(&dquot->dq_count);
501 dqstats.cache_hits++;
502 dqstats.lookups++;
503 spin_unlock(&dq_list_lock);
504 wait_on_dquot(dquot);
505 if (empty)
506 kmem_cache_free(dquot_cachep, empty);
509 #ifdef __DQUOT_PARANOIA
510 if (!dquot->dq_sb) /* Has somebody invalidated entry under us? */
511 BUG();
512 #endif
514 return dquot;
517 static int dqinit_needed(struct inode *inode, int type)
519 int cnt;
521 if (IS_NOQUOTA(inode))
522 return 0;
523 if (type != -1)
524 return inode->i_dquot[type] == NODQUOT;
525 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
526 if (inode->i_dquot[cnt] == NODQUOT)
527 return 1;
528 return 0;
531 /* This routine is guarded by dqptr_sem semaphore */
532 static void add_dquot_ref(struct super_block *sb, int type)
534 struct list_head *p;
536 restart:
537 file_list_lock();
538 list_for_each(p, &sb->s_files) {
539 struct file *filp = list_entry(p, struct file, f_list);
540 struct inode *inode = filp->f_dentry->d_inode;
541 if (filp->f_mode & FMODE_WRITE && dqinit_needed(inode, type)) {
542 struct vfsmount *mnt = mntget(filp->f_vfsmnt);
543 struct dentry *dentry = dget(filp->f_dentry);
544 file_list_unlock();
545 sb->dq_op->initialize(inode, type);
546 dput(dentry);
547 mntput(mnt);
548 /* As we may have blocked we had better restart... */
549 goto restart;
552 file_list_unlock();
555 /* Return 0 if dqput() won't block (note that 1 doesn't necessarily mean blocking) */
556 static inline int dqput_blocks(struct dquot *dquot)
558 if (atomic_read(&dquot->dq_count) <= 1 && dquot_dirty(dquot))
559 return 1;
560 return 0;
563 /* Remove references to dquots from inode - add dquot to list for freeing if needed */
564 /* We can't race with anybody because we hold dqptr_sem for writing... */
565 int remove_inode_dquot_ref(struct inode *inode, int type, struct list_head *tofree_head)
567 struct dquot *dquot = inode->i_dquot[type];
568 int cnt;
570 inode->i_dquot[type] = NODQUOT;
571 /* any other quota in use? */
572 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
573 if (inode->i_dquot[cnt] != NODQUOT)
574 goto put_it;
576 inode->i_flags &= ~S_QUOTA;
577 put_it:
578 if (dquot != NODQUOT) {
579 if (dqput_blocks(dquot)) {
580 #ifdef __DQUOT_PARANOIA
581 if (atomic_read(&dquot->dq_count) != 1)
582 printk(KERN_WARNING "VFS: Adding dquot with dq_count %d to dispose list.\n", atomic_read(&dquot->dq_count));
583 #endif
584 spin_lock(&dq_list_lock);
585 list_add(&dquot->dq_free, tofree_head); /* As dquot must have currently users it can't be on the free list... */
586 spin_unlock(&dq_list_lock);
587 return 1;
589 else
590 dqput(dquot); /* We have guaranteed we won't block */
592 return 0;
595 /* Free list of dquots - called from inode.c */
596 /* dquots are removed from inodes, no new references can be got so we are the only ones holding reference */
597 void put_dquot_list(struct list_head *tofree_head)
599 struct list_head *act_head;
600 struct dquot *dquot;
602 act_head = tofree_head->next;
603 /* So now we have dquots on the list... Just free them */
604 while (act_head != tofree_head) {
605 dquot = list_entry(act_head, struct dquot, dq_free);
606 act_head = act_head->next;
607 list_del_init(&dquot->dq_free); /* Remove dquot from the list so we won't have problems... */
608 dqput(dquot);
612 static inline void dquot_incr_inodes(struct dquot *dquot, unsigned long number)
614 dquot->dq_dqb.dqb_curinodes += number;
615 mark_dquot_dirty(dquot);
618 static inline void dquot_incr_space(struct dquot *dquot, qsize_t number)
620 dquot->dq_dqb.dqb_curspace += number;
621 mark_dquot_dirty(dquot);
624 static inline void dquot_decr_inodes(struct dquot *dquot, unsigned long number)
626 if (dquot->dq_dqb.dqb_curinodes > number)
627 dquot->dq_dqb.dqb_curinodes -= number;
628 else
629 dquot->dq_dqb.dqb_curinodes = 0;
630 if (dquot->dq_dqb.dqb_curinodes < dquot->dq_dqb.dqb_isoftlimit)
631 dquot->dq_dqb.dqb_itime = (time_t) 0;
632 clear_bit(DQ_INODES_B, &dquot->dq_flags);
633 mark_dquot_dirty(dquot);
636 static inline void dquot_decr_space(struct dquot *dquot, qsize_t number)
638 if (dquot->dq_dqb.dqb_curspace > number)
639 dquot->dq_dqb.dqb_curspace -= number;
640 else
641 dquot->dq_dqb.dqb_curspace = 0;
642 if (toqb(dquot->dq_dqb.dqb_curspace) < dquot->dq_dqb.dqb_bsoftlimit)
643 dquot->dq_dqb.dqb_btime = (time_t) 0;
644 clear_bit(DQ_BLKS_B, &dquot->dq_flags);
645 mark_dquot_dirty(dquot);
648 static inline int need_print_warning(struct dquot *dquot)
650 switch (dquot->dq_type) {
651 case USRQUOTA:
652 return current->fsuid == dquot->dq_id;
653 case GRPQUOTA:
654 return in_group_p(dquot->dq_id);
656 return 0;
659 /* Values of warnings */
660 #define NOWARN 0
661 #define IHARDWARN 1
662 #define ISOFTLONGWARN 2
663 #define ISOFTWARN 3
664 #define BHARDWARN 4
665 #define BSOFTLONGWARN 5
666 #define BSOFTWARN 6
668 /* Print warning to user which exceeded quota */
669 static void print_warning(struct dquot *dquot, const char warntype)
671 char *msg = NULL;
672 int flag = (warntype == BHARDWARN || warntype == BSOFTLONGWARN) ? DQ_BLKS_B :
673 ((warntype == IHARDWARN || warntype == ISOFTLONGWARN) ? DQ_INODES_B : 0);
675 if (!need_print_warning(dquot) || (flag && test_and_set_bit(flag, &dquot->dq_flags)))
676 return;
677 tty_write_message(current->tty, dquot->dq_sb->s_id);
678 if (warntype == ISOFTWARN || warntype == BSOFTWARN)
679 tty_write_message(current->tty, ": warning, ");
680 else
681 tty_write_message(current->tty, ": write failed, ");
682 tty_write_message(current->tty, quotatypes[dquot->dq_type]);
683 switch (warntype) {
684 case IHARDWARN:
685 msg = " file limit reached.\n";
686 break;
687 case ISOFTLONGWARN:
688 msg = " file quota exceeded too long.\n";
689 break;
690 case ISOFTWARN:
691 msg = " file quota exceeded.\n";
692 break;
693 case BHARDWARN:
694 msg = " block limit reached.\n";
695 break;
696 case BSOFTLONGWARN:
697 msg = " block quota exceeded too long.\n";
698 break;
699 case BSOFTWARN:
700 msg = " block quota exceeded.\n";
701 break;
703 tty_write_message(current->tty, msg);
706 static inline void flush_warnings(struct dquot **dquots, char *warntype)
708 int i;
710 for (i = 0; i < MAXQUOTAS; i++)
711 if (dquots[i] != NODQUOT && warntype[i] != NOWARN)
712 print_warning(dquots[i], warntype[i]);
715 static inline char ignore_hardlimit(struct dquot *dquot)
717 struct mem_dqinfo *info = &sb_dqopt(dquot->dq_sb)->info[dquot->dq_type];
719 return capable(CAP_SYS_RESOURCE) &&
720 (info->dqi_format->qf_fmt_id != QFMT_VFS_OLD || !(info->dqi_flags & V1_DQF_RSQUASH));
723 /* needs dq_data_lock */
724 static int check_idq(struct dquot *dquot, ulong inodes, char *warntype)
726 *warntype = NOWARN;
727 if (inodes <= 0 || test_bit(DQ_FAKE_B, &dquot->dq_flags))
728 return QUOTA_OK;
730 if (dquot->dq_dqb.dqb_ihardlimit &&
731 (dquot->dq_dqb.dqb_curinodes + inodes) > dquot->dq_dqb.dqb_ihardlimit &&
732 !ignore_hardlimit(dquot)) {
733 *warntype = IHARDWARN;
734 return NO_QUOTA;
737 if (dquot->dq_dqb.dqb_isoftlimit &&
738 (dquot->dq_dqb.dqb_curinodes + inodes) > dquot->dq_dqb.dqb_isoftlimit &&
739 dquot->dq_dqb.dqb_itime && get_seconds() >= dquot->dq_dqb.dqb_itime &&
740 !ignore_hardlimit(dquot)) {
741 *warntype = ISOFTLONGWARN;
742 return NO_QUOTA;
745 if (dquot->dq_dqb.dqb_isoftlimit &&
746 (dquot->dq_dqb.dqb_curinodes + inodes) > dquot->dq_dqb.dqb_isoftlimit &&
747 dquot->dq_dqb.dqb_itime == 0) {
748 *warntype = ISOFTWARN;
749 dquot->dq_dqb.dqb_itime = get_seconds() + sb_dqopt(dquot->dq_sb)->info[dquot->dq_type].dqi_igrace;
752 return QUOTA_OK;
755 /* needs dq_data_lock */
756 static int check_bdq(struct dquot *dquot, qsize_t space, int prealloc, char *warntype)
758 *warntype = 0;
759 if (space <= 0 || test_bit(DQ_FAKE_B, &dquot->dq_flags))
760 return QUOTA_OK;
762 if (dquot->dq_dqb.dqb_bhardlimit &&
763 toqb(dquot->dq_dqb.dqb_curspace + space) > dquot->dq_dqb.dqb_bhardlimit &&
764 !ignore_hardlimit(dquot)) {
765 if (!prealloc)
766 *warntype = BHARDWARN;
767 return NO_QUOTA;
770 if (dquot->dq_dqb.dqb_bsoftlimit &&
771 toqb(dquot->dq_dqb.dqb_curspace + space) > dquot->dq_dqb.dqb_bsoftlimit &&
772 dquot->dq_dqb.dqb_btime && get_seconds() >= dquot->dq_dqb.dqb_btime &&
773 !ignore_hardlimit(dquot)) {
774 if (!prealloc)
775 *warntype = BSOFTLONGWARN;
776 return NO_QUOTA;
779 if (dquot->dq_dqb.dqb_bsoftlimit &&
780 toqb(dquot->dq_dqb.dqb_curspace + space) > dquot->dq_dqb.dqb_bsoftlimit &&
781 dquot->dq_dqb.dqb_btime == 0) {
782 if (!prealloc) {
783 *warntype = BSOFTWARN;
784 dquot->dq_dqb.dqb_btime = get_seconds() + sb_dqopt(dquot->dq_sb)->info[dquot->dq_type].dqi_bgrace;
786 else
788 * We don't allow preallocation to exceed softlimit so exceeding will
789 * be always printed
791 return NO_QUOTA;
794 return QUOTA_OK;
798 * Externally referenced functions through dquot_operations in inode.
800 * Note: this is a blocking operation.
802 void dquot_initialize(struct inode *inode, int type)
804 unsigned int id = 0;
805 int cnt;
807 down_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
808 /* Having dqptr_sem we know NOQUOTA flags can't be altered... */
809 if (IS_NOQUOTA(inode)) {
810 up_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
811 return;
813 /* Build list of quotas to initialize... */
814 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
815 if (type != -1 && cnt != type)
816 continue;
817 if (inode->i_dquot[cnt] == NODQUOT) {
818 switch (cnt) {
819 case USRQUOTA:
820 id = inode->i_uid;
821 break;
822 case GRPQUOTA:
823 id = inode->i_gid;
824 break;
826 inode->i_dquot[cnt] = dqget(inode->i_sb, id, cnt);
827 if (inode->i_dquot[cnt])
828 inode->i_flags |= S_QUOTA;
831 up_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
835 * Remove references to quota from inode
836 * This function needs dqptr_sem for writing
838 static void dquot_drop_iupdate(struct inode *inode, struct dquot **to_drop)
840 int cnt;
842 inode->i_flags &= ~S_QUOTA;
843 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
844 to_drop[cnt] = inode->i_dquot[cnt];
845 inode->i_dquot[cnt] = NODQUOT;
850 * Release all quotas referenced by inode
852 void dquot_drop(struct inode *inode)
854 struct dquot *to_drop[MAXQUOTAS];
855 int cnt;
857 down_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
858 dquot_drop_iupdate(inode, to_drop);
859 up_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
860 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
861 if (to_drop[cnt] != NODQUOT)
862 dqput(to_drop[cnt]);
866 * Release all quotas referenced by inode.
867 * This function assumes dqptr_sem for writing
869 void dquot_drop_nolock(struct inode *inode)
871 struct dquot *to_drop[MAXQUOTAS];
872 int cnt;
874 dquot_drop_iupdate(inode, to_drop);
875 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
876 if (to_drop[cnt] != NODQUOT)
877 dqput(to_drop[cnt]);
881 * This operation can block, but only after everything is updated
883 int dquot_alloc_space(struct inode *inode, qsize_t number, int warn)
885 int cnt, ret = NO_QUOTA;
886 char warntype[MAXQUOTAS];
888 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
889 warntype[cnt] = NOWARN;
891 down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
892 spin_lock(&dq_data_lock);
893 if (IS_NOQUOTA(inode))
894 goto add_bytes;
895 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
896 if (inode->i_dquot[cnt] == NODQUOT)
897 continue;
898 if (check_bdq(inode->i_dquot[cnt], number, warn, warntype+cnt) == NO_QUOTA)
899 goto warn_put_all;
901 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
902 if (inode->i_dquot[cnt] == NODQUOT)
903 continue;
904 dquot_incr_space(inode->i_dquot[cnt], number);
906 add_bytes:
907 inode_add_bytes(inode, number);
908 ret = QUOTA_OK;
909 warn_put_all:
910 spin_unlock(&dq_data_lock);
911 flush_warnings(inode->i_dquot, warntype);
912 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
913 return ret;
917 * This operation can block, but only after everything is updated
919 int dquot_alloc_inode(const struct inode *inode, unsigned long number)
921 int cnt, ret = NO_QUOTA;
922 char warntype[MAXQUOTAS];
924 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
925 warntype[cnt] = NOWARN;
926 down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
927 if (IS_NOQUOTA(inode)) {
928 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
929 return QUOTA_OK;
931 spin_lock(&dq_data_lock);
932 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
933 if (inode->i_dquot[cnt] == NODQUOT)
934 continue;
935 if (check_idq(inode->i_dquot[cnt], number, warntype+cnt) == NO_QUOTA)
936 goto warn_put_all;
939 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
940 if (inode->i_dquot[cnt] == NODQUOT)
941 continue;
942 dquot_incr_inodes(inode->i_dquot[cnt], number);
944 ret = QUOTA_OK;
945 warn_put_all:
946 spin_unlock(&dq_data_lock);
947 flush_warnings((struct dquot **)inode->i_dquot, warntype);
948 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
949 return ret;
953 * This is a non-blocking operation.
955 void dquot_free_space(struct inode *inode, qsize_t number)
957 unsigned int cnt;
959 down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
960 spin_lock(&dq_data_lock);
961 if (IS_NOQUOTA(inode))
962 goto sub_bytes;
963 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
964 if (inode->i_dquot[cnt] == NODQUOT)
965 continue;
966 dquot_decr_space(inode->i_dquot[cnt], number);
968 sub_bytes:
969 inode_sub_bytes(inode, number);
970 spin_unlock(&dq_data_lock);
971 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
975 * This is a non-blocking operation.
977 void dquot_free_inode(const struct inode *inode, unsigned long number)
979 unsigned int cnt;
981 down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
982 if (IS_NOQUOTA(inode)) {
983 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
984 return;
986 spin_lock(&dq_data_lock);
987 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
988 if (inode->i_dquot[cnt] == NODQUOT)
989 continue;
990 dquot_decr_inodes(inode->i_dquot[cnt], number);
992 spin_unlock(&dq_data_lock);
993 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
997 * Transfer the number of inode and blocks from one diskquota to an other.
999 * This operation can block, but only after everything is updated
1001 int dquot_transfer(struct inode *inode, struct iattr *iattr)
1003 qsize_t space;
1004 struct dquot *transfer_from[MAXQUOTAS];
1005 struct dquot *transfer_to[MAXQUOTAS];
1006 int cnt, ret = NO_QUOTA, chuid = (iattr->ia_valid & ATTR_UID) && inode->i_uid != iattr->ia_uid,
1007 chgid = (iattr->ia_valid & ATTR_GID) && inode->i_gid != iattr->ia_gid;
1008 char warntype[MAXQUOTAS];
1010 /* Clear the arrays */
1011 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1012 transfer_to[cnt] = transfer_from[cnt] = NODQUOT;
1013 warntype[cnt] = NOWARN;
1015 down_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
1016 if (IS_NOQUOTA(inode)) { /* File without quota accounting? */
1017 up_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
1018 return QUOTA_OK;
1020 /* First build the transfer_to list - here we can block on reading of dquots... */
1021 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1022 switch (cnt) {
1023 case USRQUOTA:
1024 if (!chuid)
1025 continue;
1026 transfer_to[cnt] = dqget(inode->i_sb, iattr->ia_uid, cnt);
1027 break;
1028 case GRPQUOTA:
1029 if (!chgid)
1030 continue;
1031 transfer_to[cnt] = dqget(inode->i_sb, iattr->ia_gid, cnt);
1032 break;
1035 spin_lock(&dq_data_lock);
1036 space = inode_get_bytes(inode);
1037 /* Build the transfer_from list and check the limits */
1038 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1039 if (transfer_to[cnt] == NODQUOT)
1040 continue;
1041 transfer_from[cnt] = inode->i_dquot[cnt];
1042 if (check_idq(transfer_to[cnt], 1, warntype+cnt) == NO_QUOTA ||
1043 check_bdq(transfer_to[cnt], space, 0, warntype+cnt) == NO_QUOTA)
1044 goto warn_put_all;
1048 * Finally perform the needed transfer from transfer_from to transfer_to
1050 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1052 * Skip changes for same uid or gid or for turned off quota-type.
1054 if (transfer_to[cnt] == NODQUOT)
1055 continue;
1057 dquot_decr_inodes(transfer_from[cnt], 1);
1058 dquot_decr_space(transfer_from[cnt], space);
1060 dquot_incr_inodes(transfer_to[cnt], 1);
1061 dquot_incr_space(transfer_to[cnt], space);
1063 inode->i_dquot[cnt] = transfer_to[cnt];
1065 ret = QUOTA_OK;
1066 warn_put_all:
1067 spin_unlock(&dq_data_lock);
1068 flush_warnings(transfer_to, warntype);
1070 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1071 if (ret == QUOTA_OK && transfer_from[cnt] != NODQUOT)
1072 dqput(transfer_from[cnt]);
1073 if (ret == NO_QUOTA && transfer_to[cnt] != NODQUOT)
1074 dqput(transfer_to[cnt]);
1076 up_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
1077 return ret;
1081 * Definitions of diskquota operations.
1083 struct dquot_operations dquot_operations = {
1084 .initialize = dquot_initialize, /* mandatory */
1085 .drop = dquot_drop, /* mandatory */
1086 .alloc_space = dquot_alloc_space,
1087 .alloc_inode = dquot_alloc_inode,
1088 .free_space = dquot_free_space,
1089 .free_inode = dquot_free_inode,
1090 .transfer = dquot_transfer,
1091 .write_dquot = commit_dqblk
1094 /* Function used by filesystems for initializing the dquot_operations structure */
1095 void init_dquot_operations(struct dquot_operations *fsdqops)
1097 memcpy(fsdqops, &dquot_operations, sizeof(dquot_operations));
1100 static inline void set_enable_flags(struct quota_info *dqopt, int type)
1102 switch (type) {
1103 case USRQUOTA:
1104 dqopt->flags |= DQUOT_USR_ENABLED;
1105 break;
1106 case GRPQUOTA:
1107 dqopt->flags |= DQUOT_GRP_ENABLED;
1108 break;
1112 static inline void reset_enable_flags(struct quota_info *dqopt, int type)
1114 switch (type) {
1115 case USRQUOTA:
1116 dqopt->flags &= ~DQUOT_USR_ENABLED;
1117 break;
1118 case GRPQUOTA:
1119 dqopt->flags &= ~DQUOT_GRP_ENABLED;
1120 break;
1124 /* Function in inode.c - remove pointers to dquots in icache */
1125 extern void remove_dquot_ref(struct super_block *, int);
1128 * Turn quota off on a device. type == -1 ==> quotaoff for all types (umount)
1130 int vfs_quota_off(struct super_block *sb, int type)
1132 int cnt;
1133 struct quota_info *dqopt = sb_dqopt(sb);
1135 if (!sb)
1136 goto out;
1138 /* We need to serialize quota_off() for device */
1139 down(&dqopt->dqonoff_sem);
1140 down_write(&dqopt->dqptr_sem);
1141 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1142 if (type != -1 && cnt != type)
1143 continue;
1144 if (!sb_has_quota_enabled(sb, cnt))
1145 continue;
1146 reset_enable_flags(dqopt, cnt);
1148 /* Note: these are blocking operations */
1149 remove_dquot_ref(sb, cnt);
1150 invalidate_dquots(sb, cnt);
1152 * Now all dquots should be invalidated, all writes done so we should be only
1153 * users of the info. No locks needed.
1155 if (info_dirty(&dqopt->info[cnt])) {
1156 down(&dqopt->dqio_sem);
1157 dqopt->ops[cnt]->write_file_info(sb, cnt);
1158 up(&dqopt->dqio_sem);
1160 if (dqopt->ops[cnt]->free_file_info)
1161 dqopt->ops[cnt]->free_file_info(sb, cnt);
1162 put_quota_format(dqopt->info[cnt].dqi_format);
1164 fput(dqopt->files[cnt]);
1165 dqopt->files[cnt] = (struct file *)NULL;
1166 dqopt->info[cnt].dqi_flags = 0;
1167 dqopt->info[cnt].dqi_igrace = 0;
1168 dqopt->info[cnt].dqi_bgrace = 0;
1169 dqopt->ops[cnt] = NULL;
1171 up_write(&dqopt->dqptr_sem);
1172 up(&dqopt->dqonoff_sem);
1173 out:
1174 return 0;
1177 int vfs_quota_on(struct super_block *sb, int type, int format_id, char *path)
1179 struct file *f;
1180 struct inode *inode;
1181 struct quota_info *dqopt = sb_dqopt(sb);
1182 struct quota_format_type *fmt = find_quota_format(format_id);
1183 int error;
1184 unsigned int oldflags;
1186 if (!fmt)
1187 return -ESRCH;
1188 f = filp_open(path, O_RDWR, 0600);
1189 if (IS_ERR(f)) {
1190 error = PTR_ERR(f);
1191 goto out_fmt;
1193 error = -EIO;
1194 if (!f->f_op || !f->f_op->read || !f->f_op->write)
1195 goto out_f;
1196 error = security_quota_on(f);
1197 if (error)
1198 goto out_f;
1199 inode = f->f_dentry->d_inode;
1200 error = -EACCES;
1201 if (!S_ISREG(inode->i_mode))
1202 goto out_f;
1204 down(&dqopt->dqonoff_sem);
1205 down_write(&dqopt->dqptr_sem);
1206 if (sb_has_quota_enabled(sb, type)) {
1207 error = -EBUSY;
1208 goto out_lock;
1210 oldflags = inode->i_flags;
1211 dqopt->files[type] = f;
1212 error = -EINVAL;
1213 if (!fmt->qf_ops->check_quota_file(sb, type))
1214 goto out_file_init;
1215 /* We don't want quota and atime on quota files (deadlocks possible) */
1216 dquot_drop_nolock(inode);
1217 inode->i_flags |= S_NOQUOTA | S_NOATIME;
1219 dqopt->ops[type] = fmt->qf_ops;
1220 dqopt->info[type].dqi_format = fmt;
1221 down(&dqopt->dqio_sem);
1222 if ((error = dqopt->ops[type]->read_file_info(sb, type)) < 0) {
1223 up(&dqopt->dqio_sem);
1224 goto out_file_init;
1226 up(&dqopt->dqio_sem);
1227 set_enable_flags(dqopt, type);
1228 up_write(&dqopt->dqptr_sem);
1230 add_dquot_ref(sb, type);
1231 up(&dqopt->dqonoff_sem);
1233 return 0;
1235 out_file_init:
1236 inode->i_flags = oldflags;
1237 dqopt->files[type] = NULL;
1238 out_lock:
1239 up_write(&dqopt->dqptr_sem);
1240 up(&dqopt->dqonoff_sem);
1241 out_f:
1242 filp_close(f, NULL);
1243 out_fmt:
1244 put_quota_format(fmt);
1246 return error;
1249 /* Generic routine for getting common part of quota structure */
1250 static void do_get_dqblk(struct dquot *dquot, struct if_dqblk *di)
1252 struct mem_dqblk *dm = &dquot->dq_dqb;
1254 spin_lock(&dq_data_lock);
1255 di->dqb_bhardlimit = dm->dqb_bhardlimit;
1256 di->dqb_bsoftlimit = dm->dqb_bsoftlimit;
1257 di->dqb_curspace = dm->dqb_curspace;
1258 di->dqb_ihardlimit = dm->dqb_ihardlimit;
1259 di->dqb_isoftlimit = dm->dqb_isoftlimit;
1260 di->dqb_curinodes = dm->dqb_curinodes;
1261 di->dqb_btime = dm->dqb_btime;
1262 di->dqb_itime = dm->dqb_itime;
1263 di->dqb_valid = QIF_ALL;
1264 spin_unlock(&dq_data_lock);
1267 int vfs_get_dqblk(struct super_block *sb, int type, qid_t id, struct if_dqblk *di)
1269 struct dquot *dquot;
1271 down_read(&sb_dqopt(sb)->dqptr_sem);
1272 if (!(dquot = dqget(sb, id, type))) {
1273 up_read(&sb_dqopt(sb)->dqptr_sem);
1274 return -ESRCH;
1276 do_get_dqblk(dquot, di);
1277 dqput(dquot);
1278 up_read(&sb_dqopt(sb)->dqptr_sem);
1279 return 0;
1282 /* Generic routine for setting common part of quota structure */
1283 static void do_set_dqblk(struct dquot *dquot, struct if_dqblk *di)
1285 struct mem_dqblk *dm = &dquot->dq_dqb;
1286 int check_blim = 0, check_ilim = 0;
1288 spin_lock(&dq_data_lock);
1289 if (di->dqb_valid & QIF_SPACE) {
1290 dm->dqb_curspace = di->dqb_curspace;
1291 check_blim = 1;
1293 if (di->dqb_valid & QIF_BLIMITS) {
1294 dm->dqb_bsoftlimit = di->dqb_bsoftlimit;
1295 dm->dqb_bhardlimit = di->dqb_bhardlimit;
1296 check_blim = 1;
1298 if (di->dqb_valid & QIF_INODES) {
1299 dm->dqb_curinodes = di->dqb_curinodes;
1300 check_ilim = 1;
1302 if (di->dqb_valid & QIF_ILIMITS) {
1303 dm->dqb_isoftlimit = di->dqb_isoftlimit;
1304 dm->dqb_ihardlimit = di->dqb_ihardlimit;
1305 check_ilim = 1;
1307 if (di->dqb_valid & QIF_BTIME)
1308 dm->dqb_btime = di->dqb_btime;
1309 if (di->dqb_valid & QIF_ITIME)
1310 dm->dqb_itime = di->dqb_itime;
1312 if (check_blim) {
1313 if (!dm->dqb_bsoftlimit || toqb(dm->dqb_curspace) < dm->dqb_bsoftlimit) {
1314 dm->dqb_btime = 0;
1315 clear_bit(DQ_BLKS_B, &dquot->dq_flags);
1317 else if (!(di->dqb_valid & QIF_BTIME)) /* Set grace only if user hasn't provided his own... */
1318 dm->dqb_btime = get_seconds() + sb_dqopt(dquot->dq_sb)->info[dquot->dq_type].dqi_bgrace;
1320 if (check_ilim) {
1321 if (!dm->dqb_isoftlimit || dm->dqb_curinodes < dm->dqb_isoftlimit) {
1322 dm->dqb_itime = 0;
1323 clear_bit(DQ_INODES_B, &dquot->dq_flags);
1325 else if (!(di->dqb_valid & QIF_ITIME)) /* Set grace only if user hasn't provided his own... */
1326 dm->dqb_itime = get_seconds() + sb_dqopt(dquot->dq_sb)->info[dquot->dq_type].dqi_igrace;
1328 if (dm->dqb_bhardlimit || dm->dqb_bsoftlimit || dm->dqb_ihardlimit || dm->dqb_isoftlimit)
1329 clear_bit(DQ_FAKE_B, &dquot->dq_flags);
1330 else
1331 set_bit(DQ_FAKE_B, &dquot->dq_flags);
1332 mark_dquot_dirty(dquot);
1333 spin_unlock(&dq_data_lock);
1336 int vfs_set_dqblk(struct super_block *sb, int type, qid_t id, struct if_dqblk *di)
1338 struct dquot *dquot;
1340 down_read(&sb_dqopt(sb)->dqptr_sem);
1341 if (!(dquot = dqget(sb, id, type))) {
1342 up_read(&sb_dqopt(sb)->dqptr_sem);
1343 return -ESRCH;
1345 do_set_dqblk(dquot, di);
1346 dqput(dquot);
1347 up_read(&sb_dqopt(sb)->dqptr_sem);
1348 return 0;
1351 /* Generic routine for getting common part of quota file information */
1352 int vfs_get_dqinfo(struct super_block *sb, int type, struct if_dqinfo *ii)
1354 struct mem_dqinfo *mi;
1356 down_read(&sb_dqopt(sb)->dqptr_sem);
1357 if (!sb_has_quota_enabled(sb, type)) {
1358 up_read(&sb_dqopt(sb)->dqptr_sem);
1359 return -ESRCH;
1361 mi = sb_dqopt(sb)->info + type;
1362 spin_lock(&dq_data_lock);
1363 ii->dqi_bgrace = mi->dqi_bgrace;
1364 ii->dqi_igrace = mi->dqi_igrace;
1365 ii->dqi_flags = mi->dqi_flags & DQF_MASK;
1366 ii->dqi_valid = IIF_ALL;
1367 spin_unlock(&dq_data_lock);
1368 up_read(&sb_dqopt(sb)->dqptr_sem);
1369 return 0;
1372 /* Generic routine for setting common part of quota file information */
1373 int vfs_set_dqinfo(struct super_block *sb, int type, struct if_dqinfo *ii)
1375 struct mem_dqinfo *mi;
1377 down_read(&sb_dqopt(sb)->dqptr_sem);
1378 if (!sb_has_quota_enabled(sb, type)) {
1379 up_read(&sb_dqopt(sb)->dqptr_sem);
1380 return -ESRCH;
1382 mi = sb_dqopt(sb)->info + type;
1383 spin_lock(&dq_data_lock);
1384 if (ii->dqi_valid & IIF_BGRACE)
1385 mi->dqi_bgrace = ii->dqi_bgrace;
1386 if (ii->dqi_valid & IIF_IGRACE)
1387 mi->dqi_igrace = ii->dqi_igrace;
1388 if (ii->dqi_valid & IIF_FLAGS)
1389 mi->dqi_flags = (mi->dqi_flags & ~DQF_MASK) | (ii->dqi_flags & DQF_MASK);
1390 mark_info_dirty(mi);
1391 spin_unlock(&dq_data_lock);
1392 up_read(&sb_dqopt(sb)->dqptr_sem);
1393 return 0;
1396 struct quotactl_ops vfs_quotactl_ops = {
1397 .quota_on = vfs_quota_on,
1398 .quota_off = vfs_quota_off,
1399 .quota_sync = vfs_quota_sync,
1400 .get_info = vfs_get_dqinfo,
1401 .set_info = vfs_set_dqinfo,
1402 .get_dqblk = vfs_get_dqblk,
1403 .set_dqblk = vfs_set_dqblk
1406 static ctl_table fs_dqstats_table[] = {
1408 .ctl_name = FS_DQ_LOOKUPS,
1409 .procname = "lookups",
1410 .data = &dqstats.lookups,
1411 .maxlen = sizeof(int),
1412 .mode = 0444,
1413 .proc_handler = &proc_dointvec,
1416 .ctl_name = FS_DQ_DROPS,
1417 .procname = "drops",
1418 .data = &dqstats.drops,
1419 .maxlen = sizeof(int),
1420 .mode = 0444,
1421 .proc_handler = &proc_dointvec,
1424 .ctl_name = FS_DQ_READS,
1425 .procname = "reads",
1426 .data = &dqstats.reads,
1427 .maxlen = sizeof(int),
1428 .mode = 0444,
1429 .proc_handler = &proc_dointvec,
1432 .ctl_name = FS_DQ_WRITES,
1433 .procname = "writes",
1434 .data = &dqstats.writes,
1435 .maxlen = sizeof(int),
1436 .mode = 0444,
1437 .proc_handler = &proc_dointvec,
1440 .ctl_name = FS_DQ_CACHE_HITS,
1441 .procname = "cache_hits",
1442 .data = &dqstats.cache_hits,
1443 .maxlen = sizeof(int),
1444 .mode = 0444,
1445 .proc_handler = &proc_dointvec,
1448 .ctl_name = FS_DQ_ALLOCATED,
1449 .procname = "allocated_dquots",
1450 .data = &dqstats.allocated_dquots,
1451 .maxlen = sizeof(int),
1452 .mode = 0444,
1453 .proc_handler = &proc_dointvec,
1456 .ctl_name = FS_DQ_FREE,
1457 .procname = "free_dquots",
1458 .data = &dqstats.free_dquots,
1459 .maxlen = sizeof(int),
1460 .mode = 0444,
1461 .proc_handler = &proc_dointvec,
1464 .ctl_name = FS_DQ_SYNCS,
1465 .procname = "syncs",
1466 .data = &dqstats.syncs,
1467 .maxlen = sizeof(int),
1468 .mode = 0444,
1469 .proc_handler = &proc_dointvec,
1471 { .ctl_name = 0 },
1474 static ctl_table fs_table[] = {
1476 .ctl_name = FS_DQSTATS,
1477 .procname = "quota",
1478 .mode = 0555,
1479 .child = fs_dqstats_table,
1481 { .ctl_name = 0 },
1484 static ctl_table sys_table[] = {
1486 .ctl_name = CTL_FS,
1487 .procname = "fs",
1488 .mode = 0555,
1489 .child = fs_table,
1491 { .ctl_name = 0 },
1494 /* SLAB cache for dquot structures */
1495 kmem_cache_t *dquot_cachep;
1497 static int __init dquot_init(void)
1499 int i;
1501 register_sysctl_table(sys_table, 0);
1502 for (i = 0; i < NR_DQHASH; i++)
1503 INIT_LIST_HEAD(dquot_hash + i);
1504 printk(KERN_NOTICE "VFS: Disk quotas %s\n", __DQUOT_VERSION__);
1506 dquot_cachep = kmem_cache_create("dquot",
1507 sizeof(struct dquot), sizeof(unsigned long) * 4,
1508 SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT, NULL, NULL);
1509 if (!dquot_cachep)
1510 panic("Cannot create dquot SLAB cache");
1512 set_shrinker(DEFAULT_SEEKS, shrink_dqcache_memory);
1514 return 0;
1516 module_init(dquot_init);
1518 EXPORT_SYMBOL(register_quota_format);
1519 EXPORT_SYMBOL(unregister_quota_format);
1520 EXPORT_SYMBOL(dqstats);
1521 EXPORT_SYMBOL(dq_list_lock);
1522 EXPORT_SYMBOL(dq_data_lock);
1523 EXPORT_SYMBOL(init_dquot_operations);