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