xref: /freebsd/sys/ufs/ufs/dir.h (revision 29363fb4)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
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. Neither the name of the University nor the names of its contributors
21  *    may be used to endorse or promote products derived from this software
22  *    without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34  * SUCH DAMAGE.
35  */
36 
37 #ifndef _UFS_UFS_DIR_H_
38 #define	_UFS_UFS_DIR_H_
39 
40 /*
41  * Theoretically, directories can be more than 2Gb in length, however, in
42  * practice this seems unlikely. So, we define the type doff_t as a 32-bit
43  * quantity to keep down the cost of doing lookup on a 32-bit machine.
44  */
45 #define	doff_t		int32_t
46 #define	MAXDIRSIZE	(0x7fffffff)
47 
48 /*
49  * A directory consists of some number of blocks of DIRBLKSIZ
50  * bytes, where DIRBLKSIZ is chosen such that it can be transferred
51  * to disk in a single atomic operation (e.g. 512 bytes on most machines).
52  *
53  * Each DIRBLKSIZ byte block contains some number of directory entry
54  * structures, which are of variable length.  Each directory entry has
55  * a struct direct at the front of it, containing its inode number,
56  * the length of the entry, and the length of the name contained in
57  * the entry.  These are followed by the name padded to a 4 byte boundary
58  * with null bytes.  All names are guaranteed null terminated.
59  * The maximum length of a name in a directory is UFS_MAXNAMLEN.
60  *
61  * The macro DIRSIZ(fmt, dp) gives the amount of space required to represent
62  * a directory entry.  Free space in a directory is represented by
63  * entries which have dp->d_reclen > DIRSIZ(fmt, dp).  All DIRBLKSIZ bytes
64  * in a directory block are claimed by the directory entries.  This
65  * usually results in the last entry in a directory having a large
66  * dp->d_reclen.  When entries are deleted from a directory, the
67  * space is returned to the previous entry in the same directory
68  * block by increasing its dp->d_reclen.  If the first entry of
69  * a directory block is free, then its dp->d_ino is set to 0.
70  * Entries other than the first in a directory do not normally have
71  * dp->d_ino set to 0.
72  */
73 #define	DIRBLKSIZ	DEV_BSIZE
74 #define	UFS_MAXNAMLEN	255
75 
76 struct	direct {
77 	uint32_t d_ino;		/* inode number of entry */
78 	uint16_t d_reclen;		/* length of this record */
79 	uint8_t  d_type; 		/* file type, see below */
80 	uint8_t  d_namlen;		/* length of string in d_name */
81 	char	  d_name[UFS_MAXNAMLEN + 1];
82 					/* name with length <= UFS_MAXNAMLEN */
83 };
84 
85 /*
86  * File types
87  */
88 #define	DT_UNKNOWN	 0
89 #define	DT_FIFO		 1
90 #define	DT_CHR		 2
91 #define	DT_DIR		 4
92 #define	DT_BLK		 6
93 #define	DT_REG		 8
94 #define	DT_LNK		10
95 #define	DT_SOCK		12
96 #define	DT_WHT		14
97 
98 /*
99  * Convert between stat structure types and directory types.
100  */
101 #define	IFTODT(mode)	(((mode) & 0170000) >> 12)
102 #define	DTTOIF(dirtype)	((dirtype) << 12)
103 
104 /*
105  * The DIRSIZ macro gives the minimum record length which will hold
106  * the directory entry.  This requires the amount of space in struct direct
107  * without the d_name field, plus enough space for the name with a terminating
108  * null byte (dp->d_namlen + 1), rounded up to a 4 byte boundary.
109  */
110 #define	DIR_ROUNDUP	4	/* Directory name roundup size */
111 #define	DIRECTSIZ(namlen) \
112     (roundup2(__offsetof(struct direct, d_name) + (namlen) + 1, DIR_ROUNDUP))
113 #if (BYTE_ORDER == LITTLE_ENDIAN)
114 #define	DIRSIZ(oldfmt, dp) \
115     ((oldfmt) ? DIRECTSIZ((dp)->d_type) : DIRECTSIZ((dp)->d_namlen))
116 #else
117 #define	DIRSIZ(oldfmt, dp) \
118     DIRECTSIZ((dp)->d_namlen)
119 #endif
120 #define	OLDDIRFMT	1
121 #define	NEWDIRFMT	0
122 
123 /*
124  * Template for manipulating directories.  Should use struct direct's,
125  * but the name field is UFS_MAXNAMLEN - 1, and this just won't do.
126  */
127 struct dirtemplate {
128 	uint32_t	dot_ino;
129 	int16_t		dot_reclen;
130 	uint8_t	dot_type;
131 	uint8_t	dot_namlen;
132 	char		dot_name[4];	/* must be multiple of 4 */
133 	uint32_t	dotdot_ino;
134 	int16_t		dotdot_reclen;
135 	uint8_t	dotdot_type;
136 	uint8_t	dotdot_namlen;
137 	char		dotdot_name[4];	/* ditto */
138 };
139 
140 /*
141  * This is the old format of directories, sanz type element.
142  */
143 struct odirtemplate {
144 	uint32_t	dot_ino;
145 	int16_t		dot_reclen;
146 	uint16_t	dot_namlen;
147 	char		dot_name[4];	/* must be multiple of 4 */
148 	uint32_t	dotdot_ino;
149 	int16_t		dotdot_reclen;
150 	uint16_t	dotdot_namlen;
151 	char		dotdot_name[4];	/* ditto */
152 };
153 #endif /* !_DIR_H_ */
154