xref: /csrg-svn/sys/kern/vfs_syscalls.c (revision 26473)
123405Smckusick /*
223405Smckusick  * Copyright (c) 1982 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*26473Skarels  *	@(#)vfs_syscalls.c	6.23 (Berkeley) 03/04/86
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 
604*26473Skarels 	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;
650*26473Skarels 	if (uid != ip->i_uid && !suser())
651*26473Skarels 		return (u.u_error);
652*26473Skarels 	if (gid != ip->i_gid && !groupmember((gid_t)gid) && !suser())
653*26473Skarels 		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;
718*26473Skarels 		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 	}
735*26473Skarels 	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;
747*26473Skarels 		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);
761*26473Skarels 	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);
7819167Ssam 	syncip(ip);
78216664Smckusick 	IUNLOCK(ip);
7839167Ssam }
7849167Ssam 
7859167Ssam /*
7869167Ssam  * Rename system call.
7879167Ssam  * 	rename("foo", "bar");
7889167Ssam  * is essentially
7899167Ssam  *	unlink("bar");
7909167Ssam  *	link("foo", "bar");
7919167Ssam  *	unlink("foo");
7929167Ssam  * but ``atomically''.  Can't do full commit without saving state in the
7939167Ssam  * inode on disk which isn't feasible at this time.  Best we can do is
7949167Ssam  * always guarantee the target exists.
7959167Ssam  *
7969167Ssam  * Basic algorithm is:
7979167Ssam  *
7989167Ssam  * 1) Bump link count on source while we're linking it to the
7999167Ssam  *    target.  This also insure the inode won't be deleted out
80016776Smckusick  *    from underneath us while we work (it may be truncated by
80116776Smckusick  *    a concurrent `trunc' or `open' for creation).
8029167Ssam  * 2) Link source to destination.  If destination already exists,
8039167Ssam  *    delete it first.
80416776Smckusick  * 3) Unlink source reference to inode if still around. If a
80516776Smckusick  *    directory was moved and the parent of the destination
8069167Ssam  *    is different from the source, patch the ".." entry in the
8079167Ssam  *    directory.
8089167Ssam  *
8099167Ssam  * Source and destination must either both be directories, or both
8109167Ssam  * not be directories.  If target is a directory, it must be empty.
8119167Ssam  */
8127701Ssam rename()
8137701Ssam {
8147701Ssam 	struct a {
8157701Ssam 		char	*from;
8167701Ssam 		char	*to;
81716694Smckusick 	} *uap = (struct a *)u.u_ap;
8189167Ssam 	register struct inode *ip, *xp, *dp;
81916776Smckusick 	struct dirtemplate dirbuf;
82016776Smckusick 	int doingdirectory = 0, oldparent = 0, newparent = 0;
82116694Smckusick 	register struct nameidata *ndp = &u.u_nd;
82210051Ssam 	int error = 0;
8237701Ssam 
82416694Smckusick 	ndp->ni_nameiop = DELETE | LOCKPARENT;
82516694Smckusick 	ndp->ni_segflg = UIO_USERSPACE;
82616694Smckusick 	ndp->ni_dirp = uap->from;
82716694Smckusick 	ip = namei(ndp);
8289167Ssam 	if (ip == NULL)
8299167Ssam 		return;
83016694Smckusick 	dp = ndp->ni_pdir;
8319167Ssam 	if ((ip->i_mode&IFMT) == IFDIR) {
8329167Ssam 		register struct direct *d;
8339167Ssam 
83416694Smckusick 		d = &ndp->ni_dent;
8359167Ssam 		/*
83611641Ssam 		 * Avoid ".", "..", and aliases of "." for obvious reasons.
8379167Ssam 		 */
83811641Ssam 		if ((d->d_namlen == 1 && d->d_name[0] == '.') ||
83911641Ssam 		    (d->d_namlen == 2 && bcmp(d->d_name, "..", 2) == 0) ||
84016776Smckusick 		    (dp == ip) || (ip->i_flag & IRENAME)) {
84111641Ssam 			iput(dp);
84211641Ssam 			if (dp == ip)
84311641Ssam 				irele(ip);
84411641Ssam 			else
84510051Ssam 				iput(ip);
84611641Ssam 			u.u_error = EINVAL;
84711641Ssam 			return;
8489167Ssam 		}
84916776Smckusick 		ip->i_flag |= IRENAME;
8509167Ssam 		oldparent = dp->i_number;
8519167Ssam 		doingdirectory++;
8529167Ssam 	}
85311641Ssam 	iput(dp);
8549167Ssam 
8559167Ssam 	/*
8569167Ssam 	 * 1) Bump link count while we're moving stuff
8579167Ssam 	 *    around.  If we crash somewhere before
8589167Ssam 	 *    completing our work, the link count
8599167Ssam 	 *    may be wrong, but correctable.
8609167Ssam 	 */
8619167Ssam 	ip->i_nlink++;
8629167Ssam 	ip->i_flag |= ICHG;
8639167Ssam 	iupdat(ip, &time, &time, 1);
86416664Smckusick 	IUNLOCK(ip);
8659167Ssam 
8669167Ssam 	/*
8679167Ssam 	 * When the target exists, both the directory
8689167Ssam 	 * and target inodes are returned locked.
8699167Ssam 	 */
87016694Smckusick 	ndp->ni_nameiop = CREATE | LOCKPARENT | NOCACHE;
87116694Smckusick 	ndp->ni_dirp = (caddr_t)uap->to;
87216694Smckusick 	xp = namei(ndp);
87310051Ssam 	if (u.u_error) {
87410051Ssam 		error = u.u_error;
8759167Ssam 		goto out;
87610051Ssam 	}
87716694Smckusick 	dp = ndp->ni_pdir;
8789167Ssam 	/*
87911641Ssam 	 * If ".." must be changed (ie the directory gets a new
88012816Smckusick 	 * parent) then the source directory must not be in the
88112816Smckusick 	 * directory heirarchy above the target, as this would
88212816Smckusick 	 * orphan everything below the source directory. Also
88312816Smckusick 	 * the user must have write permission in the source so
88412816Smckusick 	 * as to be able to change "..". We must repeat the call
88512816Smckusick 	 * to namei, as the parent directory is unlocked by the
88612816Smckusick 	 * call to checkpath().
88711641Ssam 	 */
88816776Smckusick 	if (oldparent != dp->i_number)
88916776Smckusick 		newparent = dp->i_number;
89016776Smckusick 	if (doingdirectory && newparent) {
89112816Smckusick 		if (access(ip, IWRITE))
89212816Smckusick 			goto bad;
89312816Smckusick 		do {
89416694Smckusick 			dp = ndp->ni_pdir;
89512816Smckusick 			if (xp != NULL)
89612816Smckusick 				iput(xp);
89712816Smckusick 			u.u_error = checkpath(ip, dp);
89812816Smckusick 			if (u.u_error)
89912816Smckusick 				goto out;
90016694Smckusick 			xp = namei(ndp);
90112816Smckusick 			if (u.u_error) {
90212816Smckusick 				error = u.u_error;
90312816Smckusick 				goto out;
90412816Smckusick 			}
90516694Smckusick 		} while (dp != ndp->ni_pdir);
90612816Smckusick 	}
90711641Ssam 	/*
9089167Ssam 	 * 2) If target doesn't exist, link the target
9099167Ssam 	 *    to the source and unlink the source.
9109167Ssam 	 *    Otherwise, rewrite the target directory
9119167Ssam 	 *    entry to reference the source inode and
9129167Ssam 	 *    expunge the original entry's existence.
9139167Ssam 	 */
9149167Ssam 	if (xp == NULL) {
9159167Ssam 		if (dp->i_dev != ip->i_dev) {
91610051Ssam 			error = EXDEV;
9179167Ssam 			goto bad;
9189167Ssam 		}
9199167Ssam 		/*
92016776Smckusick 		 * Account for ".." in new directory.
92116776Smckusick 		 * When source and destination have the same
92216776Smckusick 		 * parent we don't fool with the link count.
9239167Ssam 		 */
92416776Smckusick 		if (doingdirectory && newparent) {
9259167Ssam 			dp->i_nlink++;
9269167Ssam 			dp->i_flag |= ICHG;
9279167Ssam 			iupdat(dp, &time, &time, 1);
9289167Ssam 		}
92916694Smckusick 		error = direnter(ip, ndp);
93010850Ssam 		if (error)
9319167Ssam 			goto out;
9329167Ssam 	} else {
9339167Ssam 		if (xp->i_dev != dp->i_dev || xp->i_dev != ip->i_dev) {
93410051Ssam 			error = EXDEV;
9359167Ssam 			goto bad;
9369167Ssam 		}
9379167Ssam 		/*
93810590Ssam 		 * Short circuit rename(foo, foo).
93910590Ssam 		 */
94010590Ssam 		if (xp->i_number == ip->i_number)
94110590Ssam 			goto bad;
94210590Ssam 		/*
94324433Sbloom 		 * If the parent directory is "sticky", then the user must
94424433Sbloom 		 * own the parent directory, or the destination of the rename,
94524433Sbloom 		 * otherwise the destination may not be changed (except by
94624433Sbloom 		 * root). This implements append-only directories.
94724433Sbloom 		 */
94824433Sbloom 		if ((dp->i_mode & ISVTX) && u.u_uid != 0 &&
94924433Sbloom 		    u.u_uid != dp->i_uid && xp->i_uid != u.u_uid) {
95024433Sbloom 			error = EPERM;
95124433Sbloom 			goto bad;
95224433Sbloom 		}
95324433Sbloom 		/*
95410051Ssam 		 * Target must be empty if a directory
95510051Ssam 		 * and have no links to it.
9569167Ssam 		 * Also, insure source and target are
9579167Ssam 		 * compatible (both directories, or both
9589167Ssam 		 * not directories).
9599167Ssam 		 */
9609167Ssam 		if ((xp->i_mode&IFMT) == IFDIR) {
96116776Smckusick 			if (!dirempty(xp, dp->i_number) || xp->i_nlink > 2) {
96210051Ssam 				error = ENOTEMPTY;
9639167Ssam 				goto bad;
9649167Ssam 			}
9659167Ssam 			if (!doingdirectory) {
96610051Ssam 				error = ENOTDIR;
9679167Ssam 				goto bad;
9689167Ssam 			}
96916776Smckusick 			cacheinval(dp);
9709167Ssam 		} else if (doingdirectory) {
97110051Ssam 			error = EISDIR;
9729167Ssam 			goto bad;
9739167Ssam 		}
97416694Smckusick 		dirrewrite(dp, ip, ndp);
97510051Ssam 		if (u.u_error) {
97610051Ssam 			error = u.u_error;
9779167Ssam 			goto bad1;
97810051Ssam 		}
9799167Ssam 		/*
98010051Ssam 		 * Adjust the link count of the target to
98110051Ssam 		 * reflect the dirrewrite above.  If this is
98210051Ssam 		 * a directory it is empty and there are
98310051Ssam 		 * no links to it, so we can squash the inode and
98410051Ssam 		 * any space associated with it.  We disallowed
98510051Ssam 		 * renaming over top of a directory with links to
98616776Smckusick 		 * it above, as the remaining link would point to
98716776Smckusick 		 * a directory without "." or ".." entries.
9889167Ssam 		 */
98910051Ssam 		xp->i_nlink--;
9909167Ssam 		if (doingdirectory) {
99110051Ssam 			if (--xp->i_nlink != 0)
99210051Ssam 				panic("rename: linked directory");
9939167Ssam 			itrunc(xp, (u_long)0);
99410051Ssam 		}
9959167Ssam 		xp->i_flag |= ICHG;
9969167Ssam 		iput(xp);
99710246Ssam 		xp = NULL;
9989167Ssam 	}
9999167Ssam 
10009167Ssam 	/*
10019167Ssam 	 * 3) Unlink the source.
10029167Ssam 	 */
100316694Smckusick 	ndp->ni_nameiop = DELETE | LOCKPARENT;
100416694Smckusick 	ndp->ni_segflg = UIO_USERSPACE;
100516694Smckusick 	ndp->ni_dirp = uap->from;
100616776Smckusick 	xp = namei(ndp);
100717758Smckusick 	if (xp != NULL)
100817758Smckusick 		dp = ndp->ni_pdir;
100917758Smckusick 	else
101017758Smckusick 		dp = NULL;
10119167Ssam 	/*
101216776Smckusick 	 * Insure that the directory entry still exists and has not
101316776Smckusick 	 * changed while the new name has been entered. If the source is
101416776Smckusick 	 * a file then the entry may have been unlinked or renamed. In
101516776Smckusick 	 * either case there is no further work to be done. If the source
101616776Smckusick 	 * is a directory then it cannot have been rmdir'ed; its link
101716776Smckusick 	 * count of three would cause a rmdir to fail with ENOTEMPTY.
101816776Smckusick 	 * The IRENAME flag insures that it cannot be moved by another
101916776Smckusick 	 * rename.
10209167Ssam 	 */
102117758Smckusick 	if (xp != ip) {
102216776Smckusick 		if (doingdirectory)
102317758Smckusick 			panic("rename: lost dir entry");
102416776Smckusick 	} else {
10259167Ssam 		/*
102616776Smckusick 		 * If the source is a directory with a
102716776Smckusick 		 * new parent, the link count of the old
102816776Smckusick 		 * parent directory must be decremented
102916776Smckusick 		 * and ".." set to point to the new parent.
10309167Ssam 		 */
103116776Smckusick 		if (doingdirectory && newparent) {
10329167Ssam 			dp->i_nlink--;
10339167Ssam 			dp->i_flag |= ICHG;
103416776Smckusick 			error = rdwri(UIO_READ, xp, (caddr_t)&dirbuf,
103516776Smckusick 				sizeof (struct dirtemplate), (off_t)0, 1,
103616776Smckusick 				(int *)0);
103716776Smckusick 			if (error == 0) {
103816776Smckusick 				if (dirbuf.dotdot_namlen != 2 ||
103916776Smckusick 				    dirbuf.dotdot_name[0] != '.' ||
104016776Smckusick 				    dirbuf.dotdot_name[1] != '.') {
104116776Smckusick 					printf("rename: mangled dir\n");
104216776Smckusick 				} else {
104316776Smckusick 					dirbuf.dotdot_ino = newparent;
104416776Smckusick 					(void) rdwri(UIO_WRITE, xp,
104516776Smckusick 					    (caddr_t)&dirbuf,
104616776Smckusick 					    sizeof (struct dirtemplate),
104716776Smckusick 					    (off_t)0, 1, (int *)0);
104816776Smckusick 					cacheinval(dp);
104916776Smckusick 				}
105016776Smckusick 			}
10519167Ssam 		}
105216694Smckusick 		if (dirremove(ndp)) {
105316776Smckusick 			xp->i_nlink--;
105416776Smckusick 			xp->i_flag |= ICHG;
10559167Ssam 		}
105616776Smckusick 		xp->i_flag &= ~IRENAME;
105716776Smckusick 		if (error == 0)		/* XXX conservative */
105810051Ssam 			error = u.u_error;
10599167Ssam 	}
10609167Ssam 	if (dp)
10619167Ssam 		iput(dp);
106216776Smckusick 	if (xp)
106316776Smckusick 		iput(xp);
106416776Smckusick 	irele(ip);
106516776Smckusick 	if (error)
106616776Smckusick 		u.u_error = error;
106716776Smckusick 	return;
10689167Ssam 
10699167Ssam bad:
107010246Ssam 	iput(dp);
10719167Ssam bad1:
10729167Ssam 	if (xp)
107310246Ssam 		iput(xp);
10749167Ssam out:
10759167Ssam 	ip->i_nlink--;
10769167Ssam 	ip->i_flag |= ICHG;
10779167Ssam 	irele(ip);
107810051Ssam 	if (error)
107910051Ssam 		u.u_error = error;
10807701Ssam }
10817701Ssam 
10827535Sroot /*
10837535Sroot  * Make a new file.
10847535Sroot  */
10857535Sroot struct inode *
108616694Smckusick maknode(mode, ndp)
10877535Sroot 	int mode;
108816694Smckusick 	register struct nameidata *ndp;
10897535Sroot {
10907535Sroot 	register struct inode *ip;
109116694Smckusick 	register struct inode *pdir = ndp->ni_pdir;
10927535Sroot 	ino_t ipref;
10937535Sroot 
10947535Sroot 	if ((mode & IFMT) == IFDIR)
109516694Smckusick 		ipref = dirpref(pdir->i_fs);
10967535Sroot 	else
109716694Smckusick 		ipref = pdir->i_number;
109816694Smckusick 	ip = ialloc(pdir, ipref, mode);
10997535Sroot 	if (ip == NULL) {
110016694Smckusick 		iput(pdir);
11017701Ssam 		return (NULL);
11027535Sroot 	}
11037701Ssam #ifdef QUOTA
11047535Sroot 	if (ip->i_dquot != NODQUOT)
11057535Sroot 		panic("maknode: dquot");
11067535Sroot #endif
11077535Sroot 	ip->i_flag |= IACC|IUPD|ICHG;
11087535Sroot 	if ((mode & IFMT) == 0)
11097535Sroot 		mode |= IFREG;
11107535Sroot 	ip->i_mode = mode & ~u.u_cmask;
11117535Sroot 	ip->i_nlink = 1;
11127535Sroot 	ip->i_uid = u.u_uid;
111316694Smckusick 	ip->i_gid = pdir->i_gid;
111411811Ssam 	if (ip->i_mode & ISGID && !groupmember(ip->i_gid))
111511811Ssam 		ip->i_mode &= ~ISGID;
11167701Ssam #ifdef QUOTA
11177535Sroot 	ip->i_dquot = inoquota(ip);
11187535Sroot #endif
11197535Sroot 
11207535Sroot 	/*
11217535Sroot 	 * Make sure inode goes to disk before directory entry.
11227535Sroot 	 */
11238673Sroot 	iupdat(ip, &time, &time, 1);
112416694Smckusick 	u.u_error = direnter(ip, ndp);
11257535Sroot 	if (u.u_error) {
11267535Sroot 		/*
112710850Ssam 		 * Write error occurred trying to update directory
112810850Ssam 		 * so must deallocate the inode.
11297535Sroot 		 */
11307535Sroot 		ip->i_nlink = 0;
11317535Sroot 		ip->i_flag |= ICHG;
11327535Sroot 		iput(ip);
11337701Ssam 		return (NULL);
11347535Sroot 	}
11357701Ssam 	return (ip);
11367535Sroot }
113712756Ssam 
113812756Ssam /*
113912756Ssam  * A virgin directory (no blushing please).
114012756Ssam  */
114112756Ssam struct dirtemplate mastertemplate = {
114212756Ssam 	0, 12, 1, ".",
114312756Ssam 	0, DIRBLKSIZ - 12, 2, ".."
114412756Ssam };
114512756Ssam 
114612756Ssam /*
114712756Ssam  * Mkdir system call
114812756Ssam  */
114912756Ssam mkdir()
115012756Ssam {
115112756Ssam 	struct a {
115212756Ssam 		char	*name;
115312756Ssam 		int	dmode;
115416694Smckusick 	} *uap = (struct a *)u.u_ap;
115512756Ssam 	register struct inode *ip, *dp;
115612756Ssam 	struct dirtemplate dirtemplate;
115716694Smckusick 	register struct nameidata *ndp = &u.u_nd;
115812756Ssam 
115916694Smckusick 	ndp->ni_nameiop = CREATE;
116016694Smckusick 	ndp->ni_segflg = UIO_USERSPACE;
116116694Smckusick 	ndp->ni_dirp = uap->name;
116216694Smckusick 	ip = namei(ndp);
116312756Ssam 	if (u.u_error)
116412756Ssam 		return;
116512756Ssam 	if (ip != NULL) {
116612756Ssam 		iput(ip);
116712756Ssam 		u.u_error = EEXIST;
116812756Ssam 		return;
116912756Ssam 	}
117016694Smckusick 	dp = ndp->ni_pdir;
117112756Ssam 	uap->dmode &= 0777;
117212756Ssam 	uap->dmode |= IFDIR;
117312756Ssam 	/*
117412756Ssam 	 * Must simulate part of maknode here
117512756Ssam 	 * in order to acquire the inode, but
117612756Ssam 	 * not have it entered in the parent
117712756Ssam 	 * directory.  The entry is made later
117812756Ssam 	 * after writing "." and ".." entries out.
117912756Ssam 	 */
118012756Ssam 	ip = ialloc(dp, dirpref(dp->i_fs), uap->dmode);
118112756Ssam 	if (ip == NULL) {
118212756Ssam 		iput(dp);
118312756Ssam 		return;
118412756Ssam 	}
118512756Ssam #ifdef QUOTA
118612756Ssam 	if (ip->i_dquot != NODQUOT)
118712756Ssam 		panic("mkdir: dquot");
118812756Ssam #endif
118912756Ssam 	ip->i_flag |= IACC|IUPD|ICHG;
119012756Ssam 	ip->i_mode = uap->dmode & ~u.u_cmask;
119112756Ssam 	ip->i_nlink = 2;
119212756Ssam 	ip->i_uid = u.u_uid;
119312756Ssam 	ip->i_gid = dp->i_gid;
119412756Ssam #ifdef QUOTA
119512756Ssam 	ip->i_dquot = inoquota(ip);
119612756Ssam #endif
119712756Ssam 	iupdat(ip, &time, &time, 1);
119812756Ssam 
119912756Ssam 	/*
120012756Ssam 	 * Bump link count in parent directory
120112756Ssam 	 * to reflect work done below.  Should
120212756Ssam 	 * be done before reference is created
120312756Ssam 	 * so reparation is possible if we crash.
120412756Ssam 	 */
120512756Ssam 	dp->i_nlink++;
120612756Ssam 	dp->i_flag |= ICHG;
120712756Ssam 	iupdat(dp, &time, &time, 1);
120812756Ssam 
120912756Ssam 	/*
121012756Ssam 	 * Initialize directory with "."
121112756Ssam 	 * and ".." from static template.
121212756Ssam 	 */
121312756Ssam 	dirtemplate = mastertemplate;
121412756Ssam 	dirtemplate.dot_ino = ip->i_number;
121512756Ssam 	dirtemplate.dotdot_ino = dp->i_number;
121612756Ssam 	u.u_error = rdwri(UIO_WRITE, ip, (caddr_t)&dirtemplate,
121712756Ssam 		sizeof (dirtemplate), (off_t)0, 1, (int *)0);
121812756Ssam 	if (u.u_error) {
121912756Ssam 		dp->i_nlink--;
122012756Ssam 		dp->i_flag |= ICHG;
122112756Ssam 		goto bad;
122212756Ssam 	}
122318103Smckusick 	if (DIRBLKSIZ > ip->i_fs->fs_fsize)
122418103Smckusick 		panic("mkdir: blksize");     /* XXX - should grow with bmap() */
122518103Smckusick 	else
122618103Smckusick 		ip->i_size = DIRBLKSIZ;
122712756Ssam 	/*
122812756Ssam 	 * Directory all set up, now
122912756Ssam 	 * install the entry for it in
123012756Ssam 	 * the parent directory.
123112756Ssam 	 */
123216694Smckusick 	u.u_error = direnter(ip, ndp);
123312756Ssam 	dp = NULL;
123412756Ssam 	if (u.u_error) {
123516694Smckusick 		ndp->ni_nameiop = LOOKUP | NOCACHE;
123616694Smckusick 		ndp->ni_segflg = UIO_USERSPACE;
123716694Smckusick 		ndp->ni_dirp = uap->name;
123816694Smckusick 		dp = namei(ndp);
123912756Ssam 		if (dp) {
124012756Ssam 			dp->i_nlink--;
124112756Ssam 			dp->i_flag |= ICHG;
124212756Ssam 		}
124312756Ssam 	}
124412756Ssam bad:
124512756Ssam 	/*
124612756Ssam 	 * No need to do an explicit itrunc here,
124712756Ssam 	 * irele will do this for us because we set
124812756Ssam 	 * the link count to 0.
124912756Ssam 	 */
125012756Ssam 	if (u.u_error) {
125112756Ssam 		ip->i_nlink = 0;
125212756Ssam 		ip->i_flag |= ICHG;
125312756Ssam 	}
125412756Ssam 	if (dp)
125512756Ssam 		iput(dp);
125612756Ssam 	iput(ip);
125712756Ssam }
125812756Ssam 
125912756Ssam /*
126012756Ssam  * Rmdir system call.
126112756Ssam  */
126212756Ssam rmdir()
126312756Ssam {
126412756Ssam 	struct a {
126512756Ssam 		char	*name;
126616694Smckusick 	} *uap = (struct a *)u.u_ap;
126712756Ssam 	register struct inode *ip, *dp;
126816694Smckusick 	register struct nameidata *ndp = &u.u_nd;
126912756Ssam 
127016694Smckusick 	ndp->ni_nameiop = DELETE | LOCKPARENT;
127116694Smckusick 	ndp->ni_segflg = UIO_USERSPACE;
127216694Smckusick 	ndp->ni_dirp = uap->name;
127316694Smckusick 	ip = namei(ndp);
127412756Ssam 	if (ip == NULL)
127512756Ssam 		return;
127616694Smckusick 	dp = ndp->ni_pdir;
127712756Ssam 	/*
127812756Ssam 	 * No rmdir "." please.
127912756Ssam 	 */
128012756Ssam 	if (dp == ip) {
128112756Ssam 		irele(dp);
128212756Ssam 		iput(ip);
128312756Ssam 		u.u_error = EINVAL;
128412756Ssam 		return;
128512756Ssam 	}
128612756Ssam 	if ((ip->i_mode&IFMT) != IFDIR) {
128712756Ssam 		u.u_error = ENOTDIR;
128812756Ssam 		goto out;
128912756Ssam 	}
129012756Ssam 	/*
129112756Ssam 	 * Don't remove a mounted on directory.
129212756Ssam 	 */
129312756Ssam 	if (ip->i_dev != dp->i_dev) {
129412756Ssam 		u.u_error = EBUSY;
129512756Ssam 		goto out;
129612756Ssam 	}
129712756Ssam 	/*
129812756Ssam 	 * Verify the directory is empty (and valid).
129912756Ssam 	 * (Rmdir ".." won't be valid since
130012756Ssam 	 *  ".." will contain a reference to
130112756Ssam 	 *  the current directory and thus be
130212756Ssam 	 *  non-empty.)
130312756Ssam 	 */
130416776Smckusick 	if (ip->i_nlink != 2 || !dirempty(ip, dp->i_number)) {
130512756Ssam 		u.u_error = ENOTEMPTY;
130612756Ssam 		goto out;
130712756Ssam 	}
130812756Ssam 	/*
130912756Ssam 	 * Delete reference to directory before purging
131012756Ssam 	 * inode.  If we crash in between, the directory
131112756Ssam 	 * will be reattached to lost+found,
131212756Ssam 	 */
131316694Smckusick 	if (dirremove(ndp) == 0)
131412756Ssam 		goto out;
131512756Ssam 	dp->i_nlink--;
131612756Ssam 	dp->i_flag |= ICHG;
131716776Smckusick 	cacheinval(dp);
131812756Ssam 	iput(dp);
131912756Ssam 	dp = NULL;
132012756Ssam 	/*
132112756Ssam 	 * Truncate inode.  The only stuff left
132212756Ssam 	 * in the directory is "." and "..".  The
132312756Ssam 	 * "." reference is inconsequential since
132412756Ssam 	 * we're quashing it.  The ".." reference
132512756Ssam 	 * has already been adjusted above.  We've
132612756Ssam 	 * removed the "." reference and the reference
132712756Ssam 	 * in the parent directory, but there may be
132812756Ssam 	 * other hard links so decrement by 2 and
132912756Ssam 	 * worry about them later.
133012756Ssam 	 */
133112756Ssam 	ip->i_nlink -= 2;
133212756Ssam 	itrunc(ip, (u_long)0);
133316739Smckusick 	cacheinval(ip);
133412756Ssam out:
133512756Ssam 	if (dp)
133612756Ssam 		iput(dp);
133712756Ssam 	iput(ip);
133812756Ssam }
133912756Ssam 
134012756Ssam struct file *
134112756Ssam getinode(fdes)
134212756Ssam 	int fdes;
134312756Ssam {
134416540Ssam 	struct file *fp;
134512756Ssam 
134616540Ssam 	if ((unsigned)fdes >= NOFILE || (fp = u.u_ofile[fdes]) == NULL) {
134716540Ssam 		u.u_error = EBADF;
134816540Ssam 		return ((struct file *)0);
134916540Ssam 	}
135012756Ssam 	if (fp->f_type != DTYPE_INODE) {
135112756Ssam 		u.u_error = EINVAL;
135216540Ssam 		return ((struct file *)0);
135312756Ssam 	}
135412756Ssam 	return (fp);
135512756Ssam }
135612756Ssam 
135712756Ssam /*
135812756Ssam  * mode mask for creation of files
135912756Ssam  */
136012756Ssam umask()
136112756Ssam {
136212756Ssam 	register struct a {
136312756Ssam 		int	mask;
136416694Smckusick 	} *uap = (struct a *)u.u_ap;
136512756Ssam 
136612756Ssam 	u.u_r.r_val1 = u.u_cmask;
136712756Ssam 	u.u_cmask = uap->mask & 07777;
136812756Ssam }
1369