Skip to contentSkip to conversation

btrfs: Error handling/propagation queue

PATCH In review Jeff Mahoney 1 participant 12 messages Nov 4, 2009 Btrfs file system

lore
Share & tools
lore
#0 lore raw

cover btrfs: Error handling/propagation queue

This patch series fixes a few existing problems and then addresses the need for better error handling and propagation in btrfs.

The handling and propagation patch set first finds all locations of a particular condition not being checked and annotates them with BTRFS_UERROR to designate that there is an error condition that is currently unhandled but should be. Then, another patch traverses the call path to ensure that errors are properly propogated. When the fix for recovering or passing the error is obvious, we do that too.

This set is not complete and I expect there to be more fixes coming. In particular, it currently only annotates btrfs_alloc_path failures as the call graph for it is quite large. This is in response to Chris asking for what I already have instead of waiting for it to be complete.

-Jeff

Signature · 2 lines
Jeff Mahoney
SUSE Labs
#1 lore raw

PATCH 1/10 btrfs: fix btrfs_read_block_groups return value

btrfs_read_block_groups returns an ambiguous value. Whether it finds a block group or not, it will return -ENOENT. find_first_block_group will eventually return -ENOENT when it reaches past the last block group, and that is what is returned to the caller.

Also, if the kzalloc fails, it will return 0.

None of this matters right now because open_ctree isn't checking the return value, but I have a patch to handle that as well.

This patch returns 0 if find_first_block_group after it has already found at least one block group, and -ENOENT if it has found none. It also returns -ENOMEM if the kzalloc fails.

---
 fs/btrfs/extent-tree.c |   12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)
fs/btrfs/extent-tree.c +6 -6
@@ -7339,7 +7339,7 @@ int btrfs_free_block_groups(struct btrfs
 int btrfs_read_block_groups(struct btrfs_root *root)
 {
 	struct btrfs_path *path;
-	int ret;
+	int ret, found = 0;
 	struct btrfs_block_group_cache *cache;
 	struct btrfs_fs_info *info = root->fs_info;
 	struct btrfs_space_info *space_info;
@@ -7357,12 +7357,13 @@ int btrfs_read_block_groups(struct btrfs
 
 	while (1) {
 		ret = find_first_block_group(root, path, &key);
-		if (ret > 0) {
+		if (ret > 0 || (found && ret == -ENOENT)) {
 			ret = 0;
-			goto error;
+			break;
 		}
+
 		if (ret != 0)
-			goto error;
+			break;
 
 		leaf = path->nodes[0];
 		btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
@@ -7439,9 +7440,8 @@ int btrfs_read_block_groups(struct btrfs
 		set_avail_alloc_bits(root->fs_info, cache->flags);
 		if (btrfs_chunk_readonly(root, cache->key.objectid))
 			set_block_group_readonly(cache);
+		found = 1;
 	}
-	ret = 0;
-error:
 	btrfs_free_path(path);
 	return ret;
 }
#2 lore raw

PATCH 2/10 btrfs: fix memleak in btrfs_init_new_device

In reply to Jeff Mahoney

If find_next_devid fails, btrfs_init_new_device frees device but not device->name.

This patch fixes it.

---
 fs/btrfs/volumes.c |    1 +
 1 file changed, 1 insertion(+)
fs/btrfs/volumes.c +1 -0
@@ -1474,6 +1474,7 @@ int btrfs_init_new_device(struct btrfs_r
 
 	ret = find_next_devid(root, &device->devid);
 	if (ret) {
+		kfree(device->name);
 		kfree(device);
 		goto error;
 	}
#3 lore raw

PATCH 3/10 btrfs: fix btrfs_read_fs_root* return values

In reply to Jeff Mahoney

The btrfs_read_fs_root* functions return ERR_PTR values but the return is being checked for NULL instead.

This patch fixes up the checks to use IS_ERR.

---
 fs/btrfs/disk-io.c     |    4 +++-
 fs/btrfs/extent-tree.c |    6 +++---
 fs/btrfs/tree-log.c    |    4 ++--
 3 files changed, 8 insertions(+), 6 deletions(-)
fs/btrfs/disk-io.c +3 -1
@@ -1987,8 +1987,10 @@ struct btrfs_root *open_ctree(struct sup
 	location.offset = (u64)-1;
 
 	fs_info->fs_root = btrfs_read_fs_root_no_name(fs_info, &location);
-	if (!fs_info->fs_root)
+	if (IS_ERR(fs_info->fs_root)) {
+		err = PTR_ERR(fs_info->fs_root);
 		goto fail_trans_kthread;
+	}
 
 	return tree_root;
 
fs/btrfs/extent-tree.c +3 -3
@@ -6712,7 +6712,7 @@ int btrfs_cleanup_reloc_trees(struct btr
 	location.type = BTRFS_ROOT_ITEM_KEY;
 
 	reloc_root = btrfs_read_fs_root_no_name(root->fs_info, &location);
-	BUG_ON(!reloc_root);
+	BUG_ON(IS_ERR(reloc_root));
 	btrfs_orphan_cleanup(reloc_root);
 	return 0;
 }
@@ -6757,7 +6757,7 @@ static noinline int init_reloc_tree(stru
 
 	reloc_root = btrfs_read_fs_root_no_radix(root->fs_info->tree_root,
 						 &root_key);
-	BUG_ON(!reloc_root);
+	BUG_ON(IS_ERR(reloc_root));
 	reloc_root->last_trans = trans->transid;
 	reloc_root->commit_root = NULL;
 	reloc_root->ref_tree = &root->fs_info->reloc_ref_tree;
@@ -6976,7 +6976,7 @@ static noinline int relocate_one_extent(
 			continue;
 
 		found_root = read_ref_root(extent_root->fs_info, ref_path);
-		BUG_ON(!found_root);
+		BUG_ON(IS_ERR(found_root));
 		/*
 		 * for reference counted tree, only process reference paths
 		 * rooted at the latest committed root.
fs/btrfs/tree-log.c +2 -2
@@ -3042,7 +3042,7 @@ again:
 
 		log = btrfs_read_fs_root_no_radix(log_root_tree,
 						  &found_key);
-		BUG_ON(!log);
+		BUG_ON(IS_ERR(log));
 
 
 		tmp_key.objectid = found_key.offset;
@@ -3050,7 +3050,7 @@ again:
 		tmp_key.offset = (u64)-1;
 
 		wc.replay_dest = btrfs_read_fs_root_no_name(fs_info, &tmp_key);
-		BUG_ON(!wc.replay_dest);
+		BUG_ON(IS_ERR(wc.replay_dest));
 
 		wc.replay_dest->log_root = log;
 		btrfs_record_root_in_trans(trans, wc.replay_dest);
#4 lore raw

PATCH 4/10 btrfs: btrfs_sync_file should return -EIO not EIO

In reply to Jeff Mahoney

This patch fixes a typo in the return path of btrfs_sync_file, which currently returns EIO instead of -EIO.

---
 fs/btrfs/file.c |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
fs/btrfs/file.c +1 -1
@@ -1223,7 +1223,7 @@ int btrfs_sync_file(struct file *file, s
 	}
 	mutex_lock(&dentry->d_inode->i_mutex);
 out:
-	return ret > 0 ? EIO : ret;
+	return ret > 0 ? -EIO : ret;
 }
 
 static struct vm_operations_struct btrfs_file_vm_ops = {
#5 lore raw

PATCH 5/10 btrfs: Add BTRFS_UERROR for unhandled errors

In reply to Jeff Mahoney

This patch adds a BTRFS_UERROR macro to indicate than an error is unhandled but should be. This is to differentiate between internal logic errors and runtime errors so that we can go through later more easily to locate and fix them. It currently BUGs after printing a message indicating where the failure occured and what the failure condition was.

---

 fs/btrfs/ctree.h |    8 ++++++++
 fs/btrfs/super.c |    8 ++++++++
 2 files changed, 16 insertions(+)
fs/btrfs/ctree.h +8 -0
@@ -1951,6 +1951,13 @@ static inline struct dentry *fdentry(str
 	return file->f_path.dentry;
 }
 
+#define BTRFS_UERROR(condition) \
+	do { \
+		if (condition) \
+			btrfs_unhandled_error(__FILE__, __LINE__, \
+					      #condition); \
+	} while(0);
+
 /* extent-tree.c */
 void btrfs_put_block_group(struct btrfs_block_group_cache *cache);
 int btrfs_run_delayed_refs(struct btrfs_trans_handle *trans,
@@ -2373,6 +2380,7 @@ ssize_t btrfs_listxattr(struct dentry *d
 u64 btrfs_parse_size(char *str);
 int btrfs_parse_options(struct btrfs_root *root, char *options);
 int btrfs_sync_fs(struct super_block *sb, int wait);
+void btrfs_unhandled_error(const char *file, int line, const char *condition);
 
 /* acl.c */
 #ifdef CONFIG_BTRFS_FS_POSIX_ACL
fs/btrfs/super.c +8 -0
@@ -51,6 +51,14 @@
 #include "export.h"
 #include "compression.h"
 
+void btrfs_unhandled_error(const char *file, int line, const char *msg)
+{
+	printk(KERN_CRIT "btrfs: Unhandled error at %s:%d: %s\n",
+	       file, line, msg);
+
+	BUG();
+}
+
 static struct super_operations btrfs_super_ops;
 
 static void btrfs_put_super(struct super_block *sb)
#6 lore raw

PATCH 6/10 btrfs: annotate kmalloc failures

In reply to Jeff Mahoney

This patch adds checks to ensure that kmalloc or kmem_cache_alloc has succeeded before using the memory.

---

 fs/btrfs/compression.c |    2 ++
 fs/btrfs/ctree.c       |    1 +
 fs/btrfs/disk-io.c     |    1 +
 fs/btrfs/extent-tree.c |   12 +++++++-----
 fs/btrfs/file-item.c   |    4 ++--
 fs/btrfs/file.c        |    1 +
 fs/btrfs/inode.c       |    4 ++++
 fs/btrfs/relocation.c  |    8 ++++----
 fs/btrfs/transaction.c |    4 +++-
 fs/btrfs/tree-log.c    |    9 +++++++--
 fs/btrfs/volumes.c     |    2 +-
 11 files changed, 33 insertions(+), 15 deletions(-)
fs/btrfs/compression.c +2 -0
@@ -351,6 +351,7 @@ int btrfs_submit_compressed_write(struct
 
 	WARN_ON(start & ((u64)PAGE_CACHE_SIZE - 1));
 	cb = kmalloc(compressed_bio_size(root, compressed_len), GFP_NOFS);
+	BTRFS_UERROR(!cb);
 	atomic_set(&cb->pending_bios, 0);
 	cb->errors = 0;
 	cb->inode = inode;
@@ -622,6 +623,7 @@ int btrfs_submit_compressed_read(struct
 				 PAGE_CACHE_SIZE;
 	cb->compressed_pages = kmalloc(sizeof(struct page *) * nr_pages,
 				       GFP_NOFS);
+	BTRFS_UERROR(!cb->compressed_pages);
 	bdev = BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev;
 
 	for (page_index = 0; page_index < nr_pages; page_index++) {
fs/btrfs/ctree.c +1 -0
@@ -3073,6 +3073,7 @@ split:
 	item_size = btrfs_item_size(leaf, item);
 
 	buf = kmalloc(item_size, GFP_NOFS);
+	BTRFS_UERROR(!buf);
 	read_extent_buffer(leaf, buf, btrfs_item_ptr_offset(leaf,
 			    path->slots[0]), item_size);
 	slot = path->slots[0] + 1;
fs/btrfs/disk-io.c +1 -0
@@ -1958,6 +1958,7 @@ struct btrfs_root *open_ctree(struct sup
 
 		log_tree_root = kzalloc(sizeof(struct btrfs_root),
 						      GFP_NOFS);
+		BTRFS_UERROR(!log_tree_root);
 
 		__setup_root(nodesize, leafsize, sectorsize, stripesize,
 			     log_tree_root, fs_info, BTRFS_TREE_LOG_OBJECTID);
fs/btrfs/extent-tree.c +7 -5
@@ -414,7 +414,7 @@ static int cache_block_group(struct btrf
 		return 0;
 
 	caching_ctl = kzalloc(sizeof(*caching_ctl), GFP_KERNEL);
-	BUG_ON(!caching_ctl);
+	BTRFS_UERROR(!caching_ctl);
 
 	INIT_LIST_HEAD(&caching_ctl->list);
 	mutex_init(&caching_ctl->mutex);
@@ -5388,7 +5388,7 @@ int btrfs_drop_snapshot(struct btrfs_roo
 	BUG_ON(!path);
 
 	wc = kzalloc(sizeof(*wc), GFP_NOFS);
-	BUG_ON(!wc);
+	BTRFS_UERROR(!wc);
 
 	trans = btrfs_start_transaction(tree_root, 1);
 
@@ -5550,7 +5550,7 @@ int btrfs_drop_subtree(struct btrfs_tran
 	BUG_ON(!path);
 
 	wc = kzalloc(sizeof(*wc), GFP_NOFS);
-	BUG_ON(!wc);
+	BTRFS_UERROR(!wc);
 
 	btrfs_assert_tree_locked(parent);
 	parent_level = btrfs_header_level(parent);
@@ -6048,6 +6048,7 @@ static noinline int get_new_locations(st
 			struct disk_extent *old = exts;
 			max *= 2;
 			exts = kzalloc(sizeof(*exts) * max, GFP_NOFS);
+			BTRFS_UERROR(!exts);
 			memcpy(exts, old, sizeof(*exts) * nr);
 			if (old != *extents)
 				kfree(old);
@@ -6527,7 +6528,7 @@ static noinline int replace_extents_in_l
 	int ret;
 
 	new_extent = kmalloc(sizeof(*new_extent), GFP_NOFS);
-	BUG_ON(!new_extent);
+	BTRFS_UERROR(!new_extent)
 
 	ref = btrfs_lookup_leaf_ref(root, leaf->start);
 	BUG_ON(!ref);
@@ -6731,7 +6732,7 @@ static noinline int init_reloc_tree(stru
 		return 0;
 
 	root_item = kmalloc(sizeof(*root_item), GFP_NOFS);
-	BUG_ON(!root_item);
+	BTRFS_UERROR(!root_item);
 
 	ret = btrfs_copy_root(trans, root, root->commit_root,
 			      &eb, BTRFS_TREE_RELOC_OBJECTID);
@@ -7047,6 +7048,7 @@ static noinline int relocate_one_extent(
 				u64 group_start = group->key.objectid;
 				new_extents = kmalloc(sizeof(*new_extents),
 						      GFP_NOFS);
+				BTRFS_UERROR(!new_extents);
 				nr_extents = 1;
 				ret = get_new_locations(reloc_inode,
 							extent_key,
fs/btrfs/file-item.c +2 -2
@@ -320,7 +320,7 @@ int btrfs_lookup_csums_range(struct btrf
 					MAX_ORDERED_SUM_BYTES(root));
 			sums = kzalloc(btrfs_ordered_sum_size(root, size),
 					GFP_NOFS);
-			BUG_ON(!sums);
+			BTRFS_UERROR(!sums);
 
 			sector_sum = sums->sums;
 			sums->bytenr = start;
@@ -401,7 +401,7 @@ int btrfs_csum_one_bio(struct btrfs_root
 
 			sums = kzalloc(btrfs_ordered_sum_size(root, bytes_left),
 				       GFP_NOFS);
-			BUG_ON(!sums);
+			BTRFS_UERROR(!sums);
 			sector_sum = sums->sums;
 			sums->len = bytes_left;
 			ordered = btrfs_lookup_ordered_extent(inode, offset);
fs/btrfs/file.c +1 -0
@@ -946,6 +946,7 @@ static ssize_t btrfs_file_write(struct f
 	file_update_time(file);
 
 	pages = kmalloc(nrptrs * sizeof(struct page *), GFP_KERNEL);
+	BTRFS_UERROR(!pages);
 
 	/* generic_write_checks can change our pos */
 	start_pos = pos;
fs/btrfs/inode.c +4 -0
@@ -273,6 +273,7 @@ static noinline int add_async_extent(str
 	struct async_extent *async_extent;
 
 	async_extent = kmalloc(sizeof(*async_extent), GFP_NOFS);
+	BTRFS_UERROR(!async_extent);
 	async_extent->start = start;
 	async_extent->ram_size = ram_size;
 	async_extent->compressed_size = compressed_size;
@@ -372,6 +373,7 @@ again:
 	    btrfs_test_opt(root, COMPRESS)) {
 		WARN_ON(pages);
 		pages = kzalloc(sizeof(struct page *) * nr_pages, GFP_NOFS);
+		BTRFS_UERROR(!pages);
 
 		ret = btrfs_zlib_compress_pages(inode->i_mapping, start,
 						total_compressed, pages,
@@ -885,6 +887,7 @@ static int cow_file_range_async(struct i
 			 1, 0, NULL, GFP_NOFS);
 	while (start < end) {
 		async_cow = kmalloc(sizeof(*async_cow), GFP_NOFS);
+		BTRFS_UERROR(!async_cow);
 		async_cow->inode = inode;
 		async_cow->root = root;
 		async_cow->locked_page = locked_page;
@@ -4447,6 +4450,7 @@ static noinline int uncompress_inline(st
 	inline_size = btrfs_file_extent_inline_item_len(leaf,
 					btrfs_item_nr(leaf, path->slots[0]));
 	tmp = kmalloc(inline_size, GFP_NOFS);
+	BTRFS_UERROR(!tmp);
 	ptr = btrfs_file_extent_inline_start(item);
 
 	read_extent_buffer(leaf, tmp, ptr, inline_size);
fs/btrfs/relocation.c +4 -4
@@ -850,7 +850,7 @@ static int __add_reloc_root(struct btrfs
 	struct reloc_control *rc = root->fs_info->reloc_ctl;
 
 	node = kmalloc(sizeof(*node), GFP_NOFS);
-	BUG_ON(!node);
+	BTRFS_UERROR(!node);
 
 	node->bytenr = root->node->start;
 	node->data = root;
@@ -925,7 +925,7 @@ int btrfs_init_reloc_root(struct btrfs_t
 		return 0;
 
 	root_item = kmalloc(sizeof(*root_item), GFP_NOFS);
-	BUG_ON(!root_item);
+	BTRFS_UERROR(!root_item);
 
 	root_key.objectid = BTRFS_TREE_RELOC_OBJECTID;
 	root_key.type = BTRFS_ROOT_ITEM_KEY;
@@ -1167,7 +1167,7 @@ static int replace_file_extents(struct b
 		if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) {
 			if (!ivec || ivec->nr == INODEVEC_SIZE) {
 				ivec = kmalloc(sizeof(*ivec), GFP_NOFS);
-				BUG_ON(!ivec);
+				BTRFS_UERROR(!ivec);
 				ivec->nr = 0;
 				list_add_tail(&ivec->list, inode_list);
 			}
@@ -1824,7 +1824,7 @@ static int merge_reloc_roots(struct relo
 		list_del_init(&root->root_list);
 
 		async = kmalloc(sizeof(*async), GFP_NOFS);
-		BUG_ON(!async);
+		BTRFS_UERROR(!async);
 		async->work.func = merge_func;
 		async->work.flags = 0;
 		async->rc = rc;
fs/btrfs/transaction.c +3 -1
@@ -56,7 +56,7 @@ static noinline int join_transaction(str
 	if (!cur_trans) {
 		cur_trans = kmem_cache_alloc(btrfs_transaction_cachep,
 					     GFP_NOFS);
-		BUG_ON(!cur_trans);
+		BTRFS_UERROR(!cur_trans);
 		root->fs_info->generation++;
 		cur_trans->num_writers = 1;
 		cur_trans->num_joined = 0;
@@ -170,6 +170,8 @@ static struct btrfs_trans_handle *start_
 		kmem_cache_alloc(btrfs_trans_handle_cachep, GFP_NOFS);
 	int ret;
 
+	BTRFS_UERROR(!h);
+
 	mutex_lock(&root->fs_info->trans_mutex);
 	if (!root->fs_info->log_root_recovering &&
 	    ((wait == 1 && !root->fs_info->open_ioctl_trans) || wait == 2))
fs/btrfs/tree-log.c +7 -2
@@ -333,7 +333,9 @@ static noinline int overwrite_item(struc
 			return 0;
 		}
 		dst_copy = kmalloc(item_size, GFP_NOFS);
+		BTRFS_UERROR(!dst_copy);
 		src_copy = kmalloc(item_size, GFP_NOFS);
+		BTRFS_UERROR(!src_copy);
 
 		read_extent_buffer(eb, src_copy, src_ptr, item_size);
 
@@ -661,6 +663,7 @@ static noinline int drop_one_dir_item(st
 	btrfs_dir_item_key_to_cpu(leaf, di, &location);
 	name_len = btrfs_dir_name_len(leaf, di);
 	name = kmalloc(name_len, GFP_NOFS);
+	BTRFS_UERROR(!name);
 	read_extent_buffer(leaf, name, (unsigned long)(di + 1), name_len);
 	btrfs_release_path(root, path);
 
@@ -816,7 +819,7 @@ again:
 
 	namelen = btrfs_inode_ref_name_len(eb, ref);
 	name = kmalloc(namelen, GFP_NOFS);
-	BUG_ON(!name);
+	BTRFS_UERROR(!name);
 
 	read_extent_buffer(eb, name, (unsigned long)(ref + 1), namelen);
 
@@ -861,7 +864,7 @@ conflict_again:
 			victim_name_len = btrfs_inode_ref_name_len(leaf,
 								   victim_ref);
 			victim_name = kmalloc(victim_name_len, GFP_NOFS);
-			BUG_ON(!victim_name);
+			BTRFS_UERROR(!victim_name);
 
 			read_extent_buffer(leaf, victim_name,
 					   (unsigned long)(victim_ref + 1),
@@ -1164,6 +1167,7 @@ static noinline int replay_one_name(stru
 
 	name_len = btrfs_dir_name_len(eb, di);
 	name = kmalloc(name_len, GFP_NOFS);
+	BTRFS_UERROR(!name);
 	log_type = btrfs_dir_type(eb, di);
 	read_extent_buffer(eb, name, (unsigned long)(di + 1),
 		   name_len);
@@ -2571,6 +2575,7 @@ static noinline int copy_items(struct bt
 
 	ins_data = kmalloc(nr * sizeof(struct btrfs_key) +
 			   nr * sizeof(u32), GFP_NOFS);
+	BTRFS_UERROR(!ins_data);
 	ins_sizes = (u32 *)ins_data;
 	ins_keys = (struct btrfs_key *)(ins_data + nr * sizeof(u32));
 
fs/btrfs/volumes.c +1 -1
@@ -2819,7 +2819,7 @@ int btrfs_rmap_block(struct btrfs_mappin
 		do_div(length, map->num_stripes);
 
 	buf = kzalloc(sizeof(u64) * map->num_stripes, GFP_NOFS);
-	BUG_ON(!buf);
+	BTRFS_UERROR(!buf);
 
 	for (i = 0; i < map->num_stripes; i++) {
 		if (devid && map->stripes[i].dev->devid != devid)
#7 lore raw

PATCH 7/10 btrfs: handle kmalloc call path failures

In reply to Jeff Mahoney

This patch adds checks to ensure that kmalloc or kmem_cache_alloc has succeeded before using the memory.

---
 fs/btrfs/compression.c |    3 ++-
 fs/btrfs/disk-io.c     |    5 ++++-
 fs/btrfs/extent-tree.c |   28 +++++++++++++++++++---------
 fs/btrfs/file.c        |    5 ++++-
 fs/btrfs/inode.c       |   17 +++++++++++------
 fs/btrfs/relocation.c  |   12 ++++++++----
 fs/btrfs/transaction.c |   10 +++++++---
 fs/btrfs/tree-log.c    |   25 +++++++++++++++----------
 fs/btrfs/volumes.c     |    5 ++++-
 9 files changed, 74 insertions(+), 36 deletions(-)
fs/btrfs/compression.c +2 -1
@@ -351,7 +351,8 @@ int btrfs_submit_compressed_write(struct
 
 	WARN_ON(start & ((u64)PAGE_CACHE_SIZE - 1));
 	cb = kmalloc(compressed_bio_size(root, compressed_len), GFP_NOFS);
-	BTRFS_UERROR(!cb);
+	if (!cb)
+		return -ENOMEM;
 	atomic_set(&cb->pending_bios, 0);
 	cb->errors = 0;
 	cb->inode = inode;
fs/btrfs/disk-io.c +4 -1
@@ -1958,7 +1958,10 @@ struct btrfs_root *open_ctree(struct sup
 
 		log_tree_root = kzalloc(sizeof(struct btrfs_root),
 						      GFP_NOFS);
-		BTRFS_UERROR(!log_tree_root);
+		if (!log_tree_root) {
+			err = -EIO;
+			goto fail_trans_kthread;
+		}
 
 		__setup_root(nodesize, leafsize, sectorsize, stripesize,
 			     log_tree_root, fs_info, BTRFS_TREE_LOG_OBJECTID);
fs/btrfs/extent-tree.c +19 -9
@@ -414,7 +414,8 @@ static int cache_block_group(struct btrf
 		return 0;
 
 	caching_ctl = kzalloc(sizeof(*caching_ctl), GFP_KERNEL);
-	BTRFS_UERROR(!caching_ctl);
+	if (!caching_ctl)
+		return -ENOMEM;
 
 	INIT_LIST_HEAD(&caching_ctl->list);
 	mutex_init(&caching_ctl->mutex);
@@ -4217,7 +4218,7 @@ have_block_group:
 			if (loop > LOOP_CACHING_NOWAIT ||
 			    atomic_read(&space_info->caching_threads) < 2) {
 				ret = cache_block_group(block_group);
-				BUG_ON(ret);
+				BTRFS_UERROR(ret);
 			}
 		}
 
@@ -4743,7 +4744,8 @@ int btrfs_alloc_logged_file_extent(struc
 	u64 num_bytes = ins->offset;
 
 	block_group = btrfs_lookup_block_group(root->fs_info, ins->objectid);
-	cache_block_group(block_group);
+	ret = cache_block_group(block_group);
+	BTRFS_UERROR(ret);
 	caching_ctl = get_caching_control(block_group);
 
 	if (!caching_ctl) {
@@ -5388,7 +5390,10 @@ int btrfs_drop_snapshot(struct btrfs_roo
 	BUG_ON(!path);
 
 	wc = kzalloc(sizeof(*wc), GFP_NOFS);
-	BTRFS_UERROR(!wc);
+	if (!wc) {
+		btrfs_free_path(path);
+		return -ENOMEM;
+	}
 
 	trans = btrfs_start_transaction(tree_root, 1);
 
@@ -5550,7 +5555,10 @@ int btrfs_drop_subtree(struct btrfs_tran
 	BUG_ON(!path);
 
 	wc = kzalloc(sizeof(*wc), GFP_NOFS);
-	BTRFS_UERROR(!wc);
+	if (!wc) {
+		btrfs_free_path(path);
+		return -ENOMEM;
+	}
 
 	btrfs_assert_tree_locked(parent);
 	parent_level = btrfs_header_level(parent);
@@ -6528,7 +6536,8 @@ static noinline int replace_extents_in_l
 	int ret;
 
 	new_extent = kmalloc(sizeof(*new_extent), GFP_NOFS);
-	BTRFS_UERROR(!new_extent)
+	if (!new_extent)
+		return -ENOMEM;
 
 	ref = btrfs_lookup_leaf_ref(root, leaf->start);
 	BUG_ON(!ref);
@@ -6732,7 +6741,8 @@ static noinline int init_reloc_tree(stru
 		return 0;
 
 	root_item = kmalloc(sizeof(*root_item), GFP_NOFS);
-	BTRFS_UERROR(!root_item);
+	if (!root_item)
+		return -ENOMEM;
 
 	ret = btrfs_copy_root(trans, root, root->commit_root,
 			      &eb, BTRFS_TREE_RELOC_OBJECTID);
@@ -6816,7 +6826,7 @@ static noinline int relocate_one_path(st
 
 	mutex_lock(&root->fs_info->tree_reloc_mutex);
 	ret = init_reloc_tree(trans, root);
-	BUG_ON(ret);
+	BTRFS_UERROR(ret);
 	reloc_root = root->reloc_root;
 
 	shared_level = ref_path->shared_level;
@@ -6849,7 +6859,7 @@ static noinline int relocate_one_path(st
 			eb = path->nodes[0];
 			ret = replace_extents_in_leaf(trans, reloc_root, eb,
 						      group, reloc_inode);
-			BUG_ON(ret);
+			BTRFS_UERROR(ret);
 		}
 		btrfs_release_path(reloc_root, path);
 	} else {
fs/btrfs/file.c +4 -1
@@ -946,7 +946,10 @@ static ssize_t btrfs_file_write(struct f
 	file_update_time(file);
 
 	pages = kmalloc(nrptrs * sizeof(struct page *), GFP_KERNEL);
-	BTRFS_UERROR(!pages);
+	if (!pages) {
+		err = -ENOMEM;
+		goto out;
+	}
 
 	/* generic_write_checks can change our pos */
 	start_pos = pos;
fs/btrfs/inode.c +11 -6
@@ -273,7 +273,8 @@ static noinline int add_async_extent(str
 	struct async_extent *async_extent;
 
 	async_extent = kmalloc(sizeof(*async_extent), GFP_NOFS);
-	BTRFS_UERROR(!async_extent);
+	if (!async_extent)
+		return -ENOMEM;
 	async_extent->start = start;
 	async_extent->ram_size = ram_size;
 	async_extent->compressed_size = compressed_size;
@@ -483,8 +484,9 @@ again:
 		 * allocation on disk for these compressed pages,
 		 * and will submit them to the elevator.
 		 */
-		add_async_extent(async_cow, start, num_bytes,
-				 total_compressed, pages, nr_pages_ret);
+		ret = add_async_extent(async_cow, start, num_bytes,
+				       total_compressed, pages, nr_pages_ret);
+		BTRFS_UERROR(ret);
 
 		if (start + num_bytes < end && start + num_bytes < actual_end) {
 			start += num_bytes;
@@ -506,7 +508,9 @@ cleanup_and_bail_uncompressed:
 			__set_page_dirty_nobuffers(locked_page);
 			/* unlocked later on in the async handlers */
 		}
-		add_async_extent(async_cow, start, end - start + 1, 0, NULL, 0);
+		ret = add_async_extent(async_cow, start,
+				       end - start + 1, 0, NULL, 0);
+		BTRFS_UERROR(ret);
 		*num_added += 1;
 	}
 
@@ -4450,7 +4454,8 @@ static noinline int uncompress_inline(st
 	inline_size = btrfs_file_extent_inline_item_len(leaf,
 					btrfs_item_nr(leaf, path->slots[0]));
 	tmp = kmalloc(inline_size, GFP_NOFS);
-	BTRFS_UERROR(!tmp);
+	if (!tmp)
+		return -ENOMEM;
 	ptr = btrfs_file_extent_inline_start(item);
 
 	read_extent_buffer(leaf, tmp, ptr, inline_size);
@@ -4648,7 +4653,7 @@ again:
 				ret = uncompress_inline(path, inode, page,
 							pg_offset,
 							extent_offset, item);
-				BUG_ON(ret);
+				BTRFS_UERROR(ret);
 			} else {
 				map = kmap(page);
 				read_extent_buffer(leaf, map + pg_offset, ptr,
fs/btrfs/relocation.c +8 -4
@@ -850,7 +850,8 @@ static int __add_reloc_root(struct btrfs
 	struct reloc_control *rc = root->fs_info->reloc_ctl;
 
 	node = kmalloc(sizeof(*node), GFP_NOFS);
-	BTRFS_UERROR(!node);
+	if (!node)
+		return -ENOMEM;
 
 	node->bytenr = root->node->start;
 	node->data = root;
@@ -925,7 +926,8 @@ int btrfs_init_reloc_root(struct btrfs_t
 		return 0;
 
 	root_item = kmalloc(sizeof(*root_item), GFP_NOFS);
-	BTRFS_UERROR(!root_item);
+	if (!root_item)
+		return -ENOMEM;
 
 	root_key.objectid = BTRFS_TREE_RELOC_OBJECTID;
 	root_key.type = BTRFS_ROOT_ITEM_KEY;
@@ -957,7 +959,8 @@ int btrfs_init_reloc_root(struct btrfs_t
 	BUG_ON(IS_ERR(reloc_root));
 	reloc_root->last_trans = trans->transid;
 
-	__add_reloc_root(reloc_root);
+	ret = __add_reloc_root(reloc_root);
+	BTRFS_UERROR(ret);
 	root->reloc_root = reloc_root;
 	return 0;
 }
@@ -3721,7 +3724,8 @@ int btrfs_recover_relocation(struct btrf
 				       reloc_root->root_key.offset);
 		BUG_ON(IS_ERR(fs_root));
 
-		__add_reloc_root(reloc_root);
+		err = __add_reloc_root(reloc_root);
+		BTRFS_UERROR(err);
 		fs_root->reloc_root = reloc_root;
 	}
 
fs/btrfs/transaction.c +7 -3
@@ -56,7 +56,8 @@ static noinline int join_transaction(str
 	if (!cur_trans) {
 		cur_trans = kmem_cache_alloc(btrfs_transaction_cachep,
 					     GFP_NOFS);
-		BTRFS_UERROR(!cur_trans);
+		if (!cur_trans)
+			return -ENOMEM;
 		root->fs_info->generation++;
 		cur_trans->num_writers = 1;
 		cur_trans->num_joined = 0;
@@ -102,6 +103,7 @@ static noinline int join_transaction(str
 static noinline int record_root_in_trans(struct btrfs_trans_handle *trans,
 					 struct btrfs_root *root)
 {
+	int ret;
 	if (root->ref_cows && root->last_trans < trans->transid) {
 		WARN_ON(root == root->fs_info->extent_root);
 		WARN_ON(root->commit_root != root->node);
@@ -110,7 +112,8 @@ static noinline int record_root_in_trans
 			   (unsigned long)root->root_key.objectid,
 			   BTRFS_ROOT_TRANS_TAG);
 		root->last_trans = trans->transid;
-		btrfs_init_reloc_root(trans, root);
+		ret = btrfs_init_reloc_root(trans, root);
+		BTRFS_UERROR(ret);
 	}
 	return 0;
 }
@@ -170,7 +173,8 @@ static struct btrfs_trans_handle *start_
 		kmem_cache_alloc(btrfs_trans_handle_cachep, GFP_NOFS);
 	int ret;
 
-	BTRFS_UERROR(!h);
+	if (!h)
+		return ERR_PTR(-ENOMEM);
 
 	mutex_lock(&root->fs_info->trans_mutex);
 	if (!root->fs_info->log_root_recovering &&
fs/btrfs/tree-log.c +15 -10
@@ -663,7 +663,8 @@ static noinline int drop_one_dir_item(st
 	btrfs_dir_item_key_to_cpu(leaf, di, &location);
 	name_len = btrfs_dir_name_len(leaf, di);
 	name = kmalloc(name_len, GFP_NOFS);
-	BTRFS_UERROR(!name);
+	if (!name)
+		return -ENOMEM;
 	read_extent_buffer(leaf, name, (unsigned long)(di + 1), name_len);
 	btrfs_release_path(root, path);
 
@@ -895,7 +896,7 @@ conflict_again:
 					 name, namelen, 0);
 	if (di && !IS_ERR(di)) {
 		ret = drop_one_dir_item(trans, root, path, dir, di);
-		BUG_ON(ret);
+		BTRFS_UERROR(ret);
 	}
 	btrfs_release_path(root, path);
 
@@ -905,7 +906,7 @@ conflict_again:
 				   name, namelen, 0);
 	if (di && !IS_ERR(di)) {
 		ret = drop_one_dir_item(trans, root, path, dir, di);
-		BUG_ON(ret);
+		BTRFS_UERROR(ret);
 	}
 	btrfs_release_path(root, path);
 
@@ -1167,7 +1168,10 @@ static noinline int replay_one_name(stru
 
 	name_len = btrfs_dir_name_len(eb, di);
 	name = kmalloc(name_len, GFP_NOFS);
-	BTRFS_UERROR(!name);
+	if (!name) {
+		iput(dir);
+		return -ENOMEM;
+	}
 	log_type = btrfs_dir_type(eb, di);
 	read_extent_buffer(eb, name, (unsigned long)(di + 1),
 		   name_len);
@@ -1217,7 +1221,7 @@ static noinline int replay_one_name(stru
 		goto out;
 
 	ret = drop_one_dir_item(trans, root, path, dir, dst_di);
-	BUG_ON(ret);
+	BTRFS_UERROR(ret);
 
 	if (key->type == BTRFS_DIR_INDEX_KEY)
 		goto insert;
@@ -1261,7 +1265,7 @@ static noinline int replay_one_dir_item(
 		di = (struct btrfs_dir_item *)ptr;
 		name_len = btrfs_dir_name_len(eb, di);
 		ret = replay_one_name(trans, root, path, eb, di, key);
-		BUG_ON(ret);
+		BTRFS_UERROR(ret);
 		ptr = (unsigned long)(di + 1);
 		ptr += name_len;
 	}
@@ -2575,7 +2579,8 @@ static noinline int copy_items(struct bt
 
 	ins_data = kmalloc(nr * sizeof(struct btrfs_key) +
 			   nr * sizeof(u32), GFP_NOFS);
-	BTRFS_UERROR(!ins_data);
+	if (!ins_data)
+		return -ENOMEM;
 	ins_sizes = (u32 *)ins_data;
 	ins_keys = (struct btrfs_key *)(ins_data + nr * sizeof(u32));
 
@@ -2766,7 +2771,7 @@ again:
 
 		ret = copy_items(trans, log, dst_path, src, ins_start_slot,
 				 ins_nr, inode_only);
-		BUG_ON(ret);
+		BTRFS_UERROR(ret);
 		ins_nr = 1;
 		ins_start_slot = path->slots[0];
 next_slot:
@@ -2782,7 +2787,7 @@ next_slot:
 			ret = copy_items(trans, log, dst_path, src,
 					 ins_start_slot,
 					 ins_nr, inode_only);
-			BUG_ON(ret);
+			BTRFS_UERROR(ret);
 			ins_nr = 0;
 		}
 		btrfs_release_path(root, path);
@@ -2800,7 +2805,7 @@ next_slot:
 		ret = copy_items(trans, log, dst_path, src,
 				 ins_start_slot,
 				 ins_nr, inode_only);
-		BUG_ON(ret);
+		BTRFS_UERROR(ret);
 		ins_nr = 0;
 	}
 	WARN_ON(ins_nr);
fs/btrfs/volumes.c +4 -1
@@ -2819,7 +2819,10 @@ int btrfs_rmap_block(struct btrfs_mappin
 		do_div(length, map->num_stripes);
 
 	buf = kzalloc(sizeof(u64) * map->num_stripes, GFP_NOFS);
-	BTRFS_UERROR(!buf);
+	if (!buf) {
+		free_extent_map(em);
+		return -ENOMEM;
+	}
 
 	for (i = 0; i < map->num_stripes; i++) {
 		if (devid && map->stripes[i].dev->devid != devid)
#8 lore raw

PATCH 8/10 btrfs: annotate btrfs_{start,join}_transaction failures

In reply to Jeff Mahoney

This patch locates and adds checks for failed transaction joins and starts. It doesn't do anything other than use BTRFS_UERROR to annotate them to keep the patch simple and obvious.

The next patch will add handling up the call chain where it's obvious.

---
 fs/btrfs/disk-io.c     |    3 +++
 fs/btrfs/extent-tree.c |   11 +++++++----
 fs/btrfs/file.c        |    1 +
 fs/btrfs/inode.c       |   23 ++++++++++++++++++++---
 fs/btrfs/ioctl.c       |   10 ++++++----
 fs/btrfs/relocation.c  |   12 +++++++++++-
 fs/btrfs/super.c       |    1 +
 fs/btrfs/transaction.c |    3 +++
 fs/btrfs/tree-log.c    |    1 +
 fs/btrfs/volumes.c     |    6 ++++--
 fs/btrfs/xattr.c       |    1 +
 11 files changed, 58 insertions(+), 14 deletions(-)
fs/btrfs/disk-io.c +3 -0
@@ -1529,6 +1529,7 @@ static int transaction_kthread(void *arg
 		}
 		mutex_unlock(&root->fs_info->trans_mutex);
 		trans = btrfs_start_transaction(root, 1);
+		BTRFS_UERROR(!trans);
 		ret = btrfs_commit_transaction(trans, root);
 
 sleep:
@@ -2395,10 +2396,12 @@ int btrfs_commit_super(struct btrfs_root
 	btrfs_clean_old_snapshots(root);
 	mutex_unlock(&root->fs_info->cleaner_mutex);
 	trans = btrfs_start_transaction(root, 1);
+	BTRFS_UERROR(!trans);
 	ret = btrfs_commit_transaction(trans, root);
 	BUG_ON(ret);
 	/* run commit again to drop the original snapshot */
 	trans = btrfs_start_transaction(root, 1);
+	BTRFS_UERROR(!trans);
 	btrfs_commit_transaction(trans, root);
 	ret = btrfs_write_and_wait_transaction(NULL, root);
 	BUG_ON(ret);
fs/btrfs/extent-tree.c +7 -4
@@ -3272,7 +3272,7 @@ alloc:
 			committed = 1;
 			trans = btrfs_join_transaction(root, 1);
 			if (!trans)
-				return -ENOMEM;
+				return PTR_ERR(trans);
 			ret = btrfs_commit_transaction(trans, root);
 			if (ret)
 				return ret;
@@ -5396,6 +5396,7 @@ int btrfs_drop_snapshot(struct btrfs_roo
 	}
 
 	trans = btrfs_start_transaction(tree_root, 1);
+	BTRFS_UERROR(!trans);
 
 	if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) {
 		level = btrfs_header_level(root->node);
@@ -5492,6 +5493,7 @@ int btrfs_drop_snapshot(struct btrfs_roo
 
 			btrfs_end_transaction(trans, tree_root);
 			trans = btrfs_start_transaction(tree_root, 1);
+			BTRFS_UERROR(!trans);
 		} else {
 			unsigned long update;
 			update = trans->delayed_ref_updates;
@@ -6654,7 +6656,7 @@ int btrfs_drop_dead_reloc_roots(struct b
 		BUG_ON(reloc_root->commit_root != NULL);
 		while (1) {
 			trans = btrfs_join_transaction(root, 1);
-			BUG_ON(!trans);
+			BTRFS_UERROR(!trans);
 
 			mutex_lock(&root->fs_info->drop_mutex);
 			ret = btrfs_drop_snapshot(trans, reloc_root);
@@ -6712,7 +6714,7 @@ int btrfs_cleanup_reloc_trees(struct btr
 
 	if (found) {
 		trans = btrfs_start_transaction(root, 1);
-		BUG_ON(!trans);
+		BTRFS_UERROR(!trans);
 		ret = btrfs_commit_transaction(trans, root);
 		BUG_ON(ret);
 	}
@@ -6957,7 +6959,7 @@ static noinline int relocate_one_extent(
 
 
 	trans = btrfs_start_transaction(extent_root, 1);
-	BUG_ON(!trans);
+	BTRFS_UERROR(!trans);
 
 	if (extent_key->objectid == 0) {
 		ret = del_extent_zero(trans, extent_root, path, extent_key);
@@ -7140,6 +7142,7 @@ static int __alloc_chunk_for_shrink(stru
 		spin_unlock(&shrink_block_group->lock);
 
 		trans = btrfs_start_transaction(root, 1);
+		BTRFS_UERROR(!trans);
 		spin_lock(&shrink_block_group->lock);
 
 		new_alloc_flags = update_block_group_flags(root,
fs/btrfs/file.c +1 -0
@@ -1083,6 +1083,7 @@ out_nolock:
 
 		if ((file->f_flags & O_SYNC) || IS_SYNC(inode)) {
 			trans = btrfs_start_transaction(root, 1);
+			BTRFS_UERROR(!trans);
 			ret = btrfs_log_dentry_safe(trans, root,
 						    file->f_dentry);
 			if (ret == 0) {
fs/btrfs/inode.c +20 -3
@@ -403,7 +403,7 @@ again:
 	}
 	if (start == 0) {
 		trans = btrfs_join_transaction(root, 1);
-		BUG_ON(!trans);
+		BTRFS_UERROR(!trans);
 		btrfs_set_trans_block_group(trans, inode);
 
 		/* lets try to make an inline extent */
@@ -550,6 +550,7 @@ static noinline int submit_compressed_ex
 		return 0;
 
 	trans = btrfs_join_transaction(root, 1);
+	BTRFS_UERROR(!trans);
 
 	while (!list_empty(&async_cow->extents)) {
 		async_extent = list_entry(async_cow->extents.next,
@@ -664,6 +665,7 @@ static noinline int submit_compressed_ex
 
 		BUG_ON(ret);
 		trans = btrfs_join_transaction(root, 1);
+		BTRFS_UERROR(!trans);
 		alloc_hint = ins.objectid + ins.offset;
 		kfree(async_extent);
 		cond_resched();
@@ -708,7 +710,7 @@ static noinline int cow_file_range(struc
 	int ret = 0;
 
 	trans = btrfs_join_transaction(root, 1);
-	BUG_ON(!trans);
+	BTRFS_UERROR(!trans);
 	btrfs_set_trans_block_group(trans, inode);
 
 	actual_end = min_t(u64, isize, end + 1);
@@ -990,7 +992,7 @@ static noinline int run_delalloc_nocow(s
 	path = btrfs_alloc_path();
 	BUG_ON(!path);
 	trans = btrfs_join_transaction(root, 1);
-	BUG_ON(!trans);
+	BTRFS_UERROR(!trans);
 
 	cow_start = (u64)-1;
 	cur_offset = start;
@@ -1709,6 +1711,7 @@ static int btrfs_finish_ordered_io(struc
 	}
 
 	trans = btrfs_join_transaction(root, 1);
+	BTRFS_UERROR(!trans);
 
 	if (!ordered_extent)
 		ordered_extent = btrfs_lookup_ordered_extent(inode, start);
@@ -2129,6 +2132,7 @@ void btrfs_orphan_cleanup(struct btrfs_r
 		 */
 		if (is_bad_inode(inode)) {
 			trans = btrfs_start_transaction(root, 1);
+			BTRFS_UERROR(!trans);
 			btrfs_orphan_del(trans, inode);
 			btrfs_end_transaction(trans, root);
 			iput(inode);
@@ -2482,6 +2486,7 @@ static int btrfs_unlink(struct inode *di
 	root = BTRFS_I(dir)->root;
 
 	trans = btrfs_start_transaction(root, 1);
+	BTRFS_UERROR(!trans);
 
 	btrfs_set_trans_block_group(trans, dir);
 
@@ -2577,6 +2582,7 @@ static int btrfs_rmdir(struct inode *dir
 		return -ENOTEMPTY;
 
 	trans = btrfs_start_transaction(root, 1);
+	BTRFS_UERROR(!trans);
 	btrfs_set_trans_block_group(trans, dir);
 
 	if (unlikely(inode->i_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
@@ -3152,6 +3158,7 @@ int btrfs_cont_expand(struct inode *inod
 	}
 
 	trans = btrfs_start_transaction(root, 1);
+	BTRFS_UERROR(!trans);
 	btrfs_set_trans_block_group(trans, inode);
 
 	cur_offset = hole_start;
@@ -3249,6 +3256,7 @@ void btrfs_delete_inode(struct inode *in
 
 	btrfs_i_size_write(inode, 0);
 	trans = btrfs_join_transaction(root, 1);
+	BTRFS_UERROR(!trans);
 
 	btrfs_set_trans_block_group(trans, inode);
 	ret = btrfs_truncate_inode_items(trans, root, inode, inode->i_size, 0);
@@ -3845,6 +3853,7 @@ int btrfs_write_inode(struct inode *inod
 
 	if (wait) {
 		trans = btrfs_join_transaction(root, 1);
+		BTRFS_UERROR(!trans);
 		btrfs_set_trans_block_group(trans, inode);
 		ret = btrfs_commit_transaction(trans, root);
 	}
@@ -3863,6 +3872,7 @@ void btrfs_dirty_inode(struct inode *ino
 	struct btrfs_trans_handle *trans;
 
 	trans = btrfs_join_transaction(root, 1);
+	BTRFS_UERROR(!trans);
 	btrfs_set_trans_block_group(trans, inode);
 	btrfs_update_inode(trans, root, inode);
 	btrfs_end_transaction(trans, root);
@@ -4156,6 +4166,7 @@ static int btrfs_mknod(struct inode *dir
 		return err;
 
 	trans = btrfs_start_transaction(root, 1);
+	err = -ENOMEM;
 	if (!trans)
 		goto fail;
 	btrfs_set_trans_block_group(trans, dir);
@@ -4226,6 +4237,7 @@ static int btrfs_create(struct inode *di
 		return err;
 
 	trans = btrfs_start_transaction(root, 1);
+	err = -ENOMEM;
 	if (!trans)
 		goto fail;
 	btrfs_set_trans_block_group(trans, dir);
@@ -4306,6 +4318,7 @@ static int btrfs_link(struct dentry *old
 		goto fail;
 
 	trans = btrfs_start_transaction(root, 1);
+	BTRFS_UERROR(!trans);
 
 	btrfs_set_trans_block_group(trans, dir);
 	atomic_inc(&inode->i_count);
@@ -4673,6 +4686,7 @@ again:
 				em = NULL;
 				btrfs_release_path(root, path);
 				trans = btrfs_join_transaction(root, 1);
+				BTRFS_UERROR(!trans);
 				goto again;
 			}
 			map = kmap(page);
@@ -5042,6 +5056,7 @@ static void btrfs_truncate(struct inode
 	btrfs_wait_ordered_range(inode, inode->i_size & (~mask), (u64)-1);
 
 	trans = btrfs_start_transaction(root, 1);
+	BTRFS_UERROR(!trans);
 
 	/*
 	 * setattr is responsible for setting the ordered_data_close flag,
@@ -5314,6 +5329,7 @@ static int btrfs_rename(struct inode *ol
 		down_read(&root->fs_info->subvol_sem);
 
 	trans = btrfs_start_transaction(root, 1);
+	BTRFS_UERROR(!trans);
 	btrfs_set_trans_block_group(trans, new_dir);
 
 	if (dest != root)
@@ -5494,6 +5510,7 @@ static int btrfs_symlink(struct inode *d
 		return err;
 
 	trans = btrfs_start_transaction(root, 1);
+	err = -ENOMEM;
 	if (!trans)
 		goto out_fail;
 	btrfs_set_trans_block_group(trans, dir);
fs/btrfs/ioctl.c +6 -4
@@ -199,7 +199,7 @@ static int btrfs_ioctl_setflags(struct f
 
 
 	trans = btrfs_join_transaction(root, 1);
-	BUG_ON(!trans);
+	BTRFS_UERROR(!trans);
 
 	ret = btrfs_update_inode(trans, root, inode);
 	BUG_ON(ret);
@@ -250,7 +250,7 @@ static noinline int create_subvol(struct
 		return ret;
 
 	trans = btrfs_start_transaction(root, 1);
-	BUG_ON(!trans);
+	BTRFS_UERROR(!trans);
 
 	ret = btrfs_find_free_objectid(trans, root->fs_info->tree_root,
 				       0, &objectid);
@@ -391,7 +391,7 @@ static int create_snapshot(struct btrfs_
 	pending_snapshot->name[namelen] = '\0';
 	pending_snapshot->dentry = dentry;
 	trans = btrfs_start_transaction(root, 1);
-	BUG_ON(!trans);
+	BTRFS_UERROR(!trans);
 	pending_snapshot->root = root;
 	list_add(&pending_snapshot->list,
 		 &trans->transaction->pending_snapshots);
@@ -639,6 +639,7 @@ static noinline int btrfs_ioctl_resize(s
 
 	if (new_size > old_size) {
 		trans = btrfs_start_transaction(root, 1);
+		BTRFS_UERROR(!trans);
 		ret = btrfs_grow_device(trans, device, new_size);
 		btrfs_commit_transaction(trans, root);
 	} else {
@@ -804,6 +805,7 @@ static noinline int btrfs_ioctl_snap_des
 		goto out_up_write;
 
 	trans = btrfs_start_transaction(root, 1);
+	BTRFS_UERROR(!trans);
 	ret = btrfs_unlink_subvol(trans, root, dir,
 				dest->root_key.objectid,
 				dentry->d_name.name,
@@ -1024,7 +1026,7 @@ static noinline long btrfs_ioctl_clone(s
 	}
 
 	trans = btrfs_start_transaction(root, 1);
-	BUG_ON(!trans);
+	BTRFS_UERROR(!trans);
 
 	/* punch hole in destination first */
 	btrfs_drop_extents(trans, root, inode, off, off + len,
fs/btrfs/relocation.c +11 -1
@@ -1638,6 +1638,7 @@ static noinline_for_stack int merge_relo
 
 	if (level == 0 && rc->stage == UPDATE_DATA_PTRS) {
 		trans = btrfs_start_transaction(root, 1);
+		BTRFS_UERROR(!trans);
 
 		leaf = path->nodes[0];
 		btrfs_item_key_to_cpu(leaf, &key, 0);
@@ -1664,6 +1665,7 @@ static noinline_for_stack int merge_relo
 		leaf = NULL;
 		replaced = 0;
 		trans = btrfs_start_transaction(root, 1);
+		BTRFS_UERROR(!trans);
 		max_level = level;
 
 		ret = walk_down_reloc_tree(reloc_root, path, &level);
@@ -1799,6 +1801,7 @@ static void merge_func(struct btrfs_work
 		merge_reloc_root(async->rc, root);
 
 		trans = btrfs_start_transaction(root, 1);
+		BTRFS_UERROR(!trans);
 		btrfs_update_reloc_root(trans, root);
 		btrfs_end_transaction(trans, root);
 	}
@@ -3291,10 +3294,12 @@ static noinline_for_stack int relocate_b
 	set_reloc_control(rc);
 
 	trans = btrfs_start_transaction(rc->extent_root, 1);
+	BTRFS_UERROR(!trans);
 	btrfs_commit_transaction(trans, rc->extent_root);
 
 	while (1) {
 		trans = btrfs_start_transaction(rc->extent_root, 1);
+		BTRFS_UERROR(!trans);
 
 		ret = find_next_extent(trans, rc, path);
 		if (ret < 0)
@@ -3405,6 +3410,7 @@ static noinline_for_stack int relocate_b
 
 	if (rc->extents_found > 0) {
 		trans = btrfs_start_transaction(rc->extent_root, 1);
+		BTRFS_UERROR(!trans);
 		btrfs_commit_transaction(trans, rc->extent_root);
 	}
 
@@ -3414,6 +3420,7 @@ static noinline_for_stack int relocate_b
 
 	/* get rid of pinned extents */
 	trans = btrfs_start_transaction(rc->extent_root, 1);
+	BTRFS_UERROR(!trans);
 	btrfs_commit_transaction(trans, rc->extent_root);
 
 	return err;
@@ -3469,7 +3476,7 @@ static struct inode *create_reloc_inode(
 		return ERR_CAST(root);
 
 	trans = btrfs_start_transaction(root, 1);
-	BUG_ON(!trans);
+	BTRFS_UERROR(!trans);
 
 	err = btrfs_find_free_objectid(trans, root, objectid, &objectid);
 	if (err)
@@ -3603,6 +3610,7 @@ static noinline_for_stack int mark_garba
 	int ret;
 
 	trans = btrfs_start_transaction(root->fs_info->tree_root, 1);
+	BTRFS_UERROR(!trans);
 
 	memset(&root->root_item.drop_progress, 0,
 		sizeof(root->root_item.drop_progress));
@@ -3730,6 +3738,7 @@ int btrfs_recover_relocation(struct btrf
 	}
 
 	trans = btrfs_start_transaction(rc->extent_root, 1);
+	BTRFS_UERROR(!trans);
 	btrfs_commit_transaction(trans, rc->extent_root);
 
 	merge_reloc_roots(rc);
@@ -3737,6 +3746,7 @@ int btrfs_recover_relocation(struct btrf
 	unset_reloc_control(rc);
 
 	trans = btrfs_start_transaction(rc->extent_root, 1);
+	BTRFS_UERROR(!trans);
 	btrfs_commit_transaction(trans, rc->extent_root);
 out:
 	if (rc) {
fs/btrfs/super.c +1 -0
@@ -417,6 +417,7 @@ int btrfs_sync_fs(struct super_block *sb
 	btrfs_wait_ordered_extents(root, 0);
 
 	trans = btrfs_start_transaction(root, 1);
+	BTRFS_UERROR(!trans);
 	ret = btrfs_commit_transaction(trans, root);
 	return ret;
 }
fs/btrfs/transaction.c +3 -0
@@ -630,6 +630,7 @@ int btrfs_defrag_root(struct btrfs_root
 	if (root->defrag_running)
 		return 0;
 	trans = btrfs_start_transaction(root, 1);
+	BTRFS_UERROR(!trans);
 	while (1) {
 		root->defrag_running = 1;
 		ret = btrfs_defrag_leaves(trans, root, cacheonly);
@@ -639,6 +640,7 @@ int btrfs_defrag_root(struct btrfs_root
 		cond_resched();
 
 		trans = btrfs_start_transaction(root, 1);
+		BTRFS_UERROR(!trans);
 		if (root->fs_info->closing || ret != -EAGAIN)
 			break;
 	}
@@ -812,6 +814,7 @@ static noinline int finish_pending_snaps
 	parent_inode = pending->dentry->d_parent->d_inode;
 	parent_root = BTRFS_I(parent_inode)->root;
 	trans = btrfs_join_transaction(parent_root, 1);
+	BTRFS_UERROR(!trans);
 
 	/*
 	 * insert the directory item
fs/btrfs/tree-log.c +1 -0
@@ -3024,6 +3024,7 @@ int btrfs_recover_log_trees(struct btrfs
 	BUG_ON(!path);
 
 	trans = btrfs_start_transaction(fs_info->tree_root, 1);
+	BTRFS_UERROR(!trans);
 
 	wc.trans = trans;
 	wc.pin = 1;
fs/btrfs/volumes.c +4 -2
@@ -1090,6 +1090,7 @@ static int btrfs_rm_dev_item(struct btrf
 		return -ENOMEM;
 
 	trans = btrfs_start_transaction(root, 1);
+	BTRFS_UERROR(!trans);
 	key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
 	key.type = BTRFS_DEV_ITEM_KEY;
 	key.offset = device->devid;
@@ -1480,6 +1481,7 @@ int btrfs_init_new_device(struct btrfs_r
 	}
 
 	trans = btrfs_start_transaction(root, 1);
+	BTRFS_UERROR(!trans);
 	lock_chunks(root);
 
 	device->barriers = 1;
@@ -1747,7 +1749,7 @@ static int btrfs_relocate_chunk(struct b
 	BUG_ON(ret);
 
 	trans = btrfs_start_transaction(root, 1);
-	BUG_ON(!trans);
+	BTRFS_UERROR(!trans);
 
 	lock_chunks(root);
 
@@ -1919,7 +1921,7 @@ int btrfs_balance(struct btrfs_root *dev
 		BUG_ON(ret);
 
 		trans = btrfs_start_transaction(dev_root, 1);
-		BUG_ON(!trans);
+		BTRFS_UERROR(!trans);
 
 		ret = btrfs_grow_device(trans, device, old_size);
 		BUG_ON(ret);
fs/btrfs/xattr.c +1 -0
@@ -99,6 +99,7 @@ int __btrfs_setxattr(struct inode *inode
 		return -ENOMEM;
 
 	trans = btrfs_join_transaction(root, 1);
+	BTRFS_UERROR(!trans);
 	btrfs_set_trans_block_group(trans, inode);
 
 	/* first lets see if we already have this xattr */
#9 lore raw

PATCH 9/10 btrfs: handle btrfs_{start,join}_transaction call path failures

In reply to Jeff Mahoney

This patch changes start_transaction() to return an ERR_PTR instead of NULL. Things like I/O errors and allocation failures can be handled differently.

Many calls that were previously handled by BTRFS_UERROR are changed to actually pass the error up the tree where appropriate.

In cases where the handling isn't small or obvious, the BTRFS_UERROR call is kept for handling in small patches specific to that site.

Eventually it may be preferable to allow passing of gfp_t to btrfs_start_transaction (or a one-off) so that handling can be done at allocation time instead of failing all the way up the call path.

---
 fs/btrfs/compression.c |   10 ++-
 fs/btrfs/disk-io.c     |   37 +++++++++-----
 fs/btrfs/extent-tree.c |   43 ++++++++++------
 fs/btrfs/extent_io.c   |   60 ++++++++++++++--------
 fs/btrfs/file.c        |   16 ++++--
 fs/btrfs/inode.c       |  128 ++++++++++++++++++++++++++++++-------------------
 fs/btrfs/ioctl.c       |   73 +++++++++++++++++++--------
 fs/btrfs/relocation.c  |   96 ++++++++++++++++++++++++++++--------
 fs/btrfs/super.c       |   12 +++-
 fs/btrfs/transaction.c |   34 +++++++++----
 fs/btrfs/tree-log.c    |   14 +++--
 fs/btrfs/volumes.c     |   44 ++++++++++------
 fs/btrfs/xattr.c       |    3 -
 13 files changed, 382 insertions(+), 188 deletions(-)
fs/btrfs/compression.c +6 -4
@@ -280,6 +280,7 @@ static void end_compressed_bio_write(str
 	struct inode *inode;
 	struct page *page;
 	unsigned long index;
+	int ret;
 
 	if (err)
 		cb->errors = 1;
@@ -296,10 +297,11 @@ static void end_compressed_bio_write(str
 	inode = cb->inode;
 	tree = &BTRFS_I(inode)->io_tree;
 	cb->compressed_pages[0]->mapping = cb->inode->i_mapping;
-	tree->ops->writepage_end_io_hook(cb->compressed_pages[0],
-					 cb->start,
-					 cb->start + cb->len - 1,
-					 NULL, 1);
+	ret = tree->ops->writepage_end_io_hook(cb->compressed_pages[0],
+					       cb->start,
+					       cb->start + cb->len - 1,
+					       NULL, 1);
+	BTRFS_UERROR(ret);
 	cb->compressed_pages[0]->mapping = NULL;
 
 	end_compressed_writeback(inode, cb->start, cb->len);
fs/btrfs/disk-io.c +26 -11
@@ -1468,6 +1468,7 @@ static void end_workqueue_fn(struct btrf
 static int cleaner_kthread(void *arg)
 {
 	struct btrfs_root *root = arg;
+	int ret;
 
 	do {
 		smp_mb();
@@ -1478,7 +1479,8 @@ static int cleaner_kthread(void *arg)
 
 		if (!(root->fs_info->sb->s_flags & MS_RDONLY) &&
 		    mutex_trylock(&root->fs_info->cleaner_mutex)) {
-			btrfs_clean_old_snapshots(root);
+			ret = btrfs_clean_old_snapshots(root);
+			BTRFS_UERROR(ret);
 			mutex_unlock(&root->fs_info->cleaner_mutex);
 		}
 
@@ -1529,9 +1531,11 @@ static int transaction_kthread(void *arg
 		}
 		mutex_unlock(&root->fs_info->trans_mutex);
 		trans = btrfs_start_transaction(root, 1);
-		BTRFS_UERROR(!trans);
-		ret = btrfs_commit_transaction(trans, root);
+		if (!IS_ERR(trans))
+			ret = btrfs_commit_transaction(trans, root);
 
+		if (IS_ERR(trans) || ret)
+			delay = HZ * 5;
 sleep:
 		wake_up_process(root->fs_info->cleaner_kthread);
 		mutex_unlock(&root->fs_info->transaction_kthread_mutex);
@@ -1971,11 +1975,15 @@ struct btrfs_root *open_ctree(struct sup
 						      blocksize,
 						      generation + 1);
 		ret = btrfs_recover_log_trees(log_tree_root);
-		BUG_ON(ret);
+		if (ret) {
+			/* log_tree_root is cleaned up by
+			 * btrfs_recover_log_trees */
+			goto fail_trans_thread;
+		}
 
 		if (sb->s_flags & MS_RDONLY) {
 			ret =  btrfs_commit_super(tree_root);
-			BUG_ON(ret);
+			BTRFS_UERROR(ret);
 		}
 	}
 
@@ -1984,7 +1992,7 @@ struct btrfs_root *open_ctree(struct sup
 
 	if (!(sb->s_flags & MS_RDONLY)) {
 		ret = btrfs_recover_relocation(tree_root);
-		BUG_ON(ret);
+		BTRFS_UERROR(ret);
 	}
 
 	location.objectid = BTRFS_FS_TREE_OBJECTID;
@@ -2393,16 +2401,23 @@ int btrfs_commit_super(struct btrfs_root
 	int ret;
 
 	mutex_lock(&root->fs_info->cleaner_mutex);
-	btrfs_clean_old_snapshots(root);
+	ret = btrfs_clean_old_snapshots(root);
 	mutex_unlock(&root->fs_info->cleaner_mutex);
+	if (ret)
+		return ret;
 	trans = btrfs_start_transaction(root, 1);
-	BTRFS_UERROR(!trans);
+	if (IS_ERR(trans))
+		return PTR_ERR(trans);
 	ret = btrfs_commit_transaction(trans, root);
-	BUG_ON(ret);
+	if (ret)
+		return ret;
 	/* run commit again to drop the original snapshot */
 	trans = btrfs_start_transaction(root, 1);
-	BTRFS_UERROR(!trans);
-	btrfs_commit_transaction(trans, root);
+	if (IS_ERR(trans))
+		return PTR_ERR(trans);
+	ret = btrfs_commit_transaction(trans, root);
+	if (ret)
+		return ret;
 	ret = btrfs_write_and_wait_transaction(NULL, root);
 	BUG_ON(ret);
 
fs/btrfs/extent-tree.c +27 -16

128 lines not on this page · raw message

fs/btrfs/extent_io.c +37 -23

108 lines not on this page · raw message

fs/btrfs/file.c +11 -5
@@ -1083,7 +1083,10 @@ out_nolock:
 
 		if ((file->f_flags & O_SYNC) || IS_SYNC(inode)) {
 			trans = btrfs_start_transaction(root, 1);
-			BTRFS_UERROR(!trans);
+			if (IS_ERR(trans)) {
+				err = PTR_ERR(trans);
+				goto out;
+			}
 			ret = btrfs_log_dentry_safe(trans, root,
 						    file->f_dentry);
 			if (ret == 0) {
@@ -1091,9 +1094,11 @@ out_nolock:
 				if (ret == 0)
 					btrfs_end_transaction(trans, root);
 				else
-					btrfs_commit_transaction(trans, root);
+					err = btrfs_commit_transaction(trans,
+								       root);
+
 			} else if (ret != BTRFS_NO_LOG_SYNC) {
-				btrfs_commit_transaction(trans, root);
+				err = btrfs_commit_transaction(trans, root);
 			} else {
 				btrfs_end_transaction(trans, root);
 			}
@@ -1104,6 +1109,7 @@ out_nolock:
 			     (start_pos + num_written - 1) >> PAGE_CACHE_SHIFT);
 		}
 	}
+out:
 	current->backing_dev_info = NULL;
 	return num_written ? num_written : err;
 }
@@ -1180,8 +1186,8 @@ int btrfs_sync_file(struct file *file, s
 		btrfs_ioctl_trans_end(file);
 
 	trans = btrfs_start_transaction(root, 1);
-	if (!trans) {
-		ret = -ENOMEM;
+	if (IS_ERR(trans)) {
+		ret = PTR_ERR(trans);
 		goto out;
 	}
 
fs/btrfs/inode.c +80 -48

338 lines not on this page · raw message

fs/btrfs/ioctl.c +50 -23

183 lines not on this page · raw message

fs/btrfs/relocation.c +74 -22

259 lines not on this page · raw message

fs/btrfs/super.c +8 -4
@@ -417,8 +417,11 @@ int btrfs_sync_fs(struct super_block *sb
 	btrfs_wait_ordered_extents(root, 0);
 
 	trans = btrfs_start_transaction(root, 1);
-	BTRFS_UERROR(!trans);
-	ret = btrfs_commit_transaction(trans, root);
+	if (!IS_ERR(trans))
+		ret = btrfs_commit_transaction(trans, root);
+	else
+		ret = PTR_ERR(trans);
+
 	return ret;
 }
 
@@ -601,7 +604,8 @@ static int btrfs_remount(struct super_bl
 
 		/* recover relocation */
 		ret = btrfs_recover_relocation(root);
-		WARN_ON(ret);
+		if (ret)
+			return ret;
 
 		ret = btrfs_cleanup_fs_roots(root->fs_info);
 		WARN_ON(ret);
@@ -609,7 +613,7 @@ static int btrfs_remount(struct super_bl
 		sb->s_flags &= ~MS_RDONLY;
 	}
 
-	return 0;
+	return ret;
 }
 
 static int btrfs_statfs(struct dentry *dentry, struct kstatfs *buf)
fs/btrfs/transaction.c +24 -10
@@ -181,7 +181,10 @@ static struct btrfs_trans_handle *start_
 	    ((wait == 1 && !root->fs_info->open_ioctl_trans) || wait == 2))
 		wait_current_trans(root);
 	ret = join_transaction(root);
-	BUG_ON(ret);
+	if (ret) {
+		h = ERR_PTR(ret);
+		goto out;
+	}
 
 	h->transid = root->fs_info->running_transaction->transid;
 	h->transaction = root->fs_info->running_transaction;
@@ -197,6 +200,7 @@ static struct btrfs_trans_handle *start_
 
 	root->fs_info->running_transaction->use_count++;
 	record_root_in_trans(h, root);
+out:
 	mutex_unlock(&root->fs_info->trans_mutex);
 	return h;
 }
@@ -629,8 +633,11 @@ int btrfs_defrag_root(struct btrfs_root
 	smp_mb();
 	if (root->defrag_running)
 		return 0;
+
 	trans = btrfs_start_transaction(root, 1);
-	BTRFS_UERROR(!trans);
+	if (IS_ERR(trans))
+		return PTR_ERR(trans);
+
 	while (1) {
 		root->defrag_running = 1;
 		ret = btrfs_defrag_leaves(trans, root, cacheonly);
@@ -640,14 +647,16 @@ int btrfs_defrag_root(struct btrfs_root
 		cond_resched();
 
 		trans = btrfs_start_transaction(root, 1);
-		BTRFS_UERROR(!trans);
+		if (IS_ERR(trans))
+			ret = PTR_ERR(trans);
 		if (root->fs_info->closing || ret != -EAGAIN)
 			break;
 	}
 	root->defrag_running = 0;
 	smp_mb();
-	btrfs_end_transaction(trans, root);
-	return 0;
+	if (!IS_ERR(trans))
+		btrfs_end_transaction(trans, root);
+	return ret;
 }
 
 #if 0
@@ -814,7 +823,8 @@ static noinline int finish_pending_snaps
 	parent_inode = pending->dentry->d_parent->d_inode;
 	parent_root = BTRFS_I(parent_inode)->root;
 	trans = btrfs_join_transaction(parent_root, 1);
-	BTRFS_UERROR(!trans);
+	if (IS_ERR(trans))
+		return PTR_ERR(trans);
 
 	/*
 	 * insert the directory item
@@ -842,6 +852,7 @@ static noinline int finish_pending_snaps
 	BUG_ON(ret);
 
 	inode = btrfs_lookup_dentry(parent_inode, pending->dentry);
+	BTRFS_UERROR(IS_ERR(inode));
 	d_instantiate(pending->dentry, inode);
 fail:
 	btrfs_end_transaction(trans, fs_info->fs_root);
@@ -876,7 +887,7 @@ static noinline int finish_pending_snaps
 		pending = list_entry(head->next,
 				     struct btrfs_pending_snapshot, list);
 		ret = finish_pending_snapshot(fs_info, pending);
-		BUG_ON(ret);
+		BTRFS_UERROR(ret);
 		list_del(&pending->list);
 		kfree(pending->name);
 		kfree(pending);
@@ -1099,7 +1110,8 @@ int btrfs_commit_transaction(struct btrf
 	btrfs_finish_extent_commit(trans, root);
 
 	/* do the directory inserts of any pending snapshot creations */
-	finish_pending_snapshots(trans, root->fs_info);
+	ret = finish_pending_snapshots(trans, root->fs_info);
+	BTRFS_UERROR(ret);
 
 	mutex_lock(&root->fs_info->trans_mutex);
 
@@ -1128,6 +1140,7 @@ int btrfs_clean_old_snapshots(struct btr
 {
 	LIST_HEAD(list);
 	struct btrfs_fs_info *fs_info = root->fs_info;
+	int ret = 0;
 
 	mutex_lock(&fs_info->trans_mutex);
 	list_splice_init(&fs_info->dead_roots, &list);
@@ -1139,9 +1152,10 @@ int btrfs_clean_old_snapshots(struct btr
 
 		if (btrfs_header_backref_rev(root->node) <
 		    BTRFS_MIXED_BACKREF_REV)
-			btrfs_drop_snapshot(root, 0);
+			ret = btrfs_drop_snapshot(root, 0);
 		else
-			btrfs_drop_snapshot(root, 1);
+			ret = btrfs_drop_snapshot(root, 1);
+		BTRFS_UERROR(ret);
 	}
 	return 0;
 }
fs/btrfs/tree-log.c +9 -5
@@ -3024,7 +3024,10 @@ int btrfs_recover_log_trees(struct btrfs
 	BUG_ON(!path);
 
 	trans = btrfs_start_transaction(fs_info->tree_root, 1);
-	BTRFS_UERROR(!trans);
+	if (IS_ERR(trans)) {
+		ret = PTR_ERR(trans);
+		goto fail;
+	}
 
 	wc.trans = trans;
 	wc.pin = 1;
@@ -3053,8 +3056,7 @@ again:
 
 		log = btrfs_read_fs_root_no_radix(log_root_tree,
 						  &found_key);
-		BUG_ON(IS_ERR(log));
-
+		BTRFS_UERROR(IS_ERR(log));
 
 		tmp_key.objectid = found_key.offset;
 		tmp_key.type = BTRFS_ROOT_ITEM_KEY;
@@ -3098,6 +3100,7 @@ again:
 		goto again;
 	}
 
+fail:
 	btrfs_free_path(path);
 
 	free_extent_buffer(log_root_tree->node);
@@ -3105,10 +3108,11 @@ again:
 	fs_info->log_root_recovering = 0;
 
 	/* step 4: commit the transaction, which also unpins the blocks */
-	btrfs_commit_transaction(trans, fs_info->tree_root);
+	if (!ret)
+		ret = btrfs_commit_transaction(trans, fs_info->tree_root);
 
 	kfree(log_root_tree);
-	return 0;
+	return ret;
 }
 
 /*
fs/btrfs/volumes.c +28 -16

120 lines not on this page · raw message

fs/btrfs/xattr.c +2 -1
@@ -99,7 +99,8 @@ int __btrfs_setxattr(struct inode *inode
 		return -ENOMEM;
 
 	trans = btrfs_join_transaction(root, 1);
-	BTRFS_UERROR(!trans);
+	if (IS_ERR(trans))
+		return PTR_ERR(trans);
 	btrfs_set_trans_block_group(trans, inode);
 
 	/* first lets see if we already have this xattr */
#10 lore raw

PATCH 10/10 btrfs: annotate btrfs_alloc_path failures

In reply to Jeff Mahoney

This patch adds checks for btrfs_alloc_path failures and annotates them with BTRFS_UERROR.

---
 fs/btrfs/ctree.c       |    2 +-
 fs/btrfs/dir-item.c    |    1 +
 fs/btrfs/disk-io.c     |    2 +-
 fs/btrfs/export.c      |    1 +
 fs/btrfs/extent-tree.c |   14 +++++++-------
 fs/btrfs/file-item.c   |    8 +++++---
 fs/btrfs/file.c        |    2 +-
 fs/btrfs/inode-map.c   |    2 +-
 fs/btrfs/inode.c       |   19 ++++++++++---------
 fs/btrfs/root-tree.c   |    8 ++++----
 fs/btrfs/tree-log.c    |   11 ++++++++---
 fs/btrfs/volumes.c     |    4 ++--
 12 files changed, 42 insertions(+), 32 deletions(-)
fs/btrfs/ctree.c +1 -1
@@ -3635,7 +3635,7 @@ int btrfs_insert_item(struct btrfs_trans
 	unsigned long ptr;
 
 	path = btrfs_alloc_path();
-	BUG_ON(!path);
+	BTRFS_UERROR(!path);
 	ret = btrfs_insert_empty_item(trans, root, path, cpu_key, data_size);
 	if (!ret) {
 		leaf = path->nodes[0];
fs/btrfs/dir-item.c +1 -0
@@ -147,6 +147,7 @@ int btrfs_insert_dir_item(struct btrfs_t
 	key.offset = btrfs_name_hash(name, name_len);
 
 	path = btrfs_alloc_path();
+	BTRFS_UERROR(!path);
 	path->leave_spinning = 1;
 
 	data_size = sizeof(*dir_item) + name_len;
fs/btrfs/disk-io.c +1 -1
@@ -1123,7 +1123,7 @@ struct btrfs_root *btrfs_read_fs_root_no
 		     root, fs_info, location->objectid);
 
 	path = btrfs_alloc_path();
-	BUG_ON(!path);
+	BTRFS_UERROR(!path);
 	ret = btrfs_search_slot(NULL, tree_root, location, path, 0, 0);
 	if (ret == 0) {
 		l = path->nodes[0];
fs/btrfs/export.c +1 -0
@@ -176,6 +176,7 @@ static struct dentry *btrfs_get_parent(s
 	int ret;
 
 	path = btrfs_alloc_path();
+	BTRFS_UERROR(!path);
 
 	if (dir->i_ino == BTRFS_FIRST_FREE_OBJECTID) {
 		key.objectid = root->root_key.objectid;
fs/btrfs/extent-tree.c +7 -7
@@ -583,7 +583,7 @@ int btrfs_lookup_extent(struct btrfs_roo
 	struct btrfs_path *path;
 
 	path = btrfs_alloc_path();
-	BUG_ON(!path);
+	BTRFS_UERROR(!path);
 	key.objectid = start;
 	key.offset = len;
 	btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
@@ -4607,7 +4607,7 @@ static int alloc_reserved_file_extent(st
 	size = sizeof(*extent_item) + btrfs_extent_inline_ref_size(type);
 
 	path = btrfs_alloc_path();
-	BUG_ON(!path);
+	BTRFS_UERROR(!path);
 
 	path->leave_spinning = 1;
 	ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path,
@@ -4668,7 +4668,7 @@ static int alloc_reserved_tree_block(str
 	u32 size = sizeof(*extent_item) + sizeof(*block_info) + sizeof(*iref);
 
 	path = btrfs_alloc_path();
-	BUG_ON(!path);
+	BTRFS_UERROR(!path);
 
 	path->leave_spinning = 1;
 	ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path,
@@ -5388,7 +5388,7 @@ int btrfs_drop_snapshot(struct btrfs_roo
 	int level;
 
 	path = btrfs_alloc_path();
-	BUG_ON(!path);
+	BTRFS_UERROR(!path);
 
 	wc = kzalloc(sizeof(*wc), GFP_NOFS);
 	if (!wc) {
@@ -5559,7 +5559,7 @@ int btrfs_drop_subtree(struct btrfs_tran
 	BUG_ON(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID);
 
 	path = btrfs_alloc_path();
-	BUG_ON(!path);
+	BTRFS_UERROR(!path);
 
 	wc = kzalloc(sizeof(*wc), GFP_NOFS);
 	if (!wc) {
@@ -6021,7 +6021,7 @@ static noinline int get_new_locations(st
 	}
 
 	path = btrfs_alloc_path();
-	BUG_ON(!path);
+	BTRFS_UERROR(!path);
 
 	cur_pos = extent_key->objectid - offset;
 	last_byte = extent_key->objectid + extent_key->offset;
@@ -7578,7 +7578,7 @@ int btrfs_remove_block_group(struct btrf
 	spin_unlock(&cluster->refill_lock);
 
 	path = btrfs_alloc_path();
-	BUG_ON(!path);
+	BTRFS_UERROR(!path);
 
 	spin_lock(&root->fs_info->block_group_cache_lock);
 	rb_erase(&block_group->cache_node,
fs/btrfs/file-item.c +5 -3
@@ -47,7 +47,7 @@ int btrfs_insert_file_extent(struct btrf
 	struct extent_buffer *leaf;
 
 	path = btrfs_alloc_path();
-	BUG_ON(!path);
+	BTRFS_UERROR(!path);
 	file_key.objectid = objectid;
 	file_key.offset = pos;
 	btrfs_set_key_type(&file_key, BTRFS_EXTENT_DATA_KEY);
@@ -167,6 +167,7 @@ int btrfs_lookup_bio_sums(struct btrfs_r
 	struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
 
 	path = btrfs_alloc_path();
+	BTRFS_UERROR(!path);
 	if (bio->bi_size > PAGE_CACHE_SIZE * 8)
 		path->reada = 2;
 
@@ -260,7 +261,7 @@ int btrfs_lookup_csums_range(struct btrf
 	u16 csum_size = btrfs_super_csum_size(&root->fs_info->super_copy);
 
 	path = btrfs_alloc_path();
-	BUG_ON(!path);
+	BTRFS_UERROR(!path);
 
 	key.objectid = BTRFS_EXTENT_CSUM_OBJECTID;
 	key.offset = start;
@@ -518,6 +519,7 @@ int btrfs_del_csums(struct btrfs_trans_h
 	root = root->fs_info->csum_root;
 
 	path = btrfs_alloc_path();
+	BTRFS_UERROR(!path);
 
 	while (1) {
 		key.objectid = BTRFS_EXTENT_CSUM_OBJECTID;
@@ -639,7 +641,7 @@ int btrfs_csum_file_blocks(struct btrfs_
 		btrfs_super_csum_size(&root->fs_info->super_copy);
 
 	path = btrfs_alloc_path();
-	BUG_ON(!path);
+	BTRFS_UERROR(!path);
 	sector_sum = sums->sums;
 again:
 	next_offset = (u64)-1;
fs/btrfs/file.c +1 -1
@@ -642,7 +642,7 @@ int btrfs_mark_extent_written(struct btr
 	btrfs_drop_extent_cache(inode, start, end - 1, 0);
 
 	path = btrfs_alloc_path();
-	BUG_ON(!path);
+	BTRFS_UERROR(!path);
 again:
 	key.objectid = inode->i_ino;
 	key.type = BTRFS_EXTENT_DATA_KEY;
fs/btrfs/inode-map.c +1 -1
@@ -30,7 +30,7 @@ int btrfs_find_highest_inode(struct btrf
 	int slot;
 
 	path = btrfs_alloc_path();
-	BUG_ON(!path);
+	BTRFS_UERROR(!path);
 
 	search_key.objectid = BTRFS_LAST_FREE_OBJECTID;
 	search_key.type = -1;
fs/btrfs/inode.c +10 -9
@@ -997,7 +997,7 @@ static noinline int run_delalloc_nocow(s
 	int check_prev = 1;
 
 	path = btrfs_alloc_path();
-	BUG_ON(!path);
+	BTRFS_UERROR(!path);
 	trans = btrfs_join_transaction(root, 1);
 	if (IS_ERR(trans))
 		return PTR_ERR(trans);
@@ -1597,7 +1597,7 @@ static int insert_reserved_file_extent(s
 	int ret;
 
 	path = btrfs_alloc_path();
-	BUG_ON(!path);
+	BTRFS_UERROR(!path);
 
 	path->leave_spinning = 1;
 
@@ -2236,7 +2236,7 @@ static void btrfs_read_locked_inode(stru
 	int ret;
 
 	path = btrfs_alloc_path();
-	BUG_ON(!path);
+	BTRFS_UERROR(!path);
 	memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
 
 	ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
@@ -2374,7 +2374,7 @@ noinline int btrfs_update_inode(struct b
 	int ret;
 
 	path = btrfs_alloc_path();
-	BUG_ON(!path);
+	BTRFS_UERROR(!path);
 	path->leave_spinning = 1;
 	ret = btrfs_lookup_inode(trans, root, path,
 				 &BTRFS_I(inode)->location, 1);
@@ -2834,7 +2834,7 @@ noinline int btrfs_truncate_inode_items(
 	if (root->ref_cows)
 		btrfs_drop_extent_cache(inode, new_size & (~mask), (u64)-1, 0);
 	path = btrfs_alloc_path();
-	BUG_ON(!path);
+	BTRFS_UERROR(!path);
 	path->reada = -1;
 
 	/* FIXME, add redo link to tree so we don't leak on crash */
@@ -3313,7 +3313,7 @@ static int btrfs_inode_by_name(struct in
 	int ret = 0;
 
 	path = btrfs_alloc_path();
-	BUG_ON(!path);
+	BTRFS_UERROR(!path);
 
 	di = btrfs_lookup_dir_item(NULL, root, path, dir->i_ino, name,
 				    namelen, 0);
@@ -3756,6 +3756,7 @@ static int btrfs_real_readdir(struct fil
 		filp->f_pos = 2;
 	}
 	path = btrfs_alloc_path();
+	BTRFS_UERROR(!path);
 	path->reada = 2;
 
 	btrfs_set_key_type(&key, key_type);
@@ -3992,7 +3993,7 @@ static struct inode *btrfs_new_inode(str
 	int owner;
 
 	path = btrfs_alloc_path();
-	BUG_ON(!path);
+	BTRFS_UERROR(!path);
 
 	inode = new_inode(root->fs_info->sb);
 	if (!inode)
@@ -4572,7 +4573,7 @@ again:
 
 	if (!path) {
 		path = btrfs_alloc_path();
-		BUG_ON(!path);
+		BTRFS_UERROR(!path);
 	}
 
 	ret = btrfs_lookup_file_extent(trans, root, path,
@@ -5586,7 +5587,7 @@ static int btrfs_symlink(struct inode *d
 		goto out_unlock;
 
 	path = btrfs_alloc_path();
-	BUG_ON(!path);
+	BTRFS_UERROR(!path);
 	key.objectid = inode->i_ino;
 	key.offset = 0;
 	btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
fs/btrfs/root-tree.c +4 -4
@@ -40,7 +40,7 @@ int btrfs_search_root(struct btrfs_root
 	search_key.offset = (u64)-1;
 
 	path = btrfs_alloc_path();
-	BUG_ON(!path);
+	BTRFS_UERROR(!path);
 again:
 	ret = btrfs_search_slot(NULL, root, &search_key, path, 0, 0);
 	if (ret < 0)
@@ -88,7 +88,7 @@ int btrfs_find_last_root(struct btrfs_ro
 	search_key.offset = (u64)-1;
 
 	path = btrfs_alloc_path();
-	BUG_ON(!path);
+	BTRFS_UERROR(!path);
 	ret = btrfs_search_slot(NULL, root, &search_key, path, 0, 0);
 	if (ret < 0)
 		goto out;
@@ -140,7 +140,7 @@ int btrfs_update_root(struct btrfs_trans
 	unsigned long ptr;
 
 	path = btrfs_alloc_path();
-	BUG_ON(!path);
+	BTRFS_UERROR(!path);
 	ret = btrfs_search_slot(trans, root, key, path, 0, 1);
 	if (ret < 0)
 		goto out;
@@ -319,7 +319,7 @@ int btrfs_del_root(struct btrfs_trans_ha
 	struct extent_buffer *leaf;
 
 	path = btrfs_alloc_path();
-	BUG_ON(!path);
+	BTRFS_UERROR(!path);
 	ret = btrfs_search_slot(trans, root, key, path, -1, 1);
 	if (ret < 0)
 		goto out;
fs/btrfs/tree-log.c +8 -3
@@ -744,6 +744,7 @@ static noinline int backref_in_log(struc
 	int match = 0;
 
 	path = btrfs_alloc_path();
+	BTRFS_UERROR(!path);
 	ret = btrfs_search_slot(NULL, log, key, path, 0, 0);
 	if (ret != 0)
 		goto out;
@@ -961,6 +962,7 @@ static noinline int fixup_inode_link_cou
 	key.offset = (u64)-1;
 
 	path = btrfs_alloc_path();
+	BTRFS_UERROR(!path);
 
 	while (1) {
 		ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
@@ -1585,7 +1587,7 @@ static int replay_one_buffer(struct btrf
 		return 0;
 
 	path = btrfs_alloc_path();
-	BUG_ON(!path);
+	BTRFS_UERROR(!path);
 
 	nritems = btrfs_header_nritems(eb);
 	for (i = 0; i < nritems; i++) {
@@ -1848,7 +1850,7 @@ static int walk_log_tree(struct btrfs_tr
 	int orig_level;
 
 	path = btrfs_alloc_path();
-	BUG_ON(!path);
+	BTRFS_UERROR(!path);
 
 	level = btrfs_header_level(log->node);
 	orig_level = level;
@@ -2217,6 +2219,7 @@ int btrfs_del_dir_entries_in_log(struct
 
 	log = root->log_root;
 	path = btrfs_alloc_path();
+	BTRFS_UERROR(!path);
 	di = btrfs_lookup_dir_item(trans, log, path, dir->i_ino,
 				   name, name_len, -1);
 	if (di && !IS_ERR(di)) {
@@ -2708,7 +2711,9 @@ static int btrfs_log_inode(struct btrfs_
 	log = root->log_root;
 
 	path = btrfs_alloc_path();
+	BTRFS_UERROR(!path);
 	dst_path = btrfs_alloc_path();
+	BTRFS_UERROR(!dst_path);
 
 	min_key.objectid = inode->i_ino;
 	min_key.type = BTRFS_INODE_ITEM_KEY;
@@ -3021,7 +3026,7 @@ int btrfs_recover_log_trees(struct btrfs
 
 	fs_info->log_root_recovering = 1;
 	path = btrfs_alloc_path();
-	BUG_ON(!path);
+	BTRFS_UERROR(!path);
 
 	trans = btrfs_start_transaction(fs_info->tree_root, 1);
 	if (IS_ERR(trans)) {
fs/btrfs/volumes.c +2 -2
@@ -946,7 +946,7 @@ static noinline int find_next_chunk(stru
 	struct btrfs_key found_key;
 
 	path = btrfs_alloc_path();
-	BUG_ON(!path);
+	BTRFS_UERROR(!path);
 
 	key.objectid = objectid;
 	key.offset = (u64)-1;
@@ -1943,7 +1943,7 @@ int btrfs_balance(struct btrfs_root *dev
 
 	/* step two, relocate all the chunks */
 	path = btrfs_alloc_path();
-	BUG_ON(!path);
+	BTRFS_UERROR(!path);
 
 	key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
 	key.offset = (u64)-1;
#11 lore raw
In reply to Jeff Mahoney
Quoted 10 lines · Jeff Mahoney

This patch series fixes a few existing problems and then addresses the need for better error handling and propagation in btrfs.

The handling and propagation patch set first finds all locations of a particular condition not being checked and annotates them with BTRFS_UERROR to designate that there is an error condition that is currently unhandled but should be. Then, another patch traverses the call path to ensure that errors are properly propogated. When the fix for recovering or passing the error is obvious, we do that too.

Quoted 4 lines

This set is not complete and I expect there to be more fixes coming. In particular, it currently only annotates btrfs_alloc_path failures as the call graph for it is quite large. This is in response to Chris asking for what I already have instead of waiting for it to be complete.

I should be clear that this is just what I've been working on so far and hasn't received any testing yet. It was a quick quilt mail in the middle of working on it. (As you might notice by the fact that it doesn't build.)

-Jeff

Signature · 2 lines
Jeff Mahoney
SUSE Labs