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