xref: /netbsd-src/sbin/fsck_lfs/lfs.c (revision ce2c90c7c172d95d2402a5b3d96d8f8e6d138a21)
1 /* $NetBSD: lfs.c,v 1.25 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) 1989, 1991, 1993
39  *	The Regents of the University of California.  All rights reserved.
40  * (c) UNIX System Laboratories, Inc.
41  * All or some portions of this file are derived from material licensed
42  * to the University of California by American Telephone and Telegraph
43  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
44  * the permission of UNIX System Laboratories, Inc.
45  *
46  * Redistribution and use in source and binary forms, with or without
47  * modification, are permitted provided that the following conditions
48  * are met:
49  * 1. Redistributions of source code must retain the above copyright
50  *    notice, this list of conditions and the following disclaimer.
51  * 2. Redistributions in binary form must reproduce the above copyright
52  *    notice, this list of conditions and the following disclaimer in the
53  *    documentation and/or other materials provided with the distribution.
54  * 3. Neither the name of the University nor the names of its contributors
55  *    may be used to endorse or promote products derived from this software
56  *    without specific prior written permission.
57  *
58  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
59  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
60  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
61  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
62  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
63  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
64  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
65  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
66  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
67  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
68  * SUCH DAMAGE.
69  *
70  *	@(#)ufs_bmap.c	8.8 (Berkeley) 8/11/95
71  */
72 
73 
74 #include <sys/types.h>
75 #include <sys/param.h>
76 #include <sys/time.h>
77 #include <sys/buf.h>
78 #include <sys/mount.h>
79 
80 #include <ufs/ufs/inode.h>
81 #include <ufs/ufs/ufsmount.h>
82 #define vnode uvnode
83 #include <ufs/lfs/lfs.h>
84 #undef vnode
85 
86 #include <assert.h>
87 #include <err.h>
88 #include <errno.h>
89 #include <stdarg.h>
90 #include <stdio.h>
91 #include <stdlib.h>
92 #include <string.h>
93 #include <unistd.h>
94 
95 #include "bufcache.h"
96 #include "vnode.h"
97 #include "lfs_user.h"
98 #include "segwrite.h"
99 
100 #define panic call_panic
101 
102 extern u_int32_t cksum(void *, size_t);
103 extern u_int32_t lfs_sb_cksum(struct dlfs *);
104 extern void pwarn(const char *, ...);
105 
106 extern struct uvnodelst vnodelist;
107 extern struct uvnodelst getvnodelist[VNODE_HASH_MAX];
108 extern int nvnodes;
109 
110 static int
111 lfs_fragextend(struct uvnode *, int, int, daddr_t, struct ubuf **);
112 
113 int fsdirty = 0;
114 void (*panic_func)(int, const char *, va_list) = my_vpanic;
115 
116 /*
117  * LFS buffer and uvnode operations
118  */
119 
120 int
121 lfs_vop_strategy(struct ubuf * bp)
122 {
123 	int count;
124 
125 	if (bp->b_flags & B_READ) {
126 		count = pread(bp->b_vp->v_fd, bp->b_data, bp->b_bcount,
127 		    dbtob(bp->b_blkno));
128 		if (count == bp->b_bcount)
129 			bp->b_flags |= B_DONE;
130 	} else {
131 		count = pwrite(bp->b_vp->v_fd, bp->b_data, bp->b_bcount,
132 		    dbtob(bp->b_blkno));
133 		if (count == 0) {
134 			perror("pwrite");
135 			return -1;
136 		}
137 		bp->b_flags &= ~B_DELWRI;
138 		reassignbuf(bp, bp->b_vp);
139 	}
140 	return 0;
141 }
142 
143 int
144 lfs_vop_bwrite(struct ubuf * bp)
145 {
146 	struct lfs *fs;
147 
148 	fs = bp->b_vp->v_fs;
149 	if (!(bp->b_flags & B_DELWRI)) {
150 		fs->lfs_avail -= btofsb(fs, bp->b_bcount);
151 	}
152 	bp->b_flags |= B_DELWRI | B_LOCKED;
153 	reassignbuf(bp, bp->b_vp);
154 	brelse(bp);
155 	return 0;
156 }
157 
158 /*
159  * ufs_bmaparray does the bmap conversion, and if requested returns the
160  * array of logical blocks which must be traversed to get to a block.
161  * Each entry contains the offset into that block that gets you to the
162  * next block and the disk address of the block (if it is assigned).
163  */
164 int
165 ufs_bmaparray(struct lfs * fs, struct uvnode * vp, daddr_t bn, daddr_t * bnp, struct indir * ap, int *nump)
166 {
167 	struct inode *ip;
168 	struct ubuf *bp;
169 	struct indir a[NIADDR + 1], *xap;
170 	daddr_t daddr;
171 	daddr_t metalbn;
172 	int error, num;
173 
174 	ip = VTOI(vp);
175 
176 	if (bn >= 0 && bn < NDADDR) {
177 		if (nump != NULL)
178 			*nump = 0;
179 		*bnp = fsbtodb(fs, ip->i_ffs1_db[bn]);
180 		if (*bnp == 0)
181 			*bnp = -1;
182 		return (0);
183 	}
184 	xap = ap == NULL ? a : ap;
185 	if (!nump)
186 		nump = &num;
187 	if ((error = ufs_getlbns(fs, vp, bn, xap, nump)) != 0)
188 		return (error);
189 
190 	num = *nump;
191 
192 	/* Get disk address out of indirect block array */
193 	daddr = ip->i_ffs1_ib[xap->in_off];
194 
195 	for (bp = NULL, ++xap; --num; ++xap) {
196 		/* Exit the loop if there is no disk address assigned yet and
197 		 * the indirect block isn't in the cache, or if we were
198 		 * looking for an indirect block and we've found it. */
199 
200 		metalbn = xap->in_lbn;
201 		if ((daddr == 0 && !incore(vp, metalbn)) || metalbn == bn)
202 			break;
203 		/*
204 		 * If we get here, we've either got the block in the cache
205 		 * or we have a disk address for it, go fetch it.
206 		 */
207 		if (bp)
208 			brelse(bp);
209 
210 		xap->in_exists = 1;
211 		bp = getblk(vp, metalbn, fs->lfs_bsize);
212 
213 		if (!(bp->b_flags & (B_DONE | B_DELWRI))) {
214 			bp->b_blkno = fsbtodb(fs, daddr);
215 			bp->b_flags |= B_READ;
216 			VOP_STRATEGY(bp);
217 		}
218 		daddr = ((ufs_daddr_t *) bp->b_data)[xap->in_off];
219 	}
220 	if (bp)
221 		brelse(bp);
222 
223 	daddr = fsbtodb(fs, (ufs_daddr_t) daddr);
224 	*bnp = daddr == 0 ? -1 : daddr;
225 	return (0);
226 }
227 
228 /*
229  * Create an array of logical block number/offset pairs which represent the
230  * path of indirect blocks required to access a data block.  The first "pair"
231  * contains the logical block number of the appropriate single, double or
232  * triple indirect block and the offset into the inode indirect block array.
233  * Note, the logical block number of the inode single/double/triple indirect
234  * block appears twice in the array, once with the offset into the i_ffs1_ib and
235  * once with the offset into the page itself.
236  */
237 int
238 ufs_getlbns(struct lfs * fs, struct uvnode * vp, daddr_t bn, struct indir * ap, int *nump)
239 {
240 	daddr_t metalbn, realbn;
241 	int64_t blockcnt;
242 	int lbc;
243 	int i, numlevels, off;
244 	int lognindir, indir;
245 
246 	metalbn = 0;    /* XXXGCC -Wuninitialized [sh3] */
247 
248 	if (nump)
249 		*nump = 0;
250 	numlevels = 0;
251 	realbn = bn;
252 	if (bn < 0)
253 		bn = -bn;
254 
255 	lognindir = -1;
256 	for (indir = fs->lfs_nindir; indir; indir >>= 1)
257 		++lognindir;
258 
259 	/* Determine the number of levels of indirection.  After this loop is
260 	 * done, blockcnt indicates the number of data blocks possible at the
261 	 * given level of indirection, and NIADDR - i is the number of levels
262 	 * of indirection needed to locate the requested block. */
263 
264 	bn -= NDADDR;
265 	for (lbc = 0, i = NIADDR;; i--, bn -= blockcnt) {
266 		if (i == 0)
267 			return (EFBIG);
268 
269 		lbc += lognindir;
270 		blockcnt = (int64_t) 1 << lbc;
271 
272 		if (bn < blockcnt)
273 			break;
274 	}
275 
276 	/* Calculate the address of the first meta-block. */
277 	metalbn = -((realbn >= 0 ? realbn : -realbn) - bn + NIADDR - i);
278 
279 	/* At each iteration, off is the offset into the bap array which is an
280 	 * array of disk addresses at the current level of indirection. The
281 	 * logical block number and the offset in that block are stored into
282 	 * the argument array. */
283 	ap->in_lbn = metalbn;
284 	ap->in_off = off = NIADDR - i;
285 	ap->in_exists = 0;
286 	ap++;
287 	for (++numlevels; i <= NIADDR; i++) {
288 		/* If searching for a meta-data block, quit when found. */
289 		if (metalbn == realbn)
290 			break;
291 
292 		lbc -= lognindir;
293 		blockcnt = (int64_t) 1 << lbc;
294 		off = (bn >> lbc) & (fs->lfs_nindir - 1);
295 
296 		++numlevels;
297 		ap->in_lbn = metalbn;
298 		ap->in_off = off;
299 		ap->in_exists = 0;
300 		++ap;
301 
302 		metalbn -= -1 + (off << lbc);
303 	}
304 	if (nump)
305 		*nump = numlevels;
306 	return (0);
307 }
308 
309 int
310 lfs_vop_bmap(struct uvnode * vp, daddr_t lbn, daddr_t * daddrp)
311 {
312 	return ufs_bmaparray(vp->v_fs, vp, lbn, daddrp, NULL, NULL);
313 }
314 
315 /* Search a block for a specific dinode. */
316 struct ufs1_dinode *
317 lfs_ifind(struct lfs * fs, ino_t ino, struct ubuf * bp)
318 {
319 	struct ufs1_dinode *dip = (struct ufs1_dinode *) bp->b_data;
320 	struct ufs1_dinode *ldip, *fin;
321 
322 	fin = dip + INOPB(fs);
323 
324 	/*
325 	 * Read the inode block backwards, since later versions of the
326 	 * inode will supercede earlier ones.  Though it is unlikely, it is
327 	 * possible that the same inode will appear in the same inode block.
328 	 */
329 	for (ldip = fin - 1; ldip >= dip; --ldip)
330 		if (ldip->di_inumber == ino)
331 			return (ldip);
332 	return NULL;
333 }
334 
335 /*
336  * lfs_raw_vget makes us a new vnode from the inode at the given disk address.
337  * XXX it currently loses atime information.
338  */
339 struct uvnode *
340 lfs_raw_vget(struct lfs * fs, ino_t ino, int fd, ufs_daddr_t daddr)
341 {
342 	struct uvnode *vp;
343 	struct inode *ip;
344 	struct ufs1_dinode *dip;
345 	struct ubuf *bp;
346 	int i, hash;
347 
348 	vp = (struct uvnode *) malloc(sizeof(*vp));
349 	if (vp == NULL)
350 		err(1, NULL);
351 	memset(vp, 0, sizeof(*vp));
352 	vp->v_fd = fd;
353 	vp->v_fs = fs;
354 	vp->v_usecount = 0;
355 	vp->v_strategy_op = lfs_vop_strategy;
356 	vp->v_bwrite_op = lfs_vop_bwrite;
357 	vp->v_bmap_op = lfs_vop_bmap;
358 	LIST_INIT(&vp->v_cleanblkhd);
359 	LIST_INIT(&vp->v_dirtyblkhd);
360 
361 	ip = (struct inode *) malloc(sizeof(*ip));
362 	if (ip == NULL)
363 		err(1, NULL);
364 	memset(ip, 0, sizeof(*ip));
365 
366 	ip->i_din.ffs1_din = (struct ufs1_dinode *)
367 	    malloc(sizeof(struct ufs1_dinode));
368 	if (ip->i_din.ffs1_din == NULL)
369 		err(1, NULL);
370 	memset(ip->i_din.ffs1_din, 0, sizeof (struct ufs1_dinode));
371 
372 	/* Initialize the inode -- from lfs_vcreate. */
373 	ip->inode_ext.lfs = malloc(sizeof(struct lfs_inode_ext));
374 	if (ip->inode_ext.lfs == NULL)
375 		err(1, NULL);
376 	memset(ip->inode_ext.lfs, 0, sizeof(struct lfs_inode_ext));
377 	vp->v_data = ip;
378 	/* ip->i_vnode = vp; */
379 	ip->i_number = ino;
380 	ip->i_lockf = 0;
381 	ip->i_diroff = 0;
382 	ip->i_lfs_effnblks = 0;
383 	ip->i_flag = 0;
384 
385 	/* Load inode block and find inode */
386 	if (daddr > 0) {
387 		bread(fs->lfs_devvp, fsbtodb(fs, daddr), fs->lfs_ibsize, NULL, &bp);
388 		bp->b_flags |= B_AGE;
389 		dip = lfs_ifind(fs, ino, bp);
390 		if (dip == NULL) {
391 			brelse(bp);
392 			free(ip);
393 			free(vp);
394 			return NULL;
395 		}
396 		memcpy(ip->i_din.ffs1_din, dip, sizeof(*dip));
397 		brelse(bp);
398 	}
399 	ip->i_number = ino;
400 	/* ip->i_devvp = fs->lfs_devvp; */
401 	ip->i_lfs = fs;
402 
403 	ip->i_ffs_effnlink = ip->i_ffs1_nlink;
404 	ip->i_lfs_effnblks = ip->i_ffs1_blocks;
405 	ip->i_lfs_osize = ip->i_ffs1_size;
406 #if 0
407 	if (fs->lfs_version > 1) {
408 		ip->i_ffs1_atime = ts.tv_sec;
409 		ip->i_ffs1_atimensec = ts.tv_nsec;
410 	}
411 #endif
412 
413 	memset(ip->i_lfs_fragsize, 0, NDADDR * sizeof(*ip->i_lfs_fragsize));
414 	for (i = 0; i < NDADDR; i++)
415 		if (ip->i_ffs1_db[i] != 0)
416 			ip->i_lfs_fragsize[i] = blksize(fs, ip, i);
417 
418 	++nvnodes;
419 	hash = ((int)(intptr_t)fs + ino) & (VNODE_HASH_MAX - 1);
420 	LIST_INSERT_HEAD(&getvnodelist[hash], vp, v_getvnodes);
421 	LIST_INSERT_HEAD(&vnodelist, vp, v_mntvnodes);
422 
423 	return vp;
424 }
425 
426 static struct uvnode *
427 lfs_vget(void *vfs, ino_t ino)
428 {
429 	struct lfs *fs = (struct lfs *)vfs;
430 	ufs_daddr_t daddr;
431 	struct ubuf *bp;
432 	IFILE *ifp;
433 
434 	LFS_IENTRY(ifp, fs, ino, bp);
435 	daddr = ifp->if_daddr;
436 	brelse(bp);
437 	if (daddr <= 0 || dtosn(fs, daddr) >= fs->lfs_nseg)
438 		return NULL;
439 	return lfs_raw_vget(fs, ino, fs->lfs_ivnode->v_fd, daddr);
440 }
441 
442 /* Check superblock magic number and checksum */
443 static int
444 check_sb(struct lfs *fs)
445 {
446 	u_int32_t checksum;
447 
448 	if (fs->lfs_magic != LFS_MAGIC) {
449 		printf("Superblock magic number (0x%lx) does not match "
450 		       "expected 0x%lx\n", (unsigned long) fs->lfs_magic,
451 		       (unsigned long) LFS_MAGIC);
452 		return 1;
453 	}
454 	/* checksum */
455 	checksum = lfs_sb_cksum(&(fs->lfs_dlfs));
456 	if (fs->lfs_cksum != checksum) {
457 		printf("Superblock checksum (%lx) does not match computed checksum (%lx)\n",
458 		    (unsigned long) fs->lfs_cksum, (unsigned long) checksum);
459 		return 1;
460 	}
461 	return 0;
462 }
463 
464 /* Initialize LFS library; load superblocks and choose which to use. */
465 struct lfs *
466 lfs_init(int devfd, daddr_t sblkno, daddr_t idaddr, int dummy_read, int debug)
467 {
468 	struct uvnode *devvp;
469 	struct ubuf *bp;
470 	int tryalt;
471 	struct lfs *fs, *altfs;
472 	int error;
473 
474 	vfs_init();
475 
476 	devvp = (struct uvnode *) malloc(sizeof(*devvp));
477 	if (devvp == NULL)
478 		err(1, NULL);
479 	memset(devvp, 0, sizeof(*devvp));
480 	devvp->v_fs = NULL;
481 	devvp->v_fd = devfd;
482 	devvp->v_strategy_op = raw_vop_strategy;
483 	devvp->v_bwrite_op = raw_vop_bwrite;
484 	devvp->v_bmap_op = raw_vop_bmap;
485 	LIST_INIT(&devvp->v_cleanblkhd);
486 	LIST_INIT(&devvp->v_dirtyblkhd);
487 
488 	tryalt = 0;
489 	if (dummy_read) {
490 		if (sblkno == 0)
491 			sblkno = btodb(LFS_LABELPAD);
492 		fs = (struct lfs *) malloc(sizeof(*fs));
493 		if (fs == NULL)
494 			err(1, NULL);
495 		memset(fs, 0, sizeof(*fs));
496 		fs->lfs_devvp = devvp;
497 	} else {
498 		if (sblkno == 0) {
499 			sblkno = btodb(LFS_LABELPAD);
500 			tryalt = 1;
501 		} else if (debug) {
502 			printf("No -b flag given, not attempting to verify checkpoint\n");
503 		}
504 		error = bread(devvp, sblkno, LFS_SBPAD, NOCRED, &bp);
505 		fs = (struct lfs *) malloc(sizeof(*fs));
506 		if (fs == NULL)
507 			err(1, NULL);
508 		memset(fs, 0, sizeof(*fs));
509 		fs->lfs_dlfs = *((struct dlfs *) bp->b_data);
510 		fs->lfs_devvp = devvp;
511 		bp->b_flags |= B_INVAL;
512 		brelse(bp);
513 
514 		if (tryalt) {
515 			error = bread(devvp, fsbtodb(fs, fs->lfs_sboffs[1]),
516 		    	LFS_SBPAD, NOCRED, &bp);
517 			altfs = (struct lfs *) malloc(sizeof(*altfs));
518 			if (altfs == NULL)
519 				err(1, NULL);
520 			memset(altfs, 0, sizeof(*altfs));
521 			altfs->lfs_dlfs = *((struct dlfs *) bp->b_data);
522 			altfs->lfs_devvp = devvp;
523 			bp->b_flags |= B_INVAL;
524 			brelse(bp);
525 
526 			if (check_sb(fs) || fs->lfs_idaddr <= 0) {
527 				if (debug)
528 					printf("Primary superblock is no good, using first alternate\n");
529 				free(fs);
530 				fs = altfs;
531 			} else {
532 				/* If both superblocks check out, try verification */
533 				if (check_sb(altfs)) {
534 					if (debug)
535 						printf("First alternate superblock is no good, using primary\n");
536 					free(altfs);
537 				} else {
538 					if (lfs_verify(fs, altfs, devvp, debug) == fs) {
539 						free(altfs);
540 					} else {
541 						free(fs);
542 						fs = altfs;
543 					}
544 				}
545 			}
546 		}
547 		if (check_sb(fs)) {
548 			free(fs);
549 			return NULL;
550 		}
551 	}
552 
553 	/* Compatibility */
554 	if (fs->lfs_version < 2) {
555 		fs->lfs_sumsize = LFS_V1_SUMMARY_SIZE;
556 		fs->lfs_ibsize = fs->lfs_bsize;
557 		fs->lfs_start = fs->lfs_sboffs[0];
558 		fs->lfs_tstamp = fs->lfs_otstamp;
559 		fs->lfs_fsbtodb = 0;
560 	}
561 
562 	if (!dummy_read) {
563 		fs->lfs_suflags = (u_int32_t **) malloc(2 * sizeof(u_int32_t *));
564 		if (fs->lfs_suflags == NULL)
565 			err(1, NULL);
566 		fs->lfs_suflags[0] = (u_int32_t *) malloc(fs->lfs_nseg * sizeof(u_int32_t));
567 		if (fs->lfs_suflags[0] == NULL)
568 			err(1, NULL);
569 		fs->lfs_suflags[1] = (u_int32_t *) malloc(fs->lfs_nseg * sizeof(u_int32_t));
570 		if (fs->lfs_suflags[1] == NULL)
571 			err(1, NULL);
572 	}
573 
574 	if (idaddr == 0)
575 		idaddr = fs->lfs_idaddr;
576 	else
577 		fs->lfs_idaddr = idaddr;
578 	/* NB: If dummy_read!=0, idaddr==0 here so we get a fake inode. */
579 	fs->lfs_ivnode = lfs_raw_vget(fs,
580 		(dummy_read ? LFS_IFILE_INUM : fs->lfs_ifile), devvp->v_fd,
581 		idaddr);
582 	if (fs->lfs_ivnode == NULL)
583 		return NULL;
584 
585 	register_vget((void *)fs, lfs_vget);
586 
587 	return fs;
588 }
589 
590 /*
591  * Check partial segment validity between fs->lfs_offset and the given goal.
592  *
593  * If goal == 0, just keep on going until the segments stop making sense,
594  * and return the address of the last valid partial segment.
595  *
596  * If goal != 0, return the address of the first partial segment that failed,
597  * or "goal" if we reached it without failure (the partial segment *at* goal
598  * need not be valid).
599  */
600 ufs_daddr_t
601 try_verify(struct lfs *osb, struct uvnode *devvp, ufs_daddr_t goal, int debug)
602 {
603 	ufs_daddr_t daddr, odaddr;
604 	SEGSUM *sp;
605 	int i, bc, hitclean;
606 	struct ubuf *bp;
607 	ufs_daddr_t nodirop_daddr;
608 	u_int64_t serial;
609 
610 	bc = 0;
611 	hitclean = 0;
612 	odaddr = -1;
613 	daddr = osb->lfs_offset;
614 	nodirop_daddr = daddr;
615 	serial = osb->lfs_serial;
616 	while (daddr != goal) {
617 		/*
618 		 * Don't mistakenly read a superblock, if there is one here.
619 		 */
620 		if (sntod(osb, dtosn(osb, daddr)) == daddr) {
621 			if (daddr == osb->lfs_start)
622 				daddr += btofsb(osb, LFS_LABELPAD);
623 			for (i = 0; i < LFS_MAXNUMSB; i++) {
624 				if (osb->lfs_sboffs[i] < daddr)
625 					break;
626 				if (osb->lfs_sboffs[i] == daddr)
627 					daddr += btofsb(osb, LFS_SBPAD);
628 			}
629 		}
630 
631 		/* Read in summary block */
632 		bread(devvp, fsbtodb(osb, daddr), osb->lfs_sumsize, NULL, &bp);
633 		sp = (SEGSUM *)bp->b_data;
634 
635 		/*
636 		 * Check for a valid segment summary belonging to our fs.
637 		 */
638 		if (sp->ss_magic != SS_MAGIC ||
639 		    sp->ss_ident != osb->lfs_ident ||
640 		    sp->ss_serial < serial ||	/* XXX strengthen this */
641 		    sp->ss_sumsum != cksum(&sp->ss_datasum, osb->lfs_sumsize -
642 			sizeof(sp->ss_sumsum))) {
643 			brelse(bp);
644 			if (debug) {
645 				if (sp->ss_magic != SS_MAGIC)
646 					pwarn("pseg at 0x%x: "
647 					      "wrong magic number\n",
648 					      (int)daddr);
649 				else if (sp->ss_ident != osb->lfs_ident)
650 					pwarn("pseg at 0x%x: "
651 					      "expected ident %llx, got %llx\n",
652 					      (int)daddr,
653 					      (long long)sp->ss_ident,
654 					      (long long)osb->lfs_ident);
655 				else if (sp->ss_serial >= serial)
656 					pwarn("pseg at 0x%x: "
657 					      "serial %d < %d\n", (int)daddr,
658 					      (int)sp->ss_serial, (int)serial);
659 				else
660 					pwarn("pseg at 0x%x: "
661 					      "summary checksum wrong\n",
662 					      (int)daddr);
663 			}
664 			break;
665 		}
666 		if (debug && sp->ss_serial != serial)
667 			pwarn("warning, serial=%d ss_serial=%d\n",
668 				(int)serial, (int)sp->ss_serial);
669 		++serial;
670 		bc = check_summary(osb, sp, daddr, debug, devvp, NULL);
671 		if (bc == 0) {
672 			brelse(bp);
673 			break;
674 		}
675 		if (debug)
676 			pwarn("summary good: 0x%x/%d\n", (int)daddr,
677 			      (int)sp->ss_serial);
678 		assert (bc > 0);
679 		odaddr = daddr;
680 		daddr += btofsb(osb, osb->lfs_sumsize + bc);
681 		if (dtosn(osb, odaddr) != dtosn(osb, daddr) ||
682 		    dtosn(osb, daddr) != dtosn(osb, daddr +
683 			btofsb(osb, osb->lfs_sumsize + osb->lfs_bsize) - 1)) {
684 			daddr = sp->ss_next;
685 		}
686 
687 		/*
688 		 * Check for the beginning and ending of a sequence of
689 		 * dirops.  Writes from the cleaner never involve new
690 		 * information, and are always checkpoints; so don't try
691 		 * to roll forward through them.  Likewise, psegs written
692 		 * by a previous roll-forward attempt are not interesting.
693 		 */
694 		if (sp->ss_flags & (SS_CLEAN | SS_RFW))
695 			hitclean = 1;
696 		if (hitclean == 0 && (sp->ss_flags & SS_CONT) == 0)
697 			nodirop_daddr = daddr;
698 
699 		brelse(bp);
700 	}
701 
702 	if (goal == 0)
703 		return nodirop_daddr;
704 	else
705 		return daddr;
706 }
707 
708 /* Use try_verify to check whether the newer superblock is valid. */
709 struct lfs *
710 lfs_verify(struct lfs *sb0, struct lfs *sb1, struct uvnode *devvp, int debug)
711 {
712 	ufs_daddr_t daddr;
713 	struct lfs *osb, *nsb;
714 
715 	/*
716 	 * Verify the checkpoint of the newer superblock,
717 	 * if the timestamp/serial number of the two superblocks is
718 	 * different.
719 	 */
720 
721 	osb = NULL;
722 	if (debug)
723 		pwarn("sb0 %lld, sb1 %lld",
724 		      (long long) sb0->lfs_serial,
725 		      (long long) sb1->lfs_serial);
726 
727 	if ((sb0->lfs_version == 1 &&
728 		sb0->lfs_otstamp != sb1->lfs_otstamp) ||
729 	    (sb0->lfs_version > 1 &&
730 		sb0->lfs_serial != sb1->lfs_serial)) {
731 		if (sb0->lfs_version == 1) {
732 			if (sb0->lfs_otstamp > sb1->lfs_otstamp) {
733 				osb = sb1;
734 				nsb = sb0;
735 			} else {
736 				osb = sb0;
737 				nsb = sb1;
738 			}
739 		} else {
740 			if (sb0->lfs_serial > sb1->lfs_serial) {
741 				osb = sb1;
742 				nsb = sb0;
743 			} else {
744 				osb = sb0;
745 				nsb = sb1;
746 			}
747 		}
748 		if (debug) {
749 			printf("Attempting to verify newer checkpoint...");
750 			fflush(stdout);
751 		}
752 		daddr = try_verify(osb, devvp, nsb->lfs_offset, debug);
753 
754 		if (debug)
755 			printf("done.\n");
756 		if (daddr == nsb->lfs_offset) {
757 			pwarn("** Newer checkpoint verified, recovered %lld seconds of data\n",
758 			    (long long) nsb->lfs_tstamp - (long long) osb->lfs_tstamp);
759 			sbdirty();
760 		} else {
761 			pwarn("** Newer checkpoint invalid, lost %lld seconds of data\n", (long long) nsb->lfs_tstamp - (long long) osb->lfs_tstamp);
762 		}
763 		return (daddr == nsb->lfs_offset ? nsb : osb);
764 	}
765 	/* Nothing to check */
766 	return osb;
767 }
768 
769 /* Verify a partial-segment summary; return the number of bytes on disk. */
770 int
771 check_summary(struct lfs *fs, SEGSUM *sp, ufs_daddr_t pseg_addr, int debug,
772 	      struct uvnode *devvp, void (func(ufs_daddr_t, FINFO *)))
773 {
774 	FINFO *fp;
775 	int bc;			/* Bytes in partial segment */
776 	int nblocks;
777 	ufs_daddr_t seg_addr, daddr;
778 	ufs_daddr_t *dp, *idp;
779 	struct ubuf *bp;
780 	int i, j, k, datac, len;
781 	long sn;
782 	u_int32_t *datap;
783 	u_int32_t ccksum;
784 
785 	sn = dtosn(fs, pseg_addr);
786 	seg_addr = sntod(fs, sn);
787 
788 	/* We've already checked the sumsum, just do the data bounds and sum */
789 
790 	/* Count the blocks. */
791 	nblocks = howmany(sp->ss_ninos, INOPB(fs));
792 	bc = nblocks << (fs->lfs_version > 1 ? fs->lfs_ffshift : fs->lfs_bshift);
793 	assert(bc >= 0);
794 
795 	fp = (FINFO *) (sp + 1);
796 	for (i = 0; i < sp->ss_nfinfo; i++) {
797 		nblocks += fp->fi_nblocks;
798 		bc += fp->fi_lastlength + ((fp->fi_nblocks - 1)
799 					   << fs->lfs_bshift);
800 		assert(bc >= 0);
801 		fp = (FINFO *) (fp->fi_blocks + fp->fi_nblocks);
802 		if (((char *)fp) - (char *)sp > fs->lfs_sumsize)
803 			return 0;
804 	}
805 	datap = (u_int32_t *) malloc(nblocks * sizeof(*datap));
806 	if (datap == NULL)
807 		err(1, NULL);
808 	datac = 0;
809 
810 	dp = (ufs_daddr_t *) sp;
811 	dp += fs->lfs_sumsize / sizeof(ufs_daddr_t);
812 	dp--;
813 
814 	idp = dp;
815 	daddr = pseg_addr + btofsb(fs, fs->lfs_sumsize);
816 	fp = (FINFO *) (sp + 1);
817 	for (i = 0, j = 0;
818 	     i < sp->ss_nfinfo || j < howmany(sp->ss_ninos, INOPB(fs)); i++) {
819 		if (i >= sp->ss_nfinfo && *idp != daddr) {
820 			pwarn("Not enough inode blocks in pseg at 0x%" PRIx32
821 			      ": found %d, wanted %d\n",
822 			      pseg_addr, j, howmany(sp->ss_ninos, INOPB(fs)));
823 			if (debug)
824 				pwarn("*idp=%x, daddr=%" PRIx32 "\n", *idp,
825 				      daddr);
826 			break;
827 		}
828 		while (j < howmany(sp->ss_ninos, INOPB(fs)) && *idp == daddr) {
829 			bread(devvp, fsbtodb(fs, daddr), fs->lfs_ibsize, NOCRED, &bp);
830 			datap[datac++] = ((u_int32_t *) (bp->b_data))[0];
831 			brelse(bp);
832 
833 			++j;
834 			daddr += btofsb(fs, fs->lfs_ibsize);
835 			--idp;
836 		}
837 		if (i < sp->ss_nfinfo) {
838 			if (func)
839 				func(daddr, fp);
840 			for (k = 0; k < fp->fi_nblocks; k++) {
841 				len = (k == fp->fi_nblocks - 1 ?
842 				       fp->fi_lastlength
843 				       : fs->lfs_bsize);
844 				bread(devvp, fsbtodb(fs, daddr), len, NOCRED, &bp);
845 				datap[datac++] = ((u_int32_t *) (bp->b_data))[0];
846 				brelse(bp);
847 				daddr += btofsb(fs, len);
848 			}
849 			fp = (FINFO *) (fp->fi_blocks + fp->fi_nblocks);
850 		}
851 	}
852 
853 	if (datac != nblocks) {
854 		pwarn("Partial segment at 0x%llx expected %d blocks counted %d\n",
855 		    (long long) pseg_addr, nblocks, datac);
856 	}
857 	ccksum = cksum(datap, nblocks * sizeof(u_int32_t));
858 	/* Check the data checksum */
859 	if (ccksum != sp->ss_datasum) {
860 		pwarn("Partial segment at 0x%" PRIx32 " data checksum"
861 		      " mismatch: given 0x%x, computed 0x%x\n",
862 		      pseg_addr, sp->ss_datasum, ccksum);
863 		free(datap);
864 		return 0;
865 	}
866 	free(datap);
867 	assert(bc >= 0);
868 	return bc;
869 }
870 
871 /* print message and exit */
872 void
873 my_vpanic(int fatal, const char *fmt, va_list ap)
874 {
875         (void) vprintf(fmt, ap);
876 	exit(8);
877 }
878 
879 void
880 call_panic(const char *fmt, ...)
881 {
882 	va_list ap;
883 
884 	va_start(ap, fmt);
885         panic_func(1, fmt, ap);
886 	va_end(ap);
887 }
888 
889 /* Allocate a new inode. */
890 struct uvnode *
891 lfs_valloc(struct lfs *fs, ino_t ino)
892 {
893 	struct ubuf *bp, *cbp;
894 	struct ifile *ifp;
895 	ino_t new_ino;
896 	int error;
897 	int new_gen;
898 	CLEANERINFO *cip;
899 
900 	/* Get the head of the freelist. */
901 	LFS_GET_HEADFREE(fs, cip, cbp, &new_ino);
902 
903 	/*
904 	 * Remove the inode from the free list and write the new start
905 	 * of the free list into the superblock.
906 	 */
907 	LFS_IENTRY(ifp, fs, new_ino, bp);
908 	if (ifp->if_daddr != LFS_UNUSED_DADDR)
909 		panic("lfs_valloc: inuse inode %d on the free list", new_ino);
910 	LFS_PUT_HEADFREE(fs, cip, cbp, ifp->if_nextfree);
911 
912 	new_gen = ifp->if_version; /* version was updated by vfree */
913 	brelse(bp);
914 
915 	/* Extend IFILE so that the next lfs_valloc will succeed. */
916 	if (fs->lfs_freehd == LFS_UNUSED_INUM) {
917 		if ((error = extend_ifile(fs)) != 0) {
918 			LFS_PUT_HEADFREE(fs, cip, cbp, new_ino);
919 			return NULL;
920 		}
921 	}
922 
923 	/* Set superblock modified bit and increment file count. */
924         sbdirty();
925 	++fs->lfs_nfiles;
926 
927         return lfs_raw_vget(fs, ino, fs->lfs_devvp->v_fd, 0x0);
928 }
929 
930 #ifdef IN_FSCK_LFS
931 void reset_maxino(ino_t);
932 #endif
933 
934 /*
935  * Add a new block to the Ifile, to accommodate future file creations.
936  */
937 int
938 extend_ifile(struct lfs *fs)
939 {
940 	struct uvnode *vp;
941 	struct inode *ip;
942 	IFILE *ifp;
943 	IFILE_V1 *ifp_v1;
944 	struct ubuf *bp, *cbp;
945 	daddr_t i, blkno, max;
946 	ino_t oldlast;
947 	CLEANERINFO *cip;
948 
949 	vp = fs->lfs_ivnode;
950 	ip = VTOI(vp);
951 	blkno = lblkno(fs, ip->i_ffs1_size);
952 
953 	lfs_balloc(vp, ip->i_ffs1_size, fs->lfs_bsize, &bp);
954 	ip->i_ffs1_size += fs->lfs_bsize;
955 	ip->i_flag |= IN_MODIFIED;
956 
957 	i = (blkno - fs->lfs_segtabsz - fs->lfs_cleansz) *
958 		fs->lfs_ifpb;
959 	LFS_GET_HEADFREE(fs, cip, cbp, &oldlast);
960 	LFS_PUT_HEADFREE(fs, cip, cbp, i);
961 	max = i + fs->lfs_ifpb;
962 	fs->lfs_bfree -= btofsb(fs, fs->lfs_bsize);
963 
964 	if (fs->lfs_version == 1) {
965 		for (ifp_v1 = (IFILE_V1 *)bp->b_data; i < max; ++ifp_v1) {
966 			ifp_v1->if_version = 1;
967 			ifp_v1->if_daddr = LFS_UNUSED_DADDR;
968 			ifp_v1->if_nextfree = ++i;
969 		}
970 		ifp_v1--;
971 		ifp_v1->if_nextfree = oldlast;
972 	} else {
973 		for (ifp = (IFILE *)bp->b_data; i < max; ++ifp) {
974 			ifp->if_version = 1;
975 			ifp->if_daddr = LFS_UNUSED_DADDR;
976 			ifp->if_nextfree = ++i;
977 		}
978 		ifp--;
979 		ifp->if_nextfree = oldlast;
980 	}
981 	LFS_PUT_TAILFREE(fs, cip, cbp, max - 1);
982 
983 	LFS_BWRITE_LOG(bp);
984 
985 #ifdef IN_FSCK_LFS
986 	reset_maxino(((ip->i_ffs1_size >> fs->lfs_bshift) - fs->lfs_segtabsz -
987 		     fs->lfs_cleansz) * fs->lfs_ifpb);
988 #endif
989 	return 0;
990 }
991 
992 /*
993  * Allocate a block, and to inode and filesystem block accounting for it
994  * and for any indirect blocks the may need to be created in order for
995  * this block to be created.
996  *
997  * Blocks which have never been accounted for (i.e., which "do not exist")
998  * have disk address 0, which is translated by ufs_bmap to the special value
999  * UNASSIGNED == -1, as in the historical UFS.
1000  *
1001  * Blocks which have been accounted for but which have not yet been written
1002  * to disk are given the new special disk address UNWRITTEN == -2, so that
1003  * they can be differentiated from completely new blocks.
1004  */
1005 int
1006 lfs_balloc(struct uvnode *vp, off_t startoffset, int iosize, struct ubuf **bpp)
1007 {
1008 	int offset;
1009 	daddr_t daddr, idaddr;
1010 	struct ubuf *ibp, *bp;
1011 	struct inode *ip;
1012 	struct lfs *fs;
1013 	struct indir indirs[NIADDR+2], *idp;
1014 	daddr_t	lbn, lastblock;
1015 	int bb, bcount;
1016 	int error, frags, i, nsize, osize, num;
1017 
1018 	ip = VTOI(vp);
1019 	fs = ip->i_lfs;
1020 	offset = blkoff(fs, startoffset);
1021 	lbn = lblkno(fs, startoffset);
1022 
1023 	/*
1024 	 * Three cases: it's a block beyond the end of file, it's a block in
1025 	 * the file that may or may not have been assigned a disk address or
1026 	 * we're writing an entire block.
1027 	 *
1028 	 * Note, if the daddr is UNWRITTEN, the block already exists in
1029 	 * the cache (it was read or written earlier).	If so, make sure
1030 	 * we don't count it as a new block or zero out its contents. If
1031 	 * it did not, make sure we allocate any necessary indirect
1032 	 * blocks.
1033 	 *
1034 	 * If we are writing a block beyond the end of the file, we need to
1035 	 * check if the old last block was a fragment.	If it was, we need
1036 	 * to rewrite it.
1037 	 */
1038 
1039 	if (bpp)
1040 		*bpp = NULL;
1041 
1042 	/* Check for block beyond end of file and fragment extension needed. */
1043 	lastblock = lblkno(fs, ip->i_ffs1_size);
1044 	if (lastblock < NDADDR && lastblock < lbn) {
1045 		osize = blksize(fs, ip, lastblock);
1046 		if (osize < fs->lfs_bsize && osize > 0) {
1047 			if ((error = lfs_fragextend(vp, osize, fs->lfs_bsize,
1048 						    lastblock,
1049 						    (bpp ? &bp : NULL))))
1050 				return (error);
1051 			ip->i_ffs1_size = ip->i_ffs1_size =
1052 			    (lastblock + 1) * fs->lfs_bsize;
1053 			ip->i_flag |= IN_CHANGE | IN_UPDATE;
1054 			if (bpp)
1055 				(void) VOP_BWRITE(bp);
1056 		}
1057 	}
1058 
1059 	/*
1060 	 * If the block we are writing is a direct block, it's the last
1061 	 * block in the file, and offset + iosize is less than a full
1062 	 * block, we can write one or more fragments.  There are two cases:
1063 	 * the block is brand new and we should allocate it the correct
1064 	 * size or it already exists and contains some fragments and
1065 	 * may need to extend it.
1066 	 */
1067 	if (lbn < NDADDR && lblkno(fs, ip->i_ffs1_size) <= lbn) {
1068 		osize = blksize(fs, ip, lbn);
1069 		nsize = fragroundup(fs, offset + iosize);
1070 		if (lblktosize(fs, lbn) >= ip->i_ffs1_size) {
1071 			/* Brand new block or fragment */
1072 			frags = numfrags(fs, nsize);
1073 			bb = fragstofsb(fs, frags);
1074 			if (bpp) {
1075 				*bpp = bp = getblk(vp, lbn, nsize);
1076 				bp->b_blkno = UNWRITTEN;
1077 			}
1078 			ip->i_lfs_effnblks += bb;
1079 			fs->lfs_bfree -= bb;
1080 			ip->i_ffs1_db[lbn] = UNWRITTEN;
1081 		} else {
1082 			if (nsize <= osize) {
1083 				/* No need to extend */
1084 				if (bpp && (error = bread(vp, lbn, osize, NOCRED, &bp)))
1085 					return error;
1086 			} else {
1087 				/* Extend existing block */
1088 				if ((error =
1089 				     lfs_fragextend(vp, osize, nsize, lbn,
1090 						    (bpp ? &bp : NULL))))
1091 					return error;
1092 			}
1093 			if (bpp)
1094 				*bpp = bp;
1095 		}
1096 		return 0;
1097 	}
1098 
1099 	error = ufs_bmaparray(fs, vp, lbn, &daddr, &indirs[0], &num);
1100 	if (error)
1101 		return (error);
1102 
1103 	daddr = (daddr_t)((int32_t)daddr); /* XXX ondisk32 */
1104 
1105 	/*
1106 	 * Do byte accounting all at once, so we can gracefully fail *before*
1107 	 * we start assigning blocks.
1108 	 */
1109         bb = fsbtodb(fs, 1); /* bb = VFSTOUFS(vp->v_mount)->um_seqinc; */
1110 	bcount = 0;
1111 	if (daddr == UNASSIGNED) {
1112 		bcount = bb;
1113 	}
1114 	for (i = 1; i < num; ++i) {
1115 		if (!indirs[i].in_exists) {
1116 			bcount += bb;
1117 		}
1118 	}
1119 	fs->lfs_bfree -= bcount;
1120 	ip->i_lfs_effnblks += bcount;
1121 
1122 	if (daddr == UNASSIGNED) {
1123 		if (num > 0 && ip->i_ffs1_ib[indirs[0].in_off] == 0) {
1124 			ip->i_ffs1_ib[indirs[0].in_off] = UNWRITTEN;
1125 		}
1126 
1127 		/*
1128 		 * Create new indirect blocks if necessary
1129 		 */
1130 		if (num > 1) {
1131 			idaddr = ip->i_ffs1_ib[indirs[0].in_off];
1132 			for (i = 1; i < num; ++i) {
1133 				ibp = getblk(vp, indirs[i].in_lbn,
1134 				    fs->lfs_bsize);
1135 				if (!indirs[i].in_exists) {
1136 					memset(ibp->b_data, 0, ibp->b_bufsize);
1137 					ibp->b_blkno = UNWRITTEN;
1138 				} else if (!(ibp->b_flags & (B_DELWRI | B_DONE))) {
1139 					ibp->b_blkno = fsbtodb(fs, idaddr);
1140 					ibp->b_flags |= B_READ;
1141 					VOP_STRATEGY(ibp);
1142 				}
1143 				/*
1144 				 * This block exists, but the next one may not.
1145 				 * If that is the case mark it UNWRITTEN to
1146                                  * keep the accounting straight.
1147 				 */
1148 				/* XXX ondisk32 */
1149 				if (((int32_t *)ibp->b_data)[indirs[i].in_off] == 0)
1150 					((int32_t *)ibp->b_data)[indirs[i].in_off] =
1151 						UNWRITTEN;
1152 				/* XXX ondisk32 */
1153 				idaddr = ((int32_t *)ibp->b_data)[indirs[i].in_off];
1154 				if ((error = VOP_BWRITE(ibp)))
1155 					return error;
1156 			}
1157 		}
1158 	}
1159 
1160 
1161 	/*
1162 	 * Get the existing block from the cache, if requested.
1163 	 */
1164 	frags = fsbtofrags(fs, bb);
1165 	if (bpp)
1166 		*bpp = bp = getblk(vp, lbn, blksize(fs, ip, lbn));
1167 
1168 	/*
1169 	 * The block we are writing may be a brand new block
1170 	 * in which case we need to do accounting.
1171 	 *
1172 	 * We can tell a truly new block because ufs_bmaparray will say
1173 	 * it is UNASSIGNED.  Once we allocate it we will assign it the
1174 	 * disk address UNWRITTEN.
1175 	 */
1176 	if (daddr == UNASSIGNED) {
1177 		if (bpp) {
1178 			/* Note the new address */
1179 			bp->b_blkno = UNWRITTEN;
1180 		}
1181 
1182 		switch (num) {
1183 		    case 0:
1184 			ip->i_ffs1_db[lbn] = UNWRITTEN;
1185 			break;
1186 		    case 1:
1187 			ip->i_ffs1_ib[indirs[0].in_off] = UNWRITTEN;
1188 			break;
1189 		    default:
1190 			idp = &indirs[num - 1];
1191 			if (bread(vp, idp->in_lbn, fs->lfs_bsize, NOCRED,
1192 				  &ibp))
1193 				panic("lfs_balloc: bread bno %lld",
1194 				    (long long)idp->in_lbn);
1195 			/* XXX ondisk32 */
1196 			((int32_t *)ibp->b_data)[idp->in_off] = UNWRITTEN;
1197 			VOP_BWRITE(ibp);
1198 		}
1199 	} else if (bpp && !(bp->b_flags & (B_DONE|B_DELWRI))) {
1200 		/*
1201 		 * Not a brand new block, also not in the cache;
1202 		 * read it in from disk.
1203 		 */
1204 		if (iosize == fs->lfs_bsize)
1205 			/* Optimization: I/O is unnecessary. */
1206 			bp->b_blkno = daddr;
1207 		else {
1208 			/*
1209 			 * We need to read the block to preserve the
1210 			 * existing bytes.
1211 			 */
1212 			bp->b_blkno = daddr;
1213 			bp->b_flags |= B_READ;
1214 			VOP_STRATEGY(bp);
1215 			return 0;
1216 		}
1217 	}
1218 
1219 	return (0);
1220 }
1221 
1222 int
1223 lfs_fragextend(struct uvnode *vp, int osize, int nsize, daddr_t lbn,
1224                struct ubuf **bpp)
1225 {
1226 	struct inode *ip;
1227 	struct lfs *fs;
1228 	long bb;
1229 	int error;
1230 	size_t obufsize;
1231 
1232 	ip = VTOI(vp);
1233 	fs = ip->i_lfs;
1234 	bb = (long)fragstofsb(fs, numfrags(fs, nsize - osize));
1235 	error = 0;
1236 
1237 	/*
1238 	 * If we are not asked to actually return the block, all we need
1239 	 * to do is allocate space for it.  UBC will handle dirtying the
1240 	 * appropriate things and making sure it all goes to disk.
1241 	 * Don't bother to read in that case.
1242 	 */
1243 	if (bpp && (error = bread(vp, lbn, osize, NOCRED, bpp))) {
1244 		brelse(*bpp);
1245 		goto out;
1246 	}
1247 
1248 	fs->lfs_bfree -= bb;
1249 	ip->i_lfs_effnblks += bb;
1250 	ip->i_flag |= IN_CHANGE | IN_UPDATE;
1251 
1252 	if (bpp) {
1253 		obufsize = (*bpp)->b_bufsize;
1254 		(*bpp)->b_data = realloc((*bpp)->b_data, nsize);
1255 		bzero((char *)((*bpp)->b_data) + osize, (u_int)(nsize - osize));
1256 	}
1257 
1258     out:
1259 	return (error);
1260 }
1261