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