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 #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 
     50 extern 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__
     76 typedef unsigned long u64;
     77 typedef signed long s64;
     78 
     79 #define PRIext4u64 "lu"
     80 #else
     81 typedef unsigned long long u64;
     82 typedef signed long long s64;
     83 
     84 #define PRIext4u64 PRIu64
     85 #endif
     86 typedef unsigned int u32;
     87 typedef unsigned short int u16;
     88 typedef unsigned char u8;
     89 
     90 struct block_group_info;
     91 struct xattr_list_element;
     92 
     93 struct ext2_group_desc {
     94 	u32 bg_block_bitmap;
     95 	u32 bg_inode_bitmap;
     96 	u32 bg_inode_table;
     97 	u16 bg_free_blocks_count;
     98 	u16 bg_free_inodes_count;
     99 	u16 bg_used_dirs_count;
    100 	u16 bg_flags;
    101 	u32 bg_reserved[2];
    102 	u16 bg_reserved16;
    103 	u16 bg_checksum;
    104 };
    105 
    106 struct fs_aux_info {
    107 	struct ext4_super_block *sb;
    108 	struct ext4_super_block *sb_block;
    109 	struct ext4_super_block *sb_zero;
    110 	struct ext4_super_block **backup_sb;
    111 	struct ext2_group_desc *bg_desc;
    112 	struct block_group_info *bgs;
    113 	struct xattr_list_element *xattrs;
    114 	u32 first_data_block;
    115 	u64 len_blocks;
    116 	u32 inode_table_blocks;
    117 	u32 groups;
    118 	u32 bg_desc_blocks;
    119 	u32 default_i_flags;
    120 	u64 blocks_per_ind;
    121 	u64 blocks_per_dind;
    122 	u64 blocks_per_tind;
    123 };
    124 
    125 extern struct fs_info info;
    126 extern struct fs_aux_info aux_info;
    127 
    128 extern jmp_buf setjmp_env;
    129 
    130 int bitmap_get_bit(u8 *bitmap, u32 bit);	// vold
    131 u64 get_block_device_size(int fd);		// recovery
    132 int is_block_device_fd(int fd);			// wipe.c
    133 u64 get_file_size(int fd);			// fs_mgr
    134 
    135 int read_ext(int fd, int verbose);		// vold
    136 
    137 #ifdef __cplusplus
    138 }
    139 #endif
    140 
    141 #endif
    142