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