From patchwork Thu Feb 6 13:59:12 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Disseldorp X-Patchwork-Id: 3596261 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 89D49C02DC for ; Thu, 6 Feb 2014 13:59:39 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 8F7B72016C for ; Thu, 6 Feb 2014 13:59:38 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 6ACAE20163 for ; Thu, 6 Feb 2014 13:59:37 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S932906AbaBFN72 (ORCPT ); Thu, 6 Feb 2014 08:59:28 -0500 Received: from cantor2.suse.de ([195.135.220.15]:42936 "EHLO mx2.suse.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S932359AbaBFN7W (ORCPT ); Thu, 6 Feb 2014 08:59:22 -0500 Received: from relay2.suse.de (charybdis-ext.suse.de [195.135.220.254]) by mx2.suse.de (Postfix) with ESMTP id A213CABCF; Thu, 6 Feb 2014 13:59:21 +0000 (UTC) From: David Disseldorp To: xfs@oss.sgi.com Cc: dsterba@suse.cz, linux-btrfs@vger.kernel.org, David Disseldorp Subject: [PATCH v2 1/3] btrfs: add small program for clone testing Date: Thu, 6 Feb 2014 14:59:12 +0100 Message-Id: <1391695154-2039-2-git-send-email-ddiss@suse.de> X-Mailer: git-send-email 1.8.4.5 In-Reply-To: <1391695154-2039-1-git-send-email-ddiss@suse.de> References: <1391695154-2039-1-git-send-email-ddiss@suse.de> 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, RCVD_IN_DNSWL_HI, RP_MATCHES_RCVD, UNPARSEABLE_RELAY autolearn=unavailable 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 cloner program is capable of cloning files using the BTRFS_IOC_CLONE and BTRFS_IOC_CLONE_RANGE ioctls. Signed-off-by: David Disseldorp --- .gitignore | 1 + src/Makefile | 2 +- src/cloner.c | 188 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 190 insertions(+), 1 deletion(-) create mode 100644 src/cloner.c diff --git a/.gitignore b/.gitignore index ee4cb41..b6f2463 100644 --- a/.gitignore +++ b/.gitignore @@ -104,6 +104,7 @@ /src/aio-dio-regress/aio-free-ring-with-bogus-nr-pages /src/aio-dio-regress/aio-io-setup-with-nonwritable-context-pointer /src/aio-dio-regress/aiodio_sparse2 +/src/cloner # dmapi/ binaries /dmapi/src/common/cmd/read_invis diff --git a/src/Makefile b/src/Makefile index 84c8297..6509f2d 100644 --- a/src/Makefile +++ b/src/Makefile @@ -18,7 +18,7 @@ LINUX_TARGETS = xfsctl bstat t_mtab getdevicesize preallo_rw_pattern_reader \ locktest unwritten_mmap bulkstat_unlink_test t_stripealign \ bulkstat_unlink_test_modified t_dir_offset t_futimens t_immutable \ stale_handle pwrite_mmap_blocked t_dir_offset2 seek_sanity_test \ - seek_copy_test t_readdir_1 t_readdir_2 fsync-tester nsexec + seek_copy_test t_readdir_1 t_readdir_2 fsync-tester nsexec cloner SUBDIRS = diff --git a/src/cloner.c b/src/cloner.c new file mode 100644 index 0000000..dfce837 --- /dev/null +++ b/src/cloner.c @@ -0,0 +1,188 @@ +/* + * Tiny program to perform file (range) clones using raw Btrfs ioctls. + * It should only be needed until btrfs-progs has an xfs_io equivalent. + * + * Copyright (C) 2014 SUSE Linux Products GmbH. All Rights Reserved. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +struct btrfs_ioctl_clone_range_args { + int64_t src_fd; + uint64_t src_offset; + uint64_t src_length; + uint64_t dest_offset; +}; + +#define BTRFS_IOCTL_MAGIC 0x94 +#define BTRFS_IOC_CLONE _IOW(BTRFS_IOCTL_MAGIC, 9, int) +#define BTRFS_IOC_CLONE_RANGE _IOW(BTRFS_IOCTL_MAGIC, 13, \ + struct btrfs_ioctl_clone_range_args) + +static void +usage(char *name, const char *msg) +{ + printf("Fatal: %s\n" + "Usage:\n" + "%s [options] \n" + "\tA full file clone (reflink) is performed by default, " + "unless any of the following are specified:\n" + "\t-s : source file offset (default = 0)\n" + "\t-d : destination file offset (default = 0)\n" + "\t-l : length of clone (default = 0)\n", + msg, name); + _exit(1); +} + +static int +clone_file(int src_fd, int dst_fd) +{ + int ret = ioctl(dst_fd, BTRFS_IOC_CLONE, src_fd); + if (ret != 0) + ret = errno; + return ret; +} + +static int +clone_file_range(int src_fd, int dst_fd, uint64_t src_off, uint64_t dst_off, + uint64_t len) +{ + struct btrfs_ioctl_clone_range_args cr_args; + int ret; + + memset(&cr_args, 0, sizeof(cr_args)); + cr_args.src_fd = src_fd; + cr_args.src_offset = src_off; + cr_args.src_length = len; + cr_args.dest_offset = dst_off; + ret = ioctl(dst_fd, BTRFS_IOC_CLONE_RANGE, &cr_args); + if (ret != 0) + ret = errno; + return ret; +} + +int +main(int argc, char **argv) +{ + bool full_file = true; + uint64_t src_off = 0; + uint64_t dst_off = 0; + uint64_t len = 0; + char *src_file; + int src_fd; + char *dst_file; + int dst_fd; + int ret; + int opt; + + while ((opt = getopt(argc, argv, "s:d:l:")) != -1) { + char *sval_end; + switch (opt) { + case 's': + errno = 0; + src_off = strtoull(optarg, &sval_end, 10); + if ((errno) || (*sval_end != '\0')) + usage(argv[0], "invalid source offset"); + full_file = false; + break; + case 'd': + errno = 0; + dst_off = strtoull(optarg, &sval_end, 10); + if ((errno) || (*sval_end != '\0')) + usage(argv[0], "invalid destination offset"); + full_file = false; + break; + case 'l': + errno = 0; + len = strtoull(optarg, &sval_end, 10); + if ((errno) || (*sval_end != '\0')) + usage(argv[0], "invalid length"); + full_file = false; + break; + default: + usage(argv[0], "invalid argument"); + } + } + + /* should be exactly two args left */ + if (optind != argc - 2) + usage(argv[0], "src_file and dst_file arguments are madatory"); + + src_file = (char *)strdup(argv[optind++]); + if (src_file == NULL) { + ret = ENOMEM; + printf("no memory\n"); + goto err_out; + } + dst_file = (char *)strdup(argv[optind++]); + if (dst_file == NULL) { + ret = ENOMEM; + printf("no memory\n"); + goto err_src_free; + } + + src_fd = open(src_file, O_RDONLY); + if (src_fd == -1) { + ret = errno; + printf("failed to open %s: %s\n", src_file, strerror(errno)); + goto err_dst_free; + } + dst_fd = open(dst_file, O_CREAT | O_WRONLY, 0644); + if (dst_fd == -1) { + ret = errno; + printf("failed to open %s: %s\n", dst_file, strerror(errno)); + goto err_src_close; + } + + if (full_file) { + ret = clone_file(src_fd, dst_fd); + } else { + ret = clone_file_range(src_fd, dst_fd, src_off, dst_off, len); + } + if (ret != 0) { + printf("clone failed: %s\n", strerror(ret)); + goto err_dst_close; + } + + ret = 0; +err_dst_close: + if (close(dst_fd)) { + ret |= errno; + printf("failed to close dst file: %s\n", strerror(errno)); + } +err_src_close: + if (close(src_fd)) { + ret |= errno; + printf("failed to close src file: %s\n", strerror(errno)); + } +err_dst_free: + free(dst_file); +err_src_free: + free(src_file); +err_out: + return ret; +}