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