/*-1* SPDX-License-Identifier: BSD-2-Clause2*3* Copyright (c) 2012, 2010 Zheng Liu <[email protected]>4* All rights reserved.5*6* Redistribution and use in source and binary forms, with or without7* modification, are permitted provided that the following conditions8* are met:9* 1. Redistributions of source code must retain the above copyright10* notice, this list of conditions and the following disclaimer.11* 2. Redistributions in binary form must reproduce the above copyright12* notice, this list of conditions and the following disclaimer in the13* documentation and/or other materials provided with the distribution.14*15* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND16* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE17* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE18* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE19* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL20* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS21* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)22* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT23* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY24* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF25* SUCH DAMAGE.26*/27#ifndef _FS_EXT2FS_EXT2_EXTENTS_H_28#define _FS_EXT2FS_EXT2_EXTENTS_H_2930#include <sys/types.h>3132#define EXT4_EXT_MAGIC 0xf30a33#define EXT4_MAX_BLOCKS 0xffffffff34#define EXT_INIT_MAX_LEN (1UL << 15)35#define EXT4_MAX_LEN (EXT_INIT_MAX_LEN - 1)36#define EXT4_EXT_DEPTH_MAX 53738#define EXT4_EXT_CACHE_NO 039#define EXT4_EXT_CACHE_GAP 140#define EXT4_EXT_CACHE_IN 24142/*43* Ext4 extent tail with csum44*/45struct ext4_extent_tail {46uint32_t et_checksum; /* crc32c(uuid+inum+extent_block) */47};4849/*50* Ext4 file system extent on disk.51*/52struct ext4_extent {53uint32_t e_blk; /* first logical block */54uint16_t e_len; /* number of blocks */55uint16_t e_start_hi; /* high 16 bits of physical block */56uint32_t e_start_lo; /* low 32 bits of physical block */57};5859/*60* Extent index on disk.61*/62struct ext4_extent_index {63uint32_t ei_blk; /* indexes logical blocks */64uint32_t ei_leaf_lo; /* points to physical block of the65* next level */66uint16_t ei_leaf_hi; /* high 16 bits of physical block */67uint16_t ei_unused;68};6970/*71* Extent tree header.72*/73struct ext4_extent_header {74uint16_t eh_magic; /* magic number: 0xf30a */75uint16_t eh_ecount; /* number of valid entries */76uint16_t eh_max; /* capacity of store in entries */77uint16_t eh_depth; /* the depth of extent tree */78uint32_t eh_gen; /* generation of extent tree */79};8081/*82* Save cached extent.83*/84struct ext4_extent_cache {85daddr_t ec_start; /* extent start */86uint32_t ec_blk; /* logical block */87uint32_t ec_len;88uint32_t ec_type;89};9091/*92* Save path to some extent.93*/94struct ext4_extent_path {95int index_count;96uint16_t ep_depth;97uint64_t ep_blk;98char *ep_data;99struct ext4_extent *ep_ext;100struct ext4_extent_index *ep_index;101struct ext4_extent_header *ep_header;102};103104#define EXT_FIRST_EXTENT(hdr) ((struct ext4_extent *)(((char *)(hdr)) + \105sizeof(struct ext4_extent_header)))106#define EXT_FIRST_INDEX(hdr) ((struct ext4_extent_index *)(((char *)(hdr)) + \107sizeof(struct ext4_extent_header)))108#define EXT_LAST_EXTENT(hdr) (EXT_FIRST_EXTENT((hdr)) + le16toh((hdr)->eh_ecount) - 1)109#define EXT_LAST_INDEX(hdr) (EXT_FIRST_INDEX((hdr)) + le16toh((hdr)->eh_ecount) - 1)110#define EXT4_EXTENT_TAIL_OFFSET(hdr) (sizeof(struct ext4_extent_header) + \111(sizeof(struct ext4_extent) * le16toh((hdr)->eh_max)))112#define EXT_HAS_FREE_INDEX(path) \113(le16toh((path)->ep_header->eh_ecount) < le16toh((path)->ep_header->eh_max))114#define EXT_MAX_EXTENT(hdr) (EXT_FIRST_EXTENT(hdr) + le16toh((hdr)->eh_max) - 1)115#define EXT_MAX_INDEX(hdr) (EXT_FIRST_INDEX((hdr)) + le16toh((hdr)->eh_max) - 1)116117struct inode;118struct m_ext2fs;119void ext4_ext_tree_init(struct inode *ip);120int ext4_ext_in_cache(struct inode *, daddr_t, struct ext4_extent *);121void ext4_ext_put_cache(struct inode *, struct ext4_extent *, int);122int ext4_ext_find_extent(struct inode *, daddr_t, struct ext4_extent_path **);123void ext4_ext_path_free(struct ext4_extent_path *path);124int ext4_ext_remove_space(struct inode *ip, off_t length, int flags,125struct ucred *cred, struct thread *td);126int ext4_ext_get_blocks(struct inode *ip, int64_t iblock,127unsigned long max_blocks, struct ucred *cred, struct buf **bpp,128int *allocate, daddr_t *);129#ifdef EXT2FS_PRINT_EXTENTS130int ext4_ext_walk(struct inode *ip);131#endif132133#endif /* !_FS_EXT2FS_EXT2_EXTENTS_H_ */134135136