Home | History | Annotate | Line # | Download | only in lfs
lfs_subr.c revision 1.40.2.6
      1  1.40.2.6     skrll /*	$NetBSD: lfs_subr.c,v 1.40.2.6 2005/11/10 14:12:32 skrll Exp $	*/
      2       1.2       cgd 
      3       1.8  perseant /*-
      4      1.31  perseant  * Copyright (c) 1999, 2000, 2001, 2002, 2003 The NetBSD Foundation, Inc.
      5       1.8  perseant  * All rights reserved.
      6       1.8  perseant  *
      7       1.8  perseant  * This code is derived from software contributed to The NetBSD Foundation
      8       1.8  perseant  * by Konrad E. Schroder <perseant (at) hhhh.org>.
      9       1.8  perseant  *
     10       1.8  perseant  * Redistribution and use in source and binary forms, with or without
     11       1.8  perseant  * modification, are permitted provided that the following conditions
     12       1.8  perseant  * are met:
     13       1.8  perseant  * 1. Redistributions of source code must retain the above copyright
     14       1.8  perseant  *    notice, this list of conditions and the following disclaimer.
     15       1.8  perseant  * 2. Redistributions in binary form must reproduce the above copyright
     16       1.8  perseant  *    notice, this list of conditions and the following disclaimer in the
     17       1.8  perseant  *    documentation and/or other materials provided with the distribution.
     18       1.8  perseant  * 3. All advertising materials mentioning features or use of this software
     19       1.8  perseant  *    must display the following acknowledgement:
     20      1.33  perseant  *	This product includes software developed by the NetBSD
     21      1.33  perseant  *	Foundation, Inc. and its contributors.
     22       1.8  perseant  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23       1.8  perseant  *    contributors may be used to endorse or promote products derived
     24       1.8  perseant  *    from this software without specific prior written permission.
     25       1.8  perseant  *
     26       1.8  perseant  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27       1.8  perseant  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28       1.8  perseant  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29       1.8  perseant  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30       1.8  perseant  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31       1.8  perseant  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32       1.8  perseant  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33       1.8  perseant  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34       1.8  perseant  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35       1.8  perseant  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36       1.8  perseant  * POSSIBILITY OF SUCH DAMAGE.
     37       1.8  perseant  */
     38       1.1   mycroft /*
     39       1.1   mycroft  * Copyright (c) 1991, 1993
     40       1.1   mycroft  *	The Regents of the University of California.  All rights reserved.
     41       1.1   mycroft  *
     42       1.1   mycroft  * Redistribution and use in source and binary forms, with or without
     43       1.1   mycroft  * modification, are permitted provided that the following conditions
     44       1.1   mycroft  * are met:
     45       1.1   mycroft  * 1. Redistributions of source code must retain the above copyright
     46       1.1   mycroft  *    notice, this list of conditions and the following disclaimer.
     47       1.1   mycroft  * 2. Redistributions in binary form must reproduce the above copyright
     48       1.1   mycroft  *    notice, this list of conditions and the following disclaimer in the
     49       1.1   mycroft  *    documentation and/or other materials provided with the distribution.
     50  1.40.2.1     skrll  * 3. Neither the name of the University nor the names of its contributors
     51       1.1   mycroft  *    may be used to endorse or promote products derived from this software
     52       1.1   mycroft  *    without specific prior written permission.
     53       1.1   mycroft  *
     54       1.1   mycroft  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     55       1.1   mycroft  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     56       1.1   mycroft  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     57       1.1   mycroft  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     58       1.1   mycroft  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     59       1.1   mycroft  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     60       1.1   mycroft  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     61       1.1   mycroft  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     62       1.1   mycroft  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     63       1.1   mycroft  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     64       1.1   mycroft  * SUCH DAMAGE.
     65       1.1   mycroft  *
     66       1.6      fvdl  *	@(#)lfs_subr.c	8.4 (Berkeley) 5/8/95
     67       1.1   mycroft  */
     68      1.20     lukem 
     69      1.20     lukem #include <sys/cdefs.h>
     70  1.40.2.6     skrll __KERNEL_RCSID(0, "$NetBSD: lfs_subr.c,v 1.40.2.6 2005/11/10 14:12:32 skrll Exp $");
     71       1.1   mycroft 
     72       1.1   mycroft #include <sys/param.h>
     73       1.3  christos #include <sys/systm.h>
     74       1.1   mycroft #include <sys/namei.h>
     75       1.1   mycroft #include <sys/vnode.h>
     76       1.1   mycroft #include <sys/buf.h>
     77       1.1   mycroft #include <sys/mount.h>
     78       1.1   mycroft #include <sys/malloc.h>
     79       1.1   mycroft #include <sys/proc.h>
     80       1.1   mycroft 
     81       1.1   mycroft #include <ufs/ufs/inode.h>
     82       1.1   mycroft #include <ufs/lfs/lfs.h>
     83       1.1   mycroft #include <ufs/lfs/lfs_extern.h>
     84       1.1   mycroft 
     85      1.31  perseant #include <uvm/uvm.h>
     86      1.31  perseant 
     87       1.1   mycroft /*
     88       1.1   mycroft  * Return buffer with the contents of block "offset" from the beginning of
     89       1.1   mycroft  * directory "ip".  If "res" is non-zero, fill it in with a pointer to the
     90       1.1   mycroft  * remaining space in the directory.
     91       1.1   mycroft  */
     92       1.1   mycroft int
     93  1.40.2.6     skrll lfs_blkatoff(struct vnode *vp, off_t offset, char **res, struct buf **bpp)
     94       1.3  christos {
     95      1.13  augustss 	struct lfs *fs;
     96       1.1   mycroft 	struct inode *ip;
     97       1.1   mycroft 	struct buf *bp;
     98      1.29      fvdl 	daddr_t lbn;
     99       1.1   mycroft 	int bsize, error;
    100  1.40.2.4     skrll 
    101  1.40.2.6     skrll 	ip = VTOI(vp);
    102       1.1   mycroft 	fs = ip->i_lfs;
    103  1.40.2.6     skrll 	lbn = lblkno(fs, offset);
    104       1.6      fvdl 	bsize = blksize(fs, ip, lbn);
    105  1.40.2.4     skrll 
    106  1.40.2.6     skrll 	*bpp = NULL;
    107  1.40.2.6     skrll 	if ((error = bread(vp, lbn, bsize, NOCRED, &bp)) != 0) {
    108       1.1   mycroft 		brelse(bp);
    109       1.1   mycroft 		return (error);
    110       1.1   mycroft 	}
    111  1.40.2.6     skrll 	if (res)
    112  1.40.2.6     skrll 		*res = (char *)bp->b_data + blkoff(fs, offset);
    113  1.40.2.6     skrll 	*bpp = bp;
    114       1.1   mycroft 	return (0);
    115       1.1   mycroft }
    116       1.1   mycroft 
    117  1.40.2.5     skrll #ifdef DEBUG
    118  1.40.2.6     skrll const char *lfs_res_names[LFS_NB_COUNT] = {
    119      1.31  perseant 	"summary",
    120      1.31  perseant 	"superblock",
    121  1.40.2.6     skrll 	"file block",
    122      1.31  perseant 	"cluster",
    123      1.31  perseant 	"clean",
    124  1.40.2.6     skrll 	"blkiov",
    125      1.31  perseant };
    126      1.31  perseant #endif
    127      1.31  perseant 
    128      1.31  perseant int lfs_res_qty[LFS_NB_COUNT] = {
    129      1.31  perseant 	LFS_N_SUMMARIES,
    130      1.31  perseant 	LFS_N_SBLOCKS,
    131      1.31  perseant 	LFS_N_IBLOCKS,
    132      1.31  perseant 	LFS_N_CLUSTERS,
    133      1.31  perseant 	LFS_N_CLEAN,
    134  1.40.2.6     skrll 	LFS_N_BLKIOV,
    135      1.31  perseant };
    136      1.31  perseant 
    137      1.31  perseant void
    138      1.31  perseant lfs_setup_resblks(struct lfs *fs)
    139      1.31  perseant {
    140      1.31  perseant 	int i, j;
    141      1.31  perseant 	int maxbpp;
    142      1.31  perseant 
    143  1.40.2.6     skrll 	ASSERT_NO_SEGLOCK(fs);
    144      1.31  perseant 	fs->lfs_resblk = (res_t *)malloc(LFS_N_TOTAL * sizeof(res_t), M_SEGMENT,
    145      1.33  perseant 					  M_WAITOK);
    146      1.31  perseant 	for (i = 0; i < LFS_N_TOTAL; i++) {
    147      1.31  perseant 		fs->lfs_resblk[i].inuse = 0;
    148      1.31  perseant 		fs->lfs_resblk[i].p = NULL;
    149      1.31  perseant 	}
    150      1.31  perseant 	for (i = 0; i < LFS_RESHASH_WIDTH; i++)
    151      1.31  perseant 		LIST_INIT(fs->lfs_reshash + i);
    152      1.31  perseant 
    153      1.31  perseant 	/*
    154      1.31  perseant 	 * These types of allocations can be larger than a page,
    155      1.31  perseant 	 * so we can't use the pool subsystem for them.
    156      1.31  perseant 	 */
    157      1.31  perseant 	for (i = 0, j = 0; j < LFS_N_SUMMARIES; j++, i++)
    158      1.34  perseant 		fs->lfs_resblk[i].size = fs->lfs_sumsize;
    159      1.31  perseant 	for (j = 0; j < LFS_N_SBLOCKS; j++, i++)
    160      1.34  perseant 		fs->lfs_resblk[i].size = LFS_SBPAD;
    161      1.31  perseant 	for (j = 0; j < LFS_N_IBLOCKS; j++, i++)
    162      1.34  perseant 		fs->lfs_resblk[i].size = fs->lfs_bsize;
    163      1.31  perseant 	for (j = 0; j < LFS_N_CLUSTERS; j++, i++)
    164      1.34  perseant 		fs->lfs_resblk[i].size = MAXPHYS;
    165      1.31  perseant 	for (j = 0; j < LFS_N_CLEAN; j++, i++)
    166      1.34  perseant 		fs->lfs_resblk[i].size = MAXPHYS;
    167  1.40.2.6     skrll 	for (j = 0; j < LFS_N_BLKIOV; j++, i++)
    168  1.40.2.6     skrll 		fs->lfs_resblk[i].size = LFS_MARKV_MAXBLKCNT * sizeof(BLOCK_INFO);
    169      1.34  perseant 
    170      1.34  perseant 	for (i = 0; i < LFS_N_TOTAL; i++) {
    171      1.34  perseant 		fs->lfs_resblk[i].p = malloc(fs->lfs_resblk[i].size,
    172      1.34  perseant 					     M_SEGMENT, M_WAITOK);
    173      1.34  perseant 	}
    174      1.31  perseant 
    175      1.31  perseant 	/*
    176      1.31  perseant 	 * Initialize pools for small types (XXX is BPP small?)
    177      1.31  perseant 	 */
    178  1.40.2.1     skrll 	pool_init(&fs->lfs_clpool, sizeof(struct lfs_cluster), 0, 0, 0,
    179  1.40.2.1     skrll 		"lfsclpl", &pool_allocator_nointr);
    180  1.40.2.1     skrll 	pool_init(&fs->lfs_segpool, sizeof(struct segment), 0, 0, 0,
    181  1.40.2.1     skrll 		"lfssegpool", &pool_allocator_nointr);
    182      1.31  perseant 	maxbpp = ((fs->lfs_sumsize - SEGSUM_SIZE(fs)) / sizeof(int32_t) + 2);
    183  1.40.2.1     skrll 	maxbpp = MIN(maxbpp, segsize(fs) / fs->lfs_fsize + 2);
    184  1.40.2.1     skrll 	pool_init(&fs->lfs_bpppool, maxbpp * sizeof(struct buf *), 0, 0, 0,
    185  1.40.2.1     skrll 		"lfsbpppl", &pool_allocator_nointr);
    186      1.31  perseant }
    187      1.31  perseant 
    188      1.31  perseant void
    189      1.31  perseant lfs_free_resblks(struct lfs *fs)
    190      1.31  perseant {
    191      1.31  perseant 	int i;
    192      1.31  perseant 
    193      1.31  perseant 	pool_destroy(&fs->lfs_bpppool);
    194      1.31  perseant 	pool_destroy(&fs->lfs_segpool);
    195      1.31  perseant 	pool_destroy(&fs->lfs_clpool);
    196      1.31  perseant 
    197  1.40.2.6     skrll 	simple_lock(&fs->lfs_interlock);
    198      1.31  perseant 	for (i = 0; i < LFS_N_TOTAL; i++) {
    199      1.39  perseant 		while (fs->lfs_resblk[i].inuse)
    200  1.40.2.6     skrll 			ltsleep(&fs->lfs_resblk, PRIBIO + 1, "lfs_free", 0,
    201  1.40.2.6     skrll 				&fs->lfs_interlock);
    202      1.31  perseant 		if (fs->lfs_resblk[i].p != NULL)
    203      1.31  perseant 			free(fs->lfs_resblk[i].p, M_SEGMENT);
    204      1.31  perseant 	}
    205      1.31  perseant 	free(fs->lfs_resblk, M_SEGMENT);
    206  1.40.2.6     skrll 	simple_unlock(&fs->lfs_interlock);
    207      1.31  perseant }
    208      1.31  perseant 
    209      1.31  perseant static unsigned int
    210      1.31  perseant lfs_mhash(void *vp)
    211      1.31  perseant {
    212      1.31  perseant 	return (unsigned int)(((unsigned long)vp) >> 2) % LFS_RESHASH_WIDTH;
    213      1.31  perseant }
    214      1.31  perseant 
    215      1.31  perseant /*
    216      1.31  perseant  * Return memory of the given size for the given purpose, or use one of a
    217      1.31  perseant  * number of spare last-resort buffers, if malloc returns NULL.
    218  1.40.2.4     skrll  */
    219      1.31  perseant void *
    220      1.31  perseant lfs_malloc(struct lfs *fs, size_t size, int type)
    221      1.31  perseant {
    222      1.31  perseant 	struct lfs_res_blk *re;
    223      1.31  perseant 	void *r;
    224      1.31  perseant 	int i, s, start;
    225      1.31  perseant 	unsigned int h;
    226      1.31  perseant 
    227  1.40.2.6     skrll 	ASSERT_MAYBE_SEGLOCK(fs);
    228      1.34  perseant 	r = NULL;
    229      1.34  perseant 
    230      1.31  perseant 	/* If no mem allocated for this type, it just waits */
    231      1.34  perseant 	if (lfs_res_qty[type] == 0) {
    232      1.34  perseant 		r = malloc(size, M_SEGMENT, M_WAITOK);
    233      1.34  perseant 		return r;
    234      1.34  perseant 	}
    235      1.31  perseant 
    236      1.31  perseant 	/* Otherwise try a quick malloc, and if it works, great */
    237      1.34  perseant 	if ((r = malloc(size, M_SEGMENT, M_NOWAIT)) != NULL) {
    238      1.31  perseant 		return r;
    239      1.34  perseant 	}
    240      1.31  perseant 
    241      1.31  perseant 	/*
    242      1.31  perseant 	 * If malloc returned NULL, we are forced to use one of our
    243      1.31  perseant 	 * reserve blocks.  We have on hand at least one summary block,
    244      1.31  perseant 	 * at least one cluster block, at least one superblock,
    245      1.31  perseant 	 * and several indirect blocks.
    246      1.31  perseant 	 */
    247  1.40.2.6     skrll 
    248  1.40.2.6     skrll 	simple_lock(&fs->lfs_interlock);
    249      1.31  perseant 	/* skip over blocks of other types */
    250      1.31  perseant 	for (i = 0, start = 0; i < type; i++)
    251      1.31  perseant 		start += lfs_res_qty[i];
    252      1.31  perseant 	while (r == NULL) {
    253      1.31  perseant 		for (i = 0; i < lfs_res_qty[type]; i++) {
    254      1.31  perseant 			if (fs->lfs_resblk[start + i].inuse == 0) {
    255      1.31  perseant 				re = fs->lfs_resblk + start + i;
    256      1.31  perseant 				re->inuse = 1;
    257      1.31  perseant 				r = re->p;
    258      1.34  perseant 				KASSERT(re->size >= size);
    259      1.31  perseant 				h = lfs_mhash(r);
    260      1.31  perseant 				s = splbio();
    261      1.31  perseant 				LIST_INSERT_HEAD(&fs->lfs_reshash[h], re, res);
    262      1.31  perseant 				splx(s);
    263  1.40.2.6     skrll 				simple_unlock(&fs->lfs_interlock);
    264      1.31  perseant 				return r;
    265      1.31  perseant 			}
    266      1.31  perseant 		}
    267  1.40.2.6     skrll 		DLOG((DLOG_MALLOC, "sleeping on %s (%d)\n",
    268  1.40.2.6     skrll 		      lfs_res_names[type], lfs_res_qty[type]));
    269  1.40.2.6     skrll 		ltsleep(&fs->lfs_resblk, PVM, "lfs_malloc", 0,
    270  1.40.2.6     skrll 			&fs->lfs_interlock);
    271  1.40.2.6     skrll 		DLOG((DLOG_MALLOC, "done sleeping on %s\n",
    272  1.40.2.6     skrll 		      lfs_res_names[type]));
    273      1.31  perseant 	}
    274      1.31  perseant 	/* NOTREACHED */
    275  1.40.2.6     skrll 	simple_unlock(&fs->lfs_interlock);
    276      1.31  perseant 	return r;
    277      1.31  perseant }
    278      1.31  perseant 
    279      1.31  perseant void
    280      1.31  perseant lfs_free(struct lfs *fs, void *p, int type)
    281      1.31  perseant {
    282      1.31  perseant 	int s;
    283      1.31  perseant 	unsigned int h;
    284      1.31  perseant 	res_t *re;
    285      1.32      yamt #ifdef DEBUG
    286      1.32      yamt 	int i;
    287      1.32      yamt #endif
    288      1.31  perseant 
    289  1.40.2.6     skrll 	ASSERT_MAYBE_SEGLOCK(fs);
    290      1.31  perseant 	h = lfs_mhash(p);
    291  1.40.2.6     skrll 	simple_lock(&fs->lfs_interlock);
    292      1.31  perseant 	s = splbio();
    293      1.31  perseant 	LIST_FOREACH(re, &fs->lfs_reshash[h], res) {
    294      1.31  perseant 		if (re->p == p) {
    295      1.32      yamt 			KASSERT(re->inuse == 1);
    296      1.31  perseant 			LIST_REMOVE(re, res);
    297      1.31  perseant 			re->inuse = 0;
    298      1.31  perseant 			wakeup(&fs->lfs_resblk);
    299      1.31  perseant 			splx(s);
    300  1.40.2.6     skrll 			simple_unlock(&fs->lfs_interlock);
    301      1.31  perseant 			return;
    302      1.31  perseant 		}
    303      1.31  perseant 	}
    304      1.32      yamt #ifdef DEBUG
    305      1.32      yamt 	for (i = 0; i < LFS_N_TOTAL; i++) {
    306      1.32      yamt 		if (fs->lfs_resblk[i].p == p)
    307      1.34  perseant 			panic("lfs_free: inconsistent reserved block");
    308      1.32      yamt 	}
    309      1.32      yamt #endif
    310      1.31  perseant 	splx(s);
    311  1.40.2.6     skrll 	simple_unlock(&fs->lfs_interlock);
    312  1.40.2.6     skrll 
    313      1.31  perseant 	/*
    314      1.31  perseant 	 * If we didn't find it, free it.
    315      1.31  perseant 	 */
    316      1.31  perseant 	free(p, M_SEGMENT);
    317      1.31  perseant }
    318       1.1   mycroft 
    319       1.1   mycroft /*
    320       1.1   mycroft  * lfs_seglock --
    321       1.1   mycroft  *	Single thread the segment writer.
    322       1.1   mycroft  */
    323      1.31  perseant int
    324      1.18  perseant lfs_seglock(struct lfs *fs, unsigned long flags)
    325       1.1   mycroft {
    326       1.1   mycroft 	struct segment *sp;
    327  1.40.2.4     skrll 
    328      1.38  perseant 	simple_lock(&fs->lfs_interlock);
    329       1.7   thorpej 	if (fs->lfs_seglock) {
    330       1.1   mycroft 		if (fs->lfs_lockpid == curproc->p_pid) {
    331      1.38  perseant 			simple_unlock(&fs->lfs_interlock);
    332       1.1   mycroft 			++fs->lfs_seglock;
    333       1.1   mycroft 			fs->lfs_sp->seg_flags |= flags;
    334      1.31  perseant 			return 0;
    335      1.38  perseant 		} else if (flags & SEGM_PAGEDAEMON) {
    336      1.38  perseant 			simple_unlock(&fs->lfs_interlock);
    337      1.31  perseant 			return EWOULDBLOCK;
    338  1.40.2.6     skrll 		} else {
    339  1.40.2.6     skrll 			while (fs->lfs_seglock) {
    340  1.40.2.6     skrll 				(void)ltsleep(&fs->lfs_seglock, PRIBIO + 1,
    341  1.40.2.6     skrll 					"lfs seglock", 0, &fs->lfs_interlock);
    342  1.40.2.6     skrll 			}
    343  1.40.2.6     skrll 		}
    344       1.7   thorpej 	}
    345  1.40.2.4     skrll 
    346       1.1   mycroft 	fs->lfs_seglock = 1;
    347       1.1   mycroft 	fs->lfs_lockpid = curproc->p_pid;
    348      1.38  perseant 	simple_unlock(&fs->lfs_interlock);
    349      1.36  perseant 	fs->lfs_cleanind = 0;
    350      1.36  perseant 
    351  1.40.2.6     skrll #ifdef DEBUG
    352  1.40.2.6     skrll 	LFS_ENTER_LOG("seglock", __FILE__, __LINE__, 0, flags, curproc->p_pid);
    353  1.40.2.6     skrll #endif
    354      1.27  perseant 	/* Drain fragment size changes out */
    355      1.27  perseant 	lockmgr(&fs->lfs_fraglock, LK_EXCLUSIVE, 0);
    356      1.27  perseant 
    357      1.31  perseant 	sp = fs->lfs_sp = pool_get(&fs->lfs_segpool, PR_WAITOK);
    358      1.31  perseant 	sp->bpp = pool_get(&fs->lfs_bpppool, PR_WAITOK);
    359       1.1   mycroft 	sp->seg_flags = flags;
    360       1.1   mycroft 	sp->vp = NULL;
    361      1.26  perseant 	sp->seg_iocount = 0;
    362       1.1   mycroft 	(void) lfs_initseg(fs);
    363  1.40.2.4     skrll 
    364       1.1   mycroft 	/*
    365       1.1   mycroft 	 * Keep a cumulative count of the outstanding I/O operations.  If the
    366       1.1   mycroft 	 * disk drive catches up with us it could go to zero before we finish,
    367       1.1   mycroft 	 * so we artificially increment it by one until we've scheduled all of
    368       1.1   mycroft 	 * the writes we intend to do.
    369       1.1   mycroft 	 */
    370  1.40.2.6     skrll 	simple_lock(&fs->lfs_interlock);
    371       1.1   mycroft 	++fs->lfs_iocount;
    372  1.40.2.6     skrll 	simple_unlock(&fs->lfs_interlock);
    373      1.31  perseant 	return 0;
    374       1.1   mycroft }
    375       1.8  perseant 
    376      1.31  perseant static void lfs_unmark_dirop(struct lfs *);
    377      1.31  perseant 
    378      1.31  perseant static void
    379      1.31  perseant lfs_unmark_dirop(struct lfs *fs)
    380      1.31  perseant {
    381      1.31  perseant 	struct inode *ip, *nip;
    382      1.31  perseant 	struct vnode *vp;
    383      1.40  perseant 	int doit;
    384      1.31  perseant 
    385  1.40.2.6     skrll 	ASSERT_NO_SEGLOCK(fs);
    386      1.40  perseant 	simple_lock(&fs->lfs_interlock);
    387      1.40  perseant 	doit = !(fs->lfs_flags & LFS_UNDIROP);
    388      1.40  perseant 	if (doit)
    389      1.40  perseant 		fs->lfs_flags |= LFS_UNDIROP;
    390  1.40.2.6     skrll 	if (!doit) {
    391  1.40.2.6     skrll 		simple_unlock(&fs->lfs_interlock);
    392      1.40  perseant 		return;
    393  1.40.2.6     skrll 	}
    394      1.40  perseant 
    395      1.31  perseant 	for (ip = TAILQ_FIRST(&fs->lfs_dchainhd); ip != NULL; ip = nip) {
    396      1.31  perseant 		nip = TAILQ_NEXT(ip, i_lfs_dchain);
    397  1.40.2.6     skrll 		simple_unlock(&fs->lfs_interlock);
    398      1.31  perseant 		vp = ITOV(ip);
    399      1.31  perseant 
    400  1.40.2.6     skrll 		simple_lock(&vp->v_interlock);
    401      1.31  perseant 		if (VOP_ISLOCKED(vp) &&
    402      1.33  perseant 			   vp->v_lock.lk_lockholder != curproc->p_pid) {
    403  1.40.2.6     skrll 			simple_lock(&fs->lfs_interlock);
    404  1.40.2.6     skrll 			simple_unlock(&vp->v_interlock);
    405      1.31  perseant 			continue;
    406      1.31  perseant 		}
    407      1.31  perseant 		if ((VTOI(vp)->i_flag & IN_ADIROP) == 0) {
    408  1.40.2.6     skrll 			simple_lock(&fs->lfs_interlock);
    409  1.40.2.6     skrll 			simple_lock(&lfs_subsys_lock);
    410      1.31  perseant 			--lfs_dirvcount;
    411  1.40.2.6     skrll 			simple_unlock(&lfs_subsys_lock);
    412      1.31  perseant 			vp->v_flag &= ~VDIROP;
    413      1.31  perseant 			TAILQ_REMOVE(&fs->lfs_dchainhd, ip, i_lfs_dchain);
    414  1.40.2.6     skrll 			simple_unlock(&fs->lfs_interlock);
    415      1.31  perseant 			wakeup(&lfs_dirvcount);
    416  1.40.2.6     skrll 			simple_unlock(&vp->v_interlock);
    417  1.40.2.6     skrll 			simple_lock(&fs->lfs_interlock);
    418      1.31  perseant 			fs->lfs_unlockvp = vp;
    419  1.40.2.6     skrll 			simple_unlock(&fs->lfs_interlock);
    420      1.31  perseant 			vrele(vp);
    421  1.40.2.6     skrll 			simple_lock(&fs->lfs_interlock);
    422      1.31  perseant 			fs->lfs_unlockvp = NULL;
    423  1.40.2.6     skrll 			simple_unlock(&fs->lfs_interlock);
    424  1.40.2.6     skrll 		} else
    425  1.40.2.6     skrll 			simple_unlock(&vp->v_interlock);
    426  1.40.2.6     skrll 		simple_lock(&fs->lfs_interlock);
    427      1.31  perseant 	}
    428      1.40  perseant 
    429      1.40  perseant 	fs->lfs_flags &= ~LFS_UNDIROP;
    430      1.40  perseant 	simple_unlock(&fs->lfs_interlock);
    431  1.40.2.6     skrll 	wakeup(&fs->lfs_flags);
    432      1.31  perseant }
    433      1.31  perseant 
    434      1.31  perseant static void
    435      1.31  perseant lfs_auto_segclean(struct lfs *fs)
    436      1.31  perseant {
    437  1.40.2.4     skrll 	int i, error, s, waited;
    438      1.31  perseant 
    439  1.40.2.6     skrll 	ASSERT_SEGLOCK(fs);
    440      1.31  perseant 	/*
    441      1.31  perseant 	 * Now that we've swapped lfs_activesb, but while we still
    442      1.31  perseant 	 * hold the segment lock, run through the segment list marking
    443      1.31  perseant 	 * the empty ones clean.
    444      1.31  perseant 	 * XXX - do we really need to do them all at once?
    445      1.31  perseant 	 */
    446  1.40.2.4     skrll 	waited = 0;
    447      1.31  perseant 	for (i = 0; i < fs->lfs_nseg; i++) {
    448      1.31  perseant 		if ((fs->lfs_suflags[0][i] &
    449      1.31  perseant 		     (SEGUSE_ACTIVE | SEGUSE_DIRTY | SEGUSE_EMPTY)) ==
    450      1.31  perseant 		    (SEGUSE_DIRTY | SEGUSE_EMPTY) &&
    451      1.31  perseant 		    (fs->lfs_suflags[1][i] &
    452      1.31  perseant 		     (SEGUSE_ACTIVE | SEGUSE_DIRTY | SEGUSE_EMPTY)) ==
    453      1.31  perseant 		    (SEGUSE_DIRTY | SEGUSE_EMPTY)) {
    454      1.31  perseant 
    455  1.40.2.4     skrll 			/* Make sure the sb is written before we clean */
    456  1.40.2.6     skrll 			simple_lock(&fs->lfs_interlock);
    457  1.40.2.4     skrll 			s = splbio();
    458  1.40.2.4     skrll 			while (waited == 0 && fs->lfs_sbactive)
    459  1.40.2.6     skrll 				ltsleep(&fs->lfs_sbactive, PRIBIO+1, "lfs asb",
    460  1.40.2.6     skrll 					0, &fs->lfs_interlock);
    461  1.40.2.4     skrll 			splx(s);
    462  1.40.2.6     skrll 			simple_unlock(&fs->lfs_interlock);
    463  1.40.2.4     skrll 			waited = 1;
    464  1.40.2.4     skrll 
    465      1.31  perseant 			if ((error = lfs_do_segclean(fs, i)) != 0) {
    466  1.40.2.5     skrll 				DLOG((DLOG_CLEAN, "lfs_auto_segclean: lfs_do_segclean returned %d for seg %d\n", error, i));
    467      1.31  perseant 			}
    468      1.31  perseant 		}
    469      1.31  perseant 		fs->lfs_suflags[1 - fs->lfs_activesb][i] =
    470      1.31  perseant 			fs->lfs_suflags[fs->lfs_activesb][i];
    471      1.31  perseant 	}
    472      1.31  perseant }
    473      1.31  perseant 
    474       1.1   mycroft /*
    475       1.1   mycroft  * lfs_segunlock --
    476       1.1   mycroft  *	Single thread the segment writer.
    477       1.1   mycroft  */
    478       1.1   mycroft void
    479      1.18  perseant lfs_segunlock(struct lfs *fs)
    480       1.1   mycroft {
    481       1.1   mycroft 	struct segment *sp;
    482       1.1   mycroft 	unsigned long sync, ckp;
    483      1.22  perseant 	struct buf *bp;
    484      1.40  perseant 	int do_unmark_dirop = 0;
    485  1.40.2.4     skrll 
    486      1.15  perseant 	sp = fs->lfs_sp;
    487      1.15  perseant 
    488      1.38  perseant 	simple_lock(&fs->lfs_interlock);
    489  1.40.2.6     skrll 	LOCK_ASSERT(LFS_SEGLOCK_HELD(fs));
    490      1.15  perseant 	if (fs->lfs_seglock == 1) {
    491      1.40  perseant 		if ((sp->seg_flags & SEGM_PROT) == 0)
    492      1.40  perseant 			do_unmark_dirop = 1;
    493      1.38  perseant 		simple_unlock(&fs->lfs_interlock);
    494       1.1   mycroft 		sync = sp->seg_flags & SEGM_SYNC;
    495       1.1   mycroft 		ckp = sp->seg_flags & SEGM_CKP;
    496       1.1   mycroft 		if (sp->bpp != sp->cbpp) {
    497       1.1   mycroft 			/* Free allocated segment summary */
    498      1.18  perseant 			fs->lfs_offset -= btofsb(fs, fs->lfs_sumsize);
    499      1.22  perseant 			bp = *sp->bpp;
    500      1.31  perseant 			lfs_freebuf(fs, bp);
    501       1.1   mycroft 		} else
    502  1.40.2.5     skrll 			DLOG((DLOG_SEG, "lfs_segunlock: unlock to 0 with no summary"));
    503       1.8  perseant 
    504      1.31  perseant 		pool_put(&fs->lfs_bpppool, sp->bpp);
    505      1.18  perseant 		sp->bpp = NULL;
    506      1.36  perseant 
    507      1.36  perseant 		/*
    508      1.36  perseant 		 * If we're not sync, we're done with sp, get rid of it.
    509      1.36  perseant 		 * Otherwise, we keep a local copy around but free
    510      1.36  perseant 		 * fs->lfs_sp so another process can use it (we have to
    511      1.36  perseant 		 * wait but they don't have to wait for us).
    512      1.36  perseant 		 */
    513      1.26  perseant 		if (!sync)
    514      1.31  perseant 			pool_put(&fs->lfs_segpool, sp);
    515      1.18  perseant 		fs->lfs_sp = NULL;
    516       1.1   mycroft 
    517       1.1   mycroft 		/*
    518       1.1   mycroft 		 * If the I/O count is non-zero, sleep until it reaches zero.
    519       1.1   mycroft 		 * At the moment, the user's process hangs around so we can
    520       1.1   mycroft 		 * sleep.
    521       1.1   mycroft 		 */
    522  1.40.2.6     skrll 		simple_lock(&fs->lfs_interlock);
    523  1.40.2.1     skrll 		if (--fs->lfs_iocount == 0)
    524  1.40.2.1     skrll 			LFS_DEBUG_COUNTLOCKED("lfs_segunlock");
    525      1.36  perseant 		if (fs->lfs_iocount <= 1)
    526      1.22  perseant 			wakeup(&fs->lfs_iocount);
    527  1.40.2.6     skrll 		simple_unlock(&fs->lfs_interlock);
    528       1.1   mycroft 		/*
    529      1.26  perseant 		 * If we're not checkpointing, we don't have to block
    530      1.26  perseant 		 * other processes to wait for a synchronous write
    531      1.26  perseant 		 * to complete.
    532      1.26  perseant 		 */
    533      1.26  perseant 		if (!ckp) {
    534  1.40.2.6     skrll #ifdef DEBUG
    535  1.40.2.6     skrll 			LFS_ENTER_LOG("segunlock_std", __FILE__, __LINE__, 0, 0, curproc->p_pid);
    536  1.40.2.6     skrll #endif
    537      1.38  perseant 			simple_lock(&fs->lfs_interlock);
    538      1.26  perseant 			--fs->lfs_seglock;
    539      1.26  perseant 			fs->lfs_lockpid = 0;
    540      1.38  perseant 			simple_unlock(&fs->lfs_interlock);
    541      1.26  perseant 			wakeup(&fs->lfs_seglock);
    542      1.26  perseant 		}
    543      1.26  perseant 		/*
    544       1.1   mycroft 		 * We let checkpoints happen asynchronously.  That means
    545       1.1   mycroft 		 * that during recovery, we have to roll forward between
    546       1.1   mycroft 		 * the two segments described by the first and second
    547       1.1   mycroft 		 * superblocks to make sure that the checkpoint described
    548       1.1   mycroft 		 * by a superblock completed.
    549       1.1   mycroft 		 */
    550  1.40.2.6     skrll 		simple_lock(&fs->lfs_interlock);
    551      1.26  perseant 		while (ckp && sync && fs->lfs_iocount)
    552  1.40.2.6     skrll 			(void)ltsleep(&fs->lfs_iocount, PRIBIO + 1,
    553  1.40.2.6     skrll 				      "lfs_iocount", 0, &fs->lfs_interlock);
    554      1.26  perseant 		while (sync && sp->seg_iocount) {
    555  1.40.2.6     skrll 			(void)ltsleep(&sp->seg_iocount, PRIBIO + 1,
    556  1.40.2.6     skrll 				     "seg_iocount", 0, &fs->lfs_interlock);
    557  1.40.2.5     skrll 			DLOG((DLOG_SEG, "sleeping on iocount %x == %d\n", sp, sp->seg_iocount));
    558      1.26  perseant 		}
    559  1.40.2.6     skrll 		simple_unlock(&fs->lfs_interlock);
    560      1.26  perseant 		if (sync)
    561      1.31  perseant 			pool_put(&fs->lfs_segpool, sp);
    562      1.36  perseant 
    563       1.1   mycroft 		if (ckp) {
    564       1.1   mycroft 			fs->lfs_nactive = 0;
    565       1.8  perseant 			/* If we *know* everything's on disk, write both sbs */
    566      1.33  perseant 			/* XXX should wait for this one	 */
    567      1.21       chs 			if (sync)
    568      1.31  perseant 				lfs_writesuper(fs, fs->lfs_sboffs[fs->lfs_activesb]);
    569      1.31  perseant 			lfs_writesuper(fs, fs->lfs_sboffs[1 - fs->lfs_activesb]);
    570  1.40.2.4     skrll 			if (!(fs->lfs_ivnode->v_mount->mnt_iflag & IMNT_UNMOUNT)) {
    571      1.35  perseant 				lfs_auto_segclean(fs);
    572  1.40.2.4     skrll 				/* If sync, we can clean the remainder too */
    573  1.40.2.4     skrll 				if (sync)
    574  1.40.2.4     skrll 					lfs_auto_segclean(fs);
    575  1.40.2.4     skrll 			}
    576       1.8  perseant 			fs->lfs_activesb = 1 - fs->lfs_activesb;
    577  1.40.2.6     skrll #ifdef DEBUG
    578  1.40.2.6     skrll 			LFS_ENTER_LOG("segunlock_ckp", __FILE__, __LINE__, 0, 0, curproc->p_pid);
    579  1.40.2.6     skrll #endif
    580      1.38  perseant 			simple_lock(&fs->lfs_interlock);
    581      1.26  perseant 			--fs->lfs_seglock;
    582      1.26  perseant 			fs->lfs_lockpid = 0;
    583      1.38  perseant 			simple_unlock(&fs->lfs_interlock);
    584      1.26  perseant 			wakeup(&fs->lfs_seglock);
    585       1.1   mycroft 		}
    586      1.27  perseant 		/* Reenable fragment size changes */
    587      1.27  perseant 		lockmgr(&fs->lfs_fraglock, LK_RELEASE, 0);
    588      1.40  perseant 		if (do_unmark_dirop)
    589      1.40  perseant 			lfs_unmark_dirop(fs);
    590       1.1   mycroft 	} else if (fs->lfs_seglock == 0) {
    591      1.38  perseant 		simple_unlock(&fs->lfs_interlock);
    592       1.1   mycroft 		panic ("Seglock not held");
    593       1.1   mycroft 	} else {
    594       1.1   mycroft 		--fs->lfs_seglock;
    595      1.38  perseant 		simple_unlock(&fs->lfs_interlock);
    596       1.1   mycroft 	}
    597       1.1   mycroft }
    598  1.40.2.1     skrll 
    599  1.40.2.1     skrll /*
    600  1.40.2.1     skrll  * drain dirops and start writer.
    601  1.40.2.1     skrll  */
    602  1.40.2.1     skrll int
    603  1.40.2.1     skrll lfs_writer_enter(struct lfs *fs, const char *wmesg)
    604  1.40.2.1     skrll {
    605  1.40.2.1     skrll 	int error = 0;
    606  1.40.2.1     skrll 
    607  1.40.2.6     skrll 	ASSERT_MAYBE_SEGLOCK(fs);
    608  1.40.2.1     skrll 	simple_lock(&fs->lfs_interlock);
    609  1.40.2.1     skrll 
    610  1.40.2.1     skrll 	/* disallow dirops during flush */
    611  1.40.2.1     skrll 	fs->lfs_writer++;
    612  1.40.2.1     skrll 
    613  1.40.2.1     skrll 	while (fs->lfs_dirops > 0) {
    614  1.40.2.4     skrll 		++fs->lfs_diropwait;
    615  1.40.2.1     skrll 		error = ltsleep(&fs->lfs_writer, PRIBIO+1, wmesg, 0,
    616  1.40.2.6     skrll 				&fs->lfs_interlock);
    617  1.40.2.4     skrll 		--fs->lfs_diropwait;
    618  1.40.2.1     skrll 	}
    619  1.40.2.1     skrll 
    620  1.40.2.1     skrll 	if (error)
    621  1.40.2.1     skrll 		fs->lfs_writer--;
    622  1.40.2.1     skrll 
    623  1.40.2.1     skrll 	simple_unlock(&fs->lfs_interlock);
    624  1.40.2.1     skrll 
    625  1.40.2.1     skrll 	return error;
    626  1.40.2.1     skrll }
    627  1.40.2.1     skrll 
    628  1.40.2.1     skrll void
    629  1.40.2.1     skrll lfs_writer_leave(struct lfs *fs)
    630  1.40.2.1     skrll {
    631  1.40.2.1     skrll 	boolean_t dowakeup;
    632  1.40.2.1     skrll 
    633  1.40.2.6     skrll 	ASSERT_MAYBE_SEGLOCK(fs);
    634  1.40.2.1     skrll 	simple_lock(&fs->lfs_interlock);
    635  1.40.2.1     skrll 	dowakeup = !(--fs->lfs_writer);
    636  1.40.2.1     skrll 	simple_unlock(&fs->lfs_interlock);
    637  1.40.2.1     skrll 	if (dowakeup)
    638  1.40.2.1     skrll 		wakeup(&fs->lfs_dirops);
    639  1.40.2.1     skrll }
    640