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