1671cd2188e2f224aaeac4955785199f228235719Ken Sumrall/*
2671cd2188e2f224aaeac4955785199f228235719Ken Sumrall * Copyright (C) 2010 The Android Open Source Project
3671cd2188e2f224aaeac4955785199f228235719Ken Sumrall *
4671cd2188e2f224aaeac4955785199f228235719Ken Sumrall * Licensed under the Apache License, Version 2.0 (the "License");
5671cd2188e2f224aaeac4955785199f228235719Ken Sumrall * you may not use this file except in compliance with the License.
6671cd2188e2f224aaeac4955785199f228235719Ken Sumrall * You may obtain a copy of the License at
7671cd2188e2f224aaeac4955785199f228235719Ken Sumrall *
8671cd2188e2f224aaeac4955785199f228235719Ken Sumrall *      http://www.apache.org/licenses/LICENSE-2.0
9671cd2188e2f224aaeac4955785199f228235719Ken Sumrall *
10671cd2188e2f224aaeac4955785199f228235719Ken Sumrall * Unless required by applicable law or agreed to in writing, software
11671cd2188e2f224aaeac4955785199f228235719Ken Sumrall * distributed under the License is distributed on an "AS IS" BASIS,
12671cd2188e2f224aaeac4955785199f228235719Ken Sumrall * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13671cd2188e2f224aaeac4955785199f228235719Ken Sumrall * See the License for the specific language governing permissions and
14671cd2188e2f224aaeac4955785199f228235719Ken Sumrall * limitations under the License.
15671cd2188e2f224aaeac4955785199f228235719Ken Sumrall */
16671cd2188e2f224aaeac4955785199f228235719Ken Sumrall#include "ext4_utils.h"
17671cd2188e2f224aaeac4955785199f228235719Ken Sumrall#include "make_ext4fs.h"
18671cd2188e2f224aaeac4955785199f228235719Ken Sumrall#include "ext4_extents.h"
19671cd2188e2f224aaeac4955785199f228235719Ken Sumrall#include "allocate.h"
20671cd2188e2f224aaeac4955785199f228235719Ken Sumrall#include "ext4fixup.h"
21671cd2188e2f224aaeac4955785199f228235719Ken Sumrall
22dc5abeee1e6fc4827ee0d5ece12aaed2dd56f4c7Colin Cross#include <sparse/sparse.h>
23dc5abeee1e6fc4827ee0d5ece12aaed2dd56f4c7Colin Cross
24671cd2188e2f224aaeac4955785199f228235719Ken Sumrall#include <sys/types.h>
25671cd2188e2f224aaeac4955785199f228235719Ken Sumrall#include <sys/stat.h>
26671cd2188e2f224aaeac4955785199f228235719Ken Sumrall#include <sys/types.h>
27671cd2188e2f224aaeac4955785199f228235719Ken Sumrall#include <fcntl.h>
28671cd2188e2f224aaeac4955785199f228235719Ken Sumrall#include <unistd.h>
29671cd2188e2f224aaeac4955785199f228235719Ken Sumrall
304605b3fb8a00fa37f617a8d0fe3a095d0503a845Raphael Moll#ifndef USE_MINGW
314605b3fb8a00fa37f617a8d0fe3a095d0503a845Raphael Moll#include <sys/mman.h>
324605b3fb8a00fa37f617a8d0fe3a095d0503a845Raphael Moll#endif
334605b3fb8a00fa37f617a8d0fe3a095d0503a845Raphael Moll
34671cd2188e2f224aaeac4955785199f228235719Ken Sumrall#if defined(__APPLE__) && defined(__MACH__)
35671cd2188e2f224aaeac4955785199f228235719Ken Sumrall#define lseek64 lseek
36671cd2188e2f224aaeac4955785199f228235719Ken Sumrall#define off64_t off_t
37671cd2188e2f224aaeac4955785199f228235719Ken Sumrall#endif
38671cd2188e2f224aaeac4955785199f228235719Ken Sumrall
39671cd2188e2f224aaeac4955785199f228235719Ken Sumrall/* The inode block count for a file/directory is in units of 512 byte blocks,
40671cd2188e2f224aaeac4955785199f228235719Ken Sumrall * _NOT_ the filesystem block size!
41671cd2188e2f224aaeac4955785199f228235719Ken Sumrall */
42671cd2188e2f224aaeac4955785199f228235719Ken Sumrall#define INODE_BLOCK_SIZE 512
43671cd2188e2f224aaeac4955785199f228235719Ken Sumrall
44671cd2188e2f224aaeac4955785199f228235719Ken Sumrall#define MAX_EXT4_BLOCK_SIZE 4096
45671cd2188e2f224aaeac4955785199f228235719Ken Sumrall
46671cd2188e2f224aaeac4955785199f228235719Ken Sumrall/* The two modes the recurse_dir() can be in */
47671cd2188e2f224aaeac4955785199f228235719Ken Sumrall#define SANITY_CHECK_PASS 1
48671cd2188e2f224aaeac4955785199f228235719Ken Sumrall#define MARK_INODE_NUMS   2
49671cd2188e2f224aaeac4955785199f228235719Ken Sumrall#define UPDATE_INODE_NUMS 3
50671cd2188e2f224aaeac4955785199f228235719Ken Sumrall
51671cd2188e2f224aaeac4955785199f228235719Ken Sumrall/* Magic numbers to indicate what state the update process is in */
52671cd2188e2f224aaeac4955785199f228235719Ken Sumrall#define MAGIC_STATE_MARKING_INUMS  0x7000151515565512ll
53671cd2188e2f224aaeac4955785199f228235719Ken Sumrall#define MAGIC_STATE_UPDATING_INUMS 0x6121131211735123ll
54671cd2188e2f224aaeac4955785199f228235719Ken Sumrall#define MAGIC_STATE_UPDATING_SB    0x15e1715151558477ll
55671cd2188e2f224aaeac4955785199f228235719Ken Sumrall
56671cd2188e2f224aaeac4955785199f228235719Ken Sumrall/* Internal state variables corresponding to the magic numbers */
57671cd2188e2f224aaeac4955785199f228235719Ken Sumrall#define STATE_UNSET          0
58671cd2188e2f224aaeac4955785199f228235719Ken Sumrall#define STATE_MARKING_INUMS  1
59671cd2188e2f224aaeac4955785199f228235719Ken Sumrall#define STATE_UPDATING_INUMS 2
60671cd2188e2f224aaeac4955785199f228235719Ken Sumrall#define STATE_UPDATING_SB    3
61671cd2188e2f224aaeac4955785199f228235719Ken Sumrall
627e5ff13c55eb29748c07768bd7134819cbcbd4b0Ken Sumrall/* Used for automated testing of this programs ability to stop and be restarted wthout error */
637e5ff13c55eb29748c07768bd7134819cbcbd4b0Ken Sumrallstatic int bail_phase = 0;
647e5ff13c55eb29748c07768bd7134819cbcbd4b0Ken Sumrallstatic int bail_loc = 0;
657e5ff13c55eb29748c07768bd7134819cbcbd4b0Ken Sumrallstatic int bail_count = 0;
667e5ff13c55eb29748c07768bd7134819cbcbd4b0Ken Sumrallstatic int count = 0;
677e5ff13c55eb29748c07768bd7134819cbcbd4b0Ken Sumrall
6897fc910ce0e05862888fd1d9e1938feba40f7539Ken Sumrall/* global flags */
69671cd2188e2f224aaeac4955785199f228235719Ken Sumrallstatic int verbose = 0;
70671cd2188e2f224aaeac4955785199f228235719Ken Sumrallstatic int no_write = 0;
7197fc910ce0e05862888fd1d9e1938feba40f7539Ken Sumrall
72671cd2188e2f224aaeac4955785199f228235719Ken Sumrallstatic int new_inodes_per_group = 0;
7397fc910ce0e05862888fd1d9e1938feba40f7539Ken Sumrall
74671cd2188e2f224aaeac4955785199f228235719Ken Sumrallstatic int no_write_fixup_state = 0;
75671cd2188e2f224aaeac4955785199f228235719Ken Sumrall
76671cd2188e2f224aaeac4955785199f228235719Ken Sumrallstatic int compute_new_inum(unsigned int old_inum)
77671cd2188e2f224aaeac4955785199f228235719Ken Sumrall{
78671cd2188e2f224aaeac4955785199f228235719Ken Sumrall    unsigned int group, offset;
79671cd2188e2f224aaeac4955785199f228235719Ken Sumrall
8097fc910ce0e05862888fd1d9e1938feba40f7539Ken Sumrall    group = (old_inum - 1) / info.inodes_per_group;
8197fc910ce0e05862888fd1d9e1938feba40f7539Ken Sumrall    offset = (old_inum -1) % info.inodes_per_group;
82671cd2188e2f224aaeac4955785199f228235719Ken Sumrall
8397fc910ce0e05862888fd1d9e1938feba40f7539Ken Sumrall    return (group * new_inodes_per_group) + offset + 1;
84671cd2188e2f224aaeac4955785199f228235719Ken Sumrall}
85671cd2188e2f224aaeac4955785199f228235719Ken Sumrall
86671cd2188e2f224aaeac4955785199f228235719Ken Sumrall/* Function to read the primary superblock */
87671cd2188e2f224aaeac4955785199f228235719Ken Sumrallstatic void read_sb(int fd, struct ext4_super_block *sb)
88671cd2188e2f224aaeac4955785199f228235719Ken Sumrall{
89671cd2188e2f224aaeac4955785199f228235719Ken Sumrall    off64_t ret;
90671cd2188e2f224aaeac4955785199f228235719Ken Sumrall
91671cd2188e2f224aaeac4955785199f228235719Ken Sumrall    ret = lseek64(fd, 1024, SEEK_SET);
92671cd2188e2f224aaeac4955785199f228235719Ken Sumrall    if (ret < 0)
93671cd2188e2f224aaeac4955785199f228235719Ken Sumrall        critical_error_errno("failed to seek to superblock");
94671cd2188e2f224aaeac4955785199f228235719Ken Sumrall
95671cd2188e2f224aaeac4955785199f228235719Ken Sumrall    ret = read(fd, sb, sizeof(*sb));
96671cd2188e2f224aaeac4955785199f228235719Ken Sumrall    if (ret < 0)
97671cd2188e2f224aaeac4955785199f228235719Ken Sumrall        critical_error_errno("failed to read superblock");
98671cd2188e2f224aaeac4955785199f228235719Ken Sumrall    if (ret != sizeof(*sb))
99671cd2188e2f224aaeac4955785199f228235719Ken Sumrall        critical_error("failed to read all of superblock");
100671cd2188e2f224aaeac4955785199f228235719Ken Sumrall}
101671cd2188e2f224aaeac4955785199f228235719Ken Sumrall
102671cd2188e2f224aaeac4955785199f228235719Ken Sumrall/* Function to write a primary or backup superblock at a given offset */
103671cd2188e2f224aaeac4955785199f228235719Ken Sumrallstatic void write_sb(int fd, unsigned long long offset, struct ext4_super_block *sb)
104671cd2188e2f224aaeac4955785199f228235719Ken Sumrall{
105671cd2188e2f224aaeac4955785199f228235719Ken Sumrall    off64_t ret;
106671cd2188e2f224aaeac4955785199f228235719Ken Sumrall
107671cd2188e2f224aaeac4955785199f228235719Ken Sumrall    if (no_write) {
108671cd2188e2f224aaeac4955785199f228235719Ken Sumrall        return;
109671cd2188e2f224aaeac4955785199f228235719Ken Sumrall    }
110671cd2188e2f224aaeac4955785199f228235719Ken Sumrall
111671cd2188e2f224aaeac4955785199f228235719Ken Sumrall    ret = lseek64(fd, offset, SEEK_SET);
112671cd2188e2f224aaeac4955785199f228235719Ken Sumrall    if (ret < 0)
113671cd2188e2f224aaeac4955785199f228235719Ken Sumrall        critical_error_errno("failed to seek to superblock");
114671cd2188e2f224aaeac4955785199f228235719Ken Sumrall
115671cd2188e2f224aaeac4955785199f228235719Ken Sumrall    ret = write(fd, sb, sizeof(*sb));
116671cd2188e2f224aaeac4955785199f228235719Ken Sumrall    if (ret < 0)
117671cd2188e2f224aaeac4955785199f228235719Ken Sumrall        critical_error_errno("failed to write superblock");
118671cd2188e2f224aaeac4955785199f228235719Ken Sumrall    if (ret != sizeof(*sb))
119671cd2188e2f224aaeac4955785199f228235719Ken Sumrall        critical_error("failed to write all of superblock");
120671cd2188e2f224aaeac4955785199f228235719Ken Sumrall}
121671cd2188e2f224aaeac4955785199f228235719Ken Sumrall
122671cd2188e2f224aaeac4955785199f228235719Ken Sumrallstatic int get_fs_fixup_state(int fd)
123671cd2188e2f224aaeac4955785199f228235719Ken Sumrall{
124671cd2188e2f224aaeac4955785199f228235719Ken Sumrall    unsigned long long magic;
125671cd2188e2f224aaeac4955785199f228235719Ken Sumrall    int ret, len;
126671cd2188e2f224aaeac4955785199f228235719Ken Sumrall
127671cd2188e2f224aaeac4955785199f228235719Ken Sumrall    if (no_write) {
128671cd2188e2f224aaeac4955785199f228235719Ken Sumrall        return no_write_fixup_state;
129671cd2188e2f224aaeac4955785199f228235719Ken Sumrall    }
130671cd2188e2f224aaeac4955785199f228235719Ken Sumrall
131671cd2188e2f224aaeac4955785199f228235719Ken Sumrall    lseek64(fd, 0, SEEK_SET);
132671cd2188e2f224aaeac4955785199f228235719Ken Sumrall    len = read(fd, &magic, sizeof(magic));
133671cd2188e2f224aaeac4955785199f228235719Ken Sumrall    if (len != sizeof(magic)) {
134671cd2188e2f224aaeac4955785199f228235719Ken Sumrall        critical_error("cannot read fixup_state\n");
135671cd2188e2f224aaeac4955785199f228235719Ken Sumrall    }
136671cd2188e2f224aaeac4955785199f228235719Ken Sumrall
137671cd2188e2f224aaeac4955785199f228235719Ken Sumrall    switch (magic) {
138671cd2188e2f224aaeac4955785199f228235719Ken Sumrall        case MAGIC_STATE_MARKING_INUMS:
139671cd2188e2f224aaeac4955785199f228235719Ken Sumrall            ret = STATE_MARKING_INUMS;
140671cd2188e2f224aaeac4955785199f228235719Ken Sumrall            break;
141671cd2188e2f224aaeac4955785199f228235719Ken Sumrall        case MAGIC_STATE_UPDATING_INUMS:
142671cd2188e2f224aaeac4955785199f228235719Ken Sumrall            ret = STATE_UPDATING_INUMS;
143671cd2188e2f224aaeac4955785199f228235719Ken Sumrall            break;
144671cd2188e2f224aaeac4955785199f228235719Ken Sumrall        case MAGIC_STATE_UPDATING_SB:
145671cd2188e2f224aaeac4955785199f228235719Ken Sumrall            ret = STATE_UPDATING_SB;
146671cd2188e2f224aaeac4955785199f228235719Ken Sumrall            break;
147671cd2188e2f224aaeac4955785199f228235719Ken Sumrall        default:
148671cd2188e2f224aaeac4955785199f228235719Ken Sumrall            ret = STATE_UNSET;
149671cd2188e2f224aaeac4955785199f228235719Ken Sumrall    }
150671cd2188e2f224aaeac4955785199f228235719Ken Sumrall    return ret;
151671cd2188e2f224aaeac4955785199f228235719Ken Sumrall}
152671cd2188e2f224aaeac4955785199f228235719Ken Sumrall
153671cd2188e2f224aaeac4955785199f228235719Ken Sumrallstatic int set_fs_fixup_state(int fd, int state)
154671cd2188e2f224aaeac4955785199f228235719Ken Sumrall{
155671cd2188e2f224aaeac4955785199f228235719Ken Sumrall    unsigned long long magic;
156671cd2188e2f224aaeac4955785199f228235719Ken Sumrall    struct ext4_super_block sb;
157671cd2188e2f224aaeac4955785199f228235719Ken Sumrall    int len;
158671cd2188e2f224aaeac4955785199f228235719Ken Sumrall
159671cd2188e2f224aaeac4955785199f228235719Ken Sumrall    if (no_write) {
160671cd2188e2f224aaeac4955785199f228235719Ken Sumrall        no_write_fixup_state = state;
161671cd2188e2f224aaeac4955785199f228235719Ken Sumrall        return 0;
162671cd2188e2f224aaeac4955785199f228235719Ken Sumrall    }
163671cd2188e2f224aaeac4955785199f228235719Ken Sumrall
164671cd2188e2f224aaeac4955785199f228235719Ken Sumrall    switch (state) {
165671cd2188e2f224aaeac4955785199f228235719Ken Sumrall        case STATE_MARKING_INUMS:
166671cd2188e2f224aaeac4955785199f228235719Ken Sumrall            magic = MAGIC_STATE_MARKING_INUMS;
167671cd2188e2f224aaeac4955785199f228235719Ken Sumrall            break;
168671cd2188e2f224aaeac4955785199f228235719Ken Sumrall        case STATE_UPDATING_INUMS:
169671cd2188e2f224aaeac4955785199f228235719Ken Sumrall            magic = MAGIC_STATE_UPDATING_INUMS;
170671cd2188e2f224aaeac4955785199f228235719Ken Sumrall            break;
171671cd2188e2f224aaeac4955785199f228235719Ken Sumrall        case STATE_UPDATING_SB:
172671cd2188e2f224aaeac4955785199f228235719Ken Sumrall            magic = MAGIC_STATE_UPDATING_SB;
173671cd2188e2f224aaeac4955785199f228235719Ken Sumrall            break;
174671cd2188e2f224aaeac4955785199f228235719Ken Sumrall        case STATE_UNSET:
175671cd2188e2f224aaeac4955785199f228235719Ken Sumrall        default:
176671cd2188e2f224aaeac4955785199f228235719Ken Sumrall            magic = 0ll;
177671cd2188e2f224aaeac4955785199f228235719Ken Sumrall            break;
178671cd2188e2f224aaeac4955785199f228235719Ken Sumrall    }
179671cd2188e2f224aaeac4955785199f228235719Ken Sumrall
180671cd2188e2f224aaeac4955785199f228235719Ken Sumrall    lseek64(fd, 0, SEEK_SET);
181671cd2188e2f224aaeac4955785199f228235719Ken Sumrall    len = write(fd, &magic, sizeof(magic));
182671cd2188e2f224aaeac4955785199f228235719Ken Sumrall    if (len != sizeof(magic)) {
183671cd2188e2f224aaeac4955785199f228235719Ken Sumrall        critical_error("cannot write fixup_state\n");
184671cd2188e2f224aaeac4955785199f228235719Ken Sumrall    }
185671cd2188e2f224aaeac4955785199f228235719Ken Sumrall
186671cd2188e2f224aaeac4955785199f228235719Ken Sumrall    read_sb(fd, &sb);
187671cd2188e2f224aaeac4955785199f228235719Ken Sumrall    if (magic) {
188671cd2188e2f224aaeac4955785199f228235719Ken Sumrall        /* If we are in the process of updating the filesystem, make it unmountable */
189671cd2188e2f224aaeac4955785199f228235719Ken Sumrall        sb.s_desc_size |= 1;
190671cd2188e2f224aaeac4955785199f228235719Ken Sumrall    } else {
191671cd2188e2f224aaeac4955785199f228235719Ken Sumrall        /* we are done, so make the filesystem mountable again */
192671cd2188e2f224aaeac4955785199f228235719Ken Sumrall        sb.s_desc_size &= ~1;
193671cd2188e2f224aaeac4955785199f228235719Ken Sumrall    }
194671cd2188e2f224aaeac4955785199f228235719Ken Sumrall    write_sb(fd, 1024, &sb);
195671cd2188e2f224aaeac4955785199f228235719Ken Sumrall
196671cd2188e2f224aaeac4955785199f228235719Ken Sumrall    return 0;
197671cd2188e2f224aaeac4955785199f228235719Ken Sumrall}
198671cd2188e2f224aaeac4955785199f228235719Ken Sumrall
199671cd2188e2f224aaeac4955785199f228235719Ken Sumrallstatic int read_ext(int fd)
200671cd2188e2f224aaeac4955785199f228235719Ken Sumrall{
201671cd2188e2f224aaeac4955785199f228235719Ken Sumrall    off64_t ret;
202671cd2188e2f224aaeac4955785199f228235719Ken Sumrall    struct ext4_super_block sb;
203671cd2188e2f224aaeac4955785199f228235719Ken Sumrall    unsigned int i;
204671cd2188e2f224aaeac4955785199f228235719Ken Sumrall
205671cd2188e2f224aaeac4955785199f228235719Ken Sumrall    read_sb(fd, &sb);
206671cd2188e2f224aaeac4955785199f228235719Ken Sumrall
207671cd2188e2f224aaeac4955785199f228235719Ken Sumrall    ext4_parse_sb(&sb);
208671cd2188e2f224aaeac4955785199f228235719Ken Sumrall
20997fc910ce0e05862888fd1d9e1938feba40f7539Ken Sumrall    if (info.feat_incompat & EXT4_FEATURE_INCOMPAT_RECOVER) {
21097fc910ce0e05862888fd1d9e1938feba40f7539Ken Sumrall        critical_error("Filesystem needs recovery first, mount and unmount to do that\n");
21197fc910ce0e05862888fd1d9e1938feba40f7539Ken Sumrall    }
21297fc910ce0e05862888fd1d9e1938feba40f7539Ken Sumrall
213671cd2188e2f224aaeac4955785199f228235719Ken Sumrall    /* Clear the low bit which is set while this tool is in progress.
214671cd2188e2f224aaeac4955785199f228235719Ken Sumrall     * If the tool crashes, it will still be set when we restart.
215671cd2188e2f224aaeac4955785199f228235719Ken Sumrall     * The low bit is set to make the filesystem unmountable while
216671cd2188e2f224aaeac4955785199f228235719Ken Sumrall     * it is being fixed up.  Also allow 0, which means the old ext2
217671cd2188e2f224aaeac4955785199f228235719Ken Sumrall     * size is in use.
218671cd2188e2f224aaeac4955785199f228235719Ken Sumrall     */
219671cd2188e2f224aaeac4955785199f228235719Ken Sumrall    if (((sb.s_desc_size & ~1) != sizeof(struct ext2_group_desc)) &&
220671cd2188e2f224aaeac4955785199f228235719Ken Sumrall        ((sb.s_desc_size & ~1) != 0))
221671cd2188e2f224aaeac4955785199f228235719Ken Sumrall        critical_error("error: bg_desc_size != sizeof(struct ext2_group_desc)\n");
222671cd2188e2f224aaeac4955785199f228235719Ken Sumrall
223671cd2188e2f224aaeac4955785199f228235719Ken Sumrall    ret = lseek64(fd, info.len, SEEK_SET);
224671cd2188e2f224aaeac4955785199f228235719Ken Sumrall    if (ret < 0)
225671cd2188e2f224aaeac4955785199f228235719Ken Sumrall        critical_error_errno("failed to seek to end of input image");
226671cd2188e2f224aaeac4955785199f228235719Ken Sumrall
227671cd2188e2f224aaeac4955785199f228235719Ken Sumrall    ret = lseek64(fd, info.block_size * (aux_info.first_data_block + 1), SEEK_SET);
228671cd2188e2f224aaeac4955785199f228235719Ken Sumrall    if (ret < 0)
229671cd2188e2f224aaeac4955785199f228235719Ken Sumrall        critical_error_errno("failed to seek to block group descriptors");
230671cd2188e2f224aaeac4955785199f228235719Ken Sumrall
231671cd2188e2f224aaeac4955785199f228235719Ken Sumrall    ret = read(fd, aux_info.bg_desc, info.block_size * aux_info.bg_desc_blocks);
232671cd2188e2f224aaeac4955785199f228235719Ken Sumrall    if (ret < 0)
233671cd2188e2f224aaeac4955785199f228235719Ken Sumrall        critical_error_errno("failed to read block group descriptors");
234671cd2188e2f224aaeac4955785199f228235719Ken Sumrall    if (ret != (int)info.block_size * (int)aux_info.bg_desc_blocks)
235671cd2188e2f224aaeac4955785199f228235719Ken Sumrall        critical_error("failed to read all of block group descriptors");
236671cd2188e2f224aaeac4955785199f228235719Ken Sumrall
237671cd2188e2f224aaeac4955785199f228235719Ken Sumrall    if (verbose) {
238671cd2188e2f224aaeac4955785199f228235719Ken Sumrall        printf("Found filesystem with parameters:\n");
239671cd2188e2f224aaeac4955785199f228235719Ken Sumrall        printf("    Size: %llu\n", info.len);
240671cd2188e2f224aaeac4955785199f228235719Ken Sumrall        printf("    Block size: %d\n", info.block_size);
241671cd2188e2f224aaeac4955785199f228235719Ken Sumrall        printf("    Blocks per group: %d\n", info.blocks_per_group);
242671cd2188e2f224aaeac4955785199f228235719Ken Sumrall        printf("    Inodes per group: %d\n", info.inodes_per_group);
243671cd2188e2f224aaeac4955785199f228235719Ken Sumrall        printf("    Inode size: %d\n", info.inode_size);
244671cd2188e2f224aaeac4955785199f228235719Ken Sumrall        printf("    Label: %s\n", info.label);
245671cd2188e2f224aaeac4955785199f228235719Ken Sumrall        printf("    Blocks: %llu\n", aux_info.len_blocks);
246671cd2188e2f224aaeac4955785199f228235719Ken Sumrall        printf("    Block groups: %d\n", aux_info.groups);
247671cd2188e2f224aaeac4955785199f228235719Ken Sumrall        printf("    Reserved block group size: %d\n", info.bg_desc_reserve_blocks);
248671cd2188e2f224aaeac4955785199f228235719Ken Sumrall        printf("    Used %d/%d inodes and %d/%d blocks\n",
249671cd2188e2f224aaeac4955785199f228235719Ken Sumrall                aux_info.sb->s_inodes_count - aux_info.sb->s_free_inodes_count,
250671cd2188e2f224aaeac4955785199f228235719Ken Sumrall                aux_info.sb->s_inodes_count,
251671cd2188e2f224aaeac4955785199f228235719Ken Sumrall                aux_info.sb->s_blocks_count_lo - aux_info.sb->s_free_blocks_count_lo,
252671cd2188e2f224aaeac4955785199f228235719Ken Sumrall                aux_info.sb->s_blocks_count_lo);
253671cd2188e2f224aaeac4955785199f228235719Ken Sumrall    }
254671cd2188e2f224aaeac4955785199f228235719Ken Sumrall
255671cd2188e2f224aaeac4955785199f228235719Ken Sumrall    return 0;
256671cd2188e2f224aaeac4955785199f228235719Ken Sumrall}
257671cd2188e2f224aaeac4955785199f228235719Ken Sumrall
258671cd2188e2f224aaeac4955785199f228235719Ken Sumrallstatic int read_inode(int fd, unsigned int inum, struct ext4_inode *inode)
259671cd2188e2f224aaeac4955785199f228235719Ken Sumrall{
260671cd2188e2f224aaeac4955785199f228235719Ken Sumrall    unsigned int bg_num, bg_offset;
261671cd2188e2f224aaeac4955785199f228235719Ken Sumrall    off64_t inode_offset;
262671cd2188e2f224aaeac4955785199f228235719Ken Sumrall    int len;
263671cd2188e2f224aaeac4955785199f228235719Ken Sumrall
264671cd2188e2f224aaeac4955785199f228235719Ken Sumrall    bg_num = (inum-1) / info.inodes_per_group;
265671cd2188e2f224aaeac4955785199f228235719Ken Sumrall    bg_offset = (inum-1) % info.inodes_per_group;
266671cd2188e2f224aaeac4955785199f228235719Ken Sumrall
267671cd2188e2f224aaeac4955785199f228235719Ken Sumrall    inode_offset = ((unsigned long long)aux_info.bg_desc[bg_num].bg_inode_table * info.block_size) +
268671cd2188e2f224aaeac4955785199f228235719Ken Sumrall                    (bg_offset * info.inode_size);
269671cd2188e2f224aaeac4955785199f228235719Ken Sumrall
270671cd2188e2f224aaeac4955785199f228235719Ken Sumrall    if (lseek64(fd, inode_offset, SEEK_SET) < 0) {
271671cd2188e2f224aaeac4955785199f228235719Ken Sumrall        critical_error_errno("failed to seek to inode %d\n", inum);
272671cd2188e2f224aaeac4955785199f228235719Ken Sumrall    }
273671cd2188e2f224aaeac4955785199f228235719Ken Sumrall
274671cd2188e2f224aaeac4955785199f228235719Ken Sumrall    len=read(fd, inode, sizeof(*inode));
275671cd2188e2f224aaeac4955785199f228235719Ken Sumrall    if (len != sizeof(*inode)) {
276671cd2188e2f224aaeac4955785199f228235719Ken Sumrall        critical_error_errno("failed to read inode %d\n", inum);
277671cd2188e2f224aaeac4955785199f228235719Ken Sumrall    }
278671cd2188e2f224aaeac4955785199f228235719Ken Sumrall
279671cd2188e2f224aaeac4955785199f228235719Ken Sumrall    return 0;
280671cd2188e2f224aaeac4955785199f228235719Ken Sumrall}
281671cd2188e2f224aaeac4955785199f228235719Ken Sumrall
282671cd2188e2f224aaeac4955785199f228235719Ken Sumrallstatic int read_block(int fd, unsigned long long block_num, void *block)
283671cd2188e2f224aaeac4955785199f228235719Ken Sumrall{
284671cd2188e2f224aaeac4955785199f228235719Ken Sumrall    off64_t off;
285671cd2188e2f224aaeac4955785199f228235719Ken Sumrall    unsigned int len;
286671cd2188e2f224aaeac4955785199f228235719Ken Sumrall
287671cd2188e2f224aaeac4955785199f228235719Ken Sumrall    off = block_num * info.block_size;
288671cd2188e2f224aaeac4955785199f228235719Ken Sumrall
289671cd2188e2f224aaeac4955785199f228235719Ken Sumrall    if (lseek64(fd, off, SEEK_SET) , 0) {
290671cd2188e2f224aaeac4955785199f228235719Ken Sumrall        critical_error_errno("failed to seek to block %lld\n", block_num);
291671cd2188e2f224aaeac4955785199f228235719Ken Sumrall    }
292671cd2188e2f224aaeac4955785199f228235719Ken Sumrall
293671cd2188e2f224aaeac4955785199f228235719Ken Sumrall    len=read(fd, block, info.block_size);
294671cd2188e2f224aaeac4955785199f228235719Ken Sumrall    if (len != info.block_size) {
295671cd2188e2f224aaeac4955785199f228235719Ken Sumrall        critical_error_errno("failed to read block %lld\n", block_num);
296671cd2188e2f224aaeac4955785199f228235719Ken Sumrall    }
297671cd2188e2f224aaeac4955785199f228235719Ken Sumrall
298671cd2188e2f224aaeac4955785199f228235719Ken Sumrall    return 0;
299671cd2188e2f224aaeac4955785199f228235719Ken Sumrall}
300671cd2188e2f224aaeac4955785199f228235719Ken Sumrall
301671cd2188e2f224aaeac4955785199f228235719Ken Sumrallstatic int write_block(int fd, unsigned long long block_num, void *block)
302671cd2188e2f224aaeac4955785199f228235719Ken Sumrall{
303671cd2188e2f224aaeac4955785199f228235719Ken Sumrall    off64_t off;
304671cd2188e2f224aaeac4955785199f228235719Ken Sumrall    unsigned int len;
305671cd2188e2f224aaeac4955785199f228235719Ken Sumrall
306671cd2188e2f224aaeac4955785199f228235719Ken Sumrall    if (no_write) {
307671cd2188e2f224aaeac4955785199f228235719Ken Sumrall        return 0;
308671cd2188e2f224aaeac4955785199f228235719Ken Sumrall    }
309671cd2188e2f224aaeac4955785199f228235719Ken Sumrall
310671cd2188e2f224aaeac4955785199f228235719Ken Sumrall    off = block_num * info.block_size;
311671cd2188e2f224aaeac4955785199f228235719Ken Sumrall
312671cd2188e2f224aaeac4955785199f228235719Ken Sumrall    if (lseek64(fd, off, SEEK_SET) < 0) {
313671cd2188e2f224aaeac4955785199f228235719Ken Sumrall        critical_error_errno("failed to seek to block %lld\n", block_num);
314671cd2188e2f224aaeac4955785199f228235719Ken Sumrall    }
315671cd2188e2f224aaeac4955785199f228235719Ken Sumrall
316671cd2188e2f224aaeac4955785199f228235719Ken Sumrall    len=write(fd, block, info.block_size);
317671cd2188e2f224aaeac4955785199f228235719Ken Sumrall    if (len != info.block_size) {
318671cd2188e2f224aaeac4955785199f228235719Ken Sumrall        critical_error_errno("failed to write block %lld\n", block_num);
319671cd2188e2f224aaeac4955785199f228235719Ken Sumrall    }
320671cd2188e2f224aaeac4955785199f228235719Ken Sumrall
321671cd2188e2f224aaeac4955785199f228235719Ken Sumrall    return 0;
322671cd2188e2f224aaeac4955785199f228235719Ken Sumrall}
323671cd2188e2f224aaeac4955785199f228235719Ken Sumrall
324671cd2188e2f224aaeac4955785199f228235719Ken Sumrallstatic int bitmap_get_bit(u8 *bitmap, u32 bit)
325671cd2188e2f224aaeac4955785199f228235719Ken Sumrall{
326671cd2188e2f224aaeac4955785199f228235719Ken Sumrall        if (bitmap[bit / 8] & (1 << (bit % 8)))
327671cd2188e2f224aaeac4955785199f228235719Ken Sumrall                return 1;
328671cd2188e2f224aaeac4955785199f228235719Ken Sumrall
329671cd2188e2f224aaeac4955785199f228235719Ken Sumrall        return 0;
330671cd2188e2f224aaeac4955785199f228235719Ken Sumrall}
331671cd2188e2f224aaeac4955785199f228235719Ken Sumrall
332671cd2188e2f224aaeac4955785199f228235719Ken Sumrallstatic void bitmap_clear_bit(u8 *bitmap, u32 bit)
333671cd2188e2f224aaeac4955785199f228235719Ken Sumrall{
334671cd2188e2f224aaeac4955785199f228235719Ken Sumrall        bitmap[bit / 8] &= ~(1 << (bit % 8));
335671cd2188e2f224aaeac4955785199f228235719Ken Sumrall
336671cd2188e2f224aaeac4955785199f228235719Ken Sumrall        return;
337671cd2188e2f224aaeac4955785199f228235719Ken Sumrall}
338671cd2188e2f224aaeac4955785199f228235719Ken Sumrall
339671cd2188e2f224aaeac4955785199f228235719Ken Sumrallstatic void check_inode_bitmap(int fd, unsigned int bg_num)
340671cd2188e2f224aaeac4955785199f228235719Ken Sumrall{
341671cd2188e2f224aaeac4955785199f228235719Ken Sumrall    unsigned int inode_bitmap_block_num;
342671cd2188e2f224aaeac4955785199f228235719Ken Sumrall    unsigned char block[MAX_EXT4_BLOCK_SIZE];
343671cd2188e2f224aaeac4955785199f228235719Ken Sumrall    int i, bitmap_updated = 0;
344671cd2188e2f224aaeac4955785199f228235719Ken Sumrall
345671cd2188e2f224aaeac4955785199f228235719Ken Sumrall    /* Using the bg_num, aux_info.bg_desc[], info.inodes_per_group and
346671cd2188e2f224aaeac4955785199f228235719Ken Sumrall     * new_inodes_per_group, retrieve the inode bitmap, and make sure
347671cd2188e2f224aaeac4955785199f228235719Ken Sumrall     * the bits between the old and new size are clear
348671cd2188e2f224aaeac4955785199f228235719Ken Sumrall     */
349671cd2188e2f224aaeac4955785199f228235719Ken Sumrall    inode_bitmap_block_num = aux_info.bg_desc[bg_num].bg_inode_bitmap;
350671cd2188e2f224aaeac4955785199f228235719Ken Sumrall
351671cd2188e2f224aaeac4955785199f228235719Ken Sumrall    read_block(fd, inode_bitmap_block_num, block);
352671cd2188e2f224aaeac4955785199f228235719Ken Sumrall
353671cd2188e2f224aaeac4955785199f228235719Ken Sumrall    for (i = info.inodes_per_group; i < new_inodes_per_group; i++) {
354671cd2188e2f224aaeac4955785199f228235719Ken Sumrall        if (bitmap_get_bit(block, i)) {
355671cd2188e2f224aaeac4955785199f228235719Ken Sumrall            bitmap_clear_bit(block, i);
356671cd2188e2f224aaeac4955785199f228235719Ken Sumrall            bitmap_updated = 1;
357671cd2188e2f224aaeac4955785199f228235719Ken Sumrall        }
358671cd2188e2f224aaeac4955785199f228235719Ken Sumrall    }
359671cd2188e2f224aaeac4955785199f228235719Ken Sumrall
360671cd2188e2f224aaeac4955785199f228235719Ken Sumrall    if (bitmap_updated) {
361671cd2188e2f224aaeac4955785199f228235719Ken Sumrall        if (verbose) {
362671cd2188e2f224aaeac4955785199f228235719Ken Sumrall            printf("Warning: updated inode bitmap for block group %d\n", bg_num);
363671cd2188e2f224aaeac4955785199f228235719Ken Sumrall        }
364671cd2188e2f224aaeac4955785199f228235719Ken Sumrall        write_block(fd, inode_bitmap_block_num, block);
365671cd2188e2f224aaeac4955785199f228235719Ken Sumrall    }
366671cd2188e2f224aaeac4955785199f228235719Ken Sumrall
367671cd2188e2f224aaeac4955785199f228235719Ken Sumrall    return;
368671cd2188e2f224aaeac4955785199f228235719Ken Sumrall}
369671cd2188e2f224aaeac4955785199f228235719Ken Sumrall
370671cd2188e2f224aaeac4955785199f228235719Ken Sumrall/* Update the superblock and bgdesc of the specified block group */
371671cd2188e2f224aaeac4955785199f228235719Ken Sumrallstatic int update_superblocks_and_bg_desc(int fd, int state)
372671cd2188e2f224aaeac4955785199f228235719Ken Sumrall{
373671cd2188e2f224aaeac4955785199f228235719Ken Sumrall    off64_t ret;
374671cd2188e2f224aaeac4955785199f228235719Ken Sumrall    struct ext4_super_block sb;
375671cd2188e2f224aaeac4955785199f228235719Ken Sumrall    unsigned int num_block_groups, total_new_inodes;
376671cd2188e2f224aaeac4955785199f228235719Ken Sumrall    unsigned int i;
377671cd2188e2f224aaeac4955785199f228235719Ken Sumrall
378671cd2188e2f224aaeac4955785199f228235719Ken Sumrall
379671cd2188e2f224aaeac4955785199f228235719Ken Sumrall    read_sb(fd, &sb);
380671cd2188e2f224aaeac4955785199f228235719Ken Sumrall
381671cd2188e2f224aaeac4955785199f228235719Ken Sumrall    /* Compute how many more inodes are now available */
382671cd2188e2f224aaeac4955785199f228235719Ken Sumrall    num_block_groups = DIV_ROUND_UP(aux_info.len_blocks, info.blocks_per_group);
383671cd2188e2f224aaeac4955785199f228235719Ken Sumrall    total_new_inodes = num_block_groups * (new_inodes_per_group - sb.s_inodes_per_group);
384671cd2188e2f224aaeac4955785199f228235719Ken Sumrall
385671cd2188e2f224aaeac4955785199f228235719Ken Sumrall    if (verbose) {
386671cd2188e2f224aaeac4955785199f228235719Ken Sumrall        printf("created %d additional inodes\n", total_new_inodes);
387671cd2188e2f224aaeac4955785199f228235719Ken Sumrall    }
388671cd2188e2f224aaeac4955785199f228235719Ken Sumrall
389671cd2188e2f224aaeac4955785199f228235719Ken Sumrall    /* Update the free inodes count in each block group descriptor */
390671cd2188e2f224aaeac4955785199f228235719Ken Sumrall    for (i = 0; i < num_block_groups; i++) {
391671cd2188e2f224aaeac4955785199f228235719Ken Sumrall       if (state == STATE_UPDATING_SB) {
392671cd2188e2f224aaeac4955785199f228235719Ken Sumrall           aux_info.bg_desc[i].bg_free_inodes_count += (new_inodes_per_group - sb.s_inodes_per_group);
393671cd2188e2f224aaeac4955785199f228235719Ken Sumrall       }
394671cd2188e2f224aaeac4955785199f228235719Ken Sumrall       check_inode_bitmap(fd, i);
395671cd2188e2f224aaeac4955785199f228235719Ken Sumrall    }
396671cd2188e2f224aaeac4955785199f228235719Ken Sumrall
397671cd2188e2f224aaeac4955785199f228235719Ken Sumrall    /* First some sanity checks */
398671cd2188e2f224aaeac4955785199f228235719Ken Sumrall    if ((sb.s_inodes_count + total_new_inodes) != (new_inodes_per_group * num_block_groups)) {
399671cd2188e2f224aaeac4955785199f228235719Ken Sumrall        critical_error("Failed sanity check on new inode count\n");
400671cd2188e2f224aaeac4955785199f228235719Ken Sumrall    }
401671cd2188e2f224aaeac4955785199f228235719Ken Sumrall    if (new_inodes_per_group % (info.block_size/info.inode_size)) {
402671cd2188e2f224aaeac4955785199f228235719Ken Sumrall        critical_error("Failed sanity check on new inode per group alignment\n");
403671cd2188e2f224aaeac4955785199f228235719Ken Sumrall    }
404671cd2188e2f224aaeac4955785199f228235719Ken Sumrall
405671cd2188e2f224aaeac4955785199f228235719Ken Sumrall    /* Update the free inodes count in the superblock */
406671cd2188e2f224aaeac4955785199f228235719Ken Sumrall    sb.s_inodes_count += total_new_inodes;
407671cd2188e2f224aaeac4955785199f228235719Ken Sumrall    sb.s_free_inodes_count += total_new_inodes;
408671cd2188e2f224aaeac4955785199f228235719Ken Sumrall    sb.s_inodes_per_group = new_inodes_per_group;
409671cd2188e2f224aaeac4955785199f228235719Ken Sumrall
410671cd2188e2f224aaeac4955785199f228235719Ken Sumrall    for (i = 0; i < aux_info.groups; i++) {
411671cd2188e2f224aaeac4955785199f228235719Ken Sumrall        if (ext4_bg_has_super_block(i)) {
412671cd2188e2f224aaeac4955785199f228235719Ken Sumrall            unsigned int sb_offset;
413671cd2188e2f224aaeac4955785199f228235719Ken Sumrall
414671cd2188e2f224aaeac4955785199f228235719Ken Sumrall            if (i == 0) {
415671cd2188e2f224aaeac4955785199f228235719Ken Sumrall              /* The first superblock is offset by 1K to leave room for boot sectors */
416671cd2188e2f224aaeac4955785199f228235719Ken Sumrall              sb_offset = 1024;
417671cd2188e2f224aaeac4955785199f228235719Ken Sumrall            } else {
418671cd2188e2f224aaeac4955785199f228235719Ken Sumrall              sb_offset = 0;
419671cd2188e2f224aaeac4955785199f228235719Ken Sumrall            }
420671cd2188e2f224aaeac4955785199f228235719Ken Sumrall
421671cd2188e2f224aaeac4955785199f228235719Ken Sumrall            sb.s_block_group_nr = i;
422671cd2188e2f224aaeac4955785199f228235719Ken Sumrall            /* Don't write out the backup superblocks with the bit set in the s_desc_size
423671cd2188e2f224aaeac4955785199f228235719Ken Sumrall             * which prevents the filesystem from mounting.  The bit for the primary
424671cd2188e2f224aaeac4955785199f228235719Ken Sumrall             * superblock will be cleared on the final call to set_fs_fixup_state() */
425671cd2188e2f224aaeac4955785199f228235719Ken Sumrall            if (i != 0) {
426671cd2188e2f224aaeac4955785199f228235719Ken Sumrall                sb.s_desc_size &= ~1;
427671cd2188e2f224aaeac4955785199f228235719Ken Sumrall            }
428671cd2188e2f224aaeac4955785199f228235719Ken Sumrall
429671cd2188e2f224aaeac4955785199f228235719Ken Sumrall            write_sb(fd, (unsigned long long)i * info.blocks_per_group * info.block_size + sb_offset, &sb);
430671cd2188e2f224aaeac4955785199f228235719Ken Sumrall
431671cd2188e2f224aaeac4955785199f228235719Ken Sumrall            ret = lseek64(fd, ((unsigned long long)i * info.blocks_per_group * info.block_size) +
432671cd2188e2f224aaeac4955785199f228235719Ken Sumrall                              (info.block_size * (aux_info.first_data_block + 1)), SEEK_SET);
433671cd2188e2f224aaeac4955785199f228235719Ken Sumrall            if (ret < 0)
434671cd2188e2f224aaeac4955785199f228235719Ken Sumrall                critical_error_errno("failed to seek to block group descriptors");
435671cd2188e2f224aaeac4955785199f228235719Ken Sumrall
436671cd2188e2f224aaeac4955785199f228235719Ken Sumrall            if (!no_write) {
437671cd2188e2f224aaeac4955785199f228235719Ken Sumrall                ret = write(fd, aux_info.bg_desc, info.block_size * aux_info.bg_desc_blocks);
438671cd2188e2f224aaeac4955785199f228235719Ken Sumrall                if (ret < 0)
439671cd2188e2f224aaeac4955785199f228235719Ken Sumrall                    critical_error_errno("failed to write block group descriptors");
440671cd2188e2f224aaeac4955785199f228235719Ken Sumrall                if (ret != (int)info.block_size * (int)aux_info.bg_desc_blocks)
441671cd2188e2f224aaeac4955785199f228235719Ken Sumrall                    critical_error("failed to write all of block group descriptors");
442671cd2188e2f224aaeac4955785199f228235719Ken Sumrall            }
443671cd2188e2f224aaeac4955785199f228235719Ken Sumrall        }
4447e5ff13c55eb29748c07768bd7134819cbcbd4b0Ken Sumrall        if ((bail_phase == 4) && ((unsigned int)bail_count == i)) {
4457e5ff13c55eb29748c07768bd7134819cbcbd4b0Ken Sumrall            critical_error("bailing at phase 4\n");
4467e5ff13c55eb29748c07768bd7134819cbcbd4b0Ken Sumrall        }
447671cd2188e2f224aaeac4955785199f228235719Ken Sumrall    }
448671cd2188e2f224aaeac4955785199f228235719Ken Sumrall
449671cd2188e2f224aaeac4955785199f228235719Ken Sumrall    return 0;
450671cd2188e2f224aaeac4955785199f228235719Ken Sumrall}
451671cd2188e2f224aaeac4955785199f228235719Ken Sumrall
452671cd2188e2f224aaeac4955785199f228235719Ken Sumrall
453671cd2188e2f224aaeac4955785199f228235719Ken Sumrallstatic int get_direct_blocks(struct ext4_inode *inode, unsigned long long *block_list,
454671cd2188e2f224aaeac4955785199f228235719Ken Sumrall                                                       unsigned int *count)
455671cd2188e2f224aaeac4955785199f228235719Ken Sumrall{
456671cd2188e2f224aaeac4955785199f228235719Ken Sumrall    unsigned int i = 0;
457671cd2188e2f224aaeac4955785199f228235719Ken Sumrall    unsigned int ret = 0;
458671cd2188e2f224aaeac4955785199f228235719Ken Sumrall    unsigned int sectors_per_block;
459671cd2188e2f224aaeac4955785199f228235719Ken Sumrall
460671cd2188e2f224aaeac4955785199f228235719Ken Sumrall    sectors_per_block = info.block_size / INODE_BLOCK_SIZE;
461671cd2188e2f224aaeac4955785199f228235719Ken Sumrall    while ((i < (inode->i_blocks_lo / sectors_per_block)) && (i < EXT4_NDIR_BLOCKS)) {
462671cd2188e2f224aaeac4955785199f228235719Ken Sumrall        block_list[i] = inode->i_block[i];
463671cd2188e2f224aaeac4955785199f228235719Ken Sumrall        i++;
464671cd2188e2f224aaeac4955785199f228235719Ken Sumrall    }
465671cd2188e2f224aaeac4955785199f228235719Ken Sumrall
466671cd2188e2f224aaeac4955785199f228235719Ken Sumrall    *count += i;
467671cd2188e2f224aaeac4955785199f228235719Ken Sumrall
468671cd2188e2f224aaeac4955785199f228235719Ken Sumrall    if ((inode->i_blocks_lo / sectors_per_block) > EXT4_NDIR_BLOCKS) {
469671cd2188e2f224aaeac4955785199f228235719Ken Sumrall        ret = 1;
470671cd2188e2f224aaeac4955785199f228235719Ken Sumrall    }
471671cd2188e2f224aaeac4955785199f228235719Ken Sumrall
472671cd2188e2f224aaeac4955785199f228235719Ken Sumrall    return ret;
473671cd2188e2f224aaeac4955785199f228235719Ken Sumrall}
474671cd2188e2f224aaeac4955785199f228235719Ken Sumrall
475671cd2188e2f224aaeac4955785199f228235719Ken Sumrallstatic int get_indirect_blocks(int fd, struct ext4_inode *inode,
476671cd2188e2f224aaeac4955785199f228235719Ken Sumrall                               unsigned long long *block_list, unsigned int *count)
477671cd2188e2f224aaeac4955785199f228235719Ken Sumrall{
478671cd2188e2f224aaeac4955785199f228235719Ken Sumrall    unsigned int i;
479671cd2188e2f224aaeac4955785199f228235719Ken Sumrall    unsigned int *indirect_block;
480671cd2188e2f224aaeac4955785199f228235719Ken Sumrall    unsigned int sectors_per_block;
481671cd2188e2f224aaeac4955785199f228235719Ken Sumrall
482671cd2188e2f224aaeac4955785199f228235719Ken Sumrall    sectors_per_block = info.block_size / INODE_BLOCK_SIZE;
483671cd2188e2f224aaeac4955785199f228235719Ken Sumrall
484671cd2188e2f224aaeac4955785199f228235719Ken Sumrall    indirect_block = (unsigned int *)malloc(info.block_size);
485671cd2188e2f224aaeac4955785199f228235719Ken Sumrall    if (indirect_block == 0) {
486671cd2188e2f224aaeac4955785199f228235719Ken Sumrall        critical_error("failed to allocate memory for indirect_block\n");
487671cd2188e2f224aaeac4955785199f228235719Ken Sumrall    }
488671cd2188e2f224aaeac4955785199f228235719Ken Sumrall
489671cd2188e2f224aaeac4955785199f228235719Ken Sumrall    read_block(fd, inode->i_block[EXT4_NDIR_BLOCKS], indirect_block);
490671cd2188e2f224aaeac4955785199f228235719Ken Sumrall
491671cd2188e2f224aaeac4955785199f228235719Ken Sumrall    for(i = 0; i < (inode->i_blocks_lo / sectors_per_block - EXT4_NDIR_BLOCKS); i++) {
492671cd2188e2f224aaeac4955785199f228235719Ken Sumrall       block_list[EXT4_NDIR_BLOCKS+i] = indirect_block[i];
493671cd2188e2f224aaeac4955785199f228235719Ken Sumrall    }
494671cd2188e2f224aaeac4955785199f228235719Ken Sumrall
495671cd2188e2f224aaeac4955785199f228235719Ken Sumrall    *count += i;
496671cd2188e2f224aaeac4955785199f228235719Ken Sumrall
497671cd2188e2f224aaeac4955785199f228235719Ken Sumrall    free(indirect_block);
498671cd2188e2f224aaeac4955785199f228235719Ken Sumrall
499671cd2188e2f224aaeac4955785199f228235719Ken Sumrall    return 0;
500671cd2188e2f224aaeac4955785199f228235719Ken Sumrall}
501671cd2188e2f224aaeac4955785199f228235719Ken Sumrall
502671cd2188e2f224aaeac4955785199f228235719Ken Sumrallstatic int get_block_list_indirect(int fd, struct ext4_inode *inode, unsigned long long *block_list)
503671cd2188e2f224aaeac4955785199f228235719Ken Sumrall{
504671cd2188e2f224aaeac4955785199f228235719Ken Sumrall    unsigned int count=0;
505671cd2188e2f224aaeac4955785199f228235719Ken Sumrall
506671cd2188e2f224aaeac4955785199f228235719Ken Sumrall    if (get_direct_blocks(inode, block_list, &count)) {
507671cd2188e2f224aaeac4955785199f228235719Ken Sumrall        get_indirect_blocks(fd, inode, block_list, &count);
508671cd2188e2f224aaeac4955785199f228235719Ken Sumrall    }
509671cd2188e2f224aaeac4955785199f228235719Ken Sumrall
510671cd2188e2f224aaeac4955785199f228235719Ken Sumrall    return count;
511671cd2188e2f224aaeac4955785199f228235719Ken Sumrall}
512671cd2188e2f224aaeac4955785199f228235719Ken Sumrall
513671cd2188e2f224aaeac4955785199f228235719Ken Sumrallstatic int get_extent_ents(int fd, struct ext4_extent_header *ext_hdr, unsigned long long *block_list)
514671cd2188e2f224aaeac4955785199f228235719Ken Sumrall{
515671cd2188e2f224aaeac4955785199f228235719Ken Sumrall    int i, j;
516671cd2188e2f224aaeac4955785199f228235719Ken Sumrall    struct ext4_extent *extent;
517671cd2188e2f224aaeac4955785199f228235719Ken Sumrall    off64_t fs_block_num;
518671cd2188e2f224aaeac4955785199f228235719Ken Sumrall
519671cd2188e2f224aaeac4955785199f228235719Ken Sumrall    if (ext_hdr->eh_depth != 0) {
520671cd2188e2f224aaeac4955785199f228235719Ken Sumrall        critical_error("get_extent_ents called with eh_depth != 0\n");
521671cd2188e2f224aaeac4955785199f228235719Ken Sumrall    }
522671cd2188e2f224aaeac4955785199f228235719Ken Sumrall
523671cd2188e2f224aaeac4955785199f228235719Ken Sumrall    /* The extent entries immediately follow the header, so add 1 to the pointer
524671cd2188e2f224aaeac4955785199f228235719Ken Sumrall     * and cast it to an extent pointer.
525671cd2188e2f224aaeac4955785199f228235719Ken Sumrall     */
526671cd2188e2f224aaeac4955785199f228235719Ken Sumrall    extent = (struct ext4_extent *)(ext_hdr + 1);
527671cd2188e2f224aaeac4955785199f228235719Ken Sumrall
528671cd2188e2f224aaeac4955785199f228235719Ken Sumrall    for (i = 0; i < ext_hdr->eh_entries; i++) {
529671cd2188e2f224aaeac4955785199f228235719Ken Sumrall         fs_block_num = ((off64_t)extent->ee_start_hi << 32) | extent->ee_start_lo;
530671cd2188e2f224aaeac4955785199f228235719Ken Sumrall         for (j = 0; j < extent->ee_len; j++) {
531671cd2188e2f224aaeac4955785199f228235719Ken Sumrall             block_list[extent->ee_block+j] = fs_block_num+j;
532671cd2188e2f224aaeac4955785199f228235719Ken Sumrall         }
533671cd2188e2f224aaeac4955785199f228235719Ken Sumrall         extent++;
534671cd2188e2f224aaeac4955785199f228235719Ken Sumrall    }
535671cd2188e2f224aaeac4955785199f228235719Ken Sumrall
536671cd2188e2f224aaeac4955785199f228235719Ken Sumrall    return 0;
537671cd2188e2f224aaeac4955785199f228235719Ken Sumrall}
538671cd2188e2f224aaeac4955785199f228235719Ken Sumrall
539671cd2188e2f224aaeac4955785199f228235719Ken Sumrallstatic int get_extent_idx(int fd, struct ext4_extent_header *ext_hdr, unsigned long long *block_list)
540671cd2188e2f224aaeac4955785199f228235719Ken Sumrall{
541671cd2188e2f224aaeac4955785199f228235719Ken Sumrall    int i;
542671cd2188e2f224aaeac4955785199f228235719Ken Sumrall    struct ext4_extent_idx *extent_idx;
543671cd2188e2f224aaeac4955785199f228235719Ken Sumrall    struct ext4_extent_header *tmp_ext_hdr;
544671cd2188e2f224aaeac4955785199f228235719Ken Sumrall    off64_t fs_block_num;
545671cd2188e2f224aaeac4955785199f228235719Ken Sumrall    unsigned char block[MAX_EXT4_BLOCK_SIZE];
546671cd2188e2f224aaeac4955785199f228235719Ken Sumrall
547671cd2188e2f224aaeac4955785199f228235719Ken Sumrall    /* Sanity check */
548671cd2188e2f224aaeac4955785199f228235719Ken Sumrall    if (ext_hdr->eh_depth == 0) {
549671cd2188e2f224aaeac4955785199f228235719Ken Sumrall        critical_error("get_extent_idx called with eh_depth == 0\n");
550671cd2188e2f224aaeac4955785199f228235719Ken Sumrall    }
551671cd2188e2f224aaeac4955785199f228235719Ken Sumrall
552671cd2188e2f224aaeac4955785199f228235719Ken Sumrall    /* The extent entries immediately follow the header, so add 1 to the pointer
553