| 1 | // SPDX-License-Identifier: GPL-2.0 | 
|---|
| 2 | /* | 
|---|
| 3 | * Copyright (c) 2003-2006, Cluster File Systems, Inc, info@clusterfs.com | 
|---|
| 4 | * Written by Alex Tomas <alex@clusterfs.com> | 
|---|
| 5 | */ | 
|---|
| 6 |  | 
|---|
| 7 | #ifndef _EXT4_EXTENTS | 
|---|
| 8 | #define _EXT4_EXTENTS | 
|---|
| 9 |  | 
|---|
| 10 | #include "ext4.h" | 
|---|
| 11 |  | 
|---|
| 12 | /* | 
|---|
| 13 | * With AGGRESSIVE_TEST defined, the capacity of index/leaf blocks | 
|---|
| 14 | * becomes very small, so index split, in-depth growing and | 
|---|
| 15 | * other hard changes happen much more often. | 
|---|
| 16 | * This is for debug purposes only. | 
|---|
| 17 | */ | 
|---|
| 18 | #define AGGRESSIVE_TEST_ | 
|---|
| 19 |  | 
|---|
| 20 | /* | 
|---|
| 21 | * With EXTENTS_STATS defined, the number of blocks and extents | 
|---|
| 22 | * are collected in the truncate path. They'll be shown at | 
|---|
| 23 | * umount time. | 
|---|
| 24 | */ | 
|---|
| 25 | #define EXTENTS_STATS__ | 
|---|
| 26 |  | 
|---|
| 27 | /* | 
|---|
| 28 | * If CHECK_BINSEARCH is defined, then the results of the binary search | 
|---|
| 29 | * will also be checked by linear search. | 
|---|
| 30 | */ | 
|---|
| 31 | #define CHECK_BINSEARCH__ | 
|---|
| 32 |  | 
|---|
| 33 | /* | 
|---|
| 34 | * ext4_inode has i_block array (60 bytes total). | 
|---|
| 35 | * The first 12 bytes store ext4_extent_header; | 
|---|
| 36 | * the remainder stores an array of ext4_extent. | 
|---|
| 37 | * For non-inode extent blocks, ext4_extent_tail | 
|---|
| 38 | * follows the array. | 
|---|
| 39 | */ | 
|---|
| 40 |  | 
|---|
| 41 | /* | 
|---|
| 42 | * This is the extent tail on-disk structure. | 
|---|
| 43 | * All other extent structures are 12 bytes long.  It turns out that | 
|---|
| 44 | * block_size % 12 >= 4 for at least all powers of 2 greater than 512, which | 
|---|
| 45 | * covers all valid ext4 block sizes.  Therefore, this tail structure can be | 
|---|
| 46 | * crammed into the end of the block without having to rebalance the tree. | 
|---|
| 47 | */ | 
|---|
| 48 | struct ext4_extent_tail { | 
|---|
| 49 | __le32	et_checksum;	/* crc32c(uuid+inum+extent_block) */ | 
|---|
| 50 | }; | 
|---|
| 51 |  | 
|---|
| 52 | /* | 
|---|
| 53 | * This is the extent on-disk structure. | 
|---|
| 54 | * It's used at the bottom of the tree. | 
|---|
| 55 | */ | 
|---|
| 56 | struct ext4_extent { | 
|---|
| 57 | __le32	ee_block;	/* first logical block extent covers */ | 
|---|
| 58 | __le16	ee_len;		/* number of blocks covered by extent */ | 
|---|
| 59 | __le16	ee_start_hi;	/* high 16 bits of physical block */ | 
|---|
| 60 | __le32	ee_start_lo;	/* low 32 bits of physical block */ | 
|---|
| 61 | }; | 
|---|
| 62 |  | 
|---|
| 63 | /* | 
|---|
| 64 | * This is index on-disk structure. | 
|---|
| 65 | * It's used at all the levels except the bottom. | 
|---|
| 66 | */ | 
|---|
| 67 | struct ext4_extent_idx { | 
|---|
| 68 | __le32	ei_block;	/* index covers logical blocks from 'block' */ | 
|---|
| 69 | __le32	ei_leaf_lo;	/* pointer to the physical block of the next * | 
|---|
| 70 | * level. leaf or next index could be there */ | 
|---|
| 71 | __le16	ei_leaf_hi;	/* high 16 bits of physical block */ | 
|---|
| 72 | __u16	ei_unused; | 
|---|
| 73 | }; | 
|---|
| 74 |  | 
|---|
| 75 | /* | 
|---|
| 76 | * Each block (leaves and indexes), even inode-stored has header. | 
|---|
| 77 | */ | 
|---|
| 78 | struct  { | 
|---|
| 79 | __le16	;	/* probably will support different formats */ | 
|---|
| 80 | __le16	;	/* number of valid entries */ | 
|---|
| 81 | __le16	;		/* capacity of store in entries */ | 
|---|
| 82 | __le16	;	/* has tree real underlying blocks? */ | 
|---|
| 83 | __le32	;	/* generation of the tree */ | 
|---|
| 84 | }; | 
|---|
| 85 |  | 
|---|
| 86 | #define EXT4_EXT_MAGIC		cpu_to_le16(0xf30a) | 
|---|
| 87 | #define EXT4_MAX_EXTENT_DEPTH 5 | 
|---|
| 88 |  | 
|---|
| 89 | #define EXT4_EXTENT_TAIL_OFFSET(hdr) \ | 
|---|
| 90 | (sizeof(struct ext4_extent_header) + \ | 
|---|
| 91 | (sizeof(struct ext4_extent) * le16_to_cpu((hdr)->eh_max))) | 
|---|
| 92 |  | 
|---|
| 93 | static inline struct ext4_extent_tail * | 
|---|
| 94 | find_ext4_extent_tail(struct ext4_extent_header *eh) | 
|---|
| 95 | { | 
|---|
| 96 | return (struct ext4_extent_tail *)(((void *)eh) + | 
|---|
| 97 | EXT4_EXTENT_TAIL_OFFSET(eh)); | 
|---|
| 98 | } | 
|---|
| 99 |  | 
|---|
| 100 | /* | 
|---|
| 101 | * Array of ext4_ext_path contains path to some extent. | 
|---|
| 102 | * Creation/lookup routines use it for traversal/splitting/etc. | 
|---|
| 103 | * Truncate uses it to simulate recursive walking. | 
|---|
| 104 | */ | 
|---|
| 105 | struct ext4_ext_path { | 
|---|
| 106 | ext4_fsblk_t			p_block; | 
|---|
| 107 | __u16				p_depth; | 
|---|
| 108 | __u16				p_maxdepth; | 
|---|
| 109 | struct ext4_extent		*p_ext; | 
|---|
| 110 | struct ext4_extent_idx		*p_idx; | 
|---|
| 111 | struct ext4_extent_header	*p_hdr; | 
|---|
| 112 | struct buffer_head		*p_bh; | 
|---|
| 113 | }; | 
|---|
| 114 |  | 
|---|
| 115 | /* | 
|---|
| 116 | * Used to record a portion of a cluster found at the beginning or end | 
|---|
| 117 | * of an extent while traversing the extent tree during space removal. | 
|---|
| 118 | * A partial cluster may be removed if it does not contain blocks shared | 
|---|
| 119 | * with extents that aren't being deleted (tofree state).  Otherwise, | 
|---|
| 120 | * it cannot be removed (nofree state). | 
|---|
| 121 | */ | 
|---|
| 122 | struct partial_cluster { | 
|---|
| 123 | ext4_fsblk_t pclu;  /* physical cluster number */ | 
|---|
| 124 | ext4_lblk_t lblk;   /* logical block number within logical cluster */ | 
|---|
| 125 | enum {initial, tofree, nofree} state; | 
|---|
| 126 | }; | 
|---|
| 127 |  | 
|---|
| 128 | /* | 
|---|
| 129 | * structure for external API | 
|---|
| 130 | */ | 
|---|
| 131 |  | 
|---|
| 132 | /* | 
|---|
| 133 | * EXT_INIT_MAX_LEN is the maximum number of blocks we can have in an | 
|---|
| 134 | * initialized extent. This is 2^15 and not (2^16 - 1), since we use the | 
|---|
| 135 | * MSB of ee_len field in the extent datastructure to signify if this | 
|---|
| 136 | * particular extent is an initialized extent or an unwritten (i.e. | 
|---|
| 137 | * preallocated). | 
|---|
| 138 | * EXT_UNWRITTEN_MAX_LEN is the maximum number of blocks we can have in an | 
|---|
| 139 | * unwritten extent. | 
|---|
| 140 | * If ee_len is <= 0x8000, it is an initialized extent. Otherwise, it is an | 
|---|
| 141 | * unwritten one. In other words, if MSB of ee_len is set, it is an | 
|---|
| 142 | * unwritten extent with only one special scenario when ee_len = 0x8000. | 
|---|
| 143 | * In this case we can not have an unwritten extent of zero length and | 
|---|
| 144 | * thus we make it as a special case of initialized extent with 0x8000 length. | 
|---|
| 145 | * This way we get better extent-to-group alignment for initialized extents. | 
|---|
| 146 | * Hence, the maximum number of blocks we can have in an *initialized* | 
|---|
| 147 | * extent is 2^15 (32768) and in an *unwritten* extent is 2^15-1 (32767). | 
|---|
| 148 | */ | 
|---|
| 149 | #define EXT_INIT_MAX_LEN	(1UL << 15) | 
|---|
| 150 | #define EXT_UNWRITTEN_MAX_LEN	(EXT_INIT_MAX_LEN - 1) | 
|---|
| 151 |  | 
|---|
| 152 |  | 
|---|
| 153 | #define EXT_FIRST_EXTENT(__hdr__) \ | 
|---|
| 154 | ((struct ext4_extent *) (((char *) (__hdr__)) +		\ | 
|---|
| 155 | sizeof(struct ext4_extent_header))) | 
|---|
| 156 | #define EXT_FIRST_INDEX(__hdr__) \ | 
|---|
| 157 | ((struct ext4_extent_idx *) (((char *) (__hdr__)) +	\ | 
|---|
| 158 | sizeof(struct ext4_extent_header))) | 
|---|
| 159 | #define EXT_HAS_FREE_INDEX(__path__) \ | 
|---|
| 160 | (le16_to_cpu((__path__)->p_hdr->eh_entries) \ | 
|---|
| 161 | < le16_to_cpu((__path__)->p_hdr->eh_max)) | 
|---|
| 162 | #define EXT_LAST_EXTENT(__hdr__) \ | 
|---|
| 163 | (EXT_FIRST_EXTENT((__hdr__)) + le16_to_cpu((__hdr__)->eh_entries) - 1) | 
|---|
| 164 | #define EXT_LAST_INDEX(__hdr__) \ | 
|---|
| 165 | (EXT_FIRST_INDEX((__hdr__)) + le16_to_cpu((__hdr__)->eh_entries) - 1) | 
|---|
| 166 | #define EXT_MAX_EXTENT(__hdr__)	\ | 
|---|
| 167 | ((le16_to_cpu((__hdr__)->eh_max)) ? \ | 
|---|
| 168 | ((EXT_FIRST_EXTENT((__hdr__)) + le16_to_cpu((__hdr__)->eh_max) - 1)) \ | 
|---|
| 169 | : NULL) | 
|---|
| 170 | #define EXT_MAX_INDEX(__hdr__) \ | 
|---|
| 171 | ((le16_to_cpu((__hdr__)->eh_max)) ? \ | 
|---|
| 172 | ((EXT_FIRST_INDEX((__hdr__)) + le16_to_cpu((__hdr__)->eh_max) - 1)) \ | 
|---|
| 173 | : NULL) | 
|---|
| 174 |  | 
|---|
| 175 | static inline struct ext4_extent_header *ext_inode_hdr(struct inode *inode) | 
|---|
| 176 | { | 
|---|
| 177 | return (struct ext4_extent_header *) EXT4_I(inode)->i_data; | 
|---|
| 178 | } | 
|---|
| 179 |  | 
|---|
| 180 | static inline struct ext4_extent_header *ext_block_hdr(struct buffer_head *bh) | 
|---|
| 181 | { | 
|---|
| 182 | return (struct ext4_extent_header *) bh->b_data; | 
|---|
| 183 | } | 
|---|
| 184 |  | 
|---|
| 185 | static inline unsigned short ext_depth(struct inode *inode) | 
|---|
| 186 | { | 
|---|
| 187 | return le16_to_cpu(ext_inode_hdr(inode)->eh_depth); | 
|---|
| 188 | } | 
|---|
| 189 |  | 
|---|
| 190 | static inline void ext4_ext_mark_unwritten(struct ext4_extent *ext) | 
|---|
| 191 | { | 
|---|
| 192 | /* We can not have an unwritten extent of zero length! */ | 
|---|
| 193 | BUG_ON((le16_to_cpu(ext->ee_len) & ~EXT_INIT_MAX_LEN) == 0); | 
|---|
| 194 | ext->ee_len |= cpu_to_le16(EXT_INIT_MAX_LEN); | 
|---|
| 195 | } | 
|---|
| 196 |  | 
|---|
| 197 | static inline int ext4_ext_is_unwritten(struct ext4_extent *ext) | 
|---|
| 198 | { | 
|---|
| 199 | /* Extent with ee_len of 0x8000 is treated as an initialized extent */ | 
|---|
| 200 | return (le16_to_cpu(ext->ee_len) > EXT_INIT_MAX_LEN); | 
|---|
| 201 | } | 
|---|
| 202 |  | 
|---|
| 203 | static inline int ext4_ext_get_actual_len(struct ext4_extent *ext) | 
|---|
| 204 | { | 
|---|
| 205 | return (le16_to_cpu(ext->ee_len) <= EXT_INIT_MAX_LEN ? | 
|---|
| 206 | le16_to_cpu(ext->ee_len) : | 
|---|
| 207 | (le16_to_cpu(ext->ee_len) - EXT_INIT_MAX_LEN)); | 
|---|
| 208 | } | 
|---|
| 209 |  | 
|---|
| 210 | static inline void ext4_ext_mark_initialized(struct ext4_extent *ext) | 
|---|
| 211 | { | 
|---|
| 212 | ext->ee_len = cpu_to_le16(ext4_ext_get_actual_len(ext)); | 
|---|
| 213 | } | 
|---|
| 214 |  | 
|---|
| 215 | /* | 
|---|
| 216 | * ext4_ext_pblock: | 
|---|
| 217 | * combine low and high parts of physical block number into ext4_fsblk_t | 
|---|
| 218 | */ | 
|---|
| 219 | static inline ext4_fsblk_t ext4_ext_pblock(struct ext4_extent *ex) | 
|---|
| 220 | { | 
|---|
| 221 | ext4_fsblk_t block; | 
|---|
| 222 |  | 
|---|
| 223 | block = le32_to_cpu(ex->ee_start_lo); | 
|---|
| 224 | block |= ((ext4_fsblk_t) le16_to_cpu(ex->ee_start_hi) << 31) << 1; | 
|---|
| 225 | return block; | 
|---|
| 226 | } | 
|---|
| 227 |  | 
|---|
| 228 | /* | 
|---|
| 229 | * ext4_idx_pblock: | 
|---|
| 230 | * combine low and high parts of a leaf physical block number into ext4_fsblk_t | 
|---|
| 231 | */ | 
|---|
| 232 | static inline ext4_fsblk_t ext4_idx_pblock(struct ext4_extent_idx *ix) | 
|---|
| 233 | { | 
|---|
| 234 | ext4_fsblk_t block; | 
|---|
| 235 |  | 
|---|
| 236 | block = le32_to_cpu(ix->ei_leaf_lo); | 
|---|
| 237 | block |= ((ext4_fsblk_t) le16_to_cpu(ix->ei_leaf_hi) << 31) << 1; | 
|---|
| 238 | return block; | 
|---|
| 239 | } | 
|---|
| 240 |  | 
|---|
| 241 | /* | 
|---|
| 242 | * ext4_ext_store_pblock: | 
|---|
| 243 | * stores a large physical block number into an extent struct, | 
|---|
| 244 | * breaking it into parts | 
|---|
| 245 | */ | 
|---|
| 246 | static inline void ext4_ext_store_pblock(struct ext4_extent *ex, | 
|---|
| 247 | ext4_fsblk_t pb) | 
|---|
| 248 | { | 
|---|
| 249 | ex->ee_start_lo = cpu_to_le32((unsigned long) (pb & 0xffffffff)); | 
|---|
| 250 | ex->ee_start_hi = cpu_to_le16((unsigned long) ((pb >> 31) >> 1) & | 
|---|
| 251 | 0xffff); | 
|---|
| 252 | } | 
|---|
| 253 |  | 
|---|
| 254 | /* | 
|---|
| 255 | * ext4_idx_store_pblock: | 
|---|
| 256 | * stores a large physical block number into an index struct, | 
|---|
| 257 | * breaking it into parts | 
|---|
| 258 | */ | 
|---|
| 259 | static inline void ext4_idx_store_pblock(struct ext4_extent_idx *ix, | 
|---|
| 260 | ext4_fsblk_t pb) | 
|---|
| 261 | { | 
|---|
| 262 | ix->ei_leaf_lo = cpu_to_le32((unsigned long) (pb & 0xffffffff)); | 
|---|
| 263 | ix->ei_leaf_hi = cpu_to_le16((unsigned long) ((pb >> 31) >> 1) & | 
|---|
| 264 | 0xffff); | 
|---|
| 265 | } | 
|---|
| 266 |  | 
|---|
| 267 | #endif /* _EXT4_EXTENTS */ | 
|---|
| 268 |  | 
|---|
| 269 |  | 
|---|