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