xref: /csrg-svn/sys/kern/vfs_syscalls.c (revision 17758)
1*17758Smckusick /*	vfs_syscalls.c	6.15	85/01/21	*/
237Sbill 
317101Sbloom #include "param.h"
417101Sbloom #include "systm.h"
517101Sbloom #include "dir.h"
617101Sbloom #include "user.h"
717101Sbloom #include "kernel.h"
817101Sbloom #include "file.h"
917101Sbloom #include "stat.h"
1017101Sbloom #include "inode.h"
1117101Sbloom #include "fs.h"
1217101Sbloom #include "buf.h"
1317101Sbloom #include "proc.h"
1417101Sbloom #include "quota.h"
1517101Sbloom #include "uio.h"
1617101Sbloom #include "socket.h"
1717101Sbloom #include "socketvar.h"
1817101Sbloom #include "mount.h"
1937Sbill 
2012756Ssam extern	struct fileops inodeops;
2112756Ssam struct	file *getinode();
2212756Ssam 
239167Ssam /*
249167Ssam  * Change current working directory (``.'').
259167Ssam  */
266254Sroot chdir()
276254Sroot {
286254Sroot 
296254Sroot 	chdirec(&u.u_cdir);
306254Sroot }
316254Sroot 
329167Ssam /*
339167Ssam  * Change notion of root (``/'') directory.
349167Ssam  */
356254Sroot chroot()
366254Sroot {
376254Sroot 
386254Sroot 	if (suser())
396254Sroot 		chdirec(&u.u_rdir);
406254Sroot }
416254Sroot 
429167Ssam /*
439167Ssam  * Common routine for chroot and chdir.
449167Ssam  */
456254Sroot chdirec(ipp)
467701Ssam 	register struct inode **ipp;
476254Sroot {
486254Sroot 	register struct inode *ip;
496254Sroot 	struct a {
506254Sroot 		char	*fname;
5116694Smckusick 	} *uap = (struct a *)u.u_ap;
5216694Smckusick 	register struct nameidata *ndp = &u.u_nd;
536254Sroot 
5416694Smckusick 	ndp->ni_nameiop = LOOKUP | FOLLOW;
5516694Smckusick 	ndp->ni_segflg = UIO_USERSPACE;
5616694Smckusick 	ndp->ni_dirp = uap->fname;
5716694Smckusick 	ip = namei(ndp);
589167Ssam 	if (ip == NULL)
596254Sroot 		return;
609167Ssam 	if ((ip->i_mode&IFMT) != IFDIR) {
616254Sroot 		u.u_error = ENOTDIR;
626254Sroot 		goto bad;
636254Sroot 	}
649167Ssam 	if (access(ip, IEXEC))
656254Sroot 		goto bad;
6616664Smckusick 	IUNLOCK(ip);
677142Smckusick 	if (*ipp)
687142Smckusick 		irele(*ipp);
696254Sroot 	*ipp = ip;
706254Sroot 	return;
716254Sroot 
726254Sroot bad:
736254Sroot 	iput(ip);
746254Sroot }
756254Sroot 
7637Sbill /*
776254Sroot  * Open system call.
786254Sroot  */
796254Sroot open()
806254Sroot {
8112756Ssam 	struct a {
826254Sroot 		char	*fname;
837701Ssam 		int	mode;
8412756Ssam 		int	crtmode;
8512756Ssam 	} *uap = (struct a *) u.u_ap;
866254Sroot 
8716694Smckusick 	copen(uap->mode-FOPEN, uap->crtmode, uap->fname);
886254Sroot }
896254Sroot 
906254Sroot /*
916254Sroot  * Creat system call.
926254Sroot  */
9312756Ssam creat()
946254Sroot {
9512756Ssam 	struct a {
966254Sroot 		char	*fname;
976254Sroot 		int	fmode;
9812756Ssam 	} *uap = (struct a *)u.u_ap;
996254Sroot 
10016694Smckusick 	copen(FWRITE|FCREAT|FTRUNC, uap->fmode, uap->fname);
1016254Sroot }
1026254Sroot 
1036254Sroot /*
1046254Sroot  * Common code for open and creat.
10512756Ssam  * Check permissions, allocate an open file structure,
10612756Ssam  * and call the device open routine if any.
1076254Sroot  */
10816694Smckusick copen(mode, arg, fname)
10912756Ssam 	register int mode;
11012756Ssam 	int arg;
11116694Smckusick 	caddr_t fname;
11212756Ssam {
1136254Sroot 	register struct inode *ip;
1146254Sroot 	register struct file *fp;
11516694Smckusick 	register struct nameidata *ndp = &u.u_nd;
11612756Ssam 	int i;
1176254Sroot 
11812756Ssam #ifdef notdef
11912756Ssam 	if ((mode&(FREAD|FWRITE)) == 0) {
12012756Ssam 		u.u_error = EINVAL;
12112756Ssam 		return;
12212756Ssam 	}
12312756Ssam #endif
12416694Smckusick 	ndp->ni_segflg = UIO_USERSPACE;
12516694Smckusick 	ndp->ni_dirp = fname;
12612756Ssam 	if (mode&FCREAT) {
12716694Smckusick 		ndp->ni_nameiop = CREATE | FOLLOW;
12816694Smckusick 		ip = namei(ndp);
12912756Ssam 		if (ip == NULL) {
13012756Ssam 			if (u.u_error)
13112756Ssam 				return;
13216694Smckusick 			ip = maknode(arg&07777&(~ISVTX), ndp);
13312756Ssam 			if (ip == NULL)
13412756Ssam 				return;
13512756Ssam 			mode &= ~FTRUNC;
13612756Ssam 		} else {
13712756Ssam 			if (mode&FEXCL) {
13812756Ssam 				u.u_error = EEXIST;
13912756Ssam 				iput(ip);
14012756Ssam 				return;
14112756Ssam 			}
14212756Ssam 			mode &= ~FCREAT;
14312756Ssam 		}
14412756Ssam 	} else {
14516694Smckusick 		ndp->ni_nameiop = LOOKUP | FOLLOW;
14616694Smckusick 		ip = namei(ndp);
14712756Ssam 		if (ip == NULL)
14812756Ssam 			return;
14912756Ssam 	}
15012756Ssam 	if ((ip->i_mode & IFMT) == IFSOCK) {
15112756Ssam 		u.u_error = EOPNOTSUPP;
15212756Ssam 		goto bad;
15312756Ssam 	}
15412756Ssam 	if ((mode&FCREAT) == 0) {
1556254Sroot 		if (mode&FREAD)
1567701Ssam 			if (access(ip, IREAD))
1577701Ssam 				goto bad;
15816032Skarels 		if (mode&(FWRITE|FTRUNC)) {
1597701Ssam 			if (access(ip, IWRITE))
1607701Ssam 				goto bad;
1617701Ssam 			if ((ip->i_mode&IFMT) == IFDIR) {
1626254Sroot 				u.u_error = EISDIR;
1637701Ssam 				goto bad;
1647701Ssam 			}
1656254Sroot 		}
1666254Sroot 	}
16712756Ssam 	fp = falloc();
16812756Ssam 	if (fp == NULL)
16912756Ssam 		goto bad;
17012756Ssam 	if (mode&FTRUNC)
1719167Ssam 		itrunc(ip, (u_long)0);
17216664Smckusick 	IUNLOCK(ip);
17312756Ssam 	fp->f_flag = mode&FMASK;
17412756Ssam 	fp->f_type = DTYPE_INODE;
17512756Ssam 	fp->f_ops = &inodeops;
17612756Ssam 	fp->f_data = (caddr_t)ip;
1776254Sroot 	i = u.u_r.r_val1;
17812756Ssam 	if (setjmp(&u.u_qsave)) {
17912756Ssam 		if (u.u_error == 0)
18012756Ssam 			u.u_error = EINTR;
18112756Ssam 		u.u_ofile[i] = NULL;
18212756Ssam 		closef(fp);
18312756Ssam 		return;
18412756Ssam 	}
1858559Sroot 	u.u_error = openi(ip, mode);
18612756Ssam 	if (u.u_error == 0)
1876254Sroot 		return;
1886254Sroot 	u.u_ofile[i] = NULL;
1896254Sroot 	fp->f_count--;
1907142Smckusick 	irele(ip);
1917701Ssam 	return;
1927701Ssam bad:
1937701Ssam 	iput(ip);
1946254Sroot }
1956254Sroot 
1966254Sroot /*
1976254Sroot  * Mknod system call
1986254Sroot  */
1996254Sroot mknod()
2006254Sroot {
2016254Sroot 	register struct inode *ip;
2026254Sroot 	register struct a {
2036254Sroot 		char	*fname;
2046254Sroot 		int	fmode;
2056254Sroot 		int	dev;
20616694Smckusick 	} *uap = (struct a *)u.u_ap;
20716694Smckusick 	register struct nameidata *ndp = &u.u_nd;
2086254Sroot 
20912756Ssam 	if (!suser())
21012756Ssam 		return;
21116694Smckusick 	ndp->ni_nameiop = CREATE;
21216694Smckusick 	ndp->ni_segflg = UIO_USERSPACE;
21316694Smckusick 	ndp->ni_dirp = uap->fname;
21416694Smckusick 	ip = namei(ndp);
21512756Ssam 	if (ip != NULL) {
21612756Ssam 		u.u_error = EEXIST;
21712756Ssam 		goto out;
2186254Sroot 	}
2196254Sroot 	if (u.u_error)
2206254Sroot 		return;
22116694Smckusick 	ip = maknode(uap->fmode, ndp);
2226254Sroot 	if (ip == NULL)
2236254Sroot 		return;
22412756Ssam 	switch (ip->i_mode & IFMT) {
22512756Ssam 
22615093Smckusick 	case IFMT:	/* used by badsect to flag bad sectors */
22712756Ssam 	case IFCHR:
22812756Ssam 	case IFBLK:
22912756Ssam 		if (uap->dev) {
23012756Ssam 			/*
23112756Ssam 			 * Want to be able to use this to make badblock
23212756Ssam 			 * inodes, so don't truncate the dev number.
23312756Ssam 			 */
23412756Ssam 			ip->i_rdev = uap->dev;
23512756Ssam 			ip->i_flag |= IACC|IUPD|ICHG;
23612756Ssam 		}
2376254Sroot 	}
2386254Sroot 
2396254Sroot out:
2406254Sroot 	iput(ip);
2416254Sroot }
2426254Sroot 
2436254Sroot /*
2446254Sroot  * link system call
2456254Sroot  */
2466254Sroot link()
2476254Sroot {
2486254Sroot 	register struct inode *ip, *xp;
2496254Sroot 	register struct a {
2506254Sroot 		char	*target;
2516254Sroot 		char	*linkname;
25216694Smckusick 	} *uap = (struct a *)u.u_ap;
25316694Smckusick 	register struct nameidata *ndp = &u.u_nd;
2546254Sroot 
25516694Smckusick 	ndp->ni_nameiop = LOOKUP | FOLLOW;
25616694Smckusick 	ndp->ni_segflg = UIO_USERSPACE;
25716694Smckusick 	ndp->ni_dirp = uap->target;
25816694Smckusick 	ip = namei(ndp);	/* well, this routine is doomed anyhow */
2596254Sroot 	if (ip == NULL)
2606254Sroot 		return;
2619167Ssam 	if ((ip->i_mode&IFMT) == IFDIR && !suser()) {
2627439Sroot 		iput(ip);
2637439Sroot 		return;
2647439Sroot 	}
2656254Sroot 	ip->i_nlink++;
2666254Sroot 	ip->i_flag |= ICHG;
2678673Sroot 	iupdat(ip, &time, &time, 1);
26816664Smckusick 	IUNLOCK(ip);
26916694Smckusick 	ndp->ni_nameiop = CREATE;
27016694Smckusick 	ndp->ni_segflg = UIO_USERSPACE;
27116694Smckusick 	ndp->ni_dirp = (caddr_t)uap->linkname;
27216694Smckusick 	xp = namei(ndp);
2736254Sroot 	if (xp != NULL) {
2746254Sroot 		u.u_error = EEXIST;
2756254Sroot 		iput(xp);
2766254Sroot 		goto out;
2776254Sroot 	}
2786254Sroot 	if (u.u_error)
2796254Sroot 		goto out;
28016694Smckusick 	if (ndp->ni_pdir->i_dev != ip->i_dev) {
28116694Smckusick 		iput(ndp->ni_pdir);
2826254Sroot 		u.u_error = EXDEV;
2836254Sroot 		goto out;
2846254Sroot 	}
28516694Smckusick 	u.u_error = direnter(ip, ndp);
2866254Sroot out:
2876254Sroot 	if (u.u_error) {
2886254Sroot 		ip->i_nlink--;
2896254Sroot 		ip->i_flag |= ICHG;
2906254Sroot 	}
2917142Smckusick 	irele(ip);
2926254Sroot }
2936254Sroot 
2946254Sroot /*
2956254Sroot  * symlink -- make a symbolic link
2966254Sroot  */
2976254Sroot symlink()
2986254Sroot {
2996254Sroot 	register struct a {
3006254Sroot 		char	*target;
3016254Sroot 		char	*linkname;
30216694Smckusick 	} *uap = (struct a *)u.u_ap;
3036254Sroot 	register struct inode *ip;
3046254Sroot 	register char *tp;
3056254Sroot 	register c, nc;
30616694Smckusick 	register struct nameidata *ndp = &u.u_nd;
3076254Sroot 
3086254Sroot 	tp = uap->target;
3096254Sroot 	nc = 0;
3106254Sroot 	while (c = fubyte(tp)) {
3116254Sroot 		if (c < 0) {
3126254Sroot 			u.u_error = EFAULT;
3136254Sroot 			return;
3146254Sroot 		}
3156254Sroot 		tp++;
3166254Sroot 		nc++;
3176254Sroot 	}
31816694Smckusick 	ndp->ni_nameiop = CREATE;
31916694Smckusick 	ndp->ni_segflg = UIO_USERSPACE;
32016694Smckusick 	ndp->ni_dirp = uap->linkname;
32116694Smckusick 	ip = namei(ndp);
3226254Sroot 	if (ip) {
3236254Sroot 		iput(ip);
3246254Sroot 		u.u_error = EEXIST;
3256254Sroot 		return;
3266254Sroot 	}
3276254Sroot 	if (u.u_error)
3286254Sroot 		return;
32916694Smckusick 	ip = maknode(IFLNK | 0777, ndp);
3306254Sroot 	if (ip == NULL)
3316254Sroot 		return;
3327826Sroot 	u.u_error = rdwri(UIO_WRITE, ip, uap->target, nc, 0, 0, (int *)0);
3339167Ssam 	/* handle u.u_error != 0 */
3346254Sroot 	iput(ip);
3356254Sroot }
3366254Sroot 
3376254Sroot /*
3386254Sroot  * Unlink system call.
3396254Sroot  * Hard to avoid races here, especially
3406254Sroot  * in unlinking directories.
3416254Sroot  */
3426254Sroot unlink()
3436254Sroot {
3446254Sroot 	struct a {
3456254Sroot 		char	*fname;
34616694Smckusick 	} *uap = (struct a *)u.u_ap;
3479167Ssam 	register struct inode *ip, *dp;
34816694Smckusick 	register struct nameidata *ndp = &u.u_nd;
3496254Sroot 
35016694Smckusick 	ndp->ni_nameiop = DELETE | LOCKPARENT;
35116694Smckusick 	ndp->ni_segflg = UIO_USERSPACE;
35216694Smckusick 	ndp->ni_dirp = uap->fname;
35316694Smckusick 	ip = namei(ndp);
3549167Ssam 	if (ip == NULL)
3556254Sroot 		return;
35616694Smckusick 	dp = ndp->ni_pdir;
3579167Ssam 	if ((ip->i_mode&IFMT) == IFDIR && !suser())
3586254Sroot 		goto out;
3596254Sroot 	/*
3606254Sroot 	 * Don't unlink a mounted file.
3616254Sroot 	 */
3629167Ssam 	if (ip->i_dev != dp->i_dev) {
3636254Sroot 		u.u_error = EBUSY;
3646254Sroot 		goto out;
3656254Sroot 	}
3666254Sroot 	if (ip->i_flag&ITEXT)
3676254Sroot 		xrele(ip);	/* try once to free text */
36816694Smckusick 	if (dirremove(ndp)) {
3697535Sroot 		ip->i_nlink--;
3707535Sroot 		ip->i_flag |= ICHG;
3716254Sroot 	}
3726254Sroot out:
3739167Ssam 	if (dp == ip)
3747142Smckusick 		irele(ip);
3757142Smckusick 	else
3767142Smckusick 		iput(ip);
3779167Ssam 	iput(dp);
3786254Sroot }
3796254Sroot 
3806254Sroot /*
3816254Sroot  * Seek system call
3826254Sroot  */
3838040Sroot lseek()
3846254Sroot {
3856254Sroot 	register struct file *fp;
3866254Sroot 	register struct a {
3877701Ssam 		int	fd;
3886254Sroot 		off_t	off;
3896254Sroot 		int	sbase;
39016694Smckusick 	} *uap = (struct a *)u.u_ap;
3916254Sroot 
39216540Ssam 	GETF(fp, uap->fd);
39316540Ssam 	if (fp->f_type != DTYPE_INODE) {
39416540Ssam 		u.u_error = ESPIPE;
3956254Sroot 		return;
39616540Ssam 	}
39713878Ssam 	switch (uap->sbase) {
39813878Ssam 
39913878Ssam 	case L_INCR:
40013878Ssam 		fp->f_offset += uap->off;
40113878Ssam 		break;
40213878Ssam 
40313878Ssam 	case L_XTND:
40413878Ssam 		fp->f_offset = uap->off + ((struct inode *)fp->f_data)->i_size;
40513878Ssam 		break;
40613878Ssam 
40713878Ssam 	case L_SET:
40813878Ssam 		fp->f_offset = uap->off;
40913878Ssam 		break;
41013878Ssam 
41113878Ssam 	default:
41213878Ssam 		u.u_error = EINVAL;
41313878Ssam 		return;
41413878Ssam 	}
41513878Ssam 	u.u_r.r_off = fp->f_offset;
4166254Sroot }
4176254Sroot 
4186254Sroot /*
4196254Sroot  * Access system call
4206254Sroot  */
4216254Sroot saccess()
4226254Sroot {
4236254Sroot 	register svuid, svgid;
4246254Sroot 	register struct inode *ip;
4256254Sroot 	register struct a {
4266254Sroot 		char	*fname;
4276254Sroot 		int	fmode;
42816694Smckusick 	} *uap = (struct a *)u.u_ap;
42916694Smckusick 	register struct nameidata *ndp = &u.u_nd;
4306254Sroot 
4316254Sroot 	svuid = u.u_uid;
4326254Sroot 	svgid = u.u_gid;
4336254Sroot 	u.u_uid = u.u_ruid;
4346254Sroot 	u.u_gid = u.u_rgid;
43516694Smckusick 	ndp->ni_nameiop = LOOKUP | FOLLOW;
43616694Smckusick 	ndp->ni_segflg = UIO_USERSPACE;
43716694Smckusick 	ndp->ni_dirp = uap->fname;
43816694Smckusick 	ip = namei(ndp);
4396254Sroot 	if (ip != NULL) {
44012756Ssam 		if ((uap->fmode&R_OK) && access(ip, IREAD))
4417701Ssam 			goto done;
44212756Ssam 		if ((uap->fmode&W_OK) && access(ip, IWRITE))
4437701Ssam 			goto done;
44412756Ssam 		if ((uap->fmode&X_OK) && access(ip, IEXEC))
4457701Ssam 			goto done;
4467701Ssam done:
4476254Sroot 		iput(ip);
4486254Sroot 	}
4496254Sroot 	u.u_uid = svuid;
4506254Sroot 	u.u_gid = svgid;
4516254Sroot }
4526254Sroot 
4536254Sroot /*
4546574Smckusic  * Stat system call.  This version follows links.
45537Sbill  */
45637Sbill stat()
45737Sbill {
45837Sbill 
45916694Smckusick 	stat1(FOLLOW);
46037Sbill }
46137Sbill 
46237Sbill /*
4636574Smckusic  * Lstat system call.  This version does not follow links.
4645992Swnj  */
4655992Swnj lstat()
4665992Swnj {
46712756Ssam 
46816694Smckusick 	stat1(NOFOLLOW);
46912756Ssam }
47012756Ssam 
47112756Ssam stat1(follow)
47212756Ssam 	int follow;
47312756Ssam {
4745992Swnj 	register struct inode *ip;
4755992Swnj 	register struct a {
4765992Swnj 		char	*fname;
47712756Ssam 		struct stat *ub;
47816694Smckusick 	} *uap = (struct a *)u.u_ap;
47912756Ssam 	struct stat sb;
48016694Smckusick 	register struct nameidata *ndp = &u.u_nd;
4815992Swnj 
48216694Smckusick 	ndp->ni_nameiop = LOOKUP | follow;
48316694Smckusick 	ndp->ni_segflg = UIO_USERSPACE;
48416694Smckusick 	ndp->ni_dirp = uap->fname;
48516694Smckusick 	ip = namei(ndp);
4865992Swnj 	if (ip == NULL)
4875992Swnj 		return;
48813043Ssam 	(void) ino_stat(ip, &sb);
4895992Swnj 	iput(ip);
49012756Ssam 	u.u_error = copyout((caddr_t)&sb, (caddr_t)uap->ub, sizeof (sb));
4915992Swnj }
4925992Swnj 
4935992Swnj /*
4945992Swnj  * Return target name of a symbolic link
49537Sbill  */
4965992Swnj readlink()
4975992Swnj {
4985992Swnj 	register struct inode *ip;
4995992Swnj 	register struct a {
5005992Swnj 		char	*name;
5015992Swnj 		char	*buf;
5025992Swnj 		int	count;
5037826Sroot 	} *uap = (struct a *)u.u_ap;
50416694Smckusick 	register struct nameidata *ndp = &u.u_nd;
5057826Sroot 	int resid;
5065992Swnj 
50716694Smckusick 	ndp->ni_nameiop = LOOKUP;
50816694Smckusick 	ndp->ni_segflg = UIO_USERSPACE;
50916694Smckusick 	ndp->ni_dirp = uap->name;
51016694Smckusick 	ip = namei(ndp);
5115992Swnj 	if (ip == NULL)
5125992Swnj 		return;
5135992Swnj 	if ((ip->i_mode&IFMT) != IFLNK) {
5145992Swnj 		u.u_error = ENXIO;
5155992Swnj 		goto out;
5165992Swnj 	}
5177826Sroot 	u.u_error = rdwri(UIO_READ, ip, uap->buf, uap->count, 0, 0, &resid);
5185992Swnj out:
5195992Swnj 	iput(ip);
5207826Sroot 	u.u_r.r_val1 = uap->count - resid;
5215992Swnj }
5225992Swnj 
5239167Ssam /*
5249167Ssam  * Change mode of a file given path name.
5259167Ssam  */
5266254Sroot chmod()
5275992Swnj {
5287701Ssam 	struct inode *ip;
5297701Ssam 	struct a {
5306254Sroot 		char	*fname;
5316254Sroot 		int	fmode;
53216694Smckusick 	} *uap = (struct a *)u.u_ap;
5335992Swnj 
53416694Smckusick 	if ((ip = owner(uap->fname, FOLLOW)) == NULL)
5355992Swnj 		return;
5367701Ssam 	chmod1(ip, uap->fmode);
5379167Ssam 	iput(ip);
5387701Ssam }
5397439Sroot 
5409167Ssam /*
5419167Ssam  * Change mode of a file given a file descriptor.
5429167Ssam  */
5437701Ssam fchmod()
5447701Ssam {
5457701Ssam 	struct a {
5467701Ssam 		int	fd;
5477701Ssam 		int	fmode;
54816694Smckusick 	} *uap = (struct a *)u.u_ap;
5497701Ssam 	register struct inode *ip;
5507701Ssam 	register struct file *fp;
5517701Ssam 
55212756Ssam 	fp = getinode(uap->fd);
5537701Ssam 	if (fp == NULL)
5547701Ssam 		return;
55512756Ssam 	ip = (struct inode *)fp->f_data;
5569167Ssam 	if (u.u_uid != ip->i_uid && !suser())
5579167Ssam 		return;
55816664Smckusick 	ILOCK(ip);
5597701Ssam 	chmod1(ip, uap->fmode);
56016664Smckusick 	IUNLOCK(ip);
5617701Ssam }
5627701Ssam 
5639167Ssam /*
5649167Ssam  * Change the mode on a file.
5659167Ssam  * Inode must be locked before calling.
5669167Ssam  */
5677701Ssam chmod1(ip, mode)
5687701Ssam 	register struct inode *ip;
5697701Ssam 	register int mode;
5707701Ssam {
5717868Sroot 
5726254Sroot 	ip->i_mode &= ~07777;
5737439Sroot 	if (u.u_uid) {
5747701Ssam 		mode &= ~ISVTX;
57511811Ssam 		if (!groupmember(ip->i_gid))
57611811Ssam 			mode &= ~ISGID;
5777439Sroot 	}
5787701Ssam 	ip->i_mode |= mode&07777;
5796254Sroot 	ip->i_flag |= ICHG;
5806254Sroot 	if (ip->i_flag&ITEXT && (ip->i_mode&ISVTX)==0)
5816254Sroot 		xrele(ip);
5825992Swnj }
5835992Swnj 
5849167Ssam /*
5859167Ssam  * Set ownership given a path name.
5869167Ssam  */
5876254Sroot chown()
58837Sbill {
5897701Ssam 	struct inode *ip;
5907701Ssam 	struct a {
5916254Sroot 		char	*fname;
5926254Sroot 		int	uid;
5936254Sroot 		int	gid;
59416694Smckusick 	} *uap = (struct a *)u.u_ap;
59537Sbill 
59616694Smckusick 	if (!suser() || (ip = owner(uap->fname, NOFOLLOW)) == NULL)
59737Sbill 		return;
59811811Ssam 	u.u_error = chown1(ip, uap->uid, uap->gid);
5999167Ssam 	iput(ip);
6007701Ssam }
6017439Sroot 
6029167Ssam /*
6039167Ssam  * Set ownership given a file descriptor.
6049167Ssam  */
6057701Ssam fchown()
6067701Ssam {
6077701Ssam 	struct a {
6087701Ssam 		int	fd;
6097701Ssam 		int	uid;
6107701Ssam 		int	gid;
61116694Smckusick 	} *uap = (struct a *)u.u_ap;
6127701Ssam 	register struct inode *ip;
6137701Ssam 	register struct file *fp;
6147701Ssam 
61512756Ssam 	fp = getinode(uap->fd);
6167701Ssam 	if (fp == NULL)
6177701Ssam 		return;
61812756Ssam 	ip = (struct inode *)fp->f_data;
61911821Ssam 	if (!suser())
6209167Ssam 		return;
62116664Smckusick 	ILOCK(ip);
62211811Ssam 	u.u_error = chown1(ip, uap->uid, uap->gid);
62316664Smckusick 	IUNLOCK(ip);
6247701Ssam }
6257701Ssam 
6267701Ssam /*
6277701Ssam  * Perform chown operation on inode ip;
6287701Ssam  * inode must be locked prior to call.
6297701Ssam  */
6307701Ssam chown1(ip, uid, gid)
6317701Ssam 	register struct inode *ip;
6327701Ssam 	int uid, gid;
6337701Ssam {
6347701Ssam #ifdef QUOTA
6357701Ssam 	register long change;
63611811Ssam #endif
6377701Ssam 
63811811Ssam 	if (uid == -1)
63911811Ssam 		uid = ip->i_uid;
64011811Ssam 	if (gid == -1)
64111811Ssam 		gid = ip->i_gid;
64211811Ssam #ifdef QUOTA
64314385Ssam 	if (ip->i_uid == uid)		/* this just speeds things a little */
6447482Skre 		change = 0;
64512646Ssam 	else
64612646Ssam 		change = ip->i_blocks;
64712646Ssam 	(void) chkdq(ip, -change, 1);
64812646Ssam 	(void) chkiq(ip->i_dev, ip, ip->i_uid, 1);
6497482Skre 	dqrele(ip->i_dquot);
6507482Skre #endif
65111811Ssam 	ip->i_uid = uid;
65211811Ssam 	ip->i_gid = gid;
6536254Sroot 	ip->i_flag |= ICHG;
6546254Sroot 	if (u.u_ruid != 0)
6556254Sroot 		ip->i_mode &= ~(ISUID|ISGID);
6567701Ssam #ifdef QUOTA
6577482Skre 	ip->i_dquot = inoquota(ip);
65812646Ssam 	(void) chkdq(ip, change, 1);
65912646Ssam 	(void) chkiq(ip->i_dev, (struct inode *)NULL, uid, 1);
66012646Ssam 	return (u.u_error);		/* should == 0 ALWAYS !! */
66112646Ssam #else
66212646Ssam 	return (0);
6637482Skre #endif
66437Sbill }
66537Sbill 
66611811Ssam utimes()
66711811Ssam {
66811811Ssam 	register struct a {
66911811Ssam 		char	*fname;
67011811Ssam 		struct	timeval *tptr;
67111811Ssam 	} *uap = (struct a *)u.u_ap;
67211811Ssam 	register struct inode *ip;
67311811Ssam 	struct timeval tv[2];
67411811Ssam 
67516694Smckusick 	if ((ip = owner(uap->fname, FOLLOW)) == NULL)
67611811Ssam 		return;
67711811Ssam 	u.u_error = copyin((caddr_t)uap->tptr, (caddr_t)tv, sizeof (tv));
67811811Ssam 	if (u.u_error == 0) {
67911811Ssam 		ip->i_flag |= IACC|IUPD|ICHG;
68011811Ssam 		iupdat(ip, &tv[0], &tv[1], 0);
68111811Ssam 	}
68211811Ssam 	iput(ip);
68311811Ssam }
68411811Ssam 
6859167Ssam /*
6869167Ssam  * Flush any pending I/O.
6879167Ssam  */
6886254Sroot sync()
68937Sbill {
69037Sbill 
6918673Sroot 	update();
69237Sbill }
6937535Sroot 
6949167Ssam /*
6959167Ssam  * Truncate a file given its path name.
6969167Ssam  */
6977701Ssam truncate()
6987701Ssam {
6997701Ssam 	struct a {
7007701Ssam 		char	*fname;
7019167Ssam 		u_long	length;
7027826Sroot 	} *uap = (struct a *)u.u_ap;
7037701Ssam 	struct inode *ip;
70416694Smckusick 	register struct nameidata *ndp = &u.u_nd;
7057701Ssam 
70616694Smckusick 	ndp->ni_nameiop = LOOKUP | FOLLOW;
70716694Smckusick 	ndp->ni_segflg = UIO_USERSPACE;
70816694Smckusick 	ndp->ni_dirp = uap->fname;
70916694Smckusick 	ip = namei(ndp);
7107701Ssam 	if (ip == NULL)
7117701Ssam 		return;
7127701Ssam 	if (access(ip, IWRITE))
7137701Ssam 		goto bad;
7147701Ssam 	if ((ip->i_mode&IFMT) == IFDIR) {
7157701Ssam 		u.u_error = EISDIR;
7167701Ssam 		goto bad;
7177701Ssam 	}
7187701Ssam 	itrunc(ip, uap->length);
7197701Ssam bad:
7207701Ssam 	iput(ip);
7217701Ssam }
7227701Ssam 
7239167Ssam /*
7249167Ssam  * Truncate a file given a file descriptor.
7259167Ssam  */
7267701Ssam ftruncate()
7277701Ssam {
7287701Ssam 	struct a {
7297701Ssam 		int	fd;
7309167Ssam 		u_long	length;
7317826Sroot 	} *uap = (struct a *)u.u_ap;
7327701Ssam 	struct inode *ip;
7337701Ssam 	struct file *fp;
7347701Ssam 
73512756Ssam 	fp = getinode(uap->fd);
7367701Ssam 	if (fp == NULL)
7377701Ssam 		return;
7387701Ssam 	if ((fp->f_flag&FWRITE) == 0) {
7397701Ssam 		u.u_error = EINVAL;
7407701Ssam 		return;
7417701Ssam 	}
74212756Ssam 	ip = (struct inode *)fp->f_data;
74316664Smckusick 	ILOCK(ip);
7447701Ssam 	itrunc(ip, uap->length);
74516664Smckusick 	IUNLOCK(ip);
7467701Ssam }
7477701Ssam 
7489167Ssam /*
7499167Ssam  * Synch an open file.
7509167Ssam  */
7519167Ssam fsync()
7529167Ssam {
7539167Ssam 	struct a {
7549167Ssam 		int	fd;
7559167Ssam 	} *uap = (struct a *)u.u_ap;
7569167Ssam 	struct inode *ip;
7579167Ssam 	struct file *fp;
7589167Ssam 
75912756Ssam 	fp = getinode(uap->fd);
7609167Ssam 	if (fp == NULL)
7619167Ssam 		return;
76212756Ssam 	ip = (struct inode *)fp->f_data;
76316664Smckusick 	ILOCK(ip);
7649167Ssam 	syncip(ip);
76516664Smckusick 	IUNLOCK(ip);
7669167Ssam }
7679167Ssam 
7689167Ssam /*
7699167Ssam  * Rename system call.
7709167Ssam  * 	rename("foo", "bar");
7719167Ssam  * is essentially
7729167Ssam  *	unlink("bar");
7739167Ssam  *	link("foo", "bar");
7749167Ssam  *	unlink("foo");
7759167Ssam  * but ``atomically''.  Can't do full commit without saving state in the
7769167Ssam  * inode on disk which isn't feasible at this time.  Best we can do is
7779167Ssam  * always guarantee the target exists.
7789167Ssam  *
7799167Ssam  * Basic algorithm is:
7809167Ssam  *
7819167Ssam  * 1) Bump link count on source while we're linking it to the
7829167Ssam  *    target.  This also insure the inode won't be deleted out
78316776Smckusick  *    from underneath us while we work (it may be truncated by
78416776Smckusick  *    a concurrent `trunc' or `open' for creation).
7859167Ssam  * 2) Link source to destination.  If destination already exists,
7869167Ssam  *    delete it first.
78716776Smckusick  * 3) Unlink source reference to inode if still around. If a
78816776Smckusick  *    directory was moved and the parent of the destination
7899167Ssam  *    is different from the source, patch the ".." entry in the
7909167Ssam  *    directory.
7919167Ssam  *
7929167Ssam  * Source and destination must either both be directories, or both
7939167Ssam  * not be directories.  If target is a directory, it must be empty.
7949167Ssam  */
7957701Ssam rename()
7967701Ssam {
7977701Ssam 	struct a {
7987701Ssam 		char	*from;
7997701Ssam 		char	*to;
80016694Smckusick 	} *uap = (struct a *)u.u_ap;
8019167Ssam 	register struct inode *ip, *xp, *dp;
80216776Smckusick 	struct dirtemplate dirbuf;
80316776Smckusick 	int doingdirectory = 0, oldparent = 0, newparent = 0;
80416694Smckusick 	register struct nameidata *ndp = &u.u_nd;
80510051Ssam 	int error = 0;
8067701Ssam 
80716694Smckusick 	ndp->ni_nameiop = DELETE | LOCKPARENT;
80816694Smckusick 	ndp->ni_segflg = UIO_USERSPACE;
80916694Smckusick 	ndp->ni_dirp = uap->from;
81016694Smckusick 	ip = namei(ndp);
8119167Ssam 	if (ip == NULL)
8129167Ssam 		return;
81316694Smckusick 	dp = ndp->ni_pdir;
8149167Ssam 	if ((ip->i_mode&IFMT) == IFDIR) {
8159167Ssam 		register struct direct *d;
8169167Ssam 
81716694Smckusick 		d = &ndp->ni_dent;
8189167Ssam 		/*
81911641Ssam 		 * Avoid ".", "..", and aliases of "." for obvious reasons.
8209167Ssam 		 */
82111641Ssam 		if ((d->d_namlen == 1 && d->d_name[0] == '.') ||
82211641Ssam 		    (d->d_namlen == 2 && bcmp(d->d_name, "..", 2) == 0) ||
82316776Smckusick 		    (dp == ip) || (ip->i_flag & IRENAME)) {
82411641Ssam 			iput(dp);
82511641Ssam 			if (dp == ip)
82611641Ssam 				irele(ip);
82711641Ssam 			else
82810051Ssam 				iput(ip);
82911641Ssam 			u.u_error = EINVAL;
83011641Ssam 			return;
8319167Ssam 		}
83216776Smckusick 		ip->i_flag |= IRENAME;
8339167Ssam 		oldparent = dp->i_number;
8349167Ssam 		doingdirectory++;
8359167Ssam 	}
83611641Ssam 	iput(dp);
8379167Ssam 
8389167Ssam 	/*
8399167Ssam 	 * 1) Bump link count while we're moving stuff
8409167Ssam 	 *    around.  If we crash somewhere before
8419167Ssam 	 *    completing our work, the link count
8429167Ssam 	 *    may be wrong, but correctable.
8439167Ssam 	 */
8449167Ssam 	ip->i_nlink++;
8459167Ssam 	ip->i_flag |= ICHG;
8469167Ssam 	iupdat(ip, &time, &time, 1);
84716664Smckusick 	IUNLOCK(ip);
8489167Ssam 
8499167Ssam 	/*
8509167Ssam 	 * When the target exists, both the directory
8519167Ssam 	 * and target inodes are returned locked.
8529167Ssam 	 */
85316694Smckusick 	ndp->ni_nameiop = CREATE | LOCKPARENT | NOCACHE;
85416694Smckusick 	ndp->ni_dirp = (caddr_t)uap->to;
85516694Smckusick 	xp = namei(ndp);
85610051Ssam 	if (u.u_error) {
85710051Ssam 		error = u.u_error;
8589167Ssam 		goto out;
85910051Ssam 	}
86016694Smckusick 	dp = ndp->ni_pdir;
8619167Ssam 	/*
86211641Ssam 	 * If ".." must be changed (ie the directory gets a new
86312816Smckusick 	 * parent) then the source directory must not be in the
86412816Smckusick 	 * directory heirarchy above the target, as this would
86512816Smckusick 	 * orphan everything below the source directory. Also
86612816Smckusick 	 * the user must have write permission in the source so
86712816Smckusick 	 * as to be able to change "..". We must repeat the call
86812816Smckusick 	 * to namei, as the parent directory is unlocked by the
86912816Smckusick 	 * call to checkpath().
87011641Ssam 	 */
87116776Smckusick 	if (oldparent != dp->i_number)
87216776Smckusick 		newparent = dp->i_number;
87316776Smckusick 	if (doingdirectory && newparent) {
87412816Smckusick 		if (access(ip, IWRITE))
87512816Smckusick 			goto bad;
87612816Smckusick 		do {
87716694Smckusick 			dp = ndp->ni_pdir;
87812816Smckusick 			if (xp != NULL)
87912816Smckusick 				iput(xp);
88012816Smckusick 			u.u_error = checkpath(ip, dp);
88112816Smckusick 			if (u.u_error)
88212816Smckusick 				goto out;
88316694Smckusick 			xp = namei(ndp);
88412816Smckusick 			if (u.u_error) {
88512816Smckusick 				error = u.u_error;
88612816Smckusick 				goto out;
88712816Smckusick 			}
88816694Smckusick 		} while (dp != ndp->ni_pdir);
88912816Smckusick 	}
89011641Ssam 	/*
8919167Ssam 	 * 2) If target doesn't exist, link the target
8929167Ssam 	 *    to the source and unlink the source.
8939167Ssam 	 *    Otherwise, rewrite the target directory
8949167Ssam 	 *    entry to reference the source inode and
8959167Ssam 	 *    expunge the original entry's existence.
8969167Ssam 	 */
8979167Ssam 	if (xp == NULL) {
8989167Ssam 		if (dp->i_dev != ip->i_dev) {
89910051Ssam 			error = EXDEV;
9009167Ssam 			goto bad;
9019167Ssam 		}
9029167Ssam 		/*
90316776Smckusick 		 * Account for ".." in new directory.
90416776Smckusick 		 * When source and destination have the same
90516776Smckusick 		 * parent we don't fool with the link count.
9069167Ssam 		 */
90716776Smckusick 		if (doingdirectory && newparent) {
9089167Ssam 			dp->i_nlink++;
9099167Ssam 			dp->i_flag |= ICHG;
9109167Ssam 			iupdat(dp, &time, &time, 1);
9119167Ssam 		}
91216694Smckusick 		error = direnter(ip, ndp);
91310850Ssam 		if (error)
9149167Ssam 			goto out;
9159167Ssam 	} else {
9169167Ssam 		if (xp->i_dev != dp->i_dev || xp->i_dev != ip->i_dev) {
91710051Ssam 			error = EXDEV;
9189167Ssam 			goto bad;
9199167Ssam 		}
9209167Ssam 		/*
92110590Ssam 		 * Short circuit rename(foo, foo).
92210590Ssam 		 */
92310590Ssam 		if (xp->i_number == ip->i_number)
92410590Ssam 			goto bad;
92510590Ssam 		/*
92610051Ssam 		 * Target must be empty if a directory
92710051Ssam 		 * and have no links to it.
9289167Ssam 		 * Also, insure source and target are
9299167Ssam 		 * compatible (both directories, or both
9309167Ssam 		 * not directories).
9319167Ssam 		 */
9329167Ssam 		if ((xp->i_mode&IFMT) == IFDIR) {
93316776Smckusick 			if (!dirempty(xp, dp->i_number) || xp->i_nlink > 2) {
93410051Ssam 				error = ENOTEMPTY;
9359167Ssam 				goto bad;
9369167Ssam 			}
9379167Ssam 			if (!doingdirectory) {
93810051Ssam 				error = ENOTDIR;
9399167Ssam 				goto bad;
9409167Ssam 			}
94116776Smckusick 			cacheinval(dp);
9429167Ssam 		} else if (doingdirectory) {
94310051Ssam 			error = EISDIR;
9449167Ssam 			goto bad;
9459167Ssam 		}
94616694Smckusick 		dirrewrite(dp, ip, ndp);
94710051Ssam 		if (u.u_error) {
94810051Ssam 			error = u.u_error;
9499167Ssam 			goto bad1;
95010051Ssam 		}
9519167Ssam 		/*
95210051Ssam 		 * Adjust the link count of the target to
95310051Ssam 		 * reflect the dirrewrite above.  If this is
95410051Ssam 		 * a directory it is empty and there are
95510051Ssam 		 * no links to it, so we can squash the inode and
95610051Ssam 		 * any space associated with it.  We disallowed
95710051Ssam 		 * renaming over top of a directory with links to
95816776Smckusick 		 * it above, as the remaining link would point to
95916776Smckusick 		 * a directory without "." or ".." entries.
9609167Ssam 		 */
96110051Ssam 		xp->i_nlink--;
9629167Ssam 		if (doingdirectory) {
96310051Ssam 			if (--xp->i_nlink != 0)
96410051Ssam 				panic("rename: linked directory");
9659167Ssam 			itrunc(xp, (u_long)0);
96610051Ssam 		}
9679167Ssam 		xp->i_flag |= ICHG;
9689167Ssam 		iput(xp);
96910246Ssam 		xp = NULL;
9709167Ssam 	}
9719167Ssam 
9729167Ssam 	/*
9739167Ssam 	 * 3) Unlink the source.
9749167Ssam 	 */
97516694Smckusick 	ndp->ni_nameiop = DELETE | LOCKPARENT;
97616694Smckusick 	ndp->ni_segflg = UIO_USERSPACE;
97716694Smckusick 	ndp->ni_dirp = uap->from;
97816776Smckusick 	xp = namei(ndp);
979*17758Smckusick 	if (xp != NULL)
980*17758Smckusick 		dp = ndp->ni_pdir;
981*17758Smckusick 	else
982*17758Smckusick 		dp = NULL;
9839167Ssam 	/*
98416776Smckusick 	 * Insure that the directory entry still exists and has not
98516776Smckusick 	 * changed while the new name has been entered. If the source is
98616776Smckusick 	 * a file then the entry may have been unlinked or renamed. In
98716776Smckusick 	 * either case there is no further work to be done. If the source
98816776Smckusick 	 * is a directory then it cannot have been rmdir'ed; its link
98916776Smckusick 	 * count of three would cause a rmdir to fail with ENOTEMPTY.
99016776Smckusick 	 * The IRENAME flag insures that it cannot be moved by another
99116776Smckusick 	 * rename.
9929167Ssam 	 */
993*17758Smckusick 	if (xp != ip) {
99416776Smckusick 		if (doingdirectory)
995*17758Smckusick 			panic("rename: lost dir entry");
99616776Smckusick 	} else {
9979167Ssam 		/*
99816776Smckusick 		 * If the source is a directory with a
99916776Smckusick 		 * new parent, the link count of the old
100016776Smckusick 		 * parent directory must be decremented
100116776Smckusick 		 * and ".." set to point to the new parent.
10029167Ssam 		 */
100316776Smckusick 		if (doingdirectory && newparent) {
10049167Ssam 			dp->i_nlink--;
10059167Ssam 			dp->i_flag |= ICHG;
100616776Smckusick 			error = rdwri(UIO_READ, xp, (caddr_t)&dirbuf,
100716776Smckusick 				sizeof (struct dirtemplate), (off_t)0, 1,
100816776Smckusick 				(int *)0);
100916776Smckusick 			if (error == 0) {
101016776Smckusick 				if (dirbuf.dotdot_namlen != 2 ||
101116776Smckusick 				    dirbuf.dotdot_name[0] != '.' ||
101216776Smckusick 				    dirbuf.dotdot_name[1] != '.') {
101316776Smckusick 					printf("rename: mangled dir\n");
101416776Smckusick 				} else {
101516776Smckusick 					dirbuf.dotdot_ino = newparent;
101616776Smckusick 					(void) rdwri(UIO_WRITE, xp,
101716776Smckusick 					    (caddr_t)&dirbuf,
101816776Smckusick 					    sizeof (struct dirtemplate),
101916776Smckusick 					    (off_t)0, 1, (int *)0);
102016776Smckusick 					cacheinval(dp);
102116776Smckusick 				}
102216776Smckusick 			}
10239167Ssam 		}
102416694Smckusick 		if (dirremove(ndp)) {
102516776Smckusick 			xp->i_nlink--;
102616776Smckusick 			xp->i_flag |= ICHG;
10279167Ssam 		}
102816776Smckusick 		xp->i_flag &= ~IRENAME;
102916776Smckusick 		if (error == 0)		/* XXX conservative */
103010051Ssam 			error = u.u_error;
10319167Ssam 	}
10329167Ssam 	if (dp)
10339167Ssam 		iput(dp);
103416776Smckusick 	if (xp)
103516776Smckusick 		iput(xp);
103616776Smckusick 	irele(ip);
103716776Smckusick 	if (error)
103816776Smckusick 		u.u_error = error;
103916776Smckusick 	return;
10409167Ssam 
10419167Ssam bad:
104210246Ssam 	iput(dp);
10439167Ssam bad1:
10449167Ssam 	if (xp)
104510246Ssam 		iput(xp);
10469167Ssam out:
10479167Ssam 	ip->i_nlink--;
10489167Ssam 	ip->i_flag |= ICHG;
10499167Ssam 	irele(ip);
105010051Ssam 	if (error)
105110051Ssam 		u.u_error = error;
10527701Ssam }
10537701Ssam 
10547535Sroot /*
10557535Sroot  * Make a new file.
10567535Sroot  */
10577535Sroot struct inode *
105816694Smckusick maknode(mode, ndp)
10597535Sroot 	int mode;
106016694Smckusick 	register struct nameidata *ndp;
10617535Sroot {
10627535Sroot 	register struct inode *ip;
106316694Smckusick 	register struct inode *pdir = ndp->ni_pdir;
10647535Sroot 	ino_t ipref;
10657535Sroot 
10667535Sroot 	if ((mode & IFMT) == IFDIR)
106716694Smckusick 		ipref = dirpref(pdir->i_fs);
10687535Sroot 	else
106916694Smckusick 		ipref = pdir->i_number;
107016694Smckusick 	ip = ialloc(pdir, ipref, mode);
10717535Sroot 	if (ip == NULL) {
107216694Smckusick 		iput(pdir);
10737701Ssam 		return (NULL);
10747535Sroot 	}
10757701Ssam #ifdef QUOTA
10767535Sroot 	if (ip->i_dquot != NODQUOT)
10777535Sroot 		panic("maknode: dquot");
10787535Sroot #endif
10797535Sroot 	ip->i_flag |= IACC|IUPD|ICHG;
10807535Sroot 	if ((mode & IFMT) == 0)
10817535Sroot 		mode |= IFREG;
10827535Sroot 	ip->i_mode = mode & ~u.u_cmask;
10837535Sroot 	ip->i_nlink = 1;
10847535Sroot 	ip->i_uid = u.u_uid;
108516694Smckusick 	ip->i_gid = pdir->i_gid;
108611811Ssam 	if (ip->i_mode & ISGID && !groupmember(ip->i_gid))
108711811Ssam 		ip->i_mode &= ~ISGID;
10887701Ssam #ifdef QUOTA
10897535Sroot 	ip->i_dquot = inoquota(ip);
10907535Sroot #endif
10917535Sroot 
10927535Sroot 	/*
10937535Sroot 	 * Make sure inode goes to disk before directory entry.
10947535Sroot 	 */
10958673Sroot 	iupdat(ip, &time, &time, 1);
109616694Smckusick 	u.u_error = direnter(ip, ndp);
10977535Sroot 	if (u.u_error) {
10987535Sroot 		/*
109910850Ssam 		 * Write error occurred trying to update directory
110010850Ssam 		 * so must deallocate the inode.
11017535Sroot 		 */
11027535Sroot 		ip->i_nlink = 0;
11037535Sroot 		ip->i_flag |= ICHG;
11047535Sroot 		iput(ip);
11057701Ssam 		return (NULL);
11067535Sroot 	}
11077701Ssam 	return (ip);
11087535Sroot }
110912756Ssam 
111012756Ssam /*
111112756Ssam  * A virgin directory (no blushing please).
111212756Ssam  */
111312756Ssam struct dirtemplate mastertemplate = {
111412756Ssam 	0, 12, 1, ".",
111512756Ssam 	0, DIRBLKSIZ - 12, 2, ".."
111612756Ssam };
111712756Ssam 
111812756Ssam /*
111912756Ssam  * Mkdir system call
112012756Ssam  */
112112756Ssam mkdir()
112212756Ssam {
112312756Ssam 	struct a {
112412756Ssam 		char	*name;
112512756Ssam 		int	dmode;
112616694Smckusick 	} *uap = (struct a *)u.u_ap;
112712756Ssam 	register struct inode *ip, *dp;
112812756Ssam 	struct dirtemplate dirtemplate;
112916694Smckusick 	register struct nameidata *ndp = &u.u_nd;
113012756Ssam 
113116694Smckusick 	ndp->ni_nameiop = CREATE;
113216694Smckusick 	ndp->ni_segflg = UIO_USERSPACE;
113316694Smckusick 	ndp->ni_dirp = uap->name;
113416694Smckusick 	ip = namei(ndp);
113512756Ssam 	if (u.u_error)
113612756Ssam 		return;
113712756Ssam 	if (ip != NULL) {
113812756Ssam 		iput(ip);
113912756Ssam 		u.u_error = EEXIST;
114012756Ssam 		return;
114112756Ssam 	}
114216694Smckusick 	dp = ndp->ni_pdir;
114312756Ssam 	uap->dmode &= 0777;
114412756Ssam 	uap->dmode |= IFDIR;
114512756Ssam 	/*
114612756Ssam 	 * Must simulate part of maknode here
114712756Ssam 	 * in order to acquire the inode, but
114812756Ssam 	 * not have it entered in the parent
114912756Ssam 	 * directory.  The entry is made later
115012756Ssam 	 * after writing "." and ".." entries out.
115112756Ssam 	 */
115212756Ssam 	ip = ialloc(dp, dirpref(dp->i_fs), uap->dmode);
115312756Ssam 	if (ip == NULL) {
115412756Ssam 		iput(dp);
115512756Ssam 		return;
115612756Ssam 	}
115712756Ssam #ifdef QUOTA
115812756Ssam 	if (ip->i_dquot != NODQUOT)
115912756Ssam 		panic("mkdir: dquot");
116012756Ssam #endif
116112756Ssam 	ip->i_flag |= IACC|IUPD|ICHG;
116212756Ssam 	ip->i_mode = uap->dmode & ~u.u_cmask;
116312756Ssam 	ip->i_nlink = 2;
116412756Ssam 	ip->i_uid = u.u_uid;
116512756Ssam 	ip->i_gid = dp->i_gid;
116612756Ssam #ifdef QUOTA
116712756Ssam 	ip->i_dquot = inoquota(ip);
116812756Ssam #endif
116912756Ssam 	iupdat(ip, &time, &time, 1);
117012756Ssam 
117112756Ssam 	/*
117212756Ssam 	 * Bump link count in parent directory
117312756Ssam 	 * to reflect work done below.  Should
117412756Ssam 	 * be done before reference is created
117512756Ssam 	 * so reparation is possible if we crash.
117612756Ssam 	 */
117712756Ssam 	dp->i_nlink++;
117812756Ssam 	dp->i_flag |= ICHG;
117912756Ssam 	iupdat(dp, &time, &time, 1);
118012756Ssam 
118112756Ssam 	/*
118212756Ssam 	 * Initialize directory with "."
118312756Ssam 	 * and ".." from static template.
118412756Ssam 	 */
118512756Ssam 	dirtemplate = mastertemplate;
118612756Ssam 	dirtemplate.dot_ino = ip->i_number;
118712756Ssam 	dirtemplate.dotdot_ino = dp->i_number;
118812756Ssam 	u.u_error = rdwri(UIO_WRITE, ip, (caddr_t)&dirtemplate,
118912756Ssam 		sizeof (dirtemplate), (off_t)0, 1, (int *)0);
119012756Ssam 	if (u.u_error) {
119112756Ssam 		dp->i_nlink--;
119212756Ssam 		dp->i_flag |= ICHG;
119312756Ssam 		goto bad;
119412756Ssam 	}
119512756Ssam 	/*
119612756Ssam 	 * Directory all set up, now
119712756Ssam 	 * install the entry for it in
119812756Ssam 	 * the parent directory.
119912756Ssam 	 */
120016694Smckusick 	u.u_error = direnter(ip, ndp);
120112756Ssam 	dp = NULL;
120212756Ssam 	if (u.u_error) {
120316694Smckusick 		ndp->ni_nameiop = LOOKUP | NOCACHE;
120416694Smckusick 		ndp->ni_segflg = UIO_USERSPACE;
120516694Smckusick 		ndp->ni_dirp = uap->name;
120616694Smckusick 		dp = namei(ndp);
120712756Ssam 		if (dp) {
120812756Ssam 			dp->i_nlink--;
120912756Ssam 			dp->i_flag |= ICHG;
121012756Ssam 		}
121112756Ssam 	}
121212756Ssam bad:
121312756Ssam 	/*
121412756Ssam 	 * No need to do an explicit itrunc here,
121512756Ssam 	 * irele will do this for us because we set
121612756Ssam 	 * the link count to 0.
121712756Ssam 	 */
121812756Ssam 	if (u.u_error) {
121912756Ssam 		ip->i_nlink = 0;
122012756Ssam 		ip->i_flag |= ICHG;
122112756Ssam 	}
122212756Ssam 	if (dp)
122312756Ssam 		iput(dp);
122412756Ssam 	iput(ip);
122512756Ssam }
122612756Ssam 
122712756Ssam /*
122812756Ssam  * Rmdir system call.
122912756Ssam  */
123012756Ssam rmdir()
123112756Ssam {
123212756Ssam 	struct a {
123312756Ssam 		char	*name;
123416694Smckusick 	} *uap = (struct a *)u.u_ap;
123512756Ssam 	register struct inode *ip, *dp;
123616694Smckusick 	register struct nameidata *ndp = &u.u_nd;
123712756Ssam 
123816694Smckusick 	ndp->ni_nameiop = DELETE | LOCKPARENT;
123916694Smckusick 	ndp->ni_segflg = UIO_USERSPACE;
124016694Smckusick 	ndp->ni_dirp = uap->name;
124116694Smckusick 	ip = namei(ndp);
124212756Ssam 	if (ip == NULL)
124312756Ssam 		return;
124416694Smckusick 	dp = ndp->ni_pdir;
124512756Ssam 	/*
124612756Ssam 	 * No rmdir "." please.
124712756Ssam 	 */
124812756Ssam 	if (dp == ip) {
124912756Ssam 		irele(dp);
125012756Ssam 		iput(ip);
125112756Ssam 		u.u_error = EINVAL;
125212756Ssam 		return;
125312756Ssam 	}
125412756Ssam 	if ((ip->i_mode&IFMT) != IFDIR) {
125512756Ssam 		u.u_error = ENOTDIR;
125612756Ssam 		goto out;
125712756Ssam 	}
125812756Ssam 	/*
125912756Ssam 	 * Don't remove a mounted on directory.
126012756Ssam 	 */
126112756Ssam 	if (ip->i_dev != dp->i_dev) {
126212756Ssam 		u.u_error = EBUSY;
126312756Ssam 		goto out;
126412756Ssam 	}
126512756Ssam 	/*
126612756Ssam 	 * Verify the directory is empty (and valid).
126712756Ssam 	 * (Rmdir ".." won't be valid since
126812756Ssam 	 *  ".." will contain a reference to
126912756Ssam 	 *  the current directory and thus be
127012756Ssam 	 *  non-empty.)
127112756Ssam 	 */
127216776Smckusick 	if (ip->i_nlink != 2 || !dirempty(ip, dp->i_number)) {
127312756Ssam 		u.u_error = ENOTEMPTY;
127412756Ssam 		goto out;
127512756Ssam 	}
127612756Ssam 	/*
127712756Ssam 	 * Delete reference to directory before purging
127812756Ssam 	 * inode.  If we crash in between, the directory
127912756Ssam 	 * will be reattached to lost+found,
128012756Ssam 	 */
128116694Smckusick 	if (dirremove(ndp) == 0)
128212756Ssam 		goto out;
128312756Ssam 	dp->i_nlink--;
128412756Ssam 	dp->i_flag |= ICHG;
128516776Smckusick 	cacheinval(dp);
128612756Ssam 	iput(dp);
128712756Ssam 	dp = NULL;
128812756Ssam 	/*
128912756Ssam 	 * Truncate inode.  The only stuff left
129012756Ssam 	 * in the directory is "." and "..".  The
129112756Ssam 	 * "." reference is inconsequential since
129212756Ssam 	 * we're quashing it.  The ".." reference
129312756Ssam 	 * has already been adjusted above.  We've
129412756Ssam 	 * removed the "." reference and the reference
129512756Ssam 	 * in the parent directory, but there may be
129612756Ssam 	 * other hard links so decrement by 2 and
129712756Ssam 	 * worry about them later.
129812756Ssam 	 */
129912756Ssam 	ip->i_nlink -= 2;
130012756Ssam 	itrunc(ip, (u_long)0);
130116739Smckusick 	cacheinval(ip);
130212756Ssam out:
130312756Ssam 	if (dp)
130412756Ssam 		iput(dp);
130512756Ssam 	iput(ip);
130612756Ssam }
130712756Ssam 
130812756Ssam struct file *
130912756Ssam getinode(fdes)
131012756Ssam 	int fdes;
131112756Ssam {
131216540Ssam 	struct file *fp;
131312756Ssam 
131416540Ssam 	if ((unsigned)fdes >= NOFILE || (fp = u.u_ofile[fdes]) == NULL) {
131516540Ssam 		u.u_error = EBADF;
131616540Ssam 		return ((struct file *)0);
131716540Ssam 	}
131812756Ssam 	if (fp->f_type != DTYPE_INODE) {
131912756Ssam 		u.u_error = EINVAL;
132016540Ssam 		return ((struct file *)0);
132112756Ssam 	}
132212756Ssam 	return (fp);
132312756Ssam }
132412756Ssam 
132512756Ssam /*
132612756Ssam  * mode mask for creation of files
132712756Ssam  */
132812756Ssam umask()
132912756Ssam {
133012756Ssam 	register struct a {
133112756Ssam 		int	mask;
133216694Smckusick 	} *uap = (struct a *)u.u_ap;
133312756Ssam 
133412756Ssam 	u.u_r.r_val1 = u.u_cmask;
133512756Ssam 	u.u_cmask = uap->mask & 07777;
133612756Ssam }
1337