Only include sys/sysmacros.h on glibc, it is not portable
[project/make_ext4fs.git] / ext4_utils.h
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
31 #ifdef __GLIBC__
32 #include <sys/sysmacros.h>
33 #endif
34
35 #include <unistd.h>
36 #include <errno.h>
37 #include <stdarg.h>
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <string.h>
41 #include <setjmp.h>
42 #include <stdint.h>
43
44 #if defined(__APPLE__) && defined(__MACH__)
45 #define lseek64 lseek
46 #define ftruncate64 ftruncate
47 #define mmap64 mmap
48 #define off64_t off_t
49 #endif
50
51 #include "ext4_sb.h"
52
53 extern int force;
54
55 #define warn(fmt, args...) do { fprintf(stderr, "warning: %s: " fmt "\n", __func__, ## args); } while (0)
56 #define error(fmt, args...) do { fprintf(stderr, "error: %s: " fmt "\n", __func__, ## args); if (!force) longjmp(setjmp_env, EXIT_FAILURE); } while (0)
57 #define error_errno(s, args...) error(s ": %s", ##args, strerror(errno))
58 #define critical_error(fmt, args...) do { fprintf(stderr, "critical error: %s: " fmt "\n", __func__, ## args); longjmp(setjmp_env, EXIT_FAILURE); } while (0)
59 #define critical_error_errno(s, args...) critical_error(s ": %s", ##args, strerror(errno))
60
61 #define EXT4_JNL_BACKUP_BLOCKS 1
62
63 #ifndef min /* already defined by windows.h */
64 #define min(a, b) ((a) < (b) ? (a) : (b))
65 #endif
66
67 #define DIV_ROUND_UP(x, y) (((x) + (y) - 1)/(y))
68 #define EXT4_ALIGN(x, y) ((y) * DIV_ROUND_UP((x), (y)))
69
70 /* XXX */
71 #define cpu_to_le32(x) (x)
72 #define cpu_to_le16(x) (x)
73 #define le32_to_cpu(x) (x)
74 #define le16_to_cpu(x) (x)
75
76 #ifdef __LP64__
77 typedef unsigned long u64;
78 typedef signed long s64;
79 #else
80 typedef unsigned long long u64;
81 typedef signed long long s64;
82 #endif
83 typedef unsigned int u32;
84 typedef unsigned short int u16;
85 typedef unsigned char u8;
86
87 struct block_group_info;
88 struct xattr_list_element;
89
90 struct ext2_group_desc {
91         u32 bg_block_bitmap;
92         u32 bg_inode_bitmap;
93         u32 bg_inode_table;
94         u16 bg_free_blocks_count;
95         u16 bg_free_inodes_count;
96         u16 bg_used_dirs_count;
97         u16 bg_flags;
98         u32 bg_reserved[2];
99         u16 bg_reserved16;
100         u16 bg_checksum;
101 };
102
103 struct fs_aux_info {
104         struct ext4_super_block *sb;
105         struct ext4_super_block **backup_sb;
106         struct ext2_group_desc *bg_desc;
107         struct block_group_info *bgs;
108         struct xattr_list_element *xattrs;
109         u32 first_data_block;
110         u64 len_blocks;
111         u32 inode_table_blocks;
112         u32 groups;
113         u32 bg_desc_blocks;
114         u32 default_i_flags;
115         u32 blocks_per_ind;
116         u32 blocks_per_dind;
117         u32 blocks_per_tind;
118 };
119
120 extern struct fs_info info;
121 extern struct fs_aux_info aux_info;
122 extern struct sparse_file *ext4_sparse_file;
123
124 extern jmp_buf setjmp_env;
125
126 static inline int log_2(int j)
127 {
128         int i;
129
130         for (i = 0; j > 0; i++)
131                 j >>= 1;
132
133         return i - 1;
134 }
135
136 int bitmap_get_bit(u8 *bitmap, u32 bit);
137 void bitmap_clear_bit(u8 *bitmap, u32 bit);
138 int ext4_bg_has_super_block(int bg);
139 void read_sb(int fd, struct ext4_super_block *sb);
140 void write_sb(int fd, unsigned long long offset, struct ext4_super_block *sb);
141 void write_ext4_image(int fd, int gz, int sparse, int crc);
142 void ext4_create_fs_aux_info(void);
143 void ext4_free_fs_aux_info(void);
144 void ext4_fill_in_sb(void);
145 void ext4_create_resize_inode(void);
146 void ext4_create_journal_inode(void);
147 void ext4_update_free(void);
148 void ext4_queue_sb(void);
149 u64 get_block_device_size(int fd);
150 int is_block_device_fd(int fd);
151 u64 get_file_size(int fd);
152 u64 parse_num(const char *arg);
153 void ext4_parse_sb_info(struct ext4_super_block *sb);
154 u16 ext4_crc16(u16 crc_in, const void *buf, int size);
155
156 typedef int (*fs_config_func_t)(const char *path, int dir, unsigned *uid, unsigned *gid,
157                 unsigned *mode, uint64_t *capabilities);
158
159 struct selabel_handle;
160
161 int make_ext4fs_internal(int fd, const char *directory,
162                                                  fs_config_func_t fs_config_func, int gzip,
163                                                  int sparse, int crc, int wipe,
164                                                  int verbose, time_t fixed_time,
165                                                  FILE* block_list_file);
166
167 int read_ext(int fd, int verbose);
168
169 #ifdef __cplusplus
170 }
171 #endif
172
173 #endif