1 /*- 2 * SPDX-License-Identifier: MIT-CMU 3 * 4 * Copyright (c) 1995 The University of Utah and 5 * the Computer Systems Laboratory at the University of Utah (CSL). 6 * All rights reserved. 7 * 8 * Permission to use, copy, modify and distribute this software is hereby 9 * granted provided that (1) source code retains these copyright, permission, 10 * and disclaimer notices, and (2) redistributions including binaries 11 * reproduce the notices in supporting documentation, and (3) all advertising 12 * materials mentioning features or use of this software display the following 13 * acknowledgement: ``This product includes software developed by the 14 * Computer Systems Laboratory at the University of Utah.'' 15 * 16 * THE UNIVERSITY OF UTAH AND CSL ALLOW FREE USE OF THIS SOFTWARE IN ITS "AS 17 * IS" CONDITION. THE UNIVERSITY OF UTAH AND CSL DISCLAIM ANY LIABILITY OF 18 * ANY KIND FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE. 19 * 20 * CSL requests users of this software to return to csl-dist@cs.utah.edu any 21 * improvements that they make and grant CSL redistribution rights. 22 * 23 * Utah $Hdr$ 24 * $FreeBSD$ 25 */ 26 27 /* 28 * routines to convert on disk ext2 inodes into inodes and back 29 */ 30 #include <sys/param.h> 31 #include <sys/systm.h> 32 #include <sys/endian.h> 33 #include <sys/lock.h> 34 #include <sys/stat.h> 35 #include <sys/vnode.h> 36 37 #include <fs/ext2fs/fs.h> 38 #include <fs/ext2fs/inode.h> 39 #include <fs/ext2fs/ext2fs.h> 40 #include <fs/ext2fs/ext2_dinode.h> 41 #include <fs/ext2fs/ext2_extern.h> 42 43 #define XTIME_TO_NSEC(x) ((x & EXT3_NSEC_MASK) >> 2) 44 #define NSEC_TO_XTIME(t) (le32toh(t << 2) & EXT3_NSEC_MASK) 45 46 #ifdef EXT2FS_DEBUG 47 void 48 ext2_print_inode(struct inode *in) 49 { 50 int i; 51 struct ext4_extent_header *ehp; 52 struct ext4_extent *ep; 53 54 printf("Inode: %5ju", (uintmax_t)in->i_number); 55 printf( /* "Inode: %5d" */ 56 " Type: %10s Mode: 0x%o Flags: 0x%x Version: %d acl: 0x%jx\n", 57 "n/a", in->i_mode, in->i_flags, in->i_gen, in->i_facl); 58 printf("User: %5u Group: %5u Size: %ju\n", 59 in->i_uid, in->i_gid, (uintmax_t)in->i_size); 60 printf("Links: %3d Blockcount: %ju\n", 61 in->i_nlink, (uintmax_t)in->i_blocks); 62 printf("ctime: 0x%x ", in->i_ctime); 63 printf("atime: 0x%x ", in->i_atime); 64 printf("mtime: 0x%x ", in->i_mtime); 65 if (E2DI_HAS_XTIME(in)) 66 printf("crtime %#x\n", in->i_birthtime); 67 else 68 printf("\n"); 69 if (in->i_flag & IN_E4EXTENTS) { 70 printf("Extents:\n"); 71 ehp = (struct ext4_extent_header *)in->i_db; 72 printf("Header (magic 0x%x entries %d max %d depth %d gen %d)\n", 73 ehp->eh_magic, ehp->eh_ecount, ehp->eh_max, ehp->eh_depth, 74 ehp->eh_gen); 75 ep = (struct ext4_extent *)(char *)(ehp + 1); 76 printf("Index (blk %d len %d start_lo %d start_hi %d)\n", ep->e_blk, 77 ep->e_len, ep->e_start_lo, ep->e_start_hi); 78 printf("\n"); 79 } else { 80 printf("BLOCKS:"); 81 for (i = 0; i < (in->i_blocks <= 24 ? (in->i_blocks + 1) / 2 : 12); i++) 82 printf(" %d", in->i_db[i]); 83 printf("\n"); 84 } 85 } 86 #endif /* EXT2FS_DEBUG */ 87 88 /* 89 * raw ext2 inode to inode 90 */ 91 int 92 ext2_ei2i(struct ext2fs_dinode *ei, struct inode *ip) 93 { 94 struct m_ext2fs *fs = ip->i_e2fs; 95 96 if ((ip->i_number < EXT2_FIRST_INO(fs) && ip->i_number != EXT2_ROOTINO) || 97 (ip->i_number < EXT2_ROOTINO) || 98 (ip->i_number > fs->e2fs->e2fs_icount)) { 99 printf("ext2fs: bad inode number %ju\n", ip->i_number); 100 return (EINVAL); 101 } 102 103 if (ip->i_number == EXT2_ROOTINO && ei->e2di_nlink == 0) { 104 printf("ext2fs: root inode unallocated\n"); 105 return (EINVAL); 106 } 107 ip->i_nlink = ei->e2di_nlink; 108 109 /* Check extra inode size */ 110 if (EXT2_INODE_SIZE(fs) > E2FS_REV0_INODE_SIZE) { 111 if (E2FS_REV0_INODE_SIZE + ei->e2di_extra_isize > 112 EXT2_INODE_SIZE(fs) || (ei->e2di_extra_isize & 3)) { 113 printf("ext2fs: bad extra inode size %u, inode size=%u\n", 114 ei->e2di_extra_isize, EXT2_INODE_SIZE(fs)); 115 return (EINVAL); 116 } 117 } 118 119 /* 120 * Godmar thinks - if the link count is zero, then the inode is 121 * unused - according to ext2 standards. Ufs marks this fact by 122 * setting i_mode to zero - why ? I can see that this might lead to 123 * problems in an undelete. 124 */ 125 ip->i_mode = ei->e2di_nlink ? ei->e2di_mode : 0; 126 ip->i_size = ei->e2di_size; 127 if (S_ISREG(ip->i_mode)) 128 ip->i_size |= ((u_int64_t)ei->e2di_size_high) << 32; 129 ip->i_atime = ei->e2di_atime; 130 ip->i_mtime = ei->e2di_mtime; 131 ip->i_ctime = ei->e2di_ctime; 132 if (E2DI_HAS_XTIME(ip)) { 133 ip->i_atimensec = XTIME_TO_NSEC(ei->e2di_atime_extra); 134 ip->i_mtimensec = XTIME_TO_NSEC(ei->e2di_mtime_extra); 135 ip->i_ctimensec = XTIME_TO_NSEC(ei->e2di_ctime_extra); 136 ip->i_birthtime = ei->e2di_crtime; 137 ip->i_birthnsec = XTIME_TO_NSEC(ei->e2di_crtime_extra); 138 } 139 ip->i_flags = 0; 140 ip->i_flags |= (ei->e2di_flags & EXT2_APPEND) ? SF_APPEND : 0; 141 ip->i_flags |= (ei->e2di_flags & EXT2_IMMUTABLE) ? SF_IMMUTABLE : 0; 142 ip->i_flags |= (ei->e2di_flags & EXT2_NODUMP) ? UF_NODUMP : 0; 143 ip->i_flag |= (ei->e2di_flags & EXT3_INDEX) ? IN_E3INDEX : 0; 144 ip->i_flag |= (ei->e2di_flags & EXT4_EXTENTS) ? IN_E4EXTENTS : 0; 145 ip->i_blocks = ei->e2di_nblock; 146 ip->i_facl = ei->e2di_facl; 147 if (E2DI_HAS_HUGE_FILE(ip)) { 148 ip->i_blocks |= (uint64_t)ei->e2di_nblock_high << 32; 149 ip->i_facl |= (uint64_t)ei->e2di_facl_high << 32; 150 if (ei->e2di_flags & EXT4_HUGE_FILE) 151 ip->i_blocks = fsbtodb(ip->i_e2fs, ip->i_blocks); 152 } 153 ip->i_gen = ei->e2di_gen; 154 ip->i_uid = ei->e2di_uid; 155 ip->i_gid = ei->e2di_gid; 156 ip->i_uid |= (uint32_t)ei->e2di_uid_high << 16; 157 ip->i_gid |= (uint32_t)ei->e2di_gid_high << 16; 158 159 memcpy(ip->i_data, ei->e2di_blocks, sizeof(ei->e2di_blocks)); 160 161 /* Verify inode csum. */ 162 return (ext2_ei_csum_verify(ip, ei)); 163 } 164 165 /* 166 * inode to raw ext2 inode 167 */ 168 int 169 ext2_i2ei(struct inode *ip, struct ext2fs_dinode *ei) 170 { 171 struct m_ext2fs *fs; 172 173 fs = ip->i_e2fs; 174 ei->e2di_mode = ip->i_mode; 175 ei->e2di_nlink = ip->i_nlink; 176 /* 177 * Godmar thinks: if dtime is nonzero, ext2 says this inode has been 178 * deleted, this would correspond to a zero link count 179 */ 180 ei->e2di_dtime = ei->e2di_nlink ? 0 : ip->i_mtime; 181 ei->e2di_size = ip->i_size; 182 if (S_ISREG(ip->i_mode)) 183 ei->e2di_size_high = ip->i_size >> 32; 184 ei->e2di_atime = ip->i_atime; 185 ei->e2di_mtime = ip->i_mtime; 186 ei->e2di_ctime = ip->i_ctime; 187 if (E2DI_HAS_XTIME(ip)) { 188 ei->e2di_ctime_extra = NSEC_TO_XTIME(ip->i_ctimensec); 189 ei->e2di_mtime_extra = NSEC_TO_XTIME(ip->i_mtimensec); 190 ei->e2di_atime_extra = NSEC_TO_XTIME(ip->i_atimensec); 191 ei->e2di_crtime = ip->i_birthtime; 192 ei->e2di_crtime_extra = NSEC_TO_XTIME(ip->i_birthnsec); 193 } 194 ei->e2di_flags = 0; 195 ei->e2di_flags |= (ip->i_flags & SF_APPEND) ? EXT2_APPEND : 0; 196 ei->e2di_flags |= (ip->i_flags & SF_IMMUTABLE) ? EXT2_IMMUTABLE : 0; 197 ei->e2di_flags |= (ip->i_flags & UF_NODUMP) ? EXT2_NODUMP : 0; 198 ei->e2di_flags |= (ip->i_flag & IN_E3INDEX) ? EXT3_INDEX : 0; 199 ei->e2di_flags |= (ip->i_flag & IN_E4EXTENTS) ? EXT4_EXTENTS : 0; 200 if (ip->i_blocks > ~0U && 201 !EXT2_HAS_RO_COMPAT_FEATURE(fs, EXT2F_ROCOMPAT_HUGE_FILE)) { 202 ext2_fserr(fs, ip->i_uid, "i_blocks value is out of range"); 203 return (EIO); 204 } 205 if (ip->i_blocks <= 0xffffffffffffULL) { 206 ei->e2di_nblock = ip->i_blocks & 0xffffffff; 207 ei->e2di_nblock_high = ip->i_blocks >> 32 & 0xffff; 208 } else { 209 ei->e2di_flags |= EXT4_HUGE_FILE; 210 ei->e2di_nblock = dbtofsb(fs, ip->i_blocks); 211 ei->e2di_nblock_high = dbtofsb(fs, ip->i_blocks) >> 32 & 0xffff; 212 } 213 ei->e2di_facl = ip->i_facl & 0xffffffff; 214 ei->e2di_facl_high = ip->i_facl >> 32 & 0xffff; 215 ei->e2di_gen = ip->i_gen; 216 ei->e2di_uid = ip->i_uid & 0xffff; 217 ei->e2di_uid_high = ip->i_uid >> 16 & 0xffff; 218 ei->e2di_gid = ip->i_gid & 0xffff; 219 ei->e2di_gid_high = ip->i_gid >> 16 & 0xffff; 220 221 memcpy(ei->e2di_blocks, ip->i_data, sizeof(ei->e2di_blocks)); 222 223 /* Set inode csum. */ 224 ext2_ei_csum_set(ip, ei); 225 226 return (0); 227 } 228