• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
17 #include "ext4_utils/ext4_utils.h"
18 
19 #include <fcntl.h>
20 #include <inttypes.h>
21 #include <stddef.h>
22 #include <string.h>
23 #include <sys/stat.h>
24 #include <sys/types.h>
25 
26 #ifdef _WIN32
27 #include <winsock2.h>
28 #else
29 #include <arpa/inet.h>
30 #include <sys/ioctl.h>
31 #endif
32 
33 #if defined(__linux__)
34 #include <linux/fs.h>
35 #elif defined(__APPLE__) && defined(__MACH__)
36 #include <sys/disk.h>
37 #endif
38 
39 #include "helpers.h"
40 
41 int force = 0;
42 struct fs_info info;
43 struct fs_aux_info aux_info;
44 
45 jmp_buf setjmp_env;
46 
47 /* returns 1 if a is a power of b */
is_power_of(int a,int b)48 static int is_power_of(int a, int b) {
49     while (a > b) {
50         if (a % b) return 0;
51         a /= b;
52     }
53 
54     return (a == b) ? 1 : 0;
55 }
56 
bitmap_get_bit(u8 * bitmap,u32 bit)57 int bitmap_get_bit(u8* bitmap, u32 bit) {
58     if (bitmap[bit / 8] & (1 << (bit % 8))) return 1;
59 
60     return 0;
61 }
62 
bitmap_clear_bit(u8 * bitmap,u32 bit)63 void bitmap_clear_bit(u8* bitmap, u32 bit) {
64     bitmap[bit / 8] &= ~(1 << (bit % 8));
65 
66     return;
67 }
68 
69 /* Returns 1 if the bg contains a backup superblock.  On filesystems with
70    the sparse_super feature, only block groups 0, 1, and powers of 3, 5,
71    and 7 have backup superblocks.  Otherwise, all block groups have backup
72    superblocks */
ext4_bg_has_super_block(int bg)73 int ext4_bg_has_super_block(int bg) {
74     /* Without sparse_super, every block group has a superblock */
75     if (!(info.feat_ro_compat & EXT4_FEATURE_RO_COMPAT_SPARSE_SUPER)) return 1;
76 
77     if (bg == 0 || bg == 1) return 1;
78 
79     if (is_power_of(bg, 3) || is_power_of(bg, 5) || is_power_of(bg, 7)) return 1;
80 
81     return 0;
82 }
83 
84 /* Function to read the primary superblock */
read_sb(int fd,struct ext4_super_block * sb)85 void read_sb(int fd, struct ext4_super_block* sb) {
86     if (lseek(fd, 1024, SEEK_SET) < 0) critical_error_errno("failed to seek to superblock");
87 
88     ssize_t ret = read(fd, sb, sizeof(*sb));
89     if (ret < 0) critical_error_errno("failed to read superblock");
90     if (ret != sizeof(*sb)) critical_error("failed to read all of superblock");
91 }
92 
93 /* Compute the rest of the parameters of the filesystem from the basic info */
ext4_create_fs_aux_info()94 void ext4_create_fs_aux_info() {
95     aux_info.first_data_block = (info.block_size > 1024) ? 0 : 1;
96     aux_info.len_blocks = info.len / info.block_size;
97     aux_info.inode_table_blocks =
98             DIV_ROUND_UP(info.inodes_per_group * info.inode_size, info.block_size);
99     aux_info.groups =
100             DIV_ROUND_UP(aux_info.len_blocks - aux_info.first_data_block, info.blocks_per_group);
101     aux_info.blocks_per_ind = info.block_size / sizeof(u32);
102     aux_info.blocks_per_dind = aux_info.blocks_per_ind * aux_info.blocks_per_ind;
103     aux_info.blocks_per_tind = aux_info.blocks_per_dind * aux_info.blocks_per_dind;
104 
105     aux_info.bg_desc_blocks =
106             DIV_ROUND_UP(aux_info.groups * (size_t)info.bg_desc_size, info.block_size);
107 
108     aux_info.default_i_flags = EXT4_NOATIME_FL;
109 
110     u32 last_group_size = aux_info.len_blocks == info.blocks_per_group
111                                   ? aux_info.len_blocks
112                                   : aux_info.len_blocks % info.blocks_per_group;
113     u32 last_header_size = 2 + aux_info.inode_table_blocks;
114     if (ext4_bg_has_super_block((int)aux_info.groups - 1))
115         last_header_size += 1 + aux_info.bg_desc_blocks + info.bg_desc_reserve_blocks;
116     if (aux_info.groups <= 1 && last_group_size < last_header_size) {
117         critical_error("filesystem size too small");
118     }
119     if (last_group_size > 0 && last_group_size < last_header_size) {
120         aux_info.groups--;
121         aux_info.len_blocks -= last_group_size;
122     }
123 
124     /* A zero-filled superblock to be written firstly to the block
125      * device to mark the file-system as invalid
126      */
127     aux_info.sb_zero = (struct ext4_super_block*)calloc(1, info.block_size);
128     if (!aux_info.sb_zero) critical_error_errno("calloc");
129 
130     /* The write_data* functions expect only block aligned calls.
131      * This is not an issue, except when we write out the super
132      * block on a system with a block size > 1K.  So, we need to
133      * deal with that here.
134      */
135     aux_info.sb_block = (struct ext4_super_block*)calloc(1, info.block_size);
136     if (!aux_info.sb_block) critical_error_errno("calloc");
137 
138     if (info.block_size > 1024)
139         aux_info.sb = (struct ext4_super_block*)((char*)aux_info.sb_block + 1024);
140     else
141         aux_info.sb = aux_info.sb_block;
142 
143     /* Alloc an array to hold the pointers to the backup superblocks */
144     aux_info.backup_sb =
145             (struct ext4_super_block**)calloc(aux_info.groups, sizeof(struct ext4_super_block*));
146 
147     if (!aux_info.sb) critical_error_errno("calloc");
148 
149     aux_info.bg_desc =
150             (struct ext2_group_desc*)calloc(aux_info.groups, sizeof(struct ext2_group_desc));
151     if (!aux_info.bg_desc) critical_error_errno("calloc");
152     aux_info.xattrs = NULL;
153 }
154 
ext4_free_fs_aux_info()155 void ext4_free_fs_aux_info() {
156     unsigned int i;
157 
158     for (i = 0; i < aux_info.groups; i++) {
159         if (aux_info.backup_sb[i]) free(aux_info.backup_sb[i]);
160     }
161     free(aux_info.sb_block);
162     free(aux_info.sb_zero);
163     free(aux_info.bg_desc);
164 }
165 
ext4_parse_sb_info(struct ext4_super_block * sb)166 void ext4_parse_sb_info(struct ext4_super_block* sb) {
167     if (sb->s_magic != EXT4_SUPER_MAGIC) error("superblock magic incorrect");
168 
169     if ((sb->s_state & EXT4_VALID_FS) != EXT4_VALID_FS) error("filesystem state not valid");
170 
171     ext4_parse_sb(sb, &info);
172 
173     ext4_create_fs_aux_info();
174 
175     memcpy(aux_info.sb, sb, sizeof(*sb));
176 
177     if (aux_info.first_data_block != sb->s_first_data_block)
178         critical_error("first data block does not match");
179 }
180 
get_block_device_size(int fd)181 u64 get_block_device_size(int fd) {
182     u64 size = 0;
183     int ret;
184 
185 #if defined(__linux__)
186     ret = ioctl(fd, BLKGETSIZE64, &size);
187 #elif defined(__APPLE__) && defined(__MACH__)
188     ret = ioctl(fd, DKIOCGETBLOCKCOUNT, &size);
189 #else
190     close(fd);
191     return 0;
192 #endif
193 
194     if (ret) return 0;
195 
196     return size;
197 }
198 
is_block_device_fd(int fd)199 int is_block_device_fd(int fd __attribute__((unused))) {
200 #ifdef _WIN32
201     return 0;
202 #else
203     struct stat st;
204     int ret = fstat(fd, &st);
205     if (ret < 0) return 0;
206 
207     return S_ISBLK(st.st_mode);
208 #endif
209 }
210 
get_file_size(int fd)211 u64 get_file_size(int fd) {
212     struct stat buf;
213     int ret;
214     u64 reserve_len = 0;
215     s64 computed_size;
216 
217     ret = fstat(fd, &buf);
218     if (ret) return 0;
219 
220     if (info.len < 0) reserve_len = -info.len;
221 
222     if (S_ISREG(buf.st_mode))
223         computed_size = buf.st_size - reserve_len;
224     else if (S_ISBLK(buf.st_mode))
225         computed_size = get_block_device_size(fd) - reserve_len;
226     else
227         computed_size = 0;
228 
229     if (computed_size < 0) {
230         warn("Computed filesystem size less than 0");
231         computed_size = 0;
232     }
233 
234     return computed_size;
235 }
236 
read_block_group_descriptors(int fd)237 static void read_block_group_descriptors(int fd) {
238     size_t size = info.block_size * (size_t)aux_info.bg_desc_blocks;
239     void* buf = malloc(size);
240     ssize_t ret;
241 
242     if (!buf) critical_error("failed to alloc buffer");
243 
244     ret = read(fd, buf, size);
245     if (ret < 0) {
246         free(buf);
247         critical_error_errno("failed to read block group descriptors");
248     }
249     if (ret != size) {
250         free(buf);
251         critical_error("failed to read all the block group descriptors");
252     }
253     const struct ext4_group_desc* gdp = (const struct ext4_group_desc*)buf;
254     bool extended = (info.bg_desc_size >= EXT4_MIN_DESC_SIZE_64BIT);
255     for (size_t i = 0; i < aux_info.groups; i++) {
256         aux_info.bg_desc[i].bg_block_bitmap =
257                 (extended ? (u64)gdp->bg_block_bitmap_hi << 32 : 0) | gdp->bg_block_bitmap_lo;
258         aux_info.bg_desc[i].bg_inode_bitmap =
259                 (extended ? (u64)gdp->bg_inode_bitmap_hi << 32 : 0) | gdp->bg_inode_bitmap_lo;
260         aux_info.bg_desc[i].bg_inode_table =
261                 (extended ? (u64)gdp->bg_inode_table_hi << 32 : 0) | gdp->bg_inode_table_lo;
262         aux_info.bg_desc[i].bg_free_blocks_count =
263                 (extended ? (u32)gdp->bg_free_blocks_count_hi << 16 : 0) |
264                 gdp->bg_free_blocks_count_lo;
265         aux_info.bg_desc[i].bg_free_inodes_count =
266                 (extended ? (u32)gdp->bg_free_inodes_count_hi << 16 : 0) |
267                 gdp->bg_free_inodes_count_lo;
268         aux_info.bg_desc[i].bg_used_dirs_count =
269                 (extended ? (u32)gdp->bg_used_dirs_count_hi << 16 : 0) | gdp->bg_used_dirs_count_lo;
270         aux_info.bg_desc[i].bg_flags = gdp->bg_flags;
271         gdp = (const struct ext4_group_desc*)((u8*)gdp + info.bg_desc_size);
272     }
273     free(buf);
274 }
275 
read_ext(int fd,int verbose)276 int read_ext(int fd, int verbose) {
277     off_t ret;
278     struct ext4_super_block sb;
279 
280     read_sb(fd, &sb);
281 
282     ext4_parse_sb_info(&sb);
283 
284     ret = lseek(fd, info.len, SEEK_SET);
285     if (ret < 0) critical_error_errno("failed to seek to end of input image");
286 
287     ret = lseek(fd, info.block_size * (aux_info.first_data_block + 1), SEEK_SET);
288     if (ret < 0) critical_error_errno("failed to seek to block group descriptors");
289 
290     read_block_group_descriptors(fd);
291 
292     if (verbose) {
293         printf("Found filesystem with parameters:\n");
294         printf("    Size: %" PRIu64 "\n", info.len);
295         printf("    Block size: %d\n", info.block_size);
296         printf("    Blocks per group: %d\n", info.blocks_per_group);
297         printf("    Inodes per group: %d\n", info.inodes_per_group);
298         printf("    Inode size: %d\n", info.inode_size);
299         printf("    Label: %s\n", info.label);
300         printf("    Blocks: %" PRIext4u64 "\n", aux_info.len_blocks);
301         printf("    Block groups: %d\n", aux_info.groups);
302         printf("    Reserved block group size: %d\n", info.bg_desc_reserve_blocks);
303         printf("    Block group descriptor size: %d\n", info.bg_desc_size);
304         printf("    Used %d/%d inodes and %d/%d blocks\n",
305                aux_info.sb->s_inodes_count - aux_info.sb->s_free_inodes_count,
306                aux_info.sb->s_inodes_count,
307                aux_info.sb->s_blocks_count_lo - aux_info.sb->s_free_blocks_count_lo,
308                aux_info.sb->s_blocks_count_lo);
309     }
310 
311     return 0;
312 }
313