ext4_utils.h revision c2470654d4b4db09a7052fc5fa108ac21f1b1948
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 ftruncate64 ftruncate
36#define mmap64 mmap
37#define off64_t off_t
38#endif
39
40#ifdef __BIONIC__
41extern void*  __mmap2(void *, size_t, int, int, int, off_t);
42static inline void *mmap64(void *addr, size_t length, int prot, int flags,
43        int fd, off64_t offset)
44{
45    return __mmap2(addr, length, prot, flags, fd, offset >> 12);
46}
47#endif
48
49extern 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) exit(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); exit(EXIT_FAILURE); } while (0)
55#define critical_error_errno(s, args...) critical_error(s ": %s", ##args, strerror(errno))
56
57#define EXT4_SUPER_MAGIC 0xEF53
58#define EXT4_JNL_BACKUP_BLOCKS 1
59
60#define min(a, b) ((a) < (b) ? (a) : (b))
61
62#define DIV_ROUND_UP(x, y) (((x) + (y) - 1)/(y))
63#define ALIGN(x, y) ((y) * DIV_ROUND_UP((x), (y)))
64
65#define __le64 u64
66#define __le32 u32
67#define __le16 u16
68
69#define __be64 u64
70#define __be32 u32
71#define __be16 u16
72
73#define __u64 u64
74#define __u32 u32
75#define __u16 u16
76#define __u8 u8
77
78typedef unsigned long long u64;
79typedef signed long long s64;
80typedef unsigned int u32;
81typedef unsigned short int u16;
82typedef unsigned char u8;
83
84struct block_group_info;
85
86struct ext2_group_desc {
87	__le32 bg_block_bitmap;
88	__le32 bg_inode_bitmap;
89	__le32 bg_inode_table;
90	__le16 bg_free_blocks_count;
91	__le16 bg_free_inodes_count;
92	__le16 bg_used_dirs_count;
93	__le16 bg_pad;
94	__le32 bg_reserved[3];
95};
96
97struct fs_info {
98	s64 len;	/* If set to 0, ask the block device for the size,
99			 * if less than 0, reserve that much space at the
100			 * end of the partition, else use the size given. */
101	u32 block_size;
102	u32 blocks_per_group;
103	u32 inodes_per_group;
104	u32 inode_size;
105	u32 inodes;
106	u32 journal_blocks;
107	u16 feat_ro_compat;
108	u16 feat_compat;
109	u16 feat_incompat;
110	u32 bg_desc_reserve_blocks;
111	const char *label;
112	u8 no_journal;
113};
114
115struct fs_aux_info {
116	struct ext4_super_block *sb;
117	struct ext2_group_desc *bg_desc;
118	struct block_group_info *bgs;
119	u32 first_data_block;
120	u64 len_blocks;
121	u32 inode_table_blocks;
122	u32 groups;
123	u32 bg_desc_blocks;
124	u32 default_i_flags;
125	u32 blocks_per_ind;
126	u32 blocks_per_dind;
127	u32 blocks_per_tind;
128};
129
130extern struct fs_info info;
131extern struct fs_aux_info aux_info;
132
133static inline int log_2(int j)
134{
135	int i;
136
137	for (i = 0; j > 0; i++)
138		j >>= 1;
139
140	return i - 1;
141}
142
143int ext4_bg_has_super_block(int bg);
144void write_ext4_image(const char *filename, int gz, int sparse, int crc,
145		int wipe);
146void ext4_create_fs_aux_info(void);
147void ext4_free_fs_aux_info(void);
148void ext4_fill_in_sb(void);
149void ext4_create_resize_inode(void);
150void ext4_create_journal_inode(void);
151void ext4_update_free(void);
152void ext4_queue_sb(void);
153u64 get_file_size(const char *filename);
154u64 parse_num(const char *arg);
155void ext4_parse_sb(struct ext4_super_block *sb);
156
157#endif
158