Linux kernel mirror (for testing)
git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel
os
linux
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
22struct root_name_map {
23 u64 id;
24 const char *name;
25};
26
27static 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
43const 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
62static 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}
75static 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}
83static 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
93static 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
100static 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 " BTRFS_KEY_FMT " 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
196static 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
214static 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 */
229static 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
237static 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
247static 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
267static 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 " BTRFS_KEY_FMT " type %d\n",
281 BTRFS_KEY_FMT_VALUE(&location), btrfs_dir_ftype(eb, di));
282 pr_info("\t\ttransid %llu data_len %u name_len %u\n",
283 btrfs_dir_transid(eb, di), data_len, name_len);
284 di = (struct btrfs_dir_item *)((char *)di + len);
285 cur += len;
286 }
287}
288
289static void print_inode_ref_item(const struct extent_buffer *eb, int i)
290{
291 const u32 size = btrfs_item_size(eb, i);
292 struct btrfs_inode_ref *ref = btrfs_item_ptr(eb, i, struct btrfs_inode_ref);
293 u32 cur = 0;
294
295 while (cur < size) {
296 const u64 index = btrfs_inode_ref_index(eb, ref);
297 const u32 name_len = btrfs_inode_ref_name_len(eb, ref);
298 const u32 len = sizeof(*ref) + name_len;
299
300 pr_info("\t\tindex %llu name_len %u\n", index, name_len);
301 ref = (struct btrfs_inode_ref *)((char *)ref + len);
302 cur += len;
303 }
304}
305
306static void print_inode_extref_item(const struct extent_buffer *eb, int i)
307{
308 const u32 size = btrfs_item_size(eb, i);
309 struct btrfs_inode_extref *extref;
310 u32 cur = 0;
311
312 extref = btrfs_item_ptr(eb, i, struct btrfs_inode_extref);
313 while (cur < size) {
314 const u64 index = btrfs_inode_extref_index(eb, extref);
315 const u32 name_len = btrfs_inode_extref_name_len(eb, extref);
316 const u64 parent = btrfs_inode_extref_parent(eb, extref);
317 const u32 len = sizeof(*extref) + name_len;
318
319 pr_info("\t\tindex %llu parent %llu name_len %u\n",
320 index, parent, name_len);
321 extref = (struct btrfs_inode_extref *)((char *)extref + len);
322 cur += len;
323 }
324}
325
326static void print_dir_log_index_item(const struct extent_buffer *eb, int i)
327{
328 struct btrfs_dir_log_item *dlog;
329
330 dlog = btrfs_item_ptr(eb, i, struct btrfs_dir_log_item);
331 pr_info("\t\tdir log end %llu\n", btrfs_dir_log_end(eb, dlog));
332}
333
334static void print_extent_csum(const struct extent_buffer *eb, int i)
335{
336 const struct btrfs_fs_info *fs_info = eb->fs_info;
337 const u32 size = btrfs_item_size(eb, i);
338 const u32 csum_bytes = (size / fs_info->csum_size) * fs_info->sectorsize;
339 struct btrfs_key key;
340
341 btrfs_item_key_to_cpu(eb, &key, i);
342 pr_info("\t\trange start %llu end %llu length %u\n",
343 key.offset, key.offset + csum_bytes, csum_bytes);
344}
345
346static void print_file_extent_item(const struct extent_buffer *eb, int i)
347{
348 struct btrfs_file_extent_item *fi;
349
350 fi = btrfs_item_ptr(eb, i, struct btrfs_file_extent_item);
351 pr_info("\t\tgeneration %llu type %hhu\n",
352 btrfs_file_extent_generation(eb, fi),
353 btrfs_file_extent_type(eb, fi));
354
355 if (btrfs_file_extent_type(eb, fi) == BTRFS_FILE_EXTENT_INLINE) {
356 pr_info("\t\tinline extent data size %u ram_bytes %llu compression %hhu\n",
357 btrfs_file_extent_inline_item_len(eb, i),
358 btrfs_file_extent_ram_bytes(eb, fi),
359 btrfs_file_extent_compression(eb, fi));
360 return;
361 }
362
363 pr_info("\t\textent data disk bytenr %llu nr %llu\n",
364 btrfs_file_extent_disk_bytenr(eb, fi),
365 btrfs_file_extent_disk_num_bytes(eb, fi));
366 pr_info("\t\textent data offset %llu nr %llu ram %llu\n",
367 btrfs_file_extent_offset(eb, fi),
368 btrfs_file_extent_num_bytes(eb, fi),
369 btrfs_file_extent_ram_bytes(eb, fi));
370 pr_info("\t\textent compression %hhu\n",
371 btrfs_file_extent_compression(eb, fi));
372}
373
374static void key_type_string(const struct btrfs_key *key, char *buf, int buf_size)
375{
376 static const char *key_to_str[256] = {
377 [BTRFS_INODE_ITEM_KEY] = "INODE_ITEM",
378 [BTRFS_INODE_REF_KEY] = "INODE_REF",
379 [BTRFS_INODE_EXTREF_KEY] = "INODE_EXTREF",
380 [BTRFS_DIR_ITEM_KEY] = "DIR_ITEM",
381 [BTRFS_DIR_INDEX_KEY] = "DIR_INDEX",
382 [BTRFS_DIR_LOG_ITEM_KEY] = "DIR_LOG_ITEM",
383 [BTRFS_DIR_LOG_INDEX_KEY] = "DIR_LOG_INDEX",
384 [BTRFS_XATTR_ITEM_KEY] = "XATTR_ITEM",
385 [BTRFS_VERITY_DESC_ITEM_KEY] = "VERITY_DESC_ITEM",
386 [BTRFS_VERITY_MERKLE_ITEM_KEY] = "VERITY_MERKLE_ITEM",
387 [BTRFS_ORPHAN_ITEM_KEY] = "ORPHAN_ITEM",
388 [BTRFS_ROOT_ITEM_KEY] = "ROOT_ITEM",
389 [BTRFS_ROOT_REF_KEY] = "ROOT_REF",
390 [BTRFS_ROOT_BACKREF_KEY] = "ROOT_BACKREF",
391 [BTRFS_EXTENT_ITEM_KEY] = "EXTENT_ITEM",
392 [BTRFS_METADATA_ITEM_KEY] = "METADATA_ITEM",
393 [BTRFS_TREE_BLOCK_REF_KEY] = "TREE_BLOCK_REF",
394 [BTRFS_SHARED_BLOCK_REF_KEY] = "SHARED_BLOCK_REF",
395 [BTRFS_EXTENT_DATA_REF_KEY] = "EXTENT_DATA_REF",
396 [BTRFS_SHARED_DATA_REF_KEY] = "SHARED_DATA_REF",
397 [BTRFS_EXTENT_OWNER_REF_KEY] = "EXTENT_OWNER_REF",
398 [BTRFS_EXTENT_CSUM_KEY] = "EXTENT_CSUM",
399 [BTRFS_EXTENT_DATA_KEY] = "EXTENT_DATA",
400 [BTRFS_BLOCK_GROUP_ITEM_KEY] = "BLOCK_GROUP_ITEM",
401 [BTRFS_FREE_SPACE_INFO_KEY] = "FREE_SPACE_INFO",
402 [BTRFS_FREE_SPACE_EXTENT_KEY] = "FREE_SPACE_EXTENT",
403 [BTRFS_FREE_SPACE_BITMAP_KEY] = "FREE_SPACE_BITMAP",
404 [BTRFS_CHUNK_ITEM_KEY] = "CHUNK_ITEM",
405 [BTRFS_DEV_ITEM_KEY] = "DEV_ITEM",
406 [BTRFS_DEV_EXTENT_KEY] = "DEV_EXTENT",
407 [BTRFS_TEMPORARY_ITEM_KEY] = "TEMPORARY_ITEM",
408 [BTRFS_DEV_REPLACE_KEY] = "DEV_REPLACE",
409 [BTRFS_STRING_ITEM_KEY] = "STRING_ITEM",
410 [BTRFS_QGROUP_STATUS_KEY] = "QGROUP_STATUS",
411 [BTRFS_QGROUP_RELATION_KEY] = "QGROUP_RELATION",
412 [BTRFS_QGROUP_INFO_KEY] = "QGROUP_INFO",
413 [BTRFS_QGROUP_LIMIT_KEY] = "QGROUP_LIMIT",
414 [BTRFS_PERSISTENT_ITEM_KEY] = "PERSISTENT_ITEM",
415 [BTRFS_UUID_KEY_SUBVOL] = "UUID_KEY_SUBVOL",
416 [BTRFS_UUID_KEY_RECEIVED_SUBVOL] = "UUID_KEY_RECEIVED_SUBVOL",
417 [BTRFS_RAID_STRIPE_KEY] = "RAID_STRIPE",
418 };
419
420 if (key->type == 0 && key->objectid == BTRFS_FREE_SPACE_OBJECTID)
421 scnprintf(buf, buf_size, "UNTYPED");
422 else if (key_to_str[key->type])
423 scnprintf(buf, buf_size, "%s", key_to_str[key->type]);
424 else
425 scnprintf(buf, buf_size, "UNKNOWN.%d", key->type);
426}
427
428void btrfs_print_leaf(const struct extent_buffer *l)
429{
430 struct btrfs_fs_info *fs_info;
431 int i;
432 u32 type, nr;
433 struct btrfs_root_item *ri;
434 struct btrfs_block_group_item *bi;
435 struct btrfs_extent_data_ref *dref;
436 struct btrfs_shared_data_ref *sref;
437 struct btrfs_dev_extent *dev_extent;
438 struct btrfs_key key;
439
440 if (!l)
441 return;
442
443 fs_info = l->fs_info;
444 nr = btrfs_header_nritems(l);
445
446 btrfs_info(fs_info,
447 "leaf %llu gen %llu total ptrs %d free space %d owner %llu",
448 btrfs_header_bytenr(l), btrfs_header_generation(l), nr,
449 btrfs_leaf_free_space(l), btrfs_header_owner(l));
450 print_eb_refs_lock(l);
451 for (i = 0 ; i < nr ; i++) {
452 char key_buf[KEY_TYPE_BUF_SIZE];
453
454 btrfs_item_key_to_cpu(l, &key, i);
455 type = key.type;
456 key_type_string(&key, key_buf, KEY_TYPE_BUF_SIZE);
457
458 pr_info("\titem %d key (%llu %s %llu) itemoff %d itemsize %d\n",
459 i, key.objectid, key_buf, key.offset,
460 btrfs_item_offset(l, i), btrfs_item_size(l, i));
461 switch (type) {
462 case BTRFS_INODE_ITEM_KEY:
463 print_inode_item(l, i);
464 break;
465 case BTRFS_INODE_REF_KEY:
466 print_inode_ref_item(l, i);
467 break;
468 case BTRFS_INODE_EXTREF_KEY:
469 print_inode_extref_item(l, i);
470 break;
471 case BTRFS_DIR_ITEM_KEY:
472 case BTRFS_DIR_INDEX_KEY:
473 case BTRFS_XATTR_ITEM_KEY:
474 print_dir_item(l, i);
475 break;
476 case BTRFS_DIR_LOG_INDEX_KEY:
477 print_dir_log_index_item(l, i);
478 break;
479 case BTRFS_EXTENT_CSUM_KEY:
480 print_extent_csum(l, i);
481 break;
482 case BTRFS_ROOT_ITEM_KEY:
483 ri = btrfs_item_ptr(l, i, struct btrfs_root_item);
484 pr_info("\t\troot data bytenr %llu refs %u\n",
485 btrfs_disk_root_bytenr(l, ri),
486 btrfs_disk_root_refs(l, ri));
487 break;
488 case BTRFS_EXTENT_ITEM_KEY:
489 case BTRFS_METADATA_ITEM_KEY:
490 print_extent_item(l, i, type);
491 break;
492 case BTRFS_TREE_BLOCK_REF_KEY:
493 pr_info("\t\ttree block backref\n");
494 break;
495 case BTRFS_SHARED_BLOCK_REF_KEY:
496 pr_info("\t\tshared block backref\n");
497 break;
498 case BTRFS_EXTENT_DATA_REF_KEY:
499 dref = btrfs_item_ptr(l, i,
500 struct btrfs_extent_data_ref);
501 print_extent_data_ref(l, dref);
502 break;
503 case BTRFS_SHARED_DATA_REF_KEY:
504 sref = btrfs_item_ptr(l, i,
505 struct btrfs_shared_data_ref);
506 pr_info("\t\tshared data backref count %u\n",
507 btrfs_shared_data_ref_count(l, sref));
508 break;
509 case BTRFS_EXTENT_DATA_KEY:
510 print_file_extent_item(l, i);
511 break;
512 case BTRFS_BLOCK_GROUP_ITEM_KEY:
513 bi = btrfs_item_ptr(l, i,
514 struct btrfs_block_group_item);
515 pr_info(
516 "\t\tblock group used %llu chunk_objectid %llu flags %llu\n",
517 btrfs_block_group_used(l, bi),
518 btrfs_block_group_chunk_objectid(l, bi),
519 btrfs_block_group_flags(l, bi));
520 break;
521 case BTRFS_CHUNK_ITEM_KEY:
522 print_chunk(l, btrfs_item_ptr(l, i,
523 struct btrfs_chunk));
524 break;
525 case BTRFS_DEV_ITEM_KEY:
526 print_dev_item(l, btrfs_item_ptr(l, i,
527 struct btrfs_dev_item));
528 break;
529 case BTRFS_DEV_EXTENT_KEY:
530 dev_extent = btrfs_item_ptr(l, i,
531 struct btrfs_dev_extent);
532 pr_info("\t\tdev extent chunk_tree %llu\n\t\tchunk objectid %llu chunk offset %llu length %llu\n",
533 btrfs_dev_extent_chunk_tree(l, dev_extent),
534 btrfs_dev_extent_chunk_objectid(l, dev_extent),
535 btrfs_dev_extent_chunk_offset(l, dev_extent),
536 btrfs_dev_extent_length(l, dev_extent));
537 break;
538 case BTRFS_PERSISTENT_ITEM_KEY:
539 pr_info("\t\tpersistent item objectid %llu offset %llu\n",
540 key.objectid, key.offset);
541 switch (key.objectid) {
542 case BTRFS_DEV_STATS_OBJECTID:
543 pr_info("\t\tdevice stats\n");
544 break;
545 default:
546 pr_info("\t\tunknown persistent item\n");
547 }
548 break;
549 case BTRFS_TEMPORARY_ITEM_KEY:
550 pr_info("\t\ttemporary item objectid %llu offset %llu\n",
551 key.objectid, key.offset);
552 switch (key.objectid) {
553 case BTRFS_BALANCE_OBJECTID:
554 pr_info("\t\tbalance status\n");
555 break;
556 default:
557 pr_info("\t\tunknown temporary item\n");
558 }
559 break;
560 case BTRFS_DEV_REPLACE_KEY:
561 pr_info("\t\tdev replace\n");
562 break;
563 case BTRFS_UUID_KEY_SUBVOL:
564 case BTRFS_UUID_KEY_RECEIVED_SUBVOL:
565 print_uuid_item(l, btrfs_item_ptr_offset(l, i),
566 btrfs_item_size(l, i));
567 break;
568 case BTRFS_RAID_STRIPE_KEY:
569 print_raid_stripe_key(l, btrfs_item_size(l, i),
570 btrfs_item_ptr(l, i, struct btrfs_stripe_extent));
571 break;
572 }
573 }
574}
575
576void btrfs_print_tree(const struct extent_buffer *c, bool follow)
577{
578 struct btrfs_fs_info *fs_info;
579 int i; u32 nr;
580 struct btrfs_key key;
581 int level;
582
583 if (!c)
584 return;
585 fs_info = c->fs_info;
586 nr = btrfs_header_nritems(c);
587 level = btrfs_header_level(c);
588 if (level == 0) {
589 btrfs_print_leaf(c);
590 return;
591 }
592 btrfs_info(fs_info,
593 "node %llu level %d gen %llu total ptrs %d free spc %u owner %llu",
594 btrfs_header_bytenr(c), level, btrfs_header_generation(c),
595 nr, (u32)BTRFS_NODEPTRS_PER_BLOCK(fs_info) - nr,
596 btrfs_header_owner(c));
597 print_eb_refs_lock(c);
598 for (i = 0; i < nr; i++) {
599 btrfs_node_key_to_cpu(c, &key, i);
600 pr_info("\tkey %d " BTRFS_KEY_FMT " block %llu gen %llu\n",
601 i, BTRFS_KEY_FMT_VALUE(&key), btrfs_node_blockptr(c, i),
602 btrfs_node_ptr_generation(c, i));
603 }
604 if (!follow)
605 return;
606 for (i = 0; i < nr; i++) {
607 struct btrfs_tree_parent_check check = {
608 .level = level - 1,
609 .transid = btrfs_node_ptr_generation(c, i),
610 .owner_root = btrfs_header_owner(c),
611 .has_first_key = true
612 };
613 struct extent_buffer *next;
614
615 btrfs_node_key_to_cpu(c, &check.first_key, i);
616 next = read_tree_block(fs_info, btrfs_node_blockptr(c, i), &check);
617 if (IS_ERR(next))
618 continue;
619 if (!extent_buffer_uptodate(next)) {
620 free_extent_buffer(next);
621 continue;
622 }
623
624 if (btrfs_is_leaf(next) &&
625 level != 1)
626 BUG();
627 if (btrfs_header_level(next) !=
628 level - 1)
629 BUG();
630 btrfs_print_tree(next, follow);
631 free_extent_buffer(next);
632 }
633}