From patchwork Tue Sep 11 11:29:00 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lu Fengqi X-Patchwork-Id: 10595521 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 874E815E2 for ; Tue, 11 Sep 2018 11:29:16 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 730AD28CFB for ; Tue, 11 Sep 2018 11:29:16 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 675A528F2F; Tue, 11 Sep 2018 11:29:16 +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 0B95728CFF for ; Tue, 11 Sep 2018 11:29:16 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726949AbeIKQ2J (ORCPT ); Tue, 11 Sep 2018 12:28:09 -0400 Received: from mail.cn.fujitsu.com ([183.91.158.132]:23627 "EHLO heian.cn.fujitsu.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1726713AbeIKQ2I (ORCPT ); Tue, 11 Sep 2018 12:28:08 -0400 X-IronPort-AV: E=Sophos;i="5.43,368,1503331200"; d="scan'208";a="44777954" Received: from unknown (HELO cn.fujitsu.com) ([10.167.33.5]) by heian.cn.fujitsu.com with ESMTP; 11 Sep 2018 19:29:09 +0800 Received: from G08CNEXCHPEKD01.g08.fujitsu.local (unknown [10.167.33.80]) by cn.fujitsu.com (Postfix) with ESMTP id 2AD4A4B6AE18 for ; Tue, 11 Sep 2018 19:29:11 +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:29:14 +0800 From: Lu Fengqi To: Subject: [RFC PATCH v2 1/4] btrfs: factor out btrfs_link_subvol from create_subvol Date: Tue, 11 Sep 2018 19:29:00 +0800 Message-ID: <20180911112903.25985-2-lufq.fnst@cn.fujitsu.com> X-Mailer: git-send-email 2.18.0 In-Reply-To: <20180911112903.25985-1-lufq.fnst@cn.fujitsu.com> References: <20180911112903.25985-1-lufq.fnst@cn.fujitsu.com> MIME-Version: 1.0 X-Originating-IP: [10.167.226.155] X-yoursite-MailScanner-ID: 2AD4A4B6AE18.ACA11 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 btrfs_link_subvol is responsible to link the subvolume to the specified directory, which is the opposite of what btrfs_unlink_subvol does. No functional change. Signed-off-by: Lu Fengqi --- fs/btrfs/ioctl.c | 64 +++++++++++++++++++++++++++++++----------------- 1 file changed, 41 insertions(+), 23 deletions(-) diff --git a/fs/btrfs/ioctl.c b/fs/btrfs/ioctl.c index 4905d13dee0a..1b03d07acde2 100644 --- a/fs/btrfs/ioctl.c +++ b/fs/btrfs/ioctl.c @@ -542,6 +542,45 @@ int btrfs_is_empty_uuid(u8 *uuid) return 1; } +static int btrfs_link_subvol(struct btrfs_trans_handle *trans, + struct inode *dir, u64 objectid, const char *name, + int namelen) +{ + struct btrfs_root *root = BTRFS_I(dir)->root; + struct btrfs_key key; + u64 index = 0; + int ret; + + /* + * insert the directory item + */ + ret = btrfs_set_inode_index(BTRFS_I(dir), &index); + if (ret) { + btrfs_abort_transaction(trans, ret); + return ret; + } + + key.objectid = objectid; + key.type = BTRFS_ROOT_ITEM_KEY; + key.offset = -1; + ret = btrfs_insert_dir_item(trans, name, namelen, BTRFS_I(dir), &key, + BTRFS_FT_DIR, index); + if (ret) { + btrfs_abort_transaction(trans, ret); + return ret; + } + + btrfs_i_size_write(BTRFS_I(dir), dir->i_size + namelen * 2); + ret = btrfs_update_inode(trans, root, dir); + BUG_ON(ret); + + ret = btrfs_add_root_ref(trans, objectid, root->root_key.objectid, + btrfs_ino(BTRFS_I(dir)), index, name, namelen); + BUG_ON(ret); + + return ret; +} + static noinline int create_subvol(struct inode *dir, struct dentry *dentry, const char *name, int namelen, @@ -563,7 +602,6 @@ static noinline int create_subvol(struct inode *dir, int err; u64 objectid; u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID; - u64 index = 0; uuid_le new_uuid; root_item = kzalloc(sizeof(*root_item), GFP_KERNEL); @@ -677,29 +715,9 @@ static noinline int create_subvol(struct inode *dir, new_root->highest_objectid = new_dirid; mutex_unlock(&new_root->objectid_mutex); - /* - * insert the directory item - */ - ret = btrfs_set_inode_index(BTRFS_I(dir), &index); - if (ret) { - btrfs_abort_transaction(trans, ret); - goto fail; - } - - ret = btrfs_insert_dir_item(trans, name, namelen, BTRFS_I(dir), &key, - BTRFS_FT_DIR, index); - if (ret) { - btrfs_abort_transaction(trans, ret); + ret = btrfs_link_subvol(trans, dir, objectid, name, namelen); + if (ret) goto fail; - } - - btrfs_i_size_write(BTRFS_I(dir), dir->i_size + namelen * 2); - ret = btrfs_update_inode(trans, root, dir); - BUG_ON(ret); - - ret = btrfs_add_root_ref(trans, objectid, root->root_key.objectid, - btrfs_ino(BTRFS_I(dir)), index, name, namelen); - BUG_ON(ret); ret = btrfs_uuid_tree_add(trans, root_item->uuid, BTRFS_UUID_KEY_SUBVOL, objectid); From patchwork Tue Sep 11 11:29:01 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lu Fengqi X-Patchwork-Id: 10595519 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 51B32920 for ; Tue, 11 Sep 2018 11:29:16 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 428EE28CFB for ; Tue, 11 Sep 2018 11:29:16 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 375B329024; Tue, 11 Sep 2018 11:29:16 +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 D4EA728CFB for ; Tue, 11 Sep 2018 11:29:15 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726852AbeIKQ2I (ORCPT ); Tue, 11 Sep 2018 12:28:08 -0400 Received: from mail.cn.fujitsu.com ([183.91.158.132]:51677 "EHLO heian.cn.fujitsu.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1726798AbeIKQ2I (ORCPT ); Tue, 11 Sep 2018 12:28:08 -0400 X-IronPort-AV: E=Sophos;i="5.43,368,1503331200"; d="scan'208";a="44777956" Received: from unknown (HELO cn.fujitsu.com) ([10.167.33.5]) by heian.cn.fujitsu.com with ESMTP; 11 Sep 2018 19:29:10 +0800 Received: from G08CNEXCHPEKD01.g08.fujitsu.local (unknown [10.167.33.80]) by cn.fujitsu.com (Postfix) with ESMTP id 9E11D4B6ADFF for ; Tue, 11 Sep 2018 19:29:11 +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:29:15 +0800 From: Lu Fengqi To: Subject: [RFC PATCH v2 2/4] btrfs: don't BUG_ON() in btrfs_link_subvol() Date: Tue, 11 Sep 2018 19:29:01 +0800 Message-ID: <20180911112903.25985-3-lufq.fnst@cn.fujitsu.com> X-Mailer: git-send-email 2.18.0 In-Reply-To: <20180911112903.25985-1-lufq.fnst@cn.fujitsu.com> References: <20180911112903.25985-1-lufq.fnst@cn.fujitsu.com> MIME-Version: 1.0 X-Originating-IP: [10.167.226.155] X-yoursite-MailScanner-ID: 9E11D4B6ADFF.AF15D 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 Both of btrfs_update_inode() and btrfs_add_root_ref() may fail because of ENOMEM. So there's no reason to panic here, we can replace BUG_ON() with btrfs_abort_transaction() here. Signed-off-by: Lu Fengqi Reviewed-by: Qu Wenruo --- fs/btrfs/ioctl.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/fs/btrfs/ioctl.c b/fs/btrfs/ioctl.c index 1b03d07acde2..f6173d4e7ced 100644 --- a/fs/btrfs/ioctl.c +++ b/fs/btrfs/ioctl.c @@ -572,11 +572,17 @@ static int btrfs_link_subvol(struct btrfs_trans_handle *trans, btrfs_i_size_write(BTRFS_I(dir), dir->i_size + namelen * 2); ret = btrfs_update_inode(trans, root, dir); - BUG_ON(ret); + if (ret) { + btrfs_abort_transaction(trans, ret); + return ret; + } ret = btrfs_add_root_ref(trans, objectid, root->root_key.objectid, btrfs_ino(BTRFS_I(dir)), index, name, namelen); - BUG_ON(ret); + if (ret) { + btrfs_abort_transaction(trans, ret); + return ret; + } return ret; } From patchwork Tue Sep 11 11:29:02 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lu Fengqi X-Patchwork-Id: 10595525 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 6A60B920 for ; Tue, 11 Sep 2018 11:29:19 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 5A1DF28CFB for ; Tue, 11 Sep 2018 11:29:19 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 4E74F28F2F; Tue, 11 Sep 2018 11:29:19 +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 DC9D228CFB for ; Tue, 11 Sep 2018 11:29:18 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727004AbeIKQ2K (ORCPT ); Tue, 11 Sep 2018 12:28:10 -0400 Received: from mail.cn.fujitsu.com ([183.91.158.132]:51677 "EHLO heian.cn.fujitsu.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1726798AbeIKQ2J (ORCPT ); Tue, 11 Sep 2018 12:28:09 -0400 X-IronPort-AV: E=Sophos;i="5.43,368,1503331200"; d="scan'208";a="44777958" Received: from unknown (HELO cn.fujitsu.com) ([10.167.33.5]) by heian.cn.fujitsu.com with ESMTP; 11 Sep 2018 19:29:12 +0800 Received: from G08CNEXCHPEKD01.g08.fujitsu.local (unknown [10.167.33.80]) by cn.fujitsu.com (Postfix) with ESMTP id 0F37A4B6ADFF for ; Tue, 11 Sep 2018 19:29:12 +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:29:15 +0800 From: Lu Fengqi To: Subject: [RFC PATCH v2 3/4] btrfs: undelete: introduce btrfs_undelete_subvolume Date: Tue, 11 Sep 2018 19:29:02 +0800 Message-ID: <20180911112903.25985-4-lufq.fnst@cn.fujitsu.com> X-Mailer: git-send-email 2.18.0 In-Reply-To: <20180911112903.25985-1-lufq.fnst@cn.fujitsu.com> References: <20180911112903.25985-1-lufq.fnst@cn.fujitsu.com> MIME-Version: 1.0 X-Originating-IP: [10.167.226.155] X-yoursite-MailScanner-ID: 0F37A4B6ADFF.AEC1F 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 do the following things which are almost the opposite of what btrfs_delete_subvolume() does: 1. link the subvolume to the parent specified; 2. clear root flag and set root_refs to 1; 3. add the subvol to the uuid_tree; 4. delete the orphan_item. Signed-off-by: Lu Fengqi --- fs/btrfs/ioctl.c | 113 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 113 insertions(+) diff --git a/fs/btrfs/ioctl.c b/fs/btrfs/ioctl.c index f6173d4e7ced..f088dea53c16 100644 --- a/fs/btrfs/ioctl.c +++ b/fs/btrfs/ioctl.c @@ -1880,6 +1880,119 @@ static noinline int btrfs_ioctl_snap_create_v2(struct file *file, return ret; } +static int btrfs_undelete_subvolume(struct btrfs_root *root, + struct dentry *parent, const char *name, + int namelen) +{ + struct inode *dir = d_inode(parent); + struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb); + struct btrfs_root_item *root_item = &root->root_item; + struct btrfs_trans_handle *trans; + struct btrfs_block_rsv block_rsv; + struct dentry *dentry; + struct inode *inode; + u64 root_flags; + int ret; + + btrfs_debug(fs_info, "Undelete subvolume %llu", + root->root_key.objectid); + + /* only care about the intact subvolume */ + if (btrfs_disk_key_objectid(&root_item->drop_progress) != 0) + return 0; + + ret = down_write_killable_nested(&dir->i_rwsem, I_MUTEX_PARENT); + if (ret == -EINTR) + return ret; + + dentry = lookup_one_len(name, parent, namelen); + if (IS_ERR(dentry)) { + ret = PTR_ERR(dentry); + goto out_unlock; + } + + down_write(&fs_info->subvol_sem); + + ret = btrfs_may_create(dir, dentry); + if (ret) + goto out_up_write; + + ret = btrfs_check_dir_item_collision(BTRFS_I(dir)->root, dir->i_ino, + name, namelen); + if (ret) + goto out_up_write; + + btrfs_init_block_rsv(&block_rsv, BTRFS_BLOCK_RSV_TEMP); + /* + * 1 - parent dir inode + * 2 - dir entries + * 2 - root ref/backref + * 1 - UUID item + */ + ret = btrfs_subvolume_reserve_metadata(root, &block_rsv, 6, false); + if (ret) + goto out_up_write; + + trans = btrfs_start_transaction(BTRFS_I(dir)->root, 0); + if (IS_ERR(trans)) { + ret = PTR_ERR(trans); + btrfs_subvolume_release_metadata(fs_info, &block_rsv); + goto out_up_write; + } + + trans->block_rsv = &block_rsv; + trans->bytes_reserved = block_rsv.size; + + ret = btrfs_link_subvol(trans, dir, root->root_key.objectid, name, + namelen); + if (ret) + goto fail; + + /* clear BTRFS_ROOT_SUBVOL_DEAD root flag and set root_refs to 1*/ + root_flags = btrfs_root_flags(root_item); + btrfs_set_root_flags(root_item, + root_flags & ~BTRFS_ROOT_SUBVOL_DEAD); + btrfs_set_root_refs(root_item, 1); + ret = btrfs_update_root(trans, fs_info->tree_root, + &root->root_key, &root->root_item); + if (ret) { + btrfs_abort_transaction(trans, ret); + goto fail; + } + + ret = btrfs_uuid_tree_add(trans, root_item->uuid, BTRFS_UUID_KEY_SUBVOL, + root->root_key.objectid); + if (ret) { + btrfs_abort_transaction(trans, ret); + goto fail; + } + + ret = btrfs_del_orphan_item(trans, fs_info->tree_root, + root->root_key.objectid); + if (ret && ret != -ENOENT) { + btrfs_abort_transaction(trans, ret); + goto fail; + } +fail: + trans->block_rsv = NULL; + trans->bytes_reserved = 0; + btrfs_subvolume_release_metadata(fs_info, &block_rsv); + ret = btrfs_commit_transaction(trans); + if (!ret) { + inode = btrfs_lookup_dentry(dir, dentry); + if (IS_ERR(inode)) + return PTR_ERR(inode); + d_instantiate(dentry, inode); + fsnotify_mkdir(dir, dentry); + } +out_up_write: + up_write(&fs_info->subvol_sem); + dput(dentry); +out_unlock: + inode_unlock(dir); + return ret; +} + static noinline int btrfs_ioctl_subvol_getflags(struct file *file, void __user *arg) { From patchwork Tue Sep 11 11:29:03 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lu Fengqi X-Patchwork-Id: 10595523 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 8D45514BD for ; Tue, 11 Sep 2018 11:29:18 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 7C47F28CFB for ; Tue, 11 Sep 2018 11:29:18 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 7092C28F2F; Tue, 11 Sep 2018 11:29:18 +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 1005828CFB for ; Tue, 11 Sep 2018 11:29:18 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727010AbeIKQ2K (ORCPT ); Tue, 11 Sep 2018 12:28:10 -0400 Received: from mail.cn.fujitsu.com ([183.91.158.132]:23627 "EHLO heian.cn.fujitsu.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1726881AbeIKQ2K (ORCPT ); Tue, 11 Sep 2018 12:28:10 -0400 X-IronPort-AV: E=Sophos;i="5.43,368,1503331200"; d="scan'208";a="44777959" Received: from unknown (HELO cn.fujitsu.com) ([10.167.33.5]) by heian.cn.fujitsu.com with ESMTP; 11 Sep 2018 19:29:12 +0800 Received: from G08CNEXCHPEKD01.g08.fujitsu.local (unknown [10.167.33.80]) by cn.fujitsu.com (Postfix) with ESMTP id 99AFA4B6AE13 for ; Tue, 11 Sep 2018 19:29:12 +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:29:16 +0800 From: Lu Fengqi To: Subject: [RFC PATCH v2 4/4] btrfs: undelete: Add BTRFS_IOCTL_SUBVOL_UNDELETE ioctl Date: Tue, 11 Sep 2018 19:29:03 +0800 Message-ID: <20180911112903.25985-5-lufq.fnst@cn.fujitsu.com> X-Mailer: git-send-email 2.18.0 In-Reply-To: <20180911112903.25985-1-lufq.fnst@cn.fujitsu.com> References: <20180911112903.25985-1-lufq.fnst@cn.fujitsu.com> MIME-Version: 1.0 X-Originating-IP: [10.167.226.155] X-yoursite-MailScanner-ID: 99AFA4B6AE13.AE3ED 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 This ioctl will provide user the ability to recover the subvolume of the given id to the given directory. Note: It will lock fs_info->cleaner_mutex to keep the cleaner kthread from deleting the subvolume which we want to recover. Signed-off-by: Lu Fengqi --- fs/btrfs/ioctl.c | 64 ++++++++++++++++++++++++++++++++++++++ include/uapi/linux/btrfs.h | 7 +++++ 2 files changed, 71 insertions(+) diff --git a/fs/btrfs/ioctl.c b/fs/btrfs/ioctl.c index f088dea53c16..3ddf6e1c117b 100644 --- a/fs/btrfs/ioctl.c +++ b/fs/btrfs/ioctl.c @@ -1993,6 +1993,68 @@ static int btrfs_undelete_subvolume(struct btrfs_root *root, return ret; } +static int btrfs_ioctl_undelete(struct file *file, void __user *argp) +{ + struct btrfs_ioctl_subvol_undelete_args *args; + struct inode *inode = file_inode(file); + struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); + struct btrfs_root *root; + int ret = 0; + + if (!S_ISDIR(inode->i_mode)) + return -ENOTDIR; + + args = memdup_user(argp, sizeof(*args)); + if (IS_ERR(args)) + return PTR_ERR(args); + + args->name[BTRFS_PATH_NAME_MAX] = '\0'; + + if (!capable(CAP_SYS_ADMIN)) { + ret = -EPERM; + goto free; + } + + ret = mnt_want_write_file(file); + if (ret) + goto free; + + ret = -ENOENT; + spin_lock(&fs_info->trans_lock); + list_for_each_entry(root, &fs_info->dead_roots, root_list) { + if (root->root_key.objectid == args->subvol_id) { + list_del_init(&root->root_list); + ret = 0; + break; + } + } + spin_unlock(&fs_info->trans_lock); + if (ret) + goto drop_write; + + /* + * Lock cleaner_mutex to prevent the cleaner kthread from deleting the + * subvolume we want to recover so that we can perform the next rescue + * in a relaxed manner. + */ + mutex_lock(&fs_info->cleaner_mutex); + + ret = btrfs_undelete_subvolume(root, file->f_path.dentry, args->name, + strlen(args->name)); + if (ret) { + btrfs_add_dead_root(root); + goto unlock; + } + +unlock: + mutex_unlock(&fs_info->cleaner_mutex); +drop_write: + mnt_drop_write_file(file); +free: + kfree(args); + return ret; +} + static noinline int btrfs_ioctl_subvol_getflags(struct file *file, void __user *arg) { @@ -6118,6 +6180,8 @@ long btrfs_ioctl(struct file *file, unsigned int return btrfs_ioctl_get_subvol_rootref(file, argp); case BTRFS_IOC_INO_LOOKUP_USER: return btrfs_ioctl_ino_lookup_user(file, argp); + case BTRFS_IOC_SUBVOL_UNDELETE: + return btrfs_ioctl_undelete(file, argp); } return -ENOTTY; diff --git a/include/uapi/linux/btrfs.h b/include/uapi/linux/btrfs.h index 5ca1d21fc4a7..e6d3c8e24bb8 100644 --- a/include/uapi/linux/btrfs.h +++ b/include/uapi/linux/btrfs.h @@ -816,6 +816,11 @@ struct btrfs_ioctl_get_subvol_rootref_args { __u8 align[7]; }; +struct btrfs_ioctl_subvol_undelete_args { + __u64 subvol_id; + char name[BTRFS_PATH_NAME_MAX + 1]; +}; + /* Error codes as returned by the kernel */ enum btrfs_err_code { BTRFS_ERROR_DEV_RAID1_MIN_NOT_MET = 1, @@ -940,5 +945,7 @@ enum btrfs_err_code { struct btrfs_ioctl_get_subvol_rootref_args) #define BTRFS_IOC_INO_LOOKUP_USER _IOWR(BTRFS_IOCTL_MAGIC, 62, \ struct btrfs_ioctl_ino_lookup_user_args) +#define BTRFS_IOC_SUBVOL_UNDELETE _IOWR(BTRFS_IOCTL_MAGIC, 63, \ + struct btrfs_ioctl_subvol_undelete_args) #endif /* _UAPI_LINUX_BTRFS_H */