aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--fs/btrfs/ctree.c36
-rw-r--r--fs/btrfs/ctree.h44
-rw-r--r--fs/btrfs/dev-replace.c2
-rw-r--r--fs/btrfs/disk-io.c21
-rw-r--r--fs/btrfs/extent_io.c3
-rw-r--r--fs/btrfs/file-item.c92
-rw-r--r--fs/btrfs/file.c105
-rw-r--r--fs/btrfs/inode.c250
-rw-r--r--fs/btrfs/ioctl.c9
-rw-r--r--fs/btrfs/print-tree.c23
-rw-r--r--fs/btrfs/reada.c10
-rw-r--r--fs/btrfs/scrub.c24
-rw-r--r--fs/btrfs/send.c36
-rw-r--r--fs/btrfs/volumes.c23
14 files changed, 445 insertions, 233 deletions
diff --git a/fs/btrfs/ctree.c b/fs/btrfs/ctree.c
index 769e0ff1b4ce..77592931ab4f 100644
--- a/fs/btrfs/ctree.c
+++ b/fs/btrfs/ctree.c
@@ -311,7 +311,7 @@ struct tree_mod_root {
311 311
312struct tree_mod_elem { 312struct tree_mod_elem {
313 struct rb_node node; 313 struct rb_node node;
314 u64 index; /* shifted logical */ 314 u64 logical;
315 u64 seq; 315 u64 seq;
316 enum mod_log_op op; 316 enum mod_log_op op;
317 317
@@ -435,11 +435,11 @@ void btrfs_put_tree_mod_seq(struct btrfs_fs_info *fs_info,
435 435
436/* 436/*
437 * key order of the log: 437 * key order of the log:
438 * index -> sequence 438 * node/leaf start address -> sequence
439 * 439 *
440 * the index is the shifted logical of the *new* root node for root replace 440 * The 'start address' is the logical address of the *new* root node
441 * operations, or the shifted logical of the affected block for all other 441 * for root replace operations, or the logical address of the affected
442 * operations. 442 * block for all other operations.
443 * 443 *
444 * Note: must be called with write lock (tree_mod_log_write_lock). 444 * Note: must be called with write lock (tree_mod_log_write_lock).
445 */ 445 */
@@ -460,9 +460,9 @@ __tree_mod_log_insert(struct btrfs_fs_info *fs_info, struct tree_mod_elem *tm)
460 while (*new) { 460 while (*new) {
461 cur = container_of(*new, struct tree_mod_elem, node); 461 cur = container_of(*new, struct tree_mod_elem, node);
462 parent = *new; 462 parent = *new;
463 if (cur->index < tm->index) 463 if (cur->logical < tm->logical)
464 new = &((*new)->rb_left); 464 new = &((*new)->rb_left);
465 else if (cur->index > tm->index) 465 else if (cur->logical > tm->logical)
466 new = &((*new)->rb_right); 466 new = &((*new)->rb_right);
467 else if (cur->seq < tm->seq) 467 else if (cur->seq < tm->seq)
468 new = &((*new)->rb_left); 468 new = &((*new)->rb_left);
@@ -523,7 +523,7 @@ alloc_tree_mod_elem(struct extent_buffer *eb, int slot,
523 if (!tm) 523 if (!tm)
524 return NULL; 524 return NULL;
525 525
526 tm->index = eb->start >> PAGE_CACHE_SHIFT; 526 tm->logical = eb->start;
527 if (op != MOD_LOG_KEY_ADD) { 527 if (op != MOD_LOG_KEY_ADD) {
528 btrfs_node_key(eb, &tm->key, slot); 528 btrfs_node_key(eb, &tm->key, slot);
529 tm->blockptr = btrfs_node_blockptr(eb, slot); 529 tm->blockptr = btrfs_node_blockptr(eb, slot);
@@ -588,7 +588,7 @@ tree_mod_log_insert_move(struct btrfs_fs_info *fs_info,
588 goto free_tms; 588 goto free_tms;
589 } 589 }
590 590
591 tm->index = eb->start >> PAGE_CACHE_SHIFT; 591 tm->logical = eb->start;
592 tm->slot = src_slot; 592 tm->slot = src_slot;
593 tm->move.dst_slot = dst_slot; 593 tm->move.dst_slot = dst_slot;
594 tm->move.nr_items = nr_items; 594 tm->move.nr_items = nr_items;
@@ -699,7 +699,7 @@ tree_mod_log_insert_root(struct btrfs_fs_info *fs_info,
699 goto free_tms; 699 goto free_tms;
700 } 700 }
701 701
702 tm->index = new_root->start >> PAGE_CACHE_SHIFT; 702 tm->logical = new_root->start;
703 tm->old_root.logical = old_root->start; 703 tm->old_root.logical = old_root->start;
704 tm->old_root.level = btrfs_header_level(old_root); 704 tm->old_root.level = btrfs_header_level(old_root);
705 tm->generation = btrfs_header_generation(old_root); 705 tm->generation = btrfs_header_generation(old_root);
@@ -739,16 +739,15 @@ __tree_mod_log_search(struct btrfs_fs_info *fs_info, u64 start, u64 min_seq,
739 struct rb_node *node; 739 struct rb_node *node;
740 struct tree_mod_elem *cur = NULL; 740 struct tree_mod_elem *cur = NULL;
741 struct tree_mod_elem *found = NULL; 741 struct tree_mod_elem *found = NULL;
742 u64 index = start >> PAGE_CACHE_SHIFT;
743 742
744 tree_mod_log_read_lock(fs_info); 743 tree_mod_log_read_lock(fs_info);
745 tm_root = &fs_info->tree_mod_log; 744 tm_root = &fs_info->tree_mod_log;
746 node = tm_root->rb_node; 745 node = tm_root->rb_node;
747 while (node) { 746 while (node) {
748 cur = container_of(node, struct tree_mod_elem, node); 747 cur = container_of(node, struct tree_mod_elem, node);
749 if (cur->index < index) { 748 if (cur->logical < start) {
750 node = node->rb_left; 749 node = node->rb_left;
751 } else if (cur->index > index) { 750 } else if (cur->logical > start) {
752 node = node->rb_right; 751 node = node->rb_right;
753 } else if (cur->seq < min_seq) { 752 } else if (cur->seq < min_seq) {
754 node = node->rb_left; 753 node = node->rb_left;
@@ -1230,9 +1229,10 @@ __tree_mod_log_oldest_root(struct btrfs_fs_info *fs_info,
1230 return NULL; 1229 return NULL;
1231 1230
1232 /* 1231 /*
1233 * the very last operation that's logged for a root is the replacement 1232 * the very last operation that's logged for a root is the
1234 * operation (if it is replaced at all). this has the index of the *new* 1233 * replacement operation (if it is replaced at all). this has
1235 * root, making it the very first operation that's logged for this root. 1234 * the logical address of the *new* root, making it the very
1235 * first operation that's logged for this root.
1236 */ 1236 */
1237 while (1) { 1237 while (1) {
1238 tm = tree_mod_log_search_oldest(fs_info, root_logical, 1238 tm = tree_mod_log_search_oldest(fs_info, root_logical,
@@ -1336,7 +1336,7 @@ __tree_mod_log_rewind(struct btrfs_fs_info *fs_info, struct extent_buffer *eb,
1336 if (!next) 1336 if (!next)
1337 break; 1337 break;
1338 tm = container_of(next, struct tree_mod_elem, node); 1338 tm = container_of(next, struct tree_mod_elem, node);
1339 if (tm->index != first_tm->index) 1339 if (tm->logical != first_tm->logical)
1340 break; 1340 break;
1341 } 1341 }
1342 tree_mod_log_read_unlock(fs_info); 1342 tree_mod_log_read_unlock(fs_info);
@@ -5361,7 +5361,7 @@ int btrfs_compare_trees(struct btrfs_root *left_root,
5361 goto out; 5361 goto out;
5362 } 5362 }
5363 5363
5364 tmp_buf = kmalloc(left_root->nodesize, GFP_NOFS); 5364 tmp_buf = kmalloc(left_root->nodesize, GFP_KERNEL);
5365 if (!tmp_buf) { 5365 if (!tmp_buf) {
5366 ret = -ENOMEM; 5366 ret = -ENOMEM;
5367 goto out; 5367 goto out;
diff --git a/fs/btrfs/ctree.h b/fs/btrfs/ctree.h
index a79bb734f6c3..6af1211d4ee9 100644
--- a/fs/btrfs/ctree.h
+++ b/fs/btrfs/ctree.h
@@ -100,6 +100,9 @@ struct btrfs_ordered_sum;
100/* tracks free space in block groups. */ 100/* tracks free space in block groups. */
101#define BTRFS_FREE_SPACE_TREE_OBJECTID 10ULL 101#define BTRFS_FREE_SPACE_TREE_OBJECTID 10ULL
102 102
103/* device stats in the device tree */
104#define BTRFS_DEV_STATS_OBJECTID 0ULL
105
103/* for storing balance parameters in the root tree */ 106/* for storing balance parameters in the root tree */
104#define BTRFS_BALANCE_OBJECTID -4ULL 107#define BTRFS_BALANCE_OBJECTID -4ULL
105 108
@@ -2185,13 +2188,43 @@ struct btrfs_ioctl_defrag_range_args {
2185 */ 2188 */
2186#define BTRFS_QGROUP_RELATION_KEY 246 2189#define BTRFS_QGROUP_RELATION_KEY 246
2187 2190
2191/*
2192 * Obsolete name, see BTRFS_TEMPORARY_ITEM_KEY.
2193 */
2188#define BTRFS_BALANCE_ITEM_KEY 248 2194#define BTRFS_BALANCE_ITEM_KEY 248
2189 2195
2190/* 2196/*
2191 * Persistantly stores the io stats in the device tree. 2197 * The key type for tree items that are stored persistently, but do not need to
2192 * One key for all stats, (0, BTRFS_DEV_STATS_KEY, devid). 2198 * exist for extended period of time. The items can exist in any tree.
2199 *
2200 * [subtype, BTRFS_TEMPORARY_ITEM_KEY, data]
2201 *
2202 * Existing items:
2203 *
2204 * - balance status item
2205 * (BTRFS_BALANCE_OBJECTID, BTRFS_TEMPORARY_ITEM_KEY, 0)
2206 */
2207#define BTRFS_TEMPORARY_ITEM_KEY 248
2208
2209/*
2210 * Obsolete name, see BTRFS_PERSISTENT_ITEM_KEY
2211 */
2212#define BTRFS_DEV_STATS_KEY 249
2213
2214/*
2215 * The key type for tree items that are stored persistently and usually exist
2216 * for a long period, eg. filesystem lifetime. The item kinds can be status
2217 * information, stats or preference values. The item can exist in any tree.
2218 *
2219 * [subtype, BTRFS_PERSISTENT_ITEM_KEY, data]
2220 *
2221 * Existing items:
2222 *
2223 * - device statistics, store IO stats in the device tree, one key for all
2224 * stats
2225 * (BTRFS_DEV_STATS_OBJECTID, BTRFS_DEV_STATS_KEY, 0)
2193 */ 2226 */
2194#define BTRFS_DEV_STATS_KEY 249 2227#define BTRFS_PERSISTENT_ITEM_KEY 249
2195 2228
2196/* 2229/*
2197 * Persistantly stores the device replace state in the device tree. 2230 * Persistantly stores the device replace state in the device tree.
@@ -2354,6 +2387,9 @@ struct btrfs_map_token {
2354 unsigned long offset; 2387 unsigned long offset;
2355}; 2388};
2356 2389
2390#define BTRFS_BYTES_TO_BLKS(fs_info, bytes) \
2391 ((bytes) >> (fs_info)->sb->s_blocksize_bits)
2392
2357static inline void btrfs_init_map_token (struct btrfs_map_token *token) 2393static inline void btrfs_init_map_token (struct btrfs_map_token *token)
2358{ 2394{
2359 token->kaddr = NULL; 2395 token->kaddr = NULL;
@@ -4028,7 +4064,7 @@ int btrfs_unlink_subvol(struct btrfs_trans_handle *trans,
4028 struct btrfs_root *root, 4064 struct btrfs_root *root,
4029 struct inode *dir, u64 objectid, 4065 struct inode *dir, u64 objectid,
4030 const char *name, int name_len); 4066 const char *name, int name_len);
4031int btrfs_truncate_page(struct inode *inode, loff_t from, loff_t len, 4067int btrfs_truncate_block(struct inode *inode, loff_t from, loff_t len,
4032 int front); 4068 int front);
4033int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans, 4069int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
4034 struct btrfs_root *root, 4070 struct btrfs_root *root,
diff --git a/fs/btrfs/dev-replace.c b/fs/btrfs/dev-replace.c
index cbb7dbfb3fff..01ce5fcecc5c 100644
--- a/fs/btrfs/dev-replace.c
+++ b/fs/btrfs/dev-replace.c
@@ -802,7 +802,7 @@ static int btrfs_dev_replace_kthread(void *data)
802 struct btrfs_ioctl_dev_replace_args *status_args; 802 struct btrfs_ioctl_dev_replace_args *status_args;
803 u64 progress; 803 u64 progress;
804 804
805 status_args = kzalloc(sizeof(*status_args), GFP_NOFS); 805 status_args = kzalloc(sizeof(*status_args), GFP_KERNEL);
806 if (status_args) { 806 if (status_args) {
807 btrfs_dev_replace_status(fs_info, status_args); 807 btrfs_dev_replace_status(fs_info, status_args);
808 progress = status_args->status.progress_1000; 808 progress = status_args->status.progress_1000;
diff --git a/fs/btrfs/disk-io.c b/fs/btrfs/disk-io.c
index 18541dc4ec0b..294c77729df3 100644
--- a/fs/btrfs/disk-io.c
+++ b/fs/btrfs/disk-io.c
@@ -1296,9 +1296,10 @@ static void __setup_root(u32 nodesize, u32 sectorsize, u32 stripesize,
1296 spin_lock_init(&root->root_item_lock); 1296 spin_lock_init(&root->root_item_lock);
1297} 1297}
1298 1298
1299static struct btrfs_root *btrfs_alloc_root(struct btrfs_fs_info *fs_info) 1299static struct btrfs_root *btrfs_alloc_root(struct btrfs_fs_info *fs_info,
1300 gfp_t flags)
1300{ 1301{
1301 struct btrfs_root *root = kzalloc(sizeof(*root), GFP_NOFS); 1302 struct btrfs_root *root = kzalloc(sizeof(*root), flags);
1302 if (root) 1303 if (root)
1303 root->fs_info = fs_info; 1304 root->fs_info = fs_info;
1304 return root; 1305 return root;
@@ -1310,7 +1311,7 @@ struct btrfs_root *btrfs_alloc_dummy_root(void)
1310{ 1311{
1311 struct btrfs_root *root; 1312 struct btrfs_root *root;
1312 1313
1313 root = btrfs_alloc_root(NULL); 1314 root = btrfs_alloc_root(NULL, GFP_KERNEL);
1314 if (!root) 1315 if (!root)
1315 return ERR_PTR(-ENOMEM); 1316 return ERR_PTR(-ENOMEM);
1316 __setup_root(4096, 4096, 4096, root, NULL, 1); 1317 __setup_root(4096, 4096, 4096, root, NULL, 1);
@@ -1332,7 +1333,7 @@ struct btrfs_root *btrfs_create_tree(struct btrfs_trans_handle *trans,
1332 int ret = 0; 1333 int ret = 0;
1333 uuid_le uuid; 1334 uuid_le uuid;
1334 1335
1335 root = btrfs_alloc_root(fs_info); 1336 root = btrfs_alloc_root(fs_info, GFP_KERNEL);
1336 if (!root) 1337 if (!root)
1337 return ERR_PTR(-ENOMEM); 1338 return ERR_PTR(-ENOMEM);
1338 1339
@@ -1408,7 +1409,7 @@ static struct btrfs_root *alloc_log_tree(struct btrfs_trans_handle *trans,
1408 struct btrfs_root *tree_root = fs_info->tree_root; 1409 struct btrfs_root *tree_root = fs_info->tree_root;
1409 struct extent_buffer *leaf; 1410 struct extent_buffer *leaf;
1410 1411
1411 root = btrfs_alloc_root(fs_info); 1412 root = btrfs_alloc_root(fs_info, GFP_NOFS);
1412 if (!root) 1413 if (!root)
1413 return ERR_PTR(-ENOMEM); 1414 return ERR_PTR(-ENOMEM);
1414 1415
@@ -1506,7 +1507,7 @@ static struct btrfs_root *btrfs_read_tree_root(struct btrfs_root *tree_root,
1506 if (!path) 1507 if (!path)
1507 return ERR_PTR(-ENOMEM); 1508 return ERR_PTR(-ENOMEM);
1508 1509
1509 root = btrfs_alloc_root(fs_info); 1510 root = btrfs_alloc_root(fs_info, GFP_NOFS);
1510 if (!root) { 1511 if (!root) {
1511 ret = -ENOMEM; 1512 ret = -ENOMEM;
1512 goto alloc_fail; 1513 goto alloc_fail;
@@ -2385,7 +2386,7 @@ static int btrfs_replay_log(struct btrfs_fs_info *fs_info,
2385 return -EIO; 2386 return -EIO;
2386 } 2387 }
2387 2388
2388 log_tree_root = btrfs_alloc_root(fs_info); 2389 log_tree_root = btrfs_alloc_root(fs_info, GFP_KERNEL);
2389 if (!log_tree_root) 2390 if (!log_tree_root)
2390 return -ENOMEM; 2391 return -ENOMEM;
2391 2392
@@ -2510,8 +2511,8 @@ int open_ctree(struct super_block *sb,
2510 int backup_index = 0; 2511 int backup_index = 0;
2511 int max_active; 2512 int max_active;
2512 2513
2513 tree_root = fs_info->tree_root = btrfs_alloc_root(fs_info); 2514 tree_root = fs_info->tree_root = btrfs_alloc_root(fs_info, GFP_KERNEL);
2514 chunk_root = fs_info->chunk_root = btrfs_alloc_root(fs_info); 2515 chunk_root = fs_info->chunk_root = btrfs_alloc_root(fs_info, GFP_KERNEL);
2515 if (!tree_root || !chunk_root) { 2516 if (!tree_root || !chunk_root) {
2516 err = -ENOMEM; 2517 err = -ENOMEM;
2517 goto fail; 2518 goto fail;
@@ -2622,7 +2623,7 @@ int open_ctree(struct super_block *sb,
2622 INIT_LIST_HEAD(&fs_info->ordered_roots); 2623 INIT_LIST_HEAD(&fs_info->ordered_roots);
2623 spin_lock_init(&fs_info->ordered_root_lock); 2624 spin_lock_init(&fs_info->ordered_root_lock);
2624 fs_info->delayed_root = kmalloc(sizeof(struct btrfs_delayed_root), 2625 fs_info->delayed_root = kmalloc(sizeof(struct btrfs_delayed_root),
2625 GFP_NOFS); 2626 GFP_KERNEL);
2626 if (!fs_info->delayed_root) { 2627 if (!fs_info->delayed_root) {
2627 err = -ENOMEM; 2628 err = -ENOMEM;
2628 goto fail_iput; 2629 goto fail_iput;
diff --git a/fs/btrfs/extent_io.c b/fs/btrfs/extent_io.c
index 2e7c97a3f344..1b2073389dc2 100644
--- a/fs/btrfs/extent_io.c
+++ b/fs/btrfs/extent_io.c
@@ -3186,7 +3186,8 @@ static int __extent_read_full_page(struct extent_io_tree *tree,
3186 3186
3187 while (1) { 3187 while (1) {
3188 lock_extent(tree, start, end); 3188 lock_extent(tree, start, end);
3189 ordered = btrfs_lookup_ordered_extent(inode, start); 3189 ordered = btrfs_lookup_ordered_range(inode, start,
3190 PAGE_CACHE_SIZE);
3190 if (!ordered) 3191 if (!ordered)
3191 break; 3192 break;
3192 unlock_extent(tree, start, end); 3193 unlock_extent(tree, start, end);
diff --git a/fs/btrfs/file-item.c b/fs/btrfs/file-item.c
index a67e1c828d0f..1c50a7b09b4e 100644
--- a/fs/btrfs/file-item.c
+++ b/fs/btrfs/file-item.c
@@ -172,6 +172,7 @@ static int __btrfs_lookup_bio_sums(struct btrfs_root *root,
172 u64 item_start_offset = 0; 172 u64 item_start_offset = 0;
173 u64 item_last_offset = 0; 173 u64 item_last_offset = 0;
174 u64 disk_bytenr; 174 u64 disk_bytenr;
175 u64 page_bytes_left;
175 u32 diff; 176 u32 diff;
176 int nblocks; 177 int nblocks;
177 int bio_index = 0; 178 int bio_index = 0;
@@ -220,6 +221,8 @@ static int __btrfs_lookup_bio_sums(struct btrfs_root *root,
220 disk_bytenr = (u64)bio->bi_iter.bi_sector << 9; 221 disk_bytenr = (u64)bio->bi_iter.bi_sector << 9;
221 if (dio) 222 if (dio)
222 offset = logical_offset; 223 offset = logical_offset;
224
225 page_bytes_left = bvec->bv_len;
223 while (bio_index < bio->bi_vcnt) { 226 while (bio_index < bio->bi_vcnt) {
224 if (!dio) 227 if (!dio)
225 offset = page_offset(bvec->bv_page) + bvec->bv_offset; 228 offset = page_offset(bvec->bv_page) + bvec->bv_offset;
@@ -243,7 +246,7 @@ static int __btrfs_lookup_bio_sums(struct btrfs_root *root,
243 if (BTRFS_I(inode)->root->root_key.objectid == 246 if (BTRFS_I(inode)->root->root_key.objectid ==
244 BTRFS_DATA_RELOC_TREE_OBJECTID) { 247 BTRFS_DATA_RELOC_TREE_OBJECTID) {
245 set_extent_bits(io_tree, offset, 248 set_extent_bits(io_tree, offset,
246 offset + bvec->bv_len - 1, 249 offset + root->sectorsize - 1,
247 EXTENT_NODATASUM, GFP_NOFS); 250 EXTENT_NODATASUM, GFP_NOFS);
248 } else { 251 } else {
249 btrfs_info(BTRFS_I(inode)->root->fs_info, 252 btrfs_info(BTRFS_I(inode)->root->fs_info,
@@ -281,11 +284,17 @@ static int __btrfs_lookup_bio_sums(struct btrfs_root *root,
281found: 284found:
282 csum += count * csum_size; 285 csum += count * csum_size;
283 nblocks -= count; 286 nblocks -= count;
284 bio_index += count; 287
285 while (count--) { 288 while (count--) {
286 disk_bytenr += bvec->bv_len; 289 disk_bytenr += root->sectorsize;
287 offset += bvec->bv_len; 290 offset += root->sectorsize;
288 bvec++; 291 page_bytes_left -= root->sectorsize;
292 if (!page_bytes_left) {
293 bio_index++;
294 bvec++;
295 page_bytes_left = bvec->bv_len;
296 }
297
289 } 298 }
290 } 299 }
291 btrfs_free_path(path); 300 btrfs_free_path(path);
@@ -432,6 +441,8 @@ int btrfs_csum_one_bio(struct btrfs_root *root, struct inode *inode,
432 struct bio_vec *bvec = bio->bi_io_vec; 441 struct bio_vec *bvec = bio->bi_io_vec;
433 int bio_index = 0; 442 int bio_index = 0;
434 int index; 443 int index;
444 int nr_sectors;
445 int i;
435 unsigned long total_bytes = 0; 446 unsigned long total_bytes = 0;
436 unsigned long this_sum_bytes = 0; 447 unsigned long this_sum_bytes = 0;
437 u64 offset; 448 u64 offset;
@@ -459,41 +470,56 @@ int btrfs_csum_one_bio(struct btrfs_root *root, struct inode *inode,
459 if (!contig) 470 if (!contig)
460 offset = page_offset(bvec->bv_page) + bvec->bv_offset; 471 offset = page_offset(bvec->bv_page) + bvec->bv_offset;
461 472
462 if (offset >= ordered->file_offset + ordered->len || 473 data = kmap_atomic(bvec->bv_page);
463 offset < ordered->file_offset) {
464 unsigned long bytes_left;
465 sums->len = this_sum_bytes;
466 this_sum_bytes = 0;
467 btrfs_add_ordered_sum(inode, ordered, sums);
468 btrfs_put_ordered_extent(ordered);
469 474
470 bytes_left = bio->bi_iter.bi_size - total_bytes; 475 nr_sectors = BTRFS_BYTES_TO_BLKS(root->fs_info,
476 bvec->bv_len + root->sectorsize
477 - 1);
478
479 for (i = 0; i < nr_sectors; i++) {
480 if (offset >= ordered->file_offset + ordered->len ||
481 offset < ordered->file_offset) {
482 unsigned long bytes_left;
483
484 kunmap_atomic(data);
485 sums->len = this_sum_bytes;
486 this_sum_bytes = 0;
487 btrfs_add_ordered_sum(inode, ordered, sums);
488 btrfs_put_ordered_extent(ordered);
489
490 bytes_left = bio->bi_iter.bi_size - total_bytes;
491
492 sums = kzalloc(btrfs_ordered_sum_size(root, bytes_left),
493 GFP_NOFS);
494 BUG_ON(!sums); /* -ENOMEM */
495 sums->len = bytes_left;
496 ordered = btrfs_lookup_ordered_extent(inode,
497 offset);
498 ASSERT(ordered); /* Logic error */
499 sums->bytenr = ((u64)bio->bi_iter.bi_sector << 9)
500 + total_bytes;
501 index = 0;
502
503 data = kmap_atomic(bvec->bv_page);
504 }
471 505
472 sums = kzalloc(btrfs_ordered_sum_size(root, bytes_left), 506 sums->sums[index] = ~(u32)0;
473 GFP_NOFS); 507 sums->sums[index]
474 BUG_ON(!sums); /* -ENOMEM */ 508 = btrfs_csum_data(data + bvec->bv_offset
475 sums->len = bytes_left; 509 + (i * root->sectorsize),
476 ordered = btrfs_lookup_ordered_extent(inode, offset); 510 sums->sums[index],
477 BUG_ON(!ordered); /* Logic error */ 511 root->sectorsize);
478 sums->bytenr = ((u64)bio->bi_iter.bi_sector << 9) + 512 btrfs_csum_final(sums->sums[index],
479 total_bytes; 513 (char *)(sums->sums + index));
480 index = 0; 514 index++;
515 offset += root->sectorsize;
516 this_sum_bytes += root->sectorsize;
517 total_bytes += root->sectorsize;
481 } 518 }
482 519
483 data = kmap_atomic(bvec->bv_page);
484 sums->sums[index] = ~(u32)0;
485 sums->sums[index] = btrfs_csum_data(data + bvec->bv_offset,
486 sums->sums[index],
487 bvec->bv_len);
488 kunmap_atomic(data); 520 kunmap_atomic(data);
489 btrfs_csum_final(sums->sums[index],
490 (char *)(sums->sums + index));
491 521
492 bio_index++; 522 bio_index++;
493 index++;
494 total_bytes += bvec->bv_len;
495 this_sum_bytes += bvec->bv_len;
496 offset += bvec->bv_len;
497 bvec++; 523 bvec++;
498 } 524 }
499 this_sum_bytes = 0; 525 this_sum_bytes = 0;
diff --git a/fs/btrfs/file.c b/fs/btrfs/file.c
index 098bb8f690c9..2d9e4009c7e4 100644
--- a/fs/btrfs/file.c
+++ b/fs/btrfs/file.c
@@ -498,7 +498,7 @@ int btrfs_dirty_pages(struct btrfs_root *root, struct inode *inode,
498 loff_t isize = i_size_read(inode); 498 loff_t isize = i_size_read(inode);
499 499
500 start_pos = pos & ~((u64)root->sectorsize - 1); 500 start_pos = pos & ~((u64)root->sectorsize - 1);
501 num_bytes = ALIGN(write_bytes + pos - start_pos, root->sectorsize); 501 num_bytes = round_up(write_bytes + pos - start_pos, root->sectorsize);
502 502
503 end_of_last_block = start_pos + num_bytes - 1; 503 end_of_last_block = start_pos + num_bytes - 1;
504 err = btrfs_set_extent_delalloc(inode, start_pos, end_of_last_block, 504 err = btrfs_set_extent_delalloc(inode, start_pos, end_of_last_block,
@@ -1379,16 +1379,19 @@ fail:
1379static noinline int 1379static noinline int
1380lock_and_cleanup_extent_if_need(struct inode *inode, struct page **pages, 1380lock_and_cleanup_extent_if_need(struct inode *inode, struct page **pages,
1381 size_t num_pages, loff_t pos, 1381 size_t num_pages, loff_t pos,
1382 size_t write_bytes,
1382 u64 *lockstart, u64 *lockend, 1383 u64 *lockstart, u64 *lockend,
1383 struct extent_state **cached_state) 1384 struct extent_state **cached_state)
1384{ 1385{
1386 struct btrfs_root *root = BTRFS_I(inode)->root;
1385 u64 start_pos; 1387 u64 start_pos;
1386 u64 last_pos; 1388 u64 last_pos;
1387 int i; 1389 int i;
1388 int ret = 0; 1390 int ret = 0;
1389 1391
1390 start_pos = pos & ~((u64)PAGE_CACHE_SIZE - 1); 1392 start_pos = round_down(pos, root->sectorsize);
1391 last_pos = start_pos + ((u64)num_pages << PAGE_CACHE_SHIFT) - 1; 1393 last_pos = start_pos
1394 + round_up(pos + write_bytes - start_pos, root->sectorsize) - 1;
1392 1395
1393 if (start_pos < inode->i_size) { 1396 if (start_pos < inode->i_size) {
1394 struct btrfs_ordered_extent *ordered; 1397 struct btrfs_ordered_extent *ordered;
@@ -1503,6 +1506,7 @@ static noinline ssize_t __btrfs_buffered_write(struct file *file,
1503 1506
1504 while (iov_iter_count(i) > 0) { 1507 while (iov_iter_count(i) > 0) {
1505 size_t offset = pos & (PAGE_CACHE_SIZE - 1); 1508 size_t offset = pos & (PAGE_CACHE_SIZE - 1);
1509 size_t sector_offset;
1506 size_t write_bytes = min(iov_iter_count(i), 1510 size_t write_bytes = min(iov_iter_count(i),
1507 nrptrs * (size_t)PAGE_CACHE_SIZE - 1511 nrptrs * (size_t)PAGE_CACHE_SIZE -
1508 offset); 1512 offset);
@@ -1511,6 +1515,8 @@ static noinline ssize_t __btrfs_buffered_write(struct file *file,
1511 size_t reserve_bytes; 1515 size_t reserve_bytes;
1512 size_t dirty_pages; 1516 size_t dirty_pages;
1513 size_t copied; 1517 size_t copied;
1518 size_t dirty_sectors;
1519 size_t num_sectors;
1514 1520
1515 WARN_ON(num_pages > nrptrs); 1521 WARN_ON(num_pages > nrptrs);
1516 1522
@@ -1523,7 +1529,9 @@ static noinline ssize_t __btrfs_buffered_write(struct file *file,
1523 break; 1529 break;
1524 } 1530 }
1525 1531
1526 reserve_bytes = num_pages << PAGE_CACHE_SHIFT; 1532 sector_offset = pos & (root->sectorsize - 1);
1533 reserve_bytes = round_up(write_bytes + sector_offset,
1534 root->sectorsize);
1527 1535
1528 if (BTRFS_I(inode)->flags & (BTRFS_INODE_NODATACOW | 1536 if (BTRFS_I(inode)->flags & (BTRFS_INODE_NODATACOW |
1529 BTRFS_INODE_PREALLOC)) { 1537 BTRFS_INODE_PREALLOC)) {
@@ -1542,7 +1550,9 @@ static noinline ssize_t __btrfs_buffered_write(struct file *file,
1542 */ 1550 */
1543 num_pages = DIV_ROUND_UP(write_bytes + offset, 1551 num_pages = DIV_ROUND_UP(write_bytes + offset,
1544 PAGE_CACHE_SIZE); 1552 PAGE_CACHE_SIZE);
1545 reserve_bytes = num_pages << PAGE_CACHE_SHIFT; 1553 reserve_bytes = round_up(write_bytes
1554 + sector_offset,
1555 root->sectorsize);
1546 goto reserve_metadata; 1556 goto reserve_metadata;
1547 } 1557 }
1548 } 1558 }
@@ -1576,8 +1586,8 @@ again:
1576 break; 1586 break;
1577 1587
1578 ret = lock_and_cleanup_extent_if_need(inode, pages, num_pages, 1588 ret = lock_and_cleanup_extent_if_need(inode, pages, num_pages,
1579 pos, &lockstart, &lockend, 1589 pos, write_bytes, &lockstart,
1580 &cached_state); 1590 &lockend, &cached_state);
1581 if (ret < 0) { 1591 if (ret < 0) {
1582 if (ret == -EAGAIN) 1592 if (ret == -EAGAIN)
1583 goto again; 1593 goto again;
@@ -1612,9 +1622,16 @@ again:
1612 * we still have an outstanding extent for the chunk we actually 1622 * we still have an outstanding extent for the chunk we actually
1613 * managed to copy. 1623 * managed to copy.
1614 */ 1624 */
1615 if (num_pages > dirty_pages) { 1625 num_sectors = BTRFS_BYTES_TO_BLKS(root->fs_info,
1616 release_bytes = (num_pages - dirty_pages) << 1626 reserve_bytes);
1617 PAGE_CACHE_SHIFT; 1627 dirty_sectors = round_up(copied + sector_offset,
1628 root->sectorsize);
1629 dirty_sectors = BTRFS_BYTES_TO_BLKS(root->fs_info,
1630 dirty_sectors);
1631
1632 if (num_sectors > dirty_sectors) {
1633 release_bytes = (write_bytes - copied)
1634 & ~((u64)root->sectorsize - 1);
1618 if (copied > 0) { 1635 if (copied > 0) {
1619 spin_lock(&BTRFS_I(inode)->lock); 1636 spin_lock(&BTRFS_I(inode)->lock);
1620 BTRFS_I(inode)->outstanding_extents++; 1637 BTRFS_I(inode)->outstanding_extents++;
@@ -1633,7 +1650,8 @@ again:
1633 } 1650 }
1634 } 1651 }
1635 1652
1636 release_bytes = dirty_pages << PAGE_CACHE_SHIFT; 1653 release_bytes = round_up(copied + sector_offset,
1654 root->sectorsize);
1637 1655
1638 if (copied > 0) 1656 if (copied > 0)
1639 ret = btrfs_dirty_pages(root, inode, pages, 1657 ret = btrfs_dirty_pages(root, inode, pages,
@@ -1654,8 +1672,7 @@ again:
1654 1672
1655 if (only_release_metadata && copied > 0) { 1673 if (only_release_metadata && copied > 0) {
1656 lockstart = round_down(pos, root->sectorsize); 1674 lockstart = round_down(pos, root->sectorsize);
1657 lockend = lockstart + 1675 lockend = round_up(pos + copied, root->sectorsize) - 1;
1658 (dirty_pages << PAGE_CACHE_SHIFT) - 1;
1659 1676
1660 set_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, 1677 set_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
1661 lockend, EXTENT_NORESERVE, NULL, 1678 lockend, EXTENT_NORESERVE, NULL,
@@ -1761,6 +1778,8 @@ static ssize_t btrfs_file_write_iter(struct kiocb *iocb,
1761 ssize_t err; 1778 ssize_t err;
1762 loff_t pos; 1779 loff_t pos;
1763 size_t count; 1780 size_t count;
1781 loff_t oldsize;
1782 int clean_page = 0;
1764 1783
1765 inode_lock(inode); 1784 inode_lock(inode);
1766 err = generic_write_checks(iocb, from); 1785 err = generic_write_checks(iocb, from);
@@ -1799,14 +1818,17 @@ static ssize_t btrfs_file_write_iter(struct kiocb *iocb,
1799 pos = iocb->ki_pos; 1818 pos = iocb->ki_pos;
1800 count = iov_iter_count(from); 1819 count = iov_iter_count(from);
1801 start_pos = round_down(pos, root->sectorsize); 1820 start_pos = round_down(pos, root->sectorsize);
1802 if (start_pos > i_size_read(inode)) { 1821 oldsize = i_size_read(inode);
1822 if (start_pos > oldsize) {
1803 /* Expand hole size to cover write data, preventing empty gap */ 1823 /* Expand hole size to cover write data, preventing empty gap */
1804 end_pos = round_up(pos + count, root->sectorsize); 1824 end_pos = round_up(pos + count, root->sectorsize);
1805 err = btrfs_cont_expand(inode, i_size_read(inode), end_pos); 1825 err = btrfs_cont_expand(inode, oldsize, end_pos);
1806 if (err) { 1826 if (err) {
1807 inode_unlock(inode); 1827 inode_unlock(inode);
1808 goto out; 1828 goto out;
1809 } 1829 }
1830 if (start_pos > round_up(oldsize, root->sectorsize))
1831 clean_page = 1;
1810 } 1832 }
1811 1833
1812 if (sync) 1834 if (sync)
@@ -1818,6 +1840,9 @@ static ssize_t btrfs_file_write_iter(struct kiocb *iocb,
1818 num_written = __btrfs_buffered_write(file, from, pos); 1840 num_written = __btrfs_buffered_write(file, from, pos);
1819 if (num_written > 0) 1841 if (num_written > 0)
1820 iocb->ki_pos = pos + num_written; 1842 iocb->ki_pos = pos + num_written;
1843 if (clean_page)
1844 pagecache_isize_extended(inode, oldsize,
1845 i_size_read(inode));
1821 } 1846 }
1822 1847
1823 inode_unlock(inode); 1848 inode_unlock(inode);
@@ -2293,10 +2318,10 @@ static int btrfs_punch_hole(struct inode *inode, loff_t offset, loff_t len)
2293 int ret = 0; 2318 int ret = 0;
2294 int err = 0; 2319 int err = 0;
2295 unsigned int rsv_count; 2320 unsigned int rsv_count;
2296 bool same_page; 2321 bool same_block;
2297 bool no_holes = btrfs_fs_incompat(root->fs_info, NO_HOLES); 2322 bool no_holes = btrfs_fs_incompat(root->fs_info, NO_HOLES);
2298 u64 ino_size; 2323 u64 ino_size;
2299 bool truncated_page = false; 2324 bool truncated_block = false;
2300 bool updated_inode = false; 2325 bool updated_inode = false;
2301 2326
2302 ret = btrfs_wait_ordered_range(inode, offset, len); 2327 ret = btrfs_wait_ordered_range(inode, offset, len);
@@ -2304,7 +2329,7 @@ static int btrfs_punch_hole(struct inode *inode, loff_t offset, loff_t len)
2304 return ret; 2329 return ret;
2305 2330
2306 inode_lock(inode); 2331 inode_lock(inode);
2307 ino_size = round_up(inode->i_size, PAGE_CACHE_SIZE); 2332 ino_size = round_up(inode->i_size, root->sectorsize);
2308 ret = find_first_non_hole(inode, &offset, &len); 2333 ret = find_first_non_hole(inode, &offset, &len);
2309 if (ret < 0) 2334 if (ret < 0)
2310 goto out_only_mutex; 2335 goto out_only_mutex;
@@ -2317,31 +2342,30 @@ static int btrfs_punch_hole(struct inode *inode, loff_t offset, loff_t len)
2317 lockstart = round_up(offset, BTRFS_I(inode)->root->sectorsize); 2342 lockstart = round_up(offset, BTRFS_I(inode)->root->sectorsize);
2318 lockend = round_down(offset + len, 2343 lockend = round_down(offset + len,
2319 BTRFS_I(inode)->root->sectorsize) - 1; 2344 BTRFS_I(inode)->root->sectorsize) - 1;
2320 same_page = ((offset >> PAGE_CACHE_SHIFT) == 2345 same_block = (BTRFS_BYTES_TO_BLKS(root->fs_info, offset))
2321 ((offset + len - 1) >> PAGE_CACHE_SHIFT)); 2346 == (BTRFS_BYTES_TO_BLKS(root->fs_info, offset + len - 1));
2322
2323 /* 2347 /*
2324 * We needn't truncate any page which is beyond the end of the file 2348 * We needn't truncate any block which is beyond the end of the file
2325 * because we are sure there is no data there. 2349 * because we are sure there is no data there.
2326 */ 2350 */
2327 /* 2351 /*
2328 * Only do this if we are in the same page and we aren't doing the 2352 * Only do this if we are in the same block and we aren't doing the
2329 * entire page. 2353 * entire block.
2330 */ 2354 */
2331 if (same_page && len < PAGE_CACHE_SIZE) { 2355 if (same_block && len < root->sectorsize) {
2332 if (offset < ino_size) { 2356 if (offset < ino_size) {
2333 truncated_page = true; 2357 truncated_block = true;
2334 ret = btrfs_truncate_page(inode, offset, len, 0); 2358 ret = btrfs_truncate_block(inode, offset, len, 0);
2335 } else { 2359 } else {
2336 ret = 0; 2360 ret = 0;
2337 } 2361 }
2338 goto out_only_mutex; 2362 goto out_only_mutex;
2339 } 2363 }
2340 2364
2341 /* zero back part of the first page */ 2365 /* zero back part of the first block */
2342 if (offset < ino_size) { 2366 if (offset < ino_size) {
2343 truncated_page = true; 2367 truncated_block = true;
2344 ret = btrfs_truncate_page(inode, offset, 0, 0); 2368 ret = btrfs_truncate_block(inode, offset, 0, 0);
2345 if (ret) { 2369 if (ret) {
2346 inode_unlock(inode); 2370 inode_unlock(inode);
2347 return ret; 2371 return ret;
@@ -2376,9 +2400,10 @@ static int btrfs_punch_hole(struct inode *inode, loff_t offset, loff_t len)
2376 if (!ret) { 2400 if (!ret) {
2377 /* zero the front end of the last page */ 2401 /* zero the front end of the last page */
2378 if (tail_start + tail_len < ino_size) { 2402 if (tail_start + tail_len < ino_size) {
2379 truncated_page = true; 2403 truncated_block = true;
2380 ret = btrfs_truncate_page(inode, 2404 ret = btrfs_truncate_block(inode,
2381 tail_start + tail_len, 0, 1); 2405 tail_start + tail_len,
2406 0, 1);
2382 if (ret) 2407 if (ret)
2383 goto out_only_mutex; 2408 goto out_only_mutex;
2384 } 2409 }
@@ -2558,7 +2583,7 @@ out:
2558 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend, 2583 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
2559 &cached_state, GFP_NOFS); 2584 &cached_state, GFP_NOFS);
2560out_only_mutex: 2585out_only_mutex:
2561 if (!updated_inode && truncated_page && !ret && !err) { 2586 if (!updated_inode && truncated_block && !ret && !err) {
2562 /* 2587 /*
2563 * If we only end up zeroing part of a page, we still need to 2588 * If we only end up zeroing part of a page, we still need to
2564 * update the inode item, so that all the time fields are 2589 * update the inode item, so that all the time fields are
@@ -2611,7 +2636,7 @@ static int add_falloc_range(struct list_head *head, u64 start, u64 len)
2611 return 0; 2636 return 0;
2612 } 2637 }
2613insert: 2638insert:
2614 range = kmalloc(sizeof(*range), GFP_NOFS); 2639 range = kmalloc(sizeof(*range), GFP_KERNEL);
2615 if (!range) 2640 if (!range)
2616 return -ENOMEM; 2641 return -ENOMEM;
2617 range->start = start; 2642 range->start = start;
@@ -2678,10 +2703,10 @@ static long btrfs_fallocate(struct file *file, int mode,
2678 } else if (offset + len > inode->i_size) { 2703 } else if (offset + len > inode->i_size) {
2679 /* 2704 /*
2680 * If we are fallocating from the end of the file onward we 2705 * If we are fallocating from the end of the file onward we
2681 * need to zero out the end of the page if i_size lands in the 2706 * need to zero out the end of the block if i_size lands in the
2682 * middle of a page. 2707 * middle of a block.
2683 */ 2708 */
2684 ret = btrfs_truncate_page(inode, inode->i_size, 0, 0); 2709 ret = btrfs_truncate_block(inode, inode->i_size, 0, 0);
2685 if (ret) 2710 if (ret)
2686 goto out; 2711 goto out;
2687 } 2712 }
@@ -2712,7 +2737,7 @@ static long btrfs_fallocate(struct file *file, int mode,
2712 btrfs_put_ordered_extent(ordered); 2737 btrfs_put_ordered_extent(ordered);
2713 unlock_extent_cached(&BTRFS_I(inode)->io_tree, 2738 unlock_extent_cached(&BTRFS_I(inode)->io_tree,
2714 alloc_start, locked_end, 2739 alloc_start, locked_end,
2715 &cached_state, GFP_NOFS); 2740 &cached_state, GFP_KERNEL);
2716 /* 2741 /*
2717 * we can't wait on the range with the transaction 2742 * we can't wait on the range with the transaction
2718 * running or with the extent lock held 2743 * running or with the extent lock held
@@ -2806,7 +2831,7 @@ static long btrfs_fallocate(struct file *file, int mode,
2806 } 2831 }
2807out_unlock: 2832out_unlock:
2808 unlock_extent_cached(&BTRFS_I(inode)->io_tree, alloc_start, locked_end, 2833 unlock_extent_cached(&BTRFS_I(inode)->io_tree, alloc_start, locked_end,
2809 &cached_state, GFP_NOFS); 2834 &cached_state, GFP_KERNEL);
2810out: 2835out:
2811 /* 2836 /*
2812 * As we waited the extent range, the data_rsv_map must be empty 2837 * As we waited the extent range, the data_rsv_map must be empty
diff --git a/fs/btrfs/inode.c b/fs/btrfs/inode.c
index 5f06eb1f4384..f8be74037f34 100644
--- a/fs/btrfs/inode.c
+++ b/fs/btrfs/inode.c
@@ -263,7 +263,7 @@ static noinline int cow_file_range_inline(struct btrfs_root *root,
263 data_len = compressed_size; 263 data_len = compressed_size;
264 264
265 if (start > 0 || 265 if (start > 0 ||
266 actual_end > PAGE_CACHE_SIZE || 266 actual_end > root->sectorsize ||
267 data_len > BTRFS_MAX_INLINE_DATA_SIZE(root) || 267 data_len > BTRFS_MAX_INLINE_DATA_SIZE(root) ||
268 (!compressed_size && 268 (!compressed_size &&
269 (actual_end & (root->sectorsize - 1)) == 0) || 269 (actual_end & (root->sectorsize - 1)) == 0) ||
@@ -2002,7 +2002,8 @@ again:
2002 if (PagePrivate2(page)) 2002 if (PagePrivate2(page))
2003 goto out; 2003 goto out;
2004 2004
2005 ordered = btrfs_lookup_ordered_extent(inode, page_start); 2005 ordered = btrfs_lookup_ordered_range(inode, page_start,
2006 PAGE_CACHE_SIZE);
2006 if (ordered) { 2007 if (ordered) {
2007 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, 2008 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start,
2008 page_end, &cached_state, GFP_NOFS); 2009 page_end, &cached_state, GFP_NOFS);
@@ -4248,7 +4249,8 @@ static int truncate_inline_extent(struct inode *inode,
4248 * read the extent item from disk (data not in the page cache). 4249 * read the extent item from disk (data not in the page cache).
4249 */ 4250 */
4250 btrfs_release_path(path); 4251 btrfs_release_path(path);
4251 return btrfs_truncate_page(inode, offset, page_end - offset, 0); 4252 return btrfs_truncate_block(inode, offset, page_end - offset,
4253 0);
4252 } 4254 }
4253 4255
4254 btrfs_set_file_extent_ram_bytes(leaf, fi, size); 4256 btrfs_set_file_extent_ram_bytes(leaf, fi, size);
@@ -4601,17 +4603,17 @@ error:
4601} 4603}
4602 4604
4603/* 4605/*
4604 * btrfs_truncate_page - read, zero a chunk and write a page 4606 * btrfs_truncate_block - read, zero a chunk and write a block
4605 * @inode - inode that we're zeroing 4607 * @inode - inode that we're zeroing
4606 * @from - the offset to start zeroing 4608 * @from - the offset to start zeroing
4607 * @len - the length to zero, 0 to zero the entire range respective to the 4609 * @len - the length to zero, 0 to zero the entire range respective to the
4608 * offset 4610 * offset
4609 * @front - zero up to the offset instead of from the offset on 4611 * @front - zero up to the offset instead of from the offset on
4610 * 4612 *
4611 * This will find the page for the "from" offset and cow the page and zero the 4613 * This will find the block for the "from" offset and cow the block and zero the
4612 * part we want to zero. This is used with truncate and hole punching. 4614 * part we want to zero. This is used with truncate and hole punching.
4613 */ 4615 */
4614int btrfs_truncate_page(struct inode *inode, loff_t from, loff_t len, 4616int btrfs_truncate_block(struct inode *inode, loff_t from, loff_t len,
4615 int front) 4617 int front)
4616{ 4618{
4617 struct address_space *mapping = inode->i_mapping; 4619 struct address_space *mapping = inode->i_mapping;
@@ -4622,18 +4624,19 @@ int btrfs_truncate_page(struct inode *inode, loff_t from, loff_t len,
4622 char *kaddr; 4624 char *kaddr;
4623 u32 blocksize = root->sectorsize; 4625 u32 blocksize = root->sectorsize;
4624 pgoff_t index = from >> PAGE_CACHE_SHIFT; 4626 pgoff_t index = from >> PAGE_CACHE_SHIFT;
4625 unsigned offset = from & (PAGE_CACHE_SIZE-1); 4627 unsigned offset = from & (blocksize - 1);
4626 struct page *page; 4628 struct page *page;
4627 gfp_t mask = btrfs_alloc_write_mask(mapping); 4629 gfp_t mask = btrfs_alloc_write_mask(mapping);
4628 int ret = 0; 4630 int ret = 0;
4629 u64 page_start; 4631 u64 block_start;
4630 u64 page_end; 4632 u64 block_end;
4631 4633
4632 if ((offset & (blocksize - 1)) == 0 && 4634 if ((offset & (blocksize - 1)) == 0 &&
4633 (!len || ((len & (blocksize - 1)) == 0))) 4635 (!len || ((len & (blocksize - 1)) == 0)))
4634 goto out; 4636 goto out;
4637
4635 ret = btrfs_delalloc_reserve_space(inode, 4638 ret = btrfs_delalloc_reserve_space(inode,
4636 round_down(from, PAGE_CACHE_SIZE), PAGE_CACHE_SIZE); 4639 round_down(from, blocksize), blocksize);
4637 if (ret) 4640 if (ret)
4638 goto out; 4641 goto out;
4639 4642
@@ -4641,14 +4644,14 @@ again:
4641 page = find_or_create_page(mapping, index, mask); 4644 page = find_or_create_page(mapping, index, mask);
4642 if (!page) { 4645 if (!page) {
4643 btrfs_delalloc_release_space(inode, 4646 btrfs_delalloc_release_space(inode,
4644 round_down(from, PAGE_CACHE_SIZE), 4647 round_down(from, blocksize),
4645 PAGE_CACHE_SIZE); 4648 blocksize);
4646 ret = -ENOMEM; 4649 ret = -ENOMEM;
4647 goto out; 4650 goto out;
4648 } 4651 }
4649 4652
4650 page_start = page_offset(page); 4653 block_start = round_down(from, blocksize);
4651 page_end = page_start + PAGE_CACHE_SIZE - 1; 4654 block_end = block_start + blocksize - 1;
4652 4655
4653 if (!PageUptodate(page)) { 4656 if (!PageUptodate(page)) {
4654 ret = btrfs_readpage(NULL, page); 4657 ret = btrfs_readpage(NULL, page);
@@ -4665,12 +4668,12 @@ again:
4665 } 4668 }
4666 wait_on_page_writeback(page); 4669 wait_on_page_writeback(page);
4667 4670
4668 lock_extent_bits(io_tree, page_start, page_end, &cached_state); 4671 lock_extent_bits(io_tree, block_start, block_end, &cached_state);
4669 set_page_extent_mapped(page); 4672 set_page_extent_mapped(page);
4670 4673
4671 ordered = btrfs_lookup_ordered_extent(inode, page_start); 4674 ordered = btrfs_lookup_ordered_extent(inode, block_start);
4672 if (ordered) { 4675 if (ordered) {
4673 unlock_extent_cached(io_tree, page_start, page_end, 4676 unlock_extent_cached(io_tree, block_start, block_end,
4674 &cached_state, GFP_NOFS); 4677 &cached_state, GFP_NOFS);
4675 unlock_page(page); 4678 unlock_page(page);
4676 page_cache_release(page); 4679 page_cache_release(page);
@@ -4679,39 +4682,41 @@ again:
4679 goto again; 4682 goto again;
4680 } 4683 }
4681 4684
4682 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end, 4685 clear_extent_bit(&BTRFS_I(inode)->io_tree, block_start, block_end,
4683 EXTENT_DIRTY | EXTENT_DELALLOC | 4686 EXTENT_DIRTY | EXTENT_DELALLOC |
4684 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 4687 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
4685 0, 0, &cached_state, GFP_NOFS); 4688 0, 0, &cached_state, GFP_NOFS);
4686 4689
4687 ret = btrfs_set_extent_delalloc(inode, page_start, page_end, 4690 ret = btrfs_set_extent_delalloc(inode, block_start, block_end,
4688 &cached_state); 4691 &cached_state);
4689 if (ret) { 4692 if (ret) {
4690 unlock_extent_cached(io_tree, page_start, page_end, 4693 unlock_extent_cached(io_tree, block_start, block_end,
4691 &cached_state, GFP_NOFS); 4694 &cached_state, GFP_NOFS);
4692 goto out_unlock; 4695 goto out_unlock;
4693 } 4696 }
4694 4697
4695 if (offset != PAGE_CACHE_SIZE) { 4698 if (offset != blocksize) {
4696 if (!len) 4699 if (!len)
4697 len = PAGE_CACHE_SIZE - offset; 4700 len = blocksize - offset;
4698 kaddr = kmap(page); 4701 kaddr = kmap(page);
4699 if (front) 4702 if (front)
4700 memset(kaddr, 0, offset); 4703 memset(kaddr + (block_start - page_offset(page)),
4704 0, offset);
4701 else 4705 else
4702 memset(kaddr + offset, 0, len); 4706 memset(kaddr + (block_start - page_offset(page)) + offset,
4707 0, len);
4703 flush_dcache_page(page); 4708 flush_dcache_page(page);
4704 kunmap(page); 4709 kunmap(page);
4705 } 4710 }
4706 ClearPageChecked(page); 4711 ClearPageChecked(page);
4707 set_page_dirty(page); 4712 set_page_dirty(page);
4708 unlock_extent_cached(io_tree, page_start, page_end, &cached_state, 4713 unlock_extent_cached(io_tree, block_start, block_end, &cached_state,
4709 GFP_NOFS); 4714 GFP_NOFS);
4710 4715
4711out_unlock: 4716out_unlock:
4712 if (ret) 4717 if (ret)
4713 btrfs_delalloc_release_space(inode, page_start, 4718 btrfs_delalloc_release_space(inode, block_start,
4714 PAGE_CACHE_SIZE); 4719 blocksize);
4715 unlock_page(page); 4720 unlock_page(page);
4716 page_cache_release(page); 4721 page_cache_release(page);
4717out: 4722out:
@@ -4782,11 +4787,11 @@ int btrfs_cont_expand(struct inode *inode, loff_t oldsize, loff_t size)
4782 int err = 0; 4787 int err = 0;
4783 4788
4784 /* 4789 /*
4785 * If our size started in the middle of a page we need to zero out the 4790 * If our size started in the middle of a block we need to zero out the
4786 * rest of the page before we expand the i_size, otherwise we could 4791 * rest of the block before we expand the i_size, otherwise we could
4787 * expose stale data. 4792 * expose stale data.
4788 */ 4793 */
4789 err = btrfs_truncate_page(inode, oldsize, 0, 0); 4794 err = btrfs_truncate_block(inode, oldsize, 0, 0);
4790 if (err) 4795 if (err)
4791 return err; 4796 return err;
4792 4797
@@ -4895,7 +4900,6 @@ static int btrfs_setsize(struct inode *inode, struct iattr *attr)
4895 } 4900 }
4896 4901
4897 if (newsize > oldsize) { 4902 if (newsize > oldsize) {
4898 truncate_pagecache(inode, newsize);
4899 /* 4903 /*
4900 * Don't do an expanding truncate while snapshoting is ongoing. 4904 * Don't do an expanding truncate while snapshoting is ongoing.
4901 * This is to ensure the snapshot captures a fully consistent 4905 * This is to ensure the snapshot captures a fully consistent
@@ -4918,6 +4922,7 @@ static int btrfs_setsize(struct inode *inode, struct iattr *attr)
4918 4922
4919 i_size_write(inode, newsize); 4923 i_size_write(inode, newsize);
4920 btrfs_ordered_update_i_size(inode, i_size_read(inode), NULL); 4924 btrfs_ordered_update_i_size(inode, i_size_read(inode), NULL);
4925 pagecache_isize_extended(inode, oldsize, newsize);
4921 ret = btrfs_update_inode(trans, root, inode); 4926 ret = btrfs_update_inode(trans, root, inode);
4922 btrfs_end_write_no_snapshoting(root); 4927 btrfs_end_write_no_snapshoting(root);
4923 btrfs_end_transaction(trans, root); 4928 btrfs_end_transaction(trans, root);
@@ -5788,7 +5793,7 @@ static int btrfs_real_readdir(struct file *file, struct dir_context *ctx)
5788 if (name_len <= sizeof(tmp_name)) { 5793 if (name_len <= sizeof(tmp_name)) {
5789 name_ptr = tmp_name; 5794 name_ptr = tmp_name;
5790 } else { 5795 } else {
5791 name_ptr = kmalloc(name_len, GFP_NOFS); 5796 name_ptr = kmalloc(name_len, GFP_KERNEL);
5792 if (!name_ptr) { 5797 if (!name_ptr) {
5793 ret = -ENOMEM; 5798 ret = -ENOMEM;
5794 goto err; 5799 goto err;
@@ -7752,9 +7757,9 @@ static int btrfs_check_dio_repairable(struct inode *inode,
7752} 7757}
7753 7758
7754static int dio_read_error(struct inode *inode, struct bio *failed_bio, 7759static int dio_read_error(struct inode *inode, struct bio *failed_bio,
7755 struct page *page, u64 start, u64 end, 7760 struct page *page, unsigned int pgoff,
7756 int failed_mirror, bio_end_io_t *repair_endio, 7761 u64 start, u64 end, int failed_mirror,
7757 void *repair_arg) 7762 bio_end_io_t *repair_endio, void *repair_arg)
7758{ 7763{
7759 struct io_failure_record *failrec; 7764 struct io_failure_record *failrec;
7760 struct bio *bio; 7765 struct bio *bio;
@@ -7775,7 +7780,9 @@ static int dio_read_error(struct inode *inode, struct bio *failed_bio,
7775 return -EIO; 7780 return -EIO;
7776 } 7781 }
7777 7782
7778 if (failed_bio->bi_vcnt > 1) 7783 if ((failed_bio->bi_vcnt > 1)
7784 || (failed_bio->bi_io_vec->bv_len
7785 > BTRFS_I(inode)->root->sectorsize))
7779 read_mode = READ_SYNC | REQ_FAILFAST_DEV; 7786 read_mode = READ_SYNC | REQ_FAILFAST_DEV;
7780 else 7787 else
7781 read_mode = READ_SYNC; 7788 read_mode = READ_SYNC;
@@ -7783,7 +7790,7 @@ static int dio_read_error(struct inode *inode, struct bio *failed_bio,
7783 isector = start - btrfs_io_bio(failed_bio)->logical; 7790 isector = start - btrfs_io_bio(failed_bio)->logical;
7784 isector >>= inode->i_sb->s_blocksize_bits; 7791 isector >>= inode->i_sb->s_blocksize_bits;
7785 bio = btrfs_create_repair_bio(inode, failed_bio, failrec, page, 7792 bio = btrfs_create_repair_bio(inode, failed_bio, failrec, page,
7786 0, isector, repair_endio, repair_arg); 7793 pgoff, isector, repair_endio, repair_arg);
7787 if (!bio) { 7794 if (!bio) {
7788 free_io_failure(inode, failrec); 7795 free_io_failure(inode, failrec);
7789 return -EIO; 7796 return -EIO;
@@ -7813,12 +7820,17 @@ struct btrfs_retry_complete {
7813static void btrfs_retry_endio_nocsum(struct bio *bio) 7820static void btrfs_retry_endio_nocsum(struct bio *bio)
7814{ 7821{
7815 struct btrfs_retry_complete *done = bio->bi_private; 7822 struct btrfs_retry_complete *done = bio->bi_private;
7823 struct inode *inode;
7816 struct bio_vec *bvec; 7824 struct bio_vec *bvec;
7817 int i; 7825 int i;
7818 7826
7819 if (bio->bi_error) 7827 if (bio->bi_error)
7820 goto end; 7828 goto end;
7821 7829
7830 ASSERT(bio->bi_vcnt == 1);
7831 inode = bio->bi_io_vec->bv_page->mapping->host;
7832 ASSERT(bio->bi_io_vec->bv_len == BTRFS_I(inode)->root->sectorsize);
7833
7822 done->uptodate = 1; 7834 done->uptodate = 1;
7823 bio_for_each_segment_all(bvec, bio, i) 7835 bio_for_each_segment_all(bvec, bio, i)
7824 clean_io_failure(done->inode, done->start, bvec->bv_page, 0); 7836 clean_io_failure(done->inode, done->start, bvec->bv_page, 0);
@@ -7830,25 +7842,35 @@ end:
7830static int __btrfs_correct_data_nocsum(struct inode *inode, 7842static int __btrfs_correct_data_nocsum(struct inode *inode,
7831 struct btrfs_io_bio *io_bio) 7843 struct btrfs_io_bio *io_bio)
7832{ 7844{
7845 struct btrfs_fs_info *fs_info;
7833 struct bio_vec *bvec; 7846 struct bio_vec *bvec;
7834 struct btrfs_retry_complete done; 7847 struct btrfs_retry_complete done;
7835 u64 start; 7848 u64 start;
7849 unsigned int pgoff;
7850 u32 sectorsize;
7851 int nr_sectors;
7836 int i; 7852 int i;
7837 int ret; 7853 int ret;
7838 7854
7855 fs_info = BTRFS_I(inode)->root->fs_info;
7856 sectorsize = BTRFS_I(inode)->root->sectorsize;
7857
7839 start = io_bio->logical; 7858 start = io_bio->logical;
7840 done.inode = inode; 7859 done.inode = inode;
7841 7860
7842 bio_for_each_segment_all(bvec, &io_bio->bio, i) { 7861 bio_for_each_segment_all(bvec, &io_bio->bio, i) {
7843try_again: 7862 nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec->bv_len);
7863 pgoff = bvec->bv_offset;
7864
7865next_block_or_try_again:
7844 done.uptodate = 0; 7866 done.uptodate = 0;
7845 done.start = start; 7867 done.start = start;
7846 init_completion(&done.done); 7868 init_completion(&done.done);
7847 7869
7848 ret = dio_read_error(inode, &io_bio->bio, bvec->bv_page, start, 7870 ret = dio_read_error(inode, &io_bio->bio, bvec->bv_page,
7849 start + bvec->bv_len - 1, 7871 pgoff, start, start + sectorsize - 1,
7850 io_bio->mirror_num, 7872 io_bio->mirror_num,
7851 btrfs_retry_endio_nocsum, &done); 7873 btrfs_retry_endio_nocsum, &done);
7852 if (ret) 7874 if (ret)
7853 return ret; 7875 return ret;
7854 7876
@@ -7856,10 +7878,15 @@ try_again:
7856 7878
7857 if (!done.uptodate) { 7879 if (!done.uptodate) {
7858 /* We might have another mirror, so try again */ 7880 /* We might have another mirror, so try again */
7859 goto try_again; 7881 goto next_block_or_try_again;
7860 } 7882 }
7861 7883
7862 start += bvec->bv_len; 7884 start += sectorsize;
7885
7886 if (nr_sectors--) {
7887 pgoff += sectorsize;
7888 goto next_block_or_try_again;
7889 }
7863 } 7890 }
7864 7891
7865 return 0; 7892 return 0;
@@ -7869,7 +7896,9 @@ static void btrfs_retry_endio(struct bio *bio)
7869{ 7896{
7870 struct btrfs_retry_complete *done = bio->bi_private; 7897 struct btrfs_retry_complete *done = bio->bi_private;
7871 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio); 7898 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
7899 struct inode *inode;
7872 struct bio_vec *bvec; 7900 struct bio_vec *bvec;
7901 u64 start;
7873 int uptodate; 7902 int uptodate;
7874 int ret; 7903 int ret;
7875 int i; 7904 int i;
@@ -7878,13 +7907,20 @@ static void btrfs_retry_endio(struct bio *bio)
7878 goto end; 7907 goto end;
7879 7908
7880 uptodate = 1; 7909 uptodate = 1;
7910
7911 start = done->start;
7912
7913 ASSERT(bio->bi_vcnt == 1);
7914 inode = bio->bi_io_vec->bv_page->mapping->host;
7915 ASSERT(bio->bi_io_vec->bv_len == BTRFS_I(inode)->root->sectorsize);
7916
7881 bio_for_each_segment_all(bvec, bio, i) { 7917 bio_for_each_segment_all(bvec, bio, i) {
7882 ret = __readpage_endio_check(done->inode, io_bio, i, 7918 ret = __readpage_endio_check(done->inode, io_bio, i,
7883 bvec->bv_page, 0, 7919 bvec->bv_page, bvec->bv_offset,
7884 done->start, bvec->bv_len); 7920 done->start, bvec->bv_len);
7885 if (!ret) 7921 if (!ret)
7886 clean_io_failure(done->inode, done->start, 7922 clean_io_failure(done->inode, done->start,
7887 bvec->bv_page, 0); 7923 bvec->bv_page, bvec->bv_offset);
7888 else 7924 else
7889 uptodate = 0; 7925 uptodate = 0;
7890 } 7926 }
@@ -7898,20 +7934,34 @@ end:
7898static int __btrfs_subio_endio_read(struct inode *inode, 7934static int __btrfs_subio_endio_read(struct inode *inode,
7899 struct btrfs_io_bio *io_bio, int err) 7935 struct btrfs_io_bio *io_bio, int err)
7900{ 7936{
7937 struct btrfs_fs_info *fs_info;
7901 struct bio_vec *bvec; 7938 struct bio_vec *bvec;
7902 struct btrfs_retry_complete done; 7939 struct btrfs_retry_complete done;
7903 u64 start; 7940 u64 start;
7904 u64 offset = 0; 7941 u64 offset = 0;
7942 u32 sectorsize;
7943 int nr_sectors;
7944 unsigned int pgoff;
7945 int csum_pos;
7905 int i; 7946 int i;
7906 int ret; 7947 int ret;
7907 7948
7949 fs_info = BTRFS_I(inode)->root->fs_info;
7950 sectorsize = BTRFS_I(inode)->root->sectorsize;
7951
7908 err = 0; 7952 err = 0;
7909 start = io_bio->logical; 7953 start = io_bio->logical;
7910 done.inode = inode; 7954 done.inode = inode;
7911 7955
7912 bio_for_each_segment_all(bvec, &io_bio->bio, i) { 7956 bio_for_each_segment_all(bvec, &io_bio->bio, i) {
7913 ret = __readpage_endio_check(inode, io_bio, i, bvec->bv_page, 7957 nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec->bv_len);
7914 0, start, bvec->bv_len); 7958
7959 pgoff = bvec->bv_offset;
7960next_block:
7961 csum_pos = BTRFS_BYTES_TO_BLKS(fs_info, offset);
7962 ret = __readpage_endio_check(inode, io_bio, csum_pos,
7963 bvec->bv_page, pgoff, start,
7964 sectorsize);
7915 if (likely(!ret)) 7965 if (likely(!ret))
7916 goto next; 7966 goto next;
7917try_again: 7967try_again:
@@ -7919,10 +7969,10 @@ try_again:
7919 done.start = start; 7969 done.start = start;
7920 init_completion(&done.done); 7970 init_completion(&done.done);
7921 7971
7922 ret = dio_read_error(inode, &io_bio->bio, bvec->bv_page, start, 7972 ret = dio_read_error(inode, &io_bio->bio, bvec->bv_page,
7923 start + bvec->bv_len - 1, 7973 pgoff, start, start + sectorsize - 1,
7924 io_bio->mirror_num, 7974 io_bio->mirror_num,
7925 btrfs_retry_endio, &done); 7975 btrfs_retry_endio, &done);
7926 if (ret) { 7976 if (ret) {
7927 err = ret; 7977 err = ret;
7928 goto next; 7978 goto next;
@@ -7935,8 +7985,15 @@ try_again:
7935 goto try_again; 7985 goto try_again;
7936 } 7986 }
7937next: 7987next:
7938 offset += bvec->bv_len; 7988 offset += sectorsize;
7939 start += bvec->bv_len; 7989 start += sectorsize;
7990
7991 ASSERT(nr_sectors);
7992
7993 if (--nr_sectors) {
7994 pgoff += sectorsize;
7995 goto next_block;
7996 }
7940 } 7997 }
7941 7998
7942 return err; 7999 return err;
@@ -8188,9 +8245,11 @@ static int btrfs_submit_direct_hook(int rw, struct btrfs_dio_private *dip,
8188 u64 file_offset = dip->logical_offset; 8245 u64 file_offset = dip->logical_offset;
8189 u64 submit_len = 0; 8246 u64 submit_len = 0;
8190 u64 map_length; 8247 u64 map_length;
8191 int nr_pages = 0; 8248 u32 blocksize = root->sectorsize;
8192 int ret;
8193 int async_submit = 0; 8249 int async_submit = 0;
8250 int nr_sectors;
8251 int ret;
8252 int i;
8194 8253
8195 map_length = orig_bio->bi_iter.bi_size; 8254 map_length = orig_bio->bi_iter.bi_size;
8196 ret = btrfs_map_block(root->fs_info, rw, start_sector << 9, 8255 ret = btrfs_map_block(root->fs_info, rw, start_sector << 9,
@@ -8220,9 +8279,12 @@ static int btrfs_submit_direct_hook(int rw, struct btrfs_dio_private *dip,
8220 atomic_inc(&dip->pending_bios); 8279 atomic_inc(&dip->pending_bios);
8221 8280
8222 while (bvec <= (orig_bio->bi_io_vec + orig_bio->bi_vcnt - 1)) { 8281 while (bvec <= (orig_bio->bi_io_vec + orig_bio->bi_vcnt - 1)) {
8223 if (map_length < submit_len + bvec->bv_len || 8282 nr_sectors = BTRFS_BYTES_TO_BLKS(root->fs_info, bvec->bv_len);
8224 bio_add_page(bio, bvec->bv_page, bvec->bv_len, 8283 i = 0;
8225 bvec->bv_offset) < bvec->bv_len) { 8284next_block:
8285 if (unlikely(map_length < submit_len + blocksize ||
8286 bio_add_page(bio, bvec->bv_page, blocksize,
8287 bvec->bv_offset + (i * blocksize)) < blocksize)) {
8226 /* 8288 /*
8227 * inc the count before we submit the bio so 8289 * inc the count before we submit the bio so
8228 * we know the end IO handler won't happen before 8290 * we know the end IO handler won't happen before
@@ -8243,7 +8305,6 @@ static int btrfs_submit_direct_hook(int rw, struct btrfs_dio_private *dip,
8243 file_offset += submit_len; 8305 file_offset += submit_len;
8244 8306
8245 submit_len = 0; 8307 submit_len = 0;
8246 nr_pages = 0;
8247 8308
8248 bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev, 8309 bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev,
8249 start_sector, GFP_NOFS); 8310 start_sector, GFP_NOFS);
@@ -8261,9 +8322,14 @@ static int btrfs_submit_direct_hook(int rw, struct btrfs_dio_private *dip,
8261 bio_put(bio); 8322 bio_put(bio);
8262 goto out_err; 8323 goto out_err;
8263 } 8324 }
8325
8326 goto next_block;
8264 } else { 8327 } else {
8265 submit_len += bvec->bv_len; 8328 submit_len += blocksize;
8266 nr_pages++; 8329 if (--nr_sectors) {
8330 i++;
8331 goto next_block;
8332 }
8267 bvec++; 8333 bvec++;
8268 } 8334 }
8269 } 8335 }
@@ -8628,6 +8694,8 @@ static void btrfs_invalidatepage(struct page *page, unsigned int offset,
8628 struct extent_state *cached_state = NULL; 8694 struct extent_state *cached_state = NULL;
8629 u64 page_start = page_offset(page); 8695 u64 page_start = page_offset(page);
8630 u64 page_end = page_start + PAGE_CACHE_SIZE - 1; 8696 u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
8697 u64 start;
8698 u64 end;
8631 int inode_evicting = inode->i_state & I_FREEING; 8699 int inode_evicting = inode->i_state & I_FREEING;
8632 8700
8633 /* 8701 /*
@@ -8647,14 +8715,18 @@ static void btrfs_invalidatepage(struct page *page, unsigned int offset,
8647 8715
8648 if (!inode_evicting) 8716 if (!inode_evicting)
8649 lock_extent_bits(tree, page_start, page_end, &cached_state); 8717 lock_extent_bits(tree, page_start, page_end, &cached_state);
8650 ordered = btrfs_lookup_ordered_extent(inode, page_start); 8718again:
8719 start = page_start;
8720 ordered = btrfs_lookup_ordered_range(inode, start,
8721 page_end - start + 1);
8651 if (ordered) { 8722 if (ordered) {
8723 end = min(page_end, ordered->file_offset + ordered->len - 1);
8652 /* 8724 /*
8653 * IO on this page will never be started, so we need 8725 * IO on this page will never be started, so we need
8654 * to account for any ordered extents now 8726 * to account for any ordered extents now
8655 */ 8727 */
8656 if (!inode_evicting) 8728 if (!inode_evicting)
8657 clear_extent_bit(tree, page_start, page_end, 8729 clear_extent_bit(tree, start, end,
8658 EXTENT_DIRTY | EXTENT_DELALLOC | 8730 EXTENT_DIRTY | EXTENT_DELALLOC |
8659 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING | 8731 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING |
8660 EXTENT_DEFRAG, 1, 0, &cached_state, 8732 EXTENT_DEFRAG, 1, 0, &cached_state,
@@ -8671,22 +8743,26 @@ static void btrfs_invalidatepage(struct page *page, unsigned int offset,
8671 8743
8672 spin_lock_irq(&tree->lock); 8744 spin_lock_irq(&tree->lock);
8673 set_bit(BTRFS_ORDERED_TRUNCATED, &ordered->flags); 8745 set_bit(BTRFS_ORDERED_TRUNCATED, &ordered->flags);
8674 new_len = page_start - ordered->file_offset; 8746 new_len = start - ordered->file_offset;
8675 if (new_len < ordered->truncated_len) 8747 if (new_len < ordered->truncated_len)
8676 ordered->truncated_len = new_len; 8748 ordered->truncated_len = new_len;
8677 spin_unlock_irq(&tree->lock); 8749 spin_unlock_irq(&tree->lock);
8678 8750
8679 if (btrfs_dec_test_ordered_pending(inode, &ordered, 8751 if (btrfs_dec_test_ordered_pending(inode, &ordered,
8680 page_start, 8752 start,
8681 PAGE_CACHE_SIZE, 1)) 8753 end - start + 1, 1))
8682 btrfs_finish_ordered_io(ordered); 8754 btrfs_finish_ordered_io(ordered);
8683 } 8755 }
8684 btrfs_put_ordered_extent(ordered); 8756 btrfs_put_ordered_extent(ordered);
8685 if (!inode_evicting) { 8757 if (!inode_evicting) {
8686 cached_state = NULL; 8758 cached_state = NULL;
8687 lock_extent_bits(tree, page_start, page_end, 8759 lock_extent_bits(tree, start, end,
8688 &cached_state); 8760 &cached_state);
8689 } 8761 }
8762
8763 start = end + 1;
8764 if (start < page_end)
8765 goto again;
8690 } 8766 }
8691 8767
8692 /* 8768 /*
@@ -8747,15 +8823,28 @@ int btrfs_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
8747 loff_t size; 8823 loff_t size;
8748 int ret; 8824 int ret;
8749 int reserved = 0; 8825 int reserved = 0;
8826 u64 reserved_space;
8750 u64 page_start; 8827 u64 page_start;
8751 u64 page_end; 8828 u64 page_end;
8829 u64 end;
8830
8831 reserved_space = PAGE_CACHE_SIZE;
8752 8832
8753 sb_start_pagefault(inode->i_sb); 8833 sb_start_pagefault(inode->i_sb);
8754 page_start = page_offset(page); 8834 page_start = page_offset(page);
8755 page_end = page_start + PAGE_CACHE_SIZE - 1; 8835 page_end = page_start + PAGE_CACHE_SIZE - 1;
8836 end = page_end;
8756 8837
8838 /*
8839 * Reserving delalloc space after obtaining the page lock can lead to
8840 * deadlock. For example, if a dirty page is locked by this function
8841 * and the call to btrfs_delalloc_reserve_space() ends up triggering
8842 * dirty page write out, then the btrfs_writepage() function could
8843 * end up waiting indefinitely to get a lock on the page currently
8844 * being processed by btrfs_page_mkwrite() function.
8845 */
8757 ret = btrfs_delalloc_reserve_space(inode, page_start, 8846 ret = btrfs_delalloc_reserve_space(inode, page_start,
8758 PAGE_CACHE_SIZE); 8847 reserved_space);
8759 if (!ret) { 8848 if (!ret) {
8760 ret = file_update_time(vma->vm_file); 8849 ret = file_update_time(vma->vm_file);
8761 reserved = 1; 8850 reserved = 1;
@@ -8789,7 +8878,7 @@ again:
8789 * we can't set the delalloc bits if there are pending ordered 8878 * we can't set the delalloc bits if there are pending ordered
8790 * extents. Drop our locks and wait for them to finish 8879 * extents. Drop our locks and wait for them to finish
8791 */ 8880 */
8792 ordered = btrfs_lookup_ordered_extent(inode, page_start); 8881 ordered = btrfs_lookup_ordered_range(inode, page_start, page_end);
8793 if (ordered) { 8882 if (ordered) {
8794 unlock_extent_cached(io_tree, page_start, page_end, 8883 unlock_extent_cached(io_tree, page_start, page_end,
8795 &cached_state, GFP_NOFS); 8884 &cached_state, GFP_NOFS);
@@ -8799,6 +8888,18 @@ again:
8799 goto again; 8888 goto again;
8800 } 8889 }
8801 8890
8891 if (page->index == ((size - 1) >> PAGE_CACHE_SHIFT)) {
8892 reserved_space = round_up(size - page_start, root->sectorsize);
8893 if (reserved_space < PAGE_CACHE_SIZE) {
8894 end = page_start + reserved_space - 1;
8895 spin_lock(&BTRFS_I(inode)->lock);
8896 BTRFS_I(inode)->outstanding_extents++;
8897 spin_unlock(&BTRFS_I(inode)->lock);
8898 btrfs_delalloc_release_space(inode, page_start,
8899 PAGE_CACHE_SIZE - reserved_space);
8900 }
8901 }
8902
8802 /* 8903 /*
8803 * XXX - page_mkwrite gets called every time the page is dirtied, even 8904 * XXX - page_mkwrite gets called every time the page is dirtied, even
8804 * if it was already dirty, so for space accounting reasons we need to 8905 * if it was already dirty, so for space accounting reasons we need to
@@ -8806,12 +8907,12 @@ again:
8806 * is probably a better way to do this, but for now keep consistent with 8907 * is probably a better way to do this, but for now keep consistent with
8807 * prepare_pages in the normal write path. 8908 * prepare_pages in the normal write path.
8808 */ 8909 */
8809 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end, 8910 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, end,
8810 EXTENT_DIRTY | EXTENT_DELALLOC | 8911 EXTENT_DIRTY | EXTENT_DELALLOC |
8811 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 8912 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
8812 0, 0, &cached_state, GFP_NOFS); 8913 0, 0, &cached_state, GFP_NOFS);
8813 8914
8814 ret = btrfs_set_extent_delalloc(inode, page_start, page_end, 8915 ret = btrfs_set_extent_delalloc(inode, page_start, end,
8815 &cached_state); 8916 &cached_state);
8816 if (ret) { 8917 if (ret) {
8817 unlock_extent_cached(io_tree, page_start, page_end, 8918 unlock_extent_cached(io_tree, page_start, page_end,
@@ -8850,7 +8951,7 @@ out_unlock:
8850 } 8951 }
8851 unlock_page(page); 8952 unlock_page(page);
8852out: 8953out:
8853 btrfs_delalloc_release_space(inode, page_start, PAGE_CACHE_SIZE); 8954 btrfs_delalloc_release_space(inode, page_start, reserved_space);
8854out_noreserve: 8955out_noreserve:
8855 sb_end_pagefault(inode->i_sb); 8956 sb_end_pagefault(inode->i_sb);
8856 return ret; 8957 return ret;
@@ -9236,7 +9337,6 @@ static int btrfs_getattr(struct vfsmount *mnt,
9236 9337
9237 generic_fillattr(inode, stat); 9338 generic_fillattr(inode, stat);
9238 stat->dev = BTRFS_I(inode)->root->anon_dev; 9339 stat->dev = BTRFS_I(inode)->root->anon_dev;
9239 stat->blksize = PAGE_CACHE_SIZE;
9240 9340
9241 spin_lock(&BTRFS_I(inode)->lock); 9341 spin_lock(&BTRFS_I(inode)->lock);
9242 delalloc_bytes = BTRFS_I(inode)->delalloc_bytes; 9342 delalloc_bytes = BTRFS_I(inode)->delalloc_bytes;
diff --git a/fs/btrfs/ioctl.c b/fs/btrfs/ioctl.c
index 952172ca7e45..9ba7b5be3fe5 100644
--- a/fs/btrfs/ioctl.c
+++ b/fs/btrfs/ioctl.c
@@ -2925,8 +2925,8 @@ static int btrfs_cmp_data_prepare(struct inode *src, u64 loff,
2925 * of the array is bounded by len, which is in turn bounded by 2925 * of the array is bounded by len, which is in turn bounded by
2926 * BTRFS_MAX_DEDUPE_LEN. 2926 * BTRFS_MAX_DEDUPE_LEN.
2927 */ 2927 */
2928 src_pgarr = kzalloc(num_pages * sizeof(struct page *), GFP_NOFS); 2928 src_pgarr = kcalloc(num_pages, sizeof(struct page *), GFP_KERNEL);
2929 dst_pgarr = kzalloc(num_pages * sizeof(struct page *), GFP_NOFS); 2929 dst_pgarr = kcalloc(num_pages, sizeof(struct page *), GFP_KERNEL);
2930 if (!src_pgarr || !dst_pgarr) { 2930 if (!src_pgarr || !dst_pgarr) {
2931 kfree(src_pgarr); 2931 kfree(src_pgarr);
2932 kfree(dst_pgarr); 2932 kfree(dst_pgarr);
@@ -3814,8 +3814,9 @@ static noinline int btrfs_clone_files(struct file *file, struct file *file_src,
3814 * Truncate page cache pages so that future reads will see the cloned 3814 * Truncate page cache pages so that future reads will see the cloned
3815 * data immediately and not the previous data. 3815 * data immediately and not the previous data.
3816 */ 3816 */
3817 truncate_inode_pages_range(&inode->i_data, destoff, 3817 truncate_inode_pages_range(&inode->i_data,
3818 PAGE_CACHE_ALIGN(destoff + len) - 1); 3818 round_down(destoff, PAGE_CACHE_SIZE),
3819 round_up(destoff + len, PAGE_CACHE_SIZE) - 1);
3819out_unlock: 3820out_unlock:
3820 if (!same_inode) 3821 if (!same_inode)
3821 btrfs_double_inode_unlock(src, inode); 3822 btrfs_double_inode_unlock(src, inode);
diff --git a/fs/btrfs/print-tree.c b/fs/btrfs/print-tree.c
index 647ab12fdf5d..147dc6ca5de1 100644
--- a/fs/btrfs/print-tree.c
+++ b/fs/btrfs/print-tree.c
@@ -295,8 +295,27 @@ void btrfs_print_leaf(struct btrfs_root *root, struct extent_buffer *l)
295 btrfs_dev_extent_chunk_offset(l, dev_extent), 295 btrfs_dev_extent_chunk_offset(l, dev_extent),
296 btrfs_dev_extent_length(l, dev_extent)); 296 btrfs_dev_extent_length(l, dev_extent));
297 break; 297 break;
298 case BTRFS_DEV_STATS_KEY: 298 case BTRFS_PERSISTENT_ITEM_KEY:
299 printk(KERN_INFO "\t\tdevice stats\n"); 299 printk(KERN_INFO "\t\tpersistent item objectid %llu offset %llu\n",
300 key.objectid, key.offset);
301 switch (key.objectid) {
302 case BTRFS_DEV_STATS_OBJECTID:
303 printk(KERN_INFO "\t\tdevice stats\n");
304 break;
305 default:
306 printk(KERN_INFO "\t\tunknown persistent item\n");
307 }
308 break;
309 case BTRFS_TEMPORARY_ITEM_KEY:
310 printk(KERN_INFO "\t\ttemporary item objectid %llu offset %llu\n",
311 key.objectid, key.offset);
312 switch (key.objectid) {
313 case BTRFS_BALANCE_OBJECTID:
314 printk(KERN_INFO "\t\tbalance status\n");
315 break;
316 default:
317 printk(KERN_INFO "\t\tunknown temporary item\n");
318 }
300 break; 319 break;
301 case BTRFS_DEV_REPLACE_KEY: 320 case BTRFS_DEV_REPLACE_KEY:
302 printk(KERN_INFO "\t\tdev replace\n"); 321 printk(KERN_INFO "\t\tdev replace\n");
diff --git a/fs/btrfs/reada.c b/fs/btrfs/reada.c
index 619f92963e27..c5f1773c4794 100644
--- a/fs/btrfs/reada.c
+++ b/fs/btrfs/reada.c
@@ -280,7 +280,7 @@ static struct reada_zone *reada_find_zone(struct btrfs_fs_info *fs_info,
280 end = start + cache->key.offset - 1; 280 end = start + cache->key.offset - 1;
281 btrfs_put_block_group(cache); 281 btrfs_put_block_group(cache);
282 282
283 zone = kzalloc(sizeof(*zone), GFP_NOFS); 283 zone = kzalloc(sizeof(*zone), GFP_KERNEL);
284 if (!zone) 284 if (!zone)
285 return NULL; 285 return NULL;
286 286
@@ -343,7 +343,7 @@ static struct reada_extent *reada_find_extent(struct btrfs_root *root,
343 if (re) 343 if (re)
344 return re; 344 return re;
345 345
346 re = kzalloc(sizeof(*re), GFP_NOFS); 346 re = kzalloc(sizeof(*re), GFP_KERNEL);
347 if (!re) 347 if (!re)
348 return NULL; 348 return NULL;
349 349
@@ -566,7 +566,7 @@ static int reada_add_block(struct reada_control *rc, u64 logical,
566 if (!re) 566 if (!re)
567 return -1; 567 return -1;
568 568
569 rec = kzalloc(sizeof(*rec), GFP_NOFS); 569 rec = kzalloc(sizeof(*rec), GFP_KERNEL);
570 if (!rec) { 570 if (!rec) {
571 reada_extent_put(root->fs_info, re); 571 reada_extent_put(root->fs_info, re);
572 return -ENOMEM; 572 return -ENOMEM;
@@ -791,7 +791,7 @@ static void reada_start_machine(struct btrfs_fs_info *fs_info)
791{ 791{
792 struct reada_machine_work *rmw; 792 struct reada_machine_work *rmw;
793 793
794 rmw = kzalloc(sizeof(*rmw), GFP_NOFS); 794 rmw = kzalloc(sizeof(*rmw), GFP_KERNEL);
795 if (!rmw) { 795 if (!rmw) {
796 /* FIXME we cannot handle this properly right now */ 796 /* FIXME we cannot handle this properly right now */
797 BUG(); 797 BUG();
@@ -926,7 +926,7 @@ struct reada_control *btrfs_reada_add(struct btrfs_root *root,
926 .offset = (u64)-1 926 .offset = (u64)-1
927 }; 927 };
928 928
929 rc = kzalloc(sizeof(*rc), GFP_NOFS); 929 rc = kzalloc(sizeof(*rc), GFP_KERNEL);
930 if (!rc) 930 if (!rc)
931 return ERR_PTR(-ENOMEM); 931 return ERR_PTR(-ENOMEM);
932 932
diff --git a/fs/btrfs/scrub.c b/fs/btrfs/scrub.c
index 92bf5ee732fb..2de7817d0e1b 100644
--- a/fs/btrfs/scrub.c
+++ b/fs/btrfs/scrub.c
@@ -461,7 +461,7 @@ struct scrub_ctx *scrub_setup_ctx(struct btrfs_device *dev, int is_dev_replace)
461 struct btrfs_fs_info *fs_info = dev->dev_root->fs_info; 461 struct btrfs_fs_info *fs_info = dev->dev_root->fs_info;
462 int ret; 462 int ret;
463 463
464 sctx = kzalloc(sizeof(*sctx), GFP_NOFS); 464 sctx = kzalloc(sizeof(*sctx), GFP_KERNEL);
465 if (!sctx) 465 if (!sctx)
466 goto nomem; 466 goto nomem;
467 atomic_set(&sctx->refs, 1); 467 atomic_set(&sctx->refs, 1);
@@ -472,7 +472,7 @@ struct scrub_ctx *scrub_setup_ctx(struct btrfs_device *dev, int is_dev_replace)
472 for (i = 0; i < SCRUB_BIOS_PER_SCTX; ++i) { 472 for (i = 0; i < SCRUB_BIOS_PER_SCTX; ++i) {
473 struct scrub_bio *sbio; 473 struct scrub_bio *sbio;
474 474
475 sbio = kzalloc(sizeof(*sbio), GFP_NOFS); 475 sbio = kzalloc(sizeof(*sbio), GFP_KERNEL);
476 if (!sbio) 476 if (!sbio)
477 goto nomem; 477 goto nomem;
478 sctx->bios[i] = sbio; 478 sctx->bios[i] = sbio;
@@ -1654,7 +1654,7 @@ static int scrub_add_page_to_wr_bio(struct scrub_ctx *sctx,
1654again: 1654again:
1655 if (!wr_ctx->wr_curr_bio) { 1655 if (!wr_ctx->wr_curr_bio) {
1656 wr_ctx->wr_curr_bio = kzalloc(sizeof(*wr_ctx->wr_curr_bio), 1656 wr_ctx->wr_curr_bio = kzalloc(sizeof(*wr_ctx->wr_curr_bio),
1657 GFP_NOFS); 1657 GFP_KERNEL);
1658 if (!wr_ctx->wr_curr_bio) { 1658 if (!wr_ctx->wr_curr_bio) {
1659 mutex_unlock(&wr_ctx->wr_lock); 1659 mutex_unlock(&wr_ctx->wr_lock);
1660 return -ENOMEM; 1660 return -ENOMEM;
@@ -1671,7 +1671,8 @@ again:
1671 sbio->dev = wr_ctx->tgtdev; 1671 sbio->dev = wr_ctx->tgtdev;
1672 bio = sbio->bio; 1672 bio = sbio->bio;
1673 if (!bio) { 1673 if (!bio) {
1674 bio = btrfs_io_bio_alloc(GFP_NOFS, wr_ctx->pages_per_wr_bio); 1674 bio = btrfs_io_bio_alloc(GFP_KERNEL,
1675 wr_ctx->pages_per_wr_bio);
1675 if (!bio) { 1676 if (!bio) {
1676 mutex_unlock(&wr_ctx->wr_lock); 1677 mutex_unlock(&wr_ctx->wr_lock);
1677 return -ENOMEM; 1678 return -ENOMEM;
@@ -2076,7 +2077,8 @@ again:
2076 sbio->dev = spage->dev; 2077 sbio->dev = spage->dev;
2077 bio = sbio->bio; 2078 bio = sbio->bio;
2078 if (!bio) { 2079 if (!bio) {
2079 bio = btrfs_io_bio_alloc(GFP_NOFS, sctx->pages_per_rd_bio); 2080 bio = btrfs_io_bio_alloc(GFP_KERNEL,
2081 sctx->pages_per_rd_bio);
2080 if (!bio) 2082 if (!bio)
2081 return -ENOMEM; 2083 return -ENOMEM;
2082 sbio->bio = bio; 2084 sbio->bio = bio;
@@ -2241,7 +2243,7 @@ static int scrub_pages(struct scrub_ctx *sctx, u64 logical, u64 len,
2241 struct scrub_block *sblock; 2243 struct scrub_block *sblock;
2242 int index; 2244 int index;
2243 2245
2244 sblock = kzalloc(sizeof(*sblock), GFP_NOFS); 2246 sblock = kzalloc(sizeof(*sblock), GFP_KERNEL);
2245 if (!sblock) { 2247 if (!sblock) {
2246 spin_lock(&sctx->stat_lock); 2248 spin_lock(&sctx->stat_lock);
2247 sctx->stat.malloc_errors++; 2249 sctx->stat.malloc_errors++;
@@ -2259,7 +2261,7 @@ static int scrub_pages(struct scrub_ctx *sctx, u64 logical, u64 len,
2259 struct scrub_page *spage; 2261 struct scrub_page *spage;
2260 u64 l = min_t(u64, len, PAGE_SIZE); 2262 u64 l = min_t(u64, len, PAGE_SIZE);
2261 2263
2262 spage = kzalloc(sizeof(*spage), GFP_NOFS); 2264 spage = kzalloc(sizeof(*spage), GFP_KERNEL);
2263 if (!spage) { 2265 if (!spage) {
2264leave_nomem: 2266leave_nomem:
2265 spin_lock(&sctx->stat_lock); 2267 spin_lock(&sctx->stat_lock);
@@ -2286,7 +2288,7 @@ leave_nomem:
2286 spage->have_csum = 0; 2288 spage->have_csum = 0;
2287 } 2289 }
2288 sblock->page_count++; 2290 sblock->page_count++;
2289 spage->page = alloc_page(GFP_NOFS); 2291 spage->page = alloc_page(GFP_KERNEL);
2290 if (!spage->page) 2292 if (!spage->page)
2291 goto leave_nomem; 2293 goto leave_nomem;
2292 len -= l; 2294 len -= l;
@@ -2541,7 +2543,7 @@ static int scrub_pages_for_parity(struct scrub_parity *sparity,
2541 struct scrub_block *sblock; 2543 struct scrub_block *sblock;
2542 int index; 2544 int index;
2543 2545
2544 sblock = kzalloc(sizeof(*sblock), GFP_NOFS); 2546 sblock = kzalloc(sizeof(*sblock), GFP_KERNEL);
2545 if (!sblock) { 2547 if (!sblock) {
2546 spin_lock(&sctx->stat_lock); 2548 spin_lock(&sctx->stat_lock);
2547 sctx->stat.malloc_errors++; 2549 sctx->stat.malloc_errors++;
@@ -2561,7 +2563,7 @@ static int scrub_pages_for_parity(struct scrub_parity *sparity,
2561 struct scrub_page *spage; 2563 struct scrub_page *spage;
2562 u64 l = min_t(u64, len, PAGE_SIZE); 2564 u64 l = min_t(u64, len, PAGE_SIZE);
2563 2565
2564 spage = kzalloc(sizeof(*spage), GFP_NOFS); 2566 spage = kzalloc(sizeof(*spage), GFP_KERNEL);
2565 if (!spage) { 2567 if (!spage) {
2566leave_nomem: 2568leave_nomem:
2567 spin_lock(&sctx->stat_lock); 2569 spin_lock(&sctx->stat_lock);
@@ -2591,7 +2593,7 @@ leave_nomem:
2591 spage->have_csum = 0; 2593 spage->have_csum = 0;
2592 } 2594 }
2593 sblock->page_count++; 2595 sblock->page_count++;
2594 spage->page = alloc_page(GFP_NOFS); 2596 spage->page = alloc_page(GFP_KERNEL);
2595 if (!spage->page) 2597 if (!spage->page)
2596 goto leave_nomem; 2598 goto leave_nomem;
2597 len -= l; 2599 len -= l;
diff --git a/fs/btrfs/send.c b/fs/btrfs/send.c
index 63a6152be04b..d2e29925f1da 100644
--- a/fs/btrfs/send.c
+++ b/fs/btrfs/send.c
@@ -304,7 +304,7 @@ static struct fs_path *fs_path_alloc(void)
304{ 304{
305 struct fs_path *p; 305 struct fs_path *p;
306 306
307 p = kmalloc(sizeof(*p), GFP_NOFS); 307 p = kmalloc(sizeof(*p), GFP_KERNEL);
308 if (!p) 308 if (!p)
309 return NULL; 309 return NULL;
310 p->reversed = 0; 310 p->reversed = 0;
@@ -363,11 +363,11 @@ static int fs_path_ensure_buf(struct fs_path *p, int len)
363 * First time the inline_buf does not suffice 363 * First time the inline_buf does not suffice
364 */ 364 */
365 if (p->buf == p->inline_buf) { 365 if (p->buf == p->inline_buf) {
366 tmp_buf = kmalloc(len, GFP_NOFS); 366 tmp_buf = kmalloc(len, GFP_KERNEL);
367 if (tmp_buf) 367 if (tmp_buf)
368 memcpy(tmp_buf, p->buf, old_buf_len); 368 memcpy(tmp_buf, p->buf, old_buf_len);
369 } else { 369 } else {
370 tmp_buf = krealloc(p->buf, len, GFP_NOFS); 370 tmp_buf = krealloc(p->buf, len, GFP_KERNEL);
371 } 371 }
372 if (!tmp_buf) 372 if (!tmp_buf)
373 return -ENOMEM; 373 return -ENOMEM;
@@ -995,7 +995,7 @@ static int iterate_dir_item(struct btrfs_root *root, struct btrfs_path *path,
995 * values are small. 995 * values are small.
996 */ 996 */
997 buf_len = PATH_MAX; 997 buf_len = PATH_MAX;
998 buf = kmalloc(buf_len, GFP_NOFS); 998 buf = kmalloc(buf_len, GFP_KERNEL);
999 if (!buf) { 999 if (!buf) {
1000 ret = -ENOMEM; 1000 ret = -ENOMEM;
1001 goto out; 1001 goto out;
@@ -1042,7 +1042,7 @@ static int iterate_dir_item(struct btrfs_root *root, struct btrfs_path *path,
1042 buf = NULL; 1042 buf = NULL;
1043 } else { 1043 } else {
1044 char *tmp = krealloc(buf, buf_len, 1044 char *tmp = krealloc(buf, buf_len,
1045 GFP_NOFS | __GFP_NOWARN); 1045 GFP_KERNEL | __GFP_NOWARN);
1046 1046
1047 if (!tmp) 1047 if (!tmp)
1048 kfree(buf); 1048 kfree(buf);
@@ -1303,7 +1303,7 @@ static int find_extent_clone(struct send_ctx *sctx,
1303 /* We only use this path under the commit sem */ 1303 /* We only use this path under the commit sem */
1304 tmp_path->need_commit_sem = 0; 1304 tmp_path->need_commit_sem = 0;
1305 1305
1306 backref_ctx = kmalloc(sizeof(*backref_ctx), GFP_NOFS); 1306 backref_ctx = kmalloc(sizeof(*backref_ctx), GFP_KERNEL);
1307 if (!backref_ctx) { 1307 if (!backref_ctx) {
1308 ret = -ENOMEM; 1308 ret = -ENOMEM;
1309 goto out; 1309 goto out;
@@ -1984,7 +1984,7 @@ static int name_cache_insert(struct send_ctx *sctx,
1984 nce_head = radix_tree_lookup(&sctx->name_cache, 1984 nce_head = radix_tree_lookup(&sctx->name_cache,
1985 (unsigned long)nce->ino); 1985 (unsigned long)nce->ino);
1986 if (!nce_head) { 1986 if (!nce_head) {
1987 nce_head = kmalloc(sizeof(*nce_head), GFP_NOFS); 1987 nce_head = kmalloc(sizeof(*nce_head), GFP_KERNEL);
1988 if (!nce_head) { 1988 if (!nce_head) {
1989 kfree(nce); 1989 kfree(nce);
1990 return -ENOMEM; 1990 return -ENOMEM;
@@ -2179,7 +2179,7 @@ out_cache:
2179 /* 2179 /*
2180 * Store the result of the lookup in the name cache. 2180 * Store the result of the lookup in the name cache.
2181 */ 2181 */
2182 nce = kmalloc(sizeof(*nce) + fs_path_len(dest) + 1, GFP_NOFS); 2182 nce = kmalloc(sizeof(*nce) + fs_path_len(dest) + 1, GFP_KERNEL);
2183 if (!nce) { 2183 if (!nce) {
2184 ret = -ENOMEM; 2184 ret = -ENOMEM;
2185 goto out; 2185 goto out;
@@ -2315,7 +2315,7 @@ static int send_subvol_begin(struct send_ctx *sctx)
2315 if (!path) 2315 if (!path)
2316 return -ENOMEM; 2316 return -ENOMEM;
2317 2317
2318 name = kmalloc(BTRFS_PATH_NAME_MAX, GFP_NOFS); 2318 name = kmalloc(BTRFS_PATH_NAME_MAX, GFP_KERNEL);
2319 if (!name) { 2319 if (!name) {
2320 btrfs_free_path(path); 2320 btrfs_free_path(path);
2321 return -ENOMEM; 2321 return -ENOMEM;
@@ -2730,7 +2730,7 @@ static int __record_ref(struct list_head *head, u64 dir,
2730{ 2730{
2731 struct recorded_ref *ref; 2731 struct recorded_ref *ref;
2732 2732
2733 ref = kmalloc(sizeof(*ref), GFP_NOFS); 2733 ref = kmalloc(sizeof(*ref), GFP_KERNEL);
2734 if (!ref) 2734 if (!ref)
2735 return -ENOMEM; 2735 return -ENOMEM;
2736 2736
@@ -2755,7 +2755,7 @@ static int dup_ref(struct recorded_ref *ref, struct list_head *list)
2755{ 2755{
2756 struct recorded_ref *new; 2756 struct recorded_ref *new;
2757 2757
2758 new = kmalloc(sizeof(*ref), GFP_NOFS); 2758 new = kmalloc(sizeof(*ref), GFP_KERNEL);
2759 if (!new) 2759 if (!new)
2760 return -ENOMEM; 2760 return -ENOMEM;
2761 2761
@@ -2818,7 +2818,7 @@ add_orphan_dir_info(struct send_ctx *sctx, u64 dir_ino)
2818 struct rb_node *parent = NULL; 2818 struct rb_node *parent = NULL;
2819 struct orphan_dir_info *entry, *odi; 2819 struct orphan_dir_info *entry, *odi;
2820 2820
2821 odi = kmalloc(sizeof(*odi), GFP_NOFS); 2821 odi = kmalloc(sizeof(*odi), GFP_KERNEL);
2822 if (!odi) 2822 if (!odi)
2823 return ERR_PTR(-ENOMEM); 2823 return ERR_PTR(-ENOMEM);
2824 odi->ino = dir_ino; 2824 odi->ino = dir_ino;
@@ -2973,7 +2973,7 @@ static int add_waiting_dir_move(struct send_ctx *sctx, u64 ino, bool orphanized)
2973 struct rb_node *parent = NULL; 2973 struct rb_node *parent = NULL;
2974 struct waiting_dir_move *entry, *dm; 2974 struct waiting_dir_move *entry, *dm;
2975 2975
2976 dm = kmalloc(sizeof(*dm), GFP_NOFS); 2976 dm = kmalloc(sizeof(*dm), GFP_KERNEL);
2977 if (!dm) 2977 if (!dm)
2978 return -ENOMEM; 2978 return -ENOMEM;
2979 dm->ino = ino; 2979 dm->ino = ino;
@@ -3040,7 +3040,7 @@ static int add_pending_dir_move(struct send_ctx *sctx,
3040 int exists = 0; 3040 int exists = 0;
3041 int ret; 3041 int ret;
3042 3042
3043 pm = kmalloc(sizeof(*pm), GFP_NOFS); 3043 pm = kmalloc(sizeof(*pm), GFP_KERNEL);
3044 if (!pm) 3044 if (!pm)
3045 return -ENOMEM; 3045 return -ENOMEM;
3046 pm->parent_ino = parent_ino; 3046 pm->parent_ino = parent_ino;
@@ -4280,7 +4280,7 @@ static int __find_xattr(int num, struct btrfs_key *di_key,
4280 strncmp(name, ctx->name, name_len) == 0) { 4280 strncmp(name, ctx->name, name_len) == 0) {
4281 ctx->found_idx = num; 4281 ctx->found_idx = num;
4282 ctx->found_data_len = data_len; 4282 ctx->found_data_len = data_len;
4283 ctx->found_data = kmemdup(data, data_len, GFP_NOFS); 4283 ctx->found_data = kmemdup(data, data_len, GFP_KERNEL);
4284 if (!ctx->found_data) 4284 if (!ctx->found_data)
4285 return -ENOMEM; 4285 return -ENOMEM;
4286 return 1; 4286 return 1;
@@ -4481,7 +4481,7 @@ static ssize_t fill_read_buf(struct send_ctx *sctx, u64 offset, u32 len)
4481 while (index <= last_index) { 4481 while (index <= last_index) {
4482 unsigned cur_len = min_t(unsigned, len, 4482 unsigned cur_len = min_t(unsigned, len,
4483 PAGE_CACHE_SIZE - pg_offset); 4483 PAGE_CACHE_SIZE - pg_offset);
4484 page = find_or_create_page(inode->i_mapping, index, GFP_NOFS); 4484 page = find_or_create_page(inode->i_mapping, index, GFP_KERNEL);
4485 if (!page) { 4485 if (!page) {
4486 ret = -ENOMEM; 4486 ret = -ENOMEM;
4487 break; 4487 break;
@@ -5989,7 +5989,7 @@ long btrfs_ioctl_send(struct file *mnt_file, void __user *arg_)
5989 goto out; 5989 goto out;
5990 } 5990 }
5991 5991
5992 sctx = kzalloc(sizeof(struct send_ctx), GFP_NOFS); 5992 sctx = kzalloc(sizeof(struct send_ctx), GFP_KERNEL);
5993 if (!sctx) { 5993 if (!sctx) {
5994 ret = -ENOMEM; 5994 ret = -ENOMEM;
5995 goto out; 5995 goto out;
@@ -5997,7 +5997,7 @@ long btrfs_ioctl_send(struct file *mnt_file, void __user *arg_)
5997 5997
5998 INIT_LIST_HEAD(&sctx->new_refs); 5998 INIT_LIST_HEAD(&sctx->new_refs);
5999 INIT_LIST_HEAD(&sctx->deleted_refs); 5999 INIT_LIST_HEAD(&sctx->deleted_refs);
6000 INIT_RADIX_TREE(&sctx->name_cache, GFP_NOFS); 6000 INIT_RADIX_TREE(&sctx->name_cache, GFP_KERNEL);
6001 INIT_LIST_HEAD(&sctx->name_cache_list); 6001 INIT_LIST_HEAD(&sctx->name_cache_list);
6002 6002
6003 sctx->flags = arg->flags; 6003 sctx->flags = arg->flags;
diff --git a/fs/btrfs/volumes.c b/fs/btrfs/volumes.c
index 366b335946fa..6e0e4396d3a4 100644
--- a/fs/btrfs/volumes.c
+++ b/fs/btrfs/volumes.c
@@ -138,7 +138,7 @@ static struct btrfs_fs_devices *__alloc_fs_devices(void)
138{ 138{
139 struct btrfs_fs_devices *fs_devs; 139 struct btrfs_fs_devices *fs_devs;
140 140
141 fs_devs = kzalloc(sizeof(*fs_devs), GFP_NOFS); 141 fs_devs = kzalloc(sizeof(*fs_devs), GFP_KERNEL);
142 if (!fs_devs) 142 if (!fs_devs)
143 return ERR_PTR(-ENOMEM); 143 return ERR_PTR(-ENOMEM);
144 144
@@ -220,7 +220,7 @@ static struct btrfs_device *__alloc_device(void)
220{ 220{
221 struct btrfs_device *dev; 221 struct btrfs_device *dev;
222 222
223 dev = kzalloc(sizeof(*dev), GFP_NOFS); 223 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
224 if (!dev) 224 if (!dev)
225 return ERR_PTR(-ENOMEM); 225 return ERR_PTR(-ENOMEM);
226 226
@@ -733,7 +733,8 @@ static struct btrfs_fs_devices *clone_fs_devices(struct btrfs_fs_devices *orig)
733 * uuid mutex so nothing we touch in here is going to disappear. 733 * uuid mutex so nothing we touch in here is going to disappear.
734 */ 734 */
735 if (orig_dev->name) { 735 if (orig_dev->name) {
736 name = rcu_string_strdup(orig_dev->name->str, GFP_NOFS); 736 name = rcu_string_strdup(orig_dev->name->str,
737 GFP_KERNEL);
737 if (!name) { 738 if (!name) {
738 kfree(device); 739 kfree(device);
739 goto error; 740 goto error;
@@ -2287,7 +2288,7 @@ int btrfs_init_new_device(struct btrfs_root *root, char *device_path)
2287 goto error; 2288 goto error;
2288 } 2289 }
2289 2290
2290 name = rcu_string_strdup(device_path, GFP_NOFS); 2291 name = rcu_string_strdup(device_path, GFP_KERNEL);
2291 if (!name) { 2292 if (!name) {
2292 kfree(device); 2293 kfree(device);
2293 ret = -ENOMEM; 2294 ret = -ENOMEM;
@@ -2966,7 +2967,7 @@ static int insert_balance_item(struct btrfs_root *root,
2966 } 2967 }
2967 2968
2968 key.objectid = BTRFS_BALANCE_OBJECTID; 2969 key.objectid = BTRFS_BALANCE_OBJECTID;
2969 key.type = BTRFS_BALANCE_ITEM_KEY; 2970 key.type = BTRFS_TEMPORARY_ITEM_KEY;
2970 key.offset = 0; 2971 key.offset = 0;
2971 2972
2972 ret = btrfs_insert_empty_item(trans, root, path, &key, 2973 ret = btrfs_insert_empty_item(trans, root, path, &key,
@@ -3015,7 +3016,7 @@ static int del_balance_item(struct btrfs_root *root)
3015 } 3016 }
3016 3017
3017 key.objectid = BTRFS_BALANCE_OBJECTID; 3018 key.objectid = BTRFS_BALANCE_OBJECTID;
3018 key.type = BTRFS_BALANCE_ITEM_KEY; 3019 key.type = BTRFS_TEMPORARY_ITEM_KEY;
3019 key.offset = 0; 3020 key.offset = 0;
3020 3021
3021 ret = btrfs_search_slot(trans, root, &key, path, -1, 1); 3022 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
@@ -3867,7 +3868,7 @@ int btrfs_recover_balance(struct btrfs_fs_info *fs_info)
3867 return -ENOMEM; 3868 return -ENOMEM;
3868 3869
3869 key.objectid = BTRFS_BALANCE_OBJECTID; 3870 key.objectid = BTRFS_BALANCE_OBJECTID;
3870 key.type = BTRFS_BALANCE_ITEM_KEY; 3871 key.type = BTRFS_TEMPORARY_ITEM_KEY;
3871 key.offset = 0; 3872 key.offset = 0;
3872 3873
3873 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0); 3874 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
@@ -6705,8 +6706,8 @@ int btrfs_init_dev_stats(struct btrfs_fs_info *fs_info)
6705 int item_size; 6706 int item_size;
6706 struct btrfs_dev_stats_item *ptr; 6707 struct btrfs_dev_stats_item *ptr;
6707 6708
6708 key.objectid = 0; 6709 key.objectid = BTRFS_DEV_STATS_OBJECTID;
6709 key.type = BTRFS_DEV_STATS_KEY; 6710 key.type = BTRFS_PERSISTENT_ITEM_KEY;
6710 key.offset = device->devid; 6711 key.offset = device->devid;
6711 ret = btrfs_search_slot(NULL, dev_root, &key, path, 0, 0); 6712 ret = btrfs_search_slot(NULL, dev_root, &key, path, 0, 0);
6712 if (ret) { 6713 if (ret) {
@@ -6753,8 +6754,8 @@ static int update_dev_stat_item(struct btrfs_trans_handle *trans,
6753 int ret; 6754 int ret;
6754 int i; 6755 int i;
6755 6756
6756 key.objectid = 0; 6757 key.objectid = BTRFS_DEV_STATS_OBJECTID;
6757 key.type = BTRFS_DEV_STATS_KEY; 6758 key.type = BTRFS_PERSISTENT_ITEM_KEY;
6758 key.offset = device->devid; 6759 key.offset = device->devid;
6759 6760
6760 path = btrfs_alloc_path(); 6761 path = btrfs_alloc_path();