Skip to content

Commit 884b826

Browse files
committed
Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/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
2 parents d14f5b8 + c9ddec7 commit 884b826

File tree

9 files changed

+145
-25
lines changed

9 files changed

+145
-25
lines changed

fs/btrfs/ctree.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1284,6 +1284,8 @@ struct btrfs_root {
12841284
#define BTRFS_INODE_DIRSYNC (1 << 10)
12851285
#define BTRFS_INODE_COMPRESS (1 << 11)
12861286

1287+
#define BTRFS_INODE_ROOT_ITEM_INIT (1 << 31)
1288+
12871289
/* some macros to generate set/get funcs for the struct fields. This
12881290
* assumes there is a lefoo_to_cpu for every type, so lets make a simple
12891291
* one for u8:
@@ -2359,6 +2361,8 @@ int btrfs_find_dead_roots(struct btrfs_root *root, u64 objectid);
23592361
int btrfs_find_orphan_roots(struct btrfs_root *tree_root);
23602362
int btrfs_set_root_node(struct btrfs_root_item *item,
23612363
struct extent_buffer *node);
2364+
void btrfs_check_and_init_root_item(struct btrfs_root_item *item);
2365+
23622366
/* dir-item.c */
23632367
int btrfs_insert_dir_item(struct btrfs_trans_handle *trans,
23642368
struct btrfs_root *root, const char *name,

fs/btrfs/disk-io.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1275,8 +1275,10 @@ struct btrfs_root *btrfs_read_fs_root_no_radix(struct btrfs_root *tree_root,
12751275
root->commit_root = btrfs_root_node(root);
12761276
BUG_ON(!root->node);
12771277
out:
1278-
if (location->objectid != BTRFS_TREE_LOG_OBJECTID)
1278+
if (location->objectid != BTRFS_TREE_LOG_OBJECTID) {
12791279
root->ref_cows = 1;
1280+
btrfs_check_and_init_root_item(&root->root_item);
1281+
}
12801282

12811283
return root;
12821284
}

fs/btrfs/file.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -906,7 +906,7 @@ static noinline ssize_t __btrfs_buffered_write(struct file *file,
906906
unsigned long last_index;
907907
size_t num_written = 0;
908908
int nrptrs;
909-
int ret;
909+
int ret = 0;
910910

911911
nrptrs = min((iov_iter_count(i) + PAGE_CACHE_SIZE - 1) /
912912
PAGE_CACHE_SIZE, PAGE_CACHE_SIZE /

fs/btrfs/free-space-cache.c

Lines changed: 80 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include "free-space-cache.h"
2525
#include "transaction.h"
2626
#include "disk-io.h"
27+
#include "extent_io.h"
2728

2829
#define BITS_PER_BITMAP (PAGE_CACHE_SIZE * 8)
2930
#define MAX_CACHE_BYTES_PER_GIG (32 * 1024)
@@ -81,6 +82,8 @@ struct inode *lookup_free_space_inode(struct btrfs_root *root,
8182
return ERR_PTR(-ENOENT);
8283
}
8384

85+
inode->i_mapping->flags &= ~__GFP_FS;
86+
8487
spin_lock(&block_group->lock);
8588
if (!root->fs_info->closing) {
8689
block_group->inode = igrab(inode);
@@ -222,6 +225,7 @@ int load_free_space_cache(struct btrfs_fs_info *fs_info,
222225
u64 num_entries;
223226
u64 num_bitmaps;
224227
u64 generation;
228+
u64 used = btrfs_block_group_used(&block_group->item);
225229
u32 cur_crc = ~(u32)0;
226230
pgoff_t index = 0;
227231
unsigned long first_page_offset;
@@ -467,6 +471,17 @@ int load_free_space_cache(struct btrfs_fs_info *fs_info,
467471
index++;
468472
}
469473

474+
spin_lock(&block_group->tree_lock);
475+
if (block_group->free_space != (block_group->key.offset - used -
476+
block_group->bytes_super)) {
477+
spin_unlock(&block_group->tree_lock);
478+
printk(KERN_ERR "block group %llu has an wrong amount of free "
479+
"space\n", block_group->key.objectid);
480+
ret = 0;
481+
goto free_cache;
482+
}
483+
spin_unlock(&block_group->tree_lock);
484+
470485
ret = 1;
471486
out:
472487
kfree(checksums);
@@ -495,8 +510,11 @@ int btrfs_write_out_cache(struct btrfs_root *root,
495510
struct list_head *pos, *n;
496511
struct page *page;
497512
struct extent_state *cached_state = NULL;
513+
struct btrfs_free_cluster *cluster = NULL;
514+
struct extent_io_tree *unpin = NULL;
498515
struct list_head bitmap_list;
499516
struct btrfs_key key;
517+
u64 start, end, len;
500518
u64 bytes = 0;
501519
u32 *crc, *checksums;
502520
pgoff_t index = 0, last_index = 0;
@@ -505,6 +523,7 @@ int btrfs_write_out_cache(struct btrfs_root *root,
505523
int entries = 0;
506524
int bitmaps = 0;
507525
int ret = 0;
526+
bool next_page = false;
508527

509528
root = root->fs_info->tree_root;
510529

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

573+
/* Get the cluster for this block_group if it exists */
574+
if (!list_empty(&block_group->cluster_list))
575+
cluster = list_entry(block_group->cluster_list.next,
576+
struct btrfs_free_cluster,
577+
block_group_list);
578+
579+
/*
580+
* We shouldn't have switched the pinned extents yet so this is the
581+
* right one
582+
*/
583+
unpin = root->fs_info->pinned_extents;
584+
554585
/*
555586
* Lock all pages first so we can lock the extent safely.
556587
*
@@ -580,13 +611,21 @@ int btrfs_write_out_cache(struct btrfs_root *root,
580611
lock_extent_bits(&BTRFS_I(inode)->io_tree, 0, i_size_read(inode) - 1,
581612
0, &cached_state, GFP_NOFS);
582613

614+
/*
615+
* When searching for pinned extents, we need to start at our start
616+
* offset.
617+
*/
618+
start = block_group->key.objectid;
619+
583620
/* Write out the extent entries */
584621
do {
585622
struct btrfs_free_space_entry *entry;
586623
void *addr;
587624
unsigned long offset = 0;
588625
unsigned long start_offset = 0;
589626

627+
next_page = false;
628+
590629
if (index == 0) {
591630
start_offset = first_page_offset;
592631
offset = start_offset;
@@ -598,7 +637,7 @@ int btrfs_write_out_cache(struct btrfs_root *root,
598637
entry = addr + start_offset;
599638

600639
memset(addr, 0, PAGE_CACHE_SIZE);
601-
while (1) {
640+
while (node && !next_page) {
602641
struct btrfs_free_space *e;
603642

604643
e = rb_entry(node, struct btrfs_free_space, offset_index);
@@ -614,12 +653,49 @@ int btrfs_write_out_cache(struct btrfs_root *root,
614653
entry->type = BTRFS_FREE_SPACE_EXTENT;
615654
}
616655
node = rb_next(node);
617-
if (!node)
618-
break;
656+
if (!node && cluster) {
657+
node = rb_first(&cluster->root);
658+
cluster = NULL;
659+
}
619660
offset += sizeof(struct btrfs_free_space_entry);
620661
if (offset + sizeof(struct btrfs_free_space_entry) >=
621662
PAGE_CACHE_SIZE)
663+
next_page = true;
664+
entry++;
665+
}
666+
667+
/*
668+
* We want to add any pinned extents to our free space cache
669+
* so we don't leak the space
670+
*/
671+
while (!next_page && (start < block_group->key.objectid +
672+
block_group->key.offset)) {
673+
ret = find_first_extent_bit(unpin, start, &start, &end,
674+
EXTENT_DIRTY);
675+
if (ret) {
676+
ret = 0;
677+
break;
678+
}
679+
680+
/* This pinned extent is out of our range */
681+
if (start >= block_group->key.objectid +
682+
block_group->key.offset)
622683
break;
684+
685+
len = block_group->key.objectid +
686+
block_group->key.offset - start;
687+
len = min(len, end + 1 - start);
688+
689+
entries++;
690+
entry->offset = cpu_to_le64(start);
691+
entry->bytes = cpu_to_le64(len);
692+
entry->type = BTRFS_FREE_SPACE_EXTENT;
693+
694+
start = end + 1;
695+
offset += sizeof(struct btrfs_free_space_entry);
696+
if (offset + sizeof(struct btrfs_free_space_entry) >=
697+
PAGE_CACHE_SIZE)
698+
next_page = true;
623699
entry++;
624700
}
625701
*crc = ~(u32)0;
@@ -650,7 +726,7 @@ int btrfs_write_out_cache(struct btrfs_root *root,
650726
page_cache_release(page);
651727

652728
index++;
653-
} while (node);
729+
} while (node || next_page);
654730

655731
/* Write out the bitmaps */
656732
list_for_each_safe(pos, n, &bitmap_list) {

fs/btrfs/inode.c

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ static int btrfs_init_inode_security(struct btrfs_trans_handle *trans,
112112
static noinline int insert_inline_extent(struct btrfs_trans_handle *trans,
113113
struct btrfs_root *root, struct inode *inode,
114114
u64 start, size_t size, size_t compressed_size,
115+
int compress_type,
115116
struct page **compressed_pages)
116117
{
117118
struct btrfs_key key;
@@ -126,12 +127,9 @@ static noinline int insert_inline_extent(struct btrfs_trans_handle *trans,
126127
size_t cur_size = size;
127128
size_t datasize;
128129
unsigned long offset;
129-
int compress_type = BTRFS_COMPRESS_NONE;
130130

131-
if (compressed_size && compressed_pages) {
132-
compress_type = root->fs_info->compress_type;
131+
if (compressed_size && compressed_pages)
133132
cur_size = compressed_size;
134-
}
135133

136134
path = btrfs_alloc_path();
137135
if (!path)
@@ -221,7 +219,7 @@ static noinline int insert_inline_extent(struct btrfs_trans_handle *trans,
221219
static noinline int cow_file_range_inline(struct btrfs_trans_handle *trans,
222220
struct btrfs_root *root,
223221
struct inode *inode, u64 start, u64 end,
224-
size_t compressed_size,
222+
size_t compressed_size, int compress_type,
225223
struct page **compressed_pages)
226224
{
227225
u64 isize = i_size_read(inode);
@@ -254,7 +252,7 @@ static noinline int cow_file_range_inline(struct btrfs_trans_handle *trans,
254252
inline_len = min_t(u64, isize, actual_end);
255253
ret = insert_inline_extent(trans, root, inode, start,
256254
inline_len, compressed_size,
257-
compressed_pages);
255+
compress_type, compressed_pages);
258256
BUG_ON(ret);
259257
btrfs_delalloc_release_metadata(inode, end + 1 - start);
260258
btrfs_drop_extent_cache(inode, start, aligned_end - 1, 0);
@@ -433,12 +431,13 @@ static noinline int compress_file_range(struct inode *inode,
433431
* to make an uncompressed inline extent.
434432
*/
435433
ret = cow_file_range_inline(trans, root, inode,
436-
start, end, 0, NULL);
434+
start, end, 0, 0, NULL);
437435
} else {
438436
/* try making a compressed inline extent */
439437
ret = cow_file_range_inline(trans, root, inode,
440438
start, end,
441-
total_compressed, pages);
439+
total_compressed,
440+
compress_type, pages);
442441
}
443442
if (ret == 0) {
444443
/*
@@ -792,7 +791,7 @@ static noinline int cow_file_range(struct inode *inode,
792791
if (start == 0) {
793792
/* lets try to make an inline extent */
794793
ret = cow_file_range_inline(trans, root, inode,
795-
start, end, 0, NULL);
794+
start, end, 0, 0, NULL);
796795
if (ret == 0) {
797796
extent_clear_unlock_delalloc(inode,
798797
&BTRFS_I(inode)->io_tree,
@@ -2222,8 +2221,6 @@ int btrfs_orphan_add(struct btrfs_trans_handle *trans, struct inode *inode)
22222221
insert = 1;
22232222
#endif
22242223
insert = 1;
2225-
} else {
2226-
WARN_ON(!BTRFS_I(inode)->orphan_meta_reserved);
22272224
}
22282225

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

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

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

69666963
btrfs_set_trans_block_group(trans, new_dir);
69676964

@@ -7061,7 +7058,7 @@ static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
70617058
}
70627059
out_fail:
70637060
btrfs_end_transaction_throttle(trans, root);
7064-
7061+
out_notrans:
70657062
if (old_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)
70667063
up_read(&root->fs_info->subvol_sem);
70677064

fs/btrfs/ioctl.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,10 @@ static noinline int create_subvol(struct btrfs_root *root,
373373
inode_item->nbytes = cpu_to_le64(root->leafsize);
374374
inode_item->mode = cpu_to_le32(S_IFDIR | 0755);
375375

376+
root_item.flags = 0;
377+
root_item.byte_limit = 0;
378+
inode_item->flags = cpu_to_le64(BTRFS_INODE_ROOT_ITEM_INIT);
379+
376380
btrfs_set_root_bytenr(&root_item, leaf->start);
377381
btrfs_set_root_generation(&root_item, trans->transid);
378382
btrfs_set_root_level(&root_item, 0);
@@ -2436,8 +2440,10 @@ static noinline long btrfs_ioctl_start_sync(struct file *file, void __user *argp
24362440
return PTR_ERR(trans);
24372441
transid = trans->transid;
24382442
ret = btrfs_commit_transaction_async(trans, root, 0);
2439-
if (ret)
2443+
if (ret) {
2444+
btrfs_end_transaction(trans, root);
24402445
return ret;
2446+
}
24412447

24422448
if (argp)
24432449
if (copy_to_user(argp, &transid, sizeof(transid)))

fs/btrfs/root-tree.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -473,3 +473,21 @@ int btrfs_add_root_ref(struct btrfs_trans_handle *trans,
473473
btrfs_free_path(path);
474474
return 0;
475475
}
476+
477+
/*
478+
* Old btrfs forgets to init root_item->flags and root_item->byte_limit
479+
* for subvolumes. To work around this problem, we steal a bit from
480+
* root_item->inode_item->flags, and use it to indicate if those fields
481+
* have been properly initialized.
482+
*/
483+
void btrfs_check_and_init_root_item(struct btrfs_root_item *root_item)
484+
{
485+
u64 inode_flags = le64_to_cpu(root_item->inode.flags);
486+
487+
if (!(inode_flags & BTRFS_INODE_ROOT_ITEM_INIT)) {
488+
inode_flags |= BTRFS_INODE_ROOT_ITEM_INIT;
489+
root_item->inode.flags = cpu_to_le64(inode_flags);
490+
root_item->flags = 0;
491+
root_item->byte_limit = 0;
492+
}
493+
}

0 commit comments

Comments
 (0)