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