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