xref: /csrg-svn/sys/ufs/lfs/lfs_vnops.c (revision 38771)
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*38771Smckusick  *	@(#)lfs_vnops.c	7.16 (Berkeley) 08/26/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;
22538578Smckusick 	vap->va_atime.tv_usec = 0;
22637737Smckusick 	vap->va_mtime.tv_sec = ip->i_mtime;
22738578Smckusick 	vap->va_mtime.tv_usec = 0;
22837737Smckusick 	vap->va_ctime.tv_sec = ip->i_ctime;
22938578Smckusick 	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;
23938657Smckusick 	vap->va_bytes = dbtob(ip->i_blocks);
24037737Smckusick 	vap->va_bytes1 = -1;
24137737Smckusick 	vap->va_type = vp->v_type;
24237737Smckusick 	return (0);
2436254Sroot }
2446254Sroot 
2456254Sroot /*
24637737Smckusick  * Set attribute vnode op. called from several syscalls
2476254Sroot  */
24837737Smckusick ufs_setattr(vp, vap, cred)
24937737Smckusick 	register struct vnode *vp;
25037737Smckusick 	register struct vattr *vap;
25137737Smckusick 	register struct ucred *cred;
2526254Sroot {
25337737Smckusick 	register struct inode *ip = VTOI(vp);
25437737Smckusick 	int error = 0;
2556254Sroot 
25637737Smckusick 	/*
25737737Smckusick 	 * Check for unsetable attributes.
25837737Smckusick 	 */
25937737Smckusick 	if ((vap->va_type != VNON) || (vap->va_nlink != VNOVAL) ||
26037737Smckusick 	    (vap->va_fsid != VNOVAL) || (vap->va_fileid != VNOVAL) ||
26137737Smckusick 	    (vap->va_blocksize != VNOVAL) || (vap->va_rdev != VNOVAL) ||
26238254Smckusick 	    ((int)vap->va_bytes != VNOVAL) || (vap->va_gen != VNOVAL)) {
26337737Smckusick 		return (EINVAL);
26416540Ssam 	}
26537737Smckusick 	/*
26637737Smckusick 	 * Go through the fields and update iff not VNOVAL.
26737737Smckusick 	 */
26837737Smckusick 	if (vap->va_uid != (u_short)VNOVAL || vap->va_gid != (u_short)VNOVAL)
26937737Smckusick 		if (error = chown1(vp, vap->va_uid, vap->va_gid, cred))
27037737Smckusick 			return (error);
27137737Smckusick 	if (vap->va_size != VNOVAL) {
27237737Smckusick 		if (vp->v_type == VDIR)
27337737Smckusick 			return (EISDIR);
27437737Smckusick 		if (error = itrunc(ip, vap->va_size))
27537737Smckusick 			return (error);
27613878Ssam 	}
27737737Smckusick 	if (vap->va_atime.tv_sec != VNOVAL || vap->va_mtime.tv_sec != VNOVAL) {
27837773Smckusick 		if (cred->cr_uid != ip->i_uid &&
27937773Smckusick 		    (error = suser(cred, &u.u_acflag)))
28037773Smckusick 			return (error);
28137737Smckusick 		if (vap->va_atime.tv_sec != VNOVAL)
28237737Smckusick 			ip->i_flag |= IACC;
28337737Smckusick 		if (vap->va_mtime.tv_sec != VNOVAL)
28437737Smckusick 			ip->i_flag |= IUPD;
28537737Smckusick 		ip->i_flag |= ICHG;
28637737Smckusick 		if (error = iupdat(ip, &vap->va_atime, &vap->va_mtime, 1))
28737737Smckusick 			return (error);
2886254Sroot 	}
28937737Smckusick 	if (vap->va_mode != (u_short)VNOVAL)
29037737Smckusick 		error = chmod1(vp, (int)vap->va_mode, cred);
29138254Smckusick 	if (vap->va_flags != VNOVAL) {
29238254Smckusick 		if (cred->cr_uid != ip->i_uid &&
29338254Smckusick 		    (error = suser(cred, &u.u_acflag)))
29438254Smckusick 			return (error);
29538254Smckusick 		if (cred->cr_uid == 0) {
29638254Smckusick 			ip->i_flags = vap->va_flags;
29738254Smckusick 		} else {
29838254Smckusick 			ip->i_flags &= 0xffff0000;
29938254Smckusick 			ip->i_flags |= (vap->va_flags & 0xffff);
30038254Smckusick 		}
30138254Smckusick 		ip->i_flag |= ICHG;
30238254Smckusick 	}
30337737Smckusick 	return (error);
3046254Sroot }
3056254Sroot 
3066254Sroot /*
3079167Ssam  * Change the mode on a file.
3089167Ssam  * Inode must be locked before calling.
3099167Ssam  */
31037737Smckusick chmod1(vp, mode, cred)
31137737Smckusick 	register struct vnode *vp;
3127701Ssam 	register int mode;
31337737Smckusick 	struct ucred *cred;
3147701Ssam {
31537737Smckusick 	register struct inode *ip = VTOI(vp);
31637773Smckusick 	int error;
3177868Sroot 
31837773Smckusick 	if (cred->cr_uid != ip->i_uid &&
31937773Smckusick 	    (error = suser(cred, &u.u_acflag)))
32037773Smckusick 		return (error);
3216254Sroot 	ip->i_mode &= ~07777;
32237737Smckusick 	if (cred->cr_uid) {
32337737Smckusick 		if (vp->v_type != VDIR)
32421015Smckusick 			mode &= ~ISVTX;
32537737Smckusick 		if (!groupmember(ip->i_gid, cred))
32611811Ssam 			mode &= ~ISGID;
3277439Sroot 	}
32837737Smckusick 	ip->i_mode |= mode & 07777;
3296254Sroot 	ip->i_flag |= ICHG;
33037737Smckusick 	if ((vp->v_flag & VTEXT) && (ip->i_mode & ISVTX) == 0)
33137737Smckusick 		xrele(vp);
33221015Smckusick 	return (0);
3335992Swnj }
3345992Swnj 
3359167Ssam /*
3367701Ssam  * Perform chown operation on inode ip;
3377701Ssam  * inode must be locked prior to call.
3387701Ssam  */
33937737Smckusick chown1(vp, uid, gid, cred)
34037737Smckusick 	register struct vnode *vp;
34137737Smckusick 	uid_t uid;
34237737Smckusick 	gid_t gid;
34337737Smckusick 	struct ucred *cred;
3447701Ssam {
34537737Smckusick 	register struct inode *ip = VTOI(vp);
3467701Ssam #ifdef QUOTA
3477701Ssam 	register long change;
34811811Ssam #endif
34937737Smckusick 	int error;
3507701Ssam 
35137737Smckusick 	if (uid == (u_short)VNOVAL)
35211811Ssam 		uid = ip->i_uid;
35337737Smckusick 	if (gid == (u_short)VNOVAL)
35411811Ssam 		gid = ip->i_gid;
35536614Sbostic 	/*
35636614Sbostic 	 * If we don't own the file, are trying to change the owner
35736614Sbostic 	 * of the file, or are not a member of the target group,
35836614Sbostic 	 * the caller must be superuser or the call fails.
35936614Sbostic 	 */
36037737Smckusick 	if ((cred->cr_uid != ip->i_uid || uid != ip->i_uid ||
36137737Smckusick 	    !groupmember((gid_t)gid, cred)) &&
36237737Smckusick 	    (error = suser(cred, &u.u_acflag)))
36337737Smckusick 		return (error);
36411811Ssam #ifdef QUOTA
36514385Ssam 	if (ip->i_uid == uid)		/* this just speeds things a little */
3667482Skre 		change = 0;
36712646Ssam 	else
36812646Ssam 		change = ip->i_blocks;
36912646Ssam 	(void) chkdq(ip, -change, 1);
37012646Ssam 	(void) chkiq(ip->i_dev, ip, ip->i_uid, 1);
3717482Skre 	dqrele(ip->i_dquot);
3727482Skre #endif
37311811Ssam 	ip->i_uid = uid;
37411811Ssam 	ip->i_gid = gid;
3756254Sroot 	ip->i_flag |= ICHG;
376*38771Smckusick 	ip->i_mode &= ~(ISUID|ISGID);
3777701Ssam #ifdef QUOTA
3787482Skre 	ip->i_dquot = inoquota(ip);
37912646Ssam 	(void) chkdq(ip, change, 1);
38026361Skarels 	(void) chkiq(ip->i_dev, (struct inode *)NULL, (uid_t)uid, 1);
38112646Ssam 	return (u.u_error);		/* should == 0 ALWAYS !! */
38212646Ssam #else
38312646Ssam 	return (0);
3847482Skre #endif
38537Sbill }
38637Sbill 
38737737Smckusick /* ARGSUSED */
38837737Smckusick ufs_ioctl(vp, com, data, fflag, cred)
38937737Smckusick 	struct vnode *vp;
39037737Smckusick 	int com;
39137737Smckusick 	caddr_t data;
39237737Smckusick 	int fflag;
39337737Smckusick 	struct ucred *cred;
39411811Ssam {
39511811Ssam 
39637737Smckusick 	printf("ufs_ioctl called with type %d\n", vp->v_type);
39737737Smckusick 	return (ENOTTY);
39811811Ssam }
39911811Ssam 
40037737Smckusick /* ARGSUSED */
40137737Smckusick ufs_select(vp, which, cred)
40237737Smckusick 	struct vnode *vp;
40337737Smckusick 	int which;
40437737Smckusick 	struct ucred *cred;
40537737Smckusick {
40637737Smckusick 
40737737Smckusick 	printf("ufs_select called with type %d\n", vp->v_type);
40837737Smckusick 	return (1);		/* XXX */
40937737Smckusick }
41037737Smckusick 
4119167Ssam /*
41237737Smckusick  * Mmap a file
41337737Smckusick  *
41437737Smckusick  * NB Currently unsupported.
4159167Ssam  */
41637737Smckusick /* ARGSUSED */
41737737Smckusick ufs_mmap(vp, fflags, cred)
41837737Smckusick 	struct vnode *vp;
41937737Smckusick 	int fflags;
42037737Smckusick 	struct ucred *cred;
42137Sbill {
42237Sbill 
42337737Smckusick 	return (EINVAL);
42437Sbill }
4257535Sroot 
4269167Ssam /*
42737737Smckusick  * Synch an open file.
4289167Ssam  */
42937737Smckusick /* ARGSUSED */
43037737Smckusick ufs_fsync(vp, fflags, cred)
43137737Smckusick 	struct vnode *vp;
43237737Smckusick 	int fflags;
43337737Smckusick 	struct ucred *cred;
4347701Ssam {
43537737Smckusick 	register struct inode *ip = VTOI(vp);
43637737Smckusick 	int error;
4377701Ssam 
43837737Smckusick 	ILOCK(ip);
43937737Smckusick 	if (fflags&FWRITE)
44037737Smckusick 		ip->i_flag |= ICHG;
44137737Smckusick 	error = syncip(ip);
44237737Smckusick 	IUNLOCK(ip);
44337737Smckusick 	return (error);
4447701Ssam }
4457701Ssam 
4469167Ssam /*
44737737Smckusick  * Seek on a file
44837737Smckusick  *
44937737Smckusick  * Nothing to do, so just return.
4509167Ssam  */
45137737Smckusick /* ARGSUSED */
45237737Smckusick ufs_seek(vp, oldoff, newoff, cred)
45337737Smckusick 	struct vnode *vp;
45437737Smckusick 	off_t oldoff, newoff;
45537737Smckusick 	struct ucred *cred;
4567701Ssam {
4577701Ssam 
45837737Smckusick 	return (0);
45937737Smckusick }
46037737Smckusick 
46137737Smckusick /*
46237737Smckusick  * ufs remove
46337737Smckusick  * Hard to avoid races here, especially
46437737Smckusick  * in unlinking directories.
46537737Smckusick  */
46637737Smckusick ufs_remove(ndp)
46737737Smckusick 	struct nameidata *ndp;
46837737Smckusick {
46937737Smckusick 	register struct inode *ip, *dp;
47037737Smckusick 	int error;
47137737Smckusick 
47237737Smckusick 	ip = VTOI(ndp->ni_vp);
47337737Smckusick 	dp = VTOI(ndp->ni_dvp);
47437737Smckusick 	error = dirremove(ndp);
47537737Smckusick 	if (!error) {
47637737Smckusick 		ip->i_nlink--;
47737737Smckusick 		ip->i_flag |= ICHG;
4787701Ssam 	}
47937737Smckusick 	if (dp == ip)
48037737Smckusick 		vrele(ITOV(ip));
48137737Smckusick 	else
48237737Smckusick 		iput(ip);
48337737Smckusick 	iput(dp);
48437737Smckusick 	return (error);
4857701Ssam }
4867701Ssam 
4879167Ssam /*
48837737Smckusick  * link vnode call
4899167Ssam  */
49037737Smckusick ufs_link(vp, ndp)
49137737Smckusick 	register struct vnode *vp;
49237737Smckusick 	register struct nameidata *ndp;
4939167Ssam {
49437737Smckusick 	register struct inode *ip = VTOI(vp);
49537737Smckusick 	int error;
4969167Ssam 
49737737Smckusick 	if (ndp->ni_dvp != vp)
49837737Smckusick 		ILOCK(ip);
49937737Smckusick 	if (ip->i_nlink == LINK_MAX - 1) {
50037737Smckusick 		error = EMLINK;
50137737Smckusick 		goto out;
50237737Smckusick 	}
50337737Smckusick 	ip->i_nlink++;
50437737Smckusick 	ip->i_flag |= ICHG;
50537737Smckusick 	error = iupdat(ip, &time, &time, 1);
50637737Smckusick 	if (!error)
50737737Smckusick 		error = direnter(ip, ndp);
50837737Smckusick out:
50937737Smckusick 	if (ndp->ni_dvp != vp)
51037737Smckusick 		IUNLOCK(ip);
51137737Smckusick 	if (error) {
51237737Smckusick 		ip->i_nlink--;
51330598Smckusick 		ip->i_flag |= ICHG;
51437737Smckusick 	}
51537737Smckusick 	return (error);
5169167Ssam }
5179167Ssam 
5189167Ssam /*
5199167Ssam  * Rename system call.
5209167Ssam  * 	rename("foo", "bar");
5219167Ssam  * is essentially
5229167Ssam  *	unlink("bar");
5239167Ssam  *	link("foo", "bar");
5249167Ssam  *	unlink("foo");
5259167Ssam  * but ``atomically''.  Can't do full commit without saving state in the
5269167Ssam  * inode on disk which isn't feasible at this time.  Best we can do is
5279167Ssam  * always guarantee the target exists.
5289167Ssam  *
5299167Ssam  * Basic algorithm is:
5309167Ssam  *
5319167Ssam  * 1) Bump link count on source while we're linking it to the
53237737Smckusick  *    target.  This also ensure the inode won't be deleted out
53316776Smckusick  *    from underneath us while we work (it may be truncated by
53416776Smckusick  *    a concurrent `trunc' or `open' for creation).
5359167Ssam  * 2) Link source to destination.  If destination already exists,
5369167Ssam  *    delete it first.
53716776Smckusick  * 3) Unlink source reference to inode if still around. If a
53816776Smckusick  *    directory was moved and the parent of the destination
5399167Ssam  *    is different from the source, patch the ".." entry in the
5409167Ssam  *    directory.
5419167Ssam  */
54237737Smckusick ufs_rename(fndp, tndp)
54337737Smckusick 	register struct nameidata *fndp, *tndp;
5447701Ssam {
5459167Ssam 	register struct inode *ip, *xp, *dp;
54616776Smckusick 	struct dirtemplate dirbuf;
54716776Smckusick 	int doingdirectory = 0, oldparent = 0, newparent = 0;
54810051Ssam 	int error = 0;
5497701Ssam 
55037737Smckusick 	dp = VTOI(fndp->ni_dvp);
55137737Smckusick 	ip = VTOI(fndp->ni_vp);
55237737Smckusick 	ILOCK(ip);
5539167Ssam 	if ((ip->i_mode&IFMT) == IFDIR) {
55437737Smckusick 		register struct direct *d = &fndp->ni_dent;
5559167Ssam 
5569167Ssam 		/*
55711641Ssam 		 * Avoid ".", "..", and aliases of "." for obvious reasons.
5589167Ssam 		 */
55937737Smckusick 		if ((d->d_namlen == 1 && d->d_name[0] == '.') || dp == ip ||
56037737Smckusick 		    fndp->ni_isdotdot || (ip->i_flag & IRENAME)) {
56137737Smckusick 			IUNLOCK(ip);
56237737Smckusick 			ufs_abortop(fndp);
56337737Smckusick 			ufs_abortop(tndp);
56437737Smckusick 			return (EINVAL);
5659167Ssam 		}
56616776Smckusick 		ip->i_flag |= IRENAME;
5679167Ssam 		oldparent = dp->i_number;
5689167Ssam 		doingdirectory++;
5699167Ssam 	}
57037737Smckusick 	vrele(fndp->ni_dvp);
5719167Ssam 
5729167Ssam 	/*
5739167Ssam 	 * 1) Bump link count while we're moving stuff
5749167Ssam 	 *    around.  If we crash somewhere before
5759167Ssam 	 *    completing our work, the link count
5769167Ssam 	 *    may be wrong, but correctable.
5779167Ssam 	 */
5789167Ssam 	ip->i_nlink++;
5799167Ssam 	ip->i_flag |= ICHG;
58037737Smckusick 	error = iupdat(ip, &time, &time, 1);
58116664Smckusick 	IUNLOCK(ip);
5829167Ssam 
5839167Ssam 	/*
5849167Ssam 	 * When the target exists, both the directory
58537737Smckusick 	 * and target vnodes are returned locked.
5869167Ssam 	 */
58737737Smckusick 	dp = VTOI(tndp->ni_dvp);
58837737Smckusick 	xp = NULL;
58937737Smckusick 	if (tndp->ni_vp)
59037737Smckusick 		xp = VTOI(tndp->ni_vp);
5919167Ssam 	/*
59211641Ssam 	 * If ".." must be changed (ie the directory gets a new
59312816Smckusick 	 * parent) then the source directory must not be in the
59412816Smckusick 	 * directory heirarchy above the target, as this would
59512816Smckusick 	 * orphan everything below the source directory. Also
59612816Smckusick 	 * the user must have write permission in the source so
59712816Smckusick 	 * as to be able to change "..". We must repeat the call
59812816Smckusick 	 * to namei, as the parent directory is unlocked by the
59912816Smckusick 	 * call to checkpath().
60011641Ssam 	 */
60116776Smckusick 	if (oldparent != dp->i_number)
60216776Smckusick 		newparent = dp->i_number;
60316776Smckusick 	if (doingdirectory && newparent) {
60437737Smckusick 		if (error = iaccess(ip, IWRITE, tndp->ni_cred))
60512816Smckusick 			goto bad;
60637737Smckusick 		tndp->ni_nameiop = RENAME | LOCKPARENT | LOCKLEAF | NOCACHE;
60712816Smckusick 		do {
60837737Smckusick 			dp = VTOI(tndp->ni_dvp);
60912816Smckusick 			if (xp != NULL)
61038069Smckusick 				iput(xp);
61137737Smckusick 			if (error = checkpath(ip, dp, tndp->ni_cred))
61212816Smckusick 				goto out;
61337737Smckusick 			if (error = namei(tndp))
61412816Smckusick 				goto out;
61537737Smckusick 			xp = NULL;
61637737Smckusick 			if (tndp->ni_vp)
61737737Smckusick 				xp = VTOI(tndp->ni_vp);
61837737Smckusick 		} while (dp != VTOI(tndp->ni_dvp));
61912816Smckusick 	}
62011641Ssam 	/*
6219167Ssam 	 * 2) If target doesn't exist, link the target
6229167Ssam 	 *    to the source and unlink the source.
6239167Ssam 	 *    Otherwise, rewrite the target directory
6249167Ssam 	 *    entry to reference the source inode and
6259167Ssam 	 *    expunge the original entry's existence.
6269167Ssam 	 */
6279167Ssam 	if (xp == NULL) {
62837737Smckusick 		if (dp->i_dev != ip->i_dev)
62937737Smckusick 			panic("rename: EXDEV");
6309167Ssam 		/*
63116776Smckusick 		 * Account for ".." in new directory.
63216776Smckusick 		 * When source and destination have the same
63316776Smckusick 		 * parent we don't fool with the link count.
6349167Ssam 		 */
63516776Smckusick 		if (doingdirectory && newparent) {
6369167Ssam 			dp->i_nlink++;
6379167Ssam 			dp->i_flag |= ICHG;
63837737Smckusick 			error = iupdat(dp, &time, &time, 1);
6399167Ssam 		}
64037737Smckusick 		if (error = direnter(ip, tndp))
6419167Ssam 			goto out;
6429167Ssam 	} else {
64337737Smckusick 		if (xp->i_dev != dp->i_dev || xp->i_dev != ip->i_dev)
64437737Smckusick 			panic("rename: EXDEV");
6459167Ssam 		/*
64610590Ssam 		 * Short circuit rename(foo, foo).
64710590Ssam 		 */
64810590Ssam 		if (xp->i_number == ip->i_number)
64937737Smckusick 			panic("rename: same file");
65010590Ssam 		/*
65124433Sbloom 		 * If the parent directory is "sticky", then the user must
65224433Sbloom 		 * own the parent directory, or the destination of the rename,
65324433Sbloom 		 * otherwise the destination may not be changed (except by
65424433Sbloom 		 * root). This implements append-only directories.
65524433Sbloom 		 */
65637737Smckusick 		if ((dp->i_mode & ISVTX) && tndp->ni_cred->cr_uid != 0 &&
65737737Smckusick 		    tndp->ni_cred->cr_uid != dp->i_uid &&
65837737Smckusick 		    xp->i_uid != tndp->ni_cred->cr_uid) {
65924433Sbloom 			error = EPERM;
66024433Sbloom 			goto bad;
66124433Sbloom 		}
66224433Sbloom 		/*
66310051Ssam 		 * Target must be empty if a directory
66410051Ssam 		 * and have no links to it.
6659167Ssam 		 * Also, insure source and target are
6669167Ssam 		 * compatible (both directories, or both
6679167Ssam 		 * not directories).
6689167Ssam 		 */
6699167Ssam 		if ((xp->i_mode&IFMT) == IFDIR) {
67037737Smckusick 			if (!dirempty(xp, dp->i_number, tndp->ni_cred) ||
67137737Smckusick 			    xp->i_nlink > 2) {
67210051Ssam 				error = ENOTEMPTY;
6739167Ssam 				goto bad;
6749167Ssam 			}
6759167Ssam 			if (!doingdirectory) {
67610051Ssam 				error = ENOTDIR;
6779167Ssam 				goto bad;
6789167Ssam 			}
67937737Smckusick 			cache_purge(ITOV(dp));
6809167Ssam 		} else if (doingdirectory) {
68110051Ssam 			error = EISDIR;
6829167Ssam 			goto bad;
6839167Ssam 		}
68437737Smckusick 		if (error = dirrewrite(dp, ip, tndp))
68537737Smckusick 			goto bad;
68637737Smckusick 		vput(ITOV(dp));
6879167Ssam 		/*
68810051Ssam 		 * Adjust the link count of the target to
68910051Ssam 		 * reflect the dirrewrite above.  If this is
69010051Ssam 		 * a directory it is empty and there are
69110051Ssam 		 * no links to it, so we can squash the inode and
69210051Ssam 		 * any space associated with it.  We disallowed
69310051Ssam 		 * renaming over top of a directory with links to
69416776Smckusick 		 * it above, as the remaining link would point to
69516776Smckusick 		 * a directory without "." or ".." entries.
6969167Ssam 		 */
69710051Ssam 		xp->i_nlink--;
6989167Ssam 		if (doingdirectory) {
69910051Ssam 			if (--xp->i_nlink != 0)
70010051Ssam 				panic("rename: linked directory");
70137737Smckusick 			error = itrunc(xp, (u_long)0);
70210051Ssam 		}
7039167Ssam 		xp->i_flag |= ICHG;
70438398Smckusick 		iput(xp);
70510246Ssam 		xp = NULL;
7069167Ssam 	}
7079167Ssam 
7089167Ssam 	/*
7099167Ssam 	 * 3) Unlink the source.
7109167Ssam 	 */
71137737Smckusick 	fndp->ni_nameiop = DELETE | LOCKPARENT | LOCKLEAF;
71237737Smckusick 	(void)namei(fndp);
71337737Smckusick 	if (fndp->ni_vp != NULL) {
71437737Smckusick 		xp = VTOI(fndp->ni_vp);
71537737Smckusick 		dp = VTOI(fndp->ni_dvp);
71637737Smckusick 	} else {
71738069Smckusick 		if (fndp->ni_dvp != NULL)
71838069Smckusick 			vput(fndp->ni_dvp);
71937737Smckusick 		xp = NULL;
72017758Smckusick 		dp = NULL;
72137737Smckusick 	}
7229167Ssam 	/*
72337737Smckusick 	 * Ensure that the directory entry still exists and has not
72416776Smckusick 	 * changed while the new name has been entered. If the source is
72516776Smckusick 	 * a file then the entry may have been unlinked or renamed. In
72616776Smckusick 	 * either case there is no further work to be done. If the source
72716776Smckusick 	 * is a directory then it cannot have been rmdir'ed; its link
72816776Smckusick 	 * count of three would cause a rmdir to fail with ENOTEMPTY.
72937737Smckusick 	 * The IRENAME flag ensures that it cannot be moved by another
73016776Smckusick 	 * rename.
7319167Ssam 	 */
73217758Smckusick 	if (xp != ip) {
73316776Smckusick 		if (doingdirectory)
73417758Smckusick 			panic("rename: lost dir entry");
73516776Smckusick 	} else {
7369167Ssam 		/*
73716776Smckusick 		 * If the source is a directory with a
73816776Smckusick 		 * new parent, the link count of the old
73916776Smckusick 		 * parent directory must be decremented
74016776Smckusick 		 * and ".." set to point to the new parent.
7419167Ssam 		 */
74216776Smckusick 		if (doingdirectory && newparent) {
7439167Ssam 			dp->i_nlink--;
7449167Ssam 			dp->i_flag |= ICHG;
74516776Smckusick 			error = rdwri(UIO_READ, xp, (caddr_t)&dirbuf,
74637737Smckusick 				sizeof (struct dirtemplate), (off_t)0,
74737740Smckusick 				UIO_SYSSPACE, tndp->ni_cred, (int *)0);
74816776Smckusick 			if (error == 0) {
74916776Smckusick 				if (dirbuf.dotdot_namlen != 2 ||
75016776Smckusick 				    dirbuf.dotdot_name[0] != '.' ||
75116776Smckusick 				    dirbuf.dotdot_name[1] != '.') {
75216776Smckusick 					printf("rename: mangled dir\n");
75316776Smckusick 				} else {
75416776Smckusick 					dirbuf.dotdot_ino = newparent;
75516776Smckusick 					(void) rdwri(UIO_WRITE, xp,
75616776Smckusick 					    (caddr_t)&dirbuf,
75716776Smckusick 					    sizeof (struct dirtemplate),
75837740Smckusick 					    (off_t)0, UIO_SYSSPACE,
75937737Smckusick 					    tndp->ni_cred, (int *)0);
76037737Smckusick 					cache_purge(ITOV(dp));
76116776Smckusick 				}
76216776Smckusick 			}
7639167Ssam 		}
76437737Smckusick 		error = dirremove(fndp);
76537737Smckusick 		if (!error) {
76616776Smckusick 			xp->i_nlink--;
76716776Smckusick 			xp->i_flag |= ICHG;
7689167Ssam 		}
76916776Smckusick 		xp->i_flag &= ~IRENAME;
7709167Ssam 	}
7719167Ssam 	if (dp)
77237737Smckusick 		vput(ITOV(dp));
77316776Smckusick 	if (xp)
77437737Smckusick 		vput(ITOV(xp));
77537737Smckusick 	vrele(ITOV(ip));
77637737Smckusick 	return (error);
7779167Ssam 
7789167Ssam bad:
7799167Ssam 	if (xp)
78037737Smckusick 		vput(ITOV(xp));
78137737Smckusick 	vput(ITOV(dp));
7829167Ssam out:
7839167Ssam 	ip->i_nlink--;
7849167Ssam 	ip->i_flag |= ICHG;
78537737Smckusick 	vrele(ITOV(ip));
78637737Smckusick 	return (error);
7877701Ssam }
7887701Ssam 
7897535Sroot /*
79012756Ssam  * A virgin directory (no blushing please).
79112756Ssam  */
79212756Ssam struct dirtemplate mastertemplate = {
79312756Ssam 	0, 12, 1, ".",
79412756Ssam 	0, DIRBLKSIZ - 12, 2, ".."
79512756Ssam };
79612756Ssam 
79712756Ssam /*
79812756Ssam  * Mkdir system call
79912756Ssam  */
80037737Smckusick ufs_mkdir(ndp, vap)
80137737Smckusick 	struct nameidata *ndp;
80237737Smckusick 	struct vattr *vap;
80312756Ssam {
80412756Ssam 	register struct inode *ip, *dp;
80537737Smckusick 	struct inode *tip;
80637737Smckusick 	struct vnode *dvp;
80712756Ssam 	struct dirtemplate dirtemplate;
80837737Smckusick 	int error;
80937737Smckusick 	int dmode;
81012756Ssam 
81137737Smckusick 	dvp = ndp->ni_dvp;
81237737Smckusick 	dp = VTOI(dvp);
81337737Smckusick 	dmode = vap->va_mode&0777;
81437737Smckusick 	dmode |= IFDIR;
81512756Ssam 	/*
81612756Ssam 	 * Must simulate part of maknode here
81712756Ssam 	 * in order to acquire the inode, but
81812756Ssam 	 * not have it entered in the parent
81912756Ssam 	 * directory.  The entry is made later
82012756Ssam 	 * after writing "." and ".." entries out.
82112756Ssam 	 */
82237737Smckusick 	error = ialloc(dp, dirpref(dp->i_fs), dmode, &tip);
82337737Smckusick 	if (error) {
82412756Ssam 		iput(dp);
82537737Smckusick 		return (error);
82612756Ssam 	}
82737737Smckusick 	ip = tip;
82812756Ssam #ifdef QUOTA
82912756Ssam 	if (ip->i_dquot != NODQUOT)
83012756Ssam 		panic("mkdir: dquot");
83112756Ssam #endif
83212756Ssam 	ip->i_flag |= IACC|IUPD|ICHG;
83337737Smckusick 	ip->i_mode = dmode;
83437737Smckusick 	ITOV(ip)->v_type = VDIR;	/* Rest init'd in iget() */
83512756Ssam 	ip->i_nlink = 2;
83637737Smckusick 	ip->i_uid = ndp->ni_cred->cr_uid;
83712756Ssam 	ip->i_gid = dp->i_gid;
83812756Ssam #ifdef QUOTA
83912756Ssam 	ip->i_dquot = inoquota(ip);
84012756Ssam #endif
84137737Smckusick 	error = iupdat(ip, &time, &time, 1);
84212756Ssam 
84312756Ssam 	/*
84412756Ssam 	 * Bump link count in parent directory
84512756Ssam 	 * to reflect work done below.  Should
84612756Ssam 	 * be done before reference is created
84712756Ssam 	 * so reparation is possible if we crash.
84812756Ssam 	 */
84912756Ssam 	dp->i_nlink++;
85012756Ssam 	dp->i_flag |= ICHG;
85137737Smckusick 	error = iupdat(dp, &time, &time, 1);
85212756Ssam 
85312756Ssam 	/*
85412756Ssam 	 * Initialize directory with "."
85512756Ssam 	 * and ".." from static template.
85612756Ssam 	 */
85712756Ssam 	dirtemplate = mastertemplate;
85812756Ssam 	dirtemplate.dot_ino = ip->i_number;
85912756Ssam 	dirtemplate.dotdot_ino = dp->i_number;
86037737Smckusick 	error = rdwri(UIO_WRITE, ip, (caddr_t)&dirtemplate,
86137737Smckusick 		sizeof (dirtemplate), (off_t)0, UIO_SYSSPACE,
86237737Smckusick 		ndp->ni_cred, (int *)0);
86337737Smckusick 	if (error) {
86412756Ssam 		dp->i_nlink--;
86512756Ssam 		dp->i_flag |= ICHG;
86612756Ssam 		goto bad;
86712756Ssam 	}
86837737Smckusick 	if (DIRBLKSIZ > dp->i_fs->fs_fsize)
86937737Smckusick 		panic("mkdir: blksize");     /* XXX - should grow w/balloc() */
87018103Smckusick 	else
87118103Smckusick 		ip->i_size = DIRBLKSIZ;
87212756Ssam 	/*
87312756Ssam 	 * Directory all set up, now
87412756Ssam 	 * install the entry for it in
87512756Ssam 	 * the parent directory.
87612756Ssam 	 */
87737737Smckusick 	error = direnter(ip, ndp);
87812756Ssam 	dp = NULL;
87937737Smckusick 	if (error) {
88016694Smckusick 		ndp->ni_nameiop = LOOKUP | NOCACHE;
88137737Smckusick 		error = namei(ndp);
88237737Smckusick 		if (!error) {
88337737Smckusick 			dp = VTOI(ndp->ni_vp);
88412756Ssam 			dp->i_nlink--;
88512756Ssam 			dp->i_flag |= ICHG;
88612756Ssam 		}
88712756Ssam 	}
88812756Ssam bad:
88912756Ssam 	/*
89012756Ssam 	 * No need to do an explicit itrunc here,
89137737Smckusick 	 * vrele will do this for us because we set
89212756Ssam 	 * the link count to 0.
89312756Ssam 	 */
89437737Smckusick 	if (error) {
89512756Ssam 		ip->i_nlink = 0;
89612756Ssam 		ip->i_flag |= ICHG;
89738144Smckusick 		iput(ip);
89838144Smckusick 	} else
89938144Smckusick 		ndp->ni_vp = ITOV(ip);
90012756Ssam 	if (dp)
90112756Ssam 		iput(dp);
90237737Smckusick 	return (error);
90312756Ssam }
90412756Ssam 
90512756Ssam /*
90612756Ssam  * Rmdir system call.
90712756Ssam  */
90837737Smckusick ufs_rmdir(ndp)
90937737Smckusick 	register struct nameidata *ndp;
91012756Ssam {
91112756Ssam 	register struct inode *ip, *dp;
91237737Smckusick 	int error = 0;
91312756Ssam 
91437737Smckusick 	ip = VTOI(ndp->ni_vp);
91537737Smckusick 	dp = VTOI(ndp->ni_dvp);
91612756Ssam 	/*
91712756Ssam 	 * No rmdir "." please.
91812756Ssam 	 */
91912756Ssam 	if (dp == ip) {
92037737Smckusick 		vrele(ITOV(dp));
92112756Ssam 		iput(ip);
92237737Smckusick 		return (EINVAL);
92312756Ssam 	}
92412756Ssam 	/*
92512756Ssam 	 * Verify the directory is empty (and valid).
92612756Ssam 	 * (Rmdir ".." won't be valid since
92712756Ssam 	 *  ".." will contain a reference to
92812756Ssam 	 *  the current directory and thus be
92912756Ssam 	 *  non-empty.)
93012756Ssam 	 */
93137737Smckusick 	if (ip->i_nlink != 2 || !dirempty(ip, dp->i_number, ndp->ni_cred)) {
93237737Smckusick 		error = ENOTEMPTY;
93312756Ssam 		goto out;
93412756Ssam 	}
93512756Ssam 	/*
93612756Ssam 	 * Delete reference to directory before purging
93712756Ssam 	 * inode.  If we crash in between, the directory
93812756Ssam 	 * will be reattached to lost+found,
93912756Ssam 	 */
94037737Smckusick 	if (error = dirremove(ndp))
94112756Ssam 		goto out;
94212756Ssam 	dp->i_nlink--;
94312756Ssam 	dp->i_flag |= ICHG;
94437737Smckusick 	cache_purge(ITOV(dp));
94512756Ssam 	iput(dp);
94637737Smckusick 	ndp->ni_dvp = NULL;
94712756Ssam 	/*
94812756Ssam 	 * Truncate inode.  The only stuff left
94912756Ssam 	 * in the directory is "." and "..".  The
95012756Ssam 	 * "." reference is inconsequential since
95112756Ssam 	 * we're quashing it.  The ".." reference
95212756Ssam 	 * has already been adjusted above.  We've
95312756Ssam 	 * removed the "." reference and the reference
95412756Ssam 	 * in the parent directory, but there may be
95512756Ssam 	 * other hard links so decrement by 2 and
95612756Ssam 	 * worry about them later.
95712756Ssam 	 */
95812756Ssam 	ip->i_nlink -= 2;
95937737Smckusick 	error = itrunc(ip, (u_long)0);
96037737Smckusick 	cache_purge(ITOV(ip));
96112756Ssam out:
96237737Smckusick 	if (ndp->ni_dvp)
96312756Ssam 		iput(dp);
96412756Ssam 	iput(ip);
96537737Smckusick 	return (error);
96612756Ssam }
96712756Ssam 
96837737Smckusick /*
96937737Smckusick  * symlink -- make a symbolic link
97037737Smckusick  */
97137737Smckusick ufs_symlink(ndp, vap, target)
97237737Smckusick 	struct nameidata *ndp;
97337737Smckusick 	struct vattr *vap;
97437737Smckusick 	char *target;
97512756Ssam {
97637737Smckusick 	struct inode *ip;
97737737Smckusick 	int error;
97812756Ssam 
97937737Smckusick 	error = maknode(IFLNK | vap->va_mode, ndp, &ip);
98037737Smckusick 	if (error)
98137737Smckusick 		return (error);
98237737Smckusick 	error = rdwri(UIO_WRITE, ip, target, strlen(target), (off_t)0,
98337737Smckusick 		UIO_SYSSPACE, ndp->ni_cred, (int *)0);
98437737Smckusick 	iput(ip);
98537737Smckusick 	return (error);
98637737Smckusick }
98737737Smckusick 
98837737Smckusick /*
98937737Smckusick  * Vnode op for read and write
99037737Smckusick  */
99137737Smckusick ufs_readdir(vp, uio, offp, cred)
99237737Smckusick 	struct vnode *vp;
99337737Smckusick 	register struct uio *uio;
99437737Smckusick 	off_t *offp;
99537737Smckusick 	struct ucred *cred;
99637737Smckusick {
99737737Smckusick 	register struct inode *ip = VTOI(vp);
99837737Smckusick 	int count, error;
99937737Smckusick 
100037737Smckusick 	ILOCK(ip);
100137737Smckusick 	uio->uio_offset = *offp;
100237737Smckusick 	count = uio->uio_resid;
100337737Smckusick 	count &= ~(DIRBLKSIZ - 1);
100437737Smckusick 	if (vp->v_type != VDIR || uio->uio_iovcnt != 1 ||
100537737Smckusick 	    (count < DIRBLKSIZ) || (uio->uio_offset & (DIRBLKSIZ -1))) {
100637737Smckusick 		IUNLOCK(ip);
100737737Smckusick 		return (EINVAL);
100816540Ssam 	}
100937737Smckusick 	uio->uio_resid = count;
101037737Smckusick 	uio->uio_iov->iov_len = count;
101137737Smckusick 	error = readip(ip, uio, cred);
101237737Smckusick 	*offp += count - uio->uio_resid;
101337737Smckusick 	IUNLOCK(ip);
101437737Smckusick 	return (error);
101537737Smckusick }
101637737Smckusick 
101737737Smckusick /*
101837737Smckusick  * Return target name of a symbolic link
101937737Smckusick  */
102037737Smckusick ufs_readlink(vp, uiop, cred)
102137737Smckusick 	struct vnode *vp;
102237737Smckusick 	struct uio *uiop;
102337737Smckusick 	struct ucred *cred;
102437737Smckusick {
102537737Smckusick 
102637737Smckusick 	return (readip(VTOI(vp), uiop, cred));
102737737Smckusick }
102837737Smckusick 
102937737Smckusick /*
103037737Smckusick  * Ufs abort op, called after namei() when a CREATE/DELETE isn't actually
103137737Smckusick  * done. Iff ni_vp/ni_dvp not null and locked, unlock.
103237737Smckusick  */
103337737Smckusick ufs_abortop(ndp)
103437737Smckusick 	register struct nameidata *ndp;
103537737Smckusick {
103637737Smckusick 	register struct inode *ip;
103737737Smckusick 
103837737Smckusick 	if (ndp->ni_vp) {
103937737Smckusick 		ip = VTOI(ndp->ni_vp);
104037737Smckusick 		if (ip->i_flag & ILOCKED)
104137737Smckusick 			IUNLOCK(ip);
104237737Smckusick 		vrele(ndp->ni_vp);
104312756Ssam 	}
104437737Smckusick 	if (ndp->ni_dvp) {
104537737Smckusick 		ip = VTOI(ndp->ni_dvp);
104637737Smckusick 		if (ip->i_flag & ILOCKED)
104737737Smckusick 			IUNLOCK(ip);
104837737Smckusick 		vrele(ndp->ni_dvp);
104937737Smckusick 	}
105037737Smckusick 	return;
105112756Ssam }
105212756Ssam 
105337737Smckusick ufs_lock(vp)
105437737Smckusick 	struct vnode *vp;
105537737Smckusick {
105637737Smckusick 	register struct inode *ip = VTOI(vp);
105737737Smckusick 
105837737Smckusick 	ILOCK(ip);
105937737Smckusick 	return (0);
106037737Smckusick }
106137737Smckusick 
106237737Smckusick ufs_unlock(vp)
106337737Smckusick 	struct vnode *vp;
106437737Smckusick {
106537737Smckusick 	register struct inode *ip = VTOI(vp);
106637737Smckusick 
106737737Smckusick 	if (!(ip->i_flag & ILOCKED))
106837737Smckusick 		panic("ufs_unlock NOT LOCKED");
106937737Smckusick 	IUNLOCK(ip);
107037737Smckusick 	return (0);
107137737Smckusick }
107237737Smckusick 
107312756Ssam /*
107437737Smckusick  * Get access to bmap
107512756Ssam  */
107637737Smckusick ufs_bmap(vp, bn, vpp, bnp)
107737737Smckusick 	struct vnode *vp;
107837737Smckusick 	daddr_t bn;
107937737Smckusick 	struct vnode **vpp;
108037737Smckusick 	daddr_t *bnp;
108112756Ssam {
108237737Smckusick 	struct inode *ip = VTOI(vp);
108312756Ssam 
108437737Smckusick 	if (vpp != NULL)
108537737Smckusick 		*vpp = ip->i_devvp;
108637737Smckusick 	if (bnp == NULL)
108737737Smckusick 		return (0);
108837737Smckusick 	return (bmap(ip, bn, bnp, (daddr_t *)0, (int *)0));
108912756Ssam }
109037737Smckusick 
109137737Smckusick /*
109237737Smckusick  * Just call the device strategy routine
109337737Smckusick  */
109437737Smckusick ufs_strategy(bp)
109537737Smckusick 	register struct buf *bp;
109637737Smckusick {
109737737Smckusick 	(*bdevsw[major(bp->b_dev)].d_strategy)(bp);
109837737Smckusick 	return (0);
109937737Smckusick }
110037737Smckusick 
110137737Smckusick /*
110237737Smckusick  * Make a new file.
110337737Smckusick  */
110437737Smckusick maknode(mode, ndp, ipp)
110537737Smckusick 	int mode;
110637737Smckusick 	register struct nameidata *ndp;
110737737Smckusick 	struct inode **ipp;
110837737Smckusick {
110937737Smckusick 	register struct inode *ip;
111037737Smckusick 	struct inode *tip;
111137737Smckusick 	register struct inode *pdir = VTOI(ndp->ni_dvp);
111237737Smckusick 	ino_t ipref;
111337737Smckusick 	int error;
111437737Smckusick 
111537737Smckusick 	*ipp = 0;
111637737Smckusick 	if ((mode & IFMT) == IFDIR)
111737737Smckusick 		ipref = dirpref(pdir->i_fs);
111837737Smckusick 	else
111937737Smckusick 		ipref = pdir->i_number;
112037737Smckusick 	error = ialloc(pdir, ipref, mode, &tip);
112137737Smckusick 	if (error) {
112237737Smckusick 		iput(pdir);
112337737Smckusick 		return (error);
112437737Smckusick 	}
112537737Smckusick 	ip = tip;
112637737Smckusick #ifdef QUOTA
112737737Smckusick 	if (ip->i_dquot != NODQUOT)
112837737Smckusick 		panic("maknode: dquot");
112937737Smckusick #endif
113037737Smckusick 	ip->i_flag |= IACC|IUPD|ICHG;
113137737Smckusick 	if ((mode & IFMT) == 0)
113237737Smckusick 		mode |= IFREG;
113337737Smckusick 	ip->i_mode = mode;
113437737Smckusick 	ITOV(ip)->v_type = IFTOVT(mode);	/* Rest init'd in iget() */
113537737Smckusick 	ip->i_nlink = 1;
113637737Smckusick 	ip->i_uid = ndp->ni_cred->cr_uid;
113737737Smckusick 	ip->i_gid = pdir->i_gid;
113837737Smckusick 	if ((ip->i_mode & ISGID) && !groupmember(ip->i_gid, ndp->ni_cred) &&
113937737Smckusick 	    suser(ndp->ni_cred, NULL))
114037737Smckusick 		ip->i_mode &= ~ISGID;
114137737Smckusick #ifdef QUOTA
114237737Smckusick 	ip->i_dquot = inoquota(ip);
114337737Smckusick #endif
114437737Smckusick 
114537737Smckusick 	/*
114637737Smckusick 	 * Make sure inode goes to disk before directory entry.
114737737Smckusick 	 */
114837737Smckusick 	if ((error = iupdat(ip, &time, &time, 1)) ||
114937737Smckusick 	    (error = direnter(ip, ndp))) {
115037737Smckusick 		/*
115137737Smckusick 		 * Write error occurred trying to update the inode
115237737Smckusick 		 * or the directory so must deallocate the inode.
115337737Smckusick 		 */
115437737Smckusick 		ip->i_nlink = 0;
115537737Smckusick 		ip->i_flag |= ICHG;
115637737Smckusick 		iput(ip);
115737737Smckusick 		return (error);
115837737Smckusick 	}
115937737Smckusick 	*ipp = ip;
116037737Smckusick 	return (0);
116137737Smckusick }
1162