Home | History | Annotate | Line # | Download | only in lfs
ulfs_vnops.c revision 1.15
      1 /*	$NetBSD: ulfs_vnops.c,v 1.15 2013/07/21 00:01:22 dholland Exp $	*/
      2 /*  from NetBSD: ufs_vnops.c,v 1.213 2013/06/08 05:47:02 kardel Exp  */
      3 
      4 /*-
      5  * Copyright (c) 2008 The NetBSD Foundation, Inc.
      6  * All rights reserved.
      7  *
      8  * This code is derived from software contributed to The NetBSD Foundation
      9  * by Wasabi Systems, Inc.
     10  *
     11  * Redistribution and use in source and binary forms, with or without
     12  * modification, are permitted provided that the following conditions
     13  * are met:
     14  * 1. Redistributions of source code must retain the above copyright
     15  *    notice, this list of conditions and the following disclaimer.
     16  * 2. Redistributions in binary form must reproduce the above copyright
     17  *    notice, this list of conditions and the following disclaimer in the
     18  *    documentation and/or other materials provided with the distribution.
     19  *
     20  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     22  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     23  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     24  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     30  * POSSIBILITY OF SUCH DAMAGE.
     31  */
     32 
     33 /*
     34  * Copyright (c) 1982, 1986, 1989, 1993, 1995
     35  *	The Regents of the University of California.  All rights reserved.
     36  * (c) UNIX System Laboratories, Inc.
     37  * All or some portions of this file are derived from material licensed
     38  * to the University of California by American Telephone and Telegraph
     39  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
     40  * the permission of UNIX System Laboratories, Inc.
     41  *
     42  * Redistribution and use in source and binary forms, with or without
     43  * modification, are permitted provided that the following conditions
     44  * are met:
     45  * 1. Redistributions of source code must retain the above copyright
     46  *    notice, this list of conditions and the following disclaimer.
     47  * 2. Redistributions in binary form must reproduce the above copyright
     48  *    notice, this list of conditions and the following disclaimer in the
     49  *    documentation and/or other materials provided with the distribution.
     50  * 3. Neither the name of the University nor the names of its contributors
     51  *    may be used to endorse or promote products derived from this software
     52  *    without specific prior written permission.
     53  *
     54  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     55  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     56  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     57  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     58  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     59  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     60  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     61  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     62  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     63  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     64  * SUCH DAMAGE.
     65  *
     66  *	@(#)ufs_vnops.c	8.28 (Berkeley) 7/31/95
     67  */
     68 
     69 #include <sys/cdefs.h>
     70 __KERNEL_RCSID(0, "$NetBSD: ulfs_vnops.c,v 1.15 2013/07/21 00:01:22 dholland Exp $");
     71 
     72 #if defined(_KERNEL_OPT)
     73 #include "opt_lfs.h"
     74 #include "opt_quota.h"
     75 #endif
     76 
     77 #include <sys/param.h>
     78 #include <sys/systm.h>
     79 #include <sys/namei.h>
     80 #include <sys/resourcevar.h>
     81 #include <sys/kernel.h>
     82 #include <sys/file.h>
     83 #include <sys/stat.h>
     84 #include <sys/buf.h>
     85 #include <sys/proc.h>
     86 #include <sys/mount.h>
     87 #include <sys/vnode.h>
     88 #include <sys/kmem.h>
     89 #include <sys/malloc.h>
     90 #include <sys/dirent.h>
     91 #include <sys/lockf.h>
     92 #include <sys/kauth.h>
     93 #include <sys/wapbl.h>
     94 #include <sys/fstrans.h>
     95 
     96 #include <miscfs/specfs/specdev.h>
     97 #include <miscfs/fifofs/fifo.h>
     98 #include <miscfs/genfs/genfs.h>
     99 
    100 #include <ufs/lfs/ulfs_inode.h>
    101 #include <ufs/lfs/ulfsmount.h>
    102 #include <ufs/lfs/ulfs_bswap.h>
    103 #include <ufs/lfs/ulfs_extern.h>
    104 #ifdef LFS_DIRHASH
    105 #include <ufs/lfs/ulfs_dirhash.h>
    106 #endif
    107 #include <ufs/lfs/lfs_extern.h>
    108 #include <ufs/lfs/lfs.h>
    109 
    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  * A virgin directory (no blushing please).
    118  */
    119 static const struct lfs_dirtemplate mastertemplate = {
    120 	0,	12,			LFS_DT_DIR,	1,	".",
    121 	0,	LFS_DIRBLKSIZ - 12,	LFS_DT_DIR,	2,	".."
    122 };
    123 
    124 /*
    125  * Create a regular file
    126  */
    127 int
    128 ulfs_create(void *v)
    129 {
    130 	struct vop_create_args /* {
    131 		struct vnode		*a_dvp;
    132 		struct vnode		**a_vpp;
    133 		struct componentname	*a_cnp;
    134 		struct vattr		*a_vap;
    135 	} */ *ap = v;
    136 	int	error;
    137 	struct vnode *dvp = ap->a_dvp;
    138 	struct ulfs_lookup_results *ulr;
    139 
    140 	/* XXX should handle this material another way */
    141 	ulr = &VTOI(dvp)->i_crap;
    142 	ULFS_CHECK_CRAPCOUNTER(VTOI(dvp));
    143 
    144 	fstrans_start(dvp->v_mount, FSTRANS_SHARED);
    145 	error =
    146 	    ulfs_makeinode(MAKEIMODE(ap->a_vap->va_type, ap->a_vap->va_mode),
    147 			  dvp, ulr, ap->a_vpp, ap->a_cnp);
    148 	if (error) {
    149 		fstrans_done(dvp->v_mount);
    150 		return (error);
    151 	}
    152 	fstrans_done(dvp->v_mount);
    153 	VN_KNOTE(dvp, NOTE_WRITE);
    154 	return (0);
    155 }
    156 
    157 /*
    158  * Open called.
    159  *
    160  * Nothing to do.
    161  */
    162 /* ARGSUSED */
    163 int
    164 ulfs_open(void *v)
    165 {
    166 	struct vop_open_args /* {
    167 		struct vnode	*a_vp;
    168 		int		a_mode;
    169 		kauth_cred_t	a_cred;
    170 	} */ *ap = v;
    171 
    172 	/*
    173 	 * Files marked append-only must be opened for appending.
    174 	 */
    175 	if ((VTOI(ap->a_vp)->i_flags & APPEND) &&
    176 	    (ap->a_mode & (FWRITE | O_APPEND)) == FWRITE)
    177 		return (EPERM);
    178 	return (0);
    179 }
    180 
    181 static int
    182 ulfs_check_possible(struct vnode *vp, struct inode *ip, mode_t mode,
    183     kauth_cred_t cred)
    184 {
    185 #if defined(LFS_QUOTA) || defined(LFS_QUOTA2)
    186 	int error;
    187 #endif
    188 
    189 	/*
    190 	 * Disallow write attempts on read-only file systems;
    191 	 * unless the file is a socket, fifo, or a block or
    192 	 * character device resident on the file system.
    193 	 */
    194 	if (mode & VWRITE) {
    195 		switch (vp->v_type) {
    196 		case VDIR:
    197 		case VLNK:
    198 		case VREG:
    199 			if (vp->v_mount->mnt_flag & MNT_RDONLY)
    200 				return (EROFS);
    201 #if defined(LFS_QUOTA) || defined(LFS_QUOTA2)
    202 			fstrans_start(vp->v_mount, FSTRANS_SHARED);
    203 			error = lfs_chkdq(ip, 0, cred, 0);
    204 			fstrans_done(vp->v_mount);
    205 			if (error != 0)
    206 				return error;
    207 #endif
    208 			break;
    209 		case VBAD:
    210 		case VBLK:
    211 		case VCHR:
    212 		case VSOCK:
    213 		case VFIFO:
    214 		case VNON:
    215 		default:
    216 			break;
    217 		}
    218 	}
    219 
    220 	/* If it is a snapshot, nobody gets access to it. */
    221 	if ((ip->i_flags & SF_SNAPSHOT))
    222 		return (EPERM);
    223 	/* If immutable bit set, nobody gets to write it. */
    224 	if ((mode & VWRITE) && (ip->i_flags & IMMUTABLE))
    225 		return (EPERM);
    226 
    227 	return 0;
    228 }
    229 
    230 static int
    231 ulfs_check_permitted(struct vnode *vp, struct inode *ip, mode_t mode,
    232     kauth_cred_t cred)
    233 {
    234 
    235 	return kauth_authorize_vnode(cred, KAUTH_ACCESS_ACTION(mode, vp->v_type,
    236 	    ip->i_mode & ALLPERMS), vp, NULL, genfs_can_access(vp->v_type,
    237 	    ip->i_mode & ALLPERMS, ip->i_uid, ip->i_gid, mode, cred));
    238 }
    239 
    240 int
    241 ulfs_access(void *v)
    242 {
    243 	struct vop_access_args /* {
    244 		struct vnode	*a_vp;
    245 		int		a_mode;
    246 		kauth_cred_t	a_cred;
    247 	} */ *ap = v;
    248 	struct vnode	*vp;
    249 	struct inode	*ip;
    250 	mode_t		mode;
    251 	int		error;
    252 
    253 	vp = ap->a_vp;
    254 	ip = VTOI(vp);
    255 	mode = ap->a_mode;
    256 
    257 	error = ulfs_check_possible(vp, ip, mode, ap->a_cred);
    258 	if (error)
    259 		return error;
    260 
    261 	error = ulfs_check_permitted(vp, ip, mode, ap->a_cred);
    262 
    263 	return error;
    264 }
    265 
    266 /*
    267  * Set attribute vnode op. called from several syscalls
    268  */
    269 int
    270 ulfs_setattr(void *v)
    271 {
    272 	struct vop_setattr_args /* {
    273 		struct vnode	*a_vp;
    274 		struct vattr	*a_vap;
    275 		kauth_cred_t	a_cred;
    276 	} */ *ap = v;
    277 	struct vattr	*vap;
    278 	struct vnode	*vp;
    279 	struct inode	*ip;
    280 	kauth_cred_t	cred;
    281 	struct lwp	*l;
    282 	int		error;
    283 	kauth_action_t	action;
    284 	bool		changing_sysflags;
    285 
    286 	vap = ap->a_vap;
    287 	vp = ap->a_vp;
    288 	ip = VTOI(vp);
    289 	cred = ap->a_cred;
    290 	l = curlwp;
    291 	action = KAUTH_VNODE_WRITE_FLAGS;
    292 	changing_sysflags = false;
    293 
    294 	/*
    295 	 * Check for unsettable attributes.
    296 	 */
    297 	if ((vap->va_type != VNON) || (vap->va_nlink != VNOVAL) ||
    298 	    (vap->va_fsid != VNOVAL) || (vap->va_fileid != VNOVAL) ||
    299 	    (vap->va_blocksize != VNOVAL) || (vap->va_rdev != VNOVAL) ||
    300 	    ((int)vap->va_bytes != VNOVAL) || (vap->va_gen != VNOVAL)) {
    301 		return (EINVAL);
    302 	}
    303 
    304 	fstrans_start(vp->v_mount, FSTRANS_SHARED);
    305 
    306 	if (vap->va_flags != VNOVAL) {
    307 		if (vp->v_mount->mnt_flag & MNT_RDONLY) {
    308 			error = EROFS;
    309 			goto out;
    310 		}
    311 
    312 		/* Snapshot flag cannot be set or cleared */
    313 		if ((vap->va_flags & (SF_SNAPSHOT | SF_SNAPINVAL)) !=
    314 		    (ip->i_flags & (SF_SNAPSHOT | SF_SNAPINVAL))) {
    315 			error = EPERM;
    316 			goto out;
    317 		}
    318 
    319 		if (ip->i_flags & (SF_IMMUTABLE | SF_APPEND)) {
    320 			action |= KAUTH_VNODE_HAS_SYSFLAGS;
    321 		}
    322 
    323 		if ((vap->va_flags & SF_SETTABLE) != (ip->i_flags & SF_SETTABLE)) {
    324 			action |= KAUTH_VNODE_WRITE_SYSFLAGS;
    325 			changing_sysflags = true;
    326 		}
    327 
    328 		error = kauth_authorize_vnode(cred, action, vp, NULL,
    329 		    genfs_can_chflags(cred, vp->v_type, ip->i_uid,
    330 		    changing_sysflags));
    331 		if (error)
    332 			goto out;
    333 
    334 		if (changing_sysflags) {
    335 			ip->i_flags = vap->va_flags;
    336 			DIP_ASSIGN(ip, flags, ip->i_flags);
    337 		} else {
    338 			ip->i_flags &= SF_SETTABLE;
    339 			ip->i_flags |= (vap->va_flags & UF_SETTABLE);
    340 			DIP_ASSIGN(ip, flags, ip->i_flags);
    341 		}
    342 		ip->i_flag |= IN_CHANGE;
    343 		if (vap->va_flags & (IMMUTABLE | APPEND)) {
    344 			error = 0;
    345 			goto out;
    346 		}
    347 	}
    348 	if (ip->i_flags & (IMMUTABLE | APPEND)) {
    349 		error = EPERM;
    350 		goto out;
    351 	}
    352 	/*
    353 	 * Go through the fields and update iff not VNOVAL.
    354 	 */
    355 	if (vap->va_uid != (uid_t)VNOVAL || vap->va_gid != (gid_t)VNOVAL) {
    356 		if (vp->v_mount->mnt_flag & MNT_RDONLY) {
    357 			error = EROFS;
    358 			goto out;
    359 		}
    360 		error = ulfs_chown(vp, vap->va_uid, vap->va_gid, cred, l);
    361 		if (error)
    362 			goto out;
    363 	}
    364 	if (vap->va_size != VNOVAL) {
    365 		/*
    366 		 * Disallow write attempts on read-only file systems;
    367 		 * unless the file is a socket, fifo, or a block or
    368 		 * character device resident on the file system.
    369 		 */
    370 		switch (vp->v_type) {
    371 		case VDIR:
    372 			error = EISDIR;
    373 			goto out;
    374 		case VCHR:
    375 		case VBLK:
    376 		case VFIFO:
    377 			break;
    378 		case VREG:
    379 			if (vp->v_mount->mnt_flag & MNT_RDONLY) {
    380 				error = EROFS;
    381 				goto out;
    382 			}
    383 			if ((ip->i_flags & SF_SNAPSHOT) != 0) {
    384 				error = EPERM;
    385 				goto out;
    386 			}
    387 			error = ULFS_TRUNCATE(vp, vap->va_size, 0, cred);
    388 			if (error)
    389 				goto out;
    390 			break;
    391 		default:
    392 			error = EOPNOTSUPP;
    393 			goto out;
    394 		}
    395 	}
    396 	ip = VTOI(vp);
    397 	if (vap->va_atime.tv_sec != VNOVAL || vap->va_mtime.tv_sec != VNOVAL ||
    398 	    vap->va_birthtime.tv_sec != VNOVAL) {
    399 		if (vp->v_mount->mnt_flag & MNT_RDONLY) {
    400 			error = EROFS;
    401 			goto out;
    402 		}
    403 		if ((ip->i_flags & SF_SNAPSHOT) != 0) {
    404 			error = EPERM;
    405 			goto out;
    406 		}
    407 		error = kauth_authorize_vnode(cred, KAUTH_VNODE_WRITE_TIMES, vp,
    408 		    NULL, genfs_can_chtimes(vp, vap->va_vaflags, ip->i_uid, cred));
    409 		if (error)
    410 			goto out;
    411 		if (vap->va_atime.tv_sec != VNOVAL)
    412 			if (!(vp->v_mount->mnt_flag & MNT_NOATIME))
    413 				ip->i_flag |= IN_ACCESS;
    414 		if (vap->va_mtime.tv_sec != VNOVAL) {
    415 			ip->i_flag |= IN_CHANGE | IN_UPDATE;
    416 			if (vp->v_mount->mnt_flag & MNT_RELATIME)
    417 				ip->i_flag |= IN_ACCESS;
    418 		}
    419 		if (vap->va_birthtime.tv_sec != VNOVAL &&
    420 		    ip->i_ump->um_fstype == ULFS2) {
    421 			ip->i_ffs2_birthtime = vap->va_birthtime.tv_sec;
    422 			ip->i_ffs2_birthnsec = vap->va_birthtime.tv_nsec;
    423 		}
    424 		error = ULFS_UPDATE(vp, &vap->va_atime, &vap->va_mtime, 0);
    425 		if (error)
    426 			goto out;
    427 	}
    428 	error = 0;
    429 	if (vap->va_mode != (mode_t)VNOVAL) {
    430 		if (vp->v_mount->mnt_flag & MNT_RDONLY) {
    431 			error = EROFS;
    432 			goto out;
    433 		}
    434 		if ((ip->i_flags & SF_SNAPSHOT) != 0 &&
    435 		    (vap->va_mode & (S_IXUSR | S_IWUSR | S_IXGRP | S_IWGRP |
    436 		     S_IXOTH | S_IWOTH))) {
    437 			error = EPERM;
    438 			goto out;
    439 		}
    440 		error = ulfs_chmod(vp, (int)vap->va_mode, cred, l);
    441 	}
    442 	VN_KNOTE(vp, NOTE_ATTRIB);
    443 out:
    444 	fstrans_done(vp->v_mount);
    445 	return (error);
    446 }
    447 
    448 /*
    449  * Change the mode on a file.
    450  * Inode must be locked before calling.
    451  */
    452 static int
    453 ulfs_chmod(struct vnode *vp, int mode, kauth_cred_t cred, struct lwp *l)
    454 {
    455 	struct inode	*ip;
    456 	int		error;
    457 
    458 	ip = VTOI(vp);
    459 
    460 	error = kauth_authorize_vnode(cred, KAUTH_VNODE_WRITE_SECURITY, vp,
    461 	    NULL, genfs_can_chmod(vp->v_type, cred, ip->i_uid, ip->i_gid, mode));
    462 	if (error)
    463 		return (error);
    464 
    465 	fstrans_start(vp->v_mount, FSTRANS_SHARED);
    466 	ip->i_mode &= ~ALLPERMS;
    467 	ip->i_mode |= (mode & ALLPERMS);
    468 	ip->i_flag |= IN_CHANGE;
    469 	DIP_ASSIGN(ip, mode, ip->i_mode);
    470 	fstrans_done(vp->v_mount);
    471 	return (0);
    472 }
    473 
    474 /*
    475  * Perform chown operation on inode ip;
    476  * inode must be locked prior to call.
    477  */
    478 static int
    479 ulfs_chown(struct vnode *vp, uid_t uid, gid_t gid, kauth_cred_t cred,
    480     	struct lwp *l)
    481 {
    482 	struct inode	*ip;
    483 	int		error = 0;
    484 #if defined(LFS_QUOTA) || defined(LFS_QUOTA2)
    485 	uid_t		ouid;
    486 	gid_t		ogid;
    487 	int64_t		change;
    488 #endif
    489 	ip = VTOI(vp);
    490 	error = 0;
    491 
    492 	if (uid == (uid_t)VNOVAL)
    493 		uid = ip->i_uid;
    494 	if (gid == (gid_t)VNOVAL)
    495 		gid = ip->i_gid;
    496 
    497 	error = kauth_authorize_vnode(cred, KAUTH_VNODE_CHANGE_OWNERSHIP, vp,
    498 	    NULL, genfs_can_chown(cred, ip->i_uid, ip->i_gid, uid, gid));
    499 	if (error)
    500 		return (error);
    501 
    502 	fstrans_start(vp->v_mount, FSTRANS_SHARED);
    503 #if defined(LFS_QUOTA) || defined(LFS_QUOTA2)
    504 	ogid = ip->i_gid;
    505 	ouid = ip->i_uid;
    506 	change = DIP(ip, blocks);
    507 	(void) lfs_chkdq(ip, -change, cred, 0);
    508 	(void) lfs_chkiq(ip, -1, cred, 0);
    509 #endif
    510 	ip->i_gid = gid;
    511 	DIP_ASSIGN(ip, gid, gid);
    512 	ip->i_uid = uid;
    513 	DIP_ASSIGN(ip, uid, uid);
    514 #if defined(LFS_QUOTA) || defined(LFS_QUOTA2)
    515 	if ((error = lfs_chkdq(ip, change, cred, 0)) == 0) {
    516 		if ((error = lfs_chkiq(ip, 1, cred, 0)) == 0)
    517 			goto good;
    518 		else
    519 			(void) lfs_chkdq(ip, -change, cred, FORCE);
    520 	}
    521 	ip->i_gid = ogid;
    522 	DIP_ASSIGN(ip, gid, ogid);
    523 	ip->i_uid = ouid;
    524 	DIP_ASSIGN(ip, uid, ouid);
    525 	(void) lfs_chkdq(ip, change, cred, FORCE);
    526 	(void) lfs_chkiq(ip, 1, cred, FORCE);
    527 	fstrans_done(vp->v_mount);
    528 	return (error);
    529  good:
    530 #endif /* LFS_QUOTA || LFS_QUOTA2 */
    531 	ip->i_flag |= IN_CHANGE;
    532 	fstrans_done(vp->v_mount);
    533 	return (0);
    534 }
    535 
    536 int
    537 ulfs_remove(void *v)
    538 {
    539 	struct vop_remove_args /* {
    540 		struct vnode		*a_dvp;
    541 		struct vnode		*a_vp;
    542 		struct componentname	*a_cnp;
    543 	} */ *ap = v;
    544 	struct vnode	*vp, *dvp;
    545 	struct inode	*ip;
    546 	int		error;
    547 	struct ulfs_lookup_results *ulr;
    548 
    549 	vp = ap->a_vp;
    550 	dvp = ap->a_dvp;
    551 	ip = VTOI(vp);
    552 
    553 	/* XXX should handle this material another way */
    554 	ulr = &VTOI(dvp)->i_crap;
    555 	ULFS_CHECK_CRAPCOUNTER(VTOI(dvp));
    556 
    557 	fstrans_start(dvp->v_mount, FSTRANS_SHARED);
    558 	if (vp->v_type == VDIR || (ip->i_flags & (IMMUTABLE | APPEND)) ||
    559 	    (VTOI(dvp)->i_flags & APPEND))
    560 		error = EPERM;
    561 	else {
    562 		error = ulfs_dirremove(dvp, ulr,
    563 				      ip, ap->a_cnp->cn_flags, 0);
    564 	}
    565 	VN_KNOTE(vp, NOTE_DELETE);
    566 	VN_KNOTE(dvp, NOTE_WRITE);
    567 	if (dvp == vp)
    568 		vrele(vp);
    569 	else
    570 		vput(vp);
    571 	vput(dvp);
    572 	fstrans_done(dvp->v_mount);
    573 	return (error);
    574 }
    575 
    576 /*
    577  * ulfs_link: create hard link.
    578  */
    579 int
    580 ulfs_link(void *v)
    581 {
    582 	struct vop_link_args /* {
    583 		struct vnode *a_dvp;
    584 		struct vnode *a_vp;
    585 		struct componentname *a_cnp;
    586 	} */ *ap = v;
    587 	struct vnode *dvp = ap->a_dvp;
    588 	struct vnode *vp = ap->a_vp;
    589 	struct componentname *cnp = ap->a_cnp;
    590 	struct inode *ip;
    591 	struct lfs_direct *newdir;
    592 	int error;
    593 	struct ulfs_lookup_results *ulr;
    594 
    595 	KASSERT(dvp != vp);
    596 	KASSERT(vp->v_type != VDIR);
    597 	KASSERT(dvp->v_mount == vp->v_mount);
    598 
    599 	/* XXX should handle this material another way */
    600 	ulr = &VTOI(dvp)->i_crap;
    601 	ULFS_CHECK_CRAPCOUNTER(VTOI(dvp));
    602 
    603 	fstrans_start(dvp->v_mount, FSTRANS_SHARED);
    604 	error = vn_lock(vp, LK_EXCLUSIVE);
    605 	if (error) {
    606 		VOP_ABORTOP(dvp, cnp);
    607 		goto out2;
    608 	}
    609 	ip = VTOI(vp);
    610 	if ((nlink_t)ip->i_nlink >= LINK_MAX) {
    611 		VOP_ABORTOP(dvp, cnp);
    612 		error = EMLINK;
    613 		goto out1;
    614 	}
    615 	if (ip->i_flags & (IMMUTABLE | APPEND)) {
    616 		VOP_ABORTOP(dvp, cnp);
    617 		error = EPERM;
    618 		goto out1;
    619 	}
    620 	ip->i_nlink++;
    621 	DIP_ASSIGN(ip, nlink, ip->i_nlink);
    622 	ip->i_flag |= IN_CHANGE;
    623 	error = ULFS_UPDATE(vp, NULL, NULL, UPDATE_DIROP);
    624 	if (!error) {
    625 		newdir = pool_cache_get(ulfs_direct_cache, PR_WAITOK);
    626 		ulfs_makedirentry(ip, cnp, newdir);
    627 		error = ulfs_direnter(dvp, ulr, vp, newdir, cnp, NULL);
    628 		pool_cache_put(ulfs_direct_cache, newdir);
    629 	}
    630 	if (error) {
    631 		ip->i_nlink--;
    632 		DIP_ASSIGN(ip, nlink, ip->i_nlink);
    633 		ip->i_flag |= IN_CHANGE;
    634 	}
    635  out1:
    636 	VOP_UNLOCK(vp);
    637  out2:
    638 	VN_KNOTE(vp, NOTE_LINK);
    639 	VN_KNOTE(dvp, NOTE_WRITE);
    640 	vput(dvp);
    641 	fstrans_done(dvp->v_mount);
    642 	return (error);
    643 }
    644 
    645 /*
    646  * whiteout vnode call
    647  */
    648 int
    649 ulfs_whiteout(void *v)
    650 {
    651 	struct vop_whiteout_args /* {
    652 		struct vnode		*a_dvp;
    653 		struct componentname	*a_cnp;
    654 		int			a_flags;
    655 	} */ *ap = v;
    656 	struct vnode		*dvp = ap->a_dvp;
    657 	struct componentname	*cnp = ap->a_cnp;
    658 	struct lfs_direct		*newdir;
    659 	int			error;
    660 	struct ulfsmount	*ump = VFSTOULFS(dvp->v_mount);
    661 	struct ulfs_lookup_results *ulr;
    662 
    663 	/* XXX should handle this material another way */
    664 	ulr = &VTOI(dvp)->i_crap;
    665 	ULFS_CHECK_CRAPCOUNTER(VTOI(dvp));
    666 
    667 	error = 0;
    668 	switch (ap->a_flags) {
    669 	case LOOKUP:
    670 		/* 4.4 format directories support whiteout operations */
    671 		if (ump->um_maxsymlinklen > 0)
    672 			return (0);
    673 		return (EOPNOTSUPP);
    674 
    675 	case CREATE:
    676 		/* create a new directory whiteout */
    677 		fstrans_start(dvp->v_mount, FSTRANS_SHARED);
    678 #ifdef DIAGNOSTIC
    679 		if (ump->um_maxsymlinklen <= 0)
    680 			panic("ulfs_whiteout: old format filesystem");
    681 #endif
    682 
    683 		newdir = pool_cache_get(ulfs_direct_cache, PR_WAITOK);
    684 		newdir->d_ino = ULFS_WINO;
    685 		newdir->d_namlen = cnp->cn_namelen;
    686 		memcpy(newdir->d_name, cnp->cn_nameptr,
    687 		    (size_t)cnp->cn_namelen);
    688 		newdir->d_name[cnp->cn_namelen] = '\0';
    689 		newdir->d_type = LFS_DT_WHT;
    690 		error = ulfs_direnter(dvp, ulr, NULL, newdir, cnp, NULL);
    691 		pool_cache_put(ulfs_direct_cache, newdir);
    692 		break;
    693 
    694 	case DELETE:
    695 		/* remove an existing directory whiteout */
    696 		fstrans_start(dvp->v_mount, FSTRANS_SHARED);
    697 #ifdef DIAGNOSTIC
    698 		if (ump->um_maxsymlinklen <= 0)
    699 			panic("ulfs_whiteout: old format filesystem");
    700 #endif
    701 
    702 		cnp->cn_flags &= ~DOWHITEOUT;
    703 		error = ulfs_dirremove(dvp, ulr, NULL, cnp->cn_flags, 0);
    704 		break;
    705 	default:
    706 		panic("ulfs_whiteout: unknown op");
    707 		/* NOTREACHED */
    708 	}
    709 	fstrans_done(dvp->v_mount);
    710 	return (error);
    711 }
    712 
    713 int
    714 ulfs_mkdir(void *v)
    715 {
    716 	struct vop_mkdir_args /* {
    717 		struct vnode		*a_dvp;
    718 		struct vnode		**a_vpp;
    719 		struct componentname	*a_cnp;
    720 		struct vattr		*a_vap;
    721 	} */ *ap = v;
    722 	struct vnode		*dvp = ap->a_dvp, *tvp;
    723 	struct vattr		*vap = ap->a_vap;
    724 	struct componentname	*cnp = ap->a_cnp;
    725 	struct inode		*ip, *dp = VTOI(dvp);
    726 	struct buf		*bp;
    727 	struct lfs_dirtemplate	dirtemplate;
    728 	struct lfs_direct		*newdir;
    729 	int			error, dmode;
    730 	struct ulfsmount	*ump = dp->i_ump;
    731 	int			dirblksiz = ump->um_dirblksiz;
    732 	struct ulfs_lookup_results *ulr;
    733 
    734 	fstrans_start(dvp->v_mount, FSTRANS_SHARED);
    735 
    736 	/* XXX should handle this material another way */
    737 	ulr = &dp->i_crap;
    738 	ULFS_CHECK_CRAPCOUNTER(dp);
    739 
    740 	if ((nlink_t)dp->i_nlink >= LINK_MAX) {
    741 		error = EMLINK;
    742 		goto out;
    743 	}
    744 	dmode = vap->va_mode & ACCESSPERMS;
    745 	dmode |= LFS_IFDIR;
    746 	/*
    747 	 * Must simulate part of ulfs_makeinode here to acquire the inode,
    748 	 * but not have it entered in the parent directory. The entry is
    749 	 * made later after writing "." and ".." entries.
    750 	 */
    751 	if ((error = ULFS_VALLOC(dvp, dmode, cnp->cn_cred, ap->a_vpp)) != 0)
    752 		goto out;
    753 
    754 	tvp = *ap->a_vpp;
    755 	ip = VTOI(tvp);
    756 
    757 	ip->i_uid = kauth_cred_geteuid(cnp->cn_cred);
    758 	DIP_ASSIGN(ip, uid, ip->i_uid);
    759 	ip->i_gid = dp->i_gid;
    760 	DIP_ASSIGN(ip, gid, ip->i_gid);
    761 #if defined(LFS_QUOTA) || defined(LFS_QUOTA2)
    762 	if ((error = lfs_chkiq(ip, 1, cnp->cn_cred, 0))) {
    763 		ULFS_VFREE(tvp, ip->i_number, dmode);
    764 		fstrans_done(dvp->v_mount);
    765 		vput(tvp);
    766 		vput(dvp);
    767 		return (error);
    768 	}
    769 #endif
    770 	ip->i_flag |= IN_ACCESS | IN_CHANGE | IN_UPDATE;
    771 	ip->i_mode = dmode;
    772 	DIP_ASSIGN(ip, mode, dmode);
    773 	tvp->v_type = VDIR;	/* Rest init'd in getnewvnode(). */
    774 	ip->i_nlink = 2;
    775 	DIP_ASSIGN(ip, nlink, 2);
    776 	if (cnp->cn_flags & ISWHITEOUT) {
    777 		ip->i_flags |= UF_OPAQUE;
    778 		DIP_ASSIGN(ip, flags, ip->i_flags);
    779 	}
    780 
    781 	/*
    782 	 * Bump link count in parent directory to reflect work done below.
    783 	 * Should be done before reference is created so cleanup is
    784 	 * possible if we crash.
    785 	 */
    786 	dp->i_nlink++;
    787 	DIP_ASSIGN(dp, nlink, dp->i_nlink);
    788 	dp->i_flag |= IN_CHANGE;
    789 	if ((error = ULFS_UPDATE(dvp, NULL, NULL, UPDATE_DIROP)) != 0)
    790 		goto bad;
    791 
    792 	/*
    793 	 * Initialize directory with "." and ".." from static template.
    794 	 */
    795 	dirtemplate = mastertemplate;
    796 	dirtemplate.dotdot_reclen = dirblksiz - dirtemplate.dot_reclen;
    797 	dirtemplate.dot_ino = ulfs_rw32(ip->i_number, ULFS_MPNEEDSWAP(ump));
    798 	dirtemplate.dotdot_ino = ulfs_rw32(dp->i_number, ULFS_MPNEEDSWAP(ump));
    799 	dirtemplate.dot_reclen = ulfs_rw16(dirtemplate.dot_reclen,
    800 	    ULFS_MPNEEDSWAP(ump));
    801 	dirtemplate.dotdot_reclen = ulfs_rw16(dirtemplate.dotdot_reclen,
    802 	    ULFS_MPNEEDSWAP(ump));
    803 	if (ump->um_maxsymlinklen <= 0) {
    804 #if BYTE_ORDER == LITTLE_ENDIAN
    805 		if (ULFS_MPNEEDSWAP(ump) == 0)
    806 #else
    807 		if (ULFS_MPNEEDSWAP(ump) != 0)
    808 #endif
    809 		{
    810 			dirtemplate.dot_type = dirtemplate.dot_namlen;
    811 			dirtemplate.dotdot_type = dirtemplate.dotdot_namlen;
    812 			dirtemplate.dot_namlen = dirtemplate.dotdot_namlen = 0;
    813 		} else
    814 			dirtemplate.dot_type = dirtemplate.dotdot_type = 0;
    815 	}
    816 	if ((error = ULFS_BALLOC(tvp, (off_t)0, dirblksiz, cnp->cn_cred,
    817 	    B_CLRBUF, &bp)) != 0)
    818 		goto bad;
    819 	ip->i_size = dirblksiz;
    820 	DIP_ASSIGN(ip, size, dirblksiz);
    821 	ip->i_flag |= IN_ACCESS | IN_CHANGE | IN_UPDATE;
    822 	uvm_vnp_setsize(tvp, ip->i_size);
    823 	memcpy((void *)bp->b_data, (void *)&dirtemplate, sizeof dirtemplate);
    824 
    825 	/*
    826 	 * Directory set up, now install it's entry in the parent directory.
    827 	 * We must write out the buffer containing the new directory body
    828 	 * before entering the new name in the parent.
    829 	 */
    830 	if ((error = VOP_BWRITE(bp->b_vp, bp)) != 0)
    831 		goto bad;
    832 	if ((error = ULFS_UPDATE(tvp, NULL, NULL, UPDATE_DIROP)) != 0) {
    833 		goto bad;
    834 	}
    835 	newdir = pool_cache_get(ulfs_direct_cache, PR_WAITOK);
    836 	ulfs_makedirentry(ip, cnp, newdir);
    837 	error = ulfs_direnter(dvp, ulr, tvp, newdir, cnp, bp);
    838 	pool_cache_put(ulfs_direct_cache, newdir);
    839  bad:
    840 	if (error == 0) {
    841 		VN_KNOTE(dvp, NOTE_WRITE | NOTE_LINK);
    842 	} else {
    843 		dp->i_nlink--;
    844 		DIP_ASSIGN(dp, nlink, dp->i_nlink);
    845 		dp->i_flag |= IN_CHANGE;
    846 		/*
    847 		 * No need to do an explicit ULFS_TRUNCATE here, vrele will
    848 		 * do this for us because we set the link count to 0.
    849 		 */
    850 		ip->i_nlink = 0;
    851 		DIP_ASSIGN(ip, nlink, 0);
    852 		ip->i_flag |= IN_CHANGE;
    853 		/* If IN_ADIROP, account for it */
    854 		ULFS_UNMARK_VNODE(tvp);
    855 		vput(tvp);
    856 	}
    857  out:
    858 	fstrans_done(dvp->v_mount);
    859 	vput(dvp);
    860 	return (error);
    861 }
    862 
    863 int
    864 ulfs_rmdir(void *v)
    865 {
    866 	struct vop_rmdir_args /* {
    867 		struct vnode		*a_dvp;
    868 		struct vnode		*a_vp;
    869 		struct componentname	*a_cnp;
    870 	} */ *ap = v;
    871 	struct vnode		*vp, *dvp;
    872 	struct componentname	*cnp;
    873 	struct inode		*ip, *dp;
    874 	int			error;
    875 	struct ulfs_lookup_results *ulr;
    876 
    877 	vp = ap->a_vp;
    878 	dvp = ap->a_dvp;
    879 	cnp = ap->a_cnp;
    880 	ip = VTOI(vp);
    881 	dp = VTOI(dvp);
    882 
    883 	/* XXX should handle this material another way */
    884 	ulr = &dp->i_crap;
    885 	ULFS_CHECK_CRAPCOUNTER(dp);
    886 
    887 	/*
    888 	 * No rmdir "." or of mounted directories please.
    889 	 */
    890 	if (dp == ip || vp->v_mountedhere != NULL) {
    891 		if (dp == ip)
    892 			vrele(dvp);
    893 		else
    894 			vput(dvp);
    895 		vput(vp);
    896 		return (EINVAL);
    897 	}
    898 
    899 	fstrans_start(dvp->v_mount, FSTRANS_SHARED);
    900 
    901 	/*
    902 	 * Do not remove a directory that is in the process of being renamed.
    903 	 * Verify that the directory is empty (and valid). (Rmdir ".." won't
    904 	 * be valid since ".." will contain a reference to the current
    905 	 * directory and thus be non-empty.)
    906 	 */
    907 	error = 0;
    908 	if (ip->i_nlink != 2 ||
    909 	    !ulfs_dirempty(ip, dp->i_number, cnp->cn_cred)) {
    910 		error = ENOTEMPTY;
    911 		goto out;
    912 	}
    913 	if ((dp->i_flags & APPEND) ||
    914 		(ip->i_flags & (IMMUTABLE | APPEND))) {
    915 		error = EPERM;
    916 		goto out;
    917 	}
    918 	/*
    919 	 * Delete reference to directory before purging
    920 	 * inode.  If we crash in between, the directory
    921 	 * will be reattached to lost+found,
    922 	 */
    923 	error = ulfs_dirremove(dvp, ulr, ip, cnp->cn_flags, 1);
    924 	if (error) {
    925 		goto out;
    926 	}
    927 	VN_KNOTE(dvp, NOTE_WRITE | NOTE_LINK);
    928 	cache_purge(dvp);
    929 	/*
    930 	 * Truncate inode.  The only stuff left in the directory is "." and
    931 	 * "..".  The "." reference is inconsequential since we're quashing
    932 	 * it.
    933 	 */
    934 	dp->i_nlink--;
    935 	DIP_ASSIGN(dp, nlink, dp->i_nlink);
    936 	dp->i_flag |= IN_CHANGE;
    937 	ip->i_nlink--;
    938 	DIP_ASSIGN(ip, nlink, ip->i_nlink);
    939 	ip->i_flag |= IN_CHANGE;
    940 	error = ULFS_TRUNCATE(vp, (off_t)0, IO_SYNC, cnp->cn_cred);
    941 	cache_purge(vp);
    942 #ifdef LFS_DIRHASH
    943 	if (ip->i_dirhash != NULL)
    944 		ulfsdirhash_free(ip);
    945 #endif
    946  out:
    947 	VN_KNOTE(vp, NOTE_DELETE);
    948 	vput(vp);
    949 	fstrans_done(dvp->v_mount);
    950 	vput(dvp);
    951 	return (error);
    952 }
    953 
    954 /*
    955  * symlink -- make a symbolic link
    956  */
    957 int
    958 ulfs_symlink(void *v)
    959 {
    960 	struct vop_symlink_args /* {
    961 		struct vnode		*a_dvp;
    962 		struct vnode		**a_vpp;
    963 		struct componentname	*a_cnp;
    964 		struct vattr		*a_vap;
    965 		char			*a_target;
    966 	} */ *ap = v;
    967 	struct vnode	*vp, **vpp;
    968 	struct inode	*ip;
    969 	int		len, error;
    970 	struct ulfs_lookup_results *ulr;
    971 
    972 	vpp = ap->a_vpp;
    973 
    974 	/* XXX should handle this material another way */
    975 	ulr = &VTOI(ap->a_dvp)->i_crap;
    976 	ULFS_CHECK_CRAPCOUNTER(VTOI(ap->a_dvp));
    977 
    978 	fstrans_start(ap->a_dvp->v_mount, FSTRANS_SHARED);
    979 	error = ulfs_makeinode(LFS_IFLNK | ap->a_vap->va_mode, ap->a_dvp, ulr,
    980 			      vpp, ap->a_cnp);
    981 	if (error)
    982 		goto out;
    983 	VN_KNOTE(ap->a_dvp, NOTE_WRITE);
    984 	vp = *vpp;
    985 	len = strlen(ap->a_target);
    986 	ip = VTOI(vp);
    987 	if (len < ip->i_ump->um_maxsymlinklen) {
    988 		memcpy((char *)SHORTLINK(ip), ap->a_target, len);
    989 		ip->i_size = len;
    990 		DIP_ASSIGN(ip, size, len);
    991 		uvm_vnp_setsize(vp, ip->i_size);
    992 		ip->i_flag |= IN_CHANGE | IN_UPDATE;
    993 		if (vp->v_mount->mnt_flag & MNT_RELATIME)
    994 			ip->i_flag |= IN_ACCESS;
    995 	} else
    996 		error = vn_rdwr(UIO_WRITE, vp, ap->a_target, len, (off_t)0,
    997 		    UIO_SYSSPACE, IO_NODELOCKED | IO_JOURNALLOCKED,
    998 		    ap->a_cnp->cn_cred, NULL, NULL);
    999 	if (error)
   1000 		vput(vp);
   1001 out:
   1002 	fstrans_done(ap->a_dvp->v_mount);
   1003 	return (error);
   1004 }
   1005 
   1006 /*
   1007  * Vnode op for reading directories.
   1008  *
   1009  * This routine handles converting from the on-disk directory format
   1010  * "struct lfs_direct" to the in-memory format "struct dirent" as well as
   1011  * byte swapping the entries if necessary.
   1012  */
   1013 int
   1014 ulfs_readdir(void *v)
   1015 {
   1016 	struct vop_readdir_args /* {
   1017 		struct vnode	*a_vp;
   1018 		struct uio	*a_uio;
   1019 		kauth_cred_t	a_cred;
   1020 		int		*a_eofflag;
   1021 		off_t		**a_cookies;
   1022 		int		*ncookies;
   1023 	} */ *ap = v;
   1024 	struct vnode	*vp = ap->a_vp;
   1025 	struct lfs_direct	*cdp, *ecdp;
   1026 	struct dirent	*ndp;
   1027 	char		*cdbuf, *ndbuf, *endp;
   1028 	struct uio	auio, *uio;
   1029 	struct iovec	aiov;
   1030 	int		error;
   1031 	size_t		count, ccount, rcount, cdbufsz, ndbufsz;
   1032 	off_t		off, *ccp;
   1033 	off_t		startoff;
   1034 	size_t		skipbytes;
   1035 	struct ulfsmount *ump = VFSTOULFS(vp->v_mount);
   1036 	int nswap = ULFS_MPNEEDSWAP(ump);
   1037 #if BYTE_ORDER == LITTLE_ENDIAN
   1038 	int needswap = ump->um_maxsymlinklen <= 0 && nswap == 0;
   1039 #else
   1040 	int needswap = ump->um_maxsymlinklen <= 0 && nswap != 0;
   1041 #endif
   1042 	uio = ap->a_uio;
   1043 	count = uio->uio_resid;
   1044 	rcount = count - ((uio->uio_offset + count) & (ump->um_dirblksiz - 1));
   1045 
   1046 	if (rcount < _DIRENT_MINSIZE(cdp) || count < _DIRENT_MINSIZE(ndp))
   1047 		return EINVAL;
   1048 
   1049 	startoff = uio->uio_offset & ~(ump->um_dirblksiz - 1);
   1050 	skipbytes = uio->uio_offset - startoff;
   1051 	rcount += skipbytes;
   1052 
   1053 	auio.uio_iov = &aiov;
   1054 	auio.uio_iovcnt = 1;
   1055 	auio.uio_offset = startoff;
   1056 	auio.uio_resid = rcount;
   1057 	UIO_SETUP_SYSSPACE(&auio);
   1058 	auio.uio_rw = UIO_READ;
   1059 	cdbufsz = rcount;
   1060 	cdbuf = kmem_alloc(cdbufsz, KM_SLEEP);
   1061 	aiov.iov_base = cdbuf;
   1062 	aiov.iov_len = rcount;
   1063 	error = VOP_READ(vp, &auio, 0, ap->a_cred);
   1064 	if (error != 0) {
   1065 		kmem_free(cdbuf, cdbufsz);
   1066 		return error;
   1067 	}
   1068 
   1069 	rcount -= auio.uio_resid;
   1070 
   1071 	cdp = (struct lfs_direct *)(void *)cdbuf;
   1072 	ecdp = (struct lfs_direct *)(void *)&cdbuf[rcount];
   1073 
   1074 	ndbufsz = count;
   1075 	ndbuf = kmem_alloc(ndbufsz, KM_SLEEP);
   1076 	ndp = (struct dirent *)(void *)ndbuf;
   1077 	endp = &ndbuf[count];
   1078 
   1079 	off = uio->uio_offset;
   1080 	if (ap->a_cookies) {
   1081 		ccount = rcount / _DIRENT_RECLEN(cdp, 1);
   1082 		ccp = *(ap->a_cookies) = malloc(ccount * sizeof(*ccp),
   1083 		    M_TEMP, M_WAITOK);
   1084 	} else {
   1085 		/* XXX: GCC */
   1086 		ccount = 0;
   1087 		ccp = NULL;
   1088 	}
   1089 
   1090 	while (cdp < ecdp) {
   1091 		cdp->d_reclen = ulfs_rw16(cdp->d_reclen, nswap);
   1092 		if (skipbytes > 0) {
   1093 			if (cdp->d_reclen <= skipbytes) {
   1094 				skipbytes -= cdp->d_reclen;
   1095 				cdp = _DIRENT_NEXT(cdp);
   1096 				continue;
   1097 			}
   1098 			/*
   1099 			 * invalid cookie.
   1100 			 */
   1101 			error = EINVAL;
   1102 			goto out;
   1103 		}
   1104 		if (cdp->d_reclen == 0) {
   1105 			struct dirent *ondp = ndp;
   1106 			ndp->d_reclen = _DIRENT_MINSIZE(ndp);
   1107 			ndp = _DIRENT_NEXT(ndp);
   1108 			ondp->d_reclen = 0;
   1109 			cdp = ecdp;
   1110 			break;
   1111 		}
   1112 		if (needswap) {
   1113 			ndp->d_type = cdp->d_namlen;
   1114 			ndp->d_namlen = cdp->d_type;
   1115 		} else {
   1116 			ndp->d_type = cdp->d_type;
   1117 			ndp->d_namlen = cdp->d_namlen;
   1118 		}
   1119 		ndp->d_reclen = _DIRENT_RECLEN(ndp, ndp->d_namlen);
   1120 		if ((char *)(void *)ndp + ndp->d_reclen +
   1121 		    _DIRENT_MINSIZE(ndp) > endp)
   1122 			break;
   1123 		ndp->d_fileno = ulfs_rw32(cdp->d_ino, nswap);
   1124 		(void)memcpy(ndp->d_name, cdp->d_name, ndp->d_namlen);
   1125 		memset(&ndp->d_name[ndp->d_namlen], 0,
   1126 		    ndp->d_reclen - _DIRENT_NAMEOFF(ndp) - ndp->d_namlen);
   1127 		off += cdp->d_reclen;
   1128 		if (ap->a_cookies) {
   1129 			KASSERT(ccp - *(ap->a_cookies) < ccount);
   1130 			*(ccp++) = off;
   1131 		}
   1132 		ndp = _DIRENT_NEXT(ndp);
   1133 		cdp = _DIRENT_NEXT(cdp);
   1134 	}
   1135 
   1136 	count = ((char *)(void *)ndp - ndbuf);
   1137 	error = uiomove(ndbuf, count, uio);
   1138 out:
   1139 	if (ap->a_cookies) {
   1140 		if (error) {
   1141 			free(*(ap->a_cookies), M_TEMP);
   1142 			*(ap->a_cookies) = NULL;
   1143 			*(ap->a_ncookies) = 0;
   1144 		} else {
   1145 			*ap->a_ncookies = ccp - *(ap->a_cookies);
   1146 		}
   1147 	}
   1148 	uio->uio_offset = off;
   1149 	kmem_free(ndbuf, ndbufsz);
   1150 	kmem_free(cdbuf, cdbufsz);
   1151 	*ap->a_eofflag = VTOI(vp)->i_size <= uio->uio_offset;
   1152 	return error;
   1153 }
   1154 
   1155 /*
   1156  * Return target name of a symbolic link
   1157  */
   1158 int
   1159 ulfs_readlink(void *v)
   1160 {
   1161 	struct vop_readlink_args /* {
   1162 		struct vnode	*a_vp;
   1163 		struct uio	*a_uio;
   1164 		kauth_cred_t	a_cred;
   1165 	} */ *ap = v;
   1166 	struct vnode	*vp = ap->a_vp;
   1167 	struct inode	*ip = VTOI(vp);
   1168 	struct ulfsmount *ump = VFSTOULFS(vp->v_mount);
   1169 	int		isize;
   1170 
   1171 	isize = ip->i_size;
   1172 	if (isize < ump->um_maxsymlinklen ||
   1173 	    (ump->um_maxsymlinklen == 0 && DIP(ip, blocks) == 0)) {
   1174 		uiomove((char *)SHORTLINK(ip), isize, ap->a_uio);
   1175 		return (0);
   1176 	}
   1177 	return (VOP_READ(vp, ap->a_uio, 0, ap->a_cred));
   1178 }
   1179 
   1180 /*
   1181  * Print out the contents of an inode.
   1182  */
   1183 int
   1184 ulfs_print(void *v)
   1185 {
   1186 	struct vop_print_args /* {
   1187 		struct vnode	*a_vp;
   1188 	} */ *ap = v;
   1189 	struct vnode	*vp;
   1190 	struct inode	*ip;
   1191 
   1192 	vp = ap->a_vp;
   1193 	ip = VTOI(vp);
   1194 	printf("tag VT_ULFS, ino %llu, on dev %llu, %llu",
   1195 	    (unsigned long long)ip->i_number,
   1196 	    (unsigned long long)major(ip->i_dev),
   1197 	    (unsigned long long)minor(ip->i_dev));
   1198 	printf(" flags 0x%x, nlink %d\n",
   1199 	    ip->i_flag, ip->i_nlink);
   1200 	printf("\tmode 0%o, owner %d, group %d, size %qd",
   1201 	    ip->i_mode, ip->i_uid, ip->i_gid,
   1202 	    (long long)ip->i_size);
   1203 	if (vp->v_type == VFIFO)
   1204 		VOCALL(fifo_vnodeop_p, VOFFSET(vop_print), v);
   1205 	printf("\n");
   1206 	return (0);
   1207 }
   1208 
   1209 /*
   1210  * Read wrapper for special devices.
   1211  */
   1212 int
   1213 ulfsspec_read(void *v)
   1214 {
   1215 	struct vop_read_args /* {
   1216 		struct vnode	*a_vp;
   1217 		struct uio	*a_uio;
   1218 		int		a_ioflag;
   1219 		kauth_cred_t	a_cred;
   1220 	} */ *ap = v;
   1221 
   1222 	/*
   1223 	 * Set access flag.
   1224 	 */
   1225 	if ((ap->a_vp->v_mount->mnt_flag & MNT_NODEVMTIME) == 0)
   1226 		VTOI(ap->a_vp)->i_flag |= IN_ACCESS;
   1227 	return (VOCALL (spec_vnodeop_p, VOFFSET(vop_read), ap));
   1228 }
   1229 
   1230 /*
   1231  * Write wrapper for special devices.
   1232  */
   1233 int
   1234 ulfsspec_write(void *v)
   1235 {
   1236 	struct vop_write_args /* {
   1237 		struct vnode	*a_vp;
   1238 		struct uio	*a_uio;
   1239 		int		a_ioflag;
   1240 		kauth_cred_t	a_cred;
   1241 	} */ *ap = v;
   1242 
   1243 	/*
   1244 	 * Set update and change flags.
   1245 	 */
   1246 	if ((ap->a_vp->v_mount->mnt_flag & MNT_NODEVMTIME) == 0)
   1247 		VTOI(ap->a_vp)->i_flag |= IN_MODIFY;
   1248 	return (VOCALL (spec_vnodeop_p, VOFFSET(vop_write), ap));
   1249 }
   1250 
   1251 /*
   1252  * Close wrapper for special devices.
   1253  *
   1254  * Update the times on the inode then do device close.
   1255  */
   1256 int
   1257 ulfsspec_close(void *v)
   1258 {
   1259 	struct vop_close_args /* {
   1260 		struct vnode	*a_vp;
   1261 		int		a_fflag;
   1262 		kauth_cred_t	a_cred;
   1263 	} */ *ap = v;
   1264 	struct vnode	*vp;
   1265 	struct inode	*ip;
   1266 
   1267 	vp = ap->a_vp;
   1268 	ip = VTOI(vp);
   1269 	if (vp->v_usecount > 1)
   1270 		ULFS_ITIMES(vp, NULL, NULL, NULL);
   1271 	return (VOCALL (spec_vnodeop_p, VOFFSET(vop_close), ap));
   1272 }
   1273 
   1274 /*
   1275  * Read wrapper for fifo's
   1276  */
   1277 int
   1278 ulfsfifo_read(void *v)
   1279 {
   1280 	struct vop_read_args /* {
   1281 		struct vnode	*a_vp;
   1282 		struct uio	*a_uio;
   1283 		int		a_ioflag;
   1284 		kauth_cred_t	a_cred;
   1285 	} */ *ap = v;
   1286 
   1287 	/*
   1288 	 * Set access flag.
   1289 	 */
   1290 	VTOI(ap->a_vp)->i_flag |= IN_ACCESS;
   1291 	return (VOCALL (fifo_vnodeop_p, VOFFSET(vop_read), ap));
   1292 }
   1293 
   1294 /*
   1295  * Write wrapper for fifo's.
   1296  */
   1297 int
   1298 ulfsfifo_write(void *v)
   1299 {
   1300 	struct vop_write_args /* {
   1301 		struct vnode	*a_vp;
   1302 		struct uio	*a_uio;
   1303 		int		a_ioflag;
   1304 		kauth_cred_t	a_cred;
   1305 	} */ *ap = v;
   1306 
   1307 	/*
   1308 	 * Set update and change flags.
   1309 	 */
   1310 	VTOI(ap->a_vp)->i_flag |= IN_MODIFY;
   1311 	return (VOCALL (fifo_vnodeop_p, VOFFSET(vop_write), ap));
   1312 }
   1313 
   1314 /*
   1315  * Close wrapper for fifo's.
   1316  *
   1317  * Update the times on the inode then do device close.
   1318  */
   1319 int
   1320 ulfsfifo_close(void *v)
   1321 {
   1322 	struct vop_close_args /* {
   1323 		struct vnode	*a_vp;
   1324 		int		a_fflag;
   1325 		kauth_cred_t	a_cred;
   1326 	} */ *ap = v;
   1327 	struct vnode	*vp;
   1328 	struct inode	*ip;
   1329 
   1330 	vp = ap->a_vp;
   1331 	ip = VTOI(vp);
   1332 	if (ap->a_vp->v_usecount > 1)
   1333 		ULFS_ITIMES(vp, NULL, NULL, NULL);
   1334 	return (VOCALL (fifo_vnodeop_p, VOFFSET(vop_close), ap));
   1335 }
   1336 
   1337 /*
   1338  * Return POSIX pathconf information applicable to ulfs filesystems.
   1339  */
   1340 int
   1341 ulfs_pathconf(void *v)
   1342 {
   1343 	struct vop_pathconf_args /* {
   1344 		struct vnode	*a_vp;
   1345 		int		a_name;
   1346 		register_t	*a_retval;
   1347 	} */ *ap = v;
   1348 
   1349 	switch (ap->a_name) {
   1350 	case _PC_LINK_MAX:
   1351 		*ap->a_retval = LINK_MAX;
   1352 		return (0);
   1353 	case _PC_NAME_MAX:
   1354 		*ap->a_retval = LFS_MAXNAMLEN;
   1355 		return (0);
   1356 	case _PC_PATH_MAX:
   1357 		*ap->a_retval = PATH_MAX;
   1358 		return (0);
   1359 	case _PC_PIPE_BUF:
   1360 		*ap->a_retval = PIPE_BUF;
   1361 		return (0);
   1362 	case _PC_CHOWN_RESTRICTED:
   1363 		*ap->a_retval = 1;
   1364 		return (0);
   1365 	case _PC_NO_TRUNC:
   1366 		*ap->a_retval = 1;
   1367 		return (0);
   1368 	case _PC_SYNC_IO:
   1369 		*ap->a_retval = 1;
   1370 		return (0);
   1371 	case _PC_FILESIZEBITS:
   1372 		*ap->a_retval = 42;
   1373 		return (0);
   1374 	case _PC_SYMLINK_MAX:
   1375 		*ap->a_retval = MAXPATHLEN;
   1376 		return (0);
   1377 	case _PC_2_SYMLINKS:
   1378 		*ap->a_retval = 1;
   1379 		return (0);
   1380 	default:
   1381 		return (EINVAL);
   1382 	}
   1383 	/* NOTREACHED */
   1384 }
   1385 
   1386 /*
   1387  * Advisory record locking support
   1388  */
   1389 int
   1390 ulfs_advlock(void *v)
   1391 {
   1392 	struct vop_advlock_args /* {
   1393 		struct vnode	*a_vp;
   1394 		void *		a_id;
   1395 		int		a_op;
   1396 		struct flock	*a_fl;
   1397 		int		a_flags;
   1398 	} */ *ap = v;
   1399 	struct inode *ip;
   1400 
   1401 	ip = VTOI(ap->a_vp);
   1402 	return lf_advlock(ap, &ip->i_lockf, ip->i_size);
   1403 }
   1404 
   1405 /*
   1406  * Initialize the vnode associated with a new inode, handle aliased
   1407  * vnodes.
   1408  */
   1409 void
   1410 ulfs_vinit(struct mount *mntp, int (**specops)(void *), int (**fifoops)(void *),
   1411 	struct vnode **vpp)
   1412 {
   1413 	struct timeval	tv;
   1414 	struct inode	*ip;
   1415 	struct vnode	*vp;
   1416 	dev_t		rdev;
   1417 	struct ulfsmount *ump;
   1418 
   1419 	vp = *vpp;
   1420 	ip = VTOI(vp);
   1421 	switch(vp->v_type = IFTOVT(ip->i_mode)) {
   1422 	case VCHR:
   1423 	case VBLK:
   1424 		vp->v_op = specops;
   1425 		ump = ip->i_ump;
   1426 		if (ump->um_fstype == ULFS1)
   1427 			rdev = (dev_t)ulfs_rw32(ip->i_ffs1_rdev,
   1428 			    ULFS_MPNEEDSWAP(ump));
   1429 		else
   1430 			rdev = (dev_t)ulfs_rw64(ip->i_ffs2_rdev,
   1431 			    ULFS_MPNEEDSWAP(ump));
   1432 		spec_node_init(vp, rdev);
   1433 		break;
   1434 	case VFIFO:
   1435 		vp->v_op = fifoops;
   1436 		break;
   1437 	case VNON:
   1438 	case VBAD:
   1439 	case VSOCK:
   1440 	case VLNK:
   1441 	case VDIR:
   1442 	case VREG:
   1443 		break;
   1444 	}
   1445 	if (ip->i_number == ULFS_ROOTINO)
   1446                 vp->v_vflag |= VV_ROOT;
   1447 	/*
   1448 	 * Initialize modrev times
   1449 	 */
   1450 	getmicrouptime(&tv);
   1451 	ip->i_modrev = (uint64_t)(uint)tv.tv_sec << 32
   1452 			| tv.tv_usec * 4294u;
   1453 	*vpp = vp;
   1454 }
   1455 
   1456 /*
   1457  * Allocate a new inode.
   1458  */
   1459 int
   1460 ulfs_makeinode(int mode, struct vnode *dvp, const struct ulfs_lookup_results *ulr,
   1461 	struct vnode **vpp, struct componentname *cnp)
   1462 {
   1463 	struct inode	*ip, *pdir;
   1464 	struct lfs_direct	*newdir;
   1465 	struct vnode	*tvp;
   1466 	int		error;
   1467 
   1468 	pdir = VTOI(dvp);
   1469 
   1470 	if ((mode & LFS_IFMT) == 0)
   1471 		mode |= LFS_IFREG;
   1472 
   1473 	if ((error = ULFS_VALLOC(dvp, mode, cnp->cn_cred, vpp)) != 0) {
   1474 		vput(dvp);
   1475 		return (error);
   1476 	}
   1477 	tvp = *vpp;
   1478 	ip = VTOI(tvp);
   1479 	ip->i_gid = pdir->i_gid;
   1480 	DIP_ASSIGN(ip, gid, ip->i_gid);
   1481 	ip->i_uid = kauth_cred_geteuid(cnp->cn_cred);
   1482 	DIP_ASSIGN(ip, uid, ip->i_uid);
   1483 #if defined(LFS_QUOTA) || defined(LFS_QUOTA2)
   1484 	if ((error = lfs_chkiq(ip, 1, cnp->cn_cred, 0))) {
   1485 		ULFS_VFREE(tvp, ip->i_number, mode);
   1486 		vput(tvp);
   1487 		vput(dvp);
   1488 		return (error);
   1489 	}
   1490 #endif
   1491 	ip->i_flag |= IN_ACCESS | IN_CHANGE | IN_UPDATE;
   1492 	ip->i_mode = mode;
   1493 	DIP_ASSIGN(ip, mode, mode);
   1494 	tvp->v_type = IFTOVT(mode);	/* Rest init'd in getnewvnode(). */
   1495 	ip->i_nlink = 1;
   1496 	DIP_ASSIGN(ip, nlink, 1);
   1497 
   1498 	/* Authorize setting SGID if needed. */
   1499 	if (ip->i_mode & ISGID) {
   1500 		error = kauth_authorize_vnode(cnp->cn_cred, KAUTH_VNODE_WRITE_SECURITY,
   1501 		    tvp, NULL, genfs_can_chmod(tvp->v_type, cnp->cn_cred, ip->i_uid,
   1502 		    ip->i_gid, mode));
   1503 		if (error) {
   1504 			ip->i_mode &= ~ISGID;
   1505 			DIP_ASSIGN(ip, mode, ip->i_mode);
   1506 		}
   1507 	}
   1508 
   1509 	if (cnp->cn_flags & ISWHITEOUT) {
   1510 		ip->i_flags |= UF_OPAQUE;
   1511 		DIP_ASSIGN(ip, flags, ip->i_flags);
   1512 	}
   1513 
   1514 	/*
   1515 	 * Make sure inode goes to disk before directory entry.
   1516 	 */
   1517 	if ((error = ULFS_UPDATE(tvp, NULL, NULL, UPDATE_DIROP)) != 0)
   1518 		goto bad;
   1519 	newdir = pool_cache_get(ulfs_direct_cache, PR_WAITOK);
   1520 	ulfs_makedirentry(ip, cnp, newdir);
   1521 	error = ulfs_direnter(dvp, ulr, tvp, newdir, cnp, NULL);
   1522 	pool_cache_put(ulfs_direct_cache, newdir);
   1523 	if (error)
   1524 		goto bad;
   1525 	vput(dvp);
   1526 	*vpp = tvp;
   1527 	return (0);
   1528 
   1529  bad:
   1530 	/*
   1531 	 * Write error occurred trying to update the inode
   1532 	 * or the directory so must deallocate the inode.
   1533 	 */
   1534 	ip->i_nlink = 0;
   1535 	DIP_ASSIGN(ip, nlink, 0);
   1536 	ip->i_flag |= IN_CHANGE;
   1537 	/* If IN_ADIROP, account for it */
   1538 	ULFS_UNMARK_VNODE(tvp);
   1539 	tvp->v_type = VNON;		/* explodes later if VBLK */
   1540 	vput(tvp);
   1541 	vput(dvp);
   1542 	return (error);
   1543 }
   1544 
   1545 /*
   1546  * Allocate len bytes at offset off.
   1547  */
   1548 int
   1549 ulfs_gop_alloc(struct vnode *vp, off_t off, off_t len, int flags,
   1550     kauth_cred_t cred)
   1551 {
   1552         struct inode *ip = VTOI(vp);
   1553         int error, delta, bshift, bsize;
   1554         UVMHIST_FUNC("ulfs_gop_alloc"); UVMHIST_CALLED(ubchist);
   1555 
   1556         error = 0;
   1557         bshift = vp->v_mount->mnt_fs_bshift;
   1558         bsize = 1 << bshift;
   1559 
   1560         delta = off & (bsize - 1);
   1561         off -= delta;
   1562         len += delta;
   1563 
   1564         while (len > 0) {
   1565                 bsize = MIN(bsize, len);
   1566 
   1567                 error = ULFS_BALLOC(vp, off, bsize, cred, flags, NULL);
   1568                 if (error) {
   1569                         goto out;
   1570                 }
   1571 
   1572                 /*
   1573                  * increase file size now, ULFS_BALLOC() requires that
   1574                  * EOF be up-to-date before each call.
   1575                  */
   1576 
   1577                 if (ip->i_size < off + bsize) {
   1578                         UVMHIST_LOG(ubchist, "vp %p old 0x%x new 0x%x",
   1579                             vp, ip->i_size, off + bsize, 0);
   1580                         ip->i_size = off + bsize;
   1581 			DIP_ASSIGN(ip, size, ip->i_size);
   1582                 }
   1583 
   1584                 off += bsize;
   1585                 len -= bsize;
   1586         }
   1587 
   1588 out:
   1589 	return error;
   1590 }
   1591 
   1592 void
   1593 ulfs_gop_markupdate(struct vnode *vp, int flags)
   1594 {
   1595 	u_int32_t mask = 0;
   1596 
   1597 	if ((flags & GOP_UPDATE_ACCESSED) != 0) {
   1598 		mask = IN_ACCESS;
   1599 	}
   1600 	if ((flags & GOP_UPDATE_MODIFIED) != 0) {
   1601 		if (vp->v_type == VREG) {
   1602 			mask |= IN_CHANGE | IN_UPDATE;
   1603 		} else {
   1604 			mask |= IN_MODIFY;
   1605 		}
   1606 	}
   1607 	if (mask) {
   1608 		struct inode *ip = VTOI(vp);
   1609 
   1610 		ip->i_flag |= mask;
   1611 	}
   1612 }
   1613