Various minor LFS improvements:

* Extend the lfs library from fsck_lfs(8) so that it can be used with a
  not-yet-existent LFS.  Make newfs_lfs(8) use this library, so it can
  create LFSs whose Ifile is larger than one segment.
* Make newfs_lfs(8) use strsuftoi64() for its arguments, a la newfs(8).
* Make fsck_lfs(8) respect the "file system is clean" flag.
* Don't let fsck_lfs(8) think it has dirty blocks when invoked with the
  -n flag.
This commit is contained in:
perseant 2005-02-26 05:45:54 +00:00
parent 6e52bfc264
commit 5d2f3e4908
16 changed files with 1074 additions and 1162 deletions

View File

@ -1,4 +1,4 @@
# $NetBSD: Makefile,v 1.3 2003/02/03 07:45:05 perseant Exp $
# $NetBSD: Makefile,v 1.4 2005/02/26 05:45:54 perseant Exp $
NOMAN= # defined
@ -35,6 +35,7 @@ test_rename_hardlink:
cd ${TMPMP} && : > foo && ln foo bar && mv foo bar
sync
rm -f bar
${MAKE} TMPMP=${TMPMP} VND=${VND} MPART=${MPART} fsck
# Trivial newfs & fsck test
test_mkfs_mount:
@ -45,11 +46,12 @@ test_mkfs_mount:
#
fsck:
umount ${TMPMP}
fsck_lfs -n ${CVND}${MPART}
fsck_lfs -f -n ${CVND}${MPART}
mount ${BVND}${MPART} ${TMPMP}
clean:
-umount ${TMPMP}
sleep 2
vnconfig -u ${BVND}${RPART}
rmdir ${TMPMP}
rm -f ${TMPIM}

View File

@ -8,7 +8,7 @@ BVND=/dev/${VND}
CVND=/dev/r${VND}
if [ "x$NEWFS_LFS_FLAGS" = "x" ]
then
NEWFS_LFS_FLAGS="-B 131072 -b 4096 -f 512 -M 2"
NEWFS_LFS_FLAGS="-B 131072 -b 4096 -f 4096 -M 6"
fi
echo "*** Creating a dummy directory tree at ${TMPMP} mounted on ${TMPIM}."
@ -16,7 +16,9 @@ dd if=/dev/zero of=${TMPIM} count=5860
vnconfig -v ${BVND}${RPART} ${TMPIM}
disklabel -f ${SRCDIR}/disktab -rw ${VND} floppy288
newfs_lfs ${NEWFS_LFS_FLAGS} ${CVND}${MPART}
echo "*** Checking that fs made by newfs_lfs could be understood by fsck_lfs"
fsck_lfs -n ${CVND}${MPART} || exit 1
fsck_lfs -f -n ${CVND}${MPART} || exit 1
mkdir ${TMPMP}
mount -t lfs ${BVND}${MPART} ${TMPMP}
mount -t lfs -o-N1 ${BVND}${MPART} ${TMPMP}

View File

@ -1,31 +1,31 @@
#!/bin/sh
#
# Create a bunch of small files at once, then delete most of them.
# This forces live blocks in nearly empty segments.
# Create a bunch of small files at once, then delete half of them.
# The file size is less than the segment size.
# This forces live blocks in half-empty segments.
# If the filesystem is small enough, the cleaner will have to run in
# order for this to complete.
#
# Argument is directory in which to run.
#
for i in 0 1 2 3 4 5
echo -n "making small files: "
for i in 0 1 2 3 4 5 6 7 8 9 a b c d e f
do
echo -n "$i "
for j in 0 1
do
# Quarter megabyte files, two at a time
# for a maximum of six at once
# (enough to force cleaning on a 2.88 disk)
dd if=/dev/zero of=$1/f$i$j bs=512 count=512 &
dd if=/dev/zero of=$1/f$i$j bs=65536 count=1 >/dev/null 2>&1
done
wait
sync
rm -f $1/f*1
df -k $1
ls -l $1
done
echo "done."
echo "Sleeping for 10 seconds...."
sleep 10
echo "Trying with another half meg"
dd if=/dev/zero of=$1/f61 bs=512 count=1024
echo "Trying with another half meg; expect 'no space on device'"
dd if=/dev/zero of=$1/f61 bs=512 count=1024 >/dev/null || true
sync
df -h $1

View File

@ -1,4 +1,4 @@
/* $NetBSD: bufcache.c,v 1.2 2003/03/31 19:56:59 perseant Exp $ */
/* $NetBSD: bufcache.c,v 1.3 2005/02/26 05:45:54 perseant Exp $ */
/*-
* Copyright (c) 2003 The NetBSD Foundation, Inc.
* All rights reserved.
@ -174,6 +174,9 @@ struct ubuf *
getblk(struct uvnode * vp, daddr_t lbn, int size)
{
struct ubuf *bp;
#ifdef DEBUG
static int warned;
#endif
/*
* First check the buffer cache lists.
@ -199,7 +202,7 @@ getblk(struct uvnode * vp, daddr_t lbn, int size)
if (bp == NULL) {
bp = TAILQ_FIRST(&bufqueues[BQ_LRU]);
if (bp)
TAILQ_REMOVE(&bufqueues[BQ_AGE], bp,
TAILQ_REMOVE(&bufqueues[BQ_LRU], bp,
b_freelist);
}
if (bp) {
@ -209,8 +212,11 @@ getblk(struct uvnode * vp, daddr_t lbn, int size)
}
#ifdef DEBUG
else {
warnx("no free buffers, allocating more than %d",
maxbufs);
if (!warned)
warnx("allocating more than %d buffers",
maxbufs);
++warned;
break;
}
#endif
}
@ -315,3 +321,20 @@ reassignbuf(struct ubuf * bp, struct uvnode * vp)
LIST_INSERT_HEAD(&vp->v_cleanblkhd, bp, b_vnbufs);
}
}
#ifdef DEBUG
void
dump_free_lists(void)
{
struct ubuf *bp;
int i;
for (i = 0; i <= BQ_LOCKED; i++) {
printf("==> free list %d:\n", i);
TAILQ_FOREACH(bp, &bufqueues[i], b_freelist) {
printf("vp %p lbn %" PRId64 " flags %lx\n",
bp->b_vp, bp->b_lblkno, bp->b_flags);
}
}
}
#endif

View File

@ -1,4 +1,4 @@
/* $NetBSD: bufcache.h,v 1.2 2003/08/07 10:04:22 agc Exp $ */
/* $NetBSD: bufcache.h,v 1.3 2005/02/26 05:45:54 perseant Exp $ */
/*-
* Copyright (c) 1999, 2000 The NetBSD Foundation, Inc.
@ -119,3 +119,4 @@ void bwrite(struct ubuf *);
void brelse(struct ubuf *);
int bread(struct uvnode *, daddr_t, int, struct ucred *, struct ubuf **);
void reassignbuf(struct ubuf *, struct uvnode *);
void dump_free_lists(void);

View File

@ -1,4 +1,4 @@
/* $NetBSD: lfs.c,v 1.7 2003/08/07 10:04:23 agc Exp $ */
/* $NetBSD: lfs.c,v 1.8 2005/02/26 05:45:54 perseant Exp $ */
/*-
* Copyright (c) 2003 The NetBSD Foundation, Inc.
* All rights reserved.
@ -101,6 +101,7 @@
extern u_int32_t cksum(void *, size_t);
extern u_int32_t lfs_sb_cksum(struct dlfs *);
extern void pwarn(const char *, ...);
extern struct uvnodelst vnodelist;
extern struct uvnodelst getvnodelist;
@ -372,17 +373,19 @@ lfs_raw_vget(struct lfs * fs, ino_t ino, int fd, ufs_daddr_t daddr)
ip->i_flag = 0;
/* Load inode block and find inode */
bread(fs->lfs_unlockvp, fsbtodb(fs, daddr), fs->lfs_ibsize, NULL, &bp);
bp->b_flags |= B_AGE;
dip = lfs_ifind(fs, ino, bp);
if (dip == NULL) {
if (daddr > 0) {
bread(fs->lfs_unlockvp, fsbtodb(fs, daddr), fs->lfs_ibsize, NULL, &bp);
bp->b_flags |= B_AGE;
dip = lfs_ifind(fs, ino, bp);
if (dip == NULL) {
brelse(bp);
free(ip);
free(vp);
return NULL;
}
memcpy(ip->i_din.ffs1_din, dip, sizeof(*dip));
brelse(bp);
free(ip);
free(vp);
return NULL;
}
memcpy(ip->i_din.ffs1_din, dip, sizeof(*dip));
brelse(bp);
ip->i_number = ino;
/* ip->i_devvp = fs->lfs_unlockvp; */
ip->i_lfs = fs;
@ -449,7 +452,7 @@ check_sb(struct lfs *fs)
/* Initialize LFS library; load superblocks and choose which to use. */
struct lfs *
lfs_init(int devfd, daddr_t sblkno, daddr_t idaddr, int debug)
lfs_init(int devfd, daddr_t sblkno, daddr_t idaddr, int dummy_read, int debug)
{
struct uvnode *devvp;
struct ubuf *bp;
@ -470,55 +473,64 @@ lfs_init(int devfd, daddr_t sblkno, daddr_t idaddr, int debug)
LIST_INIT(&devvp->v_dirtyblkhd);
tryalt = 0;
if (sblkno == 0) {
sblkno = btodb(LFS_LABELPAD);
tryalt = 1;
} else if (debug) {
printf("No -b flag given, not attempting to verify checkpoint\n");
}
error = bread(devvp, sblkno, LFS_SBPAD, NOCRED, &bp);
fs = (struct lfs *) malloc(sizeof(*fs));
memset(fs, 0, sizeof(*fs));
fs->lfs_dlfs = *((struct dlfs *) bp->b_data);
fs->lfs_unlockvp = devvp;
bp->b_flags |= B_INVAL;
brelse(bp);
if (tryalt) {
error = bread(devvp, fsbtodb(fs, fs->lfs_sboffs[1]),
LFS_SBPAD, NOCRED, &bp);
altfs = (struct lfs *) malloc(sizeof(*altfs));
memset(altfs, 0, sizeof(*altfs));
altfs->lfs_dlfs = *((struct dlfs *) bp->b_data);
altfs->lfs_unlockvp = devvp;
if (dummy_read) {
if (sblkno == 0)
sblkno = btodb(LFS_LABELPAD);
fs = (struct lfs *) malloc(sizeof(*fs));
memset(fs, 0, sizeof(*fs));
fs->lfs_unlockvp = devvp;
} else {
if (sblkno == 0) {
sblkno = btodb(LFS_LABELPAD);
tryalt = 1;
} else if (debug) {
printf("No -b flag given, not attempting to verify checkpoint\n");
}
error = bread(devvp, sblkno, LFS_SBPAD, NOCRED, &bp);
fs = (struct lfs *) malloc(sizeof(*fs));
memset(fs, 0, sizeof(*fs));
fs->lfs_dlfs = *((struct dlfs *) bp->b_data);
fs->lfs_unlockvp = devvp;
bp->b_flags |= B_INVAL;
brelse(bp);
if (check_sb(fs)) {
if (debug)
printf("Primary superblock is no good, using first alternate\n");
free(fs);
fs = altfs;
} else {
/* If both superblocks check out, try verification */
if (check_sb(altfs)) {
if (tryalt) {
error = bread(devvp, fsbtodb(fs, fs->lfs_sboffs[1]),
LFS_SBPAD, NOCRED, &bp);
altfs = (struct lfs *) malloc(sizeof(*altfs));
memset(altfs, 0, sizeof(*altfs));
altfs->lfs_dlfs = *((struct dlfs *) bp->b_data);
altfs->lfs_unlockvp = devvp;
bp->b_flags |= B_INVAL;
brelse(bp);
if (check_sb(fs) || fs->lfs_idaddr <= 0) {
if (debug)
printf("First alternate superblock is no good, using primary\n");
free(altfs);
printf("Primary superblock is no good, using first alternate\n");
free(fs);
fs = altfs;
} else {
if (lfs_verify(fs, altfs, devvp, debug) == fs) {
/* If both superblocks check out, try verification */
if (check_sb(altfs)) {
if (debug)
printf("First alternate superblock is no good, using primary\n");
free(altfs);
} else {
free(fs);
fs = altfs;
if (lfs_verify(fs, altfs, devvp, debug) == fs) {
free(altfs);
} else {
free(fs);
fs = altfs;
}
}
}
}
if (check_sb(fs)) {
free(fs);
return NULL;
}
}
if (check_sb(fs)) {
free(fs);
return NULL;
}
/* Compatibility */
if (fs->lfs_version < 2) {
fs->lfs_sumsize = LFS_V1_SUMMARY_SIZE;
@ -527,13 +539,19 @@ lfs_init(int devfd, daddr_t sblkno, daddr_t idaddr, int debug)
fs->lfs_tstamp = fs->lfs_otstamp;
fs->lfs_fsbtodb = 0;
}
fs->lfs_suflags = (u_int32_t **) malloc(2 * sizeof(u_int32_t *));
fs->lfs_suflags[0] = (u_int32_t *) malloc(fs->lfs_nseg * sizeof(u_int32_t));
fs->lfs_suflags[1] = (u_int32_t *) malloc(fs->lfs_nseg * sizeof(u_int32_t));
if (!dummy_read) {
fs->lfs_suflags = (u_int32_t **) malloc(2 * sizeof(u_int32_t *));
fs->lfs_suflags[0] = (u_int32_t *) malloc(fs->lfs_nseg * sizeof(u_int32_t));
fs->lfs_suflags[1] = (u_int32_t *) malloc(fs->lfs_nseg * sizeof(u_int32_t));
}
if (idaddr == 0)
idaddr = fs->lfs_idaddr;
fs->lfs_ivnode = lfs_raw_vget(fs, fs->lfs_ifile, devvp->v_fd, idaddr);
/* NB: If dummy_read!=0, idaddr==0 here so we get a fake inode. */
fs->lfs_ivnode = lfs_raw_vget(fs,
(dummy_read ? LFS_IFILE_INUM : fs->lfs_ifile), devvp->v_fd,
idaddr);
register_vget((void *)fs, lfs_vget);
@ -655,11 +673,11 @@ lfs_verify(struct lfs *sb0, struct lfs *sb1, struct uvnode *devvp, int debug)
if (debug)
printf("done.\n");
if (daddr == nsb->lfs_offset) {
warnx("** Newer checkpoint verified, recovered %lld seconds of data\n",
pwarn("** Newer checkpoint verified, recovered %lld seconds of data\n",
(long long) nsb->lfs_tstamp - (long long) osb->lfs_tstamp);
sbdirty();
} else {
warnx("** Newer checkpoint invalid, lost %lld seconds of data\n", (long long) nsb->lfs_tstamp - (long long) osb->lfs_tstamp);
pwarn("** Newer checkpoint invalid, lost %lld seconds of data\n", (long long) nsb->lfs_tstamp - (long long) osb->lfs_tstamp);
}
return (daddr == nsb->lfs_offset ? nsb : osb);
}
@ -714,11 +732,11 @@ check_summary(struct lfs *fs, SEGSUM *sp, ufs_daddr_t pseg_addr, int debug,
for (i = 0, j = 0;
i < sp->ss_nfinfo || j < howmany(sp->ss_ninos, INOPB(fs)); i++) {
if (i >= sp->ss_nfinfo && *idp != daddr) {
warnx("Not enough inode blocks in pseg at 0x%" PRIx32
pwarn("Not enough inode blocks in pseg at 0x%" PRIx32
": found %d, wanted %d\n",
pseg_addr, j, howmany(sp->ss_ninos, INOPB(fs)));
if (debug)
warnx("*idp=%x, daddr=%" PRIx32 "\n", *idp,
pwarn("*idp=%x, daddr=%" PRIx32 "\n", *idp,
daddr);
break;
}
@ -748,13 +766,13 @@ check_summary(struct lfs *fs, SEGSUM *sp, ufs_daddr_t pseg_addr, int debug,
}
if (datac != nblocks) {
warnx("Partial segment at 0x%llx expected %d blocks counted %d\n",
pwarn("Partial segment at 0x%llx expected %d blocks counted %d\n",
(long long) pseg_addr, nblocks, datac);
}
ccksum = cksum(datap, nblocks * sizeof(u_int32_t));
/* Check the data checksum */
if (ccksum != sp->ss_datasum) {
warnx("Partial segment at 0x%" PRIx32 " data checksum"
pwarn("Partial segment at 0x%" PRIx32 " data checksum"
" mismatch: given 0x%x, computed 0x%x\n",
pseg_addr, sp->ss_datasum, ccksum);
free(datap);

View File

@ -1,4 +1,4 @@
/* $NetBSD: lfs.h,v 1.2 2003/04/02 10:39:28 fvdl Exp $ */
/* $NetBSD: lfs.h,v 1.3 2005/02/26 05:45:54 perseant Exp $ */
/*-
* Copyright (c) 2003 The NetBSD Foundation, Inc.
* All rights reserved.
@ -67,7 +67,7 @@ int lfs_vop_bwrite(struct ubuf *);
int lfs_vop_bmap(struct uvnode *, daddr_t, daddr_t *);
struct uvnode *lfs_raw_vget(struct lfs *, ino_t, int, ufs_daddr_t);
struct lfs *lfs_init(int, daddr_t, daddr_t, int);
struct lfs *lfs_init(int, daddr_t, daddr_t, int, int);
struct lfs *lfs_verify(struct lfs *, struct lfs *, struct uvnode *, int);
int check_summary(struct lfs *, struct segsum *, ufs_daddr_t, int, struct uvnode *, void (*)(ufs_daddr_t, struct finfo *));
ufs_daddr_t try_verify(struct lfs *, struct uvnode *, ufs_daddr_t, int);

View File

@ -1,4 +1,4 @@
/* $NetBSD: pass0.c,v 1.17 2005/01/19 19:41:59 xtraeme Exp $ */
/* $NetBSD: pass0.c,v 1.18 2005/02/26 05:45:54 perseant Exp $ */
/*-
* Copyright (c) 1999, 2000, 2001, 2002, 2003 The NetBSD Foundation, Inc.
@ -202,5 +202,8 @@ pass0(void)
writeit = 1;
}
}
LFS_SYNC_CLEANERINFO(cip, fs, bp, writeit);
if (writeit)
LFS_SYNC_CLEANERINFO(cip, fs, bp, writeit);
else
brelse(bp);
}

View File

@ -1,4 +1,4 @@
/* $NetBSD: pass3.c,v 1.7 2005/02/06 06:13:47 perry Exp $ */
/* $NetBSD: pass3.c,v 1.8 2005/02/26 05:45:54 perseant Exp $ */
/*
* Copyright (c) 1980, 1986, 1993
@ -46,7 +46,7 @@ pass3(void)
for (inpp = &inpsort[inplast - 1]; inpp >= inpsort; inpp--) {
inp = *inpp;
if (inp->i_number == ROOTINO ||
if (inp->i_number == ROOTINO || inp->i_number == LFS_IFILE_INUM ||
!(inp->i_parent == 0 || statemap[inp->i_number] == DSTATE))
continue;
if (statemap[inp->i_number] == DCLEAR)

View File

@ -1,4 +1,4 @@
/* $NetBSD: segwrite.c,v 1.6 2003/12/24 01:39:27 heas Exp $ */
/* $NetBSD: segwrite.c,v 1.7 2005/02/26 05:45:54 perseant Exp $ */
/*-
* Copyright (c) 2003 The NetBSD Foundation, Inc.
* All rights reserved.
@ -114,6 +114,7 @@ time_t write_time;
extern u_int32_t cksum(void *, size_t);
extern u_int32_t lfs_sb_cksum(struct dlfs *);
extern int preen;
/*
* Logical block number match routines used when traversing the dirty block
@ -174,7 +175,7 @@ lfs_segwrite(struct lfs * fs, int flags)
vp = fs->lfs_ivnode;
fs->lfs_flags &= ~LFS_IFDIRTY;
ip = VTOI(vp);
if (LIST_FIRST(&vp->v_dirtyblkhd) != NULL)
if (LIST_FIRST(&vp->v_dirtyblkhd) != NULL || fs->lfs_idaddr <= 0)
lfs_writefile(fs, sp, vp);
redo = lfs_writeinode(fs, sp, ip);
@ -902,7 +903,7 @@ lfs_seglock(struct lfs * fs, unsigned long flags)
sp = fs->lfs_sp = (struct segment *) malloc(sizeof(*sp));
sp->bpp = (struct ubuf **) malloc(fs->lfs_ssize * sizeof(struct ubuf *));
if (!sp->bpp)
errx(1, "Could not allocate %zu bytes: %s",
errx(!preen, "Could not allocate %zu bytes: %s",
(size_t)(fs->lfs_ssize * sizeof(struct ubuf *)),
strerror(errno));
sp->seg_flags = flags;

View File

@ -1,4 +1,4 @@
/* $NetBSD: setup.c,v 1.18 2004/09/15 03:24:09 minoura Exp $ */
/* $NetBSD: setup.c,v 1.19 2005/02/26 05:45:54 perseant Exp $ */
/*-
* Copyright (c) 2003 The NetBSD Foundation, Inc.
@ -179,7 +179,7 @@ setup(const char *dev)
lfdir = 0;
bufinit();
fs = lfs_init(fsreadfd, bflag, idaddr, debug);
fs = lfs_init(fsreadfd, bflag, idaddr, 0, debug);
if (fs == NULL) {
if (preen)
printf("%s: ", cdevname());
@ -190,7 +190,6 @@ setup(const char *dev)
else
dev_bsize = secsize = DEV_BSIZE;
#if 0
if (fs->lfs_pflags & LFS_PF_CLEAN) {
if (doskipclean) {
pwarn("%sile system is clean; not checking\n",
@ -200,7 +199,6 @@ setup(const char *dev)
if (!preen)
pwarn("** File system is already clean\n");
}
#endif
if (debug) {
printf("idaddr = 0x%lx\n", idaddr ? (unsigned long)idaddr :

View File

@ -1,4 +1,4 @@
/* $NetBSD: utilities.c,v 1.17 2005/02/06 06:13:47 perry Exp $ */
/* $NetBSD: utilities.c,v 1.18 2005/02/26 05:45:54 perseant Exp $ */
/*
* Copyright (c) 1980, 1986, 1993
@ -136,8 +136,9 @@ ckfini(int markclean)
}
}
if ((fs->lfs_flags & LFS_PF_CLEAN) == 0)
if ((fs->lfs_pflags & LFS_PF_CLEAN) == 0) {
fsmodified = 1;
}
fs->lfs_pflags |= LFS_PF_CLEAN;
if (fsmodified && (preen || reply("UPDATE STANDARD SUPERBLOCK"))) {

View File

@ -1,14 +1,17 @@
# $NetBSD: Makefile,v 1.2 2002/08/19 10:17:00 lukem Exp $
# $NetBSD: Makefile,v 1.3 2005/02/26 05:45:54 perseant Exp $
# @(#)Makefile 8.1 (Berkeley) 6/18/93
.include <bsd.own.mk>
PROG= newfs_lfs
SRCS= dkcksum.c lfs.c lfs_cksum.c misc.c newfs.c
SRCS= dkcksum.c make_lfs.c lfs_cksum.c misc.c newfs.c
SRCS+= bufcache.c vnode.c lfs.c segwrite.c
MAN= newfs_lfs.8
.PATH: ${NETBSDSRCDIR}/sys/ufs/lfs ${NETBSDSRCDIR}/sbin/disklabel
.PATH: ${NETBSDSRCDIR}/sys/ufs/lfs ${NETBSDSRCDIR}/sbin/disklabel ${NETBSDSRCDIR}/sbin/fsck_lfs
FSCK_LFS= ${NETBSDSRCDIR}/sbin/fsck_lfs
DPADD= ${LIBUTIL}
LDADD= -lutil
CPPFLAGS+=-I${FSCK_LFS} # -DNDEBUG # -DVERBOSE_BLOCKMAP
.include <bsd.prog.mk>

File diff suppressed because it is too large Load Diff

853
sbin/newfs_lfs/make_lfs.c Normal file
View File

@ -0,0 +1,853 @@
/* $NetBSD: make_lfs.c,v 1.1 2005/02/26 05:45:54 perseant Exp $ */
/*-
* Copyright (c) 2003 The NetBSD Foundation, Inc.
* All rights reserved.
*
* This code is derived from software contributed to The NetBSD Foundation
* by Konrad E. Schroder <perseant@hhhh.org>.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the NetBSD
* Foundation, Inc. and its contributors.
* 4. Neither the name of The NetBSD Foundation nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
/*-
* Copyright (c) 1991, 1993
* The Regents of the University of California. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include <sys/cdefs.h>
#ifndef lint
#if 0
static char sccsid[] = "@(#)lfs.c 8.5 (Berkeley) 5/24/95";
#else
__RCSID("$NetBSD: make_lfs.c,v 1.1 2005/02/26 05:45:54 perseant Exp $");
#endif
#endif /* not lint */
#include <sys/param.h>
#define FSTYPENAMES
#include <sys/disklabel.h>
#include <sys/time.h>
#include <sys/mount.h>
#include <sys/stat.h>
#include <ufs/ufs/dir.h>
#include <ufs/ufs/quota.h>
#include <ufs/ufs/inode.h>
/* Override certain things to make <ufs/lfs/lfs.h> work */
# undef simple_lock
# define simple_lock(x)
# undef simple_unlock
# define simple_unlock(x)
# define vnode uvnode
# define buf ubuf
# define panic call_panic
#include <ufs/lfs/lfs.h>
#include <err.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include "config.h"
#include "extern.h"
#include "bufcache.h"
#include "vnode.h"
#include "lfs.h"
#include "segwrite.h"
extern int Nflag; /* Don't write anything */
ufs_daddr_t ifibc; /* How many indirect blocks */
#ifdef MAKE_LF_DIR
# define HIGHEST_USED_INO LOSTFOUNDINO
#else
# define HIGHEST_USED_INO ROOTINO
#endif
static struct lfs lfs_default = {
{ /* lfs_dlfs */
/* dlfs_magic */ LFS_MAGIC,
/* dlfs_version */ LFS_VERSION,
/* dlfs_size */ 0,
/* dlfs_ssize */ DFL_LFSSEG,
/* dlfs_dsize */ 0,
/* dlfs_bsize */ DFL_LFSBLOCK,
/* dlfs_fsize */ DFL_LFSFRAG,
/* dlfs_frag */ DFL_LFSBLOCK/DFL_LFSFRAG,
/* dlfs_free */ HIGHEST_USED_INO + 1,
/* dlfs_bfree */ 0,
/* dlfs_nfiles */ 0,
/* dlfs_avail */ 0,
/* dlfs_uinodes */ 0,
/* dlfs_idaddr */ 0,
/* dlfs_ifile */ LFS_IFILE_INUM,
/* dlfs_lastseg */ 0,
/* dlfs_nextseg */ 0,
/* dlfs_curseg */ 0,
/* dlfs_offset */ 0,
/* dlfs_lastpseg */ 0,
/* dlfs_inopf */ 0,
/* dlfs_minfree */ MINFREE,
/* dlfs_maxfilesize */ 0,
/* dlfs_fsbpseg */ 0,
/* dlfs_inopb */ DFL_LFSBLOCK/sizeof(struct ufs1_dinode),
/* dlfs_ifpb */ DFL_LFSBLOCK/sizeof(IFILE),
/* dlfs_sepb */ DFL_LFSBLOCK/sizeof(SEGUSE),
/* XXX ondisk32 */
/* dlfs_nindir */ DFL_LFSBLOCK/sizeof(int32_t),
/* dlfs_nseg */ 0,
/* dlfs_nspf */ 0,
/* dlfs_cleansz */ 0,
/* dlfs_segtabsz */ 0,
/* dlfs_segmask */ DFL_LFSSEG_MASK,
/* dlfs_segshift */ DFL_LFSSEG_SHIFT,
/* dlfs_bshift */ DFL_LFSBLOCK_SHIFT,
/* dlfs_ffshift */ DFL_LFS_FFSHIFT,
/* dlfs_fbshift */ DFL_LFS_FBSHIFT,
/* dlfs_bmask */ DFL_LFSBLOCK_MASK,
/* dlfs_ffmask */ DFL_LFS_FFMASK,
/* dlfs_fbmask */ DFL_LFS_FBMASK,
/* dlfs_blktodb */ 0,
/* dlfs_sushift */ 0,
/* dlfs_maxsymlinklen */ MAXSYMLINKLEN_UFS1,
/* dlfs_sboffs */ { 0 },
/* dlfs_nclean */ 0,
/* dlfs_fsmnt */ { 0 },
/* dlfs_pflags */ LFS_PF_CLEAN,
/* dlfs_dmeta */ 0,
/* dlfs_minfreeseg */ 0,
/* dlfs_sumsize */ 0,
/* dlfs_serial */ 0,
/* dlfs_ibsize */ DFL_LFSFRAG,
/* dlfs_start */ 0,
/* dlfs_inodefmt */ LFS_44INODEFMT,
/* dlfs_tstamp */ 0,
/* dlfs_interleave */ 0,
/* dlfs_ident */ 0,
/* dlfs_fsbtodb */ 0,
/* dlfs_pad */ { 0 },
/* dlfs_cksum */ 0
},
/* lfs_sp */ NULL,
/* lfs_ivnode */ NULL,
/* lfs_seglock */ 0,
/* lfs_lockpid */ 0,
/* lfs_iocount */ 0,
/* lfs_writer */ 0,
/* lfs_dirops */ 0,
/* lfs_doifile */ 0,
/* lfs_nactive */ 0,
/* lfs_fmod */ 0,
/* lfs_ronly */ 0,
/* lfs_flags */ 0
};
#define UMASK 0755
struct direct lfs_root_dir[] = {
{ ROOTINO, sizeof(struct direct), DT_DIR, 1, "."},
{ ROOTINO, sizeof(struct direct), DT_DIR, 2, ".."},
/* { LFS_IFILE_INUM, sizeof(struct direct), DT_REG, 5, "ifile"}, */
#ifdef MAKE_LF_DIR
{ LOSTFOUNDINO, sizeof(struct direct), DT_DIR, 10, "lost+found"},
#endif
};
#ifdef MAKE_LF_DIR
struct direct lfs_lf_dir[] = {
{ LOSTFOUNDINO, sizeof(struct direct), DT_DIR, 1, "." },
{ ROOTINO, sizeof(struct direct), DT_DIR, 2, ".." },
};
#endif
void pwarn(const char *, ...);
static void make_dinode(ino_t, struct ufs1_dinode *, int, struct lfs *);
static void make_dir( void *, struct direct *, int);
static uint64_t maxfilesize(int);
/*
* calculate the maximum file size allowed with the specified block shift.
*/
static uint64_t
maxfilesize(int bshift)
{
uint64_t nptr; /* number of block pointers per block */
uint64_t maxblock;
nptr = (1 << bshift) / sizeof(uint32_t);
maxblock = NDADDR + nptr + nptr * nptr + nptr * nptr * nptr;
return maxblock << bshift;
}
/*
* Create the root directory for this file system and the lost+found
* directory.
*/
static void
make_dinode(ino_t ino, struct ufs1_dinode *dip, int nfrags, struct lfs *fs)
{
int fsb_per_blk, i;
int nblocks, bb, base, factor, lvl;
nblocks = howmany(nfrags, fs->lfs_frag);
if(nblocks >= NDADDR)
nfrags = roundup(nfrags, fs->lfs_frag);
dip->di_nlink = 1;
dip->di_blocks = fragstofsb(fs, nfrags);
dip->di_size = (nfrags << fs->lfs_ffshift);
dip->di_atime = dip->di_mtime = dip->di_ctime = fs->lfs_tstamp;
dip->di_atimensec = dip->di_mtimensec = dip->di_ctimensec = 0;
dip->di_inumber = ino;
dip->di_gen = 1;
fsb_per_blk = fragstofsb(fs, blkstofrags(fs, 1));
if (NDADDR < nblocks) {
/* Count up how many indirect blocks we need, recursively */
/* XXX We are only called with nblocks > 1 for Ifile */
bb = nblocks - NDADDR;
while (bb > 0) {
bb = howmany(bb, NINDIR(fs));
ifibc += bb;
--bb;
}
dip->di_blocks += fragstofsb(fs, blkstofrags(fs, ifibc));
}
/* Assign the block addresses for the ifile */
for (i = 0; i < MIN(nblocks,NDADDR); i++) {
dip->di_db[i] = 0x0;
}
if(nblocks > NDADDR) {
dip->di_ib[0] = 0x0;
bb = howmany(nblocks - NDADDR, NINDIR(fs)) - 1;
factor = NINDIR(fs);
base = -NDADDR - factor;
lvl = 1;
while (bb > 0) {
dip->di_ib[lvl] = 0x0;
bb = howmany(bb, NINDIR(fs));
--bb;
factor *= NINDIR(fs);
base -= factor;
++lvl;
}
}
}
/*
* Construct a set of directory entries in "bufp". We assume that all the
* entries in protodir fir in the first DIRBLKSIZ.
*/
static void
make_dir(void *bufp, struct direct *protodir, int entries)
{
char *cp;
int i, spcleft;
spcleft = DIRBLKSIZ;
for (cp = bufp, i = 0; i < entries - 1; i++) {
protodir[i].d_reclen = DIRSIZ(NEWDIRFMT, &protodir[i], 0);
memmove(cp, &protodir[i], protodir[i].d_reclen);
cp += protodir[i].d_reclen;
if ((spcleft -= protodir[i].d_reclen) < 0)
fatal("%s: %s", special, "directory too big");
}
protodir[i].d_reclen = spcleft;
memmove(cp, &protodir[i], DIRSIZ(NEWDIRFMT, &protodir[i], 0));
}
int
make_lfs(int devfd, uint secsize, struct partition *partp, int minfree,
int block_size, int frag_size, int seg_size, int minfreeseg,
int version, daddr_t start, int ibsize, int interleave,
u_int32_t roll_id)
{
struct ufs1_dinode *dip; /* Pointer to a disk inode */
CLEANERINFO *cip; /* Segment cleaner information table */
IFILE *ip; /* Pointer to array of ifile structures */
IFILE_V1 *ip_v1;
struct lfs *fs; /* Superblock */
SEGUSE *segp; /* Segment usage table */
daddr_t sb_addr; /* Address of superblocks */
daddr_t seg_addr; /* Address of current segment */
int bsize; /* Block size */
int fsize; /* Fragment size */
int db_per_blk; /* Disk blocks per file block */
int i, j;
int sb_interval; /* number of segs between super blocks */
int ssize; /* Segment size */
double fssize;
int warned_segtoobig=0;
int label_fsb, sb_fsb;
int curw, ww;
char tbuf[BUFSIZ];
struct ubuf *bp;
struct uvnode *vp, *save_devvp;
int bb, ubb, dmeta, labelskew;
/* Initialize buffer cache */
bufinit();
/* Initialize LFS subsystem with blank superblock and ifile. */
fs = lfs_init(devfd, start, (ufs_daddr_t)0, 1, 1/* XXX debug*/);
save_devvp = fs->lfs_unlockvp;
vp = fs->lfs_ivnode;
*fs = lfs_default;
fs->lfs_ivnode = vp;
fs->lfs_unlockvp = save_devvp;
/* Set version first of all since it is used to compute other fields */
fs->lfs_version = version;
/* If partition is not an LFS partition, warn that that is the case */
if(partp->p_fstype != FS_BSDLFS) {
fatal("partition label indicated fs type \"%s\", expected \"%s\"",
fstypenames[partp->p_fstype], fstypenames[FS_BSDLFS]);
}
if (!(bsize = block_size))
if (!(bsize = partp->p_fsize * partp->p_frag))
bsize = DFL_LFSBLOCK;
if (!(fsize = frag_size))
if (!(fsize = partp->p_fsize))
fsize = DFL_LFSFRAG;
if (!(ssize = seg_size)) {
ssize = DFL_LFSSEG;
if (partp->p_sgs == 0 ||
!(ssize = (partp->p_fsize * partp->p_frag) << partp->p_sgs))
{
ssize = DFL_LFSSEG;
}
}
if (version > 1) {
if (ibsize == 0)
ibsize = fsize;
if (ibsize <= 0 || ibsize % fsize)
fatal("illegal inode block size: %d\n", ibsize);
} else if (ibsize && ibsize != bsize)
fatal("cannot specify inode block size when version == 1\n");
/* Sanity check: fsize<=bsize<ssize */
if (fsize > bsize) {
/* Only complain if fsize was explicitly set */
if(frag_size)
fatal("fragment size must be <= block size %d", bsize);
fsize = bsize;
}
if (bsize >= ssize) {
/* Only fatal if ssize was explicitly set */
if(seg_size)
fatal("block size must be < segment size");
warnx("%s: disklabel segment size (%d) too small, using default (%d)",
progname, ssize, DFL_LFSSEG);
ssize = DFL_LFSSEG;
}
if (start < 0 || start >= partp->p_size)
fatal("filesystem offset %ld out of range", (long)start);
if (version == 1) {
if (start)
warnx("filesystem offset ignored for version 1 filesystem");
start = LFS_LABELPAD / secsize;
}
tryagain:
/* Modify parts of superblock overridden by command line arguments */
if (bsize != DFL_LFSBLOCK || fsize != DFL_LFSFRAG) {
fs->lfs_bshift = log2(bsize);
if (1 << fs->lfs_bshift != bsize)
fatal("%d: block size not a power of 2", bsize);
fs->lfs_bsize = bsize;
fs->lfs_fsize = fsize;
fs->lfs_bmask = bsize - 1;
fs->lfs_ffmask = fsize - 1;
fs->lfs_ffshift = log2(fsize);
if (1 << fs->lfs_ffshift != fsize)
fatal("%d: frag size not a power of 2", fsize);
fs->lfs_frag = numfrags(fs, bsize);
fs->lfs_fbmask = fs->lfs_frag - 1;
fs->lfs_fbshift = log2(fs->lfs_frag);
fs->lfs_ifpb = bsize / sizeof(IFILE);
/* XXX ondisk32 */
fs->lfs_nindir = bsize / sizeof(int32_t);
}
if (fs->lfs_version == 1) {
fs->lfs_sumsize = LFS_V1_SUMMARY_SIZE;
fs->lfs_segshift = log2(ssize);
if (1 << fs->lfs_segshift != ssize)
fatal("%d: segment size not power of 2", ssize);
fs->lfs_segmask = ssize - 1;
fs->lfs_ifpb = fs->lfs_bsize / sizeof(IFILE_V1);
fs->lfs_ibsize = fs->lfs_bsize;
fs->lfs_sepb = bsize / sizeof(SEGUSE_V1);
fs->lfs_ssize = ssize >> fs->lfs_bshift;
} else {
if (ssize % fsize) {
fprintf(stderr,
"Segment size %d is not a multiple of frag size; ",
ssize);
ssize = roundup(ssize, fsize);
fprintf(stderr, "trying size %d.\n", ssize);
goto tryagain;
}
fs->lfs_sumsize = fsize;
fs->lfs_segshift = 0;
fs->lfs_segmask = 0;
fs->lfs_sepb = bsize / sizeof(SEGUSE);
fs->lfs_ssize = ssize;
fs->lfs_ibsize = ibsize;
}
fs->lfs_inopb = fs->lfs_ibsize / sizeof(struct ufs1_dinode);
fs->lfs_minfree = minfree;
if (version > 1) {
fs->lfs_inopf = secsize/DINODE1_SIZE;
fs->lfs_interleave = interleave;
if (roll_id == 0)
roll_id = arc4random();
fs->lfs_ident = roll_id;
}
/*
* Fill in parts of superblock that can be computed from file system
* size, disk geometry and current time.
*/
db_per_blk = bsize/secsize;
fs->lfs_blktodb = log2(db_per_blk);
fs->lfs_fsbtodb = log2(fsize / secsize);
if (version == 1) {
fs->lfs_sushift = log2(fs->lfs_sepb);
fs->lfs_fsbtodb = 0;
fs->lfs_size = partp->p_size >> fs->lfs_blktodb;
}
label_fsb = btofsb(fs, roundup(LFS_LABELPAD, fsize));
sb_fsb = btofsb(fs, roundup(LFS_SBPAD, fsize));
fs->lfs_fsbpseg = dbtofsb(fs, ssize / secsize);
fs->lfs_size = partp->p_size >> fs->lfs_fsbtodb;
fs->lfs_dsize = dbtofsb(fs, partp->p_size) -
MAX(label_fsb, dbtofsb(fs, start));
fs->lfs_nseg = fs->lfs_dsize / segtod(fs, 1);
fs->lfs_nclean = fs->lfs_nseg - 1;
fs->lfs_maxfilesize = maxfilesize(fs->lfs_bshift);
if (minfreeseg == 0)
fs->lfs_minfreeseg = fs->lfs_nseg / DFL_MIN_FREE_SEGS;
else
fs->lfs_minfreeseg = minfreeseg;
if (fs->lfs_minfreeseg < MIN_FREE_SEGS)
fs->lfs_minfreeseg = MIN_FREE_SEGS;
if(fs->lfs_nseg < fs->lfs_minfreeseg + 1
|| fs->lfs_nseg < LFS_MIN_SBINTERVAL + 1)
{
if(seg_size == 0 && ssize > (bsize<<1)) {
if(!warned_segtoobig) {
fprintf(stderr,"Segment size %d is too large; "
"trying smaller sizes.\n", ssize);
if (ssize == (bsize << 16)) {
fprintf(stderr, "(Did you perhaps "
"accidentally leave \"16\" "
"in the disklabel's sgs "
"field?)\n");
}
}
++warned_segtoobig;
ssize >>= 1;
goto tryagain;
}
fatal("Could not allocate enough segments with segment "
"size %d and block size %d;\nplease decrease the "
"segment size.\n", ssize, fs->lfs_bsize);
}
/*
* Now that we've determined what we're going to do, announce it
* to the user.
*/
printf("Creating a version %d LFS", fs->lfs_version);
if (fs->lfs_version > 1)
printf(" with roll-forward ident 0x%x", fs->lfs_ident);
printf("\n");
fssize = (double)fs->lfs_nseg;
fssize *= (double)ssize;
fssize /= 1048576.0;
printf("%.1fMB in %d segments of size %d\n", fssize,
fs->lfs_nseg, ssize);
/*
* The number of free blocks is set from the number of segments
* times the segment size - lfs_minfreesegs (that we never write
* because we need to make sure the cleaner can run). Then
* we'll subtract off the room for the superblocks ifile entries
* and segment usage table, and half a block per segment that can't
* be written due to fragmentation.
*/
fs->lfs_dsize = (fs->lfs_nseg - fs->lfs_minfreeseg) *
segtod(fs, 1);
fs->lfs_bfree = fs->lfs_dsize;
fs->lfs_bfree -= dbtofsb(fs, ((fs->lfs_nseg / 2) <<
fs->lfs_blktodb));
fs->lfs_segtabsz = SEGTABSIZE_SU(fs);
fs->lfs_cleansz = CLEANSIZE_SU(fs);
if ((fs->lfs_tstamp = time(NULL)) == -1)
fatal("time: %s", strerror(errno));
if (version == 1)
fs->lfs_otstamp = fs->lfs_tstamp;
if ((sb_interval = fs->lfs_nseg / LFS_MAXNUMSB) < LFS_MIN_SBINTERVAL)
sb_interval = LFS_MIN_SBINTERVAL;
/*
* Figure out where the superblocks are going to live.
*
* Make segment 0 start at either zero, or LFS_LABELPAD, or
* >= LFS_SBPAD+LFS_LABELPAD, in order to prevent segment 0
* from having half a superblock in it.
*/
if (fsbtodb(fs, dbtofsb(fs, start)) != start)
fatal("Segment 0 offset is not multiple of frag size\n");
if (start != 0 && dbtob(start) != LFS_LABELPAD &&
dbtob(start) < LFS_SBPAD + LFS_LABELPAD) {
fatal("Using flags \"-O %" PRId64 "\" would result in the "
"first segment containing only\npart of a superblock. "
"Please choose an offset of 0, %d, or %d or more,\n",
start, btodb(LFS_LABELPAD),
btodb(LFS_LABELPAD + LFS_SBPAD));
}
fs->lfs_sboffs[0] = label_fsb;
if (version == 1)
fs->lfs_start = fs->lfs_sboffs[0];
else
fs->lfs_start = dbtofsb(fs, start);
fs->lfs_dsize -= sb_fsb;
for (i = 1; i < LFS_MAXNUMSB; i++) {
sb_addr = ((i * sb_interval) * segtod(fs, 1))
+ fs->lfs_sboffs[0];
/* Segment 0 eats the label, except for version 1 */
if (fs->lfs_version > 1 && fs->lfs_start < label_fsb)
sb_addr -= label_fsb - start;
if (sb_addr + sizeof(struct dlfs)
>= dbtofsb(fs, partp->p_size))
break;
fs->lfs_sboffs[i] = sb_addr;
fs->lfs_dsize -= sb_fsb;
}
/* We need >= 2 superblocks */
if(fs->lfs_sboffs[1] == 0x0) {
fatal("Could not assign a disk address for the second "
"superblock.\nPlease decrease the segment size.\n");
}
fs->lfs_lastseg = sntod(fs, fs->lfs_nseg - 2);
fs->lfs_curseg = sntod(fs, fs->lfs_nseg - 1);
fs->lfs_offset = sntod(fs, fs->lfs_nseg);
fs->lfs_nextseg = sntod(fs, 0);
/*
* Initialize the Ifile inode. Do this before we do anything
* with the Ifile or segment tables.
*/
dip = VTOI(fs->lfs_ivnode)->i_din.ffs1_din = (struct ufs1_dinode *)
malloc(sizeof(*dip));
memset(dip, 0, sizeof(*dip));
dip->di_mode = IFREG|IREAD|IWRITE;
dip->di_flags = SF_IMMUTABLE;
make_dinode(LFS_IFILE_INUM, dip,
blkstofrags(fs, fs->lfs_cleansz + fs->lfs_segtabsz + 1), fs);
dip->di_size = (fs->lfs_cleansz + fs->lfs_segtabsz + 1) << fs->lfs_bshift;
for (i = 0; i < NDADDR && i < (dip->di_size >> fs->lfs_bshift); i++)
VTOI(fs->lfs_ivnode)->i_lfs_fragsize[i] = fs->lfs_bsize;
/*
* Set up in-superblock segment usage cache
*/
fs->lfs_suflags = (u_int32_t **) malloc(2 * sizeof(u_int32_t *));
fs->lfs_suflags[0] = (u_int32_t *) malloc(fs->lfs_nseg * sizeof(u_int32_t));
fs->lfs_suflags[1] = (u_int32_t *) malloc(fs->lfs_nseg * sizeof(u_int32_t));
/*
* Initialize the cleanerinfo block
*/
LFS_CLEANERINFO(cip, fs, bp);
cip->clean = fs->lfs_nseg;
cip->dirty = 0;
if (version > 1) {
cip->free_head = HIGHEST_USED_INO + 1;
cip->free_tail = fs->lfs_ifpb - 1;
}
LFS_SYNC_CLEANERINFO(cip, fs, bp, 1);
/*
* Run through segment table and initialize that
*/
for (i = j = 0; i < fs->lfs_nseg; i++) {
LFS_SEGENTRY(segp, fs, i, bp);
if (i == 0 &&
fs->lfs_start < btofsb(fs, LFS_LABELPAD + LFS_SBPAD)) {
segp->su_flags = SEGUSE_SUPERBLOCK;
fs->lfs_bfree -= sb_fsb;
++j;
}
if (i > 0) {
if ((i % sb_interval) == 0 && j < LFS_MAXNUMSB) {
segp->su_flags = SEGUSE_SUPERBLOCK;
fs->lfs_bfree -= sb_fsb;
++j;
} else
segp->su_flags = 0;
}
segp->su_lastmod = 0;
segp->su_nbytes = 0;
segp->su_ninos = 0;
segp->su_nsums = 0;
LFS_WRITESEGENTRY(segp, fs, i, bp);
}
/* Initialize root directory */
vp = lfs_raw_vget(fs, ROOTINO, devfd, 0x0);
dip = VTOI(vp)->i_din.ffs1_din;
make_dinode(ROOTINO, dip, howmany(DIRBLKSIZ,fs->lfs_fsize), fs);
dip->di_mode = IFDIR | UMASK;
VTOI(vp)->i_lfs_osize = dip->di_size = DIRBLKSIZ;
#ifdef MAKE_LF_DIR
VTOI(vp)->i_ffs_effnlink = dip->di_nlink = 3;
#else
VTOI(vp)->i_ffs_effnlink = dip->di_nlink = 2;
#endif
VTOI(vp)->i_lfs_effnblks = dip->di_blocks =
btofsb(fs, roundup(DIRBLKSIZ,fs->lfs_fsize));
for (i = 0; i < NDADDR && i < howmany(DIRBLKSIZ, fs->lfs_bsize); i++)
VTOI(vp)->i_lfs_fragsize[i] = fs->lfs_bsize;
if (DIRBLKSIZ < fs->lfs_bsize)
VTOI(vp)->i_lfs_fragsize[i - 1] =
roundup(DIRBLKSIZ,fs->lfs_fsize);
bread(vp, 0, fs->lfs_fsize, NOCRED, &bp);
make_dir(bp->b_data, lfs_root_dir,
sizeof(lfs_root_dir) / sizeof(struct direct));
VOP_BWRITE(bp);
#ifdef MAKE_LF_DIR
/* Initialize lost+found directory */
vp = lfs_raw_vget(fs, LOSTFOUNDINO, devfd, 0x0);
dip = VTOI(vp)->i_din.ffs1_din;
make_dinode(LOSTFOUNDINO, dip, howmany(DIRBLKSIZ,fs->lfs_fsize), fs);
dip->di_mode = IFDIR | UMASK;
VTOI(vp)->i_lfs_osize = dip->di_size = DIRBLKSIZ;
VTOI(vp)->i_ffs_effnlink = dip->di_nlink = 2;
VTOI(vp)->i_lfs_effnblks = dip->di_blocks =
btofsb(fs, roundup(DIRBLKSIZ,fs->lfs_fsize));
for (i = 0; i < NDADDR && i < howmany(DIRBLKSIZ, fs->lfs_bsize); i++)
VTOI(vp)->i_lfs_fragsize[i] = fs->lfs_bsize;
if (DIRBLKSIZ < fs->lfs_bsize)
VTOI(vp)->i_lfs_fragsize[i - 1] =
roundup(DIRBLKSIZ,fs->lfs_fsize);
bread(vp, 0, fs->lfs_fsize, NOCRED, &bp);
make_dir(bp->b_data, lfs_lf_dir,
sizeof(lfs_lf_dir) / sizeof(struct direct));
VOP_BWRITE(bp);
#endif /* MAKE_LF_DIR */
/* Set their IFILE entry version numbers to 1 */
LFS_IENTRY(ip, fs, 1, bp);
if (version == 1) {
ip_v1 = (IFILE_V1 *)ip;
for (i = LFS_IFILE_INUM; i <= HIGHEST_USED_INO; i++) {
ip_v1->if_version = 1;
ip_v1->if_daddr = 0x0;
ip_v1->if_nextfree = 0;
++ip_v1;
}
} else {
for (i = LFS_IFILE_INUM; i <= HIGHEST_USED_INO; i++) {
ip->if_version = 1;
ip->if_daddr = 0x0;
ip->if_nextfree = 0;
++ip;
}
}
/* Link remaining IFILE entries in free list */
if (version == 1) {
for (;
i < fs->lfs_ifpb; ++ip_v1) {
ip_v1->if_version = 1;
ip_v1->if_daddr = LFS_UNUSED_DADDR;
ip_v1->if_nextfree = ++i;
}
--ip_v1;
ip_v1->if_nextfree = LFS_UNUSED_INUM;
} else {
for (;
i < fs->lfs_ifpb; ++ip) {
ip->if_version = 1;
ip->if_daddr = LFS_UNUSED_DADDR;
ip->if_nextfree = ++i;
}
--ip;
ip->if_nextfree = LFS_UNUSED_INUM;
}
VOP_BWRITE(bp);
/* Write it all to disk. */
if (!Nflag)
lfs_segwrite(fs, SEGM_CKP);
/*
* Now that we've written everything, look to see what's available
* for writing.
*/
fs->lfs_avail = 0;
bb = ubb = dmeta = 0;
for (i = 0; i < fs->lfs_nseg; i++) {
LFS_SEGENTRY(segp, fs, i, bp);
if (segp->su_flags & SEGUSE_DIRTY) {
bb += btofsb(fs, segp->su_nbytes +
segp->su_nsums * fs->lfs_sumsize);
ubb += btofsb(fs, segp->su_nbytes +
segp->su_nsums * fs->lfs_sumsize +
segp->su_ninos * fs->lfs_ibsize);
dmeta += btofsb(fs,
fs->lfs_sumsize * segp->su_nsums);
dmeta += btofsb(fs,
fs->lfs_ibsize * segp->su_ninos);
} else {
fs->lfs_avail += segtod(fs, 1);
if (segp->su_flags & SEGUSE_SUPERBLOCK)
fs->lfs_avail -= btofsb(fs, LFS_SBPAD);
if (i == 0 && fs->lfs_version > 1 &&
fs->lfs_start < btofsb(fs, LFS_LABELPAD))
fs->lfs_avail -= btofsb(fs, LFS_LABELPAD) -
fs->lfs_start;
}
brelse(bp);
}
/* Also may be available bytes in current seg */
i = dtosn(fs, fs->lfs_offset);
fs->lfs_avail += sntod(fs, i + 1) - fs->lfs_offset;
/* But do not count minfreesegs */
fs->lfs_avail -= segtod(fs, (fs->lfs_minfreeseg - (fs->lfs_minfreeseg / 2)));
labelskew = 0;
if (fs->lfs_version > 1 && fs->lfs_start < btofsb(fs, LFS_LABELPAD))
labelskew = btofsb(fs, LFS_LABELPAD);
fs->lfs_bfree = fs->lfs_dsize - labelskew - (ubb + bb) / 2;
/* Put that in the Ifile version too, and write it */
LFS_CLEANERINFO(cip, fs, bp);
cip->bfree = fs->lfs_bfree;
cip->avail = fs->lfs_avail;
LFS_SYNC_CLEANERINFO(cip, fs, bp, 1);
if (!Nflag)
lfs_segwrite(fs, SEGM_CKP);
/*
* Finally write out superblocks.
*/
printf("super-block backups (for fsck -b #) at:\n");
curw = 0;
for (i = 0; i < LFS_MAXNUMSB; i++) {
seg_addr = fs->lfs_sboffs[i];
if (seg_addr == 0)
break;
if (i != 0)
curw += printf(", ");
ww = snprintf(tbuf, sizeof(tbuf), "%lld",
(long long)fsbtodb(fs, seg_addr));
curw += ww;
if (curw >= 78) {
printf("\n%s", tbuf);
curw = ww;
} else
printf("%s", tbuf);
fflush(stdout);
/* Leave the time stamp on the alt sb, zero the rest */
if (i == 2) {
fs->lfs_tstamp = 0;
fs->lfs_cksum = lfs_sb_cksum(&(fs->lfs_dlfs));
}
if (!Nflag)
lfs_writesuper(fs, seg_addr);
}
printf(".\n");
return 0;
}
/*
* Compatibility with fsck_lfs, since the "generic" LFS userland code uses it.
*/
void
pwarn(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
vfprintf(stderr, fmt, ap);
va_end(ap);
}

View File

@ -1,4 +1,4 @@
/* $NetBSD: newfs.c,v 1.14 2004/10/29 19:04:39 dsl Exp $ */
/* $NetBSD: newfs.c,v 1.15 2005/02/26 05:45:54 perseant Exp $ */
/*-
* Copyright (c) 1989, 1992, 1993
@ -39,7 +39,7 @@ __COPYRIGHT("@(#) Copyright (c) 1989, 1992, 1993\n\
#if 0
static char sccsid[] = "@(#)newfs.c 8.5 (Berkeley) 5/24/95";
#else
__RCSID("$NetBSD: newfs.c,v 1.14 2004/10/29 19:04:39 dsl Exp $");
__RCSID("$NetBSD: newfs.c,v 1.15 2005/02/26 05:45:54 perseant Exp $");
#endif
#endif /* not lint */
@ -91,6 +91,7 @@ caddr_t membase; /* start address of memory based filesystem */
char *disktype;
int unlabeled;
#endif
int preen = 0; /* Coexistence with fsck_lfs */
char device[MAXPATHLEN];
char *progname, *special;
@ -100,6 +101,7 @@ static struct disklabel *debug_readlabel(int);
#ifdef notdef
static void rewritelabel(char *, int, struct disklabel *);
#endif
static int64_t strsuftoi64(const char *, const char *, int64_t, int64_t, int *);
static void usage(void);
/* CHUNKSIZE should be larger than MAXPHYS */
@ -166,6 +168,7 @@ main(int argc, char **argv)
uint secsize = 0;
daddr_t start;
char *cp, *opstring;
int byte_sized = 0;
version = DFL_VERSION; /* what version of lfs to make */
@ -178,7 +181,7 @@ main(int argc, char **argv)
if (maxpartitions > 26)
fatal("insane maxpartitions value %d", maxpartitions);
opstring = "AB:b:DFf:I:i:LM:m:NO:r:Ss:v:";
opstring = "AB:b:DFf:I:i:LM:m:NO:r:S:s:v:";
debug = force = segsize = start = 0;
while ((ch = getopt(argc, argv, opstring)) != -1)
@ -187,8 +190,7 @@ main(int argc, char **argv)
segsize = -1;
break;
case 'B': /* LFS segment size */
if ((segsize = atoi(optarg)) < LFS_MINSEGSIZE)
fatal("%s: bad segment size", optarg);
segsize = strsuftoi64("segment size", optarg, LFS_MINSEGSIZE, INT64_MAX, NULL);
break;
case 'D':
debug = 1;
@ -197,21 +199,21 @@ main(int argc, char **argv)
force = 1;
break;
case 'I':
interleave = atoi(optarg);
interleave = strsuftoi64("interleave", optarg, 0, INT64_MAX, NULL);
break;
case 'L': /* Compatibility only */
break;
case 'M':
minfreeseg = atoi(optarg);
minfreeseg = strsuftoi64("minfreeseg", optarg, 0, INT64_MAX, NULL);
break;
case 'N':
Nflag++;
break;
case 'O':
start = atoi(optarg);
start = strsuftoi64("start", optarg, 0, INT64_MAX, NULL);
break;
case 'S':
secsize = atoi(optarg);
secsize = strsuftoi64("sector size", optarg, 1, INT64_MAX, NULL);
if (secsize <= 0 || (secsize & (secsize - 1)))
fatal("%s: bad sector size", optarg);
break;
@ -221,33 +223,25 @@ main(int argc, char **argv)
break;
#endif
case 'b':
if ((bsize = atoi(optarg)) < LFS_MINBLOCKSIZE)
fatal("%s: bad block size", optarg);
bsize = strsuftoi64("block size", optarg, LFS_MINBLOCKSIZE, INT64_MAX, NULL);
break;
case 'f':
if ((fsize = atoi(optarg)) <= 0)
fatal("%s: bad frag size", optarg);
fsize = strsuftoi64("fragment size", optarg, LFS_MINBLOCKSIZE, INT64_MAX, NULL);
break;
case 'i':
if ((ibsize = atoi(optarg)) <= 0)
fatal("%s: bad inode block size", optarg);
ibsize = strsuftoi64("inode block size", optarg, LFS_MINBLOCKSIZE, INT64_MAX, NULL);
break;
case 'm':
if ((minfree = atoi(optarg)) < 0 || minfree > 99)
fatal("%s: bad free space %%\n", optarg);
minfree = strsuftoi64("free space %", optarg, 0, 99, NULL);
break;
case 'r':
if ((roll_id = strtoul(optarg, NULL, 0)) == 0)
fatal("%s: bad roll-forward id\n", optarg);
roll_id = strsuftoi64("roll-forward id", optarg, 1, UINT_MAX, NULL);
break;
case 's':
if ((fssize = atoi(optarg)) <= 0)
fatal("%s: bad file system size", optarg);
fssize = strsuftoi64("file system size", optarg, 0, INT64_MAX, &byte_sized);
break;
case 'v':
version = atoi(optarg);
if (version <= 0 || version > LFS_VERSION)
fatal("%s: bad version", optarg);
version = strsuftoi64("file system version", optarg, 1, LFS_VERSION, NULL);
break;
case '?':
default:
@ -274,7 +268,7 @@ main(int argc, char **argv)
}
if (!Nflag) {
fso = open(special,
(debug ? O_CREAT : 0) | O_WRONLY, DEFFILEMODE);
(debug ? O_CREAT : 0) | O_RDWR, DEFFILEMODE);
if (fso < 0)
fatal("%s: %s", special, strerror(errno));
} else
@ -325,6 +319,11 @@ main(int argc, char **argv)
if (secsize == 0)
secsize = lp->d_secsize;
/* From here on out fssize is in sectors */
if (byte_sized) {
fssize /= secsize;
}
/* If force, make the partition look like an LFS */
if (force) {
pp->p_fstype = FS_BSDLFS;
@ -453,6 +452,51 @@ rewritelabel(char *s, int fd, struct disklabel *lp)
}
#endif /* notdef */
static int64_t
strsuftoi64(const char *desc, const char *arg, int64_t min, int64_t max, int *num_suffix)
{
int64_t result, r1;
int shift = 0;
char *ep;
errno = 0;
r1 = strtoll(arg, &ep, 10);
if (ep[0] != '\0' && ep[1] != '\0')
errx(1, "%s `%s' is not a valid number.", desc, arg);
switch (ep[0]) {
case '\0':
case 's': case 'S':
if (num_suffix != NULL)
*num_suffix = 0;
break;
case 'g': case 'G':
shift += 10;
/* FALLTHROUGH */
case 'm': case 'M':
shift += 10;
/* FALLTHROUGH */
case 'k': case 'K':
shift += 10;
/* FALLTHROUGH */
case 'b': case 'B':
if (num_suffix != NULL)
*num_suffix = 1;
break;
default:
errx(1, "`%s' is not a valid suffix for %s.", ep, desc);
}
result = r1 << shift;
if (errno == ERANGE || result >> shift != r1)
errx(1, "%s `%s' is too large to convert.", desc, arg);
if (result < min)
errx(1, "%s `%s' (%" PRId64 ") is less than the minimum (%" PRId64 ").",
desc, arg, result, min);
if (result > max)
errx(1, "%s `%s' (%" PRId64 ") is greater than the maximum (%" PRId64 ").",
desc, arg, result, max);
return result;
}
void
usage()
{