xref: /illumos-gate/usr/src/boot/include/ufs/ufs/dir.h (revision 8130c4e1)
1199767f8SToomas Soome /*-
2199767f8SToomas Soome  * Copyright (c) 1982, 1986, 1989, 1993
3199767f8SToomas Soome  *	The Regents of the University of California.  All rights reserved.
4199767f8SToomas Soome  * (c) UNIX System Laboratories, Inc.
5199767f8SToomas Soome  * All or some portions of this file are derived from material licensed
6199767f8SToomas Soome  * to the University of California by American Telephone and Telegraph
7199767f8SToomas Soome  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
8199767f8SToomas Soome  * the permission of UNIX System Laboratories, Inc.
9199767f8SToomas Soome  *
10199767f8SToomas Soome  * Redistribution and use in source and binary forms, with or without
11199767f8SToomas Soome  * modification, are permitted provided that the following conditions
12199767f8SToomas Soome  * are met:
13199767f8SToomas Soome  * 1. Redistributions of source code must retain the above copyright
14199767f8SToomas Soome  *    notice, this list of conditions and the following disclaimer.
15199767f8SToomas Soome  * 2. Redistributions in binary form must reproduce the above copyright
16199767f8SToomas Soome  *    notice, this list of conditions and the following disclaimer in the
17199767f8SToomas Soome  *    documentation and/or other materials provided with the distribution.
18199767f8SToomas Soome  * 4. Neither the name of the University nor the names of its contributors
19199767f8SToomas Soome  *    may be used to endorse or promote products derived from this software
20199767f8SToomas Soome  *    without specific prior written permission.
21199767f8SToomas Soome  *
22199767f8SToomas Soome  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23199767f8SToomas Soome  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24199767f8SToomas Soome  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25199767f8SToomas Soome  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26199767f8SToomas Soome  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27199767f8SToomas Soome  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28199767f8SToomas Soome  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29199767f8SToomas Soome  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30199767f8SToomas Soome  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31199767f8SToomas Soome  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32199767f8SToomas Soome  * SUCH DAMAGE.
33199767f8SToomas Soome  *
34199767f8SToomas Soome  *	@(#)dir.h	8.2 (Berkeley) 1/21/94
35199767f8SToomas Soome  */
36199767f8SToomas Soome 
37199767f8SToomas Soome #ifndef _UFS_UFS_DIR_H_
38199767f8SToomas Soome #define	_UFS_UFS_DIR_H_
39199767f8SToomas Soome 
40199767f8SToomas Soome /*
41199767f8SToomas Soome  * Theoretically, directories can be more than 2Gb in length, however, in
42199767f8SToomas Soome  * practice this seems unlikely. So, we define the type doff_t as a 32-bit
43199767f8SToomas Soome  * quantity to keep down the cost of doing lookup on a 32-bit machine.
44199767f8SToomas Soome  */
45199767f8SToomas Soome #define	doff_t		int32_t
46199767f8SToomas Soome #define	MAXDIRSIZE	(0x7fffffff)
47199767f8SToomas Soome 
48199767f8SToomas Soome /*
49199767f8SToomas Soome  * A directory consists of some number of blocks of DIRBLKSIZ
50199767f8SToomas Soome  * bytes, where DIRBLKSIZ is chosen such that it can be transferred
51199767f8SToomas Soome  * to disk in a single atomic operation (e.g. 512 bytes on most machines).
52199767f8SToomas Soome  *
53199767f8SToomas Soome  * Each DIRBLKSIZ byte block contains some number of directory entry
54199767f8SToomas Soome  * structures, which are of variable length.  Each directory entry has
55199767f8SToomas Soome  * a struct direct at the front of it, containing its inode number,
56199767f8SToomas Soome  * the length of the entry, and the length of the name contained in
57199767f8SToomas Soome  * the entry.  These are followed by the name padded to a 4 byte boundary
58199767f8SToomas Soome  * with null bytes.  All names are guaranteed null terminated.
59*8130c4e1SToomas Soome  * The maximum length of a name in a directory is UFS_MAXNAMLEN.
60199767f8SToomas Soome  *
61199767f8SToomas Soome  * The macro DIRSIZ(fmt, dp) gives the amount of space required to represent
62199767f8SToomas Soome  * a directory entry.  Free space in a directory is represented by
63199767f8SToomas Soome  * entries which have dp->d_reclen > DIRSIZ(fmt, dp).  All DIRBLKSIZ bytes
64199767f8SToomas Soome  * in a directory block are claimed by the directory entries.  This
65199767f8SToomas Soome  * usually results in the last entry in a directory having a large
66199767f8SToomas Soome  * dp->d_reclen.  When entries are deleted from a directory, the
67199767f8SToomas Soome  * space is returned to the previous entry in the same directory
68199767f8SToomas Soome  * block by increasing its dp->d_reclen.  If the first entry of
69199767f8SToomas Soome  * a directory block is free, then its dp->d_ino is set to 0.
70199767f8SToomas Soome  * Entries other than the first in a directory do not normally have
71199767f8SToomas Soome  * dp->d_ino set to 0.
72199767f8SToomas Soome  */
73199767f8SToomas Soome #define	DIRBLKSIZ	DEV_BSIZE
74*8130c4e1SToomas Soome #define	UFS_MAXNAMLEN	255
75199767f8SToomas Soome 
76199767f8SToomas Soome struct	direct {
77199767f8SToomas Soome 	u_int32_t d_ino;		/* inode number of entry */
78199767f8SToomas Soome 	u_int16_t d_reclen;		/* length of this record */
79199767f8SToomas Soome 	u_int16_t d_namlen;		/* length of string in d_name */
80*8130c4e1SToomas Soome 	char	  d_name[UFS_MAXNAMLEN + 1];
81*8130c4e1SToomas Soome 				/* name with length <= UFS_MAXNAMLEN */
82199767f8SToomas Soome };
83199767f8SToomas Soome 
84199767f8SToomas Soome /*
85199767f8SToomas Soome  * File types
86199767f8SToomas Soome  */
87199767f8SToomas Soome #define	DT_UNKNOWN	 0
88199767f8SToomas Soome #define	DT_FIFO		 1
89199767f8SToomas Soome #define	DT_CHR		 2
90199767f8SToomas Soome #define	DT_DIR		 4
91199767f8SToomas Soome #define	DT_BLK		 6
92199767f8SToomas Soome #define	DT_REG		 8
93199767f8SToomas Soome #define	DT_LNK		10
94199767f8SToomas Soome #define	DT_SOCK		12
95199767f8SToomas Soome #define	DT_WHT		14
96199767f8SToomas Soome 
97199767f8SToomas Soome /*
98199767f8SToomas Soome  * Convert between stat structure types and directory types.
99199767f8SToomas Soome  */
100199767f8SToomas Soome #define	IFTODT(mode)	(((mode) & 0170000) >> 12)
101199767f8SToomas Soome #define	DTTOIF(dirtype)	((dirtype) << 12)
102199767f8SToomas Soome 
103199767f8SToomas Soome /*
104199767f8SToomas Soome  * The DIRSIZ macro gives the minimum record length which will hold
105199767f8SToomas Soome  * the directory entry.  This requires the amount of space in struct direct
106199767f8SToomas Soome  * without the d_name field, plus enough space for the name with a terminating
107199767f8SToomas Soome  * null byte (dp->d_namlen+1), rounded up to a 4 byte boundary.
108199767f8SToomas Soome  *
109199767f8SToomas Soome  *
110199767f8SToomas Soome  */
111199767f8SToomas Soome #define	DIRECTSIZ(namlen)						\
112199767f8SToomas Soome 	((__offsetof(struct direct, d_name) +				\
113199767f8SToomas Soome 	  ((namlen)+1)*sizeof(((struct direct *)0)->d_name[0]) + 3) & ~3)
114199767f8SToomas Soome #if (BYTE_ORDER == LITTLE_ENDIAN)
115199767f8SToomas Soome #define	DIRSIZ(oldfmt, dp) \
116199767f8SToomas Soome     ((oldfmt) ? DIRECTSIZ((dp)->d_type) : DIRECTSIZ((dp)->d_namlen))
117199767f8SToomas Soome #else
118199767f8SToomas Soome #define	DIRSIZ(oldfmt, dp) \
119199767f8SToomas Soome     DIRECTSIZ((dp)->d_namlen)
120199767f8SToomas Soome #endif
121199767f8SToomas Soome #define	OLDDIRFMT	1
122199767f8SToomas Soome #define	NEWDIRFMT	0
123199767f8SToomas Soome 
124199767f8SToomas Soome /*
125199767f8SToomas Soome  * Template for manipulating directories.  Should use struct direct's,
126*8130c4e1SToomas Soome  * but the name field is UFS_MAXNAMLEN - 1, and this just won't do.
127199767f8SToomas Soome  */
128199767f8SToomas Soome struct dirtemplate {
129199767f8SToomas Soome 	u_int32_t	dot_ino;
130199767f8SToomas Soome 	int16_t		dot_reclen;
131199767f8SToomas Soome 	u_int8_t	dot_type;
132199767f8SToomas Soome 	u_int8_t	dot_namlen;
133199767f8SToomas Soome 	char		dot_name[4];	/* must be multiple of 4 */
134199767f8SToomas Soome 	u_int32_t	dotdot_ino;
135199767f8SToomas Soome 	int16_t		dotdot_reclen;
136199767f8SToomas Soome 	u_int8_t	dotdot_type;
137199767f8SToomas Soome 	u_int8_t	dotdot_namlen;
138199767f8SToomas Soome 	char		dotdot_name[4];	/* ditto */
139199767f8SToomas Soome };
140199767f8SToomas Soome 
141199767f8SToomas Soome /*
142199767f8SToomas Soome  * This is the old format of directories, sanz type element.
143199767f8SToomas Soome  */
144199767f8SToomas Soome struct odirtemplate {
145199767f8SToomas Soome 	u_int32_t	dot_ino;
146199767f8SToomas Soome 	int16_t		dot_reclen;
147199767f8SToomas Soome 	u_int16_t	dot_namlen;
148199767f8SToomas Soome 	char		dot_name[4];	/* must be multiple of 4 */
149199767f8SToomas Soome 	u_int32_t	dotdot_ino;
150199767f8SToomas Soome 	int16_t		dotdot_reclen;
151199767f8SToomas Soome 	u_int16_t	dotdot_namlen;
152199767f8SToomas Soome 	char		dotdot_name[4];	/* ditto */
153199767f8SToomas Soome };
154199767f8SToomas Soome #endif /* !_DIR_H_ */
155