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