if (btrfs_block_can_be_shared(root, buf)) {
                ret = btrfs_lookup_extent_info(trans, root, buf->start,
                                               buf->len, &refs, &flags);
-               BUG_ON(ret);
+               if (ret)
+                       return ret;
                BUG_ON(refs == 0);
        } else {
                refs = 1;
                                                          buf->start,
                                                          buf->len,
                                                          new_flags, 0);
-                       BUG_ON(ret);
+                       if (ret)
+                               return ret;
                }
        } else {
                if (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF) {
 {
        struct btrfs_disk_key disk_key;
        struct extent_buffer *cow;
-       int level;
+       int level, ret;
        int last_ref = 0;
        int unlock_orig = 0;
        u64 parent_start;
                            (unsigned long)btrfs_header_fsid(cow),
                            BTRFS_FSID_SIZE);
 
-       update_ref_for_cow(trans, root, buf, cow, &last_ref);
+       ret = update_ref_for_cow(trans, root, buf, cow, &last_ref);
+       BUG_ON(ret);
 
        if (root->ref_cows)
                btrfs_reloc_cow_block(trans, root, buf, cow);