OSDN Git Service

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