1 /* $OpenBSD: ext2fs_dir.h,v 1.12 2024/01/09 03:16:00 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(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(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 enum slotstatus { 91 NONE, 92 COMPACT, 93 FOUND 94 }; 95 96 struct ext2fs_searchslot { 97 enum slotstatus slotstatus; 98 doff_t slotoffset; /* offset of area with free space */ 99 int slotsize; /* size of area at slotoffset */ 100 int slotfreespace; /* amount of space free in slot */ 101 int slotneeded; /* sizeof the entry we are seeking */ 102 }; 103 104 /* Ext2 directory file types (not the same as FFS. Sigh. */ 105 #define EXT2_FT_UNKNOWN 0 106 #define EXT2_FT_REG_FILE 1 107 #define EXT2_FT_DIR 2 108 #define EXT2_FT_CHRDEV 3 109 #define EXT2_FT_BLKDEV 4 110 #define EXT2_FT_FIFO 5 111 #define EXT2_FT_SOCK 6 112 #define EXT2_FT_SYMLINK 7 113 114 #define EXT2_FT_MAX 8 115 116 #define E2IFTODT(mode) (((mode) & 0170000) >> 12) 117 118 static __inline__ u_int8_t inot2ext2dt(u_int16_t) 119 __attribute__((__unused__)); 120 static __inline__ u_int8_t 121 inot2ext2dt(u_int16_t type) 122 { 123 switch(type) { 124 case E2IFTODT(EXT2_IFIFO): 125 return EXT2_FT_FIFO; 126 case E2IFTODT(EXT2_IFCHR): 127 return EXT2_FT_CHRDEV; 128 case E2IFTODT(EXT2_IFDIR): 129 return EXT2_FT_DIR; 130 case E2IFTODT(EXT2_IFBLK): 131 return EXT2_FT_BLKDEV; 132 case E2IFTODT(EXT2_IFREG): 133 return EXT2_FT_REG_FILE; 134 case E2IFTODT(EXT2_IFLNK): 135 return EXT2_FT_SYMLINK; 136 case E2IFTODT(EXT2_IFSOCK): 137 return EXT2_FT_SOCK; 138 default: 139 return 0; 140 } 141 } 142 143 /* 144 * The EXT2FS_DIRSIZ macro gives the minimum record length which will hold 145 * the directory entryfor a name len "len" (without the terminating null byte). 146 * This requires the amount of space in struct direct 147 * without the d_name field, plus enough space for the name without a 148 * terminating null byte, rounded up to a 4 byte boundary. 149 */ 150 #define EXT2FS_DIRSIZ(len) \ 151 (( 8 + len + 3) &~ 3) 152 153 /* 154 * Template for manipulating directories. Should use struct direct's, 155 * but the name field is EXT2FS_MAXNAMLEN - 1, and this just won't do. 156 */ 157 struct ext2fs_dirtemplate { 158 u_int32_t dot_ino; 159 int16_t dot_reclen; 160 u_int8_t dot_namlen; 161 u_int8_t dot_type; 162 char dot_name[4]; /* must be multiple of 4 */ 163 u_int32_t dotdot_ino; 164 int16_t dotdot_reclen; 165 u_int8_t dotdot_namlen; 166 u_int8_t dotdot_type; 167 char dotdot_name[4]; /* ditto */ 168 }; 169 170 #endif /* !_EXT2FS_DIR_H_ */ 171