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