xref: /netbsd/sys/ufs/ufs/dir.h (revision c4a72b64)
1 /*	$NetBSD: dir.h,v 1.15 2002/12/01 00:12:11 matt Exp $	*/
2 
3 /*
4  * Copyright (c) 1982, 1986, 1989, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  * (c) UNIX System Laboratories, Inc.
7  * All or some portions of this file are derived from material licensed
8  * to the University of California by American Telephone and Telegraph
9  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
10  * the permission of UNIX System Laboratories, Inc.
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
15  * 1. Redistributions of source code must retain the above copyright
16  *    notice, this list of conditions and the following disclaimer.
17  * 2. Redistributions in binary form must reproduce the above copyright
18  *    notice, this list of conditions and the following disclaimer in the
19  *    documentation and/or other materials provided with the distribution.
20  * 3. All advertising materials mentioning features or use of this software
21  *    must display the following acknowledgement:
22  *	This product includes software developed by the University of
23  *	California, Berkeley and its contributors.
24  * 4. Neither the name of the University nor the names of its contributors
25  *    may be used to endorse or promote products derived from this software
26  *    without specific prior written permission.
27  *
28  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
29  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
32  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38  * SUCH DAMAGE.
39  *
40  *	@(#)dir.h	8.5 (Berkeley) 4/27/95
41  */
42 
43 #ifndef _UFS_UFS_DIR_H_
44 #define	_UFS_UFS_DIR_H_
45 
46 /*
47  * Theoretically, directories can be more than 2Gb in length, however, in
48  * practice this seems unlikely. So, we define the type doff_t as a 32-bit
49  * quantity to keep down the cost of doing lookup on a 32-bit machine.
50  */
51 #define	doff_t		int32_t
52 #define	MAXDIRSIZE	(0x7fffffff)
53 
54 /*
55  * A directory consists of some number of blocks of DIRBLKSIZ
56  * bytes, where DIRBLKSIZ is chosen such that it can be transferred
57  * to disk in a single atomic operation (e.g. 512 bytes on most machines).
58  *
59  * Each DIRBLKSIZ byte block contains some number of directory entry
60  * structures, which are of variable length.  Each directory entry has
61  * a struct direct at the front of it, containing its inode number,
62  * the length of the entry, and the length of the name contained in
63  * the entry.  These are followed by the name padded to a 4 byte boundary
64  * with null bytes.  All names are guaranteed null terminated.
65  * The maximum length of a name in a directory is MAXNAMLEN.
66  *
67  * The macro DIRSIZ(fmt, dp) gives the amount of space required to represent
68  * a directory entry.  Free space in a directory is represented by
69  * entries which have dp->d_reclen > DIRSIZ(fmt, dp).  All DIRBLKSIZ bytes
70  * in a directory block are claimed by the directory entries.  This
71  * usually results in the last entry in a directory having a large
72  * dp->d_reclen.  When entries are deleted from a directory, the
73  * space is returned to the previous entry in the same directory
74  * block by increasing its dp->d_reclen.  If the first entry of
75  * a directory block is free, then its dp->d_ino is set to 0.
76  * Entries other than the first in a directory do not normally have
77  * dp->d_ino set to 0.
78  */
79 #undef	DIRBLKSIZ
80 #define	DIRBLKSIZ	DEV_BSIZE
81 #undef	MAXNAMLEN
82 #define	MAXNAMLEN	255
83 #define APPLEUFS_DIRBLKSIZ 1024
84 
85 struct	direct {
86 	u_int32_t d_ino;		/* inode number of entry */
87 	u_int16_t d_reclen;		/* length of this record */
88 	u_int8_t  d_type; 		/* file type, see below */
89 	u_int8_t  d_namlen;		/* length of string in d_name */
90 	char	  d_name[MAXNAMLEN + 1];/* name with length <= MAXNAMLEN */
91 };
92 
93 /*
94  * File types
95  */
96 #define	DT_UNKNOWN	 0
97 #define	DT_FIFO		 1
98 #define	DT_CHR		 2
99 #define	DT_DIR		 4
100 #define	DT_BLK		 6
101 #define	DT_REG		 8
102 #define	DT_LNK		10
103 #define	DT_SOCK		12
104 #define	DT_WHT		14
105 
106 /*
107  * Convert between stat structure types and directory types.
108  */
109 #define	IFTODT(mode)	(((mode) & 0170000) >> 12)
110 #define	DTTOIF(dirtype)	((dirtype) << 12)
111 
112 /*
113  * The DIRSIZ macro gives the minimum record length which will hold
114  * the directory entry.  This requires the amount of space in struct direct
115  * without the d_name field, plus enough space for the name with a terminating
116  * null byte (dp->d_namlen+1), rounded up to a 4 byte boundary.
117  */
118 #define	DIRECTSIZ(namlen) \
119 	((sizeof(struct direct) - (MAXNAMLEN+1)) + (((namlen)+1 + 3) &~ 3))
120 
121 #if (BYTE_ORDER == LITTLE_ENDIAN)
122 #define DIRSIZ(oldfmt, dp, needswap)	\
123     (((oldfmt) && !(needswap)) ?	\
124     DIRECTSIZ((dp)->d_type) : DIRECTSIZ((dp)->d_namlen))
125 #else
126 #define DIRSIZ(oldfmt, dp, needswap)	\
127     (((oldfmt) && (needswap)) ?		\
128     DIRECTSIZ((dp)->d_type) : DIRECTSIZ((dp)->d_namlen))
129 #endif
130 
131 #define OLDDIRFMT	1
132 #define NEWDIRFMT	0
133 
134 /*
135  * Template for manipulating directories.  Should use struct direct's,
136  * but the name field is MAXNAMLEN - 1, and this just won't do.
137  */
138 struct dirtemplate {
139 	u_int32_t	dot_ino;
140 	int16_t		dot_reclen;
141 	u_int8_t	dot_type;
142 	u_int8_t	dot_namlen;
143 	char		dot_name[4];	/* must be multiple of 4 */
144 	u_int32_t	dotdot_ino;
145 	int16_t		dotdot_reclen;
146 	u_int8_t	dotdot_type;
147 	u_int8_t	dotdot_namlen;
148 	char		dotdot_name[4];	/* ditto */
149 };
150 
151 /*
152  * This is the old format of directories, sanz type element.
153  */
154 struct odirtemplate {
155 	u_int32_t	dot_ino;
156 	int16_t		dot_reclen;
157 	u_int16_t	dot_namlen;
158 	char		dot_name[4];	/* must be multiple of 4 */
159 	u_int32_t	dotdot_ino;
160 	int16_t		dotdot_reclen;
161 	u_int16_t	dotdot_namlen;
162 	char		dotdot_name[4];	/* ditto */
163 };
164 #endif /* !_UFS_UFS_DIR_H_ */
165