xref: /openbsd/sys/ufs/ext2fs/ext2fs_vnops.c (revision 1330d409)
1*1330d409Smatthew /*	$OpenBSD: ext2fs_vnops.c,v 1.53 2010/08/02 02:03:22 matthew Exp $	*/
21414b0faSart /*	$NetBSD: ext2fs_vnops.c,v 1.1 1997/06/11 09:34:09 bouyer Exp $	*/
35ac2d602Sdownsj 
45ac2d602Sdownsj /*
51f3ff51cSdownsj  * Copyright (c) 1997 Manuel Bouyer.
65ac2d602Sdownsj  * Copyright (c) 1982, 1986, 1989, 1993
75ac2d602Sdownsj  *	The Regents of the University of California.  All rights reserved.
85ac2d602Sdownsj  * (c) UNIX System Laboratories, Inc.
95ac2d602Sdownsj  * All or some portions of this file are derived from material licensed
105ac2d602Sdownsj  * to the University of California by American Telephone and Telegraph
115ac2d602Sdownsj  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
125ac2d602Sdownsj  * the permission of UNIX System Laboratories, Inc.
135ac2d602Sdownsj  *
145ac2d602Sdownsj  * Redistribution and use in source and binary forms, with or without
155ac2d602Sdownsj  * modification, are permitted provided that the following conditions
165ac2d602Sdownsj  * are met:
175ac2d602Sdownsj  * 1. Redistributions of source code must retain the above copyright
185ac2d602Sdownsj  *    notice, this list of conditions and the following disclaimer.
195ac2d602Sdownsj  * 2. Redistributions in binary form must reproduce the above copyright
205ac2d602Sdownsj  *    notice, this list of conditions and the following disclaimer in the
215ac2d602Sdownsj  *    documentation and/or other materials provided with the distribution.
2229295d1cSmillert  * 3. Neither the name of the University nor the names of its contributors
235ac2d602Sdownsj  *    may be used to endorse or promote products derived from this software
245ac2d602Sdownsj  *    without specific prior written permission.
255ac2d602Sdownsj  *
265ac2d602Sdownsj  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
275ac2d602Sdownsj  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
285ac2d602Sdownsj  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
295ac2d602Sdownsj  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
305ac2d602Sdownsj  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
315ac2d602Sdownsj  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
325ac2d602Sdownsj  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
335ac2d602Sdownsj  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
345ac2d602Sdownsj  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
355ac2d602Sdownsj  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
365ac2d602Sdownsj  * SUCH DAMAGE.
375ac2d602Sdownsj  *
385ac2d602Sdownsj  *	@(#)ufs_vnops.c	8.14 (Berkeley) 10/26/94
391f3ff51cSdownsj  * Modified for ext2fs by Manuel Bouyer.
405ac2d602Sdownsj  */
415ac2d602Sdownsj 
425ac2d602Sdownsj #include <sys/param.h>
435ac2d602Sdownsj #include <sys/systm.h>
445ac2d602Sdownsj #include <sys/resourcevar.h>
455ac2d602Sdownsj #include <sys/kernel.h>
465ac2d602Sdownsj #include <sys/file.h>
475ac2d602Sdownsj #include <sys/stat.h>
485ac2d602Sdownsj #include <sys/buf.h>
495ac2d602Sdownsj #include <sys/proc.h>
505ac2d602Sdownsj #include <sys/conf.h>
515ac2d602Sdownsj #include <sys/mount.h>
525ac2d602Sdownsj #include <sys/namei.h>
535ac2d602Sdownsj #include <sys/vnode.h>
545ac2d602Sdownsj #include <sys/lockf.h>
555ac2d602Sdownsj #include <sys/malloc.h>
56f5ee6277Sjasoni #include <sys/pool.h>
575ac2d602Sdownsj #include <sys/signalvar.h>
585ac2d602Sdownsj 
59f5ee6277Sjasoni #include <uvm/uvm_extern.h>
60f5ee6277Sjasoni 
615ac2d602Sdownsj #include <miscfs/fifofs/fifo.h>
625ac2d602Sdownsj #include <miscfs/specfs/specdev.h>
635ac2d602Sdownsj 
645ac2d602Sdownsj #include <ufs/ufs/quota.h>
655ac2d602Sdownsj #include <ufs/ufs/inode.h>
665ac2d602Sdownsj #include <ufs/ufs/ufs_extern.h>
675ac2d602Sdownsj #include <ufs/ufs/ufsmount.h>
685ac2d602Sdownsj 
695ac2d602Sdownsj #include <ufs/ext2fs/ext2fs.h>
705ac2d602Sdownsj #include <ufs/ext2fs/ext2fs_extern.h>
715ac2d602Sdownsj #include <ufs/ext2fs/ext2fs_dir.h>
725ac2d602Sdownsj 
73cc2fc615Smillert static int ext2fs_chmod(struct vnode *, mode_t, struct ucred *, struct proc *);
74c4071fd1Smillert static int ext2fs_chown(struct vnode *, uid_t, gid_t, struct ucred *, struct proc *);
755ac2d602Sdownsj 
765ac2d602Sdownsj /*
775ac2d602Sdownsj  * Create a regular file
785ac2d602Sdownsj  */
795ac2d602Sdownsj int
805f64cd9cSjasper ext2fs_create(void *v)
815ac2d602Sdownsj {
8299bc9d31Sderaadt 	struct vop_create_args *ap = v;
83f5ee6277Sjasoni 	return ext2fs_makeinode(MAKEIMODE(ap->a_vap->va_type,
84f5ee6277Sjasoni 					  ap->a_vap->va_mode),
855ac2d602Sdownsj 			  	ap->a_dvp, ap->a_vpp, ap->a_cnp);
865ac2d602Sdownsj }
875ac2d602Sdownsj 
885ac2d602Sdownsj /*
895ac2d602Sdownsj  * Mknod vnode call
905ac2d602Sdownsj  */
915ac2d602Sdownsj /* ARGSUSED */
925ac2d602Sdownsj int
935f64cd9cSjasper ext2fs_mknod(void *v)
945ac2d602Sdownsj {
9599bc9d31Sderaadt 	struct vop_mknod_args *ap = v;
965f64cd9cSjasper 	struct vattr *vap = ap->a_vap;
975f64cd9cSjasper 	struct vnode **vpp = ap->a_vpp;
985f64cd9cSjasper 	struct inode *ip;
995ac2d602Sdownsj 	int error;
1005ac2d602Sdownsj 
1015ac2d602Sdownsj 	if ((error =
1025ac2d602Sdownsj 		ext2fs_makeinode(MAKEIMODE(vap->va_type, vap->va_mode),
1035ac2d602Sdownsj 		ap->a_dvp, vpp, ap->a_cnp)) != 0)
1045ac2d602Sdownsj 		return (error);
1055ac2d602Sdownsj 	ip = VTOI(*vpp);
1065ac2d602Sdownsj 	ip->i_flag |= IN_ACCESS | IN_CHANGE | IN_UPDATE;
1075ac2d602Sdownsj 	if (vap->va_rdev != VNOVAL) {
1085ac2d602Sdownsj 		/*
1095ac2d602Sdownsj 		 * Want to be able to use this to make badblock
1105ac2d602Sdownsj 		 * inodes, so don't truncate the dev number.
1115ac2d602Sdownsj 		 */
1122374824dSpedro 		ip->i_e2din->e2di_rdev = h2fs32(vap->va_rdev);
1135ac2d602Sdownsj 	}
1145ac2d602Sdownsj 	/*
1155ac2d602Sdownsj 	 * Remove inode so that it will be reloaded by VFS_VGET and
1165ac2d602Sdownsj 	 * checked to see if it is an alias of an existing entry in
1175ac2d602Sdownsj 	 * the inode cache.
1185ac2d602Sdownsj 	 */
1195ac2d602Sdownsj 	vput(*vpp);
1205ac2d602Sdownsj 	(*vpp)->v_type = VNON;
1215ac2d602Sdownsj 	vgone(*vpp);
1225ac2d602Sdownsj 	*vpp = 0;
1235ac2d602Sdownsj 	return (0);
1245ac2d602Sdownsj }
1255ac2d602Sdownsj 
1265ac2d602Sdownsj /*
1275ac2d602Sdownsj  * Open called.
1285ac2d602Sdownsj  *
1295ac2d602Sdownsj  * Just check the APPEND flag.
1305ac2d602Sdownsj  */
1315ac2d602Sdownsj /* ARGSUSED */
1325ac2d602Sdownsj int
1335f64cd9cSjasper ext2fs_open(void *v)
1345ac2d602Sdownsj {
13599bc9d31Sderaadt 	struct vop_open_args *ap = v;
1365ac2d602Sdownsj 
1375ac2d602Sdownsj 	/*
1385ac2d602Sdownsj 	 * Files marked append-only must be opened for appending.
1395ac2d602Sdownsj 	 */
1405ac2d602Sdownsj 	if ((VTOI(ap->a_vp)->i_e2fs_flags & EXT2_APPEND) &&
1415ac2d602Sdownsj 		(ap->a_mode & (FWRITE | O_APPEND)) == FWRITE)
1425ac2d602Sdownsj 		return (EPERM);
1435ac2d602Sdownsj 	return (0);
1445ac2d602Sdownsj }
1455ac2d602Sdownsj 
1465ac2d602Sdownsj int
1475f64cd9cSjasper ext2fs_access(void *v)
1485ac2d602Sdownsj {
14999bc9d31Sderaadt 	struct vop_access_args *ap = v;
1505f64cd9cSjasper 	struct vnode *vp = ap->a_vp;
1515f64cd9cSjasper 	struct inode *ip = VTOI(vp);
1525ac2d602Sdownsj 	mode_t mode = ap->a_mode;
1535ac2d602Sdownsj 
1545ac2d602Sdownsj 	/* If immutable bit set, nobody gets to write it. */
1555ac2d602Sdownsj 	if ((mode & VWRITE) && (ip->i_e2fs_flags & EXT2_IMMUTABLE))
1565ac2d602Sdownsj 		return (EPERM);
1575ac2d602Sdownsj 
158141c07a8Smillert 	return (vaccess(vp->v_type, ip->i_e2fs_mode, ip->i_e2fs_uid,
159141c07a8Smillert 			ip->i_e2fs_gid, mode, ap->a_cred));
1605ac2d602Sdownsj }
1615ac2d602Sdownsj 
1625ac2d602Sdownsj /* ARGSUSED */
1635ac2d602Sdownsj int
1645f64cd9cSjasper ext2fs_getattr(void *v)
1655ac2d602Sdownsj {
16699bc9d31Sderaadt 	struct vop_getattr_args *ap = v;
1675f64cd9cSjasper 	struct vnode *vp = ap->a_vp;
1685f64cd9cSjasper 	struct inode *ip = VTOI(vp);
1695f64cd9cSjasper 	struct vattr *vap = ap->a_vap;
170c2475275Stholo 	struct timeval tv;
1715ac2d602Sdownsj 
172c2475275Stholo 	getmicrotime(&tv);
173c2475275Stholo 	EXT2FS_ITIMES(ip, &tv, &tv);
1745ac2d602Sdownsj 	/*
1755ac2d602Sdownsj 	 * Copy from inode table
1765ac2d602Sdownsj 	 */
1775ac2d602Sdownsj 	vap->va_fsid = ip->i_dev;
1785ac2d602Sdownsj 	vap->va_fileid = ip->i_number;
1791f3ff51cSdownsj 	vap->va_mode = ip->i_e2fs_mode & ALLPERMS;
1805ac2d602Sdownsj 	vap->va_nlink = ip->i_e2fs_nlink;
1815ac2d602Sdownsj 	vap->va_uid = ip->i_e2fs_uid;
1825ac2d602Sdownsj 	vap->va_gid = ip->i_e2fs_gid;
1832374824dSpedro 	vap->va_rdev = (dev_t)fs2h32(ip->i_e2din->e2di_rdev);
1844dcbbbb0Sniallo 	vap->va_size = ext2fs_size(ip);
1855ac2d602Sdownsj 	vap->va_atime.tv_sec = ip->i_e2fs_atime;
1865ac2d602Sdownsj 	vap->va_atime.tv_nsec = 0;
1875ac2d602Sdownsj 	vap->va_mtime.tv_sec = ip->i_e2fs_mtime;
1885ac2d602Sdownsj 	vap->va_mtime.tv_nsec = 0;
1895ac2d602Sdownsj 	vap->va_ctime.tv_sec = ip->i_e2fs_ctime;
1905ac2d602Sdownsj 	vap->va_ctime.tv_nsec = 0;
1915ac2d602Sdownsj #ifdef EXT2FS_SYSTEM_FLAGS
1925ac2d602Sdownsj 	vap->va_flags = (ip->i_e2fs_flags & EXT2_APPEND) ? SF_APPEND : 0;
1935ac2d602Sdownsj 	vap->va_flags |= (ip->i_e2fs_flags & EXT2_IMMUTABLE) ? SF_IMMUTABLE : 0;
1945ac2d602Sdownsj #else
1955ac2d602Sdownsj 	vap->va_flags = (ip->i_e2fs_flags & EXT2_APPEND) ? UF_APPEND : 0;
1965ac2d602Sdownsj 	vap->va_flags |= (ip->i_e2fs_flags & EXT2_IMMUTABLE) ? UF_IMMUTABLE : 0;
1975ac2d602Sdownsj #endif
1985ac2d602Sdownsj 	vap->va_gen = ip->i_e2fs_gen;
1995ac2d602Sdownsj 	/* this doesn't belong here */
2005ac2d602Sdownsj 	if (vp->v_type == VBLK)
2015ac2d602Sdownsj 		vap->va_blocksize = BLKDEV_IOSIZE;
2025ac2d602Sdownsj 	else if (vp->v_type == VCHR)
2035ac2d602Sdownsj 		vap->va_blocksize = MAXBSIZE;
2045ac2d602Sdownsj 	else
2055ac2d602Sdownsj 		vap->va_blocksize = vp->v_mount->mnt_stat.f_iosize;
206f5ee6277Sjasoni 	vap->va_bytes = dbtob((u_quad_t)ip->i_e2fs_nblock);
2075ac2d602Sdownsj 	vap->va_type = vp->v_type;
2085ac2d602Sdownsj 	vap->va_filerev = ip->i_modrev;
2095ac2d602Sdownsj 	return (0);
2105ac2d602Sdownsj }
2115ac2d602Sdownsj 
2125ac2d602Sdownsj /*
2135ac2d602Sdownsj  * Set attribute vnode op. called from several syscalls
2145ac2d602Sdownsj  */
2155ac2d602Sdownsj int
2165f64cd9cSjasper ext2fs_setattr(void *v)
2175ac2d602Sdownsj {
21899bc9d31Sderaadt 	struct vop_setattr_args *ap = v;
2195f64cd9cSjasper 	struct vattr *vap = ap->a_vap;
2205f64cd9cSjasper 	struct vnode *vp = ap->a_vp;
2215f64cd9cSjasper 	struct inode *ip = VTOI(vp);
2225f64cd9cSjasper 	struct ucred *cred = ap->a_cred;
2235f64cd9cSjasper 	struct proc *p = ap->a_p;
2245ac2d602Sdownsj 	int error;
2255ac2d602Sdownsj 
2265ac2d602Sdownsj 	/*
2275ac2d602Sdownsj 	 * Check for unsettable attributes.
2285ac2d602Sdownsj 	 */
2295ac2d602Sdownsj 	if ((vap->va_type != VNON) || (vap->va_nlink != VNOVAL) ||
2305ac2d602Sdownsj 		(vap->va_fsid != VNOVAL) || (vap->va_fileid != VNOVAL) ||
2315ac2d602Sdownsj 		(vap->va_blocksize != VNOVAL) || (vap->va_rdev != VNOVAL) ||
2325ac2d602Sdownsj 		((int)vap->va_bytes != VNOVAL) || (vap->va_gen != VNOVAL)) {
2335ac2d602Sdownsj 		return (EINVAL);
2345ac2d602Sdownsj 	}
2355ac2d602Sdownsj 	if (vap->va_flags != VNOVAL) {
236f5ee6277Sjasoni 		if (vp->v_mount->mnt_flag & MNT_RDONLY)
237f5ee6277Sjasoni 			return (EROFS);
2385ac2d602Sdownsj 		if (cred->cr_uid != ip->i_e2fs_uid &&
239ad392958Stedu 			(error = suser_ucred(cred)))
2405ac2d602Sdownsj 			return (error);
2415ac2d602Sdownsj #ifdef EXT2FS_SYSTEM_FLAGS
2425ac2d602Sdownsj 		if (cred->cr_uid == 0) {
243f5ee6277Sjasoni 			if ((ip->i_e2fs_flags &
244f5ee6277Sjasoni 			    (EXT2_APPEND | EXT2_IMMUTABLE)) && securelevel > 0)
2455ac2d602Sdownsj 				return (EPERM);
2465ac2d602Sdownsj 			ip->i_e2fs_flags &= ~(EXT2_APPEND | EXT2_IMMUTABLE);
247f5ee6277Sjasoni 			ip->i_e2fs_flags |=
248f5ee6277Sjasoni 			    (vap->va_flags & SF_APPEND) ? EXT2_APPEND : 0 |
2495ac2d602Sdownsj 			    (vap->va_flags & SF_IMMUTABLE) ? EXT2_IMMUTABLE: 0;
2505ac2d602Sdownsj 		} else {
2515ac2d602Sdownsj 			return (EPERM);
2525ac2d602Sdownsj 		}
2535ac2d602Sdownsj #else
2545ac2d602Sdownsj 		ip->i_e2fs_flags &= ~(EXT2_APPEND | EXT2_IMMUTABLE);
255f5ee6277Sjasoni 		ip->i_e2fs_flags |=
256f5ee6277Sjasoni 		    (vap->va_flags & UF_APPEND) ? EXT2_APPEND : 0 |
2575ac2d602Sdownsj 		    (vap->va_flags & UF_IMMUTABLE) ? EXT2_IMMUTABLE: 0;
2585ac2d602Sdownsj #endif
2595ac2d602Sdownsj 		ip->i_flag |= IN_CHANGE;
2605ac2d602Sdownsj 		if (vap->va_flags & (IMMUTABLE | APPEND))
2615ac2d602Sdownsj 			return (0);
2625ac2d602Sdownsj 	}
2635ac2d602Sdownsj 	if (ip->i_e2fs_flags & (EXT2_APPEND | EXT2_IMMUTABLE))
2645ac2d602Sdownsj 		return (EPERM);
2655ac2d602Sdownsj 	/*
2665ac2d602Sdownsj 	 * Go through the fields and update iff not VNOVAL.
2675ac2d602Sdownsj 	 */
2685ac2d602Sdownsj 	if (vap->va_uid != (uid_t)VNOVAL || vap->va_gid != (gid_t)VNOVAL) {
269f5ee6277Sjasoni 		if (vp->v_mount->mnt_flag & MNT_RDONLY)
270f5ee6277Sjasoni 			return (EROFS);
2715ac2d602Sdownsj 		error = ext2fs_chown(vp, vap->va_uid, vap->va_gid, cred, p);
2725ac2d602Sdownsj 		if (error)
2735ac2d602Sdownsj 			return (error);
2745ac2d602Sdownsj 	}
2755ac2d602Sdownsj 	if (vap->va_size != VNOVAL) {
276f5ee6277Sjasoni 		/*
277f5ee6277Sjasoni 		 * Disallow write attempts on read-only file systems;
278f5ee6277Sjasoni 		 * unless the file is a socket, fifo, or a block or
279f5ee6277Sjasoni 		 * character device resident on the file system.
280f5ee6277Sjasoni 		 */
281f5ee6277Sjasoni 		switch (vp->v_type) {
282f5ee6277Sjasoni 		case VDIR:
2835ac2d602Sdownsj 			return (EISDIR);
284f5ee6277Sjasoni 		case VLNK:
285f5ee6277Sjasoni 		case VREG:
286f5ee6277Sjasoni 			if (vp->v_mount->mnt_flag & MNT_RDONLY)
287f5ee6277Sjasoni 				return (EROFS);
288f5ee6277Sjasoni 		default:
289f5ee6277Sjasoni 			break;
290f5ee6277Sjasoni 		}
291b080ad39Scsapuntz 		error = ext2fs_truncate(ip, vap->va_size, 0, cred);
2925ac2d602Sdownsj 		if (error)
2935ac2d602Sdownsj 			return (error);
2945ac2d602Sdownsj 	}
2955ac2d602Sdownsj 	if (vap->va_atime.tv_sec != VNOVAL || vap->va_mtime.tv_sec != VNOVAL) {
296f5ee6277Sjasoni 		if (vp->v_mount->mnt_flag & MNT_RDONLY)
297f5ee6277Sjasoni 			return (EROFS);
2985ac2d602Sdownsj 		if (cred->cr_uid != ip->i_e2fs_uid &&
299ad392958Stedu 			(error = suser_ucred(cred)) &&
3005ac2d602Sdownsj 			((vap->va_vaflags & VA_UTIMES_NULL) == 0 ||
3015ac2d602Sdownsj 			(error = VOP_ACCESS(vp, VWRITE, cred, p))))
3025ac2d602Sdownsj 			return (error);
3035ac2d602Sdownsj 		if (vap->va_mtime.tv_sec != VNOVAL)
3045ac2d602Sdownsj 			ip->i_flag |= IN_CHANGE | IN_UPDATE;
3055fdfb110Smillert 		if (vap->va_atime.tv_sec != VNOVAL) {
3065fdfb110Smillert 			if (!(vp->v_mount->mnt_flag & MNT_NOATIME) ||
3075fdfb110Smillert 			    (ip->i_flag & (IN_CHANGE | IN_UPDATE)))
3085fdfb110Smillert 				ip->i_flag |= IN_ACCESS;
3095fdfb110Smillert 		}
310b080ad39Scsapuntz 		error = ext2fs_update(ip, &vap->va_atime, &vap->va_mtime, 1);
3115ac2d602Sdownsj 		if (error)
3125ac2d602Sdownsj 			return (error);
3135ac2d602Sdownsj 	}
3145ac2d602Sdownsj 	error = 0;
315f5ee6277Sjasoni 	if (vap->va_mode != (mode_t)VNOVAL) {
316f5ee6277Sjasoni 		if (vp->v_mount->mnt_flag & MNT_RDONLY)
317f5ee6277Sjasoni 			return (EROFS);
3185ac2d602Sdownsj 		error = ext2fs_chmod(vp, (int)vap->va_mode, cred, p);
319f5ee6277Sjasoni 	}
3205ac2d602Sdownsj 	return (error);
3215ac2d602Sdownsj }
3225ac2d602Sdownsj 
3235ac2d602Sdownsj /*
3245ac2d602Sdownsj  * Change the mode on a file.
3255ac2d602Sdownsj  * Inode must be locked before calling.
3265ac2d602Sdownsj  */
3275ac2d602Sdownsj static int
3285f64cd9cSjasper ext2fs_chmod(struct vnode *vp, mode_t mode, struct ucred *cred, struct proc *p)
3295ac2d602Sdownsj {
3305f64cd9cSjasper 	struct inode *ip = VTOI(vp);
3315ac2d602Sdownsj 	int error;
3325ac2d602Sdownsj 
333ad392958Stedu 	if (cred->cr_uid != ip->i_e2fs_uid && (error = suser_ucred(cred)))
3345ac2d602Sdownsj 		return (error);
3355ac2d602Sdownsj 	if (cred->cr_uid) {
3365ac2d602Sdownsj 		if (vp->v_type != VDIR && (mode & S_ISTXT))
3375ac2d602Sdownsj 			return (EFTYPE);
3385ac2d602Sdownsj 		if (!groupmember(ip->i_e2fs_gid, cred) && (mode & ISGID))
3395ac2d602Sdownsj 			return (EPERM);
3405ac2d602Sdownsj 	}
3415ac2d602Sdownsj 	ip->i_e2fs_mode &= ~ALLPERMS;
3425ac2d602Sdownsj 	ip->i_e2fs_mode |= (mode & ALLPERMS);
3435ac2d602Sdownsj 	ip->i_flag |= IN_CHANGE;
3441414b0faSart 	if ((vp->v_flag & VTEXT) && (ip->i_e2fs_mode & S_ISTXT) == 0)
3451414b0faSart 		(void) uvm_vnp_uncache(vp);
3465ac2d602Sdownsj 	return (0);
3475ac2d602Sdownsj }
3485ac2d602Sdownsj 
3495ac2d602Sdownsj /*
3505ac2d602Sdownsj  * Perform chown operation on inode ip;
3515ac2d602Sdownsj  * inode must be locked prior to call.
3525ac2d602Sdownsj  */
3535ac2d602Sdownsj static int
3545f64cd9cSjasper ext2fs_chown(struct vnode *vp, uid_t uid, gid_t gid, struct ucred *cred, struct proc *p)
3555ac2d602Sdownsj {
3565f64cd9cSjasper 	struct inode *ip = VTOI(vp);
3575ac2d602Sdownsj 	uid_t ouid;
3585ac2d602Sdownsj 	gid_t ogid;
3595ac2d602Sdownsj 	int error = 0;
3605ac2d602Sdownsj 
3615ac2d602Sdownsj 	if (uid == (uid_t)VNOVAL)
3625ac2d602Sdownsj 		uid = ip->i_e2fs_uid;
3635ac2d602Sdownsj 	if (gid == (gid_t)VNOVAL)
3645ac2d602Sdownsj 		gid = ip->i_e2fs_gid;
3655ac2d602Sdownsj 	/*
3665ac2d602Sdownsj 	 * If we don't own the file, are trying to change the owner
3675ac2d602Sdownsj 	 * of the file, or are not a member of the target group,
3685ac2d602Sdownsj 	 * the caller must be superuser or the call fails.
3695ac2d602Sdownsj 	 */
3705ac2d602Sdownsj 	if ((cred->cr_uid != ip->i_e2fs_uid || uid != ip->i_e2fs_uid ||
3715ac2d602Sdownsj 		(gid != ip->i_e2fs_gid && !groupmember((gid_t)gid, cred))) &&
372ad392958Stedu 		(error = suser_ucred(cred)))
3735ac2d602Sdownsj 		return (error);
3745ac2d602Sdownsj 	ogid = ip->i_e2fs_gid;
3755ac2d602Sdownsj 	ouid = ip->i_e2fs_uid;
3765ac2d602Sdownsj 
3775ac2d602Sdownsj 	ip->i_e2fs_gid = gid;
3785ac2d602Sdownsj 	ip->i_e2fs_uid = uid;
3795ac2d602Sdownsj 	if (ouid != uid || ogid != gid)
3805ac2d602Sdownsj 		ip->i_flag |= IN_CHANGE;
3815ac2d602Sdownsj 	if (ouid != uid && cred->cr_uid != 0)
3825ac2d602Sdownsj 		ip->i_e2fs_mode &= ~ISUID;
3835ac2d602Sdownsj 	if (ogid != gid && cred->cr_uid != 0)
3845ac2d602Sdownsj 		ip->i_e2fs_mode &= ~ISGID;
3855ac2d602Sdownsj 	return (0);
3865ac2d602Sdownsj }
3875ac2d602Sdownsj 
3885ac2d602Sdownsj int
3895f64cd9cSjasper ext2fs_remove(void *v)
3905ac2d602Sdownsj {
39199bc9d31Sderaadt 	struct vop_remove_args *ap = v;
3925f64cd9cSjasper 	struct inode *ip;
3935f64cd9cSjasper 	struct vnode *vp = ap->a_vp;
3945f64cd9cSjasper 	struct vnode *dvp = ap->a_dvp;
3955ac2d602Sdownsj 	int error;
3965ac2d602Sdownsj 
3975ac2d602Sdownsj 	ip = VTOI(vp);
398d56d3b9eSderaadt 	if (vp->v_type == VDIR ||
399d56d3b9eSderaadt 		(ip->i_e2fs_flags & (EXT2_IMMUTABLE | EXT2_APPEND)) ||
4005ac2d602Sdownsj 	    	(VTOI(dvp)->i_e2fs_flags & EXT2_APPEND)) {
4015ac2d602Sdownsj 		error = EPERM;
4025ac2d602Sdownsj 		goto out;
4035ac2d602Sdownsj 	}
4045ac2d602Sdownsj 	error = ext2fs_dirremove(dvp, ap->a_cnp);
4055ac2d602Sdownsj 	if (error == 0) {
4065ac2d602Sdownsj 		ip->i_e2fs_nlink--;
4075ac2d602Sdownsj 		ip->i_flag |= IN_CHANGE;
4085ac2d602Sdownsj 	}
4095ac2d602Sdownsj out:
4105ac2d602Sdownsj 	if (dvp == vp)
4115ac2d602Sdownsj 		vrele(vp);
4125ac2d602Sdownsj 	else
4135ac2d602Sdownsj 		vput(vp);
4145ac2d602Sdownsj 	vput(dvp);
4155ac2d602Sdownsj 	return (error);
4165ac2d602Sdownsj }
4175ac2d602Sdownsj 
4185ac2d602Sdownsj /*
4195ac2d602Sdownsj  * link vnode call
4205ac2d602Sdownsj  */
4215ac2d602Sdownsj int
4225f64cd9cSjasper ext2fs_link(void *v)
4235ac2d602Sdownsj {
42499bc9d31Sderaadt 	struct vop_link_args *ap = v;
4255f64cd9cSjasper 	struct vnode *dvp = ap->a_dvp;
4265f64cd9cSjasper 	struct vnode *vp = ap->a_vp;
4275f64cd9cSjasper 	struct componentname *cnp = ap->a_cnp;
42807feb63cScsapuntz 	struct proc *p = cnp->cn_proc;
4295f64cd9cSjasper 	struct inode *ip;
4305ac2d602Sdownsj 	int error;
4315ac2d602Sdownsj 
4325ac2d602Sdownsj #ifdef DIAGNOSTIC
4335ac2d602Sdownsj 	if ((cnp->cn_flags & HASBUF) == 0)
4345ac2d602Sdownsj 		panic("ext2fs_link: no name");
4355ac2d602Sdownsj #endif
4365ac2d602Sdownsj 	if (vp->v_type == VDIR) {
4375ac2d602Sdownsj 		VOP_ABORTOP(dvp, cnp);
4385ac2d602Sdownsj 		error = EISDIR;
4395ac2d602Sdownsj 		goto out2;
4405ac2d602Sdownsj 	}
4415ac2d602Sdownsj 	if (dvp->v_mount != vp->v_mount) {
4425ac2d602Sdownsj 		VOP_ABORTOP(dvp, cnp);
4435ac2d602Sdownsj 		error = EXDEV;
4445ac2d602Sdownsj 		goto out2;
4455ac2d602Sdownsj 	}
446f5ee6277Sjasoni 	if (dvp != vp && (error = vn_lock(vp, LK_EXCLUSIVE, p))) {
4475ac2d602Sdownsj 		VOP_ABORTOP(dvp, cnp);
4485ac2d602Sdownsj 		goto out2;
4495ac2d602Sdownsj 	}
4505ac2d602Sdownsj 	ip = VTOI(vp);
4515ac2d602Sdownsj 	if ((nlink_t)ip->i_e2fs_nlink >= LINK_MAX) {
4525ac2d602Sdownsj 		VOP_ABORTOP(dvp, cnp);
4535ac2d602Sdownsj 		error = EMLINK;
4545ac2d602Sdownsj 		goto out1;
4555ac2d602Sdownsj 	}
4565ac2d602Sdownsj 	if (ip->i_e2fs_flags & (EXT2_IMMUTABLE | EXT2_APPEND)) {
4575ac2d602Sdownsj 		VOP_ABORTOP(dvp, cnp);
4585ac2d602Sdownsj 		error = EPERM;
4595ac2d602Sdownsj 		goto out1;
4605ac2d602Sdownsj 	}
4615ac2d602Sdownsj 	ip->i_e2fs_nlink++;
4625ac2d602Sdownsj 	ip->i_flag |= IN_CHANGE;
463b080ad39Scsapuntz 	error = ext2fs_update(ip, NULL, NULL, 1);
4645ac2d602Sdownsj 	if (!error)
4655ac2d602Sdownsj 		error = ext2fs_direnter(ip, dvp, cnp);
4665ac2d602Sdownsj 	if (error) {
4675ac2d602Sdownsj 		ip->i_e2fs_nlink--;
4685ac2d602Sdownsj 		ip->i_flag |= IN_CHANGE;
4695ac2d602Sdownsj 	}
470dbe27ba0Stedu 	pool_put(&namei_pool, cnp->cn_pnbuf);
4715ac2d602Sdownsj out1:
4725ac2d602Sdownsj 	if (dvp != vp)
47307feb63cScsapuntz 		VOP_UNLOCK(vp, 0, p);
4745ac2d602Sdownsj out2:
4755ac2d602Sdownsj 	vput(dvp);
4765ac2d602Sdownsj 	return (error);
4775ac2d602Sdownsj }
4785ac2d602Sdownsj 
4795ac2d602Sdownsj /*
4805ac2d602Sdownsj  * Rename system call.
4815ac2d602Sdownsj  * 	rename("foo", "bar");
4825ac2d602Sdownsj  * is essentially
4835ac2d602Sdownsj  *	unlink("bar");
4845ac2d602Sdownsj  *	link("foo", "bar");
4855ac2d602Sdownsj  *	unlink("foo");
4865ac2d602Sdownsj  * but ``atomically''.  Can't do full commit without saving state in the
4875ac2d602Sdownsj  * inode on disk which isn't feasible at this time.  Best we can do is
4885ac2d602Sdownsj  * always guarantee the target exists.
4895ac2d602Sdownsj  *
4905ac2d602Sdownsj  * Basic algorithm is:
4915ac2d602Sdownsj  *
4925ac2d602Sdownsj  * 1) Bump link count on source while we're linking it to the
4935ac2d602Sdownsj  *    target.  This also ensure the inode won't be deleted out
4945ac2d602Sdownsj  *    from underneath us while we work (it may be truncated by
4955ac2d602Sdownsj  *    a concurrent `trunc' or `open' for creation).
4965ac2d602Sdownsj  * 2) Link source to destination.  If destination already exists,
4975ac2d602Sdownsj  *    delete it first.
4985ac2d602Sdownsj  * 3) Unlink source reference to inode if still around. If a
4995ac2d602Sdownsj  *    directory was moved and the parent of the destination
5005ac2d602Sdownsj  *    is different from the source, patch the ".." entry in the
5015ac2d602Sdownsj  *    directory.
5025ac2d602Sdownsj  */
5035ac2d602Sdownsj int
5045f64cd9cSjasper ext2fs_rename(void *v)
5055ac2d602Sdownsj {
50699bc9d31Sderaadt 	struct vop_rename_args  *ap = v;
5075ac2d602Sdownsj 	struct vnode *tvp = ap->a_tvp;
5085f64cd9cSjasper 	struct vnode *tdvp = ap->a_tdvp;
5095ac2d602Sdownsj 	struct vnode *fvp = ap->a_fvp;
5105f64cd9cSjasper 	struct vnode *fdvp = ap->a_fdvp;
5115f64cd9cSjasper 	struct componentname *tcnp = ap->a_tcnp;
5125f64cd9cSjasper 	struct componentname *fcnp = ap->a_fcnp;
5135f64cd9cSjasper 	struct inode *ip, *xp, *dp;
51407feb63cScsapuntz 	struct proc *p = fcnp->cn_proc;
5155ac2d602Sdownsj 	struct ext2fs_dirtemplate dirbuf;
5165f64cd9cSjasper 	/* struct timespec ts; */
5175ac2d602Sdownsj 	int doingdirectory = 0, oldparent = 0, newparent = 0;
5185ac2d602Sdownsj 	int error = 0;
5195ac2d602Sdownsj 	u_char namlen;
5205ac2d602Sdownsj 
5215ac2d602Sdownsj #ifdef DIAGNOSTIC
5225ac2d602Sdownsj 	if ((tcnp->cn_flags & HASBUF) == 0 ||
5235ac2d602Sdownsj 	    (fcnp->cn_flags & HASBUF) == 0)
5245ac2d602Sdownsj 		panic("ext2fs_rename: no name");
5255ac2d602Sdownsj #endif
5265ac2d602Sdownsj 	/*
5275ac2d602Sdownsj 	 * Check for cross-device rename.
5285ac2d602Sdownsj 	 */
5295ac2d602Sdownsj 	if ((fvp->v_mount != tdvp->v_mount) ||
5305ac2d602Sdownsj 	    (tvp && (fvp->v_mount != tvp->v_mount))) {
5315ac2d602Sdownsj 		error = EXDEV;
5325ac2d602Sdownsj abortit:
5335ac2d602Sdownsj 		VOP_ABORTOP(tdvp, tcnp); /* XXX, why not in NFS? */
5345ac2d602Sdownsj 		if (tdvp == tvp)
5355ac2d602Sdownsj 			vrele(tdvp);
5365ac2d602Sdownsj 		else
5375ac2d602Sdownsj 			vput(tdvp);
5385ac2d602Sdownsj 		if (tvp)
5395ac2d602Sdownsj 			vput(tvp);
5405ac2d602Sdownsj 		VOP_ABORTOP(fdvp, fcnp); /* XXX, why not in NFS? */
5415ac2d602Sdownsj 		vrele(fdvp);
5425ac2d602Sdownsj 		vrele(fvp);
5435ac2d602Sdownsj 		return (error);
5445ac2d602Sdownsj 	}
5455ac2d602Sdownsj 
5465ac2d602Sdownsj 	/*
5475ac2d602Sdownsj 	 * Check if just deleting a link name.
5485ac2d602Sdownsj 	 */
5495ac2d602Sdownsj 	if (tvp && ((VTOI(tvp)->i_e2fs_flags & (EXT2_IMMUTABLE | EXT2_APPEND)) ||
5505ac2d602Sdownsj 	    (VTOI(tdvp)->i_e2fs_flags & EXT2_APPEND))) {
5515ac2d602Sdownsj 		error = EPERM;
5525ac2d602Sdownsj 		goto abortit;
5535ac2d602Sdownsj 	}
5545ac2d602Sdownsj 	if (fvp == tvp) {
5555ac2d602Sdownsj 		if (fvp->v_type == VDIR) {
5565ac2d602Sdownsj 			error = EINVAL;
5575ac2d602Sdownsj 			goto abortit;
5585ac2d602Sdownsj 		}
5595ac2d602Sdownsj 
5605ac2d602Sdownsj 		/* Release destination completely. */
5615ac2d602Sdownsj 		VOP_ABORTOP(tdvp, tcnp);
5625ac2d602Sdownsj 		vput(tdvp);
5635ac2d602Sdownsj 		vput(tvp);
5645ac2d602Sdownsj 
5655ac2d602Sdownsj 		/* Delete source. */
5665ac2d602Sdownsj 		vrele(fdvp);
5675ac2d602Sdownsj 		vrele(fvp);
5685ac2d602Sdownsj 		fcnp->cn_flags &= ~MODMASK;
5695ac2d602Sdownsj 		fcnp->cn_flags |= LOCKPARENT | LOCKLEAF;
5705ac2d602Sdownsj 		if ((fcnp->cn_flags & SAVESTART) == 0)
5715ac2d602Sdownsj 			panic("ext2fs_rename: lost from startdir");
5725ac2d602Sdownsj 		fcnp->cn_nameiop = DELETE;
5735ac2d602Sdownsj 		(void) relookup(fdvp, &fvp, fcnp);
5745ac2d602Sdownsj 		return (VOP_REMOVE(fdvp, fvp, fcnp));
5755ac2d602Sdownsj 	}
576f5ee6277Sjasoni 	if ((error = vn_lock(fvp, LK_EXCLUSIVE, p)) != 0)
5775ac2d602Sdownsj 		goto abortit;
5785ac2d602Sdownsj 	dp = VTOI(fdvp);
5795ac2d602Sdownsj 	ip = VTOI(fvp);
5804592dbefSmillert 	if ((nlink_t)ip->i_e2fs_nlink >= LINK_MAX) {
5814592dbefSmillert 		VOP_UNLOCK(fvp, 0, p);
5824592dbefSmillert 		error = EMLINK;
5834592dbefSmillert 		goto abortit;
5844592dbefSmillert 	}
5855ac2d602Sdownsj 	if ((ip->i_e2fs_flags & (EXT2_IMMUTABLE | EXT2_APPEND)) ||
5865ac2d602Sdownsj 		(dp->i_e2fs_flags & EXT2_APPEND)) {
58707feb63cScsapuntz 		VOP_UNLOCK(fvp, 0, p);
5885ac2d602Sdownsj 		error = EPERM;
5895ac2d602Sdownsj 		goto abortit;
5905ac2d602Sdownsj 	}
5915ac2d602Sdownsj 	if ((ip->i_e2fs_mode & IFMT) == IFDIR) {
5925ac2d602Sdownsj         	error = VOP_ACCESS(fvp, VWRITE, tcnp->cn_cred, tcnp->cn_proc);
5935ac2d602Sdownsj         	if (!error && tvp)
594f5ee6277Sjasoni                 	error = VOP_ACCESS(tvp, VWRITE, tcnp->cn_cred,
595f5ee6277Sjasoni 			    tcnp->cn_proc);
5965ac2d602Sdownsj         	if (error) {
59707feb63cScsapuntz                 	VOP_UNLOCK(fvp, 0, p);
5985ac2d602Sdownsj                 	error = EACCES;
5995ac2d602Sdownsj                 	goto abortit;
6005ac2d602Sdownsj         	}
6015ac2d602Sdownsj 		/*
6025ac2d602Sdownsj 		 * Avoid ".", "..", and aliases of "." for obvious reasons.
6035ac2d602Sdownsj 		 */
6045ac2d602Sdownsj 		if ((fcnp->cn_namelen == 1 && fcnp->cn_nameptr[0] == '.') ||
605d56d3b9eSderaadt 		    dp == ip ||
606d56d3b9eSderaadt 			(fcnp->cn_flags&ISDOTDOT) ||
607d56d3b9eSderaadt 			(tcnp->cn_flags & ISDOTDOT) ||
6085ac2d602Sdownsj 		    (ip->i_flag & IN_RENAME)) {
60907feb63cScsapuntz 			VOP_UNLOCK(fvp, 0, p);
6105ac2d602Sdownsj 			error = EINVAL;
6115ac2d602Sdownsj 			goto abortit;
6125ac2d602Sdownsj 		}
6135ac2d602Sdownsj 		ip->i_flag |= IN_RENAME;
6145ac2d602Sdownsj 		oldparent = dp->i_number;
6155ac2d602Sdownsj 		doingdirectory++;
6165ac2d602Sdownsj 	}
6175ac2d602Sdownsj 	vrele(fdvp);
6185ac2d602Sdownsj 
6195ac2d602Sdownsj 	/*
6205ac2d602Sdownsj 	 * When the target exists, both the directory
6215ac2d602Sdownsj 	 * and target vnodes are returned locked.
6225ac2d602Sdownsj 	 */
6235ac2d602Sdownsj 	dp = VTOI(tdvp);
6245ac2d602Sdownsj 	xp = NULL;
6255ac2d602Sdownsj 	if (tvp)
6265ac2d602Sdownsj 		xp = VTOI(tvp);
6275ac2d602Sdownsj 
6285ac2d602Sdownsj 	/*
6295ac2d602Sdownsj 	 * 1) Bump link count while we're moving stuff
6305ac2d602Sdownsj 	 *    around.  If we crash somewhere before
6315ac2d602Sdownsj 	 *    completing our work, the link count
6325ac2d602Sdownsj 	 *    may be wrong, but correctable.
6335ac2d602Sdownsj 	 */
6345ac2d602Sdownsj 	ip->i_e2fs_nlink++;
6355ac2d602Sdownsj 	ip->i_flag |= IN_CHANGE;
636b080ad39Scsapuntz 	if ((error = ext2fs_update(ip, NULL, NULL, 1)) != 0) {
63707feb63cScsapuntz 		VOP_UNLOCK(fvp, 0, p);
6385ac2d602Sdownsj 		goto bad;
6395ac2d602Sdownsj 	}
6405ac2d602Sdownsj 
6415ac2d602Sdownsj 	/*
6425ac2d602Sdownsj 	 * If ".." must be changed (ie the directory gets a new
6435ac2d602Sdownsj 	 * parent) then the source directory must not be in the
6442f4b598dStedu 	 * directory hierarchy above the target, as this would
6455ac2d602Sdownsj 	 * orphan everything below the source directory. Also
6465ac2d602Sdownsj 	 * the user must have write permission in the source so
6475ac2d602Sdownsj 	 * as to be able to change "..". We must repeat the call
6485ac2d602Sdownsj 	 * to namei, as the parent directory is unlocked by the
6495ac2d602Sdownsj 	 * call to checkpath().
6505ac2d602Sdownsj 	 */
6515ac2d602Sdownsj 	error = VOP_ACCESS(fvp, VWRITE, tcnp->cn_cred, tcnp->cn_proc);
65207feb63cScsapuntz 	VOP_UNLOCK(fvp, 0, p);
6535ac2d602Sdownsj 	if (oldparent != dp->i_number)
6545ac2d602Sdownsj 		newparent = dp->i_number;
6555ac2d602Sdownsj 	if (doingdirectory && newparent) {
6565ac2d602Sdownsj 		if (error)	/* write access check above */
6575ac2d602Sdownsj 			goto bad;
6585ac2d602Sdownsj 		if (xp != NULL)
6595ac2d602Sdownsj 			vput(tvp);
6605ac2d602Sdownsj 		error = ext2fs_checkpath(ip, dp, tcnp->cn_cred);
6615ac2d602Sdownsj 		if (error != 0)
6625ac2d602Sdownsj 			goto out;
6635ac2d602Sdownsj 		if ((tcnp->cn_flags & SAVESTART) == 0)
6645ac2d602Sdownsj 			panic("ext2fs_rename: lost to startdir");
6655ac2d602Sdownsj 		if ((error = relookup(tdvp, &tvp, tcnp)) != 0)
6665ac2d602Sdownsj 			goto out;
6675ac2d602Sdownsj 		dp = VTOI(tdvp);
6685ac2d602Sdownsj 		xp = NULL;
6695ac2d602Sdownsj 		if (tvp)
6705ac2d602Sdownsj 			xp = VTOI(tvp);
6715ac2d602Sdownsj 	}
6725ac2d602Sdownsj 	/*
6735ac2d602Sdownsj 	 * 2) If target doesn't exist, link the target
6745ac2d602Sdownsj 	 *    to the source and unlink the source.
6755ac2d602Sdownsj 	 *    Otherwise, rewrite the target directory
6765ac2d602Sdownsj 	 *    entry to reference the source inode and
6775ac2d602Sdownsj 	 *    expunge the original entry's existence.
6785ac2d602Sdownsj 	 */
6795ac2d602Sdownsj 	if (xp == NULL) {
6805ac2d602Sdownsj 		if (dp->i_dev != ip->i_dev)
6815ac2d602Sdownsj 			panic("rename: EXDEV");
6825ac2d602Sdownsj 		/*
6835ac2d602Sdownsj 		 * Account for ".." in new directory.
6845ac2d602Sdownsj 		 * When source and destination have the same
6855ac2d602Sdownsj 		 * parent we don't fool with the link count.
6865ac2d602Sdownsj 		 */
6875ac2d602Sdownsj 		if (doingdirectory && newparent) {
6885ac2d602Sdownsj 			if ((nlink_t)dp->i_e2fs_nlink >= LINK_MAX) {
6895ac2d602Sdownsj 				error = EMLINK;
6905ac2d602Sdownsj 				goto bad;
6915ac2d602Sdownsj 			}
6925ac2d602Sdownsj 			dp->i_e2fs_nlink++;
6935ac2d602Sdownsj 			dp->i_flag |= IN_CHANGE;
694b080ad39Scsapuntz 			if ((error = ext2fs_update(dp, NULL, NULL, 1)) != 0)
6955ac2d602Sdownsj 				goto bad;
6965ac2d602Sdownsj 		}
6975ac2d602Sdownsj 		error = ext2fs_direnter(ip, tdvp, tcnp);
6985ac2d602Sdownsj 		if (error != 0) {
6995ac2d602Sdownsj 			if (doingdirectory && newparent) {
7005ac2d602Sdownsj 				dp->i_e2fs_nlink--;
7015ac2d602Sdownsj 				dp->i_flag |= IN_CHANGE;
702b080ad39Scsapuntz 				(void)ext2fs_update(dp, NULL, NULL, 1);
7035ac2d602Sdownsj 			}
7045ac2d602Sdownsj 			goto bad;
7055ac2d602Sdownsj 		}
7065ac2d602Sdownsj 		vput(tdvp);
7075ac2d602Sdownsj 	} else {
7085ac2d602Sdownsj 		if (xp->i_dev != dp->i_dev || xp->i_dev != ip->i_dev)
7095ac2d602Sdownsj 			panic("rename: EXDEV");
7105ac2d602Sdownsj 		/*
7115ac2d602Sdownsj 		 * Short circuit rename(foo, foo).
7125ac2d602Sdownsj 		 */
7135ac2d602Sdownsj 		if (xp->i_number == ip->i_number)
7145ac2d602Sdownsj 			panic("rename: same file");
7155ac2d602Sdownsj 		/*
7165ac2d602Sdownsj 		 * If the parent directory is "sticky", then the user must
7175ac2d602Sdownsj 		 * own the parent directory, or the destination of the rename,
7185ac2d602Sdownsj 		 * otherwise the destination may not be changed (except by
7195ac2d602Sdownsj 		 * root). This implements append-only directories.
7205ac2d602Sdownsj 		 */
7215ac2d602Sdownsj 		if ((dp->i_e2fs_mode & S_ISTXT) && tcnp->cn_cred->cr_uid != 0 &&
7225ac2d602Sdownsj 		    tcnp->cn_cred->cr_uid != dp->i_e2fs_uid &&
7235ac2d602Sdownsj 		    xp->i_e2fs_uid != tcnp->cn_cred->cr_uid) {
7245ac2d602Sdownsj 			error = EPERM;
7255ac2d602Sdownsj 			goto bad;
7265ac2d602Sdownsj 		}
7275ac2d602Sdownsj 		/*
7285ac2d602Sdownsj 		 * Target must be empty if a directory and have no links
7295ac2d602Sdownsj 		 * to it. Also, ensure source and target are compatible
7305ac2d602Sdownsj 		 * (both directories, or both not directories).
7315ac2d602Sdownsj 		 */
7325ac2d602Sdownsj 		if ((xp->i_e2fs_mode & IFMT) == IFDIR) {
7335ac2d602Sdownsj 			if (!ext2fs_dirempty(xp, dp->i_number, tcnp->cn_cred) ||
7345ac2d602Sdownsj 				xp->i_e2fs_nlink > 2) {
7355ac2d602Sdownsj 				error = ENOTEMPTY;
7365ac2d602Sdownsj 				goto bad;
7375ac2d602Sdownsj 			}
7385ac2d602Sdownsj 			if (!doingdirectory) {
7395ac2d602Sdownsj 				error = ENOTDIR;
7405ac2d602Sdownsj 				goto bad;
7415ac2d602Sdownsj 			}
7425ac2d602Sdownsj 			cache_purge(tdvp);
7435ac2d602Sdownsj 		} else if (doingdirectory) {
7445ac2d602Sdownsj 			error = EISDIR;
7455ac2d602Sdownsj 			goto bad;
7465ac2d602Sdownsj 		}
7475ac2d602Sdownsj 		error = ext2fs_dirrewrite(dp, ip, tcnp);
7485ac2d602Sdownsj 		if (error != 0)
7495ac2d602Sdownsj 			goto bad;
7505ac2d602Sdownsj 		/*
7515ac2d602Sdownsj 		 * If the target directory is in the same
7525ac2d602Sdownsj 		 * directory as the source directory,
7535ac2d602Sdownsj 		 * decrement the link count on the parent
7545ac2d602Sdownsj 		 * of the target directory.
7555ac2d602Sdownsj 		 */
7565ac2d602Sdownsj 		 if (doingdirectory && !newparent) {
7575ac2d602Sdownsj 			dp->i_e2fs_nlink--;
7585ac2d602Sdownsj 			dp->i_flag |= IN_CHANGE;
7595ac2d602Sdownsj 		}
7605ac2d602Sdownsj 		vput(tdvp);
7615ac2d602Sdownsj 		/*
7625ac2d602Sdownsj 		 * Adjust the link count of the target to
7635ac2d602Sdownsj 		 * reflect the dirrewrite above.  If this is
7645ac2d602Sdownsj 		 * a directory it is empty and there are
7655ac2d602Sdownsj 		 * no links to it, so we can squash the inode and
7665ac2d602Sdownsj 		 * any space associated with it.  We disallowed
7675ac2d602Sdownsj 		 * renaming over top of a directory with links to
7685ac2d602Sdownsj 		 * it above, as the remaining link would point to
7695ac2d602Sdownsj 		 * a directory without "." or ".." entries.
7705ac2d602Sdownsj 		 */
7715ac2d602Sdownsj 		xp->i_e2fs_nlink--;
7725ac2d602Sdownsj 		if (doingdirectory) {
7735ac2d602Sdownsj 			if (--xp->i_e2fs_nlink != 0)
7745ac2d602Sdownsj 				panic("rename: linked directory");
775b080ad39Scsapuntz 			error = ext2fs_truncate(xp, (off_t)0, IO_SYNC,
776b080ad39Scsapuntz 			    tcnp->cn_cred);
7775ac2d602Sdownsj 		}
7785ac2d602Sdownsj 		xp->i_flag |= IN_CHANGE;
7795ac2d602Sdownsj 		vput(tvp);
7805ac2d602Sdownsj 		xp = NULL;
7815ac2d602Sdownsj 	}
7825ac2d602Sdownsj 
7835ac2d602Sdownsj 	/*
7845ac2d602Sdownsj 	 * 3) Unlink the source.
7855ac2d602Sdownsj 	 */
7865ac2d602Sdownsj 	fcnp->cn_flags &= ~MODMASK;
7875ac2d602Sdownsj 	fcnp->cn_flags |= LOCKPARENT | LOCKLEAF;
7885ac2d602Sdownsj 	if ((fcnp->cn_flags & SAVESTART) == 0)
7895ac2d602Sdownsj 		panic("ext2fs_rename: lost from startdir");
7905ac2d602Sdownsj 	(void) relookup(fdvp, &fvp, fcnp);
7915ac2d602Sdownsj 	if (fvp != NULL) {
7925ac2d602Sdownsj 		xp = VTOI(fvp);
7935ac2d602Sdownsj 		dp = VTOI(fdvp);
7945ac2d602Sdownsj 	} else {
7955ac2d602Sdownsj 		/*
7965ac2d602Sdownsj 		 * From name has disappeared.
7975ac2d602Sdownsj 		 */
7985ac2d602Sdownsj 		if (doingdirectory)
7995ac2d602Sdownsj 			panic("ext2fs_rename: lost dir entry");
8005ac2d602Sdownsj 		vrele(ap->a_fvp);
8015ac2d602Sdownsj 		return (0);
8025ac2d602Sdownsj 	}
8035ac2d602Sdownsj 	/*
8045ac2d602Sdownsj 	 * Ensure that the directory entry still exists and has not
8055ac2d602Sdownsj 	 * changed while the new name has been entered. If the source is
8065ac2d602Sdownsj 	 * a file then the entry may have been unlinked or renamed. In
8075ac2d602Sdownsj 	 * either case there is no further work to be done. If the source
8085ac2d602Sdownsj 	 * is a directory then it cannot have been rmdir'ed; its link
8095ac2d602Sdownsj 	 * count of three would cause a rmdir to fail with ENOTEMPTY.
8105ac2d602Sdownsj 	 * The IRENAME flag ensures that it cannot be moved by another
8115ac2d602Sdownsj 	 * rename.
8125ac2d602Sdownsj 	 */
8135ac2d602Sdownsj 	if (xp != ip) {
8145ac2d602Sdownsj 		if (doingdirectory)
8155ac2d602Sdownsj 			panic("ext2fs_rename: lost dir entry");
8165ac2d602Sdownsj 	} else {
8175ac2d602Sdownsj 		/*
8185ac2d602Sdownsj 		 * If the source is a directory with a
8195ac2d602Sdownsj 		 * new parent, the link count of the old
8205ac2d602Sdownsj 		 * parent directory must be decremented
8215ac2d602Sdownsj 		 * and ".." set to point to the new parent.
8225ac2d602Sdownsj 		 */
8235ac2d602Sdownsj 		if (doingdirectory && newparent) {
8245ac2d602Sdownsj 			dp->i_e2fs_nlink--;
8255ac2d602Sdownsj 			dp->i_flag |= IN_CHANGE;
8265ac2d602Sdownsj 			error = vn_rdwr(UIO_READ, fvp, (caddr_t)&dirbuf,
8275ac2d602Sdownsj 				sizeof (struct ext2fs_dirtemplate), (off_t)0,
8285ac2d602Sdownsj 				UIO_SYSSPACE, IO_NODELOCKED,
829f5ee6277Sjasoni 				tcnp->cn_cred, (size_t *)0, (struct proc *)0);
8305ac2d602Sdownsj 			if (error == 0) {
8315ac2d602Sdownsj 					namlen = dirbuf.dotdot_namlen;
8325ac2d602Sdownsj 				if (namlen != 2 ||
8335ac2d602Sdownsj 				    dirbuf.dotdot_name[0] != '.' ||
8345ac2d602Sdownsj 				    dirbuf.dotdot_name[1] != '.') {
8355ac2d602Sdownsj 					ufs_dirbad(xp, (doff_t)12,
8365ac2d602Sdownsj 					    "ext2fs_rename: mangled dir");
8375ac2d602Sdownsj 				} else {
838f5ee6277Sjasoni 					dirbuf.dotdot_ino = h2fs32(newparent);
8395ac2d602Sdownsj 					(void) vn_rdwr(UIO_WRITE, fvp,
8405ac2d602Sdownsj 					    (caddr_t)&dirbuf,
8415ac2d602Sdownsj 					    sizeof (struct dirtemplate),
8425ac2d602Sdownsj 					    (off_t)0, UIO_SYSSPACE,
8435ac2d602Sdownsj 					    IO_NODELOCKED|IO_SYNC,
844f5ee6277Sjasoni 					    tcnp->cn_cred, (size_t *)0,
8455ac2d602Sdownsj 					    (struct proc *)0);
8465ac2d602Sdownsj 					cache_purge(fdvp);
8475ac2d602Sdownsj 				}
8485ac2d602Sdownsj 			}
8495ac2d602Sdownsj 		}
8505ac2d602Sdownsj 		error = ext2fs_dirremove(fdvp, fcnp);
8515ac2d602Sdownsj 		if (!error) {
8525ac2d602Sdownsj 			xp->i_e2fs_nlink--;
8535ac2d602Sdownsj 			xp->i_flag |= IN_CHANGE;
8545ac2d602Sdownsj 		}
8555ac2d602Sdownsj 		xp->i_flag &= ~IN_RENAME;
8565ac2d602Sdownsj 	}
8575ac2d602Sdownsj 	if (dp)
8585ac2d602Sdownsj 		vput(fdvp);
8595ac2d602Sdownsj 	if (xp)
8605ac2d602Sdownsj 		vput(fvp);
8615ac2d602Sdownsj 	vrele(ap->a_fvp);
8625ac2d602Sdownsj 	return (error);
8635ac2d602Sdownsj 
8645ac2d602Sdownsj bad:
8655ac2d602Sdownsj 	if (xp)
8665ac2d602Sdownsj 		vput(ITOV(xp));
8675ac2d602Sdownsj 	vput(ITOV(dp));
8685ac2d602Sdownsj out:
8695ac2d602Sdownsj 	if (doingdirectory)
8705ac2d602Sdownsj 		ip->i_flag &= ~IN_RENAME;
871f5ee6277Sjasoni 	if (vn_lock(fvp, LK_EXCLUSIVE, p) == 0) {
8725ac2d602Sdownsj 		ip->i_e2fs_nlink--;
8735ac2d602Sdownsj 		ip->i_flag |= IN_CHANGE;
8745ac2d602Sdownsj 		vput(fvp);
8755ac2d602Sdownsj 	} else
8765ac2d602Sdownsj 		vrele(fvp);
8775ac2d602Sdownsj 	return (error);
8785ac2d602Sdownsj }
8795ac2d602Sdownsj 
8805ac2d602Sdownsj /*
8815ac2d602Sdownsj  * Mkdir system call
8825ac2d602Sdownsj  */
8835ac2d602Sdownsj int
8845f64cd9cSjasper ext2fs_mkdir(void *v)
8855ac2d602Sdownsj {
88699bc9d31Sderaadt 	struct vop_mkdir_args *ap = v;
8875f64cd9cSjasper 	struct vnode *dvp = ap->a_dvp;
8885f64cd9cSjasper 	struct vattr *vap = ap->a_vap;
8895f64cd9cSjasper 	struct componentname *cnp = ap->a_cnp;
8905f64cd9cSjasper 	struct inode *ip, *dp;
8915ac2d602Sdownsj 	struct vnode *tvp;
892f5ee6277Sjasoni 	struct ext2fs_dirtemplate dirtemplate;
893cc2fc615Smillert 	mode_t dmode;
894cc2fc615Smillert 	int error;
8955ac2d602Sdownsj 
8965ac2d602Sdownsj #ifdef DIAGNOSTIC
8975ac2d602Sdownsj 	if ((cnp->cn_flags & HASBUF) == 0)
8985ac2d602Sdownsj 		panic("ext2fs_mkdir: no name");
8995ac2d602Sdownsj #endif
9005ac2d602Sdownsj 	dp = VTOI(dvp);
9015ac2d602Sdownsj 	if ((nlink_t)dp->i_e2fs_nlink >= LINK_MAX) {
9025ac2d602Sdownsj 		error = EMLINK;
9035ac2d602Sdownsj 		goto out;
9045ac2d602Sdownsj 	}
9051f3ff51cSdownsj 	dmode = vap->va_mode & ACCESSPERMS;
9065ac2d602Sdownsj 	dmode |= IFDIR;
9075ac2d602Sdownsj 	/*
9085ac2d602Sdownsj 	 * Must simulate part of ext2fs_makeinode here to acquire the inode,
9095ac2d602Sdownsj 	 * but not have it entered in the parent directory. The entry is
9105ac2d602Sdownsj 	 * made later after writing "." and ".." entries.
9115ac2d602Sdownsj 	 */
912b080ad39Scsapuntz 	if ((error = ext2fs_inode_alloc(dp, dmode, cnp->cn_cred, &tvp)) != 0)
9135ac2d602Sdownsj 		goto out;
9145ac2d602Sdownsj 	ip = VTOI(tvp);
9155ac2d602Sdownsj 	ip->i_e2fs_uid = cnp->cn_cred->cr_uid;
9165ac2d602Sdownsj 	ip->i_e2fs_gid = dp->i_e2fs_gid;
9175ac2d602Sdownsj 	ip->i_flag |= IN_ACCESS | IN_CHANGE | IN_UPDATE;
9185ac2d602Sdownsj 	ip->i_e2fs_mode = dmode;
9195ac2d602Sdownsj 	tvp->v_type = VDIR;	/* Rest init'd in getnewvnode(). */
9205ac2d602Sdownsj 	ip->i_e2fs_nlink = 2;
921b080ad39Scsapuntz 	error = ext2fs_update(ip, NULL, NULL, 1);
9225ac2d602Sdownsj 
9235ac2d602Sdownsj 	/*
9245ac2d602Sdownsj 	 * Bump link count in parent directory
9255ac2d602Sdownsj 	 * to reflect work done below.  Should
9265ac2d602Sdownsj 	 * be done before reference is created
9275ac2d602Sdownsj 	 * so reparation is possible if we crash.
9285ac2d602Sdownsj 	 */
9295ac2d602Sdownsj 	dp->i_e2fs_nlink++;
9305ac2d602Sdownsj 	dp->i_flag |= IN_CHANGE;
931b080ad39Scsapuntz 	if ((error = ext2fs_update(dp, NULL, NULL, 1)) != 0)
9325ac2d602Sdownsj 		goto bad;
9335ac2d602Sdownsj 
9345ac2d602Sdownsj 	/* Initialize directory with "." and ".." from static template. */
935f5ee6277Sjasoni 	bzero(&dirtemplate, sizeof(dirtemplate));
936f5ee6277Sjasoni 	dirtemplate.dot_ino = h2fs32(ip->i_number);
937f5ee6277Sjasoni 	dirtemplate.dot_reclen = h2fs16(12);
938f5ee6277Sjasoni 	dirtemplate.dot_namlen = 1;
939f5ee6277Sjasoni 	if (ip->i_e2fs->e2fs.e2fs_rev > E2FS_REV0 &&
940f5ee6277Sjasoni 	    (ip->i_e2fs->e2fs.e2fs_features_incompat & EXT2F_INCOMPAT_FTYPE)) {
941f5ee6277Sjasoni 		dirtemplate.dot_type = EXT2_FT_DIR;
942f5ee6277Sjasoni 	}
943f5ee6277Sjasoni 	dirtemplate.dot_name[0] = '.';
944f5ee6277Sjasoni 	dirtemplate.dotdot_ino = h2fs32(dp->i_number);
945f5ee6277Sjasoni 	dirtemplate.dotdot_reclen = h2fs16(VTOI(dvp)->i_e2fs->e2fs_bsize - 12);
946f5ee6277Sjasoni 	dirtemplate.dotdot_namlen = 2;
947f5ee6277Sjasoni 	if (ip->i_e2fs->e2fs.e2fs_rev > E2FS_REV0 &&
948f5ee6277Sjasoni 	    (ip->i_e2fs->e2fs.e2fs_features_incompat & EXT2F_INCOMPAT_FTYPE)) {
949f5ee6277Sjasoni 		dirtemplate.dotdot_type = EXT2_FT_DIR;
950f5ee6277Sjasoni 	}
951f5ee6277Sjasoni 	dirtemplate.dotdot_name[0] = dirtemplate.dotdot_name[1] = '.';
9525ac2d602Sdownsj 	error = vn_rdwr(UIO_WRITE, tvp, (caddr_t)&dirtemplate,
9535ac2d602Sdownsj 	    sizeof (dirtemplate), (off_t)0, UIO_SYSSPACE,
954f5ee6277Sjasoni 	    IO_NODELOCKED|IO_SYNC, cnp->cn_cred, (size_t *)0, (struct proc *)0);
9555ac2d602Sdownsj 	if (error) {
9565ac2d602Sdownsj 		dp->i_e2fs_nlink--;
9575ac2d602Sdownsj 		dp->i_flag |= IN_CHANGE;
9585ac2d602Sdownsj 		goto bad;
9595ac2d602Sdownsj 	}
9605ac2d602Sdownsj 	if (VTOI(dvp)->i_e2fs->e2fs_bsize >
9615ac2d602Sdownsj 							VFSTOUFS(dvp->v_mount)->um_mountp->mnt_stat.f_bsize)
9625ac2d602Sdownsj 		panic("ext2fs_mkdir: blksize"); /* XXX should grow with balloc() */
9635ac2d602Sdownsj 	else {
9644dcbbbb0Sniallo 		error = ext2fs_setsize(ip, VTOI(dvp)->i_e2fs->e2fs_bsize);
9654dcbbbb0Sniallo   	        if (error) {
9664dcbbbb0Sniallo   	        	dp->i_e2fs_nlink--;
9674dcbbbb0Sniallo   	        	dp->i_flag |= IN_CHANGE;
9684dcbbbb0Sniallo   	        	goto bad;
9694dcbbbb0Sniallo   	        }
9705ac2d602Sdownsj 		ip->i_flag |= IN_CHANGE;
9715ac2d602Sdownsj 	}
9725ac2d602Sdownsj 
9730f1683a6Smiod 	/* Directory set up, now install its entry in the parent directory. */
9745ac2d602Sdownsj 	error = ext2fs_direnter(ip, dvp, cnp);
9755ac2d602Sdownsj 	if (error != 0) {
9765ac2d602Sdownsj 		dp->i_e2fs_nlink--;
9775ac2d602Sdownsj 		dp->i_flag |= IN_CHANGE;
9785ac2d602Sdownsj 	}
9795ac2d602Sdownsj bad:
9805ac2d602Sdownsj 	/*
9815ac2d602Sdownsj 	 * No need to do an explicit VOP_TRUNCATE here, vrele will do this
9825ac2d602Sdownsj 	 * for us because we set the link count to 0.
9835ac2d602Sdownsj 	 */
9845ac2d602Sdownsj 	if (error) {
9855ac2d602Sdownsj 		ip->i_e2fs_nlink = 0;
9865ac2d602Sdownsj 		ip->i_flag |= IN_CHANGE;
9875ac2d602Sdownsj 		vput(tvp);
9885ac2d602Sdownsj 	} else
9895ac2d602Sdownsj 		*ap->a_vpp = tvp;
9905ac2d602Sdownsj out:
991dbe27ba0Stedu 	pool_put(&namei_pool, cnp->cn_pnbuf);
9925ac2d602Sdownsj 	vput(dvp);
9935ac2d602Sdownsj 	return (error);
9945ac2d602Sdownsj }
9955ac2d602Sdownsj 
9965ac2d602Sdownsj /*
9975ac2d602Sdownsj  * Rmdir system call.
9985ac2d602Sdownsj  */
9995ac2d602Sdownsj int
10005f64cd9cSjasper ext2fs_rmdir(void *v)
10015ac2d602Sdownsj {
100299bc9d31Sderaadt 	struct vop_rmdir_args *ap = v;
10035f64cd9cSjasper 	struct vnode *vp = ap->a_vp;
10045f64cd9cSjasper 	struct vnode *dvp = ap->a_dvp;
10055f64cd9cSjasper 	struct componentname *cnp = ap->a_cnp;
10065f64cd9cSjasper 	struct inode *ip, *dp;
10075ac2d602Sdownsj 	int error;
10085ac2d602Sdownsj 
10095ac2d602Sdownsj 	ip = VTOI(vp);
10105ac2d602Sdownsj 	dp = VTOI(dvp);
10115ac2d602Sdownsj 	/*
10125ac2d602Sdownsj 	 * No rmdir "." please.
10135ac2d602Sdownsj 	 */
10145ac2d602Sdownsj 	if (dp == ip) {
10155ac2d602Sdownsj 		vrele(dvp);
10165ac2d602Sdownsj 		vput(vp);
10175ac2d602Sdownsj 		return (EINVAL);
10185ac2d602Sdownsj 	}
10195ac2d602Sdownsj 	/*
10205ac2d602Sdownsj 	 * Verify the directory is empty (and valid).
10215ac2d602Sdownsj 	 * (Rmdir ".." won't be valid since
10225ac2d602Sdownsj 	 *  ".." will contain a reference to
10235ac2d602Sdownsj 	 *  the current directory and thus be
10245ac2d602Sdownsj 	 *  non-empty.)
10255ac2d602Sdownsj 	 */
10265ac2d602Sdownsj 	error = 0;
10275ac2d602Sdownsj 	if (ip->i_e2fs_nlink != 2 ||
10285ac2d602Sdownsj 	    !ext2fs_dirempty(ip, dp->i_number, cnp->cn_cred)) {
10295ac2d602Sdownsj 		error = ENOTEMPTY;
10305ac2d602Sdownsj 		goto out;
10315ac2d602Sdownsj 	}
10325ac2d602Sdownsj 	if ((dp->i_e2fs_flags & EXT2_APPEND) ||
10335ac2d602Sdownsj 				 (ip->i_e2fs_flags & (EXT2_IMMUTABLE | EXT2_APPEND))) {
10345ac2d602Sdownsj 		error = EPERM;
10355ac2d602Sdownsj 		goto out;
10365ac2d602Sdownsj 	}
10375ac2d602Sdownsj 	/*
10385ac2d602Sdownsj 	 * Delete reference to directory before purging
10395ac2d602Sdownsj 	 * inode.  If we crash in between, the directory
10405ac2d602Sdownsj 	 * will be reattached to lost+found,
10415ac2d602Sdownsj 	 */
10425ac2d602Sdownsj 	error = ext2fs_dirremove(dvp, cnp);
10435ac2d602Sdownsj 	if (error != 0)
10445ac2d602Sdownsj 		goto out;
10455ac2d602Sdownsj 	dp->i_e2fs_nlink--;
10465ac2d602Sdownsj 	dp->i_flag |= IN_CHANGE;
10475ac2d602Sdownsj 	cache_purge(dvp);
10485ac2d602Sdownsj 	vput(dvp);
10495ac2d602Sdownsj 	dvp = NULL;
10505ac2d602Sdownsj 	/*
10515ac2d602Sdownsj 	 * Truncate inode.  The only stuff left
10525ac2d602Sdownsj 	 * in the directory is "." and "..".  The
10535ac2d602Sdownsj 	 * "." reference is inconsequential since
10545ac2d602Sdownsj 	 * we're quashing it.  The ".." reference
10555ac2d602Sdownsj 	 * has already been adjusted above.  We've
10565ac2d602Sdownsj 	 * removed the "." reference and the reference
10575ac2d602Sdownsj 	 * in the parent directory, but there may be
10585ac2d602Sdownsj 	 * other hard links so decrement by 2 and
10595ac2d602Sdownsj 	 * worry about them later.
10605ac2d602Sdownsj 	 */
10615ac2d602Sdownsj 	ip->i_e2fs_nlink -= 2;
1062b080ad39Scsapuntz 	error = ext2fs_truncate(ip, (off_t)0, IO_SYNC, cnp->cn_cred);
10635ac2d602Sdownsj 	cache_purge(ITOV(ip));
10645ac2d602Sdownsj out:
10655ac2d602Sdownsj 	if (dvp)
10665ac2d602Sdownsj 		vput(dvp);
10675ac2d602Sdownsj 	vput(vp);
10685ac2d602Sdownsj 	return (error);
10695ac2d602Sdownsj }
10705ac2d602Sdownsj 
10715ac2d602Sdownsj /*
10725ac2d602Sdownsj  * symlink -- make a symbolic link
10735ac2d602Sdownsj  */
10745ac2d602Sdownsj int
10755f64cd9cSjasper ext2fs_symlink(void *v)
10765ac2d602Sdownsj {
107799bc9d31Sderaadt 	struct vop_symlink_args *ap = v;
10785f64cd9cSjasper 	struct vnode *vp, **vpp = ap->a_vpp;
10795f64cd9cSjasper 	struct inode *ip;
10805ac2d602Sdownsj 	int len, error;
10815ac2d602Sdownsj 
10825ac2d602Sdownsj 	error = ext2fs_makeinode(IFLNK | ap->a_vap->va_mode, ap->a_dvp,
10835ac2d602Sdownsj 			      vpp, ap->a_cnp);
10845ac2d602Sdownsj 	if (error)
10855ac2d602Sdownsj 		return (error);
10865ac2d602Sdownsj 	vp = *vpp;
10875ac2d602Sdownsj 	len = strlen(ap->a_target);
10885ac2d602Sdownsj 	if (len < vp->v_mount->mnt_maxsymlinklen) {
10895ac2d602Sdownsj 		ip = VTOI(vp);
10902374824dSpedro 		bcopy(ap->a_target, (char *)ip->i_e2din->e2di_shortlink, len);
10914dcbbbb0Sniallo 		error = ext2fs_setsize(ip, len);
10924dcbbbb0Sniallo 		if (error)
10934dcbbbb0Sniallo 			goto bad;
10945ac2d602Sdownsj 		ip->i_flag |= IN_CHANGE | IN_UPDATE;
10955ac2d602Sdownsj 	} else
10965ac2d602Sdownsj 		error = vn_rdwr(UIO_WRITE, vp, ap->a_target, len, (off_t)0,
10979b355cb2Smillert 		    UIO_SYSSPACE, IO_NODELOCKED, ap->a_cnp->cn_cred, NULL,
10985ac2d602Sdownsj 		    (struct proc *)0);
10994dcbbbb0Sniallo bad:
11005ac2d602Sdownsj 	vput(vp);
11015ac2d602Sdownsj 	return (error);
11025ac2d602Sdownsj }
11035ac2d602Sdownsj 
11045ac2d602Sdownsj /*
11055ac2d602Sdownsj  * Return target name of a symbolic link
11065ac2d602Sdownsj  */
11075ac2d602Sdownsj int
11085f64cd9cSjasper ext2fs_readlink(void *v)
11095ac2d602Sdownsj {
111099bc9d31Sderaadt 	struct vop_readlink_args *ap = v;
11115f64cd9cSjasper 	struct vnode *vp = ap->a_vp;
11125f64cd9cSjasper 	struct inode *ip = VTOI(vp);
11135ac2d602Sdownsj 	int isize;
11145ac2d602Sdownsj 
11154dcbbbb0Sniallo 	isize = ext2fs_size(ip);
11165ac2d602Sdownsj 	if (isize < vp->v_mount->mnt_maxsymlinklen ||
11175ac2d602Sdownsj 	    (vp->v_mount->mnt_maxsymlinklen == 0 && ip->i_e2fs_nblock == 0)) {
1118*1330d409Smatthew 		return (uiomove((char *)ip->i_e2din->e2di_shortlink, isize,
1119*1330d409Smatthew 		    ap->a_uio));
11205ac2d602Sdownsj 	}
11215ac2d602Sdownsj 	return (VOP_READ(vp, ap->a_uio, 0, ap->a_cred));
11225ac2d602Sdownsj }
11235ac2d602Sdownsj 
11245ac2d602Sdownsj /*
11255ac2d602Sdownsj  * Advisory record locking support
11265ac2d602Sdownsj  */
11275ac2d602Sdownsj int
11285f64cd9cSjasper ext2fs_advlock(void *v)
11295ac2d602Sdownsj {
113099bc9d31Sderaadt 	struct vop_advlock_args *ap = v;
11315f64cd9cSjasper 	struct inode *ip = VTOI(ap->a_vp);
11325ac2d602Sdownsj 
11334dcbbbb0Sniallo 	return (lf_advlock(&ip->i_lockf, ext2fs_size(ip), ap->a_id, ap->a_op,
11345ac2d602Sdownsj 	    ap->a_fl, ap->a_flags));
11355ac2d602Sdownsj }
11365ac2d602Sdownsj 
11375ac2d602Sdownsj /*
11385ac2d602Sdownsj  * Allocate a new inode.
11395ac2d602Sdownsj  */
11405ac2d602Sdownsj int
11415f64cd9cSjasper ext2fs_makeinode(int mode, struct vnode *dvp, struct vnode **vpp,
11425f64cd9cSjasper     struct componentname *cnp)
11435ac2d602Sdownsj {
11445f64cd9cSjasper 	struct inode *ip, *pdir;
11455ac2d602Sdownsj 	struct vnode *tvp;
11465ac2d602Sdownsj 	int error;
11475ac2d602Sdownsj 
11485ac2d602Sdownsj 	pdir = VTOI(dvp);
11495ac2d602Sdownsj #ifdef DIAGNOSTIC
11505ac2d602Sdownsj 	if ((cnp->cn_flags & HASBUF) == 0)
11515ac2d602Sdownsj 		panic("ext2fs_makeinode: no name");
11525ac2d602Sdownsj #endif
11535ac2d602Sdownsj 	*vpp = NULL;
11545ac2d602Sdownsj 	if ((mode & IFMT) == 0)
11555ac2d602Sdownsj 		mode |= IFREG;
11565ac2d602Sdownsj 
1157b080ad39Scsapuntz 	if ((error = ext2fs_inode_alloc(pdir, mode, cnp->cn_cred, &tvp))
1158b080ad39Scsapuntz 	    != 0) {
1159dbe27ba0Stedu 		pool_put(&namei_pool, cnp->cn_pnbuf);
11605ac2d602Sdownsj 		vput(dvp);
11615ac2d602Sdownsj 		return (error);
11625ac2d602Sdownsj 	}
11635ac2d602Sdownsj 	ip = VTOI(tvp);
11645ac2d602Sdownsj 	ip->i_e2fs_gid = pdir->i_e2fs_gid;
11655ac2d602Sdownsj 	ip->i_e2fs_uid = cnp->cn_cred->cr_uid;
11665ac2d602Sdownsj 	ip->i_flag |= IN_ACCESS | IN_CHANGE | IN_UPDATE;
11675ac2d602Sdownsj 	ip->i_e2fs_mode = mode;
11685ac2d602Sdownsj 	tvp->v_type = IFTOVT(mode);	/* Rest init'd in getnewvnode(). */
11695ac2d602Sdownsj 	ip->i_e2fs_nlink = 1;
11705ac2d602Sdownsj 	if ((ip->i_e2fs_mode & ISGID) &&
11715ac2d602Sdownsj 		!groupmember(ip->i_e2fs_gid, cnp->cn_cred) &&
1172ad392958Stedu 	    suser_ucred(cnp->cn_cred))
11735ac2d602Sdownsj 		ip->i_e2fs_mode &= ~ISGID;
11745ac2d602Sdownsj 
11755ac2d602Sdownsj 	/*
11765ac2d602Sdownsj 	 * Make sure inode goes to disk before directory entry.
11775ac2d602Sdownsj 	 */
1178b080ad39Scsapuntz 	if ((error = ext2fs_update(ip, NULL, NULL, 1)) != 0)
11795ac2d602Sdownsj 		goto bad;
11805ac2d602Sdownsj 	error = ext2fs_direnter(ip, dvp, cnp);
11815ac2d602Sdownsj 	if (error != 0)
11825ac2d602Sdownsj 		goto bad;
11835ac2d602Sdownsj 	if ((cnp->cn_flags & SAVESTART) == 0)
1184dbe27ba0Stedu 		pool_put(&namei_pool, cnp->cn_pnbuf);
11855ac2d602Sdownsj 	vput(dvp);
11865ac2d602Sdownsj 	*vpp = tvp;
11875ac2d602Sdownsj 	return (0);
11885ac2d602Sdownsj 
11895ac2d602Sdownsj bad:
11905ac2d602Sdownsj 	/*
11915ac2d602Sdownsj 	 * Write error occurred trying to update the inode
11925ac2d602Sdownsj 	 * or the directory so must deallocate the inode.
11935ac2d602Sdownsj 	 */
1194dbe27ba0Stedu 	pool_put(&namei_pool, cnp->cn_pnbuf);
11955ac2d602Sdownsj 	vput(dvp);
11965ac2d602Sdownsj 	ip->i_e2fs_nlink = 0;
11975ac2d602Sdownsj 	ip->i_flag |= IN_CHANGE;
1198e318f6c7Stedu 	tvp->v_type = VNON;
11995ac2d602Sdownsj 	vput(tvp);
12005ac2d602Sdownsj 	return (error);
12015ac2d602Sdownsj }
12025ac2d602Sdownsj 
12035ac2d602Sdownsj /*
12047dc61945Sdownsj  * Synch an open file.
12057dc61945Sdownsj  */
12067dc61945Sdownsj /* ARGSUSED */
12077dc61945Sdownsj int
12085f64cd9cSjasper ext2fs_fsync(void *v)
12097dc61945Sdownsj {
121099bc9d31Sderaadt 	struct vop_fsync_args *ap = v;
12115f64cd9cSjasper 	struct vnode *vp = ap->a_vp;
12127dc61945Sdownsj 
12137dc61945Sdownsj 	vflushbuf(vp, ap->a_waitfor == MNT_WAIT);
1214b080ad39Scsapuntz 	return (ext2fs_update(VTOI(ap->a_vp), NULL, NULL,
1215b080ad39Scsapuntz 		    ap->a_waitfor == MNT_WAIT));
12167dc61945Sdownsj }
12177dc61945Sdownsj 
12187dc61945Sdownsj /*
12195ac2d602Sdownsj  * Reclaim an inode so that it can be used for other purposes.
12205ac2d602Sdownsj  */
12215ac2d602Sdownsj int
12225f64cd9cSjasper ext2fs_reclaim(void *v)
12235ac2d602Sdownsj {
122499bc9d31Sderaadt 	struct vop_reclaim_args *ap = v;
12255f64cd9cSjasper 	struct vnode *vp = ap->a_vp;
12265ac2d602Sdownsj 	struct inode *ip;
12274df17c52Spedro #ifdef DIAGNOSTIC
12285ac2d602Sdownsj 	extern int prtactive;
12295ac2d602Sdownsj 
12305ac2d602Sdownsj 	if (prtactive && vp->v_usecount != 0)
12315ac2d602Sdownsj 		vprint("ext2fs_reclaim: pushing active", vp);
12324df17c52Spedro #endif
1233d2e0ba60Spedro 
12345ac2d602Sdownsj 	/*
12355ac2d602Sdownsj 	 * Remove the inode from its hash chain.
12365ac2d602Sdownsj 	 */
12375ac2d602Sdownsj 	ip = VTOI(vp);
12385ac2d602Sdownsj 	ufs_ihashrem(ip);
1239d2e0ba60Spedro 
12405ac2d602Sdownsj 	/*
12415ac2d602Sdownsj 	 * Purge old data structures associated with the inode.
12425ac2d602Sdownsj 	 */
12435ac2d602Sdownsj 	cache_purge(vp);
1244d2e0ba60Spedro 	if (ip->i_devvp)
12455ac2d602Sdownsj 		vrele(ip->i_devvp);
12465ac2d602Sdownsj 
12472374824dSpedro 	if (ip->i_e2din != NULL)
12482374824dSpedro 		pool_put(&ext2fs_dinode_pool, ip->i_e2din);
12492374824dSpedro 
1250b7ade7a9Spedro 	pool_put(&ext2fs_inode_pool, ip);
12512374824dSpedro 
12525ac2d602Sdownsj 	vp->v_data = NULL;
1253d2e0ba60Spedro 
12545ac2d602Sdownsj 	return (0);
12555ac2d602Sdownsj }
12565ac2d602Sdownsj 
12575ac2d602Sdownsj /* Global vfs data structures for ext2fs. */
1258c4071fd1Smillert int (**ext2fs_vnodeop_p)(void *);
12595ac2d602Sdownsj struct vnodeopv_entry_desc ext2fs_vnodeop_entries[] = {
1260e265d2c7Sthib 	{ &vop_default_desc, eopnotsupp },
1261f34766f9Sjasper 	{ &vop_lookup_desc, ext2fs_lookup },
1262f34766f9Sjasper 	{ &vop_create_desc, ext2fs_create },
1263f34766f9Sjasper 	{ &vop_mknod_desc, ext2fs_mknod },
1264f34766f9Sjasper 	{ &vop_open_desc, ext2fs_open },
1265f34766f9Sjasper 	{ &vop_close_desc, ufs_close },
1266f34766f9Sjasper 	{ &vop_access_desc, ext2fs_access },
1267f34766f9Sjasper 	{ &vop_getattr_desc, ext2fs_getattr },
1268f34766f9Sjasper 	{ &vop_setattr_desc, ext2fs_setattr },
1269f34766f9Sjasper 	{ &vop_read_desc, ext2fs_read },
1270f34766f9Sjasper 	{ &vop_write_desc, ext2fs_write },
1271f34766f9Sjasper 	{ &vop_ioctl_desc, ufs_ioctl },
1272f34766f9Sjasper 	{ &vop_poll_desc, ufs_poll },
1273f34766f9Sjasper 	{ &vop_kqfilter_desc, vop_generic_kqfilter },
1274f34766f9Sjasper 	{ &vop_fsync_desc, ext2fs_fsync },
1275f34766f9Sjasper 	{ &vop_remove_desc, ext2fs_remove },
1276f34766f9Sjasper 	{ &vop_link_desc, ext2fs_link },
1277f34766f9Sjasper 	{ &vop_rename_desc, ext2fs_rename },
1278f34766f9Sjasper 	{ &vop_mkdir_desc, ext2fs_mkdir },
1279f34766f9Sjasper 	{ &vop_rmdir_desc, ext2fs_rmdir },
1280f34766f9Sjasper 	{ &vop_symlink_desc, ext2fs_symlink },
1281f34766f9Sjasper 	{ &vop_readdir_desc, ext2fs_readdir },
1282f34766f9Sjasper 	{ &vop_readlink_desc, ext2fs_readlink },
1283f34766f9Sjasper 	{ &vop_abortop_desc, vop_generic_abortop },
1284f34766f9Sjasper 	{ &vop_inactive_desc, ext2fs_inactive },
1285f34766f9Sjasper 	{ &vop_reclaim_desc, ext2fs_reclaim },
1286f34766f9Sjasper 	{ &vop_lock_desc, ufs_lock },
1287f34766f9Sjasper 	{ &vop_unlock_desc, ufs_unlock },
1288f34766f9Sjasper 	{ &vop_bmap_desc, ext2fs_bmap },
1289f34766f9Sjasper 	{ &vop_strategy_desc, ufs_strategy },
1290f34766f9Sjasper 	{ &vop_print_desc, ufs_print },
1291f34766f9Sjasper 	{ &vop_islocked_desc, ufs_islocked },
1292f34766f9Sjasper 	{ &vop_pathconf_desc, ufs_pathconf },
1293f34766f9Sjasper 	{ &vop_advlock_desc, ext2fs_advlock },
1294f34766f9Sjasper 	{ &vop_bwrite_desc, vop_generic_bwrite },
1295c20cb638Sart 	{ NULL, NULL }
12965ac2d602Sdownsj };
12975ac2d602Sdownsj struct vnodeopv_desc ext2fs_vnodeop_opv_desc =
12985ac2d602Sdownsj 	{ &ext2fs_vnodeop_p, ext2fs_vnodeop_entries };
12995ac2d602Sdownsj 
1300c4071fd1Smillert int (**ext2fs_specop_p)(void *);
13015ac2d602Sdownsj struct vnodeopv_entry_desc ext2fs_specop_entries[] = {
1302c20cb638Sart 	{ &vop_default_desc, spec_vnoperate },
1303f34766f9Sjasper 	{ &vop_close_desc, ufsspec_close },
1304f34766f9Sjasper 	{ &vop_access_desc, ext2fs_access },
1305f34766f9Sjasper 	{ &vop_getattr_desc, ext2fs_getattr },
1306f34766f9Sjasper 	{ &vop_setattr_desc, ext2fs_setattr },
1307f34766f9Sjasper 	{ &vop_read_desc, ufsspec_read },
1308f34766f9Sjasper 	{ &vop_write_desc, ufsspec_write },
1309f34766f9Sjasper 	{ &vop_fsync_desc, ext2fs_fsync },
1310f34766f9Sjasper 	{ &vop_inactive_desc, ext2fs_inactive },
1311f34766f9Sjasper 	{ &vop_reclaim_desc, ext2fs_reclaim },
1312f34766f9Sjasper 	{ &vop_lock_desc, ufs_lock },
1313f34766f9Sjasper 	{ &vop_unlock_desc, ufs_unlock },
1314f34766f9Sjasper 	{ &vop_print_desc, ufs_print },
1315f34766f9Sjasper 	{ &vop_islocked_desc, ufs_islocked },
1316c20cb638Sart 	{ NULL, NULL }
13175ac2d602Sdownsj };
13185ac2d602Sdownsj struct vnodeopv_desc ext2fs_specop_opv_desc =
13195ac2d602Sdownsj 	{ &ext2fs_specop_p, ext2fs_specop_entries };
13205ac2d602Sdownsj 
13215ac2d602Sdownsj #ifdef FIFO
1322c4071fd1Smillert int (**ext2fs_fifoop_p)(void *);
13235ac2d602Sdownsj struct vnodeopv_entry_desc ext2fs_fifoop_entries[] = {
1324c20cb638Sart 	{ &vop_default_desc, fifo_vnoperate },
1325f34766f9Sjasper 	{ &vop_close_desc, ufsfifo_close },
1326f34766f9Sjasper 	{ &vop_access_desc, ext2fs_access },
1327f34766f9Sjasper 	{ &vop_getattr_desc, ext2fs_getattr },
1328f34766f9Sjasper 	{ &vop_setattr_desc, ext2fs_setattr },
1329f34766f9Sjasper 	{ &vop_read_desc, ufsfifo_read },
1330f34766f9Sjasper 	{ &vop_write_desc, ufsfifo_write },
1331f34766f9Sjasper 	{ &vop_fsync_desc, ext2fs_fsync },
1332f34766f9Sjasper 	{ &vop_inactive_desc, ext2fs_inactive },
1333f34766f9Sjasper 	{ &vop_reclaim_desc, ext2fsfifo_reclaim },
1334f34766f9Sjasper 	{ &vop_lock_desc, ufs_lock },
1335f34766f9Sjasper 	{ &vop_unlock_desc, ufs_unlock },
1336f34766f9Sjasper 	{ &vop_print_desc, ufs_print },
1337f34766f9Sjasper 	{ &vop_islocked_desc, ufs_islocked },
1338f34766f9Sjasper 	{ &vop_bwrite_desc, vop_generic_bwrite },
1339c20cb638Sart 	{ NULL, NULL }
13405ac2d602Sdownsj };
13415ac2d602Sdownsj struct vnodeopv_desc ext2fs_fifoop_opv_desc =
13425ac2d602Sdownsj 	{ &ext2fs_fifoop_p, ext2fs_fifoop_entries };
13431816238dStedu 
13441816238dStedu int
13451816238dStedu ext2fsfifo_reclaim(void *v)
13461816238dStedu {
13471816238dStedu 	fifo_reclaim(v);
13481816238dStedu 	return (ext2fs_reclaim(v));
13491816238dStedu }
13505ac2d602Sdownsj #endif /* FIFO */
1351