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