xref: /minix3/sys/ufs/ext2fs/ext2fs_dir.h (revision 84d9c625bfea59e274550651111ae9edfdc40fbd)
1 /*	$NetBSD: ext2fs_dir.h,v 1.19 2012/05/09 00:21:18 riastradh 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. 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  *	@(#)dir.h	8.4 (Berkeley) 8/10/94
37  * Modified for ext2fs by Manuel Bouyer.
38  */
39 
40 /*
41  * Copyright (c) 1997 Manuel Bouyer.
42  *
43  * Redistribution and use in source and binary forms, with or without
44  * modification, are permitted provided that the following conditions
45  * are met:
46  * 1. Redistributions of source code must retain the above copyright
47  *    notice, this list of conditions and the following disclaimer.
48  * 2. Redistributions in binary form must reproduce the above copyright
49  *    notice, this list of conditions and the following disclaimer in the
50  *    documentation and/or other materials provided with the distribution.
51  *
52  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
53  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
54  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
55  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
56  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
57  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
58  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
59  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
60  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
61  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
62  *
63  *	@(#)dir.h	8.4 (Berkeley) 8/10/94
64  * Modified for ext2fs by Manuel Bouyer.
65  */
66 
67 #ifndef _UFS_EXT2FS_EXT2FS_DIR_H_
68 #define	_UFS_EXT2FS_EXT2FS_DIR_H_
69 
70 #include <ufs/ext2fs/ext2fs_dinode.h>
71 
72 /*
73  * Theoretically, directories can be more than 2Gb in length, however, in
74  * practice this seems unlikely. So, we define the type doff_t as a 32-bit
75  * quantity to keep down the cost of doing lookup on a 32-bit machine.
76  */
77 #define	doff_t			int32_t
78 #define	EXT2FS_MAXDIRSIZE	INT32_MAX
79 
80 /*
81  * A directory consists of some number of blocks of e2fs_bsize bytes.
82  *
83  * Each block contains some number of directory entry
84  * structures, which are of variable length.  Each directory entry has
85  * a struct direct at the front of it, containing its inode number,
86  * the length of the entry, and the length of the name contained in
87  * the entry.  These are followed by the name padded to a 4 byte boundary
88  * with null bytes.  All names are guaranteed null terminated.
89  * The maximum length of a name in a directory is EXT2FS_MAXNAMLEN.
90  *
91  * The macro EXT2FS_DIRSIZ(fmt, dp) gives the amount of space required to
92  * represent a directory entry.  Free space in a directory is represented by
93  * entries which have dp->e2d_reclen > DIRSIZ(fmt, dp).  All d2fs_bsize bytes
94  * in a directory block are claimed by the directory entries.  This
95  * usually results in the last entry in a directory having a large
96  * dp->e2d_reclen.  When entries are deleted from a directory, the
97  * space is returned to the previous entry in the same directory
98  * block by increasing its dp->e2d_reclen.  If the first entry of
99  * a directory block is free, then its dp->e2d_ino is set to 0.
100  * Entries other than the first in a directory do not normally have
101  * dp->e2d_ino set to 0.
102  * Ext2 rev 0 has a 16 bits e2d_namlen. For Ext2 vev 1 this has been split
103  * into a 8 bits e2d_namlen and 8 bits e2d_type (looks like ffs, isnt't it ? :)
104  * It's safe to use this for rev 0 as well because all ext2 are little-endian.
105  */
106 
107 #define	EXT2FS_MAXNAMLEN	255
108 
109 struct	ext2fs_direct {
110 	uint32_t e2d_ino;		/* inode number of entry */
111 	uint16_t e2d_reclen;		/* length of this record */
112 	uint8_t e2d_namlen;		/* length of string in d_name */
113 	uint8_t e2d_type;		/* file type */
114 	char e2d_name[EXT2FS_MAXNAMLEN];/* name with length<=EXT2FS_MAXNAMLEN */
115 };
116 
117 /* Ext2 directory file types (not the same as FFS. Sigh.) */
118 #define EXT2_FT_UNKNOWN         0
119 #define EXT2_FT_REG_FILE        1
120 #define EXT2_FT_DIR             2
121 #define EXT2_FT_CHRDEV          3
122 #define EXT2_FT_BLKDEV          4
123 #define EXT2_FT_FIFO            5
124 #define EXT2_FT_SOCK            6
125 #define EXT2_FT_SYMLINK         7
126 
127 #define EXT2_FT_MAX             8
128 
129 #define E2IFTODT(mode)    (((mode) & 0170000) >> 12)
130 
131 static __inline uint8_t inot2ext2dt(uint16_t) __unused;
132 static __inline uint8_t
inot2ext2dt(uint16_t type)133 inot2ext2dt(uint16_t type)
134 {
135 
136 	switch (type) {
137 	case E2IFTODT(EXT2_IFIFO):
138 		return EXT2_FT_FIFO;
139 	case E2IFTODT(EXT2_IFCHR):
140 		return EXT2_FT_CHRDEV;
141 	case E2IFTODT(EXT2_IFDIR):
142 		return EXT2_FT_DIR;
143 	case E2IFTODT(EXT2_IFBLK):
144 		return EXT2_FT_BLKDEV;
145 	case E2IFTODT(EXT2_IFREG):
146 		return EXT2_FT_REG_FILE;
147 	case E2IFTODT(EXT2_IFLNK):
148 		return EXT2_FT_SYMLINK;
149 	case E2IFTODT(EXT2_IFSOCK):
150 		return EXT2_FT_SOCK;
151 	default:
152 		return 0;
153 	}
154 }
155 
156 /*
157  * The EXT2FS_DIRSIZ macro gives the minimum record length which will hold
158  * the directory entryfor a name len "len" (without the terminating null byte).
159  * This requires the amount of space in struct direct
160  * without the d_name field, plus enough space for the name without a
161  * terminating null byte, rounded up to a 4 byte boundary.
162  */
163 #define EXT2FS_DIRSIZ(len)	roundup2(8 + len, 4)
164 
165 /*
166  * Template for manipulating directories.  Should use struct direct's,
167  * but the name field is EXT2FS_MAXNAMLEN - 1, and this just won't do.
168  */
169 struct ext2fs_dirtemplate {
170 	uint32_t	dot_ino;
171 	int16_t		dot_reclen;
172 	uint8_t		dot_namlen;
173 	uint8_t		dot_type;
174 	char		dot_name[4];	/* must be multiple of 4 */
175 	uint32_t	dotdot_ino;
176 	int16_t		dotdot_reclen;
177 	uint8_t		dotdot_namlen;
178 	uint8_t		dotdot_type;
179 	char		dotdot_name[4];	/* ditto */
180 };
181 
182 #endif /* !_UFS_EXT2FS_EXT2FS_DIR_H_ */
183