blob: 9376a4378988b0d9217edfc39010975b2def6719 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Implementation of the diskquota system for the LINUX operating system. QUOTA
3 * is implemented using the BSD system call interface as the means of
4 * communication with the user level. This file contains the generic routines
5 * called by the different filesystems on allocation of an inode or block.
6 * These routines take care of the administration needed to have a consistent
7 * diskquota tracking system. The ideas of both user and group quotas are based
8 * on the Melbourne quota system as used on BSD derived systems. The internal
9 * implementation is based on one of the several variants of the LINUX
10 * inode-subsystem with added complexity of the diskquota system.
11 *
12 * Version: $Id: dquot.c,v 6.3 1996/11/17 18:35:34 mvw Exp mvw $
13 *
14 * Author: Marco van Wieringen <mvw@planets.elm.net>
15 *
16 * Fixes: Dmitry Gorodchanin <pgmdsg@ibi.com>, 11 Feb 96
17 *
18 * Revised list management to avoid races
19 * -- Bill Hawes, <whawes@star.net>, 9/98
20 *
21 * Fixed races in dquot_transfer(), dqget() and dquot_alloc_...().
22 * As the consequence the locking was moved from dquot_decr_...(),
23 * dquot_incr_...() to calling functions.
24 * invalidate_dquots() now writes modified dquots.
25 * Serialized quota_off() and quota_on() for mount point.
26 * Fixed a few bugs in grow_dquots().
27 * Fixed deadlock in write_dquot() - we no longer account quotas on
28 * quota files
29 * remove_dquot_ref() moved to inode.c - it now traverses through inodes
30 * add_dquot_ref() restarts after blocking
31 * Added check for bogus uid and fixed check for group in quotactl.
32 * Jan Kara, <jack@suse.cz>, sponsored by SuSE CR, 10-11/99
33 *
34 * Used struct list_head instead of own list struct
35 * Invalidation of referenced dquots is no longer possible
36 * Improved free_dquots list management
37 * Quota and i_blocks are now updated in one place to avoid races
38 * Warnings are now delayed so we won't block in critical section
39 * Write updated not to require dquot lock
40 * Jan Kara, <jack@suse.cz>, 9/2000
41 *
42 * Added dynamic quota structure allocation
43 * Jan Kara <jack@suse.cz> 12/2000
44 *
45 * Rewritten quota interface. Implemented new quota format and
46 * formats registering.
47 * Jan Kara, <jack@suse.cz>, 2001,2002
48 *
49 * New SMP locking.
50 * Jan Kara, <jack@suse.cz>, 10/2002
51 *
52 * Added journalled quota support, fix lock inversion problems
53 * Jan Kara, <jack@suse.cz>, 2003,2004
54 *
55 * (C) Copyright 1994 - 1997 Marco van Wieringen
56 */
57
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>
78#include <linux/namei.h>
79#include <linux/buffer_head.h>
Randy Dunlap16f7e0f2006-01-11 12:17:46 -080080#include <linux/capability.h>
Adrian Bunkbe586ba2005-11-07 00:59:35 -080081#include <linux/quotaops.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070082
83#include <asm/uaccess.h>
84
85#define __DQUOT_PARANOIA
86
87/*
88 * There are two quota SMP locks. dq_list_lock protects all lists with quotas
89 * and quota formats and also dqstats structure containing statistics about the
90 * lists. dq_data_lock protects data from dq_dqb and also mem_dqinfo structures
91 * and also guards consistency of dquot->dq_dqb with inode->i_blocks, i_bytes.
92 * i_blocks and i_bytes updates itself are guarded by i_lock acquired directly
93 * in inode_add_bytes() and inode_sub_bytes().
94 *
95 * The spinlock ordering is hence: dq_data_lock > dq_list_lock > i_lock
96 *
97 * Note that some things (eg. sb pointer, type, id) doesn't change during
98 * the life of the dquot structure and so needn't to be protected by a lock
99 *
100 * Any operation working on dquots via inode pointers must hold dqptr_sem. If
101 * operation is just reading pointers from inode (or not using them at all) the
102 * read lock is enough. If pointers are altered function must hold write lock
103 * (these locking rules also apply for S_NOQUOTA flag in the inode - note that
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800104 * for altering the flag i_mutex is also needed). If operation is holding
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 * reference to dquot in other way (e.g. quotactl ops) it must be guarded by
106 * dqonoff_sem.
107 * This locking assures that:
108 * a) update/access to dquot pointers in inode is serialized
109 * b) everyone is guarded against invalidate_dquots()
110 *
111 * Each dquot has its dq_lock semaphore. Locked dquots might not be referenced
112 * from inodes (dquot_alloc_space() and such don't check the dq_lock).
113 * Currently dquot is locked only when it is being read to memory (or space for
114 * it is being allocated) on the first dqget() and when it is being released on
115 * the last dqput(). The allocation and release oparations are serialized by
116 * the dq_lock and by checking the use count in dquot_release(). Write
117 * operations on dquots don't hold dq_lock as they copy data under dq_data_lock
118 * spinlock to internal buffers before writing.
119 *
120 * Lock ordering (including related VFS locks) is the following:
Jan Kara6362e4d2006-03-23 03:00:17 -0800121 * i_mutex > dqonoff_sem > journal_lock > dqptr_sem > dquot->dq_lock > dqio_sem
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800122 * i_mutex on quota files is special (it's below dqio_sem)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 */
124
125static DEFINE_SPINLOCK(dq_list_lock);
126DEFINE_SPINLOCK(dq_data_lock);
127
128static char *quotatypes[] = INITQFNAMES;
129static struct quota_format_type *quota_formats; /* List of registered formats */
130static struct quota_module_name module_names[] = INIT_QUOTA_MODULE_NAMES;
131
132/* SLAB cache for dquot structures */
133static kmem_cache_t *dquot_cachep;
134
135int register_quota_format(struct quota_format_type *fmt)
136{
137 spin_lock(&dq_list_lock);
138 fmt->qf_next = quota_formats;
139 quota_formats = fmt;
140 spin_unlock(&dq_list_lock);
141 return 0;
142}
143
144void unregister_quota_format(struct quota_format_type *fmt)
145{
146 struct quota_format_type **actqf;
147
148 spin_lock(&dq_list_lock);
149 for (actqf = &quota_formats; *actqf && *actqf != fmt; actqf = &(*actqf)->qf_next);
150 if (*actqf)
151 *actqf = (*actqf)->qf_next;
152 spin_unlock(&dq_list_lock);
153}
154
155static struct quota_format_type *find_quota_format(int id)
156{
157 struct quota_format_type *actqf;
158
159 spin_lock(&dq_list_lock);
160 for (actqf = quota_formats; actqf && actqf->qf_fmt_id != id; actqf = actqf->qf_next);
161 if (!actqf || !try_module_get(actqf->qf_owner)) {
162 int qm;
163
164 spin_unlock(&dq_list_lock);
165
166 for (qm = 0; module_names[qm].qm_fmt_id && module_names[qm].qm_fmt_id != id; qm++);
167 if (!module_names[qm].qm_fmt_id || request_module(module_names[qm].qm_mod_name))
168 return NULL;
169
170 spin_lock(&dq_list_lock);
171 for (actqf = quota_formats; actqf && actqf->qf_fmt_id != id; actqf = actqf->qf_next);
172 if (actqf && !try_module_get(actqf->qf_owner))
173 actqf = NULL;
174 }
175 spin_unlock(&dq_list_lock);
176 return actqf;
177}
178
179static void put_quota_format(struct quota_format_type *fmt)
180{
181 module_put(fmt->qf_owner);
182}
183
184/*
185 * Dquot List Management:
186 * The quota code uses three lists for dquot management: the inuse_list,
187 * free_dquots, and dquot_hash[] array. A single dquot structure may be
188 * on all three lists, depending on its current state.
189 *
190 * All dquots are placed to the end of inuse_list when first created, and this
191 * list is used for invalidate operation, which must look at every dquot.
192 *
193 * Unused dquots (dq_count == 0) are added to the free_dquots list when freed,
194 * and this list is searched whenever we need an available dquot. Dquots are
195 * removed from the list as soon as they are used again, and
196 * dqstats.free_dquots gives the number of dquots on the list. When
197 * dquot is invalidated it's completely released from memory.
198 *
199 * Dquots with a specific identity (device, type and id) are placed on
200 * one of the dquot_hash[] hash chains. The provides an efficient search
201 * mechanism to locate a specific dquot.
202 */
203
204static LIST_HEAD(inuse_list);
205static LIST_HEAD(free_dquots);
206static unsigned int dq_hash_bits, dq_hash_mask;
207static struct hlist_head *dquot_hash;
208
209struct dqstats dqstats;
210
211static void dqput(struct dquot *dquot);
212
213static inline unsigned int
214hashfn(const struct super_block *sb, unsigned int id, int type)
215{
216 unsigned long tmp;
217
218 tmp = (((unsigned long)sb>>L1_CACHE_SHIFT) ^ id) * (MAXQUOTAS - type);
219 return (tmp + (tmp >> dq_hash_bits)) & dq_hash_mask;
220}
221
222/*
223 * Following list functions expect dq_list_lock to be held
224 */
225static inline void insert_dquot_hash(struct dquot *dquot)
226{
227 struct hlist_head *head = dquot_hash + hashfn(dquot->dq_sb, dquot->dq_id, dquot->dq_type);
228 hlist_add_head(&dquot->dq_hash, head);
229}
230
231static inline void remove_dquot_hash(struct dquot *dquot)
232{
233 hlist_del_init(&dquot->dq_hash);
234}
235
236static inline struct dquot *find_dquot(unsigned int hashent, struct super_block *sb, unsigned int id, int type)
237{
238 struct hlist_node *node;
239 struct dquot *dquot;
240
241 hlist_for_each (node, dquot_hash+hashent) {
242 dquot = hlist_entry(node, struct dquot, dq_hash);
243 if (dquot->dq_sb == sb && dquot->dq_id == id && dquot->dq_type == type)
244 return dquot;
245 }
246 return NODQUOT;
247}
248
249/* Add a dquot to the tail of the free list */
250static inline void put_dquot_last(struct dquot *dquot)
251{
252 list_add(&dquot->dq_free, free_dquots.prev);
253 dqstats.free_dquots++;
254}
255
256static inline void remove_free_dquot(struct dquot *dquot)
257{
258 if (list_empty(&dquot->dq_free))
259 return;
260 list_del_init(&dquot->dq_free);
261 dqstats.free_dquots--;
262}
263
264static inline void put_inuse(struct dquot *dquot)
265{
266 /* We add to the back of inuse list so we don't have to restart
267 * when traversing this list and we block */
268 list_add(&dquot->dq_inuse, inuse_list.prev);
269 dqstats.allocated_dquots++;
270}
271
272static inline void remove_inuse(struct dquot *dquot)
273{
274 dqstats.allocated_dquots--;
275 list_del(&dquot->dq_inuse);
276}
277/*
278 * End of list functions needing dq_list_lock
279 */
280
281static void wait_on_dquot(struct dquot *dquot)
282{
283 down(&dquot->dq_lock);
284 up(&dquot->dq_lock);
285}
286
287#define mark_dquot_dirty(dquot) ((dquot)->dq_sb->dq_op->mark_dirty(dquot))
288
289int dquot_mark_dquot_dirty(struct dquot *dquot)
290{
291 spin_lock(&dq_list_lock);
292 if (!test_and_set_bit(DQ_MOD_B, &dquot->dq_flags))
293 list_add(&dquot->dq_dirty, &sb_dqopt(dquot->dq_sb)->
294 info[dquot->dq_type].dqi_dirty_list);
295 spin_unlock(&dq_list_lock);
296 return 0;
297}
298
299/* This function needs dq_list_lock */
300static inline int clear_dquot_dirty(struct dquot *dquot)
301{
302 if (!test_and_clear_bit(DQ_MOD_B, &dquot->dq_flags))
303 return 0;
304 list_del_init(&dquot->dq_dirty);
305 return 1;
306}
307
308void mark_info_dirty(struct super_block *sb, int type)
309{
310 set_bit(DQF_INFO_DIRTY_B, &sb_dqopt(sb)->info[type].dqi_flags);
311}
312EXPORT_SYMBOL(mark_info_dirty);
313
314/*
315 * Read dquot from disk and alloc space for it
316 */
317
318int dquot_acquire(struct dquot *dquot)
319{
320 int ret = 0, ret2 = 0;
321 struct quota_info *dqopt = sb_dqopt(dquot->dq_sb);
322
323 down(&dquot->dq_lock);
324 down(&dqopt->dqio_sem);
325 if (!test_bit(DQ_READ_B, &dquot->dq_flags))
326 ret = dqopt->ops[dquot->dq_type]->read_dqblk(dquot);
327 if (ret < 0)
328 goto out_iolock;
329 set_bit(DQ_READ_B, &dquot->dq_flags);
330 /* Instantiate dquot if needed */
331 if (!test_bit(DQ_ACTIVE_B, &dquot->dq_flags) && !dquot->dq_off) {
332 ret = dqopt->ops[dquot->dq_type]->commit_dqblk(dquot);
333 /* Write the info if needed */
334 if (info_dirty(&dqopt->info[dquot->dq_type]))
335 ret2 = dqopt->ops[dquot->dq_type]->write_file_info(dquot->dq_sb, dquot->dq_type);
336 if (ret < 0)
337 goto out_iolock;
338 if (ret2 < 0) {
339 ret = ret2;
340 goto out_iolock;
341 }
342 }
343 set_bit(DQ_ACTIVE_B, &dquot->dq_flags);
344out_iolock:
345 up(&dqopt->dqio_sem);
346 up(&dquot->dq_lock);
347 return ret;
348}
349
350/*
351 * Write dquot to disk
352 */
353int dquot_commit(struct dquot *dquot)
354{
355 int ret = 0, ret2 = 0;
356 struct quota_info *dqopt = sb_dqopt(dquot->dq_sb);
357
358 down(&dqopt->dqio_sem);
359 spin_lock(&dq_list_lock);
360 if (!clear_dquot_dirty(dquot)) {
361 spin_unlock(&dq_list_lock);
362 goto out_sem;
363 }
364 spin_unlock(&dq_list_lock);
365 /* Inactive dquot can be only if there was error during read/init
366 * => we have better not writing it */
367 if (test_bit(DQ_ACTIVE_B, &dquot->dq_flags)) {
368 ret = dqopt->ops[dquot->dq_type]->commit_dqblk(dquot);
369 if (info_dirty(&dqopt->info[dquot->dq_type]))
370 ret2 = dqopt->ops[dquot->dq_type]->write_file_info(dquot->dq_sb, dquot->dq_type);
371 if (ret >= 0)
372 ret = ret2;
373 }
374out_sem:
375 up(&dqopt->dqio_sem);
376 return ret;
377}
378
379/*
380 * Release dquot
381 */
382int dquot_release(struct dquot *dquot)
383{
384 int ret = 0, ret2 = 0;
385 struct quota_info *dqopt = sb_dqopt(dquot->dq_sb);
386
387 down(&dquot->dq_lock);
388 /* Check whether we are not racing with some other dqget() */
389 if (atomic_read(&dquot->dq_count) > 1)
390 goto out_dqlock;
391 down(&dqopt->dqio_sem);
392 if (dqopt->ops[dquot->dq_type]->release_dqblk) {
393 ret = dqopt->ops[dquot->dq_type]->release_dqblk(dquot);
394 /* Write the info */
395 if (info_dirty(&dqopt->info[dquot->dq_type]))
396 ret2 = dqopt->ops[dquot->dq_type]->write_file_info(dquot->dq_sb, dquot->dq_type);
397 if (ret >= 0)
398 ret = ret2;
399 }
400 clear_bit(DQ_ACTIVE_B, &dquot->dq_flags);
401 up(&dqopt->dqio_sem);
402out_dqlock:
403 up(&dquot->dq_lock);
404 return ret;
405}
406
407/* Invalidate all dquots on the list. Note that this function is called after
408 * quota is disabled and pointers from inodes removed so there cannot be new
Jan Kara6362e4d2006-03-23 03:00:17 -0800409 * quota users. There can still be some users of quotas due to inodes being
410 * just deleted or pruned by prune_icache() (those are not attached to any
411 * list). We have to wait for such users.
412 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413static void invalidate_dquots(struct super_block *sb, int type)
414{
Domen Puncerc33ed272005-06-25 14:59:36 -0700415 struct dquot *dquot, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416
Jan Kara6362e4d2006-03-23 03:00:17 -0800417restart:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 spin_lock(&dq_list_lock);
Domen Puncerc33ed272005-06-25 14:59:36 -0700419 list_for_each_entry_safe(dquot, tmp, &inuse_list, dq_inuse) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 if (dquot->dq_sb != sb)
421 continue;
422 if (dquot->dq_type != type)
423 continue;
Jan Kara6362e4d2006-03-23 03:00:17 -0800424 /* Wait for dquot users */
425 if (atomic_read(&dquot->dq_count)) {
426 DEFINE_WAIT(wait);
427
428 atomic_inc(&dquot->dq_count);
429 prepare_to_wait(&dquot->dq_wait_unused, &wait,
430 TASK_UNINTERRUPTIBLE);
431 spin_unlock(&dq_list_lock);
432 /* Once dqput() wakes us up, we know it's time to free
433 * the dquot.
434 * IMPORTANT: we rely on the fact that there is always
435 * at most one process waiting for dquot to free.
436 * Otherwise dq_count would be > 1 and we would never
437 * wake up.
438 */
439 if (atomic_read(&dquot->dq_count) > 1)
440 schedule();
441 finish_wait(&dquot->dq_wait_unused, &wait);
442 dqput(dquot);
443 /* At this moment dquot() need not exist (it could be
444 * reclaimed by prune_dqcache(). Hence we must
445 * restart. */
446 goto restart;
447 }
448 /*
449 * Quota now has no users and it has been written on last
450 * dqput()
451 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 remove_dquot_hash(dquot);
453 remove_free_dquot(dquot);
454 remove_inuse(dquot);
455 kmem_cache_free(dquot_cachep, dquot);
456 }
457 spin_unlock(&dq_list_lock);
458}
459
460int vfs_quota_sync(struct super_block *sb, int type)
461{
462 struct list_head *dirty;
463 struct dquot *dquot;
464 struct quota_info *dqopt = sb_dqopt(sb);
465 int cnt;
466
467 down(&dqopt->dqonoff_sem);
468 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
469 if (type != -1 && cnt != type)
470 continue;
471 if (!sb_has_quota_enabled(sb, cnt))
472 continue;
473 spin_lock(&dq_list_lock);
474 dirty = &dqopt->info[cnt].dqi_dirty_list;
475 while (!list_empty(dirty)) {
476 dquot = list_entry(dirty->next, struct dquot, dq_dirty);
477 /* Dirty and inactive can be only bad dquot... */
478 if (!test_bit(DQ_ACTIVE_B, &dquot->dq_flags)) {
479 clear_dquot_dirty(dquot);
480 continue;
481 }
482 /* Now we have active dquot from which someone is
483 * holding reference so we can safely just increase
484 * use count */
485 atomic_inc(&dquot->dq_count);
486 dqstats.lookups++;
487 spin_unlock(&dq_list_lock);
488 sb->dq_op->write_dquot(dquot);
489 dqput(dquot);
490 spin_lock(&dq_list_lock);
491 }
492 spin_unlock(&dq_list_lock);
493 }
494
495 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
496 if ((cnt == type || type == -1) && sb_has_quota_enabled(sb, cnt)
497 && info_dirty(&dqopt->info[cnt]))
498 sb->dq_op->write_info(sb, cnt);
499 spin_lock(&dq_list_lock);
500 dqstats.syncs++;
501 spin_unlock(&dq_list_lock);
502 up(&dqopt->dqonoff_sem);
503
504 return 0;
505}
506
507/* Free unused dquots from cache */
508static void prune_dqcache(int count)
509{
510 struct list_head *head;
511 struct dquot *dquot;
512
513 head = free_dquots.prev;
514 while (head != &free_dquots && count) {
515 dquot = list_entry(head, struct dquot, dq_free);
516 remove_dquot_hash(dquot);
517 remove_free_dquot(dquot);
518 remove_inuse(dquot);
519 kmem_cache_free(dquot_cachep, dquot);
520 count--;
521 head = free_dquots.prev;
522 }
523}
524
525/*
526 * This is called from kswapd when we think we need some
527 * more memory
528 */
529
Al Viro27496a82005-10-21 03:20:48 -0400530static int shrink_dqcache_memory(int nr, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531{
532 if (nr) {
533 spin_lock(&dq_list_lock);
534 prune_dqcache(nr);
535 spin_unlock(&dq_list_lock);
536 }
537 return (dqstats.free_dquots / 100) * sysctl_vfs_cache_pressure;
538}
539
540/*
541 * Put reference to dquot
542 * NOTE: If you change this function please check whether dqput_blocks() works right...
543 * MUST be called with either dqptr_sem or dqonoff_sem held
544 */
545static void dqput(struct dquot *dquot)
546{
547 if (!dquot)
548 return;
549#ifdef __DQUOT_PARANOIA
550 if (!atomic_read(&dquot->dq_count)) {
551 printk("VFS: dqput: trying to free free dquot\n");
552 printk("VFS: device %s, dquot of %s %d\n",
553 dquot->dq_sb->s_id,
554 quotatypes[dquot->dq_type],
555 dquot->dq_id);
556 BUG();
557 }
558#endif
559
560 spin_lock(&dq_list_lock);
561 dqstats.drops++;
562 spin_unlock(&dq_list_lock);
563we_slept:
564 spin_lock(&dq_list_lock);
565 if (atomic_read(&dquot->dq_count) > 1) {
566 /* We have more than one user... nothing to do */
567 atomic_dec(&dquot->dq_count);
Jan Kara6362e4d2006-03-23 03:00:17 -0800568 /* Releasing dquot during quotaoff phase? */
569 if (!sb_has_quota_enabled(dquot->dq_sb, dquot->dq_type) &&
570 atomic_read(&dquot->dq_count) == 1)
571 wake_up(&dquot->dq_wait_unused);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 spin_unlock(&dq_list_lock);
573 return;
574 }
575 /* Need to release dquot? */
576 if (test_bit(DQ_ACTIVE_B, &dquot->dq_flags) && dquot_dirty(dquot)) {
577 spin_unlock(&dq_list_lock);
578 /* Commit dquot before releasing */
579 dquot->dq_sb->dq_op->write_dquot(dquot);
580 goto we_slept;
581 }
582 /* Clear flag in case dquot was inactive (something bad happened) */
583 clear_dquot_dirty(dquot);
584 if (test_bit(DQ_ACTIVE_B, &dquot->dq_flags)) {
585 spin_unlock(&dq_list_lock);
586 dquot->dq_sb->dq_op->release_dquot(dquot);
587 goto we_slept;
588 }
589 atomic_dec(&dquot->dq_count);
590#ifdef __DQUOT_PARANOIA
591 /* sanity check */
592 if (!list_empty(&dquot->dq_free))
593 BUG();
594#endif
595 put_dquot_last(dquot);
596 spin_unlock(&dq_list_lock);
597}
598
599static struct dquot *get_empty_dquot(struct super_block *sb, int type)
600{
601 struct dquot *dquot;
602
603 dquot = kmem_cache_alloc(dquot_cachep, SLAB_NOFS);
604 if(!dquot)
605 return NODQUOT;
606
607 memset((caddr_t)dquot, 0, sizeof(struct dquot));
608 sema_init(&dquot->dq_lock, 1);
609 INIT_LIST_HEAD(&dquot->dq_free);
610 INIT_LIST_HEAD(&dquot->dq_inuse);
611 INIT_HLIST_NODE(&dquot->dq_hash);
612 INIT_LIST_HEAD(&dquot->dq_dirty);
Jan Kara6362e4d2006-03-23 03:00:17 -0800613 init_waitqueue_head(&dquot->dq_wait_unused);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 dquot->dq_sb = sb;
615 dquot->dq_type = type;
616 atomic_set(&dquot->dq_count, 1);
617
618 return dquot;
619}
620
621/*
622 * Get reference to dquot
623 * MUST be called with either dqptr_sem or dqonoff_sem held
624 */
625static struct dquot *dqget(struct super_block *sb, unsigned int id, int type)
626{
627 unsigned int hashent = hashfn(sb, id, type);
628 struct dquot *dquot, *empty = NODQUOT;
629
630 if (!sb_has_quota_enabled(sb, type))
631 return NODQUOT;
632we_slept:
633 spin_lock(&dq_list_lock);
634 if ((dquot = find_dquot(hashent, sb, id, type)) == NODQUOT) {
635 if (empty == NODQUOT) {
636 spin_unlock(&dq_list_lock);
637 if ((empty = get_empty_dquot(sb, type)) == NODQUOT)
638 schedule(); /* Try to wait for a moment... */
639 goto we_slept;
640 }
641 dquot = empty;
642 dquot->dq_id = id;
643 /* all dquots go on the inuse_list */
644 put_inuse(dquot);
645 /* hash it first so it can be found */
646 insert_dquot_hash(dquot);
647 dqstats.lookups++;
648 spin_unlock(&dq_list_lock);
649 } else {
650 if (!atomic_read(&dquot->dq_count))
651 remove_free_dquot(dquot);
652 atomic_inc(&dquot->dq_count);
653 dqstats.cache_hits++;
654 dqstats.lookups++;
655 spin_unlock(&dq_list_lock);
656 if (empty)
657 kmem_cache_free(dquot_cachep, empty);
658 }
659 /* Wait for dq_lock - after this we know that either dquot_release() is already
660 * finished or it will be canceled due to dq_count > 1 test */
661 wait_on_dquot(dquot);
662 /* Read the dquot and instantiate it (everything done only if needed) */
663 if (!test_bit(DQ_ACTIVE_B, &dquot->dq_flags) && sb->dq_op->acquire_dquot(dquot) < 0) {
664 dqput(dquot);
665 return NODQUOT;
666 }
667#ifdef __DQUOT_PARANOIA
668 if (!dquot->dq_sb) /* Has somebody invalidated entry under us? */
669 BUG();
670#endif
671
672 return dquot;
673}
674
675static int dqinit_needed(struct inode *inode, int type)
676{
677 int cnt;
678
679 if (IS_NOQUOTA(inode))
680 return 0;
681 if (type != -1)
682 return inode->i_dquot[type] == NODQUOT;
683 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
684 if (inode->i_dquot[cnt] == NODQUOT)
685 return 1;
686 return 0;
687}
688
689/* This routine is guarded by dqonoff_sem semaphore */
690static void add_dquot_ref(struct super_block *sb, int type)
691{
692 struct list_head *p;
693
694restart:
695 file_list_lock();
696 list_for_each(p, &sb->s_files) {
Eric Dumazet2f512012005-10-30 15:02:16 -0800697 struct file *filp = list_entry(p, struct file, f_u.fu_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 struct inode *inode = filp->f_dentry->d_inode;
699 if (filp->f_mode & FMODE_WRITE && dqinit_needed(inode, type)) {
700 struct dentry *dentry = dget(filp->f_dentry);
701 file_list_unlock();
702 sb->dq_op->initialize(inode, type);
703 dput(dentry);
704 /* As we may have blocked we had better restart... */
705 goto restart;
706 }
707 }
708 file_list_unlock();
709}
710
711/* Return 0 if dqput() won't block (note that 1 doesn't necessarily mean blocking) */
712static inline int dqput_blocks(struct dquot *dquot)
713{
714 if (atomic_read(&dquot->dq_count) <= 1)
715 return 1;
716 return 0;
717}
718
719/* Remove references to dquots from inode - add dquot to list for freeing if needed */
720/* We can't race with anybody because we hold dqptr_sem for writing... */
721int remove_inode_dquot_ref(struct inode *inode, int type, struct list_head *tofree_head)
722{
723 struct dquot *dquot = inode->i_dquot[type];
724
725 inode->i_dquot[type] = NODQUOT;
726 if (dquot != NODQUOT) {
727 if (dqput_blocks(dquot)) {
728#ifdef __DQUOT_PARANOIA
729 if (atomic_read(&dquot->dq_count) != 1)
730 printk(KERN_WARNING "VFS: Adding dquot with dq_count %d to dispose list.\n", atomic_read(&dquot->dq_count));
731#endif
732 spin_lock(&dq_list_lock);
733 list_add(&dquot->dq_free, tofree_head); /* As dquot must have currently users it can't be on the free list... */
734 spin_unlock(&dq_list_lock);
735 return 1;
736 }
737 else
738 dqput(dquot); /* We have guaranteed we won't block */
739 }
740 return 0;
741}
742
743/* Free list of dquots - called from inode.c */
744/* dquots are removed from inodes, no new references can be got so we are the only ones holding reference */
745static void put_dquot_list(struct list_head *tofree_head)
746{
747 struct list_head *act_head;
748 struct dquot *dquot;
749
750 act_head = tofree_head->next;
751 /* So now we have dquots on the list... Just free them */
752 while (act_head != tofree_head) {
753 dquot = list_entry(act_head, struct dquot, dq_free);
754 act_head = act_head->next;
755 list_del_init(&dquot->dq_free); /* Remove dquot from the list so we won't have problems... */
756 dqput(dquot);
757 }
758}
759
760/* Gather all references from inodes and drop them */
761static void drop_dquot_ref(struct super_block *sb, int type)
762{
763 LIST_HEAD(tofree_head);
764
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 down_write(&sb_dqopt(sb)->dqptr_sem);
766 remove_dquot_ref(sb, type, &tofree_head);
767 up_write(&sb_dqopt(sb)->dqptr_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 put_dquot_list(&tofree_head);
769}
770
771static inline void dquot_incr_inodes(struct dquot *dquot, unsigned long number)
772{
773 dquot->dq_dqb.dqb_curinodes += number;
774}
775
776static inline void dquot_incr_space(struct dquot *dquot, qsize_t number)
777{
778 dquot->dq_dqb.dqb_curspace += number;
779}
780
781static inline void dquot_decr_inodes(struct dquot *dquot, unsigned long number)
782{
783 if (dquot->dq_dqb.dqb_curinodes > number)
784 dquot->dq_dqb.dqb_curinodes -= number;
785 else
786 dquot->dq_dqb.dqb_curinodes = 0;
787 if (dquot->dq_dqb.dqb_curinodes <= dquot->dq_dqb.dqb_isoftlimit)
788 dquot->dq_dqb.dqb_itime = (time_t) 0;
789 clear_bit(DQ_INODES_B, &dquot->dq_flags);
790}
791
792static inline void dquot_decr_space(struct dquot *dquot, qsize_t number)
793{
794 if (dquot->dq_dqb.dqb_curspace > number)
795 dquot->dq_dqb.dqb_curspace -= number;
796 else
797 dquot->dq_dqb.dqb_curspace = 0;
798 if (toqb(dquot->dq_dqb.dqb_curspace) <= dquot->dq_dqb.dqb_bsoftlimit)
799 dquot->dq_dqb.dqb_btime = (time_t) 0;
800 clear_bit(DQ_BLKS_B, &dquot->dq_flags);
801}
802
803static int flag_print_warnings = 1;
804
805static inline int need_print_warning(struct dquot *dquot)
806{
807 if (!flag_print_warnings)
808 return 0;
809
810 switch (dquot->dq_type) {
811 case USRQUOTA:
812 return current->fsuid == dquot->dq_id;
813 case GRPQUOTA:
814 return in_group_p(dquot->dq_id);
815 }
816 return 0;
817}
818
819/* Values of warnings */
820#define NOWARN 0
821#define IHARDWARN 1
822#define ISOFTLONGWARN 2
823#define ISOFTWARN 3
824#define BHARDWARN 4
825#define BSOFTLONGWARN 5
826#define BSOFTWARN 6
827
828/* Print warning to user which exceeded quota */
829static void print_warning(struct dquot *dquot, const char warntype)
830{
831 char *msg = NULL;
832 int flag = (warntype == BHARDWARN || warntype == BSOFTLONGWARN) ? DQ_BLKS_B :
833 ((warntype == IHARDWARN || warntype == ISOFTLONGWARN) ? DQ_INODES_B : 0);
834
835 if (!need_print_warning(dquot) || (flag && test_and_set_bit(flag, &dquot->dq_flags)))
836 return;
837
838 tty_write_message(current->signal->tty, dquot->dq_sb->s_id);
839 if (warntype == ISOFTWARN || warntype == BSOFTWARN)
840 tty_write_message(current->signal->tty, ": warning, ");
841 else
842 tty_write_message(current->signal->tty, ": write failed, ");
843 tty_write_message(current->signal->tty, quotatypes[dquot->dq_type]);
844 switch (warntype) {
845 case IHARDWARN:
846 msg = " file limit reached.\r\n";
847 break;
848 case ISOFTLONGWARN:
849 msg = " file quota exceeded too long.\r\n";
850 break;
851 case ISOFTWARN:
852 msg = " file quota exceeded.\r\n";
853 break;
854 case BHARDWARN:
855 msg = " block limit reached.\r\n";
856 break;
857 case BSOFTLONGWARN:
858 msg = " block quota exceeded too long.\r\n";
859 break;
860 case BSOFTWARN:
861 msg = " block quota exceeded.\r\n";
862 break;
863 }
864 tty_write_message(current->signal->tty, msg);
865}
866
867static inline void flush_warnings(struct dquot **dquots, char *warntype)
868{
869 int i;
870
871 for (i = 0; i < MAXQUOTAS; i++)
872 if (dquots[i] != NODQUOT && warntype[i] != NOWARN)
873 print_warning(dquots[i], warntype[i]);
874}
875
876static inline char ignore_hardlimit(struct dquot *dquot)
877{
878 struct mem_dqinfo *info = &sb_dqopt(dquot->dq_sb)->info[dquot->dq_type];
879
880 return capable(CAP_SYS_RESOURCE) &&
881 (info->dqi_format->qf_fmt_id != QFMT_VFS_OLD || !(info->dqi_flags & V1_DQF_RSQUASH));
882}
883
884/* needs dq_data_lock */
885static int check_idq(struct dquot *dquot, ulong inodes, char *warntype)
886{
887 *warntype = NOWARN;
888 if (inodes <= 0 || test_bit(DQ_FAKE_B, &dquot->dq_flags))
889 return QUOTA_OK;
890
891 if (dquot->dq_dqb.dqb_ihardlimit &&
892 (dquot->dq_dqb.dqb_curinodes + inodes) > dquot->dq_dqb.dqb_ihardlimit &&
893 !ignore_hardlimit(dquot)) {
894 *warntype = IHARDWARN;
895 return NO_QUOTA;
896 }
897
898 if (dquot->dq_dqb.dqb_isoftlimit &&
899 (dquot->dq_dqb.dqb_curinodes + inodes) > dquot->dq_dqb.dqb_isoftlimit &&
900 dquot->dq_dqb.dqb_itime && get_seconds() >= dquot->dq_dqb.dqb_itime &&
901 !ignore_hardlimit(dquot)) {
902 *warntype = ISOFTLONGWARN;
903 return NO_QUOTA;
904 }
905
906 if (dquot->dq_dqb.dqb_isoftlimit &&
907 (dquot->dq_dqb.dqb_curinodes + inodes) > dquot->dq_dqb.dqb_isoftlimit &&
908 dquot->dq_dqb.dqb_itime == 0) {
909 *warntype = ISOFTWARN;
910 dquot->dq_dqb.dqb_itime = get_seconds() + sb_dqopt(dquot->dq_sb)->info[dquot->dq_type].dqi_igrace;
911 }
912
913 return QUOTA_OK;
914}
915
916/* needs dq_data_lock */
917static int check_bdq(struct dquot *dquot, qsize_t space, int prealloc, char *warntype)
918{
919 *warntype = 0;
920 if (space <= 0 || test_bit(DQ_FAKE_B, &dquot->dq_flags))
921 return QUOTA_OK;
922
923 if (dquot->dq_dqb.dqb_bhardlimit &&
924 toqb(dquot->dq_dqb.dqb_curspace + space) > dquot->dq_dqb.dqb_bhardlimit &&
925 !ignore_hardlimit(dquot)) {
926 if (!prealloc)
927 *warntype = BHARDWARN;
928 return NO_QUOTA;
929 }
930
931 if (dquot->dq_dqb.dqb_bsoftlimit &&
932 toqb(dquot->dq_dqb.dqb_curspace + space) > dquot->dq_dqb.dqb_bsoftlimit &&
933 dquot->dq_dqb.dqb_btime && get_seconds() >= dquot->dq_dqb.dqb_btime &&
934 !ignore_hardlimit(dquot)) {
935 if (!prealloc)
936 *warntype = BSOFTLONGWARN;
937 return NO_QUOTA;
938 }
939
940 if (dquot->dq_dqb.dqb_bsoftlimit &&
941 toqb(dquot->dq_dqb.dqb_curspace + space) > dquot->dq_dqb.dqb_bsoftlimit &&
942 dquot->dq_dqb.dqb_btime == 0) {
943 if (!prealloc) {
944 *warntype = BSOFTWARN;
945 dquot->dq_dqb.dqb_btime = get_seconds() + sb_dqopt(dquot->dq_sb)->info[dquot->dq_type].dqi_bgrace;
946 }
947 else
948 /*
949 * We don't allow preallocation to exceed softlimit so exceeding will
950 * be always printed
951 */
952 return NO_QUOTA;
953 }
954
955 return QUOTA_OK;
956}
957
958/*
959 * Initialize quota pointers in inode
960 * Transaction must be started at entry
961 */
962int dquot_initialize(struct inode *inode, int type)
963{
964 unsigned int id = 0;
965 int cnt, ret = 0;
966
967 /* First test before acquiring semaphore - solves deadlocks when we
968 * re-enter the quota code and are already holding the semaphore */
969 if (IS_NOQUOTA(inode))
970 return 0;
971 down_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
972 /* Having dqptr_sem we know NOQUOTA flags can't be altered... */
973 if (IS_NOQUOTA(inode))
974 goto out_err;
975 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
976 if (type != -1 && cnt != type)
977 continue;
978 if (inode->i_dquot[cnt] == NODQUOT) {
979 switch (cnt) {
980 case USRQUOTA:
981 id = inode->i_uid;
982 break;
983 case GRPQUOTA:
984 id = inode->i_gid;
985 break;
986 }
987 inode->i_dquot[cnt] = dqget(inode->i_sb, id, cnt);
988 }
989 }
990out_err:
991 up_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
992 return ret;
993}
994
995/*
996 * Release all quotas referenced by inode
997 * Transaction must be started at an entry
998 */
999int dquot_drop(struct inode *inode)
1000{
1001 int cnt;
1002
1003 down_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
1004 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1005 if (inode->i_dquot[cnt] != NODQUOT) {
1006 dqput(inode->i_dquot[cnt]);
1007 inode->i_dquot[cnt] = NODQUOT;
1008 }
1009 }
1010 up_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
1011 return 0;
1012}
1013
1014/*
1015 * Following four functions update i_blocks+i_bytes fields and
1016 * quota information (together with appropriate checks)
1017 * NOTE: We absolutely rely on the fact that caller dirties
1018 * the inode (usually macros in quotaops.h care about this) and
1019 * holds a handle for the current transaction so that dquot write and
1020 * inode write go into the same transaction.
1021 */
1022
1023/*
1024 * This operation can block, but only after everything is updated
1025 */
1026int dquot_alloc_space(struct inode *inode, qsize_t number, int warn)
1027{
1028 int cnt, ret = NO_QUOTA;
1029 char warntype[MAXQUOTAS];
1030
1031 /* First test before acquiring semaphore - solves deadlocks when we
1032 * re-enter the quota code and are already holding the semaphore */
1033 if (IS_NOQUOTA(inode)) {
1034out_add:
1035 inode_add_bytes(inode, number);
1036 return QUOTA_OK;
1037 }
1038 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1039 warntype[cnt] = NOWARN;
1040
1041 down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1042 if (IS_NOQUOTA(inode)) { /* Now we can do reliable test... */
1043 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1044 goto out_add;
1045 }
1046 spin_lock(&dq_data_lock);
1047 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1048 if (inode->i_dquot[cnt] == NODQUOT)
1049 continue;
1050 if (check_bdq(inode->i_dquot[cnt], number, warn, warntype+cnt) == NO_QUOTA)
1051 goto warn_put_all;
1052 }
1053 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1054 if (inode->i_dquot[cnt] == NODQUOT)
1055 continue;
1056 dquot_incr_space(inode->i_dquot[cnt], number);
1057 }
1058 inode_add_bytes(inode, number);
1059 ret = QUOTA_OK;
1060warn_put_all:
1061 spin_unlock(&dq_data_lock);
1062 if (ret == QUOTA_OK)
1063 /* Dirtify all the dquots - this can block when journalling */
1064 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1065 if (inode->i_dquot[cnt])
1066 mark_dquot_dirty(inode->i_dquot[cnt]);
1067 flush_warnings(inode->i_dquot, warntype);
1068 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1069 return ret;
1070}
1071
1072/*
1073 * This operation can block, but only after everything is updated
1074 */
1075int dquot_alloc_inode(const struct inode *inode, unsigned long number)
1076{
1077 int cnt, ret = NO_QUOTA;
1078 char warntype[MAXQUOTAS];
1079
1080 /* First test before acquiring semaphore - solves deadlocks when we
1081 * re-enter the quota code and are already holding the semaphore */
1082 if (IS_NOQUOTA(inode))
1083 return QUOTA_OK;
1084 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1085 warntype[cnt] = NOWARN;
1086 down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1087 if (IS_NOQUOTA(inode)) {
1088 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1089 return QUOTA_OK;
1090 }
1091 spin_lock(&dq_data_lock);
1092 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1093 if (inode->i_dquot[cnt] == NODQUOT)
1094 continue;
1095 if (check_idq(inode->i_dquot[cnt], number, warntype+cnt) == NO_QUOTA)
1096 goto warn_put_all;
1097 }
1098
1099 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1100 if (inode->i_dquot[cnt] == NODQUOT)
1101 continue;
1102 dquot_incr_inodes(inode->i_dquot[cnt], number);
1103 }
1104 ret = QUOTA_OK;
1105warn_put_all:
1106 spin_unlock(&dq_data_lock);
1107 if (ret == QUOTA_OK)
1108 /* Dirtify all the dquots - this can block when journalling */
1109 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1110 if (inode->i_dquot[cnt])
1111 mark_dquot_dirty(inode->i_dquot[cnt]);
1112 flush_warnings((struct dquot **)inode->i_dquot, warntype);
1113 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1114 return ret;
1115}
1116
1117/*
1118 * This operation can block, but only after everything is updated
1119 */
1120int dquot_free_space(struct inode *inode, qsize_t number)
1121{
1122 unsigned int cnt;
1123
1124 /* First test before acquiring semaphore - solves deadlocks when we
1125 * re-enter the quota code and are already holding the semaphore */
1126 if (IS_NOQUOTA(inode)) {
1127out_sub:
1128 inode_sub_bytes(inode, number);
1129 return QUOTA_OK;
1130 }
1131 down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1132 /* Now recheck reliably when holding dqptr_sem */
1133 if (IS_NOQUOTA(inode)) {
1134 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1135 goto out_sub;
1136 }
1137 spin_lock(&dq_data_lock);
1138 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1139 if (inode->i_dquot[cnt] == NODQUOT)
1140 continue;
1141 dquot_decr_space(inode->i_dquot[cnt], number);
1142 }
1143 inode_sub_bytes(inode, number);
1144 spin_unlock(&dq_data_lock);
1145 /* Dirtify all the dquots - this can block when journalling */
1146 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1147 if (inode->i_dquot[cnt])
1148 mark_dquot_dirty(inode->i_dquot[cnt]);
1149 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1150 return QUOTA_OK;
1151}
1152
1153/*
1154 * This operation can block, but only after everything is updated
1155 */
1156int dquot_free_inode(const struct inode *inode, unsigned long number)
1157{
1158 unsigned int cnt;
1159
1160 /* First test before acquiring semaphore - solves deadlocks when we
1161 * re-enter the quota code and are already holding the semaphore */
1162 if (IS_NOQUOTA(inode))
1163 return QUOTA_OK;
1164 down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1165 /* Now recheck reliably when holding dqptr_sem */
1166 if (IS_NOQUOTA(inode)) {
1167 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1168 return QUOTA_OK;
1169 }
1170 spin_lock(&dq_data_lock);
1171 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1172 if (inode->i_dquot[cnt] == NODQUOT)
1173 continue;
1174 dquot_decr_inodes(inode->i_dquot[cnt], number);
1175 }
1176 spin_unlock(&dq_data_lock);
1177 /* Dirtify all the dquots - this can block when journalling */
1178 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1179 if (inode->i_dquot[cnt])
1180 mark_dquot_dirty(inode->i_dquot[cnt]);
1181 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1182 return QUOTA_OK;
1183}
1184
1185/*
1186 * Transfer the number of inode and blocks from one diskquota to an other.
1187 *
1188 * This operation can block, but only after everything is updated
1189 * A transaction must be started when entering this function.
1190 */
1191int dquot_transfer(struct inode *inode, struct iattr *iattr)
1192{
1193 qsize_t space;
1194 struct dquot *transfer_from[MAXQUOTAS];
1195 struct dquot *transfer_to[MAXQUOTAS];
1196 int cnt, ret = NO_QUOTA, chuid = (iattr->ia_valid & ATTR_UID) && inode->i_uid != iattr->ia_uid,
1197 chgid = (iattr->ia_valid & ATTR_GID) && inode->i_gid != iattr->ia_gid;
1198 char warntype[MAXQUOTAS];
1199
1200 /* First test before acquiring semaphore - solves deadlocks when we
1201 * re-enter the quota code and are already holding the semaphore */
1202 if (IS_NOQUOTA(inode))
1203 return QUOTA_OK;
1204 /* Clear the arrays */
1205 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1206 transfer_to[cnt] = transfer_from[cnt] = NODQUOT;
1207 warntype[cnt] = NOWARN;
1208 }
1209 down_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
1210 /* Now recheck reliably when holding dqptr_sem */
1211 if (IS_NOQUOTA(inode)) { /* File without quota accounting? */
1212 up_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
1213 return QUOTA_OK;
1214 }
1215 /* First build the transfer_to list - here we can block on
1216 * reading/instantiating of dquots. We know that the transaction for
1217 * us was already started so we don't violate lock ranking here */
1218 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1219 switch (cnt) {
1220 case USRQUOTA:
1221 if (!chuid)
1222 continue;
1223 transfer_to[cnt] = dqget(inode->i_sb, iattr->ia_uid, cnt);
1224 break;
1225 case GRPQUOTA:
1226 if (!chgid)
1227 continue;
1228 transfer_to[cnt] = dqget(inode->i_sb, iattr->ia_gid, cnt);
1229 break;
1230 }
1231 }
1232 spin_lock(&dq_data_lock);
1233 space = inode_get_bytes(inode);
1234 /* Build the transfer_from list and check the limits */
1235 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1236 if (transfer_to[cnt] == NODQUOT)
1237 continue;
1238 transfer_from[cnt] = inode->i_dquot[cnt];
1239 if (check_idq(transfer_to[cnt], 1, warntype+cnt) == NO_QUOTA ||
1240 check_bdq(transfer_to[cnt], space, 0, warntype+cnt) == NO_QUOTA)
1241 goto warn_put_all;
1242 }
1243
1244 /*
1245 * Finally perform the needed transfer from transfer_from to transfer_to
1246 */
1247 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1248 /*
1249 * Skip changes for same uid or gid or for turned off quota-type.
1250 */
1251 if (transfer_to[cnt] == NODQUOT)
1252 continue;
1253
1254 /* Due to IO error we might not have transfer_from[] structure */
1255 if (transfer_from[cnt]) {
1256 dquot_decr_inodes(transfer_from[cnt], 1);
1257 dquot_decr_space(transfer_from[cnt], space);
1258 }
1259
1260 dquot_incr_inodes(transfer_to[cnt], 1);
1261 dquot_incr_space(transfer_to[cnt], space);
1262
1263 inode->i_dquot[cnt] = transfer_to[cnt];
1264 }
1265 ret = QUOTA_OK;
1266warn_put_all:
1267 spin_unlock(&dq_data_lock);
1268 /* Dirtify all the dquots - this can block when journalling */
1269 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1270 if (transfer_from[cnt])
1271 mark_dquot_dirty(transfer_from[cnt]);
1272 if (transfer_to[cnt])
1273 mark_dquot_dirty(transfer_to[cnt]);
1274 }
1275 flush_warnings(transfer_to, warntype);
1276
1277 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1278 if (ret == QUOTA_OK && transfer_from[cnt] != NODQUOT)
1279 dqput(transfer_from[cnt]);
1280 if (ret == NO_QUOTA && transfer_to[cnt] != NODQUOT)
1281 dqput(transfer_to[cnt]);
1282 }
1283 up_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
1284 return ret;
1285}
1286
1287/*
1288 * Write info of quota file to disk
1289 */
1290int dquot_commit_info(struct super_block *sb, int type)
1291{
1292 int ret;
1293 struct quota_info *dqopt = sb_dqopt(sb);
1294
1295 down(&dqopt->dqio_sem);
1296 ret = dqopt->ops[type]->write_file_info(sb, type);
1297 up(&dqopt->dqio_sem);
1298 return ret;
1299}
1300
1301/*
1302 * Definitions of diskquota operations.
1303 */
1304struct dquot_operations dquot_operations = {
1305 .initialize = dquot_initialize,
1306 .drop = dquot_drop,
1307 .alloc_space = dquot_alloc_space,
1308 .alloc_inode = dquot_alloc_inode,
1309 .free_space = dquot_free_space,
1310 .free_inode = dquot_free_inode,
1311 .transfer = dquot_transfer,
1312 .write_dquot = dquot_commit,
1313 .acquire_dquot = dquot_acquire,
1314 .release_dquot = dquot_release,
1315 .mark_dirty = dquot_mark_dquot_dirty,
1316 .write_info = dquot_commit_info
1317};
1318
1319static inline void set_enable_flags(struct quota_info *dqopt, int type)
1320{
1321 switch (type) {
1322 case USRQUOTA:
1323 dqopt->flags |= DQUOT_USR_ENABLED;
1324 break;
1325 case GRPQUOTA:
1326 dqopt->flags |= DQUOT_GRP_ENABLED;
1327 break;
1328 }
1329}
1330
1331static inline void reset_enable_flags(struct quota_info *dqopt, int type)
1332{
1333 switch (type) {
1334 case USRQUOTA:
1335 dqopt->flags &= ~DQUOT_USR_ENABLED;
1336 break;
1337 case GRPQUOTA:
1338 dqopt->flags &= ~DQUOT_GRP_ENABLED;
1339 break;
1340 }
1341}
1342
1343/*
1344 * Turn quota off on a device. type == -1 ==> quotaoff for all types (umount)
1345 */
1346int vfs_quota_off(struct super_block *sb, int type)
1347{
1348 int cnt;
1349 struct quota_info *dqopt = sb_dqopt(sb);
1350 struct inode *toputinode[MAXQUOTAS];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351
1352 /* We need to serialize quota_off() for device */
1353 down(&dqopt->dqonoff_sem);
1354 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1355 toputinode[cnt] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 if (type != -1 && cnt != type)
1357 continue;
1358 if (!sb_has_quota_enabled(sb, cnt))
1359 continue;
1360 reset_enable_flags(dqopt, cnt);
1361
1362 /* Note: these are blocking operations */
1363 drop_dquot_ref(sb, cnt);
1364 invalidate_dquots(sb, cnt);
1365 /*
1366 * Now all dquots should be invalidated, all writes done so we should be only
1367 * users of the info. No locks needed.
1368 */
1369 if (info_dirty(&dqopt->info[cnt]))
1370 sb->dq_op->write_info(sb, cnt);
1371 if (dqopt->ops[cnt]->free_file_info)
1372 dqopt->ops[cnt]->free_file_info(sb, cnt);
1373 put_quota_format(dqopt->info[cnt].dqi_format);
1374
1375 toputinode[cnt] = dqopt->files[cnt];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 dqopt->files[cnt] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 dqopt->info[cnt].dqi_flags = 0;
1378 dqopt->info[cnt].dqi_igrace = 0;
1379 dqopt->info[cnt].dqi_bgrace = 0;
1380 dqopt->ops[cnt] = NULL;
1381 }
1382 up(&dqopt->dqonoff_sem);
1383 /* Sync the superblock so that buffers with quota data are written to
Al Viro7b7b1ac2005-11-07 17:13:39 -05001384 * disk (and so userspace sees correct data afterwards). */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 if (sb->s_op->sync_fs)
1386 sb->s_op->sync_fs(sb, 1);
1387 sync_blockdev(sb->s_bdev);
1388 /* Now the quota files are just ordinary files and we can set the
1389 * inode flags back. Moreover we discard the pagecache so that
1390 * userspace sees the writes we did bypassing the pagecache. We
1391 * must also discard the blockdev buffers so that we see the
1392 * changes done by userspace on the next quotaon() */
1393 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1394 if (toputinode[cnt]) {
1395 down(&dqopt->dqonoff_sem);
1396 /* If quota was reenabled in the meantime, we have
1397 * nothing to do */
1398 if (!sb_has_quota_enabled(sb, cnt)) {
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001399 mutex_lock(&toputinode[cnt]->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 toputinode[cnt]->i_flags &= ~(S_IMMUTABLE |
1401 S_NOATIME | S_NOQUOTA);
1402 truncate_inode_pages(&toputinode[cnt]->i_data, 0);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001403 mutex_unlock(&toputinode[cnt]->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 mark_inode_dirty(toputinode[cnt]);
1405 iput(toputinode[cnt]);
1406 }
1407 up(&dqopt->dqonoff_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408 }
1409 if (sb->s_bdev)
1410 invalidate_bdev(sb->s_bdev, 0);
1411 return 0;
1412}
1413
1414/*
1415 * Turn quotas on on a device
1416 */
1417
1418/* Helper function when we already have the inode */
1419static int vfs_quota_on_inode(struct inode *inode, int type, int format_id)
1420{
1421 struct quota_format_type *fmt = find_quota_format(format_id);
1422 struct super_block *sb = inode->i_sb;
1423 struct quota_info *dqopt = sb_dqopt(sb);
1424 int error;
1425 int oldflags = -1;
1426
1427 if (!fmt)
1428 return -ESRCH;
1429 if (!S_ISREG(inode->i_mode)) {
1430 error = -EACCES;
1431 goto out_fmt;
1432 }
1433 if (IS_RDONLY(inode)) {
1434 error = -EROFS;
1435 goto out_fmt;
1436 }
1437 if (!sb->s_op->quota_write || !sb->s_op->quota_read) {
1438 error = -EINVAL;
1439 goto out_fmt;
1440 }
1441
1442 /* As we bypass the pagecache we must now flush the inode so that
1443 * we see all the changes from userspace... */
1444 write_inode_now(inode, 1);
1445 /* And now flush the block cache so that kernel sees the changes */
1446 invalidate_bdev(sb->s_bdev, 0);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001447 mutex_lock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448 down(&dqopt->dqonoff_sem);
1449 if (sb_has_quota_enabled(sb, type)) {
1450 error = -EBUSY;
1451 goto out_lock;
1452 }
1453 /* We don't want quota and atime on quota files (deadlocks possible)
1454 * Also nobody should write to the file - we use special IO operations
1455 * which ignore the immutable bit. */
1456 down_write(&dqopt->dqptr_sem);
1457 oldflags = inode->i_flags & (S_NOATIME | S_IMMUTABLE | S_NOQUOTA);
1458 inode->i_flags |= S_NOQUOTA | S_NOATIME | S_IMMUTABLE;
1459 up_write(&dqopt->dqptr_sem);
Jan Kara31e7ad62005-04-16 15:25:46 -07001460 sb->dq_op->drop(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461
1462 error = -EIO;
1463 dqopt->files[type] = igrab(inode);
1464 if (!dqopt->files[type])
1465 goto out_lock;
1466 error = -EINVAL;
1467 if (!fmt->qf_ops->check_quota_file(sb, type))
1468 goto out_file_init;
1469
1470 dqopt->ops[type] = fmt->qf_ops;
1471 dqopt->info[type].dqi_format = fmt;
1472 INIT_LIST_HEAD(&dqopt->info[type].dqi_dirty_list);
1473 down(&dqopt->dqio_sem);
1474 if ((error = dqopt->ops[type]->read_file_info(sb, type)) < 0) {
1475 up(&dqopt->dqio_sem);
1476 goto out_file_init;
1477 }
1478 up(&dqopt->dqio_sem);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001479 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 set_enable_flags(dqopt, type);
1481
1482 add_dquot_ref(sb, type);
1483 up(&dqopt->dqonoff_sem);
1484
1485 return 0;
1486
1487out_file_init:
1488 dqopt->files[type] = NULL;
1489 iput(inode);
1490out_lock:
1491 up(&dqopt->dqonoff_sem);
1492 if (oldflags != -1) {
1493 down_write(&dqopt->dqptr_sem);
1494 /* Set the flags back (in the case of accidental quotaon()
1495 * on a wrong file we don't want to mess up the flags) */
1496 inode->i_flags &= ~(S_NOATIME | S_NOQUOTA | S_IMMUTABLE);
1497 inode->i_flags |= oldflags;
1498 up_write(&dqopt->dqptr_sem);
1499 }
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001500 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501out_fmt:
1502 put_quota_format(fmt);
1503
1504 return error;
1505}
1506
1507/* Actual function called from quotactl() */
1508int vfs_quota_on(struct super_block *sb, int type, int format_id, char *path)
1509{
1510 struct nameidata nd;
1511 int error;
1512
1513 error = path_lookup(path, LOOKUP_FOLLOW, &nd);
1514 if (error < 0)
1515 return error;
1516 error = security_quota_on(nd.dentry);
1517 if (error)
1518 goto out_path;
1519 /* Quota file not on the same filesystem? */
1520 if (nd.mnt->mnt_sb != sb)
1521 error = -EXDEV;
Al Viro7b7b1ac2005-11-07 17:13:39 -05001522 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523 error = vfs_quota_on_inode(nd.dentry->d_inode, type, format_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524out_path:
1525 path_release(&nd);
1526 return error;
1527}
1528
1529/*
1530 * This function is used when filesystem needs to initialize quotas
1531 * during mount time.
1532 */
Christoph Hellwig84de8562005-06-23 00:09:16 -07001533int vfs_quota_on_mount(struct super_block *sb, char *qf_name,
1534 int format_id, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535{
Christoph Hellwig84de8562005-06-23 00:09:16 -07001536 struct dentry *dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537 int error;
1538
Christoph Hellwig2fa389c2005-06-23 00:09:16 -07001539 dentry = lookup_one_len(qf_name, sb->s_root, strlen(qf_name));
Christoph Hellwig84de8562005-06-23 00:09:16 -07001540 if (IS_ERR(dentry))
1541 return PTR_ERR(dentry);
1542
Jan Kara154f4842005-11-28 13:44:14 -08001543 if (!dentry->d_inode) {
1544 error = -ENOENT;
1545 goto out;
1546 }
1547
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548 error = security_quota_on(dentry);
Christoph Hellwig84de8562005-06-23 00:09:16 -07001549 if (!error)
1550 error = vfs_quota_on_inode(dentry->d_inode, type, format_id);
1551
Jan Kara154f4842005-11-28 13:44:14 -08001552out:
Christoph Hellwig84de8562005-06-23 00:09:16 -07001553 dput(dentry);
1554 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555}
1556
1557/* Generic routine for getting common part of quota structure */
1558static void do_get_dqblk(struct dquot *dquot, struct if_dqblk *di)
1559{
1560 struct mem_dqblk *dm = &dquot->dq_dqb;
1561
1562 spin_lock(&dq_data_lock);
1563 di->dqb_bhardlimit = dm->dqb_bhardlimit;
1564 di->dqb_bsoftlimit = dm->dqb_bsoftlimit;
1565 di->dqb_curspace = dm->dqb_curspace;
1566 di->dqb_ihardlimit = dm->dqb_ihardlimit;
1567 di->dqb_isoftlimit = dm->dqb_isoftlimit;
1568 di->dqb_curinodes = dm->dqb_curinodes;
1569 di->dqb_btime = dm->dqb_btime;
1570 di->dqb_itime = dm->dqb_itime;
1571 di->dqb_valid = QIF_ALL;
1572 spin_unlock(&dq_data_lock);
1573}
1574
1575int vfs_get_dqblk(struct super_block *sb, int type, qid_t id, struct if_dqblk *di)
1576{
1577 struct dquot *dquot;
1578
1579 down(&sb_dqopt(sb)->dqonoff_sem);
1580 if (!(dquot = dqget(sb, id, type))) {
1581 up(&sb_dqopt(sb)->dqonoff_sem);
1582 return -ESRCH;
1583 }
1584 do_get_dqblk(dquot, di);
1585 dqput(dquot);
1586 up(&sb_dqopt(sb)->dqonoff_sem);
1587 return 0;
1588}
1589
1590/* Generic routine for setting common part of quota structure */
1591static void do_set_dqblk(struct dquot *dquot, struct if_dqblk *di)
1592{
1593 struct mem_dqblk *dm = &dquot->dq_dqb;
1594 int check_blim = 0, check_ilim = 0;
1595
1596 spin_lock(&dq_data_lock);
1597 if (di->dqb_valid & QIF_SPACE) {
1598 dm->dqb_curspace = di->dqb_curspace;
1599 check_blim = 1;
1600 }
1601 if (di->dqb_valid & QIF_BLIMITS) {
1602 dm->dqb_bsoftlimit = di->dqb_bsoftlimit;
1603 dm->dqb_bhardlimit = di->dqb_bhardlimit;
1604 check_blim = 1;
1605 }
1606 if (di->dqb_valid & QIF_INODES) {
1607 dm->dqb_curinodes = di->dqb_curinodes;
1608 check_ilim = 1;
1609 }
1610 if (di->dqb_valid & QIF_ILIMITS) {
1611 dm->dqb_isoftlimit = di->dqb_isoftlimit;
1612 dm->dqb_ihardlimit = di->dqb_ihardlimit;
1613 check_ilim = 1;
1614 }
1615 if (di->dqb_valid & QIF_BTIME)
1616 dm->dqb_btime = di->dqb_btime;
1617 if (di->dqb_valid & QIF_ITIME)
1618 dm->dqb_itime = di->dqb_itime;
1619
1620 if (check_blim) {
1621 if (!dm->dqb_bsoftlimit || toqb(dm->dqb_curspace) < dm->dqb_bsoftlimit) {
1622 dm->dqb_btime = 0;
1623 clear_bit(DQ_BLKS_B, &dquot->dq_flags);
1624 }
1625 else if (!(di->dqb_valid & QIF_BTIME)) /* Set grace only if user hasn't provided his own... */
1626 dm->dqb_btime = get_seconds() + sb_dqopt(dquot->dq_sb)->info[dquot->dq_type].dqi_bgrace;
1627 }
1628 if (check_ilim) {
1629 if (!dm->dqb_isoftlimit || dm->dqb_curinodes < dm->dqb_isoftlimit) {
1630 dm->dqb_itime = 0;
1631 clear_bit(DQ_INODES_B, &dquot->dq_flags);
1632 }
1633 else if (!(di->dqb_valid & QIF_ITIME)) /* Set grace only if user hasn't provided his own... */
1634 dm->dqb_itime = get_seconds() + sb_dqopt(dquot->dq_sb)->info[dquot->dq_type].dqi_igrace;
1635 }
1636 if (dm->dqb_bhardlimit || dm->dqb_bsoftlimit || dm->dqb_ihardlimit || dm->dqb_isoftlimit)
1637 clear_bit(DQ_FAKE_B, &dquot->dq_flags);
1638 else
1639 set_bit(DQ_FAKE_B, &dquot->dq_flags);
1640 spin_unlock(&dq_data_lock);
1641 mark_dquot_dirty(dquot);
1642}
1643
1644int vfs_set_dqblk(struct super_block *sb, int type, qid_t id, struct if_dqblk *di)
1645{
1646 struct dquot *dquot;
1647
1648 down(&sb_dqopt(sb)->dqonoff_sem);
1649 if (!(dquot = dqget(sb, id, type))) {
1650 up(&sb_dqopt(sb)->dqonoff_sem);
1651 return -ESRCH;
1652 }
1653 do_set_dqblk(dquot, di);
1654 dqput(dquot);
1655 up(&sb_dqopt(sb)->dqonoff_sem);
1656 return 0;
1657}
1658
1659/* Generic routine for getting common part of quota file information */
1660int vfs_get_dqinfo(struct super_block *sb, int type, struct if_dqinfo *ii)
1661{
1662 struct mem_dqinfo *mi;
1663
1664 down(&sb_dqopt(sb)->dqonoff_sem);
1665 if (!sb_has_quota_enabled(sb, type)) {
1666 up(&sb_dqopt(sb)->dqonoff_sem);
1667 return -ESRCH;
1668 }
1669 mi = sb_dqopt(sb)->info + type;
1670 spin_lock(&dq_data_lock);
1671 ii->dqi_bgrace = mi->dqi_bgrace;
1672 ii->dqi_igrace = mi->dqi_igrace;
1673 ii->dqi_flags = mi->dqi_flags & DQF_MASK;
1674 ii->dqi_valid = IIF_ALL;
1675 spin_unlock(&dq_data_lock);
1676 up(&sb_dqopt(sb)->dqonoff_sem);
1677 return 0;
1678}
1679
1680/* Generic routine for setting common part of quota file information */
1681int vfs_set_dqinfo(struct super_block *sb, int type, struct if_dqinfo *ii)
1682{
1683 struct mem_dqinfo *mi;
1684
1685 down(&sb_dqopt(sb)->dqonoff_sem);
1686 if (!sb_has_quota_enabled(sb, type)) {
1687 up(&sb_dqopt(sb)->dqonoff_sem);
1688 return -ESRCH;
1689 }
1690 mi = sb_dqopt(sb)->info + type;
1691 spin_lock(&dq_data_lock);
1692 if (ii->dqi_valid & IIF_BGRACE)
1693 mi->dqi_bgrace = ii->dqi_bgrace;
1694 if (ii->dqi_valid & IIF_IGRACE)
1695 mi->dqi_igrace = ii->dqi_igrace;
1696 if (ii->dqi_valid & IIF_FLAGS)
1697 mi->dqi_flags = (mi->dqi_flags & ~DQF_MASK) | (ii->dqi_flags & DQF_MASK);
1698 spin_unlock(&dq_data_lock);
1699 mark_info_dirty(sb, type);
1700 /* Force write to disk */
1701 sb->dq_op->write_info(sb, type);
1702 up(&sb_dqopt(sb)->dqonoff_sem);
1703 return 0;
1704}
1705
1706struct quotactl_ops vfs_quotactl_ops = {
1707 .quota_on = vfs_quota_on,
1708 .quota_off = vfs_quota_off,
1709 .quota_sync = vfs_quota_sync,
1710 .get_info = vfs_get_dqinfo,
1711 .set_info = vfs_set_dqinfo,
1712 .get_dqblk = vfs_get_dqblk,
1713 .set_dqblk = vfs_set_dqblk
1714};
1715
1716static ctl_table fs_dqstats_table[] = {
1717 {
1718 .ctl_name = FS_DQ_LOOKUPS,
1719 .procname = "lookups",
1720 .data = &dqstats.lookups,
1721 .maxlen = sizeof(int),
1722 .mode = 0444,
1723 .proc_handler = &proc_dointvec,
1724 },
1725 {
1726 .ctl_name = FS_DQ_DROPS,
1727 .procname = "drops",
1728 .data = &dqstats.drops,
1729 .maxlen = sizeof(int),
1730 .mode = 0444,
1731 .proc_handler = &proc_dointvec,
1732 },
1733 {
1734 .ctl_name = FS_DQ_READS,
1735 .procname = "reads",
1736 .data = &dqstats.reads,
1737 .maxlen = sizeof(int),
1738 .mode = 0444,
1739 .proc_handler = &proc_dointvec,
1740 },
1741 {
1742 .ctl_name = FS_DQ_WRITES,
1743 .procname = "writes",
1744 .data = &dqstats.writes,
1745 .maxlen = sizeof(int),
1746 .mode = 0444,
1747 .proc_handler = &proc_dointvec,
1748 },
1749 {
1750 .ctl_name = FS_DQ_CACHE_HITS,
1751 .procname = "cache_hits",
1752 .data = &dqstats.cache_hits,
1753 .maxlen = sizeof(int),
1754 .mode = 0444,
1755 .proc_handler = &proc_dointvec,
1756 },
1757 {
1758 .ctl_name = FS_DQ_ALLOCATED,
1759 .procname = "allocated_dquots",
1760 .data = &dqstats.allocated_dquots,
1761 .maxlen = sizeof(int),
1762 .mode = 0444,
1763 .proc_handler = &proc_dointvec,
1764 },
1765 {
1766 .ctl_name = FS_DQ_FREE,
1767 .procname = "free_dquots",
1768 .data = &dqstats.free_dquots,
1769 .maxlen = sizeof(int),
1770 .mode = 0444,
1771 .proc_handler = &proc_dointvec,
1772 },
1773 {
1774 .ctl_name = FS_DQ_SYNCS,
1775 .procname = "syncs",
1776 .data = &dqstats.syncs,
1777 .maxlen = sizeof(int),
1778 .mode = 0444,
1779 .proc_handler = &proc_dointvec,
1780 },
1781 {
1782 .ctl_name = FS_DQ_WARNINGS,
1783 .procname = "warnings",
1784 .data = &flag_print_warnings,
1785 .maxlen = sizeof(int),
1786 .mode = 0644,
1787 .proc_handler = &proc_dointvec,
1788 },
1789 { .ctl_name = 0 },
1790};
1791
1792static ctl_table fs_table[] = {
1793 {
1794 .ctl_name = FS_DQSTATS,
1795 .procname = "quota",
1796 .mode = 0555,
1797 .child = fs_dqstats_table,
1798 },
1799 { .ctl_name = 0 },
1800};
1801
1802static ctl_table sys_table[] = {
1803 {
1804 .ctl_name = CTL_FS,
1805 .procname = "fs",
1806 .mode = 0555,
1807 .child = fs_table,
1808 },
1809 { .ctl_name = 0 },
1810};
1811
1812static int __init dquot_init(void)
1813{
1814 int i;
1815 unsigned long nr_hash, order;
1816
1817 printk(KERN_NOTICE "VFS: Disk quotas %s\n", __DQUOT_VERSION__);
1818
1819 register_sysctl_table(sys_table, 0);
1820
1821 dquot_cachep = kmem_cache_create("dquot",
1822 sizeof(struct dquot), sizeof(unsigned long) * 4,
1823 SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT|SLAB_PANIC,
1824 NULL, NULL);
1825
1826 order = 0;
1827 dquot_hash = (struct hlist_head *)__get_free_pages(GFP_ATOMIC, order);
1828 if (!dquot_hash)
1829 panic("Cannot create dquot hash table");
1830
1831 /* Find power-of-two hlist_heads which can fit into allocation */
1832 nr_hash = (1UL << order) * PAGE_SIZE / sizeof(struct hlist_head);
1833 dq_hash_bits = 0;
1834 do {
1835 dq_hash_bits++;
1836 } while (nr_hash >> dq_hash_bits);
1837 dq_hash_bits--;
1838
1839 nr_hash = 1UL << dq_hash_bits;
1840 dq_hash_mask = nr_hash - 1;
1841 for (i = 0; i < nr_hash; i++)
1842 INIT_HLIST_HEAD(dquot_hash + i);
1843
1844 printk("Dquot-cache hash table entries: %ld (order %ld, %ld bytes)\n",
1845 nr_hash, order, (PAGE_SIZE << order));
1846
1847 set_shrinker(DEFAULT_SEEKS, shrink_dqcache_memory);
1848
1849 return 0;
1850}
1851module_init(dquot_init);
1852
1853EXPORT_SYMBOL(register_quota_format);
1854EXPORT_SYMBOL(unregister_quota_format);
1855EXPORT_SYMBOL(dqstats);
1856EXPORT_SYMBOL(dq_data_lock);
1857EXPORT_SYMBOL(vfs_quota_on);
1858EXPORT_SYMBOL(vfs_quota_on_mount);
1859EXPORT_SYMBOL(vfs_quota_off);
1860EXPORT_SYMBOL(vfs_quota_sync);
1861EXPORT_SYMBOL(vfs_get_dqinfo);
1862EXPORT_SYMBOL(vfs_set_dqinfo);
1863EXPORT_SYMBOL(vfs_get_dqblk);
1864EXPORT_SYMBOL(vfs_set_dqblk);
1865EXPORT_SYMBOL(dquot_commit);
1866EXPORT_SYMBOL(dquot_commit_info);
1867EXPORT_SYMBOL(dquot_acquire);
1868EXPORT_SYMBOL(dquot_release);
1869EXPORT_SYMBOL(dquot_mark_dquot_dirty);
1870EXPORT_SYMBOL(dquot_initialize);
1871EXPORT_SYMBOL(dquot_drop);
1872EXPORT_SYMBOL(dquot_alloc_space);
1873EXPORT_SYMBOL(dquot_alloc_inode);
1874EXPORT_SYMBOL(dquot_free_space);
1875EXPORT_SYMBOL(dquot_free_inode);
1876EXPORT_SYMBOL(dquot_transfer);