Module Name:    src
Committed By:   dholland
Date:           Sun Sep 20 04:51:43 UTC 2015

Modified Files:
        src/sbin/fsck_lfs: dir.c
        src/sys/ufs/lfs: lfs.h lfs_accessors.h lfs_rename.c lfs_vnops.c

Log Message:
Clean up struct lfs_dirtemplate.


To generate a diff of this commit:
cvs rdiff -u -r1.43 -r1.44 src/sbin/fsck_lfs/dir.c
cvs rdiff -u -r1.189 -r1.190 src/sys/ufs/lfs/lfs.h
cvs rdiff -u -r1.28 -r1.29 src/sys/ufs/lfs/lfs_accessors.h
cvs rdiff -u -r1.13 -r1.14 src/sys/ufs/lfs/lfs_rename.c
cvs rdiff -u -r1.290 -r1.291 src/sys/ufs/lfs/lfs_vnops.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sbin/fsck_lfs/dir.c
diff -u src/sbin/fsck_lfs/dir.c:1.43 src/sbin/fsck_lfs/dir.c:1.44
--- src/sbin/fsck_lfs/dir.c:1.43	Tue Sep 15 15:02:25 2015
+++ src/sbin/fsck_lfs/dir.c	Sun Sep 20 04:51:43 2015
@@ -1,4 +1,4 @@
-/* $NetBSD: dir.c,v 1.43 2015/09/15 15:02:25 dholland Exp $	 */
+/* $NetBSD: dir.c,v 1.44 2015/09/20 04:51:43 dholland Exp $	 */
 
 /*
  * Copyright (c) 1980, 1986, 1993
@@ -54,6 +54,7 @@
 
 const char *lfname = "lost+found";
 int lfmode = 01700;
+#if 0
 struct lfs_dirtemplate emptydir = {
 	.dot_ino = 0,
 	.dot_reclen = LFS_DIRBLKSIZ,
@@ -70,7 +71,6 @@ struct lfs_dirtemplate dirhead = {
 	.dotdot_namlen = 2,
 	.dotdot_name = ".."
 };
-#if 0
 struct lfs_odirtemplate odirhead = {
 	.dot_ino = 0,
 	.dot_reclen = 12,
@@ -587,6 +587,24 @@ makeentry(ino_t parent, ino_t ino, const
 }
 
 /*
+ * Initialize a completely empty directory block.
+ * (block size is LFS_DIRBLKSIZ)
+ */
+static void
+zerodirblk(void *buf)
+{
+	struct lfs_dirheader *dirp;
+
+	dirp = buf;
+	lfs_dir_setino(fs, dirp, 0);
+	lfs_dir_setreclen(fs, dirp, LFS_DIRBLKSIZ);
+	lfs_dir_settype(fs, dirp, LFS_DT_UNKNOWN);
+	lfs_dir_setnamlen(fs, dirp, 0);
+	lfs_copydirname(fs, lfs_dir_nameptr(fs, dirp), "", 0,
+			LFS_DIRBLKSIZ);
+}
+
+/*
  * Attempt to expand the size of a directory
  */
 static int
@@ -620,13 +638,13 @@ expanddir(struct uvnode *vp, union lfs_d
 	for (cp = &bp->b_data[LFS_DIRBLKSIZ];
 	    cp < &bp->b_data[lfs_sb_getbsize(fs)];
 	    cp += LFS_DIRBLKSIZ)
-		memcpy(cp, &emptydir, sizeof emptydir);
+		zerodirblk(cp);
 	VOP_BWRITE(bp);
 	bread(vp, lfs_dino_getdb(fs, dp, lastbn + 1),
 	    (long) lfs_dblksize(fs, dp, lastbn + 1), 0, &bp);
 	if (bp->b_flags & B_ERROR)
 		goto bad;
-	memcpy(bp->b_data, &emptydir, sizeof emptydir);
+	zerodirblk(bp->b_data);
 	pwarn("NO SPACE LEFT IN %s", name);
 	if (preen)
 		printf(" (EXPANDED)\n");
@@ -655,13 +673,10 @@ allocdir(ino_t parent, ino_t request, in
 	char *cp;
 	union lfs_dinode *dp;
 	struct ubuf *bp;
-	struct lfs_dirtemplate *dirp;
+	struct lfs_dirheader *dirp;
 	struct uvnode *vp;
 
 	ino = allocino(request, LFS_IFDIR | mode);
-	dirp = &dirhead;
-	dirp->dot_ino = ino;
-	dirp->dotdot_ino = parent;
 	vp = vget(fs, ino);
 	dp = VTOD(vp);
 	bread(vp, lfs_dino_getdb(fs, dp, 0), lfs_sb_getfsize(fs), 0, &bp);
@@ -670,11 +685,27 @@ allocdir(ino_t parent, ino_t request, in
 		freeino(ino);
 		return (0);
 	}
-	memcpy(bp->b_data, dirp, sizeof(struct lfs_dirtemplate));
+	dirp = (struct lfs_dirheader *)bp->b_data;
+	/* . */
+	lfs_dir_setino(fs, dirp, ino);
+	lfs_dir_setreclen(fs, dirp, LFS_DIRECTSIZ(1));
+	lfs_dir_settype(fs, dirp, LFS_DT_DIR);
+	lfs_dir_setnamlen(fs, dirp, 1);
+	lfs_copydirname(fs, lfs_dir_nameptr(fs, dirp), ".", 1,
+			LFS_DIRECTSIZ(1));
+	/* .. */
+	dirp = LFS_NEXTDIR(fs, dirp);
+	lfs_dir_setino(fs, dirp, parent);
+	lfs_dir_setreclen(fs, dirp, LFS_DIRBLKSIZ - LFS_DIRECTSIZ(1));
+	lfs_dir_settype(fs, dirp, LFS_DT_DIR);
+	lfs_dir_setnamlen(fs, dirp, 2);
+	lfs_copydirname(fs, lfs_dir_nameptr(fs, dirp), "..", 2,
+			LFS_DIRBLKSIZ - LFS_DIRECTSIZ(1));
 	for (cp = &bp->b_data[LFS_DIRBLKSIZ];
 	    cp < &bp->b_data[lfs_sb_getfsize(fs)];
-	    cp += LFS_DIRBLKSIZ)
-		memcpy(cp, &emptydir, sizeof emptydir);
+	    cp += LFS_DIRBLKSIZ) {
+		zerodirblk(cp);
+	}
 	VOP_BWRITE(bp);
 	lfs_dino_setnlink(fs, dp, 2);
 	inodirty(VTOI(vp));

Index: src/sys/ufs/lfs/lfs.h
diff -u src/sys/ufs/lfs/lfs.h:1.189 src/sys/ufs/lfs/lfs.h:1.190
--- src/sys/ufs/lfs/lfs.h:1.189	Tue Sep 15 15:02:40 2015
+++ src/sys/ufs/lfs/lfs.h	Sun Sep 20 04:51:43 2015
@@ -1,4 +1,4 @@
-/*	$NetBSD: lfs.h,v 1.189 2015/09/15 15:02:40 dholland Exp $	*/
+/*	$NetBSD: lfs.h,v 1.190 2015/09/20 04:51:43 dholland Exp $	*/
 
 /*  from NetBSD: dinode.h,v 1.22 2013/01/22 09:39:18 dholland Exp  */
 /*  from NetBSD: dir.h,v 1.21 2009/07/22 04:49:19 dholland Exp  */
@@ -356,22 +356,16 @@ struct lfs_dirheader {
 };
 
 /*
- * Template for manipulating directories.  Should use struct lfs_direct's,
- * but the name field is LFS_MAXNAMLEN - 1, and this just won't do.
+ * Template for manipulating directories.
  */
 struct lfs_dirtemplate {
-	u_int32_t	dot_ino;
-	int16_t		dot_reclen;
-	u_int8_t	dot_type;
-	u_int8_t	dot_namlen;
-	char		dot_name[4];	/* must be multiple of 4 */
-	u_int32_t	dotdot_ino;
-	int16_t		dotdot_reclen;
-	u_int8_t	dotdot_type;
-	u_int8_t	dotdot_namlen;
-	char		dotdot_name[4];	/* ditto */
+	struct lfs_dirheader	dot_header;
+	char			dot_name[4];	/* must be multiple of 4 */
+	struct lfs_dirheader	dotdot_header;
+	char			dotdot_name[4];	/* ditto */
 };
 
+#if 0
 /*
  * This is the old format of directories, sans type element.
  */
@@ -385,6 +379,7 @@ struct lfs_odirtemplate {
 	u_int16_t	dotdot_namlen;
 	char		dotdot_name[4];	/* ditto */
 };
+#endif
 
 /*
  * Inodes

Index: src/sys/ufs/lfs/lfs_accessors.h
diff -u src/sys/ufs/lfs/lfs_accessors.h:1.28 src/sys/ufs/lfs/lfs_accessors.h:1.29
--- src/sys/ufs/lfs/lfs_accessors.h:1.28	Sun Sep 20 04:50:58 2015
+++ src/sys/ufs/lfs/lfs_accessors.h	Sun Sep 20 04:51:43 2015
@@ -1,4 +1,4 @@
-/*	$NetBSD: lfs_accessors.h,v 1.28 2015/09/20 04:50:58 dholland Exp $	*/
+/*	$NetBSD: lfs_accessors.h,v 1.29 2015/09/20 04:51:43 dholland Exp $	*/
 
 /*  from NetBSD: lfs.h,v 1.165 2015/07/24 06:59:32 dholland Exp  */
 /*  from NetBSD: dinode.h,v 1.22 2013/01/22 09:39:18 dholland Exp  */
@@ -330,78 +330,6 @@ lfs_copydirname(STRUCT_LFS *fs, char *de
 }
 
 /*
- * These are called "dirt" because they ought to be cleaned up.
- */
-
-static __unused inline uint8_t
-lfs_dirt_getdottype(const STRUCT_LFS *fs, const struct lfs_dirtemplate *dp)
-{
-	if (fs->lfs_hasolddirfmt) {
-		return LFS_DT_UNKNOWN;
-	}
-	return dp->dot_type;
-}
-
-static __unused inline uint8_t
-lfs_dirt_getdotnamlen(const STRUCT_LFS *fs, const struct lfs_dirtemplate *dp)
-{
-	if (fs->lfs_hasolddirfmt && LFS_LITTLE_ENDIAN_ONDISK(fs)) {
-		/* low-order byte of old 16-bit namlen field */
-		return dp->dot_type;
-	}
-	return dp->dot_namlen;
-}
-
-static __unused inline uint8_t
-lfs_dirt_getdotdottype(const STRUCT_LFS *fs, const struct lfs_dirtemplate *dp)
-{
-	if (fs->lfs_hasolddirfmt) {
-		return LFS_DT_UNKNOWN;
-	}
-	return dp->dotdot_type;
-}
-
-static __unused inline uint8_t
-lfs_dirt_getdotdotnamlen(const STRUCT_LFS *fs, const struct lfs_dirtemplate *dp)
-{
-	if (fs->lfs_hasolddirfmt && LFS_LITTLE_ENDIAN_ONDISK(fs)) {
-		/* low-order byte of old 16-bit namlen field */
-		return dp->dotdot_type;
-	}
-	return dp->dotdot_namlen;
-}
-
-static __unused inline void
-lfs_dirt_settypes(const STRUCT_LFS *fs, struct lfs_dirtemplate *dtp,
-    unsigned dt1, unsigned dt2)
-{
-	if (fs->lfs_hasolddirfmt) {
-		/* do nothing */
-		return;
-	}
-	dtp->dot_type = dt1;
-	dtp->dotdot_type = dt2;
-}
-
-static __unused inline void
-lfs_dirt_setnamlens(const STRUCT_LFS *fs, struct lfs_dirtemplate *dtp,
-    unsigned len1, unsigned len2)
-{
-	if (fs->lfs_hasolddirfmt && LFS_LITTLE_ENDIAN_ONDISK(fs)) {
-		/* low-order bytes of old 16-bit namlen field */
-		dtp->dot_type = len1;
-		dtp->dotdot_type = len2;
-		/* clear the high-order bytes */
-		dtp->dot_namlen = 0;
-		dtp->dotdot_namlen = 0;
-		return;
-	}
-	dtp->dot_namlen = len1;
-	dtp->dotdot_namlen = len2;
-}
-
-
-/*
  * dinodes
  */
 

Index: src/sys/ufs/lfs/lfs_rename.c
diff -u src/sys/ufs/lfs/lfs_rename.c:1.13 src/sys/ufs/lfs/lfs_rename.c:1.14
--- src/sys/ufs/lfs/lfs_rename.c:1.13	Tue Sep 15 15:02:25 2015
+++ src/sys/ufs/lfs/lfs_rename.c	Sun Sep 20 04:51:43 2015
@@ -1,4 +1,4 @@
-/*	$NetBSD: lfs_rename.c,v 1.13 2015/09/15 15:02:25 dholland Exp $	*/
+/*	$NetBSD: lfs_rename.c,v 1.14 2015/09/20 04:51:43 dholland Exp $	*/
 /*  from NetBSD: ufs_rename.c,v 1.6 2013/01/22 09:39:18 dholland Exp  */
 
 /*-
@@ -89,7 +89,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: lfs_rename.c,v 1.13 2015/09/15 15:02:25 dholland Exp $");
+__KERNEL_RCSID(0, "$NetBSD: lfs_rename.c,v 1.14 2015/09/20 04:51:43 dholland Exp $");
 
 #include <sys/param.h>
 #include <sys/systm.h>
@@ -128,16 +128,6 @@ __KERNEL_RCSID(0, "$NetBSD: lfs_rename.c
 #include <ufs/lfs/lfs_extern.h>
 
 /*
- * A virgin directory (no blushing please).
- *
- * XXX Copypasta from ulfs_vnops.c.  Kill!
- */
-static const struct lfs_dirtemplate mastertemplate = {
-	0,	12,			LFS_DT_DIR,	1,	".",
-	0,	LFS_DIRBLKSIZ - 12,	LFS_DT_DIR,	2,	".."
-};
-
-/*
  * ulfs_gro_directory_empty_p: Return true if the directory vp is
  * empty.  dvp is its parent.
  *
@@ -584,33 +574,13 @@ ulfs_rmdired_p(struct vnode *vp)
 }
 
 /*
- * ulfs_dirbuf_dotdot_namlen: Return the namlen of the directory buffer
- * dirbuf that came from the directory vp.  Swap byte order if
- * necessary.
- */
-static int			/* XXX int?  uint8_t?  */
-ulfs_dirbuf_dotdot_namlen(const struct lfs_dirtemplate *dirbuf,
-    const struct vnode *vp)
-{
-	struct lfs *fs;
-
-	KASSERT(dirbuf != NULL);
-	KASSERT(vp != NULL);
-	KASSERT(VTOI(vp) != NULL);
-	KASSERT(VTOI(vp)->i_ump != NULL);
-	KASSERT(VTOI(vp)->i_lfs != NULL);
-	fs = VTOI(vp)->i_lfs;
-
-	return lfs_dirt_getdotdotnamlen(fs, dirbuf);
-}
-
-/*
  * ulfs_read_dotdot: Store in *ino_ret the inode number of the parent
  * of the directory vp.
  */
 static int
 ulfs_read_dotdot(struct vnode *vp, kauth_cred_t cred, ino_t *ino_ret)
 {
+	struct lfs *fs;
 	struct lfs_dirtemplate dirbuf;
 	int error;
 
@@ -618,19 +588,22 @@ ulfs_read_dotdot(struct vnode *vp, kauth
 	KASSERT(ino_ret != NULL);
 	KASSERT(vp->v_type == VDIR);
 
+	KASSERT(VTOI(vp) != NULL);
+	KASSERT(VTOI(vp)->i_lfs != NULL);
+	fs = VTOI(vp)->i_lfs;
+
 	error = ulfs_bufio(UIO_READ, vp, &dirbuf, sizeof dirbuf, (off_t)0,
 	    IO_NODELOCKED, cred, NULL, NULL);
 	if (error)
 		return error;
 
-	if (ulfs_dirbuf_dotdot_namlen(&dirbuf, vp) != 2 ||
+	if (lfs_dir_getnamlen(fs, &dirbuf.dotdot_header) != 2 ||
 	    dirbuf.dotdot_name[0] != '.' ||
 	    dirbuf.dotdot_name[1] != '.')
 		/* XXX Panic?  Print warning?  */
 		return ENOTDIR;
 
-	*ino_ret = ulfs_rw32(dirbuf.dotdot_ino,
-	    ULFS_IPNEEDSWAP(VTOI(vp)));
+	*ino_ret = lfs_dir_getino(fs, &dirbuf.dotdot_header);
 	return 0;
 }
 
@@ -962,7 +935,15 @@ ulfs_gro_rename(struct mount *mp, kauth_
 	 * the link count of fvp or the link count of tdvp.  Go figure.
 	 */
 	if (directory_p && reparent_p) {
-		error = ulfs_dirrewrite(VTOI(fvp), mastertemplate.dot_reclen,
+		off_t position;
+
+		/*
+		 * The .. entry goes immediately after the . entry, so
+		 * the position is the record length of the . entry,
+		 * namely LFS_DIRECTSIZ(1).
+		 */
+		position = LFS_DIRECTSIZ(1);
+		error = ulfs_dirrewrite(VTOI(fvp), position,
 		    VTOI(fdvp), VTOI(tdvp)->i_number, LFS_DT_DIR, 0, IN_CHANGE);
 #if 0		/* XXX This branch was not in ulfs_rename! */
 		if (error)

Index: src/sys/ufs/lfs/lfs_vnops.c
diff -u src/sys/ufs/lfs/lfs_vnops.c:1.290 src/sys/ufs/lfs/lfs_vnops.c:1.291
--- src/sys/ufs/lfs/lfs_vnops.c:1.290	Tue Sep 15 15:00:32 2015
+++ src/sys/ufs/lfs/lfs_vnops.c	Sun Sep 20 04:51:43 2015
@@ -1,4 +1,4 @@
-/*	$NetBSD: lfs_vnops.c,v 1.290 2015/09/15 15:00:32 dholland Exp $	*/
+/*	$NetBSD: lfs_vnops.c,v 1.291 2015/09/20 04:51:43 dholland Exp $	*/
 
 /*-
  * Copyright (c) 1999, 2000, 2001, 2002, 2003 The NetBSD Foundation, Inc.
@@ -125,7 +125,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: lfs_vnops.c,v 1.290 2015/09/15 15:00:32 dholland Exp $");
+__KERNEL_RCSID(0, "$NetBSD: lfs_vnops.c,v 1.291 2015/09/20 04:51:43 dholland Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_compat_netbsd.h"
@@ -178,14 +178,6 @@ static int lfs_setextattr(void *v);
 static int lfs_listextattr(void *v);
 static int lfs_deleteextattr(void *v);
 
-/*
- * A virgin directory (no blushing please).
- */
-static const struct lfs_dirtemplate mastertemplate = {
-	0,	12,			LFS_DT_DIR,	1,	".",
-	0,	LFS_DIRBLKSIZ - 12,	LFS_DT_DIR,	2,	".."
-};
-
 /* Global vfs data structures for lfs. */
 int (**lfs_vnodeop_p)(void *);
 const struct vnodeopv_entry_desc lfs_vnodeop_entries[] = {
@@ -857,7 +849,7 @@ lfs_mkdir(void *v)
 	struct vattr *vap;
 	struct ulfs_lookup_results *ulr;
 	struct buf *bp;
-	struct lfs_dirtemplate dirtemplate;
+	struct lfs_dirheader *dirp;
 	int dirblksiz;
 	int error;
 
@@ -884,6 +876,8 @@ lfs_mkdir(void *v)
 		return EROFS;
 	}
 	dirblksiz = fs->um_dirblksiz;
+	/* XXX dholland 20150911 I believe this to be true, but... */
+	//KASSERT(dirblksiz == LFS_DIRBLKSIZ);
 
 	error = lfs_set_dirop(dvp, NULL);
 	if (error)
@@ -933,18 +927,10 @@ lfs_mkdir(void *v)
 		goto bad;
 
 	/*
-	 * Initialize directory with "." and ".." from static template.
+	 * Initialize directory with "." and "..". This used to use a
+	 * static template but that adds moving parts for very little
+	 * benefit.
 	 */
-	dirtemplate = mastertemplate;
-	dirtemplate.dotdot_reclen = dirblksiz - dirtemplate.dot_reclen;
-	dirtemplate.dot_ino = ulfs_rw32(ip->i_number, ULFS_MPNEEDSWAP(fs));
-	dirtemplate.dotdot_ino = ulfs_rw32(dp->i_number, ULFS_MPNEEDSWAP(fs));
-	dirtemplate.dot_reclen = ulfs_rw16(dirtemplate.dot_reclen,
-	    ULFS_MPNEEDSWAP(fs));
-	dirtemplate.dotdot_reclen = ulfs_rw16(dirtemplate.dotdot_reclen,
-	    ULFS_MPNEEDSWAP(fs));
-	lfs_dirt_settypes(fs, &dirtemplate, LFS_DT_DIR, LFS_DT_DIR);
-	lfs_dirt_setnamlens(fs, &dirtemplate, 1, 2);
 	if ((error = lfs_balloc(tvp, (off_t)0, dirblksiz, cnp->cn_cred,
 	    B_CLRBUF, &bp)) != 0)
 		goto bad;
@@ -952,7 +938,23 @@ lfs_mkdir(void *v)
 	DIP_ASSIGN(ip, size, dirblksiz);
 	ip->i_flag |= IN_ACCESS | IN_CHANGE | IN_UPDATE;
 	uvm_vnp_setsize(tvp, ip->i_size);
-	memcpy((void *)bp->b_data, (void *)&dirtemplate, sizeof dirtemplate);
+	dirp = bp->b_data;
+
+	/* . */
+	lfs_dir_setino(fs, dirp, ip->i_number);
+	lfs_dir_setreclen(fs, dirp, LFS_DIRECTSIZ(1));
+	lfs_dir_settype(fs, dirp, LFS_DT_DIR);
+	lfs_dir_setnamlen(fs, dirp, 1);
+	lfs_copydirname(fs, lfs_dir_nameptr(fs, dirp), ".", 1,
+			LFS_DIRECTSIZ(1));
+	dirp = LFS_NEXTDIR(fs, dirp);
+	/* .. */
+	lfs_dir_setino(fs, dirp, dp->i_number);
+	lfs_dir_setreclen(fs, dirp, dirblksiz - LFS_DIRECTSIZ(1));
+	lfs_dir_settype(fs, dirp, LFS_DT_DIR);
+	lfs_dir_setnamlen(fs, dirp, 2);
+	lfs_copydirname(fs, lfs_dir_nameptr(fs, dirp), "..", 2,
+			dirblksiz - LFS_DIRECTSIZ(1));
 
 	/*
 	 * Directory set up; now install its entry in the parent directory.

Reply via email to