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