Home | History | Annotate | Line # | Download | only in lfs
lfs_vnops.c revision 1.254
      1 /*	$NetBSD: lfs_vnops.c,v 1.254 2013/07/28 01:27:02 dholland Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1999, 2000, 2001, 2002, 2003 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Konrad E. Schroder <perseant (at) hhhh.org>.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 /*
     32  * Copyright (c) 1986, 1989, 1991, 1993, 1995
     33  *	The Regents of the University of California.  All rights reserved.
     34  *
     35  * Redistribution and use in source and binary forms, with or without
     36  * modification, are permitted provided that the following conditions
     37  * are met:
     38  * 1. Redistributions of source code must retain the above copyright
     39  *    notice, this list of conditions and the following disclaimer.
     40  * 2. Redistributions in binary form must reproduce the above copyright
     41  *    notice, this list of conditions and the following disclaimer in the
     42  *    documentation and/or other materials provided with the distribution.
     43  * 3. Neither the name of the University nor the names of its contributors
     44  *    may be used to endorse or promote products derived from this software
     45  *    without specific prior written permission.
     46  *
     47  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     48  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     49  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     50  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     51  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     52  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     53  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     54  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     55  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     56  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     57  * SUCH DAMAGE.
     58  *
     59  *	@(#)lfs_vnops.c	8.13 (Berkeley) 6/10/95
     60  */
     61 
     62 #include <sys/cdefs.h>
     63 __KERNEL_RCSID(0, "$NetBSD: lfs_vnops.c,v 1.254 2013/07/28 01:27:02 dholland Exp $");
     64 
     65 #ifdef _KERNEL_OPT
     66 #include "opt_compat_netbsd.h"
     67 #include "opt_uvm_page_trkown.h"
     68 #endif
     69 
     70 #include <sys/param.h>
     71 #include <sys/systm.h>
     72 #include <sys/namei.h>
     73 #include <sys/resourcevar.h>
     74 #include <sys/kernel.h>
     75 #include <sys/file.h>
     76 #include <sys/stat.h>
     77 #include <sys/buf.h>
     78 #include <sys/proc.h>
     79 #include <sys/mount.h>
     80 #include <sys/vnode.h>
     81 #include <sys/pool.h>
     82 #include <sys/signalvar.h>
     83 #include <sys/kauth.h>
     84 #include <sys/syslog.h>
     85 #include <sys/fstrans.h>
     86 
     87 #include <miscfs/fifofs/fifo.h>
     88 #include <miscfs/genfs/genfs.h>
     89 #include <miscfs/specfs/specdev.h>
     90 
     91 #include <ufs/lfs/ulfs_inode.h>
     92 #include <ufs/lfs/ulfsmount.h>
     93 #include <ufs/lfs/ulfs_bswap.h>
     94 #include <ufs/lfs/ulfs_extern.h>
     95 
     96 #include <uvm/uvm.h>
     97 #include <uvm/uvm_pmap.h>
     98 #include <uvm/uvm_stat.h>
     99 #include <uvm/uvm_pager.h>
    100 
    101 #include <ufs/lfs/lfs.h>
    102 #include <ufs/lfs/lfs_kernel.h>
    103 #include <ufs/lfs/lfs_extern.h>
    104 
    105 extern pid_t lfs_writer_daemon;
    106 int lfs_ignore_lazy_sync = 1;
    107 
    108 static int lfs_openextattr(void *v);
    109 static int lfs_closeextattr(void *v);
    110 static int lfs_getextattr(void *v);
    111 static int lfs_setextattr(void *v);
    112 static int lfs_listextattr(void *v);
    113 static int lfs_deleteextattr(void *v);
    114 
    115 /* Global vfs data structures for lfs. */
    116 int (**lfs_vnodeop_p)(void *);
    117 const struct vnodeopv_entry_desc lfs_vnodeop_entries[] = {
    118 	{ &vop_default_desc, vn_default_error },
    119 	{ &vop_lookup_desc, ulfs_lookup },		/* lookup */
    120 	{ &vop_create_desc, lfs_create },		/* create */
    121 	{ &vop_whiteout_desc, ulfs_whiteout },		/* whiteout */
    122 	{ &vop_mknod_desc, lfs_mknod },			/* mknod */
    123 	{ &vop_open_desc, ulfs_open },			/* open */
    124 	{ &vop_close_desc, lfs_close },			/* close */
    125 	{ &vop_access_desc, ulfs_access },		/* access */
    126 	{ &vop_getattr_desc, lfs_getattr },		/* getattr */
    127 	{ &vop_setattr_desc, lfs_setattr },		/* setattr */
    128 	{ &vop_read_desc, lfs_read },			/* read */
    129 	{ &vop_write_desc, lfs_write },			/* write */
    130 	{ &vop_ioctl_desc, ulfs_ioctl },		/* ioctl */
    131 	{ &vop_fcntl_desc, lfs_fcntl },			/* fcntl */
    132 	{ &vop_poll_desc, ulfs_poll },			/* poll */
    133 	{ &vop_kqfilter_desc, genfs_kqfilter },		/* kqfilter */
    134 	{ &vop_revoke_desc, ulfs_revoke },		/* revoke */
    135 	{ &vop_mmap_desc, lfs_mmap },			/* mmap */
    136 	{ &vop_fsync_desc, lfs_fsync },			/* fsync */
    137 	{ &vop_seek_desc, ulfs_seek },			/* seek */
    138 	{ &vop_remove_desc, lfs_remove },		/* remove */
    139 	{ &vop_link_desc, lfs_link },			/* link */
    140 	{ &vop_rename_desc, lfs_rename },		/* rename */
    141 	{ &vop_mkdir_desc, lfs_mkdir },			/* mkdir */
    142 	{ &vop_rmdir_desc, lfs_rmdir },			/* rmdir */
    143 	{ &vop_symlink_desc, lfs_symlink },		/* symlink */
    144 	{ &vop_readdir_desc, ulfs_readdir },		/* readdir */
    145 	{ &vop_readlink_desc, ulfs_readlink },		/* readlink */
    146 	{ &vop_abortop_desc, ulfs_abortop },		/* abortop */
    147 	{ &vop_inactive_desc, lfs_inactive },		/* inactive */
    148 	{ &vop_reclaim_desc, lfs_reclaim },		/* reclaim */
    149 	{ &vop_lock_desc, ulfs_lock },			/* lock */
    150 	{ &vop_unlock_desc, ulfs_unlock },		/* unlock */
    151 	{ &vop_bmap_desc, ulfs_bmap },			/* bmap */
    152 	{ &vop_strategy_desc, lfs_strategy },		/* strategy */
    153 	{ &vop_print_desc, ulfs_print },		/* print */
    154 	{ &vop_islocked_desc, ulfs_islocked },		/* islocked */
    155 	{ &vop_pathconf_desc, ulfs_pathconf },		/* pathconf */
    156 	{ &vop_advlock_desc, ulfs_advlock },		/* advlock */
    157 	{ &vop_bwrite_desc, lfs_bwrite },		/* bwrite */
    158 	{ &vop_getpages_desc, lfs_getpages },		/* getpages */
    159 	{ &vop_putpages_desc, lfs_putpages },		/* putpages */
    160 	{ &vop_openextattr_desc, lfs_openextattr },	/* openextattr */
    161 	{ &vop_closeextattr_desc, lfs_closeextattr },	/* closeextattr */
    162 	{ &vop_getextattr_desc, lfs_getextattr },	/* getextattr */
    163 	{ &vop_setextattr_desc, lfs_setextattr },	/* setextattr */
    164 	{ &vop_listextattr_desc, lfs_listextattr },	/* listextattr */
    165 	{ &vop_deleteextattr_desc, lfs_deleteextattr },	/* deleteextattr */
    166 	{ NULL, NULL }
    167 };
    168 const struct vnodeopv_desc lfs_vnodeop_opv_desc =
    169 	{ &lfs_vnodeop_p, lfs_vnodeop_entries };
    170 
    171 int (**lfs_specop_p)(void *);
    172 const struct vnodeopv_entry_desc lfs_specop_entries[] = {
    173 	{ &vop_default_desc, vn_default_error },
    174 	{ &vop_lookup_desc, spec_lookup },		/* lookup */
    175 	{ &vop_create_desc, spec_create },		/* create */
    176 	{ &vop_mknod_desc, spec_mknod },		/* mknod */
    177 	{ &vop_open_desc, spec_open },			/* open */
    178 	{ &vop_close_desc, lfsspec_close },		/* close */
    179 	{ &vop_access_desc, ulfs_access },		/* access */
    180 	{ &vop_getattr_desc, lfs_getattr },		/* getattr */
    181 	{ &vop_setattr_desc, lfs_setattr },		/* setattr */
    182 	{ &vop_read_desc, ulfsspec_read },		/* read */
    183 	{ &vop_write_desc, ulfsspec_write },		/* write */
    184 	{ &vop_ioctl_desc, spec_ioctl },		/* ioctl */
    185 	{ &vop_fcntl_desc, ulfs_fcntl },		/* fcntl */
    186 	{ &vop_poll_desc, spec_poll },			/* poll */
    187 	{ &vop_kqfilter_desc, spec_kqfilter },		/* kqfilter */
    188 	{ &vop_revoke_desc, spec_revoke },		/* revoke */
    189 	{ &vop_mmap_desc, spec_mmap },			/* mmap */
    190 	{ &vop_fsync_desc, spec_fsync },		/* fsync */
    191 	{ &vop_seek_desc, spec_seek },			/* seek */
    192 	{ &vop_remove_desc, spec_remove },		/* remove */
    193 	{ &vop_link_desc, spec_link },			/* link */
    194 	{ &vop_rename_desc, spec_rename },		/* rename */
    195 	{ &vop_mkdir_desc, spec_mkdir },		/* mkdir */
    196 	{ &vop_rmdir_desc, spec_rmdir },		/* rmdir */
    197 	{ &vop_symlink_desc, spec_symlink },		/* symlink */
    198 	{ &vop_readdir_desc, spec_readdir },		/* readdir */
    199 	{ &vop_readlink_desc, spec_readlink },		/* readlink */
    200 	{ &vop_abortop_desc, spec_abortop },		/* abortop */
    201 	{ &vop_inactive_desc, lfs_inactive },		/* inactive */
    202 	{ &vop_reclaim_desc, lfs_reclaim },		/* reclaim */
    203 	{ &vop_lock_desc, ulfs_lock },			/* lock */
    204 	{ &vop_unlock_desc, ulfs_unlock },		/* unlock */
    205 	{ &vop_bmap_desc, spec_bmap },			/* bmap */
    206 	{ &vop_strategy_desc, spec_strategy },		/* strategy */
    207 	{ &vop_print_desc, ulfs_print },		/* print */
    208 	{ &vop_islocked_desc, ulfs_islocked },		/* islocked */
    209 	{ &vop_pathconf_desc, spec_pathconf },		/* pathconf */
    210 	{ &vop_advlock_desc, spec_advlock },		/* advlock */
    211 	{ &vop_bwrite_desc, vn_bwrite },		/* bwrite */
    212 	{ &vop_getpages_desc, spec_getpages },		/* getpages */
    213 	{ &vop_putpages_desc, spec_putpages },		/* putpages */
    214 	{ &vop_openextattr_desc, lfs_openextattr },	/* openextattr */
    215 	{ &vop_closeextattr_desc, lfs_closeextattr },	/* closeextattr */
    216 	{ &vop_getextattr_desc, lfs_getextattr },	/* getextattr */
    217 	{ &vop_setextattr_desc, lfs_setextattr },	/* setextattr */
    218 	{ &vop_listextattr_desc, lfs_listextattr },	/* listextattr */
    219 	{ &vop_deleteextattr_desc, lfs_deleteextattr },	/* deleteextattr */
    220 	{ NULL, NULL }
    221 };
    222 const struct vnodeopv_desc lfs_specop_opv_desc =
    223 	{ &lfs_specop_p, lfs_specop_entries };
    224 
    225 int (**lfs_fifoop_p)(void *);
    226 const struct vnodeopv_entry_desc lfs_fifoop_entries[] = {
    227 	{ &vop_default_desc, vn_default_error },
    228 	{ &vop_lookup_desc, vn_fifo_bypass },		/* lookup */
    229 	{ &vop_create_desc, vn_fifo_bypass },		/* create */
    230 	{ &vop_mknod_desc, vn_fifo_bypass },		/* mknod */
    231 	{ &vop_open_desc, vn_fifo_bypass },		/* open */
    232 	{ &vop_close_desc, lfsfifo_close },		/* close */
    233 	{ &vop_access_desc, ulfs_access },		/* access */
    234 	{ &vop_getattr_desc, lfs_getattr },		/* getattr */
    235 	{ &vop_setattr_desc, lfs_setattr },		/* setattr */
    236 	{ &vop_read_desc, ulfsfifo_read },		/* read */
    237 	{ &vop_write_desc, ulfsfifo_write },		/* write */
    238 	{ &vop_ioctl_desc, vn_fifo_bypass },		/* ioctl */
    239 	{ &vop_fcntl_desc, ulfs_fcntl },		/* fcntl */
    240 	{ &vop_poll_desc, vn_fifo_bypass },		/* poll */
    241 	{ &vop_kqfilter_desc, vn_fifo_bypass },		/* kqfilter */
    242 	{ &vop_revoke_desc, vn_fifo_bypass },		/* revoke */
    243 	{ &vop_mmap_desc, vn_fifo_bypass },		/* mmap */
    244 	{ &vop_fsync_desc, vn_fifo_bypass },		/* fsync */
    245 	{ &vop_seek_desc, vn_fifo_bypass },		/* seek */
    246 	{ &vop_remove_desc, vn_fifo_bypass },		/* remove */
    247 	{ &vop_link_desc, vn_fifo_bypass },		/* link */
    248 	{ &vop_rename_desc, vn_fifo_bypass },		/* rename */
    249 	{ &vop_mkdir_desc, vn_fifo_bypass },		/* mkdir */
    250 	{ &vop_rmdir_desc, vn_fifo_bypass },		/* rmdir */
    251 	{ &vop_symlink_desc, vn_fifo_bypass },		/* symlink */
    252 	{ &vop_readdir_desc, vn_fifo_bypass },		/* readdir */
    253 	{ &vop_readlink_desc, vn_fifo_bypass },		/* readlink */
    254 	{ &vop_abortop_desc, vn_fifo_bypass },		/* abortop */
    255 	{ &vop_inactive_desc, lfs_inactive },		/* inactive */
    256 	{ &vop_reclaim_desc, lfs_reclaim },		/* reclaim */
    257 	{ &vop_lock_desc, ulfs_lock },			/* lock */
    258 	{ &vop_unlock_desc, ulfs_unlock },		/* unlock */
    259 	{ &vop_bmap_desc, vn_fifo_bypass },		/* bmap */
    260 	{ &vop_strategy_desc, vn_fifo_bypass },		/* strategy */
    261 	{ &vop_print_desc, ulfs_print },		/* print */
    262 	{ &vop_islocked_desc, ulfs_islocked },		/* islocked */
    263 	{ &vop_pathconf_desc, vn_fifo_bypass },		/* pathconf */
    264 	{ &vop_advlock_desc, vn_fifo_bypass },		/* advlock */
    265 	{ &vop_bwrite_desc, lfs_bwrite },		/* bwrite */
    266 	{ &vop_putpages_desc, vn_fifo_bypass },		/* putpages */
    267 	{ &vop_openextattr_desc, lfs_openextattr },	/* openextattr */
    268 	{ &vop_closeextattr_desc, lfs_closeextattr },	/* closeextattr */
    269 	{ &vop_getextattr_desc, lfs_getextattr },	/* getextattr */
    270 	{ &vop_setextattr_desc, lfs_setextattr },	/* setextattr */
    271 	{ &vop_listextattr_desc, lfs_listextattr },	/* listextattr */
    272 	{ &vop_deleteextattr_desc, lfs_deleteextattr },	/* deleteextattr */
    273 	{ NULL, NULL }
    274 };
    275 const struct vnodeopv_desc lfs_fifoop_opv_desc =
    276 	{ &lfs_fifoop_p, lfs_fifoop_entries };
    277 
    278 static int check_dirty(struct lfs *, struct vnode *, off_t, off_t, off_t, int, int, struct vm_page **);
    279 
    280 #define	LFS_READWRITE
    281 #include <ufs/lfs/ulfs_readwrite.c>
    282 #undef	LFS_READWRITE
    283 
    284 /*
    285  * Synch an open file.
    286  */
    287 /* ARGSUSED */
    288 int
    289 lfs_fsync(void *v)
    290 {
    291 	struct vop_fsync_args /* {
    292 		struct vnode *a_vp;
    293 		kauth_cred_t a_cred;
    294 		int a_flags;
    295 		off_t offlo;
    296 		off_t offhi;
    297 	} */ *ap = v;
    298 	struct vnode *vp = ap->a_vp;
    299 	int error, wait;
    300 	struct inode *ip = VTOI(vp);
    301 	struct lfs *fs = ip->i_lfs;
    302 
    303 	/* If we're mounted read-only, don't try to sync. */
    304 	if (fs->lfs_ronly)
    305 		return 0;
    306 
    307 	/* If a removed vnode is being cleaned, no need to sync here. */
    308 	if ((ap->a_flags & FSYNC_RECLAIM) != 0 && ip->i_mode == 0)
    309 		return 0;
    310 
    311 	/*
    312 	 * Trickle sync simply adds this vnode to the pager list, as if
    313 	 * the pagedaemon had requested a pageout.
    314 	 */
    315 	if (ap->a_flags & FSYNC_LAZY) {
    316 		if (lfs_ignore_lazy_sync == 0) {
    317 			mutex_enter(&lfs_lock);
    318 			if (!(ip->i_flags & IN_PAGING)) {
    319 				ip->i_flags |= IN_PAGING;
    320 				TAILQ_INSERT_TAIL(&fs->lfs_pchainhd, ip,
    321 						  i_lfs_pchain);
    322 			}
    323 			wakeup(&lfs_writer_daemon);
    324 			mutex_exit(&lfs_lock);
    325 		}
    326 		return 0;
    327 	}
    328 
    329 	/*
    330 	 * If a vnode is bring cleaned, flush it out before we try to
    331 	 * reuse it.  This prevents the cleaner from writing files twice
    332 	 * in the same partial segment, causing an accounting underflow.
    333 	 */
    334 	if (ap->a_flags & FSYNC_RECLAIM && ip->i_flags & IN_CLEANING) {
    335 		lfs_vflush(vp);
    336 	}
    337 
    338 	wait = (ap->a_flags & FSYNC_WAIT);
    339 	do {
    340 		mutex_enter(vp->v_interlock);
    341 		error = VOP_PUTPAGES(vp, trunc_page(ap->a_offlo),
    342 				     round_page(ap->a_offhi),
    343 				     PGO_CLEANIT | (wait ? PGO_SYNCIO : 0));
    344 		if (error == EAGAIN) {
    345 			mutex_enter(&lfs_lock);
    346 			mtsleep(&fs->lfs_avail, PCATCH | PUSER, "lfs_fsync",
    347 				hz / 100 + 1, &lfs_lock);
    348 			mutex_exit(&lfs_lock);
    349 		}
    350 	} while (error == EAGAIN);
    351 	if (error)
    352 		return error;
    353 
    354 	if ((ap->a_flags & FSYNC_DATAONLY) == 0)
    355 		error = lfs_update(vp, NULL, NULL, wait ? UPDATE_WAIT : 0);
    356 
    357 	if (error == 0 && ap->a_flags & FSYNC_CACHE) {
    358 		int l = 0;
    359 		error = VOP_IOCTL(ip->i_devvp, DIOCCACHESYNC, &l, FWRITE,
    360 				  curlwp->l_cred);
    361 	}
    362 	if (wait && !VPISEMPTY(vp))
    363 		LFS_SET_UINO(ip, IN_MODIFIED);
    364 
    365 	return error;
    366 }
    367 
    368 /*
    369  * Take IN_ADIROP off, then call ulfs_inactive.
    370  */
    371 int
    372 lfs_inactive(void *v)
    373 {
    374 	struct vop_inactive_args /* {
    375 		struct vnode *a_vp;
    376 	} */ *ap = v;
    377 
    378 	lfs_unmark_vnode(ap->a_vp);
    379 
    380 	/*
    381 	 * The Ifile is only ever inactivated on unmount.
    382 	 * Streamline this process by not giving it more dirty blocks.
    383 	 */
    384 	if (VTOI(ap->a_vp)->i_number == LFS_IFILE_INUM) {
    385 		mutex_enter(&lfs_lock);
    386 		LFS_CLR_UINO(VTOI(ap->a_vp), IN_ALLMOD);
    387 		mutex_exit(&lfs_lock);
    388 		VOP_UNLOCK(ap->a_vp);
    389 		return 0;
    390 	}
    391 
    392 #ifdef DEBUG
    393 	/*
    394 	 * This might happen on unmount.
    395 	 * XXX If it happens at any other time, it should be a panic.
    396 	 */
    397 	if (ap->a_vp->v_uflag & VU_DIROP) {
    398 		struct inode *ip = VTOI(ap->a_vp);
    399 		printf("lfs_inactive: inactivating VU_DIROP? ino = %d\n", (int)ip->i_number);
    400 	}
    401 #endif /* DIAGNOSTIC */
    402 
    403 	return ulfs_inactive(v);
    404 }
    405 
    406 int
    407 lfs_set_dirop(struct vnode *dvp, struct vnode *vp)
    408 {
    409 	struct lfs *fs;
    410 	int error;
    411 
    412 	KASSERT(VOP_ISLOCKED(dvp));
    413 	KASSERT(vp == NULL || VOP_ISLOCKED(vp));
    414 
    415 	fs = VTOI(dvp)->i_lfs;
    416 
    417 	ASSERT_NO_SEGLOCK(fs);
    418 	/*
    419 	 * LFS_NRESERVE calculates direct and indirect blocks as well
    420 	 * as an inode block; an overestimate in most cases.
    421 	 */
    422 	if ((error = lfs_reserve(fs, dvp, vp, LFS_NRESERVE(fs))) != 0)
    423 		return (error);
    424 
    425     restart:
    426 	mutex_enter(&lfs_lock);
    427 	if (fs->lfs_dirops == 0) {
    428 		mutex_exit(&lfs_lock);
    429 		lfs_check(dvp, LFS_UNUSED_LBN, 0);
    430 		mutex_enter(&lfs_lock);
    431 	}
    432 	while (fs->lfs_writer) {
    433 		error = mtsleep(&fs->lfs_dirops, (PRIBIO + 1) | PCATCH,
    434 		    "lfs_sdirop", 0, &lfs_lock);
    435 		if (error == EINTR) {
    436 			mutex_exit(&lfs_lock);
    437 			goto unreserve;
    438 		}
    439 	}
    440 	if (lfs_dirvcount > LFS_MAX_DIROP && fs->lfs_dirops == 0) {
    441 		wakeup(&lfs_writer_daemon);
    442 		mutex_exit(&lfs_lock);
    443 		preempt();
    444 		goto restart;
    445 	}
    446 
    447 	if (lfs_dirvcount > LFS_MAX_DIROP) {
    448 		DLOG((DLOG_DIROP, "lfs_set_dirop: sleeping with dirops=%d, "
    449 		      "dirvcount=%d\n", fs->lfs_dirops, lfs_dirvcount));
    450 		if ((error = mtsleep(&lfs_dirvcount,
    451 		    PCATCH | PUSER | PNORELOCK, "lfs_maxdirop", 0,
    452 		    &lfs_lock)) != 0) {
    453 			goto unreserve;
    454 		}
    455 		goto restart;
    456 	}
    457 
    458 	++fs->lfs_dirops;
    459 	/* fs->lfs_doifile = 1; */ /* XXX why? --ks */
    460 	mutex_exit(&lfs_lock);
    461 
    462 	/* Hold a reference so SET_ENDOP will be happy */
    463 	vref(dvp);
    464 	if (vp) {
    465 		vref(vp);
    466 		MARK_VNODE(vp);
    467 	}
    468 
    469 	MARK_VNODE(dvp);
    470 	return 0;
    471 
    472   unreserve:
    473 	lfs_reserve(fs, dvp, vp, -LFS_NRESERVE(fs));
    474 	return error;
    475 }
    476 
    477 /*
    478  * Get a new vnode *before* adjusting the dirop count, to avoid a deadlock
    479  * in getnewvnode(), if we have a stacked filesystem mounted on top
    480  * of us.
    481  *
    482  * NB: this means we have to clear the new vnodes on error.  Fortunately
    483  * SET_ENDOP is there to do that for us.
    484  */
    485 int
    486 lfs_set_dirop_create(struct vnode *dvp, struct vnode **vpp)
    487 {
    488 	int error;
    489 	struct lfs *fs;
    490 
    491 	fs = VFSTOULFS(dvp->v_mount)->um_lfs;
    492 	ASSERT_NO_SEGLOCK(fs);
    493 	if (fs->lfs_ronly)
    494 		return EROFS;
    495 	if (vpp == NULL) {
    496 		return lfs_set_dirop(dvp, NULL);
    497 	}
    498 	error = getnewvnode(VT_LFS, dvp->v_mount, lfs_vnodeop_p, NULL, vpp);
    499 	if (error) {
    500 		DLOG((DLOG_ALLOC, "lfs_set_dirop_create: dvp %p error %d\n",
    501 		      dvp, error));
    502 		return error;
    503 	}
    504 	if ((error = lfs_set_dirop(dvp, NULL)) != 0) {
    505 		ungetnewvnode(*vpp);
    506 		*vpp = NULL;
    507 		return error;
    508 	}
    509 	return 0;
    510 }
    511 
    512 void
    513 lfs_mark_vnode(struct vnode *vp)
    514 {
    515 	struct inode *ip = VTOI(vp);
    516 	struct lfs *fs = ip->i_lfs;
    517 
    518 	mutex_enter(&lfs_lock);
    519 	if (!(ip->i_flag & IN_ADIROP)) {
    520 		if (!(vp->v_uflag & VU_DIROP)) {
    521 			mutex_exit(&lfs_lock);
    522 			mutex_enter(vp->v_interlock);
    523 			if (lfs_vref(vp) != 0)
    524 				panic("lfs_mark_vnode: could not vref");
    525 			mutex_enter(&lfs_lock);
    526 			++lfs_dirvcount;
    527 			++fs->lfs_dirvcount;
    528 			TAILQ_INSERT_TAIL(&fs->lfs_dchainhd, ip, i_lfs_dchain);
    529 			vp->v_uflag |= VU_DIROP;
    530 		}
    531 		++fs->lfs_nadirop;
    532 		ip->i_flag &= ~IN_CDIROP;
    533 		ip->i_flag |= IN_ADIROP;
    534 	} else
    535 		KASSERT(vp->v_uflag & VU_DIROP);
    536 	mutex_exit(&lfs_lock);
    537 }
    538 
    539 void
    540 lfs_unmark_vnode(struct vnode *vp)
    541 {
    542 	struct inode *ip = VTOI(vp);
    543 
    544 	mutex_enter(&lfs_lock);
    545 	if (ip && (ip->i_flag & IN_ADIROP)) {
    546 		KASSERT(vp->v_uflag & VU_DIROP);
    547 		--ip->i_lfs->lfs_nadirop;
    548 		ip->i_flag &= ~IN_ADIROP;
    549 	}
    550 	mutex_exit(&lfs_lock);
    551 }
    552 
    553 int
    554 lfs_symlink(void *v)
    555 {
    556 	struct vop_symlink_args /* {
    557 		struct vnode *a_dvp;
    558 		struct vnode **a_vpp;
    559 		struct componentname *a_cnp;
    560 		struct vattr *a_vap;
    561 		char *a_target;
    562 	} */ *ap = v;
    563 	int error;
    564 
    565 	if ((error = SET_DIROP_CREATE(ap->a_dvp, ap->a_vpp)) != 0) {
    566 		vput(ap->a_dvp);
    567 		return error;
    568 	}
    569 	error = ulfs_symlink(ap);
    570 	SET_ENDOP_CREATE_AP(ap, "symlink");
    571 	return (error);
    572 }
    573 
    574 int
    575 lfs_mknod(void *v)
    576 {
    577 	struct vop_mknod_args	/* {
    578 		struct vnode *a_dvp;
    579 		struct vnode **a_vpp;
    580 		struct componentname *a_cnp;
    581 		struct vattr *a_vap;
    582 	} */ *ap = v;
    583 	struct vattr *vap;
    584 	struct vnode **vpp;
    585 	struct inode *ip;
    586 	int error;
    587 	struct mount	*mp;
    588 	ino_t		ino;
    589 	struct ulfs_lookup_results *ulr;
    590 
    591 	vap = ap->a_vap;
    592 	vpp = ap->a_vpp;
    593 
    594 	/* XXX should handle this material another way */
    595 	ulr = &VTOI(ap->a_dvp)->i_crap;
    596 	ULFS_CHECK_CRAPCOUNTER(VTOI(ap->a_dvp));
    597 
    598 	if ((error = SET_DIROP_CREATE(ap->a_dvp, ap->a_vpp)) != 0) {
    599 		vput(ap->a_dvp);
    600 		return error;
    601 	}
    602 
    603 	fstrans_start(ap->a_dvp->v_mount, FSTRANS_SHARED);
    604 	error = ulfs_makeinode(MAKEIMODE(vap->va_type, vap->va_mode),
    605 			      ap->a_dvp, ulr, vpp, ap->a_cnp);
    606 
    607 	/* Either way we're done with the dirop at this point */
    608 	SET_ENDOP_CREATE_AP(ap, "mknod");
    609 
    610 	if (error) {
    611 		fstrans_done(ap->a_dvp->v_mount);
    612 		*vpp = NULL;
    613 		return (error);
    614 	}
    615 
    616 	VN_KNOTE(ap->a_dvp, NOTE_WRITE);
    617 	ip = VTOI(*vpp);
    618 	mp  = (*vpp)->v_mount;
    619 	ino = ip->i_number;
    620 	ip->i_flag |= IN_ACCESS | IN_CHANGE | IN_UPDATE;
    621 	if (vap->va_rdev != VNOVAL) {
    622 		struct ulfsmount *ump = ip->i_ump;
    623 		struct lfs *fs = ip->i_lfs;
    624 		/*
    625 		 * Want to be able to use this to make badblock
    626 		 * inodes, so don't truncate the dev number.
    627 		 */
    628 		if (ump->um_fstype == ULFS1)
    629 			ip->i_ffs1_rdev = ulfs_rw32(vap->va_rdev,
    630 			    ULFS_MPNEEDSWAP(fs));
    631 		else
    632 			ip->i_ffs2_rdev = ulfs_rw64(vap->va_rdev,
    633 			    ULFS_MPNEEDSWAP(fs));
    634 	}
    635 
    636 	/*
    637 	 * Call fsync to write the vnode so that we don't have to deal with
    638 	 * flushing it when it's marked VU_DIROP|VI_XLOCK.
    639 	 *
    640 	 * XXX KS - If we can't flush we also can't call vgone(), so must
    641 	 * return.  But, that leaves this vnode in limbo, also not good.
    642 	 * Can this ever happen (barring hardware failure)?
    643 	 */
    644 	if ((error = VOP_FSYNC(*vpp, NOCRED, FSYNC_WAIT, 0, 0)) != 0) {
    645 		panic("lfs_mknod: couldn't fsync (ino %llu)",
    646 		      (unsigned long long)ino);
    647 		/* return (error); */
    648 	}
    649 	/*
    650 	 * Remove vnode so that it will be reloaded by VFS_VGET and
    651 	 * checked to see if it is an alias of an existing entry in
    652 	 * the inode cache.
    653 	 */
    654 	/* Used to be vput, but that causes us to call VOP_INACTIVE twice. */
    655 
    656 	(*vpp)->v_type = VNON;
    657 	VOP_UNLOCK(*vpp);
    658 	vgone(*vpp);
    659 	error = VFS_VGET(mp, ino, vpp);
    660 
    661 	fstrans_done(ap->a_dvp->v_mount);
    662 	if (error != 0) {
    663 		*vpp = NULL;
    664 		return (error);
    665 	}
    666 	return (0);
    667 }
    668 
    669 int
    670 lfs_create(void *v)
    671 {
    672 	struct vop_create_args	/* {
    673 		struct vnode *a_dvp;
    674 		struct vnode **a_vpp;
    675 		struct componentname *a_cnp;
    676 		struct vattr *a_vap;
    677 	} */ *ap = v;
    678 	int error;
    679 
    680 	if ((error = SET_DIROP_CREATE(ap->a_dvp, ap->a_vpp)) != 0) {
    681 		vput(ap->a_dvp);
    682 		return error;
    683 	}
    684 	error = ulfs_create(ap);
    685 	SET_ENDOP_CREATE_AP(ap, "create");
    686 	return (error);
    687 }
    688 
    689 int
    690 lfs_mkdir(void *v)
    691 {
    692 	struct vop_mkdir_args	/* {
    693 		struct vnode *a_dvp;
    694 		struct vnode **a_vpp;
    695 		struct componentname *a_cnp;
    696 		struct vattr *a_vap;
    697 	} */ *ap = v;
    698 	int error;
    699 
    700 	if ((error = SET_DIROP_CREATE(ap->a_dvp, ap->a_vpp)) != 0) {
    701 		vput(ap->a_dvp);
    702 		return error;
    703 	}
    704 	error = ulfs_mkdir(ap);
    705 	SET_ENDOP_CREATE_AP(ap, "mkdir");
    706 	return (error);
    707 }
    708 
    709 int
    710 lfs_remove(void *v)
    711 {
    712 	struct vop_remove_args	/* {
    713 		struct vnode *a_dvp;
    714 		struct vnode *a_vp;
    715 		struct componentname *a_cnp;
    716 	} */ *ap = v;
    717 	struct vnode *dvp, *vp;
    718 	struct inode *ip;
    719 	int error;
    720 
    721 	dvp = ap->a_dvp;
    722 	vp = ap->a_vp;
    723 	ip = VTOI(vp);
    724 	if ((error = SET_DIROP_REMOVE(dvp, vp)) != 0) {
    725 		if (dvp == vp)
    726 			vrele(vp);
    727 		else
    728 			vput(vp);
    729 		vput(dvp);
    730 		return error;
    731 	}
    732 	error = ulfs_remove(ap);
    733 	if (ip->i_nlink == 0)
    734 		lfs_orphan(ip->i_lfs, ip->i_number);
    735 	SET_ENDOP_REMOVE(ip->i_lfs, dvp, ap->a_vp, "remove");
    736 	return (error);
    737 }
    738 
    739 int
    740 lfs_rmdir(void *v)
    741 {
    742 	struct vop_rmdir_args	/* {
    743 		struct vnodeop_desc *a_desc;
    744 		struct vnode *a_dvp;
    745 		struct vnode *a_vp;
    746 		struct componentname *a_cnp;
    747 	} */ *ap = v;
    748 	struct vnode *vp;
    749 	struct inode *ip;
    750 	int error;
    751 
    752 	vp = ap->a_vp;
    753 	ip = VTOI(vp);
    754 	if ((error = SET_DIROP_REMOVE(ap->a_dvp, ap->a_vp)) != 0) {
    755 		if (ap->a_dvp == vp)
    756 			vrele(ap->a_dvp);
    757 		else
    758 			vput(ap->a_dvp);
    759 		vput(vp);
    760 		return error;
    761 	}
    762 	error = ulfs_rmdir(ap);
    763 	if (ip->i_nlink == 0)
    764 		lfs_orphan(ip->i_lfs, ip->i_number);
    765 	SET_ENDOP_REMOVE(ip->i_lfs, ap->a_dvp, ap->a_vp, "rmdir");
    766 	return (error);
    767 }
    768 
    769 int
    770 lfs_link(void *v)
    771 {
    772 	struct vop_link_args	/* {
    773 		struct vnode *a_dvp;
    774 		struct vnode *a_vp;
    775 		struct componentname *a_cnp;
    776 	} */ *ap = v;
    777 	int error;
    778 	struct vnode **vpp = NULL;
    779 
    780 	if ((error = SET_DIROP_CREATE(ap->a_dvp, vpp)) != 0) {
    781 		vput(ap->a_dvp);
    782 		return error;
    783 	}
    784 	error = ulfs_link(ap);
    785 	SET_ENDOP_CREATE(VTOI(ap->a_dvp)->i_lfs, ap->a_dvp, vpp, "link");
    786 	return (error);
    787 }
    788 
    789 /* XXX hack to avoid calling ITIMES in getattr */
    790 int
    791 lfs_getattr(void *v)
    792 {
    793 	struct vop_getattr_args /* {
    794 		struct vnode *a_vp;
    795 		struct vattr *a_vap;
    796 		kauth_cred_t a_cred;
    797 	} */ *ap = v;
    798 	struct vnode *vp = ap->a_vp;
    799 	struct inode *ip = VTOI(vp);
    800 	struct vattr *vap = ap->a_vap;
    801 	struct lfs *fs = ip->i_lfs;
    802 
    803 	fstrans_start(vp->v_mount, FSTRANS_SHARED);
    804 	/*
    805 	 * Copy from inode table
    806 	 */
    807 	vap->va_fsid = ip->i_dev;
    808 	vap->va_fileid = ip->i_number;
    809 	vap->va_mode = ip->i_mode & ~LFS_IFMT;
    810 	vap->va_nlink = ip->i_nlink;
    811 	vap->va_uid = ip->i_uid;
    812 	vap->va_gid = ip->i_gid;
    813 	vap->va_rdev = (dev_t)ip->i_ffs1_rdev;
    814 	vap->va_size = vp->v_size;
    815 	vap->va_atime.tv_sec = ip->i_ffs1_atime;
    816 	vap->va_atime.tv_nsec = ip->i_ffs1_atimensec;
    817 	vap->va_mtime.tv_sec = ip->i_ffs1_mtime;
    818 	vap->va_mtime.tv_nsec = ip->i_ffs1_mtimensec;
    819 	vap->va_ctime.tv_sec = ip->i_ffs1_ctime;
    820 	vap->va_ctime.tv_nsec = ip->i_ffs1_ctimensec;
    821 	vap->va_flags = ip->i_flags;
    822 	vap->va_gen = ip->i_gen;
    823 	/* this doesn't belong here */
    824 	if (vp->v_type == VBLK)
    825 		vap->va_blocksize = BLKDEV_IOSIZE;
    826 	else if (vp->v_type == VCHR)
    827 		vap->va_blocksize = MAXBSIZE;
    828 	else
    829 		vap->va_blocksize = vp->v_mount->mnt_stat.f_iosize;
    830 	vap->va_bytes = lfs_fsbtob(fs, (u_quad_t)ip->i_lfs_effnblks);
    831 	vap->va_type = vp->v_type;
    832 	vap->va_filerev = ip->i_modrev;
    833 	fstrans_done(vp->v_mount);
    834 	return (0);
    835 }
    836 
    837 /*
    838  * Check to make sure the inode blocks won't choke the buffer
    839  * cache, then call ulfs_setattr as usual.
    840  */
    841 int
    842 lfs_setattr(void *v)
    843 {
    844 	struct vop_setattr_args /* {
    845 		struct vnode *a_vp;
    846 		struct vattr *a_vap;
    847 		kauth_cred_t a_cred;
    848 	} */ *ap = v;
    849 	struct vnode *vp = ap->a_vp;
    850 
    851 	lfs_check(vp, LFS_UNUSED_LBN, 0);
    852 	return ulfs_setattr(v);
    853 }
    854 
    855 /*
    856  * Release the block we hold on lfs_newseg wrapping.  Called on file close,
    857  * or explicitly from LFCNWRAPGO.  Called with the interlock held.
    858  */
    859 static int
    860 lfs_wrapgo(struct lfs *fs, struct inode *ip, int waitfor)
    861 {
    862 	if (fs->lfs_stoplwp != curlwp)
    863 		return EBUSY;
    864 
    865 	fs->lfs_stoplwp = NULL;
    866 	cv_signal(&fs->lfs_stopcv);
    867 
    868 	KASSERT(fs->lfs_nowrap > 0);
    869 	if (fs->lfs_nowrap <= 0) {
    870 		return 0;
    871 	}
    872 
    873 	if (--fs->lfs_nowrap == 0) {
    874 		log(LOG_NOTICE, "%s: re-enabled log wrap\n", fs->lfs_fsmnt);
    875 		wakeup(&fs->lfs_wrappass);
    876 		lfs_wakeup_cleaner(fs);
    877 	}
    878 	if (waitfor) {
    879 		mtsleep(&fs->lfs_nextseg, PCATCH | PUSER, "segment",
    880 		    0, &lfs_lock);
    881 	}
    882 
    883 	return 0;
    884 }
    885 
    886 /*
    887  * Close called.
    888  *
    889  * Update the times on the inode.
    890  */
    891 /* ARGSUSED */
    892 int
    893 lfs_close(void *v)
    894 {
    895 	struct vop_close_args /* {
    896 		struct vnode *a_vp;
    897 		int  a_fflag;
    898 		kauth_cred_t a_cred;
    899 	} */ *ap = v;
    900 	struct vnode *vp = ap->a_vp;
    901 	struct inode *ip = VTOI(vp);
    902 	struct lfs *fs = ip->i_lfs;
    903 
    904 	if ((ip->i_number == ULFS_ROOTINO || ip->i_number == LFS_IFILE_INUM) &&
    905 	    fs->lfs_stoplwp == curlwp) {
    906 		mutex_enter(&lfs_lock);
    907 		log(LOG_NOTICE, "lfs_close: releasing log wrap control\n");
    908 		lfs_wrapgo(fs, ip, 0);
    909 		mutex_exit(&lfs_lock);
    910 	}
    911 
    912 	if (vp == ip->i_lfs->lfs_ivnode &&
    913 	    vp->v_mount->mnt_iflag & IMNT_UNMOUNT)
    914 		return 0;
    915 
    916 	fstrans_start(vp->v_mount, FSTRANS_SHARED);
    917 	if (vp->v_usecount > 1 && vp != ip->i_lfs->lfs_ivnode) {
    918 		LFS_ITIMES(ip, NULL, NULL, NULL);
    919 	}
    920 	fstrans_done(vp->v_mount);
    921 	return (0);
    922 }
    923 
    924 /*
    925  * Close wrapper for special devices.
    926  *
    927  * Update the times on the inode then do device close.
    928  */
    929 int
    930 lfsspec_close(void *v)
    931 {
    932 	struct vop_close_args /* {
    933 		struct vnode	*a_vp;
    934 		int		a_fflag;
    935 		kauth_cred_t	a_cred;
    936 	} */ *ap = v;
    937 	struct vnode	*vp;
    938 	struct inode	*ip;
    939 
    940 	vp = ap->a_vp;
    941 	ip = VTOI(vp);
    942 	if (vp->v_usecount > 1) {
    943 		LFS_ITIMES(ip, NULL, NULL, NULL);
    944 	}
    945 	return (VOCALL (spec_vnodeop_p, VOFFSET(vop_close), ap));
    946 }
    947 
    948 /*
    949  * Close wrapper for fifo's.
    950  *
    951  * Update the times on the inode then do device close.
    952  */
    953 int
    954 lfsfifo_close(void *v)
    955 {
    956 	struct vop_close_args /* {
    957 		struct vnode	*a_vp;
    958 		int		a_fflag;
    959 		kauth_cred_	a_cred;
    960 	} */ *ap = v;
    961 	struct vnode	*vp;
    962 	struct inode	*ip;
    963 
    964 	vp = ap->a_vp;
    965 	ip = VTOI(vp);
    966 	if (ap->a_vp->v_usecount > 1) {
    967 		LFS_ITIMES(ip, NULL, NULL, NULL);
    968 	}
    969 	return (VOCALL (fifo_vnodeop_p, VOFFSET(vop_close), ap));
    970 }
    971 
    972 /*
    973  * Reclaim an inode so that it can be used for other purposes.
    974  */
    975 
    976 int
    977 lfs_reclaim(void *v)
    978 {
    979 	struct vop_reclaim_args /* {
    980 		struct vnode *a_vp;
    981 	} */ *ap = v;
    982 	struct vnode *vp = ap->a_vp;
    983 	struct inode *ip = VTOI(vp);
    984 	struct lfs *fs = ip->i_lfs;
    985 	int error;
    986 
    987 	/*
    988 	 * The inode must be freed and updated before being removed
    989 	 * from its hash chain.  Other threads trying to gain a hold
    990 	 * on the inode will be stalled because it is locked (VI_XLOCK).
    991 	 */
    992 	if (ip->i_nlink <= 0 && (vp->v_mount->mnt_flag & MNT_RDONLY) == 0)
    993 		lfs_vfree(vp, ip->i_number, ip->i_omode);
    994 
    995 	mutex_enter(&lfs_lock);
    996 	LFS_CLR_UINO(ip, IN_ALLMOD);
    997 	mutex_exit(&lfs_lock);
    998 	if ((error = ulfs_reclaim(vp)))
    999 		return (error);
   1000 
   1001 	/*
   1002 	 * Take us off the paging and/or dirop queues if we were on them.
   1003 	 * We shouldn't be on them.
   1004 	 */
   1005 	mutex_enter(&lfs_lock);
   1006 	if (ip->i_flags & IN_PAGING) {
   1007 		log(LOG_WARNING, "%s: reclaimed vnode is IN_PAGING\n",
   1008 		    fs->lfs_fsmnt);
   1009 		ip->i_flags &= ~IN_PAGING;
   1010 		TAILQ_REMOVE(&fs->lfs_pchainhd, ip, i_lfs_pchain);
   1011 	}
   1012 	if (vp->v_uflag & VU_DIROP) {
   1013 		panic("reclaimed vnode is VU_DIROP");
   1014 		vp->v_uflag &= ~VU_DIROP;
   1015 		TAILQ_REMOVE(&fs->lfs_dchainhd, ip, i_lfs_dchain);
   1016 	}
   1017 	mutex_exit(&lfs_lock);
   1018 
   1019 	pool_put(&lfs_dinode_pool, ip->i_din.ffs1_din);
   1020 	lfs_deregister_all(vp);
   1021 	pool_put(&lfs_inoext_pool, ip->inode_ext.lfs);
   1022 	ip->inode_ext.lfs = NULL;
   1023 	genfs_node_destroy(vp);
   1024 	pool_put(&lfs_inode_pool, vp->v_data);
   1025 	vp->v_data = NULL;
   1026 	return (0);
   1027 }
   1028 
   1029 /*
   1030  * Read a block from a storage device.
   1031  *
   1032  * Calculate the logical to physical mapping if not done already,
   1033  * then call the device strategy routine.
   1034  *
   1035  * In order to avoid reading blocks that are in the process of being
   1036  * written by the cleaner---and hence are not mutexed by the normal
   1037  * buffer cache / page cache mechanisms---check for collisions before
   1038  * reading.
   1039  *
   1040  * We inline ulfs_strategy to make sure that the VOP_BMAP occurs *before*
   1041  * the active cleaner test.
   1042  *
   1043  * XXX This code assumes that lfs_markv makes synchronous checkpoints.
   1044  */
   1045 int
   1046 lfs_strategy(void *v)
   1047 {
   1048 	struct vop_strategy_args /* {
   1049 		struct vnode *a_vp;
   1050 		struct buf *a_bp;
   1051 	} */ *ap = v;
   1052 	struct buf	*bp;
   1053 	struct lfs	*fs;
   1054 	struct vnode	*vp;
   1055 	struct inode	*ip;
   1056 	daddr_t		tbn;
   1057 #define MAXLOOP 25
   1058 	int		i, sn, error, slept, loopcount;
   1059 
   1060 	bp = ap->a_bp;
   1061 	vp = ap->a_vp;
   1062 	ip = VTOI(vp);
   1063 	fs = ip->i_lfs;
   1064 
   1065 	/* lfs uses its strategy routine only for read */
   1066 	KASSERT(bp->b_flags & B_READ);
   1067 
   1068 	if (vp->v_type == VBLK || vp->v_type == VCHR)
   1069 		panic("lfs_strategy: spec");
   1070 	KASSERT(bp->b_bcount != 0);
   1071 	if (bp->b_blkno == bp->b_lblkno) {
   1072 		error = VOP_BMAP(vp, bp->b_lblkno, NULL, &bp->b_blkno,
   1073 				 NULL);
   1074 		if (error) {
   1075 			bp->b_error = error;
   1076 			bp->b_resid = bp->b_bcount;
   1077 			biodone(bp);
   1078 			return (error);
   1079 		}
   1080 		if ((long)bp->b_blkno == -1) /* no valid data */
   1081 			clrbuf(bp);
   1082 	}
   1083 	if ((long)bp->b_blkno < 0) { /* block is not on disk */
   1084 		bp->b_resid = bp->b_bcount;
   1085 		biodone(bp);
   1086 		return (0);
   1087 	}
   1088 
   1089 	slept = 1;
   1090 	loopcount = 0;
   1091 	mutex_enter(&lfs_lock);
   1092 	while (slept && fs->lfs_seglock) {
   1093 		mutex_exit(&lfs_lock);
   1094 		/*
   1095 		 * Look through list of intervals.
   1096 		 * There will only be intervals to look through
   1097 		 * if the cleaner holds the seglock.
   1098 		 * Since the cleaner is synchronous, we can trust
   1099 		 * the list of intervals to be current.
   1100 		 */
   1101 		tbn = LFS_DBTOFSB(fs, bp->b_blkno);
   1102 		sn = lfs_dtosn(fs, tbn);
   1103 		slept = 0;
   1104 		for (i = 0; i < fs->lfs_cleanind; i++) {
   1105 			if (sn == lfs_dtosn(fs, fs->lfs_cleanint[i]) &&
   1106 			    tbn >= fs->lfs_cleanint[i]) {
   1107 				DLOG((DLOG_CLEAN,
   1108 				      "lfs_strategy: ino %d lbn %" PRId64
   1109 				      " ind %d sn %d fsb %" PRIx32
   1110 				      " given sn %d fsb %" PRIx64 "\n",
   1111 				      ip->i_number, bp->b_lblkno, i,
   1112 				      lfs_dtosn(fs, fs->lfs_cleanint[i]),
   1113 				      fs->lfs_cleanint[i], sn, tbn));
   1114 				DLOG((DLOG_CLEAN,
   1115 				      "lfs_strategy: sleeping on ino %d lbn %"
   1116 				      PRId64 "\n", ip->i_number, bp->b_lblkno));
   1117 				mutex_enter(&lfs_lock);
   1118 				if (LFS_SEGLOCK_HELD(fs) && fs->lfs_iocount) {
   1119 					/*
   1120 					 * Cleaner can't wait for itself.
   1121 					 * Instead, wait for the blocks
   1122 					 * to be written to disk.
   1123 					 * XXX we need pribio in the test
   1124 					 * XXX here.
   1125 					 */
   1126  					mtsleep(&fs->lfs_iocount,
   1127  						(PRIBIO + 1) | PNORELOCK,
   1128 						"clean2", hz/10 + 1,
   1129  						&lfs_lock);
   1130 					slept = 1;
   1131 					++loopcount;
   1132 					break;
   1133 				} else if (fs->lfs_seglock) {
   1134 					mtsleep(&fs->lfs_seglock,
   1135 						(PRIBIO + 1) | PNORELOCK,
   1136 						"clean1", 0,
   1137 						&lfs_lock);
   1138 					slept = 1;
   1139 					break;
   1140 				}
   1141 				mutex_exit(&lfs_lock);
   1142 			}
   1143 		}
   1144 		mutex_enter(&lfs_lock);
   1145 		if (loopcount > MAXLOOP) {
   1146 			printf("lfs_strategy: breaking out of clean2 loop\n");
   1147 			break;
   1148 		}
   1149 	}
   1150 	mutex_exit(&lfs_lock);
   1151 
   1152 	vp = ip->i_devvp;
   1153 	return VOP_STRATEGY(vp, bp);
   1154 }
   1155 
   1156 /*
   1157  * Inline lfs_segwrite/lfs_writevnodes, but just for dirops.
   1158  * Technically this is a checkpoint (the on-disk state is valid)
   1159  * even though we are leaving out all the file data.
   1160  */
   1161 int
   1162 lfs_flush_dirops(struct lfs *fs)
   1163 {
   1164 	struct inode *ip, *nip;
   1165 	struct vnode *vp;
   1166 	extern int lfs_dostats;
   1167 	struct segment *sp;
   1168 	int flags = 0;
   1169 	int error = 0;
   1170 
   1171 	ASSERT_MAYBE_SEGLOCK(fs);
   1172 	KASSERT(fs->lfs_nadirop == 0);
   1173 
   1174 	if (fs->lfs_ronly)
   1175 		return EROFS;
   1176 
   1177 	mutex_enter(&lfs_lock);
   1178 	if (TAILQ_FIRST(&fs->lfs_dchainhd) == NULL) {
   1179 		mutex_exit(&lfs_lock);
   1180 		return 0;
   1181 	} else
   1182 		mutex_exit(&lfs_lock);
   1183 
   1184 	if (lfs_dostats)
   1185 		++lfs_stats.flush_invoked;
   1186 
   1187 	lfs_imtime(fs);
   1188 	lfs_seglock(fs, flags);
   1189 	sp = fs->lfs_sp;
   1190 
   1191 	/*
   1192 	 * lfs_writevnodes, optimized to get dirops out of the way.
   1193 	 * Only write dirops, and don't flush files' pages, only
   1194 	 * blocks from the directories.
   1195 	 *
   1196 	 * We don't need to vref these files because they are
   1197 	 * dirops and so hold an extra reference until the
   1198 	 * segunlock clears them of that status.
   1199 	 *
   1200 	 * We don't need to check for IN_ADIROP because we know that
   1201 	 * no dirops are active.
   1202 	 *
   1203 	 */
   1204 	mutex_enter(&lfs_lock);
   1205 	for (ip = TAILQ_FIRST(&fs->lfs_dchainhd); ip != NULL; ip = nip) {
   1206 		nip = TAILQ_NEXT(ip, i_lfs_dchain);
   1207 		mutex_exit(&lfs_lock);
   1208 		vp = ITOV(ip);
   1209 
   1210 		KASSERT((ip->i_flag & IN_ADIROP) == 0);
   1211 		KASSERT(vp->v_uflag & VU_DIROP);
   1212 		KASSERT(!(vp->v_iflag & VI_XLOCK));
   1213 
   1214 		/*
   1215 		 * All writes to directories come from dirops; all
   1216 		 * writes to files' direct blocks go through the page
   1217 		 * cache, which we're not touching.  Reads to files
   1218 		 * and/or directories will not be affected by writing
   1219 		 * directory blocks inodes and file inodes.  So we don't
   1220 		 * really need to lock.
   1221 		 */
   1222 		if (vp->v_iflag & VI_XLOCK) {
   1223 			mutex_enter(&lfs_lock);
   1224 			continue;
   1225 		}
   1226 		/* XXX see below
   1227 		 * waslocked = VOP_ISLOCKED(vp);
   1228 		 */
   1229 		if (vp->v_type != VREG &&
   1230 		    ((ip->i_flag & IN_ALLMOD) || !VPISEMPTY(vp))) {
   1231 			error = lfs_writefile(fs, sp, vp);
   1232 			if (!VPISEMPTY(vp) && !WRITEINPROG(vp) &&
   1233 			    !(ip->i_flag & IN_ALLMOD)) {
   1234 			    	mutex_enter(&lfs_lock);
   1235 				LFS_SET_UINO(ip, IN_MODIFIED);
   1236 			    	mutex_exit(&lfs_lock);
   1237 			}
   1238 			if (error && (sp->seg_flags & SEGM_SINGLE)) {
   1239 				mutex_enter(&lfs_lock);
   1240 				error = EAGAIN;
   1241 				break;
   1242 			}
   1243 		}
   1244 		KDASSERT(ip->i_number != LFS_IFILE_INUM);
   1245 		error = lfs_writeinode(fs, sp, ip);
   1246 		mutex_enter(&lfs_lock);
   1247 		if (error && (sp->seg_flags & SEGM_SINGLE)) {
   1248 			error = EAGAIN;
   1249 			break;
   1250 		}
   1251 
   1252 		/*
   1253 		 * We might need to update these inodes again,
   1254 		 * for example, if they have data blocks to write.
   1255 		 * Make sure that after this flush, they are still
   1256 		 * marked IN_MODIFIED so that we don't forget to
   1257 		 * write them.
   1258 		 */
   1259 		/* XXX only for non-directories? --KS */
   1260 		LFS_SET_UINO(ip, IN_MODIFIED);
   1261 	}
   1262 	mutex_exit(&lfs_lock);
   1263 	/* We've written all the dirops there are */
   1264 	((SEGSUM *)(sp->segsum))->ss_flags &= ~(SS_CONT);
   1265 	lfs_finalize_fs_seguse(fs);
   1266 	(void) lfs_writeseg(fs, sp);
   1267 	lfs_segunlock(fs);
   1268 
   1269 	return error;
   1270 }
   1271 
   1272 /*
   1273  * Flush all vnodes for which the pagedaemon has requested pageouts.
   1274  * Skip over any files that are marked VU_DIROP (since lfs_flush_dirop()
   1275  * has just run, this would be an error).  If we have to skip a vnode
   1276  * for any reason, just skip it; if we have to wait for the cleaner,
   1277  * abort.  The writer daemon will call us again later.
   1278  */
   1279 int
   1280 lfs_flush_pchain(struct lfs *fs)
   1281 {
   1282 	struct inode *ip, *nip;
   1283 	struct vnode *vp;
   1284 	extern int lfs_dostats;
   1285 	struct segment *sp;
   1286 	int error, error2;
   1287 
   1288 	ASSERT_NO_SEGLOCK(fs);
   1289 
   1290 	if (fs->lfs_ronly)
   1291 		return EROFS;
   1292 
   1293 	mutex_enter(&lfs_lock);
   1294 	if (TAILQ_FIRST(&fs->lfs_pchainhd) == NULL) {
   1295 		mutex_exit(&lfs_lock);
   1296 		return 0;
   1297 	} else
   1298 		mutex_exit(&lfs_lock);
   1299 
   1300 	/* Get dirops out of the way */
   1301 	if ((error = lfs_flush_dirops(fs)) != 0)
   1302 		return error;
   1303 
   1304 	if (lfs_dostats)
   1305 		++lfs_stats.flush_invoked;
   1306 
   1307 	/*
   1308 	 * Inline lfs_segwrite/lfs_writevnodes, but just for pageouts.
   1309 	 */
   1310 	lfs_imtime(fs);
   1311 	lfs_seglock(fs, 0);
   1312 	sp = fs->lfs_sp;
   1313 
   1314 	/*
   1315 	 * lfs_writevnodes, optimized to clear pageout requests.
   1316 	 * Only write non-dirop files that are in the pageout queue.
   1317 	 * We're very conservative about what we write; we want to be
   1318 	 * fast and async.
   1319 	 */
   1320 	mutex_enter(&lfs_lock);
   1321     top:
   1322 	for (ip = TAILQ_FIRST(&fs->lfs_pchainhd); ip != NULL; ip = nip) {
   1323 		nip = TAILQ_NEXT(ip, i_lfs_pchain);
   1324 		vp = ITOV(ip);
   1325 
   1326 		if (!(ip->i_flags & IN_PAGING))
   1327 			goto top;
   1328 
   1329 		mutex_enter(vp->v_interlock);
   1330 		if ((vp->v_iflag & VI_XLOCK) || (vp->v_uflag & VU_DIROP) != 0) {
   1331 			mutex_exit(vp->v_interlock);
   1332 			continue;
   1333 		}
   1334 		if (vp->v_type != VREG) {
   1335 			mutex_exit(vp->v_interlock);
   1336 			continue;
   1337 		}
   1338 		if (lfs_vref(vp))
   1339 			continue;
   1340 		mutex_exit(&lfs_lock);
   1341 
   1342 		if (vn_lock(vp, LK_EXCLUSIVE | LK_NOWAIT | LK_RETRY) != 0) {
   1343 			lfs_vunref(vp);
   1344 			mutex_enter(&lfs_lock);
   1345 			continue;
   1346 		}
   1347 
   1348 		error = lfs_writefile(fs, sp, vp);
   1349 		if (!VPISEMPTY(vp) && !WRITEINPROG(vp) &&
   1350 		    !(ip->i_flag & IN_ALLMOD)) {
   1351 		    	mutex_enter(&lfs_lock);
   1352 			LFS_SET_UINO(ip, IN_MODIFIED);
   1353 		    	mutex_exit(&lfs_lock);
   1354 		}
   1355 		KDASSERT(ip->i_number != LFS_IFILE_INUM);
   1356 		error2 = lfs_writeinode(fs, sp, ip);
   1357 
   1358 		VOP_UNLOCK(vp);
   1359 		lfs_vunref(vp);
   1360 
   1361 		if (error == EAGAIN || error2 == EAGAIN) {
   1362 			lfs_writeseg(fs, sp);
   1363 			mutex_enter(&lfs_lock);
   1364 			break;
   1365 		}
   1366 		mutex_enter(&lfs_lock);
   1367 	}
   1368 	mutex_exit(&lfs_lock);
   1369 	(void) lfs_writeseg(fs, sp);
   1370 	lfs_segunlock(fs);
   1371 
   1372 	return 0;
   1373 }
   1374 
   1375 /*
   1376  * Provide a fcntl interface to sys_lfs_{segwait,bmapv,markv}.
   1377  */
   1378 int
   1379 lfs_fcntl(void *v)
   1380 {
   1381 	struct vop_fcntl_args /* {
   1382 		struct vnode *a_vp;
   1383 		u_int a_command;
   1384 		void * a_data;
   1385 		int  a_fflag;
   1386 		kauth_cred_t a_cred;
   1387 	} */ *ap = v;
   1388 	struct timeval tv;
   1389 	struct timeval *tvp;
   1390 	BLOCK_INFO *blkiov;
   1391 	CLEANERINFO *cip;
   1392 	SEGUSE *sup;
   1393 	int blkcnt, error, oclean;
   1394 	size_t fh_size;
   1395 	struct lfs_fcntl_markv blkvp;
   1396 	struct lwp *l;
   1397 	fsid_t *fsidp;
   1398 	struct lfs *fs;
   1399 	struct buf *bp;
   1400 	fhandle_t *fhp;
   1401 	daddr_t off;
   1402 
   1403 	/* Only respect LFS fcntls on fs root or Ifile */
   1404 	if (VTOI(ap->a_vp)->i_number != ULFS_ROOTINO &&
   1405 	    VTOI(ap->a_vp)->i_number != LFS_IFILE_INUM) {
   1406 		return ulfs_fcntl(v);
   1407 	}
   1408 
   1409 	/* Avoid locking a draining lock */
   1410 	if (ap->a_vp->v_mount->mnt_iflag & IMNT_UNMOUNT) {
   1411 		return ESHUTDOWN;
   1412 	}
   1413 
   1414 	/* LFS control and monitoring fcntls are available only to root */
   1415 	l = curlwp;
   1416 	if (((ap->a_command & 0xff00) >> 8) == 'L' &&
   1417 	    (error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_LFS,
   1418 	     KAUTH_REQ_SYSTEM_LFS_FCNTL, NULL, NULL, NULL)) != 0)
   1419 		return (error);
   1420 
   1421 	fs = VTOI(ap->a_vp)->i_lfs;
   1422 	fsidp = &ap->a_vp->v_mount->mnt_stat.f_fsidx;
   1423 
   1424 	error = 0;
   1425 	switch ((int)ap->a_command) {
   1426 	    case LFCNSEGWAITALL_COMPAT_50:
   1427 	    case LFCNSEGWAITALL_COMPAT:
   1428 		fsidp = NULL;
   1429 		/* FALLSTHROUGH */
   1430 	    case LFCNSEGWAIT_COMPAT_50:
   1431 	    case LFCNSEGWAIT_COMPAT:
   1432 		{
   1433 			struct timeval50 *tvp50
   1434 				= (struct timeval50 *)ap->a_data;
   1435 			timeval50_to_timeval(tvp50, &tv);
   1436 			tvp = &tv;
   1437 		}
   1438 		goto segwait_common;
   1439 	    case LFCNSEGWAITALL:
   1440 		fsidp = NULL;
   1441 		/* FALLSTHROUGH */
   1442 	    case LFCNSEGWAIT:
   1443 		tvp = (struct timeval *)ap->a_data;
   1444 segwait_common:
   1445 		mutex_enter(&lfs_lock);
   1446 		++fs->lfs_sleepers;
   1447 		mutex_exit(&lfs_lock);
   1448 
   1449 		error = lfs_segwait(fsidp, tvp);
   1450 
   1451 		mutex_enter(&lfs_lock);
   1452 		if (--fs->lfs_sleepers == 0)
   1453 			wakeup(&fs->lfs_sleepers);
   1454 		mutex_exit(&lfs_lock);
   1455 		return error;
   1456 
   1457 	    case LFCNBMAPV:
   1458 	    case LFCNMARKV:
   1459 		blkvp = *(struct lfs_fcntl_markv *)ap->a_data;
   1460 
   1461 		blkcnt = blkvp.blkcnt;
   1462 		if ((u_int) blkcnt > LFS_MARKV_MAXBLKCNT)
   1463 			return (EINVAL);
   1464 		blkiov = lfs_malloc(fs, blkcnt * sizeof(BLOCK_INFO), LFS_NB_BLKIOV);
   1465 		if ((error = copyin(blkvp.blkiov, blkiov,
   1466 		     blkcnt * sizeof(BLOCK_INFO))) != 0) {
   1467 			lfs_free(fs, blkiov, LFS_NB_BLKIOV);
   1468 			return error;
   1469 		}
   1470 
   1471 		mutex_enter(&lfs_lock);
   1472 		++fs->lfs_sleepers;
   1473 		mutex_exit(&lfs_lock);
   1474 		if (ap->a_command == LFCNBMAPV)
   1475 			error = lfs_bmapv(l->l_proc, fsidp, blkiov, blkcnt);
   1476 		else /* LFCNMARKV */
   1477 			error = lfs_markv(l->l_proc, fsidp, blkiov, blkcnt);
   1478 		if (error == 0)
   1479 			error = copyout(blkiov, blkvp.blkiov,
   1480 					blkcnt * sizeof(BLOCK_INFO));
   1481 		mutex_enter(&lfs_lock);
   1482 		if (--fs->lfs_sleepers == 0)
   1483 			wakeup(&fs->lfs_sleepers);
   1484 		mutex_exit(&lfs_lock);
   1485 		lfs_free(fs, blkiov, LFS_NB_BLKIOV);
   1486 		return error;
   1487 
   1488 	    case LFCNRECLAIM:
   1489 		/*
   1490 		 * Flush dirops and write Ifile, allowing empty segments
   1491 		 * to be immediately reclaimed.
   1492 		 */
   1493 		lfs_writer_enter(fs, "pndirop");
   1494 		off = fs->lfs_offset;
   1495 		lfs_seglock(fs, SEGM_FORCE_CKP | SEGM_CKP);
   1496 		lfs_flush_dirops(fs);
   1497 		LFS_CLEANERINFO(cip, fs, bp);
   1498 		oclean = cip->clean;
   1499 		LFS_SYNC_CLEANERINFO(cip, fs, bp, 1);
   1500 		lfs_segwrite(ap->a_vp->v_mount, SEGM_FORCE_CKP);
   1501 		fs->lfs_sp->seg_flags |= SEGM_PROT;
   1502 		lfs_segunlock(fs);
   1503 		lfs_writer_leave(fs);
   1504 
   1505 #ifdef DEBUG
   1506 		LFS_CLEANERINFO(cip, fs, bp);
   1507 		DLOG((DLOG_CLEAN, "lfs_fcntl: reclaim wrote %" PRId64
   1508 		      " blocks, cleaned %" PRId32 " segments (activesb %d)\n",
   1509 		      fs->lfs_offset - off, cip->clean - oclean,
   1510 		      fs->lfs_activesb));
   1511 		LFS_SYNC_CLEANERINFO(cip, fs, bp, 0);
   1512 #endif
   1513 
   1514 		return 0;
   1515 
   1516 	    case LFCNIFILEFH_COMPAT:
   1517 		/* Return the filehandle of the Ifile */
   1518 		if ((error = kauth_authorize_system(l->l_cred,
   1519 		    KAUTH_SYSTEM_FILEHANDLE, 0, NULL, NULL, NULL)) != 0)
   1520 			return (error);
   1521 		fhp = (struct fhandle *)ap->a_data;
   1522 		fhp->fh_fsid = *fsidp;
   1523 		fh_size = 16;	/* former VFS_MAXFIDSIZ */
   1524 		return lfs_vptofh(fs->lfs_ivnode, &(fhp->fh_fid), &fh_size);
   1525 
   1526 	    case LFCNIFILEFH_COMPAT2:
   1527 	    case LFCNIFILEFH:
   1528 		/* Return the filehandle of the Ifile */
   1529 		fhp = (struct fhandle *)ap->a_data;
   1530 		fhp->fh_fsid = *fsidp;
   1531 		fh_size = sizeof(struct lfs_fhandle) -
   1532 		    offsetof(fhandle_t, fh_fid);
   1533 		return lfs_vptofh(fs->lfs_ivnode, &(fhp->fh_fid), &fh_size);
   1534 
   1535 	    case LFCNREWIND:
   1536 		/* Move lfs_offset to the lowest-numbered segment */
   1537 		return lfs_rewind(fs, *(int *)ap->a_data);
   1538 
   1539 	    case LFCNINVAL:
   1540 		/* Mark a segment SEGUSE_INVAL */
   1541 		LFS_SEGENTRY(sup, fs, *(int *)ap->a_data, bp);
   1542 		if (sup->su_nbytes > 0) {
   1543 			brelse(bp, 0);
   1544 			lfs_unset_inval_all(fs);
   1545 			return EBUSY;
   1546 		}
   1547 		sup->su_flags |= SEGUSE_INVAL;
   1548 		VOP_BWRITE(bp->b_vp, bp);
   1549 		return 0;
   1550 
   1551 	    case LFCNRESIZE:
   1552 		/* Resize the filesystem */
   1553 		return lfs_resize_fs(fs, *(int *)ap->a_data);
   1554 
   1555 	    case LFCNWRAPSTOP:
   1556 	    case LFCNWRAPSTOP_COMPAT:
   1557 		/*
   1558 		 * Hold lfs_newseg at segment 0; if requested, sleep until
   1559 		 * the filesystem wraps around.  To support external agents
   1560 		 * (dump, fsck-based regression test) that need to look at
   1561 		 * a snapshot of the filesystem, without necessarily
   1562 		 * requiring that all fs activity stops.
   1563 		 */
   1564 		if (fs->lfs_stoplwp == curlwp)
   1565 			return EALREADY;
   1566 
   1567 		mutex_enter(&lfs_lock);
   1568 		while (fs->lfs_stoplwp != NULL)
   1569 			cv_wait(&fs->lfs_stopcv, &lfs_lock);
   1570 		fs->lfs_stoplwp = curlwp;
   1571 		if (fs->lfs_nowrap == 0)
   1572 			log(LOG_NOTICE, "%s: disabled log wrap\n", fs->lfs_fsmnt);
   1573 		++fs->lfs_nowrap;
   1574 		if (*(int *)ap->a_data == 1
   1575 		    || ap->a_command == LFCNWRAPSTOP_COMPAT) {
   1576 			log(LOG_NOTICE, "LFCNSTOPWRAP waiting for log wrap\n");
   1577 			error = mtsleep(&fs->lfs_nowrap, PCATCH | PUSER,
   1578 				"segwrap", 0, &lfs_lock);
   1579 			log(LOG_NOTICE, "LFCNSTOPWRAP done waiting\n");
   1580 			if (error) {
   1581 				lfs_wrapgo(fs, VTOI(ap->a_vp), 0);
   1582 			}
   1583 		}
   1584 		mutex_exit(&lfs_lock);
   1585 		return 0;
   1586 
   1587 	    case LFCNWRAPGO:
   1588 	    case LFCNWRAPGO_COMPAT:
   1589 		/*
   1590 		 * Having done its work, the agent wakes up the writer.
   1591 		 * If the argument is 1, it sleeps until a new segment
   1592 		 * is selected.
   1593 		 */
   1594 		mutex_enter(&lfs_lock);
   1595 		error = lfs_wrapgo(fs, VTOI(ap->a_vp),
   1596 				   ap->a_command == LFCNWRAPGO_COMPAT ? 1 :
   1597 				    *((int *)ap->a_data));
   1598 		mutex_exit(&lfs_lock);
   1599 		return error;
   1600 
   1601 	    case LFCNWRAPPASS:
   1602 		if ((VTOI(ap->a_vp)->i_lfs_iflags & LFSI_WRAPWAIT))
   1603 			return EALREADY;
   1604 		mutex_enter(&lfs_lock);
   1605 		if (fs->lfs_stoplwp != curlwp) {
   1606 			mutex_exit(&lfs_lock);
   1607 			return EALREADY;
   1608 		}
   1609 		if (fs->lfs_nowrap == 0) {
   1610 			mutex_exit(&lfs_lock);
   1611 			return EBUSY;
   1612 		}
   1613 		fs->lfs_wrappass = 1;
   1614 		wakeup(&fs->lfs_wrappass);
   1615 		/* Wait for the log to wrap, if asked */
   1616 		if (*(int *)ap->a_data) {
   1617 			mutex_enter(ap->a_vp->v_interlock);
   1618 			if (lfs_vref(ap->a_vp) != 0)
   1619 				panic("LFCNWRAPPASS: lfs_vref failed");
   1620 			VTOI(ap->a_vp)->i_lfs_iflags |= LFSI_WRAPWAIT;
   1621 			log(LOG_NOTICE, "LFCNPASS waiting for log wrap\n");
   1622 			error = mtsleep(&fs->lfs_nowrap, PCATCH | PUSER,
   1623 				"segwrap", 0, &lfs_lock);
   1624 			log(LOG_NOTICE, "LFCNPASS done waiting\n");
   1625 			VTOI(ap->a_vp)->i_lfs_iflags &= ~LFSI_WRAPWAIT;
   1626 			lfs_vunref(ap->a_vp);
   1627 		}
   1628 		mutex_exit(&lfs_lock);
   1629 		return error;
   1630 
   1631 	    case LFCNWRAPSTATUS:
   1632 		mutex_enter(&lfs_lock);
   1633 		*(int *)ap->a_data = fs->lfs_wrapstatus;
   1634 		mutex_exit(&lfs_lock);
   1635 		return 0;
   1636 
   1637 	    default:
   1638 		return ulfs_fcntl(v);
   1639 	}
   1640 	return 0;
   1641 }
   1642 
   1643 int
   1644 lfs_getpages(void *v)
   1645 {
   1646 	struct vop_getpages_args /* {
   1647 		struct vnode *a_vp;
   1648 		voff_t a_offset;
   1649 		struct vm_page **a_m;
   1650 		int *a_count;
   1651 		int a_centeridx;
   1652 		vm_prot_t a_access_type;
   1653 		int a_advice;
   1654 		int a_flags;
   1655 	} */ *ap = v;
   1656 
   1657 	if (VTOI(ap->a_vp)->i_number == LFS_IFILE_INUM &&
   1658 	    (ap->a_access_type & VM_PROT_WRITE) != 0) {
   1659 		return EPERM;
   1660 	}
   1661 	if ((ap->a_access_type & VM_PROT_WRITE) != 0) {
   1662 		mutex_enter(&lfs_lock);
   1663 		LFS_SET_UINO(VTOI(ap->a_vp), IN_MODIFIED);
   1664 		mutex_exit(&lfs_lock);
   1665 	}
   1666 
   1667 	/*
   1668 	 * we're relying on the fact that genfs_getpages() always read in
   1669 	 * entire filesystem blocks.
   1670 	 */
   1671 	return genfs_getpages(v);
   1672 }
   1673 
   1674 /*
   1675  * Wait for a page to become unbusy, possibly printing diagnostic messages
   1676  * as well.
   1677  *
   1678  * Called with vp->v_interlock held; return with it held.
   1679  */
   1680 static void
   1681 wait_for_page(struct vnode *vp, struct vm_page *pg, const char *label)
   1682 {
   1683 	KASSERT(mutex_owned(vp->v_interlock));
   1684 	if ((pg->flags & PG_BUSY) == 0)
   1685 		return;		/* Nothing to wait for! */
   1686 
   1687 #if defined(DEBUG) && defined(UVM_PAGE_TRKOWN)
   1688 	static struct vm_page *lastpg;
   1689 
   1690 	if (label != NULL && pg != lastpg) {
   1691 		if (pg->owner_tag) {
   1692 			printf("lfs_putpages[%d.%d]: %s: page %p owner %d.%d [%s]\n",
   1693 			       curproc->p_pid, curlwp->l_lid, label,
   1694 			       pg, pg->owner, pg->lowner, pg->owner_tag);
   1695 		} else {
   1696 			printf("lfs_putpages[%d.%d]: %s: page %p unowned?!\n",
   1697 			       curproc->p_pid, curlwp->l_lid, label, pg);
   1698 		}
   1699 	}
   1700 	lastpg = pg;
   1701 #endif
   1702 
   1703 	pg->flags |= PG_WANTED;
   1704 	UVM_UNLOCK_AND_WAIT(pg, vp->v_interlock, 0, "lfsput", 0);
   1705 	mutex_enter(vp->v_interlock);
   1706 }
   1707 
   1708 /*
   1709  * This routine is called by lfs_putpages() when it can't complete the
   1710  * write because a page is busy.  This means that either (1) someone,
   1711  * possibly the pagedaemon, is looking at this page, and will give it up
   1712  * presently; or (2) we ourselves are holding the page busy in the
   1713  * process of being written (either gathered or actually on its way to
   1714  * disk).  We don't need to give up the segment lock, but we might need
   1715  * to call lfs_writeseg() to expedite the page's journey to disk.
   1716  *
   1717  * Called with vp->v_interlock held; return with it held.
   1718  */
   1719 /* #define BUSYWAIT */
   1720 static void
   1721 write_and_wait(struct lfs *fs, struct vnode *vp, struct vm_page *pg,
   1722 	       int seglocked, const char *label)
   1723 {
   1724 	KASSERT(mutex_owned(vp->v_interlock));
   1725 #ifndef BUSYWAIT
   1726 	struct inode *ip = VTOI(vp);
   1727 	struct segment *sp = fs->lfs_sp;
   1728 	int count = 0;
   1729 
   1730 	if (pg == NULL)
   1731 		return;
   1732 
   1733 	while (pg->flags & PG_BUSY &&
   1734 	    pg->uobject == &vp->v_uobj) {
   1735 		mutex_exit(vp->v_interlock);
   1736 		if (sp->cbpp - sp->bpp > 1) {
   1737 			/* Write gathered pages */
   1738 			lfs_updatemeta(sp);
   1739 			lfs_release_finfo(fs);
   1740 			(void) lfs_writeseg(fs, sp);
   1741 
   1742 			/*
   1743 			 * Reinitialize FIP
   1744 			 */
   1745 			KASSERT(sp->vp == vp);
   1746 			lfs_acquire_finfo(fs, ip->i_number,
   1747 					  ip->i_gen);
   1748 		}
   1749 		++count;
   1750 		mutex_enter(vp->v_interlock);
   1751 		wait_for_page(vp, pg, label);
   1752 	}
   1753 	if (label != NULL && count > 1) {
   1754 		DLOG((DLOG_PAGE, "lfs_putpages[%d]: %s: %sn = %d\n",
   1755 		      curproc->p_pid, label, (count > 0 ? "looping, " : ""),
   1756 		      count));
   1757 	}
   1758 #else
   1759 	preempt(1);
   1760 #endif
   1761 	KASSERT(mutex_owned(vp->v_interlock));
   1762 }
   1763 
   1764 /*
   1765  * Make sure that for all pages in every block in the given range,
   1766  * either all are dirty or all are clean.  If any of the pages
   1767  * we've seen so far are dirty, put the vnode on the paging chain,
   1768  * and mark it IN_PAGING.
   1769  *
   1770  * If checkfirst != 0, don't check all the pages but return at the
   1771  * first dirty page.
   1772  */
   1773 static int
   1774 check_dirty(struct lfs *fs, struct vnode *vp,
   1775 	    off_t startoffset, off_t endoffset, off_t blkeof,
   1776 	    int flags, int checkfirst, struct vm_page **pgp)
   1777 {
   1778 	int by_list;
   1779 	struct vm_page *curpg = NULL; /* XXX: gcc */
   1780 	struct vm_page *pgs[MAXBSIZE / PAGE_SIZE], *pg;
   1781 	off_t soff = 0; /* XXX: gcc */
   1782 	voff_t off;
   1783 	int i;
   1784 	int nonexistent;
   1785 	int any_dirty;	/* number of dirty pages */
   1786 	int dirty;	/* number of dirty pages in a block */
   1787 	int tdirty;
   1788 	int pages_per_block = fs->lfs_bsize >> PAGE_SHIFT;
   1789 	int pagedaemon = (curlwp == uvm.pagedaemon_lwp);
   1790 
   1791 	KASSERT(mutex_owned(vp->v_interlock));
   1792 	ASSERT_MAYBE_SEGLOCK(fs);
   1793   top:
   1794 	by_list = (vp->v_uobj.uo_npages <=
   1795 		   ((endoffset - startoffset) >> PAGE_SHIFT) *
   1796 		   UVM_PAGE_TREE_PENALTY);
   1797 	any_dirty = 0;
   1798 
   1799 	if (by_list) {
   1800 		curpg = TAILQ_FIRST(&vp->v_uobj.memq);
   1801 	} else {
   1802 		soff = startoffset;
   1803 	}
   1804 	while (by_list || soff < MIN(blkeof, endoffset)) {
   1805 		if (by_list) {
   1806 			/*
   1807 			 * Find the first page in a block.  Skip
   1808 			 * blocks outside our area of interest or beyond
   1809 			 * the end of file.
   1810 			 */
   1811 			KASSERT(curpg == NULL
   1812 			    || (curpg->flags & PG_MARKER) == 0);
   1813 			if (pages_per_block > 1) {
   1814 				while (curpg &&
   1815 				    ((curpg->offset & fs->lfs_bmask) ||
   1816 				    curpg->offset >= vp->v_size ||
   1817 				    curpg->offset >= endoffset)) {
   1818 					curpg = TAILQ_NEXT(curpg, listq.queue);
   1819 					KASSERT(curpg == NULL ||
   1820 					    (curpg->flags & PG_MARKER) == 0);
   1821 				}
   1822 			}
   1823 			if (curpg == NULL)
   1824 				break;
   1825 			soff = curpg->offset;
   1826 		}
   1827 
   1828 		/*
   1829 		 * Mark all pages in extended range busy; find out if any
   1830 		 * of them are dirty.
   1831 		 */
   1832 		nonexistent = dirty = 0;
   1833 		for (i = 0; i == 0 || i < pages_per_block; i++) {
   1834 			KASSERT(mutex_owned(vp->v_interlock));
   1835 			if (by_list && pages_per_block <= 1) {
   1836 				pgs[i] = pg = curpg;
   1837 			} else {
   1838 				off = soff + (i << PAGE_SHIFT);
   1839 				pgs[i] = pg = uvm_pagelookup(&vp->v_uobj, off);
   1840 				if (pg == NULL) {
   1841 					++nonexistent;
   1842 					continue;
   1843 				}
   1844 			}
   1845 			KASSERT(pg != NULL);
   1846 
   1847 			/*
   1848 			 * If we're holding the segment lock, we can deadlock
   1849 			 * against a process that has our page and is waiting
   1850 			 * for the cleaner, while the cleaner waits for the
   1851 			 * segment lock.  Just bail in that case.
   1852 			 */
   1853 			if ((pg->flags & PG_BUSY) &&
   1854 			    (pagedaemon || LFS_SEGLOCK_HELD(fs))) {
   1855 				if (i > 0)
   1856 					uvm_page_unbusy(pgs, i);
   1857 				DLOG((DLOG_PAGE, "lfs_putpages: avoiding 3-way or pagedaemon deadlock\n"));
   1858 				if (pgp)
   1859 					*pgp = pg;
   1860 				KASSERT(mutex_owned(vp->v_interlock));
   1861 				return -1;
   1862 			}
   1863 
   1864 			while (pg->flags & PG_BUSY) {
   1865 				wait_for_page(vp, pg, NULL);
   1866 				KASSERT(mutex_owned(vp->v_interlock));
   1867 				if (i > 0)
   1868 					uvm_page_unbusy(pgs, i);
   1869 				KASSERT(mutex_owned(vp->v_interlock));
   1870 				goto top;
   1871 			}
   1872 			pg->flags |= PG_BUSY;
   1873 			UVM_PAGE_OWN(pg, "lfs_putpages");
   1874 
   1875 			pmap_page_protect(pg, VM_PROT_NONE);
   1876 			tdirty = (pmap_clear_modify(pg) ||
   1877 				  (pg->flags & PG_CLEAN) == 0);
   1878 			dirty += tdirty;
   1879 		}
   1880 		if (pages_per_block > 0 && nonexistent >= pages_per_block) {
   1881 			if (by_list) {
   1882 				curpg = TAILQ_NEXT(curpg, listq.queue);
   1883 			} else {
   1884 				soff += fs->lfs_bsize;
   1885 			}
   1886 			continue;
   1887 		}
   1888 
   1889 		any_dirty += dirty;
   1890 		KASSERT(nonexistent == 0);
   1891 		KASSERT(mutex_owned(vp->v_interlock));
   1892 
   1893 		/*
   1894 		 * If any are dirty make all dirty; unbusy them,
   1895 		 * but if we were asked to clean, wire them so that
   1896 		 * the pagedaemon doesn't bother us about them while
   1897 		 * they're on their way to disk.
   1898 		 */
   1899 		for (i = 0; i == 0 || i < pages_per_block; i++) {
   1900 			KASSERT(mutex_owned(vp->v_interlock));
   1901 			pg = pgs[i];
   1902 			KASSERT(!((pg->flags & PG_CLEAN) && (pg->flags & PG_DELWRI)));
   1903 			KASSERT(pg->flags & PG_BUSY);
   1904 			if (dirty) {
   1905 				pg->flags &= ~PG_CLEAN;
   1906 				if (flags & PGO_FREE) {
   1907 					/*
   1908 					 * Wire the page so that
   1909 					 * pdaemon doesn't see it again.
   1910 					 */
   1911 					mutex_enter(&uvm_pageqlock);
   1912 					uvm_pagewire(pg);
   1913 					mutex_exit(&uvm_pageqlock);
   1914 
   1915 					/* Suspended write flag */
   1916 					pg->flags |= PG_DELWRI;
   1917 				}
   1918 			}
   1919 			if (pg->flags & PG_WANTED)
   1920 				wakeup(pg);
   1921 			pg->flags &= ~(PG_WANTED|PG_BUSY);
   1922 			UVM_PAGE_OWN(pg, NULL);
   1923 		}
   1924 
   1925 		if (checkfirst && any_dirty)
   1926 			break;
   1927 
   1928 		if (by_list) {
   1929 			curpg = TAILQ_NEXT(curpg, listq.queue);
   1930 		} else {
   1931 			soff += MAX(PAGE_SIZE, fs->lfs_bsize);
   1932 		}
   1933 	}
   1934 
   1935 	KASSERT(mutex_owned(vp->v_interlock));
   1936 	return any_dirty;
   1937 }
   1938 
   1939 /*
   1940  * lfs_putpages functions like genfs_putpages except that
   1941  *
   1942  * (1) It needs to bounds-check the incoming requests to ensure that
   1943  *     they are block-aligned; if they are not, expand the range and
   1944  *     do the right thing in case, e.g., the requested range is clean
   1945  *     but the expanded range is dirty.
   1946  *
   1947  * (2) It needs to explicitly send blocks to be written when it is done.
   1948  *     If VOP_PUTPAGES is called without the seglock held, we simply take
   1949  *     the seglock and let lfs_segunlock wait for us.
   1950  *     XXX There might be a bad situation if we have to flush a vnode while
   1951  *     XXX lfs_markv is in operation.  As of this writing we panic in this
   1952  *     XXX case.
   1953  *
   1954  * Assumptions:
   1955  *
   1956  * (1) The caller does not hold any pages in this vnode busy.  If it does,
   1957  *     there is a danger that when we expand the page range and busy the
   1958  *     pages we will deadlock.
   1959  *
   1960  * (2) We are called with vp->v_interlock held; we must return with it
   1961  *     released.
   1962  *
   1963  * (3) We don't absolutely have to free pages right away, provided that
   1964  *     the request does not have PGO_SYNCIO.  When the pagedaemon gives
   1965  *     us a request with PGO_FREE, we take the pages out of the paging
   1966  *     queue and wake up the writer, which will handle freeing them for us.
   1967  *
   1968  *     We ensure that for any filesystem block, all pages for that
   1969  *     block are either resident or not, even if those pages are higher
   1970  *     than EOF; that means that we will be getting requests to free
   1971  *     "unused" pages above EOF all the time, and should ignore them.
   1972  *
   1973  * (4) If we are called with PGO_LOCKED, the finfo array we are to write
   1974  *     into has been set up for us by lfs_writefile.  If not, we will
   1975  *     have to handle allocating and/or freeing an finfo entry.
   1976  *
   1977  * XXX note that we're (ab)using PGO_LOCKED as "seglock held".
   1978  */
   1979 
   1980 /* How many times to loop before we should start to worry */
   1981 #define TOOMANY 4
   1982 
   1983 int
   1984 lfs_putpages(void *v)
   1985 {
   1986 	int error;
   1987 	struct vop_putpages_args /* {
   1988 		struct vnode *a_vp;
   1989 		voff_t a_offlo;
   1990 		voff_t a_offhi;
   1991 		int a_flags;
   1992 	} */ *ap = v;
   1993 	struct vnode *vp;
   1994 	struct inode *ip;
   1995 	struct lfs *fs;
   1996 	struct segment *sp;
   1997 	off_t origoffset, startoffset, endoffset, origendoffset, blkeof;
   1998 	off_t off, max_endoffset;
   1999 	bool seglocked, sync, pagedaemon, reclaim;
   2000 	struct vm_page *pg, *busypg;
   2001 	UVMHIST_FUNC("lfs_putpages"); UVMHIST_CALLED(ubchist);
   2002 	int oreclaim = 0;
   2003 	int donewriting = 0;
   2004 #ifdef DEBUG
   2005 	int debug_n_again, debug_n_dirtyclean;
   2006 #endif
   2007 
   2008 	vp = ap->a_vp;
   2009 	ip = VTOI(vp);
   2010 	fs = ip->i_lfs;
   2011 	sync = (ap->a_flags & PGO_SYNCIO) != 0;
   2012 	reclaim = (ap->a_flags & PGO_RECLAIM) != 0;
   2013 	pagedaemon = (curlwp == uvm.pagedaemon_lwp);
   2014 
   2015 	KASSERT(mutex_owned(vp->v_interlock));
   2016 
   2017 	/* Putpages does nothing for metadata. */
   2018 	if (vp == fs->lfs_ivnode || vp->v_type != VREG) {
   2019 		mutex_exit(vp->v_interlock);
   2020 		return 0;
   2021 	}
   2022 
   2023 	/*
   2024 	 * If there are no pages, don't do anything.
   2025 	 */
   2026 	if (vp->v_uobj.uo_npages == 0) {
   2027 		if (TAILQ_EMPTY(&vp->v_uobj.memq) &&
   2028 		    (vp->v_iflag & VI_ONWORKLST) &&
   2029 		    LIST_FIRST(&vp->v_dirtyblkhd) == NULL) {
   2030 			vp->v_iflag &= ~VI_WRMAPDIRTY;
   2031 			vn_syncer_remove_from_worklist(vp);
   2032 		}
   2033 		mutex_exit(vp->v_interlock);
   2034 
   2035 		/* Remove us from paging queue, if we were on it */
   2036 		mutex_enter(&lfs_lock);
   2037 		if (ip->i_flags & IN_PAGING) {
   2038 			ip->i_flags &= ~IN_PAGING;
   2039 			TAILQ_REMOVE(&fs->lfs_pchainhd, ip, i_lfs_pchain);
   2040 		}
   2041 		mutex_exit(&lfs_lock);
   2042 
   2043 		KASSERT(!mutex_owned(vp->v_interlock));
   2044 		return 0;
   2045 	}
   2046 
   2047 	blkeof = lfs_blkroundup(fs, ip->i_size);
   2048 
   2049 	/*
   2050 	 * Ignore requests to free pages past EOF but in the same block
   2051 	 * as EOF, unless the vnode is being reclaimed or the request
   2052 	 * is synchronous.  (If the request is sync, it comes from
   2053 	 * lfs_truncate.)
   2054 	 *
   2055 	 * To avoid being flooded with this request, make these pages
   2056 	 * look "active".
   2057 	 */
   2058 	if (!sync && !reclaim &&
   2059 	    ap->a_offlo >= ip->i_size && ap->a_offlo < blkeof) {
   2060 		origoffset = ap->a_offlo;
   2061 		for (off = origoffset; off < blkeof; off += fs->lfs_bsize) {
   2062 			pg = uvm_pagelookup(&vp->v_uobj, off);
   2063 			KASSERT(pg != NULL);
   2064 			while (pg->flags & PG_BUSY) {
   2065 				pg->flags |= PG_WANTED;
   2066 				UVM_UNLOCK_AND_WAIT(pg, vp->v_interlock, 0,
   2067 						    "lfsput2", 0);
   2068 				mutex_enter(vp->v_interlock);
   2069 			}
   2070 			mutex_enter(&uvm_pageqlock);
   2071 			uvm_pageactivate(pg);
   2072 			mutex_exit(&uvm_pageqlock);
   2073 		}
   2074 		ap->a_offlo = blkeof;
   2075 		if (ap->a_offhi > 0 && ap->a_offhi <= ap->a_offlo) {
   2076 			mutex_exit(vp->v_interlock);
   2077 			return 0;
   2078 		}
   2079 	}
   2080 
   2081 	/*
   2082 	 * Extend page range to start and end at block boundaries.
   2083 	 * (For the purposes of VOP_PUTPAGES, fragments don't exist.)
   2084 	 */
   2085 	origoffset = ap->a_offlo;
   2086 	origendoffset = ap->a_offhi;
   2087 	startoffset = origoffset & ~(fs->lfs_bmask);
   2088 	max_endoffset = (trunc_page(LLONG_MAX) >> fs->lfs_bshift)
   2089 					       << fs->lfs_bshift;
   2090 
   2091 	if (origendoffset == 0 || ap->a_flags & PGO_ALLPAGES) {
   2092 		endoffset = max_endoffset;
   2093 		origendoffset = endoffset;
   2094 	} else {
   2095 		origendoffset = round_page(ap->a_offhi);
   2096 		endoffset = round_page(lfs_blkroundup(fs, origendoffset));
   2097 	}
   2098 
   2099 	KASSERT(startoffset > 0 || endoffset >= startoffset);
   2100 	if (startoffset == endoffset) {
   2101 		/* Nothing to do, why were we called? */
   2102 		mutex_exit(vp->v_interlock);
   2103 		DLOG((DLOG_PAGE, "lfs_putpages: startoffset = endoffset = %"
   2104 		      PRId64 "\n", startoffset));
   2105 		return 0;
   2106 	}
   2107 
   2108 	ap->a_offlo = startoffset;
   2109 	ap->a_offhi = endoffset;
   2110 
   2111 	/*
   2112 	 * If not cleaning, just send the pages through genfs_putpages
   2113 	 * to be returned to the pool.
   2114 	 */
   2115 	if (!(ap->a_flags & PGO_CLEANIT)) {
   2116 		DLOG((DLOG_PAGE, "lfs_putpages: no cleanit vn %p ino %d (flags %x)\n",
   2117 		      vp, (int)ip->i_number, ap->a_flags));
   2118 		int r = genfs_putpages(v);
   2119 		KASSERT(!mutex_owned(vp->v_interlock));
   2120 		return r;
   2121 	}
   2122 
   2123 	/* Set PGO_BUSYFAIL to avoid deadlocks */
   2124 	ap->a_flags |= PGO_BUSYFAIL;
   2125 
   2126 	/*
   2127 	 * Likewise, if we are asked to clean but the pages are not
   2128 	 * dirty, we can just free them using genfs_putpages.
   2129 	 */
   2130 #ifdef DEBUG
   2131 	debug_n_dirtyclean = 0;
   2132 #endif
   2133 	do {
   2134 		int r;
   2135 		KASSERT(mutex_owned(vp->v_interlock));
   2136 
   2137 		/* Count the number of dirty pages */
   2138 		r = check_dirty(fs, vp, startoffset, endoffset, blkeof,
   2139 				ap->a_flags, 1, NULL);
   2140 		if (r < 0) {
   2141 			/* Pages are busy with another process */
   2142 			mutex_exit(vp->v_interlock);
   2143 			return EDEADLK;
   2144 		}
   2145 		if (r > 0) /* Some pages are dirty */
   2146 			break;
   2147 
   2148 		/*
   2149 		 * Sometimes pages are dirtied between the time that
   2150 		 * we check and the time we try to clean them.
   2151 		 * Instruct lfs_gop_write to return EDEADLK in this case
   2152 		 * so we can write them properly.
   2153 		 */
   2154 		ip->i_lfs_iflags |= LFSI_NO_GOP_WRITE;
   2155 		r = genfs_do_putpages(vp, startoffset, endoffset,
   2156 				       ap->a_flags & ~PGO_SYNCIO, &busypg);
   2157 		ip->i_lfs_iflags &= ~LFSI_NO_GOP_WRITE;
   2158 		if (r != EDEADLK) {
   2159 			KASSERT(!mutex_owned(vp->v_interlock));
   2160  			return r;
   2161 		}
   2162 
   2163 		/* One of the pages was busy.  Start over. */
   2164 		mutex_enter(vp->v_interlock);
   2165 		wait_for_page(vp, busypg, "dirtyclean");
   2166 #ifdef DEBUG
   2167 		++debug_n_dirtyclean;
   2168 #endif
   2169 	} while(1);
   2170 
   2171 #ifdef DEBUG
   2172 	if (debug_n_dirtyclean > TOOMANY)
   2173 		DLOG((DLOG_PAGE, "lfs_putpages: dirtyclean: looping, n = %d\n",
   2174 		      debug_n_dirtyclean));
   2175 #endif
   2176 
   2177 	/*
   2178 	 * Dirty and asked to clean.
   2179 	 *
   2180 	 * Pagedaemon can't actually write LFS pages; wake up
   2181 	 * the writer to take care of that.  The writer will
   2182 	 * notice the pager inode queue and act on that.
   2183 	 *
   2184 	 * XXX We must drop the vp->interlock before taking the lfs_lock or we
   2185 	 * get a nasty deadlock with lfs_flush_pchain().
   2186 	 */
   2187 	if (pagedaemon) {
   2188 		mutex_exit(vp->v_interlock);
   2189 		mutex_enter(&lfs_lock);
   2190 		if (!(ip->i_flags & IN_PAGING)) {
   2191 			ip->i_flags |= IN_PAGING;
   2192 			TAILQ_INSERT_TAIL(&fs->lfs_pchainhd, ip, i_lfs_pchain);
   2193 		}
   2194 		wakeup(&lfs_writer_daemon);
   2195 		mutex_exit(&lfs_lock);
   2196 		preempt();
   2197 		KASSERT(!mutex_owned(vp->v_interlock));
   2198 		return EWOULDBLOCK;
   2199 	}
   2200 
   2201 	/*
   2202 	 * If this is a file created in a recent dirop, we can't flush its
   2203 	 * inode until the dirop is complete.  Drain dirops, then flush the
   2204 	 * filesystem (taking care of any other pending dirops while we're
   2205 	 * at it).
   2206 	 */
   2207 	if ((ap->a_flags & (PGO_CLEANIT|PGO_LOCKED)) == PGO_CLEANIT &&
   2208 	    (vp->v_uflag & VU_DIROP)) {
   2209 		DLOG((DLOG_PAGE, "lfs_putpages: flushing VU_DIROP\n"));
   2210 
   2211  		lfs_writer_enter(fs, "ppdirop");
   2212 
   2213 		/* Note if we hold the vnode locked */
   2214 		if (VOP_ISLOCKED(vp) == LK_EXCLUSIVE)
   2215 		{
   2216 		    DLOG((DLOG_PAGE, "lfs_putpages: dirop inode already locked\n"));
   2217 		} else {
   2218 		    DLOG((DLOG_PAGE, "lfs_putpages: dirop inode not locked\n"));
   2219 		}
   2220 		mutex_exit(vp->v_interlock);
   2221 
   2222 		mutex_enter(&lfs_lock);
   2223 		lfs_flush_fs(fs, sync ? SEGM_SYNC : 0);
   2224 		mutex_exit(&lfs_lock);
   2225 
   2226 		mutex_enter(vp->v_interlock);
   2227 		lfs_writer_leave(fs);
   2228 
   2229 		/* The flush will have cleaned out this vnode as well,
   2230 		   no need to do more to it. */
   2231 	}
   2232 
   2233 	/*
   2234 	 * This is it.	We are going to write some pages.  From here on
   2235 	 * down it's all just mechanics.
   2236 	 *
   2237 	 * Don't let genfs_putpages wait; lfs_segunlock will wait for us.
   2238 	 */
   2239 	ap->a_flags &= ~PGO_SYNCIO;
   2240 
   2241 	/*
   2242 	 * If we've already got the seglock, flush the node and return.
   2243 	 * The FIP has already been set up for us by lfs_writefile,
   2244 	 * and FIP cleanup and lfs_updatemeta will also be done there,
   2245 	 * unless genfs_putpages returns EDEADLK; then we must flush
   2246 	 * what we have, and correct FIP and segment header accounting.
   2247 	 */
   2248   get_seglock:
   2249 	/*
   2250 	 * If we are not called with the segment locked, lock it.
   2251 	 * Account for a new FIP in the segment header, and set sp->vp.
   2252 	 * (This should duplicate the setup at the top of lfs_writefile().)
   2253 	 */
   2254 	seglocked = (ap->a_flags & PGO_LOCKED) != 0;
   2255 	if (!seglocked) {
   2256 		mutex_exit(vp->v_interlock);
   2257 		error = lfs_seglock(fs, SEGM_PROT | (sync ? SEGM_SYNC : 0));
   2258 		if (error != 0) {
   2259 			KASSERT(!mutex_owned(vp->v_interlock));
   2260  			return error;
   2261 		}
   2262 		mutex_enter(vp->v_interlock);
   2263 		lfs_acquire_finfo(fs, ip->i_number, ip->i_gen);
   2264 	}
   2265 	sp = fs->lfs_sp;
   2266 	KASSERT(sp->vp == NULL);
   2267 	sp->vp = vp;
   2268 
   2269 	/* Note segments written by reclaim; only for debugging */
   2270 	if ((vp->v_iflag & VI_XLOCK) != 0) {
   2271 		sp->seg_flags |= SEGM_RECLAIM;
   2272 		fs->lfs_reclino = ip->i_number;
   2273 	}
   2274 
   2275 	/*
   2276 	 * Ensure that the partial segment is marked SS_DIROP if this
   2277 	 * vnode is a DIROP.
   2278 	 */
   2279 	if (!seglocked && vp->v_uflag & VU_DIROP)
   2280 		((SEGSUM *)(sp->segsum))->ss_flags |= (SS_DIROP|SS_CONT);
   2281 
   2282 	/*
   2283 	 * Loop over genfs_putpages until all pages are gathered.
   2284 	 * genfs_putpages() drops the interlock, so reacquire it if necessary.
   2285 	 * Whenever we lose the interlock we have to rerun check_dirty, as
   2286 	 * well, since more pages might have been dirtied in our absence.
   2287 	 */
   2288 #ifdef DEBUG
   2289 	debug_n_again = 0;
   2290 #endif
   2291 	do {
   2292 		busypg = NULL;
   2293 		KASSERT(mutex_owned(vp->v_interlock));
   2294 		if (check_dirty(fs, vp, startoffset, endoffset, blkeof,
   2295 				ap->a_flags, 0, &busypg) < 0) {
   2296 			mutex_exit(vp->v_interlock);
   2297 			/* XXX why? --ks */
   2298 			mutex_enter(vp->v_interlock);
   2299 			write_and_wait(fs, vp, busypg, seglocked, NULL);
   2300 			if (!seglocked) {
   2301 				mutex_exit(vp->v_interlock);
   2302 				lfs_release_finfo(fs);
   2303 				lfs_segunlock(fs);
   2304 				mutex_enter(vp->v_interlock);
   2305 			}
   2306 			sp->vp = NULL;
   2307 			goto get_seglock;
   2308 		}
   2309 
   2310 		busypg = NULL;
   2311 		KASSERT(!mutex_owned(&uvm_pageqlock));
   2312 		oreclaim = (ap->a_flags & PGO_RECLAIM);
   2313 		ap->a_flags &= ~PGO_RECLAIM;
   2314 		error = genfs_do_putpages(vp, startoffset, endoffset,
   2315 					   ap->a_flags, &busypg);
   2316 		ap->a_flags |= oreclaim;
   2317 
   2318 		if (error == EDEADLK || error == EAGAIN) {
   2319 			DLOG((DLOG_PAGE, "lfs_putpages: genfs_putpages returned"
   2320 			      " %d ino %d off %x (seg %d)\n", error,
   2321 			      ip->i_number, fs->lfs_offset,
   2322 			      lfs_dtosn(fs, fs->lfs_offset)));
   2323 
   2324 			if (oreclaim) {
   2325 				mutex_enter(vp->v_interlock);
   2326 				write_and_wait(fs, vp, busypg, seglocked, "again");
   2327 				mutex_exit(vp->v_interlock);
   2328 			} else {
   2329 				if ((sp->seg_flags & SEGM_SINGLE) &&
   2330 				    fs->lfs_curseg != fs->lfs_startseg)
   2331 					donewriting = 1;
   2332 			}
   2333 		} else if (error) {
   2334 			DLOG((DLOG_PAGE, "lfs_putpages: genfs_putpages returned"
   2335 			      " %d ino %d off %x (seg %d)\n", error,
   2336 			      (int)ip->i_number, fs->lfs_offset,
   2337 			      lfs_dtosn(fs, fs->lfs_offset)));
   2338 		}
   2339 		/* genfs_do_putpages loses the interlock */
   2340 #ifdef DEBUG
   2341 		++debug_n_again;
   2342 #endif
   2343 		if (oreclaim && error == EAGAIN) {
   2344 			DLOG((DLOG_PAGE, "vp %p ino %d vi_flags %x a_flags %x avoiding vclean panic\n",
   2345 			      vp, (int)ip->i_number, vp->v_iflag, ap->a_flags));
   2346 			mutex_enter(vp->v_interlock);
   2347 		}
   2348 		if (error == EDEADLK)
   2349 			mutex_enter(vp->v_interlock);
   2350 	} while (error == EDEADLK || (oreclaim && error == EAGAIN));
   2351 #ifdef DEBUG
   2352 	if (debug_n_again > TOOMANY)
   2353 		DLOG((DLOG_PAGE, "lfs_putpages: again: looping, n = %d\n", debug_n_again));
   2354 #endif
   2355 
   2356 	KASSERT(sp != NULL && sp->vp == vp);
   2357 	if (!seglocked && !donewriting) {
   2358 		sp->vp = NULL;
   2359 
   2360 		/* Write indirect blocks as well */
   2361 		lfs_gather(fs, fs->lfs_sp, vp, lfs_match_indir);
   2362 		lfs_gather(fs, fs->lfs_sp, vp, lfs_match_dindir);
   2363 		lfs_gather(fs, fs->lfs_sp, vp, lfs_match_tindir);
   2364 
   2365 		KASSERT(sp->vp == NULL);
   2366 		sp->vp = vp;
   2367 	}
   2368 
   2369 	/*
   2370 	 * Blocks are now gathered into a segment waiting to be written.
   2371 	 * All that's left to do is update metadata, and write them.
   2372 	 */
   2373 	lfs_updatemeta(sp);
   2374 	KASSERT(sp->vp == vp);
   2375 	sp->vp = NULL;
   2376 
   2377 	/*
   2378 	 * If we were called from lfs_writefile, we don't need to clean up
   2379 	 * the FIP or unlock the segment lock.	We're done.
   2380 	 */
   2381 	if (seglocked) {
   2382 		KASSERT(!mutex_owned(vp->v_interlock));
   2383 		return error;
   2384 	}
   2385 
   2386 	/* Clean up FIP and send it to disk. */
   2387 	lfs_release_finfo(fs);
   2388 	lfs_writeseg(fs, fs->lfs_sp);
   2389 
   2390 	/*
   2391 	 * Remove us from paging queue if we wrote all our pages.
   2392 	 */
   2393 	if (origendoffset == 0 || ap->a_flags & PGO_ALLPAGES) {
   2394 		mutex_enter(&lfs_lock);
   2395 		if (ip->i_flags & IN_PAGING) {
   2396 			ip->i_flags &= ~IN_PAGING;
   2397 			TAILQ_REMOVE(&fs->lfs_pchainhd, ip, i_lfs_pchain);
   2398 		}
   2399 		mutex_exit(&lfs_lock);
   2400 	}
   2401 
   2402 	/*
   2403 	 * XXX - with the malloc/copy writeseg, the pages are freed by now
   2404 	 * even if we don't wait (e.g. if we hold a nested lock).  This
   2405 	 * will not be true if we stop using malloc/copy.
   2406 	 */
   2407 	KASSERT(fs->lfs_sp->seg_flags & SEGM_PROT);
   2408 	lfs_segunlock(fs);
   2409 
   2410 	/*
   2411 	 * Wait for v_numoutput to drop to zero.  The seglock should
   2412 	 * take care of this, but there is a slight possibility that
   2413 	 * aiodoned might not have got around to our buffers yet.
   2414 	 */
   2415 	if (sync) {
   2416 		mutex_enter(vp->v_interlock);
   2417 		while (vp->v_numoutput > 0) {
   2418 			DLOG((DLOG_PAGE, "lfs_putpages: ino %d sleeping on"
   2419 			      " num %d\n", ip->i_number, vp->v_numoutput));
   2420 			cv_wait(&vp->v_cv, vp->v_interlock);
   2421 		}
   2422 		mutex_exit(vp->v_interlock);
   2423 	}
   2424 	KASSERT(!mutex_owned(vp->v_interlock));
   2425 	return error;
   2426 }
   2427 
   2428 /*
   2429  * Return the last logical file offset that should be written for this file
   2430  * if we're doing a write that ends at "size".	If writing, we need to know
   2431  * about sizes on disk, i.e. fragments if there are any; if reading, we need
   2432  * to know about entire blocks.
   2433  */
   2434 void
   2435 lfs_gop_size(struct vnode *vp, off_t size, off_t *eobp, int flags)
   2436 {
   2437 	struct inode *ip = VTOI(vp);
   2438 	struct lfs *fs = ip->i_lfs;
   2439 	daddr_t olbn, nlbn;
   2440 
   2441 	olbn = lfs_lblkno(fs, ip->i_size);
   2442 	nlbn = lfs_lblkno(fs, size);
   2443 	if (!(flags & GOP_SIZE_MEM) && nlbn < ULFS_NDADDR && olbn <= nlbn) {
   2444 		*eobp = lfs_fragroundup(fs, size);
   2445 	} else {
   2446 		*eobp = lfs_blkroundup(fs, size);
   2447 	}
   2448 }
   2449 
   2450 #ifdef DEBUG
   2451 void lfs_dump_vop(void *);
   2452 
   2453 void
   2454 lfs_dump_vop(void *v)
   2455 {
   2456 	struct vop_putpages_args /* {
   2457 		struct vnode *a_vp;
   2458 		voff_t a_offlo;
   2459 		voff_t a_offhi;
   2460 		int a_flags;
   2461 	} */ *ap = v;
   2462 
   2463 #ifdef DDB
   2464 	vfs_vnode_print(ap->a_vp, 0, printf);
   2465 #endif
   2466 	lfs_dump_dinode(VTOI(ap->a_vp)->i_din.ffs1_din);
   2467 }
   2468 #endif
   2469 
   2470 int
   2471 lfs_mmap(void *v)
   2472 {
   2473 	struct vop_mmap_args /* {
   2474 		const struct vnodeop_desc *a_desc;
   2475 		struct vnode *a_vp;
   2476 		vm_prot_t a_prot;
   2477 		kauth_cred_t a_cred;
   2478 	} */ *ap = v;
   2479 
   2480 	if (VTOI(ap->a_vp)->i_number == LFS_IFILE_INUM)
   2481 		return EOPNOTSUPP;
   2482 	return ulfs_mmap(v);
   2483 }
   2484 
   2485 static int
   2486 lfs_openextattr(void *v)
   2487 {
   2488 	struct vop_openextattr_args /* {
   2489 		struct vnode *a_vp;
   2490 		kauth_cred_t a_cred;
   2491 		struct proc *a_p;
   2492 	} */ *ap = v;
   2493 	struct inode *ip = VTOI(ap->a_vp);
   2494 	struct ulfsmount *ump = ip->i_ump;
   2495 	//struct lfs *fs = ip->i_lfs;
   2496 
   2497 	/* Not supported for ULFS1 file systems. */
   2498 	if (ump->um_fstype == ULFS1)
   2499 		return (EOPNOTSUPP);
   2500 
   2501 	/* XXX Not implemented for ULFS2 file systems. */
   2502 	return (EOPNOTSUPP);
   2503 }
   2504 
   2505 static int
   2506 lfs_closeextattr(void *v)
   2507 {
   2508 	struct vop_closeextattr_args /* {
   2509 		struct vnode *a_vp;
   2510 		int a_commit;
   2511 		kauth_cred_t a_cred;
   2512 		struct proc *a_p;
   2513 	} */ *ap = v;
   2514 	struct inode *ip = VTOI(ap->a_vp);
   2515 	struct ulfsmount *ump = ip->i_ump;
   2516 	//struct lfs *fs = ip->i_lfs;
   2517 
   2518 	/* Not supported for ULFS1 file systems. */
   2519 	if (ump->um_fstype == ULFS1)
   2520 		return (EOPNOTSUPP);
   2521 
   2522 	/* XXX Not implemented for ULFS2 file systems. */
   2523 	return (EOPNOTSUPP);
   2524 }
   2525 
   2526 static int
   2527 lfs_getextattr(void *v)
   2528 {
   2529 	struct vop_getextattr_args /* {
   2530 		struct vnode *a_vp;
   2531 		int a_attrnamespace;
   2532 		const char *a_name;
   2533 		struct uio *a_uio;
   2534 		size_t *a_size;
   2535 		kauth_cred_t a_cred;
   2536 		struct proc *a_p;
   2537 	} */ *ap = v;
   2538 	struct vnode *vp = ap->a_vp;
   2539 	struct inode *ip = VTOI(vp);
   2540 	struct ulfsmount *ump = ip->i_ump;
   2541 	//struct lfs *fs = ip->i_lfs;
   2542 	int error;
   2543 
   2544 	if (ump->um_fstype == ULFS1) {
   2545 #ifdef LFS_EXTATTR
   2546 		fstrans_start(vp->v_mount, FSTRANS_SHARED);
   2547 		error = ulfs_getextattr(ap);
   2548 		fstrans_done(vp->v_mount);
   2549 #else
   2550 		error = EOPNOTSUPP;
   2551 #endif
   2552 		return error;
   2553 	}
   2554 
   2555 	/* XXX Not implemented for ULFS2 file systems. */
   2556 	return (EOPNOTSUPP);
   2557 }
   2558 
   2559 static int
   2560 lfs_setextattr(void *v)
   2561 {
   2562 	struct vop_setextattr_args /* {
   2563 		struct vnode *a_vp;
   2564 		int a_attrnamespace;
   2565 		const char *a_name;
   2566 		struct uio *a_uio;
   2567 		kauth_cred_t a_cred;
   2568 		struct proc *a_p;
   2569 	} */ *ap = v;
   2570 	struct vnode *vp = ap->a_vp;
   2571 	struct inode *ip = VTOI(vp);
   2572 	struct ulfsmount *ump = ip->i_ump;
   2573 	//struct lfs *fs = ip->i_lfs;
   2574 	int error;
   2575 
   2576 	if (ump->um_fstype == ULFS1) {
   2577 #ifdef LFS_EXTATTR
   2578 		fstrans_start(vp->v_mount, FSTRANS_SHARED);
   2579 		error = ulfs_setextattr(ap);
   2580 		fstrans_done(vp->v_mount);
   2581 #else
   2582 		error = EOPNOTSUPP;
   2583 #endif
   2584 		return error;
   2585 	}
   2586 
   2587 	/* XXX Not implemented for ULFS2 file systems. */
   2588 	return (EOPNOTSUPP);
   2589 }
   2590 
   2591 static int
   2592 lfs_listextattr(void *v)
   2593 {
   2594 	struct vop_listextattr_args /* {
   2595 		struct vnode *a_vp;
   2596 		int a_attrnamespace;
   2597 		struct uio *a_uio;
   2598 		size_t *a_size;
   2599 		kauth_cred_t a_cred;
   2600 		struct proc *a_p;
   2601 	} */ *ap = v;
   2602 	struct vnode *vp = ap->a_vp;
   2603 	struct inode *ip = VTOI(vp);
   2604 	struct ulfsmount *ump = ip->i_ump;
   2605 	//struct lfs *fs = ip->i_lfs;
   2606 	int error;
   2607 
   2608 	if (ump->um_fstype == ULFS1) {
   2609 #ifdef LFS_EXTATTR
   2610 		fstrans_start(vp->v_mount, FSTRANS_SHARED);
   2611 		error = ulfs_listextattr(ap);
   2612 		fstrans_done(vp->v_mount);
   2613 #else
   2614 		error = EOPNOTSUPP;
   2615 #endif
   2616 		return error;
   2617 	}
   2618 
   2619 	/* XXX Not implemented for ULFS2 file systems. */
   2620 	return (EOPNOTSUPP);
   2621 }
   2622 
   2623 static int
   2624 lfs_deleteextattr(void *v)
   2625 {
   2626 	struct vop_deleteextattr_args /* {
   2627 		struct vnode *a_vp;
   2628 		int a_attrnamespace;
   2629 		kauth_cred_t a_cred;
   2630 		struct proc *a_p;
   2631 	} */ *ap = v;
   2632 	struct vnode *vp = ap->a_vp;
   2633 	struct inode *ip = VTOI(vp);
   2634 	struct ulfsmount *ump = ip->i_ump;
   2635 	//struct fs *fs = ip->i_lfs;
   2636 	int error;
   2637 
   2638 	if (ump->um_fstype == ULFS1) {
   2639 #ifdef LFS_EXTATTR
   2640 		fstrans_start(vp->v_mount, FSTRANS_SHARED);
   2641 		error = ulfs_deleteextattr(ap);
   2642 		fstrans_done(vp->v_mount);
   2643 #else
   2644 		error = EOPNOTSUPP;
   2645 #endif
   2646 		return error;
   2647 	}
   2648 
   2649 	/* XXX Not implemented for ULFS2 file systems. */
   2650 	return (EOPNOTSUPP);
   2651 }
   2652