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