ef77b72fe989c406cac521d15506bb0ddbb377a0
[project/make_ext4fs.git] / ext4_utils.c
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.h"
18 #include "uuid.h"
19 #include "allocate.h"
20 #include "indirect.h"
21 #include "extent.h"
22
23 #include <sparse/sparse.h>
24
25 #include <fcntl.h>
26 #include <inttypes.h>
27 #include <sys/stat.h>
28 #include <sys/types.h>
29 #include <stddef.h>
30 #include <string.h>
31
32 #include <arpa/inet.h>
33 #include <sys/ioctl.h>
34
35 #if defined(__linux__)
36 #include <linux/fs.h>
37 #elif defined(__APPLE__) && defined(__MACH__)
38 #include <sys/disk.h>
39 #endif
40
41 int force = 0;
42 struct fs_info info;
43 struct fs_aux_info aux_info;
44 struct sparse_file *ext4_sparse_file;
45
46 jmp_buf setjmp_env;
47
48 /* returns 1 if a is a power of b */
49 static int is_power_of(int a, int b)
50 {
51         while (a > b) {
52                 if (a % b)
53                         return 0;
54                 a /= b;
55         }
56
57         return (a == b) ? 1 : 0;
58 }
59
60 int bitmap_get_bit(u8 *bitmap, u32 bit)
61 {
62         if (bitmap[bit / 8] & (1 << (bit % 8)))
63                 return 1;
64
65         return 0;
66 }
67
68 void bitmap_clear_bit(u8 *bitmap, u32 bit)
69 {
70         bitmap[bit / 8] &= ~(1 << (bit % 8));
71
72         return;
73 }
74
75 /* Returns 1 if the bg contains a backup superblock.  On filesystems with
76    the sparse_super feature, only block groups 0, 1, and powers of 3, 5,
77    and 7 have backup superblocks.  Otherwise, all block groups have backup
78    superblocks */
79 int ext4_bg_has_super_block(int bg)
80 {
81         /* Without sparse_super, every block group has a superblock */
82         if (!(info.feat_ro_compat & EXT4_FEATURE_RO_COMPAT_SPARSE_SUPER))
83                 return 1;
84
85         if (bg == 0 || bg == 1)
86                 return 1;
87
88         if (is_power_of(bg, 3) || is_power_of(bg, 5) || is_power_of(bg, 7))
89                 return 1;
90
91         return 0;
92 }
93
94 /* Function to read the primary superblock */
95 void read_sb(int fd, struct ext4_super_block *sb)
96 {
97         off64_t ret;
98
99         ret = lseek64(fd, 1024, SEEK_SET);
100         if (ret < 0)
101                 critical_error_errno("failed to seek to superblock");
102
103         ret = read(fd, sb, sizeof(*sb));
104         if (ret < 0)
105                 critical_error_errno("failed to read superblock");
106         if (ret != sizeof(*sb))
107                 critical_error("failed to read all of superblock");
108 }
109
110 /* Function to write a primary or backup superblock at a given offset */
111 void write_sb(int fd, unsigned long long offset, struct ext4_super_block *sb)
112 {
113         off64_t ret;
114
115         ret = lseek64(fd, offset, SEEK_SET);
116         if (ret < 0)
117                 critical_error_errno("failed to seek to superblock");
118
119         ret = write(fd, sb, sizeof(*sb));
120         if (ret < 0)
121                 critical_error_errno("failed to write superblock");
122         if (ret != sizeof(*sb))
123                 critical_error("failed to write all of superblock");
124 }
125
126 /* Write the filesystem image to a file */
127 void write_ext4_image(int fd, int gz, int sparse, int crc)
128 {
129         sparse_file_write(ext4_sparse_file, fd, gz, sparse, crc);
130 }
131
132 /* Compute the rest of the parameters of the filesystem from the basic info */
133 void ext4_create_fs_aux_info()
134 {
135         aux_info.first_data_block = (info.block_size > 1024) ? 0 : 1;
136         aux_info.len_blocks = info.len / info.block_size;
137         aux_info.inode_table_blocks = DIV_ROUND_UP(info.inodes_per_group * info.inode_size,
138                 info.block_size);
139         aux_info.groups = DIV_ROUND_UP(aux_info.len_blocks - aux_info.first_data_block,
140                 info.blocks_per_group);
141         aux_info.blocks_per_ind = info.block_size / sizeof(u32);
142         aux_info.blocks_per_dind = aux_info.blocks_per_ind * aux_info.blocks_per_ind;
143         aux_info.blocks_per_tind = aux_info.blocks_per_dind * aux_info.blocks_per_dind;
144
145         aux_info.bg_desc_blocks =
146                 DIV_ROUND_UP(aux_info.groups * sizeof(struct ext2_group_desc),
147                         info.block_size);
148
149         aux_info.default_i_flags = EXT4_NOATIME_FL;
150
151         u32 last_group_size = aux_info.len_blocks % info.blocks_per_group;
152         u32 last_header_size = 2 + aux_info.inode_table_blocks;
153         if (ext4_bg_has_super_block(aux_info.groups - 1))
154                 last_header_size += 1 + aux_info.bg_desc_blocks +
155                         info.bg_desc_reserve_blocks;
156         if (last_group_size > 0 && last_group_size < last_header_size) {
157                 aux_info.groups--;
158                 aux_info.len_blocks -= last_group_size;
159         }
160
161         aux_info.sb = calloc(info.block_size, 1);
162         /* Alloc an array to hold the pointers to the backup superblocks */
163         aux_info.backup_sb = calloc(aux_info.groups, sizeof(char *));
164
165         if (!aux_info.sb)
166                 critical_error_errno("calloc");
167
168         aux_info.bg_desc = calloc(info.block_size, aux_info.bg_desc_blocks);
169         if (!aux_info.bg_desc)
170                 critical_error_errno("calloc");
171         aux_info.xattrs = NULL;
172 }
173
174 void ext4_free_fs_aux_info()
175 {
176         unsigned int i;
177
178         for (i=0; i<aux_info.groups; i++) {
179                 if (aux_info.backup_sb[i])
180                         free(aux_info.backup_sb[i]);
181         }
182         free(aux_info.sb);
183         free(aux_info.bg_desc);
184 }
185
186 /* Fill in the superblock memory buffer based on the filesystem parameters */
187 void ext4_fill_in_sb()
188 {
189         unsigned int i;
190         struct ext4_super_block *sb = aux_info.sb;
191
192         sb->s_inodes_count = info.inodes_per_group * aux_info.groups;
193         sb->s_blocks_count_lo = aux_info.len_blocks;
194         sb->s_r_blocks_count_lo = 0;
195         sb->s_free_blocks_count_lo = 0;
196         sb->s_free_inodes_count = 0;
197         sb->s_first_data_block = aux_info.first_data_block;
198         sb->s_log_block_size = log_2(info.block_size / 1024);
199         sb->s_obso_log_frag_size = log_2(info.block_size / 1024);
200         sb->s_blocks_per_group = info.blocks_per_group;
201         sb->s_obso_frags_per_group = info.blocks_per_group;
202         sb->s_inodes_per_group = info.inodes_per_group;
203         sb->s_mtime = 0;
204         sb->s_wtime = 0;
205         sb->s_mnt_count = 0;
206         sb->s_max_mnt_count = 0xFFFF;
207         sb->s_magic = EXT4_SUPER_MAGIC;
208         sb->s_state = EXT4_VALID_FS;
209         sb->s_errors = EXT4_ERRORS_RO;
210         sb->s_minor_rev_level = 0;
211         sb->s_lastcheck = 0;
212         sb->s_checkinterval = 0;
213         sb->s_creator_os = EXT4_OS_LINUX;
214         sb->s_rev_level = EXT4_DYNAMIC_REV;
215         sb->s_def_resuid = EXT4_DEF_RESUID;
216         sb->s_def_resgid = EXT4_DEF_RESGID;
217
218         sb->s_first_ino = EXT4_GOOD_OLD_FIRST_INO;
219         sb->s_inode_size = info.inode_size;
220         sb->s_block_group_nr = 0;
221         sb->s_feature_compat = info.feat_compat;
222         sb->s_feature_incompat = info.feat_incompat;
223         sb->s_feature_ro_compat = info.feat_ro_compat;
224         generate_uuid("extandroid/make_ext4fs", info.label, sb->s_uuid);
225         memset(sb->s_volume_name, 0, sizeof(sb->s_volume_name));
226         strncpy(sb->s_volume_name, info.label, sizeof(sb->s_volume_name));
227         memset(sb->s_last_mounted, 0, sizeof(sb->s_last_mounted));
228         sb->s_algorithm_usage_bitmap = 0;
229
230         sb->s_reserved_gdt_blocks = info.bg_desc_reserve_blocks;
231         sb->s_prealloc_blocks = 0;
232         sb->s_prealloc_dir_blocks = 0;
233
234         //memcpy(sb->s_journal_uuid, sb->s_uuid, sizeof(sb->s_journal_uuid));
235         if (info.feat_compat & EXT4_FEATURE_COMPAT_HAS_JOURNAL)
236                 sb->s_journal_inum = EXT4_JOURNAL_INO;
237         sb->s_journal_dev = 0;
238         sb->s_last_orphan = 0;
239         sb->s_hash_seed[0] = 0; /* FIXME */
240         sb->s_def_hash_version = DX_HASH_TEA;
241         sb->s_reserved_char_pad = EXT4_JNL_BACKUP_BLOCKS;
242         sb->s_desc_size = sizeof(struct ext2_group_desc);
243         sb->s_default_mount_opts = 0; /* FIXME */
244         sb->s_first_meta_bg = 0;
245         sb->s_mkfs_time = 0;
246         //sb->s_jnl_blocks[17]; /* FIXME */
247
248         sb->s_blocks_count_hi = aux_info.len_blocks >> 32;
249         sb->s_r_blocks_count_hi = 0;
250         sb->s_free_blocks_count_hi = 0;
251         sb->s_min_extra_isize = sizeof(struct ext4_inode) -
252                 EXT4_GOOD_OLD_INODE_SIZE;
253         sb->s_want_extra_isize = sizeof(struct ext4_inode) -
254                 EXT4_GOOD_OLD_INODE_SIZE;
255         sb->s_flags = 2;
256         sb->s_raid_stride = 0;
257         sb->s_mmp_interval = 0;
258         sb->s_mmp_block = 0;
259         sb->s_raid_stripe_width = 0;
260         sb->s_log_groups_per_flex = 0;
261         sb->s_kbytes_written = 0;
262
263         for (i = 0; i < aux_info.groups; i++) {
264                 u64 group_start_block = aux_info.first_data_block + i *
265                         info.blocks_per_group;
266                 u32 header_size = 0;
267                 if (ext4_bg_has_super_block(i)) {
268                         if (i != 0) {
269                                 aux_info.backup_sb[i] = calloc(info.block_size, 1);
270                                 memcpy(aux_info.backup_sb[i], sb, info.block_size);
271                                 /* Update the block group nr of this backup superblock */
272                                 aux_info.backup_sb[i]->s_block_group_nr = i;
273                                 sparse_file_add_data(ext4_sparse_file, aux_info.backup_sb[i],
274                                                 info.block_size, group_start_block);
275                         }
276                         sparse_file_add_data(ext4_sparse_file, aux_info.bg_desc,
277                                 aux_info.bg_desc_blocks * info.block_size,
278                                 group_start_block + 1);
279                         header_size = 1 + aux_info.bg_desc_blocks + info.bg_desc_reserve_blocks;
280                 }
281
282                 aux_info.bg_desc[i].bg_block_bitmap = group_start_block + header_size;
283                 aux_info.bg_desc[i].bg_inode_bitmap = group_start_block + header_size + 1;
284                 aux_info.bg_desc[i].bg_inode_table = group_start_block + header_size + 2;
285
286                 aux_info.bg_desc[i].bg_free_blocks_count = sb->s_blocks_per_group;
287                 aux_info.bg_desc[i].bg_free_inodes_count = sb->s_inodes_per_group;
288                 aux_info.bg_desc[i].bg_used_dirs_count = 0;
289         }
290 }
291
292 void ext4_queue_sb(void)
293 {
294         /* The write_data* functions expect only block aligned calls.
295          * This is not an issue, except when we write out the super
296          * block on a system with a block size > 1K.  So, we need to
297          * deal with that here.
298          */
299         if (info.block_size > 1024) {
300                 u8 *buf = calloc(info.block_size, 1);
301                 memcpy(buf + 1024, (u8*)aux_info.sb, 1024);
302                 sparse_file_add_data(ext4_sparse_file, buf, info.block_size, 0);
303         } else {
304                 sparse_file_add_data(ext4_sparse_file, aux_info.sb, 1024, 1);
305         }
306 }
307
308 void ext4_parse_sb_info(struct ext4_super_block *sb)
309 {
310         if (sb->s_magic != EXT4_SUPER_MAGIC)
311                 error("superblock magic incorrect");
312
313         if ((sb->s_state & EXT4_VALID_FS) != EXT4_VALID_FS)
314                 error("filesystem state not valid");
315
316         ext4_parse_sb(sb, &info);
317
318         ext4_create_fs_aux_info();
319
320         memcpy(aux_info.sb, sb, sizeof(*sb));
321
322         if (aux_info.first_data_block != sb->s_first_data_block)
323                 critical_error("first data block does not match");
324 }
325
326 void ext4_create_resize_inode()
327 {
328         struct block_allocation *reserve_inode_alloc = create_allocation();
329         u32 reserve_inode_len = 0;
330         unsigned int i;
331
332         struct ext4_inode *inode = get_inode(EXT4_RESIZE_INO);
333         if (inode == NULL) {
334                 error("failed to get resize inode");
335                 return;
336         }
337
338         for (i = 0; i < aux_info.groups; i++) {
339                 if (ext4_bg_has_super_block(i)) {
340                         u64 group_start_block = aux_info.first_data_block + i *
341                                 info.blocks_per_group;
342                         u32 reserved_block_start = group_start_block + 1 +
343                                 aux_info.bg_desc_blocks;
344                         u32 reserved_block_len = info.bg_desc_reserve_blocks;
345                         append_region(reserve_inode_alloc, reserved_block_start,
346                                 reserved_block_len, i);
347                         reserve_inode_len += reserved_block_len;
348                 }
349         }
350
351         inode_attach_resize(inode, reserve_inode_alloc);
352
353         inode->i_mode = S_IFREG | S_IRUSR | S_IWUSR;
354         inode->i_links_count = 1;
355
356         free_alloc(reserve_inode_alloc);
357 }
358
359 /* Allocate the blocks to hold a journal inode and connect them to the
360    reserved journal inode */
361 void ext4_create_journal_inode()
362 {
363         struct ext4_inode *inode = get_inode(EXT4_JOURNAL_INO);
364         if (inode == NULL) {
365                 error("failed to get journal inode");
366                 return;
367         }
368
369         u8 *journal_data = inode_allocate_data_extents(inode,
370                         info.journal_blocks * info.block_size,
371                         info.journal_blocks * info.block_size);
372         if (!journal_data) {
373                 error("failed to allocate extents for journal data");
374                 return;
375         }
376
377         inode->i_mode = S_IFREG | S_IRUSR | S_IWUSR;
378         inode->i_links_count = 1;
379
380         journal_superblock_t *jsb = (journal_superblock_t *)journal_data;
381         jsb->s_header.h_magic = htonl(JBD2_MAGIC_NUMBER);
382         jsb->s_header.h_blocktype = htonl(JBD2_SUPERBLOCK_V2);
383         jsb->s_blocksize = htonl(info.block_size);
384         jsb->s_maxlen = htonl(info.journal_blocks);
385         jsb->s_nr_users = htonl(1);
386         jsb->s_first = htonl(1);
387         jsb->s_sequence = htonl(1);
388
389         memcpy(aux_info.sb->s_jnl_blocks, &inode->i_block, sizeof(inode->i_block));
390 }
391
392 /* Update the number of free blocks and inodes in the filesystem and in each
393    block group */
394 void ext4_update_free()
395 {
396         u32 i;
397
398         for (i = 0; i < aux_info.groups; i++) {
399                 u32 bg_free_blocks = get_free_blocks(i);
400                 u32 bg_free_inodes = get_free_inodes(i);
401                 u16 crc;
402
403                 aux_info.bg_desc[i].bg_free_blocks_count = bg_free_blocks;
404                 aux_info.sb->s_free_blocks_count_lo += bg_free_blocks;
405
406                 aux_info.bg_desc[i].bg_free_inodes_count = bg_free_inodes;
407                 aux_info.sb->s_free_inodes_count += bg_free_inodes;
408
409                 aux_info.bg_desc[i].bg_used_dirs_count += get_directories(i);
410
411                 aux_info.bg_desc[i].bg_flags = get_bg_flags(i);
412
413                 crc = ext4_crc16(~0, aux_info.sb->s_uuid, sizeof(aux_info.sb->s_uuid));
414                 crc = ext4_crc16(crc, &i, sizeof(i));
415                 crc = ext4_crc16(crc, &aux_info.bg_desc[i], offsetof(struct ext2_group_desc, bg_checksum));
416                 aux_info.bg_desc[i].bg_checksum = crc;
417         }
418 }
419
420 u64 get_block_device_size(int fd)
421 {
422         u64 size = 0;
423         int ret;
424
425 #if defined(__linux__)
426         ret = ioctl(fd, BLKGETSIZE64, &size);
427 #elif defined(__APPLE__) && defined(__MACH__)
428         ret = ioctl(fd, DKIOCGETBLOCKCOUNT, &size);
429 #else
430         close(fd);
431         return 0;
432 #endif
433
434         if (ret)
435                 return 0;
436
437         return size;
438 }
439
440 int is_block_device_fd(int fd)
441 {
442         struct stat st;
443         int ret = fstat(fd, &st);
444         if (ret < 0)
445                 return 0;
446
447         return S_ISBLK(st.st_mode);
448 }
449
450 u64 get_file_size(int fd)
451 {
452         struct stat buf;
453         int ret;
454         u64 reserve_len = 0;
455         s64 computed_size;
456
457         ret = fstat(fd, &buf);
458         if (ret)
459                 return 0;
460
461         if (info.len < 0)
462                 reserve_len = -info.len;
463
464         if (S_ISREG(buf.st_mode))
465                 computed_size = buf.st_size - reserve_len;
466         else if (S_ISBLK(buf.st_mode))
467                 computed_size = get_block_device_size(fd) - reserve_len;
468         else
469                 computed_size = 0;
470
471         if (computed_size < 0) {
472                 warn("Computed filesystem size less than 0");
473                 computed_size = 0;
474         }
475
476         return computed_size;
477 }
478
479 u64 parse_num(const char *arg)
480 {
481         char *endptr;
482         u64 num = strtoull(arg, &endptr, 10);
483         if (*endptr == 'k' || *endptr == 'K')
484                 num *= 1024LL;
485         else if (*endptr == 'm' || *endptr == 'M')
486                 num *= 1024LL * 1024LL;
487         else if (*endptr == 'g' || *endptr == 'G')
488                 num *= 1024LL * 1024LL * 1024LL;
489
490         return num;
491 }
492
493 int read_ext(int fd, int verbose)
494 {
495         off64_t ret;
496         struct ext4_super_block sb;
497
498         read_sb(fd, &sb);
499
500         ext4_parse_sb_info(&sb);
501
502         ret = lseek64(fd, info.len, SEEK_SET);
503         if (ret < 0)
504                 critical_error_errno("failed to seek to end of input image");
505
506         ret = lseek64(fd, info.block_size * (aux_info.first_data_block + 1), SEEK_SET);
507         if (ret < 0)
508                 critical_error_errno("failed to seek to block group descriptors");
509
510         ret = read(fd, aux_info.bg_desc, info.block_size * aux_info.bg_desc_blocks);
511         if (ret < 0)
512                 critical_error_errno("failed to read block group descriptors");
513         if (ret != (int)info.block_size * (int)aux_info.bg_desc_blocks)
514                 critical_error("failed to read all of block group descriptors");
515
516         if (verbose) {
517                 printf("Found filesystem with parameters:\n");
518                 printf("    Size: %"PRIu64"\n", info.len);
519                 printf("    Block size: %d\n", info.block_size);
520                 printf("    Blocks per group: %d\n", info.blocks_per_group);
521                 printf("    Inodes per group: %d\n", info.inodes_per_group);
522                 printf("    Inode size: %d\n", info.inode_size);
523                 printf("    Label: %s\n", info.label);
524                 printf("    Blocks: %"PRIu64"\n", aux_info.len_blocks);
525                 printf("    Block groups: %d\n", aux_info.groups);
526                 printf("    Reserved block group size: %d\n", info.bg_desc_reserve_blocks);
527                 printf("    Used %d/%d inodes and %d/%d blocks\n",
528                         aux_info.sb->s_inodes_count - aux_info.sb->s_free_inodes_count,
529                         aux_info.sb->s_inodes_count,
530                         aux_info.sb->s_blocks_count_lo - aux_info.sb->s_free_blocks_count_lo,
531                         aux_info.sb->s_blocks_count_lo);
532         }
533
534         return 0;
535 }
536