Home | History | Annotate | Line # | Download | only in lfs
ulfs_vnops.c revision 1.29
      1 /*	$NetBSD: ulfs_vnops.c,v 1.29 2015/09/15 14:58:06 dholland 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.29 2015/09/15 14:58:06 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/lfs_extern.h>
    101 #include <ufs/lfs/lfs.h>
    102 #include <ufs/lfs/lfs_accessors.h>
    103 
    104 #include <ufs/lfs/ulfs_inode.h>
    105 #include <ufs/lfs/ulfsmount.h>
    106 #include <ufs/lfs/ulfs_bswap.h>
    107 #include <ufs/lfs/ulfs_extern.h>
    108 #ifdef LFS_DIRHASH
    109 #include <ufs/lfs/ulfs_dirhash.h>
    110 #endif
    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  * Open called.
    120  *
    121  * Nothing to do.
    122  */
    123 /* ARGSUSED */
    124 int
    125 ulfs_open(void *v)
    126 {
    127 	struct vop_open_args /* {
    128 		struct vnode	*a_vp;
    129 		int		a_mode;
    130 		kauth_cred_t	a_cred;
    131 	} */ *ap = v;
    132 
    133 	/*
    134 	 * Files marked append-only must be opened for appending.
    135 	 */
    136 	if ((VTOI(ap->a_vp)->i_flags & APPEND) &&
    137 	    (ap->a_mode & (FWRITE | O_APPEND)) == FWRITE)
    138 		return (EPERM);
    139 	return (0);
    140 }
    141 
    142 static int
    143 ulfs_check_possible(struct vnode *vp, struct inode *ip, mode_t mode,
    144     kauth_cred_t cred)
    145 {
    146 #if defined(LFS_QUOTA) || defined(LFS_QUOTA2)
    147 	int error;
    148 #endif
    149 
    150 	/*
    151 	 * Disallow write attempts on read-only file systems;
    152 	 * unless the file is a socket, fifo, or a block or
    153 	 * character device resident on the file system.
    154 	 */
    155 	if (mode & VWRITE) {
    156 		switch (vp->v_type) {
    157 		case VDIR:
    158 		case VLNK:
    159 		case VREG:
    160 			if (vp->v_mount->mnt_flag & MNT_RDONLY)
    161 				return (EROFS);
    162 #if defined(LFS_QUOTA) || defined(LFS_QUOTA2)
    163 			fstrans_start(vp->v_mount, FSTRANS_SHARED);
    164 			error = lfs_chkdq(ip, 0, cred, 0);
    165 			fstrans_done(vp->v_mount);
    166 			if (error != 0)
    167 				return error;
    168 #endif
    169 			break;
    170 		case VBAD:
    171 		case VBLK:
    172 		case VCHR:
    173 		case VSOCK:
    174 		case VFIFO:
    175 		case VNON:
    176 		default:
    177 			break;
    178 		}
    179 	}
    180 
    181 	/* If it is a snapshot, nobody gets access to it. */
    182 	if ((ip->i_flags & SF_SNAPSHOT))
    183 		return (EPERM);
    184 	/* If immutable bit set, nobody gets to write it. */
    185 	if ((mode & VWRITE) && (ip->i_flags & IMMUTABLE))
    186 		return (EPERM);
    187 
    188 	return 0;
    189 }
    190 
    191 static int
    192 ulfs_check_permitted(struct vnode *vp, struct inode *ip, mode_t mode,
    193     kauth_cred_t cred)
    194 {
    195 
    196 	return kauth_authorize_vnode(cred, KAUTH_ACCESS_ACTION(mode, vp->v_type,
    197 	    ip->i_mode & ALLPERMS), vp, NULL, genfs_can_access(vp->v_type,
    198 	    ip->i_mode & ALLPERMS, ip->i_uid, ip->i_gid, mode, cred));
    199 }
    200 
    201 int
    202 ulfs_access(void *v)
    203 {
    204 	struct vop_access_args /* {
    205 		struct vnode	*a_vp;
    206 		int		a_mode;
    207 		kauth_cred_t	a_cred;
    208 	} */ *ap = v;
    209 	struct vnode	*vp;
    210 	struct inode	*ip;
    211 	mode_t		mode;
    212 	int		error;
    213 
    214 	vp = ap->a_vp;
    215 	ip = VTOI(vp);
    216 	mode = ap->a_mode;
    217 
    218 	error = ulfs_check_possible(vp, ip, mode, ap->a_cred);
    219 	if (error)
    220 		return error;
    221 
    222 	error = ulfs_check_permitted(vp, ip, mode, ap->a_cred);
    223 
    224 	return error;
    225 }
    226 
    227 /*
    228  * Set attribute vnode op. called from several syscalls
    229  */
    230 int
    231 ulfs_setattr(void *v)
    232 {
    233 	struct vop_setattr_args /* {
    234 		struct vnode	*a_vp;
    235 		struct vattr	*a_vap;
    236 		kauth_cred_t	a_cred;
    237 	} */ *ap = v;
    238 	struct vattr	*vap;
    239 	struct vnode	*vp;
    240 	struct inode	*ip;
    241 	struct lfs	*fs;
    242 	kauth_cred_t	cred;
    243 	struct lwp	*l;
    244 	int		error;
    245 	kauth_action_t	action;
    246 	bool		changing_sysflags;
    247 
    248 	vap = ap->a_vap;
    249 	vp = ap->a_vp;
    250 	ip = VTOI(vp);
    251 	fs = ip->i_lfs;
    252 	cred = ap->a_cred;
    253 	l = curlwp;
    254 	action = KAUTH_VNODE_WRITE_FLAGS;
    255 	changing_sysflags = false;
    256 
    257 	/*
    258 	 * Check for unsettable attributes.
    259 	 */
    260 	if ((vap->va_type != VNON) || (vap->va_nlink != VNOVAL) ||
    261 	    (vap->va_fsid != VNOVAL) || (vap->va_fileid != VNOVAL) ||
    262 	    (vap->va_blocksize != VNOVAL) || (vap->va_rdev != VNOVAL) ||
    263 	    ((int)vap->va_bytes != VNOVAL) || (vap->va_gen != VNOVAL)) {
    264 		return (EINVAL);
    265 	}
    266 
    267 	fstrans_start(vp->v_mount, FSTRANS_SHARED);
    268 
    269 	if (vap->va_flags != VNOVAL) {
    270 		if (vp->v_mount->mnt_flag & MNT_RDONLY) {
    271 			error = EROFS;
    272 			goto out;
    273 		}
    274 
    275 		/* Snapshot flag cannot be set or cleared */
    276 		if ((vap->va_flags & (SF_SNAPSHOT | SF_SNAPINVAL)) !=
    277 		    (ip->i_flags & (SF_SNAPSHOT | SF_SNAPINVAL))) {
    278 			error = EPERM;
    279 			goto out;
    280 		}
    281 
    282 		if (ip->i_flags & (SF_IMMUTABLE | SF_APPEND)) {
    283 			action |= KAUTH_VNODE_HAS_SYSFLAGS;
    284 		}
    285 
    286 		if ((vap->va_flags & SF_SETTABLE) != (ip->i_flags & SF_SETTABLE)) {
    287 			action |= KAUTH_VNODE_WRITE_SYSFLAGS;
    288 			changing_sysflags = true;
    289 		}
    290 
    291 		error = kauth_authorize_vnode(cred, action, vp, NULL,
    292 		    genfs_can_chflags(cred, vp->v_type, ip->i_uid,
    293 		    changing_sysflags));
    294 		if (error)
    295 			goto out;
    296 
    297 		if (changing_sysflags) {
    298 			ip->i_flags = vap->va_flags;
    299 			DIP_ASSIGN(ip, flags, ip->i_flags);
    300 		} else {
    301 			ip->i_flags &= SF_SETTABLE;
    302 			ip->i_flags |= (vap->va_flags & UF_SETTABLE);
    303 			DIP_ASSIGN(ip, flags, ip->i_flags);
    304 		}
    305 		ip->i_flag |= IN_CHANGE;
    306 		if (vap->va_flags & (IMMUTABLE | APPEND)) {
    307 			error = 0;
    308 			goto out;
    309 		}
    310 	}
    311 	if (ip->i_flags & (IMMUTABLE | APPEND)) {
    312 		error = EPERM;
    313 		goto out;
    314 	}
    315 	/*
    316 	 * Go through the fields and update iff not VNOVAL.
    317 	 */
    318 	if (vap->va_uid != (uid_t)VNOVAL || vap->va_gid != (gid_t)VNOVAL) {
    319 		if (vp->v_mount->mnt_flag & MNT_RDONLY) {
    320 			error = EROFS;
    321 			goto out;
    322 		}
    323 		error = ulfs_chown(vp, vap->va_uid, vap->va_gid, cred, l);
    324 		if (error)
    325 			goto out;
    326 	}
    327 	if (vap->va_size != VNOVAL) {
    328 		/*
    329 		 * Disallow write attempts on read-only file systems;
    330 		 * unless the file is a socket, fifo, or a block or
    331 		 * character device resident on the file system.
    332 		 */
    333 		switch (vp->v_type) {
    334 		case VDIR:
    335 			error = EISDIR;
    336 			goto out;
    337 		case VCHR:
    338 		case VBLK:
    339 		case VFIFO:
    340 			break;
    341 		case VREG:
    342 			if (vp->v_mount->mnt_flag & MNT_RDONLY) {
    343 				error = EROFS;
    344 				goto out;
    345 			}
    346 			if ((ip->i_flags & SF_SNAPSHOT) != 0) {
    347 				error = EPERM;
    348 				goto out;
    349 			}
    350 			error = lfs_truncate(vp, vap->va_size, 0, cred);
    351 			if (error)
    352 				goto out;
    353 			break;
    354 		default:
    355 			error = EOPNOTSUPP;
    356 			goto out;
    357 		}
    358 	}
    359 	ip = VTOI(vp);
    360 	if (vap->va_atime.tv_sec != VNOVAL || vap->va_mtime.tv_sec != VNOVAL ||
    361 	    vap->va_birthtime.tv_sec != VNOVAL) {
    362 		if (vp->v_mount->mnt_flag & MNT_RDONLY) {
    363 			error = EROFS;
    364 			goto out;
    365 		}
    366 		if ((ip->i_flags & SF_SNAPSHOT) != 0) {
    367 			error = EPERM;
    368 			goto out;
    369 		}
    370 		error = kauth_authorize_vnode(cred, KAUTH_VNODE_WRITE_TIMES, vp,
    371 		    NULL, genfs_can_chtimes(vp, vap->va_vaflags, ip->i_uid, cred));
    372 		if (error)
    373 			goto out;
    374 		if (vap->va_atime.tv_sec != VNOVAL)
    375 			if (!(vp->v_mount->mnt_flag & MNT_NOATIME))
    376 				ip->i_flag |= IN_ACCESS;
    377 		if (vap->va_mtime.tv_sec != VNOVAL) {
    378 			ip->i_flag |= IN_CHANGE | IN_UPDATE;
    379 			if (vp->v_mount->mnt_flag & MNT_RELATIME)
    380 				ip->i_flag |= IN_ACCESS;
    381 		}
    382 		if (vap->va_birthtime.tv_sec != VNOVAL) {
    383 			lfs_dino_setbirthtime(fs, ip->i_din,
    384 					      &vap->va_birthtime);
    385 		}
    386 		error = lfs_update(vp, &vap->va_atime, &vap->va_mtime, 0);
    387 		if (error)
    388 			goto out;
    389 	}
    390 	error = 0;
    391 	if (vap->va_mode != (mode_t)VNOVAL) {
    392 		if (vp->v_mount->mnt_flag & MNT_RDONLY) {
    393 			error = EROFS;
    394 			goto out;
    395 		}
    396 		if ((ip->i_flags & SF_SNAPSHOT) != 0 &&
    397 		    (vap->va_mode & (S_IXUSR | S_IWUSR | S_IXGRP | S_IWGRP |
    398 		     S_IXOTH | S_IWOTH))) {
    399 			error = EPERM;
    400 			goto out;
    401 		}
    402 		error = ulfs_chmod(vp, (int)vap->va_mode, cred, l);
    403 	}
    404 	VN_KNOTE(vp, NOTE_ATTRIB);
    405 out:
    406 	fstrans_done(vp->v_mount);
    407 	return (error);
    408 }
    409 
    410 /*
    411  * Change the mode on a file.
    412  * Inode must be locked before calling.
    413  */
    414 static int
    415 ulfs_chmod(struct vnode *vp, int mode, kauth_cred_t cred, struct lwp *l)
    416 {
    417 	struct inode	*ip;
    418 	int		error;
    419 
    420 	ip = VTOI(vp);
    421 
    422 	error = kauth_authorize_vnode(cred, KAUTH_VNODE_WRITE_SECURITY, vp,
    423 	    NULL, genfs_can_chmod(vp->v_type, cred, ip->i_uid, ip->i_gid, mode));
    424 	if (error)
    425 		return (error);
    426 
    427 	fstrans_start(vp->v_mount, FSTRANS_SHARED);
    428 	ip->i_mode &= ~ALLPERMS;
    429 	ip->i_mode |= (mode & ALLPERMS);
    430 	ip->i_flag |= IN_CHANGE;
    431 	DIP_ASSIGN(ip, mode, ip->i_mode);
    432 	fstrans_done(vp->v_mount);
    433 	return (0);
    434 }
    435 
    436 /*
    437  * Perform chown operation on inode ip;
    438  * inode must be locked prior to call.
    439  */
    440 static int
    441 ulfs_chown(struct vnode *vp, uid_t uid, gid_t gid, kauth_cred_t cred,
    442     	struct lwp *l)
    443 {
    444 	struct inode	*ip;
    445 	int		error = 0;
    446 #if defined(LFS_QUOTA) || defined(LFS_QUOTA2)
    447 	uid_t		ouid;
    448 	gid_t		ogid;
    449 	int64_t		change;
    450 #endif
    451 	ip = VTOI(vp);
    452 	error = 0;
    453 
    454 	if (uid == (uid_t)VNOVAL)
    455 		uid = ip->i_uid;
    456 	if (gid == (gid_t)VNOVAL)
    457 		gid = ip->i_gid;
    458 
    459 	error = kauth_authorize_vnode(cred, KAUTH_VNODE_CHANGE_OWNERSHIP, vp,
    460 	    NULL, genfs_can_chown(cred, ip->i_uid, ip->i_gid, uid, gid));
    461 	if (error)
    462 		return (error);
    463 
    464 	fstrans_start(vp->v_mount, FSTRANS_SHARED);
    465 #if defined(LFS_QUOTA) || defined(LFS_QUOTA2)
    466 	ogid = ip->i_gid;
    467 	ouid = ip->i_uid;
    468 	change = DIP(ip, blocks);
    469 	(void) lfs_chkdq(ip, -change, cred, 0);
    470 	(void) lfs_chkiq(ip, -1, cred, 0);
    471 #endif
    472 	ip->i_gid = gid;
    473 	DIP_ASSIGN(ip, gid, gid);
    474 	ip->i_uid = uid;
    475 	DIP_ASSIGN(ip, uid, uid);
    476 #if defined(LFS_QUOTA) || defined(LFS_QUOTA2)
    477 	if ((error = lfs_chkdq(ip, change, cred, 0)) == 0) {
    478 		if ((error = lfs_chkiq(ip, 1, cred, 0)) == 0)
    479 			goto good;
    480 		else
    481 			(void) lfs_chkdq(ip, -change, cred, FORCE);
    482 	}
    483 	ip->i_gid = ogid;
    484 	DIP_ASSIGN(ip, gid, ogid);
    485 	ip->i_uid = ouid;
    486 	DIP_ASSIGN(ip, uid, ouid);
    487 	(void) lfs_chkdq(ip, change, cred, FORCE);
    488 	(void) lfs_chkiq(ip, 1, cred, FORCE);
    489 	fstrans_done(vp->v_mount);
    490 	return (error);
    491  good:
    492 #endif /* LFS_QUOTA || LFS_QUOTA2 */
    493 	ip->i_flag |= IN_CHANGE;
    494 	fstrans_done(vp->v_mount);
    495 	return (0);
    496 }
    497 
    498 int
    499 ulfs_remove(void *v)
    500 {
    501 	struct vop_remove_args /* {
    502 		struct vnode		*a_dvp;
    503 		struct vnode		*a_vp;
    504 		struct componentname	*a_cnp;
    505 	} */ *ap = v;
    506 	struct vnode	*vp, *dvp;
    507 	struct inode	*ip;
    508 	struct mount	*mp;
    509 	int		error;
    510 	struct ulfs_lookup_results *ulr;
    511 
    512 	vp = ap->a_vp;
    513 	dvp = ap->a_dvp;
    514 	ip = VTOI(vp);
    515 	mp = dvp->v_mount;
    516 	KASSERT(mp == vp->v_mount); /* XXX Not stable without lock.  */
    517 
    518 	/* XXX should handle this material another way */
    519 	ulr = &VTOI(dvp)->i_crap;
    520 	ULFS_CHECK_CRAPCOUNTER(VTOI(dvp));
    521 
    522 	fstrans_start(mp, FSTRANS_SHARED);
    523 	if (vp->v_type == VDIR || (ip->i_flags & (IMMUTABLE | APPEND)) ||
    524 	    (VTOI(dvp)->i_flags & APPEND))
    525 		error = EPERM;
    526 	else {
    527 		error = ulfs_dirremove(dvp, ulr,
    528 				      ip, ap->a_cnp->cn_flags, 0);
    529 	}
    530 	VN_KNOTE(vp, NOTE_DELETE);
    531 	VN_KNOTE(dvp, NOTE_WRITE);
    532 	if (dvp == vp)
    533 		vrele(vp);
    534 	else
    535 		vput(vp);
    536 	vput(dvp);
    537 	fstrans_done(mp);
    538 	return (error);
    539 }
    540 
    541 /*
    542  * ulfs_link: create hard link.
    543  */
    544 int
    545 ulfs_link(void *v)
    546 {
    547 	struct vop_link_v2_args /* {
    548 		struct vnode *a_dvp;
    549 		struct vnode *a_vp;
    550 		struct componentname *a_cnp;
    551 	} */ *ap = v;
    552 	struct vnode *dvp = ap->a_dvp;
    553 	struct vnode *vp = ap->a_vp;
    554 	struct componentname *cnp = ap->a_cnp;
    555 	struct mount *mp = dvp->v_mount;
    556 	struct inode *ip;
    557 	struct lfs_direct *newdir;
    558 	int error;
    559 	struct ulfs_lookup_results *ulr;
    560 
    561 	KASSERT(dvp != vp);
    562 	KASSERT(vp->v_type != VDIR);
    563 	KASSERT(mp == vp->v_mount); /* XXX Not stable without lock.  */
    564 
    565 	/* XXX should handle this material another way */
    566 	ulr = &VTOI(dvp)->i_crap;
    567 	ULFS_CHECK_CRAPCOUNTER(VTOI(dvp));
    568 
    569 	fstrans_start(mp, FSTRANS_SHARED);
    570 	error = vn_lock(vp, LK_EXCLUSIVE);
    571 	if (error) {
    572 		VOP_ABORTOP(dvp, cnp);
    573 		goto out2;
    574 	}
    575 	ip = VTOI(vp);
    576 	if ((nlink_t)ip->i_nlink >= LINK_MAX) {
    577 		VOP_ABORTOP(dvp, cnp);
    578 		error = EMLINK;
    579 		goto out1;
    580 	}
    581 	if (ip->i_flags & (IMMUTABLE | APPEND)) {
    582 		VOP_ABORTOP(dvp, cnp);
    583 		error = EPERM;
    584 		goto out1;
    585 	}
    586 	ip->i_nlink++;
    587 	DIP_ASSIGN(ip, nlink, ip->i_nlink);
    588 	ip->i_flag |= IN_CHANGE;
    589 	error = lfs_update(vp, NULL, NULL, UPDATE_DIROP);
    590 	if (!error) {
    591 		newdir = pool_cache_get(ulfs_direct_cache, PR_WAITOK);
    592 		ulfs_makedirentry(ip, cnp, newdir);
    593 		error = ulfs_direnter(dvp, ulr, vp, newdir, cnp, NULL);
    594 		pool_cache_put(ulfs_direct_cache, newdir);
    595 	}
    596 	if (error) {
    597 		ip->i_nlink--;
    598 		DIP_ASSIGN(ip, nlink, ip->i_nlink);
    599 		ip->i_flag |= IN_CHANGE;
    600 	}
    601  out1:
    602 	VOP_UNLOCK(vp);
    603  out2:
    604 	VN_KNOTE(vp, NOTE_LINK);
    605 	VN_KNOTE(dvp, NOTE_WRITE);
    606 	fstrans_done(mp);
    607 	return (error);
    608 }
    609 
    610 /*
    611  * whiteout vnode call
    612  */
    613 int
    614 ulfs_whiteout(void *v)
    615 {
    616 	struct vop_whiteout_args /* {
    617 		struct vnode		*a_dvp;
    618 		struct componentname	*a_cnp;
    619 		int			a_flags;
    620 	} */ *ap = v;
    621 	struct vnode		*dvp = ap->a_dvp;
    622 	struct componentname	*cnp = ap->a_cnp;
    623 	struct lfs_direct		*newdir;
    624 	int			error;
    625 	struct ulfsmount	*ump = VFSTOULFS(dvp->v_mount);
    626 	struct lfs *fs = ump->um_lfs;
    627 	struct ulfs_lookup_results *ulr;
    628 
    629 	/* XXX should handle this material another way */
    630 	ulr = &VTOI(dvp)->i_crap;
    631 	ULFS_CHECK_CRAPCOUNTER(VTOI(dvp));
    632 
    633 	error = 0;
    634 	switch (ap->a_flags) {
    635 	case LOOKUP:
    636 		/* 4.4 format directories support whiteout operations */
    637 		if (fs->um_maxsymlinklen > 0)
    638 			return (0);
    639 		return (EOPNOTSUPP);
    640 
    641 	case CREATE:
    642 		/* create a new directory whiteout */
    643 		fstrans_start(dvp->v_mount, FSTRANS_SHARED);
    644 #ifdef DIAGNOSTIC
    645 		if (fs->um_maxsymlinklen <= 0)
    646 			panic("ulfs_whiteout: old format filesystem");
    647 #endif
    648 
    649 		newdir = pool_cache_get(ulfs_direct_cache, PR_WAITOK);
    650 		ulfs_makedirentry_bytype(fs, cnp, ULFS_WINO, LFS_DT_WHT,
    651 					 newdir);
    652 		error = ulfs_direnter(dvp, ulr, NULL, newdir, cnp, NULL);
    653 		pool_cache_put(ulfs_direct_cache, newdir);
    654 		break;
    655 
    656 	case DELETE:
    657 		/* remove an existing directory whiteout */
    658 		fstrans_start(dvp->v_mount, FSTRANS_SHARED);
    659 #ifdef DIAGNOSTIC
    660 		if (fs->um_maxsymlinklen <= 0)
    661 			panic("ulfs_whiteout: old format filesystem");
    662 #endif
    663 
    664 		cnp->cn_flags &= ~DOWHITEOUT;
    665 		error = ulfs_dirremove(dvp, ulr, NULL, cnp->cn_flags, 0);
    666 		break;
    667 	default:
    668 		panic("ulfs_whiteout: unknown op");
    669 		/* NOTREACHED */
    670 	}
    671 	fstrans_done(dvp->v_mount);
    672 	return (error);
    673 }
    674 
    675 int
    676 ulfs_rmdir(void *v)
    677 {
    678 	struct vop_rmdir_args /* {
    679 		struct vnode		*a_dvp;
    680 		struct vnode		*a_vp;
    681 		struct componentname	*a_cnp;
    682 	} */ *ap = v;
    683 	struct vnode		*vp, *dvp;
    684 	struct componentname	*cnp;
    685 	struct inode		*ip, *dp;
    686 	int			error;
    687 	struct ulfs_lookup_results *ulr;
    688 
    689 	vp = ap->a_vp;
    690 	dvp = ap->a_dvp;
    691 	cnp = ap->a_cnp;
    692 	ip = VTOI(vp);
    693 	dp = VTOI(dvp);
    694 
    695 	/* XXX should handle this material another way */
    696 	ulr = &dp->i_crap;
    697 	ULFS_CHECK_CRAPCOUNTER(dp);
    698 
    699 	/*
    700 	 * No rmdir "." or of mounted directories please.
    701 	 */
    702 	if (dp == ip || vp->v_mountedhere != NULL) {
    703 		if (dp == ip)
    704 			vrele(dvp);
    705 		else
    706 			vput(dvp);
    707 		vput(vp);
    708 		return (EINVAL);
    709 	}
    710 
    711 	fstrans_start(dvp->v_mount, FSTRANS_SHARED);
    712 
    713 	/*
    714 	 * Do not remove a directory that is in the process of being renamed.
    715 	 * Verify that the directory is empty (and valid). (Rmdir ".." won't
    716 	 * be valid since ".." will contain a reference to the current
    717 	 * directory and thus be non-empty.)
    718 	 */
    719 	error = 0;
    720 	if (ip->i_nlink != 2 ||
    721 	    !ulfs_dirempty(ip, dp->i_number, cnp->cn_cred)) {
    722 		error = ENOTEMPTY;
    723 		goto out;
    724 	}
    725 	if ((dp->i_flags & APPEND) ||
    726 		(ip->i_flags & (IMMUTABLE | APPEND))) {
    727 		error = EPERM;
    728 		goto out;
    729 	}
    730 	/*
    731 	 * Delete reference to directory before purging
    732 	 * inode.  If we crash in between, the directory
    733 	 * will be reattached to lost+found,
    734 	 */
    735 	error = ulfs_dirremove(dvp, ulr, ip, cnp->cn_flags, 1);
    736 	if (error) {
    737 		goto out;
    738 	}
    739 	VN_KNOTE(dvp, NOTE_WRITE | NOTE_LINK);
    740 	cache_purge(dvp);
    741 	/*
    742 	 * Truncate inode.  The only stuff left in the directory is "." and
    743 	 * "..".  The "." reference is inconsequential since we're quashing
    744 	 * it.
    745 	 */
    746 	dp->i_nlink--;
    747 	DIP_ASSIGN(dp, nlink, dp->i_nlink);
    748 	dp->i_flag |= IN_CHANGE;
    749 	ip->i_nlink--;
    750 	DIP_ASSIGN(ip, nlink, ip->i_nlink);
    751 	ip->i_flag |= IN_CHANGE;
    752 	error = lfs_truncate(vp, (off_t)0, IO_SYNC, cnp->cn_cred);
    753 	cache_purge(vp);
    754 #ifdef LFS_DIRHASH
    755 	if (ip->i_dirhash != NULL)
    756 		ulfsdirhash_free(ip);
    757 #endif
    758  out:
    759 	VN_KNOTE(vp, NOTE_DELETE);
    760 	vput(vp);
    761 	fstrans_done(dvp->v_mount);
    762 	vput(dvp);
    763 	return (error);
    764 }
    765 
    766 /*
    767  * Vnode op for reading directories.
    768  *
    769  * This routine handles converting from the on-disk directory format
    770  * "struct lfs_direct" to the in-memory format "struct dirent" as well as
    771  * byte swapping the entries if necessary.
    772  */
    773 int
    774 ulfs_readdir(void *v)
    775 {
    776 	struct vop_readdir_args /* {
    777 		struct vnode	*a_vp;
    778 		struct uio	*a_uio;
    779 		kauth_cred_t	a_cred;
    780 		int		*a_eofflag;
    781 		off_t		**a_cookies;
    782 		int		*ncookies;
    783 	} */ *ap = v;
    784 	struct vnode	*vp = ap->a_vp;
    785 	struct lfs_direct	*cdp, *ecdp;
    786 	struct dirent	*ndp;
    787 	char		*cdbuf, *ndbuf, *endp;
    788 	struct uio	auio, *uio;
    789 	struct iovec	aiov;
    790 	int		error;
    791 	size_t		count, ccount, rcount, cdbufsz, ndbufsz;
    792 	off_t		off, *ccp;
    793 	off_t		startoff;
    794 	size_t		skipbytes;
    795 	struct ulfsmount *ump = VFSTOULFS(vp->v_mount);
    796 	struct lfs *fs = ump->um_lfs;
    797 	uio = ap->a_uio;
    798 	count = uio->uio_resid;
    799 	rcount = count - ((uio->uio_offset + count) & (fs->um_dirblksiz - 1));
    800 
    801 	if (rcount < LFS_DIRECTSIZ(0) || count < _DIRENT_MINSIZE(ndp))
    802 		return EINVAL;
    803 
    804 	startoff = uio->uio_offset & ~(fs->um_dirblksiz - 1);
    805 	skipbytes = uio->uio_offset - startoff;
    806 	rcount += skipbytes;
    807 
    808 	auio.uio_iov = &aiov;
    809 	auio.uio_iovcnt = 1;
    810 	auio.uio_offset = startoff;
    811 	auio.uio_resid = rcount;
    812 	UIO_SETUP_SYSSPACE(&auio);
    813 	auio.uio_rw = UIO_READ;
    814 	cdbufsz = rcount;
    815 	cdbuf = kmem_alloc(cdbufsz, KM_SLEEP);
    816 	aiov.iov_base = cdbuf;
    817 	aiov.iov_len = rcount;
    818 	error = VOP_READ(vp, &auio, 0, ap->a_cred);
    819 	if (error != 0) {
    820 		kmem_free(cdbuf, cdbufsz);
    821 		return error;
    822 	}
    823 
    824 	rcount -= auio.uio_resid;
    825 
    826 	cdp = (struct lfs_direct *)(void *)cdbuf;
    827 	ecdp = (struct lfs_direct *)(void *)&cdbuf[rcount];
    828 
    829 	ndbufsz = count;
    830 	ndbuf = kmem_alloc(ndbufsz, KM_SLEEP);
    831 	ndp = (struct dirent *)(void *)ndbuf;
    832 	endp = &ndbuf[count];
    833 
    834 	off = uio->uio_offset;
    835 	if (ap->a_cookies) {
    836 		ccount = rcount / _DIRENT_RECLEN(ndp, 1);
    837 		ccp = *(ap->a_cookies) = malloc(ccount * sizeof(*ccp),
    838 		    M_TEMP, M_WAITOK);
    839 	} else {
    840 		/* XXX: GCC */
    841 		ccount = 0;
    842 		ccp = NULL;
    843 	}
    844 
    845 	while (cdp < ecdp) {
    846 		if (skipbytes > 0) {
    847 			if (lfs_dir_getreclen(fs, cdp) <= skipbytes) {
    848 				skipbytes -= lfs_dir_getreclen(fs, cdp);
    849 				cdp = LFS_NEXTDIR(fs, cdp);
    850 				continue;
    851 			}
    852 			/*
    853 			 * invalid cookie.
    854 			 */
    855 			error = EINVAL;
    856 			goto out;
    857 		}
    858 		if (lfs_dir_getreclen(fs, cdp) == 0) {
    859 			struct dirent *ondp = ndp;
    860 			ndp->d_reclen = _DIRENT_MINSIZE(ndp);
    861 			ndp = _DIRENT_NEXT(ndp);
    862 			ondp->d_reclen = 0;
    863 			cdp = ecdp;
    864 			break;
    865 		}
    866 		ndp->d_type = lfs_dir_gettype(fs, cdp);
    867 		ndp->d_namlen = lfs_dir_getnamlen(fs, cdp);
    868 		ndp->d_reclen = _DIRENT_RECLEN(ndp, ndp->d_namlen);
    869 		if ((char *)(void *)ndp + ndp->d_reclen +
    870 		    _DIRENT_MINSIZE(ndp) > endp)
    871 			break;
    872 		ndp->d_fileno = lfs_dir_getino(fs, cdp);
    873 		(void)memcpy(ndp->d_name, cdp->d_name, ndp->d_namlen);
    874 		memset(&ndp->d_name[ndp->d_namlen], 0,
    875 		    ndp->d_reclen - _DIRENT_NAMEOFF(ndp) - ndp->d_namlen);
    876 		off += lfs_dir_getreclen(fs, cdp);
    877 		if (ap->a_cookies) {
    878 			KASSERT(ccp - *(ap->a_cookies) < ccount);
    879 			*(ccp++) = off;
    880 		}
    881 		ndp = _DIRENT_NEXT(ndp);
    882 		cdp = LFS_NEXTDIR(fs, cdp);
    883 	}
    884 
    885 	count = ((char *)(void *)ndp - ndbuf);
    886 	error = uiomove(ndbuf, count, uio);
    887 out:
    888 	if (ap->a_cookies) {
    889 		if (error) {
    890 			free(*(ap->a_cookies), M_TEMP);
    891 			*(ap->a_cookies) = NULL;
    892 			*(ap->a_ncookies) = 0;
    893 		} else {
    894 			*ap->a_ncookies = ccp - *(ap->a_cookies);
    895 		}
    896 	}
    897 	uio->uio_offset = off;
    898 	kmem_free(ndbuf, ndbufsz);
    899 	kmem_free(cdbuf, cdbufsz);
    900 	*ap->a_eofflag = VTOI(vp)->i_size <= uio->uio_offset;
    901 	return error;
    902 }
    903 
    904 /*
    905  * Return target name of a symbolic link
    906  */
    907 int
    908 ulfs_readlink(void *v)
    909 {
    910 	struct vop_readlink_args /* {
    911 		struct vnode	*a_vp;
    912 		struct uio	*a_uio;
    913 		kauth_cred_t	a_cred;
    914 	} */ *ap = v;
    915 	struct vnode	*vp = ap->a_vp;
    916 	struct inode	*ip = VTOI(vp);
    917 	struct ulfsmount *ump = VFSTOULFS(vp->v_mount);
    918 	struct lfs *fs = ump->um_lfs;
    919 	int		isize;
    920 
    921 	isize = ip->i_size;
    922 	if (isize < fs->um_maxsymlinklen ||
    923 	    (fs->um_maxsymlinklen == 0 && DIP(ip, blocks) == 0)) {
    924 		uiomove((char *)SHORTLINK(ip), isize, ap->a_uio);
    925 		return (0);
    926 	}
    927 	return (lfs_bufrd(vp, ap->a_uio, 0, ap->a_cred));
    928 }
    929 
    930 /*
    931  * Print out the contents of an inode.
    932  */
    933 int
    934 ulfs_print(void *v)
    935 {
    936 	struct vop_print_args /* {
    937 		struct vnode	*a_vp;
    938 	} */ *ap = v;
    939 	struct vnode	*vp;
    940 	struct inode	*ip;
    941 
    942 	vp = ap->a_vp;
    943 	ip = VTOI(vp);
    944 	printf("tag VT_ULFS, ino %llu, on dev %llu, %llu",
    945 	    (unsigned long long)ip->i_number,
    946 	    (unsigned long long)major(ip->i_dev),
    947 	    (unsigned long long)minor(ip->i_dev));
    948 	printf(" flags 0x%x, nlink %d\n",
    949 	    ip->i_flag, ip->i_nlink);
    950 	printf("\tmode 0%o, owner %d, group %d, size %qd",
    951 	    ip->i_mode, ip->i_uid, ip->i_gid,
    952 	    (long long)ip->i_size);
    953 	if (vp->v_type == VFIFO)
    954 		VOCALL(fifo_vnodeop_p, VOFFSET(vop_print), v);
    955 	printf("\n");
    956 	return (0);
    957 }
    958 
    959 /*
    960  * Read wrapper for special devices.
    961  */
    962 int
    963 ulfsspec_read(void *v)
    964 {
    965 	struct vop_read_args /* {
    966 		struct vnode	*a_vp;
    967 		struct uio	*a_uio;
    968 		int		a_ioflag;
    969 		kauth_cred_t	a_cred;
    970 	} */ *ap = v;
    971 
    972 	/*
    973 	 * Set access flag.
    974 	 */
    975 	if ((ap->a_vp->v_mount->mnt_flag & MNT_NODEVMTIME) == 0)
    976 		VTOI(ap->a_vp)->i_flag |= IN_ACCESS;
    977 	return (VOCALL (spec_vnodeop_p, VOFFSET(vop_read), ap));
    978 }
    979 
    980 /*
    981  * Write wrapper for special devices.
    982  */
    983 int
    984 ulfsspec_write(void *v)
    985 {
    986 	struct vop_write_args /* {
    987 		struct vnode	*a_vp;
    988 		struct uio	*a_uio;
    989 		int		a_ioflag;
    990 		kauth_cred_t	a_cred;
    991 	} */ *ap = v;
    992 
    993 	/*
    994 	 * Set update and change flags.
    995 	 */
    996 	if ((ap->a_vp->v_mount->mnt_flag & MNT_NODEVMTIME) == 0)
    997 		VTOI(ap->a_vp)->i_flag |= IN_MODIFY;
    998 	return (VOCALL (spec_vnodeop_p, VOFFSET(vop_write), ap));
    999 }
   1000 
   1001 /*
   1002  * Read wrapper for fifo's
   1003  */
   1004 int
   1005 ulfsfifo_read(void *v)
   1006 {
   1007 	struct vop_read_args /* {
   1008 		struct vnode	*a_vp;
   1009 		struct uio	*a_uio;
   1010 		int		a_ioflag;
   1011 		kauth_cred_t	a_cred;
   1012 	} */ *ap = v;
   1013 
   1014 	/*
   1015 	 * Set access flag.
   1016 	 */
   1017 	VTOI(ap->a_vp)->i_flag |= IN_ACCESS;
   1018 	return (VOCALL (fifo_vnodeop_p, VOFFSET(vop_read), ap));
   1019 }
   1020 
   1021 /*
   1022  * Write wrapper for fifo's.
   1023  */
   1024 int
   1025 ulfsfifo_write(void *v)
   1026 {
   1027 	struct vop_write_args /* {
   1028 		struct vnode	*a_vp;
   1029 		struct uio	*a_uio;
   1030 		int		a_ioflag;
   1031 		kauth_cred_t	a_cred;
   1032 	} */ *ap = v;
   1033 
   1034 	/*
   1035 	 * Set update and change flags.
   1036 	 */
   1037 	VTOI(ap->a_vp)->i_flag |= IN_MODIFY;
   1038 	return (VOCALL (fifo_vnodeop_p, VOFFSET(vop_write), ap));
   1039 }
   1040 
   1041 /*
   1042  * Return POSIX pathconf information applicable to ulfs filesystems.
   1043  */
   1044 int
   1045 ulfs_pathconf(void *v)
   1046 {
   1047 	struct vop_pathconf_args /* {
   1048 		struct vnode	*a_vp;
   1049 		int		a_name;
   1050 		register_t	*a_retval;
   1051 	} */ *ap = v;
   1052 
   1053 	switch (ap->a_name) {
   1054 	case _PC_LINK_MAX:
   1055 		*ap->a_retval = LINK_MAX;
   1056 		return (0);
   1057 	case _PC_NAME_MAX:
   1058 		*ap->a_retval = LFS_MAXNAMLEN;
   1059 		return (0);
   1060 	case _PC_PATH_MAX:
   1061 		*ap->a_retval = PATH_MAX;
   1062 		return (0);
   1063 	case _PC_PIPE_BUF:
   1064 		*ap->a_retval = PIPE_BUF;
   1065 		return (0);
   1066 	case _PC_CHOWN_RESTRICTED:
   1067 		*ap->a_retval = 1;
   1068 		return (0);
   1069 	case _PC_NO_TRUNC:
   1070 		*ap->a_retval = 1;
   1071 		return (0);
   1072 	case _PC_SYNC_IO:
   1073 		*ap->a_retval = 1;
   1074 		return (0);
   1075 	case _PC_FILESIZEBITS:
   1076 		*ap->a_retval = 42;
   1077 		return (0);
   1078 	case _PC_SYMLINK_MAX:
   1079 		*ap->a_retval = MAXPATHLEN;
   1080 		return (0);
   1081 	case _PC_2_SYMLINKS:
   1082 		*ap->a_retval = 1;
   1083 		return (0);
   1084 	default:
   1085 		return (EINVAL);
   1086 	}
   1087 	/* NOTREACHED */
   1088 }
   1089 
   1090 /*
   1091  * Advisory record locking support
   1092  */
   1093 int
   1094 ulfs_advlock(void *v)
   1095 {
   1096 	struct vop_advlock_args /* {
   1097 		struct vnode	*a_vp;
   1098 		void *		a_id;
   1099 		int		a_op;
   1100 		struct flock	*a_fl;
   1101 		int		a_flags;
   1102 	} */ *ap = v;
   1103 	struct inode *ip;
   1104 
   1105 	ip = VTOI(ap->a_vp);
   1106 	return lf_advlock(ap, &ip->i_lockf, ip->i_size);
   1107 }
   1108 
   1109 /*
   1110  * Initialize the vnode associated with a new inode, handle aliased
   1111  * vnodes.
   1112  */
   1113 void
   1114 ulfs_vinit(struct mount *mntp, int (**specops)(void *), int (**fifoops)(void *),
   1115 	struct vnode **vpp)
   1116 {
   1117 	struct timeval	tv;
   1118 	struct inode	*ip;
   1119 	struct vnode	*vp;
   1120 	dev_t		rdev;
   1121 	struct ulfsmount *ump;
   1122 
   1123 	vp = *vpp;
   1124 	ip = VTOI(vp);
   1125 	switch(vp->v_type = IFTOVT(ip->i_mode)) {
   1126 	case VCHR:
   1127 	case VBLK:
   1128 		vp->v_op = specops;
   1129 		ump = ip->i_ump;
   1130 		// XXX clean this up
   1131 		if (ump->um_fstype == ULFS1)
   1132 			rdev = (dev_t)ulfs_rw32(ip->i_din->u_32.di_rdev,
   1133 			    ULFS_MPNEEDSWAP(ump->um_lfs));
   1134 		else
   1135 			rdev = (dev_t)ulfs_rw64(ip->i_din->u_64.di_rdev,
   1136 			    ULFS_MPNEEDSWAP(ump->um_lfs));
   1137 		spec_node_init(vp, rdev);
   1138 		break;
   1139 	case VFIFO:
   1140 		vp->v_op = fifoops;
   1141 		break;
   1142 	case VNON:
   1143 	case VBAD:
   1144 	case VSOCK:
   1145 	case VLNK:
   1146 	case VDIR:
   1147 	case VREG:
   1148 		break;
   1149 	}
   1150 	if (ip->i_number == ULFS_ROOTINO)
   1151                 vp->v_vflag |= VV_ROOT;
   1152 	/*
   1153 	 * Initialize modrev times
   1154 	 */
   1155 	getmicrouptime(&tv);
   1156 	ip->i_modrev = (uint64_t)(uint)tv.tv_sec << 32
   1157 			| tv.tv_usec * 4294u;
   1158 	*vpp = vp;
   1159 }
   1160 
   1161 /*
   1162  * Allocate a new inode.
   1163  */
   1164 int
   1165 ulfs_makeinode(struct vattr *vap, struct vnode *dvp,
   1166 	const struct ulfs_lookup_results *ulr,
   1167 	struct vnode **vpp, struct componentname *cnp)
   1168 {
   1169 	struct inode	*ip;
   1170 	struct lfs_direct	*newdir;
   1171 	struct vnode	*tvp;
   1172 	int		error;
   1173 
   1174 	error = vcache_new(dvp->v_mount, dvp, vap, cnp->cn_cred, &tvp);
   1175 	if (error)
   1176 		return error;
   1177 	error = vn_lock(tvp, LK_EXCLUSIVE);
   1178 	if (error) {
   1179 		vrele(tvp);
   1180 		return error;
   1181 	}
   1182 	lfs_mark_vnode(tvp);
   1183 	*vpp = tvp;
   1184 	ip = VTOI(tvp);
   1185 	ip->i_flag |= IN_ACCESS | IN_CHANGE | IN_UPDATE;
   1186 	ip->i_nlink = 1;
   1187 	DIP_ASSIGN(ip, nlink, 1);
   1188 
   1189 	/* Authorize setting SGID if needed. */
   1190 	if (ip->i_mode & ISGID) {
   1191 		error = kauth_authorize_vnode(cnp->cn_cred, KAUTH_VNODE_WRITE_SECURITY,
   1192 		    tvp, NULL, genfs_can_chmod(tvp->v_type, cnp->cn_cred, ip->i_uid,
   1193 		    ip->i_gid, MAKEIMODE(vap->va_type, vap->va_mode)));
   1194 		if (error) {
   1195 			ip->i_mode &= ~ISGID;
   1196 			DIP_ASSIGN(ip, mode, ip->i_mode);
   1197 		}
   1198 	}
   1199 
   1200 	if (cnp->cn_flags & ISWHITEOUT) {
   1201 		ip->i_flags |= UF_OPAQUE;
   1202 		DIP_ASSIGN(ip, flags, ip->i_flags);
   1203 	}
   1204 
   1205 	/*
   1206 	 * Make sure inode goes to disk before directory entry.
   1207 	 */
   1208 	if ((error = lfs_update(tvp, NULL, NULL, UPDATE_DIROP)) != 0)
   1209 		goto bad;
   1210 	newdir = pool_cache_get(ulfs_direct_cache, PR_WAITOK);
   1211 	ulfs_makedirentry(ip, cnp, newdir);
   1212 	error = ulfs_direnter(dvp, ulr, tvp, newdir, cnp, NULL);
   1213 	pool_cache_put(ulfs_direct_cache, newdir);
   1214 	if (error)
   1215 		goto bad;
   1216 	*vpp = tvp;
   1217 	return (0);
   1218 
   1219  bad:
   1220 	/*
   1221 	 * Write error occurred trying to update the inode
   1222 	 * or the directory so must deallocate the inode.
   1223 	 */
   1224 	ip->i_nlink = 0;
   1225 	DIP_ASSIGN(ip, nlink, 0);
   1226 	ip->i_flag |= IN_CHANGE;
   1227 	/* If IN_ADIROP, account for it */
   1228 	lfs_unmark_vnode(tvp);
   1229 	vput(tvp);
   1230 	return (error);
   1231 }
   1232 
   1233 /*
   1234  * Allocate len bytes at offset off.
   1235  */
   1236 int
   1237 ulfs_gop_alloc(struct vnode *vp, off_t off, off_t len, int flags,
   1238     kauth_cred_t cred)
   1239 {
   1240         struct inode *ip = VTOI(vp);
   1241         int error, delta, bshift, bsize;
   1242         UVMHIST_FUNC("ulfs_gop_alloc"); UVMHIST_CALLED(ubchist);
   1243 
   1244         error = 0;
   1245         bshift = vp->v_mount->mnt_fs_bshift;
   1246         bsize = 1 << bshift;
   1247 
   1248         delta = off & (bsize - 1);
   1249         off -= delta;
   1250         len += delta;
   1251 
   1252         while (len > 0) {
   1253                 bsize = MIN(bsize, len);
   1254 
   1255                 error = lfs_balloc(vp, off, bsize, cred, flags, NULL);
   1256                 if (error) {
   1257                         goto out;
   1258                 }
   1259 
   1260                 /*
   1261                  * increase file size now, lfs_balloc() requires that
   1262                  * EOF be up-to-date before each call.
   1263                  */
   1264 
   1265                 if (ip->i_size < off + bsize) {
   1266                         UVMHIST_LOG(ubchist, "vp %p old 0x%x new 0x%x",
   1267                             vp, ip->i_size, off + bsize, 0);
   1268                         ip->i_size = off + bsize;
   1269 			DIP_ASSIGN(ip, size, ip->i_size);
   1270                 }
   1271 
   1272                 off += bsize;
   1273                 len -= bsize;
   1274         }
   1275 
   1276 out:
   1277 	return error;
   1278 }
   1279 
   1280 void
   1281 ulfs_gop_markupdate(struct vnode *vp, int flags)
   1282 {
   1283 	u_int32_t mask = 0;
   1284 
   1285 	if ((flags & GOP_UPDATE_ACCESSED) != 0) {
   1286 		mask = IN_ACCESS;
   1287 	}
   1288 	if ((flags & GOP_UPDATE_MODIFIED) != 0) {
   1289 		if (vp->v_type == VREG) {
   1290 			mask |= IN_CHANGE | IN_UPDATE;
   1291 		} else {
   1292 			mask |= IN_MODIFY;
   1293 		}
   1294 	}
   1295 	if (mask) {
   1296 		struct inode *ip = VTOI(vp);
   1297 
   1298 		ip->i_flag |= mask;
   1299 	}
   1300 }
   1301 
   1302 int
   1303 ulfs_bufio(enum uio_rw rw, struct vnode *vp, void *buf, size_t len, off_t off,
   1304     int ioflg, kauth_cred_t cred, size_t *aresid, struct lwp *l)
   1305 {
   1306 	struct iovec iov;
   1307 	struct uio uio;
   1308 	int error;
   1309 
   1310 	KASSERT(ISSET(ioflg, IO_NODELOCKED));
   1311 	KASSERT(VOP_ISLOCKED(vp));
   1312 	KASSERT(rw != UIO_WRITE || VOP_ISLOCKED(vp) == LK_EXCLUSIVE);
   1313 
   1314 	iov.iov_base = buf;
   1315 	iov.iov_len = len;
   1316 	uio.uio_iov = &iov;
   1317 	uio.uio_iovcnt = 1;
   1318 	uio.uio_resid = len;
   1319 	uio.uio_offset = off;
   1320 	uio.uio_rw = rw;
   1321 	UIO_SETUP_SYSSPACE(&uio);
   1322 
   1323 	switch (rw) {
   1324 	case UIO_READ:
   1325 		error = lfs_bufrd(vp, &uio, ioflg, cred);
   1326 		break;
   1327 	case UIO_WRITE:
   1328 		error = lfs_bufwr(vp, &uio, ioflg, cred);
   1329 		break;
   1330 	default:
   1331 		panic("invalid uio rw: %d", (int)rw);
   1332 	}
   1333 
   1334 	if (aresid)
   1335 		*aresid = uio.uio_resid;
   1336 	else if (uio.uio_resid && error == 0)
   1337 		error = EIO;
   1338 
   1339 	KASSERT(VOP_ISLOCKED(vp));
   1340 	KASSERT(rw != UIO_WRITE || VOP_ISLOCKED(vp) == LK_EXCLUSIVE);
   1341 	return error;
   1342 }
   1343