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