Home | History | Annotate | Line # | Download | only in lfs
lfs_vnops.c revision 1.257
      1 /*	$NetBSD: lfs_vnops.c,v 1.257 2013/07/29 16:40:46 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.257 2013/07/29 16:40:46 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 		(void)fs; /* temporary: needed when LFS_EI is off */
    625 		/*
    626 		 * Want to be able to use this to make badblock
    627 		 * inodes, so don't truncate the dev number.
    628 		 */
    629 		if (ump->um_fstype == ULFS1)
    630 			ip->i_ffs1_rdev = ulfs_rw32(vap->va_rdev,
    631 			    ULFS_MPNEEDSWAP(fs));
    632 		else
    633 			ip->i_ffs2_rdev = ulfs_rw64(vap->va_rdev,
    634 			    ULFS_MPNEEDSWAP(fs));
    635 	}
    636 
    637 	/*
    638 	 * Call fsync to write the vnode so that we don't have to deal with
    639 	 * flushing it when it's marked VU_DIROP|VI_XLOCK.
    640 	 *
    641 	 * XXX KS - If we can't flush we also can't call vgone(), so must
    642 	 * return.  But, that leaves this vnode in limbo, also not good.
    643 	 * Can this ever happen (barring hardware failure)?
    644 	 */
    645 	if ((error = VOP_FSYNC(*vpp, NOCRED, FSYNC_WAIT, 0, 0)) != 0) {
    646 		panic("lfs_mknod: couldn't fsync (ino %llu)",
    647 		      (unsigned long long)ino);
    648 		/* return (error); */
    649 	}
    650 	/*
    651 	 * Remove vnode so that it will be reloaded by VFS_VGET and
    652 	 * checked to see if it is an alias of an existing entry in
    653 	 * the inode cache.
    654 	 */
    655 	/* Used to be vput, but that causes us to call VOP_INACTIVE twice. */
    656 
    657 	(*vpp)->v_type = VNON;
    658 	VOP_UNLOCK(*vpp);
    659 	vgone(*vpp);
    660 	error = VFS_VGET(mp, ino, vpp);
    661 
    662 	fstrans_done(ap->a_dvp->v_mount);
    663 	if (error != 0) {
    664 		*vpp = NULL;
    665 		return (error);
    666 	}
    667 	return (0);
    668 }
    669 
    670 int
    671 lfs_create(void *v)
    672 {
    673 	struct vop_create_args	/* {
    674 		struct vnode *a_dvp;
    675 		struct vnode **a_vpp;
    676 		struct componentname *a_cnp;
    677 		struct vattr *a_vap;
    678 	} */ *ap = v;
    679 	int error;
    680 
    681 	if ((error = SET_DIROP_CREATE(ap->a_dvp, ap->a_vpp)) != 0) {
    682 		vput(ap->a_dvp);
    683 		return error;
    684 	}
    685 	error = ulfs_create(ap);
    686 	SET_ENDOP_CREATE_AP(ap, "create");
    687 	return (error);
    688 }
    689 
    690 int
    691 lfs_mkdir(void *v)
    692 {
    693 	struct vop_mkdir_args	/* {
    694 		struct vnode *a_dvp;
    695 		struct vnode **a_vpp;
    696 		struct componentname *a_cnp;
    697 		struct vattr *a_vap;
    698 	} */ *ap = v;
    699 	int error;
    700 
    701 	if ((error = SET_DIROP_CREATE(ap->a_dvp, ap->a_vpp)) != 0) {
    702 		vput(ap->a_dvp);
    703 		return error;
    704 	}
    705 	error = ulfs_mkdir(ap);
    706 	SET_ENDOP_CREATE_AP(ap, "mkdir");
    707 	return (error);
    708 }
    709 
    710 int
    711 lfs_remove(void *v)
    712 {
    713 	struct vop_remove_args	/* {
    714 		struct vnode *a_dvp;
    715 		struct vnode *a_vp;
    716 		struct componentname *a_cnp;
    717 	} */ *ap = v;
    718 	struct vnode *dvp, *vp;
    719 	struct inode *ip;
    720 	int error;
    721 
    722 	dvp = ap->a_dvp;
    723 	vp = ap->a_vp;
    724 	ip = VTOI(vp);
    725 	if ((error = SET_DIROP_REMOVE(dvp, vp)) != 0) {
    726 		if (dvp == vp)
    727 			vrele(vp);
    728 		else
    729 			vput(vp);
    730 		vput(dvp);
    731 		return error;
    732 	}
    733 	error = ulfs_remove(ap);
    734 	if (ip->i_nlink == 0)
    735 		lfs_orphan(ip->i_lfs, ip->i_number);
    736 	SET_ENDOP_REMOVE(ip->i_lfs, dvp, ap->a_vp, "remove");
    737 	return (error);
    738 }
    739 
    740 int
    741 lfs_rmdir(void *v)
    742 {
    743 	struct vop_rmdir_args	/* {
    744 		struct vnodeop_desc *a_desc;
    745 		struct vnode *a_dvp;
    746 		struct vnode *a_vp;
    747 		struct componentname *a_cnp;
    748 	} */ *ap = v;
    749 	struct vnode *vp;
    750 	struct inode *ip;
    751 	int error;
    752 
    753 	vp = ap->a_vp;
    754 	ip = VTOI(vp);
    755 	if ((error = SET_DIROP_REMOVE(ap->a_dvp, ap->a_vp)) != 0) {
    756 		if (ap->a_dvp == vp)
    757 			vrele(ap->a_dvp);
    758 		else
    759 			vput(ap->a_dvp);
    760 		vput(vp);
    761 		return error;
    762 	}
    763 	error = ulfs_rmdir(ap);
    764 	if (ip->i_nlink == 0)
    765 		lfs_orphan(ip->i_lfs, ip->i_number);
    766 	SET_ENDOP_REMOVE(ip->i_lfs, ap->a_dvp, ap->a_vp, "rmdir");
    767 	return (error);
    768 }
    769 
    770 int
    771 lfs_link(void *v)
    772 {
    773 	struct vop_link_args	/* {
    774 		struct vnode *a_dvp;
    775 		struct vnode *a_vp;
    776 		struct componentname *a_cnp;
    777 	} */ *ap = v;
    778 	int error;
    779 	struct vnode **vpp = NULL;
    780 
    781 	if ((error = SET_DIROP_CREATE(ap->a_dvp, vpp)) != 0) {
    782 		vput(ap->a_dvp);
    783 		return error;
    784 	}
    785 	error = ulfs_link(ap);
    786 	SET_ENDOP_CREATE(VTOI(ap->a_dvp)->i_lfs, ap->a_dvp, vpp, "link");
    787 	return (error);
    788 }
    789 
    790 /* XXX hack to avoid calling ITIMES in getattr */
    791 int
    792 lfs_getattr(void *v)
    793 {
    794 	struct vop_getattr_args /* {
    795 		struct vnode *a_vp;
    796 		struct vattr *a_vap;
    797 		kauth_cred_t a_cred;
    798 	} */ *ap = v;
    799 	struct vnode *vp = ap->a_vp;
    800 	struct inode *ip = VTOI(vp);
    801 	struct vattr *vap = ap->a_vap;
    802 	struct lfs *fs = ip->i_lfs;
    803 
    804 	fstrans_start(vp->v_mount, FSTRANS_SHARED);
    805 	/*
    806 	 * Copy from inode table
    807 	 */
    808 	vap->va_fsid = ip->i_dev;
    809 	vap->va_fileid = ip->i_number;
    810 	vap->va_mode = ip->i_mode & ~LFS_IFMT;
    811 	vap->va_nlink = ip->i_nlink;
    812 	vap->va_uid = ip->i_uid;
    813 	vap->va_gid = ip->i_gid;
    814 	vap->va_rdev = (dev_t)ip->i_ffs1_rdev;
    815 	vap->va_size = vp->v_size;
    816 	vap->va_atime.tv_sec = ip->i_ffs1_atime;
    817 	vap->va_atime.tv_nsec = ip->i_ffs1_atimensec;
    818 	vap->va_mtime.tv_sec = ip->i_ffs1_mtime;
    819 	vap->va_mtime.tv_nsec = ip->i_ffs1_mtimensec;
    820 	vap->va_ctime.tv_sec = ip->i_ffs1_ctime;
    821 	vap->va_ctime.tv_nsec = ip->i_ffs1_ctimensec;
    822 	vap->va_flags = ip->i_flags;
    823 	vap->va_gen = ip->i_gen;
    824 	/* this doesn't belong here */
    825 	if (vp->v_type == VBLK)
    826 		vap->va_blocksize = BLKDEV_IOSIZE;
    827 	else if (vp->v_type == VCHR)
    828 		vap->va_blocksize = MAXBSIZE;
    829 	else
    830 		vap->va_blocksize = vp->v_mount->mnt_stat.f_iosize;
    831 	vap->va_bytes = lfs_fsbtob(fs, (u_quad_t)ip->i_lfs_effnblks);
    832 	vap->va_type = vp->v_type;
    833 	vap->va_filerev = ip->i_modrev;
    834 	fstrans_done(vp->v_mount);
    835 	return (0);
    836 }
    837 
    838 /*
    839  * Check to make sure the inode blocks won't choke the buffer
    840  * cache, then call ulfs_setattr as usual.
    841  */
    842 int
    843 lfs_setattr(void *v)
    844 {
    845 	struct vop_setattr_args /* {
    846 		struct vnode *a_vp;
    847 		struct vattr *a_vap;
    848 		kauth_cred_t a_cred;
    849 	} */ *ap = v;
    850 	struct vnode *vp = ap->a_vp;
    851 
    852 	lfs_check(vp, LFS_UNUSED_LBN, 0);
    853 	return ulfs_setattr(v);
    854 }
    855 
    856 /*
    857  * Release the block we hold on lfs_newseg wrapping.  Called on file close,
    858  * or explicitly from LFCNWRAPGO.  Called with the interlock held.
    859  */
    860 static int
    861 lfs_wrapgo(struct lfs *fs, struct inode *ip, int waitfor)
    862 {
    863 	if (fs->lfs_stoplwp != curlwp)
    864 		return EBUSY;
    865 
    866 	fs->lfs_stoplwp = NULL;
    867 	cv_signal(&fs->lfs_stopcv);
    868 
    869 	KASSERT(fs->lfs_nowrap > 0);
    870 	if (fs->lfs_nowrap <= 0) {
    871 		return 0;
    872 	}
    873 
    874 	if (--fs->lfs_nowrap == 0) {
    875 		log(LOG_NOTICE, "%s: re-enabled log wrap\n", fs->lfs_fsmnt);
    876 		wakeup(&fs->lfs_wrappass);
    877 		lfs_wakeup_cleaner(fs);
    878 	}
    879 	if (waitfor) {
    880 		mtsleep(&fs->lfs_nextseg, PCATCH | PUSER, "segment",
    881 		    0, &lfs_lock);
    882 	}
    883 
    884 	return 0;
    885 }
    886 
    887 /*
    888  * Close called.
    889  *
    890  * Update the times on the inode.
    891  */
    892 /* ARGSUSED */
    893 int
    894 lfs_close(void *v)
    895 {
    896 	struct vop_close_args /* {
    897 		struct vnode *a_vp;
    898 		int  a_fflag;
    899 		kauth_cred_t a_cred;
    900 	} */ *ap = v;
    901 	struct vnode *vp = ap->a_vp;
    902 	struct inode *ip = VTOI(vp);
    903 	struct lfs *fs = ip->i_lfs;
    904 
    905 	if ((ip->i_number == ULFS_ROOTINO || ip->i_number == LFS_IFILE_INUM) &&
    906 	    fs->lfs_stoplwp == curlwp) {
    907 		mutex_enter(&lfs_lock);
    908 		log(LOG_NOTICE, "lfs_close: releasing log wrap control\n");
    909 		lfs_wrapgo(fs, ip, 0);
    910 		mutex_exit(&lfs_lock);
    911 	}
    912 
    913 	if (vp == ip->i_lfs->lfs_ivnode &&
    914 	    vp->v_mount->mnt_iflag & IMNT_UNMOUNT)
    915 		return 0;
    916 
    917 	fstrans_start(vp->v_mount, FSTRANS_SHARED);
    918 	if (vp->v_usecount > 1 && vp != ip->i_lfs->lfs_ivnode) {
    919 		LFS_ITIMES(ip, NULL, NULL, NULL);
    920 	}
    921 	fstrans_done(vp->v_mount);
    922 	return (0);
    923 }
    924 
    925 /*
    926  * Close wrapper for special devices.
    927  *
    928  * Update the times on the inode then do device close.
    929  */
    930 int
    931 lfsspec_close(void *v)
    932 {
    933 	struct vop_close_args /* {
    934 		struct vnode	*a_vp;
    935 		int		a_fflag;
    936 		kauth_cred_t	a_cred;
    937 	} */ *ap = v;
    938 	struct vnode	*vp;
    939 	struct inode	*ip;
    940 
    941 	vp = ap->a_vp;
    942 	ip = VTOI(vp);
    943 	if (vp->v_usecount > 1) {
    944 		LFS_ITIMES(ip, NULL, NULL, NULL);
    945 	}
    946 	return (VOCALL (spec_vnodeop_p, VOFFSET(vop_close), ap));
    947 }
    948 
    949 /*
    950  * Close wrapper for fifo's.
    951  *
    952  * Update the times on the inode then do device close.
    953  */
    954 int
    955 lfsfifo_close(void *v)
    956 {
    957 	struct vop_close_args /* {
    958 		struct vnode	*a_vp;
    959 		int		a_fflag;
    960 		kauth_cred_	a_cred;
    961 	} */ *ap = v;
    962 	struct vnode	*vp;
    963 	struct inode	*ip;
    964 
    965 	vp = ap->a_vp;
    966 	ip = VTOI(vp);
    967 	if (ap->a_vp->v_usecount > 1) {
    968 		LFS_ITIMES(ip, NULL, NULL, NULL);
    969 	}
    970 	return (VOCALL (fifo_vnodeop_p, VOFFSET(vop_close), ap));
    971 }
    972 
    973 /*
    974  * Reclaim an inode so that it can be used for other purposes.
    975  */
    976 
    977 int
    978 lfs_reclaim(void *v)
    979 {
    980 	struct vop_reclaim_args /* {
    981 		struct vnode *a_vp;
    982 	} */ *ap = v;
    983 	struct vnode *vp = ap->a_vp;
    984 	struct inode *ip = VTOI(vp);
    985 	struct lfs *fs = ip->i_lfs;
    986 	int error;
    987 
    988 	/*
    989 	 * The inode must be freed and updated before being removed
    990 	 * from its hash chain.  Other threads trying to gain a hold
    991 	 * on the inode will be stalled because it is locked (VI_XLOCK).
    992 	 */
    993 	if (ip->i_nlink <= 0 && (vp->v_mount->mnt_flag & MNT_RDONLY) == 0)
    994 		lfs_vfree(vp, ip->i_number, ip->i_omode);
    995 
    996 	mutex_enter(&lfs_lock);
    997 	LFS_CLR_UINO(ip, IN_ALLMOD);
    998 	mutex_exit(&lfs_lock);
    999 	if ((error = ulfs_reclaim(vp)))
   1000 		return (error);
   1001 
   1002 	/*
   1003 	 * Take us off the paging and/or dirop queues if we were on them.
   1004 	 * We shouldn't be on them.
   1005 	 */
   1006 	mutex_enter(&lfs_lock);
   1007 	if (ip->i_flags & IN_PAGING) {
   1008 		log(LOG_WARNING, "%s: reclaimed vnode is IN_PAGING\n",
   1009 		    fs->lfs_fsmnt);
   1010 		ip->i_flags &= ~IN_PAGING;
   1011 		TAILQ_REMOVE(&fs->lfs_pchainhd, ip, i_lfs_pchain);
   1012 	}
   1013 	if (vp->v_uflag & VU_DIROP) {
   1014 		panic("reclaimed vnode is VU_DIROP");
   1015 		vp->v_uflag &= ~VU_DIROP;
   1016 		TAILQ_REMOVE(&fs->lfs_dchainhd, ip, i_lfs_dchain);
   1017 	}
   1018 	mutex_exit(&lfs_lock);
   1019 
   1020 	pool_put(&lfs_dinode_pool, ip->i_din.ffs1_din);
   1021 	lfs_deregister_all(vp);
   1022 	pool_put(&lfs_inoext_pool, ip->inode_ext.lfs);
   1023 	ip->inode_ext.lfs = NULL;
   1024 	genfs_node_destroy(vp);
   1025 	pool_put(&lfs_inode_pool, vp->v_data);
   1026 	vp->v_data = NULL;
   1027 	return (0);
   1028 }
   1029 
   1030 /*
   1031  * Read a block from a storage device.
   1032  *
   1033  * Calculate the logical to physical mapping if not done already,
   1034  * then call the device strategy routine.
   1035  *
   1036  * In order to avoid reading blocks that are in the process of being
   1037  * written by the cleaner---and hence are not mutexed by the normal
   1038  * buffer cache / page cache mechanisms---check for collisions before
   1039  * reading.
   1040  *
   1041  * We inline ulfs_strategy to make sure that the VOP_BMAP occurs *before*
   1042  * the active cleaner test.
   1043  *
   1044  * XXX This code assumes that lfs_markv makes synchronous checkpoints.
   1045  */
   1046 int
   1047 lfs_strategy(void *v)
   1048 {
   1049 	struct vop_strategy_args /* {
   1050 		struct vnode *a_vp;
   1051 		struct buf *a_bp;
   1052 	} */ *ap = v;
   1053 	struct buf	*bp;
   1054 	struct lfs	*fs;
   1055 	struct vnode	*vp;
   1056 	struct inode	*ip;
   1057 	daddr_t		tbn;
   1058 #define MAXLOOP 25
   1059 	int		i, sn, error, slept, loopcount;
   1060 
   1061 	bp = ap->a_bp;
   1062 	vp = ap->a_vp;
   1063 	ip = VTOI(vp);
   1064 	fs = ip->i_lfs;
   1065 
   1066 	/* lfs uses its strategy routine only for read */
   1067 	KASSERT(bp->b_flags & B_READ);
   1068 
   1069 	if (vp->v_type == VBLK || vp->v_type == VCHR)
   1070 		panic("lfs_strategy: spec");
   1071 	KASSERT(bp->b_bcount != 0);
   1072 	if (bp->b_blkno == bp->b_lblkno) {
   1073 		error = VOP_BMAP(vp, bp->b_lblkno, NULL, &bp->b_blkno,
   1074 				 NULL);
   1075 		if (error) {
   1076 			bp->b_error = error;
   1077 			bp->b_resid = bp->b_bcount;
   1078 			biodone(bp);
   1079 			return (error);
   1080 		}
   1081 		if ((long)bp->b_blkno == -1) /* no valid data */
   1082 			clrbuf(bp);
   1083 	}
   1084 	if ((long)bp->b_blkno < 0) { /* block is not on disk */
   1085 		bp->b_resid = bp->b_bcount;
   1086 		biodone(bp);
   1087 		return (0);
   1088 	}
   1089 
   1090 	slept = 1;
   1091 	loopcount = 0;
   1092 	mutex_enter(&lfs_lock);
   1093 	while (slept && fs->lfs_seglock) {
   1094 		mutex_exit(&lfs_lock);
   1095 		/*
   1096 		 * Look through list of intervals.
   1097 		 * There will only be intervals to look through
   1098 		 * if the cleaner holds the seglock.
   1099 		 * Since the cleaner is synchronous, we can trust
   1100 		 * the list of intervals to be current.
   1101 		 */
   1102 		tbn = LFS_DBTOFSB(fs, bp->b_blkno);
   1103 		sn = lfs_dtosn(fs, tbn);
   1104 		slept = 0;
   1105 		for (i = 0; i < fs->lfs_cleanind; i++) {
   1106 			if (sn == lfs_dtosn(fs, fs->lfs_cleanint[i]) &&
   1107 			    tbn >= fs->lfs_cleanint[i]) {
   1108 				DLOG((DLOG_CLEAN,
   1109 				      "lfs_strategy: ino %d lbn %" PRId64
   1110 				      " ind %d sn %d fsb %" PRIx32
   1111 				      " given sn %d fsb %" PRIx64 "\n",
   1112 				      ip->i_number, bp->b_lblkno, i,
   1113 				      lfs_dtosn(fs, fs->lfs_cleanint[i]),
   1114 				      fs->lfs_cleanint[i], sn, tbn));
   1115 				DLOG((DLOG_CLEAN,
   1116 				      "lfs_strategy: sleeping on ino %d lbn %"
   1117 				      PRId64 "\n", ip->i_number, bp->b_lblkno));
   1118 				mutex_enter(&lfs_lock);
   1119 				if (LFS_SEGLOCK_HELD(fs) && fs->lfs_iocount) {
   1120 					/*
   1121 					 * Cleaner can't wait for itself.
   1122 					 * Instead, wait for the blocks
   1123 					 * to be written to disk.
   1124 					 * XXX we need pribio in the test
   1125 					 * XXX here.
   1126 					 */
   1127  					mtsleep(&fs->lfs_iocount,
   1128  						(PRIBIO + 1) | PNORELOCK,
   1129 						"clean2", hz/10 + 1,
   1130  						&lfs_lock);
   1131 					slept = 1;
   1132 					++loopcount;
   1133 					break;
   1134 				} else if (fs->lfs_seglock) {
   1135 					mtsleep(&fs->lfs_seglock,
   1136 						(PRIBIO + 1) | PNORELOCK,
   1137 						"clean1", 0,
   1138 						&lfs_lock);
   1139 					slept = 1;
   1140 					break;
   1141 				}
   1142 				mutex_exit(&lfs_lock);
   1143 			}
   1144 		}
   1145 		mutex_enter(&lfs_lock);
   1146 		if (loopcount > MAXLOOP) {
   1147 			printf("lfs_strategy: breaking out of clean2 loop\n");
   1148 			break;
   1149 		}
   1150 	}
   1151 	mutex_exit(&lfs_lock);
   1152 
   1153 	vp = ip->i_devvp;
   1154 	return VOP_STRATEGY(vp, bp);
   1155 }
   1156 
   1157 /*
   1158  * Inline lfs_segwrite/lfs_writevnodes, but just for dirops.
   1159  * Technically this is a checkpoint (the on-disk state is valid)
   1160  * even though we are leaving out all the file data.
   1161  */
   1162 int
   1163 lfs_flush_dirops(struct lfs *fs)
   1164 {
   1165 	struct inode *ip, *nip;
   1166 	struct vnode *vp;
   1167 	extern int lfs_dostats;
   1168 	struct segment *sp;
   1169 	int flags = 0;
   1170 	int error = 0;
   1171 
   1172 	ASSERT_MAYBE_SEGLOCK(fs);
   1173 	KASSERT(fs->lfs_nadirop == 0);
   1174 
   1175 	if (fs->lfs_ronly)
   1176 		return EROFS;
   1177 
   1178 	mutex_enter(&lfs_lock);
   1179 	if (TAILQ_FIRST(&fs->lfs_dchainhd) == NULL) {
   1180 		mutex_exit(&lfs_lock);
   1181 		return 0;
   1182 	} else
   1183 		mutex_exit(&lfs_lock);
   1184 
   1185 	if (lfs_dostats)
   1186 		++lfs_stats.flush_invoked;
   1187 
   1188 	lfs_imtime(fs);
   1189 	lfs_seglock(fs, flags);
   1190 	sp = fs->lfs_sp;
   1191 
   1192 	/*
   1193 	 * lfs_writevnodes, optimized to get dirops out of the way.
   1194 	 * Only write dirops, and don't flush files' pages, only
   1195 	 * blocks from the directories.
   1196 	 *
   1197 	 * We don't need to vref these files because they are
   1198 	 * dirops and so hold an extra reference until the
   1199 	 * segunlock clears them of that status.
   1200 	 *
   1201 	 * We don't need to check for IN_ADIROP because we know that
   1202 	 * no dirops are active.
   1203 	 *
   1204 	 */
   1205 	mutex_enter(&lfs_lock);
   1206 	for (ip = TAILQ_FIRST(&fs->lfs_dchainhd); ip != NULL; ip = nip) {
   1207 		nip = TAILQ_NEXT(ip, i_lfs_dchain);
   1208 		mutex_exit(&lfs_lock);
   1209 		vp = ITOV(ip);
   1210 
   1211 		KASSERT((ip->i_flag & IN_ADIROP) == 0);
   1212 		KASSERT(vp->v_uflag & VU_DIROP);
   1213 		KASSERT(!(vp->v_iflag & VI_XLOCK));
   1214 
   1215 		/*
   1216 		 * All writes to directories come from dirops; all
   1217 		 * writes to files' direct blocks go through the page
   1218 		 * cache, which we're not touching.  Reads to files
   1219 		 * and/or directories will not be affected by writing
   1220 		 * directory blocks inodes and file inodes.  So we don't
   1221 		 * really need to lock.
   1222 		 */
   1223 		if (vp->v_iflag & VI_XLOCK) {
   1224 			mutex_enter(&lfs_lock);
   1225 			continue;
   1226 		}
   1227 		/* XXX see below
   1228 		 * waslocked = VOP_ISLOCKED(vp);
   1229 		 */
   1230 		if (vp->v_type != VREG &&
   1231 		    ((ip->i_flag & IN_ALLMOD) || !VPISEMPTY(vp))) {
   1232 			error = lfs_writefile(fs, sp, vp);
   1233 			if (!VPISEMPTY(vp) && !WRITEINPROG(vp) &&
   1234 			    !(ip->i_flag & IN_ALLMOD)) {
   1235 			    	mutex_enter(&lfs_lock);
   1236 				LFS_SET_UINO(ip, IN_MODIFIED);
   1237 			    	mutex_exit(&lfs_lock);
   1238 			}
   1239 			if (error && (sp->seg_flags & SEGM_SINGLE)) {
   1240 				mutex_enter(&lfs_lock);
   1241 				error = EAGAIN;
   1242 				break;
   1243 			}
   1244 		}
   1245 		KDASSERT(ip->i_number != LFS_IFILE_INUM);
   1246 		error = lfs_writeinode(fs, sp, ip);
   1247 		mutex_enter(&lfs_lock);
   1248 		if (error && (sp->seg_flags & SEGM_SINGLE)) {
   1249 			error = EAGAIN;
   1250 			break;
   1251 		}
   1252 
   1253 		/*
   1254 		 * We might need to update these inodes again,
   1255 		 * for example, if they have data blocks to write.
   1256 		 * Make sure that after this flush, they are still
   1257 		 * marked IN_MODIFIED so that we don't forget to
   1258 		 * write them.
   1259 		 */
   1260 		/* XXX only for non-directories? --KS */
   1261 		LFS_SET_UINO(ip, IN_MODIFIED);
   1262 	}
   1263 	mutex_exit(&lfs_lock);
   1264 	/* We've written all the dirops there are */
   1265 	((SEGSUM *)(sp->segsum))->ss_flags &= ~(SS_CONT);
   1266 	lfs_finalize_fs_seguse(fs);
   1267 	(void) lfs_writeseg(fs, sp);
   1268 	lfs_segunlock(fs);
   1269 
   1270 	return error;
   1271 }
   1272 
   1273 /*
   1274  * Flush all vnodes for which the pagedaemon has requested pageouts.
   1275  * Skip over any files that are marked VU_DIROP (since lfs_flush_dirop()
   1276  * has just run, this would be an error).  If we have to skip a vnode
   1277  * for any reason, just skip it; if we have to wait for the cleaner,
   1278  * abort.  The writer daemon will call us again later.
   1279  */
   1280 int
   1281 lfs_flush_pchain(struct lfs *fs)
   1282 {
   1283 	struct inode *ip, *nip;
   1284 	struct vnode *vp;
   1285 	extern int lfs_dostats;
   1286 	struct segment *sp;
   1287 	int error, error2;
   1288 
   1289 	ASSERT_NO_SEGLOCK(fs);
   1290 
   1291 	if (fs->lfs_ronly)
   1292 		return EROFS;
   1293 
   1294 	mutex_enter(&lfs_lock);
   1295 	if (TAILQ_FIRST(&fs->lfs_pchainhd) == NULL) {
   1296 		mutex_exit(&lfs_lock);
   1297 		return 0;
   1298 	} else
   1299 		mutex_exit(&lfs_lock);
   1300 
   1301 	/* Get dirops out of the way */
   1302 	if ((error = lfs_flush_dirops(fs)) != 0)
   1303 		return error;
   1304 
   1305 	if (lfs_dostats)
   1306 		++lfs_stats.flush_invoked;
   1307 
   1308 	/*
   1309 	 * Inline lfs_segwrite/lfs_writevnodes, but just for pageouts.
   1310 	 */
   1311 	lfs_imtime(fs);
   1312 	lfs_seglock(fs, 0);
   1313 	sp = fs->lfs_sp;
   1314 
   1315 	/*
   1316 	 * lfs_writevnodes, optimized to clear pageout requests.
   1317 	 * Only write non-dirop files that are in the pageout queue.
   1318 	 * We're very conservative about what we write; we want to be
   1319 	 * fast and async.
   1320 	 */
   1321 	mutex_enter(&lfs_lock);
   1322     top:
   1323 	for (ip = TAILQ_FIRST(&fs->lfs_pchainhd); ip != NULL; ip = nip) {
   1324 		nip = TAILQ_NEXT(ip, i_lfs_pchain);
   1325 		vp = ITOV(ip);
   1326 
   1327 		if (!(ip->i_flags & IN_PAGING))
   1328 			goto top;
   1329 
   1330 		mutex_enter(vp->v_interlock);
   1331 		if ((vp->v_iflag & VI_XLOCK) || (vp->v_uflag & VU_DIROP) != 0) {
   1332 			mutex_exit(vp->v_interlock);
   1333 			continue;
   1334 		}
   1335 		if (vp->v_type != VREG) {
   1336 			mutex_exit(vp->v_interlock);
   1337 			continue;
   1338 		}
   1339 		if (lfs_vref(vp))
   1340 			continue;
   1341 		mutex_exit(&lfs_lock);
   1342 
   1343 		if (vn_lock(vp, LK_EXCLUSIVE | LK_NOWAIT | LK_RETRY) != 0) {
   1344 			lfs_vunref(vp);
   1345 			mutex_enter(&lfs_lock);
   1346 			continue;
   1347 		}
   1348 
   1349 		error = lfs_writefile(fs, sp, vp);
   1350 		if (!VPISEMPTY(vp) && !WRITEINPROG(vp) &&
   1351 		    !(ip->i_flag & IN_ALLMOD)) {
   1352 		    	mutex_enter(&lfs_lock);
   1353 			LFS_SET_UINO(ip, IN_MODIFIED);
   1354 		    	mutex_exit(&lfs_lock);
   1355 		}
   1356 		KDASSERT(ip->i_number != LFS_IFILE_INUM);
   1357 		error2 = lfs_writeinode(fs, sp, ip);
   1358 
   1359 		VOP_UNLOCK(vp);
   1360 		lfs_vunref(vp);
   1361 
   1362 		if (error == EAGAIN || error2 == EAGAIN) {
   1363 			lfs_writeseg(fs, sp);
   1364 			mutex_enter(&lfs_lock);
   1365 			break;
   1366 		}
   1367 		mutex_enter(&lfs_lock);
   1368 	}
   1369 	mutex_exit(&lfs_lock);
   1370 	(void) lfs_writeseg(fs, sp);
   1371 	lfs_segunlock(fs);
   1372 
   1373 	return 0;
   1374 }
   1375 
   1376 /*
   1377  * Provide a fcntl interface to sys_lfs_{segwait,bmapv,markv}.
   1378  */
   1379 int
   1380 lfs_fcntl(void *v)
   1381 {
   1382 	struct vop_fcntl_args /* {
   1383 		struct vnode *a_vp;
   1384 		u_int a_command;
   1385 		void * a_data;
   1386 		int  a_fflag;
   1387 		kauth_cred_t a_cred;
   1388 	} */ *ap = v;
   1389 	struct timeval tv;
   1390 	struct timeval *tvp;
   1391 	BLOCK_INFO *blkiov;
   1392 	CLEANERINFO *cip;
   1393 	SEGUSE *sup;
   1394 	int blkcnt, error, oclean;
   1395 	size_t fh_size;
   1396 	struct lfs_fcntl_markv blkvp;
   1397 	struct lwp *l;
   1398 	fsid_t *fsidp;
   1399 	struct lfs *fs;
   1400 	struct buf *bp;
   1401 	fhandle_t *fhp;
   1402 	daddr_t off;
   1403 
   1404 	/* Only respect LFS fcntls on fs root or Ifile */
   1405 	if (VTOI(ap->a_vp)->i_number != ULFS_ROOTINO &&
   1406 	    VTOI(ap->a_vp)->i_number != LFS_IFILE_INUM) {
   1407 		return ulfs_fcntl(v);
   1408 	}
   1409 
   1410 	/* Avoid locking a draining lock */
   1411 	if (ap->a_vp->v_mount->mnt_iflag & IMNT_UNMOUNT) {
   1412 		return ESHUTDOWN;
   1413 	}
   1414 
   1415 	/* LFS control and monitoring fcntls are available only to root */
   1416 	l = curlwp;
   1417 	if (((ap->a_command & 0xff00) >> 8) == 'L' &&
   1418 	    (error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_LFS,
   1419 	     KAUTH_REQ_SYSTEM_LFS_FCNTL, NULL, NULL, NULL)) != 0)
   1420 		return (error);
   1421 
   1422 	fs = VTOI(ap->a_vp)->i_lfs;
   1423 	fsidp = &ap->a_vp->v_mount->mnt_stat.f_fsidx;
   1424 
   1425 	error = 0;
   1426 	switch ((int)ap->a_command) {
   1427 	    case LFCNSEGWAITALL_COMPAT_50:
   1428 	    case LFCNSEGWAITALL_COMPAT:
   1429 		fsidp = NULL;
   1430 		/* FALLSTHROUGH */
   1431 	    case LFCNSEGWAIT_COMPAT_50:
   1432 	    case LFCNSEGWAIT_COMPAT:
   1433 		{
   1434 			struct timeval50 *tvp50
   1435 				= (struct timeval50 *)ap->a_data;
   1436 			timeval50_to_timeval(tvp50, &tv);
   1437 			tvp = &tv;
   1438 		}
   1439 		goto segwait_common;
   1440 	    case LFCNSEGWAITALL:
   1441 		fsidp = NULL;
   1442 		/* FALLSTHROUGH */
   1443 	    case LFCNSEGWAIT:
   1444 		tvp = (struct timeval *)ap->a_data;
   1445 segwait_common:
   1446 		mutex_enter(&lfs_lock);
   1447 		++fs->lfs_sleepers;
   1448 		mutex_exit(&lfs_lock);
   1449 
   1450 		error = lfs_segwait(fsidp, tvp);
   1451 
   1452 		mutex_enter(&lfs_lock);
   1453 		if (--fs->lfs_sleepers == 0)
   1454 			wakeup(&fs->lfs_sleepers);
   1455 		mutex_exit(&lfs_lock);
   1456 		return error;
   1457 
   1458 	    case LFCNBMAPV:
   1459 	    case LFCNMARKV:
   1460 		blkvp = *(struct lfs_fcntl_markv *)ap->a_data;
   1461 
   1462 		blkcnt = blkvp.blkcnt;
   1463 		if ((u_int) blkcnt > LFS_MARKV_MAXBLKCNT)
   1464 			return (EINVAL);
   1465 		blkiov = lfs_malloc(fs, blkcnt * sizeof(BLOCK_INFO), LFS_NB_BLKIOV);
   1466 		if ((error = copyin(blkvp.blkiov, blkiov,
   1467 		     blkcnt * sizeof(BLOCK_INFO))) != 0) {
   1468 			lfs_free(fs, blkiov, LFS_NB_BLKIOV);
   1469 			return error;
   1470 		}
   1471 
   1472 		mutex_enter(&lfs_lock);
   1473 		++fs->lfs_sleepers;
   1474 		mutex_exit(&lfs_lock);
   1475 		if (ap->a_command == LFCNBMAPV)
   1476 			error = lfs_bmapv(l->l_proc, fsidp, blkiov, blkcnt);
   1477 		else /* LFCNMARKV */
   1478 			error = lfs_markv(l->l_proc, fsidp, blkiov, blkcnt);
   1479 		if (error == 0)
   1480 			error = copyout(blkiov, blkvp.blkiov,
   1481 					blkcnt * sizeof(BLOCK_INFO));
   1482 		mutex_enter(&lfs_lock);
   1483 		if (--fs->lfs_sleepers == 0)
   1484 			wakeup(&fs->lfs_sleepers);
   1485 		mutex_exit(&lfs_lock);
   1486 		lfs_free(fs, blkiov, LFS_NB_BLKIOV);
   1487 		return error;
   1488 
   1489 	    case LFCNRECLAIM:
   1490 		/*
   1491 		 * Flush dirops and write Ifile, allowing empty segments
   1492 		 * to be immediately reclaimed.
   1493 		 */
   1494 		lfs_writer_enter(fs, "pndirop");
   1495 		off = fs->lfs_offset;
   1496 		lfs_seglock(fs, SEGM_FORCE_CKP | SEGM_CKP);
   1497 		lfs_flush_dirops(fs);
   1498 		LFS_CLEANERINFO(cip, fs, bp);
   1499 		oclean = cip->clean;
   1500 		LFS_SYNC_CLEANERINFO(cip, fs, bp, 1);
   1501 		lfs_segwrite(ap->a_vp->v_mount, SEGM_FORCE_CKP);
   1502 		fs->lfs_sp->seg_flags |= SEGM_PROT;
   1503 		lfs_segunlock(fs);
   1504 		lfs_writer_leave(fs);
   1505 
   1506 #ifdef DEBUG
   1507 		LFS_CLEANERINFO(cip, fs, bp);
   1508 		DLOG((DLOG_CLEAN, "lfs_fcntl: reclaim wrote %" PRId64
   1509 		      " blocks, cleaned %" PRId32 " segments (activesb %d)\n",
   1510 		      fs->lfs_offset - off, cip->clean - oclean,
   1511 		      fs->lfs_activesb));
   1512 		LFS_SYNC_CLEANERINFO(cip, fs, bp, 0);
   1513 #endif
   1514 
   1515 		return 0;
   1516 
   1517 	    case LFCNIFILEFH_COMPAT:
   1518 		/* Return the filehandle of the Ifile */
   1519 		if ((error = kauth_authorize_system(l->l_cred,
   1520 		    KAUTH_SYSTEM_FILEHANDLE, 0, NULL, NULL, NULL)) != 0)
   1521 			return (error);
   1522 		fhp = (struct fhandle *)ap->a_data;
   1523 		fhp->fh_fsid = *fsidp;
   1524 		fh_size = 16;	/* former VFS_MAXFIDSIZ */
   1525 		return lfs_vptofh(fs->lfs_ivnode, &(fhp->fh_fid), &fh_size);
   1526 
   1527 	    case LFCNIFILEFH_COMPAT2:
   1528 	    case LFCNIFILEFH:
   1529 		/* Return the filehandle of the Ifile */
   1530 		fhp = (struct fhandle *)ap->a_data;
   1531 		fhp->fh_fsid = *fsidp;
   1532 		fh_size = sizeof(struct lfs_fhandle) -
   1533 		    offsetof(fhandle_t, fh_fid);
   1534 		return lfs_vptofh(fs->lfs_ivnode, &(fhp->fh_fid), &fh_size);
   1535 
   1536 	    case LFCNREWIND:
   1537 		/* Move lfs_offset to the lowest-numbered segment */
   1538 		return lfs_rewind(fs, *(int *)ap->a_data);
   1539 
   1540 	    case LFCNINVAL:
   1541 		/* Mark a segment SEGUSE_INVAL */
   1542 		LFS_SEGENTRY(sup, fs, *(int *)ap->a_data, bp);
   1543 		if (sup->su_nbytes > 0) {
   1544 			brelse(bp, 0);
   1545 			lfs_unset_inval_all(fs);
   1546 			return EBUSY;
   1547 		}
   1548 		sup->su_flags |= SEGUSE_INVAL;
   1549 		VOP_BWRITE(bp->b_vp, bp);
   1550 		return 0;
   1551 
   1552 	    case LFCNRESIZE:
   1553 		/* Resize the filesystem */
   1554 		return lfs_resize_fs(fs, *(int *)ap->a_data);
   1555 
   1556 	    case LFCNWRAPSTOP:
   1557 	    case LFCNWRAPSTOP_COMPAT:
   1558 		/*
   1559 		 * Hold lfs_newseg at segment 0; if requested, sleep until
   1560 		 * the filesystem wraps around.  To support external agents
   1561 		 * (dump, fsck-based regression test) that need to look at
   1562 		 * a snapshot of the filesystem, without necessarily
   1563 		 * requiring that all fs activity stops.
   1564 		 */
   1565 		if (fs->lfs_stoplwp == curlwp)
   1566 			return EALREADY;
   1567 
   1568 		mutex_enter(&lfs_lock);
   1569 		while (fs->lfs_stoplwp != NULL)
   1570 			cv_wait(&fs->lfs_stopcv, &lfs_lock);
   1571 		fs->lfs_stoplwp = curlwp;
   1572 		if (fs->lfs_nowrap == 0)
   1573 			log(LOG_NOTICE, "%s: disabled log wrap\n", fs->lfs_fsmnt);
   1574 		++fs->lfs_nowrap;
   1575 		if (*(int *)ap->a_data == 1
   1576 		    || ap->a_command == LFCNWRAPSTOP_COMPAT) {
   1577 			log(LOG_NOTICE, "LFCNSTOPWRAP waiting for log wrap\n");
   1578 			error = mtsleep(&fs->lfs_nowrap, PCATCH | PUSER,
   1579 				"segwrap", 0, &lfs_lock);
   1580 			log(LOG_NOTICE, "LFCNSTOPWRAP done waiting\n");
   1581 			if (error) {
   1582 				lfs_wrapgo(fs, VTOI(ap->a_vp), 0);
   1583 			}
   1584 		}
   1585 		mutex_exit(&lfs_lock);
   1586 		return 0;
   1587 
   1588 	    case LFCNWRAPGO:
   1589 	    case LFCNWRAPGO_COMPAT:
   1590 		/*
   1591 		 * Having done its work, the agent wakes up the writer.
   1592 		 * If the argument is 1, it sleeps until a new segment
   1593 		 * is selected.
   1594 		 */
   1595 		mutex_enter(&lfs_lock);
   1596 		error = lfs_wrapgo(fs, VTOI(ap->a_vp),
   1597 				   ap->a_command == LFCNWRAPGO_COMPAT ? 1 :
   1598 				    *((int *)ap->a_data));
   1599 		mutex_exit(&lfs_lock);
   1600 		return error;
   1601 
   1602 	    case LFCNWRAPPASS:
   1603 		if ((VTOI(ap->a_vp)->i_lfs_iflags & LFSI_WRAPWAIT))
   1604 			return EALREADY;
   1605 		mutex_enter(&lfs_lock);
   1606 		if (fs->lfs_stoplwp != curlwp) {
   1607 			mutex_exit(&lfs_lock);
   1608 			return EALREADY;
   1609 		}
   1610 		if (fs->lfs_nowrap == 0) {
   1611 			mutex_exit(&lfs_lock);
   1612 			return EBUSY;
   1613 		}
   1614 		fs->lfs_wrappass = 1;
   1615 		wakeup(&fs->lfs_wrappass);
   1616 		/* Wait for the log to wrap, if asked */
   1617 		if (*(int *)ap->a_data) {
   1618 			mutex_enter(ap->a_vp->v_interlock);
   1619 			if (lfs_vref(ap->a_vp) != 0)
   1620 				panic("LFCNWRAPPASS: lfs_vref failed");
   1621 			VTOI(ap->a_vp)->i_lfs_iflags |= LFSI_WRAPWAIT;
   1622 			log(LOG_NOTICE, "LFCNPASS waiting for log wrap\n");
   1623 			error = mtsleep(&fs->lfs_nowrap, PCATCH | PUSER,
   1624 				"segwrap", 0, &lfs_lock);
   1625 			log(LOG_NOTICE, "LFCNPASS done waiting\n");
   1626 			VTOI(ap->a_vp)->i_lfs_iflags &= ~LFSI_WRAPWAIT;
   1627 			lfs_vunref(ap->a_vp);
   1628 		}
   1629 		mutex_exit(&lfs_lock);
   1630 		return error;
   1631 
   1632 	    case LFCNWRAPSTATUS:
   1633 		mutex_enter(&lfs_lock);
   1634 		*(int *)ap->a_data = fs->lfs_wrapstatus;
   1635 		mutex_exit(&lfs_lock);
   1636 		return 0;
   1637 
   1638 	    default:
   1639 		return ulfs_fcntl(v);
   1640 	}
   1641 	return 0;
   1642 }
   1643 
   1644 int
   1645 lfs_getpages(void *v)
   1646 {
   1647 	struct vop_getpages_args /* {
   1648 		struct vnode *a_vp;
   1649 		voff_t a_offset;
   1650 		struct vm_page **a_m;
   1651 		int *a_count;
   1652 		int a_centeridx;
   1653 		vm_prot_t a_access_type;
   1654 		int a_advice;
   1655 		int a_flags;
   1656 	} */ *ap = v;
   1657 
   1658 	if (VTOI(ap->a_vp)->i_number == LFS_IFILE_INUM &&
   1659 	    (ap->a_access_type & VM_PROT_WRITE) != 0) {
   1660 		return EPERM;
   1661 	}
   1662 	if ((ap->a_access_type & VM_PROT_WRITE) != 0) {
   1663 		mutex_enter(&lfs_lock);
   1664 		LFS_SET_UINO(VTOI(ap->a_vp), IN_MODIFIED);
   1665 		mutex_exit(&lfs_lock);
   1666 	}
   1667 
   1668 	/*
   1669 	 * we're relying on the fact that genfs_getpages() always read in
   1670 	 * entire filesystem blocks.
   1671 	 */
   1672 	return genfs_getpages(v);
   1673 }
   1674 
   1675 /*
   1676  * Wait for a page to become unbusy, possibly printing diagnostic messages
   1677  * as well.
   1678  *
   1679  * Called with vp->v_interlock held; return with it held.
   1680  */
   1681 static void
   1682 wait_for_page(struct vnode *vp, struct vm_page *pg, const char *label)
   1683 {
   1684 	KASSERT(mutex_owned(vp->v_interlock));
   1685 	if ((pg->flags & PG_BUSY) == 0)
   1686 		return;		/* Nothing to wait for! */
   1687 
   1688 #if defined(DEBUG) && defined(UVM_PAGE_TRKOWN)
   1689 	static struct vm_page *lastpg;
   1690 
   1691 	if (label != NULL && pg != lastpg) {
   1692 		if (pg->owner_tag) {
   1693 			printf("lfs_putpages[%d.%d]: %s: page %p owner %d.%d [%s]\n",
   1694 			       curproc->p_pid, curlwp->l_lid, label,
   1695 			       pg, pg->owner, pg->lowner, pg->owner_tag);
   1696 		} else {
   1697 			printf("lfs_putpages[%d.%d]: %s: page %p unowned?!\n",
   1698 			       curproc->p_pid, curlwp->l_lid, label, pg);
   1699 		}
   1700 	}
   1701 	lastpg = pg;
   1702 #endif
   1703 
   1704 	pg->flags |= PG_WANTED;
   1705 	UVM_UNLOCK_AND_WAIT(pg, vp->v_interlock, 0, "lfsput", 0);
   1706 	mutex_enter(vp->v_interlock);
   1707 }
   1708 
   1709 /*
   1710  * This routine is called by lfs_putpages() when it can't complete the
   1711  * write because a page is busy.  This means that either (1) someone,
   1712  * possibly the pagedaemon, is looking at this page, and will give it up
   1713  * presently; or (2) we ourselves are holding the page busy in the
   1714  * process of being written (either gathered or actually on its way to
   1715  * disk).  We don't need to give up the segment lock, but we might need
   1716  * to call lfs_writeseg() to expedite the page's journey to disk.
   1717  *
   1718  * Called with vp->v_interlock held; return with it held.
   1719  */
   1720 /* #define BUSYWAIT */
   1721 static void
   1722 write_and_wait(struct lfs *fs, struct vnode *vp, struct vm_page *pg,
   1723 	       int seglocked, const char *label)
   1724 {
   1725 	KASSERT(mutex_owned(vp->v_interlock));
   1726 #ifndef BUSYWAIT
   1727 	struct inode *ip = VTOI(vp);
   1728 	struct segment *sp = fs->lfs_sp;
   1729 	int count = 0;
   1730 
   1731 	if (pg == NULL)
   1732 		return;
   1733 
   1734 	while (pg->flags & PG_BUSY &&
   1735 	    pg->uobject == &vp->v_uobj) {
   1736 		mutex_exit(vp->v_interlock);
   1737 		if (sp->cbpp - sp->bpp > 1) {
   1738 			/* Write gathered pages */
   1739 			lfs_updatemeta(sp);
   1740 			lfs_release_finfo(fs);
   1741 			(void) lfs_writeseg(fs, sp);
   1742 
   1743 			/*
   1744 			 * Reinitialize FIP
   1745 			 */
   1746 			KASSERT(sp->vp == vp);
   1747 			lfs_acquire_finfo(fs, ip->i_number,
   1748 					  ip->i_gen);
   1749 		}
   1750 		++count;
   1751 		mutex_enter(vp->v_interlock);
   1752 		wait_for_page(vp, pg, label);
   1753 	}
   1754 	if (label != NULL && count > 1) {
   1755 		DLOG((DLOG_PAGE, "lfs_putpages[%d]: %s: %sn = %d\n",
   1756 		      curproc->p_pid, label, (count > 0 ? "looping, " : ""),
   1757 		      count));
   1758 	}
   1759 #else
   1760 	preempt(1);
   1761 #endif
   1762 	KASSERT(mutex_owned(vp->v_interlock));
   1763 }
   1764 
   1765 /*
   1766  * Make sure that for all pages in every block in the given range,
   1767  * either all are dirty or all are clean.  If any of the pages
   1768  * we've seen so far are dirty, put the vnode on the paging chain,
   1769  * and mark it IN_PAGING.
   1770  *
   1771  * If checkfirst != 0, don't check all the pages but return at the
   1772  * first dirty page.
   1773  */
   1774 static int
   1775 check_dirty(struct lfs *fs, struct vnode *vp,
   1776 	    off_t startoffset, off_t endoffset, off_t blkeof,
   1777 	    int flags, int checkfirst, struct vm_page **pgp)
   1778 {
   1779 	int by_list;
   1780 	struct vm_page *curpg = NULL; /* XXX: gcc */
   1781 	struct vm_page *pgs[MAXBSIZE / PAGE_SIZE], *pg;
   1782 	off_t soff = 0; /* XXX: gcc */
   1783 	voff_t off;
   1784 	int i;
   1785 	int nonexistent;
   1786 	int any_dirty;	/* number of dirty pages */
   1787 	int dirty;	/* number of dirty pages in a block */
   1788 	int tdirty;
   1789 	int pages_per_block = fs->lfs_bsize >> PAGE_SHIFT;
   1790 	int pagedaemon = (curlwp == uvm.pagedaemon_lwp);
   1791 
   1792 	KASSERT(mutex_owned(vp->v_interlock));
   1793 	ASSERT_MAYBE_SEGLOCK(fs);
   1794   top:
   1795 	by_list = (vp->v_uobj.uo_npages <=
   1796 		   ((endoffset - startoffset) >> PAGE_SHIFT) *
   1797 		   UVM_PAGE_TREE_PENALTY);
   1798 	any_dirty = 0;
   1799 
   1800 	if (by_list) {
   1801 		curpg = TAILQ_FIRST(&vp->v_uobj.memq);
   1802 	} else {
   1803 		soff = startoffset;
   1804 	}
   1805 	while (by_list || soff < MIN(blkeof, endoffset)) {
   1806 		if (by_list) {
   1807 			/*
   1808 			 * Find the first page in a block.  Skip
   1809 			 * blocks outside our area of interest or beyond
   1810 			 * the end of file.
   1811 			 */
   1812 			KASSERT(curpg == NULL
   1813 			    || (curpg->flags & PG_MARKER) == 0);
   1814 			if (pages_per_block > 1) {
   1815 				while (curpg &&
   1816 				    ((curpg->offset & fs->lfs_bmask) ||
   1817 				    curpg->offset >= vp->v_size ||
   1818 				    curpg->offset >= endoffset)) {
   1819 					curpg = TAILQ_NEXT(curpg, listq.queue);
   1820 					KASSERT(curpg == NULL ||
   1821 					    (curpg->flags & PG_MARKER) == 0);
   1822 				}
   1823 			}
   1824 			if (curpg == NULL)
   1825 				break;
   1826 			soff = curpg->offset;
   1827 		}
   1828 
   1829 		/*
   1830 		 * Mark all pages in extended range busy; find out if any
   1831 		 * of them are dirty.
   1832 		 */
   1833 		nonexistent = dirty = 0;
   1834 		for (i = 0; i == 0 || i < pages_per_block; i++) {
   1835 			KASSERT(mutex_owned(vp->v_interlock));
   1836 			if (by_list && pages_per_block <= 1) {
   1837 				pgs[i] = pg = curpg;
   1838 			} else {
   1839 				off = soff + (i << PAGE_SHIFT);
   1840 				pgs[i] = pg = uvm_pagelookup(&vp->v_uobj, off);
   1841 				if (pg == NULL) {
   1842 					++nonexistent;
   1843 					continue;
   1844 				}
   1845 			}
   1846 			KASSERT(pg != NULL);
   1847 
   1848 			/*
   1849 			 * If we're holding the segment lock, we can deadlock
   1850 			 * against a process that has our page and is waiting
   1851 			 * for the cleaner, while the cleaner waits for the
   1852 			 * segment lock.  Just bail in that case.
   1853 			 */
   1854 			if ((pg->flags & PG_BUSY) &&
   1855 			    (pagedaemon || LFS_SEGLOCK_HELD(fs))) {
   1856 				if (i > 0)
   1857 					uvm_page_unbusy(pgs, i);
   1858 				DLOG((DLOG_PAGE, "lfs_putpages: avoiding 3-way or pagedaemon deadlock\n"));
   1859 				if (pgp)
   1860 					*pgp = pg;
   1861 				KASSERT(mutex_owned(vp->v_interlock));
   1862 				return -1;
   1863 			}
   1864 
   1865 			while (pg->flags & PG_BUSY) {
   1866 				wait_for_page(vp, pg, NULL);
   1867 				KASSERT(mutex_owned(vp->v_interlock));
   1868 				if (i > 0)
   1869 					uvm_page_unbusy(pgs, i);
   1870 				KASSERT(mutex_owned(vp->v_interlock));
   1871 				goto top;
   1872 			}
   1873 			pg->flags |= PG_BUSY;
   1874 			UVM_PAGE_OWN(pg, "lfs_putpages");
   1875 
   1876 			pmap_page_protect(pg, VM_PROT_NONE);
   1877 			tdirty = (pmap_clear_modify(pg) ||
   1878 				  (pg->flags & PG_CLEAN) == 0);
   1879 			dirty += tdirty;
   1880 		}
   1881 		if (pages_per_block > 0 && nonexistent >= pages_per_block) {
   1882 			if (by_list) {
   1883 				curpg = TAILQ_NEXT(curpg, listq.queue);
   1884 			} else {
   1885 				soff += fs->lfs_bsize;
   1886 			}
   1887 			continue;
   1888 		}
   1889 
   1890 		any_dirty += dirty;
   1891 		KASSERT(nonexistent == 0);
   1892 		KASSERT(mutex_owned(vp->v_interlock));
   1893 
   1894 		/*
   1895 		 * If any are dirty make all dirty; unbusy them,
   1896 		 * but if we were asked to clean, wire them so that
   1897 		 * the pagedaemon doesn't bother us about them while
   1898 		 * they're on their way to disk.
   1899 		 */
   1900 		for (i = 0; i == 0 || i < pages_per_block; i++) {
   1901 			KASSERT(mutex_owned(vp->v_interlock));
   1902 			pg = pgs[i];
   1903 			KASSERT(!((pg->flags & PG_CLEAN) && (pg->flags & PG_DELWRI)));
   1904 			KASSERT(pg->flags & PG_BUSY);
   1905 			if (dirty) {
   1906 				pg->flags &= ~PG_CLEAN;
   1907 				if (flags & PGO_FREE) {
   1908 					/*
   1909 					 * Wire the page so that
   1910 					 * pdaemon doesn't see it again.
   1911 					 */
   1912 					mutex_enter(&uvm_pageqlock);
   1913 					uvm_pagewire(pg);
   1914 					mutex_exit(&uvm_pageqlock);
   1915 
   1916 					/* Suspended write flag */
   1917 					pg->flags |= PG_DELWRI;
   1918 				}
   1919 			}
   1920 			if (pg->flags & PG_WANTED)
   1921 				wakeup(pg);
   1922 			pg->flags &= ~(PG_WANTED|PG_BUSY);
   1923 			UVM_PAGE_OWN(pg, NULL);
   1924 		}
   1925 
   1926 		if (checkfirst && any_dirty)
   1927 			break;
   1928 
   1929 		if (by_list) {
   1930 			curpg = TAILQ_NEXT(curpg, listq.queue);
   1931 		} else {
   1932 			soff += MAX(PAGE_SIZE, fs->lfs_bsize);
   1933 		}
   1934 	}
   1935 
   1936 	KASSERT(mutex_owned(vp->v_interlock));
   1937 	return any_dirty;
   1938 }
   1939 
   1940 /*
   1941  * lfs_putpages functions like genfs_putpages except that
   1942  *
   1943  * (1) It needs to bounds-check the incoming requests to ensure that
   1944  *     they are block-aligned; if they are not, expand the range and
   1945  *     do the right thing in case, e.g., the requested range is clean
   1946  *     but the expanded range is dirty.
   1947  *
   1948  * (2) It needs to explicitly send blocks to be written when it is done.
   1949  *     If VOP_PUTPAGES is called without the seglock held, we simply take
   1950  *     the seglock and let lfs_segunlock wait for us.
   1951  *     XXX There might be a bad situation if we have to flush a vnode while
   1952  *     XXX lfs_markv is in operation.  As of this writing we panic in this
   1953  *     XXX case.
   1954  *
   1955  * Assumptions:
   1956  *
   1957  * (1) The caller does not hold any pages in this vnode busy.  If it does,
   1958  *     there is a danger that when we expand the page range and busy the
   1959  *     pages we will deadlock.
   1960  *
   1961  * (2) We are called with vp->v_interlock held; we must return with it
   1962  *     released.
   1963  *
   1964  * (3) We don't absolutely have to free pages right away, provided that
   1965  *     the request does not have PGO_SYNCIO.  When the pagedaemon gives
   1966  *     us a request with PGO_FREE, we take the pages out of the paging
   1967  *     queue and wake up the writer, which will handle freeing them for us.
   1968  *
   1969  *     We ensure that for any filesystem block, all pages for that
   1970  *     block are either resident or not, even if those pages are higher
   1971  *     than EOF; that means that we will be getting requests to free
   1972  *     "unused" pages above EOF all the time, and should ignore them.
   1973  *
   1974  * (4) If we are called with PGO_LOCKED, the finfo array we are to write
   1975  *     into has been set up for us by lfs_writefile.  If not, we will
   1976  *     have to handle allocating and/or freeing an finfo entry.
   1977  *
   1978  * XXX note that we're (ab)using PGO_LOCKED as "seglock held".
   1979  */
   1980 
   1981 /* How many times to loop before we should start to worry */
   1982 #define TOOMANY 4
   1983 
   1984 int
   1985 lfs_putpages(void *v)
   1986 {
   1987 	int error;
   1988 	struct vop_putpages_args /* {
   1989 		struct vnode *a_vp;
   1990 		voff_t a_offlo;
   1991 		voff_t a_offhi;
   1992 		int a_flags;
   1993 	} */ *ap = v;
   1994 	struct vnode *vp;
   1995 	struct inode *ip;
   1996 	struct lfs *fs;
   1997 	struct segment *sp;
   1998 	off_t origoffset, startoffset, endoffset, origendoffset, blkeof;
   1999 	off_t off, max_endoffset;
   2000 	bool seglocked, sync, pagedaemon, reclaim;
   2001 	struct vm_page *pg, *busypg;
   2002 	UVMHIST_FUNC("lfs_putpages"); UVMHIST_CALLED(ubchist);
   2003 	int oreclaim = 0;
   2004 	int donewriting = 0;
   2005 #ifdef DEBUG
   2006 	int debug_n_again, debug_n_dirtyclean;
   2007 #endif
   2008 
   2009 	vp = ap->a_vp;
   2010 	ip = VTOI(vp);
   2011 	fs = ip->i_lfs;
   2012 	sync = (ap->a_flags & PGO_SYNCIO) != 0;
   2013 	reclaim = (ap->a_flags & PGO_RECLAIM) != 0;
   2014 	pagedaemon = (curlwp == uvm.pagedaemon_lwp);
   2015 
   2016 	KASSERT(mutex_owned(vp->v_interlock));
   2017 
   2018 	/* Putpages does nothing for metadata. */
   2019 	if (vp == fs->lfs_ivnode || vp->v_type != VREG) {
   2020 		mutex_exit(vp->v_interlock);
   2021 		return 0;
   2022 	}
   2023 
   2024 	/*
   2025 	 * If there are no pages, don't do anything.
   2026 	 */
   2027 	if (vp->v_uobj.uo_npages == 0) {
   2028 		if (TAILQ_EMPTY(&vp->v_uobj.memq) &&
   2029 		    (vp->v_iflag & VI_ONWORKLST) &&
   2030 		    LIST_FIRST(&vp->v_dirtyblkhd) == NULL) {
   2031 			vp->v_iflag &= ~VI_WRMAPDIRTY;
   2032 			vn_syncer_remove_from_worklist(vp);
   2033 		}
   2034 		mutex_exit(vp->v_interlock);
   2035 
   2036 		/* Remove us from paging queue, if we were on it */
   2037 		mutex_enter(&lfs_lock);
   2038 		if (ip->i_flags & IN_PAGING) {
   2039 			ip->i_flags &= ~IN_PAGING;
   2040 			TAILQ_REMOVE(&fs->lfs_pchainhd, ip, i_lfs_pchain);
   2041 		}
   2042 		mutex_exit(&lfs_lock);
   2043 
   2044 		KASSERT(!mutex_owned(vp->v_interlock));
   2045 		return 0;
   2046 	}
   2047 
   2048 	blkeof = lfs_blkroundup(fs, ip->i_size);
   2049 
   2050 	/*
   2051 	 * Ignore requests to free pages past EOF but in the same block
   2052 	 * as EOF, unless the vnode is being reclaimed or the request
   2053 	 * is synchronous.  (If the request is sync, it comes from
   2054 	 * lfs_truncate.)
   2055 	 *
   2056 	 * To avoid being flooded with this request, make these pages
   2057 	 * look "active".
   2058 	 */
   2059 	if (!sync && !reclaim &&
   2060 	    ap->a_offlo >= ip->i_size && ap->a_offlo < blkeof) {
   2061 		origoffset = ap->a_offlo;
   2062 		for (off = origoffset; off < blkeof; off += fs->lfs_bsize) {
   2063 			pg = uvm_pagelookup(&vp->v_uobj, off);
   2064 			KASSERT(pg != NULL);
   2065 			while (pg->flags & PG_BUSY) {
   2066 				pg->flags |= PG_WANTED;
   2067 				UVM_UNLOCK_AND_WAIT(pg, vp->v_interlock, 0,
   2068 						    "lfsput2", 0);
   2069 				mutex_enter(vp->v_interlock);
   2070 			}
   2071 			mutex_enter(&uvm_pageqlock);
   2072 			uvm_pageactivate(pg);
   2073 			mutex_exit(&uvm_pageqlock);
   2074 		}
   2075 		ap->a_offlo = blkeof;
   2076 		if (ap->a_offhi > 0 && ap->a_offhi <= ap->a_offlo) {
   2077 			mutex_exit(vp->v_interlock);
   2078 			return 0;
   2079 		}
   2080 	}
   2081 
   2082 	/*
   2083 	 * Extend page range to start and end at block boundaries.
   2084 	 * (For the purposes of VOP_PUTPAGES, fragments don't exist.)
   2085 	 */
   2086 	origoffset = ap->a_offlo;
   2087 	origendoffset = ap->a_offhi;
   2088 	startoffset = origoffset & ~(fs->lfs_bmask);
   2089 	max_endoffset = (trunc_page(LLONG_MAX) >> fs->lfs_bshift)
   2090 					       << fs->lfs_bshift;
   2091 
   2092 	if (origendoffset == 0 || ap->a_flags & PGO_ALLPAGES) {
   2093 		endoffset = max_endoffset;
   2094 		origendoffset = endoffset;
   2095 	} else {
   2096 		origendoffset = round_page(ap->a_offhi);
   2097 		endoffset = round_page(lfs_blkroundup(fs, origendoffset));
   2098 	}
   2099 
   2100 	KASSERT(startoffset > 0 || endoffset >= startoffset);
   2101 	if (startoffset == endoffset) {
   2102 		/* Nothing to do, why were we called? */
   2103 		mutex_exit(vp->v_interlock);
   2104 		DLOG((DLOG_PAGE, "lfs_putpages: startoffset = endoffset = %"
   2105 		      PRId64 "\n", startoffset));
   2106 		return 0;
   2107 	}
   2108 
   2109 	ap->a_offlo = startoffset;
   2110 	ap->a_offhi = endoffset;
   2111 
   2112 	/*
   2113 	 * If not cleaning, just send the pages through genfs_putpages
   2114 	 * to be returned to the pool.
   2115 	 */
   2116 	if (!(ap->a_flags & PGO_CLEANIT)) {
   2117 		DLOG((DLOG_PAGE, "lfs_putpages: no cleanit vn %p ino %d (flags %x)\n",
   2118 		      vp, (int)ip->i_number, ap->a_flags));
   2119 		int r = genfs_putpages(v);
   2120 		KASSERT(!mutex_owned(vp->v_interlock));
   2121 		return r;
   2122 	}
   2123 
   2124 	/* Set PGO_BUSYFAIL to avoid deadlocks */
   2125 	ap->a_flags |= PGO_BUSYFAIL;
   2126 
   2127 	/*
   2128 	 * Likewise, if we are asked to clean but the pages are not
   2129 	 * dirty, we can just free them using genfs_putpages.
   2130 	 */
   2131 #ifdef DEBUG
   2132 	debug_n_dirtyclean = 0;
   2133 #endif
   2134 	do {
   2135 		int r;
   2136 		KASSERT(mutex_owned(vp->v_interlock));
   2137 
   2138 		/* Count the number of dirty pages */
   2139 		r = check_dirty(fs, vp, startoffset, endoffset, blkeof,
   2140 				ap->a_flags, 1, NULL);
   2141 		if (r < 0) {
   2142 			/* Pages are busy with another process */
   2143 			mutex_exit(vp->v_interlock);
   2144 			return EDEADLK;
   2145 		}
   2146 		if (r > 0) /* Some pages are dirty */
   2147 			break;
   2148 
   2149 		/*
   2150 		 * Sometimes pages are dirtied between the time that
   2151 		 * we check and the time we try to clean them.
   2152 		 * Instruct lfs_gop_write to return EDEADLK in this case
   2153 		 * so we can write them properly.
   2154 		 */
   2155 		ip->i_lfs_iflags |= LFSI_NO_GOP_WRITE;
   2156 		r = genfs_do_putpages(vp, startoffset, endoffset,
   2157 				       ap->a_flags & ~PGO_SYNCIO, &busypg);
   2158 		ip->i_lfs_iflags &= ~LFSI_NO_GOP_WRITE;
   2159 		if (r != EDEADLK) {
   2160 			KASSERT(!mutex_owned(vp->v_interlock));
   2161  			return r;
   2162 		}
   2163 
   2164 		/* One of the pages was busy.  Start over. */
   2165 		mutex_enter(vp->v_interlock);
   2166 		wait_for_page(vp, busypg, "dirtyclean");
   2167 #ifdef DEBUG
   2168 		++debug_n_dirtyclean;
   2169 #endif
   2170 	} while(1);
   2171 
   2172 #ifdef DEBUG
   2173 	if (debug_n_dirtyclean > TOOMANY)
   2174 		DLOG((DLOG_PAGE, "lfs_putpages: dirtyclean: looping, n = %d\n",
   2175 		      debug_n_dirtyclean));
   2176 #endif
   2177 
   2178 	/*
   2179 	 * Dirty and asked to clean.
   2180 	 *
   2181 	 * Pagedaemon can't actually write LFS pages; wake up
   2182 	 * the writer to take care of that.  The writer will
   2183 	 * notice the pager inode queue and act on that.
   2184 	 *
   2185 	 * XXX We must drop the vp->interlock before taking the lfs_lock or we
   2186 	 * get a nasty deadlock with lfs_flush_pchain().
   2187 	 */
   2188 	if (pagedaemon) {
   2189 		mutex_exit(vp->v_interlock);
   2190 		mutex_enter(&lfs_lock);
   2191 		if (!(ip->i_flags & IN_PAGING)) {
   2192 			ip->i_flags |= IN_PAGING;
   2193 			TAILQ_INSERT_TAIL(&fs->lfs_pchainhd, ip, i_lfs_pchain);
   2194 		}
   2195 		wakeup(&lfs_writer_daemon);
   2196 		mutex_exit(&lfs_lock);
   2197 		preempt();
   2198 		KASSERT(!mutex_owned(vp->v_interlock));
   2199 		return EWOULDBLOCK;
   2200 	}
   2201 
   2202 	/*
   2203 	 * If this is a file created in a recent dirop, we can't flush its
   2204 	 * inode until the dirop is complete.  Drain dirops, then flush the
   2205 	 * filesystem (taking care of any other pending dirops while we're
   2206 	 * at it).
   2207 	 */
   2208 	if ((ap->a_flags & (PGO_CLEANIT|PGO_LOCKED)) == PGO_CLEANIT &&
   2209 	    (vp->v_uflag & VU_DIROP)) {
   2210 		DLOG((DLOG_PAGE, "lfs_putpages: flushing VU_DIROP\n"));
   2211 
   2212  		lfs_writer_enter(fs, "ppdirop");
   2213 
   2214 		/* Note if we hold the vnode locked */
   2215 		if (VOP_ISLOCKED(vp) == LK_EXCLUSIVE)
   2216 		{
   2217 		    DLOG((DLOG_PAGE, "lfs_putpages: dirop inode already locked\n"));
   2218 		} else {
   2219 		    DLOG((DLOG_PAGE, "lfs_putpages: dirop inode not locked\n"));
   2220 		}
   2221 		mutex_exit(vp->v_interlock);
   2222 
   2223 		mutex_enter(&lfs_lock);
   2224 		lfs_flush_fs(fs, sync ? SEGM_SYNC : 0);
   2225 		mutex_exit(&lfs_lock);
   2226 
   2227 		mutex_enter(vp->v_interlock);
   2228 		lfs_writer_leave(fs);
   2229 
   2230 		/* The flush will have cleaned out this vnode as well,
   2231 		   no need to do more to it. */
   2232 	}
   2233 
   2234 	/*
   2235 	 * This is it.	We are going to write some pages.  From here on
   2236 	 * down it's all just mechanics.
   2237 	 *
   2238 	 * Don't let genfs_putpages wait; lfs_segunlock will wait for us.
   2239 	 */
   2240 	ap->a_flags &= ~PGO_SYNCIO;
   2241 
   2242 	/*
   2243 	 * If we've already got the seglock, flush the node and return.
   2244 	 * The FIP has already been set up for us by lfs_writefile,
   2245 	 * and FIP cleanup and lfs_updatemeta will also be done there,
   2246 	 * unless genfs_putpages returns EDEADLK; then we must flush
   2247 	 * what we have, and correct FIP and segment header accounting.
   2248 	 */
   2249   get_seglock:
   2250 	/*
   2251 	 * If we are not called with the segment locked, lock it.
   2252 	 * Account for a new FIP in the segment header, and set sp->vp.
   2253 	 * (This should duplicate the setup at the top of lfs_writefile().)
   2254 	 */
   2255 	seglocked = (ap->a_flags & PGO_LOCKED) != 0;
   2256 	if (!seglocked) {
   2257 		mutex_exit(vp->v_interlock);
   2258 		error = lfs_seglock(fs, SEGM_PROT | (sync ? SEGM_SYNC : 0));
   2259 		if (error != 0) {
   2260 			KASSERT(!mutex_owned(vp->v_interlock));
   2261  			return error;
   2262 		}
   2263 		mutex_enter(vp->v_interlock);
   2264 		lfs_acquire_finfo(fs, ip->i_number, ip->i_gen);
   2265 	}
   2266 	sp = fs->lfs_sp;
   2267 	KASSERT(sp->vp == NULL);
   2268 	sp->vp = vp;
   2269 
   2270 	/* Note segments written by reclaim; only for debugging */
   2271 	if ((vp->v_iflag & VI_XLOCK) != 0) {
   2272 		sp->seg_flags |= SEGM_RECLAIM;
   2273 		fs->lfs_reclino = ip->i_number;
   2274 	}
   2275 
   2276 	/*
   2277 	 * Ensure that the partial segment is marked SS_DIROP if this
   2278 	 * vnode is a DIROP.
   2279 	 */
   2280 	if (!seglocked && vp->v_uflag & VU_DIROP)
   2281 		((SEGSUM *)(sp->segsum))->ss_flags |= (SS_DIROP|SS_CONT);
   2282 
   2283 	/*
   2284 	 * Loop over genfs_putpages until all pages are gathered.
   2285 	 * genfs_putpages() drops the interlock, so reacquire it if necessary.
   2286 	 * Whenever we lose the interlock we have to rerun check_dirty, as
   2287 	 * well, since more pages might have been dirtied in our absence.
   2288 	 */
   2289 #ifdef DEBUG
   2290 	debug_n_again = 0;
   2291 #endif
   2292 	do {
   2293 		busypg = NULL;
   2294 		KASSERT(mutex_owned(vp->v_interlock));
   2295 		if (check_dirty(fs, vp, startoffset, endoffset, blkeof,
   2296 				ap->a_flags, 0, &busypg) < 0) {
   2297 			mutex_exit(vp->v_interlock);
   2298 			/* XXX why? --ks */
   2299 			mutex_enter(vp->v_interlock);
   2300 			write_and_wait(fs, vp, busypg, seglocked, NULL);
   2301 			if (!seglocked) {
   2302 				mutex_exit(vp->v_interlock);
   2303 				lfs_release_finfo(fs);
   2304 				lfs_segunlock(fs);
   2305 				mutex_enter(vp->v_interlock);
   2306 			}
   2307 			sp->vp = NULL;
   2308 			goto get_seglock;
   2309 		}
   2310 
   2311 		busypg = NULL;
   2312 		KASSERT(!mutex_owned(&uvm_pageqlock));
   2313 		oreclaim = (ap->a_flags & PGO_RECLAIM);
   2314 		ap->a_flags &= ~PGO_RECLAIM;
   2315 		error = genfs_do_putpages(vp, startoffset, endoffset,
   2316 					   ap->a_flags, &busypg);
   2317 		ap->a_flags |= oreclaim;
   2318 
   2319 		if (error == EDEADLK || error == EAGAIN) {
   2320 			DLOG((DLOG_PAGE, "lfs_putpages: genfs_putpages returned"
   2321 			      " %d ino %d off %x (seg %d)\n", error,
   2322 			      ip->i_number, fs->lfs_offset,
   2323 			      lfs_dtosn(fs, fs->lfs_offset)));
   2324 
   2325 			if (oreclaim) {
   2326 				mutex_enter(vp->v_interlock);
   2327 				write_and_wait(fs, vp, busypg, seglocked, "again");
   2328 				mutex_exit(vp->v_interlock);
   2329 			} else {
   2330 				if ((sp->seg_flags & SEGM_SINGLE) &&
   2331 				    fs->lfs_curseg != fs->lfs_startseg)
   2332 					donewriting = 1;
   2333 			}
   2334 		} else if (error) {
   2335 			DLOG((DLOG_PAGE, "lfs_putpages: genfs_putpages returned"
   2336 			      " %d ino %d off %x (seg %d)\n", error,
   2337 			      (int)ip->i_number, fs->lfs_offset,
   2338 			      lfs_dtosn(fs, fs->lfs_offset)));
   2339 		}
   2340 		/* genfs_do_putpages loses the interlock */
   2341 #ifdef DEBUG
   2342 		++debug_n_again;
   2343 #endif
   2344 		if (oreclaim && error == EAGAIN) {
   2345 			DLOG((DLOG_PAGE, "vp %p ino %d vi_flags %x a_flags %x avoiding vclean panic\n",
   2346 			      vp, (int)ip->i_number, vp->v_iflag, ap->a_flags));
   2347 			mutex_enter(vp->v_interlock);
   2348 		}
   2349 		if (error == EDEADLK)
   2350 			mutex_enter(vp->v_interlock);
   2351 	} while (error == EDEADLK || (oreclaim && error == EAGAIN));
   2352 #ifdef DEBUG
   2353 	if (debug_n_again > TOOMANY)
   2354 		DLOG((DLOG_PAGE, "lfs_putpages: again: looping, n = %d\n", debug_n_again));
   2355 #endif
   2356 
   2357 	KASSERT(sp != NULL && sp->vp == vp);
   2358 	if (!seglocked && !donewriting) {
   2359 		sp->vp = NULL;
   2360 
   2361 		/* Write indirect blocks as well */
   2362 		lfs_gather(fs, fs->lfs_sp, vp, lfs_match_indir);
   2363 		lfs_gather(fs, fs->lfs_sp, vp, lfs_match_dindir);
   2364 		lfs_gather(fs, fs->lfs_sp, vp, lfs_match_tindir);
   2365 
   2366 		KASSERT(sp->vp == NULL);
   2367 		sp->vp = vp;
   2368 	}
   2369 
   2370 	/*
   2371 	 * Blocks are now gathered into a segment waiting to be written.
   2372 	 * All that's left to do is update metadata, and write them.
   2373 	 */
   2374 	lfs_updatemeta(sp);
   2375 	KASSERT(sp->vp == vp);
   2376 	sp->vp = NULL;
   2377 
   2378 	/*
   2379 	 * If we were called from lfs_writefile, we don't need to clean up
   2380 	 * the FIP or unlock the segment lock.	We're done.
   2381 	 */
   2382 	if (seglocked) {
   2383 		KASSERT(!mutex_owned(vp->v_interlock));
   2384 		return error;
   2385 	}
   2386 
   2387 	/* Clean up FIP and send it to disk. */
   2388 	lfs_release_finfo(fs);
   2389 	lfs_writeseg(fs, fs->lfs_sp);
   2390 
   2391 	/*
   2392 	 * Remove us from paging queue if we wrote all our pages.
   2393 	 */
   2394 	if (origendoffset == 0 || ap->a_flags & PGO_ALLPAGES) {
   2395 		mutex_enter(&lfs_lock);
   2396 		if (ip->i_flags & IN_PAGING) {
   2397 			ip->i_flags &= ~IN_PAGING;
   2398 			TAILQ_REMOVE(&fs->lfs_pchainhd, ip, i_lfs_pchain);
   2399 		}
   2400 		mutex_exit(&lfs_lock);
   2401 	}
   2402 
   2403 	/*
   2404 	 * XXX - with the malloc/copy writeseg, the pages are freed by now
   2405 	 * even if we don't wait (e.g. if we hold a nested lock).  This
   2406 	 * will not be true if we stop using malloc/copy.
   2407 	 */
   2408 	KASSERT(fs->lfs_sp->seg_flags & SEGM_PROT);
   2409 	lfs_segunlock(fs);
   2410 
   2411 	/*
   2412 	 * Wait for v_numoutput to drop to zero.  The seglock should
   2413 	 * take care of this, but there is a slight possibility that
   2414 	 * aiodoned might not have got around to our buffers yet.
   2415 	 */
   2416 	if (sync) {
   2417 		mutex_enter(vp->v_interlock);
   2418 		while (vp->v_numoutput > 0) {
   2419 			DLOG((DLOG_PAGE, "lfs_putpages: ino %d sleeping on"
   2420 			      " num %d\n", ip->i_number, vp->v_numoutput));
   2421 			cv_wait(&vp->v_cv, vp->v_interlock);
   2422 		}
   2423 		mutex_exit(vp->v_interlock);
   2424 	}
   2425 	KASSERT(!mutex_owned(vp->v_interlock));
   2426 	return error;
   2427 }
   2428 
   2429 /*
   2430  * Return the last logical file offset that should be written for this file
   2431  * if we're doing a write that ends at "size".	If writing, we need to know
   2432  * about sizes on disk, i.e. fragments if there are any; if reading, we need
   2433  * to know about entire blocks.
   2434  */
   2435 void
   2436 lfs_gop_size(struct vnode *vp, off_t size, off_t *eobp, int flags)
   2437 {
   2438 	struct inode *ip = VTOI(vp);
   2439 	struct lfs *fs = ip->i_lfs;
   2440 	daddr_t olbn, nlbn;
   2441 
   2442 	olbn = lfs_lblkno(fs, ip->i_size);
   2443 	nlbn = lfs_lblkno(fs, size);
   2444 	if (!(flags & GOP_SIZE_MEM) && nlbn < ULFS_NDADDR && olbn <= nlbn) {
   2445 		*eobp = lfs_fragroundup(fs, size);
   2446 	} else {
   2447 		*eobp = lfs_blkroundup(fs, size);
   2448 	}
   2449 }
   2450 
   2451 #ifdef DEBUG
   2452 void lfs_dump_vop(void *);
   2453 
   2454 void
   2455 lfs_dump_vop(void *v)
   2456 {
   2457 	struct vop_putpages_args /* {
   2458 		struct vnode *a_vp;
   2459 		voff_t a_offlo;
   2460 		voff_t a_offhi;
   2461 		int a_flags;
   2462 	} */ *ap = v;
   2463 
   2464 #ifdef DDB
   2465 	vfs_vnode_print(ap->a_vp, 0, printf);
   2466 #endif
   2467 	lfs_dump_dinode(VTOI(ap->a_vp)->i_din.ffs1_din);
   2468 }
   2469 #endif
   2470 
   2471 int
   2472 lfs_mmap(void *v)
   2473 {
   2474 	struct vop_mmap_args /* {
   2475 		const struct vnodeop_desc *a_desc;
   2476 		struct vnode *a_vp;
   2477 		vm_prot_t a_prot;
   2478 		kauth_cred_t a_cred;
   2479 	} */ *ap = v;
   2480 
   2481 	if (VTOI(ap->a_vp)->i_number == LFS_IFILE_INUM)
   2482 		return EOPNOTSUPP;
   2483 	return ulfs_mmap(v);
   2484 }
   2485 
   2486 static int
   2487 lfs_openextattr(void *v)
   2488 {
   2489 	struct vop_openextattr_args /* {
   2490 		struct vnode *a_vp;
   2491 		kauth_cred_t a_cred;
   2492 		struct proc *a_p;
   2493 	} */ *ap = v;
   2494 	struct inode *ip = VTOI(ap->a_vp);
   2495 	struct ulfsmount *ump = ip->i_ump;
   2496 	//struct lfs *fs = ip->i_lfs;
   2497 
   2498 	/* Not supported for ULFS1 file systems. */
   2499 	if (ump->um_fstype == ULFS1)
   2500 		return (EOPNOTSUPP);
   2501 
   2502 	/* XXX Not implemented for ULFS2 file systems. */
   2503 	return (EOPNOTSUPP);
   2504 }
   2505 
   2506 static int
   2507 lfs_closeextattr(void *v)
   2508 {
   2509 	struct vop_closeextattr_args /* {
   2510 		struct vnode *a_vp;
   2511 		int a_commit;
   2512 		kauth_cred_t a_cred;
   2513 		struct proc *a_p;
   2514 	} */ *ap = v;
   2515 	struct inode *ip = VTOI(ap->a_vp);
   2516 	struct ulfsmount *ump = ip->i_ump;
   2517 	//struct lfs *fs = ip->i_lfs;
   2518 
   2519 	/* Not supported for ULFS1 file systems. */
   2520 	if (ump->um_fstype == ULFS1)
   2521 		return (EOPNOTSUPP);
   2522 
   2523 	/* XXX Not implemented for ULFS2 file systems. */
   2524 	return (EOPNOTSUPP);
   2525 }
   2526 
   2527 static int
   2528 lfs_getextattr(void *v)
   2529 {
   2530 	struct vop_getextattr_args /* {
   2531 		struct vnode *a_vp;
   2532 		int a_attrnamespace;
   2533 		const char *a_name;
   2534 		struct uio *a_uio;
   2535 		size_t *a_size;
   2536 		kauth_cred_t a_cred;
   2537 		struct proc *a_p;
   2538 	} */ *ap = v;
   2539 	struct vnode *vp = ap->a_vp;
   2540 	struct inode *ip = VTOI(vp);
   2541 	struct ulfsmount *ump = ip->i_ump;
   2542 	//struct lfs *fs = ip->i_lfs;
   2543 	int error;
   2544 
   2545 	if (ump->um_fstype == ULFS1) {
   2546 #ifdef LFS_EXTATTR
   2547 		fstrans_start(vp->v_mount, FSTRANS_SHARED);
   2548 		error = ulfs_getextattr(ap);
   2549 		fstrans_done(vp->v_mount);
   2550 #else
   2551 		error = EOPNOTSUPP;
   2552 #endif
   2553 		return error;
   2554 	}
   2555 
   2556 	/* XXX Not implemented for ULFS2 file systems. */
   2557 	return (EOPNOTSUPP);
   2558 }
   2559 
   2560 static int
   2561 lfs_setextattr(void *v)
   2562 {
   2563 	struct vop_setextattr_args /* {
   2564 		struct vnode *a_vp;
   2565 		int a_attrnamespace;
   2566 		const char *a_name;
   2567 		struct uio *a_uio;
   2568 		kauth_cred_t a_cred;
   2569 		struct proc *a_p;
   2570 	} */ *ap = v;
   2571 	struct vnode *vp = ap->a_vp;
   2572 	struct inode *ip = VTOI(vp);
   2573 	struct ulfsmount *ump = ip->i_ump;
   2574 	//struct lfs *fs = ip->i_lfs;
   2575 	int error;
   2576 
   2577 	if (ump->um_fstype == ULFS1) {
   2578 #ifdef LFS_EXTATTR
   2579 		fstrans_start(vp->v_mount, FSTRANS_SHARED);
   2580 		error = ulfs_setextattr(ap);
   2581 		fstrans_done(vp->v_mount);
   2582 #else
   2583 		error = EOPNOTSUPP;
   2584 #endif
   2585 		return error;
   2586 	}
   2587 
   2588 	/* XXX Not implemented for ULFS2 file systems. */
   2589 	return (EOPNOTSUPP);
   2590 }
   2591 
   2592 static int
   2593 lfs_listextattr(void *v)
   2594 {
   2595 	struct vop_listextattr_args /* {
   2596 		struct vnode *a_vp;
   2597 		int a_attrnamespace;
   2598 		struct uio *a_uio;
   2599 		size_t *a_size;
   2600 		kauth_cred_t a_cred;
   2601 		struct proc *a_p;
   2602 	} */ *ap = v;
   2603 	struct vnode *vp = ap->a_vp;
   2604 	struct inode *ip = VTOI(vp);
   2605 	struct ulfsmount *ump = ip->i_ump;
   2606 	//struct lfs *fs = ip->i_lfs;
   2607 	int error;
   2608 
   2609 	if (ump->um_fstype == ULFS1) {
   2610 #ifdef LFS_EXTATTR
   2611 		fstrans_start(vp->v_mount, FSTRANS_SHARED);
   2612 		error = ulfs_listextattr(ap);
   2613 		fstrans_done(vp->v_mount);
   2614 #else
   2615 		error = EOPNOTSUPP;
   2616 #endif
   2617 		return error;
   2618 	}
   2619 
   2620 	/* XXX Not implemented for ULFS2 file systems. */
   2621 	return (EOPNOTSUPP);
   2622 }
   2623 
   2624 static int
   2625 lfs_deleteextattr(void *v)
   2626 {
   2627 	struct vop_deleteextattr_args /* {
   2628 		struct vnode *a_vp;
   2629 		int a_attrnamespace;
   2630 		kauth_cred_t a_cred;
   2631 		struct proc *a_p;
   2632 	} */ *ap = v;
   2633 	struct vnode *vp = ap->a_vp;
   2634 	struct inode *ip = VTOI(vp);
   2635 	struct ulfsmount *ump = ip->i_ump;
   2636 	//struct fs *fs = ip->i_lfs;
   2637 	int error;
   2638 
   2639 	if (ump->um_fstype == ULFS1) {
   2640 #ifdef LFS_EXTATTR
   2641 		fstrans_start(vp->v_mount, FSTRANS_SHARED);
   2642 		error = ulfs_deleteextattr(ap);
   2643 		fstrans_done(vp->v_mount);
   2644 #else
   2645 		error = EOPNOTSUPP;
   2646 #endif
   2647 		return error;
   2648 	}
   2649 
   2650 	/* XXX Not implemented for ULFS2 file systems. */
   2651 	return (EOPNOTSUPP);
   2652 }
   2653