lfs.c revision 1.24
1/* $NetBSD: lfs.c,v 1.24 2006/07/18 23:37:13 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
102extern u_int32_t cksum(void *, size_t);
103extern u_int32_t lfs_sb_cksum(struct dlfs *);
104extern void pwarn(const char *, ...);
105
106extern struct uvnodelst vnodelist;
107extern struct uvnodelst getvnodelist[VNODE_HASH_MAX];
108extern int nvnodes;
109
110static int
111lfs_fragextend(struct uvnode *, int, int, daddr_t, struct ubuf **);
112
113int fsdirty = 0;
114void (*panic_func)(int, const char *, va_list) = my_vpanic;
115
116/*
117 * LFS buffer and uvnode operations
118 */
119
120int
121lfs_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
143int
144lfs_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 */
164int
165ufs_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 */
237int
238ufs_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
309int
310lfs_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. */
316struct ufs1_dinode *
317lfs_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 */
339struct uvnode *
340lfs_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
426static struct uvnode *
427lfs_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 */
443static int
444check_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. */
465struct lfs *
466lfs_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 */
600ufs_daddr_t
601try_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, dc;
606	struct ubuf *bp;
607	ufs_daddr_t nodirop_daddr;
608	u_int64_t serial;
609
610	bc = dc = 0;
611	odaddr = -1;
612	daddr = osb->lfs_offset;
613	nodirop_daddr = daddr;
614	serial = osb->lfs_serial;
615	while (daddr != goal) {
616		/*
617		 * Don't mistakenly read a superblock, if there is one here.
618		 */
619		if (sntod(osb, dtosn(osb, daddr)) == daddr) {
620			for (i = 0; i < LFS_MAXNUMSB; i++) {
621				if (osb->lfs_sboffs[i] < daddr)
622					break;
623				if (osb->lfs_sboffs[i] == daddr)
624					daddr += btofsb(osb, LFS_SBPAD);
625			}
626		}
627
628		/* Read in summary block */
629		bread(devvp, fsbtodb(osb, daddr), osb->lfs_sumsize, NULL, &bp);
630		sp = (SEGSUM *)bp->b_data;
631
632		/*
633		 * Check for a valid segment summary belonging to our fs.
634		 */
635		if (sp->ss_magic != SS_MAGIC ||
636		    sp->ss_ident != osb->lfs_ident ||
637		    sp->ss_serial < serial ||	/* XXX strengthen this */
638		    sp->ss_sumsum != cksum(&sp->ss_datasum, osb->lfs_sumsize -
639			sizeof(sp->ss_sumsum))) {
640			brelse(bp);
641			if (debug) {
642				if (sp->ss_magic != SS_MAGIC)
643					pwarn("pseg at 0x%x: "
644					      "wrong magic number\n",
645					      (int)daddr);
646				else if (sp->ss_ident != osb->lfs_ident)
647					pwarn("pseg at 0x%x: "
648					      "expected ident %llx, got %llx\n",
649					      (int)daddr,
650					      (long long)sp->ss_ident,
651					      (long long)osb->lfs_ident);
652				else if (sp->ss_serial >= serial)
653					pwarn("pseg at 0x%x: "
654					      "serial %d < %d\n", (int)daddr,
655					      (int)sp->ss_serial, (int)serial);
656				else
657					pwarn("pseg at 0x%x: "
658					      "summary checksum wrong\n",
659					      (int)daddr);
660			}
661			break;
662		}
663		if (debug && sp->ss_serial != serial)
664			pwarn("warning, serial=%d ss_serial=%d",
665				(int)serial, (int)sp->ss_serial);
666		++serial;
667		bc = check_summary(osb, sp, daddr, debug, devvp, NULL);
668		if (bc == 0) {
669			brelse(bp);
670			break;
671		}
672		if (debug)
673			pwarn("summary good: 0x%x/%d\n", (int)daddr,
674			      (int)sp->ss_serial);
675		assert (bc > 0);
676		odaddr = daddr;
677		daddr += btofsb(osb, osb->lfs_sumsize + bc);
678		if (dtosn(osb, odaddr) != dtosn(osb, daddr) ||
679		    dtosn(osb, daddr) != dtosn(osb, daddr +
680			btofsb(osb, osb->lfs_sumsize + osb->lfs_bsize))) {
681			daddr = sp->ss_next;
682		}
683
684		/*
685		 * Check for the beginning and ending of a sequence of
686		 * dirops.  We have to do the check this way, rather than
687		 * simply checking for the lack of SS_CONT, because the
688		 * cleaner sometimes injects SS_DIROP|SS_CONT partial-segments
689		 * without actually completing the dirop.
690		 */
691		if (sp->ss_flags & SS_CONT)
692			dc = 1;
693		if ((sp->ss_flags & (SS_DIROP | SS_CONT)) == SS_DIROP)
694			dc = 0;
695		if (dc == 0)
696			nodirop_daddr = daddr;
697
698		brelse(bp);
699	}
700
701	if (goal == 0)
702		return nodirop_daddr;
703	else
704		return daddr;
705}
706
707/* Use try_verify to check whether the newer superblock is valid. */
708struct lfs *
709lfs_verify(struct lfs *sb0, struct lfs *sb1, struct uvnode *devvp, int debug)
710{
711	ufs_daddr_t daddr;
712	struct lfs *osb, *nsb;
713
714	/*
715	 * Verify the checkpoint of the newer superblock,
716	 * if the timestamp/serial number of the two superblocks is
717	 * different.
718	 */
719
720	osb = NULL;
721	if (debug)
722		pwarn("sb0 %lld, sb1 %lld",
723		      (long long) sb0->lfs_serial,
724		      (long long) sb1->lfs_serial);
725
726	if ((sb0->lfs_version == 1 &&
727		sb0->lfs_otstamp != sb1->lfs_otstamp) ||
728	    (sb0->lfs_version > 1 &&
729		sb0->lfs_serial != sb1->lfs_serial)) {
730		if (sb0->lfs_version == 1) {
731			if (sb0->lfs_otstamp > sb1->lfs_otstamp) {
732				osb = sb1;
733				nsb = sb0;
734			} else {
735				osb = sb0;
736				nsb = sb1;
737			}
738		} else {
739			if (sb0->lfs_serial > sb1->lfs_serial) {
740				osb = sb1;
741				nsb = sb0;
742			} else {
743				osb = sb0;
744				nsb = sb1;
745			}
746		}
747		if (debug) {
748			printf("Attempting to verify newer checkpoint...");
749			fflush(stdout);
750		}
751		daddr = try_verify(osb, devvp, nsb->lfs_offset, debug);
752
753		if (debug)
754			printf("done.\n");
755		if (daddr == nsb->lfs_offset) {
756			pwarn("** Newer checkpoint verified, recovered %lld seconds of data\n",
757			    (long long) nsb->lfs_tstamp - (long long) osb->lfs_tstamp);
758			sbdirty();
759		} else {
760			pwarn("** Newer checkpoint invalid, lost %lld seconds of data\n", (long long) nsb->lfs_tstamp - (long long) osb->lfs_tstamp);
761		}
762		return (daddr == nsb->lfs_offset ? nsb : osb);
763	}
764	/* Nothing to check */
765	return osb;
766}
767
768/* Verify a partial-segment summary; return the number of bytes on disk. */
769int
770check_summary(struct lfs *fs, SEGSUM *sp, ufs_daddr_t pseg_addr, int debug,
771	      struct uvnode *devvp, void (func(ufs_daddr_t, FINFO *)))
772{
773	FINFO *fp;
774	int bc;			/* Bytes in partial segment */
775	int nblocks;
776	ufs_daddr_t seg_addr, daddr;
777	ufs_daddr_t *dp, *idp;
778	struct ubuf *bp;
779	int i, j, k, datac, len;
780	long sn;
781	u_int32_t *datap;
782	u_int32_t ccksum;
783
784	sn = dtosn(fs, pseg_addr);
785	seg_addr = sntod(fs, sn);
786
787	/* We've already checked the sumsum, just do the data bounds and sum */
788
789	/* Count the blocks. */
790	nblocks = howmany(sp->ss_ninos, INOPB(fs));
791	bc = nblocks << (fs->lfs_version > 1 ? fs->lfs_ffshift : fs->lfs_bshift);
792	assert(bc >= 0);
793
794	fp = (FINFO *) (sp + 1);
795	for (i = 0; i < sp->ss_nfinfo; i++) {
796		nblocks += fp->fi_nblocks;
797		bc += fp->fi_lastlength + ((fp->fi_nblocks - 1)
798					   << fs->lfs_bshift);
799		assert(bc >= 0);
800		fp = (FINFO *) (fp->fi_blocks + fp->fi_nblocks);
801		if (((char *)fp) - (char *)sp > fs->lfs_sumsize)
802			return 0;
803	}
804	datap = (u_int32_t *) malloc(nblocks * sizeof(*datap));
805	if (datap == NULL)
806		err(1, NULL);
807	datac = 0;
808
809	dp = (ufs_daddr_t *) sp;
810	dp += fs->lfs_sumsize / sizeof(ufs_daddr_t);
811	dp--;
812
813	idp = dp;
814	daddr = pseg_addr + btofsb(fs, fs->lfs_sumsize);
815	fp = (FINFO *) (sp + 1);
816	for (i = 0, j = 0;
817	     i < sp->ss_nfinfo || j < howmany(sp->ss_ninos, INOPB(fs)); i++) {
818		if (i >= sp->ss_nfinfo && *idp != daddr) {
819			pwarn("Not enough inode blocks in pseg at 0x%" PRIx32
820			      ": found %d, wanted %d\n",
821			      pseg_addr, j, howmany(sp->ss_ninos, INOPB(fs)));
822			if (debug)
823				pwarn("*idp=%x, daddr=%" PRIx32 "\n", *idp,
824				      daddr);
825			break;
826		}
827		while (j < howmany(sp->ss_ninos, INOPB(fs)) && *idp == daddr) {
828			bread(devvp, fsbtodb(fs, daddr), fs->lfs_ibsize, NOCRED, &bp);
829			datap[datac++] = ((u_int32_t *) (bp->b_data))[0];
830			brelse(bp);
831
832			++j;
833			daddr += btofsb(fs, fs->lfs_ibsize);
834			--idp;
835		}
836		if (i < sp->ss_nfinfo) {
837			if (func)
838				func(daddr, fp);
839			for (k = 0; k < fp->fi_nblocks; k++) {
840				len = (k == fp->fi_nblocks - 1 ?
841				       fp->fi_lastlength
842				       : fs->lfs_bsize);
843				bread(devvp, fsbtodb(fs, daddr), len, NOCRED, &bp);
844				datap[datac++] = ((u_int32_t *) (bp->b_data))[0];
845				brelse(bp);
846				daddr += btofsb(fs, len);
847			}
848			fp = (FINFO *) (fp->fi_blocks + fp->fi_nblocks);
849		}
850	}
851
852	if (datac != nblocks) {
853		pwarn("Partial segment at 0x%llx expected %d blocks counted %d\n",
854		    (long long) pseg_addr, nblocks, datac);
855	}
856	ccksum = cksum(datap, nblocks * sizeof(u_int32_t));
857	/* Check the data checksum */
858	if (ccksum != sp->ss_datasum) {
859		pwarn("Partial segment at 0x%" PRIx32 " data checksum"
860		      " mismatch: given 0x%x, computed 0x%x\n",
861		      pseg_addr, sp->ss_datasum, ccksum);
862		free(datap);
863		return 0;
864	}
865	free(datap);
866	assert(bc >= 0);
867	return bc;
868}
869
870/* print message and exit */
871void
872my_vpanic(int fatal, const char *fmt, va_list ap)
873{
874        (void) vprintf(fmt, ap);
875	exit(8);
876}
877
878void
879call_panic(const char *fmt, ...)
880{
881	va_list ap;
882
883	va_start(ap, fmt);
884        panic_func(1, fmt, ap);
885	va_end(ap);
886}
887
888/* Allocate a new inode. */
889struct uvnode *
890lfs_valloc(struct lfs *fs, ino_t ino)
891{
892	struct ubuf *bp, *cbp;
893	struct ifile *ifp;
894	ino_t new_ino;
895	int error;
896	int new_gen;
897	CLEANERINFO *cip;
898
899	/* Get the head of the freelist. */
900	LFS_GET_HEADFREE(fs, cip, cbp, &new_ino);
901
902	/*
903	 * Remove the inode from the free list and write the new start
904	 * of the free list into the superblock.
905	 */
906	LFS_IENTRY(ifp, fs, new_ino, bp);
907	if (ifp->if_daddr != LFS_UNUSED_DADDR)
908		panic("lfs_valloc: inuse inode %d on the free list", new_ino);
909	LFS_PUT_HEADFREE(fs, cip, cbp, ifp->if_nextfree);
910
911	new_gen = ifp->if_version; /* version was updated by vfree */
912	brelse(bp);
913
914	/* Extend IFILE so that the next lfs_valloc will succeed. */
915	if (fs->lfs_freehd == LFS_UNUSED_INUM) {
916		if ((error = extend_ifile(fs)) != 0) {
917			LFS_PUT_HEADFREE(fs, cip, cbp, new_ino);
918			return NULL;
919		}
920	}
921
922	/* Set superblock modified bit and increment file count. */
923        sbdirty();
924	++fs->lfs_nfiles;
925
926        return lfs_raw_vget(fs, ino, fs->lfs_devvp->v_fd, 0x0);
927}
928
929#ifdef IN_FSCK_LFS
930void reset_maxino(ino_t);
931#endif
932
933/*
934 * Add a new block to the Ifile, to accommodate future file creations.
935 */
936int
937extend_ifile(struct lfs *fs)
938{
939	struct uvnode *vp;
940	struct inode *ip;
941	IFILE *ifp;
942	IFILE_V1 *ifp_v1;
943	struct ubuf *bp, *cbp;
944	daddr_t i, blkno, max;
945	ino_t oldlast;
946	CLEANERINFO *cip;
947
948	vp = fs->lfs_ivnode;
949	ip = VTOI(vp);
950	blkno = lblkno(fs, ip->i_ffs1_size);
951
952	lfs_balloc(vp, ip->i_ffs1_size, fs->lfs_bsize, &bp);
953	ip->i_ffs1_size += fs->lfs_bsize;
954	ip->i_flag |= IN_MODIFIED;
955
956	i = (blkno - fs->lfs_segtabsz - fs->lfs_cleansz) *
957		fs->lfs_ifpb;
958	LFS_GET_HEADFREE(fs, cip, cbp, &oldlast);
959	LFS_PUT_HEADFREE(fs, cip, cbp, i);
960	max = i + fs->lfs_ifpb;
961	fs->lfs_bfree -= btofsb(fs, fs->lfs_bsize);
962
963	if (fs->lfs_version == 1) {
964		for (ifp_v1 = (IFILE_V1 *)bp->b_data; i < max; ++ifp_v1) {
965			ifp_v1->if_version = 1;
966			ifp_v1->if_daddr = LFS_UNUSED_DADDR;
967			ifp_v1->if_nextfree = ++i;
968		}
969		ifp_v1--;
970		ifp_v1->if_nextfree = oldlast;
971	} else {
972		for (ifp = (IFILE *)bp->b_data; i < max; ++ifp) {
973			ifp->if_version = 1;
974			ifp->if_daddr = LFS_UNUSED_DADDR;
975			ifp->if_nextfree = ++i;
976		}
977		ifp--;
978		ifp->if_nextfree = oldlast;
979	}
980	LFS_PUT_TAILFREE(fs, cip, cbp, max - 1);
981
982	LFS_BWRITE_LOG(bp);
983
984#ifdef IN_FSCK_LFS
985	reset_maxino(((ip->i_ffs1_size >> fs->lfs_bshift) - fs->lfs_segtabsz -
986		     fs->lfs_cleansz) * fs->lfs_ifpb);
987#endif
988	return 0;
989}
990
991/*
992 * Allocate a block, and to inode and filesystem block accounting for it
993 * and for any indirect blocks the may need to be created in order for
994 * this block to be created.
995 *
996 * Blocks which have never been accounted for (i.e., which "do not exist")
997 * have disk address 0, which is translated by ufs_bmap to the special value
998 * UNASSIGNED == -1, as in the historical UFS.
999 *
1000 * Blocks which have been accounted for but which have not yet been written
1001 * to disk are given the new special disk address UNWRITTEN == -2, so that
1002 * they can be differentiated from completely new blocks.
1003 */
1004int
1005lfs_balloc(struct uvnode *vp, off_t startoffset, int iosize, struct ubuf **bpp)
1006{
1007	int offset;
1008	daddr_t daddr, idaddr;
1009	struct ubuf *ibp, *bp;
1010	struct inode *ip;
1011	struct lfs *fs;
1012	struct indir indirs[NIADDR+2], *idp;
1013	daddr_t	lbn, lastblock;
1014	int bb, bcount;
1015	int error, frags, i, nsize, osize, num;
1016
1017	ip = VTOI(vp);
1018	fs = ip->i_lfs;
1019	offset = blkoff(fs, startoffset);
1020	lbn = lblkno(fs, startoffset);
1021
1022	/*
1023	 * Three cases: it's a block beyond the end of file, it's a block in
1024	 * the file that may or may not have been assigned a disk address or
1025	 * we're writing an entire block.
1026	 *
1027	 * Note, if the daddr is UNWRITTEN, the block already exists in
1028	 * the cache (it was read or written earlier).	If so, make sure
1029	 * we don't count it as a new block or zero out its contents. If
1030	 * it did not, make sure we allocate any necessary indirect
1031	 * blocks.
1032	 *
1033	 * If we are writing a block beyond the end of the file, we need to
1034	 * check if the old last block was a fragment.	If it was, we need
1035	 * to rewrite it.
1036	 */
1037
1038	if (bpp)
1039		*bpp = NULL;
1040
1041	/* Check for block beyond end of file and fragment extension needed. */
1042	lastblock = lblkno(fs, ip->i_ffs1_size);
1043	if (lastblock < NDADDR && lastblock < lbn) {
1044		osize = blksize(fs, ip, lastblock);
1045		if (osize < fs->lfs_bsize && osize > 0) {
1046			if ((error = lfs_fragextend(vp, osize, fs->lfs_bsize,
1047						    lastblock,
1048						    (bpp ? &bp : NULL))))
1049				return (error);
1050			ip->i_ffs1_size = ip->i_ffs1_size =
1051			    (lastblock + 1) * fs->lfs_bsize;
1052			ip->i_flag |= IN_CHANGE | IN_UPDATE;
1053			if (bpp)
1054				(void) VOP_BWRITE(bp);
1055		}
1056	}
1057
1058	/*
1059	 * If the block we are writing is a direct block, it's the last
1060	 * block in the file, and offset + iosize is less than a full
1061	 * block, we can write one or more fragments.  There are two cases:
1062	 * the block is brand new and we should allocate it the correct
1063	 * size or it already exists and contains some fragments and
1064	 * may need to extend it.
1065	 */
1066	if (lbn < NDADDR && lblkno(fs, ip->i_ffs1_size) <= lbn) {
1067		osize = blksize(fs, ip, lbn);
1068		nsize = fragroundup(fs, offset + iosize);
1069		if (lblktosize(fs, lbn) >= ip->i_ffs1_size) {
1070			/* Brand new block or fragment */
1071			frags = numfrags(fs, nsize);
1072			bb = fragstofsb(fs, frags);
1073			if (bpp) {
1074				*bpp = bp = getblk(vp, lbn, nsize);
1075				bp->b_blkno = UNWRITTEN;
1076			}
1077			ip->i_lfs_effnblks += bb;
1078			fs->lfs_bfree -= bb;
1079			ip->i_ffs1_db[lbn] = UNWRITTEN;
1080		} else {
1081			if (nsize <= osize) {
1082				/* No need to extend */
1083				if (bpp && (error = bread(vp, lbn, osize, NOCRED, &bp)))
1084					return error;
1085			} else {
1086				/* Extend existing block */
1087				if ((error =
1088				     lfs_fragextend(vp, osize, nsize, lbn,
1089						    (bpp ? &bp : NULL))))
1090					return error;
1091			}
1092			if (bpp)
1093				*bpp = bp;
1094		}
1095		return 0;
1096	}
1097
1098	error = ufs_bmaparray(fs, vp, lbn, &daddr, &indirs[0], &num);
1099	if (error)
1100		return (error);
1101
1102	daddr = (daddr_t)((int32_t)daddr); /* XXX ondisk32 */
1103
1104	/*
1105	 * Do byte accounting all at once, so we can gracefully fail *before*
1106	 * we start assigning blocks.
1107	 */
1108        bb = fsbtodb(fs, 1); /* bb = VFSTOUFS(vp->v_mount)->um_seqinc; */
1109	bcount = 0;
1110	if (daddr == UNASSIGNED) {
1111		bcount = bb;
1112	}
1113	for (i = 1; i < num; ++i) {
1114		if (!indirs[i].in_exists) {
1115			bcount += bb;
1116		}
1117	}
1118	fs->lfs_bfree -= bcount;
1119	ip->i_lfs_effnblks += bcount;
1120
1121	if (daddr == UNASSIGNED) {
1122		if (num > 0 && ip->i_ffs1_ib[indirs[0].in_off] == 0) {
1123			ip->i_ffs1_ib[indirs[0].in_off] = UNWRITTEN;
1124		}
1125
1126		/*
1127		 * Create new indirect blocks if necessary
1128		 */
1129		if (num > 1) {
1130			idaddr = ip->i_ffs1_ib[indirs[0].in_off];
1131			for (i = 1; i < num; ++i) {
1132				ibp = getblk(vp, indirs[i].in_lbn,
1133				    fs->lfs_bsize);
1134				if (!indirs[i].in_exists) {
1135					memset(ibp->b_data, 0, ibp->b_bufsize);
1136					ibp->b_blkno = UNWRITTEN;
1137				} else if (!(ibp->b_flags & (B_DELWRI | B_DONE))) {
1138					ibp->b_blkno = fsbtodb(fs, idaddr);
1139					ibp->b_flags |= B_READ;
1140					VOP_STRATEGY(ibp);
1141				}
1142				/*
1143				 * This block exists, but the next one may not.
1144				 * If that is the case mark it UNWRITTEN to
1145                                 * keep the accounting straight.
1146				 */
1147				/* XXX ondisk32 */
1148				if (((int32_t *)ibp->b_data)[indirs[i].in_off] == 0)
1149					((int32_t *)ibp->b_data)[indirs[i].in_off] =
1150						UNWRITTEN;
1151				/* XXX ondisk32 */
1152				idaddr = ((int32_t *)ibp->b_data)[indirs[i].in_off];
1153				if ((error = VOP_BWRITE(ibp)))
1154					return error;
1155			}
1156		}
1157	}
1158
1159
1160	/*
1161	 * Get the existing block from the cache, if requested.
1162	 */
1163	frags = fsbtofrags(fs, bb);
1164	if (bpp)
1165		*bpp = bp = getblk(vp, lbn, blksize(fs, ip, lbn));
1166
1167	/*
1168	 * The block we are writing may be a brand new block
1169	 * in which case we need to do accounting.
1170	 *
1171	 * We can tell a truly new block because ufs_bmaparray will say
1172	 * it is UNASSIGNED.  Once we allocate it we will assign it the
1173	 * disk address UNWRITTEN.
1174	 */
1175	if (daddr == UNASSIGNED) {
1176		if (bpp) {
1177			/* Note the new address */
1178			bp->b_blkno = UNWRITTEN;
1179		}
1180
1181		switch (num) {
1182		    case 0:
1183			ip->i_ffs1_db[lbn] = UNWRITTEN;
1184			break;
1185		    case 1:
1186			ip->i_ffs1_ib[indirs[0].in_off] = UNWRITTEN;
1187			break;
1188		    default:
1189			idp = &indirs[num - 1];
1190			if (bread(vp, idp->in_lbn, fs->lfs_bsize, NOCRED,
1191				  &ibp))
1192				panic("lfs_balloc: bread bno %lld",
1193				    (long long)idp->in_lbn);
1194			/* XXX ondisk32 */
1195			((int32_t *)ibp->b_data)[idp->in_off] = UNWRITTEN;
1196			VOP_BWRITE(ibp);
1197		}
1198	} else if (bpp && !(bp->b_flags & (B_DONE|B_DELWRI))) {
1199		/*
1200		 * Not a brand new block, also not in the cache;
1201		 * read it in from disk.
1202		 */
1203		if (iosize == fs->lfs_bsize)
1204			/* Optimization: I/O is unnecessary. */
1205			bp->b_blkno = daddr;
1206		else {
1207			/*
1208			 * We need to read the block to preserve the
1209			 * existing bytes.
1210			 */
1211			bp->b_blkno = daddr;
1212			bp->b_flags |= B_READ;
1213			VOP_STRATEGY(bp);
1214			return 0;
1215		}
1216	}
1217
1218	return (0);
1219}
1220
1221int
1222lfs_fragextend(struct uvnode *vp, int osize, int nsize, daddr_t lbn,
1223               struct ubuf **bpp)
1224{
1225	struct inode *ip;
1226	struct lfs *fs;
1227	long bb;
1228	int error;
1229	size_t obufsize;
1230
1231	ip = VTOI(vp);
1232	fs = ip->i_lfs;
1233	bb = (long)fragstofsb(fs, numfrags(fs, nsize - osize));
1234	error = 0;
1235
1236	/*
1237	 * If we are not asked to actually return the block, all we need
1238	 * to do is allocate space for it.  UBC will handle dirtying the
1239	 * appropriate things and making sure it all goes to disk.
1240	 * Don't bother to read in that case.
1241	 */
1242	if (bpp && (error = bread(vp, lbn, osize, NOCRED, bpp))) {
1243		brelse(*bpp);
1244		goto out;
1245	}
1246
1247	fs->lfs_bfree -= bb;
1248	ip->i_lfs_effnblks += bb;
1249	ip->i_flag |= IN_CHANGE | IN_UPDATE;
1250
1251	if (bpp) {
1252		obufsize = (*bpp)->b_bufsize;
1253		(*bpp)->b_data = realloc((*bpp)->b_data, nsize);
1254		bzero((char *)((*bpp)->b_data) + osize, (u_int)(nsize - osize));
1255	}
1256
1257    out:
1258	return (error);
1259}
1260