]> git.saurik.com Git - apple/xnu.git/blob - bsd/sys/quota.h
470d04dc1a60abecfa31924396a85e605fca528d
[apple/xnu.git] / bsd / sys / quota.h
1 /*
2 * Copyright (c) 2000-2002 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
11 *
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
18 * under the License.
19 *
20 * @APPLE_LICENSE_HEADER_END@
21 */
22 /*
23 * Copyright (c) 1982, 1986, 1993
24 * The Regents of the University of California. All rights reserved.
25 *
26 * This code is derived from software contributed to Berkeley by
27 * Robert Elz at The University of Melbourne.
28 *
29 * Redistribution and use in source and binary forms, with or without
30 * modification, are permitted provided that the following conditions
31 * are met:
32 * 1. Redistributions of source code must retain the above copyright
33 * notice, this list of conditions and the following disclaimer.
34 * 2. Redistributions in binary form must reproduce the above copyright
35 * notice, this list of conditions and the following disclaimer in the
36 * documentation and/or other materials provided with the distribution.
37 * 3. All advertising materials mentioning features or use of this software
38 * must display the following acknowledgement:
39 * This product includes software developed by the University of
40 * California, Berkeley and its contributors.
41 * 4. Neither the name of the University nor the names of its contributors
42 * may be used to endorse or promote products derived from this software
43 * without specific prior written permission.
44 *
45 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
46 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
47 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
48 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
49 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
50 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
51 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
52 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
53 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
54 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
55 * SUCH DAMAGE.
56 *
57 * @(#)quota.h
58 * derived from @(#)ufs/ufs/quota.h 8.3 (Berkeley) 8/19/94
59 */
60
61 #ifndef _SYS_QUOTA_H
62 #define _SYS_QUOTA_H
63
64 #include <sys/appleapiopts.h>
65
66 #ifdef __APPLE_API_UNSTABLE
67 /*
68 * Definitions for disk quotas imposed on the average user
69 * (big brother finally hits UNIX).
70 *
71 * The following constants define the amount of time given a user before the
72 * soft limits are treated as hard limits (usually resulting in an allocation
73 * failure). The timer is started when the user crosses their soft limit, it
74 * is reset when they go below their soft limit.
75 */
76 #define MAX_IQ_TIME (7*24*60*60) /* seconds in 1 week */
77 #define MAX_DQ_TIME (7*24*60*60) /* seconds in 1 week */
78
79 /*
80 * The following constants define the usage of the quota file array in the
81 * file system mount structure and dquot array in the inode structure. The semantics
82 * of the elements of these arrays are defined in the routine getinoquota;
83 * the remainder of the quota code treats them generically and need not be
84 * inspected when changing the size of the array.
85 */
86 #define MAXQUOTAS 2
87 #define USRQUOTA 0 /* element used for user quotas */
88 #define GRPQUOTA 1 /* element used for group quotas */
89
90 /*
91 * Definitions for the default names of the quotas files.
92 */
93 #define INITQFNAMES { \
94 "user", /* USRQUOTA */ \
95 "group", /* GRPQUOTA */ \
96 "undefined", \
97 };
98 #define QUOTAFILENAME ".quota"
99 #define QUOTAOPSNAME ".quota.ops"
100 #define QUOTAGROUP "operator"
101
102 /*
103 * Command definitions for the 'quotactl' system call. The commands are
104 * broken into a main command defined below and a subcommand that is used
105 * to convey the type of quota that is being manipulated (see above).
106 */
107 #define SUBCMDMASK 0x00ff
108 #define SUBCMDSHIFT 8
109 #define QCMD(cmd, type) (((cmd) << SUBCMDSHIFT) | ((type) & SUBCMDMASK))
110
111 #define Q_QUOTAON 0x0100 /* enable quotas */
112 #define Q_QUOTAOFF 0x0200 /* disable quotas */
113 #define Q_GETQUOTA 0x0300 /* get limits and usage */
114 #define Q_SETQUOTA 0x0400 /* set limits and usage */
115 #define Q_SETUSE 0x0500 /* set usage */
116 #define Q_SYNC 0x0600 /* sync disk copy of a filesystems quotas */
117 #define Q_QUOTASTAT 0x0700 /* get quota on/off status */
118
119 /*
120 * The following two structures define the format of the disk
121 * quota file (as it appears on disk) - the file contains a
122 * header followed by a hash table of dqblk entries. To find
123 * a particular entry, the user or group number (id) is first
124 * converted to an index into this table by means of the hash
125 * function dqhash1. If there is a collision at that index
126 * location then a second hash value is computed which using
127 * dqhash2. This second hash value is then used as an offset
128 * to the next location to probe. ID = 0 is used to indicate
129 * an empty (unused) entry. So there can never be an entry in
130 * the quota file for user 0 or group 0 (which is OK since disk
131 * quotas are never enforced for user 0).
132 *
133 * The setquota system call establishes the vnode for each quota
134 * file (a pointer is retained in the filesystem mount structure).
135 */
136 struct dqfilehdr {
137 u_int32_t dqh_magic;
138 u_int32_t dqh_version; /* == QF_VERSION */
139 u_int32_t dqh_maxentries; /* must be a power of 2 */
140 u_int32_t dqh_entrycnt; /* count of active entries */
141 u_int32_t dqh_flags; /* reserved for now (0) */
142 time_t dqh_chktime; /* time of last quota check */
143 time_t dqh_btime; /* time limit for excessive disk use */
144 time_t dqh_itime; /* time limit for excessive files */
145 char dqh_string[16]; /* tag string */
146 u_int32_t dqh_spare[4]; /* pad struct to power of 2 */
147 };
148
149 struct dqblk {
150 u_int64_t dqb_bhardlimit; /* absolute limit on disk bytes alloc */
151 u_int64_t dqb_bsoftlimit; /* preferred limit on disk bytes */
152 u_int64_t dqb_curbytes; /* current byte count */
153 u_int32_t dqb_ihardlimit; /* maximum # allocated inodes + 1 */
154 u_int32_t dqb_isoftlimit; /* preferred inode limit */
155 u_int32_t dqb_curinodes; /* current # allocated inodes */
156 time_t dqb_btime; /* time limit for excessive disk use */
157 time_t dqb_itime; /* time limit for excessive files */
158 u_int32_t dqb_id; /* identifier (0 for empty entries) */
159 u_int32_t dqb_spare[4]; /* pad struct to power of 2 */
160 };
161
162
163 #define INITQMAGICS { \
164 0xff31ff35, /* USRQUOTA */ \
165 0xff31ff27, /* GRPQUOTA */ \
166 };
167
168 #define QF_VERSION 1
169 #define QF_STRING_TAG "QUOTA HASH FILE"
170
171 #define QF_USERS_PER_GB 256
172 #define QF_MIN_USERS 2048
173 #define QF_MAX_USERS (2048*1024)
174
175 #define QF_GROUPS_PER_GB 32
176 #define QF_MIN_GROUPS 2048
177 #define QF_MAX_GROUPS (256*1024)
178
179
180 /*
181 * The primary and secondary multiplicative hash functions are
182 * derived from Knuth (vol. 3). They use a prime that is in
183 * golden ratio to the machine's word size.
184 */
185 #define dqhash1(id, shift, mask) \
186 ((((id) * 2654435761UL) >> (shift)) & (mask))
187
188 #define dqhash2(id, mask) \
189 (dqhash1((id), 11, (mask)>>1) | 1)
190
191 /*
192 * Compute a disk offset into a quota file.
193 */
194 #define dqoffset(index) \
195 (sizeof (struct dqfilehdr) + ((index) * sizeof (struct dqblk)))
196 /*
197 * Compute the hash shift value.
198 * It is the word size, in bits, minus the hash table size, in bits.
199 */
200 static __inline int dqhashshift(u_long);
201
202 static __inline int
203 dqhashshift(u_long size)
204 {
205 int shift;
206
207 for (shift = 32; size > 1; size >>= 1, --shift)
208 continue;
209 return (shift);
210 }
211
212
213 #ifndef KERNEL
214
215 #include <sys/cdefs.h>
216
217 __BEGIN_DECLS
218 int quotactl __P((char *, int, int, caddr_t));
219 __END_DECLS
220 #endif /* !KERNEL */
221
222 #ifdef KERNEL
223 #include <sys/queue.h>
224
225 /*
226 * Macros to avoid subroutine calls to trivial functions.
227 */
228 #if DIAGNOSTIC
229 #define DQREF(dq) dqref(dq)
230 #else
231 #define DQREF(dq) (dq)->dq_cnt++
232 #endif
233
234
235 /* Quota file info
236 */
237 struct quotafile {
238 struct vnode *qf_vp; /* quota file vnode */
239 struct ucred *qf_cred; /* quota file access cred */
240 int qf_shift; /* primary hash shift */
241 int qf_maxentries; /* size of hash table (power of 2) */
242 int qf_entrycnt; /* count of active entries */
243 time_t qf_btime; /* block quota time limit */
244 time_t qf_itime; /* inode quota time limit */
245 char qf_qflags; /* quota specific flags */
246 };
247
248 /*
249 * Flags describing the runtime state of quotas.
250 * (in qf_qflags)
251 */
252 #define QTF_OPENING 0x01 /* Q_QUOTAON in progress */
253 #define QTF_CLOSING 0x02 /* Q_QUOTAOFF in progress */
254
255
256 /*
257 * The following structure records disk usage for a user or group on a
258 * filesystem. There is one allocated for each quota that exists on any
259 * filesystem for the current user or group. A cache is kept of recently
260 * used entries.
261 */
262 struct dquot {
263 LIST_ENTRY(dquot) dq_hash; /* hash list */
264 TAILQ_ENTRY(dquot) dq_freelist; /* free list */
265 u_int16_t dq_flags; /* flags, see below */
266 u_int16_t dq_cnt; /* count of active references */
267 u_int16_t dq_spare; /* unused spare padding */
268 u_int16_t dq_type; /* quota type of this dquot */
269 u_int32_t dq_id; /* identifier this applies to */
270 u_int32_t dq_index; /* index into quota file */
271 struct quotafile *dq_qfile; /* quota file that this is taken from */
272 struct dqblk dq_dqb; /* actual usage & quotas */
273 };
274 /*
275 * Flag values.
276 */
277 #define DQ_LOCK 0x01 /* this quota locked (no MODS) */
278 #define DQ_WANT 0x02 /* wakeup on unlock */
279 #define DQ_MOD 0x04 /* this quota modified since read */
280 #define DQ_FAKE 0x08 /* no limits here, just usage */
281 #define DQ_BLKS 0x10 /* has been warned about blk limit */
282 #define DQ_INODS 0x20 /* has been warned about inode limit */
283 /*
284 * Shorthand notation.
285 */
286 #define dq_bhardlimit dq_dqb.dqb_bhardlimit
287 #define dq_bsoftlimit dq_dqb.dqb_bsoftlimit
288 #define dq_curbytes dq_dqb.dqb_curbytes
289 #define dq_ihardlimit dq_dqb.dqb_ihardlimit
290 #define dq_isoftlimit dq_dqb.dqb_isoftlimit
291 #define dq_curinodes dq_dqb.dqb_curinodes
292 #define dq_btime dq_dqb.dqb_btime
293 #define dq_itime dq_dqb.dqb_itime
294
295 /*
296 * If the system has never checked for a quota for this file, then it is
297 * set to NODQUOT. Once a write attempt is made the inode pointer is set
298 * to reference a dquot structure.
299 */
300 #define NODQUOT NULL
301
302 /*
303 * Flags to chkdq() and chkiq()
304 */
305 #define FORCE 0x01 /* force usage changes independent of limits */
306 #define CHOWN 0x02 /* (advisory) change initiated by chown */
307
308
309 /*
310 * Functions that manage the in-core dquot and the
311 * on-disk dqblk data structures.
312 */
313 __BEGIN_DECLS
314 int dqfileopen(struct quotafile *, int);
315 void dqfileclose(struct quotafile *, int);
316 void dqflush(struct vnode *);
317 int dqget(struct vnode *, u_long, struct quotafile *, int, struct dquot **);
318 void dqinit(void);
319 void dqref(struct dquot *);
320 void dqrele(struct vnode *, struct dquot *);
321 int dqsync(struct vnode *, struct dquot *);
322 __END_DECLS
323
324 #endif /* KERNEL */
325
326 #endif /* __APPLE_API_UNSTABLE */
327
328 #endif /* !_SYS_QUOTA_H_ */