blob: 0ccd4ba3a2467f6783a396686b5ae20266ba515b [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Implementation of the diskquota system for the LINUX operating system. QUOTA
3 * is implemented using the BSD system call interface as the means of
4 * communication with the user level. This file contains the generic routines
5 * called by the different filesystems on allocation of an inode or block.
6 * These routines take care of the administration needed to have a consistent
7 * diskquota tracking system. The ideas of both user and group quotas are based
8 * on the Melbourne quota system as used on BSD derived systems. The internal
9 * implementation is based on one of the several variants of the LINUX
10 * inode-subsystem with added complexity of the diskquota system.
11 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070012 * Author: Marco van Wieringen <mvw@planets.elm.net>
13 *
14 * Fixes: Dmitry Gorodchanin <pgmdsg@ibi.com>, 11 Feb 96
15 *
16 * Revised list management to avoid races
17 * -- Bill Hawes, <whawes@star.net>, 9/98
18 *
19 * Fixed races in dquot_transfer(), dqget() and dquot_alloc_...().
20 * As the consequence the locking was moved from dquot_decr_...(),
21 * dquot_incr_...() to calling functions.
22 * invalidate_dquots() now writes modified dquots.
23 * Serialized quota_off() and quota_on() for mount point.
24 * Fixed a few bugs in grow_dquots().
25 * Fixed deadlock in write_dquot() - we no longer account quotas on
26 * quota files
27 * remove_dquot_ref() moved to inode.c - it now traverses through inodes
28 * add_dquot_ref() restarts after blocking
29 * Added check for bogus uid and fixed check for group in quotactl.
30 * Jan Kara, <jack@suse.cz>, sponsored by SuSE CR, 10-11/99
31 *
32 * Used struct list_head instead of own list struct
33 * Invalidation of referenced dquots is no longer possible
34 * Improved free_dquots list management
35 * Quota and i_blocks are now updated in one place to avoid races
36 * Warnings are now delayed so we won't block in critical section
37 * Write updated not to require dquot lock
38 * Jan Kara, <jack@suse.cz>, 9/2000
39 *
40 * Added dynamic quota structure allocation
41 * Jan Kara <jack@suse.cz> 12/2000
42 *
43 * Rewritten quota interface. Implemented new quota format and
44 * formats registering.
45 * Jan Kara, <jack@suse.cz>, 2001,2002
46 *
47 * New SMP locking.
48 * Jan Kara, <jack@suse.cz>, 10/2002
49 *
50 * Added journalled quota support, fix lock inversion problems
51 * Jan Kara, <jack@suse.cz>, 2003,2004
52 *
53 * (C) Copyright 1994 - 1997 Marco van Wieringen
54 */
55
56#include <linux/errno.h>
57#include <linux/kernel.h>
58#include <linux/fs.h>
59#include <linux/mount.h>
60#include <linux/mm.h>
61#include <linux/time.h>
62#include <linux/types.h>
63#include <linux/string.h>
64#include <linux/fcntl.h>
65#include <linux/stat.h>
66#include <linux/tty.h>
67#include <linux/file.h>
68#include <linux/slab.h>
69#include <linux/sysctl.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070070#include <linux/init.h>
71#include <linux/module.h>
72#include <linux/proc_fs.h>
73#include <linux/security.h>
Al Viro40401532012-02-13 03:58:52 +000074#include <linux/sched.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070075#include <linux/kmod.h>
76#include <linux/namei.h>
Randy Dunlap16f7e0f2006-01-11 12:17:46 -080077#include <linux/capability.h>
Adrian Bunkbe586ba2005-11-07 00:59:35 -080078#include <linux/quotaops.h>
Dave Chinner55fa6092011-03-22 22:23:40 +110079#include "../internal.h" /* ugh */
Linus Torvalds1da177e2005-04-16 15:20:36 -070080
Jeff Liuf3da9312012-05-28 23:40:17 +080081#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070082
Linus Torvalds1da177e2005-04-16 15:20:36 -070083/*
Jan Karacc334122009-01-12 17:23:05 +010084 * There are three quota SMP locks. dq_list_lock protects all lists with quotas
Dmitry Monakhovdde95882010-04-26 20:03:33 +040085 * and quota formats.
Jan Karacc334122009-01-12 17:23:05 +010086 * dq_data_lock protects data from dq_dqb and also mem_dqinfo structures and
87 * also guards consistency of dquot->dq_dqb with inode->i_blocks, i_bytes.
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 * i_blocks and i_bytes updates itself are guarded by i_lock acquired directly
Jan Karacc334122009-01-12 17:23:05 +010089 * in inode_add_bytes() and inode_sub_bytes(). dq_state_lock protects
90 * modifications of quota state (on quotaon and quotaoff) and readers who care
91 * about latest values take it as well.
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 *
Jan Karacc334122009-01-12 17:23:05 +010093 * The spinlock ordering is hence: dq_data_lock > dq_list_lock > i_lock,
94 * dq_list_lock > dq_state_lock
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 *
96 * Note that some things (eg. sb pointer, type, id) doesn't change during
97 * the life of the dquot structure and so needn't to be protected by a lock
98 *
Niu Yaweib9ba6f92014-06-04 12:23:19 +080099 * Operation accessing dquots via inode pointers are protected by dquot_srcu.
100 * Operation of reading pointer needs srcu_read_lock(&dquot_srcu), and
101 * synchronize_srcu(&dquot_srcu) is called after clearing pointers from
102 * inode and before dropping dquot references to avoid use of dquots after
103 * they are freed. dq_data_lock is used to serialize the pointer setting and
104 * clearing operations.
Jan Kara26245c92010-01-06 17:20:35 +0100105 * Special care needs to be taken about S_NOQUOTA inode flag (marking that
106 * inode is a quota file). Functions adding pointers from inode to dquots have
Niu Yaweib9ba6f92014-06-04 12:23:19 +0800107 * to check this flag under dq_data_lock and then (if S_NOQUOTA is not set) they
108 * have to do all pointer modifications before dropping dq_data_lock. This makes
Jan Kara26245c92010-01-06 17:20:35 +0100109 * sure they cannot race with quotaon which first sets S_NOQUOTA flag and
110 * then drops all pointers to dquots from an inode.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 *
Ingo Molnard3be9152006-03-23 03:00:29 -0800112 * Each dquot has its dq_lock mutex. Locked dquots might not be referenced
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 * from inodes (dquot_alloc_space() and such don't check the dq_lock).
114 * Currently dquot is locked only when it is being read to memory (or space for
115 * it is being allocated) on the first dqget() and when it is being released on
116 * the last dqput(). The allocation and release oparations are serialized by
117 * the dq_lock and by checking the use count in dquot_release(). Write
118 * operations on dquots don't hold dq_lock as they copy data under dq_data_lock
119 * spinlock to internal buffers before writing.
120 *
121 * Lock ordering (including related VFS locks) is the following:
Niu Yaweib9ba6f92014-06-04 12:23:19 +0800122 * dqonoff_mutex > i_mutex > journal_lock > dquot->dq_lock > dqio_mutex
Jan Karaa80b12c2012-04-25 21:15:53 +0200123 * dqonoff_mutex > i_mutex comes from dquot_quota_sync, dquot_enable, etc.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 */
125
Jan Karac5166102009-01-26 15:32:46 +0100126static __cacheline_aligned_in_smp DEFINE_SPINLOCK(dq_list_lock);
127static __cacheline_aligned_in_smp DEFINE_SPINLOCK(dq_state_lock);
128__cacheline_aligned_in_smp DEFINE_SPINLOCK(dq_data_lock);
Mingming Cao08d03502009-01-14 16:19:32 +0100129EXPORT_SYMBOL(dq_data_lock);
Niu Yaweib9ba6f92014-06-04 12:23:19 +0800130DEFINE_STATIC_SRCU(dquot_srcu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131
Jiaying Zhangfb5ffb02010-07-20 16:54:43 +0200132void __quota_error(struct super_block *sb, const char *func,
Joe Perches055adcb2010-11-23 18:49:54 -0800133 const char *fmt, ...)
Jiaying Zhangfb5ffb02010-07-20 16:54:43 +0200134{
Jiaying Zhangfb5ffb02010-07-20 16:54:43 +0200135 if (printk_ratelimit()) {
Joe Perches055adcb2010-11-23 18:49:54 -0800136 va_list args;
137 struct va_format vaf;
138
Jiaying Zhangfb5ffb02010-07-20 16:54:43 +0200139 va_start(args, fmt);
Joe Perches055adcb2010-11-23 18:49:54 -0800140
141 vaf.fmt = fmt;
142 vaf.va = &args;
143
144 printk(KERN_ERR "Quota error (device %s): %s: %pV\n",
145 sb->s_id, func, &vaf);
146
Jiaying Zhangfb5ffb02010-07-20 16:54:43 +0200147 va_end(args);
148 }
149}
150EXPORT_SYMBOL(__quota_error);
151
Sergey Senozhatskyda8d1ba2010-04-26 12:09:26 +0200152#if defined(CONFIG_QUOTA_DEBUG) || defined(CONFIG_PRINT_QUOTA_WARNING)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153static char *quotatypes[] = INITQFNAMES;
Sergey Senozhatskyda8d1ba2010-04-26 12:09:26 +0200154#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155static struct quota_format_type *quota_formats; /* List of registered formats */
156static struct quota_module_name module_names[] = INIT_QUOTA_MODULE_NAMES;
157
158/* SLAB cache for dquot structures */
Christoph Lametere18b8902006-12-06 20:33:20 -0800159static struct kmem_cache *dquot_cachep;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160
161int register_quota_format(struct quota_format_type *fmt)
162{
163 spin_lock(&dq_list_lock);
164 fmt->qf_next = quota_formats;
165 quota_formats = fmt;
166 spin_unlock(&dq_list_lock);
167 return 0;
168}
Mingming Cao08d03502009-01-14 16:19:32 +0100169EXPORT_SYMBOL(register_quota_format);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170
171void unregister_quota_format(struct quota_format_type *fmt)
172{
173 struct quota_format_type **actqf;
174
175 spin_lock(&dq_list_lock);
Jan Kara268157b2009-01-27 15:47:22 +0100176 for (actqf = &quota_formats; *actqf && *actqf != fmt;
177 actqf = &(*actqf)->qf_next)
178 ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 if (*actqf)
180 *actqf = (*actqf)->qf_next;
181 spin_unlock(&dq_list_lock);
182}
Mingming Cao08d03502009-01-14 16:19:32 +0100183EXPORT_SYMBOL(unregister_quota_format);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184
185static struct quota_format_type *find_quota_format(int id)
186{
187 struct quota_format_type *actqf;
188
189 spin_lock(&dq_list_lock);
Jan Kara268157b2009-01-27 15:47:22 +0100190 for (actqf = quota_formats; actqf && actqf->qf_fmt_id != id;
191 actqf = actqf->qf_next)
192 ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 if (!actqf || !try_module_get(actqf->qf_owner)) {
194 int qm;
195
196 spin_unlock(&dq_list_lock);
197
Jan Kara268157b2009-01-27 15:47:22 +0100198 for (qm = 0; module_names[qm].qm_fmt_id &&
199 module_names[qm].qm_fmt_id != id; qm++)
200 ;
201 if (!module_names[qm].qm_fmt_id ||
202 request_module(module_names[qm].qm_mod_name))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 return NULL;
204
205 spin_lock(&dq_list_lock);
Jan Kara268157b2009-01-27 15:47:22 +0100206 for (actqf = quota_formats; actqf && actqf->qf_fmt_id != id;
207 actqf = actqf->qf_next)
208 ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 if (actqf && !try_module_get(actqf->qf_owner))
210 actqf = NULL;
211 }
212 spin_unlock(&dq_list_lock);
213 return actqf;
214}
215
216static void put_quota_format(struct quota_format_type *fmt)
217{
218 module_put(fmt->qf_owner);
219}
220
221/*
222 * Dquot List Management:
223 * The quota code uses three lists for dquot management: the inuse_list,
224 * free_dquots, and dquot_hash[] array. A single dquot structure may be
225 * on all three lists, depending on its current state.
226 *
227 * All dquots are placed to the end of inuse_list when first created, and this
228 * list is used for invalidate operation, which must look at every dquot.
229 *
230 * Unused dquots (dq_count == 0) are added to the free_dquots list when freed,
231 * and this list is searched whenever we need an available dquot. Dquots are
232 * removed from the list as soon as they are used again, and
233 * dqstats.free_dquots gives the number of dquots on the list. When
234 * dquot is invalidated it's completely released from memory.
235 *
236 * Dquots with a specific identity (device, type and id) are placed on
237 * one of the dquot_hash[] hash chains. The provides an efficient search
238 * mechanism to locate a specific dquot.
239 */
240
241static LIST_HEAD(inuse_list);
242static LIST_HEAD(free_dquots);
243static unsigned int dq_hash_bits, dq_hash_mask;
244static struct hlist_head *dquot_hash;
245
246struct dqstats dqstats;
Mingming Cao08d03502009-01-14 16:19:32 +0100247EXPORT_SYMBOL(dqstats);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248
Jan Kara0a5a9c72010-02-09 18:20:39 +0100249static qsize_t inode_get_rsv_space(struct inode *inode);
Christoph Hellwig871a2932010-03-03 09:05:07 -0500250static void __dquot_initialize(struct inode *inode, int type);
Jan Kara0a5a9c72010-02-09 18:20:39 +0100251
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252static inline unsigned int
Eric W. Biederman1a06d422012-09-16 05:45:30 -0700253hashfn(const struct super_block *sb, struct kqid qid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254{
Eric W. Biederman1a06d422012-09-16 05:45:30 -0700255 unsigned int id = from_kqid(&init_user_ns, qid);
256 int type = qid.type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 unsigned long tmp;
258
259 tmp = (((unsigned long)sb>>L1_CACHE_SHIFT) ^ id) * (MAXQUOTAS - type);
260 return (tmp + (tmp >> dq_hash_bits)) & dq_hash_mask;
261}
262
263/*
264 * Following list functions expect dq_list_lock to be held
265 */
266static inline void insert_dquot_hash(struct dquot *dquot)
267{
Jan Kara268157b2009-01-27 15:47:22 +0100268 struct hlist_head *head;
Eric W. Biederman1a06d422012-09-16 05:45:30 -0700269 head = dquot_hash + hashfn(dquot->dq_sb, dquot->dq_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 hlist_add_head(&dquot->dq_hash, head);
271}
272
273static inline void remove_dquot_hash(struct dquot *dquot)
274{
275 hlist_del_init(&dquot->dq_hash);
276}
277
Jan Kara7a2435d2009-01-27 01:47:11 +0100278static struct dquot *find_dquot(unsigned int hashent, struct super_block *sb,
Eric W. Biederman1a06d422012-09-16 05:45:30 -0700279 struct kqid qid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280{
281 struct hlist_node *node;
282 struct dquot *dquot;
283
284 hlist_for_each (node, dquot_hash+hashent) {
285 dquot = hlist_entry(node, struct dquot, dq_hash);
Eric W. Biederman4c376dc2012-09-16 03:56:19 -0700286 if (dquot->dq_sb == sb && qid_eq(dquot->dq_id, qid))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 return dquot;
288 }
Jan Karadd6f3c62009-01-26 16:01:43 +0100289 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290}
291
292/* Add a dquot to the tail of the free list */
293static inline void put_dquot_last(struct dquot *dquot)
294{
Akinobu Mita8e130592006-06-26 00:24:37 -0700295 list_add_tail(&dquot->dq_free, &free_dquots);
Dmitry Monakhovdde95882010-04-26 20:03:33 +0400296 dqstats_inc(DQST_FREE_DQUOTS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297}
298
299static inline void remove_free_dquot(struct dquot *dquot)
300{
301 if (list_empty(&dquot->dq_free))
302 return;
303 list_del_init(&dquot->dq_free);
Dmitry Monakhovdde95882010-04-26 20:03:33 +0400304 dqstats_dec(DQST_FREE_DQUOTS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305}
306
307static inline void put_inuse(struct dquot *dquot)
308{
309 /* We add to the back of inuse list so we don't have to restart
310 * when traversing this list and we block */
Akinobu Mita8e130592006-06-26 00:24:37 -0700311 list_add_tail(&dquot->dq_inuse, &inuse_list);
Dmitry Monakhovdde95882010-04-26 20:03:33 +0400312 dqstats_inc(DQST_ALLOC_DQUOTS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313}
314
315static inline void remove_inuse(struct dquot *dquot)
316{
Dmitry Monakhovdde95882010-04-26 20:03:33 +0400317 dqstats_dec(DQST_ALLOC_DQUOTS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 list_del(&dquot->dq_inuse);
319}
320/*
321 * End of list functions needing dq_list_lock
322 */
323
324static void wait_on_dquot(struct dquot *dquot)
325{
Ingo Molnard3be9152006-03-23 03:00:29 -0800326 mutex_lock(&dquot->dq_lock);
327 mutex_unlock(&dquot->dq_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328}
329
Jan Kara03f6e922008-04-28 02:14:32 -0700330static inline int dquot_dirty(struct dquot *dquot)
331{
332 return test_bit(DQ_MOD_B, &dquot->dq_flags);
333}
334
335static inline int mark_dquot_dirty(struct dquot *dquot)
336{
337 return dquot->dq_sb->dq_op->mark_dirty(dquot);
338}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339
Dmitry Monakhoveabf2902010-03-27 15:15:38 +0300340/* Mark dquot dirty in atomic manner, and return it's old dirty flag state */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341int dquot_mark_dquot_dirty(struct dquot *dquot)
342{
Dmitry Monakhoveabf2902010-03-27 15:15:38 +0300343 int ret = 1;
344
345 /* If quota is dirty already, we don't have to acquire dq_list_lock */
346 if (test_bit(DQ_MOD_B, &dquot->dq_flags))
347 return 1;
348
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 spin_lock(&dq_list_lock);
Dmitry Monakhoveabf2902010-03-27 15:15:38 +0300350 if (!test_and_set_bit(DQ_MOD_B, &dquot->dq_flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 list_add(&dquot->dq_dirty, &sb_dqopt(dquot->dq_sb)->
Eric W. Biederman4c376dc2012-09-16 03:56:19 -0700352 info[dquot->dq_id.type].dqi_dirty_list);
Dmitry Monakhoveabf2902010-03-27 15:15:38 +0300353 ret = 0;
354 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 spin_unlock(&dq_list_lock);
Dmitry Monakhoveabf2902010-03-27 15:15:38 +0300356 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357}
Mingming Cao08d03502009-01-14 16:19:32 +0100358EXPORT_SYMBOL(dquot_mark_dquot_dirty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359
Dmitry Monakhovdc52dd32009-12-14 15:21:15 +0300360/* Dirtify all the dquots - this can block when journalling */
361static inline int mark_all_dquot_dirty(struct dquot * const *dquot)
362{
363 int ret, err, cnt;
364
365 ret = err = 0;
366 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
367 if (dquot[cnt])
368 /* Even in case of error we have to continue */
369 ret = mark_dquot_dirty(dquot[cnt]);
370 if (!err)
371 err = ret;
372 }
373 return err;
374}
375
376static inline void dqput_all(struct dquot **dquot)
377{
378 unsigned int cnt;
379
380 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
381 dqput(dquot[cnt]);
382}
383
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384/* This function needs dq_list_lock */
385static inline int clear_dquot_dirty(struct dquot *dquot)
386{
387 if (!test_and_clear_bit(DQ_MOD_B, &dquot->dq_flags))
388 return 0;
389 list_del_init(&dquot->dq_dirty);
390 return 1;
391}
392
393void mark_info_dirty(struct super_block *sb, int type)
394{
395 set_bit(DQF_INFO_DIRTY_B, &sb_dqopt(sb)->info[type].dqi_flags);
396}
397EXPORT_SYMBOL(mark_info_dirty);
398
399/*
400 * Read dquot from disk and alloc space for it
401 */
402
403int dquot_acquire(struct dquot *dquot)
404{
405 int ret = 0, ret2 = 0;
406 struct quota_info *dqopt = sb_dqopt(dquot->dq_sb);
407
Ingo Molnard3be9152006-03-23 03:00:29 -0800408 mutex_lock(&dquot->dq_lock);
409 mutex_lock(&dqopt->dqio_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 if (!test_bit(DQ_READ_B, &dquot->dq_flags))
Eric W. Biederman4c376dc2012-09-16 03:56:19 -0700411 ret = dqopt->ops[dquot->dq_id.type]->read_dqblk(dquot);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 if (ret < 0)
413 goto out_iolock;
414 set_bit(DQ_READ_B, &dquot->dq_flags);
415 /* Instantiate dquot if needed */
416 if (!test_bit(DQ_ACTIVE_B, &dquot->dq_flags) && !dquot->dq_off) {
Eric W. Biederman4c376dc2012-09-16 03:56:19 -0700417 ret = dqopt->ops[dquot->dq_id.type]->commit_dqblk(dquot);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 /* Write the info if needed */
Eric W. Biederman4c376dc2012-09-16 03:56:19 -0700419 if (info_dirty(&dqopt->info[dquot->dq_id.type])) {
420 ret2 = dqopt->ops[dquot->dq_id.type]->write_file_info(
421 dquot->dq_sb, dquot->dq_id.type);
Jan Kara268157b2009-01-27 15:47:22 +0100422 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 if (ret < 0)
424 goto out_iolock;
425 if (ret2 < 0) {
426 ret = ret2;
427 goto out_iolock;
428 }
429 }
430 set_bit(DQ_ACTIVE_B, &dquot->dq_flags);
431out_iolock:
Ingo Molnard3be9152006-03-23 03:00:29 -0800432 mutex_unlock(&dqopt->dqio_mutex);
433 mutex_unlock(&dquot->dq_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 return ret;
435}
Mingming Cao08d03502009-01-14 16:19:32 +0100436EXPORT_SYMBOL(dquot_acquire);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437
438/*
439 * Write dquot to disk
440 */
441int dquot_commit(struct dquot *dquot)
442{
Jan Karab03f2452011-03-31 18:36:52 +0200443 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 struct quota_info *dqopt = sb_dqopt(dquot->dq_sb);
445
Ingo Molnard3be9152006-03-23 03:00:29 -0800446 mutex_lock(&dqopt->dqio_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 spin_lock(&dq_list_lock);
448 if (!clear_dquot_dirty(dquot)) {
449 spin_unlock(&dq_list_lock);
450 goto out_sem;
451 }
452 spin_unlock(&dq_list_lock);
453 /* Inactive dquot can be only if there was error during read/init
454 * => we have better not writing it */
Jan Karab03f2452011-03-31 18:36:52 +0200455 if (test_bit(DQ_ACTIVE_B, &dquot->dq_flags))
Eric W. Biederman4c376dc2012-09-16 03:56:19 -0700456 ret = dqopt->ops[dquot->dq_id.type]->commit_dqblk(dquot);
Jan Karab03f2452011-03-31 18:36:52 +0200457 else
458 ret = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459out_sem:
Ingo Molnard3be9152006-03-23 03:00:29 -0800460 mutex_unlock(&dqopt->dqio_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 return ret;
462}
Mingming Cao08d03502009-01-14 16:19:32 +0100463EXPORT_SYMBOL(dquot_commit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464
465/*
466 * Release dquot
467 */
468int dquot_release(struct dquot *dquot)
469{
470 int ret = 0, ret2 = 0;
471 struct quota_info *dqopt = sb_dqopt(dquot->dq_sb);
472
Ingo Molnard3be9152006-03-23 03:00:29 -0800473 mutex_lock(&dquot->dq_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 /* Check whether we are not racing with some other dqget() */
475 if (atomic_read(&dquot->dq_count) > 1)
476 goto out_dqlock;
Ingo Molnard3be9152006-03-23 03:00:29 -0800477 mutex_lock(&dqopt->dqio_mutex);
Eric W. Biederman4c376dc2012-09-16 03:56:19 -0700478 if (dqopt->ops[dquot->dq_id.type]->release_dqblk) {
479 ret = dqopt->ops[dquot->dq_id.type]->release_dqblk(dquot);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 /* Write the info */
Eric W. Biederman4c376dc2012-09-16 03:56:19 -0700481 if (info_dirty(&dqopt->info[dquot->dq_id.type])) {
482 ret2 = dqopt->ops[dquot->dq_id.type]->write_file_info(
483 dquot->dq_sb, dquot->dq_id.type);
Jan Kara268157b2009-01-27 15:47:22 +0100484 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 if (ret >= 0)
486 ret = ret2;
487 }
488 clear_bit(DQ_ACTIVE_B, &dquot->dq_flags);
Ingo Molnard3be9152006-03-23 03:00:29 -0800489 mutex_unlock(&dqopt->dqio_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490out_dqlock:
Ingo Molnard3be9152006-03-23 03:00:29 -0800491 mutex_unlock(&dquot->dq_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 return ret;
493}
Mingming Cao08d03502009-01-14 16:19:32 +0100494EXPORT_SYMBOL(dquot_release);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495
Jan Kara7d9056b2008-11-25 15:31:32 +0100496void dquot_destroy(struct dquot *dquot)
Jan Kara74f783a2008-08-19 14:51:22 +0200497{
498 kmem_cache_free(dquot_cachep, dquot);
499}
Jan Kara7d9056b2008-11-25 15:31:32 +0100500EXPORT_SYMBOL(dquot_destroy);
Jan Kara74f783a2008-08-19 14:51:22 +0200501
502static inline void do_destroy_dquot(struct dquot *dquot)
503{
504 dquot->dq_sb->dq_op->destroy_dquot(dquot);
505}
506
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507/* Invalidate all dquots on the list. Note that this function is called after
508 * quota is disabled and pointers from inodes removed so there cannot be new
Jan Kara6362e4d2006-03-23 03:00:17 -0800509 * quota users. There can still be some users of quotas due to inodes being
510 * just deleted or pruned by prune_icache() (those are not attached to any
Jan Karacc334122009-01-12 17:23:05 +0100511 * list) or parallel quotactl call. We have to wait for such users.
Jan Kara6362e4d2006-03-23 03:00:17 -0800512 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513static void invalidate_dquots(struct super_block *sb, int type)
514{
Domen Puncerc33ed272005-06-25 14:59:36 -0700515 struct dquot *dquot, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516
Jan Kara6362e4d2006-03-23 03:00:17 -0800517restart:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 spin_lock(&dq_list_lock);
Domen Puncerc33ed272005-06-25 14:59:36 -0700519 list_for_each_entry_safe(dquot, tmp, &inuse_list, dq_inuse) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 if (dquot->dq_sb != sb)
521 continue;
Eric W. Biederman4c376dc2012-09-16 03:56:19 -0700522 if (dquot->dq_id.type != type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 continue;
Jan Kara6362e4d2006-03-23 03:00:17 -0800524 /* Wait for dquot users */
525 if (atomic_read(&dquot->dq_count)) {
526 DEFINE_WAIT(wait);
527
Jan Kara9f985cb2014-04-03 14:46:55 -0700528 dqgrab(dquot);
Jan Kara6362e4d2006-03-23 03:00:17 -0800529 prepare_to_wait(&dquot->dq_wait_unused, &wait,
530 TASK_UNINTERRUPTIBLE);
531 spin_unlock(&dq_list_lock);
532 /* Once dqput() wakes us up, we know it's time to free
533 * the dquot.
534 * IMPORTANT: we rely on the fact that there is always
535 * at most one process waiting for dquot to free.
536 * Otherwise dq_count would be > 1 and we would never
537 * wake up.
538 */
539 if (atomic_read(&dquot->dq_count) > 1)
540 schedule();
541 finish_wait(&dquot->dq_wait_unused, &wait);
542 dqput(dquot);
543 /* At this moment dquot() need not exist (it could be
544 * reclaimed by prune_dqcache(). Hence we must
545 * restart. */
546 goto restart;
547 }
548 /*
549 * Quota now has no users and it has been written on last
550 * dqput()
551 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 remove_dquot_hash(dquot);
553 remove_free_dquot(dquot);
554 remove_inuse(dquot);
Jan Kara74f783a2008-08-19 14:51:22 +0200555 do_destroy_dquot(dquot);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 }
557 spin_unlock(&dq_list_lock);
558}
559
Jan Kara12c77522008-10-20 17:05:00 +0200560/* Call callback for every active dquot on given filesystem */
561int dquot_scan_active(struct super_block *sb,
562 int (*fn)(struct dquot *dquot, unsigned long priv),
563 unsigned long priv)
564{
565 struct dquot *dquot, *old_dquot = NULL;
566 int ret = 0;
567
568 mutex_lock(&sb_dqopt(sb)->dqonoff_mutex);
569 spin_lock(&dq_list_lock);
570 list_for_each_entry(dquot, &inuse_list, dq_inuse) {
571 if (!test_bit(DQ_ACTIVE_B, &dquot->dq_flags))
572 continue;
573 if (dquot->dq_sb != sb)
574 continue;
575 /* Now we have active dquot so we can just increase use count */
576 atomic_inc(&dquot->dq_count);
Jan Kara12c77522008-10-20 17:05:00 +0200577 spin_unlock(&dq_list_lock);
Dmitry Monakhovdde95882010-04-26 20:03:33 +0400578 dqstats_inc(DQST_LOOKUPS);
Jan Kara12c77522008-10-20 17:05:00 +0200579 dqput(old_dquot);
580 old_dquot = dquot;
Jan Kara1362f4e2014-02-20 17:02:27 +0100581 /*
582 * ->release_dquot() can be racing with us. Our reference
583 * protects us from new calls to it so just wait for any
584 * outstanding call and recheck the DQ_ACTIVE_B after that.
585 */
586 wait_on_dquot(dquot);
587 if (test_bit(DQ_ACTIVE_B, &dquot->dq_flags)) {
588 ret = fn(dquot, priv);
589 if (ret < 0)
590 goto out;
591 }
Jan Kara12c77522008-10-20 17:05:00 +0200592 spin_lock(&dq_list_lock);
593 /* We are safe to continue now because our dquot could not
594 * be moved out of the inuse list while we hold the reference */
595 }
596 spin_unlock(&dq_list_lock);
597out:
598 dqput(old_dquot);
599 mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
600 return ret;
601}
Mingming Cao08d03502009-01-14 16:19:32 +0100602EXPORT_SYMBOL(dquot_scan_active);
Jan Kara12c77522008-10-20 17:05:00 +0200603
Jan Karaceed1722012-07-03 16:45:28 +0200604/* Write all dquot structures to quota files */
605int dquot_writeback_dquots(struct super_block *sb, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606{
607 struct list_head *dirty;
608 struct dquot *dquot;
609 struct quota_info *dqopt = sb_dqopt(sb);
610 int cnt;
Jan Karaceed1722012-07-03 16:45:28 +0200611 int err, ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612
Ingo Molnard3be9152006-03-23 03:00:29 -0800613 mutex_lock(&dqopt->dqonoff_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
615 if (type != -1 && cnt != type)
616 continue;
Jan Karaf55abc02008-08-20 17:50:32 +0200617 if (!sb_has_quota_active(sb, cnt))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 continue;
619 spin_lock(&dq_list_lock);
620 dirty = &dqopt->info[cnt].dqi_dirty_list;
621 while (!list_empty(dirty)) {
Jan Kara268157b2009-01-27 15:47:22 +0100622 dquot = list_first_entry(dirty, struct dquot,
623 dq_dirty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 /* Dirty and inactive can be only bad dquot... */
625 if (!test_bit(DQ_ACTIVE_B, &dquot->dq_flags)) {
626 clear_dquot_dirty(dquot);
627 continue;
628 }
629 /* Now we have active dquot from which someone is
630 * holding reference so we can safely just increase
631 * use count */
Jan Kara9f985cb2014-04-03 14:46:55 -0700632 dqgrab(dquot);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 spin_unlock(&dq_list_lock);
Dmitry Monakhovdde95882010-04-26 20:03:33 +0400634 dqstats_inc(DQST_LOOKUPS);
Jan Karaceed1722012-07-03 16:45:28 +0200635 err = sb->dq_op->write_dquot(dquot);
636 if (!ret && err)
Jan Kara474d2602014-10-22 09:06:49 +0200637 ret = err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 dqput(dquot);
639 spin_lock(&dq_list_lock);
640 }
641 spin_unlock(&dq_list_lock);
642 }
643
644 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
Jan Karaf55abc02008-08-20 17:50:32 +0200645 if ((cnt == type || type == -1) && sb_has_quota_active(sb, cnt)
646 && info_dirty(&dqopt->info[cnt]))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 sb->dq_op->write_info(sb, cnt);
Dmitry Monakhovdde95882010-04-26 20:03:33 +0400648 dqstats_inc(DQST_SYNCS);
Ingo Molnard3be9152006-03-23 03:00:29 -0800649 mutex_unlock(&dqopt->dqonoff_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650
Jan Karaceed1722012-07-03 16:45:28 +0200651 return ret;
652}
653EXPORT_SYMBOL(dquot_writeback_dquots);
654
655/* Write all dquot structures to disk and make them visible from userspace */
656int dquot_quota_sync(struct super_block *sb, int type)
657{
658 struct quota_info *dqopt = sb_dqopt(sb);
659 int cnt;
660 int ret;
661
662 ret = dquot_writeback_dquots(sb, type);
663 if (ret)
664 return ret;
665 if (dqopt->flags & DQUOT_QUOTA_SYS_FILE)
Christoph Hellwig5fb324a2010-02-16 03:44:52 -0500666 return 0;
667
668 /* This is not very clever (and fast) but currently I don't know about
669 * any other simple way of getting quota data to disk and we must get
670 * them there for userspace to be visible... */
671 if (sb->s_op->sync_fs)
672 sb->s_op->sync_fs(sb, 1);
673 sync_blockdev(sb->s_bdev);
674
675 /*
676 * Now when everything is written we can discard the pagecache so
677 * that userspace sees the changes.
678 */
Jan Karaf9ef1782012-04-25 21:10:31 +0200679 mutex_lock(&dqopt->dqonoff_mutex);
Christoph Hellwig5fb324a2010-02-16 03:44:52 -0500680 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
681 if (type != -1 && cnt != type)
682 continue;
683 if (!sb_has_quota_active(sb, cnt))
684 continue;
Jan Karaa80b12c2012-04-25 21:15:53 +0200685 mutex_lock(&dqopt->files[cnt]->i_mutex);
Jan Karaf9ef1782012-04-25 21:10:31 +0200686 truncate_inode_pages(&dqopt->files[cnt]->i_data, 0);
687 mutex_unlock(&dqopt->files[cnt]->i_mutex);
Christoph Hellwig5fb324a2010-02-16 03:44:52 -0500688 }
Jan Karaf9ef1782012-04-25 21:10:31 +0200689 mutex_unlock(&dqopt->dqonoff_mutex);
Christoph Hellwig5fb324a2010-02-16 03:44:52 -0500690
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 return 0;
692}
Christoph Hellwig287a8092010-05-19 07:16:45 -0400693EXPORT_SYMBOL(dquot_quota_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694
Dave Chinner1ab6c492013-08-28 10:18:09 +1000695static unsigned long
696dqcache_shrink_scan(struct shrinker *shrink, struct shrink_control *sc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697{
698 struct list_head *head;
699 struct dquot *dquot;
Dave Chinner1ab6c492013-08-28 10:18:09 +1000700 unsigned long freed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701
Niu Yaweid68aab6b2014-06-04 12:22:13 +0800702 spin_lock(&dq_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 head = free_dquots.prev;
Dave Chinner1ab6c492013-08-28 10:18:09 +1000704 while (head != &free_dquots && sc->nr_to_scan) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 dquot = list_entry(head, struct dquot, dq_free);
706 remove_dquot_hash(dquot);
707 remove_free_dquot(dquot);
708 remove_inuse(dquot);
Jan Kara74f783a2008-08-19 14:51:22 +0200709 do_destroy_dquot(dquot);
Dave Chinner1ab6c492013-08-28 10:18:09 +1000710 sc->nr_to_scan--;
711 freed++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 head = free_dquots.prev;
713 }
Niu Yaweid68aab6b2014-06-04 12:22:13 +0800714 spin_unlock(&dq_list_lock);
Dave Chinner1ab6c492013-08-28 10:18:09 +1000715 return freed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716}
717
Dave Chinner1ab6c492013-08-28 10:18:09 +1000718static unsigned long
719dqcache_shrink_count(struct shrinker *shrink, struct shrink_control *sc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720{
Glauber Costa55f841c2013-08-28 10:17:53 +1000721 return vfs_pressure_ratio(
722 percpu_counter_read_positive(&dqstats.counter[DQST_FREE_DQUOTS]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723}
724
Rusty Russell8e1f9362007-07-17 04:03:17 -0700725static struct shrinker dqcache_shrinker = {
Dave Chinner1ab6c492013-08-28 10:18:09 +1000726 .count_objects = dqcache_shrink_count,
727 .scan_objects = dqcache_shrink_scan,
Rusty Russell8e1f9362007-07-17 04:03:17 -0700728 .seeks = DEFAULT_SEEKS,
729};
730
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731/*
732 * Put reference to dquot
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 */
Jan Kara3d9ea252008-10-10 16:12:23 +0200734void dqput(struct dquot *dquot)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735{
Jan Karab48d3802008-07-25 01:46:49 -0700736 int ret;
737
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 if (!dquot)
739 return;
Jan Kara62af9b52010-04-19 16:47:20 +0200740#ifdef CONFIG_QUOTA_DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 if (!atomic_read(&dquot->dq_count)) {
Jiaying Zhangfb5ffb02010-07-20 16:54:43 +0200742 quota_error(dquot->dq_sb, "trying to free free dquot of %s %d",
Eric W. Biederman4c376dc2012-09-16 03:56:19 -0700743 quotatypes[dquot->dq_id.type],
744 from_kqid(&init_user_ns, dquot->dq_id));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 BUG();
746 }
747#endif
Dmitry Monakhovdde95882010-04-26 20:03:33 +0400748 dqstats_inc(DQST_DROPS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749we_slept:
750 spin_lock(&dq_list_lock);
751 if (atomic_read(&dquot->dq_count) > 1) {
752 /* We have more than one user... nothing to do */
753 atomic_dec(&dquot->dq_count);
Jan Kara6362e4d2006-03-23 03:00:17 -0800754 /* Releasing dquot during quotaoff phase? */
Eric W. Biederman4c376dc2012-09-16 03:56:19 -0700755 if (!sb_has_quota_active(dquot->dq_sb, dquot->dq_id.type) &&
Jan Kara6362e4d2006-03-23 03:00:17 -0800756 atomic_read(&dquot->dq_count) == 1)
757 wake_up(&dquot->dq_wait_unused);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 spin_unlock(&dq_list_lock);
759 return;
760 }
761 /* Need to release dquot? */
762 if (test_bit(DQ_ACTIVE_B, &dquot->dq_flags) && dquot_dirty(dquot)) {
763 spin_unlock(&dq_list_lock);
764 /* Commit dquot before releasing */
Jan Karab48d3802008-07-25 01:46:49 -0700765 ret = dquot->dq_sb->dq_op->write_dquot(dquot);
766 if (ret < 0) {
Jiaying Zhangfb5ffb02010-07-20 16:54:43 +0200767 quota_error(dquot->dq_sb, "Can't write quota structure"
768 " (error %d). Quota may get out of sync!",
769 ret);
Jan Karab48d3802008-07-25 01:46:49 -0700770 /*
771 * We clear dirty bit anyway, so that we avoid
772 * infinite loop here
773 */
774 spin_lock(&dq_list_lock);
775 clear_dquot_dirty(dquot);
776 spin_unlock(&dq_list_lock);
777 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 goto we_slept;
779 }
780 /* Clear flag in case dquot was inactive (something bad happened) */
781 clear_dquot_dirty(dquot);
782 if (test_bit(DQ_ACTIVE_B, &dquot->dq_flags)) {
783 spin_unlock(&dq_list_lock);
784 dquot->dq_sb->dq_op->release_dquot(dquot);
785 goto we_slept;
786 }
787 atomic_dec(&dquot->dq_count);
Jan Kara62af9b52010-04-19 16:47:20 +0200788#ifdef CONFIG_QUOTA_DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 /* sanity check */
Eric Sesterhenn8abf6a42006-04-02 13:36:13 +0200790 BUG_ON(!list_empty(&dquot->dq_free));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791#endif
792 put_dquot_last(dquot);
793 spin_unlock(&dq_list_lock);
794}
Mingming Cao08d03502009-01-14 16:19:32 +0100795EXPORT_SYMBOL(dqput);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796
Jan Kara7d9056b2008-11-25 15:31:32 +0100797struct dquot *dquot_alloc(struct super_block *sb, int type)
Jan Kara74f783a2008-08-19 14:51:22 +0200798{
799 return kmem_cache_zalloc(dquot_cachep, GFP_NOFS);
800}
Jan Kara7d9056b2008-11-25 15:31:32 +0100801EXPORT_SYMBOL(dquot_alloc);
Jan Kara74f783a2008-08-19 14:51:22 +0200802
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803static struct dquot *get_empty_dquot(struct super_block *sb, int type)
804{
805 struct dquot *dquot;
806
Jan Kara74f783a2008-08-19 14:51:22 +0200807 dquot = sb->dq_op->alloc_dquot(sb, type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 if(!dquot)
Jan Karadd6f3c62009-01-26 16:01:43 +0100809 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810
Ingo Molnard3be9152006-03-23 03:00:29 -0800811 mutex_init(&dquot->dq_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 INIT_LIST_HEAD(&dquot->dq_free);
813 INIT_LIST_HEAD(&dquot->dq_inuse);
814 INIT_HLIST_NODE(&dquot->dq_hash);
815 INIT_LIST_HEAD(&dquot->dq_dirty);
Jan Kara6362e4d2006-03-23 03:00:17 -0800816 init_waitqueue_head(&dquot->dq_wait_unused);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 dquot->dq_sb = sb;
Eric W. Biederman1a06d422012-09-16 05:45:30 -0700818 dquot->dq_id = make_kqid_invalid(type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 atomic_set(&dquot->dq_count, 1);
820
821 return dquot;
822}
823
824/*
825 * Get reference to dquot
Jan Karacc334122009-01-12 17:23:05 +0100826 *
827 * Locking is slightly tricky here. We are guarded from parallel quotaoff()
828 * destroying our dquot by:
829 * a) checking for quota flags under dq_list_lock and
830 * b) getting a reference to dquot before we release dq_list_lock
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 */
Eric W. Biedermanaca645a2012-09-16 03:11:50 -0700832struct dquot *dqget(struct super_block *sb, struct kqid qid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833{
Eric W. Biederman1a06d422012-09-16 05:45:30 -0700834 unsigned int hashent = hashfn(sb, qid);
Jan Karadd6f3c62009-01-26 16:01:43 +0100835 struct dquot *dquot = NULL, *empty = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836
Eric W. Biederman1a06d422012-09-16 05:45:30 -0700837 if (!sb_has_quota_active(sb, qid.type))
Jan Karadd6f3c62009-01-26 16:01:43 +0100838 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839we_slept:
840 spin_lock(&dq_list_lock);
Jan Karacc334122009-01-12 17:23:05 +0100841 spin_lock(&dq_state_lock);
Eric W. Biederman1a06d422012-09-16 05:45:30 -0700842 if (!sb_has_quota_active(sb, qid.type)) {
Jan Karacc334122009-01-12 17:23:05 +0100843 spin_unlock(&dq_state_lock);
844 spin_unlock(&dq_list_lock);
845 goto out;
846 }
847 spin_unlock(&dq_state_lock);
848
Eric W. Biederman1a06d422012-09-16 05:45:30 -0700849 dquot = find_dquot(hashent, sb, qid);
Jan Karadd6f3c62009-01-26 16:01:43 +0100850 if (!dquot) {
851 if (!empty) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 spin_unlock(&dq_list_lock);
Eric W. Biederman1a06d422012-09-16 05:45:30 -0700853 empty = get_empty_dquot(sb, qid.type);
Jan Karadd6f3c62009-01-26 16:01:43 +0100854 if (!empty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 schedule(); /* Try to wait for a moment... */
856 goto we_slept;
857 }
858 dquot = empty;
Jan Karadd6f3c62009-01-26 16:01:43 +0100859 empty = NULL;
Eric W. Biederman4c376dc2012-09-16 03:56:19 -0700860 dquot->dq_id = qid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 /* all dquots go on the inuse_list */
862 put_inuse(dquot);
863 /* hash it first so it can be found */
864 insert_dquot_hash(dquot);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 spin_unlock(&dq_list_lock);
Dmitry Monakhovdde95882010-04-26 20:03:33 +0400866 dqstats_inc(DQST_LOOKUPS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 } else {
868 if (!atomic_read(&dquot->dq_count))
869 remove_free_dquot(dquot);
870 atomic_inc(&dquot->dq_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 spin_unlock(&dq_list_lock);
Dmitry Monakhovdde95882010-04-26 20:03:33 +0400872 dqstats_inc(DQST_CACHE_HITS);
873 dqstats_inc(DQST_LOOKUPS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 }
Jan Kara268157b2009-01-27 15:47:22 +0100875 /* Wait for dq_lock - after this we know that either dquot_release() is
876 * already finished or it will be canceled due to dq_count > 1 test */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 wait_on_dquot(dquot);
Jan Kara268157b2009-01-27 15:47:22 +0100878 /* Read the dquot / allocate space in quota file */
879 if (!test_bit(DQ_ACTIVE_B, &dquot->dq_flags) &&
880 sb->dq_op->acquire_dquot(dquot) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 dqput(dquot);
Jan Karadd6f3c62009-01-26 16:01:43 +0100882 dquot = NULL;
Jan Karacc334122009-01-12 17:23:05 +0100883 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 }
Jan Kara62af9b52010-04-19 16:47:20 +0200885#ifdef CONFIG_QUOTA_DEBUG
Eric Sesterhenn8abf6a42006-04-02 13:36:13 +0200886 BUG_ON(!dquot->dq_sb); /* Has somebody invalidated entry under us? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887#endif
Jan Karacc334122009-01-12 17:23:05 +0100888out:
889 if (empty)
890 do_destroy_dquot(empty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891
892 return dquot;
893}
Mingming Cao08d03502009-01-14 16:19:32 +0100894EXPORT_SYMBOL(dqget);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895
Jan Kara2d0fa462014-09-25 16:36:14 +0200896static inline struct dquot **i_dquot(struct inode *inode)
897{
Jan Kara2d0fa462014-09-25 16:36:14 +0200898 return inode->i_sb->s_op->get_dquots(inode);
899}
900
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901static int dqinit_needed(struct inode *inode, int type)
902{
903 int cnt;
904
905 if (IS_NOQUOTA(inode))
906 return 0;
907 if (type != -1)
Jan Kara2d0fa462014-09-25 16:36:14 +0200908 return !i_dquot(inode)[type];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
Jan Kara2d0fa462014-09-25 16:36:14 +0200910 if (!i_dquot(inode)[cnt])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 return 1;
912 return 0;
913}
914
Ingo Molnard3be9152006-03-23 03:00:29 -0800915/* This routine is guarded by dqonoff_mutex mutex */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916static void add_dquot_ref(struct super_block *sb, int type)
917{
Jan Kara941d2382008-02-06 01:37:36 -0800918 struct inode *inode, *old_inode = NULL;
Jan Kara62af9b52010-04-19 16:47:20 +0200919#ifdef CONFIG_QUOTA_DEBUG
Jan Kara0a5a9c72010-02-09 18:20:39 +0100920 int reserved = 0;
Jan Kara4c5e6c02010-04-06 18:52:47 +0200921#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922
Dave Chinner55fa6092011-03-22 22:23:40 +1100923 spin_lock(&inode_sb_list_lock);
Christoph Hellwigd003fb72007-02-12 00:51:58 -0800924 list_for_each_entry(inode, &sb->s_inodes, i_sb_list) {
Dave Chinner250df6e2011-03-22 22:23:36 +1100925 spin_lock(&inode->i_lock);
926 if ((inode->i_state & (I_FREEING|I_WILL_FREE|I_NEW)) ||
927 !atomic_read(&inode->i_writecount) ||
928 !dqinit_needed(inode, type)) {
929 spin_unlock(&inode->i_lock);
Nick Pigginaabb8fd2009-03-11 13:17:36 -0700930 continue;
Dave Chinner250df6e2011-03-22 22:23:36 +1100931 }
Christoph Hellwigd003fb72007-02-12 00:51:58 -0800932 __iget(inode);
Dave Chinner250df6e2011-03-22 22:23:36 +1100933 spin_unlock(&inode->i_lock);
Dave Chinner55fa6092011-03-22 22:23:40 +1100934 spin_unlock(&inode_sb_list_lock);
Christoph Hellwigd003fb72007-02-12 00:51:58 -0800935
Jan Karad7e97112012-04-24 17:08:41 +0200936#ifdef CONFIG_QUOTA_DEBUG
937 if (unlikely(inode_get_rsv_space(inode) > 0))
938 reserved = 1;
939#endif
Jan Kara941d2382008-02-06 01:37:36 -0800940 iput(old_inode);
Christoph Hellwig871a2932010-03-03 09:05:07 -0500941 __dquot_initialize(inode, type);
Dave Chinner55fa6092011-03-22 22:23:40 +1100942
943 /*
944 * We hold a reference to 'inode' so it couldn't have been
945 * removed from s_inodes list while we dropped the
946 * inode_sb_list_lock We cannot iput the inode now as we can be
947 * holding the last reference and we cannot iput it under
948 * inode_sb_list_lock. So we keep the reference and iput it
949 * later.
950 */
Jan Kara941d2382008-02-06 01:37:36 -0800951 old_inode = inode;
Dave Chinner55fa6092011-03-22 22:23:40 +1100952 spin_lock(&inode_sb_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 }
Dave Chinner55fa6092011-03-22 22:23:40 +1100954 spin_unlock(&inode_sb_list_lock);
Jan Kara941d2382008-02-06 01:37:36 -0800955 iput(old_inode);
Jan Kara0a5a9c72010-02-09 18:20:39 +0100956
Jan Kara62af9b52010-04-19 16:47:20 +0200957#ifdef CONFIG_QUOTA_DEBUG
Jan Kara0a5a9c72010-02-09 18:20:39 +0100958 if (reserved) {
Jiaying Zhangfb5ffb02010-07-20 16:54:43 +0200959 quota_error(sb, "Writes happened before quota was turned on "
960 "thus quota information is probably inconsistent. "
961 "Please run quotacheck(8)");
Jan Kara0a5a9c72010-02-09 18:20:39 +0100962 }
Jan Kara4c5e6c02010-04-06 18:52:47 +0200963#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964}
965
Jan Kara268157b2009-01-27 15:47:22 +0100966/*
Jan Kara268157b2009-01-27 15:47:22 +0100967 * Remove references to dquots from inode and add dquot to list for freeing
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300968 * if we have the last reference to dquot
Jan Kara268157b2009-01-27 15:47:22 +0100969 */
Niu Yawei9eb64632014-06-04 12:21:30 +0800970static void remove_inode_dquot_ref(struct inode *inode, int type,
971 struct list_head *tofree_head)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972{
Jan Kara2d0fa462014-09-25 16:36:14 +0200973 struct dquot *dquot = i_dquot(inode)[type];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974
Jan Kara2d0fa462014-09-25 16:36:14 +0200975 i_dquot(inode)[type] = NULL;
Niu Yawei9eb64632014-06-04 12:21:30 +0800976 if (!dquot)
977 return;
978
979 if (list_empty(&dquot->dq_free)) {
980 /*
981 * The inode still has reference to dquot so it can't be in the
982 * free list
983 */
984 spin_lock(&dq_list_lock);
985 list_add(&dquot->dq_free, tofree_head);
986 spin_unlock(&dq_list_lock);
987 } else {
988 /*
989 * Dquot is already in a list to put so we won't drop the last
990 * reference here.
991 */
992 dqput(dquot);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994}
995
Jan Kara268157b2009-01-27 15:47:22 +0100996/*
997 * Free list of dquots
998 * Dquots are removed from inodes and no new references can be got so we are
999 * the only ones holding reference
1000 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001static void put_dquot_list(struct list_head *tofree_head)
1002{
1003 struct list_head *act_head;
1004 struct dquot *dquot;
1005
1006 act_head = tofree_head->next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 while (act_head != tofree_head) {
1008 dquot = list_entry(act_head, struct dquot, dq_free);
1009 act_head = act_head->next;
Jan Kara268157b2009-01-27 15:47:22 +01001010 /* Remove dquot from the list so we won't have problems... */
1011 list_del_init(&dquot->dq_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 dqput(dquot);
1013 }
1014}
1015
Christoph Hellwigfb58b732007-02-12 00:51:57 -08001016static void remove_dquot_ref(struct super_block *sb, int type,
1017 struct list_head *tofree_head)
1018{
1019 struct inode *inode;
Dmitry Monakhov7af9cce2010-06-01 11:39:48 +04001020 int reserved = 0;
Christoph Hellwigfb58b732007-02-12 00:51:57 -08001021
Dave Chinner55fa6092011-03-22 22:23:40 +11001022 spin_lock(&inode_sb_list_lock);
Christoph Hellwigfb58b732007-02-12 00:51:57 -08001023 list_for_each_entry(inode, &sb->s_inodes, i_sb_list) {
Nick Pigginaabb8fd2009-03-11 13:17:36 -07001024 /*
1025 * We have to scan also I_NEW inodes because they can already
1026 * have quota pointer initialized. Luckily, we need to touch
1027 * only quota pointers and these have separate locking
Niu Yaweib9ba6f92014-06-04 12:23:19 +08001028 * (dq_data_lock).
Nick Pigginaabb8fd2009-03-11 13:17:36 -07001029 */
Niu Yaweib9ba6f92014-06-04 12:23:19 +08001030 spin_lock(&dq_data_lock);
Dmitry Monakhov7af9cce2010-06-01 11:39:48 +04001031 if (!IS_NOQUOTA(inode)) {
1032 if (unlikely(inode_get_rsv_space(inode) > 0))
1033 reserved = 1;
Christoph Hellwigfb58b732007-02-12 00:51:57 -08001034 remove_inode_dquot_ref(inode, type, tofree_head);
Dmitry Monakhov7af9cce2010-06-01 11:39:48 +04001035 }
Niu Yaweib9ba6f92014-06-04 12:23:19 +08001036 spin_unlock(&dq_data_lock);
Christoph Hellwigfb58b732007-02-12 00:51:57 -08001037 }
Dave Chinner55fa6092011-03-22 22:23:40 +11001038 spin_unlock(&inode_sb_list_lock);
Dmitry Monakhov7af9cce2010-06-01 11:39:48 +04001039#ifdef CONFIG_QUOTA_DEBUG
1040 if (reserved) {
1041 printk(KERN_WARNING "VFS (%s): Writes happened after quota"
1042 " was disabled thus quota information is probably "
1043 "inconsistent. Please run quotacheck(8).\n", sb->s_id);
1044 }
1045#endif
Christoph Hellwigfb58b732007-02-12 00:51:57 -08001046}
1047
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048/* Gather all references from inodes and drop them */
1049static void drop_dquot_ref(struct super_block *sb, int type)
1050{
1051 LIST_HEAD(tofree_head);
1052
Christoph Hellwigfb58b732007-02-12 00:51:57 -08001053 if (sb->dq_op) {
Christoph Hellwigfb58b732007-02-12 00:51:57 -08001054 remove_dquot_ref(sb, type, &tofree_head);
Niu Yaweib9ba6f92014-06-04 12:23:19 +08001055 synchronize_srcu(&dquot_srcu);
Christoph Hellwigfb58b732007-02-12 00:51:57 -08001056 put_dquot_list(&tofree_head);
1057 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058}
1059
Jan Kara12095462008-08-20 14:45:12 +02001060static inline void dquot_incr_inodes(struct dquot *dquot, qsize_t number)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061{
1062 dquot->dq_dqb.dqb_curinodes += number;
1063}
1064
1065static inline void dquot_incr_space(struct dquot *dquot, qsize_t number)
1066{
1067 dquot->dq_dqb.dqb_curspace += number;
1068}
1069
Mingming Caof18df222009-01-13 16:43:09 +01001070static inline void dquot_resv_space(struct dquot *dquot, qsize_t number)
1071{
1072 dquot->dq_dqb.dqb_rsvspace += number;
1073}
1074
Mingming Cao740d9dc2009-01-13 16:43:14 +01001075/*
1076 * Claim reserved quota space
1077 */
Jan Kara0a5a9c72010-02-09 18:20:39 +01001078static void dquot_claim_reserved_space(struct dquot *dquot, qsize_t number)
Mingming Cao740d9dc2009-01-13 16:43:14 +01001079{
Jan Kara0a5a9c72010-02-09 18:20:39 +01001080 if (dquot->dq_dqb.dqb_rsvspace < number) {
1081 WARN_ON_ONCE(1);
1082 number = dquot->dq_dqb.dqb_rsvspace;
1083 }
Mingming Cao740d9dc2009-01-13 16:43:14 +01001084 dquot->dq_dqb.dqb_curspace += number;
1085 dquot->dq_dqb.dqb_rsvspace -= number;
1086}
1087
Jan Kara1c8924e2013-08-17 09:32:32 -04001088static void dquot_reclaim_reserved_space(struct dquot *dquot, qsize_t number)
1089{
1090 if (WARN_ON_ONCE(dquot->dq_dqb.dqb_curspace < number))
1091 number = dquot->dq_dqb.dqb_curspace;
1092 dquot->dq_dqb.dqb_rsvspace += number;
1093 dquot->dq_dqb.dqb_curspace -= number;
1094}
1095
Mingming Cao740d9dc2009-01-13 16:43:14 +01001096static inline
1097void dquot_free_reserved_space(struct dquot *dquot, qsize_t number)
1098{
Jan Kara0a5a9c72010-02-09 18:20:39 +01001099 if (dquot->dq_dqb.dqb_rsvspace >= number)
1100 dquot->dq_dqb.dqb_rsvspace -= number;
1101 else {
1102 WARN_ON_ONCE(1);
1103 dquot->dq_dqb.dqb_rsvspace = 0;
1104 }
Mingming Cao740d9dc2009-01-13 16:43:14 +01001105}
1106
Jan Kara7a2435d2009-01-27 01:47:11 +01001107static void dquot_decr_inodes(struct dquot *dquot, qsize_t number)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108{
Jan Karadb49d2d2008-10-01 18:21:39 +02001109 if (sb_dqopt(dquot->dq_sb)->flags & DQUOT_NEGATIVE_USAGE ||
1110 dquot->dq_dqb.dqb_curinodes >= number)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 dquot->dq_dqb.dqb_curinodes -= number;
1112 else
1113 dquot->dq_dqb.dqb_curinodes = 0;
1114 if (dquot->dq_dqb.dqb_curinodes <= dquot->dq_dqb.dqb_isoftlimit)
1115 dquot->dq_dqb.dqb_itime = (time_t) 0;
1116 clear_bit(DQ_INODES_B, &dquot->dq_flags);
1117}
1118
Jan Kara7a2435d2009-01-27 01:47:11 +01001119static void dquot_decr_space(struct dquot *dquot, qsize_t number)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120{
Jan Karadb49d2d2008-10-01 18:21:39 +02001121 if (sb_dqopt(dquot->dq_sb)->flags & DQUOT_NEGATIVE_USAGE ||
1122 dquot->dq_dqb.dqb_curspace >= number)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 dquot->dq_dqb.dqb_curspace -= number;
1124 else
1125 dquot->dq_dqb.dqb_curspace = 0;
Jan Kara12095462008-08-20 14:45:12 +02001126 if (dquot->dq_dqb.dqb_curspace <= dquot->dq_dqb.dqb_bsoftlimit)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 dquot->dq_dqb.dqb_btime = (time_t) 0;
1128 clear_bit(DQ_BLKS_B, &dquot->dq_flags);
1129}
1130
Jan Karabf097aa2012-02-14 13:28:01 +01001131struct dquot_warn {
1132 struct super_block *w_sb;
Eric W. Biederman7b9c7322012-09-16 04:05:34 -07001133 struct kqid w_dq_id;
Jan Karabf097aa2012-02-14 13:28:01 +01001134 short w_type;
1135};
1136
Jan Karac5254602007-12-22 14:03:25 -08001137static int warning_issued(struct dquot *dquot, const int warntype)
1138{
1139 int flag = (warntype == QUOTA_NL_BHARDWARN ||
1140 warntype == QUOTA_NL_BSOFTLONGWARN) ? DQ_BLKS_B :
1141 ((warntype == QUOTA_NL_IHARDWARN ||
1142 warntype == QUOTA_NL_ISOFTLONGWARN) ? DQ_INODES_B : 0);
1143
1144 if (!flag)
1145 return 0;
1146 return test_and_set_bit(flag, &dquot->dq_flags);
1147}
1148
Jan Kara8e893462007-10-16 23:29:31 -07001149#ifdef CONFIG_PRINT_QUOTA_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150static int flag_print_warnings = 1;
1151
Jan Karabf097aa2012-02-14 13:28:01 +01001152static int need_print_warning(struct dquot_warn *warn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153{
1154 if (!flag_print_warnings)
1155 return 0;
1156
Eric W. Biederman7b9c7322012-09-16 04:05:34 -07001157 switch (warn->w_dq_id.type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 case USRQUOTA:
Eric W. Biederman1a06d422012-09-16 05:45:30 -07001159 return uid_eq(current_fsuid(), warn->w_dq_id.uid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 case GRPQUOTA:
Eric W. Biederman7b9c7322012-09-16 04:05:34 -07001161 return in_group_p(warn->w_dq_id.gid);
Jan Kara6c29c502012-10-09 23:30:17 +02001162 case PRJQUOTA: /* Never taken... Just make gcc happy */
1163 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 }
1165 return 0;
1166}
1167
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168/* Print warning to user which exceeded quota */
Jan Karabf097aa2012-02-14 13:28:01 +01001169static void print_warning(struct dquot_warn *warn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170{
1171 char *msg = NULL;
Peter Zijlstra24ec8392006-12-08 02:36:04 -08001172 struct tty_struct *tty;
Jan Karabf097aa2012-02-14 13:28:01 +01001173 int warntype = warn->w_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174
Jan Kara657d3bf2008-07-25 01:46:52 -07001175 if (warntype == QUOTA_NL_IHARDBELOW ||
1176 warntype == QUOTA_NL_ISOFTBELOW ||
1177 warntype == QUOTA_NL_BHARDBELOW ||
Jan Karabf097aa2012-02-14 13:28:01 +01001178 warntype == QUOTA_NL_BSOFTBELOW || !need_print_warning(warn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 return;
1180
Peter Zijlstra24ec8392006-12-08 02:36:04 -08001181 tty = get_current_tty();
1182 if (!tty)
Alan Cox452a00d2008-10-13 10:39:13 +01001183 return;
Jan Karabf097aa2012-02-14 13:28:01 +01001184 tty_write_message(tty, warn->w_sb->s_id);
Jan Kara8e893462007-10-16 23:29:31 -07001185 if (warntype == QUOTA_NL_ISOFTWARN || warntype == QUOTA_NL_BSOFTWARN)
Peter Zijlstra24ec8392006-12-08 02:36:04 -08001186 tty_write_message(tty, ": warning, ");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 else
Peter Zijlstra24ec8392006-12-08 02:36:04 -08001188 tty_write_message(tty, ": write failed, ");
Eric W. Biederman7b9c7322012-09-16 04:05:34 -07001189 tty_write_message(tty, quotatypes[warn->w_dq_id.type]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 switch (warntype) {
Jan Kara8e893462007-10-16 23:29:31 -07001191 case QUOTA_NL_IHARDWARN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 msg = " file limit reached.\r\n";
1193 break;
Jan Kara8e893462007-10-16 23:29:31 -07001194 case QUOTA_NL_ISOFTLONGWARN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 msg = " file quota exceeded too long.\r\n";
1196 break;
Jan Kara8e893462007-10-16 23:29:31 -07001197 case QUOTA_NL_ISOFTWARN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 msg = " file quota exceeded.\r\n";
1199 break;
Jan Kara8e893462007-10-16 23:29:31 -07001200 case QUOTA_NL_BHARDWARN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 msg = " block limit reached.\r\n";
1202 break;
Jan Kara8e893462007-10-16 23:29:31 -07001203 case QUOTA_NL_BSOFTLONGWARN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 msg = " block quota exceeded too long.\r\n";
1205 break;
Jan Kara8e893462007-10-16 23:29:31 -07001206 case QUOTA_NL_BSOFTWARN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 msg = " block quota exceeded.\r\n";
1208 break;
1209 }
Peter Zijlstra24ec8392006-12-08 02:36:04 -08001210 tty_write_message(tty, msg);
Alan Cox452a00d2008-10-13 10:39:13 +01001211 tty_kref_put(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212}
Jan Kara8e893462007-10-16 23:29:31 -07001213#endif
1214
Jan Karabf097aa2012-02-14 13:28:01 +01001215static void prepare_warning(struct dquot_warn *warn, struct dquot *dquot,
1216 int warntype)
1217{
1218 if (warning_issued(dquot, warntype))
1219 return;
1220 warn->w_type = warntype;
1221 warn->w_sb = dquot->dq_sb;
1222 warn->w_dq_id = dquot->dq_id;
Jan Karabf097aa2012-02-14 13:28:01 +01001223}
1224
Mingming Caof18df222009-01-13 16:43:09 +01001225/*
1226 * Write warnings to the console and send warning messages over netlink.
1227 *
Jan Karabf097aa2012-02-14 13:28:01 +01001228 * Note that this function can call into tty and networking code.
Mingming Caof18df222009-01-13 16:43:09 +01001229 */
Jan Karabf097aa2012-02-14 13:28:01 +01001230static void flush_warnings(struct dquot_warn *warn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231{
1232 int i;
1233
Steven Whitehouse86e931a2009-09-28 12:35:17 +01001234 for (i = 0; i < MAXQUOTAS; i++) {
Jan Karabf097aa2012-02-14 13:28:01 +01001235 if (warn[i].w_type == QUOTA_NL_NOWARN)
1236 continue;
Jan Kara8e893462007-10-16 23:29:31 -07001237#ifdef CONFIG_PRINT_QUOTA_WARNING
Jan Karabf097aa2012-02-14 13:28:01 +01001238 print_warning(&warn[i]);
Jan Kara8e893462007-10-16 23:29:31 -07001239#endif
Eric W. Biederman7b9c7322012-09-16 04:05:34 -07001240 quota_send_warning(warn[i].w_dq_id,
Jan Karabf097aa2012-02-14 13:28:01 +01001241 warn[i].w_sb->s_dev, warn[i].w_type);
Steven Whitehouse86e931a2009-09-28 12:35:17 +01001242 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243}
1244
Jan Kara7a2435d2009-01-27 01:47:11 +01001245static int ignore_hardlimit(struct dquot *dquot)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246{
Eric W. Biederman4c376dc2012-09-16 03:56:19 -07001247 struct mem_dqinfo *info = &sb_dqopt(dquot->dq_sb)->info[dquot->dq_id.type];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248
1249 return capable(CAP_SYS_RESOURCE) &&
Jan Kara268157b2009-01-27 15:47:22 +01001250 (info->dqi_format->qf_fmt_id != QFMT_VFS_OLD ||
Jan Kara9c451012014-11-19 09:21:58 +01001251 !(info->dqi_flags & DQF_ROOT_SQUASH));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252}
1253
1254/* needs dq_data_lock */
Jan Karabf097aa2012-02-14 13:28:01 +01001255static int check_idq(struct dquot *dquot, qsize_t inodes,
1256 struct dquot_warn *warn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257{
Jan Kara268157b2009-01-27 15:47:22 +01001258 qsize_t newinodes = dquot->dq_dqb.dqb_curinodes + inodes;
1259
Eric W. Biederman4c376dc2012-09-16 03:56:19 -07001260 if (!sb_has_quota_limits_enabled(dquot->dq_sb, dquot->dq_id.type) ||
Jan Karaf55abc02008-08-20 17:50:32 +02001261 test_bit(DQ_FAKE_B, &dquot->dq_flags))
Christoph Hellwigefd8f0e2010-03-03 09:05:08 -05001262 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263
1264 if (dquot->dq_dqb.dqb_ihardlimit &&
Jan Kara268157b2009-01-27 15:47:22 +01001265 newinodes > dquot->dq_dqb.dqb_ihardlimit &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 !ignore_hardlimit(dquot)) {
Jan Karabf097aa2012-02-14 13:28:01 +01001267 prepare_warning(warn, dquot, QUOTA_NL_IHARDWARN);
Christoph Hellwigefd8f0e2010-03-03 09:05:08 -05001268 return -EDQUOT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 }
1270
1271 if (dquot->dq_dqb.dqb_isoftlimit &&
Jan Kara268157b2009-01-27 15:47:22 +01001272 newinodes > dquot->dq_dqb.dqb_isoftlimit &&
1273 dquot->dq_dqb.dqb_itime &&
1274 get_seconds() >= dquot->dq_dqb.dqb_itime &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 !ignore_hardlimit(dquot)) {
Jan Karabf097aa2012-02-14 13:28:01 +01001276 prepare_warning(warn, dquot, QUOTA_NL_ISOFTLONGWARN);
Christoph Hellwigefd8f0e2010-03-03 09:05:08 -05001277 return -EDQUOT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 }
1279
1280 if (dquot->dq_dqb.dqb_isoftlimit &&
Jan Kara268157b2009-01-27 15:47:22 +01001281 newinodes > dquot->dq_dqb.dqb_isoftlimit &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 dquot->dq_dqb.dqb_itime == 0) {
Jan Karabf097aa2012-02-14 13:28:01 +01001283 prepare_warning(warn, dquot, QUOTA_NL_ISOFTWARN);
Jan Kara268157b2009-01-27 15:47:22 +01001284 dquot->dq_dqb.dqb_itime = get_seconds() +
Eric W. Biederman4c376dc2012-09-16 03:56:19 -07001285 sb_dqopt(dquot->dq_sb)->info[dquot->dq_id.type].dqi_igrace;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 }
1287
Christoph Hellwigefd8f0e2010-03-03 09:05:08 -05001288 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289}
1290
1291/* needs dq_data_lock */
Jan Karabf097aa2012-02-14 13:28:01 +01001292static int check_bdq(struct dquot *dquot, qsize_t space, int prealloc,
1293 struct dquot_warn *warn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294{
Mingming Caof18df222009-01-13 16:43:09 +01001295 qsize_t tspace;
Jan Kara268157b2009-01-27 15:47:22 +01001296 struct super_block *sb = dquot->dq_sb;
Mingming Caof18df222009-01-13 16:43:09 +01001297
Eric W. Biederman4c376dc2012-09-16 03:56:19 -07001298 if (!sb_has_quota_limits_enabled(sb, dquot->dq_id.type) ||
Jan Karaf55abc02008-08-20 17:50:32 +02001299 test_bit(DQ_FAKE_B, &dquot->dq_flags))
Christoph Hellwigefd8f0e2010-03-03 09:05:08 -05001300 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301
Mingming Caof18df222009-01-13 16:43:09 +01001302 tspace = dquot->dq_dqb.dqb_curspace + dquot->dq_dqb.dqb_rsvspace
1303 + space;
1304
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 if (dquot->dq_dqb.dqb_bhardlimit &&
Mingming Caof18df222009-01-13 16:43:09 +01001306 tspace > dquot->dq_dqb.dqb_bhardlimit &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 !ignore_hardlimit(dquot)) {
1308 if (!prealloc)
Jan Karabf097aa2012-02-14 13:28:01 +01001309 prepare_warning(warn, dquot, QUOTA_NL_BHARDWARN);
Christoph Hellwigefd8f0e2010-03-03 09:05:08 -05001310 return -EDQUOT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 }
1312
1313 if (dquot->dq_dqb.dqb_bsoftlimit &&
Mingming Caof18df222009-01-13 16:43:09 +01001314 tspace > dquot->dq_dqb.dqb_bsoftlimit &&
Jan Kara268157b2009-01-27 15:47:22 +01001315 dquot->dq_dqb.dqb_btime &&
1316 get_seconds() >= dquot->dq_dqb.dqb_btime &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317 !ignore_hardlimit(dquot)) {
1318 if (!prealloc)
Jan Karabf097aa2012-02-14 13:28:01 +01001319 prepare_warning(warn, dquot, QUOTA_NL_BSOFTLONGWARN);
Christoph Hellwigefd8f0e2010-03-03 09:05:08 -05001320 return -EDQUOT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 }
1322
1323 if (dquot->dq_dqb.dqb_bsoftlimit &&
Mingming Caof18df222009-01-13 16:43:09 +01001324 tspace > dquot->dq_dqb.dqb_bsoftlimit &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325 dquot->dq_dqb.dqb_btime == 0) {
1326 if (!prealloc) {
Jan Karabf097aa2012-02-14 13:28:01 +01001327 prepare_warning(warn, dquot, QUOTA_NL_BSOFTWARN);
Jan Kara268157b2009-01-27 15:47:22 +01001328 dquot->dq_dqb.dqb_btime = get_seconds() +
Eric W. Biederman4c376dc2012-09-16 03:56:19 -07001329 sb_dqopt(sb)->info[dquot->dq_id.type].dqi_bgrace;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 }
1331 else
1332 /*
1333 * We don't allow preallocation to exceed softlimit so exceeding will
1334 * be always printed
1335 */
Christoph Hellwigefd8f0e2010-03-03 09:05:08 -05001336 return -EDQUOT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 }
1338
Christoph Hellwigefd8f0e2010-03-03 09:05:08 -05001339 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340}
1341
Jan Kara12095462008-08-20 14:45:12 +02001342static int info_idq_free(struct dquot *dquot, qsize_t inodes)
Jan Kara657d3bf2008-07-25 01:46:52 -07001343{
Jan Kara268157b2009-01-27 15:47:22 +01001344 qsize_t newinodes;
1345
Jan Kara657d3bf2008-07-25 01:46:52 -07001346 if (test_bit(DQ_FAKE_B, &dquot->dq_flags) ||
Jan Karaf55abc02008-08-20 17:50:32 +02001347 dquot->dq_dqb.dqb_curinodes <= dquot->dq_dqb.dqb_isoftlimit ||
Eric W. Biederman4c376dc2012-09-16 03:56:19 -07001348 !sb_has_quota_limits_enabled(dquot->dq_sb, dquot->dq_id.type))
Jan Kara657d3bf2008-07-25 01:46:52 -07001349 return QUOTA_NL_NOWARN;
1350
Jan Kara268157b2009-01-27 15:47:22 +01001351 newinodes = dquot->dq_dqb.dqb_curinodes - inodes;
1352 if (newinodes <= dquot->dq_dqb.dqb_isoftlimit)
Jan Kara657d3bf2008-07-25 01:46:52 -07001353 return QUOTA_NL_ISOFTBELOW;
1354 if (dquot->dq_dqb.dqb_curinodes >= dquot->dq_dqb.dqb_ihardlimit &&
Jan Kara268157b2009-01-27 15:47:22 +01001355 newinodes < dquot->dq_dqb.dqb_ihardlimit)
Jan Kara657d3bf2008-07-25 01:46:52 -07001356 return QUOTA_NL_IHARDBELOW;
1357 return QUOTA_NL_NOWARN;
1358}
1359
1360static int info_bdq_free(struct dquot *dquot, qsize_t space)
1361{
1362 if (test_bit(DQ_FAKE_B, &dquot->dq_flags) ||
Jan Kara12095462008-08-20 14:45:12 +02001363 dquot->dq_dqb.dqb_curspace <= dquot->dq_dqb.dqb_bsoftlimit)
Jan Kara657d3bf2008-07-25 01:46:52 -07001364 return QUOTA_NL_NOWARN;
1365
Jan Kara12095462008-08-20 14:45:12 +02001366 if (dquot->dq_dqb.dqb_curspace - space <= dquot->dq_dqb.dqb_bsoftlimit)
Jan Kara657d3bf2008-07-25 01:46:52 -07001367 return QUOTA_NL_BSOFTBELOW;
Jan Kara12095462008-08-20 14:45:12 +02001368 if (dquot->dq_dqb.dqb_curspace >= dquot->dq_dqb.dqb_bhardlimit &&
1369 dquot->dq_dqb.dqb_curspace - space < dquot->dq_dqb.dqb_bhardlimit)
Jan Kara657d3bf2008-07-25 01:46:52 -07001370 return QUOTA_NL_BHARDBELOW;
1371 return QUOTA_NL_NOWARN;
1372}
Jan Kara0a5a9c72010-02-09 18:20:39 +01001373
Christoph Hellwig189eef52010-06-04 10:56:29 +02001374static int dquot_active(const struct inode *inode)
1375{
1376 struct super_block *sb = inode->i_sb;
1377
1378 if (IS_NOQUOTA(inode))
1379 return 0;
1380 return sb_any_quota_loaded(sb) & ~sb_any_quota_suspended(sb);
1381}
1382
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383/*
Christoph Hellwig871a2932010-03-03 09:05:07 -05001384 * Initialize quota pointers in inode
1385 *
Christoph Hellwig871a2932010-03-03 09:05:07 -05001386 * It is better to call this function outside of any transaction as it
1387 * might need a lot of space in journal for dquot structure allocation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 */
Christoph Hellwig871a2932010-03-03 09:05:07 -05001389static void __dquot_initialize(struct inode *inode, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390{
Niu Yawei1ea06be2014-06-04 12:20:30 +08001391 int cnt, init_needed = 0;
Dmitry Monakhovad1e6e82010-02-16 08:31:49 +03001392 struct dquot *got[MAXQUOTAS];
Jan Karacc334122009-01-12 17:23:05 +01001393 struct super_block *sb = inode->i_sb;
Jan Kara0a5a9c72010-02-09 18:20:39 +01001394 qsize_t rsv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395
Christoph Hellwig189eef52010-06-04 10:56:29 +02001396 if (!dquot_active(inode))
Christoph Hellwig871a2932010-03-03 09:05:07 -05001397 return;
Jan Karacc334122009-01-12 17:23:05 +01001398
1399 /* First get references to structures we might need. */
1400 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
Eric W. Biedermanaca645a2012-09-16 03:11:50 -07001401 struct kqid qid;
Dmitry Monakhovad1e6e82010-02-16 08:31:49 +03001402 got[cnt] = NULL;
Jan Karacc334122009-01-12 17:23:05 +01001403 if (type != -1 && cnt != type)
1404 continue;
Niu Yawei1ea06be2014-06-04 12:20:30 +08001405 /*
1406 * The i_dquot should have been initialized in most cases,
1407 * we check it without locking here to avoid unnecessary
1408 * dqget()/dqput() calls.
1409 */
Jan Kara2d0fa462014-09-25 16:36:14 +02001410 if (i_dquot(inode)[cnt])
Niu Yawei1ea06be2014-06-04 12:20:30 +08001411 continue;
1412 init_needed = 1;
1413
Jan Karacc334122009-01-12 17:23:05 +01001414 switch (cnt) {
1415 case USRQUOTA:
Eric W. Biedermanaca645a2012-09-16 03:11:50 -07001416 qid = make_kqid_uid(inode->i_uid);
Jan Karacc334122009-01-12 17:23:05 +01001417 break;
1418 case GRPQUOTA:
Eric W. Biedermanaca645a2012-09-16 03:11:50 -07001419 qid = make_kqid_gid(inode->i_gid);
Jan Karacc334122009-01-12 17:23:05 +01001420 break;
1421 }
Eric W. Biedermanaca645a2012-09-16 03:11:50 -07001422 got[cnt] = dqget(sb, qid);
Jan Karacc334122009-01-12 17:23:05 +01001423 }
1424
Niu Yawei1ea06be2014-06-04 12:20:30 +08001425 /* All required i_dquot has been initialized */
1426 if (!init_needed)
1427 return;
1428
Niu Yaweib9ba6f92014-06-04 12:23:19 +08001429 spin_lock(&dq_data_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 if (IS_NOQUOTA(inode))
1431 goto out_err;
1432 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1433 if (type != -1 && cnt != type)
1434 continue;
Jan Karacc334122009-01-12 17:23:05 +01001435 /* Avoid races with quotaoff() */
1436 if (!sb_has_quota_active(sb, cnt))
1437 continue;
Jan Kara4408ea42010-10-19 00:24:21 +02001438 /* We could race with quotaon or dqget() could have failed */
1439 if (!got[cnt])
1440 continue;
Jan Kara2d0fa462014-09-25 16:36:14 +02001441 if (!i_dquot(inode)[cnt]) {
1442 i_dquot(inode)[cnt] = got[cnt];
Jan Karadd6f3c62009-01-26 16:01:43 +01001443 got[cnt] = NULL;
Jan Kara0a5a9c72010-02-09 18:20:39 +01001444 /*
1445 * Make quota reservation system happy if someone
1446 * did a write before quota was turned on
1447 */
1448 rsv = inode_get_rsv_space(inode);
Niu Yaweib9ba6f92014-06-04 12:23:19 +08001449 if (unlikely(rsv))
Jan Kara2d0fa462014-09-25 16:36:14 +02001450 dquot_resv_space(i_dquot(inode)[cnt], rsv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451 }
1452 }
1453out_err:
Niu Yaweib9ba6f92014-06-04 12:23:19 +08001454 spin_unlock(&dq_data_lock);
Jan Karacc334122009-01-12 17:23:05 +01001455 /* Drop unused references */
Dmitry Monakhovdc52dd32009-12-14 15:21:15 +03001456 dqput_all(got);
Christoph Hellwig871a2932010-03-03 09:05:07 -05001457}
1458
1459void dquot_initialize(struct inode *inode)
1460{
1461 __dquot_initialize(inode, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462}
Mingming Cao08d03502009-01-14 16:19:32 +01001463EXPORT_SYMBOL(dquot_initialize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464
1465/*
Niu Yaweib9ba6f92014-06-04 12:23:19 +08001466 * Release all quotas referenced by inode.
1467 *
1468 * This function only be called on inode free or converting
1469 * a file to quota file, no other users for the i_dquot in
1470 * both cases, so we needn't call synchronize_srcu() after
1471 * clearing i_dquot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 */
Christoph Hellwig9f754752010-03-03 09:05:05 -05001473static void __dquot_drop(struct inode *inode)
Jan Kara3d9ea252008-10-10 16:12:23 +02001474{
Jan Karacc334122009-01-12 17:23:05 +01001475 int cnt;
1476 struct dquot *put[MAXQUOTAS];
1477
Niu Yaweib9ba6f92014-06-04 12:23:19 +08001478 spin_lock(&dq_data_lock);
Jan Karacc334122009-01-12 17:23:05 +01001479 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
Jan Kara2d0fa462014-09-25 16:36:14 +02001480 put[cnt] = i_dquot(inode)[cnt];
1481 i_dquot(inode)[cnt] = NULL;
Jan Karacc334122009-01-12 17:23:05 +01001482 }
Niu Yaweib9ba6f92014-06-04 12:23:19 +08001483 spin_unlock(&dq_data_lock);
Dmitry Monakhovdc52dd32009-12-14 15:21:15 +03001484 dqput_all(put);
Christoph Hellwig9f754752010-03-03 09:05:05 -05001485}
1486
1487void dquot_drop(struct inode *inode)
1488{
1489 int cnt;
1490
1491 if (IS_NOQUOTA(inode))
1492 return;
1493
1494 /*
1495 * Test before calling to rule out calls from proc and such
1496 * where we are not allowed to block. Note that this is
1497 * actually reliable test even without the lock - the caller
1498 * must assure that nobody can come after the DQUOT_DROP and
1499 * add quota pointers back anyway.
1500 */
1501 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
Jan Kara2d0fa462014-09-25 16:36:14 +02001502 if (i_dquot(inode)[cnt])
Christoph Hellwig9f754752010-03-03 09:05:05 -05001503 break;
1504 }
1505
1506 if (cnt < MAXQUOTAS)
1507 __dquot_drop(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508}
Mingming Cao08d03502009-01-14 16:19:32 +01001509EXPORT_SYMBOL(dquot_drop);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510
1511/*
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001512 * inode_reserved_space is managed internally by quota, and protected by
1513 * i_lock similar to i_blocks+i_bytes.
1514 */
1515static qsize_t *inode_reserved_space(struct inode * inode)
1516{
1517 /* Filesystem must explicitly define it's own method in order to use
1518 * quota reservation interface */
1519 BUG_ON(!inode->i_sb->dq_op->get_reserved_space);
1520 return inode->i_sb->dq_op->get_reserved_space(inode);
1521}
1522
Dmitry Monakhovc4690702010-02-09 17:53:36 +01001523void inode_add_rsv_space(struct inode *inode, qsize_t number)
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001524{
1525 spin_lock(&inode->i_lock);
1526 *inode_reserved_space(inode) += number;
1527 spin_unlock(&inode->i_lock);
1528}
Dmitry Monakhovc4690702010-02-09 17:53:36 +01001529EXPORT_SYMBOL(inode_add_rsv_space);
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001530
Dmitry Monakhovc4690702010-02-09 17:53:36 +01001531void inode_claim_rsv_space(struct inode *inode, qsize_t number)
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001532{
1533 spin_lock(&inode->i_lock);
1534 *inode_reserved_space(inode) -= number;
1535 __inode_add_bytes(inode, number);
1536 spin_unlock(&inode->i_lock);
1537}
Dmitry Monakhovc4690702010-02-09 17:53:36 +01001538EXPORT_SYMBOL(inode_claim_rsv_space);
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001539
Jan Kara1c8924e2013-08-17 09:32:32 -04001540void inode_reclaim_rsv_space(struct inode *inode, qsize_t number)
1541{
1542 spin_lock(&inode->i_lock);
1543 *inode_reserved_space(inode) += number;
1544 __inode_sub_bytes(inode, number);
1545 spin_unlock(&inode->i_lock);
1546}
1547EXPORT_SYMBOL(inode_reclaim_rsv_space);
1548
Dmitry Monakhovc4690702010-02-09 17:53:36 +01001549void inode_sub_rsv_space(struct inode *inode, qsize_t number)
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001550{
1551 spin_lock(&inode->i_lock);
1552 *inode_reserved_space(inode) -= number;
1553 spin_unlock(&inode->i_lock);
1554}
Dmitry Monakhovc4690702010-02-09 17:53:36 +01001555EXPORT_SYMBOL(inode_sub_rsv_space);
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001556
1557static qsize_t inode_get_rsv_space(struct inode *inode)
1558{
1559 qsize_t ret;
Jan Kara05b5d892010-01-06 18:03:36 +01001560
1561 if (!inode->i_sb->dq_op->get_reserved_space)
1562 return 0;
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001563 spin_lock(&inode->i_lock);
1564 ret = *inode_reserved_space(inode);
1565 spin_unlock(&inode->i_lock);
1566 return ret;
1567}
1568
1569static void inode_incr_space(struct inode *inode, qsize_t number,
1570 int reserve)
1571{
1572 if (reserve)
1573 inode_add_rsv_space(inode, number);
1574 else
1575 inode_add_bytes(inode, number);
1576}
1577
1578static void inode_decr_space(struct inode *inode, qsize_t number, int reserve)
1579{
1580 if (reserve)
1581 inode_sub_rsv_space(inode, number);
1582 else
1583 inode_sub_bytes(inode, number);
1584}
1585
1586/*
Christoph Hellwig5dd40562010-03-03 09:05:00 -05001587 * This functions updates i_blocks+i_bytes fields and quota information
1588 * (together with appropriate checks).
1589 *
1590 * NOTE: We absolutely rely on the fact that caller dirties the inode
1591 * (usually helpers in quotaops.h care about this) and holds a handle for
1592 * the current transaction so that dquot write and inode write go into the
1593 * same transaction.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594 */
1595
1596/*
1597 * This operation can block, but only after everything is updated
1598 */
Eric Sandeen56246f92010-05-16 09:00:00 -04001599int __dquot_alloc_space(struct inode *inode, qsize_t number, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600{
Niu Yaweib9ba6f92014-06-04 12:23:19 +08001601 int cnt, ret = 0, index;
Jan Karabf097aa2012-02-14 13:28:01 +01001602 struct dquot_warn warn[MAXQUOTAS];
Jan Kara2d0fa462014-09-25 16:36:14 +02001603 struct dquot **dquots = i_dquot(inode);
Eric Sandeen56246f92010-05-16 09:00:00 -04001604 int reserve = flags & DQUOT_SPACE_RESERVE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605
Christoph Hellwig189eef52010-06-04 10:56:29 +02001606 if (!dquot_active(inode)) {
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001607 inode_incr_space(inode, number, reserve);
1608 goto out;
1609 }
1610
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
Jan Karabf097aa2012-02-14 13:28:01 +01001612 warn[cnt].w_type = QUOTA_NL_NOWARN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613
Niu Yaweib9ba6f92014-06-04 12:23:19 +08001614 index = srcu_read_lock(&dquot_srcu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615 spin_lock(&dq_data_lock);
1616 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
Jan Karabf097aa2012-02-14 13:28:01 +01001617 if (!dquots[cnt])
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618 continue;
Jan Karabf097aa2012-02-14 13:28:01 +01001619 ret = check_bdq(dquots[cnt], number,
1620 !(flags & DQUOT_SPACE_WARN), &warn[cnt]);
1621 if (ret && !(flags & DQUOT_SPACE_NOFAIL)) {
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001622 spin_unlock(&dq_data_lock);
1623 goto out_flush_warn;
Mingming Caof18df222009-01-13 16:43:09 +01001624 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625 }
1626 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
Jan Karabf097aa2012-02-14 13:28:01 +01001627 if (!dquots[cnt])
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628 continue;
Mingming Caof18df222009-01-13 16:43:09 +01001629 if (reserve)
Jan Karabf097aa2012-02-14 13:28:01 +01001630 dquot_resv_space(dquots[cnt], number);
Mingming Caof18df222009-01-13 16:43:09 +01001631 else
Jan Karabf097aa2012-02-14 13:28:01 +01001632 dquot_incr_space(dquots[cnt], number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633 }
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001634 inode_incr_space(inode, number, reserve);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635 spin_unlock(&dq_data_lock);
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001636
1637 if (reserve)
1638 goto out_flush_warn;
Jan Karabf097aa2012-02-14 13:28:01 +01001639 mark_all_dquot_dirty(dquots);
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001640out_flush_warn:
Niu Yaweib9ba6f92014-06-04 12:23:19 +08001641 srcu_read_unlock(&dquot_srcu, index);
Jan Karabf097aa2012-02-14 13:28:01 +01001642 flush_warnings(warn);
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001643out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644 return ret;
1645}
Christoph Hellwig5dd40562010-03-03 09:05:00 -05001646EXPORT_SYMBOL(__dquot_alloc_space);
Mingming Caof18df222009-01-13 16:43:09 +01001647
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648/*
1649 * This operation can block, but only after everything is updated
1650 */
Jan Kara6bab3592014-10-20 16:01:33 +02001651int dquot_alloc_inode(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652{
Niu Yaweib9ba6f92014-06-04 12:23:19 +08001653 int cnt, ret = 0, index;
Jan Karabf097aa2012-02-14 13:28:01 +01001654 struct dquot_warn warn[MAXQUOTAS];
Jan Kara2d0fa462014-09-25 16:36:14 +02001655 struct dquot * const *dquots = i_dquot(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656
Christoph Hellwig189eef52010-06-04 10:56:29 +02001657 if (!dquot_active(inode))
Christoph Hellwig63936dd2010-03-03 09:05:01 -05001658 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
Jan Karabf097aa2012-02-14 13:28:01 +01001660 warn[cnt].w_type = QUOTA_NL_NOWARN;
Niu Yaweib9ba6f92014-06-04 12:23:19 +08001661
1662 index = srcu_read_lock(&dquot_srcu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663 spin_lock(&dq_data_lock);
1664 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
Jan Karabf097aa2012-02-14 13:28:01 +01001665 if (!dquots[cnt])
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666 continue;
Jan Karabf097aa2012-02-14 13:28:01 +01001667 ret = check_idq(dquots[cnt], 1, &warn[cnt]);
Christoph Hellwigefd8f0e2010-03-03 09:05:08 -05001668 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669 goto warn_put_all;
1670 }
1671
1672 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
Jan Karabf097aa2012-02-14 13:28:01 +01001673 if (!dquots[cnt])
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674 continue;
Jan Karabf097aa2012-02-14 13:28:01 +01001675 dquot_incr_inodes(dquots[cnt], 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676 }
Christoph Hellwigefd8f0e2010-03-03 09:05:08 -05001677
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678warn_put_all:
1679 spin_unlock(&dq_data_lock);
Christoph Hellwig63936dd2010-03-03 09:05:01 -05001680 if (ret == 0)
Jan Karabf097aa2012-02-14 13:28:01 +01001681 mark_all_dquot_dirty(dquots);
Niu Yaweib9ba6f92014-06-04 12:23:19 +08001682 srcu_read_unlock(&dquot_srcu, index);
Jan Karabf097aa2012-02-14 13:28:01 +01001683 flush_warnings(warn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684 return ret;
1685}
Mingming Cao08d03502009-01-14 16:19:32 +01001686EXPORT_SYMBOL(dquot_alloc_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687
Christoph Hellwig5dd40562010-03-03 09:05:00 -05001688/*
1689 * Convert in-memory reserved quotas to real consumed quotas
1690 */
1691int dquot_claim_space_nodirty(struct inode *inode, qsize_t number)
Mingming Cao740d9dc2009-01-13 16:43:14 +01001692{
Niu Yaweib9ba6f92014-06-04 12:23:19 +08001693 int cnt, index;
Mingming Cao740d9dc2009-01-13 16:43:14 +01001694
Christoph Hellwig189eef52010-06-04 10:56:29 +02001695 if (!dquot_active(inode)) {
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001696 inode_claim_rsv_space(inode, number);
Christoph Hellwig5dd40562010-03-03 09:05:00 -05001697 return 0;
Mingming Cao740d9dc2009-01-13 16:43:14 +01001698 }
1699
Niu Yaweib9ba6f92014-06-04 12:23:19 +08001700 index = srcu_read_lock(&dquot_srcu);
Mingming Cao740d9dc2009-01-13 16:43:14 +01001701 spin_lock(&dq_data_lock);
1702 /* Claim reserved quotas to allocated quotas */
1703 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
Jan Kara2d0fa462014-09-25 16:36:14 +02001704 if (i_dquot(inode)[cnt])
1705 dquot_claim_reserved_space(i_dquot(inode)[cnt],
Mingming Cao740d9dc2009-01-13 16:43:14 +01001706 number);
1707 }
1708 /* Update inode bytes */
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001709 inode_claim_rsv_space(inode, number);
Mingming Cao740d9dc2009-01-13 16:43:14 +01001710 spin_unlock(&dq_data_lock);
Jan Kara2d0fa462014-09-25 16:36:14 +02001711 mark_all_dquot_dirty(i_dquot(inode));
Niu Yaweib9ba6f92014-06-04 12:23:19 +08001712 srcu_read_unlock(&dquot_srcu, index);
Christoph Hellwig5dd40562010-03-03 09:05:00 -05001713 return 0;
Mingming Cao740d9dc2009-01-13 16:43:14 +01001714}
Christoph Hellwig5dd40562010-03-03 09:05:00 -05001715EXPORT_SYMBOL(dquot_claim_space_nodirty);
Mingming Cao740d9dc2009-01-13 16:43:14 +01001716
1717/*
Jan Kara1c8924e2013-08-17 09:32:32 -04001718 * Convert allocated space back to in-memory reserved quotas
1719 */
1720void dquot_reclaim_space_nodirty(struct inode *inode, qsize_t number)
1721{
Niu Yaweib9ba6f92014-06-04 12:23:19 +08001722 int cnt, index;
Jan Kara1c8924e2013-08-17 09:32:32 -04001723
1724 if (!dquot_active(inode)) {
1725 inode_reclaim_rsv_space(inode, number);
1726 return;
1727 }
1728
Niu Yaweib9ba6f92014-06-04 12:23:19 +08001729 index = srcu_read_lock(&dquot_srcu);
Jan Kara1c8924e2013-08-17 09:32:32 -04001730 spin_lock(&dq_data_lock);
1731 /* Claim reserved quotas to allocated quotas */
1732 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
Jan Kara2d0fa462014-09-25 16:36:14 +02001733 if (i_dquot(inode)[cnt])
1734 dquot_reclaim_reserved_space(i_dquot(inode)[cnt],
Jan Kara1c8924e2013-08-17 09:32:32 -04001735 number);
1736 }
1737 /* Update inode bytes */
1738 inode_reclaim_rsv_space(inode, number);
1739 spin_unlock(&dq_data_lock);
Jan Kara2d0fa462014-09-25 16:36:14 +02001740 mark_all_dquot_dirty(i_dquot(inode));
Niu Yaweib9ba6f92014-06-04 12:23:19 +08001741 srcu_read_unlock(&dquot_srcu, index);
Jan Kara1c8924e2013-08-17 09:32:32 -04001742 return;
1743}
1744EXPORT_SYMBOL(dquot_reclaim_space_nodirty);
1745
1746/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747 * This operation can block, but only after everything is updated
1748 */
Eric Sandeen56246f92010-05-16 09:00:00 -04001749void __dquot_free_space(struct inode *inode, qsize_t number, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750{
1751 unsigned int cnt;
Jan Karabf097aa2012-02-14 13:28:01 +01001752 struct dquot_warn warn[MAXQUOTAS];
Jan Kara2d0fa462014-09-25 16:36:14 +02001753 struct dquot **dquots = i_dquot(inode);
Niu Yaweib9ba6f92014-06-04 12:23:19 +08001754 int reserve = flags & DQUOT_SPACE_RESERVE, index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755
Christoph Hellwig189eef52010-06-04 10:56:29 +02001756 if (!dquot_active(inode)) {
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001757 inode_decr_space(inode, number, reserve);
Christoph Hellwig5dd40562010-03-03 09:05:00 -05001758 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759 }
Jan Kara657d3bf2008-07-25 01:46:52 -07001760
Niu Yaweib9ba6f92014-06-04 12:23:19 +08001761 index = srcu_read_lock(&dquot_srcu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762 spin_lock(&dq_data_lock);
1763 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
Jan Karabf097aa2012-02-14 13:28:01 +01001764 int wtype;
1765
1766 warn[cnt].w_type = QUOTA_NL_NOWARN;
1767 if (!dquots[cnt])
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768 continue;
Jan Karabf097aa2012-02-14 13:28:01 +01001769 wtype = info_bdq_free(dquots[cnt], number);
1770 if (wtype != QUOTA_NL_NOWARN)
1771 prepare_warning(&warn[cnt], dquots[cnt], wtype);
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001772 if (reserve)
Jan Karabf097aa2012-02-14 13:28:01 +01001773 dquot_free_reserved_space(dquots[cnt], number);
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001774 else
Jan Karabf097aa2012-02-14 13:28:01 +01001775 dquot_decr_space(dquots[cnt], number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776 }
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001777 inode_decr_space(inode, number, reserve);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778 spin_unlock(&dq_data_lock);
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001779
1780 if (reserve)
1781 goto out_unlock;
Jan Karabf097aa2012-02-14 13:28:01 +01001782 mark_all_dquot_dirty(dquots);
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001783out_unlock:
Niu Yaweib9ba6f92014-06-04 12:23:19 +08001784 srcu_read_unlock(&dquot_srcu, index);
Jan Karabf097aa2012-02-14 13:28:01 +01001785 flush_warnings(warn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786}
Christoph Hellwig5dd40562010-03-03 09:05:00 -05001787EXPORT_SYMBOL(__dquot_free_space);
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001788
1789/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790 * This operation can block, but only after everything is updated
1791 */
Jan Kara6bab3592014-10-20 16:01:33 +02001792void dquot_free_inode(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793{
1794 unsigned int cnt;
Jan Karabf097aa2012-02-14 13:28:01 +01001795 struct dquot_warn warn[MAXQUOTAS];
Jan Kara2d0fa462014-09-25 16:36:14 +02001796 struct dquot * const *dquots = i_dquot(inode);
Niu Yaweib9ba6f92014-06-04 12:23:19 +08001797 int index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798
Christoph Hellwig189eef52010-06-04 10:56:29 +02001799 if (!dquot_active(inode))
Christoph Hellwig63936dd2010-03-03 09:05:01 -05001800 return;
Jan Kara657d3bf2008-07-25 01:46:52 -07001801
Niu Yaweib9ba6f92014-06-04 12:23:19 +08001802 index = srcu_read_lock(&dquot_srcu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803 spin_lock(&dq_data_lock);
1804 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
Jan Karabf097aa2012-02-14 13:28:01 +01001805 int wtype;
1806
1807 warn[cnt].w_type = QUOTA_NL_NOWARN;
1808 if (!dquots[cnt])
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809 continue;
Jan Karabf097aa2012-02-14 13:28:01 +01001810 wtype = info_idq_free(dquots[cnt], 1);
1811 if (wtype != QUOTA_NL_NOWARN)
1812 prepare_warning(&warn[cnt], dquots[cnt], wtype);
1813 dquot_decr_inodes(dquots[cnt], 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814 }
1815 spin_unlock(&dq_data_lock);
Jan Karabf097aa2012-02-14 13:28:01 +01001816 mark_all_dquot_dirty(dquots);
Niu Yaweib9ba6f92014-06-04 12:23:19 +08001817 srcu_read_unlock(&dquot_srcu, index);
Jan Karabf097aa2012-02-14 13:28:01 +01001818 flush_warnings(warn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819}
Mingming Cao08d03502009-01-14 16:19:32 +01001820EXPORT_SYMBOL(dquot_free_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821
1822/*
1823 * Transfer the number of inode and blocks from one diskquota to an other.
Jan Karabc8e5f02010-05-13 19:58:50 +02001824 * On success, dquot references in transfer_to are consumed and references
1825 * to original dquots that need to be released are placed there. On failure,
1826 * references are kept untouched.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827 *
1828 * This operation can block, but only after everything is updated
1829 * A transaction must be started when entering this function.
Jan Karabc8e5f02010-05-13 19:58:50 +02001830 *
Niu Yaweib9ba6f92014-06-04 12:23:19 +08001831 * We are holding reference on transfer_from & transfer_to, no need to
1832 * protect them by srcu_read_lock().
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833 */
Jan Karabc8e5f02010-05-13 19:58:50 +02001834int __dquot_transfer(struct inode *inode, struct dquot **transfer_to)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835{
Mingming Cao740d9dc2009-01-13 16:43:14 +01001836 qsize_t space, cur_space;
1837 qsize_t rsv_space = 0;
Jan Karabc8e5f02010-05-13 19:58:50 +02001838 struct dquot *transfer_from[MAXQUOTAS] = {};
Christoph Hellwigefd8f0e2010-03-03 09:05:08 -05001839 int cnt, ret = 0;
Dmitry9e327842010-10-09 23:15:30 +04001840 char is_valid[MAXQUOTAS] = {};
Jan Karabf097aa2012-02-14 13:28:01 +01001841 struct dquot_warn warn_to[MAXQUOTAS];
1842 struct dquot_warn warn_from_inodes[MAXQUOTAS];
1843 struct dquot_warn warn_from_space[MAXQUOTAS];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845 if (IS_NOQUOTA(inode))
Christoph Hellwigefd8f0e2010-03-03 09:05:08 -05001846 return 0;
Jan Karacc334122009-01-12 17:23:05 +01001847 /* Initialize the arrays */
Jan Karabf097aa2012-02-14 13:28:01 +01001848 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1849 warn_to[cnt].w_type = QUOTA_NL_NOWARN;
1850 warn_from_inodes[cnt].w_type = QUOTA_NL_NOWARN;
1851 warn_from_space[cnt].w_type = QUOTA_NL_NOWARN;
1852 }
Niu Yaweib9ba6f92014-06-04 12:23:19 +08001853
1854 spin_lock(&dq_data_lock);
Jan Karacc334122009-01-12 17:23:05 +01001855 if (IS_NOQUOTA(inode)) { /* File without quota accounting? */
Niu Yaweib9ba6f92014-06-04 12:23:19 +08001856 spin_unlock(&dq_data_lock);
Jan Karabc8e5f02010-05-13 19:58:50 +02001857 return 0;
Jan Karacc334122009-01-12 17:23:05 +01001858 }
Mingming Cao740d9dc2009-01-13 16:43:14 +01001859 cur_space = inode_get_bytes(inode);
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +03001860 rsv_space = inode_get_rsv_space(inode);
Mingming Cao740d9dc2009-01-13 16:43:14 +01001861 space = cur_space + rsv_space;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862 /* Build the transfer_from list and check the limits */
1863 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
Dmitry9e327842010-10-09 23:15:30 +04001864 /*
1865 * Skip changes for same uid or gid or for turned off quota-type.
1866 */
Jan Karadd6f3c62009-01-26 16:01:43 +01001867 if (!transfer_to[cnt])
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868 continue;
Dmitry9e327842010-10-09 23:15:30 +04001869 /* Avoid races with quotaoff() */
1870 if (!sb_has_quota_active(inode->i_sb, cnt))
1871 continue;
1872 is_valid[cnt] = 1;
Jan Kara2d0fa462014-09-25 16:36:14 +02001873 transfer_from[cnt] = i_dquot(inode)[cnt];
Jan Karabf097aa2012-02-14 13:28:01 +01001874 ret = check_idq(transfer_to[cnt], 1, &warn_to[cnt]);
Christoph Hellwigefd8f0e2010-03-03 09:05:08 -05001875 if (ret)
1876 goto over_quota;
Jan Karabf097aa2012-02-14 13:28:01 +01001877 ret = check_bdq(transfer_to[cnt], space, 0, &warn_to[cnt]);
Christoph Hellwigefd8f0e2010-03-03 09:05:08 -05001878 if (ret)
Jan Karacc334122009-01-12 17:23:05 +01001879 goto over_quota;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880 }
1881
1882 /*
1883 * Finally perform the needed transfer from transfer_from to transfer_to
1884 */
1885 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
Dmitry9e327842010-10-09 23:15:30 +04001886 if (!is_valid[cnt])
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888 /* Due to IO error we might not have transfer_from[] structure */
1889 if (transfer_from[cnt]) {
Jan Karabf097aa2012-02-14 13:28:01 +01001890 int wtype;
1891 wtype = info_idq_free(transfer_from[cnt], 1);
1892 if (wtype != QUOTA_NL_NOWARN)
1893 prepare_warning(&warn_from_inodes[cnt],
1894 transfer_from[cnt], wtype);
1895 wtype = info_bdq_free(transfer_from[cnt], space);
1896 if (wtype != QUOTA_NL_NOWARN)
1897 prepare_warning(&warn_from_space[cnt],
1898 transfer_from[cnt], wtype);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899 dquot_decr_inodes(transfer_from[cnt], 1);
Mingming Cao740d9dc2009-01-13 16:43:14 +01001900 dquot_decr_space(transfer_from[cnt], cur_space);
1901 dquot_free_reserved_space(transfer_from[cnt],
1902 rsv_space);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903 }
1904
1905 dquot_incr_inodes(transfer_to[cnt], 1);
Mingming Cao740d9dc2009-01-13 16:43:14 +01001906 dquot_incr_space(transfer_to[cnt], cur_space);
1907 dquot_resv_space(transfer_to[cnt], rsv_space);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908
Jan Kara2d0fa462014-09-25 16:36:14 +02001909 i_dquot(inode)[cnt] = transfer_to[cnt];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911 spin_unlock(&dq_data_lock);
Jan Karacc334122009-01-12 17:23:05 +01001912
Dmitry Monakhovdc52dd32009-12-14 15:21:15 +03001913 mark_all_dquot_dirty(transfer_from);
1914 mark_all_dquot_dirty(transfer_to);
Jan Karabf097aa2012-02-14 13:28:01 +01001915 flush_warnings(warn_to);
1916 flush_warnings(warn_from_inodes);
1917 flush_warnings(warn_from_space);
Jan Karabc8e5f02010-05-13 19:58:50 +02001918 /* Pass back references to put */
Dmitry Monakhovdc52dd32009-12-14 15:21:15 +03001919 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
Dmitry9e327842010-10-09 23:15:30 +04001920 if (is_valid[cnt])
1921 transfer_to[cnt] = transfer_from[cnt];
Jan Kara86f3cbe2010-10-11 15:22:21 +02001922 return 0;
Jan Karacc334122009-01-12 17:23:05 +01001923over_quota:
1924 spin_unlock(&dq_data_lock);
Jan Karabf097aa2012-02-14 13:28:01 +01001925 flush_warnings(warn_to);
Jan Kara86f3cbe2010-10-11 15:22:21 +02001926 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927}
Jan Karabc8e5f02010-05-13 19:58:50 +02001928EXPORT_SYMBOL(__dquot_transfer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929
Dmitry Monakhov8ddd69d2010-02-16 08:31:50 +03001930/* Wrapper for transferring ownership of an inode for uid/gid only
1931 * Called from FSXXX_setattr()
1932 */
Christoph Hellwigb43fa822010-03-03 09:05:03 -05001933int dquot_transfer(struct inode *inode, struct iattr *iattr)
Jan Karab85f4b82008-07-25 01:46:50 -07001934{
Jan Karabc8e5f02010-05-13 19:58:50 +02001935 struct dquot *transfer_to[MAXQUOTAS] = {};
1936 struct super_block *sb = inode->i_sb;
1937 int ret;
Dmitry Monakhov8ddd69d2010-02-16 08:31:50 +03001938
Christoph Hellwig189eef52010-06-04 10:56:29 +02001939 if (!dquot_active(inode))
Jan Karabc8e5f02010-05-13 19:58:50 +02001940 return 0;
Dmitry Monakhov12755622010-04-08 22:04:20 +04001941
Eric W. Biederman1a06d422012-09-16 05:45:30 -07001942 if (iattr->ia_valid & ATTR_UID && !uid_eq(iattr->ia_uid, inode->i_uid))
Eric W. Biedermanaca645a2012-09-16 03:11:50 -07001943 transfer_to[USRQUOTA] = dqget(sb, make_kqid_uid(iattr->ia_uid));
Eric W. Biederman1a06d422012-09-16 05:45:30 -07001944 if (iattr->ia_valid & ATTR_GID && !gid_eq(iattr->ia_gid, inode->i_gid))
Eric W. Biedermanaca645a2012-09-16 03:11:50 -07001945 transfer_to[GRPQUOTA] = dqget(sb, make_kqid_gid(iattr->ia_gid));
Jan Karabc8e5f02010-05-13 19:58:50 +02001946
1947 ret = __dquot_transfer(inode, transfer_to);
1948 dqput_all(transfer_to);
1949 return ret;
Jan Karab85f4b82008-07-25 01:46:50 -07001950}
Christoph Hellwigb43fa822010-03-03 09:05:03 -05001951EXPORT_SYMBOL(dquot_transfer);
Jan Karab85f4b82008-07-25 01:46:50 -07001952
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953/*
1954 * Write info of quota file to disk
1955 */
1956int dquot_commit_info(struct super_block *sb, int type)
1957{
1958 int ret;
1959 struct quota_info *dqopt = sb_dqopt(sb);
1960
Ingo Molnard3be9152006-03-23 03:00:29 -08001961 mutex_lock(&dqopt->dqio_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962 ret = dqopt->ops[type]->write_file_info(sb, type);
Ingo Molnard3be9152006-03-23 03:00:29 -08001963 mutex_unlock(&dqopt->dqio_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964 return ret;
1965}
Mingming Cao08d03502009-01-14 16:19:32 +01001966EXPORT_SYMBOL(dquot_commit_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967
1968/*
1969 * Definitions of diskquota operations.
1970 */
Alexey Dobriyan61e225d2009-09-21 17:01:08 -07001971const struct dquot_operations dquot_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972 .write_dquot = dquot_commit,
1973 .acquire_dquot = dquot_acquire,
1974 .release_dquot = dquot_release,
1975 .mark_dirty = dquot_mark_dquot_dirty,
Jan Kara74f783a2008-08-19 14:51:22 +02001976 .write_info = dquot_commit_info,
1977 .alloc_dquot = dquot_alloc,
1978 .destroy_dquot = dquot_destroy,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979};
Christoph Hellwig123e9ca2010-05-19 07:16:44 -04001980EXPORT_SYMBOL(dquot_operations);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982/*
Christoph Hellwig907f4552010-03-03 09:05:06 -05001983 * Generic helper for ->open on filesystems supporting disk quotas.
1984 */
1985int dquot_file_open(struct inode *inode, struct file *file)
1986{
1987 int error;
1988
1989 error = generic_file_open(inode, file);
1990 if (!error && (file->f_mode & FMODE_WRITE))
Christoph Hellwig871a2932010-03-03 09:05:07 -05001991 dquot_initialize(inode);
Christoph Hellwig907f4552010-03-03 09:05:06 -05001992 return error;
1993}
1994EXPORT_SYMBOL(dquot_file_open);
1995
1996/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997 * Turn quota off on a device. type == -1 ==> quotaoff for all types (umount)
1998 */
Christoph Hellwig0f0dd622010-05-19 07:16:41 -04001999int dquot_disable(struct super_block *sb, int type, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000{
Jan Kara0ff5af82008-04-28 02:14:33 -07002001 int cnt, ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002 struct quota_info *dqopt = sb_dqopt(sb);
2003 struct inode *toputinode[MAXQUOTAS];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004
Jan Karaf55abc02008-08-20 17:50:32 +02002005 /* Cannot turn off usage accounting without turning off limits, or
2006 * suspend quotas and simultaneously turn quotas off. */
2007 if ((flags & DQUOT_USAGE_ENABLED && !(flags & DQUOT_LIMITS_ENABLED))
2008 || (flags & DQUOT_SUSPENDED && flags & (DQUOT_LIMITS_ENABLED |
2009 DQUOT_USAGE_ENABLED)))
2010 return -EINVAL;
2011
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012 /* We need to serialize quota_off() for device */
Ingo Molnard3be9152006-03-23 03:00:29 -08002013 mutex_lock(&dqopt->dqonoff_mutex);
Jan Kara9377abd2008-05-12 14:02:08 -07002014
2015 /*
2016 * Skip everything if there's nothing to do. We have to do this because
2017 * sometimes we are called when fill_super() failed and calling
2018 * sync_fs() in such cases does no good.
2019 */
Jan Karaf55abc02008-08-20 17:50:32 +02002020 if (!sb_any_quota_loaded(sb)) {
Jan Kara9377abd2008-05-12 14:02:08 -07002021 mutex_unlock(&dqopt->dqonoff_mutex);
2022 return 0;
2023 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
2025 toputinode[cnt] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026 if (type != -1 && cnt != type)
2027 continue;
Jan Karaf55abc02008-08-20 17:50:32 +02002028 if (!sb_has_quota_loaded(sb, cnt))
Jan Kara0ff5af82008-04-28 02:14:33 -07002029 continue;
Jan Karaf55abc02008-08-20 17:50:32 +02002030
2031 if (flags & DQUOT_SUSPENDED) {
Jan Karacc334122009-01-12 17:23:05 +01002032 spin_lock(&dq_state_lock);
Jan Karaf55abc02008-08-20 17:50:32 +02002033 dqopt->flags |=
2034 dquot_state_flag(DQUOT_SUSPENDED, cnt);
Jan Karacc334122009-01-12 17:23:05 +01002035 spin_unlock(&dq_state_lock);
Jan Karaf55abc02008-08-20 17:50:32 +02002036 } else {
Jan Karacc334122009-01-12 17:23:05 +01002037 spin_lock(&dq_state_lock);
Jan Karaf55abc02008-08-20 17:50:32 +02002038 dqopt->flags &= ~dquot_state_flag(flags, cnt);
2039 /* Turning off suspended quotas? */
2040 if (!sb_has_quota_loaded(sb, cnt) &&
2041 sb_has_quota_suspended(sb, cnt)) {
2042 dqopt->flags &= ~dquot_state_flag(
2043 DQUOT_SUSPENDED, cnt);
Jan Karacc334122009-01-12 17:23:05 +01002044 spin_unlock(&dq_state_lock);
Jan Karaf55abc02008-08-20 17:50:32 +02002045 iput(dqopt->files[cnt]);
2046 dqopt->files[cnt] = NULL;
2047 continue;
2048 }
Jan Karacc334122009-01-12 17:23:05 +01002049 spin_unlock(&dq_state_lock);
Jan Kara0ff5af82008-04-28 02:14:33 -07002050 }
Jan Karaf55abc02008-08-20 17:50:32 +02002051
2052 /* We still have to keep quota loaded? */
2053 if (sb_has_quota_loaded(sb, cnt) && !(flags & DQUOT_SUSPENDED))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055
2056 /* Note: these are blocking operations */
2057 drop_dquot_ref(sb, cnt);
2058 invalidate_dquots(sb, cnt);
2059 /*
Jan Kara268157b2009-01-27 15:47:22 +01002060 * Now all dquots should be invalidated, all writes done so we
2061 * should be only users of the info. No locks needed.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062 */
2063 if (info_dirty(&dqopt->info[cnt]))
2064 sb->dq_op->write_info(sb, cnt);
2065 if (dqopt->ops[cnt]->free_file_info)
2066 dqopt->ops[cnt]->free_file_info(sb, cnt);
2067 put_quota_format(dqopt->info[cnt].dqi_format);
2068
2069 toputinode[cnt] = dqopt->files[cnt];
Jan Karaf55abc02008-08-20 17:50:32 +02002070 if (!sb_has_quota_loaded(sb, cnt))
Jan Kara0ff5af82008-04-28 02:14:33 -07002071 dqopt->files[cnt] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072 dqopt->info[cnt].dqi_flags = 0;
2073 dqopt->info[cnt].dqi_igrace = 0;
2074 dqopt->info[cnt].dqi_bgrace = 0;
2075 dqopt->ops[cnt] = NULL;
2076 }
Ingo Molnard3be9152006-03-23 03:00:29 -08002077 mutex_unlock(&dqopt->dqonoff_mutex);
Jan Karaca785ec2008-09-30 17:53:37 +02002078
2079 /* Skip syncing and setting flags if quota files are hidden */
2080 if (dqopt->flags & DQUOT_QUOTA_SYS_FILE)
2081 goto put_inodes;
2082
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083 /* Sync the superblock so that buffers with quota data are written to
Al Viro7b7b1ac2005-11-07 17:13:39 -05002084 * disk (and so userspace sees correct data afterwards). */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085 if (sb->s_op->sync_fs)
2086 sb->s_op->sync_fs(sb, 1);
2087 sync_blockdev(sb->s_bdev);
2088 /* Now the quota files are just ordinary files and we can set the
2089 * inode flags back. Moreover we discard the pagecache so that
2090 * userspace sees the writes we did bypassing the pagecache. We
2091 * must also discard the blockdev buffers so that we see the
2092 * changes done by userspace on the next quotaon() */
2093 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
2094 if (toputinode[cnt]) {
Ingo Molnard3be9152006-03-23 03:00:29 -08002095 mutex_lock(&dqopt->dqonoff_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096 /* If quota was reenabled in the meantime, we have
2097 * nothing to do */
Jan Karaf55abc02008-08-20 17:50:32 +02002098 if (!sb_has_quota_loaded(sb, cnt)) {
Jan Karaa80b12c2012-04-25 21:15:53 +02002099 mutex_lock(&toputinode[cnt]->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100 toputinode[cnt]->i_flags &= ~(S_IMMUTABLE |
2101 S_NOATIME | S_NOQUOTA);
Jan Kara268157b2009-01-27 15:47:22 +01002102 truncate_inode_pages(&toputinode[cnt]->i_data,
2103 0);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002104 mutex_unlock(&toputinode[cnt]->i_mutex);
Jan Kara43d29322010-07-21 14:22:21 +02002105 mark_inode_dirty_sync(toputinode[cnt]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106 }
Ingo Molnard3be9152006-03-23 03:00:29 -08002107 mutex_unlock(&dqopt->dqonoff_mutex);
Jan Karaca785ec2008-09-30 17:53:37 +02002108 }
2109 if (sb->s_bdev)
2110 invalidate_bdev(sb->s_bdev);
2111put_inodes:
2112 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
2113 if (toputinode[cnt]) {
Jan Kara0ff5af82008-04-28 02:14:33 -07002114 /* On remount RO, we keep the inode pointer so that we
Jan Karaf55abc02008-08-20 17:50:32 +02002115 * can reenable quota on the subsequent remount RW. We
2116 * have to check 'flags' variable and not use sb_has_
2117 * function because another quotaon / quotaoff could
2118 * change global state before we got here. We refuse
2119 * to suspend quotas when there is pending delete on
2120 * the quota file... */
2121 if (!(flags & DQUOT_SUSPENDED))
Jan Kara0ff5af82008-04-28 02:14:33 -07002122 iput(toputinode[cnt]);
2123 else if (!toputinode[cnt]->i_nlink)
2124 ret = -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125 }
Jan Kara0ff5af82008-04-28 02:14:33 -07002126 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127}
Christoph Hellwig0f0dd622010-05-19 07:16:41 -04002128EXPORT_SYMBOL(dquot_disable);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129
Christoph Hellwig287a8092010-05-19 07:16:45 -04002130int dquot_quota_off(struct super_block *sb, int type)
Jan Karaf55abc02008-08-20 17:50:32 +02002131{
Christoph Hellwig0f0dd622010-05-19 07:16:41 -04002132 return dquot_disable(sb, type,
2133 DQUOT_USAGE_ENABLED | DQUOT_LIMITS_ENABLED);
Jan Karaf55abc02008-08-20 17:50:32 +02002134}
Christoph Hellwig287a8092010-05-19 07:16:45 -04002135EXPORT_SYMBOL(dquot_quota_off);
Christoph Hellwig0f0dd622010-05-19 07:16:41 -04002136
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137/*
2138 * Turn quotas on on a device
2139 */
2140
Jan Karaf55abc02008-08-20 17:50:32 +02002141/*
2142 * Helper function to turn quotas on when we already have the inode of
2143 * quota file and no quota information is loaded.
2144 */
2145static int vfs_load_quota_inode(struct inode *inode, int type, int format_id,
2146 unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147{
2148 struct quota_format_type *fmt = find_quota_format(format_id);
2149 struct super_block *sb = inode->i_sb;
2150 struct quota_info *dqopt = sb_dqopt(sb);
2151 int error;
2152 int oldflags = -1;
2153
2154 if (!fmt)
2155 return -ESRCH;
2156 if (!S_ISREG(inode->i_mode)) {
2157 error = -EACCES;
2158 goto out_fmt;
2159 }
2160 if (IS_RDONLY(inode)) {
2161 error = -EROFS;
2162 goto out_fmt;
2163 }
2164 if (!sb->s_op->quota_write || !sb->s_op->quota_read) {
2165 error = -EINVAL;
2166 goto out_fmt;
2167 }
Jan Karaf55abc02008-08-20 17:50:32 +02002168 /* Usage always has to be set... */
2169 if (!(flags & DQUOT_USAGE_ENABLED)) {
2170 error = -EINVAL;
2171 goto out_fmt;
2172 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173
Jan Karaca785ec2008-09-30 17:53:37 +02002174 if (!(dqopt->flags & DQUOT_QUOTA_SYS_FILE)) {
Jan Karaab94c392010-02-22 21:07:17 +01002175 /* As we bypass the pagecache we must now flush all the
2176 * dirty data and invalidate caches so that kernel sees
2177 * changes from userspace. It is not enough to just flush
2178 * the quota file since if blocksize < pagesize, invalidation
2179 * of the cache could fail because of other unrelated dirty
2180 * data */
2181 sync_filesystem(sb);
Jan Karaca785ec2008-09-30 17:53:37 +02002182 invalidate_bdev(sb->s_bdev);
2183 }
Ingo Molnard3be9152006-03-23 03:00:29 -08002184 mutex_lock(&dqopt->dqonoff_mutex);
Jan Karaf55abc02008-08-20 17:50:32 +02002185 if (sb_has_quota_loaded(sb, type)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186 error = -EBUSY;
2187 goto out_lock;
2188 }
Jan Karaca785ec2008-09-30 17:53:37 +02002189
2190 if (!(dqopt->flags & DQUOT_QUOTA_SYS_FILE)) {
2191 /* We don't want quota and atime on quota files (deadlocks
2192 * possible) Also nobody should write to the file - we use
2193 * special IO operations which ignore the immutable bit. */
Jan Karaa80b12c2012-04-25 21:15:53 +02002194 mutex_lock(&inode->i_mutex);
Jan Kara268157b2009-01-27 15:47:22 +01002195 oldflags = inode->i_flags & (S_NOATIME | S_IMMUTABLE |
2196 S_NOQUOTA);
Jan Karaca785ec2008-09-30 17:53:37 +02002197 inode->i_flags |= S_NOQUOTA | S_NOATIME | S_IMMUTABLE;
Jan Karadee86562009-07-22 18:12:17 +02002198 mutex_unlock(&inode->i_mutex);
Jan Kara26245c92010-01-06 17:20:35 +01002199 /*
2200 * When S_NOQUOTA is set, remove dquot references as no more
2201 * references can be added
2202 */
Christoph Hellwig9f754752010-03-03 09:05:05 -05002203 __dquot_drop(inode);
Jan Karaca785ec2008-09-30 17:53:37 +02002204 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205
2206 error = -EIO;
2207 dqopt->files[type] = igrab(inode);
2208 if (!dqopt->files[type])
2209 goto out_lock;
2210 error = -EINVAL;
2211 if (!fmt->qf_ops->check_quota_file(sb, type))
2212 goto out_file_init;
2213
2214 dqopt->ops[type] = fmt->qf_ops;
2215 dqopt->info[type].dqi_format = fmt;
Jan Kara0ff5af82008-04-28 02:14:33 -07002216 dqopt->info[type].dqi_fmt_id = format_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217 INIT_LIST_HEAD(&dqopt->info[type].dqi_dirty_list);
Ingo Molnard3be9152006-03-23 03:00:29 -08002218 mutex_lock(&dqopt->dqio_mutex);
Jan Kara268157b2009-01-27 15:47:22 +01002219 error = dqopt->ops[type]->read_file_info(sb, type);
2220 if (error < 0) {
Ingo Molnard3be9152006-03-23 03:00:29 -08002221 mutex_unlock(&dqopt->dqio_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222 goto out_file_init;
2223 }
Jan Kara46fe44c2011-11-16 15:03:59 +01002224 if (dqopt->flags & DQUOT_QUOTA_SYS_FILE)
2225 dqopt->info[type].dqi_flags |= DQF_SYS_FILE;
Ingo Molnard3be9152006-03-23 03:00:29 -08002226 mutex_unlock(&dqopt->dqio_mutex);
Jan Karacc334122009-01-12 17:23:05 +01002227 spin_lock(&dq_state_lock);
Jan Karaf55abc02008-08-20 17:50:32 +02002228 dqopt->flags |= dquot_state_flag(flags, type);
Jan Karacc334122009-01-12 17:23:05 +01002229 spin_unlock(&dq_state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230
2231 add_dquot_ref(sb, type);
Ingo Molnard3be9152006-03-23 03:00:29 -08002232 mutex_unlock(&dqopt->dqonoff_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233
2234 return 0;
2235
2236out_file_init:
2237 dqopt->files[type] = NULL;
2238 iput(inode);
2239out_lock:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240 if (oldflags != -1) {
Jan Karaa80b12c2012-04-25 21:15:53 +02002241 mutex_lock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242 /* Set the flags back (in the case of accidental quotaon()
2243 * on a wrong file we don't want to mess up the flags) */
2244 inode->i_flags &= ~(S_NOATIME | S_NOQUOTA | S_IMMUTABLE);
2245 inode->i_flags |= oldflags;
Jan Karadee86562009-07-22 18:12:17 +02002246 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247 }
Jiaying Zhangd01730d2009-07-07 18:15:21 +02002248 mutex_unlock(&dqopt->dqonoff_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249out_fmt:
2250 put_quota_format(fmt);
2251
2252 return error;
2253}
2254
Jan Kara0ff5af82008-04-28 02:14:33 -07002255/* Reenable quotas on remount RW */
Christoph Hellwig0f0dd622010-05-19 07:16:41 -04002256int dquot_resume(struct super_block *sb, int type)
Jan Kara0ff5af82008-04-28 02:14:33 -07002257{
2258 struct quota_info *dqopt = sb_dqopt(sb);
2259 struct inode *inode;
Christoph Hellwig0f0dd622010-05-19 07:16:41 -04002260 int ret = 0, cnt;
Jan Karaf55abc02008-08-20 17:50:32 +02002261 unsigned int flags;
Jan Kara0ff5af82008-04-28 02:14:33 -07002262
Christoph Hellwig0f0dd622010-05-19 07:16:41 -04002263 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
2264 if (type != -1 && cnt != type)
2265 continue;
Jan Kara0ff5af82008-04-28 02:14:33 -07002266
Christoph Hellwig0f0dd622010-05-19 07:16:41 -04002267 mutex_lock(&dqopt->dqonoff_mutex);
2268 if (!sb_has_quota_suspended(sb, cnt)) {
2269 mutex_unlock(&dqopt->dqonoff_mutex);
2270 continue;
2271 }
2272 inode = dqopt->files[cnt];
2273 dqopt->files[cnt] = NULL;
2274 spin_lock(&dq_state_lock);
2275 flags = dqopt->flags & dquot_state_flag(DQUOT_USAGE_ENABLED |
2276 DQUOT_LIMITS_ENABLED,
2277 cnt);
2278 dqopt->flags &= ~dquot_state_flag(DQUOT_STATE_FLAGS, cnt);
2279 spin_unlock(&dq_state_lock);
2280 mutex_unlock(&dqopt->dqonoff_mutex);
2281
2282 flags = dquot_generic_flag(flags, cnt);
2283 ret = vfs_load_quota_inode(inode, cnt,
2284 dqopt->info[cnt].dqi_fmt_id, flags);
2285 iput(inode);
2286 }
Jan Kara0ff5af82008-04-28 02:14:33 -07002287
2288 return ret;
2289}
Christoph Hellwig0f0dd622010-05-19 07:16:41 -04002290EXPORT_SYMBOL(dquot_resume);
Jan Kara0ff5af82008-04-28 02:14:33 -07002291
Jan Karaf00c9e42010-09-15 17:38:58 +02002292int dquot_quota_on(struct super_block *sb, int type, int format_id,
2293 struct path *path)
Al Viro77e69da2008-08-01 04:29:18 -04002294{
2295 int error = security_quota_on(path->dentry);
2296 if (error)
2297 return error;
2298 /* Quota file not on the same filesystem? */
Al Virod8c95842011-12-07 18:16:57 -05002299 if (path->dentry->d_sb != sb)
Al Viro77e69da2008-08-01 04:29:18 -04002300 error = -EXDEV;
2301 else
Jan Karaf55abc02008-08-20 17:50:32 +02002302 error = vfs_load_quota_inode(path->dentry->d_inode, type,
2303 format_id, DQUOT_USAGE_ENABLED |
2304 DQUOT_LIMITS_ENABLED);
Al Viro77e69da2008-08-01 04:29:18 -04002305 return error;
2306}
Christoph Hellwig287a8092010-05-19 07:16:45 -04002307EXPORT_SYMBOL(dquot_quota_on);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308
2309/*
Jan Karaf55abc02008-08-20 17:50:32 +02002310 * More powerful function for turning on quotas allowing setting
2311 * of individual quota flags
2312 */
Christoph Hellwig287a8092010-05-19 07:16:45 -04002313int dquot_enable(struct inode *inode, int type, int format_id,
2314 unsigned int flags)
Jan Karaf55abc02008-08-20 17:50:32 +02002315{
2316 int ret = 0;
2317 struct super_block *sb = inode->i_sb;
2318 struct quota_info *dqopt = sb_dqopt(sb);
2319
2320 /* Just unsuspend quotas? */
Christoph Hellwig0f0dd622010-05-19 07:16:41 -04002321 BUG_ON(flags & DQUOT_SUSPENDED);
2322
Jan Karaf55abc02008-08-20 17:50:32 +02002323 if (!flags)
2324 return 0;
2325 /* Just updating flags needed? */
2326 if (sb_has_quota_loaded(sb, type)) {
2327 mutex_lock(&dqopt->dqonoff_mutex);
2328 /* Now do a reliable test... */
2329 if (!sb_has_quota_loaded(sb, type)) {
2330 mutex_unlock(&dqopt->dqonoff_mutex);
2331 goto load_quota;
2332 }
2333 if (flags & DQUOT_USAGE_ENABLED &&
2334 sb_has_quota_usage_enabled(sb, type)) {
2335 ret = -EBUSY;
2336 goto out_lock;
2337 }
2338 if (flags & DQUOT_LIMITS_ENABLED &&
2339 sb_has_quota_limits_enabled(sb, type)) {
2340 ret = -EBUSY;
2341 goto out_lock;
2342 }
Jan Karacc334122009-01-12 17:23:05 +01002343 spin_lock(&dq_state_lock);
Jan Karaf55abc02008-08-20 17:50:32 +02002344 sb_dqopt(sb)->flags |= dquot_state_flag(flags, type);
Jan Karacc334122009-01-12 17:23:05 +01002345 spin_unlock(&dq_state_lock);
Jan Karaf55abc02008-08-20 17:50:32 +02002346out_lock:
2347 mutex_unlock(&dqopt->dqonoff_mutex);
2348 return ret;
2349 }
2350
2351load_quota:
2352 return vfs_load_quota_inode(inode, type, format_id, flags);
2353}
Christoph Hellwig287a8092010-05-19 07:16:45 -04002354EXPORT_SYMBOL(dquot_enable);
Jan Karaf55abc02008-08-20 17:50:32 +02002355
2356/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357 * This function is used when filesystem needs to initialize quotas
2358 * during mount time.
2359 */
Christoph Hellwig287a8092010-05-19 07:16:45 -04002360int dquot_quota_on_mount(struct super_block *sb, char *qf_name,
Christoph Hellwig84de8562005-06-23 00:09:16 -07002361 int format_id, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362{
Christoph Hellwig84de8562005-06-23 00:09:16 -07002363 struct dentry *dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364 int error;
2365
Jan Karac56818d2009-11-12 15:42:08 +01002366 mutex_lock(&sb->s_root->d_inode->i_mutex);
Christoph Hellwig2fa389c2005-06-23 00:09:16 -07002367 dentry = lookup_one_len(qf_name, sb->s_root, strlen(qf_name));
Jan Karac56818d2009-11-12 15:42:08 +01002368 mutex_unlock(&sb->s_root->d_inode->i_mutex);
Christoph Hellwig84de8562005-06-23 00:09:16 -07002369 if (IS_ERR(dentry))
2370 return PTR_ERR(dentry);
2371
Jan Kara154f4842005-11-28 13:44:14 -08002372 if (!dentry->d_inode) {
2373 error = -ENOENT;
2374 goto out;
2375 }
2376
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377 error = security_quota_on(dentry);
Christoph Hellwig84de8562005-06-23 00:09:16 -07002378 if (!error)
Jan Karaf55abc02008-08-20 17:50:32 +02002379 error = vfs_load_quota_inode(dentry->d_inode, type, format_id,
2380 DQUOT_USAGE_ENABLED | DQUOT_LIMITS_ENABLED);
Christoph Hellwig84de8562005-06-23 00:09:16 -07002381
Jan Kara154f4842005-11-28 13:44:14 -08002382out:
Christoph Hellwig84de8562005-06-23 00:09:16 -07002383 dput(dentry);
2384 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002385}
Christoph Hellwig287a8092010-05-19 07:16:45 -04002386EXPORT_SYMBOL(dquot_quota_on_mount);
Jan Karab85f4b82008-07-25 01:46:50 -07002387
Jan Kara3e2af672014-10-06 18:40:51 +02002388static int dquot_quota_enable(struct super_block *sb, unsigned int flags)
2389{
2390 int ret;
2391 int type;
2392 struct quota_info *dqopt = sb_dqopt(sb);
2393
2394 if (!(dqopt->flags & DQUOT_QUOTA_SYS_FILE))
2395 return -ENOSYS;
2396 /* Accounting cannot be turned on while fs is mounted */
2397 flags &= ~(FS_QUOTA_UDQ_ACCT | FS_QUOTA_GDQ_ACCT | FS_QUOTA_PDQ_ACCT);
2398 if (!flags)
2399 return -EINVAL;
2400 for (type = 0; type < MAXQUOTAS; type++) {
2401 if (!(flags & qtype_enforce_flag(type)))
2402 continue;
2403 /* Can't enforce without accounting */
2404 if (!sb_has_quota_usage_enabled(sb, type))
2405 return -EINVAL;
2406 ret = dquot_enable(dqopt->files[type], type,
2407 dqopt->info[type].dqi_fmt_id,
2408 DQUOT_LIMITS_ENABLED);
2409 if (ret < 0)
2410 goto out_err;
2411 }
2412 return 0;
2413out_err:
2414 /* Backout enforcement enablement we already did */
2415 for (type--; type >= 0; type--) {
2416 if (flags & qtype_enforce_flag(type))
2417 dquot_disable(sb, type, DQUOT_LIMITS_ENABLED);
2418 }
2419 /* Error code translation for better compatibility with XFS */
2420 if (ret == -EBUSY)
2421 ret = -EEXIST;
2422 return ret;
2423}
2424
2425static int dquot_quota_disable(struct super_block *sb, unsigned int flags)
2426{
2427 int ret;
2428 int type;
2429 struct quota_info *dqopt = sb_dqopt(sb);
2430
2431 if (!(dqopt->flags & DQUOT_QUOTA_SYS_FILE))
2432 return -ENOSYS;
2433 /*
2434 * We don't support turning off accounting via quotactl. In principle
2435 * quota infrastructure can do this but filesystems don't expect
2436 * userspace to be able to do it.
2437 */
2438 if (flags &
2439 (FS_QUOTA_UDQ_ACCT | FS_QUOTA_GDQ_ACCT | FS_QUOTA_PDQ_ACCT))
2440 return -EOPNOTSUPP;
2441
2442 /* Filter out limits not enabled */
2443 for (type = 0; type < MAXQUOTAS; type++)
2444 if (!sb_has_quota_limits_enabled(sb, type))
2445 flags &= ~qtype_enforce_flag(type);
2446 /* Nothing left? */
2447 if (!flags)
2448 return -EEXIST;
2449 for (type = 0; type < MAXQUOTAS; type++) {
2450 if (flags & qtype_enforce_flag(type)) {
2451 ret = dquot_disable(sb, type, DQUOT_LIMITS_ENABLED);
2452 if (ret < 0)
2453 goto out_err;
2454 }
2455 }
2456 return 0;
2457out_err:
2458 /* Backout enforcement disabling we already did */
2459 for (type--; type >= 0; type--) {
2460 if (flags & qtype_enforce_flag(type))
2461 dquot_enable(dqopt->files[type], type,
2462 dqopt->info[type].dqi_fmt_id,
2463 DQUOT_LIMITS_ENABLED);
2464 }
2465 return ret;
2466}
2467
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468/* Generic routine for getting common part of quota structure */
Jan Kara14bf61f2014-10-09 16:03:13 +02002469static void do_get_dqblk(struct dquot *dquot, struct qc_dqblk *di)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470{
2471 struct mem_dqblk *dm = &dquot->dq_dqb;
2472
Christoph Hellwigb9b2dd32010-05-06 17:04:58 -04002473 memset(di, 0, sizeof(*di));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002474 spin_lock(&dq_data_lock);
Jan Kara14bf61f2014-10-09 16:03:13 +02002475 di->d_spc_hardlimit = dm->dqb_bhardlimit;
2476 di->d_spc_softlimit = dm->dqb_bsoftlimit;
Christoph Hellwigb9b2dd32010-05-06 17:04:58 -04002477 di->d_ino_hardlimit = dm->dqb_ihardlimit;
2478 di->d_ino_softlimit = dm->dqb_isoftlimit;
Jan Kara14bf61f2014-10-09 16:03:13 +02002479 di->d_space = dm->dqb_curspace + dm->dqb_rsvspace;
2480 di->d_ino_count = dm->dqb_curinodes;
2481 di->d_spc_timer = dm->dqb_btime;
2482 di->d_ino_timer = dm->dqb_itime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002483 spin_unlock(&dq_data_lock);
2484}
2485
Eric W. Biederman74a8a102012-09-16 02:07:49 -07002486int dquot_get_dqblk(struct super_block *sb, struct kqid qid,
Jan Kara14bf61f2014-10-09 16:03:13 +02002487 struct qc_dqblk *di)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002488{
2489 struct dquot *dquot;
2490
Eric W. Biedermanaca645a2012-09-16 03:11:50 -07002491 dquot = dqget(sb, qid);
Jan Karadd6f3c62009-01-26 16:01:43 +01002492 if (!dquot)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002493 return -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002494 do_get_dqblk(dquot, di);
2495 dqput(dquot);
Jan Karacc334122009-01-12 17:23:05 +01002496
Linus Torvalds1da177e2005-04-16 15:20:36 -07002497 return 0;
2498}
Christoph Hellwig287a8092010-05-19 07:16:45 -04002499EXPORT_SYMBOL(dquot_get_dqblk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002500
Jan Kara14bf61f2014-10-09 16:03:13 +02002501#define VFS_QC_MASK \
2502 (QC_SPACE | QC_SPC_SOFT | QC_SPC_HARD | \
2503 QC_INO_COUNT | QC_INO_SOFT | QC_INO_HARD | \
2504 QC_SPC_TIMER | QC_INO_TIMER)
Christoph Hellwigc472b432010-05-06 17:05:17 -04002505
Linus Torvalds1da177e2005-04-16 15:20:36 -07002506/* Generic routine for setting common part of quota structure */
Jan Kara14bf61f2014-10-09 16:03:13 +02002507static int do_set_dqblk(struct dquot *dquot, struct qc_dqblk *di)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002508{
2509 struct mem_dqblk *dm = &dquot->dq_dqb;
2510 int check_blim = 0, check_ilim = 0;
Eric W. Biederman4c376dc2012-09-16 03:56:19 -07002511 struct mem_dqinfo *dqi = &sb_dqopt(dquot->dq_sb)->info[dquot->dq_id.type];
Andrew Perepechko338bf9a2008-04-28 02:14:31 -07002512
Jan Kara14bf61f2014-10-09 16:03:13 +02002513 if (di->d_fieldmask & ~VFS_QC_MASK)
Christoph Hellwigc472b432010-05-06 17:05:17 -04002514 return -EINVAL;
2515
Jan Kara14bf61f2014-10-09 16:03:13 +02002516 if (((di->d_fieldmask & QC_SPC_SOFT) &&
Jan Karab10a0812014-10-09 16:54:13 +02002517 di->d_spc_softlimit > dqi->dqi_max_spc_limit) ||
Jan Kara14bf61f2014-10-09 16:03:13 +02002518 ((di->d_fieldmask & QC_SPC_HARD) &&
Jan Karab10a0812014-10-09 16:54:13 +02002519 di->d_spc_hardlimit > dqi->dqi_max_spc_limit) ||
Jan Kara14bf61f2014-10-09 16:03:13 +02002520 ((di->d_fieldmask & QC_INO_SOFT) &&
Jan Karab10a0812014-10-09 16:54:13 +02002521 (di->d_ino_softlimit > dqi->dqi_max_ino_limit)) ||
Jan Kara14bf61f2014-10-09 16:03:13 +02002522 ((di->d_fieldmask & QC_INO_HARD) &&
Jan Karab10a0812014-10-09 16:54:13 +02002523 (di->d_ino_hardlimit > dqi->dqi_max_ino_limit)))
Andrew Perepechko338bf9a2008-04-28 02:14:31 -07002524 return -ERANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002525
2526 spin_lock(&dq_data_lock);
Jan Kara14bf61f2014-10-09 16:03:13 +02002527 if (di->d_fieldmask & QC_SPACE) {
2528 dm->dqb_curspace = di->d_space - dm->dqb_rsvspace;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002529 check_blim = 1;
Andrew Perepechko08261672010-04-12 22:16:50 +04002530 set_bit(DQ_LASTSET_B + QIF_SPACE_B, &dquot->dq_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002531 }
Christoph Hellwigc472b432010-05-06 17:05:17 -04002532
Jan Kara14bf61f2014-10-09 16:03:13 +02002533 if (di->d_fieldmask & QC_SPC_SOFT)
2534 dm->dqb_bsoftlimit = di->d_spc_softlimit;
2535 if (di->d_fieldmask & QC_SPC_HARD)
2536 dm->dqb_bhardlimit = di->d_spc_hardlimit;
2537 if (di->d_fieldmask & (QC_SPC_SOFT | QC_SPC_HARD)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002538 check_blim = 1;
Andrew Perepechko08261672010-04-12 22:16:50 +04002539 set_bit(DQ_LASTSET_B + QIF_BLIMITS_B, &dquot->dq_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002540 }
Christoph Hellwigc472b432010-05-06 17:05:17 -04002541
Jan Kara14bf61f2014-10-09 16:03:13 +02002542 if (di->d_fieldmask & QC_INO_COUNT) {
2543 dm->dqb_curinodes = di->d_ino_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002544 check_ilim = 1;
Andrew Perepechko08261672010-04-12 22:16:50 +04002545 set_bit(DQ_LASTSET_B + QIF_INODES_B, &dquot->dq_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002546 }
Christoph Hellwigc472b432010-05-06 17:05:17 -04002547
Jan Kara14bf61f2014-10-09 16:03:13 +02002548 if (di->d_fieldmask & QC_INO_SOFT)
Christoph Hellwigc472b432010-05-06 17:05:17 -04002549 dm->dqb_isoftlimit = di->d_ino_softlimit;
Jan Kara14bf61f2014-10-09 16:03:13 +02002550 if (di->d_fieldmask & QC_INO_HARD)
Christoph Hellwigc472b432010-05-06 17:05:17 -04002551 dm->dqb_ihardlimit = di->d_ino_hardlimit;
Jan Kara14bf61f2014-10-09 16:03:13 +02002552 if (di->d_fieldmask & (QC_INO_SOFT | QC_INO_HARD)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002553 check_ilim = 1;
Andrew Perepechko08261672010-04-12 22:16:50 +04002554 set_bit(DQ_LASTSET_B + QIF_ILIMITS_B, &dquot->dq_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002555 }
Christoph Hellwigc472b432010-05-06 17:05:17 -04002556
Jan Kara14bf61f2014-10-09 16:03:13 +02002557 if (di->d_fieldmask & QC_SPC_TIMER) {
2558 dm->dqb_btime = di->d_spc_timer;
Jan Karae04a88a92009-01-07 18:07:29 -08002559 check_blim = 1;
Andrew Perepechko08261672010-04-12 22:16:50 +04002560 set_bit(DQ_LASTSET_B + QIF_BTIME_B, &dquot->dq_flags);
Jan Kara4d59bce2008-10-02 16:48:10 +02002561 }
Christoph Hellwigc472b432010-05-06 17:05:17 -04002562
Jan Kara14bf61f2014-10-09 16:03:13 +02002563 if (di->d_fieldmask & QC_INO_TIMER) {
2564 dm->dqb_itime = di->d_ino_timer;
Jan Karae04a88a92009-01-07 18:07:29 -08002565 check_ilim = 1;
Andrew Perepechko08261672010-04-12 22:16:50 +04002566 set_bit(DQ_LASTSET_B + QIF_ITIME_B, &dquot->dq_flags);
Jan Kara4d59bce2008-10-02 16:48:10 +02002567 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002568
2569 if (check_blim) {
Jan Kara268157b2009-01-27 15:47:22 +01002570 if (!dm->dqb_bsoftlimit ||
2571 dm->dqb_curspace < dm->dqb_bsoftlimit) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002572 dm->dqb_btime = 0;
2573 clear_bit(DQ_BLKS_B, &dquot->dq_flags);
Jan Kara14bf61f2014-10-09 16:03:13 +02002574 } else if (!(di->d_fieldmask & QC_SPC_TIMER))
Jan Kara268157b2009-01-27 15:47:22 +01002575 /* Set grace only if user hasn't provided his own... */
Andrew Perepechko338bf9a2008-04-28 02:14:31 -07002576 dm->dqb_btime = get_seconds() + dqi->dqi_bgrace;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002577 }
2578 if (check_ilim) {
Jan Kara268157b2009-01-27 15:47:22 +01002579 if (!dm->dqb_isoftlimit ||
2580 dm->dqb_curinodes < dm->dqb_isoftlimit) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002581 dm->dqb_itime = 0;
2582 clear_bit(DQ_INODES_B, &dquot->dq_flags);
Jan Kara14bf61f2014-10-09 16:03:13 +02002583 } else if (!(di->d_fieldmask & QC_INO_TIMER))
Jan Kara268157b2009-01-27 15:47:22 +01002584 /* Set grace only if user hasn't provided his own... */
Andrew Perepechko338bf9a2008-04-28 02:14:31 -07002585 dm->dqb_itime = get_seconds() + dqi->dqi_igrace;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002586 }
Jan Kara268157b2009-01-27 15:47:22 +01002587 if (dm->dqb_bhardlimit || dm->dqb_bsoftlimit || dm->dqb_ihardlimit ||
2588 dm->dqb_isoftlimit)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002589 clear_bit(DQ_FAKE_B, &dquot->dq_flags);
2590 else
2591 set_bit(DQ_FAKE_B, &dquot->dq_flags);
2592 spin_unlock(&dq_data_lock);
2593 mark_dquot_dirty(dquot);
Andrew Perepechko338bf9a2008-04-28 02:14:31 -07002594
2595 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002596}
2597
Eric W. Biederman74a8a102012-09-16 02:07:49 -07002598int dquot_set_dqblk(struct super_block *sb, struct kqid qid,
Jan Kara14bf61f2014-10-09 16:03:13 +02002599 struct qc_dqblk *di)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002600{
2601 struct dquot *dquot;
Andrew Perepechko338bf9a2008-04-28 02:14:31 -07002602 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002603
Eric W. Biedermanaca645a2012-09-16 03:11:50 -07002604 dquot = dqget(sb, qid);
Jan Karaf55abc02008-08-20 17:50:32 +02002605 if (!dquot) {
2606 rc = -ESRCH;
2607 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002608 }
Andrew Perepechko338bf9a2008-04-28 02:14:31 -07002609 rc = do_set_dqblk(dquot, di);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002610 dqput(dquot);
Jan Karaf55abc02008-08-20 17:50:32 +02002611out:
Andrew Perepechko338bf9a2008-04-28 02:14:31 -07002612 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002613}
Christoph Hellwig287a8092010-05-19 07:16:45 -04002614EXPORT_SYMBOL(dquot_set_dqblk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002615
2616/* Generic routine for getting common part of quota file information */
Christoph Hellwig287a8092010-05-19 07:16:45 -04002617int dquot_get_dqinfo(struct super_block *sb, int type, struct if_dqinfo *ii)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002618{
2619 struct mem_dqinfo *mi;
2620
Ingo Molnard3be9152006-03-23 03:00:29 -08002621 mutex_lock(&sb_dqopt(sb)->dqonoff_mutex);
Jan Karaf55abc02008-08-20 17:50:32 +02002622 if (!sb_has_quota_active(sb, type)) {
Ingo Molnard3be9152006-03-23 03:00:29 -08002623 mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002624 return -ESRCH;
2625 }
2626 mi = sb_dqopt(sb)->info + type;
2627 spin_lock(&dq_data_lock);
2628 ii->dqi_bgrace = mi->dqi_bgrace;
2629 ii->dqi_igrace = mi->dqi_igrace;
Jan Kara46fe44c2011-11-16 15:03:59 +01002630 ii->dqi_flags = mi->dqi_flags & DQF_GETINFO_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002631 ii->dqi_valid = IIF_ALL;
2632 spin_unlock(&dq_data_lock);
Ingo Molnard3be9152006-03-23 03:00:29 -08002633 mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002634 return 0;
2635}
Christoph Hellwig287a8092010-05-19 07:16:45 -04002636EXPORT_SYMBOL(dquot_get_dqinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002637
2638/* Generic routine for setting common part of quota file information */
Christoph Hellwig287a8092010-05-19 07:16:45 -04002639int dquot_set_dqinfo(struct super_block *sb, int type, struct if_dqinfo *ii)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002640{
2641 struct mem_dqinfo *mi;
Jan Karaf55abc02008-08-20 17:50:32 +02002642 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002643
Ingo Molnard3be9152006-03-23 03:00:29 -08002644 mutex_lock(&sb_dqopt(sb)->dqonoff_mutex);
Jan Karaf55abc02008-08-20 17:50:32 +02002645 if (!sb_has_quota_active(sb, type)) {
2646 err = -ESRCH;
2647 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002648 }
2649 mi = sb_dqopt(sb)->info + type;
Jan Karaca6cb0912014-11-19 09:32:39 +01002650 if (ii->dqi_valid & IIF_FLAGS) {
2651 if (ii->dqi_flags & ~DQF_SETINFO_MASK ||
2652 (ii->dqi_flags & DQF_ROOT_SQUASH &&
2653 mi->dqi_format->qf_fmt_id != QFMT_VFS_OLD)) {
2654 err = -EINVAL;
2655 goto out;
2656 }
2657 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002658 spin_lock(&dq_data_lock);
2659 if (ii->dqi_valid & IIF_BGRACE)
2660 mi->dqi_bgrace = ii->dqi_bgrace;
2661 if (ii->dqi_valid & IIF_IGRACE)
2662 mi->dqi_igrace = ii->dqi_igrace;
2663 if (ii->dqi_valid & IIF_FLAGS)
Jan Kara46fe44c2011-11-16 15:03:59 +01002664 mi->dqi_flags = (mi->dqi_flags & ~DQF_SETINFO_MASK) |
2665 (ii->dqi_flags & DQF_SETINFO_MASK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002666 spin_unlock(&dq_data_lock);
2667 mark_info_dirty(sb, type);
2668 /* Force write to disk */
2669 sb->dq_op->write_info(sb, type);
Jan Karaf55abc02008-08-20 17:50:32 +02002670out:
Ingo Molnard3be9152006-03-23 03:00:29 -08002671 mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
Jan Karaf55abc02008-08-20 17:50:32 +02002672 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002673}
Christoph Hellwig287a8092010-05-19 07:16:45 -04002674EXPORT_SYMBOL(dquot_set_dqinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002675
Christoph Hellwig287a8092010-05-19 07:16:45 -04002676const struct quotactl_ops dquot_quotactl_ops = {
2677 .quota_on = dquot_quota_on,
2678 .quota_off = dquot_quota_off,
2679 .quota_sync = dquot_quota_sync,
2680 .get_info = dquot_get_dqinfo,
2681 .set_info = dquot_set_dqinfo,
2682 .get_dqblk = dquot_get_dqblk,
2683 .set_dqblk = dquot_set_dqblk
Linus Torvalds1da177e2005-04-16 15:20:36 -07002684};
Christoph Hellwig287a8092010-05-19 07:16:45 -04002685EXPORT_SYMBOL(dquot_quotactl_ops);
Dmitry Monakhovdde95882010-04-26 20:03:33 +04002686
Jan Kara3e2af672014-10-06 18:40:51 +02002687const struct quotactl_ops dquot_quotactl_sysfile_ops = {
2688 .quota_enable = dquot_quota_enable,
2689 .quota_disable = dquot_quota_disable,
2690 .quota_sync = dquot_quota_sync,
2691 .get_info = dquot_get_dqinfo,
2692 .set_info = dquot_set_dqinfo,
2693 .get_dqblk = dquot_get_dqblk,
2694 .set_dqblk = dquot_set_dqblk
2695};
2696EXPORT_SYMBOL(dquot_quotactl_sysfile_ops);
2697
Dmitry Monakhovdde95882010-04-26 20:03:33 +04002698static int do_proc_dqstats(struct ctl_table *table, int write,
2699 void __user *buffer, size_t *lenp, loff_t *ppos)
2700{
Dmitry Monakhovdde95882010-04-26 20:03:33 +04002701 unsigned int type = (int *)table->data - dqstats.stat;
Dmitry Monakhovf32764b2010-05-26 23:21:58 +02002702
2703 /* Update global table */
2704 dqstats.stat[type] =
2705 percpu_counter_sum_positive(&dqstats.counter[type]);
Dmitry Monakhovdde95882010-04-26 20:03:33 +04002706 return proc_dointvec(table, write, buffer, lenp, ppos);
2707}
2708
Joe Perchese6287532013-06-13 19:37:49 -07002709static struct ctl_table fs_dqstats_table[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002710 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002711 .procname = "lookups",
Dmitry Monakhovdde95882010-04-26 20:03:33 +04002712 .data = &dqstats.stat[DQST_LOOKUPS],
Linus Torvalds1da177e2005-04-16 15:20:36 -07002713 .maxlen = sizeof(int),
2714 .mode = 0444,
Dmitry Monakhovdde95882010-04-26 20:03:33 +04002715 .proc_handler = do_proc_dqstats,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002716 },
2717 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002718 .procname = "drops",
Dmitry Monakhovdde95882010-04-26 20:03:33 +04002719 .data = &dqstats.stat[DQST_DROPS],
Linus Torvalds1da177e2005-04-16 15:20:36 -07002720 .maxlen = sizeof(int),
2721 .mode = 0444,
Dmitry Monakhovdde95882010-04-26 20:03:33 +04002722 .proc_handler = do_proc_dqstats,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002723 },
2724 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002725 .procname = "reads",
Dmitry Monakhovdde95882010-04-26 20:03:33 +04002726 .data = &dqstats.stat[DQST_READS],
Linus Torvalds1da177e2005-04-16 15:20:36 -07002727 .maxlen = sizeof(int),
2728 .mode = 0444,
Dmitry Monakhovdde95882010-04-26 20:03:33 +04002729 .proc_handler = do_proc_dqstats,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002730 },
2731 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002732 .procname = "writes",
Dmitry Monakhovdde95882010-04-26 20:03:33 +04002733 .data = &dqstats.stat[DQST_WRITES],
Linus Torvalds1da177e2005-04-16 15:20:36 -07002734 .maxlen = sizeof(int),
2735 .mode = 0444,
Dmitry Monakhovdde95882010-04-26 20:03:33 +04002736 .proc_handler = do_proc_dqstats,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002737 },
2738 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002739 .procname = "cache_hits",
Dmitry Monakhovdde95882010-04-26 20:03:33 +04002740 .data = &dqstats.stat[DQST_CACHE_HITS],
Linus Torvalds1da177e2005-04-16 15:20:36 -07002741 .maxlen = sizeof(int),
2742 .mode = 0444,
Dmitry Monakhovdde95882010-04-26 20:03:33 +04002743 .proc_handler = do_proc_dqstats,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002744 },
2745 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002746 .procname = "allocated_dquots",
Dmitry Monakhovdde95882010-04-26 20:03:33 +04002747 .data = &dqstats.stat[DQST_ALLOC_DQUOTS],
Linus Torvalds1da177e2005-04-16 15:20:36 -07002748 .maxlen = sizeof(int),
2749 .mode = 0444,
Dmitry Monakhovdde95882010-04-26 20:03:33 +04002750 .proc_handler = do_proc_dqstats,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002751 },
2752 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002753 .procname = "free_dquots",
Dmitry Monakhovdde95882010-04-26 20:03:33 +04002754 .data = &dqstats.stat[DQST_FREE_DQUOTS],
Linus Torvalds1da177e2005-04-16 15:20:36 -07002755 .maxlen = sizeof(int),
2756 .mode = 0444,
Dmitry Monakhovdde95882010-04-26 20:03:33 +04002757 .proc_handler = do_proc_dqstats,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002758 },
2759 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002760 .procname = "syncs",
Dmitry Monakhovdde95882010-04-26 20:03:33 +04002761 .data = &dqstats.stat[DQST_SYNCS],
Linus Torvalds1da177e2005-04-16 15:20:36 -07002762 .maxlen = sizeof(int),
2763 .mode = 0444,
Dmitry Monakhovdde95882010-04-26 20:03:33 +04002764 .proc_handler = do_proc_dqstats,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002765 },
Jan Kara8e893462007-10-16 23:29:31 -07002766#ifdef CONFIG_PRINT_QUOTA_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07002767 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002768 .procname = "warnings",
2769 .data = &flag_print_warnings,
2770 .maxlen = sizeof(int),
2771 .mode = 0644,
Eric W. Biederman6d456112009-11-16 03:11:48 -08002772 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002773 },
Jan Kara8e893462007-10-16 23:29:31 -07002774#endif
Eric W. Biedermanab092032009-11-05 14:25:10 -08002775 { },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002776};
2777
Joe Perchese6287532013-06-13 19:37:49 -07002778static struct ctl_table fs_table[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002779 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002780 .procname = "quota",
2781 .mode = 0555,
2782 .child = fs_dqstats_table,
2783 },
Eric W. Biedermanab092032009-11-05 14:25:10 -08002784 { },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002785};
2786
Joe Perchese6287532013-06-13 19:37:49 -07002787static struct ctl_table sys_table[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002788 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002789 .procname = "fs",
2790 .mode = 0555,
2791 .child = fs_table,
2792 },
Eric W. Biedermanab092032009-11-05 14:25:10 -08002793 { },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002794};
2795
2796static int __init dquot_init(void)
2797{
Dmitry Monakhovf32764b2010-05-26 23:21:58 +02002798 int i, ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002799 unsigned long nr_hash, order;
2800
2801 printk(KERN_NOTICE "VFS: Disk quotas %s\n", __DQUOT_VERSION__);
2802
Eric W. Biederman0b4d4142007-02-14 00:34:09 -08002803 register_sysctl_table(sys_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002804
Paul Mundt20c2df82007-07-20 10:11:58 +09002805 dquot_cachep = kmem_cache_create("dquot",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002806 sizeof(struct dquot), sizeof(unsigned long) * 4,
Paul Jacksonfffb60f2006-03-24 03:16:06 -08002807 (SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT|
2808 SLAB_MEM_SPREAD|SLAB_PANIC),
Paul Mundt20c2df82007-07-20 10:11:58 +09002809 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002810
2811 order = 0;
2812 dquot_hash = (struct hlist_head *)__get_free_pages(GFP_ATOMIC, order);
2813 if (!dquot_hash)
2814 panic("Cannot create dquot hash table");
2815
Dmitry Monakhovf32764b2010-05-26 23:21:58 +02002816 for (i = 0; i < _DQST_DQSTAT_LAST; i++) {
Tejun Heo908c7f12014-09-08 09:51:29 +09002817 ret = percpu_counter_init(&dqstats.counter[i], 0, GFP_KERNEL);
Dmitry Monakhovf32764b2010-05-26 23:21:58 +02002818 if (ret)
2819 panic("Cannot create dquot stat counters");
2820 }
Dmitry Monakhovdde95882010-04-26 20:03:33 +04002821
Linus Torvalds1da177e2005-04-16 15:20:36 -07002822 /* Find power-of-two hlist_heads which can fit into allocation */
2823 nr_hash = (1UL << order) * PAGE_SIZE / sizeof(struct hlist_head);
2824 dq_hash_bits = 0;
2825 do {
2826 dq_hash_bits++;
2827 } while (nr_hash >> dq_hash_bits);
2828 dq_hash_bits--;
2829
2830 nr_hash = 1UL << dq_hash_bits;
2831 dq_hash_mask = nr_hash - 1;
2832 for (i = 0; i < nr_hash; i++)
2833 INIT_HLIST_HEAD(dquot_hash + i);
2834
Anton Blanchard19858e72014-11-04 12:05:01 +11002835 pr_info("VFS: Dquot-cache hash table entries: %ld (order %ld,"
2836 " %ld bytes)\n", nr_hash, order, (PAGE_SIZE << order));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002837
Rusty Russell8e1f9362007-07-17 04:03:17 -07002838 register_shrinker(&dqcache_shrinker);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002839
Linus Torvalds1da177e2005-04-16 15:20:36 -07002840 return 0;
2841}
2842module_init(dquot_init);