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