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