Colin Cross | ec0a2e8 | 2010-06-11 14:21:37 -0700 | [diff] [blame] | 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 | #ifndef _EXT4_UTILS_H_ |
| 18 | #define _EXT4_UTILS_H_ |
| 19 | |
| 20 | #include <sys/types.h> |
| 21 | #include <errno.h> |
| 22 | #include <stdarg.h> |
| 23 | #include <stdio.h> |
| 24 | #include <stdlib.h> |
| 25 | #include <string.h> |
| 26 | |
| 27 | extern int force; |
| 28 | |
| 29 | #define warn(fmt, args...) do { fprintf(stderr, "warning: %s: " fmt "\n", __func__, ## args); } while (0) |
| 30 | #define error(fmt, args...) do { fprintf(stderr, "error: %s: " fmt "\n", __func__, ## args); if (!force) exit(EXIT_FAILURE); } while (0) |
| 31 | #define error_errno(s) error(s ": %s", strerror(errno)) |
| 32 | #define critical_error(fmt, args...) do { fprintf(stderr, "critical error: %s: " fmt "\n", __func__, ## args); exit(EXIT_FAILURE); } while (0) |
| 33 | #define critical_error_errno(s) critical_error(s ": %s", strerror(errno)) |
| 34 | |
| 35 | #define EXT4_SUPER_MAGIC 0xEF53 |
| 36 | #define EXT4_JNL_BACKUP_BLOCKS 1 |
| 37 | |
| 38 | #define min(a, b) ((a) < (b) ? (a) : (b)) |
| 39 | |
| 40 | #define DIV_ROUND_UP(x, y) (((x) + (y) - 1)/(y)) |
Colin Cross | 6bd2b5d | 2010-08-05 11:56:38 -0700 | [diff] [blame] | 41 | #define ALIGN(x, y) ((y) * DIV_ROUND_UP((x), (y))) |
Colin Cross | ec0a2e8 | 2010-06-11 14:21:37 -0700 | [diff] [blame] | 42 | |
| 43 | #define __le64 u64 |
| 44 | #define __le32 u32 |
| 45 | #define __le16 u16 |
| 46 | |
| 47 | #define __be64 u64 |
| 48 | #define __be32 u32 |
| 49 | #define __be16 u16 |
| 50 | |
| 51 | #define __u64 u64 |
| 52 | #define __u32 u32 |
| 53 | #define __u16 u16 |
| 54 | #define __u8 u8 |
| 55 | |
| 56 | typedef unsigned long long u64; |
| 57 | typedef unsigned int u32; |
| 58 | typedef unsigned short int u16; |
| 59 | typedef unsigned char u8; |
| 60 | |
| 61 | struct block_group_info; |
| 62 | |
| 63 | struct ext2_group_desc { |
| 64 | __le32 bg_block_bitmap; |
| 65 | __le32 bg_inode_bitmap; |
| 66 | __le32 bg_inode_table; |
| 67 | __le16 bg_free_blocks_count; |
| 68 | __le16 bg_free_inodes_count; |
| 69 | __le16 bg_used_dirs_count; |
| 70 | __le16 bg_pad; |
| 71 | __le32 bg_reserved[3]; |
| 72 | }; |
| 73 | |
| 74 | struct fs_info { |
| 75 | u64 len; |
| 76 | u32 block_size; |
| 77 | u32 blocks_per_group; |
| 78 | u32 inodes_per_group; |
| 79 | u32 inode_size; |
| 80 | u32 inodes; |
| 81 | u32 journal_blocks; |
| 82 | u16 feat_ro_compat; |
| 83 | u16 feat_compat; |
| 84 | u16 feat_incompat; |
| 85 | const char *label; |
Colin Cross | e4b5ae8 | 2010-08-03 14:10:07 -0700 | [diff] [blame] | 86 | u8 no_journal; |
Colin Cross | ec0a2e8 | 2010-06-11 14:21:37 -0700 | [diff] [blame] | 87 | }; |
| 88 | |
| 89 | struct fs_aux_info { |
| 90 | struct ext4_super_block *sb; |
| 91 | struct ext2_group_desc *bg_desc; |
| 92 | struct block_group_info *bgs; |
| 93 | u32 first_data_block; |
| 94 | u64 len_blocks; |
| 95 | u32 inode_table_blocks; |
| 96 | u32 groups; |
| 97 | u32 bg_desc_blocks; |
| 98 | u32 bg_desc_reserve_blocks; |
| 99 | u32 default_i_flags; |
| 100 | u32 blocks_per_ind; |
| 101 | u32 blocks_per_dind; |
| 102 | u32 blocks_per_tind; |
| 103 | }; |
| 104 | |
| 105 | extern struct fs_info info; |
| 106 | extern struct fs_aux_info aux_info; |
| 107 | |
| 108 | static inline int log_2(int j) |
| 109 | { |
| 110 | int i; |
| 111 | |
| 112 | for (i = 0; j > 0; i++) |
| 113 | j >>= 1; |
| 114 | |
| 115 | return i - 1; |
| 116 | } |
| 117 | |
Colin Cross | 881cca2 | 2010-06-20 23:57:06 -0700 | [diff] [blame] | 118 | int ext4_bg_has_super_block(int bg); |
Ken Sumrall | 75249ed | 2010-08-13 16:04:49 -0700 | [diff] [blame^] | 119 | void write_ext4_image(const char *filename, int gz, int sparse); |
Colin Cross | 881cca2 | 2010-06-20 23:57:06 -0700 | [diff] [blame] | 120 | void ext4_create_fs_aux_info(void); |
| 121 | void ext4_free_fs_aux_info(void); |
| 122 | void ext4_fill_in_sb(void); |
| 123 | void ext4_create_resize_inode(void); |
| 124 | void ext4_create_journal_inode(void); |
| 125 | void ext4_update_free(void); |
| 126 | u64 get_file_size(const char *filename); |
| 127 | u64 parse_num(const char *arg); |
| 128 | |
Colin Cross | ec0a2e8 | 2010-06-11 14:21:37 -0700 | [diff] [blame] | 129 | #endif |