Home | History | Annotate | Line # | Download | only in fsck_lfs
segwrite.c revision 1.35
      1 /* $NetBSD: segwrite.c,v 1.35 2015/08/02 18:14:16 dholland Exp $ */
      2 /*-
      3  * Copyright (c) 2003 The NetBSD Foundation, Inc.
      4  * All rights reserved.
      5  *
      6  * This code is derived from software contributed to The NetBSD Foundation
      7  * by Konrad E. Schroder <perseant (at) hhhh.org>.
      8  *
      9  * Redistribution and use in source and binary forms, with or without
     10  * modification, are permitted provided that the following conditions
     11  * are met:
     12  * 1. Redistributions of source code must retain the above copyright
     13  *    notice, this list of conditions and the following disclaimer.
     14  * 2. Redistributions in binary form must reproduce the above copyright
     15  *    notice, this list of conditions and the following disclaimer in the
     16  *    documentation and/or other materials provided with the distribution.
     17  *
     18  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     19  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     20  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     21  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     22  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     24  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     26  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     28  * POSSIBILITY OF SUCH DAMAGE.
     29  */
     30 /*
     31  * Copyright (c) 1991, 1993
     32  *	The Regents of the University of California.  All rights reserved.
     33  *
     34  * Redistribution and use in source and binary forms, with or without
     35  * modification, are permitted provided that the following conditions
     36  * are met:
     37  * 1. Redistributions of source code must retain the above copyright
     38  *    notice, this list of conditions and the following disclaimer.
     39  * 2. Redistributions in binary form must reproduce the above copyright
     40  *    notice, this list of conditions and the following disclaimer in the
     41  *    documentation and/or other materials provided with the distribution.
     42  * 3. Neither the name of the University nor the names of its contributors
     43  *    may be used to endorse or promote products derived from this software
     44  *    without specific prior written permission.
     45  *
     46  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     47  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     48  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     49  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     50  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     51  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     52  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     53  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     54  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     55  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     56  * SUCH DAMAGE.
     57  *
     58  *	@(#)lfs_segment.c	8.10 (Berkeley) 6/10/95
     59  */
     60 
     61 /*
     62  * Partial segment writer, taken from the kernel and adapted for userland.
     63  */
     64 #include <sys/types.h>
     65 #include <sys/param.h>
     66 #include <sys/time.h>
     67 #include <sys/buf.h>
     68 #include <sys/mount.h>
     69 
     70 /* Override certain things to make <ufs/lfs/lfs.h> work */
     71 #define VU_DIROP 0x01000000 /* XXX XXX from sys/vnode.h */
     72 #define vnode uvnode
     73 #define buf ubuf
     74 #define panic call_panic
     75 
     76 #include <ufs/lfs/lfs.h>
     77 #include <ufs/lfs/lfs_accessors.h>
     78 #include <ufs/lfs/lfs_inode.h>
     79 
     80 #include <assert.h>
     81 #include <stdio.h>
     82 #include <stdlib.h>
     83 #include <string.h>
     84 #include <err.h>
     85 #include <errno.h>
     86 #include <util.h>
     87 
     88 #include "bufcache.h"
     89 #include "vnode.h"
     90 #include "lfs_user.h"
     91 #include "segwrite.h"
     92 
     93 /* Compatibility definitions */
     94 extern off_t locked_queue_bytes;
     95 int locked_queue_count;
     96 off_t written_bytes = 0;
     97 off_t written_data = 0;
     98 off_t written_indir = 0;
     99 off_t written_dev = 0;
    100 int written_inodes = 0;
    101 
    102 /* Global variables */
    103 time_t write_time;
    104 
    105 extern u_int32_t cksum(void *, size_t);
    106 extern u_int32_t lfs_sb_cksum(struct dlfs *);
    107 extern int preen;
    108 
    109 /*
    110  * Logical block number match routines used when traversing the dirty block
    111  * chain.
    112  */
    113 int
    114 lfs_match_data(struct lfs * fs, struct ubuf * bp)
    115 {
    116 	return (bp->b_lblkno >= 0);
    117 }
    118 
    119 int
    120 lfs_match_indir(struct lfs * fs, struct ubuf * bp)
    121 {
    122 	daddr_t lbn;
    123 
    124 	lbn = bp->b_lblkno;
    125 	return (lbn < 0 && (-lbn - ULFS_NDADDR) % LFS_NINDIR(fs) == 0);
    126 }
    127 
    128 int
    129 lfs_match_dindir(struct lfs * fs, struct ubuf * bp)
    130 {
    131 	daddr_t lbn;
    132 
    133 	lbn = bp->b_lblkno;
    134 	return (lbn < 0 && (-lbn - ULFS_NDADDR) % LFS_NINDIR(fs) == 1);
    135 }
    136 
    137 int
    138 lfs_match_tindir(struct lfs * fs, struct ubuf * bp)
    139 {
    140 	daddr_t lbn;
    141 
    142 	lbn = bp->b_lblkno;
    143 	return (lbn < 0 && (-lbn - ULFS_NDADDR) % LFS_NINDIR(fs) == 2);
    144 }
    145 
    146 /*
    147  * Do a checkpoint.
    148  */
    149 int
    150 lfs_segwrite(struct lfs * fs, int flags)
    151 {
    152 	struct inode *ip;
    153 	struct segment *sp;
    154 	struct uvnode *vp;
    155 	int redo;
    156 
    157 	lfs_seglock(fs, flags | SEGM_CKP);
    158 	sp = fs->lfs_sp;
    159 
    160 	lfs_writevnodes(fs, sp, VN_REG);
    161 	lfs_writevnodes(fs, sp, VN_DIROP);
    162 	((SEGSUM *) (sp->segsum))->ss_flags &= ~(SS_CONT);
    163 
    164 	do {
    165 		vp = fs->lfs_ivnode;
    166 		fs->lfs_flags &= ~LFS_IFDIRTY;
    167 		ip = VTOI(vp);
    168 		if (LIST_FIRST(&vp->v_dirtyblkhd) != NULL || lfs_sb_getidaddr(fs) <= 0)
    169 			lfs_writefile(fs, sp, vp);
    170 
    171 		redo = lfs_writeinode(fs, sp, ip);
    172 		redo += lfs_writeseg(fs, sp);
    173 		redo += (fs->lfs_flags & LFS_IFDIRTY);
    174 	} while (redo);
    175 
    176 	lfs_segunlock(fs);
    177 #if 0
    178 	printf("wrote %" PRId64 " bytes (%" PRId32 " fsb)\n",
    179 		written_bytes, (ulfs_daddr_t)lfs_btofsb(fs, written_bytes));
    180 	printf("wrote %" PRId64 " bytes data (%" PRId32 " fsb)\n",
    181 		written_data, (ulfs_daddr_t)lfs_btofsb(fs, written_data));
    182 	printf("wrote %" PRId64 " bytes indir (%" PRId32 " fsb)\n",
    183 		written_indir, (ulfs_daddr_t)lfs_btofsb(fs, written_indir));
    184 	printf("wrote %" PRId64 " bytes dev (%" PRId32 " fsb)\n",
    185 		written_dev, (ulfs_daddr_t)lfs_btofsb(fs, written_dev));
    186 	printf("wrote %d inodes (%" PRId32 " fsb)\n",
    187 		written_inodes, lfs_btofsb(fs, written_inodes * fs->lfs_ibsize));
    188 #endif
    189 	return 0;
    190 }
    191 
    192 /*
    193  * Write the dirty blocks associated with a vnode.
    194  */
    195 void
    196 lfs_writefile(struct lfs * fs, struct segment * sp, struct uvnode * vp)
    197 {
    198 	struct ubuf *bp;
    199 	struct finfo *fip;
    200 	struct inode *ip;
    201 	IFILE *ifp;
    202 
    203 	ip = VTOI(vp);
    204 
    205 	if (sp->seg_bytes_left < lfs_sb_getbsize(fs) ||
    206 	    sp->sum_bytes_left < sizeof(struct finfo))
    207 		(void) lfs_writeseg(fs, sp);
    208 
    209 	sp->sum_bytes_left -= FINFOSIZE;
    210 	++((SEGSUM *) (sp->segsum))->ss_nfinfo;
    211 
    212 	if (vp->v_uflag & VU_DIROP)
    213 		((SEGSUM *) (sp->segsum))->ss_flags |= (SS_DIROP | SS_CONT);
    214 
    215 	fip = sp->fip;
    216 	fip->fi_nblocks = 0;
    217 	fip->fi_ino = ip->i_number;
    218 	LFS_IENTRY(ifp, fs, fip->fi_ino, bp);
    219 	fip->fi_version = ifp->if_version;
    220 	brelse(bp, 0);
    221 
    222 	lfs_gather(fs, sp, vp, lfs_match_data);
    223 	lfs_gather(fs, sp, vp, lfs_match_indir);
    224 	lfs_gather(fs, sp, vp, lfs_match_dindir);
    225 	lfs_gather(fs, sp, vp, lfs_match_tindir);
    226 
    227 	fip = sp->fip;
    228 	if (fip->fi_nblocks != 0) {
    229 		sp->fip = (FINFO *) ((caddr_t) fip + FINFOSIZE +
    230 		    sizeof(ulfs_daddr_t) * (fip->fi_nblocks));
    231 		sp->start_lbp = &sp->fip->fi_blocks[0];
    232 	} else {
    233 		sp->sum_bytes_left += FINFOSIZE;
    234 		--((SEGSUM *) (sp->segsum))->ss_nfinfo;
    235 	}
    236 }
    237 
    238 int
    239 lfs_writeinode(struct lfs * fs, struct segment * sp, struct inode * ip)
    240 {
    241 	struct ubuf *bp, *ibp;
    242 	struct ulfs1_dinode *cdp;
    243 	IFILE *ifp;
    244 	SEGUSE *sup;
    245 	daddr_t daddr;
    246 	ino_t ino;
    247 	int i, ndx, fsb = 0;
    248 	int redo_ifile = 0;
    249 	struct timespec ts;
    250 	int gotblk = 0;
    251 
    252 	/* Allocate a new inode block if necessary. */
    253 	if ((ip->i_number != LFS_IFILE_INUM || sp->idp == NULL) &&
    254 	    sp->ibp == NULL) {
    255 		/* Allocate a new segment if necessary. */
    256 		if (sp->seg_bytes_left < lfs_sb_getibsize(fs) ||
    257 		    sp->sum_bytes_left < sizeof(ulfs_daddr_t))
    258 			(void) lfs_writeseg(fs, sp);
    259 
    260 		/* Get next inode block. */
    261 		daddr = lfs_sb_getoffset(fs);
    262 		lfs_sb_addoffset(fs, lfs_btofsb(fs, lfs_sb_getibsize(fs)));
    263 		sp->ibp = *sp->cbpp++ =
    264 		    getblk(fs->lfs_devvp, LFS_FSBTODB(fs, daddr),
    265 		    lfs_sb_getibsize(fs));
    266 		sp->ibp->b_flags |= B_GATHERED;
    267 		gotblk++;
    268 
    269 		/* Zero out inode numbers */
    270 		for (i = 0; i < LFS_INOPB(fs); ++i)
    271 			((struct ulfs1_dinode *) sp->ibp->b_data)[i].di_inumber = 0;
    272 
    273 		++sp->start_bpp;
    274 		lfs_sb_subavail(fs, lfs_btofsb(fs, lfs_sb_getibsize(fs)));
    275 		/* Set remaining space counters. */
    276 		sp->seg_bytes_left -= lfs_sb_getibsize(fs);
    277 		sp->sum_bytes_left -= sizeof(ulfs_daddr_t);
    278 		ndx = lfs_sb_getsumsize(fs) / sizeof(ulfs_daddr_t) -
    279 		    sp->ninodes / LFS_INOPB(fs) - 1;
    280 		((ulfs_daddr_t *) (sp->segsum))[ndx] = daddr;
    281 	}
    282 	/* Update the inode times and copy the inode onto the inode page. */
    283 	ts.tv_nsec = 0;
    284 	ts.tv_sec = write_time;
    285 	/* XXX kludge --- don't redirty the ifile just to put times on it */
    286 	if (ip->i_number != LFS_IFILE_INUM)
    287 		LFS_ITIMES(ip, &ts, &ts, &ts);
    288 
    289 	/*
    290 	 * If this is the Ifile, and we've already written the Ifile in this
    291 	 * partial segment, just overwrite it (it's not on disk yet) and
    292 	 * continue.
    293 	 *
    294 	 * XXX we know that the bp that we get the second time around has
    295 	 * already been gathered.
    296 	 */
    297 	if (ip->i_number == LFS_IFILE_INUM && sp->idp) {
    298 		*(sp->idp) = *ip->i_din.ffs1_din;
    299 		ip->i_lfs_osize = ip->i_ffs1_size;
    300 		return 0;
    301 	}
    302 	bp = sp->ibp;
    303 	cdp = ((struct ulfs1_dinode *) bp->b_data) + (sp->ninodes % LFS_INOPB(fs));
    304 	*cdp = *ip->i_din.ffs1_din;
    305 
    306 	/* If all blocks are goig to disk, update the "size on disk" */
    307 	ip->i_lfs_osize = ip->i_ffs1_size;
    308 
    309 	if (ip->i_number == LFS_IFILE_INUM)	/* We know sp->idp == NULL */
    310 		sp->idp = ((struct ulfs1_dinode *) bp->b_data) +
    311 		    (sp->ninodes % LFS_INOPB(fs));
    312 	if (gotblk) {
    313 		LFS_LOCK_BUF(bp);
    314 		assert(!(bp->b_flags & B_INVAL));
    315 		brelse(bp, 0);
    316 	}
    317 	/* Increment inode count in segment summary block. */
    318 	++((SEGSUM *) (sp->segsum))->ss_ninos;
    319 
    320 	/* If this page is full, set flag to allocate a new page. */
    321 	if (++sp->ninodes % LFS_INOPB(fs) == 0)
    322 		sp->ibp = NULL;
    323 
    324 	/*
    325 	 * If updating the ifile, update the super-block.  Update the disk
    326 	 * address and access times for this inode in the ifile.
    327 	 */
    328 	ino = ip->i_number;
    329 	if (ino == LFS_IFILE_INUM) {
    330 		daddr = lfs_sb_getidaddr(fs);
    331 		lfs_sb_setidaddr(fs, LFS_DBTOFSB(fs, bp->b_blkno));
    332 		sbdirty();
    333 	} else {
    334 		LFS_IENTRY(ifp, fs, ino, ibp);
    335 		daddr = ifp->if_daddr;
    336 		ifp->if_daddr = LFS_DBTOFSB(fs, bp->b_blkno) + fsb;
    337 		(void)LFS_BWRITE_LOG(ibp);	/* Ifile */
    338 	}
    339 
    340 	/*
    341 	 * Account the inode: it no longer belongs to its former segment,
    342 	 * though it will not belong to the new segment until that segment
    343 	 * is actually written.
    344 	 */
    345 	if (daddr != LFS_UNUSED_DADDR) {
    346 		u_int32_t oldsn = lfs_dtosn(fs, daddr);
    347 		LFS_SEGENTRY(sup, fs, oldsn, bp);
    348 		sup->su_nbytes -= LFS_DINODE1_SIZE;
    349 		redo_ifile =
    350 		    (ino == LFS_IFILE_INUM && !(bp->b_flags & B_GATHERED));
    351 		if (redo_ifile)
    352 			fs->lfs_flags |= LFS_IFDIRTY;
    353 		LFS_WRITESEGENTRY(sup, fs, oldsn, bp);	/* Ifile */
    354 	}
    355 	return redo_ifile;
    356 }
    357 
    358 int
    359 lfs_gatherblock(struct segment * sp, struct ubuf * bp)
    360 {
    361 	struct lfs *fs;
    362 	int version;
    363 	int j, blksinblk;
    364 
    365 	/*
    366 	 * If full, finish this segment.  We may be doing I/O, so
    367 	 * release and reacquire the splbio().
    368 	 */
    369 	fs = sp->fs;
    370 	blksinblk = howmany(bp->b_bcount, lfs_sb_getbsize(fs));
    371 	if (sp->sum_bytes_left < sizeof(ulfs_daddr_t) * blksinblk ||
    372 	    sp->seg_bytes_left < bp->b_bcount) {
    373 		lfs_updatemeta(sp);
    374 
    375 		version = sp->fip->fi_version;
    376 		(void) lfs_writeseg(fs, sp);
    377 
    378 		sp->fip->fi_version = version;
    379 		sp->fip->fi_ino = VTOI(sp->vp)->i_number;
    380 		/* Add the current file to the segment summary. */
    381 		++((SEGSUM *) (sp->segsum))->ss_nfinfo;
    382 		sp->sum_bytes_left -= FINFOSIZE;
    383 
    384 		return 1;
    385 	}
    386 	/* Insert into the buffer list, update the FINFO block. */
    387 	bp->b_flags |= B_GATHERED;
    388 	/* bp->b_flags &= ~B_DONE; */
    389 
    390 	*sp->cbpp++ = bp;
    391 	for (j = 0; j < blksinblk; j++)
    392 		sp->fip->fi_blocks[sp->fip->fi_nblocks++] = bp->b_lblkno + j;
    393 
    394 	sp->sum_bytes_left -= sizeof(ulfs_daddr_t) * blksinblk;
    395 	sp->seg_bytes_left -= bp->b_bcount;
    396 	return 0;
    397 }
    398 
    399 int
    400 lfs_gather(struct lfs * fs, struct segment * sp, struct uvnode * vp, int (*match) (struct lfs *, struct ubuf *))
    401 {
    402 	struct ubuf *bp, *nbp;
    403 	int count = 0;
    404 
    405 	sp->vp = vp;
    406 loop:
    407 	for (bp = LIST_FIRST(&vp->v_dirtyblkhd); bp; bp = nbp) {
    408 		nbp = LIST_NEXT(bp, b_vnbufs);
    409 
    410 		assert(bp->b_flags & B_DELWRI);
    411 		if ((bp->b_flags & (B_BUSY | B_GATHERED)) || !match(fs, bp)) {
    412 			continue;
    413 		}
    414 		if (lfs_gatherblock(sp, bp)) {
    415 			goto loop;
    416 		}
    417 		count++;
    418 	}
    419 
    420 	lfs_updatemeta(sp);
    421 	sp->vp = NULL;
    422 	return count;
    423 }
    424 
    425 
    426 /*
    427  * Change the given block's address to ndaddr, finding its previous
    428  * location using ulfs_bmaparray().
    429  *
    430  * Account for this change in the segment table.
    431  */
    432 void
    433 lfs_update_single(struct lfs * fs, struct segment * sp, daddr_t lbn,
    434     ulfs_daddr_t ndaddr, int size)
    435 {
    436 	SEGUSE *sup;
    437 	struct ubuf *bp;
    438 	struct indir a[ULFS_NIADDR + 2], *ap;
    439 	struct inode *ip;
    440 	struct uvnode *vp;
    441 	daddr_t daddr, ooff;
    442 	int num, error;
    443 	int osize;
    444 	int frags, ofrags;
    445 
    446 	vp = sp->vp;
    447 	ip = VTOI(vp);
    448 
    449 	error = ulfs_bmaparray(fs, vp, lbn, &daddr, a, &num);
    450 	if (error)
    451 		errx(EXIT_FAILURE, "%s: ulfs_bmaparray returned %d looking up lbn %"
    452 		    PRId64 "", __func__, error, lbn);
    453 	if (daddr > 0)
    454 		daddr = LFS_DBTOFSB(fs, daddr);
    455 
    456 	frags = lfs_numfrags(fs, size);
    457 	switch (num) {
    458 	case 0:
    459 		ooff = ip->i_ffs1_db[lbn];
    460 		if (ooff == UNWRITTEN)
    461 			ip->i_ffs1_blocks += frags;
    462 		else {
    463 			/* possible fragment truncation or extension */
    464 			ofrags = lfs_btofsb(fs, ip->i_lfs_fragsize[lbn]);
    465 			ip->i_ffs1_blocks += (frags - ofrags);
    466 		}
    467 		ip->i_ffs1_db[lbn] = ndaddr;
    468 		break;
    469 	case 1:
    470 		ooff = ip->i_ffs1_ib[a[0].in_off];
    471 		if (ooff == UNWRITTEN)
    472 			ip->i_ffs1_blocks += frags;
    473 		ip->i_ffs1_ib[a[0].in_off] = ndaddr;
    474 		break;
    475 	default:
    476 		ap = &a[num - 1];
    477 		if (bread(vp, ap->in_lbn, lfs_sb_getbsize(fs), 0, &bp))
    478 			errx(EXIT_FAILURE, "%s: bread bno %" PRId64, __func__,
    479 			    ap->in_lbn);
    480 
    481 		ooff = ((ulfs_daddr_t *) bp->b_data)[ap->in_off];
    482 		if (ooff == UNWRITTEN)
    483 			ip->i_ffs1_blocks += frags;
    484 		((ulfs_daddr_t *) bp->b_data)[ap->in_off] = ndaddr;
    485 		(void) VOP_BWRITE(bp);
    486 	}
    487 
    488 	/*
    489 	 * Update segment usage information, based on old size
    490 	 * and location.
    491 	 */
    492 	if (daddr > 0) {
    493 		u_int32_t oldsn = lfs_dtosn(fs, daddr);
    494 		if (lbn >= 0 && lbn < ULFS_NDADDR)
    495 			osize = ip->i_lfs_fragsize[lbn];
    496 		else
    497 			osize = lfs_sb_getbsize(fs);
    498 		LFS_SEGENTRY(sup, fs, oldsn, bp);
    499 		sup->su_nbytes -= osize;
    500 		if (!(bp->b_flags & B_GATHERED))
    501 			fs->lfs_flags |= LFS_IFDIRTY;
    502 		LFS_WRITESEGENTRY(sup, fs, oldsn, bp);
    503 	}
    504 	/*
    505 	 * Now that this block has a new address, and its old
    506 	 * segment no longer owns it, we can forget about its
    507 	 * old size.
    508 	 */
    509 	if (lbn >= 0 && lbn < ULFS_NDADDR)
    510 		ip->i_lfs_fragsize[lbn] = size;
    511 }
    512 
    513 /*
    514  * Update the metadata that points to the blocks listed in the FINFO
    515  * array.
    516  */
    517 void
    518 lfs_updatemeta(struct segment * sp)
    519 {
    520 	struct ubuf *sbp;
    521 	struct lfs *fs;
    522 	struct uvnode *vp;
    523 	daddr_t lbn;
    524 	int i, nblocks, num;
    525 	int frags;
    526 	int bytesleft, size;
    527 
    528 	vp = sp->vp;
    529 	nblocks = &sp->fip->fi_blocks[sp->fip->fi_nblocks] - sp->start_lbp;
    530 
    531 	if (vp == NULL || nblocks == 0)
    532 		return;
    533 
    534 	/*
    535 	 * This count may be high due to oversize blocks from lfs_gop_write.
    536 	 * Correct for this. (XXX we should be able to keep track of these.)
    537 	 */
    538 	fs = sp->fs;
    539 	for (i = 0; i < nblocks; i++) {
    540 		if (sp->start_bpp[i] == NULL) {
    541 			printf("nblocks = %d, not %d\n", i, nblocks);
    542 			nblocks = i;
    543 			break;
    544 		}
    545 		num = howmany(sp->start_bpp[i]->b_bcount, lfs_sb_getbsize(fs));
    546 		nblocks -= num - 1;
    547 	}
    548 
    549 	/*
    550 	 * Sort the blocks.
    551 	 */
    552 	lfs_shellsort(sp->start_bpp, sp->start_lbp, nblocks, lfs_sb_getbsize(fs));
    553 
    554 	/*
    555 	 * Record the length of the last block in case it's a fragment.
    556 	 * If there are indirect blocks present, they sort last.  An
    557 	 * indirect block will be lfs_bsize and its presence indicates
    558 	 * that you cannot have fragments.
    559 	 */
    560 	sp->fip->fi_lastlength = ((sp->start_bpp[nblocks - 1]->b_bcount - 1) &
    561 	    lfs_sb_getbmask(fs)) + 1;
    562 
    563 	/*
    564 	 * Assign disk addresses, and update references to the logical
    565 	 * block and the segment usage information.
    566 	 */
    567 	for (i = nblocks; i--; ++sp->start_bpp) {
    568 		sbp = *sp->start_bpp;
    569 		lbn = *sp->start_lbp;
    570 
    571 		sbp->b_blkno = LFS_FSBTODB(fs, lfs_sb_getoffset(fs));
    572 
    573 		/*
    574 		 * If we write a frag in the wrong place, the cleaner won't
    575 		 * be able to correctly identify its size later, and the
    576 		 * segment will be uncleanable.	 (Even worse, it will assume
    577 		 * that the indirect block that actually ends the list
    578 		 * is of a smaller size!)
    579 		 */
    580 		if ((sbp->b_bcount & lfs_sb_getbmask(fs)) && i != 0)
    581 			errx(EXIT_FAILURE, "%s: fragment is not last block", __func__);
    582 
    583 		/*
    584 		 * For each subblock in this possibly oversized block,
    585 		 * update its address on disk.
    586 		 */
    587 		for (bytesleft = sbp->b_bcount; bytesleft > 0;
    588 		    bytesleft -= lfs_sb_getbsize(fs)) {
    589 			size = MIN(bytesleft, lfs_sb_getbsize(fs));
    590 			frags = lfs_numfrags(fs, size);
    591 			lbn = *sp->start_lbp++;
    592 			lfs_update_single(fs, sp, lbn, lfs_sb_getoffset(fs), size);
    593 			lfs_sb_addoffset(fs, frags);
    594 		}
    595 
    596 	}
    597 }
    598 
    599 /*
    600  * Start a new segment.
    601  */
    602 int
    603 lfs_initseg(struct lfs * fs)
    604 {
    605 	struct segment *sp;
    606 	SEGUSE *sup;
    607 	SEGSUM *ssp;
    608 	struct ubuf *bp, *sbp;
    609 	int repeat;
    610 
    611 	sp = fs->lfs_sp;
    612 
    613 	repeat = 0;
    614 
    615 	/* Advance to the next segment. */
    616 	if (!LFS_PARTIAL_FITS(fs)) {
    617 		/* lfs_avail eats the remaining space */
    618 		lfs_sb_subavail(fs, lfs_sb_getfsbpseg(fs) - (lfs_sb_getoffset(fs) -
    619 		    lfs_sb_getcurseg(fs)));
    620 		lfs_newseg(fs);
    621 		repeat = 1;
    622 		lfs_sb_setoffset(fs, lfs_sb_getcurseg(fs));
    623 
    624 		sp->seg_number = lfs_dtosn(fs, lfs_sb_getcurseg(fs));
    625 		sp->seg_bytes_left = lfs_fsbtob(fs, lfs_sb_getfsbpseg(fs));
    626 
    627 		/*
    628 		 * If the segment contains a superblock, update the offset
    629 		 * and summary address to skip over it.
    630 		 */
    631 		LFS_SEGENTRY(sup, fs, sp->seg_number, bp);
    632 		if (sup->su_flags & SEGUSE_SUPERBLOCK) {
    633 			lfs_sb_addoffset(fs, lfs_btofsb(fs, LFS_SBPAD));
    634 			sp->seg_bytes_left -= LFS_SBPAD;
    635 		}
    636 		brelse(bp, 0);
    637 		/* Segment zero could also contain the labelpad */
    638 		if (lfs_sb_getversion(fs) > 1 && sp->seg_number == 0 &&
    639 		    lfs_sb_gets0addr(fs) < lfs_btofsb(fs, LFS_LABELPAD)) {
    640 			lfs_sb_addoffset(fs, lfs_btofsb(fs, LFS_LABELPAD) - lfs_sb_gets0addr(fs));
    641 			sp->seg_bytes_left -= LFS_LABELPAD - lfs_fsbtob(fs, lfs_sb_gets0addr(fs));
    642 		}
    643 	} else {
    644 		sp->seg_number = lfs_dtosn(fs, lfs_sb_getcurseg(fs));
    645 		sp->seg_bytes_left = lfs_fsbtob(fs, lfs_sb_getfsbpseg(fs) -
    646 		    (lfs_sb_getoffset(fs) - lfs_sb_getcurseg(fs)));
    647 	}
    648 	lfs_sb_setlastpseg(fs, lfs_sb_getoffset(fs));
    649 
    650 	sp->fs = fs;
    651 	sp->ibp = NULL;
    652 	sp->idp = NULL;
    653 	sp->ninodes = 0;
    654 	sp->ndupino = 0;
    655 
    656 	/* Get a new buffer for SEGSUM and enter it into the buffer list. */
    657 	sp->cbpp = sp->bpp;
    658 	sbp = *sp->cbpp = getblk(fs->lfs_devvp,
    659 	    LFS_FSBTODB(fs, lfs_sb_getoffset(fs)), lfs_sb_getsumsize(fs));
    660 	sp->segsum = sbp->b_data;
    661 	memset(sp->segsum, 0, lfs_sb_getsumsize(fs));
    662 	sp->start_bpp = ++sp->cbpp;
    663 	lfs_sb_addoffset(fs, lfs_btofsb(fs, lfs_sb_getsumsize(fs)));
    664 
    665 	/* Set point to SEGSUM, initialize it. */
    666 	ssp = sp->segsum;
    667 	ssp->ss_next = lfs_sb_getnextseg(fs);
    668 	ssp->ss_nfinfo = ssp->ss_ninos = 0;
    669 	ssp->ss_magic = SS_MAGIC;
    670 
    671 	/* Set pointer to first FINFO, initialize it. */
    672 	sp->fip = (struct finfo *) ((caddr_t) sp->segsum + SEGSUM_SIZE(fs));
    673 	sp->fip->fi_nblocks = 0;
    674 	sp->start_lbp = &sp->fip->fi_blocks[0];
    675 	sp->fip->fi_lastlength = 0;
    676 
    677 	sp->seg_bytes_left -= lfs_sb_getsumsize(fs);
    678 	sp->sum_bytes_left = lfs_sb_getsumsize(fs) - SEGSUM_SIZE(fs);
    679 
    680 	LFS_LOCK_BUF(sbp);
    681 	brelse(sbp, 0);
    682 	return repeat;
    683 }
    684 
    685 /*
    686  * Return the next segment to write.
    687  */
    688 void
    689 lfs_newseg(struct lfs * fs)
    690 {
    691 	CLEANERINFO *cip;
    692 	SEGUSE *sup;
    693 	struct ubuf *bp;
    694 	int curseg, isdirty, sn;
    695 
    696 	LFS_SEGENTRY(sup, fs, lfs_dtosn(fs, lfs_sb_getnextseg(fs)), bp);
    697 	sup->su_flags |= SEGUSE_DIRTY | SEGUSE_ACTIVE;
    698 	sup->su_nbytes = 0;
    699 	sup->su_nsums = 0;
    700 	sup->su_ninos = 0;
    701 	LFS_WRITESEGENTRY(sup, fs, lfs_dtosn(fs, lfs_sb_getnextseg(fs)), bp);
    702 
    703 	LFS_CLEANERINFO(cip, fs, bp);
    704 	--cip->clean;
    705 	++cip->dirty;
    706 	lfs_sb_setnclean(fs, cip->clean);
    707 	LFS_SYNC_CLEANERINFO(cip, fs, bp, 1);
    708 
    709 	lfs_sb_setlastseg(fs, lfs_sb_getcurseg(fs));
    710 	lfs_sb_setcurseg(fs, lfs_sb_getnextseg(fs));
    711 	for (sn = curseg = lfs_dtosn(fs, lfs_sb_getcurseg(fs)) + lfs_sb_getinterleave(fs);;) {
    712 		sn = (sn + 1) % lfs_sb_getnseg(fs);
    713 		if (sn == curseg)
    714 			errx(EXIT_FAILURE, "%s: no clean segments", __func__);
    715 		LFS_SEGENTRY(sup, fs, sn, bp);
    716 		isdirty = sup->su_flags & SEGUSE_DIRTY;
    717 		brelse(bp, 0);
    718 
    719 		if (!isdirty)
    720 			break;
    721 	}
    722 
    723 	++fs->lfs_nactive;
    724 	lfs_sb_setnextseg(fs, lfs_sntod(fs, sn));
    725 }
    726 
    727 
    728 int
    729 lfs_writeseg(struct lfs * fs, struct segment * sp)
    730 {
    731 	struct ubuf **bpp, *bp;
    732 	SEGUSE *sup;
    733 	SEGSUM *ssp;
    734 	char *datap, *dp;
    735 	int i;
    736 	int do_again, nblocks, byteoffset;
    737 	size_t el_size;
    738 	u_short ninos;
    739 	struct uvnode *devvp;
    740 
    741 	/*
    742 	 * If there are no buffers other than the segment summary to write
    743 	 * and it is not a checkpoint, don't do anything.  On a checkpoint,
    744 	 * even if there aren't any buffers, you need to write the superblock.
    745 	 */
    746 	nblocks = sp->cbpp - sp->bpp;
    747 #if 0
    748 	printf("write %d blocks at 0x%x\n",
    749 		nblocks, (int)LFS_DBTOFSB(fs, (*sp->bpp)->b_blkno));
    750 #endif
    751 	if (nblocks == 1)
    752 		return 0;
    753 
    754 	devvp = fs->lfs_devvp;
    755 
    756 	/* Update the segment usage information. */
    757 	LFS_SEGENTRY(sup, fs, sp->seg_number, bp);
    758 	sup->su_flags |= SEGUSE_DIRTY | SEGUSE_ACTIVE;
    759 
    760 	/* Loop through all blocks, except the segment summary. */
    761 	for (bpp = sp->bpp; ++bpp < sp->cbpp;) {
    762 		if ((*bpp)->b_vp != devvp) {
    763 			sup->su_nbytes += (*bpp)->b_bcount;
    764 		}
    765 		assert(lfs_dtosn(fs, LFS_DBTOFSB(fs, (*bpp)->b_blkno)) == sp->seg_number);
    766 	}
    767 
    768 	ssp = (SEGSUM *) sp->segsum;
    769 	ssp->ss_flags |= SS_RFW;
    770 
    771 	ninos = (ssp->ss_ninos + LFS_INOPB(fs) - 1) / LFS_INOPB(fs);
    772 	sup->su_nbytes += ssp->ss_ninos * LFS_DINODE1_SIZE;
    773 
    774 	if (lfs_sb_getversion(fs) == 1)
    775 		sup->su_olastmod = write_time;
    776 	else
    777 		sup->su_lastmod = write_time;
    778 	sup->su_ninos += ninos;
    779 	++sup->su_nsums;
    780 	lfs_sb_adddmeta(fs, (lfs_btofsb(fs, lfs_sb_getsumsize(fs)) + lfs_btofsb(fs, ninos *
    781 		lfs_sb_getibsize(fs))));
    782 	lfs_sb_subavail(fs, lfs_btofsb(fs, lfs_sb_getsumsize(fs)));
    783 
    784 	do_again = !(bp->b_flags & B_GATHERED);
    785 	LFS_WRITESEGENTRY(sup, fs, sp->seg_number, bp);	/* Ifile */
    786 
    787 	/*
    788 	 * Compute checksum across data and then across summary; the first
    789 	 * block (the summary block) is skipped.  Set the create time here
    790 	 * so that it's guaranteed to be later than the inode mod times.
    791 	 */
    792 	if (lfs_sb_getversion(fs) == 1)
    793 		el_size = sizeof(u_long);
    794 	else
    795 		el_size = sizeof(u_int32_t);
    796 	datap = dp = emalloc(nblocks * el_size);
    797 	for (bpp = sp->bpp, i = nblocks - 1; i--;) {
    798 		++bpp;
    799 		/* Loop through gop_write cluster blocks */
    800 		for (byteoffset = 0; byteoffset < (*bpp)->b_bcount;
    801 		    byteoffset += lfs_sb_getbsize(fs)) {
    802 			memcpy(dp, (*bpp)->b_data + byteoffset, el_size);
    803 			dp += el_size;
    804 		}
    805 		bremfree(*bpp);
    806 		(*bpp)->b_flags |= B_BUSY;
    807 	}
    808 	if (lfs_sb_getversion(fs) == 1)
    809 		ssp->ss_ocreate = write_time;
    810 	else {
    811 		ssp->ss_create = write_time;
    812 		lfs_sb_addserial(fs, 1);
    813 		ssp->ss_serial = lfs_sb_getserial(fs);
    814 		ssp->ss_ident = lfs_sb_getident(fs);
    815 	}
    816 	/* Set the summary block busy too */
    817 	bremfree(*(sp->bpp));
    818 	(*(sp->bpp))->b_flags |= B_BUSY;
    819 
    820 	ssp->ss_datasum = cksum(datap, (nblocks - 1) * el_size);
    821 	ssp->ss_sumsum =
    822 	    cksum(&ssp->ss_datasum, lfs_sb_getsumsize(fs) - sizeof(ssp->ss_sumsum));
    823 	free(datap);
    824 	datap = dp = NULL;
    825 	lfs_sb_subbfree(fs, (lfs_btofsb(fs, ninos * lfs_sb_getibsize(fs)) +
    826 	    lfs_btofsb(fs, lfs_sb_getsumsize(fs))));
    827 
    828 	if (devvp == NULL)
    829 		errx(EXIT_FAILURE, "devvp is NULL");
    830 	for (bpp = sp->bpp, i = nblocks; i; bpp++, i--) {
    831 		bp = *bpp;
    832 #if 0
    833 		printf("i = %d, bp = %p, flags %lx, bn = %" PRIx64 "\n",
    834 		       nblocks - i, bp, bp->b_flags, bp->b_blkno);
    835 		printf("  vp = %p\n", bp->b_vp);
    836 		if (bp->b_vp != fs->lfs_devvp)
    837 			printf("  ino = %d lbn = %" PRId64 "\n",
    838 			       VTOI(bp->b_vp)->i_number, bp->b_lblkno);
    839 #endif
    840 		if (bp->b_vp == fs->lfs_devvp)
    841 			written_dev += bp->b_bcount;
    842 		else {
    843 			if (bp->b_lblkno >= 0)
    844 				written_data += bp->b_bcount;
    845 			else
    846 				written_indir += bp->b_bcount;
    847 		}
    848 		bp->b_flags &= ~(B_DELWRI | B_READ | B_GATHERED | B_ERROR |
    849 				 B_LOCKED);
    850 		bwrite(bp);
    851 		written_bytes += bp->b_bcount;
    852 	}
    853 	written_inodes += ninos;
    854 
    855 	return (lfs_initseg(fs) || do_again);
    856 }
    857 
    858 /*
    859  * Our own copy of shellsort.  XXX use qsort or heapsort.
    860  */
    861 void
    862 lfs_shellsort(struct ubuf ** bp_array, ulfs_daddr_t * lb_array, int nmemb, int size)
    863 {
    864 	static int __rsshell_increments[] = {4, 1, 0};
    865 	int incr, *incrp, t1, t2;
    866 	struct ubuf *bp_temp;
    867 
    868 	for (incrp = __rsshell_increments; (incr = *incrp++) != 0;)
    869 		for (t1 = incr; t1 < nmemb; ++t1)
    870 			for (t2 = t1 - incr; t2 >= 0;)
    871 				if ((u_int32_t) bp_array[t2]->b_lblkno >
    872 				    (u_int32_t) bp_array[t2 + incr]->b_lblkno) {
    873 					bp_temp = bp_array[t2];
    874 					bp_array[t2] = bp_array[t2 + incr];
    875 					bp_array[t2 + incr] = bp_temp;
    876 					t2 -= incr;
    877 				} else
    878 					break;
    879 
    880 	/* Reform the list of logical blocks */
    881 	incr = 0;
    882 	for (t1 = 0; t1 < nmemb; t1++) {
    883 		for (t2 = 0; t2 * size < bp_array[t1]->b_bcount; t2++) {
    884 			lb_array[incr++] = bp_array[t1]->b_lblkno + t2;
    885 		}
    886 	}
    887 }
    888 
    889 
    890 /*
    891  * lfs_seglock --
    892  *	Single thread the segment writer.
    893  */
    894 int
    895 lfs_seglock(struct lfs * fs, unsigned long flags)
    896 {
    897 	struct segment *sp;
    898 	size_t allocsize;
    899 
    900 	if (fs->lfs_seglock) {
    901 		++fs->lfs_seglock;
    902 		fs->lfs_sp->seg_flags |= flags;
    903 		return 0;
    904 	}
    905 	fs->lfs_seglock = 1;
    906 
    907 	sp = fs->lfs_sp = emalloc(sizeof(*sp));
    908 	allocsize = lfs_sb_getssize(fs) * sizeof(struct ubuf *);
    909 	sp->bpp = emalloc(allocsize);
    910 	if (!sp->bpp)
    911 		err(!preen, "Could not allocate %zu bytes", allocsize);
    912 	sp->seg_flags = flags;
    913 	sp->vp = NULL;
    914 	sp->seg_iocount = 0;
    915 	(void) lfs_initseg(fs);
    916 
    917 	return 0;
    918 }
    919 
    920 /*
    921  * lfs_segunlock --
    922  *	Single thread the segment writer.
    923  */
    924 void
    925 lfs_segunlock(struct lfs * fs)
    926 {
    927 	struct segment *sp;
    928 	struct ubuf *bp;
    929 
    930 	sp = fs->lfs_sp;
    931 
    932 	if (fs->lfs_seglock == 1) {
    933 		if (sp->bpp != sp->cbpp) {
    934 			/* Free allocated segment summary */
    935 			lfs_sb_suboffset(fs, lfs_btofsb(fs, lfs_sb_getsumsize(fs)));
    936 			bp = *sp->bpp;
    937 			bremfree(bp);
    938 			bp->b_flags |= B_DONE | B_INVAL;
    939 			bp->b_flags &= ~B_DELWRI;
    940 			reassignbuf(bp, bp->b_vp);
    941 			bp->b_flags |= B_BUSY; /* XXX */
    942 			brelse(bp, 0);
    943 		} else
    944 			printf("unlock to 0 with no summary");
    945 
    946 		free(sp->bpp);
    947 		sp->bpp = NULL;
    948 		free(sp);
    949 		fs->lfs_sp = NULL;
    950 
    951 		fs->lfs_nactive = 0;
    952 
    953 		/* Since we *know* everything's on disk, write both sbs */
    954 		lfs_writesuper(fs, lfs_sb_getsboff(fs, 0));
    955 		lfs_writesuper(fs, lfs_sb_getsboff(fs, 1));
    956 
    957 		--fs->lfs_seglock;
    958 		fs->lfs_lockpid = 0;
    959 	} else if (fs->lfs_seglock == 0) {
    960 		errx(EXIT_FAILURE, "Seglock not held");
    961 	} else {
    962 		--fs->lfs_seglock;
    963 	}
    964 }
    965 
    966 int
    967 lfs_writevnodes(struct lfs *fs, struct segment *sp, int op)
    968 {
    969 	struct inode *ip;
    970 	struct uvnode *vp;
    971 	int inodes_written = 0;
    972 
    973 	LIST_FOREACH(vp, &vnodelist, v_mntvnodes) {
    974 		if (vp->v_bmap_op != lfs_vop_bmap)
    975 			continue;
    976 
    977 		ip = VTOI(vp);
    978 
    979 		if ((op == VN_DIROP && !(vp->v_uflag & VU_DIROP)) ||
    980 		    (op != VN_DIROP && (vp->v_uflag & VU_DIROP))) {
    981 			continue;
    982 		}
    983 		/*
    984 		 * Write the inode/file if dirty and it's not the IFILE.
    985 		 */
    986 		if (ip->i_flag & IN_ALLMOD || !LIST_EMPTY(&vp->v_dirtyblkhd)) {
    987 			if (ip->i_number != LFS_IFILE_INUM)
    988 				lfs_writefile(fs, sp, vp);
    989 			(void) lfs_writeinode(fs, sp, ip);
    990 			inodes_written++;
    991 		}
    992 	}
    993 	return inodes_written;
    994 }
    995 
    996 void
    997 lfs_writesuper(struct lfs *fs, ulfs_daddr_t daddr)
    998 {
    999 	struct ubuf *bp;
   1000 
   1001 	/* Set timestamp of this version of the superblock */
   1002 	if (lfs_sb_getversion(fs) == 1)
   1003 		lfs_sb_setotstamp(fs, write_time);
   1004 	lfs_sb_settstamp(fs, write_time);
   1005 
   1006 	/* Checksum the superblock and copy it into a buffer. */
   1007 	lfs_sb_setcksum(fs, lfs_sb_cksum(&(fs->lfs_dlfs)));
   1008 	assert(daddr > 0);
   1009 	bp = getblk(fs->lfs_devvp, LFS_FSBTODB(fs, daddr), LFS_SBPAD);
   1010 	memset(bp->b_data + sizeof(struct dlfs), 0,
   1011 	    LFS_SBPAD - sizeof(struct dlfs));
   1012 	*(struct dlfs *) bp->b_data = fs->lfs_dlfs;
   1013 
   1014 	bwrite(bp);
   1015 }
   1016