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