xref: /csrg-svn/sys/ufs/lfs/lfs_vnops.c (revision 38069)
123405Smckusick /*
237737Smckusick  * Copyright (c) 1982, 1986, 1989 Regents of the University of California.
337737Smckusick  * All rights reserved.
423405Smckusick  *
537737Smckusick  * Redistribution and use in source and binary forms are permitted
637737Smckusick  * provided that the above copyright notice and this paragraph are
737737Smckusick  * duplicated in all such forms and that any documentation,
837737Smckusick  * advertising materials, and other materials related to such
937737Smckusick  * distribution and use acknowledge that the software was developed
1037737Smckusick  * by the University of California, Berkeley.  The name of the
1137737Smckusick  * University may not be used to endorse or promote products derived
1237737Smckusick  * from this software without specific prior written permission.
1337737Smckusick  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
1437737Smckusick  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
1537737Smckusick  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
1637737Smckusick  *
17*38069Smckusick  *	@(#)lfs_vnops.c	7.10 (Berkeley) 05/19/89
1823405Smckusick  */
1937Sbill 
2017101Sbloom #include "param.h"
2117101Sbloom #include "systm.h"
2217101Sbloom #include "user.h"
2317101Sbloom #include "kernel.h"
2417101Sbloom #include "file.h"
2517101Sbloom #include "stat.h"
2617101Sbloom #include "buf.h"
2717101Sbloom #include "proc.h"
2817101Sbloom #include "uio.h"
2917101Sbloom #include "socket.h"
3017101Sbloom #include "socketvar.h"
3137737Smckusick #include "conf.h"
3217101Sbloom #include "mount.h"
3337737Smckusick #include "vnode.h"
3437737Smckusick #include "../ufs/inode.h"
3537737Smckusick #include "../ufs/fs.h"
3637737Smckusick #include "../ufs/quota.h"
3737Sbill 
389167Ssam /*
3937737Smckusick  * Global vfs data structures for ufs
409167Ssam  */
416254Sroot 
4237737Smckusick int	ufs_lookup(),
4337737Smckusick 	ufs_create(),
4437737Smckusick 	ufs_mknod(),
4537737Smckusick 	ufs_open(),
4637737Smckusick 	ufs_close(),
4737737Smckusick 	ufs_access(),
4837737Smckusick 	ufs_getattr(),
4937737Smckusick 	ufs_setattr(),
5037737Smckusick 	ufs_read(),
5137737Smckusick 	ufs_write(),
5237737Smckusick 	ufs_ioctl(),
5337737Smckusick 	ufs_select(),
5437737Smckusick 	ufs_mmap(),
5537737Smckusick 	ufs_fsync(),
5637737Smckusick 	ufs_seek(),
5737737Smckusick 	ufs_remove(),
5837737Smckusick 	ufs_link(),
5937737Smckusick 	ufs_rename(),
6037737Smckusick 	ufs_mkdir(),
6137737Smckusick 	ufs_rmdir(),
6237737Smckusick 	ufs_symlink(),
6337737Smckusick 	ufs_readdir(),
6437737Smckusick 	ufs_readlink(),
6537737Smckusick 	ufs_abortop(),
6637737Smckusick 	ufs_inactive(),
6737737Smckusick 	ufs_lock(),
6837737Smckusick 	ufs_unlock(),
6937737Smckusick 	ufs_bmap(),
7037737Smckusick 	ufs_strategy();
716254Sroot 
7237737Smckusick struct vnodeops ufs_vnodeops = {
7337737Smckusick 	ufs_lookup,
7437737Smckusick 	ufs_create,
7537737Smckusick 	ufs_mknod,
7637737Smckusick 	ufs_open,
7737737Smckusick 	ufs_close,
7837737Smckusick 	ufs_access,
7937737Smckusick 	ufs_getattr,
8037737Smckusick 	ufs_setattr,
8137737Smckusick 	ufs_read,
8237737Smckusick 	ufs_write,
8337737Smckusick 	ufs_ioctl,
8437737Smckusick 	ufs_select,
8537737Smckusick 	ufs_mmap,
8637737Smckusick 	ufs_fsync,
8737737Smckusick 	ufs_seek,
8837737Smckusick 	ufs_remove,
8937737Smckusick 	ufs_link,
9037737Smckusick 	ufs_rename,
9137737Smckusick 	ufs_mkdir,
9237737Smckusick 	ufs_rmdir,
9337737Smckusick 	ufs_symlink,
9437737Smckusick 	ufs_readdir,
9537737Smckusick 	ufs_readlink,
9637737Smckusick 	ufs_abortop,
9737737Smckusick 	ufs_inactive,
9837737Smckusick 	ufs_lock,
9937737Smckusick 	ufs_unlock,
10037737Smckusick 	ufs_bmap,
10137737Smckusick 	ufs_strategy,
10237737Smckusick };
1036254Sroot 
10437737Smckusick enum vtype iftovt_tab[8] = {
10537737Smckusick 	VNON, VCHR, VDIR, VBLK, VREG, VLNK, VSOCK, VBAD,
10637737Smckusick };
10737737Smckusick int	vttoif_tab[8] = {
10837737Smckusick 	0, IFREG, IFDIR, IFBLK, IFCHR, IFLNK, IFSOCK, IFMT,
10937737Smckusick };
1106254Sroot 
1119167Ssam /*
11237737Smckusick  * Create a regular file
1139167Ssam  */
11437737Smckusick ufs_create(ndp, vap)
11537737Smckusick 	struct nameidata *ndp;
11637737Smckusick 	struct vattr *vap;
1176254Sroot {
11837737Smckusick 	struct inode *ip;
11937737Smckusick 	int error;
1206254Sroot 
12137737Smckusick 	if (error = maknode(MAKEIMODE(vap->va_type, vap->va_mode), ndp, &ip))
12237737Smckusick 		return (error);
12337737Smckusick 	ndp->ni_vp = ITOV(ip);
12437737Smckusick 	return (0);
1256254Sroot }
1266254Sroot 
12737Sbill /*
12837737Smckusick  * Mknod vnode call
1296254Sroot  */
13037737Smckusick /* ARGSUSED */
13137737Smckusick ufs_mknod(ndp, vap, cred)
13237737Smckusick 	struct nameidata *ndp;
13337737Smckusick 	struct ucred *cred;
13437737Smckusick 	struct vattr *vap;
1356254Sroot {
13637737Smckusick 	struct inode *ip;
13737737Smckusick 	int error;
1386254Sroot 
13937737Smckusick 	if (error = maknode(MAKEIMODE(vap->va_type, vap->va_mode), ndp, &ip))
14037737Smckusick 		return (error);
14137737Smckusick 	if (vap->va_rdev) {
14237737Smckusick 		/*
14337737Smckusick 		 * Want to be able to use this to make badblock
14437737Smckusick 		 * inodes, so don't truncate the dev number.
14537737Smckusick 		 */
14637737Smckusick 		ITOV(ip)->v_rdev = ip->i_rdev = vap->va_rdev;
14737737Smckusick 		ip->i_flag |= IACC|IUPD|ICHG;
14812756Ssam 	}
1497701Ssam 	iput(ip);
15037737Smckusick 	/*
15137737Smckusick 	 * Remove inode so that it will be reloaded by iget and
15237737Smckusick 	 * checked to see if it is an alias of an existing entry
15337737Smckusick 	 * in the inode cache.
15437737Smckusick 	 */
15537737Smckusick 	remque(ip);
15637737Smckusick 	ip->i_forw = ip;
15737737Smckusick 	ip->i_back = ip;
15837737Smckusick 	return (0);
1596254Sroot }
1606254Sroot 
1616254Sroot /*
16237737Smckusick  * Open called.
16337737Smckusick  *
16437737Smckusick  * Nothing to do.
1656254Sroot  */
16637737Smckusick /* ARGSUSED */
16737737Smckusick ufs_open(vp, mode, cred)
16837737Smckusick 	struct vnode *vp;
16937737Smckusick 	int mode;
17037737Smckusick 	struct ucred *cred;
1716254Sroot {
1726254Sroot 
17337737Smckusick 	return (0);
1746254Sroot }
1756254Sroot 
1766254Sroot /*
17737737Smckusick  * Close called
17837737Smckusick  *
17937737Smckusick  * Update the times on the inode.
1806254Sroot  */
18137737Smckusick /* ARGSUSED */
18237737Smckusick ufs_close(vp, fflag, cred)
18337737Smckusick 	struct vnode *vp;
18437737Smckusick 	int fflag;
18537737Smckusick 	struct ucred *cred;
1866254Sroot {
18737737Smckusick 	register struct inode *ip = VTOI(vp);
1886254Sroot 
18937737Smckusick 	if (vp->v_count > 1 && !(ip->i_flag & ILOCKED))
19037737Smckusick 		ITIMES(ip, &time, &time);
19137737Smckusick 	return (0);
1926254Sroot }
1936254Sroot 
19437737Smckusick ufs_access(vp, mode, cred)
19537737Smckusick 	struct vnode *vp;
19637737Smckusick 	int mode;
19737737Smckusick 	struct ucred *cred;
1986254Sroot {
1996254Sroot 
20037737Smckusick 	return (iaccess(VTOI(vp), mode, cred));
2016254Sroot }
2026254Sroot 
20337737Smckusick /* ARGSUSED */
20437737Smckusick ufs_getattr(vp, vap, cred)
20537737Smckusick 	struct vnode *vp;
20637737Smckusick 	register struct vattr *vap;
20737737Smckusick 	struct ucred *cred;
2086254Sroot {
20937737Smckusick 	register struct inode *ip = VTOI(vp);
2106254Sroot 
21137737Smckusick 	ITIMES(ip, &time, &time);
2126254Sroot 	/*
21337737Smckusick 	 * Copy from inode table
2146254Sroot 	 */
21537737Smckusick 	vap->va_fsid = ip->i_dev;
21637737Smckusick 	vap->va_fileid = ip->i_number;
21737737Smckusick 	vap->va_mode = ip->i_mode & ~IFMT;
21837737Smckusick 	vap->va_nlink = ip->i_nlink;
21937737Smckusick 	vap->va_uid = ip->i_uid;
22037737Smckusick 	vap->va_gid = ip->i_gid;
22137737Smckusick 	vap->va_rdev = (dev_t)ip->i_rdev;
22237737Smckusick 	vap->va_size = ip->i_ic.ic_size.val[0];
22337737Smckusick 	vap->va_size1 = ip->i_ic.ic_size.val[1];
22437737Smckusick 	vap->va_atime.tv_sec = ip->i_atime;
22537737Smckusick 	vap->va_mtime.tv_sec = ip->i_mtime;
22637737Smckusick 	vap->va_ctime.tv_sec = ip->i_ctime;
22737737Smckusick 	/* this doesn't belong here */
22837737Smckusick 	if (vp->v_type == VBLK)
22937737Smckusick 		vap->va_blocksize = BLKDEV_IOSIZE;
23037737Smckusick 	else if (vp->v_type == VCHR)
23137737Smckusick 		vap->va_blocksize = MAXBSIZE;
2327142Smckusick 	else
23337737Smckusick 		vap->va_blocksize = ip->i_fs->fs_bsize;
23437737Smckusick 	/*
23537737Smckusick 	 * XXX THIS IS NOT CORRECT!!, but be sure to change vn_stat()
23637737Smckusick 	 * if you change it.
23737737Smckusick 	 */
23837737Smckusick 	vap->va_bytes = ip->i_blocks;
23937737Smckusick 	vap->va_bytes1 = -1;
24037737Smckusick 	vap->va_type = vp->v_type;
24137737Smckusick 	return (0);
2426254Sroot }
2436254Sroot 
2446254Sroot /*
24537737Smckusick  * Set attribute vnode op. called from several syscalls
2466254Sroot  */
24737737Smckusick ufs_setattr(vp, vap, cred)
24837737Smckusick 	register struct vnode *vp;
24937737Smckusick 	register struct vattr *vap;
25037737Smckusick 	register struct ucred *cred;
2516254Sroot {
25237737Smckusick 	register struct inode *ip = VTOI(vp);
25337737Smckusick 	int error = 0;
2546254Sroot 
25537737Smckusick 	/*
25637737Smckusick 	 * Check for unsetable attributes.
25737737Smckusick 	 */
25837737Smckusick 	if ((vap->va_type != VNON) || (vap->va_nlink != VNOVAL) ||
25937737Smckusick 	    (vap->va_fsid != VNOVAL) || (vap->va_fileid != VNOVAL) ||
26037737Smckusick 	    (vap->va_blocksize != VNOVAL) || (vap->va_rdev != VNOVAL) ||
26137737Smckusick 	    ((int)vap->va_bytes != VNOVAL)) {
26237737Smckusick 		return (EINVAL);
26316540Ssam 	}
26437737Smckusick 	/*
26537737Smckusick 	 * Go through the fields and update iff not VNOVAL.
26637737Smckusick 	 */
26737737Smckusick 	if (vap->va_uid != (u_short)VNOVAL || vap->va_gid != (u_short)VNOVAL)
26837737Smckusick 		if (error = chown1(vp, vap->va_uid, vap->va_gid, cred))
26937737Smckusick 			return (error);
27037737Smckusick 	if (vap->va_size != VNOVAL) {
27137737Smckusick 		if (vp->v_type == VDIR)
27237737Smckusick 			return (EISDIR);
27337737Smckusick 		if (error = iaccess(ip, IWRITE, cred))
27437737Smckusick 			return (error);
27537737Smckusick 		if (error = itrunc(ip, vap->va_size))
27637737Smckusick 			return (error);
27713878Ssam 	}
27837737Smckusick 	if (vap->va_atime.tv_sec != VNOVAL || vap->va_mtime.tv_sec != VNOVAL) {
27937773Smckusick 		if (cred->cr_uid != ip->i_uid &&
28037773Smckusick 		    (error = suser(cred, &u.u_acflag)))
28137773Smckusick 			return (error);
28237737Smckusick 		if (vap->va_atime.tv_sec != VNOVAL)
28337737Smckusick 			ip->i_flag |= IACC;
28437737Smckusick 		if (vap->va_mtime.tv_sec != VNOVAL)
28537737Smckusick 			ip->i_flag |= IUPD;
28637737Smckusick 		ip->i_flag |= ICHG;
28737737Smckusick 		if (error = iupdat(ip, &vap->va_atime, &vap->va_mtime, 1))
28837737Smckusick 			return (error);
2896254Sroot 	}
29037737Smckusick 	if (vap->va_mode != (u_short)VNOVAL)
29137737Smckusick 		error = chmod1(vp, (int)vap->va_mode, cred);
29237737Smckusick 	return (error);
2936254Sroot }
2946254Sroot 
2956254Sroot /*
2969167Ssam  * Change the mode on a file.
2979167Ssam  * Inode must be locked before calling.
2989167Ssam  */
29937737Smckusick chmod1(vp, mode, cred)
30037737Smckusick 	register struct vnode *vp;
3017701Ssam 	register int mode;
30237737Smckusick 	struct ucred *cred;
3037701Ssam {
30437737Smckusick 	register struct inode *ip = VTOI(vp);
30537773Smckusick 	int error;
3067868Sroot 
30737773Smckusick 	if (cred->cr_uid != ip->i_uid &&
30837773Smckusick 	    (error = suser(cred, &u.u_acflag)))
30937773Smckusick 		return (error);
3106254Sroot 	ip->i_mode &= ~07777;
31137737Smckusick 	if (cred->cr_uid) {
31237737Smckusick 		if (vp->v_type != VDIR)
31321015Smckusick 			mode &= ~ISVTX;
31437737Smckusick 		if (!groupmember(ip->i_gid, cred))
31511811Ssam 			mode &= ~ISGID;
3167439Sroot 	}
31737737Smckusick 	ip->i_mode |= mode & 07777;
3186254Sroot 	ip->i_flag |= ICHG;
31937737Smckusick 	if ((vp->v_flag & VTEXT) && (ip->i_mode & ISVTX) == 0)
32037737Smckusick 		xrele(vp);
32121015Smckusick 	return (0);
3225992Swnj }
3235992Swnj 
3249167Ssam /*
3257701Ssam  * Perform chown operation on inode ip;
3267701Ssam  * inode must be locked prior to call.
3277701Ssam  */
32837737Smckusick chown1(vp, uid, gid, cred)
32937737Smckusick 	register struct vnode *vp;
33037737Smckusick 	uid_t uid;
33137737Smckusick 	gid_t gid;
33237737Smckusick 	struct ucred *cred;
3337701Ssam {
33437737Smckusick 	register struct inode *ip = VTOI(vp);
3357701Ssam #ifdef QUOTA
3367701Ssam 	register long change;
33711811Ssam #endif
33837737Smckusick 	int error;
3397701Ssam 
34037737Smckusick 	if (uid == (u_short)VNOVAL)
34111811Ssam 		uid = ip->i_uid;
34237737Smckusick 	if (gid == (u_short)VNOVAL)
34311811Ssam 		gid = ip->i_gid;
34436614Sbostic 	/*
34536614Sbostic 	 * If we don't own the file, are trying to change the owner
34636614Sbostic 	 * of the file, or are not a member of the target group,
34736614Sbostic 	 * the caller must be superuser or the call fails.
34836614Sbostic 	 */
34937737Smckusick 	if ((cred->cr_uid != ip->i_uid || uid != ip->i_uid ||
35037737Smckusick 	    !groupmember((gid_t)gid, cred)) &&
35137737Smckusick 	    (error = suser(cred, &u.u_acflag)))
35237737Smckusick 		return (error);
35311811Ssam #ifdef QUOTA
35414385Ssam 	if (ip->i_uid == uid)		/* this just speeds things a little */
3557482Skre 		change = 0;
35612646Ssam 	else
35712646Ssam 		change = ip->i_blocks;
35812646Ssam 	(void) chkdq(ip, -change, 1);
35912646Ssam 	(void) chkiq(ip->i_dev, ip, ip->i_uid, 1);
3607482Skre 	dqrele(ip->i_dquot);
3617482Skre #endif
36211811Ssam 	ip->i_uid = uid;
36311811Ssam 	ip->i_gid = gid;
3646254Sroot 	ip->i_flag |= ICHG;
36537737Smckusick 	if (cred->cr_ruid != 0)
3666254Sroot 		ip->i_mode &= ~(ISUID|ISGID);
3677701Ssam #ifdef QUOTA
3687482Skre 	ip->i_dquot = inoquota(ip);
36912646Ssam 	(void) chkdq(ip, change, 1);
37026361Skarels 	(void) chkiq(ip->i_dev, (struct inode *)NULL, (uid_t)uid, 1);
37112646Ssam 	return (u.u_error);		/* should == 0 ALWAYS !! */
37212646Ssam #else
37312646Ssam 	return (0);
3747482Skre #endif
37537Sbill }
37637Sbill 
37737737Smckusick /* ARGSUSED */
37837737Smckusick ufs_ioctl(vp, com, data, fflag, cred)
37937737Smckusick 	struct vnode *vp;
38037737Smckusick 	int com;
38137737Smckusick 	caddr_t data;
38237737Smckusick 	int fflag;
38337737Smckusick 	struct ucred *cred;
38411811Ssam {
38511811Ssam 
38637737Smckusick 	printf("ufs_ioctl called with type %d\n", vp->v_type);
38737737Smckusick 	return (ENOTTY);
38811811Ssam }
38911811Ssam 
39037737Smckusick /* ARGSUSED */
39137737Smckusick ufs_select(vp, which, cred)
39237737Smckusick 	struct vnode *vp;
39337737Smckusick 	int which;
39437737Smckusick 	struct ucred *cred;
39537737Smckusick {
39637737Smckusick 
39737737Smckusick 	printf("ufs_select called with type %d\n", vp->v_type);
39837737Smckusick 	return (1);		/* XXX */
39937737Smckusick }
40037737Smckusick 
4019167Ssam /*
40237737Smckusick  * Mmap a file
40337737Smckusick  *
40437737Smckusick  * NB Currently unsupported.
4059167Ssam  */
40637737Smckusick /* ARGSUSED */
40737737Smckusick ufs_mmap(vp, fflags, cred)
40837737Smckusick 	struct vnode *vp;
40937737Smckusick 	int fflags;
41037737Smckusick 	struct ucred *cred;
41137Sbill {
41237Sbill 
41337737Smckusick 	return (EINVAL);
41437Sbill }
4157535Sroot 
4169167Ssam /*
41737737Smckusick  * Synch an open file.
4189167Ssam  */
41937737Smckusick /* ARGSUSED */
42037737Smckusick ufs_fsync(vp, fflags, cred)
42137737Smckusick 	struct vnode *vp;
42237737Smckusick 	int fflags;
42337737Smckusick 	struct ucred *cred;
4247701Ssam {
42537737Smckusick 	register struct inode *ip = VTOI(vp);
42637737Smckusick 	int error;
4277701Ssam 
42837737Smckusick 	ILOCK(ip);
42937737Smckusick 	if (fflags&FWRITE)
43037737Smckusick 		ip->i_flag |= ICHG;
43137737Smckusick 	error = syncip(ip);
43237737Smckusick 	IUNLOCK(ip);
43337737Smckusick 	return (error);
4347701Ssam }
4357701Ssam 
4369167Ssam /*
43737737Smckusick  * Seek on a file
43837737Smckusick  *
43937737Smckusick  * Nothing to do, so just return.
4409167Ssam  */
44137737Smckusick /* ARGSUSED */
44237737Smckusick ufs_seek(vp, oldoff, newoff, cred)
44337737Smckusick 	struct vnode *vp;
44437737Smckusick 	off_t oldoff, newoff;
44537737Smckusick 	struct ucred *cred;
4467701Ssam {
4477701Ssam 
44837737Smckusick 	return (0);
44937737Smckusick }
45037737Smckusick 
45137737Smckusick /*
45237737Smckusick  * ufs remove
45337737Smckusick  * Hard to avoid races here, especially
45437737Smckusick  * in unlinking directories.
45537737Smckusick  */
45637737Smckusick ufs_remove(ndp)
45737737Smckusick 	struct nameidata *ndp;
45837737Smckusick {
45937737Smckusick 	register struct inode *ip, *dp;
46037737Smckusick 	int error;
46137737Smckusick 
46237737Smckusick 	ip = VTOI(ndp->ni_vp);
46337737Smckusick 	dp = VTOI(ndp->ni_dvp);
46437737Smckusick 	error = dirremove(ndp);
46537737Smckusick 	if (!error) {
46637737Smckusick 		ip->i_nlink--;
46737737Smckusick 		ip->i_flag |= ICHG;
4687701Ssam 	}
46937737Smckusick 	if (dp == ip)
47037737Smckusick 		vrele(ITOV(ip));
47137737Smckusick 	else
47237737Smckusick 		iput(ip);
47337737Smckusick 	iput(dp);
47437737Smckusick 	return (error);
4757701Ssam }
4767701Ssam 
4779167Ssam /*
47837737Smckusick  * link vnode call
4799167Ssam  */
48037737Smckusick ufs_link(vp, ndp)
48137737Smckusick 	register struct vnode *vp;
48237737Smckusick 	register struct nameidata *ndp;
4839167Ssam {
48437737Smckusick 	register struct inode *ip = VTOI(vp);
48537737Smckusick 	int error;
4869167Ssam 
48737737Smckusick 	if (ndp->ni_dvp != vp)
48837737Smckusick 		ILOCK(ip);
48937737Smckusick 	if (ip->i_nlink == LINK_MAX - 1) {
49037737Smckusick 		error = EMLINK;
49137737Smckusick 		goto out;
49237737Smckusick 	}
49337737Smckusick 	ip->i_nlink++;
49437737Smckusick 	ip->i_flag |= ICHG;
49537737Smckusick 	error = iupdat(ip, &time, &time, 1);
49637737Smckusick 	if (!error)
49737737Smckusick 		error = direnter(ip, ndp);
49837737Smckusick out:
49937737Smckusick 	if (ndp->ni_dvp != vp)
50037737Smckusick 		IUNLOCK(ip);
50137737Smckusick 	if (error) {
50237737Smckusick 		ip->i_nlink--;
50330598Smckusick 		ip->i_flag |= ICHG;
50437737Smckusick 	}
50537737Smckusick 	return (error);
5069167Ssam }
5079167Ssam 
5089167Ssam /*
5099167Ssam  * Rename system call.
5109167Ssam  * 	rename("foo", "bar");
5119167Ssam  * is essentially
5129167Ssam  *	unlink("bar");
5139167Ssam  *	link("foo", "bar");
5149167Ssam  *	unlink("foo");
5159167Ssam  * but ``atomically''.  Can't do full commit without saving state in the
5169167Ssam  * inode on disk which isn't feasible at this time.  Best we can do is
5179167Ssam  * always guarantee the target exists.
5189167Ssam  *
5199167Ssam  * Basic algorithm is:
5209167Ssam  *
5219167Ssam  * 1) Bump link count on source while we're linking it to the
52237737Smckusick  *    target.  This also ensure the inode won't be deleted out
52316776Smckusick  *    from underneath us while we work (it may be truncated by
52416776Smckusick  *    a concurrent `trunc' or `open' for creation).
5259167Ssam  * 2) Link source to destination.  If destination already exists,
5269167Ssam  *    delete it first.
52716776Smckusick  * 3) Unlink source reference to inode if still around. If a
52816776Smckusick  *    directory was moved and the parent of the destination
5299167Ssam  *    is different from the source, patch the ".." entry in the
5309167Ssam  *    directory.
5319167Ssam  */
53237737Smckusick ufs_rename(fndp, tndp)
53337737Smckusick 	register struct nameidata *fndp, *tndp;
5347701Ssam {
5359167Ssam 	register struct inode *ip, *xp, *dp;
53616776Smckusick 	struct dirtemplate dirbuf;
53716776Smckusick 	int doingdirectory = 0, oldparent = 0, newparent = 0;
53810051Ssam 	int error = 0;
5397701Ssam 
54037737Smckusick 	dp = VTOI(fndp->ni_dvp);
54137737Smckusick 	ip = VTOI(fndp->ni_vp);
54237737Smckusick 	ILOCK(ip);
5439167Ssam 	if ((ip->i_mode&IFMT) == IFDIR) {
54437737Smckusick 		register struct direct *d = &fndp->ni_dent;
5459167Ssam 
5469167Ssam 		/*
54711641Ssam 		 * Avoid ".", "..", and aliases of "." for obvious reasons.
5489167Ssam 		 */
54937737Smckusick 		if ((d->d_namlen == 1 && d->d_name[0] == '.') || dp == ip ||
55037737Smckusick 		    fndp->ni_isdotdot || (ip->i_flag & IRENAME)) {
55137737Smckusick 			IUNLOCK(ip);
55237737Smckusick 			ufs_abortop(fndp);
55337737Smckusick 			ufs_abortop(tndp);
55437737Smckusick 			return (EINVAL);
5559167Ssam 		}
55616776Smckusick 		ip->i_flag |= IRENAME;
5579167Ssam 		oldparent = dp->i_number;
5589167Ssam 		doingdirectory++;
5599167Ssam 	}
56037737Smckusick 	vrele(fndp->ni_dvp);
5619167Ssam 
5629167Ssam 	/*
5639167Ssam 	 * 1) Bump link count while we're moving stuff
5649167Ssam 	 *    around.  If we crash somewhere before
5659167Ssam 	 *    completing our work, the link count
5669167Ssam 	 *    may be wrong, but correctable.
5679167Ssam 	 */
5689167Ssam 	ip->i_nlink++;
5699167Ssam 	ip->i_flag |= ICHG;
57037737Smckusick 	error = iupdat(ip, &time, &time, 1);
57116664Smckusick 	IUNLOCK(ip);
5729167Ssam 
5739167Ssam 	/*
5749167Ssam 	 * When the target exists, both the directory
57537737Smckusick 	 * and target vnodes are returned locked.
5769167Ssam 	 */
57737737Smckusick 	dp = VTOI(tndp->ni_dvp);
57837737Smckusick 	xp = NULL;
57937737Smckusick 	if (tndp->ni_vp)
58037737Smckusick 		xp = VTOI(tndp->ni_vp);
5819167Ssam 	/*
58211641Ssam 	 * If ".." must be changed (ie the directory gets a new
58312816Smckusick 	 * parent) then the source directory must not be in the
58412816Smckusick 	 * directory heirarchy above the target, as this would
58512816Smckusick 	 * orphan everything below the source directory. Also
58612816Smckusick 	 * the user must have write permission in the source so
58712816Smckusick 	 * as to be able to change "..". We must repeat the call
58812816Smckusick 	 * to namei, as the parent directory is unlocked by the
58912816Smckusick 	 * call to checkpath().
59011641Ssam 	 */
59116776Smckusick 	if (oldparent != dp->i_number)
59216776Smckusick 		newparent = dp->i_number;
59316776Smckusick 	if (doingdirectory && newparent) {
59437737Smckusick 		if (error = iaccess(ip, IWRITE, tndp->ni_cred))
59512816Smckusick 			goto bad;
59637737Smckusick 		tndp->ni_nameiop = RENAME | LOCKPARENT | LOCKLEAF | NOCACHE;
59712816Smckusick 		do {
59837737Smckusick 			dp = VTOI(tndp->ni_dvp);
59912816Smckusick 			if (xp != NULL)
600*38069Smckusick 				iput(xp);
60137737Smckusick 			if (error = checkpath(ip, dp, tndp->ni_cred))
60212816Smckusick 				goto out;
60337737Smckusick 			if (error = namei(tndp))
60412816Smckusick 				goto out;
60537737Smckusick 			xp = NULL;
60637737Smckusick 			if (tndp->ni_vp)
60737737Smckusick 				xp = VTOI(tndp->ni_vp);
60837737Smckusick 		} while (dp != VTOI(tndp->ni_dvp));
60912816Smckusick 	}
61011641Ssam 	/*
6119167Ssam 	 * 2) If target doesn't exist, link the target
6129167Ssam 	 *    to the source and unlink the source.
6139167Ssam 	 *    Otherwise, rewrite the target directory
6149167Ssam 	 *    entry to reference the source inode and
6159167Ssam 	 *    expunge the original entry's existence.
6169167Ssam 	 */
6179167Ssam 	if (xp == NULL) {
61837737Smckusick 		if (dp->i_dev != ip->i_dev)
61937737Smckusick 			panic("rename: EXDEV");
6209167Ssam 		/*
62116776Smckusick 		 * Account for ".." in new directory.
62216776Smckusick 		 * When source and destination have the same
62316776Smckusick 		 * parent we don't fool with the link count.
6249167Ssam 		 */
62516776Smckusick 		if (doingdirectory && newparent) {
6269167Ssam 			dp->i_nlink++;
6279167Ssam 			dp->i_flag |= ICHG;
62837737Smckusick 			error = iupdat(dp, &time, &time, 1);
6299167Ssam 		}
63037737Smckusick 		if (error = direnter(ip, tndp))
6319167Ssam 			goto out;
6329167Ssam 	} else {
63337737Smckusick 		if (xp->i_dev != dp->i_dev || xp->i_dev != ip->i_dev)
63437737Smckusick 			panic("rename: EXDEV");
6359167Ssam 		/*
63610590Ssam 		 * Short circuit rename(foo, foo).
63710590Ssam 		 */
63810590Ssam 		if (xp->i_number == ip->i_number)
63937737Smckusick 			panic("rename: same file");
64010590Ssam 		/*
64124433Sbloom 		 * If the parent directory is "sticky", then the user must
64224433Sbloom 		 * own the parent directory, or the destination of the rename,
64324433Sbloom 		 * otherwise the destination may not be changed (except by
64424433Sbloom 		 * root). This implements append-only directories.
64524433Sbloom 		 */
64637737Smckusick 		if ((dp->i_mode & ISVTX) && tndp->ni_cred->cr_uid != 0 &&
64737737Smckusick 		    tndp->ni_cred->cr_uid != dp->i_uid &&
64837737Smckusick 		    xp->i_uid != tndp->ni_cred->cr_uid) {
64924433Sbloom 			error = EPERM;
65024433Sbloom 			goto bad;
65124433Sbloom 		}
65224433Sbloom 		/*
65310051Ssam 		 * Target must be empty if a directory
65410051Ssam 		 * and have no links to it.
6559167Ssam 		 * Also, insure source and target are
6569167Ssam 		 * compatible (both directories, or both
6579167Ssam 		 * not directories).
6589167Ssam 		 */
6599167Ssam 		if ((xp->i_mode&IFMT) == IFDIR) {
66037737Smckusick 			if (!dirempty(xp, dp->i_number, tndp->ni_cred) ||
66137737Smckusick 			    xp->i_nlink > 2) {
66210051Ssam 				error = ENOTEMPTY;
6639167Ssam 				goto bad;
6649167Ssam 			}
6659167Ssam 			if (!doingdirectory) {
66610051Ssam 				error = ENOTDIR;
6679167Ssam 				goto bad;
6689167Ssam 			}
66937737Smckusick 			cache_purge(ITOV(dp));
6709167Ssam 		} else if (doingdirectory) {
67110051Ssam 			error = EISDIR;
6729167Ssam 			goto bad;
6739167Ssam 		}
67437737Smckusick 		if (error = dirrewrite(dp, ip, tndp))
67537737Smckusick 			goto bad;
67637737Smckusick 		vput(ITOV(dp));
6779167Ssam 		/*
67810051Ssam 		 * Adjust the link count of the target to
67910051Ssam 		 * reflect the dirrewrite above.  If this is
68010051Ssam 		 * a directory it is empty and there are
68110051Ssam 		 * no links to it, so we can squash the inode and
68210051Ssam 		 * any space associated with it.  We disallowed
68310051Ssam 		 * renaming over top of a directory with links to
68416776Smckusick 		 * it above, as the remaining link would point to
68516776Smckusick 		 * a directory without "." or ".." entries.
6869167Ssam 		 */
68710051Ssam 		xp->i_nlink--;
6889167Ssam 		if (doingdirectory) {
68910051Ssam 			if (--xp->i_nlink != 0)
69010051Ssam 				panic("rename: linked directory");
69137737Smckusick 			error = itrunc(xp, (u_long)0);
69210051Ssam 		}
6939167Ssam 		xp->i_flag |= ICHG;
69437737Smckusick 		vput(ITOV(xp));
69510246Ssam 		xp = NULL;
6969167Ssam 	}
6979167Ssam 
6989167Ssam 	/*
6999167Ssam 	 * 3) Unlink the source.
7009167Ssam 	 */
70137737Smckusick 	fndp->ni_nameiop = DELETE | LOCKPARENT | LOCKLEAF;
70237737Smckusick 	(void)namei(fndp);
70337737Smckusick 	if (fndp->ni_vp != NULL) {
70437737Smckusick 		xp = VTOI(fndp->ni_vp);
70537737Smckusick 		dp = VTOI(fndp->ni_dvp);
70637737Smckusick 	} else {
707*38069Smckusick 		if (fndp->ni_dvp != NULL)
708*38069Smckusick 			vput(fndp->ni_dvp);
70937737Smckusick 		xp = NULL;
71017758Smckusick 		dp = NULL;
71137737Smckusick 	}
7129167Ssam 	/*
71337737Smckusick 	 * Ensure that the directory entry still exists and has not
71416776Smckusick 	 * changed while the new name has been entered. If the source is
71516776Smckusick 	 * a file then the entry may have been unlinked or renamed. In
71616776Smckusick 	 * either case there is no further work to be done. If the source
71716776Smckusick 	 * is a directory then it cannot have been rmdir'ed; its link
71816776Smckusick 	 * count of three would cause a rmdir to fail with ENOTEMPTY.
71937737Smckusick 	 * The IRENAME flag ensures that it cannot be moved by another
72016776Smckusick 	 * rename.
7219167Ssam 	 */
72217758Smckusick 	if (xp != ip) {
72316776Smckusick 		if (doingdirectory)
72417758Smckusick 			panic("rename: lost dir entry");
72516776Smckusick 	} else {
7269167Ssam 		/*
72716776Smckusick 		 * If the source is a directory with a
72816776Smckusick 		 * new parent, the link count of the old
72916776Smckusick 		 * parent directory must be decremented
73016776Smckusick 		 * and ".." set to point to the new parent.
7319167Ssam 		 */
73216776Smckusick 		if (doingdirectory && newparent) {
7339167Ssam 			dp->i_nlink--;
7349167Ssam 			dp->i_flag |= ICHG;
73516776Smckusick 			error = rdwri(UIO_READ, xp, (caddr_t)&dirbuf,
73637737Smckusick 				sizeof (struct dirtemplate), (off_t)0,
73737740Smckusick 				UIO_SYSSPACE, tndp->ni_cred, (int *)0);
73816776Smckusick 			if (error == 0) {
73916776Smckusick 				if (dirbuf.dotdot_namlen != 2 ||
74016776Smckusick 				    dirbuf.dotdot_name[0] != '.' ||
74116776Smckusick 				    dirbuf.dotdot_name[1] != '.') {
74216776Smckusick 					printf("rename: mangled dir\n");
74316776Smckusick 				} else {
74416776Smckusick 					dirbuf.dotdot_ino = newparent;
74516776Smckusick 					(void) rdwri(UIO_WRITE, xp,
74616776Smckusick 					    (caddr_t)&dirbuf,
74716776Smckusick 					    sizeof (struct dirtemplate),
74837740Smckusick 					    (off_t)0, UIO_SYSSPACE,
74937737Smckusick 					    tndp->ni_cred, (int *)0);
75037737Smckusick 					cache_purge(ITOV(dp));
75116776Smckusick 				}
75216776Smckusick 			}
7539167Ssam 		}
75437737Smckusick 		error = dirremove(fndp);
75537737Smckusick 		if (!error) {
75616776Smckusick 			xp->i_nlink--;
75716776Smckusick 			xp->i_flag |= ICHG;
7589167Ssam 		}
75916776Smckusick 		xp->i_flag &= ~IRENAME;
7609167Ssam 	}
7619167Ssam 	if (dp)
76237737Smckusick 		vput(ITOV(dp));
76316776Smckusick 	if (xp)
76437737Smckusick 		vput(ITOV(xp));
76537737Smckusick 	vrele(ITOV(ip));
76637737Smckusick 	return (error);
7679167Ssam 
7689167Ssam bad:
7699167Ssam 	if (xp)
77037737Smckusick 		vput(ITOV(xp));
77137737Smckusick 	vput(ITOV(dp));
7729167Ssam out:
7739167Ssam 	ip->i_nlink--;
7749167Ssam 	ip->i_flag |= ICHG;
77537737Smckusick 	vrele(ITOV(ip));
77637737Smckusick 	return (error);
7777701Ssam }
7787701Ssam 
7797535Sroot /*
78012756Ssam  * A virgin directory (no blushing please).
78112756Ssam  */
78212756Ssam struct dirtemplate mastertemplate = {
78312756Ssam 	0, 12, 1, ".",
78412756Ssam 	0, DIRBLKSIZ - 12, 2, ".."
78512756Ssam };
78612756Ssam 
78712756Ssam /*
78812756Ssam  * Mkdir system call
78912756Ssam  */
79037737Smckusick ufs_mkdir(ndp, vap)
79137737Smckusick 	struct nameidata *ndp;
79237737Smckusick 	struct vattr *vap;
79312756Ssam {
79412756Ssam 	register struct inode *ip, *dp;
79537737Smckusick 	struct inode *tip;
79637737Smckusick 	struct vnode *dvp;
79712756Ssam 	struct dirtemplate dirtemplate;
79837737Smckusick 	int error;
79937737Smckusick 	int dmode;
80012756Ssam 
80137737Smckusick 	dvp = ndp->ni_dvp;
80237737Smckusick 	dp = VTOI(dvp);
80337737Smckusick 	dmode = vap->va_mode&0777;
80437737Smckusick 	dmode |= IFDIR;
80512756Ssam 	/*
80612756Ssam 	 * Must simulate part of maknode here
80712756Ssam 	 * in order to acquire the inode, but
80812756Ssam 	 * not have it entered in the parent
80912756Ssam 	 * directory.  The entry is made later
81012756Ssam 	 * after writing "." and ".." entries out.
81112756Ssam 	 */
81237737Smckusick 	error = ialloc(dp, dirpref(dp->i_fs), dmode, &tip);
81337737Smckusick 	if (error) {
81412756Ssam 		iput(dp);
81537737Smckusick 		return (error);
81612756Ssam 	}
81737737Smckusick 	ip = tip;
81812756Ssam #ifdef QUOTA
81912756Ssam 	if (ip->i_dquot != NODQUOT)
82012756Ssam 		panic("mkdir: dquot");
82112756Ssam #endif
82212756Ssam 	ip->i_flag |= IACC|IUPD|ICHG;
82337737Smckusick 	ip->i_mode = dmode;
82437737Smckusick 	ITOV(ip)->v_type = VDIR;	/* Rest init'd in iget() */
82512756Ssam 	ip->i_nlink = 2;
82637737Smckusick 	ip->i_uid = ndp->ni_cred->cr_uid;
82712756Ssam 	ip->i_gid = dp->i_gid;
82812756Ssam #ifdef QUOTA
82912756Ssam 	ip->i_dquot = inoquota(ip);
83012756Ssam #endif
83137737Smckusick 	error = iupdat(ip, &time, &time, 1);
83212756Ssam 
83312756Ssam 	/*
83412756Ssam 	 * Bump link count in parent directory
83512756Ssam 	 * to reflect work done below.  Should
83612756Ssam 	 * be done before reference is created
83712756Ssam 	 * so reparation is possible if we crash.
83812756Ssam 	 */
83912756Ssam 	dp->i_nlink++;
84012756Ssam 	dp->i_flag |= ICHG;
84137737Smckusick 	error = iupdat(dp, &time, &time, 1);
84212756Ssam 
84312756Ssam 	/*
84412756Ssam 	 * Initialize directory with "."
84512756Ssam 	 * and ".." from static template.
84612756Ssam 	 */
84712756Ssam 	dirtemplate = mastertemplate;
84812756Ssam 	dirtemplate.dot_ino = ip->i_number;
84912756Ssam 	dirtemplate.dotdot_ino = dp->i_number;
85037737Smckusick 	error = rdwri(UIO_WRITE, ip, (caddr_t)&dirtemplate,
85137737Smckusick 		sizeof (dirtemplate), (off_t)0, UIO_SYSSPACE,
85237737Smckusick 		ndp->ni_cred, (int *)0);
85337737Smckusick 	if (error) {
85412756Ssam 		dp->i_nlink--;
85512756Ssam 		dp->i_flag |= ICHG;
85612756Ssam 		goto bad;
85712756Ssam 	}
85837737Smckusick 	if (DIRBLKSIZ > dp->i_fs->fs_fsize)
85937737Smckusick 		panic("mkdir: blksize");     /* XXX - should grow w/balloc() */
86018103Smckusick 	else
86118103Smckusick 		ip->i_size = DIRBLKSIZ;
86212756Ssam 	/*
86312756Ssam 	 * Directory all set up, now
86412756Ssam 	 * install the entry for it in
86512756Ssam 	 * the parent directory.
86612756Ssam 	 */
86737737Smckusick 	error = direnter(ip, ndp);
86812756Ssam 	dp = NULL;
86937737Smckusick 	if (error) {
87016694Smckusick 		ndp->ni_nameiop = LOOKUP | NOCACHE;
87137737Smckusick 		error = namei(ndp);
87237737Smckusick 		if (!error) {
87337737Smckusick 			dp = VTOI(ndp->ni_vp);
87412756Ssam 			dp->i_nlink--;
87512756Ssam 			dp->i_flag |= ICHG;
87612756Ssam 		}
87712756Ssam 	}
87812756Ssam bad:
87912756Ssam 	/*
88012756Ssam 	 * No need to do an explicit itrunc here,
88137737Smckusick 	 * vrele will do this for us because we set
88212756Ssam 	 * the link count to 0.
88312756Ssam 	 */
88437737Smckusick 	if (error) {
88512756Ssam 		ip->i_nlink = 0;
88612756Ssam 		ip->i_flag |= ICHG;
88712756Ssam 	}
88837737Smckusick 	iput(ip);
88912756Ssam 	if (dp)
89012756Ssam 		iput(dp);
89137737Smckusick 	return (error);
89212756Ssam }
89312756Ssam 
89412756Ssam /*
89512756Ssam  * Rmdir system call.
89612756Ssam  */
89737737Smckusick ufs_rmdir(ndp)
89837737Smckusick 	register struct nameidata *ndp;
89912756Ssam {
90012756Ssam 	register struct inode *ip, *dp;
90137737Smckusick 	int error = 0;
90212756Ssam 
90337737Smckusick 	ip = VTOI(ndp->ni_vp);
90437737Smckusick 	dp = VTOI(ndp->ni_dvp);
90512756Ssam 	/*
90612756Ssam 	 * No rmdir "." please.
90712756Ssam 	 */
90812756Ssam 	if (dp == ip) {
90937737Smckusick 		vrele(ITOV(dp));
91012756Ssam 		iput(ip);
91137737Smckusick 		return (EINVAL);
91212756Ssam 	}
91312756Ssam 	/*
91412756Ssam 	 * Verify the directory is empty (and valid).
91512756Ssam 	 * (Rmdir ".." won't be valid since
91612756Ssam 	 *  ".." will contain a reference to
91712756Ssam 	 *  the current directory and thus be
91812756Ssam 	 *  non-empty.)
91912756Ssam 	 */
92037737Smckusick 	if (ip->i_nlink != 2 || !dirempty(ip, dp->i_number, ndp->ni_cred)) {
92137737Smckusick 		error = ENOTEMPTY;
92212756Ssam 		goto out;
92312756Ssam 	}
92412756Ssam 	/*
92512756Ssam 	 * Delete reference to directory before purging
92612756Ssam 	 * inode.  If we crash in between, the directory
92712756Ssam 	 * will be reattached to lost+found,
92812756Ssam 	 */
92937737Smckusick 	if (error = dirremove(ndp))
93012756Ssam 		goto out;
93112756Ssam 	dp->i_nlink--;
93212756Ssam 	dp->i_flag |= ICHG;
93337737Smckusick 	cache_purge(ITOV(dp));
93412756Ssam 	iput(dp);
93537737Smckusick 	ndp->ni_dvp = NULL;
93612756Ssam 	/*
93712756Ssam 	 * Truncate inode.  The only stuff left
93812756Ssam 	 * in the directory is "." and "..".  The
93912756Ssam 	 * "." reference is inconsequential since
94012756Ssam 	 * we're quashing it.  The ".." reference
94112756Ssam 	 * has already been adjusted above.  We've
94212756Ssam 	 * removed the "." reference and the reference
94312756Ssam 	 * in the parent directory, but there may be
94412756Ssam 	 * other hard links so decrement by 2 and
94512756Ssam 	 * worry about them later.
94612756Ssam 	 */
94712756Ssam 	ip->i_nlink -= 2;
94837737Smckusick 	error = itrunc(ip, (u_long)0);
94937737Smckusick 	cache_purge(ITOV(ip));
95012756Ssam out:
95137737Smckusick 	if (ndp->ni_dvp)
95212756Ssam 		iput(dp);
95312756Ssam 	iput(ip);
95437737Smckusick 	return (error);
95512756Ssam }
95612756Ssam 
95737737Smckusick /*
95837737Smckusick  * symlink -- make a symbolic link
95937737Smckusick  */
96037737Smckusick ufs_symlink(ndp, vap, target)
96137737Smckusick 	struct nameidata *ndp;
96237737Smckusick 	struct vattr *vap;
96337737Smckusick 	char *target;
96412756Ssam {
96537737Smckusick 	struct inode *ip;
96637737Smckusick 	int error;
96712756Ssam 
96837737Smckusick 	error = maknode(IFLNK | vap->va_mode, ndp, &ip);
96937737Smckusick 	if (error)
97037737Smckusick 		return (error);
97137737Smckusick 	error = rdwri(UIO_WRITE, ip, target, strlen(target), (off_t)0,
97237737Smckusick 		UIO_SYSSPACE, ndp->ni_cred, (int *)0);
97337737Smckusick 	iput(ip);
97437737Smckusick 	return (error);
97537737Smckusick }
97637737Smckusick 
97737737Smckusick /*
97837737Smckusick  * Vnode op for read and write
97937737Smckusick  */
98037737Smckusick ufs_readdir(vp, uio, offp, cred)
98137737Smckusick 	struct vnode *vp;
98237737Smckusick 	register struct uio *uio;
98337737Smckusick 	off_t *offp;
98437737Smckusick 	struct ucred *cred;
98537737Smckusick {
98637737Smckusick 	register struct inode *ip = VTOI(vp);
98737737Smckusick 	int count, error;
98837737Smckusick 
98937737Smckusick 	ILOCK(ip);
99037737Smckusick 	uio->uio_offset = *offp;
99137737Smckusick 	count = uio->uio_resid;
99237737Smckusick 	count &= ~(DIRBLKSIZ - 1);
99337737Smckusick 	if (vp->v_type != VDIR || uio->uio_iovcnt != 1 ||
99437737Smckusick 	    (count < DIRBLKSIZ) || (uio->uio_offset & (DIRBLKSIZ -1))) {
99537737Smckusick 		IUNLOCK(ip);
99637737Smckusick 		return (EINVAL);
99716540Ssam 	}
99837737Smckusick 	uio->uio_resid = count;
99937737Smckusick 	uio->uio_iov->iov_len = count;
100037737Smckusick 	error = readip(ip, uio, cred);
100137737Smckusick 	*offp += count - uio->uio_resid;
100237737Smckusick 	IUNLOCK(ip);
100337737Smckusick 	return (error);
100437737Smckusick }
100537737Smckusick 
100637737Smckusick /*
100737737Smckusick  * Return target name of a symbolic link
100837737Smckusick  */
100937737Smckusick ufs_readlink(vp, uiop, cred)
101037737Smckusick 	struct vnode *vp;
101137737Smckusick 	struct uio *uiop;
101237737Smckusick 	struct ucred *cred;
101337737Smckusick {
101437737Smckusick 
101537737Smckusick 	return (readip(VTOI(vp), uiop, cred));
101637737Smckusick }
101737737Smckusick 
101837737Smckusick /*
101937737Smckusick  * Ufs abort op, called after namei() when a CREATE/DELETE isn't actually
102037737Smckusick  * done. Iff ni_vp/ni_dvp not null and locked, unlock.
102137737Smckusick  */
102237737Smckusick ufs_abortop(ndp)
102337737Smckusick 	register struct nameidata *ndp;
102437737Smckusick {
102537737Smckusick 	register struct inode *ip;
102637737Smckusick 
102737737Smckusick 	if (ndp->ni_vp) {
102837737Smckusick 		ip = VTOI(ndp->ni_vp);
102937737Smckusick 		if (ip->i_flag & ILOCKED)
103037737Smckusick 			IUNLOCK(ip);
103137737Smckusick 		vrele(ndp->ni_vp);
103212756Ssam 	}
103337737Smckusick 	if (ndp->ni_dvp) {
103437737Smckusick 		ip = VTOI(ndp->ni_dvp);
103537737Smckusick 		if (ip->i_flag & ILOCKED)
103637737Smckusick 			IUNLOCK(ip);
103737737Smckusick 		vrele(ndp->ni_dvp);
103837737Smckusick 	}
103937737Smckusick 	return;
104012756Ssam }
104112756Ssam 
104237737Smckusick ufs_lock(vp)
104337737Smckusick 	struct vnode *vp;
104437737Smckusick {
104537737Smckusick 	register struct inode *ip = VTOI(vp);
104637737Smckusick 
104737737Smckusick 	ILOCK(ip);
104837737Smckusick 	return (0);
104937737Smckusick }
105037737Smckusick 
105137737Smckusick ufs_unlock(vp)
105237737Smckusick 	struct vnode *vp;
105337737Smckusick {
105437737Smckusick 	register struct inode *ip = VTOI(vp);
105537737Smckusick 
105637737Smckusick 	if (!(ip->i_flag & ILOCKED))
105737737Smckusick 		panic("ufs_unlock NOT LOCKED");
105837737Smckusick 	IUNLOCK(ip);
105937737Smckusick 	return (0);
106037737Smckusick }
106137737Smckusick 
106212756Ssam /*
106337737Smckusick  * Get access to bmap
106412756Ssam  */
106537737Smckusick ufs_bmap(vp, bn, vpp, bnp)
106637737Smckusick 	struct vnode *vp;
106737737Smckusick 	daddr_t bn;
106837737Smckusick 	struct vnode **vpp;
106937737Smckusick 	daddr_t *bnp;
107012756Ssam {
107137737Smckusick 	struct inode *ip = VTOI(vp);
107212756Ssam 
107337737Smckusick 	if (vpp != NULL)
107437737Smckusick 		*vpp = ip->i_devvp;
107537737Smckusick 	if (bnp == NULL)
107637737Smckusick 		return (0);
107737737Smckusick 	return (bmap(ip, bn, bnp, (daddr_t *)0, (int *)0));
107812756Ssam }
107937737Smckusick 
108037737Smckusick /*
108137737Smckusick  * Just call the device strategy routine
108237737Smckusick  */
108337737Smckusick ufs_strategy(bp)
108437737Smckusick 	register struct buf *bp;
108537737Smckusick {
108637737Smckusick 	(*bdevsw[major(bp->b_dev)].d_strategy)(bp);
108737737Smckusick 	return (0);
108837737Smckusick }
108937737Smckusick 
109037737Smckusick /*
109137737Smckusick  * Make a new file.
109237737Smckusick  */
109337737Smckusick maknode(mode, ndp, ipp)
109437737Smckusick 	int mode;
109537737Smckusick 	register struct nameidata *ndp;
109637737Smckusick 	struct inode **ipp;
109737737Smckusick {
109837737Smckusick 	register struct inode *ip;
109937737Smckusick 	struct inode *tip;
110037737Smckusick 	register struct inode *pdir = VTOI(ndp->ni_dvp);
110137737Smckusick 	ino_t ipref;
110237737Smckusick 	int error;
110337737Smckusick 
110437737Smckusick 	*ipp = 0;
110537737Smckusick 	if ((mode & IFMT) == IFDIR)
110637737Smckusick 		ipref = dirpref(pdir->i_fs);
110737737Smckusick 	else
110837737Smckusick 		ipref = pdir->i_number;
110937737Smckusick 	error = ialloc(pdir, ipref, mode, &tip);
111037737Smckusick 	if (error) {
111137737Smckusick 		iput(pdir);
111237737Smckusick 		return (error);
111337737Smckusick 	}
111437737Smckusick 	ip = tip;
111537737Smckusick #ifdef QUOTA
111637737Smckusick 	if (ip->i_dquot != NODQUOT)
111737737Smckusick 		panic("maknode: dquot");
111837737Smckusick #endif
111937737Smckusick 	ip->i_flag |= IACC|IUPD|ICHG;
112037737Smckusick 	if ((mode & IFMT) == 0)
112137737Smckusick 		mode |= IFREG;
112237737Smckusick 	ip->i_mode = mode;
112337737Smckusick 	ITOV(ip)->v_type = IFTOVT(mode);	/* Rest init'd in iget() */
112437737Smckusick 	ip->i_nlink = 1;
112537737Smckusick 	ip->i_uid = ndp->ni_cred->cr_uid;
112637737Smckusick 	ip->i_gid = pdir->i_gid;
112737737Smckusick 	if ((ip->i_mode & ISGID) && !groupmember(ip->i_gid, ndp->ni_cred) &&
112837737Smckusick 	    suser(ndp->ni_cred, NULL))
112937737Smckusick 		ip->i_mode &= ~ISGID;
113037737Smckusick #ifdef QUOTA
113137737Smckusick 	ip->i_dquot = inoquota(ip);
113237737Smckusick #endif
113337737Smckusick 
113437737Smckusick 	/*
113537737Smckusick 	 * Make sure inode goes to disk before directory entry.
113637737Smckusick 	 */
113737737Smckusick 	if ((error = iupdat(ip, &time, &time, 1)) ||
113837737Smckusick 	    (error = direnter(ip, ndp))) {
113937737Smckusick 		/*
114037737Smckusick 		 * Write error occurred trying to update the inode
114137737Smckusick 		 * or the directory so must deallocate the inode.
114237737Smckusick 		 */
114337737Smckusick 		ip->i_nlink = 0;
114437737Smckusick 		ip->i_flag |= ICHG;
114537737Smckusick 		iput(ip);
114637737Smckusick 		return (error);
114737737Smckusick 	}
114837737Smckusick 	*ipp = ip;
114937737Smckusick 	return (0);
115037737Smckusick }
1151