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