2 * Copyright (C) 2010 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include "ext4_utils/ext4_utils.h"
24 #include <sys/types.h>
29 #include <arpa/inet.h>
30 #include <sys/ioctl.h>
33 #if defined(__linux__)
35 #elif defined(__APPLE__) && defined(__MACH__)
41 struct fs_aux_info aux_info;
45 /* returns 1 if a is a power of b */
46 static int is_power_of(int a, int b)
54 return (a == b) ? 1 : 0;
57 int bitmap_get_bit(u8 *bitmap, u32 bit)
59 if (bitmap[bit / 8] & (1 << (bit % 8)))
65 void bitmap_clear_bit(u8 *bitmap, u32 bit)
67 bitmap[bit / 8] &= ~(1 << (bit % 8));
72 /* Returns 1 if the bg contains a backup superblock. On filesystems with
73 the sparse_super feature, only block groups 0, 1, and powers of 3, 5,
74 and 7 have backup superblocks. Otherwise, all block groups have backup
76 int ext4_bg_has_super_block(int bg)
78 /* Without sparse_super, every block group has a superblock */
79 if (!(info.feat_ro_compat & EXT4_FEATURE_RO_COMPAT_SPARSE_SUPER))
82 if (bg == 0 || bg == 1)
85 if (is_power_of(bg, 3) || is_power_of(bg, 5) || is_power_of(bg, 7))
91 /* Function to read the primary superblock */
92 void read_sb(int fd, struct ext4_super_block *sb)
96 ret = lseek64(fd, 1024, SEEK_SET);
98 critical_error_errno("failed to seek to superblock");
100 ret = read(fd, sb, sizeof(*sb));
102 critical_error_errno("failed to read superblock");
103 if (ret != sizeof(*sb))
104 critical_error("failed to read all of superblock");
107 /* Compute the rest of the parameters of the filesystem from the basic info */
108 void ext4_create_fs_aux_info()
110 aux_info.first_data_block = (info.block_size > 1024) ? 0 : 1;
111 aux_info.len_blocks = info.len / info.block_size;
112 aux_info.inode_table_blocks = DIV_ROUND_UP(info.inodes_per_group * info.inode_size,
114 aux_info.groups = DIV_ROUND_UP(aux_info.len_blocks - aux_info.first_data_block,
115 info.blocks_per_group);
116 aux_info.blocks_per_ind = info.block_size / sizeof(u32);
117 aux_info.blocks_per_dind = aux_info.blocks_per_ind * aux_info.blocks_per_ind;
118 aux_info.blocks_per_tind = aux_info.blocks_per_dind * aux_info.blocks_per_dind;
120 aux_info.bg_desc_blocks =
121 DIV_ROUND_UP(aux_info.groups * sizeof(struct ext2_group_desc),
124 aux_info.default_i_flags = EXT4_NOATIME_FL;
126 u32 last_group_size = aux_info.len_blocks % info.blocks_per_group;
127 u32 last_header_size = 2 + aux_info.inode_table_blocks;
128 if (ext4_bg_has_super_block((int)aux_info.groups - 1))
129 last_header_size += 1 + aux_info.bg_desc_blocks +
130 info.bg_desc_reserve_blocks;
131 if (aux_info.groups <= 1 && last_group_size < last_header_size) {
132 critical_error("filesystem size too small");
134 if (last_group_size > 0 && last_group_size < last_header_size) {
136 aux_info.len_blocks -= last_group_size;
139 /* A zero-filled superblock to be written firstly to the block
140 * device to mark the file-system as invalid
142 aux_info.sb_zero = calloc(1, info.block_size);
143 if (!aux_info.sb_zero)
144 critical_error_errno("calloc");
146 /* The write_data* functions expect only block aligned calls.
147 * This is not an issue, except when we write out the super
148 * block on a system with a block size > 1K. So, we need to
149 * deal with that here.
151 aux_info.sb_block = calloc(1, info.block_size);
152 if (!aux_info.sb_block)
153 critical_error_errno("calloc");
155 if (info.block_size > 1024)
156 aux_info.sb = (struct ext4_super_block *)((char *)aux_info.sb_block + 1024);
158 aux_info.sb = aux_info.sb_block;
160 /* Alloc an array to hold the pointers to the backup superblocks */
161 aux_info.backup_sb = calloc(aux_info.groups, sizeof(char *));
164 critical_error_errno("calloc");
166 aux_info.bg_desc = calloc(info.block_size, aux_info.bg_desc_blocks);
167 if (!aux_info.bg_desc)
168 critical_error_errno("calloc");
169 aux_info.xattrs = NULL;
172 void ext4_free_fs_aux_info()
176 for (i=0; i<aux_info.groups; i++) {
177 if (aux_info.backup_sb[i])
178 free(aux_info.backup_sb[i]);
180 free(aux_info.sb_block);
181 free(aux_info.sb_zero);
182 free(aux_info.bg_desc);
185 void ext4_parse_sb_info(struct ext4_super_block *sb)
187 if (sb->s_magic != EXT4_SUPER_MAGIC)
188 error("superblock magic incorrect");
190 if ((sb->s_state & EXT4_VALID_FS) != EXT4_VALID_FS)
191 error("filesystem state not valid");
193 ext4_parse_sb(sb, &info);
195 ext4_create_fs_aux_info();
197 memcpy(aux_info.sb, sb, sizeof(*sb));
199 if (aux_info.first_data_block != sb->s_first_data_block)
200 critical_error("first data block does not match");
203 u64 get_block_device_size(int fd)
208 #if defined(__linux__)
209 ret = ioctl(fd, BLKGETSIZE64, &size);
210 #elif defined(__APPLE__) && defined(__MACH__)
211 ret = ioctl(fd, DKIOCGETBLOCKCOUNT, &size);
223 int is_block_device_fd(int fd)
229 int ret = fstat(fd, &st);
233 return S_ISBLK(st.st_mode);
237 u64 get_file_size(int fd)
244 ret = fstat(fd, &buf);
249 reserve_len = -info.len;
251 if (S_ISREG(buf.st_mode))
252 computed_size = buf.st_size - reserve_len;
253 else if (S_ISBLK(buf.st_mode))
254 computed_size = get_block_device_size(fd) - reserve_len;
258 if (computed_size < 0) {
259 warn("Computed filesystem size less than 0");
263 return computed_size;
266 int read_ext(int fd, int verbose)
269 struct ext4_super_block sb;
273 ext4_parse_sb_info(&sb);
275 ret = lseek64(fd, info.len, SEEK_SET);
277 critical_error_errno("failed to seek to end of input image");
279 ret = lseek64(fd, info.block_size * (aux_info.first_data_block + 1), SEEK_SET);
281 critical_error_errno("failed to seek to block group descriptors");
283 ret = read(fd, aux_info.bg_desc, info.block_size * aux_info.bg_desc_blocks);
285 critical_error_errno("failed to read block group descriptors");
286 if (ret != (int)info.block_size * (int)aux_info.bg_desc_blocks)
287 critical_error("failed to read all of block group descriptors");
290 printf("Found filesystem with parameters:\n");
291 printf(" Size: %"PRIu64"\n", info.len);
292 printf(" Block size: %d\n", info.block_size);
293 printf(" Blocks per group: %d\n", info.blocks_per_group);
294 printf(" Inodes per group: %d\n", info.inodes_per_group);
295 printf(" Inode size: %d\n", info.inode_size);
296 printf(" Label: %s\n", info.label);
297 printf(" Blocks: %"PRIext4u64"\n", aux_info.len_blocks);
298 printf(" Block groups: %d\n", aux_info.groups);
299 printf(" Reserved block group size: %d\n", info.bg_desc_reserve_blocks);
300 printf(" Used %d/%d inodes and %d/%d blocks\n",
301 aux_info.sb->s_inodes_count - aux_info.sb->s_free_inodes_count,
302 aux_info.sb->s_inodes_count,
303 aux_info.sb->s_blocks_count_lo - aux_info.sb->s_free_blocks_count_lo,
304 aux_info.sb->s_blocks_count_lo);