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