• source navigation  • diff markup  • identifier search  • freetext search  • 

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

This page was automatically generated by LXR 0.3.1.  •  OpenWrt