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