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. All advertising materials mentioning features or use of this software
17  *    must display the following acknowledgement:
18  *   This product includes software developed by the University of
19  *   California, Berkeley and its contributors.
20  * 4. Neither the name of the University nor the names of its contributors
21  *    may be used to endorse or promote products derived from this software
22  *    without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34  * SUCH DAMAGE.
35  *
36  * Version: $Id: quota.h,v 2.0 1996/11/17 16:48:14 mvw Exp mvw $
37  */
38 
39 #ifndef _LINUX_QUOTA_
40 #define _LINUX_QUOTA_
41 
42 #include <linux/errno.h>
43 #include <linux/types.h>
44 
45 #define __DQUOT_VERSION__	"dquot_6.5.1"
46 #define __DQUOT_NUM_VERSION__	6*10000+5*100+1
47 
48 typedef __kernel_uid32_t qid_t; /* Type in which we store ids in memory */
49 typedef __u64 qsize_t;          /* Type in which we store sizes */
50 
51 /* Size of blocks in which are counted size limits */
52 #define QUOTABLOCK_BITS 10
53 #define QUOTABLOCK_SIZE (1 << QUOTABLOCK_BITS)
54 
55 /* Conversion routines from and to quota blocks */
56 #define qb2kb(x) ((x) << (QUOTABLOCK_BITS-10))
57 #define kb2qb(x) ((x) >> (QUOTABLOCK_BITS-10))
58 #define toqb(x) (((x) + QUOTABLOCK_SIZE - 1) >> QUOTABLOCK_BITS)
59 
60 #define MAXQUOTAS 2
61 #define USRQUOTA  0		/* element used for user quotas */
62 #define GRPQUOTA  1		/* element used for group quotas */
63 
64 /*
65  * Definitions for the default names of the quotas files.
66  */
67 #define INITQFNAMES { \
68 	"user",    /* USRQUOTA */ \
69 	"group",   /* GRPQUOTA */ \
70 	"undefined", \
71 };
72 
73 /*
74  * Command definitions for the 'quotactl' system call.
75  * The commands are broken into a main command defined below
76  * and a subcommand that is used to convey the type of
77  * quota that is being manipulated (see above).
78  */
79 #define SUBCMDMASK  0x00ff
80 #define SUBCMDSHIFT 8
81 #define QCMD(cmd, type)  (((cmd) << SUBCMDSHIFT) | ((type) & SUBCMDMASK))
82 
83 #define Q_SYNC     0x800001	/* sync disk copy of a filesystems quotas */
84 #define Q_QUOTAON  0x800002	/* turn quotas on */
85 #define Q_QUOTAOFF 0x800003	/* turn quotas off */
86 #define Q_GETFMT   0x800004	/* get quota format used on given filesystem */
87 #define Q_GETINFO  0x800005	/* get information about quota files */
88 #define Q_SETINFO  0x800006	/* set information about quota files */
89 #define Q_GETQUOTA 0x800007	/* get user quota structure */
90 #define Q_SETQUOTA 0x800008	/* set user quota structure */
91 
92 /*
93  * Quota structure used for communication with userspace via quotactl
94  * Following flags are used to specify which fields are valid
95  */
96 #define QIF_BLIMITS	1
97 #define QIF_SPACE	2
98 #define QIF_ILIMITS	4
99 #define QIF_INODES	8
100 #define QIF_BTIME	16
101 #define QIF_ITIME	32
102 #define QIF_LIMITS	(QIF_BLIMITS | QIF_ILIMITS)
103 #define QIF_USAGE	(QIF_SPACE | QIF_INODES)
104 #define QIF_TIMES	(QIF_BTIME | QIF_ITIME)
105 #define QIF_ALL		(QIF_LIMITS | QIF_USAGE | QIF_TIMES)
106 
107 struct if_dqblk {
108 	__u64 dqb_bhardlimit;
109 	__u64 dqb_bsoftlimit;
110 	__u64 dqb_curspace;
111 	__u64 dqb_ihardlimit;
112 	__u64 dqb_isoftlimit;
113 	__u64 dqb_curinodes;
114 	__u64 dqb_btime;
115 	__u64 dqb_itime;
116 	__u32 dqb_valid;
117 };
118 
119 /*
120  * Structure used for setting quota information about file via quotactl
121  * Following flags are used to specify which fields are valid
122  */
123 #define IIF_BGRACE	1
124 #define IIF_IGRACE	2
125 #define IIF_FLAGS	4
126 #define IIF_ALL		(IIF_BGRACE | IIF_IGRACE | IIF_FLAGS)
127 
128 struct if_dqinfo {
129 	__u64 dqi_bgrace;
130 	__u64 dqi_igrace;
131 	__u32 dqi_flags;
132 	__u32 dqi_valid;
133 };
134 
135 #ifdef __KERNEL__
136 
137 #include <linux/dqblk_xfs.h>
138 #include <linux/dqblk_v1.h>
139 #include <linux/dqblk_v2.h>
140 
141 /*
142  * Data for one user/group kept in memory
143  */
144 struct mem_dqblk {
145 	__u32 dqb_bhardlimit;	/* absolute limit on disk blks alloc */
146 	__u32 dqb_bsoftlimit;	/* preferred limit on disk blks */
147 	qsize_t dqb_curspace;	/* current used space */
148 	__u32 dqb_ihardlimit;	/* absolute limit on allocated inodes */
149 	__u32 dqb_isoftlimit;	/* preferred inode limit */
150 	__u32 dqb_curinodes;	/* current # allocated inodes */
151 	time_t dqb_btime;	/* time limit for excessive disk use */
152 	time_t dqb_itime;	/* time limit for excessive inode use */
153 };
154 
155 /*
156  * Data for one quotafile kept in memory
157  */
158 struct quota_format_type;
159 
160 struct mem_dqinfo {
161 	struct quota_format_type *dqi_format;
162 	int dqi_flags;
163 	unsigned int dqi_bgrace;
164 	unsigned int dqi_igrace;
165 	union {
166 		struct v1_mem_dqinfo v1_i;
167 		struct v2_mem_dqinfo v2_i;
168 	} u;
169 };
170 
171 #define DQF_MASK 0xffff		/* Mask for format specific flags */
172 #define DQF_INFO_DIRTY 0x10000  /* Is info dirty? */
173 #define DQF_ANY_DQUOT_DIRTY 0x20000	/* Is any dquot dirty? */
174 
mark_info_dirty(struct mem_dqinfo * info)175 extern inline void mark_info_dirty(struct mem_dqinfo *info)
176 {
177 	info->dqi_flags |= DQF_INFO_DIRTY;
178 }
179 
180 #define info_dirty(info) ((info)->dqi_flags & DQF_INFO_DIRTY)
181 
182 #define info_any_dirty(info) ((info)->dqi_flags & DQF_INFO_DIRTY ||\
183 			      (info)->dqi_flags & DQF_ANY_DQUOT_DIRTY)
184 
185 #define sb_dqopt(sb) (&(sb)->s_dquot)
186 
187 struct dqstats {
188 	int lookups;
189 	int drops;
190 	int reads;
191 	int writes;
192 	int cache_hits;
193 	int allocated_dquots;
194 	int free_dquots;
195 	int syncs;
196 };
197 
198 extern struct dqstats dqstats;
199 
200 #define NR_DQHASH 43            /* Just an arbitrary number */
201 
202 #define DQ_LOCKED     0x01	/* dquot under IO */
203 #define DQ_MOD        0x02	/* dquot modified since read */
204 #define DQ_BLKS       0x10	/* uid/gid has been warned about blk limit */
205 #define DQ_INODES     0x20	/* uid/gid has been warned about inode limit */
206 #define DQ_FAKE       0x40	/* no limits only usage */
207 #define DQ_INVAL      0x80	/* dquot is going to be invalidated */
208 
209 struct dquot {
210 	struct list_head dq_hash;	/* Hash list in memory */
211 	struct list_head dq_inuse;	/* List of all quotas */
212 	struct list_head dq_free;	/* Free list element */
213 	wait_queue_head_t dq_wait_lock;	/* Pointer to waitqueue on dquot lock */
214 	wait_queue_head_t dq_wait_free;	/* Pointer to waitqueue for quota to be unused */
215 	int dq_count;			/* Use count */
216 	int dq_dup_ref;			/* Number of duplicated refences */
217 
218 	/* fields after this point are cleared when invalidating */
219 	struct super_block *dq_sb;	/* superblock this applies to */
220 	unsigned int dq_id;		/* ID this applies to (uid, gid) */
221 	kdev_t dq_dev;			/* Device this applies to */
222 	loff_t dq_off;			/* Offset of dquot on disk */
223 	short dq_type;			/* Type of quota */
224 	short dq_flags;			/* See DQ_* */
225 	struct mem_dqblk dq_dqb;	/* Diskquota usage */
226 };
227 
228 #define NODQUOT (struct dquot *)NULL
229 
230 #define QUOTA_OK          0
231 #define NO_QUOTA          1
232 
233 /* Operations which must be implemented by each quota format */
234 struct quota_format_ops {
235 	int (*check_quota_file)(struct super_block *sb, int type);	/* Detect whether file is in our format */
236 	int (*read_file_info)(struct super_block *sb, int type);	/* Read main info about file - called on quotaon() */
237 	int (*write_file_info)(struct super_block *sb, int type);	/* Write main info about file */
238 	int (*free_file_info)(struct super_block *sb, int type);	/* Called on quotaoff() */
239 	int (*read_dqblk)(struct dquot *dquot);		/* Read structure for one user */
240 	int (*commit_dqblk)(struct dquot *dquot);	/* Write (or delete) structure for one user */
241 };
242 
243 /* Operations working with dquots */
244 struct dquot_operations {
245 	void (*initialize) (struct inode *, int);
246 	void (*drop) (struct inode *);
247 	int (*alloc_space) (struct inode *, qsize_t, int);
248 	int (*alloc_inode) (const struct inode *, unsigned long);
249 	void (*free_space) (struct inode *, qsize_t);
250 	void (*free_inode) (const struct inode *, unsigned long);
251 	int (*transfer) (struct inode *, struct iattr *);
252 	int (*write_dquot) (struct dquot *);
253 };
254 
255 /* Operations handling requests from userspace */
256 struct quotactl_ops {
257 	int (*quota_on)(struct super_block *, int, int, char *);
258 	int (*quota_off)(struct super_block *, int);
259 	int (*quota_sync)(struct super_block *, int);
260 	int (*get_info)(struct super_block *, int, struct if_dqinfo *);
261 	int (*set_info)(struct super_block *, int, struct if_dqinfo *);
262 	int (*get_dqblk)(struct super_block *, int, qid_t, struct if_dqblk *);
263 	int (*set_dqblk)(struct super_block *, int, qid_t, struct if_dqblk *);
264 	int (*get_xstate)(struct super_block *, struct fs_quota_stat *);
265 	int (*set_xstate)(struct super_block *, unsigned int, int);
266 	int (*get_xquota)(struct super_block *, int, qid_t, struct fs_disk_quota *);
267 	int (*set_xquota)(struct super_block *, int, qid_t, struct fs_disk_quota *);
268 };
269 
270 struct quota_format_type {
271 	int qf_fmt_id;	/* Quota format id */
272 	struct quota_format_ops *qf_ops;	/* Operations of format */
273 	struct module *qf_owner;		/* Module implementing quota format */
274 	struct quota_format_type *qf_next;
275 };
276 
277 #define DQUOT_USR_ENABLED	0x01		/* User diskquotas enabled */
278 #define DQUOT_GRP_ENABLED	0x02		/* Group diskquotas enabled */
279 
280 struct quota_info {
281 	unsigned int flags;			/* Flags for diskquotas on this device */
282 	struct semaphore dqio_sem;		/* lock device while I/O in progress */
283 	struct semaphore dqoff_sem;		/* serialize quota_off() and quota_on() on device */
284 	struct file *files[MAXQUOTAS];		/* fp's to quotafiles */
285 	struct mem_dqinfo info[MAXQUOTAS];	/* Information for each quota type */
286 	struct quota_format_ops *ops[MAXQUOTAS];	/* Operations for each type */
287 };
288 
289 /* Inline would be better but we need to dereference super_block which is not defined yet */
290 #define mark_dquot_dirty(dquot) do {\
291 	dquot->dq_flags |= DQ_MOD;\
292 	sb_dqopt(dquot->dq_sb)->info[dquot->dq_type].dqi_flags |= DQF_ANY_DQUOT_DIRTY;\
293 } while (0)
294 
295 #define dquot_dirty(dquot) ((dquot)->dq_flags & DQ_MOD)
296 
is_enabled(struct quota_info * dqopt,int type)297 static inline int is_enabled(struct quota_info *dqopt, int type)
298 {
299 	switch (type) {
300 		case USRQUOTA:
301 			return dqopt->flags & DQUOT_USR_ENABLED;
302 		case GRPQUOTA:
303 			return dqopt->flags & DQUOT_GRP_ENABLED;
304 	}
305 	return 0;
306 }
307 
308 #define sb_any_quota_enabled(sb) (is_enabled(sb_dqopt(sb), USRQUOTA) | is_enabled(sb_dqopt(sb), GRPQUOTA))
309 
310 #define sb_has_quota_enabled(sb, type) (is_enabled(sb_dqopt(sb), type))
311 
312 int register_quota_format(struct quota_format_type *fmt);
313 void unregister_quota_format(struct quota_format_type *fmt);
314 void init_dquot_operations(struct dquot_operations *fsdqops);
315 
316 #else
317 
318 # /* nodep */ include <sys/cdefs.h>
319 
320 __BEGIN_DECLS
321 long quotactl __P ((unsigned int, const char *, int, caddr_t));
322 __END_DECLS
323 
324 #endif /* __KERNEL__ */
325 #endif /* _QUOTA_ */
326