Wed Jun 6 15:49:02 2018 UTC ()
Pull up following revision(s) (requested by maya in ticket #1615):

	usr.sbin/makefs/ffs.c: revision 1.70

PR/52828: Mark Johnston: makefs UFS2 lazy inode initialization is buggy
makefs(8) emulates UFS2 in performing lazy initialization of inode
blocks when allocating and writing inodes. However, it only ever
initializes one inode block at a time, which may be insufficient.

If so, a later initialization may clobber an inode, resulting in
an inconsistent filesystem.

I committed a minimal fix for the problem to FreeBSD:
https://svnweb.freebsd.org/changeset/base/326912


(martin)
diff -r1.63.6.1 -r1.63.6.2 src/usr.sbin/makefs/ffs.c

cvs diff -r1.63.6.1 -r1.63.6.2 src/usr.sbin/makefs/ffs.c (expand / switch to unified diff)

--- src/usr.sbin/makefs/ffs.c 2015/04/14 05:08:09 1.63.6.1
+++ src/usr.sbin/makefs/ffs.c 2018/06/06 15:49:02 1.63.6.2
@@ -1,14 +1,14 @@ @@ -1,14 +1,14 @@
1/* $NetBSD: ffs.c,v 1.63.6.1 2015/04/14 05:08:09 snj Exp $ */ 1/* $NetBSD: ffs.c,v 1.63.6.2 2018/06/06 15:49:02 martin Exp $ */
2 2
3/* 3/*
4 * Copyright (c) 2001 Wasabi Systems, Inc. 4 * Copyright (c) 2001 Wasabi Systems, Inc.
5 * All rights reserved. 5 * All rights reserved.
6 * 6 *
7 * Written by Luke Mewburn for Wasabi Systems, Inc. 7 * Written by Luke Mewburn for Wasabi Systems, Inc.
8 * 8 *
9 * Redistribution and use in source and binary forms, with or without 9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions 10 * modification, are permitted provided that the following conditions
11 * are met: 11 * are met:
12 * 1. Redistributions of source code must retain the above copyright 12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer. 13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright 14 * 2. Redistributions in binary form must reproduce the above copyright
@@ -61,27 +61,27 @@ @@ -61,27 +61,27 @@
61 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 61 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
62 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 62 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
63 * SUCH DAMAGE. 63 * SUCH DAMAGE.
64 * 64 *
65 * @(#)ffs_alloc.c 8.19 (Berkeley) 7/13/95 65 * @(#)ffs_alloc.c 8.19 (Berkeley) 7/13/95
66 */ 66 */
67 67
68#if HAVE_NBTOOL_CONFIG_H 68#if HAVE_NBTOOL_CONFIG_H
69#include "nbtool_config.h" 69#include "nbtool_config.h"
70#endif 70#endif
71 71
72#include <sys/cdefs.h> 72#include <sys/cdefs.h>
73#if defined(__RCSID) && !defined(__lint) 73#if defined(__RCSID) && !defined(__lint)
74__RCSID("$NetBSD: ffs.c,v 1.63.6.1 2015/04/14 05:08:09 snj Exp $"); 74__RCSID("$NetBSD: ffs.c,v 1.63.6.2 2018/06/06 15:49:02 martin Exp $");
75#endif /* !__lint */ 75#endif /* !__lint */
76 76
77#include <sys/param.h> 77#include <sys/param.h>
78 78
79#if !HAVE_NBTOOL_CONFIG_H 79#if !HAVE_NBTOOL_CONFIG_H
80#include <sys/mount.h> 80#include <sys/mount.h>
81#endif 81#endif
82 82
83#include <assert.h> 83#include <assert.h>
84#include <errno.h> 84#include <errno.h>
85#include <fcntl.h> 85#include <fcntl.h>
86#include <stdarg.h> 86#include <stdarg.h>
87#include <stdio.h> 87#include <stdio.h>
@@ -1092,27 +1092,27 @@ ffs_write_inode(union dinode *dp, uint32 @@ -1092,27 +1092,27 @@ ffs_write_inode(union dinode *dp, uint32
1092 ufs_add32(cgp->cg_cs.cs_nifree, -1, fsopts->needswap); 1092 ufs_add32(cgp->cg_cs.cs_nifree, -1, fsopts->needswap);
1093 fs->fs_cstotal.cs_nifree--; 1093 fs->fs_cstotal.cs_nifree--;
1094 fs->fs_cs(fs, cg).cs_nifree--; 1094 fs->fs_cs(fs, cg).cs_nifree--;
1095 if (S_ISDIR(DIP(dp, mode))) { 1095 if (S_ISDIR(DIP(dp, mode))) {
1096 ufs_add32(cgp->cg_cs.cs_ndir, 1, fsopts->needswap); 1096 ufs_add32(cgp->cg_cs.cs_ndir, 1, fsopts->needswap);
1097 fs->fs_cstotal.cs_ndir++; 1097 fs->fs_cstotal.cs_ndir++;
1098 fs->fs_cs(fs, cg).cs_ndir++;  1098 fs->fs_cs(fs, cg).cs_ndir++;
1099 } 1099 }
1100 1100
1101 /* 1101 /*
1102 * Initialize inode blocks on the fly for UFS2. 1102 * Initialize inode blocks on the fly for UFS2.
1103 */ 1103 */
1104 initediblk = ufs_rw32(cgp->cg_initediblk, fsopts->needswap); 1104 initediblk = ufs_rw32(cgp->cg_initediblk, fsopts->needswap);
1105 if (ffs_opts->version == 2 && 1105 while (ffs_opts->version == 2 &&
1106 (uint32_t)(cgino + FFS_INOPB(fs)) > initediblk && 1106 (uint32_t)(cgino + FFS_INOPB(fs)) > initediblk &&
1107 initediblk < ufs_rw32(cgp->cg_niblk, fsopts->needswap)) { 1107 initediblk < ufs_rw32(cgp->cg_niblk, fsopts->needswap)) {
1108 memset(buf, 0, fs->fs_bsize); 1108 memset(buf, 0, fs->fs_bsize);
1109 dip = (struct ufs2_dinode *)buf; 1109 dip = (struct ufs2_dinode *)buf;
1110 srandom(time(NULL)); 1110 srandom(time(NULL));
1111 for (i = 0; i < FFS_INOPB(fs); i++) { 1111 for (i = 0; i < FFS_INOPB(fs); i++) {
1112 dip->di_gen = random() / 2 + 1; 1112 dip->di_gen = random() / 2 + 1;
1113 dip++; 1113 dip++;
1114 } 1114 }
1115 ffs_wtfs(FFS_FSBTODB(fs, ino_to_fsba(fs, 1115 ffs_wtfs(FFS_FSBTODB(fs, ino_to_fsba(fs,
1116 cg * fs->fs_ipg + initediblk)), 1116 cg * fs->fs_ipg + initediblk)),
1117 fs->fs_bsize, buf, fsopts); 1117 fs->fs_bsize, buf, fsopts);
1118 initediblk += FFS_INOPB(fs); 1118 initediblk += FFS_INOPB(fs);