Module Name:    src
Committed By:   dholland
Date:           Tue Sep 15 14:59:58 UTC 2015

Modified Files:
        src/sbin/newfs_lfs: make_lfs.c
        src/sys/ufs/lfs: lfs.h lfs_accessors.h

Log Message:
Move the header part of struct lfs_direct to its own structure.
(lfs_dirheader)

Take the opportunity to improve the directory generation code in
make_lfs.c. (Everything else was unaffected by virtue of using
accessor functions.)


To generate a diff of this commit:
cvs rdiff -u -r1.50 -r1.51 src/sbin/newfs_lfs/make_lfs.c
cvs rdiff -u -r1.186 -r1.187 src/sys/ufs/lfs/lfs.h
cvs rdiff -u -r1.23 -r1.24 src/sys/ufs/lfs/lfs_accessors.h

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

Modified files:

Index: src/sbin/newfs_lfs/make_lfs.c
diff -u src/sbin/newfs_lfs/make_lfs.c:1.50 src/sbin/newfs_lfs/make_lfs.c:1.51
--- src/sbin/newfs_lfs/make_lfs.c:1.50	Tue Sep 15 14:58:05 2015
+++ src/sbin/newfs_lfs/make_lfs.c	Tue Sep 15 14:59:58 2015
@@ -1,4 +1,4 @@
-/*	$NetBSD: make_lfs.c,v 1.50 2015/09/15 14:58:05 dholland Exp $	*/
+/*	$NetBSD: make_lfs.c,v 1.51 2015/09/15 14:59:58 dholland Exp $	*/
 
 /*-
  * Copyright (c) 2003 The NetBSD Foundation, Inc.
@@ -62,7 +62,7 @@
 #if 0
 static char sccsid[] = "@(#)lfs.c	8.5 (Berkeley) 5/24/95";
 #else
-__RCSID("$NetBSD: make_lfs.c,v 1.50 2015/09/15 14:58:05 dholland Exp $");
+__RCSID("$NetBSD: make_lfs.c,v 1.51 2015/09/15 14:59:58 dholland Exp $");
 #endif
 #endif /* not lint */
 
@@ -242,63 +242,32 @@ static const struct lfs lfs_default;
 
 #define	UMASK	0755
 
-struct lfs_direct lfs_root_dir[] = {
-	{
-		.d_ino = ULFS_ROOTINO,
-		.d_reclen = sizeof(struct lfs_direct),
-		.d_type = LFS_DT_DIR,
-		.d_namlen = 1,
-		.d_name = "."
-	},
-	{
-		.d_ino = ULFS_ROOTINO,
-		.d_reclen = sizeof(struct lfs_direct),
-		.d_type = LFS_DT_DIR,
-		.d_namlen = 2,
-		.d_name = ".."
-	},
-/*
-	{
-		.d_ino = LFS_IFILE_INUM,
-		.d_reclen = sizeof(struct lfs_direct),
-		.d_type = LFS_DT_REG,
-		.d_namlen = 5,
-		.d_name = "ifile"
-	},
-*/
+struct dirproto {
+	ino_t dp_ino;
+	const char *dp_name;
+	unsigned dp_type;
+};
+
+static const struct dirproto lfs_root_dir[] = {
+	{ ULFS_ROOTINO, ".", LFS_DT_DIR },
+	{ ULFS_ROOTINO, "..", LFS_DT_DIR },
+	/*{ LFS_IFILE_INUM, "ifile", LFS_DT_REG },*/
 #ifdef MAKE_LF_DIR
-	{
-		.d_ino = LOSTFOUNDINO,
-		.d_reclen = sizeof(struct lfs_direct),
-		.d_type = LFS_DT_DIR,
-		.d_namlen = 10,
-		.d_name = "lost+found"
-	},
+	{ LOSTFOUNDINO, "lost+found", LFS_DT_DIR },
 #endif
 };
 
 #ifdef MAKE_LF_DIR
-struct lfs_direct lfs_lf_dir[] = {
-        {
-		.d_ino = LOSTFOUNDINO,
-		.d_reclen = sizeof(struct lfs_direct),
-		.d_type = LFS_DT_DIR,
-		.d_reclen = 1,
-		.d_name = "."
-	},
-        {
-		.d_ino = ULFS_ROOTINO,
-		.d_reclen = sizeof(struct lfs_direct),
-		.d_type = LFS_DT_DIR,
-		.d_reclen = 2,
-		.d_name = ".."
-	},
+static const struct dirproto lfs_lf_dir[] = {
+        { LOSTFOUNDINO, ".", LFS_DT_DIR },
+	{ ULFS_ROOTINO, "..", LFS_DT_DIR },
 };
 #endif
 
 void pwarn(const char *, ...);
 static void make_dinode(ino_t, union lfs_dinode *, int, struct lfs *);
-static void make_dir(struct lfs *, void *, struct lfs_direct *, int);
+static void make_dir(struct lfs *, void *,
+		const struct dirproto *, unsigned);
 static uint64_t maxfilesize(int);
 
 /*
@@ -385,23 +354,42 @@ make_dinode(ino_t ino, union lfs_dinode 
  * entries in protodir fit in the first DIRBLKSIZ.  
  */
 static void
-make_dir(struct lfs *fs, void *bufp, struct lfs_direct *protodir, int entries)
+make_dir(struct lfs *fs, void *bufp,
+    const struct dirproto *protodir, unsigned numentries)
 {
-	char *cp;
-	int i, spcleft;
-	unsigned reclen;
-
-	spcleft = LFS_DIRBLKSIZ;
-	for (cp = bufp, i = 0; i < entries - 1; i++) {
-		reclen = LFS_DIRSIZ(fs, &protodir[i]);
-		lfs_dir_setreclen(fs, &protodir[i], reclen);
-		memmove(cp, &protodir[i], lfs_dir_getreclen(fs, &protodir[i]));
-		cp += reclen;
-		if ((spcleft -= reclen) < 0)
+	struct lfs_direct *ep;
+	unsigned spaceleft;
+	unsigned namlen, reclen;
+	unsigned i;
+	char *pad;
+
+	spaceleft = LFS_DIRBLKSIZ;
+	ep = bufp;
+	for (i = 0; i < numentries; i++) {
+		namlen = strlen(protodir[i].dp_name);
+		reclen = LFS_DIRECTSIZ(namlen);
+		if (spaceleft < reclen)
 			fatal("%s: %s", special, "directory too big");
+
+		/* Last entry includes all the free space. */
+		if (i + 1 == numentries) {
+			reclen = spaceleft;
+		}
+		spaceleft -= reclen;
+
+		lfs_dir_setino(fs, ep, protodir[i].dp_ino);
+		lfs_dir_setreclen(fs, ep, reclen);
+		lfs_dir_settype(fs, ep, protodir[i].dp_type);
+		lfs_dir_setnamlen(fs, ep, namlen);
+		memcpy(ep->d_name, protodir[i].dp_name, namlen);
+		pad = ep->d_name + namlen;
+		ep = LFS_NEXTDIR(fs, ep);
+
+		while (pad < (char *)ep) {
+			*pad++ = '\0';
+		}
 	}
-	lfs_dir_setreclen(fs, &protodir[i], spcleft);
-	memmove(cp, &protodir[i], LFS_DIRSIZ(fs, &protodir[i]));
+	assert(spaceleft == 0);
 }
 
 int
@@ -838,8 +826,7 @@ make_lfs(int devfd, uint secsize, struct
 		VTOI(vp)->i_lfs_fragsize[i - 1] =
 			roundup(LFS_DIRBLKSIZ, lfs_sb_getfsize(fs));
 	bread(vp, 0, lfs_sb_getfsize(fs), 0, &bp);
-	make_dir(fs, bp->b_data, lfs_root_dir, 
-		 sizeof(lfs_root_dir) / sizeof(struct lfs_direct));
+	make_dir(fs, bp->b_data, lfs_root_dir, __arraycount(lfs_root_dir));
 	VOP_BWRITE(bp);
 
 #ifdef MAKE_LF_DIR
@@ -858,8 +845,7 @@ make_lfs(int devfd, uint secsize, struct
 		VTOI(vp)->i_lfs_fragsize[i - 1] =
 			roundup(DIRBLKSIZ,fs->lfs_fsize);
 	bread(vp, 0, fs->lfs_fsize, 0, &bp);
-	make_dir(fs, bp->b_data, lfs_lf_dir, 
-		 sizeof(lfs_lf_dir) / sizeof(struct lfs_direct));
+	make_dir(fs, bp->b_data, lfs_lf_dir, __arraycount(lfs_lf_dir));
 	VOP_BWRITE(bp);
 #endif /* MAKE_LF_DIR */
 

Index: src/sys/ufs/lfs/lfs.h
diff -u src/sys/ufs/lfs/lfs.h:1.186 src/sys/ufs/lfs/lfs.h:1.187
--- src/sys/ufs/lfs/lfs.h:1.186	Tue Sep 15 14:58:06 2015
+++ src/sys/ufs/lfs/lfs.h	Tue Sep 15 14:59:58 2015
@@ -1,4 +1,4 @@
-/*	$NetBSD: lfs.h,v 1.186 2015/09/15 14:58:06 dholland Exp $	*/
+/*	$NetBSD: lfs.h,v 1.187 2015/09/15 14:59:58 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  */
@@ -365,11 +365,14 @@
 /*
  * (See notes above)
  */
+struct lfs_dirheader {
+	u_int32_t dh_ino;		/* inode number of entry */
+	u_int16_t dh_reclen;		/* length of this record */
+	u_int8_t  dh_type; 		/* file type, see below */
+	u_int8_t  dh_namlen;		/* length of string in d_name */
+};
 struct lfs_direct {
-	u_int32_t d_ino;		/* inode number of entry */
-	u_int16_t d_reclen;		/* length of this record */
-	u_int8_t  d_type; 		/* file type, see below */
-	u_int8_t  d_namlen;		/* length of string in d_name */
+	struct lfs_dirheader d_header;
 	char	  d_name[LFS_MAXNAMLEN + 1];/* name with length <= LFS_MAXNAMLEN */
 };
 

Index: src/sys/ufs/lfs/lfs_accessors.h
diff -u src/sys/ufs/lfs/lfs_accessors.h:1.23 src/sys/ufs/lfs/lfs_accessors.h:1.24
--- src/sys/ufs/lfs/lfs_accessors.h:1.23	Tue Sep 15 14:58:06 2015
+++ src/sys/ufs/lfs/lfs_accessors.h	Tue Sep 15 14:59:58 2015
@@ -1,4 +1,4 @@
-/*	$NetBSD: lfs_accessors.h,v 1.23 2015/09/15 14:58:06 dholland Exp $	*/
+/*	$NetBSD: lfs_accessors.h,v 1.24 2015/09/15 14:59:58 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  */
@@ -247,13 +247,13 @@
 static __unused inline uint32_t
 lfs_dir_getino(const STRUCT_LFS *fs, const struct lfs_direct *dp)
 {
-	return LFS_SWAP_uint32_t(fs, dp->d_ino);
+	return LFS_SWAP_uint32_t(fs, dp->d_header.dh_ino);
 }
 
 static __unused inline uint16_t
 lfs_dir_getreclen(const STRUCT_LFS *fs, const struct lfs_direct *dp)
 {
-	return LFS_SWAP_uint16_t(fs, dp->d_reclen);
+	return LFS_SWAP_uint16_t(fs, dp->d_header.dh_reclen);
 }
 
 static __unused inline uint8_t
@@ -262,7 +262,7 @@ lfs_dir_gettype(const STRUCT_LFS *fs, co
 	if (fs->lfs_hasolddirfmt) {
 		return LFS_DT_UNKNOWN;
 	}
-	return dp->d_type;
+	return dp->d_header.dh_type;
 }
 
 static __unused inline uint8_t
@@ -270,21 +270,21 @@ lfs_dir_getnamlen(const STRUCT_LFS *fs, 
 {
 	if (fs->lfs_hasolddirfmt && LFS_LITTLE_ENDIAN_ONDISK(fs)) {
 		/* low-order byte of old 16-bit namlen field */
-		return dp->d_type;
+		return dp->d_header.dh_type;
 	}
-	return dp->d_namlen;
+	return dp->d_header.dh_namlen;
 }
 
 static __unused inline void
 lfs_dir_setino(STRUCT_LFS *fs, struct lfs_direct *dp, uint32_t ino)
 {
-	dp->d_ino = LFS_SWAP_uint32_t(fs, ino);
+	dp->d_header.dh_ino = LFS_SWAP_uint32_t(fs, ino);
 }
 
 static __unused inline void
 lfs_dir_setreclen(STRUCT_LFS *fs, struct lfs_direct *dp, uint16_t reclen)
 {
-	dp->d_reclen = LFS_SWAP_uint16_t(fs, reclen);
+	dp->d_header.dh_reclen = LFS_SWAP_uint16_t(fs, reclen);
 }
 
 static __unused inline void
@@ -294,7 +294,7 @@ lfs_dir_settype(const STRUCT_LFS *fs, st
 		/* do nothing */
 		return;
 	}
-	dp->d_type = type;
+	dp->d_header.dh_type = type;
 }
 
 static __unused inline void
@@ -302,9 +302,9 @@ lfs_dir_setnamlen(const STRUCT_LFS *fs, 
 {
 	if (fs->lfs_hasolddirfmt && LFS_LITTLE_ENDIAN_ONDISK(fs)) {
 		/* low-order byte of old 16-bit namlen field */
-		dp->d_type = namlen;
+		dp->d_header.dh_type = namlen;
 	}
-	dp->d_namlen = namlen;
+	dp->d_header.dh_namlen = namlen;
 }
 
 /*

Reply via email to