1 /* 2 * Copyright (c) 2000-2001,2005 Silicon Graphics, Inc. 3 * All Rights Reserved. 4 * 5 * This program is free software; you can redistribute it and/or 6 * modify it under the terms of the GNU General Public License as 7 * published by the Free Software Foundation. 8 * 9 * This program is distributed in the hope that it would 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 License 15 * along with this program; if not, write the Free Software Foundation, 16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 17 */ 18 #ifndef __XFS_BTREE_H__ 19 #define __XFS_BTREE_H__ 20 21 struct xfs_buf; 22 struct xfs_defer_ops; 23 struct xfs_inode; 24 struct xfs_mount; 25 struct xfs_trans; 26 27 extern kmem_zone_t *xfs_btree_cur_zone; 28 29 /* 30 * Generic key, ptr and record wrapper structures. 31 * 32 * These are disk format structures, and are converted where necessary 33 * by the btree specific code that needs to interpret them. 34 */ 35 union xfs_btree_ptr { 36 __be32 s; /* short form ptr */ 37 __be64 l; /* long form ptr */ 38 }; 39 40 /* 41 * The in-core btree key. Overlapping btrees actually store two keys 42 * per pointer, so we reserve enough memory to hold both. The __*bigkey 43 * items should never be accessed directly. 44 */ 45 union xfs_btree_key { 46 struct xfs_bmbt_key bmbt; 47 xfs_bmdr_key_t bmbr; /* bmbt root block */ 48 xfs_alloc_key_t alloc; 49 struct xfs_inobt_key inobt; 50 struct xfs_rmap_key rmap; 51 struct xfs_rmap_key __rmap_bigkey[2]; 52 struct xfs_refcount_key refc; 53 }; 54 55 union xfs_btree_rec { 56 struct xfs_bmbt_rec bmbt; 57 xfs_bmdr_rec_t bmbr; /* bmbt root block */ 58 struct xfs_alloc_rec alloc; 59 struct xfs_inobt_rec inobt; 60 struct xfs_rmap_rec rmap; 61 struct xfs_refcount_rec refc; 62 }; 63 64 /* 65 * This nonsense is to make -wlint happy. 66 */ 67 #define XFS_LOOKUP_EQ ((xfs_lookup_t)XFS_LOOKUP_EQi) 68 #define XFS_LOOKUP_LE ((xfs_lookup_t)XFS_LOOKUP_LEi) 69 #define XFS_LOOKUP_GE ((xfs_lookup_t)XFS_LOOKUP_GEi) 70 71 #define XFS_BTNUM_BNO ((xfs_btnum_t)XFS_BTNUM_BNOi) 72 #define XFS_BTNUM_CNT ((xfs_btnum_t)XFS_BTNUM_CNTi) 73 #define XFS_BTNUM_BMAP ((xfs_btnum_t)XFS_BTNUM_BMAPi) 74 #define XFS_BTNUM_INO ((xfs_btnum_t)XFS_BTNUM_INOi) 75 #define XFS_BTNUM_FINO ((xfs_btnum_t)XFS_BTNUM_FINOi) 76 #define XFS_BTNUM_RMAP ((xfs_btnum_t)XFS_BTNUM_RMAPi) 77 #define XFS_BTNUM_REFC ((xfs_btnum_t)XFS_BTNUM_REFCi) 78 79 uint32_t xfs_btree_magic(int crc, xfs_btnum_t btnum); 80 81 /* 82 * For logging record fields. 83 */ 84 #define XFS_BB_MAGIC (1 << 0) 85 #define XFS_BB_LEVEL (1 << 1) 86 #define XFS_BB_NUMRECS (1 << 2) 87 #define XFS_BB_LEFTSIB (1 << 3) 88 #define XFS_BB_RIGHTSIB (1 << 4) 89 #define XFS_BB_BLKNO (1 << 5) 90 #define XFS_BB_LSN (1 << 6) 91 #define XFS_BB_UUID (1 << 7) 92 #define XFS_BB_OWNER (1 << 8) 93 #define XFS_BB_NUM_BITS 5 94 #define XFS_BB_ALL_BITS ((1 << XFS_BB_NUM_BITS) - 1) 95 #define XFS_BB_NUM_BITS_CRC 9 96 #define XFS_BB_ALL_BITS_CRC ((1 << XFS_BB_NUM_BITS_CRC) - 1) 97 98 /* 99 * Generic stats interface 100 */ 101 #define XFS_BTREE_STATS_INC(cur, stat) \ 102 XFS_STATS_INC_OFF((cur)->bc_mp, (cur)->bc_statoff + __XBTS_ ## stat) 103 #define XFS_BTREE_STATS_ADD(cur, stat, val) \ 104 XFS_STATS_ADD_OFF((cur)->bc_mp, (cur)->bc_statoff + __XBTS_ ## stat, val) 105 106 #define XFS_BTREE_MAXLEVELS 9 /* max of all btrees */ 107 108 struct xfs_btree_ops { 109 /* size of the key and record structures */ 110 size_t key_len; 111 size_t rec_len; 112 113 /* cursor operations */ 114 struct xfs_btree_cur *(*dup_cursor)(struct xfs_btree_cur *); 115 void (*update_cursor)(struct xfs_btree_cur *src, 116 struct xfs_btree_cur *dst); 117 118 /* update btree root pointer */ 119 void (*set_root)(struct xfs_btree_cur *cur, 120 union xfs_btree_ptr *nptr, int level_change); 121 122 /* block allocation / freeing */ 123 int (*alloc_block)(struct xfs_btree_cur *cur, 124 union xfs_btree_ptr *start_bno, 125 union xfs_btree_ptr *new_bno, 126 int *stat); 127 int (*free_block)(struct xfs_btree_cur *cur, struct xfs_buf *bp); 128 129 /* update last record information */ 130 void (*update_lastrec)(struct xfs_btree_cur *cur, 131 struct xfs_btree_block *block, 132 union xfs_btree_rec *rec, 133 int ptr, int reason); 134 135 /* records in block/level */ 136 int (*get_minrecs)(struct xfs_btree_cur *cur, int level); 137 int (*get_maxrecs)(struct xfs_btree_cur *cur, int level); 138 139 /* records on disk. Matter for the root in inode case. */ 140 int (*get_dmaxrecs)(struct xfs_btree_cur *cur, int level); 141 142 /* init values of btree structures */ 143 void (*init_key_from_rec)(union xfs_btree_key *key, 144 union xfs_btree_rec *rec); 145 void (*init_rec_from_cur)(struct xfs_btree_cur *cur, 146 union xfs_btree_rec *rec); 147 void (*init_ptr_from_cur)(struct xfs_btree_cur *cur, 148 union xfs_btree_ptr *ptr); 149 void (*init_high_key_from_rec)(union xfs_btree_key *key, 150 union xfs_btree_rec *rec); 151 152 /* difference between key value and cursor value */ 153 int64_t (*key_diff)(struct xfs_btree_cur *cur, 154 union xfs_btree_key *key); 155 156 /* 157 * Difference between key2 and key1 -- positive if key1 > key2, 158 * negative if key1 < key2, and zero if equal. 159 */ 160 int64_t (*diff_two_keys)(struct xfs_btree_cur *cur, 161 union xfs_btree_key *key1, 162 union xfs_btree_key *key2); 163 164 const struct xfs_buf_ops *buf_ops; 165 166 /* check that k1 is lower than k2 */ 167 int (*keys_inorder)(struct xfs_btree_cur *cur, 168 union xfs_btree_key *k1, 169 union xfs_btree_key *k2); 170 171 /* check that r1 is lower than r2 */ 172 int (*recs_inorder)(struct xfs_btree_cur *cur, 173 union xfs_btree_rec *r1, 174 union xfs_btree_rec *r2); 175 }; 176 177 /* 178 * Reasons for the update_lastrec method to be called. 179 */ 180 #define LASTREC_UPDATE 0 181 #define LASTREC_INSREC 1 182 #define LASTREC_DELREC 2 183 184 185 union xfs_btree_irec { 186 struct xfs_alloc_rec_incore a; 187 struct xfs_bmbt_irec b; 188 struct xfs_inobt_rec_incore i; 189 struct xfs_rmap_irec r; 190 struct xfs_refcount_irec rc; 191 }; 192 193 /* Per-AG btree private information. */ 194 union xfs_btree_cur_private { 195 struct { 196 unsigned long nr_ops; /* # record updates */ 197 int shape_changes; /* # of extent splits */ 198 } refc; 199 }; 200 201 /* 202 * Btree cursor structure. 203 * This collects all information needed by the btree code in one place. 204 */ 205 typedef struct xfs_btree_cur 206 { 207 struct xfs_trans *bc_tp; /* transaction we're in, if any */ 208 struct xfs_mount *bc_mp; /* file system mount struct */ 209 const struct xfs_btree_ops *bc_ops; 210 uint bc_flags; /* btree features - below */ 211 union xfs_btree_irec bc_rec; /* current insert/search record value */ 212 struct xfs_buf *bc_bufs[XFS_BTREE_MAXLEVELS]; /* buf ptr per level */ 213 int bc_ptrs[XFS_BTREE_MAXLEVELS]; /* key/record # */ 214 uint8_t bc_ra[XFS_BTREE_MAXLEVELS]; /* readahead bits */ 215 #define XFS_BTCUR_LEFTRA 1 /* left sibling has been read-ahead */ 216 #define XFS_BTCUR_RIGHTRA 2 /* right sibling has been read-ahead */ 217 uint8_t bc_nlevels; /* number of levels in the tree */ 218 uint8_t bc_blocklog; /* log2(blocksize) of btree blocks */ 219 xfs_btnum_t bc_btnum; /* identifies which btree type */ 220 int bc_statoff; /* offset of btre stats array */ 221 union { 222 struct { /* needed for BNO, CNT, INO */ 223 struct xfs_buf *agbp; /* agf/agi buffer pointer */ 224 struct xfs_defer_ops *dfops; /* deferred updates */ 225 xfs_agnumber_t agno; /* ag number */ 226 union xfs_btree_cur_private priv; 227 } a; 228 struct { /* needed for BMAP */ 229 struct xfs_inode *ip; /* pointer to our inode */ 230 struct xfs_defer_ops *dfops; /* deferred updates */ 231 xfs_fsblock_t firstblock; /* 1st blk allocated */ 232 int allocated; /* count of alloced */ 233 short forksize; /* fork's inode space */ 234 char whichfork; /* data or attr fork */ 235 char flags; /* flags */ 236 #define XFS_BTCUR_BPRV_WASDEL 1 /* was delayed */ 237 } b; 238 } bc_private; /* per-btree type data */ 239 } xfs_btree_cur_t; 240 241 /* cursor flags */ 242 #define XFS_BTREE_LONG_PTRS (1<<0) /* pointers are 64bits long */ 243 #define XFS_BTREE_ROOT_IN_INODE (1<<1) /* root may be variable size */ 244 #define XFS_BTREE_LASTREC_UPDATE (1<<2) /* track last rec externally */ 245 #define XFS_BTREE_CRC_BLOCKS (1<<3) /* uses extended btree blocks */ 246 #define XFS_BTREE_OVERLAPPING (1<<4) /* overlapping intervals */ 247 248 249 #define XFS_BTREE_NOERROR 0 250 #define XFS_BTREE_ERROR 1 251 252 /* 253 * Convert from buffer to btree block header. 254 */ 255 #define XFS_BUF_TO_BLOCK(bp) ((struct xfs_btree_block *)((bp)->b_addr)) 256 257 258 /* 259 * Check that block header is ok. 260 */ 261 int 262 xfs_btree_check_block( 263 struct xfs_btree_cur *cur, /* btree cursor */ 264 struct xfs_btree_block *block, /* generic btree block pointer */ 265 int level, /* level of the btree block */ 266 struct xfs_buf *bp); /* buffer containing block, if any */ 267 268 /* 269 * Check that (long) pointer is ok. 270 */ 271 int /* error (0 or EFSCORRUPTED) */ 272 xfs_btree_check_lptr( 273 struct xfs_btree_cur *cur, /* btree cursor */ 274 xfs_fsblock_t ptr, /* btree block disk address */ 275 int level); /* btree block level */ 276 277 /* 278 * Delete the btree cursor. 279 */ 280 void 281 xfs_btree_del_cursor( 282 xfs_btree_cur_t *cur, /* btree cursor */ 283 int error); /* del because of error */ 284 285 /* 286 * Duplicate the btree cursor. 287 * Allocate a new one, copy the record, re-get the buffers. 288 */ 289 int /* error */ 290 xfs_btree_dup_cursor( 291 xfs_btree_cur_t *cur, /* input cursor */ 292 xfs_btree_cur_t **ncur);/* output cursor */ 293 294 /* 295 * Get a buffer for the block, return it with no data read. 296 * Long-form addressing. 297 */ 298 struct xfs_buf * /* buffer for fsbno */ 299 xfs_btree_get_bufl( 300 struct xfs_mount *mp, /* file system mount point */ 301 struct xfs_trans *tp, /* transaction pointer */ 302 xfs_fsblock_t fsbno, /* file system block number */ 303 uint lock); /* lock flags for get_buf */ 304 305 /* 306 * Get a buffer for the block, return it with no data read. 307 * Short-form addressing. 308 */ 309 struct xfs_buf * /* buffer for agno/agbno */ 310 xfs_btree_get_bufs( 311 struct xfs_mount *mp, /* file system mount point */ 312 struct xfs_trans *tp, /* transaction pointer */ 313 xfs_agnumber_t agno, /* allocation group number */ 314 xfs_agblock_t agbno, /* allocation group block number */ 315 uint lock); /* lock flags for get_buf */ 316 317 /* 318 * Check for the cursor referring to the last block at the given level. 319 */ 320 int /* 1=is last block, 0=not last block */ 321 xfs_btree_islastblock( 322 xfs_btree_cur_t *cur, /* btree cursor */ 323 int level); /* level to check */ 324 325 /* 326 * Compute first and last byte offsets for the fields given. 327 * Interprets the offsets table, which contains struct field offsets. 328 */ 329 void 330 xfs_btree_offsets( 331 int64_t fields, /* bitmask of fields */ 332 const short *offsets,/* table of field offsets */ 333 int nbits, /* number of bits to inspect */ 334 int *first, /* output: first byte offset */ 335 int *last); /* output: last byte offset */ 336 337 /* 338 * Get a buffer for the block, return it read in. 339 * Long-form addressing. 340 */ 341 int /* error */ 342 xfs_btree_read_bufl( 343 struct xfs_mount *mp, /* file system mount point */ 344 struct xfs_trans *tp, /* transaction pointer */ 345 xfs_fsblock_t fsbno, /* file system block number */ 346 uint lock, /* lock flags for read_buf */ 347 struct xfs_buf **bpp, /* buffer for fsbno */ 348 int refval, /* ref count value for buffer */ 349 const struct xfs_buf_ops *ops); 350 351 /* 352 * Read-ahead the block, don't wait for it, don't return a buffer. 353 * Long-form addressing. 354 */ 355 void /* error */ 356 xfs_btree_reada_bufl( 357 struct xfs_mount *mp, /* file system mount point */ 358 xfs_fsblock_t fsbno, /* file system block number */ 359 xfs_extlen_t count, /* count of filesystem blocks */ 360 const struct xfs_buf_ops *ops); 361 362 /* 363 * Read-ahead the block, don't wait for it, don't return a buffer. 364 * Short-form addressing. 365 */ 366 void /* error */ 367 xfs_btree_reada_bufs( 368 struct xfs_mount *mp, /* file system mount point */ 369 xfs_agnumber_t agno, /* allocation group number */ 370 xfs_agblock_t agbno, /* allocation group block number */ 371 xfs_extlen_t count, /* count of filesystem blocks */ 372 const struct xfs_buf_ops *ops); 373 374 /* 375 * Initialise a new btree block header 376 */ 377 void 378 xfs_btree_init_block( 379 struct xfs_mount *mp, 380 struct xfs_buf *bp, 381 xfs_btnum_t btnum, 382 __u16 level, 383 __u16 numrecs, 384 __u64 owner, 385 unsigned int flags); 386 387 void 388 xfs_btree_init_block_int( 389 struct xfs_mount *mp, 390 struct xfs_btree_block *buf, 391 xfs_daddr_t blkno, 392 xfs_btnum_t btnum, 393 __u16 level, 394 __u16 numrecs, 395 __u64 owner, 396 unsigned int flags); 397 398 /* 399 * Common btree core entry points. 400 */ 401 int xfs_btree_increment(struct xfs_btree_cur *, int, int *); 402 int xfs_btree_decrement(struct xfs_btree_cur *, int, int *); 403 int xfs_btree_lookup(struct xfs_btree_cur *, xfs_lookup_t, int *); 404 int xfs_btree_update(struct xfs_btree_cur *, union xfs_btree_rec *); 405 int xfs_btree_new_iroot(struct xfs_btree_cur *, int *, int *); 406 int xfs_btree_insert(struct xfs_btree_cur *, int *); 407 int xfs_btree_delete(struct xfs_btree_cur *, int *); 408 int xfs_btree_get_rec(struct xfs_btree_cur *, union xfs_btree_rec **, int *); 409 int xfs_btree_change_owner(struct xfs_btree_cur *cur, uint64_t new_owner, 410 struct list_head *buffer_list); 411 412 /* 413 * btree block CRC helpers 414 */ 415 void xfs_btree_lblock_calc_crc(struct xfs_buf *); 416 bool xfs_btree_lblock_verify_crc(struct xfs_buf *); 417 void xfs_btree_sblock_calc_crc(struct xfs_buf *); 418 bool xfs_btree_sblock_verify_crc(struct xfs_buf *); 419 420 /* 421 * Internal btree helpers also used by xfs_bmap.c. 422 */ 423 void xfs_btree_log_block(struct xfs_btree_cur *, struct xfs_buf *, int); 424 void xfs_btree_log_recs(struct xfs_btree_cur *, struct xfs_buf *, int, int); 425 426 /* 427 * Helpers. 428 */ 429 static inline int xfs_btree_get_numrecs(struct xfs_btree_block *block) 430 { 431 return be16_to_cpu(block->bb_numrecs); 432 } 433 434 static inline void xfs_btree_set_numrecs(struct xfs_btree_block *block, 435 uint16_t numrecs) 436 { 437 block->bb_numrecs = cpu_to_be16(numrecs); 438 } 439 440 static inline int xfs_btree_get_level(struct xfs_btree_block *block) 441 { 442 return be16_to_cpu(block->bb_level); 443 } 444 445 446 /* 447 * Min and max functions for extlen, agblock, fileoff, and filblks types. 448 */ 449 #define XFS_EXTLEN_MIN(a,b) min_t(xfs_extlen_t, (a), (b)) 450 #define XFS_EXTLEN_MAX(a,b) max_t(xfs_extlen_t, (a), (b)) 451 #define XFS_AGBLOCK_MIN(a,b) min_t(xfs_agblock_t, (a), (b)) 452 #define XFS_AGBLOCK_MAX(a,b) max_t(xfs_agblock_t, (a), (b)) 453 #define XFS_FILEOFF_MIN(a,b) min_t(xfs_fileoff_t, (a), (b)) 454 #define XFS_FILEOFF_MAX(a,b) max_t(xfs_fileoff_t, (a), (b)) 455 #define XFS_FILBLKS_MIN(a,b) min_t(xfs_filblks_t, (a), (b)) 456 #define XFS_FILBLKS_MAX(a,b) max_t(xfs_filblks_t, (a), (b)) 457 458 #define XFS_FSB_SANITY_CHECK(mp,fsb) \ 459 (fsb && XFS_FSB_TO_AGNO(mp, fsb) < mp->m_sb.sb_agcount && \ 460 XFS_FSB_TO_AGBNO(mp, fsb) < mp->m_sb.sb_agblocks) 461 462 /* 463 * Trace hooks. Currently not implemented as they need to be ported 464 * over to the generic tracing functionality, which is some effort. 465 * 466 * i,j = integer (32 bit) 467 * b = btree block buffer (xfs_buf_t) 468 * p = btree ptr 469 * r = btree record 470 * k = btree key 471 */ 472 #define XFS_BTREE_TRACE_ARGBI(c, b, i) 473 #define XFS_BTREE_TRACE_ARGBII(c, b, i, j) 474 #define XFS_BTREE_TRACE_ARGI(c, i) 475 #define XFS_BTREE_TRACE_ARGIPK(c, i, p, s) 476 #define XFS_BTREE_TRACE_ARGIPR(c, i, p, r) 477 #define XFS_BTREE_TRACE_ARGIK(c, i, k) 478 #define XFS_BTREE_TRACE_ARGR(c, r) 479 #define XFS_BTREE_TRACE_CURSOR(c, t) 480 481 bool xfs_btree_sblock_v5hdr_verify(struct xfs_buf *bp); 482 bool xfs_btree_sblock_verify(struct xfs_buf *bp, unsigned int max_recs); 483 uint xfs_btree_compute_maxlevels(struct xfs_mount *mp, uint *limits, 484 unsigned long len); 485 xfs_extlen_t xfs_btree_calc_size(struct xfs_mount *mp, uint *limits, 486 unsigned long long len); 487 488 /* return codes */ 489 #define XFS_BTREE_QUERY_RANGE_CONTINUE 0 /* keep iterating */ 490 #define XFS_BTREE_QUERY_RANGE_ABORT 1 /* stop iterating */ 491 typedef int (*xfs_btree_query_range_fn)(struct xfs_btree_cur *cur, 492 union xfs_btree_rec *rec, void *priv); 493 494 int xfs_btree_query_range(struct xfs_btree_cur *cur, 495 union xfs_btree_irec *low_rec, union xfs_btree_irec *high_rec, 496 xfs_btree_query_range_fn fn, void *priv); 497 int xfs_btree_query_all(struct xfs_btree_cur *cur, xfs_btree_query_range_fn fn, 498 void *priv); 499 500 typedef int (*xfs_btree_visit_blocks_fn)(struct xfs_btree_cur *cur, int level, 501 void *data); 502 int xfs_btree_visit_blocks(struct xfs_btree_cur *cur, 503 xfs_btree_visit_blocks_fn fn, void *data); 504 505 int xfs_btree_count_blocks(struct xfs_btree_cur *cur, xfs_extlen_t *blocks); 506 507 union xfs_btree_rec *xfs_btree_rec_addr(struct xfs_btree_cur *cur, int n, 508 struct xfs_btree_block *block); 509 union xfs_btree_key *xfs_btree_key_addr(struct xfs_btree_cur *cur, int n, 510 struct xfs_btree_block *block); 511 union xfs_btree_key *xfs_btree_high_key_addr(struct xfs_btree_cur *cur, int n, 512 struct xfs_btree_block *block); 513 union xfs_btree_ptr *xfs_btree_ptr_addr(struct xfs_btree_cur *cur, int n, 514 struct xfs_btree_block *block); 515 int xfs_btree_lookup_get_block(struct xfs_btree_cur *cur, int level, 516 union xfs_btree_ptr *pp, struct xfs_btree_block **blkp); 517 struct xfs_btree_block *xfs_btree_get_block(struct xfs_btree_cur *cur, 518 int level, struct xfs_buf **bpp); 519 520 #endif /* __XFS_BTREE_H__ */ 521