Skip to content

Commit

Permalink
Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel…
Browse files Browse the repository at this point in the history
…/git/mason/btrfs-unstable

* 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/mason/btrfs-unstable:
  Btrfs: don't warn in btrfs_add_orphan
  Btrfs: fix free space cache when there are pinned extents and clusters V2
  Btrfs: Fix uninitialized root flags for subvolumes
  btrfs: clear __GFP_FS flag in the space cache inode
  Btrfs: fix memory leak in start_transaction()
  Btrfs: fix memory leak in btrfs_ioctl_start_sync()
  Btrfs: fix subvol_sem leak in btrfs_rename()
  Btrfs: Fix oops for defrag with compression turned on
  Btrfs: fix /proc/mounts info.
  Btrfs: fix compiler warning in file.c
  • Loading branch information
Linus Torvalds committed Apr 5, 2011
2 parents d14f5b8 + c9ddec7 commit 884b826
Show file tree
Hide file tree
Showing 9 changed files with 145 additions and 25 deletions.
4 changes: 4 additions & 0 deletions fs/btrfs/ctree.h
Original file line number Diff line number Diff line change
Expand Up @@ -1284,6 +1284,8 @@ struct btrfs_root {
#define BTRFS_INODE_DIRSYNC (1 << 10)
#define BTRFS_INODE_COMPRESS (1 << 11)

#define BTRFS_INODE_ROOT_ITEM_INIT (1 << 31)

/* some macros to generate set/get funcs for the struct fields. This
* assumes there is a lefoo_to_cpu for every type, so lets make a simple
* one for u8:
Expand Down Expand Up @@ -2359,6 +2361,8 @@ int btrfs_find_dead_roots(struct btrfs_root *root, u64 objectid);
int btrfs_find_orphan_roots(struct btrfs_root *tree_root);
int btrfs_set_root_node(struct btrfs_root_item *item,
struct extent_buffer *node);
void btrfs_check_and_init_root_item(struct btrfs_root_item *item);

/* dir-item.c */
int btrfs_insert_dir_item(struct btrfs_trans_handle *trans,
struct btrfs_root *root, const char *name,
Expand Down
4 changes: 3 additions & 1 deletion fs/btrfs/disk-io.c
Original file line number Diff line number Diff line change
Expand Up @@ -1275,8 +1275,10 @@ struct btrfs_root *btrfs_read_fs_root_no_radix(struct btrfs_root *tree_root,
root->commit_root = btrfs_root_node(root);
BUG_ON(!root->node);
out:
if (location->objectid != BTRFS_TREE_LOG_OBJECTID)
if (location->objectid != BTRFS_TREE_LOG_OBJECTID) {
root->ref_cows = 1;
btrfs_check_and_init_root_item(&root->root_item);
}

return root;
}
Expand Down
2 changes: 1 addition & 1 deletion fs/btrfs/file.c
Original file line number Diff line number Diff line change
Expand Up @@ -906,7 +906,7 @@ static noinline ssize_t __btrfs_buffered_write(struct file *file,
unsigned long last_index;
size_t num_written = 0;
int nrptrs;
int ret;
int ret = 0;

nrptrs = min((iov_iter_count(i) + PAGE_CACHE_SIZE - 1) /
PAGE_CACHE_SIZE, PAGE_CACHE_SIZE /
Expand Down
84 changes: 80 additions & 4 deletions fs/btrfs/free-space-cache.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include "free-space-cache.h"
#include "transaction.h"
#include "disk-io.h"
#include "extent_io.h"

#define BITS_PER_BITMAP (PAGE_CACHE_SIZE * 8)
#define MAX_CACHE_BYTES_PER_GIG (32 * 1024)
Expand Down Expand Up @@ -81,6 +82,8 @@ struct inode *lookup_free_space_inode(struct btrfs_root *root,
return ERR_PTR(-ENOENT);
}

inode->i_mapping->flags &= ~__GFP_FS;

spin_lock(&block_group->lock);
if (!root->fs_info->closing) {
block_group->inode = igrab(inode);
Expand Down Expand Up @@ -222,6 +225,7 @@ int load_free_space_cache(struct btrfs_fs_info *fs_info,
u64 num_entries;
u64 num_bitmaps;
u64 generation;
u64 used = btrfs_block_group_used(&block_group->item);
u32 cur_crc = ~(u32)0;
pgoff_t index = 0;
unsigned long first_page_offset;
Expand Down Expand Up @@ -467,6 +471,17 @@ int load_free_space_cache(struct btrfs_fs_info *fs_info,
index++;
}

spin_lock(&block_group->tree_lock);
if (block_group->free_space != (block_group->key.offset - used -
block_group->bytes_super)) {
spin_unlock(&block_group->tree_lock);
printk(KERN_ERR "block group %llu has an wrong amount of free "
"space\n", block_group->key.objectid);
ret = 0;
goto free_cache;
}
spin_unlock(&block_group->tree_lock);

ret = 1;
out:
kfree(checksums);
Expand Down Expand Up @@ -495,8 +510,11 @@ int btrfs_write_out_cache(struct btrfs_root *root,
struct list_head *pos, *n;
struct page *page;
struct extent_state *cached_state = NULL;
struct btrfs_free_cluster *cluster = NULL;
struct extent_io_tree *unpin = NULL;
struct list_head bitmap_list;
struct btrfs_key key;
u64 start, end, len;
u64 bytes = 0;
u32 *crc, *checksums;
pgoff_t index = 0, last_index = 0;
Expand All @@ -505,6 +523,7 @@ int btrfs_write_out_cache(struct btrfs_root *root,
int entries = 0;
int bitmaps = 0;
int ret = 0;
bool next_page = false;

root = root->fs_info->tree_root;

Expand Down Expand Up @@ -551,6 +570,18 @@ int btrfs_write_out_cache(struct btrfs_root *root,
*/
first_page_offset = (sizeof(u32) * num_checksums) + sizeof(u64);

/* Get the cluster for this block_group if it exists */
if (!list_empty(&block_group->cluster_list))
cluster = list_entry(block_group->cluster_list.next,
struct btrfs_free_cluster,
block_group_list);

/*
* We shouldn't have switched the pinned extents yet so this is the
* right one
*/
unpin = root->fs_info->pinned_extents;

/*
* Lock all pages first so we can lock the extent safely.
*
Expand Down Expand Up @@ -580,13 +611,21 @@ int btrfs_write_out_cache(struct btrfs_root *root,
lock_extent_bits(&BTRFS_I(inode)->io_tree, 0, i_size_read(inode) - 1,
0, &cached_state, GFP_NOFS);

/*
* When searching for pinned extents, we need to start at our start
* offset.
*/
start = block_group->key.objectid;

/* Write out the extent entries */
do {
struct btrfs_free_space_entry *entry;
void *addr;
unsigned long offset = 0;
unsigned long start_offset = 0;

next_page = false;

if (index == 0) {
start_offset = first_page_offset;
offset = start_offset;
Expand All @@ -598,7 +637,7 @@ int btrfs_write_out_cache(struct btrfs_root *root,
entry = addr + start_offset;

memset(addr, 0, PAGE_CACHE_SIZE);
while (1) {
while (node && !next_page) {
struct btrfs_free_space *e;

e = rb_entry(node, struct btrfs_free_space, offset_index);
Expand All @@ -614,12 +653,49 @@ int btrfs_write_out_cache(struct btrfs_root *root,
entry->type = BTRFS_FREE_SPACE_EXTENT;
}
node = rb_next(node);
if (!node)
break;
if (!node && cluster) {
node = rb_first(&cluster->root);
cluster = NULL;
}
offset += sizeof(struct btrfs_free_space_entry);
if (offset + sizeof(struct btrfs_free_space_entry) >=
PAGE_CACHE_SIZE)
next_page = true;
entry++;
}

/*
* We want to add any pinned extents to our free space cache
* so we don't leak the space
*/
while (!next_page && (start < block_group->key.objectid +
block_group->key.offset)) {
ret = find_first_extent_bit(unpin, start, &start, &end,
EXTENT_DIRTY);
if (ret) {
ret = 0;
break;
}

/* This pinned extent is out of our range */
if (start >= block_group->key.objectid +
block_group->key.offset)
break;

len = block_group->key.objectid +
block_group->key.offset - start;
len = min(len, end + 1 - start);

entries++;
entry->offset = cpu_to_le64(start);
entry->bytes = cpu_to_le64(len);
entry->type = BTRFS_FREE_SPACE_EXTENT;

start = end + 1;
offset += sizeof(struct btrfs_free_space_entry);
if (offset + sizeof(struct btrfs_free_space_entry) >=
PAGE_CACHE_SIZE)
next_page = true;
entry++;
}
*crc = ~(u32)0;
Expand Down Expand Up @@ -650,7 +726,7 @@ int btrfs_write_out_cache(struct btrfs_root *root,
page_cache_release(page);

index++;
} while (node);
} while (node || next_page);

/* Write out the bitmaps */
list_for_each_safe(pos, n, &bitmap_list) {
Expand Down
29 changes: 13 additions & 16 deletions fs/btrfs/inode.c
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ static int btrfs_init_inode_security(struct btrfs_trans_handle *trans,
static noinline int insert_inline_extent(struct btrfs_trans_handle *trans,
struct btrfs_root *root, struct inode *inode,
u64 start, size_t size, size_t compressed_size,
int compress_type,
struct page **compressed_pages)
{
struct btrfs_key key;
Expand All @@ -126,12 +127,9 @@ static noinline int insert_inline_extent(struct btrfs_trans_handle *trans,
size_t cur_size = size;
size_t datasize;
unsigned long offset;
int compress_type = BTRFS_COMPRESS_NONE;

if (compressed_size && compressed_pages) {
compress_type = root->fs_info->compress_type;
if (compressed_size && compressed_pages)
cur_size = compressed_size;
}

path = btrfs_alloc_path();
if (!path)
Expand Down Expand Up @@ -221,7 +219,7 @@ static noinline int insert_inline_extent(struct btrfs_trans_handle *trans,
static noinline int cow_file_range_inline(struct btrfs_trans_handle *trans,
struct btrfs_root *root,
struct inode *inode, u64 start, u64 end,
size_t compressed_size,
size_t compressed_size, int compress_type,
struct page **compressed_pages)
{
u64 isize = i_size_read(inode);
Expand Down Expand Up @@ -254,7 +252,7 @@ static noinline int cow_file_range_inline(struct btrfs_trans_handle *trans,
inline_len = min_t(u64, isize, actual_end);
ret = insert_inline_extent(trans, root, inode, start,
inline_len, compressed_size,
compressed_pages);
compress_type, compressed_pages);
BUG_ON(ret);
btrfs_delalloc_release_metadata(inode, end + 1 - start);
btrfs_drop_extent_cache(inode, start, aligned_end - 1, 0);
Expand Down Expand Up @@ -433,12 +431,13 @@ static noinline int compress_file_range(struct inode *inode,
* to make an uncompressed inline extent.
*/
ret = cow_file_range_inline(trans, root, inode,
start, end, 0, NULL);
start, end, 0, 0, NULL);
} else {
/* try making a compressed inline extent */
ret = cow_file_range_inline(trans, root, inode,
start, end,
total_compressed, pages);
total_compressed,
compress_type, pages);
}
if (ret == 0) {
/*
Expand Down Expand Up @@ -792,7 +791,7 @@ static noinline int cow_file_range(struct inode *inode,
if (start == 0) {
/* lets try to make an inline extent */
ret = cow_file_range_inline(trans, root, inode,
start, end, 0, NULL);
start, end, 0, 0, NULL);
if (ret == 0) {
extent_clear_unlock_delalloc(inode,
&BTRFS_I(inode)->io_tree,
Expand Down Expand Up @@ -2222,8 +2221,6 @@ int btrfs_orphan_add(struct btrfs_trans_handle *trans, struct inode *inode)
insert = 1;
#endif
insert = 1;
} else {
WARN_ON(!BTRFS_I(inode)->orphan_meta_reserved);
}

if (!BTRFS_I(inode)->orphan_meta_reserved) {
Expand Down Expand Up @@ -2537,8 +2534,6 @@ static void btrfs_read_locked_inode(struct inode *inode)
BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);

alloc_group_block = btrfs_inode_block_group(leaf, inode_item);
if (location.objectid == BTRFS_FREE_SPACE_OBJECTID)
inode->i_mapping->flags &= ~__GFP_FS;

/*
* try to precache a NULL acl entry for files that don't have
Expand Down Expand Up @@ -6960,8 +6955,10 @@ static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
* should cover the worst case number of items we'll modify.
*/
trans = btrfs_start_transaction(root, 20);
if (IS_ERR(trans))
return PTR_ERR(trans);
if (IS_ERR(trans)) {
ret = PTR_ERR(trans);
goto out_notrans;
}

btrfs_set_trans_block_group(trans, new_dir);

Expand Down Expand Up @@ -7061,7 +7058,7 @@ static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
}
out_fail:
btrfs_end_transaction_throttle(trans, root);

out_notrans:
if (old_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)
up_read(&root->fs_info->subvol_sem);

Expand Down
8 changes: 7 additions & 1 deletion fs/btrfs/ioctl.c
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,10 @@ static noinline int create_subvol(struct btrfs_root *root,
inode_item->nbytes = cpu_to_le64(root->leafsize);
inode_item->mode = cpu_to_le32(S_IFDIR | 0755);

root_item.flags = 0;
root_item.byte_limit = 0;
inode_item->flags = cpu_to_le64(BTRFS_INODE_ROOT_ITEM_INIT);

btrfs_set_root_bytenr(&root_item, leaf->start);
btrfs_set_root_generation(&root_item, trans->transid);
btrfs_set_root_level(&root_item, 0);
Expand Down Expand Up @@ -2436,8 +2440,10 @@ static noinline long btrfs_ioctl_start_sync(struct file *file, void __user *argp
return PTR_ERR(trans);
transid = trans->transid;
ret = btrfs_commit_transaction_async(trans, root, 0);
if (ret)
if (ret) {
btrfs_end_transaction(trans, root);
return ret;
}

if (argp)
if (copy_to_user(argp, &transid, sizeof(transid)))
Expand Down
18 changes: 18 additions & 0 deletions fs/btrfs/root-tree.c
Original file line number Diff line number Diff line change
Expand Up @@ -473,3 +473,21 @@ int btrfs_add_root_ref(struct btrfs_trans_handle *trans,
btrfs_free_path(path);
return 0;
}

/*
* Old btrfs forgets to init root_item->flags and root_item->byte_limit
* for subvolumes. To work around this problem, we steal a bit from
* root_item->inode_item->flags, and use it to indicate if those fields
* have been properly initialized.
*/
void btrfs_check_and_init_root_item(struct btrfs_root_item *root_item)
{
u64 inode_flags = le64_to_cpu(root_item->inode.flags);

if (!(inode_flags & BTRFS_INODE_ROOT_ITEM_INIT)) {
inode_flags |= BTRFS_INODE_ROOT_ITEM_INIT;
root_item->inode.flags = cpu_to_le64(inode_flags);
root_item->flags = 0;
root_item->byte_limit = 0;
}
}
Loading

0 comments on commit 884b826

Please sign in to comment.