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 28dc5abeee1e6fc4827ee0d5ece12aaed2dd56f4c7Colin Cross#define _LARGEFILE64_SOURCE 1 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> 394df62f342dbbe2f5cca831ce789dc0426d32ec03Nick Kralevich#include <stdint.h> 40ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross 4133f96c66e9a1f2e266a75e5e84c091dffa6ef118Colin Cross#if defined(__APPLE__) && defined(__MACH__) 4233f96c66e9a1f2e266a75e5e84c091dffa6ef118Colin Cross#define lseek64 lseek 43fe4a03182b5b17d333511c72406f926a791345d3Colin Cross#define ftruncate64 ftruncate 44fe4a03182b5b17d333511c72406f926a791345d3Colin Cross#define mmap64 mmap 4533f96c66e9a1f2e266a75e5e84c091dffa6ef118Colin Cross#define off64_t off_t 4633f96c66e9a1f2e266a75e5e84c091dffa6ef118Colin Cross#endif 4733f96c66e9a1f2e266a75e5e84c091dffa6ef118Colin Cross 489a2b60b28d866b94b86c7ceb3a9004d28e20b483Colin Cross#include "ext4_sb.h" 497900c773815d062deb266f744f95aa76b3573fa3Colin Cross 50ec0a2e83dc66d67addeb90e83144187691852a3eColin Crossextern int force; 51ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross 52ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross#define warn(fmt, args...) do { fprintf(stderr, "warning: %s: " fmt "\n", __func__, ## args); } while (0) 532ae7663e1e064000356ee9e49ebd08bdddc545f9Ken Sumrall#define error(fmt, args...) do { fprintf(stderr, "error: %s: " fmt "\n", __func__, ## args); if (!force) longjmp(setjmp_env, EXIT_FAILURE); } while (0) 54a7ed433f2dc0116627a93b18fbb260f0665ca0cbColin Cross#define error_errno(s, args...) error(s ": %s", ##args, strerror(errno)) 552ae7663e1e064000356ee9e49ebd08bdddc545f9Ken Sumrall#define critical_error(fmt, args...) do { fprintf(stderr, "critical error: %s: " fmt "\n", __func__, ## args); longjmp(setjmp_env, EXIT_FAILURE); } while (0) 56a7ed433f2dc0116627a93b18fbb260f0665ca0cbColin Cross#define critical_error_errno(s, args...) critical_error(s ": %s", ##args, strerror(errno)) 57ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross 58ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross#define EXT4_JNL_BACKUP_BLOCKS 1 59ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross 604605b3fb8a00fa37f617a8d0fe3a095d0503a845Raphael Moll#ifndef min /* already defined by windows.h */ 61ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross#define min(a, b) ((a) < (b) ? (a) : (b)) 624605b3fb8a00fa37f617a8d0fe3a095d0503a845Raphael Moll#endif 63ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross 64ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross#define DIV_ROUND_UP(x, y) (((x) + (y) - 1)/(y)) 6540ce87a70a064eeb462d3c3935422918c1f6114ePaul Lawrence#define EXT4_ALIGN(x, y) ((y) * DIV_ROUND_UP((x), (y))) 66ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross 674df62f342dbbe2f5cca831ce789dc0426d32ec03Nick Kralevich/* XXX */ 684df62f342dbbe2f5cca831ce789dc0426d32ec03Nick Kralevich#define cpu_to_le32(x) (x) 694df62f342dbbe2f5cca831ce789dc0426d32ec03Nick Kralevich#define cpu_to_le16(x) (x) 704df62f342dbbe2f5cca831ce789dc0426d32ec03Nick Kralevich#define le32_to_cpu(x) (x) 714df62f342dbbe2f5cca831ce789dc0426d32ec03Nick Kralevich#define le16_to_cpu(x) (x) 724df62f342dbbe2f5cca831ce789dc0426d32ec03Nick Kralevich 73af0723439af552c170425416ee8e35f4f20bbe67Colin Cross#ifdef __LP64__ 74af0723439af552c170425416ee8e35f4f20bbe67Colin Crosstypedef unsigned long u64; 75af0723439af552c170425416ee8e35f4f20bbe67Colin Crosstypedef signed long s64; 76af0723439af552c170425416ee8e35f4f20bbe67Colin Cross#else 77ec0a2e83dc66d67addeb90e83144187691852a3eColin Crosstypedef unsigned long long u64; 78435a8b61e925e3efb22fce08612efe210e83f791Ken Sumralltypedef signed long long s64; 79af0723439af552c170425416ee8e35f4f20bbe67Colin Cross#endif 80ec0a2e83dc66d67addeb90e83144187691852a3eColin Crosstypedef unsigned int u32; 81ec0a2e83dc66d67addeb90e83144187691852a3eColin Crosstypedef unsigned short int u16; 82ec0a2e83dc66d67addeb90e83144187691852a3eColin Crosstypedef unsigned char u8; 83ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross 84ec0a2e83dc66d67addeb90e83144187691852a3eColin Crossstruct block_group_info; 854df62f342dbbe2f5cca831ce789dc0426d32ec03Nick Kralevichstruct xattr_list_element; 86ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross 87ec0a2e83dc66d67addeb90e83144187691852a3eColin Crossstruct ext2_group_desc { 887900c773815d062deb266f744f95aa76b3573fa3Colin Cross u32 bg_block_bitmap; 897900c773815d062deb266f744f95aa76b3573fa3Colin Cross u32 bg_inode_bitmap; 907900c773815d062deb266f744f95aa76b3573fa3Colin Cross u32 bg_inode_table; 917900c773815d062deb266f744f95aa76b3573fa3Colin Cross u16 bg_free_blocks_count; 927900c773815d062deb266f744f95aa76b3573fa3Colin Cross u16 bg_free_inodes_count; 937900c773815d062deb266f744f95aa76b3573fa3Colin Cross u16 bg_used_dirs_count; 947900c773815d062deb266f744f95aa76b3573fa3Colin Cross u16 bg_flags; 957900c773815d062deb266f744f95aa76b3573fa3Colin Cross u32 bg_reserved[2]; 967900c773815d062deb266f744f95aa76b3573fa3Colin Cross u16 bg_reserved16; 977900c773815d062deb266f744f95aa76b3573fa3Colin Cross u16 bg_checksum; 98ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross}; 99ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross 100ec0a2e83dc66d67addeb90e83144187691852a3eColin Crossstruct fs_aux_info { 101ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross struct ext4_super_block *sb; 102107a9f161babc20daf915311146b0e864d3b4157Ken Sumrall struct ext4_super_block **backup_sb; 103ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross struct ext2_group_desc *bg_desc; 104ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross struct block_group_info *bgs; 1054df62f342dbbe2f5cca831ce789dc0426d32ec03Nick Kralevich struct xattr_list_element *xattrs; 106ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross u32 first_data_block; 107ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross u64 len_blocks; 108ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross u32 inode_table_blocks; 109ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross u32 groups; 110ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross u32 bg_desc_blocks; 111ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross u32 default_i_flags; 112ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross u32 blocks_per_ind; 113ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross u32 blocks_per_dind; 114ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross u32 blocks_per_tind; 115ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross}; 116ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross 117ec0a2e83dc66d67addeb90e83144187691852a3eColin Crossextern struct fs_info info; 118ec0a2e83dc66d67addeb90e83144187691852a3eColin Crossextern struct fs_aux_info aux_info; 119782879ab61fe825835a9c6a701f91aa7d305acefColin Crossextern struct sparse_file *ext4_sparse_file; 120ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross 1212ae7663e1e064000356ee9e49ebd08bdddc545f9Ken Sumrallextern jmp_buf setjmp_env; 1222ae7663e1e064000356ee9e49ebd08bdddc545f9Ken Sumrall 123ec0a2e83dc66d67addeb90e83144187691852a3eColin Crossstatic inline int log_2(int j) 124ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross{ 125ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross int i; 126ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross 127ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross for (i = 0; j > 0; i++) 128ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross j >>= 1; 129ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross 130ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross return i - 1; 131ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross} 132ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross 133ee2bba62529ce544a60d141ca10a37407c03ab1ePaul Lawrenceint bitmap_get_bit(u8 *bitmap, u32 bit); 134ee2bba62529ce544a60d141ca10a37407c03ab1ePaul Lawrencevoid bitmap_clear_bit(u8 *bitmap, u32 bit); 135881cca2f88ddcce86483b3ba95546b5641de8c0eColin Crossint ext4_bg_has_super_block(int bg); 136ee2bba62529ce544a60d141ca10a37407c03ab1ePaul Lawrencevoid read_sb(int fd, struct ext4_super_block *sb); 137ee2bba62529ce544a60d141ca10a37407c03ab1ePaul Lawrencevoid write_sb(int fd, unsigned long long offset, struct ext4_super_block *sb); 138dc5abeee1e6fc4827ee0d5ece12aaed2dd56f4c7Colin Crossvoid write_ext4_image(int fd, int gz, int sparse, int crc); 139881cca2f88ddcce86483b3ba95546b5641de8c0eColin Crossvoid ext4_create_fs_aux_info(void); 140881cca2f88ddcce86483b3ba95546b5641de8c0eColin Crossvoid ext4_free_fs_aux_info(void); 1417538cc97e1695608123739d6ef8ff2ee1572aaecJeff Sharkeyvoid ext4_fill_in_sb(int real_uuid); 142881cca2f88ddcce86483b3ba95546b5641de8c0eColin Crossvoid ext4_create_resize_inode(void); 143881cca2f88ddcce86483b3ba95546b5641de8c0eColin Crossvoid ext4_create_journal_inode(void); 144881cca2f88ddcce86483b3ba95546b5641de8c0eColin Crossvoid ext4_update_free(void); 145b781330b1acae2e5706bbda8d81e5f7575f40e2aColin Crossvoid ext4_queue_sb(void); 146fe87e119a6ba97b7bd8e460f4e930fcef9b1f1feSzymon Starzyckiu64 get_block_device_size(int fd); 147eb5fcc3e932a8ccac1b580788a213c7782aff31bDavid 'Digit' Turnerint is_block_device_fd(int fd); 1480349bd9f14d252673a7a25767da4a80121aaaaf2Anatol Pomazauu64 get_file_size(int fd); 149881cca2f88ddcce86483b3ba95546b5641de8c0eColin Crossu64 parse_num(const char *arg); 1509a2b60b28d866b94b86c7ceb3a9004d28e20b483Colin Crossvoid ext4_parse_sb_info(struct ext4_super_block *sb); 15156497f28bd20001dd5f931208e8d948cf2f81b2fColin Crossu16 ext4_crc16(u16 crc_in, const void *buf, int size); 152881cca2f88ddcce86483b3ba95546b5641de8c0eColin Cross 153b89e81dcb9bfa707912d9e370949b250367b0998Thierry Strudeltypedef void (*fs_config_func_t)(const char *path, int dir, const char *target_out_path, 154b89e81dcb9bfa707912d9e370949b250367b0998Thierry Strudel unsigned *uid, unsigned *gid, unsigned *mode, uint64_t *capabilities); 155965298695c981ee5a67e86977a8e40a50f7392abColin Cross 156965298695c981ee5a67e86977a8e40a50f7392abColin Crossstruct selabel_handle; 157965298695c981ee5a67e86977a8e40a50f7392abColin Cross 158b89e81dcb9bfa707912d9e370949b250367b0998Thierry Strudelint make_ext4fs_internal(int fd, const char *directory, const char *_target_out_directory, 1599526680de97e2bc963a70d1fabffe165a688bb1eDoug Zongker const char *mountpoint, fs_config_func_t fs_config_func, int gzip, 1607538cc97e1695608123739d6ef8ff2ee1572aaecJeff Sharkey int sparse, int crc, int wipe, int real_uuid, 161bec598e982301bf2714d37b14e312c9845c7cc0cDoug Zongker struct selabel_handle *sehnd, int verbose, time_t fixed_time, 162bec598e982301bf2714d37b14e312c9845c7cc0cDoug Zongker FILE* block_list_file); 163965298695c981ee5a67e86977a8e40a50f7392abColin Cross 164ee2bba62529ce544a60d141ca10a37407c03ab1ePaul Lawrenceint read_ext(int fd, int verbose); 165ee2bba62529ce544a60d141ca10a37407c03ab1ePaul Lawrence 166e8d74ff5c3253403d3e5c96edc80dfc3e5f8ada8Doug Zongker#ifdef __cplusplus 167e8d74ff5c3253403d3e5c96edc80dfc3e5f8ada8Doug Zongker} 168e8d74ff5c3253403d3e5c96edc80dfc3e5f8ada8Doug Zongker#endif 169e8d74ff5c3253403d3e5c96edc80dfc3e5f8ada8Doug Zongker 170ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross#endif 171