blob: 9b5b7c4a08360a5ee1507f81f58f51ef79c402dc [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
Doug Zongkere8d74ff2011-10-28 10:21:08 -070020#ifdef __cplusplus
21extern "C" {
22#endif
23
24#ifndef _GNU_SOURCE
Colin Cross33f96c62010-12-22 18:40:14 -080025#define _GNU_SOURCE
Doug Zongkere8d74ff2011-10-28 10:21:08 -070026#endif
Colin Cross33f96c62010-12-22 18:40:14 -080027#define _FILE_OFFSET_BITS 64
Colin Crossdc5abee2012-04-23 23:20:48 -070028#define _LARGEFILE64_SOURCE 1
Colin Cross33f96c62010-12-22 18:40:14 -080029#include <sys/types.h>
30#include <unistd.h>
31
Colin Crossec0a2e82010-06-11 14:21:37 -070032#include <sys/types.h>
33#include <errno.h>
34#include <stdarg.h>
35#include <stdio.h>
36#include <stdlib.h>
37#include <string.h>
Ken Sumrall2ae76632011-03-23 22:08:53 -070038#include <setjmp.h>
Nick Kralevich4df62f32013-02-07 14:21:34 -080039#include <stdint.h>
Colin Crossec0a2e82010-06-11 14:21:37 -070040
Colin Cross33f96c62010-12-22 18:40:14 -080041#if defined(__APPLE__) && defined(__MACH__)
42#define lseek64 lseek
Colin Crossfe4a0312011-01-05 18:05:34 -080043#define ftruncate64 ftruncate
44#define mmap64 mmap
Colin Cross33f96c62010-12-22 18:40:14 -080045#define off64_t off_t
46#endif
47
Colin Cross9a2b60b2014-01-23 13:13:02 -080048#include "ext4_sb.h"
Colin Cross7900c772014-01-23 12:53:30 -080049
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
Colin Crossec0a2e82010-06-11 14:21:37 -070058#define EXT4_JNL_BACKUP_BLOCKS 1
59
Raphael Moll4605b3f2012-02-03 23:02:33 -080060#ifndef min /* already defined by windows.h */
Colin Crossec0a2e82010-06-11 14:21:37 -070061#define min(a, b) ((a) < (b) ? (a) : (b))
Raphael Moll4605b3f2012-02-03 23:02:33 -080062#endif
Colin Crossec0a2e82010-06-11 14:21:37 -070063
64#define DIV_ROUND_UP(x, y) (((x) + (y) - 1)/(y))
Colin Cross6bd2b5d2010-08-05 11:56:38 -070065#define ALIGN(x, y) ((y) * DIV_ROUND_UP((x), (y)))
Colin Crossec0a2e82010-06-11 14:21:37 -070066
Nick Kralevich4df62f32013-02-07 14:21:34 -080067/* XXX */
68#define cpu_to_le32(x) (x)
69#define cpu_to_le16(x) (x)
70#define le32_to_cpu(x) (x)
71#define le16_to_cpu(x) (x)
72
Colin Crossec0a2e82010-06-11 14:21:37 -070073typedef unsigned long long u64;
Ken Sumrall435a8b62011-01-14 18:33:06 -080074typedef signed long long s64;
Colin Crossec0a2e82010-06-11 14:21:37 -070075typedef unsigned int u32;
76typedef unsigned short int u16;
77typedef unsigned char u8;
78
79struct block_group_info;
Nick Kralevich4df62f32013-02-07 14:21:34 -080080struct xattr_list_element;
Colin Crossec0a2e82010-06-11 14:21:37 -070081
82struct ext2_group_desc {
Colin Cross7900c772014-01-23 12:53:30 -080083 u32 bg_block_bitmap;
84 u32 bg_inode_bitmap;
85 u32 bg_inode_table;
86 u16 bg_free_blocks_count;
87 u16 bg_free_inodes_count;
88 u16 bg_used_dirs_count;
89 u16 bg_flags;
90 u32 bg_reserved[2];
91 u16 bg_reserved16;
92 u16 bg_checksum;
Colin Crossec0a2e82010-06-11 14:21:37 -070093};
94
Colin Crossec0a2e82010-06-11 14:21:37 -070095struct fs_aux_info {
96 struct ext4_super_block *sb;
Ken Sumrall107a9f12011-06-29 20:28:30 -070097 struct ext4_super_block **backup_sb;
Colin Crossec0a2e82010-06-11 14:21:37 -070098 struct ext2_group_desc *bg_desc;
99 struct block_group_info *bgs;
Nick Kralevich4df62f32013-02-07 14:21:34 -0800100 struct xattr_list_element *xattrs;
Colin Crossec0a2e82010-06-11 14:21:37 -0700101 u32 first_data_block;
102 u64 len_blocks;
103 u32 inode_table_blocks;
104 u32 groups;
105 u32 bg_desc_blocks;
Colin Crossec0a2e82010-06-11 14:21:37 -0700106 u32 default_i_flags;
107 u32 blocks_per_ind;
108 u32 blocks_per_dind;
109 u32 blocks_per_tind;
110};
111
112extern struct fs_info info;
113extern struct fs_aux_info aux_info;
Colin Cross782879a2014-01-23 13:08:16 -0800114extern struct sparse_file *ext4_sparse_file;
Colin Crossec0a2e82010-06-11 14:21:37 -0700115
Ken Sumrall2ae76632011-03-23 22:08:53 -0700116extern jmp_buf setjmp_env;
117
Colin Crossec0a2e82010-06-11 14:21:37 -0700118static inline int log_2(int j)
119{
120 int i;
121
122 for (i = 0; j > 0; i++)
123 j >>= 1;
124
125 return i - 1;
126}
127
Colin Cross881cca22010-06-20 23:57:06 -0700128int ext4_bg_has_super_block(int bg);
Colin Crossdc5abee2012-04-23 23:20:48 -0700129void write_ext4_image(int fd, int gz, int sparse, int crc);
Colin Cross881cca22010-06-20 23:57:06 -0700130void ext4_create_fs_aux_info(void);
131void ext4_free_fs_aux_info(void);
132void ext4_fill_in_sb(void);
133void ext4_create_resize_inode(void);
134void ext4_create_journal_inode(void);
135void ext4_update_free(void);
Colin Crossb7813302010-12-22 18:41:13 -0800136void ext4_queue_sb(void);
Szymon Starzyckife87e112013-07-24 12:34:50 -0700137u64 get_block_device_size(int fd);
Anatol Pomazau0349bd92012-01-11 15:12:27 -0800138u64 get_file_size(int fd);
Colin Cross881cca22010-06-20 23:57:06 -0700139u64 parse_num(const char *arg);
Colin Cross9a2b60b2014-01-23 13:13:02 -0800140void ext4_parse_sb_info(struct ext4_super_block *sb);
Colin Cross56497f22013-02-04 00:44:55 -0800141u16 ext4_crc16(u16 crc_in, const void *buf, int size);
Colin Cross881cca22010-06-20 23:57:06 -0700142
Colin Cross96529862013-01-23 15:38:57 -0800143typedef void (*fs_config_func_t)(const char *path, int dir, unsigned *uid, unsigned *gid,
Nick Kralevich4df62f32013-02-07 14:21:34 -0800144 unsigned *mode, uint64_t *capabilities);
Colin Cross96529862013-01-23 15:38:57 -0800145
146struct selabel_handle;
147
148int make_ext4fs_internal(int fd, const char *directory,
149 const char *mountpoint, fs_config_func_t fs_config_func, int gzip,
Colin Cross56497f22013-02-04 00:44:55 -0800150 int sparse, int crc, int wipe,
William Roberts20573702013-01-17 13:24:27 -0800151 struct selabel_handle *sehnd, int verbose);
Colin Cross96529862013-01-23 15:38:57 -0800152
Doug Zongkere8d74ff2011-10-28 10:21:08 -0700153#ifdef __cplusplus
154}
155#endif
156
Colin Crossec0a2e82010-06-11 14:21:37 -0700157#endif