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