git: 1ccbdf561f41 - main - tmpfs: Rework file handles

From: Olivier Certner <olce_at_FreeBSD.org>
Date: Mon, 23 Dec 2024 14:47:51 UTC
The branch main has been updated by olce:

URL: https://cgit.FreeBSD.org/src/commit/?id=1ccbdf561f417f9fe802131d5b1756ac45fd314d

commit 1ccbdf561f417f9fe802131d5b1756ac45fd314d
Author:     Olivier Certner <olce@FreeBSD.org>
AuthorDate: 2024-12-06 20:23:59 +0000
Commit:     Olivier Certner <olce@FreeBSD.org>
CommitDate: 2024-12-23 14:46:55 +0000

    tmpfs: Rework file handles
    
    Change 'struct tmpfs_fid_data' to behave consistently with the private
    structure other FSes use.  In a nutshell, make it a full alias of
    'struct fid', instead of just using it to fill 'fid_data'.  This implies
    adding a length field at start (aliasing 'fid_len' of 'struct fid'), and
    filling 'fid_len' with the full size of the aliased structure.
    
    To ensure that the new 'struct tmpfs_fid_data' is smaller than 'struct
    fid', which the compile-time assert introduced in commit
    91b5592a1e1af974 ("fs: Add static asserts for the size of fid
    structures") checks (and thus was not strong enough when added), use
    '__packed'.
    
    A consequence of this change is that copying the 'struct tmpfs_fid_data'
    into a stack-allocated variable becomes unnecessary, we simply rely on
    the compiler emitting the proper code on seeing '__packed' (and on the
    start of 'struct tmpfs_fid_data' being naturally aligned, which is
    normally guaranteed by kernel's malloc() and/or inclusion in 'struct
    fhandle').
    
    Reviewed by:    markj
    Sponsored by:   The FreeBSD Foundation
    Differential Revision:  https://reviews.freebsd.org/D47956
---
 sys/fs/tmpfs/tmpfs.h        |  5 ++---
 sys/fs/tmpfs/tmpfs_vfsops.c | 16 ++++++----------
 sys/fs/tmpfs/tmpfs_vnops.c  | 16 ++++------------
 3 files changed, 12 insertions(+), 25 deletions(-)

diff --git a/sys/fs/tmpfs/tmpfs.h b/sys/fs/tmpfs/tmpfs.h
index 58f8720c3f84..52307cc7c7b2 100644
--- a/sys/fs/tmpfs/tmpfs.h
+++ b/sys/fs/tmpfs/tmpfs.h
@@ -448,11 +448,10 @@ struct tmpfs_mount {
  * NFS code.
  */
 struct tmpfs_fid_data {
+	unsigned short		tfd_len;
 	ino_t			tfd_id;
 	unsigned long		tfd_gen;
-};
-_Static_assert(sizeof(struct tmpfs_fid_data) <= MAXFIDSZ,
-    "(struct tmpfs_fid_data) is larger than (struct fid).fid_data");
+} __packed;
 
 struct tmpfs_dir_cursor {
 	struct tmpfs_dirent	*tdc_current;
diff --git a/sys/fs/tmpfs/tmpfs_vfsops.c b/sys/fs/tmpfs/tmpfs_vfsops.c
index dd48a24dab2f..431893b77bb9 100644
--- a/sys/fs/tmpfs/tmpfs_vfsops.c
+++ b/sys/fs/tmpfs/tmpfs_vfsops.c
@@ -585,29 +585,25 @@ static int
 tmpfs_fhtovp(struct mount *mp, struct fid *fhp, int flags,
     struct vnode **vpp)
 {
-	struct tmpfs_fid_data tfd;
+	struct tmpfs_fid_data *tfd;
 	struct tmpfs_mount *tmp;
 	struct tmpfs_node *node;
 	int error;
 
-	if (fhp->fid_len != sizeof(tfd))
+	if (fhp->fid_len != sizeof(*tfd))
 		return (EINVAL);
 
-	/*
-	 * Copy from fid_data onto the stack to avoid unaligned pointer use.
-	 * See the comment in sys/mount.h on struct fid for details.
-	 */
-	memcpy(&tfd, fhp->fid_data, fhp->fid_len);
+	tfd = (struct tmpfs_fid_data *)fhp;
 
 	tmp = VFS_TO_TMPFS(mp);
 
-	if (tfd.tfd_id >= tmp->tm_nodes_max)
+	if (tfd->tfd_id >= tmp->tm_nodes_max)
 		return (EINVAL);
 
 	TMPFS_LOCK(tmp);
 	LIST_FOREACH(node, &tmp->tm_nodes_used, tn_entries) {
-		if (node->tn_id == tfd.tfd_id &&
-		    node->tn_gen == tfd.tfd_gen) {
+		if (node->tn_id == tfd->tfd_id &&
+		    node->tn_gen == tfd->tfd_gen) {
 			tmpfs_ref_node(node);
 			break;
 		}
diff --git a/sys/fs/tmpfs/tmpfs_vnops.c b/sys/fs/tmpfs/tmpfs_vnops.c
index 162977b8abf7..83e43ec7ba13 100644
--- a/sys/fs/tmpfs/tmpfs_vnops.c
+++ b/sys/fs/tmpfs/tmpfs_vnops.c
@@ -1706,23 +1706,15 @@ vop_vptofh {
 };
 */
 {
-	struct tmpfs_fid_data tfd;
+	struct tmpfs_fid_data *const tfd = (struct tmpfs_fid_data *)ap->a_fhp;
 	struct tmpfs_node *node;
-	struct fid *fhp;
 	_Static_assert(sizeof(struct tmpfs_fid_data) <= sizeof(struct fid),
 	    "struct tmpfs_fid_data cannot be larger than struct fid");
 
 	node = VP_TO_TMPFS_NODE(ap->a_vp);
-	fhp = ap->a_fhp;
-	fhp->fid_len = sizeof(tfd);
-
-	/*
-	 * Copy into fid_data from the stack to avoid unaligned pointer use.
-	 * See the comment in sys/mount.h on struct fid for details.
-	 */
-	tfd.tfd_id = node->tn_id;
-	tfd.tfd_gen = node->tn_gen;
-	memcpy(fhp->fid_data, &tfd, fhp->fid_len);
+	tfd->tfd_len = sizeof(*tfd);
+	tfd->tfd_gen = node->tn_gen;
+	tfd->tfd_id = node->tn_id;
 
 	return (0);
 }