From patchwork Mon May 7 03:10:29 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lu Fengqi X-Patchwork-Id: 10383177 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 B9F4D6053A for ; Mon, 7 May 2018 03:11:12 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id AB85A28478 for ; Mon, 7 May 2018 03:11:12 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id A021428B21; Mon, 7 May 2018 03:11:12 +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 2F74828478 for ; Mon, 7 May 2018 03:11:12 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751996AbeEGDLK (ORCPT ); Sun, 6 May 2018 23:11:10 -0400 Received: from mail.cn.fujitsu.com ([183.91.158.132]:56801 "EHLO heian.cn.fujitsu.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1751903AbeEGDKs (ORCPT ); Sun, 6 May 2018 23:10:48 -0400 X-IronPort-AV: E=Sophos;i="5.43,368,1503331200"; d="scan'208";a="39622271" 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 18B884B34D23 for ; Mon, 7 May 2018 11:10:45 +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:46 +0800 From: Lu Fengqi To: Subject: [PATCH v3 06/10] btrfs-progs: undelete-subvol: introduce link_subvol_to_lostfound Date: Mon, 7 May 2018 11:10:29 +0800 Message-ID: <20180507031033.4125-7-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: 18B884B34D23.AB5CC 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 --- V3: pass btrfs_fs_info instead of btrfs_root; add Reviewed-by from Qu Wenruo. undelete-subvol.c | 76 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) diff --git a/undelete-subvol.c b/undelete-subvol.c index 6d3ef19977d6..9851998ec3df 100644 --- a/undelete-subvol.c +++ b/undelete-subvol.c @@ -79,3 +79,79 @@ out: btrfs_release_path(&path); return ret; } + +/* + * Recover a subvolume specified by subvol_id, and link it to the lost+found + * directory. + * + * @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_fs_info *fs_info, + u64 subvol_id) +{ + struct btrfs_trans_handle *trans; + struct btrfs_root *root = fs_info->tree_root; + 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 */ + ret = recover_dead_root(trans, 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; +}