xref: /csrg-svn/sys/ufs/lfs/lfs_vnops.c (revision 39597)
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*39597Smckusick  *	@(#)lfs_vnops.c	7.21 (Berkeley) 11/22/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(),
6739391Smckusick 	ufs_reclaim(),
6837737Smckusick 	ufs_lock(),
6937737Smckusick 	ufs_unlock(),
7037737Smckusick 	ufs_bmap(),
7137737Smckusick 	ufs_strategy();
726254Sroot 
7337737Smckusick struct vnodeops ufs_vnodeops = {
7437737Smckusick 	ufs_lookup,
7537737Smckusick 	ufs_create,
7637737Smckusick 	ufs_mknod,
7737737Smckusick 	ufs_open,
7837737Smckusick 	ufs_close,
7937737Smckusick 	ufs_access,
8037737Smckusick 	ufs_getattr,
8137737Smckusick 	ufs_setattr,
8237737Smckusick 	ufs_read,
8337737Smckusick 	ufs_write,
8437737Smckusick 	ufs_ioctl,
8537737Smckusick 	ufs_select,
8637737Smckusick 	ufs_mmap,
8737737Smckusick 	ufs_fsync,
8837737Smckusick 	ufs_seek,
8937737Smckusick 	ufs_remove,
9037737Smckusick 	ufs_link,
9137737Smckusick 	ufs_rename,
9237737Smckusick 	ufs_mkdir,
9337737Smckusick 	ufs_rmdir,
9437737Smckusick 	ufs_symlink,
9537737Smckusick 	ufs_readdir,
9637737Smckusick 	ufs_readlink,
9737737Smckusick 	ufs_abortop,
9837737Smckusick 	ufs_inactive,
9939391Smckusick 	ufs_reclaim,
10037737Smckusick 	ufs_lock,
10137737Smckusick 	ufs_unlock,
10237737Smckusick 	ufs_bmap,
10337737Smckusick 	ufs_strategy,
10437737Smckusick };
1056254Sroot 
10639435Smckusick int	spec_lookup(),
10739435Smckusick 	spec_open(),
10839435Smckusick 	spec_read(),
10939435Smckusick 	spec_write(),
11039435Smckusick 	spec_strategy(),
11139435Smckusick 	spec_ioctl(),
11239435Smckusick 	spec_select(),
11339435Smckusick 	spec_close(),
11439435Smckusick 	spec_badop(),
11539435Smckusick 	spec_nullop();
11639435Smckusick 
11739435Smckusick struct vnodeops spec_inodeops = {
118*39597Smckusick 	spec_lookup,		/* lookup */
119*39597Smckusick 	spec_badop,		/* create */
120*39597Smckusick 	spec_badop,		/* mknod */
121*39597Smckusick 	spec_open,		/* open */
122*39597Smckusick 	spec_close,		/* close */
123*39597Smckusick 	ufs_access,		/* access */
124*39597Smckusick 	ufs_getattr,		/* getattr */
125*39597Smckusick 	ufs_setattr,		/* setattr */
126*39597Smckusick 	spec_read,		/* read */
127*39597Smckusick 	spec_write,		/* write */
128*39597Smckusick 	spec_ioctl,		/* ioctl */
129*39597Smckusick 	spec_select,		/* select */
130*39597Smckusick 	spec_badop,		/* mmap */
131*39597Smckusick 	spec_nullop,		/* fsync */
132*39597Smckusick 	spec_badop,		/* seek */
133*39597Smckusick 	spec_badop,		/* remove */
134*39597Smckusick 	spec_badop,		/* link */
135*39597Smckusick 	spec_badop,		/* rename */
136*39597Smckusick 	spec_badop,		/* mkdir */
137*39597Smckusick 	spec_badop,		/* rmdir */
138*39597Smckusick 	spec_badop,		/* symlink */
139*39597Smckusick 	spec_badop,		/* readdir */
140*39597Smckusick 	spec_badop,		/* readlink */
141*39597Smckusick 	spec_badop,		/* abortop */
142*39597Smckusick 	ufs_inactive,		/* inactive */
143*39597Smckusick 	ufs_reclaim,		/* reclaim */
144*39597Smckusick 	ufs_lock,		/* lock */
145*39597Smckusick 	ufs_unlock,		/* unlock */
146*39597Smckusick 	spec_badop,		/* bmap */
147*39597Smckusick 	spec_strategy,		/* strategy */
14839435Smckusick };
14939435Smckusick 
15037737Smckusick enum vtype iftovt_tab[8] = {
15137737Smckusick 	VNON, VCHR, VDIR, VBLK, VREG, VLNK, VSOCK, VBAD,
15237737Smckusick };
15337737Smckusick int	vttoif_tab[8] = {
15437737Smckusick 	0, IFREG, IFDIR, IFBLK, IFCHR, IFLNK, IFSOCK, IFMT,
15537737Smckusick };
1566254Sroot 
1579167Ssam /*
15837737Smckusick  * Create a regular file
1599167Ssam  */
16037737Smckusick ufs_create(ndp, vap)
16137737Smckusick 	struct nameidata *ndp;
16237737Smckusick 	struct vattr *vap;
1636254Sroot {
16437737Smckusick 	struct inode *ip;
16537737Smckusick 	int error;
1666254Sroot 
16737737Smckusick 	if (error = maknode(MAKEIMODE(vap->va_type, vap->va_mode), ndp, &ip))
16837737Smckusick 		return (error);
16937737Smckusick 	ndp->ni_vp = ITOV(ip);
17037737Smckusick 	return (0);
1716254Sroot }
1726254Sroot 
17337Sbill /*
17437737Smckusick  * Mknod vnode call
1756254Sroot  */
17637737Smckusick /* ARGSUSED */
17737737Smckusick ufs_mknod(ndp, vap, cred)
17837737Smckusick 	struct nameidata *ndp;
17937737Smckusick 	struct ucred *cred;
18037737Smckusick 	struct vattr *vap;
1816254Sroot {
18239435Smckusick 	register struct vnode *vp;
18337737Smckusick 	struct inode *ip;
18437737Smckusick 	int error;
1856254Sroot 
18637737Smckusick 	if (error = maknode(MAKEIMODE(vap->va_type, vap->va_mode), ndp, &ip))
18737737Smckusick 		return (error);
18839435Smckusick 	vp = ITOV(ip);
18937737Smckusick 	if (vap->va_rdev) {
19037737Smckusick 		/*
19137737Smckusick 		 * Want to be able to use this to make badblock
19237737Smckusick 		 * inodes, so don't truncate the dev number.
19337737Smckusick 		 */
19439435Smckusick 		vp->v_rdev = ip->i_rdev = vap->va_rdev;
19537737Smckusick 		ip->i_flag |= IACC|IUPD|ICHG;
19612756Ssam 	}
19737737Smckusick 	/*
19837737Smckusick 	 * Remove inode so that it will be reloaded by iget and
19937737Smckusick 	 * checked to see if it is an alias of an existing entry
20037737Smckusick 	 * in the inode cache.
20137737Smckusick 	 */
20238809Smckusick 	iput(ip);
20339435Smckusick 	vp->v_type = VNON;
20439435Smckusick 	vgone(vp);
20537737Smckusick 	return (0);
2066254Sroot }
2076254Sroot 
2086254Sroot /*
20937737Smckusick  * Open called.
21037737Smckusick  *
21137737Smckusick  * Nothing to do.
2126254Sroot  */
21337737Smckusick /* ARGSUSED */
21437737Smckusick ufs_open(vp, mode, cred)
21537737Smckusick 	struct vnode *vp;
21637737Smckusick 	int mode;
21737737Smckusick 	struct ucred *cred;
2186254Sroot {
2196254Sroot 
22037737Smckusick 	return (0);
2216254Sroot }
2226254Sroot 
2236254Sroot /*
22437737Smckusick  * Close called
22537737Smckusick  *
22637737Smckusick  * Update the times on the inode.
2276254Sroot  */
22837737Smckusick /* ARGSUSED */
22937737Smckusick ufs_close(vp, fflag, cred)
23037737Smckusick 	struct vnode *vp;
23137737Smckusick 	int fflag;
23237737Smckusick 	struct ucred *cred;
2336254Sroot {
23437737Smckusick 	register struct inode *ip = VTOI(vp);
2356254Sroot 
23637737Smckusick 	if (vp->v_count > 1 && !(ip->i_flag & ILOCKED))
23737737Smckusick 		ITIMES(ip, &time, &time);
23837737Smckusick 	return (0);
2396254Sroot }
2406254Sroot 
24137737Smckusick ufs_access(vp, mode, cred)
24237737Smckusick 	struct vnode *vp;
24337737Smckusick 	int mode;
24437737Smckusick 	struct ucred *cred;
2456254Sroot {
2466254Sroot 
24737737Smckusick 	return (iaccess(VTOI(vp), mode, cred));
2486254Sroot }
2496254Sroot 
25037737Smckusick /* ARGSUSED */
25137737Smckusick ufs_getattr(vp, vap, cred)
25237737Smckusick 	struct vnode *vp;
25337737Smckusick 	register struct vattr *vap;
25437737Smckusick 	struct ucred *cred;
2556254Sroot {
25637737Smckusick 	register struct inode *ip = VTOI(vp);
2576254Sroot 
25837737Smckusick 	ITIMES(ip, &time, &time);
2596254Sroot 	/*
26037737Smckusick 	 * Copy from inode table
2616254Sroot 	 */
26237737Smckusick 	vap->va_fsid = ip->i_dev;
26337737Smckusick 	vap->va_fileid = ip->i_number;
26437737Smckusick 	vap->va_mode = ip->i_mode & ~IFMT;
26537737Smckusick 	vap->va_nlink = ip->i_nlink;
26637737Smckusick 	vap->va_uid = ip->i_uid;
26737737Smckusick 	vap->va_gid = ip->i_gid;
26837737Smckusick 	vap->va_rdev = (dev_t)ip->i_rdev;
26939391Smckusick 	vap->va_size = ip->i_din.di_qsize.val[0];
27039391Smckusick 	vap->va_size1 = ip->i_din.di_qsize.val[1];
27137737Smckusick 	vap->va_atime.tv_sec = ip->i_atime;
27238578Smckusick 	vap->va_atime.tv_usec = 0;
27337737Smckusick 	vap->va_mtime.tv_sec = ip->i_mtime;
27438578Smckusick 	vap->va_mtime.tv_usec = 0;
27537737Smckusick 	vap->va_ctime.tv_sec = ip->i_ctime;
27638578Smckusick 	vap->va_ctime.tv_usec = 0;
27738254Smckusick 	vap->va_flags = ip->i_flags;
27838254Smckusick 	vap->va_gen = ip->i_gen;
27937737Smckusick 	/* this doesn't belong here */
28037737Smckusick 	if (vp->v_type == VBLK)
28137737Smckusick 		vap->va_blocksize = BLKDEV_IOSIZE;
28237737Smckusick 	else if (vp->v_type == VCHR)
28337737Smckusick 		vap->va_blocksize = MAXBSIZE;
2847142Smckusick 	else
28537737Smckusick 		vap->va_blocksize = ip->i_fs->fs_bsize;
28638657Smckusick 	vap->va_bytes = dbtob(ip->i_blocks);
28737737Smckusick 	vap->va_bytes1 = -1;
28837737Smckusick 	vap->va_type = vp->v_type;
28937737Smckusick 	return (0);
2906254Sroot }
2916254Sroot 
2926254Sroot /*
29337737Smckusick  * Set attribute vnode op. called from several syscalls
2946254Sroot  */
29537737Smckusick ufs_setattr(vp, vap, cred)
29637737Smckusick 	register struct vnode *vp;
29737737Smckusick 	register struct vattr *vap;
29837737Smckusick 	register struct ucred *cred;
2996254Sroot {
30037737Smckusick 	register struct inode *ip = VTOI(vp);
30137737Smckusick 	int error = 0;
3026254Sroot 
30337737Smckusick 	/*
30437737Smckusick 	 * Check for unsetable attributes.
30537737Smckusick 	 */
30637737Smckusick 	if ((vap->va_type != VNON) || (vap->va_nlink != VNOVAL) ||
30737737Smckusick 	    (vap->va_fsid != VNOVAL) || (vap->va_fileid != VNOVAL) ||
30837737Smckusick 	    (vap->va_blocksize != VNOVAL) || (vap->va_rdev != VNOVAL) ||
30938254Smckusick 	    ((int)vap->va_bytes != VNOVAL) || (vap->va_gen != VNOVAL)) {
31037737Smckusick 		return (EINVAL);
31116540Ssam 	}
31237737Smckusick 	/*
31337737Smckusick 	 * Go through the fields and update iff not VNOVAL.
31437737Smckusick 	 */
31537737Smckusick 	if (vap->va_uid != (u_short)VNOVAL || vap->va_gid != (u_short)VNOVAL)
31637737Smckusick 		if (error = chown1(vp, vap->va_uid, vap->va_gid, cred))
31737737Smckusick 			return (error);
31837737Smckusick 	if (vap->va_size != VNOVAL) {
31937737Smckusick 		if (vp->v_type == VDIR)
32037737Smckusick 			return (EISDIR);
32137737Smckusick 		if (error = itrunc(ip, vap->va_size))
32237737Smckusick 			return (error);
32313878Ssam 	}
32437737Smckusick 	if (vap->va_atime.tv_sec != VNOVAL || vap->va_mtime.tv_sec != VNOVAL) {
32537773Smckusick 		if (cred->cr_uid != ip->i_uid &&
32637773Smckusick 		    (error = suser(cred, &u.u_acflag)))
32737773Smckusick 			return (error);
32837737Smckusick 		if (vap->va_atime.tv_sec != VNOVAL)
32937737Smckusick 			ip->i_flag |= IACC;
33037737Smckusick 		if (vap->va_mtime.tv_sec != VNOVAL)
33137737Smckusick 			ip->i_flag |= IUPD;
33237737Smckusick 		ip->i_flag |= ICHG;
33337737Smckusick 		if (error = iupdat(ip, &vap->va_atime, &vap->va_mtime, 1))
33437737Smckusick 			return (error);
3356254Sroot 	}
33637737Smckusick 	if (vap->va_mode != (u_short)VNOVAL)
33737737Smckusick 		error = chmod1(vp, (int)vap->va_mode, cred);
33838254Smckusick 	if (vap->va_flags != VNOVAL) {
33938254Smckusick 		if (cred->cr_uid != ip->i_uid &&
34038254Smckusick 		    (error = suser(cred, &u.u_acflag)))
34138254Smckusick 			return (error);
34238254Smckusick 		if (cred->cr_uid == 0) {
34338254Smckusick 			ip->i_flags = vap->va_flags;
34438254Smckusick 		} else {
34538254Smckusick 			ip->i_flags &= 0xffff0000;
34638254Smckusick 			ip->i_flags |= (vap->va_flags & 0xffff);
34738254Smckusick 		}
34838254Smckusick 		ip->i_flag |= ICHG;
34938254Smckusick 	}
35037737Smckusick 	return (error);
3516254Sroot }
3526254Sroot 
3536254Sroot /*
3549167Ssam  * Change the mode on a file.
3559167Ssam  * Inode must be locked before calling.
3569167Ssam  */
35737737Smckusick chmod1(vp, mode, cred)
35837737Smckusick 	register struct vnode *vp;
3597701Ssam 	register int mode;
36037737Smckusick 	struct ucred *cred;
3617701Ssam {
36237737Smckusick 	register struct inode *ip = VTOI(vp);
36337773Smckusick 	int error;
3647868Sroot 
36537773Smckusick 	if (cred->cr_uid != ip->i_uid &&
36637773Smckusick 	    (error = suser(cred, &u.u_acflag)))
36737773Smckusick 		return (error);
3686254Sroot 	ip->i_mode &= ~07777;
36937737Smckusick 	if (cred->cr_uid) {
37037737Smckusick 		if (vp->v_type != VDIR)
37121015Smckusick 			mode &= ~ISVTX;
37237737Smckusick 		if (!groupmember(ip->i_gid, cred))
37311811Ssam 			mode &= ~ISGID;
3747439Sroot 	}
37537737Smckusick 	ip->i_mode |= mode & 07777;
3766254Sroot 	ip->i_flag |= ICHG;
37737737Smckusick 	if ((vp->v_flag & VTEXT) && (ip->i_mode & ISVTX) == 0)
37837737Smckusick 		xrele(vp);
37921015Smckusick 	return (0);
3805992Swnj }
3815992Swnj 
3829167Ssam /*
3837701Ssam  * Perform chown operation on inode ip;
3847701Ssam  * inode must be locked prior to call.
3857701Ssam  */
38637737Smckusick chown1(vp, uid, gid, cred)
38737737Smckusick 	register struct vnode *vp;
38837737Smckusick 	uid_t uid;
38937737Smckusick 	gid_t gid;
39037737Smckusick 	struct ucred *cred;
3917701Ssam {
39237737Smckusick 	register struct inode *ip = VTOI(vp);
3937701Ssam #ifdef QUOTA
3947701Ssam 	register long change;
39511811Ssam #endif
39637737Smckusick 	int error;
3977701Ssam 
39837737Smckusick 	if (uid == (u_short)VNOVAL)
39911811Ssam 		uid = ip->i_uid;
40037737Smckusick 	if (gid == (u_short)VNOVAL)
40111811Ssam 		gid = ip->i_gid;
40236614Sbostic 	/*
40336614Sbostic 	 * If we don't own the file, are trying to change the owner
40436614Sbostic 	 * of the file, or are not a member of the target group,
40536614Sbostic 	 * the caller must be superuser or the call fails.
40636614Sbostic 	 */
40737737Smckusick 	if ((cred->cr_uid != ip->i_uid || uid != ip->i_uid ||
40837737Smckusick 	    !groupmember((gid_t)gid, cred)) &&
40937737Smckusick 	    (error = suser(cred, &u.u_acflag)))
41037737Smckusick 		return (error);
41111811Ssam #ifdef QUOTA
41214385Ssam 	if (ip->i_uid == uid)		/* this just speeds things a little */
4137482Skre 		change = 0;
41412646Ssam 	else
41512646Ssam 		change = ip->i_blocks;
41612646Ssam 	(void) chkdq(ip, -change, 1);
41712646Ssam 	(void) chkiq(ip->i_dev, ip, ip->i_uid, 1);
4187482Skre 	dqrele(ip->i_dquot);
4197482Skre #endif
42039306Smckusick 	if (ip->i_uid != uid && cred->cr_uid != 0)
42139306Smckusick 		ip->i_mode &= ~ISUID;
42239306Smckusick 	if (ip->i_gid != gid && cred->cr_uid != 0)
42339306Smckusick 		ip->i_mode &= ~ISGID;
42411811Ssam 	ip->i_uid = uid;
42511811Ssam 	ip->i_gid = gid;
4266254Sroot 	ip->i_flag |= ICHG;
4277701Ssam #ifdef QUOTA
4287482Skre 	ip->i_dquot = inoquota(ip);
42912646Ssam 	(void) chkdq(ip, change, 1);
43026361Skarels 	(void) chkiq(ip->i_dev, (struct inode *)NULL, (uid_t)uid, 1);
43112646Ssam 	return (u.u_error);		/* should == 0 ALWAYS !! */
43212646Ssam #else
43312646Ssam 	return (0);
4347482Skre #endif
43537Sbill }
43637Sbill 
43737737Smckusick /* ARGSUSED */
43837737Smckusick ufs_ioctl(vp, com, data, fflag, cred)
43937737Smckusick 	struct vnode *vp;
44037737Smckusick 	int com;
44137737Smckusick 	caddr_t data;
44237737Smckusick 	int fflag;
44337737Smckusick 	struct ucred *cred;
44411811Ssam {
44511811Ssam 
44637737Smckusick 	printf("ufs_ioctl called with type %d\n", vp->v_type);
44737737Smckusick 	return (ENOTTY);
44811811Ssam }
44911811Ssam 
45037737Smckusick /* ARGSUSED */
45137737Smckusick ufs_select(vp, which, cred)
45237737Smckusick 	struct vnode *vp;
45337737Smckusick 	int which;
45437737Smckusick 	struct ucred *cred;
45537737Smckusick {
45637737Smckusick 
45737737Smckusick 	printf("ufs_select called with type %d\n", vp->v_type);
45837737Smckusick 	return (1);		/* XXX */
45937737Smckusick }
46037737Smckusick 
4619167Ssam /*
46237737Smckusick  * Mmap a file
46337737Smckusick  *
46437737Smckusick  * NB Currently unsupported.
4659167Ssam  */
46637737Smckusick /* ARGSUSED */
46737737Smckusick ufs_mmap(vp, fflags, cred)
46837737Smckusick 	struct vnode *vp;
46937737Smckusick 	int fflags;
47037737Smckusick 	struct ucred *cred;
47137Sbill {
47237Sbill 
47337737Smckusick 	return (EINVAL);
47437Sbill }
4757535Sroot 
4769167Ssam /*
47737737Smckusick  * Synch an open file.
4789167Ssam  */
47937737Smckusick /* ARGSUSED */
480*39597Smckusick ufs_fsync(vp, fflags, cred, waitfor)
48137737Smckusick 	struct vnode *vp;
48237737Smckusick 	int fflags;
48337737Smckusick 	struct ucred *cred;
484*39597Smckusick 	int waitfor;
4857701Ssam {
486*39597Smckusick 	struct inode *ip = VTOI(vp);
4877701Ssam 
48837737Smckusick 	if (fflags&FWRITE)
48937737Smckusick 		ip->i_flag |= ICHG;
490*39597Smckusick 	return (syncip(ip, waitfor));
4917701Ssam }
4927701Ssam 
4939167Ssam /*
49437737Smckusick  * Seek on a file
49537737Smckusick  *
49637737Smckusick  * Nothing to do, so just return.
4979167Ssam  */
49837737Smckusick /* ARGSUSED */
49937737Smckusick ufs_seek(vp, oldoff, newoff, cred)
50037737Smckusick 	struct vnode *vp;
50137737Smckusick 	off_t oldoff, newoff;
50237737Smckusick 	struct ucred *cred;
5037701Ssam {
5047701Ssam 
50537737Smckusick 	return (0);
50637737Smckusick }
50737737Smckusick 
50837737Smckusick /*
50937737Smckusick  * ufs remove
51037737Smckusick  * Hard to avoid races here, especially
51137737Smckusick  * in unlinking directories.
51237737Smckusick  */
51337737Smckusick ufs_remove(ndp)
51437737Smckusick 	struct nameidata *ndp;
51537737Smckusick {
51637737Smckusick 	register struct inode *ip, *dp;
51737737Smckusick 	int error;
51837737Smckusick 
51937737Smckusick 	ip = VTOI(ndp->ni_vp);
52037737Smckusick 	dp = VTOI(ndp->ni_dvp);
52137737Smckusick 	error = dirremove(ndp);
52237737Smckusick 	if (!error) {
52337737Smckusick 		ip->i_nlink--;
52437737Smckusick 		ip->i_flag |= ICHG;
5257701Ssam 	}
52637737Smckusick 	if (dp == ip)
52737737Smckusick 		vrele(ITOV(ip));
52837737Smckusick 	else
52937737Smckusick 		iput(ip);
53037737Smckusick 	iput(dp);
53137737Smckusick 	return (error);
5327701Ssam }
5337701Ssam 
5349167Ssam /*
53537737Smckusick  * link vnode call
5369167Ssam  */
53737737Smckusick ufs_link(vp, ndp)
53837737Smckusick 	register struct vnode *vp;
53937737Smckusick 	register struct nameidata *ndp;
5409167Ssam {
54137737Smckusick 	register struct inode *ip = VTOI(vp);
54237737Smckusick 	int error;
5439167Ssam 
54437737Smckusick 	if (ndp->ni_dvp != vp)
54537737Smckusick 		ILOCK(ip);
54637737Smckusick 	if (ip->i_nlink == LINK_MAX - 1) {
54737737Smckusick 		error = EMLINK;
54837737Smckusick 		goto out;
54937737Smckusick 	}
55037737Smckusick 	ip->i_nlink++;
55137737Smckusick 	ip->i_flag |= ICHG;
55237737Smckusick 	error = iupdat(ip, &time, &time, 1);
55337737Smckusick 	if (!error)
55437737Smckusick 		error = direnter(ip, ndp);
55537737Smckusick out:
55637737Smckusick 	if (ndp->ni_dvp != vp)
55737737Smckusick 		IUNLOCK(ip);
55837737Smckusick 	if (error) {
55937737Smckusick 		ip->i_nlink--;
56030598Smckusick 		ip->i_flag |= ICHG;
56137737Smckusick 	}
56237737Smckusick 	return (error);
5639167Ssam }
5649167Ssam 
5659167Ssam /*
5669167Ssam  * Rename system call.
5679167Ssam  * 	rename("foo", "bar");
5689167Ssam  * is essentially
5699167Ssam  *	unlink("bar");
5709167Ssam  *	link("foo", "bar");
5719167Ssam  *	unlink("foo");
5729167Ssam  * but ``atomically''.  Can't do full commit without saving state in the
5739167Ssam  * inode on disk which isn't feasible at this time.  Best we can do is
5749167Ssam  * always guarantee the target exists.
5759167Ssam  *
5769167Ssam  * Basic algorithm is:
5779167Ssam  *
5789167Ssam  * 1) Bump link count on source while we're linking it to the
57937737Smckusick  *    target.  This also ensure the inode won't be deleted out
58016776Smckusick  *    from underneath us while we work (it may be truncated by
58116776Smckusick  *    a concurrent `trunc' or `open' for creation).
5829167Ssam  * 2) Link source to destination.  If destination already exists,
5839167Ssam  *    delete it first.
58416776Smckusick  * 3) Unlink source reference to inode if still around. If a
58516776Smckusick  *    directory was moved and the parent of the destination
5869167Ssam  *    is different from the source, patch the ".." entry in the
5879167Ssam  *    directory.
5889167Ssam  */
58937737Smckusick ufs_rename(fndp, tndp)
59037737Smckusick 	register struct nameidata *fndp, *tndp;
5917701Ssam {
5929167Ssam 	register struct inode *ip, *xp, *dp;
59316776Smckusick 	struct dirtemplate dirbuf;
59416776Smckusick 	int doingdirectory = 0, oldparent = 0, newparent = 0;
59510051Ssam 	int error = 0;
5967701Ssam 
59737737Smckusick 	dp = VTOI(fndp->ni_dvp);
59837737Smckusick 	ip = VTOI(fndp->ni_vp);
59937737Smckusick 	ILOCK(ip);
6009167Ssam 	if ((ip->i_mode&IFMT) == IFDIR) {
60137737Smckusick 		register struct direct *d = &fndp->ni_dent;
6029167Ssam 
6039167Ssam 		/*
60411641Ssam 		 * Avoid ".", "..", and aliases of "." for obvious reasons.
6059167Ssam 		 */
60637737Smckusick 		if ((d->d_namlen == 1 && d->d_name[0] == '.') || dp == ip ||
60737737Smckusick 		    fndp->ni_isdotdot || (ip->i_flag & IRENAME)) {
60837737Smckusick 			IUNLOCK(ip);
60937737Smckusick 			ufs_abortop(fndp);
61037737Smckusick 			ufs_abortop(tndp);
61137737Smckusick 			return (EINVAL);
6129167Ssam 		}
61316776Smckusick 		ip->i_flag |= IRENAME;
6149167Ssam 		oldparent = dp->i_number;
6159167Ssam 		doingdirectory++;
6169167Ssam 	}
61737737Smckusick 	vrele(fndp->ni_dvp);
6189167Ssam 
6199167Ssam 	/*
6209167Ssam 	 * 1) Bump link count while we're moving stuff
6219167Ssam 	 *    around.  If we crash somewhere before
6229167Ssam 	 *    completing our work, the link count
6239167Ssam 	 *    may be wrong, but correctable.
6249167Ssam 	 */
6259167Ssam 	ip->i_nlink++;
6269167Ssam 	ip->i_flag |= ICHG;
62737737Smckusick 	error = iupdat(ip, &time, &time, 1);
62816664Smckusick 	IUNLOCK(ip);
6299167Ssam 
6309167Ssam 	/*
6319167Ssam 	 * When the target exists, both the directory
63237737Smckusick 	 * and target vnodes are returned locked.
6339167Ssam 	 */
63437737Smckusick 	dp = VTOI(tndp->ni_dvp);
63537737Smckusick 	xp = NULL;
63637737Smckusick 	if (tndp->ni_vp)
63737737Smckusick 		xp = VTOI(tndp->ni_vp);
6389167Ssam 	/*
63911641Ssam 	 * If ".." must be changed (ie the directory gets a new
64012816Smckusick 	 * parent) then the source directory must not be in the
64112816Smckusick 	 * directory heirarchy above the target, as this would
64212816Smckusick 	 * orphan everything below the source directory. Also
64312816Smckusick 	 * the user must have write permission in the source so
64412816Smckusick 	 * as to be able to change "..". We must repeat the call
64512816Smckusick 	 * to namei, as the parent directory is unlocked by the
64612816Smckusick 	 * call to checkpath().
64711641Ssam 	 */
64816776Smckusick 	if (oldparent != dp->i_number)
64916776Smckusick 		newparent = dp->i_number;
65016776Smckusick 	if (doingdirectory && newparent) {
65137737Smckusick 		if (error = iaccess(ip, IWRITE, tndp->ni_cred))
65212816Smckusick 			goto bad;
65337737Smckusick 		tndp->ni_nameiop = RENAME | LOCKPARENT | LOCKLEAF | NOCACHE;
65412816Smckusick 		do {
65537737Smckusick 			dp = VTOI(tndp->ni_dvp);
65612816Smckusick 			if (xp != NULL)
65738069Smckusick 				iput(xp);
65837737Smckusick 			if (error = checkpath(ip, dp, tndp->ni_cred))
65912816Smckusick 				goto out;
66037737Smckusick 			if (error = namei(tndp))
66112816Smckusick 				goto out;
66237737Smckusick 			xp = NULL;
66337737Smckusick 			if (tndp->ni_vp)
66437737Smckusick 				xp = VTOI(tndp->ni_vp);
66537737Smckusick 		} while (dp != VTOI(tndp->ni_dvp));
66612816Smckusick 	}
66711641Ssam 	/*
6689167Ssam 	 * 2) If target doesn't exist, link the target
6699167Ssam 	 *    to the source and unlink the source.
6709167Ssam 	 *    Otherwise, rewrite the target directory
6719167Ssam 	 *    entry to reference the source inode and
6729167Ssam 	 *    expunge the original entry's existence.
6739167Ssam 	 */
6749167Ssam 	if (xp == NULL) {
67537737Smckusick 		if (dp->i_dev != ip->i_dev)
67637737Smckusick 			panic("rename: EXDEV");
6779167Ssam 		/*
67816776Smckusick 		 * Account for ".." in new directory.
67916776Smckusick 		 * When source and destination have the same
68016776Smckusick 		 * parent we don't fool with the link count.
6819167Ssam 		 */
68216776Smckusick 		if (doingdirectory && newparent) {
6839167Ssam 			dp->i_nlink++;
6849167Ssam 			dp->i_flag |= ICHG;
68537737Smckusick 			error = iupdat(dp, &time, &time, 1);
6869167Ssam 		}
68737737Smckusick 		if (error = direnter(ip, tndp))
6889167Ssam 			goto out;
6899167Ssam 	} else {
69037737Smckusick 		if (xp->i_dev != dp->i_dev || xp->i_dev != ip->i_dev)
69137737Smckusick 			panic("rename: EXDEV");
6929167Ssam 		/*
69310590Ssam 		 * Short circuit rename(foo, foo).
69410590Ssam 		 */
69510590Ssam 		if (xp->i_number == ip->i_number)
69637737Smckusick 			panic("rename: same file");
69710590Ssam 		/*
69824433Sbloom 		 * If the parent directory is "sticky", then the user must
69924433Sbloom 		 * own the parent directory, or the destination of the rename,
70024433Sbloom 		 * otherwise the destination may not be changed (except by
70124433Sbloom 		 * root). This implements append-only directories.
70224433Sbloom 		 */
70337737Smckusick 		if ((dp->i_mode & ISVTX) && tndp->ni_cred->cr_uid != 0 &&
70437737Smckusick 		    tndp->ni_cred->cr_uid != dp->i_uid &&
70537737Smckusick 		    xp->i_uid != tndp->ni_cred->cr_uid) {
70624433Sbloom 			error = EPERM;
70724433Sbloom 			goto bad;
70824433Sbloom 		}
70924433Sbloom 		/*
71010051Ssam 		 * Target must be empty if a directory
71110051Ssam 		 * and have no links to it.
7129167Ssam 		 * Also, insure source and target are
7139167Ssam 		 * compatible (both directories, or both
7149167Ssam 		 * not directories).
7159167Ssam 		 */
7169167Ssam 		if ((xp->i_mode&IFMT) == IFDIR) {
71737737Smckusick 			if (!dirempty(xp, dp->i_number, tndp->ni_cred) ||
71837737Smckusick 			    xp->i_nlink > 2) {
71910051Ssam 				error = ENOTEMPTY;
7209167Ssam 				goto bad;
7219167Ssam 			}
7229167Ssam 			if (!doingdirectory) {
72310051Ssam 				error = ENOTDIR;
7249167Ssam 				goto bad;
7259167Ssam 			}
72637737Smckusick 			cache_purge(ITOV(dp));
7279167Ssam 		} else if (doingdirectory) {
72810051Ssam 			error = EISDIR;
7299167Ssam 			goto bad;
7309167Ssam 		}
73137737Smckusick 		if (error = dirrewrite(dp, ip, tndp))
73237737Smckusick 			goto bad;
73337737Smckusick 		vput(ITOV(dp));
7349167Ssam 		/*
73510051Ssam 		 * Adjust the link count of the target to
73610051Ssam 		 * reflect the dirrewrite above.  If this is
73710051Ssam 		 * a directory it is empty and there are
73810051Ssam 		 * no links to it, so we can squash the inode and
73910051Ssam 		 * any space associated with it.  We disallowed
74010051Ssam 		 * renaming over top of a directory with links to
74116776Smckusick 		 * it above, as the remaining link would point to
74216776Smckusick 		 * a directory without "." or ".." entries.
7439167Ssam 		 */
74410051Ssam 		xp->i_nlink--;
7459167Ssam 		if (doingdirectory) {
74610051Ssam 			if (--xp->i_nlink != 0)
74710051Ssam 				panic("rename: linked directory");
74837737Smckusick 			error = itrunc(xp, (u_long)0);
74910051Ssam 		}
7509167Ssam 		xp->i_flag |= ICHG;
75138398Smckusick 		iput(xp);
75210246Ssam 		xp = NULL;
7539167Ssam 	}
7549167Ssam 
7559167Ssam 	/*
7569167Ssam 	 * 3) Unlink the source.
7579167Ssam 	 */
75837737Smckusick 	fndp->ni_nameiop = DELETE | LOCKPARENT | LOCKLEAF;
75937737Smckusick 	(void)namei(fndp);
76037737Smckusick 	if (fndp->ni_vp != NULL) {
76137737Smckusick 		xp = VTOI(fndp->ni_vp);
76237737Smckusick 		dp = VTOI(fndp->ni_dvp);
76337737Smckusick 	} else {
76438069Smckusick 		if (fndp->ni_dvp != NULL)
76538069Smckusick 			vput(fndp->ni_dvp);
76637737Smckusick 		xp = NULL;
76717758Smckusick 		dp = NULL;
76837737Smckusick 	}
7699167Ssam 	/*
77037737Smckusick 	 * Ensure that the directory entry still exists and has not
77116776Smckusick 	 * changed while the new name has been entered. If the source is
77216776Smckusick 	 * a file then the entry may have been unlinked or renamed. In
77316776Smckusick 	 * either case there is no further work to be done. If the source
77416776Smckusick 	 * is a directory then it cannot have been rmdir'ed; its link
77516776Smckusick 	 * count of three would cause a rmdir to fail with ENOTEMPTY.
77637737Smckusick 	 * The IRENAME flag ensures that it cannot be moved by another
77716776Smckusick 	 * rename.
7789167Ssam 	 */
77917758Smckusick 	if (xp != ip) {
78016776Smckusick 		if (doingdirectory)
78117758Smckusick 			panic("rename: lost dir entry");
78216776Smckusick 	} else {
7839167Ssam 		/*
78416776Smckusick 		 * If the source is a directory with a
78516776Smckusick 		 * new parent, the link count of the old
78616776Smckusick 		 * parent directory must be decremented
78716776Smckusick 		 * and ".." set to point to the new parent.
7889167Ssam 		 */
78916776Smckusick 		if (doingdirectory && newparent) {
7909167Ssam 			dp->i_nlink--;
7919167Ssam 			dp->i_flag |= ICHG;
792*39597Smckusick 			error = vn_rdwr(UIO_READ, ITOV(xp), (caddr_t)&dirbuf,
79337737Smckusick 				sizeof (struct dirtemplate), (off_t)0,
794*39597Smckusick 				UIO_SYSSPACE, IO_NODELOCKED,
795*39597Smckusick 				tndp->ni_cred, (int *)0);
79616776Smckusick 			if (error == 0) {
79716776Smckusick 				if (dirbuf.dotdot_namlen != 2 ||
79816776Smckusick 				    dirbuf.dotdot_name[0] != '.' ||
79916776Smckusick 				    dirbuf.dotdot_name[1] != '.') {
80016776Smckusick 					printf("rename: mangled dir\n");
80116776Smckusick 				} else {
80216776Smckusick 					dirbuf.dotdot_ino = newparent;
803*39597Smckusick 					(void) vn_rdwr(UIO_WRITE, ITOV(xp),
80416776Smckusick 					    (caddr_t)&dirbuf,
80516776Smckusick 					    sizeof (struct dirtemplate),
80637740Smckusick 					    (off_t)0, UIO_SYSSPACE,
807*39597Smckusick 					    IO_NODELOCKED|IO_SYNC,
80837737Smckusick 					    tndp->ni_cred, (int *)0);
80937737Smckusick 					cache_purge(ITOV(dp));
81016776Smckusick 				}
81116776Smckusick 			}
8129167Ssam 		}
81337737Smckusick 		error = dirremove(fndp);
81437737Smckusick 		if (!error) {
81516776Smckusick 			xp->i_nlink--;
81616776Smckusick 			xp->i_flag |= ICHG;
8179167Ssam 		}
81816776Smckusick 		xp->i_flag &= ~IRENAME;
8199167Ssam 	}
8209167Ssam 	if (dp)
82137737Smckusick 		vput(ITOV(dp));
82216776Smckusick 	if (xp)
82337737Smckusick 		vput(ITOV(xp));
82437737Smckusick 	vrele(ITOV(ip));
82537737Smckusick 	return (error);
8269167Ssam 
8279167Ssam bad:
8289167Ssam 	if (xp)
82937737Smckusick 		vput(ITOV(xp));
83037737Smckusick 	vput(ITOV(dp));
8319167Ssam out:
8329167Ssam 	ip->i_nlink--;
8339167Ssam 	ip->i_flag |= ICHG;
83437737Smckusick 	vrele(ITOV(ip));
83537737Smckusick 	return (error);
8367701Ssam }
8377701Ssam 
8387535Sroot /*
83912756Ssam  * A virgin directory (no blushing please).
84012756Ssam  */
84112756Ssam struct dirtemplate mastertemplate = {
84212756Ssam 	0, 12, 1, ".",
84312756Ssam 	0, DIRBLKSIZ - 12, 2, ".."
84412756Ssam };
84512756Ssam 
84612756Ssam /*
84712756Ssam  * Mkdir system call
84812756Ssam  */
84937737Smckusick ufs_mkdir(ndp, vap)
85037737Smckusick 	struct nameidata *ndp;
85137737Smckusick 	struct vattr *vap;
85212756Ssam {
85312756Ssam 	register struct inode *ip, *dp;
85437737Smckusick 	struct inode *tip;
85537737Smckusick 	struct vnode *dvp;
85612756Ssam 	struct dirtemplate dirtemplate;
85737737Smckusick 	int error;
85837737Smckusick 	int dmode;
85912756Ssam 
86037737Smckusick 	dvp = ndp->ni_dvp;
86137737Smckusick 	dp = VTOI(dvp);
86237737Smckusick 	dmode = vap->va_mode&0777;
86337737Smckusick 	dmode |= IFDIR;
86412756Ssam 	/*
86512756Ssam 	 * Must simulate part of maknode here
86612756Ssam 	 * in order to acquire the inode, but
86712756Ssam 	 * not have it entered in the parent
86812756Ssam 	 * directory.  The entry is made later
86912756Ssam 	 * after writing "." and ".." entries out.
87012756Ssam 	 */
87137737Smckusick 	error = ialloc(dp, dirpref(dp->i_fs), dmode, &tip);
87237737Smckusick 	if (error) {
87312756Ssam 		iput(dp);
87437737Smckusick 		return (error);
87512756Ssam 	}
87637737Smckusick 	ip = tip;
87712756Ssam #ifdef QUOTA
87812756Ssam 	if (ip->i_dquot != NODQUOT)
87912756Ssam 		panic("mkdir: dquot");
88012756Ssam #endif
88112756Ssam 	ip->i_flag |= IACC|IUPD|ICHG;
88237737Smckusick 	ip->i_mode = dmode;
88337737Smckusick 	ITOV(ip)->v_type = VDIR;	/* Rest init'd in iget() */
88412756Ssam 	ip->i_nlink = 2;
88537737Smckusick 	ip->i_uid = ndp->ni_cred->cr_uid;
88612756Ssam 	ip->i_gid = dp->i_gid;
88712756Ssam #ifdef QUOTA
88812756Ssam 	ip->i_dquot = inoquota(ip);
88912756Ssam #endif
89037737Smckusick 	error = iupdat(ip, &time, &time, 1);
89112756Ssam 
89212756Ssam 	/*
89312756Ssam 	 * Bump link count in parent directory
89412756Ssam 	 * to reflect work done below.  Should
89512756Ssam 	 * be done before reference is created
89612756Ssam 	 * so reparation is possible if we crash.
89712756Ssam 	 */
89812756Ssam 	dp->i_nlink++;
89912756Ssam 	dp->i_flag |= ICHG;
90037737Smckusick 	error = iupdat(dp, &time, &time, 1);
90112756Ssam 
90212756Ssam 	/*
90312756Ssam 	 * Initialize directory with "."
90412756Ssam 	 * and ".." from static template.
90512756Ssam 	 */
90612756Ssam 	dirtemplate = mastertemplate;
90712756Ssam 	dirtemplate.dot_ino = ip->i_number;
90812756Ssam 	dirtemplate.dotdot_ino = dp->i_number;
909*39597Smckusick 	error = vn_rdwr(UIO_WRITE, ITOV(ip), (caddr_t)&dirtemplate,
91037737Smckusick 		sizeof (dirtemplate), (off_t)0, UIO_SYSSPACE,
911*39597Smckusick 		IO_NODELOCKED|IO_SYNC, ndp->ni_cred, (int *)0);
91237737Smckusick 	if (error) {
91312756Ssam 		dp->i_nlink--;
91412756Ssam 		dp->i_flag |= ICHG;
91512756Ssam 		goto bad;
91612756Ssam 	}
91737737Smckusick 	if (DIRBLKSIZ > dp->i_fs->fs_fsize)
91837737Smckusick 		panic("mkdir: blksize");     /* XXX - should grow w/balloc() */
91918103Smckusick 	else
92018103Smckusick 		ip->i_size = DIRBLKSIZ;
92112756Ssam 	/*
92212756Ssam 	 * Directory all set up, now
92312756Ssam 	 * install the entry for it in
92412756Ssam 	 * the parent directory.
92512756Ssam 	 */
92637737Smckusick 	error = direnter(ip, ndp);
92712756Ssam 	dp = NULL;
92837737Smckusick 	if (error) {
92916694Smckusick 		ndp->ni_nameiop = LOOKUP | NOCACHE;
93037737Smckusick 		error = namei(ndp);
93137737Smckusick 		if (!error) {
93237737Smckusick 			dp = VTOI(ndp->ni_vp);
93312756Ssam 			dp->i_nlink--;
93412756Ssam 			dp->i_flag |= ICHG;
93512756Ssam 		}
93612756Ssam 	}
93712756Ssam bad:
93812756Ssam 	/*
93912756Ssam 	 * No need to do an explicit itrunc here,
94037737Smckusick 	 * vrele will do this for us because we set
94112756Ssam 	 * the link count to 0.
94212756Ssam 	 */
94337737Smckusick 	if (error) {
94412756Ssam 		ip->i_nlink = 0;
94512756Ssam 		ip->i_flag |= ICHG;
94638144Smckusick 		iput(ip);
94738144Smckusick 	} else
94838144Smckusick 		ndp->ni_vp = ITOV(ip);
94912756Ssam 	if (dp)
95012756Ssam 		iput(dp);
95137737Smckusick 	return (error);
95212756Ssam }
95312756Ssam 
95412756Ssam /*
95512756Ssam  * Rmdir system call.
95612756Ssam  */
95737737Smckusick ufs_rmdir(ndp)
95837737Smckusick 	register struct nameidata *ndp;
95912756Ssam {
96012756Ssam 	register struct inode *ip, *dp;
96137737Smckusick 	int error = 0;
96212756Ssam 
96337737Smckusick 	ip = VTOI(ndp->ni_vp);
96437737Smckusick 	dp = VTOI(ndp->ni_dvp);
96512756Ssam 	/*
96612756Ssam 	 * No rmdir "." please.
96712756Ssam 	 */
96812756Ssam 	if (dp == ip) {
96937737Smckusick 		vrele(ITOV(dp));
97012756Ssam 		iput(ip);
97137737Smckusick 		return (EINVAL);
97212756Ssam 	}
97312756Ssam 	/*
97412756Ssam 	 * Verify the directory is empty (and valid).
97512756Ssam 	 * (Rmdir ".." won't be valid since
97612756Ssam 	 *  ".." will contain a reference to
97712756Ssam 	 *  the current directory and thus be
97812756Ssam 	 *  non-empty.)
97912756Ssam 	 */
98037737Smckusick 	if (ip->i_nlink != 2 || !dirempty(ip, dp->i_number, ndp->ni_cred)) {
98137737Smckusick 		error = ENOTEMPTY;
98212756Ssam 		goto out;
98312756Ssam 	}
98412756Ssam 	/*
98512756Ssam 	 * Delete reference to directory before purging
98612756Ssam 	 * inode.  If we crash in between, the directory
98712756Ssam 	 * will be reattached to lost+found,
98812756Ssam 	 */
98937737Smckusick 	if (error = dirremove(ndp))
99012756Ssam 		goto out;
99112756Ssam 	dp->i_nlink--;
99212756Ssam 	dp->i_flag |= ICHG;
99337737Smckusick 	cache_purge(ITOV(dp));
99412756Ssam 	iput(dp);
99537737Smckusick 	ndp->ni_dvp = NULL;
99612756Ssam 	/*
99712756Ssam 	 * Truncate inode.  The only stuff left
99812756Ssam 	 * in the directory is "." and "..".  The
99912756Ssam 	 * "." reference is inconsequential since
100012756Ssam 	 * we're quashing it.  The ".." reference
100112756Ssam 	 * has already been adjusted above.  We've
100212756Ssam 	 * removed the "." reference and the reference
100312756Ssam 	 * in the parent directory, but there may be
100412756Ssam 	 * other hard links so decrement by 2 and
100512756Ssam 	 * worry about them later.
100612756Ssam 	 */
100712756Ssam 	ip->i_nlink -= 2;
100837737Smckusick 	error = itrunc(ip, (u_long)0);
100937737Smckusick 	cache_purge(ITOV(ip));
101012756Ssam out:
101137737Smckusick 	if (ndp->ni_dvp)
101212756Ssam 		iput(dp);
101312756Ssam 	iput(ip);
101437737Smckusick 	return (error);
101512756Ssam }
101612756Ssam 
101737737Smckusick /*
101837737Smckusick  * symlink -- make a symbolic link
101937737Smckusick  */
102037737Smckusick ufs_symlink(ndp, vap, target)
102137737Smckusick 	struct nameidata *ndp;
102237737Smckusick 	struct vattr *vap;
102337737Smckusick 	char *target;
102412756Ssam {
102537737Smckusick 	struct inode *ip;
102637737Smckusick 	int error;
102712756Ssam 
102837737Smckusick 	error = maknode(IFLNK | vap->va_mode, ndp, &ip);
102937737Smckusick 	if (error)
103037737Smckusick 		return (error);
1031*39597Smckusick 	error = vn_rdwr(UIO_WRITE, ITOV(ip), target, strlen(target), (off_t)0,
1032*39597Smckusick 		UIO_SYSSPACE, IO_NODELOCKED, ndp->ni_cred, (int *)0);
103337737Smckusick 	iput(ip);
103437737Smckusick 	return (error);
103537737Smckusick }
103637737Smckusick 
103737737Smckusick /*
103837737Smckusick  * Vnode op for read and write
103937737Smckusick  */
1040*39597Smckusick ufs_readdir(vp, uio, cred)
104137737Smckusick 	struct vnode *vp;
104237737Smckusick 	register struct uio *uio;
104337737Smckusick 	struct ucred *cred;
104437737Smckusick {
1045*39597Smckusick 	int count, lost, error;
104637737Smckusick 
104737737Smckusick 	count = uio->uio_resid;
104837737Smckusick 	count &= ~(DIRBLKSIZ - 1);
1049*39597Smckusick 	lost = uio->uio_resid - count;
1050*39597Smckusick 	if (count < DIRBLKSIZ || (uio->uio_offset & (DIRBLKSIZ -1)))
105137737Smckusick 		return (EINVAL);
105237737Smckusick 	uio->uio_resid = count;
105337737Smckusick 	uio->uio_iov->iov_len = count;
1054*39597Smckusick 	error = ufs_read(vp, uio, 0, cred);
1055*39597Smckusick 	uio->uio_resid += lost;
105637737Smckusick 	return (error);
105737737Smckusick }
105837737Smckusick 
105937737Smckusick /*
106037737Smckusick  * Return target name of a symbolic link
106137737Smckusick  */
106237737Smckusick ufs_readlink(vp, uiop, cred)
106337737Smckusick 	struct vnode *vp;
106437737Smckusick 	struct uio *uiop;
106537737Smckusick 	struct ucred *cred;
106637737Smckusick {
106737737Smckusick 
1068*39597Smckusick 	return (ufs_read(vp, uiop, 0, cred));
106937737Smckusick }
107037737Smckusick 
107137737Smckusick /*
107237737Smckusick  * Ufs abort op, called after namei() when a CREATE/DELETE isn't actually
107337737Smckusick  * done. Iff ni_vp/ni_dvp not null and locked, unlock.
107437737Smckusick  */
107537737Smckusick ufs_abortop(ndp)
107637737Smckusick 	register struct nameidata *ndp;
107737737Smckusick {
107837737Smckusick 	register struct inode *ip;
107937737Smckusick 
108037737Smckusick 	if (ndp->ni_vp) {
108137737Smckusick 		ip = VTOI(ndp->ni_vp);
108237737Smckusick 		if (ip->i_flag & ILOCKED)
108337737Smckusick 			IUNLOCK(ip);
108437737Smckusick 		vrele(ndp->ni_vp);
108512756Ssam 	}
108637737Smckusick 	if (ndp->ni_dvp) {
108737737Smckusick 		ip = VTOI(ndp->ni_dvp);
108837737Smckusick 		if (ip->i_flag & ILOCKED)
108937737Smckusick 			IUNLOCK(ip);
109037737Smckusick 		vrele(ndp->ni_dvp);
109137737Smckusick 	}
109237737Smckusick 	return;
109312756Ssam }
109412756Ssam 
109537737Smckusick ufs_lock(vp)
109637737Smckusick 	struct vnode *vp;
109737737Smckusick {
109837737Smckusick 	register struct inode *ip = VTOI(vp);
109937737Smckusick 
110037737Smckusick 	ILOCK(ip);
110137737Smckusick 	return (0);
110237737Smckusick }
110337737Smckusick 
110437737Smckusick ufs_unlock(vp)
110537737Smckusick 	struct vnode *vp;
110637737Smckusick {
110737737Smckusick 	register struct inode *ip = VTOI(vp);
110837737Smckusick 
110937737Smckusick 	if (!(ip->i_flag & ILOCKED))
111037737Smckusick 		panic("ufs_unlock NOT LOCKED");
111137737Smckusick 	IUNLOCK(ip);
111237737Smckusick 	return (0);
111337737Smckusick }
111437737Smckusick 
111512756Ssam /*
111637737Smckusick  * Get access to bmap
111712756Ssam  */
111837737Smckusick ufs_bmap(vp, bn, vpp, bnp)
111937737Smckusick 	struct vnode *vp;
112037737Smckusick 	daddr_t bn;
112137737Smckusick 	struct vnode **vpp;
112237737Smckusick 	daddr_t *bnp;
112312756Ssam {
112437737Smckusick 	struct inode *ip = VTOI(vp);
112512756Ssam 
112637737Smckusick 	if (vpp != NULL)
112737737Smckusick 		*vpp = ip->i_devvp;
112837737Smckusick 	if (bnp == NULL)
112937737Smckusick 		return (0);
113037737Smckusick 	return (bmap(ip, bn, bnp, (daddr_t *)0, (int *)0));
113112756Ssam }
113237737Smckusick 
113337737Smckusick /*
113437737Smckusick  * Just call the device strategy routine
113537737Smckusick  */
113637737Smckusick ufs_strategy(bp)
113737737Smckusick 	register struct buf *bp;
113837737Smckusick {
113937737Smckusick 	(*bdevsw[major(bp->b_dev)].d_strategy)(bp);
114037737Smckusick 	return (0);
114137737Smckusick }
114237737Smckusick 
114337737Smckusick /*
114437737Smckusick  * Make a new file.
114537737Smckusick  */
114637737Smckusick maknode(mode, ndp, ipp)
114737737Smckusick 	int mode;
114837737Smckusick 	register struct nameidata *ndp;
114937737Smckusick 	struct inode **ipp;
115037737Smckusick {
115137737Smckusick 	register struct inode *ip;
115237737Smckusick 	struct inode *tip;
115337737Smckusick 	register struct inode *pdir = VTOI(ndp->ni_dvp);
115437737Smckusick 	ino_t ipref;
115537737Smckusick 	int error;
115637737Smckusick 
115737737Smckusick 	*ipp = 0;
115837737Smckusick 	if ((mode & IFMT) == IFDIR)
115937737Smckusick 		ipref = dirpref(pdir->i_fs);
116037737Smckusick 	else
116137737Smckusick 		ipref = pdir->i_number;
116237737Smckusick 	error = ialloc(pdir, ipref, mode, &tip);
116337737Smckusick 	if (error) {
116437737Smckusick 		iput(pdir);
116537737Smckusick 		return (error);
116637737Smckusick 	}
116737737Smckusick 	ip = tip;
116837737Smckusick #ifdef QUOTA
116937737Smckusick 	if (ip->i_dquot != NODQUOT)
117037737Smckusick 		panic("maknode: dquot");
117137737Smckusick #endif
117237737Smckusick 	ip->i_flag |= IACC|IUPD|ICHG;
117337737Smckusick 	if ((mode & IFMT) == 0)
117437737Smckusick 		mode |= IFREG;
117537737Smckusick 	ip->i_mode = mode;
117637737Smckusick 	ITOV(ip)->v_type = IFTOVT(mode);	/* Rest init'd in iget() */
117737737Smckusick 	ip->i_nlink = 1;
117837737Smckusick 	ip->i_uid = ndp->ni_cred->cr_uid;
117937737Smckusick 	ip->i_gid = pdir->i_gid;
118037737Smckusick 	if ((ip->i_mode & ISGID) && !groupmember(ip->i_gid, ndp->ni_cred) &&
118137737Smckusick 	    suser(ndp->ni_cred, NULL))
118237737Smckusick 		ip->i_mode &= ~ISGID;
118337737Smckusick #ifdef QUOTA
118437737Smckusick 	ip->i_dquot = inoquota(ip);
118537737Smckusick #endif
118637737Smckusick 
118737737Smckusick 	/*
118837737Smckusick 	 * Make sure inode goes to disk before directory entry.
118937737Smckusick 	 */
119037737Smckusick 	if ((error = iupdat(ip, &time, &time, 1)) ||
119137737Smckusick 	    (error = direnter(ip, ndp))) {
119237737Smckusick 		/*
119337737Smckusick 		 * Write error occurred trying to update the inode
119437737Smckusick 		 * or the directory so must deallocate the inode.
119537737Smckusick 		 */
119637737Smckusick 		ip->i_nlink = 0;
119737737Smckusick 		ip->i_flag |= ICHG;
119837737Smckusick 		iput(ip);
119937737Smckusick 		return (error);
120037737Smckusick 	}
120137737Smckusick 	*ipp = ip;
120237737Smckusick 	return (0);
120337737Smckusick }
1204