1d7511a40SPedro F. Giffuni /*- 2*4d846d26SWarner Losh * SPDX-License-Identifier: BSD-2-Clause 3d63027b6SPedro F. Giffuni * 4d7511a40SPedro F. Giffuni * Copyright (c) 2012, 2010 Zheng Liu <lz@freebsd.org> 5d7511a40SPedro F. Giffuni * All rights reserved. 6d7511a40SPedro F. Giffuni * 7d7511a40SPedro F. Giffuni * Redistribution and use in source and binary forms, with or without 8d7511a40SPedro F. Giffuni * modification, are permitted provided that the following conditions 9d7511a40SPedro F. Giffuni * are met: 10d7511a40SPedro F. Giffuni * 1. Redistributions of source code must retain the above copyright 11d7511a40SPedro F. Giffuni * notice, this list of conditions and the following disclaimer. 12d7511a40SPedro F. Giffuni * 2. Redistributions in binary form must reproduce the above copyright 13d7511a40SPedro F. Giffuni * notice, this list of conditions and the following disclaimer in the 14d7511a40SPedro F. Giffuni * documentation and/or other materials provided with the distribution. 15d7511a40SPedro F. Giffuni * 16d7511a40SPedro F. Giffuni * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 17d7511a40SPedro F. Giffuni * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18d7511a40SPedro F. Giffuni * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19d7511a40SPedro F. Giffuni * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 20d7511a40SPedro F. Giffuni * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21d7511a40SPedro F. Giffuni * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22d7511a40SPedro F. Giffuni * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23d7511a40SPedro F. Giffuni * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24d7511a40SPedro F. Giffuni * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25d7511a40SPedro F. Giffuni * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26d7511a40SPedro F. Giffuni * SUCH DAMAGE. 27d7511a40SPedro F. Giffuni */ 28d7511a40SPedro F. Giffuni #ifndef _FS_EXT2FS_EXT2_EXTENTS_H_ 29d7511a40SPedro F. Giffuni #define _FS_EXT2FS_EXT2_EXTENTS_H_ 30d7511a40SPedro F. Giffuni 31d7511a40SPedro F. Giffuni #include <sys/types.h> 32d7511a40SPedro F. Giffuni 33d7511a40SPedro F. Giffuni #define EXT4_EXT_MAGIC 0xf30a 34b394cd1eSFedor Uporov #define EXT4_MAX_BLOCKS 0xffffffff 35b394cd1eSFedor Uporov #define EXT_INIT_MAX_LEN (1UL << 15) 36b394cd1eSFedor Uporov #define EXT4_MAX_LEN (EXT_INIT_MAX_LEN - 1) 37b394cd1eSFedor Uporov #define EXT4_EXT_DEPTH_MAX 5 38d7511a40SPedro F. Giffuni 39d7511a40SPedro F. Giffuni #define EXT4_EXT_CACHE_NO 0 40d7511a40SPedro F. Giffuni #define EXT4_EXT_CACHE_GAP 1 41d7511a40SPedro F. Giffuni #define EXT4_EXT_CACHE_IN 2 42d7511a40SPedro F. Giffuni 43d7511a40SPedro F. Giffuni /* 44512f29d1SFedor Uporov * Ext4 extent tail with csum 45512f29d1SFedor Uporov */ 46512f29d1SFedor Uporov struct ext4_extent_tail { 47512f29d1SFedor Uporov uint32_t et_checksum; /* crc32c(uuid+inum+extent_block) */ 48512f29d1SFedor Uporov }; 49512f29d1SFedor Uporov 50512f29d1SFedor Uporov /* 51d7511a40SPedro F. Giffuni * Ext4 file system extent on disk. 52d7511a40SPedro F. Giffuni */ 53d7511a40SPedro F. Giffuni struct ext4_extent { 54d7511a40SPedro F. Giffuni uint32_t e_blk; /* first logical block */ 55d7511a40SPedro F. Giffuni uint16_t e_len; /* number of blocks */ 56d7511a40SPedro F. Giffuni uint16_t e_start_hi; /* high 16 bits of physical block */ 57d7511a40SPedro F. Giffuni uint32_t e_start_lo; /* low 32 bits of physical block */ 58d7511a40SPedro F. Giffuni }; 59d7511a40SPedro F. Giffuni 60d7511a40SPedro F. Giffuni /* 61d7511a40SPedro F. Giffuni * Extent index on disk. 62d7511a40SPedro F. Giffuni */ 63d7511a40SPedro F. Giffuni struct ext4_extent_index { 64d7511a40SPedro F. Giffuni uint32_t ei_blk; /* indexes logical blocks */ 65d7511a40SPedro F. Giffuni uint32_t ei_leaf_lo; /* points to physical block of the 66d7511a40SPedro F. Giffuni * next level */ 67d7511a40SPedro F. Giffuni uint16_t ei_leaf_hi; /* high 16 bits of physical block */ 68d7511a40SPedro F. Giffuni uint16_t ei_unused; 69d7511a40SPedro F. Giffuni }; 70d7511a40SPedro F. Giffuni 71d7511a40SPedro F. Giffuni /* 72d7511a40SPedro F. Giffuni * Extent tree header. 73d7511a40SPedro F. Giffuni */ 74d7511a40SPedro F. Giffuni struct ext4_extent_header { 75d7511a40SPedro F. Giffuni uint16_t eh_magic; /* magic number: 0xf30a */ 76d7511a40SPedro F. Giffuni uint16_t eh_ecount; /* number of valid entries */ 77d7511a40SPedro F. Giffuni uint16_t eh_max; /* capacity of store in entries */ 78d7511a40SPedro F. Giffuni uint16_t eh_depth; /* the depth of extent tree */ 79d7511a40SPedro F. Giffuni uint32_t eh_gen; /* generation of extent tree */ 80d7511a40SPedro F. Giffuni }; 81d7511a40SPedro F. Giffuni 82d7511a40SPedro F. Giffuni /* 83d7511a40SPedro F. Giffuni * Save cached extent. 84d7511a40SPedro F. Giffuni */ 85d7511a40SPedro F. Giffuni struct ext4_extent_cache { 86d7511a40SPedro F. Giffuni daddr_t ec_start; /* extent start */ 87d7511a40SPedro F. Giffuni uint32_t ec_blk; /* logical block */ 88d7511a40SPedro F. Giffuni uint32_t ec_len; 89d7511a40SPedro F. Giffuni uint32_t ec_type; 90d7511a40SPedro F. Giffuni }; 91d7511a40SPedro F. Giffuni 92d7511a40SPedro F. Giffuni /* 93d7511a40SPedro F. Giffuni * Save path to some extent. 94d7511a40SPedro F. Giffuni */ 95d7511a40SPedro F. Giffuni struct ext4_extent_path { 96b394cd1eSFedor Uporov int index_count; 97d7511a40SPedro F. Giffuni uint16_t ep_depth; 98b394cd1eSFedor Uporov uint64_t ep_blk; 99b394cd1eSFedor Uporov char *ep_data; 100d7511a40SPedro F. Giffuni struct ext4_extent *ep_ext; 101d7511a40SPedro F. Giffuni struct ext4_extent_index *ep_index; 102d7511a40SPedro F. Giffuni struct ext4_extent_header *ep_header; 103d7511a40SPedro F. Giffuni }; 104d7511a40SPedro F. Giffuni 105b394cd1eSFedor Uporov #define EXT_FIRST_EXTENT(hdr) ((struct ext4_extent *)(((char *)(hdr)) + \ 106b394cd1eSFedor Uporov sizeof(struct ext4_extent_header))) 107b394cd1eSFedor Uporov #define EXT_FIRST_INDEX(hdr) ((struct ext4_extent_index *)(((char *)(hdr)) + \ 108b394cd1eSFedor Uporov sizeof(struct ext4_extent_header))) 109cd3acfe7SFedor Uporov #define EXT_LAST_EXTENT(hdr) (EXT_FIRST_EXTENT((hdr)) + le16toh((hdr)->eh_ecount) - 1) 110cd3acfe7SFedor Uporov #define EXT_LAST_INDEX(hdr) (EXT_FIRST_INDEX((hdr)) + le16toh((hdr)->eh_ecount) - 1) 111b394cd1eSFedor Uporov #define EXT4_EXTENT_TAIL_OFFSET(hdr) (sizeof(struct ext4_extent_header) + \ 112cd3acfe7SFedor Uporov (sizeof(struct ext4_extent) * le16toh((hdr)->eh_max))) 113b394cd1eSFedor Uporov #define EXT_HAS_FREE_INDEX(path) \ 114cd3acfe7SFedor Uporov (le16toh((path)->ep_header->eh_ecount) < le16toh((path)->ep_header->eh_max)) 115cd3acfe7SFedor Uporov #define EXT_MAX_EXTENT(hdr) (EXT_FIRST_EXTENT(hdr) + le16toh((hdr)->eh_max) - 1) 116cd3acfe7SFedor Uporov #define EXT_MAX_INDEX(hdr) (EXT_FIRST_INDEX((hdr)) + le16toh((hdr)->eh_max) - 1) 117b394cd1eSFedor Uporov 118d7511a40SPedro F. Giffuni struct inode; 119d7511a40SPedro F. Giffuni struct m_ext2fs; 120b394cd1eSFedor Uporov void ext4_ext_tree_init(struct inode *ip); 121d7511a40SPedro F. Giffuni int ext4_ext_in_cache(struct inode *, daddr_t, struct ext4_extent *); 122d7511a40SPedro F. Giffuni void ext4_ext_put_cache(struct inode *, struct ext4_extent *, int); 123b394cd1eSFedor Uporov int ext4_ext_find_extent(struct inode *, daddr_t, struct ext4_extent_path **); 124b394cd1eSFedor Uporov void ext4_ext_path_free(struct ext4_extent_path *path); 125b394cd1eSFedor Uporov int ext4_ext_remove_space(struct inode *ip, off_t length, int flags, 126b394cd1eSFedor Uporov struct ucred *cred, struct thread *td); 127b394cd1eSFedor Uporov int ext4_ext_get_blocks(struct inode *ip, int64_t iblock, 1283acd9182SFedor Uporov unsigned long max_blocks, struct ucred *cred, struct buf **bpp, 1293acd9182SFedor Uporov int *allocate, daddr_t *); 130ebc94b66SFedor Uporov #ifdef EXT2FS_PRINT_EXTENTS 1315679656eSFedor Uporov int ext4_ext_walk(struct inode *ip); 132b394cd1eSFedor Uporov #endif 133d7511a40SPedro F. Giffuni 134d7511a40SPedro F. Giffuni #endif /* !_FS_EXT2FS_EXT2_EXTENTS_H_ */ 135