xref: /csrg-svn/sys/ufs/lfs/lfs_vnops.c (revision 39391)
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*39391Smckusick  *	@(#)lfs_vnops.c	7.19 (Berkeley) 10/24/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(),
67*39391Smckusick 	ufs_reclaim(),
6837737Smckusick 	ufs_lock(),
6937737Smckusick 	ufs_unlock(),
7037737Smckusick 	ufs_bmap(),
7137737Smckusick 	ufs_strategy();
726254Sroot 
7337737Smckusick struct vnodeops ufs_vnodeops = {
7437737Smckusick 	ufs_lookup,
7537737Smckusick 	ufs_create,
7637737Smckusick 	ufs_mknod,
7737737Smckusick 	ufs_open,
7837737Smckusick 	ufs_close,
7937737Smckusick 	ufs_access,
8037737Smckusick 	ufs_getattr,
8137737Smckusick 	ufs_setattr,
8237737Smckusick 	ufs_read,
8337737Smckusick 	ufs_write,
8437737Smckusick 	ufs_ioctl,
8537737Smckusick 	ufs_select,
8637737Smckusick 	ufs_mmap,
8737737Smckusick 	ufs_fsync,
8837737Smckusick 	ufs_seek,
8937737Smckusick 	ufs_remove,
9037737Smckusick 	ufs_link,
9137737Smckusick 	ufs_rename,
9237737Smckusick 	ufs_mkdir,
9337737Smckusick 	ufs_rmdir,
9437737Smckusick 	ufs_symlink,
9537737Smckusick 	ufs_readdir,
9637737Smckusick 	ufs_readlink,
9737737Smckusick 	ufs_abortop,
9837737Smckusick 	ufs_inactive,
99*39391Smckusick 	ufs_reclaim,
10037737Smckusick 	ufs_lock,
10137737Smckusick 	ufs_unlock,
10237737Smckusick 	ufs_bmap,
10337737Smckusick 	ufs_strategy,
10437737Smckusick };
1056254Sroot 
10637737Smckusick enum vtype iftovt_tab[8] = {
10737737Smckusick 	VNON, VCHR, VDIR, VBLK, VREG, VLNK, VSOCK, VBAD,
10837737Smckusick };
10937737Smckusick int	vttoif_tab[8] = {
11037737Smckusick 	0, IFREG, IFDIR, IFBLK, IFCHR, IFLNK, IFSOCK, IFMT,
11137737Smckusick };
1126254Sroot 
1139167Ssam /*
11437737Smckusick  * Create a regular file
1159167Ssam  */
11637737Smckusick ufs_create(ndp, vap)
11737737Smckusick 	struct nameidata *ndp;
11837737Smckusick 	struct vattr *vap;
1196254Sroot {
12037737Smckusick 	struct inode *ip;
12137737Smckusick 	int error;
1226254Sroot 
12337737Smckusick 	if (error = maknode(MAKEIMODE(vap->va_type, vap->va_mode), ndp, &ip))
12437737Smckusick 		return (error);
12537737Smckusick 	ndp->ni_vp = ITOV(ip);
12637737Smckusick 	return (0);
1276254Sroot }
1286254Sroot 
12937Sbill /*
13037737Smckusick  * Mknod vnode call
1316254Sroot  */
13237737Smckusick /* ARGSUSED */
13337737Smckusick ufs_mknod(ndp, vap, cred)
13437737Smckusick 	struct nameidata *ndp;
13537737Smckusick 	struct ucred *cred;
13637737Smckusick 	struct vattr *vap;
1376254Sroot {
13837737Smckusick 	struct inode *ip;
13937737Smckusick 	int error;
1406254Sroot 
14137737Smckusick 	if (error = maknode(MAKEIMODE(vap->va_type, vap->va_mode), ndp, &ip))
14237737Smckusick 		return (error);
14337737Smckusick 	if (vap->va_rdev) {
14437737Smckusick 		/*
14537737Smckusick 		 * Want to be able to use this to make badblock
14637737Smckusick 		 * inodes, so don't truncate the dev number.
14737737Smckusick 		 */
14837737Smckusick 		ITOV(ip)->v_rdev = ip->i_rdev = vap->va_rdev;
14937737Smckusick 		ip->i_flag |= IACC|IUPD|ICHG;
15012756Ssam 	}
15137737Smckusick 	/*
15237737Smckusick 	 * Remove inode so that it will be reloaded by iget and
15337737Smckusick 	 * checked to see if it is an alias of an existing entry
15437737Smckusick 	 * in the inode cache.
15537737Smckusick 	 */
15637737Smckusick 	remque(ip);
15737737Smckusick 	ip->i_forw = ip;
15837737Smckusick 	ip->i_back = ip;
15938809Smckusick 	ITOV(ip)->v_type = VNON;
16038809Smckusick 	iput(ip);
16137737Smckusick 	return (0);
1626254Sroot }
1636254Sroot 
1646254Sroot /*
16537737Smckusick  * Open called.
16637737Smckusick  *
16737737Smckusick  * Nothing to do.
1686254Sroot  */
16937737Smckusick /* ARGSUSED */
17037737Smckusick ufs_open(vp, mode, cred)
17137737Smckusick 	struct vnode *vp;
17237737Smckusick 	int mode;
17337737Smckusick 	struct ucred *cred;
1746254Sroot {
1756254Sroot 
17637737Smckusick 	return (0);
1776254Sroot }
1786254Sroot 
1796254Sroot /*
18037737Smckusick  * Close called
18137737Smckusick  *
18237737Smckusick  * Update the times on the inode.
1836254Sroot  */
18437737Smckusick /* ARGSUSED */
18537737Smckusick ufs_close(vp, fflag, cred)
18637737Smckusick 	struct vnode *vp;
18737737Smckusick 	int fflag;
18837737Smckusick 	struct ucred *cred;
1896254Sroot {
19037737Smckusick 	register struct inode *ip = VTOI(vp);
1916254Sroot 
19237737Smckusick 	if (vp->v_count > 1 && !(ip->i_flag & ILOCKED))
19337737Smckusick 		ITIMES(ip, &time, &time);
19437737Smckusick 	return (0);
1956254Sroot }
1966254Sroot 
19737737Smckusick ufs_access(vp, mode, cred)
19837737Smckusick 	struct vnode *vp;
19937737Smckusick 	int mode;
20037737Smckusick 	struct ucred *cred;
2016254Sroot {
2026254Sroot 
20337737Smckusick 	return (iaccess(VTOI(vp), mode, cred));
2046254Sroot }
2056254Sroot 
20637737Smckusick /* ARGSUSED */
20737737Smckusick ufs_getattr(vp, vap, cred)
20837737Smckusick 	struct vnode *vp;
20937737Smckusick 	register struct vattr *vap;
21037737Smckusick 	struct ucred *cred;
2116254Sroot {
21237737Smckusick 	register struct inode *ip = VTOI(vp);
2136254Sroot 
21437737Smckusick 	ITIMES(ip, &time, &time);
2156254Sroot 	/*
21637737Smckusick 	 * Copy from inode table
2176254Sroot 	 */
21837737Smckusick 	vap->va_fsid = ip->i_dev;
21937737Smckusick 	vap->va_fileid = ip->i_number;
22037737Smckusick 	vap->va_mode = ip->i_mode & ~IFMT;
22137737Smckusick 	vap->va_nlink = ip->i_nlink;
22237737Smckusick 	vap->va_uid = ip->i_uid;
22337737Smckusick 	vap->va_gid = ip->i_gid;
22437737Smckusick 	vap->va_rdev = (dev_t)ip->i_rdev;
225*39391Smckusick 	vap->va_size = ip->i_din.di_qsize.val[0];
226*39391Smckusick 	vap->va_size1 = ip->i_din.di_qsize.val[1];
22737737Smckusick 	vap->va_atime.tv_sec = ip->i_atime;
22838578Smckusick 	vap->va_atime.tv_usec = 0;
22937737Smckusick 	vap->va_mtime.tv_sec = ip->i_mtime;
23038578Smckusick 	vap->va_mtime.tv_usec = 0;
23137737Smckusick 	vap->va_ctime.tv_sec = ip->i_ctime;
23238578Smckusick 	vap->va_ctime.tv_usec = 0;
23338254Smckusick 	vap->va_flags = ip->i_flags;
23438254Smckusick 	vap->va_gen = ip->i_gen;
23537737Smckusick 	/* this doesn't belong here */
23637737Smckusick 	if (vp->v_type == VBLK)
23737737Smckusick 		vap->va_blocksize = BLKDEV_IOSIZE;
23837737Smckusick 	else if (vp->v_type == VCHR)
23937737Smckusick 		vap->va_blocksize = MAXBSIZE;
2407142Smckusick 	else
24137737Smckusick 		vap->va_blocksize = ip->i_fs->fs_bsize;
24238657Smckusick 	vap->va_bytes = dbtob(ip->i_blocks);
24337737Smckusick 	vap->va_bytes1 = -1;
24437737Smckusick 	vap->va_type = vp->v_type;
24537737Smckusick 	return (0);
2466254Sroot }
2476254Sroot 
2486254Sroot /*
24937737Smckusick  * Set attribute vnode op. called from several syscalls
2506254Sroot  */
25137737Smckusick ufs_setattr(vp, vap, cred)
25237737Smckusick 	register struct vnode *vp;
25337737Smckusick 	register struct vattr *vap;
25437737Smckusick 	register struct ucred *cred;
2556254Sroot {
25637737Smckusick 	register struct inode *ip = VTOI(vp);
25737737Smckusick 	int error = 0;
2586254Sroot 
25937737Smckusick 	/*
26037737Smckusick 	 * Check for unsetable attributes.
26137737Smckusick 	 */
26237737Smckusick 	if ((vap->va_type != VNON) || (vap->va_nlink != VNOVAL) ||
26337737Smckusick 	    (vap->va_fsid != VNOVAL) || (vap->va_fileid != VNOVAL) ||
26437737Smckusick 	    (vap->va_blocksize != VNOVAL) || (vap->va_rdev != VNOVAL) ||
26538254Smckusick 	    ((int)vap->va_bytes != VNOVAL) || (vap->va_gen != VNOVAL)) {
26637737Smckusick 		return (EINVAL);
26716540Ssam 	}
26837737Smckusick 	/*
26937737Smckusick 	 * Go through the fields and update iff not VNOVAL.
27037737Smckusick 	 */
27137737Smckusick 	if (vap->va_uid != (u_short)VNOVAL || vap->va_gid != (u_short)VNOVAL)
27237737Smckusick 		if (error = chown1(vp, vap->va_uid, vap->va_gid, cred))
27337737Smckusick 			return (error);
27437737Smckusick 	if (vap->va_size != VNOVAL) {
27537737Smckusick 		if (vp->v_type == VDIR)
27637737Smckusick 			return (EISDIR);
27737737Smckusick 		if (error = itrunc(ip, vap->va_size))
27837737Smckusick 			return (error);
27913878Ssam 	}
28037737Smckusick 	if (vap->va_atime.tv_sec != VNOVAL || vap->va_mtime.tv_sec != VNOVAL) {
28137773Smckusick 		if (cred->cr_uid != ip->i_uid &&
28237773Smckusick 		    (error = suser(cred, &u.u_acflag)))
28337773Smckusick 			return (error);
28437737Smckusick 		if (vap->va_atime.tv_sec != VNOVAL)
28537737Smckusick 			ip->i_flag |= IACC;
28637737Smckusick 		if (vap->va_mtime.tv_sec != VNOVAL)
28737737Smckusick 			ip->i_flag |= IUPD;
28837737Smckusick 		ip->i_flag |= ICHG;
28937737Smckusick 		if (error = iupdat(ip, &vap->va_atime, &vap->va_mtime, 1))
29037737Smckusick 			return (error);
2916254Sroot 	}
29237737Smckusick 	if (vap->va_mode != (u_short)VNOVAL)
29337737Smckusick 		error = chmod1(vp, (int)vap->va_mode, cred);
29438254Smckusick 	if (vap->va_flags != VNOVAL) {
29538254Smckusick 		if (cred->cr_uid != ip->i_uid &&
29638254Smckusick 		    (error = suser(cred, &u.u_acflag)))
29738254Smckusick 			return (error);
29838254Smckusick 		if (cred->cr_uid == 0) {
29938254Smckusick 			ip->i_flags = vap->va_flags;
30038254Smckusick 		} else {
30138254Smckusick 			ip->i_flags &= 0xffff0000;
30238254Smckusick 			ip->i_flags |= (vap->va_flags & 0xffff);
30338254Smckusick 		}
30438254Smckusick 		ip->i_flag |= ICHG;
30538254Smckusick 	}
30637737Smckusick 	return (error);
3076254Sroot }
3086254Sroot 
3096254Sroot /*
3109167Ssam  * Change the mode on a file.
3119167Ssam  * Inode must be locked before calling.
3129167Ssam  */
31337737Smckusick chmod1(vp, mode, cred)
31437737Smckusick 	register struct vnode *vp;
3157701Ssam 	register int mode;
31637737Smckusick 	struct ucred *cred;
3177701Ssam {
31837737Smckusick 	register struct inode *ip = VTOI(vp);
31937773Smckusick 	int error;
3207868Sroot 
32137773Smckusick 	if (cred->cr_uid != ip->i_uid &&
32237773Smckusick 	    (error = suser(cred, &u.u_acflag)))
32337773Smckusick 		return (error);
3246254Sroot 	ip->i_mode &= ~07777;
32537737Smckusick 	if (cred->cr_uid) {
32637737Smckusick 		if (vp->v_type != VDIR)
32721015Smckusick 			mode &= ~ISVTX;
32837737Smckusick 		if (!groupmember(ip->i_gid, cred))
32911811Ssam 			mode &= ~ISGID;
3307439Sroot 	}
33137737Smckusick 	ip->i_mode |= mode & 07777;
3326254Sroot 	ip->i_flag |= ICHG;
33337737Smckusick 	if ((vp->v_flag & VTEXT) && (ip->i_mode & ISVTX) == 0)
33437737Smckusick 		xrele(vp);
33521015Smckusick 	return (0);
3365992Swnj }
3375992Swnj 
3389167Ssam /*
3397701Ssam  * Perform chown operation on inode ip;
3407701Ssam  * inode must be locked prior to call.
3417701Ssam  */
34237737Smckusick chown1(vp, uid, gid, cred)
34337737Smckusick 	register struct vnode *vp;
34437737Smckusick 	uid_t uid;
34537737Smckusick 	gid_t gid;
34637737Smckusick 	struct ucred *cred;
3477701Ssam {
34837737Smckusick 	register struct inode *ip = VTOI(vp);
3497701Ssam #ifdef QUOTA
3507701Ssam 	register long change;
35111811Ssam #endif
35237737Smckusick 	int error;
3537701Ssam 
35437737Smckusick 	if (uid == (u_short)VNOVAL)
35511811Ssam 		uid = ip->i_uid;
35637737Smckusick 	if (gid == (u_short)VNOVAL)
35711811Ssam 		gid = ip->i_gid;
35836614Sbostic 	/*
35936614Sbostic 	 * If we don't own the file, are trying to change the owner
36036614Sbostic 	 * of the file, or are not a member of the target group,
36136614Sbostic 	 * the caller must be superuser or the call fails.
36236614Sbostic 	 */
36337737Smckusick 	if ((cred->cr_uid != ip->i_uid || uid != ip->i_uid ||
36437737Smckusick 	    !groupmember((gid_t)gid, cred)) &&
36537737Smckusick 	    (error = suser(cred, &u.u_acflag)))
36637737Smckusick 		return (error);
36711811Ssam #ifdef QUOTA
36814385Ssam 	if (ip->i_uid == uid)		/* this just speeds things a little */
3697482Skre 		change = 0;
37012646Ssam 	else
37112646Ssam 		change = ip->i_blocks;
37212646Ssam 	(void) chkdq(ip, -change, 1);
37312646Ssam 	(void) chkiq(ip->i_dev, ip, ip->i_uid, 1);
3747482Skre 	dqrele(ip->i_dquot);
3757482Skre #endif
37639306Smckusick 	if (ip->i_uid != uid && cred->cr_uid != 0)
37739306Smckusick 		ip->i_mode &= ~ISUID;
37839306Smckusick 	if (ip->i_gid != gid && cred->cr_uid != 0)
37939306Smckusick 		ip->i_mode &= ~ISGID;
38011811Ssam 	ip->i_uid = uid;
38111811Ssam 	ip->i_gid = gid;
3826254Sroot 	ip->i_flag |= ICHG;
3837701Ssam #ifdef QUOTA
3847482Skre 	ip->i_dquot = inoquota(ip);
38512646Ssam 	(void) chkdq(ip, change, 1);
38626361Skarels 	(void) chkiq(ip->i_dev, (struct inode *)NULL, (uid_t)uid, 1);
38712646Ssam 	return (u.u_error);		/* should == 0 ALWAYS !! */
38812646Ssam #else
38912646Ssam 	return (0);
3907482Skre #endif
39137Sbill }
39237Sbill 
39337737Smckusick /* ARGSUSED */
39437737Smckusick ufs_ioctl(vp, com, data, fflag, cred)
39537737Smckusick 	struct vnode *vp;
39637737Smckusick 	int com;
39737737Smckusick 	caddr_t data;
39837737Smckusick 	int fflag;
39937737Smckusick 	struct ucred *cred;
40011811Ssam {
40111811Ssam 
40237737Smckusick 	printf("ufs_ioctl called with type %d\n", vp->v_type);
40337737Smckusick 	return (ENOTTY);
40411811Ssam }
40511811Ssam 
40637737Smckusick /* ARGSUSED */
40737737Smckusick ufs_select(vp, which, cred)
40837737Smckusick 	struct vnode *vp;
40937737Smckusick 	int which;
41037737Smckusick 	struct ucred *cred;
41137737Smckusick {
41237737Smckusick 
41337737Smckusick 	printf("ufs_select called with type %d\n", vp->v_type);
41437737Smckusick 	return (1);		/* XXX */
41537737Smckusick }
41637737Smckusick 
4179167Ssam /*
41837737Smckusick  * Mmap a file
41937737Smckusick  *
42037737Smckusick  * NB Currently unsupported.
4219167Ssam  */
42237737Smckusick /* ARGSUSED */
42337737Smckusick ufs_mmap(vp, fflags, cred)
42437737Smckusick 	struct vnode *vp;
42537737Smckusick 	int fflags;
42637737Smckusick 	struct ucred *cred;
42737Sbill {
42837Sbill 
42937737Smckusick 	return (EINVAL);
43037Sbill }
4317535Sroot 
4329167Ssam /*
43337737Smckusick  * Synch an open file.
4349167Ssam  */
43537737Smckusick /* ARGSUSED */
43637737Smckusick ufs_fsync(vp, fflags, cred)
43737737Smckusick 	struct vnode *vp;
43837737Smckusick 	int fflags;
43937737Smckusick 	struct ucred *cred;
4407701Ssam {
44137737Smckusick 	register struct inode *ip = VTOI(vp);
44237737Smckusick 	int error;
4437701Ssam 
44437737Smckusick 	ILOCK(ip);
44537737Smckusick 	if (fflags&FWRITE)
44637737Smckusick 		ip->i_flag |= ICHG;
44737737Smckusick 	error = syncip(ip);
44837737Smckusick 	IUNLOCK(ip);
44937737Smckusick 	return (error);
4507701Ssam }
4517701Ssam 
4529167Ssam /*
45337737Smckusick  * Seek on a file
45437737Smckusick  *
45537737Smckusick  * Nothing to do, so just return.
4569167Ssam  */
45737737Smckusick /* ARGSUSED */
45837737Smckusick ufs_seek(vp, oldoff, newoff, cred)
45937737Smckusick 	struct vnode *vp;
46037737Smckusick 	off_t oldoff, newoff;
46137737Smckusick 	struct ucred *cred;
4627701Ssam {
4637701Ssam 
46437737Smckusick 	return (0);
46537737Smckusick }
46637737Smckusick 
46737737Smckusick /*
46837737Smckusick  * ufs remove
46937737Smckusick  * Hard to avoid races here, especially
47037737Smckusick  * in unlinking directories.
47137737Smckusick  */
47237737Smckusick ufs_remove(ndp)
47337737Smckusick 	struct nameidata *ndp;
47437737Smckusick {
47537737Smckusick 	register struct inode *ip, *dp;
47637737Smckusick 	int error;
47737737Smckusick 
47837737Smckusick 	ip = VTOI(ndp->ni_vp);
47937737Smckusick 	dp = VTOI(ndp->ni_dvp);
48037737Smckusick 	error = dirremove(ndp);
48137737Smckusick 	if (!error) {
48237737Smckusick 		ip->i_nlink--;
48337737Smckusick 		ip->i_flag |= ICHG;
4847701Ssam 	}
48537737Smckusick 	if (dp == ip)
48637737Smckusick 		vrele(ITOV(ip));
48737737Smckusick 	else
48837737Smckusick 		iput(ip);
48937737Smckusick 	iput(dp);
49037737Smckusick 	return (error);
4917701Ssam }
4927701Ssam 
4939167Ssam /*
49437737Smckusick  * link vnode call
4959167Ssam  */
49637737Smckusick ufs_link(vp, ndp)
49737737Smckusick 	register struct vnode *vp;
49837737Smckusick 	register struct nameidata *ndp;
4999167Ssam {
50037737Smckusick 	register struct inode *ip = VTOI(vp);
50137737Smckusick 	int error;
5029167Ssam 
50337737Smckusick 	if (ndp->ni_dvp != vp)
50437737Smckusick 		ILOCK(ip);
50537737Smckusick 	if (ip->i_nlink == LINK_MAX - 1) {
50637737Smckusick 		error = EMLINK;
50737737Smckusick 		goto out;
50837737Smckusick 	}
50937737Smckusick 	ip->i_nlink++;
51037737Smckusick 	ip->i_flag |= ICHG;
51137737Smckusick 	error = iupdat(ip, &time, &time, 1);
51237737Smckusick 	if (!error)
51337737Smckusick 		error = direnter(ip, ndp);
51437737Smckusick out:
51537737Smckusick 	if (ndp->ni_dvp != vp)
51637737Smckusick 		IUNLOCK(ip);
51737737Smckusick 	if (error) {
51837737Smckusick 		ip->i_nlink--;
51930598Smckusick 		ip->i_flag |= ICHG;
52037737Smckusick 	}
52137737Smckusick 	return (error);
5229167Ssam }
5239167Ssam 
5249167Ssam /*
5259167Ssam  * Rename system call.
5269167Ssam  * 	rename("foo", "bar");
5279167Ssam  * is essentially
5289167Ssam  *	unlink("bar");
5299167Ssam  *	link("foo", "bar");
5309167Ssam  *	unlink("foo");
5319167Ssam  * but ``atomically''.  Can't do full commit without saving state in the
5329167Ssam  * inode on disk which isn't feasible at this time.  Best we can do is
5339167Ssam  * always guarantee the target exists.
5349167Ssam  *
5359167Ssam  * Basic algorithm is:
5369167Ssam  *
5379167Ssam  * 1) Bump link count on source while we're linking it to the
53837737Smckusick  *    target.  This also ensure the inode won't be deleted out
53916776Smckusick  *    from underneath us while we work (it may be truncated by
54016776Smckusick  *    a concurrent `trunc' or `open' for creation).
5419167Ssam  * 2) Link source to destination.  If destination already exists,
5429167Ssam  *    delete it first.
54316776Smckusick  * 3) Unlink source reference to inode if still around. If a
54416776Smckusick  *    directory was moved and the parent of the destination
5459167Ssam  *    is different from the source, patch the ".." entry in the
5469167Ssam  *    directory.
5479167Ssam  */
54837737Smckusick ufs_rename(fndp, tndp)
54937737Smckusick 	register struct nameidata *fndp, *tndp;
5507701Ssam {
5519167Ssam 	register struct inode *ip, *xp, *dp;
55216776Smckusick 	struct dirtemplate dirbuf;
55316776Smckusick 	int doingdirectory = 0, oldparent = 0, newparent = 0;
55410051Ssam 	int error = 0;
5557701Ssam 
55637737Smckusick 	dp = VTOI(fndp->ni_dvp);
55737737Smckusick 	ip = VTOI(fndp->ni_vp);
55837737Smckusick 	ILOCK(ip);
5599167Ssam 	if ((ip->i_mode&IFMT) == IFDIR) {
56037737Smckusick 		register struct direct *d = &fndp->ni_dent;
5619167Ssam 
5629167Ssam 		/*
56311641Ssam 		 * Avoid ".", "..", and aliases of "." for obvious reasons.
5649167Ssam 		 */
56537737Smckusick 		if ((d->d_namlen == 1 && d->d_name[0] == '.') || dp == ip ||
56637737Smckusick 		    fndp->ni_isdotdot || (ip->i_flag & IRENAME)) {
56737737Smckusick 			IUNLOCK(ip);
56837737Smckusick 			ufs_abortop(fndp);
56937737Smckusick 			ufs_abortop(tndp);
57037737Smckusick 			return (EINVAL);
5719167Ssam 		}
57216776Smckusick 		ip->i_flag |= IRENAME;
5739167Ssam 		oldparent = dp->i_number;
5749167Ssam 		doingdirectory++;
5759167Ssam 	}
57637737Smckusick 	vrele(fndp->ni_dvp);
5779167Ssam 
5789167Ssam 	/*
5799167Ssam 	 * 1) Bump link count while we're moving stuff
5809167Ssam 	 *    around.  If we crash somewhere before
5819167Ssam 	 *    completing our work, the link count
5829167Ssam 	 *    may be wrong, but correctable.
5839167Ssam 	 */
5849167Ssam 	ip->i_nlink++;
5859167Ssam 	ip->i_flag |= ICHG;
58637737Smckusick 	error = iupdat(ip, &time, &time, 1);
58716664Smckusick 	IUNLOCK(ip);
5889167Ssam 
5899167Ssam 	/*
5909167Ssam 	 * When the target exists, both the directory
59137737Smckusick 	 * and target vnodes are returned locked.
5929167Ssam 	 */
59337737Smckusick 	dp = VTOI(tndp->ni_dvp);
59437737Smckusick 	xp = NULL;
59537737Smckusick 	if (tndp->ni_vp)
59637737Smckusick 		xp = VTOI(tndp->ni_vp);
5979167Ssam 	/*
59811641Ssam 	 * If ".." must be changed (ie the directory gets a new
59912816Smckusick 	 * parent) then the source directory must not be in the
60012816Smckusick 	 * directory heirarchy above the target, as this would
60112816Smckusick 	 * orphan everything below the source directory. Also
60212816Smckusick 	 * the user must have write permission in the source so
60312816Smckusick 	 * as to be able to change "..". We must repeat the call
60412816Smckusick 	 * to namei, as the parent directory is unlocked by the
60512816Smckusick 	 * call to checkpath().
60611641Ssam 	 */
60716776Smckusick 	if (oldparent != dp->i_number)
60816776Smckusick 		newparent = dp->i_number;
60916776Smckusick 	if (doingdirectory && newparent) {
61037737Smckusick 		if (error = iaccess(ip, IWRITE, tndp->ni_cred))
61112816Smckusick 			goto bad;
61237737Smckusick 		tndp->ni_nameiop = RENAME | LOCKPARENT | LOCKLEAF | NOCACHE;
61312816Smckusick 		do {
61437737Smckusick 			dp = VTOI(tndp->ni_dvp);
61512816Smckusick 			if (xp != NULL)
61638069Smckusick 				iput(xp);
61737737Smckusick 			if (error = checkpath(ip, dp, tndp->ni_cred))
61812816Smckusick 				goto out;
61937737Smckusick 			if (error = namei(tndp))
62012816Smckusick 				goto out;
62137737Smckusick 			xp = NULL;
62237737Smckusick 			if (tndp->ni_vp)
62337737Smckusick 				xp = VTOI(tndp->ni_vp);
62437737Smckusick 		} while (dp != VTOI(tndp->ni_dvp));
62512816Smckusick 	}
62611641Ssam 	/*
6279167Ssam 	 * 2) If target doesn't exist, link the target
6289167Ssam 	 *    to the source and unlink the source.
6299167Ssam 	 *    Otherwise, rewrite the target directory
6309167Ssam 	 *    entry to reference the source inode and
6319167Ssam 	 *    expunge the original entry's existence.
6329167Ssam 	 */
6339167Ssam 	if (xp == NULL) {
63437737Smckusick 		if (dp->i_dev != ip->i_dev)
63537737Smckusick 			panic("rename: EXDEV");
6369167Ssam 		/*
63716776Smckusick 		 * Account for ".." in new directory.
63816776Smckusick 		 * When source and destination have the same
63916776Smckusick 		 * parent we don't fool with the link count.
6409167Ssam 		 */
64116776Smckusick 		if (doingdirectory && newparent) {
6429167Ssam 			dp->i_nlink++;
6439167Ssam 			dp->i_flag |= ICHG;
64437737Smckusick 			error = iupdat(dp, &time, &time, 1);
6459167Ssam 		}
64637737Smckusick 		if (error = direnter(ip, tndp))
6479167Ssam 			goto out;
6489167Ssam 	} else {
64937737Smckusick 		if (xp->i_dev != dp->i_dev || xp->i_dev != ip->i_dev)
65037737Smckusick 			panic("rename: EXDEV");
6519167Ssam 		/*
65210590Ssam 		 * Short circuit rename(foo, foo).
65310590Ssam 		 */
65410590Ssam 		if (xp->i_number == ip->i_number)
65537737Smckusick 			panic("rename: same file");
65610590Ssam 		/*
65724433Sbloom 		 * If the parent directory is "sticky", then the user must
65824433Sbloom 		 * own the parent directory, or the destination of the rename,
65924433Sbloom 		 * otherwise the destination may not be changed (except by
66024433Sbloom 		 * root). This implements append-only directories.
66124433Sbloom 		 */
66237737Smckusick 		if ((dp->i_mode & ISVTX) && tndp->ni_cred->cr_uid != 0 &&
66337737Smckusick 		    tndp->ni_cred->cr_uid != dp->i_uid &&
66437737Smckusick 		    xp->i_uid != tndp->ni_cred->cr_uid) {
66524433Sbloom 			error = EPERM;
66624433Sbloom 			goto bad;
66724433Sbloom 		}
66824433Sbloom 		/*
66910051Ssam 		 * Target must be empty if a directory
67010051Ssam 		 * and have no links to it.
6719167Ssam 		 * Also, insure source and target are
6729167Ssam 		 * compatible (both directories, or both
6739167Ssam 		 * not directories).
6749167Ssam 		 */
6759167Ssam 		if ((xp->i_mode&IFMT) == IFDIR) {
67637737Smckusick 			if (!dirempty(xp, dp->i_number, tndp->ni_cred) ||
67737737Smckusick 			    xp->i_nlink > 2) {
67810051Ssam 				error = ENOTEMPTY;
6799167Ssam 				goto bad;
6809167Ssam 			}
6819167Ssam 			if (!doingdirectory) {
68210051Ssam 				error = ENOTDIR;
6839167Ssam 				goto bad;
6849167Ssam 			}
68537737Smckusick 			cache_purge(ITOV(dp));
6869167Ssam 		} else if (doingdirectory) {
68710051Ssam 			error = EISDIR;
6889167Ssam 			goto bad;
6899167Ssam 		}
69037737Smckusick 		if (error = dirrewrite(dp, ip, tndp))
69137737Smckusick 			goto bad;
69237737Smckusick 		vput(ITOV(dp));
6939167Ssam 		/*
69410051Ssam 		 * Adjust the link count of the target to
69510051Ssam 		 * reflect the dirrewrite above.  If this is
69610051Ssam 		 * a directory it is empty and there are
69710051Ssam 		 * no links to it, so we can squash the inode and
69810051Ssam 		 * any space associated with it.  We disallowed
69910051Ssam 		 * renaming over top of a directory with links to
70016776Smckusick 		 * it above, as the remaining link would point to
70116776Smckusick 		 * a directory without "." or ".." entries.
7029167Ssam 		 */
70310051Ssam 		xp->i_nlink--;
7049167Ssam 		if (doingdirectory) {
70510051Ssam 			if (--xp->i_nlink != 0)
70610051Ssam 				panic("rename: linked directory");
70737737Smckusick 			error = itrunc(xp, (u_long)0);
70810051Ssam 		}
7099167Ssam 		xp->i_flag |= ICHG;
71038398Smckusick 		iput(xp);
71110246Ssam 		xp = NULL;
7129167Ssam 	}
7139167Ssam 
7149167Ssam 	/*
7159167Ssam 	 * 3) Unlink the source.
7169167Ssam 	 */
71737737Smckusick 	fndp->ni_nameiop = DELETE | LOCKPARENT | LOCKLEAF;
71837737Smckusick 	(void)namei(fndp);
71937737Smckusick 	if (fndp->ni_vp != NULL) {
72037737Smckusick 		xp = VTOI(fndp->ni_vp);
72137737Smckusick 		dp = VTOI(fndp->ni_dvp);
72237737Smckusick 	} else {
72338069Smckusick 		if (fndp->ni_dvp != NULL)
72438069Smckusick 			vput(fndp->ni_dvp);
72537737Smckusick 		xp = NULL;
72617758Smckusick 		dp = NULL;
72737737Smckusick 	}
7289167Ssam 	/*
72937737Smckusick 	 * Ensure that the directory entry still exists and has not
73016776Smckusick 	 * changed while the new name has been entered. If the source is
73116776Smckusick 	 * a file then the entry may have been unlinked or renamed. In
73216776Smckusick 	 * either case there is no further work to be done. If the source
73316776Smckusick 	 * is a directory then it cannot have been rmdir'ed; its link
73416776Smckusick 	 * count of three would cause a rmdir to fail with ENOTEMPTY.
73537737Smckusick 	 * The IRENAME flag ensures that it cannot be moved by another
73616776Smckusick 	 * rename.
7379167Ssam 	 */
73817758Smckusick 	if (xp != ip) {
73916776Smckusick 		if (doingdirectory)
74017758Smckusick 			panic("rename: lost dir entry");
74116776Smckusick 	} else {
7429167Ssam 		/*
74316776Smckusick 		 * If the source is a directory with a
74416776Smckusick 		 * new parent, the link count of the old
74516776Smckusick 		 * parent directory must be decremented
74616776Smckusick 		 * and ".." set to point to the new parent.
7479167Ssam 		 */
74816776Smckusick 		if (doingdirectory && newparent) {
7499167Ssam 			dp->i_nlink--;
7509167Ssam 			dp->i_flag |= ICHG;
75116776Smckusick 			error = rdwri(UIO_READ, xp, (caddr_t)&dirbuf,
75237737Smckusick 				sizeof (struct dirtemplate), (off_t)0,
75337740Smckusick 				UIO_SYSSPACE, tndp->ni_cred, (int *)0);
75416776Smckusick 			if (error == 0) {
75516776Smckusick 				if (dirbuf.dotdot_namlen != 2 ||
75616776Smckusick 				    dirbuf.dotdot_name[0] != '.' ||
75716776Smckusick 				    dirbuf.dotdot_name[1] != '.') {
75816776Smckusick 					printf("rename: mangled dir\n");
75916776Smckusick 				} else {
76016776Smckusick 					dirbuf.dotdot_ino = newparent;
76116776Smckusick 					(void) rdwri(UIO_WRITE, xp,
76216776Smckusick 					    (caddr_t)&dirbuf,
76316776Smckusick 					    sizeof (struct dirtemplate),
76437740Smckusick 					    (off_t)0, UIO_SYSSPACE,
76537737Smckusick 					    tndp->ni_cred, (int *)0);
76637737Smckusick 					cache_purge(ITOV(dp));
76716776Smckusick 				}
76816776Smckusick 			}
7699167Ssam 		}
77037737Smckusick 		error = dirremove(fndp);
77137737Smckusick 		if (!error) {
77216776Smckusick 			xp->i_nlink--;
77316776Smckusick 			xp->i_flag |= ICHG;
7749167Ssam 		}
77516776Smckusick 		xp->i_flag &= ~IRENAME;
7769167Ssam 	}
7779167Ssam 	if (dp)
77837737Smckusick 		vput(ITOV(dp));
77916776Smckusick 	if (xp)
78037737Smckusick 		vput(ITOV(xp));
78137737Smckusick 	vrele(ITOV(ip));
78237737Smckusick 	return (error);
7839167Ssam 
7849167Ssam bad:
7859167Ssam 	if (xp)
78637737Smckusick 		vput(ITOV(xp));
78737737Smckusick 	vput(ITOV(dp));
7889167Ssam out:
7899167Ssam 	ip->i_nlink--;
7909167Ssam 	ip->i_flag |= ICHG;
79137737Smckusick 	vrele(ITOV(ip));
79237737Smckusick 	return (error);
7937701Ssam }
7947701Ssam 
7957535Sroot /*
79612756Ssam  * A virgin directory (no blushing please).
79712756Ssam  */
79812756Ssam struct dirtemplate mastertemplate = {
79912756Ssam 	0, 12, 1, ".",
80012756Ssam 	0, DIRBLKSIZ - 12, 2, ".."
80112756Ssam };
80212756Ssam 
80312756Ssam /*
80412756Ssam  * Mkdir system call
80512756Ssam  */
80637737Smckusick ufs_mkdir(ndp, vap)
80737737Smckusick 	struct nameidata *ndp;
80837737Smckusick 	struct vattr *vap;
80912756Ssam {
81012756Ssam 	register struct inode *ip, *dp;
81137737Smckusick 	struct inode *tip;
81237737Smckusick 	struct vnode *dvp;
81312756Ssam 	struct dirtemplate dirtemplate;
81437737Smckusick 	int error;
81537737Smckusick 	int dmode;
81612756Ssam 
81737737Smckusick 	dvp = ndp->ni_dvp;
81837737Smckusick 	dp = VTOI(dvp);
81937737Smckusick 	dmode = vap->va_mode&0777;
82037737Smckusick 	dmode |= IFDIR;
82112756Ssam 	/*
82212756Ssam 	 * Must simulate part of maknode here
82312756Ssam 	 * in order to acquire the inode, but
82412756Ssam 	 * not have it entered in the parent
82512756Ssam 	 * directory.  The entry is made later
82612756Ssam 	 * after writing "." and ".." entries out.
82712756Ssam 	 */
82837737Smckusick 	error = ialloc(dp, dirpref(dp->i_fs), dmode, &tip);
82937737Smckusick 	if (error) {
83012756Ssam 		iput(dp);
83137737Smckusick 		return (error);
83212756Ssam 	}
83337737Smckusick 	ip = tip;
83412756Ssam #ifdef QUOTA
83512756Ssam 	if (ip->i_dquot != NODQUOT)
83612756Ssam 		panic("mkdir: dquot");
83712756Ssam #endif
83812756Ssam 	ip->i_flag |= IACC|IUPD|ICHG;
83937737Smckusick 	ip->i_mode = dmode;
84037737Smckusick 	ITOV(ip)->v_type = VDIR;	/* Rest init'd in iget() */
84112756Ssam 	ip->i_nlink = 2;
84237737Smckusick 	ip->i_uid = ndp->ni_cred->cr_uid;
84312756Ssam 	ip->i_gid = dp->i_gid;
84412756Ssam #ifdef QUOTA
84512756Ssam 	ip->i_dquot = inoquota(ip);
84612756Ssam #endif
84737737Smckusick 	error = iupdat(ip, &time, &time, 1);
84812756Ssam 
84912756Ssam 	/*
85012756Ssam 	 * Bump link count in parent directory
85112756Ssam 	 * to reflect work done below.  Should
85212756Ssam 	 * be done before reference is created
85312756Ssam 	 * so reparation is possible if we crash.
85412756Ssam 	 */
85512756Ssam 	dp->i_nlink++;
85612756Ssam 	dp->i_flag |= ICHG;
85737737Smckusick 	error = iupdat(dp, &time, &time, 1);
85812756Ssam 
85912756Ssam 	/*
86012756Ssam 	 * Initialize directory with "."
86112756Ssam 	 * and ".." from static template.
86212756Ssam 	 */
86312756Ssam 	dirtemplate = mastertemplate;
86412756Ssam 	dirtemplate.dot_ino = ip->i_number;
86512756Ssam 	dirtemplate.dotdot_ino = dp->i_number;
86637737Smckusick 	error = rdwri(UIO_WRITE, ip, (caddr_t)&dirtemplate,
86737737Smckusick 		sizeof (dirtemplate), (off_t)0, UIO_SYSSPACE,
86837737Smckusick 		ndp->ni_cred, (int *)0);
86937737Smckusick 	if (error) {
87012756Ssam 		dp->i_nlink--;
87112756Ssam 		dp->i_flag |= ICHG;
87212756Ssam 		goto bad;
87312756Ssam 	}
87437737Smckusick 	if (DIRBLKSIZ > dp->i_fs->fs_fsize)
87537737Smckusick 		panic("mkdir: blksize");     /* XXX - should grow w/balloc() */
87618103Smckusick 	else
87718103Smckusick 		ip->i_size = DIRBLKSIZ;
87812756Ssam 	/*
87912756Ssam 	 * Directory all set up, now
88012756Ssam 	 * install the entry for it in
88112756Ssam 	 * the parent directory.
88212756Ssam 	 */
88337737Smckusick 	error = direnter(ip, ndp);
88412756Ssam 	dp = NULL;
88537737Smckusick 	if (error) {
88616694Smckusick 		ndp->ni_nameiop = LOOKUP | NOCACHE;
88737737Smckusick 		error = namei(ndp);
88837737Smckusick 		if (!error) {
88937737Smckusick 			dp = VTOI(ndp->ni_vp);
89012756Ssam 			dp->i_nlink--;
89112756Ssam 			dp->i_flag |= ICHG;
89212756Ssam 		}
89312756Ssam 	}
89412756Ssam bad:
89512756Ssam 	/*
89612756Ssam 	 * No need to do an explicit itrunc here,
89737737Smckusick 	 * vrele will do this for us because we set
89812756Ssam 	 * the link count to 0.
89912756Ssam 	 */
90037737Smckusick 	if (error) {
90112756Ssam 		ip->i_nlink = 0;
90212756Ssam 		ip->i_flag |= ICHG;
90338144Smckusick 		iput(ip);
90438144Smckusick 	} else
90538144Smckusick 		ndp->ni_vp = ITOV(ip);
90612756Ssam 	if (dp)
90712756Ssam 		iput(dp);
90837737Smckusick 	return (error);
90912756Ssam }
91012756Ssam 
91112756Ssam /*
91212756Ssam  * Rmdir system call.
91312756Ssam  */
91437737Smckusick ufs_rmdir(ndp)
91537737Smckusick 	register struct nameidata *ndp;
91612756Ssam {
91712756Ssam 	register struct inode *ip, *dp;
91837737Smckusick 	int error = 0;
91912756Ssam 
92037737Smckusick 	ip = VTOI(ndp->ni_vp);
92137737Smckusick 	dp = VTOI(ndp->ni_dvp);
92212756Ssam 	/*
92312756Ssam 	 * No rmdir "." please.
92412756Ssam 	 */
92512756Ssam 	if (dp == ip) {
92637737Smckusick 		vrele(ITOV(dp));
92712756Ssam 		iput(ip);
92837737Smckusick 		return (EINVAL);
92912756Ssam 	}
93012756Ssam 	/*
93112756Ssam 	 * Verify the directory is empty (and valid).
93212756Ssam 	 * (Rmdir ".." won't be valid since
93312756Ssam 	 *  ".." will contain a reference to
93412756Ssam 	 *  the current directory and thus be
93512756Ssam 	 *  non-empty.)
93612756Ssam 	 */
93737737Smckusick 	if (ip->i_nlink != 2 || !dirempty(ip, dp->i_number, ndp->ni_cred)) {
93837737Smckusick 		error = ENOTEMPTY;
93912756Ssam 		goto out;
94012756Ssam 	}
94112756Ssam 	/*
94212756Ssam 	 * Delete reference to directory before purging
94312756Ssam 	 * inode.  If we crash in between, the directory
94412756Ssam 	 * will be reattached to lost+found,
94512756Ssam 	 */
94637737Smckusick 	if (error = dirremove(ndp))
94712756Ssam 		goto out;
94812756Ssam 	dp->i_nlink--;
94912756Ssam 	dp->i_flag |= ICHG;
95037737Smckusick 	cache_purge(ITOV(dp));
95112756Ssam 	iput(dp);
95237737Smckusick 	ndp->ni_dvp = NULL;
95312756Ssam 	/*
95412756Ssam 	 * Truncate inode.  The only stuff left
95512756Ssam 	 * in the directory is "." and "..".  The
95612756Ssam 	 * "." reference is inconsequential since
95712756Ssam 	 * we're quashing it.  The ".." reference
95812756Ssam 	 * has already been adjusted above.  We've
95912756Ssam 	 * removed the "." reference and the reference
96012756Ssam 	 * in the parent directory, but there may be
96112756Ssam 	 * other hard links so decrement by 2 and
96212756Ssam 	 * worry about them later.
96312756Ssam 	 */
96412756Ssam 	ip->i_nlink -= 2;
96537737Smckusick 	error = itrunc(ip, (u_long)0);
96637737Smckusick 	cache_purge(ITOV(ip));
96712756Ssam out:
96837737Smckusick 	if (ndp->ni_dvp)
96912756Ssam 		iput(dp);
97012756Ssam 	iput(ip);
97137737Smckusick 	return (error);
97212756Ssam }
97312756Ssam 
97437737Smckusick /*
97537737Smckusick  * symlink -- make a symbolic link
97637737Smckusick  */
97737737Smckusick ufs_symlink(ndp, vap, target)
97837737Smckusick 	struct nameidata *ndp;
97937737Smckusick 	struct vattr *vap;
98037737Smckusick 	char *target;
98112756Ssam {
98237737Smckusick 	struct inode *ip;
98337737Smckusick 	int error;
98412756Ssam 
98537737Smckusick 	error = maknode(IFLNK | vap->va_mode, ndp, &ip);
98637737Smckusick 	if (error)
98737737Smckusick 		return (error);
98837737Smckusick 	error = rdwri(UIO_WRITE, ip, target, strlen(target), (off_t)0,
98937737Smckusick 		UIO_SYSSPACE, ndp->ni_cred, (int *)0);
99037737Smckusick 	iput(ip);
99137737Smckusick 	return (error);
99237737Smckusick }
99337737Smckusick 
99437737Smckusick /*
99537737Smckusick  * Vnode op for read and write
99637737Smckusick  */
99737737Smckusick ufs_readdir(vp, uio, offp, cred)
99837737Smckusick 	struct vnode *vp;
99937737Smckusick 	register struct uio *uio;
100037737Smckusick 	off_t *offp;
100137737Smckusick 	struct ucred *cred;
100237737Smckusick {
100337737Smckusick 	register struct inode *ip = VTOI(vp);
100437737Smckusick 	int count, error;
100537737Smckusick 
100637737Smckusick 	ILOCK(ip);
100737737Smckusick 	uio->uio_offset = *offp;
100837737Smckusick 	count = uio->uio_resid;
100937737Smckusick 	count &= ~(DIRBLKSIZ - 1);
101037737Smckusick 	if (vp->v_type != VDIR || uio->uio_iovcnt != 1 ||
101137737Smckusick 	    (count < DIRBLKSIZ) || (uio->uio_offset & (DIRBLKSIZ -1))) {
101237737Smckusick 		IUNLOCK(ip);
101337737Smckusick 		return (EINVAL);
101416540Ssam 	}
101537737Smckusick 	uio->uio_resid = count;
101637737Smckusick 	uio->uio_iov->iov_len = count;
101737737Smckusick 	error = readip(ip, uio, cred);
101837737Smckusick 	*offp += count - uio->uio_resid;
101937737Smckusick 	IUNLOCK(ip);
102037737Smckusick 	return (error);
102137737Smckusick }
102237737Smckusick 
102337737Smckusick /*
102437737Smckusick  * Return target name of a symbolic link
102537737Smckusick  */
102637737Smckusick ufs_readlink(vp, uiop, cred)
102737737Smckusick 	struct vnode *vp;
102837737Smckusick 	struct uio *uiop;
102937737Smckusick 	struct ucred *cred;
103037737Smckusick {
103137737Smckusick 
103237737Smckusick 	return (readip(VTOI(vp), uiop, cred));
103337737Smckusick }
103437737Smckusick 
103537737Smckusick /*
103637737Smckusick  * Ufs abort op, called after namei() when a CREATE/DELETE isn't actually
103737737Smckusick  * done. Iff ni_vp/ni_dvp not null and locked, unlock.
103837737Smckusick  */
103937737Smckusick ufs_abortop(ndp)
104037737Smckusick 	register struct nameidata *ndp;
104137737Smckusick {
104237737Smckusick 	register struct inode *ip;
104337737Smckusick 
104437737Smckusick 	if (ndp->ni_vp) {
104537737Smckusick 		ip = VTOI(ndp->ni_vp);
104637737Smckusick 		if (ip->i_flag & ILOCKED)
104737737Smckusick 			IUNLOCK(ip);
104837737Smckusick 		vrele(ndp->ni_vp);
104912756Ssam 	}
105037737Smckusick 	if (ndp->ni_dvp) {
105137737Smckusick 		ip = VTOI(ndp->ni_dvp);
105237737Smckusick 		if (ip->i_flag & ILOCKED)
105337737Smckusick 			IUNLOCK(ip);
105437737Smckusick 		vrele(ndp->ni_dvp);
105537737Smckusick 	}
105637737Smckusick 	return;
105712756Ssam }
105812756Ssam 
105937737Smckusick ufs_lock(vp)
106037737Smckusick 	struct vnode *vp;
106137737Smckusick {
106237737Smckusick 	register struct inode *ip = VTOI(vp);
106337737Smckusick 
106437737Smckusick 	ILOCK(ip);
106537737Smckusick 	return (0);
106637737Smckusick }
106737737Smckusick 
106837737Smckusick ufs_unlock(vp)
106937737Smckusick 	struct vnode *vp;
107037737Smckusick {
107137737Smckusick 	register struct inode *ip = VTOI(vp);
107237737Smckusick 
107337737Smckusick 	if (!(ip->i_flag & ILOCKED))
107437737Smckusick 		panic("ufs_unlock NOT LOCKED");
107537737Smckusick 	IUNLOCK(ip);
107637737Smckusick 	return (0);
107737737Smckusick }
107837737Smckusick 
107912756Ssam /*
108037737Smckusick  * Get access to bmap
108112756Ssam  */
108237737Smckusick ufs_bmap(vp, bn, vpp, bnp)
108337737Smckusick 	struct vnode *vp;
108437737Smckusick 	daddr_t bn;
108537737Smckusick 	struct vnode **vpp;
108637737Smckusick 	daddr_t *bnp;
108712756Ssam {
108837737Smckusick 	struct inode *ip = VTOI(vp);
108912756Ssam 
109037737Smckusick 	if (vpp != NULL)
109137737Smckusick 		*vpp = ip->i_devvp;
109237737Smckusick 	if (bnp == NULL)
109337737Smckusick 		return (0);
109437737Smckusick 	return (bmap(ip, bn, bnp, (daddr_t *)0, (int *)0));
109512756Ssam }
109637737Smckusick 
109737737Smckusick /*
109837737Smckusick  * Just call the device strategy routine
109937737Smckusick  */
110037737Smckusick ufs_strategy(bp)
110137737Smckusick 	register struct buf *bp;
110237737Smckusick {
110337737Smckusick 	(*bdevsw[major(bp->b_dev)].d_strategy)(bp);
110437737Smckusick 	return (0);
110537737Smckusick }
110637737Smckusick 
110737737Smckusick /*
110837737Smckusick  * Make a new file.
110937737Smckusick  */
111037737Smckusick maknode(mode, ndp, ipp)
111137737Smckusick 	int mode;
111237737Smckusick 	register struct nameidata *ndp;
111337737Smckusick 	struct inode **ipp;
111437737Smckusick {
111537737Smckusick 	register struct inode *ip;
111637737Smckusick 	struct inode *tip;
111737737Smckusick 	register struct inode *pdir = VTOI(ndp->ni_dvp);
111837737Smckusick 	ino_t ipref;
111937737Smckusick 	int error;
112037737Smckusick 
112137737Smckusick 	*ipp = 0;
112237737Smckusick 	if ((mode & IFMT) == IFDIR)
112337737Smckusick 		ipref = dirpref(pdir->i_fs);
112437737Smckusick 	else
112537737Smckusick 		ipref = pdir->i_number;
112637737Smckusick 	error = ialloc(pdir, ipref, mode, &tip);
112737737Smckusick 	if (error) {
112837737Smckusick 		iput(pdir);
112937737Smckusick 		return (error);
113037737Smckusick 	}
113137737Smckusick 	ip = tip;
113237737Smckusick #ifdef QUOTA
113337737Smckusick 	if (ip->i_dquot != NODQUOT)
113437737Smckusick 		panic("maknode: dquot");
113537737Smckusick #endif
113637737Smckusick 	ip->i_flag |= IACC|IUPD|ICHG;
113737737Smckusick 	if ((mode & IFMT) == 0)
113837737Smckusick 		mode |= IFREG;
113937737Smckusick 	ip->i_mode = mode;
114037737Smckusick 	ITOV(ip)->v_type = IFTOVT(mode);	/* Rest init'd in iget() */
114137737Smckusick 	ip->i_nlink = 1;
114237737Smckusick 	ip->i_uid = ndp->ni_cred->cr_uid;
114337737Smckusick 	ip->i_gid = pdir->i_gid;
114437737Smckusick 	if ((ip->i_mode & ISGID) && !groupmember(ip->i_gid, ndp->ni_cred) &&
114537737Smckusick 	    suser(ndp->ni_cred, NULL))
114637737Smckusick 		ip->i_mode &= ~ISGID;
114737737Smckusick #ifdef QUOTA
114837737Smckusick 	ip->i_dquot = inoquota(ip);
114937737Smckusick #endif
115037737Smckusick 
115137737Smckusick 	/*
115237737Smckusick 	 * Make sure inode goes to disk before directory entry.
115337737Smckusick 	 */
115437737Smckusick 	if ((error = iupdat(ip, &time, &time, 1)) ||
115537737Smckusick 	    (error = direnter(ip, ndp))) {
115637737Smckusick 		/*
115737737Smckusick 		 * Write error occurred trying to update the inode
115837737Smckusick 		 * or the directory so must deallocate the inode.
115937737Smckusick 		 */
116037737Smckusick 		ip->i_nlink = 0;
116137737Smckusick 		ip->i_flag |= ICHG;
116237737Smckusick 		iput(ip);
116337737Smckusick 		return (error);
116437737Smckusick 	}
116537737Smckusick 	*ipp = ip;
116637737Smckusick 	return (0);
116737737Smckusick }
1168