Home | History | Annotate | Line # | Download | only in lfs
lfs_vnops.c revision 1.75
      1 /*	$NetBSD: lfs_vnops.c,v 1.75 2002/12/29 07:05:55 yamt 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.75 2002/12/29 07:05:55 yamt 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, lfs_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=%d\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 	struct inode *ip = VTOI(ap->a_vp);
    341 
    342 	if (ip->i_flag & IN_ADIROP)
    343 		--ip->i_lfs->lfs_nadirop;
    344 	ip->i_flag &= ~IN_ADIROP;
    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 	lfs_vref(vp);
    413 	if (vp2 != NULL)
    414 		lfs_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 	lfs_vunref(vp);							\
    436 	if (vp2 != NULL)						\
    437 		lfs_vunref(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, curproc)) != 0) {
    548 		printf("Couldn't fsync in mknod (ino %d)---what do I do?\n",
    549 		       VTOI(*vpp)->i_number);
    550 		return (error);
    551 	}
    552         /*
    553          * Remove vnode so that it will be reloaded by VFS_VGET and
    554          * checked to see if it is an alias of an existing entry in
    555          * the inode cache.
    556          */
    557 	/* Used to be vput, but that causes us to call VOP_INACTIVE twice. */
    558 	VOP_UNLOCK(*vpp, 0);
    559 	lfs_vunref(*vpp);
    560         (*vpp)->v_type = VNON;
    561         vgone(*vpp);
    562 	error = VFS_VGET(mp, ino, vpp);
    563 	if (error != 0) {
    564 		*vpp = NULL;
    565 		return (error);
    566 	}
    567         return (0);
    568 }
    569 
    570 int
    571 lfs_create(void *v)
    572 {
    573 	struct vop_create_args	/* {
    574 		struct vnode *a_dvp;
    575 		struct vnode **a_vpp;
    576 		struct componentname *a_cnp;
    577 		struct vattr *a_vap;
    578 	} */ *ap = v;
    579 	int error;
    580 
    581 	if ((error = SET_DIROP(ap->a_dvp)) != 0) {
    582 		vput(ap->a_dvp);
    583 		return error;
    584 	}
    585 	MARK_VNODE(ap->a_dvp);
    586 	error = ufs_create(ap);
    587 	UNMARK_VNODE(ap->a_dvp);
    588         if (*(ap->a_vpp))
    589                 UNMARK_VNODE(*(ap->a_vpp));
    590 	SET_ENDOP(VTOI(ap->a_dvp)->i_lfs,ap->a_dvp,"create");
    591 	return (error);
    592 }
    593 
    594 int
    595 lfs_whiteout(void *v)
    596 {
    597 	struct vop_whiteout_args /* {
    598 		struct vnode *a_dvp;
    599 		struct componentname *a_cnp;
    600 		int a_flags;
    601 	} */ *ap = v;
    602 	int error;
    603 
    604 	if ((error = SET_DIROP(ap->a_dvp)) != 0)
    605 		/* XXX no unlock here? */
    606 		return error;
    607 	MARK_VNODE(ap->a_dvp);
    608 	error = ufs_whiteout(ap);
    609 	UNMARK_VNODE(ap->a_dvp);
    610 	SET_ENDOP(VTOI(ap->a_dvp)->i_lfs,ap->a_dvp,"whiteout");
    611 	return (error);
    612 }
    613 
    614 int
    615 lfs_mkdir(void *v)
    616 {
    617 	struct vop_mkdir_args	/* {
    618 		struct vnode *a_dvp;
    619 		struct vnode **a_vpp;
    620 		struct componentname *a_cnp;
    621 		struct vattr *a_vap;
    622 	} */ *ap = v;
    623 	int error;
    624 
    625 	if ((error = SET_DIROP(ap->a_dvp)) != 0) {
    626 		vput(ap->a_dvp);
    627 		return error;
    628 	}
    629 	MARK_VNODE(ap->a_dvp);
    630 	error = ufs_mkdir(ap);
    631 	UNMARK_VNODE(ap->a_dvp);
    632         if (*(ap->a_vpp))
    633                 UNMARK_VNODE(*(ap->a_vpp));
    634 	SET_ENDOP(VTOI(ap->a_dvp)->i_lfs,ap->a_dvp,"mkdir");
    635 	return (error);
    636 }
    637 
    638 int
    639 lfs_remove(void *v)
    640 {
    641 	struct vop_remove_args	/* {
    642 		struct vnode *a_dvp;
    643 		struct vnode *a_vp;
    644 		struct componentname *a_cnp;
    645 	} */ *ap = v;
    646 	struct vnode *dvp, *vp;
    647 	int error;
    648 
    649 	dvp = ap->a_dvp;
    650 	vp = ap->a_vp;
    651 	if ((error = SET_DIROP2(dvp, vp)) != 0) {
    652 		if (dvp == vp)
    653 			vrele(vp);
    654 		else
    655 			vput(vp);
    656 		vput(dvp);
    657 		return error;
    658 	}
    659 	MARK_VNODE(dvp);
    660 	MARK_VNODE(vp);
    661 	error = ufs_remove(ap);
    662 	UNMARK_VNODE(dvp);
    663 	UNMARK_VNODE(vp);
    664 
    665 	/*
    666 	 * If ufs_remove failed, vp doesn't need to be VDIROP any more.
    667 	 * If it succeeded, we can go ahead and wipe out vp, since
    668 	 * its loss won't appear on disk until checkpoint, and by then
    669 	 * dvp will have been written, completing the dirop.
    670 	 */
    671 	--lfs_dirvcount;
    672 	vp->v_flag &= ~VDIROP;
    673 	wakeup(&lfs_dirvcount);
    674 	vrele(vp);
    675 
    676 	SET_ENDOP2(VTOI(dvp)->i_lfs, dvp, vp, "remove");
    677 	return (error);
    678 }
    679 
    680 int
    681 lfs_rmdir(void *v)
    682 {
    683 	struct vop_rmdir_args	/* {
    684 		struct vnodeop_desc *a_desc;
    685 		struct vnode *a_dvp;
    686 		struct vnode *a_vp;
    687 		struct componentname *a_cnp;
    688 	} */ *ap = v;
    689 	int error;
    690 
    691 	if ((error = SET_DIROP2(ap->a_dvp, ap->a_vp)) != 0) {
    692 		vrele(ap->a_dvp);
    693 		if (ap->a_vp != ap->a_dvp)
    694 			VOP_UNLOCK(ap->a_dvp, 0);
    695 		vput(ap->a_vp);
    696 		return error;
    697 	}
    698 	MARK_VNODE(ap->a_dvp);
    699 	MARK_VNODE(ap->a_vp);
    700 	error = ufs_rmdir(ap);
    701 	UNMARK_VNODE(ap->a_dvp);
    702 	UNMARK_VNODE(ap->a_vp);
    703 
    704 	/*
    705 	 * If ufs_rmdir failed, vp doesn't need to be VDIROP any more.
    706 	 * If it succeeded, we can go ahead and wipe out vp, since
    707 	 * its loss won't appear on disk until checkpoint, and by then
    708 	 * dvp will have been written, completing the dirop.
    709 	 */
    710 	--lfs_dirvcount;
    711 	ap->a_vp->v_flag &= ~VDIROP;
    712 	wakeup(&lfs_dirvcount);
    713 	vrele(ap->a_vp);
    714 
    715 	SET_ENDOP2(VTOI(ap->a_dvp)->i_lfs, ap->a_dvp, ap->a_vp, "rmdir");
    716 	return (error);
    717 }
    718 
    719 int
    720 lfs_link(void *v)
    721 {
    722 	struct vop_link_args	/* {
    723 		struct vnode *a_dvp;
    724 		struct vnode *a_vp;
    725 		struct componentname *a_cnp;
    726 	} */ *ap = v;
    727 	int error;
    728 
    729 	if ((error = SET_DIROP(ap->a_dvp)) != 0) {
    730 		vput(ap->a_dvp);
    731 		return error;
    732 	}
    733 	MARK_VNODE(ap->a_dvp);
    734 	error = ufs_link(ap);
    735 	UNMARK_VNODE(ap->a_dvp);
    736 	SET_ENDOP(VTOI(ap->a_dvp)->i_lfs,ap->a_dvp,"link");
    737 	return (error);
    738 }
    739 
    740 int
    741 lfs_rename(void *v)
    742 {
    743 	struct vop_rename_args	/* {
    744 		struct vnode *a_fdvp;
    745 		struct vnode *a_fvp;
    746 		struct componentname *a_fcnp;
    747 		struct vnode *a_tdvp;
    748 		struct vnode *a_tvp;
    749 		struct componentname *a_tcnp;
    750 	} */ *ap = v;
    751 	struct vnode *tvp, *fvp, *tdvp, *fdvp;
    752 	int error;
    753 	struct lfs *fs;
    754 
    755 	fs = VTOI(ap->a_fdvp)->i_lfs;
    756 	tvp = ap->a_tvp;
    757 	tdvp = ap->a_tdvp;
    758 	fvp = ap->a_fvp;
    759 	fdvp = ap->a_fdvp;
    760 
    761 	/*
    762 	 * Check for cross-device rename.
    763 	 * If it is, we don't want to set dirops, just error out.
    764 	 * (In particular note that MARK_VNODE(tdvp) will DTWT on
    765 	 * a cross-device rename.)
    766 	 *
    767 	 * Copied from ufs_rename.
    768 	 */
    769 	if ((fvp->v_mount != tdvp->v_mount) ||
    770 	    (tvp && (fvp->v_mount != tvp->v_mount))) {
    771 		error = EXDEV;
    772 		goto errout;
    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 	error = ufs_rename(ap);
    783 	UNMARK_VNODE(fdvp);
    784 	UNMARK_VNODE(tdvp);
    785 	UNMARK_VNODE(fvp);
    786 	if (tvp) {
    787 		UNMARK_VNODE(tvp);
    788 	}
    789 	SET_ENDOP2(fs, tdvp, tvp, "rename");
    790 	return (error);
    791 
    792     errout:
    793 	VOP_ABORTOP(tdvp, ap->a_tcnp); /* XXX, why not in NFS? */
    794 	if (tdvp == tvp)
    795 		vrele(tdvp);
    796 	else
    797 		vput(tdvp);
    798 	if (tvp)
    799 		vput(tvp);
    800 	VOP_ABORTOP(fdvp, ap->a_fcnp); /* XXX, why not in NFS? */
    801 	vrele(fdvp);
    802 	vrele(fvp);
    803 	return (error);
    804 }
    805 
    806 /* XXX hack to avoid calling ITIMES in getattr */
    807 int
    808 lfs_getattr(void *v)
    809 {
    810 	struct vop_getattr_args /* {
    811 		struct vnode *a_vp;
    812 		struct vattr *a_vap;
    813 		struct ucred *a_cred;
    814 		struct proc *a_p;
    815 	} */ *ap = v;
    816 	struct vnode *vp = ap->a_vp;
    817 	struct inode *ip = VTOI(vp);
    818 	struct vattr *vap = ap->a_vap;
    819 	struct lfs *fs = ip->i_lfs;
    820 	/*
    821 	 * Copy from inode table
    822 	 */
    823 	vap->va_fsid = ip->i_dev;
    824 	vap->va_fileid = ip->i_number;
    825 	vap->va_mode = ip->i_ffs_mode & ~IFMT;
    826 	vap->va_nlink = ip->i_ffs_nlink;
    827 	vap->va_uid = ip->i_ffs_uid;
    828 	vap->va_gid = ip->i_ffs_gid;
    829 	vap->va_rdev = (dev_t)ip->i_ffs_rdev;
    830 	vap->va_size = vp->v_size;
    831 	vap->va_atime.tv_sec = ip->i_ffs_atime;
    832 	vap->va_atime.tv_nsec = ip->i_ffs_atimensec;
    833 	vap->va_mtime.tv_sec = ip->i_ffs_mtime;
    834 	vap->va_mtime.tv_nsec = ip->i_ffs_mtimensec;
    835 	vap->va_ctime.tv_sec = ip->i_ffs_ctime;
    836 	vap->va_ctime.tv_nsec = ip->i_ffs_ctimensec;
    837 	vap->va_flags = ip->i_ffs_flags;
    838 	vap->va_gen = ip->i_ffs_gen;
    839 	/* this doesn't belong here */
    840 	if (vp->v_type == VBLK)
    841 		vap->va_blocksize = BLKDEV_IOSIZE;
    842 	else if (vp->v_type == VCHR)
    843 		vap->va_blocksize = MAXBSIZE;
    844 	else
    845 		vap->va_blocksize = vp->v_mount->mnt_stat.f_iosize;
    846 	vap->va_bytes = fsbtob(fs, (u_quad_t)ip->i_ffs_blocks);
    847 	vap->va_type = vp->v_type;
    848 	vap->va_filerev = ip->i_modrev;
    849 	return (0);
    850 }
    851 
    852 /*
    853  * Check to make sure the inode blocks won't choke the buffer
    854  * cache, then call ufs_setattr as usual.
    855  */
    856 int
    857 lfs_setattr(void *v)
    858 {
    859 	struct vop_getattr_args /* {
    860 		struct vnode *a_vp;
    861 		struct vattr *a_vap;
    862 		struct ucred *a_cred;
    863 		struct proc *a_p;
    864 	} */ *ap = v;
    865 	struct vnode *vp = ap->a_vp;
    866 
    867 	lfs_check(vp, LFS_UNUSED_LBN, 0);
    868 	return ufs_setattr(v);
    869 }
    870 
    871 /*
    872  * Close called
    873  *
    874  * XXX -- we were using ufs_close, but since it updates the
    875  * times on the inode, we might need to bump the uinodes
    876  * count.
    877  */
    878 /* ARGSUSED */
    879 int
    880 lfs_close(void *v)
    881 {
    882 	struct vop_close_args /* {
    883 		struct vnode *a_vp;
    884 		int  a_fflag;
    885 		struct ucred *a_cred;
    886 		struct proc *a_p;
    887 	} */ *ap = v;
    888 	struct vnode *vp = ap->a_vp;
    889 	struct inode *ip = VTOI(vp);
    890 	struct timespec ts;
    891 
    892 	if (vp->v_usecount > 1) {
    893 		TIMEVAL_TO_TIMESPEC(&time, &ts);
    894 		LFS_ITIMES(ip, &ts, &ts, &ts);
    895 	}
    896 	return (0);
    897 }
    898 
    899 /*
    900  * Close wrapper for special devices.
    901  *
    902  * Update the times on the inode then do device close.
    903  */
    904 int
    905 lfsspec_close(void *v)
    906 {
    907 	struct vop_close_args /* {
    908 		struct vnode	*a_vp;
    909 		int		a_fflag;
    910 		struct ucred	*a_cred;
    911 		struct proc	*a_p;
    912 	} */ *ap = v;
    913 	struct vnode	*vp;
    914 	struct inode	*ip;
    915 	struct timespec	ts;
    916 
    917 	vp = ap->a_vp;
    918 	ip = VTOI(vp);
    919 	if (vp->v_usecount > 1) {
    920 		TIMEVAL_TO_TIMESPEC(&time, &ts);
    921 		LFS_ITIMES(ip, &ts, &ts, &ts);
    922 	}
    923 	return (VOCALL (spec_vnodeop_p, VOFFSET(vop_close), ap));
    924 }
    925 
    926 /*
    927  * Close wrapper for fifo's.
    928  *
    929  * Update the times on the inode then do device close.
    930  */
    931 int
    932 lfsfifo_close(void *v)
    933 {
    934 	struct vop_close_args /* {
    935 		struct vnode	*a_vp;
    936 		int		a_fflag;
    937 		struct ucred	*a_cred;
    938 		struct proc	*a_p;
    939 	} */ *ap = v;
    940 	struct vnode	*vp;
    941 	struct inode	*ip;
    942 	struct timespec	ts;
    943 
    944 	vp = ap->a_vp;
    945 	ip = VTOI(vp);
    946 	if (ap->a_vp->v_usecount > 1) {
    947 		TIMEVAL_TO_TIMESPEC(&time, &ts);
    948 		LFS_ITIMES(ip, &ts, &ts, &ts);
    949 	}
    950 	return (VOCALL (fifo_vnodeop_p, VOFFSET(vop_close), ap));
    951 }
    952 
    953 /*
    954  * Reclaim an inode so that it can be used for other purposes.
    955  */
    956 int lfs_no_inactive = 0;
    957 
    958 int
    959 lfs_reclaim(void *v)
    960 {
    961 	struct vop_reclaim_args /* {
    962 		struct vnode *a_vp;
    963 		struct proc *a_p;
    964 	} */ *ap = v;
    965 	struct vnode *vp = ap->a_vp;
    966 	int error;
    967 
    968 	LFS_CLR_UINO(VTOI(vp), IN_ALLMOD);
    969 	if ((error = ufs_reclaim(vp, ap->a_p)))
    970 		return (error);
    971 	pool_put(&lfs_inode_pool, vp->v_data);
    972 	vp->v_data = NULL;
    973 	return (0);
    974 }
    975 
    976 int
    977 lfs_getpages(void *v)
    978 {
    979 	struct vop_getpages_args /* {
    980 		struct vnode *a_vp;
    981 		voff_t a_offset;
    982 		struct vm_page **a_m;
    983 		int *a_count;
    984 		int a_centeridx;
    985 		vm_prot_t a_access_type;
    986 		int a_advice;
    987 		int a_flags;
    988 	} */ *ap = v;
    989 
    990 	if ((ap->a_access_type & VM_PROT_WRITE) != 0) {
    991 		LFS_SET_UINO(VTOI(ap->a_vp), IN_MODIFIED);
    992 	}
    993 	return genfs_compat_getpages(v);
    994 }
    995 
    996 int
    997 lfs_putpages(void *v)
    998 {
    999 	int error;
   1000 
   1001 	error = genfs_putpages(v);
   1002 	return error;
   1003 }
   1004