From patchwork Fri Aug 30 20:24:54 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Leo Martins X-Patchwork-Id: 13785521 Received: from mail-ot1-f67.google.com (mail-ot1-f67.google.com [209.85.210.67]) (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 DE3421BF317 for ; Fri, 30 Aug 2024 20:25:31 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.67 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725049533; cv=none; b=O4zGYcmT1tU8CqpEn07pDyiVoKRv4yOlk1cNet0fQi4Hz4W+A9InAFrk0+tAp8iflp0DUn+oKFxzrPkqnOzdMloc5QIfnGP4jY1Pq1ixwG1f7SMyeNGa7hbaQd9H11QeSMJoiOgmpEIRPspeYolDvNqVYGU+JWeFYioW8zzjM3I= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725049533; c=relaxed/simple; bh=3t+KyyzdtcfR1wwpyHcunpXA6IkhZ0QQjCSdICphS1o=; h=From:To:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=Tb77BDKLBOlUd4WkzXAF03kk3GHP1orDZbQaC1oolQfie5qg4SE1Cpq/viyLlxqurdUCAtCteRvqSqjwVTw6BLP2B7Dh0UL4CNTtNqWTkp9pyk2TPdIkZM9TFJnobdrBKMU+az0znGIsP5rkbi30o5f3FLsq9+H4f4oSEZvB4h8= 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=IcoR6xLq; arc=none smtp.client-ip=209.85.210.67 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="IcoR6xLq" Received: by mail-ot1-f67.google.com with SMTP id 46e09a7af769-7093c94435bso846193a34.0 for ; Fri, 30 Aug 2024 13:25:31 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1725049531; x=1725654331; 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=+T6vOjr6Ft1G0QZ4RXDnhdGk/36KaNdS+jaSptuVrgM=; b=IcoR6xLqFHEajO5NNZoe9r/g2mJ3N5YPKpMSW1ruWm0YeVv25aZpMtQh4awmpmZ2yV kDxg0HsRYFz5alPCQN1g4ytzscmLsDeaqSBlpQ4uSIbGxDN6bfqPvmistFe1l/Rx2bL/ 5umDem0c3MB+oQYPOHghUdkne0SrpOHMJwOIJkECoKV+gUcs4G88KtloohJjD6w/vW5x 4XOJhUj7C01CDgNPb+PExmcQChPD00aEWjrQ2fszJP3WHGCTPMI1Om4oRYDKbyQxC8/U 2IgIK1MyoxbDgazqWpxOqnwKbtpCFuogiI6+EblFrrsfdg00U0nvfuhEDEt5FClCIowI qmQw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1725049531; x=1725654331; 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=+T6vOjr6Ft1G0QZ4RXDnhdGk/36KaNdS+jaSptuVrgM=; b=nEJu+EdObgNDarrrxmBfPN5bG2Uru8xM+NFUqQJnC9/QMx30cifGl9JnqMcL2IiHMw jWkahObno0apUqETH3pi0gtqZX1vOOKDnNfdvsNj2oJXUqjEsEZ7WIInRkQX4KTIevi2 EGGUyc32AVWThrB9xBGCxubrlLfF+xZqHS4BAt0JKWZCAdtQE+SgR7wJfk1XzlVfjNRp 6Ci+BUEUFq0dxVM5tZGhhcawQDs3rMs2I9Yo4zML5LUkeDCCrXUIzWLabiVoNhtmpQVB g6UBo7Hsf+2SeBGHuE5NjiVNDwRzymssX5eDTohxrw111Gldy9/FtkemS9GxO0RKUMyn tKFg== X-Gm-Message-State: AOJu0YxaSZZxI74jAZd0WR/4CvSDq3dzMWcVntpaV2xFsJH5ZoysBPty 7PY3R9ZeWd7ikKydylyMSPKJbfphS0xKmYwmgvnJIMBC6Z2CNWnCzGGcbffO X-Google-Smtp-Source: AGHT+IHmVGT4mZCICAdQDH7UhOU0dNbXcub0fEzpuLpmGyA6/hsc8wJxoVWcv5ebW1eDJtlXfiTusw== X-Received: by 2002:a05:6830:44a9:b0:70d:f595:2cc7 with SMTP id 46e09a7af769-70f5c363e73mr7655092a34.14.1725049530687; Fri, 30 Aug 2024 13:25:30 -0700 (PDT) Received: from localhost (fwdproxy-eag-001.fbsv.net. [2a03:2880:3ff:1::face:b00c]) by smtp.gmail.com with ESMTPSA id 46e09a7af769-70f671c29adsm616985a34.65.2024.08.30.13.25.30 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 30 Aug 2024 13:25:30 -0700 (PDT) From: Leo Martins To: linux-btrfs@vger.kernel.org, kernel-team@fb.com Subject: [PATCH v4 1/2] btrfs: push cleanup into read_locked_inode Date: Fri, 30 Aug 2024 13:24:54 -0700 Message-ID: <874b2ef5a8decb15f52ba97442706ea68c3f13f3.1724970046.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 btrfs_add_inode_to_root so it can be called from btrfs_read_locked_inode, no changes were made to the function. Move cleanup code from btrfs_iget_path to btrfs_read_locked_inode. This improves readability and improves a leaky abstraction. Previously btrfs_iget_path had to handle a positive error case as a result of a call to btrfs_search_slot, but it makes more sense to handle this closer to the source of the call. Signed-off-by: Leo Martins --- fs/btrfs/inode.c | 99 +++++++++++++++++++++++++----------------------- 1 file changed, 51 insertions(+), 48 deletions(-) diff --git a/fs/btrfs/inode.c b/fs/btrfs/inode.c index e96b63d7e8fd..df0fe5e79ea2 100644 --- a/fs/btrfs/inode.c +++ b/fs/btrfs/inode.c @@ -3786,8 +3786,39 @@ 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 + * read a locked inode from the btree into the in-memory inode + * on failure clean up the inode */ static int btrfs_read_locked_inode(struct inode *inode, struct btrfs_path *in_path) @@ -3807,7 +3838,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) @@ -3816,7 +3847,7 @@ static int btrfs_read_locked_inode(struct inode *inode, if (!path) { path = btrfs_alloc_path(); if (!path) - return -ENOMEM; + goto error; } btrfs_get_inode_key(BTRFS_I(inode), &location); @@ -3825,7 +3856,13 @@ static int btrfs_read_locked_inode(struct inode *inode, if (ret) { if (path != in_path) btrfs_free_path(path); - return ret; + /* + * ret > 0 can come from btrfs_search_slot called by + * btrfs_lookup_inode(), this means the inode was not found. + */ + if (ret > 0) + ret = -ENOENT; + goto error; } leaf = path->nodes[0]; @@ -3988,7 +4025,15 @@ 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) + goto error; + return 0; +error: + iget_failed(inode); + return ret; } /* @@ -5500,35 +5545,7 @@ 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) { @@ -5609,25 +5626,11 @@ 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; + if (ret) + return ERR_PTR(ret); unlock_new_inode(inode); - return inode; -error: - iget_failed(inode); - return ERR_PTR(ret); } struct inode *btrfs_iget(u64 ino, struct btrfs_root *root) From patchwork Fri Aug 30 20:24:55 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Leo Martins X-Patchwork-Id: 13785522 Received: from mail-ot1-f66.google.com (mail-ot1-f66.google.com [209.85.210.66]) (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 4BC5D1BD503 for ; Fri, 30 Aug 2024 20:25:33 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.66 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725049534; cv=none; b=cFbg+J7i83LAbO+zRnzUq6EkHyxhqWJ2CNWA6DMEjlrpryA5q0+j/YTwvseFgQeX/Uw+x+FGjg43q038f/gTINPQ7LT4jmw2+MO2w+3UB0Wq9m062vxuEuYLYfdSQRa1sLauS4YOXG9CSoHSLvFWueS8V5iUSoy7CloV4GeZ4gQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725049534; c=relaxed/simple; bh=kqu3UFTtViNPx941VnSyTnq23AgoM4A1L59U+vZjIVs=; h=From:To:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=VQXi7pF/zmTVmKrdjw69wU3gGFBrodHEukv1sRD8liUL83XbBpuVtjy6WrWvEa3B+Uvb5We5+e/uYHlvQwakP2MPa7YI6ilE5EtuaolpPIXYcCTX5QrzvQVUvSiFdt4BbNJo6OfuuRig/IZMcdx88O2HGm+Tbl8UheBXEOS3c3g= 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=QtVNtTgS; arc=none smtp.client-ip=209.85.210.66 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="QtVNtTgS" Received: by mail-ot1-f66.google.com with SMTP id 46e09a7af769-70f60d73436so1298570a34.1 for ; Fri, 30 Aug 2024 13:25:33 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1725049532; x=1725654332; 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=/R6c+2xa/lb1gVE0PSUlSGWED0gxm7v/GXUI9/6qG58=; b=QtVNtTgSSkbSyypcQfyOmksGpftb0wCvkrsrD3T3ouA0ajT+k67S16MPPpUibRsZOO Ly/aaf01xkh8sR4XXI0rJeN2pxND5Gx5oYB7QsBQJU8xrXckEZmac9VL9NZ/swseWt34 XiKEfNTZQMpQLyTUEUBnq/aU4tq2A5/KHum4J7QGaMo6lrF/FfDJDg2acqt9OgM5esJH WDX1i3Z1XzobAPVnOlOLoCI6ILxIV0vWT2Dm+fq+bFwNZoD1EUsPegu5qc3YQvpqw99F Zv1Kf1nNFFSED5fGxdnFm0Wi0nJJBWxLvAL1EjbRWjVMd8xfROIIhbB3dt0SIt9NJI/6 P5XQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1725049532; x=1725654332; 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=/R6c+2xa/lb1gVE0PSUlSGWED0gxm7v/GXUI9/6qG58=; b=VHCcLKriHsqJng/SYDomHmdM7kcx671OKTdvJtpEjw/Ya82HQKXQDZ18AdLMZNkifB 0qFDCxsAhtSjMbZAyQBROXWdcYL/8kzSBOVhcpCweslMUh7CvA2RGNzs7G2Wf7t/e+qO AJdaNl8X12OdD6coTn6dLwr1PXwghKIUSbjxHPAsl/9F87dyC083LImP+1dYxqA9/YmE mmk9UDa8UV39o/hROpzgE69Uz3kjtbm7yGr1qde9gz8Knau6HKvIfkCBXr0AVY+s48N4 sbtPrF6ic8rSuXh18GZtsWP5RtrNxPpvdyRzhf72wGHhRcSd1+ERC2Hpe/9Ny2hdBYOk mcVA== X-Gm-Message-State: AOJu0YxgV8R2hvCKqYf1N8xxwskQHhHL5JxAKmMbdbbXqLMzcZ3m0Xjc PheHz6yADhTa1/+yiJLGr/fD8hIhkiKbsdbVhQ3v0Xy1sHdc3JtFVftnmyK8 X-Google-Smtp-Source: AGHT+IHlJ3awfdTBkv6FbNc7yhKVaS8X+h02MxCREtDrcQ+GuV7VDsohHN/QQkAICO04Z0TgZTDsfw== X-Received: by 2002:a05:6830:6991:b0:6f9:6577:71c3 with SMTP id 46e09a7af769-70f5c353e80mr7093055a34.6.1725049532115; Fri, 30 Aug 2024 13:25:32 -0700 (PDT) Received: from localhost (fwdproxy-eag-114.fbsv.net. [2a03:2880:3ff:72::face:b00c]) by smtp.gmail.com with ESMTPSA id 46e09a7af769-70f671cf763sm629840a34.80.2024.08.30.13.25.31 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 30 Aug 2024 13:25:31 -0700 (PDT) From: Leo Martins To: linux-btrfs@vger.kernel.org, kernel-team@fb.com Subject: [PATCH v4 2/2] btrfs: remove conditional path allocation Date: Fri, 30 Aug 2024 13:24:55 -0700 Message-ID: <070163e08339759d751bf956bd7084772efbb074.1724970046.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 Remove conditional path allocation from btrfs_read_locked_inode. Add an ASSERT(path) to indicate it should never be called with a NULL path. Call btrfs_read_locked_inode directly from btrfs_iget. This causes code duplication between btrfs_iget and btrfs_iget_path, but I think this is justifiable as it removes the need for conditionally allocating the path inside of read_locked_inode. This makes the code easier to reason about and makes it clear who has the responsibility of allocating and freeing the path. Signed-off-by: Leo Martins --- fs/btrfs/inode.c | 46 +++++++++++++++++++++++++++++++--------------- 1 file changed, 31 insertions(+), 15 deletions(-) diff --git a/fs/btrfs/inode.c b/fs/btrfs/inode.c index df0fe5e79ea2..a4b4e4190624 100644 --- a/fs/btrfs/inode.c +++ b/fs/btrfs/inode.c @@ -3821,10 +3821,9 @@ static int btrfs_add_inode_to_root(struct btrfs_inode *inode, bool prealloc) * on failure clean up the 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; @@ -3844,18 +3843,12 @@ static int btrfs_read_locked_inode(struct inode *inode, if (!ret) filled = true; - if (!path) { - path = btrfs_alloc_path(); - if (!path) - goto error; - } + 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); /* * ret > 0 can come from btrfs_search_slot called by * btrfs_lookup_inode(), this means the inode was not found. @@ -3997,8 +3990,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); @@ -5608,9 +5599,8 @@ static struct inode *btrfs_iget_locked(u64 ino, struct btrfs_root *root) /* * Get an inode object given its inode number and corresponding root. - * Path can be preallocated to prevent recursing back to iget through - * allocator. NULL is also valid but may require an additional allocation - * later. + * Path is preallocated to prevent recursing back to iget through + * allocator. */ struct inode *btrfs_iget_path(u64 ino, struct btrfs_root *root, struct btrfs_path *path) @@ -5633,9 +5623,35 @@ struct inode *btrfs_iget_path(u64 ino, struct btrfs_root *root, return inode; } +/* + * Get an inode object given its inode number and corresponding root. + */ struct inode *btrfs_iget(u64 ino, struct btrfs_root *root) { - return btrfs_iget_path(ino, root, NULL); + struct inode *inode; + struct btrfs_path *path; + int ret; + + inode = btrfs_iget_locked(ino, root); + if (!inode) + return ERR_PTR(-ENOMEM); + + if (!(inode->i_state & I_NEW)) + return inode; + + path = btrfs_alloc_path(); + if (!path) + return ERR_PTR(-ENOMEM); + + ret = btrfs_read_locked_inode(inode, path); + + btrfs_free_path(path); + + if (ret) + return ERR_PTR(ret); + + unlock_new_inode(inode); + return inode; } static struct inode *new_simple_dir(struct inode *dir,