1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Copyright (C) 2007 Oracle. All rights reserved. 4 */ 5 6 #include "messages.h" 7 #include "ctree.h" 8 #include "disk-io.h" 9 #include "file-item.h" 10 #include "print-tree.h" 11 #include "accessors.h" 12 #include "tree-checker.h" 13 #include "volumes.h" 14 #include "raid-stripe-tree.h" 15 16 /* 17 * Large enough buffer size for the stringification of any key type yet short 18 * enough to use the stack and avoid allocations. 19 */ 20 #define KEY_TYPE_BUF_SIZE 32 21 22 struct root_name_map { 23 u64 id; 24 const char *name; 25 }; 26 27 static const struct root_name_map root_map[] = { 28 { BTRFS_ROOT_TREE_OBJECTID, "ROOT_TREE" }, 29 { BTRFS_EXTENT_TREE_OBJECTID, "EXTENT_TREE" }, 30 { BTRFS_CHUNK_TREE_OBJECTID, "CHUNK_TREE" }, 31 { BTRFS_DEV_TREE_OBJECTID, "DEV_TREE" }, 32 { BTRFS_FS_TREE_OBJECTID, "FS_TREE" }, 33 { BTRFS_CSUM_TREE_OBJECTID, "CSUM_TREE" }, 34 { BTRFS_TREE_LOG_OBJECTID, "TREE_LOG" }, 35 { BTRFS_QUOTA_TREE_OBJECTID, "QUOTA_TREE" }, 36 { BTRFS_UUID_TREE_OBJECTID, "UUID_TREE" }, 37 { BTRFS_FREE_SPACE_TREE_OBJECTID, "FREE_SPACE_TREE" }, 38 { BTRFS_BLOCK_GROUP_TREE_OBJECTID, "BLOCK_GROUP_TREE" }, 39 { BTRFS_DATA_RELOC_TREE_OBJECTID, "DATA_RELOC_TREE" }, 40 { BTRFS_RAID_STRIPE_TREE_OBJECTID, "RAID_STRIPE_TREE" }, 41 }; 42 43 const char *btrfs_root_name(const struct btrfs_key *key, char *buf) 44 { 45 int i; 46 47 if (key->objectid == BTRFS_TREE_RELOC_OBJECTID) { 48 snprintf(buf, BTRFS_ROOT_NAME_BUF_LEN, 49 "TREE_RELOC offset=%llu", key->offset); 50 return buf; 51 } 52 53 for (i = 0; i < ARRAY_SIZE(root_map); i++) { 54 if (root_map[i].id == key->objectid) 55 return root_map[i].name; 56 } 57 58 snprintf(buf, BTRFS_ROOT_NAME_BUF_LEN, "%llu", key->objectid); 59 return buf; 60 } 61 62 static void print_chunk(const struct extent_buffer *eb, struct btrfs_chunk *chunk) 63 { 64 int num_stripes = btrfs_chunk_num_stripes(eb, chunk); 65 int i; 66 pr_info("\t\tchunk length %llu owner %llu type %llu num_stripes %d\n", 67 btrfs_chunk_length(eb, chunk), btrfs_chunk_owner(eb, chunk), 68 btrfs_chunk_type(eb, chunk), num_stripes); 69 for (i = 0 ; i < num_stripes ; i++) { 70 pr_info("\t\t\tstripe %d devid %llu offset %llu\n", i, 71 btrfs_stripe_devid_nr(eb, chunk, i), 72 btrfs_stripe_offset_nr(eb, chunk, i)); 73 } 74 } 75 static void print_dev_item(const struct extent_buffer *eb, 76 struct btrfs_dev_item *dev_item) 77 { 78 pr_info("\t\tdev item devid %llu total_bytes %llu bytes used %llu\n", 79 btrfs_device_id(eb, dev_item), 80 btrfs_device_total_bytes(eb, dev_item), 81 btrfs_device_bytes_used(eb, dev_item)); 82 } 83 static void print_extent_data_ref(const struct extent_buffer *eb, 84 struct btrfs_extent_data_ref *ref) 85 { 86 pr_cont("extent data backref root %llu objectid %llu offset %llu count %u\n", 87 btrfs_extent_data_ref_root(eb, ref), 88 btrfs_extent_data_ref_objectid(eb, ref), 89 btrfs_extent_data_ref_offset(eb, ref), 90 btrfs_extent_data_ref_count(eb, ref)); 91 } 92 93 static void print_extent_owner_ref(const struct extent_buffer *eb, 94 const struct btrfs_extent_owner_ref *ref) 95 { 96 ASSERT(btrfs_fs_incompat(eb->fs_info, SIMPLE_QUOTA)); 97 pr_cont("extent data owner root %llu\n", btrfs_extent_owner_ref_root_id(eb, ref)); 98 } 99 100 static void print_extent_item(const struct extent_buffer *eb, int slot, int type) 101 { 102 struct btrfs_extent_item *ei; 103 struct btrfs_extent_inline_ref *iref; 104 struct btrfs_extent_data_ref *dref; 105 struct btrfs_shared_data_ref *sref; 106 struct btrfs_extent_owner_ref *oref; 107 struct btrfs_disk_key key; 108 unsigned long end; 109 unsigned long ptr; 110 u32 item_size = btrfs_item_size(eb, slot); 111 u64 flags; 112 u64 offset; 113 int ref_index = 0; 114 115 if (unlikely(item_size < sizeof(*ei))) { 116 btrfs_err(eb->fs_info, 117 "unexpected extent item size, has %u expect >= %zu", 118 item_size, sizeof(*ei)); 119 return; 120 } 121 122 ei = btrfs_item_ptr(eb, slot, struct btrfs_extent_item); 123 flags = btrfs_extent_flags(eb, ei); 124 125 pr_info("\t\textent refs %llu gen %llu flags %llu\n", 126 btrfs_extent_refs(eb, ei), btrfs_extent_generation(eb, ei), 127 flags); 128 129 if ((type == BTRFS_EXTENT_ITEM_KEY) && 130 flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) { 131 struct btrfs_tree_block_info *info; 132 info = (struct btrfs_tree_block_info *)(ei + 1); 133 btrfs_tree_block_key(eb, info, &key); 134 pr_info("\t\ttree block key (%llu %u %llu) level %d\n", 135 btrfs_disk_key_objectid(&key), key.type, 136 btrfs_disk_key_offset(&key), 137 btrfs_tree_block_level(eb, info)); 138 iref = (struct btrfs_extent_inline_ref *)(info + 1); 139 } else { 140 iref = (struct btrfs_extent_inline_ref *)(ei + 1); 141 } 142 143 ptr = (unsigned long)iref; 144 end = (unsigned long)ei + item_size; 145 while (ptr < end) { 146 iref = (struct btrfs_extent_inline_ref *)ptr; 147 type = btrfs_extent_inline_ref_type(eb, iref); 148 offset = btrfs_extent_inline_ref_offset(eb, iref); 149 pr_info("\t\tref#%d: ", ref_index++); 150 switch (type) { 151 case BTRFS_TREE_BLOCK_REF_KEY: 152 pr_cont("tree block backref root %llu\n", offset); 153 break; 154 case BTRFS_SHARED_BLOCK_REF_KEY: 155 pr_cont("shared block backref parent %llu\n", offset); 156 /* 157 * offset is supposed to be a tree block which 158 * must be aligned to nodesize. 159 */ 160 if (!IS_ALIGNED(offset, eb->fs_info->sectorsize)) 161 pr_info( 162 "\t\t\t(parent %llu not aligned to sectorsize %u)\n", 163 offset, eb->fs_info->sectorsize); 164 break; 165 case BTRFS_EXTENT_DATA_REF_KEY: 166 dref = (struct btrfs_extent_data_ref *)(&iref->offset); 167 print_extent_data_ref(eb, dref); 168 break; 169 case BTRFS_SHARED_DATA_REF_KEY: 170 sref = (struct btrfs_shared_data_ref *)(iref + 1); 171 pr_cont("shared data backref parent %llu count %u\n", 172 offset, btrfs_shared_data_ref_count(eb, sref)); 173 /* 174 * Offset is supposed to be a tree block which must be 175 * aligned to sectorsize. 176 */ 177 if (!IS_ALIGNED(offset, eb->fs_info->sectorsize)) 178 pr_info( 179 "\t\t\t(parent %llu not aligned to sectorsize %u)\n", 180 offset, eb->fs_info->sectorsize); 181 break; 182 case BTRFS_EXTENT_OWNER_REF_KEY: 183 oref = (struct btrfs_extent_owner_ref *)(&iref->offset); 184 print_extent_owner_ref(eb, oref); 185 break; 186 default: 187 pr_cont("(extent %llu has INVALID ref type %d)\n", 188 eb->start, type); 189 return; 190 } 191 ptr += btrfs_extent_inline_ref_size(type); 192 } 193 WARN_ON(ptr > end); 194 } 195 196 static void print_uuid_item(const struct extent_buffer *l, unsigned long offset, 197 u32 item_size) 198 { 199 if (!IS_ALIGNED(item_size, sizeof(u64))) { 200 btrfs_warn(l->fs_info, "uuid item with illegal size %lu", 201 (unsigned long)item_size); 202 return; 203 } 204 while (item_size) { 205 __le64 subvol_id; 206 207 read_extent_buffer(l, &subvol_id, offset, sizeof(subvol_id)); 208 pr_info("\t\tsubvol_id %llu\n", le64_to_cpu(subvol_id)); 209 item_size -= sizeof(u64); 210 offset += sizeof(u64); 211 } 212 } 213 214 static void print_raid_stripe_key(const struct extent_buffer *eb, u32 item_size, 215 struct btrfs_stripe_extent *stripe) 216 { 217 const int num_stripes = btrfs_num_raid_stripes(item_size); 218 219 for (int i = 0; i < num_stripes; i++) 220 pr_info("\t\t\tstride %d devid %llu physical %llu\n", 221 i, btrfs_raid_stride_devid(eb, &stripe->strides[i]), 222 btrfs_raid_stride_physical(eb, &stripe->strides[i])); 223 } 224 225 /* 226 * Helper to output refs and locking status of extent buffer. Useful to debug 227 * race condition related problems. 228 */ 229 static void print_eb_refs_lock(const struct extent_buffer *eb) 230 { 231 #ifdef CONFIG_BTRFS_DEBUG 232 btrfs_info(eb->fs_info, "refs %u lock_owner %u current %u", 233 refcount_read(&eb->refs), eb->lock_owner, current->pid); 234 #endif 235 } 236 237 static void print_timespec(const struct extent_buffer *eb, 238 struct btrfs_timespec *timespec, 239 const char *prefix, const char *suffix) 240 { 241 const u64 secs = btrfs_timespec_sec(eb, timespec); 242 const u32 nsecs = btrfs_timespec_nsec(eb, timespec); 243 244 pr_info("%s%llu.%u%s", prefix, secs, nsecs, suffix); 245 } 246 247 static void print_inode_item(const struct extent_buffer *eb, int i) 248 { 249 struct btrfs_inode_item *ii = btrfs_item_ptr(eb, i, struct btrfs_inode_item); 250 251 pr_info("\t\tinode generation %llu transid %llu size %llu nbytes %llu\n", 252 btrfs_inode_generation(eb, ii), btrfs_inode_transid(eb, ii), 253 btrfs_inode_size(eb, ii), btrfs_inode_nbytes(eb, ii)); 254 pr_info("\t\tblock group %llu mode %o links %u uid %u gid %u\n", 255 btrfs_inode_block_group(eb, ii), btrfs_inode_mode(eb, ii), 256 btrfs_inode_nlink(eb, ii), btrfs_inode_uid(eb, ii), 257 btrfs_inode_gid(eb, ii)); 258 pr_info("\t\trdev %llu sequence %llu flags 0x%llx\n", 259 btrfs_inode_rdev(eb, ii), btrfs_inode_sequence(eb, ii), 260 btrfs_inode_flags(eb, ii)); 261 print_timespec(eb, &ii->atime, "\t\tatime ", "\n"); 262 print_timespec(eb, &ii->ctime, "\t\tctime ", "\n"); 263 print_timespec(eb, &ii->mtime, "\t\tmtime ", "\n"); 264 print_timespec(eb, &ii->otime, "\t\totime ", "\n"); 265 } 266 267 static void print_dir_item(const struct extent_buffer *eb, int i) 268 { 269 const u32 size = btrfs_item_size(eb, i); 270 struct btrfs_dir_item *di = btrfs_item_ptr(eb, i, struct btrfs_dir_item); 271 u32 cur = 0; 272 273 while (cur < size) { 274 const u32 name_len = btrfs_dir_name_len(eb, di); 275 const u32 data_len = btrfs_dir_data_len(eb, di); 276 const u32 len = sizeof(*di) + name_len + data_len; 277 struct btrfs_key location; 278 279 btrfs_dir_item_key_to_cpu(eb, di, &location); 280 pr_info("\t\tlocation key (%llu %u %llu) type %d\n", 281 location.objectid, location.type, location.offset, 282 btrfs_dir_ftype(eb, di)); 283 pr_info("\t\ttransid %llu data_len %u name_len %u\n", 284 btrfs_dir_transid(eb, di), data_len, name_len); 285 di = (struct btrfs_dir_item *)((char *)di + len); 286 cur += len; 287 } 288 } 289 290 static void print_inode_ref_item(const struct extent_buffer *eb, int i) 291 { 292 const u32 size = btrfs_item_size(eb, i); 293 struct btrfs_inode_ref *ref = btrfs_item_ptr(eb, i, struct btrfs_inode_ref); 294 u32 cur = 0; 295 296 while (cur < size) { 297 const u64 index = btrfs_inode_ref_index(eb, ref); 298 const u32 name_len = btrfs_inode_ref_name_len(eb, ref); 299 const u32 len = sizeof(*ref) + name_len; 300 301 pr_info("\t\tindex %llu name_len %u\n", index, name_len); 302 ref = (struct btrfs_inode_ref *)((char *)ref + len); 303 cur += len; 304 } 305 } 306 307 static void print_inode_extref_item(const struct extent_buffer *eb, int i) 308 { 309 const u32 size = btrfs_item_size(eb, i); 310 struct btrfs_inode_extref *extref; 311 u32 cur = 0; 312 313 extref = btrfs_item_ptr(eb, i, struct btrfs_inode_extref); 314 while (cur < size) { 315 const u64 index = btrfs_inode_extref_index(eb, extref); 316 const u32 name_len = btrfs_inode_extref_name_len(eb, extref); 317 const u64 parent = btrfs_inode_extref_parent(eb, extref); 318 const u32 len = sizeof(*extref) + name_len; 319 320 pr_info("\t\tindex %llu parent %llu name_len %u\n", 321 index, parent, name_len); 322 extref = (struct btrfs_inode_extref *)((char *)extref + len); 323 cur += len; 324 } 325 } 326 327 static void print_dir_log_index_item(const struct extent_buffer *eb, int i) 328 { 329 struct btrfs_dir_log_item *dlog; 330 331 dlog = btrfs_item_ptr(eb, i, struct btrfs_dir_log_item); 332 pr_info("\t\tdir log end %llu\n", btrfs_dir_log_end(eb, dlog)); 333 } 334 335 static void print_extent_csum(const struct extent_buffer *eb, int i) 336 { 337 const struct btrfs_fs_info *fs_info = eb->fs_info; 338 const u32 size = btrfs_item_size(eb, i); 339 const u32 csum_bytes = (size / fs_info->csum_size) * fs_info->sectorsize; 340 struct btrfs_key key; 341 342 btrfs_item_key_to_cpu(eb, &key, i); 343 pr_info("\t\trange start %llu end %llu length %u\n", 344 key.offset, key.offset + csum_bytes, csum_bytes); 345 } 346 347 static void print_file_extent_item(const struct extent_buffer *eb, int i) 348 { 349 struct btrfs_file_extent_item *fi; 350 351 fi = btrfs_item_ptr(eb, i, struct btrfs_file_extent_item); 352 pr_info("\t\tgeneration %llu type %hhu\n", 353 btrfs_file_extent_generation(eb, fi), 354 btrfs_file_extent_type(eb, fi)); 355 356 if (btrfs_file_extent_type(eb, fi) == BTRFS_FILE_EXTENT_INLINE) { 357 pr_info("\t\tinline extent data size %u ram_bytes %llu compression %hhu\n", 358 btrfs_file_extent_inline_item_len(eb, i), 359 btrfs_file_extent_ram_bytes(eb, fi), 360 btrfs_file_extent_compression(eb, fi)); 361 return; 362 } 363 364 pr_info("\t\textent data disk bytenr %llu nr %llu\n", 365 btrfs_file_extent_disk_bytenr(eb, fi), 366 btrfs_file_extent_disk_num_bytes(eb, fi)); 367 pr_info("\t\textent data offset %llu nr %llu ram %llu\n", 368 btrfs_file_extent_offset(eb, fi), 369 btrfs_file_extent_num_bytes(eb, fi), 370 btrfs_file_extent_ram_bytes(eb, fi)); 371 pr_info("\t\textent compression %hhu\n", 372 btrfs_file_extent_compression(eb, fi)); 373 } 374 375 static void key_type_string(const struct btrfs_key *key, char *buf, int buf_size) 376 { 377 static const char *key_to_str[256] = { 378 [BTRFS_INODE_ITEM_KEY] = "INODE_ITEM", 379 [BTRFS_INODE_REF_KEY] = "INODE_REF", 380 [BTRFS_INODE_EXTREF_KEY] = "INODE_EXTREF", 381 [BTRFS_DIR_ITEM_KEY] = "DIR_ITEM", 382 [BTRFS_DIR_INDEX_KEY] = "DIR_INDEX", 383 [BTRFS_DIR_LOG_ITEM_KEY] = "DIR_LOG_ITEM", 384 [BTRFS_DIR_LOG_INDEX_KEY] = "DIR_LOG_INDEX", 385 [BTRFS_XATTR_ITEM_KEY] = "XATTR_ITEM", 386 [BTRFS_VERITY_DESC_ITEM_KEY] = "VERITY_DESC_ITEM", 387 [BTRFS_VERITY_MERKLE_ITEM_KEY] = "VERITY_MERKLE_ITEM", 388 [BTRFS_ORPHAN_ITEM_KEY] = "ORPHAN_ITEM", 389 [BTRFS_ROOT_ITEM_KEY] = "ROOT_ITEM", 390 [BTRFS_ROOT_REF_KEY] = "ROOT_REF", 391 [BTRFS_ROOT_BACKREF_KEY] = "ROOT_BACKREF", 392 [BTRFS_EXTENT_ITEM_KEY] = "EXTENT_ITEM", 393 [BTRFS_METADATA_ITEM_KEY] = "METADATA_ITEM", 394 [BTRFS_TREE_BLOCK_REF_KEY] = "TREE_BLOCK_REF", 395 [BTRFS_SHARED_BLOCK_REF_KEY] = "SHARED_BLOCK_REF", 396 [BTRFS_EXTENT_DATA_REF_KEY] = "EXTENT_DATA_REF", 397 [BTRFS_SHARED_DATA_REF_KEY] = "SHARED_DATA_REF", 398 [BTRFS_EXTENT_OWNER_REF_KEY] = "EXTENT_OWNER_REF", 399 [BTRFS_EXTENT_CSUM_KEY] = "EXTENT_CSUM", 400 [BTRFS_EXTENT_DATA_KEY] = "EXTENT_DATA", 401 [BTRFS_BLOCK_GROUP_ITEM_KEY] = "BLOCK_GROUP_ITEM", 402 [BTRFS_FREE_SPACE_INFO_KEY] = "FREE_SPACE_INFO", 403 [BTRFS_FREE_SPACE_EXTENT_KEY] = "FREE_SPACE_EXTENT", 404 [BTRFS_FREE_SPACE_BITMAP_KEY] = "FREE_SPACE_BITMAP", 405 [BTRFS_CHUNK_ITEM_KEY] = "CHUNK_ITEM", 406 [BTRFS_DEV_ITEM_KEY] = "DEV_ITEM", 407 [BTRFS_DEV_EXTENT_KEY] = "DEV_EXTENT", 408 [BTRFS_TEMPORARY_ITEM_KEY] = "TEMPORARY_ITEM", 409 [BTRFS_DEV_REPLACE_KEY] = "DEV_REPLACE", 410 [BTRFS_STRING_ITEM_KEY] = "STRING_ITEM", 411 [BTRFS_QGROUP_STATUS_KEY] = "QGROUP_STATUS", 412 [BTRFS_QGROUP_RELATION_KEY] = "QGROUP_RELATION", 413 [BTRFS_QGROUP_INFO_KEY] = "QGROUP_INFO", 414 [BTRFS_QGROUP_LIMIT_KEY] = "QGROUP_LIMIT", 415 [BTRFS_PERSISTENT_ITEM_KEY] = "PERSISTENT_ITEM", 416 [BTRFS_UUID_KEY_SUBVOL] = "UUID_KEY_SUBVOL", 417 [BTRFS_UUID_KEY_RECEIVED_SUBVOL] = "UUID_KEY_RECEIVED_SUBVOL", 418 [BTRFS_RAID_STRIPE_KEY] = "RAID_STRIPE", 419 }; 420 421 if (key->type == 0 && key->objectid == BTRFS_FREE_SPACE_OBJECTID) 422 scnprintf(buf, buf_size, "UNTYPED"); 423 else if (key_to_str[key->type]) 424 scnprintf(buf, buf_size, key_to_str[key->type]); 425 else 426 scnprintf(buf, buf_size, "UNKNOWN.%d", key->type); 427 } 428 429 void btrfs_print_leaf(const struct extent_buffer *l) 430 { 431 struct btrfs_fs_info *fs_info; 432 int i; 433 u32 type, nr; 434 struct btrfs_root_item *ri; 435 struct btrfs_block_group_item *bi; 436 struct btrfs_extent_data_ref *dref; 437 struct btrfs_shared_data_ref *sref; 438 struct btrfs_dev_extent *dev_extent; 439 struct btrfs_key key; 440 441 if (!l) 442 return; 443 444 fs_info = l->fs_info; 445 nr = btrfs_header_nritems(l); 446 447 btrfs_info(fs_info, 448 "leaf %llu gen %llu total ptrs %d free space %d owner %llu", 449 btrfs_header_bytenr(l), btrfs_header_generation(l), nr, 450 btrfs_leaf_free_space(l), btrfs_header_owner(l)); 451 print_eb_refs_lock(l); 452 for (i = 0 ; i < nr ; i++) { 453 char key_buf[KEY_TYPE_BUF_SIZE]; 454 455 btrfs_item_key_to_cpu(l, &key, i); 456 type = key.type; 457 key_type_string(&key, key_buf, KEY_TYPE_BUF_SIZE); 458 459 pr_info("\titem %d key (%llu %s %llu) itemoff %d itemsize %d\n", 460 i, key.objectid, key_buf, key.offset, 461 btrfs_item_offset(l, i), btrfs_item_size(l, i)); 462 switch (type) { 463 case BTRFS_INODE_ITEM_KEY: 464 print_inode_item(l, i); 465 break; 466 case BTRFS_INODE_REF_KEY: 467 print_inode_ref_item(l, i); 468 break; 469 case BTRFS_INODE_EXTREF_KEY: 470 print_inode_extref_item(l, i); 471 break; 472 case BTRFS_DIR_ITEM_KEY: 473 case BTRFS_DIR_INDEX_KEY: 474 case BTRFS_XATTR_ITEM_KEY: 475 print_dir_item(l, i); 476 break; 477 case BTRFS_DIR_LOG_INDEX_KEY: 478 print_dir_log_index_item(l, i); 479 break; 480 case BTRFS_EXTENT_CSUM_KEY: 481 print_extent_csum(l, i); 482 break; 483 case BTRFS_ROOT_ITEM_KEY: 484 ri = btrfs_item_ptr(l, i, struct btrfs_root_item); 485 pr_info("\t\troot data bytenr %llu refs %u\n", 486 btrfs_disk_root_bytenr(l, ri), 487 btrfs_disk_root_refs(l, ri)); 488 break; 489 case BTRFS_EXTENT_ITEM_KEY: 490 case BTRFS_METADATA_ITEM_KEY: 491 print_extent_item(l, i, type); 492 break; 493 case BTRFS_TREE_BLOCK_REF_KEY: 494 pr_info("\t\ttree block backref\n"); 495 break; 496 case BTRFS_SHARED_BLOCK_REF_KEY: 497 pr_info("\t\tshared block backref\n"); 498 break; 499 case BTRFS_EXTENT_DATA_REF_KEY: 500 dref = btrfs_item_ptr(l, i, 501 struct btrfs_extent_data_ref); 502 print_extent_data_ref(l, dref); 503 break; 504 case BTRFS_SHARED_DATA_REF_KEY: 505 sref = btrfs_item_ptr(l, i, 506 struct btrfs_shared_data_ref); 507 pr_info("\t\tshared data backref count %u\n", 508 btrfs_shared_data_ref_count(l, sref)); 509 break; 510 case BTRFS_EXTENT_DATA_KEY: 511 print_file_extent_item(l, i); 512 break; 513 case BTRFS_BLOCK_GROUP_ITEM_KEY: 514 bi = btrfs_item_ptr(l, i, 515 struct btrfs_block_group_item); 516 pr_info( 517 "\t\tblock group used %llu chunk_objectid %llu flags %llu\n", 518 btrfs_block_group_used(l, bi), 519 btrfs_block_group_chunk_objectid(l, bi), 520 btrfs_block_group_flags(l, bi)); 521 break; 522 case BTRFS_CHUNK_ITEM_KEY: 523 print_chunk(l, btrfs_item_ptr(l, i, 524 struct btrfs_chunk)); 525 break; 526 case BTRFS_DEV_ITEM_KEY: 527 print_dev_item(l, btrfs_item_ptr(l, i, 528 struct btrfs_dev_item)); 529 break; 530 case BTRFS_DEV_EXTENT_KEY: 531 dev_extent = btrfs_item_ptr(l, i, 532 struct btrfs_dev_extent); 533 pr_info("\t\tdev extent chunk_tree %llu\n\t\tchunk objectid %llu chunk offset %llu length %llu\n", 534 btrfs_dev_extent_chunk_tree(l, dev_extent), 535 btrfs_dev_extent_chunk_objectid(l, dev_extent), 536 btrfs_dev_extent_chunk_offset(l, dev_extent), 537 btrfs_dev_extent_length(l, dev_extent)); 538 break; 539 case BTRFS_PERSISTENT_ITEM_KEY: 540 pr_info("\t\tpersistent item objectid %llu offset %llu\n", 541 key.objectid, key.offset); 542 switch (key.objectid) { 543 case BTRFS_DEV_STATS_OBJECTID: 544 pr_info("\t\tdevice stats\n"); 545 break; 546 default: 547 pr_info("\t\tunknown persistent item\n"); 548 } 549 break; 550 case BTRFS_TEMPORARY_ITEM_KEY: 551 pr_info("\t\ttemporary item objectid %llu offset %llu\n", 552 key.objectid, key.offset); 553 switch (key.objectid) { 554 case BTRFS_BALANCE_OBJECTID: 555 pr_info("\t\tbalance status\n"); 556 break; 557 default: 558 pr_info("\t\tunknown temporary item\n"); 559 } 560 break; 561 case BTRFS_DEV_REPLACE_KEY: 562 pr_info("\t\tdev replace\n"); 563 break; 564 case BTRFS_UUID_KEY_SUBVOL: 565 case BTRFS_UUID_KEY_RECEIVED_SUBVOL: 566 print_uuid_item(l, btrfs_item_ptr_offset(l, i), 567 btrfs_item_size(l, i)); 568 break; 569 case BTRFS_RAID_STRIPE_KEY: 570 print_raid_stripe_key(l, btrfs_item_size(l, i), 571 btrfs_item_ptr(l, i, struct btrfs_stripe_extent)); 572 break; 573 } 574 } 575 } 576 577 void btrfs_print_tree(const struct extent_buffer *c, bool follow) 578 { 579 struct btrfs_fs_info *fs_info; 580 int i; u32 nr; 581 struct btrfs_key key; 582 int level; 583 584 if (!c) 585 return; 586 fs_info = c->fs_info; 587 nr = btrfs_header_nritems(c); 588 level = btrfs_header_level(c); 589 if (level == 0) { 590 btrfs_print_leaf(c); 591 return; 592 } 593 btrfs_info(fs_info, 594 "node %llu level %d gen %llu total ptrs %d free spc %u owner %llu", 595 btrfs_header_bytenr(c), level, btrfs_header_generation(c), 596 nr, (u32)BTRFS_NODEPTRS_PER_BLOCK(fs_info) - nr, 597 btrfs_header_owner(c)); 598 print_eb_refs_lock(c); 599 for (i = 0; i < nr; i++) { 600 btrfs_node_key_to_cpu(c, &key, i); 601 pr_info("\tkey %d (%llu %u %llu) block %llu gen %llu\n", 602 i, key.objectid, key.type, key.offset, 603 btrfs_node_blockptr(c, i), 604 btrfs_node_ptr_generation(c, i)); 605 } 606 if (!follow) 607 return; 608 for (i = 0; i < nr; i++) { 609 struct btrfs_tree_parent_check check = { 610 .level = level - 1, 611 .transid = btrfs_node_ptr_generation(c, i), 612 .owner_root = btrfs_header_owner(c), 613 .has_first_key = true 614 }; 615 struct extent_buffer *next; 616 617 btrfs_node_key_to_cpu(c, &check.first_key, i); 618 next = read_tree_block(fs_info, btrfs_node_blockptr(c, i), &check); 619 if (IS_ERR(next)) 620 continue; 621 if (!extent_buffer_uptodate(next)) { 622 free_extent_buffer(next); 623 continue; 624 } 625 626 if (btrfs_is_leaf(next) && 627 level != 1) 628 BUG(); 629 if (btrfs_header_level(next) != 630 level - 1) 631 BUG(); 632 btrfs_print_tree(next, follow); 633 free_extent_buffer(next); 634 } 635 } 636