ext4_utils.h revision b781330b1acae2e5706bbda8d81e5f7575f40e2a
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#define _GNU_SOURCE
21#define _FILE_OFFSET_BITS 64
22#define _LARGEFILE64_SOURCE
23#include <sys/types.h>
24#include <unistd.h>
25
26#include <sys/types.h>
27#include <errno.h>
28#include <stdarg.h>
29#include <stdio.h>
30#include <stdlib.h>
31#include <string.h>
32
33#if defined(__APPLE__) && defined(__MACH__)
34#define lseek64 lseek
35#define off64_t off_t
36#endif
37
38#ifdef __BIONIC__
39extern void*  __mmap2(void *, size_t, int, int, int, off_t);
40static inline void *mmap64(void *addr, size_t length, int prot, int flags,
41        int fd, off64_t offset)
42{
43    return __mmap2(addr, length, prot, flags, fd, offset >> 12);
44}
45#endif
46
47extern int force;
48
49#define warn(fmt, args...) do { fprintf(stderr, "warning: %s: " fmt "\n", __func__, ## args); } while (0)
50#define error(fmt, args...) do { fprintf(stderr, "error: %s: " fmt "\n", __func__, ## args); if (!force) exit(EXIT_FAILURE); } while (0)
51#define error_errno(s) error(s ": %s", strerror(errno))
52#define critical_error(fmt, args...) do { fprintf(stderr, "critical error: %s: " fmt "\n", __func__, ## args); exit(EXIT_FAILURE); } while (0)
53#define critical_error_errno(s) critical_error(s ": %s", strerror(errno))
54
55#define EXT4_SUPER_MAGIC 0xEF53
56#define EXT4_JNL_BACKUP_BLOCKS 1
57
58#define min(a, b) ((a) < (b) ? (a) : (b))
59
60#define DIV_ROUND_UP(x, y) (((x) + (y) - 1)/(y))
61#define ALIGN(x, y) ((y) * DIV_ROUND_UP((x), (y)))
62
63#define __le64 u64
64#define __le32 u32
65#define __le16 u16
66
67#define __be64 u64
68#define __be32 u32
69#define __be16 u16
70
71#define __u64 u64
72#define __u32 u32
73#define __u16 u16
74#define __u8 u8
75
76typedef unsigned long long u64;
77typedef unsigned int u32;
78typedef unsigned short int u16;
79typedef unsigned char u8;
80
81struct block_group_info;
82
83struct ext2_group_desc {
84	__le32 bg_block_bitmap;
85	__le32 bg_inode_bitmap;
86	__le32 bg_inode_table;
87	__le16 bg_free_blocks_count;
88	__le16 bg_free_inodes_count;
89	__le16 bg_used_dirs_count;
90	__le16 bg_pad;
91	__le32 bg_reserved[3];
92};
93
94struct fs_info {
95	u64 len;
96	u32 block_size;
97	u32 blocks_per_group;
98	u32 inodes_per_group;
99	u32 inode_size;
100	u32 inodes;
101	u32 journal_blocks;
102	u16 feat_ro_compat;
103	u16 feat_compat;
104	u16 feat_incompat;
105	u32 bg_desc_reserve_blocks;
106	const char *label;
107	u8 no_journal;
108};
109
110struct fs_aux_info {
111	struct ext4_super_block *sb;
112	struct ext2_group_desc *bg_desc;
113	struct block_group_info *bgs;
114	u32 first_data_block;
115	u64 len_blocks;
116	u32 inode_table_blocks;
117	u32 groups;
118	u32 bg_desc_blocks;
119	u32 default_i_flags;
120	u32 blocks_per_ind;
121	u32 blocks_per_dind;
122	u32 blocks_per_tind;
123};
124
125extern struct fs_info info;
126extern struct fs_aux_info aux_info;
127
128static inline int log_2(int j)
129{
130	int i;
131
132	for (i = 0; j > 0; i++)
133		j >>= 1;
134
135	return i - 1;
136}
137
138int ext4_bg_has_super_block(int bg);
139void write_ext4_image(const char *filename, int gz, int sparse);
140void ext4_create_fs_aux_info(void);
141void ext4_free_fs_aux_info(void);
142void ext4_fill_in_sb(void);
143void ext4_create_resize_inode(void);
144void ext4_create_journal_inode(void);
145void ext4_update_free(void);
146void ext4_queue_sb(void);
147u64 get_file_size(const char *filename);
148u64 parse_num(const char *arg);
149
150#endif
151