Home | History | Annotate | Download | only in ext4_utils
      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
     21 extern "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 
     40 #if defined(__APPLE__) && defined(__MACH__)
     41 #define lseek64 lseek
     42 #define ftruncate64 ftruncate
     43 #define mmap64 mmap
     44 #define off64_t off_t
     45 #endif
     46 
     47 #ifdef __BIONIC__
     48 extern void*  __mmap2(void *, size_t, int, int, int, off_t);
     49 static inline void *mmap64(void *addr, size_t length, int prot, int flags,
     50         int fd, off64_t offset)
     51 {
     52     return __mmap2(addr, length, prot, flags, fd, offset >> 12);
     53 }
     54 #endif
     55 
     56 extern int force;
     57 
     58 #define warn(fmt, args...) do { fprintf(stderr, "warning: %s: " fmt "\n", __func__, ## args); } while (0)
     59 #define error(fmt, args...) do { fprintf(stderr, "error: %s: " fmt "\n", __func__, ## args); if (!force) longjmp(setjmp_env, EXIT_FAILURE); } while (0)
     60 #define error_errno(s, args...) error(s ": %s", ##args, strerror(errno))
     61 #define critical_error(fmt, args...) do { fprintf(stderr, "critical error: %s: " fmt "\n", __func__, ## args); longjmp(setjmp_env, EXIT_FAILURE); } while (0)
     62 #define critical_error_errno(s, args...) critical_error(s ": %s", ##args, strerror(errno))
     63 
     64 #define EXT4_SUPER_MAGIC 0xEF53
     65 #define EXT4_JNL_BACKUP_BLOCKS 1
     66 
     67 #ifndef min /* already defined by windows.h */
     68 #define min(a, b) ((a) < (b) ? (a) : (b))
     69 #endif
     70 
     71 #define DIV_ROUND_UP(x, y) (((x) + (y) - 1)/(y))
     72 #define ALIGN(x, y) ((y) * DIV_ROUND_UP((x), (y)))
     73 
     74 #define __le64 u64
     75 #define __le32 u32
     76 #define __le16 u16
     77 
     78 #define __be64 u64
     79 #define __be32 u32
     80 #define __be16 u16
     81 
     82 #define __u64 u64
     83 #define __u32 u32
     84 #define __u16 u16
     85 #define __u8 u8
     86 
     87 typedef unsigned long long u64;
     88 typedef signed long long s64;
     89 typedef unsigned int u32;
     90 typedef unsigned short int u16;
     91 typedef unsigned char u8;
     92 
     93 struct block_group_info;
     94 
     95 struct ext2_group_desc {
     96 	__le32 bg_block_bitmap;
     97 	__le32 bg_inode_bitmap;
     98 	__le32 bg_inode_table;
     99 	__le16 bg_free_blocks_count;
    100 	__le16 bg_free_inodes_count;
    101 	__le16 bg_used_dirs_count;
    102 	__le16 bg_pad;
    103 	__le32 bg_reserved[3];
    104 };
    105 
    106 struct fs_info {
    107 	s64 len;	/* If set to 0, ask the block device for the size,
    108 			 * if less than 0, reserve that much space at the
    109 			 * end of the partition, else use the size given. */
    110 	u32 block_size;
    111 	u32 blocks_per_group;
    112 	u32 inodes_per_group;
    113 	u32 inode_size;
    114 	u32 inodes;
    115 	u32 journal_blocks;
    116 	u16 feat_ro_compat;
    117 	u16 feat_compat;
    118 	u16 feat_incompat;
    119 	u32 bg_desc_reserve_blocks;
    120 	const char *label;
    121 	u8 no_journal;
    122 
    123 	struct sparse_file *sparse_file;
    124 };
    125 
    126 struct fs_aux_info {
    127 	struct ext4_super_block *sb;
    128 	struct ext4_super_block **backup_sb;
    129 	struct ext2_group_desc *bg_desc;
    130 	struct block_group_info *bgs;
    131 	u32 first_data_block;
    132 	u64 len_blocks;
    133 	u32 inode_table_blocks;
    134 	u32 groups;
    135 	u32 bg_desc_blocks;
    136 	u32 default_i_flags;
    137 	u32 blocks_per_ind;
    138 	u32 blocks_per_dind;
    139 	u32 blocks_per_tind;
    140 };
    141 
    142 extern struct fs_info info;
    143 extern struct fs_aux_info aux_info;
    144 
    145 extern jmp_buf setjmp_env;
    146 
    147 static inline int log_2(int j)
    148 {
    149 	int i;
    150 
    151 	for (i = 0; j > 0; i++)
    152 		j >>= 1;
    153 
    154 	return i - 1;
    155 }
    156 
    157 int ext4_bg_has_super_block(int bg);
    158 void write_ext4_image(int fd, int gz, int sparse, int crc);
    159 void ext4_create_fs_aux_info(void);
    160 void ext4_free_fs_aux_info(void);
    161 void ext4_fill_in_sb(void);
    162 void ext4_create_resize_inode(void);
    163 void ext4_create_journal_inode(void);
    164 void ext4_update_free(void);
    165 void ext4_queue_sb(void);
    166 u64 get_file_size(int fd);
    167 u64 parse_num(const char *arg);
    168 void ext4_parse_sb(struct ext4_super_block *sb);
    169 
    170 #ifdef __cplusplus
    171 }
    172 #endif
    173 
    174 #endif
    175