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