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