From patchwork Tue Aug 20 20:13:18 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Leo Martins X-Patchwork-Id: 13770580 Received: from mail-oi1-f196.google.com (mail-oi1-f196.google.com [209.85.167.196]) (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 0F5881C37A3 for ; Tue, 20 Aug 2024 20:13:49 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.167.196 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724184831; cv=none; b=ovUgWKMXKdRnE2ieE6Pe8103S0orldNgkJmrQeBXUodXvRNww/qXzI0qI000dZlIkT35dhOfhI4DxvafTEEyeOwhdQwIIrq4punQK8MjFohJQo3h3rBnbQEO9ZVNDy16yO1pZNk7Q4kIDzc2SVWWJkylHmXA4MJZ8f8Yn8re6Zw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724184831; c=relaxed/simple; bh=O8V3eH/rKGr520DaDTqY9ch+GuZcFemzL+NKd8NLHWs=; h=From:To:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=FRqsenXzSoEPXKshKz2Wl8iTETLqMWOAmBIqvi/Ek0zrRuokCvFt3oku0vzMDalgJa3V069M6z0RXdxJuC3jL+ir5SHaLkgYomvforHAXzO9rpNLzAab4SIDvWhkCGykOgsLyaidFR/05hSR6MtSaORM2VPPiTwzAlATlgtOIUM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=azGRy6tO; arc=none smtp.client-ip=209.85.167.196 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="azGRy6tO" Received: by mail-oi1-f196.google.com with SMTP id 5614622812f47-3db18c4927bso3351560b6e.1 for ; Tue, 20 Aug 2024 13:13:49 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1724184829; x=1724789629; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:from:to:cc:subject:date:message-id :reply-to; bh=Y+AJcbxw1ym5nhBriORjo5dFxe1Q9MhybF9bfCe6J2w=; b=azGRy6tOdHXoCACNoUB7iSXeK59YMNjfltfKX1iVX2WZM8f4OeIny7Pqgy9ewuRDfN AKHedI5Fe+XMv1zEk+2PcQYLUy1Ca/Va6tdKibg1D7vhrv7nZ5N1REcAsAeUTUkhZNZo OPGDMlLnUwSCaQaVZ1FV79zH8yreAk2OGdpOczJ8DdjwzJ092UptNdtEV067bfAgdrsl SwTIJekWtFxejtuuz+DRuL8ZnybS4ESzxO70+NBw6rMcmr3TO5845qY/Ylf9lZHxZWcx KM6oQmyIHHAPm5IKJl52MLLEz1Ol0Ake+Xm+nbKcbO1x8ftxoEg7MbRvQwtjuX1cBo1Y fNmQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1724184829; x=1724789629; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=Y+AJcbxw1ym5nhBriORjo5dFxe1Q9MhybF9bfCe6J2w=; b=v0FKEzZyJKnhieDrwMkR8HNuWWItgz+2x71D4FwsDAz2vEe71Ewjb1X90ZQEODl4QE Wke44dbaNM2VQ5qaouqq7dmpMtdqfHQgE9RYB/NW0y64r8oxH0Vbsmz2sm/GfYaYfBhD u5SWnSYgM9zj4ZwjP6shIkh6eSo9U2LXLgADfb/nXUNZa+3a4SndYPwu+FgvrQSrXTde uQQxFYggc/lHVlXZGBiOHsPtllVEW2Czfw7LQNZX0sTJAhudPJAY5TV+6TnWG30H07b+ JkYlSxZGq4kZ2nCU1PZDmY3hPUuAe8E5X3YO2grYxG3bmtqQ/1digv+S4rF1XKH6Cvld pNCQ== X-Gm-Message-State: AOJu0YzvwhqGASKtHul3RtKDw1e1F5k/9hMfABP1HQbj4gsZDMHVorww mwIBolaWXAjDw3GiSNLfII5Bp//vRoww4MKlieq5pCrZbbuM5HZNjrO4MLJj X-Google-Smtp-Source: AGHT+IHc6DJ0r9GLYffwn7Sjbbn82jPts1a4hsL9uTsRAPlqu9ec6sbPYJZWMl6ajOGoSJPcHAKxPw== X-Received: by 2002:a05:6808:f04:b0:3d5:64f3:df63 with SMTP id 5614622812f47-3de194f5de4mr560229b6e.17.1724184828804; Tue, 20 Aug 2024 13:13:48 -0700 (PDT) Received: from localhost (fwdproxy-eag-007.fbsv.net. [2a03:2880:3ff:7::face:b00c]) by smtp.gmail.com with ESMTPSA id 5614622812f47-3dd4236b798sm2329116b6e.5.2024.08.20.13.13.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 20 Aug 2024 13:13:48 -0700 (PDT) From: Leo Martins To: linux-btrfs@vger.kernel.org, kernel-team@fb.com Subject: [PATCH v2 1/2] btrfs: remove conditional path allocation from read_locked_inode, add path allocation to iget Date: Tue, 20 Aug 2024 13:13:18 -0700 Message-ID: <652ef8f5f0b46c2488a2f72bf34a83d9bc8357db.1724184314.git.loemra.dev@gmail.com> X-Mailer: git-send-email 2.43.5 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-btrfs@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Move the path allocation from inside btrfs_read_locked_inode to btrfs_iget. This makes the code easier to reason about as it is clear where the allocation occurs and who is in charge of freeing the path. I have investigated all of the callers of btrfs_iget_path to make sure that it is never called with a null path with the expectation of a path allocation. All of the null calls seem to come from btrfs_iget so it makes sense to do the allocation within btrfs_iget. Reviewed-by: Josef Bacik --- fs/btrfs/inode.c | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/fs/btrfs/inode.c b/fs/btrfs/inode.c index a8ad540d6de2..f2959803f9d7 100644 --- a/fs/btrfs/inode.c +++ b/fs/btrfs/inode.c @@ -3790,10 +3790,9 @@ static int btrfs_init_file_extent_tree(struct btrfs_inode *inode) * read an inode from the btree into the in-memory inode */ static int btrfs_read_locked_inode(struct inode *inode, - struct btrfs_path *in_path) + struct btrfs_path *path) { struct btrfs_fs_info *fs_info = inode_to_fs_info(inode); - struct btrfs_path *path = in_path; struct extent_buffer *leaf; struct btrfs_inode_item *inode_item; struct btrfs_root *root = BTRFS_I(inode)->root; @@ -3813,20 +3812,13 @@ static int btrfs_read_locked_inode(struct inode *inode, if (!ret) filled = true; - if (!path) { - path = btrfs_alloc_path(); - if (!path) - return -ENOMEM; - } + ASSERT(path); btrfs_get_inode_key(BTRFS_I(inode), &location); ret = btrfs_lookup_inode(NULL, root, path, &location, 0); - if (ret) { - if (path != in_path) - btrfs_free_path(path); + if (ret) return ret; - } leaf = path->nodes[0]; @@ -3960,8 +3952,6 @@ static int btrfs_read_locked_inode(struct inode *inode, btrfs_ino(BTRFS_I(inode)), btrfs_root_id(root), ret); } - if (path != in_path) - btrfs_free_path(path); if (!maybe_acls) cache_no_acl(inode); @@ -5632,7 +5622,17 @@ struct inode *btrfs_iget_path(u64 ino, struct btrfs_root *root, struct inode *btrfs_iget(u64 ino, struct btrfs_root *root) { - return btrfs_iget_path(ino, root, NULL); + struct btrfs_path *path; + struct inode *inode; + + path = btrfs_alloc_path(); + if (!path) + return ERR_PTR(-ENOMEM); + + inode = btrfs_iget_path(ino, root, path); + + btrfs_free_path(path); + return inode; } static struct inode *new_simple_dir(struct inode *dir, From patchwork Tue Aug 20 20:13:19 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Leo Martins X-Patchwork-Id: 13770581 Received: from mail-ot1-f65.google.com (mail-ot1-f65.google.com [209.85.210.65]) (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 895B31B86F8 for ; Tue, 20 Aug 2024 20:13:51 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.65 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724184833; cv=none; b=AwfCi0KyMy7WrLQgmMnByPQZnZrlro+OvhetEsgjDRsL6IAvVSF71Uk8/VhytA4EZMYOeIl6DQw7sNZ67XlLHGU10huQ7XvKGtg4EydhnlHU4U4r4dNMtLIXHZzpBhUIZNziE29JFYCu1aAZLbLMpPIehqs9pL9w8Bf8b7KdsNE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724184833; c=relaxed/simple; bh=1PahAhzy2dX2r1F92KipdARSLiqB9OL7fdHR98OMBHo=; h=From:To:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=Uf5QiGBuIoZ8L3g67EjB4rv560vCjjrd4wGmSnhxK8vH2sNdYP7XnGxvgitdmPHdYp8bLojTXyWz2XWU5UI/r+kDVdck2XPFtRkN4SO6mEZBsGALUQHnopDi2us+3Dxh/puh0C5G1eSr2ShpQvQaHEIn/pw7mz4m+/Aalw12ZKE= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=cwJjR7MM; arc=none smtp.client-ip=209.85.210.65 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="cwJjR7MM" Received: by mail-ot1-f65.google.com with SMTP id 46e09a7af769-70968db52d0so6173239a34.3 for ; Tue, 20 Aug 2024 13:13:51 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1724184830; x=1724789630; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:from:to:cc:subject:date:message-id :reply-to; bh=eSLMbz4l62KUhngn3MJd2AxIBdF6UC9GIso9Qkc0bhw=; b=cwJjR7MM8sMwjNaEbx4o/dT/gNjGWfMtUlZ2xjdWVF5D9eWLCJz7v2WNV/mQFbdTZq IP8My7qkDsmjvG2nJ8UnNWvVMNlfCzf+U7JhPCrYdjZzpX+bFqlUI9PG5GBGkAbQ7oDp SJsZ3YL305mRM1o+DSwLPFkqonCSZ2Au1S7zB1R0yz5CT91Iq/dg5sRumnHLSMw6ipru vgBJVznZe49pTdIefxCT3W8TmAizcRexSUYKYg6nDjjTs7arlgCU1N3PQb8lqOGNOHjR 696gjXAsyrZfxmHlIERLboWWm8W8VqwcDYHl3kMeNn0oowqHWKigWaUEe1bWqn7c4mhR MO3w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1724184830; x=1724789630; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=eSLMbz4l62KUhngn3MJd2AxIBdF6UC9GIso9Qkc0bhw=; b=QiX1xL135GKrVZqqtPtXLv1X3tJTZ+K3NPmpsjb9a3PehzOxFPiW3c8XhF/L2dyAle nPq+RsZ0UsKcs5NLJblNjBI5P1uSUUcklQjwKw7JuziU9cD8qO9Qo5ldFw/iHw/kEUxs q6EV/niEumdghQnX2L5hUVoLO9hkaHdVWLiH+Elb3kT+aQ7A72JN8Ma3206IzuY8JYlt lH8XAOmnA2sJ8JDeGUHFZ/AVhcppGvW71octg4+INFPmxeykrwb5udMtsvCgUCBXuqNY tTNdTN0t+yy9/NOCRzmRU5uBTKUa2JbP5+yf0vopVvMO+5eUI+4RnBWT7U4ozFF+U2+i D6vA== X-Gm-Message-State: AOJu0Yy+mMpd4Y3Clrq9lfOTbgJ0Y77NIFBoRp7836rQe7k/IXAaIsmE CL5VnU3nAcVqlQgWLAIO1MvBin8KxUUXbts0LSbK6oj5FpNx/EeBzPLFYiYE X-Google-Smtp-Source: AGHT+IGM84FEaoBcDUwqBMpObjNgxIAVmuJLQ3fL6hyY33tILjslw7YTFrxjhrEDPEN5vNE4v4tNoQ== X-Received: by 2002:a05:6830:61c8:b0:703:6578:8e2b with SMTP id 46e09a7af769-70df886b52dmr210239a34.23.1724184830258; Tue, 20 Aug 2024 13:13:50 -0700 (PDT) Received: from localhost (fwdproxy-eag-116.fbsv.net. [2a03:2880:3ff:74::face:b00c]) by smtp.gmail.com with ESMTPSA id 46e09a7af769-70ca66205dcsm2906096a34.63.2024.08.20.13.13.49 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 20 Aug 2024 13:13:50 -0700 (PDT) From: Leo Martins To: linux-btrfs@vger.kernel.org, kernel-team@fb.com Subject: [PATCH v2 2/2] btrfs: move clean up code from btrfs_iget_path to btrfs_read_locked_inode Date: Tue, 20 Aug 2024 13:13:19 -0700 Message-ID: X-Mailer: git-send-email 2.43.5 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-btrfs@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Move all the clean up code from btrfs_iget_path to btrfs_read_locked_inode. I had to move btrfs_add_inode_to_root as it needs to be called by btrfs_read_locked_inode, but made no changes to it. --- fs/btrfs/inode.c | 102 +++++++++++++++++++++++------------------------ 1 file changed, 51 insertions(+), 51 deletions(-) diff --git a/fs/btrfs/inode.c b/fs/btrfs/inode.c index f2959803f9d7..f4d20e590b70 100644 --- a/fs/btrfs/inode.c +++ b/fs/btrfs/inode.c @@ -3786,6 +3786,36 @@ static int btrfs_init_file_extent_tree(struct btrfs_inode *inode) return 0; } +static int btrfs_add_inode_to_root(struct btrfs_inode *inode, bool prealloc) +{ + struct btrfs_root *root = inode->root; + struct btrfs_inode *existing; + const u64 ino = btrfs_ino(inode); + int ret; + + if (inode_unhashed(&inode->vfs_inode)) + return 0; + + if (prealloc) { + ret = xa_reserve(&root->inodes, ino, GFP_NOFS); + if (ret) + return ret; + } + + existing = xa_store(&root->inodes, ino, inode, GFP_ATOMIC); + + if (xa_is_err(existing)) { + ret = xa_err(existing); + ASSERT(ret != -EINVAL); + ASSERT(ret != -ENOMEM); + return ret; + } else if (existing) { + WARN_ON(!(existing->vfs_inode.i_state & (I_WILL_FREE | I_FREEING))); + } + + return 0; +} + /* * read an inode from the btree into the in-memory inode */ @@ -3806,7 +3836,7 @@ static int btrfs_read_locked_inode(struct inode *inode, ret = btrfs_init_file_extent_tree(BTRFS_I(inode)); if (ret) - return ret; + goto error; ret = btrfs_fill_inode(inode, &rdev); if (!ret) @@ -3817,8 +3847,15 @@ static int btrfs_read_locked_inode(struct inode *inode, btrfs_get_inode_key(BTRFS_I(inode), &location); ret = btrfs_lookup_inode(NULL, root, path, &location, 0); - if (ret) - return ret; + /* + * ret > 0 can come from btrfs_search_slot called by + * btrfs_read_locked_inode(), this means the inode item was not found. + */ + if (ret > 0) + ret = -ENOENT; + if (ret < 0) + goto error; + leaf = path->nodes[0]; @@ -3978,7 +4015,16 @@ static int btrfs_read_locked_inode(struct inode *inode, } btrfs_sync_inode_flags_to_i_flags(inode); + + ret = btrfs_add_inode_to_root(BTRFS_I(inode), true); + if (ret < 0) + goto error; + + unlock_new_inode(inode); return 0; +error: + iget_failed(inode); + return ret; } /* @@ -5490,36 +5536,6 @@ static int fixup_tree_root_location(struct btrfs_fs_info *fs_info, return err; } -static int btrfs_add_inode_to_root(struct btrfs_inode *inode, bool prealloc) -{ - struct btrfs_root *root = inode->root; - struct btrfs_inode *existing; - const u64 ino = btrfs_ino(inode); - int ret; - - if (inode_unhashed(&inode->vfs_inode)) - return 0; - - if (prealloc) { - ret = xa_reserve(&root->inodes, ino, GFP_NOFS); - if (ret) - return ret; - } - - existing = xa_store(&root->inodes, ino, inode, GFP_ATOMIC); - - if (xa_is_err(existing)) { - ret = xa_err(existing); - ASSERT(ret != -EINVAL); - ASSERT(ret != -ENOMEM); - return ret; - } else if (existing) { - WARN_ON(!(existing->vfs_inode.i_state & (I_WILL_FREE | I_FREEING))); - } - - return 0; -} - static void btrfs_del_inode_from_root(struct btrfs_inode *inode) { struct btrfs_root *root = inode->root; @@ -5599,25 +5615,9 @@ struct inode *btrfs_iget_path(u64 ino, struct btrfs_root *root, return inode; ret = btrfs_read_locked_inode(inode, path); - /* - * ret > 0 can come from btrfs_search_slot called by - * btrfs_read_locked_inode(), this means the inode item was not found. - */ - if (ret > 0) - ret = -ENOENT; - if (ret < 0) - goto error; - - ret = btrfs_add_inode_to_root(BTRFS_I(inode), true); - if (ret < 0) - goto error; - - unlock_new_inode(inode); - + if (ret) + return ERR_PTR(ret); return inode; -error: - iget_failed(inode); - return ERR_PTR(ret); } struct inode *btrfs_iget(u64 ino, struct btrfs_root *root)