From patchwork Fri Feb 2 08:27:49 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lu Fengqi X-Patchwork-Id: 10196383 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 BA36860362 for ; Fri, 2 Feb 2018 08:28:30 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id A7F0C28BF2 for ; Fri, 2 Feb 2018 08:28:30 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 9CE4F28E1C; Fri, 2 Feb 2018 08:28:30 +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=-6.9 required=2.0 tests=BAYES_00,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 2CE3628BF2 for ; Fri, 2 Feb 2018 08:28:30 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751924AbeBBI22 (ORCPT ); Fri, 2 Feb 2018 03:28:28 -0500 Received: from mail.cn.fujitsu.com ([183.91.158.132]:33627 "EHLO heian.cn.fujitsu.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1751795AbeBBI2K (ORCPT ); Fri, 2 Feb 2018 03:28:10 -0500 X-IronPort-AV: E=Sophos;i="5.43,368,1503331200"; d="scan'208";a="36672989" Received: from bogon (HELO cn.fujitsu.com) ([10.167.33.5]) by heian.cn.fujitsu.com with ESMTP; 02 Feb 2018 16:28:07 +0800 Received: from G08CNEXCHPEKD01.g08.fujitsu.local (unknown [10.167.33.80]) by cn.fujitsu.com (Postfix) with ESMTP id CA98949F19B7 for ; Fri, 2 Feb 2018 16:28:04 +0800 (CST) Received: from localhost.localdomain (10.167.226.155) by G08CNEXCHPEKD01.g08.fujitsu.local (10.167.33.89) with Microsoft SMTP Server (TLS) id 14.3.361.1; Fri, 2 Feb 2018 16:28:02 +0800 From: Lu Fengqi To: Subject: [PATCH 08/10] btrfs-progs: undelete-subvol: add undelete-subvol subcommand Date: Fri, 2 Feb 2018 16:27:49 +0800 Message-ID: <20180202082751.26225-9-lufq.fnst@cn.fujitsu.com> X-Mailer: git-send-email 2.16.1 In-Reply-To: <20180202082751.26225-1-lufq.fnst@cn.fujitsu.com> References: <20180202082751.26225-1-lufq.fnst@cn.fujitsu.com> MIME-Version: 1.0 X-Originating-IP: [10.167.226.155] X-yoursite-MailScanner-ID: CA98949F19B7.ABF0D 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 all deleted subvolume left intact on the device. Signed-off-by: Lu Fengqi --- cmds-rescue.c | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/cmds-rescue.c b/cmds-rescue.c index c40088ad374e..17bc1df33a23 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,50 @@ out: return !!ret; } +static const char * const cmd_rescue_undelete_subvol_usage[] = { + "btrfs rescue undelete-subvol ", + "Undelete all deleted subvolume that still left intact on the device", + "", + NULL +}; + +static int cmd_rescue_undelete_subvol(int argc, char **argv) +{ + struct btrfs_fs_info *fs_info; + char *devname; + int ret; + + clean_args_no_options(argc, argv, cmd_rescue_undelete_subvol_usage); + + if (check_argc_exact(argc, 2)) + 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 | + OPEN_CTREE_PARTIAL); + if (!fs_info) { + error("could not open btrfs"); + ret = -EIO; + goto out; + } + + ret = btrfs_undelete_intact_subvols(fs_info->tree_root); + + close_ctree(fs_info->tree_root); +out: + return ret; +} + static const char rescue_cmd_group_info[] = "toolbox for specific rescue operations"; @@ -260,6 +305,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 } };