git: 5e6079209e9b - stable/14 - tmpfs: Rework file handles

From: Olivier Certner <olce_at_FreeBSD.org>
Date: Thu, 16 Jan 2025 18:08:41 UTC
The branch stable/14 has been updated by olce:

URL: https://cgit.FreeBSD.org/src/commit/?id=5e6079209e9b6db2bd2980e1b97e7cb4cf8b5364

commit 5e6079209e9b6db2bd2980e1b97e7cb4cf8b5364
Author:     Olivier Certner <olce@FreeBSD.org>
AuthorDate: 2024-12-06 20:23:59 +0000
Commit:     Olivier Certner <olce@FreeBSD.org>
CommitDate: 2025-01-16 18:07:02 +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
    
    (cherry picked from commit 1ccbdf561f417f9fe802131d5b1756ac45fd314d)
---
 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 7ebdffbec84f..277cb599d78b 100644
--- a/sys/fs/tmpfs/tmpfs.h
+++ b/sys/fs/tmpfs/tmpfs.h
@@ -439,11 +439,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 65d54b446849..c404eb81a8d2 100644
--- a/sys/fs/tmpfs/tmpfs_vfsops.c
+++ b/sys/fs/tmpfs/tmpfs_vfsops.c
@@ -586,29 +586,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 4571a2855be3..729feb0329a4 100644
--- a/sys/fs/tmpfs/tmpfs_vnops.c
+++ b/sys/fs/tmpfs/tmpfs_vnops.c
@@ -1684,23 +1684,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);
 }