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