blob: 0f3c5d38da1fb299b3ad03da4469635c375bf0ae [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Copyright (c) 1982, 1986 Regents of the University of California.
3 * All rights reserved.
4 *
5 * This code is derived from software contributed to Berkeley by
6 * Robert Elz at The University of Melbourne.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
Linus Torvalds1da177e2005-04-16 15:20:36 -070031 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#ifndef _LINUX_QUOTA_
33#define _LINUX_QUOTA_
34
Matthew Wilcox8b91de22008-02-26 09:53:20 -050035#include <linux/list.h>
David Woodhouse0409d3a2006-04-25 14:52:13 +010036#include <linux/mutex.h>
Matthew Wilcox8b91de22008-02-26 09:53:20 -050037#include <linux/rwsem.h>
38#include <linux/spinlock.h>
39#include <linux/wait.h>
Dmitry Monakhovf32764b2010-05-26 23:21:58 +020040#include <linux/percpu_counter.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
42#include <linux/dqblk_xfs.h>
43#include <linux/dqblk_v1.h>
44#include <linux/dqblk_v2.h>
45
Arun Sharma600634972011-07-26 16:09:06 -070046#include <linux/atomic.h>
Eric W. Biedermane8a3e472012-09-16 01:11:45 -070047#include <linux/uidgid.h>
48#include <linux/projid.h>
David Howells607ca462012-10-13 10:46:48 +010049#include <uapi/linux/quota.h>
Eric W. Biedermane8a3e472012-09-16 01:11:45 -070050
51#undef USRQUOTA
52#undef GRPQUOTA
53enum quota_type {
54 USRQUOTA = 0, /* element used for user quotas */
55 GRPQUOTA = 1, /* element used for group quotas */
56 PRJQUOTA = 2, /* element used for project quotas */
57};
Matthew Wilcox8b91de22008-02-26 09:53:20 -050058
Jan Kara74abb982008-07-25 01:46:51 -070059typedef __kernel_uid32_t qid_t; /* Type in which we store ids in memory */
Jan Karadb49d2d2008-10-01 18:21:39 +020060typedef long long qsize_t; /* Type in which we store sizes */
Jan Kara74abb982008-07-25 01:46:51 -070061
Eric W. Biedermane8a3e472012-09-16 01:11:45 -070062struct kqid { /* Type in which we store the quota identifier */
63 union {
64 kuid_t uid;
65 kgid_t gid;
66 kprojid_t projid;
67 };
68 enum quota_type type; /* USRQUOTA (uid) or GRPQUOTA (gid) or PRJQUOTA (projid) */
69};
70
71extern bool qid_eq(struct kqid left, struct kqid right);
72extern bool qid_lt(struct kqid left, struct kqid right);
73extern qid_t from_kqid(struct user_namespace *to, struct kqid qid);
74extern qid_t from_kqid_munged(struct user_namespace *to, struct kqid qid);
75extern bool qid_valid(struct kqid qid);
76
77/**
78 * make_kqid - Map a user-namespace, type, qid tuple into a kqid.
79 * @from: User namespace that the qid is in
80 * @type: The type of quota
81 * @qid: Quota identifier
82 *
83 * Maps a user-namespace, type qid tuple into a kernel internal
84 * kqid, and returns that kqid.
85 *
86 * When there is no mapping defined for the user-namespace, type,
87 * qid tuple an invalid kqid is returned. Callers are expected to
88 * test for and handle handle invalid kqids being returned.
89 * Invalid kqids may be tested for using qid_valid().
90 */
91static inline struct kqid make_kqid(struct user_namespace *from,
92 enum quota_type type, qid_t qid)
93{
94 struct kqid kqid;
95
96 kqid.type = type;
97 switch (type) {
98 case USRQUOTA:
99 kqid.uid = make_kuid(from, qid);
100 break;
101 case GRPQUOTA:
102 kqid.gid = make_kgid(from, qid);
103 break;
104 case PRJQUOTA:
105 kqid.projid = make_kprojid(from, qid);
106 break;
107 default:
108 BUG();
109 }
110 return kqid;
111}
112
113/**
114 * make_kqid_invalid - Explicitly make an invalid kqid
115 * @type: The type of quota identifier
116 *
117 * Returns an invalid kqid with the specified type.
118 */
119static inline struct kqid make_kqid_invalid(enum quota_type type)
120{
121 struct kqid kqid;
122
123 kqid.type = type;
124 switch (type) {
125 case USRQUOTA:
126 kqid.uid = INVALID_UID;
127 break;
128 case GRPQUOTA:
129 kqid.gid = INVALID_GID;
130 break;
131 case PRJQUOTA:
132 kqid.projid = INVALID_PROJID;
133 break;
134 default:
135 BUG();
136 }
137 return kqid;
138}
139
140/**
141 * make_kqid_uid - Make a kqid from a kuid
142 * @uid: The kuid to make the quota identifier from
143 */
144static inline struct kqid make_kqid_uid(kuid_t uid)
145{
146 struct kqid kqid;
147 kqid.type = USRQUOTA;
148 kqid.uid = uid;
149 return kqid;
150}
151
152/**
153 * make_kqid_gid - Make a kqid from a kgid
154 * @gid: The kgid to make the quota identifier from
155 */
156static inline struct kqid make_kqid_gid(kgid_t gid)
157{
158 struct kqid kqid;
159 kqid.type = GRPQUOTA;
160 kqid.gid = gid;
161 return kqid;
162}
163
164/**
165 * make_kqid_projid - Make a kqid from a projid
166 * @projid: The kprojid to make the quota identifier from
167 */
168static inline struct kqid make_kqid_projid(kprojid_t projid)
169{
170 struct kqid kqid;
171 kqid.type = PRJQUOTA;
172 kqid.projid = projid;
173 return kqid;
174}
175
176
Mike Frysingeracd64b72007-05-08 00:31:51 -0700177extern spinlock_t dq_data_lock;
178
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179/* Maximal numbers of writes for quota operation (insert/delete/update)
Jan Kara4e5117b2005-06-23 22:01:03 -0700180 * (over VFS all formats) */
181#define DQUOT_INIT_ALLOC max(V1_INIT_ALLOC, V2_INIT_ALLOC)
182#define DQUOT_INIT_REWRITE max(V1_INIT_REWRITE, V2_INIT_REWRITE)
183#define DQUOT_DEL_ALLOC max(V1_DEL_ALLOC, V2_DEL_ALLOC)
184#define DQUOT_DEL_REWRITE max(V1_DEL_REWRITE, V2_DEL_REWRITE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185
186/*
187 * Data for one user/group kept in memory
188 */
189struct mem_dqblk {
Jan Kara12095462008-08-20 14:45:12 +0200190 qsize_t dqb_bhardlimit; /* absolute limit on disk blks alloc */
191 qsize_t dqb_bsoftlimit; /* preferred limit on disk blks */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 qsize_t dqb_curspace; /* current used space */
Mingming Caof18df222009-01-13 16:43:09 +0100193 qsize_t dqb_rsvspace; /* current reserved space for delalloc*/
Jan Kara12095462008-08-20 14:45:12 +0200194 qsize_t dqb_ihardlimit; /* absolute limit on allocated inodes */
195 qsize_t dqb_isoftlimit; /* preferred inode limit */
196 qsize_t dqb_curinodes; /* current # allocated inodes */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 time_t dqb_btime; /* time limit for excessive disk use */
198 time_t dqb_itime; /* time limit for excessive inode use */
199};
200
201/*
202 * Data for one quotafile kept in memory
203 */
204struct quota_format_type;
205
206struct mem_dqinfo {
207 struct quota_format_type *dqi_format;
Jan Kara0ff5af82008-04-28 02:14:33 -0700208 int dqi_fmt_id; /* Id of the dqi_format - used when turning
209 * quotas on after remount RW */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 struct list_head dqi_dirty_list; /* List of dirty dquots */
211 unsigned long dqi_flags;
212 unsigned int dqi_bgrace;
213 unsigned int dqi_igrace;
Andrew Perepechko338bf9a2008-04-28 02:14:31 -0700214 qsize_t dqi_maxblimit;
215 qsize_t dqi_maxilimit;
Jan Karae3d4d562008-10-02 18:44:14 +0200216 void *dqi_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217};
218
219struct super_block;
220
221#define DQF_MASK 0xffff /* Mask for format specific flags */
Jan Kara46fe44c2011-11-16 15:03:59 +0100222#define DQF_GETINFO_MASK 0x1ffff /* Mask for flags passed to userspace */
223#define DQF_SETINFO_MASK 0xffff /* Mask for flags modifiable from userspace */
224#define DQF_SYS_FILE_B 16
225#define DQF_SYS_FILE (1 << DQF_SYS_FILE_B) /* Quota file stored as system file */
226#define DQF_INFO_DIRTY_B 31
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227#define DQF_INFO_DIRTY (1 << DQF_INFO_DIRTY_B) /* Is info dirty? */
228
229extern void mark_info_dirty(struct super_block *sb, int type);
Jan Kara03b06342008-07-25 01:46:52 -0700230static inline int info_dirty(struct mem_dqinfo *info)
231{
232 return test_bit(DQF_INFO_DIRTY_B, &info->dqi_flags);
233}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234
Dmitry Monakhovdde95882010-04-26 20:03:33 +0400235enum {
236 DQST_LOOKUPS,
237 DQST_DROPS,
238 DQST_READS,
239 DQST_WRITES,
240 DQST_CACHE_HITS,
241 DQST_ALLOC_DQUOTS,
242 DQST_FREE_DQUOTS,
243 DQST_SYNCS,
244 _DQST_DQSTAT_LAST
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245};
246
Dmitry Monakhovdde95882010-04-26 20:03:33 +0400247struct dqstats {
248 int stat[_DQST_DQSTAT_LAST];
Dmitry Monakhovf32764b2010-05-26 23:21:58 +0200249 struct percpu_counter counter[_DQST_DQSTAT_LAST];
Dmitry Monakhovdde95882010-04-26 20:03:33 +0400250};
251
252extern struct dqstats *dqstats_pcpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253extern struct dqstats dqstats;
254
Dmitry Monakhovdde95882010-04-26 20:03:33 +0400255static inline void dqstats_inc(unsigned int type)
256{
Dmitry Monakhovf32764b2010-05-26 23:21:58 +0200257 percpu_counter_inc(&dqstats.counter[type]);
Dmitry Monakhovdde95882010-04-26 20:03:33 +0400258}
259
260static inline void dqstats_dec(unsigned int type)
261{
Dmitry Monakhovf32764b2010-05-26 23:21:58 +0200262 percpu_counter_dec(&dqstats.counter[type]);
Dmitry Monakhovdde95882010-04-26 20:03:33 +0400263}
264
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265#define DQ_MOD_B 0 /* dquot modified since read */
266#define DQ_BLKS_B 1 /* uid/gid has been warned about blk limit */
267#define DQ_INODES_B 2 /* uid/gid has been warned about inode limit */
268#define DQ_FAKE_B 3 /* no limits only usage */
269#define DQ_READ_B 4 /* dquot was read into memory */
270#define DQ_ACTIVE_B 5 /* dquot is active (dquot_release not called) */
Jan Kara4d59bce2008-10-02 16:48:10 +0200271#define DQ_LASTSET_B 6 /* Following 6 bits (see QIF_) are reserved\
272 * for the mask of entries set via SETQUOTA\
273 * quotactl. They are set under dq_data_lock\
274 * and the quota format handling dquot can\
275 * clear them when it sees fit. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276
277struct dquot {
278 struct hlist_node dq_hash; /* Hash list in memory */
279 struct list_head dq_inuse; /* List of all quotas */
280 struct list_head dq_free; /* Free list element */
281 struct list_head dq_dirty; /* List of dirty dquots */
Ingo Molnard3be9152006-03-23 03:00:29 -0800282 struct mutex dq_lock; /* dquot IO lock */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 atomic_t dq_count; /* Use count */
284 wait_queue_head_t dq_wait_unused; /* Wait queue for dquot to become unused */
285 struct super_block *dq_sb; /* superblock this applies to */
Eric W. Biederman4c376dc2012-09-16 03:56:19 -0700286 struct kqid dq_id; /* ID this applies to (uid, gid, projid) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 loff_t dq_off; /* Offset of dquot on disk */
288 unsigned long dq_flags; /* See DQ_* */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 struct mem_dqblk dq_dqb; /* Diskquota usage */
290};
291
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292/* Operations which must be implemented by each quota format */
293struct quota_format_ops {
294 int (*check_quota_file)(struct super_block *sb, int type); /* Detect whether file is in our format */
295 int (*read_file_info)(struct super_block *sb, int type); /* Read main info about file - called on quotaon() */
296 int (*write_file_info)(struct super_block *sb, int type); /* Write main info about file */
297 int (*free_file_info)(struct super_block *sb, int type); /* Called on quotaoff() */
298 int (*read_dqblk)(struct dquot *dquot); /* Read structure for one user */
299 int (*commit_dqblk)(struct dquot *dquot); /* Write structure for one user */
300 int (*release_dqblk)(struct dquot *dquot); /* Called when last reference to dquot is being dropped */
301};
302
303/* Operations working with dquots */
304struct dquot_operations {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 int (*write_dquot) (struct dquot *); /* Ordinary dquot write */
Jan Kara74f783a2008-08-19 14:51:22 +0200306 struct dquot *(*alloc_dquot)(struct super_block *, int); /* Allocate memory for new dquot */
307 void (*destroy_dquot)(struct dquot *); /* Free memory for dquot */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 int (*acquire_dquot) (struct dquot *); /* Quota is going to be created on disk */
309 int (*release_dquot) (struct dquot *); /* Quota is going to be deleted from disk */
310 int (*mark_dirty) (struct dquot *); /* Dquot is marked dirty */
311 int (*write_info) (struct super_block *, int); /* Write of quota "superblock" */
Dmitry Monakhovfd8fbfc2009-12-14 15:21:13 +0300312 /* get reserved quota for delayed alloc, value returned is managed by
313 * quota code only */
314 qsize_t *(*get_reserved_space) (struct inode *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315};
316
Jan Karaf00c9e42010-09-15 17:38:58 +0200317struct path;
318
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319/* Operations handling requests from userspace */
320struct quotactl_ops {
Jan Karaf00c9e42010-09-15 17:38:58 +0200321 int (*quota_on)(struct super_block *, int, int, struct path *);
322 int (*quota_on_meta)(struct super_block *, int, int);
Christoph Hellwig307ae182010-05-19 07:16:43 -0400323 int (*quota_off)(struct super_block *, int);
Jan Karaceed1722012-07-03 16:45:28 +0200324 int (*quota_sync)(struct super_block *, int);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 int (*get_info)(struct super_block *, int, struct if_dqinfo *);
326 int (*set_info)(struct super_block *, int, struct if_dqinfo *);
Eric W. Biederman74a8a102012-09-16 02:07:49 -0700327 int (*get_dqblk)(struct super_block *, struct kqid, struct fs_disk_quota *);
328 int (*set_dqblk)(struct super_block *, struct kqid, struct fs_disk_quota *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 int (*get_xstate)(struct super_block *, struct fs_quota_stat *);
330 int (*set_xstate)(struct super_block *, unsigned int, int);
Chandra Seetharamanaf30cb42013-08-06 17:27:07 -0500331 int (*get_xstatev)(struct super_block *, struct fs_quota_statv *);
Eric Sandeen9da93f92014-05-05 17:25:50 +1000332 int (*rm_xquota)(struct super_block *, unsigned int);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333};
334
335struct quota_format_type {
336 int qf_fmt_id; /* Quota format id */
Alexey Dobriyan1472da52009-10-16 15:26:03 +0400337 const struct quota_format_ops *qf_ops; /* Operations of format */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 struct module *qf_owner; /* Module implementing quota format */
339 struct quota_format_type *qf_next;
340};
341
Jan Karaf55abc02008-08-20 17:50:32 +0200342/* Quota state flags - they actually come in two flavors - for users and groups */
343enum {
344 _DQUOT_USAGE_ENABLED = 0, /* Track disk usage for users */
345 _DQUOT_LIMITS_ENABLED, /* Enforce quota limits for users */
346 _DQUOT_SUSPENDED, /* User diskquotas are off, but
Jan Kara0ff5af82008-04-28 02:14:33 -0700347 * we have necessary info in
348 * memory to turn them on */
Jan Karaf55abc02008-08-20 17:50:32 +0200349 _DQUOT_STATE_FLAGS
350};
351#define DQUOT_USAGE_ENABLED (1 << _DQUOT_USAGE_ENABLED)
352#define DQUOT_LIMITS_ENABLED (1 << _DQUOT_LIMITS_ENABLED)
353#define DQUOT_SUSPENDED (1 << _DQUOT_SUSPENDED)
354#define DQUOT_STATE_FLAGS (DQUOT_USAGE_ENABLED | DQUOT_LIMITS_ENABLED | \
355 DQUOT_SUSPENDED)
Jan Karaca785ec2008-09-30 17:53:37 +0200356/* Other quota flags */
Dmitry Monakhovad1e6e82010-02-16 08:31:49 +0300357#define DQUOT_STATE_LAST (_DQUOT_STATE_FLAGS * MAXQUOTAS)
358#define DQUOT_QUOTA_SYS_FILE (1 << DQUOT_STATE_LAST)
359 /* Quota file is a special
Jan Karaca785ec2008-09-30 17:53:37 +0200360 * system file and user cannot
361 * touch it. Filesystem is
362 * responsible for setting
363 * S_NOQUOTA, S_NOATIME flags
364 */
Dmitry Monakhovad1e6e82010-02-16 08:31:49 +0300365#define DQUOT_NEGATIVE_USAGE (1 << (DQUOT_STATE_LAST + 1))
366 /* Allow negative quota usage */
Jan Karaf55abc02008-08-20 17:50:32 +0200367
368static inline unsigned int dquot_state_flag(unsigned int flags, int type)
369{
Dmitry Monakhovad1e6e82010-02-16 08:31:49 +0300370 return flags << _DQUOT_STATE_FLAGS * type;
Jan Karaf55abc02008-08-20 17:50:32 +0200371}
372
373static inline unsigned int dquot_generic_flag(unsigned int flags, int type)
374{
Dmitry Monakhovad1e6e82010-02-16 08:31:49 +0300375 return (flags >> _DQUOT_STATE_FLAGS * type) & DQUOT_STATE_FLAGS;
Jan Karaf55abc02008-08-20 17:50:32 +0200376}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377
Steven Whitehouse86e931a2009-09-28 12:35:17 +0100378#ifdef CONFIG_QUOTA_NETLINK_INTERFACE
Eric W. Biederman431f1972012-09-16 02:32:43 -0700379extern void quota_send_warning(struct kqid qid, dev_t dev,
Steven Whitehouse86e931a2009-09-28 12:35:17 +0100380 const char warntype);
381#else
Eric W. Biederman431f1972012-09-16 02:32:43 -0700382static inline void quota_send_warning(struct kqid qid, dev_t dev,
Steven Whitehouse86e931a2009-09-28 12:35:17 +0100383 const char warntype)
384{
385 return;
386}
387#endif /* CONFIG_QUOTA_NETLINK_INTERFACE */
388
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389struct quota_info {
390 unsigned int flags; /* Flags for diskquotas on this device */
Ingo Molnard3be9152006-03-23 03:00:29 -0800391 struct mutex dqio_mutex; /* lock device while I/O in progress */
392 struct mutex dqonoff_mutex; /* Serialize quotaon & quotaoff */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 struct rw_semaphore dqptr_sem; /* serialize ops using quota_info struct, pointers from inode to dquots */
394 struct inode *files[MAXQUOTAS]; /* inodes of quotafiles */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 struct mem_dqinfo info[MAXQUOTAS]; /* Information for each quota type */
Alexey Dobriyan1472da52009-10-16 15:26:03 +0400396 const struct quota_format_ops *ops[MAXQUOTAS]; /* Operations for each type */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397};
398
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399int register_quota_format(struct quota_format_type *fmt);
400void unregister_quota_format(struct quota_format_type *fmt);
401
402struct quota_module_name {
403 int qm_fmt_id;
404 char *qm_mod_name;
405};
406
407#define INIT_QUOTA_MODULE_NAMES {\
408 {QFMT_VFS_OLD, "quota_v1"},\
409 {QFMT_VFS_V0, "quota_v2"},\
Theodore Ts'oc3ad83d2013-01-24 23:24:56 -0500410 {QFMT_VFS_V1, "quota_v2"},\
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 {0, NULL}}
412
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413#endif /* _QUOTA_ */