Home | History | Annotate | Line # | Download | only in lfs
lfs_vnops.c revision 1.263
      1 /*	$NetBSD: lfs_vnops.c,v 1.263 2014/05/16 09:34:03 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.263 2014/05/16 09:34:03 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 #define	LFS_READWRITE
    279 #include <ufs/lfs/ulfs_readwrite.c>
    280 #undef	LFS_READWRITE
    281 
    282 /*
    283  * Synch an open file.
    284  */
    285 /* ARGSUSED */
    286 int
    287 lfs_fsync(void *v)
    288 {
    289 	struct vop_fsync_args /* {
    290 		struct vnode *a_vp;
    291 		kauth_cred_t a_cred;
    292 		int a_flags;
    293 		off_t offlo;
    294 		off_t offhi;
    295 	} */ *ap = v;
    296 	struct vnode *vp = ap->a_vp;
    297 	int error, wait;
    298 	struct inode *ip = VTOI(vp);
    299 	struct lfs *fs = ip->i_lfs;
    300 
    301 	/* If we're mounted read-only, don't try to sync. */
    302 	if (fs->lfs_ronly)
    303 		return 0;
    304 
    305 	/* If a removed vnode is being cleaned, no need to sync here. */
    306 	if ((ap->a_flags & FSYNC_RECLAIM) != 0 && ip->i_mode == 0)
    307 		return 0;
    308 
    309 	/*
    310 	 * Trickle sync simply adds this vnode to the pager list, as if
    311 	 * the pagedaemon had requested a pageout.
    312 	 */
    313 	if (ap->a_flags & FSYNC_LAZY) {
    314 		if (lfs_ignore_lazy_sync == 0) {
    315 			mutex_enter(&lfs_lock);
    316 			if (!(ip->i_flags & IN_PAGING)) {
    317 				ip->i_flags |= IN_PAGING;
    318 				TAILQ_INSERT_TAIL(&fs->lfs_pchainhd, ip,
    319 						  i_lfs_pchain);
    320 			}
    321 			wakeup(&lfs_writer_daemon);
    322 			mutex_exit(&lfs_lock);
    323 		}
    324 		return 0;
    325 	}
    326 
    327 	/*
    328 	 * If a vnode is bring cleaned, flush it out before we try to
    329 	 * reuse it.  This prevents the cleaner from writing files twice
    330 	 * in the same partial segment, causing an accounting underflow.
    331 	 */
    332 	if (ap->a_flags & FSYNC_RECLAIM && ip->i_flags & IN_CLEANING) {
    333 		lfs_vflush(vp);
    334 	}
    335 
    336 	wait = (ap->a_flags & FSYNC_WAIT);
    337 	do {
    338 		mutex_enter(vp->v_interlock);
    339 		error = VOP_PUTPAGES(vp, trunc_page(ap->a_offlo),
    340 				     round_page(ap->a_offhi),
    341 				     PGO_CLEANIT | (wait ? PGO_SYNCIO : 0));
    342 		if (error == EAGAIN) {
    343 			mutex_enter(&lfs_lock);
    344 			mtsleep(&fs->lfs_avail, PCATCH | PUSER, "lfs_fsync",
    345 				hz / 100 + 1, &lfs_lock);
    346 			mutex_exit(&lfs_lock);
    347 		}
    348 	} while (error == EAGAIN);
    349 	if (error)
    350 		return error;
    351 
    352 	if ((ap->a_flags & FSYNC_DATAONLY) == 0)
    353 		error = lfs_update(vp, NULL, NULL, wait ? UPDATE_WAIT : 0);
    354 
    355 	if (error == 0 && ap->a_flags & FSYNC_CACHE) {
    356 		int l = 0;
    357 		error = VOP_IOCTL(ip->i_devvp, DIOCCACHESYNC, &l, FWRITE,
    358 				  curlwp->l_cred);
    359 	}
    360 	if (wait && !VPISEMPTY(vp))
    361 		LFS_SET_UINO(ip, IN_MODIFIED);
    362 
    363 	return error;
    364 }
    365 
    366 /*
    367  * Take IN_ADIROP off, then call ulfs_inactive.
    368  */
    369 int
    370 lfs_inactive(void *v)
    371 {
    372 	struct vop_inactive_args /* {
    373 		struct vnode *a_vp;
    374 	} */ *ap = v;
    375 
    376 	lfs_unmark_vnode(ap->a_vp);
    377 
    378 	/*
    379 	 * The Ifile is only ever inactivated on unmount.
    380 	 * Streamline this process by not giving it more dirty blocks.
    381 	 */
    382 	if (VTOI(ap->a_vp)->i_number == LFS_IFILE_INUM) {
    383 		mutex_enter(&lfs_lock);
    384 		LFS_CLR_UINO(VTOI(ap->a_vp), IN_ALLMOD);
    385 		mutex_exit(&lfs_lock);
    386 		VOP_UNLOCK(ap->a_vp);
    387 		return 0;
    388 	}
    389 
    390 #ifdef DEBUG
    391 	/*
    392 	 * This might happen on unmount.
    393 	 * XXX If it happens at any other time, it should be a panic.
    394 	 */
    395 	if (ap->a_vp->v_uflag & VU_DIROP) {
    396 		struct inode *ip = VTOI(ap->a_vp);
    397 		printf("lfs_inactive: inactivating VU_DIROP? ino = %d\n", (int)ip->i_number);
    398 	}
    399 #endif /* DIAGNOSTIC */
    400 
    401 	return ulfs_inactive(v);
    402 }
    403 
    404 int
    405 lfs_set_dirop(struct vnode *dvp, struct vnode *vp)
    406 {
    407 	struct lfs *fs;
    408 	int error;
    409 
    410 	KASSERT(VOP_ISLOCKED(dvp));
    411 	KASSERT(vp == NULL || VOP_ISLOCKED(vp));
    412 
    413 	fs = VTOI(dvp)->i_lfs;
    414 
    415 	ASSERT_NO_SEGLOCK(fs);
    416 	/*
    417 	 * LFS_NRESERVE calculates direct and indirect blocks as well
    418 	 * as an inode block; an overestimate in most cases.
    419 	 */
    420 	if ((error = lfs_reserve(fs, dvp, vp, LFS_NRESERVE(fs))) != 0)
    421 		return (error);
    422 
    423     restart:
    424 	mutex_enter(&lfs_lock);
    425 	if (fs->lfs_dirops == 0) {
    426 		mutex_exit(&lfs_lock);
    427 		lfs_check(dvp, LFS_UNUSED_LBN, 0);
    428 		mutex_enter(&lfs_lock);
    429 	}
    430 	while (fs->lfs_writer) {
    431 		error = mtsleep(&fs->lfs_dirops, (PRIBIO + 1) | PCATCH,
    432 		    "lfs_sdirop", 0, &lfs_lock);
    433 		if (error == EINTR) {
    434 			mutex_exit(&lfs_lock);
    435 			goto unreserve;
    436 		}
    437 	}
    438 	if (lfs_dirvcount > LFS_MAX_DIROP && fs->lfs_dirops == 0) {
    439 		wakeup(&lfs_writer_daemon);
    440 		mutex_exit(&lfs_lock);
    441 		preempt();
    442 		goto restart;
    443 	}
    444 
    445 	if (lfs_dirvcount > LFS_MAX_DIROP) {
    446 		DLOG((DLOG_DIROP, "lfs_set_dirop: sleeping with dirops=%d, "
    447 		      "dirvcount=%d\n", fs->lfs_dirops, lfs_dirvcount));
    448 		if ((error = mtsleep(&lfs_dirvcount,
    449 		    PCATCH | PUSER | PNORELOCK, "lfs_maxdirop", 0,
    450 		    &lfs_lock)) != 0) {
    451 			goto unreserve;
    452 		}
    453 		goto restart;
    454 	}
    455 
    456 	++fs->lfs_dirops;
    457 	/* fs->lfs_doifile = 1; */ /* XXX why? --ks */
    458 	mutex_exit(&lfs_lock);
    459 
    460 	/* Hold a reference so SET_ENDOP will be happy */
    461 	vref(dvp);
    462 	if (vp) {
    463 		vref(vp);
    464 		MARK_VNODE(vp);
    465 	}
    466 
    467 	MARK_VNODE(dvp);
    468 	return 0;
    469 
    470   unreserve:
    471 	lfs_reserve(fs, dvp, vp, -LFS_NRESERVE(fs));
    472 	return error;
    473 }
    474 
    475 /*
    476  * Get a new vnode *before* adjusting the dirop count, to avoid a deadlock
    477  * in getnewvnode(), if we have a stacked filesystem mounted on top
    478  * of us.
    479  *
    480  * NB: this means we have to clear the new vnodes on error.  Fortunately
    481  * SET_ENDOP is there to do that for us.
    482  */
    483 int
    484 lfs_set_dirop_create(struct vnode *dvp, struct vnode **vpp)
    485 {
    486 	int error;
    487 	struct lfs *fs;
    488 
    489 	fs = VFSTOULFS(dvp->v_mount)->um_lfs;
    490 	ASSERT_NO_SEGLOCK(fs);
    491 	if (fs->lfs_ronly)
    492 		return EROFS;
    493 	if (vpp == NULL) {
    494 		return lfs_set_dirop(dvp, NULL);
    495 	}
    496 	error = getnewvnode(VT_LFS, dvp->v_mount, lfs_vnodeop_p, NULL, vpp);
    497 	if (error) {
    498 		DLOG((DLOG_ALLOC, "lfs_set_dirop_create: dvp %p error %d\n",
    499 		      dvp, error));
    500 		return error;
    501 	}
    502 	if ((error = lfs_set_dirop(dvp, NULL)) != 0) {
    503 		ungetnewvnode(*vpp);
    504 		*vpp = NULL;
    505 		return error;
    506 	}
    507 	return 0;
    508 }
    509 
    510 void
    511 lfs_mark_vnode(struct vnode *vp)
    512 {
    513 	struct inode *ip = VTOI(vp);
    514 	struct lfs *fs = ip->i_lfs;
    515 
    516 	mutex_enter(&lfs_lock);
    517 	if (!(ip->i_flag & IN_ADIROP)) {
    518 		if (!(vp->v_uflag & VU_DIROP)) {
    519 			mutex_exit(&lfs_lock);
    520 			mutex_enter(vp->v_interlock);
    521 			if (lfs_vref(vp) != 0)
    522 				panic("lfs_mark_vnode: could not vref");
    523 			mutex_enter(&lfs_lock);
    524 			++lfs_dirvcount;
    525 			++fs->lfs_dirvcount;
    526 			TAILQ_INSERT_TAIL(&fs->lfs_dchainhd, ip, i_lfs_dchain);
    527 			vp->v_uflag |= VU_DIROP;
    528 		}
    529 		++fs->lfs_nadirop;
    530 		ip->i_flag &= ~IN_CDIROP;
    531 		ip->i_flag |= IN_ADIROP;
    532 	} else
    533 		KASSERT(vp->v_uflag & VU_DIROP);
    534 	mutex_exit(&lfs_lock);
    535 }
    536 
    537 void
    538 lfs_unmark_vnode(struct vnode *vp)
    539 {
    540 	struct inode *ip = VTOI(vp);
    541 
    542 	mutex_enter(&lfs_lock);
    543 	if (ip && (ip->i_flag & IN_ADIROP)) {
    544 		KASSERT(vp->v_uflag & VU_DIROP);
    545 		--ip->i_lfs->lfs_nadirop;
    546 		ip->i_flag &= ~IN_ADIROP;
    547 	}
    548 	mutex_exit(&lfs_lock);
    549 }
    550 
    551 int
    552 lfs_symlink(void *v)
    553 {
    554 	struct vop_symlink_v3_args /* {
    555 		struct vnode *a_dvp;
    556 		struct vnode **a_vpp;
    557 		struct componentname *a_cnp;
    558 		struct vattr *a_vap;
    559 		char *a_target;
    560 	} */ *ap = v;
    561 	int error;
    562 
    563 	if ((error = SET_DIROP_CREATE(ap->a_dvp, ap->a_vpp)) != 0) {
    564 		return error;
    565 	}
    566 	error = ulfs_symlink(ap);
    567 	SET_ENDOP_CREATE_AP(ap, "symlink");
    568 	return (error);
    569 }
    570 
    571 int
    572 lfs_mknod(void *v)
    573 {
    574 	struct vop_mknod_v3_args	/* {
    575 		struct vnode *a_dvp;
    576 		struct vnode **a_vpp;
    577 		struct componentname *a_cnp;
    578 		struct vattr *a_vap;
    579 	} */ *ap = v;
    580 	struct vattr *vap;
    581 	struct vnode **vpp;
    582 	struct inode *ip;
    583 	int error;
    584 	struct mount	*mp;
    585 	ino_t		ino;
    586 	struct ulfs_lookup_results *ulr;
    587 
    588 	vap = ap->a_vap;
    589 	vpp = ap->a_vpp;
    590 
    591 	/* XXX should handle this material another way */
    592 	ulr = &VTOI(ap->a_dvp)->i_crap;
    593 	ULFS_CHECK_CRAPCOUNTER(VTOI(ap->a_dvp));
    594 
    595 	if ((error = SET_DIROP_CREATE(ap->a_dvp, ap->a_vpp)) != 0) {
    596 		return error;
    597 	}
    598 
    599 	fstrans_start(ap->a_dvp->v_mount, FSTRANS_SHARED);
    600 	error = ulfs_makeinode(MAKEIMODE(vap->va_type, vap->va_mode),
    601 			      ap->a_dvp, ulr, vpp, ap->a_cnp);
    602 
    603 	/* Either way we're done with the dirop at this point */
    604 	SET_ENDOP_CREATE_AP(ap, "mknod");
    605 
    606 	if (error) {
    607 		fstrans_done(ap->a_dvp->v_mount);
    608 		*vpp = NULL;
    609 		return (error);
    610 	}
    611 
    612 	VN_KNOTE(ap->a_dvp, NOTE_WRITE);
    613 	ip = VTOI(*vpp);
    614 	mp  = (*vpp)->v_mount;
    615 	ino = ip->i_number;
    616 	ip->i_flag |= IN_ACCESS | IN_CHANGE | IN_UPDATE;
    617 	if (vap->va_rdev != VNOVAL) {
    618 		struct ulfsmount *ump = ip->i_ump;
    619 		struct lfs *fs = ip->i_lfs;
    620 		/*
    621 		 * Want to be able to use this to make badblock
    622 		 * inodes, so don't truncate the dev number.
    623 		 */
    624 		if (ump->um_fstype == ULFS1)
    625 			ip->i_ffs1_rdev = ulfs_rw32(vap->va_rdev,
    626 			    ULFS_MPNEEDSWAP(fs));
    627 		else
    628 			ip->i_ffs2_rdev = ulfs_rw64(vap->va_rdev,
    629 			    ULFS_MPNEEDSWAP(fs));
    630 	}
    631 
    632 	/*
    633 	 * Call fsync to write the vnode so that we don't have to deal with
    634 	 * flushing it when it's marked VU_DIROP or reclaiming.
    635 	 *
    636 	 * XXX KS - If we can't flush we also can't call vgone(), so must
    637 	 * return.  But, that leaves this vnode in limbo, also not good.
    638 	 * Can this ever happen (barring hardware failure)?
    639 	 */
    640 	if ((error = VOP_FSYNC(*vpp, NOCRED, FSYNC_WAIT, 0, 0)) != 0) {
    641 		panic("lfs_mknod: couldn't fsync (ino %llu)",
    642 		      (unsigned long long)ino);
    643 		/* return (error); */
    644 	}
    645 	/*
    646 	 * Remove vnode so that it will be reloaded by VFS_VGET and
    647 	 * checked to see if it is an alias of an existing entry in
    648 	 * the inode cache.
    649 	 */
    650 	/* Used to be vput, but that causes us to call VOP_INACTIVE twice. */
    651 
    652 	(*vpp)->v_type = VNON;
    653 	VOP_UNLOCK(*vpp);
    654 	vgone(*vpp);
    655 	error = VFS_VGET(mp, ino, vpp);
    656 
    657 	fstrans_done(ap->a_dvp->v_mount);
    658 	if (error != 0) {
    659 		*vpp = NULL;
    660 		return (error);
    661 	}
    662 	VOP_UNLOCK(*vpp);
    663 	return (0);
    664 }
    665 
    666 int
    667 lfs_create(void *v)
    668 {
    669 	struct vop_create_v3_args	/* {
    670 		struct vnode *a_dvp;
    671 		struct vnode **a_vpp;
    672 		struct componentname *a_cnp;
    673 		struct vattr *a_vap;
    674 	} */ *ap = v;
    675 	int error;
    676 
    677 	if ((error = SET_DIROP_CREATE(ap->a_dvp, ap->a_vpp)) != 0) {
    678 		return error;
    679 	}
    680 	error = ulfs_create(ap);
    681 	SET_ENDOP_CREATE_AP(ap, "create");
    682 	return (error);
    683 }
    684 
    685 int
    686 lfs_mkdir(void *v)
    687 {
    688 	struct vop_mkdir_v3_args	/* {
    689 		struct vnode *a_dvp;
    690 		struct vnode **a_vpp;
    691 		struct componentname *a_cnp;
    692 		struct vattr *a_vap;
    693 	} */ *ap = v;
    694 	int error;
    695 
    696 	if ((error = SET_DIROP_CREATE(ap->a_dvp, ap->a_vpp)) != 0) {
    697 		return error;
    698 	}
    699 	error = ulfs_mkdir(ap);
    700 	SET_ENDOP_CREATE_AP(ap, "mkdir");
    701 	return (error);
    702 }
    703 
    704 int
    705 lfs_remove(void *v)
    706 {
    707 	struct vop_remove_args	/* {
    708 		struct vnode *a_dvp;
    709 		struct vnode *a_vp;
    710 		struct componentname *a_cnp;
    711 	} */ *ap = v;
    712 	struct vnode *dvp, *vp;
    713 	struct inode *ip;
    714 	int error;
    715 
    716 	dvp = ap->a_dvp;
    717 	vp = ap->a_vp;
    718 	ip = VTOI(vp);
    719 	if ((error = SET_DIROP_REMOVE(dvp, vp)) != 0) {
    720 		if (dvp == vp)
    721 			vrele(vp);
    722 		else
    723 			vput(vp);
    724 		vput(dvp);
    725 		return error;
    726 	}
    727 	error = ulfs_remove(ap);
    728 	if (ip->i_nlink == 0)
    729 		lfs_orphan(ip->i_lfs, ip->i_number);
    730 	SET_ENDOP_REMOVE(ip->i_lfs, dvp, ap->a_vp, "remove");
    731 	return (error);
    732 }
    733 
    734 int
    735 lfs_rmdir(void *v)
    736 {
    737 	struct vop_rmdir_args	/* {
    738 		struct vnodeop_desc *a_desc;
    739 		struct vnode *a_dvp;
    740 		struct vnode *a_vp;
    741 		struct componentname *a_cnp;
    742 	} */ *ap = v;
    743 	struct vnode *vp;
    744 	struct inode *ip;
    745 	int error;
    746 
    747 	vp = ap->a_vp;
    748 	ip = VTOI(vp);
    749 	if ((error = SET_DIROP_REMOVE(ap->a_dvp, ap->a_vp)) != 0) {
    750 		if (ap->a_dvp == vp)
    751 			vrele(ap->a_dvp);
    752 		else
    753 			vput(ap->a_dvp);
    754 		vput(vp);
    755 		return error;
    756 	}
    757 	error = ulfs_rmdir(ap);
    758 	if (ip->i_nlink == 0)
    759 		lfs_orphan(ip->i_lfs, ip->i_number);
    760 	SET_ENDOP_REMOVE(ip->i_lfs, ap->a_dvp, ap->a_vp, "rmdir");
    761 	return (error);
    762 }
    763 
    764 int
    765 lfs_link(void *v)
    766 {
    767 	struct vop_link_args	/* {
    768 		struct vnode *a_dvp;
    769 		struct vnode *a_vp;
    770 		struct componentname *a_cnp;
    771 	} */ *ap = v;
    772 	int error;
    773 	struct vnode **vpp = NULL;
    774 
    775 	if ((error = SET_DIROP_CREATE(ap->a_dvp, vpp)) != 0) {
    776 		vput(ap->a_dvp);
    777 		return error;
    778 	}
    779 	error = ulfs_link(ap);
    780 	SET_ENDOP_CREATE(VTOI(ap->a_dvp)->i_lfs, ap->a_dvp, vpp, "link");
    781 	return (error);
    782 }
    783 
    784 /* XXX hack to avoid calling ITIMES in getattr */
    785 int
    786 lfs_getattr(void *v)
    787 {
    788 	struct vop_getattr_args /* {
    789 		struct vnode *a_vp;
    790 		struct vattr *a_vap;
    791 		kauth_cred_t a_cred;
    792 	} */ *ap = v;
    793 	struct vnode *vp = ap->a_vp;
    794 	struct inode *ip = VTOI(vp);
    795 	struct vattr *vap = ap->a_vap;
    796 	struct lfs *fs = ip->i_lfs;
    797 
    798 	fstrans_start(vp->v_mount, FSTRANS_SHARED);
    799 	/*
    800 	 * Copy from inode table
    801 	 */
    802 	vap->va_fsid = ip->i_dev;
    803 	vap->va_fileid = ip->i_number;
    804 	vap->va_mode = ip->i_mode & ~LFS_IFMT;
    805 	vap->va_nlink = ip->i_nlink;
    806 	vap->va_uid = ip->i_uid;
    807 	vap->va_gid = ip->i_gid;
    808 	vap->va_rdev = (dev_t)ip->i_ffs1_rdev;
    809 	vap->va_size = vp->v_size;
    810 	vap->va_atime.tv_sec = ip->i_ffs1_atime;
    811 	vap->va_atime.tv_nsec = ip->i_ffs1_atimensec;
    812 	vap->va_mtime.tv_sec = ip->i_ffs1_mtime;
    813 	vap->va_mtime.tv_nsec = ip->i_ffs1_mtimensec;
    814 	vap->va_ctime.tv_sec = ip->i_ffs1_ctime;
    815 	vap->va_ctime.tv_nsec = ip->i_ffs1_ctimensec;
    816 	vap->va_flags = ip->i_flags;
    817 	vap->va_gen = ip->i_gen;
    818 	/* this doesn't belong here */
    819 	if (vp->v_type == VBLK)
    820 		vap->va_blocksize = BLKDEV_IOSIZE;
    821 	else if (vp->v_type == VCHR)
    822 		vap->va_blocksize = MAXBSIZE;
    823 	else
    824 		vap->va_blocksize = vp->v_mount->mnt_stat.f_iosize;
    825 	vap->va_bytes = lfs_fsbtob(fs, (u_quad_t)ip->i_lfs_effnblks);
    826 	vap->va_type = vp->v_type;
    827 	vap->va_filerev = ip->i_modrev;
    828 	fstrans_done(vp->v_mount);
    829 	return (0);
    830 }
    831 
    832 /*
    833  * Check to make sure the inode blocks won't choke the buffer
    834  * cache, then call ulfs_setattr as usual.
    835  */
    836 int
    837 lfs_setattr(void *v)
    838 {
    839 	struct vop_setattr_args /* {
    840 		struct vnode *a_vp;
    841 		struct vattr *a_vap;
    842 		kauth_cred_t a_cred;
    843 	} */ *ap = v;
    844 	struct vnode *vp = ap->a_vp;
    845 
    846 	lfs_check(vp, LFS_UNUSED_LBN, 0);
    847 	return ulfs_setattr(v);
    848 }
    849 
    850 /*
    851  * Release the block we hold on lfs_newseg wrapping.  Called on file close,
    852  * or explicitly from LFCNWRAPGO.  Called with the interlock held.
    853  */
    854 static int
    855 lfs_wrapgo(struct lfs *fs, struct inode *ip, int waitfor)
    856 {
    857 	if (fs->lfs_stoplwp != curlwp)
    858 		return EBUSY;
    859 
    860 	fs->lfs_stoplwp = NULL;
    861 	cv_signal(&fs->lfs_stopcv);
    862 
    863 	KASSERT(fs->lfs_nowrap > 0);
    864 	if (fs->lfs_nowrap <= 0) {
    865 		return 0;
    866 	}
    867 
    868 	if (--fs->lfs_nowrap == 0) {
    869 		log(LOG_NOTICE, "%s: re-enabled log wrap\n", fs->lfs_fsmnt);
    870 		wakeup(&fs->lfs_wrappass);
    871 		lfs_wakeup_cleaner(fs);
    872 	}
    873 	if (waitfor) {
    874 		mtsleep(&fs->lfs_nextseg, PCATCH | PUSER, "segment",
    875 		    0, &lfs_lock);
    876 	}
    877 
    878 	return 0;
    879 }
    880 
    881 /*
    882  * Close called.
    883  *
    884  * Update the times on the inode.
    885  */
    886 /* ARGSUSED */
    887 int
    888 lfs_close(void *v)
    889 {
    890 	struct vop_close_args /* {
    891 		struct vnode *a_vp;
    892 		int  a_fflag;
    893 		kauth_cred_t a_cred;
    894 	} */ *ap = v;
    895 	struct vnode *vp = ap->a_vp;
    896 	struct inode *ip = VTOI(vp);
    897 	struct lfs *fs = ip->i_lfs;
    898 
    899 	if ((ip->i_number == ULFS_ROOTINO || ip->i_number == LFS_IFILE_INUM) &&
    900 	    fs->lfs_stoplwp == curlwp) {
    901 		mutex_enter(&lfs_lock);
    902 		log(LOG_NOTICE, "lfs_close: releasing log wrap control\n");
    903 		lfs_wrapgo(fs, ip, 0);
    904 		mutex_exit(&lfs_lock);
    905 	}
    906 
    907 	if (vp == ip->i_lfs->lfs_ivnode &&
    908 	    vp->v_mount->mnt_iflag & IMNT_UNMOUNT)
    909 		return 0;
    910 
    911 	fstrans_start(vp->v_mount, FSTRANS_SHARED);
    912 	if (vp->v_usecount > 1 && vp != ip->i_lfs->lfs_ivnode) {
    913 		LFS_ITIMES(ip, NULL, NULL, NULL);
    914 	}
    915 	fstrans_done(vp->v_mount);
    916 	return (0);
    917 }
    918 
    919 /*
    920  * Close wrapper for special devices.
    921  *
    922  * Update the times on the inode then do device close.
    923  */
    924 int
    925 lfsspec_close(void *v)
    926 {
    927 	struct vop_close_args /* {
    928 		struct vnode	*a_vp;
    929 		int		a_fflag;
    930 		kauth_cred_t	a_cred;
    931 	} */ *ap = v;
    932 	struct vnode	*vp;
    933 	struct inode	*ip;
    934 
    935 	vp = ap->a_vp;
    936 	ip = VTOI(vp);
    937 	if (vp->v_usecount > 1) {
    938 		LFS_ITIMES(ip, NULL, NULL, NULL);
    939 	}
    940 	return (VOCALL (spec_vnodeop_p, VOFFSET(vop_close), ap));
    941 }
    942 
    943 /*
    944  * Close wrapper for fifo's.
    945  *
    946  * Update the times on the inode then do device close.
    947  */
    948 int
    949 lfsfifo_close(void *v)
    950 {
    951 	struct vop_close_args /* {
    952 		struct vnode	*a_vp;
    953 		int		a_fflag;
    954 		kauth_cred_	a_cred;
    955 	} */ *ap = v;
    956 	struct vnode	*vp;
    957 	struct inode	*ip;
    958 
    959 	vp = ap->a_vp;
    960 	ip = VTOI(vp);
    961 	if (ap->a_vp->v_usecount > 1) {
    962 		LFS_ITIMES(ip, NULL, NULL, NULL);
    963 	}
    964 	return (VOCALL (fifo_vnodeop_p, VOFFSET(vop_close), ap));
    965 }
    966 
    967 /*
    968  * Reclaim an inode so that it can be used for other purposes.
    969  */
    970 
    971 int
    972 lfs_reclaim(void *v)
    973 {
    974 	struct vop_reclaim_args /* {
    975 		struct vnode *a_vp;
    976 	} */ *ap = v;
    977 	struct vnode *vp = ap->a_vp;
    978 	struct inode *ip = VTOI(vp);
    979 	struct lfs *fs = ip->i_lfs;
    980 	int error;
    981 
    982 	/*
    983 	 * The inode must be freed and updated before being removed
    984 	 * from its hash chain.  Other threads trying to gain a hold
    985 	 * or lock on the inode will be stalled.
    986 	 */
    987 	if (ip->i_nlink <= 0 && (vp->v_mount->mnt_flag & MNT_RDONLY) == 0)
    988 		lfs_vfree(vp, ip->i_number, ip->i_omode);
    989 
    990 	mutex_enter(&lfs_lock);
    991 	LFS_CLR_UINO(ip, IN_ALLMOD);
    992 	mutex_exit(&lfs_lock);
    993 	if ((error = ulfs_reclaim(vp)))
    994 		return (error);
    995 
    996 	/*
    997 	 * Take us off the paging and/or dirop queues if we were on them.
    998 	 * We shouldn't be on them.
    999 	 */
   1000 	mutex_enter(&lfs_lock);
   1001 	if (ip->i_flags & IN_PAGING) {
   1002 		log(LOG_WARNING, "%s: reclaimed vnode is IN_PAGING\n",
   1003 		    fs->lfs_fsmnt);
   1004 		ip->i_flags &= ~IN_PAGING;
   1005 		TAILQ_REMOVE(&fs->lfs_pchainhd, ip, i_lfs_pchain);
   1006 	}
   1007 	if (vp->v_uflag & VU_DIROP) {
   1008 		panic("reclaimed vnode is VU_DIROP");
   1009 		vp->v_uflag &= ~VU_DIROP;
   1010 		TAILQ_REMOVE(&fs->lfs_dchainhd, ip, i_lfs_dchain);
   1011 	}
   1012 	mutex_exit(&lfs_lock);
   1013 
   1014 	pool_put(&lfs_dinode_pool, ip->i_din.ffs1_din);
   1015 	lfs_deregister_all(vp);
   1016 	pool_put(&lfs_inoext_pool, ip->inode_ext.lfs);
   1017 	ip->inode_ext.lfs = NULL;
   1018 	genfs_node_destroy(vp);
   1019 	pool_put(&lfs_inode_pool, vp->v_data);
   1020 	vp->v_data = NULL;
   1021 	return (0);
   1022 }
   1023 
   1024 /*
   1025  * Read a block from a storage device.
   1026  *
   1027  * Calculate the logical to physical mapping if not done already,
   1028  * then call the device strategy routine.
   1029  *
   1030  * In order to avoid reading blocks that are in the process of being
   1031  * written by the cleaner---and hence are not mutexed by the normal
   1032  * buffer cache / page cache mechanisms---check for collisions before
   1033  * reading.
   1034  *
   1035  * We inline ulfs_strategy to make sure that the VOP_BMAP occurs *before*
   1036  * the active cleaner test.
   1037  *
   1038  * XXX This code assumes that lfs_markv makes synchronous checkpoints.
   1039  */
   1040 int
   1041 lfs_strategy(void *v)
   1042 {
   1043 	struct vop_strategy_args /* {
   1044 		struct vnode *a_vp;
   1045 		struct buf *a_bp;
   1046 	} */ *ap = v;
   1047 	struct buf	*bp;
   1048 	struct lfs	*fs;
   1049 	struct vnode	*vp;
   1050 	struct inode	*ip;
   1051 	daddr_t		tbn;
   1052 #define MAXLOOP 25
   1053 	int		i, sn, error, slept, loopcount;
   1054 
   1055 	bp = ap->a_bp;
   1056 	vp = ap->a_vp;
   1057 	ip = VTOI(vp);
   1058 	fs = ip->i_lfs;
   1059 
   1060 	/* lfs uses its strategy routine only for read */
   1061 	KASSERT(bp->b_flags & B_READ);
   1062 
   1063 	if (vp->v_type == VBLK || vp->v_type == VCHR)
   1064 		panic("lfs_strategy: spec");
   1065 	KASSERT(bp->b_bcount != 0);
   1066 	if (bp->b_blkno == bp->b_lblkno) {
   1067 		error = VOP_BMAP(vp, bp->b_lblkno, NULL, &bp->b_blkno,
   1068 				 NULL);
   1069 		if (error) {
   1070 			bp->b_error = error;
   1071 			bp->b_resid = bp->b_bcount;
   1072 			biodone(bp);
   1073 			return (error);
   1074 		}
   1075 		if ((long)bp->b_blkno == -1) /* no valid data */
   1076 			clrbuf(bp);
   1077 	}
   1078 	if ((long)bp->b_blkno < 0) { /* block is not on disk */
   1079 		bp->b_resid = bp->b_bcount;
   1080 		biodone(bp);
   1081 		return (0);
   1082 	}
   1083 
   1084 	slept = 1;
   1085 	loopcount = 0;
   1086 	mutex_enter(&lfs_lock);
   1087 	while (slept && fs->lfs_seglock) {
   1088 		mutex_exit(&lfs_lock);
   1089 		/*
   1090 		 * Look through list of intervals.
   1091 		 * There will only be intervals to look through
   1092 		 * if the cleaner holds the seglock.
   1093 		 * Since the cleaner is synchronous, we can trust
   1094 		 * the list of intervals to be current.
   1095 		 */
   1096 		tbn = LFS_DBTOFSB(fs, bp->b_blkno);
   1097 		sn = lfs_dtosn(fs, tbn);
   1098 		slept = 0;
   1099 		for (i = 0; i < fs->lfs_cleanind; i++) {
   1100 			if (sn == lfs_dtosn(fs, fs->lfs_cleanint[i]) &&
   1101 			    tbn >= fs->lfs_cleanint[i]) {
   1102 				DLOG((DLOG_CLEAN,
   1103 				      "lfs_strategy: ino %d lbn %" PRId64
   1104 				      " ind %d sn %d fsb %" PRIx32
   1105 				      " given sn %d fsb %" PRIx64 "\n",
   1106 				      ip->i_number, bp->b_lblkno, i,
   1107 				      lfs_dtosn(fs, fs->lfs_cleanint[i]),
   1108 				      fs->lfs_cleanint[i], sn, tbn));
   1109 				DLOG((DLOG_CLEAN,
   1110 				      "lfs_strategy: sleeping on ino %d lbn %"
   1111 				      PRId64 "\n", ip->i_number, bp->b_lblkno));
   1112 				mutex_enter(&lfs_lock);
   1113 				if (LFS_SEGLOCK_HELD(fs) && fs->lfs_iocount) {
   1114 					/*
   1115 					 * Cleaner can't wait for itself.
   1116 					 * Instead, wait for the blocks
   1117 					 * to be written to disk.
   1118 					 * XXX we need pribio in the test
   1119 					 * XXX here.
   1120 					 */
   1121  					mtsleep(&fs->lfs_iocount,
   1122  						(PRIBIO + 1) | PNORELOCK,
   1123 						"clean2", hz/10 + 1,
   1124  						&lfs_lock);
   1125 					slept = 1;
   1126 					++loopcount;
   1127 					break;
   1128 				} else if (fs->lfs_seglock) {
   1129 					mtsleep(&fs->lfs_seglock,
   1130 						(PRIBIO + 1) | PNORELOCK,
   1131 						"clean1", 0,
   1132 						&lfs_lock);
   1133 					slept = 1;
   1134 					break;
   1135 				}
   1136 				mutex_exit(&lfs_lock);
   1137 			}
   1138 		}
   1139 		mutex_enter(&lfs_lock);
   1140 		if (loopcount > MAXLOOP) {
   1141 			printf("lfs_strategy: breaking out of clean2 loop\n");
   1142 			break;
   1143 		}
   1144 	}
   1145 	mutex_exit(&lfs_lock);
   1146 
   1147 	vp = ip->i_devvp;
   1148 	return VOP_STRATEGY(vp, bp);
   1149 }
   1150 
   1151 /*
   1152  * Inline lfs_segwrite/lfs_writevnodes, but just for dirops.
   1153  * Technically this is a checkpoint (the on-disk state is valid)
   1154  * even though we are leaving out all the file data.
   1155  */
   1156 int
   1157 lfs_flush_dirops(struct lfs *fs)
   1158 {
   1159 	struct inode *ip, *nip;
   1160 	struct vnode *vp;
   1161 	extern int lfs_dostats;
   1162 	struct segment *sp;
   1163 	int flags = 0;
   1164 	int error = 0;
   1165 
   1166 	ASSERT_MAYBE_SEGLOCK(fs);
   1167 	KASSERT(fs->lfs_nadirop == 0);
   1168 
   1169 	if (fs->lfs_ronly)
   1170 		return EROFS;
   1171 
   1172 	mutex_enter(&lfs_lock);
   1173 	if (TAILQ_FIRST(&fs->lfs_dchainhd) == NULL) {
   1174 		mutex_exit(&lfs_lock);
   1175 		return 0;
   1176 	} else
   1177 		mutex_exit(&lfs_lock);
   1178 
   1179 	if (lfs_dostats)
   1180 		++lfs_stats.flush_invoked;
   1181 
   1182 	lfs_imtime(fs);
   1183 	lfs_seglock(fs, flags);
   1184 	sp = fs->lfs_sp;
   1185 
   1186 	/*
   1187 	 * lfs_writevnodes, optimized to get dirops out of the way.
   1188 	 * Only write dirops, and don't flush files' pages, only
   1189 	 * blocks from the directories.
   1190 	 *
   1191 	 * We don't need to vref these files because they are
   1192 	 * dirops and so hold an extra reference until the
   1193 	 * segunlock clears them of that status.
   1194 	 *
   1195 	 * We don't need to check for IN_ADIROP because we know that
   1196 	 * no dirops are active.
   1197 	 *
   1198 	 */
   1199 	mutex_enter(&lfs_lock);
   1200 	for (ip = TAILQ_FIRST(&fs->lfs_dchainhd); ip != NULL; ip = nip) {
   1201 		nip = TAILQ_NEXT(ip, i_lfs_dchain);
   1202 		mutex_exit(&lfs_lock);
   1203 		vp = ITOV(ip);
   1204 		mutex_enter(vp->v_interlock);
   1205 
   1206 		KASSERT((ip->i_flag & IN_ADIROP) == 0);
   1207 		KASSERT(vp->v_uflag & VU_DIROP);
   1208 		KASSERT(vdead_check(vp, VDEAD_NOWAIT) == 0);
   1209 
   1210 		/*
   1211 		 * All writes to directories come from dirops; all
   1212 		 * writes to files' direct blocks go through the page
   1213 		 * cache, which we're not touching.  Reads to files
   1214 		 * and/or directories will not be affected by writing
   1215 		 * directory blocks inodes and file inodes.  So we don't
   1216 		 * really need to lock.
   1217 		 */
   1218 		if (vdead_check(vp, VDEAD_NOWAIT) != 0) {
   1219 			mutex_exit(vp->v_interlock);
   1220 			mutex_enter(&lfs_lock);
   1221 			continue;
   1222 		}
   1223 		mutex_exit(vp->v_interlock);
   1224 		/* XXX see below
   1225 		 * waslocked = VOP_ISLOCKED(vp);
   1226 		 */
   1227 		if (vp->v_type != VREG &&
   1228 		    ((ip->i_flag & IN_ALLMOD) || !VPISEMPTY(vp))) {
   1229 			error = lfs_writefile(fs, sp, vp);
   1230 			if (!VPISEMPTY(vp) && !WRITEINPROG(vp) &&
   1231 			    !(ip->i_flag & IN_ALLMOD)) {
   1232 			    	mutex_enter(&lfs_lock);
   1233 				LFS_SET_UINO(ip, IN_MODIFIED);
   1234 			    	mutex_exit(&lfs_lock);
   1235 			}
   1236 			if (error && (sp->seg_flags & SEGM_SINGLE)) {
   1237 				mutex_enter(&lfs_lock);
   1238 				error = EAGAIN;
   1239 				break;
   1240 			}
   1241 		}
   1242 		KDASSERT(ip->i_number != LFS_IFILE_INUM);
   1243 		error = lfs_writeinode(fs, sp, ip);
   1244 		mutex_enter(&lfs_lock);
   1245 		if (error && (sp->seg_flags & SEGM_SINGLE)) {
   1246 			error = EAGAIN;
   1247 			break;
   1248 		}
   1249 
   1250 		/*
   1251 		 * We might need to update these inodes again,
   1252 		 * for example, if they have data blocks to write.
   1253 		 * Make sure that after this flush, they are still
   1254 		 * marked IN_MODIFIED so that we don't forget to
   1255 		 * write them.
   1256 		 */
   1257 		/* XXX only for non-directories? --KS */
   1258 		LFS_SET_UINO(ip, IN_MODIFIED);
   1259 	}
   1260 	mutex_exit(&lfs_lock);
   1261 	/* We've written all the dirops there are */
   1262 	((SEGSUM *)(sp->segsum))->ss_flags &= ~(SS_CONT);
   1263 	lfs_finalize_fs_seguse(fs);
   1264 	(void) lfs_writeseg(fs, sp);
   1265 	lfs_segunlock(fs);
   1266 
   1267 	return error;
   1268 }
   1269 
   1270 /*
   1271  * Flush all vnodes for which the pagedaemon has requested pageouts.
   1272  * Skip over any files that are marked VU_DIROP (since lfs_flush_dirop()
   1273  * has just run, this would be an error).  If we have to skip a vnode
   1274  * for any reason, just skip it; if we have to wait for the cleaner,
   1275  * abort.  The writer daemon will call us again later.
   1276  */
   1277 int
   1278 lfs_flush_pchain(struct lfs *fs)
   1279 {
   1280 	struct inode *ip, *nip;
   1281 	struct vnode *vp;
   1282 	extern int lfs_dostats;
   1283 	struct segment *sp;
   1284 	int error, error2;
   1285 
   1286 	ASSERT_NO_SEGLOCK(fs);
   1287 
   1288 	if (fs->lfs_ronly)
   1289 		return EROFS;
   1290 
   1291 	mutex_enter(&lfs_lock);
   1292 	if (TAILQ_FIRST(&fs->lfs_pchainhd) == NULL) {
   1293 		mutex_exit(&lfs_lock);
   1294 		return 0;
   1295 	} else
   1296 		mutex_exit(&lfs_lock);
   1297 
   1298 	/* Get dirops out of the way */
   1299 	if ((error = lfs_flush_dirops(fs)) != 0)
   1300 		return error;
   1301 
   1302 	if (lfs_dostats)
   1303 		++lfs_stats.flush_invoked;
   1304 
   1305 	/*
   1306 	 * Inline lfs_segwrite/lfs_writevnodes, but just for pageouts.
   1307 	 */
   1308 	lfs_imtime(fs);
   1309 	lfs_seglock(fs, 0);
   1310 	sp = fs->lfs_sp;
   1311 
   1312 	/*
   1313 	 * lfs_writevnodes, optimized to clear pageout requests.
   1314 	 * Only write non-dirop files that are in the pageout queue.
   1315 	 * We're very conservative about what we write; we want to be
   1316 	 * fast and async.
   1317 	 */
   1318 	mutex_enter(&lfs_lock);
   1319     top:
   1320 	for (ip = TAILQ_FIRST(&fs->lfs_pchainhd); ip != NULL; ip = nip) {
   1321 		nip = TAILQ_NEXT(ip, i_lfs_pchain);
   1322 		vp = ITOV(ip);
   1323 
   1324 		if (!(ip->i_flags & IN_PAGING))
   1325 			goto top;
   1326 
   1327 		mutex_enter(vp->v_interlock);
   1328 		if (vdead_check(vp, VDEAD_NOWAIT) != 0 ||
   1329 		    (vp->v_uflag & VU_DIROP) != 0) {
   1330 			mutex_exit(vp->v_interlock);
   1331 			continue;
   1332 		}
   1333 		if (vp->v_type != VREG) {
   1334 			mutex_exit(vp->v_interlock);
   1335 			continue;
   1336 		}
   1337 		if (lfs_vref(vp))
   1338 			continue;
   1339 		mutex_exit(&lfs_lock);
   1340 
   1341 		if (vn_lock(vp, LK_EXCLUSIVE | LK_NOWAIT | LK_RETRY) != 0) {
   1342 			lfs_vunref(vp);
   1343 			mutex_enter(&lfs_lock);
   1344 			continue;
   1345 		}
   1346 
   1347 		error = lfs_writefile(fs, sp, vp);
   1348 		if (!VPISEMPTY(vp) && !WRITEINPROG(vp) &&
   1349 		    !(ip->i_flag & IN_ALLMOD)) {
   1350 		    	mutex_enter(&lfs_lock);
   1351 			LFS_SET_UINO(ip, IN_MODIFIED);
   1352 		    	mutex_exit(&lfs_lock);
   1353 		}
   1354 		KDASSERT(ip->i_number != LFS_IFILE_INUM);
   1355 		error2 = lfs_writeinode(fs, sp, ip);
   1356 
   1357 		VOP_UNLOCK(vp);
   1358 		lfs_vunref(vp);
   1359 
   1360 		if (error == EAGAIN || error2 == EAGAIN) {
   1361 			lfs_writeseg(fs, sp);
   1362 			mutex_enter(&lfs_lock);
   1363 			break;
   1364 		}
   1365 		mutex_enter(&lfs_lock);
   1366 	}
   1367 	mutex_exit(&lfs_lock);
   1368 	(void) lfs_writeseg(fs, sp);
   1369 	lfs_segunlock(fs);
   1370 
   1371 	return 0;
   1372 }
   1373 
   1374 /*
   1375  * Provide a fcntl interface to sys_lfs_{segwait,bmapv,markv}.
   1376  */
   1377 int
   1378 lfs_fcntl(void *v)
   1379 {
   1380 	struct vop_fcntl_args /* {
   1381 		struct vnode *a_vp;
   1382 		u_int a_command;
   1383 		void * a_data;
   1384 		int  a_fflag;
   1385 		kauth_cred_t a_cred;
   1386 	} */ *ap = v;
   1387 	struct timeval tv;
   1388 	struct timeval *tvp;
   1389 	BLOCK_INFO *blkiov;
   1390 	CLEANERINFO *cip;
   1391 	SEGUSE *sup;
   1392 	int blkcnt, error;
   1393 	size_t fh_size;
   1394 	struct lfs_fcntl_markv blkvp;
   1395 	struct lwp *l;
   1396 	fsid_t *fsidp;
   1397 	struct lfs *fs;
   1398 	struct buf *bp;
   1399 	fhandle_t *fhp;
   1400 	daddr_t off;
   1401 	int oclean;
   1402 
   1403 	/* Only respect LFS fcntls on fs root or Ifile */
   1404 	if (VTOI(ap->a_vp)->i_number != ULFS_ROOTINO &&
   1405 	    VTOI(ap->a_vp)->i_number != LFS_IFILE_INUM) {
   1406 		return ulfs_fcntl(v);
   1407 	}
   1408 
   1409 	/* Avoid locking a draining lock */
   1410 	if (ap->a_vp->v_mount->mnt_iflag & IMNT_UNMOUNT) {
   1411 		return ESHUTDOWN;
   1412 	}
   1413 
   1414 	/* LFS control and monitoring fcntls are available only to root */
   1415 	l = curlwp;
   1416 	if (((ap->a_command & 0xff00) >> 8) == 'L' &&
   1417 	    (error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_LFS,
   1418 	     KAUTH_REQ_SYSTEM_LFS_FCNTL, NULL, NULL, NULL)) != 0)
   1419 		return (error);
   1420 
   1421 	fs = VTOI(ap->a_vp)->i_lfs;
   1422 	fsidp = &ap->a_vp->v_mount->mnt_stat.f_fsidx;
   1423 
   1424 	error = 0;
   1425 	switch ((int)ap->a_command) {
   1426 	    case LFCNSEGWAITALL_COMPAT_50:
   1427 	    case LFCNSEGWAITALL_COMPAT:
   1428 		fsidp = NULL;
   1429 		/* FALLSTHROUGH */
   1430 	    case LFCNSEGWAIT_COMPAT_50:
   1431 	    case LFCNSEGWAIT_COMPAT:
   1432 		{
   1433 			struct timeval50 *tvp50
   1434 				= (struct timeval50 *)ap->a_data;
   1435 			timeval50_to_timeval(tvp50, &tv);
   1436 			tvp = &tv;
   1437 		}
   1438 		goto segwait_common;
   1439 	    case LFCNSEGWAITALL:
   1440 		fsidp = NULL;
   1441 		/* FALLSTHROUGH */
   1442 	    case LFCNSEGWAIT:
   1443 		tvp = (struct timeval *)ap->a_data;
   1444 segwait_common:
   1445 		mutex_enter(&lfs_lock);
   1446 		++fs->lfs_sleepers;
   1447 		mutex_exit(&lfs_lock);
   1448 
   1449 		error = lfs_segwait(fsidp, tvp);
   1450 
   1451 		mutex_enter(&lfs_lock);
   1452 		if (--fs->lfs_sleepers == 0)
   1453 			wakeup(&fs->lfs_sleepers);
   1454 		mutex_exit(&lfs_lock);
   1455 		return error;
   1456 
   1457 	    case LFCNBMAPV:
   1458 	    case LFCNMARKV:
   1459 		blkvp = *(struct lfs_fcntl_markv *)ap->a_data;
   1460 
   1461 		blkcnt = blkvp.blkcnt;
   1462 		if ((u_int) blkcnt > LFS_MARKV_MAXBLKCNT)
   1463 			return (EINVAL);
   1464 		blkiov = lfs_malloc(fs, blkcnt * sizeof(BLOCK_INFO), LFS_NB_BLKIOV);
   1465 		if ((error = copyin(blkvp.blkiov, blkiov,
   1466 		     blkcnt * sizeof(BLOCK_INFO))) != 0) {
   1467 			lfs_free(fs, blkiov, LFS_NB_BLKIOV);
   1468 			return error;
   1469 		}
   1470 
   1471 		mutex_enter(&lfs_lock);
   1472 		++fs->lfs_sleepers;
   1473 		mutex_exit(&lfs_lock);
   1474 		if (ap->a_command == LFCNBMAPV)
   1475 			error = lfs_bmapv(l->l_proc, fsidp, blkiov, blkcnt);
   1476 		else /* LFCNMARKV */
   1477 			error = lfs_markv(l->l_proc, fsidp, blkiov, blkcnt);
   1478 		if (error == 0)
   1479 			error = copyout(blkiov, blkvp.blkiov,
   1480 					blkcnt * sizeof(BLOCK_INFO));
   1481 		mutex_enter(&lfs_lock);
   1482 		if (--fs->lfs_sleepers == 0)
   1483 			wakeup(&fs->lfs_sleepers);
   1484 		mutex_exit(&lfs_lock);
   1485 		lfs_free(fs, blkiov, LFS_NB_BLKIOV);
   1486 		return error;
   1487 
   1488 	    case LFCNRECLAIM:
   1489 		/*
   1490 		 * Flush dirops and write Ifile, allowing empty segments
   1491 		 * to be immediately reclaimed.
   1492 		 */
   1493 		lfs_writer_enter(fs, "pndirop");
   1494 		off = fs->lfs_offset;
   1495 		lfs_seglock(fs, SEGM_FORCE_CKP | SEGM_CKP);
   1496 		lfs_flush_dirops(fs);
   1497 		LFS_CLEANERINFO(cip, fs, bp);
   1498 		oclean = cip->clean;
   1499 		LFS_SYNC_CLEANERINFO(cip, fs, bp, 1);
   1500 		lfs_segwrite(ap->a_vp->v_mount, SEGM_FORCE_CKP);
   1501 		fs->lfs_sp->seg_flags |= SEGM_PROT;
   1502 		lfs_segunlock(fs);
   1503 		lfs_writer_leave(fs);
   1504 
   1505 #ifdef DEBUG
   1506 		LFS_CLEANERINFO(cip, fs, bp);
   1507 		DLOG((DLOG_CLEAN, "lfs_fcntl: reclaim wrote %" PRId64
   1508 		      " blocks, cleaned %" PRId32 " segments (activesb %d)\n",
   1509 		      fs->lfs_offset - off, cip->clean - oclean,
   1510 		      fs->lfs_activesb));
   1511 		LFS_SYNC_CLEANERINFO(cip, fs, bp, 0);
   1512 #else
   1513 		__USE(oclean);
   1514 		__USE(off);
   1515 #endif
   1516 
   1517 		return 0;
   1518 
   1519 	    case LFCNIFILEFH_COMPAT:
   1520 		/* Return the filehandle of the Ifile */
   1521 		if ((error = kauth_authorize_system(l->l_cred,
   1522 		    KAUTH_SYSTEM_FILEHANDLE, 0, NULL, NULL, NULL)) != 0)
   1523 			return (error);
   1524 		fhp = (struct fhandle *)ap->a_data;
   1525 		fhp->fh_fsid = *fsidp;
   1526 		fh_size = 16;	/* former VFS_MAXFIDSIZ */
   1527 		return lfs_vptofh(fs->lfs_ivnode, &(fhp->fh_fid), &fh_size);
   1528 
   1529 	    case LFCNIFILEFH_COMPAT2:
   1530 	    case LFCNIFILEFH:
   1531 		/* Return the filehandle of the Ifile */
   1532 		fhp = (struct fhandle *)ap->a_data;
   1533 		fhp->fh_fsid = *fsidp;
   1534 		fh_size = sizeof(struct lfs_fhandle) -
   1535 		    offsetof(fhandle_t, fh_fid);
   1536 		return lfs_vptofh(fs->lfs_ivnode, &(fhp->fh_fid), &fh_size);
   1537 
   1538 	    case LFCNREWIND:
   1539 		/* Move lfs_offset to the lowest-numbered segment */
   1540 		return lfs_rewind(fs, *(int *)ap->a_data);
   1541 
   1542 	    case LFCNINVAL:
   1543 		/* Mark a segment SEGUSE_INVAL */
   1544 		LFS_SEGENTRY(sup, fs, *(int *)ap->a_data, bp);
   1545 		if (sup->su_nbytes > 0) {
   1546 			brelse(bp, 0);
   1547 			lfs_unset_inval_all(fs);
   1548 			return EBUSY;
   1549 		}
   1550 		sup->su_flags |= SEGUSE_INVAL;
   1551 		VOP_BWRITE(bp->b_vp, bp);
   1552 		return 0;
   1553 
   1554 	    case LFCNRESIZE:
   1555 		/* Resize the filesystem */
   1556 		return lfs_resize_fs(fs, *(int *)ap->a_data);
   1557 
   1558 	    case LFCNWRAPSTOP:
   1559 	    case LFCNWRAPSTOP_COMPAT:
   1560 		/*
   1561 		 * Hold lfs_newseg at segment 0; if requested, sleep until
   1562 		 * the filesystem wraps around.  To support external agents
   1563 		 * (dump, fsck-based regression test) that need to look at
   1564 		 * a snapshot of the filesystem, without necessarily
   1565 		 * requiring that all fs activity stops.
   1566 		 */
   1567 		if (fs->lfs_stoplwp == curlwp)
   1568 			return EALREADY;
   1569 
   1570 		mutex_enter(&lfs_lock);
   1571 		while (fs->lfs_stoplwp != NULL)
   1572 			cv_wait(&fs->lfs_stopcv, &lfs_lock);
   1573 		fs->lfs_stoplwp = curlwp;
   1574 		if (fs->lfs_nowrap == 0)
   1575 			log(LOG_NOTICE, "%s: disabled log wrap\n", fs->lfs_fsmnt);
   1576 		++fs->lfs_nowrap;
   1577 		if (*(int *)ap->a_data == 1
   1578 		    || ap->a_command == LFCNWRAPSTOP_COMPAT) {
   1579 			log(LOG_NOTICE, "LFCNSTOPWRAP waiting for log wrap\n");
   1580 			error = mtsleep(&fs->lfs_nowrap, PCATCH | PUSER,
   1581 				"segwrap", 0, &lfs_lock);
   1582 			log(LOG_NOTICE, "LFCNSTOPWRAP done waiting\n");
   1583 			if (error) {
   1584 				lfs_wrapgo(fs, VTOI(ap->a_vp), 0);
   1585 			}
   1586 		}
   1587 		mutex_exit(&lfs_lock);
   1588 		return 0;
   1589 
   1590 	    case LFCNWRAPGO:
   1591 	    case LFCNWRAPGO_COMPAT:
   1592 		/*
   1593 		 * Having done its work, the agent wakes up the writer.
   1594 		 * If the argument is 1, it sleeps until a new segment
   1595 		 * is selected.
   1596 		 */
   1597 		mutex_enter(&lfs_lock);
   1598 		error = lfs_wrapgo(fs, VTOI(ap->a_vp),
   1599 				   ap->a_command == LFCNWRAPGO_COMPAT ? 1 :
   1600 				    *((int *)ap->a_data));
   1601 		mutex_exit(&lfs_lock);
   1602 		return error;
   1603 
   1604 	    case LFCNWRAPPASS:
   1605 		if ((VTOI(ap->a_vp)->i_lfs_iflags & LFSI_WRAPWAIT))
   1606 			return EALREADY;
   1607 		mutex_enter(&lfs_lock);
   1608 		if (fs->lfs_stoplwp != curlwp) {
   1609 			mutex_exit(&lfs_lock);
   1610 			return EALREADY;
   1611 		}
   1612 		if (fs->lfs_nowrap == 0) {
   1613 			mutex_exit(&lfs_lock);
   1614 			return EBUSY;
   1615 		}
   1616 		fs->lfs_wrappass = 1;
   1617 		wakeup(&fs->lfs_wrappass);
   1618 		/* Wait for the log to wrap, if asked */
   1619 		if (*(int *)ap->a_data) {
   1620 			mutex_enter(ap->a_vp->v_interlock);
   1621 			if (lfs_vref(ap->a_vp) != 0)
   1622 				panic("LFCNWRAPPASS: lfs_vref failed");
   1623 			VTOI(ap->a_vp)->i_lfs_iflags |= LFSI_WRAPWAIT;
   1624 			log(LOG_NOTICE, "LFCNPASS waiting for log wrap\n");
   1625 			error = mtsleep(&fs->lfs_nowrap, PCATCH | PUSER,
   1626 				"segwrap", 0, &lfs_lock);
   1627 			log(LOG_NOTICE, "LFCNPASS done waiting\n");
   1628 			VTOI(ap->a_vp)->i_lfs_iflags &= ~LFSI_WRAPWAIT;
   1629 			lfs_vunref(ap->a_vp);
   1630 		}
   1631 		mutex_exit(&lfs_lock);
   1632 		return error;
   1633 
   1634 	    case LFCNWRAPSTATUS:
   1635 		mutex_enter(&lfs_lock);
   1636 		*(int *)ap->a_data = fs->lfs_wrapstatus;
   1637 		mutex_exit(&lfs_lock);
   1638 		return 0;
   1639 
   1640 	    default:
   1641 		return ulfs_fcntl(v);
   1642 	}
   1643 	return 0;
   1644 }
   1645 
   1646 /*
   1647  * Return the last logical file offset that should be written for this file
   1648  * if we're doing a write that ends at "size".	If writing, we need to know
   1649  * about sizes on disk, i.e. fragments if there are any; if reading, we need
   1650  * to know about entire blocks.
   1651  */
   1652 void
   1653 lfs_gop_size(struct vnode *vp, off_t size, off_t *eobp, int flags)
   1654 {
   1655 	struct inode *ip = VTOI(vp);
   1656 	struct lfs *fs = ip->i_lfs;
   1657 	daddr_t olbn, nlbn;
   1658 
   1659 	olbn = lfs_lblkno(fs, ip->i_size);
   1660 	nlbn = lfs_lblkno(fs, size);
   1661 	if (!(flags & GOP_SIZE_MEM) && nlbn < ULFS_NDADDR && olbn <= nlbn) {
   1662 		*eobp = lfs_fragroundup(fs, size);
   1663 	} else {
   1664 		*eobp = lfs_blkroundup(fs, size);
   1665 	}
   1666 }
   1667 
   1668 #ifdef DEBUG
   1669 void lfs_dump_vop(void *);
   1670 
   1671 void
   1672 lfs_dump_vop(void *v)
   1673 {
   1674 	struct vop_putpages_args /* {
   1675 		struct vnode *a_vp;
   1676 		voff_t a_offlo;
   1677 		voff_t a_offhi;
   1678 		int a_flags;
   1679 	} */ *ap = v;
   1680 
   1681 #ifdef DDB
   1682 	vfs_vnode_print(ap->a_vp, 0, printf);
   1683 #endif
   1684 	lfs_dump_dinode(VTOI(ap->a_vp)->i_din.ffs1_din);
   1685 }
   1686 #endif
   1687 
   1688 int
   1689 lfs_mmap(void *v)
   1690 {
   1691 	struct vop_mmap_args /* {
   1692 		const struct vnodeop_desc *a_desc;
   1693 		struct vnode *a_vp;
   1694 		vm_prot_t a_prot;
   1695 		kauth_cred_t a_cred;
   1696 	} */ *ap = v;
   1697 
   1698 	if (VTOI(ap->a_vp)->i_number == LFS_IFILE_INUM)
   1699 		return EOPNOTSUPP;
   1700 	return ulfs_mmap(v);
   1701 }
   1702 
   1703 static int
   1704 lfs_openextattr(void *v)
   1705 {
   1706 	struct vop_openextattr_args /* {
   1707 		struct vnode *a_vp;
   1708 		kauth_cred_t a_cred;
   1709 		struct proc *a_p;
   1710 	} */ *ap = v;
   1711 	struct inode *ip = VTOI(ap->a_vp);
   1712 	struct ulfsmount *ump = ip->i_ump;
   1713 	//struct lfs *fs = ip->i_lfs;
   1714 
   1715 	/* Not supported for ULFS1 file systems. */
   1716 	if (ump->um_fstype == ULFS1)
   1717 		return (EOPNOTSUPP);
   1718 
   1719 	/* XXX Not implemented for ULFS2 file systems. */
   1720 	return (EOPNOTSUPP);
   1721 }
   1722 
   1723 static int
   1724 lfs_closeextattr(void *v)
   1725 {
   1726 	struct vop_closeextattr_args /* {
   1727 		struct vnode *a_vp;
   1728 		int a_commit;
   1729 		kauth_cred_t a_cred;
   1730 		struct proc *a_p;
   1731 	} */ *ap = v;
   1732 	struct inode *ip = VTOI(ap->a_vp);
   1733 	struct ulfsmount *ump = ip->i_ump;
   1734 	//struct lfs *fs = ip->i_lfs;
   1735 
   1736 	/* Not supported for ULFS1 file systems. */
   1737 	if (ump->um_fstype == ULFS1)
   1738 		return (EOPNOTSUPP);
   1739 
   1740 	/* XXX Not implemented for ULFS2 file systems. */
   1741 	return (EOPNOTSUPP);
   1742 }
   1743 
   1744 static int
   1745 lfs_getextattr(void *v)
   1746 {
   1747 	struct vop_getextattr_args /* {
   1748 		struct vnode *a_vp;
   1749 		int a_attrnamespace;
   1750 		const char *a_name;
   1751 		struct uio *a_uio;
   1752 		size_t *a_size;
   1753 		kauth_cred_t a_cred;
   1754 		struct proc *a_p;
   1755 	} */ *ap = v;
   1756 	struct vnode *vp = ap->a_vp;
   1757 	struct inode *ip = VTOI(vp);
   1758 	struct ulfsmount *ump = ip->i_ump;
   1759 	//struct lfs *fs = ip->i_lfs;
   1760 	int error;
   1761 
   1762 	if (ump->um_fstype == ULFS1) {
   1763 #ifdef LFS_EXTATTR
   1764 		fstrans_start(vp->v_mount, FSTRANS_SHARED);
   1765 		error = ulfs_getextattr(ap);
   1766 		fstrans_done(vp->v_mount);
   1767 #else
   1768 		error = EOPNOTSUPP;
   1769 #endif
   1770 		return error;
   1771 	}
   1772 
   1773 	/* XXX Not implemented for ULFS2 file systems. */
   1774 	return (EOPNOTSUPP);
   1775 }
   1776 
   1777 static int
   1778 lfs_setextattr(void *v)
   1779 {
   1780 	struct vop_setextattr_args /* {
   1781 		struct vnode *a_vp;
   1782 		int a_attrnamespace;
   1783 		const char *a_name;
   1784 		struct uio *a_uio;
   1785 		kauth_cred_t a_cred;
   1786 		struct proc *a_p;
   1787 	} */ *ap = v;
   1788 	struct vnode *vp = ap->a_vp;
   1789 	struct inode *ip = VTOI(vp);
   1790 	struct ulfsmount *ump = ip->i_ump;
   1791 	//struct lfs *fs = ip->i_lfs;
   1792 	int error;
   1793 
   1794 	if (ump->um_fstype == ULFS1) {
   1795 #ifdef LFS_EXTATTR
   1796 		fstrans_start(vp->v_mount, FSTRANS_SHARED);
   1797 		error = ulfs_setextattr(ap);
   1798 		fstrans_done(vp->v_mount);
   1799 #else
   1800 		error = EOPNOTSUPP;
   1801 #endif
   1802 		return error;
   1803 	}
   1804 
   1805 	/* XXX Not implemented for ULFS2 file systems. */
   1806 	return (EOPNOTSUPP);
   1807 }
   1808 
   1809 static int
   1810 lfs_listextattr(void *v)
   1811 {
   1812 	struct vop_listextattr_args /* {
   1813 		struct vnode *a_vp;
   1814 		int a_attrnamespace;
   1815 		struct uio *a_uio;
   1816 		size_t *a_size;
   1817 		kauth_cred_t a_cred;
   1818 		struct proc *a_p;
   1819 	} */ *ap = v;
   1820 	struct vnode *vp = ap->a_vp;
   1821 	struct inode *ip = VTOI(vp);
   1822 	struct ulfsmount *ump = ip->i_ump;
   1823 	//struct lfs *fs = ip->i_lfs;
   1824 	int error;
   1825 
   1826 	if (ump->um_fstype == ULFS1) {
   1827 #ifdef LFS_EXTATTR
   1828 		fstrans_start(vp->v_mount, FSTRANS_SHARED);
   1829 		error = ulfs_listextattr(ap);
   1830 		fstrans_done(vp->v_mount);
   1831 #else
   1832 		error = EOPNOTSUPP;
   1833 #endif
   1834 		return error;
   1835 	}
   1836 
   1837 	/* XXX Not implemented for ULFS2 file systems. */
   1838 	return (EOPNOTSUPP);
   1839 }
   1840 
   1841 static int
   1842 lfs_deleteextattr(void *v)
   1843 {
   1844 	struct vop_deleteextattr_args /* {
   1845 		struct vnode *a_vp;
   1846 		int a_attrnamespace;
   1847 		kauth_cred_t a_cred;
   1848 		struct proc *a_p;
   1849 	} */ *ap = v;
   1850 	struct vnode *vp = ap->a_vp;
   1851 	struct inode *ip = VTOI(vp);
   1852 	struct ulfsmount *ump = ip->i_ump;
   1853 	//struct fs *fs = ip->i_lfs;
   1854 	int error;
   1855 
   1856 	if (ump->um_fstype == ULFS1) {
   1857 #ifdef LFS_EXTATTR
   1858 		fstrans_start(vp->v_mount, FSTRANS_SHARED);
   1859 		error = ulfs_deleteextattr(ap);
   1860 		fstrans_done(vp->v_mount);
   1861 #else
   1862 		error = EOPNOTSUPP;
   1863 #endif
   1864 		return error;
   1865 	}
   1866 
   1867 	/* XXX Not implemented for ULFS2 file systems. */
   1868 	return (EOPNOTSUPP);
   1869 }
   1870