xref: /csrg-svn/sys/ufs/lfs/lfs_vnops.c (revision 38578)
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*38578Smckusick  *	@(#)lfs_vnops.c	7.14 (Berkeley) 08/10/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;
225*38578Smckusick 	vap->va_atime.tv_usec = 0;
22637737Smckusick 	vap->va_mtime.tv_sec = ip->i_mtime;
227*38578Smckusick 	vap->va_mtime.tv_usec = 0;
22837737Smckusick 	vap->va_ctime.tv_sec = ip->i_ctime;
229*38578Smckusick 	vap->va_ctime.tv_usec = 0;
23038254Smckusick 	vap->va_flags = ip->i_flags;
23138254Smckusick 	vap->va_gen = ip->i_gen;
23237737Smckusick 	/* this doesn't belong here */
23337737Smckusick 	if (vp->v_type == VBLK)
23437737Smckusick 		vap->va_blocksize = BLKDEV_IOSIZE;
23537737Smckusick 	else if (vp->v_type == VCHR)
23637737Smckusick 		vap->va_blocksize = MAXBSIZE;
2377142Smckusick 	else
23837737Smckusick 		vap->va_blocksize = ip->i_fs->fs_bsize;
23937737Smckusick 	/*
24037737Smckusick 	 * XXX THIS IS NOT CORRECT!!, but be sure to change vn_stat()
24137737Smckusick 	 * if you change it.
24237737Smckusick 	 */
24337737Smckusick 	vap->va_bytes = ip->i_blocks;
24437737Smckusick 	vap->va_bytes1 = -1;
24537737Smckusick 	vap->va_type = vp->v_type;
24637737Smckusick 	return (0);
2476254Sroot }
2486254Sroot 
2496254Sroot /*
25037737Smckusick  * Set attribute vnode op. called from several syscalls
2516254Sroot  */
25237737Smckusick ufs_setattr(vp, vap, cred)
25337737Smckusick 	register struct vnode *vp;
25437737Smckusick 	register struct vattr *vap;
25537737Smckusick 	register struct ucred *cred;
2566254Sroot {
25737737Smckusick 	register struct inode *ip = VTOI(vp);
25837737Smckusick 	int error = 0;
2596254Sroot 
26037737Smckusick 	/*
26137737Smckusick 	 * Check for unsetable attributes.
26237737Smckusick 	 */
26337737Smckusick 	if ((vap->va_type != VNON) || (vap->va_nlink != VNOVAL) ||
26437737Smckusick 	    (vap->va_fsid != VNOVAL) || (vap->va_fileid != VNOVAL) ||
26537737Smckusick 	    (vap->va_blocksize != VNOVAL) || (vap->va_rdev != VNOVAL) ||
26638254Smckusick 	    ((int)vap->va_bytes != VNOVAL) || (vap->va_gen != VNOVAL)) {
26737737Smckusick 		return (EINVAL);
26816540Ssam 	}
26937737Smckusick 	/*
27037737Smckusick 	 * Go through the fields and update iff not VNOVAL.
27137737Smckusick 	 */
27237737Smckusick 	if (vap->va_uid != (u_short)VNOVAL || vap->va_gid != (u_short)VNOVAL)
27337737Smckusick 		if (error = chown1(vp, vap->va_uid, vap->va_gid, cred))
27437737Smckusick 			return (error);
27537737Smckusick 	if (vap->va_size != VNOVAL) {
27637737Smckusick 		if (vp->v_type == VDIR)
27737737Smckusick 			return (EISDIR);
27837737Smckusick 		if (error = itrunc(ip, vap->va_size))
27937737Smckusick 			return (error);
28013878Ssam 	}
28137737Smckusick 	if (vap->va_atime.tv_sec != VNOVAL || vap->va_mtime.tv_sec != VNOVAL) {
28237773Smckusick 		if (cred->cr_uid != ip->i_uid &&
28337773Smckusick 		    (error = suser(cred, &u.u_acflag)))
28437773Smckusick 			return (error);
28537737Smckusick 		if (vap->va_atime.tv_sec != VNOVAL)
28637737Smckusick 			ip->i_flag |= IACC;
28737737Smckusick 		if (vap->va_mtime.tv_sec != VNOVAL)
28837737Smckusick 			ip->i_flag |= IUPD;
28937737Smckusick 		ip->i_flag |= ICHG;
29037737Smckusick 		if (error = iupdat(ip, &vap->va_atime, &vap->va_mtime, 1))
29137737Smckusick 			return (error);
2926254Sroot 	}
29337737Smckusick 	if (vap->va_mode != (u_short)VNOVAL)
29437737Smckusick 		error = chmod1(vp, (int)vap->va_mode, cred);
29538254Smckusick 	if (vap->va_flags != VNOVAL) {
29638254Smckusick 		if (cred->cr_uid != ip->i_uid &&
29738254Smckusick 		    (error = suser(cred, &u.u_acflag)))
29838254Smckusick 			return (error);
29938254Smckusick 		if (cred->cr_uid == 0) {
30038254Smckusick 			ip->i_flags = vap->va_flags;
30138254Smckusick 		} else {
30238254Smckusick 			ip->i_flags &= 0xffff0000;
30338254Smckusick 			ip->i_flags |= (vap->va_flags & 0xffff);
30438254Smckusick 		}
30538254Smckusick 		ip->i_flag |= ICHG;
30638254Smckusick 	}
30737737Smckusick 	return (error);
3086254Sroot }
3096254Sroot 
3106254Sroot /*
3119167Ssam  * Change the mode on a file.
3129167Ssam  * Inode must be locked before calling.
3139167Ssam  */
31437737Smckusick chmod1(vp, mode, cred)
31537737Smckusick 	register struct vnode *vp;
3167701Ssam 	register int mode;
31737737Smckusick 	struct ucred *cred;
3187701Ssam {
31937737Smckusick 	register struct inode *ip = VTOI(vp);
32037773Smckusick 	int error;
3217868Sroot 
32237773Smckusick 	if (cred->cr_uid != ip->i_uid &&
32337773Smckusick 	    (error = suser(cred, &u.u_acflag)))
32437773Smckusick 		return (error);
3256254Sroot 	ip->i_mode &= ~07777;
32637737Smckusick 	if (cred->cr_uid) {
32737737Smckusick 		if (vp->v_type != VDIR)
32821015Smckusick 			mode &= ~ISVTX;
32937737Smckusick 		if (!groupmember(ip->i_gid, cred))
33011811Ssam 			mode &= ~ISGID;
3317439Sroot 	}
33237737Smckusick 	ip->i_mode |= mode & 07777;
3336254Sroot 	ip->i_flag |= ICHG;
33437737Smckusick 	if ((vp->v_flag & VTEXT) && (ip->i_mode & ISVTX) == 0)
33537737Smckusick 		xrele(vp);
33621015Smckusick 	return (0);
3375992Swnj }
3385992Swnj 
3399167Ssam /*
3407701Ssam  * Perform chown operation on inode ip;
3417701Ssam  * inode must be locked prior to call.
3427701Ssam  */
34337737Smckusick chown1(vp, uid, gid, cred)
34437737Smckusick 	register struct vnode *vp;
34537737Smckusick 	uid_t uid;
34637737Smckusick 	gid_t gid;
34737737Smckusick 	struct ucred *cred;
3487701Ssam {
34937737Smckusick 	register struct inode *ip = VTOI(vp);
3507701Ssam #ifdef QUOTA
3517701Ssam 	register long change;
35211811Ssam #endif
35337737Smckusick 	int error;
3547701Ssam 
35537737Smckusick 	if (uid == (u_short)VNOVAL)
35611811Ssam 		uid = ip->i_uid;
35737737Smckusick 	if (gid == (u_short)VNOVAL)
35811811Ssam 		gid = ip->i_gid;
35936614Sbostic 	/*
36036614Sbostic 	 * If we don't own the file, are trying to change the owner
36136614Sbostic 	 * of the file, or are not a member of the target group,
36236614Sbostic 	 * the caller must be superuser or the call fails.
36336614Sbostic 	 */
36437737Smckusick 	if ((cred->cr_uid != ip->i_uid || uid != ip->i_uid ||
36537737Smckusick 	    !groupmember((gid_t)gid, cred)) &&
36637737Smckusick 	    (error = suser(cred, &u.u_acflag)))
36737737Smckusick 		return (error);
36811811Ssam #ifdef QUOTA
36914385Ssam 	if (ip->i_uid == uid)		/* this just speeds things a little */
3707482Skre 		change = 0;
37112646Ssam 	else
37212646Ssam 		change = ip->i_blocks;
37312646Ssam 	(void) chkdq(ip, -change, 1);
37412646Ssam 	(void) chkiq(ip->i_dev, ip, ip->i_uid, 1);
3757482Skre 	dqrele(ip->i_dquot);
3767482Skre #endif
37711811Ssam 	ip->i_uid = uid;
37811811Ssam 	ip->i_gid = gid;
3796254Sroot 	ip->i_flag |= ICHG;
38037737Smckusick 	if (cred->cr_ruid != 0)
3816254Sroot 		ip->i_mode &= ~(ISUID|ISGID);
3827701Ssam #ifdef QUOTA
3837482Skre 	ip->i_dquot = inoquota(ip);
38412646Ssam 	(void) chkdq(ip, change, 1);
38526361Skarels 	(void) chkiq(ip->i_dev, (struct inode *)NULL, (uid_t)uid, 1);
38612646Ssam 	return (u.u_error);		/* should == 0 ALWAYS !! */
38712646Ssam #else
38812646Ssam 	return (0);
3897482Skre #endif
39037Sbill }
39137Sbill 
39237737Smckusick /* ARGSUSED */
39337737Smckusick ufs_ioctl(vp, com, data, fflag, cred)
39437737Smckusick 	struct vnode *vp;
39537737Smckusick 	int com;
39637737Smckusick 	caddr_t data;
39737737Smckusick 	int fflag;
39837737Smckusick 	struct ucred *cred;
39911811Ssam {
40011811Ssam 
40137737Smckusick 	printf("ufs_ioctl called with type %d\n", vp->v_type);
40237737Smckusick 	return (ENOTTY);
40311811Ssam }
40411811Ssam 
40537737Smckusick /* ARGSUSED */
40637737Smckusick ufs_select(vp, which, cred)
40737737Smckusick 	struct vnode *vp;
40837737Smckusick 	int which;
40937737Smckusick 	struct ucred *cred;
41037737Smckusick {
41137737Smckusick 
41237737Smckusick 	printf("ufs_select called with type %d\n", vp->v_type);
41337737Smckusick 	return (1);		/* XXX */
41437737Smckusick }
41537737Smckusick 
4169167Ssam /*
41737737Smckusick  * Mmap a file
41837737Smckusick  *
41937737Smckusick  * NB Currently unsupported.
4209167Ssam  */
42137737Smckusick /* ARGSUSED */
42237737Smckusick ufs_mmap(vp, fflags, cred)
42337737Smckusick 	struct vnode *vp;
42437737Smckusick 	int fflags;
42537737Smckusick 	struct ucred *cred;
42637Sbill {
42737Sbill 
42837737Smckusick 	return (EINVAL);
42937Sbill }
4307535Sroot 
4319167Ssam /*
43237737Smckusick  * Synch an open file.
4339167Ssam  */
43437737Smckusick /* ARGSUSED */
43537737Smckusick ufs_fsync(vp, fflags, cred)
43637737Smckusick 	struct vnode *vp;
43737737Smckusick 	int fflags;
43837737Smckusick 	struct ucred *cred;
4397701Ssam {
44037737Smckusick 	register struct inode *ip = VTOI(vp);
44137737Smckusick 	int error;
4427701Ssam 
44337737Smckusick 	ILOCK(ip);
44437737Smckusick 	if (fflags&FWRITE)
44537737Smckusick 		ip->i_flag |= ICHG;
44637737Smckusick 	error = syncip(ip);
44737737Smckusick 	IUNLOCK(ip);
44837737Smckusick 	return (error);
4497701Ssam }
4507701Ssam 
4519167Ssam /*
45237737Smckusick  * Seek on a file
45337737Smckusick  *
45437737Smckusick  * Nothing to do, so just return.
4559167Ssam  */
45637737Smckusick /* ARGSUSED */
45737737Smckusick ufs_seek(vp, oldoff, newoff, cred)
45837737Smckusick 	struct vnode *vp;
45937737Smckusick 	off_t oldoff, newoff;
46037737Smckusick 	struct ucred *cred;
4617701Ssam {
4627701Ssam 
46337737Smckusick 	return (0);
46437737Smckusick }
46537737Smckusick 
46637737Smckusick /*
46737737Smckusick  * ufs remove
46837737Smckusick  * Hard to avoid races here, especially
46937737Smckusick  * in unlinking directories.
47037737Smckusick  */
47137737Smckusick ufs_remove(ndp)
47237737Smckusick 	struct nameidata *ndp;
47337737Smckusick {
47437737Smckusick 	register struct inode *ip, *dp;
47537737Smckusick 	int error;
47637737Smckusick 
47737737Smckusick 	ip = VTOI(ndp->ni_vp);
47837737Smckusick 	dp = VTOI(ndp->ni_dvp);
47937737Smckusick 	error = dirremove(ndp);
48037737Smckusick 	if (!error) {
48137737Smckusick 		ip->i_nlink--;
48237737Smckusick 		ip->i_flag |= ICHG;
4837701Ssam 	}
48437737Smckusick 	if (dp == ip)
48537737Smckusick 		vrele(ITOV(ip));
48637737Smckusick 	else
48737737Smckusick 		iput(ip);
48837737Smckusick 	iput(dp);
48937737Smckusick 	return (error);
4907701Ssam }
4917701Ssam 
4929167Ssam /*
49337737Smckusick  * link vnode call
4949167Ssam  */
49537737Smckusick ufs_link(vp, ndp)
49637737Smckusick 	register struct vnode *vp;
49737737Smckusick 	register struct nameidata *ndp;
4989167Ssam {
49937737Smckusick 	register struct inode *ip = VTOI(vp);
50037737Smckusick 	int error;
5019167Ssam 
50237737Smckusick 	if (ndp->ni_dvp != vp)
50337737Smckusick 		ILOCK(ip);
50437737Smckusick 	if (ip->i_nlink == LINK_MAX - 1) {
50537737Smckusick 		error = EMLINK;
50637737Smckusick 		goto out;
50737737Smckusick 	}
50837737Smckusick 	ip->i_nlink++;
50937737Smckusick 	ip->i_flag |= ICHG;
51037737Smckusick 	error = iupdat(ip, &time, &time, 1);
51137737Smckusick 	if (!error)
51237737Smckusick 		error = direnter(ip, ndp);
51337737Smckusick out:
51437737Smckusick 	if (ndp->ni_dvp != vp)
51537737Smckusick 		IUNLOCK(ip);
51637737Smckusick 	if (error) {
51737737Smckusick 		ip->i_nlink--;
51830598Smckusick 		ip->i_flag |= ICHG;
51937737Smckusick 	}
52037737Smckusick 	return (error);
5219167Ssam }
5229167Ssam 
5239167Ssam /*
5249167Ssam  * Rename system call.
5259167Ssam  * 	rename("foo", "bar");
5269167Ssam  * is essentially
5279167Ssam  *	unlink("bar");
5289167Ssam  *	link("foo", "bar");
5299167Ssam  *	unlink("foo");
5309167Ssam  * but ``atomically''.  Can't do full commit without saving state in the
5319167Ssam  * inode on disk which isn't feasible at this time.  Best we can do is
5329167Ssam  * always guarantee the target exists.
5339167Ssam  *
5349167Ssam  * Basic algorithm is:
5359167Ssam  *
5369167Ssam  * 1) Bump link count on source while we're linking it to the
53737737Smckusick  *    target.  This also ensure the inode won't be deleted out
53816776Smckusick  *    from underneath us while we work (it may be truncated by
53916776Smckusick  *    a concurrent `trunc' or `open' for creation).
5409167Ssam  * 2) Link source to destination.  If destination already exists,
5419167Ssam  *    delete it first.
54216776Smckusick  * 3) Unlink source reference to inode if still around. If a
54316776Smckusick  *    directory was moved and the parent of the destination
5449167Ssam  *    is different from the source, patch the ".." entry in the
5459167Ssam  *    directory.
5469167Ssam  */
54737737Smckusick ufs_rename(fndp, tndp)
54837737Smckusick 	register struct nameidata *fndp, *tndp;
5497701Ssam {
5509167Ssam 	register struct inode *ip, *xp, *dp;
55116776Smckusick 	struct dirtemplate dirbuf;
55216776Smckusick 	int doingdirectory = 0, oldparent = 0, newparent = 0;
55310051Ssam 	int error = 0;
5547701Ssam 
55537737Smckusick 	dp = VTOI(fndp->ni_dvp);
55637737Smckusick 	ip = VTOI(fndp->ni_vp);
55737737Smckusick 	ILOCK(ip);
5589167Ssam 	if ((ip->i_mode&IFMT) == IFDIR) {
55937737Smckusick 		register struct direct *d = &fndp->ni_dent;
5609167Ssam 
5619167Ssam 		/*
56211641Ssam 		 * Avoid ".", "..", and aliases of "." for obvious reasons.
5639167Ssam 		 */
56437737Smckusick 		if ((d->d_namlen == 1 && d->d_name[0] == '.') || dp == ip ||
56537737Smckusick 		    fndp->ni_isdotdot || (ip->i_flag & IRENAME)) {
56637737Smckusick 			IUNLOCK(ip);
56737737Smckusick 			ufs_abortop(fndp);
56837737Smckusick 			ufs_abortop(tndp);
56937737Smckusick 			return (EINVAL);
5709167Ssam 		}
57116776Smckusick 		ip->i_flag |= IRENAME;
5729167Ssam 		oldparent = dp->i_number;
5739167Ssam 		doingdirectory++;
5749167Ssam 	}
57537737Smckusick 	vrele(fndp->ni_dvp);
5769167Ssam 
5779167Ssam 	/*
5789167Ssam 	 * 1) Bump link count while we're moving stuff
5799167Ssam 	 *    around.  If we crash somewhere before
5809167Ssam 	 *    completing our work, the link count
5819167Ssam 	 *    may be wrong, but correctable.
5829167Ssam 	 */
5839167Ssam 	ip->i_nlink++;
5849167Ssam 	ip->i_flag |= ICHG;
58537737Smckusick 	error = iupdat(ip, &time, &time, 1);
58616664Smckusick 	IUNLOCK(ip);
5879167Ssam 
5889167Ssam 	/*
5899167Ssam 	 * When the target exists, both the directory
59037737Smckusick 	 * and target vnodes are returned locked.
5919167Ssam 	 */
59237737Smckusick 	dp = VTOI(tndp->ni_dvp);
59337737Smckusick 	xp = NULL;
59437737Smckusick 	if (tndp->ni_vp)
59537737Smckusick 		xp = VTOI(tndp->ni_vp);
5969167Ssam 	/*
59711641Ssam 	 * If ".." must be changed (ie the directory gets a new
59812816Smckusick 	 * parent) then the source directory must not be in the
59912816Smckusick 	 * directory heirarchy above the target, as this would
60012816Smckusick 	 * orphan everything below the source directory. Also
60112816Smckusick 	 * the user must have write permission in the source so
60212816Smckusick 	 * as to be able to change "..". We must repeat the call
60312816Smckusick 	 * to namei, as the parent directory is unlocked by the
60412816Smckusick 	 * call to checkpath().
60511641Ssam 	 */
60616776Smckusick 	if (oldparent != dp->i_number)
60716776Smckusick 		newparent = dp->i_number;
60816776Smckusick 	if (doingdirectory && newparent) {
60937737Smckusick 		if (error = iaccess(ip, IWRITE, tndp->ni_cred))
61012816Smckusick 			goto bad;
61137737Smckusick 		tndp->ni_nameiop = RENAME | LOCKPARENT | LOCKLEAF | NOCACHE;
61212816Smckusick 		do {
61337737Smckusick 			dp = VTOI(tndp->ni_dvp);
61412816Smckusick 			if (xp != NULL)
61538069Smckusick 				iput(xp);
61637737Smckusick 			if (error = checkpath(ip, dp, tndp->ni_cred))
61712816Smckusick 				goto out;
61837737Smckusick 			if (error = namei(tndp))
61912816Smckusick 				goto out;
62037737Smckusick 			xp = NULL;
62137737Smckusick 			if (tndp->ni_vp)
62237737Smckusick 				xp = VTOI(tndp->ni_vp);
62337737Smckusick 		} while (dp != VTOI(tndp->ni_dvp));
62412816Smckusick 	}
62511641Ssam 	/*
6269167Ssam 	 * 2) If target doesn't exist, link the target
6279167Ssam 	 *    to the source and unlink the source.
6289167Ssam 	 *    Otherwise, rewrite the target directory
6299167Ssam 	 *    entry to reference the source inode and
6309167Ssam 	 *    expunge the original entry's existence.
6319167Ssam 	 */
6329167Ssam 	if (xp == NULL) {
63337737Smckusick 		if (dp->i_dev != ip->i_dev)
63437737Smckusick 			panic("rename: EXDEV");
6359167Ssam 		/*
63616776Smckusick 		 * Account for ".." in new directory.
63716776Smckusick 		 * When source and destination have the same
63816776Smckusick 		 * parent we don't fool with the link count.
6399167Ssam 		 */
64016776Smckusick 		if (doingdirectory && newparent) {
6419167Ssam 			dp->i_nlink++;
6429167Ssam 			dp->i_flag |= ICHG;
64337737Smckusick 			error = iupdat(dp, &time, &time, 1);
6449167Ssam 		}
64537737Smckusick 		if (error = direnter(ip, tndp))
6469167Ssam 			goto out;
6479167Ssam 	} else {
64837737Smckusick 		if (xp->i_dev != dp->i_dev || xp->i_dev != ip->i_dev)
64937737Smckusick 			panic("rename: EXDEV");
6509167Ssam 		/*
65110590Ssam 		 * Short circuit rename(foo, foo).
65210590Ssam 		 */
65310590Ssam 		if (xp->i_number == ip->i_number)
65437737Smckusick 			panic("rename: same file");
65510590Ssam 		/*
65624433Sbloom 		 * If the parent directory is "sticky", then the user must
65724433Sbloom 		 * own the parent directory, or the destination of the rename,
65824433Sbloom 		 * otherwise the destination may not be changed (except by
65924433Sbloom 		 * root). This implements append-only directories.
66024433Sbloom 		 */
66137737Smckusick 		if ((dp->i_mode & ISVTX) && tndp->ni_cred->cr_uid != 0 &&
66237737Smckusick 		    tndp->ni_cred->cr_uid != dp->i_uid &&
66337737Smckusick 		    xp->i_uid != tndp->ni_cred->cr_uid) {
66424433Sbloom 			error = EPERM;
66524433Sbloom 			goto bad;
66624433Sbloom 		}
66724433Sbloom 		/*
66810051Ssam 		 * Target must be empty if a directory
66910051Ssam 		 * and have no links to it.
6709167Ssam 		 * Also, insure source and target are
6719167Ssam 		 * compatible (both directories, or both
6729167Ssam 		 * not directories).
6739167Ssam 		 */
6749167Ssam 		if ((xp->i_mode&IFMT) == IFDIR) {
67537737Smckusick 			if (!dirempty(xp, dp->i_number, tndp->ni_cred) ||
67637737Smckusick 			    xp->i_nlink > 2) {
67710051Ssam 				error = ENOTEMPTY;
6789167Ssam 				goto bad;
6799167Ssam 			}
6809167Ssam 			if (!doingdirectory) {
68110051Ssam 				error = ENOTDIR;
6829167Ssam 				goto bad;
6839167Ssam 			}
68437737Smckusick 			cache_purge(ITOV(dp));
6859167Ssam 		} else if (doingdirectory) {
68610051Ssam 			error = EISDIR;
6879167Ssam 			goto bad;
6889167Ssam 		}
68937737Smckusick 		if (error = dirrewrite(dp, ip, tndp))
69037737Smckusick 			goto bad;
69137737Smckusick 		vput(ITOV(dp));
6929167Ssam 		/*
69310051Ssam 		 * Adjust the link count of the target to
69410051Ssam 		 * reflect the dirrewrite above.  If this is
69510051Ssam 		 * a directory it is empty and there are
69610051Ssam 		 * no links to it, so we can squash the inode and
69710051Ssam 		 * any space associated with it.  We disallowed
69810051Ssam 		 * renaming over top of a directory with links to
69916776Smckusick 		 * it above, as the remaining link would point to
70016776Smckusick 		 * a directory without "." or ".." entries.
7019167Ssam 		 */
70210051Ssam 		xp->i_nlink--;
7039167Ssam 		if (doingdirectory) {
70410051Ssam 			if (--xp->i_nlink != 0)
70510051Ssam 				panic("rename: linked directory");
70637737Smckusick 			error = itrunc(xp, (u_long)0);
70710051Ssam 		}
7089167Ssam 		xp->i_flag |= ICHG;
70938398Smckusick 		iput(xp);
71010246Ssam 		xp = NULL;
7119167Ssam 	}
7129167Ssam 
7139167Ssam 	/*
7149167Ssam 	 * 3) Unlink the source.
7159167Ssam 	 */
71637737Smckusick 	fndp->ni_nameiop = DELETE | LOCKPARENT | LOCKLEAF;
71737737Smckusick 	(void)namei(fndp);
71837737Smckusick 	if (fndp->ni_vp != NULL) {
71937737Smckusick 		xp = VTOI(fndp->ni_vp);
72037737Smckusick 		dp = VTOI(fndp->ni_dvp);
72137737Smckusick 	} else {
72238069Smckusick 		if (fndp->ni_dvp != NULL)
72338069Smckusick 			vput(fndp->ni_dvp);
72437737Smckusick 		xp = NULL;
72517758Smckusick 		dp = NULL;
72637737Smckusick 	}
7279167Ssam 	/*
72837737Smckusick 	 * Ensure that the directory entry still exists and has not
72916776Smckusick 	 * changed while the new name has been entered. If the source is
73016776Smckusick 	 * a file then the entry may have been unlinked or renamed. In
73116776Smckusick 	 * either case there is no further work to be done. If the source
73216776Smckusick 	 * is a directory then it cannot have been rmdir'ed; its link
73316776Smckusick 	 * count of three would cause a rmdir to fail with ENOTEMPTY.
73437737Smckusick 	 * The IRENAME flag ensures that it cannot be moved by another
73516776Smckusick 	 * rename.
7369167Ssam 	 */
73717758Smckusick 	if (xp != ip) {
73816776Smckusick 		if (doingdirectory)
73917758Smckusick 			panic("rename: lost dir entry");
74016776Smckusick 	} else {
7419167Ssam 		/*
74216776Smckusick 		 * If the source is a directory with a
74316776Smckusick 		 * new parent, the link count of the old
74416776Smckusick 		 * parent directory must be decremented
74516776Smckusick 		 * and ".." set to point to the new parent.
7469167Ssam 		 */
74716776Smckusick 		if (doingdirectory && newparent) {
7489167Ssam 			dp->i_nlink--;
7499167Ssam 			dp->i_flag |= ICHG;
75016776Smckusick 			error = rdwri(UIO_READ, xp, (caddr_t)&dirbuf,
75137737Smckusick 				sizeof (struct dirtemplate), (off_t)0,
75237740Smckusick 				UIO_SYSSPACE, tndp->ni_cred, (int *)0);
75316776Smckusick 			if (error == 0) {
75416776Smckusick 				if (dirbuf.dotdot_namlen != 2 ||
75516776Smckusick 				    dirbuf.dotdot_name[0] != '.' ||
75616776Smckusick 				    dirbuf.dotdot_name[1] != '.') {
75716776Smckusick 					printf("rename: mangled dir\n");
75816776Smckusick 				} else {
75916776Smckusick 					dirbuf.dotdot_ino = newparent;
76016776Smckusick 					(void) rdwri(UIO_WRITE, xp,
76116776Smckusick 					    (caddr_t)&dirbuf,
76216776Smckusick 					    sizeof (struct dirtemplate),
76337740Smckusick 					    (off_t)0, UIO_SYSSPACE,
76437737Smckusick 					    tndp->ni_cred, (int *)0);
76537737Smckusick 					cache_purge(ITOV(dp));
76616776Smckusick 				}
76716776Smckusick 			}
7689167Ssam 		}
76937737Smckusick 		error = dirremove(fndp);
77037737Smckusick 		if (!error) {
77116776Smckusick 			xp->i_nlink--;
77216776Smckusick 			xp->i_flag |= ICHG;
7739167Ssam 		}
77416776Smckusick 		xp->i_flag &= ~IRENAME;
7759167Ssam 	}
7769167Ssam 	if (dp)
77737737Smckusick 		vput(ITOV(dp));
77816776Smckusick 	if (xp)
77937737Smckusick 		vput(ITOV(xp));
78037737Smckusick 	vrele(ITOV(ip));
78137737Smckusick 	return (error);
7829167Ssam 
7839167Ssam bad:
7849167Ssam 	if (xp)
78537737Smckusick 		vput(ITOV(xp));
78637737Smckusick 	vput(ITOV(dp));
7879167Ssam out:
7889167Ssam 	ip->i_nlink--;
7899167Ssam 	ip->i_flag |= ICHG;
79037737Smckusick 	vrele(ITOV(ip));
79137737Smckusick 	return (error);
7927701Ssam }
7937701Ssam 
7947535Sroot /*
79512756Ssam  * A virgin directory (no blushing please).
79612756Ssam  */
79712756Ssam struct dirtemplate mastertemplate = {
79812756Ssam 	0, 12, 1, ".",
79912756Ssam 	0, DIRBLKSIZ - 12, 2, ".."
80012756Ssam };
80112756Ssam 
80212756Ssam /*
80312756Ssam  * Mkdir system call
80412756Ssam  */
80537737Smckusick ufs_mkdir(ndp, vap)
80637737Smckusick 	struct nameidata *ndp;
80737737Smckusick 	struct vattr *vap;
80812756Ssam {
80912756Ssam 	register struct inode *ip, *dp;
81037737Smckusick 	struct inode *tip;
81137737Smckusick 	struct vnode *dvp;
81212756Ssam 	struct dirtemplate dirtemplate;
81337737Smckusick 	int error;
81437737Smckusick 	int dmode;
81512756Ssam 
81637737Smckusick 	dvp = ndp->ni_dvp;
81737737Smckusick 	dp = VTOI(dvp);
81837737Smckusick 	dmode = vap->va_mode&0777;
81937737Smckusick 	dmode |= IFDIR;
82012756Ssam 	/*
82112756Ssam 	 * Must simulate part of maknode here
82212756Ssam 	 * in order to acquire the inode, but
82312756Ssam 	 * not have it entered in the parent
82412756Ssam 	 * directory.  The entry is made later
82512756Ssam 	 * after writing "." and ".." entries out.
82612756Ssam 	 */
82737737Smckusick 	error = ialloc(dp, dirpref(dp->i_fs), dmode, &tip);
82837737Smckusick 	if (error) {
82912756Ssam 		iput(dp);
83037737Smckusick 		return (error);
83112756Ssam 	}
83237737Smckusick 	ip = tip;
83312756Ssam #ifdef QUOTA
83412756Ssam 	if (ip->i_dquot != NODQUOT)
83512756Ssam 		panic("mkdir: dquot");
83612756Ssam #endif
83712756Ssam 	ip->i_flag |= IACC|IUPD|ICHG;
83837737Smckusick 	ip->i_mode = dmode;
83937737Smckusick 	ITOV(ip)->v_type = VDIR;	/* Rest init'd in iget() */
84012756Ssam 	ip->i_nlink = 2;
84137737Smckusick 	ip->i_uid = ndp->ni_cred->cr_uid;
84212756Ssam 	ip->i_gid = dp->i_gid;
84312756Ssam #ifdef QUOTA
84412756Ssam 	ip->i_dquot = inoquota(ip);
84512756Ssam #endif
84637737Smckusick 	error = iupdat(ip, &time, &time, 1);
84712756Ssam 
84812756Ssam 	/*
84912756Ssam 	 * Bump link count in parent directory
85012756Ssam 	 * to reflect work done below.  Should
85112756Ssam 	 * be done before reference is created
85212756Ssam 	 * so reparation is possible if we crash.
85312756Ssam 	 */
85412756Ssam 	dp->i_nlink++;
85512756Ssam 	dp->i_flag |= ICHG;
85637737Smckusick 	error = iupdat(dp, &time, &time, 1);
85712756Ssam 
85812756Ssam 	/*
85912756Ssam 	 * Initialize directory with "."
86012756Ssam 	 * and ".." from static template.
86112756Ssam 	 */
86212756Ssam 	dirtemplate = mastertemplate;
86312756Ssam 	dirtemplate.dot_ino = ip->i_number;
86412756Ssam 	dirtemplate.dotdot_ino = dp->i_number;
86537737Smckusick 	error = rdwri(UIO_WRITE, ip, (caddr_t)&dirtemplate,
86637737Smckusick 		sizeof (dirtemplate), (off_t)0, UIO_SYSSPACE,
86737737Smckusick 		ndp->ni_cred, (int *)0);
86837737Smckusick 	if (error) {
86912756Ssam 		dp->i_nlink--;
87012756Ssam 		dp->i_flag |= ICHG;
87112756Ssam 		goto bad;
87212756Ssam 	}
87337737Smckusick 	if (DIRBLKSIZ > dp->i_fs->fs_fsize)
87437737Smckusick 		panic("mkdir: blksize");     /* XXX - should grow w/balloc() */
87518103Smckusick 	else
87618103Smckusick 		ip->i_size = DIRBLKSIZ;
87712756Ssam 	/*
87812756Ssam 	 * Directory all set up, now
87912756Ssam 	 * install the entry for it in
88012756Ssam 	 * the parent directory.
88112756Ssam 	 */
88237737Smckusick 	error = direnter(ip, ndp);
88312756Ssam 	dp = NULL;
88437737Smckusick 	if (error) {
88516694Smckusick 		ndp->ni_nameiop = LOOKUP | NOCACHE;
88637737Smckusick 		error = namei(ndp);
88737737Smckusick 		if (!error) {
88837737Smckusick 			dp = VTOI(ndp->ni_vp);
88912756Ssam 			dp->i_nlink--;
89012756Ssam 			dp->i_flag |= ICHG;
89112756Ssam 		}
89212756Ssam 	}
89312756Ssam bad:
89412756Ssam 	/*
89512756Ssam 	 * No need to do an explicit itrunc here,
89637737Smckusick 	 * vrele will do this for us because we set
89712756Ssam 	 * the link count to 0.
89812756Ssam 	 */
89937737Smckusick 	if (error) {
90012756Ssam 		ip->i_nlink = 0;
90112756Ssam 		ip->i_flag |= ICHG;
90238144Smckusick 		iput(ip);
90338144Smckusick 	} else
90438144Smckusick 		ndp->ni_vp = ITOV(ip);
90512756Ssam 	if (dp)
90612756Ssam 		iput(dp);
90737737Smckusick 	return (error);
90812756Ssam }
90912756Ssam 
91012756Ssam /*
91112756Ssam  * Rmdir system call.
91212756Ssam  */
91337737Smckusick ufs_rmdir(ndp)
91437737Smckusick 	register struct nameidata *ndp;
91512756Ssam {
91612756Ssam 	register struct inode *ip, *dp;
91737737Smckusick 	int error = 0;
91812756Ssam 
91937737Smckusick 	ip = VTOI(ndp->ni_vp);
92037737Smckusick 	dp = VTOI(ndp->ni_dvp);
92112756Ssam 	/*
92212756Ssam 	 * No rmdir "." please.
92312756Ssam 	 */
92412756Ssam 	if (dp == ip) {
92537737Smckusick 		vrele(ITOV(dp));
92612756Ssam 		iput(ip);
92737737Smckusick 		return (EINVAL);
92812756Ssam 	}
92912756Ssam 	/*
93012756Ssam 	 * Verify the directory is empty (and valid).
93112756Ssam 	 * (Rmdir ".." won't be valid since
93212756Ssam 	 *  ".." will contain a reference to
93312756Ssam 	 *  the current directory and thus be
93412756Ssam 	 *  non-empty.)
93512756Ssam 	 */
93637737Smckusick 	if (ip->i_nlink != 2 || !dirempty(ip, dp->i_number, ndp->ni_cred)) {
93737737Smckusick 		error = ENOTEMPTY;
93812756Ssam 		goto out;
93912756Ssam 	}
94012756Ssam 	/*
94112756Ssam 	 * Delete reference to directory before purging
94212756Ssam 	 * inode.  If we crash in between, the directory
94312756Ssam 	 * will be reattached to lost+found,
94412756Ssam 	 */
94537737Smckusick 	if (error = dirremove(ndp))
94612756Ssam 		goto out;
94712756Ssam 	dp->i_nlink--;
94812756Ssam 	dp->i_flag |= ICHG;
94937737Smckusick 	cache_purge(ITOV(dp));
95012756Ssam 	iput(dp);
95137737Smckusick 	ndp->ni_dvp = NULL;
95212756Ssam 	/*
95312756Ssam 	 * Truncate inode.  The only stuff left
95412756Ssam 	 * in the directory is "." and "..".  The
95512756Ssam 	 * "." reference is inconsequential since
95612756Ssam 	 * we're quashing it.  The ".." reference
95712756Ssam 	 * has already been adjusted above.  We've
95812756Ssam 	 * removed the "." reference and the reference
95912756Ssam 	 * in the parent directory, but there may be
96012756Ssam 	 * other hard links so decrement by 2 and
96112756Ssam 	 * worry about them later.
96212756Ssam 	 */
96312756Ssam 	ip->i_nlink -= 2;
96437737Smckusick 	error = itrunc(ip, (u_long)0);
96537737Smckusick 	cache_purge(ITOV(ip));
96612756Ssam out:
96737737Smckusick 	if (ndp->ni_dvp)
96812756Ssam 		iput(dp);
96912756Ssam 	iput(ip);
97037737Smckusick 	return (error);
97112756Ssam }
97212756Ssam 
97337737Smckusick /*
97437737Smckusick  * symlink -- make a symbolic link
97537737Smckusick  */
97637737Smckusick ufs_symlink(ndp, vap, target)
97737737Smckusick 	struct nameidata *ndp;
97837737Smckusick 	struct vattr *vap;
97937737Smckusick 	char *target;
98012756Ssam {
98137737Smckusick 	struct inode *ip;
98237737Smckusick 	int error;
98312756Ssam 
98437737Smckusick 	error = maknode(IFLNK | vap->va_mode, ndp, &ip);
98537737Smckusick 	if (error)
98637737Smckusick 		return (error);
98737737Smckusick 	error = rdwri(UIO_WRITE, ip, target, strlen(target), (off_t)0,
98837737Smckusick 		UIO_SYSSPACE, ndp->ni_cred, (int *)0);
98937737Smckusick 	iput(ip);
99037737Smckusick 	return (error);
99137737Smckusick }
99237737Smckusick 
99337737Smckusick /*
99437737Smckusick  * Vnode op for read and write
99537737Smckusick  */
99637737Smckusick ufs_readdir(vp, uio, offp, cred)
99737737Smckusick 	struct vnode *vp;
99837737Smckusick 	register struct uio *uio;
99937737Smckusick 	off_t *offp;
100037737Smckusick 	struct ucred *cred;
100137737Smckusick {
100237737Smckusick 	register struct inode *ip = VTOI(vp);
100337737Smckusick 	int count, error;
100437737Smckusick 
100537737Smckusick 	ILOCK(ip);
100637737Smckusick 	uio->uio_offset = *offp;
100737737Smckusick 	count = uio->uio_resid;
100837737Smckusick 	count &= ~(DIRBLKSIZ - 1);
100937737Smckusick 	if (vp->v_type != VDIR || uio->uio_iovcnt != 1 ||
101037737Smckusick 	    (count < DIRBLKSIZ) || (uio->uio_offset & (DIRBLKSIZ -1))) {
101137737Smckusick 		IUNLOCK(ip);
101237737Smckusick 		return (EINVAL);
101316540Ssam 	}
101437737Smckusick 	uio->uio_resid = count;
101537737Smckusick 	uio->uio_iov->iov_len = count;
101637737Smckusick 	error = readip(ip, uio, cred);
101737737Smckusick 	*offp += count - uio->uio_resid;
101837737Smckusick 	IUNLOCK(ip);
101937737Smckusick 	return (error);
102037737Smckusick }
102137737Smckusick 
102237737Smckusick /*
102337737Smckusick  * Return target name of a symbolic link
102437737Smckusick  */
102537737Smckusick ufs_readlink(vp, uiop, cred)
102637737Smckusick 	struct vnode *vp;
102737737Smckusick 	struct uio *uiop;
102837737Smckusick 	struct ucred *cred;
102937737Smckusick {
103037737Smckusick 
103137737Smckusick 	return (readip(VTOI(vp), uiop, cred));
103237737Smckusick }
103337737Smckusick 
103437737Smckusick /*
103537737Smckusick  * Ufs abort op, called after namei() when a CREATE/DELETE isn't actually
103637737Smckusick  * done. Iff ni_vp/ni_dvp not null and locked, unlock.
103737737Smckusick  */
103837737Smckusick ufs_abortop(ndp)
103937737Smckusick 	register struct nameidata *ndp;
104037737Smckusick {
104137737Smckusick 	register struct inode *ip;
104237737Smckusick 
104337737Smckusick 	if (ndp->ni_vp) {
104437737Smckusick 		ip = VTOI(ndp->ni_vp);
104537737Smckusick 		if (ip->i_flag & ILOCKED)
104637737Smckusick 			IUNLOCK(ip);
104737737Smckusick 		vrele(ndp->ni_vp);
104812756Ssam 	}
104937737Smckusick 	if (ndp->ni_dvp) {
105037737Smckusick 		ip = VTOI(ndp->ni_dvp);
105137737Smckusick 		if (ip->i_flag & ILOCKED)
105237737Smckusick 			IUNLOCK(ip);
105337737Smckusick 		vrele(ndp->ni_dvp);
105437737Smckusick 	}
105537737Smckusick 	return;
105612756Ssam }
105712756Ssam 
105837737Smckusick ufs_lock(vp)
105937737Smckusick 	struct vnode *vp;
106037737Smckusick {
106137737Smckusick 	register struct inode *ip = VTOI(vp);
106237737Smckusick 
106337737Smckusick 	ILOCK(ip);
106437737Smckusick 	return (0);
106537737Smckusick }
106637737Smckusick 
106737737Smckusick ufs_unlock(vp)
106837737Smckusick 	struct vnode *vp;
106937737Smckusick {
107037737Smckusick 	register struct inode *ip = VTOI(vp);
107137737Smckusick 
107237737Smckusick 	if (!(ip->i_flag & ILOCKED))
107337737Smckusick 		panic("ufs_unlock NOT LOCKED");
107437737Smckusick 	IUNLOCK(ip);
107537737Smckusick 	return (0);
107637737Smckusick }
107737737Smckusick 
107812756Ssam /*
107937737Smckusick  * Get access to bmap
108012756Ssam  */
108137737Smckusick ufs_bmap(vp, bn, vpp, bnp)
108237737Smckusick 	struct vnode *vp;
108337737Smckusick 	daddr_t bn;
108437737Smckusick 	struct vnode **vpp;
108537737Smckusick 	daddr_t *bnp;
108612756Ssam {
108737737Smckusick 	struct inode *ip = VTOI(vp);
108812756Ssam 
108937737Smckusick 	if (vpp != NULL)
109037737Smckusick 		*vpp = ip->i_devvp;
109137737Smckusick 	if (bnp == NULL)
109237737Smckusick 		return (0);
109337737Smckusick 	return (bmap(ip, bn, bnp, (daddr_t *)0, (int *)0));
109412756Ssam }
109537737Smckusick 
109637737Smckusick /*
109737737Smckusick  * Just call the device strategy routine
109837737Smckusick  */
109937737Smckusick ufs_strategy(bp)
110037737Smckusick 	register struct buf *bp;
110137737Smckusick {
110237737Smckusick 	(*bdevsw[major(bp->b_dev)].d_strategy)(bp);
110337737Smckusick 	return (0);
110437737Smckusick }
110537737Smckusick 
110637737Smckusick /*
110737737Smckusick  * Make a new file.
110837737Smckusick  */
110937737Smckusick maknode(mode, ndp, ipp)
111037737Smckusick 	int mode;
111137737Smckusick 	register struct nameidata *ndp;
111237737Smckusick 	struct inode **ipp;
111337737Smckusick {
111437737Smckusick 	register struct inode *ip;
111537737Smckusick 	struct inode *tip;
111637737Smckusick 	register struct inode *pdir = VTOI(ndp->ni_dvp);
111737737Smckusick 	ino_t ipref;
111837737Smckusick 	int error;
111937737Smckusick 
112037737Smckusick 	*ipp = 0;
112137737Smckusick 	if ((mode & IFMT) == IFDIR)
112237737Smckusick 		ipref = dirpref(pdir->i_fs);
112337737Smckusick 	else
112437737Smckusick 		ipref = pdir->i_number;
112537737Smckusick 	error = ialloc(pdir, ipref, mode, &tip);
112637737Smckusick 	if (error) {
112737737Smckusick 		iput(pdir);
112837737Smckusick 		return (error);
112937737Smckusick 	}
113037737Smckusick 	ip = tip;
113137737Smckusick #ifdef QUOTA
113237737Smckusick 	if (ip->i_dquot != NODQUOT)
113337737Smckusick 		panic("maknode: dquot");
113437737Smckusick #endif
113537737Smckusick 	ip->i_flag |= IACC|IUPD|ICHG;
113637737Smckusick 	if ((mode & IFMT) == 0)
113737737Smckusick 		mode |= IFREG;
113837737Smckusick 	ip->i_mode = mode;
113937737Smckusick 	ITOV(ip)->v_type = IFTOVT(mode);	/* Rest init'd in iget() */
114037737Smckusick 	ip->i_nlink = 1;
114137737Smckusick 	ip->i_uid = ndp->ni_cred->cr_uid;
114237737Smckusick 	ip->i_gid = pdir->i_gid;
114337737Smckusick 	if ((ip->i_mode & ISGID) && !groupmember(ip->i_gid, ndp->ni_cred) &&
114437737Smckusick 	    suser(ndp->ni_cred, NULL))
114537737Smckusick 		ip->i_mode &= ~ISGID;
114637737Smckusick #ifdef QUOTA
114737737Smckusick 	ip->i_dquot = inoquota(ip);
114837737Smckusick #endif
114937737Smckusick 
115037737Smckusick 	/*
115137737Smckusick 	 * Make sure inode goes to disk before directory entry.
115237737Smckusick 	 */
115337737Smckusick 	if ((error = iupdat(ip, &time, &time, 1)) ||
115437737Smckusick 	    (error = direnter(ip, ndp))) {
115537737Smckusick 		/*
115637737Smckusick 		 * Write error occurred trying to update the inode
115737737Smckusick 		 * or the directory so must deallocate the inode.
115837737Smckusick 		 */
115937737Smckusick 		ip->i_nlink = 0;
116037737Smckusick 		ip->i_flag |= ICHG;
116137737Smckusick 		iput(ip);
116237737Smckusick 		return (error);
116337737Smckusick 	}
116437737Smckusick 	*ipp = ip;
116537737Smckusick 	return (0);
116637737Smckusick }
1167