ext4fixup.c revision 97fc910ce0e05862888fd1d9e1938feba40f7539
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#include "ext4_utils.h" 17#include "make_ext4fs.h" 18#include "ext4_extents.h" 19#include "output_file.h" 20#include "backed_block.h" 21#include "allocate.h" 22#include "ext4fixup.h" 23 24#include <sys/types.h> 25#include <sys/stat.h> 26#include <sys/types.h> 27#include <sys/mman.h> 28#include <fcntl.h> 29#include <unistd.h> 30 31#if defined(__APPLE__) && defined(__MACH__) 32#define lseek64 lseek 33#define off64_t off_t 34#endif 35 36/* The inode block count for a file/directory is in units of 512 byte blocks, 37 * _NOT_ the filesystem block size! 38 */ 39#define INODE_BLOCK_SIZE 512 40 41#define MAX_EXT4_BLOCK_SIZE 4096 42 43/* The two modes the recurse_dir() can be in */ 44#define SANITY_CHECK_PASS 1 45#define MARK_INODE_NUMS 2 46#define UPDATE_INODE_NUMS 3 47 48/* Magic numbers to indicate what state the update process is in */ 49#define MAGIC_STATE_MARKING_INUMS 0x7000151515565512ll 50#define MAGIC_STATE_UPDATING_INUMS 0x6121131211735123ll 51#define MAGIC_STATE_UPDATING_SB 0x15e1715151558477ll 52 53/* Internal state variables corresponding to the magic numbers */ 54#define STATE_UNSET 0 55#define STATE_MARKING_INUMS 1 56#define STATE_UPDATING_INUMS 2 57#define STATE_UPDATING_SB 3 58 59/* global flags */ 60static int verbose = 0; 61static int no_write = 0; 62 63static int new_inodes_per_group = 0; 64 65static int no_write_fixup_state = 0; 66 67static int compute_new_inum(unsigned int old_inum) 68{ 69 unsigned int group, offset; 70 71 group = (old_inum - 1) / info.inodes_per_group; 72 offset = (old_inum -1) % info.inodes_per_group; 73 74 return (group * new_inodes_per_group) + offset + 1; 75} 76 77/* Function to read the primary superblock */ 78static void read_sb(int fd, struct ext4_super_block *sb) 79{ 80 off64_t ret; 81 82 ret = lseek64(fd, 1024, SEEK_SET); 83 if (ret < 0) 84 critical_error_errno("failed to seek to superblock"); 85 86 ret = read(fd, sb, sizeof(*sb)); 87 if (ret < 0) 88 critical_error_errno("failed to read superblock"); 89 if (ret != sizeof(*sb)) 90 critical_error("failed to read all of superblock"); 91} 92 93/* Function to write a primary or backup superblock at a given offset */ 94static void write_sb(int fd, unsigned long long offset, struct ext4_super_block *sb) 95{ 96 off64_t ret; 97 98 if (no_write) { 99 return; 100 } 101 102 ret = lseek64(fd, offset, SEEK_SET); 103 if (ret < 0) 104 critical_error_errno("failed to seek to superblock"); 105 106 ret = write(fd, sb, sizeof(*sb)); 107 if (ret < 0) 108 critical_error_errno("failed to write superblock"); 109 if (ret != sizeof(*sb)) 110 critical_error("failed to write all of superblock"); 111} 112 113static int get_fs_fixup_state(int fd) 114{ 115 unsigned long long magic; 116 int ret, len; 117 118 if (no_write) { 119 return no_write_fixup_state; 120 } 121 122 lseek64(fd, 0, SEEK_SET); 123 len = read(fd, &magic, sizeof(magic)); 124 if (len != sizeof(magic)) { 125 critical_error("cannot read fixup_state\n"); 126 } 127 128 switch (magic) { 129 case MAGIC_STATE_MARKING_INUMS: 130 ret = STATE_MARKING_INUMS; 131 break; 132 case MAGIC_STATE_UPDATING_INUMS: 133 ret = STATE_UPDATING_INUMS; 134 break; 135 case MAGIC_STATE_UPDATING_SB: 136 ret = STATE_UPDATING_SB; 137 break; 138 default: 139 ret = STATE_UNSET; 140 } 141 return ret; 142} 143 144static int set_fs_fixup_state(int fd, int state) 145{ 146 unsigned long long magic; 147 struct ext4_super_block sb; 148 int len; 149 150 if (no_write) { 151 no_write_fixup_state = state; 152 return 0; 153 } 154 155 switch (state) { 156 case STATE_MARKING_INUMS: 157 magic = MAGIC_STATE_MARKING_INUMS; 158 break; 159 case STATE_UPDATING_INUMS: 160 magic = MAGIC_STATE_UPDATING_INUMS; 161 break; 162 case STATE_UPDATING_SB: 163 magic = MAGIC_STATE_UPDATING_SB; 164 break; 165 case STATE_UNSET: 166 default: 167 magic = 0ll; 168 break; 169 } 170 171 lseek64(fd, 0, SEEK_SET); 172 len = write(fd, &magic, sizeof(magic)); 173 if (len != sizeof(magic)) { 174 critical_error("cannot write fixup_state\n"); 175 } 176 177 read_sb(fd, &sb); 178 if (magic) { 179 /* If we are in the process of updating the filesystem, make it unmountable */ 180 sb.s_desc_size |= 1; 181 } else { 182 /* we are done, so make the filesystem mountable again */ 183 sb.s_desc_size &= ~1; 184 } 185 write_sb(fd, 1024, &sb); 186 187 return 0; 188} 189 190static int read_ext(int fd) 191{ 192 off64_t ret; 193 struct ext4_super_block sb; 194 unsigned int i; 195 196 read_sb(fd, &sb); 197 198 ext4_parse_sb(&sb); 199 200 if (info.feat_incompat & EXT4_FEATURE_INCOMPAT_RECOVER) { 201 critical_error("Filesystem needs recovery first, mount and unmount to do that\n"); 202 } 203 204 /* Clear the low bit which is set while this tool is in progress. 205 * If the tool crashes, it will still be set when we restart. 206 * The low bit is set to make the filesystem unmountable while 207 * it is being fixed up. Also allow 0, which means the old ext2 208 * size is in use. 209 */ 210 if (((sb.s_desc_size & ~1) != sizeof(struct ext2_group_desc)) && 211 ((sb.s_desc_size & ~1) != 0)) 212 critical_error("error: bg_desc_size != sizeof(struct ext2_group_desc)\n"); 213 214 ret = lseek64(fd, info.len, SEEK_SET); 215 if (ret < 0) 216 critical_error_errno("failed to seek to end of input image"); 217 218 ret = lseek64(fd, info.block_size * (aux_info.first_data_block + 1), SEEK_SET); 219 if (ret < 0) 220 critical_error_errno("failed to seek to block group descriptors"); 221 222 ret = read(fd, aux_info.bg_desc, info.block_size * aux_info.bg_desc_blocks); 223 if (ret < 0) 224 critical_error_errno("failed to read block group descriptors"); 225 if (ret != (int)info.block_size * (int)aux_info.bg_desc_blocks) 226 critical_error("failed to read all of block group descriptors"); 227 228 if (verbose) { 229 printf("Found filesystem with parameters:\n"); 230 printf(" Size: %llu\n", info.len); 231 printf(" Block size: %d\n", info.block_size); 232 printf(" Blocks per group: %d\n", info.blocks_per_group); 233 printf(" Inodes per group: %d\n", info.inodes_per_group); 234 printf(" Inode size: %d\n", info.inode_size); 235 printf(" Label: %s\n", info.label); 236 printf(" Blocks: %llu\n", aux_info.len_blocks); 237 printf(" Block groups: %d\n", aux_info.groups); 238 printf(" Reserved block group size: %d\n", info.bg_desc_reserve_blocks); 239 printf(" Used %d/%d inodes and %d/%d blocks\n", 240 aux_info.sb->s_inodes_count - aux_info.sb->s_free_inodes_count, 241 aux_info.sb->s_inodes_count, 242 aux_info.sb->s_blocks_count_lo - aux_info.sb->s_free_blocks_count_lo, 243 aux_info.sb->s_blocks_count_lo); 244 } 245 246 return 0; 247} 248 249static int read_inode(int fd, unsigned int inum, struct ext4_inode *inode) 250{ 251 unsigned int bg_num, bg_offset; 252 off64_t inode_offset; 253 int len; 254 255 bg_num = (inum-1) / info.inodes_per_group; 256 bg_offset = (inum-1) % info.inodes_per_group; 257 258 inode_offset = ((unsigned long long)aux_info.bg_desc[bg_num].bg_inode_table * info.block_size) + 259 (bg_offset * info.inode_size); 260 261 if (lseek64(fd, inode_offset, SEEK_SET) < 0) { 262 critical_error_errno("failed to seek to inode %d\n", inum); 263 } 264 265 len=read(fd, inode, sizeof(*inode)); 266 if (len != sizeof(*inode)) { 267 critical_error_errno("failed to read inode %d\n", inum); 268 } 269 270 return 0; 271} 272 273static int read_block(int fd, unsigned long long block_num, void *block) 274{ 275 off64_t off; 276 unsigned int len; 277 278 off = block_num * info.block_size; 279 280 if (lseek64(fd, off, SEEK_SET) , 0) { 281 critical_error_errno("failed to seek to block %lld\n", block_num); 282 } 283 284 len=read(fd, block, info.block_size); 285 if (len != info.block_size) { 286 critical_error_errno("failed to read block %lld\n", block_num); 287 } 288 289 return 0; 290} 291 292static int write_block(int fd, unsigned long long block_num, void *block) 293{ 294 off64_t off; 295 unsigned int len; 296 297 if (no_write) { 298 return 0; 299 } 300 301 off = block_num * info.block_size; 302 303 if (lseek64(fd, off, SEEK_SET) < 0) { 304 critical_error_errno("failed to seek to block %lld\n", block_num); 305 } 306 307 len=write(fd, block, info.block_size); 308 if (len != info.block_size) { 309 critical_error_errno("failed to write block %lld\n", block_num); 310 } 311 312 return 0; 313} 314 315static int bitmap_get_bit(u8 *bitmap, u32 bit) 316{ 317 if (bitmap[bit / 8] & (1 << (bit % 8))) 318 return 1; 319 320 return 0; 321} 322 323static void bitmap_clear_bit(u8 *bitmap, u32 bit) 324{ 325 bitmap[bit / 8] &= ~(1 << (bit % 8)); 326 327 return; 328} 329 330static void check_inode_bitmap(int fd, unsigned int bg_num) 331{ 332 unsigned int inode_bitmap_block_num; 333 unsigned char block[MAX_EXT4_BLOCK_SIZE]; 334 int i, bitmap_updated = 0; 335 336 /* Using the bg_num, aux_info.bg_desc[], info.inodes_per_group and 337 * new_inodes_per_group, retrieve the inode bitmap, and make sure 338 * the bits between the old and new size are clear 339 */ 340 inode_bitmap_block_num = aux_info.bg_desc[bg_num].bg_inode_bitmap; 341 342 read_block(fd, inode_bitmap_block_num, block); 343 344 for (i = info.inodes_per_group; i < new_inodes_per_group; i++) { 345 if (bitmap_get_bit(block, i)) { 346 bitmap_clear_bit(block, i); 347 bitmap_updated = 1; 348 } 349 } 350 351 if (bitmap_updated) { 352 if (verbose) { 353 printf("Warning: updated inode bitmap for block group %d\n", bg_num); 354 } 355 write_block(fd, inode_bitmap_block_num, block); 356 } 357 358 return; 359} 360 361/* Update the superblock and bgdesc of the specified block group */ 362static int update_superblocks_and_bg_desc(int fd, int state) 363{ 364 off64_t ret; 365 struct ext4_super_block sb; 366 unsigned int num_block_groups, total_new_inodes; 367 unsigned int i; 368 369 370 read_sb(fd, &sb); 371 372 /* Compute how many more inodes are now available */ 373 num_block_groups = DIV_ROUND_UP(aux_info.len_blocks, info.blocks_per_group); 374 total_new_inodes = num_block_groups * (new_inodes_per_group - sb.s_inodes_per_group); 375 376 if (verbose) { 377 printf("created %d additional inodes\n", total_new_inodes); 378 } 379 380 /* Update the free inodes count in each block group descriptor */ 381 for (i = 0; i < num_block_groups; i++) { 382 if (state == STATE_UPDATING_SB) { 383 aux_info.bg_desc[i].bg_free_inodes_count += (new_inodes_per_group - sb.s_inodes_per_group); 384 } 385 check_inode_bitmap(fd, i); 386 } 387 388 /* First some sanity checks */ 389 if ((sb.s_inodes_count + total_new_inodes) != (new_inodes_per_group * num_block_groups)) { 390 critical_error("Failed sanity check on new inode count\n"); 391 } 392 if (new_inodes_per_group % (info.block_size/info.inode_size)) { 393 critical_error("Failed sanity check on new inode per group alignment\n"); 394 } 395 396 /* Update the free inodes count in the superblock */ 397 sb.s_inodes_count += total_new_inodes; 398 sb.s_free_inodes_count += total_new_inodes; 399 sb.s_inodes_per_group = new_inodes_per_group; 400 401 for (i = 0; i < aux_info.groups; i++) { 402 if (ext4_bg_has_super_block(i)) { 403 unsigned int sb_offset; 404 405 if (i == 0) { 406 /* The first superblock is offset by 1K to leave room for boot sectors */ 407 sb_offset = 1024; 408 } else { 409 sb_offset = 0; 410 } 411 412 sb.s_block_group_nr = i; 413 /* Don't write out the backup superblocks with the bit set in the s_desc_size 414 * which prevents the filesystem from mounting. The bit for the primary 415 * superblock will be cleared on the final call to set_fs_fixup_state() */ 416 if (i != 0) { 417 sb.s_desc_size &= ~1; 418 } 419 420 write_sb(fd, (unsigned long long)i * info.blocks_per_group * info.block_size + sb_offset, &sb); 421 422 ret = lseek64(fd, ((unsigned long long)i * info.blocks_per_group * info.block_size) + 423 (info.block_size * (aux_info.first_data_block + 1)), SEEK_SET); 424 if (ret < 0) 425 critical_error_errno("failed to seek to block group descriptors"); 426 427 if (!no_write) { 428 ret = write(fd, aux_info.bg_desc, info.block_size * aux_info.bg_desc_blocks); 429 if (ret < 0) 430 critical_error_errno("failed to write block group descriptors"); 431 if (ret != (int)info.block_size * (int)aux_info.bg_desc_blocks) 432 critical_error("failed to write all of block group descriptors"); 433 } 434 } 435 } 436 437 return 0; 438} 439 440 441static int get_direct_blocks(struct ext4_inode *inode, unsigned long long *block_list, 442 unsigned int *count) 443{ 444 unsigned int i = 0; 445 unsigned int ret = 0; 446 unsigned int sectors_per_block; 447 448 sectors_per_block = info.block_size / INODE_BLOCK_SIZE; 449 while ((i < (inode->i_blocks_lo / sectors_per_block)) && (i < EXT4_NDIR_BLOCKS)) { 450 block_list[i] = inode->i_block[i]; 451 i++; 452 } 453 454 *count += i; 455 456 if ((inode->i_blocks_lo / sectors_per_block) > EXT4_NDIR_BLOCKS) { 457 ret = 1; 458 } 459 460 return ret; 461} 462 463static int get_indirect_blocks(int fd, struct ext4_inode *inode, 464 unsigned long long *block_list, unsigned int *count) 465{ 466 unsigned int i; 467 unsigned int *indirect_block; 468 unsigned int sectors_per_block; 469 470 sectors_per_block = info.block_size / INODE_BLOCK_SIZE; 471 472 indirect_block = (unsigned int *)malloc(info.block_size); 473 if (indirect_block == 0) { 474 critical_error("failed to allocate memory for indirect_block\n"); 475 } 476 477 read_block(fd, inode->i_block[EXT4_NDIR_BLOCKS], indirect_block); 478 479 for(i = 0; i < (inode->i_blocks_lo / sectors_per_block - EXT4_NDIR_BLOCKS); i++) { 480 block_list[EXT4_NDIR_BLOCKS+i] = indirect_block[i]; 481 } 482 483 *count += i; 484 485 free(indirect_block); 486 487 return 0; 488} 489 490static int get_block_list_indirect(int fd, struct ext4_inode *inode, unsigned long long *block_list) 491{ 492 unsigned int count=0; 493 494 if (get_direct_blocks(inode, block_list, &count)) { 495 get_indirect_blocks(fd, inode, block_list, &count); 496 } 497 498 return count; 499} 500 501static int get_extent_ents(int fd, struct ext4_extent_header *ext_hdr, unsigned long long *block_list) 502{ 503 int i, j; 504 struct ext4_extent *extent; 505 off64_t fs_block_num; 506 507 if (ext_hdr->eh_depth != 0) { 508 critical_error("get_extent_ents called with eh_depth != 0\n"); 509 } 510 511 /* The extent entries immediately follow the header, so add 1 to the pointer 512 * and cast it to an extent pointer. 513 */ 514 extent = (struct ext4_extent *)(ext_hdr + 1); 515 516 for (i = 0; i < ext_hdr->eh_entries; i++) { 517 fs_block_num = ((off64_t)extent->ee_start_hi << 32) | extent->ee_start_lo; 518 for (j = 0; j < extent->ee_len; j++) { 519 block_list[extent->ee_block+j] = fs_block_num+j; 520 } 521 extent++; 522 } 523 524 return 0; 525} 526 527static int get_extent_idx(int fd, struct ext4_extent_header *ext_hdr, unsigned long long *block_list) 528{ 529 int i; 530 struct ext4_extent_idx *extent_idx; 531 struct ext4_extent_header *tmp_ext_hdr; 532 off64_t fs_block_num; 533 unsigned char block[MAX_EXT4_BLOCK_SIZE]; 534 535 /* Sanity check */ 536 if (ext_hdr->eh_depth == 0) { 537 critical_error("get_extent_idx called with eh_depth == 0\n"); 538 } 539 540 /* The extent entries immediately follow the header, so add 1 to the pointer 541 * and cast it to an extent pointer. 542 */ 543 extent_idx = (struct ext4_extent_idx *)(ext_hdr + 1); 544 545 for (i = 0; i < ext_hdr->eh_entries; i++) { 546 fs_block_num = ((off64_t)extent_idx->ei_leaf_hi << 32) | extent_idx->ei_leaf_lo; 547 read_block(fd, fs_block_num, block); 548 tmp_ext_hdr = (struct ext4_extent_header *)block; 549 550 if (tmp_ext_hdr->eh_depth == 0) { 551 get_extent_ents(fd, tmp_ext_hdr, block_list); /* leaf node, fill in block_list */ 552 } else { 553 get_extent_idx(fd, tmp_ext_hdr, block_list); /* recurse down the tree */ 554 } 555 } 556 557 return 0; 558} 559 560static int get_block_list_extents(int fd, struct ext4_inode *inode, unsigned long long *block_list) 561{ 562 struct ext4_extent_header *extent_hdr; 563 564 extent_hdr = (struct ext4_extent_header *)inode->i_block; 565 566 if (extent_hdr->eh_magic != EXT4_EXT_MAGIC) { 567 critical_error("extent header has unexpected magic value 0x%4.4x\n", 568 extent_hdr->eh_magic); 569 } 570 571 if (extent_hdr->eh_depth == 0) { 572 get_extent_ents(fd, (struct ext4_extent_header *)inode->i_block, block_list); 573 return 0; 574 } 575 576 get_extent_idx(fd, (struct ext4_extent_header *)inode->i_block, block_list); 577 578 return 0; 579} 580 581static int is_entry_dir(int fd, struct ext4_dir_entry_2 *dirp, int pass) 582{ 583 struct ext4_inode inode; 584 int ret = 0; 585 586 if (dirp->file_type == EXT4_FT_DIR) { 587 ret = 1; 588 } else if (dirp->file_type == EXT4_FT_UNKNOWN) { 589 /* Somebody was too lazy to fill in the dir entry, 590 * so we have to go fetch it from the inode. Grrr. 591 */ 592 /* if UPDATE_INODE_NUMS pass and the inode high bit is not 593 * set return false so we don't recurse down the tree that is 594 * already updated. Otherwise, fetch inode, and return answer. 595 */ 596 if ((pass == UPDATE_INODE_NUMS) && !(dirp->inode & 0x80000000)) { 597 ret = 0; 598 } else { 599 read_inode(fd, (dirp->inode & 0x7fffffff), &inode); 600 if (S_ISDIR(inode.i_mode)) { 601 ret = 1; 602 } 603 } 604 } 605 606 return ret; 607} 608 609static int recurse_dir(int fd, struct ext4_inode *inode, char *dirbuf, int dirsize, int mode) 610{ 611 unsigned long long *block_list; 612 unsigned int num_blocks; 613 struct ext4_dir_entry_2 *dirp, *prev_dirp = 0; 614 char name[256]; 615 unsigned int i, leftover_space, is_dir; 616 struct ext4_inode tmp_inode; 617 int tmp_dirsize; 618 char *tmp_dirbuf; 619 620 switch (mode) { 621 case SANITY_CHECK_PASS: 622 case MARK_INODE_NUMS: 623 case UPDATE_INODE_NUMS: 624 break; 625 default: 626 critical_error("recurse_dir() called witn unknown mode!\n"); 627 } 628 629 if (dirsize % info.block_size) { 630 critical_error("dirsize %d not a multiple of block_size %d. This is unexpected!\n", 631 dirsize, info.block_size); 632 } 633 634 num_blocks = dirsize / info.block_size; 635 636 block_list = malloc((num_blocks + 1) * sizeof(*block_list)); 637 if (block_list == 0) { 638 critical_error("failed to allocate memory for block_list\n"); 639 } 640 641 if (inode->i_flags & EXT4_EXTENTS_FL) { 642 get_block_list_extents(fd, inode, block_list); 643 } else { 644 /* A directory that requires doubly or triply indirect blocks in huge indeed, 645 * and will almost certainly not exist, especially since make_ext4fs only creates 646 * directories with extents, and the kernel will too, but check to make sure the 647 * directory is not that big and give an error if so. Our limit is 12 direct blocks, 648 * plus block_size/4 singly indirect blocks, which for a filesystem with 4K blocks 649 * is a directory 1036 blocks long, or 4,243,456 bytes long! Assuming an average 650 * filename length of 20 (which I think is generous) thats 20 + 8 bytes overhead 651 * per entry, or 151,552 entries in the directory! 652 */ 653 if (num_blocks > (info.block_size / 4 + EXT4_NDIR_BLOCKS)) { 654 critical_error("Non-extent based directory is too big!\n"); 655 } 656 get_block_list_indirect(fd, inode, block_list); 657 } 658 659 /* Read in all the blocks for this directory */ 660 for (i = 0; i < num_blocks; i++) { 661 read_block(fd, block_list[i], dirbuf + (i * info.block_size)); 662 } 663 664 dirp = (struct ext4_dir_entry_2 *)dirbuf; 665 while (dirp < (struct ext4_dir_entry_2 *)(dirbuf + dirsize)) { 666 leftover_space = (char *)(dirbuf + dirsize) - (char *)dirp; 667 if (((mode == SANITY_CHECK_PASS) || (mode == UPDATE_INODE_NUMS)) && 668 (leftover_space <= 8) && prev_dirp) { 669 /* This is a bug in an older version of make_ext4fs, where it 670 * didn't properly include the rest of the block in rec_len. 671 * Update rec_len on the previous entry to include the rest of 672 * the block and exit the loop. 673 */ 674 if (verbose) { 675 printf("fixing up short rec_len for diretory entry for %s\n", name); 676 } 677 prev_dirp->rec_len += leftover_space; 678 break; 679 } 680 681 if (dirp->inode == 0) { 682 /* This is the last entry in the directory */ 683 break; 684 } 685 686 strncpy(name, dirp->name, dirp->name_len); 687 name[dirp->name_len]='\0'; 688 689 /* Only recurse on pass UPDATE_INODE_NUMS if the high bit is set. 690 * Otherwise, this inode entry has already been updated 691 * and we'll do the wrong thing. Also don't recurse on . or .., 692 * and certainly not on non-directories! 693 */ 694 /* Hrm, looks like filesystems made by fastboot on stingray set the file_type 695 * flag, but the lost+found directory has the type set to Unknown, which 696 * seems to imply I need to read the inode and get it. 697 */ 698 is_dir = is_entry_dir(fd, dirp, mode); 699 if ( is_dir && (strcmp(name, ".") && strcmp(name, "..")) && 700 ((mode == SANITY_CHECK_PASS) || (mode == MARK_INODE_NUMS) || 701 ((mode == UPDATE_INODE_NUMS) && (dirp->inode & 0x80000000))) ) { 702 /* A directory! Recurse! */ 703 read_inode(fd, dirp->inode & 0x7fffffff, &tmp_inode); 704 705 if (!S_ISDIR(tmp_inode.i_mode)) { 706 critical_error("inode %d for name %s does not point to a directory\n", 707 dirp->inode & 0x7fffffff, name); 708 } 709 if (verbose) { 710 printf("inode %d %s use extents\n", dirp->inode & 0x7fffffff, 711 (tmp_inode.i_flags & EXT4_EXTENTS_FL) ? "does" : "does not"); 712 } 713 714 tmp_dirsize = tmp_inode.i_blocks_lo * INODE_BLOCK_SIZE; 715 if (verbose) { 716 printf("dir size = %d bytes\n", tmp_dirsize); 717 } 718 719 tmp_dirbuf = malloc(tmp_dirsize); 720 if (tmp_dirbuf == 0) { 721 critical_error("failed to allocate memory for tmp_dirbuf\n"); 722 } 723 724 recurse_dir(fd, &tmp_inode, tmp_dirbuf, tmp_dirsize, mode); 725 726 free(tmp_dirbuf); 727 } 728 729 if (verbose) { 730 if (is_dir) { 731 printf("Directory %s\n", name); 732 } else { 733 printf("Non-directory %s\n", name); 734 } 735 } 736 737 /* Process entry based on current mode. Either set high bit or change inode number */ 738 if (mode == MARK_INODE_NUMS) { 739 dirp->inode |= 0x80000000; 740 } else if (mode == UPDATE_INODE_NUMS) { 741 if (dirp->inode & 0x80000000) { 742 dirp->inode = compute_new_inum(dirp->inode & 0x7fffffff); 743 } 744 } 745 746 /* Point dirp at the next entry */ 747 prev_dirp = dirp; 748 dirp = (struct ext4_dir_entry_2*)((char *)dirp + dirp->rec_len); 749 } 750 751 /* Write out all the blocks for this directory */ 752 for (i = 0; i < num_blocks; i++) { 753 write_block(fd, block_list[i], dirbuf + (i * info.block_size)); 754 } 755 756 free(block_list); 757 758 return 0; 759} 760 761int ext4fixup(char *fsdev) 762{ 763 return ext4fixup_internal(fsdev, 0, 0); 764} 765 766int ext4fixup_internal(char *fsdev, int v_flag, int n_flag) 767{ 768 int fd; 769 struct ext4_inode root_inode; 770 unsigned int dirsize; 771 char *dirbuf; 772 773 if (setjmp(setjmp_env)) 774 return EXIT_FAILURE; /* Handle a call to longjmp() */ 775 776 verbose = v_flag; 777 no_write = n_flag; 778 779 fd = open(fsdev, O_RDWR); 780 781 if (fd < 0) 782 critical_error_errno("failed to open filesystem image"); 783 784 read_ext(fd); 785 786 if ((info.feat_incompat & EXT4_FEATURE_INCOMPAT_FILETYPE) == 0) { 787 critical_error("Expected filesystem to have filetype flag set\n"); 788 } 789 790#if 0 // If we have to fix the directory rec_len issue, we can't use this check 791 /* Check to see if the inodes/group is copacetic */ 792 if (info.inodes_per_blockgroup % (info.block_size/info.inode_size) == 0) { 793 /* This filesystem has either already been updated, or was 794 * made correctly. 795 */ 796 if (verbose) { 797 printf("%s: filesystem correct, no work to do\n", me); 798 } 799 exit(0); 800 } 801#endif 802 803 /* Compute what the new value of inodes_per_blockgroup will be when we're done */ 804 new_inodes_per_group=ALIGN(info.inodes_per_group,(info.block_size/info.inode_size)); 805 806 read_inode(fd, EXT4_ROOT_INO, &root_inode); 807 808 if (!S_ISDIR(root_inode.i_mode)) { 809 critical_error("root inode %d does not point to a directory\n", EXT4_ROOT_INO); 810 } 811 if (verbose) { 812 printf("inode %d %s use extents\n", EXT4_ROOT_INO, 813 (root_inode.i_flags & EXT4_EXTENTS_FL) ? "does" : "does not"); 814 } 815 816 dirsize = root_inode.i_blocks_lo * INODE_BLOCK_SIZE; 817 if (verbose) { 818 printf("root dir size = %d bytes\n", dirsize); 819 } 820 821 dirbuf = malloc(dirsize); 822 if (dirbuf == 0) { 823 critical_error("failed to allocate memory for dirbuf\n"); 824 } 825 826 /* Perform a sanity check pass first, try to catch any errors that will occur 827 * before we actually change anything, so we don't leave a filesystem in a 828 * corrupted, unrecoverable state. Set no_write, make it quiet, and do a recurse 829 * pass and a update_superblock pass. Set flags back to requested state when done. 830 * Only perform sanity check if the state is unset. If the state is _NOT_ unset, 831 * then the tool has already been run and interrupted, and it presumably ran and 832 * passed sanity checked before it got interrupted. It is _NOT_ safe to run sanity 833 * check if state is unset because it assumes inodes are to be computed using the 834 * old inodes/group, but some inode numbers may be updated to the new number. 835 */ 836 if (get_fs_fixup_state(fd) == STATE_UNSET) { 837 verbose = 0; 838 no_write = 1; 839 recurse_dir(fd, &root_inode, dirbuf, dirsize, SANITY_CHECK_PASS); 840 update_superblocks_and_bg_desc(fd, STATE_UNSET); 841 verbose = v_flag; 842 no_write = n_flag; 843 844 set_fs_fixup_state(fd, STATE_MARKING_INUMS); 845 } 846 847 if (get_fs_fixup_state(fd) == STATE_MARKING_INUMS) { 848 if (!recurse_dir(fd, &root_inode, dirbuf, dirsize, MARK_INODE_NUMS)) { 849 set_fs_fixup_state(fd, STATE_UPDATING_INUMS); 850 } 851 } 852 853 if (get_fs_fixup_state(fd) == STATE_UPDATING_INUMS) { 854 if (!recurse_dir(fd, &root_inode, dirbuf, dirsize, UPDATE_INODE_NUMS)) { 855 set_fs_fixup_state(fd, STATE_UPDATING_SB); 856 } 857 } 858 859 if (get_fs_fixup_state(fd) == STATE_UPDATING_SB) { 860 /* set the new inodes/blockgroup number, 861 * and sets the state back to 0. 862 */ 863 if (!update_superblocks_and_bg_desc(fd, STATE_UPDATING_SB)) { 864 set_fs_fixup_state(fd, STATE_UNSET); 865 } 866 } 867 868 close(fd); 869 870 return 0; 871} 872