quota: Unexport dqblk_v1.h and dqblk_v2.h
[linux-2.6-block.git] / fs / 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 <mvw@planets.elm.net>
13 *
14 * Fixes: Dmitry Gorodchanin <pgmdsg@ibi.com>, 11 Feb 96
15 *
16 * Revised list management to avoid races
17 * -- Bill Hawes, <whawes@star.net>, 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, <jack@suse.cz>, 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, <jack@suse.cz>, 9/2000
39 *
40 * Added dynamic quota structure allocation
41 * Jan Kara <jack@suse.cz> 12/2000
42 *
43 * Rewritten quota interface. Implemented new quota format and
44 * formats registering.
45 * Jan Kara, <jack@suse.cz>, 2001,2002
46 *
47 * New SMP locking.
48 * Jan Kara, <jack@suse.cz>, 10/2002
49 *
50 * Added journalled quota support, fix lock inversion problems
51 * Jan Kara, <jack@suse.cz>, 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>
74#include <linux/kmod.h>
75#include <linux/namei.h>
76#include <linux/buffer_head.h>
16f7e0fe 77#include <linux/capability.h>
be586bab 78#include <linux/quotaops.h>
fb58b731 79#include <linux/writeback.h> /* for inode_lock, oddly enough.. */
8e893469
JK
80#ifdef CONFIG_QUOTA_NETLINK_INTERFACE
81#include <net/netlink.h>
82#include <net/genetlink.h>
83#endif
1da177e4
LT
84
85#include <asm/uaccess.h>
86
87#define __DQUOT_PARANOIA
88
89/*
90 * There are two quota SMP locks. dq_list_lock protects all lists with quotas
91 * and quota formats and also dqstats structure containing statistics about the
92 * lists. dq_data_lock protects data from dq_dqb and also mem_dqinfo structures
93 * and also guards consistency of dquot->dq_dqb with inode->i_blocks, i_bytes.
94 * i_blocks and i_bytes updates itself are guarded by i_lock acquired directly
95 * in inode_add_bytes() and inode_sub_bytes().
96 *
97 * The spinlock ordering is hence: dq_data_lock > dq_list_lock > i_lock
98 *
99 * Note that some things (eg. sb pointer, type, id) doesn't change during
100 * the life of the dquot structure and so needn't to be protected by a lock
101 *
102 * Any operation working on dquots via inode pointers must hold dqptr_sem. If
103 * operation is just reading pointers from inode (or not using them at all) the
104 * read lock is enough. If pointers are altered function must hold write lock
105 * (these locking rules also apply for S_NOQUOTA flag in the inode - note that
1b1dcc1b 106 * for altering the flag i_mutex is also needed). If operation is holding
1da177e4 107 * reference to dquot in other way (e.g. quotactl ops) it must be guarded by
d3be915f 108 * dqonoff_mutex.
1da177e4
LT
109 * This locking assures that:
110 * a) update/access to dquot pointers in inode is serialized
111 * b) everyone is guarded against invalidate_dquots()
112 *
d3be915f 113 * Each dquot has its dq_lock mutex. Locked dquots might not be referenced
1da177e4
LT
114 * from inodes (dquot_alloc_space() and such don't check the dq_lock).
115 * Currently dquot is locked only when it is being read to memory (or space for
116 * it is being allocated) on the first dqget() and when it is being released on
117 * the last dqput(). The allocation and release oparations are serialized by
118 * the dq_lock and by checking the use count in dquot_release(). Write
119 * operations on dquots don't hold dq_lock as they copy data under dq_data_lock
120 * spinlock to internal buffers before writing.
121 *
122 * Lock ordering (including related VFS locks) is the following:
d3be915f
IM
123 * i_mutex > dqonoff_sem > journal_lock > dqptr_sem > dquot->dq_lock >
124 * dqio_mutex
125 * i_mutex on quota files is special (it's below dqio_mutex)
1da177e4
LT
126 */
127
128static DEFINE_SPINLOCK(dq_list_lock);
129DEFINE_SPINLOCK(dq_data_lock);
130
131static char *quotatypes[] = INITQFNAMES;
132static struct quota_format_type *quota_formats; /* List of registered formats */
133static struct quota_module_name module_names[] = INIT_QUOTA_MODULE_NAMES;
134
135/* SLAB cache for dquot structures */
e18b890b 136static struct kmem_cache *dquot_cachep;
1da177e4
LT
137
138int register_quota_format(struct quota_format_type *fmt)
139{
140 spin_lock(&dq_list_lock);
141 fmt->qf_next = quota_formats;
142 quota_formats = fmt;
143 spin_unlock(&dq_list_lock);
144 return 0;
145}
146
147void unregister_quota_format(struct quota_format_type *fmt)
148{
149 struct quota_format_type **actqf;
150
151 spin_lock(&dq_list_lock);
152 for (actqf = &quota_formats; *actqf && *actqf != fmt; actqf = &(*actqf)->qf_next);
153 if (*actqf)
154 *actqf = (*actqf)->qf_next;
155 spin_unlock(&dq_list_lock);
156}
157
158static struct quota_format_type *find_quota_format(int id)
159{
160 struct quota_format_type *actqf;
161
162 spin_lock(&dq_list_lock);
163 for (actqf = quota_formats; actqf && actqf->qf_fmt_id != id; actqf = actqf->qf_next);
164 if (!actqf || !try_module_get(actqf->qf_owner)) {
165 int qm;
166
167 spin_unlock(&dq_list_lock);
168
169 for (qm = 0; module_names[qm].qm_fmt_id && module_names[qm].qm_fmt_id != id; qm++);
170 if (!module_names[qm].qm_fmt_id || request_module(module_names[qm].qm_mod_name))
171 return NULL;
172
173 spin_lock(&dq_list_lock);
174 for (actqf = quota_formats; actqf && actqf->qf_fmt_id != id; actqf = actqf->qf_next);
175 if (actqf && !try_module_get(actqf->qf_owner))
176 actqf = NULL;
177 }
178 spin_unlock(&dq_list_lock);
179 return actqf;
180}
181
182static void put_quota_format(struct quota_format_type *fmt)
183{
184 module_put(fmt->qf_owner);
185}
186
187/*
188 * Dquot List Management:
189 * The quota code uses three lists for dquot management: the inuse_list,
190 * free_dquots, and dquot_hash[] array. A single dquot structure may be
191 * on all three lists, depending on its current state.
192 *
193 * All dquots are placed to the end of inuse_list when first created, and this
194 * list is used for invalidate operation, which must look at every dquot.
195 *
196 * Unused dquots (dq_count == 0) are added to the free_dquots list when freed,
197 * and this list is searched whenever we need an available dquot. Dquots are
198 * removed from the list as soon as they are used again, and
199 * dqstats.free_dquots gives the number of dquots on the list. When
200 * dquot is invalidated it's completely released from memory.
201 *
202 * Dquots with a specific identity (device, type and id) are placed on
203 * one of the dquot_hash[] hash chains. The provides an efficient search
204 * mechanism to locate a specific dquot.
205 */
206
207static LIST_HEAD(inuse_list);
208static LIST_HEAD(free_dquots);
209static unsigned int dq_hash_bits, dq_hash_mask;
210static struct hlist_head *dquot_hash;
211
212struct dqstats dqstats;
213
1da177e4
LT
214static inline unsigned int
215hashfn(const struct super_block *sb, unsigned int id, int type)
216{
217 unsigned long tmp;
218
219 tmp = (((unsigned long)sb>>L1_CACHE_SHIFT) ^ id) * (MAXQUOTAS - type);
220 return (tmp + (tmp >> dq_hash_bits)) & dq_hash_mask;
221}
222
223/*
224 * Following list functions expect dq_list_lock to be held
225 */
226static inline void insert_dquot_hash(struct dquot *dquot)
227{
228 struct hlist_head *head = dquot_hash + hashfn(dquot->dq_sb, dquot->dq_id, dquot->dq_type);
229 hlist_add_head(&dquot->dq_hash, head);
230}
231
232static inline void remove_dquot_hash(struct dquot *dquot)
233{
234 hlist_del_init(&dquot->dq_hash);
235}
236
237static inline struct dquot *find_dquot(unsigned int hashent, struct super_block *sb, unsigned int id, int type)
238{
239 struct hlist_node *node;
240 struct dquot *dquot;
241
242 hlist_for_each (node, dquot_hash+hashent) {
243 dquot = hlist_entry(node, struct dquot, dq_hash);
244 if (dquot->dq_sb == sb && dquot->dq_id == id && dquot->dq_type == type)
245 return dquot;
246 }
247 return NODQUOT;
248}
249
250/* Add a dquot to the tail of the free list */
251static inline void put_dquot_last(struct dquot *dquot)
252{
8e13059a 253 list_add_tail(&dquot->dq_free, &free_dquots);
1da177e4
LT
254 dqstats.free_dquots++;
255}
256
257static inline void remove_free_dquot(struct dquot *dquot)
258{
259 if (list_empty(&dquot->dq_free))
260 return;
261 list_del_init(&dquot->dq_free);
262 dqstats.free_dquots--;
263}
264
265static inline void put_inuse(struct dquot *dquot)
266{
267 /* We add to the back of inuse list so we don't have to restart
268 * when traversing this list and we block */
8e13059a 269 list_add_tail(&dquot->dq_inuse, &inuse_list);
1da177e4
LT
270 dqstats.allocated_dquots++;
271}
272
273static inline void remove_inuse(struct dquot *dquot)
274{
275 dqstats.allocated_dquots--;
276 list_del(&dquot->dq_inuse);
277}
278/*
279 * End of list functions needing dq_list_lock
280 */
281
282static void wait_on_dquot(struct dquot *dquot)
283{
d3be915f
IM
284 mutex_lock(&dquot->dq_lock);
285 mutex_unlock(&dquot->dq_lock);
1da177e4
LT
286}
287
03f6e92b
JK
288static inline int dquot_dirty(struct dquot *dquot)
289{
290 return test_bit(DQ_MOD_B, &dquot->dq_flags);
291}
292
293static inline int mark_dquot_dirty(struct dquot *dquot)
294{
295 return dquot->dq_sb->dq_op->mark_dirty(dquot);
296}
1da177e4
LT
297
298int dquot_mark_dquot_dirty(struct dquot *dquot)
299{
300 spin_lock(&dq_list_lock);
301 if (!test_and_set_bit(DQ_MOD_B, &dquot->dq_flags))
302 list_add(&dquot->dq_dirty, &sb_dqopt(dquot->dq_sb)->
303 info[dquot->dq_type].dqi_dirty_list);
304 spin_unlock(&dq_list_lock);
305 return 0;
306}
307
308/* This function needs dq_list_lock */
309static inline int clear_dquot_dirty(struct dquot *dquot)
310{
311 if (!test_and_clear_bit(DQ_MOD_B, &dquot->dq_flags))
312 return 0;
313 list_del_init(&dquot->dq_dirty);
314 return 1;
315}
316
317void mark_info_dirty(struct super_block *sb, int type)
318{
319 set_bit(DQF_INFO_DIRTY_B, &sb_dqopt(sb)->info[type].dqi_flags);
320}
321EXPORT_SYMBOL(mark_info_dirty);
322
323/*
324 * Read dquot from disk and alloc space for it
325 */
326
327int dquot_acquire(struct dquot *dquot)
328{
329 int ret = 0, ret2 = 0;
330 struct quota_info *dqopt = sb_dqopt(dquot->dq_sb);
331
d3be915f
IM
332 mutex_lock(&dquot->dq_lock);
333 mutex_lock(&dqopt->dqio_mutex);
1da177e4
LT
334 if (!test_bit(DQ_READ_B, &dquot->dq_flags))
335 ret = dqopt->ops[dquot->dq_type]->read_dqblk(dquot);
336 if (ret < 0)
337 goto out_iolock;
338 set_bit(DQ_READ_B, &dquot->dq_flags);
339 /* Instantiate dquot if needed */
340 if (!test_bit(DQ_ACTIVE_B, &dquot->dq_flags) && !dquot->dq_off) {
341 ret = dqopt->ops[dquot->dq_type]->commit_dqblk(dquot);
342 /* Write the info if needed */
343 if (info_dirty(&dqopt->info[dquot->dq_type]))
344 ret2 = dqopt->ops[dquot->dq_type]->write_file_info(dquot->dq_sb, dquot->dq_type);
345 if (ret < 0)
346 goto out_iolock;
347 if (ret2 < 0) {
348 ret = ret2;
349 goto out_iolock;
350 }
351 }
352 set_bit(DQ_ACTIVE_B, &dquot->dq_flags);
353out_iolock:
d3be915f
IM
354 mutex_unlock(&dqopt->dqio_mutex);
355 mutex_unlock(&dquot->dq_lock);
1da177e4
LT
356 return ret;
357}
358
359/*
360 * Write dquot to disk
361 */
362int dquot_commit(struct dquot *dquot)
363{
364 int ret = 0, ret2 = 0;
365 struct quota_info *dqopt = sb_dqopt(dquot->dq_sb);
366
d3be915f 367 mutex_lock(&dqopt->dqio_mutex);
1da177e4
LT
368 spin_lock(&dq_list_lock);
369 if (!clear_dquot_dirty(dquot)) {
370 spin_unlock(&dq_list_lock);
371 goto out_sem;
372 }
373 spin_unlock(&dq_list_lock);
374 /* Inactive dquot can be only if there was error during read/init
375 * => we have better not writing it */
376 if (test_bit(DQ_ACTIVE_B, &dquot->dq_flags)) {
377 ret = dqopt->ops[dquot->dq_type]->commit_dqblk(dquot);
378 if (info_dirty(&dqopt->info[dquot->dq_type]))
379 ret2 = dqopt->ops[dquot->dq_type]->write_file_info(dquot->dq_sb, dquot->dq_type);
380 if (ret >= 0)
381 ret = ret2;
382 }
383out_sem:
d3be915f 384 mutex_unlock(&dqopt->dqio_mutex);
1da177e4
LT
385 return ret;
386}
387
388/*
389 * Release dquot
390 */
391int dquot_release(struct dquot *dquot)
392{
393 int ret = 0, ret2 = 0;
394 struct quota_info *dqopt = sb_dqopt(dquot->dq_sb);
395
d3be915f 396 mutex_lock(&dquot->dq_lock);
1da177e4
LT
397 /* Check whether we are not racing with some other dqget() */
398 if (atomic_read(&dquot->dq_count) > 1)
399 goto out_dqlock;
d3be915f 400 mutex_lock(&dqopt->dqio_mutex);
1da177e4
LT
401 if (dqopt->ops[dquot->dq_type]->release_dqblk) {
402 ret = dqopt->ops[dquot->dq_type]->release_dqblk(dquot);
403 /* Write the info */
404 if (info_dirty(&dqopt->info[dquot->dq_type]))
405 ret2 = dqopt->ops[dquot->dq_type]->write_file_info(dquot->dq_sb, dquot->dq_type);
406 if (ret >= 0)
407 ret = ret2;
408 }
409 clear_bit(DQ_ACTIVE_B, &dquot->dq_flags);
d3be915f 410 mutex_unlock(&dqopt->dqio_mutex);
1da177e4 411out_dqlock:
d3be915f 412 mutex_unlock(&dquot->dq_lock);
1da177e4
LT
413 return ret;
414}
415
74f783af
JK
416static void dquot_destroy(struct dquot *dquot)
417{
418 kmem_cache_free(dquot_cachep, dquot);
419}
420
421static inline void do_destroy_dquot(struct dquot *dquot)
422{
423 dquot->dq_sb->dq_op->destroy_dquot(dquot);
424}
425
1da177e4
LT
426/* Invalidate all dquots on the list. Note that this function is called after
427 * quota is disabled and pointers from inodes removed so there cannot be new
6362e4d4
JK
428 * quota users. There can still be some users of quotas due to inodes being
429 * just deleted or pruned by prune_icache() (those are not attached to any
430 * list). We have to wait for such users.
431 */
1da177e4
LT
432static void invalidate_dquots(struct super_block *sb, int type)
433{
c33ed271 434 struct dquot *dquot, *tmp;
1da177e4 435
6362e4d4 436restart:
1da177e4 437 spin_lock(&dq_list_lock);
c33ed271 438 list_for_each_entry_safe(dquot, tmp, &inuse_list, dq_inuse) {
1da177e4
LT
439 if (dquot->dq_sb != sb)
440 continue;
441 if (dquot->dq_type != type)
442 continue;
6362e4d4
JK
443 /* Wait for dquot users */
444 if (atomic_read(&dquot->dq_count)) {
445 DEFINE_WAIT(wait);
446
447 atomic_inc(&dquot->dq_count);
448 prepare_to_wait(&dquot->dq_wait_unused, &wait,
449 TASK_UNINTERRUPTIBLE);
450 spin_unlock(&dq_list_lock);
451 /* Once dqput() wakes us up, we know it's time to free
452 * the dquot.
453 * IMPORTANT: we rely on the fact that there is always
454 * at most one process waiting for dquot to free.
455 * Otherwise dq_count would be > 1 and we would never
456 * wake up.
457 */
458 if (atomic_read(&dquot->dq_count) > 1)
459 schedule();
460 finish_wait(&dquot->dq_wait_unused, &wait);
461 dqput(dquot);
462 /* At this moment dquot() need not exist (it could be
463 * reclaimed by prune_dqcache(). Hence we must
464 * restart. */
465 goto restart;
466 }
467 /*
468 * Quota now has no users and it has been written on last
469 * dqput()
470 */
1da177e4
LT
471 remove_dquot_hash(dquot);
472 remove_free_dquot(dquot);
473 remove_inuse(dquot);
74f783af 474 do_destroy_dquot(dquot);
1da177e4
LT
475 }
476 spin_unlock(&dq_list_lock);
477}
478
12c77527
JK
479/* Call callback for every active dquot on given filesystem */
480int dquot_scan_active(struct super_block *sb,
481 int (*fn)(struct dquot *dquot, unsigned long priv),
482 unsigned long priv)
483{
484 struct dquot *dquot, *old_dquot = NULL;
485 int ret = 0;
486
487 mutex_lock(&sb_dqopt(sb)->dqonoff_mutex);
488 spin_lock(&dq_list_lock);
489 list_for_each_entry(dquot, &inuse_list, dq_inuse) {
490 if (!test_bit(DQ_ACTIVE_B, &dquot->dq_flags))
491 continue;
492 if (dquot->dq_sb != sb)
493 continue;
494 /* Now we have active dquot so we can just increase use count */
495 atomic_inc(&dquot->dq_count);
496 dqstats.lookups++;
497 spin_unlock(&dq_list_lock);
498 dqput(old_dquot);
499 old_dquot = dquot;
500 ret = fn(dquot, priv);
501 if (ret < 0)
502 goto out;
503 spin_lock(&dq_list_lock);
504 /* We are safe to continue now because our dquot could not
505 * be moved out of the inuse list while we hold the reference */
506 }
507 spin_unlock(&dq_list_lock);
508out:
509 dqput(old_dquot);
510 mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
511 return ret;
512}
513
1da177e4
LT
514int vfs_quota_sync(struct super_block *sb, int type)
515{
516 struct list_head *dirty;
517 struct dquot *dquot;
518 struct quota_info *dqopt = sb_dqopt(sb);
519 int cnt;
520
d3be915f 521 mutex_lock(&dqopt->dqonoff_mutex);
1da177e4
LT
522 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
523 if (type != -1 && cnt != type)
524 continue;
f55abc0f 525 if (!sb_has_quota_active(sb, cnt))
1da177e4
LT
526 continue;
527 spin_lock(&dq_list_lock);
528 dirty = &dqopt->info[cnt].dqi_dirty_list;
529 while (!list_empty(dirty)) {
b5e61818 530 dquot = list_first_entry(dirty, struct dquot, dq_dirty);
1da177e4
LT
531 /* Dirty and inactive can be only bad dquot... */
532 if (!test_bit(DQ_ACTIVE_B, &dquot->dq_flags)) {
533 clear_dquot_dirty(dquot);
534 continue;
535 }
536 /* Now we have active dquot from which someone is
537 * holding reference so we can safely just increase
538 * use count */
539 atomic_inc(&dquot->dq_count);
540 dqstats.lookups++;
541 spin_unlock(&dq_list_lock);
542 sb->dq_op->write_dquot(dquot);
543 dqput(dquot);
544 spin_lock(&dq_list_lock);
545 }
546 spin_unlock(&dq_list_lock);
547 }
548
549 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
f55abc0f
JK
550 if ((cnt == type || type == -1) && sb_has_quota_active(sb, cnt)
551 && info_dirty(&dqopt->info[cnt]))
1da177e4
LT
552 sb->dq_op->write_info(sb, cnt);
553 spin_lock(&dq_list_lock);
554 dqstats.syncs++;
555 spin_unlock(&dq_list_lock);
d3be915f 556 mutex_unlock(&dqopt->dqonoff_mutex);
1da177e4
LT
557
558 return 0;
559}
560
561/* Free unused dquots from cache */
562static void prune_dqcache(int count)
563{
564 struct list_head *head;
565 struct dquot *dquot;
566
567 head = free_dquots.prev;
568 while (head != &free_dquots && count) {
569 dquot = list_entry(head, struct dquot, dq_free);
570 remove_dquot_hash(dquot);
571 remove_free_dquot(dquot);
572 remove_inuse(dquot);
74f783af 573 do_destroy_dquot(dquot);
1da177e4
LT
574 count--;
575 head = free_dquots.prev;
576 }
577}
578
579/*
580 * This is called from kswapd when we think we need some
581 * more memory
582 */
583
27496a8c 584static int shrink_dqcache_memory(int nr, gfp_t gfp_mask)
1da177e4
LT
585{
586 if (nr) {
587 spin_lock(&dq_list_lock);
588 prune_dqcache(nr);
589 spin_unlock(&dq_list_lock);
590 }
591 return (dqstats.free_dquots / 100) * sysctl_vfs_cache_pressure;
592}
593
8e1f936b
RR
594static struct shrinker dqcache_shrinker = {
595 .shrink = shrink_dqcache_memory,
596 .seeks = DEFAULT_SEEKS,
597};
598
1da177e4
LT
599/*
600 * Put reference to dquot
601 * NOTE: If you change this function please check whether dqput_blocks() works right...
d3be915f 602 * MUST be called with either dqptr_sem or dqonoff_mutex held
1da177e4 603 */
3d9ea253 604void dqput(struct dquot *dquot)
1da177e4 605{
b48d3805
JK
606 int ret;
607
1da177e4
LT
608 if (!dquot)
609 return;
610#ifdef __DQUOT_PARANOIA
611 if (!atomic_read(&dquot->dq_count)) {
612 printk("VFS: dqput: trying to free free dquot\n");
613 printk("VFS: device %s, dquot of %s %d\n",
614 dquot->dq_sb->s_id,
615 quotatypes[dquot->dq_type],
616 dquot->dq_id);
617 BUG();
618 }
619#endif
620
621 spin_lock(&dq_list_lock);
622 dqstats.drops++;
623 spin_unlock(&dq_list_lock);
624we_slept:
625 spin_lock(&dq_list_lock);
626 if (atomic_read(&dquot->dq_count) > 1) {
627 /* We have more than one user... nothing to do */
628 atomic_dec(&dquot->dq_count);
6362e4d4 629 /* Releasing dquot during quotaoff phase? */
f55abc0f 630 if (!sb_has_quota_active(dquot->dq_sb, dquot->dq_type) &&
6362e4d4
JK
631 atomic_read(&dquot->dq_count) == 1)
632 wake_up(&dquot->dq_wait_unused);
1da177e4
LT
633 spin_unlock(&dq_list_lock);
634 return;
635 }
636 /* Need to release dquot? */
637 if (test_bit(DQ_ACTIVE_B, &dquot->dq_flags) && dquot_dirty(dquot)) {
638 spin_unlock(&dq_list_lock);
639 /* Commit dquot before releasing */
b48d3805
JK
640 ret = dquot->dq_sb->dq_op->write_dquot(dquot);
641 if (ret < 0) {
642 printk(KERN_ERR "VFS: cannot write quota structure on "
643 "device %s (error %d). Quota may get out of "
644 "sync!\n", dquot->dq_sb->s_id, ret);
645 /*
646 * We clear dirty bit anyway, so that we avoid
647 * infinite loop here
648 */
649 spin_lock(&dq_list_lock);
650 clear_dquot_dirty(dquot);
651 spin_unlock(&dq_list_lock);
652 }
1da177e4
LT
653 goto we_slept;
654 }
655 /* Clear flag in case dquot was inactive (something bad happened) */
656 clear_dquot_dirty(dquot);
657 if (test_bit(DQ_ACTIVE_B, &dquot->dq_flags)) {
658 spin_unlock(&dq_list_lock);
659 dquot->dq_sb->dq_op->release_dquot(dquot);
660 goto we_slept;
661 }
662 atomic_dec(&dquot->dq_count);
663#ifdef __DQUOT_PARANOIA
664 /* sanity check */
8abf6a47 665 BUG_ON(!list_empty(&dquot->dq_free));
1da177e4
LT
666#endif
667 put_dquot_last(dquot);
668 spin_unlock(&dq_list_lock);
669}
670
74f783af
JK
671static struct dquot *dquot_alloc(struct super_block *sb, int type)
672{
673 return kmem_cache_zalloc(dquot_cachep, GFP_NOFS);
674}
675
1da177e4
LT
676static struct dquot *get_empty_dquot(struct super_block *sb, int type)
677{
678 struct dquot *dquot;
679
74f783af 680 dquot = sb->dq_op->alloc_dquot(sb, type);
1da177e4
LT
681 if(!dquot)
682 return NODQUOT;
683
d3be915f 684 mutex_init(&dquot->dq_lock);
1da177e4
LT
685 INIT_LIST_HEAD(&dquot->dq_free);
686 INIT_LIST_HEAD(&dquot->dq_inuse);
687 INIT_HLIST_NODE(&dquot->dq_hash);
688 INIT_LIST_HEAD(&dquot->dq_dirty);
6362e4d4 689 init_waitqueue_head(&dquot->dq_wait_unused);
1da177e4
LT
690 dquot->dq_sb = sb;
691 dquot->dq_type = type;
692 atomic_set(&dquot->dq_count, 1);
693
694 return dquot;
695}
696
3d9ea253
JK
697/*
698 * Check whether dquot is in memory.
699 * MUST be called with either dqptr_sem or dqonoff_mutex held
700 */
701int dquot_is_cached(struct super_block *sb, unsigned int id, int type)
702{
703 unsigned int hashent = hashfn(sb, id, type);
704 int ret = 0;
705
706 if (!sb_has_quota_active(sb, type))
707 return 0;
708 spin_lock(&dq_list_lock);
709 if (find_dquot(hashent, sb, id, type) != NODQUOT)
710 ret = 1;
711 spin_unlock(&dq_list_lock);
712 return ret;
713}
714
1da177e4
LT
715/*
716 * Get reference to dquot
d3be915f 717 * MUST be called with either dqptr_sem or dqonoff_mutex held
1da177e4 718 */
3d9ea253 719struct dquot *dqget(struct super_block *sb, unsigned int id, int type)
1da177e4
LT
720{
721 unsigned int hashent = hashfn(sb, id, type);
722 struct dquot *dquot, *empty = NODQUOT;
723
f55abc0f 724 if (!sb_has_quota_active(sb, type))
1da177e4
LT
725 return NODQUOT;
726we_slept:
727 spin_lock(&dq_list_lock);
728 if ((dquot = find_dquot(hashent, sb, id, type)) == NODQUOT) {
729 if (empty == NODQUOT) {
730 spin_unlock(&dq_list_lock);
731 if ((empty = get_empty_dquot(sb, type)) == NODQUOT)
732 schedule(); /* Try to wait for a moment... */
733 goto we_slept;
734 }
735 dquot = empty;
736 dquot->dq_id = id;
737 /* all dquots go on the inuse_list */
738 put_inuse(dquot);
739 /* hash it first so it can be found */
740 insert_dquot_hash(dquot);
741 dqstats.lookups++;
742 spin_unlock(&dq_list_lock);
743 } else {
744 if (!atomic_read(&dquot->dq_count))
745 remove_free_dquot(dquot);
746 atomic_inc(&dquot->dq_count);
747 dqstats.cache_hits++;
748 dqstats.lookups++;
749 spin_unlock(&dq_list_lock);
750 if (empty)
74f783af 751 do_destroy_dquot(empty);
1da177e4
LT
752 }
753 /* Wait for dq_lock - after this we know that either dquot_release() is already
754 * finished or it will be canceled due to dq_count > 1 test */
755 wait_on_dquot(dquot);
756 /* Read the dquot and instantiate it (everything done only if needed) */
757 if (!test_bit(DQ_ACTIVE_B, &dquot->dq_flags) && sb->dq_op->acquire_dquot(dquot) < 0) {
758 dqput(dquot);
759 return NODQUOT;
760 }
761#ifdef __DQUOT_PARANOIA
8abf6a47 762 BUG_ON(!dquot->dq_sb); /* Has somebody invalidated entry under us? */
1da177e4
LT
763#endif
764
765 return dquot;
766}
767
768static int dqinit_needed(struct inode *inode, int type)
769{
770 int cnt;
771
772 if (IS_NOQUOTA(inode))
773 return 0;
774 if (type != -1)
775 return inode->i_dquot[type] == NODQUOT;
776 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
777 if (inode->i_dquot[cnt] == NODQUOT)
778 return 1;
779 return 0;
780}
781
d3be915f 782/* This routine is guarded by dqonoff_mutex mutex */
1da177e4
LT
783static void add_dquot_ref(struct super_block *sb, int type)
784{
941d2380 785 struct inode *inode, *old_inode = NULL;
1da177e4 786
d003fb70
CH
787 spin_lock(&inode_lock);
788 list_for_each_entry(inode, &sb->s_inodes, i_sb_list) {
789 if (!atomic_read(&inode->i_writecount))
790 continue;
791 if (!dqinit_needed(inode, type))
792 continue;
793 if (inode->i_state & (I_FREEING|I_WILL_FREE))
794 continue;
795
796 __iget(inode);
797 spin_unlock(&inode_lock);
798
941d2380 799 iput(old_inode);
d003fb70 800 sb->dq_op->initialize(inode, type);
941d2380
JK
801 /* We hold a reference to 'inode' so it couldn't have been
802 * removed from s_inodes list while we dropped the inode_lock.
803 * We cannot iput the inode now as we can be holding the last
804 * reference and we cannot iput it under inode_lock. So we
805 * keep the reference and iput it later. */
806 old_inode = inode;
807 spin_lock(&inode_lock);
1da177e4 808 }
d003fb70 809 spin_unlock(&inode_lock);
941d2380 810 iput(old_inode);
1da177e4
LT
811}
812
813/* Return 0 if dqput() won't block (note that 1 doesn't necessarily mean blocking) */
814static inline int dqput_blocks(struct dquot *dquot)
815{
816 if (atomic_read(&dquot->dq_count) <= 1)
817 return 1;
818 return 0;
819}
820
821/* Remove references to dquots from inode - add dquot to list for freeing if needed */
822/* We can't race with anybody because we hold dqptr_sem for writing... */
e5f00f42
AB
823static int remove_inode_dquot_ref(struct inode *inode, int type,
824 struct list_head *tofree_head)
1da177e4
LT
825{
826 struct dquot *dquot = inode->i_dquot[type];
827
828 inode->i_dquot[type] = NODQUOT;
829 if (dquot != NODQUOT) {
830 if (dqput_blocks(dquot)) {
831#ifdef __DQUOT_PARANOIA
832 if (atomic_read(&dquot->dq_count) != 1)
833 printk(KERN_WARNING "VFS: Adding dquot with dq_count %d to dispose list.\n", atomic_read(&dquot->dq_count));
834#endif
835 spin_lock(&dq_list_lock);
836 list_add(&dquot->dq_free, tofree_head); /* As dquot must have currently users it can't be on the free list... */
837 spin_unlock(&dq_list_lock);
838 return 1;
839 }
840 else
841 dqput(dquot); /* We have guaranteed we won't block */
842 }
843 return 0;
844}
845
846/* Free list of dquots - called from inode.c */
847/* dquots are removed from inodes, no new references can be got so we are the only ones holding reference */
848static void put_dquot_list(struct list_head *tofree_head)
849{
850 struct list_head *act_head;
851 struct dquot *dquot;
852
853 act_head = tofree_head->next;
854 /* So now we have dquots on the list... Just free them */
855 while (act_head != tofree_head) {
856 dquot = list_entry(act_head, struct dquot, dq_free);
857 act_head = act_head->next;
858 list_del_init(&dquot->dq_free); /* Remove dquot from the list so we won't have problems... */
859 dqput(dquot);
860 }
861}
862
fb58b731
CH
863static void remove_dquot_ref(struct super_block *sb, int type,
864 struct list_head *tofree_head)
865{
866 struct inode *inode;
867
868 spin_lock(&inode_lock);
869 list_for_each_entry(inode, &sb->s_inodes, i_sb_list) {
870 if (!IS_NOQUOTA(inode))
871 remove_inode_dquot_ref(inode, type, tofree_head);
872 }
873 spin_unlock(&inode_lock);
874}
875
1da177e4
LT
876/* Gather all references from inodes and drop them */
877static void drop_dquot_ref(struct super_block *sb, int type)
878{
879 LIST_HEAD(tofree_head);
880
fb58b731
CH
881 if (sb->dq_op) {
882 down_write(&sb_dqopt(sb)->dqptr_sem);
883 remove_dquot_ref(sb, type, &tofree_head);
884 up_write(&sb_dqopt(sb)->dqptr_sem);
885 put_dquot_list(&tofree_head);
886 }
1da177e4
LT
887}
888
12095460 889static inline void dquot_incr_inodes(struct dquot *dquot, qsize_t number)
1da177e4
LT
890{
891 dquot->dq_dqb.dqb_curinodes += number;
892}
893
894static inline void dquot_incr_space(struct dquot *dquot, qsize_t number)
895{
896 dquot->dq_dqb.dqb_curspace += number;
897}
898
12095460 899static inline void dquot_decr_inodes(struct dquot *dquot, qsize_t number)
1da177e4 900{
db49d2df
JK
901 if (sb_dqopt(dquot->dq_sb)->flags & DQUOT_NEGATIVE_USAGE ||
902 dquot->dq_dqb.dqb_curinodes >= number)
1da177e4
LT
903 dquot->dq_dqb.dqb_curinodes -= number;
904 else
905 dquot->dq_dqb.dqb_curinodes = 0;
906 if (dquot->dq_dqb.dqb_curinodes <= dquot->dq_dqb.dqb_isoftlimit)
907 dquot->dq_dqb.dqb_itime = (time_t) 0;
908 clear_bit(DQ_INODES_B, &dquot->dq_flags);
909}
910
911static inline void dquot_decr_space(struct dquot *dquot, qsize_t number)
912{
db49d2df
JK
913 if (sb_dqopt(dquot->dq_sb)->flags & DQUOT_NEGATIVE_USAGE ||
914 dquot->dq_dqb.dqb_curspace >= number)
1da177e4
LT
915 dquot->dq_dqb.dqb_curspace -= number;
916 else
917 dquot->dq_dqb.dqb_curspace = 0;
12095460 918 if (dquot->dq_dqb.dqb_curspace <= dquot->dq_dqb.dqb_bsoftlimit)
1da177e4
LT
919 dquot->dq_dqb.dqb_btime = (time_t) 0;
920 clear_bit(DQ_BLKS_B, &dquot->dq_flags);
921}
922
c525460e
JK
923static int warning_issued(struct dquot *dquot, const int warntype)
924{
925 int flag = (warntype == QUOTA_NL_BHARDWARN ||
926 warntype == QUOTA_NL_BSOFTLONGWARN) ? DQ_BLKS_B :
927 ((warntype == QUOTA_NL_IHARDWARN ||
928 warntype == QUOTA_NL_ISOFTLONGWARN) ? DQ_INODES_B : 0);
929
930 if (!flag)
931 return 0;
932 return test_and_set_bit(flag, &dquot->dq_flags);
933}
934
8e893469 935#ifdef CONFIG_PRINT_QUOTA_WARNING
1da177e4
LT
936static int flag_print_warnings = 1;
937
938static inline int need_print_warning(struct dquot *dquot)
939{
940 if (!flag_print_warnings)
941 return 0;
942
943 switch (dquot->dq_type) {
944 case USRQUOTA:
da9592ed 945 return current_fsuid() == dquot->dq_id;
1da177e4
LT
946 case GRPQUOTA:
947 return in_group_p(dquot->dq_id);
948 }
949 return 0;
950}
951
1da177e4 952/* Print warning to user which exceeded quota */
c525460e 953static void print_warning(struct dquot *dquot, const int warntype)
1da177e4
LT
954{
955 char *msg = NULL;
24ec839c 956 struct tty_struct *tty;
1da177e4 957
657d3bfa
JK
958 if (warntype == QUOTA_NL_IHARDBELOW ||
959 warntype == QUOTA_NL_ISOFTBELOW ||
960 warntype == QUOTA_NL_BHARDBELOW ||
961 warntype == QUOTA_NL_BSOFTBELOW || !need_print_warning(dquot))
1da177e4
LT
962 return;
963
24ec839c
PZ
964 tty = get_current_tty();
965 if (!tty)
452a00d2 966 return;
24ec839c 967 tty_write_message(tty, dquot->dq_sb->s_id);
8e893469 968 if (warntype == QUOTA_NL_ISOFTWARN || warntype == QUOTA_NL_BSOFTWARN)
24ec839c 969 tty_write_message(tty, ": warning, ");
1da177e4 970 else
24ec839c
PZ
971 tty_write_message(tty, ": write failed, ");
972 tty_write_message(tty, quotatypes[dquot->dq_type]);
1da177e4 973 switch (warntype) {
8e893469 974 case QUOTA_NL_IHARDWARN:
1da177e4
LT
975 msg = " file limit reached.\r\n";
976 break;
8e893469 977 case QUOTA_NL_ISOFTLONGWARN:
1da177e4
LT
978 msg = " file quota exceeded too long.\r\n";
979 break;
8e893469 980 case QUOTA_NL_ISOFTWARN:
1da177e4
LT
981 msg = " file quota exceeded.\r\n";
982 break;
8e893469 983 case QUOTA_NL_BHARDWARN:
1da177e4
LT
984 msg = " block limit reached.\r\n";
985 break;
8e893469 986 case QUOTA_NL_BSOFTLONGWARN:
1da177e4
LT
987 msg = " block quota exceeded too long.\r\n";
988 break;
8e893469 989 case QUOTA_NL_BSOFTWARN:
1da177e4
LT
990 msg = " block quota exceeded.\r\n";
991 break;
992 }
24ec839c 993 tty_write_message(tty, msg);
452a00d2 994 tty_kref_put(tty);
1da177e4 995}
8e893469
JK
996#endif
997
998#ifdef CONFIG_QUOTA_NETLINK_INTERFACE
999
8e893469
JK
1000/* Netlink family structure for quota */
1001static struct genl_family quota_genl_family = {
1002 .id = GENL_ID_GENERATE,
1003 .hdrsize = 0,
1004 .name = "VFS_DQUOT",
1005 .version = 1,
1006 .maxattr = QUOTA_NL_A_MAX,
1007};
1008
1009/* Send warning to userspace about user which exceeded quota */
1010static void send_warning(const struct dquot *dquot, const char warntype)
1011{
1012 static atomic_t seq;
1013 struct sk_buff *skb;
1014 void *msg_head;
1015 int ret;
22dd4837
JK
1016 int msg_size = 4 * nla_total_size(sizeof(u32)) +
1017 2 * nla_total_size(sizeof(u64));
8e893469
JK
1018
1019 /* We have to allocate using GFP_NOFS as we are called from a
1020 * filesystem performing write and thus further recursion into
1021 * the fs to free some data could cause deadlocks. */
22dd4837 1022 skb = genlmsg_new(msg_size, GFP_NOFS);
8e893469
JK
1023 if (!skb) {
1024 printk(KERN_ERR
1025 "VFS: Not enough memory to send quota warning.\n");
1026 return;
1027 }
1028 msg_head = genlmsg_put(skb, 0, atomic_add_return(1, &seq),
1029 &quota_genl_family, 0, QUOTA_NL_C_WARNING);
1030 if (!msg_head) {
1031 printk(KERN_ERR
1032 "VFS: Cannot store netlink header in quota warning.\n");
1033 goto err_out;
1034 }
1035 ret = nla_put_u32(skb, QUOTA_NL_A_QTYPE, dquot->dq_type);
1036 if (ret)
1037 goto attr_err_out;
1038 ret = nla_put_u64(skb, QUOTA_NL_A_EXCESS_ID, dquot->dq_id);
1039 if (ret)
1040 goto attr_err_out;
1041 ret = nla_put_u32(skb, QUOTA_NL_A_WARNING, warntype);
1042 if (ret)
1043 goto attr_err_out;
1044 ret = nla_put_u32(skb, QUOTA_NL_A_DEV_MAJOR,
1045 MAJOR(dquot->dq_sb->s_dev));
1046 if (ret)
1047 goto attr_err_out;
1048 ret = nla_put_u32(skb, QUOTA_NL_A_DEV_MINOR,
1049 MINOR(dquot->dq_sb->s_dev));
1050 if (ret)
1051 goto attr_err_out;
da9592ed 1052 ret = nla_put_u64(skb, QUOTA_NL_A_CAUSED_ID, current_uid());
8e893469
JK
1053 if (ret)
1054 goto attr_err_out;
1055 genlmsg_end(skb, msg_head);
1056
1057 ret = genlmsg_multicast(skb, 0, quota_genl_family.id, GFP_NOFS);
1058 if (ret < 0 && ret != -ESRCH)
1059 printk(KERN_ERR
1060 "VFS: Failed to send notification message: %d\n", ret);
1061 return;
1062attr_err_out:
22dd4837 1063 printk(KERN_ERR "VFS: Not enough space to compose quota message!\n");
8e893469
JK
1064err_out:
1065 kfree_skb(skb);
1066}
1067#endif
1da177e4 1068
087ee8d5 1069static inline void flush_warnings(struct dquot * const *dquots, char *warntype)
1da177e4
LT
1070{
1071 int i;
1072
1073 for (i = 0; i < MAXQUOTAS; i++)
c525460e
JK
1074 if (dquots[i] != NODQUOT && warntype[i] != QUOTA_NL_NOWARN &&
1075 !warning_issued(dquots[i], warntype[i])) {
8e893469 1076#ifdef CONFIG_PRINT_QUOTA_WARNING
1da177e4 1077 print_warning(dquots[i], warntype[i]);
8e893469
JK
1078#endif
1079#ifdef CONFIG_QUOTA_NETLINK_INTERFACE
1080 send_warning(dquots[i], warntype[i]);
1081#endif
1082 }
1da177e4
LT
1083}
1084
1085static inline char ignore_hardlimit(struct dquot *dquot)
1086{
1087 struct mem_dqinfo *info = &sb_dqopt(dquot->dq_sb)->info[dquot->dq_type];
1088
1089 return capable(CAP_SYS_RESOURCE) &&
1090 (info->dqi_format->qf_fmt_id != QFMT_VFS_OLD || !(info->dqi_flags & V1_DQF_RSQUASH));
1091}
1092
1093/* needs dq_data_lock */
12095460 1094static int check_idq(struct dquot *dquot, qsize_t inodes, char *warntype)
1da177e4 1095{
8e893469 1096 *warntype = QUOTA_NL_NOWARN;
f55abc0f
JK
1097 if (!sb_has_quota_limits_enabled(dquot->dq_sb, dquot->dq_type) ||
1098 test_bit(DQ_FAKE_B, &dquot->dq_flags))
1da177e4
LT
1099 return QUOTA_OK;
1100
1101 if (dquot->dq_dqb.dqb_ihardlimit &&
1102 (dquot->dq_dqb.dqb_curinodes + inodes) > dquot->dq_dqb.dqb_ihardlimit &&
1103 !ignore_hardlimit(dquot)) {
8e893469 1104 *warntype = QUOTA_NL_IHARDWARN;
1da177e4
LT
1105 return NO_QUOTA;
1106 }
1107
1108 if (dquot->dq_dqb.dqb_isoftlimit &&
1109 (dquot->dq_dqb.dqb_curinodes + inodes) > dquot->dq_dqb.dqb_isoftlimit &&
1110 dquot->dq_dqb.dqb_itime && get_seconds() >= dquot->dq_dqb.dqb_itime &&
1111 !ignore_hardlimit(dquot)) {
8e893469 1112 *warntype = QUOTA_NL_ISOFTLONGWARN;
1da177e4
LT
1113 return NO_QUOTA;
1114 }
1115
1116 if (dquot->dq_dqb.dqb_isoftlimit &&
1117 (dquot->dq_dqb.dqb_curinodes + inodes) > dquot->dq_dqb.dqb_isoftlimit &&
1118 dquot->dq_dqb.dqb_itime == 0) {
8e893469 1119 *warntype = QUOTA_NL_ISOFTWARN;
1da177e4
LT
1120 dquot->dq_dqb.dqb_itime = get_seconds() + sb_dqopt(dquot->dq_sb)->info[dquot->dq_type].dqi_igrace;
1121 }
1122
1123 return QUOTA_OK;
1124}
1125
1126/* needs dq_data_lock */
1127static int check_bdq(struct dquot *dquot, qsize_t space, int prealloc, char *warntype)
1128{
8e893469 1129 *warntype = QUOTA_NL_NOWARN;
f55abc0f
JK
1130 if (!sb_has_quota_limits_enabled(dquot->dq_sb, dquot->dq_type) ||
1131 test_bit(DQ_FAKE_B, &dquot->dq_flags))
1da177e4
LT
1132 return QUOTA_OK;
1133
1134 if (dquot->dq_dqb.dqb_bhardlimit &&
12095460 1135 dquot->dq_dqb.dqb_curspace + space > dquot->dq_dqb.dqb_bhardlimit &&
1da177e4
LT
1136 !ignore_hardlimit(dquot)) {
1137 if (!prealloc)
8e893469 1138 *warntype = QUOTA_NL_BHARDWARN;
1da177e4
LT
1139 return NO_QUOTA;
1140 }
1141
1142 if (dquot->dq_dqb.dqb_bsoftlimit &&
12095460 1143 dquot->dq_dqb.dqb_curspace + space > dquot->dq_dqb.dqb_bsoftlimit &&
1da177e4
LT
1144 dquot->dq_dqb.dqb_btime && get_seconds() >= dquot->dq_dqb.dqb_btime &&
1145 !ignore_hardlimit(dquot)) {
1146 if (!prealloc)
8e893469 1147 *warntype = QUOTA_NL_BSOFTLONGWARN;
1da177e4
LT
1148 return NO_QUOTA;
1149 }
1150
1151 if (dquot->dq_dqb.dqb_bsoftlimit &&
12095460 1152 dquot->dq_dqb.dqb_curspace + space > dquot->dq_dqb.dqb_bsoftlimit &&
1da177e4
LT
1153 dquot->dq_dqb.dqb_btime == 0) {
1154 if (!prealloc) {
8e893469 1155 *warntype = QUOTA_NL_BSOFTWARN;
1da177e4
LT
1156 dquot->dq_dqb.dqb_btime = get_seconds() + sb_dqopt(dquot->dq_sb)->info[dquot->dq_type].dqi_bgrace;
1157 }
1158 else
1159 /*
1160 * We don't allow preallocation to exceed softlimit so exceeding will
1161 * be always printed
1162 */
1163 return NO_QUOTA;
1164 }
1165
1166 return QUOTA_OK;
1167}
1168
12095460 1169static int info_idq_free(struct dquot *dquot, qsize_t inodes)
657d3bfa
JK
1170{
1171 if (test_bit(DQ_FAKE_B, &dquot->dq_flags) ||
f55abc0f
JK
1172 dquot->dq_dqb.dqb_curinodes <= dquot->dq_dqb.dqb_isoftlimit ||
1173 !sb_has_quota_limits_enabled(dquot->dq_sb, dquot->dq_type))
657d3bfa
JK
1174 return QUOTA_NL_NOWARN;
1175
1176 if (dquot->dq_dqb.dqb_curinodes - inodes <= dquot->dq_dqb.dqb_isoftlimit)
1177 return QUOTA_NL_ISOFTBELOW;
1178 if (dquot->dq_dqb.dqb_curinodes >= dquot->dq_dqb.dqb_ihardlimit &&
1179 dquot->dq_dqb.dqb_curinodes - inodes < dquot->dq_dqb.dqb_ihardlimit)
1180 return QUOTA_NL_IHARDBELOW;
1181 return QUOTA_NL_NOWARN;
1182}
1183
1184static int info_bdq_free(struct dquot *dquot, qsize_t space)
1185{
1186 if (test_bit(DQ_FAKE_B, &dquot->dq_flags) ||
12095460 1187 dquot->dq_dqb.dqb_curspace <= dquot->dq_dqb.dqb_bsoftlimit)
657d3bfa
JK
1188 return QUOTA_NL_NOWARN;
1189
12095460 1190 if (dquot->dq_dqb.dqb_curspace - space <= dquot->dq_dqb.dqb_bsoftlimit)
657d3bfa 1191 return QUOTA_NL_BSOFTBELOW;
12095460
JK
1192 if (dquot->dq_dqb.dqb_curspace >= dquot->dq_dqb.dqb_bhardlimit &&
1193 dquot->dq_dqb.dqb_curspace - space < dquot->dq_dqb.dqb_bhardlimit)
657d3bfa
JK
1194 return QUOTA_NL_BHARDBELOW;
1195 return QUOTA_NL_NOWARN;
1196}
1da177e4
LT
1197/*
1198 * Initialize quota pointers in inode
1199 * Transaction must be started at entry
1200 */
1201int dquot_initialize(struct inode *inode, int type)
1202{
1203 unsigned int id = 0;
1204 int cnt, ret = 0;
1205
d3be915f
IM
1206 /* First test before acquiring mutex - solves deadlocks when we
1207 * re-enter the quota code and are already holding the mutex */
1da177e4
LT
1208 if (IS_NOQUOTA(inode))
1209 return 0;
1210 down_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
1211 /* Having dqptr_sem we know NOQUOTA flags can't be altered... */
1212 if (IS_NOQUOTA(inode))
1213 goto out_err;
1214 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1215 if (type != -1 && cnt != type)
1216 continue;
1217 if (inode->i_dquot[cnt] == NODQUOT) {
1218 switch (cnt) {
1219 case USRQUOTA:
1220 id = inode->i_uid;
1221 break;
1222 case GRPQUOTA:
1223 id = inode->i_gid;
1224 break;
1225 }
1226 inode->i_dquot[cnt] = dqget(inode->i_sb, id, cnt);
1227 }
1228 }
1229out_err:
1230 up_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
1231 return ret;
1232}
1233
1234/*
1235 * Release all quotas referenced by inode
1236 * Transaction must be started at an entry
1237 */
3d9ea253 1238int dquot_drop_locked(struct inode *inode)
1da177e4
LT
1239{
1240 int cnt;
1241
1da177e4
LT
1242 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1243 if (inode->i_dquot[cnt] != NODQUOT) {
1244 dqput(inode->i_dquot[cnt]);
1245 inode->i_dquot[cnt] = NODQUOT;
1246 }
1247 }
3d9ea253
JK
1248 return 0;
1249}
1250
1251int dquot_drop(struct inode *inode)
1252{
1253 down_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
1254 dquot_drop_locked(inode);
1da177e4
LT
1255 up_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
1256 return 0;
1257}
1258
b85f4b87
JK
1259/* Wrapper to remove references to quota structures from inode */
1260void vfs_dq_drop(struct inode *inode)
1261{
1262 /* Here we can get arbitrary inode from clear_inode() so we have
1263 * to be careful. OTOH we don't need locking as quota operations
1264 * are allowed to change only at mount time */
1265 if (!IS_NOQUOTA(inode) && inode->i_sb && inode->i_sb->dq_op
1266 && inode->i_sb->dq_op->drop) {
1267 int cnt;
1268 /* Test before calling to rule out calls from proc and such
1269 * where we are not allowed to block. Note that this is
1270 * actually reliable test even without the lock - the caller
1271 * must assure that nobody can come after the DQUOT_DROP and
1272 * add quota pointers back anyway */
1273 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1274 if (inode->i_dquot[cnt] != NODQUOT)
1275 break;
1276 if (cnt < MAXQUOTAS)
1277 inode->i_sb->dq_op->drop(inode);
1278 }
1279}
1280
1da177e4
LT
1281/*
1282 * Following four functions update i_blocks+i_bytes fields and
1283 * quota information (together with appropriate checks)
1284 * NOTE: We absolutely rely on the fact that caller dirties
1285 * the inode (usually macros in quotaops.h care about this) and
1286 * holds a handle for the current transaction so that dquot write and
1287 * inode write go into the same transaction.
1288 */
1289
1290/*
1291 * This operation can block, but only after everything is updated
1292 */
1293int dquot_alloc_space(struct inode *inode, qsize_t number, int warn)
1294{
1295 int cnt, ret = NO_QUOTA;
1296 char warntype[MAXQUOTAS];
1297
d3be915f
IM
1298 /* First test before acquiring mutex - solves deadlocks when we
1299 * re-enter the quota code and are already holding the mutex */
1da177e4
LT
1300 if (IS_NOQUOTA(inode)) {
1301out_add:
1302 inode_add_bytes(inode, number);
1303 return QUOTA_OK;
1304 }
1305 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
8e893469 1306 warntype[cnt] = QUOTA_NL_NOWARN;
1da177e4
LT
1307
1308 down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1309 if (IS_NOQUOTA(inode)) { /* Now we can do reliable test... */
1310 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1311 goto out_add;
1312 }
1313 spin_lock(&dq_data_lock);
1314 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1315 if (inode->i_dquot[cnt] == NODQUOT)
1316 continue;
1317 if (check_bdq(inode->i_dquot[cnt], number, warn, warntype+cnt) == NO_QUOTA)
1318 goto warn_put_all;
1319 }
1320 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1321 if (inode->i_dquot[cnt] == NODQUOT)
1322 continue;
1323 dquot_incr_space(inode->i_dquot[cnt], number);
1324 }
1325 inode_add_bytes(inode, number);
1326 ret = QUOTA_OK;
1327warn_put_all:
1328 spin_unlock(&dq_data_lock);
1329 if (ret == QUOTA_OK)
1330 /* Dirtify all the dquots - this can block when journalling */
1331 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1332 if (inode->i_dquot[cnt])
1333 mark_dquot_dirty(inode->i_dquot[cnt]);
1334 flush_warnings(inode->i_dquot, warntype);
1335 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1336 return ret;
1337}
1338
1339/*
1340 * This operation can block, but only after everything is updated
1341 */
12095460 1342int dquot_alloc_inode(const struct inode *inode, qsize_t number)
1da177e4
LT
1343{
1344 int cnt, ret = NO_QUOTA;
1345 char warntype[MAXQUOTAS];
1346
d3be915f
IM
1347 /* First test before acquiring mutex - solves deadlocks when we
1348 * re-enter the quota code and are already holding the mutex */
1da177e4
LT
1349 if (IS_NOQUOTA(inode))
1350 return QUOTA_OK;
1351 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
8e893469 1352 warntype[cnt] = QUOTA_NL_NOWARN;
1da177e4
LT
1353 down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1354 if (IS_NOQUOTA(inode)) {
1355 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1356 return QUOTA_OK;
1357 }
1358 spin_lock(&dq_data_lock);
1359 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1360 if (inode->i_dquot[cnt] == NODQUOT)
1361 continue;
1362 if (check_idq(inode->i_dquot[cnt], number, warntype+cnt) == NO_QUOTA)
1363 goto warn_put_all;
1364 }
1365
1366 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1367 if (inode->i_dquot[cnt] == NODQUOT)
1368 continue;
1369 dquot_incr_inodes(inode->i_dquot[cnt], number);
1370 }
1371 ret = QUOTA_OK;
1372warn_put_all:
1373 spin_unlock(&dq_data_lock);
1374 if (ret == QUOTA_OK)
1375 /* Dirtify all the dquots - this can block when journalling */
1376 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1377 if (inode->i_dquot[cnt])
1378 mark_dquot_dirty(inode->i_dquot[cnt]);
087ee8d5 1379 flush_warnings(inode->i_dquot, warntype);
1da177e4
LT
1380 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1381 return ret;
1382}
1383
1384/*
1385 * This operation can block, but only after everything is updated
1386 */
1387int dquot_free_space(struct inode *inode, qsize_t number)
1388{
1389 unsigned int cnt;
657d3bfa 1390 char warntype[MAXQUOTAS];
1da177e4 1391
d3be915f
IM
1392 /* First test before acquiring mutex - solves deadlocks when we
1393 * re-enter the quota code and are already holding the mutex */
1da177e4
LT
1394 if (IS_NOQUOTA(inode)) {
1395out_sub:
1396 inode_sub_bytes(inode, number);
1397 return QUOTA_OK;
1398 }
657d3bfa 1399
1da177e4
LT
1400 down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1401 /* Now recheck reliably when holding dqptr_sem */
1402 if (IS_NOQUOTA(inode)) {
1403 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1404 goto out_sub;
1405 }
1406 spin_lock(&dq_data_lock);
1407 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1408 if (inode->i_dquot[cnt] == NODQUOT)
1409 continue;
657d3bfa 1410 warntype[cnt] = info_bdq_free(inode->i_dquot[cnt], number);
1da177e4
LT
1411 dquot_decr_space(inode->i_dquot[cnt], number);
1412 }
1413 inode_sub_bytes(inode, number);
1414 spin_unlock(&dq_data_lock);
1415 /* Dirtify all the dquots - this can block when journalling */
1416 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1417 if (inode->i_dquot[cnt])
1418 mark_dquot_dirty(inode->i_dquot[cnt]);
657d3bfa 1419 flush_warnings(inode->i_dquot, warntype);
1da177e4
LT
1420 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1421 return QUOTA_OK;
1422}
1423
1424/*
1425 * This operation can block, but only after everything is updated
1426 */
12095460 1427int dquot_free_inode(const struct inode *inode, qsize_t number)
1da177e4
LT
1428{
1429 unsigned int cnt;
657d3bfa 1430 char warntype[MAXQUOTAS];
1da177e4 1431
d3be915f
IM
1432 /* First test before acquiring mutex - solves deadlocks when we
1433 * re-enter the quota code and are already holding the mutex */
1da177e4
LT
1434 if (IS_NOQUOTA(inode))
1435 return QUOTA_OK;
657d3bfa 1436
1da177e4
LT
1437 down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1438 /* Now recheck reliably when holding dqptr_sem */
1439 if (IS_NOQUOTA(inode)) {
1440 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1441 return QUOTA_OK;
1442 }
1443 spin_lock(&dq_data_lock);
1444 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1445 if (inode->i_dquot[cnt] == NODQUOT)
1446 continue;
657d3bfa 1447 warntype[cnt] = info_idq_free(inode->i_dquot[cnt], number);
1da177e4
LT
1448 dquot_decr_inodes(inode->i_dquot[cnt], number);
1449 }
1450 spin_unlock(&dq_data_lock);
1451 /* Dirtify all the dquots - this can block when journalling */
1452 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1453 if (inode->i_dquot[cnt])
1454 mark_dquot_dirty(inode->i_dquot[cnt]);
657d3bfa 1455 flush_warnings(inode->i_dquot, warntype);
1da177e4
LT
1456 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1457 return QUOTA_OK;
1458}
1459
1460/*
1461 * Transfer the number of inode and blocks from one diskquota to an other.
1462 *
1463 * This operation can block, but only after everything is updated
1464 * A transaction must be started when entering this function.
1465 */
1466int dquot_transfer(struct inode *inode, struct iattr *iattr)
1467{
1468 qsize_t space;
1469 struct dquot *transfer_from[MAXQUOTAS];
1470 struct dquot *transfer_to[MAXQUOTAS];
1471 int cnt, ret = NO_QUOTA, chuid = (iattr->ia_valid & ATTR_UID) && inode->i_uid != iattr->ia_uid,
1472 chgid = (iattr->ia_valid & ATTR_GID) && inode->i_gid != iattr->ia_gid;
657d3bfa
JK
1473 char warntype_to[MAXQUOTAS];
1474 char warntype_from_inodes[MAXQUOTAS], warntype_from_space[MAXQUOTAS];
1da177e4 1475
d3be915f
IM
1476 /* First test before acquiring mutex - solves deadlocks when we
1477 * re-enter the quota code and are already holding the mutex */
1da177e4
LT
1478 if (IS_NOQUOTA(inode))
1479 return QUOTA_OK;
1480 /* Clear the arrays */
1481 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1482 transfer_to[cnt] = transfer_from[cnt] = NODQUOT;
657d3bfa 1483 warntype_to[cnt] = QUOTA_NL_NOWARN;
1da177e4
LT
1484 }
1485 down_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
1486 /* Now recheck reliably when holding dqptr_sem */
1487 if (IS_NOQUOTA(inode)) { /* File without quota accounting? */
1488 up_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
1489 return QUOTA_OK;
1490 }
1491 /* First build the transfer_to list - here we can block on
1492 * reading/instantiating of dquots. We know that the transaction for
1493 * us was already started so we don't violate lock ranking here */
1494 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1495 switch (cnt) {
1496 case USRQUOTA:
1497 if (!chuid)
1498 continue;
1499 transfer_to[cnt] = dqget(inode->i_sb, iattr->ia_uid, cnt);
1500 break;
1501 case GRPQUOTA:
1502 if (!chgid)
1503 continue;
1504 transfer_to[cnt] = dqget(inode->i_sb, iattr->ia_gid, cnt);
1505 break;
1506 }
1507 }
1508 spin_lock(&dq_data_lock);
1509 space = inode_get_bytes(inode);
1510 /* Build the transfer_from list and check the limits */
1511 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1512 if (transfer_to[cnt] == NODQUOT)
1513 continue;
1514 transfer_from[cnt] = inode->i_dquot[cnt];
657d3bfa
JK
1515 if (check_idq(transfer_to[cnt], 1, warntype_to + cnt) ==
1516 NO_QUOTA || check_bdq(transfer_to[cnt], space, 0,
1517 warntype_to + cnt) == NO_QUOTA)
1da177e4
LT
1518 goto warn_put_all;
1519 }
1520
1521 /*
1522 * Finally perform the needed transfer from transfer_from to transfer_to
1523 */
1524 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1525 /*
1526 * Skip changes for same uid or gid or for turned off quota-type.
1527 */
1528 if (transfer_to[cnt] == NODQUOT)
1529 continue;
1530
1531 /* Due to IO error we might not have transfer_from[] structure */
1532 if (transfer_from[cnt]) {
657d3bfa
JK
1533 warntype_from_inodes[cnt] =
1534 info_idq_free(transfer_from[cnt], 1);
1535 warntype_from_space[cnt] =
1536 info_bdq_free(transfer_from[cnt], space);
1da177e4
LT
1537 dquot_decr_inodes(transfer_from[cnt], 1);
1538 dquot_decr_space(transfer_from[cnt], space);
1539 }
1540
1541 dquot_incr_inodes(transfer_to[cnt], 1);
1542 dquot_incr_space(transfer_to[cnt], space);
1543
1544 inode->i_dquot[cnt] = transfer_to[cnt];
1545 }
1546 ret = QUOTA_OK;
1547warn_put_all:
1548 spin_unlock(&dq_data_lock);
1549 /* Dirtify all the dquots - this can block when journalling */
1550 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1551 if (transfer_from[cnt])
1552 mark_dquot_dirty(transfer_from[cnt]);
1553 if (transfer_to[cnt])
1554 mark_dquot_dirty(transfer_to[cnt]);
1555 }
657d3bfa
JK
1556 flush_warnings(transfer_to, warntype_to);
1557 flush_warnings(transfer_from, warntype_from_inodes);
1558 flush_warnings(transfer_from, warntype_from_space);
1da177e4
LT
1559
1560 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1561 if (ret == QUOTA_OK && transfer_from[cnt] != NODQUOT)
1562 dqput(transfer_from[cnt]);
1563 if (ret == NO_QUOTA && transfer_to[cnt] != NODQUOT)
1564 dqput(transfer_to[cnt]);
1565 }
1566 up_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
1567 return ret;
1568}
1569
b85f4b87
JK
1570/* Wrapper for transferring ownership of an inode */
1571int vfs_dq_transfer(struct inode *inode, struct iattr *iattr)
1572{
f55abc0f 1573 if (sb_any_quota_active(inode->i_sb) && !IS_NOQUOTA(inode)) {
b85f4b87
JK
1574 vfs_dq_init(inode);
1575 if (inode->i_sb->dq_op->transfer(inode, iattr) == NO_QUOTA)
1576 return 1;
1577 }
1578 return 0;
1579}
1580
1581
1da177e4
LT
1582/*
1583 * Write info of quota file to disk
1584 */
1585int dquot_commit_info(struct super_block *sb, int type)
1586{
1587 int ret;
1588 struct quota_info *dqopt = sb_dqopt(sb);
1589
d3be915f 1590 mutex_lock(&dqopt->dqio_mutex);
1da177e4 1591 ret = dqopt->ops[type]->write_file_info(sb, type);
d3be915f 1592 mutex_unlock(&dqopt->dqio_mutex);
1da177e4
LT
1593 return ret;
1594}
1595
1596/*
1597 * Definitions of diskquota operations.
1598 */
1599struct dquot_operations dquot_operations = {
1600 .initialize = dquot_initialize,
1601 .drop = dquot_drop,
1602 .alloc_space = dquot_alloc_space,
1603 .alloc_inode = dquot_alloc_inode,
1604 .free_space = dquot_free_space,
1605 .free_inode = dquot_free_inode,
1606 .transfer = dquot_transfer,
1607 .write_dquot = dquot_commit,
1608 .acquire_dquot = dquot_acquire,
1609 .release_dquot = dquot_release,
1610 .mark_dirty = dquot_mark_dquot_dirty,
74f783af
JK
1611 .write_info = dquot_commit_info,
1612 .alloc_dquot = dquot_alloc,
1613 .destroy_dquot = dquot_destroy,
1da177e4
LT
1614};
1615
1da177e4
LT
1616/*
1617 * Turn quota off on a device. type == -1 ==> quotaoff for all types (umount)
1618 */
f55abc0f 1619int vfs_quota_disable(struct super_block *sb, int type, unsigned int flags)
1da177e4 1620{
0ff5af83 1621 int cnt, ret = 0;
1da177e4
LT
1622 struct quota_info *dqopt = sb_dqopt(sb);
1623 struct inode *toputinode[MAXQUOTAS];
1da177e4 1624
f55abc0f
JK
1625 /* Cannot turn off usage accounting without turning off limits, or
1626 * suspend quotas and simultaneously turn quotas off. */
1627 if ((flags & DQUOT_USAGE_ENABLED && !(flags & DQUOT_LIMITS_ENABLED))
1628 || (flags & DQUOT_SUSPENDED && flags & (DQUOT_LIMITS_ENABLED |
1629 DQUOT_USAGE_ENABLED)))
1630 return -EINVAL;
1631
1da177e4 1632 /* We need to serialize quota_off() for device */
d3be915f 1633 mutex_lock(&dqopt->dqonoff_mutex);
9377abd0
JK
1634
1635 /*
1636 * Skip everything if there's nothing to do. We have to do this because
1637 * sometimes we are called when fill_super() failed and calling
1638 * sync_fs() in such cases does no good.
1639 */
f55abc0f 1640 if (!sb_any_quota_loaded(sb)) {
9377abd0
JK
1641 mutex_unlock(&dqopt->dqonoff_mutex);
1642 return 0;
1643 }
1da177e4
LT
1644 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1645 toputinode[cnt] = NULL;
1da177e4
LT
1646 if (type != -1 && cnt != type)
1647 continue;
f55abc0f 1648 if (!sb_has_quota_loaded(sb, cnt))
0ff5af83 1649 continue;
f55abc0f
JK
1650
1651 if (flags & DQUOT_SUSPENDED) {
1652 dqopt->flags |=
1653 dquot_state_flag(DQUOT_SUSPENDED, cnt);
1654 } else {
1655 dqopt->flags &= ~dquot_state_flag(flags, cnt);
1656 /* Turning off suspended quotas? */
1657 if (!sb_has_quota_loaded(sb, cnt) &&
1658 sb_has_quota_suspended(sb, cnt)) {
1659 dqopt->flags &= ~dquot_state_flag(
1660 DQUOT_SUSPENDED, cnt);
1661 iput(dqopt->files[cnt]);
1662 dqopt->files[cnt] = NULL;
1663 continue;
1664 }
0ff5af83 1665 }
f55abc0f
JK
1666
1667 /* We still have to keep quota loaded? */
1668 if (sb_has_quota_loaded(sb, cnt) && !(flags & DQUOT_SUSPENDED))
1da177e4 1669 continue;
1da177e4
LT
1670
1671 /* Note: these are blocking operations */
1672 drop_dquot_ref(sb, cnt);
1673 invalidate_dquots(sb, cnt);
1674 /*
1675 * Now all dquots should be invalidated, all writes done so we should be only
1676 * users of the info. No locks needed.
1677 */
1678 if (info_dirty(&dqopt->info[cnt]))
1679 sb->dq_op->write_info(sb, cnt);
1680 if (dqopt->ops[cnt]->free_file_info)
1681 dqopt->ops[cnt]->free_file_info(sb, cnt);
1682 put_quota_format(dqopt->info[cnt].dqi_format);
1683
1684 toputinode[cnt] = dqopt->files[cnt];
f55abc0f 1685 if (!sb_has_quota_loaded(sb, cnt))
0ff5af83 1686 dqopt->files[cnt] = NULL;
1da177e4
LT
1687 dqopt->info[cnt].dqi_flags = 0;
1688 dqopt->info[cnt].dqi_igrace = 0;
1689 dqopt->info[cnt].dqi_bgrace = 0;
1690 dqopt->ops[cnt] = NULL;
1691 }
d3be915f 1692 mutex_unlock(&dqopt->dqonoff_mutex);
ca785ec6
JK
1693
1694 /* Skip syncing and setting flags if quota files are hidden */
1695 if (dqopt->flags & DQUOT_QUOTA_SYS_FILE)
1696 goto put_inodes;
1697
1da177e4 1698 /* Sync the superblock so that buffers with quota data are written to
7b7b1ace 1699 * disk (and so userspace sees correct data afterwards). */
1da177e4
LT
1700 if (sb->s_op->sync_fs)
1701 sb->s_op->sync_fs(sb, 1);
1702 sync_blockdev(sb->s_bdev);
1703 /* Now the quota files are just ordinary files and we can set the
1704 * inode flags back. Moreover we discard the pagecache so that
1705 * userspace sees the writes we did bypassing the pagecache. We
1706 * must also discard the blockdev buffers so that we see the
1707 * changes done by userspace on the next quotaon() */
1708 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1709 if (toputinode[cnt]) {
d3be915f 1710 mutex_lock(&dqopt->dqonoff_mutex);
1da177e4
LT
1711 /* If quota was reenabled in the meantime, we have
1712 * nothing to do */
f55abc0f 1713 if (!sb_has_quota_loaded(sb, cnt)) {
7925409e 1714 mutex_lock_nested(&toputinode[cnt]->i_mutex, I_MUTEX_QUOTA);
1da177e4
LT
1715 toputinode[cnt]->i_flags &= ~(S_IMMUTABLE |
1716 S_NOATIME | S_NOQUOTA);
1717 truncate_inode_pages(&toputinode[cnt]->i_data, 0);
1b1dcc1b 1718 mutex_unlock(&toputinode[cnt]->i_mutex);
1da177e4 1719 mark_inode_dirty(toputinode[cnt]);
1da177e4 1720 }
d3be915f 1721 mutex_unlock(&dqopt->dqonoff_mutex);
ca785ec6
JK
1722 }
1723 if (sb->s_bdev)
1724 invalidate_bdev(sb->s_bdev);
1725put_inodes:
1726 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1727 if (toputinode[cnt]) {
0ff5af83 1728 /* On remount RO, we keep the inode pointer so that we
f55abc0f
JK
1729 * can reenable quota on the subsequent remount RW. We
1730 * have to check 'flags' variable and not use sb_has_
1731 * function because another quotaon / quotaoff could
1732 * change global state before we got here. We refuse
1733 * to suspend quotas when there is pending delete on
1734 * the quota file... */
1735 if (!(flags & DQUOT_SUSPENDED))
0ff5af83
JK
1736 iput(toputinode[cnt]);
1737 else if (!toputinode[cnt]->i_nlink)
1738 ret = -EBUSY;
1da177e4 1739 }
0ff5af83 1740 return ret;
1da177e4
LT
1741}
1742
f55abc0f
JK
1743int vfs_quota_off(struct super_block *sb, int type, int remount)
1744{
1745 return vfs_quota_disable(sb, type, remount ? DQUOT_SUSPENDED :
1746 (DQUOT_USAGE_ENABLED | DQUOT_LIMITS_ENABLED));
1747}
1748
1da177e4
LT
1749/*
1750 * Turn quotas on on a device
1751 */
1752
f55abc0f
JK
1753/*
1754 * Helper function to turn quotas on when we already have the inode of
1755 * quota file and no quota information is loaded.
1756 */
1757static int vfs_load_quota_inode(struct inode *inode, int type, int format_id,
1758 unsigned int flags)
1da177e4
LT
1759{
1760 struct quota_format_type *fmt = find_quota_format(format_id);
1761 struct super_block *sb = inode->i_sb;
1762 struct quota_info *dqopt = sb_dqopt(sb);
1763 int error;
1764 int oldflags = -1;
1765
1766 if (!fmt)
1767 return -ESRCH;
1768 if (!S_ISREG(inode->i_mode)) {
1769 error = -EACCES;
1770 goto out_fmt;
1771 }
1772 if (IS_RDONLY(inode)) {
1773 error = -EROFS;
1774 goto out_fmt;
1775 }
1776 if (!sb->s_op->quota_write || !sb->s_op->quota_read) {
1777 error = -EINVAL;
1778 goto out_fmt;
1779 }
f55abc0f
JK
1780 /* Usage always has to be set... */
1781 if (!(flags & DQUOT_USAGE_ENABLED)) {
1782 error = -EINVAL;
1783 goto out_fmt;
1784 }
1da177e4 1785
ca785ec6
JK
1786 if (!(dqopt->flags & DQUOT_QUOTA_SYS_FILE)) {
1787 /* As we bypass the pagecache we must now flush the inode so
1788 * that we see all the changes from userspace... */
1789 write_inode_now(inode, 1);
1790 /* And now flush the block cache so that kernel sees the
1791 * changes */
1792 invalidate_bdev(sb->s_bdev);
1793 }
1b1dcc1b 1794 mutex_lock(&inode->i_mutex);
d3be915f 1795 mutex_lock(&dqopt->dqonoff_mutex);
f55abc0f 1796 if (sb_has_quota_loaded(sb, type)) {
1da177e4
LT
1797 error = -EBUSY;
1798 goto out_lock;
1799 }
ca785ec6
JK
1800
1801 if (!(dqopt->flags & DQUOT_QUOTA_SYS_FILE)) {
1802 /* We don't want quota and atime on quota files (deadlocks
1803 * possible) Also nobody should write to the file - we use
1804 * special IO operations which ignore the immutable bit. */
1805 down_write(&dqopt->dqptr_sem);
1806 oldflags = inode->i_flags & (S_NOATIME | S_IMMUTABLE | S_NOQUOTA);
1807 inode->i_flags |= S_NOQUOTA | S_NOATIME | S_IMMUTABLE;
1808 up_write(&dqopt->dqptr_sem);
1809 sb->dq_op->drop(inode);
1810 }
1da177e4
LT
1811
1812 error = -EIO;
1813 dqopt->files[type] = igrab(inode);
1814 if (!dqopt->files[type])
1815 goto out_lock;
1816 error = -EINVAL;
1817 if (!fmt->qf_ops->check_quota_file(sb, type))
1818 goto out_file_init;
1819
1820 dqopt->ops[type] = fmt->qf_ops;
1821 dqopt->info[type].dqi_format = fmt;
0ff5af83 1822 dqopt->info[type].dqi_fmt_id = format_id;
1da177e4 1823 INIT_LIST_HEAD(&dqopt->info[type].dqi_dirty_list);
d3be915f 1824 mutex_lock(&dqopt->dqio_mutex);
1da177e4 1825 if ((error = dqopt->ops[type]->read_file_info(sb, type)) < 0) {
d3be915f 1826 mutex_unlock(&dqopt->dqio_mutex);
1da177e4
LT
1827 goto out_file_init;
1828 }
d3be915f 1829 mutex_unlock(&dqopt->dqio_mutex);
1b1dcc1b 1830 mutex_unlock(&inode->i_mutex);
f55abc0f 1831 dqopt->flags |= dquot_state_flag(flags, type);
1da177e4
LT
1832
1833 add_dquot_ref(sb, type);
d3be915f 1834 mutex_unlock(&dqopt->dqonoff_mutex);
1da177e4
LT
1835
1836 return 0;
1837
1838out_file_init:
1839 dqopt->files[type] = NULL;
1840 iput(inode);
1841out_lock:
d3be915f 1842 mutex_unlock(&dqopt->dqonoff_mutex);
1da177e4
LT
1843 if (oldflags != -1) {
1844 down_write(&dqopt->dqptr_sem);
1845 /* Set the flags back (in the case of accidental quotaon()
1846 * on a wrong file we don't want to mess up the flags) */
1847 inode->i_flags &= ~(S_NOATIME | S_NOQUOTA | S_IMMUTABLE);
1848 inode->i_flags |= oldflags;
1849 up_write(&dqopt->dqptr_sem);
1850 }
1b1dcc1b 1851 mutex_unlock(&inode->i_mutex);
1da177e4
LT
1852out_fmt:
1853 put_quota_format(fmt);
1854
1855 return error;
1856}
1857
0ff5af83
JK
1858/* Reenable quotas on remount RW */
1859static int vfs_quota_on_remount(struct super_block *sb, int type)
1860{
1861 struct quota_info *dqopt = sb_dqopt(sb);
1862 struct inode *inode;
1863 int ret;
f55abc0f 1864 unsigned int flags;
0ff5af83
JK
1865
1866 mutex_lock(&dqopt->dqonoff_mutex);
1867 if (!sb_has_quota_suspended(sb, type)) {
1868 mutex_unlock(&dqopt->dqonoff_mutex);
1869 return 0;
1870 }
0ff5af83
JK
1871 inode = dqopt->files[type];
1872 dqopt->files[type] = NULL;
f55abc0f
JK
1873 flags = dqopt->flags & dquot_state_flag(DQUOT_USAGE_ENABLED |
1874 DQUOT_LIMITS_ENABLED, type);
1875 dqopt->flags &= ~dquot_state_flag(DQUOT_STATE_FLAGS, type);
0ff5af83
JK
1876 mutex_unlock(&dqopt->dqonoff_mutex);
1877
f55abc0f
JK
1878 flags = dquot_generic_flag(flags, type);
1879 ret = vfs_load_quota_inode(inode, type, dqopt->info[type].dqi_fmt_id,
1880 flags);
0ff5af83
JK
1881 iput(inode);
1882
1883 return ret;
1884}
1885
77e69dac
AV
1886int vfs_quota_on_path(struct super_block *sb, int type, int format_id,
1887 struct path *path)
1888{
1889 int error = security_quota_on(path->dentry);
1890 if (error)
1891 return error;
1892 /* Quota file not on the same filesystem? */
1893 if (path->mnt->mnt_sb != sb)
1894 error = -EXDEV;
1895 else
f55abc0f
JK
1896 error = vfs_load_quota_inode(path->dentry->d_inode, type,
1897 format_id, DQUOT_USAGE_ENABLED |
1898 DQUOT_LIMITS_ENABLED);
77e69dac
AV
1899 return error;
1900}
1901
8264613d 1902int vfs_quota_on(struct super_block *sb, int type, int format_id, char *name,
0ff5af83 1903 int remount)
1da177e4 1904{
8264613d 1905 struct path path;
1da177e4
LT
1906 int error;
1907
0ff5af83
JK
1908 if (remount)
1909 return vfs_quota_on_remount(sb, type);
1910
8264613d 1911 error = kern_path(name, LOOKUP_FOLLOW, &path);
77e69dac 1912 if (!error) {
8264613d
AV
1913 error = vfs_quota_on_path(sb, type, format_id, &path);
1914 path_put(&path);
77e69dac 1915 }
1da177e4
LT
1916 return error;
1917}
1918
f55abc0f
JK
1919/*
1920 * More powerful function for turning on quotas allowing setting
1921 * of individual quota flags
1922 */
1923int vfs_quota_enable(struct inode *inode, int type, int format_id,
1924 unsigned int flags)
1925{
1926 int ret = 0;
1927 struct super_block *sb = inode->i_sb;
1928 struct quota_info *dqopt = sb_dqopt(sb);
1929
1930 /* Just unsuspend quotas? */
1931 if (flags & DQUOT_SUSPENDED)
1932 return vfs_quota_on_remount(sb, type);
1933 if (!flags)
1934 return 0;
1935 /* Just updating flags needed? */
1936 if (sb_has_quota_loaded(sb, type)) {
1937 mutex_lock(&dqopt->dqonoff_mutex);
1938 /* Now do a reliable test... */
1939 if (!sb_has_quota_loaded(sb, type)) {
1940 mutex_unlock(&dqopt->dqonoff_mutex);
1941 goto load_quota;
1942 }
1943 if (flags & DQUOT_USAGE_ENABLED &&
1944 sb_has_quota_usage_enabled(sb, type)) {
1945 ret = -EBUSY;
1946 goto out_lock;
1947 }
1948 if (flags & DQUOT_LIMITS_ENABLED &&
1949 sb_has_quota_limits_enabled(sb, type)) {
1950 ret = -EBUSY;
1951 goto out_lock;
1952 }
1953 sb_dqopt(sb)->flags |= dquot_state_flag(flags, type);
1954out_lock:
1955 mutex_unlock(&dqopt->dqonoff_mutex);
1956 return ret;
1957 }
1958
1959load_quota:
1960 return vfs_load_quota_inode(inode, type, format_id, flags);
1961}
1962
1da177e4
LT
1963/*
1964 * This function is used when filesystem needs to initialize quotas
1965 * during mount time.
1966 */
84de856e
CH
1967int vfs_quota_on_mount(struct super_block *sb, char *qf_name,
1968 int format_id, int type)
1da177e4 1969{
84de856e 1970 struct dentry *dentry;
1da177e4
LT
1971 int error;
1972
2fa389c5 1973 dentry = lookup_one_len(qf_name, sb->s_root, strlen(qf_name));
84de856e
CH
1974 if (IS_ERR(dentry))
1975 return PTR_ERR(dentry);
1976
154f484b
JK
1977 if (!dentry->d_inode) {
1978 error = -ENOENT;
1979 goto out;
1980 }
1981
1da177e4 1982 error = security_quota_on(dentry);
84de856e 1983 if (!error)
f55abc0f
JK
1984 error = vfs_load_quota_inode(dentry->d_inode, type, format_id,
1985 DQUOT_USAGE_ENABLED | DQUOT_LIMITS_ENABLED);
84de856e 1986
154f484b 1987out:
84de856e
CH
1988 dput(dentry);
1989 return error;
1da177e4
LT
1990}
1991
b85f4b87
JK
1992/* Wrapper to turn on quotas when remounting rw */
1993int vfs_dq_quota_on_remount(struct super_block *sb)
1994{
1995 int cnt;
1996 int ret = 0, err;
1997
1998 if (!sb->s_qcop || !sb->s_qcop->quota_on)
1999 return -ENOSYS;
2000 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
2001 err = sb->s_qcop->quota_on(sb, cnt, 0, NULL, 1);
2002 if (err < 0 && !ret)
2003 ret = err;
2004 }
2005 return ret;
2006}
2007
12095460
JK
2008static inline qsize_t qbtos(qsize_t blocks)
2009{
2010 return blocks << QIF_DQBLKSIZE_BITS;
2011}
2012
2013static inline qsize_t stoqb(qsize_t space)
2014{
2015 return (space + QIF_DQBLKSIZE - 1) >> QIF_DQBLKSIZE_BITS;
2016}
2017
1da177e4
LT
2018/* Generic routine for getting common part of quota structure */
2019static void do_get_dqblk(struct dquot *dquot, struct if_dqblk *di)
2020{
2021 struct mem_dqblk *dm = &dquot->dq_dqb;
2022
2023 spin_lock(&dq_data_lock);
12095460
JK
2024 di->dqb_bhardlimit = stoqb(dm->dqb_bhardlimit);
2025 di->dqb_bsoftlimit = stoqb(dm->dqb_bsoftlimit);
1da177e4
LT
2026 di->dqb_curspace = dm->dqb_curspace;
2027 di->dqb_ihardlimit = dm->dqb_ihardlimit;
2028 di->dqb_isoftlimit = dm->dqb_isoftlimit;
2029 di->dqb_curinodes = dm->dqb_curinodes;
2030 di->dqb_btime = dm->dqb_btime;
2031 di->dqb_itime = dm->dqb_itime;
2032 di->dqb_valid = QIF_ALL;
2033 spin_unlock(&dq_data_lock);
2034}
2035
2036int vfs_get_dqblk(struct super_block *sb, int type, qid_t id, struct if_dqblk *di)
2037{
2038 struct dquot *dquot;
2039
d3be915f 2040 mutex_lock(&sb_dqopt(sb)->dqonoff_mutex);
1da177e4 2041 if (!(dquot = dqget(sb, id, type))) {
d3be915f 2042 mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
1da177e4
LT
2043 return -ESRCH;
2044 }
2045 do_get_dqblk(dquot, di);
2046 dqput(dquot);
d3be915f 2047 mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
1da177e4
LT
2048 return 0;
2049}
2050
2051/* Generic routine for setting common part of quota structure */
338bf9af 2052static int do_set_dqblk(struct dquot *dquot, struct if_dqblk *di)
1da177e4
LT
2053{
2054 struct mem_dqblk *dm = &dquot->dq_dqb;
2055 int check_blim = 0, check_ilim = 0;
338bf9af
AP
2056 struct mem_dqinfo *dqi = &sb_dqopt(dquot->dq_sb)->info[dquot->dq_type];
2057
2058 if ((di->dqb_valid & QIF_BLIMITS &&
2059 (di->dqb_bhardlimit > dqi->dqi_maxblimit ||
2060 di->dqb_bsoftlimit > dqi->dqi_maxblimit)) ||
2061 (di->dqb_valid & QIF_ILIMITS &&
2062 (di->dqb_ihardlimit > dqi->dqi_maxilimit ||
2063 di->dqb_isoftlimit > dqi->dqi_maxilimit)))
2064 return -ERANGE;
1da177e4
LT
2065
2066 spin_lock(&dq_data_lock);
2067 if (di->dqb_valid & QIF_SPACE) {
2068 dm->dqb_curspace = di->dqb_curspace;
2069 check_blim = 1;
4d59bce4 2070 __set_bit(DQ_LASTSET_B + QIF_SPACE_B, &dquot->dq_flags);
1da177e4
LT
2071 }
2072 if (di->dqb_valid & QIF_BLIMITS) {
12095460
JK
2073 dm->dqb_bsoftlimit = qbtos(di->dqb_bsoftlimit);
2074 dm->dqb_bhardlimit = qbtos(di->dqb_bhardlimit);
1da177e4 2075 check_blim = 1;
4d59bce4 2076 __set_bit(DQ_LASTSET_B + QIF_BLIMITS_B, &dquot->dq_flags);
1da177e4
LT
2077 }
2078 if (di->dqb_valid & QIF_INODES) {
2079 dm->dqb_curinodes = di->dqb_curinodes;
2080 check_ilim = 1;
4d59bce4 2081 __set_bit(DQ_LASTSET_B + QIF_INODES_B, &dquot->dq_flags);
1da177e4
LT
2082 }
2083 if (di->dqb_valid & QIF_ILIMITS) {
2084 dm->dqb_isoftlimit = di->dqb_isoftlimit;
2085 dm->dqb_ihardlimit = di->dqb_ihardlimit;
2086 check_ilim = 1;
4d59bce4 2087 __set_bit(DQ_LASTSET_B + QIF_ILIMITS_B, &dquot->dq_flags);
1da177e4 2088 }
4d59bce4 2089 if (di->dqb_valid & QIF_BTIME) {
1da177e4 2090 dm->dqb_btime = di->dqb_btime;
4d59bce4
JK
2091 __set_bit(DQ_LASTSET_B + QIF_BTIME_B, &dquot->dq_flags);
2092 }
2093 if (di->dqb_valid & QIF_ITIME) {
1da177e4 2094 dm->dqb_itime = di->dqb_itime;
4d59bce4
JK
2095 __set_bit(DQ_LASTSET_B + QIF_ITIME_B, &dquot->dq_flags);
2096 }
1da177e4
LT
2097
2098 if (check_blim) {
12095460 2099 if (!dm->dqb_bsoftlimit || dm->dqb_curspace < dm->dqb_bsoftlimit) {
1da177e4
LT
2100 dm->dqb_btime = 0;
2101 clear_bit(DQ_BLKS_B, &dquot->dq_flags);
2102 }
2103 else if (!(di->dqb_valid & QIF_BTIME)) /* Set grace only if user hasn't provided his own... */
338bf9af 2104 dm->dqb_btime = get_seconds() + dqi->dqi_bgrace;
1da177e4
LT
2105 }
2106 if (check_ilim) {
2107 if (!dm->dqb_isoftlimit || dm->dqb_curinodes < dm->dqb_isoftlimit) {
2108 dm->dqb_itime = 0;
2109 clear_bit(DQ_INODES_B, &dquot->dq_flags);
2110 }
2111 else if (!(di->dqb_valid & QIF_ITIME)) /* Set grace only if user hasn't provided his own... */
338bf9af 2112 dm->dqb_itime = get_seconds() + dqi->dqi_igrace;
1da177e4
LT
2113 }
2114 if (dm->dqb_bhardlimit || dm->dqb_bsoftlimit || dm->dqb_ihardlimit || dm->dqb_isoftlimit)
2115 clear_bit(DQ_FAKE_B, &dquot->dq_flags);
2116 else
2117 set_bit(DQ_FAKE_B, &dquot->dq_flags);
2118 spin_unlock(&dq_data_lock);
2119 mark_dquot_dirty(dquot);
338bf9af
AP
2120
2121 return 0;
1da177e4
LT
2122}
2123
2124int vfs_set_dqblk(struct super_block *sb, int type, qid_t id, struct if_dqblk *di)
2125{
2126 struct dquot *dquot;
338bf9af 2127 int rc;
1da177e4 2128
d3be915f 2129 mutex_lock(&sb_dqopt(sb)->dqonoff_mutex);
f55abc0f
JK
2130 dquot = dqget(sb, id, type);
2131 if (!dquot) {
2132 rc = -ESRCH;
2133 goto out;
1da177e4 2134 }
338bf9af 2135 rc = do_set_dqblk(dquot, di);
1da177e4 2136 dqput(dquot);
f55abc0f 2137out:
d3be915f 2138 mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
338bf9af 2139 return rc;
1da177e4
LT
2140}
2141
2142/* Generic routine for getting common part of quota file information */
2143int vfs_get_dqinfo(struct super_block *sb, int type, struct if_dqinfo *ii)
2144{
2145 struct mem_dqinfo *mi;
2146
d3be915f 2147 mutex_lock(&sb_dqopt(sb)->dqonoff_mutex);
f55abc0f 2148 if (!sb_has_quota_active(sb, type)) {
d3be915f 2149 mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
1da177e4
LT
2150 return -ESRCH;
2151 }
2152 mi = sb_dqopt(sb)->info + type;
2153 spin_lock(&dq_data_lock);
2154 ii->dqi_bgrace = mi->dqi_bgrace;
2155 ii->dqi_igrace = mi->dqi_igrace;
2156 ii->dqi_flags = mi->dqi_flags & DQF_MASK;
2157 ii->dqi_valid = IIF_ALL;
2158 spin_unlock(&dq_data_lock);
d3be915f 2159 mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
1da177e4
LT
2160 return 0;
2161}
2162
2163/* Generic routine for setting common part of quota file information */
2164int vfs_set_dqinfo(struct super_block *sb, int type, struct if_dqinfo *ii)
2165{
2166 struct mem_dqinfo *mi;
f55abc0f 2167 int err = 0;
1da177e4 2168
d3be915f 2169 mutex_lock(&sb_dqopt(sb)->dqonoff_mutex);
f55abc0f
JK
2170 if (!sb_has_quota_active(sb, type)) {
2171 err = -ESRCH;
2172 goto out;
1da177e4
LT
2173 }
2174 mi = sb_dqopt(sb)->info + type;
2175 spin_lock(&dq_data_lock);
2176 if (ii->dqi_valid & IIF_BGRACE)
2177 mi->dqi_bgrace = ii->dqi_bgrace;
2178 if (ii->dqi_valid & IIF_IGRACE)
2179 mi->dqi_igrace = ii->dqi_igrace;
2180 if (ii->dqi_valid & IIF_FLAGS)
2181 mi->dqi_flags = (mi->dqi_flags & ~DQF_MASK) | (ii->dqi_flags & DQF_MASK);
2182 spin_unlock(&dq_data_lock);
2183 mark_info_dirty(sb, type);
2184 /* Force write to disk */
2185 sb->dq_op->write_info(sb, type);
f55abc0f 2186out:
d3be915f 2187 mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
f55abc0f 2188 return err;
1da177e4
LT
2189}
2190
2191struct quotactl_ops vfs_quotactl_ops = {
2192 .quota_on = vfs_quota_on,
2193 .quota_off = vfs_quota_off,
2194 .quota_sync = vfs_quota_sync,
2195 .get_info = vfs_get_dqinfo,
2196 .set_info = vfs_set_dqinfo,
2197 .get_dqblk = vfs_get_dqblk,
2198 .set_dqblk = vfs_set_dqblk
2199};
2200
2201static ctl_table fs_dqstats_table[] = {
2202 {
2203 .ctl_name = FS_DQ_LOOKUPS,
2204 .procname = "lookups",
2205 .data = &dqstats.lookups,
2206 .maxlen = sizeof(int),
2207 .mode = 0444,
2208 .proc_handler = &proc_dointvec,
2209 },
2210 {
2211 .ctl_name = FS_DQ_DROPS,
2212 .procname = "drops",
2213 .data = &dqstats.drops,
2214 .maxlen = sizeof(int),
2215 .mode = 0444,
2216 .proc_handler = &proc_dointvec,
2217 },
2218 {
2219 .ctl_name = FS_DQ_READS,
2220 .procname = "reads",
2221 .data = &dqstats.reads,
2222 .maxlen = sizeof(int),
2223 .mode = 0444,
2224 .proc_handler = &proc_dointvec,
2225 },
2226 {
2227 .ctl_name = FS_DQ_WRITES,
2228 .procname = "writes",
2229 .data = &dqstats.writes,
2230 .maxlen = sizeof(int),
2231 .mode = 0444,
2232 .proc_handler = &proc_dointvec,
2233 },
2234 {
2235 .ctl_name = FS_DQ_CACHE_HITS,
2236 .procname = "cache_hits",
2237 .data = &dqstats.cache_hits,
2238 .maxlen = sizeof(int),
2239 .mode = 0444,
2240 .proc_handler = &proc_dointvec,
2241 },
2242 {
2243 .ctl_name = FS_DQ_ALLOCATED,
2244 .procname = "allocated_dquots",
2245 .data = &dqstats.allocated_dquots,
2246 .maxlen = sizeof(int),
2247 .mode = 0444,
2248 .proc_handler = &proc_dointvec,
2249 },
2250 {
2251 .ctl_name = FS_DQ_FREE,
2252 .procname = "free_dquots",
2253 .data = &dqstats.free_dquots,
2254 .maxlen = sizeof(int),
2255 .mode = 0444,
2256 .proc_handler = &proc_dointvec,
2257 },
2258 {
2259 .ctl_name = FS_DQ_SYNCS,
2260 .procname = "syncs",
2261 .data = &dqstats.syncs,
2262 .maxlen = sizeof(int),
2263 .mode = 0444,
2264 .proc_handler = &proc_dointvec,
2265 },
8e893469 2266#ifdef CONFIG_PRINT_QUOTA_WARNING
1da177e4
LT
2267 {
2268 .ctl_name = FS_DQ_WARNINGS,
2269 .procname = "warnings",
2270 .data = &flag_print_warnings,
2271 .maxlen = sizeof(int),
2272 .mode = 0644,
2273 .proc_handler = &proc_dointvec,
2274 },
8e893469 2275#endif
1da177e4
LT
2276 { .ctl_name = 0 },
2277};
2278
2279static ctl_table fs_table[] = {
2280 {
2281 .ctl_name = FS_DQSTATS,
2282 .procname = "quota",
2283 .mode = 0555,
2284 .child = fs_dqstats_table,
2285 },
2286 { .ctl_name = 0 },
2287};
2288
2289static ctl_table sys_table[] = {
2290 {
2291 .ctl_name = CTL_FS,
2292 .procname = "fs",
2293 .mode = 0555,
2294 .child = fs_table,
2295 },
2296 { .ctl_name = 0 },
2297};
2298
2299static int __init dquot_init(void)
2300{
2301 int i;
2302 unsigned long nr_hash, order;
2303
2304 printk(KERN_NOTICE "VFS: Disk quotas %s\n", __DQUOT_VERSION__);
2305
0b4d4147 2306 register_sysctl_table(sys_table);
1da177e4 2307
20c2df83 2308 dquot_cachep = kmem_cache_create("dquot",
1da177e4 2309 sizeof(struct dquot), sizeof(unsigned long) * 4,
fffb60f9
PJ
2310 (SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT|
2311 SLAB_MEM_SPREAD|SLAB_PANIC),
20c2df83 2312 NULL);
1da177e4
LT
2313
2314 order = 0;
2315 dquot_hash = (struct hlist_head *)__get_free_pages(GFP_ATOMIC, order);
2316 if (!dquot_hash)
2317 panic("Cannot create dquot hash table");
2318
2319 /* Find power-of-two hlist_heads which can fit into allocation */
2320 nr_hash = (1UL << order) * PAGE_SIZE / sizeof(struct hlist_head);
2321 dq_hash_bits = 0;
2322 do {
2323 dq_hash_bits++;
2324 } while (nr_hash >> dq_hash_bits);
2325 dq_hash_bits--;
2326
2327 nr_hash = 1UL << dq_hash_bits;
2328 dq_hash_mask = nr_hash - 1;
2329 for (i = 0; i < nr_hash; i++)
2330 INIT_HLIST_HEAD(dquot_hash + i);
2331
2332 printk("Dquot-cache hash table entries: %ld (order %ld, %ld bytes)\n",
2333 nr_hash, order, (PAGE_SIZE << order));
2334
8e1f936b 2335 register_shrinker(&dqcache_shrinker);
1da177e4 2336
8e893469
JK
2337#ifdef CONFIG_QUOTA_NETLINK_INTERFACE
2338 if (genl_register_family(&quota_genl_family) != 0)
2339 printk(KERN_ERR "VFS: Failed to create quota netlink interface.\n");
2340#endif
2341
1da177e4
LT
2342 return 0;
2343}
2344module_init(dquot_init);
2345
2346EXPORT_SYMBOL(register_quota_format);
2347EXPORT_SYMBOL(unregister_quota_format);
2348EXPORT_SYMBOL(dqstats);
2349EXPORT_SYMBOL(dq_data_lock);
f55abc0f 2350EXPORT_SYMBOL(vfs_quota_enable);
1da177e4 2351EXPORT_SYMBOL(vfs_quota_on);
77e69dac 2352EXPORT_SYMBOL(vfs_quota_on_path);
1da177e4 2353EXPORT_SYMBOL(vfs_quota_on_mount);
f55abc0f 2354EXPORT_SYMBOL(vfs_quota_disable);
1da177e4 2355EXPORT_SYMBOL(vfs_quota_off);
12c77527 2356EXPORT_SYMBOL(dquot_scan_active);
1da177e4
LT
2357EXPORT_SYMBOL(vfs_quota_sync);
2358EXPORT_SYMBOL(vfs_get_dqinfo);
2359EXPORT_SYMBOL(vfs_set_dqinfo);
2360EXPORT_SYMBOL(vfs_get_dqblk);
2361EXPORT_SYMBOL(vfs_set_dqblk);
2362EXPORT_SYMBOL(dquot_commit);
2363EXPORT_SYMBOL(dquot_commit_info);
2364EXPORT_SYMBOL(dquot_acquire);
2365EXPORT_SYMBOL(dquot_release);
2366EXPORT_SYMBOL(dquot_mark_dquot_dirty);
2367EXPORT_SYMBOL(dquot_initialize);
2368EXPORT_SYMBOL(dquot_drop);
3d9ea253 2369EXPORT_SYMBOL(dquot_drop_locked);
b85f4b87 2370EXPORT_SYMBOL(vfs_dq_drop);
3d9ea253
JK
2371EXPORT_SYMBOL(dqget);
2372EXPORT_SYMBOL(dqput);
2373EXPORT_SYMBOL(dquot_is_cached);
1da177e4
LT
2374EXPORT_SYMBOL(dquot_alloc_space);
2375EXPORT_SYMBOL(dquot_alloc_inode);
2376EXPORT_SYMBOL(dquot_free_space);
2377EXPORT_SYMBOL(dquot_free_inode);
2378EXPORT_SYMBOL(dquot_transfer);
b85f4b87
JK
2379EXPORT_SYMBOL(vfs_dq_transfer);
2380EXPORT_SYMBOL(vfs_dq_quota_on_remount);