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