2 * This program is free software; you can redistribute it and/or
3 * modify it under the terms of the GNU General Public
4 * License v2 as published by the Free Software Foundation.
6 * This program is distributed in the hope that it will be useful,
7 * but WITHOUT ANY WARRANTY; without even the implied warranty of
8 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
9 * General Public License for more details.
11 * You should have received a copy of the GNU General Public
12 * License along with this program; if not, write to the
13 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
14 * Boston, MA 021110-1307, USA.
20 #include "transaction.h"
28 #include "check/mode-common.h"
29 #include "check/mode-lowmem.h"
31 static int calc_extent_flag(struct btrfs_root *root, struct extent_buffer *eb,
34 struct btrfs_root *extent_root = root->fs_info->extent_root;
35 struct btrfs_root_item *ri = &root->root_item;
36 struct btrfs_extent_inline_ref *iref;
37 struct btrfs_extent_item *ei;
39 struct btrfs_path *path = NULL;
50 * Except file/reloc tree, we can not have FULL BACKREF MODE
52 if (root->objectid < BTRFS_FIRST_FREE_OBJECTID)
56 if (eb->start == btrfs_root_bytenr(ri))
59 if (btrfs_header_flag(eb, BTRFS_HEADER_FLAG_RELOC))
62 owner = btrfs_header_owner(eb);
63 if (owner == root->objectid)
66 path = btrfs_alloc_path();
70 key.objectid = btrfs_header_bytenr(eb);
74 ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
81 ret = btrfs_previous_extent_item(extent_root, path,
87 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
90 slot = path->slots[0];
91 ei = btrfs_item_ptr(eb, slot, struct btrfs_extent_item);
93 flags = btrfs_extent_flags(eb, ei);
94 if (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF)
97 ptr = (unsigned long)(ei + 1);
98 end = (unsigned long)ei + btrfs_item_size_nr(eb, slot);
100 if (key.type == BTRFS_EXTENT_ITEM_KEY)
101 ptr += sizeof(struct btrfs_tree_block_info);
104 /* Reached extent item ends normally */
108 /* Beyond extent item end, wrong item size */
110 error("extent item at bytenr %llu slot %d has wrong size",
115 iref = (struct btrfs_extent_inline_ref *)ptr;
116 offset = btrfs_extent_inline_ref_offset(eb, iref);
117 type = btrfs_extent_inline_ref_type(eb, iref);
119 if (type == BTRFS_TREE_BLOCK_REF_KEY && offset == owner)
121 ptr += btrfs_extent_inline_ref_size(type);
125 *flags_ret &= ~BTRFS_BLOCK_FLAG_FULL_BACKREF;
129 *flags_ret |= BTRFS_BLOCK_FLAG_FULL_BACKREF;
131 btrfs_free_path(path);
136 * for a tree node or leaf, if it's shared, indeed we don't need to iterate it
137 * in every fs or file tree check. Here we find its all root ids, and only check
138 * it in the fs or file tree which has the smallest root id.
140 static int need_check(struct btrfs_root *root, struct ulist *roots)
142 struct rb_node *node;
143 struct ulist_node *u;
146 * @roots can be empty if it belongs to tree reloc tree
147 * In that case, we should always check the leaf, as we can't use
148 * the tree owner to ensure some other root will check it.
150 if (roots->nnodes == 1 || roots->nnodes == 0)
153 node = rb_first(&roots->root);
154 u = rb_entry(node, struct ulist_node, rb_node);
156 * current root id is not smallest, we skip it and let it be checked
157 * in the fs or file tree who hash the smallest root id.
159 if (root->objectid != u->val)
166 * for a tree node or leaf, we record its reference count, so later if we still
167 * process this node or leaf, don't need to compute its reference count again.
169 * @bytenr if @bytenr == (u64)-1, only update nrefs->full_backref[level]
171 static int update_nodes_refs(struct btrfs_root *root, u64 bytenr,
172 struct extent_buffer *eb, struct node_refs *nrefs,
173 u64 level, int check_all)
178 int root_level = btrfs_header_level(root->node);
182 if (nrefs->bytenr[level] == bytenr)
185 if (bytenr != (u64)-1) {
186 /* the return value of this function seems a mistake */
187 ret = btrfs_lookup_extent_info(NULL, root, bytenr,
188 level, 1, &refs, &flags);
190 if (ret < 0 && !check_all)
193 nrefs->bytenr[level] = bytenr;
194 nrefs->refs[level] = refs;
195 nrefs->full_backref[level] = 0;
196 nrefs->checked[level] = 0;
199 ret = btrfs_find_all_roots(NULL, root->fs_info, bytenr,
204 check = need_check(root, roots);
206 nrefs->need_check[level] = check;
209 nrefs->need_check[level] = 1;
211 if (level == root_level) {
212 nrefs->need_check[level] = 1;
215 * The node refs may have not been
216 * updated if upper needs checking (the
217 * lowest root_objectid) the node can
220 nrefs->need_check[level] =
221 nrefs->need_check[level + 1];
227 if (check_all && eb) {
228 calc_extent_flag(root, eb, &flags);
229 if (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF)
230 nrefs->full_backref[level] = 1;
237 * This function only handles BACKREF_MISSING,
238 * If corresponding extent item exists, increase the ref, else insert an extent
241 * Returns error bits after repair.
243 static int repair_tree_block_ref(struct btrfs_trans_handle *trans,
244 struct btrfs_root *root,
245 struct extent_buffer *node,
246 struct node_refs *nrefs, int level, int err)
248 struct btrfs_fs_info *fs_info = root->fs_info;
249 struct btrfs_root *extent_root = fs_info->extent_root;
250 struct btrfs_path path;
251 struct btrfs_extent_item *ei;
252 struct btrfs_tree_block_info *bi;
253 struct btrfs_key key;
254 struct extent_buffer *eb;
255 u32 size = sizeof(*ei);
256 u32 node_size = root->fs_info->nodesize;
257 int insert_extent = 0;
258 int skinny_metadata = btrfs_fs_incompat(fs_info, SKINNY_METADATA);
259 int root_level = btrfs_header_level(root->node);
264 u64 flags = BTRFS_EXTENT_FLAG_TREE_BLOCK;
267 if ((err & BACKREF_MISSING) == 0)
270 WARN_ON(level > BTRFS_MAX_LEVEL);
273 btrfs_init_path(&path);
274 bytenr = btrfs_header_bytenr(node);
275 owner = btrfs_header_owner(node);
276 generation = btrfs_header_generation(node);
278 key.objectid = bytenr;
280 key.offset = (u64)-1;
282 /* Search for the extent item */
283 ret = btrfs_search_slot(NULL, extent_root, &key, &path, 0, 0);
289 ret = btrfs_previous_extent_item(extent_root, &path, bytenr);
293 /* calculate if the extent item flag is full backref or not */
294 if (nrefs->full_backref[level] != 0)
295 flags |= BTRFS_BLOCK_FLAG_FULL_BACKREF;
297 /* insert an extent item */
299 struct btrfs_disk_key copy_key;
301 generation = btrfs_header_generation(node);
303 if (level < root_level && nrefs->full_backref[level + 1] &&
304 owner != root->objectid) {
305 flags |= BTRFS_BLOCK_FLAG_FULL_BACKREF;
308 key.objectid = bytenr;
309 if (!skinny_metadata) {
310 key.type = BTRFS_EXTENT_ITEM_KEY;
311 key.offset = node_size;
314 key.type = BTRFS_METADATA_ITEM_KEY;
318 btrfs_release_path(&path);
319 ret = btrfs_insert_empty_item(trans, extent_root, &path, &key,
325 ei = btrfs_item_ptr(eb, path.slots[0], struct btrfs_extent_item);
327 btrfs_set_extent_refs(eb, ei, 0);
328 btrfs_set_extent_generation(eb, ei, generation);
329 btrfs_set_extent_flags(eb, ei, flags);
331 if (!skinny_metadata) {
332 bi = (struct btrfs_tree_block_info *)(ei + 1);
333 memset_extent_buffer(eb, 0, (unsigned long)bi,
335 btrfs_set_disk_key_objectid(©_key, root->objectid);
336 btrfs_set_disk_key_type(©_key, 0);
337 btrfs_set_disk_key_offset(©_key, 0);
339 btrfs_set_tree_block_level(eb, bi, level);
340 btrfs_set_tree_block_key(eb, bi, ©_key);
342 btrfs_mark_buffer_dirty(eb);
343 printf("Added an extent item [%llu %u]\n", bytenr, node_size);
344 btrfs_update_block_group(extent_root, bytenr, node_size, 1, 0);
346 nrefs->refs[level] = 0;
347 nrefs->full_backref[level] =
348 flags & BTRFS_BLOCK_FLAG_FULL_BACKREF;
349 btrfs_release_path(&path);
352 if (level < root_level && nrefs->full_backref[level + 1] &&
353 owner != root->objectid)
354 parent = nrefs->bytenr[level + 1];
356 /* increase the ref */
357 ret = btrfs_inc_extent_ref(trans, extent_root, bytenr, node_size,
358 parent, root->objectid, level, 0);
360 nrefs->refs[level]++;
362 btrfs_release_path(&path);
365 "failed to repair tree block ref start %llu root %llu due to %s",
366 bytenr, root->objectid, strerror(-ret));
368 printf("Added one tree block ref start %llu %s %llu\n",
369 bytenr, parent ? "parent" : "root",
370 parent ? parent : root->objectid);
371 err &= ~BACKREF_MISSING;
378 * Update global fs information.
380 static void account_bytes(struct btrfs_root *root, struct btrfs_path *path,
384 struct extent_buffer *eb = path->nodes[level];
386 total_btree_bytes += eb->len;
387 if (fs_root_objectid(root->objectid))
388 total_fs_tree_bytes += eb->len;
389 if (btrfs_header_owner(eb) == BTRFS_EXTENT_TREE_OBJECTID)
390 total_extent_tree_bytes += eb->len;
393 btree_space_waste += btrfs_leaf_free_space(root, eb);
395 free_nrs = (BTRFS_NODEPTRS_PER_BLOCK(root->fs_info) -
396 btrfs_header_nritems(eb));
397 btree_space_waste += free_nrs * sizeof(struct btrfs_key_ptr);
402 * Find the @index according by @ino and name.
403 * Notice:time efficiency is O(N)
405 * @root: the root of the fs/file tree
406 * @index_ret: the index as return value
407 * @namebuf: the name to match
408 * @name_len: the length of name to match
409 * @file_type: the file_type of INODE_ITEM to match
411 * Returns 0 if found and *@index_ret will be modified with right value
412 * Returns< 0 not found and *@index_ret will be (u64)-1
414 static int find_dir_index(struct btrfs_root *root, u64 dirid, u64 location_id,
415 u64 *index_ret, char *namebuf, u32 name_len,
418 struct btrfs_path path;
419 struct extent_buffer *node;
420 struct btrfs_dir_item *di;
421 struct btrfs_key key;
422 struct btrfs_key location;
423 char name[BTRFS_NAME_LEN] = {0};
435 /* search from the last index */
436 key.objectid = dirid;
437 key.offset = (u64)-1;
438 key.type = BTRFS_DIR_INDEX_KEY;
440 btrfs_init_path(&path);
441 ret = btrfs_search_slot(NULL, root, &key, &path, 0, 0);
446 ret = btrfs_previous_item(root, &path, dirid, BTRFS_DIR_INDEX_KEY);
452 /* Check whether inode_id/filetype/name match */
453 node = path.nodes[0];
454 slot = path.slots[0];
455 di = btrfs_item_ptr(node, slot, struct btrfs_dir_item);
456 total = btrfs_item_size_nr(node, slot);
457 while (cur < total) {
459 len = btrfs_dir_name_len(node, di);
460 data_len = btrfs_dir_data_len(node, di);
462 btrfs_dir_item_key_to_cpu(node, di, &location);
463 if (location.objectid != location_id ||
464 location.type != BTRFS_INODE_ITEM_KEY ||
465 location.offset != 0)
468 filetype = btrfs_dir_type(node, di);
469 if (file_type != filetype)
472 if (len > BTRFS_NAME_LEN)
473 len = BTRFS_NAME_LEN;
475 read_extent_buffer(node, name, (unsigned long)(di + 1), len);
476 if (len != name_len || strncmp(namebuf, name, len))
479 btrfs_item_key_to_cpu(path.nodes[0], &key, path.slots[0]);
480 *index_ret = key.offset;
484 len += sizeof(*di) + data_len;
485 di = (struct btrfs_dir_item *)((char *)di + len);
491 btrfs_release_path(&path);
496 * Find DIR_ITEM/DIR_INDEX for the given key and check it with the specified
497 * INODE_REF/INODE_EXTREF match.
499 * @root: the root of the fs/file tree
500 * @key: the key of the DIR_ITEM/DIR_INDEX, key->offset will be right
501 * value while find index
502 * @location_key: location key of the struct btrfs_dir_item to match
503 * @name: the name to match
504 * @namelen: the length of name
505 * @file_type: the type of file to math
507 * Return 0 if no error occurred.
508 * Return DIR_ITEM_MISSING/DIR_INDEX_MISSING if couldn't find
510 * Return DIR_ITEM_MISMATCH/DIR_INDEX_MISMATCH if INODE_REF/INODE_EXTREF
511 * and DIR_ITEM/DIR_INDEX mismatch
513 static int find_dir_item(struct btrfs_root *root, struct btrfs_key *key,
514 struct btrfs_key *location_key, char *name,
515 u32 namelen, u8 file_type)
517 struct btrfs_path path;
518 struct extent_buffer *node;
519 struct btrfs_dir_item *di;
520 struct btrfs_key location;
521 char namebuf[BTRFS_NAME_LEN] = {0};
530 /* get the index by traversing all index */
531 if (key->type == BTRFS_DIR_INDEX_KEY && key->offset == (u64)-1) {
532 ret = find_dir_index(root, key->objectid,
533 location_key->objectid, &key->offset,
534 name, namelen, file_type);
536 ret = DIR_INDEX_MISSING;
540 btrfs_init_path(&path);
541 ret = btrfs_search_slot(NULL, root, key, &path, 0, 0);
543 ret = key->type == BTRFS_DIR_ITEM_KEY ? DIR_ITEM_MISSING :
548 /* Check whether inode_id/filetype/name match */
549 node = path.nodes[0];
550 slot = path.slots[0];
551 di = btrfs_item_ptr(node, slot, struct btrfs_dir_item);
552 total = btrfs_item_size_nr(node, slot);
553 while (cur < total) {
554 ret = key->type == BTRFS_DIR_ITEM_KEY ?
555 DIR_ITEM_MISMATCH : DIR_INDEX_MISMATCH;
557 len = btrfs_dir_name_len(node, di);
558 data_len = btrfs_dir_data_len(node, di);
560 btrfs_dir_item_key_to_cpu(node, di, &location);
561 if (location.objectid != location_key->objectid ||
562 location.type != location_key->type ||
563 location.offset != location_key->offset)
566 filetype = btrfs_dir_type(node, di);
567 if (file_type != filetype)
570 if (len > BTRFS_NAME_LEN) {
571 len = BTRFS_NAME_LEN;
572 warning("root %llu %s[%llu %llu] name too long %u, trimmed",
574 key->type == BTRFS_DIR_ITEM_KEY ?
575 "DIR_ITEM" : "DIR_INDEX",
576 key->objectid, key->offset, len);
578 read_extent_buffer(node, namebuf, (unsigned long)(di + 1),
580 if (len != namelen || strncmp(namebuf, name, len))
586 len += sizeof(*di) + data_len;
587 di = (struct btrfs_dir_item *)((char *)di + len);
592 btrfs_release_path(&path);
597 * The ternary means dir item, dir index and relative inode ref.
598 * The function handles errs: INODE_MISSING, DIR_INDEX_MISSING
599 * DIR_INDEX_MISMATCH, DIR_ITEM_MISSING, DIR_ITEM_MISMATCH by the follow
601 * If two of three is missing or mismatched, delete the existing one.
602 * If one of three is missing or mismatched, add the missing one.
604 * returns 0 means success.
605 * returns not 0 means on error;
607 int repair_ternary_lowmem(struct btrfs_root *root, u64 dir_ino, u64 ino,
608 u64 index, char *name, int name_len, u8 filetype,
611 struct btrfs_trans_handle *trans;
616 * stage shall be one of following valild values:
617 * 0: Fine, nothing to do.
618 * 1: One of three is wrong, so add missing one.
619 * 2: Two of three is wrong, so delete existed one.
621 if (err & (DIR_INDEX_MISMATCH | DIR_INDEX_MISSING))
623 if (err & (DIR_ITEM_MISMATCH | DIR_ITEM_MISSING))
625 if (err & (INODE_REF_MISSING))
628 /* stage must be smllarer than 3 */
631 trans = btrfs_start_transaction(root, 1);
633 ret = btrfs_unlink(trans, root, ino, dir_ino, index, name,
638 ret = btrfs_add_link(trans, root, ino, dir_ino, name, name_len,
639 filetype, &index, 1, 1);
643 btrfs_commit_transaction(trans, root);
646 error("fail to repair inode %llu name %s filetype %u",
647 ino, name, filetype);
649 printf("%s ref/dir_item of inode %llu name %s filetype %u\n",
650 stage == 2 ? "Delete" : "Add",
651 ino, name, filetype);
657 * Prints inode ref error message
659 static void print_inode_ref_err(struct btrfs_root *root, struct btrfs_key *key,
660 u64 index, const char *namebuf, int name_len,
661 u8 filetype, int err)
667 if (key->objectid == BTRFS_FIRST_FREE_OBJECTID) {
669 "root %llu root dir shouldn't have INODE REF[%llu %llu] name %s",
670 root->objectid, key->objectid, key->offset, namebuf);
675 if (err & (DIR_ITEM_MISMATCH | DIR_ITEM_MISSING))
676 error("root %llu DIR ITEM[%llu %llu] %s name %s filetype %u",
677 root->objectid, key->offset,
678 btrfs_name_hash(namebuf, name_len),
679 err & DIR_ITEM_MISMATCH ? "mismatch" : "missing",
681 if (err & (DIR_INDEX_MISMATCH | DIR_INDEX_MISSING))
682 error("root %llu DIR INDEX[%llu %llu] %s name %s filetype %u",
683 root->objectid, key->offset, index,
684 err & DIR_ITEM_MISMATCH ? "mismatch" : "missing",
689 * Traverse the given INODE_REF and call find_dir_item() to find related
690 * DIR_ITEM/DIR_INDEX.
692 * @root: the root of the fs/file tree
693 * @ref_key: the key of the INODE_REF
694 * @path the path provides node and slot
695 * @refs: the count of INODE_REF
696 * @mode: the st_mode of INODE_ITEM
697 * @name_ret: returns with the first ref's name
698 * @name_len_ret: len of the name_ret
700 * Return 0 if no error occurred.
702 static int check_inode_ref(struct btrfs_root *root, struct btrfs_key *ref_key,
703 struct btrfs_path *path, char *name_ret,
704 u32 *namelen_ret, u64 *refs_ret, int mode)
706 struct btrfs_key key;
707 struct btrfs_key location;
708 struct btrfs_inode_ref *ref;
709 struct extent_buffer *node;
710 char namebuf[BTRFS_NAME_LEN] = {0};
720 int need_research = 0;
728 /* since after repair, path and the dir item may be changed */
731 btrfs_release_path(path);
732 ret = btrfs_search_slot(NULL, root, ref_key, path, 0, 0);
734 * The item was deleted, let the path point to the last checked
738 if (path->slots[0] == 0)
739 btrfs_prev_leaf(root, path);
747 location.objectid = ref_key->objectid;
748 location.type = BTRFS_INODE_ITEM_KEY;
750 node = path->nodes[0];
751 slot = path->slots[0];
753 memset(namebuf, 0, sizeof(namebuf) / sizeof(*namebuf));
754 ref = btrfs_item_ptr(node, slot, struct btrfs_inode_ref);
755 total = btrfs_item_size_nr(node, slot);
758 /* Update inode ref count */
761 index = btrfs_inode_ref_index(node, ref);
762 name_len = btrfs_inode_ref_name_len(node, ref);
764 if (name_len <= BTRFS_NAME_LEN) {
767 len = BTRFS_NAME_LEN;
768 warning("root %llu INODE_REF[%llu %llu] name too long",
769 root->objectid, ref_key->objectid, ref_key->offset);
772 read_extent_buffer(node, namebuf, (unsigned long)(ref + 1), len);
774 /* copy the first name found to name_ret */
775 if (refs == 1 && name_ret) {
776 memcpy(name_ret, namebuf, len);
780 /* Check root dir ref */
781 if (ref_key->objectid == BTRFS_FIRST_FREE_OBJECTID) {
782 if (index != 0 || len != strlen("..") ||
783 strncmp("..", namebuf, len) ||
784 ref_key->offset != BTRFS_FIRST_FREE_OBJECTID) {
785 /* set err bits then repair will delete the ref */
786 err |= DIR_INDEX_MISSING;
787 err |= DIR_ITEM_MISSING;
792 /* Find related DIR_INDEX */
793 key.objectid = ref_key->offset;
794 key.type = BTRFS_DIR_INDEX_KEY;
796 tmp_err |= find_dir_item(root, &key, &location, namebuf, len,
797 imode_to_type(mode));
799 /* Find related dir_item */
800 key.objectid = ref_key->offset;
801 key.type = BTRFS_DIR_ITEM_KEY;
802 key.offset = btrfs_name_hash(namebuf, len);
803 tmp_err |= find_dir_item(root, &key, &location, namebuf, len,
804 imode_to_type(mode));
806 if (tmp_err && repair) {
807 ret = repair_ternary_lowmem(root, ref_key->offset,
808 ref_key->objectid, index, namebuf,
809 name_len, imode_to_type(mode),
816 print_inode_ref_err(root, ref_key, index, namebuf, name_len,
817 imode_to_type(mode), tmp_err);
819 len = sizeof(*ref) + name_len;
820 ref = (struct btrfs_inode_ref *)((char *)ref + len);
831 * Traverse the given INODE_EXTREF and call find_dir_item() to find related
832 * DIR_ITEM/DIR_INDEX.
834 * @root: the root of the fs/file tree
835 * @ref_key: the key of the INODE_EXTREF
836 * @refs: the count of INODE_EXTREF
837 * @mode: the st_mode of INODE_ITEM
839 * Return 0 if no error occurred.
841 static int check_inode_extref(struct btrfs_root *root,
842 struct btrfs_key *ref_key,
843 struct extent_buffer *node, int slot, u64 *refs,
846 struct btrfs_key key;
847 struct btrfs_key location;
848 struct btrfs_inode_extref *extref;
849 char namebuf[BTRFS_NAME_LEN] = {0};
859 location.objectid = ref_key->objectid;
860 location.type = BTRFS_INODE_ITEM_KEY;
863 extref = btrfs_item_ptr(node, slot, struct btrfs_inode_extref);
864 total = btrfs_item_size_nr(node, slot);
867 /* update inode ref count */
869 name_len = btrfs_inode_extref_name_len(node, extref);
870 index = btrfs_inode_extref_index(node, extref);
871 parent = btrfs_inode_extref_parent(node, extref);
872 if (name_len <= BTRFS_NAME_LEN) {
875 len = BTRFS_NAME_LEN;
876 warning("root %llu INODE_EXTREF[%llu %llu] name too long",
877 root->objectid, ref_key->objectid, ref_key->offset);
879 read_extent_buffer(node, namebuf, (unsigned long)(extref + 1), len);
881 /* Check root dir ref name */
882 if (index == 0 && strncmp(namebuf, "..", name_len)) {
883 error("root %llu INODE_EXTREF[%llu %llu] ROOT_DIR name shouldn't be %s",
884 root->objectid, ref_key->objectid, ref_key->offset,
886 err |= ROOT_DIR_ERROR;
889 /* find related dir_index */
890 key.objectid = parent;
891 key.type = BTRFS_DIR_INDEX_KEY;
893 ret = find_dir_item(root, &key, &location, namebuf, len, mode);
896 /* find related dir_item */
897 key.objectid = parent;
898 key.type = BTRFS_DIR_ITEM_KEY;
899 key.offset = btrfs_name_hash(namebuf, len);
900 ret = find_dir_item(root, &key, &location, namebuf, len, mode);
903 len = sizeof(*extref) + name_len;
904 extref = (struct btrfs_inode_extref *)((char *)extref + len);
914 * Find INODE_REF/INODE_EXTREF for the given key and check it with the specified
915 * DIR_ITEM/DIR_INDEX match.
916 * Return with @index_ret.
918 * @root: the root of the fs/file tree
919 * @key: the key of the INODE_REF/INODE_EXTREF
920 * @name: the name in the INODE_REF/INODE_EXTREF
921 * @namelen: the length of name in the INODE_REF/INODE_EXTREF
922 * @index_ret: the index in the INODE_REF/INODE_EXTREF,
923 * value (64)-1 means do not check index
924 * @ext_ref: the EXTENDED_IREF feature
926 * Return 0 if no error occurred.
927 * Return >0 for error bitmap
929 static int find_inode_ref(struct btrfs_root *root, struct btrfs_key *key,
930 char *name, int namelen, u64 *index_ret,
931 unsigned int ext_ref)
933 struct btrfs_path path;
934 struct btrfs_inode_ref *ref;
935 struct btrfs_inode_extref *extref;
936 struct extent_buffer *node;
937 char ref_namebuf[BTRFS_NAME_LEN] = {0};
950 btrfs_init_path(&path);
951 ret = btrfs_search_slot(NULL, root, key, &path, 0, 0);
953 ret = INODE_REF_MISSING;
957 node = path.nodes[0];
958 slot = path.slots[0];
960 ref = btrfs_item_ptr(node, slot, struct btrfs_inode_ref);
961 total = btrfs_item_size_nr(node, slot);
963 /* Iterate all entry of INODE_REF */
964 while (cur < total) {
965 ret = INODE_REF_MISSING;
967 ref_namelen = btrfs_inode_ref_name_len(node, ref);
968 ref_index = btrfs_inode_ref_index(node, ref);
969 if (*index_ret != (u64)-1 && *index_ret != ref_index)
972 if (cur + sizeof(*ref) + ref_namelen > total ||
973 ref_namelen > BTRFS_NAME_LEN) {
974 warning("root %llu INODE %s[%llu %llu] name too long",
976 key->type == BTRFS_INODE_REF_KEY ?
978 key->objectid, key->offset);
980 if (cur + sizeof(*ref) > total)
982 len = min_t(u32, total - cur - sizeof(*ref),
988 read_extent_buffer(node, ref_namebuf, (unsigned long)(ref + 1),
991 if (len != namelen || strncmp(ref_namebuf, name, len))
994 *index_ret = ref_index;
998 len = sizeof(*ref) + ref_namelen;
999 ref = (struct btrfs_inode_ref *)((char *)ref + len);
1004 /* Skip if not support EXTENDED_IREF feature */
1008 btrfs_release_path(&path);
1009 btrfs_init_path(&path);
1011 dir_id = key->offset;
1012 key->type = BTRFS_INODE_EXTREF_KEY;
1013 key->offset = btrfs_extref_hash(dir_id, name, namelen);
1015 ret = btrfs_search_slot(NULL, root, key, &path, 0, 0);
1017 ret = INODE_REF_MISSING;
1021 node = path.nodes[0];
1022 slot = path.slots[0];
1024 extref = btrfs_item_ptr(node, slot, struct btrfs_inode_extref);
1026 total = btrfs_item_size_nr(node, slot);
1028 /* Iterate all entry of INODE_EXTREF */
1029 while (cur < total) {
1030 ret = INODE_REF_MISSING;
1032 ref_namelen = btrfs_inode_extref_name_len(node, extref);
1033 ref_index = btrfs_inode_extref_index(node, extref);
1034 parent = btrfs_inode_extref_parent(node, extref);
1035 if (*index_ret != (u64)-1 && *index_ret != ref_index)
1038 if (parent != dir_id)
1041 if (ref_namelen <= BTRFS_NAME_LEN) {
1044 len = BTRFS_NAME_LEN;
1045 warning("root %llu INODE %s[%llu %llu] name too long",
1047 key->type == BTRFS_INODE_REF_KEY ?
1049 key->objectid, key->offset);
1051 read_extent_buffer(node, ref_namebuf,
1052 (unsigned long)(extref + 1), len);
1054 if (len != namelen || strncmp(ref_namebuf, name, len))
1057 *index_ret = ref_index;
1062 len = sizeof(*extref) + ref_namelen;
1063 extref = (struct btrfs_inode_extref *)((char *)extref + len);
1068 btrfs_release_path(&path);
1072 static int create_inode_item_lowmem(struct btrfs_trans_handle *trans,
1073 struct btrfs_root *root, u64 ino,
1076 u32 mode = (filetype == BTRFS_FT_DIR ? S_IFDIR : S_IFREG) | 0755;
1078 return insert_inode_item(trans, root, ino, 0, 0, 0, mode);
1082 * Insert the missing inode item.
1084 * Returns 0 means success.
1085 * Returns <0 means error.
1087 static int repair_inode_item_missing(struct btrfs_root *root, u64 ino,
1090 struct btrfs_key key;
1091 struct btrfs_trans_handle *trans;
1092 struct btrfs_path path;
1096 key.type = BTRFS_INODE_ITEM_KEY;
1099 btrfs_init_path(&path);
1100 trans = btrfs_start_transaction(root, 1);
1101 if (IS_ERR(trans)) {
1106 ret = btrfs_search_slot(trans, root, &key, &path, 1, 1);
1107 if (ret < 0 || !ret)
1110 /* insert inode item */
1111 create_inode_item_lowmem(trans, root, ino, filetype);
1114 btrfs_commit_transaction(trans, root);
1117 error("failed to repair root %llu INODE ITEM[%llu] missing",
1118 root->objectid, ino);
1119 btrfs_release_path(&path);
1124 * Call repair_inode_item_missing and repair_ternary_lowmem to repair
1126 * Returns error after repair
1128 static int repair_dir_item(struct btrfs_root *root, u64 dirid, u64 ino,
1129 u64 index, u8 filetype, char *namebuf, u32 name_len,
1134 if (err & INODE_ITEM_MISSING) {
1135 ret = repair_inode_item_missing(root, ino, filetype);
1137 err &= ~(INODE_ITEM_MISMATCH | INODE_ITEM_MISSING);
1140 if (err & ~(INODE_ITEM_MISMATCH | INODE_ITEM_MISSING)) {
1141 ret = repair_ternary_lowmem(root, dirid, ino, index, namebuf,
1142 name_len, filetype, err);
1144 err &= ~(DIR_INDEX_MISMATCH | DIR_INDEX_MISSING);
1145 err &= ~(DIR_ITEM_MISMATCH | DIR_ITEM_MISSING);
1146 err &= ~(INODE_REF_MISSING);
1152 static void print_dir_item_err(struct btrfs_root *root, struct btrfs_key *key,
1153 u64 ino, u64 index, const char *namebuf,
1154 int name_len, u8 filetype, int err)
1156 if (err & (DIR_ITEM_MISMATCH | DIR_ITEM_MISSING)) {
1157 error("root %llu DIR ITEM[%llu %llu] name %s filetype %d %s",
1158 root->objectid, key->objectid, key->offset, namebuf,
1160 err & DIR_ITEM_MISMATCH ? "mismath" : "missing");
1163 if (err & (DIR_INDEX_MISMATCH | DIR_INDEX_MISSING)) {
1164 error("root %llu DIR INDEX[%llu %llu] name %s filetype %d %s",
1165 root->objectid, key->objectid, index, namebuf, filetype,
1166 err & DIR_ITEM_MISMATCH ? "mismath" : "missing");
1169 if (err & (INODE_ITEM_MISSING | INODE_ITEM_MISMATCH)) {
1171 "root %llu INODE_ITEM[%llu] index %llu name %s filetype %d %s",
1172 root->objectid, ino, index, namebuf, filetype,
1173 err & INODE_ITEM_MISMATCH ? "mismath" : "missing");
1176 if (err & INODE_REF_MISSING)
1178 "root %llu INODE REF[%llu, %llu] name %s filetype %u missing",
1179 root->objectid, ino, key->objectid, namebuf, filetype);
1184 * Traverse the given DIR_ITEM/DIR_INDEX and check related INODE_ITEM and
1185 * call find_inode_ref() to check related INODE_REF/INODE_EXTREF.
1187 * @root: the root of the fs/file tree
1188 * @key: the key of the INODE_REF/INODE_EXTREF
1190 * @size: the st_size of the INODE_ITEM
1191 * @ext_ref: the EXTENDED_IREF feature
1193 * Return 0 if no error occurred.
1194 * Return DIR_COUNT_AGAIN if the isize of the inode should be recalculated.
1196 static int check_dir_item(struct btrfs_root *root, struct btrfs_key *di_key,
1197 struct btrfs_path *path, u64 *size,
1198 unsigned int ext_ref)
1200 struct btrfs_dir_item *di;
1201 struct btrfs_inode_item *ii;
1202 struct btrfs_key key;
1203 struct btrfs_key location;
1204 struct extent_buffer *node;
1206 char namebuf[BTRFS_NAME_LEN] = {0};
1218 int need_research = 0;
1221 * For DIR_ITEM set index to (u64)-1, so that find_inode_ref
1222 * ignore index check.
1224 if (di_key->type == BTRFS_DIR_INDEX_KEY)
1225 index = di_key->offset;
1232 /* since after repair, path and the dir item may be changed */
1233 if (need_research) {
1235 err |= DIR_COUNT_AGAIN;
1236 btrfs_release_path(path);
1237 ret = btrfs_search_slot(NULL, root, di_key, path, 0, 0);
1238 /* the item was deleted, let path point the last checked item */
1240 if (path->slots[0] == 0)
1241 btrfs_prev_leaf(root, path);
1249 node = path->nodes[0];
1250 slot = path->slots[0];
1252 di = btrfs_item_ptr(node, slot, struct btrfs_dir_item);
1253 total = btrfs_item_size_nr(node, slot);
1254 memset(namebuf, 0, sizeof(namebuf) / sizeof(*namebuf));
1256 while (cur < total) {
1257 data_len = btrfs_dir_data_len(node, di);
1260 error("root %llu %s[%llu %llu] data_len shouldn't be %u",
1262 di_key->type == BTRFS_DIR_ITEM_KEY ? "DIR_ITEM" : "DIR_INDEX",
1263 di_key->objectid, di_key->offset, data_len);
1265 name_len = btrfs_dir_name_len(node, di);
1266 if (name_len <= BTRFS_NAME_LEN) {
1269 len = BTRFS_NAME_LEN;
1270 warning("root %llu %s[%llu %llu] name too long",
1272 di_key->type == BTRFS_DIR_ITEM_KEY ? "DIR_ITEM" : "DIR_INDEX",
1273 di_key->objectid, di_key->offset);
1275 (*size) += name_len;
1276 read_extent_buffer(node, namebuf, (unsigned long)(di + 1),
1278 filetype = btrfs_dir_type(node, di);
1280 if (di_key->type == BTRFS_DIR_ITEM_KEY &&
1281 di_key->offset != btrfs_name_hash(namebuf, len)) {
1283 error("root %llu DIR_ITEM[%llu %llu] name %s namelen %u filetype %u mismatch with its hash, wanted %llu have %llu",
1284 root->objectid, di_key->objectid, di_key->offset,
1285 namebuf, len, filetype, di_key->offset,
1286 btrfs_name_hash(namebuf, len));
1289 btrfs_dir_item_key_to_cpu(node, di, &location);
1290 /* Ignore related ROOT_ITEM check */
1291 if (location.type == BTRFS_ROOT_ITEM_KEY)
1294 btrfs_release_path(path);
1295 /* Check relative INODE_ITEM(existence/filetype) */
1296 ret = btrfs_search_slot(NULL, root, &location, path, 0, 0);
1298 tmp_err |= INODE_ITEM_MISSING;
1302 ii = btrfs_item_ptr(path->nodes[0], path->slots[0],
1303 struct btrfs_inode_item);
1304 mode = btrfs_inode_mode(path->nodes[0], ii);
1305 if (imode_to_type(mode) != filetype) {
1306 tmp_err |= INODE_ITEM_MISMATCH;
1310 /* Check relative INODE_REF/INODE_EXTREF */
1311 key.objectid = location.objectid;
1312 key.type = BTRFS_INODE_REF_KEY;
1313 key.offset = di_key->objectid;
1314 tmp_err |= find_inode_ref(root, &key, namebuf, len,
1317 /* check relative INDEX/ITEM */
1318 key.objectid = di_key->objectid;
1319 if (key.type == BTRFS_DIR_ITEM_KEY) {
1320 key.type = BTRFS_DIR_INDEX_KEY;
1323 key.type = BTRFS_DIR_ITEM_KEY;
1324 key.offset = btrfs_name_hash(namebuf, name_len);
1327 tmp_err |= find_dir_item(root, &key, &location, namebuf,
1328 name_len, filetype);
1329 /* find_dir_item may find index */
1330 if (key.type == BTRFS_DIR_INDEX_KEY)
1334 if (tmp_err && repair) {
1335 ret = repair_dir_item(root, di_key->objectid,
1336 location.objectid, index,
1337 imode_to_type(mode), namebuf,
1339 if (ret != tmp_err) {
1344 btrfs_release_path(path);
1345 print_dir_item_err(root, di_key, location.objectid, index,
1346 namebuf, name_len, filetype, tmp_err);
1348 len = sizeof(*di) + name_len + data_len;
1349 di = (struct btrfs_dir_item *)((char *)di + len);
1352 if (di_key->type == BTRFS_DIR_INDEX_KEY && cur < total) {
1353 error("root %llu DIR_INDEX[%llu %llu] should contain only one entry",
1354 root->objectid, di_key->objectid,
1361 btrfs_release_path(path);
1362 ret = btrfs_search_slot(NULL, root, di_key, path, 0, 0);
1364 err |= ret > 0 ? -ENOENT : ret;
1369 * Wrapper function of btrfs_punch_hole.
1371 * Returns 0 means success.
1372 * Returns not 0 means error.
1374 static int punch_extent_hole(struct btrfs_root *root, u64 ino, u64 start,
1377 struct btrfs_trans_handle *trans;
1380 trans = btrfs_start_transaction(root, 1);
1382 return PTR_ERR(trans);
1384 ret = btrfs_punch_hole(trans, root, ino, start, len);
1386 error("failed to add hole [%llu, %llu] in inode [%llu]",
1389 printf("Add a hole [%llu, %llu] in inode [%llu]\n", start, len,
1392 btrfs_commit_transaction(trans, root);
1397 * Check file extent datasum/hole, update the size of the file extents,
1398 * check and update the last offset of the file extent.
1400 * @root: the root of fs/file tree.
1401 * @fkey: the key of the file extent.
1402 * @nodatasum: INODE_NODATASUM feature.
1403 * @size: the sum of all EXTENT_DATA items size for this inode.
1404 * @end: the offset of the last extent.
1406 * Return 0 if no error occurred.
1408 static int check_file_extent(struct btrfs_root *root, struct btrfs_key *fkey,
1409 struct extent_buffer *node, int slot,
1410 unsigned int nodatasum, u64 *size, u64 *end)
1412 struct btrfs_file_extent_item *fi;
1415 u64 extent_num_bytes;
1417 u64 csum_found; /* In byte size, sectorsize aligned */
1418 u64 search_start; /* Logical range start we search for csum */
1419 u64 search_len; /* Logical range len we search for csum */
1420 unsigned int extent_type;
1421 unsigned int is_hole;
1426 fi = btrfs_item_ptr(node, slot, struct btrfs_file_extent_item);
1428 /* Check inline extent */
1429 extent_type = btrfs_file_extent_type(node, fi);
1430 if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1431 struct btrfs_item *e = btrfs_item_nr(slot);
1432 u32 item_inline_len;
1434 item_inline_len = btrfs_file_extent_inline_item_len(node, e);
1435 extent_num_bytes = btrfs_file_extent_inline_len(node, slot, fi);
1436 compressed = btrfs_file_extent_compression(node, fi);
1437 if (extent_num_bytes == 0) {
1439 "root %llu EXTENT_DATA[%llu %llu] has empty inline extent",
1440 root->objectid, fkey->objectid, fkey->offset);
1441 err |= FILE_EXTENT_ERROR;
1443 if (!compressed && extent_num_bytes != item_inline_len) {
1445 "root %llu EXTENT_DATA[%llu %llu] wrong inline size, have: %llu, expected: %u",
1446 root->objectid, fkey->objectid, fkey->offset,
1447 extent_num_bytes, item_inline_len);
1448 err |= FILE_EXTENT_ERROR;
1450 *end += extent_num_bytes;
1451 *size += extent_num_bytes;
1455 /* Check extent type */
1456 if (extent_type != BTRFS_FILE_EXTENT_REG &&
1457 extent_type != BTRFS_FILE_EXTENT_PREALLOC) {
1458 err |= FILE_EXTENT_ERROR;
1459 error("root %llu EXTENT_DATA[%llu %llu] type bad",
1460 root->objectid, fkey->objectid, fkey->offset);
1464 /* Check REG_EXTENT/PREALLOC_EXTENT */
1465 disk_bytenr = btrfs_file_extent_disk_bytenr(node, fi);
1466 disk_num_bytes = btrfs_file_extent_disk_num_bytes(node, fi);
1467 extent_num_bytes = btrfs_file_extent_num_bytes(node, fi);
1468 extent_offset = btrfs_file_extent_offset(node, fi);
1469 compressed = btrfs_file_extent_compression(node, fi);
1470 is_hole = (disk_bytenr == 0) && (disk_num_bytes == 0);
1473 * Check EXTENT_DATA csum
1475 * For plain (uncompressed) extent, we should only check the range
1476 * we're referring to, as it's possible that part of prealloc extent
1477 * has been written, and has csum:
1479 * |<--- Original large preallocated extent A ---->|
1480 * |<- Prealloc File Extent ->|<- Regular Extent ->|
1483 * For compressed extent, we should check the whole range.
1486 search_start = disk_bytenr + extent_offset;
1487 search_len = extent_num_bytes;
1489 search_start = disk_bytenr;
1490 search_len = disk_num_bytes;
1492 ret = count_csum_range(root->fs_info, search_start, search_len,
1494 if (csum_found > 0 && nodatasum) {
1495 err |= ODD_CSUM_ITEM;
1496 error("root %llu EXTENT_DATA[%llu %llu] nodatasum shouldn't have datasum",
1497 root->objectid, fkey->objectid, fkey->offset);
1498 } else if (extent_type == BTRFS_FILE_EXTENT_REG && !nodatasum &&
1499 !is_hole && (ret < 0 || csum_found < search_len)) {
1500 err |= CSUM_ITEM_MISSING;
1501 error("root %llu EXTENT_DATA[%llu %llu] csum missing, have: %llu, expected: %llu",
1502 root->objectid, fkey->objectid, fkey->offset,
1503 csum_found, search_len);
1504 } else if (extent_type == BTRFS_FILE_EXTENT_PREALLOC &&
1506 err |= ODD_CSUM_ITEM;
1507 error("root %llu EXTENT_DATA[%llu %llu] prealloc shouldn't have csum, but has: %llu",
1508 root->objectid, fkey->objectid, fkey->offset, csum_found);
1511 /* Check EXTENT_DATA hole */
1512 if (!no_holes && *end != fkey->offset) {
1514 ret = punch_extent_hole(root, fkey->objectid,
1515 *end, fkey->offset - *end);
1516 if (!repair || ret) {
1517 err |= FILE_EXTENT_ERROR;
1519 "root %llu EXTENT_DATA[%llu %llu] gap exists, expected: EXTENT_DATA[%llu %llu]",
1520 root->objectid, fkey->objectid, fkey->offset,
1521 fkey->objectid, *end);
1525 *end += extent_num_bytes;
1527 *size += extent_num_bytes;
1532 static int __count_dir_isize(struct btrfs_root *root, u64 ino, int type,
1535 struct btrfs_key key;
1536 struct btrfs_path path;
1538 struct btrfs_dir_item *di;
1548 key.offset = (u64)-1;
1550 btrfs_init_path(&path);
1551 ret = btrfs_search_slot(NULL, root, &key, &path, 0, 0);
1556 /* if found, go to spacial case */
1561 ret = btrfs_previous_item(root, &path, ino, type);
1569 di = btrfs_item_ptr(path.nodes[0], path.slots[0], struct btrfs_dir_item);
1571 total = btrfs_item_size_nr(path.nodes[0], path.slots[0]);
1573 while (cur < total) {
1574 len = btrfs_dir_name_len(path.nodes[0], di);
1575 if (len > BTRFS_NAME_LEN)
1576 len = BTRFS_NAME_LEN;
1579 len += btrfs_dir_data_len(path.nodes[0], di);
1581 di = (struct btrfs_dir_item *)((char *)di + len);
1587 btrfs_release_path(&path);
1591 static int count_dir_isize(struct btrfs_root *root, u64 ino, u64 *size)
1598 ret = __count_dir_isize(root, ino, BTRFS_DIR_ITEM_KEY, &item_size);
1602 ret = __count_dir_isize(root, ino, BTRFS_DIR_INDEX_KEY, &index_size);
1606 *size = item_size + index_size;
1610 error("failed to count root %llu INODE[%llu] root size",
1611 root->objectid, ino);
1616 * Set inode item nbytes to @nbytes
1618 * Returns 0 on success
1619 * Returns != 0 on error
1621 static int repair_inode_nbytes_lowmem(struct btrfs_root *root,
1622 struct btrfs_path *path,
1623 u64 ino, u64 nbytes)
1625 struct btrfs_trans_handle *trans;
1626 struct btrfs_inode_item *ii;
1627 struct btrfs_key key;
1628 struct btrfs_key research_key;
1632 btrfs_item_key_to_cpu(path->nodes[0], &research_key, path->slots[0]);
1635 key.type = BTRFS_INODE_ITEM_KEY;
1638 trans = btrfs_start_transaction(root, 1);
1639 if (IS_ERR(trans)) {
1640 ret = PTR_ERR(trans);
1645 btrfs_release_path(path);
1646 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
1654 ii = btrfs_item_ptr(path->nodes[0], path->slots[0],
1655 struct btrfs_inode_item);
1656 btrfs_set_inode_nbytes(path->nodes[0], ii, nbytes);
1657 btrfs_mark_buffer_dirty(path->nodes[0]);
1659 btrfs_commit_transaction(trans, root);
1662 error("failed to set nbytes in inode %llu root %llu",
1663 ino, root->root_key.objectid);
1665 printf("Set nbytes in inode item %llu root %llu\n to %llu", ino,
1666 root->root_key.objectid, nbytes);
1669 btrfs_release_path(path);
1670 ret = btrfs_search_slot(NULL, root, &research_key, path, 0, 0);
1677 * Set directory inode isize to @isize.
1679 * Returns 0 on success.
1680 * Returns != 0 on error.
1682 static int repair_dir_isize_lowmem(struct btrfs_root *root,
1683 struct btrfs_path *path,
1686 struct btrfs_trans_handle *trans;
1687 struct btrfs_inode_item *ii;
1688 struct btrfs_key key;
1689 struct btrfs_key research_key;
1693 btrfs_item_key_to_cpu(path->nodes[0], &research_key, path->slots[0]);
1696 key.type = BTRFS_INODE_ITEM_KEY;
1699 trans = btrfs_start_transaction(root, 1);
1700 if (IS_ERR(trans)) {
1701 ret = PTR_ERR(trans);
1706 btrfs_release_path(path);
1707 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
1715 ii = btrfs_item_ptr(path->nodes[0], path->slots[0],
1716 struct btrfs_inode_item);
1717 btrfs_set_inode_size(path->nodes[0], ii, isize);
1718 btrfs_mark_buffer_dirty(path->nodes[0]);
1720 btrfs_commit_transaction(trans, root);
1723 error("failed to set isize in inode %llu root %llu",
1724 ino, root->root_key.objectid);
1726 printf("Set isize in inode %llu root %llu to %llu\n",
1727 ino, root->root_key.objectid, isize);
1729 btrfs_release_path(path);
1730 ret = btrfs_search_slot(NULL, root, &research_key, path, 0, 0);
1737 * Wrapper function for btrfs_add_orphan_item().
1739 * Returns 0 on success.
1740 * Returns != 0 on error.
1742 static int repair_inode_orphan_item_lowmem(struct btrfs_root *root,
1743 struct btrfs_path *path, u64 ino)
1745 struct btrfs_trans_handle *trans;
1746 struct btrfs_key research_key;
1750 btrfs_item_key_to_cpu(path->nodes[0], &research_key, path->slots[0]);
1752 trans = btrfs_start_transaction(root, 1);
1753 if (IS_ERR(trans)) {
1754 ret = PTR_ERR(trans);
1759 btrfs_release_path(path);
1760 ret = btrfs_add_orphan_item(trans, root, path, ino);
1762 btrfs_commit_transaction(trans, root);
1765 error("failed to add inode %llu as orphan item root %llu",
1766 ino, root->root_key.objectid);
1768 printf("Added inode %llu as orphan item root %llu\n",
1769 ino, root->root_key.objectid);
1771 btrfs_release_path(path);
1772 ret = btrfs_search_slot(NULL, root, &research_key, path, 0, 0);
1778 /* Set inode_item nlink to @ref_count.
1779 * If @ref_count == 0, move it to "lost+found" and increase @ref_count.
1781 * Returns 0 on success
1783 static int repair_inode_nlinks_lowmem(struct btrfs_root *root,
1784 struct btrfs_path *path, u64 ino,
1785 const char *name, u32 namelen,
1786 u64 ref_count, u8 filetype, u64 *nlink)
1788 struct btrfs_trans_handle *trans;
1789 struct btrfs_inode_item *ii;
1790 struct btrfs_key key;
1791 struct btrfs_key old_key;
1792 char namebuf[BTRFS_NAME_LEN] = {0};
1798 btrfs_item_key_to_cpu(path->nodes[0], &old_key, path->slots[0]);
1800 if (name && namelen) {
1801 ASSERT(namelen <= BTRFS_NAME_LEN);
1802 memcpy(namebuf, name, namelen);
1805 sprintf(namebuf, "%llu", ino);
1806 name_len = count_digits(ino);
1807 printf("Can't find file name for inode %llu, use %s instead\n",
1811 trans = btrfs_start_transaction(root, 1);
1812 if (IS_ERR(trans)) {
1813 ret = PTR_ERR(trans);
1817 btrfs_release_path(path);
1818 /* if refs is 0, put it into lostfound */
1819 if (ref_count == 0) {
1820 ret = link_inode_to_lostfound(trans, root, path, ino, namebuf,
1821 name_len, filetype, &ref_count);
1826 /* reset inode_item's nlink to ref_count */
1828 key.type = BTRFS_INODE_ITEM_KEY;
1831 btrfs_release_path(path);
1832 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
1838 ii = btrfs_item_ptr(path->nodes[0], path->slots[0],
1839 struct btrfs_inode_item);
1840 btrfs_set_inode_nlink(path->nodes[0], ii, ref_count);
1841 btrfs_mark_buffer_dirty(path->nodes[0]);
1846 btrfs_commit_transaction(trans, root);
1850 "fail to repair nlink of inode %llu root %llu name %s filetype %u",
1851 root->objectid, ino, namebuf, filetype);
1853 printf("Fixed nlink of inode %llu root %llu name %s filetype %u\n",
1854 root->objectid, ino, namebuf, filetype);
1857 btrfs_release_path(path);
1858 ret2 = btrfs_search_slot(NULL, root, &old_key, path, 0, 0);
1864 static bool has_orphan_item(struct btrfs_root *root, u64 ino)
1866 struct btrfs_path path;
1867 struct btrfs_key key;
1870 btrfs_init_path(&path);
1871 key.objectid = BTRFS_ORPHAN_OBJECTID;
1872 key.type = BTRFS_ORPHAN_ITEM_KEY;
1875 ret = btrfs_search_slot(NULL, root, &key, &path, 0, 0);
1876 btrfs_release_path(&path);
1883 * Check INODE_ITEM and related ITEMs (the same inode number)
1884 * 1. check link count
1885 * 2. check inode ref/extref
1886 * 3. check dir item/index
1888 * @ext_ref: the EXTENDED_IREF feature
1890 * Return 0 if no error occurred.
1891 * Return >0 for error or hit the traversal is done(by error bitmap)
1893 static int check_inode_item(struct btrfs_root *root, struct btrfs_path *path,
1894 unsigned int ext_ref)
1896 struct extent_buffer *node;
1897 struct btrfs_inode_item *ii;
1898 struct btrfs_key key;
1899 struct btrfs_key last_key;
1908 u64 extent_size = 0;
1910 unsigned int nodatasum;
1911 bool is_orphan = false;
1915 char namebuf[BTRFS_NAME_LEN] = {0};
1918 node = path->nodes[0];
1919 slot = path->slots[0];
1921 btrfs_item_key_to_cpu(node, &key, slot);
1922 inode_id = key.objectid;
1924 if (inode_id == BTRFS_ORPHAN_OBJECTID) {
1925 ret = btrfs_next_item(root, path);
1931 ii = btrfs_item_ptr(node, slot, struct btrfs_inode_item);
1932 isize = btrfs_inode_size(node, ii);
1933 nbytes = btrfs_inode_nbytes(node, ii);
1934 mode = btrfs_inode_mode(node, ii);
1935 dir = imode_to_type(mode) == BTRFS_FT_DIR;
1936 nlink = btrfs_inode_nlink(node, ii);
1937 nodatasum = btrfs_inode_flags(node, ii) & BTRFS_INODE_NODATASUM;
1940 btrfs_item_key_to_cpu(path->nodes[0], &last_key, path->slots[0]);
1941 ret = btrfs_next_item(root, path);
1943 /* out will fill 'err' rusing current statistics */
1945 } else if (ret > 0) {
1950 node = path->nodes[0];
1951 slot = path->slots[0];
1952 btrfs_item_key_to_cpu(node, &key, slot);
1953 if (key.objectid != inode_id)
1957 case BTRFS_INODE_REF_KEY:
1958 ret = check_inode_ref(root, &key, path, namebuf,
1959 &name_len, &refs, mode);
1962 case BTRFS_INODE_EXTREF_KEY:
1963 if (key.type == BTRFS_INODE_EXTREF_KEY && !ext_ref)
1964 warning("root %llu EXTREF[%llu %llu] isn't supported",
1965 root->objectid, key.objectid,
1967 ret = check_inode_extref(root, &key, node, slot, &refs,
1971 case BTRFS_DIR_ITEM_KEY:
1972 case BTRFS_DIR_INDEX_KEY:
1974 warning("root %llu INODE[%llu] mode %u shouldn't have DIR_INDEX[%llu %llu]",
1975 root->objectid, inode_id,
1976 imode_to_type(mode), key.objectid,
1979 ret = check_dir_item(root, &key, path, &size, ext_ref);
1982 case BTRFS_EXTENT_DATA_KEY:
1984 warning("root %llu DIR INODE[%llu] shouldn't EXTENT_DATA[%llu %llu]",
1985 root->objectid, inode_id, key.objectid,
1988 ret = check_file_extent(root, &key, node, slot,
1989 nodatasum, &extent_size,
1993 case BTRFS_XATTR_ITEM_KEY:
1996 error("ITEM[%llu %u %llu] UNKNOWN TYPE",
1997 key.objectid, key.type, key.offset);
2002 if (err & LAST_ITEM) {
2003 btrfs_release_path(path);
2004 ret = btrfs_search_slot(NULL, root, &last_key, path, 0, 0);
2009 /* verify INODE_ITEM nlink/isize/nbytes */
2011 if (repair && (err & DIR_COUNT_AGAIN)) {
2012 err &= ~DIR_COUNT_AGAIN;
2013 count_dir_isize(root, inode_id, &size);
2016 if ((nlink != 1 || refs != 1) && repair) {
2017 ret = repair_inode_nlinks_lowmem(root, path, inode_id,
2018 namebuf, name_len, refs, imode_to_type(mode),
2023 err |= LINK_COUNT_ERROR;
2024 error("root %llu DIR INODE[%llu] shouldn't have more than one link(%llu)",
2025 root->objectid, inode_id, nlink);
2029 * Just a warning, as dir inode nbytes is just an
2030 * instructive value.
2032 if (!IS_ALIGNED(nbytes, root->fs_info->nodesize)) {
2033 warning("root %llu DIR INODE[%llu] nbytes should be aligned to %u",
2034 root->objectid, inode_id,
2035 root->fs_info->nodesize);
2038 if (isize != size) {
2040 ret = repair_dir_isize_lowmem(root, path,
2042 if (!repair || ret) {
2045 "root %llu DIR INODE [%llu] size %llu not equal to %llu",
2046 root->objectid, inode_id, isize, size);
2050 if (nlink != refs) {
2052 ret = repair_inode_nlinks_lowmem(root, path,
2053 inode_id, namebuf, name_len, refs,
2054 imode_to_type(mode), &nlink);
2055 if (!repair || ret) {
2056 err |= LINK_COUNT_ERROR;
2058 "root %llu INODE[%llu] nlink(%llu) not equal to inode_refs(%llu)",
2059 root->objectid, inode_id, nlink, refs);
2061 } else if (!nlink) {
2062 is_orphan = has_orphan_item(root, inode_id);
2063 if (!is_orphan && repair)
2064 ret = repair_inode_orphan_item_lowmem(root,
2066 if (!is_orphan && (!repair || ret)) {
2068 error("root %llu INODE[%llu] is orphan item",
2069 root->objectid, inode_id);
2073 if (!nbytes && !no_holes && extent_end < isize) {
2075 ret = punch_extent_hole(root, inode_id,
2076 extent_end, isize - extent_end);
2077 if (!repair || ret) {
2078 err |= NBYTES_ERROR;
2080 "root %llu INODE[%llu] size %llu should have a file extent hole",
2081 root->objectid, inode_id, isize);
2085 if (nbytes != extent_size) {
2087 ret = repair_inode_nbytes_lowmem(root, path,
2088 inode_id, extent_size);
2089 if (!repair || ret) {
2090 err |= NBYTES_ERROR;
2092 "root %llu INODE[%llu] nbytes %llu not equal to extent_size %llu",
2093 root->objectid, inode_id, nbytes,
2099 if (err & LAST_ITEM)
2100 btrfs_next_item(root, path);
2105 * Returns >0 Found error, not fatal, should continue
2106 * Returns <0 Fatal error, must exit the whole check
2107 * Returns 0 No errors found
2109 static int process_one_leaf(struct btrfs_root *root, struct btrfs_path *path,
2110 struct node_refs *nrefs, int *level, int ext_ref)
2112 struct extent_buffer *cur = path->nodes[0];
2113 struct btrfs_key key;
2117 int root_level = btrfs_header_level(root->node);
2119 int ret = 0; /* Final return value */
2120 int err = 0; /* Positive error bitmap */
2122 cur_bytenr = cur->start;
2124 /* skip to first inode item or the first inode number change */
2125 nritems = btrfs_header_nritems(cur);
2126 for (i = 0; i < nritems; i++) {
2127 btrfs_item_key_to_cpu(cur, &key, i);
2129 first_ino = key.objectid;
2130 if (key.type == BTRFS_INODE_ITEM_KEY ||
2131 (first_ino && first_ino != key.objectid))
2135 path->slots[0] = nritems;
2141 err |= check_inode_item(root, path, ext_ref);
2143 /* modify cur since check_inode_item may change path */
2144 cur = path->nodes[0];
2146 if (err & LAST_ITEM)
2149 /* still have inode items in thie leaf */
2150 if (cur->start == cur_bytenr)
2154 * we have switched to another leaf, above nodes may
2155 * have changed, here walk down the path, if a node
2156 * or leaf is shared, check whether we can skip this
2159 for (i = root_level; i >= 0; i--) {
2160 if (path->nodes[i]->start == nrefs->bytenr[i])
2163 ret = update_nodes_refs(root, path->nodes[i]->start,
2164 path->nodes[i], nrefs, i, 0);
2168 if (!nrefs->need_check[i]) {
2174 for (i = 0; i < *level; i++) {
2175 free_extent_buffer(path->nodes[i]);
2176 path->nodes[i] = NULL;
2186 * @level if @level == -1 means extent data item
2187 * else normal treeblocl.
2189 static int should_check_extent_strictly(struct btrfs_root *root,
2190 struct node_refs *nrefs, int level)
2192 int root_level = btrfs_header_level(root->node);
2194 if (level > root_level || level < -1)
2196 if (level == root_level)
2199 * if the upper node is marked full backref, it should contain shared
2200 * backref of the parent (except owner == root->objectid).
2202 while (++level <= root_level)
2203 if (nrefs->refs[level] > 1)
2209 static int check_extent_inline_ref(struct extent_buffer *eb,
2210 struct btrfs_key *key, struct btrfs_extent_inline_ref *iref)
2213 u8 type = btrfs_extent_inline_ref_type(eb, iref);
2216 case BTRFS_TREE_BLOCK_REF_KEY:
2217 case BTRFS_EXTENT_DATA_REF_KEY:
2218 case BTRFS_SHARED_BLOCK_REF_KEY:
2219 case BTRFS_SHARED_DATA_REF_KEY:
2223 error("extent[%llu %u %llu] has unknown ref type: %d",
2224 key->objectid, key->type, key->offset, type);
2233 * Check backrefs of a tree block given by @bytenr or @eb.
2235 * @root: the root containing the @bytenr or @eb
2236 * @eb: tree block extent buffer, can be NULL
2237 * @bytenr: bytenr of the tree block to search
2238 * @level: tree level of the tree block
2239 * @owner: owner of the tree block
2241 * Return >0 for any error found and output error message
2242 * Return 0 for no error found
2244 static int check_tree_block_ref(struct btrfs_root *root,
2245 struct extent_buffer *eb, u64 bytenr,
2246 int level, u64 owner, struct node_refs *nrefs)
2248 struct btrfs_key key;
2249 struct btrfs_root *extent_root = root->fs_info->extent_root;
2250 struct btrfs_path path;
2251 struct btrfs_extent_item *ei;
2252 struct btrfs_extent_inline_ref *iref;
2253 struct extent_buffer *leaf;
2258 int root_level = btrfs_header_level(root->node);
2260 u32 nodesize = root->fs_info->nodesize;
2269 btrfs_init_path(&path);
2270 key.objectid = bytenr;
2271 if (btrfs_fs_incompat(root->fs_info, SKINNY_METADATA))
2272 key.type = BTRFS_METADATA_ITEM_KEY;
2274 key.type = BTRFS_EXTENT_ITEM_KEY;
2275 key.offset = (u64)-1;
2277 /* Search for the backref in extent tree */
2278 ret = btrfs_search_slot(NULL, extent_root, &key, &path, 0, 0);
2280 err |= BACKREF_MISSING;
2283 ret = btrfs_previous_extent_item(extent_root, &path, bytenr);
2285 err |= BACKREF_MISSING;
2289 leaf = path.nodes[0];
2290 slot = path.slots[0];
2291 btrfs_item_key_to_cpu(leaf, &key, slot);
2293 ei = btrfs_item_ptr(leaf, slot, struct btrfs_extent_item);
2295 if (key.type == BTRFS_METADATA_ITEM_KEY) {
2296 skinny_level = (int)key.offset;
2297 iref = (struct btrfs_extent_inline_ref *)(ei + 1);
2299 struct btrfs_tree_block_info *info;
2301 info = (struct btrfs_tree_block_info *)(ei + 1);
2302 skinny_level = btrfs_tree_block_level(leaf, info);
2303 iref = (struct btrfs_extent_inline_ref *)(info + 1);
2312 * Due to the feature of shared tree blocks, if the upper node
2313 * is a fs root or shared node, the extent of checked node may
2314 * not be updated until the next CoW.
2317 strict = should_check_extent_strictly(root, nrefs,
2319 if (!(btrfs_extent_flags(leaf, ei) &
2320 BTRFS_EXTENT_FLAG_TREE_BLOCK)) {
2322 "extent[%llu %u] backref type mismatch, missing bit: %llx",
2323 key.objectid, nodesize,
2324 BTRFS_EXTENT_FLAG_TREE_BLOCK);
2325 err = BACKREF_MISMATCH;
2327 header_gen = btrfs_header_generation(eb);
2328 extent_gen = btrfs_extent_generation(leaf, ei);
2329 if (header_gen != extent_gen) {
2331 "extent[%llu %u] backref generation mismatch, wanted: %llu, have: %llu",
2332 key.objectid, nodesize, header_gen,
2334 err = BACKREF_MISMATCH;
2336 if (level != skinny_level) {
2338 "extent[%llu %u] level mismatch, wanted: %u, have: %u",
2339 key.objectid, nodesize, level, skinny_level);
2340 err = BACKREF_MISMATCH;
2342 if (!is_fstree(owner) && btrfs_extent_refs(leaf, ei) != 1) {
2344 "extent[%llu %u] is referred by other roots than %llu",
2345 key.objectid, nodesize, root->objectid);
2346 err = BACKREF_MISMATCH;
2351 * Iterate the extent/metadata item to find the exact backref
2353 item_size = btrfs_item_size_nr(leaf, slot);
2354 ptr = (unsigned long)iref;
2355 end = (unsigned long)ei + item_size;
2358 iref = (struct btrfs_extent_inline_ref *)ptr;
2359 type = btrfs_extent_inline_ref_type(leaf, iref);
2360 offset = btrfs_extent_inline_ref_offset(leaf, iref);
2362 ret = check_extent_inline_ref(leaf, &key, iref);
2367 if (type == BTRFS_TREE_BLOCK_REF_KEY) {
2368 if (offset == root->objectid)
2370 if (!strict && owner == offset)
2372 } else if (type == BTRFS_SHARED_BLOCK_REF_KEY) {
2374 * Backref of tree reloc root points to itself, no need
2375 * to check backref any more.
2377 * This may be an error of loop backref, but extent tree
2378 * checker should have already handled it.
2379 * Here we only need to avoid infinite iteration.
2381 if (offset == bytenr) {
2385 * Check if the backref points to valid
2388 found_ref = !check_tree_block_ref(root, NULL,
2389 offset, level + 1, owner, NULL);
2395 ptr += btrfs_extent_inline_ref_size(type);
2399 * Inlined extent item doesn't have what we need, check
2400 * TREE_BLOCK_REF_KEY
2403 btrfs_release_path(&path);
2404 key.objectid = bytenr;
2405 key.type = BTRFS_TREE_BLOCK_REF_KEY;
2406 key.offset = root->objectid;
2408 ret = btrfs_search_slot(NULL, extent_root, &key, &path, 0, 0);
2413 * Finally check SHARED BLOCK REF, any found will be good
2414 * Here we're not doing comprehensive extent backref checking,
2415 * only need to ensure there is some extent referring to this
2419 btrfs_release_path(&path);
2420 key.objectid = bytenr;
2421 key.type = BTRFS_SHARED_BLOCK_REF_KEY;
2422 key.offset = (u64)-1;
2424 ret = btrfs_search_slot(NULL, extent_root, &key, &path, 0, 0);
2426 err |= BACKREF_MISSING;
2429 ret = btrfs_previous_extent_item(extent_root, &path, bytenr);
2431 err |= BACKREF_MISSING;
2437 err |= BACKREF_MISSING;
2439 btrfs_release_path(&path);
2440 if (nrefs && strict &&
2441 level < root_level && nrefs->full_backref[level + 1])
2442 parent = nrefs->bytenr[level + 1];
2443 if (eb && (err & BACKREF_MISSING))
2445 "extent[%llu %u] backref lost (owner: %llu, level: %u) %s %llu",
2446 bytenr, nodesize, owner, level,
2447 parent ? "parent" : "root",
2448 parent ? parent : root->objectid);
2453 * If @err contains BACKREF_MISSING then add extent of the
2454 * file_extent_data_item.
2456 * Returns error bits after reapir.
2458 static int repair_extent_data_item(struct btrfs_trans_handle *trans,
2459 struct btrfs_root *root,
2460 struct btrfs_path *pathp,
2461 struct node_refs *nrefs,
2464 struct btrfs_file_extent_item *fi;
2465 struct btrfs_key fi_key;
2466 struct btrfs_key key;
2467 struct btrfs_extent_item *ei;
2468 struct btrfs_path path;
2469 struct btrfs_root *extent_root = root->fs_info->extent_root;
2470 struct extent_buffer *eb;
2482 eb = pathp->nodes[0];
2483 slot = pathp->slots[0];
2484 btrfs_item_key_to_cpu(eb, &fi_key, slot);
2485 fi = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item);
2487 if (btrfs_file_extent_type(eb, fi) == BTRFS_FILE_EXTENT_INLINE ||
2488 btrfs_file_extent_disk_bytenr(eb, fi) == 0)
2491 file_offset = fi_key.offset;
2492 generation = btrfs_file_extent_generation(eb, fi);
2493 disk_bytenr = btrfs_file_extent_disk_bytenr(eb, fi);
2494 num_bytes = btrfs_file_extent_disk_num_bytes(eb, fi);
2495 extent_offset = btrfs_file_extent_offset(eb, fi);
2496 offset = file_offset - extent_offset;
2498 /* now repair only adds backref */
2499 if ((err & BACKREF_MISSING) == 0)
2502 /* search extent item */
2503 key.objectid = disk_bytenr;
2504 key.type = BTRFS_EXTENT_ITEM_KEY;
2505 key.offset = num_bytes;
2507 btrfs_init_path(&path);
2508 ret = btrfs_search_slot(NULL, extent_root, &key, &path, 0, 0);
2514 /* insert an extent item */
2516 key.objectid = disk_bytenr;
2517 key.type = BTRFS_EXTENT_ITEM_KEY;
2518 key.offset = num_bytes;
2521 btrfs_release_path(&path);
2522 ret = btrfs_insert_empty_item(trans, extent_root, &path, &key,
2527 ei = btrfs_item_ptr(eb, path.slots[0], struct btrfs_extent_item);
2529 btrfs_set_extent_refs(eb, ei, 0);
2530 btrfs_set_extent_generation(eb, ei, generation);
2531 btrfs_set_extent_flags(eb, ei, BTRFS_EXTENT_FLAG_DATA);
2533 btrfs_mark_buffer_dirty(eb);
2534 ret = btrfs_update_block_group(extent_root, disk_bytenr,
2536 btrfs_release_path(&path);
2539 if (nrefs->full_backref[0])
2540 parent = btrfs_header_bytenr(eb);
2544 ret = btrfs_inc_extent_ref(trans, root, disk_bytenr, num_bytes, parent,
2546 parent ? BTRFS_FIRST_FREE_OBJECTID : fi_key.objectid,
2550 "failed to increase extent data backref[%llu %llu] root %llu",
2551 disk_bytenr, num_bytes, root->objectid);
2554 printf("Add one extent data backref [%llu %llu]\n",
2555 disk_bytenr, num_bytes);
2558 err &= ~BACKREF_MISSING;
2561 error("can't repair root %llu extent data item[%llu %llu]",
2562 root->objectid, disk_bytenr, num_bytes);
2567 * Check EXTENT_DATA item, mainly for its dbackref in extent tree
2569 * Return >0 any error found and output error message
2570 * Return 0 for no error found
2572 static int check_extent_data_item(struct btrfs_root *root,
2573 struct btrfs_path *pathp,
2574 struct node_refs *nrefs, int account_bytes)
2576 struct btrfs_file_extent_item *fi;
2577 struct extent_buffer *eb = pathp->nodes[0];
2578 struct btrfs_path path;
2579 struct btrfs_root *extent_root = root->fs_info->extent_root;
2580 struct btrfs_key fi_key;
2581 struct btrfs_key dbref_key;
2582 struct extent_buffer *leaf;
2583 struct btrfs_extent_item *ei;
2584 struct btrfs_extent_inline_ref *iref;
2585 struct btrfs_extent_data_ref *dref;
2589 u64 extent_num_bytes;
2596 int found_dbackref = 0;
2597 int slot = pathp->slots[0];
2602 btrfs_item_key_to_cpu(eb, &fi_key, slot);
2603 fi = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item);
2605 /* Nothing to check for hole and inline data extents */
2606 if (btrfs_file_extent_type(eb, fi) == BTRFS_FILE_EXTENT_INLINE ||
2607 btrfs_file_extent_disk_bytenr(eb, fi) == 0)
2610 disk_bytenr = btrfs_file_extent_disk_bytenr(eb, fi);
2611 disk_num_bytes = btrfs_file_extent_disk_num_bytes(eb, fi);
2612 extent_num_bytes = btrfs_file_extent_num_bytes(eb, fi);
2613 offset = btrfs_file_extent_offset(eb, fi);
2615 /* Check unaligned disk_num_bytes and num_bytes */
2616 if (!IS_ALIGNED(disk_num_bytes, root->fs_info->sectorsize)) {
2618 "file extent [%llu, %llu] has unaligned disk num bytes: %llu, should be aligned to %u",
2619 fi_key.objectid, fi_key.offset, disk_num_bytes,
2620 root->fs_info->sectorsize);
2621 err |= BYTES_UNALIGNED;
2622 } else if (account_bytes) {
2623 data_bytes_allocated += disk_num_bytes;
2625 if (!IS_ALIGNED(extent_num_bytes, root->fs_info->sectorsize)) {
2627 "file extent [%llu, %llu] has unaligned num bytes: %llu, should be aligned to %u",
2628 fi_key.objectid, fi_key.offset, extent_num_bytes,
2629 root->fs_info->sectorsize);
2630 err |= BYTES_UNALIGNED;
2631 } else if (account_bytes) {
2632 data_bytes_referenced += extent_num_bytes;
2634 owner = btrfs_header_owner(eb);
2636 /* Check the extent item of the file extent in extent tree */
2637 btrfs_init_path(&path);
2638 dbref_key.objectid = btrfs_file_extent_disk_bytenr(eb, fi);
2639 dbref_key.type = BTRFS_EXTENT_ITEM_KEY;
2640 dbref_key.offset = btrfs_file_extent_disk_num_bytes(eb, fi);
2642 ret = btrfs_search_slot(NULL, extent_root, &dbref_key, &path, 0, 0);
2646 leaf = path.nodes[0];
2647 slot = path.slots[0];
2648 ei = btrfs_item_ptr(leaf, slot, struct btrfs_extent_item);
2650 extent_flags = btrfs_extent_flags(leaf, ei);
2652 if (!(extent_flags & BTRFS_EXTENT_FLAG_DATA)) {
2654 "file extent[%llu %llu] root %llu owner %llu backref type mismatch, wanted bit: %llx",
2655 fi_key.objectid, fi_key.offset, root->objectid, owner,
2656 BTRFS_EXTENT_FLAG_DATA);
2657 err |= BACKREF_MISMATCH;
2660 /* Check data backref inside that extent item */
2661 item_size = btrfs_item_size_nr(leaf, path.slots[0]);
2662 iref = (struct btrfs_extent_inline_ref *)(ei + 1);
2663 ptr = (unsigned long)iref;
2664 end = (unsigned long)ei + item_size;
2665 strict = should_check_extent_strictly(root, nrefs, -1);
2673 iref = (struct btrfs_extent_inline_ref *)ptr;
2674 type = btrfs_extent_inline_ref_type(leaf, iref);
2675 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
2677 ret = check_extent_inline_ref(leaf, &dbref_key, iref);
2682 if (type == BTRFS_EXTENT_DATA_REF_KEY) {
2683 ref_root = btrfs_extent_data_ref_root(leaf, dref);
2684 ref_objectid = btrfs_extent_data_ref_objectid(leaf,
2686 ref_offset = btrfs_extent_data_ref_offset(leaf, dref);
2688 if (ref_objectid == fi_key.objectid &&
2689 ref_offset == fi_key.offset - offset)
2691 if (ref_root == root->objectid && match)
2693 else if (!strict && owner == ref_root && match)
2695 } else if (type == BTRFS_SHARED_DATA_REF_KEY) {
2696 found_dbackref = !check_tree_block_ref(root, NULL,
2697 btrfs_extent_inline_ref_offset(leaf, iref),
2703 ptr += btrfs_extent_inline_ref_size(type);
2706 if (!found_dbackref) {
2707 btrfs_release_path(&path);
2709 /* Didn't find inlined data backref, try EXTENT_DATA_REF_KEY */
2710 dbref_key.objectid = btrfs_file_extent_disk_bytenr(eb, fi);
2711 dbref_key.type = BTRFS_EXTENT_DATA_REF_KEY;
2712 dbref_key.offset = hash_extent_data_ref(owner, fi_key.objectid,
2713 fi_key.offset - offset);
2715 ret = btrfs_search_slot(NULL, root->fs_info->extent_root,
2716 &dbref_key, &path, 0, 0);
2722 btrfs_release_path(&path);
2725 * Neither inlined nor EXTENT_DATA_REF found, try
2726 * SHARED_DATA_REF as last chance.
2728 dbref_key.objectid = disk_bytenr;
2729 dbref_key.type = BTRFS_SHARED_DATA_REF_KEY;
2730 dbref_key.offset = eb->start;
2732 ret = btrfs_search_slot(NULL, root->fs_info->extent_root,
2733 &dbref_key, &path, 0, 0);
2741 if (!found_dbackref)
2742 err |= BACKREF_MISSING;
2743 btrfs_release_path(&path);
2744 if (err & BACKREF_MISSING) {
2746 "file extent[%llu %llu] root %llu owner %llu backref lost",
2747 fi_key.objectid, fi_key.offset, root->objectid, owner);
2753 * Check a block group item with its referener (chunk) and its used space
2754 * with extent/metadata item
2756 static int check_block_group_item(struct btrfs_fs_info *fs_info,
2757 struct extent_buffer *eb, int slot)
2759 struct btrfs_root *extent_root = fs_info->extent_root;
2760 struct btrfs_root *chunk_root = fs_info->chunk_root;
2761 struct btrfs_block_group_item *bi;
2762 struct btrfs_block_group_item bg_item;
2763 struct btrfs_path path;
2764 struct btrfs_key bg_key;
2765 struct btrfs_key chunk_key;
2766 struct btrfs_key extent_key;
2767 struct btrfs_chunk *chunk;
2768 struct extent_buffer *leaf;
2769 struct btrfs_extent_item *ei;
2770 u32 nodesize = btrfs_super_nodesize(fs_info->super_copy);
2778 btrfs_item_key_to_cpu(eb, &bg_key, slot);
2779 bi = btrfs_item_ptr(eb, slot, struct btrfs_block_group_item);
2780 read_extent_buffer(eb, &bg_item, (unsigned long)bi, sizeof(bg_item));
2781 used = btrfs_block_group_used(&bg_item);
2782 bg_flags = btrfs_block_group_flags(&bg_item);
2784 chunk_key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
2785 chunk_key.type = BTRFS_CHUNK_ITEM_KEY;
2786 chunk_key.offset = bg_key.objectid;
2788 btrfs_init_path(&path);
2789 /* Search for the referencer chunk */
2790 ret = btrfs_search_slot(NULL, chunk_root, &chunk_key, &path, 0, 0);
2793 "block group[%llu %llu] did not find the related chunk item",
2794 bg_key.objectid, bg_key.offset);
2795 err |= REFERENCER_MISSING;
2797 chunk = btrfs_item_ptr(path.nodes[0], path.slots[0],
2798 struct btrfs_chunk);
2799 if (btrfs_chunk_length(path.nodes[0], chunk) !=
2802 "block group[%llu %llu] related chunk item length does not match",
2803 bg_key.objectid, bg_key.offset);
2804 err |= REFERENCER_MISMATCH;
2807 btrfs_release_path(&path);
2809 /* Search from the block group bytenr */
2810 extent_key.objectid = bg_key.objectid;
2811 extent_key.type = 0;
2812 extent_key.offset = 0;
2814 btrfs_init_path(&path);
2815 ret = btrfs_search_slot(NULL, extent_root, &extent_key, &path, 0, 0);
2819 /* Iterate extent tree to account used space */
2821 leaf = path.nodes[0];
2823 /* Search slot can point to the last item beyond leaf nritems */
2824 if (path.slots[0] >= btrfs_header_nritems(leaf))
2827 btrfs_item_key_to_cpu(leaf, &extent_key, path.slots[0]);
2828 if (extent_key.objectid >= bg_key.objectid + bg_key.offset)
2831 if (extent_key.type != BTRFS_METADATA_ITEM_KEY &&
2832 extent_key.type != BTRFS_EXTENT_ITEM_KEY)
2834 if (extent_key.objectid < bg_key.objectid)
2837 if (extent_key.type == BTRFS_METADATA_ITEM_KEY)
2840 total += extent_key.offset;
2842 ei = btrfs_item_ptr(leaf, path.slots[0],
2843 struct btrfs_extent_item);
2844 flags = btrfs_extent_flags(leaf, ei);
2845 if (flags & BTRFS_EXTENT_FLAG_DATA) {
2846 if (!(bg_flags & BTRFS_BLOCK_GROUP_DATA)) {
2848 "bad extent[%llu, %llu) type mismatch with chunk",
2849 extent_key.objectid,
2850 extent_key.objectid + extent_key.offset);
2851 err |= CHUNK_TYPE_MISMATCH;
2853 } else if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
2854 if (!(bg_flags & (BTRFS_BLOCK_GROUP_SYSTEM |
2855 BTRFS_BLOCK_GROUP_METADATA))) {
2857 "bad extent[%llu, %llu) type mismatch with chunk",
2858 extent_key.objectid,
2859 extent_key.objectid + nodesize);
2860 err |= CHUNK_TYPE_MISMATCH;
2864 ret = btrfs_next_item(extent_root, &path);
2870 btrfs_release_path(&path);
2872 if (total != used) {
2874 "block group[%llu %llu] used %llu but extent items used %llu",
2875 bg_key.objectid, bg_key.offset, used, total);
2876 err |= BG_ACCOUNTING_ERROR;
2882 * Get real tree block level for the case like shared block
2883 * Return >= 0 as tree level
2884 * Return <0 for error
2886 static int query_tree_block_level(struct btrfs_fs_info *fs_info, u64 bytenr)
2888 struct extent_buffer *eb;
2889 struct btrfs_path path;
2890 struct btrfs_key key;
2891 struct btrfs_extent_item *ei;
2898 /* Search extent tree for extent generation and level */
2899 key.objectid = bytenr;
2900 key.type = BTRFS_METADATA_ITEM_KEY;
2901 key.offset = (u64)-1;
2903 btrfs_init_path(&path);
2904 ret = btrfs_search_slot(NULL, fs_info->extent_root, &key, &path, 0, 0);
2907 ret = btrfs_previous_extent_item(fs_info->extent_root, &path, bytenr);
2915 btrfs_item_key_to_cpu(path.nodes[0], &key, path.slots[0]);
2916 ei = btrfs_item_ptr(path.nodes[0], path.slots[0],
2917 struct btrfs_extent_item);
2918 flags = btrfs_extent_flags(path.nodes[0], ei);
2919 if (!(flags & BTRFS_EXTENT_FLAG_TREE_BLOCK)) {
2924 /* Get transid for later read_tree_block() check */
2925 transid = btrfs_extent_generation(path.nodes[0], ei);
2927 /* Get backref level as one source */
2928 if (key.type == BTRFS_METADATA_ITEM_KEY) {
2929 backref_level = key.offset;
2931 struct btrfs_tree_block_info *info;
2933 info = (struct btrfs_tree_block_info *)(ei + 1);
2934 backref_level = btrfs_tree_block_level(path.nodes[0], info);
2936 btrfs_release_path(&path);
2938 /* Get level from tree block as an alternative source */
2939 eb = read_tree_block(fs_info, bytenr, transid);
2940 if (!extent_buffer_uptodate(eb)) {
2941 free_extent_buffer(eb);
2944 header_level = btrfs_header_level(eb);
2945 free_extent_buffer(eb);
2947 if (header_level != backref_level)
2949 return header_level;
2952 btrfs_release_path(&path);
2957 * Check if a tree block backref is valid (points to a valid tree block)
2958 * if level == -1, level will be resolved
2959 * Return >0 for any error found and print error message
2961 static int check_tree_block_backref(struct btrfs_fs_info *fs_info, u64 root_id,
2962 u64 bytenr, int level)
2964 struct btrfs_root *root;
2965 struct btrfs_key key;
2966 struct btrfs_path path;
2967 struct extent_buffer *eb;
2968 struct extent_buffer *node;
2969 u32 nodesize = btrfs_super_nodesize(fs_info->super_copy);
2973 /* Query level for level == -1 special case */
2975 level = query_tree_block_level(fs_info, bytenr);
2977 err |= REFERENCER_MISSING;
2981 key.objectid = root_id;
2982 key.type = BTRFS_ROOT_ITEM_KEY;
2983 key.offset = (u64)-1;
2985 root = btrfs_read_fs_root(fs_info, &key);
2987 err |= REFERENCER_MISSING;
2991 /* Read out the tree block to get item/node key */
2992 eb = read_tree_block(fs_info, bytenr, 0);
2993 if (!extent_buffer_uptodate(eb)) {
2994 err |= REFERENCER_MISSING;
2995 free_extent_buffer(eb);
2999 /* Empty tree, no need to check key */
3000 if (!btrfs_header_nritems(eb) && !level) {
3001 free_extent_buffer(eb);
3006 btrfs_node_key_to_cpu(eb, &key, 0);
3008 btrfs_item_key_to_cpu(eb, &key, 0);
3010 free_extent_buffer(eb);
3012 btrfs_init_path(&path);
3013 path.lowest_level = level;
3014 /* Search with the first key, to ensure we can reach it */
3015 ret = btrfs_search_slot(NULL, root, &key, &path, 0, 0);
3017 err |= REFERENCER_MISSING;
3021 node = path.nodes[level];
3022 if (btrfs_header_bytenr(node) != bytenr) {
3024 "extent [%llu %d] referencer bytenr mismatch, wanted: %llu, have: %llu",
3025 bytenr, nodesize, bytenr,
3026 btrfs_header_bytenr(node));
3027 err |= REFERENCER_MISMATCH;
3029 if (btrfs_header_level(node) != level) {
3031 "extent [%llu %d] referencer level mismatch, wanted: %d, have: %d",
3032 bytenr, nodesize, level,
3033 btrfs_header_level(node));
3034 err |= REFERENCER_MISMATCH;
3038 btrfs_release_path(&path);
3040 if (err & REFERENCER_MISSING) {
3042 error("extent [%llu %d] lost referencer (owner: %llu)",
3043 bytenr, nodesize, root_id);
3046 "extent [%llu %d] lost referencer (owner: %llu, level: %u)",
3047 bytenr, nodesize, root_id, level);
3054 * Check if tree block @eb is tree reloc root.
3055 * Return 0 if it's not or any problem happens
3056 * Return 1 if it's a tree reloc root
3058 static int is_tree_reloc_root(struct btrfs_fs_info *fs_info,
3059 struct extent_buffer *eb)
3061 struct btrfs_root *tree_reloc_root;
3062 struct btrfs_key key;
3063 u64 bytenr = btrfs_header_bytenr(eb);
3064 u64 owner = btrfs_header_owner(eb);
3067 key.objectid = BTRFS_TREE_RELOC_OBJECTID;
3069 key.type = BTRFS_ROOT_ITEM_KEY;
3071 tree_reloc_root = btrfs_read_fs_root_no_cache(fs_info, &key);
3072 if (IS_ERR(tree_reloc_root))
3075 if (bytenr == btrfs_header_bytenr(tree_reloc_root->node))
3077 btrfs_free_fs_root(tree_reloc_root);
3082 * Check referencer for shared block backref
3083 * If level == -1, this function will resolve the level.
3085 static int check_shared_block_backref(struct btrfs_fs_info *fs_info,
3086 u64 parent, u64 bytenr, int level)
3088 struct extent_buffer *eb;
3090 int found_parent = 0;
3093 eb = read_tree_block(fs_info, parent, 0);
3094 if (!extent_buffer_uptodate(eb))
3098 level = query_tree_block_level(fs_info, bytenr);
3102 /* It's possible it's a tree reloc root */
3103 if (parent == bytenr) {
3104 if (is_tree_reloc_root(fs_info, eb))
3109 if (level + 1 != btrfs_header_level(eb))
3112 nr = btrfs_header_nritems(eb);
3113 for (i = 0; i < nr; i++) {
3114 if (bytenr == btrfs_node_blockptr(eb, i)) {
3120 free_extent_buffer(eb);
3121 if (!found_parent) {
3123 "shared extent[%llu %u] lost its parent (parent: %llu, level: %u)",
3124 bytenr, fs_info->nodesize, parent, level);
3125 return REFERENCER_MISSING;
3131 * Check referencer for normal (inlined) data ref
3132 * If len == 0, it will be resolved by searching in extent tree
3134 static int check_extent_data_backref(struct btrfs_fs_info *fs_info,
3135 u64 root_id, u64 objectid, u64 offset,
3136 u64 bytenr, u64 len, u32 count)
3138 struct btrfs_root *root;
3139 struct btrfs_root *extent_root = fs_info->extent_root;
3140 struct btrfs_key key;
3141 struct btrfs_path path;
3142 struct extent_buffer *leaf;
3143 struct btrfs_file_extent_item *fi;
3144 u32 found_count = 0;
3149 key.objectid = bytenr;
3150 key.type = BTRFS_EXTENT_ITEM_KEY;
3151 key.offset = (u64)-1;
3153 btrfs_init_path(&path);
3154 ret = btrfs_search_slot(NULL, extent_root, &key, &path, 0, 0);
3157 ret = btrfs_previous_extent_item(extent_root, &path, bytenr);
3160 btrfs_item_key_to_cpu(path.nodes[0], &key, path.slots[0]);
3161 if (key.objectid != bytenr ||
3162 key.type != BTRFS_EXTENT_ITEM_KEY)
3165 btrfs_release_path(&path);
3167 key.objectid = root_id;
3168 key.type = BTRFS_ROOT_ITEM_KEY;
3169 key.offset = (u64)-1;
3170 btrfs_init_path(&path);
3172 root = btrfs_read_fs_root(fs_info, &key);
3176 key.objectid = objectid;
3177 key.type = BTRFS_EXTENT_DATA_KEY;
3179 * It can be nasty as data backref offset is
3180 * file offset - file extent offset, which is smaller or
3181 * equal to original backref offset. The only special case is
3182 * overflow. So we need to special check and do further search.
3184 key.offset = offset & (1ULL << 63) ? 0 : offset;
3186 ret = btrfs_search_slot(NULL, root, &key, &path, 0, 0);
3191 * Search afterwards to get correct one
3192 * NOTE: As we must do a comprehensive check on the data backref to
3193 * make sure the dref count also matches, we must iterate all file
3194 * extents for that inode.
3197 leaf = path.nodes[0];
3198 slot = path.slots[0];
3200 if (slot >= btrfs_header_nritems(leaf) ||
3201 btrfs_header_owner(leaf) != root_id)
3203 btrfs_item_key_to_cpu(leaf, &key, slot);
3204 if (key.objectid != objectid ||
3205 key.type != BTRFS_EXTENT_DATA_KEY)
3207 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
3209 * Except normal disk bytenr and disk num bytes, we still
3210 * need to do extra check on dbackref offset as
3211 * dbackref offset = file_offset - file_extent_offset
3213 * Also, we must check the leaf owner.
3214 * In case of shared tree blocks (snapshots) we can inherit
3215 * leaves from source snapshot.
3216 * In that case, reference from source snapshot should not
3219 if (btrfs_file_extent_disk_bytenr(leaf, fi) == bytenr &&
3220 btrfs_file_extent_disk_num_bytes(leaf, fi) == len &&
3221 (u64)(key.offset - btrfs_file_extent_offset(leaf, fi)) ==
3222 offset && btrfs_header_owner(leaf) == root_id)
3226 ret = btrfs_next_item(root, &path);
3231 btrfs_release_path(&path);
3232 if (found_count != count) {
3234 "extent[%llu, %llu] referencer count mismatch (root: %llu, owner: %llu, offset: %llu) wanted: %u, have: %u",
3235 bytenr, len, root_id, objectid, offset, count,
3237 return REFERENCER_MISSING;
3243 * Check if the referencer of a shared data backref exists
3245 static int check_shared_data_backref(struct btrfs_fs_info *fs_info,
3246 u64 parent, u64 bytenr)
3248 struct extent_buffer *eb;
3249 struct btrfs_key key;
3250 struct btrfs_file_extent_item *fi;
3252 int found_parent = 0;
3255 eb = read_tree_block(fs_info, parent, 0);
3256 if (!extent_buffer_uptodate(eb))
3259 nr = btrfs_header_nritems(eb);
3260 for (i = 0; i < nr; i++) {
3261 btrfs_item_key_to_cpu(eb, &key, i);
3262 if (key.type != BTRFS_EXTENT_DATA_KEY)
3265 fi = btrfs_item_ptr(eb, i, struct btrfs_file_extent_item);
3266 if (btrfs_file_extent_type(eb, fi) == BTRFS_FILE_EXTENT_INLINE)
3269 if (btrfs_file_extent_disk_bytenr(eb, fi) == bytenr) {
3276 free_extent_buffer(eb);
3277 if (!found_parent) {
3278 error("shared extent %llu referencer lost (parent: %llu)",
3280 return REFERENCER_MISSING;
3286 * Only delete backref if REFERENCER_MISSING now
3288 * Returns <0 the extent was deleted
3289 * Returns >0 the backref was deleted but extent still exists, returned value
3290 * means error after repair
3291 * Returns 0 nothing happened
3293 static int repair_extent_item(struct btrfs_trans_handle *trans,
3294 struct btrfs_root *root, struct btrfs_path *path,
3295 u64 bytenr, u64 num_bytes, u64 parent, u64 root_objectid,
3296 u64 owner, u64 offset, int err)
3298 struct btrfs_key old_key;
3302 btrfs_item_key_to_cpu(path->nodes[0], &old_key, path->slots[0]);
3304 if (err & (REFERENCER_MISSING | REFERENCER_MISMATCH)) {
3305 /* delete the backref */
3306 ret = btrfs_free_extent(trans, root->fs_info->fs_root, bytenr,
3307 num_bytes, parent, root_objectid, owner, offset);
3310 err &= ~REFERENCER_MISSING;
3311 printf("Delete backref in extent [%llu %llu]\n",
3314 error("fail to delete backref in extent [%llu %llu]",
3319 /* btrfs_free_extent may delete the extent */
3320 btrfs_release_path(path);
3321 ret = btrfs_search_slot(NULL, root, &old_key, path, 0, 0);
3331 * This function will check a given extent item, including its backref and
3332 * itself (like crossing stripe boundary and type)
3334 * Since we don't use extent_record anymore, introduce new error bit
3336 static int check_extent_item(struct btrfs_trans_handle *trans,
3337 struct btrfs_fs_info *fs_info,
3338 struct btrfs_path *path)
3340 struct btrfs_extent_item *ei;
3341 struct btrfs_extent_inline_ref *iref;
3342 struct btrfs_extent_data_ref *dref;
3343 struct extent_buffer *eb = path->nodes[0];
3346 int slot = path->slots[0];
3348 u32 nodesize = btrfs_super_nodesize(fs_info->super_copy);
3349 u32 item_size = btrfs_item_size_nr(eb, slot);
3359 struct btrfs_key key;
3363 btrfs_item_key_to_cpu(eb, &key, slot);
3364 if (key.type == BTRFS_EXTENT_ITEM_KEY) {
3365 bytes_used += key.offset;
3366 num_bytes = key.offset;
3368 bytes_used += nodesize;
3369 num_bytes = nodesize;
3372 if (item_size < sizeof(*ei)) {
3374 * COMPAT_EXTENT_TREE_V0 case, but it's already a super
3375 * old thing when on disk format is still un-determined.
3376 * No need to care about it anymore
3378 error("unsupported COMPAT_EXTENT_TREE_V0 detected");
3382 ei = btrfs_item_ptr(eb, slot, struct btrfs_extent_item);
3383 flags = btrfs_extent_flags(eb, ei);
3385 if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK)
3387 if (metadata && check_crossing_stripes(global_info, key.objectid,
3389 error("bad metadata [%llu, %llu) crossing stripe boundary",
3390 key.objectid, key.objectid + nodesize);
3391 err |= CROSSING_STRIPE_BOUNDARY;
3394 ptr = (unsigned long)(ei + 1);
3396 if (metadata && key.type == BTRFS_EXTENT_ITEM_KEY) {
3397 /* Old EXTENT_ITEM metadata */
3398 struct btrfs_tree_block_info *info;
3400 info = (struct btrfs_tree_block_info *)ptr;
3401 level = btrfs_tree_block_level(eb, info);
3402 ptr += sizeof(struct btrfs_tree_block_info);
3404 /* New METADATA_ITEM */
3407 end = (unsigned long)ei + item_size;
3410 /* Reached extent item end normally */
3414 /* Beyond extent item end, wrong item size */
3416 err |= ITEM_SIZE_MISMATCH;
3417 error("extent item at bytenr %llu slot %d has wrong size",
3426 /* Now check every backref in this extent item */
3427 iref = (struct btrfs_extent_inline_ref *)ptr;
3428 type = btrfs_extent_inline_ref_type(eb, iref);
3429 offset = btrfs_extent_inline_ref_offset(eb, iref);
3431 case BTRFS_TREE_BLOCK_REF_KEY:
3432 root_objectid = offset;
3434 ret = check_tree_block_backref(fs_info, offset, key.objectid,
3438 case BTRFS_SHARED_BLOCK_REF_KEY:
3440 ret = check_shared_block_backref(fs_info, offset, key.objectid,
3444 case BTRFS_EXTENT_DATA_REF_KEY:
3445 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
3446 root_objectid = btrfs_extent_data_ref_root(eb, dref);
3447 owner = btrfs_extent_data_ref_objectid(eb, dref);
3448 owner_offset = btrfs_extent_data_ref_offset(eb, dref);
3449 ret = check_extent_data_backref(fs_info, root_objectid, owner,
3450 owner_offset, key.objectid, key.offset,
3451 btrfs_extent_data_ref_count(eb, dref));
3454 case BTRFS_SHARED_DATA_REF_KEY:
3456 ret = check_shared_data_backref(fs_info, offset, key.objectid);
3460 error("extent[%llu %d %llu] has unknown ref type: %d",
3461 key.objectid, key.type, key.offset, type);
3467 if (err && repair) {
3468 ret = repair_extent_item(trans, fs_info->extent_root, path,
3469 key.objectid, num_bytes, parent, root_objectid,
3470 owner, owner_offset, ret);
3479 ptr += btrfs_extent_inline_ref_size(type);
3487 * Check if a dev extent item is referred correctly by its chunk
3489 static int check_dev_extent_item(struct btrfs_fs_info *fs_info,
3490 struct extent_buffer *eb, int slot)
3492 struct btrfs_root *chunk_root = fs_info->chunk_root;
3493 struct btrfs_dev_extent *ptr;
3494 struct btrfs_path path;
3495 struct btrfs_key chunk_key;
3496 struct btrfs_key devext_key;
3497 struct btrfs_chunk *chunk;
3498 struct extent_buffer *l;
3502 int found_chunk = 0;
3505 btrfs_item_key_to_cpu(eb, &devext_key, slot);
3506 ptr = btrfs_item_ptr(eb, slot, struct btrfs_dev_extent);
3507 length = btrfs_dev_extent_length(eb, ptr);
3509 chunk_key.objectid = btrfs_dev_extent_chunk_objectid(eb, ptr);
3510 chunk_key.type = BTRFS_CHUNK_ITEM_KEY;
3511 chunk_key.offset = btrfs_dev_extent_chunk_offset(eb, ptr);
3513 btrfs_init_path(&path);
3514 ret = btrfs_search_slot(NULL, chunk_root, &chunk_key, &path, 0, 0);
3519 chunk = btrfs_item_ptr(l, path.slots[0], struct btrfs_chunk);
3520 ret = btrfs_check_chunk_valid(fs_info, l, chunk, path.slots[0],
3525 if (btrfs_stripe_length(fs_info, l, chunk) != length)
3528 num_stripes = btrfs_chunk_num_stripes(l, chunk);
3529 for (i = 0; i < num_stripes; i++) {
3530 u64 devid = btrfs_stripe_devid_nr(l, chunk, i);
3531 u64 offset = btrfs_stripe_offset_nr(l, chunk, i);
3533 if (devid == devext_key.objectid &&
3534 offset == devext_key.offset) {
3540 btrfs_release_path(&path);
3543 "device extent[%llu, %llu, %llu] did not find the related chunk",
3544 devext_key.objectid, devext_key.offset, length);
3545 return REFERENCER_MISSING;
3551 * Check if the used space is correct with the dev item
3553 static int check_dev_item(struct btrfs_fs_info *fs_info,
3554 struct extent_buffer *eb, int slot)
3556 struct btrfs_root *dev_root = fs_info->dev_root;
3557 struct btrfs_dev_item *dev_item;
3558 struct btrfs_path path;
3559 struct btrfs_key key;
3560 struct btrfs_dev_extent *ptr;
3567 dev_item = btrfs_item_ptr(eb, slot, struct btrfs_dev_item);
3568 dev_id = btrfs_device_id(eb, dev_item);
3569 used = btrfs_device_bytes_used(eb, dev_item);
3570 total_bytes = btrfs_device_total_bytes(eb, dev_item);
3572 key.objectid = dev_id;
3573 key.type = BTRFS_DEV_EXTENT_KEY;
3576 btrfs_init_path(&path);
3577 ret = btrfs_search_slot(NULL, dev_root, &key, &path, 0, 0);
3579 btrfs_item_key_to_cpu(eb, &key, slot);
3580 error("cannot find any related dev extent for dev[%llu, %u, %llu]",
3581 key.objectid, key.type, key.offset);
3582 btrfs_release_path(&path);
3583 return REFERENCER_MISSING;
3586 /* Iterate dev_extents to calculate the used space of a device */
3588 if (path.slots[0] >= btrfs_header_nritems(path.nodes[0]))
3591 btrfs_item_key_to_cpu(path.nodes[0], &key, path.slots[0]);
3592 if (key.objectid > dev_id)
3594 if (key.type != BTRFS_DEV_EXTENT_KEY || key.objectid != dev_id)
3597 ptr = btrfs_item_ptr(path.nodes[0], path.slots[0],
3598 struct btrfs_dev_extent);
3599 total += btrfs_dev_extent_length(path.nodes[0], ptr);
3601 ret = btrfs_next_item(dev_root, &path);
3605 btrfs_release_path(&path);
3607 if (used != total) {
3608 btrfs_item_key_to_cpu(eb, &key, slot);
3610 "Dev extent's total-byte %llu is not equal to bytes-used %llu in dev[%llu, %u, %llu]",
3611 total, used, BTRFS_ROOT_TREE_OBJECTID,
3612 BTRFS_DEV_EXTENT_KEY, dev_id);
3613 return ACCOUNTING_MISMATCH;
3615 check_dev_size_alignment(dev_id, total_bytes, fs_info->sectorsize);
3621 * Check a chunk item.
3622 * Including checking all referred dev_extents and block group
3624 static int check_chunk_item(struct btrfs_fs_info *fs_info,
3625 struct extent_buffer *eb, int slot)
3627 struct btrfs_root *extent_root = fs_info->extent_root;
3628 struct btrfs_root *dev_root = fs_info->dev_root;
3629 struct btrfs_path path;
3630 struct btrfs_key chunk_key;
3631 struct btrfs_key bg_key;
3632 struct btrfs_key devext_key;
3633 struct btrfs_chunk *chunk;
3634 struct extent_buffer *leaf;
3635 struct btrfs_block_group_item *bi;
3636 struct btrfs_block_group_item bg_item;
3637 struct btrfs_dev_extent *ptr;
3649 btrfs_item_key_to_cpu(eb, &chunk_key, slot);
3650 chunk = btrfs_item_ptr(eb, slot, struct btrfs_chunk);
3651 length = btrfs_chunk_length(eb, chunk);
3652 chunk_end = chunk_key.offset + length;
3653 ret = btrfs_check_chunk_valid(fs_info, eb, chunk, slot,
3656 error("chunk[%llu %llu) is invalid", chunk_key.offset,
3658 err |= BYTES_UNALIGNED | UNKNOWN_TYPE;
3661 type = btrfs_chunk_type(eb, chunk);
3663 bg_key.objectid = chunk_key.offset;
3664 bg_key.type = BTRFS_BLOCK_GROUP_ITEM_KEY;
3665 bg_key.offset = length;
3667 btrfs_init_path(&path);
3668 ret = btrfs_search_slot(NULL, extent_root, &bg_key, &path, 0, 0);
3671 "chunk[%llu %llu) did not find the related block group item",
3672 chunk_key.offset, chunk_end);
3673 err |= REFERENCER_MISSING;
3675 leaf = path.nodes[0];
3676 bi = btrfs_item_ptr(leaf, path.slots[0],
3677 struct btrfs_block_group_item);
3678 read_extent_buffer(leaf, &bg_item, (unsigned long)bi,
3680 if (btrfs_block_group_flags(&bg_item) != type) {
3682 "chunk[%llu %llu) related block group item flags mismatch, wanted: %llu, have: %llu",
3683 chunk_key.offset, chunk_end, type,
3684 btrfs_block_group_flags(&bg_item));
3685 err |= REFERENCER_MISSING;
3689 num_stripes = btrfs_chunk_num_stripes(eb, chunk);
3690 stripe_len = btrfs_stripe_length(fs_info, eb, chunk);
3691 for (i = 0; i < num_stripes; i++) {
3692 btrfs_release_path(&path);
3693 btrfs_init_path(&path);
3694 devext_key.objectid = btrfs_stripe_devid_nr(eb, chunk, i);
3695 devext_key.type = BTRFS_DEV_EXTENT_KEY;
3696 devext_key.offset = btrfs_stripe_offset_nr(eb, chunk, i);
3698 ret = btrfs_search_slot(NULL, dev_root, &devext_key, &path,
3703 leaf = path.nodes[0];
3704 ptr = btrfs_item_ptr(leaf, path.slots[0],
3705 struct btrfs_dev_extent);
3706 objectid = btrfs_dev_extent_chunk_objectid(leaf, ptr);
3707 offset = btrfs_dev_extent_chunk_offset(leaf, ptr);
3708 if (objectid != chunk_key.objectid ||
3709 offset != chunk_key.offset ||
3710 btrfs_dev_extent_length(leaf, ptr) != stripe_len)
3714 err |= BACKREF_MISSING;
3716 "chunk[%llu %llu) stripe %d did not find the related dev extent",
3717 chunk_key.objectid, chunk_end, i);
3720 btrfs_release_path(&path);
3726 * Add block group item to the extent tree if @err contains REFERENCER_MISSING.
3727 * FIXME: We still need to repair error of dev_item.
3729 * Returns error after repair.
3731 static int repair_chunk_item(struct btrfs_trans_handle *trans,
3732 struct btrfs_root *chunk_root,
3733 struct btrfs_path *path, int err)
3735 struct btrfs_chunk *chunk;
3736 struct btrfs_key chunk_key;
3737 struct extent_buffer *eb = path->nodes[0];
3739 int slot = path->slots[0];
3743 btrfs_item_key_to_cpu(eb, &chunk_key, slot);
3744 if (chunk_key.type != BTRFS_CHUNK_ITEM_KEY)
3746 chunk = btrfs_item_ptr(eb, slot, struct btrfs_chunk);
3747 type = btrfs_chunk_type(path->nodes[0], chunk);
3748 length = btrfs_chunk_length(eb, chunk);
3750 if (err & REFERENCER_MISSING) {
3751 ret = btrfs_make_block_group(trans, chunk_root->fs_info, 0,
3752 type, chunk_key.offset, length);
3754 error("fail to add block group item[%llu %llu]",
3755 chunk_key.offset, length);
3758 err &= ~REFERENCER_MISSING;
3759 printf("Added block group item[%llu %llu]\n",
3760 chunk_key.offset, length);
3768 static int delete_extent_tree_item(struct btrfs_trans_handle *trans,
3769 struct btrfs_root *root,
3770 struct btrfs_path *path)
3772 struct btrfs_key key;
3775 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
3776 btrfs_release_path(path);
3777 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
3783 ret = btrfs_del_item(trans, root, path);
3787 if (path->slots[0] == 0)
3788 btrfs_prev_leaf(root, path);
3793 error("failed to delete root %llu item[%llu, %u, %llu]",
3794 root->objectid, key.objectid, key.type, key.offset);
3796 printf("Deleted root %llu item[%llu, %u, %llu]\n",
3797 root->objectid, key.objectid, key.type, key.offset);
3802 * Main entry function to check known items and update related accounting info
3804 static int check_leaf_items(struct btrfs_trans_handle *trans,
3805 struct btrfs_root *root, struct btrfs_path *path,
3806 struct node_refs *nrefs, int account_bytes)
3808 struct btrfs_fs_info *fs_info = root->fs_info;
3809 struct btrfs_key key;
3810 struct extent_buffer *eb;
3813 struct btrfs_extent_data_ref *dref;
3818 eb = path->nodes[0];
3819 slot = path->slots[0];
3820 if (slot >= btrfs_header_nritems(eb)) {
3822 error("empty leaf [%llu %u] root %llu", eb->start,
3823 root->fs_info->nodesize, root->objectid);
3829 btrfs_item_key_to_cpu(eb, &key, slot);
3833 case BTRFS_EXTENT_DATA_KEY:
3834 ret = check_extent_data_item(root, path, nrefs, account_bytes);
3836 ret = repair_extent_data_item(trans, root, path, nrefs,
3840 case BTRFS_BLOCK_GROUP_ITEM_KEY:
3841 ret = check_block_group_item(fs_info, eb, slot);
3843 ret & REFERENCER_MISSING)
3844 ret = delete_extent_tree_item(trans, root, path);
3847 case BTRFS_DEV_ITEM_KEY:
3848 ret = check_dev_item(fs_info, eb, slot);
3851 case BTRFS_CHUNK_ITEM_KEY:
3852 ret = check_chunk_item(fs_info, eb, slot);
3854 ret = repair_chunk_item(trans, root, path, ret);
3857 case BTRFS_DEV_EXTENT_KEY:
3858 ret = check_dev_extent_item(fs_info, eb, slot);
3861 case BTRFS_EXTENT_ITEM_KEY:
3862 case BTRFS_METADATA_ITEM_KEY:
3863 ret = check_extent_item(trans, fs_info, path);
3866 case BTRFS_EXTENT_CSUM_KEY:
3867 total_csum_bytes += btrfs_item_size_nr(eb, slot);
3870 case BTRFS_TREE_BLOCK_REF_KEY:
3871 ret = check_tree_block_backref(fs_info, key.offset,
3874 ret & (REFERENCER_MISMATCH | REFERENCER_MISSING))
3875 ret = delete_extent_tree_item(trans, root, path);
3878 case BTRFS_EXTENT_DATA_REF_KEY:
3879 dref = btrfs_item_ptr(eb, slot, struct btrfs_extent_data_ref);
3880 ret = check_extent_data_backref(fs_info,
3881 btrfs_extent_data_ref_root(eb, dref),
3882 btrfs_extent_data_ref_objectid(eb, dref),
3883 btrfs_extent_data_ref_offset(eb, dref),
3885 btrfs_extent_data_ref_count(eb, dref));
3887 ret & (REFERENCER_MISMATCH | REFERENCER_MISSING))
3888 ret = delete_extent_tree_item(trans, root, path);
3891 case BTRFS_SHARED_BLOCK_REF_KEY:
3892 ret = check_shared_block_backref(fs_info, key.offset,
3895 ret & (REFERENCER_MISMATCH | REFERENCER_MISSING))
3896 ret = delete_extent_tree_item(trans, root, path);
3899 case BTRFS_SHARED_DATA_REF_KEY:
3900 ret = check_shared_data_backref(fs_info, key.offset,
3903 ret & (REFERENCER_MISMATCH | REFERENCER_MISSING))
3904 ret = delete_extent_tree_item(trans, root, path);
3918 * @trans just for lowmem repair mode
3919 * @check all if not 0 then check all tree block backrefs and items
3920 * 0 then just check relationship of items in fs tree(s)
3922 * Returns >0 Found error, should continue
3923 * Returns <0 Fatal error, must exit the whole check
3924 * Returns 0 No errors found
3926 static int walk_down_tree(struct btrfs_trans_handle *trans,
3927 struct btrfs_root *root, struct btrfs_path *path,
3928 int *level, struct node_refs *nrefs, int ext_ref,
3931 enum btrfs_tree_block_status status;
3934 struct btrfs_fs_info *fs_info = root->fs_info;
3935 struct extent_buffer *next;
3936 struct extent_buffer *cur;
3940 int account_file_data = 0;
3942 WARN_ON(*level < 0);
3943 WARN_ON(*level >= BTRFS_MAX_LEVEL);
3945 ret = update_nodes_refs(root, btrfs_header_bytenr(path->nodes[*level]),
3946 path->nodes[*level], nrefs, *level, check_all);
3950 while (*level >= 0) {
3951 WARN_ON(*level < 0);
3952 WARN_ON(*level >= BTRFS_MAX_LEVEL);
3953 cur = path->nodes[*level];
3954 bytenr = btrfs_header_bytenr(cur);
3955 check = nrefs->need_check[*level];
3957 if (btrfs_header_level(cur) != *level)
3960 * Update bytes accounting and check tree block ref
3961 * NOTE: Doing accounting and check before checking nritems
3962 * is necessary because of empty node/leaf.
3964 if ((check_all && !nrefs->checked[*level]) ||
3965 (!check_all && nrefs->need_check[*level])) {
3966 ret = check_tree_block_ref(root, cur,
3967 btrfs_header_bytenr(cur), btrfs_header_level(cur),
3968 btrfs_header_owner(cur), nrefs);
3971 ret = repair_tree_block_ref(trans, root,
3972 path->nodes[*level], nrefs, *level, ret);
3975 if (check_all && nrefs->need_check[*level] &&
3976 nrefs->refs[*level]) {
3977 account_bytes(root, path, *level);
3978 account_file_data = 1;
3980 nrefs->checked[*level] = 1;
3983 if (path->slots[*level] >= btrfs_header_nritems(cur))
3986 /* Don't forgot to check leaf/node validation */
3988 /* skip duplicate check */
3989 if (check || !check_all) {
3990 ret = btrfs_check_leaf(root, NULL, cur);
3991 if (ret != BTRFS_TREE_BLOCK_CLEAN) {
3999 ret = process_one_leaf(root, path, nrefs,
4002 ret = check_leaf_items(trans, root, path,
4003 nrefs, account_file_data);
4007 if (check || !check_all) {
4008 ret = btrfs_check_node(root, NULL, cur);
4009 if (ret != BTRFS_TREE_BLOCK_CLEAN) {
4015 bytenr = btrfs_node_blockptr(cur, path->slots[*level]);
4016 ptr_gen = btrfs_node_ptr_generation(cur, path->slots[*level]);
4018 ret = update_nodes_refs(root, bytenr, NULL, nrefs, *level - 1,
4023 * check all trees in check_chunks_and_extent
4024 * check shared node once in check_fs_roots
4026 if (!check_all && !nrefs->need_check[*level - 1]) {
4027 path->slots[*level]++;
4031 next = btrfs_find_tree_block(fs_info, bytenr, fs_info->nodesize);
4032 if (!next || !btrfs_buffer_uptodate(next, ptr_gen)) {
4033 free_extent_buffer(next);
4034 reada_walk_down(root, cur, path->slots[*level]);
4035 next = read_tree_block(fs_info, bytenr, ptr_gen);
4036 if (!extent_buffer_uptodate(next)) {
4037 struct btrfs_key node_key;
4039 btrfs_node_key_to_cpu(path->nodes[*level],
4041 path->slots[*level]);
4042 btrfs_add_corrupt_extent_record(fs_info,
4043 &node_key, path->nodes[*level]->start,
4044 fs_info->nodesize, *level);
4050 ret = check_child_node(cur, path->slots[*level], next);
4055 if (btrfs_is_leaf(next))
4056 status = btrfs_check_leaf(root, NULL, next);
4058 status = btrfs_check_node(root, NULL, next);
4059 if (status != BTRFS_TREE_BLOCK_CLEAN) {
4060 free_extent_buffer(next);
4065 *level = *level - 1;
4066 free_extent_buffer(path->nodes[*level]);
4067 path->nodes[*level] = next;
4068 path->slots[*level] = 0;
4069 account_file_data = 0;
4071 update_nodes_refs(root, (u64)-1, next, nrefs, *level, check_all);
4076 static int walk_up_tree(struct btrfs_root *root, struct btrfs_path *path,
4080 struct extent_buffer *leaf;
4082 for (i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) {
4083 leaf = path->nodes[i];
4084 if (path->slots[i] + 1 < btrfs_header_nritems(leaf)) {
4089 free_extent_buffer(path->nodes[*level]);
4090 path->nodes[*level] = NULL;
4097 * Insert the missing inode item and inode ref.
4099 * Normal INODE_ITEM_MISSING and INODE_REF_MISSING are handled in backref * dir.
4100 * Root dir should be handled specially because root dir is the root of fs.
4102 * returns err (>0 or 0) after repair
4104 static int repair_fs_first_inode(struct btrfs_root *root, int err)
4106 struct btrfs_trans_handle *trans;
4107 struct btrfs_key key;
4108 struct btrfs_path path;
4109 int filetype = BTRFS_FT_DIR;
4112 btrfs_init_path(&path);
4114 if (err & INODE_REF_MISSING) {
4115 key.objectid = BTRFS_FIRST_FREE_OBJECTID;
4116 key.type = BTRFS_INODE_REF_KEY;
4117 key.offset = BTRFS_FIRST_FREE_OBJECTID;
4119 trans = btrfs_start_transaction(root, 1);
4120 if (IS_ERR(trans)) {
4121 ret = PTR_ERR(trans);
4125 btrfs_release_path(&path);
4126 ret = btrfs_search_slot(trans, root, &key, &path, 1, 1);
4130 ret = btrfs_insert_inode_ref(trans, root, "..", 2,
4131 BTRFS_FIRST_FREE_OBJECTID,
4132 BTRFS_FIRST_FREE_OBJECTID, 0);
4136 printf("Add INODE_REF[%llu %llu] name %s\n",
4137 BTRFS_FIRST_FREE_OBJECTID, BTRFS_FIRST_FREE_OBJECTID,
4139 err &= ~INODE_REF_MISSING;
4142 error("fail to insert first inode's ref");
4143 btrfs_commit_transaction(trans, root);
4146 if (err & INODE_ITEM_MISSING) {
4147 ret = repair_inode_item_missing(root,
4148 BTRFS_FIRST_FREE_OBJECTID, filetype);
4151 err &= ~INODE_ITEM_MISSING;
4155 error("fail to repair first inode");
4156 btrfs_release_path(&path);
4161 * check first root dir's inode_item and inode_ref
4163 * returns 0 means no error
4164 * returns >0 means error
4165 * returns <0 means fatal error
4167 static int check_fs_first_inode(struct btrfs_root *root, unsigned int ext_ref)
4169 struct btrfs_path path;
4170 struct btrfs_key key;
4171 struct btrfs_inode_item *ii;
4177 key.objectid = BTRFS_FIRST_FREE_OBJECTID;
4178 key.type = BTRFS_INODE_ITEM_KEY;
4181 /* For root being dropped, we don't need to check first inode */
4182 if (btrfs_root_refs(&root->root_item) == 0 &&
4183 btrfs_disk_key_objectid(&root->root_item.drop_progress) >=
4184 BTRFS_FIRST_FREE_OBJECTID)
4187 btrfs_init_path(&path);
4188 ret = btrfs_search_slot(NULL, root, &key, &path, 0, 0);
4193 err |= INODE_ITEM_MISSING;
4195 ii = btrfs_item_ptr(path.nodes[0], path.slots[0],
4196 struct btrfs_inode_item);
4197 mode = btrfs_inode_mode(path.nodes[0], ii);
4198 if (imode_to_type(mode) != BTRFS_FT_DIR)
4199 err |= INODE_ITEM_MISMATCH;
4202 /* lookup first inode ref */
4203 key.offset = BTRFS_FIRST_FREE_OBJECTID;
4204 key.type = BTRFS_INODE_REF_KEY;
4205 /* special index value */
4208 ret = find_inode_ref(root, &key, "..", strlen(".."), &index, ext_ref);
4214 btrfs_release_path(&path);
4217 err = repair_fs_first_inode(root, err);
4219 if (err & (INODE_ITEM_MISSING | INODE_ITEM_MISMATCH))
4220 error("root dir INODE_ITEM is %s",
4221 err & INODE_ITEM_MISMATCH ? "mismatch" : "missing");
4222 if (err & INODE_REF_MISSING)
4223 error("root dir INODE_REF is missing");
4225 return ret < 0 ? ret : err;
4229 * This function calls walk_down_tree and walk_up_tree to check tree
4230 * blocks and integrity of fs tree items.
4232 * @root: the root of the tree to be checked.
4233 * @ext_ref feature EXTENDED_IREF is enable or not.
4234 * @account if NOT 0 means check the tree (including tree)'s treeblocks.
4235 * otherwise means check fs tree(s) items relationship and
4236 * @root MUST be a fs tree root.
4237 * Returns 0 represents OK.
4238 * Returns not 0 represents error.
4240 static int check_btrfs_root(struct btrfs_trans_handle *trans,
4241 struct btrfs_root *root, unsigned int ext_ref,
4244 struct btrfs_path path;
4245 struct node_refs nrefs;
4246 struct btrfs_root_item *root_item = &root->root_item;
4251 memset(&nrefs, 0, sizeof(nrefs));
4254 * We need to manually check the first inode item (256)
4255 * As the following traversal function will only start from
4256 * the first inode item in the leaf, if inode item (256) is
4257 * missing we will skip it forever.
4259 ret = check_fs_first_inode(root, ext_ref);
4265 level = btrfs_header_level(root->node);
4266 btrfs_init_path(&path);
4268 if (btrfs_root_refs(root_item) > 0 ||
4269 btrfs_disk_key_objectid(&root_item->drop_progress) == 0) {
4270 path.nodes[level] = root->node;
4271 path.slots[level] = 0;
4272 extent_buffer_get(root->node);
4274 struct btrfs_key key;
4276 btrfs_disk_key_to_cpu(&key, &root_item->drop_progress);
4277 level = root_item->drop_level;
4278 path.lowest_level = level;
4279 ret = btrfs_search_slot(NULL, root, &key, &path, 0, 0);
4286 ret = walk_down_tree(trans, root, &path, &level, &nrefs,
4287 ext_ref, check_all);
4291 /* if ret is negative, walk shall stop */
4297 ret = walk_up_tree(root, &path, &level);
4299 /* Normal exit, reset ret to err */
4306 btrfs_release_path(&path);
4311 * Iterate all items in the tree and call check_inode_item() to check.
4313 * @root: the root of the tree to be checked.
4314 * @ext_ref: the EXTENDED_IREF feature
4316 * Return 0 if no error found.
4317 * Return <0 for error.
4319 static int check_fs_root(struct btrfs_root *root, unsigned int ext_ref)
4321 reset_cached_block_groups(root->fs_info);
4322 return check_btrfs_root(NULL, root, ext_ref, 0);
4326 * Find the relative ref for root_ref and root_backref.
4328 * @root: the root of the root tree.
4329 * @ref_key: the key of the root ref.
4331 * Return 0 if no error occurred.
4333 static int check_root_ref(struct btrfs_root *root, struct btrfs_key *ref_key,
4334 struct extent_buffer *node, int slot)
4336 struct btrfs_path path;
4337 struct btrfs_key key;
4338 struct btrfs_root_ref *ref;
4339 struct btrfs_root_ref *backref;
4340 char ref_name[BTRFS_NAME_LEN] = {0};
4341 char backref_name[BTRFS_NAME_LEN] = {0};
4347 u32 backref_namelen;
4352 ref = btrfs_item_ptr(node, slot, struct btrfs_root_ref);
4353 ref_dirid = btrfs_root_ref_dirid(node, ref);
4354 ref_seq = btrfs_root_ref_sequence(node, ref);
4355 ref_namelen = btrfs_root_ref_name_len(node, ref);
4357 if (ref_namelen <= BTRFS_NAME_LEN) {
4360 len = BTRFS_NAME_LEN;
4361 warning("%s[%llu %llu] ref_name too long",
4362 ref_key->type == BTRFS_ROOT_REF_KEY ?
4363 "ROOT_REF" : "ROOT_BACKREF", ref_key->objectid,
4366 read_extent_buffer(node, ref_name, (unsigned long)(ref + 1), len);
4368 /* Find relative root_ref */
4369 key.objectid = ref_key->offset;
4370 key.type = BTRFS_ROOT_BACKREF_KEY + BTRFS_ROOT_REF_KEY - ref_key->type;
4371 key.offset = ref_key->objectid;
4373 btrfs_init_path(&path);
4374 ret = btrfs_search_slot(NULL, root, &key, &path, 0, 0);
4376 err |= ROOT_REF_MISSING;
4377 error("%s[%llu %llu] couldn't find relative ref",
4378 ref_key->type == BTRFS_ROOT_REF_KEY ?
4379 "ROOT_REF" : "ROOT_BACKREF",
4380 ref_key->objectid, ref_key->offset);
4384 backref = btrfs_item_ptr(path.nodes[0], path.slots[0],
4385 struct btrfs_root_ref);
4386 backref_dirid = btrfs_root_ref_dirid(path.nodes[0], backref);
4387 backref_seq = btrfs_root_ref_sequence(path.nodes[0], backref);
4388 backref_namelen = btrfs_root_ref_name_len(path.nodes[0], backref);
4390 if (backref_namelen <= BTRFS_NAME_LEN) {
4391 len = backref_namelen;
4393 len = BTRFS_NAME_LEN;
4394 warning("%s[%llu %llu] ref_name too long",
4395 key.type == BTRFS_ROOT_REF_KEY ?
4396 "ROOT_REF" : "ROOT_BACKREF",
4397 key.objectid, key.offset);
4399 read_extent_buffer(path.nodes[0], backref_name,
4400 (unsigned long)(backref + 1), len);
4402 if (ref_dirid != backref_dirid || ref_seq != backref_seq ||
4403 ref_namelen != backref_namelen ||
4404 strncmp(ref_name, backref_name, len)) {
4405 err |= ROOT_REF_MISMATCH;
4406 error("%s[%llu %llu] mismatch relative ref",
4407 ref_key->type == BTRFS_ROOT_REF_KEY ?
4408 "ROOT_REF" : "ROOT_BACKREF",
4409 ref_key->objectid, ref_key->offset);
4412 btrfs_release_path(&path);
4417 * Check all fs/file tree in low_memory mode.
4419 * 1. for fs tree root item, call check_fs_root()
4420 * 2. for fs tree root ref/backref, call check_root_ref()
4422 * Return 0 if no error occurred.
4424 int check_fs_roots_lowmem(struct btrfs_fs_info *fs_info)
4426 struct btrfs_root *tree_root = fs_info->tree_root;
4427 struct btrfs_root *cur_root = NULL;
4428 struct btrfs_path path;
4429 struct btrfs_key key;
4430 struct extent_buffer *node;
4431 unsigned int ext_ref;
4436 ext_ref = btrfs_fs_incompat(fs_info, EXTENDED_IREF);
4438 btrfs_init_path(&path);
4439 key.objectid = BTRFS_FS_TREE_OBJECTID;
4441 key.type = BTRFS_ROOT_ITEM_KEY;
4443 ret = btrfs_search_slot(NULL, tree_root, &key, &path, 0, 0);
4447 } else if (ret > 0) {
4453 node = path.nodes[0];
4454 slot = path.slots[0];
4455 btrfs_item_key_to_cpu(node, &key, slot);
4456 if (key.objectid > BTRFS_LAST_FREE_OBJECTID)
4458 if (key.type == BTRFS_ROOT_ITEM_KEY &&
4459 fs_root_objectid(key.objectid)) {
4460 if (key.objectid == BTRFS_TREE_RELOC_OBJECTID) {
4461 cur_root = btrfs_read_fs_root_no_cache(fs_info,
4464 key.offset = (u64)-1;
4465 cur_root = btrfs_read_fs_root(fs_info, &key);
4468 if (IS_ERR(cur_root)) {
4469 error("Fail to read fs/subvol tree: %lld",
4475 ret = check_fs_root(cur_root, ext_ref);
4478 if (key.objectid == BTRFS_TREE_RELOC_OBJECTID)
4479 btrfs_free_fs_root(cur_root);
4480 } else if (key.type == BTRFS_ROOT_REF_KEY ||
4481 key.type == BTRFS_ROOT_BACKREF_KEY) {
4482 ret = check_root_ref(tree_root, &key, node, slot);
4486 ret = btrfs_next_item(tree_root, &path);
4496 btrfs_release_path(&path);
4501 * Low memory usage version check_chunks_and_extents.
4503 int check_chunks_and_extents_lowmem(struct btrfs_fs_info *fs_info)
4505 struct btrfs_trans_handle *trans = NULL;
4506 struct btrfs_path path;
4507 struct btrfs_key old_key;
4508 struct btrfs_key key;
4509 struct btrfs_root *root1;
4510 struct btrfs_root *root;
4511 struct btrfs_root *cur_root;
4515 root = fs_info->fs_root;
4518 trans = btrfs_start_transaction(fs_info->extent_root, 1);
4519 if (IS_ERR(trans)) {
4520 error("failed to start transaction before check");
4521 return PTR_ERR(trans);
4525 root1 = root->fs_info->chunk_root;
4526 ret = check_btrfs_root(trans, root1, 0, 1);
4529 root1 = root->fs_info->tree_root;
4530 ret = check_btrfs_root(trans, root1, 0, 1);
4533 btrfs_init_path(&path);
4534 key.objectid = BTRFS_EXTENT_TREE_OBJECTID;
4536 key.type = BTRFS_ROOT_ITEM_KEY;
4538 ret = btrfs_search_slot(NULL, root1, &key, &path, 0, 0);
4540 error("cannot find extent tree in tree_root");
4545 btrfs_item_key_to_cpu(path.nodes[0], &key, path.slots[0]);
4546 if (key.type != BTRFS_ROOT_ITEM_KEY)
4549 key.offset = (u64)-1;
4551 if (key.objectid == BTRFS_TREE_RELOC_OBJECTID)
4552 cur_root = btrfs_read_fs_root_no_cache(root->fs_info,
4555 cur_root = btrfs_read_fs_root(root->fs_info, &key);
4556 if (IS_ERR(cur_root) || !cur_root) {
4557 error("failed to read tree: %lld", key.objectid);
4561 ret = check_btrfs_root(trans, cur_root, 0, 1);
4564 if (key.objectid == BTRFS_TREE_RELOC_OBJECTID)
4565 btrfs_free_fs_root(cur_root);
4567 btrfs_release_path(&path);
4568 ret = btrfs_search_slot(NULL, root->fs_info->tree_root,
4569 &old_key, &path, 0, 0);
4573 ret = btrfs_next_item(root1, &path);
4579 /* if repair, update block accounting */
4581 ret = btrfs_fix_block_accounting(trans, root);
4585 err &= ~BG_ACCOUNTING_ERROR;
4589 btrfs_commit_transaction(trans, root->fs_info->extent_root);
4591 btrfs_release_path(&path);