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 #ifndef _EXT4_UTILS_H_
18 #define _EXT4_UTILS_H_
27 #define _FILE_OFFSET_BITS 64
28 #define _LARGEFILE64_SOURCE 1
29 #include <sys/types.h>
32 #include <sys/sysmacros.h>
48 #define warn(fmt, args...) do { fprintf(stderr, "warning: %s: " fmt "\n", __func__, ## args); } while (0)
49 #define error(fmt, args...) do { fprintf(stderr, "error: %s: " fmt "\n", __func__, ## args); if (!force) longjmp(setjmp_env, EXIT_FAILURE); } while (0)
50 #define error_errno(s, args...) error(s ": %s", ##args, strerror(errno))
51 #define critical_error(fmt, args...) do { fprintf(stderr, "critical error: %s: " fmt "\n", __func__, ## args); longjmp(setjmp_env, EXIT_FAILURE); } while (0)
52 #define critical_error_errno(s, args...) critical_error(s ": %s", ##args, strerror(errno))
54 #define EXT4_JNL_BACKUP_BLOCKS 1
56 #ifndef min /* already defined by windows.h */
57 #define min(a, b) ((a) < (b) ? (a) : (b))
60 #define DIV_ROUND_UP(x, y) (((x) + (y) - 1)/(y))
61 #define EXT4_ALIGN(x, y) ((y) * DIV_ROUND_UP((x), (y)))
64 #define cpu_to_le32(x) (x)
65 #define cpu_to_le16(x) (x)
66 #define le32_to_cpu(x) (x)
67 #define le16_to_cpu(x) (x)
70 typedef unsigned long u64
;
71 typedef signed long s64
;
73 typedef unsigned long long u64
;
74 typedef signed long long s64
;
76 typedef unsigned int u32
;
77 typedef unsigned short int u16
;
78 typedef unsigned char u8
;
80 struct block_group_info
;
81 struct xattr_list_element
;
83 struct ext2_group_desc
{
87 u16 bg_free_blocks_count
;
88 u16 bg_free_inodes_count
;
89 u16 bg_used_dirs_count
;
97 struct ext4_super_block
*sb
;
98 struct ext4_super_block
**backup_sb
;
99 struct ext2_group_desc
*bg_desc
;
100 struct block_group_info
*bgs
;
101 struct xattr_list_element
*xattrs
;
102 u32 first_data_block
;
104 u32 inode_table_blocks
;
113 extern struct fs_info info
;
114 extern struct fs_aux_info aux_info
;
115 extern struct sparse_file
*ext4_sparse_file
;
117 extern jmp_buf setjmp_env
;
119 static inline int log_2(int j
)
123 for (i
= 0; j
> 0; i
++)
129 int bitmap_get_bit(u8
*bitmap
, u32 bit
);
130 void bitmap_clear_bit(u8
*bitmap
, u32 bit
);
131 int ext4_bg_has_super_block(int bg
);
132 void read_sb(int fd
, struct ext4_super_block
*sb
);
133 void write_sb(int fd
, unsigned long long offset
, struct ext4_super_block
*sb
);
134 void write_ext4_image(int fd
, int gz
, int sparse
, int crc
);
135 void ext4_create_fs_aux_info(void);
136 void ext4_free_fs_aux_info(void);
137 void ext4_fill_in_sb(void);
138 void ext4_create_resize_inode(void);
139 void ext4_create_journal_inode(void);
140 void ext4_update_free(void);
141 void ext4_queue_sb(void);
142 u64
get_block_device_size(int fd
);
143 int is_block_device_fd(int fd
);
144 u64
get_file_size(int fd
);
145 u64
parse_num(const char *arg
);
146 void ext4_parse_sb_info(struct ext4_super_block
*sb
);
147 u16
ext4_crc16(u16 crc_in
, const void *buf
, int size
);
149 typedef int (*fs_config_func_t
)(const char *path
, int dir
, unsigned *uid
, unsigned *gid
,
150 unsigned *mode
, uint64_t *capabilities
);
152 struct selabel_handle
;
154 int make_ext4fs_internal(int fd
, const char *directory
,
155 fs_config_func_t fs_config_func
, int gzip
,
156 int sparse
, int crc
, int wipe
,
157 int verbose
, time_t fixed_time
,
158 FILE* block_list_file
);
160 int read_ext(int fd
, int verbose
);