1d7511a40SPedro F. Giffuni /*- 2d63027b6SPedro F. Giffuni * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 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 * $FreeBSD$ 29d7511a40SPedro F. Giffuni */ 30d7511a40SPedro F. Giffuni #ifndef _FS_EXT2FS_EXT2_EXTENTS_H_ 31d7511a40SPedro F. Giffuni #define _FS_EXT2FS_EXT2_EXTENTS_H_ 32d7511a40SPedro F. Giffuni 33d7511a40SPedro F. Giffuni #include <sys/types.h> 34d7511a40SPedro F. Giffuni 35d7511a40SPedro F. Giffuni #define EXT4_EXT_MAGIC 0xf30a 36b394cd1eSFedor Uporov #define EXT4_MAX_BLOCKS 0xffffffff 37b394cd1eSFedor Uporov #define EXT_INIT_MAX_LEN (1UL << 15) 38b394cd1eSFedor Uporov #define EXT4_MAX_LEN (EXT_INIT_MAX_LEN - 1) 39b394cd1eSFedor Uporov #define EXT4_EXT_DEPTH_MAX 5 40d7511a40SPedro F. Giffuni 41d7511a40SPedro F. Giffuni #define EXT4_EXT_CACHE_NO 0 42d7511a40SPedro F. Giffuni #define EXT4_EXT_CACHE_GAP 1 43d7511a40SPedro F. Giffuni #define EXT4_EXT_CACHE_IN 2 44d7511a40SPedro F. Giffuni 45d7511a40SPedro F. Giffuni /* 46512f29d1SFedor Uporov * Ext4 extent tail with csum 47512f29d1SFedor Uporov */ 48512f29d1SFedor Uporov struct ext4_extent_tail { 49512f29d1SFedor Uporov uint32_t et_checksum; /* crc32c(uuid+inum+extent_block) */ 50512f29d1SFedor Uporov }; 51512f29d1SFedor Uporov 52512f29d1SFedor Uporov /* 53d7511a40SPedro F. Giffuni * Ext4 file system extent on disk. 54d7511a40SPedro F. Giffuni */ 55d7511a40SPedro F. Giffuni struct ext4_extent { 56d7511a40SPedro F. Giffuni uint32_t e_blk; /* first logical block */ 57d7511a40SPedro F. Giffuni uint16_t e_len; /* number of blocks */ 58d7511a40SPedro F. Giffuni uint16_t e_start_hi; /* high 16 bits of physical block */ 59d7511a40SPedro F. Giffuni uint32_t e_start_lo; /* low 32 bits of physical block */ 60d7511a40SPedro F. Giffuni }; 61d7511a40SPedro F. Giffuni 62d7511a40SPedro F. Giffuni /* 63d7511a40SPedro F. Giffuni * Extent index on disk. 64d7511a40SPedro F. Giffuni */ 65d7511a40SPedro F. Giffuni struct ext4_extent_index { 66d7511a40SPedro F. Giffuni uint32_t ei_blk; /* indexes logical blocks */ 67d7511a40SPedro F. Giffuni uint32_t ei_leaf_lo; /* points to physical block of the 68d7511a40SPedro F. Giffuni * next level */ 69d7511a40SPedro F. Giffuni uint16_t ei_leaf_hi; /* high 16 bits of physical block */ 70d7511a40SPedro F. Giffuni uint16_t ei_unused; 71d7511a40SPedro F. Giffuni }; 72d7511a40SPedro F. Giffuni 73d7511a40SPedro F. Giffuni /* 74d7511a40SPedro F. Giffuni * Extent tree header. 75d7511a40SPedro F. Giffuni */ 76d7511a40SPedro F. Giffuni struct ext4_extent_header { 77d7511a40SPedro F. Giffuni uint16_t eh_magic; /* magic number: 0xf30a */ 78d7511a40SPedro F. Giffuni uint16_t eh_ecount; /* number of valid entries */ 79d7511a40SPedro F. Giffuni uint16_t eh_max; /* capacity of store in entries */ 80d7511a40SPedro F. Giffuni uint16_t eh_depth; /* the depth of extent tree */ 81d7511a40SPedro F. Giffuni uint32_t eh_gen; /* generation of extent tree */ 82d7511a40SPedro F. Giffuni }; 83d7511a40SPedro F. Giffuni 84d7511a40SPedro F. Giffuni /* 85d7511a40SPedro F. Giffuni * Save cached extent. 86d7511a40SPedro F. Giffuni */ 87d7511a40SPedro F. Giffuni struct ext4_extent_cache { 88d7511a40SPedro F. Giffuni daddr_t ec_start; /* extent start */ 89d7511a40SPedro F. Giffuni uint32_t ec_blk; /* logical block */ 90d7511a40SPedro F. Giffuni uint32_t ec_len; 91d7511a40SPedro F. Giffuni uint32_t ec_type; 92d7511a40SPedro F. Giffuni }; 93d7511a40SPedro F. Giffuni 94d7511a40SPedro F. Giffuni /* 95d7511a40SPedro F. Giffuni * Save path to some extent. 96d7511a40SPedro F. Giffuni */ 97d7511a40SPedro F. Giffuni struct ext4_extent_path { 98b394cd1eSFedor Uporov int index_count; 99d7511a40SPedro F. Giffuni uint16_t ep_depth; 100b394cd1eSFedor Uporov uint64_t ep_blk; 101b394cd1eSFedor Uporov char *ep_data; 102d7511a40SPedro F. Giffuni struct ext4_extent *ep_ext; 103d7511a40SPedro F. Giffuni struct ext4_extent_index *ep_index; 104d7511a40SPedro F. Giffuni struct ext4_extent_header *ep_header; 105d7511a40SPedro F. Giffuni }; 106d7511a40SPedro F. Giffuni 107b394cd1eSFedor Uporov #define EXT_FIRST_EXTENT(hdr) ((struct ext4_extent *)(((char *)(hdr)) + \ 108b394cd1eSFedor Uporov sizeof(struct ext4_extent_header))) 109b394cd1eSFedor Uporov #define EXT_FIRST_INDEX(hdr) ((struct ext4_extent_index *)(((char *)(hdr)) + \ 110b394cd1eSFedor Uporov sizeof(struct ext4_extent_header))) 111*cd3acfe7SFedor Uporov #define EXT_LAST_EXTENT(hdr) (EXT_FIRST_EXTENT((hdr)) + le16toh((hdr)->eh_ecount) - 1) 112*cd3acfe7SFedor Uporov #define EXT_LAST_INDEX(hdr) (EXT_FIRST_INDEX((hdr)) + le16toh((hdr)->eh_ecount) - 1) 113b394cd1eSFedor Uporov #define EXT4_EXTENT_TAIL_OFFSET(hdr) (sizeof(struct ext4_extent_header) + \ 114*cd3acfe7SFedor Uporov (sizeof(struct ext4_extent) * le16toh((hdr)->eh_max))) 115b394cd1eSFedor Uporov #define EXT_HAS_FREE_INDEX(path) \ 116*cd3acfe7SFedor Uporov (le16toh((path)->ep_header->eh_ecount) < le16toh((path)->ep_header->eh_max)) 117*cd3acfe7SFedor Uporov #define EXT_MAX_EXTENT(hdr) (EXT_FIRST_EXTENT(hdr) + le16toh((hdr)->eh_max) - 1) 118*cd3acfe7SFedor Uporov #define EXT_MAX_INDEX(hdr) (EXT_FIRST_INDEX((hdr)) + le16toh((hdr)->eh_max) - 1) 119b394cd1eSFedor Uporov 120d7511a40SPedro F. Giffuni struct inode; 121d7511a40SPedro F. Giffuni struct m_ext2fs; 122b394cd1eSFedor Uporov void ext4_ext_tree_init(struct inode *ip); 123d7511a40SPedro F. Giffuni int ext4_ext_in_cache(struct inode *, daddr_t, struct ext4_extent *); 124d7511a40SPedro F. Giffuni void ext4_ext_put_cache(struct inode *, struct ext4_extent *, int); 125b394cd1eSFedor Uporov int ext4_ext_find_extent(struct inode *, daddr_t, struct ext4_extent_path **); 126b394cd1eSFedor Uporov void ext4_ext_path_free(struct ext4_extent_path *path); 127b394cd1eSFedor Uporov int ext4_ext_remove_space(struct inode *ip, off_t length, int flags, 128b394cd1eSFedor Uporov struct ucred *cred, struct thread *td); 129b394cd1eSFedor Uporov int ext4_ext_get_blocks(struct inode *ip, int64_t iblock, 1303acd9182SFedor Uporov unsigned long max_blocks, struct ucred *cred, struct buf **bpp, 1313acd9182SFedor Uporov int *allocate, daddr_t *); 132ebc94b66SFedor Uporov #ifdef EXT2FS_PRINT_EXTENTS 133b394cd1eSFedor Uporov void ext4_ext_print_extent_tree_status(struct inode *ip); 134b394cd1eSFedor Uporov #endif 135d7511a40SPedro F. Giffuni 136d7511a40SPedro F. Giffuni #endif /* !_FS_EXT2FS_EXT2_EXTENTS_H_ */ 137