Home | History | Annotate | Line # | Download | only in lfs
ulfs_vnops.c revision 1.6
      1 /*	$NetBSD: ulfs_vnops.c,v 1.6 2013/06/06 00:51:25 dholland Exp $	*/
      2 /*  from NetBSD: ufs_vnops.c,v 1.212 2013/03/18 19:35:48 plunky Exp  */
      3 
      4 /*-
      5  * Copyright (c) 2008 The NetBSD Foundation, Inc.
      6  * All rights reserved.
      7  *
      8  * This code is derived from software contributed to The NetBSD Foundation
      9  * by Wasabi Systems, Inc.
     10  *
     11  * Redistribution and use in source and binary forms, with or without
     12  * modification, are permitted provided that the following conditions
     13  * are met:
     14  * 1. Redistributions of source code must retain the above copyright
     15  *    notice, this list of conditions and the following disclaimer.
     16  * 2. Redistributions in binary form must reproduce the above copyright
     17  *    notice, this list of conditions and the following disclaimer in the
     18  *    documentation and/or other materials provided with the distribution.
     19  *
     20  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     22  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     23  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     24  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     30  * POSSIBILITY OF SUCH DAMAGE.
     31  */
     32 
     33 /*
     34  * Copyright (c) 1982, 1986, 1989, 1993, 1995
     35  *	The Regents of the University of California.  All rights reserved.
     36  * (c) UNIX System Laboratories, Inc.
     37  * All or some portions of this file are derived from material licensed
     38  * to the University of California by American Telephone and Telegraph
     39  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
     40  * the permission of UNIX System Laboratories, Inc.
     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  *	@(#)ufs_vnops.c	8.28 (Berkeley) 7/31/95
     67  */
     68 
     69 #include <sys/cdefs.h>
     70 __KERNEL_RCSID(0, "$NetBSD: ulfs_vnops.c,v 1.6 2013/06/06 00:51:25 dholland Exp $");
     71 
     72 #if defined(_KERNEL_OPT)
     73 #include "opt_lfs.h"
     74 #include "opt_quota.h"
     75 #endif
     76 
     77 #include <sys/param.h>
     78 #include <sys/systm.h>
     79 #include <sys/namei.h>
     80 #include <sys/resourcevar.h>
     81 #include <sys/kernel.h>
     82 #include <sys/file.h>
     83 #include <sys/stat.h>
     84 #include <sys/buf.h>
     85 #include <sys/proc.h>
     86 #include <sys/mount.h>
     87 #include <sys/vnode.h>
     88 #include <sys/kmem.h>
     89 #include <sys/malloc.h>
     90 #include <sys/dirent.h>
     91 #include <sys/lockf.h>
     92 #include <sys/kauth.h>
     93 #include <sys/wapbl.h>
     94 #include <sys/fstrans.h>
     95 
     96 #include <miscfs/specfs/specdev.h>
     97 #include <miscfs/fifofs/fifo.h>
     98 #include <miscfs/genfs/genfs.h>
     99 
    100 #include <ufs/lfs/ulfs_inode.h>
    101 #include <ufs/lfs/ulfs_dir.h>
    102 #include <ufs/lfs/ulfsmount.h>
    103 #include <ufs/lfs/ulfs_bswap.h>
    104 #include <ufs/lfs/ulfs_extern.h>
    105 #include <ufs/lfs/ulfs_wapbl.h>
    106 #ifdef LFS_DIRHASH
    107 #include <ufs/lfs/ulfs_dirhash.h>
    108 #endif
    109 #include <ufs/lfs/lfs_extern.h>
    110 #include <ufs/lfs/lfs.h>
    111 
    112 #include <uvm/uvm.h>
    113 
    114 static int ulfs_chmod(struct vnode *, int, kauth_cred_t, struct lwp *);
    115 static int ulfs_chown(struct vnode *, uid_t, gid_t, kauth_cred_t,
    116     struct lwp *);
    117 
    118 /*
    119  * A virgin directory (no blushing please).
    120  */
    121 static const struct dirtemplate mastertemplate = {
    122 	0,	12,		DT_DIR,	1,	".",
    123 	0,	DIRBLKSIZ - 12,	DT_DIR,	2,	".."
    124 };
    125 
    126 /*
    127  * Create a regular file
    128  */
    129 int
    130 ulfs_create(void *v)
    131 {
    132 	struct vop_create_args /* {
    133 		struct vnode		*a_dvp;
    134 		struct vnode		**a_vpp;
    135 		struct componentname	*a_cnp;
    136 		struct vattr		*a_vap;
    137 	} */ *ap = v;
    138 	int	error;
    139 	struct vnode *dvp = ap->a_dvp;
    140 	struct ulfs_lookup_results *ulr;
    141 
    142 	/* XXX should handle this material another way */
    143 	ulr = &VTOI(dvp)->i_crap;
    144 	ULFS_CHECK_CRAPCOUNTER(VTOI(dvp));
    145 
    146 	/*
    147 	 * ULFS_WAPBL_BEGIN1(dvp->v_mount, dvp) performed by successful
    148 	 * ulfs_makeinode
    149 	 */
    150 	fstrans_start(dvp->v_mount, FSTRANS_SHARED);
    151 	error =
    152 	    ulfs_makeinode(MAKEIMODE(ap->a_vap->va_type, ap->a_vap->va_mode),
    153 			  dvp, ulr, ap->a_vpp, ap->a_cnp);
    154 	if (error) {
    155 		fstrans_done(dvp->v_mount);
    156 		return (error);
    157 	}
    158 	ULFS_WAPBL_END1(dvp->v_mount, dvp);
    159 	fstrans_done(dvp->v_mount);
    160 	VN_KNOTE(dvp, NOTE_WRITE);
    161 	return (0);
    162 }
    163 
    164 /*
    165  * Mknod vnode call
    166  */
    167 /* ARGSUSED */
    168 int
    169 ulfs_mknod(void *v)
    170 {
    171 	struct vop_mknod_args /* {
    172 		struct vnode		*a_dvp;
    173 		struct vnode		**a_vpp;
    174 		struct componentname	*a_cnp;
    175 		struct vattr		*a_vap;
    176 	} */ *ap = v;
    177 	struct vattr	*vap;
    178 	struct vnode	**vpp;
    179 	struct inode	*ip;
    180 	int		error;
    181 	struct mount	*mp;
    182 	ino_t		ino;
    183 	struct ulfs_lookup_results *ulr;
    184 
    185 	vap = ap->a_vap;
    186 	vpp = ap->a_vpp;
    187 
    188 	/* XXX should handle this material another way */
    189 	ulr = &VTOI(ap->a_dvp)->i_crap;
    190 	ULFS_CHECK_CRAPCOUNTER(VTOI(ap->a_dvp));
    191 
    192 	/*
    193 	 * ULFS_WAPBL_BEGIN1(dvp->v_mount, dvp) performed by successful
    194 	 * ulfs_makeinode
    195 	 */
    196 	fstrans_start(ap->a_dvp->v_mount, FSTRANS_SHARED);
    197 	if ((error =
    198 	    ulfs_makeinode(MAKEIMODE(vap->va_type, vap->va_mode),
    199 	    ap->a_dvp, ulr, vpp, ap->a_cnp)) != 0)
    200 		goto out;
    201 	VN_KNOTE(ap->a_dvp, NOTE_WRITE);
    202 	ip = VTOI(*vpp);
    203 	mp  = (*vpp)->v_mount;
    204 	ino = ip->i_number;
    205 	ip->i_flag |= IN_ACCESS | IN_CHANGE | IN_UPDATE;
    206 	if (vap->va_rdev != VNOVAL) {
    207 		struct ulfsmount *ump = ip->i_ump;
    208 		/*
    209 		 * Want to be able to use this to make badblock
    210 		 * inodes, so don't truncate the dev number.
    211 		 */
    212 		if (ump->um_fstype == ULFS1)
    213 			ip->i_ffs1_rdev = ulfs_rw32(vap->va_rdev,
    214 			    ULFS_MPNEEDSWAP(ump));
    215 		else
    216 			ip->i_ffs2_rdev = ulfs_rw64(vap->va_rdev,
    217 			    ULFS_MPNEEDSWAP(ump));
    218 	}
    219 	ULFS_WAPBL_UPDATE(*vpp, NULL, NULL, 0);
    220 	ULFS_WAPBL_END1(ap->a_dvp->v_mount, ap->a_dvp);
    221 	/*
    222 	 * Remove inode so that it will be reloaded by VFS_VGET and
    223 	 * checked to see if it is an alias of an existing entry in
    224 	 * the inode cache.
    225 	 */
    226 	(*vpp)->v_type = VNON;
    227 	VOP_UNLOCK(*vpp);
    228 	vgone(*vpp);
    229 	error = VFS_VGET(mp, ino, vpp);
    230 out:
    231 	fstrans_done(ap->a_dvp->v_mount);
    232 	if (error != 0) {
    233 		*vpp = NULL;
    234 		return (error);
    235 	}
    236 	return (0);
    237 }
    238 
    239 /*
    240  * Open called.
    241  *
    242  * Nothing to do.
    243  */
    244 /* ARGSUSED */
    245 int
    246 ulfs_open(void *v)
    247 {
    248 	struct vop_open_args /* {
    249 		struct vnode	*a_vp;
    250 		int		a_mode;
    251 		kauth_cred_t	a_cred;
    252 	} */ *ap = v;
    253 
    254 	/*
    255 	 * Files marked append-only must be opened for appending.
    256 	 */
    257 	if ((VTOI(ap->a_vp)->i_flags & APPEND) &&
    258 	    (ap->a_mode & (FWRITE | O_APPEND)) == FWRITE)
    259 		return (EPERM);
    260 	return (0);
    261 }
    262 
    263 /*
    264  * Close called.
    265  *
    266  * Update the times on the inode.
    267  */
    268 /* ARGSUSED */
    269 int
    270 ulfs_close(void *v)
    271 {
    272 	struct vop_close_args /* {
    273 		struct vnode	*a_vp;
    274 		int		a_fflag;
    275 		kauth_cred_t	a_cred;
    276 	} */ *ap = v;
    277 	struct vnode	*vp;
    278 	struct inode	*ip;
    279 
    280 	vp = ap->a_vp;
    281 	ip = VTOI(vp);
    282 	fstrans_start(vp->v_mount, FSTRANS_SHARED);
    283 	if (vp->v_usecount > 1)
    284 		ULFS_ITIMES(vp, NULL, NULL, NULL);
    285 	fstrans_done(vp->v_mount);
    286 	return (0);
    287 }
    288 
    289 static int
    290 ulfs_check_possible(struct vnode *vp, struct inode *ip, mode_t mode,
    291     kauth_cred_t cred)
    292 {
    293 #if defined(LFS_QUOTA) || defined(LFS_QUOTA2)
    294 	int error;
    295 #endif
    296 
    297 	/*
    298 	 * Disallow write attempts on read-only file systems;
    299 	 * unless the file is a socket, fifo, or a block or
    300 	 * character device resident on the file system.
    301 	 */
    302 	if (mode & VWRITE) {
    303 		switch (vp->v_type) {
    304 		case VDIR:
    305 		case VLNK:
    306 		case VREG:
    307 			if (vp->v_mount->mnt_flag & MNT_RDONLY)
    308 				return (EROFS);
    309 #if defined(LFS_QUOTA) || defined(LFS_QUOTA2)
    310 			fstrans_start(vp->v_mount, FSTRANS_SHARED);
    311 			error = lfs_chkdq(ip, 0, cred, 0);
    312 			fstrans_done(vp->v_mount);
    313 			if (error != 0)
    314 				return error;
    315 #endif
    316 			break;
    317 		case VBAD:
    318 		case VBLK:
    319 		case VCHR:
    320 		case VSOCK:
    321 		case VFIFO:
    322 		case VNON:
    323 		default:
    324 			break;
    325 		}
    326 	}
    327 
    328 	/* If it is a snapshot, nobody gets access to it. */
    329 	if ((ip->i_flags & SF_SNAPSHOT))
    330 		return (EPERM);
    331 	/* If immutable bit set, nobody gets to write it. */
    332 	if ((mode & VWRITE) && (ip->i_flags & IMMUTABLE))
    333 		return (EPERM);
    334 
    335 	return 0;
    336 }
    337 
    338 static int
    339 ulfs_check_permitted(struct vnode *vp, struct inode *ip, mode_t mode,
    340     kauth_cred_t cred)
    341 {
    342 
    343 	return kauth_authorize_vnode(cred, KAUTH_ACCESS_ACTION(mode, vp->v_type,
    344 	    ip->i_mode & ALLPERMS), vp, NULL, genfs_can_access(vp->v_type,
    345 	    ip->i_mode & ALLPERMS, ip->i_uid, ip->i_gid, mode, cred));
    346 }
    347 
    348 int
    349 ulfs_access(void *v)
    350 {
    351 	struct vop_access_args /* {
    352 		struct vnode	*a_vp;
    353 		int		a_mode;
    354 		kauth_cred_t	a_cred;
    355 	} */ *ap = v;
    356 	struct vnode	*vp;
    357 	struct inode	*ip;
    358 	mode_t		mode;
    359 	int		error;
    360 
    361 	vp = ap->a_vp;
    362 	ip = VTOI(vp);
    363 	mode = ap->a_mode;
    364 
    365 	error = ulfs_check_possible(vp, ip, mode, ap->a_cred);
    366 	if (error)
    367 		return error;
    368 
    369 	error = ulfs_check_permitted(vp, ip, mode, ap->a_cred);
    370 
    371 	return error;
    372 }
    373 
    374 /* ARGSUSED */
    375 int
    376 ulfs_getattr(void *v)
    377 {
    378 	struct vop_getattr_args /* {
    379 		struct vnode	*a_vp;
    380 		struct vattr	*a_vap;
    381 		kauth_cred_t	a_cred;
    382 	} */ *ap = v;
    383 	struct vnode	*vp;
    384 	struct inode	*ip;
    385 	struct vattr	*vap;
    386 
    387 	vp = ap->a_vp;
    388 	ip = VTOI(vp);
    389 	vap = ap->a_vap;
    390 	fstrans_start(vp->v_mount, FSTRANS_SHARED);
    391 	ULFS_ITIMES(vp, NULL, NULL, NULL);
    392 
    393 	/*
    394 	 * Copy from inode table
    395 	 */
    396 	vap->va_fsid = ip->i_dev;
    397 	vap->va_fileid = ip->i_number;
    398 	vap->va_mode = ip->i_mode & ALLPERMS;
    399 	vap->va_nlink = ip->i_nlink;
    400 	vap->va_uid = ip->i_uid;
    401 	vap->va_gid = ip->i_gid;
    402 	vap->va_size = vp->v_size;
    403 	if (ip->i_ump->um_fstype == ULFS1) {
    404 		vap->va_rdev = (dev_t)ulfs_rw32(ip->i_ffs1_rdev,
    405 		    ULFS_MPNEEDSWAP(ip->i_ump));
    406 		vap->va_atime.tv_sec = ip->i_ffs1_atime;
    407 		vap->va_atime.tv_nsec = ip->i_ffs1_atimensec;
    408 		vap->va_mtime.tv_sec = ip->i_ffs1_mtime;
    409 		vap->va_mtime.tv_nsec = ip->i_ffs1_mtimensec;
    410 		vap->va_ctime.tv_sec = ip->i_ffs1_ctime;
    411 		vap->va_ctime.tv_nsec = ip->i_ffs1_ctimensec;
    412 		vap->va_birthtime.tv_sec = 0;
    413 		vap->va_birthtime.tv_nsec = 0;
    414 		vap->va_bytes = dbtob((u_quad_t)ip->i_ffs1_blocks);
    415 	} else {
    416 		vap->va_rdev = (dev_t)ulfs_rw64(ip->i_ffs2_rdev,
    417 		    ULFS_MPNEEDSWAP(ip->i_ump));
    418 		vap->va_atime.tv_sec = ip->i_ffs2_atime;
    419 		vap->va_atime.tv_nsec = ip->i_ffs2_atimensec;
    420 		vap->va_mtime.tv_sec = ip->i_ffs2_mtime;
    421 		vap->va_mtime.tv_nsec = ip->i_ffs2_mtimensec;
    422 		vap->va_ctime.tv_sec = ip->i_ffs2_ctime;
    423 		vap->va_ctime.tv_nsec = ip->i_ffs2_ctimensec;
    424 		vap->va_birthtime.tv_sec = ip->i_ffs2_birthtime;
    425 		vap->va_birthtime.tv_nsec = ip->i_ffs2_birthnsec;
    426 		vap->va_bytes = dbtob(ip->i_ffs2_blocks);
    427 	}
    428 	vap->va_gen = ip->i_gen;
    429 	vap->va_flags = ip->i_flags;
    430 
    431 	/* this doesn't belong here */
    432 	if (vp->v_type == VBLK)
    433 		vap->va_blocksize = BLKDEV_IOSIZE;
    434 	else if (vp->v_type == VCHR)
    435 		vap->va_blocksize = MAXBSIZE;
    436 	else
    437 		vap->va_blocksize = vp->v_mount->mnt_stat.f_iosize;
    438 	vap->va_type = vp->v_type;
    439 	vap->va_filerev = ip->i_modrev;
    440 	fstrans_done(vp->v_mount);
    441 	return (0);
    442 }
    443 
    444 /*
    445  * Set attribute vnode op. called from several syscalls
    446  */
    447 int
    448 ulfs_setattr(void *v)
    449 {
    450 	struct vop_setattr_args /* {
    451 		struct vnode	*a_vp;
    452 		struct vattr	*a_vap;
    453 		kauth_cred_t	a_cred;
    454 	} */ *ap = v;
    455 	struct vattr	*vap;
    456 	struct vnode	*vp;
    457 	struct inode	*ip;
    458 	kauth_cred_t	cred;
    459 	struct lwp	*l;
    460 	int		error;
    461 	kauth_action_t	action;
    462 	bool		changing_sysflags;
    463 
    464 	vap = ap->a_vap;
    465 	vp = ap->a_vp;
    466 	ip = VTOI(vp);
    467 	cred = ap->a_cred;
    468 	l = curlwp;
    469 	action = KAUTH_VNODE_WRITE_FLAGS;
    470 	changing_sysflags = false;
    471 
    472 	/*
    473 	 * Check for unsettable attributes.
    474 	 */
    475 	if ((vap->va_type != VNON) || (vap->va_nlink != VNOVAL) ||
    476 	    (vap->va_fsid != VNOVAL) || (vap->va_fileid != VNOVAL) ||
    477 	    (vap->va_blocksize != VNOVAL) || (vap->va_rdev != VNOVAL) ||
    478 	    ((int)vap->va_bytes != VNOVAL) || (vap->va_gen != VNOVAL)) {
    479 		return (EINVAL);
    480 	}
    481 
    482 	fstrans_start(vp->v_mount, FSTRANS_SHARED);
    483 
    484 	if (vap->va_flags != VNOVAL) {
    485 		if (vp->v_mount->mnt_flag & MNT_RDONLY) {
    486 			error = EROFS;
    487 			goto out;
    488 		}
    489 
    490 		/* Snapshot flag cannot be set or cleared */
    491 		if ((vap->va_flags & (SF_SNAPSHOT | SF_SNAPINVAL)) !=
    492 		    (ip->i_flags & (SF_SNAPSHOT | SF_SNAPINVAL))) {
    493 			error = EPERM;
    494 			goto out;
    495 		}
    496 
    497 		if (ip->i_flags & (SF_IMMUTABLE | SF_APPEND)) {
    498 			action |= KAUTH_VNODE_HAS_SYSFLAGS;
    499 		}
    500 
    501 		if ((vap->va_flags & UF_SETTABLE) != vap->va_flags) {
    502 			action |= KAUTH_VNODE_WRITE_SYSFLAGS;
    503 			changing_sysflags = true;
    504 		}
    505 
    506 		error = kauth_authorize_vnode(cred, action, vp, NULL,
    507 		    genfs_can_chflags(cred, vp->v_type, ip->i_uid,
    508 		    changing_sysflags));
    509 		if (error)
    510 			goto out;
    511 
    512 		if (changing_sysflags) {
    513 			error = ULFS_WAPBL_BEGIN(vp->v_mount);
    514 			if (error)
    515 				goto out;
    516 			ip->i_flags = vap->va_flags;
    517 			DIP_ASSIGN(ip, flags, ip->i_flags);
    518 		} else {
    519 			error = ULFS_WAPBL_BEGIN(vp->v_mount);
    520 			if (error)
    521 				goto out;
    522 			ip->i_flags &= SF_SETTABLE;
    523 			ip->i_flags |= (vap->va_flags & UF_SETTABLE);
    524 			DIP_ASSIGN(ip, flags, ip->i_flags);
    525 		}
    526 		ip->i_flag |= IN_CHANGE;
    527 		ULFS_WAPBL_UPDATE(vp, NULL, NULL, 0);
    528 		ULFS_WAPBL_END(vp->v_mount);
    529 		if (vap->va_flags & (IMMUTABLE | APPEND)) {
    530 			error = 0;
    531 			goto out;
    532 		}
    533 	}
    534 	if (ip->i_flags & (IMMUTABLE | APPEND)) {
    535 		error = EPERM;
    536 		goto out;
    537 	}
    538 	/*
    539 	 * Go through the fields and update iff not VNOVAL.
    540 	 */
    541 	if (vap->va_uid != (uid_t)VNOVAL || vap->va_gid != (gid_t)VNOVAL) {
    542 		if (vp->v_mount->mnt_flag & MNT_RDONLY) {
    543 			error = EROFS;
    544 			goto out;
    545 		}
    546 		error = ULFS_WAPBL_BEGIN(vp->v_mount);
    547 		if (error)
    548 			goto out;
    549 		error = ulfs_chown(vp, vap->va_uid, vap->va_gid, cred, l);
    550 		ULFS_WAPBL_END(vp->v_mount);
    551 		if (error)
    552 			goto out;
    553 	}
    554 	if (vap->va_size != VNOVAL) {
    555 		/*
    556 		 * Disallow write attempts on read-only file systems;
    557 		 * unless the file is a socket, fifo, or a block or
    558 		 * character device resident on the file system.
    559 		 */
    560 		switch (vp->v_type) {
    561 		case VDIR:
    562 			error = EISDIR;
    563 			goto out;
    564 		case VCHR:
    565 		case VBLK:
    566 		case VFIFO:
    567 			break;
    568 		case VREG:
    569 			if (vp->v_mount->mnt_flag & MNT_RDONLY) {
    570 				error = EROFS;
    571 				goto out;
    572 			}
    573 			if ((ip->i_flags & SF_SNAPSHOT) != 0) {
    574 				error = EPERM;
    575 				goto out;
    576 			}
    577 			error = ULFS_WAPBL_BEGIN(vp->v_mount);
    578 			if (error)
    579 				goto out;
    580 			/*
    581 			 * When journaling, only truncate one indirect block
    582 			 * at a time.
    583 			 */
    584 			if (vp->v_mount->mnt_wapbl) {
    585 				uint64_t incr = MNINDIR(ip->i_ump) <<
    586 				    vp->v_mount->mnt_fs_bshift; /* Power of 2 */
    587 				uint64_t base = ULFS_NDADDR <<
    588 				    vp->v_mount->mnt_fs_bshift;
    589 				while (!error && ip->i_size > base + incr &&
    590 				    ip->i_size > vap->va_size + incr) {
    591 					/*
    592 					 * round down to next full indirect
    593 					 * block boundary.
    594 					 */
    595 					uint64_t nsize = base +
    596 					    ((ip->i_size - base - 1) &
    597 					    ~(incr - 1));
    598 					error = ULFS_TRUNCATE(vp, nsize, 0,
    599 					    cred);
    600 					if (error == 0) {
    601 						ULFS_WAPBL_END(vp->v_mount);
    602 						error =
    603 						   ULFS_WAPBL_BEGIN(vp->v_mount);
    604 					}
    605 				}
    606 			}
    607 			if (!error)
    608 				error = ULFS_TRUNCATE(vp, vap->va_size, 0, cred);
    609 			ULFS_WAPBL_END(vp->v_mount);
    610 			if (error)
    611 				goto out;
    612 			break;
    613 		default:
    614 			error = EOPNOTSUPP;
    615 			goto out;
    616 		}
    617 	}
    618 	ip = VTOI(vp);
    619 	if (vap->va_atime.tv_sec != VNOVAL || vap->va_mtime.tv_sec != VNOVAL ||
    620 	    vap->va_birthtime.tv_sec != VNOVAL) {
    621 		if (vp->v_mount->mnt_flag & MNT_RDONLY) {
    622 			error = EROFS;
    623 			goto out;
    624 		}
    625 		if ((ip->i_flags & SF_SNAPSHOT) != 0) {
    626 			error = EPERM;
    627 			goto out;
    628 		}
    629 		error = kauth_authorize_vnode(cred, KAUTH_VNODE_WRITE_TIMES, vp,
    630 		    NULL, genfs_can_chtimes(vp, vap->va_vaflags, ip->i_uid, cred));
    631 		if (error)
    632 			goto out;
    633 		error = ULFS_WAPBL_BEGIN(vp->v_mount);
    634 		if (error)
    635 			goto out;
    636 		if (vap->va_atime.tv_sec != VNOVAL)
    637 			if (!(vp->v_mount->mnt_flag & MNT_NOATIME))
    638 				ip->i_flag |= IN_ACCESS;
    639 		if (vap->va_mtime.tv_sec != VNOVAL) {
    640 			ip->i_flag |= IN_CHANGE | IN_UPDATE;
    641 			if (vp->v_mount->mnt_flag & MNT_RELATIME)
    642 				ip->i_flag |= IN_ACCESS;
    643 		}
    644 		if (vap->va_birthtime.tv_sec != VNOVAL &&
    645 		    ip->i_ump->um_fstype == ULFS2) {
    646 			ip->i_ffs2_birthtime = vap->va_birthtime.tv_sec;
    647 			ip->i_ffs2_birthnsec = vap->va_birthtime.tv_nsec;
    648 		}
    649 		error = ULFS_UPDATE(vp, &vap->va_atime, &vap->va_mtime, 0);
    650 		ULFS_WAPBL_END(vp->v_mount);
    651 		if (error)
    652 			goto out;
    653 	}
    654 	error = 0;
    655 	if (vap->va_mode != (mode_t)VNOVAL) {
    656 		if (vp->v_mount->mnt_flag & MNT_RDONLY) {
    657 			error = EROFS;
    658 			goto out;
    659 		}
    660 		if ((ip->i_flags & SF_SNAPSHOT) != 0 &&
    661 		    (vap->va_mode & (S_IXUSR | S_IWUSR | S_IXGRP | S_IWGRP |
    662 		     S_IXOTH | S_IWOTH))) {
    663 			error = EPERM;
    664 			goto out;
    665 		}
    666 		error = ULFS_WAPBL_BEGIN(vp->v_mount);
    667 		if (error)
    668 			goto out;
    669 		error = ulfs_chmod(vp, (int)vap->va_mode, cred, l);
    670 		ULFS_WAPBL_END(vp->v_mount);
    671 	}
    672 	VN_KNOTE(vp, NOTE_ATTRIB);
    673 out:
    674 	fstrans_done(vp->v_mount);
    675 	return (error);
    676 }
    677 
    678 /*
    679  * Change the mode on a file.
    680  * Inode must be locked before calling.
    681  */
    682 static int
    683 ulfs_chmod(struct vnode *vp, int mode, kauth_cred_t cred, struct lwp *l)
    684 {
    685 	struct inode	*ip;
    686 	int		error;
    687 
    688 	ULFS_WAPBL_JLOCK_ASSERT(vp->v_mount);
    689 
    690 	ip = VTOI(vp);
    691 
    692 	error = kauth_authorize_vnode(cred, KAUTH_VNODE_WRITE_SECURITY, vp,
    693 	    NULL, genfs_can_chmod(vp->v_type, cred, ip->i_uid, ip->i_gid, mode));
    694 	if (error)
    695 		return (error);
    696 
    697 	fstrans_start(vp->v_mount, FSTRANS_SHARED);
    698 	ip->i_mode &= ~ALLPERMS;
    699 	ip->i_mode |= (mode & ALLPERMS);
    700 	ip->i_flag |= IN_CHANGE;
    701 	DIP_ASSIGN(ip, mode, ip->i_mode);
    702 	ULFS_WAPBL_UPDATE(vp, NULL, NULL, 0);
    703 	fstrans_done(vp->v_mount);
    704 	return (0);
    705 }
    706 
    707 /*
    708  * Perform chown operation on inode ip;
    709  * inode must be locked prior to call.
    710  */
    711 static int
    712 ulfs_chown(struct vnode *vp, uid_t uid, gid_t gid, kauth_cred_t cred,
    713     	struct lwp *l)
    714 {
    715 	struct inode	*ip;
    716 	int		error = 0;
    717 #if defined(LFS_QUOTA) || defined(LFS_QUOTA2)
    718 	uid_t		ouid;
    719 	gid_t		ogid;
    720 	int64_t		change;
    721 #endif
    722 	ip = VTOI(vp);
    723 	error = 0;
    724 
    725 	if (uid == (uid_t)VNOVAL)
    726 		uid = ip->i_uid;
    727 	if (gid == (gid_t)VNOVAL)
    728 		gid = ip->i_gid;
    729 
    730 	error = kauth_authorize_vnode(cred, KAUTH_VNODE_CHANGE_OWNERSHIP, vp,
    731 	    NULL, genfs_can_chown(cred, ip->i_uid, ip->i_gid, uid, gid));
    732 	if (error)
    733 		return (error);
    734 
    735 	fstrans_start(vp->v_mount, FSTRANS_SHARED);
    736 #if defined(LFS_QUOTA) || defined(LFS_QUOTA2)
    737 	ogid = ip->i_gid;
    738 	ouid = ip->i_uid;
    739 	change = DIP(ip, blocks);
    740 	(void) lfs_chkdq(ip, -change, cred, 0);
    741 	(void) lfs_chkiq(ip, -1, cred, 0);
    742 #endif
    743 	ip->i_gid = gid;
    744 	DIP_ASSIGN(ip, gid, gid);
    745 	ip->i_uid = uid;
    746 	DIP_ASSIGN(ip, uid, uid);
    747 #if defined(LFS_QUOTA) || defined(LFS_QUOTA2)
    748 	if ((error = lfs_chkdq(ip, change, cred, 0)) == 0) {
    749 		if ((error = lfs_chkiq(ip, 1, cred, 0)) == 0)
    750 			goto good;
    751 		else
    752 			(void) lfs_chkdq(ip, -change, cred, FORCE);
    753 	}
    754 	ip->i_gid = ogid;
    755 	DIP_ASSIGN(ip, gid, ogid);
    756 	ip->i_uid = ouid;
    757 	DIP_ASSIGN(ip, uid, ouid);
    758 	(void) lfs_chkdq(ip, change, cred, FORCE);
    759 	(void) lfs_chkiq(ip, 1, cred, FORCE);
    760 	fstrans_done(vp->v_mount);
    761 	return (error);
    762  good:
    763 #endif /* LFS_QUOTA || LFS_QUOTA2 */
    764 	ip->i_flag |= IN_CHANGE;
    765 	ULFS_WAPBL_UPDATE(vp, NULL, NULL, 0);
    766 	fstrans_done(vp->v_mount);
    767 	return (0);
    768 }
    769 
    770 int
    771 ulfs_remove(void *v)
    772 {
    773 	struct vop_remove_args /* {
    774 		struct vnode		*a_dvp;
    775 		struct vnode		*a_vp;
    776 		struct componentname	*a_cnp;
    777 	} */ *ap = v;
    778 	struct vnode	*vp, *dvp;
    779 	struct inode	*ip;
    780 	int		error;
    781 	struct ulfs_lookup_results *ulr;
    782 
    783 	vp = ap->a_vp;
    784 	dvp = ap->a_dvp;
    785 	ip = VTOI(vp);
    786 
    787 	/* XXX should handle this material another way */
    788 	ulr = &VTOI(dvp)->i_crap;
    789 	ULFS_CHECK_CRAPCOUNTER(VTOI(dvp));
    790 
    791 	fstrans_start(dvp->v_mount, FSTRANS_SHARED);
    792 	if (vp->v_type == VDIR || (ip->i_flags & (IMMUTABLE | APPEND)) ||
    793 	    (VTOI(dvp)->i_flags & APPEND))
    794 		error = EPERM;
    795 	else {
    796 		error = ULFS_WAPBL_BEGIN(dvp->v_mount);
    797 		if (error == 0) {
    798 			error = ulfs_dirremove(dvp, ulr,
    799 					      ip, ap->a_cnp->cn_flags, 0);
    800 			ULFS_WAPBL_END(dvp->v_mount);
    801 		}
    802 	}
    803 	VN_KNOTE(vp, NOTE_DELETE);
    804 	VN_KNOTE(dvp, NOTE_WRITE);
    805 	if (dvp == vp)
    806 		vrele(vp);
    807 	else
    808 		vput(vp);
    809 	vput(dvp);
    810 	fstrans_done(dvp->v_mount);
    811 	return (error);
    812 }
    813 
    814 /*
    815  * ulfs_link: create hard link.
    816  */
    817 int
    818 ulfs_link(void *v)
    819 {
    820 	struct vop_link_args /* {
    821 		struct vnode *a_dvp;
    822 		struct vnode *a_vp;
    823 		struct componentname *a_cnp;
    824 	} */ *ap = v;
    825 	struct vnode *dvp = ap->a_dvp;
    826 	struct vnode *vp = ap->a_vp;
    827 	struct componentname *cnp = ap->a_cnp;
    828 	struct inode *ip;
    829 	struct direct *newdir;
    830 	int error;
    831 	struct ulfs_lookup_results *ulr;
    832 
    833 	KASSERT(dvp != vp);
    834 	KASSERT(vp->v_type != VDIR);
    835 	KASSERT(dvp->v_mount == vp->v_mount);
    836 
    837 	/* XXX should handle this material another way */
    838 	ulr = &VTOI(dvp)->i_crap;
    839 	ULFS_CHECK_CRAPCOUNTER(VTOI(dvp));
    840 
    841 	fstrans_start(dvp->v_mount, FSTRANS_SHARED);
    842 	error = vn_lock(vp, LK_EXCLUSIVE);
    843 	if (error) {
    844 		VOP_ABORTOP(dvp, cnp);
    845 		goto out2;
    846 	}
    847 	ip = VTOI(vp);
    848 	if ((nlink_t)ip->i_nlink >= LINK_MAX) {
    849 		VOP_ABORTOP(dvp, cnp);
    850 		error = EMLINK;
    851 		goto out1;
    852 	}
    853 	if (ip->i_flags & (IMMUTABLE | APPEND)) {
    854 		VOP_ABORTOP(dvp, cnp);
    855 		error = EPERM;
    856 		goto out1;
    857 	}
    858 	error = ULFS_WAPBL_BEGIN(vp->v_mount);
    859 	if (error) {
    860 		VOP_ABORTOP(dvp, cnp);
    861 		goto out1;
    862 	}
    863 	ip->i_nlink++;
    864 	DIP_ASSIGN(ip, nlink, ip->i_nlink);
    865 	ip->i_flag |= IN_CHANGE;
    866 	error = ULFS_UPDATE(vp, NULL, NULL, UPDATE_DIROP);
    867 	if (!error) {
    868 		newdir = pool_cache_get(ulfs_direct_cache, PR_WAITOK);
    869 		ulfs_makedirentry(ip, cnp, newdir);
    870 		error = ulfs_direnter(dvp, ulr, vp, newdir, cnp, NULL);
    871 		pool_cache_put(ulfs_direct_cache, newdir);
    872 	}
    873 	if (error) {
    874 		ip->i_nlink--;
    875 		DIP_ASSIGN(ip, nlink, ip->i_nlink);
    876 		ip->i_flag |= IN_CHANGE;
    877 		ULFS_WAPBL_UPDATE(vp, NULL, NULL, UPDATE_DIROP);
    878 	}
    879 	ULFS_WAPBL_END(vp->v_mount);
    880  out1:
    881 	VOP_UNLOCK(vp);
    882  out2:
    883 	VN_KNOTE(vp, NOTE_LINK);
    884 	VN_KNOTE(dvp, NOTE_WRITE);
    885 	vput(dvp);
    886 	fstrans_done(dvp->v_mount);
    887 	return (error);
    888 }
    889 
    890 /*
    891  * whiteout vnode call
    892  */
    893 int
    894 ulfs_whiteout(void *v)
    895 {
    896 	struct vop_whiteout_args /* {
    897 		struct vnode		*a_dvp;
    898 		struct componentname	*a_cnp;
    899 		int			a_flags;
    900 	} */ *ap = v;
    901 	struct vnode		*dvp = ap->a_dvp;
    902 	struct componentname	*cnp = ap->a_cnp;
    903 	struct direct		*newdir;
    904 	int			error;
    905 	struct ulfsmount	*ump = VFSTOULFS(dvp->v_mount);
    906 	struct ulfs_lookup_results *ulr;
    907 
    908 	/* XXX should handle this material another way */
    909 	ulr = &VTOI(dvp)->i_crap;
    910 	ULFS_CHECK_CRAPCOUNTER(VTOI(dvp));
    911 
    912 	error = 0;
    913 	switch (ap->a_flags) {
    914 	case LOOKUP:
    915 		/* 4.4 format directories support whiteout operations */
    916 		if (ump->um_maxsymlinklen > 0)
    917 			return (0);
    918 		return (EOPNOTSUPP);
    919 
    920 	case CREATE:
    921 		/* create a new directory whiteout */
    922 		fstrans_start(dvp->v_mount, FSTRANS_SHARED);
    923 		error = ULFS_WAPBL_BEGIN(dvp->v_mount);
    924 		if (error)
    925 			break;
    926 #ifdef DIAGNOSTIC
    927 		if (ump->um_maxsymlinklen <= 0)
    928 			panic("ulfs_whiteout: old format filesystem");
    929 #endif
    930 
    931 		newdir = pool_cache_get(ulfs_direct_cache, PR_WAITOK);
    932 		newdir->d_ino = ULFS_WINO;
    933 		newdir->d_namlen = cnp->cn_namelen;
    934 		memcpy(newdir->d_name, cnp->cn_nameptr,
    935 		    (size_t)cnp->cn_namelen);
    936 		newdir->d_name[cnp->cn_namelen] = '\0';
    937 		newdir->d_type = DT_WHT;
    938 		error = ulfs_direnter(dvp, ulr, NULL, newdir, cnp, NULL);
    939 		pool_cache_put(ulfs_direct_cache, newdir);
    940 		break;
    941 
    942 	case DELETE:
    943 		/* remove an existing directory whiteout */
    944 		fstrans_start(dvp->v_mount, FSTRANS_SHARED);
    945 		error = ULFS_WAPBL_BEGIN(dvp->v_mount);
    946 		if (error)
    947 			break;
    948 #ifdef DIAGNOSTIC
    949 		if (ump->um_maxsymlinklen <= 0)
    950 			panic("ulfs_whiteout: old format filesystem");
    951 #endif
    952 
    953 		cnp->cn_flags &= ~DOWHITEOUT;
    954 		error = ulfs_dirremove(dvp, ulr, NULL, cnp->cn_flags, 0);
    955 		break;
    956 	default:
    957 		panic("ulfs_whiteout: unknown op");
    958 		/* NOTREACHED */
    959 	}
    960 	ULFS_WAPBL_END(dvp->v_mount);
    961 	fstrans_done(dvp->v_mount);
    962 	return (error);
    963 }
    964 
    965 int
    966 ulfs_mkdir(void *v)
    967 {
    968 	struct vop_mkdir_args /* {
    969 		struct vnode		*a_dvp;
    970 		struct vnode		**a_vpp;
    971 		struct componentname	*a_cnp;
    972 		struct vattr		*a_vap;
    973 	} */ *ap = v;
    974 	struct vnode		*dvp = ap->a_dvp, *tvp;
    975 	struct vattr		*vap = ap->a_vap;
    976 	struct componentname	*cnp = ap->a_cnp;
    977 	struct inode		*ip, *dp = VTOI(dvp);
    978 	struct buf		*bp;
    979 	struct dirtemplate	dirtemplate;
    980 	struct direct		*newdir;
    981 	int			error, dmode;
    982 	struct ulfsmount	*ump = dp->i_ump;
    983 	int			dirblksiz = ump->um_dirblksiz;
    984 	struct ulfs_lookup_results *ulr;
    985 
    986 	fstrans_start(dvp->v_mount, FSTRANS_SHARED);
    987 
    988 	/* XXX should handle this material another way */
    989 	ulr = &dp->i_crap;
    990 	ULFS_CHECK_CRAPCOUNTER(dp);
    991 
    992 	if ((nlink_t)dp->i_nlink >= LINK_MAX) {
    993 		error = EMLINK;
    994 		goto out;
    995 	}
    996 	dmode = vap->va_mode & ACCESSPERMS;
    997 	dmode |= IFDIR;
    998 	/*
    999 	 * Must simulate part of ulfs_makeinode here to acquire the inode,
   1000 	 * but not have it entered in the parent directory. The entry is
   1001 	 * made later after writing "." and ".." entries.
   1002 	 */
   1003 	if ((error = ULFS_VALLOC(dvp, dmode, cnp->cn_cred, ap->a_vpp)) != 0)
   1004 		goto out;
   1005 
   1006 	tvp = *ap->a_vpp;
   1007 	ip = VTOI(tvp);
   1008 
   1009 	error = ULFS_WAPBL_BEGIN(ap->a_dvp->v_mount);
   1010 	if (error) {
   1011 		ULFS_VFREE(tvp, ip->i_number, dmode);
   1012 		vput(tvp);
   1013 		goto out;
   1014 	}
   1015 	ip->i_uid = kauth_cred_geteuid(cnp->cn_cred);
   1016 	DIP_ASSIGN(ip, uid, ip->i_uid);
   1017 	ip->i_gid = dp->i_gid;
   1018 	DIP_ASSIGN(ip, gid, ip->i_gid);
   1019 #if defined(LFS_QUOTA) || defined(LFS_QUOTA2)
   1020 	if ((error = lfs_chkiq(ip, 1, cnp->cn_cred, 0))) {
   1021 		ULFS_VFREE(tvp, ip->i_number, dmode);
   1022 		ULFS_WAPBL_END(dvp->v_mount);
   1023 		fstrans_done(dvp->v_mount);
   1024 		vput(tvp);
   1025 		vput(dvp);
   1026 		return (error);
   1027 	}
   1028 #endif
   1029 	ip->i_flag |= IN_ACCESS | IN_CHANGE | IN_UPDATE;
   1030 	ip->i_mode = dmode;
   1031 	DIP_ASSIGN(ip, mode, dmode);
   1032 	tvp->v_type = VDIR;	/* Rest init'd in getnewvnode(). */
   1033 	ip->i_nlink = 2;
   1034 	DIP_ASSIGN(ip, nlink, 2);
   1035 	if (cnp->cn_flags & ISWHITEOUT) {
   1036 		ip->i_flags |= UF_OPAQUE;
   1037 		DIP_ASSIGN(ip, flags, ip->i_flags);
   1038 	}
   1039 
   1040 	/*
   1041 	 * Bump link count in parent directory to reflect work done below.
   1042 	 * Should be done before reference is created so cleanup is
   1043 	 * possible if we crash.
   1044 	 */
   1045 	dp->i_nlink++;
   1046 	DIP_ASSIGN(dp, nlink, dp->i_nlink);
   1047 	dp->i_flag |= IN_CHANGE;
   1048 	if ((error = ULFS_UPDATE(dvp, NULL, NULL, UPDATE_DIROP)) != 0)
   1049 		goto bad;
   1050 
   1051 	/*
   1052 	 * Initialize directory with "." and ".." from static template.
   1053 	 */
   1054 	dirtemplate = mastertemplate;
   1055 	dirtemplate.dotdot_reclen = dirblksiz - dirtemplate.dot_reclen;
   1056 	dirtemplate.dot_ino = ulfs_rw32(ip->i_number, ULFS_MPNEEDSWAP(ump));
   1057 	dirtemplate.dotdot_ino = ulfs_rw32(dp->i_number, ULFS_MPNEEDSWAP(ump));
   1058 	dirtemplate.dot_reclen = ulfs_rw16(dirtemplate.dot_reclen,
   1059 	    ULFS_MPNEEDSWAP(ump));
   1060 	dirtemplate.dotdot_reclen = ulfs_rw16(dirtemplate.dotdot_reclen,
   1061 	    ULFS_MPNEEDSWAP(ump));
   1062 	if (ump->um_maxsymlinklen <= 0) {
   1063 #if BYTE_ORDER == LITTLE_ENDIAN
   1064 		if (ULFS_MPNEEDSWAP(ump) == 0)
   1065 #else
   1066 		if (ULFS_MPNEEDSWAP(ump) != 0)
   1067 #endif
   1068 		{
   1069 			dirtemplate.dot_type = dirtemplate.dot_namlen;
   1070 			dirtemplate.dotdot_type = dirtemplate.dotdot_namlen;
   1071 			dirtemplate.dot_namlen = dirtemplate.dotdot_namlen = 0;
   1072 		} else
   1073 			dirtemplate.dot_type = dirtemplate.dotdot_type = 0;
   1074 	}
   1075 	if ((error = ULFS_BALLOC(tvp, (off_t)0, dirblksiz, cnp->cn_cred,
   1076 	    B_CLRBUF, &bp)) != 0)
   1077 		goto bad;
   1078 	ip->i_size = dirblksiz;
   1079 	DIP_ASSIGN(ip, size, dirblksiz);
   1080 	ip->i_flag |= IN_ACCESS | IN_CHANGE | IN_UPDATE;
   1081 	uvm_vnp_setsize(tvp, ip->i_size);
   1082 	memcpy((void *)bp->b_data, (void *)&dirtemplate, sizeof dirtemplate);
   1083 
   1084 	/*
   1085 	 * Directory set up, now install it's entry in the parent directory.
   1086 	 * We must write out the buffer containing the new directory body
   1087 	 * before entering the new name in the parent.
   1088 	 */
   1089 	if ((error = VOP_BWRITE(bp->b_vp, bp)) != 0)
   1090 		goto bad;
   1091 	if ((error = ULFS_UPDATE(tvp, NULL, NULL, UPDATE_DIROP)) != 0) {
   1092 		goto bad;
   1093 	}
   1094 	newdir = pool_cache_get(ulfs_direct_cache, PR_WAITOK);
   1095 	ulfs_makedirentry(ip, cnp, newdir);
   1096 	error = ulfs_direnter(dvp, ulr, tvp, newdir, cnp, bp);
   1097 	pool_cache_put(ulfs_direct_cache, newdir);
   1098  bad:
   1099 	if (error == 0) {
   1100 		VN_KNOTE(dvp, NOTE_WRITE | NOTE_LINK);
   1101 		ULFS_WAPBL_END(dvp->v_mount);
   1102 	} else {
   1103 		dp->i_nlink--;
   1104 		DIP_ASSIGN(dp, nlink, dp->i_nlink);
   1105 		dp->i_flag |= IN_CHANGE;
   1106 		ULFS_WAPBL_UPDATE(dvp, NULL, NULL, UPDATE_DIROP);
   1107 		/*
   1108 		 * No need to do an explicit ULFS_TRUNCATE here, vrele will
   1109 		 * do this for us because we set the link count to 0.
   1110 		 */
   1111 		ip->i_nlink = 0;
   1112 		DIP_ASSIGN(ip, nlink, 0);
   1113 		ip->i_flag |= IN_CHANGE;
   1114 		/* If IN_ADIROP, account for it */
   1115 		ULFS_UNMARK_VNODE(tvp);
   1116 		ULFS_WAPBL_UPDATE(tvp, NULL, NULL, UPDATE_DIROP);
   1117 		ULFS_WAPBL_END(dvp->v_mount);
   1118 		vput(tvp);
   1119 	}
   1120  out:
   1121 	fstrans_done(dvp->v_mount);
   1122 	vput(dvp);
   1123 	return (error);
   1124 }
   1125 
   1126 int
   1127 ulfs_rmdir(void *v)
   1128 {
   1129 	struct vop_rmdir_args /* {
   1130 		struct vnode		*a_dvp;
   1131 		struct vnode		*a_vp;
   1132 		struct componentname	*a_cnp;
   1133 	} */ *ap = v;
   1134 	struct vnode		*vp, *dvp;
   1135 	struct componentname	*cnp;
   1136 	struct inode		*ip, *dp;
   1137 	int			error;
   1138 	struct ulfs_lookup_results *ulr;
   1139 
   1140 	vp = ap->a_vp;
   1141 	dvp = ap->a_dvp;
   1142 	cnp = ap->a_cnp;
   1143 	ip = VTOI(vp);
   1144 	dp = VTOI(dvp);
   1145 
   1146 	/* XXX should handle this material another way */
   1147 	ulr = &dp->i_crap;
   1148 	ULFS_CHECK_CRAPCOUNTER(dp);
   1149 
   1150 	/*
   1151 	 * No rmdir "." or of mounted directories please.
   1152 	 */
   1153 	if (dp == ip || vp->v_mountedhere != NULL) {
   1154 		if (dp == ip)
   1155 			vrele(dvp);
   1156 		else
   1157 			vput(dvp);
   1158 		vput(vp);
   1159 		return (EINVAL);
   1160 	}
   1161 
   1162 	fstrans_start(dvp->v_mount, FSTRANS_SHARED);
   1163 
   1164 	/*
   1165 	 * Do not remove a directory that is in the process of being renamed.
   1166 	 * Verify that the directory is empty (and valid). (Rmdir ".." won't
   1167 	 * be valid since ".." will contain a reference to the current
   1168 	 * directory and thus be non-empty.)
   1169 	 */
   1170 	error = 0;
   1171 	if (ip->i_nlink != 2 ||
   1172 	    !ulfs_dirempty(ip, dp->i_number, cnp->cn_cred)) {
   1173 		error = ENOTEMPTY;
   1174 		goto out;
   1175 	}
   1176 	if ((dp->i_flags & APPEND) ||
   1177 		(ip->i_flags & (IMMUTABLE | APPEND))) {
   1178 		error = EPERM;
   1179 		goto out;
   1180 	}
   1181 	error = ULFS_WAPBL_BEGIN(dvp->v_mount);
   1182 	if (error)
   1183 		goto out;
   1184 	/*
   1185 	 * Delete reference to directory before purging
   1186 	 * inode.  If we crash in between, the directory
   1187 	 * will be reattached to lost+found,
   1188 	 */
   1189 	error = ulfs_dirremove(dvp, ulr, ip, cnp->cn_flags, 1);
   1190 	if (error) {
   1191 		ULFS_WAPBL_END(dvp->v_mount);
   1192 		goto out;
   1193 	}
   1194 	VN_KNOTE(dvp, NOTE_WRITE | NOTE_LINK);
   1195 	cache_purge(dvp);
   1196 	/*
   1197 	 * Truncate inode.  The only stuff left in the directory is "." and
   1198 	 * "..".  The "." reference is inconsequential since we're quashing
   1199 	 * it.
   1200 	 */
   1201 	dp->i_nlink--;
   1202 	DIP_ASSIGN(dp, nlink, dp->i_nlink);
   1203 	dp->i_flag |= IN_CHANGE;
   1204 	ULFS_WAPBL_UPDATE(dvp, NULL, NULL, UPDATE_DIROP);
   1205 	ip->i_nlink--;
   1206 	DIP_ASSIGN(ip, nlink, ip->i_nlink);
   1207 	ip->i_flag |= IN_CHANGE;
   1208 	error = ULFS_TRUNCATE(vp, (off_t)0, IO_SYNC, cnp->cn_cred);
   1209 	cache_purge(vp);
   1210 	/*
   1211 	 * Unlock the log while we still have reference to unlinked
   1212 	 * directory vp so that it will not get locked for recycling
   1213 	 */
   1214 	ULFS_WAPBL_END(dvp->v_mount);
   1215 #ifdef LFS_DIRHASH
   1216 	if (ip->i_dirhash != NULL)
   1217 		ulfsdirhash_free(ip);
   1218 #endif
   1219  out:
   1220 	VN_KNOTE(vp, NOTE_DELETE);
   1221 	vput(vp);
   1222 	fstrans_done(dvp->v_mount);
   1223 	vput(dvp);
   1224 	return (error);
   1225 }
   1226 
   1227 /*
   1228  * symlink -- make a symbolic link
   1229  */
   1230 int
   1231 ulfs_symlink(void *v)
   1232 {
   1233 	struct vop_symlink_args /* {
   1234 		struct vnode		*a_dvp;
   1235 		struct vnode		**a_vpp;
   1236 		struct componentname	*a_cnp;
   1237 		struct vattr		*a_vap;
   1238 		char			*a_target;
   1239 	} */ *ap = v;
   1240 	struct vnode	*vp, **vpp;
   1241 	struct inode	*ip;
   1242 	int		len, error;
   1243 	struct ulfs_lookup_results *ulr;
   1244 
   1245 	vpp = ap->a_vpp;
   1246 
   1247 	/* XXX should handle this material another way */
   1248 	ulr = &VTOI(ap->a_dvp)->i_crap;
   1249 	ULFS_CHECK_CRAPCOUNTER(VTOI(ap->a_dvp));
   1250 
   1251 	/*
   1252 	 * ULFS_WAPBL_BEGIN1(dvp->v_mount, dvp) performed by successful
   1253 	 * ulfs_makeinode
   1254 	 */
   1255 	fstrans_start(ap->a_dvp->v_mount, FSTRANS_SHARED);
   1256 	error = ulfs_makeinode(IFLNK | ap->a_vap->va_mode, ap->a_dvp, ulr,
   1257 			      vpp, ap->a_cnp);
   1258 	if (error)
   1259 		goto out;
   1260 	VN_KNOTE(ap->a_dvp, NOTE_WRITE);
   1261 	vp = *vpp;
   1262 	len = strlen(ap->a_target);
   1263 	ip = VTOI(vp);
   1264 	if (len < ip->i_ump->um_maxsymlinklen) {
   1265 		memcpy((char *)SHORTLINK(ip), ap->a_target, len);
   1266 		ip->i_size = len;
   1267 		DIP_ASSIGN(ip, size, len);
   1268 		uvm_vnp_setsize(vp, ip->i_size);
   1269 		ip->i_flag |= IN_CHANGE | IN_UPDATE;
   1270 		if (vp->v_mount->mnt_flag & MNT_RELATIME)
   1271 			ip->i_flag |= IN_ACCESS;
   1272 		ULFS_WAPBL_UPDATE(vp, NULL, NULL, 0);
   1273 	} else
   1274 		error = vn_rdwr(UIO_WRITE, vp, ap->a_target, len, (off_t)0,
   1275 		    UIO_SYSSPACE, IO_NODELOCKED | IO_JOURNALLOCKED,
   1276 		    ap->a_cnp->cn_cred, NULL, NULL);
   1277 	ULFS_WAPBL_END1(ap->a_dvp->v_mount, ap->a_dvp);
   1278 	if (error)
   1279 		vput(vp);
   1280 out:
   1281 	fstrans_done(ap->a_dvp->v_mount);
   1282 	return (error);
   1283 }
   1284 
   1285 /*
   1286  * Vnode op for reading directories.
   1287  *
   1288  * This routine handles converting from the on-disk directory format
   1289  * "struct direct" to the in-memory format "struct dirent" as well as
   1290  * byte swapping the entries if necessary.
   1291  */
   1292 int
   1293 ulfs_readdir(void *v)
   1294 {
   1295 	struct vop_readdir_args /* {
   1296 		struct vnode	*a_vp;
   1297 		struct uio	*a_uio;
   1298 		kauth_cred_t	a_cred;
   1299 		int		*a_eofflag;
   1300 		off_t		**a_cookies;
   1301 		int		*ncookies;
   1302 	} */ *ap = v;
   1303 	struct vnode	*vp = ap->a_vp;
   1304 	struct direct	*cdp, *ecdp;
   1305 	struct dirent	*ndp;
   1306 	char		*cdbuf, *ndbuf, *endp;
   1307 	struct uio	auio, *uio;
   1308 	struct iovec	aiov;
   1309 	int		error;
   1310 	size_t		count, ccount, rcount, cdbufsz, ndbufsz;
   1311 	off_t		off, *ccp;
   1312 	off_t		startoff;
   1313 	size_t		skipbytes;
   1314 	struct ulfsmount *ump = VFSTOULFS(vp->v_mount);
   1315 	int nswap = ULFS_MPNEEDSWAP(ump);
   1316 #if BYTE_ORDER == LITTLE_ENDIAN
   1317 	int needswap = ump->um_maxsymlinklen <= 0 && nswap == 0;
   1318 #else
   1319 	int needswap = ump->um_maxsymlinklen <= 0 && nswap != 0;
   1320 #endif
   1321 	uio = ap->a_uio;
   1322 	count = uio->uio_resid;
   1323 	rcount = count - ((uio->uio_offset + count) & (ump->um_dirblksiz - 1));
   1324 
   1325 	if (rcount < _DIRENT_MINSIZE(cdp) || count < _DIRENT_MINSIZE(ndp))
   1326 		return EINVAL;
   1327 
   1328 	startoff = uio->uio_offset & ~(ump->um_dirblksiz - 1);
   1329 	skipbytes = uio->uio_offset - startoff;
   1330 	rcount += skipbytes;
   1331 
   1332 	auio.uio_iov = &aiov;
   1333 	auio.uio_iovcnt = 1;
   1334 	auio.uio_offset = startoff;
   1335 	auio.uio_resid = rcount;
   1336 	UIO_SETUP_SYSSPACE(&auio);
   1337 	auio.uio_rw = UIO_READ;
   1338 	cdbufsz = rcount;
   1339 	cdbuf = kmem_alloc(cdbufsz, KM_SLEEP);
   1340 	aiov.iov_base = cdbuf;
   1341 	aiov.iov_len = rcount;
   1342 	error = VOP_READ(vp, &auio, 0, ap->a_cred);
   1343 	if (error != 0) {
   1344 		kmem_free(cdbuf, cdbufsz);
   1345 		return error;
   1346 	}
   1347 
   1348 	rcount -= auio.uio_resid;
   1349 
   1350 	cdp = (struct direct *)(void *)cdbuf;
   1351 	ecdp = (struct direct *)(void *)&cdbuf[rcount];
   1352 
   1353 	ndbufsz = count;
   1354 	ndbuf = kmem_alloc(ndbufsz, KM_SLEEP);
   1355 	ndp = (struct dirent *)(void *)ndbuf;
   1356 	endp = &ndbuf[count];
   1357 
   1358 	off = uio->uio_offset;
   1359 	if (ap->a_cookies) {
   1360 		ccount = rcount / _DIRENT_RECLEN(cdp, 1);
   1361 		ccp = *(ap->a_cookies) = malloc(ccount * sizeof(*ccp),
   1362 		    M_TEMP, M_WAITOK);
   1363 	} else {
   1364 		/* XXX: GCC */
   1365 		ccount = 0;
   1366 		ccp = NULL;
   1367 	}
   1368 
   1369 	while (cdp < ecdp) {
   1370 		cdp->d_reclen = ulfs_rw16(cdp->d_reclen, nswap);
   1371 		if (skipbytes > 0) {
   1372 			if (cdp->d_reclen <= skipbytes) {
   1373 				skipbytes -= cdp->d_reclen;
   1374 				cdp = _DIRENT_NEXT(cdp);
   1375 				continue;
   1376 			}
   1377 			/*
   1378 			 * invalid cookie.
   1379 			 */
   1380 			error = EINVAL;
   1381 			goto out;
   1382 		}
   1383 		if (cdp->d_reclen == 0) {
   1384 			struct dirent *ondp = ndp;
   1385 			ndp->d_reclen = _DIRENT_MINSIZE(ndp);
   1386 			ndp = _DIRENT_NEXT(ndp);
   1387 			ondp->d_reclen = 0;
   1388 			cdp = ecdp;
   1389 			break;
   1390 		}
   1391 		if (needswap) {
   1392 			ndp->d_type = cdp->d_namlen;
   1393 			ndp->d_namlen = cdp->d_type;
   1394 		} else {
   1395 			ndp->d_type = cdp->d_type;
   1396 			ndp->d_namlen = cdp->d_namlen;
   1397 		}
   1398 		ndp->d_reclen = _DIRENT_RECLEN(ndp, ndp->d_namlen);
   1399 		if ((char *)(void *)ndp + ndp->d_reclen +
   1400 		    _DIRENT_MINSIZE(ndp) > endp)
   1401 			break;
   1402 		ndp->d_fileno = ulfs_rw32(cdp->d_ino, nswap);
   1403 		(void)memcpy(ndp->d_name, cdp->d_name, ndp->d_namlen);
   1404 		memset(&ndp->d_name[ndp->d_namlen], 0,
   1405 		    ndp->d_reclen - _DIRENT_NAMEOFF(ndp) - ndp->d_namlen);
   1406 		off += cdp->d_reclen;
   1407 		if (ap->a_cookies) {
   1408 			KASSERT(ccp - *(ap->a_cookies) < ccount);
   1409 			*(ccp++) = off;
   1410 		}
   1411 		ndp = _DIRENT_NEXT(ndp);
   1412 		cdp = _DIRENT_NEXT(cdp);
   1413 	}
   1414 
   1415 	count = ((char *)(void *)ndp - ndbuf);
   1416 	error = uiomove(ndbuf, count, uio);
   1417 out:
   1418 	if (ap->a_cookies) {
   1419 		if (error) {
   1420 			free(*(ap->a_cookies), M_TEMP);
   1421 			*(ap->a_cookies) = NULL;
   1422 			*(ap->a_ncookies) = 0;
   1423 		} else {
   1424 			*ap->a_ncookies = ccp - *(ap->a_cookies);
   1425 		}
   1426 	}
   1427 	uio->uio_offset = off;
   1428 	kmem_free(ndbuf, ndbufsz);
   1429 	kmem_free(cdbuf, cdbufsz);
   1430 	*ap->a_eofflag = VTOI(vp)->i_size <= uio->uio_offset;
   1431 	return error;
   1432 }
   1433 
   1434 /*
   1435  * Return target name of a symbolic link
   1436  */
   1437 int
   1438 ulfs_readlink(void *v)
   1439 {
   1440 	struct vop_readlink_args /* {
   1441 		struct vnode	*a_vp;
   1442 		struct uio	*a_uio;
   1443 		kauth_cred_t	a_cred;
   1444 	} */ *ap = v;
   1445 	struct vnode	*vp = ap->a_vp;
   1446 	struct inode	*ip = VTOI(vp);
   1447 	struct ulfsmount *ump = VFSTOULFS(vp->v_mount);
   1448 	int		isize;
   1449 
   1450 	isize = ip->i_size;
   1451 	if (isize < ump->um_maxsymlinklen ||
   1452 	    (ump->um_maxsymlinklen == 0 && DIP(ip, blocks) == 0)) {
   1453 		uiomove((char *)SHORTLINK(ip), isize, ap->a_uio);
   1454 		return (0);
   1455 	}
   1456 	return (VOP_READ(vp, ap->a_uio, 0, ap->a_cred));
   1457 }
   1458 
   1459 /*
   1460  * Calculate the logical to physical mapping if not done already,
   1461  * then call the device strategy routine.
   1462  */
   1463 int
   1464 ulfs_strategy(void *v)
   1465 {
   1466 	struct vop_strategy_args /* {
   1467 		struct vnode *a_vp;
   1468 		struct buf *a_bp;
   1469 	} */ *ap = v;
   1470 	struct buf	*bp;
   1471 	struct vnode	*vp;
   1472 	struct inode	*ip;
   1473 	struct mount	*mp;
   1474 	int		error;
   1475 
   1476 	bp = ap->a_bp;
   1477 	vp = ap->a_vp;
   1478 	ip = VTOI(vp);
   1479 	if (vp->v_type == VBLK || vp->v_type == VCHR)
   1480 		panic("ulfs_strategy: spec");
   1481 	KASSERT(bp->b_bcount != 0);
   1482 	if (bp->b_blkno == bp->b_lblkno) {
   1483 		error = VOP_BMAP(vp, bp->b_lblkno, NULL, &bp->b_blkno,
   1484 				 NULL);
   1485 		if (error) {
   1486 			bp->b_error = error;
   1487 			biodone(bp);
   1488 			return (error);
   1489 		}
   1490 		if (bp->b_blkno == -1) /* no valid data */
   1491 			clrbuf(bp);
   1492 	}
   1493 	if (bp->b_blkno < 0) { /* block is not on disk */
   1494 		biodone(bp);
   1495 		return (0);
   1496 	}
   1497 	vp = ip->i_devvp;
   1498 
   1499 	error = VOP_STRATEGY(vp, bp);
   1500 	if (error)
   1501 		return error;
   1502 
   1503 	if (!BUF_ISREAD(bp))
   1504 		return 0;
   1505 
   1506 	mp = wapbl_vptomp(vp);
   1507 	if (mp == NULL || mp->mnt_wapbl_replay == NULL ||
   1508 	    !WAPBL_REPLAY_ISOPEN(mp) ||
   1509 	    !WAPBL_REPLAY_CAN_READ(mp, bp->b_blkno, bp->b_bcount))
   1510 		return 0;
   1511 
   1512 	error = biowait(bp);
   1513 	if (error)
   1514 		return error;
   1515 
   1516 	error = WAPBL_REPLAY_READ(mp, bp->b_data, bp->b_blkno, bp->b_bcount);
   1517 	if (error) {
   1518 		mutex_enter(&bufcache_lock);
   1519 		SET(bp->b_cflags, BC_INVAL);
   1520 		mutex_exit(&bufcache_lock);
   1521 	}
   1522 	return error;
   1523 }
   1524 
   1525 /*
   1526  * Print out the contents of an inode.
   1527  */
   1528 int
   1529 ulfs_print(void *v)
   1530 {
   1531 	struct vop_print_args /* {
   1532 		struct vnode	*a_vp;
   1533 	} */ *ap = v;
   1534 	struct vnode	*vp;
   1535 	struct inode	*ip;
   1536 
   1537 	vp = ap->a_vp;
   1538 	ip = VTOI(vp);
   1539 	printf("tag VT_ULFS, ino %llu, on dev %llu, %llu",
   1540 	    (unsigned long long)ip->i_number,
   1541 	    (unsigned long long)major(ip->i_dev),
   1542 	    (unsigned long long)minor(ip->i_dev));
   1543 	printf(" flags 0x%x, nlink %d\n",
   1544 	    ip->i_flag, ip->i_nlink);
   1545 	printf("\tmode 0%o, owner %d, group %d, size %qd",
   1546 	    ip->i_mode, ip->i_uid, ip->i_gid,
   1547 	    (long long)ip->i_size);
   1548 	if (vp->v_type == VFIFO)
   1549 		VOCALL(fifo_vnodeop_p, VOFFSET(vop_print), v);
   1550 	printf("\n");
   1551 	return (0);
   1552 }
   1553 
   1554 /*
   1555  * Read wrapper for special devices.
   1556  */
   1557 int
   1558 ulfsspec_read(void *v)
   1559 {
   1560 	struct vop_read_args /* {
   1561 		struct vnode	*a_vp;
   1562 		struct uio	*a_uio;
   1563 		int		a_ioflag;
   1564 		kauth_cred_t	a_cred;
   1565 	} */ *ap = v;
   1566 
   1567 	/*
   1568 	 * Set access flag.
   1569 	 */
   1570 	if ((ap->a_vp->v_mount->mnt_flag & MNT_NODEVMTIME) == 0)
   1571 		VTOI(ap->a_vp)->i_flag |= IN_ACCESS;
   1572 	return (VOCALL (spec_vnodeop_p, VOFFSET(vop_read), ap));
   1573 }
   1574 
   1575 /*
   1576  * Write wrapper for special devices.
   1577  */
   1578 int
   1579 ulfsspec_write(void *v)
   1580 {
   1581 	struct vop_write_args /* {
   1582 		struct vnode	*a_vp;
   1583 		struct uio	*a_uio;
   1584 		int		a_ioflag;
   1585 		kauth_cred_t	a_cred;
   1586 	} */ *ap = v;
   1587 
   1588 	/*
   1589 	 * Set update and change flags.
   1590 	 */
   1591 	if ((ap->a_vp->v_mount->mnt_flag & MNT_NODEVMTIME) == 0)
   1592 		VTOI(ap->a_vp)->i_flag |= IN_MODIFY;
   1593 	return (VOCALL (spec_vnodeop_p, VOFFSET(vop_write), ap));
   1594 }
   1595 
   1596 /*
   1597  * Close wrapper for special devices.
   1598  *
   1599  * Update the times on the inode then do device close.
   1600  */
   1601 int
   1602 ulfsspec_close(void *v)
   1603 {
   1604 	struct vop_close_args /* {
   1605 		struct vnode	*a_vp;
   1606 		int		a_fflag;
   1607 		kauth_cred_t	a_cred;
   1608 	} */ *ap = v;
   1609 	struct vnode	*vp;
   1610 	struct inode	*ip;
   1611 
   1612 	vp = ap->a_vp;
   1613 	ip = VTOI(vp);
   1614 	if (vp->v_usecount > 1)
   1615 		ULFS_ITIMES(vp, NULL, NULL, NULL);
   1616 	return (VOCALL (spec_vnodeop_p, VOFFSET(vop_close), ap));
   1617 }
   1618 
   1619 /*
   1620  * Read wrapper for fifo's
   1621  */
   1622 int
   1623 ulfsfifo_read(void *v)
   1624 {
   1625 	struct vop_read_args /* {
   1626 		struct vnode	*a_vp;
   1627 		struct uio	*a_uio;
   1628 		int		a_ioflag;
   1629 		kauth_cred_t	a_cred;
   1630 	} */ *ap = v;
   1631 
   1632 	/*
   1633 	 * Set access flag.
   1634 	 */
   1635 	VTOI(ap->a_vp)->i_flag |= IN_ACCESS;
   1636 	return (VOCALL (fifo_vnodeop_p, VOFFSET(vop_read), ap));
   1637 }
   1638 
   1639 /*
   1640  * Write wrapper for fifo's.
   1641  */
   1642 int
   1643 ulfsfifo_write(void *v)
   1644 {
   1645 	struct vop_write_args /* {
   1646 		struct vnode	*a_vp;
   1647 		struct uio	*a_uio;
   1648 		int		a_ioflag;
   1649 		kauth_cred_t	a_cred;
   1650 	} */ *ap = v;
   1651 
   1652 	/*
   1653 	 * Set update and change flags.
   1654 	 */
   1655 	VTOI(ap->a_vp)->i_flag |= IN_MODIFY;
   1656 	return (VOCALL (fifo_vnodeop_p, VOFFSET(vop_write), ap));
   1657 }
   1658 
   1659 /*
   1660  * Close wrapper for fifo's.
   1661  *
   1662  * Update the times on the inode then do device close.
   1663  */
   1664 int
   1665 ulfsfifo_close(void *v)
   1666 {
   1667 	struct vop_close_args /* {
   1668 		struct vnode	*a_vp;
   1669 		int		a_fflag;
   1670 		kauth_cred_t	a_cred;
   1671 	} */ *ap = v;
   1672 	struct vnode	*vp;
   1673 	struct inode	*ip;
   1674 
   1675 	vp = ap->a_vp;
   1676 	ip = VTOI(vp);
   1677 	if (ap->a_vp->v_usecount > 1)
   1678 		ULFS_ITIMES(vp, NULL, NULL, NULL);
   1679 	return (VOCALL (fifo_vnodeop_p, VOFFSET(vop_close), ap));
   1680 }
   1681 
   1682 /*
   1683  * Return POSIX pathconf information applicable to ulfs filesystems.
   1684  */
   1685 int
   1686 ulfs_pathconf(void *v)
   1687 {
   1688 	struct vop_pathconf_args /* {
   1689 		struct vnode	*a_vp;
   1690 		int		a_name;
   1691 		register_t	*a_retval;
   1692 	} */ *ap = v;
   1693 
   1694 	switch (ap->a_name) {
   1695 	case _PC_LINK_MAX:
   1696 		*ap->a_retval = LINK_MAX;
   1697 		return (0);
   1698 	case _PC_NAME_MAX:
   1699 		*ap->a_retval = LFS_MAXNAMLEN;
   1700 		return (0);
   1701 	case _PC_PATH_MAX:
   1702 		*ap->a_retval = PATH_MAX;
   1703 		return (0);
   1704 	case _PC_PIPE_BUF:
   1705 		*ap->a_retval = PIPE_BUF;
   1706 		return (0);
   1707 	case _PC_CHOWN_RESTRICTED:
   1708 		*ap->a_retval = 1;
   1709 		return (0);
   1710 	case _PC_NO_TRUNC:
   1711 		*ap->a_retval = 1;
   1712 		return (0);
   1713 	case _PC_SYNC_IO:
   1714 		*ap->a_retval = 1;
   1715 		return (0);
   1716 	case _PC_FILESIZEBITS:
   1717 		*ap->a_retval = 42;
   1718 		return (0);
   1719 	case _PC_SYMLINK_MAX:
   1720 		*ap->a_retval = MAXPATHLEN;
   1721 		return (0);
   1722 	case _PC_2_SYMLINKS:
   1723 		*ap->a_retval = 1;
   1724 		return (0);
   1725 	default:
   1726 		return (EINVAL);
   1727 	}
   1728 	/* NOTREACHED */
   1729 }
   1730 
   1731 /*
   1732  * Advisory record locking support
   1733  */
   1734 int
   1735 ulfs_advlock(void *v)
   1736 {
   1737 	struct vop_advlock_args /* {
   1738 		struct vnode	*a_vp;
   1739 		void *		a_id;
   1740 		int		a_op;
   1741 		struct flock	*a_fl;
   1742 		int		a_flags;
   1743 	} */ *ap = v;
   1744 	struct inode *ip;
   1745 
   1746 	ip = VTOI(ap->a_vp);
   1747 	return lf_advlock(ap, &ip->i_lockf, ip->i_size);
   1748 }
   1749 
   1750 /*
   1751  * Initialize the vnode associated with a new inode, handle aliased
   1752  * vnodes.
   1753  */
   1754 void
   1755 ulfs_vinit(struct mount *mntp, int (**specops)(void *), int (**fifoops)(void *),
   1756 	struct vnode **vpp)
   1757 {
   1758 	struct timeval	tv;
   1759 	struct inode	*ip;
   1760 	struct vnode	*vp;
   1761 	dev_t		rdev;
   1762 	struct ulfsmount *ump;
   1763 
   1764 	vp = *vpp;
   1765 	ip = VTOI(vp);
   1766 	switch(vp->v_type = IFTOVT(ip->i_mode)) {
   1767 	case VCHR:
   1768 	case VBLK:
   1769 		vp->v_op = specops;
   1770 		ump = ip->i_ump;
   1771 		if (ump->um_fstype == ULFS1)
   1772 			rdev = (dev_t)ulfs_rw32(ip->i_ffs1_rdev,
   1773 			    ULFS_MPNEEDSWAP(ump));
   1774 		else
   1775 			rdev = (dev_t)ulfs_rw64(ip->i_ffs2_rdev,
   1776 			    ULFS_MPNEEDSWAP(ump));
   1777 		spec_node_init(vp, rdev);
   1778 		break;
   1779 	case VFIFO:
   1780 		vp->v_op = fifoops;
   1781 		break;
   1782 	case VNON:
   1783 	case VBAD:
   1784 	case VSOCK:
   1785 	case VLNK:
   1786 	case VDIR:
   1787 	case VREG:
   1788 		break;
   1789 	}
   1790 	if (ip->i_number == ULFS_ROOTINO)
   1791                 vp->v_vflag |= VV_ROOT;
   1792 	/*
   1793 	 * Initialize modrev times
   1794 	 */
   1795 	getmicrouptime(&tv);
   1796 	ip->i_modrev = (uint64_t)(uint)tv.tv_sec << 32
   1797 			| tv.tv_usec * 4294u;
   1798 	*vpp = vp;
   1799 }
   1800 
   1801 /*
   1802  * Allocate a new inode.
   1803  */
   1804 int
   1805 ulfs_makeinode(int mode, struct vnode *dvp, const struct ulfs_lookup_results *ulr,
   1806 	struct vnode **vpp, struct componentname *cnp)
   1807 {
   1808 	struct inode	*ip, *pdir;
   1809 	struct direct	*newdir;
   1810 	struct vnode	*tvp;
   1811 	int		error;
   1812 
   1813 	ULFS_WAPBL_JUNLOCK_ASSERT(dvp->v_mount);
   1814 
   1815 	pdir = VTOI(dvp);
   1816 
   1817 	if ((mode & IFMT) == 0)
   1818 		mode |= IFREG;
   1819 
   1820 	if ((error = ULFS_VALLOC(dvp, mode, cnp->cn_cred, vpp)) != 0) {
   1821 		vput(dvp);
   1822 		return (error);
   1823 	}
   1824 	tvp = *vpp;
   1825 	ip = VTOI(tvp);
   1826 	ip->i_gid = pdir->i_gid;
   1827 	DIP_ASSIGN(ip, gid, ip->i_gid);
   1828 	ip->i_uid = kauth_cred_geteuid(cnp->cn_cred);
   1829 	DIP_ASSIGN(ip, uid, ip->i_uid);
   1830 	error = ULFS_WAPBL_BEGIN1(dvp->v_mount, dvp);
   1831 	if (error) {
   1832 		/*
   1833 		 * Note, we can't VOP_VFREE(tvp) here like we should
   1834 		 * because we can't write to the disk.  Instead, we leave
   1835 		 * the vnode dangling from the journal.
   1836 		 */
   1837 		vput(tvp);
   1838 		vput(dvp);
   1839 		return (error);
   1840 	}
   1841 #if defined(LFS_QUOTA) || defined(LFS_QUOTA2)
   1842 	if ((error = lfs_chkiq(ip, 1, cnp->cn_cred, 0))) {
   1843 		ULFS_VFREE(tvp, ip->i_number, mode);
   1844 		ULFS_WAPBL_END1(dvp->v_mount, dvp);
   1845 		vput(tvp);
   1846 		vput(dvp);
   1847 		return (error);
   1848 	}
   1849 #endif
   1850 	ip->i_flag |= IN_ACCESS | IN_CHANGE | IN_UPDATE;
   1851 	ip->i_mode = mode;
   1852 	DIP_ASSIGN(ip, mode, mode);
   1853 	tvp->v_type = IFTOVT(mode);	/* Rest init'd in getnewvnode(). */
   1854 	ip->i_nlink = 1;
   1855 	DIP_ASSIGN(ip, nlink, 1);
   1856 
   1857 	/* Authorize setting SGID if needed. */
   1858 	if (ip->i_mode & ISGID) {
   1859 		error = kauth_authorize_vnode(cnp->cn_cred, KAUTH_VNODE_WRITE_SECURITY,
   1860 		    tvp, NULL, genfs_can_chmod(tvp->v_type, cnp->cn_cred, ip->i_uid,
   1861 		    ip->i_gid, mode));
   1862 		if (error) {
   1863 			ip->i_mode &= ~ISGID;
   1864 			DIP_ASSIGN(ip, mode, ip->i_mode);
   1865 		}
   1866 	}
   1867 
   1868 	if (cnp->cn_flags & ISWHITEOUT) {
   1869 		ip->i_flags |= UF_OPAQUE;
   1870 		DIP_ASSIGN(ip, flags, ip->i_flags);
   1871 	}
   1872 
   1873 	/*
   1874 	 * Make sure inode goes to disk before directory entry.
   1875 	 */
   1876 	if ((error = ULFS_UPDATE(tvp, NULL, NULL, UPDATE_DIROP)) != 0)
   1877 		goto bad;
   1878 	newdir = pool_cache_get(ulfs_direct_cache, PR_WAITOK);
   1879 	ulfs_makedirentry(ip, cnp, newdir);
   1880 	error = ulfs_direnter(dvp, ulr, tvp, newdir, cnp, NULL);
   1881 	pool_cache_put(ulfs_direct_cache, newdir);
   1882 	if (error)
   1883 		goto bad;
   1884 	vput(dvp);
   1885 	*vpp = tvp;
   1886 	return (0);
   1887 
   1888  bad:
   1889 	/*
   1890 	 * Write error occurred trying to update the inode
   1891 	 * or the directory so must deallocate the inode.
   1892 	 */
   1893 	ip->i_nlink = 0;
   1894 	DIP_ASSIGN(ip, nlink, 0);
   1895 	ip->i_flag |= IN_CHANGE;
   1896 	/* If IN_ADIROP, account for it */
   1897 	ULFS_UNMARK_VNODE(tvp);
   1898 	ULFS_WAPBL_UPDATE(tvp, NULL, NULL, 0);
   1899 	tvp->v_type = VNON;		/* explodes later if VBLK */
   1900 	ULFS_WAPBL_END1(dvp->v_mount, dvp);
   1901 	vput(tvp);
   1902 	vput(dvp);
   1903 	return (error);
   1904 }
   1905 
   1906 /*
   1907  * Allocate len bytes at offset off.
   1908  */
   1909 int
   1910 ulfs_gop_alloc(struct vnode *vp, off_t off, off_t len, int flags,
   1911     kauth_cred_t cred)
   1912 {
   1913         struct inode *ip = VTOI(vp);
   1914         int error, delta, bshift, bsize;
   1915         UVMHIST_FUNC("ulfs_gop_alloc"); UVMHIST_CALLED(ubchist);
   1916 
   1917         error = 0;
   1918         bshift = vp->v_mount->mnt_fs_bshift;
   1919         bsize = 1 << bshift;
   1920 
   1921         delta = off & (bsize - 1);
   1922         off -= delta;
   1923         len += delta;
   1924 
   1925         while (len > 0) {
   1926                 bsize = MIN(bsize, len);
   1927 
   1928                 error = ULFS_BALLOC(vp, off, bsize, cred, flags, NULL);
   1929                 if (error) {
   1930                         goto out;
   1931                 }
   1932 
   1933                 /*
   1934                  * increase file size now, ULFS_BALLOC() requires that
   1935                  * EOF be up-to-date before each call.
   1936                  */
   1937 
   1938                 if (ip->i_size < off + bsize) {
   1939                         UVMHIST_LOG(ubchist, "vp %p old 0x%x new 0x%x",
   1940                             vp, ip->i_size, off + bsize, 0);
   1941                         ip->i_size = off + bsize;
   1942 			DIP_ASSIGN(ip, size, ip->i_size);
   1943                 }
   1944 
   1945                 off += bsize;
   1946                 len -= bsize;
   1947         }
   1948 
   1949 out:
   1950 	ULFS_WAPBL_UPDATE(vp, NULL, NULL, 0);
   1951 	return error;
   1952 }
   1953 
   1954 void
   1955 ulfs_gop_markupdate(struct vnode *vp, int flags)
   1956 {
   1957 	u_int32_t mask = 0;
   1958 
   1959 	if ((flags & GOP_UPDATE_ACCESSED) != 0) {
   1960 		mask = IN_ACCESS;
   1961 	}
   1962 	if ((flags & GOP_UPDATE_MODIFIED) != 0) {
   1963 		if (vp->v_type == VREG) {
   1964 			mask |= IN_CHANGE | IN_UPDATE;
   1965 		} else {
   1966 			mask |= IN_MODIFY;
   1967 		}
   1968 	}
   1969 	if (mask) {
   1970 		struct inode *ip = VTOI(vp);
   1971 
   1972 		ip->i_flag |= mask;
   1973 	}
   1974 }
   1975