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 <errno.h>
33 #include <setjmp.h>
34 #include <stdarg.h>
35 #include <stdint.h>
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <string.h>
39 #include <sys/types.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 #include "ext4_sb.h"
49 
50 extern int force;
51 
52 #define EXT4_JNL_BACKUP_BLOCKS 1
53 
54 #ifndef __cplusplus
55 #ifndef min /* already defined by windows.h */
56 #define min(a, b) ((a) < (b) ? (a) : (b))
57 #endif
58 #endif
59 
60 #define DIV_ROUND_UP(x, y) (((x) + (y)-1) / (y))
61 #define EXT4_ALIGN(x, y) ((y)*DIV_ROUND_UP((x), (y)))
62 
63 /* XXX */
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)
68 
69 #ifdef __LP64__
70 typedef unsigned long u64;
71 typedef signed long s64;
72 
73 #define PRIext4u64 "lu"
74 #else
75 typedef unsigned long long u64;
76 typedef signed long long s64;
77 
78 #define PRIext4u64 PRIu64
79 #endif
80 typedef unsigned int u32;
81 typedef unsigned short int u16;
82 typedef unsigned char u8;
83 
84 struct block_group_info;
85 struct xattr_list_element;
86 
87 struct ext2_group_desc {
88     u64 bg_block_bitmap;
89     u64 bg_inode_bitmap;
90     u64 bg_inode_table;
91     u32 bg_free_blocks_count;
92     u32 bg_free_inodes_count;
93     u32 bg_used_dirs_count;
94     u16 bg_flags;
95 };
96 
97 struct fs_aux_info {
98     struct ext4_super_block* sb;
99     struct ext4_super_block* sb_block;
100     struct ext4_super_block* sb_zero;
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     u64 blocks_per_ind;
112     u64 blocks_per_dind;
113     u64 blocks_per_tind;
114 };
115 
116 extern struct fs_info info;
117 extern struct fs_aux_info aux_info;
118 
119 extern jmp_buf setjmp_env;
120 
121 int bitmap_get_bit(u8* bitmap, u32 bit);  // vold
122 u64 get_block_device_size(int fd);        // recovery
123 int is_block_device_fd(int fd);           // wipe.c
124 u64 get_file_size(int fd);                // fs_mgr
125 int ext4_bg_has_super_block(int bg);
126 int read_ext(int fd, int verbose);  // vold
127 
128 #ifdef __cplusplus
129 }
130 #endif
131 
132 #endif
133