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