mirror of
https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git
synced 2024-08-27 11:19:37 +00:00
![Filipe Manana](/assets/img/avatar_default.png)
When cloning an inline extent there are cases where we can not just copy
the inline extent from the source range to the target range (e.g. when the
target range starts at an offset greater than zero). In such cases we copy
the inline extent's data into a page of the destination inode and then
dirty that page. However, after that we will need to start a transaction
for each processed extent and, if we are ever low on available metadata
space, we may need to flush existing delalloc for all dirty inodes in an
attempt to release metadata space - if that happens we may deadlock:
* the async reclaim task queued a delalloc work to flush delalloc for
the destination inode of the clone operation;
* the task executing that delalloc work gets blocked waiting for the
range with the dirty page to be unlocked, which is currently locked
by the task doing the clone operation;
* the async reclaim task blocks waiting for the delalloc work to complete;
* the cloning task is waiting on the waitqueue of its reservation ticket
while holding the range with the dirty page locked in the inode's
io_tree;
* if metadata space is not released by some other task (like delalloc for
some other inode completing for example), the clone task waits forever
and as a consequence the delalloc work and async reclaim tasks will hang
forever as well. Releasing more space on the other hand may require
starting a transaction, which will hang as well when trying to reserve
metadata space, resulting in a deadlock between all these tasks.
When this happens, traces like the following show up in dmesg/syslog:
[87452.323003] INFO: task kworker/u16:11:1810830 blocked for more than 120 seconds.
[87452.323644] Tainted: G B W 5.10.0-rc4-btrfs-next-73 #1
[87452.324248] "echo 0 > /proc/sys/kernel/hung_task_timeout_secs" disables this message.
[87452.324852] task:kworker/u16:11 state:D stack: 0 pid:1810830 ppid: 2 flags:0x00004000
[87452.325520] Workqueue: btrfs-flush_delalloc btrfs_work_helper [btrfs]
[87452.326136] Call Trace:
[87452.326737] __schedule+0x5d1/0xcf0
[87452.327390] schedule+0x45/0xe0
[87452.328174] lock_extent_bits+0x1e6/0x2d0 [btrfs]
[87452.328894] ? finish_wait+0x90/0x90
[87452.329474] btrfs_invalidatepage+0x32c/0x390 [btrfs]
[87452.330133] ? __mod_memcg_state+0x8e/0x160
[87452.330738] __extent_writepage+0x2d4/0x400 [btrfs]
[87452.331405] extent_write_cache_pages+0x2b2/0x500 [btrfs]
[87452.332007] ? lock_release+0x20e/0x4c0
[87452.332557] ? trace_hardirqs_on+0x1b/0xf0
[87452.333127] extent_writepages+0x43/0x90 [btrfs]
[87452.333653] ? lock_acquire+0x1a3/0x490
[87452.334177] do_writepages+0x43/0xe0
[87452.334699] ? __filemap_fdatawrite_range+0xa4/0x100
[87452.335720] __filemap_fdatawrite_range+0xc5/0x100
[87452.336500] btrfs_run_delalloc_work+0x17/0x40 [btrfs]
[87452.337216] btrfs_work_helper+0xf1/0x600 [btrfs]
[87452.337838] process_one_work+0x24e/0x5e0
[87452.338437] worker_thread+0x50/0x3b0
[87452.339137] ? process_one_work+0x5e0/0x5e0
[87452.339884] kthread+0x153/0x170
[87452.340507] ? kthread_mod_delayed_work+0xc0/0xc0
[87452.341153] ret_from_fork+0x22/0x30
[87452.341806] INFO: task kworker/u16:1:2426217 blocked for more than 120 seconds.
[87452.342487] Tainted: G B W 5.10.0-rc4-btrfs-next-73 #1
[87452.343274] "echo 0 > /proc/sys/kernel/hung_task_timeout_secs" disables this message.
[87452.344049] task:kworker/u16:1 state:D stack: 0 pid:2426217 ppid: 2 flags:0x00004000
[87452.344974] Workqueue: events_unbound btrfs_async_reclaim_metadata_space [btrfs]
[87452.345655] Call Trace:
[87452.346305] __schedule+0x5d1/0xcf0
[87452.346947] ? kvm_clock_read+0x14/0x30
[87452.347676] ? wait_for_completion+0x81/0x110
[87452.348389] schedule+0x45/0xe0
[87452.349077] schedule_timeout+0x30c/0x580
[87452.349718] ? _raw_spin_unlock_irqrestore+0x3c/0x60
[87452.350340] ? lock_acquire+0x1a3/0x490
[87452.351006] ? try_to_wake_up+0x7a/0xa20
[87452.351541] ? lock_release+0x20e/0x4c0
[87452.352040] ? lock_acquired+0x199/0x490
[87452.352517] ? wait_for_completion+0x81/0x110
[87452.353000] wait_for_completion+0xab/0x110
[87452.353490] start_delalloc_inodes+0x2af/0x390 [btrfs]
[87452.353973] btrfs_start_delalloc_roots+0x12d/0x250 [btrfs]
[87452.354455] flush_space+0x24f/0x660 [btrfs]
[87452.355063] btrfs_async_reclaim_metadata_space+0x1bb/0x480 [btrfs]
[87452.355565] process_one_work+0x24e/0x5e0
[87452.356024] worker_thread+0x20f/0x3b0
[87452.356487] ? process_one_work+0x5e0/0x5e0
[87452.356973] kthread+0x153/0x170
[87452.357434] ? kthread_mod_delayed_work+0xc0/0xc0
[87452.357880] ret_from_fork+0x22/0x30
(...)
< stack traces of several tasks waiting for the locks of the inodes of the
clone operation >
(...)
[92867.444138] RSP: 002b:00007ffc3371bbe8 EFLAGS: 00000246 ORIG_RAX: 0000000000000052
[92867.444624] RAX: ffffffffffffffda RBX: 00007ffc3371bea0 RCX: 00007f61efe73f97
[92867.445116] RDX: 0000000000000000 RSI: 0000560fbd5d7a40 RDI: 0000560fbd5d8960
[92867.445595] RBP: 00007ffc3371beb0 R08: 0000000000000001 R09: 0000000000000003
[92867.446070] R10: 00007ffc3371b996 R11: 0000000000000246 R12: 0000000000000000
[92867.446820] R13: 000000000000001f R14: 00007ffc3371bea0 R15: 00007ffc3371beb0
[92867.447361] task:fsstress state:D stack: 0 pid:2508238 ppid:2508153 flags:0x00004000
[92867.447920] Call Trace:
[92867.448435] __schedule+0x5d1/0xcf0
[92867.448934] ? _raw_spin_unlock_irqrestore+0x3c/0x60
[92867.449423] schedule+0x45/0xe0
[92867.449916] __reserve_bytes+0x4a4/0xb10 [btrfs]
[92867.450576] ? finish_wait+0x90/0x90
[92867.451202] btrfs_reserve_metadata_bytes+0x29/0x190 [btrfs]
[92867.451815] btrfs_block_rsv_add+0x1f/0x50 [btrfs]
[92867.452412] start_transaction+0x2d1/0x760 [btrfs]
[92867.453216] clone_copy_inline_extent+0x333/0x490 [btrfs]
[92867.453848] ? lock_release+0x20e/0x4c0
[92867.454539] ? btrfs_search_slot+0x9a7/0xc30 [btrfs]
[92867.455218] btrfs_clone+0x569/0x7e0 [btrfs]
[92867.455952] btrfs_clone_files+0xf6/0x150 [btrfs]
[92867.456588] btrfs_remap_file_range+0x324/0x3d0 [btrfs]
[92867.457213] do_clone_file_range+0xd4/0x1f0
[92867.457828] vfs_clone_file_range+0x4d/0x230
[92867.458355] ? lock_release+0x20e/0x4c0
[92867.458890] ioctl_file_clone+0x8f/0xc0
[92867.459377] do_vfs_ioctl+0x342/0x750
[92867.459913] __x64_sys_ioctl+0x62/0xb0
[92867.460377] do_syscall_64+0x33/0x80
[92867.460842] entry_SYSCALL_64_after_hwframe+0x44/0xa9
(...)
< stack traces of more tasks blocked on metadata reservation like the clone
task above, because the async reclaim task has deadlocked >
(...)
Another thing to notice is that the worker task that is deadlocked when
trying to flush the destination inode of the clone operation is at
btrfs_invalidatepage(). This is simply because the clone operation has a
destination offset greater than the i_size and we only update the i_size
of the destination file after cloning an extent (just like we do in the
buffered write path).
Since the async reclaim path uses btrfs_start_delalloc_roots() to trigger
the flushing of delalloc for all inodes that have delalloc, add a runtime
flag to an inode to signal it should not be flushed, and for inodes with
that flag set, start_delalloc_inodes() will simply skip them. When the
cloning code needs to dirty a page to copy an inline extent, set that flag
on the inode and then clear it when the clone operation finishes.
This could be sporadically triggered with test case generic/269 from
fstests, which exercises many fsstress processes running in parallel with
several dd processes filling up the entire filesystem.
CC: stable@vger.kernel.org # 5.9+
Fixes: 05a5a7621c
("Btrfs: implement full reflink support for inline extents")
Reviewed-by: Josef Bacik <josef@toxicpanda.com>
Signed-off-by: Filipe Manana <fdmanana@suse.com>
Reviewed-by: David Sterba <dsterba@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
840 lines
25 KiB
C
840 lines
25 KiB
C
// SPDX-License-Identifier: GPL-2.0
|
|
|
|
#include <linux/blkdev.h>
|
|
#include <linux/iversion.h>
|
|
#include "compression.h"
|
|
#include "ctree.h"
|
|
#include "delalloc-space.h"
|
|
#include "reflink.h"
|
|
#include "transaction.h"
|
|
|
|
#define BTRFS_MAX_DEDUPE_LEN SZ_16M
|
|
|
|
static int clone_finish_inode_update(struct btrfs_trans_handle *trans,
|
|
struct inode *inode,
|
|
u64 endoff,
|
|
const u64 destoff,
|
|
const u64 olen,
|
|
int no_time_update)
|
|
{
|
|
struct btrfs_root *root = BTRFS_I(inode)->root;
|
|
int ret;
|
|
|
|
inode_inc_iversion(inode);
|
|
if (!no_time_update)
|
|
inode->i_mtime = inode->i_ctime = current_time(inode);
|
|
/*
|
|
* We round up to the block size at eof when determining which
|
|
* extents to clone above, but shouldn't round up the file size.
|
|
*/
|
|
if (endoff > destoff + olen)
|
|
endoff = destoff + olen;
|
|
if (endoff > inode->i_size) {
|
|
i_size_write(inode, endoff);
|
|
btrfs_inode_safe_disk_i_size_write(BTRFS_I(inode), 0);
|
|
}
|
|
|
|
ret = btrfs_update_inode(trans, root, BTRFS_I(inode));
|
|
if (ret) {
|
|
btrfs_abort_transaction(trans, ret);
|
|
btrfs_end_transaction(trans);
|
|
goto out;
|
|
}
|
|
ret = btrfs_end_transaction(trans);
|
|
out:
|
|
return ret;
|
|
}
|
|
|
|
static int copy_inline_to_page(struct btrfs_inode *inode,
|
|
const u64 file_offset,
|
|
char *inline_data,
|
|
const u64 size,
|
|
const u64 datal,
|
|
const u8 comp_type)
|
|
{
|
|
const u64 block_size = btrfs_inode_sectorsize(inode);
|
|
const u64 range_end = file_offset + block_size - 1;
|
|
const size_t inline_size = size - btrfs_file_extent_calc_inline_size(0);
|
|
char *data_start = inline_data + btrfs_file_extent_calc_inline_size(0);
|
|
struct extent_changeset *data_reserved = NULL;
|
|
struct page *page = NULL;
|
|
struct address_space *mapping = inode->vfs_inode.i_mapping;
|
|
int ret;
|
|
|
|
ASSERT(IS_ALIGNED(file_offset, block_size));
|
|
|
|
/*
|
|
* We have flushed and locked the ranges of the source and destination
|
|
* inodes, we also have locked the inodes, so we are safe to do a
|
|
* reservation here. Also we must not do the reservation while holding
|
|
* a transaction open, otherwise we would deadlock.
|
|
*/
|
|
ret = btrfs_delalloc_reserve_space(inode, &data_reserved, file_offset,
|
|
block_size);
|
|
if (ret)
|
|
goto out;
|
|
|
|
page = find_or_create_page(mapping, file_offset >> PAGE_SHIFT,
|
|
btrfs_alloc_write_mask(mapping));
|
|
if (!page) {
|
|
ret = -ENOMEM;
|
|
goto out_unlock;
|
|
}
|
|
|
|
set_page_extent_mapped(page);
|
|
clear_extent_bit(&inode->io_tree, file_offset, range_end,
|
|
EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
|
|
0, 0, NULL);
|
|
ret = btrfs_set_extent_delalloc(inode, file_offset, range_end, 0, NULL);
|
|
if (ret)
|
|
goto out_unlock;
|
|
|
|
/*
|
|
* After dirtying the page our caller will need to start a transaction,
|
|
* and if we are low on metadata free space, that can cause flushing of
|
|
* delalloc for all inodes in order to get metadata space released.
|
|
* However we are holding the range locked for the whole duration of
|
|
* the clone/dedupe operation, so we may deadlock if that happens and no
|
|
* other task releases enough space. So mark this inode as not being
|
|
* possible to flush to avoid such deadlock. We will clear that flag
|
|
* when we finish cloning all extents, since a transaction is started
|
|
* after finding each extent to clone.
|
|
*/
|
|
set_bit(BTRFS_INODE_NO_DELALLOC_FLUSH, &inode->runtime_flags);
|
|
|
|
if (comp_type == BTRFS_COMPRESS_NONE) {
|
|
char *map;
|
|
|
|
map = kmap(page);
|
|
memcpy(map, data_start, datal);
|
|
flush_dcache_page(page);
|
|
kunmap(page);
|
|
} else {
|
|
ret = btrfs_decompress(comp_type, data_start, page, 0,
|
|
inline_size, datal);
|
|
if (ret)
|
|
goto out_unlock;
|
|
flush_dcache_page(page);
|
|
}
|
|
|
|
/*
|
|
* If our inline data is smaller then the block/page size, then the
|
|
* remaining of the block/page is equivalent to zeroes. We had something
|
|
* like the following done:
|
|
*
|
|
* $ xfs_io -f -c "pwrite -S 0xab 0 500" file
|
|
* $ sync # (or fsync)
|
|
* $ xfs_io -c "falloc 0 4K" file
|
|
* $ xfs_io -c "pwrite -S 0xcd 4K 4K"
|
|
*
|
|
* So what's in the range [500, 4095] corresponds to zeroes.
|
|
*/
|
|
if (datal < block_size) {
|
|
char *map;
|
|
|
|
map = kmap(page);
|
|
memset(map + datal, 0, block_size - datal);
|
|
flush_dcache_page(page);
|
|
kunmap(page);
|
|
}
|
|
|
|
SetPageUptodate(page);
|
|
ClearPageChecked(page);
|
|
set_page_dirty(page);
|
|
out_unlock:
|
|
if (page) {
|
|
unlock_page(page);
|
|
put_page(page);
|
|
}
|
|
if (ret)
|
|
btrfs_delalloc_release_space(inode, data_reserved, file_offset,
|
|
block_size, true);
|
|
btrfs_delalloc_release_extents(inode, block_size);
|
|
out:
|
|
extent_changeset_free(data_reserved);
|
|
|
|
return ret;
|
|
}
|
|
|
|
/*
|
|
* Deal with cloning of inline extents. We try to copy the inline extent from
|
|
* the source inode to destination inode when possible. When not possible we
|
|
* copy the inline extent's data into the respective page of the inode.
|
|
*/
|
|
static int clone_copy_inline_extent(struct inode *dst,
|
|
struct btrfs_path *path,
|
|
struct btrfs_key *new_key,
|
|
const u64 drop_start,
|
|
const u64 datal,
|
|
const u64 size,
|
|
const u8 comp_type,
|
|
char *inline_data,
|
|
struct btrfs_trans_handle **trans_out)
|
|
{
|
|
struct btrfs_fs_info *fs_info = btrfs_sb(dst->i_sb);
|
|
struct btrfs_root *root = BTRFS_I(dst)->root;
|
|
const u64 aligned_end = ALIGN(new_key->offset + datal,
|
|
fs_info->sectorsize);
|
|
struct btrfs_trans_handle *trans = NULL;
|
|
struct btrfs_drop_extents_args drop_args = { 0 };
|
|
int ret;
|
|
struct btrfs_key key;
|
|
|
|
if (new_key->offset > 0) {
|
|
ret = copy_inline_to_page(BTRFS_I(dst), new_key->offset,
|
|
inline_data, size, datal, comp_type);
|
|
goto out;
|
|
}
|
|
|
|
key.objectid = btrfs_ino(BTRFS_I(dst));
|
|
key.type = BTRFS_EXTENT_DATA_KEY;
|
|
key.offset = 0;
|
|
ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
|
|
if (ret < 0) {
|
|
return ret;
|
|
} else if (ret > 0) {
|
|
if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) {
|
|
ret = btrfs_next_leaf(root, path);
|
|
if (ret < 0)
|
|
return ret;
|
|
else if (ret > 0)
|
|
goto copy_inline_extent;
|
|
}
|
|
btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
|
|
if (key.objectid == btrfs_ino(BTRFS_I(dst)) &&
|
|
key.type == BTRFS_EXTENT_DATA_KEY) {
|
|
/*
|
|
* There's an implicit hole at file offset 0, copy the
|
|
* inline extent's data to the page.
|
|
*/
|
|
ASSERT(key.offset > 0);
|
|
ret = copy_inline_to_page(BTRFS_I(dst), new_key->offset,
|
|
inline_data, size, datal,
|
|
comp_type);
|
|
goto out;
|
|
}
|
|
} else if (i_size_read(dst) <= datal) {
|
|
struct btrfs_file_extent_item *ei;
|
|
|
|
ei = btrfs_item_ptr(path->nodes[0], path->slots[0],
|
|
struct btrfs_file_extent_item);
|
|
/*
|
|
* If it's an inline extent replace it with the source inline
|
|
* extent, otherwise copy the source inline extent data into
|
|
* the respective page at the destination inode.
|
|
*/
|
|
if (btrfs_file_extent_type(path->nodes[0], ei) ==
|
|
BTRFS_FILE_EXTENT_INLINE)
|
|
goto copy_inline_extent;
|
|
|
|
ret = copy_inline_to_page(BTRFS_I(dst), new_key->offset,
|
|
inline_data, size, datal, comp_type);
|
|
goto out;
|
|
}
|
|
|
|
copy_inline_extent:
|
|
ret = 0;
|
|
/*
|
|
* We have no extent items, or we have an extent at offset 0 which may
|
|
* or may not be inlined. All these cases are dealt the same way.
|
|
*/
|
|
if (i_size_read(dst) > datal) {
|
|
/*
|
|
* At the destination offset 0 we have either a hole, a regular
|
|
* extent or an inline extent larger then the one we want to
|
|
* clone. Deal with all these cases by copying the inline extent
|
|
* data into the respective page at the destination inode.
|
|
*/
|
|
ret = copy_inline_to_page(BTRFS_I(dst), new_key->offset,
|
|
inline_data, size, datal, comp_type);
|
|
goto out;
|
|
}
|
|
|
|
btrfs_release_path(path);
|
|
/*
|
|
* If we end up here it means were copy the inline extent into a leaf
|
|
* of the destination inode. We know we will drop or adjust at most one
|
|
* extent item in the destination root.
|
|
*
|
|
* 1 unit - adjusting old extent (we may have to split it)
|
|
* 1 unit - add new extent
|
|
* 1 unit - inode update
|
|
*/
|
|
trans = btrfs_start_transaction(root, 3);
|
|
if (IS_ERR(trans)) {
|
|
ret = PTR_ERR(trans);
|
|
trans = NULL;
|
|
goto out;
|
|
}
|
|
drop_args.path = path;
|
|
drop_args.start = drop_start;
|
|
drop_args.end = aligned_end;
|
|
drop_args.drop_cache = true;
|
|
ret = btrfs_drop_extents(trans, root, BTRFS_I(dst), &drop_args);
|
|
if (ret)
|
|
goto out;
|
|
ret = btrfs_insert_empty_item(trans, root, path, new_key, size);
|
|
if (ret)
|
|
goto out;
|
|
|
|
write_extent_buffer(path->nodes[0], inline_data,
|
|
btrfs_item_ptr_offset(path->nodes[0],
|
|
path->slots[0]),
|
|
size);
|
|
btrfs_update_inode_bytes(BTRFS_I(dst), datal, drop_args.bytes_found);
|
|
set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(dst)->runtime_flags);
|
|
ret = btrfs_inode_set_file_extent_range(BTRFS_I(dst), 0, aligned_end);
|
|
out:
|
|
if (!ret && !trans) {
|
|
/*
|
|
* No transaction here means we copied the inline extent into a
|
|
* page of the destination inode.
|
|
*
|
|
* 1 unit to update inode item
|
|
*/
|
|
trans = btrfs_start_transaction(root, 1);
|
|
if (IS_ERR(trans)) {
|
|
ret = PTR_ERR(trans);
|
|
trans = NULL;
|
|
}
|
|
}
|
|
if (ret && trans) {
|
|
btrfs_abort_transaction(trans, ret);
|
|
btrfs_end_transaction(trans);
|
|
}
|
|
if (!ret)
|
|
*trans_out = trans;
|
|
|
|
return ret;
|
|
}
|
|
|
|
/**
|
|
* btrfs_clone() - clone a range from inode file to another
|
|
*
|
|
* @src: Inode to clone from
|
|
* @inode: Inode to clone to
|
|
* @off: Offset within source to start clone from
|
|
* @olen: Original length, passed by user, of range to clone
|
|
* @olen_aligned: Block-aligned value of olen
|
|
* @destoff: Offset within @inode to start clone
|
|
* @no_time_update: Whether to update mtime/ctime on the target inode
|
|
*/
|
|
static int btrfs_clone(struct inode *src, struct inode *inode,
|
|
const u64 off, const u64 olen, const u64 olen_aligned,
|
|
const u64 destoff, int no_time_update)
|
|
{
|
|
struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
|
|
struct btrfs_path *path = NULL;
|
|
struct extent_buffer *leaf;
|
|
struct btrfs_trans_handle *trans;
|
|
char *buf = NULL;
|
|
struct btrfs_key key;
|
|
u32 nritems;
|
|
int slot;
|
|
int ret;
|
|
const u64 len = olen_aligned;
|
|
u64 last_dest_end = destoff;
|
|
|
|
ret = -ENOMEM;
|
|
buf = kvmalloc(fs_info->nodesize, GFP_KERNEL);
|
|
if (!buf)
|
|
return ret;
|
|
|
|
path = btrfs_alloc_path();
|
|
if (!path) {
|
|
kvfree(buf);
|
|
return ret;
|
|
}
|
|
|
|
path->reada = READA_FORWARD;
|
|
/* Clone data */
|
|
key.objectid = btrfs_ino(BTRFS_I(src));
|
|
key.type = BTRFS_EXTENT_DATA_KEY;
|
|
key.offset = off;
|
|
|
|
while (1) {
|
|
u64 next_key_min_offset = key.offset + 1;
|
|
struct btrfs_file_extent_item *extent;
|
|
u64 extent_gen;
|
|
int type;
|
|
u32 size;
|
|
struct btrfs_key new_key;
|
|
u64 disko = 0, diskl = 0;
|
|
u64 datao = 0, datal = 0;
|
|
u8 comp;
|
|
u64 drop_start;
|
|
|
|
/* Note the key will change type as we walk through the tree */
|
|
ret = btrfs_search_slot(NULL, BTRFS_I(src)->root, &key, path,
|
|
0, 0);
|
|
if (ret < 0)
|
|
goto out;
|
|
/*
|
|
* First search, if no extent item that starts at offset off was
|
|
* found but the previous item is an extent item, it's possible
|
|
* it might overlap our target range, therefore process it.
|
|
*/
|
|
if (key.offset == off && ret > 0 && path->slots[0] > 0) {
|
|
btrfs_item_key_to_cpu(path->nodes[0], &key,
|
|
path->slots[0] - 1);
|
|
if (key.type == BTRFS_EXTENT_DATA_KEY)
|
|
path->slots[0]--;
|
|
}
|
|
|
|
nritems = btrfs_header_nritems(path->nodes[0]);
|
|
process_slot:
|
|
if (path->slots[0] >= nritems) {
|
|
ret = btrfs_next_leaf(BTRFS_I(src)->root, path);
|
|
if (ret < 0)
|
|
goto out;
|
|
if (ret > 0)
|
|
break;
|
|
nritems = btrfs_header_nritems(path->nodes[0]);
|
|
}
|
|
leaf = path->nodes[0];
|
|
slot = path->slots[0];
|
|
|
|
btrfs_item_key_to_cpu(leaf, &key, slot);
|
|
if (key.type > BTRFS_EXTENT_DATA_KEY ||
|
|
key.objectid != btrfs_ino(BTRFS_I(src)))
|
|
break;
|
|
|
|
ASSERT(key.type == BTRFS_EXTENT_DATA_KEY);
|
|
|
|
extent = btrfs_item_ptr(leaf, slot,
|
|
struct btrfs_file_extent_item);
|
|
extent_gen = btrfs_file_extent_generation(leaf, extent);
|
|
comp = btrfs_file_extent_compression(leaf, extent);
|
|
type = btrfs_file_extent_type(leaf, extent);
|
|
if (type == BTRFS_FILE_EXTENT_REG ||
|
|
type == BTRFS_FILE_EXTENT_PREALLOC) {
|
|
disko = btrfs_file_extent_disk_bytenr(leaf, extent);
|
|
diskl = btrfs_file_extent_disk_num_bytes(leaf, extent);
|
|
datao = btrfs_file_extent_offset(leaf, extent);
|
|
datal = btrfs_file_extent_num_bytes(leaf, extent);
|
|
} else if (type == BTRFS_FILE_EXTENT_INLINE) {
|
|
/* Take upper bound, may be compressed */
|
|
datal = btrfs_file_extent_ram_bytes(leaf, extent);
|
|
}
|
|
|
|
/*
|
|
* The first search might have left us at an extent item that
|
|
* ends before our target range's start, can happen if we have
|
|
* holes and NO_HOLES feature enabled.
|
|
*/
|
|
if (key.offset + datal <= off) {
|
|
path->slots[0]++;
|
|
goto process_slot;
|
|
} else if (key.offset >= off + len) {
|
|
break;
|
|
}
|
|
next_key_min_offset = key.offset + datal;
|
|
size = btrfs_item_size_nr(leaf, slot);
|
|
read_extent_buffer(leaf, buf, btrfs_item_ptr_offset(leaf, slot),
|
|
size);
|
|
|
|
btrfs_release_path(path);
|
|
|
|
memcpy(&new_key, &key, sizeof(new_key));
|
|
new_key.objectid = btrfs_ino(BTRFS_I(inode));
|
|
if (off <= key.offset)
|
|
new_key.offset = key.offset + destoff - off;
|
|
else
|
|
new_key.offset = destoff;
|
|
|
|
/*
|
|
* Deal with a hole that doesn't have an extent item that
|
|
* represents it (NO_HOLES feature enabled).
|
|
* This hole is either in the middle of the cloning range or at
|
|
* the beginning (fully overlaps it or partially overlaps it).
|
|
*/
|
|
if (new_key.offset != last_dest_end)
|
|
drop_start = last_dest_end;
|
|
else
|
|
drop_start = new_key.offset;
|
|
|
|
if (type == BTRFS_FILE_EXTENT_REG ||
|
|
type == BTRFS_FILE_EXTENT_PREALLOC) {
|
|
struct btrfs_replace_extent_info clone_info;
|
|
|
|
/*
|
|
* a | --- range to clone ---| b
|
|
* | ------------- extent ------------- |
|
|
*/
|
|
|
|
/* Subtract range b */
|
|
if (key.offset + datal > off + len)
|
|
datal = off + len - key.offset;
|
|
|
|
/* Subtract range a */
|
|
if (off > key.offset) {
|
|
datao += off - key.offset;
|
|
datal -= off - key.offset;
|
|
}
|
|
|
|
clone_info.disk_offset = disko;
|
|
clone_info.disk_len = diskl;
|
|
clone_info.data_offset = datao;
|
|
clone_info.data_len = datal;
|
|
clone_info.file_offset = new_key.offset;
|
|
clone_info.extent_buf = buf;
|
|
clone_info.is_new_extent = false;
|
|
ret = btrfs_replace_file_extents(inode, path, drop_start,
|
|
new_key.offset + datal - 1, &clone_info,
|
|
&trans);
|
|
if (ret)
|
|
goto out;
|
|
} else if (type == BTRFS_FILE_EXTENT_INLINE) {
|
|
/*
|
|
* Inline extents always have to start at file offset 0
|
|
* and can never be bigger then the sector size. We can
|
|
* never clone only parts of an inline extent, since all
|
|
* reflink operations must start at a sector size aligned
|
|
* offset, and the length must be aligned too or end at
|
|
* the i_size (which implies the whole inlined data).
|
|
*/
|
|
ASSERT(key.offset == 0);
|
|
ASSERT(datal <= fs_info->sectorsize);
|
|
if (key.offset != 0 || datal > fs_info->sectorsize)
|
|
return -EUCLEAN;
|
|
|
|
ret = clone_copy_inline_extent(inode, path, &new_key,
|
|
drop_start, datal, size,
|
|
comp, buf, &trans);
|
|
if (ret)
|
|
goto out;
|
|
}
|
|
|
|
btrfs_release_path(path);
|
|
|
|
/*
|
|
* If this is a new extent update the last_reflink_trans of both
|
|
* inodes. This is used by fsync to make sure it does not log
|
|
* multiple checksum items with overlapping ranges. For older
|
|
* extents we don't need to do it since inode logging skips the
|
|
* checksums for older extents. Also ignore holes and inline
|
|
* extents because they don't have checksums in the csum tree.
|
|
*/
|
|
if (extent_gen == trans->transid && disko > 0) {
|
|
BTRFS_I(src)->last_reflink_trans = trans->transid;
|
|
BTRFS_I(inode)->last_reflink_trans = trans->transid;
|
|
}
|
|
|
|
last_dest_end = ALIGN(new_key.offset + datal,
|
|
fs_info->sectorsize);
|
|
ret = clone_finish_inode_update(trans, inode, last_dest_end,
|
|
destoff, olen, no_time_update);
|
|
if (ret)
|
|
goto out;
|
|
if (new_key.offset + datal >= destoff + len)
|
|
break;
|
|
|
|
btrfs_release_path(path);
|
|
key.offset = next_key_min_offset;
|
|
|
|
if (fatal_signal_pending(current)) {
|
|
ret = -EINTR;
|
|
goto out;
|
|
}
|
|
|
|
cond_resched();
|
|
}
|
|
ret = 0;
|
|
|
|
if (last_dest_end < destoff + len) {
|
|
/*
|
|
* We have an implicit hole that fully or partially overlaps our
|
|
* cloning range at its end. This means that we either have the
|
|
* NO_HOLES feature enabled or the implicit hole happened due to
|
|
* mixing buffered and direct IO writes against this file.
|
|
*/
|
|
btrfs_release_path(path);
|
|
|
|
ret = btrfs_replace_file_extents(inode, path, last_dest_end,
|
|
destoff + len - 1, NULL, &trans);
|
|
if (ret)
|
|
goto out;
|
|
|
|
ret = clone_finish_inode_update(trans, inode, destoff + len,
|
|
destoff, olen, no_time_update);
|
|
}
|
|
|
|
out:
|
|
btrfs_free_path(path);
|
|
kvfree(buf);
|
|
clear_bit(BTRFS_INODE_NO_DELALLOC_FLUSH, &BTRFS_I(inode)->runtime_flags);
|
|
|
|
return ret;
|
|
}
|
|
|
|
static void btrfs_double_extent_unlock(struct inode *inode1, u64 loff1,
|
|
struct inode *inode2, u64 loff2, u64 len)
|
|
{
|
|
unlock_extent(&BTRFS_I(inode1)->io_tree, loff1, loff1 + len - 1);
|
|
unlock_extent(&BTRFS_I(inode2)->io_tree, loff2, loff2 + len - 1);
|
|
}
|
|
|
|
static void btrfs_double_extent_lock(struct inode *inode1, u64 loff1,
|
|
struct inode *inode2, u64 loff2, u64 len)
|
|
{
|
|
if (inode1 < inode2) {
|
|
swap(inode1, inode2);
|
|
swap(loff1, loff2);
|
|
} else if (inode1 == inode2 && loff2 < loff1) {
|
|
swap(loff1, loff2);
|
|
}
|
|
lock_extent(&BTRFS_I(inode1)->io_tree, loff1, loff1 + len - 1);
|
|
lock_extent(&BTRFS_I(inode2)->io_tree, loff2, loff2 + len - 1);
|
|
}
|
|
|
|
static int btrfs_extent_same_range(struct inode *src, u64 loff, u64 len,
|
|
struct inode *dst, u64 dst_loff)
|
|
{
|
|
const u64 bs = BTRFS_I(src)->root->fs_info->sb->s_blocksize;
|
|
int ret;
|
|
|
|
/*
|
|
* Lock destination range to serialize with concurrent readpages() and
|
|
* source range to serialize with relocation.
|
|
*/
|
|
btrfs_double_extent_lock(src, loff, dst, dst_loff, len);
|
|
ret = btrfs_clone(src, dst, loff, len, ALIGN(len, bs), dst_loff, 1);
|
|
btrfs_double_extent_unlock(src, loff, dst, dst_loff, len);
|
|
|
|
return ret;
|
|
}
|
|
|
|
static int btrfs_extent_same(struct inode *src, u64 loff, u64 olen,
|
|
struct inode *dst, u64 dst_loff)
|
|
{
|
|
int ret;
|
|
u64 i, tail_len, chunk_count;
|
|
struct btrfs_root *root_dst = BTRFS_I(dst)->root;
|
|
|
|
spin_lock(&root_dst->root_item_lock);
|
|
if (root_dst->send_in_progress) {
|
|
btrfs_warn_rl(root_dst->fs_info,
|
|
"cannot deduplicate to root %llu while send operations are using it (%d in progress)",
|
|
root_dst->root_key.objectid,
|
|
root_dst->send_in_progress);
|
|
spin_unlock(&root_dst->root_item_lock);
|
|
return -EAGAIN;
|
|
}
|
|
root_dst->dedupe_in_progress++;
|
|
spin_unlock(&root_dst->root_item_lock);
|
|
|
|
tail_len = olen % BTRFS_MAX_DEDUPE_LEN;
|
|
chunk_count = div_u64(olen, BTRFS_MAX_DEDUPE_LEN);
|
|
|
|
for (i = 0; i < chunk_count; i++) {
|
|
ret = btrfs_extent_same_range(src, loff, BTRFS_MAX_DEDUPE_LEN,
|
|
dst, dst_loff);
|
|
if (ret)
|
|
goto out;
|
|
|
|
loff += BTRFS_MAX_DEDUPE_LEN;
|
|
dst_loff += BTRFS_MAX_DEDUPE_LEN;
|
|
}
|
|
|
|
if (tail_len > 0)
|
|
ret = btrfs_extent_same_range(src, loff, tail_len, dst, dst_loff);
|
|
out:
|
|
spin_lock(&root_dst->root_item_lock);
|
|
root_dst->dedupe_in_progress--;
|
|
spin_unlock(&root_dst->root_item_lock);
|
|
|
|
return ret;
|
|
}
|
|
|
|
static noinline int btrfs_clone_files(struct file *file, struct file *file_src,
|
|
u64 off, u64 olen, u64 destoff)
|
|
{
|
|
struct inode *inode = file_inode(file);
|
|
struct inode *src = file_inode(file_src);
|
|
struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
|
|
int ret;
|
|
int wb_ret;
|
|
u64 len = olen;
|
|
u64 bs = fs_info->sb->s_blocksize;
|
|
|
|
/*
|
|
* VFS's generic_remap_file_range_prep() protects us from cloning the
|
|
* eof block into the middle of a file, which would result in corruption
|
|
* if the file size is not blocksize aligned. So we don't need to check
|
|
* for that case here.
|
|
*/
|
|
if (off + len == src->i_size)
|
|
len = ALIGN(src->i_size, bs) - off;
|
|
|
|
if (destoff > inode->i_size) {
|
|
const u64 wb_start = ALIGN_DOWN(inode->i_size, bs);
|
|
|
|
ret = btrfs_cont_expand(BTRFS_I(inode), inode->i_size, destoff);
|
|
if (ret)
|
|
return ret;
|
|
/*
|
|
* We may have truncated the last block if the inode's size is
|
|
* not sector size aligned, so we need to wait for writeback to
|
|
* complete before proceeding further, otherwise we can race
|
|
* with cloning and attempt to increment a reference to an
|
|
* extent that no longer exists (writeback completed right after
|
|
* we found the previous extent covering eof and before we
|
|
* attempted to increment its reference count).
|
|
*/
|
|
ret = btrfs_wait_ordered_range(inode, wb_start,
|
|
destoff - wb_start);
|
|
if (ret)
|
|
return ret;
|
|
}
|
|
|
|
/*
|
|
* Lock destination range to serialize with concurrent readpages() and
|
|
* source range to serialize with relocation.
|
|
*/
|
|
btrfs_double_extent_lock(src, off, inode, destoff, len);
|
|
ret = btrfs_clone(src, inode, off, olen, len, destoff, 0);
|
|
btrfs_double_extent_unlock(src, off, inode, destoff, len);
|
|
|
|
/*
|
|
* We may have copied an inline extent into a page of the destination
|
|
* range, so wait for writeback to complete before truncating pages
|
|
* from the page cache. This is a rare case.
|
|
*/
|
|
wb_ret = btrfs_wait_ordered_range(inode, destoff, len);
|
|
ret = ret ? ret : wb_ret;
|
|
/*
|
|
* Truncate page cache pages so that future reads will see the cloned
|
|
* data immediately and not the previous data.
|
|
*/
|
|
truncate_inode_pages_range(&inode->i_data,
|
|
round_down(destoff, PAGE_SIZE),
|
|
round_up(destoff + len, PAGE_SIZE) - 1);
|
|
|
|
return ret;
|
|
}
|
|
|
|
static int btrfs_remap_file_range_prep(struct file *file_in, loff_t pos_in,
|
|
struct file *file_out, loff_t pos_out,
|
|
loff_t *len, unsigned int remap_flags)
|
|
{
|
|
struct inode *inode_in = file_inode(file_in);
|
|
struct inode *inode_out = file_inode(file_out);
|
|
u64 bs = BTRFS_I(inode_out)->root->fs_info->sb->s_blocksize;
|
|
bool same_inode = inode_out == inode_in;
|
|
u64 wb_len;
|
|
int ret;
|
|
|
|
if (!(remap_flags & REMAP_FILE_DEDUP)) {
|
|
struct btrfs_root *root_out = BTRFS_I(inode_out)->root;
|
|
|
|
if (btrfs_root_readonly(root_out))
|
|
return -EROFS;
|
|
|
|
if (file_in->f_path.mnt != file_out->f_path.mnt ||
|
|
inode_in->i_sb != inode_out->i_sb)
|
|
return -EXDEV;
|
|
}
|
|
|
|
/* Don't make the dst file partly checksummed */
|
|
if ((BTRFS_I(inode_in)->flags & BTRFS_INODE_NODATASUM) !=
|
|
(BTRFS_I(inode_out)->flags & BTRFS_INODE_NODATASUM)) {
|
|
return -EINVAL;
|
|
}
|
|
|
|
/*
|
|
* Now that the inodes are locked, we need to start writeback ourselves
|
|
* and can not rely on the writeback from the VFS's generic helper
|
|
* generic_remap_file_range_prep() because:
|
|
*
|
|
* 1) For compression we must call filemap_fdatawrite_range() range
|
|
* twice (btrfs_fdatawrite_range() does it for us), and the generic
|
|
* helper only calls it once;
|
|
*
|
|
* 2) filemap_fdatawrite_range(), called by the generic helper only
|
|
* waits for the writeback to complete, i.e. for IO to be done, and
|
|
* not for the ordered extents to complete. We need to wait for them
|
|
* to complete so that new file extent items are in the fs tree.
|
|
*/
|
|
if (*len == 0 && !(remap_flags & REMAP_FILE_DEDUP))
|
|
wb_len = ALIGN(inode_in->i_size, bs) - ALIGN_DOWN(pos_in, bs);
|
|
else
|
|
wb_len = ALIGN(*len, bs);
|
|
|
|
/*
|
|
* Since we don't lock ranges, wait for ongoing lockless dio writes (as
|
|
* any in progress could create its ordered extents after we wait for
|
|
* existing ordered extents below).
|
|
*/
|
|
inode_dio_wait(inode_in);
|
|
if (!same_inode)
|
|
inode_dio_wait(inode_out);
|
|
|
|
/*
|
|
* Workaround to make sure NOCOW buffered write reach disk as NOCOW.
|
|
*
|
|
* Btrfs' back references do not have a block level granularity, they
|
|
* work at the whole extent level.
|
|
* NOCOW buffered write without data space reserved may not be able
|
|
* to fall back to CoW due to lack of data space, thus could cause
|
|
* data loss.
|
|
*
|
|
* Here we take a shortcut by flushing the whole inode, so that all
|
|
* nocow write should reach disk as nocow before we increase the
|
|
* reference of the extent. We could do better by only flushing NOCOW
|
|
* data, but that needs extra accounting.
|
|
*
|
|
* Also we don't need to check ASYNC_EXTENT, as async extent will be
|
|
* CoWed anyway, not affecting nocow part.
|
|
*/
|
|
ret = filemap_flush(inode_in->i_mapping);
|
|
if (ret < 0)
|
|
return ret;
|
|
|
|
ret = btrfs_wait_ordered_range(inode_in, ALIGN_DOWN(pos_in, bs),
|
|
wb_len);
|
|
if (ret < 0)
|
|
return ret;
|
|
ret = btrfs_wait_ordered_range(inode_out, ALIGN_DOWN(pos_out, bs),
|
|
wb_len);
|
|
if (ret < 0)
|
|
return ret;
|
|
|
|
return generic_remap_file_range_prep(file_in, pos_in, file_out, pos_out,
|
|
len, remap_flags);
|
|
}
|
|
|
|
loff_t btrfs_remap_file_range(struct file *src_file, loff_t off,
|
|
struct file *dst_file, loff_t destoff, loff_t len,
|
|
unsigned int remap_flags)
|
|
{
|
|
struct inode *src_inode = file_inode(src_file);
|
|
struct inode *dst_inode = file_inode(dst_file);
|
|
bool same_inode = dst_inode == src_inode;
|
|
int ret;
|
|
|
|
if (remap_flags & ~(REMAP_FILE_DEDUP | REMAP_FILE_ADVISORY))
|
|
return -EINVAL;
|
|
|
|
if (same_inode)
|
|
inode_lock(src_inode);
|
|
else
|
|
lock_two_nondirectories(src_inode, dst_inode);
|
|
|
|
ret = btrfs_remap_file_range_prep(src_file, off, dst_file, destoff,
|
|
&len, remap_flags);
|
|
if (ret < 0 || len == 0)
|
|
goto out_unlock;
|
|
|
|
if (remap_flags & REMAP_FILE_DEDUP)
|
|
ret = btrfs_extent_same(src_inode, off, len, dst_inode, destoff);
|
|
else
|
|
ret = btrfs_clone_files(dst_file, src_file, off, len, destoff);
|
|
|
|
out_unlock:
|
|
if (same_inode)
|
|
inode_unlock(src_inode);
|
|
else
|
|
unlock_two_nondirectories(src_inode, dst_inode);
|
|
|
|
return ret < 0 ? ret : len;
|
|
}
|