const u64 size,
const u8 comp_type,
char *inline_data,
- struct btrfs_trans_handle **trans_out)
+ struct btrfs_trans_handle **trans_out,
+ bool *copied_to_page)
{
struct btrfs_root *root = inode->root;
struct btrfs_fs_info *fs_info = root->fs_info;
btrfs_delalloc_release_space(inode, data_reserved,
new_key->offset,
fs_info->sectorsize, true);
- } else if (new_key->offset + datal > i_size_read(&inode->vfs_inode)) {
- /*
- * Keep i_size ahead of a folio we dirtied beyond EOF, so that
- * writeback sends it to disk instead of discarding it through
- * folio invalidation (see commit b48c980b6a7e).
- */
- i_size_write(&inode->vfs_inode, new_key->offset + datal);
+ } else {
+ *copied_to_page = true;
+ if (new_key->offset + datal > i_size_read(&inode->vfs_inode)) {
+ /*
+ * Keep i_size ahead of a folio we dirtied beyond EOF,
+ * so that writeback sends it to disk instead of
+ * discarding it through folio invalidation (see commit
+ * b48c980b6a7e).
+ */
+ i_size_write(&inode->vfs_inode, new_key->offset + datal);
+ }
}
btrfs_delalloc_release_extents(inode, fs_info->sectorsize);
const u64 len = olen_aligned;
u64 last_dest_end = destoff;
u64 prev_extent_end = off;
+ u64 unlocked_start = 0;
+ u64 unlocked_end = 0;
ret = -ENOMEM;
buf = kvmalloc(fs_info->nodesize, GFP_KERNEL);
u64 datao = 0, datal = 0;
u8 comp;
u64 drop_start;
+ bool copied_to_page = false;
/* Note the key will change type as we walk through the tree */
ret = btrfs_search_slot(NULL, src->root, &key, path, 0, 0);
ret = clone_copy_inline_extent(inode, path, &new_key,
drop_start, datal, size,
- comp, buf, &trans);
+ comp, buf, &trans,
+ &copied_to_page);
if (ret)
goto out;
}
destoff, olen, no_time_update);
if (ret)
goto out;
+ /*
+ * The inline copy left delalloc in our caller's locked range,
+ * and the rest of the clone may wait for a commit or a
+ * reservation ticket whose flushoncommit writeback would block
+ * on our lock. Unlock the dirtied block so that only clean data
+ * stays locked; i_rwsem keeps writers out of it until the clone
+ * finishes.
+ */
+ if (copied_to_page) {
+ unlocked_start = new_key.offset;
+ unlocked_end = ALIGN(new_key.offset + datal,
+ fs_info->sectorsize) - 1;
+ btrfs_unlock_extent(&inode->io_tree,
+ unlocked_start, unlocked_end, NULL);
+ }
if (new_key.offset + datal >= destoff + len)
break;
}
out:
+ /*
+ * Our caller unlocks the whole destination range, so lock the block
+ * unlocked above again, or its unlock could drop a lock that writeback
+ * or a read took meanwhile. With the path released we hold no
+ * transaction or tree lock, and nobody holding this block waits for the
+ * rest of our range (it is clean, and btrfs_lock_extent() drops a
+ * partial range before it waits), so this cannot deadlock.
+ */
+ if (unlocked_end) {
+ btrfs_release_path(path);
+ btrfs_lock_extent(&inode->io_tree, unlocked_start,
+ unlocked_end, NULL);
+ }
clear_bit(BTRFS_INODE_NO_DELALLOC_FLUSH, &inode->runtime_flags);
return ret;
* we are safe from concurrency with relocation of source extents
* because we have already locked the inode's i_mmap_lock in exclusive
* mode.
+ *
+ * The locked range must also have no delalloc: the clone reserves space
+ * and starts transactions under the lock, and with flushoncommit a
+ * commit (including one serving a reservation ticket) writes back dirty
+ * inodes and would block on our lock. Compressed writeback is
+ * asynchronous and can leave delalloc after the pre-lock flush, so wait
+ * and retry. Both inodes' i_rwsem and i_mmap_lock are held, so delalloc
+ * that survives two waits is a leaked EXTENT_DELALLOC bit: fail with
+ * -EUCLEAN, since -EAGAIN would have userspace retry forever.
*/
- btrfs_lock_extent(&dst->io_tree, dst_loff, end, &cached_state);
+ for (int i = 0; ; i++) {
+ btrfs_lock_extent(&dst->io_tree, dst_loff, end, &cached_state);
+ if (!btrfs_test_range_bit_exists(&dst->io_tree, dst_loff, end,
+ EXTENT_DELALLOC))
+ break;
+ btrfs_unlock_extent(&dst->io_tree, dst_loff, end, &cached_state);
+ if (i == 2) {
+ btrfs_warn_rl(fs_info,
+"leaked delalloc in locked reflink destination: root %llu ino %llu range [%llu, %llu]",
+ btrfs_root_id(dst->root), btrfs_ino(dst),
+ dst_loff, end);
+ WARN_ON_ONCE(1);
+ return -EUCLEAN;
+ }
+ ret = btrfs_wait_ordered_range(dst, dst_loff, end + 1 - dst_loff);
+ if (ret)
+ return ret;
+ }
ret = btrfs_clone(src, dst, loff, len, ALIGN(len, bs), dst_loff, true);
btrfs_unlock_extent(&dst->io_tree, dst_loff, end, &cached_state);
* we are safe from concurrency with relocation of source extents
* because we have already locked the inode's i_mmap_lock in exclusive
* mode.
+ *
+ * The locked range must also have no delalloc, see
+ * btrfs_extent_same_range().
*/
end = round_up(destoff + len, bs) - 1;
- btrfs_lock_extent(&inode->io_tree, destoff, end, &cached_state);
+ for (int i = 0; ; i++) {
+ btrfs_lock_extent(&inode->io_tree, destoff, end, &cached_state);
+ if (!btrfs_test_range_bit_exists(&inode->io_tree, destoff, end,
+ EXTENT_DELALLOC))
+ break;
+ btrfs_unlock_extent(&inode->io_tree, destoff, end, &cached_state);
+ if (i == 2) {
+ btrfs_warn_rl(fs_info,
+"leaked delalloc in locked reflink destination: root %llu ino %llu range [%llu, %llu]",
+ btrfs_root_id(inode->root), btrfs_ino(inode),
+ destoff, end);
+ WARN_ON_ONCE(1);
+ return -EUCLEAN;
+ }
+ ret = btrfs_wait_ordered_range(inode, destoff, end + 1 - destoff);
+ if (ret)
+ return ret;
+ }
ret = btrfs_clone(src, inode, off, olen, len, destoff, false);
btrfs_unlock_extent(&inode->io_tree, destoff, end, &cached_state);
if (ret < 0)