ext4_utils.h revision d43cc68c4d4053f8b9855462e428b0749c088a17
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#ifdef __cplusplus 21extern "C" { 22#endif 23 24#ifndef _GNU_SOURCE 25#define _GNU_SOURCE 26#endif 27#define _FILE_OFFSET_BITS 64 28#define _LARGEFILE64_SOURCE 1 29#include <sys/types.h> 30#include <unistd.h> 31 32#include <sys/types.h> 33#include <errno.h> 34#include <stdarg.h> 35#include <stdio.h> 36#include <stdlib.h> 37#include <string.h> 38#include <setjmp.h> 39#include <stdint.h> 40 41#if defined(__APPLE__) && defined(__MACH__) 42#define lseek64 lseek 43#define ftruncate64 ftruncate 44#define mmap64 mmap 45#define off64_t off_t 46#endif 47 48#include "ext4_sb.h" 49 50extern int force; 51 52#define warn(fmt, args...) do { fprintf(stderr, "warning: %s: " fmt "\n", __func__, ## args); } while (0) 53#define error(fmt, args...) do { fprintf(stderr, "error: %s: " fmt "\n", __func__, ## args); if (!force) longjmp(setjmp_env, EXIT_FAILURE); } while (0) 54#define error_errno(s, args...) error(s ": %s", ##args, strerror(errno)) 55#define critical_error(fmt, args...) do { fprintf(stderr, "critical error: %s: " fmt "\n", __func__, ## args); longjmp(setjmp_env, EXIT_FAILURE); } while (0) 56#define critical_error_errno(s, args...) critical_error(s ": %s", ##args, strerror(errno)) 57 58#define EXT4_JNL_BACKUP_BLOCKS 1 59 60#ifndef __cplusplus 61#ifndef min /* already defined by windows.h */ 62#define min(a, b) ((a) < (b) ? (a) : (b)) 63#endif 64#endif 65 66#define DIV_ROUND_UP(x, y) (((x) + (y) - 1)/(y)) 67#define EXT4_ALIGN(x, y) ((y) * DIV_ROUND_UP((x), (y))) 68 69/* XXX */ 70#define cpu_to_le32(x) (x) 71#define cpu_to_le16(x) (x) 72#define le32_to_cpu(x) (x) 73#define le16_to_cpu(x) (x) 74 75#ifdef __LP64__ 76typedef unsigned long u64; 77typedef signed long s64; 78#else 79typedef unsigned long long u64; 80typedef signed long long s64; 81#endif 82typedef unsigned int u32; 83typedef unsigned short int u16; 84typedef unsigned char u8; 85 86struct block_group_info; 87struct xattr_list_element; 88 89struct ext2_group_desc { 90 u32 bg_block_bitmap; 91 u32 bg_inode_bitmap; 92 u32 bg_inode_table; 93 u16 bg_free_blocks_count; 94 u16 bg_free_inodes_count; 95 u16 bg_used_dirs_count; 96 u16 bg_flags; 97 u32 bg_reserved[2]; 98 u16 bg_reserved16; 99 u16 bg_checksum; 100}; 101 102struct fs_aux_info { 103 struct ext4_super_block *sb; 104 struct ext4_super_block *sb_block; 105 struct ext4_super_block *sb_zero; 106 struct ext4_super_block **backup_sb; 107 struct ext2_group_desc *bg_desc; 108 struct block_group_info *bgs; 109 struct xattr_list_element *xattrs; 110 u32 first_data_block; 111 u64 len_blocks; 112 u32 inode_table_blocks; 113 u32 groups; 114 u32 bg_desc_blocks; 115 u32 default_i_flags; 116 u32 blocks_per_ind; 117 u32 blocks_per_dind; 118 u32 blocks_per_tind; 119}; 120 121extern struct fs_info info; 122extern struct fs_aux_info aux_info; 123extern struct sparse_file *ext4_sparse_file; 124extern struct block_allocation *base_fs_allocations; 125 126extern jmp_buf setjmp_env; 127 128static inline int log_2(int j) 129{ 130 int i; 131 132 for (i = 0; j > 0; i++) 133 j >>= 1; 134 135 return i - 1; 136} 137 138int bitmap_get_bit(u8 *bitmap, u32 bit); 139void bitmap_clear_bit(u8 *bitmap, u32 bit); 140int ext4_bg_has_super_block(int bg); 141void read_sb(int fd, struct ext4_super_block *sb); 142void write_sb(int fd, unsigned long long offset, struct ext4_super_block *sb); 143void write_ext4_image(int fd, int gz, int sparse, int crc); 144void ext4_create_fs_aux_info(void); 145void ext4_free_fs_aux_info(void); 146void ext4_fill_in_sb(int real_uuid); 147void ext4_create_resize_inode(void); 148void ext4_create_journal_inode(void); 149void ext4_update_free(void); 150void ext4_queue_sb(u64 start_block, struct ext4_super_block *sb); 151u64 get_block_device_size(int fd); 152int is_block_device_fd(int fd); 153u64 get_file_size(int fd); 154u64 parse_num(const char *arg); 155void ext4_parse_sb_info(struct ext4_super_block *sb); 156u16 ext4_crc16(u16 crc_in, const void *buf, int size); 157 158typedef void (*fs_config_func_t)(const char *path, int dir, const char *target_out_path, 159 unsigned *uid, unsigned *gid, unsigned *mode, uint64_t *capabilities); 160 161struct selabel_handle; 162 163int make_ext4fs_internal(int fd, const char *directory, const char *_target_out_directory, 164 const char *mountpoint, fs_config_func_t fs_config_func, int gzip, 165 int sparse, int crc, int wipe, int real_uuid, 166 struct selabel_handle *sehnd, int verbose, time_t fixed_time, 167 FILE* block_list_file, FILE* base_alloc_file_in, FILE* base_alloc_file_out); 168 169int read_ext(int fd, int verbose); 170 171#ifdef __cplusplus 172} 173#endif 174 175#endif 176