Home | History | Annotate | Line # | Download | only in ext2fs
ext2fs_vnops.c revision 1.1
      1 /*	$NetBSD: ext2fs_vnops.c,v 1.1 1997/06/11 09:34:09 bouyer Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1997 Manuel Bouyer.
      5  * Copyright (c) 1982, 1986, 1989, 1993
      6  *	The Regents of the University of California.  All rights reserved.
      7  * (c) UNIX System Laboratories, Inc.
      8  * All or some portions of this file are derived from material licensed
      9  * to the University of California by American Telephone and Telegraph
     10  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
     11  * the permission of UNIX System Laboratories, Inc.
     12  *
     13  * Redistribution and use in source and binary forms, with or without
     14  * modification, are permitted provided that the following conditions
     15  * are met:
     16  * 1. Redistributions of source code must retain the above copyright
     17  *    notice, this list of conditions and the following disclaimer.
     18  * 2. Redistributions in binary form must reproduce the above copyright
     19  *    notice, this list of conditions and the following disclaimer in the
     20  *    documentation and/or other materials provided with the distribution.
     21  * 3. All advertising materials mentioning features or use of this software
     22  *    must display the following acknowledgement:
     23  *	This product includes software developed by the University of
     24  *	California, Berkeley and its contributors.
     25  * 4. Neither the name of the University nor the names of its contributors
     26  *    may be used to endorse or promote products derived from this software
     27  *    without specific prior written permission.
     28  *
     29  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     30  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     31  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     32  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     33  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     34  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     35  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     36  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     37  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     38  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     39  * SUCH DAMAGE.
     40  *
     41  *	@(#)ufs_vnops.c	8.14 (Berkeley) 10/26/94
     42  * Modified for ext2fs by Manuel Bouyer.
     43  */
     44 
     45 #include <sys/param.h>
     46 #include <sys/systm.h>
     47 #include <sys/resourcevar.h>
     48 #include <sys/kernel.h>
     49 #include <sys/file.h>
     50 #include <sys/stat.h>
     51 #include <sys/buf.h>
     52 #include <sys/proc.h>
     53 #include <sys/conf.h>
     54 #include <sys/mount.h>
     55 #include <sys/namei.h>
     56 #include <sys/vnode.h>
     57 #include <sys/lockf.h>
     58 #include <sys/malloc.h>
     59 #include <sys/signalvar.h>
     60 
     61 #include <vm/vm.h>
     62 
     63 #include <miscfs/fifofs/fifo.h>
     64 #include <miscfs/genfs/genfs.h>
     65 #include <miscfs/specfs/specdev.h>
     66 
     67 #include <ufs/ufs/quota.h>
     68 #include <ufs/ufs/inode.h>
     69 #include <ufs/ufs/ufs_extern.h>
     70 #include <ufs/ufs/ufsmount.h>
     71 
     72 #include <ufs/ext2fs/ext2fs.h>
     73 #include <ufs/ext2fs/ext2fs_extern.h>
     74 #include <ufs/ext2fs/ext2fs_dir.h>
     75 
     76 
     77 static int ext2fs_chmod
     78 	__P((struct vnode *, int, struct ucred *, struct proc *));
     79 static int ext2fs_chown
     80 	__P((struct vnode *, uid_t, gid_t, struct ucred *, struct proc *));
     81 
     82 union _qcvt {
     83 	int64_t	qcvt;
     84 	int32_t val[2];
     85 };
     86 #define SETHIGH(q, h) { \
     87 	union _qcvt tmp; \
     88 	tmp.qcvt = (q); \
     89 	tmp.val[_QUAD_HIGHWORD] = (h); \
     90 	(q) = tmp.qcvt; \
     91 }
     92 #define SETLOW(q, l) { \
     93 	union _qcvt tmp; \
     94 	tmp.qcvt = (q); \
     95 	tmp.val[_QUAD_LOWWORD] = (l); \
     96 	(q) = tmp.qcvt; \
     97 }
     98 
     99 /*
    100  * Create a regular file
    101  */
    102 int
    103 ext2fs_create(v)
    104 	void *v;
    105 {
    106 	struct vop_create_args /* {
    107 		struct vnode *a_dvp;
    108 		struct vnode **a_vpp;
    109 		struct componentname *a_cnp;
    110 		struct vattr *a_vap;
    111 	} */ *ap = v;
    112 	return
    113 		ext2fs_makeinode(MAKEIMODE(ap->a_vap->va_type, ap->a_vap->va_mode),
    114 			  ap->a_dvp, ap->a_vpp, ap->a_cnp);
    115 }
    116 
    117 /*
    118  * Mknod vnode call
    119  */
    120 /* ARGSUSED */
    121 int
    122 ext2fs_mknod(v)
    123 	void *v;
    124 {
    125 	struct vop_mknod_args /* {
    126 		struct vnode *a_dvp;
    127 		struct vnode **a_vpp;
    128 		struct componentname *a_cnp;
    129 		struct vattr *a_vap;
    130 	} */ *ap = v;
    131 	register struct vattr *vap = ap->a_vap;
    132 	register struct vnode **vpp = ap->a_vpp;
    133 	register struct inode *ip;
    134 	int error;
    135 
    136 	if ((error =
    137 		ext2fs_makeinode(MAKEIMODE(vap->va_type, vap->va_mode),
    138 		ap->a_dvp, vpp, ap->a_cnp)) != 0)
    139 		return (error);
    140 	ip = VTOI(*vpp);
    141 	ip->i_flag |= IN_ACCESS | IN_CHANGE | IN_UPDATE;
    142 	if (vap->va_rdev != VNOVAL) {
    143 		/*
    144 		 * Want to be able to use this to make badblock
    145 		 * inodes, so don't truncate the dev number.
    146 		 */
    147 		ip->i_din.e2fs_din.e2di_rdev = vap->va_rdev;
    148 	}
    149 	/*
    150 	 * Remove inode so that it will be reloaded by VFS_VGET and
    151 	 * checked to see if it is an alias of an existing entry in
    152 	 * the inode cache.
    153 	 */
    154 	vput(*vpp);
    155 	(*vpp)->v_type = VNON;
    156 	vgone(*vpp);
    157 	*vpp = 0;
    158 	return (0);
    159 }
    160 
    161 /*
    162  * Open called.
    163  *
    164  * Just check the APPEND flag.
    165  */
    166 /* ARGSUSED */
    167 int
    168 ext2fs_open(v)
    169 	void *v;
    170 {
    171 	struct vop_open_args /* {
    172 		struct vnode *a_vp;
    173 		int  a_mode;
    174 		struct ucred *a_cred;
    175 		struct proc *a_p;
    176 	} */ *ap = v;
    177 
    178 	/*
    179 	 * Files marked append-only must be opened for appending.
    180 	 */
    181 	if ((VTOI(ap->a_vp)->i_e2fs_flags & EXT2_APPEND) &&
    182 		(ap->a_mode & (FWRITE | O_APPEND)) == FWRITE)
    183 		return (EPERM);
    184 	return (0);
    185 }
    186 
    187 int
    188 ext2fs_access(v)
    189 	void *v;
    190 {
    191 	struct vop_access_args /* {
    192 		struct vnode *a_vp;
    193 		int  a_mode;
    194 		struct ucred *a_cred;
    195 		struct proc *a_p;
    196 	} */ *ap = v;
    197 	register struct vnode *vp = ap->a_vp;
    198 	register struct inode *ip = VTOI(vp);
    199 	mode_t mode = ap->a_mode;
    200 
    201 #ifdef DIAGNOSTIC
    202 	if (!VOP_ISLOCKED(vp)) {
    203 		vprint("ext2fs_access: not locked", vp);
    204 		panic("ext2fs_access: not locked");
    205 	}
    206 #endif
    207 
    208 	/* If immutable bit set, nobody gets to write it. */
    209 	if ((mode & VWRITE) && (ip->i_e2fs_flags & EXT2_IMMUTABLE))
    210 		return (EPERM);
    211 
    212 	return (vaccess(vp->v_type, ip->i_e2fs_mode & ALLPERMS,
    213 			ip->i_e2fs_uid, ip->i_e2fs_gid, mode, ap->a_cred));
    214 }
    215 
    216 /* ARGSUSED */
    217 int
    218 ext2fs_getattr(v)
    219 	void *v;
    220 {
    221 	struct vop_getattr_args /* {
    222 		struct vnode *a_vp;
    223 		struct vattr *a_vap;
    224 		struct ucred *a_cred;
    225 		struct proc *a_p;
    226 	} */ *ap = v;
    227 	register struct vnode *vp = ap->a_vp;
    228 	register struct inode *ip = VTOI(vp);
    229 	register struct vattr *vap = ap->a_vap;
    230 	struct timespec ts;
    231 
    232 	TIMEVAL_TO_TIMESPEC(&time, &ts);
    233 	EXT2FS_ITIMES(ip, &ts, &ts, &ts);
    234 	/*
    235 	 * Copy from inode table
    236 	 */
    237 	vap->va_fsid = ip->i_dev;
    238 	vap->va_fileid = ip->i_number;
    239 	vap->va_mode = ip->i_e2fs_mode & ALLPERMS;
    240 	vap->va_nlink = ip->i_e2fs_nlink;
    241 	vap->va_uid = ip->i_e2fs_uid;
    242 	vap->va_gid = ip->i_e2fs_gid;
    243 	vap->va_rdev = (dev_t)ip->i_din.e2fs_din.e2di_rdev;
    244 	vap->va_size = ip->i_e2fs_size;
    245 	vap->va_atime.tv_sec = ip->i_e2fs_atime;
    246 	vap->va_atime.tv_nsec = 0;
    247 	vap->va_mtime.tv_sec = ip->i_e2fs_mtime;
    248 	vap->va_mtime.tv_nsec = 0;
    249 	vap->va_ctime.tv_sec = ip->i_e2fs_ctime;
    250 	vap->va_ctime.tv_nsec = 0;
    251 #ifdef EXT2FS_SYSTEM_FLAGS
    252 	vap->va_flags = (ip->i_e2fs_flags & EXT2_APPEND) ? SF_APPEND : 0;
    253 	vap->va_flags |= (ip->i_e2fs_flags & EXT2_IMMUTABLE) ? SF_IMMUTABLE : 0;
    254 #else
    255 	vap->va_flags = (ip->i_e2fs_flags & EXT2_APPEND) ? UF_APPEND : 0;
    256 	vap->va_flags |= (ip->i_e2fs_flags & EXT2_IMMUTABLE) ? UF_IMMUTABLE : 0;
    257 #endif
    258 	vap->va_gen = ip->i_e2fs_gen;
    259 	/* this doesn't belong here */
    260 	if (vp->v_type == VBLK)
    261 		vap->va_blocksize = BLKDEV_IOSIZE;
    262 	else if (vp->v_type == VCHR)
    263 		vap->va_blocksize = MAXBSIZE;
    264 	else
    265 		vap->va_blocksize = vp->v_mount->mnt_stat.f_iosize;
    266 	vap->va_bytes = dbtob(ip->i_e2fs_nblock);
    267 	vap->va_type = vp->v_type;
    268 	vap->va_filerev = ip->i_modrev;
    269 	return (0);
    270 }
    271 
    272 /*
    273  * Set attribute vnode op. called from several syscalls
    274  */
    275 int
    276 ext2fs_setattr(v)
    277 	void *v;
    278 {
    279 	struct vop_setattr_args /* {
    280 		struct vnode *a_vp;
    281 		struct vattr *a_vap;
    282 		struct ucred *a_cred;
    283 		struct proc *a_p;
    284 	} */ *ap = v;
    285 	register struct vattr *vap = ap->a_vap;
    286 	register struct vnode *vp = ap->a_vp;
    287 	register struct inode *ip = VTOI(vp);
    288 	register struct ucred *cred = ap->a_cred;
    289 	register struct proc *p = ap->a_p;
    290 	int error;
    291 
    292 	/*
    293 	 * Check for unsettable attributes.
    294 	 */
    295 	if ((vap->va_type != VNON) || (vap->va_nlink != VNOVAL) ||
    296 		(vap->va_fsid != VNOVAL) || (vap->va_fileid != VNOVAL) ||
    297 		(vap->va_blocksize != VNOVAL) || (vap->va_rdev != VNOVAL) ||
    298 		((int)vap->va_bytes != VNOVAL) || (vap->va_gen != VNOVAL)) {
    299 		return (EINVAL);
    300 	}
    301 	if (vap->va_flags != VNOVAL) {
    302 		if (cred->cr_uid != ip->i_e2fs_uid &&
    303 			(error = suser(cred, &p->p_acflag)))
    304 			return (error);
    305 #ifdef EXT2FS_SYSTEM_FLAGS
    306 		if (cred->cr_uid == 0) {
    307 			if ((ip->i_e2fs_flags & (EXT2_APPEND | EXT2_IMMUTABLE)) &&
    308 				securelevel > 0)
    309 				return (EPERM);
    310 			ip->i_e2fs_flags &= ~(EXT2_APPEND | EXT2_IMMUTABLE);
    311 			ip->i_e2fs_flags |= (vap->va_flags & SF_APPEND) ? EXT2_APPEND : 0 |
    312 					(vap->va_flags & SF_IMMUTABLE) ? EXT2_IMMUTABLE: 0;
    313 		} else {
    314 			return (EPERM);
    315 		}
    316 #else
    317 		ip->i_e2fs_flags &= ~(EXT2_APPEND | EXT2_IMMUTABLE);
    318 		ip->i_e2fs_flags |= (vap->va_flags & UF_APPEND) ? EXT2_APPEND : 0 |
    319 				(vap->va_flags & UF_IMMUTABLE) ? EXT2_IMMUTABLE: 0;
    320 #endif
    321 		ip->i_flag |= IN_CHANGE;
    322 		if (vap->va_flags & (IMMUTABLE | APPEND))
    323 			return (0);
    324 	}
    325 	if (ip->i_e2fs_flags & (EXT2_APPEND | EXT2_IMMUTABLE))
    326 		return (EPERM);
    327 	/*
    328 	 * Go through the fields and update iff not VNOVAL.
    329 	 */
    330 	if (vap->va_uid != (uid_t)VNOVAL || vap->va_gid != (gid_t)VNOVAL) {
    331 		error = ext2fs_chown(vp, vap->va_uid, vap->va_gid, cred, p);
    332 		if (error)
    333 			return (error);
    334 	}
    335 	if (vap->va_size != VNOVAL) {
    336 		if (vp->v_type == VDIR)
    337 			return (EISDIR);
    338 		error = VOP_TRUNCATE(vp, vap->va_size, 0, cred, p);
    339 		if (error)
    340 			return (error);
    341 	}
    342 	ip = VTOI(vp);
    343 	if (vap->va_atime.tv_sec != VNOVAL || vap->va_mtime.tv_sec != VNOVAL) {
    344 		if (cred->cr_uid != ip->i_e2fs_uid &&
    345 			(error = suser(cred, &p->p_acflag)) &&
    346 			((vap->va_vaflags & VA_UTIMES_NULL) == 0 ||
    347 			(error = VOP_ACCESS(vp, VWRITE, cred, p))))
    348 			return (error);
    349 		if (vap->va_atime.tv_sec != VNOVAL)
    350 			if (!(vp->v_mount->mnt_flag & MNT_NOATIME))
    351 				ip->i_flag |= IN_ACCESS;
    352 		if (vap->va_mtime.tv_sec != VNOVAL)
    353 			ip->i_flag |= IN_CHANGE | IN_UPDATE;
    354 		error = VOP_UPDATE(vp, &vap->va_atime, &vap->va_mtime, 1);
    355 		if (error)
    356 			return (error);
    357 	}
    358 	error = 0;
    359 	if (vap->va_mode != (mode_t)VNOVAL)
    360 		error = ext2fs_chmod(vp, (int)vap->va_mode, cred, p);
    361 	return (error);
    362 }
    363 
    364 /*
    365  * Change the mode on a file.
    366  * Inode must be locked before calling.
    367  */
    368 static int
    369 ext2fs_chmod(vp, mode, cred, p)
    370 	register struct vnode *vp;
    371 	register int mode;
    372 	register struct ucred *cred;
    373 	struct proc *p;
    374 {
    375 	register struct inode *ip = VTOI(vp);
    376 	int error;
    377 
    378 	if (cred->cr_uid != ip->i_e2fs_uid &&
    379 		(error = suser(cred, &p->p_acflag)))
    380 		return (error);
    381 	if (cred->cr_uid) {
    382 		if (vp->v_type != VDIR && (mode & S_ISTXT))
    383 			return (EFTYPE);
    384 		if (!groupmember(ip->i_e2fs_gid, cred) && (mode & ISGID))
    385 			return (EPERM);
    386 	}
    387 	ip->i_e2fs_mode &= ~ALLPERMS;
    388 	ip->i_e2fs_mode |= (mode & ALLPERMS);
    389 	ip->i_flag |= IN_CHANGE;
    390 	if ((vp->v_flag & VTEXT) && (ip->i_e2fs_mode & S_ISTXT) == 0)
    391 		(void) vnode_pager_uncache(vp);
    392 	return (0);
    393 }
    394 
    395 /*
    396  * Perform chown operation on inode ip;
    397  * inode must be locked prior to call.
    398  */
    399 static int
    400 ext2fs_chown(vp, uid, gid, cred, p)
    401 	register struct vnode *vp;
    402 	uid_t uid;
    403 	gid_t gid;
    404 	struct ucred *cred;
    405 	struct proc *p;
    406 {
    407 	register struct inode *ip = VTOI(vp);
    408 	uid_t ouid;
    409 	gid_t ogid;
    410 	int error = 0;
    411 
    412 	if (uid == (uid_t)VNOVAL)
    413 		uid = ip->i_e2fs_uid;
    414 	if (gid == (gid_t)VNOVAL)
    415 		gid = ip->i_e2fs_gid;
    416 	/*
    417 	 * If we don't own the file, are trying to change the owner
    418 	 * of the file, or are not a member of the target group,
    419 	 * the caller must be superuser or the call fails.
    420 	 */
    421 	if ((cred->cr_uid != ip->i_e2fs_uid || uid != ip->i_e2fs_uid ||
    422 		(gid != ip->i_e2fs_gid && !groupmember((gid_t)gid, cred))) &&
    423 		(error = suser(cred, &p->p_acflag)))
    424 		return (error);
    425 	ogid = ip->i_e2fs_gid;
    426 	ouid = ip->i_e2fs_uid;
    427 
    428 	ip->i_e2fs_gid = gid;
    429 	ip->i_e2fs_uid = uid;
    430 	if (ouid != uid || ogid != gid)
    431 		ip->i_flag |= IN_CHANGE;
    432 	if (ouid != uid && cred->cr_uid != 0)
    433 		ip->i_e2fs_mode &= ~ISUID;
    434 	if (ogid != gid && cred->cr_uid != 0)
    435 		ip->i_e2fs_mode &= ~ISGID;
    436 	return (0);
    437 }
    438 
    439 int
    440 ext2fs_remove(v)
    441 	void *v;
    442 {
    443 	struct vop_remove_args /* {
    444 		struct vnode *a_dvp;
    445 		struct vnode *a_vp;
    446 		struct componentname *a_cnp;
    447 	} */ *ap = v;
    448 	register struct inode *ip;
    449 	register struct vnode *vp = ap->a_vp;
    450 	register struct vnode *dvp = ap->a_dvp;
    451 	int error;
    452 
    453 	if (vp->v_type == VDIR) {
    454 		error = EISDIR;
    455 		goto out;
    456 	}
    457 	ip = VTOI(vp);
    458 	if ((ip->i_e2fs_flags & (EXT2_IMMUTABLE | EXT2_APPEND)) ||
    459 		(VTOI(dvp)->i_e2fs_flags & EXT2_APPEND)) {
    460 		error = EPERM;
    461 		goto out;
    462 	}
    463 	error = ext2fs_dirremove(dvp, ap->a_cnp);
    464 	if (error == 0) {
    465 		ip->i_e2fs_nlink--;
    466 		ip->i_flag |= IN_CHANGE;
    467 	}
    468 out:
    469 	if (dvp == vp)
    470 		vrele(vp);
    471 	else
    472 		vput(vp);
    473 	vput(dvp);
    474 	return (error);
    475 }
    476 
    477 /*
    478  * link vnode call
    479  */
    480 int
    481 ext2fs_link(v)
    482 	void *v;
    483 {
    484 	struct vop_link_args /* {
    485 		struct vnode *a_dvp;
    486 		struct vnode *a_vp;
    487 		struct componentname *a_cnp;
    488 	} */ *ap = v;
    489 	register struct vnode *dvp = ap->a_dvp;
    490 	register struct vnode *vp = ap->a_vp;
    491 	register struct componentname *cnp = ap->a_cnp;
    492 	register struct inode *ip;
    493 	struct timespec ts;
    494 	int error;
    495 
    496 #ifdef DIAGNOSTIC
    497 	if ((cnp->cn_flags & HASBUF) == 0)
    498 		panic("ext2fs_link: no name");
    499 #endif
    500 	if (vp->v_type == VDIR) {
    501 		VOP_ABORTOP(dvp, cnp);
    502 		error = EISDIR;
    503 		goto out2;
    504 	}
    505 	if (dvp->v_mount != vp->v_mount) {
    506 		VOP_ABORTOP(dvp, cnp);
    507 		error = EXDEV;
    508 		goto out2;
    509 	}
    510 	if (dvp != vp && (error = VOP_LOCK(vp))) {
    511 		VOP_ABORTOP(dvp, cnp);
    512 		goto out2;
    513 	}
    514 	ip = VTOI(vp);
    515 	if ((nlink_t)ip->i_e2fs_nlink >= LINK_MAX) {
    516 		VOP_ABORTOP(dvp, cnp);
    517 		error = EMLINK;
    518 		goto out1;
    519 	}
    520 	if (ip->i_e2fs_flags & (EXT2_IMMUTABLE | EXT2_APPEND)) {
    521 		VOP_ABORTOP(dvp, cnp);
    522 		error = EPERM;
    523 		goto out1;
    524 	}
    525 	ip->i_e2fs_nlink++;
    526 	ip->i_flag |= IN_CHANGE;
    527 	TIMEVAL_TO_TIMESPEC(&time, &ts);
    528 	error = VOP_UPDATE(vp, &ts, &ts, 1);
    529 	if (!error)
    530 		error = ext2fs_direnter(ip, dvp, cnp);
    531 	if (error) {
    532 		ip->i_e2fs_nlink--;
    533 		ip->i_flag |= IN_CHANGE;
    534 	}
    535 	FREE(cnp->cn_pnbuf, M_NAMEI);
    536 out1:
    537 	if (dvp != vp)
    538 		VOP_UNLOCK(vp);
    539 out2:
    540 	vput(dvp);
    541 	return (error);
    542 }
    543 
    544 /*
    545  * Rename system call.
    546  * 	rename("foo", "bar");
    547  * is essentially
    548  *	unlink("bar");
    549  *	link("foo", "bar");
    550  *	unlink("foo");
    551  * but ``atomically''.  Can't do full commit without saving state in the
    552  * inode on disk which isn't feasible at this time.  Best we can do is
    553  * always guarantee the target exists.
    554  *
    555  * Basic algorithm is:
    556  *
    557  * 1) Bump link count on source while we're linking it to the
    558  *    target.  This also ensure the inode won't be deleted out
    559  *    from underneath us while we work (it may be truncated by
    560  *    a concurrent `trunc' or `open' for creation).
    561  * 2) Link source to destination.  If destination already exists,
    562  *    delete it first.
    563  * 3) Unlink source reference to inode if still around. If a
    564  *    directory was moved and the parent of the destination
    565  *    is different from the source, patch the ".." entry in the
    566  *    directory.
    567  */
    568 int
    569 ext2fs_rename(v)
    570 	void *v;
    571 {
    572 	struct vop_rename_args  /* {
    573 		struct vnode *a_fdvp;
    574 		struct vnode *a_fvp;
    575 		struct componentname *a_fcnp;
    576 		struct vnode *a_tdvp;
    577 		struct vnode *a_tvp;
    578 		struct componentname *a_tcnp;
    579 	} */ *ap = v;
    580 	struct vnode *tvp = ap->a_tvp;
    581 	register struct vnode *tdvp = ap->a_tdvp;
    582 	struct vnode *fvp = ap->a_fvp;
    583 	register struct vnode *fdvp = ap->a_fdvp;
    584 	register struct componentname *tcnp = ap->a_tcnp;
    585 	register struct componentname *fcnp = ap->a_fcnp;
    586 	register struct inode *ip, *xp, *dp;
    587 	struct ext2fs_dirtemplate dirbuf;
    588 	struct timespec ts;
    589 	int doingdirectory = 0, oldparent = 0, newparent = 0;
    590 	int error = 0;
    591 	u_char namlen;
    592 
    593 #ifdef DIAGNOSTIC
    594 	if ((tcnp->cn_flags & HASBUF) == 0 ||
    595 	    (fcnp->cn_flags & HASBUF) == 0)
    596 		panic("ext2fs_rename: no name");
    597 #endif
    598 	/*
    599 	 * Check for cross-device rename.
    600 	 */
    601 	if ((fvp->v_mount != tdvp->v_mount) ||
    602 	    (tvp && (fvp->v_mount != tvp->v_mount))) {
    603 		error = EXDEV;
    604 abortit:
    605 		VOP_ABORTOP(tdvp, tcnp); /* XXX, why not in NFS? */
    606 		if (tdvp == tvp)
    607 			vrele(tdvp);
    608 		else
    609 			vput(tdvp);
    610 		if (tvp)
    611 			vput(tvp);
    612 		VOP_ABORTOP(fdvp, fcnp); /* XXX, why not in NFS? */
    613 		vrele(fdvp);
    614 		vrele(fvp);
    615 		return (error);
    616 	}
    617 
    618 	/*
    619 	 * Check if just deleting a link name.
    620 	 */
    621 	if (tvp && ((VTOI(tvp)->i_e2fs_flags & (EXT2_IMMUTABLE | EXT2_APPEND)) ||
    622 	    (VTOI(tdvp)->i_e2fs_flags & EXT2_APPEND))) {
    623 		error = EPERM;
    624 		goto abortit;
    625 	}
    626 	if (fvp == tvp) {
    627 		if (fvp->v_type == VDIR) {
    628 			error = EINVAL;
    629 			goto abortit;
    630 		}
    631 
    632 		/* Release destination completely. */
    633 		VOP_ABORTOP(tdvp, tcnp);
    634 		vput(tdvp);
    635 		vput(tvp);
    636 
    637 		/* Delete source. */
    638 		vrele(fdvp);
    639 		vrele(fvp);
    640 		fcnp->cn_flags &= ~MODMASK;
    641 		fcnp->cn_flags |= LOCKPARENT | LOCKLEAF;
    642 		if ((fcnp->cn_flags & SAVESTART) == 0)
    643 			panic("ext2fs_rename: lost from startdir");
    644 		fcnp->cn_nameiop = DELETE;
    645 		(void) relookup(fdvp, &fvp, fcnp);
    646 		return (VOP_REMOVE(fdvp, fvp, fcnp));
    647 	}
    648 	if ((error = VOP_LOCK(fvp)) != 0)
    649 		goto abortit;
    650 	dp = VTOI(fdvp);
    651 	ip = VTOI(fvp);
    652 	if ((ip->i_e2fs_flags & (EXT2_IMMUTABLE | EXT2_APPEND)) ||
    653 		(dp->i_e2fs_flags & EXT2_APPEND)) {
    654 		VOP_UNLOCK(fvp);
    655 		error = EPERM;
    656 		goto abortit;
    657 	}
    658 	if ((ip->i_e2fs_mode & IFMT) == IFDIR) {
    659         error = VOP_ACCESS(fvp, VWRITE, tcnp->cn_cred, tcnp->cn_proc);
    660         if (!error && tvp)
    661                 error = VOP_ACCESS(tvp, VWRITE, tcnp->cn_cred, tcnp->cn_proc);
    662         if (error) {
    663                 VOP_UNLOCK(fvp);
    664                 error = EACCES;
    665                 goto abortit;
    666         }
    667 		/*
    668 		 * Avoid ".", "..", and aliases of "." for obvious reasons.
    669 		 */
    670 		if ((fcnp->cn_namelen == 1 && fcnp->cn_nameptr[0] == '.') ||
    671 		    dp == ip || (fcnp->cn_flags&ISDOTDOT) ||
    672 		    (ip->i_flag & IN_RENAME)) {
    673 			VOP_UNLOCK(fvp);
    674 			error = EINVAL;
    675 			goto abortit;
    676 		}
    677 		ip->i_flag |= IN_RENAME;
    678 		oldparent = dp->i_number;
    679 		doingdirectory++;
    680 	}
    681 	vrele(fdvp);
    682 
    683 	/*
    684 	 * When the target exists, both the directory
    685 	 * and target vnodes are returned locked.
    686 	 */
    687 	dp = VTOI(tdvp);
    688 	xp = NULL;
    689 	if (tvp)
    690 		xp = VTOI(tvp);
    691 
    692 	/*
    693 	 * 1) Bump link count while we're moving stuff
    694 	 *    around.  If we crash somewhere before
    695 	 *    completing our work, the link count
    696 	 *    may be wrong, but correctable.
    697 	 */
    698 	ip->i_e2fs_nlink++;
    699 	ip->i_flag |= IN_CHANGE;
    700 	TIMEVAL_TO_TIMESPEC(&time, &ts);
    701 	if ((error = VOP_UPDATE(fvp, &ts, &ts, 1)) != 0) {
    702 		VOP_UNLOCK(fvp);
    703 		goto bad;
    704 	}
    705 
    706 	/*
    707 	 * If ".." must be changed (ie the directory gets a new
    708 	 * parent) then the source directory must not be in the
    709 	 * directory heirarchy above the target, as this would
    710 	 * orphan everything below the source directory. Also
    711 	 * the user must have write permission in the source so
    712 	 * as to be able to change "..". We must repeat the call
    713 	 * to namei, as the parent directory is unlocked by the
    714 	 * call to checkpath().
    715 	 */
    716 	error = VOP_ACCESS(fvp, VWRITE, tcnp->cn_cred, tcnp->cn_proc);
    717 	VOP_UNLOCK(fvp);
    718 	if (oldparent != dp->i_number)
    719 		newparent = dp->i_number;
    720 	if (doingdirectory && newparent) {
    721 		if (error)	/* write access check above */
    722 			goto bad;
    723 		if (xp != NULL)
    724 			vput(tvp);
    725 		error = ext2fs_checkpath(ip, dp, tcnp->cn_cred);
    726 		if (error != 0)
    727 			goto out;
    728 		if ((tcnp->cn_flags & SAVESTART) == 0)
    729 			panic("ext2fs_rename: lost to startdir");
    730 		if ((error = relookup(tdvp, &tvp, tcnp)) != 0)
    731 			goto out;
    732 		dp = VTOI(tdvp);
    733 		xp = NULL;
    734 		if (tvp)
    735 			xp = VTOI(tvp);
    736 	}
    737 	/*
    738 	 * 2) If target doesn't exist, link the target
    739 	 *    to the source and unlink the source.
    740 	 *    Otherwise, rewrite the target directory
    741 	 *    entry to reference the source inode and
    742 	 *    expunge the original entry's existence.
    743 	 */
    744 	if (xp == NULL) {
    745 		if (dp->i_dev != ip->i_dev)
    746 			panic("rename: EXDEV");
    747 		/*
    748 		 * Account for ".." in new directory.
    749 		 * When source and destination have the same
    750 		 * parent we don't fool with the link count.
    751 		 */
    752 		if (doingdirectory && newparent) {
    753 			if ((nlink_t)dp->i_e2fs_nlink >= LINK_MAX) {
    754 				error = EMLINK;
    755 				goto bad;
    756 			}
    757 			dp->i_e2fs_nlink++;
    758 			dp->i_flag |= IN_CHANGE;
    759 			if ((error = VOP_UPDATE(tdvp, &ts, &ts, 1)) != 0)
    760 				goto bad;
    761 		}
    762 		error = ext2fs_direnter(ip, tdvp, tcnp);
    763 		if (error != 0) {
    764 			if (doingdirectory && newparent) {
    765 				dp->i_e2fs_nlink--;
    766 				dp->i_flag |= IN_CHANGE;
    767 				(void)VOP_UPDATE(tdvp, &ts, &ts, 1);
    768 			}
    769 			goto bad;
    770 		}
    771 		vput(tdvp);
    772 	} else {
    773 		if (xp->i_dev != dp->i_dev || xp->i_dev != ip->i_dev)
    774 			panic("rename: EXDEV");
    775 		/*
    776 		 * Short circuit rename(foo, foo).
    777 		 */
    778 		if (xp->i_number == ip->i_number)
    779 			panic("rename: same file");
    780 		/*
    781 		 * If the parent directory is "sticky", then the user must
    782 		 * own the parent directory, or the destination of the rename,
    783 		 * otherwise the destination may not be changed (except by
    784 		 * root). This implements append-only directories.
    785 		 */
    786 		if ((dp->i_e2fs_mode & S_ISTXT) && tcnp->cn_cred->cr_uid != 0 &&
    787 		    tcnp->cn_cred->cr_uid != dp->i_e2fs_uid &&
    788 		    xp->i_e2fs_uid != tcnp->cn_cred->cr_uid) {
    789 			error = EPERM;
    790 			goto bad;
    791 		}
    792 		/*
    793 		 * Target must be empty if a directory and have no links
    794 		 * to it. Also, ensure source and target are compatible
    795 		 * (both directories, or both not directories).
    796 		 */
    797 		if ((xp->i_e2fs_mode & IFMT) == IFDIR) {
    798 			if (!ext2fs_dirempty(xp, dp->i_number, tcnp->cn_cred) ||
    799 				xp->i_e2fs_nlink > 2) {
    800 				error = ENOTEMPTY;
    801 				goto bad;
    802 			}
    803 			if (!doingdirectory) {
    804 				error = ENOTDIR;
    805 				goto bad;
    806 			}
    807 			cache_purge(tdvp);
    808 		} else if (doingdirectory) {
    809 			error = EISDIR;
    810 			goto bad;
    811 		}
    812 		error = ext2fs_dirrewrite(dp, ip, tcnp);
    813 		if (error != 0)
    814 			goto bad;
    815 		/*
    816 		 * If the target directory is in the same
    817 		 * directory as the source directory,
    818 		 * decrement the link count on the parent
    819 		 * of the target directory.
    820 		 */
    821 		 if (doingdirectory && !newparent) {
    822 			dp->i_e2fs_nlink--;
    823 			dp->i_flag |= IN_CHANGE;
    824 		}
    825 		vput(tdvp);
    826 		/*
    827 		 * Adjust the link count of the target to
    828 		 * reflect the dirrewrite above.  If this is
    829 		 * a directory it is empty and there are
    830 		 * no links to it, so we can squash the inode and
    831 		 * any space associated with it.  We disallowed
    832 		 * renaming over top of a directory with links to
    833 		 * it above, as the remaining link would point to
    834 		 * a directory without "." or ".." entries.
    835 		 */
    836 		xp->i_e2fs_nlink--;
    837 		if (doingdirectory) {
    838 			if (--xp->i_e2fs_nlink != 0)
    839 				panic("rename: linked directory");
    840 			error = VOP_TRUNCATE(tvp, (off_t)0, IO_SYNC,
    841 			    tcnp->cn_cred, tcnp->cn_proc);
    842 		}
    843 		xp->i_flag |= IN_CHANGE;
    844 		vput(tvp);
    845 		xp = NULL;
    846 	}
    847 
    848 	/*
    849 	 * 3) Unlink the source.
    850 	 */
    851 	fcnp->cn_flags &= ~MODMASK;
    852 	fcnp->cn_flags |= LOCKPARENT | LOCKLEAF;
    853 	if ((fcnp->cn_flags & SAVESTART) == 0)
    854 		panic("ext2fs_rename: lost from startdir");
    855 	(void) relookup(fdvp, &fvp, fcnp);
    856 	if (fvp != NULL) {
    857 		xp = VTOI(fvp);
    858 		dp = VTOI(fdvp);
    859 	} else {
    860 		/*
    861 		 * From name has disappeared.
    862 		 */
    863 		if (doingdirectory)
    864 			panic("ext2fs_rename: lost dir entry");
    865 		vrele(ap->a_fvp);
    866 		return (0);
    867 	}
    868 	/*
    869 	 * Ensure that the directory entry still exists and has not
    870 	 * changed while the new name has been entered. If the source is
    871 	 * a file then the entry may have been unlinked or renamed. In
    872 	 * either case there is no further work to be done. If the source
    873 	 * is a directory then it cannot have been rmdir'ed; its link
    874 	 * count of three would cause a rmdir to fail with ENOTEMPTY.
    875 	 * The IRENAME flag ensures that it cannot be moved by another
    876 	 * rename.
    877 	 */
    878 	if (xp != ip) {
    879 		if (doingdirectory)
    880 			panic("ext2fs_rename: lost dir entry");
    881 	} else {
    882 		/*
    883 		 * If the source is a directory with a
    884 		 * new parent, the link count of the old
    885 		 * parent directory must be decremented
    886 		 * and ".." set to point to the new parent.
    887 		 */
    888 		if (doingdirectory && newparent) {
    889 			dp->i_e2fs_nlink--;
    890 			dp->i_flag |= IN_CHANGE;
    891 			error = vn_rdwr(UIO_READ, fvp, (caddr_t)&dirbuf,
    892 				sizeof (struct ext2fs_dirtemplate), (off_t)0,
    893 				UIO_SYSSPACE, IO_NODELOCKED,
    894 				tcnp->cn_cred, (int *)0, (struct proc *)0);
    895 			if (error == 0) {
    896 					namlen = dirbuf.dotdot_namlen;
    897 				if (namlen != 2 ||
    898 				    dirbuf.dotdot_name[0] != '.' ||
    899 				    dirbuf.dotdot_name[1] != '.') {
    900 					ufs_dirbad(xp, (doff_t)12,
    901 					    "ext2fs_rename: mangled dir");
    902 				} else {
    903 					dirbuf.dotdot_ino = newparent;
    904 					(void) vn_rdwr(UIO_WRITE, fvp,
    905 					    (caddr_t)&dirbuf,
    906 					    sizeof (struct dirtemplate),
    907 					    (off_t)0, UIO_SYSSPACE,
    908 					    IO_NODELOCKED|IO_SYNC,
    909 					    tcnp->cn_cred, (int *)0,
    910 					    (struct proc *)0);
    911 					cache_purge(fdvp);
    912 				}
    913 			}
    914 		}
    915 		error = ext2fs_dirremove(fdvp, fcnp);
    916 		if (!error) {
    917 			xp->i_e2fs_nlink--;
    918 			xp->i_flag |= IN_CHANGE;
    919 		}
    920 		xp->i_flag &= ~IN_RENAME;
    921 	}
    922 	if (dp)
    923 		vput(fdvp);
    924 	if (xp)
    925 		vput(fvp);
    926 	vrele(ap->a_fvp);
    927 	return (error);
    928 
    929 bad:
    930 	if (xp)
    931 		vput(ITOV(xp));
    932 	vput(ITOV(dp));
    933 out:
    934 	if (doingdirectory)
    935 		ip->i_flag &= ~IN_RENAME;
    936 	if (VOP_LOCK(fvp) == 0) {
    937 		ip->i_e2fs_nlink--;
    938 		ip->i_flag |= IN_CHANGE;
    939 		vput(fvp);
    940 	} else
    941 		vrele(fvp);
    942 	return (error);
    943 }
    944 
    945 /*
    946  * A virgin directory (no blushing please).
    947  */
    948 static struct ext2fs_dirtemplate mastertemplate = {
    949 	0, 12, 1, ".",
    950 	0, - 12, 2, ".." /* XXX -12 should be e2fs_bsize-12 */
    951 };
    952 
    953 /*
    954  * Mkdir system call
    955  */
    956 int
    957 ext2fs_mkdir(v)
    958 	void *v;
    959 {
    960 	struct vop_mkdir_args /* {
    961 		struct vnode *a_dvp;
    962 		struct vnode **a_vpp;
    963 		struct componentname *a_cnp;
    964 		struct vattr *a_vap;
    965 	} */ *ap = v;
    966 	register struct vnode *dvp = ap->a_dvp;
    967 	register struct vattr *vap = ap->a_vap;
    968 	register struct componentname *cnp = ap->a_cnp;
    969 	register struct inode *ip, *dp;
    970 	struct vnode *tvp;
    971 	struct ext2fs_dirtemplate dirtemplate, *dtp;
    972 	struct timespec ts;
    973 	int error, dmode;
    974 
    975 #ifdef DIAGNOSTIC
    976 	if ((cnp->cn_flags & HASBUF) == 0)
    977 		panic("ext2fs_mkdir: no name");
    978 #endif
    979 	dp = VTOI(dvp);
    980 	if ((nlink_t)dp->i_e2fs_nlink >= LINK_MAX) {
    981 		error = EMLINK;
    982 		goto out;
    983 	}
    984 	dmode = vap->va_mode & ACCESSPERMS;
    985 	dmode |= IFDIR;
    986 	/*
    987 	 * Must simulate part of ext2fs_makeinode here to acquire the inode,
    988 	 * but not have it entered in the parent directory. The entry is
    989 	 * made later after writing "." and ".." entries.
    990 	 */
    991 	if ((error = VOP_VALLOC(dvp, dmode, cnp->cn_cred, &tvp)) != 0)
    992 		goto out;
    993 	ip = VTOI(tvp);
    994 	ip->i_e2fs_uid = cnp->cn_cred->cr_uid;
    995 	ip->i_e2fs_gid = dp->i_e2fs_gid;
    996 	ip->i_flag |= IN_ACCESS | IN_CHANGE | IN_UPDATE;
    997 	ip->i_e2fs_mode = dmode;
    998 	tvp->v_type = VDIR;	/* Rest init'd in getnewvnode(). */
    999 	ip->i_e2fs_nlink = 2;
   1000 	TIMEVAL_TO_TIMESPEC(&time, &ts);
   1001 	error = VOP_UPDATE(tvp, &ts, &ts, 1);
   1002 
   1003 	/*
   1004 	 * Bump link count in parent directory
   1005 	 * to reflect work done below.  Should
   1006 	 * be done before reference is created
   1007 	 * so reparation is possible if we crash.
   1008 	 */
   1009 	dp->i_e2fs_nlink++;
   1010 	dp->i_flag |= IN_CHANGE;
   1011 	if ((error = VOP_UPDATE(dvp, &ts, &ts, 1)) != 0)
   1012 		goto bad;
   1013 
   1014 	/* Initialize directory with "." and ".." from static template. */
   1015 	dtp = &mastertemplate;
   1016 	dirtemplate = *dtp;
   1017 	dirtemplate.dot_ino = ip->i_number;
   1018 	dirtemplate.dotdot_ino = dp->i_number;
   1019 	/* Correct reclen of second entry */
   1020     dirtemplate.dotdot_reclen = VTOI(dvp)->i_e2fs->e2fs_bsize - 12;
   1021 	error = vn_rdwr(UIO_WRITE, tvp, (caddr_t)&dirtemplate,
   1022 	    sizeof (dirtemplate), (off_t)0, UIO_SYSSPACE,
   1023 	    IO_NODELOCKED|IO_SYNC, cnp->cn_cred, (int *)0, (struct proc *)0);
   1024 	if (error) {
   1025 		dp->i_e2fs_nlink--;
   1026 		dp->i_flag |= IN_CHANGE;
   1027 		goto bad;
   1028 	}
   1029 	if (VTOI(dvp)->i_e2fs->e2fs_bsize >
   1030 							VFSTOUFS(dvp->v_mount)->um_mountp->mnt_stat.f_bsize)
   1031 		panic("ext2fs_mkdir: blksize"); /* XXX should grow with balloc() */
   1032 	else {
   1033 		ip->i_e2fs_size = VTOI(dvp)->i_e2fs->e2fs_bsize;
   1034 		ip->i_flag |= IN_CHANGE;
   1035 	}
   1036 
   1037 	/* Directory set up, now install it's entry in the parent directory. */
   1038 	error = ext2fs_direnter(ip, dvp, cnp);
   1039 	if (error != 0) {
   1040 		dp->i_e2fs_nlink--;
   1041 		dp->i_flag |= IN_CHANGE;
   1042 	}
   1043 bad:
   1044 	/*
   1045 	 * No need to do an explicit VOP_TRUNCATE here, vrele will do this
   1046 	 * for us because we set the link count to 0.
   1047 	 */
   1048 	if (error) {
   1049 		ip->i_e2fs_nlink = 0;
   1050 		ip->i_flag |= IN_CHANGE;
   1051 		vput(tvp);
   1052 	} else
   1053 		*ap->a_vpp = tvp;
   1054 out:
   1055 	FREE(cnp->cn_pnbuf, M_NAMEI);
   1056 	vput(dvp);
   1057 	return (error);
   1058 }
   1059 
   1060 /*
   1061  * Rmdir system call.
   1062  */
   1063 int
   1064 ext2fs_rmdir(v)
   1065 	void *v;
   1066 {
   1067 	struct vop_rmdir_args /* {
   1068 		struct vnode *a_dvp;
   1069 		struct vnode *a_vp;
   1070 		struct componentname *a_cnp;
   1071 	} */ *ap = v;
   1072 	register struct vnode *vp = ap->a_vp;
   1073 	register struct vnode *dvp = ap->a_dvp;
   1074 	register struct componentname *cnp = ap->a_cnp;
   1075 	register struct inode *ip, *dp;
   1076 	int error;
   1077 
   1078 	ip = VTOI(vp);
   1079 	dp = VTOI(dvp);
   1080 	/*
   1081 	 * No rmdir "." please.
   1082 	 */
   1083 	if (dp == ip) {
   1084 		vrele(dvp);
   1085 		vput(vp);
   1086 		return (EINVAL);
   1087 	}
   1088 	/*
   1089 	 * Verify the directory is empty (and valid).
   1090 	 * (Rmdir ".." won't be valid since
   1091 	 *  ".." will contain a reference to
   1092 	 *  the current directory and thus be
   1093 	 *  non-empty.)
   1094 	 */
   1095 	error = 0;
   1096 	if (ip->i_e2fs_nlink != 2 ||
   1097 	    !ext2fs_dirempty(ip, dp->i_number, cnp->cn_cred)) {
   1098 		error = ENOTEMPTY;
   1099 		goto out;
   1100 	}
   1101 	if ((dp->i_e2fs_flags & EXT2_APPEND) ||
   1102 				 (ip->i_e2fs_flags & (EXT2_IMMUTABLE | EXT2_APPEND))) {
   1103 		error = EPERM;
   1104 		goto out;
   1105 	}
   1106 	/*
   1107 	 * Delete reference to directory before purging
   1108 	 * inode.  If we crash in between, the directory
   1109 	 * will be reattached to lost+found,
   1110 	 */
   1111 	error = ext2fs_dirremove(dvp, cnp);
   1112 	if (error != 0)
   1113 		goto out;
   1114 	dp->i_e2fs_nlink--;
   1115 	dp->i_flag |= IN_CHANGE;
   1116 	cache_purge(dvp);
   1117 	vput(dvp);
   1118 	dvp = NULL;
   1119 	/*
   1120 	 * Truncate inode.  The only stuff left
   1121 	 * in the directory is "." and "..".  The
   1122 	 * "." reference is inconsequential since
   1123 	 * we're quashing it.  The ".." reference
   1124 	 * has already been adjusted above.  We've
   1125 	 * removed the "." reference and the reference
   1126 	 * in the parent directory, but there may be
   1127 	 * other hard links so decrement by 2 and
   1128 	 * worry about them later.
   1129 	 */
   1130 	ip->i_e2fs_nlink -= 2;
   1131 	error = VOP_TRUNCATE(vp, (off_t)0, IO_SYNC, cnp->cn_cred,
   1132 	    cnp->cn_proc);
   1133 	cache_purge(ITOV(ip));
   1134 out:
   1135 	if (dvp)
   1136 		vput(dvp);
   1137 	vput(vp);
   1138 	return (error);
   1139 }
   1140 
   1141 /*
   1142  * symlink -- make a symbolic link
   1143  */
   1144 int
   1145 ext2fs_symlink(v)
   1146 	void *v;
   1147 {
   1148 	struct vop_symlink_args /* {
   1149 		struct vnode *a_dvp;
   1150 		struct vnode **a_vpp;
   1151 		struct componentname *a_cnp;
   1152 		struct vattr *a_vap;
   1153 		char *a_target;
   1154 	} */ *ap = v;
   1155 	register struct vnode *vp, **vpp = ap->a_vpp;
   1156 	register struct inode *ip;
   1157 	int len, error;
   1158 
   1159 	error = ext2fs_makeinode(IFLNK | ap->a_vap->va_mode, ap->a_dvp,
   1160 			      vpp, ap->a_cnp);
   1161 	if (error)
   1162 		return (error);
   1163 	vp = *vpp;
   1164 	len = strlen(ap->a_target);
   1165 	if (len < vp->v_mount->mnt_maxsymlinklen) {
   1166 		ip = VTOI(vp);
   1167 		bcopy(ap->a_target, (char *)ip->i_din.e2fs_din.e2di_shortlink, len);
   1168 		ip->i_e2fs_size = len;
   1169 		ip->i_flag |= IN_CHANGE | IN_UPDATE;
   1170 	} else
   1171 		error = vn_rdwr(UIO_WRITE, vp, ap->a_target, len, (off_t)0,
   1172 		    UIO_SYSSPACE, IO_NODELOCKED, ap->a_cnp->cn_cred, (int *)0,
   1173 		    (struct proc *)0);
   1174 	vput(vp);
   1175 	return (error);
   1176 }
   1177 
   1178 /*
   1179  * Return target name of a symbolic link
   1180  */
   1181 int
   1182 ext2fs_readlink(v)
   1183 	void *v;
   1184 {
   1185 	struct vop_readlink_args /* {
   1186 		struct vnode *a_vp;
   1187 		struct uio *a_uio;
   1188 		struct ucred *a_cred;
   1189 	} */ *ap = v;
   1190 	register struct vnode *vp = ap->a_vp;
   1191 	register struct inode *ip = VTOI(vp);
   1192 	int isize;
   1193 
   1194 	isize = ip->i_e2fs_size;
   1195 	if (isize < vp->v_mount->mnt_maxsymlinklen ||
   1196 	    (vp->v_mount->mnt_maxsymlinklen == 0 && ip->i_e2fs_nblock == 0)) {
   1197 		uiomove((char *)ip->i_din.e2fs_din.e2di_shortlink, isize, ap->a_uio);
   1198 		return (0);
   1199 	}
   1200 	return (VOP_READ(vp, ap->a_uio, 0, ap->a_cred));
   1201 }
   1202 
   1203 /*
   1204  * Advisory record locking support
   1205  */
   1206 int
   1207 ext2fs_advlock(v)
   1208 	void *v;
   1209 {
   1210 	struct vop_advlock_args /* {
   1211 		struct vnode *a_vp;
   1212 		caddr_t  a_id;
   1213 		int  a_op;
   1214 		struct flock *a_fl;
   1215 		int  a_flags;
   1216 	} */ *ap = v;
   1217 	register struct inode *ip = VTOI(ap->a_vp);
   1218 
   1219 	return (lf_advlock(&ip->i_lockf, ip->i_e2fs_size, ap->a_id, ap->a_op,
   1220 	    ap->a_fl, ap->a_flags));
   1221 }
   1222 
   1223 /*
   1224  * Initialize the vnode associated with a new inode, handle aliased
   1225  * vnodes.
   1226  */
   1227 int
   1228 ext2fs_vinit(mntp, specops, fifoops, vpp)
   1229 	struct mount *mntp;
   1230 	int (**specops) __P((void *));
   1231 	int (**fifoops) __P((void *));
   1232 	struct vnode **vpp;
   1233 {
   1234 	struct inode *ip;
   1235 	struct vnode *vp, *nvp;
   1236 
   1237 	vp = *vpp;
   1238 	ip = VTOI(vp);
   1239 	switch(vp->v_type = IFTOVT(ip->i_e2fs_mode)) {
   1240 	case VCHR:
   1241 	case VBLK:
   1242 		vp->v_op = specops;
   1243 		if ((nvp = checkalias(vp, ip->i_din.e2fs_din.e2di_rdev, mntp))
   1244 			!= NULL) {
   1245 			/*
   1246 			 * Discard unneeded vnode, but save its inode.
   1247 			 */
   1248 			ufs_ihashrem(ip);
   1249 			VOP_UNLOCK(vp);
   1250 			nvp->v_data = vp->v_data;
   1251 			vp->v_data = NULL;
   1252 			vp->v_op = spec_vnodeop_p;
   1253 			vrele(vp);
   1254 			vgone(vp);
   1255 			/*
   1256 			 * Reinitialize aliased inode.
   1257 			 */
   1258 			vp = nvp;
   1259 			ip->i_vnode = vp;
   1260 			ufs_ihashins(ip);
   1261 		}
   1262 		break;
   1263 	case VFIFO:
   1264 #ifdef FIFO
   1265 		vp->v_op = fifoops;
   1266 		break;
   1267 #else
   1268 		return (EOPNOTSUPP);
   1269 #endif
   1270 	case VNON:
   1271 	case VBAD:
   1272 	case VSOCK:
   1273 	case VLNK:
   1274 	case VDIR:
   1275 	case VREG:
   1276 		break;
   1277 	}
   1278 	if (ip->i_number == ROOTINO)
   1279                 vp->v_flag |= VROOT;
   1280 	/*
   1281 	 * Initialize modrev times
   1282 	 */
   1283 	SETHIGH(ip->i_modrev, mono_time.tv_sec);
   1284 	SETLOW(ip->i_modrev, mono_time.tv_usec * 4294);
   1285 	*vpp = vp;
   1286 	return (0);
   1287 }
   1288 
   1289 /*
   1290  * Allocate a new inode.
   1291  */
   1292 int
   1293 ext2fs_makeinode(mode, dvp, vpp, cnp)
   1294 	int mode;
   1295 	struct vnode *dvp;
   1296 	struct vnode **vpp;
   1297 	struct componentname *cnp;
   1298 {
   1299 	register struct inode *ip, *pdir;
   1300 	struct timespec ts;
   1301 	struct vnode *tvp;
   1302 	int error;
   1303 
   1304 	pdir = VTOI(dvp);
   1305 #ifdef DIAGNOSTIC
   1306 	if ((cnp->cn_flags & HASBUF) == 0)
   1307 		panic("ext2fs_makeinode: no name");
   1308 #endif
   1309 	*vpp = NULL;
   1310 	if ((mode & IFMT) == 0)
   1311 		mode |= IFREG;
   1312 
   1313 	if ((error = VOP_VALLOC(dvp, mode, cnp->cn_cred, &tvp)) != 0) {
   1314 		free(cnp->cn_pnbuf, M_NAMEI);
   1315 		vput(dvp);
   1316 		return (error);
   1317 	}
   1318 	ip = VTOI(tvp);
   1319 	ip->i_e2fs_gid = pdir->i_e2fs_gid;
   1320 	ip->i_e2fs_uid = cnp->cn_cred->cr_uid;
   1321 	ip->i_flag |= IN_ACCESS | IN_CHANGE | IN_UPDATE;
   1322 	ip->i_e2fs_mode = mode;
   1323 	tvp->v_type = IFTOVT(mode);	/* Rest init'd in getnewvnode(). */
   1324 	ip->i_e2fs_nlink = 1;
   1325 	if ((ip->i_e2fs_mode & ISGID) &&
   1326 		!groupmember(ip->i_e2fs_gid, cnp->cn_cred) &&
   1327 	    suser(cnp->cn_cred, NULL))
   1328 		ip->i_e2fs_mode &= ~ISGID;
   1329 
   1330 	/*
   1331 	 * Make sure inode goes to disk before directory entry.
   1332 	 */
   1333 	TIMEVAL_TO_TIMESPEC(&time, &ts);
   1334 	if ((error = VOP_UPDATE(tvp, &ts, &ts, 1)) != 0)
   1335 		goto bad;
   1336 	error = ext2fs_direnter(ip, dvp, cnp);
   1337 	if (error != 0)
   1338 		goto bad;
   1339 	if ((cnp->cn_flags & SAVESTART) == 0)
   1340 		FREE(cnp->cn_pnbuf, M_NAMEI);
   1341 	vput(dvp);
   1342 	*vpp = tvp;
   1343 	return (0);
   1344 
   1345 bad:
   1346 	/*
   1347 	 * Write error occurred trying to update the inode
   1348 	 * or the directory so must deallocate the inode.
   1349 	 */
   1350 	free(cnp->cn_pnbuf, M_NAMEI);
   1351 	vput(dvp);
   1352 	ip->i_e2fs_nlink = 0;
   1353 	ip->i_flag |= IN_CHANGE;
   1354 	vput(tvp);
   1355 	return (error);
   1356 }
   1357 
   1358 /*
   1359  * Reclaim an inode so that it can be used for other purposes.
   1360  */
   1361 int
   1362 ext2fs_reclaim(v)
   1363 	void *v;
   1364 {
   1365 	struct vop_reclaim_args /* {
   1366 		struct vnode *a_vp;
   1367 	} */ *ap = v;
   1368 	register struct vnode *vp = ap->a_vp;
   1369 	struct inode *ip;
   1370 	extern int prtactive;
   1371 
   1372     if (prtactive && vp->v_usecount != 0)
   1373         vprint("ext2fs_reclaim: pushing active", vp);
   1374     /*
   1375      * Remove the inode from its hash chain.
   1376      */
   1377     ip = VTOI(vp);
   1378     ufs_ihashrem(ip);
   1379     /*
   1380      * Purge old data structures associated with the inode.
   1381      */
   1382     cache_purge(vp);
   1383     if (ip->i_devvp) {
   1384         vrele(ip->i_devvp);
   1385         ip->i_devvp = 0;
   1386     }
   1387 
   1388 	FREE(vp->v_data, M_EXT2FSNODE);
   1389 	vp->v_data = NULL;
   1390 	return (0);
   1391 }
   1392 
   1393 /* Global vfs data structures for ext2fs. */
   1394 int (**ext2fs_vnodeop_p) __P((void *));
   1395 struct vnodeopv_entry_desc ext2fs_vnodeop_entries[] = {
   1396 	{ &vop_default_desc, vn_default_error },
   1397 	{ &vop_lookup_desc, ext2fs_lookup },	/* lookup */
   1398 	{ &vop_create_desc, ext2fs_create },	/* create */
   1399 	{ &vop_mknod_desc, ext2fs_mknod },		/* mknod */
   1400 	{ &vop_open_desc, ext2fs_open },		/* open */
   1401 	{ &vop_close_desc, ufs_close },			/* close */
   1402 	{ &vop_access_desc, ext2fs_access },	/* access */
   1403 	{ &vop_getattr_desc, ext2fs_getattr },	/* getattr */
   1404 	{ &vop_setattr_desc, ext2fs_setattr },	/* setattr */
   1405 	{ &vop_read_desc, ext2fs_read },		/* read */
   1406 	{ &vop_write_desc, ext2fs_write },		/* write */
   1407 	{ &vop_lease_desc, ufs_lease_check },	/* lease */
   1408 	{ &vop_ioctl_desc, ufs_ioctl },			/* ioctl */
   1409 	{ &vop_poll_desc, ufs_poll },			/* poll */
   1410 	{ &vop_mmap_desc, ufs_mmap },			/* mmap */
   1411 	{ &vop_fsync_desc, ext2fs_fsync },		/* fsync */
   1412 	{ &vop_seek_desc, ufs_seek },			/* seek */
   1413 	{ &vop_remove_desc, ext2fs_remove },	/* remove */
   1414 	{ &vop_link_desc, ext2fs_link },		/* link */
   1415 	{ &vop_rename_desc, ext2fs_rename },	/* rename */
   1416 	{ &vop_mkdir_desc, ext2fs_mkdir },		/* mkdir */
   1417 	{ &vop_rmdir_desc, ext2fs_rmdir },		/* rmdir */
   1418 	{ &vop_symlink_desc, ext2fs_symlink },	/* symlink */
   1419 	{ &vop_readdir_desc, ext2fs_readdir },	/* readdir */
   1420 	{ &vop_readlink_desc, ext2fs_readlink },/* readlink */
   1421 	{ &vop_abortop_desc, ufs_abortop },		/* abortop */
   1422 	{ &vop_inactive_desc, ext2fs_inactive },/* inactive */
   1423 	{ &vop_reclaim_desc, ext2fs_reclaim },	/* reclaim */
   1424 	{ &vop_lock_desc, ufs_lock },			/* lock */
   1425 	{ &vop_unlock_desc, ufs_unlock },		/* unlock */
   1426 	{ &vop_bmap_desc, ext2fs_bmap },		/* bmap */
   1427 	{ &vop_strategy_desc, ufs_strategy },	/* strategy */
   1428 	{ &vop_print_desc, ufs_print },			/* print */
   1429 	{ &vop_islocked_desc, ufs_islocked },	/* islocked */
   1430 	{ &vop_pathconf_desc, ufs_pathconf },	/* pathconf */
   1431 	{ &vop_advlock_desc, ext2fs_advlock },	/* advlock */
   1432 	{ &vop_blkatoff_desc, ext2fs_blkatoff },/* blkatoff */
   1433 	{ &vop_valloc_desc, ext2fs_valloc },	/* valloc */
   1434 	{ &vop_vfree_desc, ext2fs_vfree },		/* vfree */
   1435 	{ &vop_truncate_desc, ext2fs_truncate },/* truncate */
   1436 	{ &vop_update_desc, ext2fs_update },	/* update */
   1437 	{ &vop_bwrite_desc, vn_bwrite },		/* bwrite */
   1438 	{ (struct vnodeop_desc*)NULL, (int(*) __P((void*)))NULL }
   1439 };
   1440 struct vnodeopv_desc ext2fs_vnodeop_opv_desc =
   1441 	{ &ext2fs_vnodeop_p, ext2fs_vnodeop_entries };
   1442 
   1443 int (**ext2fs_specop_p) __P((void *));
   1444 struct vnodeopv_entry_desc ext2fs_specop_entries[] = {
   1445 	{ &vop_default_desc, vn_default_error },
   1446 	{ &vop_lookup_desc, spec_lookup },		/* lookup */
   1447 	{ &vop_create_desc, spec_create },		/* create */
   1448 	{ &vop_mknod_desc, spec_mknod },		/* mknod */
   1449 	{ &vop_open_desc, spec_open },			/* open */
   1450 	{ &vop_close_desc, ufsspec_close },		/* close */
   1451 	{ &vop_access_desc, ext2fs_access },	/* access */
   1452 	{ &vop_getattr_desc, ext2fs_getattr },	/* getattr */
   1453 	{ &vop_setattr_desc, ext2fs_setattr },	/* setattr */
   1454 	{ &vop_read_desc, ufsspec_read },		/* read */
   1455 	{ &vop_write_desc, ufsspec_write },		/* write */
   1456 	{ &vop_lease_desc, spec_lease_check },	/* lease */
   1457 	{ &vop_ioctl_desc, spec_ioctl },		/* ioctl */
   1458 	{ &vop_poll_desc, spec_poll },			/* poll */
   1459 	{ &vop_mmap_desc, spec_mmap },			/* mmap */
   1460 	{ &vop_fsync_desc, ext2fs_fsync },		/* fsync */
   1461 	{ &vop_seek_desc, spec_seek },			/* seek */
   1462 	{ &vop_remove_desc, spec_remove },		/* remove */
   1463 	{ &vop_link_desc, spec_link },			/* link */
   1464 	{ &vop_rename_desc, spec_rename },		/* rename */
   1465 	{ &vop_mkdir_desc, spec_mkdir },		/* mkdir */
   1466 	{ &vop_rmdir_desc, spec_rmdir },		/* rmdir */
   1467 	{ &vop_symlink_desc, spec_symlink },	/* symlink */
   1468 	{ &vop_readdir_desc, spec_readdir },	/* readdir */
   1469 	{ &vop_readlink_desc, spec_readlink },	/* readlink */
   1470 	{ &vop_abortop_desc, spec_abortop },	/* abortop */
   1471 	{ &vop_inactive_desc, ext2fs_inactive },/* inactive */
   1472 	{ &vop_reclaim_desc, ext2fs_reclaim },	/* reclaim */
   1473 	{ &vop_lock_desc, ufs_lock },			/* lock */
   1474 	{ &vop_unlock_desc, ufs_unlock },		/* unlock */
   1475 	{ &vop_bmap_desc, spec_bmap },			/* bmap */
   1476 	{ &vop_strategy_desc, spec_strategy },	/* strategy */
   1477 	{ &vop_print_desc, ufs_print },			/* print */
   1478 	{ &vop_islocked_desc, ufs_islocked },	/* islocked */
   1479 	{ &vop_pathconf_desc, spec_pathconf },	/* pathconf */
   1480 	{ &vop_advlock_desc, spec_advlock },	/* advlock */
   1481 	{ &vop_blkatoff_desc, spec_blkatoff },	/* blkatoff */
   1482 	{ &vop_valloc_desc, spec_valloc },		/* valloc */
   1483 	{ &vop_vfree_desc, ext2fs_vfree },		/* vfree */
   1484 	{ &vop_truncate_desc, spec_truncate },	/* truncate */
   1485 	{ &vop_update_desc, ext2fs_update },	/* update */
   1486 	{ &vop_bwrite_desc, vn_bwrite },		/* bwrite */
   1487 	{ (struct vnodeop_desc*)NULL, (int(*) __P((void *)))NULL }
   1488 };
   1489 struct vnodeopv_desc ext2fs_specop_opv_desc =
   1490 	{ &ext2fs_specop_p, ext2fs_specop_entries };
   1491 
   1492 #ifdef FIFO
   1493 int (**ext2fs_fifoop_p) __P((void *));
   1494 struct vnodeopv_entry_desc ext2fs_fifoop_entries[] = {
   1495 	{ &vop_default_desc, vn_default_error },
   1496 	{ &vop_lookup_desc, fifo_lookup },		/* lookup */
   1497 	{ &vop_create_desc, fifo_create },		/* create */
   1498 	{ &vop_mknod_desc, fifo_mknod },		/* mknod */
   1499 	{ &vop_open_desc, fifo_open },			/* open */
   1500 	{ &vop_close_desc, ufsfifo_close },		/* close */
   1501 	{ &vop_access_desc, ext2fs_access },	/* access */
   1502 	{ &vop_getattr_desc, ext2fs_getattr },	/* getattr */
   1503 	{ &vop_setattr_desc, ext2fs_setattr },	/* setattr */
   1504 	{ &vop_read_desc, ufsfifo_read },		/* read */
   1505 	{ &vop_write_desc, ufsfifo_write },		/* write */
   1506 	{ &vop_lease_desc, fifo_lease_check },	/* lease */
   1507 	{ &vop_ioctl_desc, fifo_ioctl },		/* ioctl */
   1508 	{ &vop_poll_desc, fifo_poll },			/* poll */
   1509 	{ &vop_mmap_desc, fifo_mmap },			/* mmap */
   1510 	{ &vop_fsync_desc, ext2fs_fsync },		/* fsync */
   1511 	{ &vop_seek_desc, fifo_seek },			/* seek */
   1512 	{ &vop_remove_desc, fifo_remove },		/* remove */
   1513 	{ &vop_link_desc, fifo_link },			/* link */
   1514 	{ &vop_rename_desc, fifo_rename },		/* rename */
   1515 	{ &vop_mkdir_desc, fifo_mkdir },		/* mkdir */
   1516 	{ &vop_rmdir_desc, fifo_rmdir },		/* rmdir */
   1517 	{ &vop_symlink_desc, fifo_symlink },	/* symlink */
   1518 	{ &vop_readdir_desc, fifo_readdir },	/* readdir */
   1519 	{ &vop_readlink_desc, fifo_readlink },	/* readlink */
   1520 	{ &vop_abortop_desc, fifo_abortop },	/* abortop */
   1521 	{ &vop_inactive_desc, ext2fs_inactive },/* inactive */
   1522 	{ &vop_reclaim_desc, ext2fs_reclaim },	/* reclaim */
   1523 	{ &vop_lock_desc, ufs_lock },			/* lock */
   1524 	{ &vop_unlock_desc, ufs_unlock },		/* unlock */
   1525 	{ &vop_bmap_desc, fifo_bmap },			/* bmap */
   1526 	{ &vop_strategy_desc, fifo_strategy },	/* strategy */
   1527 	{ &vop_print_desc, ufs_print },			/* print */
   1528 	{ &vop_islocked_desc, ufs_islocked },	/* islocked */
   1529 	{ &vop_pathconf_desc, fifo_pathconf },	/* pathconf */
   1530 	{ &vop_advlock_desc, fifo_advlock },	/* advlock */
   1531 	{ &vop_blkatoff_desc, fifo_blkatoff },	/* blkatoff */
   1532 	{ &vop_valloc_desc, fifo_valloc },		/* valloc */
   1533 	{ &vop_vfree_desc, ext2fs_vfree },		/* vfree */
   1534 	{ &vop_truncate_desc, fifo_truncate },	/* truncate */
   1535 	{ &vop_update_desc, ext2fs_update },	/* update */
   1536 	{ &vop_bwrite_desc, vn_bwrite },		/* bwrite */
   1537 	{ (struct vnodeop_desc*)NULL, (int(*) __P((void *)))NULL }
   1538 };
   1539 struct vnodeopv_desc ext2fs_fifoop_opv_desc =
   1540 	{ &ext2fs_fifoop_p, ext2fs_fifoop_entries };
   1541 #endif /* FIFO */
   1542