ext4_utils.h revision 4605b3fb8a00fa37f617a8d0fe3a095d0503a845
1ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross/*
2ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross * Copyright (C) 2010 The Android Open Source Project
3ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross *
4ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross * Licensed under the Apache License, Version 2.0 (the "License");
5ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross * you may not use this file except in compliance with the License.
6ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross * You may obtain a copy of the License at
7ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross *
8ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross *      http://www.apache.org/licenses/LICENSE-2.0
9ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross *
10ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross * Unless required by applicable law or agreed to in writing, software
11ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross * distributed under the License is distributed on an "AS IS" BASIS,
12ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross * See the License for the specific language governing permissions and
14ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross * limitations under the License.
15ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross */
16ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross
17ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross#ifndef _EXT4_UTILS_H_
18ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross#define _EXT4_UTILS_H_
19ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross
20e8d74ff5c3253403d3e5c96edc80dfc3e5f8ada8Doug Zongker#ifdef __cplusplus
21e8d74ff5c3253403d3e5c96edc80dfc3e5f8ada8Doug Zongkerextern "C" {
22e8d74ff5c3253403d3e5c96edc80dfc3e5f8ada8Doug Zongker#endif
23e8d74ff5c3253403d3e5c96edc80dfc3e5f8ada8Doug Zongker
24e8d74ff5c3253403d3e5c96edc80dfc3e5f8ada8Doug Zongker#ifndef _GNU_SOURCE
2533f96c66e9a1f2e266a75e5e84c091dffa6ef118Colin Cross#define _GNU_SOURCE
26e8d74ff5c3253403d3e5c96edc80dfc3e5f8ada8Doug Zongker#endif
2733f96c66e9a1f2e266a75e5e84c091dffa6ef118Colin Cross#define _FILE_OFFSET_BITS 64
2833f96c66e9a1f2e266a75e5e84c091dffa6ef118Colin Cross#define _LARGEFILE64_SOURCE
2933f96c66e9a1f2e266a75e5e84c091dffa6ef118Colin Cross#include <sys/types.h>
3033f96c66e9a1f2e266a75e5e84c091dffa6ef118Colin Cross#include <unistd.h>
3133f96c66e9a1f2e266a75e5e84c091dffa6ef118Colin Cross
32ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross#include <sys/types.h>
33ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross#include <errno.h>
34ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross#include <stdarg.h>
35ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross#include <stdio.h>
36ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross#include <stdlib.h>
37ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross#include <string.h>
382ae7663e1e064000356ee9e49ebd08bdddc545f9Ken Sumrall#include <setjmp.h>
39ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross
4033f96c66e9a1f2e266a75e5e84c091dffa6ef118Colin Cross#if defined(__APPLE__) && defined(__MACH__)
4133f96c66e9a1f2e266a75e5e84c091dffa6ef118Colin Cross#define lseek64 lseek
42fe4a03182b5b17d333511c72406f926a791345d3Colin Cross#define ftruncate64 ftruncate
43fe4a03182b5b17d333511c72406f926a791345d3Colin Cross#define mmap64 mmap
4433f96c66e9a1f2e266a75e5e84c091dffa6ef118Colin Cross#define off64_t off_t
4533f96c66e9a1f2e266a75e5e84c091dffa6ef118Colin Cross#endif
4633f96c66e9a1f2e266a75e5e84c091dffa6ef118Colin Cross
4733f96c66e9a1f2e266a75e5e84c091dffa6ef118Colin Cross#ifdef __BIONIC__
4833f96c66e9a1f2e266a75e5e84c091dffa6ef118Colin Crossextern void*  __mmap2(void *, size_t, int, int, int, off_t);
4933f96c66e9a1f2e266a75e5e84c091dffa6ef118Colin Crossstatic inline void *mmap64(void *addr, size_t length, int prot, int flags,
5033f96c66e9a1f2e266a75e5e84c091dffa6ef118Colin Cross        int fd, off64_t offset)
5133f96c66e9a1f2e266a75e5e84c091dffa6ef118Colin Cross{
5233f96c66e9a1f2e266a75e5e84c091dffa6ef118Colin Cross    return __mmap2(addr, length, prot, flags, fd, offset >> 12);
5333f96c66e9a1f2e266a75e5e84c091dffa6ef118Colin Cross}
5433f96c66e9a1f2e266a75e5e84c091dffa6ef118Colin Cross#endif
5533f96c66e9a1f2e266a75e5e84c091dffa6ef118Colin Cross
56ec0a2e83dc66d67addeb90e83144187691852a3eColin Crossextern int force;
57ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross
58ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross#define warn(fmt, args...) do { fprintf(stderr, "warning: %s: " fmt "\n", __func__, ## args); } while (0)
592ae7663e1e064000356ee9e49ebd08bdddc545f9Ken Sumrall#define error(fmt, args...) do { fprintf(stderr, "error: %s: " fmt "\n", __func__, ## args); if (!force) longjmp(setjmp_env, EXIT_FAILURE); } while (0)
60a7ed433f2dc0116627a93b18fbb260f0665ca0cbColin Cross#define error_errno(s, args...) error(s ": %s", ##args, strerror(errno))
612ae7663e1e064000356ee9e49ebd08bdddc545f9Ken Sumrall#define critical_error(fmt, args...) do { fprintf(stderr, "critical error: %s: " fmt "\n", __func__, ## args); longjmp(setjmp_env, EXIT_FAILURE); } while (0)
62a7ed433f2dc0116627a93b18fbb260f0665ca0cbColin Cross#define critical_error_errno(s, args...) critical_error(s ": %s", ##args, strerror(errno))
63ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross
64ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross#define EXT4_SUPER_MAGIC 0xEF53
65ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross#define EXT4_JNL_BACKUP_BLOCKS 1
66ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross
674605b3fb8a00fa37f617a8d0fe3a095d0503a845Raphael Moll#ifndef min /* already defined by windows.h */
68ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross#define min(a, b) ((a) < (b) ? (a) : (b))
694605b3fb8a00fa37f617a8d0fe3a095d0503a845Raphael Moll#endif
70ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross
71ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross#define DIV_ROUND_UP(x, y) (((x) + (y) - 1)/(y))
726bd2b5d80db5630cb73e3e5a4be3b38afa241c5aColin Cross#define ALIGN(x, y) ((y) * DIV_ROUND_UP((x), (y)))
73ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross
74ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross#define __le64 u64
75ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross#define __le32 u32
76ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross#define __le16 u16
77ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross
78ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross#define __be64 u64
79ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross#define __be32 u32
80ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross#define __be16 u16
81ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross
82ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross#define __u64 u64
83ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross#define __u32 u32
84ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross#define __u16 u16
85ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross#define __u8 u8
86ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross
87ec0a2e83dc66d67addeb90e83144187691852a3eColin Crosstypedef unsigned long long u64;
88435a8b61e925e3efb22fce08612efe210e83f791Ken Sumralltypedef signed long long s64;
89ec0a2e83dc66d67addeb90e83144187691852a3eColin Crosstypedef unsigned int u32;
90ec0a2e83dc66d67addeb90e83144187691852a3eColin Crosstypedef unsigned short int u16;
91ec0a2e83dc66d67addeb90e83144187691852a3eColin Crosstypedef unsigned char u8;
92ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross
93ec0a2e83dc66d67addeb90e83144187691852a3eColin Crossstruct block_group_info;
94ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross
95ec0a2e83dc66d67addeb90e83144187691852a3eColin Crossstruct ext2_group_desc {
96ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross	__le32 bg_block_bitmap;
97ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross	__le32 bg_inode_bitmap;
98ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross	__le32 bg_inode_table;
99ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross	__le16 bg_free_blocks_count;
100ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross	__le16 bg_free_inodes_count;
101ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross	__le16 bg_used_dirs_count;
102ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross	__le16 bg_pad;
103ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross	__le32 bg_reserved[3];
104ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross};
105ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross
106ec0a2e83dc66d67addeb90e83144187691852a3eColin Crossstruct fs_info {
107435a8b61e925e3efb22fce08612efe210e83f791Ken Sumrall	s64 len;	/* If set to 0, ask the block device for the size,
108435a8b61e925e3efb22fce08612efe210e83f791Ken Sumrall			 * if less than 0, reserve that much space at the
109435a8b61e925e3efb22fce08612efe210e83f791Ken Sumrall			 * end of the partition, else use the size given. */
110ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross	u32 block_size;
111ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross	u32 blocks_per_group;
112ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross	u32 inodes_per_group;
113ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross	u32 inode_size;
114ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross	u32 inodes;
115ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross	u32 journal_blocks;
116ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross	u16 feat_ro_compat;
117ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross	u16 feat_compat;
118ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross	u16 feat_incompat;
11922742ce739a046a079b2e1b03342a25472dfa352Colin Cross	u32 bg_desc_reserve_blocks;
120ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross	const char *label;
121e4b5ae8ab07e698b95f004c9226000b02f853abcColin Cross	u8 no_journal;
122ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross};
123ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross
124ec0a2e83dc66d67addeb90e83144187691852a3eColin Crossstruct fs_aux_info {
125ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross	struct ext4_super_block *sb;
126107a9f161babc20daf915311146b0e864d3b4157Ken Sumrall	struct ext4_super_block **backup_sb;
127ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross	struct ext2_group_desc *bg_desc;
128ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross	struct block_group_info *bgs;
129ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross	u32 first_data_block;
130ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross	u64 len_blocks;
131ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross	u32 inode_table_blocks;
132ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross	u32 groups;
133ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross	u32 bg_desc_blocks;
134ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross	u32 default_i_flags;
135ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross	u32 blocks_per_ind;
136ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross	u32 blocks_per_dind;
137ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross	u32 blocks_per_tind;
138ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross};
139ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross
140ec0a2e83dc66d67addeb90e83144187691852a3eColin Crossextern struct fs_info info;
141ec0a2e83dc66d67addeb90e83144187691852a3eColin Crossextern struct fs_aux_info aux_info;
142ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross
1432ae7663e1e064000356ee9e49ebd08bdddc545f9Ken Sumrallextern jmp_buf setjmp_env;
1442ae7663e1e064000356ee9e49ebd08bdddc545f9Ken Sumrall
145ec0a2e83dc66d67addeb90e83144187691852a3eColin Crossstatic inline int log_2(int j)
146ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross{
147ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross	int i;
148ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross
149ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross	for (i = 0; j > 0; i++)
150ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross		j >>= 1;
151ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross
152ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross	return i - 1;
153ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross}
154ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross
155881cca2f88ddcce86483b3ba95546b5641de8c0eColin Crossint ext4_bg_has_super_block(int bg);
156354350e3a92955e66abd1ea15536f8b6e5e66493Anatol Pomazauvoid write_ext4_image(const char *filename, int gz, int sparse, int crc,
157c2470654d4b4db09a7052fc5fa108ac21f1b1948Colin Cross		int wipe);
158881cca2f88ddcce86483b3ba95546b5641de8c0eColin Crossvoid ext4_create_fs_aux_info(void);
159881cca2f88ddcce86483b3ba95546b5641de8c0eColin Crossvoid ext4_free_fs_aux_info(void);
160881cca2f88ddcce86483b3ba95546b5641de8c0eColin Crossvoid ext4_fill_in_sb(void);
161881cca2f88ddcce86483b3ba95546b5641de8c0eColin Crossvoid ext4_create_resize_inode(void);
162881cca2f88ddcce86483b3ba95546b5641de8c0eColin Crossvoid ext4_create_journal_inode(void);
163881cca2f88ddcce86483b3ba95546b5641de8c0eColin Crossvoid ext4_update_free(void);
164b781330b1acae2e5706bbda8d81e5f7575f40e2aColin Crossvoid ext4_queue_sb(void);
165354350e3a92955e66abd1ea15536f8b6e5e66493Anatol Pomazauu64 get_file_size(const char *filename);
166881cca2f88ddcce86483b3ba95546b5641de8c0eColin Crossu64 parse_num(const char *arg);
167a7ed433f2dc0116627a93b18fbb260f0665ca0cbColin Crossvoid ext4_parse_sb(struct ext4_super_block *sb);
168881cca2f88ddcce86483b3ba95546b5641de8c0eColin Cross
169e8d74ff5c3253403d3e5c96edc80dfc3e5f8ada8Doug Zongker#ifdef __cplusplus
170e8d74ff5c3253403d3e5c96edc80dfc3e5f8ada8Doug Zongker}
171e8d74ff5c3253403d3e5c96edc80dfc3e5f8ada8Doug Zongker#endif
172e8d74ff5c3253403d3e5c96edc80dfc3e5f8ada8Doug Zongker
173ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross#endif
174