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