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 | |
Doug Zongker | e8d74ff | 2011-10-28 10:21:08 -0700 | [diff] [blame] | 20 | #ifdef __cplusplus |
| 21 | extern "C" { |
| 22 | #endif |
| 23 | |
| 24 | #ifndef _GNU_SOURCE |
Colin Cross | 33f96c6 | 2010-12-22 18:40:14 -0800 | [diff] [blame] | 25 | #define _GNU_SOURCE |
Doug Zongker | e8d74ff | 2011-10-28 10:21:08 -0700 | [diff] [blame] | 26 | #endif |
Colin Cross | 33f96c6 | 2010-12-22 18:40:14 -0800 | [diff] [blame] | 27 | #define _FILE_OFFSET_BITS 64 |
Colin Cross | dc5abee | 2012-04-23 23:20:48 -0700 | [diff] [blame] | 28 | #define _LARGEFILE64_SOURCE 1 |
Colin Cross | 33f96c6 | 2010-12-22 18:40:14 -0800 | [diff] [blame] | 29 | #include <sys/types.h> |
| 30 | #include <unistd.h> |
| 31 | |
Colin Cross | ec0a2e8 | 2010-06-11 14:21:37 -0700 | [diff] [blame] | 32 | #include <sys/types.h> |
| 33 | #include <errno.h> |
| 34 | #include <stdarg.h> |
| 35 | #include <stdio.h> |
| 36 | #include <stdlib.h> |
| 37 | #include <string.h> |
Ken Sumrall | 2ae7663 | 2011-03-23 22:08:53 -0700 | [diff] [blame] | 38 | #include <setjmp.h> |
Nick Kralevich | 4df62f3 | 2013-02-07 14:21:34 -0800 | [diff] [blame] | 39 | #include <stdint.h> |
Colin Cross | ec0a2e8 | 2010-06-11 14:21:37 -0700 | [diff] [blame] | 40 | |
Colin Cross | 33f96c6 | 2010-12-22 18:40:14 -0800 | [diff] [blame] | 41 | #if defined(__APPLE__) && defined(__MACH__) |
| 42 | #define lseek64 lseek |
Colin Cross | fe4a031 | 2011-01-05 18:05:34 -0800 | [diff] [blame] | 43 | #define ftruncate64 ftruncate |
| 44 | #define mmap64 mmap |
Colin Cross | 33f96c6 | 2010-12-22 18:40:14 -0800 | [diff] [blame] | 45 | #define off64_t off_t |
| 46 | #endif |
| 47 | |
Colin Cross | 9a2b60b | 2014-01-23 13:13:02 -0800 | [diff] [blame] | 48 | #include "ext4_sb.h" |
Colin Cross | 7900c77 | 2014-01-23 12:53:30 -0800 | [diff] [blame] | 49 | |
Colin Cross | ec0a2e8 | 2010-06-11 14:21:37 -0700 | [diff] [blame] | 50 | extern int force; |
| 51 | |
| 52 | #define warn(fmt, args...) do { fprintf(stderr, "warning: %s: " fmt "\n", __func__, ## args); } while (0) |
Ken Sumrall | 2ae7663 | 2011-03-23 22:08:53 -0700 | [diff] [blame] | 53 | #define error(fmt, args...) do { fprintf(stderr, "error: %s: " fmt "\n", __func__, ## args); if (!force) longjmp(setjmp_env, EXIT_FAILURE); } while (0) |
Colin Cross | a7ed433 | 2010-12-22 23:08:15 -0800 | [diff] [blame] | 54 | #define error_errno(s, args...) error(s ": %s", ##args, strerror(errno)) |
Ken Sumrall | 2ae7663 | 2011-03-23 22:08:53 -0700 | [diff] [blame] | 55 | #define critical_error(fmt, args...) do { fprintf(stderr, "critical error: %s: " fmt "\n", __func__, ## args); longjmp(setjmp_env, EXIT_FAILURE); } while (0) |
Colin Cross | a7ed433 | 2010-12-22 23:08:15 -0800 | [diff] [blame] | 56 | #define critical_error_errno(s, args...) critical_error(s ": %s", ##args, strerror(errno)) |
Colin Cross | ec0a2e8 | 2010-06-11 14:21:37 -0700 | [diff] [blame] | 57 | |
Colin Cross | ec0a2e8 | 2010-06-11 14:21:37 -0700 | [diff] [blame] | 58 | #define EXT4_JNL_BACKUP_BLOCKS 1 |
| 59 | |
Raphael Moll | 4605b3f | 2012-02-03 23:02:33 -0800 | [diff] [blame] | 60 | #ifndef min /* already defined by windows.h */ |
Colin Cross | ec0a2e8 | 2010-06-11 14:21:37 -0700 | [diff] [blame] | 61 | #define min(a, b) ((a) < (b) ? (a) : (b)) |
Raphael Moll | 4605b3f | 2012-02-03 23:02:33 -0800 | [diff] [blame] | 62 | #endif |
Colin Cross | ec0a2e8 | 2010-06-11 14:21:37 -0700 | [diff] [blame] | 63 | |
| 64 | #define DIV_ROUND_UP(x, y) (((x) + (y) - 1)/(y)) |
Paul Lawrence | 40ce87a | 2014-03-03 10:51:58 -0800 | [diff] [blame] | 65 | #define EXT4_ALIGN(x, y) ((y) * DIV_ROUND_UP((x), (y))) |
Colin Cross | ec0a2e8 | 2010-06-11 14:21:37 -0700 | [diff] [blame] | 66 | |
Nick Kralevich | 4df62f3 | 2013-02-07 14:21:34 -0800 | [diff] [blame] | 67 | /* XXX */ |
| 68 | #define cpu_to_le32(x) (x) |
| 69 | #define cpu_to_le16(x) (x) |
| 70 | #define le32_to_cpu(x) (x) |
| 71 | #define le16_to_cpu(x) (x) |
| 72 | |
Colin Cross | af07234 | 2014-01-23 13:19:27 -0800 | [diff] [blame] | 73 | #ifdef __LP64__ |
| 74 | typedef unsigned long u64; |
| 75 | typedef signed long s64; |
| 76 | #else |
Colin Cross | ec0a2e8 | 2010-06-11 14:21:37 -0700 | [diff] [blame] | 77 | typedef unsigned long long u64; |
Ken Sumrall | 435a8b6 | 2011-01-14 18:33:06 -0800 | [diff] [blame] | 78 | typedef signed long long s64; |
Colin Cross | af07234 | 2014-01-23 13:19:27 -0800 | [diff] [blame] | 79 | #endif |
Colin Cross | ec0a2e8 | 2010-06-11 14:21:37 -0700 | [diff] [blame] | 80 | typedef unsigned int u32; |
| 81 | typedef unsigned short int u16; |
| 82 | typedef unsigned char u8; |
| 83 | |
| 84 | struct block_group_info; |
Nick Kralevich | 4df62f3 | 2013-02-07 14:21:34 -0800 | [diff] [blame] | 85 | struct xattr_list_element; |
Colin Cross | ec0a2e8 | 2010-06-11 14:21:37 -0700 | [diff] [blame] | 86 | |
| 87 | struct ext2_group_desc { |
Colin Cross | 7900c77 | 2014-01-23 12:53:30 -0800 | [diff] [blame] | 88 | u32 bg_block_bitmap; |
| 89 | u32 bg_inode_bitmap; |
| 90 | u32 bg_inode_table; |
| 91 | u16 bg_free_blocks_count; |
| 92 | u16 bg_free_inodes_count; |
| 93 | u16 bg_used_dirs_count; |
| 94 | u16 bg_flags; |
| 95 | u32 bg_reserved[2]; |
| 96 | u16 bg_reserved16; |
| 97 | u16 bg_checksum; |
Colin Cross | ec0a2e8 | 2010-06-11 14:21:37 -0700 | [diff] [blame] | 98 | }; |
| 99 | |
| 100 | struct fs_aux_info { |
| 101 | struct ext4_super_block *sb; |
Ken Sumrall | 107a9f1 | 2011-06-29 20:28:30 -0700 | [diff] [blame] | 102 | struct ext4_super_block **backup_sb; |
Colin Cross | ec0a2e8 | 2010-06-11 14:21:37 -0700 | [diff] [blame] | 103 | struct ext2_group_desc *bg_desc; |
| 104 | struct block_group_info *bgs; |
Nick Kralevich | 4df62f3 | 2013-02-07 14:21:34 -0800 | [diff] [blame] | 105 | struct xattr_list_element *xattrs; |
Colin Cross | ec0a2e8 | 2010-06-11 14:21:37 -0700 | [diff] [blame] | 106 | u32 first_data_block; |
| 107 | u64 len_blocks; |
| 108 | u32 inode_table_blocks; |
| 109 | u32 groups; |
| 110 | u32 bg_desc_blocks; |
Colin Cross | ec0a2e8 | 2010-06-11 14:21:37 -0700 | [diff] [blame] | 111 | u32 default_i_flags; |
| 112 | u32 blocks_per_ind; |
| 113 | u32 blocks_per_dind; |
| 114 | u32 blocks_per_tind; |
| 115 | }; |
| 116 | |
| 117 | extern struct fs_info info; |
| 118 | extern struct fs_aux_info aux_info; |
Colin Cross | 782879a | 2014-01-23 13:08:16 -0800 | [diff] [blame] | 119 | extern struct sparse_file *ext4_sparse_file; |
Colin Cross | ec0a2e8 | 2010-06-11 14:21:37 -0700 | [diff] [blame] | 120 | |
Ken Sumrall | 2ae7663 | 2011-03-23 22:08:53 -0700 | [diff] [blame] | 121 | extern jmp_buf setjmp_env; |
| 122 | |
Colin Cross | ec0a2e8 | 2010-06-11 14:21:37 -0700 | [diff] [blame] | 123 | static inline int log_2(int j) |
| 124 | { |
| 125 | int i; |
| 126 | |
| 127 | for (i = 0; j > 0; i++) |
| 128 | j >>= 1; |
| 129 | |
| 130 | return i - 1; |
| 131 | } |
| 132 | |
Paul Lawrence | ee2bba6 | 2014-01-21 08:26:21 -0800 | [diff] [blame] | 133 | int bitmap_get_bit(u8 *bitmap, u32 bit); |
| 134 | void bitmap_clear_bit(u8 *bitmap, u32 bit); |
Colin Cross | 881cca2 | 2010-06-20 23:57:06 -0700 | [diff] [blame] | 135 | int ext4_bg_has_super_block(int bg); |
Paul Lawrence | ee2bba6 | 2014-01-21 08:26:21 -0800 | [diff] [blame] | 136 | void read_sb(int fd, struct ext4_super_block *sb); |
| 137 | void write_sb(int fd, unsigned long long offset, struct ext4_super_block *sb); |
Colin Cross | dc5abee | 2012-04-23 23:20:48 -0700 | [diff] [blame] | 138 | void write_ext4_image(int fd, int gz, int sparse, int crc); |
Colin Cross | 881cca2 | 2010-06-20 23:57:06 -0700 | [diff] [blame] | 139 | void ext4_create_fs_aux_info(void); |
| 140 | void ext4_free_fs_aux_info(void); |
| 141 | void ext4_fill_in_sb(void); |
| 142 | void ext4_create_resize_inode(void); |
| 143 | void ext4_create_journal_inode(void); |
| 144 | void ext4_update_free(void); |
Colin Cross | b781330 | 2010-12-22 18:41:13 -0800 | [diff] [blame] | 145 | void ext4_queue_sb(void); |
Szymon Starzycki | fe87e11 | 2013-07-24 12:34:50 -0700 | [diff] [blame] | 146 | u64 get_block_device_size(int fd); |
David 'Digit' Turner | eb5fcc3 | 2014-06-12 20:54:50 +0200 | [diff] [blame] | 147 | int is_block_device_fd(int fd); |
Anatol Pomazau | 0349bd9 | 2012-01-11 15:12:27 -0800 | [diff] [blame] | 148 | u64 get_file_size(int fd); |
Colin Cross | 881cca2 | 2010-06-20 23:57:06 -0700 | [diff] [blame] | 149 | u64 parse_num(const char *arg); |
Colin Cross | 9a2b60b | 2014-01-23 13:13:02 -0800 | [diff] [blame] | 150 | void ext4_parse_sb_info(struct ext4_super_block *sb); |
Colin Cross | 56497f2 | 2013-02-04 00:44:55 -0800 | [diff] [blame] | 151 | u16 ext4_crc16(u16 crc_in, const void *buf, int size); |
Colin Cross | 881cca2 | 2010-06-20 23:57:06 -0700 | [diff] [blame] | 152 | |
Colin Cross | 9652986 | 2013-01-23 15:38:57 -0800 | [diff] [blame] | 153 | typedef void (*fs_config_func_t)(const char *path, int dir, unsigned *uid, unsigned *gid, |
Doug Zongker | 9526680 | 2013-12-05 15:51:28 -0800 | [diff] [blame] | 154 | unsigned *mode, uint64_t *capabilities); |
Colin Cross | 9652986 | 2013-01-23 15:38:57 -0800 | [diff] [blame] | 155 | |
| 156 | struct selabel_handle; |
| 157 | |
| 158 | int make_ext4fs_internal(int fd, const char *directory, |
Doug Zongker | 9526680 | 2013-12-05 15:51:28 -0800 | [diff] [blame] | 159 | const char *mountpoint, fs_config_func_t fs_config_func, int gzip, |
| 160 | int sparse, int crc, int wipe, |
Doug Zongker | bec598e | 2014-08-12 11:35:37 -0700 | [diff] [blame] | 161 | struct selabel_handle *sehnd, int verbose, time_t fixed_time, |
| 162 | FILE* block_list_file); |
Colin Cross | 9652986 | 2013-01-23 15:38:57 -0800 | [diff] [blame] | 163 | |
Paul Lawrence | ee2bba6 | 2014-01-21 08:26:21 -0800 | [diff] [blame] | 164 | int read_ext(int fd, int verbose); |
| 165 | |
Doug Zongker | e8d74ff | 2011-10-28 10:21:08 -0700 | [diff] [blame] | 166 | #ifdef __cplusplus |
| 167 | } |
| 168 | #endif |
| 169 | |
Colin Cross | ec0a2e8 | 2010-06-11 14:21:37 -0700 | [diff] [blame] | 170 | #endif |