Btrfs-progs: fix horrible things
Subsystems (2)
This is the culmination of two weeks worth of work with Jaap Pieroen and his broekn file system. This pulls back a few things from the kernel in order to support the dir item stuff, and the rbtree stuff to fix a weird bug we were seeing. There will be more test images coming, but this is what I have so far. David if you want this in a branch to pull let me know, this has the updated "repair missing dir index" patch so you can drop the old one. With these patches we can now deal with most if not all dir index corruption issues and be able to fix corrupt blocks that are pointed at by multiple snapshots. Thanks,
Josef
PATCH 1/12 Btrfs-progs: repair missing dir index
If we have an inode backref entry then we know enough to add back a missing dir index. When messing with the inode backrefs we need to do all of that first before we process the inode recs themselves as we may clear errors on the inode recs as we fix the directory indexes. This adds the framework for fixing backref errors and fixes missing dir index issues. Thanks,
--- cmds-check.c | 121 +++++++++++++++++++++++++++++++++- tests/fsck-tests/004-no-dir-index.img | Bin 0 -> 4096 bytes 2 files changed, 119 insertions(+), 2 deletions(-) create mode 100644 tests/fsck-tests/004-no-dir-index.img
cmds-check.c +119 -2
@@ -1516,15 +1516,111 @@ static int repair_inode_orphan_item(struct btrfs_trans_handle *trans,return ret; } +static int add_missing_dir_index (struct btrfs_root *root, +struct cache_tree *inode_cache, +struct inode_record *rec, +struct inode_backref *backref) +{ +struct btrfs_path *path; +struct btrfs_trans_handle *trans; +struct btrfs_dir_item *dir_item; +struct extent_buffer *leaf; +struct btrfs_key key; +struct btrfs_disk_key disk_key; +struct inode_record *dir_rec; +unsigned long name_ptr; +u32 data_size =sizeof (*dir_item) + backref->namelen; +int ret; + + path =btrfs_alloc_path (); +if (!path) +return -ENOMEM ; + + trans =btrfs_start_transaction (root,1 ); +if (IS_ERR (trans)) { +btrfs_free_path (path); +return PTR_ERR (trans); + } + +fprintf (stderr,"repairing missing dir index item for inode %llu\n" , + (unsigned long long )rec->ino); + key.objectid = backref->dir; + key.type =BTRFS_DIR_INDEX_KEY ; + key.offset = backref->index; + + ret =btrfs_insert_empty_item (trans, root, path, &key, data_size); +BUG_ON (ret); + + leaf = path->nodes[0 ]; + dir_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dir_item); + + disk_key.objectid =cpu_to_le64 (rec->ino); + disk_key.type =BTRFS_INODE_ITEM_KEY ; + disk_key.offset =0 ; + +btrfs_set_dir_item_key (leaf, dir_item, &disk_key); +btrfs_set_dir_type (leaf, dir_item,imode_to_type (rec->imode)); +btrfs_set_dir_data_len (leaf, dir_item,0 ); +btrfs_set_dir_name_len (leaf, dir_item, backref->namelen); + name_ptr = (unsigned long )(dir_item +1 ); +write_extent_buffer (leaf, backref->name, name_ptr, backref->namelen); +btrfs_mark_buffer_dirty (leaf); +btrfs_free_path (path); +btrfs_commit_transaction (trans, root); + + backref->found_dir_index =1 ; + dir_rec =get_inode_rec (inode_cache, backref->dir,0 ); +if (!dir_rec) +return 0 ; + dir_rec->found_size += backref->namelen; +if (dir_rec->found_size == dir_rec->isize && + (dir_rec->errors &I_ERR_DIR_ISIZE_WRONG )) + dir_rec->errors &= ~I_ERR_DIR_ISIZE_WRONG ; +if (dir_rec->found_size != dir_rec->isize) + dir_rec->errors |=I_ERR_DIR_ISIZE_WRONG ; + +return 0 ; +} + +static int repair_inode_backrefs (struct btrfs_root *root, +struct inode_record *rec, +struct cache_tree *inode_cache) +{ +struct inode_backref *tmp, *backref; +u64 root_dirid =btrfs_root_dirid (&root->root_item); +int ret =0 ; + +list_for_each_entry_safe (backref, tmp, &rec->backrefs, list) { +/* Index 0 for root dir's are special, don't mess with it */ +if (rec->ino == root_dirid && backref->index ==0 ) +continue ; + +if (!backref->found_dir_index && backref->found_inode_ref) { + ret =add_missing_dir_index (root, inode_cache, rec, + backref); +if (ret) +break ; + } + +if (backref->found_dir_item && backref->found_dir_index) { +if (!backref->errors && backref->found_inode_ref) { +list_del (&backref->list); +free (backref); + } + } + } + +return ret; +} +static int try_repair_inode (struct btrfs_root *root,struct inode_record *rec) {struct btrfs_trans_handle *trans;struct btrfs_path *path;int ret =0 ; -/* So far we just fix dir isize wrong */ if (!(rec->errors & (I_ERR_DIR_ISIZE_WRONG |I_ERR_NO_ORPHAN_ITEM ))) -return 1 ; +return rec->errors; path =btrfs_alloc_path ();if (!path)
@@ -1562,6 +1658,27 @@ static int check_inode_recs(struct btrfs_root *root,return 0 ; } +/* +* We need to repair backrefs first because we could change some of the +* errors in the inode recs. +* +* For example, if we were missing a dir index then the directories +* isize would be wrong, so if we fixed the isize to what we thought it +* would be and then fixed the backref we'd still have a invalid fs, so +* we need to add back the dir index and then check to see if the isize +* is still wrong. +*/ + cache =search_cache_extent (inode_cache,0 ); +while (repair && cache) { + node = container_of(cache, struct ptr_node, cache); + rec = node->data; + cache =next_cache_extent (cache); + +if (list_empty (&rec->backrefs)) +continue ; +repair_inode_backrefs (root, rec, inode_cache); + } + rec =get_inode_rec (inode_cache, root_dirid,0 );if (rec) { ret =check_root_dir (rec);
tests/fsck-tests/004-no-dir-index.img new file binary +0 -0
Signature · 1 line
1.8.3.1
PATCH 2/12 Btrfs-progs: pull back backref.c and fix it up
In reply to Josef BacikThis patch pulls back backref.c, adds a couple of helpers everywhere that it needs, and cleans up backref.c to fit in btrfs-progs. Thanks,
--- Makefile | 2 +- backref.c | 1651 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ backref.h | 73 +++ ctree.c | 84 +++ ctree.h | 14 + extent_io.c | 43 +- extent_io.h | 2 + kerncompat.h | 7 + ulist.h | 15 + 9 files changed, 1878 insertions(+), 13 deletions(-) create mode 100644 backref.c create mode 100644 backref.h
Makefile +1 -1
@@ -10,7 +10,7 @@ objects = ctree.o disk-io.o radix-tree.o extent-tree.o print-tree.o \ root-tree.o dir-item.o file-item.o inode-item.o inode-map.o \ extent-cache.o extent_io.o volumes.o utils.o repair.o \ qgroup.o raid6.o free-space-cache.o list_sort.o props.o \ - ulist.o qgroup-verify.o + ulist.o qgroup-verify.o backref.o cmds_objects = cmds-subvolume.o cmds-filesystem.o cmds-device.o cmds-scrub.o \ cmds-inspect.o cmds-balance.o cmds-send.o cmds-receive.o \ cmds-quota.o cmds-qgroup.o cmds-replace.o cmds-check.o \
backref.c new file +1651 -0
1,651 lines not on this page · raw message
backref.h new file +73 -0
73 lines not on this page · raw message
ctree.c +84 -0
93 lines not on this page · raw message
ctree.h +14 -0
@@ -2253,6 +2253,8 @@ struct extent_buffer *read_node_slot(struct btrfs_root *root,int btrfs_previous_item (struct btrfs_root *root,struct btrfs_path *path,u64 min_objectid,int type); +int btrfs_previous_extent_item (struct btrfs_root *root, +struct btrfs_path *path,u64 min_objectid); int btrfs_cow_block(struct btrfs_trans_handle *trans, struct btrfs_root *root, struct extent_buffer *buf, struct extent_buffer *parent, int parent_slot,
@@ -2281,6 +2283,9 @@ int btrfs_split_item(struct btrfs_trans_handle *trans,int btrfs_search_slot (struct btrfs_trans_handle *trans,struct btrfs_root *root,struct btrfs_key *key,struct btrfs_path *p,int ins_len,int cow); +int btrfs_find_item (struct btrfs_root *fs_root,struct btrfs_path *found_path, +u64 iobjectid,u64 ioff,u8 key_type, +struct btrfs_key *found_key);void btrfs_release_path (struct btrfs_path *p);void add_root_to_dirty_list (struct btrfs_root *root);struct btrfs_path *btrfs_alloc_path (void );
@@ -2313,6 +2318,15 @@ static inline int btrfs_insert_empty_item(struct btrfs_trans_handle *trans, }int btrfs_next_leaf (struct btrfs_root *root,struct btrfs_path *path); +static inline int btrfs_next_item (struct btrfs_root *root, +struct btrfs_path *p) +{ + ++p->slots[0 ]; +if (p->slots[0 ] >=btrfs_header_nritems (p->nodes[0 ])) +return btrfs_next_leaf (root, p); +return 0 ; +} +int btrfs_prev_leaf (struct btrfs_root *root,struct btrfs_path *path);int btrfs_leaf_free_space (struct btrfs_root *root,struct extent_buffer *leaf); void btrfs_fixup_low_keys(struct btrfs_root *root, struct btrfs_path *path,
extent_io.c +31 -12
76 lines not on this page · raw message
extent_io.h +2 -0
@@ -40,6 +40,7 @@#define EXTENT_BUFFER_FILLED (1 << 8)#define EXTENT_CSUM (1 << 9)#define EXTENT_BAD_TRANSID (1 << 10) +#define EXTENT_BUFFER_DUMMY (1 << 11)#define EXTENT_IOBITS (EXTENT_LOCKED | EXTENT_WRITEBACK)#define BLOCK_GROUP_DATA EXTENT_WRITEBACK
@@ -111,6 +112,7 @@ struct extent_buffer *find_first_extent_buffer(struct extent_io_tree *tree, u64 start);struct extent_buffer *alloc_extent_buffer (struct extent_io_tree *tree,u64 bytenr,u32 blocksize); +struct extent_buffer *btrfs_clone_extent_buffer (struct extent_buffer *src);void free_extent_buffer (struct extent_buffer *eb);int read_extent_from_disk (struct extent_buffer *eb,unsigned long offset,unsigned long len);
kerncompat.h +7 -0
@@ -102,6 +102,9 @@ typedef __u32 u32;typedef __u64 u64 ;typedef __u16 u16 ;typedef __u8 u8 ; +typedef __s64 s64 ; +typedef __s32 s32 ; +/* * Continuing to define __KERNEL__ breaks others parts of the code, so * we can just undefine it now that we have the correct headers...
@@ -113,6 +116,8 @@ typedef unsigned int __u32;typedef unsigned long long u64 ;typedef unsigned char u8 ;typedef unsigned short u16 ; +typedef long long s64 ; +typedef int s32#endif
@@ -263,6 +268,8 @@ static inline long IS_ERR(const void *ptr)#define kzalloc (x, y) calloc(1, x)#define kstrdup (x, y) strdup(x)#define kfree (x) free(x) +#define vmalloc (x) malloc(x) +#define vfree (x) free(x)#define BUG_ON (c) assert_trace(#c, __FILE__, __func__, __LINE__, !(c))
ulist.h +15 -0
@@ -58,6 +58,21 @@ void ulist_free(struct ulist *ulist);int ulist_add (struct ulist *ulist,u64 val,u64 aux,gfp_t gfp_mask);int ulist_add_merge (struct ulist *ulist,u64 val,u64 aux,u64 *old_aux,gfp_t gfp_mask); + +/* just like ulist_add_merge() but take a pointer for the aux data */ +static inline int ulist_add_merge_ptr (struct ulist *ulist,u64 val,void *aux, +void **old_aux,gfp_t gfp_mask) +{ +#if BITS_PER_LONG ==32 +u64 old64 = (uintptr_t )*old_aux; +int ret =ulist_add_merge (ulist, val, (uintptr_t )aux, &old64, gfp_mask); + *old_aux = (void *)((uintptr_t )old64); +return ret; +#else +return ulist_add_merge (ulist, val, (u64 )aux, (u64 *)old_aux, gfp_mask); +#endif +} +struct ulist_node *ulist_next (struct ulist *ulist,struct ulist_iterator *uiter);
Signature · 1 line
1.8.3.1
PATCH 3/12 Btrfs-progs: break out rbtree util functions
In reply to Josef BacikThese were added to deal with duplicated functionality within btrfs-progs, but we specifically copied rbtree.c from the kernel, so move these functions out into their own file. This will make it easier to keep rbtree.c in sync. Thanks,
--- Makefile | 2 +- btrfs-list.c | 1 + cmds-check.c | 1 + disk-io.c | 1 + extent-cache.c | 1 + qgroup-verify.c | 1 + rbtree-utils.c | 82 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ rbtree-utils.h | 45 +++++++++++++++++++++++++++++++ rbtree.c | 63 -------------------------------------------- rbtree.h | 22 ---------------- 10 files changed, 133 insertions(+), 86 deletions(-) create mode 100644 rbtree-utils.c create mode 100644 rbtree-utils.h
Makefile +1 -1
@@ -10,7 +10,7 @@ objects = ctree.o disk-io.o radix-tree.o extent-tree.o print-tree.o \ root-tree.o dir-item.o file-item.o inode-item.o inode-map.o \ extent-cache.o extent_io.o volumes.o utils.o repair.o \ qgroup.o raid6.o free-space-cache.o list_sort.o props.o \ - ulist.o qgroup-verify.o backref.o + ulist.o qgroup-verify.o backref.o rbtree-utils.o cmds_objects = cmds-subvolume.o cmds-filesystem.o cmds-device.o cmds-scrub.o \ cmds-inspect.o cmds-balance.o cmds-send.o cmds-receive.o \ cmds-quota.o cmds-qgroup.o cmds-replace.o cmds-check.o \
btrfs-list.c +1 -0
@@ -33,6 +33,7 @@#include "utils.h" #include <uuid/uuid.h> #include "btrfs-list.h" +#include "rbtree-utils.h" #define BTRFS_LIST_NFILTERS_INCREASE (2 * BTRFS_LIST_FILTER_MAX)#define BTRFS_LIST_NCOMPS_INCREASE (2 * BTRFS_LIST_COMP_MAX)
cmds-check.c +1 -0
@@ -39,6 +39,7 @@#include "free-space-cache.h" #include "btrfsck.h" #include "qgroup-verify.h" +#include "rbtree-utils.h" static u64 bytes_used =0 ;static u64 total_csum_bytes =0 ;
disk-io.c +1 -0
@@ -34,6 +34,7 @@#include "crc32c.h" #include "utils.h" #include "print-tree.h" +#include "rbtree-utils.h" static int check_tree_block (struct btrfs_root *root,struct extent_buffer *buf) {
extent-cache.c +1 -0
@@ -19,6 +19,7 @@#include <stdlib.h> #include "kerncompat.h" #include "extent-cache.h" +#include "rbtree-utils.h" struct cache_extent_search_range {u64 objectid;
qgroup-verify.c +1 -0
@@ -28,6 +28,7 @@#include "print-tree.h" #include "utils.h" #include "ulist.h" +#include "rbtree-utils.h" #include "qgroup-verify.h"
rbtree-utils.c new file +82 -0
82 lines not on this page · raw message
rbtree-utils.h new file +45 -0
45 lines not on this page · raw message
rbtree.c +0 -63
66 lines not on this page · raw message
rbtree.h +0 -22
@@ -157,26 +157,4 @@ static inline void rb_link_node(struct rb_node * node, struct rb_node * parent, *rb_link = node; } - -/* The common insert/search/free functions */ -typedef int (*rb_compare_nodes )(struct rb_node *node1,struct rb_node *node2); -typedef int (*rb_compare_keys )(struct rb_node *node,void *key); -typedef void (*rb_free_node )(struct rb_node *node); - -int rb_insert (struct rb_root *root,struct rb_node *node, -rb_compare_nodes comp); -/* -* In some cases, we need return the next node if we don't find the node we -* specify. At this time, we can use next_ret. -*/ -struct rb_node *rb_search (struct rb_root *root,void *key,rb_compare_keys comp, -struct rb_node **next_ret); -void rb_free_nodes (struct rb_root *root,rb_free_node free_node); - -#define FREE_RB_BASED_TREE (name, free_func) \ -static void free_##name##_tree(struct rb_root *root) \ -{ \ - rb_free_nodes(root, free_func); \ -} -#endif /* _LINUX_RBTREE_H */
Signature · 1 line
1.8.3.1
PATCH 4/12 Btrfs-progs: update rbtree libs
In reply to Josef BacikWhile debugging a broken fs we were seeing hangs in the rb_erase loops. The rbtree was simple and wasn't corrupted so it appeared to be a bug in our rbtree library. Updating to the kernels latest rbtree code made the infinite loop go away, so pull it back. Thanks,
--- kerncompat.h | 5 + rbtree.c | 667 +++++++++++++++++++++++++++++++++-------------------- rbtree.h | 142 ++++-------- rbtree_augmented.h | 231 +++++++++++++++++++ 4 files changed, 695 insertions(+), 350 deletions(-) create mode 100644 rbtree_augmented.h
kerncompat.h +5 -0
@@ -330,6 +330,11 @@ struct __una_u64 { __le64 x; } __attribute__((__packed__));#define put_unaligned_le64 (val,p) (((struct __una_u64 *)(p))->x = cpu_to_le64(val))#endif +#ifndef true +#define true 1 +#define false 0 +#endif +#ifndef noinline#define noinline#endif
rbtree.c +413 -254
772 lines not on this page · raw message
rbtree.h +46 -96
181 lines not on this page · raw message
rbtree_augmented.h new file +231 -0
231 lines not on this page · raw message
Signature · 1 line
1.8.3.1
PATCH 5/12 Btrfs-progs: lookup all roots that point to a corrupt block
In reply to Josef BacikIf we have a corrupt block that multiple snapshots point to we will only fix the guy who originally pointed to the block, and then simply loop forever because we keep finding the same bad block. So instead lookup all roots that point to this block, and then search down to the block for each root and fix the block in all snapshots. Thanks,
--- cmds-check.c | 143 +++++++++++++++++++++++++++++------------------------------ 1 file changed, 70 insertions(+), 73 deletions(-)
cmds-check.c +70 -73
@@ -40,6 +40,8 @@#include "btrfsck.h" #include "qgroup-verify.h" #include "rbtree-utils.h" +#include "backref.h" +#include "ulist.h" static u64 bytes_used =0 ;static u64 total_csum_bytes =0 ;
@@ -2554,42 +2556,14 @@ static int swap_values(struct btrfs_root *root, struct btrfs_path *path,static int fix_key_order (struct btrfs_trans_handle *trans,struct btrfs_root *root, -struct extent_buffer *buf) +struct btrfs_path *path) { -struct btrfs_path *path; +struct extent_buffer *buf;struct btrfs_key k1, k2;int i; -int level; +int level = path->lowest_level;int ret; - k1.objectid =btrfs_header_owner (buf); - k1.type =BTRFS_ROOT_ITEM_KEY ; - k1.offset = (u64)-1 ; - - root =btrfs_read_fs_root (root->fs_info, &k1); -if (IS_ERR (root)) -return -EIO ; - -record_root_in_trans (trans, root); - - path =btrfs_alloc_path (); -if (!path) -return -EIO ; - - level =btrfs_header_level (buf); - path->lowest_level = level; - path->skip_check_block =1 ; -if (level) -btrfs_node_key_to_cpu (buf, &k1,0 ); -else -btrfs_item_key_to_cpu (buf, &k1,0 ); - - ret =btrfs_search_slot (trans, root, &k1, path,0 ,1 ); -if (ret) { -btrfs_free_path (path); -return -EIO ; - } - buf = path->nodes[level];for (i =0 ; i <btrfs_header_nritems (buf) -1 ; i++) {if (level) {
@@ -2607,8 +2581,6 @@ static int fix_key_order(struct btrfs_trans_handle *trans,btrfs_mark_buffer_dirty (buf); i =0 ; } - -btrfs_free_path (path);return ret; }
@@ -2650,43 +2622,15 @@ static int delete_bogus_item(struct btrfs_trans_handle *trans,static int fix_item_offset (struct btrfs_trans_handle *trans,struct btrfs_root *root, -struct extent_buffer *buf) +struct btrfs_path *path) { -struct btrfs_path *path; -struct btrfs_key k1; +struct extent_buffer *buf;int i; -int level; -int ret; - - k1.objectid =btrfs_header_owner (buf); - k1.type =BTRFS_ROOT_ITEM_KEY ; - k1.offset = (u64)-1 ; - - root =btrfs_read_fs_root (root->fs_info, &k1); -if (IS_ERR (root)) -return -EIO ; - -record_root_in_trans (trans, root); - - path =btrfs_alloc_path (); -if (!path) -return -EIO ; - - level =btrfs_header_level (buf); - path->lowest_level = level; - path->skip_check_block =1 ; -if (level) -btrfs_node_key_to_cpu (buf, &k1,0 ); -else -btrfs_item_key_to_cpu (buf, &k1,0 ); - - ret =btrfs_search_slot (trans, root, &k1, path,0 ,1 ); -if (ret) { -btrfs_free_path (path); -return -EIO ; - } +int ret =0 ; - buf = path->nodes[level]; +/* We should only get this for leaves */ +BUG_ON (path->lowest_level); + buf = path->nodes[0 ]; again:for (i =0 ; i <btrfs_header_nritems (buf); i++) {unsigned int shift =0 , offset;
@@ -2742,7 +2686,6 @@ again:* progs this can be changed to something nicer. */ BUG_ON (ret); -btrfs_free_path (path);return ret; }
@@ -2755,11 +2698,65 @@ static int try_to_fix_bad_block(struct btrfs_trans_handle *trans,struct extent_buffer *buf, enum btrfs_tree_block_status status) { -if (status ==BTRFS_TREE_BLOCK_BAD_KEY_ORDER ) -return fix_key_order (trans, root, buf); -if (status ==BTRFS_TREE_BLOCK_INVALID_OFFSETS ) -return fix_item_offset (trans, root, buf); -return -EIO ; + struct ulist *roots; +struct ulist_node *node; +struct btrfs_root *search_root; +struct btrfs_path *path; +struct ulist_iterator iter; +struct btrfs_key root_key, key; +int ret; + +if (status !=BTRFS_TREE_BLOCK_BAD_KEY_ORDER && + status !=BTRFS_TREE_BLOCK_INVALID_OFFSETS ) +return -EIO ; + + path =btrfs_alloc_path (); +if (!path) +return -EIO ; + + ret =btrfs_find_all_roots (trans, root->fs_info, buf->start, +0 , &roots); +if (ret) { +btrfs_free_path (path); +return -EIO ; + } + +ULIST_ITER_INIT (&iter); +while ((node =ulist_next (roots, &iter))) { + root_key.objectid = node->val; + root_key.type =BTRFS_ROOT_ITEM_KEY ; + root_key.offset = (u64)-1 ; + + search_root =btrfs_read_fs_root (root->fs_info, &root_key); +if (IS_ERR (root)) { + ret = -EIO ; +break ; + } + +record_root_in_trans (trans, search_root); + + path->lowest_level =btrfs_header_level (buf); + path->skip_check_block =1 ; +if (path->lowest_level) +btrfs_node_key_to_cpu (buf, &key,0 ); +else +btrfs_item_key_to_cpu (buf, &key,0 ); + ret =btrfs_search_slot (trans, search_root, &key, path,0 ,1 ); +if (ret) { + ret = -EIO ; +break ; + } +if (status ==BTRFS_TREE_BLOCK_BAD_KEY_ORDER ) + ret =fix_key_order (trans, search_root, path); +else if (status ==BTRFS_TREE_BLOCK_INVALID_OFFSETS ) + ret =fix_item_offset (trans, search_root, path); +if (ret) +break ; +btrfs_release_path (path); + } +ulist_free (roots); +btrfs_free_path (path); +return ret; } static int check_block(struct btrfs_trans_handle *trans,
Signature · 1 line
1.8.3.1
PATCH 6/12 Btrfs-progs: reset chunk state if we restart check
In reply to Josef BacikIf we hid a corrupt block that we fix and we restart the fsck loop you will get lots of noise about duplicate block groups and such. This is because we don't clear the block group and chunk cache when we do this restart. This patch fixes that, which is a little tricky since the structs are linked together with various linked lists, but this passed with a user who was hitting this problem. Thanks,
--- cmds-check.c | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-)
cmds-check.c +12 -1
@@ -3270,6 +3270,8 @@ static void free_chunk_record(struct cache_extent *cache)struct chunk_record *rec; rec = container_of(cache, struct chunk_record, cache); +list_del_init (&rec->list); +list_del_init (&rec->dextents);free (rec); }
@@ -3306,6 +3308,7 @@ static void free_block_group_record(struct cache_extent *cache)struct block_group_record *rec; rec = container_of(cache, struct block_group_record, cache); +list_del_init (&rec->list);free (rec); }
@@ -3339,6 +3342,10 @@ static void free_device_extent_record(struct cache_extent *cache)struct device_extent_record *rec; rec = container_of(cache, struct device_extent_record, cache); +if (!list_empty (&rec->chunk_list)) +list_del_init (&rec->chunk_list); +if (!list_empty (&rec->device_list)) +list_del_init (&rec->device_list);free (rec); }
@@ -6051,7 +6058,7 @@ static int check_device_used(struct device_record *dev_rec,if (dev_extent_rec->objectid != dev_rec->devid)break ; -list_del (&dev_extent_rec->device_list); +list_del_init (&dev_extent_rec->device_list); total_byte += dev_extent_rec->length; cache =next_cache_extent (cache); }
@@ -6280,6 +6287,10 @@ again:free_extent_cache_tree (&pending);free_extent_cache_tree (&reada);free_extent_cache_tree (&nodes); +free_chunk_cache_tree (&chunk_cache); +free_block_group_tree (&block_group_cache); +free_device_cache_tree (&dev_cache); +free_device_extent_tree (&dev_extent_cache);free_extent_record_cache (root->fs_info, &extent_cache); goto again; }
Signature · 1 line
1.8.3.1
PATCH 7/12 Btrfs-progs: re-search tree root if it changes
In reply to Josef BacikIf we change something while scanning fs-roots we need to redo our search so that we get valid root items and have valid root cache. Thanks,
--- cmds-check.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-)
cmds-check.c +8 -2
@@ -2171,7 +2171,7 @@ static int check_fs_roots(struct btrfs_root *root,struct btrfs_path path;struct btrfs_key key;struct walk_control wc; -struct extent_buffer *leaf; +struct extent_buffer *leaf, *tree_node;struct btrfs_root *tmp_root;struct btrfs_root *tree_root = root->fs_info->tree_root;int ret;
@@ -2186,13 +2186,19 @@ static int check_fs_roots(struct btrfs_root *root,memset (&wc,0 ,sizeof (wc));cache_tree_init (&wc.shared);btrfs_init_path (&path); - +again: key.offset =0 ; key.objectid =0 ; key.type =BTRFS_ROOT_ITEM_KEY ; ret =btrfs_search_slot (NULL , tree_root, &key, &path,0 ,0 );BUG_ON (ret <0 ); + tree_node = tree_root->node;while (1 ) { +if (tree_node != tree_root->node) { +free_root_recs_tree (root_cache); +btrfs_release_path (&path); + goto again; + } leaf = path.nodes[0 ];if (path.slots[0 ] >=btrfs_header_nritems (leaf)) { ret =btrfs_next_leaf (tree_root, &path);
Signature · 1 line
1.8.3.1
PATCH 8/12 Btrfs-progs: delete bogus dir indexes
In reply to Josef BacikWe may run across dir indexes that are corrupt in such a way that it makes them useless, such as having a bad location key or a bad name. In this case we can just delete dir indexes that don't show up properly and then re-create what we need. When we delete dir indexes however we need to restart scanning the fs tree as we could have greated bogus inode recs if the location key was bad, so set it up so that if we had to delete an dir index we go ahead and free up our inode recs and return -EAGAIN to check_fs_roots so it knows to restart the loop. Thanks,
--- cmds-check.c | 137 +++++++++++++++++++++++++++++++++++++++++++++++++++-------- ctree.h | 9 ++++ dir-item.c | 101 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 229 insertions(+), 18 deletions(-)
cmds-check.c +119 -18
@@ -1585,35 +1585,99 @@ static int add_missing_dir_index(struct btrfs_root *root,return 0 ; } +static int delete_dir_index (struct btrfs_root *root, +struct cache_tree *inode_cache, +struct inode_record *rec, +struct inode_backref *backref) +{ +struct btrfs_trans_handle *trans; +struct btrfs_dir_item *di; +struct btrfs_path *path; +int ret =0 ; + + path =btrfs_alloc_path (); +if (!path) +return -ENOMEM ; + + trans =btrfs_start_transaction (root,1 ); +if (IS_ERR (trans)) { +btrfs_free_path (path); +return PTR_ERR (trans); + } + + +fprintf (stderr,"Deleting bad dir index [%llu,%u,%llu] root %llu\n" , + (unsigned long long )backref->dir, +BTRFS_DIR_INDEX_KEY , (unsigned long long )backref->index, + (unsigned long long )root->objectid); + + di =btrfs_lookup_dir_index (trans, root, path, backref->dir, + backref->name, backref->namelen, + backref->index,-1 ); +if (IS_ERR (di)) { + ret =PTR_ERR (di); +btrfs_free_path (path); +btrfs_commit_transaction (trans, root); +if (ret == -ENOENT ) +return 0 ; +return ret; + } + +if (!di) + ret =btrfs_del_item (trans, root, path); +else + ret =btrfs_delete_one_dir_name (trans, root, path, di); +BUG_ON (ret); +btrfs_free_path (path); +btrfs_commit_transaction (trans, root); +return ret; +} +static int repair_inode_backrefs (struct btrfs_root *root,struct inode_record *rec, -struct cache_tree *inode_cache) +struct cache_tree *inode_cache, +int delete) {struct inode_backref *tmp, *backref;u64 root_dirid =btrfs_root_dirid (&root->root_item);int ret =0 ; +int repaired =0 ;list_for_each_entry_safe (backref, tmp, &rec->backrefs, list) {/* Index 0 for root dir's are special, don't mess with it */ if (rec->ino == root_dirid && backref->index ==0 )continue ; -if (!backref->found_dir_index && backref->found_inode_ref) { - ret =add_missing_dir_index (root, inode_cache, rec, - backref); +if (delete && backref->found_dir_index && + !backref->found_inode_ref) { + ret =delete_dir_index (root, inode_cache, rec, backref);if (ret)break ; + repaired++; +list_del (&backref->list); +free (backref); } -if (backref->found_dir_item && backref->found_dir_index) { -if (!backref->errors && backref->found_inode_ref) { -list_del (&backref->list); -free (backref); +if (!delete && !backref->found_dir_index && + backref->found_dir_item && backref->found_inode_ref) { + ret =add_missing_dir_index (root, inode_cache, rec, + backref); +if (ret) +break ; + repaired++; +if (backref->found_dir_item && + backref->found_dir_index && + backref->found_dir_index) { +if (!backref->errors && + backref->found_inode_ref) { +list_del (&backref->list); +free (backref); + } } } - } -return ret; + } +return ret ? ret : repaired; }static int try_repair_inode (struct btrfs_root *root,struct inode_record *rec)
@@ -1651,7 +1715,9 @@ static int check_inode_recs(struct btrfs_root *root,struct ptr_node *node;struct inode_record *rec;struct inode_backref *backref; +int stage =0 ;int ret; +int err =0 ;u64 error =0 ;u64 root_dirid =btrfs_root_dirid (&root->root_item);
@@ -1665,22 +1731,52 @@ static int check_inode_recs(struct btrfs_root *root,* We need to repair backrefs first because we could change some of the * errors in the inode recs. * +* We also need to go through and delete invalid backrefs first and then +* add the correct ones second. We do this because we may get EEXIST +* when adding back the correct index because we hadn't yet deleted the +* invalid index. +* * For example, if we were missing a dir index then the directories * isize would be wrong, so if we fixed the isize to what we thought it * would be and then fixed the backref we'd still have a invalid fs, so * we need to add back the dir index and then check to see if the isize * is still wrong. */ - cache =search_cache_extent (inode_cache,0 ); -while (repair && cache) { - node = container_of(cache, struct ptr_node, cache); - rec = node->data; - cache =next_cache_extent (cache); +while (stage <3 ) { + stage++; +if (stage ==3 && !err) +break ; -if (list_empty (&rec->backrefs)) -continue ; -repair_inode_backrefs (root, rec, inode_cache); + cache =search_cache_extent (inode_cache,0 ); +while (repair && cache) { + node = container_of(cache, struct ptr_node, cache); + rec = node->data; + cache =next_cache_extent (cache); + +/* Need to free everything up and rescan */ +if (stage ==3 ) { +remove_cache_extent (inode_cache, &node->cache); +free (node); +free_inode_rec (rec); +continue ; + } + +if (list_empty (&rec->backrefs)) +continue ; + + ret =repair_inode_backrefs (root, rec, inode_cache, + stage ==1 ); +if (ret <0 ) { + err = ret; + stage =2 ; +break ; + }if (ret >0 ) { + err = -EAGAIN ; + } + } } +if (err) +return err; rec =get_inode_rec (inode_cache, root_dirid,0 );if (rec) {
@@ -2216,6 +2312,11 @@ again: goto next; } ret =check_fs_root (tmp_root, root_cache, &wc); +if (ret == -EAGAIN ) { +free_root_recs_tree (root_cache); +btrfs_release_path (&path); + goto again; + }if (ret) err =1 ; } else if (key.type == BTRFS_ROOT_REF_KEY ||
ctree.h +9 -0
@@ -2360,6 +2360,15 @@ struct btrfs_dir_item *btrfs_lookup_dir_item(struct btrfs_trans_handle *trans, struct btrfs_path *path, u64 dir, const char *name, int name_len, int mod); +struct btrfs_dir_item *btrfs_lookup_dir_index (struct btrfs_trans_handle *trans, +struct btrfs_root *root, +struct btrfs_path *path,u64 dir, +const char *name,int name_len, +u64 index,int mod); +int btrfs_delete_one_dir_name (struct btrfs_trans_handle *trans, +struct btrfs_root *root, +struct btrfs_path *path, +struct btrfs_dir_item *di); int btrfs_insert_xattr_item(struct btrfs_trans_handle *trans, struct btrfs_root *root, const char *name, u16 name_len, const void *data, u16 data_len,
dir-item.c +101 -0
123 lines not on this page · raw message
Signature · 1 line
1.8.3.1
PATCH 9/12 Btrfs-progs: add a dummy backref if our location is wrong
In reply to Josef BacikIf our location is bogus in our dir item we were just skipping the thing. However in this case we want to just delete the dir index, so create a dummy inode rec using BTRFS_MULTIPLE_OBJECTIDS and just add every backref we find to the list so we know to straight up delete all of these items. Thanks,
--- cmds-check.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-)
cmds-check.c +7 -1
@@ -551,6 +551,8 @@ static struct inode_backref *get_inode_backref(struct inode_record *rec,struct inode_backref *backref;list_for_each_entry (backref, &rec->backrefs, list) { +if (rec->ino ==BTRFS_MULTIPLE_OBJECTIDS ) +break ;if (backref->dir != dir || backref->namelen != namelen)continue ;if (memcmp (name, backref->name, namelen))
@@ -990,7 +992,11 @@ static int process_dir_item(struct btrfs_root *root, namebuf, len, filetype, key->type, error); }else { -fprintf (stderr,"warning line %d\n" , __LINE__); +fprintf (stderr,"invalid location in dir item %u\n" , + location.type); +add_inode_backref (inode_cache,BTRFS_MULTIPLE_OBJECTIDS , + key->objectid, key->offset, namebuf, + len, filetype, key->type, error); } len =sizeof (*di) + name_len + data_len;
Signature · 1 line
1.8.3.1
PATCH 10/12 Btrfs-progs: deal with mismatch index between dir index and inode ref
In reply to Josef BacikSometimes we have a dir index and an inode ref that don't agree on the index. In this case just assume that the inode ref is the ultimate authority on the subject and delete the dir index. This means we have to not reset index if we find a mismatched inode ref to make sure we delete the right dir index. Thanks,
--- cmds-check.c | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-)
cmds-check.c +6 -3
@@ -608,9 +608,10 @@ static int add_inode_backref(struct cache_tree *inode_cache, backref->errors |=REF_ERR_DUP_INODE_REF ;if (backref->found_dir_index && backref->index != index) backref->errors |=REF_ERR_INDEX_UNMATCH ; +else + backref->index = index; backref->ref_type = itemtype; - backref->index = index; backref->found_inode_ref =1 ; }else {BUG_ON (1 );
@@ -1654,8 +1655,10 @@ static int repair_inode_backrefs(struct btrfs_root *root,if (rec->ino == root_dirid && backref->index ==0 )continue ; -if (delete && backref->found_dir_index && - !backref->found_inode_ref) { +if (delete && + ((backref->found_dir_index && !backref->found_inode_ref) || + (backref->found_dir_index && backref->found_inode_ref && + backref->errors &REF_ERR_INDEX_UNMATCH ))) { ret =delete_dir_index (root, inode_cache, rec, backref);if (ret)break ;
Signature · 1 line
1.8.3.1
PATCH 11/12 Btrfs-progs: add ability to corrupt dir items
In reply to Josef BacikIn order to test the dir index corruption fixing patches in fsck we need to add functionality to btrfs-corrupt-block to corrupt dir item fields. Thanks,
--- btrfs-corrupt-block.c | 103 +++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 102 insertions(+), 1 deletion(-)
btrfs-corrupt-block.c +102 -1
@@ -111,6 +111,7 @@ static void print_usage(void)fprintf (stderr,"\t-d Delete this item (must specify -K)\n" );fprintf (stderr,"\t-I An item to corrupt (must also specify the field " "to corrupt and a root+key for the item)\n" ); +fprintf (stderr,"\t-D Corrupt a dir item, must specify key and field\n" );exit (1 ); }
@@ -304,6 +305,12 @@ enum btrfs_file_extent_field { BTRFS_FILE_EXTENT_BAD, }; +enum btrfs_dir_item_field { +BTRFS_DIR_ITEM_NAME , +BTRFS_DIR_ITEM_LOCATION_OBJECTID , +BTRFS_DIR_ITEM_BAD , +}; +enum btrfs_metadata_block_field {BTRFS_METADATA_BLOCK_GENERATION ,BTRFS_METADATA_BLOCK_SHIFT_ITEMS ,
@@ -364,6 +371,15 @@ static enum btrfs_item_field convert_item_field(char *field)return BTRFS_ITEM_BAD ; } +static enum btrfs_dir_item_field convert_dir_item_field (char *field) +{ +if (!strncmp (field,"name" ,FIELD_BUF_LEN )) +return BTRFS_DIR_ITEM_NAME ; +if (!strncmp (field,"location_objectid" ,FIELD_BUF_LEN )) +return BTRFS_DIR_ITEM_LOCATION_OBJECTID ; +return BTRFS_DIR_ITEM_BAD ; +} +static u64 generate_u64 (u64 orig) {u64 ret;
@@ -448,6 +464,80 @@ out:return ret; } +static int corrupt_dir_item (struct btrfs_root *root,struct btrfs_key *key, +char *field) +{ +struct btrfs_trans_handle *trans; +struct btrfs_dir_item *di; +struct btrfs_path *path; +char *name; +struct btrfs_key location; +struct btrfs_disk_key disk_key; +unsigned long name_ptr; +enum btrfs_dir_item_field corrupt_field = +convert_dir_item_field (field); +u64 bogus; +u16 name_len; +int ret; + +if (corrupt_field ==BTRFS_DIR_ITEM_BAD ) { +fprintf (stderr,"Invalid field %s\n" , field); +return -EINVAL ; + } + + path =btrfs_alloc_path (); +if (!path) +return -ENOMEM ; + + trans =btrfs_start_transaction (root,1 ); +if (IS_ERR (trans)) { +btrfs_free_path (path); +return PTR_ERR (trans); + } + + ret =btrfs_search_slot (trans, root, key, path,0 ,1 ); +if (ret) { +if (ret >0 ) + ret = -ENOENT ; +fprintf (stderr,"Error searching for dir item %d\n" , ret); + goto out; + } + + di =btrfs_item_ptr (path->nodes[0 ], path->slots[0 ], + struct btrfs_dir_item); + +switch (corrupt_field) { +case BTRFS_DIR_ITEM_NAME : + name_len =btrfs_dir_name_len (path->nodes[0 ], di); + name =malloc (name_len); +if (!name) { + ret = -ENOMEM ; + goto out; + } + name_ptr = (unsigned long )(di +1 ); +read_extent_buffer (path->nodes[0 ], name, name_ptr, name_len); + name[0 ]++; +write_extent_buffer (path->nodes[0 ], name, name_ptr, name_len); +btrfs_mark_buffer_dirty (path->nodes[0 ]); +free (name); + goto out; +case BTRFS_DIR_ITEM_LOCATION_OBJECTID : +btrfs_dir_item_key_to_cpu (path->nodes[0 ], di, &location); + bogus =generate_u64 (location.objectid); + location.objectid = bogus; +btrfs_cpu_key_to_disk (&disk_key, &location); +btrfs_set_dir_item_key (path->nodes[0 ], di, &disk_key); +btrfs_mark_buffer_dirty (path->nodes[0 ]); + goto out; +default : + ret = -EINVAL ; + goto out; + } +out: +btrfs_commit_transaction (trans, root); +btrfs_free_path (path); +return ret; +}static int corrupt_inode (struct btrfs_trans_handle *trans,struct btrfs_root *root,u64 inode,char *field)
@@ -772,6 +862,7 @@ static struct option long_options[] = { { "key", 1, NULL, 'K'}, { "delete", 0, NULL, 'd'}, { "item", 0, NULL, 'I'}, + { "dir-item", 0, NULL, 'D'}, { 0, 0, 0, 0} };
@@ -937,6 +1028,7 @@ int main(int ac, char **av)int chunk_tree =0 ;int delete =0 ;int corrupt_item =0 ; +int corrupt_di =0 ;u64 metadata_block =0 ;u64 inode =0 ;u64 file_extent = (u64)-1 ;
@@ -948,7 +1040,7 @@ int main(int ac, char **av)while (1 ) {int c; - c =getopt_long (ac, av,"l:c:b:eEkuUi:f:x:m:K:dI" , + c =getopt_long (ac, av,"l:c:b:eEkuUi:f:x:m:K:dID" , long_options, &option_index);if (c <0 )break ;
@@ -1005,6 +1097,9 @@ int main(int ac, char **av)case 'I' : corrupt_item =1 ;break ; +case 'D' : + corrupt_di =1 ; +break ;default :print_usage (); }
@@ -1113,6 +1208,12 @@ int main(int ac, char **av) ret =corrupt_btrfs_item (root, &key, field); goto out_close; } +if (corrupt_di) { +if (!key.objectid || !strlen (field)) +print_usage (); + ret =corrupt_dir_item (root, &key, field); + goto out_close; + }if (key.objectid || key.offset || key.type) {if (!strlen (field))print_usage ();
Signature · 1 line
1.8.3.1