Home | History | Annotate | Line # | Download | only in lfs
lfs_vnops.c revision 1.83
      1 /*	$NetBSD: lfs_vnops.c,v 1.83 2003/02/03 00:32:35 perseant Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1999, 2000 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. All advertising materials mentioning features or use of this software
     51  *    must display the following acknowledgement:
     52  *	This product includes software developed by the University of
     53  *	California, Berkeley and its contributors.
     54  * 4. Neither the name of the University nor the names of its contributors
     55  *    may be used to endorse or promote products derived from this software
     56  *    without specific prior written permission.
     57  *
     58  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     59  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     60  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     61  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     62  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     63  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     64  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     65  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     66  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     67  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     68  * SUCH DAMAGE.
     69  *
     70  *	@(#)lfs_vnops.c	8.13 (Berkeley) 6/10/95
     71  */
     72 
     73 #include <sys/cdefs.h>
     74 __KERNEL_RCSID(0, "$NetBSD: lfs_vnops.c,v 1.83 2003/02/03 00:32:35 perseant Exp $");
     75 
     76 #include <sys/param.h>
     77 #include <sys/systm.h>
     78 #include <sys/namei.h>
     79 #include <sys/resourcevar.h>
     80 #include <sys/kernel.h>
     81 #include <sys/file.h>
     82 #include <sys/stat.h>
     83 #include <sys/buf.h>
     84 #include <sys/proc.h>
     85 #include <sys/mount.h>
     86 #include <sys/vnode.h>
     87 #include <sys/malloc.h>
     88 #include <sys/pool.h>
     89 #include <sys/signalvar.h>
     90 
     91 #include <miscfs/fifofs/fifo.h>
     92 #include <miscfs/genfs/genfs.h>
     93 #include <miscfs/specfs/specdev.h>
     94 
     95 #include <ufs/ufs/inode.h>
     96 #include <ufs/ufs/dir.h>
     97 #include <ufs/ufs/ufsmount.h>
     98 #include <ufs/ufs/ufs_extern.h>
     99 
    100 #include <ufs/lfs/lfs.h>
    101 #include <ufs/lfs/lfs_extern.h>
    102 
    103 /* Global vfs data structures for lfs. */
    104 int (**lfs_vnodeop_p)(void *);
    105 const struct vnodeopv_entry_desc lfs_vnodeop_entries[] = {
    106 	{ &vop_default_desc, vn_default_error },
    107 	{ &vop_lookup_desc, ufs_lookup },		/* lookup */
    108 	{ &vop_create_desc, lfs_create },		/* create */
    109 	{ &vop_whiteout_desc, ufs_whiteout },		/* whiteout */
    110 	{ &vop_mknod_desc, lfs_mknod },			/* mknod */
    111 	{ &vop_open_desc, ufs_open },			/* open */
    112 	{ &vop_close_desc, lfs_close },			/* close */
    113 	{ &vop_access_desc, ufs_access },		/* access */
    114 	{ &vop_getattr_desc, lfs_getattr },		/* getattr */
    115 	{ &vop_setattr_desc, lfs_setattr },		/* setattr */
    116 	{ &vop_read_desc, lfs_read },			/* read */
    117 	{ &vop_write_desc, lfs_write },			/* write */
    118 	{ &vop_lease_desc, ufs_lease_check },		/* lease */
    119 	{ &vop_ioctl_desc, ufs_ioctl },			/* ioctl */
    120 	{ &vop_fcntl_desc, ufs_fcntl },			/* fcntl */
    121 	{ &vop_poll_desc, ufs_poll },			/* poll */
    122 	{ &vop_kqfilter_desc, genfs_kqfilter },		/* kqfilter */
    123 	{ &vop_revoke_desc, ufs_revoke },		/* revoke */
    124 	{ &vop_mmap_desc, ufs_mmap },			/* mmap */
    125 	{ &vop_fsync_desc, lfs_fsync },			/* fsync */
    126 	{ &vop_seek_desc, ufs_seek },			/* seek */
    127 	{ &vop_remove_desc, lfs_remove },		/* remove */
    128 	{ &vop_link_desc, lfs_link },			/* link */
    129 	{ &vop_rename_desc, lfs_rename },		/* rename */
    130 	{ &vop_mkdir_desc, lfs_mkdir },			/* mkdir */
    131 	{ &vop_rmdir_desc, lfs_rmdir },			/* rmdir */
    132 	{ &vop_symlink_desc, lfs_symlink },		/* symlink */
    133 	{ &vop_readdir_desc, ufs_readdir },		/* readdir */
    134 	{ &vop_readlink_desc, ufs_readlink },		/* readlink */
    135 	{ &vop_abortop_desc, ufs_abortop },		/* abortop */
    136 	{ &vop_inactive_desc, lfs_inactive },		/* inactive */
    137 	{ &vop_reclaim_desc, lfs_reclaim },		/* reclaim */
    138 	{ &vop_lock_desc, ufs_lock },			/* lock */
    139 	{ &vop_unlock_desc, ufs_unlock },		/* unlock */
    140 	{ &vop_bmap_desc, ufs_bmap },			/* bmap */
    141 	{ &vop_strategy_desc, ufs_strategy },		/* strategy */
    142 	{ &vop_print_desc, ufs_print },			/* print */
    143 	{ &vop_islocked_desc, ufs_islocked },		/* islocked */
    144 	{ &vop_pathconf_desc, ufs_pathconf },		/* pathconf */
    145 	{ &vop_advlock_desc, ufs_advlock },		/* advlock */
    146 	{ &vop_blkatoff_desc, lfs_blkatoff },		/* blkatoff */
    147 	{ &vop_valloc_desc, lfs_valloc },		/* valloc */
    148 	{ &vop_balloc_desc, lfs_balloc },		/* balloc */
    149 	{ &vop_vfree_desc, lfs_vfree },			/* vfree */
    150 	{ &vop_truncate_desc, lfs_truncate },		/* truncate */
    151 	{ &vop_update_desc, lfs_update },		/* update */
    152 	{ &vop_bwrite_desc, lfs_bwrite },		/* bwrite */
    153 	{ &vop_getpages_desc, lfs_getpages },		/* getpages */
    154 	{ &vop_putpages_desc, lfs_putpages },		/* putpages */
    155 	{ NULL, NULL }
    156 };
    157 const struct vnodeopv_desc lfs_vnodeop_opv_desc =
    158 	{ &lfs_vnodeop_p, lfs_vnodeop_entries };
    159 
    160 int (**lfs_specop_p)(void *);
    161 const struct vnodeopv_entry_desc lfs_specop_entries[] = {
    162 	{ &vop_default_desc, vn_default_error },
    163 	{ &vop_lookup_desc, spec_lookup },		/* lookup */
    164 	{ &vop_create_desc, spec_create },		/* create */
    165 	{ &vop_mknod_desc, spec_mknod },		/* mknod */
    166 	{ &vop_open_desc, spec_open },			/* open */
    167 	{ &vop_close_desc, lfsspec_close },		/* close */
    168 	{ &vop_access_desc, ufs_access },		/* access */
    169 	{ &vop_getattr_desc, lfs_getattr },		/* getattr */
    170 	{ &vop_setattr_desc, lfs_setattr },		/* setattr */
    171 	{ &vop_read_desc, ufsspec_read },		/* read */
    172 	{ &vop_write_desc, ufsspec_write },		/* write */
    173 	{ &vop_lease_desc, spec_lease_check },		/* lease */
    174 	{ &vop_ioctl_desc, spec_ioctl },		/* ioctl */
    175 	{ &vop_fcntl_desc, ufs_fcntl },			/* fcntl */
    176 	{ &vop_poll_desc, spec_poll },			/* poll */
    177 	{ &vop_kqfilter_desc, spec_kqfilter },		/* kqfilter */
    178 	{ &vop_revoke_desc, spec_revoke },		/* revoke */
    179 	{ &vop_mmap_desc, spec_mmap },			/* mmap */
    180 	{ &vop_fsync_desc, spec_fsync },		/* fsync */
    181 	{ &vop_seek_desc, spec_seek },			/* seek */
    182 	{ &vop_remove_desc, spec_remove },		/* remove */
    183 	{ &vop_link_desc, spec_link },			/* link */
    184 	{ &vop_rename_desc, spec_rename },		/* rename */
    185 	{ &vop_mkdir_desc, spec_mkdir },		/* mkdir */
    186 	{ &vop_rmdir_desc, spec_rmdir },		/* rmdir */
    187 	{ &vop_symlink_desc, spec_symlink },		/* symlink */
    188 	{ &vop_readdir_desc, spec_readdir },		/* readdir */
    189 	{ &vop_readlink_desc, spec_readlink },		/* readlink */
    190 	{ &vop_abortop_desc, spec_abortop },		/* abortop */
    191 	{ &vop_inactive_desc, lfs_inactive },		/* inactive */
    192 	{ &vop_reclaim_desc, lfs_reclaim },		/* reclaim */
    193 	{ &vop_lock_desc, ufs_lock },			/* lock */
    194 	{ &vop_unlock_desc, ufs_unlock },		/* unlock */
    195 	{ &vop_bmap_desc, spec_bmap },			/* bmap */
    196 	{ &vop_strategy_desc, spec_strategy },		/* strategy */
    197 	{ &vop_print_desc, ufs_print },			/* print */
    198 	{ &vop_islocked_desc, ufs_islocked },		/* islocked */
    199 	{ &vop_pathconf_desc, spec_pathconf },		/* pathconf */
    200 	{ &vop_advlock_desc, spec_advlock },		/* advlock */
    201 	{ &vop_blkatoff_desc, spec_blkatoff },		/* blkatoff */
    202 	{ &vop_valloc_desc, spec_valloc },		/* valloc */
    203 	{ &vop_vfree_desc, lfs_vfree },			/* vfree */
    204 	{ &vop_truncate_desc, spec_truncate },		/* truncate */
    205 	{ &vop_update_desc, lfs_update },		/* update */
    206 	{ &vop_bwrite_desc, vn_bwrite },		/* bwrite */
    207 	{ &vop_getpages_desc, spec_getpages },		/* getpages */
    208 	{ &vop_putpages_desc, spec_putpages },		/* putpages */
    209 	{ NULL, NULL }
    210 };
    211 const struct vnodeopv_desc lfs_specop_opv_desc =
    212 	{ &lfs_specop_p, lfs_specop_entries };
    213 
    214 int (**lfs_fifoop_p)(void *);
    215 const struct vnodeopv_entry_desc lfs_fifoop_entries[] = {
    216 	{ &vop_default_desc, vn_default_error },
    217 	{ &vop_lookup_desc, fifo_lookup },		/* lookup */
    218 	{ &vop_create_desc, fifo_create },		/* create */
    219 	{ &vop_mknod_desc, fifo_mknod },		/* mknod */
    220 	{ &vop_open_desc, fifo_open },			/* open */
    221 	{ &vop_close_desc, lfsfifo_close },		/* close */
    222 	{ &vop_access_desc, ufs_access },		/* access */
    223 	{ &vop_getattr_desc, lfs_getattr },		/* getattr */
    224 	{ &vop_setattr_desc, lfs_setattr },		/* setattr */
    225 	{ &vop_read_desc, ufsfifo_read },		/* read */
    226 	{ &vop_write_desc, ufsfifo_write },		/* write */
    227 	{ &vop_lease_desc, fifo_lease_check },		/* lease */
    228 	{ &vop_ioctl_desc, fifo_ioctl },		/* ioctl */
    229 	{ &vop_fcntl_desc, ufs_fcntl },			/* fcntl */
    230 	{ &vop_poll_desc, fifo_poll },			/* poll */
    231 	{ &vop_kqfilter_desc, fifo_kqfilter },		/* kqfilter */
    232 	{ &vop_revoke_desc, fifo_revoke },		/* revoke */
    233 	{ &vop_mmap_desc, fifo_mmap },			/* mmap */
    234 	{ &vop_fsync_desc, fifo_fsync },		/* fsync */
    235 	{ &vop_seek_desc, fifo_seek },			/* seek */
    236 	{ &vop_remove_desc, fifo_remove },		/* remove */
    237 	{ &vop_link_desc, fifo_link },			/* link */
    238 	{ &vop_rename_desc, fifo_rename },		/* rename */
    239 	{ &vop_mkdir_desc, fifo_mkdir },		/* mkdir */
    240 	{ &vop_rmdir_desc, fifo_rmdir },		/* rmdir */
    241 	{ &vop_symlink_desc, fifo_symlink },		/* symlink */
    242 	{ &vop_readdir_desc, fifo_readdir },		/* readdir */
    243 	{ &vop_readlink_desc, fifo_readlink },		/* readlink */
    244 	{ &vop_abortop_desc, fifo_abortop },		/* abortop */
    245 	{ &vop_inactive_desc, lfs_inactive },		/* inactive */
    246 	{ &vop_reclaim_desc, lfs_reclaim },		/* reclaim */
    247 	{ &vop_lock_desc, ufs_lock },			/* lock */
    248 	{ &vop_unlock_desc, ufs_unlock },		/* unlock */
    249 	{ &vop_bmap_desc, fifo_bmap },			/* bmap */
    250 	{ &vop_strategy_desc, fifo_strategy },		/* strategy */
    251 	{ &vop_print_desc, ufs_print },			/* print */
    252 	{ &vop_islocked_desc, ufs_islocked },		/* islocked */
    253 	{ &vop_pathconf_desc, fifo_pathconf },		/* pathconf */
    254 	{ &vop_advlock_desc, fifo_advlock },		/* advlock */
    255 	{ &vop_blkatoff_desc, fifo_blkatoff },		/* blkatoff */
    256 	{ &vop_valloc_desc, fifo_valloc },		/* valloc */
    257 	{ &vop_vfree_desc, lfs_vfree },			/* vfree */
    258 	{ &vop_truncate_desc, fifo_truncate },		/* truncate */
    259 	{ &vop_update_desc, lfs_update },		/* update */
    260 	{ &vop_bwrite_desc, lfs_bwrite },		/* bwrite */
    261 	{ &vop_putpages_desc, fifo_putpages }, 		/* putpages */
    262 	{ NULL, NULL }
    263 };
    264 const struct vnodeopv_desc lfs_fifoop_opv_desc =
    265 	{ &lfs_fifoop_p, lfs_fifoop_entries };
    266 
    267 /*
    268  * A function version of LFS_ITIMES, for the UFS functions which call ITIMES
    269  */
    270 void
    271 lfs_itimes(struct inode *ip, struct timespec *acc, struct timespec *mod, struct timespec *cre)
    272 {
    273 	LFS_ITIMES(ip, acc, mod, cre);
    274 }
    275 
    276 #define	LFS_READWRITE
    277 #include <ufs/ufs/ufs_readwrite.c>
    278 #undef	LFS_READWRITE
    279 
    280 /*
    281  * Synch an open file.
    282  */
    283 /* ARGSUSED */
    284 int
    285 lfs_fsync(void *v)
    286 {
    287 	struct vop_fsync_args /* {
    288 		struct vnode *a_vp;
    289 		struct ucred *a_cred;
    290 		int a_flags;
    291 		off_t offlo;
    292 		off_t offhi;
    293 		struct proc *a_p;
    294 	} */ *ap = v;
    295 	struct vnode *vp = ap->a_vp;
    296 	int error;
    297 
    298 	/* Ignore the trickle syncer */
    299 	if (ap->a_flags & FSYNC_LAZY)
    300 		return 0;
    301 
    302 	simple_lock(&vp->v_interlock);
    303 	error = VOP_PUTPAGES(vp, trunc_page(ap->a_offlo),
    304                     round_page(ap->a_offhi), PGO_CLEANIT | PGO_SYNCIO);
    305 	if (error)
    306 		return error;
    307 	error = VOP_UPDATE(vp, NULL, NULL,
    308 			   (ap->a_flags & FSYNC_WAIT) != 0 ? UPDATE_WAIT : 0);
    309 #ifdef DEBUG
    310 	/*
    311 	 * If we were called from vinvalbuf and lfs_update
    312 	 * didn't flush all our buffers, we're in trouble.
    313 	 */
    314 	if ((ap->a_flags & FSYNC_WAIT) && LIST_FIRST(&vp->v_dirtyblkhd) != NULL) {
    315 		struct buf *bp;
    316 
    317 		bp = LIST_FIRST(&vp->v_dirtyblkhd);
    318 		printf("lfs_fsync: ino %d failed to sync", VTOI(vp)->i_number);
    319 		printf("lfs_fsync: iocount = %d\n", VTOI(vp)->i_lfs->lfs_iocount);
    320 		printf("lfs_fsync: flags are 0x%x, numoutput=%d\n",
    321 			VTOI(vp)->i_flag, vp->v_numoutput);
    322 		printf("lfs_fsync: writecount=%ld\n", vp->v_writecount);
    323 		printf("lfs_fsync: first bp: %p, flags=0x%lx, lbn=%" PRId64 "\n",
    324 			bp, bp->b_flags, bp->b_lblkno);
    325 	}
    326 #endif
    327 	return error;
    328 }
    329 
    330 /*
    331  * Take IN_ADIROP off, then call ufs_inactive.
    332  */
    333 int
    334 lfs_inactive(void *v)
    335 {
    336 	struct vop_inactive_args /* {
    337 		struct vnode *a_vp;
    338 		struct proc *a_p;
    339 	} */ *ap = v;
    340 
    341 	KASSERT(VTOI(ap->a_vp)->i_ffs_nlink == VTOI(ap->a_vp)->i_ffs_effnlink);
    342 
    343 	lfs_unmark_vnode(ap->a_vp);
    344 
    345 	return ufs_inactive(v);
    346 }
    347 
    348 /*
    349  * These macros are used to bracket UFS directory ops, so that we can
    350  * identify all the pages touched during directory ops which need to
    351  * be ordered and flushed atomically, so that they may be recovered.
    352  */
    353 /*
    354  * XXX KS - Because we have to mark nodes VDIROP in order to prevent
    355  * the cache from reclaiming them while a dirop is in progress, we must
    356  * also manage the number of nodes so marked (otherwise we can run out).
    357  * We do this by setting lfs_dirvcount to the number of marked vnodes; it
    358  * is decremented during segment write, when VDIROP is taken off.
    359  */
    360 #define	SET_DIROP(vp)		SET_DIROP2((vp), NULL)
    361 #define	SET_DIROP2(vp, vp2)	lfs_set_dirop((vp), (vp2))
    362 static int lfs_set_dirop(struct vnode *, struct vnode *);
    363 extern int lfs_dirvcount;
    364 
    365 #define	NRESERVE(fs)	(btofsb(fs, (NIADDR + 3 + (2 * NIADDR + 3)) << fs->lfs_bshift))
    366 
    367 static int
    368 lfs_set_dirop(struct vnode *vp, struct vnode *vp2)
    369 {
    370 	struct lfs *fs;
    371 	int error;
    372 
    373 	KASSERT(VOP_ISLOCKED(vp));
    374 	KASSERT(vp2 == NULL || VOP_ISLOCKED(vp2));
    375 
    376 	fs = VTOI(vp)->i_lfs;
    377 	/*
    378 	 * We might need one directory block plus supporting indirect blocks,
    379 	 * plus an inode block and ifile page for the new vnode.
    380 	 */
    381 	if ((error = lfs_reserve(fs, vp, vp2, NRESERVE(fs))) != 0)
    382 		return (error);
    383 
    384 	if (fs->lfs_dirops == 0)
    385 		lfs_check(vp, LFS_UNUSED_LBN, 0);
    386 	while (fs->lfs_writer || lfs_dirvcount > LFS_MAXDIROP) {
    387 		if (fs->lfs_writer)
    388 			tsleep(&fs->lfs_dirops, PRIBIO + 1, "lfs_sdirop", 0);
    389 		if (lfs_dirvcount > LFS_MAXDIROP && fs->lfs_dirops == 0) {
    390                 	++fs->lfs_writer;
    391                 	lfs_flush(fs, 0);
    392                 	if (--fs->lfs_writer == 0)
    393                         	wakeup(&fs->lfs_dirops);
    394 		}
    395 
    396 		if (lfs_dirvcount > LFS_MAXDIROP) {
    397 #ifdef DEBUG_LFS
    398 			printf("lfs_set_dirop: sleeping with dirops=%d, "
    399 			       "dirvcount=%d\n", fs->lfs_dirops,
    400 			       lfs_dirvcount);
    401 #endif
    402 			if ((error = tsleep(&lfs_dirvcount, PCATCH|PUSER,
    403 					   "lfs_maxdirop", 0)) != 0) {
    404 				goto unreserve;
    405 			}
    406 		}
    407 	}
    408 	++fs->lfs_dirops;
    409 	fs->lfs_doifile = 1;
    410 
    411 	/* Hold a reference so SET_ENDOP will be happy */
    412 	vref(vp);
    413 	if (vp2)
    414 		vref(vp2);
    415 
    416 	return 0;
    417 
    418 unreserve:
    419 	lfs_reserve(fs, vp, vp2, -NRESERVE(fs));
    420 	return error;
    421 }
    422 
    423 #define	SET_ENDOP(fs, vp, str)	SET_ENDOP2((fs), (vp), NULL, (str))
    424 #define	SET_ENDOP2(fs, vp, vp2, str) {					\
    425 	--(fs)->lfs_dirops;						\
    426 	if (!(fs)->lfs_dirops) {					\
    427 		if ((fs)->lfs_nadirop) {				\
    428 			panic("SET_ENDOP: %s: no dirops but nadirop=%d", \
    429 			      (str), (fs)->lfs_nadirop);		\
    430 		}							\
    431 		wakeup(&(fs)->lfs_writer);				\
    432 		lfs_check((vp),LFS_UNUSED_LBN,0);			\
    433 	}								\
    434 	lfs_reserve((fs), vp, vp2, -NRESERVE(fs)); /* XXX */		\
    435 	vrele(vp);							\
    436 	if (vp2)							\
    437 		vrele(vp2);						\
    438 }
    439 
    440 #define	MARK_VNODE(dvp)  do {                                           \
    441         if (!((dvp)->v_flag & VDIROP)) {				\
    442                 (void)lfs_vref(dvp);					\
    443 		++lfs_dirvcount;					\
    444 	}								\
    445         (dvp)->v_flag |= VDIROP;					\
    446 	if (!(VTOI(dvp)->i_flag & IN_ADIROP)) {				\
    447 		++VTOI(dvp)->i_lfs->lfs_nadirop;			\
    448 	}								\
    449 	VTOI(dvp)->i_flag |= IN_ADIROP;					\
    450 } while (0)
    451 
    452 #define UNMARK_VNODE(vp) lfs_unmark_vnode(vp)
    453 
    454 void lfs_unmark_vnode(struct vnode *vp)
    455 {
    456 	struct inode *ip;
    457 
    458 	ip = VTOI(vp);
    459 
    460 	if (ip->i_flag & IN_ADIROP)
    461 		--ip->i_lfs->lfs_nadirop;
    462 	ip->i_flag &= ~IN_ADIROP;
    463 }
    464 
    465 int
    466 lfs_symlink(void *v)
    467 {
    468 	struct vop_symlink_args /* {
    469 		struct vnode *a_dvp;
    470 		struct vnode **a_vpp;
    471 		struct componentname *a_cnp;
    472 		struct vattr *a_vap;
    473 		char *a_target;
    474 	} */ *ap = v;
    475 	int error;
    476 
    477 	if ((error = SET_DIROP(ap->a_dvp)) != 0) {
    478 		vput(ap->a_dvp);
    479 		return error;
    480 	}
    481 	MARK_VNODE(ap->a_dvp);
    482 	error = ufs_symlink(ap);
    483 	UNMARK_VNODE(ap->a_dvp);
    484 	if (*(ap->a_vpp))
    485 		UNMARK_VNODE(*(ap->a_vpp));
    486 	SET_ENDOP(VTOI(ap->a_dvp)->i_lfs,ap->a_dvp,"symlink");
    487 	return (error);
    488 }
    489 
    490 int
    491 lfs_mknod(void *v)
    492 {
    493 	struct vop_mknod_args	/* {
    494 		struct vnode *a_dvp;
    495 		struct vnode **a_vpp;
    496 		struct componentname *a_cnp;
    497 		struct vattr *a_vap;
    498 		} */ *ap = v;
    499         struct vattr *vap = ap->a_vap;
    500         struct vnode **vpp = ap->a_vpp;
    501         struct inode *ip;
    502         int error;
    503 	struct mount	*mp;
    504 	ino_t		ino;
    505 
    506 	if ((error = SET_DIROP(ap->a_dvp)) != 0) {
    507 		vput(ap->a_dvp);
    508 		return error;
    509 	}
    510 	MARK_VNODE(ap->a_dvp);
    511 	error = ufs_makeinode(MAKEIMODE(vap->va_type, vap->va_mode),
    512             ap->a_dvp, vpp, ap->a_cnp);
    513 	UNMARK_VNODE(ap->a_dvp);
    514         if (*(ap->a_vpp))
    515                 UNMARK_VNODE(*(ap->a_vpp));
    516 
    517 	/* Either way we're done with the dirop at this point */
    518 	SET_ENDOP(VTOI(ap->a_dvp)->i_lfs,ap->a_dvp,"mknod");
    519 
    520         if (error)
    521 		return (error);
    522 
    523         ip = VTOI(*vpp);
    524 	mp  = (*vpp)->v_mount;
    525 	ino = ip->i_number;
    526         ip->i_flag |= IN_ACCESS | IN_CHANGE | IN_UPDATE;
    527         if (vap->va_rdev != VNOVAL) {
    528                 /*
    529                  * Want to be able to use this to make badblock
    530                  * inodes, so don't truncate the dev number.
    531                  */
    532 #if 0
    533                 ip->i_ffs_rdev = ufs_rw32(vap->va_rdev,
    534                     UFS_MPNEEDSWAP((*vpp)->v_mount));
    535 #else
    536                 ip->i_ffs_rdev = vap->va_rdev;
    537 #endif
    538         }
    539 	/*
    540 	 * Call fsync to write the vnode so that we don't have to deal with
    541 	 * flushing it when it's marked VDIROP|VXLOCK.
    542 	 *
    543 	 * XXX KS - If we can't flush we also can't call vgone(), so must
    544 	 * return.  But, that leaves this vnode in limbo, also not good.
    545 	 * Can this ever happen (barring hardware failure)?
    546 	 */
    547 	if ((error = VOP_FSYNC(*vpp, NOCRED, FSYNC_WAIT, 0, 0,
    548 	    curproc)) != 0) {
    549 		printf("Couldn't fsync in mknod (ino %d)---what do I do?\n",
    550 		       VTOI(*vpp)->i_number);
    551 		return (error);
    552 	}
    553         /*
    554          * Remove vnode so that it will be reloaded by VFS_VGET and
    555          * checked to see if it is an alias of an existing entry in
    556          * the inode cache.
    557          */
    558 	/* Used to be vput, but that causes us to call VOP_INACTIVE twice. */
    559 	VOP_UNLOCK(*vpp, 0);
    560 	lfs_vunref(*vpp);
    561         (*vpp)->v_type = VNON;
    562         vgone(*vpp);
    563 	error = VFS_VGET(mp, ino, vpp);
    564 	if (error != 0) {
    565 		*vpp = NULL;
    566 		return (error);
    567 	}
    568         return (0);
    569 }
    570 
    571 int
    572 lfs_create(void *v)
    573 {
    574 	struct vop_create_args	/* {
    575 		struct vnode *a_dvp;
    576 		struct vnode **a_vpp;
    577 		struct componentname *a_cnp;
    578 		struct vattr *a_vap;
    579 	} */ *ap = v;
    580 	int error;
    581 
    582 	if ((error = SET_DIROP(ap->a_dvp)) != 0) {
    583 		vput(ap->a_dvp);
    584 		return error;
    585 	}
    586 	MARK_VNODE(ap->a_dvp);
    587 	error = ufs_create(ap);
    588 	UNMARK_VNODE(ap->a_dvp);
    589         if (*(ap->a_vpp))
    590                 UNMARK_VNODE(*(ap->a_vpp));
    591 	SET_ENDOP(VTOI(ap->a_dvp)->i_lfs,ap->a_dvp,"create");
    592 	return (error);
    593 }
    594 
    595 int
    596 lfs_mkdir(void *v)
    597 {
    598 	struct vop_mkdir_args	/* {
    599 		struct vnode *a_dvp;
    600 		struct vnode **a_vpp;
    601 		struct componentname *a_cnp;
    602 		struct vattr *a_vap;
    603 	} */ *ap = v;
    604 	int error;
    605 
    606 	if ((error = SET_DIROP(ap->a_dvp)) != 0) {
    607 		vput(ap->a_dvp);
    608 		return error;
    609 	}
    610 	MARK_VNODE(ap->a_dvp);
    611 	error = ufs_mkdir(ap);
    612 	UNMARK_VNODE(ap->a_dvp);
    613         if (*(ap->a_vpp))
    614                 UNMARK_VNODE(*(ap->a_vpp));
    615 	SET_ENDOP(VTOI(ap->a_dvp)->i_lfs,ap->a_dvp,"mkdir");
    616 	return (error);
    617 }
    618 
    619 int
    620 lfs_remove(void *v)
    621 {
    622 	struct vop_remove_args	/* {
    623 		struct vnode *a_dvp;
    624 		struct vnode *a_vp;
    625 		struct componentname *a_cnp;
    626 	} */ *ap = v;
    627 	struct vnode *dvp, *vp;
    628 	int error;
    629 
    630 	dvp = ap->a_dvp;
    631 	vp = ap->a_vp;
    632 	if ((error = SET_DIROP2(dvp, vp)) != 0) {
    633 		if (dvp == vp)
    634 			vrele(vp);
    635 		else
    636 			vput(vp);
    637 		vput(dvp);
    638 		return error;
    639 	}
    640 	MARK_VNODE(dvp);
    641 	MARK_VNODE(vp);
    642 	error = ufs_remove(ap);
    643 	UNMARK_VNODE(dvp);
    644 	UNMARK_VNODE(vp);
    645 
    646 	SET_ENDOP2(VTOI(dvp)->i_lfs, dvp, vp, "remove");
    647 	return (error);
    648 }
    649 
    650 int
    651 lfs_rmdir(void *v)
    652 {
    653 	struct vop_rmdir_args	/* {
    654 		struct vnodeop_desc *a_desc;
    655 		struct vnode *a_dvp;
    656 		struct vnode *a_vp;
    657 		struct componentname *a_cnp;
    658 	} */ *ap = v;
    659 	int error;
    660 
    661 	if ((error = SET_DIROP2(ap->a_dvp, ap->a_vp)) != 0) {
    662 		vrele(ap->a_dvp);
    663 		if (ap->a_vp != ap->a_dvp)
    664 			VOP_UNLOCK(ap->a_dvp, 0);
    665 		vput(ap->a_vp);
    666 		return error;
    667 	}
    668 	MARK_VNODE(ap->a_dvp);
    669 	MARK_VNODE(ap->a_vp);
    670 	error = ufs_rmdir(ap);
    671 	UNMARK_VNODE(ap->a_dvp);
    672 	UNMARK_VNODE(ap->a_vp);
    673 
    674 	SET_ENDOP2(VTOI(ap->a_dvp)->i_lfs, ap->a_dvp, ap->a_vp, "rmdir");
    675 	return (error);
    676 }
    677 
    678 int
    679 lfs_link(void *v)
    680 {
    681 	struct vop_link_args	/* {
    682 		struct vnode *a_dvp;
    683 		struct vnode *a_vp;
    684 		struct componentname *a_cnp;
    685 	} */ *ap = v;
    686 	int error;
    687 
    688 	if ((error = SET_DIROP(ap->a_dvp)) != 0) {
    689 		vput(ap->a_dvp);
    690 		return error;
    691 	}
    692 	MARK_VNODE(ap->a_dvp);
    693 	error = ufs_link(ap);
    694 	UNMARK_VNODE(ap->a_dvp);
    695 	SET_ENDOP(VTOI(ap->a_dvp)->i_lfs,ap->a_dvp,"link");
    696 	return (error);
    697 }
    698 
    699 int
    700 lfs_rename(void *v)
    701 {
    702 	struct vop_rename_args	/* {
    703 		struct vnode *a_fdvp;
    704 		struct vnode *a_fvp;
    705 		struct componentname *a_fcnp;
    706 		struct vnode *a_tdvp;
    707 		struct vnode *a_tvp;
    708 		struct componentname *a_tcnp;
    709 	} */ *ap = v;
    710 	struct vnode *tvp, *fvp, *tdvp, *fdvp;
    711 	struct componentname *tcnp, *fcnp;
    712 	int error;
    713 	struct lfs *fs;
    714 
    715 	fs = VTOI(ap->a_fdvp)->i_lfs;
    716 	tvp = ap->a_tvp;
    717 	tdvp = ap->a_tdvp;
    718 	tcnp = ap->a_tcnp;
    719 	fvp = ap->a_fvp;
    720 	fdvp = ap->a_fdvp;
    721 	fcnp = ap->a_fcnp;
    722 
    723 	/*
    724 	 * Check for cross-device rename.
    725 	 * If it is, we don't want to set dirops, just error out.
    726 	 * (In particular note that MARK_VNODE(tdvp) will DTWT on
    727 	 * a cross-device rename.)
    728 	 *
    729 	 * Copied from ufs_rename.
    730 	 */
    731 	if ((fvp->v_mount != tdvp->v_mount) ||
    732 	    (tvp && (fvp->v_mount != tvp->v_mount))) {
    733 		error = EXDEV;
    734 		goto errout;
    735 	}
    736 
    737 	/*
    738 	 * Check to make sure we're not renaming a vnode onto itself
    739 	 * (deleting a hard link by renaming one name onto another);
    740 	 * if we are we can't recursively call VOP_REMOVE since that
    741 	 * would leave us with an unaccounted-for number of live dirops.
    742 	 *
    743 	 * Inline the relevant section of ufs_rename here, *before*
    744 	 * calling SET_DIROP2.
    745 	 */
    746 	if (tvp && ((VTOI(tvp)->i_ffs_flags & (IMMUTABLE | APPEND)) ||
    747 	    (VTOI(tdvp)->i_ffs_flags & APPEND))) {
    748 		error = EPERM;
    749 		goto errout;
    750 	}
    751         if (fvp == tvp) {
    752                 if (fvp->v_type == VDIR) {
    753                         error = EINVAL;
    754                         goto errout;
    755                 }
    756 
    757                 /* Release destination completely. */
    758                 VOP_ABORTOP(tdvp, tcnp);
    759                 vput(tdvp);
    760                 vput(tvp);
    761 
    762                 /* Delete source. */
    763                 vrele(fvp);
    764                 fcnp->cn_flags &= ~(MODMASK | SAVESTART);
    765                 fcnp->cn_flags |= LOCKPARENT | LOCKLEAF;
    766                 fcnp->cn_nameiop = DELETE;
    767                 if ((error = relookup(fdvp, &fvp, fcnp))){
    768                         /* relookup blew away fdvp */
    769                         return (error);
    770                 }
    771                 return (VOP_REMOVE(fdvp, fvp, fcnp));
    772         }
    773 
    774 	if ((error = SET_DIROP2(tdvp, tvp)) != 0)
    775 		goto errout;
    776 	MARK_VNODE(fdvp);
    777 	MARK_VNODE(tdvp);
    778 	MARK_VNODE(fvp);
    779 	if (tvp) {
    780 		MARK_VNODE(tvp);
    781 	}
    782 
    783 	error = ufs_rename(ap);
    784 	UNMARK_VNODE(fdvp);
    785 	UNMARK_VNODE(tdvp);
    786 	UNMARK_VNODE(fvp);
    787 	if (tvp) {
    788 		UNMARK_VNODE(tvp);
    789 	}
    790 	SET_ENDOP2(fs, tdvp, tvp, "rename");
    791 	return (error);
    792 
    793     errout:
    794 	VOP_ABORTOP(tdvp, ap->a_tcnp); /* XXX, why not in NFS? */
    795 	if (tdvp == tvp)
    796 		vrele(tdvp);
    797 	else
    798 		vput(tdvp);
    799 	if (tvp)
    800 		vput(tvp);
    801 	VOP_ABORTOP(fdvp, ap->a_fcnp); /* XXX, why not in NFS? */
    802 	vrele(fdvp);
    803 	vrele(fvp);
    804 	return (error);
    805 }
    806 
    807 /* XXX hack to avoid calling ITIMES in getattr */
    808 int
    809 lfs_getattr(void *v)
    810 {
    811 	struct vop_getattr_args /* {
    812 		struct vnode *a_vp;
    813 		struct vattr *a_vap;
    814 		struct ucred *a_cred;
    815 		struct proc *a_p;
    816 	} */ *ap = v;
    817 	struct vnode *vp = ap->a_vp;
    818 	struct inode *ip = VTOI(vp);
    819 	struct vattr *vap = ap->a_vap;
    820 	struct lfs *fs = ip->i_lfs;
    821 	/*
    822 	 * Copy from inode table
    823 	 */
    824 	vap->va_fsid = ip->i_dev;
    825 	vap->va_fileid = ip->i_number;
    826 	vap->va_mode = ip->i_ffs_mode & ~IFMT;
    827 	vap->va_nlink = ip->i_ffs_nlink;
    828 	vap->va_uid = ip->i_ffs_uid;
    829 	vap->va_gid = ip->i_ffs_gid;
    830 	vap->va_rdev = (dev_t)ip->i_ffs_rdev;
    831 	vap->va_size = vp->v_size;
    832 	vap->va_atime.tv_sec = ip->i_ffs_atime;
    833 	vap->va_atime.tv_nsec = ip->i_ffs_atimensec;
    834 	vap->va_mtime.tv_sec = ip->i_ffs_mtime;
    835 	vap->va_mtime.tv_nsec = ip->i_ffs_mtimensec;
    836 	vap->va_ctime.tv_sec = ip->i_ffs_ctime;
    837 	vap->va_ctime.tv_nsec = ip->i_ffs_ctimensec;
    838 	vap->va_flags = ip->i_ffs_flags;
    839 	vap->va_gen = ip->i_ffs_gen;
    840 	/* this doesn't belong here */
    841 	if (vp->v_type == VBLK)
    842 		vap->va_blocksize = BLKDEV_IOSIZE;
    843 	else if (vp->v_type == VCHR)
    844 		vap->va_blocksize = MAXBSIZE;
    845 	else
    846 		vap->va_blocksize = vp->v_mount->mnt_stat.f_iosize;
    847 	vap->va_bytes = fsbtob(fs, (u_quad_t)ip->i_ffs_blocks);
    848 	vap->va_type = vp->v_type;
    849 	vap->va_filerev = ip->i_modrev;
    850 	return (0);
    851 }
    852 
    853 /*
    854  * Check to make sure the inode blocks won't choke the buffer
    855  * cache, then call ufs_setattr as usual.
    856  */
    857 int
    858 lfs_setattr(void *v)
    859 {
    860 	struct vop_getattr_args /* {
    861 		struct vnode *a_vp;
    862 		struct vattr *a_vap;
    863 		struct ucred *a_cred;
    864 		struct proc *a_p;
    865 	} */ *ap = v;
    866 	struct vnode *vp = ap->a_vp;
    867 
    868 	lfs_check(vp, LFS_UNUSED_LBN, 0);
    869 	return ufs_setattr(v);
    870 }
    871 
    872 /*
    873  * Close called
    874  *
    875  * XXX -- we were using ufs_close, but since it updates the
    876  * times on the inode, we might need to bump the uinodes
    877  * count.
    878  */
    879 /* ARGSUSED */
    880 int
    881 lfs_close(void *v)
    882 {
    883 	struct vop_close_args /* {
    884 		struct vnode *a_vp;
    885 		int  a_fflag;
    886 		struct ucred *a_cred;
    887 		struct proc *a_p;
    888 	} */ *ap = v;
    889 	struct vnode *vp = ap->a_vp;
    890 	struct inode *ip = VTOI(vp);
    891 	struct timespec ts;
    892 
    893 	if (vp->v_usecount > 1) {
    894 		TIMEVAL_TO_TIMESPEC(&time, &ts);
    895 		LFS_ITIMES(ip, &ts, &ts, &ts);
    896 	}
    897 	return (0);
    898 }
    899 
    900 /*
    901  * Close wrapper for special devices.
    902  *
    903  * Update the times on the inode then do device close.
    904  */
    905 int
    906 lfsspec_close(void *v)
    907 {
    908 	struct vop_close_args /* {
    909 		struct vnode	*a_vp;
    910 		int		a_fflag;
    911 		struct ucred	*a_cred;
    912 		struct proc	*a_p;
    913 	} */ *ap = v;
    914 	struct vnode	*vp;
    915 	struct inode	*ip;
    916 	struct timespec	ts;
    917 
    918 	vp = ap->a_vp;
    919 	ip = VTOI(vp);
    920 	if (vp->v_usecount > 1) {
    921 		TIMEVAL_TO_TIMESPEC(&time, &ts);
    922 		LFS_ITIMES(ip, &ts, &ts, &ts);
    923 	}
    924 	return (VOCALL (spec_vnodeop_p, VOFFSET(vop_close), ap));
    925 }
    926 
    927 /*
    928  * Close wrapper for fifo's.
    929  *
    930  * Update the times on the inode then do device close.
    931  */
    932 int
    933 lfsfifo_close(void *v)
    934 {
    935 	struct vop_close_args /* {
    936 		struct vnode	*a_vp;
    937 		int		a_fflag;
    938 		struct ucred	*a_cred;
    939 		struct proc	*a_p;
    940 	} */ *ap = v;
    941 	struct vnode	*vp;
    942 	struct inode	*ip;
    943 	struct timespec	ts;
    944 
    945 	vp = ap->a_vp;
    946 	ip = VTOI(vp);
    947 	if (ap->a_vp->v_usecount > 1) {
    948 		TIMEVAL_TO_TIMESPEC(&time, &ts);
    949 		LFS_ITIMES(ip, &ts, &ts, &ts);
    950 	}
    951 	return (VOCALL (fifo_vnodeop_p, VOFFSET(vop_close), ap));
    952 }
    953 
    954 /*
    955  * Reclaim an inode so that it can be used for other purposes.
    956  */
    957 int lfs_no_inactive = 0;
    958 
    959 int
    960 lfs_reclaim(void *v)
    961 {
    962 	struct vop_reclaim_args /* {
    963 		struct vnode *a_vp;
    964 		struct proc *a_p;
    965 	} */ *ap = v;
    966 	struct vnode *vp = ap->a_vp;
    967 	int error;
    968 
    969 	KASSERT(VTOI(vp)->i_ffs_nlink == VTOI(vp)->i_ffs_effnlink);
    970 
    971 	LFS_CLR_UINO(VTOI(vp), IN_ALLMOD);
    972 	if ((error = ufs_reclaim(vp, ap->a_p)))
    973 		return (error);
    974 	pool_put(&lfs_inode_pool, vp->v_data);
    975 	vp->v_data = NULL;
    976 	return (0);
    977 }
    978 
    979 int
    980 lfs_getpages(void *v)
    981 {
    982 	struct vop_getpages_args /* {
    983 		struct vnode *a_vp;
    984 		voff_t a_offset;
    985 		struct vm_page **a_m;
    986 		int *a_count;
    987 		int a_centeridx;
    988 		vm_prot_t a_access_type;
    989 		int a_advice;
    990 		int a_flags;
    991 	} */ *ap = v;
    992 
    993 	if ((ap->a_access_type & VM_PROT_WRITE) != 0) {
    994 		LFS_SET_UINO(VTOI(ap->a_vp), IN_MODIFIED);
    995 	}
    996 	return genfs_compat_getpages(v);
    997 }
    998 
    999 int
   1000 lfs_putpages(void *v)
   1001 {
   1002 	int error;
   1003 
   1004 	error = genfs_putpages(v);
   1005 	return error;
   1006 }
   1007