ext3_extents.h revision 5c3e1b917caf780943695da3e5d772688ca7ab91
1/* 2 * Copyright (c) 2003,2004 Cluster File Systems, Inc, info@clusterfs.com 3 * Written by Alex Tomas <alex@clusterfs.com> 4 * 5 * This program is free software; you can redistribute it and/or modify 6 * it under the terms of the GNU General Public License version 2 as 7 * published by the Free Software Foundation. 8 * 9 * This program is distributed in the hope that it will be useful, 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 * GNU General Public License for more details. 13 * 14 * You should have received a copy of the GNU General Public Licens 15 * along with this program; if not, write to the Free Software 16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111- 17 */ 18 19#ifndef _LINUX_EXT3_EXTENTS 20#define _LINUX_EXT3_EXTENTS 21 22/* 23 * with AGRESSIVE_TEST defined capacity of index/leaf blocks 24 * become very little, so index split, in-depth growing and 25 * other hard changes happens much more often 26 * this is for debug purposes only 27 */ 28#define AGRESSIVE_TEST_ 29 30/* 31 * if CHECK_BINSEARCH defined, then results of binary search 32 * will be checked by linear search 33 */ 34#define CHECK_BINSEARCH_ 35 36/* 37 * if EXT_DEBUG is defined you can use 'extdebug' mount option 38 * to get lots of info what's going on 39 */ 40/* #define EXT_DEBUG */ 41#ifdef EXT_DEBUG 42#define ext_debug(tree,fmt,a...) \ 43do { \ 44 if (test_opt((tree)->inode->i_sb, EXTDEBUG)) \ 45 printk(fmt, ##a); \ 46} while (0); 47#else 48#define ext_debug(tree,fmt,a...) 49#endif 50 51/* 52 * if EXT_STATS is defined then stats numbers are collected 53 * these number will be displayed at umount time 54 */ 55#define EXT_STATS_ 56 57 58#define EXT3_ALLOC_NEEDED 3 /* block bitmap + group desc. + sb */ 59 60/* 61 * ext3_inode has i_block array (total 60 bytes) 62 * first 4 bytes are used to store: 63 * - tree depth (0 mean there is no tree yet. all extents in the inode) 64 * - number of alive extents in the inode 65 */ 66 67/* 68 * this is extent on-disk structure 69 * it's used at the bottom of the tree 70 */ 71struct ext3_extent { 72 __u32 ee_block; /* first logical block extent covers */ 73 __u16 ee_len; /* number of blocks covered by extent */ 74 __u16 ee_start_hi; /* high 16 bits of physical block */ 75 __u32 ee_start; /* low 32 bigs of physical block */ 76}; 77 78/* 79 * this is index on-disk structure 80 * it's used at all the levels, but the bottom 81 */ 82struct ext3_extent_idx { 83 __u32 ei_block; /* index covers logical blocks from 'block' */ 84 __u32 ei_leaf; /* pointer to the physical block of the next * 85 * level. leaf or next index could bet here */ 86 __u16 ei_leaf_hi; /* high 16 bits of physical block */ 87 __u16 ei_unused; 88}; 89 90/* 91 * each block (leaves and indexes), even inode-stored has header 92 */ 93struct ext3_extent_header { 94 __u16 eh_magic; /* probably will support different formats */ 95 __u16 eh_entries; /* number of valid entries */ 96 __u16 eh_max; /* capacity of store in entries */ 97 __u16 eh_depth; /* has tree real underlaying blocks? */ 98 __u32 eh_generation; /* generation of the tree */ 99}; 100 101#define EXT3_EXT_MAGIC 0xf30a 102 103/* 104 * array of ext3_ext_path contains path to some extent 105 * creation/lookup routines use it for traversal/splitting/etc 106 * truncate uses it to simulate recursive walking 107 */ 108struct ext3_ext_path { 109 __u32 p_block; 110 __u16 p_depth; 111 struct ext3_extent *p_ext; 112 struct ext3_extent_idx *p_idx; 113 struct ext3_extent_header *p_hdr; 114 struct buffer_head *p_bh; 115}; 116 117/* 118 * structure for external API 119 */ 120 121#define EXT_CONTINUE 0 122#define EXT_BREAK 1 123#define EXT_REPEAT 2 124 125 126#define EXT_MAX_BLOCK 0xffffffff 127#define EXT_CACHE_MARK 0xffff 128 129/* 130 * EXT_INIT_MAX_LEN is the maximum number of blocks we can have in an 131 * initialized extent. This is 2^15 and not (2^16 - 1), since we use the 132 * MSB of ee_len field in the extent datastructure to signify if this 133 * particular extent is an initialized extent or an uninitialized (i.e. 134 * preallocated). 135 * EXT_UNINIT_MAX_LEN is the maximum number of blocks we can have in an 136 * uninitialized extent. 137 * If ee_len is <= 0x8000, it is an initialized extent. Otherwise, it is an 138 * uninitialized one. In other words, if MSB of ee_len is set, it is an 139 * uninitialized extent with only one special scenario when ee_len = 0x8000. 140 * In this case we can not have an uninitialized extent of zero length and 141 * thus we make it as a special case of initialized extent with 0x8000 length. 142 * This way we get better extent-to-group alignment for initialized extents. 143 * Hence, the maximum number of blocks we can have in an *initialized* 144 * extent is 2^15 (32768) and in an *uninitialized* extent is 2^15-1 (32767). 145 */ 146#define EXT_INIT_MAX_LEN (1UL << 15) 147#define EXT_UNINIT_MAX_LEN (EXT_INIT_MAX_LEN - 1) 148 149 150#define EXT_FIRST_EXTENT(__hdr__) \ 151 ((struct ext3_extent *) (((char *) (__hdr__)) + \ 152 sizeof(struct ext3_extent_header))) 153#define EXT_FIRST_INDEX(__hdr__) \ 154 ((struct ext3_extent_idx *) (((char *) (__hdr__)) + \ 155 sizeof(struct ext3_extent_header))) 156#define EXT_HAS_FREE_INDEX(__path__) \ 157 ((__path__)->p_hdr->eh_entries < (__path__)->p_hdr->eh_max) 158#define EXT_LAST_EXTENT(__hdr__) \ 159 (EXT_FIRST_EXTENT((__hdr__)) + (__hdr__)->eh_entries - 1) 160#define EXT_LAST_INDEX(__hdr__) \ 161 (EXT_FIRST_INDEX((__hdr__)) + (__hdr__)->eh_entries - 1) 162#define EXT_MAX_EXTENT(__hdr__) \ 163 (EXT_FIRST_EXTENT((__hdr__)) + (__hdr__)->eh_max - 1) 164#define EXT_MAX_INDEX(__hdr__) \ 165 (EXT_FIRST_INDEX((__hdr__)) + (__hdr__)->eh_max - 1) 166 167#define EXT_ROOT_HDR(tree) \ 168 ((struct ext3_extent_header *) (tree)->root) 169#define EXT_BLOCK_HDR(bh) \ 170 ((struct ext3_extent_header *) (bh)->b_data) 171#define EXT_DEPTH(_t_) \ 172 (((struct ext3_extent_header *)((_t_)->root))->eh_depth) 173#define EXT_GENERATION(_t_) \ 174 (((struct ext3_extent_header *)((_t_)->root))->eh_generation) 175 176 177#define EXT_ASSERT(__x__) if (!(__x__)) BUG(); 178 179 180/* 181 * this structure is used to gather extents from the tree via ioctl 182 */ 183struct ext3_extent_buf { 184 unsigned long start; 185 int buflen; 186 void *buffer; 187 void *cur; 188 int err; 189}; 190 191/* 192 * this structure is used to collect stats info about the tree 193 */ 194struct ext3_extent_tree_stats { 195 int depth; 196 int extents_num; 197 int leaf_num; 198}; 199 200#ifdef __KERNEL__ 201/* 202 * ext3_extents_tree is used to pass initial information 203 * to top-level extents API 204 */ 205struct ext3_extents_helpers; 206struct ext3_extents_tree { 207 struct inode *inode; /* inode which tree belongs to */ 208 void *root; /* ptr to data top of tree resides at */ 209 void *buffer; /* will be passed as arg to ^^ routines */ 210 int buffer_len; 211 void *private; 212 struct ext3_extent *cex;/* last found extent */ 213 struct ext3_extents_helpers *ops; 214}; 215 216struct ext3_extents_helpers { 217 int (*get_write_access)(handle_t *h, void *buffer); 218 int (*mark_buffer_dirty)(handle_t *h, void *buffer); 219 int (*mergable)(struct ext3_extent *ex1, struct ext3_extent *ex2); 220 int (*remove_extent_credits)(struct ext3_extents_tree *, 221 struct ext3_extent *, unsigned long, 222 unsigned long); 223 int (*remove_extent)(struct ext3_extents_tree *, 224 struct ext3_extent *, unsigned long, 225 unsigned long); 226 int (*new_block)(handle_t *, struct ext3_extents_tree *, 227 struct ext3_ext_path *, struct ext3_extent *, 228 int *); 229}; 230 231/* 232 * to be called by ext3_ext_walk_space() 233 * negative retcode - error 234 * positive retcode - signal for ext3_ext_walk_space(), see below 235 * callback must return valid extent (passed or newly created) 236 */ 237typedef int (*ext_prepare_callback)(struct ext3_extents_tree *, 238 struct ext3_ext_path *, 239 struct ext3_extent *, int); 240void ext3_init_tree_desc(struct ext3_extents_tree *, struct inode *); 241extern int ext3_extent_tree_init(handle_t *, struct ext3_extents_tree *); 242extern int ext3_ext_calc_credits_for_insert(struct ext3_extents_tree *, struct ext3_ext_path *); 243extern int ext3_ext_insert_extent(handle_t *, struct ext3_extents_tree *, struct ext3_ext_path *, struct ext3_extent *); 244extern int ext3_ext_walk_space(struct ext3_extents_tree *, unsigned long, unsigned long, ext_prepare_callback); 245extern int ext3_ext_remove_space(struct ext3_extents_tree *, unsigned long, unsigned long); 246extern struct ext3_ext_path * ext3_ext_find_extent(struct ext3_extents_tree *, int, struct ext3_ext_path *); 247 248static inline void 249ext3_ext_invalidate_cache(struct ext3_extents_tree *tree) 250{ 251 if (tree->cex) 252 tree->cex->ee_len = 0; 253} 254#endif /* __KERNEL__ */ 255 256 257#endif /* _LINUX_EXT3_EXTENTS */ 258 259