From patchwork Mon May 7 03:10:31 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lu Fengqi X-Patchwork-Id: 10383167 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 6E8A36053A for ; Mon, 7 May 2018 03:11:00 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 5974528478 for ; Mon, 7 May 2018 03:11:00 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 4E17128B21; Mon, 7 May 2018 03:11:00 +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 B86BD28478 for ; Mon, 7 May 2018 03:10:59 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751991AbeEGDK5 (ORCPT ); Sun, 6 May 2018 23:10:57 -0400 Received: from mail.cn.fujitsu.com ([183.91.158.132]:12219 "EHLO heian.cn.fujitsu.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1751889AbeEGDKs (ORCPT ); Sun, 6 May 2018 23:10:48 -0400 X-IronPort-AV: E=Sophos;i="5.43,368,1503331200"; d="scan'208";a="39622273" Received: from bogon (HELO cn.fujitsu.com) ([10.167.33.5]) by heian.cn.fujitsu.com with ESMTP; 07 May 2018 11:10:44 +0800 Received: from G08CNEXCHPEKD01.g08.fujitsu.local (unknown [10.167.33.80]) by cn.fujitsu.com (Postfix) with ESMTP id 0AD874B3ED68 for ; Mon, 7 May 2018 11:10:46 +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.361.1; Mon, 7 May 2018 11:10:47 +0800 From: Lu Fengqi To: Subject: [PATCH v3 08/10] btrfs-progs: undelete-subvol: add undelete-subvol subcommand Date: Mon, 7 May 2018 11:10:31 +0800 Message-ID: <20180507031033.4125-9-lufq.fnst@cn.fujitsu.com> X-Mailer: git-send-email 2.17.0 In-Reply-To: <20180507031033.4125-1-lufq.fnst@cn.fujitsu.com> References: <20180507031033.4125-1-lufq.fnst@cn.fujitsu.com> MIME-Version: 1.0 X-Originating-IP: [10.167.226.155] X-yoursite-MailScanner-ID: 0AD874B3ED68.AE62E 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-subvol subcommand for btrfs rescue. This subcommand is used to recover deleted subvolume left intact on the device. Signed-off-by: Lu Fengqi --- V3: remove OPEN_CTREE_PARTIAL flag. V2: add -s option to specify subvol_id. cmds-rescue.c | 69 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) diff --git a/cmds-rescue.c b/cmds-rescue.c index c40088ad374e..536d6b37ac50 100644 --- a/cmds-rescue.c +++ b/cmds-rescue.c @@ -25,6 +25,7 @@ #include "disk-io.h" #include "commands.h" #include "utils.h" +#include "undelete-subvol.h" #include "help.h" static const char * const rescue_cmd_group_usage[] = { @@ -248,6 +249,72 @@ out: return !!ret; } +static const char * const cmd_rescue_undelete_subvol_usage[] = { + "btrfs rescue undelete-subvol [-s ] ", + "Undelete deleted subvolume", + "All deleted subvolume that still left intact on the device will be", + "recovered. If -s option is given, then just recover the", + "subvolume which specified by .", + "", + "-s specify the subvolume which will be recovered.", + NULL +}; + +static int cmd_rescue_undelete_subvol(int argc, char **argv) +{ + struct btrfs_fs_info *fs_info; + char *devname; + u64 subvol_id = 0; + int ret; + + while (1) { + int c = getopt(argc, argv, "s:"); + + if (c < 0) + break; + switch (c) { + case 's': + subvol_id = arg_strtou64(optarg); + if (!is_fstree(subvol_id)) { + error("%llu is not a valid subvolume id", + subvol_id); + ret = -EINVAL; + goto out; + } + break; + default: + usage(cmd_rescue_undelete_subvol_usage); + } + } + + if (check_argc_exact(argc - optind, 1)) + usage(cmd_rescue_undelete_subvol_usage); + + devname = argv[optind]; + ret = check_mounted(devname); + if (ret < 0) { + error("could not check mount status: %s", strerror(-ret)); + goto out; + } else if (ret) { + error("%s is currently mounted", devname); + ret = -EBUSY; + goto out; + } + + fs_info = open_ctree_fs_info(devname, 0, 0, 0, OPEN_CTREE_WRITES); + if (!fs_info) { + error("could not open btrfs"); + ret = -EIO; + goto out; + } + + ret = btrfs_undelete_subvols(fs_info, subvol_id); + + close_ctree(fs_info->tree_root); +out: + return ret; +} + static const char rescue_cmd_group_info[] = "toolbox for specific rescue operations"; @@ -260,6 +327,8 @@ const struct cmd_group rescue_cmd_group = { { "zero-log", cmd_rescue_zero_log, cmd_rescue_zero_log_usage, NULL, 0}, { "fix-device-size", cmd_rescue_fix_device_size, cmd_rescue_fix_device_size_usage, NULL, 0}, + { "undelete-subvol", cmd_rescue_undelete_subvol, + cmd_rescue_undelete_subvol_usage, NULL, 0}, NULL_CMD_STRUCT } };