Home | History | Annotate | Line # | Download | only in lfs
lfs_vnops.c revision 1.157
      1 /*	$NetBSD: lfs_vnops.c,v 1.157 2005/12/11 12:25:26 christos Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1999, 2000, 2001, 2002, 2003 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Konrad E. Schroder <perseant (at) hhhh.org>.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *	This product includes software developed by the NetBSD
     21  *	Foundation, Inc. and its contributors.
     22  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  *    contributors may be used to endorse or promote products derived
     24  *    from this software without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  * POSSIBILITY OF SUCH DAMAGE.
     37  */
     38 /*
     39  * Copyright (c) 1986, 1989, 1991, 1993, 1995
     40  *	The Regents of the University of California.  All rights reserved.
     41  *
     42  * Redistribution and use in source and binary forms, with or without
     43  * modification, are permitted provided that the following conditions
     44  * are met:
     45  * 1. Redistributions of source code must retain the above copyright
     46  *    notice, this list of conditions and the following disclaimer.
     47  * 2. Redistributions in binary form must reproduce the above copyright
     48  *    notice, this list of conditions and the following disclaimer in the
     49  *    documentation and/or other materials provided with the distribution.
     50  * 3. Neither the name of the University nor the names of its contributors
     51  *    may be used to endorse or promote products derived from this software
     52  *    without specific prior written permission.
     53  *
     54  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     55  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     56  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     57  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     58  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     59  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     60  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     61  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     62  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     63  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     64  * SUCH DAMAGE.
     65  *
     66  *	@(#)lfs_vnops.c	8.13 (Berkeley) 6/10/95
     67  */
     68 
     69 #include <sys/cdefs.h>
     70 __KERNEL_RCSID(0, "$NetBSD: lfs_vnops.c,v 1.157 2005/12/11 12:25:26 christos Exp $");
     71 
     72 #include <sys/param.h>
     73 #include <sys/systm.h>
     74 #include <sys/namei.h>
     75 #include <sys/resourcevar.h>
     76 #include <sys/kernel.h>
     77 #include <sys/file.h>
     78 #include <sys/stat.h>
     79 #include <sys/buf.h>
     80 #include <sys/proc.h>
     81 #include <sys/mount.h>
     82 #include <sys/vnode.h>
     83 #include <sys/pool.h>
     84 #include <sys/signalvar.h>
     85 
     86 #include <miscfs/fifofs/fifo.h>
     87 #include <miscfs/genfs/genfs.h>
     88 #include <miscfs/specfs/specdev.h>
     89 
     90 #include <ufs/ufs/inode.h>
     91 #include <ufs/ufs/dir.h>
     92 #include <ufs/ufs/ufsmount.h>
     93 #include <ufs/ufs/ufs_extern.h>
     94 
     95 #include <uvm/uvm.h>
     96 #include <uvm/uvm_pmap.h>
     97 #include <uvm/uvm_stat.h>
     98 #include <uvm/uvm_pager.h>
     99 
    100 #include <ufs/lfs/lfs.h>
    101 #include <ufs/lfs/lfs_extern.h>
    102 
    103 extern pid_t lfs_writer_daemon;
    104 
    105 /* Global vfs data structures for lfs. */
    106 int (**lfs_vnodeop_p)(void *);
    107 const struct vnodeopv_entry_desc lfs_vnodeop_entries[] = {
    108 	{ &vop_default_desc, vn_default_error },
    109 	{ &vop_lookup_desc, ufs_lookup },		/* lookup */
    110 	{ &vop_create_desc, lfs_create },		/* create */
    111 	{ &vop_whiteout_desc, ufs_whiteout },		/* whiteout */
    112 	{ &vop_mknod_desc, lfs_mknod },			/* mknod */
    113 	{ &vop_open_desc, ufs_open },			/* open */
    114 	{ &vop_close_desc, lfs_close },			/* close */
    115 	{ &vop_access_desc, ufs_access },		/* access */
    116 	{ &vop_getattr_desc, lfs_getattr },		/* getattr */
    117 	{ &vop_setattr_desc, lfs_setattr },		/* setattr */
    118 	{ &vop_read_desc, lfs_read },			/* read */
    119 	{ &vop_write_desc, lfs_write },			/* write */
    120 	{ &vop_lease_desc, ufs_lease_check },		/* lease */
    121 	{ &vop_ioctl_desc, ufs_ioctl },			/* ioctl */
    122 	{ &vop_fcntl_desc, lfs_fcntl },			/* fcntl */
    123 	{ &vop_poll_desc, ufs_poll },			/* poll */
    124 	{ &vop_kqfilter_desc, genfs_kqfilter },		/* kqfilter */
    125 	{ &vop_revoke_desc, ufs_revoke },		/* revoke */
    126 	{ &vop_mmap_desc, lfs_mmap },			/* mmap */
    127 	{ &vop_fsync_desc, lfs_fsync },			/* fsync */
    128 	{ &vop_seek_desc, ufs_seek },			/* seek */
    129 	{ &vop_remove_desc, lfs_remove },		/* remove */
    130 	{ &vop_link_desc, lfs_link },			/* link */
    131 	{ &vop_rename_desc, lfs_rename },		/* rename */
    132 	{ &vop_mkdir_desc, lfs_mkdir },			/* mkdir */
    133 	{ &vop_rmdir_desc, lfs_rmdir },			/* rmdir */
    134 	{ &vop_symlink_desc, lfs_symlink },		/* symlink */
    135 	{ &vop_readdir_desc, ufs_readdir },		/* readdir */
    136 	{ &vop_readlink_desc, ufs_readlink },		/* readlink */
    137 	{ &vop_abortop_desc, ufs_abortop },		/* abortop */
    138 	{ &vop_inactive_desc, lfs_inactive },		/* inactive */
    139 	{ &vop_reclaim_desc, lfs_reclaim },		/* reclaim */
    140 	{ &vop_lock_desc, ufs_lock },			/* lock */
    141 	{ &vop_unlock_desc, ufs_unlock },		/* unlock */
    142 	{ &vop_bmap_desc, ufs_bmap },			/* bmap */
    143 	{ &vop_strategy_desc, lfs_strategy },		/* strategy */
    144 	{ &vop_print_desc, ufs_print },			/* print */
    145 	{ &vop_islocked_desc, ufs_islocked },		/* islocked */
    146 	{ &vop_pathconf_desc, ufs_pathconf },		/* pathconf */
    147 	{ &vop_advlock_desc, ufs_advlock },		/* advlock */
    148 	{ &vop_bwrite_desc, lfs_bwrite },		/* bwrite */
    149 	{ &vop_getpages_desc, lfs_getpages },		/* getpages */
    150 	{ &vop_putpages_desc, lfs_putpages },		/* putpages */
    151 	{ NULL, NULL }
    152 };
    153 const struct vnodeopv_desc lfs_vnodeop_opv_desc =
    154 	{ &lfs_vnodeop_p, lfs_vnodeop_entries };
    155 
    156 int (**lfs_specop_p)(void *);
    157 const struct vnodeopv_entry_desc lfs_specop_entries[] = {
    158 	{ &vop_default_desc, vn_default_error },
    159 	{ &vop_lookup_desc, spec_lookup },		/* lookup */
    160 	{ &vop_create_desc, spec_create },		/* create */
    161 	{ &vop_mknod_desc, spec_mknod },		/* mknod */
    162 	{ &vop_open_desc, spec_open },			/* open */
    163 	{ &vop_close_desc, lfsspec_close },		/* close */
    164 	{ &vop_access_desc, ufs_access },		/* access */
    165 	{ &vop_getattr_desc, lfs_getattr },		/* getattr */
    166 	{ &vop_setattr_desc, lfs_setattr },		/* setattr */
    167 	{ &vop_read_desc, ufsspec_read },		/* read */
    168 	{ &vop_write_desc, ufsspec_write },		/* write */
    169 	{ &vop_lease_desc, spec_lease_check },		/* lease */
    170 	{ &vop_ioctl_desc, spec_ioctl },		/* ioctl */
    171 	{ &vop_fcntl_desc, ufs_fcntl },			/* fcntl */
    172 	{ &vop_poll_desc, spec_poll },			/* poll */
    173 	{ &vop_kqfilter_desc, spec_kqfilter },		/* kqfilter */
    174 	{ &vop_revoke_desc, spec_revoke },		/* revoke */
    175 	{ &vop_mmap_desc, spec_mmap },			/* mmap */
    176 	{ &vop_fsync_desc, spec_fsync },		/* fsync */
    177 	{ &vop_seek_desc, spec_seek },			/* seek */
    178 	{ &vop_remove_desc, spec_remove },		/* remove */
    179 	{ &vop_link_desc, spec_link },			/* link */
    180 	{ &vop_rename_desc, spec_rename },		/* rename */
    181 	{ &vop_mkdir_desc, spec_mkdir },		/* mkdir */
    182 	{ &vop_rmdir_desc, spec_rmdir },		/* rmdir */
    183 	{ &vop_symlink_desc, spec_symlink },		/* symlink */
    184 	{ &vop_readdir_desc, spec_readdir },		/* readdir */
    185 	{ &vop_readlink_desc, spec_readlink },		/* readlink */
    186 	{ &vop_abortop_desc, spec_abortop },		/* abortop */
    187 	{ &vop_inactive_desc, lfs_inactive },		/* inactive */
    188 	{ &vop_reclaim_desc, lfs_reclaim },		/* reclaim */
    189 	{ &vop_lock_desc, ufs_lock },			/* lock */
    190 	{ &vop_unlock_desc, ufs_unlock },		/* unlock */
    191 	{ &vop_bmap_desc, spec_bmap },			/* bmap */
    192 	{ &vop_strategy_desc, spec_strategy },		/* strategy */
    193 	{ &vop_print_desc, ufs_print },			/* print */
    194 	{ &vop_islocked_desc, ufs_islocked },		/* islocked */
    195 	{ &vop_pathconf_desc, spec_pathconf },		/* pathconf */
    196 	{ &vop_advlock_desc, spec_advlock },		/* advlock */
    197 	{ &vop_bwrite_desc, vn_bwrite },		/* bwrite */
    198 	{ &vop_getpages_desc, spec_getpages },		/* getpages */
    199 	{ &vop_putpages_desc, spec_putpages },		/* putpages */
    200 	{ NULL, NULL }
    201 };
    202 const struct vnodeopv_desc lfs_specop_opv_desc =
    203 	{ &lfs_specop_p, lfs_specop_entries };
    204 
    205 int (**lfs_fifoop_p)(void *);
    206 const struct vnodeopv_entry_desc lfs_fifoop_entries[] = {
    207 	{ &vop_default_desc, vn_default_error },
    208 	{ &vop_lookup_desc, fifo_lookup },		/* lookup */
    209 	{ &vop_create_desc, fifo_create },		/* create */
    210 	{ &vop_mknod_desc, fifo_mknod },		/* mknod */
    211 	{ &vop_open_desc, fifo_open },			/* open */
    212 	{ &vop_close_desc, lfsfifo_close },		/* close */
    213 	{ &vop_access_desc, ufs_access },		/* access */
    214 	{ &vop_getattr_desc, lfs_getattr },		/* getattr */
    215 	{ &vop_setattr_desc, lfs_setattr },		/* setattr */
    216 	{ &vop_read_desc, ufsfifo_read },		/* read */
    217 	{ &vop_write_desc, ufsfifo_write },		/* write */
    218 	{ &vop_lease_desc, fifo_lease_check },		/* lease */
    219 	{ &vop_ioctl_desc, fifo_ioctl },		/* ioctl */
    220 	{ &vop_fcntl_desc, ufs_fcntl },			/* fcntl */
    221 	{ &vop_poll_desc, fifo_poll },			/* poll */
    222 	{ &vop_kqfilter_desc, fifo_kqfilter },		/* kqfilter */
    223 	{ &vop_revoke_desc, fifo_revoke },		/* revoke */
    224 	{ &vop_mmap_desc, fifo_mmap },			/* mmap */
    225 	{ &vop_fsync_desc, fifo_fsync },		/* fsync */
    226 	{ &vop_seek_desc, fifo_seek },			/* seek */
    227 	{ &vop_remove_desc, fifo_remove },		/* remove */
    228 	{ &vop_link_desc, fifo_link },			/* link */
    229 	{ &vop_rename_desc, fifo_rename },		/* rename */
    230 	{ &vop_mkdir_desc, fifo_mkdir },		/* mkdir */
    231 	{ &vop_rmdir_desc, fifo_rmdir },		/* rmdir */
    232 	{ &vop_symlink_desc, fifo_symlink },		/* symlink */
    233 	{ &vop_readdir_desc, fifo_readdir },		/* readdir */
    234 	{ &vop_readlink_desc, fifo_readlink },		/* readlink */
    235 	{ &vop_abortop_desc, fifo_abortop },		/* abortop */
    236 	{ &vop_inactive_desc, lfs_inactive },		/* inactive */
    237 	{ &vop_reclaim_desc, lfs_reclaim },		/* reclaim */
    238 	{ &vop_lock_desc, ufs_lock },			/* lock */
    239 	{ &vop_unlock_desc, ufs_unlock },		/* unlock */
    240 	{ &vop_bmap_desc, fifo_bmap },			/* bmap */
    241 	{ &vop_strategy_desc, fifo_strategy },		/* strategy */
    242 	{ &vop_print_desc, ufs_print },			/* print */
    243 	{ &vop_islocked_desc, ufs_islocked },		/* islocked */
    244 	{ &vop_pathconf_desc, fifo_pathconf },		/* pathconf */
    245 	{ &vop_advlock_desc, fifo_advlock },		/* advlock */
    246 	{ &vop_bwrite_desc, lfs_bwrite },		/* bwrite */
    247 	{ &vop_putpages_desc, fifo_putpages },		/* putpages */
    248 	{ NULL, NULL }
    249 };
    250 const struct vnodeopv_desc lfs_fifoop_opv_desc =
    251 	{ &lfs_fifoop_p, lfs_fifoop_entries };
    252 
    253 static int check_dirty(struct lfs *, struct vnode *, off_t, off_t, off_t, int, int);
    254 
    255 #define	LFS_READWRITE
    256 #include <ufs/ufs/ufs_readwrite.c>
    257 #undef	LFS_READWRITE
    258 
    259 /*
    260  * Synch an open file.
    261  */
    262 /* ARGSUSED */
    263 int
    264 lfs_fsync(void *v)
    265 {
    266 	struct vop_fsync_args /* {
    267 		struct vnode *a_vp;
    268 		struct ucred *a_cred;
    269 		int a_flags;
    270 		off_t offlo;
    271 		off_t offhi;
    272 		struct lwp *a_l;
    273 	} */ *ap = v;
    274 	struct vnode *vp = ap->a_vp;
    275 	int error, wait;
    276 
    277 	/*
    278 	 * Trickle sync checks for need to do a checkpoint after possible
    279 	 * activity from the pagedaemon.
    280 	 */
    281 	if (ap->a_flags & FSYNC_LAZY) {
    282 		simple_lock(&lfs_subsys_lock);
    283 		wakeup(&lfs_writer_daemon);
    284 		simple_unlock(&lfs_subsys_lock);
    285 		return 0;
    286 	}
    287 
    288 	wait = (ap->a_flags & FSYNC_WAIT);
    289 	simple_lock(&vp->v_interlock);
    290 	error = VOP_PUTPAGES(vp, trunc_page(ap->a_offlo),
    291 			round_page(ap->a_offhi),
    292 			PGO_CLEANIT | (wait ? PGO_SYNCIO : 0));
    293 	if (error)
    294 		return error;
    295 	error = lfs_update(vp, NULL, NULL, wait ? UPDATE_WAIT : 0);
    296 	if (error == 0 && ap->a_flags & FSYNC_CACHE) {
    297 		int l = 0;
    298 		error = VOP_IOCTL(VTOI(vp)->i_devvp, DIOCCACHESYNC, &l, FWRITE,
    299 				  ap->a_l->l_proc->p_ucred, ap->a_l);
    300 	}
    301 	if (wait && !VPISEMPTY(vp))
    302 		LFS_SET_UINO(VTOI(vp), IN_MODIFIED);
    303 
    304 	return error;
    305 }
    306 
    307 /*
    308  * Take IN_ADIROP off, then call ufs_inactive.
    309  */
    310 int
    311 lfs_inactive(void *v)
    312 {
    313 	struct vop_inactive_args /* {
    314 		struct vnode *a_vp;
    315 		struct lwp *a_l;
    316 	} */ *ap = v;
    317 
    318 	KASSERT(VTOI(ap->a_vp)->i_nlink == VTOI(ap->a_vp)->i_ffs_effnlink);
    319 
    320 	lfs_unmark_vnode(ap->a_vp);
    321 
    322 	/*
    323 	 * The Ifile is only ever inactivated on unmount.
    324 	 * Streamline this process by not giving it more dirty blocks.
    325 	 */
    326 	if (VTOI(ap->a_vp)->i_number == LFS_IFILE_INUM) {
    327 		LFS_CLR_UINO(VTOI(ap->a_vp), IN_ALLMOD);
    328 		VOP_UNLOCK(ap->a_vp, 0);
    329 		return 0;
    330 	}
    331 
    332 	return ufs_inactive(v);
    333 }
    334 
    335 /*
    336  * These macros are used to bracket UFS directory ops, so that we can
    337  * identify all the pages touched during directory ops which need to
    338  * be ordered and flushed atomically, so that they may be recovered.
    339  *
    340  * Because we have to mark nodes VDIROP in order to prevent
    341  * the cache from reclaiming them while a dirop is in progress, we must
    342  * also manage the number of nodes so marked (otherwise we can run out).
    343  * We do this by setting lfs_dirvcount to the number of marked vnodes; it
    344  * is decremented during segment write, when VDIROP is taken off.
    345  */
    346 #define	MARK_VNODE(vp)			lfs_mark_vnode(vp)
    347 #define	UNMARK_VNODE(vp)		lfs_unmark_vnode(vp)
    348 #define	SET_DIROP_CREATE(dvp, vpp)	lfs_set_dirop_create((dvp), (vpp))
    349 #define	SET_DIROP_REMOVE(dvp, vp)	lfs_set_dirop((dvp), (vp))
    350 static int lfs_set_dirop_create(struct vnode *, struct vnode **);
    351 static int lfs_set_dirop(struct vnode *, struct vnode *);
    352 
    353 static int
    354 lfs_set_dirop(struct vnode *dvp, struct vnode *vp)
    355 {
    356 	struct lfs *fs;
    357 	int error;
    358 
    359 	KASSERT(VOP_ISLOCKED(dvp));
    360 	KASSERT(vp == NULL || VOP_ISLOCKED(vp));
    361 
    362 	fs = VTOI(dvp)->i_lfs;
    363 
    364 	ASSERT_NO_SEGLOCK(fs);
    365 	/*
    366 	 * LFS_NRESERVE calculates direct and indirect blocks as well
    367 	 * as an inode block; an overestimate in most cases.
    368 	 */
    369 	if ((error = lfs_reserve(fs, dvp, vp, LFS_NRESERVE(fs))) != 0)
    370 		return (error);
    371 
    372     restart:
    373 	simple_lock(&fs->lfs_interlock);
    374 	if (fs->lfs_dirops == 0) {
    375 		simple_unlock(&fs->lfs_interlock);
    376 		lfs_check(dvp, LFS_UNUSED_LBN, 0);
    377 		simple_lock(&fs->lfs_interlock);
    378 	}
    379 	while (fs->lfs_writer)
    380 		ltsleep(&fs->lfs_dirops, (PRIBIO + 1), "lfs_sdirop", 0,
    381 			&fs->lfs_interlock);
    382 	simple_lock(&lfs_subsys_lock);
    383 	if (lfs_dirvcount > LFS_MAX_DIROP && fs->lfs_dirops == 0) {
    384 		wakeup(&lfs_writer_daemon);
    385 		simple_unlock(&lfs_subsys_lock);
    386 		simple_unlock(&fs->lfs_interlock);
    387 		preempt(1);
    388 		goto restart;
    389 	}
    390 
    391 	if (lfs_dirvcount > LFS_MAX_DIROP) {
    392 		simple_unlock(&fs->lfs_interlock);
    393 		DLOG((DLOG_DIROP, "lfs_set_dirop: sleeping with dirops=%d, "
    394 		      "dirvcount=%d\n", fs->lfs_dirops, lfs_dirvcount));
    395 		if ((error = ltsleep(&lfs_dirvcount,
    396 		    PCATCH | PUSER | PNORELOCK, "lfs_maxdirop", 0,
    397 		    &lfs_subsys_lock)) != 0) {
    398 			goto unreserve;
    399 		}
    400 		goto restart;
    401 	}
    402 	simple_unlock(&lfs_subsys_lock);
    403 
    404 	++fs->lfs_dirops;
    405 	fs->lfs_doifile = 1;
    406 	simple_unlock(&fs->lfs_interlock);
    407 
    408 	/* Hold a reference so SET_ENDOP will be happy */
    409 	vref(dvp);
    410 	if (vp) {
    411 		vref(vp);
    412 		MARK_VNODE(vp);
    413 	}
    414 
    415 	MARK_VNODE(dvp);
    416 	return 0;
    417 
    418 unreserve:
    419 	lfs_reserve(fs, dvp, vp, -LFS_NRESERVE(fs));
    420 	return error;
    421 }
    422 
    423 /*
    424  * Get a new vnode *before* adjusting the dirop count, to avoid a deadlock
    425  * in getnewvnode(), if we have a stacked filesystem mounted on top
    426  * of us.
    427  *
    428  * NB: this means we have to clear the new vnodes on error.  Fortunately
    429  * SET_ENDOP is there to do that for us.
    430  */
    431 static int
    432 lfs_set_dirop_create(struct vnode *dvp, struct vnode **vpp)
    433 {
    434 	int error;
    435 	struct lfs *fs;
    436 
    437 	fs = VFSTOUFS(dvp->v_mount)->um_lfs;
    438 	ASSERT_NO_SEGLOCK(fs);
    439 	if (fs->lfs_ronly)
    440 		return EROFS;
    441 	if (vpp && (error = getnewvnode(VT_LFS, dvp->v_mount, lfs_vnodeop_p, vpp))) {
    442 		DLOG((DLOG_ALLOC, "lfs_set_dirop_create: dvp %p error %d\n",
    443 		      dvp, error));
    444 		return error;
    445 	}
    446 	if ((error = lfs_set_dirop(dvp, NULL)) != 0) {
    447 		if (vpp) {
    448 			ungetnewvnode(*vpp);
    449 			*vpp = NULL;
    450 		}
    451 		return error;
    452 	}
    453 	return 0;
    454 }
    455 
    456 #define	SET_ENDOP_BASE(fs, dvp, str)					\
    457 	do {								\
    458 		simple_lock(&(fs)->lfs_interlock);			\
    459 		--(fs)->lfs_dirops;					\
    460 		if (!(fs)->lfs_dirops) {				\
    461 			if ((fs)->lfs_nadirop) {			\
    462 				panic("SET_ENDOP: %s: no dirops but "	\
    463 					" nadirop=%d", (str),		\
    464 					(fs)->lfs_nadirop);		\
    465 			}						\
    466 			wakeup(&(fs)->lfs_writer);			\
    467 			simple_unlock(&(fs)->lfs_interlock);		\
    468 			lfs_check((dvp), LFS_UNUSED_LBN, 0);		\
    469 		} else							\
    470 			simple_unlock(&(fs)->lfs_interlock);		\
    471 	} while(0)
    472 #define SET_ENDOP_CREATE(fs, dvp, nvpp, str)				\
    473 	do {								\
    474 		UNMARK_VNODE(dvp);					\
    475 		if (nvpp && *nvpp)					\
    476 			UNMARK_VNODE(*nvpp);				\
    477 		/* Check for error return to stem vnode leakage */	\
    478 		if (nvpp && *nvpp && !((*nvpp)->v_flag & VDIROP))	\
    479 			ungetnewvnode(*(nvpp));				\
    480 		SET_ENDOP_BASE((fs), (dvp), (str));			\
    481 		lfs_reserve((fs), (dvp), NULL, -LFS_NRESERVE(fs));	\
    482 		vrele(dvp);						\
    483 	} while(0)
    484 #define SET_ENDOP_CREATE_AP(ap, str)					\
    485 	SET_ENDOP_CREATE(VTOI((ap)->a_dvp)->i_lfs, (ap)->a_dvp,		\
    486 			 (ap)->a_vpp, (str))
    487 #define SET_ENDOP_REMOVE(fs, dvp, ovp, str)				\
    488 	do {								\
    489 		UNMARK_VNODE(dvp);					\
    490 		if (ovp)						\
    491 			UNMARK_VNODE(ovp);				\
    492 		SET_ENDOP_BASE((fs), (dvp), (str));			\
    493 		lfs_reserve((fs), (dvp), (ovp), -LFS_NRESERVE(fs));	\
    494 		vrele(dvp);						\
    495 		if (ovp)						\
    496 			vrele(ovp);					\
    497 	} while(0)
    498 
    499 void
    500 lfs_mark_vnode(struct vnode *vp)
    501 {
    502 	struct inode *ip = VTOI(vp);
    503 	struct lfs *fs = ip->i_lfs;
    504 
    505 	simple_lock(&fs->lfs_interlock);
    506 	if (!(ip->i_flag & IN_ADIROP)) {
    507 		if (!(vp->v_flag & VDIROP)) {
    508 			(void)lfs_vref(vp);
    509 			simple_lock(&lfs_subsys_lock);
    510 			++lfs_dirvcount;
    511 			simple_unlock(&lfs_subsys_lock);
    512 			TAILQ_INSERT_TAIL(&fs->lfs_dchainhd, ip, i_lfs_dchain);
    513 			vp->v_flag |= VDIROP;
    514 		}
    515 		++fs->lfs_nadirop;
    516 		ip->i_flag |= IN_ADIROP;
    517 	} else
    518 		KASSERT(vp->v_flag & VDIROP);
    519 	simple_unlock(&fs->lfs_interlock);
    520 }
    521 
    522 void
    523 lfs_unmark_vnode(struct vnode *vp)
    524 {
    525 	struct inode *ip = VTOI(vp);
    526 
    527 	if (ip && (ip->i_flag & IN_ADIROP)) {
    528 		KASSERT(vp->v_flag & VDIROP);
    529 		simple_lock(&ip->i_lfs->lfs_interlock);
    530 		--ip->i_lfs->lfs_nadirop;
    531 		simple_unlock(&ip->i_lfs->lfs_interlock);
    532 		ip->i_flag &= ~IN_ADIROP;
    533 	}
    534 }
    535 
    536 int
    537 lfs_symlink(void *v)
    538 {
    539 	struct vop_symlink_args /* {
    540 		struct vnode *a_dvp;
    541 		struct vnode **a_vpp;
    542 		struct componentname *a_cnp;
    543 		struct vattr *a_vap;
    544 		char *a_target;
    545 	} */ *ap = v;
    546 	int error;
    547 
    548 	if ((error = SET_DIROP_CREATE(ap->a_dvp, ap->a_vpp)) != 0) {
    549 		vput(ap->a_dvp);
    550 		return error;
    551 	}
    552 	error = ufs_symlink(ap);
    553 	SET_ENDOP_CREATE_AP(ap, "symlink");
    554 	return (error);
    555 }
    556 
    557 int
    558 lfs_mknod(void *v)
    559 {
    560 	struct vop_mknod_args	/* {
    561 		struct vnode *a_dvp;
    562 		struct vnode **a_vpp;
    563 		struct componentname *a_cnp;
    564 		struct vattr *a_vap;
    565 		} */ *ap = v;
    566 	struct vattr *vap = ap->a_vap;
    567 	struct vnode **vpp = ap->a_vpp;
    568 	struct inode *ip;
    569 	int error;
    570 	struct mount	*mp;
    571 	ino_t		ino;
    572 
    573 	if ((error = SET_DIROP_CREATE(ap->a_dvp, ap->a_vpp)) != 0) {
    574 		vput(ap->a_dvp);
    575 		return error;
    576 	}
    577 	error = ufs_makeinode(MAKEIMODE(vap->va_type, vap->va_mode),
    578 	    ap->a_dvp, vpp, ap->a_cnp);
    579 
    580 	/* Either way we're done with the dirop at this point */
    581 	SET_ENDOP_CREATE_AP(ap, "mknod");
    582 
    583 	if (error)
    584 		return (error);
    585 
    586 	ip = VTOI(*vpp);
    587 	mp  = (*vpp)->v_mount;
    588 	ino = ip->i_number;
    589 	ip->i_flag |= IN_ACCESS | IN_CHANGE | IN_UPDATE;
    590 	if (vap->va_rdev != VNOVAL) {
    591 		/*
    592 		 * Want to be able to use this to make badblock
    593 		 * inodes, so don't truncate the dev number.
    594 		 */
    595 #if 0
    596 		ip->i_ffs1_rdev = ufs_rw32(vap->va_rdev,
    597 		    UFS_MPNEEDSWAP((*vpp)->v_mount));
    598 #else
    599 		ip->i_ffs1_rdev = vap->va_rdev;
    600 #endif
    601 	}
    602 
    603 	/*
    604 	 * Call fsync to write the vnode so that we don't have to deal with
    605 	 * flushing it when it's marked VDIROP|VXLOCK.
    606 	 *
    607 	 * XXX KS - If we can't flush we also can't call vgone(), so must
    608 	 * return.  But, that leaves this vnode in limbo, also not good.
    609 	 * Can this ever happen (barring hardware failure)?
    610 	 */
    611 	if ((error = VOP_FSYNC(*vpp, NOCRED, FSYNC_WAIT, 0, 0,
    612 	    curlwp)) != 0) {
    613 		panic("lfs_mknod: couldn't fsync (ino %llu)",
    614 		    (unsigned long long)ino);
    615 		/* return (error); */
    616 	}
    617 	/*
    618 	 * Remove vnode so that it will be reloaded by VFS_VGET and
    619 	 * checked to see if it is an alias of an existing entry in
    620 	 * the inode cache.
    621 	 */
    622 	/* Used to be vput, but that causes us to call VOP_INACTIVE twice. */
    623 
    624 	VOP_UNLOCK(*vpp, 0);
    625 	lfs_vunref(*vpp);
    626 	(*vpp)->v_type = VNON;
    627 	vgone(*vpp);
    628 	error = VFS_VGET(mp, ino, vpp);
    629 
    630 	if (error != 0) {
    631 		*vpp = NULL;
    632 		return (error);
    633 	}
    634 	return (0);
    635 }
    636 
    637 int
    638 lfs_create(void *v)
    639 {
    640 	struct vop_create_args	/* {
    641 		struct vnode *a_dvp;
    642 		struct vnode **a_vpp;
    643 		struct componentname *a_cnp;
    644 		struct vattr *a_vap;
    645 	} */ *ap = v;
    646 	int error;
    647 
    648 	if ((error = SET_DIROP_CREATE(ap->a_dvp, ap->a_vpp)) != 0) {
    649 		vput(ap->a_dvp);
    650 		return error;
    651 	}
    652 	error = ufs_create(ap);
    653 	SET_ENDOP_CREATE_AP(ap, "create");
    654 	return (error);
    655 }
    656 
    657 int
    658 lfs_mkdir(void *v)
    659 {
    660 	struct vop_mkdir_args	/* {
    661 		struct vnode *a_dvp;
    662 		struct vnode **a_vpp;
    663 		struct componentname *a_cnp;
    664 		struct vattr *a_vap;
    665 	} */ *ap = v;
    666 	int error;
    667 
    668 	if ((error = SET_DIROP_CREATE(ap->a_dvp, ap->a_vpp)) != 0) {
    669 		vput(ap->a_dvp);
    670 		return error;
    671 	}
    672 	error = ufs_mkdir(ap);
    673 	SET_ENDOP_CREATE_AP(ap, "mkdir");
    674 	return (error);
    675 }
    676 
    677 int
    678 lfs_remove(void *v)
    679 {
    680 	struct vop_remove_args	/* {
    681 		struct vnode *a_dvp;
    682 		struct vnode *a_vp;
    683 		struct componentname *a_cnp;
    684 	} */ *ap = v;
    685 	struct vnode *dvp, *vp;
    686 	int error;
    687 
    688 	dvp = ap->a_dvp;
    689 	vp = ap->a_vp;
    690 	if ((error = SET_DIROP_REMOVE(dvp, vp)) != 0) {
    691 		if (dvp == vp)
    692 			vrele(vp);
    693 		else
    694 			vput(vp);
    695 		vput(dvp);
    696 		return error;
    697 	}
    698 	error = ufs_remove(ap);
    699 	SET_ENDOP_REMOVE(VTOI(dvp)->i_lfs, dvp, vp, "remove");
    700 	return (error);
    701 }
    702 
    703 int
    704 lfs_rmdir(void *v)
    705 {
    706 	struct vop_rmdir_args	/* {
    707 		struct vnodeop_desc *a_desc;
    708 		struct vnode *a_dvp;
    709 		struct vnode *a_vp;
    710 		struct componentname *a_cnp;
    711 	} */ *ap = v;
    712 	struct vnode *vp;
    713 	int error;
    714 
    715 	vp = ap->a_vp;
    716 	if ((error = SET_DIROP_REMOVE(ap->a_dvp, ap->a_vp)) != 0) {
    717 		vrele(ap->a_dvp);
    718 		if (ap->a_vp != ap->a_dvp)
    719 			VOP_UNLOCK(ap->a_dvp, 0);
    720 		vput(vp);
    721 		return error;
    722 	}
    723 	error = ufs_rmdir(ap);
    724 	SET_ENDOP_REMOVE(VTOI(ap->a_dvp)->i_lfs, ap->a_dvp, vp, "rmdir");
    725 	return (error);
    726 }
    727 
    728 int
    729 lfs_link(void *v)
    730 {
    731 	struct vop_link_args	/* {
    732 		struct vnode *a_dvp;
    733 		struct vnode *a_vp;
    734 		struct componentname *a_cnp;
    735 	} */ *ap = v;
    736 	int error;
    737 	struct vnode **vpp = NULL;
    738 
    739 	if ((error = SET_DIROP_CREATE(ap->a_dvp, vpp)) != 0) {
    740 		vput(ap->a_dvp);
    741 		return error;
    742 	}
    743 	error = ufs_link(ap);
    744 	SET_ENDOP_CREATE(VTOI(ap->a_dvp)->i_lfs, ap->a_dvp, vpp, "link");
    745 	return (error);
    746 }
    747 
    748 int
    749 lfs_rename(void *v)
    750 {
    751 	struct vop_rename_args	/* {
    752 		struct vnode *a_fdvp;
    753 		struct vnode *a_fvp;
    754 		struct componentname *a_fcnp;
    755 		struct vnode *a_tdvp;
    756 		struct vnode *a_tvp;
    757 		struct componentname *a_tcnp;
    758 	} */ *ap = v;
    759 	struct vnode *tvp, *fvp, *tdvp, *fdvp;
    760 	struct componentname *tcnp, *fcnp;
    761 	int error;
    762 	struct lfs *fs;
    763 
    764 	fs = VTOI(ap->a_fdvp)->i_lfs;
    765 	tvp = ap->a_tvp;
    766 	tdvp = ap->a_tdvp;
    767 	tcnp = ap->a_tcnp;
    768 	fvp = ap->a_fvp;
    769 	fdvp = ap->a_fdvp;
    770 	fcnp = ap->a_fcnp;
    771 
    772 	/*
    773 	 * Check for cross-device rename.
    774 	 * If it is, we don't want to set dirops, just error out.
    775 	 * (In particular note that MARK_VNODE(tdvp) will DTWT on
    776 	 * a cross-device rename.)
    777 	 *
    778 	 * Copied from ufs_rename.
    779 	 */
    780 	if ((fvp->v_mount != tdvp->v_mount) ||
    781 	    (tvp && (fvp->v_mount != tvp->v_mount))) {
    782 		error = EXDEV;
    783 		goto errout;
    784 	}
    785 
    786 	/*
    787 	 * Check to make sure we're not renaming a vnode onto itself
    788 	 * (deleting a hard link by renaming one name onto another);
    789 	 * if we are we can't recursively call VOP_REMOVE since that
    790 	 * would leave us with an unaccounted-for number of live dirops.
    791 	 *
    792 	 * Inline the relevant section of ufs_rename here, *before*
    793 	 * calling SET_DIROP_REMOVE.
    794 	 */
    795 	if (tvp && ((VTOI(tvp)->i_flags & (IMMUTABLE | APPEND)) ||
    796 	    (VTOI(tdvp)->i_flags & APPEND))) {
    797 		error = EPERM;
    798 		goto errout;
    799 	}
    800 	if (fvp == tvp) {
    801 		if (fvp->v_type == VDIR) {
    802 			error = EINVAL;
    803 			goto errout;
    804 		}
    805 
    806 		/* Release destination completely. */
    807 		VOP_ABORTOP(tdvp, tcnp);
    808 		vput(tdvp);
    809 		vput(tvp);
    810 
    811 		/* Delete source. */
    812 		vrele(fvp);
    813 		fcnp->cn_flags &= ~(MODMASK | SAVESTART);
    814 		fcnp->cn_flags |= LOCKPARENT | LOCKLEAF;
    815 		fcnp->cn_nameiop = DELETE;
    816 		if ((error = relookup(fdvp, &fvp, fcnp))){
    817 			/* relookup blew away fdvp */
    818 			return (error);
    819 		}
    820 		return (VOP_REMOVE(fdvp, fvp, fcnp));
    821 	}
    822 
    823 	if ((error = SET_DIROP_REMOVE(tdvp, tvp)) != 0)
    824 		goto errout;
    825 	MARK_VNODE(fdvp);
    826 	MARK_VNODE(fvp);
    827 
    828 	error = ufs_rename(ap);
    829 	UNMARK_VNODE(fdvp);
    830 	UNMARK_VNODE(fvp);
    831 	SET_ENDOP_REMOVE(fs, tdvp, tvp, "rename");
    832 	return (error);
    833 
    834     errout:
    835 	VOP_ABORTOP(tdvp, ap->a_tcnp); /* XXX, why not in NFS? */
    836 	if (tdvp == tvp)
    837 		vrele(tdvp);
    838 	else
    839 		vput(tdvp);
    840 	if (tvp)
    841 		vput(tvp);
    842 	VOP_ABORTOP(fdvp, ap->a_fcnp); /* XXX, why not in NFS? */
    843 	vrele(fdvp);
    844 	vrele(fvp);
    845 	return (error);
    846 }
    847 
    848 /* XXX hack to avoid calling ITIMES in getattr */
    849 int
    850 lfs_getattr(void *v)
    851 {
    852 	struct vop_getattr_args /* {
    853 		struct vnode *a_vp;
    854 		struct vattr *a_vap;
    855 		struct ucred *a_cred;
    856 		struct lwp *a_l;
    857 	} */ *ap = v;
    858 	struct vnode *vp = ap->a_vp;
    859 	struct inode *ip = VTOI(vp);
    860 	struct vattr *vap = ap->a_vap;
    861 	struct lfs *fs = ip->i_lfs;
    862 	/*
    863 	 * Copy from inode table
    864 	 */
    865 	vap->va_fsid = ip->i_dev;
    866 	vap->va_fileid = ip->i_number;
    867 	vap->va_mode = ip->i_mode & ~IFMT;
    868 	vap->va_nlink = ip->i_nlink;
    869 	vap->va_uid = ip->i_uid;
    870 	vap->va_gid = ip->i_gid;
    871 	vap->va_rdev = (dev_t)ip->i_ffs1_rdev;
    872 	vap->va_size = vp->v_size;
    873 	vap->va_atime.tv_sec = ip->i_ffs1_atime;
    874 	vap->va_atime.tv_nsec = ip->i_ffs1_atimensec;
    875 	vap->va_mtime.tv_sec = ip->i_ffs1_mtime;
    876 	vap->va_mtime.tv_nsec = ip->i_ffs1_mtimensec;
    877 	vap->va_ctime.tv_sec = ip->i_ffs1_ctime;
    878 	vap->va_ctime.tv_nsec = ip->i_ffs1_ctimensec;
    879 	vap->va_flags = ip->i_flags;
    880 	vap->va_gen = ip->i_gen;
    881 	/* this doesn't belong here */
    882 	if (vp->v_type == VBLK)
    883 		vap->va_blocksize = BLKDEV_IOSIZE;
    884 	else if (vp->v_type == VCHR)
    885 		vap->va_blocksize = MAXBSIZE;
    886 	else
    887 		vap->va_blocksize = vp->v_mount->mnt_stat.f_iosize;
    888 	vap->va_bytes = fsbtob(fs, (u_quad_t)ip->i_lfs_effnblks);
    889 	vap->va_type = vp->v_type;
    890 	vap->va_filerev = ip->i_modrev;
    891 	return (0);
    892 }
    893 
    894 /*
    895  * Check to make sure the inode blocks won't choke the buffer
    896  * cache, then call ufs_setattr as usual.
    897  */
    898 int
    899 lfs_setattr(void *v)
    900 {
    901 	struct vop_setattr_args /* {
    902 		struct vnode *a_vp;
    903 		struct vattr *a_vap;
    904 		struct ucred *a_cred;
    905 		struct lwp *a_l;
    906 	} */ *ap = v;
    907 	struct vnode *vp = ap->a_vp;
    908 
    909 	lfs_check(vp, LFS_UNUSED_LBN, 0);
    910 	return ufs_setattr(v);
    911 }
    912 
    913 /*
    914  * Close called
    915  *
    916  * XXX -- we were using ufs_close, but since it updates the
    917  * times on the inode, we might need to bump the uinodes
    918  * count.
    919  */
    920 /* ARGSUSED */
    921 int
    922 lfs_close(void *v)
    923 {
    924 	struct vop_close_args /* {
    925 		struct vnode *a_vp;
    926 		int  a_fflag;
    927 		struct ucred *a_cred;
    928 		struct lwp *a_l;
    929 	} */ *ap = v;
    930 	struct vnode *vp = ap->a_vp;
    931 	struct inode *ip = VTOI(vp);
    932 
    933 	if (vp == ip->i_lfs->lfs_ivnode &&
    934 	    vp->v_mount->mnt_iflag & IMNT_UNMOUNT)
    935 		return 0;
    936 
    937 	if (vp->v_usecount > 1 && vp != ip->i_lfs->lfs_ivnode) {
    938 		LFS_ITIMES(ip, NULL, NULL, NULL);
    939 	}
    940 	return (0);
    941 }
    942 
    943 /*
    944  * Close wrapper for special devices.
    945  *
    946  * Update the times on the inode then do device close.
    947  */
    948 int
    949 lfsspec_close(void *v)
    950 {
    951 	struct vop_close_args /* {
    952 		struct vnode	*a_vp;
    953 		int		a_fflag;
    954 		struct ucred	*a_cred;
    955 		struct lwp	*a_l;
    956 	} */ *ap = v;
    957 	struct vnode	*vp;
    958 	struct inode	*ip;
    959 
    960 	vp = ap->a_vp;
    961 	ip = VTOI(vp);
    962 	if (vp->v_usecount > 1) {
    963 		LFS_ITIMES(ip, NULL, NULL, NULL);
    964 	}
    965 	return (VOCALL (spec_vnodeop_p, VOFFSET(vop_close), ap));
    966 }
    967 
    968 /*
    969  * Close wrapper for fifo's.
    970  *
    971  * Update the times on the inode then do device close.
    972  */
    973 int
    974 lfsfifo_close(void *v)
    975 {
    976 	struct vop_close_args /* {
    977 		struct vnode	*a_vp;
    978 		int		a_fflag;
    979 		struct ucred	*a_cred;
    980 		struct lwp	*a_l;
    981 	} */ *ap = v;
    982 	struct vnode	*vp;
    983 	struct inode	*ip;
    984 
    985 	vp = ap->a_vp;
    986 	ip = VTOI(vp);
    987 	if (ap->a_vp->v_usecount > 1) {
    988 		LFS_ITIMES(ip, NULL, NULL, NULL);
    989 	}
    990 	return (VOCALL (fifo_vnodeop_p, VOFFSET(vop_close), ap));
    991 }
    992 
    993 /*
    994  * Reclaim an inode so that it can be used for other purposes.
    995  */
    996 
    997 int
    998 lfs_reclaim(void *v)
    999 {
   1000 	struct vop_reclaim_args /* {
   1001 		struct vnode *a_vp;
   1002 		struct lwp *a_l;
   1003 	} */ *ap = v;
   1004 	struct vnode *vp = ap->a_vp;
   1005 	struct inode *ip = VTOI(vp);
   1006 	int error;
   1007 
   1008 	KASSERT(ip->i_nlink == ip->i_ffs_effnlink);
   1009 
   1010 	LFS_CLR_UINO(ip, IN_ALLMOD);
   1011 	if ((error = ufs_reclaim(vp, ap->a_l)))
   1012 		return (error);
   1013 	pool_put(&lfs_dinode_pool, ip->i_din.ffs1_din);
   1014 	lfs_deregister_all(vp);
   1015 	pool_put(&lfs_inoext_pool, ip->inode_ext.lfs);
   1016 	ip->inode_ext.lfs = NULL;
   1017 	pool_put(&lfs_inode_pool, vp->v_data);
   1018 	vp->v_data = NULL;
   1019 	return (0);
   1020 }
   1021 
   1022 /*
   1023  * Read a block from a storage device.
   1024  * In order to avoid reading blocks that are in the process of being
   1025  * written by the cleaner---and hence are not mutexed by the normal
   1026  * buffer cache / page cache mechanisms---check for collisions before
   1027  * reading.
   1028  *
   1029  * We inline ufs_strategy to make sure that the VOP_BMAP occurs *before*
   1030  * the active cleaner test.
   1031  *
   1032  * XXX This code assumes that lfs_markv makes synchronous checkpoints.
   1033  */
   1034 int
   1035 lfs_strategy(void *v)
   1036 {
   1037 	struct vop_strategy_args /* {
   1038 		struct vnode *a_vp;
   1039 		struct buf *a_bp;
   1040 	} */ *ap = v;
   1041 	struct buf	*bp;
   1042 	struct lfs	*fs;
   1043 	struct vnode	*vp;
   1044 	struct inode	*ip;
   1045 	daddr_t		tbn;
   1046 	int		i, sn, error, slept;
   1047 
   1048 	bp = ap->a_bp;
   1049 	vp = ap->a_vp;
   1050 	ip = VTOI(vp);
   1051 	fs = ip->i_lfs;
   1052 
   1053 	/* lfs uses its strategy routine only for read */
   1054 	KASSERT(bp->b_flags & B_READ);
   1055 
   1056 	if (vp->v_type == VBLK || vp->v_type == VCHR)
   1057 		panic("lfs_strategy: spec");
   1058 	KASSERT(bp->b_bcount != 0);
   1059 	if (bp->b_blkno == bp->b_lblkno) {
   1060 		error = VOP_BMAP(vp, bp->b_lblkno, NULL, &bp->b_blkno,
   1061 				 NULL);
   1062 		if (error) {
   1063 			bp->b_error = error;
   1064 			bp->b_flags |= B_ERROR;
   1065 			biodone(bp);
   1066 			return (error);
   1067 		}
   1068 		if ((long)bp->b_blkno == -1) /* no valid data */
   1069 			clrbuf(bp);
   1070 	}
   1071 	if ((long)bp->b_blkno < 0) { /* block is not on disk */
   1072 		biodone(bp);
   1073 		return (0);
   1074 	}
   1075 
   1076 	slept = 1;
   1077 	simple_lock(&fs->lfs_interlock);
   1078 	while (slept && fs->lfs_seglock) {
   1079 		simple_unlock(&fs->lfs_interlock);
   1080 		/*
   1081 		 * Look through list of intervals.
   1082 		 * There will only be intervals to look through
   1083 		 * if the cleaner holds the seglock.
   1084 		 * Since the cleaner is synchronous, we can trust
   1085 		 * the list of intervals to be current.
   1086 		 */
   1087 		tbn = dbtofsb(fs, bp->b_blkno);
   1088 		sn = dtosn(fs, tbn);
   1089 		slept = 0;
   1090 		for (i = 0; i < fs->lfs_cleanind; i++) {
   1091 			if (sn == dtosn(fs, fs->lfs_cleanint[i]) &&
   1092 			    tbn >= fs->lfs_cleanint[i]) {
   1093 				DLOG((DLOG_CLEAN,
   1094 				      "lfs_strategy: ino %d lbn %" PRId64
   1095 				       " ind %d sn %d fsb %" PRIx32
   1096 				       " given sn %d fsb %" PRIx64 "\n",
   1097 					ip->i_number, bp->b_lblkno, i,
   1098 					dtosn(fs, fs->lfs_cleanint[i]),
   1099 					fs->lfs_cleanint[i], sn, tbn));
   1100 				DLOG((DLOG_CLEAN,
   1101 				      "lfs_strategy: sleeping on ino %d lbn %"
   1102 				      PRId64 "\n", ip->i_number, bp->b_lblkno));
   1103 				simple_lock(&fs->lfs_interlock);
   1104 				if (fs->lfs_seglock)
   1105 					ltsleep(&fs->lfs_seglock,
   1106 						(PRIBIO + 1) | PNORELOCK,
   1107 						"lfs_strategy", 0,
   1108 						&fs->lfs_interlock);
   1109 				/* Things may be different now; start over. */
   1110 				slept = 1;
   1111 				break;
   1112 			}
   1113 		}
   1114 		simple_lock(&fs->lfs_interlock);
   1115 	}
   1116 	simple_unlock(&fs->lfs_interlock);
   1117 
   1118 	vp = ip->i_devvp;
   1119 	VOP_STRATEGY(vp, bp);
   1120 	return (0);
   1121 }
   1122 
   1123 static void
   1124 lfs_flush_dirops(struct lfs *fs)
   1125 {
   1126 	struct inode *ip, *nip;
   1127 	struct vnode *vp;
   1128 	extern int lfs_dostats;
   1129 	struct segment *sp;
   1130 	int needunlock;
   1131 
   1132 	ASSERT_NO_SEGLOCK(fs);
   1133 
   1134 	if (fs->lfs_ronly)
   1135 		return;
   1136 
   1137 	simple_lock(&fs->lfs_interlock);
   1138 	if (TAILQ_FIRST(&fs->lfs_dchainhd) == NULL) {
   1139 		simple_unlock(&fs->lfs_interlock);
   1140 		return;
   1141 	} else
   1142 		simple_unlock(&fs->lfs_interlock);
   1143 
   1144 	if (lfs_dostats)
   1145 		++lfs_stats.flush_invoked;
   1146 
   1147 	/*
   1148 	 * Inline lfs_segwrite/lfs_writevnodes, but just for dirops.
   1149 	 * Technically this is a checkpoint (the on-disk state is valid)
   1150 	 * even though we are leaving out all the file data.
   1151 	 */
   1152 	lfs_imtime(fs);
   1153 	lfs_seglock(fs, SEGM_CKP);
   1154 	sp = fs->lfs_sp;
   1155 
   1156 	/*
   1157 	 * lfs_writevnodes, optimized to get dirops out of the way.
   1158 	 * Only write dirops, and don't flush files' pages, only
   1159 	 * blocks from the directories.
   1160 	 *
   1161 	 * We don't need to vref these files because they are
   1162 	 * dirops and so hold an extra reference until the
   1163 	 * segunlock clears them of that status.
   1164 	 *
   1165 	 * We don't need to check for IN_ADIROP because we know that
   1166 	 * no dirops are active.
   1167 	 *
   1168 	 */
   1169 	simple_lock(&fs->lfs_interlock);
   1170 	for (ip = TAILQ_FIRST(&fs->lfs_dchainhd); ip != NULL; ip = nip) {
   1171 		nip = TAILQ_NEXT(ip, i_lfs_dchain);
   1172 		simple_unlock(&fs->lfs_interlock);
   1173 		vp = ITOV(ip);
   1174 
   1175 		/*
   1176 		 * All writes to directories come from dirops; all
   1177 		 * writes to files' direct blocks go through the page
   1178 		 * cache, which we're not touching.  Reads to files
   1179 		 * and/or directories will not be affected by writing
   1180 		 * directory blocks inodes and file inodes.  So we don't
   1181 		 * really need to lock.  If we don't lock, though,
   1182 		 * make sure that we don't clear IN_MODIFIED
   1183 		 * unnecessarily.
   1184 		 */
   1185 		if (vp->v_flag & VXLOCK)
   1186 			continue;
   1187 		if (vn_lock(vp, LK_EXCLUSIVE | LK_NOWAIT) == 0) {
   1188 			needunlock = 1;
   1189 		} else {
   1190 			DLOG((DLOG_VNODE, "lfs_flush_dirops: flushing locked ino %d\n",
   1191 			       VTOI(vp)->i_number));
   1192 			needunlock = 0;
   1193 		}
   1194 		if (vp->v_type != VREG &&
   1195 		    ((ip->i_flag & IN_ALLMOD) || !VPISEMPTY(vp))) {
   1196 			lfs_writefile(fs, sp, vp);
   1197 			if (!VPISEMPTY(vp) && !WRITEINPROG(vp) &&
   1198 			    !(ip->i_flag & IN_ALLMOD)) {
   1199 				LFS_SET_UINO(ip, IN_MODIFIED);
   1200 			}
   1201 		}
   1202 		(void) lfs_writeinode(fs, sp, ip);
   1203 		if (needunlock)
   1204 			VOP_UNLOCK(vp, 0);
   1205 		else
   1206 			LFS_SET_UINO(ip, IN_MODIFIED);
   1207 		simple_lock(&fs->lfs_interlock);
   1208 	}
   1209 	simple_unlock(&fs->lfs_interlock);
   1210 	/* We've written all the dirops there are */
   1211 	((SEGSUM *)(sp->segsum))->ss_flags &= ~(SS_CONT);
   1212 	(void) lfs_writeseg(fs, sp);
   1213 	lfs_segunlock(fs);
   1214 }
   1215 
   1216 /*
   1217  * Provide a fcntl interface to sys_lfs_{segwait,bmapv,markv}.
   1218  */
   1219 int
   1220 lfs_fcntl(void *v)
   1221 {
   1222 	struct vop_fcntl_args /* {
   1223 		struct vnode *a_vp;
   1224 		u_long a_command;
   1225 		caddr_t  a_data;
   1226 		int  a_fflag;
   1227 		struct ucred *a_cred;
   1228 		struct lwp *a_l;
   1229 	} */ *ap = v;
   1230 	struct timeval *tvp;
   1231 	BLOCK_INFO *blkiov;
   1232 	CLEANERINFO *cip;
   1233 	SEGUSE *sup;
   1234 	int blkcnt, error, oclean;
   1235 	struct lfs_fcntl_markv blkvp;
   1236 	struct proc *p;
   1237 	fsid_t *fsidp;
   1238 	struct lfs *fs;
   1239 	struct buf *bp;
   1240 	fhandle_t *fhp;
   1241 	daddr_t off;
   1242 
   1243 	/* Only respect LFS fcntls on fs root or Ifile */
   1244 	if (VTOI(ap->a_vp)->i_number != ROOTINO &&
   1245 	    VTOI(ap->a_vp)->i_number != LFS_IFILE_INUM) {
   1246 		return ufs_fcntl(v);
   1247 	}
   1248 
   1249 	/* Avoid locking a draining lock */
   1250 	if (ap->a_vp->v_mount->mnt_iflag & IMNT_UNMOUNT) {
   1251 		return ESHUTDOWN;
   1252 	}
   1253 
   1254 	p = ap->a_l->l_proc;
   1255 	fs = VTOI(ap->a_vp)->i_lfs;
   1256 	fsidp = &ap->a_vp->v_mount->mnt_stat.f_fsidx;
   1257 
   1258 	switch (ap->a_command) {
   1259 	    case LFCNSEGWAITALL:
   1260 	    case LFCNSEGWAITALL_COMPAT:
   1261 		fsidp = NULL;
   1262 		/* FALLSTHROUGH */
   1263 	    case LFCNSEGWAIT:
   1264 	    case LFCNSEGWAIT_COMPAT:
   1265 		tvp = (struct timeval *)ap->a_data;
   1266 		simple_lock(&fs->lfs_interlock);
   1267 		++fs->lfs_sleepers;
   1268 		simple_unlock(&fs->lfs_interlock);
   1269 		VOP_UNLOCK(ap->a_vp, 0);
   1270 
   1271 		error = lfs_segwait(fsidp, tvp);
   1272 
   1273 		VOP_LOCK(ap->a_vp, LK_EXCLUSIVE);
   1274 		simple_lock(&fs->lfs_interlock);
   1275 		if (--fs->lfs_sleepers == 0)
   1276 			wakeup(&fs->lfs_sleepers);
   1277 		simple_unlock(&fs->lfs_interlock);
   1278 		return error;
   1279 
   1280 	    case LFCNBMAPV:
   1281 	    case LFCNMARKV:
   1282 		if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
   1283 			return (error);
   1284 		blkvp = *(struct lfs_fcntl_markv *)ap->a_data;
   1285 
   1286 		blkcnt = blkvp.blkcnt;
   1287 		if ((u_int) blkcnt > LFS_MARKV_MAXBLKCNT)
   1288 			return (EINVAL);
   1289 		blkiov = lfs_malloc(fs, blkcnt * sizeof(BLOCK_INFO), LFS_NB_BLKIOV);
   1290 		if ((error = copyin(blkvp.blkiov, blkiov,
   1291 		     blkcnt * sizeof(BLOCK_INFO))) != 0) {
   1292 			lfs_free(fs, blkiov, LFS_NB_BLKIOV);
   1293 			return error;
   1294 		}
   1295 
   1296 		simple_lock(&fs->lfs_interlock);
   1297 		++fs->lfs_sleepers;
   1298 		simple_unlock(&fs->lfs_interlock);
   1299 		VOP_UNLOCK(ap->a_vp, 0);
   1300 		if (ap->a_command == LFCNBMAPV)
   1301 			error = lfs_bmapv(p, fsidp, blkiov, blkcnt);
   1302 		else /* LFCNMARKV */
   1303 			error = lfs_markv(p, fsidp, blkiov, blkcnt);
   1304 		if (error == 0)
   1305 			error = copyout(blkiov, blkvp.blkiov,
   1306 					blkcnt * sizeof(BLOCK_INFO));
   1307 		VOP_LOCK(ap->a_vp, LK_EXCLUSIVE);
   1308 		simple_lock(&fs->lfs_interlock);
   1309 		if (--fs->lfs_sleepers == 0)
   1310 			wakeup(&fs->lfs_sleepers);
   1311 		simple_unlock(&fs->lfs_interlock);
   1312 		lfs_free(fs, blkiov, LFS_NB_BLKIOV);
   1313 		return error;
   1314 
   1315 	    case LFCNRECLAIM:
   1316 		/*
   1317 		 * Flush dirops and write Ifile, allowing empty segments
   1318 		 * to be immediately reclaimed.
   1319 		 */
   1320 		VOP_UNLOCK(ap->a_vp, 0);
   1321 		lfs_writer_enter(fs, "pndirop");
   1322 		off = fs->lfs_offset;
   1323 		lfs_seglock(fs, SEGM_FORCE_CKP | SEGM_CKP);
   1324 		lfs_flush_dirops(fs);
   1325 		LFS_CLEANERINFO(cip, fs, bp);
   1326 		oclean = cip->clean;
   1327 		LFS_SYNC_CLEANERINFO(cip, fs, bp, 1);
   1328 		lfs_segwrite(ap->a_vp->v_mount, SEGM_FORCE_CKP);
   1329 		lfs_segunlock(fs);
   1330 		lfs_writer_leave(fs);
   1331 
   1332 #ifdef DEBUG
   1333 		LFS_CLEANERINFO(cip, fs, bp);
   1334 		DLOG((DLOG_CLEAN, "lfs_fcntl: reclaim wrote %" PRId64
   1335 		      " blocks, cleaned %" PRId32 " segments (activesb %d)\n",
   1336 		      fs->lfs_offset - off, cip->clean - oclean,
   1337 		      fs->lfs_activesb));
   1338 		LFS_SYNC_CLEANERINFO(cip, fs, bp, 0);
   1339 #endif
   1340 
   1341 		VOP_LOCK(ap->a_vp, LK_EXCLUSIVE);
   1342 		return 0;
   1343 
   1344 	    case LFCNIFILEFH:
   1345 		/* Return the filehandle of the Ifile */
   1346 		if ((error = suser(ap->a_l->l_proc->p_ucred, &ap->a_l->l_proc->p_acflag)) != 0)
   1347 			return (error);
   1348 		fhp = (struct fhandle *)ap->a_data;
   1349 		fhp->fh_fsid = *fsidp;
   1350 		return lfs_vptofh(fs->lfs_ivnode, &(fhp->fh_fid));
   1351 
   1352 	    case LFCNREWIND:
   1353 		/* Move lfs_offset to the lowest-numbered segment */
   1354 		return lfs_rewind(fs, *(int *)ap->a_data);
   1355 
   1356 	    case LFCNINVAL:
   1357 		/* Mark a segment SEGUSE_INVAL */
   1358 		LFS_SEGENTRY(sup, fs, *(int *)ap->a_data, bp);
   1359 		if (sup->su_nbytes > 0) {
   1360 			brelse(bp);
   1361 			lfs_unset_inval_all(fs);
   1362 			return EBUSY;
   1363 		}
   1364 		sup->su_flags |= SEGUSE_INVAL;
   1365 		VOP_BWRITE(bp);
   1366 		return 0;
   1367 
   1368 	    case LFCNRESIZE:
   1369 		/* Resize the filesystem */
   1370 		return lfs_resize_fs(fs, *(int *)ap->a_data);
   1371 
   1372 	    default:
   1373 		return ufs_fcntl(v);
   1374 	}
   1375 	return 0;
   1376 }
   1377 
   1378 int
   1379 lfs_getpages(void *v)
   1380 {
   1381 	struct vop_getpages_args /* {
   1382 		struct vnode *a_vp;
   1383 		voff_t a_offset;
   1384 		struct vm_page **a_m;
   1385 		int *a_count;
   1386 		int a_centeridx;
   1387 		vm_prot_t a_access_type;
   1388 		int a_advice;
   1389 		int a_flags;
   1390 	} */ *ap = v;
   1391 
   1392 	if (VTOI(ap->a_vp)->i_number == LFS_IFILE_INUM &&
   1393 	    (ap->a_access_type & VM_PROT_WRITE) != 0) {
   1394 		return EPERM;
   1395 	}
   1396 	if ((ap->a_access_type & VM_PROT_WRITE) != 0) {
   1397 		LFS_SET_UINO(VTOI(ap->a_vp), IN_MODIFIED);
   1398 	}
   1399 
   1400 	/*
   1401 	 * we're relying on the fact that genfs_getpages() always read in
   1402 	 * entire filesystem blocks.
   1403 	 */
   1404 	return genfs_getpages(v);
   1405 }
   1406 
   1407 /*
   1408  * Make sure that for all pages in every block in the given range,
   1409  * either all are dirty or all are clean.  If any of the pages
   1410  * we've seen so far are dirty, put the vnode on the paging chain,
   1411  * and mark it IN_PAGING.
   1412  *
   1413  * If checkfirst != 0, don't check all the pages but return at the
   1414  * first dirty page.
   1415  */
   1416 static int
   1417 check_dirty(struct lfs *fs, struct vnode *vp,
   1418 	    off_t startoffset, off_t endoffset, off_t blkeof,
   1419 	    int flags, int checkfirst)
   1420 {
   1421 	int by_list;
   1422 	struct vm_page *curpg = NULL; /* XXX: gcc */
   1423 	struct vm_page *pgs[MAXBSIZE / PAGE_SIZE], *pg;
   1424 	off_t soff = 0; /* XXX: gcc */
   1425 	voff_t off;
   1426 	int i;
   1427 	int nonexistent;
   1428 	int any_dirty;	/* number of dirty pages */
   1429 	int dirty;	/* number of dirty pages in a block */
   1430 	int tdirty;
   1431 	int pages_per_block = fs->lfs_bsize >> PAGE_SHIFT;
   1432 
   1433 	ASSERT_MAYBE_SEGLOCK(fs);
   1434   top:
   1435 	by_list = (vp->v_uobj.uo_npages <=
   1436 		   ((endoffset - startoffset) >> PAGE_SHIFT) *
   1437 		   UVM_PAGE_HASH_PENALTY);
   1438 	any_dirty = 0;
   1439 
   1440 	if (by_list) {
   1441 		curpg = TAILQ_FIRST(&vp->v_uobj.memq);
   1442 	} else {
   1443 		soff = startoffset;
   1444 	}
   1445 	while (by_list || soff < MIN(blkeof, endoffset)) {
   1446 		if (by_list) {
   1447 			/*
   1448 			 * Find the first page in a block.  Skip
   1449 			 * blocks outside our area of interest or beyond
   1450 			 * the end of file.
   1451 			 */
   1452 			if (pages_per_block > 1) {
   1453 				while (curpg &&
   1454 				       ((curpg->offset & fs->lfs_bmask) ||
   1455 					curpg->offset >= vp->v_size ||
   1456 					curpg->offset >= endoffset))
   1457 					curpg = TAILQ_NEXT(curpg, listq);
   1458 			}
   1459 			if (curpg == NULL)
   1460 				break;
   1461 			soff = curpg->offset;
   1462 		}
   1463 
   1464 		/*
   1465 		 * Mark all pages in extended range busy; find out if any
   1466 		 * of them are dirty.
   1467 		 */
   1468 		nonexistent = dirty = 0;
   1469 		for (i = 0; i == 0 || i < pages_per_block; i++) {
   1470 			if (by_list && pages_per_block <= 1) {
   1471 				pgs[i] = pg = curpg;
   1472 			} else {
   1473 				off = soff + (i << PAGE_SHIFT);
   1474 				pgs[i] = pg = uvm_pagelookup(&vp->v_uobj, off);
   1475 				if (pg == NULL) {
   1476 					++nonexistent;
   1477 					continue;
   1478 				}
   1479 			}
   1480 			KASSERT(pg != NULL);
   1481 			while (pg->flags & PG_BUSY) {
   1482 				pg->flags |= PG_WANTED;
   1483 				UVM_UNLOCK_AND_WAIT(pg, &vp->v_interlock, 0,
   1484 						    "lfsput", 0);
   1485 				simple_lock(&vp->v_interlock);
   1486 				if (by_list) {
   1487 					if (i > 0)
   1488 						uvm_page_unbusy(pgs, i);
   1489 					goto top;
   1490 				}
   1491 			}
   1492 			pg->flags |= PG_BUSY;
   1493 			UVM_PAGE_OWN(pg, "lfs_putpages");
   1494 
   1495 			pmap_page_protect(pg, VM_PROT_NONE);
   1496 			tdirty = (pmap_clear_modify(pg) ||
   1497 				  (pg->flags & PG_CLEAN) == 0);
   1498 			dirty += tdirty;
   1499 		}
   1500 		if (pages_per_block > 0 && nonexistent >= pages_per_block) {
   1501 			if (by_list) {
   1502 				curpg = TAILQ_NEXT(curpg, listq);
   1503 			} else {
   1504 				soff += fs->lfs_bsize;
   1505 			}
   1506 			continue;
   1507 		}
   1508 
   1509 		any_dirty += dirty;
   1510 		KASSERT(nonexistent == 0);
   1511 
   1512 		/*
   1513 		 * If any are dirty make all dirty; unbusy them,
   1514 		 * but if we were asked to clean, wire them so that
   1515 		 * the pagedaemon doesn't bother us about them while
   1516 		 * they're on their way to disk.
   1517 		 */
   1518 		for (i = 0; i == 0 || i < pages_per_block; i++) {
   1519 			pg = pgs[i];
   1520 			KASSERT(!((pg->flags & PG_CLEAN) && (pg->flags & PG_DELWRI)));
   1521 			if (dirty) {
   1522 				pg->flags &= ~PG_CLEAN;
   1523 				if (flags & PGO_FREE) {
   1524 					/*
   1525 					 * Wire the page so that
   1526 					 * pdaemon doesn't see it again.
   1527 					 */
   1528 					uvm_lock_pageq();
   1529 					uvm_pagewire(pg);
   1530 					uvm_unlock_pageq();
   1531 
   1532 					/* Suspended write flag */
   1533 					pg->flags |= PG_DELWRI;
   1534 				}
   1535 			}
   1536 			if (pg->flags & PG_WANTED)
   1537 				wakeup(pg);
   1538 			pg->flags &= ~(PG_WANTED|PG_BUSY);
   1539 			UVM_PAGE_OWN(pg, NULL);
   1540 		}
   1541 
   1542 		if (checkfirst && any_dirty)
   1543 			break;
   1544 
   1545 		if (by_list) {
   1546 			curpg = TAILQ_NEXT(curpg, listq);
   1547 		} else {
   1548 			soff += MAX(PAGE_SIZE, fs->lfs_bsize);
   1549 		}
   1550 	}
   1551 
   1552 	/*
   1553 	 * If any pages were dirty, mark this inode as "pageout requested",
   1554 	 * and put it on the paging queue.
   1555 	 * XXXUBC locking (check locking on dchainhd too)
   1556 	 */
   1557 #ifdef notyet
   1558 	if (any_dirty) {
   1559 		if (!(ip->i_flags & IN_PAGING)) {
   1560 			ip->i_flags |= IN_PAGING;
   1561 			simple_lock(&fs->lfs_interlock);
   1562 			TAILQ_INSERT_TAIL(&fs->lfs_pchainhd, ip, i_lfs_pchain);
   1563 			simple_unlock(&fs->lfs_interlock);
   1564 		}
   1565 	}
   1566 #endif
   1567 	return any_dirty;
   1568 }
   1569 
   1570 /*
   1571  * lfs_putpages functions like genfs_putpages except that
   1572  *
   1573  * (1) It needs to bounds-check the incoming requests to ensure that
   1574  *     they are block-aligned; if they are not, expand the range and
   1575  *     do the right thing in case, e.g., the requested range is clean
   1576  *     but the expanded range is dirty.
   1577  * (2) It needs to explicitly send blocks to be written when it is done.
   1578  *     VOP_PUTPAGES is not ever called with the seglock held, so
   1579  *     we simply take the seglock and let lfs_segunlock wait for us.
   1580  *     XXX Actually we can be called with the seglock held, if we have
   1581  *     XXX to flush a vnode while lfs_markv is in operation.  As of this
   1582  *     XXX writing we panic in this case.
   1583  *
   1584  * Assumptions:
   1585  *
   1586  * (1) The caller does not hold any pages in this vnode busy.  If it does,
   1587  *     there is a danger that when we expand the page range and busy the
   1588  *     pages we will deadlock.
   1589  * (2) We are called with vp->v_interlock held; we must return with it
   1590  *     released.
   1591  * (3) We don't absolutely have to free pages right away, provided that
   1592  *     the request does not have PGO_SYNCIO.  When the pagedaemon gives
   1593  *     us a request with PGO_FREE, we take the pages out of the paging
   1594  *     queue and wake up the writer, which will handle freeing them for us.
   1595  *
   1596  *     We ensure that for any filesystem block, all pages for that
   1597  *     block are either resident or not, even if those pages are higher
   1598  *     than EOF; that means that we will be getting requests to free
   1599  *     "unused" pages above EOF all the time, and should ignore them.
   1600  *
   1601  * XXX note that we're (ab)using PGO_LOCKED as "seglock held".
   1602  */
   1603 
   1604 int
   1605 lfs_putpages(void *v)
   1606 {
   1607 	int error;
   1608 	struct vop_putpages_args /* {
   1609 		struct vnode *a_vp;
   1610 		voff_t a_offlo;
   1611 		voff_t a_offhi;
   1612 		int a_flags;
   1613 	} */ *ap = v;
   1614 	struct vnode *vp;
   1615 	struct inode *ip;
   1616 	struct lfs *fs;
   1617 	struct segment *sp;
   1618 	off_t origoffset, startoffset, endoffset, origendoffset, blkeof;
   1619 	off_t off, max_endoffset;
   1620 	int s;
   1621 	boolean_t seglocked, sync, pagedaemon;
   1622 	struct vm_page *pg;
   1623 	UVMHIST_FUNC("lfs_putpages"); UVMHIST_CALLED(ubchist);
   1624 
   1625 	vp = ap->a_vp;
   1626 	ip = VTOI(vp);
   1627 	fs = ip->i_lfs;
   1628 	sync = (ap->a_flags & PGO_SYNCIO) != 0;
   1629 	pagedaemon = (curproc == uvm.pagedaemon_proc);
   1630 
   1631 	/* Putpages does nothing for metadata. */
   1632 	if (vp == fs->lfs_ivnode || vp->v_type != VREG) {
   1633 		simple_unlock(&vp->v_interlock);
   1634 		return 0;
   1635 	}
   1636 
   1637 	/*
   1638 	 * If there are no pages, don't do anything.
   1639 	 */
   1640 	if (vp->v_uobj.uo_npages == 0) {
   1641 		s = splbio();
   1642 		if (LIST_FIRST(&vp->v_dirtyblkhd) == NULL &&
   1643 		    (vp->v_flag & VONWORKLST)) {
   1644 			vp->v_flag &= ~VONWORKLST;
   1645 			LIST_REMOVE(vp, v_synclist);
   1646 		}
   1647 		splx(s);
   1648 		simple_unlock(&vp->v_interlock);
   1649 		return 0;
   1650 	}
   1651 
   1652 	blkeof = blkroundup(fs, ip->i_size);
   1653 
   1654 	/*
   1655 	 * Ignore requests to free pages past EOF but in the same block
   1656 	 * as EOF, unless the request is synchronous. (XXX why sync?)
   1657 	 * XXXUBC Make these pages look "active" so the pagedaemon won't
   1658 	 * XXXUBC bother us with them again.
   1659 	 */
   1660 	if (!sync && ap->a_offlo >= ip->i_size && ap->a_offlo < blkeof) {
   1661 		origoffset = ap->a_offlo;
   1662 		for (off = origoffset; off < blkeof; off += fs->lfs_bsize) {
   1663 			pg = uvm_pagelookup(&vp->v_uobj, off);
   1664 			KASSERT(pg != NULL);
   1665 			while (pg->flags & PG_BUSY) {
   1666 				pg->flags |= PG_WANTED;
   1667 				UVM_UNLOCK_AND_WAIT(pg, &vp->v_interlock, 0,
   1668 						    "lfsput2", 0);
   1669 				simple_lock(&vp->v_interlock);
   1670 			}
   1671 			uvm_lock_pageq();
   1672 			uvm_pageactivate(pg);
   1673 			uvm_unlock_pageq();
   1674 		}
   1675 		ap->a_offlo = blkeof;
   1676 		if (ap->a_offhi > 0 && ap->a_offhi <= ap->a_offlo) {
   1677 			simple_unlock(&vp->v_interlock);
   1678 			return 0;
   1679 		}
   1680 	}
   1681 
   1682 	/*
   1683 	 * Extend page range to start and end at block boundaries.
   1684 	 * (For the purposes of VOP_PUTPAGES, fragments don't exist.)
   1685 	 */
   1686 	origoffset = ap->a_offlo;
   1687 	origendoffset = ap->a_offhi;
   1688 	startoffset = origoffset & ~(fs->lfs_bmask);
   1689 	max_endoffset = (trunc_page(LLONG_MAX) >> fs->lfs_bshift)
   1690 					       << fs->lfs_bshift;
   1691 
   1692 	if (origendoffset == 0 || ap->a_flags & PGO_ALLPAGES) {
   1693 		endoffset = max_endoffset;
   1694 		origendoffset = endoffset;
   1695 	} else {
   1696 		origendoffset = round_page(ap->a_offhi);
   1697 		endoffset = round_page(blkroundup(fs, origendoffset));
   1698 	}
   1699 
   1700 	KASSERT(startoffset > 0 || endoffset >= startoffset);
   1701 	if (startoffset == endoffset) {
   1702 		/* Nothing to do, why were we called? */
   1703 		simple_unlock(&vp->v_interlock);
   1704 		DLOG((DLOG_PAGE, "lfs_putpages: startoffset = endoffset = %"
   1705 		      PRId64 "\n", startoffset));
   1706 		return 0;
   1707 	}
   1708 
   1709 	ap->a_offlo = startoffset;
   1710 	ap->a_offhi = endoffset;
   1711 
   1712 	if (!(ap->a_flags & PGO_CLEANIT))
   1713 		return genfs_putpages(v);
   1714 
   1715 	/*
   1716 	 * If there are more than one page per block, we don't want
   1717 	 * to get caught locking them backwards; so set PGO_BUSYFAIL
   1718 	 * to avoid deadlocks.
   1719 	 */
   1720 	ap->a_flags |= PGO_BUSYFAIL;
   1721 
   1722 	do {
   1723 		int r;
   1724 
   1725 		/* If no pages are dirty, we can just use genfs_putpages. */
   1726 		if (check_dirty(fs, vp, startoffset, endoffset, blkeof,
   1727 				ap->a_flags, 1) != 0)
   1728 			break;
   1729 
   1730 		/*
   1731 		 * Sometimes pages are dirtied between the time that
   1732 		 * we check and the time we try to clean them.
   1733 		 * Instruct lfs_gop_write to return EDEADLK in this case
   1734 		 * so we can write them properly.
   1735 		 */
   1736 		ip->i_lfs_iflags |= LFSI_NO_GOP_WRITE;
   1737 		r = genfs_putpages(v);
   1738 		ip->i_lfs_iflags &= ~LFSI_NO_GOP_WRITE;
   1739 		if (r != EDEADLK)
   1740 			return r;
   1741 
   1742 		/* Start over. */
   1743 		preempt(1);
   1744 		simple_lock(&vp->v_interlock);
   1745 	} while(1);
   1746 
   1747 	/*
   1748 	 * Dirty and asked to clean.
   1749 	 *
   1750 	 * Pagedaemon can't actually write LFS pages; wake up
   1751 	 * the writer to take care of that.  The writer will
   1752 	 * notice the pager inode queue and act on that.
   1753 	 */
   1754 	if (pagedaemon) {
   1755 		simple_lock(&fs->lfs_interlock);
   1756 		++fs->lfs_pdflush;
   1757 		simple_unlock(&fs->lfs_interlock);
   1758 		wakeup(&lfs_writer_daemon);
   1759 		simple_unlock(&vp->v_interlock);
   1760 		return EWOULDBLOCK;
   1761 	}
   1762 
   1763 	/*
   1764 	 * If this is a file created in a recent dirop, we can't flush its
   1765 	 * inode until the dirop is complete.  Drain dirops, then flush the
   1766 	 * filesystem (taking care of any other pending dirops while we're
   1767 	 * at it).
   1768 	 */
   1769 	if ((ap->a_flags & (PGO_CLEANIT|PGO_LOCKED)) == PGO_CLEANIT &&
   1770 	    (vp->v_flag & VDIROP)) {
   1771 		int locked;
   1772 
   1773 		DLOG((DLOG_PAGE, "lfs_putpages: flushing VDIROP\n"));
   1774 		locked = VOP_ISLOCKED(vp) && /* XXX */
   1775 			vp->v_lock.lk_lockholder == curproc->p_pid;
   1776 		simple_unlock(&vp->v_interlock);
   1777 		lfs_writer_enter(fs, "ppdirop");
   1778 		if (locked)
   1779 			VOP_UNLOCK(vp, 0);
   1780 
   1781 		simple_lock(&fs->lfs_interlock);
   1782 		lfs_flush_fs(fs, sync ? SEGM_SYNC : 0);
   1783 		simple_unlock(&fs->lfs_interlock);
   1784 
   1785 		simple_lock(&vp->v_interlock);
   1786 		if (locked) {
   1787 			VOP_LOCK(vp, LK_EXCLUSIVE | LK_INTERLOCK);
   1788 			simple_lock(&vp->v_interlock);
   1789 		}
   1790 		lfs_writer_leave(fs);
   1791 
   1792 		/* XXX the flush should have taken care of this one too! */
   1793 	}
   1794 
   1795 	/*
   1796 	 * This is it.	We are going to write some pages.  From here on
   1797 	 * down it's all just mechanics.
   1798 	 *
   1799 	 * Don't let genfs_putpages wait; lfs_segunlock will wait for us.
   1800 	 */
   1801 	ap->a_flags &= ~PGO_SYNCIO;
   1802 
   1803 	/*
   1804 	 * If we've already got the seglock, flush the node and return.
   1805 	 * The FIP has already been set up for us by lfs_writefile,
   1806 	 * and FIP cleanup and lfs_updatemeta will also be done there,
   1807 	 * unless genfs_putpages returns EDEADLK; then we must flush
   1808 	 * what we have, and correct FIP and segment header accounting.
   1809 	 */
   1810 
   1811 	seglocked = (ap->a_flags & PGO_LOCKED) != 0;
   1812 	if (!seglocked) {
   1813 		simple_unlock(&vp->v_interlock);
   1814 		/*
   1815 		 * Take the seglock, because we are going to be writing pages.
   1816 		 */
   1817 		error = lfs_seglock(fs, SEGM_PROT | (sync ? SEGM_SYNC : 0));
   1818 		if (error != 0)
   1819 			return error;
   1820 		simple_lock(&vp->v_interlock);
   1821 	}
   1822 
   1823 	/*
   1824 	 * VOP_PUTPAGES should not be called while holding the seglock.
   1825 	 * XXXUBC fix lfs_markv, or do this properly.
   1826 	 */
   1827 #ifdef notyet
   1828 	KASSERT(fs->lfs_seglock == 1);
   1829 #endif /* notyet */
   1830 
   1831 	/*
   1832 	 * We assume we're being called with sp->fip pointing at blank space.
   1833 	 * Account for a new FIP in the segment header, and set sp->vp.
   1834 	 * (This should duplicate the setup at the top of lfs_writefile().)
   1835 	 */
   1836 	sp = fs->lfs_sp;
   1837 	if (!seglocked) {
   1838 		if (sp->seg_bytes_left < fs->lfs_bsize ||
   1839 		    sp->sum_bytes_left < sizeof(struct finfo))
   1840 			(void) lfs_writeseg(fs, fs->lfs_sp);
   1841 
   1842 		sp->sum_bytes_left -= FINFOSIZE;
   1843 		++((SEGSUM *)(sp->segsum))->ss_nfinfo;
   1844 	}
   1845 	KASSERT(sp->vp == NULL);
   1846 	sp->vp = vp;
   1847 
   1848 	if (!seglocked) {
   1849 		if (vp->v_flag & VDIROP)
   1850 			((SEGSUM *)(sp->segsum))->ss_flags |= (SS_DIROP|SS_CONT);
   1851 	}
   1852 
   1853 	sp->fip->fi_nblocks = 0;
   1854 	sp->fip->fi_ino = ip->i_number;
   1855 	sp->fip->fi_version = ip->i_gen;
   1856 
   1857 	/*
   1858 	 * Loop through genfs_putpages until all pages are gathered.
   1859 	 * genfs_putpages() drops the interlock, so reacquire it if necessary.
   1860 	 * Whenever we lose the interlock we have to rerun check_dirty, as
   1861 	 * well.
   1862 	 */
   1863 again:
   1864 	check_dirty(fs, vp, startoffset, endoffset, blkeof, ap->a_flags, 0);
   1865 
   1866 	if ((error = genfs_putpages(v)) == EDEADLK) {
   1867 		DLOG((DLOG_PAGE, "lfs_putpages: genfs_putpages returned"
   1868 		      " EDEADLK [2] ino %d off %x (seg %d)\n",
   1869 		      ip->i_number, fs->lfs_offset,
   1870 		      dtosn(fs, fs->lfs_offset)));
   1871 		/* If nothing to write, short-circuit */
   1872 		if (sp->cbpp - sp->bpp > 1) {
   1873 			/* Write gathered pages */
   1874 			lfs_updatemeta(sp);
   1875 			(void) lfs_writeseg(fs, sp);
   1876 
   1877 			/*
   1878 			 * Reinitialize brand new FIP and add us to it.
   1879 			 * (This should duplicate the fixup in
   1880 			 * lfs_gatherpages().)
   1881 			 */
   1882 			KASSERT(sp->vp == vp);
   1883 			sp->fip->fi_version = ip->i_gen;
   1884 			sp->fip->fi_ino = ip->i_number;
   1885 			/* Add us to the new segment summary. */
   1886 			++((SEGSUM *)(sp->segsum))->ss_nfinfo;
   1887 			sp->sum_bytes_left -= FINFOSIZE;
   1888 		}
   1889 
   1890 		/* Give the write a chance to complete */
   1891 		preempt(1);
   1892 
   1893 		/* We've lost the interlock.  Start over. */
   1894 		simple_lock(&vp->v_interlock);
   1895 		goto again;
   1896 	}
   1897 
   1898 	KASSERT(sp->vp == vp);
   1899 	if (!seglocked) {
   1900 		sp->vp = NULL; /* XXX lfs_gather below will set this */
   1901 
   1902 		/* Write indirect blocks as well */
   1903 		lfs_gather(fs, fs->lfs_sp, vp, lfs_match_indir);
   1904 		lfs_gather(fs, fs->lfs_sp, vp, lfs_match_dindir);
   1905 		lfs_gather(fs, fs->lfs_sp, vp, lfs_match_tindir);
   1906 
   1907 		KASSERT(sp->vp == NULL);
   1908 		sp->vp = vp;
   1909 	}
   1910 
   1911 	/*
   1912 	 * Blocks are now gathered into a segment waiting to be written.
   1913 	 * All that's left to do is update metadata, and write them.
   1914 	 */
   1915 	lfs_updatemeta(sp);
   1916 	KASSERT(sp->vp == vp);
   1917 	sp->vp = NULL;
   1918 
   1919 	if (seglocked) {
   1920 		/* we're called by lfs_writefile. */
   1921 		return error;
   1922 	}
   1923 
   1924 	/*
   1925 	 * Clean up FIP, since we're done writing this file.
   1926 	 * This should duplicate cleanup at the end of lfs_writefile().
   1927 	 */
   1928 	if (sp->fip->fi_nblocks != 0) {
   1929 		sp->fip = (FINFO*)((caddr_t)sp->fip + FINFOSIZE +
   1930 			sizeof(int32_t) * sp->fip->fi_nblocks);
   1931 		sp->start_lbp = &sp->fip->fi_blocks[0];
   1932 	} else {
   1933 		sp->sum_bytes_left += FINFOSIZE;
   1934 		--((SEGSUM *)(sp->segsum))->ss_nfinfo;
   1935 	}
   1936 	lfs_writeseg(fs, fs->lfs_sp);
   1937 
   1938 	/*
   1939 	 * XXX - with the malloc/copy writeseg, the pages are freed by now
   1940 	 * even if we don't wait (e.g. if we hold a nested lock).  This
   1941 	 * will not be true if we stop using malloc/copy.
   1942 	 */
   1943 	KASSERT(fs->lfs_sp->seg_flags & SEGM_PROT);
   1944 	lfs_segunlock(fs);
   1945 
   1946 	/*
   1947 	 * Wait for v_numoutput to drop to zero.  The seglock should
   1948 	 * take care of this, but there is a slight possibility that
   1949 	 * aiodoned might not have got around to our buffers yet.
   1950 	 */
   1951 	if (sync) {
   1952 		s = splbio();
   1953 		simple_lock(&global_v_numoutput_slock);
   1954 		while (vp->v_numoutput > 0) {
   1955 			DLOG((DLOG_PAGE, "lfs_putpages: ino %d sleeping on"
   1956 			      " num %d\n", ip->i_number, vp->v_numoutput));
   1957 			vp->v_flag |= VBWAIT;
   1958 			ltsleep(&vp->v_numoutput, PRIBIO + 1, "lfs_vn", 0,
   1959 			    &global_v_numoutput_slock);
   1960 		}
   1961 		simple_unlock(&global_v_numoutput_slock);
   1962 		splx(s);
   1963 	}
   1964 	return error;
   1965 }
   1966 
   1967 /*
   1968  * Return the last logical file offset that should be written for this file
   1969  * if we're doing a write that ends at "size".	If writing, we need to know
   1970  * about sizes on disk, i.e. fragments if there are any; if reading, we need
   1971  * to know about entire blocks.
   1972  */
   1973 void
   1974 lfs_gop_size(struct vnode *vp, off_t size, off_t *eobp, int flags)
   1975 {
   1976 	struct inode *ip = VTOI(vp);
   1977 	struct lfs *fs = ip->i_lfs;
   1978 	daddr_t olbn, nlbn;
   1979 
   1980 	KASSERT(flags & (GOP_SIZE_READ | GOP_SIZE_WRITE));
   1981 	KASSERT((flags & (GOP_SIZE_READ | GOP_SIZE_WRITE))
   1982 		!= (GOP_SIZE_READ | GOP_SIZE_WRITE));
   1983 
   1984 	olbn = lblkno(fs, ip->i_size);
   1985 	nlbn = lblkno(fs, size);
   1986 	if (!(flags & GOP_SIZE_MEM) && nlbn < NDADDR && olbn <= nlbn) {
   1987 		*eobp = fragroundup(fs, size);
   1988 	} else {
   1989 		*eobp = blkroundup(fs, size);
   1990 	}
   1991 }
   1992 
   1993 #ifdef DEBUG
   1994 void lfs_dump_vop(void *);
   1995 
   1996 void
   1997 lfs_dump_vop(void *v)
   1998 {
   1999 	struct vop_putpages_args /* {
   2000 		struct vnode *a_vp;
   2001 		voff_t a_offlo;
   2002 		voff_t a_offhi;
   2003 		int a_flags;
   2004 	} */ *ap = v;
   2005 
   2006 #ifdef DDB
   2007 	vfs_vnode_print(ap->a_vp, 0, printf);
   2008 #endif
   2009 	lfs_dump_dinode(VTOI(ap->a_vp)->i_din.ffs1_din);
   2010 }
   2011 #endif
   2012 
   2013 int
   2014 lfs_mmap(void *v)
   2015 {
   2016 	struct vop_mmap_args /* {
   2017 		const struct vnodeop_desc *a_desc;
   2018 		struct vnode *a_vp;
   2019 		int a_fflags;
   2020 		struct ucred *a_cred;
   2021 		struct lwp *a_l;
   2022 	} */ *ap = v;
   2023 
   2024 	if (VTOI(ap->a_vp)->i_number == LFS_IFILE_INUM)
   2025 		return EOPNOTSUPP;
   2026 	return ufs_mmap(v);
   2027 }
   2028