From patchwork Tue Jul 2 09:04:11 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Qu Wenruo X-Patchwork-Id: 13719144 Received: from smtp-out1.suse.de (smtp-out1.suse.de [195.135.223.130]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id A4F29148300 for ; Tue, 2 Jul 2024 09:04:34 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=195.135.223.130 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1719911076; cv=none; b=TNUt48B3Rp3VkoOxTPlVnGPqfU9wBq/ef1c68ntw/OhKL2DYNWrfSYl6RbneP9pHZlyAYhA08JrNIl0o08zMXhZN+Rz9TbGF23JF0F8N14eI9RCdelkomp7SCKI7Knzf2JHpEkMb/z2CaNa7jFcJZ3jCopP5bi/aNSGmbUItN1k= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1719911076; c=relaxed/simple; bh=Xt+5vsUGB7qqkoS9oP/wmmQ3mTBd9ta5fG2frFtaAAU=; h=From:To:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=SZ6IB+ZQURR7KolPKjMjckfAefDsMJo2vUL+oaspePNvxLSQmlesfYvuK9HG6se+OFg0fMNraQCnbGEpo+h0ea4/dlLc9Ndv5lMxVNT5Lm64fPnDdMDf1v8h+LP+uGMdZgEZCuT/BrXxCa+d/VYLyXAQALUSc2NJSooJVdy5iSg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=suse.com; spf=pass smtp.mailfrom=suse.com; dkim=pass (1024-bit key) header.d=suse.com header.i=@suse.com header.b=vGE+zKL6; dkim=pass (1024-bit key) header.d=suse.com header.i=@suse.com header.b=vGE+zKL6; arc=none smtp.client-ip=195.135.223.130 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=suse.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=suse.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=suse.com header.i=@suse.com header.b="vGE+zKL6"; dkim=pass (1024-bit key) header.d=suse.com header.i=@suse.com header.b="vGE+zKL6" Received: from imap1.dmz-prg2.suse.org (unknown [10.150.64.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by smtp-out1.suse.de (Postfix) with ESMTPS id D873C21B1A for ; Tue, 2 Jul 2024 09:04:32 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=susede1; t=1719911072; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=TDGiqqcCtFhY93gfc3sAm8m8hpDyi7hXqY6AgZgscvQ=; b=vGE+zKL6ReSsBgUpJ5tg0+XJN6c7bOG3vDZflZq3P77lVvJ3HIuK4Y/bMvIGlJHewpIfLY dOmZ3vNGG2J2wh70++C6YvJV/Dqsyo9OcM8gyeQ9DK3vdeLddKCFHLQQqWauCQEwWyOxp/ rEfeiMH8WxPUIX7ue4w2VGhLXOKVAIo= Authentication-Results: smtp-out1.suse.de; none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=susede1; t=1719911072; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=TDGiqqcCtFhY93gfc3sAm8m8hpDyi7hXqY6AgZgscvQ=; b=vGE+zKL6ReSsBgUpJ5tg0+XJN6c7bOG3vDZflZq3P77lVvJ3HIuK4Y/bMvIGlJHewpIfLY dOmZ3vNGG2J2wh70++C6YvJV/Dqsyo9OcM8gyeQ9DK3vdeLddKCFHLQQqWauCQEwWyOxp/ rEfeiMH8WxPUIX7ue4w2VGhLXOKVAIo= Received: from imap1.dmz-prg2.suse.org (localhost [127.0.0.1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by imap1.dmz-prg2.suse.org (Postfix) with ESMTPS id 05D3F1395F for ; Tue, 2 Jul 2024 09:04:31 +0000 (UTC) Received: from dovecot-director2.suse.de ([2a07:de40:b281:106:10:150:64:167]) by imap1.dmz-prg2.suse.org with ESMTPSA id SO9aLJ/Cg2Z0JQAAD6G6ig (envelope-from ) for ; Tue, 02 Jul 2024 09:04:31 +0000 From: Qu Wenruo To: linux-btrfs@vger.kernel.org Subject: [PATCH 1/2] btrfs-progs: remove fs_info parameter from btrfs_create_tree() Date: Tue, 2 Jul 2024 18:34:11 +0930 Message-ID: <628405c254a709cf09e7899fae47626641959ddd.1719910680.git.wqu@suse.com> X-Mailer: git-send-email 2.45.2 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-btrfs@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Spam-Score: -2.80 X-Spam-Level: X-Spam-Flag: NO X-Spamd-Result: default: False [-2.80 / 50.00]; BAYES_HAM(-3.00)[100.00%]; NEURAL_HAM_LONG(-1.00)[-1.000]; MID_CONTAINS_FROM(1.00)[]; R_MISSING_CHARSET(0.50)[]; NEURAL_HAM_SHORT(-0.20)[-1.000]; MIME_GOOD(-0.10)[text/plain]; FUZZY_BLOCKED(0.00)[rspamd.com]; RCVD_VIA_SMTP_AUTH(0.00)[]; RCPT_COUNT_ONE(0.00)[1]; ARC_NA(0.00)[]; DKIM_SIGNED(0.00)[suse.com:s=susede1]; DBL_BLOCKED_OPENRESOLVER(0.00)[imap1.dmz-prg2.suse.org:helo,suse.com:email]; FROM_EQ_ENVFROM(0.00)[]; FROM_HAS_DN(0.00)[]; MIME_TRACE(0.00)[0:+]; RCVD_COUNT_TWO(0.00)[2]; TO_MATCH_ENVRCPT_ALL(0.00)[]; TO_DN_NONE(0.00)[]; PREVIOUSLY_DELIVERED(0.00)[linux-btrfs@vger.kernel.org]; RCVD_TLS_ALL(0.00)[] The @fs_info parameter can be easily extracted from @trans, and kernel has already remove the parameter. Signed-off-by: Qu Wenruo --- kernel-shared/disk-io.c | 4 ++-- kernel-shared/disk-io.h | 1 - kernel-shared/free-space-tree.c | 2 +- mkfs/main.c | 8 ++++---- 4 files changed, 7 insertions(+), 8 deletions(-) diff --git a/kernel-shared/disk-io.c b/kernel-shared/disk-io.c index 295bd50ad063..3e8cab1187f0 100644 --- a/kernel-shared/disk-io.c +++ b/kernel-shared/disk-io.c @@ -2369,12 +2369,12 @@ int btrfs_delete_and_free_root(struct btrfs_trans_handle *trans, } struct btrfs_root *btrfs_create_tree(struct btrfs_trans_handle *trans, - struct btrfs_fs_info *fs_info, struct btrfs_key *key) { - struct extent_buffer *leaf; + struct btrfs_fs_info *fs_info = trans->fs_info; struct btrfs_root *tree_root = fs_info->tree_root; struct btrfs_root *root; + struct extent_buffer *leaf; int ret = 0; root = kzalloc(sizeof(*root), GFP_KERNEL); diff --git a/kernel-shared/disk-io.h b/kernel-shared/disk-io.h index 9f848635fd69..0047db5e7c3e 100644 --- a/kernel-shared/disk-io.h +++ b/kernel-shared/disk-io.h @@ -239,7 +239,6 @@ int write_tree_block(struct btrfs_trans_handle *trans, struct extent_buffer *eb); int btrfs_fs_roots_compare_roots(const struct rb_node *node1, const struct rb_node *node2); struct btrfs_root *btrfs_create_tree(struct btrfs_trans_handle *trans, - struct btrfs_fs_info *fs_info, struct btrfs_key *key); int btrfs_delete_and_free_root(struct btrfs_trans_handle *trans, struct btrfs_root *root); diff --git a/kernel-shared/free-space-tree.c b/kernel-shared/free-space-tree.c index 93806ca01162..81fd57b886d2 100644 --- a/kernel-shared/free-space-tree.c +++ b/kernel-shared/free-space-tree.c @@ -1516,7 +1516,7 @@ int btrfs_create_free_space_tree(struct btrfs_fs_info *fs_info) if (IS_ERR(trans)) return PTR_ERR(trans); - free_space_root = btrfs_create_tree(trans, fs_info, &root_key); + free_space_root = btrfs_create_tree(trans, &root_key); if (IS_ERR(free_space_root)) { ret = PTR_ERR(free_space_root); goto abort; diff --git a/mkfs/main.c b/mkfs/main.c index b40f7432bb74..077da1e33fc6 100644 --- a/mkfs/main.c +++ b/mkfs/main.c @@ -748,7 +748,7 @@ static int create_data_reloc_tree(struct btrfs_trans_handle *trans) char *name = ".."; int ret; - root = btrfs_create_tree(trans, fs_info, &key); + root = btrfs_create_tree(trans, &key); if (IS_ERR(root)) { ret = PTR_ERR(root); goto out; @@ -870,7 +870,7 @@ static int create_uuid_tree(struct btrfs_trans_handle *trans) int ret = 0; UASSERT(fs_info->uuid_root == NULL); - root = btrfs_create_tree(trans, fs_info, &key); + root = btrfs_create_tree(trans, &key); if (IS_ERR(root)) { ret = PTR_ERR(root); goto out; @@ -900,7 +900,7 @@ static int create_global_root(struct btrfs_trans_handle *trans, u64 objectid, }; int ret = 0; - root = btrfs_create_tree(trans, fs_info, &key); + root = btrfs_create_tree(trans, &key); if (IS_ERR(root)) { ret = PTR_ERR(root); goto out; @@ -1127,7 +1127,7 @@ static int setup_raid_stripe_tree_root(struct btrfs_fs_info *fs_info) return ret; } - stripe_root = btrfs_create_tree(trans, fs_info, &key); + stripe_root = btrfs_create_tree(trans, &key); if (IS_ERR(stripe_root)) { ret = PTR_ERR(stripe_root); btrfs_abort_transaction(trans, ret); From patchwork Tue Jul 2 09:04:12 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Qu Wenruo X-Patchwork-Id: 13719145 Received: from smtp-out1.suse.de (smtp-out1.suse.de [195.135.223.130]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 448811514DA for ; Tue, 2 Jul 2024 09:04:36 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=195.135.223.130 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1719911078; cv=none; b=V9Zjvb4GHn4pdgxPioqk6VPuzA0KSpCowvq9nch/PeEFBJ9qmEFE5lXKM1SzdEnsrnZT2c9scNcRiC6b1Wp4n3VBgXtNV+W61o7TQXpyrFqpAfFcvI58h5XDAVH5DfGMLy4mHYbdMjdY9ReUbkTzPtRqw241Ac4dwBO9lnXJN0U= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1719911078; c=relaxed/simple; bh=mPLQY+V0CVgNaP1oRHMlDHK6+MBOwCU7aQw8xaTmKSk=; h=From:To:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=qEbF1NicGK3dSGTmMVz9eBykqg9O1WtOSo7iyPNk6eAR+od0r9sKN8E/NWwczIZIxwN15WJ+WsZFI07b7+O1HcXbv2JBt65SuIEd7ELH4UuVv4L0PE9577ch2by2adfhy2qh42o3AocNG/wY90paQf+q0Ji32zgSgtMfabzzIX8= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=suse.com; spf=pass smtp.mailfrom=suse.com; dkim=pass (1024-bit key) header.d=suse.com header.i=@suse.com header.b=fFCHjgMd; dkim=pass (1024-bit key) header.d=suse.com header.i=@suse.com header.b=fFCHjgMd; arc=none smtp.client-ip=195.135.223.130 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=suse.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=suse.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=suse.com header.i=@suse.com header.b="fFCHjgMd"; dkim=pass (1024-bit key) header.d=suse.com header.i=@suse.com header.b="fFCHjgMd" Received: from imap1.dmz-prg2.suse.org (imap1.dmz-prg2.suse.org [IPv6:2a07:de40:b281:104:10:150:64:97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by smtp-out1.suse.de (Postfix) with ESMTPS id 4899521B1D for ; Tue, 2 Jul 2024 09:04:34 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=susede1; t=1719911074; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=B68CtDl4kJyF7GuCxnbtX+7HQrvX82QuHRLEMox2Q4w=; b=fFCHjgMd80rkHAmoGtA5Nq3/kUuS/aXEqy+nAbQFU+m5hxHMluIldb5F/zfQkAs9qyE2BA fz5BKsWegaz3249TcoWUw63rkzltpbdgo2w5lgMe9NA1bhNwIBy7Ap1BPoxGKh9XbV9WZ8 qh5o8HQc3P1ZfZex1W1pmFh7BR/4M/c= Authentication-Results: smtp-out1.suse.de; dkim=pass header.d=suse.com header.s=susede1 header.b=fFCHjgMd DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=susede1; t=1719911074; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=B68CtDl4kJyF7GuCxnbtX+7HQrvX82QuHRLEMox2Q4w=; b=fFCHjgMd80rkHAmoGtA5Nq3/kUuS/aXEqy+nAbQFU+m5hxHMluIldb5F/zfQkAs9qyE2BA fz5BKsWegaz3249TcoWUw63rkzltpbdgo2w5lgMe9NA1bhNwIBy7Ap1BPoxGKh9XbV9WZ8 qh5o8HQc3P1ZfZex1W1pmFh7BR/4M/c= Received: from imap1.dmz-prg2.suse.org (localhost [127.0.0.1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by imap1.dmz-prg2.suse.org (Postfix) with ESMTPS id 680DA1395F for ; Tue, 2 Jul 2024 09:04:33 +0000 (UTC) Received: from dovecot-director2.suse.de ([2a07:de40:b281:106:10:150:64:167]) by imap1.dmz-prg2.suse.org with ESMTPSA id mKC5CKHCg2Z0JQAAD6G6ig (envelope-from ) for ; Tue, 02 Jul 2024 09:04:33 +0000 From: Qu Wenruo To: linux-btrfs@vger.kernel.org Subject: [PATCH 2/2] btrfs-progs: introduce btrfs_make_subvolume() Date: Tue, 2 Jul 2024 18:34:12 +0930 Message-ID: <380a5cb60c8417938248bfb1a2549eeabc9a6d24.1719910680.git.wqu@suse.com> X-Mailer: git-send-email 2.45.2 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-btrfs@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Rspamd-Queue-Id: 4899521B1D X-Spam-Score: -4.01 X-Spam-Level: X-Spam-Flag: NO X-Spamd-Result: default: False [-4.01 / 50.00]; BAYES_HAM(-3.00)[100.00%]; NEURAL_HAM_LONG(-1.00)[-1.000]; DWL_DNSWL_LOW(-1.00)[suse.com:dkim]; MID_CONTAINS_FROM(1.00)[]; R_MISSING_CHARSET(0.50)[]; NEURAL_HAM_SHORT(-0.20)[-1.000]; R_DKIM_ALLOW(-0.20)[suse.com:s=susede1]; MIME_GOOD(-0.10)[text/plain]; MX_GOOD(-0.01)[]; ARC_NA(0.00)[]; RCVD_VIA_SMTP_AUTH(0.00)[]; TO_DN_NONE(0.00)[]; RCPT_COUNT_ONE(0.00)[1]; FROM_HAS_DN(0.00)[]; MIME_TRACE(0.00)[0:+]; FROM_EQ_ENVFROM(0.00)[]; FUZZY_BLOCKED(0.00)[rspamd.com]; RCVD_COUNT_TWO(0.00)[2]; PREVIOUSLY_DELIVERED(0.00)[linux-btrfs@vger.kernel.org]; TO_MATCH_ENVRCPT_ALL(0.00)[]; RCVD_TLS_ALL(0.00)[]; DKIM_SIGNED(0.00)[suse.com:s=susede1]; DBL_BLOCKED_OPENRESOLVER(0.00)[imap1.dmz-prg2.suse.org:helo,imap1.dmz-prg2.suse.org:rdns,suse.com:email,suse.com:dkim]; SPAMHAUS_XBL(0.00)[2a07:de40:b281:104:10:150:64:97:from]; DKIM_TRACE(0.00)[suse.com:+] X-Rspamd-Action: no action X-Rspamd-Server: rspamd1.dmz-prg2.suse.org There are two different subvolume/data reloc tree creation routines: - create_subvol() from convert/main.c * calls btrfs_copy_root() to create an empty root This is not safe, as it relies on the source root to be empty. * calls btrfs_read_fs_root() to add it to the cache and trace it properly * calls btrfs_make_root_dir() to initialize the empty new root - create_data_reloc_tree() from mkfs/main.c * calls btrfs_create_tree() to create an empty rooo * Manually add the root to fs_root cache This is only safe for data reloc tree as it's never updated inside btrfs-progs. But not safe for other subvolume trees. * manually setup the root dir Both has its good and bad aspects, so here we introduce a new helper, btrfs_make_subvolume(): - Calls btrfs_create_tree() to create an empty root - Calls btrfs_read_fs_root() to setup the cache and tracking properly - Calls btrfs_make_root_dir() to initialize the root dir - Calls btrfs_update_root() to reflect the rootdir change So this new helper can replace both create_subvol() and create_data_reloc_tree(). Signed-off-by: Qu Wenruo --- Makefile | 1 + check/main.c | 1 + common/root-tree-utils.c | 108 +++++++++++++++++++++++++++++++++++++++ common/root-tree-utils.h | 26 ++++++++++ convert/main.c | 43 ++-------------- mkfs/common.c | 39 -------------- mkfs/common.h | 2 - mkfs/main.c | 72 +------------------------- 8 files changed, 141 insertions(+), 151 deletions(-) create mode 100644 common/root-tree-utils.c create mode 100644 common/root-tree-utils.h diff --git a/Makefile b/Makefile index 82dfb1b40cde..c0428c573933 100644 --- a/Makefile +++ b/Makefile @@ -221,6 +221,7 @@ objects = \ common/device-utils.o \ common/extent-cache.o \ common/extent-tree-utils.o \ + common/root-tree-utils.o \ common/filesystem-utils.o \ common/format-output.o \ common/fsfeatures.o \ diff --git a/check/main.c b/check/main.c index 693f77c3542d..8a572c22036e 100644 --- a/check/main.c +++ b/check/main.c @@ -59,6 +59,7 @@ #include "common/open-utils.h" #include "common/string-utils.h" #include "common/clear-cache.h" +#include "common/root-tree-utils.h" #include "cmds/commands.h" #include "mkfs/common.h" #include "check/common.h" diff --git a/common/root-tree-utils.c b/common/root-tree-utils.c new file mode 100644 index 000000000000..a937037ea4fd --- /dev/null +++ b/common/root-tree-utils.c @@ -0,0 +1,108 @@ +/* + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public + * License v2 as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public + * License along with this program; if not, write to the + * Free Software Foundation, Inc., 59 Temple Place - Suite 330, + * Boston, MA 021110-1307, USA. + */ + +#include +#include "common/root-tree-utils.h" +#include "common/rbtree-utils.h" +#include "common/messages.h" +#include "kernel-shared/disk-io.h" + +int btrfs_make_root_dir(struct btrfs_trans_handle *trans, + struct btrfs_root *root, u64 objectid) +{ + int ret; + struct btrfs_inode_item inode_item; + time_t now = time(NULL); + + memset(&inode_item, 0, sizeof(inode_item)); + btrfs_set_stack_inode_generation(&inode_item, trans->transid); + btrfs_set_stack_inode_size(&inode_item, 0); + btrfs_set_stack_inode_nlink(&inode_item, 1); + btrfs_set_stack_inode_nbytes(&inode_item, root->fs_info->nodesize); + btrfs_set_stack_inode_mode(&inode_item, S_IFDIR | 0755); + btrfs_set_stack_timespec_sec(&inode_item.atime, now); + btrfs_set_stack_timespec_nsec(&inode_item.atime, 0); + btrfs_set_stack_timespec_sec(&inode_item.ctime, now); + btrfs_set_stack_timespec_nsec(&inode_item.ctime, 0); + btrfs_set_stack_timespec_sec(&inode_item.mtime, now); + btrfs_set_stack_timespec_nsec(&inode_item.mtime, 0); + btrfs_set_stack_timespec_sec(&inode_item.otime, now); + btrfs_set_stack_timespec_nsec(&inode_item.otime, 0); + + if (root->fs_info->tree_root == root) + btrfs_set_super_root_dir(root->fs_info->super_copy, objectid); + + ret = btrfs_insert_inode(trans, root, objectid, &inode_item); + if (ret) + goto error; + + ret = btrfs_insert_inode_ref(trans, root, "..", 2, objectid, objectid, 0); + if (ret) + goto error; + + btrfs_set_root_dirid(&root->root_item, objectid); + ret = 0; +error: + return ret; +} + +/* + * Create a subvolume and initialize its content with the top inode. + * + * The created tree root would have its root_ref as 1. + * Thus for subvolumes caller needs to properly add ROOT_BACKREF items. + */ +int btrfs_make_subvolume(struct btrfs_trans_handle *trans, u64 objectid) +{ + struct btrfs_fs_info *fs_info = trans->fs_info; + struct btrfs_root *root; + struct btrfs_key key = { + .objectid = objectid, + .type = BTRFS_ROOT_ITEM_KEY, + }; + int ret; + + /* FSTREE is different and can not be created by this function. */ + UASSERT(objectid != BTRFS_FS_TREE_OBJECTID); + UASSERT(is_fstree(objectid) || objectid == BTRFS_DATA_RELOC_TREE_OBJECTID); + + root = btrfs_create_tree(trans, &key); + if (IS_ERR(root)) { + ret = PTR_ERR(root); + goto error; + } + /* + * Free it for now, and re-read it from disk to setup cache and + * tracking. + */ + btrfs_free_fs_root(root); + root = btrfs_read_fs_root(fs_info, &key); + if (IS_ERR(root)) { + ret = PTR_ERR(root); + goto error; + } + ret = btrfs_make_root_dir(trans, root, BTRFS_FIRST_FREE_OBJECTID); + if (ret < 0) + goto error; + ret = btrfs_update_root(trans, fs_info->tree_root, &root->root_key, + &root->root_item); + if (ret < 0) + goto error; + return 0; +error: + btrfs_abort_transaction(trans, ret); + return ret; +} diff --git a/common/root-tree-utils.h b/common/root-tree-utils.h new file mode 100644 index 000000000000..f4e354c0bf3e --- /dev/null +++ b/common/root-tree-utils.h @@ -0,0 +1,26 @@ +/* + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public + * License v2 as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public + * License along with this program; if not, write to the + * Free Software Foundation, Inc., 59 Temple Place - Suite 330, + * Boston, MA 021110-1307, USA. + */ + +#ifndef _ROOT_TREE_UTILS_H_ +#define _ROOT_TREE_UTILS_H_ + +#include "kernel-shared/transaction.h" + +int btrfs_make_root_dir(struct btrfs_trans_handle *trans, + struct btrfs_root *root, u64 objectid); +int btrfs_make_subvolume(struct btrfs_trans_handle *trans, u64 objectid); + +#endif diff --git a/convert/main.c b/convert/main.c index 8e73aa25b1da..31fa146c86b1 100644 --- a/convert/main.c +++ b/convert/main.c @@ -120,6 +120,7 @@ #include "common/box.h" #include "common/open-utils.h" #include "common/extent-tree-utils.h" +#include "common/root-tree-utils.h" #include "common/clear-cache.h" #include "cmds/commands.h" #include "check/repair.h" @@ -916,44 +917,6 @@ out: return ret; } -static int create_subvol(struct btrfs_trans_handle *trans, - struct btrfs_root *root, u64 root_objectid) -{ - struct extent_buffer *tmp; - struct btrfs_root *new_root; - struct btrfs_key key; - struct btrfs_root_item root_item; - int ret; - - ret = btrfs_copy_root(trans, root, root->node, &tmp, - root_objectid); - if (ret) - return ret; - - memcpy(&root_item, &root->root_item, sizeof(root_item)); - btrfs_set_root_bytenr(&root_item, tmp->start); - btrfs_set_root_level(&root_item, btrfs_header_level(tmp)); - btrfs_set_root_generation(&root_item, trans->transid); - free_extent_buffer(tmp); - - key.objectid = root_objectid; - key.type = BTRFS_ROOT_ITEM_KEY; - key.offset = trans->transid; - ret = btrfs_insert_root(trans, root->fs_info->tree_root, - &key, &root_item); - - key.offset = (u64)-1; - new_root = btrfs_read_fs_root(root->fs_info, &key); - if (!new_root || IS_ERR(new_root)) { - error("unable to fs read root: %lu", PTR_ERR(new_root)); - return PTR_ERR(new_root); - } - - ret = btrfs_make_root_dir(trans, new_root, BTRFS_FIRST_FREE_OBJECTID); - - return ret; -} - /* * New make_btrfs() has handle system and meta chunks quite well. * So only need to add remaining data chunks. @@ -1059,13 +1022,13 @@ static int init_btrfs(struct btrfs_mkfs_config *cfg, struct btrfs_root *root, BTRFS_FIRST_FREE_OBJECTID); /* subvol for fs image file */ - ret = create_subvol(trans, root, CONV_IMAGE_SUBVOL_OBJECTID); + ret = btrfs_make_subvolume(trans, CONV_IMAGE_SUBVOL_OBJECTID); if (ret < 0) { error("failed to create subvolume image root: %d", ret); goto err; } /* subvol for data relocation tree */ - ret = create_subvol(trans, root, BTRFS_DATA_RELOC_TREE_OBJECTID); + ret = btrfs_make_subvolume(trans, BTRFS_DATA_RELOC_TREE_OBJECTID); if (ret < 0) { error("failed to create DATA_RELOC root: %d", ret); goto err; diff --git a/mkfs/common.c b/mkfs/common.c index 5de3e0b6d59c..e28b1e827ee7 100644 --- a/mkfs/common.c +++ b/mkfs/common.c @@ -758,45 +758,6 @@ out: return ret; } -int btrfs_make_root_dir(struct btrfs_trans_handle *trans, - struct btrfs_root *root, u64 objectid) -{ - int ret; - struct btrfs_inode_item inode_item; - time_t now = time(NULL); - - memset(&inode_item, 0, sizeof(inode_item)); - btrfs_set_stack_inode_generation(&inode_item, trans->transid); - btrfs_set_stack_inode_size(&inode_item, 0); - btrfs_set_stack_inode_nlink(&inode_item, 1); - btrfs_set_stack_inode_nbytes(&inode_item, root->fs_info->nodesize); - btrfs_set_stack_inode_mode(&inode_item, S_IFDIR | 0755); - btrfs_set_stack_timespec_sec(&inode_item.atime, now); - btrfs_set_stack_timespec_nsec(&inode_item.atime, 0); - btrfs_set_stack_timespec_sec(&inode_item.ctime, now); - btrfs_set_stack_timespec_nsec(&inode_item.ctime, 0); - btrfs_set_stack_timespec_sec(&inode_item.mtime, now); - btrfs_set_stack_timespec_nsec(&inode_item.mtime, 0); - btrfs_set_stack_timespec_sec(&inode_item.otime, now); - btrfs_set_stack_timespec_nsec(&inode_item.otime, 0); - - if (root->fs_info->tree_root == root) - btrfs_set_super_root_dir(root->fs_info->super_copy, objectid); - - ret = btrfs_insert_inode(trans, root, objectid, &inode_item); - if (ret) - goto error; - - ret = btrfs_insert_inode_ref(trans, root, "..", 2, objectid, objectid, 0); - if (ret) - goto error; - - btrfs_set_root_dirid(&root->root_item, objectid); - ret = 0; -error: - return ret; -} - /* * Btrfs minimum size calculation is complicated, it should include at least: * 1. system group size diff --git a/mkfs/common.h b/mkfs/common.h index de0ff57beee8..c600c16622fa 100644 --- a/mkfs/common.h +++ b/mkfs/common.h @@ -103,8 +103,6 @@ struct btrfs_mkfs_config { }; int make_btrfs(int fd, struct btrfs_mkfs_config *cfg); -int btrfs_make_root_dir(struct btrfs_trans_handle *trans, - struct btrfs_root *root, u64 objectid); u64 btrfs_min_dev_size(u32 nodesize, bool mixed, u64 zone_size, u64 meta_profile, u64 data_profile); int test_minimum_size(const char *file, u64 min_dev_size); diff --git a/mkfs/main.c b/mkfs/main.c index 077da1e33fc6..cf5cae45d02a 100644 --- a/mkfs/main.c +++ b/mkfs/main.c @@ -58,6 +58,7 @@ #include "common/units.h" #include "common/string-utils.h" #include "common/string-table.h" +#include "common/root-tree-utils.h" #include "cmds/commands.h" #include "check/qgroup-verify.h" #include "mkfs/common.h" @@ -734,75 +735,6 @@ static void update_chunk_allocation(struct btrfs_fs_info *fs_info, } } -static int create_data_reloc_tree(struct btrfs_trans_handle *trans) -{ - struct btrfs_fs_info *fs_info = trans->fs_info; - struct btrfs_inode_item *inode; - struct btrfs_root *root; - struct btrfs_path path = { 0 }; - struct btrfs_key key = { - .objectid = BTRFS_DATA_RELOC_TREE_OBJECTID, - .type = BTRFS_ROOT_ITEM_KEY, - }; - u64 ino = BTRFS_FIRST_FREE_OBJECTID; - char *name = ".."; - int ret; - - root = btrfs_create_tree(trans, &key); - if (IS_ERR(root)) { - ret = PTR_ERR(root); - goto out; - } - /* Update dirid as created tree has default dirid 0 */ - btrfs_set_root_dirid(&root->root_item, ino); - ret = btrfs_update_root(trans, fs_info->tree_root, &root->root_key, - &root->root_item); - if (ret < 0) - goto out; - - /* Cache this tree so it can be cleaned up at close_ctree() */ - ret = rb_insert(&fs_info->fs_root_tree, &root->rb_node, - btrfs_fs_roots_compare_roots); - if (ret < 0) - goto out; - - /* Insert INODE_ITEM */ - ret = btrfs_new_inode(trans, root, ino, 0755 | S_IFDIR); - if (ret < 0) - goto out; - - /* then INODE_REF */ - ret = btrfs_insert_inode_ref(trans, root, name, strlen(name), ino, ino, - 0); - if (ret < 0) - goto out; - - /* Update nlink of that inode item */ - key.objectid = ino; - key.type = BTRFS_INODE_ITEM_KEY; - key.offset = 0; - - ret = btrfs_search_slot(trans, root, &key, &path, 0, 1); - if (ret > 0) { - ret = -ENOENT; - btrfs_release_path(&path); - goto out; - } - if (ret < 0) { - btrfs_release_path(&path); - goto out; - } - inode = btrfs_item_ptr(path.nodes[0], path.slots[0], - struct btrfs_inode_item); - btrfs_set_inode_nlink(path.nodes[0], inode, 1); - btrfs_mark_buffer_dirty(path.nodes[0]); - btrfs_release_path(&path); - return 0; -out: - btrfs_abort_transaction(trans, ret); - return ret; -} - static int btrfs_uuid_tree_add(struct btrfs_trans_handle *trans, u8 *uuid, u8 type, u64 subvol_id_cpu) { @@ -1939,7 +1871,7 @@ raid_groups: goto out; } - ret = create_data_reloc_tree(trans); + ret = btrfs_make_subvolume(trans, BTRFS_DATA_RELOC_TREE_OBJECTID); if (ret) { error("unable to create data reloc tree: %d", ret); goto out;