blob: 5bdc4b2a872a41818adac58bc7e2ece95ee956ec [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>
Christoph Hellwigfb58b732007-02-12 00:51:57 -080082#include <linux/writeback.h> /* for inode_lock, oddly enough.. */
Linus Torvalds1da177e2005-04-16 15:20:36 -070083
84#include <asm/uaccess.h>
85
86#define __DQUOT_PARANOIA
87
88/*
89 * There are two quota SMP locks. dq_list_lock protects all lists with quotas
90 * and quota formats and also dqstats structure containing statistics about the
91 * lists. dq_data_lock protects data from dq_dqb and also mem_dqinfo structures
92 * and also guards consistency of dquot->dq_dqb with inode->i_blocks, i_bytes.
93 * i_blocks and i_bytes updates itself are guarded by i_lock acquired directly
94 * in inode_add_bytes() and inode_sub_bytes().
95 *
96 * The spinlock ordering is hence: dq_data_lock > dq_list_lock > i_lock
97 *
98 * Note that some things (eg. sb pointer, type, id) doesn't change during
99 * the life of the dquot structure and so needn't to be protected by a lock
100 *
101 * Any operation working on dquots via inode pointers must hold dqptr_sem. If
102 * operation is just reading pointers from inode (or not using them at all) the
103 * read lock is enough. If pointers are altered function must hold write lock
104 * (these locking rules also apply for S_NOQUOTA flag in the inode - note that
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800105 * for altering the flag i_mutex is also needed). If operation is holding
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 * reference to dquot in other way (e.g. quotactl ops) it must be guarded by
Ingo Molnard3be9152006-03-23 03:00:29 -0800107 * dqonoff_mutex.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 * This locking assures that:
109 * a) update/access to dquot pointers in inode is serialized
110 * b) everyone is guarded against invalidate_dquots()
111 *
Ingo Molnard3be9152006-03-23 03:00:29 -0800112 * Each dquot has its dq_lock mutex. Locked dquots might not be referenced
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 * from inodes (dquot_alloc_space() and such don't check the dq_lock).
114 * Currently dquot is locked only when it is being read to memory (or space for
115 * it is being allocated) on the first dqget() and when it is being released on
116 * the last dqput(). The allocation and release oparations are serialized by
117 * the dq_lock and by checking the use count in dquot_release(). Write
118 * operations on dquots don't hold dq_lock as they copy data under dq_data_lock
119 * spinlock to internal buffers before writing.
120 *
121 * Lock ordering (including related VFS locks) is the following:
Ingo Molnard3be9152006-03-23 03:00:29 -0800122 * i_mutex > dqonoff_sem > journal_lock > dqptr_sem > dquot->dq_lock >
123 * dqio_mutex
124 * i_mutex on quota files is special (it's below dqio_mutex)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 */
126
127static DEFINE_SPINLOCK(dq_list_lock);
128DEFINE_SPINLOCK(dq_data_lock);
129
130static char *quotatypes[] = INITQFNAMES;
131static struct quota_format_type *quota_formats; /* List of registered formats */
132static struct quota_module_name module_names[] = INIT_QUOTA_MODULE_NAMES;
133
134/* SLAB cache for dquot structures */
Christoph Lametere18b8902006-12-06 20:33:20 -0800135static struct kmem_cache *dquot_cachep;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136
137int register_quota_format(struct quota_format_type *fmt)
138{
139 spin_lock(&dq_list_lock);
140 fmt->qf_next = quota_formats;
141 quota_formats = fmt;
142 spin_unlock(&dq_list_lock);
143 return 0;
144}
145
146void unregister_quota_format(struct quota_format_type *fmt)
147{
148 struct quota_format_type **actqf;
149
150 spin_lock(&dq_list_lock);
151 for (actqf = &quota_formats; *actqf && *actqf != fmt; actqf = &(*actqf)->qf_next);
152 if (*actqf)
153 *actqf = (*actqf)->qf_next;
154 spin_unlock(&dq_list_lock);
155}
156
157static struct quota_format_type *find_quota_format(int id)
158{
159 struct quota_format_type *actqf;
160
161 spin_lock(&dq_list_lock);
162 for (actqf = quota_formats; actqf && actqf->qf_fmt_id != id; actqf = actqf->qf_next);
163 if (!actqf || !try_module_get(actqf->qf_owner)) {
164 int qm;
165
166 spin_unlock(&dq_list_lock);
167
168 for (qm = 0; module_names[qm].qm_fmt_id && module_names[qm].qm_fmt_id != id; qm++);
169 if (!module_names[qm].qm_fmt_id || request_module(module_names[qm].qm_mod_name))
170 return NULL;
171
172 spin_lock(&dq_list_lock);
173 for (actqf = quota_formats; actqf && actqf->qf_fmt_id != id; actqf = actqf->qf_next);
174 if (actqf && !try_module_get(actqf->qf_owner))
175 actqf = NULL;
176 }
177 spin_unlock(&dq_list_lock);
178 return actqf;
179}
180
181static void put_quota_format(struct quota_format_type *fmt)
182{
183 module_put(fmt->qf_owner);
184}
185
186/*
187 * Dquot List Management:
188 * The quota code uses three lists for dquot management: the inuse_list,
189 * free_dquots, and dquot_hash[] array. A single dquot structure may be
190 * on all three lists, depending on its current state.
191 *
192 * All dquots are placed to the end of inuse_list when first created, and this
193 * list is used for invalidate operation, which must look at every dquot.
194 *
195 * Unused dquots (dq_count == 0) are added to the free_dquots list when freed,
196 * and this list is searched whenever we need an available dquot. Dquots are
197 * removed from the list as soon as they are used again, and
198 * dqstats.free_dquots gives the number of dquots on the list. When
199 * dquot is invalidated it's completely released from memory.
200 *
201 * Dquots with a specific identity (device, type and id) are placed on
202 * one of the dquot_hash[] hash chains. The provides an efficient search
203 * mechanism to locate a specific dquot.
204 */
205
206static LIST_HEAD(inuse_list);
207static LIST_HEAD(free_dquots);
208static unsigned int dq_hash_bits, dq_hash_mask;
209static struct hlist_head *dquot_hash;
210
211struct dqstats dqstats;
212
213static void dqput(struct dquot *dquot);
214
215static inline unsigned int
216hashfn(const struct super_block *sb, unsigned int id, int type)
217{
218 unsigned long tmp;
219
220 tmp = (((unsigned long)sb>>L1_CACHE_SHIFT) ^ id) * (MAXQUOTAS - type);
221 return (tmp + (tmp >> dq_hash_bits)) & dq_hash_mask;
222}
223
224/*
225 * Following list functions expect dq_list_lock to be held
226 */
227static inline void insert_dquot_hash(struct dquot *dquot)
228{
229 struct hlist_head *head = dquot_hash + hashfn(dquot->dq_sb, dquot->dq_id, dquot->dq_type);
230 hlist_add_head(&dquot->dq_hash, head);
231}
232
233static inline void remove_dquot_hash(struct dquot *dquot)
234{
235 hlist_del_init(&dquot->dq_hash);
236}
237
238static inline struct dquot *find_dquot(unsigned int hashent, struct super_block *sb, unsigned int id, int type)
239{
240 struct hlist_node *node;
241 struct dquot *dquot;
242
243 hlist_for_each (node, dquot_hash+hashent) {
244 dquot = hlist_entry(node, struct dquot, dq_hash);
245 if (dquot->dq_sb == sb && dquot->dq_id == id && dquot->dq_type == type)
246 return dquot;
247 }
248 return NODQUOT;
249}
250
251/* Add a dquot to the tail of the free list */
252static inline void put_dquot_last(struct dquot *dquot)
253{
Akinobu Mita8e130592006-06-26 00:24:37 -0700254 list_add_tail(&dquot->dq_free, &free_dquots);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 dqstats.free_dquots++;
256}
257
258static inline void remove_free_dquot(struct dquot *dquot)
259{
260 if (list_empty(&dquot->dq_free))
261 return;
262 list_del_init(&dquot->dq_free);
263 dqstats.free_dquots--;
264}
265
266static inline void put_inuse(struct dquot *dquot)
267{
268 /* We add to the back of inuse list so we don't have to restart
269 * when traversing this list and we block */
Akinobu Mita8e130592006-06-26 00:24:37 -0700270 list_add_tail(&dquot->dq_inuse, &inuse_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 dqstats.allocated_dquots++;
272}
273
274static inline void remove_inuse(struct dquot *dquot)
275{
276 dqstats.allocated_dquots--;
277 list_del(&dquot->dq_inuse);
278}
279/*
280 * End of list functions needing dq_list_lock
281 */
282
283static void wait_on_dquot(struct dquot *dquot)
284{
Ingo Molnard3be9152006-03-23 03:00:29 -0800285 mutex_lock(&dquot->dq_lock);
286 mutex_unlock(&dquot->dq_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287}
288
289#define mark_dquot_dirty(dquot) ((dquot)->dq_sb->dq_op->mark_dirty(dquot))
290
291int dquot_mark_dquot_dirty(struct dquot *dquot)
292{
293 spin_lock(&dq_list_lock);
294 if (!test_and_set_bit(DQ_MOD_B, &dquot->dq_flags))
295 list_add(&dquot->dq_dirty, &sb_dqopt(dquot->dq_sb)->
296 info[dquot->dq_type].dqi_dirty_list);
297 spin_unlock(&dq_list_lock);
298 return 0;
299}
300
301/* This function needs dq_list_lock */
302static inline int clear_dquot_dirty(struct dquot *dquot)
303{
304 if (!test_and_clear_bit(DQ_MOD_B, &dquot->dq_flags))
305 return 0;
306 list_del_init(&dquot->dq_dirty);
307 return 1;
308}
309
310void mark_info_dirty(struct super_block *sb, int type)
311{
312 set_bit(DQF_INFO_DIRTY_B, &sb_dqopt(sb)->info[type].dqi_flags);
313}
314EXPORT_SYMBOL(mark_info_dirty);
315
316/*
317 * Read dquot from disk and alloc space for it
318 */
319
320int dquot_acquire(struct dquot *dquot)
321{
322 int ret = 0, ret2 = 0;
323 struct quota_info *dqopt = sb_dqopt(dquot->dq_sb);
324
Ingo Molnard3be9152006-03-23 03:00:29 -0800325 mutex_lock(&dquot->dq_lock);
326 mutex_lock(&dqopt->dqio_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 if (!test_bit(DQ_READ_B, &dquot->dq_flags))
328 ret = dqopt->ops[dquot->dq_type]->read_dqblk(dquot);
329 if (ret < 0)
330 goto out_iolock;
331 set_bit(DQ_READ_B, &dquot->dq_flags);
332 /* Instantiate dquot if needed */
333 if (!test_bit(DQ_ACTIVE_B, &dquot->dq_flags) && !dquot->dq_off) {
334 ret = dqopt->ops[dquot->dq_type]->commit_dqblk(dquot);
335 /* Write the info if needed */
336 if (info_dirty(&dqopt->info[dquot->dq_type]))
337 ret2 = dqopt->ops[dquot->dq_type]->write_file_info(dquot->dq_sb, dquot->dq_type);
338 if (ret < 0)
339 goto out_iolock;
340 if (ret2 < 0) {
341 ret = ret2;
342 goto out_iolock;
343 }
344 }
345 set_bit(DQ_ACTIVE_B, &dquot->dq_flags);
346out_iolock:
Ingo Molnard3be9152006-03-23 03:00:29 -0800347 mutex_unlock(&dqopt->dqio_mutex);
348 mutex_unlock(&dquot->dq_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 return ret;
350}
351
352/*
353 * Write dquot to disk
354 */
355int dquot_commit(struct dquot *dquot)
356{
357 int ret = 0, ret2 = 0;
358 struct quota_info *dqopt = sb_dqopt(dquot->dq_sb);
359
Ingo Molnard3be9152006-03-23 03:00:29 -0800360 mutex_lock(&dqopt->dqio_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 spin_lock(&dq_list_lock);
362 if (!clear_dquot_dirty(dquot)) {
363 spin_unlock(&dq_list_lock);
364 goto out_sem;
365 }
366 spin_unlock(&dq_list_lock);
367 /* Inactive dquot can be only if there was error during read/init
368 * => we have better not writing it */
369 if (test_bit(DQ_ACTIVE_B, &dquot->dq_flags)) {
370 ret = dqopt->ops[dquot->dq_type]->commit_dqblk(dquot);
371 if (info_dirty(&dqopt->info[dquot->dq_type]))
372 ret2 = dqopt->ops[dquot->dq_type]->write_file_info(dquot->dq_sb, dquot->dq_type);
373 if (ret >= 0)
374 ret = ret2;
375 }
376out_sem:
Ingo Molnard3be9152006-03-23 03:00:29 -0800377 mutex_unlock(&dqopt->dqio_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 return ret;
379}
380
381/*
382 * Release dquot
383 */
384int dquot_release(struct dquot *dquot)
385{
386 int ret = 0, ret2 = 0;
387 struct quota_info *dqopt = sb_dqopt(dquot->dq_sb);
388
Ingo Molnard3be9152006-03-23 03:00:29 -0800389 mutex_lock(&dquot->dq_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 /* Check whether we are not racing with some other dqget() */
391 if (atomic_read(&dquot->dq_count) > 1)
392 goto out_dqlock;
Ingo Molnard3be9152006-03-23 03:00:29 -0800393 mutex_lock(&dqopt->dqio_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 if (dqopt->ops[dquot->dq_type]->release_dqblk) {
395 ret = dqopt->ops[dquot->dq_type]->release_dqblk(dquot);
396 /* Write the info */
397 if (info_dirty(&dqopt->info[dquot->dq_type]))
398 ret2 = dqopt->ops[dquot->dq_type]->write_file_info(dquot->dq_sb, dquot->dq_type);
399 if (ret >= 0)
400 ret = ret2;
401 }
402 clear_bit(DQ_ACTIVE_B, &dquot->dq_flags);
Ingo Molnard3be9152006-03-23 03:00:29 -0800403 mutex_unlock(&dqopt->dqio_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404out_dqlock:
Ingo Molnard3be9152006-03-23 03:00:29 -0800405 mutex_unlock(&dquot->dq_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 return ret;
407}
408
409/* Invalidate all dquots on the list. Note that this function is called after
410 * quota is disabled and pointers from inodes removed so there cannot be new
Jan Kara6362e4d2006-03-23 03:00:17 -0800411 * quota users. There can still be some users of quotas due to inodes being
412 * just deleted or pruned by prune_icache() (those are not attached to any
413 * list). We have to wait for such users.
414 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415static void invalidate_dquots(struct super_block *sb, int type)
416{
Domen Puncerc33ed272005-06-25 14:59:36 -0700417 struct dquot *dquot, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418
Jan Kara6362e4d2006-03-23 03:00:17 -0800419restart:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 spin_lock(&dq_list_lock);
Domen Puncerc33ed272005-06-25 14:59:36 -0700421 list_for_each_entry_safe(dquot, tmp, &inuse_list, dq_inuse) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 if (dquot->dq_sb != sb)
423 continue;
424 if (dquot->dq_type != type)
425 continue;
Jan Kara6362e4d2006-03-23 03:00:17 -0800426 /* Wait for dquot users */
427 if (atomic_read(&dquot->dq_count)) {
428 DEFINE_WAIT(wait);
429
430 atomic_inc(&dquot->dq_count);
431 prepare_to_wait(&dquot->dq_wait_unused, &wait,
432 TASK_UNINTERRUPTIBLE);
433 spin_unlock(&dq_list_lock);
434 /* Once dqput() wakes us up, we know it's time to free
435 * the dquot.
436 * IMPORTANT: we rely on the fact that there is always
437 * at most one process waiting for dquot to free.
438 * Otherwise dq_count would be > 1 and we would never
439 * wake up.
440 */
441 if (atomic_read(&dquot->dq_count) > 1)
442 schedule();
443 finish_wait(&dquot->dq_wait_unused, &wait);
444 dqput(dquot);
445 /* At this moment dquot() need not exist (it could be
446 * reclaimed by prune_dqcache(). Hence we must
447 * restart. */
448 goto restart;
449 }
450 /*
451 * Quota now has no users and it has been written on last
452 * dqput()
453 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 remove_dquot_hash(dquot);
455 remove_free_dquot(dquot);
456 remove_inuse(dquot);
457 kmem_cache_free(dquot_cachep, dquot);
458 }
459 spin_unlock(&dq_list_lock);
460}
461
462int vfs_quota_sync(struct super_block *sb, int type)
463{
464 struct list_head *dirty;
465 struct dquot *dquot;
466 struct quota_info *dqopt = sb_dqopt(sb);
467 int cnt;
468
Ingo Molnard3be9152006-03-23 03:00:29 -0800469 mutex_lock(&dqopt->dqonoff_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
471 if (type != -1 && cnt != type)
472 continue;
473 if (!sb_has_quota_enabled(sb, cnt))
474 continue;
475 spin_lock(&dq_list_lock);
476 dirty = &dqopt->info[cnt].dqi_dirty_list;
477 while (!list_empty(dirty)) {
478 dquot = list_entry(dirty->next, struct dquot, dq_dirty);
479 /* Dirty and inactive can be only bad dquot... */
480 if (!test_bit(DQ_ACTIVE_B, &dquot->dq_flags)) {
481 clear_dquot_dirty(dquot);
482 continue;
483 }
484 /* Now we have active dquot from which someone is
485 * holding reference so we can safely just increase
486 * use count */
487 atomic_inc(&dquot->dq_count);
488 dqstats.lookups++;
489 spin_unlock(&dq_list_lock);
490 sb->dq_op->write_dquot(dquot);
491 dqput(dquot);
492 spin_lock(&dq_list_lock);
493 }
494 spin_unlock(&dq_list_lock);
495 }
496
497 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
498 if ((cnt == type || type == -1) && sb_has_quota_enabled(sb, cnt)
499 && info_dirty(&dqopt->info[cnt]))
500 sb->dq_op->write_info(sb, cnt);
501 spin_lock(&dq_list_lock);
502 dqstats.syncs++;
503 spin_unlock(&dq_list_lock);
Ingo Molnard3be9152006-03-23 03:00:29 -0800504 mutex_unlock(&dqopt->dqonoff_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505
506 return 0;
507}
508
509/* Free unused dquots from cache */
510static void prune_dqcache(int count)
511{
512 struct list_head *head;
513 struct dquot *dquot;
514
515 head = free_dquots.prev;
516 while (head != &free_dquots && count) {
517 dquot = list_entry(head, struct dquot, dq_free);
518 remove_dquot_hash(dquot);
519 remove_free_dquot(dquot);
520 remove_inuse(dquot);
521 kmem_cache_free(dquot_cachep, dquot);
522 count--;
523 head = free_dquots.prev;
524 }
525}
526
527/*
528 * This is called from kswapd when we think we need some
529 * more memory
530 */
531
Al Viro27496a82005-10-21 03:20:48 -0400532static int shrink_dqcache_memory(int nr, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533{
534 if (nr) {
535 spin_lock(&dq_list_lock);
536 prune_dqcache(nr);
537 spin_unlock(&dq_list_lock);
538 }
539 return (dqstats.free_dquots / 100) * sysctl_vfs_cache_pressure;
540}
541
542/*
543 * Put reference to dquot
544 * NOTE: If you change this function please check whether dqput_blocks() works right...
Ingo Molnard3be9152006-03-23 03:00:29 -0800545 * MUST be called with either dqptr_sem or dqonoff_mutex held
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 */
547static void dqput(struct dquot *dquot)
548{
549 if (!dquot)
550 return;
551#ifdef __DQUOT_PARANOIA
552 if (!atomic_read(&dquot->dq_count)) {
553 printk("VFS: dqput: trying to free free dquot\n");
554 printk("VFS: device %s, dquot of %s %d\n",
555 dquot->dq_sb->s_id,
556 quotatypes[dquot->dq_type],
557 dquot->dq_id);
558 BUG();
559 }
560#endif
561
562 spin_lock(&dq_list_lock);
563 dqstats.drops++;
564 spin_unlock(&dq_list_lock);
565we_slept:
566 spin_lock(&dq_list_lock);
567 if (atomic_read(&dquot->dq_count) > 1) {
568 /* We have more than one user... nothing to do */
569 atomic_dec(&dquot->dq_count);
Jan Kara6362e4d2006-03-23 03:00:17 -0800570 /* Releasing dquot during quotaoff phase? */
571 if (!sb_has_quota_enabled(dquot->dq_sb, dquot->dq_type) &&
572 atomic_read(&dquot->dq_count) == 1)
573 wake_up(&dquot->dq_wait_unused);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 spin_unlock(&dq_list_lock);
575 return;
576 }
577 /* Need to release dquot? */
578 if (test_bit(DQ_ACTIVE_B, &dquot->dq_flags) && dquot_dirty(dquot)) {
579 spin_unlock(&dq_list_lock);
580 /* Commit dquot before releasing */
581 dquot->dq_sb->dq_op->write_dquot(dquot);
582 goto we_slept;
583 }
584 /* Clear flag in case dquot was inactive (something bad happened) */
585 clear_dquot_dirty(dquot);
586 if (test_bit(DQ_ACTIVE_B, &dquot->dq_flags)) {
587 spin_unlock(&dq_list_lock);
588 dquot->dq_sb->dq_op->release_dquot(dquot);
589 goto we_slept;
590 }
591 atomic_dec(&dquot->dq_count);
592#ifdef __DQUOT_PARANOIA
593 /* sanity check */
Eric Sesterhenn8abf6a42006-04-02 13:36:13 +0200594 BUG_ON(!list_empty(&dquot->dq_free));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595#endif
596 put_dquot_last(dquot);
597 spin_unlock(&dq_list_lock);
598}
599
600static struct dquot *get_empty_dquot(struct super_block *sb, int type)
601{
602 struct dquot *dquot;
603
Robert P. J. Dayc3762222007-02-10 01:45:03 -0800604 dquot = kmem_cache_zalloc(dquot_cachep, GFP_NOFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 if(!dquot)
606 return NODQUOT;
607
Ingo Molnard3be9152006-03-23 03:00:29 -0800608 mutex_init(&dquot->dq_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 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
Ingo Molnard3be9152006-03-23 03:00:29 -0800623 * MUST be called with either dqptr_sem or dqonoff_mutex held
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 */
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
Eric Sesterhenn8abf6a42006-04-02 13:36:13 +0200668 BUG_ON(!dquot->dq_sb); /* Has somebody invalidated entry under us? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669#endif
670
671 return dquot;
672}
673
674static int dqinit_needed(struct inode *inode, int type)
675{
676 int cnt;
677
678 if (IS_NOQUOTA(inode))
679 return 0;
680 if (type != -1)
681 return inode->i_dquot[type] == NODQUOT;
682 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
683 if (inode->i_dquot[cnt] == NODQUOT)
684 return 1;
685 return 0;
686}
687
Ingo Molnard3be9152006-03-23 03:00:29 -0800688/* This routine is guarded by dqonoff_mutex mutex */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689static void add_dquot_ref(struct super_block *sb, int type)
690{
691 struct list_head *p;
692
693restart:
694 file_list_lock();
695 list_for_each(p, &sb->s_files) {
Eric Dumazet2f512012005-10-30 15:02:16 -0800696 struct file *filp = list_entry(p, struct file, f_u.fu_list);
Josef "Jeff" Sipek0f7fc9e2006-12-08 02:36:35 -0800697 struct inode *inode = filp->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 if (filp->f_mode & FMODE_WRITE && dqinit_needed(inode, type)) {
Josef "Jeff" Sipek0f7fc9e2006-12-08 02:36:35 -0800699 struct dentry *dentry = dget(filp->f_path.dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 file_list_unlock();
701 sb->dq_op->initialize(inode, type);
702 dput(dentry);
703 /* As we may have blocked we had better restart... */
704 goto restart;
705 }
706 }
707 file_list_unlock();
708}
709
710/* Return 0 if dqput() won't block (note that 1 doesn't necessarily mean blocking) */
711static inline int dqput_blocks(struct dquot *dquot)
712{
713 if (atomic_read(&dquot->dq_count) <= 1)
714 return 1;
715 return 0;
716}
717
718/* Remove references to dquots from inode - add dquot to list for freeing if needed */
719/* We can't race with anybody because we hold dqptr_sem for writing... */
720int remove_inode_dquot_ref(struct inode *inode, int type, struct list_head *tofree_head)
721{
722 struct dquot *dquot = inode->i_dquot[type];
723
724 inode->i_dquot[type] = NODQUOT;
725 if (dquot != NODQUOT) {
726 if (dqput_blocks(dquot)) {
727#ifdef __DQUOT_PARANOIA
728 if (atomic_read(&dquot->dq_count) != 1)
729 printk(KERN_WARNING "VFS: Adding dquot with dq_count %d to dispose list.\n", atomic_read(&dquot->dq_count));
730#endif
731 spin_lock(&dq_list_lock);
732 list_add(&dquot->dq_free, tofree_head); /* As dquot must have currently users it can't be on the free list... */
733 spin_unlock(&dq_list_lock);
734 return 1;
735 }
736 else
737 dqput(dquot); /* We have guaranteed we won't block */
738 }
739 return 0;
740}
741
742/* Free list of dquots - called from inode.c */
743/* dquots are removed from inodes, no new references can be got so we are the only ones holding reference */
744static void put_dquot_list(struct list_head *tofree_head)
745{
746 struct list_head *act_head;
747 struct dquot *dquot;
748
749 act_head = tofree_head->next;
750 /* So now we have dquots on the list... Just free them */
751 while (act_head != tofree_head) {
752 dquot = list_entry(act_head, struct dquot, dq_free);
753 act_head = act_head->next;
754 list_del_init(&dquot->dq_free); /* Remove dquot from the list so we won't have problems... */
755 dqput(dquot);
756 }
757}
758
Christoph Hellwigfb58b732007-02-12 00:51:57 -0800759static void remove_dquot_ref(struct super_block *sb, int type,
760 struct list_head *tofree_head)
761{
762 struct inode *inode;
763
764 spin_lock(&inode_lock);
765 list_for_each_entry(inode, &sb->s_inodes, i_sb_list) {
766 if (!IS_NOQUOTA(inode))
767 remove_inode_dquot_ref(inode, type, tofree_head);
768 }
769 spin_unlock(&inode_lock);
770}
771
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772/* Gather all references from inodes and drop them */
773static void drop_dquot_ref(struct super_block *sb, int type)
774{
775 LIST_HEAD(tofree_head);
776
Christoph Hellwigfb58b732007-02-12 00:51:57 -0800777 if (sb->dq_op) {
778 down_write(&sb_dqopt(sb)->dqptr_sem);
779 remove_dquot_ref(sb, type, &tofree_head);
780 up_write(&sb_dqopt(sb)->dqptr_sem);
781 put_dquot_list(&tofree_head);
782 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783}
784
785static inline void dquot_incr_inodes(struct dquot *dquot, unsigned long number)
786{
787 dquot->dq_dqb.dqb_curinodes += number;
788}
789
790static inline void dquot_incr_space(struct dquot *dquot, qsize_t number)
791{
792 dquot->dq_dqb.dqb_curspace += number;
793}
794
795static inline void dquot_decr_inodes(struct dquot *dquot, unsigned long number)
796{
797 if (dquot->dq_dqb.dqb_curinodes > number)
798 dquot->dq_dqb.dqb_curinodes -= number;
799 else
800 dquot->dq_dqb.dqb_curinodes = 0;
801 if (dquot->dq_dqb.dqb_curinodes <= dquot->dq_dqb.dqb_isoftlimit)
802 dquot->dq_dqb.dqb_itime = (time_t) 0;
803 clear_bit(DQ_INODES_B, &dquot->dq_flags);
804}
805
806static inline void dquot_decr_space(struct dquot *dquot, qsize_t number)
807{
808 if (dquot->dq_dqb.dqb_curspace > number)
809 dquot->dq_dqb.dqb_curspace -= number;
810 else
811 dquot->dq_dqb.dqb_curspace = 0;
812 if (toqb(dquot->dq_dqb.dqb_curspace) <= dquot->dq_dqb.dqb_bsoftlimit)
813 dquot->dq_dqb.dqb_btime = (time_t) 0;
814 clear_bit(DQ_BLKS_B, &dquot->dq_flags);
815}
816
817static int flag_print_warnings = 1;
818
819static inline int need_print_warning(struct dquot *dquot)
820{
821 if (!flag_print_warnings)
822 return 0;
823
824 switch (dquot->dq_type) {
825 case USRQUOTA:
826 return current->fsuid == dquot->dq_id;
827 case GRPQUOTA:
828 return in_group_p(dquot->dq_id);
829 }
830 return 0;
831}
832
833/* Values of warnings */
834#define NOWARN 0
835#define IHARDWARN 1
836#define ISOFTLONGWARN 2
837#define ISOFTWARN 3
838#define BHARDWARN 4
839#define BSOFTLONGWARN 5
840#define BSOFTWARN 6
841
842/* Print warning to user which exceeded quota */
843static void print_warning(struct dquot *dquot, const char warntype)
844{
845 char *msg = NULL;
Peter Zijlstra24ec8392006-12-08 02:36:04 -0800846 struct tty_struct *tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 int flag = (warntype == BHARDWARN || warntype == BSOFTLONGWARN) ? DQ_BLKS_B :
848 ((warntype == IHARDWARN || warntype == ISOFTLONGWARN) ? DQ_INODES_B : 0);
849
850 if (!need_print_warning(dquot) || (flag && test_and_set_bit(flag, &dquot->dq_flags)))
851 return;
852
Jan Karab525a7e2006-09-29 02:00:26 -0700853 mutex_lock(&tty_mutex);
Peter Zijlstra24ec8392006-12-08 02:36:04 -0800854 tty = get_current_tty();
855 if (!tty)
Jan Karab525a7e2006-09-29 02:00:26 -0700856 goto out_lock;
Peter Zijlstra24ec8392006-12-08 02:36:04 -0800857 tty_write_message(tty, dquot->dq_sb->s_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 if (warntype == ISOFTWARN || warntype == BSOFTWARN)
Peter Zijlstra24ec8392006-12-08 02:36:04 -0800859 tty_write_message(tty, ": warning, ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 else
Peter Zijlstra24ec8392006-12-08 02:36:04 -0800861 tty_write_message(tty, ": write failed, ");
862 tty_write_message(tty, quotatypes[dquot->dq_type]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 switch (warntype) {
864 case IHARDWARN:
865 msg = " file limit reached.\r\n";
866 break;
867 case ISOFTLONGWARN:
868 msg = " file quota exceeded too long.\r\n";
869 break;
870 case ISOFTWARN:
871 msg = " file quota exceeded.\r\n";
872 break;
873 case BHARDWARN:
874 msg = " block limit reached.\r\n";
875 break;
876 case BSOFTLONGWARN:
877 msg = " block quota exceeded too long.\r\n";
878 break;
879 case BSOFTWARN:
880 msg = " block quota exceeded.\r\n";
881 break;
882 }
Peter Zijlstra24ec8392006-12-08 02:36:04 -0800883 tty_write_message(tty, msg);
Jan Karab525a7e2006-09-29 02:00:26 -0700884out_lock:
885 mutex_unlock(&tty_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886}
887
888static inline void flush_warnings(struct dquot **dquots, char *warntype)
889{
890 int i;
891
892 for (i = 0; i < MAXQUOTAS; i++)
893 if (dquots[i] != NODQUOT && warntype[i] != NOWARN)
894 print_warning(dquots[i], warntype[i]);
895}
896
897static inline char ignore_hardlimit(struct dquot *dquot)
898{
899 struct mem_dqinfo *info = &sb_dqopt(dquot->dq_sb)->info[dquot->dq_type];
900
901 return capable(CAP_SYS_RESOURCE) &&
902 (info->dqi_format->qf_fmt_id != QFMT_VFS_OLD || !(info->dqi_flags & V1_DQF_RSQUASH));
903}
904
905/* needs dq_data_lock */
906static int check_idq(struct dquot *dquot, ulong inodes, char *warntype)
907{
908 *warntype = NOWARN;
909 if (inodes <= 0 || test_bit(DQ_FAKE_B, &dquot->dq_flags))
910 return QUOTA_OK;
911
912 if (dquot->dq_dqb.dqb_ihardlimit &&
913 (dquot->dq_dqb.dqb_curinodes + inodes) > dquot->dq_dqb.dqb_ihardlimit &&
914 !ignore_hardlimit(dquot)) {
915 *warntype = IHARDWARN;
916 return NO_QUOTA;
917 }
918
919 if (dquot->dq_dqb.dqb_isoftlimit &&
920 (dquot->dq_dqb.dqb_curinodes + inodes) > dquot->dq_dqb.dqb_isoftlimit &&
921 dquot->dq_dqb.dqb_itime && get_seconds() >= dquot->dq_dqb.dqb_itime &&
922 !ignore_hardlimit(dquot)) {
923 *warntype = ISOFTLONGWARN;
924 return NO_QUOTA;
925 }
926
927 if (dquot->dq_dqb.dqb_isoftlimit &&
928 (dquot->dq_dqb.dqb_curinodes + inodes) > dquot->dq_dqb.dqb_isoftlimit &&
929 dquot->dq_dqb.dqb_itime == 0) {
930 *warntype = ISOFTWARN;
931 dquot->dq_dqb.dqb_itime = get_seconds() + sb_dqopt(dquot->dq_sb)->info[dquot->dq_type].dqi_igrace;
932 }
933
934 return QUOTA_OK;
935}
936
937/* needs dq_data_lock */
938static int check_bdq(struct dquot *dquot, qsize_t space, int prealloc, char *warntype)
939{
940 *warntype = 0;
941 if (space <= 0 || test_bit(DQ_FAKE_B, &dquot->dq_flags))
942 return QUOTA_OK;
943
944 if (dquot->dq_dqb.dqb_bhardlimit &&
945 toqb(dquot->dq_dqb.dqb_curspace + space) > dquot->dq_dqb.dqb_bhardlimit &&
946 !ignore_hardlimit(dquot)) {
947 if (!prealloc)
948 *warntype = BHARDWARN;
949 return NO_QUOTA;
950 }
951
952 if (dquot->dq_dqb.dqb_bsoftlimit &&
953 toqb(dquot->dq_dqb.dqb_curspace + space) > dquot->dq_dqb.dqb_bsoftlimit &&
954 dquot->dq_dqb.dqb_btime && get_seconds() >= dquot->dq_dqb.dqb_btime &&
955 !ignore_hardlimit(dquot)) {
956 if (!prealloc)
957 *warntype = BSOFTLONGWARN;
958 return NO_QUOTA;
959 }
960
961 if (dquot->dq_dqb.dqb_bsoftlimit &&
962 toqb(dquot->dq_dqb.dqb_curspace + space) > dquot->dq_dqb.dqb_bsoftlimit &&
963 dquot->dq_dqb.dqb_btime == 0) {
964 if (!prealloc) {
965 *warntype = BSOFTWARN;
966 dquot->dq_dqb.dqb_btime = get_seconds() + sb_dqopt(dquot->dq_sb)->info[dquot->dq_type].dqi_bgrace;
967 }
968 else
969 /*
970 * We don't allow preallocation to exceed softlimit so exceeding will
971 * be always printed
972 */
973 return NO_QUOTA;
974 }
975
976 return QUOTA_OK;
977}
978
979/*
980 * Initialize quota pointers in inode
981 * Transaction must be started at entry
982 */
983int dquot_initialize(struct inode *inode, int type)
984{
985 unsigned int id = 0;
986 int cnt, ret = 0;
987
Ingo Molnard3be9152006-03-23 03:00:29 -0800988 /* First test before acquiring mutex - solves deadlocks when we
989 * re-enter the quota code and are already holding the mutex */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 if (IS_NOQUOTA(inode))
991 return 0;
992 down_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
993 /* Having dqptr_sem we know NOQUOTA flags can't be altered... */
994 if (IS_NOQUOTA(inode))
995 goto out_err;
996 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
997 if (type != -1 && cnt != type)
998 continue;
999 if (inode->i_dquot[cnt] == NODQUOT) {
1000 switch (cnt) {
1001 case USRQUOTA:
1002 id = inode->i_uid;
1003 break;
1004 case GRPQUOTA:
1005 id = inode->i_gid;
1006 break;
1007 }
1008 inode->i_dquot[cnt] = dqget(inode->i_sb, id, cnt);
1009 }
1010 }
1011out_err:
1012 up_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
1013 return ret;
1014}
1015
1016/*
1017 * Release all quotas referenced by inode
1018 * Transaction must be started at an entry
1019 */
1020int dquot_drop(struct inode *inode)
1021{
1022 int cnt;
1023
1024 down_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
1025 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1026 if (inode->i_dquot[cnt] != NODQUOT) {
1027 dqput(inode->i_dquot[cnt]);
1028 inode->i_dquot[cnt] = NODQUOT;
1029 }
1030 }
1031 up_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
1032 return 0;
1033}
1034
1035/*
1036 * Following four functions update i_blocks+i_bytes fields and
1037 * quota information (together with appropriate checks)
1038 * NOTE: We absolutely rely on the fact that caller dirties
1039 * the inode (usually macros in quotaops.h care about this) and
1040 * holds a handle for the current transaction so that dquot write and
1041 * inode write go into the same transaction.
1042 */
1043
1044/*
1045 * This operation can block, but only after everything is updated
1046 */
1047int dquot_alloc_space(struct inode *inode, qsize_t number, int warn)
1048{
1049 int cnt, ret = NO_QUOTA;
1050 char warntype[MAXQUOTAS];
1051
Ingo Molnard3be9152006-03-23 03:00:29 -08001052 /* First test before acquiring mutex - solves deadlocks when we
1053 * re-enter the quota code and are already holding the mutex */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 if (IS_NOQUOTA(inode)) {
1055out_add:
1056 inode_add_bytes(inode, number);
1057 return QUOTA_OK;
1058 }
1059 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1060 warntype[cnt] = NOWARN;
1061
1062 down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1063 if (IS_NOQUOTA(inode)) { /* Now we can do reliable test... */
1064 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1065 goto out_add;
1066 }
1067 spin_lock(&dq_data_lock);
1068 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1069 if (inode->i_dquot[cnt] == NODQUOT)
1070 continue;
1071 if (check_bdq(inode->i_dquot[cnt], number, warn, warntype+cnt) == NO_QUOTA)
1072 goto warn_put_all;
1073 }
1074 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1075 if (inode->i_dquot[cnt] == NODQUOT)
1076 continue;
1077 dquot_incr_space(inode->i_dquot[cnt], number);
1078 }
1079 inode_add_bytes(inode, number);
1080 ret = QUOTA_OK;
1081warn_put_all:
1082 spin_unlock(&dq_data_lock);
1083 if (ret == QUOTA_OK)
1084 /* Dirtify all the dquots - this can block when journalling */
1085 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1086 if (inode->i_dquot[cnt])
1087 mark_dquot_dirty(inode->i_dquot[cnt]);
1088 flush_warnings(inode->i_dquot, warntype);
1089 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1090 return ret;
1091}
1092
1093/*
1094 * This operation can block, but only after everything is updated
1095 */
1096int dquot_alloc_inode(const struct inode *inode, unsigned long number)
1097{
1098 int cnt, ret = NO_QUOTA;
1099 char warntype[MAXQUOTAS];
1100
Ingo Molnard3be9152006-03-23 03:00:29 -08001101 /* First test before acquiring mutex - solves deadlocks when we
1102 * re-enter the quota code and are already holding the mutex */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 if (IS_NOQUOTA(inode))
1104 return QUOTA_OK;
1105 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1106 warntype[cnt] = NOWARN;
1107 down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1108 if (IS_NOQUOTA(inode)) {
1109 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1110 return QUOTA_OK;
1111 }
1112 spin_lock(&dq_data_lock);
1113 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1114 if (inode->i_dquot[cnt] == NODQUOT)
1115 continue;
1116 if (check_idq(inode->i_dquot[cnt], number, warntype+cnt) == NO_QUOTA)
1117 goto warn_put_all;
1118 }
1119
1120 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1121 if (inode->i_dquot[cnt] == NODQUOT)
1122 continue;
1123 dquot_incr_inodes(inode->i_dquot[cnt], number);
1124 }
1125 ret = QUOTA_OK;
1126warn_put_all:
1127 spin_unlock(&dq_data_lock);
1128 if (ret == QUOTA_OK)
1129 /* Dirtify all the dquots - this can block when journalling */
1130 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1131 if (inode->i_dquot[cnt])
1132 mark_dquot_dirty(inode->i_dquot[cnt]);
1133 flush_warnings((struct dquot **)inode->i_dquot, warntype);
1134 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1135 return ret;
1136}
1137
1138/*
1139 * This operation can block, but only after everything is updated
1140 */
1141int dquot_free_space(struct inode *inode, qsize_t number)
1142{
1143 unsigned int cnt;
1144
Ingo Molnard3be9152006-03-23 03:00:29 -08001145 /* First test before acquiring mutex - solves deadlocks when we
1146 * re-enter the quota code and are already holding the mutex */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 if (IS_NOQUOTA(inode)) {
1148out_sub:
1149 inode_sub_bytes(inode, number);
1150 return QUOTA_OK;
1151 }
1152 down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1153 /* Now recheck reliably when holding dqptr_sem */
1154 if (IS_NOQUOTA(inode)) {
1155 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1156 goto out_sub;
1157 }
1158 spin_lock(&dq_data_lock);
1159 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1160 if (inode->i_dquot[cnt] == NODQUOT)
1161 continue;
1162 dquot_decr_space(inode->i_dquot[cnt], number);
1163 }
1164 inode_sub_bytes(inode, number);
1165 spin_unlock(&dq_data_lock);
1166 /* Dirtify all the dquots - this can block when journalling */
1167 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1168 if (inode->i_dquot[cnt])
1169 mark_dquot_dirty(inode->i_dquot[cnt]);
1170 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1171 return QUOTA_OK;
1172}
1173
1174/*
1175 * This operation can block, but only after everything is updated
1176 */
1177int dquot_free_inode(const struct inode *inode, unsigned long number)
1178{
1179 unsigned int cnt;
1180
Ingo Molnard3be9152006-03-23 03:00:29 -08001181 /* First test before acquiring mutex - solves deadlocks when we
1182 * re-enter the quota code and are already holding the mutex */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 if (IS_NOQUOTA(inode))
1184 return QUOTA_OK;
1185 down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1186 /* Now recheck reliably when holding dqptr_sem */
1187 if (IS_NOQUOTA(inode)) {
1188 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1189 return QUOTA_OK;
1190 }
1191 spin_lock(&dq_data_lock);
1192 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1193 if (inode->i_dquot[cnt] == NODQUOT)
1194 continue;
1195 dquot_decr_inodes(inode->i_dquot[cnt], number);
1196 }
1197 spin_unlock(&dq_data_lock);
1198 /* Dirtify all the dquots - this can block when journalling */
1199 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1200 if (inode->i_dquot[cnt])
1201 mark_dquot_dirty(inode->i_dquot[cnt]);
1202 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1203 return QUOTA_OK;
1204}
1205
1206/*
1207 * Transfer the number of inode and blocks from one diskquota to an other.
1208 *
1209 * This operation can block, but only after everything is updated
1210 * A transaction must be started when entering this function.
1211 */
1212int dquot_transfer(struct inode *inode, struct iattr *iattr)
1213{
1214 qsize_t space;
1215 struct dquot *transfer_from[MAXQUOTAS];
1216 struct dquot *transfer_to[MAXQUOTAS];
1217 int cnt, ret = NO_QUOTA, chuid = (iattr->ia_valid & ATTR_UID) && inode->i_uid != iattr->ia_uid,
1218 chgid = (iattr->ia_valid & ATTR_GID) && inode->i_gid != iattr->ia_gid;
1219 char warntype[MAXQUOTAS];
1220
Ingo Molnard3be9152006-03-23 03:00:29 -08001221 /* First test before acquiring mutex - solves deadlocks when we
1222 * re-enter the quota code and are already holding the mutex */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 if (IS_NOQUOTA(inode))
1224 return QUOTA_OK;
1225 /* Clear the arrays */
1226 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1227 transfer_to[cnt] = transfer_from[cnt] = NODQUOT;
1228 warntype[cnt] = NOWARN;
1229 }
1230 down_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
1231 /* Now recheck reliably when holding dqptr_sem */
1232 if (IS_NOQUOTA(inode)) { /* File without quota accounting? */
1233 up_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
1234 return QUOTA_OK;
1235 }
1236 /* First build the transfer_to list - here we can block on
1237 * reading/instantiating of dquots. We know that the transaction for
1238 * us was already started so we don't violate lock ranking here */
1239 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1240 switch (cnt) {
1241 case USRQUOTA:
1242 if (!chuid)
1243 continue;
1244 transfer_to[cnt] = dqget(inode->i_sb, iattr->ia_uid, cnt);
1245 break;
1246 case GRPQUOTA:
1247 if (!chgid)
1248 continue;
1249 transfer_to[cnt] = dqget(inode->i_sb, iattr->ia_gid, cnt);
1250 break;
1251 }
1252 }
1253 spin_lock(&dq_data_lock);
1254 space = inode_get_bytes(inode);
1255 /* Build the transfer_from list and check the limits */
1256 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1257 if (transfer_to[cnt] == NODQUOT)
1258 continue;
1259 transfer_from[cnt] = inode->i_dquot[cnt];
1260 if (check_idq(transfer_to[cnt], 1, warntype+cnt) == NO_QUOTA ||
1261 check_bdq(transfer_to[cnt], space, 0, warntype+cnt) == NO_QUOTA)
1262 goto warn_put_all;
1263 }
1264
1265 /*
1266 * Finally perform the needed transfer from transfer_from to transfer_to
1267 */
1268 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1269 /*
1270 * Skip changes for same uid or gid or for turned off quota-type.
1271 */
1272 if (transfer_to[cnt] == NODQUOT)
1273 continue;
1274
1275 /* Due to IO error we might not have transfer_from[] structure */
1276 if (transfer_from[cnt]) {
1277 dquot_decr_inodes(transfer_from[cnt], 1);
1278 dquot_decr_space(transfer_from[cnt], space);
1279 }
1280
1281 dquot_incr_inodes(transfer_to[cnt], 1);
1282 dquot_incr_space(transfer_to[cnt], space);
1283
1284 inode->i_dquot[cnt] = transfer_to[cnt];
1285 }
1286 ret = QUOTA_OK;
1287warn_put_all:
1288 spin_unlock(&dq_data_lock);
1289 /* Dirtify all the dquots - this can block when journalling */
1290 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1291 if (transfer_from[cnt])
1292 mark_dquot_dirty(transfer_from[cnt]);
1293 if (transfer_to[cnt])
1294 mark_dquot_dirty(transfer_to[cnt]);
1295 }
1296 flush_warnings(transfer_to, warntype);
1297
1298 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1299 if (ret == QUOTA_OK && transfer_from[cnt] != NODQUOT)
1300 dqput(transfer_from[cnt]);
1301 if (ret == NO_QUOTA && transfer_to[cnt] != NODQUOT)
1302 dqput(transfer_to[cnt]);
1303 }
1304 up_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
1305 return ret;
1306}
1307
1308/*
1309 * Write info of quota file to disk
1310 */
1311int dquot_commit_info(struct super_block *sb, int type)
1312{
1313 int ret;
1314 struct quota_info *dqopt = sb_dqopt(sb);
1315
Ingo Molnard3be9152006-03-23 03:00:29 -08001316 mutex_lock(&dqopt->dqio_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317 ret = dqopt->ops[type]->write_file_info(sb, type);
Ingo Molnard3be9152006-03-23 03:00:29 -08001318 mutex_unlock(&dqopt->dqio_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 return ret;
1320}
1321
1322/*
1323 * Definitions of diskquota operations.
1324 */
1325struct dquot_operations dquot_operations = {
1326 .initialize = dquot_initialize,
1327 .drop = dquot_drop,
1328 .alloc_space = dquot_alloc_space,
1329 .alloc_inode = dquot_alloc_inode,
1330 .free_space = dquot_free_space,
1331 .free_inode = dquot_free_inode,
1332 .transfer = dquot_transfer,
1333 .write_dquot = dquot_commit,
1334 .acquire_dquot = dquot_acquire,
1335 .release_dquot = dquot_release,
1336 .mark_dirty = dquot_mark_dquot_dirty,
1337 .write_info = dquot_commit_info
1338};
1339
1340static inline void set_enable_flags(struct quota_info *dqopt, int type)
1341{
1342 switch (type) {
1343 case USRQUOTA:
1344 dqopt->flags |= DQUOT_USR_ENABLED;
1345 break;
1346 case GRPQUOTA:
1347 dqopt->flags |= DQUOT_GRP_ENABLED;
1348 break;
1349 }
1350}
1351
1352static inline void reset_enable_flags(struct quota_info *dqopt, int type)
1353{
1354 switch (type) {
1355 case USRQUOTA:
1356 dqopt->flags &= ~DQUOT_USR_ENABLED;
1357 break;
1358 case GRPQUOTA:
1359 dqopt->flags &= ~DQUOT_GRP_ENABLED;
1360 break;
1361 }
1362}
1363
1364/*
1365 * Turn quota off on a device. type == -1 ==> quotaoff for all types (umount)
1366 */
1367int vfs_quota_off(struct super_block *sb, int type)
1368{
1369 int cnt;
1370 struct quota_info *dqopt = sb_dqopt(sb);
1371 struct inode *toputinode[MAXQUOTAS];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372
1373 /* We need to serialize quota_off() for device */
Ingo Molnard3be9152006-03-23 03:00:29 -08001374 mutex_lock(&dqopt->dqonoff_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1376 toputinode[cnt] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 if (type != -1 && cnt != type)
1378 continue;
1379 if (!sb_has_quota_enabled(sb, cnt))
1380 continue;
1381 reset_enable_flags(dqopt, cnt);
1382
1383 /* Note: these are blocking operations */
1384 drop_dquot_ref(sb, cnt);
1385 invalidate_dquots(sb, cnt);
1386 /*
1387 * Now all dquots should be invalidated, all writes done so we should be only
1388 * users of the info. No locks needed.
1389 */
1390 if (info_dirty(&dqopt->info[cnt]))
1391 sb->dq_op->write_info(sb, cnt);
1392 if (dqopt->ops[cnt]->free_file_info)
1393 dqopt->ops[cnt]->free_file_info(sb, cnt);
1394 put_quota_format(dqopt->info[cnt].dqi_format);
1395
1396 toputinode[cnt] = dqopt->files[cnt];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397 dqopt->files[cnt] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 dqopt->info[cnt].dqi_flags = 0;
1399 dqopt->info[cnt].dqi_igrace = 0;
1400 dqopt->info[cnt].dqi_bgrace = 0;
1401 dqopt->ops[cnt] = NULL;
1402 }
Ingo Molnard3be9152006-03-23 03:00:29 -08001403 mutex_unlock(&dqopt->dqonoff_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 /* Sync the superblock so that buffers with quota data are written to
Al Viro7b7b1ac2005-11-07 17:13:39 -05001405 * disk (and so userspace sees correct data afterwards). */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 if (sb->s_op->sync_fs)
1407 sb->s_op->sync_fs(sb, 1);
1408 sync_blockdev(sb->s_bdev);
1409 /* Now the quota files are just ordinary files and we can set the
1410 * inode flags back. Moreover we discard the pagecache so that
1411 * userspace sees the writes we did bypassing the pagecache. We
1412 * must also discard the blockdev buffers so that we see the
1413 * changes done by userspace on the next quotaon() */
1414 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1415 if (toputinode[cnt]) {
Ingo Molnard3be9152006-03-23 03:00:29 -08001416 mutex_lock(&dqopt->dqonoff_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 /* If quota was reenabled in the meantime, we have
1418 * nothing to do */
1419 if (!sb_has_quota_enabled(sb, cnt)) {
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001420 mutex_lock(&toputinode[cnt]->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421 toputinode[cnt]->i_flags &= ~(S_IMMUTABLE |
1422 S_NOATIME | S_NOQUOTA);
1423 truncate_inode_pages(&toputinode[cnt]->i_data, 0);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001424 mutex_unlock(&toputinode[cnt]->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 mark_inode_dirty(toputinode[cnt]);
1426 iput(toputinode[cnt]);
1427 }
Ingo Molnard3be9152006-03-23 03:00:29 -08001428 mutex_unlock(&dqopt->dqonoff_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 }
1430 if (sb->s_bdev)
1431 invalidate_bdev(sb->s_bdev, 0);
1432 return 0;
1433}
1434
1435/*
1436 * Turn quotas on on a device
1437 */
1438
1439/* Helper function when we already have the inode */
1440static int vfs_quota_on_inode(struct inode *inode, int type, int format_id)
1441{
1442 struct quota_format_type *fmt = find_quota_format(format_id);
1443 struct super_block *sb = inode->i_sb;
1444 struct quota_info *dqopt = sb_dqopt(sb);
1445 int error;
1446 int oldflags = -1;
1447
1448 if (!fmt)
1449 return -ESRCH;
1450 if (!S_ISREG(inode->i_mode)) {
1451 error = -EACCES;
1452 goto out_fmt;
1453 }
1454 if (IS_RDONLY(inode)) {
1455 error = -EROFS;
1456 goto out_fmt;
1457 }
1458 if (!sb->s_op->quota_write || !sb->s_op->quota_read) {
1459 error = -EINVAL;
1460 goto out_fmt;
1461 }
1462
1463 /* As we bypass the pagecache we must now flush the inode so that
1464 * we see all the changes from userspace... */
1465 write_inode_now(inode, 1);
1466 /* And now flush the block cache so that kernel sees the changes */
1467 invalidate_bdev(sb->s_bdev, 0);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001468 mutex_lock(&inode->i_mutex);
Ingo Molnard3be9152006-03-23 03:00:29 -08001469 mutex_lock(&dqopt->dqonoff_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470 if (sb_has_quota_enabled(sb, type)) {
1471 error = -EBUSY;
1472 goto out_lock;
1473 }
1474 /* We don't want quota and atime on quota files (deadlocks possible)
1475 * Also nobody should write to the file - we use special IO operations
1476 * which ignore the immutable bit. */
1477 down_write(&dqopt->dqptr_sem);
1478 oldflags = inode->i_flags & (S_NOATIME | S_IMMUTABLE | S_NOQUOTA);
1479 inode->i_flags |= S_NOQUOTA | S_NOATIME | S_IMMUTABLE;
1480 up_write(&dqopt->dqptr_sem);
Jan Kara31e7ad62005-04-16 15:25:46 -07001481 sb->dq_op->drop(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482
1483 error = -EIO;
1484 dqopt->files[type] = igrab(inode);
1485 if (!dqopt->files[type])
1486 goto out_lock;
1487 error = -EINVAL;
1488 if (!fmt->qf_ops->check_quota_file(sb, type))
1489 goto out_file_init;
1490
1491 dqopt->ops[type] = fmt->qf_ops;
1492 dqopt->info[type].dqi_format = fmt;
1493 INIT_LIST_HEAD(&dqopt->info[type].dqi_dirty_list);
Ingo Molnard3be9152006-03-23 03:00:29 -08001494 mutex_lock(&dqopt->dqio_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 if ((error = dqopt->ops[type]->read_file_info(sb, type)) < 0) {
Ingo Molnard3be9152006-03-23 03:00:29 -08001496 mutex_unlock(&dqopt->dqio_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 goto out_file_init;
1498 }
Ingo Molnard3be9152006-03-23 03:00:29 -08001499 mutex_unlock(&dqopt->dqio_mutex);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001500 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501 set_enable_flags(dqopt, type);
1502
1503 add_dquot_ref(sb, type);
Ingo Molnard3be9152006-03-23 03:00:29 -08001504 mutex_unlock(&dqopt->dqonoff_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505
1506 return 0;
1507
1508out_file_init:
1509 dqopt->files[type] = NULL;
1510 iput(inode);
1511out_lock:
Ingo Molnard3be9152006-03-23 03:00:29 -08001512 mutex_unlock(&dqopt->dqonoff_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513 if (oldflags != -1) {
1514 down_write(&dqopt->dqptr_sem);
1515 /* Set the flags back (in the case of accidental quotaon()
1516 * on a wrong file we don't want to mess up the flags) */
1517 inode->i_flags &= ~(S_NOATIME | S_NOQUOTA | S_IMMUTABLE);
1518 inode->i_flags |= oldflags;
1519 up_write(&dqopt->dqptr_sem);
1520 }
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001521 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522out_fmt:
1523 put_quota_format(fmt);
1524
1525 return error;
1526}
1527
1528/* Actual function called from quotactl() */
1529int vfs_quota_on(struct super_block *sb, int type, int format_id, char *path)
1530{
1531 struct nameidata nd;
1532 int error;
1533
1534 error = path_lookup(path, LOOKUP_FOLLOW, &nd);
1535 if (error < 0)
1536 return error;
1537 error = security_quota_on(nd.dentry);
1538 if (error)
1539 goto out_path;
1540 /* Quota file not on the same filesystem? */
1541 if (nd.mnt->mnt_sb != sb)
1542 error = -EXDEV;
Al Viro7b7b1ac2005-11-07 17:13:39 -05001543 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544 error = vfs_quota_on_inode(nd.dentry->d_inode, type, format_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545out_path:
1546 path_release(&nd);
1547 return error;
1548}
1549
1550/*
1551 * This function is used when filesystem needs to initialize quotas
1552 * during mount time.
1553 */
Christoph Hellwig84de8562005-06-23 00:09:16 -07001554int vfs_quota_on_mount(struct super_block *sb, char *qf_name,
1555 int format_id, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556{
Christoph Hellwig84de8562005-06-23 00:09:16 -07001557 struct dentry *dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558 int error;
1559
Christoph Hellwig2fa389c2005-06-23 00:09:16 -07001560 dentry = lookup_one_len(qf_name, sb->s_root, strlen(qf_name));
Christoph Hellwig84de8562005-06-23 00:09:16 -07001561 if (IS_ERR(dentry))
1562 return PTR_ERR(dentry);
1563
Jan Kara154f4842005-11-28 13:44:14 -08001564 if (!dentry->d_inode) {
1565 error = -ENOENT;
1566 goto out;
1567 }
1568
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569 error = security_quota_on(dentry);
Christoph Hellwig84de8562005-06-23 00:09:16 -07001570 if (!error)
1571 error = vfs_quota_on_inode(dentry->d_inode, type, format_id);
1572
Jan Kara154f4842005-11-28 13:44:14 -08001573out:
Christoph Hellwig84de8562005-06-23 00:09:16 -07001574 dput(dentry);
1575 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576}
1577
1578/* Generic routine for getting common part of quota structure */
1579static void do_get_dqblk(struct dquot *dquot, struct if_dqblk *di)
1580{
1581 struct mem_dqblk *dm = &dquot->dq_dqb;
1582
1583 spin_lock(&dq_data_lock);
1584 di->dqb_bhardlimit = dm->dqb_bhardlimit;
1585 di->dqb_bsoftlimit = dm->dqb_bsoftlimit;
1586 di->dqb_curspace = dm->dqb_curspace;
1587 di->dqb_ihardlimit = dm->dqb_ihardlimit;
1588 di->dqb_isoftlimit = dm->dqb_isoftlimit;
1589 di->dqb_curinodes = dm->dqb_curinodes;
1590 di->dqb_btime = dm->dqb_btime;
1591 di->dqb_itime = dm->dqb_itime;
1592 di->dqb_valid = QIF_ALL;
1593 spin_unlock(&dq_data_lock);
1594}
1595
1596int vfs_get_dqblk(struct super_block *sb, int type, qid_t id, struct if_dqblk *di)
1597{
1598 struct dquot *dquot;
1599
Ingo Molnard3be9152006-03-23 03:00:29 -08001600 mutex_lock(&sb_dqopt(sb)->dqonoff_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601 if (!(dquot = dqget(sb, id, type))) {
Ingo Molnard3be9152006-03-23 03:00:29 -08001602 mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603 return -ESRCH;
1604 }
1605 do_get_dqblk(dquot, di);
1606 dqput(dquot);
Ingo Molnard3be9152006-03-23 03:00:29 -08001607 mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608 return 0;
1609}
1610
1611/* Generic routine for setting common part of quota structure */
1612static void do_set_dqblk(struct dquot *dquot, struct if_dqblk *di)
1613{
1614 struct mem_dqblk *dm = &dquot->dq_dqb;
1615 int check_blim = 0, check_ilim = 0;
1616
1617 spin_lock(&dq_data_lock);
1618 if (di->dqb_valid & QIF_SPACE) {
1619 dm->dqb_curspace = di->dqb_curspace;
1620 check_blim = 1;
1621 }
1622 if (di->dqb_valid & QIF_BLIMITS) {
1623 dm->dqb_bsoftlimit = di->dqb_bsoftlimit;
1624 dm->dqb_bhardlimit = di->dqb_bhardlimit;
1625 check_blim = 1;
1626 }
1627 if (di->dqb_valid & QIF_INODES) {
1628 dm->dqb_curinodes = di->dqb_curinodes;
1629 check_ilim = 1;
1630 }
1631 if (di->dqb_valid & QIF_ILIMITS) {
1632 dm->dqb_isoftlimit = di->dqb_isoftlimit;
1633 dm->dqb_ihardlimit = di->dqb_ihardlimit;
1634 check_ilim = 1;
1635 }
1636 if (di->dqb_valid & QIF_BTIME)
1637 dm->dqb_btime = di->dqb_btime;
1638 if (di->dqb_valid & QIF_ITIME)
1639 dm->dqb_itime = di->dqb_itime;
1640
1641 if (check_blim) {
1642 if (!dm->dqb_bsoftlimit || toqb(dm->dqb_curspace) < dm->dqb_bsoftlimit) {
1643 dm->dqb_btime = 0;
1644 clear_bit(DQ_BLKS_B, &dquot->dq_flags);
1645 }
1646 else if (!(di->dqb_valid & QIF_BTIME)) /* Set grace only if user hasn't provided his own... */
1647 dm->dqb_btime = get_seconds() + sb_dqopt(dquot->dq_sb)->info[dquot->dq_type].dqi_bgrace;
1648 }
1649 if (check_ilim) {
1650 if (!dm->dqb_isoftlimit || dm->dqb_curinodes < dm->dqb_isoftlimit) {
1651 dm->dqb_itime = 0;
1652 clear_bit(DQ_INODES_B, &dquot->dq_flags);
1653 }
1654 else if (!(di->dqb_valid & QIF_ITIME)) /* Set grace only if user hasn't provided his own... */
1655 dm->dqb_itime = get_seconds() + sb_dqopt(dquot->dq_sb)->info[dquot->dq_type].dqi_igrace;
1656 }
1657 if (dm->dqb_bhardlimit || dm->dqb_bsoftlimit || dm->dqb_ihardlimit || dm->dqb_isoftlimit)
1658 clear_bit(DQ_FAKE_B, &dquot->dq_flags);
1659 else
1660 set_bit(DQ_FAKE_B, &dquot->dq_flags);
1661 spin_unlock(&dq_data_lock);
1662 mark_dquot_dirty(dquot);
1663}
1664
1665int vfs_set_dqblk(struct super_block *sb, int type, qid_t id, struct if_dqblk *di)
1666{
1667 struct dquot *dquot;
1668
Ingo Molnard3be9152006-03-23 03:00:29 -08001669 mutex_lock(&sb_dqopt(sb)->dqonoff_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670 if (!(dquot = dqget(sb, id, type))) {
Ingo Molnard3be9152006-03-23 03:00:29 -08001671 mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672 return -ESRCH;
1673 }
1674 do_set_dqblk(dquot, di);
1675 dqput(dquot);
Ingo Molnard3be9152006-03-23 03:00:29 -08001676 mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677 return 0;
1678}
1679
1680/* Generic routine for getting common part of quota file information */
1681int vfs_get_dqinfo(struct super_block *sb, int type, struct if_dqinfo *ii)
1682{
1683 struct mem_dqinfo *mi;
1684
Ingo Molnard3be9152006-03-23 03:00:29 -08001685 mutex_lock(&sb_dqopt(sb)->dqonoff_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686 if (!sb_has_quota_enabled(sb, type)) {
Ingo Molnard3be9152006-03-23 03:00:29 -08001687 mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688 return -ESRCH;
1689 }
1690 mi = sb_dqopt(sb)->info + type;
1691 spin_lock(&dq_data_lock);
1692 ii->dqi_bgrace = mi->dqi_bgrace;
1693 ii->dqi_igrace = mi->dqi_igrace;
1694 ii->dqi_flags = mi->dqi_flags & DQF_MASK;
1695 ii->dqi_valid = IIF_ALL;
1696 spin_unlock(&dq_data_lock);
Ingo Molnard3be9152006-03-23 03:00:29 -08001697 mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698 return 0;
1699}
1700
1701/* Generic routine for setting common part of quota file information */
1702int vfs_set_dqinfo(struct super_block *sb, int type, struct if_dqinfo *ii)
1703{
1704 struct mem_dqinfo *mi;
1705
Ingo Molnard3be9152006-03-23 03:00:29 -08001706 mutex_lock(&sb_dqopt(sb)->dqonoff_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707 if (!sb_has_quota_enabled(sb, type)) {
Ingo Molnard3be9152006-03-23 03:00:29 -08001708 mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709 return -ESRCH;
1710 }
1711 mi = sb_dqopt(sb)->info + type;
1712 spin_lock(&dq_data_lock);
1713 if (ii->dqi_valid & IIF_BGRACE)
1714 mi->dqi_bgrace = ii->dqi_bgrace;
1715 if (ii->dqi_valid & IIF_IGRACE)
1716 mi->dqi_igrace = ii->dqi_igrace;
1717 if (ii->dqi_valid & IIF_FLAGS)
1718 mi->dqi_flags = (mi->dqi_flags & ~DQF_MASK) | (ii->dqi_flags & DQF_MASK);
1719 spin_unlock(&dq_data_lock);
1720 mark_info_dirty(sb, type);
1721 /* Force write to disk */
1722 sb->dq_op->write_info(sb, type);
Ingo Molnard3be9152006-03-23 03:00:29 -08001723 mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724 return 0;
1725}
1726
1727struct quotactl_ops vfs_quotactl_ops = {
1728 .quota_on = vfs_quota_on,
1729 .quota_off = vfs_quota_off,
1730 .quota_sync = vfs_quota_sync,
1731 .get_info = vfs_get_dqinfo,
1732 .set_info = vfs_set_dqinfo,
1733 .get_dqblk = vfs_get_dqblk,
1734 .set_dqblk = vfs_set_dqblk
1735};
1736
1737static ctl_table fs_dqstats_table[] = {
1738 {
1739 .ctl_name = FS_DQ_LOOKUPS,
1740 .procname = "lookups",
1741 .data = &dqstats.lookups,
1742 .maxlen = sizeof(int),
1743 .mode = 0444,
1744 .proc_handler = &proc_dointvec,
1745 },
1746 {
1747 .ctl_name = FS_DQ_DROPS,
1748 .procname = "drops",
1749 .data = &dqstats.drops,
1750 .maxlen = sizeof(int),
1751 .mode = 0444,
1752 .proc_handler = &proc_dointvec,
1753 },
1754 {
1755 .ctl_name = FS_DQ_READS,
1756 .procname = "reads",
1757 .data = &dqstats.reads,
1758 .maxlen = sizeof(int),
1759 .mode = 0444,
1760 .proc_handler = &proc_dointvec,
1761 },
1762 {
1763 .ctl_name = FS_DQ_WRITES,
1764 .procname = "writes",
1765 .data = &dqstats.writes,
1766 .maxlen = sizeof(int),
1767 .mode = 0444,
1768 .proc_handler = &proc_dointvec,
1769 },
1770 {
1771 .ctl_name = FS_DQ_CACHE_HITS,
1772 .procname = "cache_hits",
1773 .data = &dqstats.cache_hits,
1774 .maxlen = sizeof(int),
1775 .mode = 0444,
1776 .proc_handler = &proc_dointvec,
1777 },
1778 {
1779 .ctl_name = FS_DQ_ALLOCATED,
1780 .procname = "allocated_dquots",
1781 .data = &dqstats.allocated_dquots,
1782 .maxlen = sizeof(int),
1783 .mode = 0444,
1784 .proc_handler = &proc_dointvec,
1785 },
1786 {
1787 .ctl_name = FS_DQ_FREE,
1788 .procname = "free_dquots",
1789 .data = &dqstats.free_dquots,
1790 .maxlen = sizeof(int),
1791 .mode = 0444,
1792 .proc_handler = &proc_dointvec,
1793 },
1794 {
1795 .ctl_name = FS_DQ_SYNCS,
1796 .procname = "syncs",
1797 .data = &dqstats.syncs,
1798 .maxlen = sizeof(int),
1799 .mode = 0444,
1800 .proc_handler = &proc_dointvec,
1801 },
1802 {
1803 .ctl_name = FS_DQ_WARNINGS,
1804 .procname = "warnings",
1805 .data = &flag_print_warnings,
1806 .maxlen = sizeof(int),
1807 .mode = 0644,
1808 .proc_handler = &proc_dointvec,
1809 },
1810 { .ctl_name = 0 },
1811};
1812
1813static ctl_table fs_table[] = {
1814 {
1815 .ctl_name = FS_DQSTATS,
1816 .procname = "quota",
1817 .mode = 0555,
1818 .child = fs_dqstats_table,
1819 },
1820 { .ctl_name = 0 },
1821};
1822
1823static ctl_table sys_table[] = {
1824 {
1825 .ctl_name = CTL_FS,
1826 .procname = "fs",
1827 .mode = 0555,
1828 .child = fs_table,
1829 },
1830 { .ctl_name = 0 },
1831};
1832
1833static int __init dquot_init(void)
1834{
1835 int i;
1836 unsigned long nr_hash, order;
1837
1838 printk(KERN_NOTICE "VFS: Disk quotas %s\n", __DQUOT_VERSION__);
1839
1840 register_sysctl_table(sys_table, 0);
1841
1842 dquot_cachep = kmem_cache_create("dquot",
1843 sizeof(struct dquot), sizeof(unsigned long) * 4,
Paul Jacksonfffb60f2006-03-24 03:16:06 -08001844 (SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT|
1845 SLAB_MEM_SPREAD|SLAB_PANIC),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846 NULL, NULL);
1847
1848 order = 0;
1849 dquot_hash = (struct hlist_head *)__get_free_pages(GFP_ATOMIC, order);
1850 if (!dquot_hash)
1851 panic("Cannot create dquot hash table");
1852
1853 /* Find power-of-two hlist_heads which can fit into allocation */
1854 nr_hash = (1UL << order) * PAGE_SIZE / sizeof(struct hlist_head);
1855 dq_hash_bits = 0;
1856 do {
1857 dq_hash_bits++;
1858 } while (nr_hash >> dq_hash_bits);
1859 dq_hash_bits--;
1860
1861 nr_hash = 1UL << dq_hash_bits;
1862 dq_hash_mask = nr_hash - 1;
1863 for (i = 0; i < nr_hash; i++)
1864 INIT_HLIST_HEAD(dquot_hash + i);
1865
1866 printk("Dquot-cache hash table entries: %ld (order %ld, %ld bytes)\n",
1867 nr_hash, order, (PAGE_SIZE << order));
1868
1869 set_shrinker(DEFAULT_SEEKS, shrink_dqcache_memory);
1870
1871 return 0;
1872}
1873module_init(dquot_init);
1874
1875EXPORT_SYMBOL(register_quota_format);
1876EXPORT_SYMBOL(unregister_quota_format);
1877EXPORT_SYMBOL(dqstats);
1878EXPORT_SYMBOL(dq_data_lock);
1879EXPORT_SYMBOL(vfs_quota_on);
1880EXPORT_SYMBOL(vfs_quota_on_mount);
1881EXPORT_SYMBOL(vfs_quota_off);
1882EXPORT_SYMBOL(vfs_quota_sync);
1883EXPORT_SYMBOL(vfs_get_dqinfo);
1884EXPORT_SYMBOL(vfs_set_dqinfo);
1885EXPORT_SYMBOL(vfs_get_dqblk);
1886EXPORT_SYMBOL(vfs_set_dqblk);
1887EXPORT_SYMBOL(dquot_commit);
1888EXPORT_SYMBOL(dquot_commit_info);
1889EXPORT_SYMBOL(dquot_acquire);
1890EXPORT_SYMBOL(dquot_release);
1891EXPORT_SYMBOL(dquot_mark_dquot_dirty);
1892EXPORT_SYMBOL(dquot_initialize);
1893EXPORT_SYMBOL(dquot_drop);
1894EXPORT_SYMBOL(dquot_alloc_space);
1895EXPORT_SYMBOL(dquot_alloc_inode);
1896EXPORT_SYMBOL(dquot_free_space);
1897EXPORT_SYMBOL(dquot_free_inode);
1898EXPORT_SYMBOL(dquot_transfer);