xref: /openbsd/sys/ufs/ext2fs/ext2fs_dir.h (revision 91f110e0)
1 /*	$OpenBSD: ext2fs_dir.h,v 1.9 2013/06/02 06:41:12 guenther 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. Neither the name of the University nor the names of its contributors
23  *    may be used to endorse or promote products derived from this software
24  *    without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36  * SUCH DAMAGE.
37  *
38  *	@(#)dir.h	8.4 (Berkeley) 8/10/94
39  * Modified for ext2fs by Manuel Bouyer.
40  */
41 
42 #ifndef _EXT2FS_DIR_H_
43 #define	_EXT2FS_DIR_H_
44 
45 /*
46  * Theoretically, directories can be more than 2Gb in length, however, in
47  * practice this seems unlikely. So, we define the type doff_t as a 32-bit
48  * quantity to keep down the cost of doing lookup on a 32-bit machine.
49  */
50 #define	doff_t		int32_t
51 #define	EXT2FS_MAXDIRSIZE	(0x7fffffff)
52 
53 /*
54  * A directory consists of some number of blocks of e2fs_bsize bytes.
55  *
56  * Each 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 EXT2FS_MAXNAMLEN.
63  *
64  * The macro EXT2FS_DIRSIZ(fmt, dp) gives the amount of space required to
65  * represent a directory entry.  Free space in a directory is represented by
66  * entries which have dp->e2d_reclen > DIRSIZ(fmt, dp).  All d2fs_bsize 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->e2d_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->e2d_reclen.  If the first entry of
72  * a directory block is free, then its dp->e2d_ino is set to 0.
73  * Entries other than the first in a directory do not normally have
74  * dp->e2d_ino set to 0.
75  * Ext2 rev 0 has a 16 bits e2d_namlen. For Ext2 rev 1 this has been split
76  * into a 8 bits e2d_namlen and 8 bits e2d_type (looks like ffs, isnt't it ? :)
77  * It's safe to use this for rev 0 as well because all ext2 are little-endian.
78  */
79 
80 #define	EXT2FS_MAXNAMLEN	255
81 
82 struct	ext2fs_direct {
83 	u_int32_t e2d_ino;		/* inode number of entry */
84 	u_int16_t e2d_reclen;		/* length of this record */
85 	u_int8_t e2d_namlen;		/* length of string in d_name */
86 	u_int8_t e2d_type;		/* file type */
87 	char	  e2d_name[EXT2FS_MAXNAMLEN];/* name with length <= EXT2FS_MAXNAMLEN */
88 };
89 
90 /* Ext2 directory file types (not the same as FFS. Sigh. */
91 #define EXT2_FT_UNKNOWN         0
92 #define EXT2_FT_REG_FILE        1
93 #define EXT2_FT_DIR             2
94 #define EXT2_FT_CHRDEV          3
95 #define EXT2_FT_BLKDEV          4
96 #define EXT2_FT_FIFO            5
97 #define EXT2_FT_SOCK            6
98 #define EXT2_FT_SYMLINK         7
99 
100 #define EXT2_FT_MAX             8
101 
102 #define E2IFTODT(mode)    (((mode) & 0170000) >> 12)
103 
104 static __inline__ u_int8_t inot2ext2dt(u_int16_t)
105     __attribute__((__unused__));
106 static __inline__ u_int8_t
107 inot2ext2dt(u_int16_t type)
108 {
109 	switch(type) {
110 	case E2IFTODT(EXT2_IFIFO):
111 		return EXT2_FT_FIFO;
112 	case E2IFTODT(EXT2_IFCHR):
113 		return EXT2_FT_CHRDEV;
114 	case E2IFTODT(EXT2_IFDIR):
115 		return EXT2_FT_DIR;
116 	case E2IFTODT(EXT2_IFBLK):
117 		return EXT2_FT_BLKDEV;
118 	case E2IFTODT(EXT2_IFREG):
119 		return EXT2_FT_REG_FILE;
120 	case E2IFTODT(EXT2_IFLNK):
121 		return EXT2_FT_SYMLINK;
122 	case E2IFTODT(EXT2_IFSOCK):
123 		return EXT2_FT_SOCK;
124 	default:
125 		return 0;
126 	}
127 }
128 
129 /*
130  * The EXT2FS_DIRSIZ macro gives the minimum record length which will hold
131  * the directory entryfor a name len "len" (without the terminating null byte).
132  * This requires the amount of space in struct direct
133  * without the d_name field, plus enough space for the name without a
134  * terminating null byte, rounded up to a 4 byte boundary.
135  */
136 #define EXT2FS_DIRSIZ(len) \
137    (( 8 + len + 3) &~ 3)
138 
139 /*
140  * Template for manipulating directories.  Should use struct direct's,
141  * but the name field is EXT2FS_MAXNAMLEN - 1, and this just won't do.
142  */
143 struct ext2fs_dirtemplate {
144 	u_int32_t	dot_ino;
145 	int16_t		dot_reclen;
146 	u_int8_t	dot_namlen;
147 	u_int8_t	dot_type;
148 	char		dot_name[4];	/* must be multiple of 4 */
149 	u_int32_t	dotdot_ino;
150 	int16_t		dotdot_reclen;
151 	u_int8_t	dotdot_namlen;
152 	u_int8_t	dotdot_type;
153 	char		dotdot_name[4];	/* ditto */
154 };
155 
156 #endif /* !_EXT2FS_DIR_H_ */
157