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