xref: /csrg-svn/sys/kern/vfs_syscalls.c (revision 30598)
123405Smckusick /*
229121Smckusick  * Copyright (c) 1982, 1986 Regents of the University of California.
323405Smckusick  * All rights reserved.  The Berkeley software License Agreement
423405Smckusick  * specifies the terms and conditions for redistribution.
523405Smckusick  *
6*30598Smckusick  *	@(#)vfs_syscalls.c	7.2 (Berkeley) 03/10/87
723405Smckusick  */
837Sbill 
917101Sbloom #include "param.h"
1017101Sbloom #include "systm.h"
1117101Sbloom #include "dir.h"
1217101Sbloom #include "user.h"
1317101Sbloom #include "kernel.h"
1417101Sbloom #include "file.h"
1517101Sbloom #include "stat.h"
1617101Sbloom #include "inode.h"
1717101Sbloom #include "fs.h"
1817101Sbloom #include "buf.h"
1917101Sbloom #include "proc.h"
2017101Sbloom #include "quota.h"
2117101Sbloom #include "uio.h"
2217101Sbloom #include "socket.h"
2317101Sbloom #include "socketvar.h"
2417101Sbloom #include "mount.h"
2537Sbill 
2612756Ssam extern	struct fileops inodeops;
2712756Ssam struct	file *getinode();
2812756Ssam 
299167Ssam /*
309167Ssam  * Change current working directory (``.'').
319167Ssam  */
326254Sroot chdir()
336254Sroot {
346254Sroot 
356254Sroot 	chdirec(&u.u_cdir);
366254Sroot }
376254Sroot 
389167Ssam /*
399167Ssam  * Change notion of root (``/'') directory.
409167Ssam  */
416254Sroot chroot()
426254Sroot {
436254Sroot 
446254Sroot 	if (suser())
456254Sroot 		chdirec(&u.u_rdir);
466254Sroot }
476254Sroot 
489167Ssam /*
499167Ssam  * Common routine for chroot and chdir.
509167Ssam  */
516254Sroot chdirec(ipp)
527701Ssam 	register struct inode **ipp;
536254Sroot {
546254Sroot 	register struct inode *ip;
556254Sroot 	struct a {
566254Sroot 		char	*fname;
5716694Smckusick 	} *uap = (struct a *)u.u_ap;
5816694Smckusick 	register struct nameidata *ndp = &u.u_nd;
596254Sroot 
6016694Smckusick 	ndp->ni_nameiop = LOOKUP | FOLLOW;
6116694Smckusick 	ndp->ni_segflg = UIO_USERSPACE;
6216694Smckusick 	ndp->ni_dirp = uap->fname;
6316694Smckusick 	ip = namei(ndp);
649167Ssam 	if (ip == NULL)
656254Sroot 		return;
669167Ssam 	if ((ip->i_mode&IFMT) != IFDIR) {
676254Sroot 		u.u_error = ENOTDIR;
686254Sroot 		goto bad;
696254Sroot 	}
709167Ssam 	if (access(ip, IEXEC))
716254Sroot 		goto bad;
7216664Smckusick 	IUNLOCK(ip);
737142Smckusick 	if (*ipp)
747142Smckusick 		irele(*ipp);
756254Sroot 	*ipp = ip;
766254Sroot 	return;
776254Sroot 
786254Sroot bad:
796254Sroot 	iput(ip);
806254Sroot }
816254Sroot 
8237Sbill /*
836254Sroot  * Open system call.
846254Sroot  */
856254Sroot open()
866254Sroot {
8712756Ssam 	struct a {
886254Sroot 		char	*fname;
897701Ssam 		int	mode;
9012756Ssam 		int	crtmode;
9112756Ssam 	} *uap = (struct a *) u.u_ap;
926254Sroot 
9316694Smckusick 	copen(uap->mode-FOPEN, uap->crtmode, uap->fname);
946254Sroot }
956254Sroot 
966254Sroot /*
976254Sroot  * Creat system call.
986254Sroot  */
9912756Ssam creat()
1006254Sroot {
10112756Ssam 	struct a {
1026254Sroot 		char	*fname;
1036254Sroot 		int	fmode;
10412756Ssam 	} *uap = (struct a *)u.u_ap;
1056254Sroot 
10616694Smckusick 	copen(FWRITE|FCREAT|FTRUNC, uap->fmode, uap->fname);
1076254Sroot }
1086254Sroot 
1096254Sroot /*
1106254Sroot  * Common code for open and creat.
11112756Ssam  * Check permissions, allocate an open file structure,
11212756Ssam  * and call the device open routine if any.
1136254Sroot  */
11416694Smckusick copen(mode, arg, fname)
11512756Ssam 	register int mode;
11612756Ssam 	int arg;
11716694Smckusick 	caddr_t fname;
11812756Ssam {
1196254Sroot 	register struct inode *ip;
1206254Sroot 	register struct file *fp;
12116694Smckusick 	register struct nameidata *ndp = &u.u_nd;
12224543Smckusick 	int indx;
1236254Sroot 
12424543Smckusick 	fp = falloc();
12524543Smckusick 	if (fp == NULL)
12612756Ssam 		return;
12724543Smckusick 	indx = u.u_r.r_val1;
12816694Smckusick 	ndp->ni_segflg = UIO_USERSPACE;
12916694Smckusick 	ndp->ni_dirp = fname;
13012756Ssam 	if (mode&FCREAT) {
13118416Smckusick 		if (mode & FEXCL)
13218416Smckusick 			ndp->ni_nameiop = CREATE;
13318416Smckusick 		else
13418416Smckusick 			ndp->ni_nameiop = CREATE | FOLLOW;
13516694Smckusick 		ip = namei(ndp);
13612756Ssam 		if (ip == NULL) {
13712756Ssam 			if (u.u_error)
13824543Smckusick 				goto bad1;
13916694Smckusick 			ip = maknode(arg&07777&(~ISVTX), ndp);
14012756Ssam 			if (ip == NULL)
14124543Smckusick 				goto bad1;
14212756Ssam 			mode &= ~FTRUNC;
14312756Ssam 		} else {
14412756Ssam 			if (mode&FEXCL) {
14512756Ssam 				u.u_error = EEXIST;
14624543Smckusick 				goto bad;
14712756Ssam 			}
14812756Ssam 			mode &= ~FCREAT;
14912756Ssam 		}
15012756Ssam 	} else {
15116694Smckusick 		ndp->ni_nameiop = LOOKUP | FOLLOW;
15216694Smckusick 		ip = namei(ndp);
15312756Ssam 		if (ip == NULL)
15424543Smckusick 			goto bad1;
15512756Ssam 	}
15612756Ssam 	if ((ip->i_mode & IFMT) == IFSOCK) {
15712756Ssam 		u.u_error = EOPNOTSUPP;
15812756Ssam 		goto bad;
15912756Ssam 	}
16012756Ssam 	if ((mode&FCREAT) == 0) {
1616254Sroot 		if (mode&FREAD)
1627701Ssam 			if (access(ip, IREAD))
1637701Ssam 				goto bad;
16416032Skarels 		if (mode&(FWRITE|FTRUNC)) {
1657701Ssam 			if (access(ip, IWRITE))
1667701Ssam 				goto bad;
1677701Ssam 			if ((ip->i_mode&IFMT) == IFDIR) {
1686254Sroot 				u.u_error = EISDIR;
1697701Ssam 				goto bad;
1707701Ssam 			}
1716254Sroot 		}
1726254Sroot 	}
17312756Ssam 	if (mode&FTRUNC)
1749167Ssam 		itrunc(ip, (u_long)0);
17516664Smckusick 	IUNLOCK(ip);
17612756Ssam 	fp->f_flag = mode&FMASK;
17712756Ssam 	fp->f_type = DTYPE_INODE;
17812756Ssam 	fp->f_ops = &inodeops;
17912756Ssam 	fp->f_data = (caddr_t)ip;
18012756Ssam 	if (setjmp(&u.u_qsave)) {
18112756Ssam 		if (u.u_error == 0)
18212756Ssam 			u.u_error = EINTR;
18324543Smckusick 		u.u_ofile[indx] = NULL;
18412756Ssam 		closef(fp);
18512756Ssam 		return;
18612756Ssam 	}
1878559Sroot 	u.u_error = openi(ip, mode);
18812756Ssam 	if (u.u_error == 0)
1896254Sroot 		return;
19024543Smckusick 	ILOCK(ip);
1917701Ssam bad:
1927701Ssam 	iput(ip);
19324543Smckusick bad1:
19424543Smckusick 	u.u_ofile[indx] = NULL;
19524543Smckusick 	fp->f_count--;
1966254Sroot }
1976254Sroot 
1986254Sroot /*
1996254Sroot  * Mknod system call
2006254Sroot  */
2016254Sroot mknod()
2026254Sroot {
2036254Sroot 	register struct inode *ip;
2046254Sroot 	register struct a {
2056254Sroot 		char	*fname;
2066254Sroot 		int	fmode;
2076254Sroot 		int	dev;
20816694Smckusick 	} *uap = (struct a *)u.u_ap;
20916694Smckusick 	register struct nameidata *ndp = &u.u_nd;
2106254Sroot 
21112756Ssam 	if (!suser())
21212756Ssam 		return;
21316694Smckusick 	ndp->ni_nameiop = CREATE;
21416694Smckusick 	ndp->ni_segflg = UIO_USERSPACE;
21516694Smckusick 	ndp->ni_dirp = uap->fname;
21616694Smckusick 	ip = namei(ndp);
21712756Ssam 	if (ip != NULL) {
21812756Ssam 		u.u_error = EEXIST;
21912756Ssam 		goto out;
2206254Sroot 	}
2216254Sroot 	if (u.u_error)
2226254Sroot 		return;
22316694Smckusick 	ip = maknode(uap->fmode, ndp);
2246254Sroot 	if (ip == NULL)
2256254Sroot 		return;
22612756Ssam 	switch (ip->i_mode & IFMT) {
22712756Ssam 
22815093Smckusick 	case IFMT:	/* used by badsect to flag bad sectors */
22912756Ssam 	case IFCHR:
23012756Ssam 	case IFBLK:
23112756Ssam 		if (uap->dev) {
23212756Ssam 			/*
23312756Ssam 			 * Want to be able to use this to make badblock
23412756Ssam 			 * inodes, so don't truncate the dev number.
23512756Ssam 			 */
23612756Ssam 			ip->i_rdev = uap->dev;
23712756Ssam 			ip->i_flag |= IACC|IUPD|ICHG;
23812756Ssam 		}
2396254Sroot 	}
2406254Sroot 
2416254Sroot out:
2426254Sroot 	iput(ip);
2436254Sroot }
2446254Sroot 
2456254Sroot /*
2466254Sroot  * link system call
2476254Sroot  */
2486254Sroot link()
2496254Sroot {
2506254Sroot 	register struct inode *ip, *xp;
2516254Sroot 	register struct a {
2526254Sroot 		char	*target;
2536254Sroot 		char	*linkname;
25416694Smckusick 	} *uap = (struct a *)u.u_ap;
25516694Smckusick 	register struct nameidata *ndp = &u.u_nd;
2566254Sroot 
25716694Smckusick 	ndp->ni_nameiop = LOOKUP | FOLLOW;
25816694Smckusick 	ndp->ni_segflg = UIO_USERSPACE;
25916694Smckusick 	ndp->ni_dirp = uap->target;
26016694Smckusick 	ip = namei(ndp);	/* well, this routine is doomed anyhow */
2616254Sroot 	if (ip == NULL)
2626254Sroot 		return;
2639167Ssam 	if ((ip->i_mode&IFMT) == IFDIR && !suser()) {
2647439Sroot 		iput(ip);
2657439Sroot 		return;
2667439Sroot 	}
2676254Sroot 	ip->i_nlink++;
2686254Sroot 	ip->i_flag |= ICHG;
2698673Sroot 	iupdat(ip, &time, &time, 1);
27016664Smckusick 	IUNLOCK(ip);
27116694Smckusick 	ndp->ni_nameiop = CREATE;
27216694Smckusick 	ndp->ni_segflg = UIO_USERSPACE;
27316694Smckusick 	ndp->ni_dirp = (caddr_t)uap->linkname;
27416694Smckusick 	xp = namei(ndp);
2756254Sroot 	if (xp != NULL) {
2766254Sroot 		u.u_error = EEXIST;
2776254Sroot 		iput(xp);
2786254Sroot 		goto out;
2796254Sroot 	}
2806254Sroot 	if (u.u_error)
2816254Sroot 		goto out;
28216694Smckusick 	if (ndp->ni_pdir->i_dev != ip->i_dev) {
28316694Smckusick 		iput(ndp->ni_pdir);
2846254Sroot 		u.u_error = EXDEV;
2856254Sroot 		goto out;
2866254Sroot 	}
28716694Smckusick 	u.u_error = direnter(ip, ndp);
2886254Sroot out:
2896254Sroot 	if (u.u_error) {
2906254Sroot 		ip->i_nlink--;
2916254Sroot 		ip->i_flag |= ICHG;
2926254Sroot 	}
2937142Smckusick 	irele(ip);
2946254Sroot }
2956254Sroot 
2966254Sroot /*
2976254Sroot  * symlink -- make a symbolic link
2986254Sroot  */
2996254Sroot symlink()
3006254Sroot {
3016254Sroot 	register struct a {
3026254Sroot 		char	*target;
3036254Sroot 		char	*linkname;
30416694Smckusick 	} *uap = (struct a *)u.u_ap;
3056254Sroot 	register struct inode *ip;
3066254Sroot 	register char *tp;
3076254Sroot 	register c, nc;
30816694Smckusick 	register struct nameidata *ndp = &u.u_nd;
3096254Sroot 
3106254Sroot 	tp = uap->target;
3116254Sroot 	nc = 0;
3126254Sroot 	while (c = fubyte(tp)) {
3136254Sroot 		if (c < 0) {
3146254Sroot 			u.u_error = EFAULT;
3156254Sroot 			return;
3166254Sroot 		}
3176254Sroot 		tp++;
3186254Sroot 		nc++;
3196254Sroot 	}
32016694Smckusick 	ndp->ni_nameiop = CREATE;
32116694Smckusick 	ndp->ni_segflg = UIO_USERSPACE;
32216694Smckusick 	ndp->ni_dirp = uap->linkname;
32316694Smckusick 	ip = namei(ndp);
3246254Sroot 	if (ip) {
3256254Sroot 		iput(ip);
3266254Sroot 		u.u_error = EEXIST;
3276254Sroot 		return;
3286254Sroot 	}
3296254Sroot 	if (u.u_error)
3306254Sroot 		return;
33116694Smckusick 	ip = maknode(IFLNK | 0777, ndp);
3326254Sroot 	if (ip == NULL)
3336254Sroot 		return;
33426361Skarels 	u.u_error = rdwri(UIO_WRITE, ip, uap->target, nc, (off_t)0, 0,
33526361Skarels 	    (int *)0);
3369167Ssam 	/* handle u.u_error != 0 */
3376254Sroot 	iput(ip);
3386254Sroot }
3396254Sroot 
3406254Sroot /*
3416254Sroot  * Unlink system call.
3426254Sroot  * Hard to avoid races here, especially
3436254Sroot  * in unlinking directories.
3446254Sroot  */
3456254Sroot unlink()
3466254Sroot {
3476254Sroot 	struct a {
3486254Sroot 		char	*fname;
34916694Smckusick 	} *uap = (struct a *)u.u_ap;
3509167Ssam 	register struct inode *ip, *dp;
35116694Smckusick 	register struct nameidata *ndp = &u.u_nd;
3526254Sroot 
35316694Smckusick 	ndp->ni_nameiop = DELETE | LOCKPARENT;
35416694Smckusick 	ndp->ni_segflg = UIO_USERSPACE;
35516694Smckusick 	ndp->ni_dirp = uap->fname;
35616694Smckusick 	ip = namei(ndp);
3579167Ssam 	if (ip == NULL)
3586254Sroot 		return;
35916694Smckusick 	dp = ndp->ni_pdir;
3609167Ssam 	if ((ip->i_mode&IFMT) == IFDIR && !suser())
3616254Sroot 		goto out;
3626254Sroot 	/*
3636254Sroot 	 * Don't unlink a mounted file.
3646254Sroot 	 */
3659167Ssam 	if (ip->i_dev != dp->i_dev) {
3666254Sroot 		u.u_error = EBUSY;
3676254Sroot 		goto out;
3686254Sroot 	}
3696254Sroot 	if (ip->i_flag&ITEXT)
3706254Sroot 		xrele(ip);	/* try once to free text */
37116694Smckusick 	if (dirremove(ndp)) {
3727535Sroot 		ip->i_nlink--;
3737535Sroot 		ip->i_flag |= ICHG;
3746254Sroot 	}
3756254Sroot out:
3769167Ssam 	if (dp == ip)
3777142Smckusick 		irele(ip);
3787142Smckusick 	else
3797142Smckusick 		iput(ip);
3809167Ssam 	iput(dp);
3816254Sroot }
3826254Sroot 
3836254Sroot /*
3846254Sroot  * Seek system call
3856254Sroot  */
3868040Sroot lseek()
3876254Sroot {
3886254Sroot 	register struct file *fp;
3896254Sroot 	register struct a {
3907701Ssam 		int	fd;
3916254Sroot 		off_t	off;
3926254Sroot 		int	sbase;
39316694Smckusick 	} *uap = (struct a *)u.u_ap;
3946254Sroot 
39516540Ssam 	GETF(fp, uap->fd);
39616540Ssam 	if (fp->f_type != DTYPE_INODE) {
39716540Ssam 		u.u_error = ESPIPE;
3986254Sroot 		return;
39916540Ssam 	}
40013878Ssam 	switch (uap->sbase) {
40113878Ssam 
40213878Ssam 	case L_INCR:
40313878Ssam 		fp->f_offset += uap->off;
40413878Ssam 		break;
40513878Ssam 
40613878Ssam 	case L_XTND:
40713878Ssam 		fp->f_offset = uap->off + ((struct inode *)fp->f_data)->i_size;
40813878Ssam 		break;
40913878Ssam 
41013878Ssam 	case L_SET:
41113878Ssam 		fp->f_offset = uap->off;
41213878Ssam 		break;
41313878Ssam 
41413878Ssam 	default:
41513878Ssam 		u.u_error = EINVAL;
41613878Ssam 		return;
41713878Ssam 	}
41813878Ssam 	u.u_r.r_off = fp->f_offset;
4196254Sroot }
4206254Sroot 
4216254Sroot /*
4226254Sroot  * Access system call
4236254Sroot  */
4246254Sroot saccess()
4256254Sroot {
4266254Sroot 	register svuid, svgid;
4276254Sroot 	register struct inode *ip;
4286254Sroot 	register struct a {
4296254Sroot 		char	*fname;
4306254Sroot 		int	fmode;
43116694Smckusick 	} *uap = (struct a *)u.u_ap;
43216694Smckusick 	register struct nameidata *ndp = &u.u_nd;
4336254Sroot 
4346254Sroot 	svuid = u.u_uid;
4356254Sroot 	svgid = u.u_gid;
4366254Sroot 	u.u_uid = u.u_ruid;
4376254Sroot 	u.u_gid = u.u_rgid;
43816694Smckusick 	ndp->ni_nameiop = LOOKUP | FOLLOW;
43916694Smckusick 	ndp->ni_segflg = UIO_USERSPACE;
44016694Smckusick 	ndp->ni_dirp = uap->fname;
44116694Smckusick 	ip = namei(ndp);
4426254Sroot 	if (ip != NULL) {
44312756Ssam 		if ((uap->fmode&R_OK) && access(ip, IREAD))
4447701Ssam 			goto done;
44512756Ssam 		if ((uap->fmode&W_OK) && access(ip, IWRITE))
4467701Ssam 			goto done;
44712756Ssam 		if ((uap->fmode&X_OK) && access(ip, IEXEC))
4487701Ssam 			goto done;
4497701Ssam done:
4506254Sroot 		iput(ip);
4516254Sroot 	}
4526254Sroot 	u.u_uid = svuid;
4536254Sroot 	u.u_gid = svgid;
4546254Sroot }
4556254Sroot 
4566254Sroot /*
4576574Smckusic  * Stat system call.  This version follows links.
45837Sbill  */
45937Sbill stat()
46037Sbill {
46137Sbill 
46216694Smckusick 	stat1(FOLLOW);
46337Sbill }
46437Sbill 
46537Sbill /*
4666574Smckusic  * Lstat system call.  This version does not follow links.
4675992Swnj  */
4685992Swnj lstat()
4695992Swnj {
47012756Ssam 
47116694Smckusick 	stat1(NOFOLLOW);
47212756Ssam }
47312756Ssam 
47412756Ssam stat1(follow)
47512756Ssam 	int follow;
47612756Ssam {
4775992Swnj 	register struct inode *ip;
4785992Swnj 	register struct a {
4795992Swnj 		char	*fname;
48012756Ssam 		struct stat *ub;
48116694Smckusick 	} *uap = (struct a *)u.u_ap;
48212756Ssam 	struct stat sb;
48316694Smckusick 	register struct nameidata *ndp = &u.u_nd;
4845992Swnj 
48516694Smckusick 	ndp->ni_nameiop = LOOKUP | follow;
48616694Smckusick 	ndp->ni_segflg = UIO_USERSPACE;
48716694Smckusick 	ndp->ni_dirp = uap->fname;
48816694Smckusick 	ip = namei(ndp);
4895992Swnj 	if (ip == NULL)
4905992Swnj 		return;
49113043Ssam 	(void) ino_stat(ip, &sb);
4925992Swnj 	iput(ip);
49312756Ssam 	u.u_error = copyout((caddr_t)&sb, (caddr_t)uap->ub, sizeof (sb));
4945992Swnj }
4955992Swnj 
4965992Swnj /*
4975992Swnj  * Return target name of a symbolic link
49837Sbill  */
4995992Swnj readlink()
5005992Swnj {
5015992Swnj 	register struct inode *ip;
5025992Swnj 	register struct a {
5035992Swnj 		char	*name;
5045992Swnj 		char	*buf;
5055992Swnj 		int	count;
5067826Sroot 	} *uap = (struct a *)u.u_ap;
50716694Smckusick 	register struct nameidata *ndp = &u.u_nd;
5087826Sroot 	int resid;
5095992Swnj 
51016694Smckusick 	ndp->ni_nameiop = LOOKUP;
51116694Smckusick 	ndp->ni_segflg = UIO_USERSPACE;
51216694Smckusick 	ndp->ni_dirp = uap->name;
51316694Smckusick 	ip = namei(ndp);
5145992Swnj 	if (ip == NULL)
5155992Swnj 		return;
5165992Swnj 	if ((ip->i_mode&IFMT) != IFLNK) {
51721015Smckusick 		u.u_error = EINVAL;
5185992Swnj 		goto out;
5195992Swnj 	}
52026361Skarels 	u.u_error = rdwri(UIO_READ, ip, uap->buf, uap->count, (off_t)0, 0,
52126361Skarels 	    &resid);
5225992Swnj out:
5235992Swnj 	iput(ip);
5247826Sroot 	u.u_r.r_val1 = uap->count - resid;
5255992Swnj }
5265992Swnj 
5279167Ssam /*
5289167Ssam  * Change mode of a file given path name.
5299167Ssam  */
5306254Sroot chmod()
5315992Swnj {
5327701Ssam 	struct inode *ip;
5337701Ssam 	struct a {
5346254Sroot 		char	*fname;
5356254Sroot 		int	fmode;
53616694Smckusick 	} *uap = (struct a *)u.u_ap;
5375992Swnj 
53816694Smckusick 	if ((ip = owner(uap->fname, FOLLOW)) == NULL)
5395992Swnj 		return;
54021015Smckusick 	u.u_error = chmod1(ip, uap->fmode);
5419167Ssam 	iput(ip);
5427701Ssam }
5437439Sroot 
5449167Ssam /*
5459167Ssam  * Change mode of a file given a file descriptor.
5469167Ssam  */
5477701Ssam fchmod()
5487701Ssam {
5497701Ssam 	struct a {
5507701Ssam 		int	fd;
5517701Ssam 		int	fmode;
55216694Smckusick 	} *uap = (struct a *)u.u_ap;
5537701Ssam 	register struct inode *ip;
5547701Ssam 	register struct file *fp;
5557701Ssam 
55612756Ssam 	fp = getinode(uap->fd);
5577701Ssam 	if (fp == NULL)
5587701Ssam 		return;
55912756Ssam 	ip = (struct inode *)fp->f_data;
5609167Ssam 	if (u.u_uid != ip->i_uid && !suser())
5619167Ssam 		return;
56216664Smckusick 	ILOCK(ip);
56321015Smckusick 	u.u_error = chmod1(ip, uap->fmode);
56416664Smckusick 	IUNLOCK(ip);
5657701Ssam }
5667701Ssam 
5679167Ssam /*
5689167Ssam  * Change the mode on a file.
5699167Ssam  * Inode must be locked before calling.
5709167Ssam  */
5717701Ssam chmod1(ip, mode)
5727701Ssam 	register struct inode *ip;
5737701Ssam 	register int mode;
5747701Ssam {
5757868Sroot 
57621015Smckusick 	if (ip->i_fs->fs_ronly)
57721015Smckusick 		return (EROFS);
5786254Sroot 	ip->i_mode &= ~07777;
5797439Sroot 	if (u.u_uid) {
58021015Smckusick 		if ((ip->i_mode & IFMT) != IFDIR)
58121015Smckusick 			mode &= ~ISVTX;
58211811Ssam 		if (!groupmember(ip->i_gid))
58311811Ssam 			mode &= ~ISGID;
5847439Sroot 	}
5857701Ssam 	ip->i_mode |= mode&07777;
5866254Sroot 	ip->i_flag |= ICHG;
5876254Sroot 	if (ip->i_flag&ITEXT && (ip->i_mode&ISVTX)==0)
5886254Sroot 		xrele(ip);
58921015Smckusick 	return (0);
5905992Swnj }
5915992Swnj 
5929167Ssam /*
5939167Ssam  * Set ownership given a path name.
5949167Ssam  */
5956254Sroot chown()
59637Sbill {
5977701Ssam 	struct inode *ip;
5987701Ssam 	struct a {
5996254Sroot 		char	*fname;
6006254Sroot 		int	uid;
6016254Sroot 		int	gid;
60216694Smckusick 	} *uap = (struct a *)u.u_ap;
60337Sbill 
60426473Skarels 	if ((ip = owner(uap->fname, NOFOLLOW)) == NULL)
60537Sbill 		return;
60611811Ssam 	u.u_error = chown1(ip, uap->uid, uap->gid);
6079167Ssam 	iput(ip);
6087701Ssam }
6097439Sroot 
6109167Ssam /*
6119167Ssam  * Set ownership given a file descriptor.
6129167Ssam  */
6137701Ssam fchown()
6147701Ssam {
6157701Ssam 	struct a {
6167701Ssam 		int	fd;
6177701Ssam 		int	uid;
6187701Ssam 		int	gid;
61916694Smckusick 	} *uap = (struct a *)u.u_ap;
6207701Ssam 	register struct inode *ip;
6217701Ssam 	register struct file *fp;
6227701Ssam 
62312756Ssam 	fp = getinode(uap->fd);
6247701Ssam 	if (fp == NULL)
6257701Ssam 		return;
62612756Ssam 	ip = (struct inode *)fp->f_data;
62716664Smckusick 	ILOCK(ip);
62811811Ssam 	u.u_error = chown1(ip, uap->uid, uap->gid);
62916664Smckusick 	IUNLOCK(ip);
6307701Ssam }
6317701Ssam 
6327701Ssam /*
6337701Ssam  * Perform chown operation on inode ip;
6347701Ssam  * inode must be locked prior to call.
6357701Ssam  */
6367701Ssam chown1(ip, uid, gid)
6377701Ssam 	register struct inode *ip;
6387701Ssam 	int uid, gid;
6397701Ssam {
6407701Ssam #ifdef QUOTA
6417701Ssam 	register long change;
64211811Ssam #endif
6437701Ssam 
64421015Smckusick 	if (ip->i_fs->fs_ronly)
64521015Smckusick 		return (EROFS);
64611811Ssam 	if (uid == -1)
64711811Ssam 		uid = ip->i_uid;
64811811Ssam 	if (gid == -1)
64911811Ssam 		gid = ip->i_gid;
65026473Skarels 	if (uid != ip->i_uid && !suser())
65126473Skarels 		return (u.u_error);
65226473Skarels 	if (gid != ip->i_gid && !groupmember((gid_t)gid) && !suser())
65326473Skarels 		return (u.u_error);
65411811Ssam #ifdef QUOTA
65514385Ssam 	if (ip->i_uid == uid)		/* this just speeds things a little */
6567482Skre 		change = 0;
65712646Ssam 	else
65812646Ssam 		change = ip->i_blocks;
65912646Ssam 	(void) chkdq(ip, -change, 1);
66012646Ssam 	(void) chkiq(ip->i_dev, ip, ip->i_uid, 1);
6617482Skre 	dqrele(ip->i_dquot);
6627482Skre #endif
66311811Ssam 	ip->i_uid = uid;
66411811Ssam 	ip->i_gid = gid;
6656254Sroot 	ip->i_flag |= ICHG;
6666254Sroot 	if (u.u_ruid != 0)
6676254Sroot 		ip->i_mode &= ~(ISUID|ISGID);
6687701Ssam #ifdef QUOTA
6697482Skre 	ip->i_dquot = inoquota(ip);
67012646Ssam 	(void) chkdq(ip, change, 1);
67126361Skarels 	(void) chkiq(ip->i_dev, (struct inode *)NULL, (uid_t)uid, 1);
67212646Ssam 	return (u.u_error);		/* should == 0 ALWAYS !! */
67312646Ssam #else
67412646Ssam 	return (0);
6757482Skre #endif
67637Sbill }
67737Sbill 
67811811Ssam utimes()
67911811Ssam {
68011811Ssam 	register struct a {
68111811Ssam 		char	*fname;
68211811Ssam 		struct	timeval *tptr;
68311811Ssam 	} *uap = (struct a *)u.u_ap;
68411811Ssam 	register struct inode *ip;
68511811Ssam 	struct timeval tv[2];
68611811Ssam 
68716694Smckusick 	if ((ip = owner(uap->fname, FOLLOW)) == NULL)
68811811Ssam 		return;
68921015Smckusick 	if (ip->i_fs->fs_ronly) {
69021015Smckusick 		u.u_error = EROFS;
69121015Smckusick 		iput(ip);
69221015Smckusick 		return;
69321015Smckusick 	}
69411811Ssam 	u.u_error = copyin((caddr_t)uap->tptr, (caddr_t)tv, sizeof (tv));
69511811Ssam 	if (u.u_error == 0) {
69611811Ssam 		ip->i_flag |= IACC|IUPD|ICHG;
69711811Ssam 		iupdat(ip, &tv[0], &tv[1], 0);
69811811Ssam 	}
69911811Ssam 	iput(ip);
70011811Ssam }
70111811Ssam 
7029167Ssam /*
7039167Ssam  * Flush any pending I/O.
7049167Ssam  */
7056254Sroot sync()
70637Sbill {
70737Sbill 
7088673Sroot 	update();
70937Sbill }
7107535Sroot 
7119167Ssam /*
7129167Ssam  * Truncate a file given its path name.
7139167Ssam  */
7147701Ssam truncate()
7157701Ssam {
7167701Ssam 	struct a {
7177701Ssam 		char	*fname;
71826473Skarels 		off_t	length;
7197826Sroot 	} *uap = (struct a *)u.u_ap;
7207701Ssam 	struct inode *ip;
72116694Smckusick 	register struct nameidata *ndp = &u.u_nd;
7227701Ssam 
72316694Smckusick 	ndp->ni_nameiop = LOOKUP | FOLLOW;
72416694Smckusick 	ndp->ni_segflg = UIO_USERSPACE;
72516694Smckusick 	ndp->ni_dirp = uap->fname;
72616694Smckusick 	ip = namei(ndp);
7277701Ssam 	if (ip == NULL)
7287701Ssam 		return;
7297701Ssam 	if (access(ip, IWRITE))
7307701Ssam 		goto bad;
7317701Ssam 	if ((ip->i_mode&IFMT) == IFDIR) {
7327701Ssam 		u.u_error = EISDIR;
7337701Ssam 		goto bad;
7347701Ssam 	}
73526473Skarels 	itrunc(ip, (u_long)uap->length);
7367701Ssam bad:
7377701Ssam 	iput(ip);
7387701Ssam }
7397701Ssam 
7409167Ssam /*
7419167Ssam  * Truncate a file given a file descriptor.
7429167Ssam  */
7437701Ssam ftruncate()
7447701Ssam {
7457701Ssam 	struct a {
7467701Ssam 		int	fd;
74726473Skarels 		off_t	length;
7487826Sroot 	} *uap = (struct a *)u.u_ap;
7497701Ssam 	struct inode *ip;
7507701Ssam 	struct file *fp;
7517701Ssam 
75212756Ssam 	fp = getinode(uap->fd);
7537701Ssam 	if (fp == NULL)
7547701Ssam 		return;
7557701Ssam 	if ((fp->f_flag&FWRITE) == 0) {
7567701Ssam 		u.u_error = EINVAL;
7577701Ssam 		return;
7587701Ssam 	}
75912756Ssam 	ip = (struct inode *)fp->f_data;
76016664Smckusick 	ILOCK(ip);
76126473Skarels 	itrunc(ip, (u_long)uap->length);
76216664Smckusick 	IUNLOCK(ip);
7637701Ssam }
7647701Ssam 
7659167Ssam /*
7669167Ssam  * Synch an open file.
7679167Ssam  */
7689167Ssam fsync()
7699167Ssam {
7709167Ssam 	struct a {
7719167Ssam 		int	fd;
7729167Ssam 	} *uap = (struct a *)u.u_ap;
7739167Ssam 	struct inode *ip;
7749167Ssam 	struct file *fp;
7759167Ssam 
77612756Ssam 	fp = getinode(uap->fd);
7779167Ssam 	if (fp == NULL)
7789167Ssam 		return;
77912756Ssam 	ip = (struct inode *)fp->f_data;
78016664Smckusick 	ILOCK(ip);
781*30598Smckusick 	if (fp->f_flag&FWRITE)
782*30598Smckusick 		ip->i_flag |= ICHG;
7839167Ssam 	syncip(ip);
78416664Smckusick 	IUNLOCK(ip);
7859167Ssam }
7869167Ssam 
7879167Ssam /*
7889167Ssam  * Rename system call.
7899167Ssam  * 	rename("foo", "bar");
7909167Ssam  * is essentially
7919167Ssam  *	unlink("bar");
7929167Ssam  *	link("foo", "bar");
7939167Ssam  *	unlink("foo");
7949167Ssam  * but ``atomically''.  Can't do full commit without saving state in the
7959167Ssam  * inode on disk which isn't feasible at this time.  Best we can do is
7969167Ssam  * always guarantee the target exists.
7979167Ssam  *
7989167Ssam  * Basic algorithm is:
7999167Ssam  *
8009167Ssam  * 1) Bump link count on source while we're linking it to the
8019167Ssam  *    target.  This also insure the inode won't be deleted out
80216776Smckusick  *    from underneath us while we work (it may be truncated by
80316776Smckusick  *    a concurrent `trunc' or `open' for creation).
8049167Ssam  * 2) Link source to destination.  If destination already exists,
8059167Ssam  *    delete it first.
80616776Smckusick  * 3) Unlink source reference to inode if still around. If a
80716776Smckusick  *    directory was moved and the parent of the destination
8089167Ssam  *    is different from the source, patch the ".." entry in the
8099167Ssam  *    directory.
8109167Ssam  *
8119167Ssam  * Source and destination must either both be directories, or both
8129167Ssam  * not be directories.  If target is a directory, it must be empty.
8139167Ssam  */
8147701Ssam rename()
8157701Ssam {
8167701Ssam 	struct a {
8177701Ssam 		char	*from;
8187701Ssam 		char	*to;
81916694Smckusick 	} *uap = (struct a *)u.u_ap;
8209167Ssam 	register struct inode *ip, *xp, *dp;
82116776Smckusick 	struct dirtemplate dirbuf;
82216776Smckusick 	int doingdirectory = 0, oldparent = 0, newparent = 0;
82316694Smckusick 	register struct nameidata *ndp = &u.u_nd;
82410051Ssam 	int error = 0;
8257701Ssam 
82616694Smckusick 	ndp->ni_nameiop = DELETE | LOCKPARENT;
82716694Smckusick 	ndp->ni_segflg = UIO_USERSPACE;
82816694Smckusick 	ndp->ni_dirp = uap->from;
82916694Smckusick 	ip = namei(ndp);
8309167Ssam 	if (ip == NULL)
8319167Ssam 		return;
83216694Smckusick 	dp = ndp->ni_pdir;
8339167Ssam 	if ((ip->i_mode&IFMT) == IFDIR) {
8349167Ssam 		register struct direct *d;
8359167Ssam 
83616694Smckusick 		d = &ndp->ni_dent;
8379167Ssam 		/*
83811641Ssam 		 * Avoid ".", "..", and aliases of "." for obvious reasons.
8399167Ssam 		 */
84011641Ssam 		if ((d->d_namlen == 1 && d->d_name[0] == '.') ||
84111641Ssam 		    (d->d_namlen == 2 && bcmp(d->d_name, "..", 2) == 0) ||
84216776Smckusick 		    (dp == ip) || (ip->i_flag & IRENAME)) {
84311641Ssam 			iput(dp);
84411641Ssam 			if (dp == ip)
84511641Ssam 				irele(ip);
84611641Ssam 			else
84710051Ssam 				iput(ip);
84811641Ssam 			u.u_error = EINVAL;
84911641Ssam 			return;
8509167Ssam 		}
85116776Smckusick 		ip->i_flag |= IRENAME;
8529167Ssam 		oldparent = dp->i_number;
8539167Ssam 		doingdirectory++;
8549167Ssam 	}
85511641Ssam 	iput(dp);
8569167Ssam 
8579167Ssam 	/*
8589167Ssam 	 * 1) Bump link count while we're moving stuff
8599167Ssam 	 *    around.  If we crash somewhere before
8609167Ssam 	 *    completing our work, the link count
8619167Ssam 	 *    may be wrong, but correctable.
8629167Ssam 	 */
8639167Ssam 	ip->i_nlink++;
8649167Ssam 	ip->i_flag |= ICHG;
8659167Ssam 	iupdat(ip, &time, &time, 1);
86616664Smckusick 	IUNLOCK(ip);
8679167Ssam 
8689167Ssam 	/*
8699167Ssam 	 * When the target exists, both the directory
8709167Ssam 	 * and target inodes are returned locked.
8719167Ssam 	 */
87216694Smckusick 	ndp->ni_nameiop = CREATE | LOCKPARENT | NOCACHE;
87316694Smckusick 	ndp->ni_dirp = (caddr_t)uap->to;
87416694Smckusick 	xp = namei(ndp);
87510051Ssam 	if (u.u_error) {
87610051Ssam 		error = u.u_error;
8779167Ssam 		goto out;
87810051Ssam 	}
87916694Smckusick 	dp = ndp->ni_pdir;
8809167Ssam 	/*
88111641Ssam 	 * If ".." must be changed (ie the directory gets a new
88212816Smckusick 	 * parent) then the source directory must not be in the
88312816Smckusick 	 * directory heirarchy above the target, as this would
88412816Smckusick 	 * orphan everything below the source directory. Also
88512816Smckusick 	 * the user must have write permission in the source so
88612816Smckusick 	 * as to be able to change "..". We must repeat the call
88712816Smckusick 	 * to namei, as the parent directory is unlocked by the
88812816Smckusick 	 * call to checkpath().
88911641Ssam 	 */
89016776Smckusick 	if (oldparent != dp->i_number)
89116776Smckusick 		newparent = dp->i_number;
89216776Smckusick 	if (doingdirectory && newparent) {
89312816Smckusick 		if (access(ip, IWRITE))
89412816Smckusick 			goto bad;
89512816Smckusick 		do {
89616694Smckusick 			dp = ndp->ni_pdir;
89712816Smckusick 			if (xp != NULL)
89812816Smckusick 				iput(xp);
89912816Smckusick 			u.u_error = checkpath(ip, dp);
90012816Smckusick 			if (u.u_error)
90112816Smckusick 				goto out;
90216694Smckusick 			xp = namei(ndp);
90312816Smckusick 			if (u.u_error) {
90412816Smckusick 				error = u.u_error;
90512816Smckusick 				goto out;
90612816Smckusick 			}
90716694Smckusick 		} while (dp != ndp->ni_pdir);
90812816Smckusick 	}
90911641Ssam 	/*
9109167Ssam 	 * 2) If target doesn't exist, link the target
9119167Ssam 	 *    to the source and unlink the source.
9129167Ssam 	 *    Otherwise, rewrite the target directory
9139167Ssam 	 *    entry to reference the source inode and
9149167Ssam 	 *    expunge the original entry's existence.
9159167Ssam 	 */
9169167Ssam 	if (xp == NULL) {
9179167Ssam 		if (dp->i_dev != ip->i_dev) {
91810051Ssam 			error = EXDEV;
9199167Ssam 			goto bad;
9209167Ssam 		}
9219167Ssam 		/*
92216776Smckusick 		 * Account for ".." in new directory.
92316776Smckusick 		 * When source and destination have the same
92416776Smckusick 		 * parent we don't fool with the link count.
9259167Ssam 		 */
92616776Smckusick 		if (doingdirectory && newparent) {
9279167Ssam 			dp->i_nlink++;
9289167Ssam 			dp->i_flag |= ICHG;
9299167Ssam 			iupdat(dp, &time, &time, 1);
9309167Ssam 		}
93116694Smckusick 		error = direnter(ip, ndp);
93210850Ssam 		if (error)
9339167Ssam 			goto out;
9349167Ssam 	} else {
9359167Ssam 		if (xp->i_dev != dp->i_dev || xp->i_dev != ip->i_dev) {
93610051Ssam 			error = EXDEV;
9379167Ssam 			goto bad;
9389167Ssam 		}
9399167Ssam 		/*
94010590Ssam 		 * Short circuit rename(foo, foo).
94110590Ssam 		 */
94210590Ssam 		if (xp->i_number == ip->i_number)
94310590Ssam 			goto bad;
94410590Ssam 		/*
94524433Sbloom 		 * If the parent directory is "sticky", then the user must
94624433Sbloom 		 * own the parent directory, or the destination of the rename,
94724433Sbloom 		 * otherwise the destination may not be changed (except by
94824433Sbloom 		 * root). This implements append-only directories.
94924433Sbloom 		 */
95024433Sbloom 		if ((dp->i_mode & ISVTX) && u.u_uid != 0 &&
95124433Sbloom 		    u.u_uid != dp->i_uid && xp->i_uid != u.u_uid) {
95224433Sbloom 			error = EPERM;
95324433Sbloom 			goto bad;
95424433Sbloom 		}
95524433Sbloom 		/*
95610051Ssam 		 * Target must be empty if a directory
95710051Ssam 		 * and have no links to it.
9589167Ssam 		 * Also, insure source and target are
9599167Ssam 		 * compatible (both directories, or both
9609167Ssam 		 * not directories).
9619167Ssam 		 */
9629167Ssam 		if ((xp->i_mode&IFMT) == IFDIR) {
96316776Smckusick 			if (!dirempty(xp, dp->i_number) || xp->i_nlink > 2) {
96410051Ssam 				error = ENOTEMPTY;
9659167Ssam 				goto bad;
9669167Ssam 			}
9679167Ssam 			if (!doingdirectory) {
96810051Ssam 				error = ENOTDIR;
9699167Ssam 				goto bad;
9709167Ssam 			}
97116776Smckusick 			cacheinval(dp);
9729167Ssam 		} else if (doingdirectory) {
97310051Ssam 			error = EISDIR;
9749167Ssam 			goto bad;
9759167Ssam 		}
97616694Smckusick 		dirrewrite(dp, ip, ndp);
97710051Ssam 		if (u.u_error) {
97810051Ssam 			error = u.u_error;
9799167Ssam 			goto bad1;
98010051Ssam 		}
9819167Ssam 		/*
98210051Ssam 		 * Adjust the link count of the target to
98310051Ssam 		 * reflect the dirrewrite above.  If this is
98410051Ssam 		 * a directory it is empty and there are
98510051Ssam 		 * no links to it, so we can squash the inode and
98610051Ssam 		 * any space associated with it.  We disallowed
98710051Ssam 		 * renaming over top of a directory with links to
98816776Smckusick 		 * it above, as the remaining link would point to
98916776Smckusick 		 * a directory without "." or ".." entries.
9909167Ssam 		 */
99110051Ssam 		xp->i_nlink--;
9929167Ssam 		if (doingdirectory) {
99310051Ssam 			if (--xp->i_nlink != 0)
99410051Ssam 				panic("rename: linked directory");
9959167Ssam 			itrunc(xp, (u_long)0);
99610051Ssam 		}
9979167Ssam 		xp->i_flag |= ICHG;
9989167Ssam 		iput(xp);
99910246Ssam 		xp = NULL;
10009167Ssam 	}
10019167Ssam 
10029167Ssam 	/*
10039167Ssam 	 * 3) Unlink the source.
10049167Ssam 	 */
100516694Smckusick 	ndp->ni_nameiop = DELETE | LOCKPARENT;
100616694Smckusick 	ndp->ni_segflg = UIO_USERSPACE;
100716694Smckusick 	ndp->ni_dirp = uap->from;
100816776Smckusick 	xp = namei(ndp);
100917758Smckusick 	if (xp != NULL)
101017758Smckusick 		dp = ndp->ni_pdir;
101117758Smckusick 	else
101217758Smckusick 		dp = NULL;
10139167Ssam 	/*
101416776Smckusick 	 * Insure that the directory entry still exists and has not
101516776Smckusick 	 * changed while the new name has been entered. If the source is
101616776Smckusick 	 * a file then the entry may have been unlinked or renamed. In
101716776Smckusick 	 * either case there is no further work to be done. If the source
101816776Smckusick 	 * is a directory then it cannot have been rmdir'ed; its link
101916776Smckusick 	 * count of three would cause a rmdir to fail with ENOTEMPTY.
102016776Smckusick 	 * The IRENAME flag insures that it cannot be moved by another
102116776Smckusick 	 * rename.
10229167Ssam 	 */
102317758Smckusick 	if (xp != ip) {
102416776Smckusick 		if (doingdirectory)
102517758Smckusick 			panic("rename: lost dir entry");
102616776Smckusick 	} else {
10279167Ssam 		/*
102816776Smckusick 		 * If the source is a directory with a
102916776Smckusick 		 * new parent, the link count of the old
103016776Smckusick 		 * parent directory must be decremented
103116776Smckusick 		 * and ".." set to point to the new parent.
10329167Ssam 		 */
103316776Smckusick 		if (doingdirectory && newparent) {
10349167Ssam 			dp->i_nlink--;
10359167Ssam 			dp->i_flag |= ICHG;
103616776Smckusick 			error = rdwri(UIO_READ, xp, (caddr_t)&dirbuf,
103716776Smckusick 				sizeof (struct dirtemplate), (off_t)0, 1,
103816776Smckusick 				(int *)0);
103916776Smckusick 			if (error == 0) {
104016776Smckusick 				if (dirbuf.dotdot_namlen != 2 ||
104116776Smckusick 				    dirbuf.dotdot_name[0] != '.' ||
104216776Smckusick 				    dirbuf.dotdot_name[1] != '.') {
104316776Smckusick 					printf("rename: mangled dir\n");
104416776Smckusick 				} else {
104516776Smckusick 					dirbuf.dotdot_ino = newparent;
104616776Smckusick 					(void) rdwri(UIO_WRITE, xp,
104716776Smckusick 					    (caddr_t)&dirbuf,
104816776Smckusick 					    sizeof (struct dirtemplate),
104916776Smckusick 					    (off_t)0, 1, (int *)0);
105016776Smckusick 					cacheinval(dp);
105116776Smckusick 				}
105216776Smckusick 			}
10539167Ssam 		}
105416694Smckusick 		if (dirremove(ndp)) {
105516776Smckusick 			xp->i_nlink--;
105616776Smckusick 			xp->i_flag |= ICHG;
10579167Ssam 		}
105816776Smckusick 		xp->i_flag &= ~IRENAME;
105916776Smckusick 		if (error == 0)		/* XXX conservative */
106010051Ssam 			error = u.u_error;
10619167Ssam 	}
10629167Ssam 	if (dp)
10639167Ssam 		iput(dp);
106416776Smckusick 	if (xp)
106516776Smckusick 		iput(xp);
106616776Smckusick 	irele(ip);
106716776Smckusick 	if (error)
106816776Smckusick 		u.u_error = error;
106916776Smckusick 	return;
10709167Ssam 
10719167Ssam bad:
107210246Ssam 	iput(dp);
10739167Ssam bad1:
10749167Ssam 	if (xp)
107510246Ssam 		iput(xp);
10769167Ssam out:
10779167Ssam 	ip->i_nlink--;
10789167Ssam 	ip->i_flag |= ICHG;
10799167Ssam 	irele(ip);
108010051Ssam 	if (error)
108110051Ssam 		u.u_error = error;
10827701Ssam }
10837701Ssam 
10847535Sroot /*
10857535Sroot  * Make a new file.
10867535Sroot  */
10877535Sroot struct inode *
108816694Smckusick maknode(mode, ndp)
10897535Sroot 	int mode;
109016694Smckusick 	register struct nameidata *ndp;
10917535Sroot {
10927535Sroot 	register struct inode *ip;
109316694Smckusick 	register struct inode *pdir = ndp->ni_pdir;
10947535Sroot 	ino_t ipref;
10957535Sroot 
10967535Sroot 	if ((mode & IFMT) == IFDIR)
109716694Smckusick 		ipref = dirpref(pdir->i_fs);
10987535Sroot 	else
109916694Smckusick 		ipref = pdir->i_number;
110016694Smckusick 	ip = ialloc(pdir, ipref, mode);
11017535Sroot 	if (ip == NULL) {
110216694Smckusick 		iput(pdir);
11037701Ssam 		return (NULL);
11047535Sroot 	}
11057701Ssam #ifdef QUOTA
11067535Sroot 	if (ip->i_dquot != NODQUOT)
11077535Sroot 		panic("maknode: dquot");
11087535Sroot #endif
11097535Sroot 	ip->i_flag |= IACC|IUPD|ICHG;
11107535Sroot 	if ((mode & IFMT) == 0)
11117535Sroot 		mode |= IFREG;
11127535Sroot 	ip->i_mode = mode & ~u.u_cmask;
11137535Sroot 	ip->i_nlink = 1;
11147535Sroot 	ip->i_uid = u.u_uid;
111516694Smckusick 	ip->i_gid = pdir->i_gid;
111611811Ssam 	if (ip->i_mode & ISGID && !groupmember(ip->i_gid))
111711811Ssam 		ip->i_mode &= ~ISGID;
11187701Ssam #ifdef QUOTA
11197535Sroot 	ip->i_dquot = inoquota(ip);
11207535Sroot #endif
11217535Sroot 
11227535Sroot 	/*
11237535Sroot 	 * Make sure inode goes to disk before directory entry.
11247535Sroot 	 */
11258673Sroot 	iupdat(ip, &time, &time, 1);
112616694Smckusick 	u.u_error = direnter(ip, ndp);
11277535Sroot 	if (u.u_error) {
11287535Sroot 		/*
112910850Ssam 		 * Write error occurred trying to update directory
113010850Ssam 		 * so must deallocate the inode.
11317535Sroot 		 */
11327535Sroot 		ip->i_nlink = 0;
11337535Sroot 		ip->i_flag |= ICHG;
11347535Sroot 		iput(ip);
11357701Ssam 		return (NULL);
11367535Sroot 	}
11377701Ssam 	return (ip);
11387535Sroot }
113912756Ssam 
114012756Ssam /*
114112756Ssam  * A virgin directory (no blushing please).
114212756Ssam  */
114312756Ssam struct dirtemplate mastertemplate = {
114412756Ssam 	0, 12, 1, ".",
114512756Ssam 	0, DIRBLKSIZ - 12, 2, ".."
114612756Ssam };
114712756Ssam 
114812756Ssam /*
114912756Ssam  * Mkdir system call
115012756Ssam  */
115112756Ssam mkdir()
115212756Ssam {
115312756Ssam 	struct a {
115412756Ssam 		char	*name;
115512756Ssam 		int	dmode;
115616694Smckusick 	} *uap = (struct a *)u.u_ap;
115712756Ssam 	register struct inode *ip, *dp;
115812756Ssam 	struct dirtemplate dirtemplate;
115916694Smckusick 	register struct nameidata *ndp = &u.u_nd;
116012756Ssam 
116116694Smckusick 	ndp->ni_nameiop = CREATE;
116216694Smckusick 	ndp->ni_segflg = UIO_USERSPACE;
116316694Smckusick 	ndp->ni_dirp = uap->name;
116416694Smckusick 	ip = namei(ndp);
116512756Ssam 	if (u.u_error)
116612756Ssam 		return;
116712756Ssam 	if (ip != NULL) {
116812756Ssam 		iput(ip);
116912756Ssam 		u.u_error = EEXIST;
117012756Ssam 		return;
117112756Ssam 	}
117216694Smckusick 	dp = ndp->ni_pdir;
117312756Ssam 	uap->dmode &= 0777;
117412756Ssam 	uap->dmode |= IFDIR;
117512756Ssam 	/*
117612756Ssam 	 * Must simulate part of maknode here
117712756Ssam 	 * in order to acquire the inode, but
117812756Ssam 	 * not have it entered in the parent
117912756Ssam 	 * directory.  The entry is made later
118012756Ssam 	 * after writing "." and ".." entries out.
118112756Ssam 	 */
118212756Ssam 	ip = ialloc(dp, dirpref(dp->i_fs), uap->dmode);
118312756Ssam 	if (ip == NULL) {
118412756Ssam 		iput(dp);
118512756Ssam 		return;
118612756Ssam 	}
118712756Ssam #ifdef QUOTA
118812756Ssam 	if (ip->i_dquot != NODQUOT)
118912756Ssam 		panic("mkdir: dquot");
119012756Ssam #endif
119112756Ssam 	ip->i_flag |= IACC|IUPD|ICHG;
119212756Ssam 	ip->i_mode = uap->dmode & ~u.u_cmask;
119312756Ssam 	ip->i_nlink = 2;
119412756Ssam 	ip->i_uid = u.u_uid;
119512756Ssam 	ip->i_gid = dp->i_gid;
119612756Ssam #ifdef QUOTA
119712756Ssam 	ip->i_dquot = inoquota(ip);
119812756Ssam #endif
119912756Ssam 	iupdat(ip, &time, &time, 1);
120012756Ssam 
120112756Ssam 	/*
120212756Ssam 	 * Bump link count in parent directory
120312756Ssam 	 * to reflect work done below.  Should
120412756Ssam 	 * be done before reference is created
120512756Ssam 	 * so reparation is possible if we crash.
120612756Ssam 	 */
120712756Ssam 	dp->i_nlink++;
120812756Ssam 	dp->i_flag |= ICHG;
120912756Ssam 	iupdat(dp, &time, &time, 1);
121012756Ssam 
121112756Ssam 	/*
121212756Ssam 	 * Initialize directory with "."
121312756Ssam 	 * and ".." from static template.
121412756Ssam 	 */
121512756Ssam 	dirtemplate = mastertemplate;
121612756Ssam 	dirtemplate.dot_ino = ip->i_number;
121712756Ssam 	dirtemplate.dotdot_ino = dp->i_number;
121812756Ssam 	u.u_error = rdwri(UIO_WRITE, ip, (caddr_t)&dirtemplate,
121912756Ssam 		sizeof (dirtemplate), (off_t)0, 1, (int *)0);
122012756Ssam 	if (u.u_error) {
122112756Ssam 		dp->i_nlink--;
122212756Ssam 		dp->i_flag |= ICHG;
122312756Ssam 		goto bad;
122412756Ssam 	}
122518103Smckusick 	if (DIRBLKSIZ > ip->i_fs->fs_fsize)
122618103Smckusick 		panic("mkdir: blksize");     /* XXX - should grow with bmap() */
122718103Smckusick 	else
122818103Smckusick 		ip->i_size = DIRBLKSIZ;
122912756Ssam 	/*
123012756Ssam 	 * Directory all set up, now
123112756Ssam 	 * install the entry for it in
123212756Ssam 	 * the parent directory.
123312756Ssam 	 */
123416694Smckusick 	u.u_error = direnter(ip, ndp);
123512756Ssam 	dp = NULL;
123612756Ssam 	if (u.u_error) {
123716694Smckusick 		ndp->ni_nameiop = LOOKUP | NOCACHE;
123816694Smckusick 		ndp->ni_segflg = UIO_USERSPACE;
123916694Smckusick 		ndp->ni_dirp = uap->name;
124016694Smckusick 		dp = namei(ndp);
124112756Ssam 		if (dp) {
124212756Ssam 			dp->i_nlink--;
124312756Ssam 			dp->i_flag |= ICHG;
124412756Ssam 		}
124512756Ssam 	}
124612756Ssam bad:
124712756Ssam 	/*
124812756Ssam 	 * No need to do an explicit itrunc here,
124912756Ssam 	 * irele will do this for us because we set
125012756Ssam 	 * the link count to 0.
125112756Ssam 	 */
125212756Ssam 	if (u.u_error) {
125312756Ssam 		ip->i_nlink = 0;
125412756Ssam 		ip->i_flag |= ICHG;
125512756Ssam 	}
125612756Ssam 	if (dp)
125712756Ssam 		iput(dp);
125812756Ssam 	iput(ip);
125912756Ssam }
126012756Ssam 
126112756Ssam /*
126212756Ssam  * Rmdir system call.
126312756Ssam  */
126412756Ssam rmdir()
126512756Ssam {
126612756Ssam 	struct a {
126712756Ssam 		char	*name;
126816694Smckusick 	} *uap = (struct a *)u.u_ap;
126912756Ssam 	register struct inode *ip, *dp;
127016694Smckusick 	register struct nameidata *ndp = &u.u_nd;
127112756Ssam 
127216694Smckusick 	ndp->ni_nameiop = DELETE | LOCKPARENT;
127316694Smckusick 	ndp->ni_segflg = UIO_USERSPACE;
127416694Smckusick 	ndp->ni_dirp = uap->name;
127516694Smckusick 	ip = namei(ndp);
127612756Ssam 	if (ip == NULL)
127712756Ssam 		return;
127816694Smckusick 	dp = ndp->ni_pdir;
127912756Ssam 	/*
128012756Ssam 	 * No rmdir "." please.
128112756Ssam 	 */
128212756Ssam 	if (dp == ip) {
128312756Ssam 		irele(dp);
128412756Ssam 		iput(ip);
128512756Ssam 		u.u_error = EINVAL;
128612756Ssam 		return;
128712756Ssam 	}
128812756Ssam 	if ((ip->i_mode&IFMT) != IFDIR) {
128912756Ssam 		u.u_error = ENOTDIR;
129012756Ssam 		goto out;
129112756Ssam 	}
129212756Ssam 	/*
129312756Ssam 	 * Don't remove a mounted on directory.
129412756Ssam 	 */
129512756Ssam 	if (ip->i_dev != dp->i_dev) {
129612756Ssam 		u.u_error = EBUSY;
129712756Ssam 		goto out;
129812756Ssam 	}
129912756Ssam 	/*
130012756Ssam 	 * Verify the directory is empty (and valid).
130112756Ssam 	 * (Rmdir ".." won't be valid since
130212756Ssam 	 *  ".." will contain a reference to
130312756Ssam 	 *  the current directory and thus be
130412756Ssam 	 *  non-empty.)
130512756Ssam 	 */
130616776Smckusick 	if (ip->i_nlink != 2 || !dirempty(ip, dp->i_number)) {
130712756Ssam 		u.u_error = ENOTEMPTY;
130812756Ssam 		goto out;
130912756Ssam 	}
131012756Ssam 	/*
131112756Ssam 	 * Delete reference to directory before purging
131212756Ssam 	 * inode.  If we crash in between, the directory
131312756Ssam 	 * will be reattached to lost+found,
131412756Ssam 	 */
131516694Smckusick 	if (dirremove(ndp) == 0)
131612756Ssam 		goto out;
131712756Ssam 	dp->i_nlink--;
131812756Ssam 	dp->i_flag |= ICHG;
131916776Smckusick 	cacheinval(dp);
132012756Ssam 	iput(dp);
132112756Ssam 	dp = NULL;
132212756Ssam 	/*
132312756Ssam 	 * Truncate inode.  The only stuff left
132412756Ssam 	 * in the directory is "." and "..".  The
132512756Ssam 	 * "." reference is inconsequential since
132612756Ssam 	 * we're quashing it.  The ".." reference
132712756Ssam 	 * has already been adjusted above.  We've
132812756Ssam 	 * removed the "." reference and the reference
132912756Ssam 	 * in the parent directory, but there may be
133012756Ssam 	 * other hard links so decrement by 2 and
133112756Ssam 	 * worry about them later.
133212756Ssam 	 */
133312756Ssam 	ip->i_nlink -= 2;
133412756Ssam 	itrunc(ip, (u_long)0);
133516739Smckusick 	cacheinval(ip);
133612756Ssam out:
133712756Ssam 	if (dp)
133812756Ssam 		iput(dp);
133912756Ssam 	iput(ip);
134012756Ssam }
134112756Ssam 
134212756Ssam struct file *
134312756Ssam getinode(fdes)
134412756Ssam 	int fdes;
134512756Ssam {
134616540Ssam 	struct file *fp;
134712756Ssam 
134816540Ssam 	if ((unsigned)fdes >= NOFILE || (fp = u.u_ofile[fdes]) == NULL) {
134916540Ssam 		u.u_error = EBADF;
135016540Ssam 		return ((struct file *)0);
135116540Ssam 	}
135212756Ssam 	if (fp->f_type != DTYPE_INODE) {
135312756Ssam 		u.u_error = EINVAL;
135416540Ssam 		return ((struct file *)0);
135512756Ssam 	}
135612756Ssam 	return (fp);
135712756Ssam }
135812756Ssam 
135912756Ssam /*
136012756Ssam  * mode mask for creation of files
136112756Ssam  */
136212756Ssam umask()
136312756Ssam {
136412756Ssam 	register struct a {
136512756Ssam 		int	mask;
136616694Smckusick 	} *uap = (struct a *)u.u_ap;
136712756Ssam 
136812756Ssam 	u.u_r.r_val1 = u.u_cmask;
136912756Ssam 	u.u_cmask = uap->mask & 07777;
137012756Ssam }
1371