1*da5362d5Sguenther /* $OpenBSD: ext2fs_dir.h,v 1.12 2024/01/09 03:16:00 guenther Exp $ */
2ba7d765bSart /* $NetBSD: ext2fs_dir.h,v 1.4 2000/01/28 16:00:23 bouyer Exp $ */
35ac2d602Sdownsj
45ac2d602Sdownsj /*
51f3ff51cSdownsj * Copyright (c) 1997 Manuel Bouyer.
65ac2d602Sdownsj * Copyright (c) 1982, 1986, 1989, 1993
75ac2d602Sdownsj * The Regents of the University of California. All rights reserved.
85ac2d602Sdownsj * (c) UNIX System Laboratories, Inc.
95ac2d602Sdownsj * All or some portions of this file are derived from material licensed
105ac2d602Sdownsj * to the University of California by American Telephone and Telegraph
115ac2d602Sdownsj * Co. or Unix System Laboratories, Inc. and are reproduced herein with
125ac2d602Sdownsj * the permission of UNIX System Laboratories, Inc.
135ac2d602Sdownsj *
145ac2d602Sdownsj * Redistribution and use in source and binary forms, with or without
155ac2d602Sdownsj * modification, are permitted provided that the following conditions
165ac2d602Sdownsj * are met:
175ac2d602Sdownsj * 1. Redistributions of source code must retain the above copyright
185ac2d602Sdownsj * notice, this list of conditions and the following disclaimer.
195ac2d602Sdownsj * 2. Redistributions in binary form must reproduce the above copyright
205ac2d602Sdownsj * notice, this list of conditions and the following disclaimer in the
215ac2d602Sdownsj * documentation and/or other materials provided with the distribution.
2229295d1cSmillert * 3. Neither the name of the University nor the names of its contributors
235ac2d602Sdownsj * may be used to endorse or promote products derived from this software
245ac2d602Sdownsj * without specific prior written permission.
255ac2d602Sdownsj *
265ac2d602Sdownsj * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
275ac2d602Sdownsj * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
285ac2d602Sdownsj * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
295ac2d602Sdownsj * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
305ac2d602Sdownsj * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
315ac2d602Sdownsj * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
325ac2d602Sdownsj * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
335ac2d602Sdownsj * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
345ac2d602Sdownsj * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
355ac2d602Sdownsj * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
365ac2d602Sdownsj * SUCH DAMAGE.
375ac2d602Sdownsj *
385ac2d602Sdownsj * @(#)dir.h 8.4 (Berkeley) 8/10/94
391f3ff51cSdownsj * Modified for ext2fs by Manuel Bouyer.
405ac2d602Sdownsj */
415ac2d602Sdownsj
425ac2d602Sdownsj #ifndef _EXT2FS_DIR_H_
435ac2d602Sdownsj #define _EXT2FS_DIR_H_
445ac2d602Sdownsj
455ac2d602Sdownsj /*
465ac2d602Sdownsj * Theoretically, directories can be more than 2Gb in length, however, in
475ac2d602Sdownsj * practice this seems unlikely. So, we define the type doff_t as a 32-bit
485ac2d602Sdownsj * quantity to keep down the cost of doing lookup on a 32-bit machine.
495ac2d602Sdownsj */
505ac2d602Sdownsj #define doff_t int32_t
515ac2d602Sdownsj #define EXT2FS_MAXDIRSIZE (0x7fffffff)
525ac2d602Sdownsj
535ac2d602Sdownsj /*
545ac2d602Sdownsj * A directory consists of some number of blocks of e2fs_bsize bytes.
555ac2d602Sdownsj *
565ac2d602Sdownsj * Each block contains some number of directory entry
575ac2d602Sdownsj * structures, which are of variable length. Each directory entry has
585ac2d602Sdownsj * a struct direct at the front of it, containing its inode number,
595ac2d602Sdownsj * the length of the entry, and the length of the name contained in
605ac2d602Sdownsj * the entry. These are followed by the name padded to a 4 byte boundary
615ac2d602Sdownsj * with null bytes. All names are guaranteed null terminated.
625ac2d602Sdownsj * The maximum length of a name in a directory is EXT2FS_MAXNAMLEN.
635ac2d602Sdownsj *
64*da5362d5Sguenther * The macro EXT2FS_DIRSIZ(dp) gives the amount of space required to
655ac2d602Sdownsj * represent a directory entry. Free space in a directory is represented by
66*da5362d5Sguenther * entries which have dp->e2d_reclen > DIRSIZ(dp). All d2fs_bsize bytes
675ac2d602Sdownsj * in a directory block are claimed by the directory entries. This
685ac2d602Sdownsj * usually results in the last entry in a directory having a large
695ac2d602Sdownsj * dp->e2d_reclen. When entries are deleted from a directory, the
705ac2d602Sdownsj * space is returned to the previous entry in the same directory
715ac2d602Sdownsj * block by increasing its dp->e2d_reclen. If the first entry of
725ac2d602Sdownsj * a directory block is free, then its dp->e2d_ino is set to 0.
735ac2d602Sdownsj * Entries other than the first in a directory do not normally have
745ac2d602Sdownsj * dp->e2d_ino set to 0.
7573ecdb3fSguenther * Ext2 rev 0 has a 16 bits e2d_namlen. For Ext2 rev 1 this has been split
76ba7d765bSart * into a 8 bits e2d_namlen and 8 bits e2d_type (looks like ffs, isnt't it ? :)
77ba7d765bSart * It's safe to use this for rev 0 as well because all ext2 are little-endian.
785ac2d602Sdownsj */
795ac2d602Sdownsj
805ac2d602Sdownsj #define EXT2FS_MAXNAMLEN 255
815ac2d602Sdownsj
825ac2d602Sdownsj struct ext2fs_direct {
835ac2d602Sdownsj u_int32_t e2d_ino; /* inode number of entry */
845ac2d602Sdownsj u_int16_t e2d_reclen; /* length of this record */
85f5ee6277Sjasoni u_int8_t e2d_namlen; /* length of string in d_name */
86f5ee6277Sjasoni u_int8_t e2d_type; /* file type */
875ac2d602Sdownsj char e2d_name[EXT2FS_MAXNAMLEN];/* name with length <= EXT2FS_MAXNAMLEN */
885ac2d602Sdownsj };
895ac2d602Sdownsj
903e537b2cSpelikan enum slotstatus {
913e537b2cSpelikan NONE,
923e537b2cSpelikan COMPACT,
933e537b2cSpelikan FOUND
943e537b2cSpelikan };
953e537b2cSpelikan
963e537b2cSpelikan struct ext2fs_searchslot {
973e537b2cSpelikan enum slotstatus slotstatus;
983e537b2cSpelikan doff_t slotoffset; /* offset of area with free space */
993e537b2cSpelikan int slotsize; /* size of area at slotoffset */
1003e537b2cSpelikan int slotfreespace; /* amount of space free in slot */
1013e537b2cSpelikan int slotneeded; /* sizeof the entry we are seeking */
1023e537b2cSpelikan };
1033e537b2cSpelikan
104f5ee6277Sjasoni /* Ext2 directory file types (not the same as FFS. Sigh. */
105f5ee6277Sjasoni #define EXT2_FT_UNKNOWN 0
106f5ee6277Sjasoni #define EXT2_FT_REG_FILE 1
107f5ee6277Sjasoni #define EXT2_FT_DIR 2
108f5ee6277Sjasoni #define EXT2_FT_CHRDEV 3
109f5ee6277Sjasoni #define EXT2_FT_BLKDEV 4
110f5ee6277Sjasoni #define EXT2_FT_FIFO 5
111f5ee6277Sjasoni #define EXT2_FT_SOCK 6
112f5ee6277Sjasoni #define EXT2_FT_SYMLINK 7
113ba7d765bSart
114f5ee6277Sjasoni #define EXT2_FT_MAX 8
115f5ee6277Sjasoni
116f5ee6277Sjasoni #define E2IFTODT(mode) (((mode) & 0170000) >> 12)
117f5ee6277Sjasoni
118c4071fd1Smillert static __inline__ u_int8_t inot2ext2dt(u_int16_t)
119f5ee6277Sjasoni __attribute__((__unused__));
120f5ee6277Sjasoni static __inline__ u_int8_t
inot2ext2dt(u_int16_t type)1217d9a38bcSjsg inot2ext2dt(u_int16_t type)
122f5ee6277Sjasoni {
123f5ee6277Sjasoni switch(type) {
124f5ee6277Sjasoni case E2IFTODT(EXT2_IFIFO):
125f5ee6277Sjasoni return EXT2_FT_FIFO;
126f5ee6277Sjasoni case E2IFTODT(EXT2_IFCHR):
127f5ee6277Sjasoni return EXT2_FT_CHRDEV;
128f5ee6277Sjasoni case E2IFTODT(EXT2_IFDIR):
129f5ee6277Sjasoni return EXT2_FT_DIR;
130f5ee6277Sjasoni case E2IFTODT(EXT2_IFBLK):
131f5ee6277Sjasoni return EXT2_FT_BLKDEV;
132f5ee6277Sjasoni case E2IFTODT(EXT2_IFREG):
133f5ee6277Sjasoni return EXT2_FT_REG_FILE;
134f5ee6277Sjasoni case E2IFTODT(EXT2_IFLNK):
135f5ee6277Sjasoni return EXT2_FT_SYMLINK;
136f5ee6277Sjasoni case E2IFTODT(EXT2_IFSOCK):
137f5ee6277Sjasoni return EXT2_FT_SOCK;
138f5ee6277Sjasoni default:
139f5ee6277Sjasoni return 0;
140f5ee6277Sjasoni }
141f5ee6277Sjasoni }
142f5ee6277Sjasoni
1435ac2d602Sdownsj /*
1445ac2d602Sdownsj * The EXT2FS_DIRSIZ macro gives the minimum record length which will hold
1455ac2d602Sdownsj * the directory entryfor a name len "len" (without the terminating null byte).
1465ac2d602Sdownsj * This requires the amount of space in struct direct
1475ac2d602Sdownsj * without the d_name field, plus enough space for the name without a
1485ac2d602Sdownsj * terminating null byte, rounded up to a 4 byte boundary.
1495ac2d602Sdownsj */
1505ac2d602Sdownsj #define EXT2FS_DIRSIZ(len) \
1515ac2d602Sdownsj (( 8 + len + 3) &~ 3)
1525ac2d602Sdownsj
1535ac2d602Sdownsj /*
1545ac2d602Sdownsj * Template for manipulating directories. Should use struct direct's,
1555ac2d602Sdownsj * but the name field is EXT2FS_MAXNAMLEN - 1, and this just won't do.
1565ac2d602Sdownsj */
1575ac2d602Sdownsj struct ext2fs_dirtemplate {
1585ac2d602Sdownsj u_int32_t dot_ino;
1595ac2d602Sdownsj int16_t dot_reclen;
160f5ee6277Sjasoni u_int8_t dot_namlen;
161f5ee6277Sjasoni u_int8_t dot_type;
1625ac2d602Sdownsj char dot_name[4]; /* must be multiple of 4 */
1635ac2d602Sdownsj u_int32_t dotdot_ino;
1645ac2d602Sdownsj int16_t dotdot_reclen;
165f5ee6277Sjasoni u_int8_t dotdot_namlen;
166f5ee6277Sjasoni u_int8_t dotdot_type;
1675ac2d602Sdownsj char dotdot_name[4]; /* ditto */
1685ac2d602Sdownsj };
1695ac2d602Sdownsj
1705ac2d602Sdownsj #endif /* !_EXT2FS_DIR_H_ */
171