]>
git.saurik.com Git - apple/xnu.git/blob - bsd/sys/quota.h
1db5dba7040da151f807103b05ffc11b5907bb89
2 * Copyright (c) 2006 Apple Computer, Inc. All Rights Reserved.
4 * @APPLE_LICENSE_OSREFERENCE_HEADER_START@
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
16 * Please obtain a copy of the License at
17 * http://www.opensource.apple.com/apsl/ and read it before using this
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.
28 * @APPLE_LICENSE_OSREFERENCE_HEADER_END@
31 * Copyright (c) 1982, 1986, 1993
32 * The Regents of the University of California. All rights reserved.
34 * This code is derived from software contributed to Berkeley by
35 * Robert Elz at The University of Melbourne.
37 * Redistribution and use in source and binary forms, with or without
38 * modification, are permitted provided that the following conditions
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.
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
66 * derived from @(#)ufs/ufs/quota.h 8.3 (Berkeley) 8/19/94
72 #include <sys/appleapiopts.h>
73 #include <sys/cdefs.h>
75 #include <kern/locks.h>
78 #include <mach/boolean.h>
80 #ifdef __APPLE_API_UNSTABLE
82 * Definitions for disk quotas imposed on the average user
83 * (big brother finally hits UNIX).
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.
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 */
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.
101 #define USRQUOTA 0 /* element used for user quotas */
102 #define GRPQUOTA 1 /* element used for group quotas */
105 * Definitions for the default names of the quotas files.
107 #define INITQFNAMES { \
108 "user", /* USRQUOTA */ \
109 "group", /* GRPQUOTA */ \
112 #define QUOTAFILENAME ".quota"
113 #define QUOTAOPSNAME ".quota.ops"
114 #define QUOTAGROUP "operator"
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).
121 #define SUBCMDMASK 0x00ff
122 #define SUBCMDSHIFT 8
123 #define QCMD(cmd, type) (((cmd) << SUBCMDSHIFT) | ((type) & SUBCMDMASK))
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 */
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).
147 * The setquota system call establishes the vnode for each quota
148 * file (a pointer is retained in the filesystem mount structure).
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 */
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 */
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
183 #if __DARWIN_ALIGN_NATURAL
184 #pragma options align=natural
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 */
194 user_time_t dqb_btime
; /* time limit for excessive disk use */
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 */
200 #if __DARWIN_ALIGN_NATURAL
201 #pragma options align=reset
203 #endif /* KERNEL_PRIVATE */
205 #define INITQMAGICS { \
206 0xff31ff35, /* USRQUOTA */ \
207 0xff31ff27, /* GRPQUOTA */ \
211 #define QF_STRING_TAG "QUOTA HASH FILE"
213 #define QF_USERS_PER_GB 256
214 #define QF_MIN_USERS 2048
215 #define QF_MAX_USERS (2048*1024)
217 #define QF_GROUPS_PER_GB 32
218 #define QF_MIN_GROUPS 2048
219 #define QF_MAX_GROUPS (256*1024)
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.
227 #define dqhash1(id, shift, mask) \
228 ((((id) * 2654435761UL) >> (shift)) & (mask))
230 #define dqhash2(id, mask) \
231 (dqhash1((id), 11, (mask)>>1) | 1)
234 * Compute a disk offset into a quota file.
236 #define dqoffset(index) \
237 (sizeof (struct dqfilehdr) + ((index) * sizeof (struct dqblk)))
239 * Compute the hash shift value.
240 * It is the word size, in bits, minus the hash table size, in bits.
242 static __inline
int dqhashshift(u_long
);
245 dqhashshift(u_long size
)
249 for (shift
= 32; size
> 1; size
>>= 1, --shift
)
257 int quotactl(char *, int, int, caddr_t
);
261 #ifdef KERNEL_PRIVATE
262 #include <sys/queue.h>
269 lck_mtx_t qf_lock
; /* quota file mutex */
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) */
274 int qf_entrycnt
; /* count of active entries */
275 time_t qf_btime
; /* block quota time limit */
276 time_t qf_itime
; /* inode quota time limit */
278 /* the following 2 fields are protected */
279 /* by the quota list lock */
280 char qf_qflags
; /* quota specific flags */
281 int qf_refcnt
; /* count of dquot refs on this file */
285 * Flags describing the runtime state of quotas.
288 #define QTF_OPENING 0x01 /* Q_QUOTAON in progress */
289 #define QTF_CLOSING 0x02 /* Q_QUOTAOFF in progress */
290 #define QTF_WANTED 0x04 /* waiting for change of state */
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
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 */
303 u_int16_t dq_cnt
; /* count of active references */
304 u_int16_t dq_lflags
; /* protected by the quota list lock */
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 */
308 struct quotafile
*dq_qfile
; /* quota file that this is taken from */
309 struct dqblk dq_dqb
; /* actual usage & quotas */
315 #define DQ_LLOCK 0x01 /* this quota locked (no MODS) */
316 #define DQ_LWANT 0x02 /* wakeup on unlock */
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 */
327 * Shorthand notation.
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
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.
346 * Flags to chkdq() and chkiq()
348 #define FORCE 0x01 /* force usage changes independent of limits */
349 #define CHOWN 0x02 /* (advisory) change initiated by chown */
353 * Functions that manage the in-core dquot and the
354 * on-disk dqblk data structures.
357 void dqfileinit(struct quotafile
*);
358 int dqfileopen(struct quotafile
*, int);
359 void dqfileclose(struct quotafile
*, int);
360 void dqflush(struct vnode
*);
361 int dqget(u_long
, struct quotafile
*, int, struct dquot
**);
363 void dqref(struct dquot
*);
364 void dqrele(struct dquot
*);
365 void dqreclaim(struct dquot
*);
366 int dqsync(struct dquot
*);
367 void dqsync_orphans(struct quotafile
*);
368 void dqlock(struct dquot
*);
369 void dqunlock(struct dquot
*);
371 int qf_get(struct quotafile
*, int type
);
372 void qf_put(struct quotafile
*, int type
);
374 __private_extern__
void munge_dqblk(struct dqblk
*dqblkp
, struct user_dqblk
*user_dqblkp
, boolean_t to64
);
377 #endif /* KERNEL_PRIVATE */
379 #endif /* __APPLE_API_UNSTABLE */
381 #endif /* !_SYS_QUOTA_H_ */