1 /*
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.
31  */
32 
33 #ifndef _LINUX_QUOTA_
34 #define _LINUX_QUOTA_
35 
36 #include <linux/errno.h>
37 #include <linux/types.h>
38 
39 #define __DQUOT_VERSION__	"dquot_6.5.2"
40 
41 #define MAXQUOTAS 2
42 #define USRQUOTA  0		/* element used for user quotas */
43 #define GRPQUOTA  1		/* element used for group quotas */
44 
45 /*
46  * Definitions for the default names of the quotas files.
47  */
48 #define INITQFNAMES { \
49 	"user",    /* USRQUOTA */ \
50 	"group",   /* GRPQUOTA */ \
51 	"undefined", \
52 };
53 
54 /*
55  * Command definitions for the 'quotactl' system call.
56  * The commands are broken into a main command defined below
57  * and a subcommand that is used to convey the type of
58  * quota that is being manipulated (see above).
59  */
60 #define SUBCMDMASK  0x00ff
61 #define SUBCMDSHIFT 8
62 #define QCMD(cmd, type)  (((cmd) << SUBCMDSHIFT) | ((type) & SUBCMDMASK))
63 
64 #define Q_SYNC     0x800001	/* sync disk copy of a filesystems quotas */
65 #define Q_QUOTAON  0x800002	/* turn quotas on */
66 #define Q_QUOTAOFF 0x800003	/* turn quotas off */
67 #define Q_GETFMT   0x800004	/* get quota format used on given filesystem */
68 #define Q_GETINFO  0x800005	/* get information about quota files */
69 #define Q_SETINFO  0x800006	/* set information about quota files */
70 #define Q_GETQUOTA 0x800007	/* get user quota structure */
71 #define Q_SETQUOTA 0x800008	/* set user quota structure */
72 
73 /* Quota format type IDs */
74 #define	QFMT_VFS_OLD 1
75 #define	QFMT_VFS_V0 2
76 #define QFMT_OCFS2 3
77 #define	QFMT_VFS_V1 4
78 
79 /* Size of block in which space limits are passed through the quota
80  * interface */
81 #define QIF_DQBLKSIZE_BITS 10
82 #define QIF_DQBLKSIZE (1 << QIF_DQBLKSIZE_BITS)
83 
84 /*
85  * Quota structure used for communication with userspace via quotactl
86  * Following flags are used to specify which fields are valid
87  */
88 enum {
89 	QIF_BLIMITS_B = 0,
90 	QIF_SPACE_B,
91 	QIF_ILIMITS_B,
92 	QIF_INODES_B,
93 	QIF_BTIME_B,
94 	QIF_ITIME_B,
95 };
96 
97 #define QIF_BLIMITS	(1 << QIF_BLIMITS_B)
98 #define QIF_SPACE	(1 << QIF_SPACE_B)
99 #define QIF_ILIMITS	(1 << QIF_ILIMITS_B)
100 #define QIF_INODES	(1 << QIF_INODES_B)
101 #define QIF_BTIME	(1 << QIF_BTIME_B)
102 #define QIF_ITIME	(1 << QIF_ITIME_B)
103 #define QIF_LIMITS	(QIF_BLIMITS | QIF_ILIMITS)
104 #define QIF_USAGE	(QIF_SPACE | QIF_INODES)
105 #define QIF_TIMES	(QIF_BTIME | QIF_ITIME)
106 #define QIF_ALL		(QIF_LIMITS | QIF_USAGE | QIF_TIMES)
107 
108 struct if_dqblk {
109 	__u64 dqb_bhardlimit;
110 	__u64 dqb_bsoftlimit;
111 	__u64 dqb_curspace;
112 	__u64 dqb_ihardlimit;
113 	__u64 dqb_isoftlimit;
114 	__u64 dqb_curinodes;
115 	__u64 dqb_btime;
116 	__u64 dqb_itime;
117 	__u32 dqb_valid;
118 };
119 
120 /*
121  * Structure used for setting quota information about file via quotactl
122  * Following flags are used to specify which fields are valid
123  */
124 #define IIF_BGRACE	1
125 #define IIF_IGRACE	2
126 #define IIF_FLAGS	4
127 #define IIF_ALL		(IIF_BGRACE | IIF_IGRACE | IIF_FLAGS)
128 
129 struct if_dqinfo {
130 	__u64 dqi_bgrace;
131 	__u64 dqi_igrace;
132 	__u32 dqi_flags;
133 	__u32 dqi_valid;
134 };
135 
136 /*
137  * Definitions for quota netlink interface
138  */
139 #define QUOTA_NL_NOWARN 0
140 #define QUOTA_NL_IHARDWARN 1		/* Inode hardlimit reached */
141 #define QUOTA_NL_ISOFTLONGWARN 2 	/* Inode grace time expired */
142 #define QUOTA_NL_ISOFTWARN 3		/* Inode softlimit reached */
143 #define QUOTA_NL_BHARDWARN 4		/* Block hardlimit reached */
144 #define QUOTA_NL_BSOFTLONGWARN 5	/* Block grace time expired */
145 #define QUOTA_NL_BSOFTWARN 6		/* Block softlimit reached */
146 #define QUOTA_NL_IHARDBELOW 7		/* Usage got below inode hardlimit */
147 #define QUOTA_NL_ISOFTBELOW 8		/* Usage got below inode softlimit */
148 #define QUOTA_NL_BHARDBELOW 9		/* Usage got below block hardlimit */
149 #define QUOTA_NL_BSOFTBELOW 10		/* Usage got below block softlimit */
150 
151 enum {
152 	QUOTA_NL_C_UNSPEC,
153 	QUOTA_NL_C_WARNING,
154 	__QUOTA_NL_C_MAX,
155 };
156 #define QUOTA_NL_C_MAX (__QUOTA_NL_C_MAX - 1)
157 
158 enum {
159 	QUOTA_NL_A_UNSPEC,
160 	QUOTA_NL_A_QTYPE,
161 	QUOTA_NL_A_EXCESS_ID,
162 	QUOTA_NL_A_WARNING,
163 	QUOTA_NL_A_DEV_MAJOR,
164 	QUOTA_NL_A_DEV_MINOR,
165 	QUOTA_NL_A_CAUSED_ID,
166 	__QUOTA_NL_A_MAX,
167 };
168 #define QUOTA_NL_A_MAX (__QUOTA_NL_A_MAX - 1)
169 
170 
171 #ifdef __KERNEL__
172 #include <linux/list.h>
173 #include <linux/mutex.h>
174 #include <linux/rwsem.h>
175 #include <linux/spinlock.h>
176 #include <linux/wait.h>
177 #include <linux/percpu_counter.h>
178 
179 #include <linux/dqblk_xfs.h>
180 #include <linux/dqblk_v1.h>
181 #include <linux/dqblk_v2.h>
182 
183 #include <linux/atomic.h>
184 
185 typedef __kernel_uid32_t qid_t; /* Type in which we store ids in memory */
186 typedef long long qsize_t;	/* Type in which we store sizes */
187 
188 extern spinlock_t dq_data_lock;
189 
190 /* Maximal numbers of writes for quota operation (insert/delete/update)
191  * (over VFS all formats) */
192 #define DQUOT_INIT_ALLOC max(V1_INIT_ALLOC, V2_INIT_ALLOC)
193 #define DQUOT_INIT_REWRITE max(V1_INIT_REWRITE, V2_INIT_REWRITE)
194 #define DQUOT_DEL_ALLOC max(V1_DEL_ALLOC, V2_DEL_ALLOC)
195 #define DQUOT_DEL_REWRITE max(V1_DEL_REWRITE, V2_DEL_REWRITE)
196 
197 /*
198  * Data for one user/group kept in memory
199  */
200 struct mem_dqblk {
201 	qsize_t dqb_bhardlimit;	/* absolute limit on disk blks alloc */
202 	qsize_t dqb_bsoftlimit;	/* preferred limit on disk blks */
203 	qsize_t dqb_curspace;	/* current used space */
204 	qsize_t dqb_rsvspace;   /* current reserved space for delalloc*/
205 	qsize_t dqb_ihardlimit;	/* absolute limit on allocated inodes */
206 	qsize_t dqb_isoftlimit;	/* preferred inode limit */
207 	qsize_t dqb_curinodes;	/* current # allocated inodes */
208 	time_t dqb_btime;	/* time limit for excessive disk use */
209 	time_t dqb_itime;	/* time limit for excessive inode use */
210 };
211 
212 /*
213  * Data for one quotafile kept in memory
214  */
215 struct quota_format_type;
216 
217 struct mem_dqinfo {
218 	struct quota_format_type *dqi_format;
219 	int dqi_fmt_id;		/* Id of the dqi_format - used when turning
220 				 * quotas on after remount RW */
221 	struct list_head dqi_dirty_list;	/* List of dirty dquots */
222 	unsigned long dqi_flags;
223 	unsigned int dqi_bgrace;
224 	unsigned int dqi_igrace;
225 	qsize_t dqi_maxblimit;
226 	qsize_t dqi_maxilimit;
227 	void *dqi_priv;
228 };
229 
230 struct super_block;
231 
232 #define DQF_MASK 0xffff		/* Mask for format specific flags */
233 #define DQF_GETINFO_MASK 0x1ffff	/* Mask for flags passed to userspace */
234 #define DQF_SETINFO_MASK 0xffff		/* Mask for flags modifiable from userspace */
235 #define DQF_SYS_FILE_B		16
236 #define DQF_SYS_FILE (1 << DQF_SYS_FILE_B)	/* Quota file stored as system file */
237 #define DQF_INFO_DIRTY_B	31
238 #define DQF_INFO_DIRTY (1 << DQF_INFO_DIRTY_B)	/* Is info dirty? */
239 
240 extern void mark_info_dirty(struct super_block *sb, int type);
info_dirty(struct mem_dqinfo * info)241 static inline int info_dirty(struct mem_dqinfo *info)
242 {
243 	return test_bit(DQF_INFO_DIRTY_B, &info->dqi_flags);
244 }
245 
246 enum {
247 	DQST_LOOKUPS,
248 	DQST_DROPS,
249 	DQST_READS,
250 	DQST_WRITES,
251 	DQST_CACHE_HITS,
252 	DQST_ALLOC_DQUOTS,
253 	DQST_FREE_DQUOTS,
254 	DQST_SYNCS,
255 	_DQST_DQSTAT_LAST
256 };
257 
258 struct dqstats {
259 	int stat[_DQST_DQSTAT_LAST];
260 	struct percpu_counter counter[_DQST_DQSTAT_LAST];
261 };
262 
263 extern struct dqstats *dqstats_pcpu;
264 extern struct dqstats dqstats;
265 
dqstats_inc(unsigned int type)266 static inline void dqstats_inc(unsigned int type)
267 {
268 	percpu_counter_inc(&dqstats.counter[type]);
269 }
270 
dqstats_dec(unsigned int type)271 static inline void dqstats_dec(unsigned int type)
272 {
273 	percpu_counter_dec(&dqstats.counter[type]);
274 }
275 
276 #define DQ_MOD_B	0	/* dquot modified since read */
277 #define DQ_BLKS_B	1	/* uid/gid has been warned about blk limit */
278 #define DQ_INODES_B	2	/* uid/gid has been warned about inode limit */
279 #define DQ_FAKE_B	3	/* no limits only usage */
280 #define DQ_READ_B	4	/* dquot was read into memory */
281 #define DQ_ACTIVE_B	5	/* dquot is active (dquot_release not called) */
282 #define DQ_LASTSET_B	6	/* Following 6 bits (see QIF_) are reserved\
283 				 * for the mask of entries set via SETQUOTA\
284 				 * quotactl. They are set under dq_data_lock\
285 				 * and the quota format handling dquot can\
286 				 * clear them when it sees fit. */
287 
288 struct dquot {
289 	struct hlist_node dq_hash;	/* Hash list in memory */
290 	struct list_head dq_inuse;	/* List of all quotas */
291 	struct list_head dq_free;	/* Free list element */
292 	struct list_head dq_dirty;	/* List of dirty dquots */
293 	struct mutex dq_lock;		/* dquot IO lock */
294 	atomic_t dq_count;		/* Use count */
295 	wait_queue_head_t dq_wait_unused;	/* Wait queue for dquot to become unused */
296 	struct super_block *dq_sb;	/* superblock this applies to */
297 	unsigned int dq_id;		/* ID this applies to (uid, gid) */
298 	loff_t dq_off;			/* Offset of dquot on disk */
299 	unsigned long dq_flags;		/* See DQ_* */
300 	short dq_type;			/* Type of quota */
301 	struct mem_dqblk dq_dqb;	/* Diskquota usage */
302 };
303 
304 /* Operations which must be implemented by each quota format */
305 struct quota_format_ops {
306 	int (*check_quota_file)(struct super_block *sb, int type);	/* Detect whether file is in our format */
307 	int (*read_file_info)(struct super_block *sb, int type);	/* Read main info about file - called on quotaon() */
308 	int (*write_file_info)(struct super_block *sb, int type);	/* Write main info about file */
309 	int (*free_file_info)(struct super_block *sb, int type);	/* Called on quotaoff() */
310 	int (*read_dqblk)(struct dquot *dquot);		/* Read structure for one user */
311 	int (*commit_dqblk)(struct dquot *dquot);	/* Write structure for one user */
312 	int (*release_dqblk)(struct dquot *dquot);	/* Called when last reference to dquot is being dropped */
313 };
314 
315 /* Operations working with dquots */
316 struct dquot_operations {
317 	int (*write_dquot) (struct dquot *);		/* Ordinary dquot write */
318 	struct dquot *(*alloc_dquot)(struct super_block *, int);	/* Allocate memory for new dquot */
319 	void (*destroy_dquot)(struct dquot *);		/* Free memory for dquot */
320 	int (*acquire_dquot) (struct dquot *);		/* Quota is going to be created on disk */
321 	int (*release_dquot) (struct dquot *);		/* Quota is going to be deleted from disk */
322 	int (*mark_dirty) (struct dquot *);		/* Dquot is marked dirty */
323 	int (*write_info) (struct super_block *, int);	/* Write of quota "superblock" */
324 	/* get reserved quota for delayed alloc, value returned is managed by
325 	 * quota code only */
326 	qsize_t *(*get_reserved_space) (struct inode *);
327 };
328 
329 struct path;
330 
331 /* Operations handling requests from userspace */
332 struct quotactl_ops {
333 	int (*quota_on)(struct super_block *, int, int, struct path *);
334 	int (*quota_on_meta)(struct super_block *, int, int);
335 	int (*quota_off)(struct super_block *, int);
336 	int (*quota_sync)(struct super_block *, int, int);
337 	int (*get_info)(struct super_block *, int, struct if_dqinfo *);
338 	int (*set_info)(struct super_block *, int, struct if_dqinfo *);
339 	int (*get_dqblk)(struct super_block *, int, qid_t, struct fs_disk_quota *);
340 	int (*set_dqblk)(struct super_block *, int, qid_t, struct fs_disk_quota *);
341 	int (*get_xstate)(struct super_block *, struct fs_quota_stat *);
342 	int (*set_xstate)(struct super_block *, unsigned int, int);
343 };
344 
345 struct quota_format_type {
346 	int qf_fmt_id;	/* Quota format id */
347 	const struct quota_format_ops *qf_ops;	/* Operations of format */
348 	struct module *qf_owner;		/* Module implementing quota format */
349 	struct quota_format_type *qf_next;
350 };
351 
352 /* Quota state flags - they actually come in two flavors - for users and groups */
353 enum {
354 	_DQUOT_USAGE_ENABLED = 0,		/* Track disk usage for users */
355 	_DQUOT_LIMITS_ENABLED,			/* Enforce quota limits for users */
356 	_DQUOT_SUSPENDED,			/* User diskquotas are off, but
357 						 * we have necessary info in
358 						 * memory to turn them on */
359 	_DQUOT_STATE_FLAGS
360 };
361 #define DQUOT_USAGE_ENABLED	(1 << _DQUOT_USAGE_ENABLED)
362 #define DQUOT_LIMITS_ENABLED	(1 << _DQUOT_LIMITS_ENABLED)
363 #define DQUOT_SUSPENDED		(1 << _DQUOT_SUSPENDED)
364 #define DQUOT_STATE_FLAGS	(DQUOT_USAGE_ENABLED | DQUOT_LIMITS_ENABLED | \
365 				 DQUOT_SUSPENDED)
366 /* Other quota flags */
367 #define DQUOT_STATE_LAST	(_DQUOT_STATE_FLAGS * MAXQUOTAS)
368 #define DQUOT_QUOTA_SYS_FILE	(1 << DQUOT_STATE_LAST)
369 						/* Quota file is a special
370 						 * system file and user cannot
371 						 * touch it. Filesystem is
372 						 * responsible for setting
373 						 * S_NOQUOTA, S_NOATIME flags
374 						 */
375 #define DQUOT_NEGATIVE_USAGE	(1 << (DQUOT_STATE_LAST + 1))
376 					       /* Allow negative quota usage */
377 
dquot_state_flag(unsigned int flags,int type)378 static inline unsigned int dquot_state_flag(unsigned int flags, int type)
379 {
380 	return flags << _DQUOT_STATE_FLAGS * type;
381 }
382 
dquot_generic_flag(unsigned int flags,int type)383 static inline unsigned int dquot_generic_flag(unsigned int flags, int type)
384 {
385 	return (flags >> _DQUOT_STATE_FLAGS * type) & DQUOT_STATE_FLAGS;
386 }
387 
388 #ifdef CONFIG_QUOTA_NETLINK_INTERFACE
389 extern void quota_send_warning(short type, unsigned int id, dev_t dev,
390 			       const char warntype);
391 #else
quota_send_warning(short type,unsigned int id,dev_t dev,const char warntype)392 static inline void quota_send_warning(short type, unsigned int id, dev_t dev,
393 				      const char warntype)
394 {
395 	return;
396 }
397 #endif /* CONFIG_QUOTA_NETLINK_INTERFACE */
398 
399 struct quota_info {
400 	unsigned int flags;			/* Flags for diskquotas on this device */
401 	struct mutex dqio_mutex;		/* lock device while I/O in progress */
402 	struct mutex dqonoff_mutex;		/* Serialize quotaon & quotaoff */
403 	struct rw_semaphore dqptr_sem;		/* serialize ops using quota_info struct, pointers from inode to dquots */
404 	struct inode *files[MAXQUOTAS];		/* inodes of quotafiles */
405 	struct mem_dqinfo info[MAXQUOTAS];	/* Information for each quota type */
406 	const struct quota_format_ops *ops[MAXQUOTAS];	/* Operations for each type */
407 };
408 
409 int register_quota_format(struct quota_format_type *fmt);
410 void unregister_quota_format(struct quota_format_type *fmt);
411 
412 struct quota_module_name {
413 	int qm_fmt_id;
414 	char *qm_mod_name;
415 };
416 
417 #define INIT_QUOTA_MODULE_NAMES {\
418 	{QFMT_VFS_OLD, "quota_v1"},\
419 	{QFMT_VFS_V0, "quota_v2"},\
420 	{QFMT_VFS_V1, "quota_v2"},\
421 	{0, NULL}}
422 
423 #endif /* __KERNEL__ */
424 #endif /* _QUOTA_ */
425