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