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