Home | History | Annotate | Line # | Download | only in fsck_lfs
segwrite.c revision 1.41
      1  1.41  dholland /* $NetBSD: segwrite.c,v 1.41 2015/08/12 18:28:00 dholland 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) 1991, 1993
     32   1.1  perseant  *	The Regents of the University of California.  All rights reserved.
     33   1.1  perseant  *
     34   1.1  perseant  * Redistribution and use in source and binary forms, with or without
     35   1.1  perseant  * modification, are permitted provided that the following conditions
     36   1.1  perseant  * are met:
     37   1.1  perseant  * 1. Redistributions of source code must retain the above copyright
     38   1.1  perseant  *    notice, this list of conditions and the following disclaimer.
     39   1.1  perseant  * 2. Redistributions in binary form must reproduce the above copyright
     40   1.1  perseant  *    notice, this list of conditions and the following disclaimer in the
     41   1.1  perseant  *    documentation and/or other materials provided with the distribution.
     42   1.5       agc  * 3. Neither the name of the University nor the names of its contributors
     43   1.1  perseant  *    may be used to endorse or promote products derived from this software
     44   1.1  perseant  *    without specific prior written permission.
     45   1.1  perseant  *
     46   1.1  perseant  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     47   1.1  perseant  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     48   1.1  perseant  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     49   1.1  perseant  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     50   1.1  perseant  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     51   1.1  perseant  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     52   1.1  perseant  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     53   1.1  perseant  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     54   1.1  perseant  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     55   1.1  perseant  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     56   1.1  perseant  * SUCH DAMAGE.
     57   1.1  perseant  *
     58   1.1  perseant  *	@(#)lfs_segment.c	8.10 (Berkeley) 6/10/95
     59   1.1  perseant  */
     60   1.1  perseant 
     61   1.1  perseant /*
     62   1.1  perseant  * Partial segment writer, taken from the kernel and adapted for userland.
     63   1.1  perseant  */
     64   1.1  perseant #include <sys/types.h>
     65   1.1  perseant #include <sys/param.h>
     66   1.1  perseant #include <sys/time.h>
     67   1.1  perseant #include <sys/buf.h>
     68   1.1  perseant #include <sys/mount.h>
     69   1.1  perseant 
     70   1.1  perseant /* Override certain things to make <ufs/lfs/lfs.h> work */
     71  1.23  dholland #define VU_DIROP 0x01000000 /* XXX XXX from sys/vnode.h */
     72   1.1  perseant #define vnode uvnode
     73   1.1  perseant #define buf ubuf
     74   1.1  perseant #define panic call_panic
     75   1.1  perseant 
     76   1.1  perseant #include <ufs/lfs/lfs.h>
     77  1.34  dholland #include <ufs/lfs/lfs_accessors.h>
     78  1.25  dholland #include <ufs/lfs/lfs_inode.h>
     79   1.1  perseant 
     80   1.1  perseant #include <assert.h>
     81   1.1  perseant #include <stdio.h>
     82   1.1  perseant #include <stdlib.h>
     83   1.1  perseant #include <string.h>
     84   1.1  perseant #include <err.h>
     85   1.1  perseant #include <errno.h>
     86  1.15  christos #include <util.h>
     87   1.1  perseant 
     88   1.1  perseant #include "bufcache.h"
     89   1.1  perseant #include "vnode.h"
     90  1.10  christos #include "lfs_user.h"
     91   1.1  perseant #include "segwrite.h"
     92   1.1  perseant 
     93   1.1  perseant /* Compatibility definitions */
     94   1.1  perseant extern off_t locked_queue_bytes;
     95   1.1  perseant int locked_queue_count;
     96   1.1  perseant off_t written_bytes = 0;
     97   1.1  perseant off_t written_data = 0;
     98   1.1  perseant off_t written_indir = 0;
     99   1.1  perseant off_t written_dev = 0;
    100   1.1  perseant int written_inodes = 0;
    101   1.1  perseant 
    102   1.1  perseant /* Global variables */
    103   1.1  perseant time_t write_time;
    104   1.1  perseant 
    105   1.1  perseant extern u_int32_t cksum(void *, size_t);
    106  1.36  dholland extern u_int32_t lfs_sb_cksum(struct lfs *);
    107   1.7  perseant extern int preen;
    108   1.1  perseant 
    109  1.40  dholland static void lfs_shellsort(struct lfs *,
    110  1.40  dholland 			  struct ubuf **, union lfs_blocks *, int, int);
    111  1.40  dholland 
    112   1.1  perseant /*
    113   1.1  perseant  * Logical block number match routines used when traversing the dirty block
    114   1.1  perseant  * chain.
    115   1.1  perseant  */
    116   1.1  perseant int
    117   1.1  perseant lfs_match_data(struct lfs * fs, struct ubuf * bp)
    118   1.1  perseant {
    119   1.1  perseant 	return (bp->b_lblkno >= 0);
    120   1.1  perseant }
    121   1.1  perseant 
    122   1.1  perseant int
    123   1.1  perseant lfs_match_indir(struct lfs * fs, struct ubuf * bp)
    124   1.1  perseant {
    125   1.1  perseant 	daddr_t lbn;
    126   1.1  perseant 
    127   1.1  perseant 	lbn = bp->b_lblkno;
    128  1.26  christos 	return (lbn < 0 && (-lbn - ULFS_NDADDR) % LFS_NINDIR(fs) == 0);
    129   1.1  perseant }
    130   1.1  perseant 
    131   1.1  perseant int
    132   1.1  perseant lfs_match_dindir(struct lfs * fs, struct ubuf * bp)
    133   1.1  perseant {
    134   1.1  perseant 	daddr_t lbn;
    135   1.1  perseant 
    136   1.1  perseant 	lbn = bp->b_lblkno;
    137  1.26  christos 	return (lbn < 0 && (-lbn - ULFS_NDADDR) % LFS_NINDIR(fs) == 1);
    138   1.1  perseant }
    139   1.1  perseant 
    140   1.1  perseant int
    141   1.1  perseant lfs_match_tindir(struct lfs * fs, struct ubuf * bp)
    142   1.1  perseant {
    143   1.1  perseant 	daddr_t lbn;
    144   1.1  perseant 
    145   1.1  perseant 	lbn = bp->b_lblkno;
    146  1.26  christos 	return (lbn < 0 && (-lbn - ULFS_NDADDR) % LFS_NINDIR(fs) == 2);
    147   1.1  perseant }
    148   1.1  perseant 
    149   1.1  perseant /*
    150   1.1  perseant  * Do a checkpoint.
    151   1.1  perseant  */
    152   1.1  perseant int
    153   1.1  perseant lfs_segwrite(struct lfs * fs, int flags)
    154   1.1  perseant {
    155   1.1  perseant 	struct inode *ip;
    156   1.1  perseant 	struct segment *sp;
    157   1.1  perseant 	struct uvnode *vp;
    158  1.39  dholland 	SEGSUM *ssp;
    159   1.1  perseant 	int redo;
    160   1.1  perseant 
    161   1.1  perseant 	lfs_seglock(fs, flags | SEGM_CKP);
    162   1.1  perseant 	sp = fs->lfs_sp;
    163   1.1  perseant 
    164   1.1  perseant 	lfs_writevnodes(fs, sp, VN_REG);
    165   1.1  perseant 	lfs_writevnodes(fs, sp, VN_DIROP);
    166  1.39  dholland 	ssp = (SEGSUM *)sp->segsum;
    167  1.39  dholland 	lfs_ss_setflags(fs, ssp, lfs_ss_getflags(fs, ssp) & ~(SS_CONT));
    168   1.1  perseant 
    169   1.1  perseant 	do {
    170   1.1  perseant 		vp = fs->lfs_ivnode;
    171   1.1  perseant 		fs->lfs_flags &= ~LFS_IFDIRTY;
    172   1.1  perseant 		ip = VTOI(vp);
    173  1.32  dholland 		if (LIST_FIRST(&vp->v_dirtyblkhd) != NULL || lfs_sb_getidaddr(fs) <= 0)
    174   1.1  perseant 			lfs_writefile(fs, sp, vp);
    175   1.1  perseant 
    176   1.1  perseant 		redo = lfs_writeinode(fs, sp, ip);
    177   1.1  perseant 		redo += lfs_writeseg(fs, sp);
    178   1.1  perseant 		redo += (fs->lfs_flags & LFS_IFDIRTY);
    179   1.1  perseant 	} while (redo);
    180   1.1  perseant 
    181   1.1  perseant 	lfs_segunlock(fs);
    182   1.1  perseant #if 0
    183   1.1  perseant 	printf("wrote %" PRId64 " bytes (%" PRId32 " fsb)\n",
    184  1.26  christos 		written_bytes, (ulfs_daddr_t)lfs_btofsb(fs, written_bytes));
    185   1.1  perseant 	printf("wrote %" PRId64 " bytes data (%" PRId32 " fsb)\n",
    186  1.26  christos 		written_data, (ulfs_daddr_t)lfs_btofsb(fs, written_data));
    187   1.1  perseant 	printf("wrote %" PRId64 " bytes indir (%" PRId32 " fsb)\n",
    188  1.26  christos 		written_indir, (ulfs_daddr_t)lfs_btofsb(fs, written_indir));
    189   1.1  perseant 	printf("wrote %" PRId64 " bytes dev (%" PRId32 " fsb)\n",
    190  1.26  christos 		written_dev, (ulfs_daddr_t)lfs_btofsb(fs, written_dev));
    191   1.1  perseant 	printf("wrote %d inodes (%" PRId32 " fsb)\n",
    192  1.26  christos 		written_inodes, lfs_btofsb(fs, written_inodes * fs->lfs_ibsize));
    193   1.1  perseant #endif
    194   1.1  perseant 	return 0;
    195   1.1  perseant }
    196   1.1  perseant 
    197   1.1  perseant /*
    198   1.1  perseant  * Write the dirty blocks associated with a vnode.
    199   1.1  perseant  */
    200   1.1  perseant void
    201   1.1  perseant lfs_writefile(struct lfs * fs, struct segment * sp, struct uvnode * vp)
    202   1.1  perseant {
    203   1.1  perseant 	struct ubuf *bp;
    204  1.40  dholland 	FINFO *fip;
    205   1.1  perseant 	struct inode *ip;
    206   1.1  perseant 	IFILE *ifp;
    207  1.39  dholland 	SEGSUM *ssp;
    208   1.1  perseant 
    209   1.1  perseant 	ip = VTOI(vp);
    210   1.1  perseant 
    211  1.32  dholland 	if (sp->seg_bytes_left < lfs_sb_getbsize(fs) ||
    212  1.40  dholland 	    sp->sum_bytes_left < FINFOSIZE(fs) + LFS_BLKPTRSIZE(fs))
    213   1.1  perseant 		(void) lfs_writeseg(fs, sp);
    214   1.1  perseant 
    215  1.40  dholland 	sp->sum_bytes_left -= FINFOSIZE(fs);
    216  1.39  dholland 	ssp = (SEGSUM *)sp->segsum;
    217  1.39  dholland 	lfs_ss_setnfinfo(fs, ssp, lfs_ss_getnfinfo(fs, ssp) + 1);
    218   1.1  perseant 
    219  1.39  dholland 	if (vp->v_uflag & VU_DIROP) {
    220  1.39  dholland 		lfs_ss_setflags(fs, ssp,
    221  1.39  dholland 				lfs_ss_getflags(fs, ssp) | (SS_DIROP | SS_CONT));
    222  1.39  dholland 	}
    223   1.1  perseant 
    224   1.1  perseant 	fip = sp->fip;
    225  1.40  dholland 	lfs_fi_setnblocks(fs, fip, 0);
    226  1.40  dholland 	lfs_fi_setino(fs, fip, ip->i_number);
    227  1.40  dholland 	LFS_IENTRY(ifp, fs, lfs_fi_getino(fs, fip), bp);
    228  1.40  dholland 	lfs_fi_setversion(fs, fip, lfs_if_getversion(fs, ifp));
    229  1.16        ad 	brelse(bp, 0);
    230   1.1  perseant 
    231   1.1  perseant 	lfs_gather(fs, sp, vp, lfs_match_data);
    232   1.1  perseant 	lfs_gather(fs, sp, vp, lfs_match_indir);
    233   1.1  perseant 	lfs_gather(fs, sp, vp, lfs_match_dindir);
    234   1.1  perseant 	lfs_gather(fs, sp, vp, lfs_match_tindir);
    235   1.1  perseant 
    236   1.1  perseant 	fip = sp->fip;
    237  1.40  dholland 	if (lfs_fi_getnblocks(fs, fip) != 0) {
    238  1.39  dholland 		sp->fip = NEXT_FINFO(fs, fip);
    239  1.40  dholland 		lfs_blocks_fromfinfo(fs, &sp->start_lbp, sp->fip);
    240   1.1  perseant 	} else {
    241  1.40  dholland 		/* XXX shouldn't this update sp->fip? */
    242  1.40  dholland 		sp->sum_bytes_left += FINFOSIZE(fs);
    243  1.39  dholland 		lfs_ss_setnfinfo(fs, ssp, lfs_ss_getnfinfo(fs, ssp) - 1);
    244   1.1  perseant 	}
    245   1.1  perseant }
    246   1.1  perseant 
    247   1.1  perseant int
    248   1.1  perseant lfs_writeinode(struct lfs * fs, struct segment * sp, struct inode * ip)
    249   1.1  perseant {
    250   1.1  perseant 	struct ubuf *bp, *ibp;
    251  1.41  dholland 	union lfs_dinode *cdp;
    252   1.1  perseant 	IFILE *ifp;
    253   1.1  perseant 	SEGUSE *sup;
    254  1.39  dholland 	SEGSUM *ssp;
    255   1.1  perseant 	daddr_t daddr;
    256   1.1  perseant 	ino_t ino;
    257  1.27  christos 	int i, ndx, fsb = 0;
    258   1.1  perseant 	int redo_ifile = 0;
    259   1.1  perseant 	struct timespec ts;
    260   1.1  perseant 	int gotblk = 0;
    261   1.1  perseant 
    262   1.1  perseant 	/* Allocate a new inode block if necessary. */
    263   1.1  perseant 	if ((ip->i_number != LFS_IFILE_INUM || sp->idp == NULL) &&
    264   1.1  perseant 	    sp->ibp == NULL) {
    265   1.1  perseant 		/* Allocate a new segment if necessary. */
    266  1.33  dholland 		if (sp->seg_bytes_left < lfs_sb_getibsize(fs) ||
    267  1.22  dholland 		    sp->sum_bytes_left < sizeof(ulfs_daddr_t))
    268   1.1  perseant 			(void) lfs_writeseg(fs, sp);
    269   1.1  perseant 
    270   1.1  perseant 		/* Get next inode block. */
    271  1.32  dholland 		daddr = lfs_sb_getoffset(fs);
    272  1.32  dholland 		lfs_sb_addoffset(fs, lfs_btofsb(fs, lfs_sb_getibsize(fs)));
    273   1.1  perseant 		sp->ibp = *sp->cbpp++ =
    274  1.26  christos 		    getblk(fs->lfs_devvp, LFS_FSBTODB(fs, daddr),
    275  1.33  dholland 		    lfs_sb_getibsize(fs));
    276   1.1  perseant 		sp->ibp->b_flags |= B_GATHERED;
    277   1.1  perseant 		gotblk++;
    278   1.1  perseant 
    279   1.1  perseant 		/* Zero out inode numbers */
    280  1.41  dholland 		for (i = 0; i < LFS_INOPB(fs); ++i) {
    281  1.41  dholland 			union lfs_dinode *tmpdip;
    282  1.41  dholland 
    283  1.41  dholland 			tmpdip = DINO_IN_BLOCK(fs, sp->ibp->b_data, i);
    284  1.41  dholland 			lfs_dino_setinumber(fs, tmpdip, 0);
    285  1.41  dholland 		}
    286   1.1  perseant 
    287   1.1  perseant 		++sp->start_bpp;
    288  1.32  dholland 		lfs_sb_subavail(fs, lfs_btofsb(fs, lfs_sb_getibsize(fs)));
    289   1.1  perseant 		/* Set remaining space counters. */
    290  1.32  dholland 		sp->seg_bytes_left -= lfs_sb_getibsize(fs);
    291  1.22  dholland 		sp->sum_bytes_left -= sizeof(ulfs_daddr_t);
    292  1.32  dholland 		ndx = lfs_sb_getsumsize(fs) / sizeof(ulfs_daddr_t) -
    293  1.26  christos 		    sp->ninodes / LFS_INOPB(fs) - 1;
    294  1.22  dholland 		((ulfs_daddr_t *) (sp->segsum))[ndx] = daddr;
    295   1.1  perseant 	}
    296   1.1  perseant 	/* Update the inode times and copy the inode onto the inode page. */
    297   1.1  perseant 	ts.tv_nsec = 0;
    298   1.1  perseant 	ts.tv_sec = write_time;
    299   1.1  perseant 	/* XXX kludge --- don't redirty the ifile just to put times on it */
    300   1.1  perseant 	if (ip->i_number != LFS_IFILE_INUM)
    301   1.1  perseant 		LFS_ITIMES(ip, &ts, &ts, &ts);
    302   1.1  perseant 
    303   1.1  perseant 	/*
    304   1.1  perseant 	 * If this is the Ifile, and we've already written the Ifile in this
    305   1.1  perseant 	 * partial segment, just overwrite it (it's not on disk yet) and
    306   1.1  perseant 	 * continue.
    307   1.1  perseant 	 *
    308   1.1  perseant 	 * XXX we know that the bp that we get the second time around has
    309   1.1  perseant 	 * already been gathered.
    310   1.1  perseant 	 */
    311   1.1  perseant 	if (ip->i_number == LFS_IFILE_INUM && sp->idp) {
    312  1.41  dholland 		// XXX weak
    313  1.41  dholland 		if (fs->lfs_is64) {
    314  1.41  dholland 			sp->idp->u_64 = *ip->i_din.ffs2_din;
    315  1.41  dholland 		} else {
    316  1.41  dholland 			sp->idp->u_32 = *ip->i_din.ffs1_din;
    317  1.41  dholland 		}
    318   1.3      fvdl 		ip->i_lfs_osize = ip->i_ffs1_size;
    319   1.1  perseant 		return 0;
    320   1.1  perseant 	}
    321   1.1  perseant 	bp = sp->ibp;
    322  1.41  dholland 	cdp = DINO_IN_BLOCK(fs, bp->b_data, sp->ninodes % LFS_INOPB(fs));
    323  1.41  dholland 	// XXX weak
    324  1.41  dholland 	if (fs->lfs_is64) {
    325  1.41  dholland 		cdp->u_64 = *ip->i_din.ffs2_din;
    326  1.41  dholland 	} else {
    327  1.41  dholland 		cdp->u_32 = *ip->i_din.ffs1_din;
    328  1.41  dholland 	}
    329   1.1  perseant 
    330   1.1  perseant 	/* If all blocks are goig to disk, update the "size on disk" */
    331   1.3      fvdl 	ip->i_lfs_osize = ip->i_ffs1_size;
    332   1.1  perseant 
    333   1.1  perseant 	if (ip->i_number == LFS_IFILE_INUM)	/* We know sp->idp == NULL */
    334  1.41  dholland 		sp->idp = DINO_IN_BLOCK(fs, bp->b_data, sp->ninodes % LFS_INOPB(fs));
    335   1.1  perseant 	if (gotblk) {
    336   1.1  perseant 		LFS_LOCK_BUF(bp);
    337  1.12   jnemeth 		assert(!(bp->b_flags & B_INVAL));
    338  1.16        ad 		brelse(bp, 0);
    339   1.1  perseant 	}
    340   1.1  perseant 	/* Increment inode count in segment summary block. */
    341  1.39  dholland 	ssp = (SEGSUM *)sp->segsum;
    342  1.39  dholland 	lfs_ss_setninos(fs, ssp, lfs_ss_getninos(fs, ssp) + 1);
    343   1.1  perseant 
    344   1.1  perseant 	/* If this page is full, set flag to allocate a new page. */
    345  1.26  christos 	if (++sp->ninodes % LFS_INOPB(fs) == 0)
    346   1.1  perseant 		sp->ibp = NULL;
    347   1.1  perseant 
    348   1.1  perseant 	/*
    349   1.1  perseant 	 * If updating the ifile, update the super-block.  Update the disk
    350  1.38  dholland 	 * address for this inode in the ifile.
    351   1.1  perseant 	 */
    352   1.1  perseant 	ino = ip->i_number;
    353   1.1  perseant 	if (ino == LFS_IFILE_INUM) {
    354  1.32  dholland 		daddr = lfs_sb_getidaddr(fs);
    355  1.32  dholland 		lfs_sb_setidaddr(fs, LFS_DBTOFSB(fs, bp->b_blkno));
    356  1.13  perseant 		sbdirty();
    357   1.1  perseant 	} else {
    358   1.1  perseant 		LFS_IENTRY(ifp, fs, ino, ibp);
    359  1.38  dholland 		daddr = lfs_if_getdaddr(fs, ifp);
    360  1.38  dholland 		lfs_if_setdaddr(fs, ifp, LFS_DBTOFSB(fs, bp->b_blkno) + fsb);
    361  1.27  christos 		(void)LFS_BWRITE_LOG(ibp);	/* Ifile */
    362   1.1  perseant 	}
    363   1.1  perseant 
    364   1.1  perseant 	/*
    365   1.1  perseant 	 * Account the inode: it no longer belongs to its former segment,
    366   1.1  perseant 	 * though it will not belong to the new segment until that segment
    367   1.1  perseant 	 * is actually written.
    368   1.1  perseant 	 */
    369   1.1  perseant 	if (daddr != LFS_UNUSED_DADDR) {
    370  1.26  christos 		u_int32_t oldsn = lfs_dtosn(fs, daddr);
    371   1.1  perseant 		LFS_SEGENTRY(sup, fs, oldsn, bp);
    372  1.41  dholland 		sup->su_nbytes -= DINOSIZE(fs);
    373   1.1  perseant 		redo_ifile =
    374   1.1  perseant 		    (ino == LFS_IFILE_INUM && !(bp->b_flags & B_GATHERED));
    375   1.1  perseant 		if (redo_ifile)
    376   1.1  perseant 			fs->lfs_flags |= LFS_IFDIRTY;
    377   1.1  perseant 		LFS_WRITESEGENTRY(sup, fs, oldsn, bp);	/* Ifile */
    378   1.1  perseant 	}
    379   1.1  perseant 	return redo_ifile;
    380   1.1  perseant }
    381   1.1  perseant 
    382   1.1  perseant int
    383   1.1  perseant lfs_gatherblock(struct segment * sp, struct ubuf * bp)
    384   1.1  perseant {
    385   1.1  perseant 	struct lfs *fs;
    386  1.39  dholland 	SEGSUM *ssp;
    387   1.1  perseant 	int version;
    388   1.1  perseant 	int j, blksinblk;
    389   1.1  perseant 
    390   1.1  perseant 	/*
    391   1.1  perseant 	 * If full, finish this segment.  We may be doing I/O, so
    392   1.1  perseant 	 * release and reacquire the splbio().
    393   1.1  perseant 	 */
    394   1.1  perseant 	fs = sp->fs;
    395  1.32  dholland 	blksinblk = howmany(bp->b_bcount, lfs_sb_getbsize(fs));
    396  1.22  dholland 	if (sp->sum_bytes_left < sizeof(ulfs_daddr_t) * blksinblk ||
    397   1.1  perseant 	    sp->seg_bytes_left < bp->b_bcount) {
    398   1.1  perseant 		lfs_updatemeta(sp);
    399   1.1  perseant 
    400  1.40  dholland 		version = lfs_fi_getversion(fs, sp->fip);
    401   1.1  perseant 		(void) lfs_writeseg(fs, sp);
    402   1.1  perseant 
    403  1.40  dholland 		lfs_fi_setversion(fs, sp->fip, version);
    404  1.40  dholland 		lfs_fi_setino(fs, sp->fip, VTOI(sp->vp)->i_number);
    405   1.1  perseant 		/* Add the current file to the segment summary. */
    406  1.39  dholland 		ssp = (SEGSUM *)sp->segsum;
    407  1.39  dholland 		lfs_ss_setnfinfo(fs, ssp, lfs_ss_getnfinfo(fs, ssp) + 1);
    408  1.40  dholland 		sp->sum_bytes_left -= FINFOSIZE(fs);
    409   1.1  perseant 
    410   1.1  perseant 		return 1;
    411   1.1  perseant 	}
    412   1.1  perseant 	/* Insert into the buffer list, update the FINFO block. */
    413   1.1  perseant 	bp->b_flags |= B_GATHERED;
    414   1.1  perseant 	/* bp->b_flags &= ~B_DONE; */
    415   1.1  perseant 
    416   1.1  perseant 	*sp->cbpp++ = bp;
    417  1.40  dholland 	for (j = 0; j < blksinblk; j++) {
    418  1.40  dholland 		unsigned bn;
    419  1.40  dholland 
    420  1.40  dholland 		bn = lfs_fi_getnblocks(fs, sp->fip);
    421  1.40  dholland 		lfs_fi_setnblocks(fs, sp->fip, bn + 1);
    422  1.40  dholland 		lfs_fi_setblock(fs, sp->fip, bn, bp->b_lblkno + j);;
    423  1.40  dholland 	}
    424   1.1  perseant 
    425  1.22  dholland 	sp->sum_bytes_left -= sizeof(ulfs_daddr_t) * blksinblk;
    426   1.1  perseant 	sp->seg_bytes_left -= bp->b_bcount;
    427   1.1  perseant 	return 0;
    428   1.1  perseant }
    429   1.1  perseant 
    430   1.1  perseant int
    431   1.1  perseant lfs_gather(struct lfs * fs, struct segment * sp, struct uvnode * vp, int (*match) (struct lfs *, struct ubuf *))
    432   1.1  perseant {
    433   1.1  perseant 	struct ubuf *bp, *nbp;
    434   1.1  perseant 	int count = 0;
    435   1.1  perseant 
    436   1.1  perseant 	sp->vp = vp;
    437   1.1  perseant loop:
    438   1.1  perseant 	for (bp = LIST_FIRST(&vp->v_dirtyblkhd); bp; bp = nbp) {
    439   1.1  perseant 		nbp = LIST_NEXT(bp, b_vnbufs);
    440   1.1  perseant 
    441   1.1  perseant 		assert(bp->b_flags & B_DELWRI);
    442   1.1  perseant 		if ((bp->b_flags & (B_BUSY | B_GATHERED)) || !match(fs, bp)) {
    443   1.1  perseant 			continue;
    444   1.1  perseant 		}
    445   1.1  perseant 		if (lfs_gatherblock(sp, bp)) {
    446   1.1  perseant 			goto loop;
    447   1.1  perseant 		}
    448   1.1  perseant 		count++;
    449   1.1  perseant 	}
    450   1.1  perseant 
    451   1.1  perseant 	lfs_updatemeta(sp);
    452   1.1  perseant 	sp->vp = NULL;
    453   1.1  perseant 	return count;
    454   1.1  perseant }
    455   1.1  perseant 
    456   1.1  perseant 
    457   1.1  perseant /*
    458   1.1  perseant  * Change the given block's address to ndaddr, finding its previous
    459  1.22  dholland  * location using ulfs_bmaparray().
    460   1.1  perseant  *
    461   1.1  perseant  * Account for this change in the segment table.
    462   1.1  perseant  */
    463   1.1  perseant void
    464   1.1  perseant lfs_update_single(struct lfs * fs, struct segment * sp, daddr_t lbn,
    465  1.22  dholland     ulfs_daddr_t ndaddr, int size)
    466   1.1  perseant {
    467   1.1  perseant 	SEGUSE *sup;
    468   1.1  perseant 	struct ubuf *bp;
    469  1.22  dholland 	struct indir a[ULFS_NIADDR + 2], *ap;
    470   1.1  perseant 	struct inode *ip;
    471   1.1  perseant 	struct uvnode *vp;
    472   1.1  perseant 	daddr_t daddr, ooff;
    473   1.1  perseant 	int num, error;
    474  1.20   mlelstv 	int osize;
    475  1.20   mlelstv 	int frags, ofrags;
    476   1.1  perseant 
    477   1.1  perseant 	vp = sp->vp;
    478   1.1  perseant 	ip = VTOI(vp);
    479   1.1  perseant 
    480  1.22  dholland 	error = ulfs_bmaparray(fs, vp, lbn, &daddr, a, &num);
    481   1.1  perseant 	if (error)
    482  1.30  christos 		errx(EXIT_FAILURE, "%s: ulfs_bmaparray returned %d looking up lbn %"
    483  1.30  christos 		    PRId64 "", __func__, error, lbn);
    484   1.1  perseant 	if (daddr > 0)
    485  1.26  christos 		daddr = LFS_DBTOFSB(fs, daddr);
    486   1.1  perseant 
    487  1.26  christos 	frags = lfs_numfrags(fs, size);
    488   1.1  perseant 	switch (num) {
    489   1.1  perseant 	case 0:
    490   1.3      fvdl 		ooff = ip->i_ffs1_db[lbn];
    491   1.1  perseant 		if (ooff == UNWRITTEN)
    492  1.20   mlelstv 			ip->i_ffs1_blocks += frags;
    493   1.1  perseant 		else {
    494   1.1  perseant 			/* possible fragment truncation or extension */
    495  1.26  christos 			ofrags = lfs_btofsb(fs, ip->i_lfs_fragsize[lbn]);
    496  1.20   mlelstv 			ip->i_ffs1_blocks += (frags - ofrags);
    497   1.1  perseant 		}
    498   1.3      fvdl 		ip->i_ffs1_db[lbn] = ndaddr;
    499   1.1  perseant 		break;
    500   1.1  perseant 	case 1:
    501   1.3      fvdl 		ooff = ip->i_ffs1_ib[a[0].in_off];
    502   1.1  perseant 		if (ooff == UNWRITTEN)
    503  1.20   mlelstv 			ip->i_ffs1_blocks += frags;
    504   1.3      fvdl 		ip->i_ffs1_ib[a[0].in_off] = ndaddr;
    505   1.1  perseant 		break;
    506   1.1  perseant 	default:
    507   1.1  perseant 		ap = &a[num - 1];
    508  1.32  dholland 		if (bread(vp, ap->in_lbn, lfs_sb_getbsize(fs), 0, &bp))
    509  1.30  christos 			errx(EXIT_FAILURE, "%s: bread bno %" PRId64, __func__,
    510   1.1  perseant 			    ap->in_lbn);
    511   1.1  perseant 
    512  1.22  dholland 		ooff = ((ulfs_daddr_t *) bp->b_data)[ap->in_off];
    513   1.1  perseant 		if (ooff == UNWRITTEN)
    514  1.20   mlelstv 			ip->i_ffs1_blocks += frags;
    515  1.22  dholland 		((ulfs_daddr_t *) bp->b_data)[ap->in_off] = ndaddr;
    516   1.1  perseant 		(void) VOP_BWRITE(bp);
    517   1.1  perseant 	}
    518   1.1  perseant 
    519   1.1  perseant 	/*
    520   1.1  perseant 	 * Update segment usage information, based on old size
    521   1.1  perseant 	 * and location.
    522   1.1  perseant 	 */
    523   1.1  perseant 	if (daddr > 0) {
    524  1.26  christos 		u_int32_t oldsn = lfs_dtosn(fs, daddr);
    525  1.22  dholland 		if (lbn >= 0 && lbn < ULFS_NDADDR)
    526   1.1  perseant 			osize = ip->i_lfs_fragsize[lbn];
    527   1.1  perseant 		else
    528  1.32  dholland 			osize = lfs_sb_getbsize(fs);
    529   1.1  perseant 		LFS_SEGENTRY(sup, fs, oldsn, bp);
    530   1.1  perseant 		sup->su_nbytes -= osize;
    531   1.1  perseant 		if (!(bp->b_flags & B_GATHERED))
    532   1.1  perseant 			fs->lfs_flags |= LFS_IFDIRTY;
    533   1.1  perseant 		LFS_WRITESEGENTRY(sup, fs, oldsn, bp);
    534   1.1  perseant 	}
    535   1.1  perseant 	/*
    536   1.1  perseant 	 * Now that this block has a new address, and its old
    537   1.1  perseant 	 * segment no longer owns it, we can forget about its
    538   1.1  perseant 	 * old size.
    539   1.1  perseant 	 */
    540  1.22  dholland 	if (lbn >= 0 && lbn < ULFS_NDADDR)
    541   1.1  perseant 		ip->i_lfs_fragsize[lbn] = size;
    542   1.1  perseant }
    543   1.1  perseant 
    544   1.1  perseant /*
    545   1.1  perseant  * Update the metadata that points to the blocks listed in the FINFO
    546   1.1  perseant  * array.
    547   1.1  perseant  */
    548   1.1  perseant void
    549   1.1  perseant lfs_updatemeta(struct segment * sp)
    550   1.1  perseant {
    551   1.1  perseant 	struct ubuf *sbp;
    552   1.1  perseant 	struct lfs *fs;
    553   1.1  perseant 	struct uvnode *vp;
    554   1.1  perseant 	daddr_t lbn;
    555   1.1  perseant 	int i, nblocks, num;
    556  1.20   mlelstv 	int frags;
    557   1.1  perseant 	int bytesleft, size;
    558  1.40  dholland 	union lfs_blocks tmpptr;
    559   1.1  perseant 
    560  1.40  dholland 	fs = sp->fs;
    561   1.1  perseant 	vp = sp->vp;
    562  1.40  dholland 
    563  1.40  dholland 	/*
    564  1.40  dholland 	 * This code was cutpasted from the kernel. See the
    565  1.40  dholland 	 * corresponding comment in lfs_segment.c.
    566  1.40  dholland 	 */
    567  1.40  dholland #if 0
    568   1.1  perseant 	nblocks = &sp->fip->fi_blocks[sp->fip->fi_nblocks] - sp->start_lbp;
    569  1.40  dholland #else
    570  1.40  dholland 	lfs_blocks_fromvoid(fs, &tmpptr, (void *)NEXT_FINFO(fs, sp->fip));
    571  1.40  dholland 	nblocks = lfs_blocks_sub(fs, &tmpptr, &sp->start_lbp);
    572  1.40  dholland 	//nblocks_orig = nblocks;
    573  1.40  dholland #endif
    574   1.1  perseant 
    575   1.1  perseant 	if (vp == NULL || nblocks == 0)
    576   1.1  perseant 		return;
    577   1.1  perseant 
    578   1.1  perseant 	/*
    579   1.1  perseant 	 * This count may be high due to oversize blocks from lfs_gop_write.
    580   1.1  perseant 	 * Correct for this. (XXX we should be able to keep track of these.)
    581   1.1  perseant 	 */
    582   1.1  perseant 	for (i = 0; i < nblocks; i++) {
    583   1.1  perseant 		if (sp->start_bpp[i] == NULL) {
    584   1.1  perseant 			printf("nblocks = %d, not %d\n", i, nblocks);
    585   1.1  perseant 			nblocks = i;
    586   1.1  perseant 			break;
    587   1.1  perseant 		}
    588  1.32  dholland 		num = howmany(sp->start_bpp[i]->b_bcount, lfs_sb_getbsize(fs));
    589   1.1  perseant 		nblocks -= num - 1;
    590   1.1  perseant 	}
    591   1.1  perseant 
    592   1.1  perseant 	/*
    593   1.1  perseant 	 * Sort the blocks.
    594   1.1  perseant 	 */
    595  1.40  dholland 	lfs_shellsort(fs, sp->start_bpp, &sp->start_lbp, nblocks, lfs_sb_getbsize(fs));
    596   1.1  perseant 
    597   1.1  perseant 	/*
    598   1.1  perseant 	 * Record the length of the last block in case it's a fragment.
    599   1.1  perseant 	 * If there are indirect blocks present, they sort last.  An
    600   1.1  perseant 	 * indirect block will be lfs_bsize and its presence indicates
    601   1.1  perseant 	 * that you cannot have fragments.
    602   1.1  perseant 	 */
    603  1.40  dholland 	lfs_fi_setlastlength(fs, sp->fip, ((sp->start_bpp[nblocks - 1]->b_bcount - 1) &
    604  1.40  dholland 	    lfs_sb_getbmask(fs)) + 1);
    605   1.1  perseant 
    606   1.1  perseant 	/*
    607   1.1  perseant 	 * Assign disk addresses, and update references to the logical
    608   1.1  perseant 	 * block and the segment usage information.
    609   1.1  perseant 	 */
    610   1.1  perseant 	for (i = nblocks; i--; ++sp->start_bpp) {
    611   1.1  perseant 		sbp = *sp->start_bpp;
    612  1.40  dholland 		lbn = lfs_blocks_get(fs, &sp->start_lbp, 0);
    613   1.1  perseant 
    614  1.32  dholland 		sbp->b_blkno = LFS_FSBTODB(fs, lfs_sb_getoffset(fs));
    615   1.1  perseant 
    616   1.1  perseant 		/*
    617   1.1  perseant 		 * If we write a frag in the wrong place, the cleaner won't
    618   1.1  perseant 		 * be able to correctly identify its size later, and the
    619   1.1  perseant 		 * segment will be uncleanable.	 (Even worse, it will assume
    620   1.1  perseant 		 * that the indirect block that actually ends the list
    621   1.1  perseant 		 * is of a smaller size!)
    622   1.1  perseant 		 */
    623  1.33  dholland 		if ((sbp->b_bcount & lfs_sb_getbmask(fs)) && i != 0)
    624  1.30  christos 			errx(EXIT_FAILURE, "%s: fragment is not last block", __func__);
    625   1.1  perseant 
    626   1.1  perseant 		/*
    627   1.1  perseant 		 * For each subblock in this possibly oversized block,
    628   1.1  perseant 		 * update its address on disk.
    629   1.1  perseant 		 */
    630   1.1  perseant 		for (bytesleft = sbp->b_bcount; bytesleft > 0;
    631  1.32  dholland 		    bytesleft -= lfs_sb_getbsize(fs)) {
    632  1.32  dholland 			size = MIN(bytesleft, lfs_sb_getbsize(fs));
    633  1.26  christos 			frags = lfs_numfrags(fs, size);
    634  1.40  dholland 			lbn = lfs_blocks_get(fs, &sp->start_lbp, 0);
    635  1.40  dholland 			lfs_blocks_inc(fs, &sp->start_lbp);
    636  1.32  dholland 			lfs_update_single(fs, sp, lbn, lfs_sb_getoffset(fs), size);
    637  1.32  dholland 			lfs_sb_addoffset(fs, frags);
    638   1.1  perseant 		}
    639   1.1  perseant 
    640   1.1  perseant 	}
    641   1.1  perseant }
    642   1.1  perseant 
    643   1.1  perseant /*
    644   1.1  perseant  * Start a new segment.
    645   1.1  perseant  */
    646   1.1  perseant int
    647   1.1  perseant lfs_initseg(struct lfs * fs)
    648   1.1  perseant {
    649   1.1  perseant 	struct segment *sp;
    650   1.1  perseant 	SEGUSE *sup;
    651   1.1  perseant 	SEGSUM *ssp;
    652   1.1  perseant 	struct ubuf *bp, *sbp;
    653   1.1  perseant 	int repeat;
    654   1.1  perseant 
    655   1.1  perseant 	sp = fs->lfs_sp;
    656   1.1  perseant 
    657   1.1  perseant 	repeat = 0;
    658   1.1  perseant 
    659   1.1  perseant 	/* Advance to the next segment. */
    660   1.1  perseant 	if (!LFS_PARTIAL_FITS(fs)) {
    661   1.1  perseant 		/* lfs_avail eats the remaining space */
    662  1.32  dholland 		lfs_sb_subavail(fs, lfs_sb_getfsbpseg(fs) - (lfs_sb_getoffset(fs) -
    663  1.32  dholland 		    lfs_sb_getcurseg(fs)));
    664   1.1  perseant 		lfs_newseg(fs);
    665   1.1  perseant 		repeat = 1;
    666  1.32  dholland 		lfs_sb_setoffset(fs, lfs_sb_getcurseg(fs));
    667   1.1  perseant 
    668  1.32  dholland 		sp->seg_number = lfs_dtosn(fs, lfs_sb_getcurseg(fs));
    669  1.32  dholland 		sp->seg_bytes_left = lfs_fsbtob(fs, lfs_sb_getfsbpseg(fs));
    670   1.1  perseant 
    671   1.1  perseant 		/*
    672   1.1  perseant 		 * If the segment contains a superblock, update the offset
    673   1.1  perseant 		 * and summary address to skip over it.
    674   1.1  perseant 		 */
    675   1.1  perseant 		LFS_SEGENTRY(sup, fs, sp->seg_number, bp);
    676   1.1  perseant 		if (sup->su_flags & SEGUSE_SUPERBLOCK) {
    677  1.32  dholland 			lfs_sb_addoffset(fs, lfs_btofsb(fs, LFS_SBPAD));
    678   1.1  perseant 			sp->seg_bytes_left -= LFS_SBPAD;
    679   1.1  perseant 		}
    680  1.16        ad 		brelse(bp, 0);
    681   1.1  perseant 		/* Segment zero could also contain the labelpad */
    682  1.35  dholland 		if (lfs_sb_getversion(fs) > 1 && sp->seg_number == 0 &&
    683  1.33  dholland 		    lfs_sb_gets0addr(fs) < lfs_btofsb(fs, LFS_LABELPAD)) {
    684  1.33  dholland 			lfs_sb_addoffset(fs, lfs_btofsb(fs, LFS_LABELPAD) - lfs_sb_gets0addr(fs));
    685  1.33  dholland 			sp->seg_bytes_left -= LFS_LABELPAD - lfs_fsbtob(fs, lfs_sb_gets0addr(fs));
    686   1.1  perseant 		}
    687   1.1  perseant 	} else {
    688  1.32  dholland 		sp->seg_number = lfs_dtosn(fs, lfs_sb_getcurseg(fs));
    689  1.32  dholland 		sp->seg_bytes_left = lfs_fsbtob(fs, lfs_sb_getfsbpseg(fs) -
    690  1.32  dholland 		    (lfs_sb_getoffset(fs) - lfs_sb_getcurseg(fs)));
    691   1.1  perseant 	}
    692  1.32  dholland 	lfs_sb_setlastpseg(fs, lfs_sb_getoffset(fs));
    693   1.1  perseant 
    694   1.1  perseant 	sp->fs = fs;
    695   1.1  perseant 	sp->ibp = NULL;
    696   1.1  perseant 	sp->idp = NULL;
    697   1.1  perseant 	sp->ninodes = 0;
    698   1.1  perseant 	sp->ndupino = 0;
    699   1.1  perseant 
    700   1.1  perseant 	/* Get a new buffer for SEGSUM and enter it into the buffer list. */
    701   1.1  perseant 	sp->cbpp = sp->bpp;
    702   1.8  perseant 	sbp = *sp->cbpp = getblk(fs->lfs_devvp,
    703  1.32  dholland 	    LFS_FSBTODB(fs, lfs_sb_getoffset(fs)), lfs_sb_getsumsize(fs));
    704   1.1  perseant 	sp->segsum = sbp->b_data;
    705  1.32  dholland 	memset(sp->segsum, 0, lfs_sb_getsumsize(fs));
    706   1.1  perseant 	sp->start_bpp = ++sp->cbpp;
    707  1.32  dholland 	lfs_sb_addoffset(fs, lfs_btofsb(fs, lfs_sb_getsumsize(fs)));
    708   1.1  perseant 
    709   1.1  perseant 	/* Set point to SEGSUM, initialize it. */
    710   1.1  perseant 	ssp = sp->segsum;
    711  1.39  dholland 	lfs_ss_setnext(fs, ssp, lfs_sb_getnextseg(fs));
    712  1.39  dholland 	lfs_ss_setnfinfo(fs, ssp, 0);
    713  1.39  dholland 	lfs_ss_setninos(fs, ssp, 0);
    714  1.39  dholland 	lfs_ss_setmagic(fs, ssp, SS_MAGIC);
    715   1.1  perseant 
    716   1.1  perseant 	/* Set pointer to first FINFO, initialize it. */
    717  1.39  dholland 	sp->fip = SEGSUM_FINFOBASE(fs, ssp);
    718  1.40  dholland 	lfs_fi_setnblocks(fs, sp->fip, 0);
    719  1.40  dholland 	lfs_blocks_fromfinfo(fs, &sp->start_lbp, sp->fip);
    720  1.40  dholland 	lfs_fi_setlastlength(fs, sp->fip, 0);
    721   1.1  perseant 
    722  1.32  dholland 	sp->seg_bytes_left -= lfs_sb_getsumsize(fs);
    723  1.32  dholland 	sp->sum_bytes_left = lfs_sb_getsumsize(fs) - SEGSUM_SIZE(fs);
    724   1.1  perseant 
    725   1.1  perseant 	LFS_LOCK_BUF(sbp);
    726  1.16        ad 	brelse(sbp, 0);
    727   1.1  perseant 	return repeat;
    728   1.1  perseant }
    729   1.1  perseant 
    730   1.1  perseant /*
    731   1.1  perseant  * Return the next segment to write.
    732   1.1  perseant  */
    733   1.1  perseant void
    734   1.1  perseant lfs_newseg(struct lfs * fs)
    735   1.1  perseant {
    736   1.1  perseant 	CLEANERINFO *cip;
    737   1.1  perseant 	SEGUSE *sup;
    738   1.1  perseant 	struct ubuf *bp;
    739   1.1  perseant 	int curseg, isdirty, sn;
    740   1.1  perseant 
    741  1.32  dholland 	LFS_SEGENTRY(sup, fs, lfs_dtosn(fs, lfs_sb_getnextseg(fs)), bp);
    742   1.1  perseant 	sup->su_flags |= SEGUSE_DIRTY | SEGUSE_ACTIVE;
    743   1.1  perseant 	sup->su_nbytes = 0;
    744   1.1  perseant 	sup->su_nsums = 0;
    745   1.1  perseant 	sup->su_ninos = 0;
    746  1.32  dholland 	LFS_WRITESEGENTRY(sup, fs, lfs_dtosn(fs, lfs_sb_getnextseg(fs)), bp);
    747   1.1  perseant 
    748   1.1  perseant 	LFS_CLEANERINFO(cip, fs, bp);
    749  1.37  dholland 	lfs_ci_shiftcleantodirty(fs, cip, 1);
    750  1.37  dholland 	lfs_sb_setnclean(fs, lfs_ci_getclean(fs, cip));
    751   1.1  perseant 	LFS_SYNC_CLEANERINFO(cip, fs, bp, 1);
    752   1.1  perseant 
    753  1.32  dholland 	lfs_sb_setlastseg(fs, lfs_sb_getcurseg(fs));
    754  1.32  dholland 	lfs_sb_setcurseg(fs, lfs_sb_getnextseg(fs));
    755  1.32  dholland 	for (sn = curseg = lfs_dtosn(fs, lfs_sb_getcurseg(fs)) + lfs_sb_getinterleave(fs);;) {
    756  1.33  dholland 		sn = (sn + 1) % lfs_sb_getnseg(fs);
    757   1.1  perseant 		if (sn == curseg)
    758  1.30  christos 			errx(EXIT_FAILURE, "%s: no clean segments", __func__);
    759   1.1  perseant 		LFS_SEGENTRY(sup, fs, sn, bp);
    760   1.1  perseant 		isdirty = sup->su_flags & SEGUSE_DIRTY;
    761  1.16        ad 		brelse(bp, 0);
    762   1.1  perseant 
    763   1.1  perseant 		if (!isdirty)
    764   1.1  perseant 			break;
    765   1.1  perseant 	}
    766   1.1  perseant 
    767   1.1  perseant 	++fs->lfs_nactive;
    768  1.32  dholland 	lfs_sb_setnextseg(fs, lfs_sntod(fs, sn));
    769   1.1  perseant }
    770   1.1  perseant 
    771   1.1  perseant 
    772   1.1  perseant int
    773   1.1  perseant lfs_writeseg(struct lfs * fs, struct segment * sp)
    774   1.1  perseant {
    775   1.1  perseant 	struct ubuf **bpp, *bp;
    776   1.1  perseant 	SEGUSE *sup;
    777   1.1  perseant 	SEGSUM *ssp;
    778   1.1  perseant 	char *datap, *dp;
    779   1.1  perseant 	int i;
    780   1.1  perseant 	int do_again, nblocks, byteoffset;
    781   1.1  perseant 	size_t el_size;
    782   1.1  perseant 	u_short ninos;
    783  1.39  dholland 	size_t sumstart;
    784   1.1  perseant 	struct uvnode *devvp;
    785   1.1  perseant 
    786   1.1  perseant 	/*
    787   1.1  perseant 	 * If there are no buffers other than the segment summary to write
    788   1.1  perseant 	 * and it is not a checkpoint, don't do anything.  On a checkpoint,
    789   1.1  perseant 	 * even if there aren't any buffers, you need to write the superblock.
    790   1.1  perseant 	 */
    791  1.13  perseant 	nblocks = sp->cbpp - sp->bpp;
    792  1.13  perseant #if 0
    793  1.13  perseant 	printf("write %d blocks at 0x%x\n",
    794  1.26  christos 		nblocks, (int)LFS_DBTOFSB(fs, (*sp->bpp)->b_blkno));
    795  1.13  perseant #endif
    796  1.13  perseant 	if (nblocks == 1)
    797   1.1  perseant 		return 0;
    798   1.1  perseant 
    799   1.8  perseant 	devvp = fs->lfs_devvp;
    800   1.1  perseant 
    801   1.1  perseant 	/* Update the segment usage information. */
    802   1.1  perseant 	LFS_SEGENTRY(sup, fs, sp->seg_number, bp);
    803  1.13  perseant 	sup->su_flags |= SEGUSE_DIRTY | SEGUSE_ACTIVE;
    804   1.1  perseant 
    805   1.1  perseant 	/* Loop through all blocks, except the segment summary. */
    806   1.1  perseant 	for (bpp = sp->bpp; ++bpp < sp->cbpp;) {
    807   1.1  perseant 		if ((*bpp)->b_vp != devvp) {
    808   1.1  perseant 			sup->su_nbytes += (*bpp)->b_bcount;
    809   1.1  perseant 		}
    810  1.26  christos 		assert(lfs_dtosn(fs, LFS_DBTOFSB(fs, (*bpp)->b_blkno)) == sp->seg_number);
    811   1.1  perseant 	}
    812   1.1  perseant 
    813   1.1  perseant 	ssp = (SEGSUM *) sp->segsum;
    814  1.39  dholland 	lfs_ss_setflags(fs, ssp, lfs_ss_getflags(fs, ssp) | SS_RFW);
    815   1.1  perseant 
    816  1.39  dholland 	ninos = (lfs_ss_getninos(fs, ssp) + LFS_INOPB(fs) - 1) / LFS_INOPB(fs);
    817  1.41  dholland 	sup->su_nbytes += lfs_ss_getninos(fs, ssp) * DINOSIZE(fs);
    818   1.1  perseant 
    819  1.35  dholland 	if (lfs_sb_getversion(fs) == 1)
    820   1.1  perseant 		sup->su_olastmod = write_time;
    821   1.1  perseant 	else
    822   1.1  perseant 		sup->su_lastmod = write_time;
    823   1.1  perseant 	sup->su_ninos += ninos;
    824   1.1  perseant 	++sup->su_nsums;
    825  1.33  dholland 	lfs_sb_adddmeta(fs, (lfs_btofsb(fs, lfs_sb_getsumsize(fs)) + lfs_btofsb(fs, ninos *
    826  1.33  dholland 		lfs_sb_getibsize(fs))));
    827  1.32  dholland 	lfs_sb_subavail(fs, lfs_btofsb(fs, lfs_sb_getsumsize(fs)));
    828   1.1  perseant 
    829   1.1  perseant 	do_again = !(bp->b_flags & B_GATHERED);
    830   1.1  perseant 	LFS_WRITESEGENTRY(sup, fs, sp->seg_number, bp);	/* Ifile */
    831   1.1  perseant 
    832   1.1  perseant 	/*
    833   1.1  perseant 	 * Compute checksum across data and then across summary; the first
    834   1.1  perseant 	 * block (the summary block) is skipped.  Set the create time here
    835   1.1  perseant 	 * so that it's guaranteed to be later than the inode mod times.
    836   1.1  perseant 	 */
    837  1.35  dholland 	if (lfs_sb_getversion(fs) == 1)
    838   1.1  perseant 		el_size = sizeof(u_long);
    839   1.1  perseant 	else
    840   1.1  perseant 		el_size = sizeof(u_int32_t);
    841  1.15  christos 	datap = dp = emalloc(nblocks * el_size);
    842   1.1  perseant 	for (bpp = sp->bpp, i = nblocks - 1; i--;) {
    843   1.1  perseant 		++bpp;
    844   1.1  perseant 		/* Loop through gop_write cluster blocks */
    845   1.1  perseant 		for (byteoffset = 0; byteoffset < (*bpp)->b_bcount;
    846  1.32  dholland 		    byteoffset += lfs_sb_getbsize(fs)) {
    847   1.1  perseant 			memcpy(dp, (*bpp)->b_data + byteoffset, el_size);
    848   1.1  perseant 			dp += el_size;
    849   1.1  perseant 		}
    850   1.2  perseant 		bremfree(*bpp);
    851   1.1  perseant 		(*bpp)->b_flags |= B_BUSY;
    852   1.1  perseant 	}
    853  1.35  dholland 	if (lfs_sb_getversion(fs) == 1)
    854  1.39  dholland 		lfs_ss_setocreate(fs, ssp, write_time);
    855   1.1  perseant 	else {
    856  1.39  dholland 		lfs_ss_setcreate(fs, ssp, write_time);
    857  1.32  dholland 		lfs_sb_addserial(fs, 1);
    858  1.39  dholland 		lfs_ss_setserial(fs, ssp, lfs_sb_getserial(fs));
    859  1.39  dholland 		lfs_ss_setident(fs, ssp, lfs_sb_getident(fs));
    860   1.1  perseant 	}
    861   1.1  perseant 	/* Set the summary block busy too */
    862   1.1  perseant 	bremfree(*(sp->bpp));
    863   1.1  perseant 	(*(sp->bpp))->b_flags |= B_BUSY;
    864   1.1  perseant 
    865  1.39  dholland 	lfs_ss_setdatasum(fs, ssp, cksum(datap, (nblocks - 1) * el_size));
    866  1.39  dholland 	sumstart = lfs_ss_getsumstart(fs);
    867  1.39  dholland 	lfs_ss_setsumsum(fs, ssp,
    868  1.39  dholland 	    cksum((char *)ssp + sumstart, lfs_sb_getsumsize(fs) - sumstart));
    869   1.1  perseant 	free(datap);
    870   1.1  perseant 	datap = dp = NULL;
    871  1.32  dholland 	lfs_sb_subbfree(fs, (lfs_btofsb(fs, ninos * lfs_sb_getibsize(fs)) +
    872  1.32  dholland 	    lfs_btofsb(fs, lfs_sb_getsumsize(fs))));
    873   1.1  perseant 
    874   1.1  perseant 	if (devvp == NULL)
    875  1.30  christos 		errx(EXIT_FAILURE, "devvp is NULL");
    876   1.1  perseant 	for (bpp = sp->bpp, i = nblocks; i; bpp++, i--) {
    877   1.1  perseant 		bp = *bpp;
    878   1.1  perseant #if 0
    879   1.2  perseant 		printf("i = %d, bp = %p, flags %lx, bn = %" PRIx64 "\n",
    880   1.1  perseant 		       nblocks - i, bp, bp->b_flags, bp->b_blkno);
    881   1.1  perseant 		printf("  vp = %p\n", bp->b_vp);
    882   1.8  perseant 		if (bp->b_vp != fs->lfs_devvp)
    883   1.1  perseant 			printf("  ino = %d lbn = %" PRId64 "\n",
    884   1.1  perseant 			       VTOI(bp->b_vp)->i_number, bp->b_lblkno);
    885   1.1  perseant #endif
    886   1.8  perseant 		if (bp->b_vp == fs->lfs_devvp)
    887   1.1  perseant 			written_dev += bp->b_bcount;
    888   1.1  perseant 		else {
    889   1.1  perseant 			if (bp->b_lblkno >= 0)
    890   1.1  perseant 				written_data += bp->b_bcount;
    891   1.1  perseant 			else
    892   1.1  perseant 				written_indir += bp->b_bcount;
    893   1.1  perseant 		}
    894   1.2  perseant 		bp->b_flags &= ~(B_DELWRI | B_READ | B_GATHERED | B_ERROR |
    895   1.2  perseant 				 B_LOCKED);
    896   1.1  perseant 		bwrite(bp);
    897   1.1  perseant 		written_bytes += bp->b_bcount;
    898   1.1  perseant 	}
    899   1.1  perseant 	written_inodes += ninos;
    900   1.1  perseant 
    901   1.1  perseant 	return (lfs_initseg(fs) || do_again);
    902   1.1  perseant }
    903   1.1  perseant 
    904   1.1  perseant /*
    905   1.1  perseant  * Our own copy of shellsort.  XXX use qsort or heapsort.
    906   1.1  perseant  */
    907  1.40  dholland static void
    908  1.40  dholland lfs_shellsort(struct lfs *fs,
    909  1.40  dholland 	      struct ubuf ** bp_array, union lfs_blocks *lb_array, int nmemb, int size)
    910   1.1  perseant {
    911   1.1  perseant 	static int __rsshell_increments[] = {4, 1, 0};
    912   1.1  perseant 	int incr, *incrp, t1, t2;
    913   1.1  perseant 	struct ubuf *bp_temp;
    914   1.1  perseant 
    915   1.1  perseant 	for (incrp = __rsshell_increments; (incr = *incrp++) != 0;)
    916   1.1  perseant 		for (t1 = incr; t1 < nmemb; ++t1)
    917   1.1  perseant 			for (t2 = t1 - incr; t2 >= 0;)
    918   1.1  perseant 				if ((u_int32_t) bp_array[t2]->b_lblkno >
    919   1.1  perseant 				    (u_int32_t) bp_array[t2 + incr]->b_lblkno) {
    920   1.1  perseant 					bp_temp = bp_array[t2];
    921   1.1  perseant 					bp_array[t2] = bp_array[t2 + incr];
    922   1.1  perseant 					bp_array[t2 + incr] = bp_temp;
    923   1.1  perseant 					t2 -= incr;
    924   1.1  perseant 				} else
    925   1.1  perseant 					break;
    926   1.1  perseant 
    927   1.1  perseant 	/* Reform the list of logical blocks */
    928   1.1  perseant 	incr = 0;
    929   1.1  perseant 	for (t1 = 0; t1 < nmemb; t1++) {
    930   1.1  perseant 		for (t2 = 0; t2 * size < bp_array[t1]->b_bcount; t2++) {
    931  1.40  dholland 			lfs_blocks_set(fs, lb_array, incr++,
    932  1.40  dholland 				       bp_array[t1]->b_lblkno + t2);
    933   1.1  perseant 		}
    934   1.1  perseant 	}
    935   1.1  perseant }
    936   1.1  perseant 
    937   1.1  perseant 
    938   1.1  perseant /*
    939   1.1  perseant  * lfs_seglock --
    940   1.1  perseant  *	Single thread the segment writer.
    941   1.1  perseant  */
    942   1.1  perseant int
    943   1.1  perseant lfs_seglock(struct lfs * fs, unsigned long flags)
    944   1.1  perseant {
    945   1.1  perseant 	struct segment *sp;
    946  1.32  dholland 	size_t allocsize;
    947   1.1  perseant 
    948   1.1  perseant 	if (fs->lfs_seglock) {
    949   1.1  perseant 		++fs->lfs_seglock;
    950   1.1  perseant 		fs->lfs_sp->seg_flags |= flags;
    951   1.1  perseant 		return 0;
    952   1.1  perseant 	}
    953   1.1  perseant 	fs->lfs_seglock = 1;
    954   1.1  perseant 
    955  1.15  christos 	sp = fs->lfs_sp = emalloc(sizeof(*sp));
    956  1.32  dholland 	allocsize = lfs_sb_getssize(fs) * sizeof(struct ubuf *);
    957  1.32  dholland 	sp->bpp = emalloc(allocsize);
    958   1.6      heas 	if (!sp->bpp)
    959  1.32  dholland 		err(!preen, "Could not allocate %zu bytes", allocsize);
    960   1.1  perseant 	sp->seg_flags = flags;
    961   1.1  perseant 	sp->vp = NULL;
    962   1.1  perseant 	sp->seg_iocount = 0;
    963   1.1  perseant 	(void) lfs_initseg(fs);
    964   1.1  perseant 
    965   1.1  perseant 	return 0;
    966   1.1  perseant }
    967   1.1  perseant 
    968   1.1  perseant /*
    969   1.1  perseant  * lfs_segunlock --
    970   1.1  perseant  *	Single thread the segment writer.
    971   1.1  perseant  */
    972   1.1  perseant void
    973   1.1  perseant lfs_segunlock(struct lfs * fs)
    974   1.1  perseant {
    975   1.1  perseant 	struct segment *sp;
    976   1.1  perseant 	struct ubuf *bp;
    977   1.1  perseant 
    978   1.1  perseant 	sp = fs->lfs_sp;
    979   1.1  perseant 
    980   1.1  perseant 	if (fs->lfs_seglock == 1) {
    981   1.1  perseant 		if (sp->bpp != sp->cbpp) {
    982   1.1  perseant 			/* Free allocated segment summary */
    983  1.32  dholland 			lfs_sb_suboffset(fs, lfs_btofsb(fs, lfs_sb_getsumsize(fs)));
    984   1.1  perseant 			bp = *sp->bpp;
    985   1.1  perseant 			bremfree(bp);
    986   1.1  perseant 			bp->b_flags |= B_DONE | B_INVAL;
    987   1.1  perseant 			bp->b_flags &= ~B_DELWRI;
    988   1.1  perseant 			reassignbuf(bp, bp->b_vp);
    989   1.1  perseant 			bp->b_flags |= B_BUSY; /* XXX */
    990  1.16        ad 			brelse(bp, 0);
    991   1.1  perseant 		} else
    992   1.1  perseant 			printf("unlock to 0 with no summary");
    993   1.1  perseant 
    994   1.1  perseant 		free(sp->bpp);
    995   1.1  perseant 		sp->bpp = NULL;
    996   1.1  perseant 		free(sp);
    997   1.1  perseant 		fs->lfs_sp = NULL;
    998   1.1  perseant 
    999   1.1  perseant 		fs->lfs_nactive = 0;
   1000   1.1  perseant 
   1001   1.1  perseant 		/* Since we *know* everything's on disk, write both sbs */
   1002  1.33  dholland 		lfs_writesuper(fs, lfs_sb_getsboff(fs, 0));
   1003  1.33  dholland 		lfs_writesuper(fs, lfs_sb_getsboff(fs, 1));
   1004   1.1  perseant 
   1005   1.1  perseant 		--fs->lfs_seglock;
   1006   1.1  perseant 		fs->lfs_lockpid = 0;
   1007   1.1  perseant 	} else if (fs->lfs_seglock == 0) {
   1008  1.30  christos 		errx(EXIT_FAILURE, "Seglock not held");
   1009   1.1  perseant 	} else {
   1010   1.1  perseant 		--fs->lfs_seglock;
   1011   1.1  perseant 	}
   1012   1.1  perseant }
   1013   1.1  perseant 
   1014   1.1  perseant int
   1015   1.1  perseant lfs_writevnodes(struct lfs *fs, struct segment *sp, int op)
   1016   1.1  perseant {
   1017   1.1  perseant 	struct inode *ip;
   1018   1.1  perseant 	struct uvnode *vp;
   1019   1.1  perseant 	int inodes_written = 0;
   1020   1.1  perseant 
   1021   1.1  perseant 	LIST_FOREACH(vp, &vnodelist, v_mntvnodes) {
   1022   1.1  perseant 		if (vp->v_bmap_op != lfs_vop_bmap)
   1023   1.1  perseant 			continue;
   1024   1.1  perseant 
   1025   1.1  perseant 		ip = VTOI(vp);
   1026   1.1  perseant 
   1027  1.17        ad 		if ((op == VN_DIROP && !(vp->v_uflag & VU_DIROP)) ||
   1028  1.17        ad 		    (op != VN_DIROP && (vp->v_uflag & VU_DIROP))) {
   1029   1.1  perseant 			continue;
   1030   1.1  perseant 		}
   1031   1.1  perseant 		/*
   1032   1.1  perseant 		 * Write the inode/file if dirty and it's not the IFILE.
   1033   1.1  perseant 		 */
   1034   1.1  perseant 		if (ip->i_flag & IN_ALLMOD || !LIST_EMPTY(&vp->v_dirtyblkhd)) {
   1035   1.1  perseant 			if (ip->i_number != LFS_IFILE_INUM)
   1036   1.1  perseant 				lfs_writefile(fs, sp, vp);
   1037   1.1  perseant 			(void) lfs_writeinode(fs, sp, ip);
   1038   1.1  perseant 			inodes_written++;
   1039   1.1  perseant 		}
   1040   1.1  perseant 	}
   1041   1.1  perseant 	return inodes_written;
   1042   1.1  perseant }
   1043   1.1  perseant 
   1044   1.1  perseant void
   1045  1.22  dholland lfs_writesuper(struct lfs *fs, ulfs_daddr_t daddr)
   1046   1.1  perseant {
   1047   1.1  perseant 	struct ubuf *bp;
   1048   1.1  perseant 
   1049   1.1  perseant 	/* Set timestamp of this version of the superblock */
   1050  1.35  dholland 	if (lfs_sb_getversion(fs) == 1)
   1051  1.32  dholland 		lfs_sb_setotstamp(fs, write_time);
   1052  1.32  dholland 	lfs_sb_settstamp(fs, write_time);
   1053   1.1  perseant 
   1054  1.36  dholland 	__CTASSERT(sizeof(struct dlfs) == sizeof(struct dlfs64));
   1055  1.36  dholland 
   1056   1.1  perseant 	/* Checksum the superblock and copy it into a buffer. */
   1057  1.36  dholland 	lfs_sb_setcksum(fs, lfs_sb_cksum(fs));
   1058   1.1  perseant 	assert(daddr > 0);
   1059  1.26  christos 	bp = getblk(fs->lfs_devvp, LFS_FSBTODB(fs, daddr), LFS_SBPAD);
   1060  1.36  dholland 	memcpy(bp->b_data, &fs->lfs_dlfs_u, sizeof(struct dlfs));
   1061   1.1  perseant 	memset(bp->b_data + sizeof(struct dlfs), 0,
   1062   1.1  perseant 	    LFS_SBPAD - sizeof(struct dlfs));
   1063   1.1  perseant 
   1064   1.1  perseant 	bwrite(bp);
   1065   1.1  perseant }
   1066