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