xref: /netbsd-src/sbin/fsck_lfs/segwrite.c (revision b1c86f5f087524e68db12794ee9c3e3da1ab17a0)
1 /* $NetBSD: segwrite.c,v 1.20 2010/02/16 23:20:30 mlelstv 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  *
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 #include <ufs/ufs/inode.h>
71 #include <ufs/ufs/ufsmount.h>
72 
73 /* Override certain things to make <ufs/lfs/lfs.h> work */
74 #define vnode uvnode
75 #define buf ubuf
76 #define panic call_panic
77 
78 #include <ufs/lfs/lfs.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 - NDADDR) % 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 - NDADDR) % 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 - NDADDR) % 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 || fs->lfs_idaddr <= 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, (ufs_daddr_t)btofsb(fs, written_bytes));
180 	printf("wrote %" PRId64 " bytes data (%" PRId32 " fsb)\n",
181 		written_data, (ufs_daddr_t)btofsb(fs, written_data));
182 	printf("wrote %" PRId64 " bytes indir (%" PRId32 " fsb)\n",
183 		written_indir, (ufs_daddr_t)btofsb(fs, written_indir));
184 	printf("wrote %" PRId64 " bytes dev (%" PRId32 " fsb)\n",
185 		written_dev, (ufs_daddr_t)btofsb(fs, written_dev));
186 	printf("wrote %d inodes (%" PRId32 " fsb)\n",
187 		written_inodes, 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 < fs->lfs_bsize ||
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(ufs_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 ufs1_dinode *cdp;
243 	IFILE *ifp;
244 	SEGUSE *sup;
245 	daddr_t daddr;
246 	ino_t ino;
247 	int error, 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 < fs->lfs_ibsize ||
257 		    sp->sum_bytes_left < sizeof(ufs_daddr_t))
258 			(void) lfs_writeseg(fs, sp);
259 
260 		/* Get next inode block. */
261 		daddr = fs->lfs_offset;
262 		fs->lfs_offset += btofsb(fs, fs->lfs_ibsize);
263 		sp->ibp = *sp->cbpp++ =
264 		    getblk(fs->lfs_devvp, fsbtodb(fs, daddr),
265 		    fs->lfs_ibsize);
266 		sp->ibp->b_flags |= B_GATHERED;
267 		gotblk++;
268 
269 		/* Zero out inode numbers */
270 		for (i = 0; i < INOPB(fs); ++i)
271 			((struct ufs1_dinode *) sp->ibp->b_data)[i].di_inumber = 0;
272 
273 		++sp->start_bpp;
274 		fs->lfs_avail -= btofsb(fs, fs->lfs_ibsize);
275 		/* Set remaining space counters. */
276 		sp->seg_bytes_left -= fs->lfs_ibsize;
277 		sp->sum_bytes_left -= sizeof(ufs_daddr_t);
278 		ndx = fs->lfs_sumsize / sizeof(ufs_daddr_t) -
279 		    sp->ninodes / INOPB(fs) - 1;
280 		((ufs_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 ufs1_dinode *) bp->b_data) + (sp->ninodes % 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 ufs1_dinode *) bp->b_data) +
311 		    (sp->ninodes % 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 % 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 = fs->lfs_idaddr;
331 		fs->lfs_idaddr = 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 = dbtofsb(fs, bp->b_blkno) + fsb;
337 		error = 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 = dtosn(fs, daddr);
347 		LFS_SEGENTRY(sup, fs, oldsn, bp);
348 		sup->su_nbytes -= 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, fs->lfs_bsize);
371 	if (sp->sum_bytes_left < sizeof(ufs_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(ufs_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 ufs_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     ufs_daddr_t ndaddr, int size)
435 {
436 	SEGUSE *sup;
437 	struct ubuf *bp;
438 	struct indir a[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 = ufs_bmaparray(fs, vp, lbn, &daddr, a, &num);
450 	if (error)
451 		errx(1, "lfs_updatemeta: ufs_bmaparray returned %d looking up lbn %" PRId64 "\n", error, lbn);
452 	if (daddr > 0)
453 		daddr = dbtofsb(fs, daddr);
454 
455 	frags = numfrags(fs, size);
456 	switch (num) {
457 	case 0:
458 		ooff = ip->i_ffs1_db[lbn];
459 		if (ooff == UNWRITTEN)
460 			ip->i_ffs1_blocks += frags;
461 		else {
462 			/* possible fragment truncation or extension */
463 			ofrags = btofsb(fs, ip->i_lfs_fragsize[lbn]);
464 			ip->i_ffs1_blocks += (frags - ofrags);
465 		}
466 		ip->i_ffs1_db[lbn] = ndaddr;
467 		break;
468 	case 1:
469 		ooff = ip->i_ffs1_ib[a[0].in_off];
470 		if (ooff == UNWRITTEN)
471 			ip->i_ffs1_blocks += frags;
472 		ip->i_ffs1_ib[a[0].in_off] = ndaddr;
473 		break;
474 	default:
475 		ap = &a[num - 1];
476 		if (bread(vp, ap->in_lbn, fs->lfs_bsize, NULL, 0, &bp))
477 			errx(1, "lfs_updatemeta: bread bno %" PRId64,
478 			    ap->in_lbn);
479 
480 		ooff = ((ufs_daddr_t *) bp->b_data)[ap->in_off];
481 		if (ooff == UNWRITTEN)
482 			ip->i_ffs1_blocks += frags;
483 		((ufs_daddr_t *) bp->b_data)[ap->in_off] = ndaddr;
484 		(void) VOP_BWRITE(bp);
485 	}
486 
487 	/*
488 	 * Update segment usage information, based on old size
489 	 * and location.
490 	 */
491 	if (daddr > 0) {
492 		u_int32_t oldsn = dtosn(fs, daddr);
493 		if (lbn >= 0 && lbn < NDADDR)
494 			osize = ip->i_lfs_fragsize[lbn];
495 		else
496 			osize = fs->lfs_bsize;
497 		LFS_SEGENTRY(sup, fs, oldsn, bp);
498 		sup->su_nbytes -= osize;
499 		if (!(bp->b_flags & B_GATHERED))
500 			fs->lfs_flags |= LFS_IFDIRTY;
501 		LFS_WRITESEGENTRY(sup, fs, oldsn, bp);
502 	}
503 	/*
504 	 * Now that this block has a new address, and its old
505 	 * segment no longer owns it, we can forget about its
506 	 * old size.
507 	 */
508 	if (lbn >= 0 && lbn < NDADDR)
509 		ip->i_lfs_fragsize[lbn] = size;
510 }
511 
512 /*
513  * Update the metadata that points to the blocks listed in the FINFO
514  * array.
515  */
516 void
517 lfs_updatemeta(struct segment * sp)
518 {
519 	struct ubuf *sbp;
520 	struct lfs *fs;
521 	struct uvnode *vp;
522 	daddr_t lbn;
523 	int i, nblocks, num;
524 	int frags;
525 	int bytesleft, size;
526 
527 	vp = sp->vp;
528 	nblocks = &sp->fip->fi_blocks[sp->fip->fi_nblocks] - sp->start_lbp;
529 
530 	if (vp == NULL || nblocks == 0)
531 		return;
532 
533 	/*
534 	 * This count may be high due to oversize blocks from lfs_gop_write.
535 	 * Correct for this. (XXX we should be able to keep track of these.)
536 	 */
537 	fs = sp->fs;
538 	for (i = 0; i < nblocks; i++) {
539 		if (sp->start_bpp[i] == NULL) {
540 			printf("nblocks = %d, not %d\n", i, nblocks);
541 			nblocks = i;
542 			break;
543 		}
544 		num = howmany(sp->start_bpp[i]->b_bcount, fs->lfs_bsize);
545 		nblocks -= num - 1;
546 	}
547 
548 	/*
549 	 * Sort the blocks.
550 	 */
551 	lfs_shellsort(sp->start_bpp, sp->start_lbp, nblocks, fs->lfs_bsize);
552 
553 	/*
554 	 * Record the length of the last block in case it's a fragment.
555 	 * If there are indirect blocks present, they sort last.  An
556 	 * indirect block will be lfs_bsize and its presence indicates
557 	 * that you cannot have fragments.
558 	 */
559 	sp->fip->fi_lastlength = ((sp->start_bpp[nblocks - 1]->b_bcount - 1) &
560 	    fs->lfs_bmask) + 1;
561 
562 	/*
563 	 * Assign disk addresses, and update references to the logical
564 	 * block and the segment usage information.
565 	 */
566 	for (i = nblocks; i--; ++sp->start_bpp) {
567 		sbp = *sp->start_bpp;
568 		lbn = *sp->start_lbp;
569 
570 		sbp->b_blkno = fsbtodb(fs, fs->lfs_offset);
571 
572 		/*
573 		 * If we write a frag in the wrong place, the cleaner won't
574 		 * be able to correctly identify its size later, and the
575 		 * segment will be uncleanable.	 (Even worse, it will assume
576 		 * that the indirect block that actually ends the list
577 		 * is of a smaller size!)
578 		 */
579 		if ((sbp->b_bcount & fs->lfs_bmask) && i != 0)
580 			errx(1, "lfs_updatemeta: fragment is not last block");
581 
582 		/*
583 		 * For each subblock in this possibly oversized block,
584 		 * update its address on disk.
585 		 */
586 		for (bytesleft = sbp->b_bcount; bytesleft > 0;
587 		    bytesleft -= fs->lfs_bsize) {
588 			size = MIN(bytesleft, fs->lfs_bsize);
589 			frags = numfrags(fs, size);
590 			lbn = *sp->start_lbp++;
591 			lfs_update_single(fs, sp, lbn, fs->lfs_offset, size);
592 			fs->lfs_offset += frags;
593 		}
594 
595 	}
596 }
597 
598 /*
599  * Start a new segment.
600  */
601 int
602 lfs_initseg(struct lfs * fs)
603 {
604 	struct segment *sp;
605 	SEGUSE *sup;
606 	SEGSUM *ssp;
607 	struct ubuf *bp, *sbp;
608 	int repeat;
609 
610 	sp = fs->lfs_sp;
611 
612 	repeat = 0;
613 
614 	/* Advance to the next segment. */
615 	if (!LFS_PARTIAL_FITS(fs)) {
616 		/* lfs_avail eats the remaining space */
617 		fs->lfs_avail -= fs->lfs_fsbpseg - (fs->lfs_offset -
618 		    fs->lfs_curseg);
619 		lfs_newseg(fs);
620 		repeat = 1;
621 		fs->lfs_offset = fs->lfs_curseg;
622 
623 		sp->seg_number = dtosn(fs, fs->lfs_curseg);
624 		sp->seg_bytes_left = fsbtob(fs, fs->lfs_fsbpseg);
625 
626 		/*
627 		 * If the segment contains a superblock, update the offset
628 		 * and summary address to skip over it.
629 		 */
630 		LFS_SEGENTRY(sup, fs, sp->seg_number, bp);
631 		if (sup->su_flags & SEGUSE_SUPERBLOCK) {
632 			fs->lfs_offset += btofsb(fs, LFS_SBPAD);
633 			sp->seg_bytes_left -= LFS_SBPAD;
634 		}
635 		brelse(bp, 0);
636 		/* Segment zero could also contain the labelpad */
637 		if (fs->lfs_version > 1 && sp->seg_number == 0 &&
638 		    fs->lfs_start < btofsb(fs, LFS_LABELPAD)) {
639 			fs->lfs_offset += btofsb(fs, LFS_LABELPAD) - fs->lfs_start;
640 			sp->seg_bytes_left -= LFS_LABELPAD - fsbtob(fs, fs->lfs_start);
641 		}
642 	} else {
643 		sp->seg_number = dtosn(fs, fs->lfs_curseg);
644 		sp->seg_bytes_left = fsbtob(fs, fs->lfs_fsbpseg -
645 		    (fs->lfs_offset - fs->lfs_curseg));
646 	}
647 	fs->lfs_lastpseg = fs->lfs_offset;
648 
649 	sp->fs = fs;
650 	sp->ibp = NULL;
651 	sp->idp = NULL;
652 	sp->ninodes = 0;
653 	sp->ndupino = 0;
654 
655 	/* Get a new buffer for SEGSUM and enter it into the buffer list. */
656 	sp->cbpp = sp->bpp;
657 	sbp = *sp->cbpp = getblk(fs->lfs_devvp,
658 	    fsbtodb(fs, fs->lfs_offset), fs->lfs_sumsize);
659 	sp->segsum = sbp->b_data;
660 	memset(sp->segsum, 0, fs->lfs_sumsize);
661 	sp->start_bpp = ++sp->cbpp;
662 	fs->lfs_offset += btofsb(fs, fs->lfs_sumsize);
663 
664 	/* Set point to SEGSUM, initialize it. */
665 	ssp = sp->segsum;
666 	ssp->ss_next = fs->lfs_nextseg;
667 	ssp->ss_nfinfo = ssp->ss_ninos = 0;
668 	ssp->ss_magic = SS_MAGIC;
669 
670 	/* Set pointer to first FINFO, initialize it. */
671 	sp->fip = (struct finfo *) ((caddr_t) sp->segsum + SEGSUM_SIZE(fs));
672 	sp->fip->fi_nblocks = 0;
673 	sp->start_lbp = &sp->fip->fi_blocks[0];
674 	sp->fip->fi_lastlength = 0;
675 
676 	sp->seg_bytes_left -= fs->lfs_sumsize;
677 	sp->sum_bytes_left = fs->lfs_sumsize - SEGSUM_SIZE(fs);
678 
679 	LFS_LOCK_BUF(sbp);
680 	brelse(sbp, 0);
681 	return repeat;
682 }
683 
684 /*
685  * Return the next segment to write.
686  */
687 void
688 lfs_newseg(struct lfs * fs)
689 {
690 	CLEANERINFO *cip;
691 	SEGUSE *sup;
692 	struct ubuf *bp;
693 	int curseg, isdirty, sn;
694 
695 	LFS_SEGENTRY(sup, fs, dtosn(fs, fs->lfs_nextseg), bp);
696 	sup->su_flags |= SEGUSE_DIRTY | SEGUSE_ACTIVE;
697 	sup->su_nbytes = 0;
698 	sup->su_nsums = 0;
699 	sup->su_ninos = 0;
700 	LFS_WRITESEGENTRY(sup, fs, dtosn(fs, fs->lfs_nextseg), bp);
701 
702 	LFS_CLEANERINFO(cip, fs, bp);
703 	--cip->clean;
704 	++cip->dirty;
705 	fs->lfs_nclean = cip->clean;
706 	LFS_SYNC_CLEANERINFO(cip, fs, bp, 1);
707 
708 	fs->lfs_lastseg = fs->lfs_curseg;
709 	fs->lfs_curseg = fs->lfs_nextseg;
710 	for (sn = curseg = dtosn(fs, fs->lfs_curseg) + fs->lfs_interleave;;) {
711 		sn = (sn + 1) % fs->lfs_nseg;
712 		if (sn == curseg)
713 			errx(1, "lfs_nextseg: no clean segments");
714 		LFS_SEGENTRY(sup, fs, sn, bp);
715 		isdirty = sup->su_flags & SEGUSE_DIRTY;
716 		brelse(bp, 0);
717 
718 		if (!isdirty)
719 			break;
720 	}
721 
722 	++fs->lfs_nactive;
723 	fs->lfs_nextseg = sntod(fs, sn);
724 }
725 
726 
727 int
728 lfs_writeseg(struct lfs * fs, struct segment * sp)
729 {
730 	struct ubuf **bpp, *bp;
731 	SEGUSE *sup;
732 	SEGSUM *ssp;
733 	char *datap, *dp;
734 	int i;
735 	int do_again, nblocks, byteoffset;
736 	size_t el_size;
737 	u_short ninos;
738 	struct uvnode *devvp;
739 
740 	/*
741 	 * If there are no buffers other than the segment summary to write
742 	 * and it is not a checkpoint, don't do anything.  On a checkpoint,
743 	 * even if there aren't any buffers, you need to write the superblock.
744 	 */
745 	nblocks = sp->cbpp - sp->bpp;
746 #if 0
747 	printf("write %d blocks at 0x%x\n",
748 		nblocks, (int)dbtofsb(fs, (*sp->bpp)->b_blkno));
749 #endif
750 	if (nblocks == 1)
751 		return 0;
752 
753 	devvp = fs->lfs_devvp;
754 
755 	/* Update the segment usage information. */
756 	LFS_SEGENTRY(sup, fs, sp->seg_number, bp);
757 	sup->su_flags |= SEGUSE_DIRTY | SEGUSE_ACTIVE;
758 
759 	/* Loop through all blocks, except the segment summary. */
760 	for (bpp = sp->bpp; ++bpp < sp->cbpp;) {
761 		if ((*bpp)->b_vp != devvp) {
762 			sup->su_nbytes += (*bpp)->b_bcount;
763 		}
764 		assert(dtosn(fs, dbtofsb(fs, (*bpp)->b_blkno)) == sp->seg_number);
765 	}
766 
767 	ssp = (SEGSUM *) sp->segsum;
768 	ssp->ss_flags |= SS_RFW;
769 
770 	ninos = (ssp->ss_ninos + INOPB(fs) - 1) / INOPB(fs);
771 	sup->su_nbytes += ssp->ss_ninos * DINODE1_SIZE;
772 
773 	if (fs->lfs_version == 1)
774 		sup->su_olastmod = write_time;
775 	else
776 		sup->su_lastmod = write_time;
777 	sup->su_ninos += ninos;
778 	++sup->su_nsums;
779 	fs->lfs_dmeta += (btofsb(fs, fs->lfs_sumsize) + btofsb(fs, ninos *
780 		fs->lfs_ibsize));
781 	fs->lfs_avail -= btofsb(fs, fs->lfs_sumsize);
782 
783 	do_again = !(bp->b_flags & B_GATHERED);
784 	LFS_WRITESEGENTRY(sup, fs, sp->seg_number, bp);	/* Ifile */
785 
786 	/*
787 	 * Compute checksum across data and then across summary; the first
788 	 * block (the summary block) is skipped.  Set the create time here
789 	 * so that it's guaranteed to be later than the inode mod times.
790 	 */
791 	if (fs->lfs_version == 1)
792 		el_size = sizeof(u_long);
793 	else
794 		el_size = sizeof(u_int32_t);
795 	datap = dp = emalloc(nblocks * el_size);
796 	for (bpp = sp->bpp, i = nblocks - 1; i--;) {
797 		++bpp;
798 		/* Loop through gop_write cluster blocks */
799 		for (byteoffset = 0; byteoffset < (*bpp)->b_bcount;
800 		    byteoffset += fs->lfs_bsize) {
801 			memcpy(dp, (*bpp)->b_data + byteoffset, el_size);
802 			dp += el_size;
803 		}
804 		bremfree(*bpp);
805 		(*bpp)->b_flags |= B_BUSY;
806 	}
807 	if (fs->lfs_version == 1)
808 		ssp->ss_ocreate = write_time;
809 	else {
810 		ssp->ss_create = write_time;
811 		ssp->ss_serial = ++fs->lfs_serial;
812 		ssp->ss_ident = fs->lfs_ident;
813 	}
814 	/* Set the summary block busy too */
815 	bremfree(*(sp->bpp));
816 	(*(sp->bpp))->b_flags |= B_BUSY;
817 
818 	ssp->ss_datasum = cksum(datap, (nblocks - 1) * el_size);
819 	ssp->ss_sumsum =
820 	    cksum(&ssp->ss_datasum, fs->lfs_sumsize - sizeof(ssp->ss_sumsum));
821 	free(datap);
822 	datap = dp = NULL;
823 	fs->lfs_bfree -= (btofsb(fs, ninos * fs->lfs_ibsize) +
824 	    btofsb(fs, fs->lfs_sumsize));
825 
826 	if (devvp == NULL)
827 		errx(1, "devvp is NULL");
828 	for (bpp = sp->bpp, i = nblocks; i; bpp++, i--) {
829 		bp = *bpp;
830 #if 0
831 		printf("i = %d, bp = %p, flags %lx, bn = %" PRIx64 "\n",
832 		       nblocks - i, bp, bp->b_flags, bp->b_blkno);
833 		printf("  vp = %p\n", bp->b_vp);
834 		if (bp->b_vp != fs->lfs_devvp)
835 			printf("  ino = %d lbn = %" PRId64 "\n",
836 			       VTOI(bp->b_vp)->i_number, bp->b_lblkno);
837 #endif
838 		if (bp->b_vp == fs->lfs_devvp)
839 			written_dev += bp->b_bcount;
840 		else {
841 			if (bp->b_lblkno >= 0)
842 				written_data += bp->b_bcount;
843 			else
844 				written_indir += bp->b_bcount;
845 		}
846 		bp->b_flags &= ~(B_DELWRI | B_READ | B_GATHERED | B_ERROR |
847 				 B_LOCKED);
848 		bwrite(bp);
849 		written_bytes += bp->b_bcount;
850 	}
851 	written_inodes += ninos;
852 
853 	return (lfs_initseg(fs) || do_again);
854 }
855 
856 /*
857  * Our own copy of shellsort.  XXX use qsort or heapsort.
858  */
859 void
860 lfs_shellsort(struct ubuf ** bp_array, ufs_daddr_t * lb_array, int nmemb, int size)
861 {
862 	static int __rsshell_increments[] = {4, 1, 0};
863 	int incr, *incrp, t1, t2;
864 	struct ubuf *bp_temp;
865 
866 	for (incrp = __rsshell_increments; (incr = *incrp++) != 0;)
867 		for (t1 = incr; t1 < nmemb; ++t1)
868 			for (t2 = t1 - incr; t2 >= 0;)
869 				if ((u_int32_t) bp_array[t2]->b_lblkno >
870 				    (u_int32_t) bp_array[t2 + incr]->b_lblkno) {
871 					bp_temp = bp_array[t2];
872 					bp_array[t2] = bp_array[t2 + incr];
873 					bp_array[t2 + incr] = bp_temp;
874 					t2 -= incr;
875 				} else
876 					break;
877 
878 	/* Reform the list of logical blocks */
879 	incr = 0;
880 	for (t1 = 0; t1 < nmemb; t1++) {
881 		for (t2 = 0; t2 * size < bp_array[t1]->b_bcount; t2++) {
882 			lb_array[incr++] = bp_array[t1]->b_lblkno + t2;
883 		}
884 	}
885 }
886 
887 
888 /*
889  * lfs_seglock --
890  *	Single thread the segment writer.
891  */
892 int
893 lfs_seglock(struct lfs * fs, unsigned long flags)
894 {
895 	struct segment *sp;
896 
897 	if (fs->lfs_seglock) {
898 		++fs->lfs_seglock;
899 		fs->lfs_sp->seg_flags |= flags;
900 		return 0;
901 	}
902 	fs->lfs_seglock = 1;
903 
904 	sp = fs->lfs_sp = emalloc(sizeof(*sp));
905 	sp->bpp = emalloc(fs->lfs_ssize * sizeof(struct ubuf *));
906 	if (!sp->bpp)
907 		errx(!preen, "Could not allocate %zu bytes: %s",
908 			(size_t)(fs->lfs_ssize * sizeof(struct ubuf *)),
909 			strerror(errno));
910 	sp->seg_flags = flags;
911 	sp->vp = NULL;
912 	sp->seg_iocount = 0;
913 	(void) lfs_initseg(fs);
914 
915 	return 0;
916 }
917 
918 /*
919  * lfs_segunlock --
920  *	Single thread the segment writer.
921  */
922 void
923 lfs_segunlock(struct lfs * fs)
924 {
925 	struct segment *sp;
926 	struct ubuf *bp;
927 
928 	sp = fs->lfs_sp;
929 
930 	if (fs->lfs_seglock == 1) {
931 		if (sp->bpp != sp->cbpp) {
932 			/* Free allocated segment summary */
933 			fs->lfs_offset -= btofsb(fs, fs->lfs_sumsize);
934 			bp = *sp->bpp;
935 			bremfree(bp);
936 			bp->b_flags |= B_DONE | B_INVAL;
937 			bp->b_flags &= ~B_DELWRI;
938 			reassignbuf(bp, bp->b_vp);
939 			bp->b_flags |= B_BUSY; /* XXX */
940 			brelse(bp, 0);
941 		} else
942 			printf("unlock to 0 with no summary");
943 
944 		free(sp->bpp);
945 		sp->bpp = NULL;
946 		free(sp);
947 		fs->lfs_sp = NULL;
948 
949 		fs->lfs_nactive = 0;
950 
951 		/* Since we *know* everything's on disk, write both sbs */
952 		lfs_writesuper(fs, fs->lfs_sboffs[0]);
953 		lfs_writesuper(fs, fs->lfs_sboffs[1]);
954 
955 		--fs->lfs_seglock;
956 		fs->lfs_lockpid = 0;
957 	} else if (fs->lfs_seglock == 0) {
958 		errx(1, "Seglock not held");
959 	} else {
960 		--fs->lfs_seglock;
961 	}
962 }
963 
964 int
965 lfs_writevnodes(struct lfs *fs, struct segment *sp, int op)
966 {
967 	struct inode *ip;
968 	struct uvnode *vp;
969 	int inodes_written = 0;
970 
971 	LIST_FOREACH(vp, &vnodelist, v_mntvnodes) {
972 		if (vp->v_bmap_op != lfs_vop_bmap)
973 			continue;
974 
975 		ip = VTOI(vp);
976 
977 		if ((op == VN_DIROP && !(vp->v_uflag & VU_DIROP)) ||
978 		    (op != VN_DIROP && (vp->v_uflag & VU_DIROP))) {
979 			continue;
980 		}
981 		/*
982 		 * Write the inode/file if dirty and it's not the IFILE.
983 		 */
984 		if (ip->i_flag & IN_ALLMOD || !LIST_EMPTY(&vp->v_dirtyblkhd)) {
985 			if (ip->i_number != LFS_IFILE_INUM)
986 				lfs_writefile(fs, sp, vp);
987 			(void) lfs_writeinode(fs, sp, ip);
988 			inodes_written++;
989 		}
990 	}
991 	return inodes_written;
992 }
993 
994 void
995 lfs_writesuper(struct lfs *fs, ufs_daddr_t daddr)
996 {
997 	struct ubuf *bp;
998 
999 	/* Set timestamp of this version of the superblock */
1000 	if (fs->lfs_version == 1)
1001 		fs->lfs_otstamp = write_time;
1002 	fs->lfs_tstamp = write_time;
1003 
1004 	/* Checksum the superblock and copy it into a buffer. */
1005 	fs->lfs_cksum = lfs_sb_cksum(&(fs->lfs_dlfs));
1006 	assert(daddr > 0);
1007 	bp = getblk(fs->lfs_devvp, fsbtodb(fs, daddr), LFS_SBPAD);
1008 	memset(bp->b_data + sizeof(struct dlfs), 0,
1009 	    LFS_SBPAD - sizeof(struct dlfs));
1010 	*(struct dlfs *) bp->b_data = fs->lfs_dlfs;
1011 
1012 	bwrite(bp);
1013 }
1014