From patchwork Tue Sep 11 11:34:17 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lu Fengqi X-Patchwork-Id: 10595533 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id D890615E2 for ; Tue, 11 Sep 2018 11:34:33 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id C842528FCB for ; Tue, 11 Sep 2018 11:34:33 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id BC23829052; Tue, 11 Sep 2018 11:34:33 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-7.9 required=2.0 tests=BAYES_00,MAILING_LIST_MULTI, RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 5BBD228FCB for ; Tue, 11 Sep 2018 11:34:33 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726808AbeIKQd1 (ORCPT ); Tue, 11 Sep 2018 12:33:27 -0400 Received: from mail.cn.fujitsu.com ([183.91.158.132]:59576 "EHLO heian.cn.fujitsu.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1726601AbeIKQd1 (ORCPT ); Tue, 11 Sep 2018 12:33:27 -0400 X-IronPort-AV: E=Sophos;i="5.43,368,1503331200"; d="scan'208";a="44778090" Received: from unknown (HELO cn.fujitsu.com) ([10.167.33.5]) by heian.cn.fujitsu.com with ESMTP; 11 Sep 2018 19:34:28 +0800 Received: from G08CNEXCHPEKD01.g08.fujitsu.local (unknown [10.167.33.80]) by cn.fujitsu.com (Postfix) with ESMTP id C313F4B6ADFF for ; Tue, 11 Sep 2018 19:34:25 +0800 (CST) Received: from fnst.localdomain (10.167.226.155) by G08CNEXCHPEKD01.g08.fujitsu.local (10.167.33.89) with Microsoft SMTP Server (TLS) id 14.3.408.0; Tue, 11 Sep 2018 19:34:29 +0800 From: Lu Fengqi To: Subject: [RFC PATCH v2 2/2] btrfs-progs: subvolume: undelete: add btrfs subvolume undelete subcommand Date: Tue, 11 Sep 2018 19:34:17 +0800 Message-ID: <20180911113417.26466-2-lufq.fnst@cn.fujitsu.com> X-Mailer: git-send-email 2.18.0 In-Reply-To: <20180911113417.26466-1-lufq.fnst@cn.fujitsu.com> References: <20180911112903.25985-1-lufq.fnst@cn.fujitsu.com> <20180911113417.26466-1-lufq.fnst@cn.fujitsu.com> MIME-Version: 1.0 X-Originating-IP: [10.167.226.155] X-yoursite-MailScanner-ID: C313F4B6ADFF.A13EC X-yoursite-MailScanner: Found to be clean X-yoursite-MailScanner-From: lufq.fnst@cn.fujitsu.com Sender: linux-btrfs-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-btrfs@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Add the undelete subcommand, this is depend on the BTRFS_IOC_SUBVOL_UNDELETE ioctl. Signed-off-by: Lu Fengqi --- btrfs-completion | 2 +- cmds-subvolume.c | 70 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 71 insertions(+), 1 deletion(-) diff --git a/btrfs-completion b/btrfs-completion index ae683f4ecf61..2b43fbd63023 100644 --- a/btrfs-completion +++ b/btrfs-completion @@ -30,7 +30,7 @@ _btrfs() local cmd=${words[1]} commands='subvolume filesystem balance device scrub check rescue restore inspect-internal property send receive quota qgroup replace help version' - commands_subvolume='create delete list snapshot find-new get-default set-default show sync' + commands_subvolume='create delete list snapshot find-new get-default set-default show sync undelete' commands_filesystem='defragment sync resize show df du label usage' commands_balance='start pause cancel resume status' commands_device='scan add delete remove ready stats usage' diff --git a/cmds-subvolume.c b/cmds-subvolume.c index e7a884af1f5d..228d0b9e9b34 100644 --- a/cmds-subvolume.c +++ b/cmds-subvolume.c @@ -1219,6 +1219,74 @@ out: return !!ret; } +static const char * const cmd_subvol_undelete_usage[] = { + "btrfs subvolume undelete [-n ] ", + "Undelete the subvolume of the given to .", + "", + "-n recover the subvolume with .", + NULL +}; + +static int cmd_subvol_undelete(int argc, char **argv) +{ + struct btrfs_ioctl_subvol_undelete_args args; + bool need_assign_name = true; + DIR *dirstream = NULL; + char *dest; + int fd = -1; + int ret; + + memset(&args, 0, sizeof(args)); + + while (1) { + int c = getopt(argc, argv, "n:"); + + if (c < 0) + break; + + switch (c) { + case 'n': + strncpy_null(args.name, optarg); + need_assign_name = false; + break; + default: + usage(cmd_subvol_undelete_usage); + } + } + if (!need_assign_name) { + if (!test_issubvolname(args.name)) { + error("invalid subvolume name: %s", args.name); + return -EINVAL; + } else if (strlen(args.name) > BTRFS_VOL_NAME_MAX) { + error("subvolume name too long: %s", args.name); + return -EINVAL; + } + } + + if (check_argc_exact(argc - optind, 2)) + usage(cmd_subvol_undelete_usage); + + args.subvol_id = arg_strtou64(argv[optind]); + if (need_assign_name) + snprintf(args.name, BTRFS_VOL_NAME_MAX, "sub_%llu", + args.subvol_id); + + dest = argv[optind + 1]; + fd = btrfs_open_dir(dest, &dirstream, 1); + if (fd < 0) { + error("can't access '%s'", dest); + return -1; + } + + ret = ioctl(fd, BTRFS_IOC_SUBVOL_UNDELETE, &args); + if (ret) + perror("BTRFS_IOC_SUBVOL_UNDELETE"); + + close_file_or_dir(fd, dirstream); + + return ret; +} + static const char subvolume_cmd_group_info[] = "manage subvolumes: create, delete, list, etc"; @@ -1237,6 +1305,8 @@ const struct cmd_group subvolume_cmd_group = { NULL, 0 }, { "show", cmd_subvol_show, cmd_subvol_show_usage, NULL, 0 }, { "sync", cmd_subvol_sync, cmd_subvol_sync_usage, NULL, 0 }, + { "undelete", cmd_subvol_undelete, cmd_subvol_undelete_usage, + NULL, 0 }, NULL_CMD_STRUCT } };