diff options
Diffstat (limited to 'fs/btrfs/inode.c')
-rw-r--r-- | fs/btrfs/inode.c | 109 |
1 files changed, 64 insertions, 45 deletions
diff --git a/fs/btrfs/inode.c b/fs/btrfs/inode.c index a2aabdb85226..1af069a9a0c7 100644 --- a/fs/btrfs/inode.c +++ b/fs/btrfs/inode.c | |||
@@ -47,6 +47,7 @@ | |||
47 | #include "props.h" | 47 | #include "props.h" |
48 | #include "qgroup.h" | 48 | #include "qgroup.h" |
49 | #include "dedupe.h" | 49 | #include "dedupe.h" |
50 | #include "delalloc-space.h" | ||
50 | 51 | ||
51 | struct btrfs_iget_args { | 52 | struct btrfs_iget_args { |
52 | struct btrfs_key *location; | 53 | struct btrfs_key *location; |
@@ -1932,17 +1933,19 @@ int btrfs_bio_fits_in_stripe(struct page *page, size_t size, struct bio *bio, | |||
1932 | u64 length = 0; | 1933 | u64 length = 0; |
1933 | u64 map_length; | 1934 | u64 map_length; |
1934 | int ret; | 1935 | int ret; |
1936 | struct btrfs_io_geometry geom; | ||
1935 | 1937 | ||
1936 | if (bio_flags & EXTENT_BIO_COMPRESSED) | 1938 | if (bio_flags & EXTENT_BIO_COMPRESSED) |
1937 | return 0; | 1939 | return 0; |
1938 | 1940 | ||
1939 | length = bio->bi_iter.bi_size; | 1941 | length = bio->bi_iter.bi_size; |
1940 | map_length = length; | 1942 | map_length = length; |
1941 | ret = btrfs_map_block(fs_info, btrfs_op(bio), logical, &map_length, | 1943 | ret = btrfs_get_io_geometry(fs_info, btrfs_op(bio), logical, map_length, |
1942 | NULL, 0); | 1944 | &geom); |
1943 | if (ret < 0) | 1945 | if (ret < 0) |
1944 | return ret; | 1946 | return ret; |
1945 | if (map_length < length + size) | 1947 | |
1948 | if (geom.len < length + size) | ||
1946 | return 1; | 1949 | return 1; |
1947 | return 0; | 1950 | return 0; |
1948 | } | 1951 | } |
@@ -3203,16 +3206,23 @@ static int __readpage_endio_check(struct inode *inode, | |||
3203 | int icsum, struct page *page, | 3206 | int icsum, struct page *page, |
3204 | int pgoff, u64 start, size_t len) | 3207 | int pgoff, u64 start, size_t len) |
3205 | { | 3208 | { |
3209 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); | ||
3210 | SHASH_DESC_ON_STACK(shash, fs_info->csum_shash); | ||
3206 | char *kaddr; | 3211 | char *kaddr; |
3207 | u32 csum_expected; | 3212 | u16 csum_size = btrfs_super_csum_size(fs_info->super_copy); |
3208 | u32 csum = ~(u32)0; | 3213 | u8 *csum_expected; |
3214 | u8 csum[BTRFS_CSUM_SIZE]; | ||
3209 | 3215 | ||
3210 | csum_expected = *(((u32 *)io_bio->csum) + icsum); | 3216 | csum_expected = ((u8 *)io_bio->csum) + icsum * csum_size; |
3211 | 3217 | ||
3212 | kaddr = kmap_atomic(page); | 3218 | kaddr = kmap_atomic(page); |
3213 | csum = btrfs_csum_data(kaddr + pgoff, csum, len); | 3219 | shash->tfm = fs_info->csum_shash; |
3214 | btrfs_csum_final(csum, (u8 *)&csum); | 3220 | |
3215 | if (csum != csum_expected) | 3221 | crypto_shash_init(shash); |
3222 | crypto_shash_update(shash, kaddr + pgoff, len); | ||
3223 | crypto_shash_final(shash, csum); | ||
3224 | |||
3225 | if (memcmp(csum, csum_expected, csum_size)) | ||
3216 | goto zeroit; | 3226 | goto zeroit; |
3217 | 3227 | ||
3218 | kunmap_atomic(kaddr); | 3228 | kunmap_atomic(kaddr); |
@@ -3286,6 +3296,28 @@ void btrfs_add_delayed_iput(struct inode *inode) | |||
3286 | wake_up_process(fs_info->cleaner_kthread); | 3296 | wake_up_process(fs_info->cleaner_kthread); |
3287 | } | 3297 | } |
3288 | 3298 | ||
3299 | static void run_delayed_iput_locked(struct btrfs_fs_info *fs_info, | ||
3300 | struct btrfs_inode *inode) | ||
3301 | { | ||
3302 | list_del_init(&inode->delayed_iput); | ||
3303 | spin_unlock(&fs_info->delayed_iput_lock); | ||
3304 | iput(&inode->vfs_inode); | ||
3305 | if (atomic_dec_and_test(&fs_info->nr_delayed_iputs)) | ||
3306 | wake_up(&fs_info->delayed_iputs_wait); | ||
3307 | spin_lock(&fs_info->delayed_iput_lock); | ||
3308 | } | ||
3309 | |||
3310 | static void btrfs_run_delayed_iput(struct btrfs_fs_info *fs_info, | ||
3311 | struct btrfs_inode *inode) | ||
3312 | { | ||
3313 | if (!list_empty(&inode->delayed_iput)) { | ||
3314 | spin_lock(&fs_info->delayed_iput_lock); | ||
3315 | if (!list_empty(&inode->delayed_iput)) | ||
3316 | run_delayed_iput_locked(fs_info, inode); | ||
3317 | spin_unlock(&fs_info->delayed_iput_lock); | ||
3318 | } | ||
3319 | } | ||
3320 | |||
3289 | void btrfs_run_delayed_iputs(struct btrfs_fs_info *fs_info) | 3321 | void btrfs_run_delayed_iputs(struct btrfs_fs_info *fs_info) |
3290 | { | 3322 | { |
3291 | 3323 | ||
@@ -3295,12 +3327,7 @@ void btrfs_run_delayed_iputs(struct btrfs_fs_info *fs_info) | |||
3295 | 3327 | ||
3296 | inode = list_first_entry(&fs_info->delayed_iputs, | 3328 | inode = list_first_entry(&fs_info->delayed_iputs, |
3297 | struct btrfs_inode, delayed_iput); | 3329 | struct btrfs_inode, delayed_iput); |
3298 | list_del_init(&inode->delayed_iput); | 3330 | run_delayed_iput_locked(fs_info, inode); |
3299 | spin_unlock(&fs_info->delayed_iput_lock); | ||
3300 | iput(&inode->vfs_inode); | ||
3301 | if (atomic_dec_and_test(&fs_info->nr_delayed_iputs)) | ||
3302 | wake_up(&fs_info->delayed_iputs_wait); | ||
3303 | spin_lock(&fs_info->delayed_iput_lock); | ||
3304 | } | 3331 | } |
3305 | spin_unlock(&fs_info->delayed_iput_lock); | 3332 | spin_unlock(&fs_info->delayed_iput_lock); |
3306 | } | 3333 | } |
@@ -3935,9 +3962,7 @@ static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans, | |||
3935 | struct btrfs_fs_info *fs_info = root->fs_info; | 3962 | struct btrfs_fs_info *fs_info = root->fs_info; |
3936 | struct btrfs_path *path; | 3963 | struct btrfs_path *path; |
3937 | int ret = 0; | 3964 | int ret = 0; |
3938 | struct extent_buffer *leaf; | ||
3939 | struct btrfs_dir_item *di; | 3965 | struct btrfs_dir_item *di; |
3940 | struct btrfs_key key; | ||
3941 | u64 index; | 3966 | u64 index; |
3942 | u64 ino = btrfs_ino(inode); | 3967 | u64 ino = btrfs_ino(inode); |
3943 | u64 dir_ino = btrfs_ino(dir); | 3968 | u64 dir_ino = btrfs_ino(dir); |
@@ -3955,8 +3980,6 @@ static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans, | |||
3955 | ret = di ? PTR_ERR(di) : -ENOENT; | 3980 | ret = di ? PTR_ERR(di) : -ENOENT; |
3956 | goto err; | 3981 | goto err; |
3957 | } | 3982 | } |
3958 | leaf = path->nodes[0]; | ||
3959 | btrfs_dir_item_key_to_cpu(leaf, di, &key); | ||
3960 | ret = btrfs_delete_one_dir_name(trans, root, path, di); | 3983 | ret = btrfs_delete_one_dir_name(trans, root, path, di); |
3961 | if (ret) | 3984 | if (ret) |
3962 | goto err; | 3985 | goto err; |
@@ -4009,6 +4032,17 @@ skip_backref: | |||
4009 | ret = 0; | 4032 | ret = 0; |
4010 | else if (ret) | 4033 | else if (ret) |
4011 | btrfs_abort_transaction(trans, ret); | 4034 | btrfs_abort_transaction(trans, ret); |
4035 | |||
4036 | /* | ||
4037 | * If we have a pending delayed iput we could end up with the final iput | ||
4038 | * being run in btrfs-cleaner context. If we have enough of these built | ||
4039 | * up we can end up burning a lot of time in btrfs-cleaner without any | ||
4040 | * way to throttle the unlinks. Since we're currently holding a ref on | ||
4041 | * the inode we can run the delayed iput here without any issues as the | ||
4042 | * final iput won't be done until after we drop the ref we're currently | ||
4043 | * holding. | ||
4044 | */ | ||
4045 | btrfs_run_delayed_iput(fs_info, inode); | ||
4012 | err: | 4046 | err: |
4013 | btrfs_free_path(path); | 4047 | btrfs_free_path(path); |
4014 | if (ret) | 4048 | if (ret) |
@@ -5008,21 +5042,8 @@ int btrfs_cont_expand(struct inode *inode, loff_t oldsize, loff_t size) | |||
5008 | if (size <= hole_start) | 5042 | if (size <= hole_start) |
5009 | return 0; | 5043 | return 0; |
5010 | 5044 | ||
5011 | while (1) { | 5045 | btrfs_lock_and_flush_ordered_range(io_tree, BTRFS_I(inode), hole_start, |
5012 | struct btrfs_ordered_extent *ordered; | 5046 | block_end - 1, &cached_state); |
5013 | |||
5014 | lock_extent_bits(io_tree, hole_start, block_end - 1, | ||
5015 | &cached_state); | ||
5016 | ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), hole_start, | ||
5017 | block_end - hole_start); | ||
5018 | if (!ordered) | ||
5019 | break; | ||
5020 | unlock_extent_cached(io_tree, hole_start, block_end - 1, | ||
5021 | &cached_state); | ||
5022 | btrfs_start_ordered_extent(inode, ordered, 1); | ||
5023 | btrfs_put_ordered_extent(ordered); | ||
5024 | } | ||
5025 | |||
5026 | cur_offset = hole_start; | 5047 | cur_offset = hole_start; |
5027 | while (1) { | 5048 | while (1) { |
5028 | em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, cur_offset, | 5049 | em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, cur_offset, |
@@ -8318,22 +8339,21 @@ static int btrfs_submit_direct_hook(struct btrfs_dio_private *dip) | |||
8318 | struct bio *orig_bio = dip->orig_bio; | 8339 | struct bio *orig_bio = dip->orig_bio; |
8319 | u64 start_sector = orig_bio->bi_iter.bi_sector; | 8340 | u64 start_sector = orig_bio->bi_iter.bi_sector; |
8320 | u64 file_offset = dip->logical_offset; | 8341 | u64 file_offset = dip->logical_offset; |
8321 | u64 map_length; | ||
8322 | int async_submit = 0; | 8342 | int async_submit = 0; |
8323 | u64 submit_len; | 8343 | u64 submit_len; |
8324 | int clone_offset = 0; | 8344 | int clone_offset = 0; |
8325 | int clone_len; | 8345 | int clone_len; |
8326 | int ret; | 8346 | int ret; |
8327 | blk_status_t status; | 8347 | blk_status_t status; |
8348 | struct btrfs_io_geometry geom; | ||
8328 | 8349 | ||
8329 | map_length = orig_bio->bi_iter.bi_size; | 8350 | submit_len = orig_bio->bi_iter.bi_size; |
8330 | submit_len = map_length; | 8351 | ret = btrfs_get_io_geometry(fs_info, btrfs_op(orig_bio), |
8331 | ret = btrfs_map_block(fs_info, btrfs_op(orig_bio), start_sector << 9, | 8352 | start_sector << 9, submit_len, &geom); |
8332 | &map_length, NULL, 0); | ||
8333 | if (ret) | 8353 | if (ret) |
8334 | return -EIO; | 8354 | return -EIO; |
8335 | 8355 | ||
8336 | if (map_length >= submit_len) { | 8356 | if (geom.len >= submit_len) { |
8337 | bio = orig_bio; | 8357 | bio = orig_bio; |
8338 | dip->flags |= BTRFS_DIO_ORIG_BIO_SUBMITTED; | 8358 | dip->flags |= BTRFS_DIO_ORIG_BIO_SUBMITTED; |
8339 | goto submit; | 8359 | goto submit; |
@@ -8346,10 +8366,10 @@ static int btrfs_submit_direct_hook(struct btrfs_dio_private *dip) | |||
8346 | async_submit = 1; | 8366 | async_submit = 1; |
8347 | 8367 | ||
8348 | /* bio split */ | 8368 | /* bio split */ |
8349 | ASSERT(map_length <= INT_MAX); | 8369 | ASSERT(geom.len <= INT_MAX); |
8350 | atomic_inc(&dip->pending_bios); | 8370 | atomic_inc(&dip->pending_bios); |
8351 | do { | 8371 | do { |
8352 | clone_len = min_t(int, submit_len, map_length); | 8372 | clone_len = min_t(int, submit_len, geom.len); |
8353 | 8373 | ||
8354 | /* | 8374 | /* |
8355 | * This will never fail as it's passing GPF_NOFS and | 8375 | * This will never fail as it's passing GPF_NOFS and |
@@ -8386,9 +8406,8 @@ static int btrfs_submit_direct_hook(struct btrfs_dio_private *dip) | |||
8386 | start_sector += clone_len >> 9; | 8406 | start_sector += clone_len >> 9; |
8387 | file_offset += clone_len; | 8407 | file_offset += clone_len; |
8388 | 8408 | ||
8389 | map_length = submit_len; | 8409 | ret = btrfs_get_io_geometry(fs_info, btrfs_op(orig_bio), |
8390 | ret = btrfs_map_block(fs_info, btrfs_op(orig_bio), | 8410 | start_sector << 9, submit_len, &geom); |
8391 | start_sector << 9, &map_length, NULL, 0); | ||
8392 | if (ret) | 8411 | if (ret) |
8393 | goto out_err; | 8412 | goto out_err; |
8394 | } while (submit_len > 0); | 8413 | } while (submit_len > 0); |