xref: /csrg-svn/sys/ufs/lfs/lfs_vnops.c (revision 38657)
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*38657Smckusick  *	@(#)lfs_vnops.c	7.15 (Berkeley) 08/16/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;
239*38657Smckusick 	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;
37637737Smckusick 	if (cred->cr_ruid != 0)
3776254Sroot 		ip->i_mode &= ~(ISUID|ISGID);
3787701Ssam #ifdef QUOTA
3797482Skre 	ip->i_dquot = inoquota(ip);
38012646Ssam 	(void) chkdq(ip, change, 1);
38126361Skarels 	(void) chkiq(ip->i_dev, (struct inode *)NULL, (uid_t)uid, 1);
38212646Ssam 	return (u.u_error);		/* should == 0 ALWAYS !! */
38312646Ssam #else
38412646Ssam 	return (0);
3857482Skre #endif
38637Sbill }
38737Sbill 
38837737Smckusick /* ARGSUSED */
38937737Smckusick ufs_ioctl(vp, com, data, fflag, cred)
39037737Smckusick 	struct vnode *vp;
39137737Smckusick 	int com;
39237737Smckusick 	caddr_t data;
39337737Smckusick 	int fflag;
39437737Smckusick 	struct ucred *cred;
39511811Ssam {
39611811Ssam 
39737737Smckusick 	printf("ufs_ioctl called with type %d\n", vp->v_type);
39837737Smckusick 	return (ENOTTY);
39911811Ssam }
40011811Ssam 
40137737Smckusick /* ARGSUSED */
40237737Smckusick ufs_select(vp, which, cred)
40337737Smckusick 	struct vnode *vp;
40437737Smckusick 	int which;
40537737Smckusick 	struct ucred *cred;
40637737Smckusick {
40737737Smckusick 
40837737Smckusick 	printf("ufs_select called with type %d\n", vp->v_type);
40937737Smckusick 	return (1);		/* XXX */
41037737Smckusick }
41137737Smckusick 
4129167Ssam /*
41337737Smckusick  * Mmap a file
41437737Smckusick  *
41537737Smckusick  * NB Currently unsupported.
4169167Ssam  */
41737737Smckusick /* ARGSUSED */
41837737Smckusick ufs_mmap(vp, fflags, cred)
41937737Smckusick 	struct vnode *vp;
42037737Smckusick 	int fflags;
42137737Smckusick 	struct ucred *cred;
42237Sbill {
42337Sbill 
42437737Smckusick 	return (EINVAL);
42537Sbill }
4267535Sroot 
4279167Ssam /*
42837737Smckusick  * Synch an open file.
4299167Ssam  */
43037737Smckusick /* ARGSUSED */
43137737Smckusick ufs_fsync(vp, fflags, cred)
43237737Smckusick 	struct vnode *vp;
43337737Smckusick 	int fflags;
43437737Smckusick 	struct ucred *cred;
4357701Ssam {
43637737Smckusick 	register struct inode *ip = VTOI(vp);
43737737Smckusick 	int error;
4387701Ssam 
43937737Smckusick 	ILOCK(ip);
44037737Smckusick 	if (fflags&FWRITE)
44137737Smckusick 		ip->i_flag |= ICHG;
44237737Smckusick 	error = syncip(ip);
44337737Smckusick 	IUNLOCK(ip);
44437737Smckusick 	return (error);
4457701Ssam }
4467701Ssam 
4479167Ssam /*
44837737Smckusick  * Seek on a file
44937737Smckusick  *
45037737Smckusick  * Nothing to do, so just return.
4519167Ssam  */
45237737Smckusick /* ARGSUSED */
45337737Smckusick ufs_seek(vp, oldoff, newoff, cred)
45437737Smckusick 	struct vnode *vp;
45537737Smckusick 	off_t oldoff, newoff;
45637737Smckusick 	struct ucred *cred;
4577701Ssam {
4587701Ssam 
45937737Smckusick 	return (0);
46037737Smckusick }
46137737Smckusick 
46237737Smckusick /*
46337737Smckusick  * ufs remove
46437737Smckusick  * Hard to avoid races here, especially
46537737Smckusick  * in unlinking directories.
46637737Smckusick  */
46737737Smckusick ufs_remove(ndp)
46837737Smckusick 	struct nameidata *ndp;
46937737Smckusick {
47037737Smckusick 	register struct inode *ip, *dp;
47137737Smckusick 	int error;
47237737Smckusick 
47337737Smckusick 	ip = VTOI(ndp->ni_vp);
47437737Smckusick 	dp = VTOI(ndp->ni_dvp);
47537737Smckusick 	error = dirremove(ndp);
47637737Smckusick 	if (!error) {
47737737Smckusick 		ip->i_nlink--;
47837737Smckusick 		ip->i_flag |= ICHG;
4797701Ssam 	}
48037737Smckusick 	if (dp == ip)
48137737Smckusick 		vrele(ITOV(ip));
48237737Smckusick 	else
48337737Smckusick 		iput(ip);
48437737Smckusick 	iput(dp);
48537737Smckusick 	return (error);
4867701Ssam }
4877701Ssam 
4889167Ssam /*
48937737Smckusick  * link vnode call
4909167Ssam  */
49137737Smckusick ufs_link(vp, ndp)
49237737Smckusick 	register struct vnode *vp;
49337737Smckusick 	register struct nameidata *ndp;
4949167Ssam {
49537737Smckusick 	register struct inode *ip = VTOI(vp);
49637737Smckusick 	int error;
4979167Ssam 
49837737Smckusick 	if (ndp->ni_dvp != vp)
49937737Smckusick 		ILOCK(ip);
50037737Smckusick 	if (ip->i_nlink == LINK_MAX - 1) {
50137737Smckusick 		error = EMLINK;
50237737Smckusick 		goto out;
50337737Smckusick 	}
50437737Smckusick 	ip->i_nlink++;
50537737Smckusick 	ip->i_flag |= ICHG;
50637737Smckusick 	error = iupdat(ip, &time, &time, 1);
50737737Smckusick 	if (!error)
50837737Smckusick 		error = direnter(ip, ndp);
50937737Smckusick out:
51037737Smckusick 	if (ndp->ni_dvp != vp)
51137737Smckusick 		IUNLOCK(ip);
51237737Smckusick 	if (error) {
51337737Smckusick 		ip->i_nlink--;
51430598Smckusick 		ip->i_flag |= ICHG;
51537737Smckusick 	}
51637737Smckusick 	return (error);
5179167Ssam }
5189167Ssam 
5199167Ssam /*
5209167Ssam  * Rename system call.
5219167Ssam  * 	rename("foo", "bar");
5229167Ssam  * is essentially
5239167Ssam  *	unlink("bar");
5249167Ssam  *	link("foo", "bar");
5259167Ssam  *	unlink("foo");
5269167Ssam  * but ``atomically''.  Can't do full commit without saving state in the
5279167Ssam  * inode on disk which isn't feasible at this time.  Best we can do is
5289167Ssam  * always guarantee the target exists.
5299167Ssam  *
5309167Ssam  * Basic algorithm is:
5319167Ssam  *
5329167Ssam  * 1) Bump link count on source while we're linking it to the
53337737Smckusick  *    target.  This also ensure the inode won't be deleted out
53416776Smckusick  *    from underneath us while we work (it may be truncated by
53516776Smckusick  *    a concurrent `trunc' or `open' for creation).
5369167Ssam  * 2) Link source to destination.  If destination already exists,
5379167Ssam  *    delete it first.
53816776Smckusick  * 3) Unlink source reference to inode if still around. If a
53916776Smckusick  *    directory was moved and the parent of the destination
5409167Ssam  *    is different from the source, patch the ".." entry in the
5419167Ssam  *    directory.
5429167Ssam  */
54337737Smckusick ufs_rename(fndp, tndp)
54437737Smckusick 	register struct nameidata *fndp, *tndp;
5457701Ssam {
5469167Ssam 	register struct inode *ip, *xp, *dp;
54716776Smckusick 	struct dirtemplate dirbuf;
54816776Smckusick 	int doingdirectory = 0, oldparent = 0, newparent = 0;
54910051Ssam 	int error = 0;
5507701Ssam 
55137737Smckusick 	dp = VTOI(fndp->ni_dvp);
55237737Smckusick 	ip = VTOI(fndp->ni_vp);
55337737Smckusick 	ILOCK(ip);
5549167Ssam 	if ((ip->i_mode&IFMT) == IFDIR) {
55537737Smckusick 		register struct direct *d = &fndp->ni_dent;
5569167Ssam 
5579167Ssam 		/*
55811641Ssam 		 * Avoid ".", "..", and aliases of "." for obvious reasons.
5599167Ssam 		 */
56037737Smckusick 		if ((d->d_namlen == 1 && d->d_name[0] == '.') || dp == ip ||
56137737Smckusick 		    fndp->ni_isdotdot || (ip->i_flag & IRENAME)) {
56237737Smckusick 			IUNLOCK(ip);
56337737Smckusick 			ufs_abortop(fndp);
56437737Smckusick 			ufs_abortop(tndp);
56537737Smckusick 			return (EINVAL);
5669167Ssam 		}
56716776Smckusick 		ip->i_flag |= IRENAME;
5689167Ssam 		oldparent = dp->i_number;
5699167Ssam 		doingdirectory++;
5709167Ssam 	}
57137737Smckusick 	vrele(fndp->ni_dvp);
5729167Ssam 
5739167Ssam 	/*
5749167Ssam 	 * 1) Bump link count while we're moving stuff
5759167Ssam 	 *    around.  If we crash somewhere before
5769167Ssam 	 *    completing our work, the link count
5779167Ssam 	 *    may be wrong, but correctable.
5789167Ssam 	 */
5799167Ssam 	ip->i_nlink++;
5809167Ssam 	ip->i_flag |= ICHG;
58137737Smckusick 	error = iupdat(ip, &time, &time, 1);
58216664Smckusick 	IUNLOCK(ip);
5839167Ssam 
5849167Ssam 	/*
5859167Ssam 	 * When the target exists, both the directory
58637737Smckusick 	 * and target vnodes are returned locked.
5879167Ssam 	 */
58837737Smckusick 	dp = VTOI(tndp->ni_dvp);
58937737Smckusick 	xp = NULL;
59037737Smckusick 	if (tndp->ni_vp)
59137737Smckusick 		xp = VTOI(tndp->ni_vp);
5929167Ssam 	/*
59311641Ssam 	 * If ".." must be changed (ie the directory gets a new
59412816Smckusick 	 * parent) then the source directory must not be in the
59512816Smckusick 	 * directory heirarchy above the target, as this would
59612816Smckusick 	 * orphan everything below the source directory. Also
59712816Smckusick 	 * the user must have write permission in the source so
59812816Smckusick 	 * as to be able to change "..". We must repeat the call
59912816Smckusick 	 * to namei, as the parent directory is unlocked by the
60012816Smckusick 	 * call to checkpath().
60111641Ssam 	 */
60216776Smckusick 	if (oldparent != dp->i_number)
60316776Smckusick 		newparent = dp->i_number;
60416776Smckusick 	if (doingdirectory && newparent) {
60537737Smckusick 		if (error = iaccess(ip, IWRITE, tndp->ni_cred))
60612816Smckusick 			goto bad;
60737737Smckusick 		tndp->ni_nameiop = RENAME | LOCKPARENT | LOCKLEAF | NOCACHE;
60812816Smckusick 		do {
60937737Smckusick 			dp = VTOI(tndp->ni_dvp);
61012816Smckusick 			if (xp != NULL)
61138069Smckusick 				iput(xp);
61237737Smckusick 			if (error = checkpath(ip, dp, tndp->ni_cred))
61312816Smckusick 				goto out;
61437737Smckusick 			if (error = namei(tndp))
61512816Smckusick 				goto out;
61637737Smckusick 			xp = NULL;
61737737Smckusick 			if (tndp->ni_vp)
61837737Smckusick 				xp = VTOI(tndp->ni_vp);
61937737Smckusick 		} while (dp != VTOI(tndp->ni_dvp));
62012816Smckusick 	}
62111641Ssam 	/*
6229167Ssam 	 * 2) If target doesn't exist, link the target
6239167Ssam 	 *    to the source and unlink the source.
6249167Ssam 	 *    Otherwise, rewrite the target directory
6259167Ssam 	 *    entry to reference the source inode and
6269167Ssam 	 *    expunge the original entry's existence.
6279167Ssam 	 */
6289167Ssam 	if (xp == NULL) {
62937737Smckusick 		if (dp->i_dev != ip->i_dev)
63037737Smckusick 			panic("rename: EXDEV");
6319167Ssam 		/*
63216776Smckusick 		 * Account for ".." in new directory.
63316776Smckusick 		 * When source and destination have the same
63416776Smckusick 		 * parent we don't fool with the link count.
6359167Ssam 		 */
63616776Smckusick 		if (doingdirectory && newparent) {
6379167Ssam 			dp->i_nlink++;
6389167Ssam 			dp->i_flag |= ICHG;
63937737Smckusick 			error = iupdat(dp, &time, &time, 1);
6409167Ssam 		}
64137737Smckusick 		if (error = direnter(ip, tndp))
6429167Ssam 			goto out;
6439167Ssam 	} else {
64437737Smckusick 		if (xp->i_dev != dp->i_dev || xp->i_dev != ip->i_dev)
64537737Smckusick 			panic("rename: EXDEV");
6469167Ssam 		/*
64710590Ssam 		 * Short circuit rename(foo, foo).
64810590Ssam 		 */
64910590Ssam 		if (xp->i_number == ip->i_number)
65037737Smckusick 			panic("rename: same file");
65110590Ssam 		/*
65224433Sbloom 		 * If the parent directory is "sticky", then the user must
65324433Sbloom 		 * own the parent directory, or the destination of the rename,
65424433Sbloom 		 * otherwise the destination may not be changed (except by
65524433Sbloom 		 * root). This implements append-only directories.
65624433Sbloom 		 */
65737737Smckusick 		if ((dp->i_mode & ISVTX) && tndp->ni_cred->cr_uid != 0 &&
65837737Smckusick 		    tndp->ni_cred->cr_uid != dp->i_uid &&
65937737Smckusick 		    xp->i_uid != tndp->ni_cred->cr_uid) {
66024433Sbloom 			error = EPERM;
66124433Sbloom 			goto bad;
66224433Sbloom 		}
66324433Sbloom 		/*
66410051Ssam 		 * Target must be empty if a directory
66510051Ssam 		 * and have no links to it.
6669167Ssam 		 * Also, insure source and target are
6679167Ssam 		 * compatible (both directories, or both
6689167Ssam 		 * not directories).
6699167Ssam 		 */
6709167Ssam 		if ((xp->i_mode&IFMT) == IFDIR) {
67137737Smckusick 			if (!dirempty(xp, dp->i_number, tndp->ni_cred) ||
67237737Smckusick 			    xp->i_nlink > 2) {
67310051Ssam 				error = ENOTEMPTY;
6749167Ssam 				goto bad;
6759167Ssam 			}
6769167Ssam 			if (!doingdirectory) {
67710051Ssam 				error = ENOTDIR;
6789167Ssam 				goto bad;
6799167Ssam 			}
68037737Smckusick 			cache_purge(ITOV(dp));
6819167Ssam 		} else if (doingdirectory) {
68210051Ssam 			error = EISDIR;
6839167Ssam 			goto bad;
6849167Ssam 		}
68537737Smckusick 		if (error = dirrewrite(dp, ip, tndp))
68637737Smckusick 			goto bad;
68737737Smckusick 		vput(ITOV(dp));
6889167Ssam 		/*
68910051Ssam 		 * Adjust the link count of the target to
69010051Ssam 		 * reflect the dirrewrite above.  If this is
69110051Ssam 		 * a directory it is empty and there are
69210051Ssam 		 * no links to it, so we can squash the inode and
69310051Ssam 		 * any space associated with it.  We disallowed
69410051Ssam 		 * renaming over top of a directory with links to
69516776Smckusick 		 * it above, as the remaining link would point to
69616776Smckusick 		 * a directory without "." or ".." entries.
6979167Ssam 		 */
69810051Ssam 		xp->i_nlink--;
6999167Ssam 		if (doingdirectory) {
70010051Ssam 			if (--xp->i_nlink != 0)
70110051Ssam 				panic("rename: linked directory");
70237737Smckusick 			error = itrunc(xp, (u_long)0);
70310051Ssam 		}
7049167Ssam 		xp->i_flag |= ICHG;
70538398Smckusick 		iput(xp);
70610246Ssam 		xp = NULL;
7079167Ssam 	}
7089167Ssam 
7099167Ssam 	/*
7109167Ssam 	 * 3) Unlink the source.
7119167Ssam 	 */
71237737Smckusick 	fndp->ni_nameiop = DELETE | LOCKPARENT | LOCKLEAF;
71337737Smckusick 	(void)namei(fndp);
71437737Smckusick 	if (fndp->ni_vp != NULL) {
71537737Smckusick 		xp = VTOI(fndp->ni_vp);
71637737Smckusick 		dp = VTOI(fndp->ni_dvp);
71737737Smckusick 	} else {
71838069Smckusick 		if (fndp->ni_dvp != NULL)
71938069Smckusick 			vput(fndp->ni_dvp);
72037737Smckusick 		xp = NULL;
72117758Smckusick 		dp = NULL;
72237737Smckusick 	}
7239167Ssam 	/*
72437737Smckusick 	 * Ensure that the directory entry still exists and has not
72516776Smckusick 	 * changed while the new name has been entered. If the source is
72616776Smckusick 	 * a file then the entry may have been unlinked or renamed. In
72716776Smckusick 	 * either case there is no further work to be done. If the source
72816776Smckusick 	 * is a directory then it cannot have been rmdir'ed; its link
72916776Smckusick 	 * count of three would cause a rmdir to fail with ENOTEMPTY.
73037737Smckusick 	 * The IRENAME flag ensures that it cannot be moved by another
73116776Smckusick 	 * rename.
7329167Ssam 	 */
73317758Smckusick 	if (xp != ip) {
73416776Smckusick 		if (doingdirectory)
73517758Smckusick 			panic("rename: lost dir entry");
73616776Smckusick 	} else {
7379167Ssam 		/*
73816776Smckusick 		 * If the source is a directory with a
73916776Smckusick 		 * new parent, the link count of the old
74016776Smckusick 		 * parent directory must be decremented
74116776Smckusick 		 * and ".." set to point to the new parent.
7429167Ssam 		 */
74316776Smckusick 		if (doingdirectory && newparent) {
7449167Ssam 			dp->i_nlink--;
7459167Ssam 			dp->i_flag |= ICHG;
74616776Smckusick 			error = rdwri(UIO_READ, xp, (caddr_t)&dirbuf,
74737737Smckusick 				sizeof (struct dirtemplate), (off_t)0,
74837740Smckusick 				UIO_SYSSPACE, tndp->ni_cred, (int *)0);
74916776Smckusick 			if (error == 0) {
75016776Smckusick 				if (dirbuf.dotdot_namlen != 2 ||
75116776Smckusick 				    dirbuf.dotdot_name[0] != '.' ||
75216776Smckusick 				    dirbuf.dotdot_name[1] != '.') {
75316776Smckusick 					printf("rename: mangled dir\n");
75416776Smckusick 				} else {
75516776Smckusick 					dirbuf.dotdot_ino = newparent;
75616776Smckusick 					(void) rdwri(UIO_WRITE, xp,
75716776Smckusick 					    (caddr_t)&dirbuf,
75816776Smckusick 					    sizeof (struct dirtemplate),
75937740Smckusick 					    (off_t)0, UIO_SYSSPACE,
76037737Smckusick 					    tndp->ni_cred, (int *)0);
76137737Smckusick 					cache_purge(ITOV(dp));
76216776Smckusick 				}
76316776Smckusick 			}
7649167Ssam 		}
76537737Smckusick 		error = dirremove(fndp);
76637737Smckusick 		if (!error) {
76716776Smckusick 			xp->i_nlink--;
76816776Smckusick 			xp->i_flag |= ICHG;
7699167Ssam 		}
77016776Smckusick 		xp->i_flag &= ~IRENAME;
7719167Ssam 	}
7729167Ssam 	if (dp)
77337737Smckusick 		vput(ITOV(dp));
77416776Smckusick 	if (xp)
77537737Smckusick 		vput(ITOV(xp));
77637737Smckusick 	vrele(ITOV(ip));
77737737Smckusick 	return (error);
7789167Ssam 
7799167Ssam bad:
7809167Ssam 	if (xp)
78137737Smckusick 		vput(ITOV(xp));
78237737Smckusick 	vput(ITOV(dp));
7839167Ssam out:
7849167Ssam 	ip->i_nlink--;
7859167Ssam 	ip->i_flag |= ICHG;
78637737Smckusick 	vrele(ITOV(ip));
78737737Smckusick 	return (error);
7887701Ssam }
7897701Ssam 
7907535Sroot /*
79112756Ssam  * A virgin directory (no blushing please).
79212756Ssam  */
79312756Ssam struct dirtemplate mastertemplate = {
79412756Ssam 	0, 12, 1, ".",
79512756Ssam 	0, DIRBLKSIZ - 12, 2, ".."
79612756Ssam };
79712756Ssam 
79812756Ssam /*
79912756Ssam  * Mkdir system call
80012756Ssam  */
80137737Smckusick ufs_mkdir(ndp, vap)
80237737Smckusick 	struct nameidata *ndp;
80337737Smckusick 	struct vattr *vap;
80412756Ssam {
80512756Ssam 	register struct inode *ip, *dp;
80637737Smckusick 	struct inode *tip;
80737737Smckusick 	struct vnode *dvp;
80812756Ssam 	struct dirtemplate dirtemplate;
80937737Smckusick 	int error;
81037737Smckusick 	int dmode;
81112756Ssam 
81237737Smckusick 	dvp = ndp->ni_dvp;
81337737Smckusick 	dp = VTOI(dvp);
81437737Smckusick 	dmode = vap->va_mode&0777;
81537737Smckusick 	dmode |= IFDIR;
81612756Ssam 	/*
81712756Ssam 	 * Must simulate part of maknode here
81812756Ssam 	 * in order to acquire the inode, but
81912756Ssam 	 * not have it entered in the parent
82012756Ssam 	 * directory.  The entry is made later
82112756Ssam 	 * after writing "." and ".." entries out.
82212756Ssam 	 */
82337737Smckusick 	error = ialloc(dp, dirpref(dp->i_fs), dmode, &tip);
82437737Smckusick 	if (error) {
82512756Ssam 		iput(dp);
82637737Smckusick 		return (error);
82712756Ssam 	}
82837737Smckusick 	ip = tip;
82912756Ssam #ifdef QUOTA
83012756Ssam 	if (ip->i_dquot != NODQUOT)
83112756Ssam 		panic("mkdir: dquot");
83212756Ssam #endif
83312756Ssam 	ip->i_flag |= IACC|IUPD|ICHG;
83437737Smckusick 	ip->i_mode = dmode;
83537737Smckusick 	ITOV(ip)->v_type = VDIR;	/* Rest init'd in iget() */
83612756Ssam 	ip->i_nlink = 2;
83737737Smckusick 	ip->i_uid = ndp->ni_cred->cr_uid;
83812756Ssam 	ip->i_gid = dp->i_gid;
83912756Ssam #ifdef QUOTA
84012756Ssam 	ip->i_dquot = inoquota(ip);
84112756Ssam #endif
84237737Smckusick 	error = iupdat(ip, &time, &time, 1);
84312756Ssam 
84412756Ssam 	/*
84512756Ssam 	 * Bump link count in parent directory
84612756Ssam 	 * to reflect work done below.  Should
84712756Ssam 	 * be done before reference is created
84812756Ssam 	 * so reparation is possible if we crash.
84912756Ssam 	 */
85012756Ssam 	dp->i_nlink++;
85112756Ssam 	dp->i_flag |= ICHG;
85237737Smckusick 	error = iupdat(dp, &time, &time, 1);
85312756Ssam 
85412756Ssam 	/*
85512756Ssam 	 * Initialize directory with "."
85612756Ssam 	 * and ".." from static template.
85712756Ssam 	 */
85812756Ssam 	dirtemplate = mastertemplate;
85912756Ssam 	dirtemplate.dot_ino = ip->i_number;
86012756Ssam 	dirtemplate.dotdot_ino = dp->i_number;
86137737Smckusick 	error = rdwri(UIO_WRITE, ip, (caddr_t)&dirtemplate,
86237737Smckusick 		sizeof (dirtemplate), (off_t)0, UIO_SYSSPACE,
86337737Smckusick 		ndp->ni_cred, (int *)0);
86437737Smckusick 	if (error) {
86512756Ssam 		dp->i_nlink--;
86612756Ssam 		dp->i_flag |= ICHG;
86712756Ssam 		goto bad;
86812756Ssam 	}
86937737Smckusick 	if (DIRBLKSIZ > dp->i_fs->fs_fsize)
87037737Smckusick 		panic("mkdir: blksize");     /* XXX - should grow w/balloc() */
87118103Smckusick 	else
87218103Smckusick 		ip->i_size = DIRBLKSIZ;
87312756Ssam 	/*
87412756Ssam 	 * Directory all set up, now
87512756Ssam 	 * install the entry for it in
87612756Ssam 	 * the parent directory.
87712756Ssam 	 */
87837737Smckusick 	error = direnter(ip, ndp);
87912756Ssam 	dp = NULL;
88037737Smckusick 	if (error) {
88116694Smckusick 		ndp->ni_nameiop = LOOKUP | NOCACHE;
88237737Smckusick 		error = namei(ndp);
88337737Smckusick 		if (!error) {
88437737Smckusick 			dp = VTOI(ndp->ni_vp);
88512756Ssam 			dp->i_nlink--;
88612756Ssam 			dp->i_flag |= ICHG;
88712756Ssam 		}
88812756Ssam 	}
88912756Ssam bad:
89012756Ssam 	/*
89112756Ssam 	 * No need to do an explicit itrunc here,
89237737Smckusick 	 * vrele will do this for us because we set
89312756Ssam 	 * the link count to 0.
89412756Ssam 	 */
89537737Smckusick 	if (error) {
89612756Ssam 		ip->i_nlink = 0;
89712756Ssam 		ip->i_flag |= ICHG;
89838144Smckusick 		iput(ip);
89938144Smckusick 	} else
90038144Smckusick 		ndp->ni_vp = ITOV(ip);
90112756Ssam 	if (dp)
90212756Ssam 		iput(dp);
90337737Smckusick 	return (error);
90412756Ssam }
90512756Ssam 
90612756Ssam /*
90712756Ssam  * Rmdir system call.
90812756Ssam  */
90937737Smckusick ufs_rmdir(ndp)
91037737Smckusick 	register struct nameidata *ndp;
91112756Ssam {
91212756Ssam 	register struct inode *ip, *dp;
91337737Smckusick 	int error = 0;
91412756Ssam 
91537737Smckusick 	ip = VTOI(ndp->ni_vp);
91637737Smckusick 	dp = VTOI(ndp->ni_dvp);
91712756Ssam 	/*
91812756Ssam 	 * No rmdir "." please.
91912756Ssam 	 */
92012756Ssam 	if (dp == ip) {
92137737Smckusick 		vrele(ITOV(dp));
92212756Ssam 		iput(ip);
92337737Smckusick 		return (EINVAL);
92412756Ssam 	}
92512756Ssam 	/*
92612756Ssam 	 * Verify the directory is empty (and valid).
92712756Ssam 	 * (Rmdir ".." won't be valid since
92812756Ssam 	 *  ".." will contain a reference to
92912756Ssam 	 *  the current directory and thus be
93012756Ssam 	 *  non-empty.)
93112756Ssam 	 */
93237737Smckusick 	if (ip->i_nlink != 2 || !dirempty(ip, dp->i_number, ndp->ni_cred)) {
93337737Smckusick 		error = ENOTEMPTY;
93412756Ssam 		goto out;
93512756Ssam 	}
93612756Ssam 	/*
93712756Ssam 	 * Delete reference to directory before purging
93812756Ssam 	 * inode.  If we crash in between, the directory
93912756Ssam 	 * will be reattached to lost+found,
94012756Ssam 	 */
94137737Smckusick 	if (error = dirremove(ndp))
94212756Ssam 		goto out;
94312756Ssam 	dp->i_nlink--;
94412756Ssam 	dp->i_flag |= ICHG;
94537737Smckusick 	cache_purge(ITOV(dp));
94612756Ssam 	iput(dp);
94737737Smckusick 	ndp->ni_dvp = NULL;
94812756Ssam 	/*
94912756Ssam 	 * Truncate inode.  The only stuff left
95012756Ssam 	 * in the directory is "." and "..".  The
95112756Ssam 	 * "." reference is inconsequential since
95212756Ssam 	 * we're quashing it.  The ".." reference
95312756Ssam 	 * has already been adjusted above.  We've
95412756Ssam 	 * removed the "." reference and the reference
95512756Ssam 	 * in the parent directory, but there may be
95612756Ssam 	 * other hard links so decrement by 2 and
95712756Ssam 	 * worry about them later.
95812756Ssam 	 */
95912756Ssam 	ip->i_nlink -= 2;
96037737Smckusick 	error = itrunc(ip, (u_long)0);
96137737Smckusick 	cache_purge(ITOV(ip));
96212756Ssam out:
96337737Smckusick 	if (ndp->ni_dvp)
96412756Ssam 		iput(dp);
96512756Ssam 	iput(ip);
96637737Smckusick 	return (error);
96712756Ssam }
96812756Ssam 
96937737Smckusick /*
97037737Smckusick  * symlink -- make a symbolic link
97137737Smckusick  */
97237737Smckusick ufs_symlink(ndp, vap, target)
97337737Smckusick 	struct nameidata *ndp;
97437737Smckusick 	struct vattr *vap;
97537737Smckusick 	char *target;
97612756Ssam {
97737737Smckusick 	struct inode *ip;
97837737Smckusick 	int error;
97912756Ssam 
98037737Smckusick 	error = maknode(IFLNK | vap->va_mode, ndp, &ip);
98137737Smckusick 	if (error)
98237737Smckusick 		return (error);
98337737Smckusick 	error = rdwri(UIO_WRITE, ip, target, strlen(target), (off_t)0,
98437737Smckusick 		UIO_SYSSPACE, ndp->ni_cred, (int *)0);
98537737Smckusick 	iput(ip);
98637737Smckusick 	return (error);
98737737Smckusick }
98837737Smckusick 
98937737Smckusick /*
99037737Smckusick  * Vnode op for read and write
99137737Smckusick  */
99237737Smckusick ufs_readdir(vp, uio, offp, cred)
99337737Smckusick 	struct vnode *vp;
99437737Smckusick 	register struct uio *uio;
99537737Smckusick 	off_t *offp;
99637737Smckusick 	struct ucred *cred;
99737737Smckusick {
99837737Smckusick 	register struct inode *ip = VTOI(vp);
99937737Smckusick 	int count, error;
100037737Smckusick 
100137737Smckusick 	ILOCK(ip);
100237737Smckusick 	uio->uio_offset = *offp;
100337737Smckusick 	count = uio->uio_resid;
100437737Smckusick 	count &= ~(DIRBLKSIZ - 1);
100537737Smckusick 	if (vp->v_type != VDIR || uio->uio_iovcnt != 1 ||
100637737Smckusick 	    (count < DIRBLKSIZ) || (uio->uio_offset & (DIRBLKSIZ -1))) {
100737737Smckusick 		IUNLOCK(ip);
100837737Smckusick 		return (EINVAL);
100916540Ssam 	}
101037737Smckusick 	uio->uio_resid = count;
101137737Smckusick 	uio->uio_iov->iov_len = count;
101237737Smckusick 	error = readip(ip, uio, cred);
101337737Smckusick 	*offp += count - uio->uio_resid;
101437737Smckusick 	IUNLOCK(ip);
101537737Smckusick 	return (error);
101637737Smckusick }
101737737Smckusick 
101837737Smckusick /*
101937737Smckusick  * Return target name of a symbolic link
102037737Smckusick  */
102137737Smckusick ufs_readlink(vp, uiop, cred)
102237737Smckusick 	struct vnode *vp;
102337737Smckusick 	struct uio *uiop;
102437737Smckusick 	struct ucred *cred;
102537737Smckusick {
102637737Smckusick 
102737737Smckusick 	return (readip(VTOI(vp), uiop, cred));
102837737Smckusick }
102937737Smckusick 
103037737Smckusick /*
103137737Smckusick  * Ufs abort op, called after namei() when a CREATE/DELETE isn't actually
103237737Smckusick  * done. Iff ni_vp/ni_dvp not null and locked, unlock.
103337737Smckusick  */
103437737Smckusick ufs_abortop(ndp)
103537737Smckusick 	register struct nameidata *ndp;
103637737Smckusick {
103737737Smckusick 	register struct inode *ip;
103837737Smckusick 
103937737Smckusick 	if (ndp->ni_vp) {
104037737Smckusick 		ip = VTOI(ndp->ni_vp);
104137737Smckusick 		if (ip->i_flag & ILOCKED)
104237737Smckusick 			IUNLOCK(ip);
104337737Smckusick 		vrele(ndp->ni_vp);
104412756Ssam 	}
104537737Smckusick 	if (ndp->ni_dvp) {
104637737Smckusick 		ip = VTOI(ndp->ni_dvp);
104737737Smckusick 		if (ip->i_flag & ILOCKED)
104837737Smckusick 			IUNLOCK(ip);
104937737Smckusick 		vrele(ndp->ni_dvp);
105037737Smckusick 	}
105137737Smckusick 	return;
105212756Ssam }
105312756Ssam 
105437737Smckusick ufs_lock(vp)
105537737Smckusick 	struct vnode *vp;
105637737Smckusick {
105737737Smckusick 	register struct inode *ip = VTOI(vp);
105837737Smckusick 
105937737Smckusick 	ILOCK(ip);
106037737Smckusick 	return (0);
106137737Smckusick }
106237737Smckusick 
106337737Smckusick ufs_unlock(vp)
106437737Smckusick 	struct vnode *vp;
106537737Smckusick {
106637737Smckusick 	register struct inode *ip = VTOI(vp);
106737737Smckusick 
106837737Smckusick 	if (!(ip->i_flag & ILOCKED))
106937737Smckusick 		panic("ufs_unlock NOT LOCKED");
107037737Smckusick 	IUNLOCK(ip);
107137737Smckusick 	return (0);
107237737Smckusick }
107337737Smckusick 
107412756Ssam /*
107537737Smckusick  * Get access to bmap
107612756Ssam  */
107737737Smckusick ufs_bmap(vp, bn, vpp, bnp)
107837737Smckusick 	struct vnode *vp;
107937737Smckusick 	daddr_t bn;
108037737Smckusick 	struct vnode **vpp;
108137737Smckusick 	daddr_t *bnp;
108212756Ssam {
108337737Smckusick 	struct inode *ip = VTOI(vp);
108412756Ssam 
108537737Smckusick 	if (vpp != NULL)
108637737Smckusick 		*vpp = ip->i_devvp;
108737737Smckusick 	if (bnp == NULL)
108837737Smckusick 		return (0);
108937737Smckusick 	return (bmap(ip, bn, bnp, (daddr_t *)0, (int *)0));
109012756Ssam }
109137737Smckusick 
109237737Smckusick /*
109337737Smckusick  * Just call the device strategy routine
109437737Smckusick  */
109537737Smckusick ufs_strategy(bp)
109637737Smckusick 	register struct buf *bp;
109737737Smckusick {
109837737Smckusick 	(*bdevsw[major(bp->b_dev)].d_strategy)(bp);
109937737Smckusick 	return (0);
110037737Smckusick }
110137737Smckusick 
110237737Smckusick /*
110337737Smckusick  * Make a new file.
110437737Smckusick  */
110537737Smckusick maknode(mode, ndp, ipp)
110637737Smckusick 	int mode;
110737737Smckusick 	register struct nameidata *ndp;
110837737Smckusick 	struct inode **ipp;
110937737Smckusick {
111037737Smckusick 	register struct inode *ip;
111137737Smckusick 	struct inode *tip;
111237737Smckusick 	register struct inode *pdir = VTOI(ndp->ni_dvp);
111337737Smckusick 	ino_t ipref;
111437737Smckusick 	int error;
111537737Smckusick 
111637737Smckusick 	*ipp = 0;
111737737Smckusick 	if ((mode & IFMT) == IFDIR)
111837737Smckusick 		ipref = dirpref(pdir->i_fs);
111937737Smckusick 	else
112037737Smckusick 		ipref = pdir->i_number;
112137737Smckusick 	error = ialloc(pdir, ipref, mode, &tip);
112237737Smckusick 	if (error) {
112337737Smckusick 		iput(pdir);
112437737Smckusick 		return (error);
112537737Smckusick 	}
112637737Smckusick 	ip = tip;
112737737Smckusick #ifdef QUOTA
112837737Smckusick 	if (ip->i_dquot != NODQUOT)
112937737Smckusick 		panic("maknode: dquot");
113037737Smckusick #endif
113137737Smckusick 	ip->i_flag |= IACC|IUPD|ICHG;
113237737Smckusick 	if ((mode & IFMT) == 0)
113337737Smckusick 		mode |= IFREG;
113437737Smckusick 	ip->i_mode = mode;
113537737Smckusick 	ITOV(ip)->v_type = IFTOVT(mode);	/* Rest init'd in iget() */
113637737Smckusick 	ip->i_nlink = 1;
113737737Smckusick 	ip->i_uid = ndp->ni_cred->cr_uid;
113837737Smckusick 	ip->i_gid = pdir->i_gid;
113937737Smckusick 	if ((ip->i_mode & ISGID) && !groupmember(ip->i_gid, ndp->ni_cred) &&
114037737Smckusick 	    suser(ndp->ni_cred, NULL))
114137737Smckusick 		ip->i_mode &= ~ISGID;
114237737Smckusick #ifdef QUOTA
114337737Smckusick 	ip->i_dquot = inoquota(ip);
114437737Smckusick #endif
114537737Smckusick 
114637737Smckusick 	/*
114737737Smckusick 	 * Make sure inode goes to disk before directory entry.
114837737Smckusick 	 */
114937737Smckusick 	if ((error = iupdat(ip, &time, &time, 1)) ||
115037737Smckusick 	    (error = direnter(ip, ndp))) {
115137737Smckusick 		/*
115237737Smckusick 		 * Write error occurred trying to update the inode
115337737Smckusick 		 * or the directory so must deallocate the inode.
115437737Smckusick 		 */
115537737Smckusick 		ip->i_nlink = 0;
115637737Smckusick 		ip->i_flag |= ICHG;
115737737Smckusick 		iput(ip);
115837737Smckusick 		return (error);
115937737Smckusick 	}
116037737Smckusick 	*ipp = ip;
116137737Smckusick 	return (0);
116237737Smckusick }
1163