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