From patchwork Mon Jun 23 12:00:44 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Filipe Manana X-Patchwork-Id: 4401371 Return-Path: X-Original-To: patchwork-linux-btrfs@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork2.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.19.201]) by patchwork2.web.kernel.org (Postfix) with ESMTP id 83EA5BEEAA for ; Mon, 23 Jun 2014 11:01:24 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 3DA93201BB for ; Mon, 23 Jun 2014 11:01:20 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 51ABF2018E for ; Mon, 23 Jun 2014 11:01:17 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753403AbaFWLBO (ORCPT ); Mon, 23 Jun 2014 07:01:14 -0400 Received: from mail-wi0-f174.google.com ([209.85.212.174]:58670 "EHLO mail-wi0-f174.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751066AbaFWLBN (ORCPT ); Mon, 23 Jun 2014 07:01:13 -0400 Received: by mail-wi0-f174.google.com with SMTP id bs8so3949495wib.1 for ; Mon, 23 Jun 2014 04:01:08 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=2eqddKjWmtoHytv9tFX8utWCOAGuOLEszcPiR0bRHOE=; b=AqhvgADUOwIiCQzIElUxKv4T2Ns/h40ephLzpd9IPespfJdfUWtEISpLSyWnfvPgzj ivCocZflLzhPJFhLUcTP+KhtPMD6bKOpv28hy9A2wNi8cB/OSrazGIaoKWWKq0i8AaJU 2eJPh+A0y4mQVV2+21FOkogLQzdFlzSCM7/Wf0lQ9nS3aeTAM84re5Jj0tJETCyGny7i QciHd4fbWwm85jPUVquheiUsxJv8IVYXNjOT17NZiFAyduMENgnfnL7kuX51exXr5Fe2 5gCxQFzcGJCdz2+mvHV+Z7JvDToqwcEn2uxQzYUsUsC6uVXelr/kNKancN4hb1PCDO23 ze6Q== X-Received: by 10.194.91.144 with SMTP id ce16mr26030128wjb.18.1403521268355; Mon, 23 Jun 2014 04:01:08 -0700 (PDT) Received: from debian-vm3.lan (bl5-3-159.dsl.telepac.pt. [82.154.3.159]) by mx.google.com with ESMTPSA id ex4sm32375427wic.2.2014.06.23.04.01.07 for (version=TLSv1.2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Mon, 23 Jun 2014 04:01:07 -0700 (PDT) From: Filipe David Borba Manana To: linux-btrfs@vger.kernel.org Cc: Filipe David Borba Manana Subject: [PATCH 3/6 v5] Btrfs: send, use fallocate command to punch holes Date: Mon, 23 Jun 2014 13:00:44 +0100 Message-Id: <1403524844-32612-1-git-send-email-fdmanana@gmail.com> X-Mailer: git-send-email 1.9.1 In-Reply-To: <1398002695-10084-1-git-send-email-fdmanana@gmail.com> References: <1398002695-10084-1-git-send-email-fdmanana@gmail.com> Sender: linux-btrfs-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-btrfs@vger.kernel.org X-Spam-Status: No, score=-6.8 required=5.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED, DKIM_SIGNED, FREEMAIL_FROM, RCVD_IN_DNSWL_HI, T_DKIM_INVALID, T_RP_MATCHES_RCVD, UNPARSEABLE_RELAY autolearn=ham version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Instead of sending a write command with a data buffer filled with 0 value bytes, use the fallocate command, introduced in the send stream version 2, to tell the receiver to punch a file hole using the fallocate system call. Signed-off-by: Filipe David Borba Manana --- V2: A v2 stream is now only produced if the send ioctl caller passes in one of the new flags (BTRFS_SEND_FLAG_CALCULATE_DATA_SIZE | BTRFS_SEND_FLAG_SUPPORT_FALLOCATE) to avoid breaking old clients. V3: Added missing path allocation, messed up rebase. V4: Removed BTRFS_SEND_FLAG_SUPPORT_FALLOCATE and added BTRFS_SEND_FLAG_STREAM_V2, added commands for inode set flags and otime. V5: Rebased against latest chris/integration branch. fs/btrfs/send.c | 55 ++++++++++++++++++++++++++++++++++++++++++++++++++++--- fs/btrfs/send.h | 4 ++++ 2 files changed, 56 insertions(+), 3 deletions(-) diff --git a/fs/btrfs/send.c b/fs/btrfs/send.c index dd6f5ec..300eaee 100644 --- a/fs/btrfs/send.c +++ b/fs/btrfs/send.c @@ -569,6 +569,7 @@ static int tlv_put(struct send_ctx *sctx, u16 attr, const void *data, int len) return tlv_put(sctx, attr, &__tmp, sizeof(__tmp)); \ } +TLV_PUT_DEFINE_INT(32) TLV_PUT_DEFINE_INT(64) static int tlv_put_string(struct send_ctx *sctx, u16 attr, @@ -4500,18 +4501,59 @@ out: return ret; } +static int send_fallocate(struct send_ctx *sctx, u32 flags, + u64 offset, u64 len) +{ + struct fs_path *p = NULL; + int ret = 0; + + ASSERT(sctx->flags & BTRFS_SEND_FLAG_STREAM_V2); + + if (sctx->phase == SEND_PHASE_COMPUTE_DATA_SIZE) { + sctx->total_data_size += len; + return 0; + } + + p = fs_path_alloc(); + if (!p) + return -ENOMEM; + ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p); + if (ret < 0) + goto out; + + ret = begin_cmd(sctx, BTRFS_SEND_C_FALLOCATE); + if (ret < 0) + goto out; + TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p); + TLV_PUT_U32(sctx, BTRFS_SEND_A_FALLOCATE_FLAGS, flags); + TLV_PUT_U64(sctx, BTRFS_SEND_A_FILE_OFFSET, offset); + TLV_PUT_U64(sctx, BTRFS_SEND_A_SIZE, len); + ret = send_cmd(sctx); + +tlv_put_failure: +out: + fs_path_free(p); + return ret; +} + static int send_hole(struct send_ctx *sctx, u64 end) { struct fs_path *p = NULL; u64 offset = sctx->cur_inode_last_extent; - u64 len; + u64 len = end - offset; int ret = 0; if (sctx->phase == SEND_PHASE_COMPUTE_DATA_SIZE) { - sctx->total_data_size += end - offset; + sctx->total_data_size += len; return 0; } + if (sctx->flags & BTRFS_SEND_FLAG_STREAM_V2) + return send_fallocate(sctx, + BTRFS_SEND_PUNCH_HOLE_FALLOC_FLAGS, + offset, + len); + p = fs_path_alloc(); if (!p) return -ENOMEM; @@ -4568,7 +4610,8 @@ static int send_write_or_clone(struct send_ctx *sctx, len = btrfs_file_extent_num_bytes(path->nodes[0], ei); } - if (offset + len > sctx->cur_inode_size) + if (offset < sctx->cur_inode_size && + offset + len > sctx->cur_inode_size) len = sctx->cur_inode_size - offset; if (len == 0) { ret = 0; @@ -4585,6 +4628,12 @@ static int send_write_or_clone(struct send_ctx *sctx, ret = send_clone(sctx, offset, len, clone_root); } else if (sctx->flags & BTRFS_SEND_FLAG_NO_FILE_DATA) { ret = send_update_extent(sctx, offset, len); + } else if (btrfs_file_extent_disk_bytenr(path->nodes[0], ei) == 0 && + type != BTRFS_FILE_EXTENT_INLINE && + (sctx->flags & BTRFS_SEND_FLAG_STREAM_V2) && + offset < sctx->cur_inode_size) { + ret = send_fallocate(sctx, BTRFS_SEND_PUNCH_HOLE_FALLOC_FLAGS, + offset, len); } else { while (pos < len) { l = len - pos; diff --git a/fs/btrfs/send.h b/fs/btrfs/send.h index 96f583c..987936c 100644 --- a/fs/btrfs/send.h +++ b/fs/btrfs/send.h @@ -148,6 +148,10 @@ enum { #define BTRFS_SEND_A_FALLOCATE_FLAG_KEEP_SIZE (1 << 0) #define BTRFS_SEND_A_FALLOCATE_FLAG_PUNCH_HOLE (1 << 1) +#define BTRFS_SEND_PUNCH_HOLE_FALLOC_FLAGS \ + (BTRFS_SEND_A_FALLOCATE_FLAG_KEEP_SIZE | \ + BTRFS_SEND_A_FALLOCATE_FLAG_PUNCH_HOLE) + #ifdef __KERNEL__ long btrfs_ioctl_send(struct file *mnt_file, void __user *arg); #endif