Skip to content

Commit 73d32ce

Browse files
committed
Merge branch 'misc-4.7' into for-chris-4.7-20160516
2 parents 02da2d7 + 4673272 commit 73d32ce

File tree

16 files changed

+442
-171
lines changed

16 files changed

+442
-171
lines changed

fs/btrfs/backref.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1991,7 +1991,7 @@ struct inode_fs_paths *init_ipath(s32 total_bytes, struct btrfs_root *fs_root,
19911991

19921992
ifp = kmalloc(sizeof(*ifp), GFP_NOFS);
19931993
if (!ifp) {
1994-
kfree(fspath);
1994+
vfree(fspath);
19951995
return ERR_PTR(-ENOMEM);
19961996
}
19971997

fs/btrfs/compression.c

Lines changed: 61 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -743,8 +743,11 @@ int btrfs_submit_compressed_read(struct inode *inode, struct bio *bio,
743743
static struct {
744744
struct list_head idle_ws;
745745
spinlock_t ws_lock;
746-
int num_ws;
747-
atomic_t alloc_ws;
746+
/* Number of free workspaces */
747+
int free_ws;
748+
/* Total number of allocated workspaces */
749+
atomic_t total_ws;
750+
/* Waiters for a free workspace */
748751
wait_queue_head_t ws_wait;
749752
} btrfs_comp_ws[BTRFS_COMPRESS_TYPES];
750753

@@ -758,16 +761,34 @@ void __init btrfs_init_compress(void)
758761
int i;
759762

760763
for (i = 0; i < BTRFS_COMPRESS_TYPES; i++) {
764+
struct list_head *workspace;
765+
761766
INIT_LIST_HEAD(&btrfs_comp_ws[i].idle_ws);
762767
spin_lock_init(&btrfs_comp_ws[i].ws_lock);
763-
atomic_set(&btrfs_comp_ws[i].alloc_ws, 0);
768+
atomic_set(&btrfs_comp_ws[i].total_ws, 0);
764769
init_waitqueue_head(&btrfs_comp_ws[i].ws_wait);
770+
771+
/*
772+
* Preallocate one workspace for each compression type so
773+
* we can guarantee forward progress in the worst case
774+
*/
775+
workspace = btrfs_compress_op[i]->alloc_workspace();
776+
if (IS_ERR(workspace)) {
777+
printk(KERN_WARNING
778+
"BTRFS: cannot preallocate compression workspace, will try later");
779+
} else {
780+
atomic_set(&btrfs_comp_ws[i].total_ws, 1);
781+
btrfs_comp_ws[i].free_ws = 1;
782+
list_add(workspace, &btrfs_comp_ws[i].idle_ws);
783+
}
765784
}
766785
}
767786

768787
/*
769-
* this finds an available workspace or allocates a new one
770-
* ERR_PTR is returned if things go bad.
788+
* This finds an available workspace or allocates a new one.
789+
* If it's not possible to allocate a new one, waits until there's one.
790+
* Preallocation makes a forward progress guarantees and we do not return
791+
* errors.
771792
*/
772793
static struct list_head *find_workspace(int type)
773794
{
@@ -777,36 +798,58 @@ static struct list_head *find_workspace(int type)
777798

778799
struct list_head *idle_ws = &btrfs_comp_ws[idx].idle_ws;
779800
spinlock_t *ws_lock = &btrfs_comp_ws[idx].ws_lock;
780-
atomic_t *alloc_ws = &btrfs_comp_ws[idx].alloc_ws;
801+
atomic_t *total_ws = &btrfs_comp_ws[idx].total_ws;
781802
wait_queue_head_t *ws_wait = &btrfs_comp_ws[idx].ws_wait;
782-
int *num_ws = &btrfs_comp_ws[idx].num_ws;
803+
int *free_ws = &btrfs_comp_ws[idx].free_ws;
783804
again:
784805
spin_lock(ws_lock);
785806
if (!list_empty(idle_ws)) {
786807
workspace = idle_ws->next;
787808
list_del(workspace);
788-
(*num_ws)--;
809+
(*free_ws)--;
789810
spin_unlock(ws_lock);
790811
return workspace;
791812

792813
}
793-
if (atomic_read(alloc_ws) > cpus) {
814+
if (atomic_read(total_ws) > cpus) {
794815
DEFINE_WAIT(wait);
795816

796817
spin_unlock(ws_lock);
797818
prepare_to_wait(ws_wait, &wait, TASK_UNINTERRUPTIBLE);
798-
if (atomic_read(alloc_ws) > cpus && !*num_ws)
819+
if (atomic_read(total_ws) > cpus && !*free_ws)
799820
schedule();
800821
finish_wait(ws_wait, &wait);
801822
goto again;
802823
}
803-
atomic_inc(alloc_ws);
824+
atomic_inc(total_ws);
804825
spin_unlock(ws_lock);
805826

806827
workspace = btrfs_compress_op[idx]->alloc_workspace();
807828
if (IS_ERR(workspace)) {
808-
atomic_dec(alloc_ws);
829+
atomic_dec(total_ws);
809830
wake_up(ws_wait);
831+
832+
/*
833+
* Do not return the error but go back to waiting. There's a
834+
* workspace preallocated for each type and the compression
835+
* time is bounded so we get to a workspace eventually. This
836+
* makes our caller's life easier.
837+
*
838+
* To prevent silent and low-probability deadlocks (when the
839+
* initial preallocation fails), check if there are any
840+
* workspaces at all.
841+
*/
842+
if (atomic_read(total_ws) == 0) {
843+
static DEFINE_RATELIMIT_STATE(_rs,
844+
/* once per minute */ 60 * HZ,
845+
/* no burst */ 1);
846+
847+
if (__ratelimit(&_rs)) {
848+
printk(KERN_WARNING
849+
"no compression workspaces, low memory, retrying");
850+
}
851+
}
852+
goto again;
810853
}
811854
return workspace;
812855
}
@@ -820,21 +863,21 @@ static void free_workspace(int type, struct list_head *workspace)
820863
int idx = type - 1;
821864
struct list_head *idle_ws = &btrfs_comp_ws[idx].idle_ws;
822865
spinlock_t *ws_lock = &btrfs_comp_ws[idx].ws_lock;
823-
atomic_t *alloc_ws = &btrfs_comp_ws[idx].alloc_ws;
866+
atomic_t *total_ws = &btrfs_comp_ws[idx].total_ws;
824867
wait_queue_head_t *ws_wait = &btrfs_comp_ws[idx].ws_wait;
825-
int *num_ws = &btrfs_comp_ws[idx].num_ws;
868+
int *free_ws = &btrfs_comp_ws[idx].free_ws;
826869

827870
spin_lock(ws_lock);
828-
if (*num_ws < num_online_cpus()) {
871+
if (*free_ws < num_online_cpus()) {
829872
list_add(workspace, idle_ws);
830-
(*num_ws)++;
873+
(*free_ws)++;
831874
spin_unlock(ws_lock);
832875
goto wake;
833876
}
834877
spin_unlock(ws_lock);
835878

836879
btrfs_compress_op[idx]->free_workspace(workspace);
837-
atomic_dec(alloc_ws);
880+
atomic_dec(total_ws);
838881
wake:
839882
/*
840883
* Make sure counter is updated before we wake up waiters.
@@ -857,7 +900,7 @@ static void free_workspaces(void)
857900
workspace = btrfs_comp_ws[i].idle_ws.next;
858901
list_del(workspace);
859902
btrfs_compress_op[i]->free_workspace(workspace);
860-
atomic_dec(&btrfs_comp_ws[i].alloc_ws);
903+
atomic_dec(&btrfs_comp_ws[i].total_ws);
861904
}
862905
}
863906
}
@@ -894,8 +937,6 @@ int btrfs_compress_pages(int type, struct address_space *mapping,
894937
int ret;
895938

896939
workspace = find_workspace(type);
897-
if (IS_ERR(workspace))
898-
return PTR_ERR(workspace);
899940

900941
ret = btrfs_compress_op[type-1]->compress_pages(workspace, mapping,
901942
start, len, pages,
@@ -930,8 +971,6 @@ static int btrfs_decompress_biovec(int type, struct page **pages_in,
930971
int ret;
931972

932973
workspace = find_workspace(type);
933-
if (IS_ERR(workspace))
934-
return PTR_ERR(workspace);
935974

936975
ret = btrfs_compress_op[type-1]->decompress_biovec(workspace, pages_in,
937976
disk_start,
@@ -952,8 +991,6 @@ int btrfs_decompress(int type, unsigned char *data_in, struct page *dest_page,
952991
int ret;
953992

954993
workspace = find_workspace(type);
955-
if (IS_ERR(workspace))
956-
return PTR_ERR(workspace);
957994

958995
ret = btrfs_compress_op[type-1]->decompress(workspace, data_in,
959996
dest_page, start_byte,

fs/btrfs/ctree.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4122,6 +4122,7 @@ void btrfs_test_inode_set_ops(struct inode *inode);
41224122

41234123
/* ioctl.c */
41244124
long btrfs_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
4125+
long btrfs_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
41254126
int btrfs_ioctl_get_supported_features(void __user *arg);
41264127
void btrfs_update_iflags(struct inode *inode);
41274128
void btrfs_inherit_iflags(struct inode *inode, struct inode *dir);

fs/btrfs/disk-io.c

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2517,6 +2517,7 @@ int open_ctree(struct super_block *sb,
25172517
int num_backups_tried = 0;
25182518
int backup_index = 0;
25192519
int max_active;
2520+
bool cleaner_mutex_locked = false;
25202521

25212522
tree_root = fs_info->tree_root = btrfs_alloc_root(fs_info, GFP_KERNEL);
25222523
chunk_root = fs_info->chunk_root = btrfs_alloc_root(fs_info, GFP_KERNEL);
@@ -2997,6 +2998,13 @@ int open_ctree(struct super_block *sb,
29972998
goto fail_sysfs;
29982999
}
29993000

3001+
/*
3002+
* Hold the cleaner_mutex thread here so that we don't block
3003+
* for a long time on btrfs_recover_relocation. cleaner_kthread
3004+
* will wait for us to finish mounting the filesystem.
3005+
*/
3006+
mutex_lock(&fs_info->cleaner_mutex);
3007+
cleaner_mutex_locked = true;
30003008
fs_info->cleaner_kthread = kthread_run(cleaner_kthread, tree_root,
30013009
"btrfs-cleaner");
30023010
if (IS_ERR(fs_info->cleaner_kthread))
@@ -3056,17 +3064,17 @@ int open_ctree(struct super_block *sb,
30563064
ret = btrfs_cleanup_fs_roots(fs_info);
30573065
if (ret)
30583066
goto fail_qgroup;
3059-
3060-
mutex_lock(&fs_info->cleaner_mutex);
3067+
/* We locked cleaner_mutex before creating cleaner_kthread. */
30613068
ret = btrfs_recover_relocation(tree_root);
3062-
mutex_unlock(&fs_info->cleaner_mutex);
30633069
if (ret < 0) {
30643070
printk(KERN_WARNING
30653071
"BTRFS: failed to recover relocation\n");
30663072
err = -EINVAL;
30673073
goto fail_qgroup;
30683074
}
30693075
}
3076+
mutex_unlock(&fs_info->cleaner_mutex);
3077+
cleaner_mutex_locked = false;
30703078

30713079
location.objectid = BTRFS_FS_TREE_OBJECTID;
30723080
location.type = BTRFS_ROOT_ITEM_KEY;
@@ -3180,6 +3188,10 @@ int open_ctree(struct super_block *sb,
31803188
filemap_write_and_wait(fs_info->btree_inode->i_mapping);
31813189

31823190
fail_sysfs:
3191+
if (cleaner_mutex_locked) {
3192+
mutex_unlock(&fs_info->cleaner_mutex);
3193+
cleaner_mutex_locked = false;
3194+
}
31833195
btrfs_sysfs_remove_mounted(fs_info);
31843196

31853197
fail_fsdev_sysfs:

fs/btrfs/extent-tree.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4620,7 +4620,7 @@ static void shrink_delalloc(struct btrfs_root *root, u64 to_reclaim, u64 orig,
46204620

46214621
/* Calc the number of the pages we need flush for space reservation */
46224622
items = calc_reclaim_items_nr(root, to_reclaim);
4623-
to_reclaim = items * EXTENT_SIZE_PER_ITEM;
4623+
to_reclaim = (u64)items * EXTENT_SIZE_PER_ITEM;
46244624

46254625
trans = (struct btrfs_trans_handle *)current->journal_info;
46264626
block_rsv = &root->fs_info->delalloc_block_rsv;

fs/btrfs/extent_io.c

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3200,14 +3200,10 @@ int extent_read_full_page(struct extent_io_tree *tree, struct page *page,
32003200
return ret;
32013201
}
32023202

3203-
static noinline void update_nr_written(struct page *page,
3204-
struct writeback_control *wbc,
3205-
unsigned long nr_written)
3203+
static void update_nr_written(struct page *page, struct writeback_control *wbc,
3204+
unsigned long nr_written)
32063205
{
32073206
wbc->nr_to_write -= nr_written;
3208-
if (wbc->range_cyclic || (wbc->nr_to_write > 0 &&
3209-
wbc->range_start == 0 && wbc->range_end == LLONG_MAX))
3210-
page->mapping->writeback_index = page->index + nr_written;
32113207
}
32123208

32133209
/*
@@ -3920,12 +3916,13 @@ static int extent_write_cache_pages(struct extent_io_tree *tree,
39203916
struct inode *inode = mapping->host;
39213917
int ret = 0;
39223918
int done = 0;
3923-
int err = 0;
39243919
int nr_to_write_done = 0;
39253920
struct pagevec pvec;
39263921
int nr_pages;
39273922
pgoff_t index;
39283923
pgoff_t end; /* Inclusive */
3924+
pgoff_t done_index;
3925+
int range_whole = 0;
39293926
int scanned = 0;
39303927
int tag;
39313928

@@ -3948,6 +3945,8 @@ static int extent_write_cache_pages(struct extent_io_tree *tree,
39483945
} else {
39493946
index = wbc->range_start >> PAGE_SHIFT;
39503947
end = wbc->range_end >> PAGE_SHIFT;
3948+
if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX)
3949+
range_whole = 1;
39513950
scanned = 1;
39523951
}
39533952
if (wbc->sync_mode == WB_SYNC_ALL)
@@ -3957,6 +3956,7 @@ static int extent_write_cache_pages(struct extent_io_tree *tree,
39573956
retry:
39583957
if (wbc->sync_mode == WB_SYNC_ALL)
39593958
tag_pages_for_writeback(mapping, index, end);
3959+
done_index = index;
39603960
while (!done && !nr_to_write_done && (index <= end) &&
39613961
(nr_pages = pagevec_lookup_tag(&pvec, mapping, &index, tag,
39623962
min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1))) {
@@ -3966,6 +3966,7 @@ static int extent_write_cache_pages(struct extent_io_tree *tree,
39663966
for (i = 0; i < nr_pages; i++) {
39673967
struct page *page = pvec.pages[i];
39683968

3969+
done_index = page->index;
39693970
/*
39703971
* At this point we hold neither mapping->tree_lock nor
39713972
* lock on the page itself: the page may be truncated or
@@ -4007,8 +4008,20 @@ static int extent_write_cache_pages(struct extent_io_tree *tree,
40074008
unlock_page(page);
40084009
ret = 0;
40094010
}
4010-
if (!err && ret < 0)
4011-
err = ret;
4011+
if (ret < 0) {
4012+
/*
4013+
* done_index is set past this page,
4014+
* so media errors will not choke
4015+
* background writeout for the entire
4016+
* file. This has consequences for
4017+
* range_cyclic semantics (ie. it may
4018+
* not be suitable for data integrity
4019+
* writeout).
4020+
*/
4021+
done_index = page->index + 1;
4022+
done = 1;
4023+
break;
4024+
}
40124025

40134026
/*
40144027
* the filesystem may choose to bump up nr_to_write.
@@ -4020,7 +4033,7 @@ static int extent_write_cache_pages(struct extent_io_tree *tree,
40204033
pagevec_release(&pvec);
40214034
cond_resched();
40224035
}
4023-
if (!scanned && !done && !err) {
4036+
if (!scanned && !done) {
40244037
/*
40254038
* We hit the last page and there is more work to be done: wrap
40264039
* back to the start of the file
@@ -4029,8 +4042,12 @@ static int extent_write_cache_pages(struct extent_io_tree *tree,
40294042
index = 0;
40304043
goto retry;
40314044
}
4045+
4046+
if (wbc->range_cyclic || (wbc->nr_to_write > 0 && range_whole))
4047+
mapping->writeback_index = done_index;
4048+
40324049
btrfs_add_delayed_iput(inode);
4033-
return err;
4050+
return ret;
40344051
}
40354052

40364053
static void flush_epd_write_bio(struct extent_page_data *epd)

fs/btrfs/file.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1696,7 +1696,9 @@ static noinline ssize_t __btrfs_buffered_write(struct file *file,
16961696
btrfs_end_write_no_snapshoting(root);
16971697
btrfs_delalloc_release_metadata(inode, release_bytes);
16981698
} else {
1699-
btrfs_delalloc_release_space(inode, pos, release_bytes);
1699+
btrfs_delalloc_release_space(inode,
1700+
round_down(pos, root->sectorsize),
1701+
release_bytes);
17001702
}
17011703
}
17021704

@@ -2956,7 +2958,7 @@ const struct file_operations btrfs_file_operations = {
29562958
.fallocate = btrfs_fallocate,
29572959
.unlocked_ioctl = btrfs_ioctl,
29582960
#ifdef CONFIG_COMPAT
2959-
.compat_ioctl = btrfs_ioctl,
2961+
.compat_ioctl = btrfs_compat_ioctl,
29602962
#endif
29612963
.copy_file_range = btrfs_copy_file_range,
29622964
.clone_file_range = btrfs_clone_file_range,

fs/btrfs/inode.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10184,7 +10184,7 @@ static const struct file_operations btrfs_dir_file_operations = {
1018410184
.iterate = btrfs_real_readdir,
1018510185
.unlocked_ioctl = btrfs_ioctl,
1018610186
#ifdef CONFIG_COMPAT
10187-
.compat_ioctl = btrfs_ioctl,
10187+
.compat_ioctl = btrfs_compat_ioctl,
1018810188
#endif
1018910189
.release = btrfs_release_file,
1019010190
.fsync = btrfs_sync_file,

0 commit comments

Comments
 (0)