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