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