]> git.saurik.com Git - apple/xnu.git/blob - bsd/sys/quota.h
xnu-792.tar.gz
[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 #if __DARWIN_ALIGN_NATURAL
176 #pragma options align=natural
177 #endif
178
179 struct user_dqblk {
180 u_int64_t dqb_bhardlimit; /* absolute limit on disk bytes alloc */
181 u_int64_t dqb_bsoftlimit; /* preferred limit on disk bytes */
182 u_int64_t dqb_curbytes; /* current byte count */
183 u_int32_t dqb_ihardlimit; /* maximum # allocated inodes + 1 */
184 u_int32_t dqb_isoftlimit; /* preferred inode limit */
185 u_int32_t dqb_curinodes; /* current # allocated inodes */
186 user_time_t dqb_btime; /* time limit for excessive disk use */
187 user_time_t dqb_itime; /* time limit for excessive files */
188 u_int32_t dqb_id; /* identifier (0 for empty entries) */
189 u_int32_t dqb_spare[4]; /* pad struct to power of 2 */
190 };
191
192 #if __DARWIN_ALIGN_NATURAL
193 #pragma options align=reset
194 #endif
195 #endif /* KERNEL_PRIVATE */
196
197 #define INITQMAGICS { \
198 0xff31ff35, /* USRQUOTA */ \
199 0xff31ff27, /* GRPQUOTA */ \
200 };
201
202 #define QF_VERSION 1
203 #define QF_STRING_TAG "QUOTA HASH FILE"
204
205 #define QF_USERS_PER_GB 256
206 #define QF_MIN_USERS 2048
207 #define QF_MAX_USERS (2048*1024)
208
209 #define QF_GROUPS_PER_GB 32
210 #define QF_MIN_GROUPS 2048
211 #define QF_MAX_GROUPS (256*1024)
212
213
214 /*
215 * The primary and secondary multiplicative hash functions are
216 * derived from Knuth (vol. 3). They use a prime that is in
217 * golden ratio to the machine's word size.
218 */
219 #define dqhash1(id, shift, mask) \
220 ((((id) * 2654435761UL) >> (shift)) & (mask))
221
222 #define dqhash2(id, mask) \
223 (dqhash1((id), 11, (mask)>>1) | 1)
224
225 /*
226 * Compute a disk offset into a quota file.
227 */
228 #define dqoffset(index) \
229 (sizeof (struct dqfilehdr) + ((index) * sizeof (struct dqblk)))
230 /*
231 * Compute the hash shift value.
232 * It is the word size, in bits, minus the hash table size, in bits.
233 */
234 static __inline int dqhashshift(u_long);
235
236 static __inline int
237 dqhashshift(u_long size)
238 {
239 int shift;
240
241 for (shift = 32; size > 1; size >>= 1, --shift)
242 continue;
243 return (shift);
244 }
245
246
247 #ifndef KERNEL
248 __BEGIN_DECLS
249 int quotactl(char *, int, int, caddr_t);
250 __END_DECLS
251 #endif /* !KERNEL */
252
253 #ifdef KERNEL_PRIVATE
254 #include <sys/queue.h>
255
256
257
258 /* Quota file info
259 */
260 struct quotafile {
261 lck_mtx_t qf_lock; /* quota file mutex */
262 struct vnode *qf_vp; /* quota file vnode */
263 struct ucred *qf_cred; /* quota file access cred */
264 int qf_shift; /* primary hash shift */
265 int qf_maxentries; /* size of hash table (power of 2) */
266 int qf_entrycnt; /* count of active entries */
267 time_t qf_btime; /* block quota time limit */
268 time_t qf_itime; /* inode quota time limit */
269
270 /* the following 2 fields are protected */
271 /* by the quota list lock */
272 char qf_qflags; /* quota specific flags */
273 int qf_refcnt; /* count of dquot refs on this file */
274 };
275
276 /*
277 * Flags describing the runtime state of quotas.
278 * (in qf_qflags)
279 */
280 #define QTF_OPENING 0x01 /* Q_QUOTAON in progress */
281 #define QTF_CLOSING 0x02 /* Q_QUOTAOFF in progress */
282 #define QTF_WANTED 0x04 /* waiting for change of state */
283
284
285 /*
286 * The following structure records disk usage for a user or group on a
287 * filesystem. There is one allocated for each quota that exists on any
288 * filesystem for the current user or group. A cache is kept of recently
289 * used entries.
290 */
291 struct dquot {
292 LIST_ENTRY(dquot) dq_hash; /* hash list */
293 TAILQ_ENTRY(dquot) dq_freelist; /* free list */
294 u_int16_t dq_flags; /* flags, see below */
295 u_int16_t dq_cnt; /* count of active references */
296 u_int16_t dq_lflags; /* protected by the quota list lock */
297 u_int16_t dq_type; /* quota type of this dquot */
298 u_int32_t dq_id; /* identifier this applies to */
299 u_int32_t dq_index; /* index into quota file */
300 struct quotafile *dq_qfile; /* quota file that this is taken from */
301 struct dqblk dq_dqb; /* actual usage & quotas */
302 };
303
304 /*
305 * dq_lflags values
306 */
307 #define DQ_LLOCK 0x01 /* this quota locked (no MODS) */
308 #define DQ_LWANT 0x02 /* wakeup on unlock */
309
310 /*
311 * dq_flags values
312 */
313 #define DQ_MOD 0x01 /* this quota modified since read */
314 #define DQ_FAKE 0x02 /* no limits here, just usage */
315 #define DQ_BLKS 0x04 /* has been warned about blk limit */
316 #define DQ_INODS 0x08 /* has been warned about inode limit */
317
318 /*
319 * Shorthand notation.
320 */
321 #define dq_bhardlimit dq_dqb.dqb_bhardlimit
322 #define dq_bsoftlimit dq_dqb.dqb_bsoftlimit
323 #define dq_curbytes dq_dqb.dqb_curbytes
324 #define dq_ihardlimit dq_dqb.dqb_ihardlimit
325 #define dq_isoftlimit dq_dqb.dqb_isoftlimit
326 #define dq_curinodes dq_dqb.dqb_curinodes
327 #define dq_btime dq_dqb.dqb_btime
328 #define dq_itime dq_dqb.dqb_itime
329
330 /*
331 * If the system has never checked for a quota for this file, then it is
332 * set to NODQUOT. Once a write attempt is made the inode pointer is set
333 * to reference a dquot structure.
334 */
335 #define NODQUOT NULL
336
337 /*
338 * Flags to chkdq() and chkiq()
339 */
340 #define FORCE 0x01 /* force usage changes independent of limits */
341 #define CHOWN 0x02 /* (advisory) change initiated by chown */
342
343
344 /*
345 * Functions that manage the in-core dquot and the
346 * on-disk dqblk data structures.
347 */
348 __BEGIN_DECLS
349 void dqfileinit(struct quotafile *);
350 int dqfileopen(struct quotafile *, int);
351 void dqfileclose(struct quotafile *, int);
352 void dqflush(struct vnode *);
353 int dqget(u_long, struct quotafile *, int, struct dquot **);
354 void dqinit(void);
355 void dqref(struct dquot *);
356 void dqrele(struct dquot *);
357 void dqreclaim(struct dquot *);
358 int dqsync(struct dquot *);
359 void dqsync_orphans(struct quotafile *);
360 void dqlock(struct dquot *);
361 void dqunlock(struct dquot *);
362
363 int qf_get(struct quotafile *, int type);
364 void qf_put(struct quotafile *, int type);
365
366 __private_extern__ void munge_dqblk(struct dqblk *dqblkp, struct user_dqblk *user_dqblkp, boolean_t to64);
367 __END_DECLS
368
369 #endif /* KERNEL_PRIVATE */
370
371 #endif /* __APPLE_API_UNSTABLE */
372
373 #endif /* !_SYS_QUOTA_H_ */