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