xref: /csrg-svn/sys/ufs/lfs/lfs_vnops.c (revision 39435)
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*39435Smckusick  *	@(#)lfs_vnops.c	7.20 (Berkeley) 10/29/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 
106*39435Smckusick int	spec_lookup(),
107*39435Smckusick 	spec_open(),
108*39435Smckusick 	spec_read(),
109*39435Smckusick 	spec_write(),
110*39435Smckusick 	spec_strategy(),
111*39435Smckusick 	spec_ioctl(),
112*39435Smckusick 	spec_select(),
113*39435Smckusick 	spec_close(),
114*39435Smckusick 	spec_badop(),
115*39435Smckusick 	spec_nullop();
116*39435Smckusick 
117*39435Smckusick struct vnodeops spec_inodeops = {
118*39435Smckusick 	spec_lookup,
119*39435Smckusick 	spec_badop,
120*39435Smckusick 	spec_badop,
121*39435Smckusick 	spec_open,
122*39435Smckusick 	spec_close,
123*39435Smckusick 	ufs_access,
124*39435Smckusick 	ufs_getattr,
125*39435Smckusick 	ufs_setattr,
126*39435Smckusick 	spec_read,
127*39435Smckusick 	spec_write,
128*39435Smckusick 	spec_ioctl,
129*39435Smckusick 	spec_select,
130*39435Smckusick 	spec_badop,
131*39435Smckusick 	spec_nullop,
132*39435Smckusick 	spec_badop,
133*39435Smckusick 	spec_badop,
134*39435Smckusick 	spec_badop,
135*39435Smckusick 	spec_badop,
136*39435Smckusick 	spec_badop,
137*39435Smckusick 	spec_badop,
138*39435Smckusick 	spec_badop,
139*39435Smckusick 	spec_badop,
140*39435Smckusick 	spec_badop,
141*39435Smckusick 	spec_badop,
142*39435Smckusick 	ufs_inactive,
143*39435Smckusick 	ufs_reclaim,
144*39435Smckusick 	ufs_lock,
145*39435Smckusick 	ufs_unlock,
146*39435Smckusick 	spec_badop,
147*39435Smckusick 	spec_strategy,
148*39435Smckusick };
149*39435Smckusick 
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 {
182*39435Smckusick 	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);
188*39435Smckusick 	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 		 */
194*39435Smckusick 		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);
203*39435Smckusick 	vp->v_type = VNON;
204*39435Smckusick 	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 */
48037737Smckusick ufs_fsync(vp, fflags, cred)
48137737Smckusick 	struct vnode *vp;
48237737Smckusick 	int fflags;
48337737Smckusick 	struct ucred *cred;
4847701Ssam {
48537737Smckusick 	register struct inode *ip = VTOI(vp);
48637737Smckusick 	int error;
4877701Ssam 
48837737Smckusick 	ILOCK(ip);
48937737Smckusick 	if (fflags&FWRITE)
49037737Smckusick 		ip->i_flag |= ICHG;
49137737Smckusick 	error = syncip(ip);
49237737Smckusick 	IUNLOCK(ip);
49337737Smckusick 	return (error);
4947701Ssam }
4957701Ssam 
4969167Ssam /*
49737737Smckusick  * Seek on a file
49837737Smckusick  *
49937737Smckusick  * Nothing to do, so just return.
5009167Ssam  */
50137737Smckusick /* ARGSUSED */
50237737Smckusick ufs_seek(vp, oldoff, newoff, cred)
50337737Smckusick 	struct vnode *vp;
50437737Smckusick 	off_t oldoff, newoff;
50537737Smckusick 	struct ucred *cred;
5067701Ssam {
5077701Ssam 
50837737Smckusick 	return (0);
50937737Smckusick }
51037737Smckusick 
51137737Smckusick /*
51237737Smckusick  * ufs remove
51337737Smckusick  * Hard to avoid races here, especially
51437737Smckusick  * in unlinking directories.
51537737Smckusick  */
51637737Smckusick ufs_remove(ndp)
51737737Smckusick 	struct nameidata *ndp;
51837737Smckusick {
51937737Smckusick 	register struct inode *ip, *dp;
52037737Smckusick 	int error;
52137737Smckusick 
52237737Smckusick 	ip = VTOI(ndp->ni_vp);
52337737Smckusick 	dp = VTOI(ndp->ni_dvp);
52437737Smckusick 	error = dirremove(ndp);
52537737Smckusick 	if (!error) {
52637737Smckusick 		ip->i_nlink--;
52737737Smckusick 		ip->i_flag |= ICHG;
5287701Ssam 	}
52937737Smckusick 	if (dp == ip)
53037737Smckusick 		vrele(ITOV(ip));
53137737Smckusick 	else
53237737Smckusick 		iput(ip);
53337737Smckusick 	iput(dp);
53437737Smckusick 	return (error);
5357701Ssam }
5367701Ssam 
5379167Ssam /*
53837737Smckusick  * link vnode call
5399167Ssam  */
54037737Smckusick ufs_link(vp, ndp)
54137737Smckusick 	register struct vnode *vp;
54237737Smckusick 	register struct nameidata *ndp;
5439167Ssam {
54437737Smckusick 	register struct inode *ip = VTOI(vp);
54537737Smckusick 	int error;
5469167Ssam 
54737737Smckusick 	if (ndp->ni_dvp != vp)
54837737Smckusick 		ILOCK(ip);
54937737Smckusick 	if (ip->i_nlink == LINK_MAX - 1) {
55037737Smckusick 		error = EMLINK;
55137737Smckusick 		goto out;
55237737Smckusick 	}
55337737Smckusick 	ip->i_nlink++;
55437737Smckusick 	ip->i_flag |= ICHG;
55537737Smckusick 	error = iupdat(ip, &time, &time, 1);
55637737Smckusick 	if (!error)
55737737Smckusick 		error = direnter(ip, ndp);
55837737Smckusick out:
55937737Smckusick 	if (ndp->ni_dvp != vp)
56037737Smckusick 		IUNLOCK(ip);
56137737Smckusick 	if (error) {
56237737Smckusick 		ip->i_nlink--;
56330598Smckusick 		ip->i_flag |= ICHG;
56437737Smckusick 	}
56537737Smckusick 	return (error);
5669167Ssam }
5679167Ssam 
5689167Ssam /*
5699167Ssam  * Rename system call.
5709167Ssam  * 	rename("foo", "bar");
5719167Ssam  * is essentially
5729167Ssam  *	unlink("bar");
5739167Ssam  *	link("foo", "bar");
5749167Ssam  *	unlink("foo");
5759167Ssam  * but ``atomically''.  Can't do full commit without saving state in the
5769167Ssam  * inode on disk which isn't feasible at this time.  Best we can do is
5779167Ssam  * always guarantee the target exists.
5789167Ssam  *
5799167Ssam  * Basic algorithm is:
5809167Ssam  *
5819167Ssam  * 1) Bump link count on source while we're linking it to the
58237737Smckusick  *    target.  This also ensure the inode won't be deleted out
58316776Smckusick  *    from underneath us while we work (it may be truncated by
58416776Smckusick  *    a concurrent `trunc' or `open' for creation).
5859167Ssam  * 2) Link source to destination.  If destination already exists,
5869167Ssam  *    delete it first.
58716776Smckusick  * 3) Unlink source reference to inode if still around. If a
58816776Smckusick  *    directory was moved and the parent of the destination
5899167Ssam  *    is different from the source, patch the ".." entry in the
5909167Ssam  *    directory.
5919167Ssam  */
59237737Smckusick ufs_rename(fndp, tndp)
59337737Smckusick 	register struct nameidata *fndp, *tndp;
5947701Ssam {
5959167Ssam 	register struct inode *ip, *xp, *dp;
59616776Smckusick 	struct dirtemplate dirbuf;
59716776Smckusick 	int doingdirectory = 0, oldparent = 0, newparent = 0;
59810051Ssam 	int error = 0;
5997701Ssam 
60037737Smckusick 	dp = VTOI(fndp->ni_dvp);
60137737Smckusick 	ip = VTOI(fndp->ni_vp);
60237737Smckusick 	ILOCK(ip);
6039167Ssam 	if ((ip->i_mode&IFMT) == IFDIR) {
60437737Smckusick 		register struct direct *d = &fndp->ni_dent;
6059167Ssam 
6069167Ssam 		/*
60711641Ssam 		 * Avoid ".", "..", and aliases of "." for obvious reasons.
6089167Ssam 		 */
60937737Smckusick 		if ((d->d_namlen == 1 && d->d_name[0] == '.') || dp == ip ||
61037737Smckusick 		    fndp->ni_isdotdot || (ip->i_flag & IRENAME)) {
61137737Smckusick 			IUNLOCK(ip);
61237737Smckusick 			ufs_abortop(fndp);
61337737Smckusick 			ufs_abortop(tndp);
61437737Smckusick 			return (EINVAL);
6159167Ssam 		}
61616776Smckusick 		ip->i_flag |= IRENAME;
6179167Ssam 		oldparent = dp->i_number;
6189167Ssam 		doingdirectory++;
6199167Ssam 	}
62037737Smckusick 	vrele(fndp->ni_dvp);
6219167Ssam 
6229167Ssam 	/*
6239167Ssam 	 * 1) Bump link count while we're moving stuff
6249167Ssam 	 *    around.  If we crash somewhere before
6259167Ssam 	 *    completing our work, the link count
6269167Ssam 	 *    may be wrong, but correctable.
6279167Ssam 	 */
6289167Ssam 	ip->i_nlink++;
6299167Ssam 	ip->i_flag |= ICHG;
63037737Smckusick 	error = iupdat(ip, &time, &time, 1);
63116664Smckusick 	IUNLOCK(ip);
6329167Ssam 
6339167Ssam 	/*
6349167Ssam 	 * When the target exists, both the directory
63537737Smckusick 	 * and target vnodes are returned locked.
6369167Ssam 	 */
63737737Smckusick 	dp = VTOI(tndp->ni_dvp);
63837737Smckusick 	xp = NULL;
63937737Smckusick 	if (tndp->ni_vp)
64037737Smckusick 		xp = VTOI(tndp->ni_vp);
6419167Ssam 	/*
64211641Ssam 	 * If ".." must be changed (ie the directory gets a new
64312816Smckusick 	 * parent) then the source directory must not be in the
64412816Smckusick 	 * directory heirarchy above the target, as this would
64512816Smckusick 	 * orphan everything below the source directory. Also
64612816Smckusick 	 * the user must have write permission in the source so
64712816Smckusick 	 * as to be able to change "..". We must repeat the call
64812816Smckusick 	 * to namei, as the parent directory is unlocked by the
64912816Smckusick 	 * call to checkpath().
65011641Ssam 	 */
65116776Smckusick 	if (oldparent != dp->i_number)
65216776Smckusick 		newparent = dp->i_number;
65316776Smckusick 	if (doingdirectory && newparent) {
65437737Smckusick 		if (error = iaccess(ip, IWRITE, tndp->ni_cred))
65512816Smckusick 			goto bad;
65637737Smckusick 		tndp->ni_nameiop = RENAME | LOCKPARENT | LOCKLEAF | NOCACHE;
65712816Smckusick 		do {
65837737Smckusick 			dp = VTOI(tndp->ni_dvp);
65912816Smckusick 			if (xp != NULL)
66038069Smckusick 				iput(xp);
66137737Smckusick 			if (error = checkpath(ip, dp, tndp->ni_cred))
66212816Smckusick 				goto out;
66337737Smckusick 			if (error = namei(tndp))
66412816Smckusick 				goto out;
66537737Smckusick 			xp = NULL;
66637737Smckusick 			if (tndp->ni_vp)
66737737Smckusick 				xp = VTOI(tndp->ni_vp);
66837737Smckusick 		} while (dp != VTOI(tndp->ni_dvp));
66912816Smckusick 	}
67011641Ssam 	/*
6719167Ssam 	 * 2) If target doesn't exist, link the target
6729167Ssam 	 *    to the source and unlink the source.
6739167Ssam 	 *    Otherwise, rewrite the target directory
6749167Ssam 	 *    entry to reference the source inode and
6759167Ssam 	 *    expunge the original entry's existence.
6769167Ssam 	 */
6779167Ssam 	if (xp == NULL) {
67837737Smckusick 		if (dp->i_dev != ip->i_dev)
67937737Smckusick 			panic("rename: EXDEV");
6809167Ssam 		/*
68116776Smckusick 		 * Account for ".." in new directory.
68216776Smckusick 		 * When source and destination have the same
68316776Smckusick 		 * parent we don't fool with the link count.
6849167Ssam 		 */
68516776Smckusick 		if (doingdirectory && newparent) {
6869167Ssam 			dp->i_nlink++;
6879167Ssam 			dp->i_flag |= ICHG;
68837737Smckusick 			error = iupdat(dp, &time, &time, 1);
6899167Ssam 		}
69037737Smckusick 		if (error = direnter(ip, tndp))
6919167Ssam 			goto out;
6929167Ssam 	} else {
69337737Smckusick 		if (xp->i_dev != dp->i_dev || xp->i_dev != ip->i_dev)
69437737Smckusick 			panic("rename: EXDEV");
6959167Ssam 		/*
69610590Ssam 		 * Short circuit rename(foo, foo).
69710590Ssam 		 */
69810590Ssam 		if (xp->i_number == ip->i_number)
69937737Smckusick 			panic("rename: same file");
70010590Ssam 		/*
70124433Sbloom 		 * If the parent directory is "sticky", then the user must
70224433Sbloom 		 * own the parent directory, or the destination of the rename,
70324433Sbloom 		 * otherwise the destination may not be changed (except by
70424433Sbloom 		 * root). This implements append-only directories.
70524433Sbloom 		 */
70637737Smckusick 		if ((dp->i_mode & ISVTX) && tndp->ni_cred->cr_uid != 0 &&
70737737Smckusick 		    tndp->ni_cred->cr_uid != dp->i_uid &&
70837737Smckusick 		    xp->i_uid != tndp->ni_cred->cr_uid) {
70924433Sbloom 			error = EPERM;
71024433Sbloom 			goto bad;
71124433Sbloom 		}
71224433Sbloom 		/*
71310051Ssam 		 * Target must be empty if a directory
71410051Ssam 		 * and have no links to it.
7159167Ssam 		 * Also, insure source and target are
7169167Ssam 		 * compatible (both directories, or both
7179167Ssam 		 * not directories).
7189167Ssam 		 */
7199167Ssam 		if ((xp->i_mode&IFMT) == IFDIR) {
72037737Smckusick 			if (!dirempty(xp, dp->i_number, tndp->ni_cred) ||
72137737Smckusick 			    xp->i_nlink > 2) {
72210051Ssam 				error = ENOTEMPTY;
7239167Ssam 				goto bad;
7249167Ssam 			}
7259167Ssam 			if (!doingdirectory) {
72610051Ssam 				error = ENOTDIR;
7279167Ssam 				goto bad;
7289167Ssam 			}
72937737Smckusick 			cache_purge(ITOV(dp));
7309167Ssam 		} else if (doingdirectory) {
73110051Ssam 			error = EISDIR;
7329167Ssam 			goto bad;
7339167Ssam 		}
73437737Smckusick 		if (error = dirrewrite(dp, ip, tndp))
73537737Smckusick 			goto bad;
73637737Smckusick 		vput(ITOV(dp));
7379167Ssam 		/*
73810051Ssam 		 * Adjust the link count of the target to
73910051Ssam 		 * reflect the dirrewrite above.  If this is
74010051Ssam 		 * a directory it is empty and there are
74110051Ssam 		 * no links to it, so we can squash the inode and
74210051Ssam 		 * any space associated with it.  We disallowed
74310051Ssam 		 * renaming over top of a directory with links to
74416776Smckusick 		 * it above, as the remaining link would point to
74516776Smckusick 		 * a directory without "." or ".." entries.
7469167Ssam 		 */
74710051Ssam 		xp->i_nlink--;
7489167Ssam 		if (doingdirectory) {
74910051Ssam 			if (--xp->i_nlink != 0)
75010051Ssam 				panic("rename: linked directory");
75137737Smckusick 			error = itrunc(xp, (u_long)0);
75210051Ssam 		}
7539167Ssam 		xp->i_flag |= ICHG;
75438398Smckusick 		iput(xp);
75510246Ssam 		xp = NULL;
7569167Ssam 	}
7579167Ssam 
7589167Ssam 	/*
7599167Ssam 	 * 3) Unlink the source.
7609167Ssam 	 */
76137737Smckusick 	fndp->ni_nameiop = DELETE | LOCKPARENT | LOCKLEAF;
76237737Smckusick 	(void)namei(fndp);
76337737Smckusick 	if (fndp->ni_vp != NULL) {
76437737Smckusick 		xp = VTOI(fndp->ni_vp);
76537737Smckusick 		dp = VTOI(fndp->ni_dvp);
76637737Smckusick 	} else {
76738069Smckusick 		if (fndp->ni_dvp != NULL)
76838069Smckusick 			vput(fndp->ni_dvp);
76937737Smckusick 		xp = NULL;
77017758Smckusick 		dp = NULL;
77137737Smckusick 	}
7729167Ssam 	/*
77337737Smckusick 	 * Ensure that the directory entry still exists and has not
77416776Smckusick 	 * changed while the new name has been entered. If the source is
77516776Smckusick 	 * a file then the entry may have been unlinked or renamed. In
77616776Smckusick 	 * either case there is no further work to be done. If the source
77716776Smckusick 	 * is a directory then it cannot have been rmdir'ed; its link
77816776Smckusick 	 * count of three would cause a rmdir to fail with ENOTEMPTY.
77937737Smckusick 	 * The IRENAME flag ensures that it cannot be moved by another
78016776Smckusick 	 * rename.
7819167Ssam 	 */
78217758Smckusick 	if (xp != ip) {
78316776Smckusick 		if (doingdirectory)
78417758Smckusick 			panic("rename: lost dir entry");
78516776Smckusick 	} else {
7869167Ssam 		/*
78716776Smckusick 		 * If the source is a directory with a
78816776Smckusick 		 * new parent, the link count of the old
78916776Smckusick 		 * parent directory must be decremented
79016776Smckusick 		 * and ".." set to point to the new parent.
7919167Ssam 		 */
79216776Smckusick 		if (doingdirectory && newparent) {
7939167Ssam 			dp->i_nlink--;
7949167Ssam 			dp->i_flag |= ICHG;
79516776Smckusick 			error = rdwri(UIO_READ, xp, (caddr_t)&dirbuf,
79637737Smckusick 				sizeof (struct dirtemplate), (off_t)0,
79737740Smckusick 				UIO_SYSSPACE, tndp->ni_cred, (int *)0);
79816776Smckusick 			if (error == 0) {
79916776Smckusick 				if (dirbuf.dotdot_namlen != 2 ||
80016776Smckusick 				    dirbuf.dotdot_name[0] != '.' ||
80116776Smckusick 				    dirbuf.dotdot_name[1] != '.') {
80216776Smckusick 					printf("rename: mangled dir\n");
80316776Smckusick 				} else {
80416776Smckusick 					dirbuf.dotdot_ino = newparent;
80516776Smckusick 					(void) rdwri(UIO_WRITE, xp,
80616776Smckusick 					    (caddr_t)&dirbuf,
80716776Smckusick 					    sizeof (struct dirtemplate),
80837740Smckusick 					    (off_t)0, UIO_SYSSPACE,
80937737Smckusick 					    tndp->ni_cred, (int *)0);
81037737Smckusick 					cache_purge(ITOV(dp));
81116776Smckusick 				}
81216776Smckusick 			}
8139167Ssam 		}
81437737Smckusick 		error = dirremove(fndp);
81537737Smckusick 		if (!error) {
81616776Smckusick 			xp->i_nlink--;
81716776Smckusick 			xp->i_flag |= ICHG;
8189167Ssam 		}
81916776Smckusick 		xp->i_flag &= ~IRENAME;
8209167Ssam 	}
8219167Ssam 	if (dp)
82237737Smckusick 		vput(ITOV(dp));
82316776Smckusick 	if (xp)
82437737Smckusick 		vput(ITOV(xp));
82537737Smckusick 	vrele(ITOV(ip));
82637737Smckusick 	return (error);
8279167Ssam 
8289167Ssam bad:
8299167Ssam 	if (xp)
83037737Smckusick 		vput(ITOV(xp));
83137737Smckusick 	vput(ITOV(dp));
8329167Ssam out:
8339167Ssam 	ip->i_nlink--;
8349167Ssam 	ip->i_flag |= ICHG;
83537737Smckusick 	vrele(ITOV(ip));
83637737Smckusick 	return (error);
8377701Ssam }
8387701Ssam 
8397535Sroot /*
84012756Ssam  * A virgin directory (no blushing please).
84112756Ssam  */
84212756Ssam struct dirtemplate mastertemplate = {
84312756Ssam 	0, 12, 1, ".",
84412756Ssam 	0, DIRBLKSIZ - 12, 2, ".."
84512756Ssam };
84612756Ssam 
84712756Ssam /*
84812756Ssam  * Mkdir system call
84912756Ssam  */
85037737Smckusick ufs_mkdir(ndp, vap)
85137737Smckusick 	struct nameidata *ndp;
85237737Smckusick 	struct vattr *vap;
85312756Ssam {
85412756Ssam 	register struct inode *ip, *dp;
85537737Smckusick 	struct inode *tip;
85637737Smckusick 	struct vnode *dvp;
85712756Ssam 	struct dirtemplate dirtemplate;
85837737Smckusick 	int error;
85937737Smckusick 	int dmode;
86012756Ssam 
86137737Smckusick 	dvp = ndp->ni_dvp;
86237737Smckusick 	dp = VTOI(dvp);
86337737Smckusick 	dmode = vap->va_mode&0777;
86437737Smckusick 	dmode |= IFDIR;
86512756Ssam 	/*
86612756Ssam 	 * Must simulate part of maknode here
86712756Ssam 	 * in order to acquire the inode, but
86812756Ssam 	 * not have it entered in the parent
86912756Ssam 	 * directory.  The entry is made later
87012756Ssam 	 * after writing "." and ".." entries out.
87112756Ssam 	 */
87237737Smckusick 	error = ialloc(dp, dirpref(dp->i_fs), dmode, &tip);
87337737Smckusick 	if (error) {
87412756Ssam 		iput(dp);
87537737Smckusick 		return (error);
87612756Ssam 	}
87737737Smckusick 	ip = tip;
87812756Ssam #ifdef QUOTA
87912756Ssam 	if (ip->i_dquot != NODQUOT)
88012756Ssam 		panic("mkdir: dquot");
88112756Ssam #endif
88212756Ssam 	ip->i_flag |= IACC|IUPD|ICHG;
88337737Smckusick 	ip->i_mode = dmode;
88437737Smckusick 	ITOV(ip)->v_type = VDIR;	/* Rest init'd in iget() */
88512756Ssam 	ip->i_nlink = 2;
88637737Smckusick 	ip->i_uid = ndp->ni_cred->cr_uid;
88712756Ssam 	ip->i_gid = dp->i_gid;
88812756Ssam #ifdef QUOTA
88912756Ssam 	ip->i_dquot = inoquota(ip);
89012756Ssam #endif
89137737Smckusick 	error = iupdat(ip, &time, &time, 1);
89212756Ssam 
89312756Ssam 	/*
89412756Ssam 	 * Bump link count in parent directory
89512756Ssam 	 * to reflect work done below.  Should
89612756Ssam 	 * be done before reference is created
89712756Ssam 	 * so reparation is possible if we crash.
89812756Ssam 	 */
89912756Ssam 	dp->i_nlink++;
90012756Ssam 	dp->i_flag |= ICHG;
90137737Smckusick 	error = iupdat(dp, &time, &time, 1);
90212756Ssam 
90312756Ssam 	/*
90412756Ssam 	 * Initialize directory with "."
90512756Ssam 	 * and ".." from static template.
90612756Ssam 	 */
90712756Ssam 	dirtemplate = mastertemplate;
90812756Ssam 	dirtemplate.dot_ino = ip->i_number;
90912756Ssam 	dirtemplate.dotdot_ino = dp->i_number;
91037737Smckusick 	error = rdwri(UIO_WRITE, ip, (caddr_t)&dirtemplate,
91137737Smckusick 		sizeof (dirtemplate), (off_t)0, UIO_SYSSPACE,
91237737Smckusick 		ndp->ni_cred, (int *)0);
91337737Smckusick 	if (error) {
91412756Ssam 		dp->i_nlink--;
91512756Ssam 		dp->i_flag |= ICHG;
91612756Ssam 		goto bad;
91712756Ssam 	}
91837737Smckusick 	if (DIRBLKSIZ > dp->i_fs->fs_fsize)
91937737Smckusick 		panic("mkdir: blksize");     /* XXX - should grow w/balloc() */
92018103Smckusick 	else
92118103Smckusick 		ip->i_size = DIRBLKSIZ;
92212756Ssam 	/*
92312756Ssam 	 * Directory all set up, now
92412756Ssam 	 * install the entry for it in
92512756Ssam 	 * the parent directory.
92612756Ssam 	 */
92737737Smckusick 	error = direnter(ip, ndp);
92812756Ssam 	dp = NULL;
92937737Smckusick 	if (error) {
93016694Smckusick 		ndp->ni_nameiop = LOOKUP | NOCACHE;
93137737Smckusick 		error = namei(ndp);
93237737Smckusick 		if (!error) {
93337737Smckusick 			dp = VTOI(ndp->ni_vp);
93412756Ssam 			dp->i_nlink--;
93512756Ssam 			dp->i_flag |= ICHG;
93612756Ssam 		}
93712756Ssam 	}
93812756Ssam bad:
93912756Ssam 	/*
94012756Ssam 	 * No need to do an explicit itrunc here,
94137737Smckusick 	 * vrele will do this for us because we set
94212756Ssam 	 * the link count to 0.
94312756Ssam 	 */
94437737Smckusick 	if (error) {
94512756Ssam 		ip->i_nlink = 0;
94612756Ssam 		ip->i_flag |= ICHG;
94738144Smckusick 		iput(ip);
94838144Smckusick 	} else
94938144Smckusick 		ndp->ni_vp = ITOV(ip);
95012756Ssam 	if (dp)
95112756Ssam 		iput(dp);
95237737Smckusick 	return (error);
95312756Ssam }
95412756Ssam 
95512756Ssam /*
95612756Ssam  * Rmdir system call.
95712756Ssam  */
95837737Smckusick ufs_rmdir(ndp)
95937737Smckusick 	register struct nameidata *ndp;
96012756Ssam {
96112756Ssam 	register struct inode *ip, *dp;
96237737Smckusick 	int error = 0;
96312756Ssam 
96437737Smckusick 	ip = VTOI(ndp->ni_vp);
96537737Smckusick 	dp = VTOI(ndp->ni_dvp);
96612756Ssam 	/*
96712756Ssam 	 * No rmdir "." please.
96812756Ssam 	 */
96912756Ssam 	if (dp == ip) {
97037737Smckusick 		vrele(ITOV(dp));
97112756Ssam 		iput(ip);
97237737Smckusick 		return (EINVAL);
97312756Ssam 	}
97412756Ssam 	/*
97512756Ssam 	 * Verify the directory is empty (and valid).
97612756Ssam 	 * (Rmdir ".." won't be valid since
97712756Ssam 	 *  ".." will contain a reference to
97812756Ssam 	 *  the current directory and thus be
97912756Ssam 	 *  non-empty.)
98012756Ssam 	 */
98137737Smckusick 	if (ip->i_nlink != 2 || !dirempty(ip, dp->i_number, ndp->ni_cred)) {
98237737Smckusick 		error = ENOTEMPTY;
98312756Ssam 		goto out;
98412756Ssam 	}
98512756Ssam 	/*
98612756Ssam 	 * Delete reference to directory before purging
98712756Ssam 	 * inode.  If we crash in between, the directory
98812756Ssam 	 * will be reattached to lost+found,
98912756Ssam 	 */
99037737Smckusick 	if (error = dirremove(ndp))
99112756Ssam 		goto out;
99212756Ssam 	dp->i_nlink--;
99312756Ssam 	dp->i_flag |= ICHG;
99437737Smckusick 	cache_purge(ITOV(dp));
99512756Ssam 	iput(dp);
99637737Smckusick 	ndp->ni_dvp = NULL;
99712756Ssam 	/*
99812756Ssam 	 * Truncate inode.  The only stuff left
99912756Ssam 	 * in the directory is "." and "..".  The
100012756Ssam 	 * "." reference is inconsequential since
100112756Ssam 	 * we're quashing it.  The ".." reference
100212756Ssam 	 * has already been adjusted above.  We've
100312756Ssam 	 * removed the "." reference and the reference
100412756Ssam 	 * in the parent directory, but there may be
100512756Ssam 	 * other hard links so decrement by 2 and
100612756Ssam 	 * worry about them later.
100712756Ssam 	 */
100812756Ssam 	ip->i_nlink -= 2;
100937737Smckusick 	error = itrunc(ip, (u_long)0);
101037737Smckusick 	cache_purge(ITOV(ip));
101112756Ssam out:
101237737Smckusick 	if (ndp->ni_dvp)
101312756Ssam 		iput(dp);
101412756Ssam 	iput(ip);
101537737Smckusick 	return (error);
101612756Ssam }
101712756Ssam 
101837737Smckusick /*
101937737Smckusick  * symlink -- make a symbolic link
102037737Smckusick  */
102137737Smckusick ufs_symlink(ndp, vap, target)
102237737Smckusick 	struct nameidata *ndp;
102337737Smckusick 	struct vattr *vap;
102437737Smckusick 	char *target;
102512756Ssam {
102637737Smckusick 	struct inode *ip;
102737737Smckusick 	int error;
102812756Ssam 
102937737Smckusick 	error = maknode(IFLNK | vap->va_mode, ndp, &ip);
103037737Smckusick 	if (error)
103137737Smckusick 		return (error);
103237737Smckusick 	error = rdwri(UIO_WRITE, ip, target, strlen(target), (off_t)0,
103337737Smckusick 		UIO_SYSSPACE, ndp->ni_cred, (int *)0);
103437737Smckusick 	iput(ip);
103537737Smckusick 	return (error);
103637737Smckusick }
103737737Smckusick 
103837737Smckusick /*
103937737Smckusick  * Vnode op for read and write
104037737Smckusick  */
104137737Smckusick ufs_readdir(vp, uio, offp, cred)
104237737Smckusick 	struct vnode *vp;
104337737Smckusick 	register struct uio *uio;
104437737Smckusick 	off_t *offp;
104537737Smckusick 	struct ucred *cred;
104637737Smckusick {
104737737Smckusick 	register struct inode *ip = VTOI(vp);
104837737Smckusick 	int count, error;
104937737Smckusick 
105037737Smckusick 	ILOCK(ip);
105137737Smckusick 	uio->uio_offset = *offp;
105237737Smckusick 	count = uio->uio_resid;
105337737Smckusick 	count &= ~(DIRBLKSIZ - 1);
105437737Smckusick 	if (vp->v_type != VDIR || uio->uio_iovcnt != 1 ||
105537737Smckusick 	    (count < DIRBLKSIZ) || (uio->uio_offset & (DIRBLKSIZ -1))) {
105637737Smckusick 		IUNLOCK(ip);
105737737Smckusick 		return (EINVAL);
105816540Ssam 	}
105937737Smckusick 	uio->uio_resid = count;
106037737Smckusick 	uio->uio_iov->iov_len = count;
106137737Smckusick 	error = readip(ip, uio, cred);
106237737Smckusick 	*offp += count - uio->uio_resid;
106337737Smckusick 	IUNLOCK(ip);
106437737Smckusick 	return (error);
106537737Smckusick }
106637737Smckusick 
106737737Smckusick /*
106837737Smckusick  * Return target name of a symbolic link
106937737Smckusick  */
107037737Smckusick ufs_readlink(vp, uiop, cred)
107137737Smckusick 	struct vnode *vp;
107237737Smckusick 	struct uio *uiop;
107337737Smckusick 	struct ucred *cred;
107437737Smckusick {
107537737Smckusick 
107637737Smckusick 	return (readip(VTOI(vp), uiop, cred));
107737737Smckusick }
107837737Smckusick 
107937737Smckusick /*
108037737Smckusick  * Ufs abort op, called after namei() when a CREATE/DELETE isn't actually
108137737Smckusick  * done. Iff ni_vp/ni_dvp not null and locked, unlock.
108237737Smckusick  */
108337737Smckusick ufs_abortop(ndp)
108437737Smckusick 	register struct nameidata *ndp;
108537737Smckusick {
108637737Smckusick 	register struct inode *ip;
108737737Smckusick 
108837737Smckusick 	if (ndp->ni_vp) {
108937737Smckusick 		ip = VTOI(ndp->ni_vp);
109037737Smckusick 		if (ip->i_flag & ILOCKED)
109137737Smckusick 			IUNLOCK(ip);
109237737Smckusick 		vrele(ndp->ni_vp);
109312756Ssam 	}
109437737Smckusick 	if (ndp->ni_dvp) {
109537737Smckusick 		ip = VTOI(ndp->ni_dvp);
109637737Smckusick 		if (ip->i_flag & ILOCKED)
109737737Smckusick 			IUNLOCK(ip);
109837737Smckusick 		vrele(ndp->ni_dvp);
109937737Smckusick 	}
110037737Smckusick 	return;
110112756Ssam }
110212756Ssam 
110337737Smckusick ufs_lock(vp)
110437737Smckusick 	struct vnode *vp;
110537737Smckusick {
110637737Smckusick 	register struct inode *ip = VTOI(vp);
110737737Smckusick 
110837737Smckusick 	ILOCK(ip);
110937737Smckusick 	return (0);
111037737Smckusick }
111137737Smckusick 
111237737Smckusick ufs_unlock(vp)
111337737Smckusick 	struct vnode *vp;
111437737Smckusick {
111537737Smckusick 	register struct inode *ip = VTOI(vp);
111637737Smckusick 
111737737Smckusick 	if (!(ip->i_flag & ILOCKED))
111837737Smckusick 		panic("ufs_unlock NOT LOCKED");
111937737Smckusick 	IUNLOCK(ip);
112037737Smckusick 	return (0);
112137737Smckusick }
112237737Smckusick 
112312756Ssam /*
112437737Smckusick  * Get access to bmap
112512756Ssam  */
112637737Smckusick ufs_bmap(vp, bn, vpp, bnp)
112737737Smckusick 	struct vnode *vp;
112837737Smckusick 	daddr_t bn;
112937737Smckusick 	struct vnode **vpp;
113037737Smckusick 	daddr_t *bnp;
113112756Ssam {
113237737Smckusick 	struct inode *ip = VTOI(vp);
113312756Ssam 
113437737Smckusick 	if (vpp != NULL)
113537737Smckusick 		*vpp = ip->i_devvp;
113637737Smckusick 	if (bnp == NULL)
113737737Smckusick 		return (0);
113837737Smckusick 	return (bmap(ip, bn, bnp, (daddr_t *)0, (int *)0));
113912756Ssam }
114037737Smckusick 
114137737Smckusick /*
114237737Smckusick  * Just call the device strategy routine
114337737Smckusick  */
114437737Smckusick ufs_strategy(bp)
114537737Smckusick 	register struct buf *bp;
114637737Smckusick {
114737737Smckusick 	(*bdevsw[major(bp->b_dev)].d_strategy)(bp);
114837737Smckusick 	return (0);
114937737Smckusick }
115037737Smckusick 
115137737Smckusick /*
115237737Smckusick  * Make a new file.
115337737Smckusick  */
115437737Smckusick maknode(mode, ndp, ipp)
115537737Smckusick 	int mode;
115637737Smckusick 	register struct nameidata *ndp;
115737737Smckusick 	struct inode **ipp;
115837737Smckusick {
115937737Smckusick 	register struct inode *ip;
116037737Smckusick 	struct inode *tip;
116137737Smckusick 	register struct inode *pdir = VTOI(ndp->ni_dvp);
116237737Smckusick 	ino_t ipref;
116337737Smckusick 	int error;
116437737Smckusick 
116537737Smckusick 	*ipp = 0;
116637737Smckusick 	if ((mode & IFMT) == IFDIR)
116737737Smckusick 		ipref = dirpref(pdir->i_fs);
116837737Smckusick 	else
116937737Smckusick 		ipref = pdir->i_number;
117037737Smckusick 	error = ialloc(pdir, ipref, mode, &tip);
117137737Smckusick 	if (error) {
117237737Smckusick 		iput(pdir);
117337737Smckusick 		return (error);
117437737Smckusick 	}
117537737Smckusick 	ip = tip;
117637737Smckusick #ifdef QUOTA
117737737Smckusick 	if (ip->i_dquot != NODQUOT)
117837737Smckusick 		panic("maknode: dquot");
117937737Smckusick #endif
118037737Smckusick 	ip->i_flag |= IACC|IUPD|ICHG;
118137737Smckusick 	if ((mode & IFMT) == 0)
118237737Smckusick 		mode |= IFREG;
118337737Smckusick 	ip->i_mode = mode;
118437737Smckusick 	ITOV(ip)->v_type = IFTOVT(mode);	/* Rest init'd in iget() */
118537737Smckusick 	ip->i_nlink = 1;
118637737Smckusick 	ip->i_uid = ndp->ni_cred->cr_uid;
118737737Smckusick 	ip->i_gid = pdir->i_gid;
118837737Smckusick 	if ((ip->i_mode & ISGID) && !groupmember(ip->i_gid, ndp->ni_cred) &&
118937737Smckusick 	    suser(ndp->ni_cred, NULL))
119037737Smckusick 		ip->i_mode &= ~ISGID;
119137737Smckusick #ifdef QUOTA
119237737Smckusick 	ip->i_dquot = inoquota(ip);
119337737Smckusick #endif
119437737Smckusick 
119537737Smckusick 	/*
119637737Smckusick 	 * Make sure inode goes to disk before directory entry.
119737737Smckusick 	 */
119837737Smckusick 	if ((error = iupdat(ip, &time, &time, 1)) ||
119937737Smckusick 	    (error = direnter(ip, ndp))) {
120037737Smckusick 		/*
120137737Smckusick 		 * Write error occurred trying to update the inode
120237737Smckusick 		 * or the directory so must deallocate the inode.
120337737Smckusick 		 */
120437737Smckusick 		ip->i_nlink = 0;
120537737Smckusick 		ip->i_flag |= ICHG;
120637737Smckusick 		iput(ip);
120737737Smckusick 		return (error);
120837737Smckusick 	}
120937737Smckusick 	*ipp = ip;
121037737Smckusick 	return (0);
121137737Smckusick }
1212