xref: /openbsd/sys/ufs/ext2fs/ext2fs_dir.h (revision 78b63d65)
1 /*	$OpenBSD: ext2fs_dir.h,v 1.5 2001/09/18 00:25:59 art Exp $	*/
2 /*	$NetBSD: ext2fs_dir.h,v 1.4 2000/01/28 16:00:23 bouyer Exp $	*/
3 
4 /*
5  * Copyright (c) 1997 Manuel Bouyer.
6  * Copyright (c) 1982, 1986, 1989, 1993
7  *	The Regents of the University of California.  All rights reserved.
8  * (c) UNIX System Laboratories, Inc.
9  * All or some portions of this file are derived from material licensed
10  * to the University of California by American Telephone and Telegraph
11  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
12  * the permission of UNIX System Laboratories, Inc.
13  *
14  * Redistribution and use in source and binary forms, with or without
15  * modification, are permitted provided that the following conditions
16  * are met:
17  * 1. Redistributions of source code must retain the above copyright
18  *    notice, this list of conditions and the following disclaimer.
19  * 2. Redistributions in binary form must reproduce the above copyright
20  *    notice, this list of conditions and the following disclaimer in the
21  *    documentation and/or other materials provided with the distribution.
22  * 3. All advertising materials mentioning features or use of this software
23  *    must display the following acknowledgement:
24  *	This product includes software developed by the University of
25  *	California, Berkeley and its contributors.
26  * 4. Neither the name of the University nor the names of its contributors
27  *    may be used to endorse or promote products derived from this software
28  *    without specific prior written permission.
29  *
30  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
31  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
32  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
33  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
34  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
35  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
36  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
37  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
38  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
39  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
40  * SUCH DAMAGE.
41  *
42  *	@(#)dir.h	8.4 (Berkeley) 8/10/94
43  * Modified for ext2fs by Manuel Bouyer.
44  */
45 
46 #ifndef _EXT2FS_DIR_H_
47 #define	_EXT2FS_DIR_H_
48 
49 /*
50  * Theoretically, directories can be more than 2Gb in length, however, in
51  * practice this seems unlikely. So, we define the type doff_t as a 32-bit
52  * quantity to keep down the cost of doing lookup on a 32-bit machine.
53  */
54 #define	doff_t		int32_t
55 #define	EXT2FS_MAXDIRSIZE	(0x7fffffff)
56 
57 /*
58  * A directory consists of some number of blocks of e2fs_bsize bytes.
59  *
60  * Each block contains some number of directory entry
61  * structures, which are of variable length.  Each directory entry has
62  * a struct direct at the front of it, containing its inode number,
63  * the length of the entry, and the length of the name contained in
64  * the entry.  These are followed by the name padded to a 4 byte boundary
65  * with null bytes.  All names are guaranteed null terminated.
66  * The maximum length of a name in a directory is EXT2FS_MAXNAMLEN.
67  *
68  * The macro EXT2FS_DIRSIZ(fmt, dp) gives the amount of space required to
69  * represent a directory entry.  Free space in a directory is represented by
70  * entries which have dp->e2d_reclen > DIRSIZ(fmt, dp).  All d2fs_bsize bytes
71  * in a directory block are claimed by the directory entries.  This
72  * usually results in the last entry in a directory having a large
73  * dp->e2d_reclen.  When entries are deleted from a directory, the
74  * space is returned to the previous entry in the same directory
75  * block by increasing its dp->e2d_reclen.  If the first entry of
76  * a directory block is free, then its dp->e2d_ino is set to 0.
77  * Entries other than the first in a directory do not normally have
78  * dp->e2d_ino set to 0.
79  * Ext2 rev 0 has a 16 bits e2d_namlen. For Ext2 vev 1 this has been split
80  * into a 8 bits e2d_namlen and 8 bits e2d_type (looks like ffs, isnt't it ? :)
81  * It's safe to use this for rev 0 as well because all ext2 are little-endian.
82  */
83 
84 #define	EXT2FS_MAXNAMLEN	255
85 
86 struct	ext2fs_direct {
87 	u_int32_t e2d_ino;		/* inode number of entry */
88 	u_int16_t e2d_reclen;		/* length of this record */
89 	u_int8_t e2d_namlen;		/* length of string in d_name */
90 	u_int8_t e2d_type;		/* file type */
91 	char	  e2d_name[EXT2FS_MAXNAMLEN];/* name with length <= EXT2FS_MAXNAMLEN */
92 };
93 
94 /* Ext2 directory file types (not the same as FFS. Sigh. */
95 #define EXT2_FT_UNKNOWN         0
96 #define EXT2_FT_REG_FILE        1
97 #define EXT2_FT_DIR             2
98 #define EXT2_FT_CHRDEV          3
99 #define EXT2_FT_BLKDEV          4
100 #define EXT2_FT_FIFO            5
101 #define EXT2_FT_SOCK            6
102 #define EXT2_FT_SYMLINK         7
103 
104 #define EXT2_FT_MAX             8
105 
106 #define E2IFTODT(mode)    (((mode) & 0170000) >> 12)
107 
108 static __inline__ u_int8_t inot2ext2dt __P((u_int16_t))
109     __attribute__((__unused__));
110 static __inline__ u_int8_t
111 inot2ext2dt(type)
112 	u_int16_t type;
113 {
114 	switch(type) {
115 	case E2IFTODT(EXT2_IFIFO):
116 		return EXT2_FT_FIFO;
117 	case E2IFTODT(EXT2_IFCHR):
118 		return EXT2_FT_CHRDEV;
119 	case E2IFTODT(EXT2_IFDIR):
120 		return EXT2_FT_DIR;
121 	case E2IFTODT(EXT2_IFBLK):
122 		return EXT2_FT_BLKDEV;
123 	case E2IFTODT(EXT2_IFREG):
124 		return EXT2_FT_REG_FILE;
125 	case E2IFTODT(EXT2_IFLNK):
126 		return EXT2_FT_SYMLINK;
127 	case E2IFTODT(EXT2_IFSOCK):
128 		return EXT2_FT_SOCK;
129 	default:
130 		return 0;
131 	}
132 }
133 
134 /*
135  * The EXT2FS_DIRSIZ macro gives the minimum record length which will hold
136  * the directory entryfor a name len "len" (without the terminating null byte).
137  * This requires the amount of space in struct direct
138  * without the d_name field, plus enough space for the name without a
139  * terminating null byte, rounded up to a 4 byte boundary.
140  */
141 #define EXT2FS_DIRSIZ(len) \
142    (( 8 + len + 3) &~ 3)
143 
144 /*
145  * Template for manipulating directories.  Should use struct direct's,
146  * but the name field is EXT2FS_MAXNAMLEN - 1, and this just won't do.
147  */
148 struct ext2fs_dirtemplate {
149 	u_int32_t	dot_ino;
150 	int16_t		dot_reclen;
151 	u_int8_t	dot_namlen;
152 	u_int8_t	dot_type;
153 	char		dot_name[4];	/* must be multiple of 4 */
154 	u_int32_t	dotdot_ino;
155 	int16_t		dotdot_reclen;
156 	u_int8_t	dotdot_namlen;
157 	u_int8_t	dotdot_type;
158 	char		dotdot_name[4];	/* ditto */
159 };
160 
161 #endif /* !_EXT2FS_DIR_H_ */
162