From patchwork Wed Apr 16 14:57:43 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Filipe Manana X-Patchwork-Id: 4001751 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 EE78ABFF02 for ; Wed, 16 Apr 2014 13:57:15 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id E6F7320306 for ; Wed, 16 Apr 2014 13:57:14 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id D753020303 for ; Wed, 16 Apr 2014 13:57:13 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1161346AbaDPN5J (ORCPT ); Wed, 16 Apr 2014 09:57:09 -0400 Received: from mail-we0-f173.google.com ([74.125.82.173]:64421 "EHLO mail-we0-f173.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1161077AbaDPN5F (ORCPT ); Wed, 16 Apr 2014 09:57:05 -0400 Received: by mail-we0-f173.google.com with SMTP id w61so11029729wes.32 for ; Wed, 16 Apr 2014 06:57:04 -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=nw4b42ebXWePVs3Sg9AXuD5LwF9uTTqWdJDF1q2wkEA=; b=fC6BOTBvYnA1/pQqdU/LegMsJxDh3UCOp/okZCp1l4pKyoGlj69/xZaUx1ig/9bZso hLzrpnNeN6NFA9k4g8kgq72exhWeX+7Ds90dVD+XxC4XXIEhudsrq+C5WZj+pP9n5jD+ bj5q0gPdA36NzG03vs/Z0wGFwfoXBzogjHg3Ep32CBravQ2fBshAjyBBZ7hkY4zL7Lz4 yMF5Crd9IV7wS/h2ALKeoOxaxc7qdzy0snT6v8ic25pauLrmmYT2r1vp1Lt5455gzP6h 54aV16Tc6zNWk5uTperUV2nQ9ayk74w1PvdgZvGC8tEOeIZ0eW/ZrLk6wpdCwsrcXnjZ Gc3A== X-Received: by 10.180.74.210 with SMTP id w18mr7830748wiv.10.1397656624120; Wed, 16 Apr 2014 06:57:04 -0700 (PDT) Received: from debian-vm3.lan (bl10-198-83.dsl.telepac.pt. [85.243.198.83]) by mx.google.com with ESMTPSA id ff9sm36123415wib.11.2014.04.16.06.57.02 for (version=TLSv1.2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Wed, 16 Apr 2014 06:57:03 -0700 (PDT) From: Filipe David Borba Manana To: linux-btrfs@vger.kernel.org Cc: Filipe David Borba Manana Subject: [PATCH 3/4 v2] Btrfs-progs: send, implement fallocate command callback Date: Wed, 16 Apr 2014 15:57:43 +0100 Message-Id: <1397660263-8098-1-git-send-email-fdmanana@gmail.com> X-Mailer: git-send-email 1.9.1 In-Reply-To: <1397580051-26643-3-git-send-email-fdmanana@gmail.com> References: <1397580051-26643-3-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=-7.4 required=5.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED, DKIM_SIGNED, FREEMAIL_FROM, RCVD_IN_DNSWL_HI, RP_MATCHES_RCVD, T_DKIM_INVALID, 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 The fallocate send stream command, added in stream version 2, is used to pre-allocate space for files and punch file holes. This change implements the callback for that new command, using the fallocate function from the standard C library to carry out the specified action (allocate file space or punch a file hole). Signed-off-by: Filipe David Borba Manana --- V2: Use the new send ioctl flag BTRFS_SEND_FLAG_SUPPORT_FALLOCATE if the user asks for it (-a command line option), which will make the kernel generate a version 2 send stream, so that old clients aren't affected. Documentation/btrfs-send.txt | 3 +++ cmds-receive.c | 38 ++++++++++++++++++++++++++++++++++++++ cmds-send.c | 12 ++++++++++-- send-stream.c | 13 +++++++++++++ send-stream.h | 2 ++ 5 files changed, 66 insertions(+), 2 deletions(-) diff --git a/Documentation/btrfs-send.txt b/Documentation/btrfs-send.txt index 38470b0..e96be07 100644 --- a/Documentation/btrfs-send.txt +++ b/Documentation/btrfs-send.txt @@ -43,6 +43,9 @@ An alternative would be to use pipes. -o:: Obtain the total data size for each subvolume or snapshot to send. This demands additional processing (mostly IO bound) but is useful for the receive command to report progress. +-a:: +Use fallocate to pre-allocate file extents and to punch file holes, instead of writing zeroes +to files. EXIT STATUS ----------- diff --git a/cmds-receive.c b/cmds-receive.c index 19300fc..3f30066 100644 --- a/cmds-receive.c +++ b/cmds-receive.c @@ -41,6 +41,7 @@ #include #include #include +#include #include "ctree.h" #include "ioctl.h" @@ -887,6 +888,42 @@ out: return ret; } +static int process_fallocate(const char *path, u32 flags, u64 offset, + u64 len, void *user) +{ + struct btrfs_receive *r = user; + char *full_path = path_cat(r->full_subvol_path, path); + int mode = 0; + int ret; + + if (flags & BTRFS_SEND_A_FALLOCATE_FLAG_KEEP_SIZE) + mode |= FALLOC_FL_KEEP_SIZE; + if (flags & BTRFS_SEND_A_FALLOCATE_FLAG_PUNCH_HOLE) + mode |= FALLOC_FL_PUNCH_HOLE; + + if (g_verbose >= 2) + fprintf(stderr, + "fallocate %s - flags %u, offset %llu, len %llu\n", + path, flags, offset, len); + + ret = open_inode_for_write(r, full_path); + if (ret < 0) + goto out; + + ret = fallocate(r->write_fd, mode, offset, len); + if (ret) { + ret = -errno; + fprintf(stderr, + "ERROR: fallocate against %s failed. %s\n", + path, strerror(-ret)); + goto out; + } + update_progress(r, len); + +out: + free(full_path); + return ret; +} static struct btrfs_send_ops send_ops = { .subvol = process_subvol, @@ -910,6 +947,7 @@ static struct btrfs_send_ops send_ops = { .chown = process_chown, .utimes = process_utimes, .total_data_size = process_total_data_size, + .fallocate = process_fallocate, }; static int do_receive(struct btrfs_receive *r, const char *tomnt, int r_fd) diff --git a/cmds-send.c b/cmds-send.c index 69f5ba1..2a62e68 100644 --- a/cmds-send.c +++ b/cmds-send.c @@ -46,6 +46,7 @@ static int g_verbose = 0; static int g_total_data_size = 0; +static int g_fallocate = 0; struct btrfs_send { int send_fd; @@ -284,6 +285,8 @@ static int do_send(struct btrfs_send *send, u64 parent_root_id, io_send.flags |= BTRFS_SEND_FLAG_OMIT_END_CMD; if (g_total_data_size) io_send.flags |= BTRFS_SEND_FLAG_CALCULATE_DATA_SIZE; + if (g_fallocate) + io_send.flags |= BTRFS_SEND_FLAG_SUPPORT_FALLOCATE; ret = ioctl(subvol_fd, BTRFS_IOC_SEND, &io_send); if (ret) { ret = -errno; @@ -427,7 +430,7 @@ int cmd_send(int argc, char **argv) memset(&send, 0, sizeof(send)); send.dump_fd = fileno(stdout); - while ((c = getopt(argc, argv, "veoc:f:i:p:")) != -1) { + while ((c = getopt(argc, argv, "veoac:f:i:p:")) != -1) { switch (c) { case 'v': g_verbose++; @@ -517,6 +520,9 @@ int cmd_send(int argc, char **argv) case 'o': g_total_data_size = 1; break; + case 'a': + g_fallocate = 1; + break; case '?': default: fprintf(stderr, "ERROR: send args invalid.\n"); @@ -679,7 +685,7 @@ out: } const char * const cmd_send_usage[] = { - "btrfs send [-veo] [-p ] [-c ] [-f ] [...]", + "btrfs send [-veoa] [-p ] [-c ] [-f ] [...]", "Send the subvolume(s) to stdout.", "Sends the subvolume(s) specified by to stdout.", "By default, this will send the whole subvolume. To do an incremental", @@ -707,5 +713,7 @@ const char * const cmd_send_usage[] = { " snapshot to send. This demands additional processing", " (mostly IO bound) but is useful for the receive ", " command to report progress.", + "-a Use fallocate to pre-allocate file extents and to", + " punch file holes, instead of writing zeroes to files.", NULL }; diff --git a/send-stream.c b/send-stream.c index 474d012..f117533 100644 --- a/send-stream.c +++ b/send-stream.c @@ -425,6 +425,19 @@ static int read_and_process_cmd(struct btrfs_send_stream *s) TLV_GET_U64(s, BTRFS_SEND_A_SIZE, &tmp); ret = s->ops->total_data_size(tmp, s->user); break; + case BTRFS_SEND_C_FALLOCATE: + { + u32 flags; + u64 len; + + TLV_GET_STRING(s, BTRFS_SEND_A_PATH, &path); + TLV_GET_U32(s, BTRFS_SEND_A_FALLOCATE_FLAGS, &flags); + TLV_GET_U64(s, BTRFS_SEND_A_FILE_OFFSET, &offset); + TLV_GET_U64(s, BTRFS_SEND_A_SIZE, &len); + ret = s->ops->fallocate(path, flags, offset, len, + s->user); + } + break; case BTRFS_SEND_C_END: ret = 1; break; diff --git a/send-stream.h b/send-stream.h index 3a653a9..479e40c 100644 --- a/send-stream.h +++ b/send-stream.h @@ -55,6 +55,8 @@ struct btrfs_send_ops { void *user); int (*update_extent)(const char *path, u64 offset, u64 len, void *user); int (*total_data_size)(u64 size, void *user); + int (*fallocate)(const char *path, u32 flags, u64 offset, + u64 len, void *user); }; int btrfs_read_and_process_send_stream(int fd,