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