lfs.c revision 1.18
1/* $NetBSD: lfs.c,v 1.18 2005/10/08 03:21:17 chs 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
110int fsdirty = 0;
111void (*panic_func)(int, const char *, va_list) = my_vpanic;
112
113/*
114 * LFS buffer and uvnode operations
115 */
116
117int
118lfs_vop_strategy(struct ubuf * bp)
119{
120	int count;
121
122	if (bp->b_flags & B_READ) {
123		count = pread(bp->b_vp->v_fd, bp->b_data, bp->b_bcount,
124		    dbtob(bp->b_blkno));
125		if (count == bp->b_bcount)
126			bp->b_flags |= B_DONE;
127	} else {
128		count = pwrite(bp->b_vp->v_fd, bp->b_data, bp->b_bcount,
129		    dbtob(bp->b_blkno));
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
140int
141lfs_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);
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 */
161int
162ufs_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);
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);
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 */
234int
235ufs_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	if (nump)
244		*nump = 0;
245	numlevels = 0;
246	realbn = bn;
247	if (bn < 0)
248		bn = -bn;
249
250	lognindir = -1;
251	for (indir = fs->lfs_nindir; indir; indir >>= 1)
252		++lognindir;
253
254	/* Determine the number of levels of indirection.  After this loop is
255	 * done, blockcnt indicates the number of data blocks possible at the
256	 * given level of indirection, and NIADDR - i is the number of levels
257	 * of indirection needed to locate the requested block. */
258
259	bn -= NDADDR;
260	for (lbc = 0, i = NIADDR;; i--, bn -= blockcnt) {
261		if (i == 0)
262			return (EFBIG);
263
264		lbc += lognindir;
265		blockcnt = (int64_t) 1 << lbc;
266
267		if (bn < blockcnt)
268			break;
269	}
270
271	/* Calculate the address of the first meta-block. */
272	metalbn = -((realbn >= 0 ? realbn : -realbn) - bn + NIADDR - i);
273
274	/* At each iteration, off is the offset into the bap array which is an
275	 * array of disk addresses at the current level of indirection. The
276	 * logical block number and the offset in that block are stored into
277	 * the argument array. */
278	ap->in_lbn = metalbn;
279	ap->in_off = off = NIADDR - i;
280	ap->in_exists = 0;
281	ap++;
282	for (++numlevels; i <= NIADDR; i++) {
283		/* If searching for a meta-data block, quit when found. */
284		if (metalbn == realbn)
285			break;
286
287		lbc -= lognindir;
288		blockcnt = (int64_t) 1 << lbc;
289		off = (bn >> lbc) & (fs->lfs_nindir - 1);
290
291		++numlevels;
292		ap->in_lbn = metalbn;
293		ap->in_off = off;
294		ap->in_exists = 0;
295		++ap;
296
297		metalbn -= -1 + (off << lbc);
298	}
299	if (nump)
300		*nump = numlevels;
301	return (0);
302}
303
304int
305lfs_vop_bmap(struct uvnode * vp, daddr_t lbn, daddr_t * daddrp)
306{
307	return ufs_bmaparray(vp->v_fs, vp, lbn, daddrp, NULL, NULL);
308}
309
310/* Search a block for a specific dinode. */
311struct ufs1_dinode *
312lfs_ifind(struct lfs * fs, ino_t ino, struct ubuf * bp)
313{
314	struct ufs1_dinode *dip = (struct ufs1_dinode *) bp->b_data;
315	struct ufs1_dinode *ldip, *fin;
316
317	fin = dip + INOPB(fs);
318
319	/*
320	 * Read the inode block backwards, since later versions of the
321	 * inode will supercede earlier ones.  Though it is unlikely, it is
322	 * possible that the same inode will appear in the same inode block.
323	 */
324	for (ldip = fin - 1; ldip >= dip; --ldip)
325		if (ldip->di_inumber == ino)
326			return (ldip);
327	return NULL;
328}
329
330/*
331 * lfs_raw_vget makes us a new vnode from the inode at the given disk address.
332 * XXX it currently loses atime information.
333 */
334struct uvnode *
335lfs_raw_vget(struct lfs * fs, ino_t ino, int fd, ufs_daddr_t daddr)
336{
337	struct uvnode *vp;
338	struct inode *ip;
339	struct ufs1_dinode *dip;
340	struct ubuf *bp;
341	int i, hash;
342
343	vp = (struct uvnode *) malloc(sizeof(*vp));
344	memset(vp, 0, 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 = (struct inode *) malloc(sizeof(*ip));
355	memset(ip, 0, sizeof(*ip));
356
357	ip->i_din.ffs1_din = (struct ufs1_dinode *)
358	    malloc(sizeof(struct ufs1_dinode));
359	memset(ip->i_din.ffs1_din, 0, sizeof (struct ufs1_dinode));
360
361	/* Initialize the inode -- from lfs_vcreate. */
362	ip->inode_ext.lfs = malloc(sizeof(struct lfs_inode_ext));
363	memset(ip->inode_ext.lfs, 0, sizeof(struct lfs_inode_ext));
364	vp->v_data = ip;
365	/* ip->i_vnode = vp; */
366	ip->i_number = ino;
367	ip->i_lockf = 0;
368	ip->i_diroff = 0;
369	ip->i_lfs_effnblks = 0;
370	ip->i_flag = 0;
371
372	/* Load inode block and find inode */
373	if (daddr > 0) {
374		bread(fs->lfs_devvp, fsbtodb(fs, daddr), fs->lfs_ibsize, NULL, &bp);
375		bp->b_flags |= B_AGE;
376		dip = lfs_ifind(fs, ino, bp);
377		if (dip == NULL) {
378			brelse(bp);
379			free(ip);
380			free(vp);
381			return NULL;
382		}
383		memcpy(ip->i_din.ffs1_din, dip, sizeof(*dip));
384		brelse(bp);
385	}
386	ip->i_number = ino;
387	/* ip->i_devvp = fs->lfs_devvp; */
388	ip->i_lfs = fs;
389
390	ip->i_ffs_effnlink = ip->i_ffs1_nlink;
391	ip->i_lfs_effnblks = ip->i_ffs1_blocks;
392	ip->i_lfs_osize = ip->i_ffs1_size;
393#if 0
394	if (fs->lfs_version > 1) {
395		ip->i_ffs1_atime = ts.tv_sec;
396		ip->i_ffs1_atimensec = ts.tv_nsec;
397	}
398#endif
399
400	memset(ip->i_lfs_fragsize, 0, NDADDR * sizeof(*ip->i_lfs_fragsize));
401	for (i = 0; i < NDADDR; i++)
402		if (ip->i_ffs1_db[i] != 0)
403			ip->i_lfs_fragsize[i] = blksize(fs, ip, i);
404
405	++nvnodes;
406	hash = ((int)(intptr_t)fs + ino) & (VNODE_HASH_MAX - 1);
407	LIST_INSERT_HEAD(&getvnodelist[hash], vp, v_getvnodes);
408	LIST_INSERT_HEAD(&vnodelist, vp, v_mntvnodes);
409
410	return vp;
411}
412
413static struct uvnode *
414lfs_vget(void *vfs, ino_t ino)
415{
416	struct lfs *fs = (struct lfs *)vfs;
417	ufs_daddr_t daddr;
418	struct ubuf *bp;
419	IFILE *ifp;
420
421	LFS_IENTRY(ifp, fs, ino, bp);
422	daddr = ifp->if_daddr;
423	brelse(bp);
424	if (daddr <= 0 || dtosn(fs, daddr) >= fs->lfs_nseg)
425		return NULL;
426	return lfs_raw_vget(fs, ino, fs->lfs_ivnode->v_fd, daddr);
427}
428
429/* Check superblock magic number and checksum */
430static int
431check_sb(struct lfs *fs)
432{
433	u_int32_t checksum;
434
435	if (fs->lfs_magic != LFS_MAGIC) {
436		printf("Superblock magic number (0x%lx) does not match "
437		       "expected 0x%lx\n", (unsigned long) fs->lfs_magic,
438		       (unsigned long) LFS_MAGIC);
439		return 1;
440	}
441	/* checksum */
442	checksum = lfs_sb_cksum(&(fs->lfs_dlfs));
443	if (fs->lfs_cksum != checksum) {
444		printf("Superblock checksum (%lx) does not match computed checksum (%lx)\n",
445		    (unsigned long) fs->lfs_cksum, (unsigned long) checksum);
446		return 1;
447	}
448	return 0;
449}
450
451/* Initialize LFS library; load superblocks and choose which to use. */
452struct lfs *
453lfs_init(int devfd, daddr_t sblkno, daddr_t idaddr, int dummy_read, int debug)
454{
455	struct uvnode *devvp;
456	struct ubuf *bp;
457	int tryalt;
458	struct lfs *fs, *altfs;
459	int error;
460
461	vfs_init();
462
463	devvp = (struct uvnode *) malloc(sizeof(*devvp));
464	memset(devvp, 0, sizeof(*devvp));
465	devvp->v_fs = NULL;
466	devvp->v_fd = devfd;
467	devvp->v_strategy_op = raw_vop_strategy;
468	devvp->v_bwrite_op = raw_vop_bwrite;
469	devvp->v_bmap_op = raw_vop_bmap;
470	LIST_INIT(&devvp->v_cleanblkhd);
471	LIST_INIT(&devvp->v_dirtyblkhd);
472
473	tryalt = 0;
474	if (dummy_read) {
475		if (sblkno == 0)
476			sblkno = btodb(LFS_LABELPAD);
477		fs = (struct lfs *) malloc(sizeof(*fs));
478		memset(fs, 0, sizeof(*fs));
479		fs->lfs_devvp = devvp;
480	} else {
481		if (sblkno == 0) {
482			sblkno = btodb(LFS_LABELPAD);
483			tryalt = 1;
484		} else if (debug) {
485			printf("No -b flag given, not attempting to verify checkpoint\n");
486		}
487		error = bread(devvp, sblkno, LFS_SBPAD, NOCRED, &bp);
488		fs = (struct lfs *) malloc(sizeof(*fs));
489		memset(fs, 0, sizeof(*fs));
490		fs->lfs_dlfs = *((struct dlfs *) bp->b_data);
491		fs->lfs_devvp = devvp;
492		bp->b_flags |= B_INVAL;
493		brelse(bp);
494
495		if (tryalt) {
496			error = bread(devvp, fsbtodb(fs, fs->lfs_sboffs[1]),
497		    	LFS_SBPAD, NOCRED, &bp);
498			altfs = (struct lfs *) malloc(sizeof(*altfs));
499			memset(altfs, 0, sizeof(*altfs));
500			altfs->lfs_dlfs = *((struct dlfs *) bp->b_data);
501			altfs->lfs_devvp = devvp;
502			bp->b_flags |= B_INVAL;
503			brelse(bp);
504
505			if (check_sb(fs) || fs->lfs_idaddr <= 0) {
506				if (debug)
507					printf("Primary superblock is no good, using first alternate\n");
508				free(fs);
509				fs = altfs;
510			} else {
511				/* If both superblocks check out, try verification */
512				if (check_sb(altfs)) {
513					if (debug)
514						printf("First alternate superblock is no good, using primary\n");
515					free(altfs);
516				} else {
517					if (lfs_verify(fs, altfs, devvp, debug) == fs) {
518						free(altfs);
519					} else {
520						free(fs);
521						fs = altfs;
522					}
523				}
524			}
525		}
526		if (check_sb(fs)) {
527			free(fs);
528			return NULL;
529		}
530	}
531
532	/* Compatibility */
533	if (fs->lfs_version < 2) {
534		fs->lfs_sumsize = LFS_V1_SUMMARY_SIZE;
535		fs->lfs_ibsize = fs->lfs_bsize;
536		fs->lfs_start = fs->lfs_sboffs[0];
537		fs->lfs_tstamp = fs->lfs_otstamp;
538		fs->lfs_fsbtodb = 0;
539	}
540
541	if (!dummy_read) {
542		fs->lfs_suflags = (u_int32_t **) malloc(2 * sizeof(u_int32_t *));
543		fs->lfs_suflags[0] = (u_int32_t *) malloc(fs->lfs_nseg * sizeof(u_int32_t));
544		fs->lfs_suflags[1] = (u_int32_t *) malloc(fs->lfs_nseg * sizeof(u_int32_t));
545	}
546
547	if (idaddr == 0)
548		idaddr = fs->lfs_idaddr;
549	else
550		fs->lfs_idaddr = idaddr;
551	/* NB: If dummy_read!=0, idaddr==0 here so we get a fake inode. */
552	fs->lfs_ivnode = lfs_raw_vget(fs,
553		(dummy_read ? LFS_IFILE_INUM : fs->lfs_ifile), devvp->v_fd,
554		idaddr);
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 */
571ufs_daddr_t
572try_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 bc, flag;
577	struct ubuf *bp;
578	ufs_daddr_t nodirop_daddr;
579	u_int64_t serial;
580
581	odaddr = -1;
582	daddr = osb->lfs_offset;
583	nodirop_daddr = daddr;
584	serial = osb->lfs_serial;
585	while (daddr != goal) {
586		flag = 0;
587oncemore:
588		/* Read in summary block */
589		bread(devvp, fsbtodb(osb, daddr), osb->lfs_sumsize, NULL, &bp);
590		sp = (SEGSUM *)bp->b_data;
591
592		/*
593		 * Could be a superblock instead of a segment summary.
594		 * XXX should use gseguse, but right now we need to do more
595		 * setup before we can...fix this
596		 */
597		if (sp->ss_magic != SS_MAGIC ||
598		    sp->ss_ident != osb->lfs_ident ||
599		    sp->ss_serial < serial ||
600		    sp->ss_sumsum != cksum(&sp->ss_datasum, osb->lfs_sumsize -
601			sizeof(sp->ss_sumsum))) {
602			brelse(bp);
603			if (flag == 0) {
604				flag = 1;
605				daddr += btofsb(osb, LFS_SBPAD);
606				goto oncemore;
607			}
608			break;
609		}
610		++serial;
611		bc = check_summary(osb, sp, daddr, debug, devvp, NULL);
612		if (bc == 0) {
613			brelse(bp);
614			break;
615		}
616		assert (bc > 0);
617		odaddr = daddr;
618		daddr += btofsb(osb, osb->lfs_sumsize + bc);
619		if (dtosn(osb, odaddr) != dtosn(osb, daddr) ||
620		    dtosn(osb, daddr) != dtosn(osb, daddr +
621			btofsb(osb, osb->lfs_sumsize + osb->lfs_bsize))) {
622			daddr = sp->ss_next;
623		}
624		if (!(sp->ss_flags & SS_CONT))
625			nodirop_daddr = daddr;
626		brelse(bp);
627	}
628
629	if (goal == 0)
630		return nodirop_daddr;
631	else
632		return daddr;
633}
634
635/* Use try_verify to check whether the newer superblock is valid. */
636struct lfs *
637lfs_verify(struct lfs *sb0, struct lfs *sb1, struct uvnode *devvp, int debug)
638{
639	ufs_daddr_t daddr;
640	struct lfs *osb, *nsb;
641
642	/*
643	 * Verify the checkpoint of the newer superblock,
644	 * if the timestamp/serial number of the two superblocks is
645	 * different.
646	 */
647
648	osb = NULL;
649	if (debug)
650		printf("sb0 %lld, sb1 %lld\n", (long long) sb0->lfs_serial,
651		    (long long) sb1->lfs_serial);
652
653	if ((sb0->lfs_version == 1 &&
654		sb0->lfs_otstamp != sb1->lfs_otstamp) ||
655	    (sb0->lfs_version > 1 &&
656		sb0->lfs_serial != sb1->lfs_serial)) {
657		if (sb0->lfs_version == 1) {
658			if (sb0->lfs_otstamp > sb1->lfs_otstamp) {
659				osb = sb1;
660				nsb = sb0;
661			} else {
662				osb = sb0;
663				nsb = sb1;
664			}
665		} else {
666			if (sb0->lfs_serial > sb1->lfs_serial) {
667				osb = sb1;
668				nsb = sb0;
669			} else {
670				osb = sb0;
671				nsb = sb1;
672			}
673		}
674		if (debug) {
675			printf("Attempting to verify newer checkpoint...");
676			fflush(stdout);
677		}
678		daddr = try_verify(osb, devvp, nsb->lfs_offset, debug);
679
680		if (debug)
681			printf("done.\n");
682		if (daddr == nsb->lfs_offset) {
683			pwarn("** Newer checkpoint verified, recovered %lld seconds of data\n",
684			    (long long) nsb->lfs_tstamp - (long long) osb->lfs_tstamp);
685			sbdirty();
686		} else {
687			pwarn("** Newer checkpoint invalid, lost %lld seconds of data\n", (long long) nsb->lfs_tstamp - (long long) osb->lfs_tstamp);
688		}
689		return (daddr == nsb->lfs_offset ? nsb : osb);
690	}
691	/* Nothing to check */
692	return osb;
693}
694
695/* Verify a partial-segment summary; return the number of bytes on disk. */
696int
697check_summary(struct lfs *fs, SEGSUM *sp, ufs_daddr_t pseg_addr, int debug,
698	      struct uvnode *devvp, void (func(ufs_daddr_t, FINFO *)))
699{
700	FINFO *fp;
701	int bc;			/* Bytes in partial segment */
702	int nblocks;
703	ufs_daddr_t seg_addr, daddr;
704	ufs_daddr_t *dp, *idp;
705	struct ubuf *bp;
706	int i, j, k, datac, len;
707	long sn;
708	u_int32_t *datap;
709	u_int32_t ccksum;
710
711	sn = dtosn(fs, pseg_addr);
712	seg_addr = sntod(fs, sn);
713
714	/* We've already checked the sumsum, just do the data bounds and sum */
715
716	/* Count the blocks. */
717	nblocks = howmany(sp->ss_ninos, INOPB(fs));
718	bc = nblocks << (fs->lfs_version > 1 ? fs->lfs_ffshift : fs->lfs_bshift);
719	assert(bc >= 0);
720
721	fp = (FINFO *) (sp + 1);
722	for (i = 0; i < sp->ss_nfinfo; i++) {
723		nblocks += fp->fi_nblocks;
724		bc += fp->fi_lastlength + ((fp->fi_nblocks - 1)
725					   << fs->lfs_bshift);
726		assert(bc >= 0);
727		fp = (FINFO *) (fp->fi_blocks + fp->fi_nblocks);
728	}
729	datap = (u_int32_t *) malloc(nblocks * sizeof(*datap));
730	datac = 0;
731
732	dp = (ufs_daddr_t *) sp;
733	dp += fs->lfs_sumsize / sizeof(ufs_daddr_t);
734	dp--;
735
736	idp = dp;
737	daddr = pseg_addr + btofsb(fs, fs->lfs_sumsize);
738	fp = (FINFO *) (sp + 1);
739	for (i = 0, j = 0;
740	     i < sp->ss_nfinfo || j < howmany(sp->ss_ninos, INOPB(fs)); i++) {
741		if (i >= sp->ss_nfinfo && *idp != daddr) {
742			pwarn("Not enough inode blocks in pseg at 0x%" PRIx32
743			      ": found %d, wanted %d\n",
744			      pseg_addr, j, howmany(sp->ss_ninos, INOPB(fs)));
745			if (debug)
746				pwarn("*idp=%x, daddr=%" PRIx32 "\n", *idp,
747				      daddr);
748			break;
749		}
750		while (j < howmany(sp->ss_ninos, INOPB(fs)) && *idp == daddr) {
751			bread(devvp, fsbtodb(fs, daddr), fs->lfs_ibsize, NOCRED, &bp);
752			datap[datac++] = ((u_int32_t *) (bp->b_data))[0];
753			brelse(bp);
754
755			++j;
756			daddr += btofsb(fs, fs->lfs_ibsize);
757			--idp;
758		}
759		if (i < sp->ss_nfinfo) {
760			if (func)
761				func(daddr, fp);
762			for (k = 0; k < fp->fi_nblocks; k++) {
763				len = (k == fp->fi_nblocks - 1 ?
764				       fp->fi_lastlength
765				       : fs->lfs_bsize);
766				bread(devvp, fsbtodb(fs, daddr), len, NOCRED, &bp);
767				datap[datac++] = ((u_int32_t *) (bp->b_data))[0];
768				brelse(bp);
769				daddr += btofsb(fs, len);
770			}
771			fp = (FINFO *) (fp->fi_blocks + fp->fi_nblocks);
772		}
773	}
774
775	if (datac != nblocks) {
776		pwarn("Partial segment at 0x%llx expected %d blocks counted %d\n",
777		    (long long) pseg_addr, nblocks, datac);
778	}
779	ccksum = cksum(datap, nblocks * sizeof(u_int32_t));
780	/* Check the data checksum */
781	if (ccksum != sp->ss_datasum) {
782		pwarn("Partial segment at 0x%" PRIx32 " data checksum"
783		      " mismatch: given 0x%x, computed 0x%x\n",
784		      pseg_addr, sp->ss_datasum, ccksum);
785		free(datap);
786		return 0;
787	}
788	free(datap);
789	assert(bc >= 0);
790	return bc;
791}
792
793/* print message and exit */
794void
795my_vpanic(int fatal, const char *fmt, va_list ap)
796{
797        (void) vprintf(fmt, ap);
798	exit(8);
799}
800
801void
802call_panic(const char *fmt, ...)
803{
804	va_list ap;
805
806	va_start(ap, fmt);
807        panic_func(1, fmt, ap);
808	va_end(ap);
809}
810
811/* Allocate a new inode. */
812struct uvnode *
813lfs_valloc(struct lfs *fs, ino_t ino)
814{
815	struct ubuf *bp, *cbp;
816	struct ifile *ifp;
817	ino_t new_ino;
818	int error;
819	int new_gen;
820	CLEANERINFO *cip;
821
822	/* Get the head of the freelist. */
823	LFS_GET_HEADFREE(fs, cip, cbp, &new_ino);
824
825	/*
826	 * Remove the inode from the free list and write the new start
827	 * of the free list into the superblock.
828	 */
829	LFS_IENTRY(ifp, fs, new_ino, bp);
830	if (ifp->if_daddr != LFS_UNUSED_DADDR)
831		panic("lfs_valloc: inuse inode %d on the free list", new_ino);
832	LFS_PUT_HEADFREE(fs, cip, cbp, ifp->if_nextfree);
833
834	new_gen = ifp->if_version; /* version was updated by vfree */
835	brelse(bp);
836
837	/* Extend IFILE so that the next lfs_valloc will succeed. */
838	if (fs->lfs_freehd == LFS_UNUSED_INUM) {
839		if ((error = extend_ifile(fs)) != 0) {
840			LFS_PUT_HEADFREE(fs, cip, cbp, new_ino);
841			return NULL;
842		}
843	}
844
845	/* Set superblock modified bit and increment file count. */
846        sbdirty();
847	++fs->lfs_nfiles;
848
849        return lfs_raw_vget(fs, ino, fs->lfs_devvp->v_fd, 0x0);
850}
851
852/*
853 * Add a new block to the Ifile, to accommodate future file creations.
854 */
855int
856extend_ifile(struct lfs *fs)
857{
858	struct uvnode *vp;
859	struct inode *ip;
860	IFILE *ifp;
861	IFILE_V1 *ifp_v1;
862	struct ubuf *bp, *cbp;
863	daddr_t i, blkno, max;
864	ino_t oldlast;
865	CLEANERINFO *cip;
866
867	vp = fs->lfs_ivnode;
868	ip = VTOI(vp);
869	blkno = lblkno(fs, ip->i_ffs1_size);
870
871	bp = getblk(vp, blkno, fs->lfs_bsize);	/* XXX VOP_BALLOC() */
872	ip->i_ffs1_size += fs->lfs_bsize;
873
874	i = (blkno - fs->lfs_segtabsz - fs->lfs_cleansz) *
875		fs->lfs_ifpb;
876	LFS_GET_HEADFREE(fs, cip, cbp, &oldlast);
877	LFS_PUT_HEADFREE(fs, cip, cbp, i);
878	max = i + fs->lfs_ifpb;
879	fs->lfs_bfree -= btofsb(fs, fs->lfs_bsize);
880
881	if (fs->lfs_version == 1) {
882		for (ifp_v1 = (IFILE_V1 *)bp->b_data; i < max; ++ifp_v1) {
883			ifp_v1->if_version = 1;
884			ifp_v1->if_daddr = LFS_UNUSED_DADDR;
885			ifp_v1->if_nextfree = ++i;
886		}
887		ifp_v1--;
888		ifp_v1->if_nextfree = oldlast;
889	} else {
890		for (ifp = (IFILE *)bp->b_data; i < max; ++ifp) {
891			ifp->if_version = 1;
892			ifp->if_daddr = LFS_UNUSED_DADDR;
893			ifp->if_nextfree = ++i;
894		}
895		ifp--;
896		ifp->if_nextfree = oldlast;
897	}
898	LFS_PUT_TAILFREE(fs, cip, cbp, max - 1);
899
900	LFS_BWRITE_LOG(bp);
901
902	return 0;
903}
904
905