blob: 4518b98dd077744f4310bb7593ac8fb81a6e3224 [file] [log] [blame]
Colin Crossec0a2e82010-06-11 14:21:37 -07001/*
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
Colin Cross33f96c62010-12-22 18:40:14 -080020#define _GNU_SOURCE
21#define _FILE_OFFSET_BITS 64
22#define _LARGEFILE64_SOURCE
23#include <sys/types.h>
24#include <unistd.h>
25
Colin Crossec0a2e82010-06-11 14:21:37 -070026#include <sys/types.h>
27#include <errno.h>
28#include <stdarg.h>
29#include <stdio.h>
30#include <stdlib.h>
31#include <string.h>
Ken Sumrall2ae76632011-03-23 22:08:53 -070032#include <setjmp.h>
Colin Crossec0a2e82010-06-11 14:21:37 -070033
Colin Cross33f96c62010-12-22 18:40:14 -080034#if defined(__APPLE__) && defined(__MACH__)
35#define lseek64 lseek
Colin Crossfe4a0312011-01-05 18:05:34 -080036#define ftruncate64 ftruncate
37#define mmap64 mmap
Colin Cross33f96c62010-12-22 18:40:14 -080038#define off64_t off_t
39#endif
40
41#ifdef __BIONIC__
42extern void* __mmap2(void *, size_t, int, int, int, off_t);
43static inline void *mmap64(void *addr, size_t length, int prot, int flags,
44 int fd, off64_t offset)
45{
46 return __mmap2(addr, length, prot, flags, fd, offset >> 12);
47}
48#endif
49
Colin Crossec0a2e82010-06-11 14:21:37 -070050extern int force;
51
52#define warn(fmt, args...) do { fprintf(stderr, "warning: %s: " fmt "\n", __func__, ## args); } while (0)
Ken Sumrall2ae76632011-03-23 22:08:53 -070053#define error(fmt, args...) do { fprintf(stderr, "error: %s: " fmt "\n", __func__, ## args); if (!force) longjmp(setjmp_env, EXIT_FAILURE); } while (0)
Colin Crossa7ed4332010-12-22 23:08:15 -080054#define error_errno(s, args...) error(s ": %s", ##args, strerror(errno))
Ken Sumrall2ae76632011-03-23 22:08:53 -070055#define critical_error(fmt, args...) do { fprintf(stderr, "critical error: %s: " fmt "\n", __func__, ## args); longjmp(setjmp_env, EXIT_FAILURE); } while (0)
Colin Crossa7ed4332010-12-22 23:08:15 -080056#define critical_error_errno(s, args...) critical_error(s ": %s", ##args, strerror(errno))
Colin Crossec0a2e82010-06-11 14:21:37 -070057
58#define EXT4_SUPER_MAGIC 0xEF53
59#define EXT4_JNL_BACKUP_BLOCKS 1
60
61#define min(a, b) ((a) < (b) ? (a) : (b))
62
63#define DIV_ROUND_UP(x, y) (((x) + (y) - 1)/(y))
Colin Cross6bd2b5d2010-08-05 11:56:38 -070064#define ALIGN(x, y) ((y) * DIV_ROUND_UP((x), (y)))
Colin Crossec0a2e82010-06-11 14:21:37 -070065
66#define __le64 u64
67#define __le32 u32
68#define __le16 u16
69
70#define __be64 u64
71#define __be32 u32
72#define __be16 u16
73
74#define __u64 u64
75#define __u32 u32
76#define __u16 u16
77#define __u8 u8
78
79typedef unsigned long long u64;
Ken Sumrall435a8b62011-01-14 18:33:06 -080080typedef signed long long s64;
Colin Crossec0a2e82010-06-11 14:21:37 -070081typedef unsigned int u32;
82typedef unsigned short int u16;
83typedef unsigned char u8;
84
85struct block_group_info;
86
87struct ext2_group_desc {
88 __le32 bg_block_bitmap;
89 __le32 bg_inode_bitmap;
90 __le32 bg_inode_table;
91 __le16 bg_free_blocks_count;
92 __le16 bg_free_inodes_count;
93 __le16 bg_used_dirs_count;
94 __le16 bg_pad;
95 __le32 bg_reserved[3];
96};
97
98struct fs_info {
Ken Sumrall435a8b62011-01-14 18:33:06 -080099 s64 len; /* If set to 0, ask the block device for the size,
100 * if less than 0, reserve that much space at the
101 * end of the partition, else use the size given. */
Colin Crossec0a2e82010-06-11 14:21:37 -0700102 u32 block_size;
103 u32 blocks_per_group;
104 u32 inodes_per_group;
105 u32 inode_size;
106 u32 inodes;
107 u32 journal_blocks;
108 u16 feat_ro_compat;
109 u16 feat_compat;
110 u16 feat_incompat;
Colin Cross22742ce2010-12-22 16:01:52 -0800111 u32 bg_desc_reserve_blocks;
Colin Crossec0a2e82010-06-11 14:21:37 -0700112 const char *label;
Colin Crosse4b5ae82010-08-03 14:10:07 -0700113 u8 no_journal;
Colin Crossec0a2e82010-06-11 14:21:37 -0700114};
115
116struct fs_aux_info {
117 struct ext4_super_block *sb;
118 struct ext2_group_desc *bg_desc;
119 struct block_group_info *bgs;
120 u32 first_data_block;
121 u64 len_blocks;
122 u32 inode_table_blocks;
123 u32 groups;
124 u32 bg_desc_blocks;
Colin Crossec0a2e82010-06-11 14:21:37 -0700125 u32 default_i_flags;
126 u32 blocks_per_ind;
127 u32 blocks_per_dind;
128 u32 blocks_per_tind;
129};
130
131extern struct fs_info info;
132extern struct fs_aux_info aux_info;
133
Ken Sumrall2ae76632011-03-23 22:08:53 -0700134extern jmp_buf setjmp_env;
135
Colin Crossec0a2e82010-06-11 14:21:37 -0700136static inline int log_2(int j)
137{
138 int i;
139
140 for (i = 0; j > 0; i++)
141 j >>= 1;
142
143 return i - 1;
144}
145
Colin Cross881cca22010-06-20 23:57:06 -0700146int ext4_bg_has_super_block(int bg);
Colin Crossc2470652011-01-26 16:39:46 -0800147void write_ext4_image(const char *filename, int gz, int sparse, int crc,
148 int wipe);
Colin Cross881cca22010-06-20 23:57:06 -0700149void ext4_create_fs_aux_info(void);
150void ext4_free_fs_aux_info(void);
151void ext4_fill_in_sb(void);
152void ext4_create_resize_inode(void);
153void ext4_create_journal_inode(void);
154void ext4_update_free(void);
Colin Crossb7813302010-12-22 18:41:13 -0800155void ext4_queue_sb(void);
Colin Cross881cca22010-06-20 23:57:06 -0700156u64 get_file_size(const char *filename);
157u64 parse_num(const char *arg);
Colin Crossa7ed4332010-12-22 23:08:15 -0800158void ext4_parse_sb(struct ext4_super_block *sb);
Colin Cross881cca22010-06-20 23:57:06 -0700159
Colin Crossec0a2e82010-06-11 14:21:37 -0700160#endif