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