Home | History | Annotate | Line # | Download | only in lfs
lfs_subr.c revision 1.101
      1  1.101        ad /*	$NetBSD: lfs_subr.c,v 1.101 2020/02/23 15:09:55 ad 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  *
     19    1.8  perseant  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20    1.8  perseant  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21    1.8  perseant  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22    1.8  perseant  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23    1.8  perseant  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24    1.8  perseant  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25    1.8  perseant  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26    1.8  perseant  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27    1.8  perseant  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28    1.8  perseant  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29    1.8  perseant  * POSSIBILITY OF SUCH DAMAGE.
     30    1.8  perseant  */
     31    1.1   mycroft /*
     32    1.1   mycroft  * Copyright (c) 1991, 1993
     33    1.1   mycroft  *	The Regents of the University of California.  All rights reserved.
     34    1.1   mycroft  *
     35    1.1   mycroft  * Redistribution and use in source and binary forms, with or without
     36    1.1   mycroft  * modification, are permitted provided that the following conditions
     37    1.1   mycroft  * are met:
     38    1.1   mycroft  * 1. Redistributions of source code must retain the above copyright
     39    1.1   mycroft  *    notice, this list of conditions and the following disclaimer.
     40    1.1   mycroft  * 2. Redistributions in binary form must reproduce the above copyright
     41    1.1   mycroft  *    notice, this list of conditions and the following disclaimer in the
     42    1.1   mycroft  *    documentation and/or other materials provided with the distribution.
     43   1.43       agc  * 3. Neither the name of the University nor the names of its contributors
     44    1.1   mycroft  *    may be used to endorse or promote products derived from this software
     45    1.1   mycroft  *    without specific prior written permission.
     46    1.1   mycroft  *
     47    1.1   mycroft  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     48    1.1   mycroft  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     49    1.1   mycroft  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     50    1.1   mycroft  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     51    1.1   mycroft  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     52    1.1   mycroft  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     53    1.1   mycroft  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     54    1.1   mycroft  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     55    1.1   mycroft  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     56    1.1   mycroft  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     57    1.1   mycroft  * SUCH DAMAGE.
     58    1.1   mycroft  *
     59    1.6      fvdl  *	@(#)lfs_subr.c	8.4 (Berkeley) 5/8/95
     60    1.1   mycroft  */
     61   1.20     lukem 
     62   1.20     lukem #include <sys/cdefs.h>
     63  1.101        ad __KERNEL_RCSID(0, "$NetBSD: lfs_subr.c,v 1.101 2020/02/23 15:09:55 ad Exp $");
     64    1.1   mycroft 
     65    1.1   mycroft #include <sys/param.h>
     66    1.3  christos #include <sys/systm.h>
     67    1.1   mycroft #include <sys/namei.h>
     68    1.1   mycroft #include <sys/vnode.h>
     69    1.1   mycroft #include <sys/buf.h>
     70    1.1   mycroft #include <sys/mount.h>
     71    1.1   mycroft #include <sys/malloc.h>
     72    1.1   mycroft #include <sys/proc.h>
     73   1.61  perseant #include <sys/kauth.h>
     74    1.1   mycroft 
     75   1.78  dholland #include <ufs/lfs/ulfs_inode.h>
     76    1.1   mycroft #include <ufs/lfs/lfs.h>
     77   1.84  dholland #include <ufs/lfs/lfs_accessors.h>
     78   1.80  dholland #include <ufs/lfs/lfs_kernel.h>
     79    1.1   mycroft #include <ufs/lfs/lfs_extern.h>
     80    1.1   mycroft 
     81   1.31  perseant #include <uvm/uvm.h>
     82   1.31  perseant 
     83   1.50  perseant #ifdef DEBUG
     84   1.53  christos const char *lfs_res_names[LFS_NB_COUNT] = {
     85   1.31  perseant 	"summary",
     86   1.31  perseant 	"superblock",
     87   1.52  perseant 	"file block",
     88   1.31  perseant 	"cluster",
     89   1.31  perseant 	"clean",
     90   1.52  perseant 	"blkiov",
     91   1.31  perseant };
     92   1.31  perseant #endif
     93   1.31  perseant 
     94   1.31  perseant int lfs_res_qty[LFS_NB_COUNT] = {
     95   1.31  perseant 	LFS_N_SUMMARIES,
     96   1.31  perseant 	LFS_N_SBLOCKS,
     97   1.31  perseant 	LFS_N_IBLOCKS,
     98   1.31  perseant 	LFS_N_CLUSTERS,
     99   1.31  perseant 	LFS_N_CLEAN,
    100   1.52  perseant 	LFS_N_BLKIOV,
    101   1.31  perseant };
    102   1.31  perseant 
    103   1.31  perseant void
    104   1.31  perseant lfs_setup_resblks(struct lfs *fs)
    105   1.31  perseant {
    106   1.31  perseant 	int i, j;
    107   1.31  perseant 	int maxbpp;
    108   1.31  perseant 
    109   1.51  perseant 	ASSERT_NO_SEGLOCK(fs);
    110   1.81  dholland 	fs->lfs_resblk = malloc(LFS_N_TOTAL * sizeof(res_t), M_SEGMENT,
    111   1.81  dholland 				M_WAITOK);
    112   1.31  perseant 	for (i = 0; i < LFS_N_TOTAL; i++) {
    113   1.31  perseant 		fs->lfs_resblk[i].inuse = 0;
    114   1.31  perseant 		fs->lfs_resblk[i].p = NULL;
    115   1.31  perseant 	}
    116   1.31  perseant 	for (i = 0; i < LFS_RESHASH_WIDTH; i++)
    117   1.31  perseant 		LIST_INIT(fs->lfs_reshash + i);
    118   1.31  perseant 
    119   1.31  perseant 	/*
    120   1.31  perseant 	 * These types of allocations can be larger than a page,
    121   1.31  perseant 	 * so we can't use the pool subsystem for them.
    122   1.31  perseant 	 */
    123   1.31  perseant 	for (i = 0, j = 0; j < LFS_N_SUMMARIES; j++, i++)
    124   1.83  dholland 		fs->lfs_resblk[i].size = lfs_sb_getsumsize(fs);
    125   1.31  perseant 	for (j = 0; j < LFS_N_SBLOCKS; j++, i++)
    126   1.34  perseant 		fs->lfs_resblk[i].size = LFS_SBPAD;
    127   1.31  perseant 	for (j = 0; j < LFS_N_IBLOCKS; j++, i++)
    128   1.82  dholland 		fs->lfs_resblk[i].size = lfs_sb_getbsize(fs);
    129   1.31  perseant 	for (j = 0; j < LFS_N_CLUSTERS; j++, i++)
    130   1.34  perseant 		fs->lfs_resblk[i].size = MAXPHYS;
    131   1.31  perseant 	for (j = 0; j < LFS_N_CLEAN; j++, i++)
    132   1.34  perseant 		fs->lfs_resblk[i].size = MAXPHYS;
    133   1.52  perseant 	for (j = 0; j < LFS_N_BLKIOV; j++, i++)
    134   1.52  perseant 		fs->lfs_resblk[i].size = LFS_MARKV_MAXBLKCNT * sizeof(BLOCK_INFO);
    135   1.34  perseant 
    136   1.34  perseant 	for (i = 0; i < LFS_N_TOTAL; i++) {
    137   1.34  perseant 		fs->lfs_resblk[i].p = malloc(fs->lfs_resblk[i].size,
    138   1.34  perseant 					     M_SEGMENT, M_WAITOK);
    139   1.34  perseant 	}
    140   1.31  perseant 
    141   1.31  perseant 	/*
    142   1.31  perseant 	 * Initialize pools for small types (XXX is BPP small?)
    143   1.31  perseant 	 */
    144   1.46    simonb 	pool_init(&fs->lfs_clpool, sizeof(struct lfs_cluster), 0, 0, 0,
    145   1.68        ad 		"lfsclpl", &pool_allocator_nointr, IPL_NONE);
    146   1.46    simonb 	pool_init(&fs->lfs_segpool, sizeof(struct segment), 0, 0, 0,
    147   1.68        ad 		"lfssegpool", &pool_allocator_nointr, IPL_NONE);
    148   1.86  dholland 	/* XXX: should this int32 be 32/64? */
    149   1.83  dholland 	maxbpp = ((lfs_sb_getsumsize(fs) - SEGSUM_SIZE(fs)) / sizeof(int32_t) + 2);
    150   1.82  dholland 	maxbpp = MIN(maxbpp, lfs_segsize(fs) / lfs_sb_getfsize(fs) + 2);
    151   1.46    simonb 	pool_init(&fs->lfs_bpppool, maxbpp * sizeof(struct buf *), 0, 0, 0,
    152   1.68        ad 		"lfsbpppl", &pool_allocator_nointr, IPL_NONE);
    153   1.31  perseant }
    154   1.31  perseant 
    155   1.31  perseant void
    156   1.31  perseant lfs_free_resblks(struct lfs *fs)
    157   1.31  perseant {
    158   1.31  perseant 	int i;
    159   1.31  perseant 
    160   1.31  perseant 	pool_destroy(&fs->lfs_bpppool);
    161   1.31  perseant 	pool_destroy(&fs->lfs_segpool);
    162   1.31  perseant 	pool_destroy(&fs->lfs_clpool);
    163   1.31  perseant 
    164   1.72        ad 	mutex_enter(&lfs_lock);
    165   1.31  perseant 	for (i = 0; i < LFS_N_TOTAL; i++) {
    166   1.39  perseant 		while (fs->lfs_resblk[i].inuse)
    167   1.72        ad 			mtsleep(&fs->lfs_resblk, PRIBIO + 1, "lfs_free", 0,
    168   1.72        ad 				&lfs_lock);
    169   1.31  perseant 		if (fs->lfs_resblk[i].p != NULL)
    170   1.31  perseant 			free(fs->lfs_resblk[i].p, M_SEGMENT);
    171   1.31  perseant 	}
    172   1.31  perseant 	free(fs->lfs_resblk, M_SEGMENT);
    173   1.72        ad 	mutex_exit(&lfs_lock);
    174   1.31  perseant }
    175   1.31  perseant 
    176   1.31  perseant static unsigned int
    177   1.31  perseant lfs_mhash(void *vp)
    178   1.31  perseant {
    179   1.31  perseant 	return (unsigned int)(((unsigned long)vp) >> 2) % LFS_RESHASH_WIDTH;
    180   1.31  perseant }
    181   1.31  perseant 
    182   1.31  perseant /*
    183   1.31  perseant  * Return memory of the given size for the given purpose, or use one of a
    184   1.31  perseant  * number of spare last-resort buffers, if malloc returns NULL.
    185   1.49     perry  */
    186   1.31  perseant void *
    187   1.31  perseant lfs_malloc(struct lfs *fs, size_t size, int type)
    188   1.31  perseant {
    189   1.31  perseant 	struct lfs_res_blk *re;
    190   1.31  perseant 	void *r;
    191   1.91      maya 	int i, start;
    192   1.31  perseant 	unsigned int h;
    193   1.31  perseant 
    194   1.51  perseant 	ASSERT_MAYBE_SEGLOCK(fs);
    195   1.34  perseant 	r = NULL;
    196   1.34  perseant 
    197   1.31  perseant 	/* If no mem allocated for this type, it just waits */
    198   1.34  perseant 	if (lfs_res_qty[type] == 0) {
    199   1.34  perseant 		r = malloc(size, M_SEGMENT, M_WAITOK);
    200   1.34  perseant 		return r;
    201   1.34  perseant 	}
    202   1.31  perseant 
    203   1.31  perseant 	/* Otherwise try a quick malloc, and if it works, great */
    204   1.34  perseant 	if ((r = malloc(size, M_SEGMENT, M_NOWAIT)) != NULL) {
    205   1.31  perseant 		return r;
    206   1.34  perseant 	}
    207   1.31  perseant 
    208   1.31  perseant 	/*
    209   1.31  perseant 	 * If malloc returned NULL, we are forced to use one of our
    210   1.31  perseant 	 * reserve blocks.  We have on hand at least one summary block,
    211   1.31  perseant 	 * at least one cluster block, at least one superblock,
    212   1.31  perseant 	 * and several indirect blocks.
    213   1.31  perseant 	 */
    214   1.51  perseant 
    215   1.72        ad 	mutex_enter(&lfs_lock);
    216   1.31  perseant 	/* skip over blocks of other types */
    217   1.31  perseant 	for (i = 0, start = 0; i < type; i++)
    218   1.31  perseant 		start += lfs_res_qty[i];
    219   1.31  perseant 	while (r == NULL) {
    220   1.31  perseant 		for (i = 0; i < lfs_res_qty[type]; i++) {
    221   1.31  perseant 			if (fs->lfs_resblk[start + i].inuse == 0) {
    222   1.31  perseant 				re = fs->lfs_resblk + start + i;
    223   1.31  perseant 				re->inuse = 1;
    224   1.31  perseant 				r = re->p;
    225   1.34  perseant 				KASSERT(re->size >= size);
    226   1.31  perseant 				h = lfs_mhash(r);
    227   1.31  perseant 				LIST_INSERT_HEAD(&fs->lfs_reshash[h], re, res);
    228   1.72        ad 				mutex_exit(&lfs_lock);
    229   1.31  perseant 				return r;
    230   1.31  perseant 			}
    231   1.31  perseant 		}
    232   1.51  perseant 		DLOG((DLOG_MALLOC, "sleeping on %s (%d)\n",
    233   1.51  perseant 		      lfs_res_names[type], lfs_res_qty[type]));
    234   1.72        ad 		mtsleep(&fs->lfs_resblk, PVM, "lfs_malloc", 0,
    235   1.72        ad 			&lfs_lock);
    236   1.51  perseant 		DLOG((DLOG_MALLOC, "done sleeping on %s\n",
    237   1.51  perseant 		      lfs_res_names[type]));
    238   1.31  perseant 	}
    239   1.31  perseant 	/* NOTREACHED */
    240   1.72        ad 	mutex_exit(&lfs_lock);
    241   1.31  perseant 	return r;
    242   1.31  perseant }
    243   1.31  perseant 
    244   1.31  perseant void
    245   1.65  christos lfs_free(struct lfs *fs, void *p, int type)
    246   1.31  perseant {
    247   1.31  perseant 	unsigned int h;
    248   1.31  perseant 	res_t *re;
    249   1.31  perseant 
    250   1.51  perseant 	ASSERT_MAYBE_SEGLOCK(fs);
    251   1.31  perseant 	h = lfs_mhash(p);
    252   1.72        ad 	mutex_enter(&lfs_lock);
    253   1.31  perseant 	LIST_FOREACH(re, &fs->lfs_reshash[h], res) {
    254   1.31  perseant 		if (re->p == p) {
    255   1.32      yamt 			KASSERT(re->inuse == 1);
    256   1.31  perseant 			LIST_REMOVE(re, res);
    257   1.31  perseant 			re->inuse = 0;
    258   1.31  perseant 			wakeup(&fs->lfs_resblk);
    259   1.72        ad 			mutex_exit(&lfs_lock);
    260   1.31  perseant 			return;
    261   1.31  perseant 		}
    262   1.31  perseant 	}
    263   1.90      maya 
    264   1.95      maya #ifdef notyet /* XXX this assert fires */
    265   1.90      maya 	for (int i = 0; i < LFS_N_TOTAL; i++) {
    266   1.91      maya 		KDASSERTMSG(fs->lfs_resblk[i].p == p,
    267   1.90      maya 		    "lfs_free: inconsistent reserved block");
    268   1.32      yamt 	}
    269   1.95      maya #endif
    270   1.90      maya 
    271   1.72        ad 	mutex_exit(&lfs_lock);
    272   1.90      maya 
    273   1.31  perseant 	/*
    274   1.31  perseant 	 * If we didn't find it, free it.
    275   1.31  perseant 	 */
    276   1.31  perseant 	free(p, M_SEGMENT);
    277   1.31  perseant }
    278    1.1   mycroft 
    279    1.1   mycroft /*
    280    1.1   mycroft  * lfs_seglock --
    281    1.1   mycroft  *	Single thread the segment writer.
    282    1.1   mycroft  */
    283   1.31  perseant int
    284   1.18  perseant lfs_seglock(struct lfs *fs, unsigned long flags)
    285    1.1   mycroft {
    286    1.1   mycroft 	struct segment *sp;
    287   1.49     perry 
    288   1.72        ad 	mutex_enter(&lfs_lock);
    289    1.7   thorpej 	if (fs->lfs_seglock) {
    290   1.58  perseant 		if (fs->lfs_lockpid == curproc->p_pid &&
    291   1.58  perseant 		    fs->lfs_locklwp == curlwp->l_lid) {
    292    1.1   mycroft 			++fs->lfs_seglock;
    293    1.1   mycroft 			fs->lfs_sp->seg_flags |= flags;
    294   1.74   mlelstv 			mutex_exit(&lfs_lock);
    295   1.31  perseant 			return 0;
    296   1.38  perseant 		} else if (flags & SEGM_PAGEDAEMON) {
    297   1.72        ad 			mutex_exit(&lfs_lock);
    298   1.31  perseant 			return EWOULDBLOCK;
    299   1.51  perseant 		} else {
    300   1.51  perseant 			while (fs->lfs_seglock) {
    301   1.72        ad 				(void)mtsleep(&fs->lfs_seglock, PRIBIO + 1,
    302   1.72        ad 					"lfs_seglock", 0, &lfs_lock);
    303   1.51  perseant 			}
    304   1.51  perseant 		}
    305    1.7   thorpej 	}
    306   1.49     perry 
    307    1.1   mycroft 	fs->lfs_seglock = 1;
    308    1.1   mycroft 	fs->lfs_lockpid = curproc->p_pid;
    309   1.58  perseant 	fs->lfs_locklwp = curlwp->l_lid;
    310   1.72        ad 	mutex_exit(&lfs_lock);
    311   1.36  perseant 	fs->lfs_cleanind = 0;
    312   1.36  perseant 
    313   1.51  perseant 	LFS_ENTER_LOG("seglock", __FILE__, __LINE__, 0, flags, curproc->p_pid);
    314   1.89      maya 
    315   1.27  perseant 	/* Drain fragment size changes out */
    316   1.66        ad 	rw_enter(&fs->lfs_fraglock, RW_WRITER);
    317   1.27  perseant 
    318   1.31  perseant 	sp = fs->lfs_sp = pool_get(&fs->lfs_segpool, PR_WAITOK);
    319   1.31  perseant 	sp->bpp = pool_get(&fs->lfs_bpppool, PR_WAITOK);
    320    1.1   mycroft 	sp->seg_flags = flags;
    321    1.1   mycroft 	sp->vp = NULL;
    322   1.26  perseant 	sp->seg_iocount = 0;
    323    1.1   mycroft 	(void) lfs_initseg(fs);
    324   1.49     perry 
    325    1.1   mycroft 	/*
    326    1.1   mycroft 	 * Keep a cumulative count of the outstanding I/O operations.  If the
    327    1.1   mycroft 	 * disk drive catches up with us it could go to zero before we finish,
    328    1.1   mycroft 	 * so we artificially increment it by one until we've scheduled all of
    329    1.1   mycroft 	 * the writes we intend to do.
    330    1.1   mycroft 	 */
    331   1.72        ad 	mutex_enter(&lfs_lock);
    332    1.1   mycroft 	++fs->lfs_iocount;
    333   1.82  dholland 	fs->lfs_startseg = lfs_sb_getcurseg(fs);
    334   1.72        ad 	mutex_exit(&lfs_lock);
    335   1.31  perseant 	return 0;
    336    1.1   mycroft }
    337    1.8  perseant 
    338   1.31  perseant static void lfs_unmark_dirop(struct lfs *);
    339   1.31  perseant 
    340   1.98  riastrad static struct evcnt lfs_dchain_marker_pass_dirop =
    341   1.98  riastrad     EVCNT_INITIALIZER(EVCNT_TYPE_MISC, NULL, "lfs", "dchain marker pass dirop");
    342   1.98  riastrad EVCNT_ATTACH_STATIC(lfs_dchain_marker_pass_dirop);
    343   1.98  riastrad 
    344   1.31  perseant static void
    345   1.31  perseant lfs_unmark_dirop(struct lfs *fs)
    346   1.31  perseant {
    347   1.98  riastrad 	struct inode *ip, *marker;
    348   1.31  perseant 	struct vnode *vp;
    349   1.40  perseant 	int doit;
    350   1.31  perseant 
    351   1.51  perseant 	ASSERT_NO_SEGLOCK(fs);
    352   1.72        ad 	mutex_enter(&lfs_lock);
    353   1.40  perseant 	doit = !(fs->lfs_flags & LFS_UNDIROP);
    354   1.40  perseant 	if (doit)
    355   1.40  perseant 		fs->lfs_flags |= LFS_UNDIROP;
    356   1.98  riastrad 	mutex_exit(&lfs_lock);
    357   1.98  riastrad 
    358   1.98  riastrad 	if (!doit)
    359   1.40  perseant 		return;
    360   1.40  perseant 
    361   1.98  riastrad 	marker = pool_get(&lfs_inode_pool, PR_WAITOK);
    362   1.98  riastrad 	KASSERT(fs != NULL);
    363   1.98  riastrad 	memset(marker, 0, sizeof(*marker));
    364   1.98  riastrad 	marker->inode_ext.lfs = pool_get(&lfs_inoext_pool, PR_WAITOK);
    365   1.98  riastrad 	memset(marker->inode_ext.lfs, 0, sizeof(*marker->inode_ext.lfs));
    366   1.98  riastrad 	marker->i_state |= IN_MARKER;
    367   1.98  riastrad 
    368   1.98  riastrad 	mutex_enter(&lfs_lock);
    369   1.98  riastrad 	TAILQ_INSERT_HEAD(&fs->lfs_dchainhd, marker, i_lfs_dchain);
    370   1.98  riastrad 	while ((ip = TAILQ_NEXT(marker, i_lfs_dchain)) != NULL) {
    371   1.98  riastrad 		TAILQ_REMOVE(&fs->lfs_dchainhd, marker, i_lfs_dchain);
    372   1.98  riastrad 		TAILQ_INSERT_AFTER(&fs->lfs_dchainhd, ip, marker,
    373   1.98  riastrad 		    i_lfs_dchain);
    374   1.98  riastrad 		if (ip->i_state & IN_MARKER) {
    375   1.98  riastrad 			lfs_dchain_marker_pass_dirop.ev_count++;
    376   1.98  riastrad 			continue;
    377   1.98  riastrad 		}
    378   1.31  perseant 		vp = ITOV(ip);
    379   1.94      maya 		if ((ip->i_state & (IN_ADIROP | IN_CDIROP)) == IN_CDIROP) {
    380   1.31  perseant 			--lfs_dirvcount;
    381   1.59  perseant 			--fs->lfs_dirvcount;
    382   1.71        ad 			vp->v_uflag &= ~VU_DIROP;
    383   1.31  perseant 			TAILQ_REMOVE(&fs->lfs_dchainhd, ip, i_lfs_dchain);
    384   1.31  perseant 			wakeup(&lfs_dirvcount);
    385   1.31  perseant 			fs->lfs_unlockvp = vp;
    386   1.72        ad 			mutex_exit(&lfs_lock);
    387   1.76   hannken 			vrele(vp);
    388   1.72        ad 			mutex_enter(&lfs_lock);
    389   1.31  perseant 			fs->lfs_unlockvp = NULL;
    390   1.94      maya 			ip->i_state &= ~IN_CDIROP;
    391   1.72        ad 		}
    392   1.31  perseant 	}
    393   1.98  riastrad 	TAILQ_REMOVE(&fs->lfs_dchainhd, marker, i_lfs_dchain);
    394   1.40  perseant 	fs->lfs_flags &= ~LFS_UNDIROP;
    395   1.51  perseant 	wakeup(&fs->lfs_flags);
    396   1.72        ad 	mutex_exit(&lfs_lock);
    397   1.98  riastrad 
    398   1.98  riastrad 	pool_put(&lfs_inoext_pool, marker->inode_ext.lfs);
    399   1.98  riastrad 	pool_put(&lfs_inode_pool, marker);
    400   1.31  perseant }
    401   1.31  perseant 
    402   1.31  perseant static void
    403   1.31  perseant lfs_auto_segclean(struct lfs *fs)
    404   1.31  perseant {
    405   1.92      maya 	int i, error, waited;
    406   1.31  perseant 
    407   1.51  perseant 	ASSERT_SEGLOCK(fs);
    408   1.31  perseant 	/*
    409   1.31  perseant 	 * Now that we've swapped lfs_activesb, but while we still
    410   1.31  perseant 	 * hold the segment lock, run through the segment list marking
    411   1.31  perseant 	 * the empty ones clean.
    412   1.31  perseant 	 * XXX - do we really need to do them all at once?
    413   1.31  perseant 	 */
    414   1.48  perseant 	waited = 0;
    415   1.83  dholland 	for (i = 0; i < lfs_sb_getnseg(fs); i++) {
    416   1.31  perseant 		if ((fs->lfs_suflags[0][i] &
    417   1.31  perseant 		     (SEGUSE_ACTIVE | SEGUSE_DIRTY | SEGUSE_EMPTY)) ==
    418   1.31  perseant 		    (SEGUSE_DIRTY | SEGUSE_EMPTY) &&
    419   1.31  perseant 		    (fs->lfs_suflags[1][i] &
    420   1.31  perseant 		     (SEGUSE_ACTIVE | SEGUSE_DIRTY | SEGUSE_EMPTY)) ==
    421   1.31  perseant 		    (SEGUSE_DIRTY | SEGUSE_EMPTY)) {
    422   1.31  perseant 
    423   1.48  perseant 			/* Make sure the sb is written before we clean */
    424   1.72        ad 			mutex_enter(&lfs_lock);
    425   1.48  perseant 			while (waited == 0 && fs->lfs_sbactive)
    426   1.72        ad 				mtsleep(&fs->lfs_sbactive, PRIBIO+1, "lfs asb",
    427   1.72        ad 					0, &lfs_lock);
    428   1.72        ad 			mutex_exit(&lfs_lock);
    429   1.48  perseant 			waited = 1;
    430   1.48  perseant 
    431   1.31  perseant 			if ((error = lfs_do_segclean(fs, i)) != 0) {
    432   1.50  perseant 				DLOG((DLOG_CLEAN, "lfs_auto_segclean: lfs_do_segclean returned %d for seg %d\n", error, i));
    433   1.31  perseant 			}
    434   1.31  perseant 		}
    435   1.31  perseant 		fs->lfs_suflags[1 - fs->lfs_activesb][i] =
    436   1.31  perseant 			fs->lfs_suflags[fs->lfs_activesb][i];
    437   1.31  perseant 	}
    438   1.31  perseant }
    439   1.31  perseant 
    440    1.1   mycroft /*
    441    1.1   mycroft  * lfs_segunlock --
    442    1.1   mycroft  *	Single thread the segment writer.
    443    1.1   mycroft  */
    444    1.1   mycroft void
    445   1.18  perseant lfs_segunlock(struct lfs *fs)
    446    1.1   mycroft {
    447    1.1   mycroft 	struct segment *sp;
    448    1.1   mycroft 	unsigned long sync, ckp;
    449   1.22  perseant 	struct buf *bp;
    450   1.40  perseant 	int do_unmark_dirop = 0;
    451   1.49     perry 
    452   1.15  perseant 	sp = fs->lfs_sp;
    453   1.15  perseant 
    454   1.72        ad 	mutex_enter(&lfs_lock);
    455   1.96      maya 
    456   1.96      maya 	if (!LFS_SEGLOCK_HELD(fs))
    457   1.96      maya 		panic("lfs seglock not held");
    458   1.96      maya 
    459   1.15  perseant 	if (fs->lfs_seglock == 1) {
    460   1.77  perseant 		if ((sp->seg_flags & (SEGM_PROT | SEGM_CLEAN)) == 0)
    461   1.40  perseant 			do_unmark_dirop = 1;
    462   1.72        ad 		mutex_exit(&lfs_lock);
    463    1.1   mycroft 		sync = sp->seg_flags & SEGM_SYNC;
    464    1.1   mycroft 		ckp = sp->seg_flags & SEGM_CKP;
    465   1.57  perseant 
    466   1.57  perseant 		/* We should have a segment summary, and nothing else */
    467   1.57  perseant 		KASSERT(sp->cbpp == sp->bpp + 1);
    468   1.57  perseant 
    469   1.57  perseant 		/* Free allocated segment summary */
    470   1.82  dholland 		lfs_sb_suboffset(fs, lfs_btofsb(fs, lfs_sb_getsumsize(fs)));
    471   1.57  perseant 		bp = *sp->bpp;
    472   1.57  perseant 		lfs_freebuf(fs, bp);
    473    1.8  perseant 
    474   1.31  perseant 		pool_put(&fs->lfs_bpppool, sp->bpp);
    475   1.18  perseant 		sp->bpp = NULL;
    476   1.36  perseant 
    477   1.36  perseant 		/*
    478   1.36  perseant 		 * If we're not sync, we're done with sp, get rid of it.
    479   1.36  perseant 		 * Otherwise, we keep a local copy around but free
    480   1.36  perseant 		 * fs->lfs_sp so another process can use it (we have to
    481   1.36  perseant 		 * wait but they don't have to wait for us).
    482   1.36  perseant 		 */
    483   1.26  perseant 		if (!sync)
    484   1.31  perseant 			pool_put(&fs->lfs_segpool, sp);
    485   1.18  perseant 		fs->lfs_sp = NULL;
    486    1.1   mycroft 
    487    1.1   mycroft 		/*
    488    1.1   mycroft 		 * If the I/O count is non-zero, sleep until it reaches zero.
    489    1.1   mycroft 		 * At the moment, the user's process hangs around so we can
    490    1.1   mycroft 		 * sleep.
    491    1.1   mycroft 		 */
    492   1.72        ad 		mutex_enter(&lfs_lock);
    493   1.93       chs 		if (--fs->lfs_iocount <= 1)
    494   1.22  perseant 			wakeup(&fs->lfs_iocount);
    495   1.72        ad 		mutex_exit(&lfs_lock);
    496   1.93       chs 
    497    1.1   mycroft 		/*
    498   1.26  perseant 		 * If we're not checkpointing, we don't have to block
    499   1.26  perseant 		 * other processes to wait for a synchronous write
    500   1.26  perseant 		 * to complete.
    501   1.26  perseant 		 */
    502   1.26  perseant 		if (!ckp) {
    503   1.51  perseant 			LFS_ENTER_LOG("segunlock_std", __FILE__, __LINE__, 0, 0, curproc->p_pid);
    504   1.89      maya 
    505   1.72        ad 			mutex_enter(&lfs_lock);
    506   1.26  perseant 			--fs->lfs_seglock;
    507   1.26  perseant 			fs->lfs_lockpid = 0;
    508   1.58  perseant 			fs->lfs_locklwp = 0;
    509   1.72        ad 			mutex_exit(&lfs_lock);
    510   1.26  perseant 			wakeup(&fs->lfs_seglock);
    511   1.26  perseant 		}
    512   1.26  perseant 		/*
    513    1.1   mycroft 		 * We let checkpoints happen asynchronously.  That means
    514    1.1   mycroft 		 * that during recovery, we have to roll forward between
    515    1.1   mycroft 		 * the two segments described by the first and second
    516    1.1   mycroft 		 * superblocks to make sure that the checkpoint described
    517    1.1   mycroft 		 * by a superblock completed.
    518    1.1   mycroft 		 */
    519   1.72        ad 		mutex_enter(&lfs_lock);
    520   1.74   mlelstv 		while (ckp && sync && fs->lfs_iocount) {
    521   1.72        ad 			(void)mtsleep(&fs->lfs_iocount, PRIBIO + 1,
    522   1.72        ad 				      "lfs_iocount", 0, &lfs_lock);
    523   1.74   mlelstv 			DLOG((DLOG_SEG, "sleeping on iocount %x == %d\n", fs, fs->lfs_iocount));
    524   1.74   mlelstv 		}
    525   1.26  perseant 		while (sync && sp->seg_iocount) {
    526   1.72        ad 			(void)mtsleep(&sp->seg_iocount, PRIBIO + 1,
    527   1.72        ad 				     "seg_iocount", 0, &lfs_lock);
    528   1.50  perseant 			DLOG((DLOG_SEG, "sleeping on iocount %x == %d\n", sp, sp->seg_iocount));
    529   1.26  perseant 		}
    530   1.72        ad 		mutex_exit(&lfs_lock);
    531   1.26  perseant 		if (sync)
    532   1.31  perseant 			pool_put(&fs->lfs_segpool, sp);
    533   1.36  perseant 
    534    1.1   mycroft 		if (ckp) {
    535    1.1   mycroft 			fs->lfs_nactive = 0;
    536    1.8  perseant 			/* If we *know* everything's on disk, write both sbs */
    537   1.33  perseant 			/* XXX should wait for this one	 */
    538   1.21       chs 			if (sync)
    539   1.83  dholland 				lfs_writesuper(fs, lfs_sb_getsboff(fs, fs->lfs_activesb));
    540   1.83  dholland 			lfs_writesuper(fs, lfs_sb_getsboff(fs, 1 - fs->lfs_activesb));
    541   1.48  perseant 			if (!(fs->lfs_ivnode->v_mount->mnt_iflag & IMNT_UNMOUNT)) {
    542   1.35  perseant 				lfs_auto_segclean(fs);
    543   1.48  perseant 				/* If sync, we can clean the remainder too */
    544   1.48  perseant 				if (sync)
    545   1.48  perseant 					lfs_auto_segclean(fs);
    546   1.48  perseant 			}
    547    1.8  perseant 			fs->lfs_activesb = 1 - fs->lfs_activesb;
    548   1.89      maya 
    549   1.51  perseant 			LFS_ENTER_LOG("segunlock_ckp", __FILE__, __LINE__, 0, 0, curproc->p_pid);
    550   1.89      maya 
    551   1.72        ad 			mutex_enter(&lfs_lock);
    552   1.26  perseant 			--fs->lfs_seglock;
    553   1.26  perseant 			fs->lfs_lockpid = 0;
    554   1.58  perseant 			fs->lfs_locklwp = 0;
    555   1.72        ad 			mutex_exit(&lfs_lock);
    556   1.26  perseant 			wakeup(&fs->lfs_seglock);
    557    1.1   mycroft 		}
    558   1.27  perseant 		/* Reenable fragment size changes */
    559   1.66        ad 		rw_exit(&fs->lfs_fraglock);
    560   1.40  perseant 		if (do_unmark_dirop)
    561   1.40  perseant 			lfs_unmark_dirop(fs);
    562    1.1   mycroft 	} else {
    563    1.1   mycroft 		--fs->lfs_seglock;
    564  1.100  riastrad 		KASSERT(fs->lfs_seglock != 0);
    565   1.72        ad 		mutex_exit(&lfs_lock);
    566    1.1   mycroft 	}
    567   1.41      yamt }
    568   1.41      yamt 
    569   1.41      yamt /*
    570   1.69  perseant  * Drain dirops and start writer.
    571   1.69  perseant  *
    572   1.69  perseant  * No simple_locks are held when we enter and none are held when we return.
    573   1.41      yamt  */
    574  1.100  riastrad void
    575   1.41      yamt lfs_writer_enter(struct lfs *fs, const char *wmesg)
    576   1.41      yamt {
    577  1.101        ad 	int error __diagused;
    578   1.41      yamt 
    579   1.99  riastrad 	ASSERT_NO_SEGLOCK(fs);
    580   1.72        ad 	mutex_enter(&lfs_lock);
    581   1.41      yamt 
    582   1.41      yamt 	/* disallow dirops during flush */
    583   1.41      yamt 	fs->lfs_writer++;
    584   1.41      yamt 
    585   1.41      yamt 	while (fs->lfs_dirops > 0) {
    586   1.49     perry 		++fs->lfs_diropwait;
    587   1.72        ad 		error = mtsleep(&fs->lfs_writer, PRIBIO+1, wmesg, 0,
    588   1.72        ad 				&lfs_lock);
    589  1.100  riastrad 		KASSERT(error == 0);
    590   1.49     perry 		--fs->lfs_diropwait;
    591   1.41      yamt 	}
    592   1.41      yamt 
    593   1.72        ad 	mutex_exit(&lfs_lock);
    594   1.41      yamt }
    595   1.41      yamt 
    596   1.99  riastrad int
    597   1.99  riastrad lfs_writer_tryenter(struct lfs *fs)
    598   1.99  riastrad {
    599   1.99  riastrad 	int writer_set;
    600   1.99  riastrad 
    601   1.99  riastrad 	ASSERT_MAYBE_SEGLOCK(fs);
    602   1.99  riastrad 	mutex_enter(&lfs_lock);
    603   1.99  riastrad 	writer_set = (fs->lfs_dirops == 0);
    604   1.99  riastrad 	if (writer_set)
    605   1.99  riastrad 		fs->lfs_writer++;
    606   1.99  riastrad 	mutex_exit(&lfs_lock);
    607   1.99  riastrad 
    608   1.99  riastrad 	return writer_set;
    609   1.99  riastrad }
    610   1.99  riastrad 
    611   1.41      yamt void
    612   1.41      yamt lfs_writer_leave(struct lfs *fs)
    613   1.41      yamt {
    614   1.67   thorpej 	bool dowakeup;
    615   1.41      yamt 
    616   1.51  perseant 	ASSERT_MAYBE_SEGLOCK(fs);
    617   1.72        ad 	mutex_enter(&lfs_lock);
    618   1.41      yamt 	dowakeup = !(--fs->lfs_writer);
    619   1.41      yamt 	if (dowakeup)
    620   1.87      maya 		cv_broadcast(&fs->lfs_diropscv);
    621   1.88      maya 	mutex_exit(&lfs_lock);
    622    1.1   mycroft }
    623   1.57  perseant 
    624   1.57  perseant /*
    625   1.57  perseant  * Unlock, wait for the cleaner, then relock to where we were before.
    626   1.57  perseant  * To be used only at a fairly high level, to address a paucity of free
    627   1.57  perseant  * segments propagated back from lfs_gop_write().
    628   1.57  perseant  */
    629   1.57  perseant void
    630   1.57  perseant lfs_segunlock_relock(struct lfs *fs)
    631   1.57  perseant {
    632   1.57  perseant 	int n = fs->lfs_seglock;
    633   1.57  perseant 	u_int16_t seg_flags;
    634   1.61  perseant 	CLEANERINFO *cip;
    635   1.61  perseant 	struct buf *bp;
    636   1.57  perseant 
    637   1.57  perseant 	if (n == 0)
    638   1.57  perseant 		return;
    639   1.57  perseant 
    640   1.57  perseant 	/* Write anything we've already gathered to disk */
    641   1.57  perseant 	lfs_writeseg(fs, fs->lfs_sp);
    642   1.57  perseant 
    643   1.61  perseant 	/* Tell cleaner */
    644   1.61  perseant 	LFS_CLEANERINFO(cip, fs, bp);
    645   1.85  dholland 	lfs_ci_setflags(fs, cip,
    646   1.85  dholland 			lfs_ci_getflags(fs, cip) | LFS_CLEANER_MUST_CLEAN);
    647   1.61  perseant 	LFS_SYNC_CLEANERINFO(cip, fs, bp, 1);
    648   1.61  perseant 
    649   1.57  perseant 	/* Save segment flags for later */
    650   1.57  perseant 	seg_flags = fs->lfs_sp->seg_flags;
    651   1.57  perseant 
    652   1.57  perseant 	fs->lfs_sp->seg_flags |= SEGM_PROT; /* Don't unmark dirop nodes */
    653   1.57  perseant 	while(fs->lfs_seglock)
    654   1.57  perseant 		lfs_segunlock(fs);
    655   1.57  perseant 
    656   1.57  perseant 	/* Wait for the cleaner */
    657   1.60  perseant 	lfs_wakeup_cleaner(fs);
    658   1.72        ad 	mutex_enter(&lfs_lock);
    659   1.57  perseant 	while (LFS_STARVED_FOR_SEGS(fs))
    660   1.82  dholland 		mtsleep(&fs->lfs_availsleep, PRIBIO, "relock", 0,
    661   1.72        ad 			&lfs_lock);
    662   1.72        ad 	mutex_exit(&lfs_lock);
    663   1.57  perseant 
    664   1.57  perseant 	/* Put the segment lock back the way it was. */
    665   1.57  perseant 	while(n--)
    666   1.57  perseant 		lfs_seglock(fs, seg_flags);
    667   1.57  perseant 
    668   1.61  perseant 	/* Cleaner can relax now */
    669   1.61  perseant 	LFS_CLEANERINFO(cip, fs, bp);
    670   1.85  dholland 	lfs_ci_setflags(fs, cip,
    671   1.85  dholland 			lfs_ci_getflags(fs, cip) & ~LFS_CLEANER_MUST_CLEAN);
    672   1.61  perseant 	LFS_SYNC_CLEANERINFO(cip, fs, bp, 1);
    673   1.61  perseant 
    674   1.57  perseant 	return;
    675   1.57  perseant }
    676   1.60  perseant 
    677   1.60  perseant /*
    678   1.60  perseant  * Wake up the cleaner, provided that nowrap is not set.
    679   1.60  perseant  */
    680   1.60  perseant void
    681   1.60  perseant lfs_wakeup_cleaner(struct lfs *fs)
    682   1.60  perseant {
    683   1.60  perseant 	if (fs->lfs_nowrap > 0)
    684   1.60  perseant 		return;
    685   1.60  perseant 
    686   1.97      maya 	cv_broadcast(&fs->lfs_nextsegsleep);
    687   1.97      maya 	cv_broadcast(&lfs_allclean_wakeup);
    688   1.60  perseant }
    689