xref: /csrg-svn/sys/kern/vfs_syscalls.c (revision 9903)
1*9903Ssam /*	vfs_syscalls.c	4.45	82/12/24	*/
237Sbill 
337Sbill #include "../h/param.h"
437Sbill #include "../h/systm.h"
537Sbill #include "../h/dir.h"
637Sbill #include "../h/user.h"
78040Sroot #include "../h/kernel.h"
86254Sroot #include "../h/file.h"
96574Smckusic #include "../h/stat.h"
1037Sbill #include "../h/inode.h"
116574Smckusic #include "../h/fs.h"
126254Sroot #include "../h/buf.h"
136254Sroot #include "../h/proc.h"
147482Skre #include "../h/quota.h"
157505Sroot #include "../h/descrip.h"
167826Sroot #include "../h/uio.h"
177826Sroot #include "../h/socket.h"
188632Sroot #include "../h/socketvar.h"
199167Ssam #include "../h/nami.h"
2037Sbill 
219167Ssam /*
229167Ssam  * Change current working directory (``.'').
239167Ssam  */
246254Sroot chdir()
256254Sroot {
266254Sroot 
276254Sroot 	chdirec(&u.u_cdir);
286254Sroot }
296254Sroot 
309167Ssam /*
319167Ssam  * Change notion of root (``/'') directory.
329167Ssam  */
336254Sroot chroot()
346254Sroot {
356254Sroot 
366254Sroot 	if (suser())
376254Sroot 		chdirec(&u.u_rdir);
386254Sroot }
396254Sroot 
409167Ssam /*
419167Ssam  * Common routine for chroot and chdir.
429167Ssam  */
436254Sroot chdirec(ipp)
447701Ssam 	register struct inode **ipp;
456254Sroot {
466254Sroot 	register struct inode *ip;
476254Sroot 	struct a {
486254Sroot 		char	*fname;
496254Sroot 	};
506254Sroot 
519167Ssam 	ip = namei(uchar, LOOKUP, 1);
529167Ssam 	if (ip == NULL)
536254Sroot 		return;
549167Ssam 	if ((ip->i_mode&IFMT) != IFDIR) {
556254Sroot 		u.u_error = ENOTDIR;
566254Sroot 		goto bad;
576254Sroot 	}
589167Ssam 	if (access(ip, IEXEC))
596254Sroot 		goto bad;
607122Smckusick 	iunlock(ip);
617142Smckusick 	if (*ipp)
627142Smckusick 		irele(*ipp);
636254Sroot 	*ipp = ip;
646254Sroot 	return;
656254Sroot 
666254Sroot bad:
676254Sroot 	iput(ip);
686254Sroot }
696254Sroot 
7037Sbill /*
716254Sroot  * Open system call.
726254Sroot  */
736254Sroot open()
746254Sroot {
756254Sroot 	register struct inode *ip;
766254Sroot 	register struct a {
776254Sroot 		char	*fname;
787701Ssam 		int	flags;
797701Ssam 		int	mode;
806254Sroot 	} *uap;
819167Ssam 	int checkpermissions = 1, flags;
826254Sroot 
836254Sroot 	uap = (struct a *)u.u_ap;
849167Ssam 	flags = uap->flags + 1;
859167Ssam 	if ((flags&FTRUNCATE) && (flags&FWRITE) == 0) {
869167Ssam 		u.u_error = EINVAL;
879167Ssam 		return;
889167Ssam 	}
899167Ssam 	if (flags&FCREATE) {
909167Ssam 		ip = namei(uchar, CREATE, 1);
917701Ssam 		if (ip == NULL) {
927701Ssam 			if (u.u_error)
937701Ssam 				return;
947701Ssam 			ip = maknode(uap->mode&07777&(~ISVTX));
957701Ssam 			checkpermissions = 0;
969167Ssam 			flags &= ~FTRUNCATE;
977701Ssam 		}
987701Ssam 	} else
999167Ssam 		ip = namei(uchar, LOOKUP, 1);
1006254Sroot 	if (ip == NULL)
1016254Sroot 		return;
1029167Ssam 	open1(ip, flags, checkpermissions);
1036254Sroot }
1046254Sroot 
1057701Ssam #ifndef NOCOMPAT
1066254Sroot /*
1076254Sroot  * Creat system call.
1086254Sroot  */
1097505Sroot ocreat()
1106254Sroot {
1116254Sroot 	register struct inode *ip;
1126254Sroot 	register struct a {
1136254Sroot 		char	*fname;
1146254Sroot 		int	fmode;
1156254Sroot 	} *uap;
1166254Sroot 
1176254Sroot 	uap = (struct a *)u.u_ap;
1189167Ssam 	ip = namei(uchar, CREATE, 1);
1196254Sroot 	if (ip == NULL) {
1206254Sroot 		if (u.u_error)
1216254Sroot 			return;
1226254Sroot 		ip = maknode(uap->fmode&07777&(~ISVTX));
1237701Ssam 		if (ip == NULL)
1246254Sroot 			return;
1257701Ssam 		open1(ip, FWRITE, 0);
1266254Sroot 	} else
1279167Ssam 		open1(ip, FWRITE|FTRUNCATE, 1);
1286254Sroot }
1297701Ssam #endif
1306254Sroot 
1316254Sroot /*
1326254Sroot  * Common code for open and creat.
1337701Ssam  * Check permissions (if we haven't done so already),
1347701Ssam  * allocate an open file structure, and call
1357701Ssam  * the device open routine, if any.
1366254Sroot  */
1377701Ssam open1(ip, mode, checkpermissions)
1386254Sroot 	register struct inode *ip;
1396254Sroot 	register mode;
1406254Sroot {
1416254Sroot 	register struct file *fp;
1427701Ssam 	int i, flags;
1436254Sroot 
1447701Ssam 	if (checkpermissions) {
1456254Sroot 		if (mode&FREAD)
1467701Ssam 			if (access(ip, IREAD))
1477701Ssam 				goto bad;
1486254Sroot 		if (mode&FWRITE) {
1497701Ssam 			if (access(ip, IWRITE))
1507701Ssam 				goto bad;
1517701Ssam 			if ((ip->i_mode&IFMT) == IFDIR) {
1526254Sroot 				u.u_error = EISDIR;
1537701Ssam 				goto bad;
1547701Ssam 			}
1556254Sroot 		}
1566254Sroot 	}
1577701Ssam 
1587701Ssam 	/*
1597701Ssam 	 * Check locking on inode.  Release "inode lock"
1607701Ssam 	 * while doing so in case we block inside flocki.
1617701Ssam 	 */
1627701Ssam 	flags = 0;
1639167Ssam 	if (mode&(FSHLOCK|FEXLOCK)) {
1647701Ssam 		iunlock(ip);
1657701Ssam 		flags = flocki(ip, 0, mode);
1667701Ssam 		ilock(ip);
1677701Ssam 		if (u.u_error)
1687701Ssam 			goto bad;
1697142Smckusick 	}
1707701Ssam 	if (mode&FTRUNCATE)
1719167Ssam 		itrunc(ip, (u_long)0);
1727122Smckusick 	iunlock(ip);
1736254Sroot 	if ((fp = falloc()) == NULL)
1746254Sroot 		goto out;
1757701Ssam 	fp->f_flag = mode & FMODES;
1767505Sroot 	fp->f_type = DTYPE_FILE;
1776254Sroot 	i = u.u_r.r_val1;
1786254Sroot 	fp->f_inode = ip;
1798559Sroot 	u.u_error = openi(ip, mode);
1807701Ssam 	if (u.u_error == 0) {
1817701Ssam 		u.u_pofile[i] = flags;
1826254Sroot 		return;
1837701Ssam 	}
1846254Sroot 	u.u_ofile[i] = NULL;
1856254Sroot 	fp->f_count--;
1866254Sroot out:
1877142Smckusick 	irele(ip);
1887701Ssam 	return;
1897701Ssam bad:
1907701Ssam 	iput(ip);
1916254Sroot }
1926254Sroot 
1936254Sroot /*
1946254Sroot  * Mknod system call
1956254Sroot  */
1966254Sroot mknod()
1976254Sroot {
1986254Sroot 	register struct inode *ip;
1996254Sroot 	register struct a {
2006254Sroot 		char	*fname;
2016254Sroot 		int	fmode;
2026254Sroot 		int	dev;
2036254Sroot 	} *uap;
2046254Sroot 
2056254Sroot 	uap = (struct a *)u.u_ap;
2066254Sroot 	if (suser()) {
2079167Ssam 		ip = namei(uchar, CREATE, 0);
2086254Sroot 		if (ip != NULL) {
2096254Sroot 			u.u_error = EEXIST;
2106254Sroot 			goto out;
2116254Sroot 		}
2126254Sroot 	}
2136254Sroot 	if (u.u_error)
2146254Sroot 		return;
2156254Sroot 	ip = maknode(uap->fmode);
2166254Sroot 	if (ip == NULL)
2176254Sroot 		return;
2186254Sroot 	if (uap->dev) {
2196254Sroot 		/*
2206254Sroot 		 * Want to be able to use this to make badblock
2216254Sroot 		 * inodes, so don't truncate the dev number.
2226254Sroot 		 */
2236574Smckusic 		ip->i_rdev = uap->dev;
2246254Sroot 		ip->i_flag |= IACC|IUPD|ICHG;
2256254Sroot 	}
2266254Sroot 
2276254Sroot out:
2286254Sroot 	iput(ip);
2296254Sroot }
2306254Sroot 
2316254Sroot /*
2326254Sroot  * link system call
2336254Sroot  */
2346254Sroot link()
2356254Sroot {
2366254Sroot 	register struct inode *ip, *xp;
2376254Sroot 	register struct a {
2386254Sroot 		char	*target;
2396254Sroot 		char	*linkname;
2406254Sroot 	} *uap;
2416254Sroot 
2426254Sroot 	uap = (struct a *)u.u_ap;
2439167Ssam 	ip = namei(uchar, LOOKUP, 1); /* well, this routine is doomed anyhow */
2446254Sroot 	if (ip == NULL)
2456254Sroot 		return;
2469167Ssam 	if ((ip->i_mode&IFMT) == IFDIR && !suser()) {
2477439Sroot 		iput(ip);
2487439Sroot 		return;
2497439Sroot 	}
2506254Sroot 	ip->i_nlink++;
2516254Sroot 	ip->i_flag |= ICHG;
2528673Sroot 	iupdat(ip, &time, &time, 1);
2537122Smckusick 	iunlock(ip);
2546254Sroot 	u.u_dirp = (caddr_t)uap->linkname;
2559167Ssam 	xp = namei(uchar, CREATE, 0);
2566254Sroot 	if (xp != NULL) {
2576254Sroot 		u.u_error = EEXIST;
2586254Sroot 		iput(xp);
2596254Sroot 		goto out;
2606254Sroot 	}
2616254Sroot 	if (u.u_error)
2626254Sroot 		goto out;
2636254Sroot 	if (u.u_pdir->i_dev != ip->i_dev) {
2646254Sroot 		iput(u.u_pdir);
2656254Sroot 		u.u_error = EXDEV;
2666254Sroot 		goto out;
2676254Sroot 	}
2687535Sroot 	direnter(ip);
2696254Sroot out:
2706254Sroot 	if (u.u_error) {
2716254Sroot 		ip->i_nlink--;
2726254Sroot 		ip->i_flag |= ICHG;
2736254Sroot 	}
2747142Smckusick 	irele(ip);
2756254Sroot }
2766254Sroot 
2776254Sroot /*
2786254Sroot  * symlink -- make a symbolic link
2796254Sroot  */
2806254Sroot symlink()
2816254Sroot {
2826254Sroot 	register struct a {
2836254Sroot 		char	*target;
2846254Sroot 		char	*linkname;
2856254Sroot 	} *uap;
2866254Sroot 	register struct inode *ip;
2876254Sroot 	register char *tp;
2886254Sroot 	register c, nc;
2896254Sroot 
2906254Sroot 	uap = (struct a *)u.u_ap;
2916254Sroot 	tp = uap->target;
2926254Sroot 	nc = 0;
2936254Sroot 	while (c = fubyte(tp)) {
2946254Sroot 		if (c < 0) {
2956254Sroot 			u.u_error = EFAULT;
2966254Sroot 			return;
2976254Sroot 		}
2986254Sroot 		tp++;
2996254Sroot 		nc++;
3006254Sroot 	}
3016254Sroot 	u.u_dirp = uap->linkname;
3029167Ssam 	ip = namei(uchar, CREATE, 0);
3036254Sroot 	if (ip) {
3046254Sroot 		iput(ip);
3056254Sroot 		u.u_error = EEXIST;
3066254Sroot 		return;
3076254Sroot 	}
3086254Sroot 	if (u.u_error)
3096254Sroot 		return;
3106254Sroot 	ip = maknode(IFLNK | 0777);
3116254Sroot 	if (ip == NULL)
3126254Sroot 		return;
3137826Sroot 	u.u_error = rdwri(UIO_WRITE, ip, uap->target, nc, 0, 0, (int *)0);
3149167Ssam 	/* handle u.u_error != 0 */
3156254Sroot 	iput(ip);
3166254Sroot }
3176254Sroot 
3186254Sroot /*
3196254Sroot  * Unlink system call.
3206254Sroot  * Hard to avoid races here, especially
3216254Sroot  * in unlinking directories.
3226254Sroot  */
3236254Sroot unlink()
3246254Sroot {
3256254Sroot 	struct a {
3266254Sroot 		char	*fname;
3276254Sroot 	};
3289167Ssam 	register struct inode *ip, *dp;
3296254Sroot 
3309167Ssam 	ip = namei(uchar, DELETE | LOCKPARENT, 0);
3319167Ssam 	if (ip == NULL)
3326254Sroot 		return;
3339167Ssam 	dp = u.u_pdir;
3349167Ssam 	if ((ip->i_mode&IFMT) == IFDIR && !suser())
3356254Sroot 		goto out;
3366254Sroot 	/*
3376254Sroot 	 * Don't unlink a mounted file.
3386254Sroot 	 */
3399167Ssam 	if (ip->i_dev != dp->i_dev) {
3406254Sroot 		u.u_error = EBUSY;
3416254Sroot 		goto out;
3426254Sroot 	}
3436254Sroot 	if (ip->i_flag&ITEXT)
3446254Sroot 		xrele(ip);	/* try once to free text */
3457535Sroot 	if (dirremove()) {
3467535Sroot 		ip->i_nlink--;
3477535Sroot 		ip->i_flag |= ICHG;
3486254Sroot 	}
3496254Sroot out:
3509167Ssam 	if (dp == ip)
3517142Smckusick 		irele(ip);
3527142Smckusick 	else
3537142Smckusick 		iput(ip);
3549167Ssam 	iput(dp);
3556254Sroot }
3566254Sroot 
3576254Sroot /*
3586254Sroot  * Seek system call
3596254Sroot  */
3608040Sroot lseek()
3616254Sroot {
3626254Sroot 	register struct file *fp;
3636254Sroot 	register struct a {
3647701Ssam 		int	fd;
3656254Sroot 		off_t	off;
3666254Sroot 		int	sbase;
3676254Sroot 	} *uap;
3686254Sroot 
3696254Sroot 	uap = (struct a *)u.u_ap;
3707701Ssam 	fp = getf(uap->fd);
3716254Sroot 	if (fp == NULL)
3726254Sroot 		return;
3737505Sroot 	if (fp->f_type == DTYPE_SOCKET) {
3746254Sroot 		u.u_error = ESPIPE;
3756254Sroot 		return;
3766254Sroot 	}
3777701Ssam 	if (uap->sbase == FSEEK_RELATIVE)
3786254Sroot 		uap->off += fp->f_offset;
3797701Ssam 	else if (uap->sbase == FSEEK_EOF)
3806254Sroot 		uap->off += fp->f_inode->i_size;
3816254Sroot 	fp->f_offset = uap->off;
3826254Sroot 	u.u_r.r_off = uap->off;
3836254Sroot }
3846254Sroot 
3856254Sroot /*
3866254Sroot  * Access system call
3876254Sroot  */
3886254Sroot saccess()
3896254Sroot {
3906254Sroot 	register svuid, svgid;
3916254Sroot 	register struct inode *ip;
3926254Sroot 	register struct a {
3936254Sroot 		char	*fname;
3946254Sroot 		int	fmode;
3956254Sroot 	} *uap;
3966254Sroot 
3976254Sroot 	uap = (struct a *)u.u_ap;
3986254Sroot 	svuid = u.u_uid;
3996254Sroot 	svgid = u.u_gid;
4006254Sroot 	u.u_uid = u.u_ruid;
4016254Sroot 	u.u_gid = u.u_rgid;
4029167Ssam 	ip = namei(uchar, LOOKUP, 1);
4036254Sroot 	if (ip != NULL) {
4049167Ssam 		if ((uap->fmode&FACCESS_READ) && access(ip, IREAD))
4057701Ssam 			goto done;
4069167Ssam 		if ((uap->fmode&FACCESS_WRITE) && access(ip, IWRITE))
4077701Ssam 			goto done;
4089167Ssam 		if ((uap->fmode&FACCESS_EXECUTE) && access(ip, IEXEC))
4097701Ssam 			goto done;
4107701Ssam done:
4116254Sroot 		iput(ip);
4126254Sroot 	}
4136254Sroot 	u.u_uid = svuid;
4146254Sroot 	u.u_gid = svgid;
4156254Sroot }
4166254Sroot 
4176254Sroot /*
41837Sbill  * the fstat system call.
41937Sbill  */
42037Sbill fstat()
42137Sbill {
42237Sbill 	register struct file *fp;
42337Sbill 	register struct a {
4247701Ssam 		int	fd;
42537Sbill 		struct stat *sb;
42637Sbill 	} *uap;
42737Sbill 
42837Sbill 	uap = (struct a *)u.u_ap;
4297701Ssam 	fp = getf(uap->fd);
4304828Swnj 	if (fp == NULL)
43137Sbill 		return;
4327505Sroot 	if (fp->f_type == DTYPE_SOCKET)
4334891Swnj 		u.u_error = sostat(fp->f_socket, uap->sb);
4344828Swnj 	else
4354828Swnj 		stat1(fp->f_inode, uap->sb);
43637Sbill }
43737Sbill 
43837Sbill /*
4396574Smckusic  * Stat system call.  This version follows links.
44037Sbill  */
44137Sbill stat()
44237Sbill {
44337Sbill 	register struct inode *ip;
44437Sbill 	register struct a {
44537Sbill 		char	*fname;
44637Sbill 		struct stat *sb;
44737Sbill 	} *uap;
44837Sbill 
44937Sbill 	uap = (struct a *)u.u_ap;
4509167Ssam 	ip = namei(uchar, LOOKUP, 1);
4514828Swnj 	if (ip == NULL)
45237Sbill 		return;
4533624Sroot 	stat1(ip, uap->sb);
45437Sbill 	iput(ip);
45537Sbill }
45637Sbill 
45737Sbill /*
4586574Smckusic  * Lstat system call.  This version does not follow links.
4595992Swnj  */
4605992Swnj lstat()
4615992Swnj {
4625992Swnj 	register struct inode *ip;
4635992Swnj 	register struct a {
4645992Swnj 		char	*fname;
4655992Swnj 		struct stat *sb;
4665992Swnj 	} *uap;
4675992Swnj 
4685992Swnj 	uap = (struct a *)u.u_ap;
4699167Ssam 	ip = namei(uchar, LOOKUP, 0);
4705992Swnj 	if (ip == NULL)
4715992Swnj 		return;
4726153Ssam 	stat1(ip, uap->sb);
4735992Swnj 	iput(ip);
4745992Swnj }
4755992Swnj 
4765992Swnj /*
47737Sbill  * The basic routine for fstat and stat:
47837Sbill  * get the inode and pass appropriate parts back.
47937Sbill  */
4803624Sroot stat1(ip, ub)
4814828Swnj 	register struct inode *ip;
4824828Swnj 	struct stat *ub;
48337Sbill {
48437Sbill 	struct stat ds;
48537Sbill 
4868673Sroot 	IUPDAT(ip, &time, &time, 0);
48737Sbill 	/*
4887023Smckusick 	 * Copy from inode table
48937Sbill 	 */
49037Sbill 	ds.st_dev = ip->i_dev;
49137Sbill 	ds.st_ino = ip->i_number;
49237Sbill 	ds.st_mode = ip->i_mode;
49337Sbill 	ds.st_nlink = ip->i_nlink;
49437Sbill 	ds.st_uid = ip->i_uid;
49537Sbill 	ds.st_gid = ip->i_gid;
4966574Smckusic 	ds.st_rdev = (dev_t)ip->i_rdev;
4973624Sroot 	ds.st_size = ip->i_size;
4986574Smckusic 	ds.st_atime = ip->i_atime;
499*9903Ssam 	ds.st_spare1 = 0;
5006574Smckusic 	ds.st_mtime = ip->i_mtime;
501*9903Ssam 	ds.st_spare2 = 0;
5026574Smckusic 	ds.st_ctime = ip->i_ctime;
503*9903Ssam 	ds.st_spare3 = 0;
5047701Ssam 	/* this doesn't belong here */
5057701Ssam 	if ((ip->i_mode&IFMT) == IFBLK)
5067701Ssam 		ds.st_blksize = BLKDEV_IOSIZE;
5077701Ssam 	else if ((ip->i_mode&IFMT) == IFCHR)
5087701Ssam 		ds.st_blksize = MAXBSIZE;
5097701Ssam 	else
5107701Ssam 		ds.st_blksize = ip->i_fs->fs_bsize;
511*9903Ssam 	ds.st_spare4[0] = ds.st_spare4[1] = ds.st_spare4[2] = 0;
51237Sbill 	if (copyout((caddr_t)&ds, (caddr_t)ub, sizeof(ds)) < 0)
51337Sbill 		u.u_error = EFAULT;
51437Sbill }
51537Sbill 
51637Sbill /*
5175992Swnj  * Return target name of a symbolic link
51837Sbill  */
5195992Swnj readlink()
5205992Swnj {
5215992Swnj 	register struct inode *ip;
5225992Swnj 	register struct a {
5235992Swnj 		char	*name;
5245992Swnj 		char	*buf;
5255992Swnj 		int	count;
5267826Sroot 	} *uap = (struct a *)u.u_ap;
5277826Sroot 	int resid;
5285992Swnj 
5299167Ssam 	ip = namei(uchar, LOOKUP, 0);
5305992Swnj 	if (ip == NULL)
5315992Swnj 		return;
5325992Swnj 	if ((ip->i_mode&IFMT) != IFLNK) {
5335992Swnj 		u.u_error = ENXIO;
5345992Swnj 		goto out;
5355992Swnj 	}
5367826Sroot 	u.u_error = rdwri(UIO_READ, ip, uap->buf, uap->count, 0, 0, &resid);
5375992Swnj out:
5385992Swnj 	iput(ip);
5397826Sroot 	u.u_r.r_val1 = uap->count - resid;
5405992Swnj }
5415992Swnj 
5429167Ssam /*
5439167Ssam  * Change mode of a file given path name.
5449167Ssam  */
5456254Sroot chmod()
5465992Swnj {
5477701Ssam 	struct inode *ip;
5487701Ssam 	struct a {
5496254Sroot 		char	*fname;
5506254Sroot 		int	fmode;
5515992Swnj 	} *uap;
5525992Swnj 
5535992Swnj 	uap = (struct a *)u.u_ap;
5546254Sroot 	if ((ip = owner(1)) == NULL)
5555992Swnj 		return;
5567701Ssam 	chmod1(ip, uap->fmode);
5579167Ssam 	iput(ip);
5587701Ssam }
5597439Sroot 
5609167Ssam /*
5619167Ssam  * Change mode of a file given a file descriptor.
5629167Ssam  */
5637701Ssam fchmod()
5647701Ssam {
5657701Ssam 	struct a {
5667701Ssam 		int	fd;
5677701Ssam 		int	fmode;
5687701Ssam 	} *uap;
5697701Ssam 	register struct inode *ip;
5707701Ssam 	register struct file *fp;
5717701Ssam 
5727701Ssam 	uap = (struct a *)u.u_ap;
5737701Ssam 	fp = getf(uap->fd);
5747701Ssam 	if (fp == NULL)
5757701Ssam 		return;
5767701Ssam 	if (fp->f_type == DTYPE_SOCKET) {
5777701Ssam 		u.u_error = EINVAL;
5787701Ssam 		return;
5797439Sroot 	}
5807701Ssam 	ip = fp->f_inode;
5819167Ssam 	if (u.u_uid != ip->i_uid && !suser())
5829167Ssam 		return;
5837701Ssam 	ilock(ip);
5847701Ssam 	chmod1(ip, uap->fmode);
5859167Ssam 	iunlock(ip);
5867701Ssam }
5877701Ssam 
5889167Ssam /*
5899167Ssam  * Change the mode on a file.
5909167Ssam  * Inode must be locked before calling.
5919167Ssam  */
5927701Ssam chmod1(ip, mode)
5937701Ssam 	register struct inode *ip;
5947701Ssam 	register int mode;
5957701Ssam {
5967868Sroot 	register int *gp;
5977868Sroot 
5986254Sroot 	ip->i_mode &= ~07777;
5997439Sroot 	if (u.u_uid) {
6007701Ssam 		mode &= ~ISVTX;
6017868Sroot 		for (gp = u.u_groups; gp < &u.u_groups[NGROUPS]; gp++)
6027868Sroot 			if (*gp == ip->i_gid)
6037868Sroot 				goto ok;
6047868Sroot 		mode &= ~ISGID;
6057868Sroot ok:
6067868Sroot 		;
6077701Ssam #ifdef MUSH
6087482Skre 		if (u.u_quota->q_syflags & QF_UMASK && u.u_uid != 0 &&
6097482Skre 		    (ip->i_mode & IFMT) != IFCHR)
6107701Ssam 			mode &= ~u.u_cmask;
6117482Skre #endif
6127439Sroot 	}
6137701Ssam 	ip->i_mode |= mode&07777;
6146254Sroot 	ip->i_flag |= ICHG;
6156254Sroot 	if (ip->i_flag&ITEXT && (ip->i_mode&ISVTX)==0)
6166254Sroot 		xrele(ip);
6175992Swnj }
6185992Swnj 
6199167Ssam /*
6209167Ssam  * Set ownership given a path name.
6219167Ssam  */
6226254Sroot chown()
62337Sbill {
6247701Ssam 	struct inode *ip;
6257701Ssam 	struct a {
6266254Sroot 		char	*fname;
6276254Sroot 		int	uid;
6286254Sroot 		int	gid;
62937Sbill 	} *uap;
63037Sbill 
63137Sbill 	uap = (struct a *)u.u_ap;
6326254Sroot 	if (!suser() || (ip = owner(0)) == NULL)
63337Sbill 		return;
6347701Ssam 	chown1(ip, uap->uid, uap->gid);
6359167Ssam 	iput(ip);
6367701Ssam }
6377439Sroot 
6389167Ssam /*
6399167Ssam  * Set ownership given a file descriptor.
6409167Ssam  */
6417701Ssam fchown()
6427701Ssam {
6437701Ssam 	struct a {
6447701Ssam 		int	fd;
6457701Ssam 		int	uid;
6467701Ssam 		int	gid;
6477701Ssam 	} *uap;
6487701Ssam 	register struct inode *ip;
6497701Ssam 	register struct file *fp;
6507701Ssam 
6517701Ssam 	uap = (struct a *)u.u_ap;
6527701Ssam 	fp = getf(uap->fd);
6537701Ssam 	if (fp == NULL)
6547701Ssam 		return;
6557701Ssam 	if (fp->f_type == DTYPE_SOCKET) {
6567701Ssam 		u.u_error = EINVAL;
6577701Ssam 		return;
6587439Sroot 	}
6597701Ssam 	ip = fp->f_inode;
6609167Ssam 	if (!suser())
6619167Ssam 		return;
6627701Ssam 	ilock(ip);
6637701Ssam 	chown1(ip, uap->uid, uap->gid);
6649167Ssam 	iunlock(ip);
6657701Ssam }
6667701Ssam 
6677701Ssam /*
6687701Ssam  * Perform chown operation on inode ip;
6697701Ssam  * inode must be locked prior to call.
6707701Ssam  */
6717701Ssam chown1(ip, uid, gid)
6727701Ssam 	register struct inode *ip;
6737701Ssam 	int uid, gid;
6747701Ssam {
6757701Ssam #ifdef QUOTA
6767701Ssam 	register long change;
6777701Ssam 
6787439Sroot 	/*
6797482Skre 	 * This doesn't allow for holes in files (which hopefully don't
6807482Skre 	 * happen often in files that we chown), and is not accurate anyway
6817482Skre 	 * (eg: it totally ignores 3 level indir blk files - but hopefully
6827482Skre 	 * noone who can make a file that big will have a quota)
6837482Skre 	 */
6847701Ssam 	if (ip->i_uid == uid)
6857482Skre 		change = 0;
6867482Skre 	else {
6877482Skre 		register struct fs *fs = ip->i_fs;
6887482Skre 
6897482Skre 		if (ip->i_size > (change = NDADDR * fs->fs_bsize)) {
6907482Skre 			register off_t size;
6917482Skre 
6927482Skre 			size = blkroundup(fs, ip->i_size) - change;
6937482Skre 			change += size;
6947482Skre 			change += fs->fs_bsize;
6957701Ssam 			/* this assumes NIADDR <= 2 */
6967482Skre 			if (size > NINDIR(fs) * fs->fs_bsize)
6977482Skre 				change += fs->fs_bsize;
6987482Skre 		} else
6997482Skre 			change = fragroundup(fs, ip->i_size);
7007482Skre 		change /= DEV_BSIZE;
7017482Skre 	}
7029167Ssam 	(void)chkdq(ip, -change, 1);
7039167Ssam 	(void)chkiq(ip->i_dev, ip, ip->i_uid, 1);
7047482Skre 	dqrele(ip->i_dquot);
7057482Skre #endif
7067482Skre 	/*
7077701Ssam 	 * keep uid/gid's in sane range -- no err,
7087701Ssam 	 * so chown(file, uid, -1) will do something useful
7097439Sroot 	 */
7107701Ssam 	if (uid >= 0 && uid <= 32767)	/* should have a constant */
7117701Ssam 		ip->i_uid = uid;
7127701Ssam 	if (gid >= 0 && gid <= 32767)	/* same here */
7137701Ssam 		ip->i_gid = gid;
7146254Sroot 	ip->i_flag |= ICHG;
7156254Sroot 	if (u.u_ruid != 0)
7166254Sroot 		ip->i_mode &= ~(ISUID|ISGID);
7177701Ssam #ifdef QUOTA
7187482Skre 	ip->i_dquot = inoquota(ip);
7199167Ssam 	(void)chkdq(ip, change, 1);
7209167Ssam 	(void)chkiq(ip->i_dev, (struct inode *)NULL, uid, 1);
7217482Skre #endif
72237Sbill }
72337Sbill 
72437Sbill /*
7256254Sroot  * Set IUPD and IACC times on file.
7266254Sroot  * Can't set ICHG.
72737Sbill  */
7288107Sroot outime()
7294828Swnj {
73037Sbill 	register struct a {
7316254Sroot 		char	*fname;
7326254Sroot 		time_t	*tptr;
73337Sbill 	} *uap;
7346254Sroot 	register struct inode *ip;
7356254Sroot 	time_t tv[2];
7368632Sroot 	struct timeval tv0, tv1;
73737Sbill 
73837Sbill 	uap = (struct a *)u.u_ap;
7396254Sroot 	if ((ip = owner(1)) == NULL)
74037Sbill 		return;
7416254Sroot 	if (copyin((caddr_t)uap->tptr, (caddr_t)tv, sizeof(tv))) {
7426254Sroot 		u.u_error = EFAULT;
7436254Sroot 	} else {
7446254Sroot 		ip->i_flag |= IACC|IUPD|ICHG;
7458632Sroot 		tv0.tv_sec = tv[0]; tv0.tv_usec = 0;
7468632Sroot 		tv1.tv_sec = tv[1]; tv1.tv_usec = 0;
7478632Sroot 		iupdat(ip, &tv0, &tv1, 0);
74837Sbill 	}
74937Sbill 	iput(ip);
75037Sbill }
75137Sbill 
7529167Ssam /*
7539167Ssam  * Flush any pending I/O.
7549167Ssam  */
7556254Sroot sync()
75637Sbill {
75737Sbill 
7588673Sroot 	update();
75937Sbill }
7607535Sroot 
7619167Ssam /*
7629167Ssam  * Apply an advisory lock on a file descriptor.
7639167Ssam  */
7647701Ssam flock()
7657701Ssam {
7667701Ssam 	struct a {
7677701Ssam 		int	fd;
7687701Ssam 		int	how;
7697701Ssam 	} *uap;
7707701Ssam 	register struct file *fp;
7717701Ssam 	register int cmd, flags;
7727701Ssam 
7737701Ssam 	uap = (struct a *)u.u_ap;
7747701Ssam 	fp = getf(uap->fd);
7757701Ssam 	if (fp == NULL)
7767701Ssam 		return;
7777701Ssam 	if (fp->f_type == DTYPE_SOCKET) {		/* XXX */
7787701Ssam 		u.u_error = EINVAL;
7797701Ssam 		return;
7807701Ssam 	}
7817701Ssam 	cmd = uap->how;
7829593Ssam 	flags = u.u_pofile[uap->fd] & (UF_SHLOCK|UF_EXLOCK);
7837701Ssam 	if (cmd&FUNLOCK) {
7847701Ssam 		if (flags == 0) {
7857701Ssam 			u.u_error = EINVAL;
7867701Ssam 			return;
7877701Ssam 		}
7887701Ssam 		funlocki(fp->f_inode, flags);
7899593Ssam 		u.u_pofile[uap->fd] &= ~(UF_SHLOCK|UF_EXLOCK);
7907701Ssam 		return;
7917701Ssam 	}
7927701Ssam 	/*
7937701Ssam 	 * No reason to write lock a file we've already
7947701Ssam 	 * write locked, similarly with a read lock.
7957701Ssam 	 */
7969593Ssam 	if ((flags&UF_EXLOCK) && (cmd&FEXLOCK) ||
7979593Ssam 	    (flags&UF_SHLOCK) && (cmd&FSHLOCK))
7987701Ssam 		return;
7997701Ssam 	u.u_pofile[uap->fd] = flocki(fp->f_inode, u.u_pofile[uap->fd], cmd);
8007701Ssam }
8017701Ssam 
8029167Ssam /*
8039167Ssam  * Truncate a file given its path name.
8049167Ssam  */
8057701Ssam truncate()
8067701Ssam {
8077701Ssam 	struct a {
8087701Ssam 		char	*fname;
8099167Ssam 		u_long	length;
8107826Sroot 	} *uap = (struct a *)u.u_ap;
8117701Ssam 	struct inode *ip;
8127701Ssam 
8139167Ssam 	ip = namei(uchar, LOOKUP, 1);
8147701Ssam 	if (ip == NULL)
8157701Ssam 		return;
8167701Ssam 	if (access(ip, IWRITE))
8177701Ssam 		goto bad;
8187701Ssam 	if ((ip->i_mode&IFMT) == IFDIR) {
8197701Ssam 		u.u_error = EISDIR;
8207701Ssam 		goto bad;
8217701Ssam 	}
8227701Ssam 	itrunc(ip, uap->length);
8237701Ssam bad:
8247701Ssam 	iput(ip);
8257701Ssam }
8267701Ssam 
8279167Ssam /*
8289167Ssam  * Truncate a file given a file descriptor.
8299167Ssam  */
8307701Ssam ftruncate()
8317701Ssam {
8327701Ssam 	struct a {
8337701Ssam 		int	fd;
8349167Ssam 		u_long	length;
8357826Sroot 	} *uap = (struct a *)u.u_ap;
8367701Ssam 	struct inode *ip;
8377701Ssam 	struct file *fp;
8387701Ssam 
8397701Ssam 	fp = getf(uap->fd);
8407701Ssam 	if (fp == NULL)
8417701Ssam 		return;
8427701Ssam 	if (fp->f_type == DTYPE_SOCKET) {
8437701Ssam 		u.u_error = EINVAL;
8447701Ssam 		return;
8457701Ssam 	}
8467701Ssam 	if ((fp->f_flag&FWRITE) == 0) {
8477701Ssam 		u.u_error = EINVAL;
8487701Ssam 		return;
8497701Ssam 	}
8507701Ssam 	ip = fp->f_inode;
8517701Ssam 	ilock(ip);
8527701Ssam 	itrunc(ip, uap->length);
8539167Ssam 	iunlock(ip);
8547701Ssam }
8557701Ssam 
8569167Ssam /*
8579167Ssam  * Synch an open file.
8589167Ssam  */
8599167Ssam fsync()
8609167Ssam {
8619167Ssam 	struct a {
8629167Ssam 		int	fd;
8639167Ssam 	} *uap = (struct a *)u.u_ap;
8649167Ssam 	struct inode *ip;
8659167Ssam 	struct file *fp;
8669167Ssam 
8679167Ssam 	fp = getf(uap->fd);
8689167Ssam 	if (fp == NULL)
8699167Ssam 		return;
8709167Ssam 	if (fp->f_type == DTYPE_SOCKET) {
8719167Ssam 		u.u_error = EINVAL;
8729167Ssam 		return;
8739167Ssam 	}
8749167Ssam 	ip = fp->f_inode;
8759167Ssam 	ilock(ip);
8769167Ssam 	syncip(ip);
8779167Ssam 	iunlock(ip);
8789167Ssam }
8799167Ssam 
8809167Ssam /*
8819167Ssam  * Rename system call.
8829167Ssam  * 	rename("foo", "bar");
8839167Ssam  * is essentially
8849167Ssam  *	unlink("bar");
8859167Ssam  *	link("foo", "bar");
8869167Ssam  *	unlink("foo");
8879167Ssam  * but ``atomically''.  Can't do full commit without saving state in the
8889167Ssam  * inode on disk which isn't feasible at this time.  Best we can do is
8899167Ssam  * always guarantee the target exists.
8909167Ssam  *
8919167Ssam  * Basic algorithm is:
8929167Ssam  *
8939167Ssam  * 1) Bump link count on source while we're linking it to the
8949167Ssam  *    target.  This also insure the inode won't be deleted out
8959167Ssam  *    from underneath us while we work.
8969167Ssam  * 2) Link source to destination.  If destination already exists,
8979167Ssam  *    delete it first.
8989167Ssam  * 3) Unlink source reference to inode if still around.
8999167Ssam  * 4) If a directory was moved and the parent of the destination
9009167Ssam  *    is different from the source, patch the ".." entry in the
9019167Ssam  *    directory.
9029167Ssam  *
9039167Ssam  * Source and destination must either both be directories, or both
9049167Ssam  * not be directories.  If target is a directory, it must be empty.
9059167Ssam  */
9067701Ssam rename()
9077701Ssam {
9087701Ssam 	struct a {
9097701Ssam 		char	*from;
9107701Ssam 		char	*to;
9117701Ssam 	} *uap;
9129167Ssam 	register struct inode *ip, *xp, *dp;
9139167Ssam 	int oldparent, parentdifferent, doingdirectory;
9147701Ssam 
9159167Ssam 	uap = (struct a *)u.u_ap;
9169167Ssam 	ip = namei(uchar, LOOKUP | LOCKPARENT, 0);
9179167Ssam 	if (ip == NULL)
9189167Ssam 		return;
9199167Ssam 	dp = u.u_pdir;
9209167Ssam 	oldparent = 0, doingdirectory = 0;
9219167Ssam 	if ((ip->i_mode&IFMT) == IFDIR) {
9229167Ssam 		register struct direct *d;
9239167Ssam 
9249167Ssam 		d = &u.u_dent;
9259167Ssam 		/*
9269167Ssam 		 * Avoid "." and ".." for obvious reasons.
9279167Ssam 		 */
9289167Ssam 		if (d->d_name[0] == '.') {
9299167Ssam 			if (d->d_namlen == 1 ||
9309167Ssam 			    (d->d_namlen == 2 && d->d_name[1] == '.')) {
9319167Ssam 				u.u_error = EINVAL;
9329167Ssam 				iput(ip);
9339167Ssam 				return;
9349167Ssam 			}
9359167Ssam 		}
9369167Ssam 		oldparent = dp->i_number;
9379167Ssam 		doingdirectory++;
9389167Ssam 	}
9399167Ssam 	irele(dp);
9409167Ssam 
9419167Ssam 	/*
9429167Ssam 	 * 1) Bump link count while we're moving stuff
9439167Ssam 	 *    around.  If we crash somewhere before
9449167Ssam 	 *    completing our work, the link count
9459167Ssam 	 *    may be wrong, but correctable.
9469167Ssam 	 */
9479167Ssam 	ip->i_nlink++;
9489167Ssam 	ip->i_flag |= ICHG;
9499167Ssam 	iupdat(ip, &time, &time, 1);
9509167Ssam 	iunlock(ip);
9519167Ssam 
9529167Ssam 	/*
9539167Ssam 	 * When the target exists, both the directory
9549167Ssam 	 * and target inodes are returned locked.
9559167Ssam 	 */
9569167Ssam 	u.u_dirp = (caddr_t)uap->to;
9579167Ssam 	xp = namei(uchar, CREATE | LOCKPARENT, 0);
9589167Ssam 	if (u.u_error)
9599167Ssam 		goto out;
9609167Ssam 	dp = u.u_pdir;
9619167Ssam 	/*
9629167Ssam 	 * 2) If target doesn't exist, link the target
9639167Ssam 	 *    to the source and unlink the source.
9649167Ssam 	 *    Otherwise, rewrite the target directory
9659167Ssam 	 *    entry to reference the source inode and
9669167Ssam 	 *    expunge the original entry's existence.
9679167Ssam 	 */
9689167Ssam 	parentdifferent = oldparent != dp->i_number;
9699167Ssam 	if (xp == NULL) {
9709167Ssam 		if (dp->i_dev != ip->i_dev) {
9719167Ssam 			u.u_error = EXDEV;
9729167Ssam 			goto bad;
9739167Ssam 		}
9749167Ssam 		/*
9759167Ssam 		 * Account for ".." in directory.
9769167Ssam 		 * When source and destination have the
9779167Ssam 		 * same parent we don't fool with the
9789167Ssam 		 * link count -- this isn't required
9799167Ssam 		 * because we do a similar check below.
9809167Ssam 		 */
9819167Ssam 		if (doingdirectory && parentdifferent) {
9829167Ssam 			dp->i_nlink++;
9839167Ssam 			dp->i_flag |= ICHG;
9849167Ssam 			iupdat(dp, &time, &time, 1);
9859167Ssam 		}
9869167Ssam 		direnter(ip);
9879167Ssam 		if (u.u_error)
9889167Ssam 			goto out;
9899167Ssam 	} else {
9909167Ssam 		if (xp->i_dev != dp->i_dev || xp->i_dev != ip->i_dev) {
9919167Ssam 			u.u_error = EXDEV;
9929167Ssam 			goto bad;
9939167Ssam 		}
9949167Ssam 		/*
9959167Ssam 		 * Target must be empty if a directory.
9969167Ssam 		 * Also, insure source and target are
9979167Ssam 		 * compatible (both directories, or both
9989167Ssam 		 * not directories).
9999167Ssam 		 */
10009167Ssam 		if ((xp->i_mode&IFMT) == IFDIR) {
10019167Ssam 			if (!dirempty(xp)) {
10029847Ssam 				u.u_error = ENOTEMPTY;
10039167Ssam 				goto bad;
10049167Ssam 			}
10059167Ssam 			if (!doingdirectory) {
10069167Ssam 				u.u_error = ENOTDIR;
10079167Ssam 				goto bad;
10089167Ssam 			}
10099167Ssam 		} else if (doingdirectory) {
10109167Ssam 			u.u_error = EISDIR;
10119167Ssam 			goto bad;
10129167Ssam 		}
10139167Ssam 		dirrewrite(dp, ip);
10149167Ssam 		if (u.u_error)
10159167Ssam 			goto bad1;
10169167Ssam 		/*
10179167Ssam 		 * If this is a directory we know it is
10189167Ssam 		 * empty and we can squash the inode and
10199167Ssam 		 * any space associated with it.  Otherwise,
10209167Ssam 		 * we've got a plain file and the link count
10219167Ssam 		 * simply needs to be adjusted.
10229167Ssam 		 */
10239167Ssam 		if (doingdirectory) {
10249167Ssam 			xp->i_nlink = 0;
10259167Ssam 			itrunc(xp, (u_long)0);
10269167Ssam 		} else
10279167Ssam 			xp->i_nlink--;
10289167Ssam 		xp->i_flag |= ICHG;
10299167Ssam 		iput(xp);
10309167Ssam 	}
10319167Ssam 
10329167Ssam 	/*
10339167Ssam 	 * 3) Unlink the source.
10349167Ssam 	 */
10359167Ssam 	u.u_dirp = uap->from;
10369167Ssam 	dp = namei(uchar, DELETE, 0);
10379167Ssam 	/*
10389167Ssam 	 * Insure directory entry still exists and
10399167Ssam 	 * has not changed since the start of all
10409167Ssam 	 * this.  If either has occured, forget about
10419167Ssam 	 * about deleting the original entry and just
10429167Ssam 	 * adjust the link count in the inode.
10439167Ssam 	 */
10449167Ssam 	if (dp == NULL || u.u_dent.d_ino != ip->i_number) {
10459167Ssam 		ip->i_nlink--;
10469167Ssam 		ip->i_flag |= ICHG;
10479167Ssam 	} else {
10489167Ssam 		/*
10499167Ssam 		 * If source is a directory, must adjust
10509167Ssam 		 * link count of parent directory also.
10519167Ssam 		 * If target didn't exist and source and
10529167Ssam 		 * target have the same parent, then we
10539167Ssam 		 * needn't touch the link count, it all
10549167Ssam 		 * balances out in the end.  Otherwise, we
10559167Ssam 		 * must do so to reflect deletion of ".."
10569167Ssam 		 * done above.
10579167Ssam 		 */
10589167Ssam 		if (doingdirectory && (xp != NULL || parentdifferent)) {
10599167Ssam 			dp->i_nlink--;
10609167Ssam 			dp->i_flag |= ICHG;
10619167Ssam 		}
10629167Ssam 		if (dirremove()) {
10639167Ssam 			ip->i_nlink--;
10649167Ssam 			ip->i_flag |= ICHG;
10659167Ssam 		}
10669167Ssam 	}
10679167Ssam 	irele(ip);
10689167Ssam 	if (dp)
10699167Ssam 		iput(dp);
10709167Ssam 
10719167Ssam 	/*
10729167Ssam 	 * 4) Renaming a directory with the parent
10739167Ssam 	 *    different requires ".." to be rewritten.
10749167Ssam 	 *    The window is still there for ".." to
10759167Ssam 	 *    be inconsistent, but this is unavoidable,
10769167Ssam 	 *    and a lot shorter than when it was done
10779167Ssam 	 *    in a user process.
10789167Ssam 	 */
10799167Ssam 	if (doingdirectory && parentdifferent && u.u_error == 0) {
10809167Ssam 		struct dirtemplate dirbuf;
10819167Ssam 
10829167Ssam 		u.u_dirp = uap->to;
10839167Ssam 		ip = namei(uchar, LOOKUP | LOCKPARENT, 0);
10849167Ssam 		if (ip == NULL) {
10859167Ssam 			printf("rename: .. went away\n");
10869167Ssam 			return;
10879167Ssam 		}
10889167Ssam 		dp = u.u_pdir;
10899167Ssam 		if ((ip->i_mode&IFMT) != IFDIR) {
10909167Ssam 			printf("rename: .. not a directory\n");
10919167Ssam 			goto stuck;
10929167Ssam 		}
10939167Ssam 		u.u_error = rdwri(UIO_READ, ip, (caddr_t)&dirbuf,
10949167Ssam 			sizeof (struct dirtemplate), (off_t)0, 1, (int *)0);
10959167Ssam 		if (u.u_error == 0) {
10969167Ssam 			dirbuf.dotdot_ino = dp->i_number;
10979167Ssam 			(void) rdwri(UIO_WRITE, ip, (caddr_t)&dirbuf,
10989167Ssam 			  sizeof (struct dirtemplate), (off_t)0, 1, (int *)0);
10999167Ssam 		}
11009167Ssam stuck:
11019167Ssam 		irele(dp);
11029167Ssam 		iput(ip);
11039167Ssam 	}
11049167Ssam 	return;
11059167Ssam bad:
11069167Ssam 	iput(u.u_pdir);
11079167Ssam bad1:
11089167Ssam 	if (xp)
11099167Ssam 		irele(xp);
11109167Ssam out:
11119167Ssam 	ip->i_nlink--;
11129167Ssam 	ip->i_flag |= ICHG;
11139167Ssam 	irele(ip);
11147701Ssam }
11157701Ssam 
11167535Sroot /*
11177535Sroot  * Make a new file.
11187535Sroot  */
11197535Sroot struct inode *
11207535Sroot maknode(mode)
11217535Sroot 	int mode;
11227535Sroot {
11237535Sroot 	register struct inode *ip;
11247535Sroot 	ino_t ipref;
11257535Sroot 
11267535Sroot 	if ((mode & IFMT) == IFDIR)
11277535Sroot 		ipref = dirpref(u.u_pdir->i_fs);
11287535Sroot 	else
11297535Sroot 		ipref = u.u_pdir->i_number;
11307535Sroot 	ip = ialloc(u.u_pdir, ipref, mode);
11317535Sroot 	if (ip == NULL) {
11327535Sroot 		iput(u.u_pdir);
11337701Ssam 		return (NULL);
11347535Sroot 	}
11357701Ssam #ifdef QUOTA
11367535Sroot 	if (ip->i_dquot != NODQUOT)
11377535Sroot 		panic("maknode: dquot");
11387535Sroot #endif
11397535Sroot 	ip->i_flag |= IACC|IUPD|ICHG;
11407535Sroot 	if ((mode & IFMT) == 0)
11417535Sroot 		mode |= IFREG;
11427535Sroot 	ip->i_mode = mode & ~u.u_cmask;
11437535Sroot 	ip->i_nlink = 1;
11447535Sroot 	ip->i_uid = u.u_uid;
11457535Sroot 	ip->i_gid = u.u_pdir->i_gid;
11467701Ssam #ifdef QUOTA
11477535Sroot 	ip->i_dquot = inoquota(ip);
11487535Sroot #endif
11497535Sroot 
11507535Sroot 	/*
11517535Sroot 	 * Make sure inode goes to disk before directory entry.
11527535Sroot 	 */
11538673Sroot 	iupdat(ip, &time, &time, 1);
11547535Sroot 	direnter(ip);
11557535Sroot 	if (u.u_error) {
11567535Sroot 		/*
11577535Sroot 		 * write error occurred trying to update directory
11587535Sroot 		 * so must deallocate the inode
11597535Sroot 		 */
11607535Sroot 		ip->i_nlink = 0;
11617535Sroot 		ip->i_flag |= ICHG;
11627535Sroot 		iput(ip);
11637701Ssam 		return (NULL);
11647535Sroot 	}
11657701Ssam 	return (ip);
11667535Sroot }
1167