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