1 /* 2 * Copyright (C) 2010 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #include "ext4_utils/ext4_utils.h" 18 19 #include <fcntl.h> 20 #include <inttypes.h> 21 #include <stddef.h> 22 #include <string.h> 23 #include <sys/stat.h> 24 #include <sys/types.h> 25 26 #ifdef _WIN32 27 #include <winsock2.h> 28 #else 29 #include <arpa/inet.h> 30 #include <sys/ioctl.h> 31 #endif 32 33 #if defined(__linux__) 34 #include <linux/fs.h> 35 #elif defined(__APPLE__) && defined(__MACH__) 36 #include <sys/disk.h> 37 #endif 38 39 int force = 0; 40 struct fs_info info; 41 struct fs_aux_info aux_info; 42 43 jmp_buf setjmp_env; 44 45 /* returns 1 if a is a power of b */ 46 static int is_power_of(int a, int b) 47 { 48 while (a > b) { 49 if (a % b) 50 return 0; 51 a /= b; 52 } 53 54 return (a == b) ? 1 : 0; 55 } 56 57 int bitmap_get_bit(u8 *bitmap, u32 bit) 58 { 59 if (bitmap[bit / 8] & (1 << (bit % 8))) 60 return 1; 61 62 return 0; 63 } 64 65 void bitmap_clear_bit(u8 *bitmap, u32 bit) 66 { 67 bitmap[bit / 8] &= ~(1 << (bit % 8)); 68 69 return; 70 } 71 72 /* Returns 1 if the bg contains a backup superblock. On filesystems with 73 the sparse_super feature, only block groups 0, 1, and powers of 3, 5, 74 and 7 have backup superblocks. Otherwise, all block groups have backup 75 superblocks */ 76 int ext4_bg_has_super_block(int bg) 77 { 78 /* Without sparse_super, every block group has a superblock */ 79 if (!(info.feat_ro_compat & EXT4_FEATURE_RO_COMPAT_SPARSE_SUPER)) 80 return 1; 81 82 if (bg == 0 || bg == 1) 83 return 1; 84 85 if (is_power_of(bg, 3) || is_power_of(bg, 5) || is_power_of(bg, 7)) 86 return 1; 87 88 return 0; 89 } 90 91 /* Function to read the primary superblock */ 92 void read_sb(int fd, struct ext4_super_block *sb) 93 { 94 off64_t ret; 95 96 ret = lseek64(fd, 1024, SEEK_SET); 97 if (ret < 0) 98 critical_error_errno("failed to seek to superblock"); 99 100 ret = read(fd, sb, sizeof(*sb)); 101 if (ret < 0) 102 critical_error_errno("failed to read superblock"); 103 if (ret != sizeof(*sb)) 104 critical_error("failed to read all of superblock"); 105 } 106 107 /* Compute the rest of the parameters of the filesystem from the basic info */ 108 void ext4_create_fs_aux_info() 109 { 110 aux_info.first_data_block = (info.block_size > 1024) ? 0 : 1; 111 aux_info.len_blocks = info.len / info.block_size; 112 aux_info.inode_table_blocks = DIV_ROUND_UP(info.inodes_per_group * info.inode_size, 113 info.block_size); 114 aux_info.groups = DIV_ROUND_UP(aux_info.len_blocks - aux_info.first_data_block, 115 info.blocks_per_group); 116 aux_info.blocks_per_ind = info.block_size / sizeof(u32); 117 aux_info.blocks_per_dind = aux_info.blocks_per_ind * aux_info.blocks_per_ind; 118 aux_info.blocks_per_tind = aux_info.blocks_per_dind * aux_info.blocks_per_dind; 119 120 aux_info.bg_desc_blocks = 121 DIV_ROUND_UP(aux_info.groups * sizeof(struct ext2_group_desc), 122 info.block_size); 123 124 aux_info.default_i_flags = EXT4_NOATIME_FL; 125 126 u32 last_group_size = aux_info.len_blocks == info.blocks_per_group 127 ? aux_info.len_blocks : aux_info.len_blocks % info.blocks_per_group; 128 u32 last_header_size = 2 + aux_info.inode_table_blocks; 129 if (ext4_bg_has_super_block((int)aux_info.groups - 1)) 130 last_header_size += 1 + aux_info.bg_desc_blocks + 131 info.bg_desc_reserve_blocks; 132 if (aux_info.groups <= 1 && last_group_size < last_header_size) { 133 critical_error("filesystem size too small"); 134 } 135 if (last_group_size > 0 && last_group_size < last_header_size) { 136 aux_info.groups--; 137 aux_info.len_blocks -= last_group_size; 138 } 139 140 /* A zero-filled superblock to be written firstly to the block 141 * device to mark the file-system as invalid 142 */ 143 aux_info.sb_zero = (struct ext4_super_block *)calloc(1, info.block_size); 144 if (!aux_info.sb_zero) 145 critical_error_errno("calloc"); 146 147 /* The write_data* functions expect only block aligned calls. 148 * This is not an issue, except when we write out the super 149 * block on a system with a block size > 1K. So, we need to 150 * deal with that here. 151 */ 152 aux_info.sb_block = (struct ext4_super_block *)calloc(1, info.block_size); 153 if (!aux_info.sb_block) 154 critical_error_errno("calloc"); 155 156 if (info.block_size > 1024) 157 aux_info.sb = (struct ext4_super_block *)((char *)aux_info.sb_block + 1024); 158 else 159 aux_info.sb = aux_info.sb_block; 160 161 /* Alloc an array to hold the pointers to the backup superblocks */ 162 aux_info.backup_sb = (struct ext4_super_block **)calloc(aux_info.groups, sizeof(char *)); 163 164 if (!aux_info.sb) 165 critical_error_errno("calloc"); 166 167 aux_info.bg_desc = (struct ext2_group_desc *)calloc(info.block_size, aux_info.bg_desc_blocks); 168 if (!aux_info.bg_desc) 169 critical_error_errno("calloc"); 170 aux_info.xattrs = NULL; 171 } 172 173 void ext4_free_fs_aux_info() 174 { 175 unsigned int i; 176 177 for (i=0; i<aux_info.groups; i++) { 178 if (aux_info.backup_sb[i]) 179 free(aux_info.backup_sb[i]); 180 } 181 free(aux_info.sb_block); 182 free(aux_info.sb_zero); 183 free(aux_info.bg_desc); 184 } 185 186 void ext4_parse_sb_info(struct ext4_super_block *sb) 187 { 188 if (sb->s_magic != EXT4_SUPER_MAGIC) 189 error("superblock magic incorrect"); 190 191 if ((sb->s_state & EXT4_VALID_FS) != EXT4_VALID_FS) 192 error("filesystem state not valid"); 193 194 ext4_parse_sb(sb, &info); 195 196 ext4_create_fs_aux_info(); 197 198 memcpy(aux_info.sb, sb, sizeof(*sb)); 199 200 if (aux_info.first_data_block != sb->s_first_data_block) 201 critical_error("first data block does not match"); 202 } 203 204 u64 get_block_device_size(int fd) 205 { 206 u64 size = 0; 207 int ret; 208 209 #if defined(__linux__) 210 ret = ioctl(fd, BLKGETSIZE64, &size); 211 #elif defined(__APPLE__) && defined(__MACH__) 212 ret = ioctl(fd, DKIOCGETBLOCKCOUNT, &size); 213 #else 214 close(fd); 215 return 0; 216 #endif 217 218 if (ret) 219 return 0; 220 221 return size; 222 } 223 224 int is_block_device_fd(int fd __attribute__((unused))) 225 { 226 #ifdef _WIN32 227 return 0; 228 #else 229 struct stat st; 230 int ret = fstat(fd, &st); 231 if (ret < 0) 232 return 0; 233 234 return S_ISBLK(st.st_mode); 235 #endif 236 } 237 238 u64 get_file_size(int fd) 239 { 240 struct stat buf; 241 int ret; 242 u64 reserve_len = 0; 243 s64 computed_size; 244 245 ret = fstat(fd, &buf); 246 if (ret) 247 return 0; 248 249 if (info.len < 0) 250 reserve_len = -info.len; 251 252 if (S_ISREG(buf.st_mode)) 253 computed_size = buf.st_size - reserve_len; 254 else if (S_ISBLK(buf.st_mode)) 255 computed_size = get_block_device_size(fd) - reserve_len; 256 else 257 computed_size = 0; 258 259 if (computed_size < 0) { 260 warn("Computed filesystem size less than 0"); 261 computed_size = 0; 262 } 263 264 return computed_size; 265 } 266 267 int read_ext(int fd, int verbose) 268 { 269 off64_t ret; 270 struct ext4_super_block sb; 271 272 read_sb(fd, &sb); 273 274 ext4_parse_sb_info(&sb); 275 276 ret = lseek64(fd, info.len, SEEK_SET); 277 if (ret < 0) 278 critical_error_errno("failed to seek to end of input image"); 279 280 ret = lseek64(fd, info.block_size * (aux_info.first_data_block + 1), SEEK_SET); 281 if (ret < 0) 282 critical_error_errno("failed to seek to block group descriptors"); 283 284 ret = read(fd, aux_info.bg_desc, info.block_size * aux_info.bg_desc_blocks); 285 if (ret < 0) 286 critical_error_errno("failed to read block group descriptors"); 287 if (ret != (int)info.block_size * (int)aux_info.bg_desc_blocks) 288 critical_error("failed to read all of block group descriptors"); 289 290 if (verbose) { 291 printf("Found filesystem with parameters:\n"); 292 printf(" Size: %" PRIu64 "\n", info.len); 293 printf(" Block size: %d\n", info.block_size); 294 printf(" Blocks per group: %d\n", info.blocks_per_group); 295 printf(" Inodes per group: %d\n", info.inodes_per_group); 296 printf(" Inode size: %d\n", info.inode_size); 297 printf(" Label: %s\n", info.label); 298 printf(" Blocks: %" PRIext4u64 "\n", aux_info.len_blocks); 299 printf(" Block groups: %d\n", aux_info.groups); 300 printf(" Reserved block group size: %d\n", info.bg_desc_reserve_blocks); 301 printf(" Used %d/%d inodes and %d/%d blocks\n", 302 aux_info.sb->s_inodes_count - aux_info.sb->s_free_inodes_count, 303 aux_info.sb->s_inodes_count, 304 aux_info.sb->s_blocks_count_lo - aux_info.sb->s_free_blocks_count_lo, 305 aux_info.sb->s_blocks_count_lo); 306 } 307 308 return 0; 309 } 310 311