]> git.saurik.com Git - apple/xnu.git/blob - bsd/sys/quota.h
b2b8ebc8940e64504e6a638e5d6047787572bd64
[apple/xnu.git] / bsd / sys / quota.h
1 /*
2 * Copyright (c) 2000-2005 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 #include <sys/cdefs.h>
66 #ifdef KERNEL_PRIVATE
67 #include <kern/locks.h>
68 #endif
69
70 #include <mach/boolean.h>
71
72 #ifdef __APPLE_API_UNSTABLE
73 /*
74 * Definitions for disk quotas imposed on the average user
75 * (big brother finally hits UNIX).
76 *
77 * The following constants define the amount of time given a user before the
78 * soft limits are treated as hard limits (usually resulting in an allocation
79 * failure). The timer is started when the user crosses their soft limit, it
80 * is reset when they go below their soft limit.
81 */
82 #define MAX_IQ_TIME (7*24*60*60) /* seconds in 1 week */
83 #define MAX_DQ_TIME (7*24*60*60) /* seconds in 1 week */
84
85 /*
86 * The following constants define the usage of the quota file array in the
87 * file system mount structure and dquot array in the inode structure. The semantics
88 * of the elements of these arrays are defined in the routine getinoquota;
89 * the remainder of the quota code treats them generically and need not be
90 * inspected when changing the size of the array.
91 */
92 #define MAXQUOTAS 2
93 #define USRQUOTA 0 /* element used for user quotas */
94 #define GRPQUOTA 1 /* element used for group quotas */
95
96 /*
97 * Definitions for the default names of the quotas files.
98 */
99 #define INITQFNAMES { \
100 "user", /* USRQUOTA */ \
101 "group", /* GRPQUOTA */ \
102 "undefined", \
103 };
104 #define QUOTAFILENAME ".quota"
105 #define QUOTAOPSNAME ".quota.ops"
106 #define QUOTAGROUP "operator"
107
108 /*
109 * Command definitions for the 'quotactl' system call. The commands are
110 * broken into a main command defined below and a subcommand that is used
111 * to convey the type of quota that is being manipulated (see above).
112 */
113 #define SUBCMDMASK 0x00ff
114 #define SUBCMDSHIFT 8
115 #define QCMD(cmd, type) (((cmd) << SUBCMDSHIFT) | ((type) & SUBCMDMASK))
116
117 #define Q_QUOTAON 0x0100 /* enable quotas */
118 #define Q_QUOTAOFF 0x0200 /* disable quotas */
119 #define Q_GETQUOTA 0x0300 /* get limits and usage */
120 #define Q_SETQUOTA 0x0400 /* set limits and usage */
121 #define Q_SETUSE 0x0500 /* set usage */
122 #define Q_SYNC 0x0600 /* sync disk copy of a filesystems quotas */
123 #define Q_QUOTASTAT 0x0700 /* get quota on/off status */
124
125 /*
126 * The following two structures define the format of the disk
127 * quota file (as it appears on disk) - the file contains a
128 * header followed by a hash table of dqblk entries. To find
129 * a particular entry, the user or group number (id) is first
130 * converted to an index into this table by means of the hash
131 * function dqhash1. If there is a collision at that index
132 * location then a second hash value is computed which using
133 * dqhash2. This second hash value is then used as an offset
134 * to the next location to probe. ID = 0 is used to indicate
135 * an empty (unused) entry. So there can never be an entry in
136 * the quota file for user 0 or group 0 (which is OK since disk
137 * quotas are never enforced for user 0).
138 *
139 * The setquota system call establishes the vnode for each quota
140 * file (a pointer is retained in the filesystem mount structure).
141 */
142 struct dqfilehdr {
143 u_int32_t dqh_magic;
144 u_int32_t dqh_version; /* == QF_VERSION */
145 u_int32_t dqh_maxentries; /* must be a power of 2 */
146 u_int32_t dqh_entrycnt; /* count of active entries */
147 u_int32_t dqh_flags; /* reserved for now (0) */
148 time_t dqh_chktime; /* time of last quota check */
149 time_t dqh_btime; /* time limit for excessive disk use */
150 time_t dqh_itime; /* time limit for excessive files */
151 char dqh_string[16]; /* tag string */
152 u_int32_t dqh_spare[4]; /* pad struct to power of 2 */
153 };
154
155 struct dqblk {
156 u_int64_t dqb_bhardlimit; /* absolute limit on disk bytes alloc */
157 u_int64_t dqb_bsoftlimit; /* preferred limit on disk bytes */
158 u_int64_t dqb_curbytes; /* current byte count */
159 u_int32_t dqb_ihardlimit; /* maximum # allocated inodes + 1 */
160 u_int32_t dqb_isoftlimit; /* preferred inode limit */
161 u_int32_t dqb_curinodes; /* current # allocated inodes */
162 time_t dqb_btime; /* time limit for excessive disk use */
163 time_t dqb_itime; /* time limit for excessive files */
164 u_int32_t dqb_id; /* identifier (0 for empty entries) */
165 u_int32_t dqb_spare[4]; /* pad struct to power of 2 */
166 };
167
168 #ifdef KERNEL_PRIVATE
169 #include <machine/types.h> /* user_time_t */
170 /* LP64 version of struct dqblk. time_t is a long and must grow when
171 * we're dealing with a 64-bit process.
172 * WARNING - keep in sync with struct dqblk
173 */
174
175 struct user_dqblk {
176 u_int64_t dqb_bhardlimit; /* absolute limit on disk bytes alloc */
177 u_int64_t dqb_bsoftlimit; /* preferred limit on disk bytes */
178 u_int64_t dqb_curbytes; /* current byte count */
179 u_int32_t dqb_ihardlimit; /* maximum # allocated inodes + 1 */
180 u_int32_t dqb_isoftlimit; /* preferred inode limit */
181 u_int32_t dqb_curinodes; /* current # allocated inodes */
182 user_time_t dqb_btime __attribute((aligned(8))); /* time limit for excessive disk use */
183 user_time_t dqb_itime; /* time limit for excessive files */
184 u_int32_t dqb_id; /* identifier (0 for empty entries) */
185 u_int32_t dqb_spare[4]; /* pad struct to power of 2 */
186 };
187 #endif /* KERNEL_PRIVATE */
188
189 #define INITQMAGICS { \
190 0xff31ff35, /* USRQUOTA */ \
191 0xff31ff27, /* GRPQUOTA */ \
192 };
193
194 #define QF_VERSION 1
195 #define QF_STRING_TAG "QUOTA HASH FILE"
196
197 #define QF_USERS_PER_GB 256
198 #define QF_MIN_USERS 2048
199 #define QF_MAX_USERS (2048*1024)
200
201 #define QF_GROUPS_PER_GB 32
202 #define QF_MIN_GROUPS 2048
203 #define QF_MAX_GROUPS (256*1024)
204
205
206 /*
207 * The primary and secondary multiplicative hash functions are
208 * derived from Knuth (vol. 3). They use a prime that is in
209 * golden ratio to the machine's word size.
210 */
211 #define dqhash1(id, shift, mask) \
212 ((((id) * 2654435761UL) >> (shift)) & (mask))
213
214 #define dqhash2(id, mask) \
215 (dqhash1((id), 11, (mask)>>1) | 1)
216
217 /*
218 * Compute a disk offset into a quota file.
219 */
220 #define dqoffset(index) \
221 (sizeof (struct dqfilehdr) + ((index) * sizeof (struct dqblk)))
222 /*
223 * Compute the hash shift value.
224 * It is the word size, in bits, minus the hash table size, in bits.
225 */
226 static __inline int dqhashshift(u_long);
227
228 static __inline int
229 dqhashshift(u_long size)
230 {
231 int shift;
232
233 for (shift = 32; size > 1; size >>= 1, --shift)
234 continue;
235 return (shift);
236 }
237
238
239 #ifndef KERNEL
240 __BEGIN_DECLS
241 int quotactl(char *, int, int, caddr_t);
242 __END_DECLS
243 #endif /* !KERNEL */
244
245 #ifdef KERNEL_PRIVATE
246 #include <sys/queue.h>
247
248
249
250 /* Quota file info
251 */
252 struct quotafile {
253 lck_mtx_t qf_lock; /* quota file mutex */
254 struct vnode *qf_vp; /* quota file vnode */
255 struct ucred *qf_cred; /* quota file access cred */
256 int qf_shift; /* primary hash shift */
257 int qf_maxentries; /* size of hash table (power of 2) */
258 int qf_entrycnt; /* count of active entries */
259 time_t qf_btime; /* block quota time limit */
260 time_t qf_itime; /* inode quota time limit */
261
262 /* the following 2 fields are protected */
263 /* by the quota list lock */
264 char qf_qflags; /* quota specific flags */
265 int qf_refcnt; /* count of dquot refs on this file */
266 };
267
268 /*
269 * Flags describing the runtime state of quotas.
270 * (in qf_qflags)
271 */
272 #define QTF_OPENING 0x01 /* Q_QUOTAON in progress */
273 #define QTF_CLOSING 0x02 /* Q_QUOTAOFF in progress */
274 #define QTF_WANTED 0x04 /* waiting for change of state */
275
276
277 /*
278 * The following structure records disk usage for a user or group on a
279 * filesystem. There is one allocated for each quota that exists on any
280 * filesystem for the current user or group. A cache is kept of recently
281 * used entries.
282 */
283 struct dquot {
284 LIST_ENTRY(dquot) dq_hash; /* hash list */
285 TAILQ_ENTRY(dquot) dq_freelist; /* free list */
286 uint32_t dq_cnt; /* count of active references */
287 u_int16_t dq_flags; /* flags, see below */
288 u_int16_t dq_lflags; /* protected by the quota list lock */
289 u_int16_t dq_type; /* quota type of this dquot */
290 u_int32_t dq_id; /* identifier this applies to */
291 u_int32_t dq_index; /* index into quota file */
292 struct quotafile *dq_qfile; /* quota file that this is taken from */
293 struct dqblk dq_dqb; /* actual usage & quotas */
294 };
295
296 /*
297 * dq_lflags values
298 */
299 #define DQ_LLOCK 0x01 /* this quota locked (no MODS) */
300 #define DQ_LWANT 0x02 /* wakeup on unlock */
301
302 /*
303 * dq_flags values
304 */
305 #define DQ_MOD 0x01 /* this quota modified since read */
306 #define DQ_FAKE 0x02 /* no limits here, just usage */
307 #define DQ_BLKS 0x04 /* has been warned about blk limit */
308 #define DQ_INODS 0x08 /* has been warned about inode limit */
309
310 /*
311 * Shorthand notation.
312 */
313 #define dq_bhardlimit dq_dqb.dqb_bhardlimit
314 #define dq_bsoftlimit dq_dqb.dqb_bsoftlimit
315 #define dq_curbytes dq_dqb.dqb_curbytes
316 #define dq_ihardlimit dq_dqb.dqb_ihardlimit
317 #define dq_isoftlimit dq_dqb.dqb_isoftlimit
318 #define dq_curinodes dq_dqb.dqb_curinodes
319 #define dq_btime dq_dqb.dqb_btime
320 #define dq_itime dq_dqb.dqb_itime
321
322 /*
323 * If the system has never checked for a quota for this file, then it is
324 * set to NODQUOT. Once a write attempt is made the inode pointer is set
325 * to reference a dquot structure.
326 */
327 #define NODQUOT NULL
328
329 /*
330 * Flags to chkdq() and chkiq()
331 */
332 #define FORCE 0x01 /* force usage changes independent of limits */
333 #define CHOWN 0x02 /* (advisory) change initiated by chown */
334
335
336 /*
337 * Functions that manage the in-core dquot and the
338 * on-disk dqblk data structures.
339 */
340 __BEGIN_DECLS
341 void dqfileinit(struct quotafile *);
342 int dqfileopen(struct quotafile *, int);
343 void dqfileclose(struct quotafile *, int);
344 void dqflush(struct vnode *);
345 int dqget(u_long, struct quotafile *, int, struct dquot **);
346 void dqinit(void);
347 void dqref(struct dquot *);
348 void dqrele(struct dquot *);
349 void dqreclaim(struct dquot *);
350 int dqsync(struct dquot *);
351 void dqsync_orphans(struct quotafile *);
352 void dqlock(struct dquot *);
353 void dqunlock(struct dquot *);
354
355 int qf_get(struct quotafile *, int type);
356 void qf_put(struct quotafile *, int type);
357
358 __private_extern__ void munge_dqblk(struct dqblk *dqblkp, struct user_dqblk *user_dqblkp, boolean_t to64);
359 __END_DECLS
360
361 #endif /* KERNEL_PRIVATE */
362
363 #endif /* __APPLE_API_UNSTABLE */
364
365 #endif /* !_SYS_QUOTA_H_ */