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