ext2fs_dir.h revision 1.22 1 1.22 kre /* $NetBSD: ext2fs_dir.h,v 1.22 2016/08/07 01:47:11 kre Exp $ */
2 1.1 bouyer
3 1.1 bouyer /*
4 1.1 bouyer * Copyright (c) 1982, 1986, 1989, 1993
5 1.1 bouyer * The Regents of the University of California. All rights reserved.
6 1.1 bouyer * (c) UNIX System Laboratories, Inc.
7 1.1 bouyer * All or some portions of this file are derived from material licensed
8 1.1 bouyer * to the University of California by American Telephone and Telegraph
9 1.1 bouyer * Co. or Unix System Laboratories, Inc. and are reproduced herein with
10 1.1 bouyer * the permission of UNIX System Laboratories, Inc.
11 1.6 agc *
12 1.6 agc * Redistribution and use in source and binary forms, with or without
13 1.6 agc * modification, are permitted provided that the following conditions
14 1.6 agc * are met:
15 1.6 agc * 1. Redistributions of source code must retain the above copyright
16 1.6 agc * notice, this list of conditions and the following disclaimer.
17 1.6 agc * 2. Redistributions in binary form must reproduce the above copyright
18 1.6 agc * notice, this list of conditions and the following disclaimer in the
19 1.6 agc * documentation and/or other materials provided with the distribution.
20 1.6 agc * 3. Neither the name of the University nor the names of its contributors
21 1.6 agc * may be used to endorse or promote products derived from this software
22 1.6 agc * without specific prior written permission.
23 1.6 agc *
24 1.6 agc * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 1.6 agc * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 1.6 agc * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 1.6 agc * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 1.6 agc * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 1.6 agc * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 1.6 agc * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 1.6 agc * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 1.6 agc * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 1.6 agc * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 1.6 agc * SUCH DAMAGE.
35 1.6 agc *
36 1.6 agc * @(#)dir.h 8.4 (Berkeley) 8/10/94
37 1.6 agc * Modified for ext2fs by Manuel Bouyer.
38 1.6 agc */
39 1.6 agc
40 1.6 agc /*
41 1.6 agc * Copyright (c) 1997 Manuel Bouyer.
42 1.1 bouyer *
43 1.1 bouyer * Redistribution and use in source and binary forms, with or without
44 1.1 bouyer * modification, are permitted provided that the following conditions
45 1.1 bouyer * are met:
46 1.1 bouyer * 1. Redistributions of source code must retain the above copyright
47 1.1 bouyer * notice, this list of conditions and the following disclaimer.
48 1.1 bouyer * 2. Redistributions in binary form must reproduce the above copyright
49 1.1 bouyer * notice, this list of conditions and the following disclaimer in the
50 1.1 bouyer * documentation and/or other materials provided with the distribution.
51 1.1 bouyer *
52 1.8 bouyer * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
53 1.8 bouyer * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
54 1.8 bouyer * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
55 1.8 bouyer * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
56 1.8 bouyer * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
57 1.8 bouyer * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
58 1.8 bouyer * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
59 1.8 bouyer * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
60 1.8 bouyer * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
61 1.8 bouyer * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
62 1.1 bouyer *
63 1.1 bouyer * @(#)dir.h 8.4 (Berkeley) 8/10/94
64 1.1 bouyer * Modified for ext2fs by Manuel Bouyer.
65 1.1 bouyer */
66 1.1 bouyer
67 1.5 matt #ifndef _UFS_EXT2FS_EXT2FS_DIR_H_
68 1.5 matt #define _UFS_EXT2FS_EXT2FS_DIR_H_
69 1.1 bouyer
70 1.22 kre #include <sys/dirent.h>
71 1.19 riastrad #include <ufs/ext2fs/ext2fs_dinode.h>
72 1.19 riastrad
73 1.1 bouyer /*
74 1.1 bouyer * Theoretically, directories can be more than 2Gb in length, however, in
75 1.1 bouyer * practice this seems unlikely. So, we define the type doff_t as a 32-bit
76 1.1 bouyer * quantity to keep down the cost of doing lookup on a 32-bit machine.
77 1.2 christos */
78 1.14 tsutsui #define doff_t int32_t
79 1.17 tsutsui #define EXT2FS_MAXDIRSIZE INT32_MAX
80 1.1 bouyer
81 1.1 bouyer /*
82 1.1 bouyer * A directory consists of some number of blocks of e2fs_bsize bytes.
83 1.1 bouyer *
84 1.1 bouyer * Each block contains some number of directory entry
85 1.1 bouyer * structures, which are of variable length. Each directory entry has
86 1.1 bouyer * a struct direct at the front of it, containing its inode number,
87 1.1 bouyer * the length of the entry, and the length of the name contained in
88 1.1 bouyer * the entry. These are followed by the name padded to a 4 byte boundary
89 1.1 bouyer * with null bytes. All names are guaranteed null terminated.
90 1.1 bouyer * The maximum length of a name in a directory is EXT2FS_MAXNAMLEN.
91 1.1 bouyer *
92 1.1 bouyer * The macro EXT2FS_DIRSIZ(fmt, dp) gives the amount of space required to
93 1.1 bouyer * represent a directory entry. Free space in a directory is represented by
94 1.1 bouyer * entries which have dp->e2d_reclen > DIRSIZ(fmt, dp). All d2fs_bsize bytes
95 1.1 bouyer * in a directory block are claimed by the directory entries. This
96 1.1 bouyer * usually results in the last entry in a directory having a large
97 1.1 bouyer * dp->e2d_reclen. When entries are deleted from a directory, the
98 1.1 bouyer * space is returned to the previous entry in the same directory
99 1.1 bouyer * block by increasing its dp->e2d_reclen. If the first entry of
100 1.1 bouyer * a directory block is free, then its dp->e2d_ino is set to 0.
101 1.1 bouyer * Entries other than the first in a directory do not normally have
102 1.1 bouyer * dp->e2d_ino set to 0.
103 1.3 bouyer * Ext2 rev 0 has a 16 bits e2d_namlen. For Ext2 vev 1 this has been split
104 1.3 bouyer * into a 8 bits e2d_namlen and 8 bits e2d_type (looks like ffs, isnt't it ? :)
105 1.3 bouyer * It's safe to use this for rev 0 as well because all ext2 are little-endian.
106 1.1 bouyer */
107 1.1 bouyer
108 1.1 bouyer #define EXT2FS_MAXNAMLEN 255
109 1.1 bouyer
110 1.1 bouyer struct ext2fs_direct {
111 1.16 tsutsui uint32_t e2d_ino; /* inode number of entry */
112 1.16 tsutsui uint16_t e2d_reclen; /* length of this record */
113 1.16 tsutsui uint8_t e2d_namlen; /* length of string in d_name */
114 1.16 tsutsui uint8_t e2d_type; /* file type */
115 1.14 tsutsui char e2d_name[EXT2FS_MAXNAMLEN];/* name with length<=EXT2FS_MAXNAMLEN */
116 1.1 bouyer };
117 1.4 bouyer
118 1.20 christos enum ext2fs_slotstatus {
119 1.20 christos NONE,
120 1.20 christos COMPACT,
121 1.20 christos FOUND
122 1.20 christos };
123 1.20 christos
124 1.20 christos struct ext2fs_searchslot {
125 1.20 christos enum ext2fs_slotstatus slotstatus;
126 1.20 christos doff_t slotoffset; /* offset of area with free space */
127 1.20 christos int slotsize; /* size of area at slotoffset */
128 1.20 christos int slotfreespace; /* amount of space free in slot */
129 1.20 christos int slotneeded; /* sizeof the entry we are seeking */
130 1.20 christos };
131 1.20 christos
132 1.17 tsutsui /* Ext2 directory file types (not the same as FFS. Sigh.) */
133 1.4 bouyer #define EXT2_FT_UNKNOWN 0
134 1.9 perry #define EXT2_FT_REG_FILE 1
135 1.4 bouyer #define EXT2_FT_DIR 2
136 1.4 bouyer #define EXT2_FT_CHRDEV 3
137 1.4 bouyer #define EXT2_FT_BLKDEV 4
138 1.4 bouyer #define EXT2_FT_FIFO 5
139 1.4 bouyer #define EXT2_FT_SOCK 6
140 1.4 bouyer #define EXT2_FT_SYMLINK 7
141 1.4 bouyer
142 1.4 bouyer #define EXT2_FT_MAX 8
143 1.4 bouyer
144 1.4 bouyer #define E2IFTODT(mode) (((mode) & 0170000) >> 12)
145 1.4 bouyer
146 1.16 tsutsui static __inline uint8_t inot2ext2dt(uint16_t) __unused;
147 1.16 tsutsui static __inline uint8_t
148 1.16 tsutsui inot2ext2dt(uint16_t type)
149 1.4 bouyer {
150 1.17 tsutsui
151 1.17 tsutsui switch (type) {
152 1.4 bouyer case E2IFTODT(EXT2_IFIFO):
153 1.4 bouyer return EXT2_FT_FIFO;
154 1.4 bouyer case E2IFTODT(EXT2_IFCHR):
155 1.4 bouyer return EXT2_FT_CHRDEV;
156 1.4 bouyer case E2IFTODT(EXT2_IFDIR):
157 1.4 bouyer return EXT2_FT_DIR;
158 1.4 bouyer case E2IFTODT(EXT2_IFBLK):
159 1.4 bouyer return EXT2_FT_BLKDEV;
160 1.4 bouyer case E2IFTODT(EXT2_IFREG):
161 1.4 bouyer return EXT2_FT_REG_FILE;
162 1.4 bouyer case E2IFTODT(EXT2_IFLNK):
163 1.4 bouyer return EXT2_FT_SYMLINK;
164 1.4 bouyer case E2IFTODT(EXT2_IFSOCK):
165 1.4 bouyer return EXT2_FT_SOCK;
166 1.4 bouyer default:
167 1.4 bouyer return 0;
168 1.4 bouyer }
169 1.4 bouyer }
170 1.1 bouyer
171 1.21 jdolecek static __inline uint8_t ext2dt2dt(uint8_t) __unused;
172 1.21 jdolecek static __inline uint8_t
173 1.21 jdolecek ext2dt2dt(uint8_t type)
174 1.21 jdolecek {
175 1.21 jdolecek switch (type) {
176 1.21 jdolecek case EXT2_FT_REG_FILE:
177 1.21 jdolecek return DT_FIFO;
178 1.21 jdolecek case EXT2_FT_DIR:
179 1.21 jdolecek return DT_DIR;
180 1.21 jdolecek case EXT2_FT_CHRDEV:
181 1.21 jdolecek return DT_CHR;
182 1.21 jdolecek case EXT2_FT_BLKDEV:
183 1.21 jdolecek return DT_BLK;
184 1.21 jdolecek case EXT2_FT_FIFO:
185 1.21 jdolecek return DT_FIFO;
186 1.21 jdolecek case EXT2_FT_SOCK:
187 1.21 jdolecek return DT_SOCK;
188 1.21 jdolecek case EXT2_FT_SYMLINK:
189 1.21 jdolecek return DT_LNK;
190 1.21 jdolecek default:
191 1.21 jdolecek return DT_UNKNOWN;
192 1.21 jdolecek }
193 1.21 jdolecek }
194 1.21 jdolecek
195 1.1 bouyer /*
196 1.1 bouyer * The EXT2FS_DIRSIZ macro gives the minimum record length which will hold
197 1.1 bouyer * the directory entryfor a name len "len" (without the terminating null byte).
198 1.1 bouyer * This requires the amount of space in struct direct
199 1.1 bouyer * without the d_name field, plus enough space for the name without a
200 1.1 bouyer * terminating null byte, rounded up to a 4 byte boundary.
201 1.1 bouyer */
202 1.17 tsutsui #define EXT2FS_DIRSIZ(len) roundup2(8 + len, 4)
203 1.1 bouyer
204 1.1 bouyer /*
205 1.1 bouyer * Template for manipulating directories. Should use struct direct's,
206 1.1 bouyer * but the name field is EXT2FS_MAXNAMLEN - 1, and this just won't do.
207 1.1 bouyer */
208 1.1 bouyer struct ext2fs_dirtemplate {
209 1.16 tsutsui uint32_t dot_ino;
210 1.1 bouyer int16_t dot_reclen;
211 1.16 tsutsui uint8_t dot_namlen;
212 1.16 tsutsui uint8_t dot_type;
213 1.1 bouyer char dot_name[4]; /* must be multiple of 4 */
214 1.16 tsutsui uint32_t dotdot_ino;
215 1.1 bouyer int16_t dotdot_reclen;
216 1.16 tsutsui uint8_t dotdot_namlen;
217 1.16 tsutsui uint8_t dotdot_type;
218 1.1 bouyer char dotdot_name[4]; /* ditto */
219 1.1 bouyer };
220 1.1 bouyer
221 1.20 christos /*
222 1.20 christos * EXT2_DIR_PAD defines the directory entries boundaries
223 1.20 christos *
224 1.20 christos * NOTE: It must be a multiple of 4
225 1.20 christos */
226 1.20 christos #define EXT2_DIR_PAD 4
227 1.20 christos #define EXT2_DIR_ROUND (EXT2_DIR_PAD - 1)
228 1.20 christos #define EXT2_DIR_REC_LEN(namelen) \
229 1.20 christos (((namelen) + 8 + EXT2_DIR_ROUND) & ~EXT2_DIR_ROUND)
230 1.20 christos
231 1.5 matt #endif /* !_UFS_EXT2FS_EXT2FS_DIR_H_ */
232