blob: 499753fdf9df030e028e40f53f47da722e1aac61 [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))
Paul Lawrence40ce87a2014-03-03 10:51:58 -080065#define EXT4_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 Crossaf072342014-01-23 13:19:27 -080073#ifdef __LP64__
74typedef unsigned long u64;
75typedef signed long s64;
76#else
Colin Crossec0a2e82010-06-11 14:21:37 -070077typedef unsigned long long u64;
Ken Sumrall435a8b62011-01-14 18:33:06 -080078typedef signed long long s64;
Colin Crossaf072342014-01-23 13:19:27 -080079#endif
Colin Crossec0a2e82010-06-11 14:21:37 -070080typedef unsigned int u32;
81typedef unsigned short int u16;
82typedef unsigned char u8;
83
84struct block_group_info;
Nick Kralevich4df62f32013-02-07 14:21:34 -080085struct xattr_list_element;
Colin Crossec0a2e82010-06-11 14:21:37 -070086
87struct ext2_group_desc {
Colin Cross7900c772014-01-23 12:53:30 -080088 u32 bg_block_bitmap;
89 u32 bg_inode_bitmap;
90 u32 bg_inode_table;
91 u16 bg_free_blocks_count;
92 u16 bg_free_inodes_count;
93 u16 bg_used_dirs_count;
94 u16 bg_flags;
95 u32 bg_reserved[2];
96 u16 bg_reserved16;
97 u16 bg_checksum;
Colin Crossec0a2e82010-06-11 14:21:37 -070098};
99
100struct fs_aux_info {
101 struct ext4_super_block *sb;
Ken Sumrall107a9f12011-06-29 20:28:30 -0700102 struct ext4_super_block **backup_sb;
Colin Crossec0a2e82010-06-11 14:21:37 -0700103 struct ext2_group_desc *bg_desc;
104 struct block_group_info *bgs;
Nick Kralevich4df62f32013-02-07 14:21:34 -0800105 struct xattr_list_element *xattrs;
Colin Crossec0a2e82010-06-11 14:21:37 -0700106 u32 first_data_block;
107 u64 len_blocks;
108 u32 inode_table_blocks;
109 u32 groups;
110 u32 bg_desc_blocks;
Colin Crossec0a2e82010-06-11 14:21:37 -0700111 u32 default_i_flags;
112 u32 blocks_per_ind;
113 u32 blocks_per_dind;
114 u32 blocks_per_tind;
115};
116
117extern struct fs_info info;
118extern struct fs_aux_info aux_info;
Colin Cross782879a2014-01-23 13:08:16 -0800119extern struct sparse_file *ext4_sparse_file;
Colin Crossec0a2e82010-06-11 14:21:37 -0700120
Ken Sumrall2ae76632011-03-23 22:08:53 -0700121extern jmp_buf setjmp_env;
122
Colin Crossec0a2e82010-06-11 14:21:37 -0700123static inline int log_2(int j)
124{
125 int i;
126
127 for (i = 0; j > 0; i++)
128 j >>= 1;
129
130 return i - 1;
131}
132
Paul Lawrenceee2bba62014-01-21 08:26:21 -0800133int bitmap_get_bit(u8 *bitmap, u32 bit);
134void bitmap_clear_bit(u8 *bitmap, u32 bit);
Colin Cross881cca22010-06-20 23:57:06 -0700135int ext4_bg_has_super_block(int bg);
Paul Lawrenceee2bba62014-01-21 08:26:21 -0800136void read_sb(int fd, struct ext4_super_block *sb);
137void write_sb(int fd, unsigned long long offset, struct ext4_super_block *sb);
Colin Crossdc5abee2012-04-23 23:20:48 -0700138void write_ext4_image(int fd, int gz, int sparse, int crc);
Colin Cross881cca22010-06-20 23:57:06 -0700139void ext4_create_fs_aux_info(void);
140void ext4_free_fs_aux_info(void);
141void ext4_fill_in_sb(void);
142void ext4_create_resize_inode(void);
143void ext4_create_journal_inode(void);
144void ext4_update_free(void);
Colin Crossb7813302010-12-22 18:41:13 -0800145void ext4_queue_sb(void);
Szymon Starzyckife87e112013-07-24 12:34:50 -0700146u64 get_block_device_size(int fd);
David 'Digit' Turnereb5fcc32014-06-12 20:54:50 +0200147int is_block_device_fd(int fd);
Anatol Pomazau0349bd92012-01-11 15:12:27 -0800148u64 get_file_size(int fd);
Colin Cross881cca22010-06-20 23:57:06 -0700149u64 parse_num(const char *arg);
Colin Cross9a2b60b2014-01-23 13:13:02 -0800150void ext4_parse_sb_info(struct ext4_super_block *sb);
Colin Cross56497f22013-02-04 00:44:55 -0800151u16 ext4_crc16(u16 crc_in, const void *buf, int size);
Colin Cross881cca22010-06-20 23:57:06 -0700152
Colin Cross96529862013-01-23 15:38:57 -0800153typedef void (*fs_config_func_t)(const char *path, int dir, unsigned *uid, unsigned *gid,
Doug Zongker95266802013-12-05 15:51:28 -0800154 unsigned *mode, uint64_t *capabilities);
Colin Cross96529862013-01-23 15:38:57 -0800155
156struct selabel_handle;
157
158int make_ext4fs_internal(int fd, const char *directory,
Doug Zongker95266802013-12-05 15:51:28 -0800159 const char *mountpoint, fs_config_func_t fs_config_func, int gzip,
160 int sparse, int crc, int wipe,
Doug Zongkerbec598e2014-08-12 11:35:37 -0700161 struct selabel_handle *sehnd, int verbose, time_t fixed_time,
162 FILE* block_list_file);
Colin Cross96529862013-01-23 15:38:57 -0800163
Paul Lawrenceee2bba62014-01-21 08:26:21 -0800164int read_ext(int fd, int verbose);
165
Doug Zongkere8d74ff2011-10-28 10:21:08 -0700166#ifdef __cplusplus
167}
168#endif
169
Colin Crossec0a2e82010-06-11 14:21:37 -0700170#endif