From patchwork Tue Mar 27 07:06:54 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lu Fengqi X-Patchwork-Id: 10309385 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 A43CD60325 for ; Tue, 27 Mar 2018 07:07:31 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 87D8F29B4E for ; Tue, 27 Mar 2018 07:07:31 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 7CAEB29BBA; Tue, 27 Mar 2018 07:07:31 +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 1F54A29BB8 for ; Tue, 27 Mar 2018 07:07:31 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752121AbeC0HH0 (ORCPT ); Tue, 27 Mar 2018 03:07:26 -0400 Received: from mail.cn.fujitsu.com ([183.91.158.132]:35533 "EHLO heian.cn.fujitsu.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1752026AbeC0HHT (ORCPT ); Tue, 27 Mar 2018 03:07:19 -0400 X-IronPort-AV: E=Sophos;i="5.43,368,1503331200"; d="scan'208";a="38221617" Received: from localhost (HELO cn.fujitsu.com) ([10.167.33.5]) by heian.cn.fujitsu.com with ESMTP; 27 Mar 2018 15:07:08 +0800 Received: from G08CNEXCHPEKD01.g08.fujitsu.local (unknown [10.167.33.80]) by cn.fujitsu.com (Postfix) with ESMTP id C169F48AE76C for ; Tue, 27 Mar 2018 15:07:09 +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; Tue, 27 Mar 2018 15:07:12 +0800 From: Lu Fengqi To: Subject: [PATCH v2 06/10] btrfs-progs: undelete-subvol: introduce link_subvol_to_lostfound Date: Tue, 27 Mar 2018 15:06:54 +0800 Message-ID: <20180327070658.13064-7-lufq.fnst@cn.fujitsu.com> X-Mailer: git-send-email 2.16.2 In-Reply-To: <20180327070658.13064-1-lufq.fnst@cn.fujitsu.com> References: <20180327070658.13064-1-lufq.fnst@cn.fujitsu.com> MIME-Version: 1.0 X-Originating-IP: [10.167.226.155] X-yoursite-MailScanner-ID: C169F48AE76C.ADD8E 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 The function will create lost+found directory, link the deleted subvolume specified by the subvol_id to the directory, update the information of root_item and cleanup the associated orphan item. Signed-off-by: Lu Fengqi Reviewed-by: Qu Wenruo --- undelete-subvol.c | 76 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) diff --git a/undelete-subvol.c b/undelete-subvol.c index 781057df2b84..9243e35545c5 100644 --- a/undelete-subvol.c +++ b/undelete-subvol.c @@ -106,3 +106,79 @@ out: btrfs_release_path(&path); return ret; } + +/* + * Recover a subvolume specified by subvol_id, and link it to the lost+found + * directory. + * + * @root: the root of the root tree. + * @subvol_id: specify the subvolume which will be linked, and also be the part + * of the subvolume name. + * + * Return 0 if no error occurred. + */ +static int link_subvol_to_lostfound(struct btrfs_root *root, u64 subvol_id) +{ + struct btrfs_trans_handle *trans; + struct btrfs_fs_info *fs_info = root->fs_info; + struct btrfs_root *fs_root = fs_info->fs_root; + char buf[BTRFS_NAME_LEN + 1] = {0}; /* 1 for snprintf null */ + char *dir_name = "lost+found"; + u64 lost_found_ino = 0; + u32 mode = 0700; + int ret; + + /* + * For link subvolume to lost+found, + * 2 for parent(256)'s dir_index and dir_item + * 2 for lost+found dir's inode_item and inode_ref + * 2 for lost+found dir's dir_index and dir_item for the subvolume + * 2 for the subvolume's root_ref and root_backref + */ + trans = btrfs_start_transaction(fs_root, 8); + if (IS_ERR(trans)) { + error("unable to start transaction"); + ret = PTR_ERR(trans); + goto out; + } + + /* Create lost+found directory */ + ret = btrfs_mkdir(trans, fs_root, dir_name, strlen(dir_name), + BTRFS_FIRST_FREE_OBJECTID, &lost_found_ino, + mode); + if (ret < 0) { + error("failed to create '%s' dir: %d", dir_name, ret); + goto out; + } + + /* Link the subvolume to lost+found directory */ + snprintf(buf, BTRFS_NAME_LEN + 1, "sub%llu", subvol_id); + ret = btrfs_link_subvol(trans, fs_root, buf, subvol_id, lost_found_ino, + false); + if (ret) { + error("failed to link the subvol %llu: %d", subvol_id, ret); + goto out; + } + + /* Clear root flags BTRFS_ROOT_SUBVOL_DEAD and increase root refs */ + ret = recover_dead_root(trans, root, subvol_id); + if (ret) + goto out; + + /* Delete the orphan item after undeletion is completed. */ + ret = btrfs_del_orphan_item(trans, root, subvol_id); + if (ret) { + error("failed to delete the orphan_item for %llu: %d", + subvol_id, ret); + goto out; + } + + ret = btrfs_commit_transaction(trans, fs_root); + if (ret) { + error("transaction commit failed: %d", ret); + goto out; + } + +out: + return ret; +}