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