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 #ifdef __cplusplus
21 extern "C" {
22 #endif
23
24 #ifndef _GNU_SOURCE
25 #define _GNU_SOURCE
26 #endif
27 #define _FILE_OFFSET_BITS 64
28 #define _LARGEFILE64_SOURCE 1
29 #include <sys/types.h>
30 #include <unistd.h>
31
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>
38 #include <setjmp.h>
39 #include <stdint.h>
40
41 #if defined(__APPLE__) && defined(__MACH__)
42 #define lseek64 lseek
43 #define ftruncate64 ftruncate
44 #define mmap64 mmap
45 #define off64_t off_t
46 #endif
47
48 #ifdef __BIONIC__
49 extern void* __mmap2(void *, size_t, int, int, int, off_t);
mmap64(void * addr,size_t length,int prot,int flags,int fd,off64_t offset)50 static inline void *mmap64(void *addr, size_t length, int prot, int flags,
51 int fd, off64_t offset)
52 {
53 return __mmap2(addr, length, prot, flags, fd, offset >> 12);
54 }
55 #endif
56
57 extern int force;
58
59 #define warn(fmt, args...) do { fprintf(stderr, "warning: %s: " fmt "\n", __func__, ## args); } while (0)
60 #define error(fmt, args...) do { fprintf(stderr, "error: %s: " fmt "\n", __func__, ## args); if (!force) longjmp(setjmp_env, EXIT_FAILURE); } while (0)
61 #define error_errno(s, args...) error(s ": %s", ##args, strerror(errno))
62 #define critical_error(fmt, args...) do { fprintf(stderr, "critical error: %s: " fmt "\n", __func__, ## args); longjmp(setjmp_env, EXIT_FAILURE); } while (0)
63 #define critical_error_errno(s, args...) critical_error(s ": %s", ##args, strerror(errno))
64
65 #define EXT4_SUPER_MAGIC 0xEF53
66 #define EXT4_JNL_BACKUP_BLOCKS 1
67
68 #ifndef min /* already defined by windows.h */
69 #define min(a, b) ((a) < (b) ? (a) : (b))
70 #endif
71
72 #define DIV_ROUND_UP(x, y) (((x) + (y) - 1)/(y))
73 #define ALIGN(x, y) ((y) * DIV_ROUND_UP((x), (y)))
74
75 #define __le64 u64
76 #define __le32 u32
77 #define __le16 u16
78
79 #define __be64 u64
80 #define __be32 u32
81 #define __be16 u16
82
83 #define __u64 u64
84 #define __u32 u32
85 #define __u16 u16
86 #define __u8 u8
87
88 /* XXX */
89 #define cpu_to_le32(x) (x)
90 #define cpu_to_le16(x) (x)
91 #define le32_to_cpu(x) (x)
92 #define le16_to_cpu(x) (x)
93
94 typedef unsigned long long u64;
95 typedef signed long long s64;
96 typedef unsigned int u32;
97 typedef unsigned short int u16;
98 typedef unsigned char u8;
99
100 struct block_group_info;
101 struct xattr_list_element;
102
103 struct ext2_group_desc {
104 __le32 bg_block_bitmap;
105 __le32 bg_inode_bitmap;
106 __le32 bg_inode_table;
107 __le16 bg_free_blocks_count;
108 __le16 bg_free_inodes_count;
109 __le16 bg_used_dirs_count;
110 __le16 bg_flags;
111 __le32 bg_reserved[2];
112 __le16 bg_reserved16;
113 __le16 bg_checksum;
114 };
115
116 struct fs_info {
117 s64 len; /* If set to 0, ask the block device for the size,
118 * if less than 0, reserve that much space at the
119 * end of the partition, else use the size given. */
120 u32 block_size;
121 u32 blocks_per_group;
122 u32 inodes_per_group;
123 u32 inode_size;
124 u32 inodes;
125 u32 journal_blocks;
126 u16 feat_ro_compat;
127 u16 feat_compat;
128 u16 feat_incompat;
129 u32 bg_desc_reserve_blocks;
130 const char *label;
131 u8 no_journal;
132
133 struct sparse_file *sparse_file;
134 };
135
136 struct fs_aux_info {
137 struct ext4_super_block *sb;
138 struct ext4_super_block **backup_sb;
139 struct ext2_group_desc *bg_desc;
140 struct block_group_info *bgs;
141 struct xattr_list_element *xattrs;
142 u32 first_data_block;
143 u64 len_blocks;
144 u32 inode_table_blocks;
145 u32 groups;
146 u32 bg_desc_blocks;
147 u32 default_i_flags;
148 u32 blocks_per_ind;
149 u32 blocks_per_dind;
150 u32 blocks_per_tind;
151 };
152
153 extern struct fs_info info;
154 extern struct fs_aux_info aux_info;
155
156 extern jmp_buf setjmp_env;
157
log_2(int j)158 static inline int log_2(int j)
159 {
160 int i;
161
162 for (i = 0; j > 0; i++)
163 j >>= 1;
164
165 return i - 1;
166 }
167
168 int ext4_bg_has_super_block(int bg);
169 void write_ext4_image(int fd, int gz, int sparse, int crc);
170 void ext4_create_fs_aux_info(void);
171 void ext4_free_fs_aux_info(void);
172 void ext4_fill_in_sb(void);
173 void ext4_create_resize_inode(void);
174 void ext4_create_journal_inode(void);
175 void ext4_update_free(void);
176 void ext4_queue_sb(void);
177 u64 get_block_device_size(int fd);
178 u64 get_file_size(int fd);
179 u64 parse_num(const char *arg);
180 void ext4_parse_sb(struct ext4_super_block *sb);
181 u16 ext4_crc16(u16 crc_in, const void *buf, int size);
182
183 typedef void (*fs_config_func_t)(const char *path, int dir, unsigned *uid, unsigned *gid,
184 unsigned *mode, uint64_t *capabilities);
185
186 struct selabel_handle;
187
188 int make_ext4fs_internal(int fd, const char *directory,
189 const char *mountpoint, fs_config_func_t fs_config_func, int gzip,
190 int sparse, int crc, int wipe,
191 struct selabel_handle *sehnd, int verbose);
192
193 #ifdef __cplusplus
194 }
195 #endif
196
197 #endif
198