blob: ad7e59003e0445943d21f8b9f78090f7b9e19b53 [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070072#include <linux/init.h>
73#include <linux/module.h>
74#include <linux/proc_fs.h>
75#include <linux/security.h>
76#include <linux/kmod.h>
77#include <linux/namei.h>
78#include <linux/buffer_head.h>
Randy Dunlap16f7e0f2006-01-11 12:17:46 -080079#include <linux/capability.h>
Adrian Bunkbe586ba2005-11-07 00:59:35 -080080#include <linux/quotaops.h>
Christoph Hellwigfb58b732007-02-12 00:51:57 -080081#include <linux/writeback.h> /* for inode_lock, oddly enough.. */
Jan Kara8e893462007-10-16 23:29:31 -070082#ifdef CONFIG_QUOTA_NETLINK_INTERFACE
83#include <net/netlink.h>
84#include <net/genetlink.h>
85#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
87#include <asm/uaccess.h>
88
89#define __DQUOT_PARANOIA
90
91/*
92 * There are two quota SMP locks. dq_list_lock protects all lists with quotas
93 * and quota formats and also dqstats structure containing statistics about the
94 * lists. dq_data_lock protects data from dq_dqb and also mem_dqinfo structures
95 * and also guards consistency of dquot->dq_dqb with inode->i_blocks, i_bytes.
96 * i_blocks and i_bytes updates itself are guarded by i_lock acquired directly
97 * in inode_add_bytes() and inode_sub_bytes().
98 *
99 * The spinlock ordering is hence: dq_data_lock > dq_list_lock > i_lock
100 *
101 * Note that some things (eg. sb pointer, type, id) doesn't change during
102 * the life of the dquot structure and so needn't to be protected by a lock
103 *
104 * Any operation working on dquots via inode pointers must hold dqptr_sem. If
105 * operation is just reading pointers from inode (or not using them at all) the
106 * read lock is enough. If pointers are altered function must hold write lock
107 * (these locking rules also apply for S_NOQUOTA flag in the inode - note that
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800108 * for altering the flag i_mutex is also needed). If operation is holding
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 * reference to dquot in other way (e.g. quotactl ops) it must be guarded by
Ingo Molnard3be9152006-03-23 03:00:29 -0800110 * dqonoff_mutex.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 * This locking assures that:
112 * a) update/access to dquot pointers in inode is serialized
113 * b) everyone is guarded against invalidate_dquots()
114 *
Ingo Molnard3be9152006-03-23 03:00:29 -0800115 * Each dquot has its dq_lock mutex. Locked dquots might not be referenced
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 * from inodes (dquot_alloc_space() and such don't check the dq_lock).
117 * Currently dquot is locked only when it is being read to memory (or space for
118 * it is being allocated) on the first dqget() and when it is being released on
119 * the last dqput(). The allocation and release oparations are serialized by
120 * the dq_lock and by checking the use count in dquot_release(). Write
121 * operations on dquots don't hold dq_lock as they copy data under dq_data_lock
122 * spinlock to internal buffers before writing.
123 *
124 * Lock ordering (including related VFS locks) is the following:
Ingo Molnard3be9152006-03-23 03:00:29 -0800125 * i_mutex > dqonoff_sem > journal_lock > dqptr_sem > dquot->dq_lock >
126 * dqio_mutex
127 * i_mutex on quota files is special (it's below dqio_mutex)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 */
129
130static DEFINE_SPINLOCK(dq_list_lock);
131DEFINE_SPINLOCK(dq_data_lock);
132
133static char *quotatypes[] = INITQFNAMES;
134static struct quota_format_type *quota_formats; /* List of registered formats */
135static struct quota_module_name module_names[] = INIT_QUOTA_MODULE_NAMES;
136
137/* SLAB cache for dquot structures */
Christoph Lametere18b8902006-12-06 20:33:20 -0800138static struct kmem_cache *dquot_cachep;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139
140int register_quota_format(struct quota_format_type *fmt)
141{
142 spin_lock(&dq_list_lock);
143 fmt->qf_next = quota_formats;
144 quota_formats = fmt;
145 spin_unlock(&dq_list_lock);
146 return 0;
147}
148
149void unregister_quota_format(struct quota_format_type *fmt)
150{
151 struct quota_format_type **actqf;
152
153 spin_lock(&dq_list_lock);
154 for (actqf = &quota_formats; *actqf && *actqf != fmt; actqf = &(*actqf)->qf_next);
155 if (*actqf)
156 *actqf = (*actqf)->qf_next;
157 spin_unlock(&dq_list_lock);
158}
159
160static struct quota_format_type *find_quota_format(int id)
161{
162 struct quota_format_type *actqf;
163
164 spin_lock(&dq_list_lock);
165 for (actqf = quota_formats; actqf && actqf->qf_fmt_id != id; actqf = actqf->qf_next);
166 if (!actqf || !try_module_get(actqf->qf_owner)) {
167 int qm;
168
169 spin_unlock(&dq_list_lock);
170
171 for (qm = 0; module_names[qm].qm_fmt_id && module_names[qm].qm_fmt_id != id; qm++);
172 if (!module_names[qm].qm_fmt_id || request_module(module_names[qm].qm_mod_name))
173 return NULL;
174
175 spin_lock(&dq_list_lock);
176 for (actqf = quota_formats; actqf && actqf->qf_fmt_id != id; actqf = actqf->qf_next);
177 if (actqf && !try_module_get(actqf->qf_owner))
178 actqf = NULL;
179 }
180 spin_unlock(&dq_list_lock);
181 return actqf;
182}
183
184static void put_quota_format(struct quota_format_type *fmt)
185{
186 module_put(fmt->qf_owner);
187}
188
189/*
190 * Dquot List Management:
191 * The quota code uses three lists for dquot management: the inuse_list,
192 * free_dquots, and dquot_hash[] array. A single dquot structure may be
193 * on all three lists, depending on its current state.
194 *
195 * All dquots are placed to the end of inuse_list when first created, and this
196 * list is used for invalidate operation, which must look at every dquot.
197 *
198 * Unused dquots (dq_count == 0) are added to the free_dquots list when freed,
199 * and this list is searched whenever we need an available dquot. Dquots are
200 * removed from the list as soon as they are used again, and
201 * dqstats.free_dquots gives the number of dquots on the list. When
202 * dquot is invalidated it's completely released from memory.
203 *
204 * Dquots with a specific identity (device, type and id) are placed on
205 * one of the dquot_hash[] hash chains. The provides an efficient search
206 * mechanism to locate a specific dquot.
207 */
208
209static LIST_HEAD(inuse_list);
210static LIST_HEAD(free_dquots);
211static unsigned int dq_hash_bits, dq_hash_mask;
212static struct hlist_head *dquot_hash;
213
214struct dqstats dqstats;
215
216static void dqput(struct dquot *dquot);
217
218static inline unsigned int
219hashfn(const struct super_block *sb, unsigned int id, int type)
220{
221 unsigned long tmp;
222
223 tmp = (((unsigned long)sb>>L1_CACHE_SHIFT) ^ id) * (MAXQUOTAS - type);
224 return (tmp + (tmp >> dq_hash_bits)) & dq_hash_mask;
225}
226
227/*
228 * Following list functions expect dq_list_lock to be held
229 */
230static inline void insert_dquot_hash(struct dquot *dquot)
231{
232 struct hlist_head *head = dquot_hash + hashfn(dquot->dq_sb, dquot->dq_id, dquot->dq_type);
233 hlist_add_head(&dquot->dq_hash, head);
234}
235
236static inline void remove_dquot_hash(struct dquot *dquot)
237{
238 hlist_del_init(&dquot->dq_hash);
239}
240
241static inline struct dquot *find_dquot(unsigned int hashent, struct super_block *sb, unsigned int id, int type)
242{
243 struct hlist_node *node;
244 struct dquot *dquot;
245
246 hlist_for_each (node, dquot_hash+hashent) {
247 dquot = hlist_entry(node, struct dquot, dq_hash);
248 if (dquot->dq_sb == sb && dquot->dq_id == id && dquot->dq_type == type)
249 return dquot;
250 }
251 return NODQUOT;
252}
253
254/* Add a dquot to the tail of the free list */
255static inline void put_dquot_last(struct dquot *dquot)
256{
Akinobu Mita8e130592006-06-26 00:24:37 -0700257 list_add_tail(&dquot->dq_free, &free_dquots);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 dqstats.free_dquots++;
259}
260
261static inline void remove_free_dquot(struct dquot *dquot)
262{
263 if (list_empty(&dquot->dq_free))
264 return;
265 list_del_init(&dquot->dq_free);
266 dqstats.free_dquots--;
267}
268
269static inline void put_inuse(struct dquot *dquot)
270{
271 /* We add to the back of inuse list so we don't have to restart
272 * when traversing this list and we block */
Akinobu Mita8e130592006-06-26 00:24:37 -0700273 list_add_tail(&dquot->dq_inuse, &inuse_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 dqstats.allocated_dquots++;
275}
276
277static inline void remove_inuse(struct dquot *dquot)
278{
279 dqstats.allocated_dquots--;
280 list_del(&dquot->dq_inuse);
281}
282/*
283 * End of list functions needing dq_list_lock
284 */
285
286static void wait_on_dquot(struct dquot *dquot)
287{
Ingo Molnard3be9152006-03-23 03:00:29 -0800288 mutex_lock(&dquot->dq_lock);
289 mutex_unlock(&dquot->dq_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290}
291
Jan Kara03f6e922008-04-28 02:14:32 -0700292static inline int dquot_dirty(struct dquot *dquot)
293{
294 return test_bit(DQ_MOD_B, &dquot->dq_flags);
295}
296
297static inline int mark_dquot_dirty(struct dquot *dquot)
298{
299 return dquot->dq_sb->dq_op->mark_dirty(dquot);
300}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301
302int dquot_mark_dquot_dirty(struct dquot *dquot)
303{
304 spin_lock(&dq_list_lock);
305 if (!test_and_set_bit(DQ_MOD_B, &dquot->dq_flags))
306 list_add(&dquot->dq_dirty, &sb_dqopt(dquot->dq_sb)->
307 info[dquot->dq_type].dqi_dirty_list);
308 spin_unlock(&dq_list_lock);
309 return 0;
310}
311
312/* This function needs dq_list_lock */
313static inline int clear_dquot_dirty(struct dquot *dquot)
314{
315 if (!test_and_clear_bit(DQ_MOD_B, &dquot->dq_flags))
316 return 0;
317 list_del_init(&dquot->dq_dirty);
318 return 1;
319}
320
321void mark_info_dirty(struct super_block *sb, int type)
322{
323 set_bit(DQF_INFO_DIRTY_B, &sb_dqopt(sb)->info[type].dqi_flags);
324}
325EXPORT_SYMBOL(mark_info_dirty);
326
327/*
328 * Read dquot from disk and alloc space for it
329 */
330
331int dquot_acquire(struct dquot *dquot)
332{
333 int ret = 0, ret2 = 0;
334 struct quota_info *dqopt = sb_dqopt(dquot->dq_sb);
335
Ingo Molnard3be9152006-03-23 03:00:29 -0800336 mutex_lock(&dquot->dq_lock);
337 mutex_lock(&dqopt->dqio_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 if (!test_bit(DQ_READ_B, &dquot->dq_flags))
339 ret = dqopt->ops[dquot->dq_type]->read_dqblk(dquot);
340 if (ret < 0)
341 goto out_iolock;
342 set_bit(DQ_READ_B, &dquot->dq_flags);
343 /* Instantiate dquot if needed */
344 if (!test_bit(DQ_ACTIVE_B, &dquot->dq_flags) && !dquot->dq_off) {
345 ret = dqopt->ops[dquot->dq_type]->commit_dqblk(dquot);
346 /* Write the info if needed */
347 if (info_dirty(&dqopt->info[dquot->dq_type]))
348 ret2 = dqopt->ops[dquot->dq_type]->write_file_info(dquot->dq_sb, dquot->dq_type);
349 if (ret < 0)
350 goto out_iolock;
351 if (ret2 < 0) {
352 ret = ret2;
353 goto out_iolock;
354 }
355 }
356 set_bit(DQ_ACTIVE_B, &dquot->dq_flags);
357out_iolock:
Ingo Molnard3be9152006-03-23 03:00:29 -0800358 mutex_unlock(&dqopt->dqio_mutex);
359 mutex_unlock(&dquot->dq_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 return ret;
361}
362
363/*
364 * Write dquot to disk
365 */
366int dquot_commit(struct dquot *dquot)
367{
368 int ret = 0, ret2 = 0;
369 struct quota_info *dqopt = sb_dqopt(dquot->dq_sb);
370
Ingo Molnard3be9152006-03-23 03:00:29 -0800371 mutex_lock(&dqopt->dqio_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 spin_lock(&dq_list_lock);
373 if (!clear_dquot_dirty(dquot)) {
374 spin_unlock(&dq_list_lock);
375 goto out_sem;
376 }
377 spin_unlock(&dq_list_lock);
378 /* Inactive dquot can be only if there was error during read/init
379 * => we have better not writing it */
380 if (test_bit(DQ_ACTIVE_B, &dquot->dq_flags)) {
381 ret = dqopt->ops[dquot->dq_type]->commit_dqblk(dquot);
382 if (info_dirty(&dqopt->info[dquot->dq_type]))
383 ret2 = dqopt->ops[dquot->dq_type]->write_file_info(dquot->dq_sb, dquot->dq_type);
384 if (ret >= 0)
385 ret = ret2;
386 }
387out_sem:
Ingo Molnard3be9152006-03-23 03:00:29 -0800388 mutex_unlock(&dqopt->dqio_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 return ret;
390}
391
392/*
393 * Release dquot
394 */
395int dquot_release(struct dquot *dquot)
396{
397 int ret = 0, ret2 = 0;
398 struct quota_info *dqopt = sb_dqopt(dquot->dq_sb);
399
Ingo Molnard3be9152006-03-23 03:00:29 -0800400 mutex_lock(&dquot->dq_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 /* Check whether we are not racing with some other dqget() */
402 if (atomic_read(&dquot->dq_count) > 1)
403 goto out_dqlock;
Ingo Molnard3be9152006-03-23 03:00:29 -0800404 mutex_lock(&dqopt->dqio_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 if (dqopt->ops[dquot->dq_type]->release_dqblk) {
406 ret = dqopt->ops[dquot->dq_type]->release_dqblk(dquot);
407 /* Write the info */
408 if (info_dirty(&dqopt->info[dquot->dq_type]))
409 ret2 = dqopt->ops[dquot->dq_type]->write_file_info(dquot->dq_sb, dquot->dq_type);
410 if (ret >= 0)
411 ret = ret2;
412 }
413 clear_bit(DQ_ACTIVE_B, &dquot->dq_flags);
Ingo Molnard3be9152006-03-23 03:00:29 -0800414 mutex_unlock(&dqopt->dqio_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415out_dqlock:
Ingo Molnard3be9152006-03-23 03:00:29 -0800416 mutex_unlock(&dquot->dq_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 return ret;
418}
419
420/* Invalidate all dquots on the list. Note that this function is called after
421 * quota is disabled and pointers from inodes removed so there cannot be new
Jan Kara6362e4d2006-03-23 03:00:17 -0800422 * quota users. There can still be some users of quotas due to inodes being
423 * just deleted or pruned by prune_icache() (those are not attached to any
424 * list). We have to wait for such users.
425 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426static void invalidate_dquots(struct super_block *sb, int type)
427{
Domen Puncerc33ed272005-06-25 14:59:36 -0700428 struct dquot *dquot, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429
Jan Kara6362e4d2006-03-23 03:00:17 -0800430restart:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 spin_lock(&dq_list_lock);
Domen Puncerc33ed272005-06-25 14:59:36 -0700432 list_for_each_entry_safe(dquot, tmp, &inuse_list, dq_inuse) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 if (dquot->dq_sb != sb)
434 continue;
435 if (dquot->dq_type != type)
436 continue;
Jan Kara6362e4d2006-03-23 03:00:17 -0800437 /* Wait for dquot users */
438 if (atomic_read(&dquot->dq_count)) {
439 DEFINE_WAIT(wait);
440
441 atomic_inc(&dquot->dq_count);
442 prepare_to_wait(&dquot->dq_wait_unused, &wait,
443 TASK_UNINTERRUPTIBLE);
444 spin_unlock(&dq_list_lock);
445 /* Once dqput() wakes us up, we know it's time to free
446 * the dquot.
447 * IMPORTANT: we rely on the fact that there is always
448 * at most one process waiting for dquot to free.
449 * Otherwise dq_count would be > 1 and we would never
450 * wake up.
451 */
452 if (atomic_read(&dquot->dq_count) > 1)
453 schedule();
454 finish_wait(&dquot->dq_wait_unused, &wait);
455 dqput(dquot);
456 /* At this moment dquot() need not exist (it could be
457 * reclaimed by prune_dqcache(). Hence we must
458 * restart. */
459 goto restart;
460 }
461 /*
462 * Quota now has no users and it has been written on last
463 * dqput()
464 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 remove_dquot_hash(dquot);
466 remove_free_dquot(dquot);
467 remove_inuse(dquot);
468 kmem_cache_free(dquot_cachep, dquot);
469 }
470 spin_unlock(&dq_list_lock);
471}
472
473int vfs_quota_sync(struct super_block *sb, int type)
474{
475 struct list_head *dirty;
476 struct dquot *dquot;
477 struct quota_info *dqopt = sb_dqopt(sb);
478 int cnt;
479
Ingo Molnard3be9152006-03-23 03:00:29 -0800480 mutex_lock(&dqopt->dqonoff_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
482 if (type != -1 && cnt != type)
483 continue;
484 if (!sb_has_quota_enabled(sb, cnt))
485 continue;
486 spin_lock(&dq_list_lock);
487 dirty = &dqopt->info[cnt].dqi_dirty_list;
488 while (!list_empty(dirty)) {
Pavel Emelianovb5e61812007-05-08 00:30:19 -0700489 dquot = list_first_entry(dirty, struct dquot, dq_dirty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 /* Dirty and inactive can be only bad dquot... */
491 if (!test_bit(DQ_ACTIVE_B, &dquot->dq_flags)) {
492 clear_dquot_dirty(dquot);
493 continue;
494 }
495 /* Now we have active dquot from which someone is
496 * holding reference so we can safely just increase
497 * use count */
498 atomic_inc(&dquot->dq_count);
499 dqstats.lookups++;
500 spin_unlock(&dq_list_lock);
501 sb->dq_op->write_dquot(dquot);
502 dqput(dquot);
503 spin_lock(&dq_list_lock);
504 }
505 spin_unlock(&dq_list_lock);
506 }
507
508 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
509 if ((cnt == type || type == -1) && sb_has_quota_enabled(sb, cnt)
510 && info_dirty(&dqopt->info[cnt]))
511 sb->dq_op->write_info(sb, cnt);
512 spin_lock(&dq_list_lock);
513 dqstats.syncs++;
514 spin_unlock(&dq_list_lock);
Ingo Molnard3be9152006-03-23 03:00:29 -0800515 mutex_unlock(&dqopt->dqonoff_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516
517 return 0;
518}
519
520/* Free unused dquots from cache */
521static void prune_dqcache(int count)
522{
523 struct list_head *head;
524 struct dquot *dquot;
525
526 head = free_dquots.prev;
527 while (head != &free_dquots && count) {
528 dquot = list_entry(head, struct dquot, dq_free);
529 remove_dquot_hash(dquot);
530 remove_free_dquot(dquot);
531 remove_inuse(dquot);
532 kmem_cache_free(dquot_cachep, dquot);
533 count--;
534 head = free_dquots.prev;
535 }
536}
537
538/*
539 * This is called from kswapd when we think we need some
540 * more memory
541 */
542
Al Viro27496a82005-10-21 03:20:48 -0400543static int shrink_dqcache_memory(int nr, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544{
545 if (nr) {
546 spin_lock(&dq_list_lock);
547 prune_dqcache(nr);
548 spin_unlock(&dq_list_lock);
549 }
550 return (dqstats.free_dquots / 100) * sysctl_vfs_cache_pressure;
551}
552
Rusty Russell8e1f9362007-07-17 04:03:17 -0700553static struct shrinker dqcache_shrinker = {
554 .shrink = shrink_dqcache_memory,
555 .seeks = DEFAULT_SEEKS,
556};
557
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558/*
559 * Put reference to dquot
560 * NOTE: If you change this function please check whether dqput_blocks() works right...
Ingo Molnard3be9152006-03-23 03:00:29 -0800561 * MUST be called with either dqptr_sem or dqonoff_mutex held
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 */
563static void dqput(struct dquot *dquot)
564{
Jan Karab48d3802008-07-25 01:46:49 -0700565 int ret;
566
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 if (!dquot)
568 return;
569#ifdef __DQUOT_PARANOIA
570 if (!atomic_read(&dquot->dq_count)) {
571 printk("VFS: dqput: trying to free free dquot\n");
572 printk("VFS: device %s, dquot of %s %d\n",
573 dquot->dq_sb->s_id,
574 quotatypes[dquot->dq_type],
575 dquot->dq_id);
576 BUG();
577 }
578#endif
579
580 spin_lock(&dq_list_lock);
581 dqstats.drops++;
582 spin_unlock(&dq_list_lock);
583we_slept:
584 spin_lock(&dq_list_lock);
585 if (atomic_read(&dquot->dq_count) > 1) {
586 /* We have more than one user... nothing to do */
587 atomic_dec(&dquot->dq_count);
Jan Kara6362e4d2006-03-23 03:00:17 -0800588 /* Releasing dquot during quotaoff phase? */
589 if (!sb_has_quota_enabled(dquot->dq_sb, dquot->dq_type) &&
590 atomic_read(&dquot->dq_count) == 1)
591 wake_up(&dquot->dq_wait_unused);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 spin_unlock(&dq_list_lock);
593 return;
594 }
595 /* Need to release dquot? */
596 if (test_bit(DQ_ACTIVE_B, &dquot->dq_flags) && dquot_dirty(dquot)) {
597 spin_unlock(&dq_list_lock);
598 /* Commit dquot before releasing */
Jan Karab48d3802008-07-25 01:46:49 -0700599 ret = dquot->dq_sb->dq_op->write_dquot(dquot);
600 if (ret < 0) {
601 printk(KERN_ERR "VFS: cannot write quota structure on "
602 "device %s (error %d). Quota may get out of "
603 "sync!\n", dquot->dq_sb->s_id, ret);
604 /*
605 * We clear dirty bit anyway, so that we avoid
606 * infinite loop here
607 */
608 spin_lock(&dq_list_lock);
609 clear_dquot_dirty(dquot);
610 spin_unlock(&dq_list_lock);
611 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 goto we_slept;
613 }
614 /* Clear flag in case dquot was inactive (something bad happened) */
615 clear_dquot_dirty(dquot);
616 if (test_bit(DQ_ACTIVE_B, &dquot->dq_flags)) {
617 spin_unlock(&dq_list_lock);
618 dquot->dq_sb->dq_op->release_dquot(dquot);
619 goto we_slept;
620 }
621 atomic_dec(&dquot->dq_count);
622#ifdef __DQUOT_PARANOIA
623 /* sanity check */
Eric Sesterhenn8abf6a42006-04-02 13:36:13 +0200624 BUG_ON(!list_empty(&dquot->dq_free));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625#endif
626 put_dquot_last(dquot);
627 spin_unlock(&dq_list_lock);
628}
629
630static struct dquot *get_empty_dquot(struct super_block *sb, int type)
631{
632 struct dquot *dquot;
633
Robert P. J. Dayc3762222007-02-10 01:45:03 -0800634 dquot = kmem_cache_zalloc(dquot_cachep, GFP_NOFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 if(!dquot)
636 return NODQUOT;
637
Ingo Molnard3be9152006-03-23 03:00:29 -0800638 mutex_init(&dquot->dq_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 INIT_LIST_HEAD(&dquot->dq_free);
640 INIT_LIST_HEAD(&dquot->dq_inuse);
641 INIT_HLIST_NODE(&dquot->dq_hash);
642 INIT_LIST_HEAD(&dquot->dq_dirty);
Jan Kara6362e4d2006-03-23 03:00:17 -0800643 init_waitqueue_head(&dquot->dq_wait_unused);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 dquot->dq_sb = sb;
645 dquot->dq_type = type;
646 atomic_set(&dquot->dq_count, 1);
647
648 return dquot;
649}
650
651/*
652 * Get reference to dquot
Ingo Molnard3be9152006-03-23 03:00:29 -0800653 * MUST be called with either dqptr_sem or dqonoff_mutex held
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 */
655static struct dquot *dqget(struct super_block *sb, unsigned int id, int type)
656{
657 unsigned int hashent = hashfn(sb, id, type);
658 struct dquot *dquot, *empty = NODQUOT;
659
660 if (!sb_has_quota_enabled(sb, type))
661 return NODQUOT;
662we_slept:
663 spin_lock(&dq_list_lock);
664 if ((dquot = find_dquot(hashent, sb, id, type)) == NODQUOT) {
665 if (empty == NODQUOT) {
666 spin_unlock(&dq_list_lock);
667 if ((empty = get_empty_dquot(sb, type)) == NODQUOT)
668 schedule(); /* Try to wait for a moment... */
669 goto we_slept;
670 }
671 dquot = empty;
672 dquot->dq_id = id;
673 /* all dquots go on the inuse_list */
674 put_inuse(dquot);
675 /* hash it first so it can be found */
676 insert_dquot_hash(dquot);
677 dqstats.lookups++;
678 spin_unlock(&dq_list_lock);
679 } else {
680 if (!atomic_read(&dquot->dq_count))
681 remove_free_dquot(dquot);
682 atomic_inc(&dquot->dq_count);
683 dqstats.cache_hits++;
684 dqstats.lookups++;
685 spin_unlock(&dq_list_lock);
686 if (empty)
687 kmem_cache_free(dquot_cachep, empty);
688 }
689 /* Wait for dq_lock - after this we know that either dquot_release() is already
690 * finished or it will be canceled due to dq_count > 1 test */
691 wait_on_dquot(dquot);
692 /* Read the dquot and instantiate it (everything done only if needed) */
693 if (!test_bit(DQ_ACTIVE_B, &dquot->dq_flags) && sb->dq_op->acquire_dquot(dquot) < 0) {
694 dqput(dquot);
695 return NODQUOT;
696 }
697#ifdef __DQUOT_PARANOIA
Eric Sesterhenn8abf6a42006-04-02 13:36:13 +0200698 BUG_ON(!dquot->dq_sb); /* Has somebody invalidated entry under us? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699#endif
700
701 return dquot;
702}
703
704static int dqinit_needed(struct inode *inode, int type)
705{
706 int cnt;
707
708 if (IS_NOQUOTA(inode))
709 return 0;
710 if (type != -1)
711 return inode->i_dquot[type] == NODQUOT;
712 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
713 if (inode->i_dquot[cnt] == NODQUOT)
714 return 1;
715 return 0;
716}
717
Ingo Molnard3be9152006-03-23 03:00:29 -0800718/* This routine is guarded by dqonoff_mutex mutex */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719static void add_dquot_ref(struct super_block *sb, int type)
720{
Jan Kara941d2382008-02-06 01:37:36 -0800721 struct inode *inode, *old_inode = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722
Christoph Hellwigd003fb72007-02-12 00:51:58 -0800723 spin_lock(&inode_lock);
724 list_for_each_entry(inode, &sb->s_inodes, i_sb_list) {
725 if (!atomic_read(&inode->i_writecount))
726 continue;
727 if (!dqinit_needed(inode, type))
728 continue;
729 if (inode->i_state & (I_FREEING|I_WILL_FREE))
730 continue;
731
732 __iget(inode);
733 spin_unlock(&inode_lock);
734
Jan Kara941d2382008-02-06 01:37:36 -0800735 iput(old_inode);
Christoph Hellwigd003fb72007-02-12 00:51:58 -0800736 sb->dq_op->initialize(inode, type);
Jan Kara941d2382008-02-06 01:37:36 -0800737 /* We hold a reference to 'inode' so it couldn't have been
738 * removed from s_inodes list while we dropped the inode_lock.
739 * We cannot iput the inode now as we can be holding the last
740 * reference and we cannot iput it under inode_lock. So we
741 * keep the reference and iput it later. */
742 old_inode = inode;
743 spin_lock(&inode_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 }
Christoph Hellwigd003fb72007-02-12 00:51:58 -0800745 spin_unlock(&inode_lock);
Jan Kara941d2382008-02-06 01:37:36 -0800746 iput(old_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747}
748
749/* Return 0 if dqput() won't block (note that 1 doesn't necessarily mean blocking) */
750static inline int dqput_blocks(struct dquot *dquot)
751{
752 if (atomic_read(&dquot->dq_count) <= 1)
753 return 1;
754 return 0;
755}
756
757/* Remove references to dquots from inode - add dquot to list for freeing if needed */
758/* We can't race with anybody because we hold dqptr_sem for writing... */
Adrian Bunke5f00f42007-05-08 00:27:22 -0700759static int remove_inode_dquot_ref(struct inode *inode, int type,
760 struct list_head *tofree_head)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761{
762 struct dquot *dquot = inode->i_dquot[type];
763
764 inode->i_dquot[type] = NODQUOT;
765 if (dquot != NODQUOT) {
766 if (dqput_blocks(dquot)) {
767#ifdef __DQUOT_PARANOIA
768 if (atomic_read(&dquot->dq_count) != 1)
769 printk(KERN_WARNING "VFS: Adding dquot with dq_count %d to dispose list.\n", atomic_read(&dquot->dq_count));
770#endif
771 spin_lock(&dq_list_lock);
772 list_add(&dquot->dq_free, tofree_head); /* As dquot must have currently users it can't be on the free list... */
773 spin_unlock(&dq_list_lock);
774 return 1;
775 }
776 else
777 dqput(dquot); /* We have guaranteed we won't block */
778 }
779 return 0;
780}
781
782/* Free list of dquots - called from inode.c */
783/* dquots are removed from inodes, no new references can be got so we are the only ones holding reference */
784static void put_dquot_list(struct list_head *tofree_head)
785{
786 struct list_head *act_head;
787 struct dquot *dquot;
788
789 act_head = tofree_head->next;
790 /* So now we have dquots on the list... Just free them */
791 while (act_head != tofree_head) {
792 dquot = list_entry(act_head, struct dquot, dq_free);
793 act_head = act_head->next;
794 list_del_init(&dquot->dq_free); /* Remove dquot from the list so we won't have problems... */
795 dqput(dquot);
796 }
797}
798
Christoph Hellwigfb58b732007-02-12 00:51:57 -0800799static void remove_dquot_ref(struct super_block *sb, int type,
800 struct list_head *tofree_head)
801{
802 struct inode *inode;
803
804 spin_lock(&inode_lock);
805 list_for_each_entry(inode, &sb->s_inodes, i_sb_list) {
806 if (!IS_NOQUOTA(inode))
807 remove_inode_dquot_ref(inode, type, tofree_head);
808 }
809 spin_unlock(&inode_lock);
810}
811
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812/* Gather all references from inodes and drop them */
813static void drop_dquot_ref(struct super_block *sb, int type)
814{
815 LIST_HEAD(tofree_head);
816
Christoph Hellwigfb58b732007-02-12 00:51:57 -0800817 if (sb->dq_op) {
818 down_write(&sb_dqopt(sb)->dqptr_sem);
819 remove_dquot_ref(sb, type, &tofree_head);
820 up_write(&sb_dqopt(sb)->dqptr_sem);
821 put_dquot_list(&tofree_head);
822 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823}
824
825static inline void dquot_incr_inodes(struct dquot *dquot, unsigned long number)
826{
827 dquot->dq_dqb.dqb_curinodes += number;
828}
829
830static inline void dquot_incr_space(struct dquot *dquot, qsize_t number)
831{
832 dquot->dq_dqb.dqb_curspace += number;
833}
834
835static inline void dquot_decr_inodes(struct dquot *dquot, unsigned long number)
836{
837 if (dquot->dq_dqb.dqb_curinodes > number)
838 dquot->dq_dqb.dqb_curinodes -= number;
839 else
840 dquot->dq_dqb.dqb_curinodes = 0;
841 if (dquot->dq_dqb.dqb_curinodes <= dquot->dq_dqb.dqb_isoftlimit)
842 dquot->dq_dqb.dqb_itime = (time_t) 0;
843 clear_bit(DQ_INODES_B, &dquot->dq_flags);
844}
845
846static inline void dquot_decr_space(struct dquot *dquot, qsize_t number)
847{
848 if (dquot->dq_dqb.dqb_curspace > number)
849 dquot->dq_dqb.dqb_curspace -= number;
850 else
851 dquot->dq_dqb.dqb_curspace = 0;
852 if (toqb(dquot->dq_dqb.dqb_curspace) <= dquot->dq_dqb.dqb_bsoftlimit)
853 dquot->dq_dqb.dqb_btime = (time_t) 0;
854 clear_bit(DQ_BLKS_B, &dquot->dq_flags);
855}
856
Jan Karac5254602007-12-22 14:03:25 -0800857static int warning_issued(struct dquot *dquot, const int warntype)
858{
859 int flag = (warntype == QUOTA_NL_BHARDWARN ||
860 warntype == QUOTA_NL_BSOFTLONGWARN) ? DQ_BLKS_B :
861 ((warntype == QUOTA_NL_IHARDWARN ||
862 warntype == QUOTA_NL_ISOFTLONGWARN) ? DQ_INODES_B : 0);
863
864 if (!flag)
865 return 0;
866 return test_and_set_bit(flag, &dquot->dq_flags);
867}
868
Jan Kara8e893462007-10-16 23:29:31 -0700869#ifdef CONFIG_PRINT_QUOTA_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870static int flag_print_warnings = 1;
871
872static inline int need_print_warning(struct dquot *dquot)
873{
874 if (!flag_print_warnings)
875 return 0;
876
877 switch (dquot->dq_type) {
878 case USRQUOTA:
879 return current->fsuid == dquot->dq_id;
880 case GRPQUOTA:
881 return in_group_p(dquot->dq_id);
882 }
883 return 0;
884}
885
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886/* Print warning to user which exceeded quota */
Jan Karac5254602007-12-22 14:03:25 -0800887static void print_warning(struct dquot *dquot, const int warntype)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888{
889 char *msg = NULL;
Peter Zijlstra24ec8392006-12-08 02:36:04 -0800890 struct tty_struct *tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891
Jan Kara657d3bf2008-07-25 01:46:52 -0700892 if (warntype == QUOTA_NL_IHARDBELOW ||
893 warntype == QUOTA_NL_ISOFTBELOW ||
894 warntype == QUOTA_NL_BHARDBELOW ||
895 warntype == QUOTA_NL_BSOFTBELOW || !need_print_warning(dquot))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 return;
897
Peter Zijlstra24ec8392006-12-08 02:36:04 -0800898 tty = get_current_tty();
899 if (!tty)
Alan Cox452a00d2008-10-13 10:39:13 +0100900 return;
Peter Zijlstra24ec8392006-12-08 02:36:04 -0800901 tty_write_message(tty, dquot->dq_sb->s_id);
Jan Kara8e893462007-10-16 23:29:31 -0700902 if (warntype == QUOTA_NL_ISOFTWARN || warntype == QUOTA_NL_BSOFTWARN)
Peter Zijlstra24ec8392006-12-08 02:36:04 -0800903 tty_write_message(tty, ": warning, ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 else
Peter Zijlstra24ec8392006-12-08 02:36:04 -0800905 tty_write_message(tty, ": write failed, ");
906 tty_write_message(tty, quotatypes[dquot->dq_type]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 switch (warntype) {
Jan Kara8e893462007-10-16 23:29:31 -0700908 case QUOTA_NL_IHARDWARN:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 msg = " file limit reached.\r\n";
910 break;
Jan Kara8e893462007-10-16 23:29:31 -0700911 case QUOTA_NL_ISOFTLONGWARN:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 msg = " file quota exceeded too long.\r\n";
913 break;
Jan Kara8e893462007-10-16 23:29:31 -0700914 case QUOTA_NL_ISOFTWARN:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 msg = " file quota exceeded.\r\n";
916 break;
Jan Kara8e893462007-10-16 23:29:31 -0700917 case QUOTA_NL_BHARDWARN:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 msg = " block limit reached.\r\n";
919 break;
Jan Kara8e893462007-10-16 23:29:31 -0700920 case QUOTA_NL_BSOFTLONGWARN:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 msg = " block quota exceeded too long.\r\n";
922 break;
Jan Kara8e893462007-10-16 23:29:31 -0700923 case QUOTA_NL_BSOFTWARN:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 msg = " block quota exceeded.\r\n";
925 break;
926 }
Peter Zijlstra24ec8392006-12-08 02:36:04 -0800927 tty_write_message(tty, msg);
Alan Cox452a00d2008-10-13 10:39:13 +0100928 tty_kref_put(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929}
Jan Kara8e893462007-10-16 23:29:31 -0700930#endif
931
932#ifdef CONFIG_QUOTA_NETLINK_INTERFACE
933
Jan Kara8e893462007-10-16 23:29:31 -0700934/* Netlink family structure for quota */
935static struct genl_family quota_genl_family = {
936 .id = GENL_ID_GENERATE,
937 .hdrsize = 0,
938 .name = "VFS_DQUOT",
939 .version = 1,
940 .maxattr = QUOTA_NL_A_MAX,
941};
942
943/* Send warning to userspace about user which exceeded quota */
944static void send_warning(const struct dquot *dquot, const char warntype)
945{
946 static atomic_t seq;
947 struct sk_buff *skb;
948 void *msg_head;
949 int ret;
Jan Kara22dd4832007-12-22 14:03:25 -0800950 int msg_size = 4 * nla_total_size(sizeof(u32)) +
951 2 * nla_total_size(sizeof(u64));
Jan Kara8e893462007-10-16 23:29:31 -0700952
953 /* We have to allocate using GFP_NOFS as we are called from a
954 * filesystem performing write and thus further recursion into
955 * the fs to free some data could cause deadlocks. */
Jan Kara22dd4832007-12-22 14:03:25 -0800956 skb = genlmsg_new(msg_size, GFP_NOFS);
Jan Kara8e893462007-10-16 23:29:31 -0700957 if (!skb) {
958 printk(KERN_ERR
959 "VFS: Not enough memory to send quota warning.\n");
960 return;
961 }
962 msg_head = genlmsg_put(skb, 0, atomic_add_return(1, &seq),
963 &quota_genl_family, 0, QUOTA_NL_C_WARNING);
964 if (!msg_head) {
965 printk(KERN_ERR
966 "VFS: Cannot store netlink header in quota warning.\n");
967 goto err_out;
968 }
969 ret = nla_put_u32(skb, QUOTA_NL_A_QTYPE, dquot->dq_type);
970 if (ret)
971 goto attr_err_out;
972 ret = nla_put_u64(skb, QUOTA_NL_A_EXCESS_ID, dquot->dq_id);
973 if (ret)
974 goto attr_err_out;
975 ret = nla_put_u32(skb, QUOTA_NL_A_WARNING, warntype);
976 if (ret)
977 goto attr_err_out;
978 ret = nla_put_u32(skb, QUOTA_NL_A_DEV_MAJOR,
979 MAJOR(dquot->dq_sb->s_dev));
980 if (ret)
981 goto attr_err_out;
982 ret = nla_put_u32(skb, QUOTA_NL_A_DEV_MINOR,
983 MINOR(dquot->dq_sb->s_dev));
984 if (ret)
985 goto attr_err_out;
986 ret = nla_put_u64(skb, QUOTA_NL_A_CAUSED_ID, current->user->uid);
987 if (ret)
988 goto attr_err_out;
989 genlmsg_end(skb, msg_head);
990
991 ret = genlmsg_multicast(skb, 0, quota_genl_family.id, GFP_NOFS);
992 if (ret < 0 && ret != -ESRCH)
993 printk(KERN_ERR
994 "VFS: Failed to send notification message: %d\n", ret);
995 return;
996attr_err_out:
Jan Kara22dd4832007-12-22 14:03:25 -0800997 printk(KERN_ERR "VFS: Not enough space to compose quota message!\n");
Jan Kara8e893462007-10-16 23:29:31 -0700998err_out:
999 kfree_skb(skb);
1000}
1001#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002
Jan Kara087ee8d2007-12-17 16:20:26 -08001003static inline void flush_warnings(struct dquot * const *dquots, char *warntype)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004{
1005 int i;
1006
1007 for (i = 0; i < MAXQUOTAS; i++)
Jan Karac5254602007-12-22 14:03:25 -08001008 if (dquots[i] != NODQUOT && warntype[i] != QUOTA_NL_NOWARN &&
1009 !warning_issued(dquots[i], warntype[i])) {
Jan Kara8e893462007-10-16 23:29:31 -07001010#ifdef CONFIG_PRINT_QUOTA_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 print_warning(dquots[i], warntype[i]);
Jan Kara8e893462007-10-16 23:29:31 -07001012#endif
1013#ifdef CONFIG_QUOTA_NETLINK_INTERFACE
1014 send_warning(dquots[i], warntype[i]);
1015#endif
1016 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017}
1018
1019static inline char ignore_hardlimit(struct dquot *dquot)
1020{
1021 struct mem_dqinfo *info = &sb_dqopt(dquot->dq_sb)->info[dquot->dq_type];
1022
1023 return capable(CAP_SYS_RESOURCE) &&
1024 (info->dqi_format->qf_fmt_id != QFMT_VFS_OLD || !(info->dqi_flags & V1_DQF_RSQUASH));
1025}
1026
1027/* needs dq_data_lock */
1028static int check_idq(struct dquot *dquot, ulong inodes, char *warntype)
1029{
Jan Kara8e893462007-10-16 23:29:31 -07001030 *warntype = QUOTA_NL_NOWARN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 if (inodes <= 0 || test_bit(DQ_FAKE_B, &dquot->dq_flags))
1032 return QUOTA_OK;
1033
1034 if (dquot->dq_dqb.dqb_ihardlimit &&
1035 (dquot->dq_dqb.dqb_curinodes + inodes) > dquot->dq_dqb.dqb_ihardlimit &&
1036 !ignore_hardlimit(dquot)) {
Jan Kara8e893462007-10-16 23:29:31 -07001037 *warntype = QUOTA_NL_IHARDWARN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 return NO_QUOTA;
1039 }
1040
1041 if (dquot->dq_dqb.dqb_isoftlimit &&
1042 (dquot->dq_dqb.dqb_curinodes + inodes) > dquot->dq_dqb.dqb_isoftlimit &&
1043 dquot->dq_dqb.dqb_itime && get_seconds() >= dquot->dq_dqb.dqb_itime &&
1044 !ignore_hardlimit(dquot)) {
Jan Kara8e893462007-10-16 23:29:31 -07001045 *warntype = QUOTA_NL_ISOFTLONGWARN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 return NO_QUOTA;
1047 }
1048
1049 if (dquot->dq_dqb.dqb_isoftlimit &&
1050 (dquot->dq_dqb.dqb_curinodes + inodes) > dquot->dq_dqb.dqb_isoftlimit &&
1051 dquot->dq_dqb.dqb_itime == 0) {
Jan Kara8e893462007-10-16 23:29:31 -07001052 *warntype = QUOTA_NL_ISOFTWARN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 dquot->dq_dqb.dqb_itime = get_seconds() + sb_dqopt(dquot->dq_sb)->info[dquot->dq_type].dqi_igrace;
1054 }
1055
1056 return QUOTA_OK;
1057}
1058
1059/* needs dq_data_lock */
1060static int check_bdq(struct dquot *dquot, qsize_t space, int prealloc, char *warntype)
1061{
Jan Kara8e893462007-10-16 23:29:31 -07001062 *warntype = QUOTA_NL_NOWARN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 if (space <= 0 || test_bit(DQ_FAKE_B, &dquot->dq_flags))
1064 return QUOTA_OK;
1065
1066 if (dquot->dq_dqb.dqb_bhardlimit &&
1067 toqb(dquot->dq_dqb.dqb_curspace + space) > dquot->dq_dqb.dqb_bhardlimit &&
1068 !ignore_hardlimit(dquot)) {
1069 if (!prealloc)
Jan Kara8e893462007-10-16 23:29:31 -07001070 *warntype = QUOTA_NL_BHARDWARN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 return NO_QUOTA;
1072 }
1073
1074 if (dquot->dq_dqb.dqb_bsoftlimit &&
1075 toqb(dquot->dq_dqb.dqb_curspace + space) > dquot->dq_dqb.dqb_bsoftlimit &&
1076 dquot->dq_dqb.dqb_btime && get_seconds() >= dquot->dq_dqb.dqb_btime &&
1077 !ignore_hardlimit(dquot)) {
1078 if (!prealloc)
Jan Kara8e893462007-10-16 23:29:31 -07001079 *warntype = QUOTA_NL_BSOFTLONGWARN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 return NO_QUOTA;
1081 }
1082
1083 if (dquot->dq_dqb.dqb_bsoftlimit &&
1084 toqb(dquot->dq_dqb.dqb_curspace + space) > dquot->dq_dqb.dqb_bsoftlimit &&
1085 dquot->dq_dqb.dqb_btime == 0) {
1086 if (!prealloc) {
Jan Kara8e893462007-10-16 23:29:31 -07001087 *warntype = QUOTA_NL_BSOFTWARN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 dquot->dq_dqb.dqb_btime = get_seconds() + sb_dqopt(dquot->dq_sb)->info[dquot->dq_type].dqi_bgrace;
1089 }
1090 else
1091 /*
1092 * We don't allow preallocation to exceed softlimit so exceeding will
1093 * be always printed
1094 */
1095 return NO_QUOTA;
1096 }
1097
1098 return QUOTA_OK;
1099}
1100
Jan Kara657d3bf2008-07-25 01:46:52 -07001101static int info_idq_free(struct dquot *dquot, ulong inodes)
1102{
1103 if (test_bit(DQ_FAKE_B, &dquot->dq_flags) ||
1104 dquot->dq_dqb.dqb_curinodes <= dquot->dq_dqb.dqb_isoftlimit)
1105 return QUOTA_NL_NOWARN;
1106
1107 if (dquot->dq_dqb.dqb_curinodes - inodes <= dquot->dq_dqb.dqb_isoftlimit)
1108 return QUOTA_NL_ISOFTBELOW;
1109 if (dquot->dq_dqb.dqb_curinodes >= dquot->dq_dqb.dqb_ihardlimit &&
1110 dquot->dq_dqb.dqb_curinodes - inodes < dquot->dq_dqb.dqb_ihardlimit)
1111 return QUOTA_NL_IHARDBELOW;
1112 return QUOTA_NL_NOWARN;
1113}
1114
1115static int info_bdq_free(struct dquot *dquot, qsize_t space)
1116{
1117 if (test_bit(DQ_FAKE_B, &dquot->dq_flags) ||
1118 toqb(dquot->dq_dqb.dqb_curspace) <= dquot->dq_dqb.dqb_bsoftlimit)
1119 return QUOTA_NL_NOWARN;
1120
1121 if (toqb(dquot->dq_dqb.dqb_curspace - space) <=
1122 dquot->dq_dqb.dqb_bsoftlimit)
1123 return QUOTA_NL_BSOFTBELOW;
1124 if (toqb(dquot->dq_dqb.dqb_curspace) >= dquot->dq_dqb.dqb_bhardlimit &&
1125 toqb(dquot->dq_dqb.dqb_curspace - space) <
1126 dquot->dq_dqb.dqb_bhardlimit)
1127 return QUOTA_NL_BHARDBELOW;
1128 return QUOTA_NL_NOWARN;
1129}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130/*
1131 * Initialize quota pointers in inode
1132 * Transaction must be started at entry
1133 */
1134int dquot_initialize(struct inode *inode, int type)
1135{
1136 unsigned int id = 0;
1137 int cnt, ret = 0;
1138
Ingo Molnard3be9152006-03-23 03:00:29 -08001139 /* First test before acquiring mutex - solves deadlocks when we
1140 * re-enter the quota code and are already holding the mutex */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 if (IS_NOQUOTA(inode))
1142 return 0;
1143 down_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
1144 /* Having dqptr_sem we know NOQUOTA flags can't be altered... */
1145 if (IS_NOQUOTA(inode))
1146 goto out_err;
1147 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1148 if (type != -1 && cnt != type)
1149 continue;
1150 if (inode->i_dquot[cnt] == NODQUOT) {
1151 switch (cnt) {
1152 case USRQUOTA:
1153 id = inode->i_uid;
1154 break;
1155 case GRPQUOTA:
1156 id = inode->i_gid;
1157 break;
1158 }
1159 inode->i_dquot[cnt] = dqget(inode->i_sb, id, cnt);
1160 }
1161 }
1162out_err:
1163 up_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
1164 return ret;
1165}
1166
1167/*
1168 * Release all quotas referenced by inode
1169 * Transaction must be started at an entry
1170 */
1171int dquot_drop(struct inode *inode)
1172{
1173 int cnt;
1174
1175 down_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
1176 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1177 if (inode->i_dquot[cnt] != NODQUOT) {
1178 dqput(inode->i_dquot[cnt]);
1179 inode->i_dquot[cnt] = NODQUOT;
1180 }
1181 }
1182 up_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
1183 return 0;
1184}
1185
Jan Karab85f4b82008-07-25 01:46:50 -07001186/* Wrapper to remove references to quota structures from inode */
1187void vfs_dq_drop(struct inode *inode)
1188{
1189 /* Here we can get arbitrary inode from clear_inode() so we have
1190 * to be careful. OTOH we don't need locking as quota operations
1191 * are allowed to change only at mount time */
1192 if (!IS_NOQUOTA(inode) && inode->i_sb && inode->i_sb->dq_op
1193 && inode->i_sb->dq_op->drop) {
1194 int cnt;
1195 /* Test before calling to rule out calls from proc and such
1196 * where we are not allowed to block. Note that this is
1197 * actually reliable test even without the lock - the caller
1198 * must assure that nobody can come after the DQUOT_DROP and
1199 * add quota pointers back anyway */
1200 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1201 if (inode->i_dquot[cnt] != NODQUOT)
1202 break;
1203 if (cnt < MAXQUOTAS)
1204 inode->i_sb->dq_op->drop(inode);
1205 }
1206}
1207
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208/*
1209 * Following four functions update i_blocks+i_bytes fields and
1210 * quota information (together with appropriate checks)
1211 * NOTE: We absolutely rely on the fact that caller dirties
1212 * the inode (usually macros in quotaops.h care about this) and
1213 * holds a handle for the current transaction so that dquot write and
1214 * inode write go into the same transaction.
1215 */
1216
1217/*
1218 * This operation can block, but only after everything is updated
1219 */
1220int dquot_alloc_space(struct inode *inode, qsize_t number, int warn)
1221{
1222 int cnt, ret = NO_QUOTA;
1223 char warntype[MAXQUOTAS];
1224
Ingo Molnard3be9152006-03-23 03:00:29 -08001225 /* First test before acquiring mutex - solves deadlocks when we
1226 * re-enter the quota code and are already holding the mutex */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 if (IS_NOQUOTA(inode)) {
1228out_add:
1229 inode_add_bytes(inode, number);
1230 return QUOTA_OK;
1231 }
1232 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
Jan Kara8e893462007-10-16 23:29:31 -07001233 warntype[cnt] = QUOTA_NL_NOWARN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234
1235 down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1236 if (IS_NOQUOTA(inode)) { /* Now we can do reliable test... */
1237 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1238 goto out_add;
1239 }
1240 spin_lock(&dq_data_lock);
1241 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1242 if (inode->i_dquot[cnt] == NODQUOT)
1243 continue;
1244 if (check_bdq(inode->i_dquot[cnt], number, warn, warntype+cnt) == NO_QUOTA)
1245 goto warn_put_all;
1246 }
1247 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1248 if (inode->i_dquot[cnt] == NODQUOT)
1249 continue;
1250 dquot_incr_space(inode->i_dquot[cnt], number);
1251 }
1252 inode_add_bytes(inode, number);
1253 ret = QUOTA_OK;
1254warn_put_all:
1255 spin_unlock(&dq_data_lock);
1256 if (ret == QUOTA_OK)
1257 /* Dirtify all the dquots - this can block when journalling */
1258 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1259 if (inode->i_dquot[cnt])
1260 mark_dquot_dirty(inode->i_dquot[cnt]);
1261 flush_warnings(inode->i_dquot, warntype);
1262 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1263 return ret;
1264}
1265
1266/*
1267 * This operation can block, but only after everything is updated
1268 */
1269int dquot_alloc_inode(const struct inode *inode, unsigned long number)
1270{
1271 int cnt, ret = NO_QUOTA;
1272 char warntype[MAXQUOTAS];
1273
Ingo Molnard3be9152006-03-23 03:00:29 -08001274 /* First test before acquiring mutex - solves deadlocks when we
1275 * re-enter the quota code and are already holding the mutex */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 if (IS_NOQUOTA(inode))
1277 return QUOTA_OK;
1278 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
Jan Kara8e893462007-10-16 23:29:31 -07001279 warntype[cnt] = QUOTA_NL_NOWARN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1281 if (IS_NOQUOTA(inode)) {
1282 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1283 return QUOTA_OK;
1284 }
1285 spin_lock(&dq_data_lock);
1286 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1287 if (inode->i_dquot[cnt] == NODQUOT)
1288 continue;
1289 if (check_idq(inode->i_dquot[cnt], number, warntype+cnt) == NO_QUOTA)
1290 goto warn_put_all;
1291 }
1292
1293 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1294 if (inode->i_dquot[cnt] == NODQUOT)
1295 continue;
1296 dquot_incr_inodes(inode->i_dquot[cnt], number);
1297 }
1298 ret = QUOTA_OK;
1299warn_put_all:
1300 spin_unlock(&dq_data_lock);
1301 if (ret == QUOTA_OK)
1302 /* Dirtify all the dquots - this can block when journalling */
1303 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1304 if (inode->i_dquot[cnt])
1305 mark_dquot_dirty(inode->i_dquot[cnt]);
Jan Kara087ee8d2007-12-17 16:20:26 -08001306 flush_warnings(inode->i_dquot, warntype);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1308 return ret;
1309}
1310
1311/*
1312 * This operation can block, but only after everything is updated
1313 */
1314int dquot_free_space(struct inode *inode, qsize_t number)
1315{
1316 unsigned int cnt;
Jan Kara657d3bf2008-07-25 01:46:52 -07001317 char warntype[MAXQUOTAS];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318
Ingo Molnard3be9152006-03-23 03:00:29 -08001319 /* First test before acquiring mutex - solves deadlocks when we
1320 * re-enter the quota code and are already holding the mutex */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 if (IS_NOQUOTA(inode)) {
1322out_sub:
1323 inode_sub_bytes(inode, number);
1324 return QUOTA_OK;
1325 }
Jan Kara657d3bf2008-07-25 01:46:52 -07001326
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1328 /* Now recheck reliably when holding dqptr_sem */
1329 if (IS_NOQUOTA(inode)) {
1330 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1331 goto out_sub;
1332 }
1333 spin_lock(&dq_data_lock);
1334 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1335 if (inode->i_dquot[cnt] == NODQUOT)
1336 continue;
Jan Kara657d3bf2008-07-25 01:46:52 -07001337 warntype[cnt] = info_bdq_free(inode->i_dquot[cnt], number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 dquot_decr_space(inode->i_dquot[cnt], number);
1339 }
1340 inode_sub_bytes(inode, number);
1341 spin_unlock(&dq_data_lock);
1342 /* Dirtify all the dquots - this can block when journalling */
1343 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1344 if (inode->i_dquot[cnt])
1345 mark_dquot_dirty(inode->i_dquot[cnt]);
Jan Kara657d3bf2008-07-25 01:46:52 -07001346 flush_warnings(inode->i_dquot, warntype);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1348 return QUOTA_OK;
1349}
1350
1351/*
1352 * This operation can block, but only after everything is updated
1353 */
1354int dquot_free_inode(const struct inode *inode, unsigned long number)
1355{
1356 unsigned int cnt;
Jan Kara657d3bf2008-07-25 01:46:52 -07001357 char warntype[MAXQUOTAS];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358
Ingo Molnard3be9152006-03-23 03:00:29 -08001359 /* First test before acquiring mutex - solves deadlocks when we
1360 * re-enter the quota code and are already holding the mutex */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 if (IS_NOQUOTA(inode))
1362 return QUOTA_OK;
Jan Kara657d3bf2008-07-25 01:46:52 -07001363
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1365 /* Now recheck reliably when holding dqptr_sem */
1366 if (IS_NOQUOTA(inode)) {
1367 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1368 return QUOTA_OK;
1369 }
1370 spin_lock(&dq_data_lock);
1371 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1372 if (inode->i_dquot[cnt] == NODQUOT)
1373 continue;
Jan Kara657d3bf2008-07-25 01:46:52 -07001374 warntype[cnt] = info_idq_free(inode->i_dquot[cnt], number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 dquot_decr_inodes(inode->i_dquot[cnt], number);
1376 }
1377 spin_unlock(&dq_data_lock);
1378 /* Dirtify all the dquots - this can block when journalling */
1379 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1380 if (inode->i_dquot[cnt])
1381 mark_dquot_dirty(inode->i_dquot[cnt]);
Jan Kara657d3bf2008-07-25 01:46:52 -07001382 flush_warnings(inode->i_dquot, warntype);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1384 return QUOTA_OK;
1385}
1386
1387/*
1388 * Transfer the number of inode and blocks from one diskquota to an other.
1389 *
1390 * This operation can block, but only after everything is updated
1391 * A transaction must be started when entering this function.
1392 */
1393int dquot_transfer(struct inode *inode, struct iattr *iattr)
1394{
1395 qsize_t space;
1396 struct dquot *transfer_from[MAXQUOTAS];
1397 struct dquot *transfer_to[MAXQUOTAS];
1398 int cnt, ret = NO_QUOTA, chuid = (iattr->ia_valid & ATTR_UID) && inode->i_uid != iattr->ia_uid,
1399 chgid = (iattr->ia_valid & ATTR_GID) && inode->i_gid != iattr->ia_gid;
Jan Kara657d3bf2008-07-25 01:46:52 -07001400 char warntype_to[MAXQUOTAS];
1401 char warntype_from_inodes[MAXQUOTAS], warntype_from_space[MAXQUOTAS];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402
Ingo Molnard3be9152006-03-23 03:00:29 -08001403 /* First test before acquiring mutex - solves deadlocks when we
1404 * re-enter the quota code and are already holding the mutex */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405 if (IS_NOQUOTA(inode))
1406 return QUOTA_OK;
1407 /* Clear the arrays */
1408 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1409 transfer_to[cnt] = transfer_from[cnt] = NODQUOT;
Jan Kara657d3bf2008-07-25 01:46:52 -07001410 warntype_to[cnt] = QUOTA_NL_NOWARN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411 }
1412 down_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
1413 /* Now recheck reliably when holding dqptr_sem */
1414 if (IS_NOQUOTA(inode)) { /* File without quota accounting? */
1415 up_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
1416 return QUOTA_OK;
1417 }
1418 /* First build the transfer_to list - here we can block on
1419 * reading/instantiating of dquots. We know that the transaction for
1420 * us was already started so we don't violate lock ranking here */
1421 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1422 switch (cnt) {
1423 case USRQUOTA:
1424 if (!chuid)
1425 continue;
1426 transfer_to[cnt] = dqget(inode->i_sb, iattr->ia_uid, cnt);
1427 break;
1428 case GRPQUOTA:
1429 if (!chgid)
1430 continue;
1431 transfer_to[cnt] = dqget(inode->i_sb, iattr->ia_gid, cnt);
1432 break;
1433 }
1434 }
1435 spin_lock(&dq_data_lock);
1436 space = inode_get_bytes(inode);
1437 /* Build the transfer_from list and check the limits */
1438 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1439 if (transfer_to[cnt] == NODQUOT)
1440 continue;
1441 transfer_from[cnt] = inode->i_dquot[cnt];
Jan Kara657d3bf2008-07-25 01:46:52 -07001442 if (check_idq(transfer_to[cnt], 1, warntype_to + cnt) ==
1443 NO_QUOTA || check_bdq(transfer_to[cnt], space, 0,
1444 warntype_to + cnt) == NO_QUOTA)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445 goto warn_put_all;
1446 }
1447
1448 /*
1449 * Finally perform the needed transfer from transfer_from to transfer_to
1450 */
1451 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1452 /*
1453 * Skip changes for same uid or gid or for turned off quota-type.
1454 */
1455 if (transfer_to[cnt] == NODQUOT)
1456 continue;
1457
1458 /* Due to IO error we might not have transfer_from[] structure */
1459 if (transfer_from[cnt]) {
Jan Kara657d3bf2008-07-25 01:46:52 -07001460 warntype_from_inodes[cnt] =
1461 info_idq_free(transfer_from[cnt], 1);
1462 warntype_from_space[cnt] =
1463 info_bdq_free(transfer_from[cnt], space);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 dquot_decr_inodes(transfer_from[cnt], 1);
1465 dquot_decr_space(transfer_from[cnt], space);
1466 }
1467
1468 dquot_incr_inodes(transfer_to[cnt], 1);
1469 dquot_incr_space(transfer_to[cnt], space);
1470
1471 inode->i_dquot[cnt] = transfer_to[cnt];
1472 }
1473 ret = QUOTA_OK;
1474warn_put_all:
1475 spin_unlock(&dq_data_lock);
1476 /* Dirtify all the dquots - this can block when journalling */
1477 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1478 if (transfer_from[cnt])
1479 mark_dquot_dirty(transfer_from[cnt]);
1480 if (transfer_to[cnt])
1481 mark_dquot_dirty(transfer_to[cnt]);
1482 }
Jan Kara657d3bf2008-07-25 01:46:52 -07001483 flush_warnings(transfer_to, warntype_to);
1484 flush_warnings(transfer_from, warntype_from_inodes);
1485 flush_warnings(transfer_from, warntype_from_space);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486
1487 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1488 if (ret == QUOTA_OK && transfer_from[cnt] != NODQUOT)
1489 dqput(transfer_from[cnt]);
1490 if (ret == NO_QUOTA && transfer_to[cnt] != NODQUOT)
1491 dqput(transfer_to[cnt]);
1492 }
1493 up_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
1494 return ret;
1495}
1496
Jan Karab85f4b82008-07-25 01:46:50 -07001497/* Wrapper for transferring ownership of an inode */
1498int vfs_dq_transfer(struct inode *inode, struct iattr *iattr)
1499{
1500 if (sb_any_quota_enabled(inode->i_sb) && !IS_NOQUOTA(inode)) {
1501 vfs_dq_init(inode);
1502 if (inode->i_sb->dq_op->transfer(inode, iattr) == NO_QUOTA)
1503 return 1;
1504 }
1505 return 0;
1506}
1507
1508
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509/*
1510 * Write info of quota file to disk
1511 */
1512int dquot_commit_info(struct super_block *sb, int type)
1513{
1514 int ret;
1515 struct quota_info *dqopt = sb_dqopt(sb);
1516
Ingo Molnard3be9152006-03-23 03:00:29 -08001517 mutex_lock(&dqopt->dqio_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518 ret = dqopt->ops[type]->write_file_info(sb, type);
Ingo Molnard3be9152006-03-23 03:00:29 -08001519 mutex_unlock(&dqopt->dqio_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520 return ret;
1521}
1522
1523/*
1524 * Definitions of diskquota operations.
1525 */
1526struct dquot_operations dquot_operations = {
1527 .initialize = dquot_initialize,
1528 .drop = dquot_drop,
1529 .alloc_space = dquot_alloc_space,
1530 .alloc_inode = dquot_alloc_inode,
1531 .free_space = dquot_free_space,
1532 .free_inode = dquot_free_inode,
1533 .transfer = dquot_transfer,
1534 .write_dquot = dquot_commit,
1535 .acquire_dquot = dquot_acquire,
1536 .release_dquot = dquot_release,
1537 .mark_dirty = dquot_mark_dquot_dirty,
1538 .write_info = dquot_commit_info
1539};
1540
1541static inline void set_enable_flags(struct quota_info *dqopt, int type)
1542{
1543 switch (type) {
1544 case USRQUOTA:
1545 dqopt->flags |= DQUOT_USR_ENABLED;
Jan Kara0ff5af82008-04-28 02:14:33 -07001546 dqopt->flags &= ~DQUOT_USR_SUSPENDED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547 break;
1548 case GRPQUOTA:
1549 dqopt->flags |= DQUOT_GRP_ENABLED;
Jan Kara0ff5af82008-04-28 02:14:33 -07001550 dqopt->flags &= ~DQUOT_GRP_SUSPENDED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551 break;
1552 }
1553}
1554
Jan Kara0ff5af82008-04-28 02:14:33 -07001555static inline void reset_enable_flags(struct quota_info *dqopt, int type,
1556 int remount)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557{
1558 switch (type) {
1559 case USRQUOTA:
1560 dqopt->flags &= ~DQUOT_USR_ENABLED;
Jan Kara0ff5af82008-04-28 02:14:33 -07001561 if (remount)
1562 dqopt->flags |= DQUOT_USR_SUSPENDED;
1563 else
1564 dqopt->flags &= ~DQUOT_USR_SUSPENDED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565 break;
1566 case GRPQUOTA:
1567 dqopt->flags &= ~DQUOT_GRP_ENABLED;
Jan Kara0ff5af82008-04-28 02:14:33 -07001568 if (remount)
1569 dqopt->flags |= DQUOT_GRP_SUSPENDED;
1570 else
1571 dqopt->flags &= ~DQUOT_GRP_SUSPENDED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572 break;
1573 }
1574}
1575
Jan Kara0ff5af82008-04-28 02:14:33 -07001576
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577/*
1578 * Turn quota off on a device. type == -1 ==> quotaoff for all types (umount)
1579 */
Jan Kara0ff5af82008-04-28 02:14:33 -07001580int vfs_quota_off(struct super_block *sb, int type, int remount)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581{
Jan Kara0ff5af82008-04-28 02:14:33 -07001582 int cnt, ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583 struct quota_info *dqopt = sb_dqopt(sb);
1584 struct inode *toputinode[MAXQUOTAS];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585
1586 /* We need to serialize quota_off() for device */
Ingo Molnard3be9152006-03-23 03:00:29 -08001587 mutex_lock(&dqopt->dqonoff_mutex);
Jan Kara9377abd2008-05-12 14:02:08 -07001588
1589 /*
1590 * Skip everything if there's nothing to do. We have to do this because
1591 * sometimes we are called when fill_super() failed and calling
1592 * sync_fs() in such cases does no good.
1593 */
1594 if (!sb_any_quota_enabled(sb) && !sb_any_quota_suspended(sb)) {
1595 mutex_unlock(&dqopt->dqonoff_mutex);
1596 return 0;
1597 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1599 toputinode[cnt] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600 if (type != -1 && cnt != type)
1601 continue;
Jan Kara0ff5af82008-04-28 02:14:33 -07001602 /* If we keep inodes of quota files after remount and quotaoff
1603 * is called, drop kept inodes. */
1604 if (!remount && sb_has_quota_suspended(sb, cnt)) {
1605 iput(dqopt->files[cnt]);
1606 dqopt->files[cnt] = NULL;
1607 reset_enable_flags(dqopt, cnt, 0);
1608 continue;
1609 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610 if (!sb_has_quota_enabled(sb, cnt))
1611 continue;
Jan Kara0ff5af82008-04-28 02:14:33 -07001612 reset_enable_flags(dqopt, cnt, remount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613
1614 /* Note: these are blocking operations */
1615 drop_dquot_ref(sb, cnt);
1616 invalidate_dquots(sb, cnt);
1617 /*
1618 * Now all dquots should be invalidated, all writes done so we should be only
1619 * users of the info. No locks needed.
1620 */
1621 if (info_dirty(&dqopt->info[cnt]))
1622 sb->dq_op->write_info(sb, cnt);
1623 if (dqopt->ops[cnt]->free_file_info)
1624 dqopt->ops[cnt]->free_file_info(sb, cnt);
1625 put_quota_format(dqopt->info[cnt].dqi_format);
1626
1627 toputinode[cnt] = dqopt->files[cnt];
Jan Kara0ff5af82008-04-28 02:14:33 -07001628 if (!remount)
1629 dqopt->files[cnt] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630 dqopt->info[cnt].dqi_flags = 0;
1631 dqopt->info[cnt].dqi_igrace = 0;
1632 dqopt->info[cnt].dqi_bgrace = 0;
1633 dqopt->ops[cnt] = NULL;
1634 }
Ingo Molnard3be9152006-03-23 03:00:29 -08001635 mutex_unlock(&dqopt->dqonoff_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636 /* Sync the superblock so that buffers with quota data are written to
Al Viro7b7b1ac2005-11-07 17:13:39 -05001637 * disk (and so userspace sees correct data afterwards). */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638 if (sb->s_op->sync_fs)
1639 sb->s_op->sync_fs(sb, 1);
1640 sync_blockdev(sb->s_bdev);
1641 /* Now the quota files are just ordinary files and we can set the
1642 * inode flags back. Moreover we discard the pagecache so that
1643 * userspace sees the writes we did bypassing the pagecache. We
1644 * must also discard the blockdev buffers so that we see the
1645 * changes done by userspace on the next quotaon() */
1646 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1647 if (toputinode[cnt]) {
Ingo Molnard3be9152006-03-23 03:00:29 -08001648 mutex_lock(&dqopt->dqonoff_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649 /* If quota was reenabled in the meantime, we have
1650 * nothing to do */
1651 if (!sb_has_quota_enabled(sb, cnt)) {
Jan Kara79254092007-05-16 22:11:19 -07001652 mutex_lock_nested(&toputinode[cnt]->i_mutex, I_MUTEX_QUOTA);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653 toputinode[cnt]->i_flags &= ~(S_IMMUTABLE |
1654 S_NOATIME | S_NOQUOTA);
1655 truncate_inode_pages(&toputinode[cnt]->i_data, 0);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001656 mutex_unlock(&toputinode[cnt]->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657 mark_inode_dirty(toputinode[cnt]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658 }
Ingo Molnard3be9152006-03-23 03:00:29 -08001659 mutex_unlock(&dqopt->dqonoff_mutex);
Jan Kara0ff5af82008-04-28 02:14:33 -07001660 /* On remount RO, we keep the inode pointer so that we
1661 * can reenable quota on the subsequent remount RW.
1662 * But we have better not keep inode pointer when there
1663 * is pending delete on the quota file... */
1664 if (!remount)
1665 iput(toputinode[cnt]);
1666 else if (!toputinode[cnt]->i_nlink)
1667 ret = -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668 }
1669 if (sb->s_bdev)
Peter Zijlstraf98393a2007-05-06 14:49:54 -07001670 invalidate_bdev(sb->s_bdev);
Jan Kara0ff5af82008-04-28 02:14:33 -07001671 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672}
1673
1674/*
1675 * Turn quotas on on a device
1676 */
1677
1678/* Helper function when we already have the inode */
1679static int vfs_quota_on_inode(struct inode *inode, int type, int format_id)
1680{
1681 struct quota_format_type *fmt = find_quota_format(format_id);
1682 struct super_block *sb = inode->i_sb;
1683 struct quota_info *dqopt = sb_dqopt(sb);
1684 int error;
1685 int oldflags = -1;
1686
1687 if (!fmt)
1688 return -ESRCH;
1689 if (!S_ISREG(inode->i_mode)) {
1690 error = -EACCES;
1691 goto out_fmt;
1692 }
1693 if (IS_RDONLY(inode)) {
1694 error = -EROFS;
1695 goto out_fmt;
1696 }
1697 if (!sb->s_op->quota_write || !sb->s_op->quota_read) {
1698 error = -EINVAL;
1699 goto out_fmt;
1700 }
1701
1702 /* As we bypass the pagecache we must now flush the inode so that
1703 * we see all the changes from userspace... */
1704 write_inode_now(inode, 1);
1705 /* And now flush the block cache so that kernel sees the changes */
Peter Zijlstraf98393a2007-05-06 14:49:54 -07001706 invalidate_bdev(sb->s_bdev);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001707 mutex_lock(&inode->i_mutex);
Ingo Molnard3be9152006-03-23 03:00:29 -08001708 mutex_lock(&dqopt->dqonoff_mutex);
Jan Kara0ff5af82008-04-28 02:14:33 -07001709 if (sb_has_quota_enabled(sb, type) ||
1710 sb_has_quota_suspended(sb, type)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711 error = -EBUSY;
1712 goto out_lock;
1713 }
1714 /* We don't want quota and atime on quota files (deadlocks possible)
1715 * Also nobody should write to the file - we use special IO operations
1716 * which ignore the immutable bit. */
1717 down_write(&dqopt->dqptr_sem);
1718 oldflags = inode->i_flags & (S_NOATIME | S_IMMUTABLE | S_NOQUOTA);
1719 inode->i_flags |= S_NOQUOTA | S_NOATIME | S_IMMUTABLE;
1720 up_write(&dqopt->dqptr_sem);
Jan Kara31e7ad62005-04-16 15:25:46 -07001721 sb->dq_op->drop(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722
1723 error = -EIO;
1724 dqopt->files[type] = igrab(inode);
1725 if (!dqopt->files[type])
1726 goto out_lock;
1727 error = -EINVAL;
1728 if (!fmt->qf_ops->check_quota_file(sb, type))
1729 goto out_file_init;
1730
1731 dqopt->ops[type] = fmt->qf_ops;
1732 dqopt->info[type].dqi_format = fmt;
Jan Kara0ff5af82008-04-28 02:14:33 -07001733 dqopt->info[type].dqi_fmt_id = format_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734 INIT_LIST_HEAD(&dqopt->info[type].dqi_dirty_list);
Ingo Molnard3be9152006-03-23 03:00:29 -08001735 mutex_lock(&dqopt->dqio_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736 if ((error = dqopt->ops[type]->read_file_info(sb, type)) < 0) {
Ingo Molnard3be9152006-03-23 03:00:29 -08001737 mutex_unlock(&dqopt->dqio_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738 goto out_file_init;
1739 }
Ingo Molnard3be9152006-03-23 03:00:29 -08001740 mutex_unlock(&dqopt->dqio_mutex);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001741 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742 set_enable_flags(dqopt, type);
1743
1744 add_dquot_ref(sb, type);
Ingo Molnard3be9152006-03-23 03:00:29 -08001745 mutex_unlock(&dqopt->dqonoff_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746
1747 return 0;
1748
1749out_file_init:
1750 dqopt->files[type] = NULL;
1751 iput(inode);
1752out_lock:
Ingo Molnard3be9152006-03-23 03:00:29 -08001753 mutex_unlock(&dqopt->dqonoff_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754 if (oldflags != -1) {
1755 down_write(&dqopt->dqptr_sem);
1756 /* Set the flags back (in the case of accidental quotaon()
1757 * on a wrong file we don't want to mess up the flags) */
1758 inode->i_flags &= ~(S_NOATIME | S_NOQUOTA | S_IMMUTABLE);
1759 inode->i_flags |= oldflags;
1760 up_write(&dqopt->dqptr_sem);
1761 }
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001762 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763out_fmt:
1764 put_quota_format(fmt);
1765
1766 return error;
1767}
1768
Jan Kara0ff5af82008-04-28 02:14:33 -07001769/* Reenable quotas on remount RW */
1770static int vfs_quota_on_remount(struct super_block *sb, int type)
1771{
1772 struct quota_info *dqopt = sb_dqopt(sb);
1773 struct inode *inode;
1774 int ret;
1775
1776 mutex_lock(&dqopt->dqonoff_mutex);
1777 if (!sb_has_quota_suspended(sb, type)) {
1778 mutex_unlock(&dqopt->dqonoff_mutex);
1779 return 0;
1780 }
1781 BUG_ON(sb_has_quota_enabled(sb, type));
1782
1783 inode = dqopt->files[type];
1784 dqopt->files[type] = NULL;
1785 reset_enable_flags(dqopt, type, 0);
1786 mutex_unlock(&dqopt->dqonoff_mutex);
1787
1788 ret = vfs_quota_on_inode(inode, type, dqopt->info[type].dqi_fmt_id);
1789 iput(inode);
1790
1791 return ret;
1792}
1793
Al Viro77e69da2008-08-01 04:29:18 -04001794int vfs_quota_on_path(struct super_block *sb, int type, int format_id,
1795 struct path *path)
1796{
1797 int error = security_quota_on(path->dentry);
1798 if (error)
1799 return error;
1800 /* Quota file not on the same filesystem? */
1801 if (path->mnt->mnt_sb != sb)
1802 error = -EXDEV;
1803 else
1804 error = vfs_quota_on_inode(path->dentry->d_inode, type,
1805 format_id);
1806 return error;
1807}
1808
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809/* Actual function called from quotactl() */
Jan Kara0ff5af82008-04-28 02:14:33 -07001810int vfs_quota_on(struct super_block *sb, int type, int format_id, char *path,
1811 int remount)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812{
1813 struct nameidata nd;
1814 int error;
1815
Jan Kara0ff5af82008-04-28 02:14:33 -07001816 if (remount)
1817 return vfs_quota_on_remount(sb, type);
1818
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819 error = path_lookup(path, LOOKUP_FOLLOW, &nd);
Al Viro77e69da2008-08-01 04:29:18 -04001820 if (!error) {
1821 error = vfs_quota_on_path(sb, type, format_id, &nd.path);
1822 path_put(&nd.path);
1823 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824 return error;
1825}
1826
1827/*
1828 * This function is used when filesystem needs to initialize quotas
1829 * during mount time.
1830 */
Christoph Hellwig84de8562005-06-23 00:09:16 -07001831int vfs_quota_on_mount(struct super_block *sb, char *qf_name,
1832 int format_id, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833{
Christoph Hellwig84de8562005-06-23 00:09:16 -07001834 struct dentry *dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835 int error;
1836
Christoph Hellwig2fa389c2005-06-23 00:09:16 -07001837 dentry = lookup_one_len(qf_name, sb->s_root, strlen(qf_name));
Christoph Hellwig84de8562005-06-23 00:09:16 -07001838 if (IS_ERR(dentry))
1839 return PTR_ERR(dentry);
1840
Jan Kara154f4842005-11-28 13:44:14 -08001841 if (!dentry->d_inode) {
1842 error = -ENOENT;
1843 goto out;
1844 }
1845
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846 error = security_quota_on(dentry);
Christoph Hellwig84de8562005-06-23 00:09:16 -07001847 if (!error)
1848 error = vfs_quota_on_inode(dentry->d_inode, type, format_id);
1849
Jan Kara154f4842005-11-28 13:44:14 -08001850out:
Christoph Hellwig84de8562005-06-23 00:09:16 -07001851 dput(dentry);
1852 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853}
1854
Jan Karab85f4b82008-07-25 01:46:50 -07001855/* Wrapper to turn on quotas when remounting rw */
1856int vfs_dq_quota_on_remount(struct super_block *sb)
1857{
1858 int cnt;
1859 int ret = 0, err;
1860
1861 if (!sb->s_qcop || !sb->s_qcop->quota_on)
1862 return -ENOSYS;
1863 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1864 err = sb->s_qcop->quota_on(sb, cnt, 0, NULL, 1);
1865 if (err < 0 && !ret)
1866 ret = err;
1867 }
1868 return ret;
1869}
1870
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871/* Generic routine for getting common part of quota structure */
1872static void do_get_dqblk(struct dquot *dquot, struct if_dqblk *di)
1873{
1874 struct mem_dqblk *dm = &dquot->dq_dqb;
1875
1876 spin_lock(&dq_data_lock);
1877 di->dqb_bhardlimit = dm->dqb_bhardlimit;
1878 di->dqb_bsoftlimit = dm->dqb_bsoftlimit;
1879 di->dqb_curspace = dm->dqb_curspace;
1880 di->dqb_ihardlimit = dm->dqb_ihardlimit;
1881 di->dqb_isoftlimit = dm->dqb_isoftlimit;
1882 di->dqb_curinodes = dm->dqb_curinodes;
1883 di->dqb_btime = dm->dqb_btime;
1884 di->dqb_itime = dm->dqb_itime;
1885 di->dqb_valid = QIF_ALL;
1886 spin_unlock(&dq_data_lock);
1887}
1888
1889int vfs_get_dqblk(struct super_block *sb, int type, qid_t id, struct if_dqblk *di)
1890{
1891 struct dquot *dquot;
1892
Ingo Molnard3be9152006-03-23 03:00:29 -08001893 mutex_lock(&sb_dqopt(sb)->dqonoff_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894 if (!(dquot = dqget(sb, id, type))) {
Ingo Molnard3be9152006-03-23 03:00:29 -08001895 mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896 return -ESRCH;
1897 }
1898 do_get_dqblk(dquot, di);
1899 dqput(dquot);
Ingo Molnard3be9152006-03-23 03:00:29 -08001900 mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901 return 0;
1902}
1903
1904/* Generic routine for setting common part of quota structure */
Andrew Perepechko338bf9a2008-04-28 02:14:31 -07001905static int do_set_dqblk(struct dquot *dquot, struct if_dqblk *di)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906{
1907 struct mem_dqblk *dm = &dquot->dq_dqb;
1908 int check_blim = 0, check_ilim = 0;
Andrew Perepechko338bf9a2008-04-28 02:14:31 -07001909 struct mem_dqinfo *dqi = &sb_dqopt(dquot->dq_sb)->info[dquot->dq_type];
1910
1911 if ((di->dqb_valid & QIF_BLIMITS &&
1912 (di->dqb_bhardlimit > dqi->dqi_maxblimit ||
1913 di->dqb_bsoftlimit > dqi->dqi_maxblimit)) ||
1914 (di->dqb_valid & QIF_ILIMITS &&
1915 (di->dqb_ihardlimit > dqi->dqi_maxilimit ||
1916 di->dqb_isoftlimit > dqi->dqi_maxilimit)))
1917 return -ERANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918
1919 spin_lock(&dq_data_lock);
1920 if (di->dqb_valid & QIF_SPACE) {
1921 dm->dqb_curspace = di->dqb_curspace;
1922 check_blim = 1;
1923 }
1924 if (di->dqb_valid & QIF_BLIMITS) {
1925 dm->dqb_bsoftlimit = di->dqb_bsoftlimit;
1926 dm->dqb_bhardlimit = di->dqb_bhardlimit;
1927 check_blim = 1;
1928 }
1929 if (di->dqb_valid & QIF_INODES) {
1930 dm->dqb_curinodes = di->dqb_curinodes;
1931 check_ilim = 1;
1932 }
1933 if (di->dqb_valid & QIF_ILIMITS) {
1934 dm->dqb_isoftlimit = di->dqb_isoftlimit;
1935 dm->dqb_ihardlimit = di->dqb_ihardlimit;
1936 check_ilim = 1;
1937 }
1938 if (di->dqb_valid & QIF_BTIME)
1939 dm->dqb_btime = di->dqb_btime;
1940 if (di->dqb_valid & QIF_ITIME)
1941 dm->dqb_itime = di->dqb_itime;
1942
1943 if (check_blim) {
1944 if (!dm->dqb_bsoftlimit || toqb(dm->dqb_curspace) < dm->dqb_bsoftlimit) {
1945 dm->dqb_btime = 0;
1946 clear_bit(DQ_BLKS_B, &dquot->dq_flags);
1947 }
1948 else if (!(di->dqb_valid & QIF_BTIME)) /* Set grace only if user hasn't provided his own... */
Andrew Perepechko338bf9a2008-04-28 02:14:31 -07001949 dm->dqb_btime = get_seconds() + dqi->dqi_bgrace;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950 }
1951 if (check_ilim) {
1952 if (!dm->dqb_isoftlimit || dm->dqb_curinodes < dm->dqb_isoftlimit) {
1953 dm->dqb_itime = 0;
1954 clear_bit(DQ_INODES_B, &dquot->dq_flags);
1955 }
1956 else if (!(di->dqb_valid & QIF_ITIME)) /* Set grace only if user hasn't provided his own... */
Andrew Perepechko338bf9a2008-04-28 02:14:31 -07001957 dm->dqb_itime = get_seconds() + dqi->dqi_igrace;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958 }
1959 if (dm->dqb_bhardlimit || dm->dqb_bsoftlimit || dm->dqb_ihardlimit || dm->dqb_isoftlimit)
1960 clear_bit(DQ_FAKE_B, &dquot->dq_flags);
1961 else
1962 set_bit(DQ_FAKE_B, &dquot->dq_flags);
1963 spin_unlock(&dq_data_lock);
1964 mark_dquot_dirty(dquot);
Andrew Perepechko338bf9a2008-04-28 02:14:31 -07001965
1966 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967}
1968
1969int vfs_set_dqblk(struct super_block *sb, int type, qid_t id, struct if_dqblk *di)
1970{
1971 struct dquot *dquot;
Andrew Perepechko338bf9a2008-04-28 02:14:31 -07001972 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973
Ingo Molnard3be9152006-03-23 03:00:29 -08001974 mutex_lock(&sb_dqopt(sb)->dqonoff_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975 if (!(dquot = dqget(sb, id, type))) {
Ingo Molnard3be9152006-03-23 03:00:29 -08001976 mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977 return -ESRCH;
1978 }
Andrew Perepechko338bf9a2008-04-28 02:14:31 -07001979 rc = do_set_dqblk(dquot, di);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980 dqput(dquot);
Ingo Molnard3be9152006-03-23 03:00:29 -08001981 mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
Andrew Perepechko338bf9a2008-04-28 02:14:31 -07001982 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983}
1984
1985/* Generic routine for getting common part of quota file information */
1986int vfs_get_dqinfo(struct super_block *sb, int type, struct if_dqinfo *ii)
1987{
1988 struct mem_dqinfo *mi;
1989
Ingo Molnard3be9152006-03-23 03:00:29 -08001990 mutex_lock(&sb_dqopt(sb)->dqonoff_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991 if (!sb_has_quota_enabled(sb, type)) {
Ingo Molnard3be9152006-03-23 03:00:29 -08001992 mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993 return -ESRCH;
1994 }
1995 mi = sb_dqopt(sb)->info + type;
1996 spin_lock(&dq_data_lock);
1997 ii->dqi_bgrace = mi->dqi_bgrace;
1998 ii->dqi_igrace = mi->dqi_igrace;
1999 ii->dqi_flags = mi->dqi_flags & DQF_MASK;
2000 ii->dqi_valid = IIF_ALL;
2001 spin_unlock(&dq_data_lock);
Ingo Molnard3be9152006-03-23 03:00:29 -08002002 mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003 return 0;
2004}
2005
2006/* Generic routine for setting common part of quota file information */
2007int vfs_set_dqinfo(struct super_block *sb, int type, struct if_dqinfo *ii)
2008{
2009 struct mem_dqinfo *mi;
2010
Ingo Molnard3be9152006-03-23 03:00:29 -08002011 mutex_lock(&sb_dqopt(sb)->dqonoff_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012 if (!sb_has_quota_enabled(sb, type)) {
Ingo Molnard3be9152006-03-23 03:00:29 -08002013 mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014 return -ESRCH;
2015 }
2016 mi = sb_dqopt(sb)->info + type;
2017 spin_lock(&dq_data_lock);
2018 if (ii->dqi_valid & IIF_BGRACE)
2019 mi->dqi_bgrace = ii->dqi_bgrace;
2020 if (ii->dqi_valid & IIF_IGRACE)
2021 mi->dqi_igrace = ii->dqi_igrace;
2022 if (ii->dqi_valid & IIF_FLAGS)
2023 mi->dqi_flags = (mi->dqi_flags & ~DQF_MASK) | (ii->dqi_flags & DQF_MASK);
2024 spin_unlock(&dq_data_lock);
2025 mark_info_dirty(sb, type);
2026 /* Force write to disk */
2027 sb->dq_op->write_info(sb, type);
Ingo Molnard3be9152006-03-23 03:00:29 -08002028 mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029 return 0;
2030}
2031
2032struct quotactl_ops vfs_quotactl_ops = {
2033 .quota_on = vfs_quota_on,
2034 .quota_off = vfs_quota_off,
2035 .quota_sync = vfs_quota_sync,
2036 .get_info = vfs_get_dqinfo,
2037 .set_info = vfs_set_dqinfo,
2038 .get_dqblk = vfs_get_dqblk,
2039 .set_dqblk = vfs_set_dqblk
2040};
2041
2042static ctl_table fs_dqstats_table[] = {
2043 {
2044 .ctl_name = FS_DQ_LOOKUPS,
2045 .procname = "lookups",
2046 .data = &dqstats.lookups,
2047 .maxlen = sizeof(int),
2048 .mode = 0444,
2049 .proc_handler = &proc_dointvec,
2050 },
2051 {
2052 .ctl_name = FS_DQ_DROPS,
2053 .procname = "drops",
2054 .data = &dqstats.drops,
2055 .maxlen = sizeof(int),
2056 .mode = 0444,
2057 .proc_handler = &proc_dointvec,
2058 },
2059 {
2060 .ctl_name = FS_DQ_READS,
2061 .procname = "reads",
2062 .data = &dqstats.reads,
2063 .maxlen = sizeof(int),
2064 .mode = 0444,
2065 .proc_handler = &proc_dointvec,
2066 },
2067 {
2068 .ctl_name = FS_DQ_WRITES,
2069 .procname = "writes",
2070 .data = &dqstats.writes,
2071 .maxlen = sizeof(int),
2072 .mode = 0444,
2073 .proc_handler = &proc_dointvec,
2074 },
2075 {
2076 .ctl_name = FS_DQ_CACHE_HITS,
2077 .procname = "cache_hits",
2078 .data = &dqstats.cache_hits,
2079 .maxlen = sizeof(int),
2080 .mode = 0444,
2081 .proc_handler = &proc_dointvec,
2082 },
2083 {
2084 .ctl_name = FS_DQ_ALLOCATED,
2085 .procname = "allocated_dquots",
2086 .data = &dqstats.allocated_dquots,
2087 .maxlen = sizeof(int),
2088 .mode = 0444,
2089 .proc_handler = &proc_dointvec,
2090 },
2091 {
2092 .ctl_name = FS_DQ_FREE,
2093 .procname = "free_dquots",
2094 .data = &dqstats.free_dquots,
2095 .maxlen = sizeof(int),
2096 .mode = 0444,
2097 .proc_handler = &proc_dointvec,
2098 },
2099 {
2100 .ctl_name = FS_DQ_SYNCS,
2101 .procname = "syncs",
2102 .data = &dqstats.syncs,
2103 .maxlen = sizeof(int),
2104 .mode = 0444,
2105 .proc_handler = &proc_dointvec,
2106 },
Jan Kara8e893462007-10-16 23:29:31 -07002107#ifdef CONFIG_PRINT_QUOTA_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108 {
2109 .ctl_name = FS_DQ_WARNINGS,
2110 .procname = "warnings",
2111 .data = &flag_print_warnings,
2112 .maxlen = sizeof(int),
2113 .mode = 0644,
2114 .proc_handler = &proc_dointvec,
2115 },
Jan Kara8e893462007-10-16 23:29:31 -07002116#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117 { .ctl_name = 0 },
2118};
2119
2120static ctl_table fs_table[] = {
2121 {
2122 .ctl_name = FS_DQSTATS,
2123 .procname = "quota",
2124 .mode = 0555,
2125 .child = fs_dqstats_table,
2126 },
2127 { .ctl_name = 0 },
2128};
2129
2130static ctl_table sys_table[] = {
2131 {
2132 .ctl_name = CTL_FS,
2133 .procname = "fs",
2134 .mode = 0555,
2135 .child = fs_table,
2136 },
2137 { .ctl_name = 0 },
2138};
2139
2140static int __init dquot_init(void)
2141{
2142 int i;
2143 unsigned long nr_hash, order;
2144
2145 printk(KERN_NOTICE "VFS: Disk quotas %s\n", __DQUOT_VERSION__);
2146
Eric W. Biederman0b4d4142007-02-14 00:34:09 -08002147 register_sysctl_table(sys_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148
Paul Mundt20c2df82007-07-20 10:11:58 +09002149 dquot_cachep = kmem_cache_create("dquot",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150 sizeof(struct dquot), sizeof(unsigned long) * 4,
Paul Jacksonfffb60f2006-03-24 03:16:06 -08002151 (SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT|
2152 SLAB_MEM_SPREAD|SLAB_PANIC),
Paul Mundt20c2df82007-07-20 10:11:58 +09002153 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154
2155 order = 0;
2156 dquot_hash = (struct hlist_head *)__get_free_pages(GFP_ATOMIC, order);
2157 if (!dquot_hash)
2158 panic("Cannot create dquot hash table");
2159
2160 /* Find power-of-two hlist_heads which can fit into allocation */
2161 nr_hash = (1UL << order) * PAGE_SIZE / sizeof(struct hlist_head);
2162 dq_hash_bits = 0;
2163 do {
2164 dq_hash_bits++;
2165 } while (nr_hash >> dq_hash_bits);
2166 dq_hash_bits--;
2167
2168 nr_hash = 1UL << dq_hash_bits;
2169 dq_hash_mask = nr_hash - 1;
2170 for (i = 0; i < nr_hash; i++)
2171 INIT_HLIST_HEAD(dquot_hash + i);
2172
2173 printk("Dquot-cache hash table entries: %ld (order %ld, %ld bytes)\n",
2174 nr_hash, order, (PAGE_SIZE << order));
2175
Rusty Russell8e1f9362007-07-17 04:03:17 -07002176 register_shrinker(&dqcache_shrinker);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177
Jan Kara8e893462007-10-16 23:29:31 -07002178#ifdef CONFIG_QUOTA_NETLINK_INTERFACE
2179 if (genl_register_family(&quota_genl_family) != 0)
2180 printk(KERN_ERR "VFS: Failed to create quota netlink interface.\n");
2181#endif
2182
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183 return 0;
2184}
2185module_init(dquot_init);
2186
2187EXPORT_SYMBOL(register_quota_format);
2188EXPORT_SYMBOL(unregister_quota_format);
2189EXPORT_SYMBOL(dqstats);
2190EXPORT_SYMBOL(dq_data_lock);
2191EXPORT_SYMBOL(vfs_quota_on);
Al Viro77e69da2008-08-01 04:29:18 -04002192EXPORT_SYMBOL(vfs_quota_on_path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193EXPORT_SYMBOL(vfs_quota_on_mount);
2194EXPORT_SYMBOL(vfs_quota_off);
2195EXPORT_SYMBOL(vfs_quota_sync);
2196EXPORT_SYMBOL(vfs_get_dqinfo);
2197EXPORT_SYMBOL(vfs_set_dqinfo);
2198EXPORT_SYMBOL(vfs_get_dqblk);
2199EXPORT_SYMBOL(vfs_set_dqblk);
2200EXPORT_SYMBOL(dquot_commit);
2201EXPORT_SYMBOL(dquot_commit_info);
2202EXPORT_SYMBOL(dquot_acquire);
2203EXPORT_SYMBOL(dquot_release);
2204EXPORT_SYMBOL(dquot_mark_dquot_dirty);
2205EXPORT_SYMBOL(dquot_initialize);
2206EXPORT_SYMBOL(dquot_drop);
Jan Karab85f4b82008-07-25 01:46:50 -07002207EXPORT_SYMBOL(vfs_dq_drop);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208EXPORT_SYMBOL(dquot_alloc_space);
2209EXPORT_SYMBOL(dquot_alloc_inode);
2210EXPORT_SYMBOL(dquot_free_space);
2211EXPORT_SYMBOL(dquot_free_inode);
2212EXPORT_SYMBOL(dquot_transfer);
Jan Karab85f4b82008-07-25 01:46:50 -07002213EXPORT_SYMBOL(vfs_dq_transfer);
2214EXPORT_SYMBOL(vfs_dq_quota_on_remount);