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/common.h"
29 #include "check/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);
733 /* the item was deleted, let path point to the last checked item */
735 if (path->slots[0] == 0)
736 btrfs_prev_leaf(root, path);
744 location.objectid = ref_key->objectid;
745 location.type = BTRFS_INODE_ITEM_KEY;
747 node = path->nodes[0];
748 slot = path->slots[0];
750 memset(namebuf, 0, sizeof(namebuf) / sizeof(*namebuf));
751 ref = btrfs_item_ptr(node, slot, struct btrfs_inode_ref);
752 total = btrfs_item_size_nr(node, slot);
755 /* Update inode ref count */
758 index = btrfs_inode_ref_index(node, ref);
759 name_len = btrfs_inode_ref_name_len(node, ref);
761 if (name_len <= BTRFS_NAME_LEN) {
764 len = BTRFS_NAME_LEN;
765 warning("root %llu INODE_REF[%llu %llu] name too long",
766 root->objectid, ref_key->objectid, ref_key->offset);
769 read_extent_buffer(node, namebuf, (unsigned long)(ref + 1), len);
771 /* copy the first name found to name_ret */
772 if (refs == 1 && name_ret) {
773 memcpy(name_ret, namebuf, len);
777 /* Check root dir ref */
778 if (ref_key->objectid == BTRFS_FIRST_FREE_OBJECTID) {
779 if (index != 0 || len != strlen("..") ||
780 strncmp("..", namebuf, len) ||
781 ref_key->offset != BTRFS_FIRST_FREE_OBJECTID) {
782 /* set err bits then repair will delete the ref */
783 err |= DIR_INDEX_MISSING;
784 err |= DIR_ITEM_MISSING;
789 /* Find related DIR_INDEX */
790 key.objectid = ref_key->offset;
791 key.type = BTRFS_DIR_INDEX_KEY;
793 tmp_err |= find_dir_item(root, &key, &location, namebuf, len,
794 imode_to_type(mode));
796 /* Find related dir_item */
797 key.objectid = ref_key->offset;
798 key.type = BTRFS_DIR_ITEM_KEY;
799 key.offset = btrfs_name_hash(namebuf, len);
800 tmp_err |= find_dir_item(root, &key, &location, namebuf, len,
801 imode_to_type(mode));
803 if (tmp_err && repair) {
804 ret = repair_ternary_lowmem(root, ref_key->offset,
805 ref_key->objectid, index, namebuf,
806 name_len, imode_to_type(mode),
813 print_inode_ref_err(root, ref_key, index, namebuf, name_len,
814 imode_to_type(mode), tmp_err);
816 len = sizeof(*ref) + name_len;
817 ref = (struct btrfs_inode_ref *)((char *)ref + len);
828 * Traverse the given INODE_EXTREF and call find_dir_item() to find related
829 * DIR_ITEM/DIR_INDEX.
831 * @root: the root of the fs/file tree
832 * @ref_key: the key of the INODE_EXTREF
833 * @refs: the count of INODE_EXTREF
834 * @mode: the st_mode of INODE_ITEM
836 * Return 0 if no error occurred.
838 static int check_inode_extref(struct btrfs_root *root,
839 struct btrfs_key *ref_key,
840 struct extent_buffer *node, int slot, u64 *refs,
843 struct btrfs_key key;
844 struct btrfs_key location;
845 struct btrfs_inode_extref *extref;
846 char namebuf[BTRFS_NAME_LEN] = {0};
856 location.objectid = ref_key->objectid;
857 location.type = BTRFS_INODE_ITEM_KEY;
860 extref = btrfs_item_ptr(node, slot, struct btrfs_inode_extref);
861 total = btrfs_item_size_nr(node, slot);
864 /* update inode ref count */
866 name_len = btrfs_inode_extref_name_len(node, extref);
867 index = btrfs_inode_extref_index(node, extref);
868 parent = btrfs_inode_extref_parent(node, extref);
869 if (name_len <= BTRFS_NAME_LEN) {
872 len = BTRFS_NAME_LEN;
873 warning("root %llu INODE_EXTREF[%llu %llu] name too long",
874 root->objectid, ref_key->objectid, ref_key->offset);
876 read_extent_buffer(node, namebuf, (unsigned long)(extref + 1), len);
878 /* Check root dir ref name */
879 if (index == 0 && strncmp(namebuf, "..", name_len)) {
880 error("root %llu INODE_EXTREF[%llu %llu] ROOT_DIR name shouldn't be %s",
881 root->objectid, ref_key->objectid, ref_key->offset,
883 err |= ROOT_DIR_ERROR;
886 /* find related dir_index */
887 key.objectid = parent;
888 key.type = BTRFS_DIR_INDEX_KEY;
890 ret = find_dir_item(root, &key, &location, namebuf, len, mode);
893 /* find related dir_item */
894 key.objectid = parent;
895 key.type = BTRFS_DIR_ITEM_KEY;
896 key.offset = btrfs_name_hash(namebuf, len);
897 ret = find_dir_item(root, &key, &location, namebuf, len, mode);
900 len = sizeof(*extref) + name_len;
901 extref = (struct btrfs_inode_extref *)((char *)extref + len);
911 * Find INODE_REF/INODE_EXTREF for the given key and check it with the specified
912 * DIR_ITEM/DIR_INDEX match.
913 * Return with @index_ret.
915 * @root: the root of the fs/file tree
916 * @key: the key of the INODE_REF/INODE_EXTREF
917 * @name: the name in the INODE_REF/INODE_EXTREF
918 * @namelen: the length of name in the INODE_REF/INODE_EXTREF
919 * @index_ret: the index in the INODE_REF/INODE_EXTREF,
920 * value (64)-1 means do not check index
921 * @ext_ref: the EXTENDED_IREF feature
923 * Return 0 if no error occurred.
924 * Return >0 for error bitmap
926 static int find_inode_ref(struct btrfs_root *root, struct btrfs_key *key,
927 char *name, int namelen, u64 *index_ret,
928 unsigned int ext_ref)
930 struct btrfs_path path;
931 struct btrfs_inode_ref *ref;
932 struct btrfs_inode_extref *extref;
933 struct extent_buffer *node;
934 char ref_namebuf[BTRFS_NAME_LEN] = {0};
947 btrfs_init_path(&path);
948 ret = btrfs_search_slot(NULL, root, key, &path, 0, 0);
950 ret = INODE_REF_MISSING;
954 node = path.nodes[0];
955 slot = path.slots[0];
957 ref = btrfs_item_ptr(node, slot, struct btrfs_inode_ref);
958 total = btrfs_item_size_nr(node, slot);
960 /* Iterate all entry of INODE_REF */
961 while (cur < total) {
962 ret = INODE_REF_MISSING;
964 ref_namelen = btrfs_inode_ref_name_len(node, ref);
965 ref_index = btrfs_inode_ref_index(node, ref);
966 if (*index_ret != (u64)-1 && *index_ret != ref_index)
969 if (cur + sizeof(*ref) + ref_namelen > total ||
970 ref_namelen > BTRFS_NAME_LEN) {
971 warning("root %llu INODE %s[%llu %llu] name too long",
973 key->type == BTRFS_INODE_REF_KEY ?
975 key->objectid, key->offset);
977 if (cur + sizeof(*ref) > total)
979 len = min_t(u32, total - cur - sizeof(*ref),
985 read_extent_buffer(node, ref_namebuf, (unsigned long)(ref + 1),
988 if (len != namelen || strncmp(ref_namebuf, name, len))
991 *index_ret = ref_index;
995 len = sizeof(*ref) + ref_namelen;
996 ref = (struct btrfs_inode_ref *)((char *)ref + len);
1001 /* Skip if not support EXTENDED_IREF feature */
1005 btrfs_release_path(&path);
1006 btrfs_init_path(&path);
1008 dir_id = key->offset;
1009 key->type = BTRFS_INODE_EXTREF_KEY;
1010 key->offset = btrfs_extref_hash(dir_id, name, namelen);
1012 ret = btrfs_search_slot(NULL, root, key, &path, 0, 0);
1014 ret = INODE_REF_MISSING;
1018 node = path.nodes[0];
1019 slot = path.slots[0];
1021 extref = btrfs_item_ptr(node, slot, struct btrfs_inode_extref);
1023 total = btrfs_item_size_nr(node, slot);
1025 /* Iterate all entry of INODE_EXTREF */
1026 while (cur < total) {
1027 ret = INODE_REF_MISSING;
1029 ref_namelen = btrfs_inode_extref_name_len(node, extref);
1030 ref_index = btrfs_inode_extref_index(node, extref);
1031 parent = btrfs_inode_extref_parent(node, extref);
1032 if (*index_ret != (u64)-1 && *index_ret != ref_index)
1035 if (parent != dir_id)
1038 if (ref_namelen <= BTRFS_NAME_LEN) {
1041 len = BTRFS_NAME_LEN;
1042 warning("root %llu INODE %s[%llu %llu] name too long",
1044 key->type == BTRFS_INODE_REF_KEY ?
1046 key->objectid, key->offset);
1048 read_extent_buffer(node, ref_namebuf,
1049 (unsigned long)(extref + 1), len);
1051 if (len != namelen || strncmp(ref_namebuf, name, len))
1054 *index_ret = ref_index;
1059 len = sizeof(*extref) + ref_namelen;
1060 extref = (struct btrfs_inode_extref *)((char *)extref + len);
1065 btrfs_release_path(&path);
1069 static int create_inode_item_lowmem(struct btrfs_trans_handle *trans,
1070 struct btrfs_root *root, u64 ino,
1073 u32 mode = (filetype == BTRFS_FT_DIR ? S_IFDIR : S_IFREG) | 0755;
1075 return insert_inode_item(trans, root, ino, 0, 0, 0, mode);
1079 * Insert the missing inode item.
1081 * Returns 0 means success.
1082 * Returns <0 means error.
1084 static int repair_inode_item_missing(struct btrfs_root *root, u64 ino,
1087 struct btrfs_key key;
1088 struct btrfs_trans_handle *trans;
1089 struct btrfs_path path;
1093 key.type = BTRFS_INODE_ITEM_KEY;
1096 btrfs_init_path(&path);
1097 trans = btrfs_start_transaction(root, 1);
1098 if (IS_ERR(trans)) {
1103 ret = btrfs_search_slot(trans, root, &key, &path, 1, 1);
1104 if (ret < 0 || !ret)
1107 /* insert inode item */
1108 create_inode_item_lowmem(trans, root, ino, filetype);
1111 btrfs_commit_transaction(trans, root);
1114 error("failed to repair root %llu INODE ITEM[%llu] missing",
1115 root->objectid, ino);
1116 btrfs_release_path(&path);
1121 * Call repair_inode_item_missing and repair_ternary_lowmem to repair
1123 * Returns error after repair
1125 static int repair_dir_item(struct btrfs_root *root, u64 dirid, u64 ino,
1126 u64 index, u8 filetype, char *namebuf, u32 name_len,
1131 if (err & INODE_ITEM_MISSING) {
1132 ret = repair_inode_item_missing(root, ino, filetype);
1134 err &= ~(INODE_ITEM_MISMATCH | INODE_ITEM_MISSING);
1137 if (err & ~(INODE_ITEM_MISMATCH | INODE_ITEM_MISSING)) {
1138 ret = repair_ternary_lowmem(root, dirid, ino, index, namebuf,
1139 name_len, filetype, err);
1141 err &= ~(DIR_INDEX_MISMATCH | DIR_INDEX_MISSING);
1142 err &= ~(DIR_ITEM_MISMATCH | DIR_ITEM_MISSING);
1143 err &= ~(INODE_REF_MISSING);
1149 static void print_dir_item_err(struct btrfs_root *root, struct btrfs_key *key,
1150 u64 ino, u64 index, const char *namebuf,
1151 int name_len, u8 filetype, int err)
1153 if (err & (DIR_ITEM_MISMATCH | DIR_ITEM_MISSING)) {
1154 error("root %llu DIR ITEM[%llu %llu] name %s filetype %d %s",
1155 root->objectid, key->objectid, key->offset, namebuf,
1157 err & DIR_ITEM_MISMATCH ? "mismath" : "missing");
1160 if (err & (DIR_INDEX_MISMATCH | DIR_INDEX_MISSING)) {
1161 error("root %llu DIR INDEX[%llu %llu] name %s filetype %d %s",
1162 root->objectid, key->objectid, index, namebuf, filetype,
1163 err & DIR_ITEM_MISMATCH ? "mismath" : "missing");
1166 if (err & (INODE_ITEM_MISSING | INODE_ITEM_MISMATCH)) {
1168 "root %llu INODE_ITEM[%llu] index %llu name %s filetype %d %s",
1169 root->objectid, ino, index, namebuf, filetype,
1170 err & INODE_ITEM_MISMATCH ? "mismath" : "missing");
1173 if (err & INODE_REF_MISSING)
1175 "root %llu INODE REF[%llu, %llu] name %s filetype %u missing",
1176 root->objectid, ino, key->objectid, namebuf, filetype);
1181 * Traverse the given DIR_ITEM/DIR_INDEX and check related INODE_ITEM and
1182 * call find_inode_ref() to check related INODE_REF/INODE_EXTREF.
1184 * @root: the root of the fs/file tree
1185 * @key: the key of the INODE_REF/INODE_EXTREF
1187 * @size: the st_size of the INODE_ITEM
1188 * @ext_ref: the EXTENDED_IREF feature
1190 * Return 0 if no error occurred.
1191 * Return DIR_COUNT_AGAIN if the isize of the inode should be recalculated.
1193 static int check_dir_item(struct btrfs_root *root, struct btrfs_key *di_key,
1194 struct btrfs_path *path, u64 *size,
1195 unsigned int ext_ref)
1197 struct btrfs_dir_item *di;
1198 struct btrfs_inode_item *ii;
1199 struct btrfs_key key;
1200 struct btrfs_key location;
1201 struct extent_buffer *node;
1203 char namebuf[BTRFS_NAME_LEN] = {0};
1215 int need_research = 0;
1218 * For DIR_ITEM set index to (u64)-1, so that find_inode_ref
1219 * ignore index check.
1221 if (di_key->type == BTRFS_DIR_INDEX_KEY)
1222 index = di_key->offset;
1229 /* since after repair, path and the dir item may be changed */
1230 if (need_research) {
1232 err |= DIR_COUNT_AGAIN;
1233 btrfs_release_path(path);
1234 ret = btrfs_search_slot(NULL, root, di_key, path, 0, 0);
1235 /* the item was deleted, let path point the last checked item */
1237 if (path->slots[0] == 0)
1238 btrfs_prev_leaf(root, path);
1246 node = path->nodes[0];
1247 slot = path->slots[0];
1249 di = btrfs_item_ptr(node, slot, struct btrfs_dir_item);
1250 total = btrfs_item_size_nr(node, slot);
1251 memset(namebuf, 0, sizeof(namebuf) / sizeof(*namebuf));
1253 while (cur < total) {
1254 data_len = btrfs_dir_data_len(node, di);
1257 error("root %llu %s[%llu %llu] data_len shouldn't be %u",
1259 di_key->type == BTRFS_DIR_ITEM_KEY ? "DIR_ITEM" : "DIR_INDEX",
1260 di_key->objectid, di_key->offset, data_len);
1262 name_len = btrfs_dir_name_len(node, di);
1263 if (name_len <= BTRFS_NAME_LEN) {
1266 len = BTRFS_NAME_LEN;
1267 warning("root %llu %s[%llu %llu] name too long",
1269 di_key->type == BTRFS_DIR_ITEM_KEY ? "DIR_ITEM" : "DIR_INDEX",
1270 di_key->objectid, di_key->offset);
1272 (*size) += name_len;
1273 read_extent_buffer(node, namebuf, (unsigned long)(di + 1),
1275 filetype = btrfs_dir_type(node, di);
1277 if (di_key->type == BTRFS_DIR_ITEM_KEY &&
1278 di_key->offset != btrfs_name_hash(namebuf, len)) {
1280 error("root %llu DIR_ITEM[%llu %llu] name %s namelen %u filetype %u mismatch with its hash, wanted %llu have %llu",
1281 root->objectid, di_key->objectid, di_key->offset,
1282 namebuf, len, filetype, di_key->offset,
1283 btrfs_name_hash(namebuf, len));
1286 btrfs_dir_item_key_to_cpu(node, di, &location);
1287 /* Ignore related ROOT_ITEM check */
1288 if (location.type == BTRFS_ROOT_ITEM_KEY)
1291 btrfs_release_path(path);
1292 /* Check relative INODE_ITEM(existence/filetype) */
1293 ret = btrfs_search_slot(NULL, root, &location, path, 0, 0);
1295 tmp_err |= INODE_ITEM_MISSING;
1299 ii = btrfs_item_ptr(path->nodes[0], path->slots[0],
1300 struct btrfs_inode_item);
1301 mode = btrfs_inode_mode(path->nodes[0], ii);
1302 if (imode_to_type(mode) != filetype) {
1303 tmp_err |= INODE_ITEM_MISMATCH;
1307 /* Check relative INODE_REF/INODE_EXTREF */
1308 key.objectid = location.objectid;
1309 key.type = BTRFS_INODE_REF_KEY;
1310 key.offset = di_key->objectid;
1311 tmp_err |= find_inode_ref(root, &key, namebuf, len,
1314 /* check relative INDEX/ITEM */
1315 key.objectid = di_key->objectid;
1316 if (key.type == BTRFS_DIR_ITEM_KEY) {
1317 key.type = BTRFS_DIR_INDEX_KEY;
1320 key.type = BTRFS_DIR_ITEM_KEY;
1321 key.offset = btrfs_name_hash(namebuf, name_len);
1324 tmp_err |= find_dir_item(root, &key, &location, namebuf,
1325 name_len, filetype);
1326 /* find_dir_item may find index */
1327 if (key.type == BTRFS_DIR_INDEX_KEY)
1331 if (tmp_err && repair) {
1332 ret = repair_dir_item(root, di_key->objectid,
1333 location.objectid, index,
1334 imode_to_type(mode), namebuf,
1336 if (ret != tmp_err) {
1341 btrfs_release_path(path);
1342 print_dir_item_err(root, di_key, location.objectid, index,
1343 namebuf, name_len, filetype, tmp_err);
1345 len = sizeof(*di) + name_len + data_len;
1346 di = (struct btrfs_dir_item *)((char *)di + len);
1349 if (di_key->type == BTRFS_DIR_INDEX_KEY && cur < total) {
1350 error("root %llu DIR_INDEX[%llu %llu] should contain only one entry",
1351 root->objectid, di_key->objectid,
1358 btrfs_release_path(path);
1359 ret = btrfs_search_slot(NULL, root, di_key, path, 0, 0);
1361 err |= ret > 0 ? -ENOENT : ret;
1366 * Wrapper function of btrfs_punch_hole.
1368 * Returns 0 means success.
1369 * Returns not 0 means error.
1371 static int punch_extent_hole(struct btrfs_root *root, u64 ino, u64 start,
1374 struct btrfs_trans_handle *trans;
1377 trans = btrfs_start_transaction(root, 1);
1379 return PTR_ERR(trans);
1381 ret = btrfs_punch_hole(trans, root, ino, start, len);
1383 error("failed to add hole [%llu, %llu] in inode [%llu]",
1386 printf("Add a hole [%llu, %llu] in inode [%llu]\n", start, len,
1389 btrfs_commit_transaction(trans, root);
1394 * Check file extent datasum/hole, update the size of the file extents,
1395 * check and update the last offset of the file extent.
1397 * @root: the root of fs/file tree.
1398 * @fkey: the key of the file extent.
1399 * @nodatasum: INODE_NODATASUM feature.
1400 * @size: the sum of all EXTENT_DATA items size for this inode.
1401 * @end: the offset of the last extent.
1403 * Return 0 if no error occurred.
1405 static int check_file_extent(struct btrfs_root *root, struct btrfs_key *fkey,
1406 struct extent_buffer *node, int slot,
1407 unsigned int nodatasum, u64 *size, u64 *end)
1409 struct btrfs_file_extent_item *fi;
1412 u64 extent_num_bytes;
1414 u64 csum_found; /* In byte size, sectorsize aligned */
1415 u64 search_start; /* Logical range start we search for csum */
1416 u64 search_len; /* Logical range len we search for csum */
1417 unsigned int extent_type;
1418 unsigned int is_hole;
1423 fi = btrfs_item_ptr(node, slot, struct btrfs_file_extent_item);
1425 /* Check inline extent */
1426 extent_type = btrfs_file_extent_type(node, fi);
1427 if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1428 struct btrfs_item *e = btrfs_item_nr(slot);
1429 u32 item_inline_len;
1431 item_inline_len = btrfs_file_extent_inline_item_len(node, e);
1432 extent_num_bytes = btrfs_file_extent_inline_len(node, slot, fi);
1433 compressed = btrfs_file_extent_compression(node, fi);
1434 if (extent_num_bytes == 0) {
1436 "root %llu EXTENT_DATA[%llu %llu] has empty inline extent",
1437 root->objectid, fkey->objectid, fkey->offset);
1438 err |= FILE_EXTENT_ERROR;
1440 if (!compressed && extent_num_bytes != item_inline_len) {
1442 "root %llu EXTENT_DATA[%llu %llu] wrong inline size, have: %llu, expected: %u",
1443 root->objectid, fkey->objectid, fkey->offset,
1444 extent_num_bytes, item_inline_len);
1445 err |= FILE_EXTENT_ERROR;
1447 *end += extent_num_bytes;
1448 *size += extent_num_bytes;
1452 /* Check extent type */
1453 if (extent_type != BTRFS_FILE_EXTENT_REG &&
1454 extent_type != BTRFS_FILE_EXTENT_PREALLOC) {
1455 err |= FILE_EXTENT_ERROR;
1456 error("root %llu EXTENT_DATA[%llu %llu] type bad",
1457 root->objectid, fkey->objectid, fkey->offset);
1461 /* Check REG_EXTENT/PREALLOC_EXTENT */
1462 disk_bytenr = btrfs_file_extent_disk_bytenr(node, fi);
1463 disk_num_bytes = btrfs_file_extent_disk_num_bytes(node, fi);
1464 extent_num_bytes = btrfs_file_extent_num_bytes(node, fi);
1465 extent_offset = btrfs_file_extent_offset(node, fi);
1466 compressed = btrfs_file_extent_compression(node, fi);
1467 is_hole = (disk_bytenr == 0) && (disk_num_bytes == 0);
1470 * Check EXTENT_DATA csum
1472 * For plain (uncompressed) extent, we should only check the range
1473 * we're referring to, as it's possible that part of prealloc extent
1474 * has been written, and has csum:
1476 * |<--- Original large preallocated extent A ---->|
1477 * |<- Prealloc File Extent ->|<- Regular Extent ->|
1480 * For compressed extent, we should check the whole range.
1483 search_start = disk_bytenr + extent_offset;
1484 search_len = extent_num_bytes;
1486 search_start = disk_bytenr;
1487 search_len = disk_num_bytes;
1489 ret = count_csum_range(root->fs_info, search_start, search_len, &csum_found);
1490 if (csum_found > 0 && nodatasum) {
1491 err |= ODD_CSUM_ITEM;
1492 error("root %llu EXTENT_DATA[%llu %llu] nodatasum shouldn't have datasum",
1493 root->objectid, fkey->objectid, fkey->offset);
1494 } else if (extent_type == BTRFS_FILE_EXTENT_REG && !nodatasum &&
1495 !is_hole && (ret < 0 || csum_found < search_len)) {
1496 err |= CSUM_ITEM_MISSING;
1497 error("root %llu EXTENT_DATA[%llu %llu] csum missing, have: %llu, expected: %llu",
1498 root->objectid, fkey->objectid, fkey->offset,
1499 csum_found, search_len);
1500 } else if (extent_type == BTRFS_FILE_EXTENT_PREALLOC && csum_found > 0) {
1501 err |= ODD_CSUM_ITEM;
1502 error("root %llu EXTENT_DATA[%llu %llu] prealloc shouldn't have csum, but has: %llu",
1503 root->objectid, fkey->objectid, fkey->offset, csum_found);
1506 /* Check EXTENT_DATA hole */
1507 if (!no_holes && *end != fkey->offset) {
1509 ret = punch_extent_hole(root, fkey->objectid,
1510 *end, fkey->offset - *end);
1511 if (!repair || ret) {
1512 err |= FILE_EXTENT_ERROR;
1514 "root %llu EXTENT_DATA[%llu %llu] gap exists, expected: EXTENT_DATA[%llu %llu]",
1515 root->objectid, fkey->objectid, fkey->offset,
1516 fkey->objectid, *end);
1520 *end += extent_num_bytes;
1522 *size += extent_num_bytes;
1527 static int __count_dir_isize(struct btrfs_root *root, u64 ino, int type,
1530 struct btrfs_key key;
1531 struct btrfs_path path;
1533 struct btrfs_dir_item *di;
1543 key.offset = (u64)-1;
1545 btrfs_init_path(&path);
1546 ret = btrfs_search_slot(NULL, root, &key, &path, 0, 0);
1551 /* if found, go to spacial case */
1556 ret = btrfs_previous_item(root, &path, ino, type);
1564 di = btrfs_item_ptr(path.nodes[0], path.slots[0], struct btrfs_dir_item);
1566 total = btrfs_item_size_nr(path.nodes[0], path.slots[0]);
1568 while (cur < total) {
1569 len = btrfs_dir_name_len(path.nodes[0], di);
1570 if (len > BTRFS_NAME_LEN)
1571 len = BTRFS_NAME_LEN;
1574 len += btrfs_dir_data_len(path.nodes[0], di);
1576 di = (struct btrfs_dir_item *)((char *)di + len);
1582 btrfs_release_path(&path);
1586 static int count_dir_isize(struct btrfs_root *root, u64 ino, u64 *size)
1593 ret = __count_dir_isize(root, ino, BTRFS_DIR_ITEM_KEY, &item_size);
1597 ret = __count_dir_isize(root, ino, BTRFS_DIR_INDEX_KEY, &index_size);
1601 *size = item_size + index_size;
1605 error("failed to count root %llu INODE[%llu] root size",
1606 root->objectid, ino);
1611 * Set inode item nbytes to @nbytes
1613 * Returns 0 on success
1614 * Returns != 0 on error
1616 static int repair_inode_nbytes_lowmem(struct btrfs_root *root,
1617 struct btrfs_path *path,
1618 u64 ino, u64 nbytes)
1620 struct btrfs_trans_handle *trans;
1621 struct btrfs_inode_item *ii;
1622 struct btrfs_key key;
1623 struct btrfs_key research_key;
1627 btrfs_item_key_to_cpu(path->nodes[0], &research_key, path->slots[0]);
1630 key.type = BTRFS_INODE_ITEM_KEY;
1633 trans = btrfs_start_transaction(root, 1);
1634 if (IS_ERR(trans)) {
1635 ret = PTR_ERR(trans);
1640 btrfs_release_path(path);
1641 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
1649 ii = btrfs_item_ptr(path->nodes[0], path->slots[0],
1650 struct btrfs_inode_item);
1651 btrfs_set_inode_nbytes(path->nodes[0], ii, nbytes);
1652 btrfs_mark_buffer_dirty(path->nodes[0]);
1654 btrfs_commit_transaction(trans, root);
1657 error("failed to set nbytes in inode %llu root %llu",
1658 ino, root->root_key.objectid);
1660 printf("Set nbytes in inode item %llu root %llu\n to %llu", ino,
1661 root->root_key.objectid, nbytes);
1664 btrfs_release_path(path);
1665 ret = btrfs_search_slot(NULL, root, &research_key, path, 0, 0);
1672 * Set directory inode isize to @isize.
1674 * Returns 0 on success.
1675 * Returns != 0 on error.
1677 static int repair_dir_isize_lowmem(struct btrfs_root *root,
1678 struct btrfs_path *path,
1681 struct btrfs_trans_handle *trans;
1682 struct btrfs_inode_item *ii;
1683 struct btrfs_key key;
1684 struct btrfs_key research_key;
1688 btrfs_item_key_to_cpu(path->nodes[0], &research_key, path->slots[0]);
1691 key.type = BTRFS_INODE_ITEM_KEY;
1694 trans = btrfs_start_transaction(root, 1);
1695 if (IS_ERR(trans)) {
1696 ret = PTR_ERR(trans);
1701 btrfs_release_path(path);
1702 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
1710 ii = btrfs_item_ptr(path->nodes[0], path->slots[0],
1711 struct btrfs_inode_item);
1712 btrfs_set_inode_size(path->nodes[0], ii, isize);
1713 btrfs_mark_buffer_dirty(path->nodes[0]);
1715 btrfs_commit_transaction(trans, root);
1718 error("failed to set isize in inode %llu root %llu",
1719 ino, root->root_key.objectid);
1721 printf("Set isize in inode %llu root %llu to %llu\n",
1722 ino, root->root_key.objectid, isize);
1724 btrfs_release_path(path);
1725 ret = btrfs_search_slot(NULL, root, &research_key, path, 0, 0);
1732 * Wrapper function for btrfs_add_orphan_item().
1734 * Returns 0 on success.
1735 * Returns != 0 on error.
1737 static int repair_inode_orphan_item_lowmem(struct btrfs_root *root,
1738 struct btrfs_path *path, u64 ino)
1740 struct btrfs_trans_handle *trans;
1741 struct btrfs_key research_key;
1745 btrfs_item_key_to_cpu(path->nodes[0], &research_key, path->slots[0]);
1747 trans = btrfs_start_transaction(root, 1);
1748 if (IS_ERR(trans)) {
1749 ret = PTR_ERR(trans);
1754 btrfs_release_path(path);
1755 ret = btrfs_add_orphan_item(trans, root, path, ino);
1757 btrfs_commit_transaction(trans, root);
1760 error("failed to add inode %llu as orphan item root %llu",
1761 ino, root->root_key.objectid);
1763 printf("Added inode %llu as orphan item root %llu\n",
1764 ino, root->root_key.objectid);
1766 btrfs_release_path(path);
1767 ret = btrfs_search_slot(NULL, root, &research_key, path, 0, 0);
1773 /* Set inode_item nlink to @ref_count.
1774 * If @ref_count == 0, move it to "lost+found" and increase @ref_count.
1776 * Returns 0 on success
1778 static int repair_inode_nlinks_lowmem(struct btrfs_root *root,
1779 struct btrfs_path *path, u64 ino,
1780 const char *name, u32 namelen,
1781 u64 ref_count, u8 filetype, u64 *nlink)
1783 struct btrfs_trans_handle *trans;
1784 struct btrfs_inode_item *ii;
1785 struct btrfs_key key;
1786 struct btrfs_key old_key;
1787 char namebuf[BTRFS_NAME_LEN] = {0};
1793 btrfs_item_key_to_cpu(path->nodes[0], &old_key, path->slots[0]);
1795 if (name && namelen) {
1796 ASSERT(namelen <= BTRFS_NAME_LEN);
1797 memcpy(namebuf, name, namelen);
1800 sprintf(namebuf, "%llu", ino);
1801 name_len = count_digits(ino);
1802 printf("Can't find file name for inode %llu, use %s instead\n",
1806 trans = btrfs_start_transaction(root, 1);
1807 if (IS_ERR(trans)) {
1808 ret = PTR_ERR(trans);
1812 btrfs_release_path(path);
1813 /* if refs is 0, put it into lostfound */
1814 if (ref_count == 0) {
1815 ret = link_inode_to_lostfound(trans, root, path, ino, namebuf,
1816 name_len, filetype, &ref_count);
1821 /* reset inode_item's nlink to ref_count */
1823 key.type = BTRFS_INODE_ITEM_KEY;
1826 btrfs_release_path(path);
1827 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
1833 ii = btrfs_item_ptr(path->nodes[0], path->slots[0],
1834 struct btrfs_inode_item);
1835 btrfs_set_inode_nlink(path->nodes[0], ii, ref_count);
1836 btrfs_mark_buffer_dirty(path->nodes[0]);
1841 btrfs_commit_transaction(trans, root);
1845 "fail to repair nlink of inode %llu root %llu name %s filetype %u",
1846 root->objectid, ino, namebuf, filetype);
1848 printf("Fixed nlink of inode %llu root %llu name %s filetype %u\n",
1849 root->objectid, ino, namebuf, filetype);
1852 btrfs_release_path(path);
1853 ret2 = btrfs_search_slot(NULL, root, &old_key, path, 0, 0);
1860 * Check INODE_ITEM and related ITEMs (the same inode number)
1861 * 1. check link count
1862 * 2. check inode ref/extref
1863 * 3. check dir item/index
1865 * @ext_ref: the EXTENDED_IREF feature
1867 * Return 0 if no error occurred.
1868 * Return >0 for error or hit the traversal is done(by error bitmap)
1870 static int check_inode_item(struct btrfs_root *root, struct btrfs_path *path,
1871 unsigned int ext_ref)
1873 struct extent_buffer *node;
1874 struct btrfs_inode_item *ii;
1875 struct btrfs_key key;
1876 struct btrfs_key last_key;
1885 u64 extent_size = 0;
1887 unsigned int nodatasum;
1891 char namebuf[BTRFS_NAME_LEN] = {0};
1894 node = path->nodes[0];
1895 slot = path->slots[0];
1897 btrfs_item_key_to_cpu(node, &key, slot);
1898 inode_id = key.objectid;
1900 if (inode_id == BTRFS_ORPHAN_OBJECTID) {
1901 ret = btrfs_next_item(root, path);
1907 ii = btrfs_item_ptr(node, slot, struct btrfs_inode_item);
1908 isize = btrfs_inode_size(node, ii);
1909 nbytes = btrfs_inode_nbytes(node, ii);
1910 mode = btrfs_inode_mode(node, ii);
1911 dir = imode_to_type(mode) == BTRFS_FT_DIR;
1912 nlink = btrfs_inode_nlink(node, ii);
1913 nodatasum = btrfs_inode_flags(node, ii) & BTRFS_INODE_NODATASUM;
1916 btrfs_item_key_to_cpu(path->nodes[0], &last_key, path->slots[0]);
1917 ret = btrfs_next_item(root, path);
1919 /* out will fill 'err' rusing current statistics */
1921 } else if (ret > 0) {
1926 node = path->nodes[0];
1927 slot = path->slots[0];
1928 btrfs_item_key_to_cpu(node, &key, slot);
1929 if (key.objectid != inode_id)
1933 case BTRFS_INODE_REF_KEY:
1934 ret = check_inode_ref(root, &key, path, namebuf,
1935 &name_len, &refs, mode);
1938 case BTRFS_INODE_EXTREF_KEY:
1939 if (key.type == BTRFS_INODE_EXTREF_KEY && !ext_ref)
1940 warning("root %llu EXTREF[%llu %llu] isn't supported",
1941 root->objectid, key.objectid,
1943 ret = check_inode_extref(root, &key, node, slot, &refs,
1947 case BTRFS_DIR_ITEM_KEY:
1948 case BTRFS_DIR_INDEX_KEY:
1950 warning("root %llu INODE[%llu] mode %u shouldn't have DIR_INDEX[%llu %llu]",
1951 root->objectid, inode_id,
1952 imode_to_type(mode), key.objectid,
1955 ret = check_dir_item(root, &key, path, &size, ext_ref);
1958 case BTRFS_EXTENT_DATA_KEY:
1960 warning("root %llu DIR INODE[%llu] shouldn't EXTENT_DATA[%llu %llu]",
1961 root->objectid, inode_id, key.objectid,
1964 ret = check_file_extent(root, &key, node, slot,
1965 nodatasum, &extent_size,
1969 case BTRFS_XATTR_ITEM_KEY:
1972 error("ITEM[%llu %u %llu] UNKNOWN TYPE",
1973 key.objectid, key.type, key.offset);
1978 if (err & LAST_ITEM) {
1979 btrfs_release_path(path);
1980 ret = btrfs_search_slot(NULL, root, &last_key, path, 0, 0);
1985 /* verify INODE_ITEM nlink/isize/nbytes */
1987 if (repair && (err & DIR_COUNT_AGAIN)) {
1988 err &= ~DIR_COUNT_AGAIN;
1989 count_dir_isize(root, inode_id, &size);
1992 if ((nlink != 1 || refs != 1) && repair) {
1993 ret = repair_inode_nlinks_lowmem(root, path, inode_id,
1994 namebuf, name_len, refs, imode_to_type(mode),
1999 err |= LINK_COUNT_ERROR;
2000 error("root %llu DIR INODE[%llu] shouldn't have more than one link(%llu)",
2001 root->objectid, inode_id, nlink);
2005 * Just a warning, as dir inode nbytes is just an
2006 * instructive value.
2008 if (!IS_ALIGNED(nbytes, root->fs_info->nodesize)) {
2009 warning("root %llu DIR INODE[%llu] nbytes should be aligned to %u",
2010 root->objectid, inode_id,
2011 root->fs_info->nodesize);
2014 if (isize != size) {
2016 ret = repair_dir_isize_lowmem(root, path,
2018 if (!repair || ret) {
2021 "root %llu DIR INODE [%llu] size %llu not equal to %llu",
2022 root->objectid, inode_id, isize, size);
2026 if (nlink != refs) {
2028 ret = repair_inode_nlinks_lowmem(root, path,
2029 inode_id, namebuf, name_len, refs,
2030 imode_to_type(mode), &nlink);
2031 if (!repair || ret) {
2032 err |= LINK_COUNT_ERROR;
2034 "root %llu INODE[%llu] nlink(%llu) not equal to inode_refs(%llu)",
2035 root->objectid, inode_id, nlink, refs);
2037 } else if (!nlink) {
2039 ret = repair_inode_orphan_item_lowmem(root,
2041 if (!repair || ret) {
2043 error("root %llu INODE[%llu] is orphan item",
2044 root->objectid, inode_id);
2048 if (!nbytes && !no_holes && extent_end < isize) {
2050 ret = punch_extent_hole(root, inode_id,
2051 extent_end, isize - extent_end);
2052 if (!repair || ret) {
2053 err |= NBYTES_ERROR;
2055 "root %llu INODE[%llu] size %llu should have a file extent hole",
2056 root->objectid, inode_id, isize);
2060 if (nbytes != extent_size) {
2062 ret = repair_inode_nbytes_lowmem(root, path,
2063 inode_id, extent_size);
2064 if (!repair || ret) {
2065 err |= NBYTES_ERROR;
2067 "root %llu INODE[%llu] nbytes %llu not equal to extent_size %llu",
2068 root->objectid, inode_id, nbytes,
2074 if (err & LAST_ITEM)
2075 btrfs_next_item(root, path);
2080 * Returns >0 Found error, not fatal, should continue
2081 * Returns <0 Fatal error, must exit the whole check
2082 * Returns 0 No errors found
2084 static int process_one_leaf(struct btrfs_root *root, struct btrfs_path *path,
2085 struct node_refs *nrefs, int *level, int ext_ref)
2087 struct extent_buffer *cur = path->nodes[0];
2088 struct btrfs_key key;
2092 int root_level = btrfs_header_level(root->node);
2094 int ret = 0; /* Final return value */
2095 int err = 0; /* Positive error bitmap */
2097 cur_bytenr = cur->start;
2099 /* skip to first inode item or the first inode number change */
2100 nritems = btrfs_header_nritems(cur);
2101 for (i = 0; i < nritems; i++) {
2102 btrfs_item_key_to_cpu(cur, &key, i);
2104 first_ino = key.objectid;
2105 if (key.type == BTRFS_INODE_ITEM_KEY ||
2106 (first_ino && first_ino != key.objectid))
2110 path->slots[0] = nritems;
2116 err |= check_inode_item(root, path, ext_ref);
2118 /* modify cur since check_inode_item may change path */
2119 cur = path->nodes[0];
2121 if (err & LAST_ITEM)
2124 /* still have inode items in thie leaf */
2125 if (cur->start == cur_bytenr)
2129 * we have switched to another leaf, above nodes may
2130 * have changed, here walk down the path, if a node
2131 * or leaf is shared, check whether we can skip this
2134 for (i = root_level; i >= 0; i--) {
2135 if (path->nodes[i]->start == nrefs->bytenr[i])
2138 ret = update_nodes_refs(root, path->nodes[i]->start,
2139 path->nodes[i], nrefs, i, 0);
2143 if (!nrefs->need_check[i]) {
2149 for (i = 0; i < *level; i++) {
2150 free_extent_buffer(path->nodes[i]);
2151 path->nodes[i] = NULL;
2161 * @level if @level == -1 means extent data item
2162 * else normal treeblocl.
2164 static int should_check_extent_strictly(struct btrfs_root *root,
2165 struct node_refs *nrefs, int level)
2167 int root_level = btrfs_header_level(root->node);
2169 if (level > root_level || level < -1)
2171 if (level == root_level)
2174 * if the upper node is marked full backref, it should contain shared
2175 * backref of the parent (except owner == root->objectid).
2177 while (++level <= root_level)
2178 if (nrefs->refs[level] > 1)
2184 static int check_extent_inline_ref(struct extent_buffer *eb,
2185 struct btrfs_key *key, struct btrfs_extent_inline_ref *iref)
2188 u8 type = btrfs_extent_inline_ref_type(eb, iref);
2191 case BTRFS_TREE_BLOCK_REF_KEY:
2192 case BTRFS_EXTENT_DATA_REF_KEY:
2193 case BTRFS_SHARED_BLOCK_REF_KEY:
2194 case BTRFS_SHARED_DATA_REF_KEY:
2198 error("extent[%llu %u %llu] has unknown ref type: %d",
2199 key->objectid, key->type, key->offset, type);
2208 * Check backrefs of a tree block given by @bytenr or @eb.
2210 * @root: the root containing the @bytenr or @eb
2211 * @eb: tree block extent buffer, can be NULL
2212 * @bytenr: bytenr of the tree block to search
2213 * @level: tree level of the tree block
2214 * @owner: owner of the tree block
2216 * Return >0 for any error found and output error message
2217 * Return 0 for no error found
2219 static int check_tree_block_ref(struct btrfs_root *root,
2220 struct extent_buffer *eb, u64 bytenr,
2221 int level, u64 owner, struct node_refs *nrefs)
2223 struct btrfs_key key;
2224 struct btrfs_root *extent_root = root->fs_info->extent_root;
2225 struct btrfs_path path;
2226 struct btrfs_extent_item *ei;
2227 struct btrfs_extent_inline_ref *iref;
2228 struct extent_buffer *leaf;
2233 int root_level = btrfs_header_level(root->node);
2235 u32 nodesize = root->fs_info->nodesize;
2244 btrfs_init_path(&path);
2245 key.objectid = bytenr;
2246 if (btrfs_fs_incompat(root->fs_info, SKINNY_METADATA))
2247 key.type = BTRFS_METADATA_ITEM_KEY;
2249 key.type = BTRFS_EXTENT_ITEM_KEY;
2250 key.offset = (u64)-1;
2252 /* Search for the backref in extent tree */
2253 ret = btrfs_search_slot(NULL, extent_root, &key, &path, 0, 0);
2255 err |= BACKREF_MISSING;
2258 ret = btrfs_previous_extent_item(extent_root, &path, bytenr);
2260 err |= BACKREF_MISSING;
2264 leaf = path.nodes[0];
2265 slot = path.slots[0];
2266 btrfs_item_key_to_cpu(leaf, &key, slot);
2268 ei = btrfs_item_ptr(leaf, slot, struct btrfs_extent_item);
2270 if (key.type == BTRFS_METADATA_ITEM_KEY) {
2271 skinny_level = (int)key.offset;
2272 iref = (struct btrfs_extent_inline_ref *)(ei + 1);
2274 struct btrfs_tree_block_info *info;
2276 info = (struct btrfs_tree_block_info *)(ei + 1);
2277 skinny_level = btrfs_tree_block_level(leaf, info);
2278 iref = (struct btrfs_extent_inline_ref *)(info + 1);
2287 * Due to the feature of shared tree blocks, if the upper node
2288 * is a fs root or shared node, the extent of checked node may
2289 * not be updated until the next CoW.
2292 strict = should_check_extent_strictly(root, nrefs,
2294 if (!(btrfs_extent_flags(leaf, ei) &
2295 BTRFS_EXTENT_FLAG_TREE_BLOCK)) {
2297 "extent[%llu %u] backref type mismatch, missing bit: %llx",
2298 key.objectid, nodesize,
2299 BTRFS_EXTENT_FLAG_TREE_BLOCK);
2300 err = BACKREF_MISMATCH;
2302 header_gen = btrfs_header_generation(eb);
2303 extent_gen = btrfs_extent_generation(leaf, ei);
2304 if (header_gen != extent_gen) {
2306 "extent[%llu %u] backref generation mismatch, wanted: %llu, have: %llu",
2307 key.objectid, nodesize, header_gen,
2309 err = BACKREF_MISMATCH;
2311 if (level != skinny_level) {
2313 "extent[%llu %u] level mismatch, wanted: %u, have: %u",
2314 key.objectid, nodesize, level, skinny_level);
2315 err = BACKREF_MISMATCH;
2317 if (!is_fstree(owner) && btrfs_extent_refs(leaf, ei) != 1) {
2319 "extent[%llu %u] is referred by other roots than %llu",
2320 key.objectid, nodesize, root->objectid);
2321 err = BACKREF_MISMATCH;
2326 * Iterate the extent/metadata item to find the exact backref
2328 item_size = btrfs_item_size_nr(leaf, slot);
2329 ptr = (unsigned long)iref;
2330 end = (unsigned long)ei + item_size;
2333 iref = (struct btrfs_extent_inline_ref *)ptr;
2334 type = btrfs_extent_inline_ref_type(leaf, iref);
2335 offset = btrfs_extent_inline_ref_offset(leaf, iref);
2337 ret = check_extent_inline_ref(leaf, &key, iref);
2342 if (type == BTRFS_TREE_BLOCK_REF_KEY) {
2343 if (offset == root->objectid)
2345 if (!strict && owner == offset)
2347 } else if (type == BTRFS_SHARED_BLOCK_REF_KEY) {
2349 * Backref of tree reloc root points to itself, no need
2350 * to check backref any more.
2352 * This may be an error of loop backref, but extent tree
2353 * checker should have already handled it.
2354 * Here we only need to avoid infinite iteration.
2356 if (offset == bytenr) {
2360 * Check if the backref points to valid
2363 found_ref = !check_tree_block_ref( root, NULL,
2364 offset, level + 1, owner,
2371 ptr += btrfs_extent_inline_ref_size(type);
2375 * Inlined extent item doesn't have what we need, check
2376 * TREE_BLOCK_REF_KEY
2379 btrfs_release_path(&path);
2380 key.objectid = bytenr;
2381 key.type = BTRFS_TREE_BLOCK_REF_KEY;
2382 key.offset = root->objectid;
2384 ret = btrfs_search_slot(NULL, extent_root, &key, &path, 0, 0);
2389 * Finally check SHARED BLOCK REF, any found will be good
2390 * Here we're not doing comprehensive extent backref checking,
2391 * only need to ensure there is some extent referring to this
2395 btrfs_release_path(&path);
2396 key.objectid = bytenr;
2397 key.type = BTRFS_SHARED_BLOCK_REF_KEY;
2398 key.offset = (u64)-1;
2400 ret = btrfs_search_slot(NULL, extent_root, &key, &path, 0, 0);
2402 err |= BACKREF_MISSING;
2405 ret = btrfs_previous_extent_item(extent_root, &path, bytenr);
2407 err |= BACKREF_MISSING;
2413 err |= BACKREF_MISSING;
2415 btrfs_release_path(&path);
2416 if (nrefs && strict &&
2417 level < root_level && nrefs->full_backref[level + 1])
2418 parent = nrefs->bytenr[level + 1];
2419 if (eb && (err & BACKREF_MISSING))
2421 "extent[%llu %u] backref lost (owner: %llu, level: %u) %s %llu",
2422 bytenr, nodesize, owner, level,
2423 parent ? "parent" : "root",
2424 parent ? parent : root->objectid);
2429 * If @err contains BACKREF_MISSING then add extent of the
2430 * file_extent_data_item.
2432 * Returns error bits after reapir.
2434 static int repair_extent_data_item(struct btrfs_trans_handle *trans,
2435 struct btrfs_root *root,
2436 struct btrfs_path *pathp,
2437 struct node_refs *nrefs,
2440 struct btrfs_file_extent_item *fi;
2441 struct btrfs_key fi_key;
2442 struct btrfs_key key;
2443 struct btrfs_extent_item *ei;
2444 struct btrfs_path path;
2445 struct btrfs_root *extent_root = root->fs_info->extent_root;
2446 struct extent_buffer *eb;
2458 eb = pathp->nodes[0];
2459 slot = pathp->slots[0];
2460 btrfs_item_key_to_cpu(eb, &fi_key, slot);
2461 fi = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item);
2463 if (btrfs_file_extent_type(eb, fi) == BTRFS_FILE_EXTENT_INLINE ||
2464 btrfs_file_extent_disk_bytenr(eb, fi) == 0)
2467 file_offset = fi_key.offset;
2468 generation = btrfs_file_extent_generation(eb, fi);
2469 disk_bytenr = btrfs_file_extent_disk_bytenr(eb, fi);
2470 num_bytes = btrfs_file_extent_disk_num_bytes(eb, fi);
2471 extent_offset = btrfs_file_extent_offset(eb, fi);
2472 offset = file_offset - extent_offset;
2474 /* now repair only adds backref */
2475 if ((err & BACKREF_MISSING) == 0)
2478 /* search extent item */
2479 key.objectid = disk_bytenr;
2480 key.type = BTRFS_EXTENT_ITEM_KEY;
2481 key.offset = num_bytes;
2483 btrfs_init_path(&path);
2484 ret = btrfs_search_slot(NULL, extent_root, &key, &path, 0, 0);
2490 /* insert an extent item */
2492 key.objectid = disk_bytenr;
2493 key.type = BTRFS_EXTENT_ITEM_KEY;
2494 key.offset = num_bytes;
2497 btrfs_release_path(&path);
2498 ret = btrfs_insert_empty_item(trans, extent_root, &path, &key,
2503 ei = btrfs_item_ptr(eb, path.slots[0], struct btrfs_extent_item);
2505 btrfs_set_extent_refs(eb, ei, 0);
2506 btrfs_set_extent_generation(eb, ei, generation);
2507 btrfs_set_extent_flags(eb, ei, BTRFS_EXTENT_FLAG_DATA);
2509 btrfs_mark_buffer_dirty(eb);
2510 ret = btrfs_update_block_group(extent_root, disk_bytenr,
2512 btrfs_release_path(&path);
2515 if (nrefs->full_backref[0])
2516 parent = btrfs_header_bytenr(eb);
2520 ret = btrfs_inc_extent_ref(trans, root, disk_bytenr, num_bytes, parent,
2522 parent ? BTRFS_FIRST_FREE_OBJECTID : fi_key.objectid,
2526 "failed to increase extent data backref[%llu %llu] root %llu",
2527 disk_bytenr, num_bytes, root->objectid);
2530 printf("Add one extent data backref [%llu %llu]\n",
2531 disk_bytenr, num_bytes);
2534 err &= ~BACKREF_MISSING;
2537 error("can't repair root %llu extent data item[%llu %llu]",
2538 root->objectid, disk_bytenr, num_bytes);
2543 * Check EXTENT_DATA item, mainly for its dbackref in extent tree
2545 * Return >0 any error found and output error message
2546 * Return 0 for no error found
2548 static int check_extent_data_item(struct btrfs_root *root,
2549 struct btrfs_path *pathp,
2550 struct node_refs *nrefs, int account_bytes)
2552 struct btrfs_file_extent_item *fi;
2553 struct extent_buffer *eb = pathp->nodes[0];
2554 struct btrfs_path path;
2555 struct btrfs_root *extent_root = root->fs_info->extent_root;
2556 struct btrfs_key fi_key;
2557 struct btrfs_key dbref_key;
2558 struct extent_buffer *leaf;
2559 struct btrfs_extent_item *ei;
2560 struct btrfs_extent_inline_ref *iref;
2561 struct btrfs_extent_data_ref *dref;
2565 u64 extent_num_bytes;
2572 int found_dbackref = 0;
2573 int slot = pathp->slots[0];
2578 btrfs_item_key_to_cpu(eb, &fi_key, slot);
2579 fi = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item);
2581 /* Nothing to check for hole and inline data extents */
2582 if (btrfs_file_extent_type(eb, fi) == BTRFS_FILE_EXTENT_INLINE ||
2583 btrfs_file_extent_disk_bytenr(eb, fi) == 0)
2586 disk_bytenr = btrfs_file_extent_disk_bytenr(eb, fi);
2587 disk_num_bytes = btrfs_file_extent_disk_num_bytes(eb, fi);
2588 extent_num_bytes = btrfs_file_extent_num_bytes(eb, fi);
2589 offset = btrfs_file_extent_offset(eb, fi);
2591 /* Check unaligned disk_num_bytes and num_bytes */
2592 if (!IS_ALIGNED(disk_num_bytes, root->fs_info->sectorsize)) {
2594 "file extent [%llu, %llu] has unaligned disk num bytes: %llu, should be aligned to %u",
2595 fi_key.objectid, fi_key.offset, disk_num_bytes,
2596 root->fs_info->sectorsize);
2597 err |= BYTES_UNALIGNED;
2598 } else if (account_bytes) {
2599 data_bytes_allocated += disk_num_bytes;
2601 if (!IS_ALIGNED(extent_num_bytes, root->fs_info->sectorsize)) {
2603 "file extent [%llu, %llu] has unaligned num bytes: %llu, should be aligned to %u",
2604 fi_key.objectid, fi_key.offset, extent_num_bytes,
2605 root->fs_info->sectorsize);
2606 err |= BYTES_UNALIGNED;
2607 } else if (account_bytes) {
2608 data_bytes_referenced += extent_num_bytes;
2610 owner = btrfs_header_owner(eb);
2612 /* Check the extent item of the file extent in extent tree */
2613 btrfs_init_path(&path);
2614 dbref_key.objectid = btrfs_file_extent_disk_bytenr(eb, fi);
2615 dbref_key.type = BTRFS_EXTENT_ITEM_KEY;
2616 dbref_key.offset = btrfs_file_extent_disk_num_bytes(eb, fi);
2618 ret = btrfs_search_slot(NULL, extent_root, &dbref_key, &path, 0, 0);
2622 leaf = path.nodes[0];
2623 slot = path.slots[0];
2624 ei = btrfs_item_ptr(leaf, slot, struct btrfs_extent_item);
2626 extent_flags = btrfs_extent_flags(leaf, ei);
2628 if (!(extent_flags & BTRFS_EXTENT_FLAG_DATA)) {
2630 "extent[%llu %llu] backref type mismatch, wanted bit: %llx",
2631 disk_bytenr, disk_num_bytes,
2632 BTRFS_EXTENT_FLAG_DATA);
2633 err |= BACKREF_MISMATCH;
2636 /* Check data backref inside that extent item */
2637 item_size = btrfs_item_size_nr(leaf, path.slots[0]);
2638 iref = (struct btrfs_extent_inline_ref *)(ei + 1);
2639 ptr = (unsigned long)iref;
2640 end = (unsigned long)ei + item_size;
2641 strict = should_check_extent_strictly(root, nrefs, -1);
2649 iref = (struct btrfs_extent_inline_ref *)ptr;
2650 type = btrfs_extent_inline_ref_type(leaf, iref);
2651 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
2653 ret = check_extent_inline_ref(leaf, &dbref_key, iref);
2658 if (type == BTRFS_EXTENT_DATA_REF_KEY) {
2659 ref_root = btrfs_extent_data_ref_root(leaf, dref);
2660 ref_objectid = btrfs_extent_data_ref_objectid(leaf, dref);
2661 ref_offset = btrfs_extent_data_ref_offset(leaf, dref);
2663 if (ref_objectid == fi_key.objectid &&
2664 ref_offset == fi_key.offset - offset)
2666 if (ref_root == root->objectid && match)
2668 else if (!strict && owner == ref_root && match)
2670 } else if (type == BTRFS_SHARED_DATA_REF_KEY) {
2671 found_dbackref = !check_tree_block_ref(root, NULL,
2672 btrfs_extent_inline_ref_offset(leaf, iref),
2678 ptr += btrfs_extent_inline_ref_size(type);
2681 if (!found_dbackref) {
2682 btrfs_release_path(&path);
2684 /* Didn't find inlined data backref, try EXTENT_DATA_REF_KEY */
2685 dbref_key.objectid = btrfs_file_extent_disk_bytenr(eb, fi);
2686 dbref_key.type = BTRFS_EXTENT_DATA_REF_KEY;
2687 dbref_key.offset = hash_extent_data_ref(root->objectid,
2688 fi_key.objectid, fi_key.offset - offset);
2690 ret = btrfs_search_slot(NULL, root->fs_info->extent_root,
2691 &dbref_key, &path, 0, 0);
2697 btrfs_release_path(&path);
2700 * Neither inlined nor EXTENT_DATA_REF found, try
2701 * SHARED_DATA_REF as last chance.
2703 dbref_key.objectid = disk_bytenr;
2704 dbref_key.type = BTRFS_SHARED_DATA_REF_KEY;
2705 dbref_key.offset = eb->start;
2707 ret = btrfs_search_slot(NULL, root->fs_info->extent_root,
2708 &dbref_key, &path, 0, 0);
2716 if (!found_dbackref)
2717 err |= BACKREF_MISSING;
2718 btrfs_release_path(&path);
2719 if (err & BACKREF_MISSING) {
2720 error("data extent[%llu %llu] backref lost",
2721 disk_bytenr, disk_num_bytes);
2727 * Check a block group item with its referener (chunk) and its used space
2728 * with extent/metadata item
2730 static int check_block_group_item(struct btrfs_fs_info *fs_info,
2731 struct extent_buffer *eb, int slot)
2733 struct btrfs_root *extent_root = fs_info->extent_root;
2734 struct btrfs_root *chunk_root = fs_info->chunk_root;
2735 struct btrfs_block_group_item *bi;
2736 struct btrfs_block_group_item bg_item;
2737 struct btrfs_path path;
2738 struct btrfs_key bg_key;
2739 struct btrfs_key chunk_key;
2740 struct btrfs_key extent_key;
2741 struct btrfs_chunk *chunk;
2742 struct extent_buffer *leaf;
2743 struct btrfs_extent_item *ei;
2744 u32 nodesize = btrfs_super_nodesize(fs_info->super_copy);
2752 btrfs_item_key_to_cpu(eb, &bg_key, slot);
2753 bi = btrfs_item_ptr(eb, slot, struct btrfs_block_group_item);
2754 read_extent_buffer(eb, &bg_item, (unsigned long)bi, sizeof(bg_item));
2755 used = btrfs_block_group_used(&bg_item);
2756 bg_flags = btrfs_block_group_flags(&bg_item);
2758 chunk_key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
2759 chunk_key.type = BTRFS_CHUNK_ITEM_KEY;
2760 chunk_key.offset = bg_key.objectid;
2762 btrfs_init_path(&path);
2763 /* Search for the referencer chunk */
2764 ret = btrfs_search_slot(NULL, chunk_root, &chunk_key, &path, 0, 0);
2767 "block group[%llu %llu] did not find the related chunk item",
2768 bg_key.objectid, bg_key.offset);
2769 err |= REFERENCER_MISSING;
2771 chunk = btrfs_item_ptr(path.nodes[0], path.slots[0],
2772 struct btrfs_chunk);
2773 if (btrfs_chunk_length(path.nodes[0], chunk) !=
2776 "block group[%llu %llu] related chunk item length does not match",
2777 bg_key.objectid, bg_key.offset);
2778 err |= REFERENCER_MISMATCH;
2781 btrfs_release_path(&path);
2783 /* Search from the block group bytenr */
2784 extent_key.objectid = bg_key.objectid;
2785 extent_key.type = 0;
2786 extent_key.offset = 0;
2788 btrfs_init_path(&path);
2789 ret = btrfs_search_slot(NULL, extent_root, &extent_key, &path, 0, 0);
2793 /* Iterate extent tree to account used space */
2795 leaf = path.nodes[0];
2797 /* Search slot can point to the last item beyond leaf nritems */
2798 if (path.slots[0] >= btrfs_header_nritems(leaf))
2801 btrfs_item_key_to_cpu(leaf, &extent_key, path.slots[0]);
2802 if (extent_key.objectid >= bg_key.objectid + bg_key.offset)
2805 if (extent_key.type != BTRFS_METADATA_ITEM_KEY &&
2806 extent_key.type != BTRFS_EXTENT_ITEM_KEY)
2808 if (extent_key.objectid < bg_key.objectid)
2811 if (extent_key.type == BTRFS_METADATA_ITEM_KEY)
2814 total += extent_key.offset;
2816 ei = btrfs_item_ptr(leaf, path.slots[0],
2817 struct btrfs_extent_item);
2818 flags = btrfs_extent_flags(leaf, ei);
2819 if (flags & BTRFS_EXTENT_FLAG_DATA) {
2820 if (!(bg_flags & BTRFS_BLOCK_GROUP_DATA)) {
2822 "bad extent[%llu, %llu) type mismatch with chunk",
2823 extent_key.objectid,
2824 extent_key.objectid + extent_key.offset);
2825 err |= CHUNK_TYPE_MISMATCH;
2827 } else if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
2828 if (!(bg_flags & (BTRFS_BLOCK_GROUP_SYSTEM |
2829 BTRFS_BLOCK_GROUP_METADATA))) {
2831 "bad extent[%llu, %llu) type mismatch with chunk",
2832 extent_key.objectid,
2833 extent_key.objectid + nodesize);
2834 err |= CHUNK_TYPE_MISMATCH;
2838 ret = btrfs_next_item(extent_root, &path);
2844 btrfs_release_path(&path);
2846 if (total != used) {
2848 "block group[%llu %llu] used %llu but extent items used %llu",
2849 bg_key.objectid, bg_key.offset, used, total);
2850 err |= BG_ACCOUNTING_ERROR;
2856 * Get real tree block level for the case like shared block
2857 * Return >= 0 as tree level
2858 * Return <0 for error
2860 static int query_tree_block_level(struct btrfs_fs_info *fs_info, u64 bytenr)
2862 struct extent_buffer *eb;
2863 struct btrfs_path path;
2864 struct btrfs_key key;
2865 struct btrfs_extent_item *ei;
2872 /* Search extent tree for extent generation and level */
2873 key.objectid = bytenr;
2874 key.type = BTRFS_METADATA_ITEM_KEY;
2875 key.offset = (u64)-1;
2877 btrfs_init_path(&path);
2878 ret = btrfs_search_slot(NULL, fs_info->extent_root, &key, &path, 0, 0);
2881 ret = btrfs_previous_extent_item(fs_info->extent_root, &path, bytenr);
2889 btrfs_item_key_to_cpu(path.nodes[0], &key, path.slots[0]);
2890 ei = btrfs_item_ptr(path.nodes[0], path.slots[0],
2891 struct btrfs_extent_item);
2892 flags = btrfs_extent_flags(path.nodes[0], ei);
2893 if (!(flags & BTRFS_EXTENT_FLAG_TREE_BLOCK)) {
2898 /* Get transid for later read_tree_block() check */
2899 transid = btrfs_extent_generation(path.nodes[0], ei);
2901 /* Get backref level as one source */
2902 if (key.type == BTRFS_METADATA_ITEM_KEY) {
2903 backref_level = key.offset;
2905 struct btrfs_tree_block_info *info;
2907 info = (struct btrfs_tree_block_info *)(ei + 1);
2908 backref_level = btrfs_tree_block_level(path.nodes[0], info);
2910 btrfs_release_path(&path);
2912 /* Get level from tree block as an alternative source */
2913 eb = read_tree_block(fs_info, bytenr, transid);
2914 if (!extent_buffer_uptodate(eb)) {
2915 free_extent_buffer(eb);
2918 header_level = btrfs_header_level(eb);
2919 free_extent_buffer(eb);
2921 if (header_level != backref_level)
2923 return header_level;
2926 btrfs_release_path(&path);
2931 * Check if a tree block backref is valid (points to a valid tree block)
2932 * if level == -1, level will be resolved
2933 * Return >0 for any error found and print error message
2935 static int check_tree_block_backref(struct btrfs_fs_info *fs_info, u64 root_id,
2936 u64 bytenr, int level)
2938 struct btrfs_root *root;
2939 struct btrfs_key key;
2940 struct btrfs_path path;
2941 struct extent_buffer *eb;
2942 struct extent_buffer *node;
2943 u32 nodesize = btrfs_super_nodesize(fs_info->super_copy);
2947 /* Query level for level == -1 special case */
2949 level = query_tree_block_level(fs_info, bytenr);
2951 err |= REFERENCER_MISSING;
2955 key.objectid = root_id;
2956 key.type = BTRFS_ROOT_ITEM_KEY;
2957 key.offset = (u64)-1;
2959 root = btrfs_read_fs_root(fs_info, &key);
2961 err |= REFERENCER_MISSING;
2965 /* Read out the tree block to get item/node key */
2966 eb = read_tree_block(fs_info, bytenr, 0);
2967 if (!extent_buffer_uptodate(eb)) {
2968 err |= REFERENCER_MISSING;
2969 free_extent_buffer(eb);
2973 /* Empty tree, no need to check key */
2974 if (!btrfs_header_nritems(eb) && !level) {
2975 free_extent_buffer(eb);
2980 btrfs_node_key_to_cpu(eb, &key, 0);
2982 btrfs_item_key_to_cpu(eb, &key, 0);
2984 free_extent_buffer(eb);
2986 btrfs_init_path(&path);
2987 path.lowest_level = level;
2988 /* Search with the first key, to ensure we can reach it */
2989 ret = btrfs_search_slot(NULL, root, &key, &path, 0, 0);
2991 err |= REFERENCER_MISSING;
2995 node = path.nodes[level];
2996 if (btrfs_header_bytenr(node) != bytenr) {
2998 "extent [%llu %d] referencer bytenr mismatch, wanted: %llu, have: %llu",
2999 bytenr, nodesize, bytenr,
3000 btrfs_header_bytenr(node));
3001 err |= REFERENCER_MISMATCH;
3003 if (btrfs_header_level(node) != level) {
3005 "extent [%llu %d] referencer level mismatch, wanted: %d, have: %d",
3006 bytenr, nodesize, level,
3007 btrfs_header_level(node));
3008 err |= REFERENCER_MISMATCH;
3012 btrfs_release_path(&path);
3014 if (err & REFERENCER_MISSING) {
3016 error("extent [%llu %d] lost referencer (owner: %llu)",
3017 bytenr, nodesize, root_id);
3020 "extent [%llu %d] lost referencer (owner: %llu, level: %u)",
3021 bytenr, nodesize, root_id, level);
3028 * Check if tree block @eb is tree reloc root.
3029 * Return 0 if it's not or any problem happens
3030 * Return 1 if it's a tree reloc root
3032 static int is_tree_reloc_root(struct btrfs_fs_info *fs_info,
3033 struct extent_buffer *eb)
3035 struct btrfs_root *tree_reloc_root;
3036 struct btrfs_key key;
3037 u64 bytenr = btrfs_header_bytenr(eb);
3038 u64 owner = btrfs_header_owner(eb);
3041 key.objectid = BTRFS_TREE_RELOC_OBJECTID;
3043 key.type = BTRFS_ROOT_ITEM_KEY;
3045 tree_reloc_root = btrfs_read_fs_root_no_cache(fs_info, &key);
3046 if (IS_ERR(tree_reloc_root))
3049 if (bytenr == btrfs_header_bytenr(tree_reloc_root->node))
3051 btrfs_free_fs_root(tree_reloc_root);
3056 * Check referencer for shared block backref
3057 * If level == -1, this function will resolve the level.
3059 static int check_shared_block_backref(struct btrfs_fs_info *fs_info,
3060 u64 parent, u64 bytenr, int level)
3062 struct extent_buffer *eb;
3064 int found_parent = 0;
3067 eb = read_tree_block(fs_info, parent, 0);
3068 if (!extent_buffer_uptodate(eb))
3072 level = query_tree_block_level(fs_info, bytenr);
3076 /* It's possible it's a tree reloc root */
3077 if (parent == bytenr) {
3078 if (is_tree_reloc_root(fs_info, eb))
3083 if (level + 1 != btrfs_header_level(eb))
3086 nr = btrfs_header_nritems(eb);
3087 for (i = 0; i < nr; i++) {
3088 if (bytenr == btrfs_node_blockptr(eb, i)) {
3094 free_extent_buffer(eb);
3095 if (!found_parent) {
3097 "shared extent[%llu %u] lost its parent (parent: %llu, level: %u)",
3098 bytenr, fs_info->nodesize, parent, level);
3099 return REFERENCER_MISSING;
3105 * Check referencer for normal (inlined) data ref
3106 * If len == 0, it will be resolved by searching in extent tree
3108 static int check_extent_data_backref(struct btrfs_fs_info *fs_info,
3109 u64 root_id, u64 objectid, u64 offset,
3110 u64 bytenr, u64 len, u32 count)
3112 struct btrfs_root *root;
3113 struct btrfs_root *extent_root = fs_info->extent_root;
3114 struct btrfs_key key;
3115 struct btrfs_path path;
3116 struct extent_buffer *leaf;
3117 struct btrfs_file_extent_item *fi;
3118 u32 found_count = 0;
3123 key.objectid = bytenr;
3124 key.type = BTRFS_EXTENT_ITEM_KEY;
3125 key.offset = (u64)-1;
3127 btrfs_init_path(&path);
3128 ret = btrfs_search_slot(NULL, extent_root, &key, &path, 0, 0);
3131 ret = btrfs_previous_extent_item(extent_root, &path, bytenr);
3134 btrfs_item_key_to_cpu(path.nodes[0], &key, path.slots[0]);
3135 if (key.objectid != bytenr ||
3136 key.type != BTRFS_EXTENT_ITEM_KEY)
3139 btrfs_release_path(&path);
3141 key.objectid = root_id;
3142 key.type = BTRFS_ROOT_ITEM_KEY;
3143 key.offset = (u64)-1;
3144 btrfs_init_path(&path);
3146 root = btrfs_read_fs_root(fs_info, &key);
3150 key.objectid = objectid;
3151 key.type = BTRFS_EXTENT_DATA_KEY;
3153 * It can be nasty as data backref offset is
3154 * file offset - file extent offset, which is smaller or
3155 * equal to original backref offset. The only special case is
3156 * overflow. So we need to special check and do further search.
3158 key.offset = offset & (1ULL << 63) ? 0 : offset;
3160 ret = btrfs_search_slot(NULL, root, &key, &path, 0, 0);
3165 * Search afterwards to get correct one
3166 * NOTE: As we must do a comprehensive check on the data backref to
3167 * make sure the dref count also matches, we must iterate all file
3168 * extents for that inode.
3171 leaf = path.nodes[0];
3172 slot = path.slots[0];
3174 if (slot >= btrfs_header_nritems(leaf) ||
3175 btrfs_header_owner(leaf) != root_id)
3177 btrfs_item_key_to_cpu(leaf, &key, slot);
3178 if (key.objectid != objectid || key.type != BTRFS_EXTENT_DATA_KEY)
3180 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
3182 * Except normal disk bytenr and disk num bytes, we still
3183 * need to do extra check on dbackref offset as
3184 * dbackref offset = file_offset - file_extent_offset
3186 * Also, we must check the leaf owner.
3187 * In case of shared tree blocks (snapshots) we can inherit
3188 * leaves from source snapshot.
3189 * In that case, reference from source snapshot should not
3192 if (btrfs_file_extent_disk_bytenr(leaf, fi) == bytenr &&
3193 btrfs_file_extent_disk_num_bytes(leaf, fi) == len &&
3194 (u64)(key.offset - btrfs_file_extent_offset(leaf, fi)) ==
3195 offset && btrfs_header_owner(leaf) == root_id)
3199 ret = btrfs_next_item(root, &path);
3204 btrfs_release_path(&path);
3205 if (found_count != count) {
3207 "extent[%llu, %llu] referencer count mismatch (root: %llu, owner: %llu, offset: %llu) wanted: %u, have: %u",
3208 bytenr, len, root_id, objectid, offset, count, found_count);
3209 return REFERENCER_MISSING;
3215 * Check if the referencer of a shared data backref exists
3217 static int check_shared_data_backref(struct btrfs_fs_info *fs_info,
3218 u64 parent, u64 bytenr)
3220 struct extent_buffer *eb;
3221 struct btrfs_key key;
3222 struct btrfs_file_extent_item *fi;
3224 int found_parent = 0;
3227 eb = read_tree_block(fs_info, parent, 0);
3228 if (!extent_buffer_uptodate(eb))
3231 nr = btrfs_header_nritems(eb);
3232 for (i = 0; i < nr; i++) {
3233 btrfs_item_key_to_cpu(eb, &key, i);
3234 if (key.type != BTRFS_EXTENT_DATA_KEY)
3237 fi = btrfs_item_ptr(eb, i, struct btrfs_file_extent_item);
3238 if (btrfs_file_extent_type(eb, fi) == BTRFS_FILE_EXTENT_INLINE)
3241 if (btrfs_file_extent_disk_bytenr(eb, fi) == bytenr) {
3248 free_extent_buffer(eb);
3249 if (!found_parent) {
3250 error("shared extent %llu referencer lost (parent: %llu)",
3252 return REFERENCER_MISSING;
3258 * Only delete backref if REFERENCER_MISSING now
3260 * Returns <0 the extent was deleted
3261 * Returns >0 the backref was deleted but extent still exists, returned value
3262 * means error after repair
3263 * Returns 0 nothing happened
3265 static int repair_extent_item(struct btrfs_trans_handle *trans,
3266 struct btrfs_root *root, struct btrfs_path *path,
3267 u64 bytenr, u64 num_bytes, u64 parent, u64 root_objectid,
3268 u64 owner, u64 offset, int err)
3270 struct btrfs_key old_key;
3274 btrfs_item_key_to_cpu(path->nodes[0], &old_key, path->slots[0]);
3276 if (err & (REFERENCER_MISSING | REFERENCER_MISMATCH)) {
3277 /* delete the backref */
3278 ret = btrfs_free_extent(trans, root->fs_info->fs_root, bytenr,
3279 num_bytes, parent, root_objectid, owner, offset);
3282 err &= ~REFERENCER_MISSING;
3283 printf("Delete backref in extent [%llu %llu]\n",
3286 error("fail to delete backref in extent [%llu %llu]",
3291 /* btrfs_free_extent may delete the extent */
3292 btrfs_release_path(path);
3293 ret = btrfs_search_slot(NULL, root, &old_key, path, 0, 0);
3303 * This function will check a given extent item, including its backref and
3304 * itself (like crossing stripe boundary and type)
3306 * Since we don't use extent_record anymore, introduce new error bit
3308 static int check_extent_item(struct btrfs_trans_handle *trans,
3309 struct btrfs_fs_info *fs_info,
3310 struct btrfs_path *path)
3312 struct btrfs_extent_item *ei;
3313 struct btrfs_extent_inline_ref *iref;
3314 struct btrfs_extent_data_ref *dref;
3315 struct extent_buffer *eb = path->nodes[0];
3318 int slot = path->slots[0];
3320 u32 nodesize = btrfs_super_nodesize(fs_info->super_copy);
3321 u32 item_size = btrfs_item_size_nr(eb, slot);
3331 struct btrfs_key key;
3335 btrfs_item_key_to_cpu(eb, &key, slot);
3336 if (key.type == BTRFS_EXTENT_ITEM_KEY) {
3337 bytes_used += key.offset;
3338 num_bytes = key.offset;
3340 bytes_used += nodesize;
3341 num_bytes = nodesize;
3344 if (item_size < sizeof(*ei)) {
3346 * COMPAT_EXTENT_TREE_V0 case, but it's already a super
3347 * old thing when on disk format is still un-determined.
3348 * No need to care about it anymore
3350 error("unsupported COMPAT_EXTENT_TREE_V0 detected");
3354 ei = btrfs_item_ptr(eb, slot, struct btrfs_extent_item);
3355 flags = btrfs_extent_flags(eb, ei);
3357 if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK)
3359 if (metadata && check_crossing_stripes(global_info, key.objectid,
3361 error("bad metadata [%llu, %llu) crossing stripe boundary",
3362 key.objectid, key.objectid + nodesize);
3363 err |= CROSSING_STRIPE_BOUNDARY;
3366 ptr = (unsigned long)(ei + 1);
3368 if (metadata && key.type == BTRFS_EXTENT_ITEM_KEY) {
3369 /* Old EXTENT_ITEM metadata */
3370 struct btrfs_tree_block_info *info;
3372 info = (struct btrfs_tree_block_info *)ptr;
3373 level = btrfs_tree_block_level(eb, info);
3374 ptr += sizeof(struct btrfs_tree_block_info);
3376 /* New METADATA_ITEM */
3379 end = (unsigned long)ei + item_size;
3382 /* Reached extent item end normally */
3386 /* Beyond extent item end, wrong item size */
3388 err |= ITEM_SIZE_MISMATCH;
3389 error("extent item at bytenr %llu slot %d has wrong size",
3398 /* Now check every backref in this extent item */
3399 iref = (struct btrfs_extent_inline_ref *)ptr;
3400 type = btrfs_extent_inline_ref_type(eb, iref);
3401 offset = btrfs_extent_inline_ref_offset(eb, iref);
3403 case BTRFS_TREE_BLOCK_REF_KEY:
3404 root_objectid = offset;
3406 ret = check_tree_block_backref(fs_info, offset, key.objectid,
3410 case BTRFS_SHARED_BLOCK_REF_KEY:
3412 ret = check_shared_block_backref(fs_info, offset, key.objectid,
3416 case BTRFS_EXTENT_DATA_REF_KEY:
3417 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
3418 root_objectid = btrfs_extent_data_ref_root(eb, dref);
3419 owner = btrfs_extent_data_ref_objectid(eb, dref);
3420 owner_offset = btrfs_extent_data_ref_offset(eb, dref);
3421 ret = check_extent_data_backref(fs_info, root_objectid, owner,
3422 owner_offset, key.objectid, key.offset,
3423 btrfs_extent_data_ref_count(eb, dref));
3426 case BTRFS_SHARED_DATA_REF_KEY:
3428 ret = check_shared_data_backref(fs_info, offset, key.objectid);
3432 error("extent[%llu %d %llu] has unknown ref type: %d",
3433 key.objectid, key.type, key.offset, type);
3439 if (err && repair) {
3440 ret = repair_extent_item(trans, fs_info->extent_root, path,
3441 key.objectid, num_bytes, parent, root_objectid,
3442 owner, owner_offset, ret);
3451 ptr += btrfs_extent_inline_ref_size(type);
3459 * Check if a dev extent item is referred correctly by its chunk
3461 static int check_dev_extent_item(struct btrfs_fs_info *fs_info,
3462 struct extent_buffer *eb, int slot)
3464 struct btrfs_root *chunk_root = fs_info->chunk_root;
3465 struct btrfs_dev_extent *ptr;
3466 struct btrfs_path path;
3467 struct btrfs_key chunk_key;
3468 struct btrfs_key devext_key;
3469 struct btrfs_chunk *chunk;
3470 struct extent_buffer *l;
3474 int found_chunk = 0;
3477 btrfs_item_key_to_cpu(eb, &devext_key, slot);
3478 ptr = btrfs_item_ptr(eb, slot, struct btrfs_dev_extent);
3479 length = btrfs_dev_extent_length(eb, ptr);
3481 chunk_key.objectid = btrfs_dev_extent_chunk_objectid(eb, ptr);
3482 chunk_key.type = BTRFS_CHUNK_ITEM_KEY;
3483 chunk_key.offset = btrfs_dev_extent_chunk_offset(eb, ptr);
3485 btrfs_init_path(&path);
3486 ret = btrfs_search_slot(NULL, chunk_root, &chunk_key, &path, 0, 0);
3491 chunk = btrfs_item_ptr(l, path.slots[0], struct btrfs_chunk);
3492 ret = btrfs_check_chunk_valid(fs_info, l, chunk, path.slots[0],
3497 if (btrfs_stripe_length(fs_info, l, chunk) != length)
3500 num_stripes = btrfs_chunk_num_stripes(l, chunk);
3501 for (i = 0; i < num_stripes; i++) {
3502 u64 devid = btrfs_stripe_devid_nr(l, chunk, i);
3503 u64 offset = btrfs_stripe_offset_nr(l, chunk, i);
3505 if (devid == devext_key.objectid &&
3506 offset == devext_key.offset) {
3512 btrfs_release_path(&path);
3515 "device extent[%llu, %llu, %llu] did not find the related chunk",
3516 devext_key.objectid, devext_key.offset, length);
3517 return REFERENCER_MISSING;
3523 * Check if the used space is correct with the dev item
3525 static int check_dev_item(struct btrfs_fs_info *fs_info,
3526 struct extent_buffer *eb, int slot)
3528 struct btrfs_root *dev_root = fs_info->dev_root;
3529 struct btrfs_dev_item *dev_item;
3530 struct btrfs_path path;
3531 struct btrfs_key key;
3532 struct btrfs_dev_extent *ptr;
3539 dev_item = btrfs_item_ptr(eb, slot, struct btrfs_dev_item);
3540 dev_id = btrfs_device_id(eb, dev_item);
3541 used = btrfs_device_bytes_used(eb, dev_item);
3542 total_bytes = btrfs_device_total_bytes(eb, dev_item);
3544 key.objectid = dev_id;
3545 key.type = BTRFS_DEV_EXTENT_KEY;
3548 btrfs_init_path(&path);
3549 ret = btrfs_search_slot(NULL, dev_root, &key, &path, 0, 0);
3551 btrfs_item_key_to_cpu(eb, &key, slot);
3552 error("cannot find any related dev extent for dev[%llu, %u, %llu]",
3553 key.objectid, key.type, key.offset);
3554 btrfs_release_path(&path);
3555 return REFERENCER_MISSING;
3558 /* Iterate dev_extents to calculate the used space of a device */
3560 if (path.slots[0] >= btrfs_header_nritems(path.nodes[0]))
3563 btrfs_item_key_to_cpu(path.nodes[0], &key, path.slots[0]);
3564 if (key.objectid > dev_id)
3566 if (key.type != BTRFS_DEV_EXTENT_KEY || key.objectid != dev_id)
3569 ptr = btrfs_item_ptr(path.nodes[0], path.slots[0],
3570 struct btrfs_dev_extent);
3571 total += btrfs_dev_extent_length(path.nodes[0], ptr);
3573 ret = btrfs_next_item(dev_root, &path);
3577 btrfs_release_path(&path);
3579 if (used != total) {
3580 btrfs_item_key_to_cpu(eb, &key, slot);
3582 "Dev extent's total-byte %llu is not equal to bytes-used %llu in dev[%llu, %u, %llu]",
3583 total, used, BTRFS_ROOT_TREE_OBJECTID,
3584 BTRFS_DEV_EXTENT_KEY, dev_id);
3585 return ACCOUNTING_MISMATCH;
3587 check_dev_size_alignment(dev_id, total_bytes, fs_info->sectorsize);
3593 * Check a chunk item.
3594 * Including checking all referred dev_extents and block group
3596 static int check_chunk_item(struct btrfs_fs_info *fs_info,
3597 struct extent_buffer *eb, int slot)
3599 struct btrfs_root *extent_root = fs_info->extent_root;
3600 struct btrfs_root *dev_root = fs_info->dev_root;
3601 struct btrfs_path path;
3602 struct btrfs_key chunk_key;
3603 struct btrfs_key bg_key;
3604 struct btrfs_key devext_key;
3605 struct btrfs_chunk *chunk;
3606 struct extent_buffer *leaf;
3607 struct btrfs_block_group_item *bi;
3608 struct btrfs_block_group_item bg_item;
3609 struct btrfs_dev_extent *ptr;
3621 btrfs_item_key_to_cpu(eb, &chunk_key, slot);
3622 chunk = btrfs_item_ptr(eb, slot, struct btrfs_chunk);
3623 length = btrfs_chunk_length(eb, chunk);
3624 chunk_end = chunk_key.offset + length;
3625 ret = btrfs_check_chunk_valid(fs_info, eb, chunk, slot,
3628 error("chunk[%llu %llu) is invalid", chunk_key.offset,
3630 err |= BYTES_UNALIGNED | UNKNOWN_TYPE;
3633 type = btrfs_chunk_type(eb, chunk);
3635 bg_key.objectid = chunk_key.offset;
3636 bg_key.type = BTRFS_BLOCK_GROUP_ITEM_KEY;
3637 bg_key.offset = length;
3639 btrfs_init_path(&path);
3640 ret = btrfs_search_slot(NULL, extent_root, &bg_key, &path, 0, 0);
3643 "chunk[%llu %llu) did not find the related block group item",
3644 chunk_key.offset, chunk_end);
3645 err |= REFERENCER_MISSING;
3647 leaf = path.nodes[0];
3648 bi = btrfs_item_ptr(leaf, path.slots[0],
3649 struct btrfs_block_group_item);
3650 read_extent_buffer(leaf, &bg_item, (unsigned long)bi,
3652 if (btrfs_block_group_flags(&bg_item) != type) {
3654 "chunk[%llu %llu) related block group item flags mismatch, wanted: %llu, have: %llu",
3655 chunk_key.offset, chunk_end, type,
3656 btrfs_block_group_flags(&bg_item));
3657 err |= REFERENCER_MISSING;
3661 num_stripes = btrfs_chunk_num_stripes(eb, chunk);
3662 stripe_len = btrfs_stripe_length(fs_info, eb, chunk);
3663 for (i = 0; i < num_stripes; i++) {
3664 btrfs_release_path(&path);
3665 btrfs_init_path(&path);
3666 devext_key.objectid = btrfs_stripe_devid_nr(eb, chunk, i);
3667 devext_key.type = BTRFS_DEV_EXTENT_KEY;
3668 devext_key.offset = btrfs_stripe_offset_nr(eb, chunk, i);
3670 ret = btrfs_search_slot(NULL, dev_root, &devext_key, &path,
3675 leaf = path.nodes[0];
3676 ptr = btrfs_item_ptr(leaf, path.slots[0],
3677 struct btrfs_dev_extent);
3678 objectid = btrfs_dev_extent_chunk_objectid(leaf, ptr);
3679 offset = btrfs_dev_extent_chunk_offset(leaf, ptr);
3680 if (objectid != chunk_key.objectid ||
3681 offset != chunk_key.offset ||
3682 btrfs_dev_extent_length(leaf, ptr) != stripe_len)
3686 err |= BACKREF_MISSING;
3688 "chunk[%llu %llu) stripe %d did not find the related dev extent",
3689 chunk_key.objectid, chunk_end, i);
3692 btrfs_release_path(&path);
3698 * Add block group item to the extent tree if @err contains REFERENCER_MISSING.
3699 * FIXME: We still need to repair error of dev_item.
3701 * Returns error after repair.
3703 static int repair_chunk_item(struct btrfs_trans_handle *trans,
3704 struct btrfs_root *chunk_root,
3705 struct btrfs_path *path, int err)
3707 struct btrfs_chunk *chunk;
3708 struct btrfs_key chunk_key;
3709 struct extent_buffer *eb = path->nodes[0];
3711 int slot = path->slots[0];
3715 btrfs_item_key_to_cpu(eb, &chunk_key, slot);
3716 if (chunk_key.type != BTRFS_CHUNK_ITEM_KEY)
3718 chunk = btrfs_item_ptr(eb, slot, struct btrfs_chunk);
3719 type = btrfs_chunk_type(path->nodes[0], chunk);
3720 length = btrfs_chunk_length(eb, chunk);
3722 if (err & REFERENCER_MISSING) {
3723 ret = btrfs_make_block_group(trans, chunk_root->fs_info, 0,
3724 type, chunk_key.offset, length);
3726 error("fail to add block group item[%llu %llu]",
3727 chunk_key.offset, length);
3730 err &= ~REFERENCER_MISSING;
3731 printf("Added block group item[%llu %llu]\n",
3732 chunk_key.offset, length);
3740 static int delete_extent_tree_item(struct btrfs_trans_handle *trans,
3741 struct btrfs_root *root,
3742 struct btrfs_path *path)
3744 struct btrfs_key key;
3747 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
3748 btrfs_release_path(path);
3749 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
3755 ret = btrfs_del_item(trans, root, path);
3759 if (path->slots[0] == 0)
3760 btrfs_prev_leaf(root, path);
3765 error("failed to delete root %llu item[%llu, %u, %llu]",
3766 root->objectid, key.objectid, key.type, key.offset);
3768 printf("Deleted root %llu item[%llu, %u, %llu]\n",
3769 root->objectid, key.objectid, key.type, key.offset);
3774 * Main entry function to check known items and update related accounting info
3776 static int check_leaf_items(struct btrfs_trans_handle *trans,
3777 struct btrfs_root *root, struct btrfs_path *path,
3778 struct node_refs *nrefs, int account_bytes)
3780 struct btrfs_fs_info *fs_info = root->fs_info;
3781 struct btrfs_key key;
3782 struct extent_buffer *eb;
3785 struct btrfs_extent_data_ref *dref;
3790 eb = path->nodes[0];
3791 slot = path->slots[0];
3792 if (slot >= btrfs_header_nritems(eb)) {
3794 error("empty leaf [%llu %u] root %llu", eb->start,
3795 root->fs_info->nodesize, root->objectid);
3801 btrfs_item_key_to_cpu(eb, &key, slot);
3805 case BTRFS_EXTENT_DATA_KEY:
3806 ret = check_extent_data_item(root, path, nrefs, account_bytes);
3808 ret = repair_extent_data_item(trans, root, path, nrefs,
3812 case BTRFS_BLOCK_GROUP_ITEM_KEY:
3813 ret = check_block_group_item(fs_info, eb, slot);
3815 ret & REFERENCER_MISSING)
3816 ret = delete_extent_tree_item(trans, root, path);
3819 case BTRFS_DEV_ITEM_KEY:
3820 ret = check_dev_item(fs_info, eb, slot);
3823 case BTRFS_CHUNK_ITEM_KEY:
3824 ret = check_chunk_item(fs_info, eb, slot);
3826 ret = repair_chunk_item(trans, root, path, ret);
3829 case BTRFS_DEV_EXTENT_KEY:
3830 ret = check_dev_extent_item(fs_info, eb, slot);
3833 case BTRFS_EXTENT_ITEM_KEY:
3834 case BTRFS_METADATA_ITEM_KEY:
3835 ret = check_extent_item(trans, fs_info, path);
3838 case BTRFS_EXTENT_CSUM_KEY:
3839 total_csum_bytes += btrfs_item_size_nr(eb, slot);
3842 case BTRFS_TREE_BLOCK_REF_KEY:
3843 ret = check_tree_block_backref(fs_info, key.offset,
3846 ret & (REFERENCER_MISMATCH | REFERENCER_MISSING))
3847 ret = delete_extent_tree_item(trans, root, path);
3850 case BTRFS_EXTENT_DATA_REF_KEY:
3851 dref = btrfs_item_ptr(eb, slot, struct btrfs_extent_data_ref);
3852 ret = check_extent_data_backref(fs_info,
3853 btrfs_extent_data_ref_root(eb, dref),
3854 btrfs_extent_data_ref_objectid(eb, dref),
3855 btrfs_extent_data_ref_offset(eb, dref),
3857 btrfs_extent_data_ref_count(eb, dref));
3859 ret & (REFERENCER_MISMATCH | REFERENCER_MISSING))
3860 ret = delete_extent_tree_item(trans, root, path);
3863 case BTRFS_SHARED_BLOCK_REF_KEY:
3864 ret = check_shared_block_backref(fs_info, key.offset,
3867 ret & (REFERENCER_MISMATCH | REFERENCER_MISSING))
3868 ret = delete_extent_tree_item(trans, root, path);
3871 case BTRFS_SHARED_DATA_REF_KEY:
3872 ret = check_shared_data_backref(fs_info, key.offset,
3875 ret & (REFERENCER_MISMATCH | REFERENCER_MISSING))
3876 ret = delete_extent_tree_item(trans, root, path);
3890 * @trans just for lowmem repair mode
3891 * @check all if not 0 then check all tree block backrefs and items
3892 * 0 then just check relationship of items in fs tree(s)
3894 * Returns >0 Found error, should continue
3895 * Returns <0 Fatal error, must exit the whole check
3896 * Returns 0 No errors found
3898 static int walk_down_tree(struct btrfs_trans_handle *trans,
3899 struct btrfs_root *root, struct btrfs_path *path,
3900 int *level, struct node_refs *nrefs, int ext_ref,
3903 enum btrfs_tree_block_status status;
3906 struct btrfs_fs_info *fs_info = root->fs_info;
3907 struct extent_buffer *next;
3908 struct extent_buffer *cur;
3912 int account_file_data = 0;
3914 WARN_ON(*level < 0);
3915 WARN_ON(*level >= BTRFS_MAX_LEVEL);
3917 ret = update_nodes_refs(root, btrfs_header_bytenr(path->nodes[*level]),
3918 path->nodes[*level], nrefs, *level, check_all);
3922 while (*level >= 0) {
3923 WARN_ON(*level < 0);
3924 WARN_ON(*level >= BTRFS_MAX_LEVEL);
3925 cur = path->nodes[*level];
3926 bytenr = btrfs_header_bytenr(cur);
3927 check = nrefs->need_check[*level];
3929 if (btrfs_header_level(cur) != *level)
3932 * Update bytes accounting and check tree block ref
3933 * NOTE: Doing accounting and check before checking nritems
3934 * is necessary because of empty node/leaf.
3936 if ((check_all && !nrefs->checked[*level]) ||
3937 (!check_all && nrefs->need_check[*level])) {
3938 ret = check_tree_block_ref(root, cur,
3939 btrfs_header_bytenr(cur), btrfs_header_level(cur),
3940 btrfs_header_owner(cur), nrefs);
3943 ret = repair_tree_block_ref(trans, root,
3944 path->nodes[*level], nrefs, *level, ret);
3947 if (check_all && nrefs->need_check[*level] &&
3948 nrefs->refs[*level]) {
3949 account_bytes(root, path, *level);
3950 account_file_data = 1;
3952 nrefs->checked[*level] = 1;
3955 if (path->slots[*level] >= btrfs_header_nritems(cur))
3958 /* Don't forgot to check leaf/node validation */
3960 /* skip duplicate check */
3961 if (check || !check_all) {
3962 ret = btrfs_check_leaf(root, NULL, cur);
3963 if (ret != BTRFS_TREE_BLOCK_CLEAN) {
3971 ret = process_one_leaf(root, path, nrefs,
3974 ret = check_leaf_items(trans, root, path,
3975 nrefs, account_file_data);
3979 if (check || !check_all) {
3980 ret = btrfs_check_node(root, NULL, cur);
3981 if (ret != BTRFS_TREE_BLOCK_CLEAN) {
3988 bytenr = btrfs_node_blockptr(cur, path->slots[*level]);
3989 ptr_gen = btrfs_node_ptr_generation(cur, path->slots[*level]);
3991 ret = update_nodes_refs(root, bytenr, NULL, nrefs, *level - 1,
3996 * check all trees in check_chunks_and_extent
3997 * check shared node once in check_fs_roots
3999 if (!check_all && !nrefs->need_check[*level - 1]) {
4000 path->slots[*level]++;
4004 next = btrfs_find_tree_block(fs_info, bytenr, fs_info->nodesize);
4005 if (!next || !btrfs_buffer_uptodate(next, ptr_gen)) {
4006 free_extent_buffer(next);
4007 reada_walk_down(root, cur, path->slots[*level]);
4008 next = read_tree_block(fs_info, bytenr, ptr_gen);
4009 if (!extent_buffer_uptodate(next)) {
4010 struct btrfs_key node_key;
4012 btrfs_node_key_to_cpu(path->nodes[*level],
4014 path->slots[*level]);
4015 btrfs_add_corrupt_extent_record(fs_info,
4016 &node_key, path->nodes[*level]->start,
4017 fs_info->nodesize, *level);
4023 ret = check_child_node(cur, path->slots[*level], next);
4028 if (btrfs_is_leaf(next))
4029 status = btrfs_check_leaf(root, NULL, next);
4031 status = btrfs_check_node(root, NULL, next);
4032 if (status != BTRFS_TREE_BLOCK_CLEAN) {
4033 free_extent_buffer(next);
4038 *level = *level - 1;
4039 free_extent_buffer(path->nodes[*level]);
4040 path->nodes[*level] = next;
4041 path->slots[*level] = 0;
4042 account_file_data = 0;
4044 update_nodes_refs(root, (u64)-1, next, nrefs, *level, check_all);
4049 static int walk_up_tree(struct btrfs_root *root, struct btrfs_path *path,
4053 struct extent_buffer *leaf;
4055 for (i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) {
4056 leaf = path->nodes[i];
4057 if (path->slots[i] + 1 < btrfs_header_nritems(leaf)) {
4062 free_extent_buffer(path->nodes[*level]);
4063 path->nodes[*level] = NULL;
4071 * Insert the missing inode item and inode ref.
4073 * Normal INODE_ITEM_MISSING and INODE_REF_MISSING are handled in backref * dir.
4074 * Root dir should be handled specially because root dir is the root of fs.
4076 * returns err (>0 or 0) after repair
4078 static int repair_fs_first_inode(struct btrfs_root *root, int err)
4080 struct btrfs_trans_handle *trans;
4081 struct btrfs_key key;
4082 struct btrfs_path path;
4083 int filetype = BTRFS_FT_DIR;
4086 btrfs_init_path(&path);
4088 if (err & INODE_REF_MISSING) {
4089 key.objectid = BTRFS_FIRST_FREE_OBJECTID;
4090 key.type = BTRFS_INODE_REF_KEY;
4091 key.offset = BTRFS_FIRST_FREE_OBJECTID;
4093 trans = btrfs_start_transaction(root, 1);
4094 if (IS_ERR(trans)) {
4095 ret = PTR_ERR(trans);
4099 btrfs_release_path(&path);
4100 ret = btrfs_search_slot(trans, root, &key, &path, 1, 1);
4104 ret = btrfs_insert_inode_ref(trans, root, "..", 2,
4105 BTRFS_FIRST_FREE_OBJECTID,
4106 BTRFS_FIRST_FREE_OBJECTID, 0);
4110 printf("Add INODE_REF[%llu %llu] name %s\n",
4111 BTRFS_FIRST_FREE_OBJECTID, BTRFS_FIRST_FREE_OBJECTID,
4113 err &= ~INODE_REF_MISSING;
4116 error("fail to insert first inode's ref");
4117 btrfs_commit_transaction(trans, root);
4120 if (err & INODE_ITEM_MISSING) {
4121 ret = repair_inode_item_missing(root,
4122 BTRFS_FIRST_FREE_OBJECTID, filetype);
4125 err &= ~INODE_ITEM_MISSING;
4129 error("fail to repair first inode");
4130 btrfs_release_path(&path);
4135 * check first root dir's inode_item and inode_ref
4137 * returns 0 means no error
4138 * returns >0 means error
4139 * returns <0 means fatal error
4141 static int check_fs_first_inode(struct btrfs_root *root, unsigned int ext_ref)
4143 struct btrfs_path path;
4144 struct btrfs_key key;
4145 struct btrfs_inode_item *ii;
4151 key.objectid = BTRFS_FIRST_FREE_OBJECTID;
4152 key.type = BTRFS_INODE_ITEM_KEY;
4155 /* For root being dropped, we don't need to check first inode */
4156 if (btrfs_root_refs(&root->root_item) == 0 &&
4157 btrfs_disk_key_objectid(&root->root_item.drop_progress) >=
4158 BTRFS_FIRST_FREE_OBJECTID)
4161 btrfs_init_path(&path);
4162 ret = btrfs_search_slot(NULL, root, &key, &path, 0, 0);
4167 err |= INODE_ITEM_MISSING;
4169 ii = btrfs_item_ptr(path.nodes[0], path.slots[0],
4170 struct btrfs_inode_item);
4171 mode = btrfs_inode_mode(path.nodes[0], ii);
4172 if (imode_to_type(mode) != BTRFS_FT_DIR)
4173 err |= INODE_ITEM_MISMATCH;
4176 /* lookup first inode ref */
4177 key.offset = BTRFS_FIRST_FREE_OBJECTID;
4178 key.type = BTRFS_INODE_REF_KEY;
4179 /* special index value */
4182 ret = find_inode_ref(root, &key, "..", strlen(".."), &index, ext_ref);
4188 btrfs_release_path(&path);
4191 err = repair_fs_first_inode(root, err);
4193 if (err & (INODE_ITEM_MISSING | INODE_ITEM_MISMATCH))
4194 error("root dir INODE_ITEM is %s",
4195 err & INODE_ITEM_MISMATCH ? "mismatch" : "missing");
4196 if (err & INODE_REF_MISSING)
4197 error("root dir INODE_REF is missing");
4199 return ret < 0 ? ret : err;
4203 * This function calls walk_down_tree and walk_up_tree to check tree
4204 * blocks and integrity of fs tree items.
4206 * @root: the root of the tree to be checked.
4207 * @ext_ref feature EXTENDED_IREF is enable or not.
4208 * @account if NOT 0 means check the tree (including tree)'s treeblocks.
4209 * otherwise means check fs tree(s) items relationship and
4210 * @root MUST be a fs tree root.
4211 * Returns 0 represents OK.
4212 * Returns not 0 represents error.
4214 static int check_btrfs_root(struct btrfs_trans_handle *trans,
4215 struct btrfs_root *root, unsigned int ext_ref,
4218 struct btrfs_path path;
4219 struct node_refs nrefs;
4220 struct btrfs_root_item *root_item = &root->root_item;
4225 memset(&nrefs, 0, sizeof(nrefs));
4228 * We need to manually check the first inode item (256)
4229 * As the following traversal function will only start from
4230 * the first inode item in the leaf, if inode item (256) is
4231 * missing we will skip it forever.
4233 ret = check_fs_first_inode(root, ext_ref);
4239 level = btrfs_header_level(root->node);
4240 btrfs_init_path(&path);
4242 if (btrfs_root_refs(root_item) > 0 ||
4243 btrfs_disk_key_objectid(&root_item->drop_progress) == 0) {
4244 path.nodes[level] = root->node;
4245 path.slots[level] = 0;
4246 extent_buffer_get(root->node);
4248 struct btrfs_key key;
4250 btrfs_disk_key_to_cpu(&key, &root_item->drop_progress);
4251 level = root_item->drop_level;
4252 path.lowest_level = level;
4253 ret = btrfs_search_slot(NULL, root, &key, &path, 0, 0);
4260 ret = walk_down_tree(trans, root, &path, &level, &nrefs,
4261 ext_ref, check_all);
4265 /* if ret is negative, walk shall stop */
4271 ret = walk_up_tree(root, &path, &level);
4273 /* Normal exit, reset ret to err */
4280 btrfs_release_path(&path);
4285 * Iterate all items in the tree and call check_inode_item() to check.
4287 * @root: the root of the tree to be checked.
4288 * @ext_ref: the EXTENDED_IREF feature
4290 * Return 0 if no error found.
4291 * Return <0 for error.
4293 static int check_fs_root(struct btrfs_root *root, unsigned int ext_ref)
4295 reset_cached_block_groups(root->fs_info);
4296 return check_btrfs_root(NULL, root, ext_ref, 0);
4300 * Find the relative ref for root_ref and root_backref.
4302 * @root: the root of the root tree.
4303 * @ref_key: the key of the root ref.
4305 * Return 0 if no error occurred.
4307 static int check_root_ref(struct btrfs_root *root, struct btrfs_key *ref_key,
4308 struct extent_buffer *node, int slot)
4310 struct btrfs_path path;
4311 struct btrfs_key key;
4312 struct btrfs_root_ref *ref;
4313 struct btrfs_root_ref *backref;
4314 char ref_name[BTRFS_NAME_LEN] = {0};
4315 char backref_name[BTRFS_NAME_LEN] = {0};
4321 u32 backref_namelen;
4326 ref = btrfs_item_ptr(node, slot, struct btrfs_root_ref);
4327 ref_dirid = btrfs_root_ref_dirid(node, ref);
4328 ref_seq = btrfs_root_ref_sequence(node, ref);
4329 ref_namelen = btrfs_root_ref_name_len(node, ref);
4331 if (ref_namelen <= BTRFS_NAME_LEN) {
4334 len = BTRFS_NAME_LEN;
4335 warning("%s[%llu %llu] ref_name too long",
4336 ref_key->type == BTRFS_ROOT_REF_KEY ?
4337 "ROOT_REF" : "ROOT_BACKREF", ref_key->objectid,
4340 read_extent_buffer(node, ref_name, (unsigned long)(ref + 1), len);
4342 /* Find relative root_ref */
4343 key.objectid = ref_key->offset;
4344 key.type = BTRFS_ROOT_BACKREF_KEY + BTRFS_ROOT_REF_KEY - ref_key->type;
4345 key.offset = ref_key->objectid;
4347 btrfs_init_path(&path);
4348 ret = btrfs_search_slot(NULL, root, &key, &path, 0, 0);
4350 err |= ROOT_REF_MISSING;
4351 error("%s[%llu %llu] couldn't find relative ref",
4352 ref_key->type == BTRFS_ROOT_REF_KEY ?
4353 "ROOT_REF" : "ROOT_BACKREF",
4354 ref_key->objectid, ref_key->offset);
4358 backref = btrfs_item_ptr(path.nodes[0], path.slots[0],
4359 struct btrfs_root_ref);
4360 backref_dirid = btrfs_root_ref_dirid(path.nodes[0], backref);
4361 backref_seq = btrfs_root_ref_sequence(path.nodes[0], backref);
4362 backref_namelen = btrfs_root_ref_name_len(path.nodes[0], backref);
4364 if (backref_namelen <= BTRFS_NAME_LEN) {
4365 len = backref_namelen;
4367 len = BTRFS_NAME_LEN;
4368 warning("%s[%llu %llu] ref_name too long",
4369 key.type == BTRFS_ROOT_REF_KEY ?
4370 "ROOT_REF" : "ROOT_BACKREF",
4371 key.objectid, key.offset);
4373 read_extent_buffer(path.nodes[0], backref_name,
4374 (unsigned long)(backref + 1), len);
4376 if (ref_dirid != backref_dirid || ref_seq != backref_seq ||
4377 ref_namelen != backref_namelen ||
4378 strncmp(ref_name, backref_name, len)) {
4379 err |= ROOT_REF_MISMATCH;
4380 error("%s[%llu %llu] mismatch relative ref",
4381 ref_key->type == BTRFS_ROOT_REF_KEY ?
4382 "ROOT_REF" : "ROOT_BACKREF",
4383 ref_key->objectid, ref_key->offset);
4386 btrfs_release_path(&path);
4391 * Check all fs/file tree in low_memory mode.
4393 * 1. for fs tree root item, call check_fs_root()
4394 * 2. for fs tree root ref/backref, call check_root_ref()
4396 * Return 0 if no error occurred.
4398 int check_fs_roots_lowmem(struct btrfs_fs_info *fs_info)
4400 struct btrfs_root *tree_root = fs_info->tree_root;
4401 struct btrfs_root *cur_root = NULL;
4402 struct btrfs_path path;
4403 struct btrfs_key key;
4404 struct extent_buffer *node;
4405 unsigned int ext_ref;
4410 ext_ref = btrfs_fs_incompat(fs_info, EXTENDED_IREF);
4412 btrfs_init_path(&path);
4413 key.objectid = BTRFS_FS_TREE_OBJECTID;
4415 key.type = BTRFS_ROOT_ITEM_KEY;
4417 ret = btrfs_search_slot(NULL, tree_root, &key, &path, 0, 0);
4421 } else if (ret > 0) {
4427 node = path.nodes[0];
4428 slot = path.slots[0];
4429 btrfs_item_key_to_cpu(node, &key, slot);
4430 if (key.objectid > BTRFS_LAST_FREE_OBJECTID)
4432 if (key.type == BTRFS_ROOT_ITEM_KEY &&
4433 fs_root_objectid(key.objectid)) {
4434 if (key.objectid == BTRFS_TREE_RELOC_OBJECTID) {
4435 cur_root = btrfs_read_fs_root_no_cache(fs_info,
4438 key.offset = (u64)-1;
4439 cur_root = btrfs_read_fs_root(fs_info, &key);
4442 if (IS_ERR(cur_root)) {
4443 error("Fail to read fs/subvol tree: %lld",
4449 ret = check_fs_root(cur_root, ext_ref);
4452 if (key.objectid == BTRFS_TREE_RELOC_OBJECTID)
4453 btrfs_free_fs_root(cur_root);
4454 } else if (key.type == BTRFS_ROOT_REF_KEY ||
4455 key.type == BTRFS_ROOT_BACKREF_KEY) {
4456 ret = check_root_ref(tree_root, &key, node, slot);
4460 ret = btrfs_next_item(tree_root, &path);
4470 btrfs_release_path(&path);
4475 * Low memory usage version check_chunks_and_extents.
4477 int check_chunks_and_extents_lowmem(struct btrfs_fs_info *fs_info)
4479 struct btrfs_trans_handle *trans = NULL;
4480 struct btrfs_path path;
4481 struct btrfs_key old_key;
4482 struct btrfs_key key;
4483 struct btrfs_root *root1;
4484 struct btrfs_root *root;
4485 struct btrfs_root *cur_root;
4489 root = fs_info->fs_root;
4492 trans = btrfs_start_transaction(fs_info->extent_root, 1);
4493 if (IS_ERR(trans)) {
4494 error("failed to start transaction before check");
4495 return PTR_ERR(trans);
4499 root1 = root->fs_info->chunk_root;
4500 ret = check_btrfs_root(trans, root1, 0, 1);
4503 root1 = root->fs_info->tree_root;
4504 ret = check_btrfs_root(trans, root1, 0, 1);
4507 btrfs_init_path(&path);
4508 key.objectid = BTRFS_EXTENT_TREE_OBJECTID;
4510 key.type = BTRFS_ROOT_ITEM_KEY;
4512 ret = btrfs_search_slot(NULL, root1, &key, &path, 0, 0);
4514 error("cannot find extent tree in tree_root");
4519 btrfs_item_key_to_cpu(path.nodes[0], &key, path.slots[0]);
4520 if (key.type != BTRFS_ROOT_ITEM_KEY)
4523 key.offset = (u64)-1;
4525 if (key.objectid == BTRFS_TREE_RELOC_OBJECTID)
4526 cur_root = btrfs_read_fs_root_no_cache(root->fs_info,
4529 cur_root = btrfs_read_fs_root(root->fs_info, &key);
4530 if (IS_ERR(cur_root) || !cur_root) {
4531 error("failed to read tree: %lld", key.objectid);
4535 ret = check_btrfs_root(trans, cur_root, 0, 1);
4538 if (key.objectid == BTRFS_TREE_RELOC_OBJECTID)
4539 btrfs_free_fs_root(cur_root);
4541 btrfs_release_path(&path);
4542 ret = btrfs_search_slot(NULL, root->fs_info->tree_root,
4543 &old_key, &path, 0, 0);
4547 ret = btrfs_next_item(root1, &path);
4553 /* if repair, update block accounting */
4555 ret = btrfs_fix_block_accounting(trans, root);
4559 err &= ~BG_ACCOUNTING_ERROR;
4563 btrfs_commit_transaction(trans, root->fs_info->extent_root);
4565 btrfs_release_path(&path);