]> Git Repo - linux.git/blame - fs/quota/dquot.c
userns: Convert struct dquot_warn
[linux.git] / fs / quota / dquot.c
CommitLineData
1da177e4
LT
1/*
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 *
1da177e4
LT
12 * Author: Marco van Wieringen <[email protected]>
13 *
14 * Fixes: Dmitry Gorodchanin <[email protected]>, 11 Feb 96
15 *
16 * Revised list management to avoid races
17 * -- Bill Hawes, <[email protected]>, 9/98
18 *
19 * Fixed races in dquot_transfer(), dqget() and dquot_alloc_...().
20 * As the consequence the locking was moved from dquot_decr_...(),
21 * dquot_incr_...() to calling functions.
22 * invalidate_dquots() now writes modified dquots.
23 * Serialized quota_off() and quota_on() for mount point.
24 * Fixed a few bugs in grow_dquots().
25 * Fixed deadlock in write_dquot() - we no longer account quotas on
26 * quota files
27 * remove_dquot_ref() moved to inode.c - it now traverses through inodes
28 * add_dquot_ref() restarts after blocking
29 * Added check for bogus uid and fixed check for group in quotactl.
30 * Jan Kara, <[email protected]>, sponsored by SuSE CR, 10-11/99
31 *
32 * Used struct list_head instead of own list struct
33 * Invalidation of referenced dquots is no longer possible
34 * Improved free_dquots list management
35 * Quota and i_blocks are now updated in one place to avoid races
36 * Warnings are now delayed so we won't block in critical section
37 * Write updated not to require dquot lock
38 * Jan Kara, <[email protected]>, 9/2000
39 *
40 * Added dynamic quota structure allocation
41 * Jan Kara <[email protected]> 12/2000
42 *
43 * Rewritten quota interface. Implemented new quota format and
44 * formats registering.
45 * Jan Kara, <[email protected]>, 2001,2002
46 *
47 * New SMP locking.
48 * Jan Kara, <[email protected]>, 10/2002
49 *
50 * Added journalled quota support, fix lock inversion problems
51 * Jan Kara, <[email protected]>, 2003,2004
52 *
53 * (C) Copyright 1994 - 1997 Marco van Wieringen
54 */
55
56#include <linux/errno.h>
57#include <linux/kernel.h>
58#include <linux/fs.h>
59#include <linux/mount.h>
60#include <linux/mm.h>
61#include <linux/time.h>
62#include <linux/types.h>
63#include <linux/string.h>
64#include <linux/fcntl.h>
65#include <linux/stat.h>
66#include <linux/tty.h>
67#include <linux/file.h>
68#include <linux/slab.h>
69#include <linux/sysctl.h>
1da177e4
LT
70#include <linux/init.h>
71#include <linux/module.h>
72#include <linux/proc_fs.h>
73#include <linux/security.h>
40401530 74#include <linux/sched.h>
1da177e4
LT
75#include <linux/kmod.h>
76#include <linux/namei.h>
16f7e0fe 77#include <linux/capability.h>
be586bab 78#include <linux/quotaops.h>
55fa6091 79#include "../internal.h" /* ugh */
1da177e4 80
f3da9310 81#include <linux/uaccess.h>
1da177e4 82
1da177e4 83/*
cc33412f 84 * There are three quota SMP locks. dq_list_lock protects all lists with quotas
dde95888 85 * and quota formats.
cc33412f
JK
86 * dq_data_lock protects data from dq_dqb and also mem_dqinfo structures and
87 * also guards consistency of dquot->dq_dqb with inode->i_blocks, i_bytes.
1da177e4 88 * i_blocks and i_bytes updates itself are guarded by i_lock acquired directly
cc33412f
JK
89 * in inode_add_bytes() and inode_sub_bytes(). dq_state_lock protects
90 * modifications of quota state (on quotaon and quotaoff) and readers who care
91 * about latest values take it as well.
1da177e4 92 *
cc33412f
JK
93 * The spinlock ordering is hence: dq_data_lock > dq_list_lock > i_lock,
94 * dq_list_lock > dq_state_lock
1da177e4
LT
95 *
96 * Note that some things (eg. sb pointer, type, id) doesn't change during
97 * the life of the dquot structure and so needn't to be protected by a lock
98 *
99 * Any operation working on dquots via inode pointers must hold dqptr_sem. If
100 * operation is just reading pointers from inode (or not using them at all) the
26245c94
JK
101 * read lock is enough. If pointers are altered function must hold write lock.
102 * Special care needs to be taken about S_NOQUOTA inode flag (marking that
103 * inode is a quota file). Functions adding pointers from inode to dquots have
104 * to check this flag under dqptr_sem and then (if S_NOQUOTA is not set) they
105 * have to do all pointer modifications before dropping dqptr_sem. This makes
106 * sure they cannot race with quotaon which first sets S_NOQUOTA flag and
107 * then drops all pointers to dquots from an inode.
1da177e4 108 *
d3be915f 109 * Each dquot has its dq_lock mutex. Locked dquots might not be referenced
1da177e4
LT
110 * from inodes (dquot_alloc_space() and such don't check the dq_lock).
111 * Currently dquot is locked only when it is being read to memory (or space for
112 * it is being allocated) on the first dqget() and when it is being released on
113 * the last dqput(). The allocation and release oparations are serialized by
114 * the dq_lock and by checking the use count in dquot_release(). Write
115 * operations on dquots don't hold dq_lock as they copy data under dq_data_lock
116 * spinlock to internal buffers before writing.
117 *
118 * Lock ordering (including related VFS locks) is the following:
a80b12c3 119 * dqonoff_mutex > i_mutex > journal_lock > dqptr_sem > dquot->dq_lock >
d3be915f 120 * dqio_mutex
a80b12c3 121 * dqonoff_mutex > i_mutex comes from dquot_quota_sync, dquot_enable, etc.
cc33412f
JK
122 * The lock ordering of dqptr_sem imposed by quota code is only dqonoff_sem >
123 * dqptr_sem. But filesystem has to count with the fact that functions such as
124 * dquot_alloc_space() acquire dqptr_sem and they usually have to be called
125 * from inside a transaction to keep filesystem consistency after a crash. Also
126 * filesystems usually want to do some IO on dquot from ->mark_dirty which is
127 * called with dqptr_sem held.
1da177e4
LT
128 */
129
c516610c
JK
130static __cacheline_aligned_in_smp DEFINE_SPINLOCK(dq_list_lock);
131static __cacheline_aligned_in_smp DEFINE_SPINLOCK(dq_state_lock);
132__cacheline_aligned_in_smp DEFINE_SPINLOCK(dq_data_lock);
08d0350c 133EXPORT_SYMBOL(dq_data_lock);
1da177e4 134
fb5ffb0e 135void __quota_error(struct super_block *sb, const char *func,
055adcbd 136 const char *fmt, ...)
fb5ffb0e 137{
fb5ffb0e 138 if (printk_ratelimit()) {
055adcbd
JP
139 va_list args;
140 struct va_format vaf;
141
fb5ffb0e 142 va_start(args, fmt);
055adcbd
JP
143
144 vaf.fmt = fmt;
145 vaf.va = &args;
146
147 printk(KERN_ERR "Quota error (device %s): %s: %pV\n",
148 sb->s_id, func, &vaf);
149
fb5ffb0e
JZ
150 va_end(args);
151 }
152}
153EXPORT_SYMBOL(__quota_error);
154
da8d1ba2 155#if defined(CONFIG_QUOTA_DEBUG) || defined(CONFIG_PRINT_QUOTA_WARNING)
1da177e4 156static char *quotatypes[] = INITQFNAMES;
da8d1ba2 157#endif
1da177e4
LT
158static struct quota_format_type *quota_formats; /* List of registered formats */
159static struct quota_module_name module_names[] = INIT_QUOTA_MODULE_NAMES;
160
161/* SLAB cache for dquot structures */
e18b890b 162static struct kmem_cache *dquot_cachep;
1da177e4
LT
163
164int register_quota_format(struct quota_format_type *fmt)
165{
166 spin_lock(&dq_list_lock);
167 fmt->qf_next = quota_formats;
168 quota_formats = fmt;
169 spin_unlock(&dq_list_lock);
170 return 0;
171}
08d0350c 172EXPORT_SYMBOL(register_quota_format);
1da177e4
LT
173
174void unregister_quota_format(struct quota_format_type *fmt)
175{
176 struct quota_format_type **actqf;
177
178 spin_lock(&dq_list_lock);
268157ba
JK
179 for (actqf = &quota_formats; *actqf && *actqf != fmt;
180 actqf = &(*actqf)->qf_next)
181 ;
1da177e4
LT
182 if (*actqf)
183 *actqf = (*actqf)->qf_next;
184 spin_unlock(&dq_list_lock);
185}
08d0350c 186EXPORT_SYMBOL(unregister_quota_format);
1da177e4
LT
187
188static struct quota_format_type *find_quota_format(int id)
189{
190 struct quota_format_type *actqf;
191
192 spin_lock(&dq_list_lock);
268157ba
JK
193 for (actqf = quota_formats; actqf && actqf->qf_fmt_id != id;
194 actqf = actqf->qf_next)
195 ;
1da177e4
LT
196 if (!actqf || !try_module_get(actqf->qf_owner)) {
197 int qm;
198
199 spin_unlock(&dq_list_lock);
200
268157ba
JK
201 for (qm = 0; module_names[qm].qm_fmt_id &&
202 module_names[qm].qm_fmt_id != id; qm++)
203 ;
204 if (!module_names[qm].qm_fmt_id ||
205 request_module(module_names[qm].qm_mod_name))
1da177e4
LT
206 return NULL;
207
208 spin_lock(&dq_list_lock);
268157ba
JK
209 for (actqf = quota_formats; actqf && actqf->qf_fmt_id != id;
210 actqf = actqf->qf_next)
211 ;
1da177e4
LT
212 if (actqf && !try_module_get(actqf->qf_owner))
213 actqf = NULL;
214 }
215 spin_unlock(&dq_list_lock);
216 return actqf;
217}
218
219static void put_quota_format(struct quota_format_type *fmt)
220{
221 module_put(fmt->qf_owner);
222}
223
224/*
225 * Dquot List Management:
226 * The quota code uses three lists for dquot management: the inuse_list,
227 * free_dquots, and dquot_hash[] array. A single dquot structure may be
228 * on all three lists, depending on its current state.
229 *
230 * All dquots are placed to the end of inuse_list when first created, and this
231 * list is used for invalidate operation, which must look at every dquot.
232 *
233 * Unused dquots (dq_count == 0) are added to the free_dquots list when freed,
234 * and this list is searched whenever we need an available dquot. Dquots are
235 * removed from the list as soon as they are used again, and
236 * dqstats.free_dquots gives the number of dquots on the list. When
237 * dquot is invalidated it's completely released from memory.
238 *
239 * Dquots with a specific identity (device, type and id) are placed on
240 * one of the dquot_hash[] hash chains. The provides an efficient search
241 * mechanism to locate a specific dquot.
242 */
243
244static LIST_HEAD(inuse_list);
245static LIST_HEAD(free_dquots);
246static unsigned int dq_hash_bits, dq_hash_mask;
247static struct hlist_head *dquot_hash;
248
249struct dqstats dqstats;
08d0350c 250EXPORT_SYMBOL(dqstats);
1da177e4 251
0a5a9c72 252static qsize_t inode_get_rsv_space(struct inode *inode);
871a2931 253static void __dquot_initialize(struct inode *inode, int type);
0a5a9c72 254
1da177e4
LT
255static inline unsigned int
256hashfn(const struct super_block *sb, unsigned int id, int type)
257{
258 unsigned long tmp;
259
260 tmp = (((unsigned long)sb>>L1_CACHE_SHIFT) ^ id) * (MAXQUOTAS - type);
261 return (tmp + (tmp >> dq_hash_bits)) & dq_hash_mask;
262}
263
264/*
265 * Following list functions expect dq_list_lock to be held
266 */
267static inline void insert_dquot_hash(struct dquot *dquot)
268{
268157ba 269 struct hlist_head *head;
4c376dca 270 head = dquot_hash + hashfn(dquot->dq_sb, from_kqid(&init_user_ns, dquot->dq_id), dquot->dq_id.type);
1da177e4
LT
271 hlist_add_head(&dquot->dq_hash, head);
272}
273
274static inline void remove_dquot_hash(struct dquot *dquot)
275{
276 hlist_del_init(&dquot->dq_hash);
277}
278
7a2435d8
JK
279static struct dquot *find_dquot(unsigned int hashent, struct super_block *sb,
280 unsigned int id, int type)
1da177e4 281{
4c376dca 282 struct kqid qid = make_kqid(&init_user_ns, type, id);
1da177e4
LT
283 struct hlist_node *node;
284 struct dquot *dquot;
285
286 hlist_for_each (node, dquot_hash+hashent) {
287 dquot = hlist_entry(node, struct dquot, dq_hash);
4c376dca 288 if (dquot->dq_sb == sb && qid_eq(dquot->dq_id, qid))
1da177e4
LT
289 return dquot;
290 }
dd6f3c6d 291 return NULL;
1da177e4
LT
292}
293
294/* Add a dquot to the tail of the free list */
295static inline void put_dquot_last(struct dquot *dquot)
296{
8e13059a 297 list_add_tail(&dquot->dq_free, &free_dquots);
dde95888 298 dqstats_inc(DQST_FREE_DQUOTS);
1da177e4
LT
299}
300
301static inline void remove_free_dquot(struct dquot *dquot)
302{
303 if (list_empty(&dquot->dq_free))
304 return;
305 list_del_init(&dquot->dq_free);
dde95888 306 dqstats_dec(DQST_FREE_DQUOTS);
1da177e4
LT
307}
308
309static inline void put_inuse(struct dquot *dquot)
310{
311 /* We add to the back of inuse list so we don't have to restart
312 * when traversing this list and we block */
8e13059a 313 list_add_tail(&dquot->dq_inuse, &inuse_list);
dde95888 314 dqstats_inc(DQST_ALLOC_DQUOTS);
1da177e4
LT
315}
316
317static inline void remove_inuse(struct dquot *dquot)
318{
dde95888 319 dqstats_dec(DQST_ALLOC_DQUOTS);
1da177e4
LT
320 list_del(&dquot->dq_inuse);
321}
322/*
323 * End of list functions needing dq_list_lock
324 */
325
326static void wait_on_dquot(struct dquot *dquot)
327{
d3be915f
IM
328 mutex_lock(&dquot->dq_lock);
329 mutex_unlock(&dquot->dq_lock);
1da177e4
LT
330}
331
03f6e92b
JK
332static inline int dquot_dirty(struct dquot *dquot)
333{
334 return test_bit(DQ_MOD_B, &dquot->dq_flags);
335}
336
337static inline int mark_dquot_dirty(struct dquot *dquot)
338{
339 return dquot->dq_sb->dq_op->mark_dirty(dquot);
340}
1da177e4 341
eabf290d 342/* Mark dquot dirty in atomic manner, and return it's old dirty flag state */
1da177e4
LT
343int dquot_mark_dquot_dirty(struct dquot *dquot)
344{
eabf290d
DM
345 int ret = 1;
346
347 /* If quota is dirty already, we don't have to acquire dq_list_lock */
348 if (test_bit(DQ_MOD_B, &dquot->dq_flags))
349 return 1;
350
1da177e4 351 spin_lock(&dq_list_lock);
eabf290d 352 if (!test_and_set_bit(DQ_MOD_B, &dquot->dq_flags)) {
1da177e4 353 list_add(&dquot->dq_dirty, &sb_dqopt(dquot->dq_sb)->
4c376dca 354 info[dquot->dq_id.type].dqi_dirty_list);
eabf290d
DM
355 ret = 0;
356 }
1da177e4 357 spin_unlock(&dq_list_lock);
eabf290d 358 return ret;
1da177e4 359}
08d0350c 360EXPORT_SYMBOL(dquot_mark_dquot_dirty);
1da177e4 361
dc52dd3a
DM
362/* Dirtify all the dquots - this can block when journalling */
363static inline int mark_all_dquot_dirty(struct dquot * const *dquot)
364{
365 int ret, err, cnt;
366
367 ret = err = 0;
368 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
369 if (dquot[cnt])
370 /* Even in case of error we have to continue */
371 ret = mark_dquot_dirty(dquot[cnt]);
372 if (!err)
373 err = ret;
374 }
375 return err;
376}
377
378static inline void dqput_all(struct dquot **dquot)
379{
380 unsigned int cnt;
381
382 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
383 dqput(dquot[cnt]);
384}
385
1da177e4
LT
386/* This function needs dq_list_lock */
387static inline int clear_dquot_dirty(struct dquot *dquot)
388{
389 if (!test_and_clear_bit(DQ_MOD_B, &dquot->dq_flags))
390 return 0;
391 list_del_init(&dquot->dq_dirty);
392 return 1;
393}
394
395void mark_info_dirty(struct super_block *sb, int type)
396{
397 set_bit(DQF_INFO_DIRTY_B, &sb_dqopt(sb)->info[type].dqi_flags);
398}
399EXPORT_SYMBOL(mark_info_dirty);
400
401/*
402 * Read dquot from disk and alloc space for it
403 */
404
405int dquot_acquire(struct dquot *dquot)
406{
407 int ret = 0, ret2 = 0;
408 struct quota_info *dqopt = sb_dqopt(dquot->dq_sb);
409
d3be915f
IM
410 mutex_lock(&dquot->dq_lock);
411 mutex_lock(&dqopt->dqio_mutex);
1da177e4 412 if (!test_bit(DQ_READ_B, &dquot->dq_flags))
4c376dca 413 ret = dqopt->ops[dquot->dq_id.type]->read_dqblk(dquot);
1da177e4
LT
414 if (ret < 0)
415 goto out_iolock;
416 set_bit(DQ_READ_B, &dquot->dq_flags);
417 /* Instantiate dquot if needed */
418 if (!test_bit(DQ_ACTIVE_B, &dquot->dq_flags) && !dquot->dq_off) {
4c376dca 419 ret = dqopt->ops[dquot->dq_id.type]->commit_dqblk(dquot);
1da177e4 420 /* Write the info if needed */
4c376dca
EB
421 if (info_dirty(&dqopt->info[dquot->dq_id.type])) {
422 ret2 = dqopt->ops[dquot->dq_id.type]->write_file_info(
423 dquot->dq_sb, dquot->dq_id.type);
268157ba 424 }
1da177e4
LT
425 if (ret < 0)
426 goto out_iolock;
427 if (ret2 < 0) {
428 ret = ret2;
429 goto out_iolock;
430 }
431 }
432 set_bit(DQ_ACTIVE_B, &dquot->dq_flags);
433out_iolock:
d3be915f
IM
434 mutex_unlock(&dqopt->dqio_mutex);
435 mutex_unlock(&dquot->dq_lock);
1da177e4
LT
436 return ret;
437}
08d0350c 438EXPORT_SYMBOL(dquot_acquire);
1da177e4
LT
439
440/*
441 * Write dquot to disk
442 */
443int dquot_commit(struct dquot *dquot)
444{
b03f2456 445 int ret = 0;
1da177e4
LT
446 struct quota_info *dqopt = sb_dqopt(dquot->dq_sb);
447
d3be915f 448 mutex_lock(&dqopt->dqio_mutex);
1da177e4
LT
449 spin_lock(&dq_list_lock);
450 if (!clear_dquot_dirty(dquot)) {
451 spin_unlock(&dq_list_lock);
452 goto out_sem;
453 }
454 spin_unlock(&dq_list_lock);
455 /* Inactive dquot can be only if there was error during read/init
456 * => we have better not writing it */
b03f2456 457 if (test_bit(DQ_ACTIVE_B, &dquot->dq_flags))
4c376dca 458 ret = dqopt->ops[dquot->dq_id.type]->commit_dqblk(dquot);
b03f2456
JK
459 else
460 ret = -EIO;
1da177e4 461out_sem:
d3be915f 462 mutex_unlock(&dqopt->dqio_mutex);
1da177e4
LT
463 return ret;
464}
08d0350c 465EXPORT_SYMBOL(dquot_commit);
1da177e4
LT
466
467/*
468 * Release dquot
469 */
470int dquot_release(struct dquot *dquot)
471{
472 int ret = 0, ret2 = 0;
473 struct quota_info *dqopt = sb_dqopt(dquot->dq_sb);
474
d3be915f 475 mutex_lock(&dquot->dq_lock);
1da177e4
LT
476 /* Check whether we are not racing with some other dqget() */
477 if (atomic_read(&dquot->dq_count) > 1)
478 goto out_dqlock;
d3be915f 479 mutex_lock(&dqopt->dqio_mutex);
4c376dca
EB
480 if (dqopt->ops[dquot->dq_id.type]->release_dqblk) {
481 ret = dqopt->ops[dquot->dq_id.type]->release_dqblk(dquot);
1da177e4 482 /* Write the info */
4c376dca
EB
483 if (info_dirty(&dqopt->info[dquot->dq_id.type])) {
484 ret2 = dqopt->ops[dquot->dq_id.type]->write_file_info(
485 dquot->dq_sb, dquot->dq_id.type);
268157ba 486 }
1da177e4
LT
487 if (ret >= 0)
488 ret = ret2;
489 }
490 clear_bit(DQ_ACTIVE_B, &dquot->dq_flags);
d3be915f 491 mutex_unlock(&dqopt->dqio_mutex);
1da177e4 492out_dqlock:
d3be915f 493 mutex_unlock(&dquot->dq_lock);
1da177e4
LT
494 return ret;
495}
08d0350c 496EXPORT_SYMBOL(dquot_release);
1da177e4 497
7d9056ba 498void dquot_destroy(struct dquot *dquot)
74f783af
JK
499{
500 kmem_cache_free(dquot_cachep, dquot);
501}
7d9056ba 502EXPORT_SYMBOL(dquot_destroy);
74f783af
JK
503
504static inline void do_destroy_dquot(struct dquot *dquot)
505{
506 dquot->dq_sb->dq_op->destroy_dquot(dquot);
507}
508
1da177e4
LT
509/* Invalidate all dquots on the list. Note that this function is called after
510 * quota is disabled and pointers from inodes removed so there cannot be new
6362e4d4
JK
511 * quota users. There can still be some users of quotas due to inodes being
512 * just deleted or pruned by prune_icache() (those are not attached to any
cc33412f 513 * list) or parallel quotactl call. We have to wait for such users.
6362e4d4 514 */
1da177e4
LT
515static void invalidate_dquots(struct super_block *sb, int type)
516{
c33ed271 517 struct dquot *dquot, *tmp;
1da177e4 518
6362e4d4 519restart:
1da177e4 520 spin_lock(&dq_list_lock);
c33ed271 521 list_for_each_entry_safe(dquot, tmp, &inuse_list, dq_inuse) {
1da177e4
LT
522 if (dquot->dq_sb != sb)
523 continue;
4c376dca 524 if (dquot->dq_id.type != type)
1da177e4 525 continue;
6362e4d4
JK
526 /* Wait for dquot users */
527 if (atomic_read(&dquot->dq_count)) {
528 DEFINE_WAIT(wait);
529
530 atomic_inc(&dquot->dq_count);
531 prepare_to_wait(&dquot->dq_wait_unused, &wait,
532 TASK_UNINTERRUPTIBLE);
533 spin_unlock(&dq_list_lock);
534 /* Once dqput() wakes us up, we know it's time to free
535 * the dquot.
536 * IMPORTANT: we rely on the fact that there is always
537 * at most one process waiting for dquot to free.
538 * Otherwise dq_count would be > 1 and we would never
539 * wake up.
540 */
541 if (atomic_read(&dquot->dq_count) > 1)
542 schedule();
543 finish_wait(&dquot->dq_wait_unused, &wait);
544 dqput(dquot);
545 /* At this moment dquot() need not exist (it could be
546 * reclaimed by prune_dqcache(). Hence we must
547 * restart. */
548 goto restart;
549 }
550 /*
551 * Quota now has no users and it has been written on last
552 * dqput()
553 */
1da177e4
LT
554 remove_dquot_hash(dquot);
555 remove_free_dquot(dquot);
556 remove_inuse(dquot);
74f783af 557 do_destroy_dquot(dquot);
1da177e4
LT
558 }
559 spin_unlock(&dq_list_lock);
560}
561
12c77527
JK
562/* Call callback for every active dquot on given filesystem */
563int dquot_scan_active(struct super_block *sb,
564 int (*fn)(struct dquot *dquot, unsigned long priv),
565 unsigned long priv)
566{
567 struct dquot *dquot, *old_dquot = NULL;
568 int ret = 0;
569
570 mutex_lock(&sb_dqopt(sb)->dqonoff_mutex);
571 spin_lock(&dq_list_lock);
572 list_for_each_entry(dquot, &inuse_list, dq_inuse) {
573 if (!test_bit(DQ_ACTIVE_B, &dquot->dq_flags))
574 continue;
575 if (dquot->dq_sb != sb)
576 continue;
577 /* Now we have active dquot so we can just increase use count */
578 atomic_inc(&dquot->dq_count);
12c77527 579 spin_unlock(&dq_list_lock);
dde95888 580 dqstats_inc(DQST_LOOKUPS);
12c77527
JK
581 dqput(old_dquot);
582 old_dquot = dquot;
583 ret = fn(dquot, priv);
584 if (ret < 0)
585 goto out;
586 spin_lock(&dq_list_lock);
587 /* We are safe to continue now because our dquot could not
588 * be moved out of the inuse list while we hold the reference */
589 }
590 spin_unlock(&dq_list_lock);
591out:
592 dqput(old_dquot);
593 mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
594 return ret;
595}
08d0350c 596EXPORT_SYMBOL(dquot_scan_active);
12c77527 597
ceed1723
JK
598/* Write all dquot structures to quota files */
599int dquot_writeback_dquots(struct super_block *sb, int type)
1da177e4
LT
600{
601 struct list_head *dirty;
602 struct dquot *dquot;
603 struct quota_info *dqopt = sb_dqopt(sb);
604 int cnt;
ceed1723 605 int err, ret = 0;
1da177e4 606
d3be915f 607 mutex_lock(&dqopt->dqonoff_mutex);
1da177e4
LT
608 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
609 if (type != -1 && cnt != type)
610 continue;
f55abc0f 611 if (!sb_has_quota_active(sb, cnt))
1da177e4
LT
612 continue;
613 spin_lock(&dq_list_lock);
614 dirty = &dqopt->info[cnt].dqi_dirty_list;
615 while (!list_empty(dirty)) {
268157ba
JK
616 dquot = list_first_entry(dirty, struct dquot,
617 dq_dirty);
1da177e4
LT
618 /* Dirty and inactive can be only bad dquot... */
619 if (!test_bit(DQ_ACTIVE_B, &dquot->dq_flags)) {
620 clear_dquot_dirty(dquot);
621 continue;
622 }
623 /* Now we have active dquot from which someone is
624 * holding reference so we can safely just increase
625 * use count */
626 atomic_inc(&dquot->dq_count);
1da177e4 627 spin_unlock(&dq_list_lock);
dde95888 628 dqstats_inc(DQST_LOOKUPS);
ceed1723
JK
629 err = sb->dq_op->write_dquot(dquot);
630 if (!ret && err)
631 err = ret;
1da177e4
LT
632 dqput(dquot);
633 spin_lock(&dq_list_lock);
634 }
635 spin_unlock(&dq_list_lock);
636 }
637
638 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
f55abc0f
JK
639 if ((cnt == type || type == -1) && sb_has_quota_active(sb, cnt)
640 && info_dirty(&dqopt->info[cnt]))
1da177e4 641 sb->dq_op->write_info(sb, cnt);
dde95888 642 dqstats_inc(DQST_SYNCS);
d3be915f 643 mutex_unlock(&dqopt->dqonoff_mutex);
1da177e4 644
ceed1723
JK
645 return ret;
646}
647EXPORT_SYMBOL(dquot_writeback_dquots);
648
649/* Write all dquot structures to disk and make them visible from userspace */
650int dquot_quota_sync(struct super_block *sb, int type)
651{
652 struct quota_info *dqopt = sb_dqopt(sb);
653 int cnt;
654 int ret;
655
656 ret = dquot_writeback_dquots(sb, type);
657 if (ret)
658 return ret;
659 if (dqopt->flags & DQUOT_QUOTA_SYS_FILE)
5fb324ad
CH
660 return 0;
661
662 /* This is not very clever (and fast) but currently I don't know about
663 * any other simple way of getting quota data to disk and we must get
664 * them there for userspace to be visible... */
665 if (sb->s_op->sync_fs)
666 sb->s_op->sync_fs(sb, 1);
667 sync_blockdev(sb->s_bdev);
668
669 /*
670 * Now when everything is written we can discard the pagecache so
671 * that userspace sees the changes.
672 */
f9ef1784 673 mutex_lock(&dqopt->dqonoff_mutex);
5fb324ad
CH
674 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
675 if (type != -1 && cnt != type)
676 continue;
677 if (!sb_has_quota_active(sb, cnt))
678 continue;
a80b12c3 679 mutex_lock(&dqopt->files[cnt]->i_mutex);
f9ef1784
JK
680 truncate_inode_pages(&dqopt->files[cnt]->i_data, 0);
681 mutex_unlock(&dqopt->files[cnt]->i_mutex);
5fb324ad 682 }
f9ef1784 683 mutex_unlock(&dqopt->dqonoff_mutex);
5fb324ad 684
1da177e4
LT
685 return 0;
686}
287a8095 687EXPORT_SYMBOL(dquot_quota_sync);
1da177e4
LT
688
689/* Free unused dquots from cache */
690static void prune_dqcache(int count)
691{
692 struct list_head *head;
693 struct dquot *dquot;
694
695 head = free_dquots.prev;
696 while (head != &free_dquots && count) {
697 dquot = list_entry(head, struct dquot, dq_free);
698 remove_dquot_hash(dquot);
699 remove_free_dquot(dquot);
700 remove_inuse(dquot);
74f783af 701 do_destroy_dquot(dquot);
1da177e4
LT
702 count--;
703 head = free_dquots.prev;
704 }
705}
706
707/*
708 * This is called from kswapd when we think we need some
709 * more memory
710 */
1495f230
YH
711static int shrink_dqcache_memory(struct shrinker *shrink,
712 struct shrink_control *sc)
1da177e4 713{
1495f230
YH
714 int nr = sc->nr_to_scan;
715
1da177e4
LT
716 if (nr) {
717 spin_lock(&dq_list_lock);
718 prune_dqcache(nr);
719 spin_unlock(&dq_list_lock);
720 }
f32764bd
DM
721 return ((unsigned)
722 percpu_counter_read_positive(&dqstats.counter[DQST_FREE_DQUOTS])
723 /100) * sysctl_vfs_cache_pressure;
1da177e4
LT
724}
725
8e1f936b
RR
726static struct shrinker dqcache_shrinker = {
727 .shrink = shrink_dqcache_memory,
728 .seeks = DEFAULT_SEEKS,
729};
730
1da177e4
LT
731/*
732 * Put reference to dquot
733 * NOTE: If you change this function please check whether dqput_blocks() works right...
1da177e4 734 */
3d9ea253 735void dqput(struct dquot *dquot)
1da177e4 736{
b48d3805
JK
737 int ret;
738
1da177e4
LT
739 if (!dquot)
740 return;
62af9b52 741#ifdef CONFIG_QUOTA_DEBUG
1da177e4 742 if (!atomic_read(&dquot->dq_count)) {
fb5ffb0e 743 quota_error(dquot->dq_sb, "trying to free free dquot of %s %d",
4c376dca
EB
744 quotatypes[dquot->dq_id.type],
745 from_kqid(&init_user_ns, dquot->dq_id));
1da177e4
LT
746 BUG();
747 }
748#endif
dde95888 749 dqstats_inc(DQST_DROPS);
1da177e4
LT
750we_slept:
751 spin_lock(&dq_list_lock);
752 if (atomic_read(&dquot->dq_count) > 1) {
753 /* We have more than one user... nothing to do */
754 atomic_dec(&dquot->dq_count);
6362e4d4 755 /* Releasing dquot during quotaoff phase? */
4c376dca 756 if (!sb_has_quota_active(dquot->dq_sb, dquot->dq_id.type) &&
6362e4d4
JK
757 atomic_read(&dquot->dq_count) == 1)
758 wake_up(&dquot->dq_wait_unused);
1da177e4
LT
759 spin_unlock(&dq_list_lock);
760 return;
761 }
762 /* Need to release dquot? */
763 if (test_bit(DQ_ACTIVE_B, &dquot->dq_flags) && dquot_dirty(dquot)) {
764 spin_unlock(&dq_list_lock);
765 /* Commit dquot before releasing */
b48d3805
JK
766 ret = dquot->dq_sb->dq_op->write_dquot(dquot);
767 if (ret < 0) {
fb5ffb0e
JZ
768 quota_error(dquot->dq_sb, "Can't write quota structure"
769 " (error %d). Quota may get out of sync!",
770 ret);
b48d3805
JK
771 /*
772 * We clear dirty bit anyway, so that we avoid
773 * infinite loop here
774 */
775 spin_lock(&dq_list_lock);
776 clear_dquot_dirty(dquot);
777 spin_unlock(&dq_list_lock);
778 }
1da177e4
LT
779 goto we_slept;
780 }
781 /* Clear flag in case dquot was inactive (something bad happened) */
782 clear_dquot_dirty(dquot);
783 if (test_bit(DQ_ACTIVE_B, &dquot->dq_flags)) {
784 spin_unlock(&dq_list_lock);
785 dquot->dq_sb->dq_op->release_dquot(dquot);
786 goto we_slept;
787 }
788 atomic_dec(&dquot->dq_count);
62af9b52 789#ifdef CONFIG_QUOTA_DEBUG
1da177e4 790 /* sanity check */
8abf6a47 791 BUG_ON(!list_empty(&dquot->dq_free));
1da177e4
LT
792#endif
793 put_dquot_last(dquot);
794 spin_unlock(&dq_list_lock);
795}
08d0350c 796EXPORT_SYMBOL(dqput);
1da177e4 797
7d9056ba 798struct dquot *dquot_alloc(struct super_block *sb, int type)
74f783af
JK
799{
800 return kmem_cache_zalloc(dquot_cachep, GFP_NOFS);
801}
7d9056ba 802EXPORT_SYMBOL(dquot_alloc);
74f783af 803
1da177e4
LT
804static struct dquot *get_empty_dquot(struct super_block *sb, int type)
805{
806 struct dquot *dquot;
807
74f783af 808 dquot = sb->dq_op->alloc_dquot(sb, type);
1da177e4 809 if(!dquot)
dd6f3c6d 810 return NULL;
1da177e4 811
d3be915f 812 mutex_init(&dquot->dq_lock);
1da177e4
LT
813 INIT_LIST_HEAD(&dquot->dq_free);
814 INIT_LIST_HEAD(&dquot->dq_inuse);
815 INIT_HLIST_NODE(&dquot->dq_hash);
816 INIT_LIST_HEAD(&dquot->dq_dirty);
6362e4d4 817 init_waitqueue_head(&dquot->dq_wait_unused);
1da177e4 818 dquot->dq_sb = sb;
4c376dca 819 dquot->dq_id.type = type;
1da177e4
LT
820 atomic_set(&dquot->dq_count, 1);
821
822 return dquot;
823}
824
825/*
826 * Get reference to dquot
cc33412f
JK
827 *
828 * Locking is slightly tricky here. We are guarded from parallel quotaoff()
829 * destroying our dquot by:
830 * a) checking for quota flags under dq_list_lock and
831 * b) getting a reference to dquot before we release dq_list_lock
1da177e4 832 */
aca645a6 833struct dquot *dqget(struct super_block *sb, struct kqid qid)
1da177e4 834{
aca645a6
EB
835 unsigned int type = qid.type;
836 unsigned int id = from_kqid(&init_user_ns, qid);
1da177e4 837 unsigned int hashent = hashfn(sb, id, type);
dd6f3c6d 838 struct dquot *dquot = NULL, *empty = NULL;
1da177e4 839
f55abc0f 840 if (!sb_has_quota_active(sb, type))
dd6f3c6d 841 return NULL;
1da177e4
LT
842we_slept:
843 spin_lock(&dq_list_lock);
cc33412f
JK
844 spin_lock(&dq_state_lock);
845 if (!sb_has_quota_active(sb, type)) {
846 spin_unlock(&dq_state_lock);
847 spin_unlock(&dq_list_lock);
848 goto out;
849 }
850 spin_unlock(&dq_state_lock);
851
dd6f3c6d
JK
852 dquot = find_dquot(hashent, sb, id, type);
853 if (!dquot) {
854 if (!empty) {
1da177e4 855 spin_unlock(&dq_list_lock);
dd6f3c6d
JK
856 empty = get_empty_dquot(sb, type);
857 if (!empty)
1da177e4
LT
858 schedule(); /* Try to wait for a moment... */
859 goto we_slept;
860 }
861 dquot = empty;
dd6f3c6d 862 empty = NULL;
4c376dca 863 dquot->dq_id = qid;
1da177e4
LT
864 /* all dquots go on the inuse_list */
865 put_inuse(dquot);
866 /* hash it first so it can be found */
867 insert_dquot_hash(dquot);
1da177e4 868 spin_unlock(&dq_list_lock);
dde95888 869 dqstats_inc(DQST_LOOKUPS);
1da177e4
LT
870 } else {
871 if (!atomic_read(&dquot->dq_count))
872 remove_free_dquot(dquot);
873 atomic_inc(&dquot->dq_count);
1da177e4 874 spin_unlock(&dq_list_lock);
dde95888
DM
875 dqstats_inc(DQST_CACHE_HITS);
876 dqstats_inc(DQST_LOOKUPS);
1da177e4 877 }
268157ba
JK
878 /* Wait for dq_lock - after this we know that either dquot_release() is
879 * already finished or it will be canceled due to dq_count > 1 test */
1da177e4 880 wait_on_dquot(dquot);
268157ba
JK
881 /* Read the dquot / allocate space in quota file */
882 if (!test_bit(DQ_ACTIVE_B, &dquot->dq_flags) &&
883 sb->dq_op->acquire_dquot(dquot) < 0) {
1da177e4 884 dqput(dquot);
dd6f3c6d 885 dquot = NULL;
cc33412f 886 goto out;
1da177e4 887 }
62af9b52 888#ifdef CONFIG_QUOTA_DEBUG
8abf6a47 889 BUG_ON(!dquot->dq_sb); /* Has somebody invalidated entry under us? */
1da177e4 890#endif
cc33412f
JK
891out:
892 if (empty)
893 do_destroy_dquot(empty);
1da177e4
LT
894
895 return dquot;
896}
08d0350c 897EXPORT_SYMBOL(dqget);
1da177e4
LT
898
899static int dqinit_needed(struct inode *inode, int type)
900{
901 int cnt;
902
903 if (IS_NOQUOTA(inode))
904 return 0;
905 if (type != -1)
dd6f3c6d 906 return !inode->i_dquot[type];
1da177e4 907 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
dd6f3c6d 908 if (!inode->i_dquot[cnt])
1da177e4
LT
909 return 1;
910 return 0;
911}
912
d3be915f 913/* This routine is guarded by dqonoff_mutex mutex */
1da177e4
LT
914static void add_dquot_ref(struct super_block *sb, int type)
915{
941d2380 916 struct inode *inode, *old_inode = NULL;
62af9b52 917#ifdef CONFIG_QUOTA_DEBUG
0a5a9c72 918 int reserved = 0;
4c5e6c0e 919#endif
1da177e4 920
55fa6091 921 spin_lock(&inode_sb_list_lock);
d003fb70 922 list_for_each_entry(inode, &sb->s_inodes, i_sb_list) {
250df6ed
DC
923 spin_lock(&inode->i_lock);
924 if ((inode->i_state & (I_FREEING|I_WILL_FREE|I_NEW)) ||
925 !atomic_read(&inode->i_writecount) ||
926 !dqinit_needed(inode, type)) {
927 spin_unlock(&inode->i_lock);
aabb8fdb 928 continue;
250df6ed 929 }
d003fb70 930 __iget(inode);
250df6ed 931 spin_unlock(&inode->i_lock);
55fa6091 932 spin_unlock(&inode_sb_list_lock);
d003fb70 933
d7e97117
JK
934#ifdef CONFIG_QUOTA_DEBUG
935 if (unlikely(inode_get_rsv_space(inode) > 0))
936 reserved = 1;
937#endif
941d2380 938 iput(old_inode);
871a2931 939 __dquot_initialize(inode, type);
55fa6091
DC
940
941 /*
942 * We hold a reference to 'inode' so it couldn't have been
943 * removed from s_inodes list while we dropped the
944 * inode_sb_list_lock We cannot iput the inode now as we can be
945 * holding the last reference and we cannot iput it under
946 * inode_sb_list_lock. So we keep the reference and iput it
947 * later.
948 */
941d2380 949 old_inode = inode;
55fa6091 950 spin_lock(&inode_sb_list_lock);
1da177e4 951 }
55fa6091 952 spin_unlock(&inode_sb_list_lock);
941d2380 953 iput(old_inode);
0a5a9c72 954
62af9b52 955#ifdef CONFIG_QUOTA_DEBUG
0a5a9c72 956 if (reserved) {
fb5ffb0e
JZ
957 quota_error(sb, "Writes happened before quota was turned on "
958 "thus quota information is probably inconsistent. "
959 "Please run quotacheck(8)");
0a5a9c72 960 }
4c5e6c0e 961#endif
1da177e4
LT
962}
963
268157ba
JK
964/*
965 * Return 0 if dqput() won't block.
966 * (note that 1 doesn't necessarily mean blocking)
967 */
1da177e4
LT
968static inline int dqput_blocks(struct dquot *dquot)
969{
970 if (atomic_read(&dquot->dq_count) <= 1)
971 return 1;
972 return 0;
973}
974
268157ba
JK
975/*
976 * Remove references to dquots from inode and add dquot to list for freeing
25985edc 977 * if we have the last reference to dquot
268157ba
JK
978 * We can't race with anybody because we hold dqptr_sem for writing...
979 */
e5f00f42
AB
980static int remove_inode_dquot_ref(struct inode *inode, int type,
981 struct list_head *tofree_head)
1da177e4
LT
982{
983 struct dquot *dquot = inode->i_dquot[type];
984
dd6f3c6d
JK
985 inode->i_dquot[type] = NULL;
986 if (dquot) {
1da177e4 987 if (dqput_blocks(dquot)) {
62af9b52 988#ifdef CONFIG_QUOTA_DEBUG
1da177e4 989 if (atomic_read(&dquot->dq_count) != 1)
fb5ffb0e
JZ
990 quota_error(inode->i_sb, "Adding dquot with "
991 "dq_count %d to dispose list",
992 atomic_read(&dquot->dq_count));
1da177e4
LT
993#endif
994 spin_lock(&dq_list_lock);
268157ba
JK
995 /* As dquot must have currently users it can't be on
996 * the free list... */
997 list_add(&dquot->dq_free, tofree_head);
1da177e4
LT
998 spin_unlock(&dq_list_lock);
999 return 1;
1000 }
1001 else
1002 dqput(dquot); /* We have guaranteed we won't block */
1003 }
1004 return 0;
1005}
1006
268157ba
JK
1007/*
1008 * Free list of dquots
1009 * Dquots are removed from inodes and no new references can be got so we are
1010 * the only ones holding reference
1011 */
1da177e4
LT
1012static void put_dquot_list(struct list_head *tofree_head)
1013{
1014 struct list_head *act_head;
1015 struct dquot *dquot;
1016
1017 act_head = tofree_head->next;
1da177e4
LT
1018 while (act_head != tofree_head) {
1019 dquot = list_entry(act_head, struct dquot, dq_free);
1020 act_head = act_head->next;
268157ba
JK
1021 /* Remove dquot from the list so we won't have problems... */
1022 list_del_init(&dquot->dq_free);
1da177e4
LT
1023 dqput(dquot);
1024 }
1025}
1026
fb58b731
CH
1027static void remove_dquot_ref(struct super_block *sb, int type,
1028 struct list_head *tofree_head)
1029{
1030 struct inode *inode;
7af9cce8 1031 int reserved = 0;
fb58b731 1032
55fa6091 1033 spin_lock(&inode_sb_list_lock);
fb58b731 1034 list_for_each_entry(inode, &sb->s_inodes, i_sb_list) {
aabb8fdb
NP
1035 /*
1036 * We have to scan also I_NEW inodes because they can already
1037 * have quota pointer initialized. Luckily, we need to touch
1038 * only quota pointers and these have separate locking
1039 * (dqptr_sem).
1040 */
7af9cce8
DM
1041 if (!IS_NOQUOTA(inode)) {
1042 if (unlikely(inode_get_rsv_space(inode) > 0))
1043 reserved = 1;
fb58b731 1044 remove_inode_dquot_ref(inode, type, tofree_head);
7af9cce8 1045 }
fb58b731 1046 }
55fa6091 1047 spin_unlock(&inode_sb_list_lock);
7af9cce8
DM
1048#ifdef CONFIG_QUOTA_DEBUG
1049 if (reserved) {
1050 printk(KERN_WARNING "VFS (%s): Writes happened after quota"
1051 " was disabled thus quota information is probably "
1052 "inconsistent. Please run quotacheck(8).\n", sb->s_id);
1053 }
1054#endif
fb58b731
CH
1055}
1056
1da177e4
LT
1057/* Gather all references from inodes and drop them */
1058static void drop_dquot_ref(struct super_block *sb, int type)
1059{
1060 LIST_HEAD(tofree_head);
1061
fb58b731
CH
1062 if (sb->dq_op) {
1063 down_write(&sb_dqopt(sb)->dqptr_sem);
1064 remove_dquot_ref(sb, type, &tofree_head);
1065 up_write(&sb_dqopt(sb)->dqptr_sem);
1066 put_dquot_list(&tofree_head);
1067 }
1da177e4
LT
1068}
1069
12095460 1070static inline void dquot_incr_inodes(struct dquot *dquot, qsize_t number)
1da177e4
LT
1071{
1072 dquot->dq_dqb.dqb_curinodes += number;
1073}
1074
1075static inline void dquot_incr_space(struct dquot *dquot, qsize_t number)
1076{
1077 dquot->dq_dqb.dqb_curspace += number;
1078}
1079
f18df228
MC
1080static inline void dquot_resv_space(struct dquot *dquot, qsize_t number)
1081{
1082 dquot->dq_dqb.dqb_rsvspace += number;
1083}
1084
740d9dcd
MC
1085/*
1086 * Claim reserved quota space
1087 */
0a5a9c72 1088static void dquot_claim_reserved_space(struct dquot *dquot, qsize_t number)
740d9dcd 1089{
0a5a9c72
JK
1090 if (dquot->dq_dqb.dqb_rsvspace < number) {
1091 WARN_ON_ONCE(1);
1092 number = dquot->dq_dqb.dqb_rsvspace;
1093 }
740d9dcd
MC
1094 dquot->dq_dqb.dqb_curspace += number;
1095 dquot->dq_dqb.dqb_rsvspace -= number;
1096}
1097
1098static inline
1099void dquot_free_reserved_space(struct dquot *dquot, qsize_t number)
1100{
0a5a9c72
JK
1101 if (dquot->dq_dqb.dqb_rsvspace >= number)
1102 dquot->dq_dqb.dqb_rsvspace -= number;
1103 else {
1104 WARN_ON_ONCE(1);
1105 dquot->dq_dqb.dqb_rsvspace = 0;
1106 }
740d9dcd
MC
1107}
1108
7a2435d8 1109static void dquot_decr_inodes(struct dquot *dquot, qsize_t number)
1da177e4 1110{
db49d2df
JK
1111 if (sb_dqopt(dquot->dq_sb)->flags & DQUOT_NEGATIVE_USAGE ||
1112 dquot->dq_dqb.dqb_curinodes >= number)
1da177e4
LT
1113 dquot->dq_dqb.dqb_curinodes -= number;
1114 else
1115 dquot->dq_dqb.dqb_curinodes = 0;
1116 if (dquot->dq_dqb.dqb_curinodes <= dquot->dq_dqb.dqb_isoftlimit)
1117 dquot->dq_dqb.dqb_itime = (time_t) 0;
1118 clear_bit(DQ_INODES_B, &dquot->dq_flags);
1119}
1120
7a2435d8 1121static void dquot_decr_space(struct dquot *dquot, qsize_t number)
1da177e4 1122{
db49d2df
JK
1123 if (sb_dqopt(dquot->dq_sb)->flags & DQUOT_NEGATIVE_USAGE ||
1124 dquot->dq_dqb.dqb_curspace >= number)
1da177e4
LT
1125 dquot->dq_dqb.dqb_curspace -= number;
1126 else
1127 dquot->dq_dqb.dqb_curspace = 0;
12095460 1128 if (dquot->dq_dqb.dqb_curspace <= dquot->dq_dqb.dqb_bsoftlimit)
1da177e4
LT
1129 dquot->dq_dqb.dqb_btime = (time_t) 0;
1130 clear_bit(DQ_BLKS_B, &dquot->dq_flags);
1131}
1132
bf097aaf
JK
1133struct dquot_warn {
1134 struct super_block *w_sb;
7b9c7321 1135 struct kqid w_dq_id;
bf097aaf
JK
1136 short w_type;
1137};
1138
c525460e
JK
1139static int warning_issued(struct dquot *dquot, const int warntype)
1140{
1141 int flag = (warntype == QUOTA_NL_BHARDWARN ||
1142 warntype == QUOTA_NL_BSOFTLONGWARN) ? DQ_BLKS_B :
1143 ((warntype == QUOTA_NL_IHARDWARN ||
1144 warntype == QUOTA_NL_ISOFTLONGWARN) ? DQ_INODES_B : 0);
1145
1146 if (!flag)
1147 return 0;
1148 return test_and_set_bit(flag, &dquot->dq_flags);
1149}
1150
8e893469 1151#ifdef CONFIG_PRINT_QUOTA_WARNING
1da177e4
LT
1152static int flag_print_warnings = 1;
1153
bf097aaf 1154static int need_print_warning(struct dquot_warn *warn)
1da177e4
LT
1155{
1156 if (!flag_print_warnings)
1157 return 0;
1158
7b9c7321 1159 switch (warn->w_dq_id.type) {
1da177e4 1160 case USRQUOTA:
7b9c7321 1161 return current_fsuid() == warn->w_dq_id.uid;
1da177e4 1162 case GRPQUOTA:
7b9c7321 1163 return in_group_p(warn->w_dq_id.gid);
1da177e4
LT
1164 }
1165 return 0;
1166}
1167
1da177e4 1168/* Print warning to user which exceeded quota */
bf097aaf 1169static void print_warning(struct dquot_warn *warn)
1da177e4
LT
1170{
1171 char *msg = NULL;
24ec839c 1172 struct tty_struct *tty;
bf097aaf 1173 int warntype = warn->w_type;
1da177e4 1174
657d3bfa
JK
1175 if (warntype == QUOTA_NL_IHARDBELOW ||
1176 warntype == QUOTA_NL_ISOFTBELOW ||
1177 warntype == QUOTA_NL_BHARDBELOW ||
bf097aaf 1178 warntype == QUOTA_NL_BSOFTBELOW || !need_print_warning(warn))
1da177e4
LT
1179 return;
1180
24ec839c
PZ
1181 tty = get_current_tty();
1182 if (!tty)
452a00d2 1183 return;
bf097aaf 1184 tty_write_message(tty, warn->w_sb->s_id);
8e893469 1185 if (warntype == QUOTA_NL_ISOFTWARN || warntype == QUOTA_NL_BSOFTWARN)
24ec839c 1186 tty_write_message(tty, ": warning, ");
1da177e4 1187 else
24ec839c 1188 tty_write_message(tty, ": write failed, ");
7b9c7321 1189 tty_write_message(tty, quotatypes[warn->w_dq_id.type]);
1da177e4 1190 switch (warntype) {
8e893469 1191 case QUOTA_NL_IHARDWARN:
1da177e4
LT
1192 msg = " file limit reached.\r\n";
1193 break;
8e893469 1194 case QUOTA_NL_ISOFTLONGWARN:
1da177e4
LT
1195 msg = " file quota exceeded too long.\r\n";
1196 break;
8e893469 1197 case QUOTA_NL_ISOFTWARN:
1da177e4
LT
1198 msg = " file quota exceeded.\r\n";
1199 break;
8e893469 1200 case QUOTA_NL_BHARDWARN:
1da177e4
LT
1201 msg = " block limit reached.\r\n";
1202 break;
8e893469 1203 case QUOTA_NL_BSOFTLONGWARN:
1da177e4
LT
1204 msg = " block quota exceeded too long.\r\n";
1205 break;
8e893469 1206 case QUOTA_NL_BSOFTWARN:
1da177e4
LT
1207 msg = " block quota exceeded.\r\n";
1208 break;
1209 }
24ec839c 1210 tty_write_message(tty, msg);
452a00d2 1211 tty_kref_put(tty);
1da177e4 1212}
8e893469
JK
1213#endif
1214
bf097aaf
JK
1215static void prepare_warning(struct dquot_warn *warn, struct dquot *dquot,
1216 int warntype)
1217{
1218 if (warning_issued(dquot, warntype))
1219 return;
1220 warn->w_type = warntype;
1221 warn->w_sb = dquot->dq_sb;
7b9c7321 1222 warn->w_dq_id = dquot->dq_id;
bf097aaf
JK
1223}
1224
f18df228
MC
1225/*
1226 * Write warnings to the console and send warning messages over netlink.
1227 *
bf097aaf 1228 * Note that this function can call into tty and networking code.
f18df228 1229 */
bf097aaf 1230static void flush_warnings(struct dquot_warn *warn)
1da177e4
LT
1231{
1232 int i;
1233
86e931a3 1234 for (i = 0; i < MAXQUOTAS; i++) {
bf097aaf
JK
1235 if (warn[i].w_type == QUOTA_NL_NOWARN)
1236 continue;
8e893469 1237#ifdef CONFIG_PRINT_QUOTA_WARNING
bf097aaf 1238 print_warning(&warn[i]);
8e893469 1239#endif
7b9c7321 1240 quota_send_warning(warn[i].w_dq_id,
bf097aaf 1241 warn[i].w_sb->s_dev, warn[i].w_type);
86e931a3 1242 }
1da177e4
LT
1243}
1244
7a2435d8 1245static int ignore_hardlimit(struct dquot *dquot)
1da177e4 1246{
4c376dca 1247 struct mem_dqinfo *info = &sb_dqopt(dquot->dq_sb)->info[dquot->dq_id.type];
1da177e4
LT
1248
1249 return capable(CAP_SYS_RESOURCE) &&
268157ba
JK
1250 (info->dqi_format->qf_fmt_id != QFMT_VFS_OLD ||
1251 !(info->dqi_flags & V1_DQF_RSQUASH));
1da177e4
LT
1252}
1253
1254/* needs dq_data_lock */
bf097aaf
JK
1255static int check_idq(struct dquot *dquot, qsize_t inodes,
1256 struct dquot_warn *warn)
1da177e4 1257{
268157ba
JK
1258 qsize_t newinodes = dquot->dq_dqb.dqb_curinodes + inodes;
1259
4c376dca 1260 if (!sb_has_quota_limits_enabled(dquot->dq_sb, dquot->dq_id.type) ||
f55abc0f 1261 test_bit(DQ_FAKE_B, &dquot->dq_flags))
efd8f0e6 1262 return 0;
1da177e4
LT
1263
1264 if (dquot->dq_dqb.dqb_ihardlimit &&
268157ba 1265 newinodes > dquot->dq_dqb.dqb_ihardlimit &&
1da177e4 1266 !ignore_hardlimit(dquot)) {
bf097aaf 1267 prepare_warning(warn, dquot, QUOTA_NL_IHARDWARN);
efd8f0e6 1268 return -EDQUOT;
1da177e4
LT
1269 }
1270
1271 if (dquot->dq_dqb.dqb_isoftlimit &&
268157ba
JK
1272 newinodes > dquot->dq_dqb.dqb_isoftlimit &&
1273 dquot->dq_dqb.dqb_itime &&
1274 get_seconds() >= dquot->dq_dqb.dqb_itime &&
1da177e4 1275 !ignore_hardlimit(dquot)) {
bf097aaf 1276 prepare_warning(warn, dquot, QUOTA_NL_ISOFTLONGWARN);
efd8f0e6 1277 return -EDQUOT;
1da177e4
LT
1278 }
1279
1280 if (dquot->dq_dqb.dqb_isoftlimit &&
268157ba 1281 newinodes > dquot->dq_dqb.dqb_isoftlimit &&
1da177e4 1282 dquot->dq_dqb.dqb_itime == 0) {
bf097aaf 1283 prepare_warning(warn, dquot, QUOTA_NL_ISOFTWARN);
268157ba 1284 dquot->dq_dqb.dqb_itime = get_seconds() +
4c376dca 1285 sb_dqopt(dquot->dq_sb)->info[dquot->dq_id.type].dqi_igrace;
1da177e4
LT
1286 }
1287
efd8f0e6 1288 return 0;
1da177e4
LT
1289}
1290
1291/* needs dq_data_lock */
bf097aaf
JK
1292static int check_bdq(struct dquot *dquot, qsize_t space, int prealloc,
1293 struct dquot_warn *warn)
1da177e4 1294{
f18df228 1295 qsize_t tspace;
268157ba 1296 struct super_block *sb = dquot->dq_sb;
f18df228 1297
4c376dca 1298 if (!sb_has_quota_limits_enabled(sb, dquot->dq_id.type) ||
f55abc0f 1299 test_bit(DQ_FAKE_B, &dquot->dq_flags))
efd8f0e6 1300 return 0;
1da177e4 1301
f18df228
MC
1302 tspace = dquot->dq_dqb.dqb_curspace + dquot->dq_dqb.dqb_rsvspace
1303 + space;
1304
1da177e4 1305 if (dquot->dq_dqb.dqb_bhardlimit &&
f18df228 1306 tspace > dquot->dq_dqb.dqb_bhardlimit &&
1da177e4
LT
1307 !ignore_hardlimit(dquot)) {
1308 if (!prealloc)
bf097aaf 1309 prepare_warning(warn, dquot, QUOTA_NL_BHARDWARN);
efd8f0e6 1310 return -EDQUOT;
1da177e4
LT
1311 }
1312
1313 if (dquot->dq_dqb.dqb_bsoftlimit &&
f18df228 1314 tspace > dquot->dq_dqb.dqb_bsoftlimit &&
268157ba
JK
1315 dquot->dq_dqb.dqb_btime &&
1316 get_seconds() >= dquot->dq_dqb.dqb_btime &&
1da177e4
LT
1317 !ignore_hardlimit(dquot)) {
1318 if (!prealloc)
bf097aaf 1319 prepare_warning(warn, dquot, QUOTA_NL_BSOFTLONGWARN);
efd8f0e6 1320 return -EDQUOT;
1da177e4
LT
1321 }
1322
1323 if (dquot->dq_dqb.dqb_bsoftlimit &&
f18df228 1324 tspace > dquot->dq_dqb.dqb_bsoftlimit &&
1da177e4
LT
1325 dquot->dq_dqb.dqb_btime == 0) {
1326 if (!prealloc) {
bf097aaf 1327 prepare_warning(warn, dquot, QUOTA_NL_BSOFTWARN);
268157ba 1328 dquot->dq_dqb.dqb_btime = get_seconds() +
4c376dca 1329 sb_dqopt(sb)->info[dquot->dq_id.type].dqi_bgrace;
1da177e4
LT
1330 }
1331 else
1332 /*
1333 * We don't allow preallocation to exceed softlimit so exceeding will
1334 * be always printed
1335 */
efd8f0e6 1336 return -EDQUOT;
1da177e4
LT
1337 }
1338
efd8f0e6 1339 return 0;
1da177e4
LT
1340}
1341
12095460 1342static int info_idq_free(struct dquot *dquot, qsize_t inodes)
657d3bfa 1343{
268157ba
JK
1344 qsize_t newinodes;
1345
657d3bfa 1346 if (test_bit(DQ_FAKE_B, &dquot->dq_flags) ||
f55abc0f 1347 dquot->dq_dqb.dqb_curinodes <= dquot->dq_dqb.dqb_isoftlimit ||
4c376dca 1348 !sb_has_quota_limits_enabled(dquot->dq_sb, dquot->dq_id.type))
657d3bfa
JK
1349 return QUOTA_NL_NOWARN;
1350
268157ba
JK
1351 newinodes = dquot->dq_dqb.dqb_curinodes - inodes;
1352 if (newinodes <= dquot->dq_dqb.dqb_isoftlimit)
657d3bfa
JK
1353 return QUOTA_NL_ISOFTBELOW;
1354 if (dquot->dq_dqb.dqb_curinodes >= dquot->dq_dqb.dqb_ihardlimit &&
268157ba 1355 newinodes < dquot->dq_dqb.dqb_ihardlimit)
657d3bfa
JK
1356 return QUOTA_NL_IHARDBELOW;
1357 return QUOTA_NL_NOWARN;
1358}
1359
1360static int info_bdq_free(struct dquot *dquot, qsize_t space)
1361{
1362 if (test_bit(DQ_FAKE_B, &dquot->dq_flags) ||
12095460 1363 dquot->dq_dqb.dqb_curspace <= dquot->dq_dqb.dqb_bsoftlimit)
657d3bfa
JK
1364 return QUOTA_NL_NOWARN;
1365
12095460 1366 if (dquot->dq_dqb.dqb_curspace - space <= dquot->dq_dqb.dqb_bsoftlimit)
657d3bfa 1367 return QUOTA_NL_BSOFTBELOW;
12095460
JK
1368 if (dquot->dq_dqb.dqb_curspace >= dquot->dq_dqb.dqb_bhardlimit &&
1369 dquot->dq_dqb.dqb_curspace - space < dquot->dq_dqb.dqb_bhardlimit)
657d3bfa
JK
1370 return QUOTA_NL_BHARDBELOW;
1371 return QUOTA_NL_NOWARN;
1372}
0a5a9c72 1373
189eef59
CH
1374static int dquot_active(const struct inode *inode)
1375{
1376 struct super_block *sb = inode->i_sb;
1377
1378 if (IS_NOQUOTA(inode))
1379 return 0;
1380 return sb_any_quota_loaded(sb) & ~sb_any_quota_suspended(sb);
1381}
1382
1da177e4 1383/*
871a2931
CH
1384 * Initialize quota pointers in inode
1385 *
1386 * We do things in a bit complicated way but by that we avoid calling
1387 * dqget() and thus filesystem callbacks under dqptr_sem.
1388 *
1389 * It is better to call this function outside of any transaction as it
1390 * might need a lot of space in journal for dquot structure allocation.
1da177e4 1391 */
871a2931 1392static void __dquot_initialize(struct inode *inode, int type)
1da177e4 1393{
871a2931 1394 int cnt;
ad1e6e8d 1395 struct dquot *got[MAXQUOTAS];
cc33412f 1396 struct super_block *sb = inode->i_sb;
0a5a9c72 1397 qsize_t rsv;
1da177e4 1398
d3be915f
IM
1399 /* First test before acquiring mutex - solves deadlocks when we
1400 * re-enter the quota code and are already holding the mutex */
189eef59 1401 if (!dquot_active(inode))
871a2931 1402 return;
cc33412f
JK
1403
1404 /* First get references to structures we might need. */
1405 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
aca645a6 1406 struct kqid qid;
ad1e6e8d 1407 got[cnt] = NULL;
cc33412f
JK
1408 if (type != -1 && cnt != type)
1409 continue;
1410 switch (cnt) {
1411 case USRQUOTA:
aca645a6 1412 qid = make_kqid_uid(inode->i_uid);
cc33412f
JK
1413 break;
1414 case GRPQUOTA:
aca645a6 1415 qid = make_kqid_gid(inode->i_gid);
cc33412f
JK
1416 break;
1417 }
aca645a6 1418 got[cnt] = dqget(sb, qid);
cc33412f
JK
1419 }
1420
1421 down_write(&sb_dqopt(sb)->dqptr_sem);
1da177e4
LT
1422 if (IS_NOQUOTA(inode))
1423 goto out_err;
1424 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1425 if (type != -1 && cnt != type)
1426 continue;
cc33412f
JK
1427 /* Avoid races with quotaoff() */
1428 if (!sb_has_quota_active(sb, cnt))
1429 continue;
4408ea41
JK
1430 /* We could race with quotaon or dqget() could have failed */
1431 if (!got[cnt])
1432 continue;
dd6f3c6d 1433 if (!inode->i_dquot[cnt]) {
cc33412f 1434 inode->i_dquot[cnt] = got[cnt];
dd6f3c6d 1435 got[cnt] = NULL;
0a5a9c72
JK
1436 /*
1437 * Make quota reservation system happy if someone
1438 * did a write before quota was turned on
1439 */
1440 rsv = inode_get_rsv_space(inode);
1441 if (unlikely(rsv))
1442 dquot_resv_space(inode->i_dquot[cnt], rsv);
1da177e4
LT
1443 }
1444 }
1445out_err:
cc33412f
JK
1446 up_write(&sb_dqopt(sb)->dqptr_sem);
1447 /* Drop unused references */
dc52dd3a 1448 dqput_all(got);
871a2931
CH
1449}
1450
1451void dquot_initialize(struct inode *inode)
1452{
1453 __dquot_initialize(inode, -1);
1da177e4 1454}
08d0350c 1455EXPORT_SYMBOL(dquot_initialize);
1da177e4
LT
1456
1457/*
1458 * Release all quotas referenced by inode
1da177e4 1459 */
9f754758 1460static void __dquot_drop(struct inode *inode)
1da177e4
LT
1461{
1462 int cnt;
cc33412f 1463 struct dquot *put[MAXQUOTAS];
1da177e4 1464
cc33412f 1465 down_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
1da177e4 1466 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
cc33412f 1467 put[cnt] = inode->i_dquot[cnt];
dd6f3c6d 1468 inode->i_dquot[cnt] = NULL;
1da177e4
LT
1469 }
1470 up_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
dc52dd3a 1471 dqput_all(put);
1da177e4
LT
1472}
1473
9f754758
CH
1474void dquot_drop(struct inode *inode)
1475{
1476 int cnt;
1477
1478 if (IS_NOQUOTA(inode))
1479 return;
1480
1481 /*
1482 * Test before calling to rule out calls from proc and such
1483 * where we are not allowed to block. Note that this is
1484 * actually reliable test even without the lock - the caller
1485 * must assure that nobody can come after the DQUOT_DROP and
1486 * add quota pointers back anyway.
1487 */
1488 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1489 if (inode->i_dquot[cnt])
1490 break;
1491 }
1492
1493 if (cnt < MAXQUOTAS)
1494 __dquot_drop(inode);
1495}
1496EXPORT_SYMBOL(dquot_drop);
b85f4b87 1497
fd8fbfc1
DM
1498/*
1499 * inode_reserved_space is managed internally by quota, and protected by
1500 * i_lock similar to i_blocks+i_bytes.
1501 */
1502static qsize_t *inode_reserved_space(struct inode * inode)
1503{
1504 /* Filesystem must explicitly define it's own method in order to use
1505 * quota reservation interface */
1506 BUG_ON(!inode->i_sb->dq_op->get_reserved_space);
1507 return inode->i_sb->dq_op->get_reserved_space(inode);
1508}
1509
c469070a 1510void inode_add_rsv_space(struct inode *inode, qsize_t number)
fd8fbfc1
DM
1511{
1512 spin_lock(&inode->i_lock);
1513 *inode_reserved_space(inode) += number;
1514 spin_unlock(&inode->i_lock);
1515}
c469070a 1516EXPORT_SYMBOL(inode_add_rsv_space);
fd8fbfc1 1517
c469070a 1518void inode_claim_rsv_space(struct inode *inode, qsize_t number)
fd8fbfc1
DM
1519{
1520 spin_lock(&inode->i_lock);
1521 *inode_reserved_space(inode) -= number;
1522 __inode_add_bytes(inode, number);
1523 spin_unlock(&inode->i_lock);
1524}
c469070a 1525EXPORT_SYMBOL(inode_claim_rsv_space);
fd8fbfc1 1526
c469070a 1527void inode_sub_rsv_space(struct inode *inode, qsize_t number)
fd8fbfc1
DM
1528{
1529 spin_lock(&inode->i_lock);
1530 *inode_reserved_space(inode) -= number;
1531 spin_unlock(&inode->i_lock);
1532}
c469070a 1533EXPORT_SYMBOL(inode_sub_rsv_space);
fd8fbfc1
DM
1534
1535static qsize_t inode_get_rsv_space(struct inode *inode)
1536{
1537 qsize_t ret;
05b5d898
JK
1538
1539 if (!inode->i_sb->dq_op->get_reserved_space)
1540 return 0;
fd8fbfc1
DM
1541 spin_lock(&inode->i_lock);
1542 ret = *inode_reserved_space(inode);
1543 spin_unlock(&inode->i_lock);
1544 return ret;
1545}
1546
1547static void inode_incr_space(struct inode *inode, qsize_t number,
1548 int reserve)
1549{
1550 if (reserve)
1551 inode_add_rsv_space(inode, number);
1552 else
1553 inode_add_bytes(inode, number);
1554}
1555
1556static void inode_decr_space(struct inode *inode, qsize_t number, int reserve)
1557{
1558 if (reserve)
1559 inode_sub_rsv_space(inode, number);
1560 else
1561 inode_sub_bytes(inode, number);
1562}
1563
1da177e4 1564/*
5dd4056d
CH
1565 * This functions updates i_blocks+i_bytes fields and quota information
1566 * (together with appropriate checks).
1567 *
1568 * NOTE: We absolutely rely on the fact that caller dirties the inode
1569 * (usually helpers in quotaops.h care about this) and holds a handle for
1570 * the current transaction so that dquot write and inode write go into the
1571 * same transaction.
1da177e4
LT
1572 */
1573
1574/*
1575 * This operation can block, but only after everything is updated
1576 */
56246f9a 1577int __dquot_alloc_space(struct inode *inode, qsize_t number, int flags)
1da177e4 1578{
5dd4056d 1579 int cnt, ret = 0;
bf097aaf
JK
1580 struct dquot_warn warn[MAXQUOTAS];
1581 struct dquot **dquots = inode->i_dquot;
56246f9a 1582 int reserve = flags & DQUOT_SPACE_RESERVE;
1da177e4 1583
fd8fbfc1
DM
1584 /*
1585 * First test before acquiring mutex - solves deadlocks when we
1586 * re-enter the quota code and are already holding the mutex
1587 */
189eef59 1588 if (!dquot_active(inode)) {
fd8fbfc1
DM
1589 inode_incr_space(inode, number, reserve);
1590 goto out;
1591 }
1592
1593 down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1da177e4 1594 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
bf097aaf 1595 warn[cnt].w_type = QUOTA_NL_NOWARN;
1da177e4 1596
1da177e4
LT
1597 spin_lock(&dq_data_lock);
1598 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
bf097aaf 1599 if (!dquots[cnt])
1da177e4 1600 continue;
bf097aaf
JK
1601 ret = check_bdq(dquots[cnt], number,
1602 !(flags & DQUOT_SPACE_WARN), &warn[cnt]);
1603 if (ret && !(flags & DQUOT_SPACE_NOFAIL)) {
fd8fbfc1
DM
1604 spin_unlock(&dq_data_lock);
1605 goto out_flush_warn;
f18df228 1606 }
1da177e4
LT
1607 }
1608 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
bf097aaf 1609 if (!dquots[cnt])
1da177e4 1610 continue;
f18df228 1611 if (reserve)
bf097aaf 1612 dquot_resv_space(dquots[cnt], number);
f18df228 1613 else
bf097aaf 1614 dquot_incr_space(dquots[cnt], number);
1da177e4 1615 }
fd8fbfc1 1616 inode_incr_space(inode, number, reserve);
1da177e4 1617 spin_unlock(&dq_data_lock);
f18df228 1618
fd8fbfc1
DM
1619 if (reserve)
1620 goto out_flush_warn;
bf097aaf 1621 mark_all_dquot_dirty(dquots);
fd8fbfc1 1622out_flush_warn:
1da177e4 1623 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
bf097aaf 1624 flush_warnings(warn);
f18df228
MC
1625out:
1626 return ret;
1627}
5dd4056d 1628EXPORT_SYMBOL(__dquot_alloc_space);
1da177e4
LT
1629
1630/*
1631 * This operation can block, but only after everything is updated
1632 */
63936dda 1633int dquot_alloc_inode(const struct inode *inode)
1da177e4 1634{
efd8f0e6 1635 int cnt, ret = 0;
bf097aaf
JK
1636 struct dquot_warn warn[MAXQUOTAS];
1637 struct dquot * const *dquots = inode->i_dquot;
1da177e4 1638
d3be915f
IM
1639 /* First test before acquiring mutex - solves deadlocks when we
1640 * re-enter the quota code and are already holding the mutex */
189eef59 1641 if (!dquot_active(inode))
63936dda 1642 return 0;
1da177e4 1643 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
bf097aaf 1644 warn[cnt].w_type = QUOTA_NL_NOWARN;
1da177e4 1645 down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1da177e4
LT
1646 spin_lock(&dq_data_lock);
1647 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
bf097aaf 1648 if (!dquots[cnt])
1da177e4 1649 continue;
bf097aaf 1650 ret = check_idq(dquots[cnt], 1, &warn[cnt]);
efd8f0e6 1651 if (ret)
1da177e4
LT
1652 goto warn_put_all;
1653 }
1654
1655 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
bf097aaf 1656 if (!dquots[cnt])
1da177e4 1657 continue;
bf097aaf 1658 dquot_incr_inodes(dquots[cnt], 1);
1da177e4 1659 }
efd8f0e6 1660
1da177e4
LT
1661warn_put_all:
1662 spin_unlock(&dq_data_lock);
63936dda 1663 if (ret == 0)
bf097aaf 1664 mark_all_dquot_dirty(dquots);
1da177e4 1665 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
bf097aaf 1666 flush_warnings(warn);
1da177e4
LT
1667 return ret;
1668}
08d0350c 1669EXPORT_SYMBOL(dquot_alloc_inode);
1da177e4 1670
5dd4056d
CH
1671/*
1672 * Convert in-memory reserved quotas to real consumed quotas
1673 */
1674int dquot_claim_space_nodirty(struct inode *inode, qsize_t number)
740d9dcd
MC
1675{
1676 int cnt;
740d9dcd 1677
189eef59 1678 if (!dquot_active(inode)) {
fd8fbfc1 1679 inode_claim_rsv_space(inode, number);
5dd4056d 1680 return 0;
740d9dcd
MC
1681 }
1682
1683 down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
740d9dcd
MC
1684 spin_lock(&dq_data_lock);
1685 /* Claim reserved quotas to allocated quotas */
1686 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
dd6f3c6d 1687 if (inode->i_dquot[cnt])
740d9dcd
MC
1688 dquot_claim_reserved_space(inode->i_dquot[cnt],
1689 number);
1690 }
1691 /* Update inode bytes */
fd8fbfc1 1692 inode_claim_rsv_space(inode, number);
740d9dcd 1693 spin_unlock(&dq_data_lock);
dc52dd3a 1694 mark_all_dquot_dirty(inode->i_dquot);
740d9dcd 1695 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
5dd4056d 1696 return 0;
740d9dcd 1697}
5dd4056d 1698EXPORT_SYMBOL(dquot_claim_space_nodirty);
740d9dcd 1699
1da177e4
LT
1700/*
1701 * This operation can block, but only after everything is updated
1702 */
56246f9a 1703void __dquot_free_space(struct inode *inode, qsize_t number, int flags)
1da177e4
LT
1704{
1705 unsigned int cnt;
bf097aaf
JK
1706 struct dquot_warn warn[MAXQUOTAS];
1707 struct dquot **dquots = inode->i_dquot;
56246f9a 1708 int reserve = flags & DQUOT_SPACE_RESERVE;
1da177e4 1709
d3be915f
IM
1710 /* First test before acquiring mutex - solves deadlocks when we
1711 * re-enter the quota code and are already holding the mutex */
189eef59 1712 if (!dquot_active(inode)) {
fd8fbfc1 1713 inode_decr_space(inode, number, reserve);
5dd4056d 1714 return;
1da177e4 1715 }
657d3bfa 1716
1da177e4 1717 down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1da177e4
LT
1718 spin_lock(&dq_data_lock);
1719 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
bf097aaf
JK
1720 int wtype;
1721
1722 warn[cnt].w_type = QUOTA_NL_NOWARN;
1723 if (!dquots[cnt])
1da177e4 1724 continue;
bf097aaf
JK
1725 wtype = info_bdq_free(dquots[cnt], number);
1726 if (wtype != QUOTA_NL_NOWARN)
1727 prepare_warning(&warn[cnt], dquots[cnt], wtype);
fd8fbfc1 1728 if (reserve)
bf097aaf 1729 dquot_free_reserved_space(dquots[cnt], number);
fd8fbfc1 1730 else
bf097aaf 1731 dquot_decr_space(dquots[cnt], number);
1da177e4 1732 }
fd8fbfc1 1733 inode_decr_space(inode, number, reserve);
1da177e4 1734 spin_unlock(&dq_data_lock);
fd8fbfc1
DM
1735
1736 if (reserve)
1737 goto out_unlock;
bf097aaf 1738 mark_all_dquot_dirty(dquots);
fd8fbfc1 1739out_unlock:
1da177e4 1740 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
bf097aaf 1741 flush_warnings(warn);
fd8fbfc1 1742}
5dd4056d 1743EXPORT_SYMBOL(__dquot_free_space);
fd8fbfc1 1744
1da177e4
LT
1745/*
1746 * This operation can block, but only after everything is updated
1747 */
63936dda 1748void dquot_free_inode(const struct inode *inode)
1da177e4
LT
1749{
1750 unsigned int cnt;
bf097aaf
JK
1751 struct dquot_warn warn[MAXQUOTAS];
1752 struct dquot * const *dquots = inode->i_dquot;
1da177e4 1753
d3be915f
IM
1754 /* First test before acquiring mutex - solves deadlocks when we
1755 * re-enter the quota code and are already holding the mutex */
189eef59 1756 if (!dquot_active(inode))
63936dda 1757 return;
657d3bfa 1758
1da177e4 1759 down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1da177e4
LT
1760 spin_lock(&dq_data_lock);
1761 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
bf097aaf
JK
1762 int wtype;
1763
1764 warn[cnt].w_type = QUOTA_NL_NOWARN;
1765 if (!dquots[cnt])
1da177e4 1766 continue;
bf097aaf
JK
1767 wtype = info_idq_free(dquots[cnt], 1);
1768 if (wtype != QUOTA_NL_NOWARN)
1769 prepare_warning(&warn[cnt], dquots[cnt], wtype);
1770 dquot_decr_inodes(dquots[cnt], 1);
1da177e4
LT
1771 }
1772 spin_unlock(&dq_data_lock);
bf097aaf 1773 mark_all_dquot_dirty(dquots);
1da177e4 1774 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
bf097aaf 1775 flush_warnings(warn);
1da177e4 1776}
08d0350c 1777EXPORT_SYMBOL(dquot_free_inode);
1da177e4
LT
1778
1779/*
1780 * Transfer the number of inode and blocks from one diskquota to an other.
bc8e5f07
JK
1781 * On success, dquot references in transfer_to are consumed and references
1782 * to original dquots that need to be released are placed there. On failure,
1783 * references are kept untouched.
1da177e4
LT
1784 *
1785 * This operation can block, but only after everything is updated
1786 * A transaction must be started when entering this function.
bc8e5f07 1787 *
1da177e4 1788 */
bc8e5f07 1789int __dquot_transfer(struct inode *inode, struct dquot **transfer_to)
1da177e4 1790{
740d9dcd
MC
1791 qsize_t space, cur_space;
1792 qsize_t rsv_space = 0;
bc8e5f07 1793 struct dquot *transfer_from[MAXQUOTAS] = {};
efd8f0e6 1794 int cnt, ret = 0;
9e32784b 1795 char is_valid[MAXQUOTAS] = {};
bf097aaf
JK
1796 struct dquot_warn warn_to[MAXQUOTAS];
1797 struct dquot_warn warn_from_inodes[MAXQUOTAS];
1798 struct dquot_warn warn_from_space[MAXQUOTAS];
1da177e4 1799
d3be915f
IM
1800 /* First test before acquiring mutex - solves deadlocks when we
1801 * re-enter the quota code and are already holding the mutex */
1da177e4 1802 if (IS_NOQUOTA(inode))
efd8f0e6 1803 return 0;
cc33412f 1804 /* Initialize the arrays */
bf097aaf
JK
1805 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1806 warn_to[cnt].w_type = QUOTA_NL_NOWARN;
1807 warn_from_inodes[cnt].w_type = QUOTA_NL_NOWARN;
1808 warn_from_space[cnt].w_type = QUOTA_NL_NOWARN;
1809 }
cc33412f 1810 down_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
cc33412f
JK
1811 if (IS_NOQUOTA(inode)) { /* File without quota accounting? */
1812 up_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
bc8e5f07 1813 return 0;
cc33412f 1814 }
1da177e4 1815 spin_lock(&dq_data_lock);
740d9dcd 1816 cur_space = inode_get_bytes(inode);
fd8fbfc1 1817 rsv_space = inode_get_rsv_space(inode);
740d9dcd 1818 space = cur_space + rsv_space;
1da177e4
LT
1819 /* Build the transfer_from list and check the limits */
1820 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
9e32784b
D
1821 /*
1822 * Skip changes for same uid or gid or for turned off quota-type.
1823 */
dd6f3c6d 1824 if (!transfer_to[cnt])
1da177e4 1825 continue;
9e32784b
D
1826 /* Avoid races with quotaoff() */
1827 if (!sb_has_quota_active(inode->i_sb, cnt))
1828 continue;
1829 is_valid[cnt] = 1;
1da177e4 1830 transfer_from[cnt] = inode->i_dquot[cnt];
bf097aaf 1831 ret = check_idq(transfer_to[cnt], 1, &warn_to[cnt]);
efd8f0e6
CH
1832 if (ret)
1833 goto over_quota;
bf097aaf 1834 ret = check_bdq(transfer_to[cnt], space, 0, &warn_to[cnt]);
efd8f0e6 1835 if (ret)
cc33412f 1836 goto over_quota;
1da177e4
LT
1837 }
1838
1839 /*
1840 * Finally perform the needed transfer from transfer_from to transfer_to
1841 */
1842 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
9e32784b 1843 if (!is_valid[cnt])
1da177e4 1844 continue;
1da177e4
LT
1845 /* Due to IO error we might not have transfer_from[] structure */
1846 if (transfer_from[cnt]) {
bf097aaf
JK
1847 int wtype;
1848 wtype = info_idq_free(transfer_from[cnt], 1);
1849 if (wtype != QUOTA_NL_NOWARN)
1850 prepare_warning(&warn_from_inodes[cnt],
1851 transfer_from[cnt], wtype);
1852 wtype = info_bdq_free(transfer_from[cnt], space);
1853 if (wtype != QUOTA_NL_NOWARN)
1854 prepare_warning(&warn_from_space[cnt],
1855 transfer_from[cnt], wtype);
1da177e4 1856 dquot_decr_inodes(transfer_from[cnt], 1);
740d9dcd
MC
1857 dquot_decr_space(transfer_from[cnt], cur_space);
1858 dquot_free_reserved_space(transfer_from[cnt],
1859 rsv_space);
1da177e4
LT
1860 }
1861
1862 dquot_incr_inodes(transfer_to[cnt], 1);
740d9dcd
MC
1863 dquot_incr_space(transfer_to[cnt], cur_space);
1864 dquot_resv_space(transfer_to[cnt], rsv_space);
1da177e4
LT
1865
1866 inode->i_dquot[cnt] = transfer_to[cnt];
1867 }
1da177e4 1868 spin_unlock(&dq_data_lock);
cc33412f
JK
1869 up_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
1870
dc52dd3a
DM
1871 mark_all_dquot_dirty(transfer_from);
1872 mark_all_dquot_dirty(transfer_to);
bf097aaf
JK
1873 flush_warnings(warn_to);
1874 flush_warnings(warn_from_inodes);
1875 flush_warnings(warn_from_space);
bc8e5f07 1876 /* Pass back references to put */
dc52dd3a 1877 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
9e32784b
D
1878 if (is_valid[cnt])
1879 transfer_to[cnt] = transfer_from[cnt];
86f3cbec 1880 return 0;
cc33412f
JK
1881over_quota:
1882 spin_unlock(&dq_data_lock);
1883 up_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
bf097aaf 1884 flush_warnings(warn_to);
86f3cbec 1885 return ret;
1da177e4 1886}
bc8e5f07 1887EXPORT_SYMBOL(__dquot_transfer);
1da177e4 1888
8ddd69d6
DM
1889/* Wrapper for transferring ownership of an inode for uid/gid only
1890 * Called from FSXXX_setattr()
1891 */
b43fa828 1892int dquot_transfer(struct inode *inode, struct iattr *iattr)
b85f4b87 1893{
bc8e5f07
JK
1894 struct dquot *transfer_to[MAXQUOTAS] = {};
1895 struct super_block *sb = inode->i_sb;
1896 int ret;
8ddd69d6 1897
189eef59 1898 if (!dquot_active(inode))
bc8e5f07 1899 return 0;
12755627 1900
bc8e5f07 1901 if (iattr->ia_valid & ATTR_UID && iattr->ia_uid != inode->i_uid)
aca645a6 1902 transfer_to[USRQUOTA] = dqget(sb, make_kqid_uid(iattr->ia_uid));
bc8e5f07 1903 if (iattr->ia_valid & ATTR_GID && iattr->ia_gid != inode->i_gid)
aca645a6 1904 transfer_to[GRPQUOTA] = dqget(sb, make_kqid_gid(iattr->ia_gid));
bc8e5f07
JK
1905
1906 ret = __dquot_transfer(inode, transfer_to);
1907 dqput_all(transfer_to);
1908 return ret;
b85f4b87 1909}
b43fa828 1910EXPORT_SYMBOL(dquot_transfer);
b85f4b87 1911
1da177e4
LT
1912/*
1913 * Write info of quota file to disk
1914 */
1915int dquot_commit_info(struct super_block *sb, int type)
1916{
1917 int ret;
1918 struct quota_info *dqopt = sb_dqopt(sb);
1919
d3be915f 1920 mutex_lock(&dqopt->dqio_mutex);
1da177e4 1921 ret = dqopt->ops[type]->write_file_info(sb, type);
d3be915f 1922 mutex_unlock(&dqopt->dqio_mutex);
1da177e4
LT
1923 return ret;
1924}
08d0350c 1925EXPORT_SYMBOL(dquot_commit_info);
1da177e4
LT
1926
1927/*
1928 * Definitions of diskquota operations.
1929 */
61e225dc 1930const struct dquot_operations dquot_operations = {
1da177e4
LT
1931 .write_dquot = dquot_commit,
1932 .acquire_dquot = dquot_acquire,
1933 .release_dquot = dquot_release,
1934 .mark_dirty = dquot_mark_dquot_dirty,
74f783af
JK
1935 .write_info = dquot_commit_info,
1936 .alloc_dquot = dquot_alloc,
1937 .destroy_dquot = dquot_destroy,
1da177e4 1938};
123e9caf 1939EXPORT_SYMBOL(dquot_operations);
1da177e4 1940
907f4554
CH
1941/*
1942 * Generic helper for ->open on filesystems supporting disk quotas.
1943 */
1944int dquot_file_open(struct inode *inode, struct file *file)
1945{
1946 int error;
1947
1948 error = generic_file_open(inode, file);
1949 if (!error && (file->f_mode & FMODE_WRITE))
871a2931 1950 dquot_initialize(inode);
907f4554
CH
1951 return error;
1952}
1953EXPORT_SYMBOL(dquot_file_open);
1954
1da177e4
LT
1955/*
1956 * Turn quota off on a device. type == -1 ==> quotaoff for all types (umount)
1957 */
0f0dd62f 1958int dquot_disable(struct super_block *sb, int type, unsigned int flags)
1da177e4 1959{
0ff5af83 1960 int cnt, ret = 0;
1da177e4
LT
1961 struct quota_info *dqopt = sb_dqopt(sb);
1962 struct inode *toputinode[MAXQUOTAS];
1da177e4 1963
f55abc0f
JK
1964 /* Cannot turn off usage accounting without turning off limits, or
1965 * suspend quotas and simultaneously turn quotas off. */
1966 if ((flags & DQUOT_USAGE_ENABLED && !(flags & DQUOT_LIMITS_ENABLED))
1967 || (flags & DQUOT_SUSPENDED && flags & (DQUOT_LIMITS_ENABLED |
1968 DQUOT_USAGE_ENABLED)))
1969 return -EINVAL;
1970
1da177e4 1971 /* We need to serialize quota_off() for device */
d3be915f 1972 mutex_lock(&dqopt->dqonoff_mutex);
9377abd0
JK
1973
1974 /*
1975 * Skip everything if there's nothing to do. We have to do this because
1976 * sometimes we are called when fill_super() failed and calling
1977 * sync_fs() in such cases does no good.
1978 */
f55abc0f 1979 if (!sb_any_quota_loaded(sb)) {
9377abd0
JK
1980 mutex_unlock(&dqopt->dqonoff_mutex);
1981 return 0;
1982 }
1da177e4
LT
1983 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1984 toputinode[cnt] = NULL;
1da177e4
LT
1985 if (type != -1 && cnt != type)
1986 continue;
f55abc0f 1987 if (!sb_has_quota_loaded(sb, cnt))
0ff5af83 1988 continue;
f55abc0f
JK
1989
1990 if (flags & DQUOT_SUSPENDED) {
cc33412f 1991 spin_lock(&dq_state_lock);
f55abc0f
JK
1992 dqopt->flags |=
1993 dquot_state_flag(DQUOT_SUSPENDED, cnt);
cc33412f 1994 spin_unlock(&dq_state_lock);
f55abc0f 1995 } else {
cc33412f 1996 spin_lock(&dq_state_lock);
f55abc0f
JK
1997 dqopt->flags &= ~dquot_state_flag(flags, cnt);
1998 /* Turning off suspended quotas? */
1999 if (!sb_has_quota_loaded(sb, cnt) &&
2000 sb_has_quota_suspended(sb, cnt)) {
2001 dqopt->flags &= ~dquot_state_flag(
2002 DQUOT_SUSPENDED, cnt);
cc33412f 2003 spin_unlock(&dq_state_lock);
f55abc0f
JK
2004 iput(dqopt->files[cnt]);
2005 dqopt->files[cnt] = NULL;
2006 continue;
2007 }
cc33412f 2008 spin_unlock(&dq_state_lock);
0ff5af83 2009 }
f55abc0f
JK
2010
2011 /* We still have to keep quota loaded? */
2012 if (sb_has_quota_loaded(sb, cnt) && !(flags & DQUOT_SUSPENDED))
1da177e4 2013 continue;
1da177e4
LT
2014
2015 /* Note: these are blocking operations */
2016 drop_dquot_ref(sb, cnt);
2017 invalidate_dquots(sb, cnt);
2018 /*
268157ba
JK
2019 * Now all dquots should be invalidated, all writes done so we
2020 * should be only users of the info. No locks needed.
1da177e4
LT
2021 */
2022 if (info_dirty(&dqopt->info[cnt]))
2023 sb->dq_op->write_info(sb, cnt);
2024 if (dqopt->ops[cnt]->free_file_info)
2025 dqopt->ops[cnt]->free_file_info(sb, cnt);
2026 put_quota_format(dqopt->info[cnt].dqi_format);
2027
2028 toputinode[cnt] = dqopt->files[cnt];
f55abc0f 2029 if (!sb_has_quota_loaded(sb, cnt))
0ff5af83 2030 dqopt->files[cnt] = NULL;
1da177e4
LT
2031 dqopt->info[cnt].dqi_flags = 0;
2032 dqopt->info[cnt].dqi_igrace = 0;
2033 dqopt->info[cnt].dqi_bgrace = 0;
2034 dqopt->ops[cnt] = NULL;
2035 }
d3be915f 2036 mutex_unlock(&dqopt->dqonoff_mutex);
ca785ec6
JK
2037
2038 /* Skip syncing and setting flags if quota files are hidden */
2039 if (dqopt->flags & DQUOT_QUOTA_SYS_FILE)
2040 goto put_inodes;
2041
1da177e4 2042 /* Sync the superblock so that buffers with quota data are written to
7b7b1ace 2043 * disk (and so userspace sees correct data afterwards). */
1da177e4
LT
2044 if (sb->s_op->sync_fs)
2045 sb->s_op->sync_fs(sb, 1);
2046 sync_blockdev(sb->s_bdev);
2047 /* Now the quota files are just ordinary files and we can set the
2048 * inode flags back. Moreover we discard the pagecache so that
2049 * userspace sees the writes we did bypassing the pagecache. We
2050 * must also discard the blockdev buffers so that we see the
2051 * changes done by userspace on the next quotaon() */
2052 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
2053 if (toputinode[cnt]) {
d3be915f 2054 mutex_lock(&dqopt->dqonoff_mutex);
1da177e4
LT
2055 /* If quota was reenabled in the meantime, we have
2056 * nothing to do */
f55abc0f 2057 if (!sb_has_quota_loaded(sb, cnt)) {
a80b12c3 2058 mutex_lock(&toputinode[cnt]->i_mutex);
1da177e4
LT
2059 toputinode[cnt]->i_flags &= ~(S_IMMUTABLE |
2060 S_NOATIME | S_NOQUOTA);
268157ba
JK
2061 truncate_inode_pages(&toputinode[cnt]->i_data,
2062 0);
1b1dcc1b 2063 mutex_unlock(&toputinode[cnt]->i_mutex);
43d2932d 2064 mark_inode_dirty_sync(toputinode[cnt]);
1da177e4 2065 }
d3be915f 2066 mutex_unlock(&dqopt->dqonoff_mutex);
ca785ec6
JK
2067 }
2068 if (sb->s_bdev)
2069 invalidate_bdev(sb->s_bdev);
2070put_inodes:
2071 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
2072 if (toputinode[cnt]) {
0ff5af83 2073 /* On remount RO, we keep the inode pointer so that we
f55abc0f
JK
2074 * can reenable quota on the subsequent remount RW. We
2075 * have to check 'flags' variable and not use sb_has_
2076 * function because another quotaon / quotaoff could
2077 * change global state before we got here. We refuse
2078 * to suspend quotas when there is pending delete on
2079 * the quota file... */
2080 if (!(flags & DQUOT_SUSPENDED))
0ff5af83
JK
2081 iput(toputinode[cnt]);
2082 else if (!toputinode[cnt]->i_nlink)
2083 ret = -EBUSY;
1da177e4 2084 }
0ff5af83 2085 return ret;
1da177e4 2086}
0f0dd62f 2087EXPORT_SYMBOL(dquot_disable);
1da177e4 2088
287a8095 2089int dquot_quota_off(struct super_block *sb, int type)
f55abc0f 2090{
0f0dd62f
CH
2091 return dquot_disable(sb, type,
2092 DQUOT_USAGE_ENABLED | DQUOT_LIMITS_ENABLED);
f55abc0f 2093}
287a8095 2094EXPORT_SYMBOL(dquot_quota_off);
0f0dd62f 2095
1da177e4
LT
2096/*
2097 * Turn quotas on on a device
2098 */
2099
f55abc0f
JK
2100/*
2101 * Helper function to turn quotas on when we already have the inode of
2102 * quota file and no quota information is loaded.
2103 */
2104static int vfs_load_quota_inode(struct inode *inode, int type, int format_id,
2105 unsigned int flags)
1da177e4
LT
2106{
2107 struct quota_format_type *fmt = find_quota_format(format_id);
2108 struct super_block *sb = inode->i_sb;
2109 struct quota_info *dqopt = sb_dqopt(sb);
2110 int error;
2111 int oldflags = -1;
2112
2113 if (!fmt)
2114 return -ESRCH;
2115 if (!S_ISREG(inode->i_mode)) {
2116 error = -EACCES;
2117 goto out_fmt;
2118 }
2119 if (IS_RDONLY(inode)) {
2120 error = -EROFS;
2121 goto out_fmt;
2122 }
2123 if (!sb->s_op->quota_write || !sb->s_op->quota_read) {
2124 error = -EINVAL;
2125 goto out_fmt;
2126 }
f55abc0f
JK
2127 /* Usage always has to be set... */
2128 if (!(flags & DQUOT_USAGE_ENABLED)) {
2129 error = -EINVAL;
2130 goto out_fmt;
2131 }
1da177e4 2132
ca785ec6 2133 if (!(dqopt->flags & DQUOT_QUOTA_SYS_FILE)) {
ab94c39b
JK
2134 /* As we bypass the pagecache we must now flush all the
2135 * dirty data and invalidate caches so that kernel sees
2136 * changes from userspace. It is not enough to just flush
2137 * the quota file since if blocksize < pagesize, invalidation
2138 * of the cache could fail because of other unrelated dirty
2139 * data */
2140 sync_filesystem(sb);
ca785ec6
JK
2141 invalidate_bdev(sb->s_bdev);
2142 }
d3be915f 2143 mutex_lock(&dqopt->dqonoff_mutex);
f55abc0f 2144 if (sb_has_quota_loaded(sb, type)) {
1da177e4
LT
2145 error = -EBUSY;
2146 goto out_lock;
2147 }
ca785ec6
JK
2148
2149 if (!(dqopt->flags & DQUOT_QUOTA_SYS_FILE)) {
2150 /* We don't want quota and atime on quota files (deadlocks
2151 * possible) Also nobody should write to the file - we use
2152 * special IO operations which ignore the immutable bit. */
a80b12c3 2153 mutex_lock(&inode->i_mutex);
268157ba
JK
2154 oldflags = inode->i_flags & (S_NOATIME | S_IMMUTABLE |
2155 S_NOQUOTA);
ca785ec6 2156 inode->i_flags |= S_NOQUOTA | S_NOATIME | S_IMMUTABLE;
dee86565 2157 mutex_unlock(&inode->i_mutex);
26245c94
JK
2158 /*
2159 * When S_NOQUOTA is set, remove dquot references as no more
2160 * references can be added
2161 */
9f754758 2162 __dquot_drop(inode);
ca785ec6 2163 }
1da177e4
LT
2164
2165 error = -EIO;
2166 dqopt->files[type] = igrab(inode);
2167 if (!dqopt->files[type])
2168 goto out_lock;
2169 error = -EINVAL;
2170 if (!fmt->qf_ops->check_quota_file(sb, type))
2171 goto out_file_init;
2172
2173 dqopt->ops[type] = fmt->qf_ops;
2174 dqopt->info[type].dqi_format = fmt;
0ff5af83 2175 dqopt->info[type].dqi_fmt_id = format_id;
1da177e4 2176 INIT_LIST_HEAD(&dqopt->info[type].dqi_dirty_list);
d3be915f 2177 mutex_lock(&dqopt->dqio_mutex);
268157ba
JK
2178 error = dqopt->ops[type]->read_file_info(sb, type);
2179 if (error < 0) {
d3be915f 2180 mutex_unlock(&dqopt->dqio_mutex);
1da177e4
LT
2181 goto out_file_init;
2182 }
46fe44ce
JK
2183 if (dqopt->flags & DQUOT_QUOTA_SYS_FILE)
2184 dqopt->info[type].dqi_flags |= DQF_SYS_FILE;
d3be915f 2185 mutex_unlock(&dqopt->dqio_mutex);
cc33412f 2186 spin_lock(&dq_state_lock);
f55abc0f 2187 dqopt->flags |= dquot_state_flag(flags, type);
cc33412f 2188 spin_unlock(&dq_state_lock);
1da177e4
LT
2189
2190 add_dquot_ref(sb, type);
d3be915f 2191 mutex_unlock(&dqopt->dqonoff_mutex);
1da177e4
LT
2192
2193 return 0;
2194
2195out_file_init:
2196 dqopt->files[type] = NULL;
2197 iput(inode);
2198out_lock:
1da177e4 2199 if (oldflags != -1) {
a80b12c3 2200 mutex_lock(&inode->i_mutex);
1da177e4
LT
2201 /* Set the flags back (in the case of accidental quotaon()
2202 * on a wrong file we don't want to mess up the flags) */
2203 inode->i_flags &= ~(S_NOATIME | S_NOQUOTA | S_IMMUTABLE);
2204 inode->i_flags |= oldflags;
dee86565 2205 mutex_unlock(&inode->i_mutex);
1da177e4 2206 }
d01730d7 2207 mutex_unlock(&dqopt->dqonoff_mutex);
1da177e4
LT
2208out_fmt:
2209 put_quota_format(fmt);
2210
2211 return error;
2212}
2213
0ff5af83 2214/* Reenable quotas on remount RW */
0f0dd62f 2215int dquot_resume(struct super_block *sb, int type)
0ff5af83
JK
2216{
2217 struct quota_info *dqopt = sb_dqopt(sb);
2218 struct inode *inode;
0f0dd62f 2219 int ret = 0, cnt;
f55abc0f 2220 unsigned int flags;
0ff5af83 2221
0f0dd62f
CH
2222 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
2223 if (type != -1 && cnt != type)
2224 continue;
2225
2226 mutex_lock(&dqopt->dqonoff_mutex);
2227 if (!sb_has_quota_suspended(sb, cnt)) {
2228 mutex_unlock(&dqopt->dqonoff_mutex);
2229 continue;
2230 }
2231 inode = dqopt->files[cnt];
2232 dqopt->files[cnt] = NULL;
2233 spin_lock(&dq_state_lock);
2234 flags = dqopt->flags & dquot_state_flag(DQUOT_USAGE_ENABLED |
2235 DQUOT_LIMITS_ENABLED,
2236 cnt);
2237 dqopt->flags &= ~dquot_state_flag(DQUOT_STATE_FLAGS, cnt);
2238 spin_unlock(&dq_state_lock);
0ff5af83 2239 mutex_unlock(&dqopt->dqonoff_mutex);
0ff5af83 2240
0f0dd62f
CH
2241 flags = dquot_generic_flag(flags, cnt);
2242 ret = vfs_load_quota_inode(inode, cnt,
2243 dqopt->info[cnt].dqi_fmt_id, flags);
2244 iput(inode);
2245 }
0ff5af83
JK
2246
2247 return ret;
2248}
0f0dd62f 2249EXPORT_SYMBOL(dquot_resume);
0ff5af83 2250
f00c9e44
JK
2251int dquot_quota_on(struct super_block *sb, int type, int format_id,
2252 struct path *path)
77e69dac
AV
2253{
2254 int error = security_quota_on(path->dentry);
2255 if (error)
2256 return error;
2257 /* Quota file not on the same filesystem? */
d8c9584e 2258 if (path->dentry->d_sb != sb)
77e69dac
AV
2259 error = -EXDEV;
2260 else
f55abc0f
JK
2261 error = vfs_load_quota_inode(path->dentry->d_inode, type,
2262 format_id, DQUOT_USAGE_ENABLED |
2263 DQUOT_LIMITS_ENABLED);
77e69dac
AV
2264 return error;
2265}
287a8095 2266EXPORT_SYMBOL(dquot_quota_on);
1da177e4 2267
f55abc0f
JK
2268/*
2269 * More powerful function for turning on quotas allowing setting
2270 * of individual quota flags
2271 */
287a8095
CH
2272int dquot_enable(struct inode *inode, int type, int format_id,
2273 unsigned int flags)
f55abc0f
JK
2274{
2275 int ret = 0;
2276 struct super_block *sb = inode->i_sb;
2277 struct quota_info *dqopt = sb_dqopt(sb);
2278
2279 /* Just unsuspend quotas? */
0f0dd62f
CH
2280 BUG_ON(flags & DQUOT_SUSPENDED);
2281
f55abc0f
JK
2282 if (!flags)
2283 return 0;
2284 /* Just updating flags needed? */
2285 if (sb_has_quota_loaded(sb, type)) {
2286 mutex_lock(&dqopt->dqonoff_mutex);
2287 /* Now do a reliable test... */
2288 if (!sb_has_quota_loaded(sb, type)) {
2289 mutex_unlock(&dqopt->dqonoff_mutex);
2290 goto load_quota;
2291 }
2292 if (flags & DQUOT_USAGE_ENABLED &&
2293 sb_has_quota_usage_enabled(sb, type)) {
2294 ret = -EBUSY;
2295 goto out_lock;
2296 }
2297 if (flags & DQUOT_LIMITS_ENABLED &&
2298 sb_has_quota_limits_enabled(sb, type)) {
2299 ret = -EBUSY;
2300 goto out_lock;
2301 }
cc33412f 2302 spin_lock(&dq_state_lock);
f55abc0f 2303 sb_dqopt(sb)->flags |= dquot_state_flag(flags, type);
cc33412f 2304 spin_unlock(&dq_state_lock);
f55abc0f
JK
2305out_lock:
2306 mutex_unlock(&dqopt->dqonoff_mutex);
2307 return ret;
2308 }
2309
2310load_quota:
2311 return vfs_load_quota_inode(inode, type, format_id, flags);
2312}
287a8095 2313EXPORT_SYMBOL(dquot_enable);
f55abc0f 2314
1da177e4
LT
2315/*
2316 * This function is used when filesystem needs to initialize quotas
2317 * during mount time.
2318 */
287a8095 2319int dquot_quota_on_mount(struct super_block *sb, char *qf_name,
84de856e 2320 int format_id, int type)
1da177e4 2321{
84de856e 2322 struct dentry *dentry;
1da177e4
LT
2323 int error;
2324
c56818d7 2325 mutex_lock(&sb->s_root->d_inode->i_mutex);
2fa389c5 2326 dentry = lookup_one_len(qf_name, sb->s_root, strlen(qf_name));
c56818d7 2327 mutex_unlock(&sb->s_root->d_inode->i_mutex);
84de856e
CH
2328 if (IS_ERR(dentry))
2329 return PTR_ERR(dentry);
2330
154f484b
JK
2331 if (!dentry->d_inode) {
2332 error = -ENOENT;
2333 goto out;
2334 }
2335
1da177e4 2336 error = security_quota_on(dentry);
84de856e 2337 if (!error)
f55abc0f
JK
2338 error = vfs_load_quota_inode(dentry->d_inode, type, format_id,
2339 DQUOT_USAGE_ENABLED | DQUOT_LIMITS_ENABLED);
84de856e 2340
154f484b 2341out:
84de856e
CH
2342 dput(dentry);
2343 return error;
1da177e4 2344}
287a8095 2345EXPORT_SYMBOL(dquot_quota_on_mount);
b85f4b87 2346
12095460
JK
2347static inline qsize_t qbtos(qsize_t blocks)
2348{
2349 return blocks << QIF_DQBLKSIZE_BITS;
2350}
2351
2352static inline qsize_t stoqb(qsize_t space)
2353{
2354 return (space + QIF_DQBLKSIZE - 1) >> QIF_DQBLKSIZE_BITS;
2355}
2356
1da177e4 2357/* Generic routine for getting common part of quota structure */
b9b2dd36 2358static void do_get_dqblk(struct dquot *dquot, struct fs_disk_quota *di)
1da177e4
LT
2359{
2360 struct mem_dqblk *dm = &dquot->dq_dqb;
2361
b9b2dd36
CH
2362 memset(di, 0, sizeof(*di));
2363 di->d_version = FS_DQUOT_VERSION;
4c376dca 2364 di->d_flags = dquot->dq_id.type == USRQUOTA ?
ade7ce31 2365 FS_USER_QUOTA : FS_GROUP_QUOTA;
4c376dca 2366 di->d_id = from_kqid_munged(current_user_ns(), dquot->dq_id);
b9b2dd36 2367
1da177e4 2368 spin_lock(&dq_data_lock);
b9b2dd36
CH
2369 di->d_blk_hardlimit = stoqb(dm->dqb_bhardlimit);
2370 di->d_blk_softlimit = stoqb(dm->dqb_bsoftlimit);
2371 di->d_ino_hardlimit = dm->dqb_ihardlimit;
2372 di->d_ino_softlimit = dm->dqb_isoftlimit;
2373 di->d_bcount = dm->dqb_curspace + dm->dqb_rsvspace;
2374 di->d_icount = dm->dqb_curinodes;
2375 di->d_btimer = dm->dqb_btime;
2376 di->d_itimer = dm->dqb_itime;
1da177e4
LT
2377 spin_unlock(&dq_data_lock);
2378}
2379
74a8a103 2380int dquot_get_dqblk(struct super_block *sb, struct kqid qid,
287a8095 2381 struct fs_disk_quota *di)
1da177e4
LT
2382{
2383 struct dquot *dquot;
2384
aca645a6 2385 dquot = dqget(sb, qid);
dd6f3c6d 2386 if (!dquot)
1da177e4 2387 return -ESRCH;
1da177e4
LT
2388 do_get_dqblk(dquot, di);
2389 dqput(dquot);
cc33412f 2390
1da177e4
LT
2391 return 0;
2392}
287a8095 2393EXPORT_SYMBOL(dquot_get_dqblk);
1da177e4 2394
c472b432
CH
2395#define VFS_FS_DQ_MASK \
2396 (FS_DQ_BCOUNT | FS_DQ_BSOFT | FS_DQ_BHARD | \
2397 FS_DQ_ICOUNT | FS_DQ_ISOFT | FS_DQ_IHARD | \
2398 FS_DQ_BTIMER | FS_DQ_ITIMER)
2399
1da177e4 2400/* Generic routine for setting common part of quota structure */
c472b432 2401static int do_set_dqblk(struct dquot *dquot, struct fs_disk_quota *di)
1da177e4
LT
2402{
2403 struct mem_dqblk *dm = &dquot->dq_dqb;
2404 int check_blim = 0, check_ilim = 0;
4c376dca 2405 struct mem_dqinfo *dqi = &sb_dqopt(dquot->dq_sb)->info[dquot->dq_id.type];
338bf9af 2406
c472b432
CH
2407 if (di->d_fieldmask & ~VFS_FS_DQ_MASK)
2408 return -EINVAL;
2409
2410 if (((di->d_fieldmask & FS_DQ_BSOFT) &&
2411 (di->d_blk_softlimit > dqi->dqi_maxblimit)) ||
2412 ((di->d_fieldmask & FS_DQ_BHARD) &&
2413 (di->d_blk_hardlimit > dqi->dqi_maxblimit)) ||
2414 ((di->d_fieldmask & FS_DQ_ISOFT) &&
2415 (di->d_ino_softlimit > dqi->dqi_maxilimit)) ||
2416 ((di->d_fieldmask & FS_DQ_IHARD) &&
2417 (di->d_ino_hardlimit > dqi->dqi_maxilimit)))
338bf9af 2418 return -ERANGE;
1da177e4
LT
2419
2420 spin_lock(&dq_data_lock);
c472b432
CH
2421 if (di->d_fieldmask & FS_DQ_BCOUNT) {
2422 dm->dqb_curspace = di->d_bcount - dm->dqb_rsvspace;
1da177e4 2423 check_blim = 1;
08261673 2424 set_bit(DQ_LASTSET_B + QIF_SPACE_B, &dquot->dq_flags);
1da177e4 2425 }
c472b432
CH
2426
2427 if (di->d_fieldmask & FS_DQ_BSOFT)
2428 dm->dqb_bsoftlimit = qbtos(di->d_blk_softlimit);
2429 if (di->d_fieldmask & FS_DQ_BHARD)
2430 dm->dqb_bhardlimit = qbtos(di->d_blk_hardlimit);
2431 if (di->d_fieldmask & (FS_DQ_BSOFT | FS_DQ_BHARD)) {
1da177e4 2432 check_blim = 1;
08261673 2433 set_bit(DQ_LASTSET_B + QIF_BLIMITS_B, &dquot->dq_flags);
1da177e4 2434 }
c472b432
CH
2435
2436 if (di->d_fieldmask & FS_DQ_ICOUNT) {
2437 dm->dqb_curinodes = di->d_icount;
1da177e4 2438 check_ilim = 1;
08261673 2439 set_bit(DQ_LASTSET_B + QIF_INODES_B, &dquot->dq_flags);
1da177e4 2440 }
c472b432
CH
2441
2442 if (di->d_fieldmask & FS_DQ_ISOFT)
2443 dm->dqb_isoftlimit = di->d_ino_softlimit;
2444 if (di->d_fieldmask & FS_DQ_IHARD)
2445 dm->dqb_ihardlimit = di->d_ino_hardlimit;
2446 if (di->d_fieldmask & (FS_DQ_ISOFT | FS_DQ_IHARD)) {
1da177e4 2447 check_ilim = 1;
08261673 2448 set_bit(DQ_LASTSET_B + QIF_ILIMITS_B, &dquot->dq_flags);
1da177e4 2449 }
c472b432
CH
2450
2451 if (di->d_fieldmask & FS_DQ_BTIMER) {
2452 dm->dqb_btime = di->d_btimer;
e04a88a9 2453 check_blim = 1;
08261673 2454 set_bit(DQ_LASTSET_B + QIF_BTIME_B, &dquot->dq_flags);
4d59bce4 2455 }
c472b432
CH
2456
2457 if (di->d_fieldmask & FS_DQ_ITIMER) {
2458 dm->dqb_itime = di->d_itimer;
e04a88a9 2459 check_ilim = 1;
08261673 2460 set_bit(DQ_LASTSET_B + QIF_ITIME_B, &dquot->dq_flags);
4d59bce4 2461 }
1da177e4
LT
2462
2463 if (check_blim) {
268157ba
JK
2464 if (!dm->dqb_bsoftlimit ||
2465 dm->dqb_curspace < dm->dqb_bsoftlimit) {
1da177e4
LT
2466 dm->dqb_btime = 0;
2467 clear_bit(DQ_BLKS_B, &dquot->dq_flags);
c472b432 2468 } else if (!(di->d_fieldmask & FS_DQ_BTIMER))
268157ba 2469 /* Set grace only if user hasn't provided his own... */
338bf9af 2470 dm->dqb_btime = get_seconds() + dqi->dqi_bgrace;
1da177e4
LT
2471 }
2472 if (check_ilim) {
268157ba
JK
2473 if (!dm->dqb_isoftlimit ||
2474 dm->dqb_curinodes < dm->dqb_isoftlimit) {
1da177e4
LT
2475 dm->dqb_itime = 0;
2476 clear_bit(DQ_INODES_B, &dquot->dq_flags);
c472b432 2477 } else if (!(di->d_fieldmask & FS_DQ_ITIMER))
268157ba 2478 /* Set grace only if user hasn't provided his own... */
338bf9af 2479 dm->dqb_itime = get_seconds() + dqi->dqi_igrace;
1da177e4 2480 }
268157ba
JK
2481 if (dm->dqb_bhardlimit || dm->dqb_bsoftlimit || dm->dqb_ihardlimit ||
2482 dm->dqb_isoftlimit)
1da177e4
LT
2483 clear_bit(DQ_FAKE_B, &dquot->dq_flags);
2484 else
2485 set_bit(DQ_FAKE_B, &dquot->dq_flags);
2486 spin_unlock(&dq_data_lock);
2487 mark_dquot_dirty(dquot);
338bf9af
AP
2488
2489 return 0;
1da177e4
LT
2490}
2491
74a8a103 2492int dquot_set_dqblk(struct super_block *sb, struct kqid qid,
c472b432 2493 struct fs_disk_quota *di)
1da177e4
LT
2494{
2495 struct dquot *dquot;
338bf9af 2496 int rc;
1da177e4 2497
aca645a6 2498 dquot = dqget(sb, qid);
f55abc0f
JK
2499 if (!dquot) {
2500 rc = -ESRCH;
2501 goto out;
1da177e4 2502 }
338bf9af 2503 rc = do_set_dqblk(dquot, di);
1da177e4 2504 dqput(dquot);
f55abc0f 2505out:
338bf9af 2506 return rc;
1da177e4 2507}
287a8095 2508EXPORT_SYMBOL(dquot_set_dqblk);
1da177e4
LT
2509
2510/* Generic routine for getting common part of quota file information */
287a8095 2511int dquot_get_dqinfo(struct super_block *sb, int type, struct if_dqinfo *ii)
1da177e4
LT
2512{
2513 struct mem_dqinfo *mi;
2514
d3be915f 2515 mutex_lock(&sb_dqopt(sb)->dqonoff_mutex);
f55abc0f 2516 if (!sb_has_quota_active(sb, type)) {
d3be915f 2517 mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
1da177e4
LT
2518 return -ESRCH;
2519 }
2520 mi = sb_dqopt(sb)->info + type;
2521 spin_lock(&dq_data_lock);
2522 ii->dqi_bgrace = mi->dqi_bgrace;
2523 ii->dqi_igrace = mi->dqi_igrace;
46fe44ce 2524 ii->dqi_flags = mi->dqi_flags & DQF_GETINFO_MASK;
1da177e4
LT
2525 ii->dqi_valid = IIF_ALL;
2526 spin_unlock(&dq_data_lock);
d3be915f 2527 mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
1da177e4
LT
2528 return 0;
2529}
287a8095 2530EXPORT_SYMBOL(dquot_get_dqinfo);
1da177e4
LT
2531
2532/* Generic routine for setting common part of quota file information */
287a8095 2533int dquot_set_dqinfo(struct super_block *sb, int type, struct if_dqinfo *ii)
1da177e4
LT
2534{
2535 struct mem_dqinfo *mi;
f55abc0f 2536 int err = 0;
1da177e4 2537
d3be915f 2538 mutex_lock(&sb_dqopt(sb)->dqonoff_mutex);
f55abc0f
JK
2539 if (!sb_has_quota_active(sb, type)) {
2540 err = -ESRCH;
2541 goto out;
1da177e4
LT
2542 }
2543 mi = sb_dqopt(sb)->info + type;
2544 spin_lock(&dq_data_lock);
2545 if (ii->dqi_valid & IIF_BGRACE)
2546 mi->dqi_bgrace = ii->dqi_bgrace;
2547 if (ii->dqi_valid & IIF_IGRACE)
2548 mi->dqi_igrace = ii->dqi_igrace;
2549 if (ii->dqi_valid & IIF_FLAGS)
46fe44ce
JK
2550 mi->dqi_flags = (mi->dqi_flags & ~DQF_SETINFO_MASK) |
2551 (ii->dqi_flags & DQF_SETINFO_MASK);
1da177e4
LT
2552 spin_unlock(&dq_data_lock);
2553 mark_info_dirty(sb, type);
2554 /* Force write to disk */
2555 sb->dq_op->write_info(sb, type);
f55abc0f 2556out:
d3be915f 2557 mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
f55abc0f 2558 return err;
1da177e4 2559}
287a8095 2560EXPORT_SYMBOL(dquot_set_dqinfo);
1da177e4 2561
287a8095
CH
2562const struct quotactl_ops dquot_quotactl_ops = {
2563 .quota_on = dquot_quota_on,
2564 .quota_off = dquot_quota_off,
2565 .quota_sync = dquot_quota_sync,
2566 .get_info = dquot_get_dqinfo,
2567 .set_info = dquot_set_dqinfo,
2568 .get_dqblk = dquot_get_dqblk,
2569 .set_dqblk = dquot_set_dqblk
1da177e4 2570};
287a8095 2571EXPORT_SYMBOL(dquot_quotactl_ops);
dde95888
DM
2572
2573static int do_proc_dqstats(struct ctl_table *table, int write,
2574 void __user *buffer, size_t *lenp, loff_t *ppos)
2575{
dde95888 2576 unsigned int type = (int *)table->data - dqstats.stat;
f32764bd
DM
2577
2578 /* Update global table */
2579 dqstats.stat[type] =
2580 percpu_counter_sum_positive(&dqstats.counter[type]);
dde95888
DM
2581 return proc_dointvec(table, write, buffer, lenp, ppos);
2582}
2583
1da177e4
LT
2584static ctl_table fs_dqstats_table[] = {
2585 {
1da177e4 2586 .procname = "lookups",
dde95888 2587 .data = &dqstats.stat[DQST_LOOKUPS],
1da177e4
LT
2588 .maxlen = sizeof(int),
2589 .mode = 0444,
dde95888 2590 .proc_handler = do_proc_dqstats,
1da177e4
LT
2591 },
2592 {
1da177e4 2593 .procname = "drops",
dde95888 2594 .data = &dqstats.stat[DQST_DROPS],
1da177e4
LT
2595 .maxlen = sizeof(int),
2596 .mode = 0444,
dde95888 2597 .proc_handler = do_proc_dqstats,
1da177e4
LT
2598 },
2599 {
1da177e4 2600 .procname = "reads",
dde95888 2601 .data = &dqstats.stat[DQST_READS],
1da177e4
LT
2602 .maxlen = sizeof(int),
2603 .mode = 0444,
dde95888 2604 .proc_handler = do_proc_dqstats,
1da177e4
LT
2605 },
2606 {
1da177e4 2607 .procname = "writes",
dde95888 2608 .data = &dqstats.stat[DQST_WRITES],
1da177e4
LT
2609 .maxlen = sizeof(int),
2610 .mode = 0444,
dde95888 2611 .proc_handler = do_proc_dqstats,
1da177e4
LT
2612 },
2613 {
1da177e4 2614 .procname = "cache_hits",
dde95888 2615 .data = &dqstats.stat[DQST_CACHE_HITS],
1da177e4
LT
2616 .maxlen = sizeof(int),
2617 .mode = 0444,
dde95888 2618 .proc_handler = do_proc_dqstats,
1da177e4
LT
2619 },
2620 {
1da177e4 2621 .procname = "allocated_dquots",
dde95888 2622 .data = &dqstats.stat[DQST_ALLOC_DQUOTS],
1da177e4
LT
2623 .maxlen = sizeof(int),
2624 .mode = 0444,
dde95888 2625 .proc_handler = do_proc_dqstats,
1da177e4
LT
2626 },
2627 {
1da177e4 2628 .procname = "free_dquots",
dde95888 2629 .data = &dqstats.stat[DQST_FREE_DQUOTS],
1da177e4
LT
2630 .maxlen = sizeof(int),
2631 .mode = 0444,
dde95888 2632 .proc_handler = do_proc_dqstats,
1da177e4
LT
2633 },
2634 {
1da177e4 2635 .procname = "syncs",
dde95888 2636 .data = &dqstats.stat[DQST_SYNCS],
1da177e4
LT
2637 .maxlen = sizeof(int),
2638 .mode = 0444,
dde95888 2639 .proc_handler = do_proc_dqstats,
1da177e4 2640 },
8e893469 2641#ifdef CONFIG_PRINT_QUOTA_WARNING
1da177e4 2642 {
1da177e4
LT
2643 .procname = "warnings",
2644 .data = &flag_print_warnings,
2645 .maxlen = sizeof(int),
2646 .mode = 0644,
6d456111 2647 .proc_handler = proc_dointvec,
1da177e4 2648 },
8e893469 2649#endif
ab09203e 2650 { },
1da177e4
LT
2651};
2652
2653static ctl_table fs_table[] = {
2654 {
1da177e4
LT
2655 .procname = "quota",
2656 .mode = 0555,
2657 .child = fs_dqstats_table,
2658 },
ab09203e 2659 { },
1da177e4
LT
2660};
2661
2662static ctl_table sys_table[] = {
2663 {
1da177e4
LT
2664 .procname = "fs",
2665 .mode = 0555,
2666 .child = fs_table,
2667 },
ab09203e 2668 { },
1da177e4
LT
2669};
2670
2671static int __init dquot_init(void)
2672{
f32764bd 2673 int i, ret;
1da177e4
LT
2674 unsigned long nr_hash, order;
2675
2676 printk(KERN_NOTICE "VFS: Disk quotas %s\n", __DQUOT_VERSION__);
2677
0b4d4147 2678 register_sysctl_table(sys_table);
1da177e4 2679
20c2df83 2680 dquot_cachep = kmem_cache_create("dquot",
1da177e4 2681 sizeof(struct dquot), sizeof(unsigned long) * 4,
fffb60f9
PJ
2682 (SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT|
2683 SLAB_MEM_SPREAD|SLAB_PANIC),
20c2df83 2684 NULL);
1da177e4
LT
2685
2686 order = 0;
2687 dquot_hash = (struct hlist_head *)__get_free_pages(GFP_ATOMIC, order);
2688 if (!dquot_hash)
2689 panic("Cannot create dquot hash table");
2690
f32764bd
DM
2691 for (i = 0; i < _DQST_DQSTAT_LAST; i++) {
2692 ret = percpu_counter_init(&dqstats.counter[i], 0);
2693 if (ret)
2694 panic("Cannot create dquot stat counters");
2695 }
dde95888 2696
1da177e4
LT
2697 /* Find power-of-two hlist_heads which can fit into allocation */
2698 nr_hash = (1UL << order) * PAGE_SIZE / sizeof(struct hlist_head);
2699 dq_hash_bits = 0;
2700 do {
2701 dq_hash_bits++;
2702 } while (nr_hash >> dq_hash_bits);
2703 dq_hash_bits--;
2704
2705 nr_hash = 1UL << dq_hash_bits;
2706 dq_hash_mask = nr_hash - 1;
2707 for (i = 0; i < nr_hash; i++)
2708 INIT_HLIST_HEAD(dquot_hash + i);
2709
2710 printk("Dquot-cache hash table entries: %ld (order %ld, %ld bytes)\n",
2711 nr_hash, order, (PAGE_SIZE << order));
2712
8e1f936b 2713 register_shrinker(&dqcache_shrinker);
1da177e4
LT
2714
2715 return 0;
2716}
2717module_init(dquot_init);
This page took 1.366831 seconds and 4 git commands to generate.