Subject: Re: file id alignment
To: None <tech-kern@NetBSD.org>
From: Martin Husemann <martin@duskware.de>
List: tech-kern
Date: 06/30/2006 09:10:50
--T4sUOijqQbZv57TR
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline

On Thu, Jun 29, 2006 at 11:51:04PM +0200, Martin Husemann wrote:
> Of course we could just tell FS authors to be carefull and copy byte by 
> byte ;-)

Actually this option is not as bad as it initially sounded - creating the
real fid on the stack and memcpy to the pointer passed should work in this
case.

Guess I should convert all vptofh() in tree... - any objections? Example
(uncompiled, untested) patch appended.

Martin

--T4sUOijqQbZv57TR
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment; filename=patch

Index: tmpfs_vfsops.c
===================================================================
RCS file: /cvsroot/src/sys/fs/tmpfs/tmpfs_vfsops.c,v
retrieving revision 1.12
diff -u -p -r1.12 tmpfs_vfsops.c
--- tmpfs_vfsops.c	14 May 2006 21:31:52 -0000	1.12
+++ tmpfs_vfsops.c	30 Jun 2006 07:08:01 -0000
@@ -305,23 +305,24 @@ static int
 tmpfs_fhtovp(struct mount *mp, struct fid *fhp, struct vnode **vpp)
 {
 	boolean_t found;
-	struct tmpfs_fid *tfhp;
+	struct tmpfs_fid tfh;
 	struct tmpfs_mount *tmp;
 	struct tmpfs_node *node;
 
 	tmp = VFS_TO_TMPFS(mp);
 
-	tfhp = (struct tmpfs_fid *)fhp;
-	if (tfhp->tf_len != sizeof(struct tmpfs_fid))
+	if (fhp->fid_len != sizeof(struct tmpfs_fid))
 		return EINVAL;
 
-	if (tfhp->tf_id >= tmp->tm_nodes_max)
+	memcpy(&tfh, fhp, sizeof(struct tmpfs_fid));
+
+	if (tfh.tf_id >= tmp->tm_nodes_max)
 		return EINVAL;
 
 	found = FALSE;
 	LIST_FOREACH(node, &tmp->tm_nodes_used, tn_entries) {
-		if (node->tn_id == tfhp->tf_id &&
-		    node->tn_gen == tfhp->tf_gen) {
+		if (node->tn_id == tfh.tf_id &&
+		    node->tn_gen == tfh.tf_gen) {
 			found = TRUE;
 			break;
 		}
@@ -335,15 +336,15 @@ tmpfs_fhtovp(struct mount *mp, struct fi
 static int
 tmpfs_vptofh(struct vnode *vp, struct fid *fhp)
 {
-	struct tmpfs_fid *tfhp;
+	struct tmpfs_fid tfh;
 	struct tmpfs_node *node;
 
-	tfhp = (struct tmpfs_fid *)fhp;
 	node = VP_TO_TMPFS_NODE(vp);
 
-	tfhp->tf_len = sizeof(struct tmpfs_fid);
-	tfhp->tf_id = node->tn_id;
-	tfhp->tf_gen = node->tn_gen;
+	tfh.tf_len = sizeof(struct tmpfs_fid);
+	tfh.tf_id = node->tn_id;
+	tfh.tf_gen = node->tn_gen;
+	memcpy(fid, &tfh, sizeof tfh);
 
 	return 0;
 }

--T4sUOijqQbZv57TR--