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