xref: /csrg-svn/sys/kern/sys_generic.c (revision 7532)
1*7532Sroot /*	sys_generic.c	5.4	82/07/25	*/
27423Sroot 
37423Sroot #include "../h/param.h"
47423Sroot #include "../h/systm.h"
57423Sroot #include "../h/dir.h"
67423Sroot #include "../h/user.h"
77423Sroot #include "../h/tty.h"
87500Sroot #include "../h/fcntl.h"
97423Sroot #include "../h/file.h"
107423Sroot #include "../h/inode.h"
117423Sroot #include "../h/buf.h"
127423Sroot #include "../h/proc.h"
137423Sroot #include "../h/inline.h"
147423Sroot #include "../h/conf.h"
157423Sroot #include "../h/socket.h"
167423Sroot #include "../h/socketvar.h"
177423Sroot #include "../h/cmap.h"
187423Sroot #include "../h/vlimit.h"
197423Sroot #include "../h/fs.h"
207487Skre #ifdef MUSH
217487Skre #include "../h/quota.h"
227487Skre #include "../h/share.h"
237487Skre #else
247487Skre #define	CHARGE(nothing)
257487Skre #endif
267500Sroot #include "../h/descrip.h"
277423Sroot 
287423Sroot /*
297423Sroot  * Read system call.
307423Sroot  */
317423Sroot read()
327423Sroot {
337423Sroot 	register struct file *fp;
347423Sroot 	register struct inode *ip;
357423Sroot 	register struct a {
367423Sroot 		int	fdes;
377423Sroot 		char	*cbuf;
387423Sroot 		unsigned count;
397423Sroot 	} *uap;
407423Sroot 
417423Sroot 	uap = (struct a *)u.u_ap;
427423Sroot 	if ((int)uap->count < 0) {
437423Sroot 		u.u_error = EINVAL;
447423Sroot 		return;
457423Sroot 	}
467423Sroot 	GETF(fp, uap->fdes);
477423Sroot 	if ((fp->f_flag&FREAD) == 0) {
487423Sroot 		u.u_error = EBADF;
497423Sroot 		return;
507423Sroot 	}
517423Sroot 	u.u_base = (caddr_t)uap->cbuf;
527423Sroot 	u.u_count = uap->count;
537423Sroot 	u.u_segflg = 0;
547423Sroot 	if ((u.u_procp->p_flag&SNUSIG) && setjmp(u.u_qsav)) {
557423Sroot 		if (u.u_count == uap->count)
567423Sroot 			u.u_eosys = RESTARTSYS;
577500Sroot 	} else if (fp->f_type == DTYPE_SOCKET)
587423Sroot 		u.u_error = soreceive(fp->f_socket, (struct sockaddr *)0);
597423Sroot 	else {
607423Sroot 		ip = fp->f_inode;
617423Sroot 		u.u_offset = fp->f_offset;
627423Sroot 		if ((ip->i_mode&IFMT) == IFREG) {
637423Sroot 			ilock(ip);
647423Sroot 			readi(ip);
657423Sroot 			iunlock(ip);
667423Sroot 		} else
677423Sroot 			readi(ip);
687423Sroot 		fp->f_offset += uap->count - u.u_count;
697423Sroot 	}
707423Sroot 	u.u_r.r_val1 = uap->count - u.u_count;
717423Sroot }
727423Sroot 
737423Sroot /*
747423Sroot  * Write system call
757423Sroot  */
767423Sroot write()
777423Sroot {
787423Sroot 	register struct file *fp;
797423Sroot 	register struct inode *ip;
807423Sroot 	register struct a {
817423Sroot 		int	fdes;
827423Sroot 		char	*cbuf;
837423Sroot 		unsigned count;
847423Sroot 	} *uap;
857423Sroot 
867423Sroot 	uap = (struct a *)u.u_ap;
877423Sroot 	if ((int)uap->count < 0) {
887423Sroot 		u.u_error = EINVAL;
897423Sroot 		return;
907423Sroot 	}
917423Sroot 	GETF(fp, uap->fdes);
927423Sroot 	if ((fp->f_flag&FWRITE) == 0) {
937423Sroot 		u.u_error = EBADF;
947423Sroot 		return;
957423Sroot 	}
967423Sroot 	u.u_base = (caddr_t)uap->cbuf;
977423Sroot 	u.u_count = uap->count;
987423Sroot 	u.u_segflg = 0;
997423Sroot 	if ((u.u_procp->p_flag&SNUSIG) && setjmp(u.u_qsav)) {
1007423Sroot 		if (u.u_count == uap->count)
1017423Sroot 			u.u_eosys = RESTARTSYS;
1027500Sroot 	} else if (fp->f_type == DTYPE_SOCKET)
1037423Sroot 		u.u_error = sosend(fp->f_socket, (struct sockaddr *)0);
1047423Sroot 	else {
1057423Sroot 		ip = fp->f_inode;
1067500Sroot 		if (fp->f_flag & O_APPEND)
1077500Sroot 			fp->f_offset = ip->i_size;
1087423Sroot 		u.u_offset = fp->f_offset;
1097423Sroot 		if ((ip->i_mode&IFMT) == IFREG) {
1107423Sroot 			ilock(ip);
1117423Sroot 			writei(ip);
1127423Sroot 			iunlock(ip);
1137423Sroot 		} else
1147423Sroot 			writei(ip);
1157423Sroot 		fp->f_offset += uap->count - u.u_count;
1167423Sroot 	}
1177423Sroot 	u.u_r.r_val1 = uap->count - u.u_count;
1187423Sroot }
1197423Sroot 
1207500Sroot readv()
1217500Sroot {
1227423Sroot 
1237500Sroot }
1247500Sroot 
1257500Sroot writev()
1267500Sroot {
1277500Sroot 
1287500Sroot }
1297500Sroot 
1307423Sroot /*
1317423Sroot  * Ioctl system call
1327423Sroot  * Check legality, execute common code, and switch out to individual
1337423Sroot  * device routine.
1347423Sroot  */
1357423Sroot ioctl()
1367423Sroot {
1377423Sroot 	register struct file *fp;
1387423Sroot 	register struct inode *ip;
1397423Sroot 	register struct a {
1407423Sroot 		int	fdes;
1417423Sroot 		int	cmd;
1427423Sroot 		caddr_t	cmarg;
1437423Sroot 	} *uap;
1447423Sroot 	register dev_t dev;
1457423Sroot 	register fmt;
1467423Sroot 
1477423Sroot 	uap = (struct a *)u.u_ap;
1487423Sroot 	if ((fp = getf(uap->fdes)) == NULL)
1497423Sroot 		return;
1507423Sroot 	if ((fp->f_flag & (FREAD|FWRITE)) == 0) {
1517423Sroot 		u.u_error = EBADF;
1527423Sroot 		return;
1537423Sroot 	}
1547423Sroot 	if (uap->cmd==FIOCLEX) {
1557423Sroot 		u.u_pofile[uap->fdes] |= EXCLOSE;
1567423Sroot 		return;
1577423Sroot 	}
1587423Sroot 	if (uap->cmd==FIONCLEX) {
1597423Sroot 		u.u_pofile[uap->fdes] &= ~EXCLOSE;
1607423Sroot 		return;
1617423Sroot 	}
1627500Sroot 	if (fp->f_type == DTYPE_SOCKET) {
1637423Sroot 		soioctl(fp->f_socket, uap->cmd, uap->cmarg);
1647423Sroot 		return;
1657423Sroot 	}
1667423Sroot 	ip = fp->f_inode;
1677423Sroot 	fmt = ip->i_mode & IFMT;
1687423Sroot 	if (fmt != IFCHR) {
1697423Sroot 		if (uap->cmd==FIONREAD && (fmt == IFREG || fmt == IFDIR)) {
1707423Sroot 			off_t nread = ip->i_size - fp->f_offset;
1717423Sroot 
1727423Sroot 			if (copyout((caddr_t)&nread, uap->cmarg, sizeof(off_t)))
1737423Sroot 				u.u_error = EFAULT;
1747423Sroot 		} else if (uap->cmd == FIONBIO || uap->cmd == FIOASYNC)
1757423Sroot 			return;
1767423Sroot 		else
1777423Sroot 			u.u_error = ENOTTY;
1787423Sroot 		return;
1797423Sroot 	}
1807423Sroot 	dev = ip->i_rdev;
1817423Sroot 	u.u_r.r_val1 = 0;
1827423Sroot 	if ((u.u_procp->p_flag&SNUSIG) && setjmp(u.u_qsav)) {
1837423Sroot 		u.u_eosys = RESTARTSYS;
1847423Sroot 		return;
1857423Sroot 	}
1867423Sroot 	(*cdevsw[major(dev)].d_ioctl)(dev, uap->cmd, uap->cmarg, 0);
1877423Sroot }
1887423Sroot 
1897423Sroot /*
1907423Sroot  * Do nothing specific version of line
1917423Sroot  * discipline specific ioctl command.
1927423Sroot  */
1937423Sroot /*ARGSUSED*/
1947423Sroot nullioctl(tp, cmd, addr)
1957423Sroot 	struct tty *tp;
1967423Sroot 	caddr_t addr;
1977423Sroot {
1987423Sroot 
1997423Sroot 	return (cmd);
2007423Sroot }
2017423Sroot 
2027423Sroot /*
2037423Sroot  * Read the file corresponding to
2047423Sroot  * the inode pointed at by the argument.
2057423Sroot  * The actual read arguments are found
2067423Sroot  * in the variables:
2077423Sroot  *	u_base		core address for destination
2087423Sroot  *	u_offset	byte offset in file
2097423Sroot  *	u_count		number of bytes to read
2107423Sroot  *	u_segflg	read to kernel/user/user I
2117423Sroot  */
2127423Sroot readi(ip)
2137423Sroot 	register struct inode *ip;
2147423Sroot {
2157423Sroot 	struct buf *bp;
2167423Sroot 	struct fs *fs;
2177423Sroot 	dev_t dev;
2187423Sroot 	daddr_t lbn, bn;
2197423Sroot 	off_t diff;
2207423Sroot 	register int on, type;
2217423Sroot 	register unsigned n;
2227423Sroot 	int size;
2237423Sroot 	long bsize;
2247423Sroot 	extern int mem_no;
2257423Sroot 
2267423Sroot 	if (u.u_count == 0)
2277423Sroot 		return;
2287423Sroot 	dev = (dev_t)ip->i_rdev;
2297423Sroot 	if (u.u_offset < 0 && ((ip->i_mode&IFMT) != IFCHR ||
2307423Sroot 	    mem_no != major(dev))) {
2317423Sroot 		u.u_error = EINVAL;
2327423Sroot 		return;
2337423Sroot 	}
2347423Sroot 	ip->i_flag |= IACC;
2357423Sroot 	type = ip->i_mode&IFMT;
2367423Sroot 	if (type == IFCHR) {
2377487Skre 		register c = u.u_count;
2387423Sroot 		(*cdevsw[major(dev)].d_read)(dev);
2397487Skre 		CHARGE(sc_tio * (c - u.u_count));
2407423Sroot 		return;
2417423Sroot 	}
2427423Sroot 	if (type != IFBLK) {
2437423Sroot 		dev = ip->i_dev;
2447423Sroot 		fs = ip->i_fs;
2457423Sroot 		bsize = fs->fs_bsize;
2467423Sroot 	} else
2477423Sroot 		bsize = BLKDEV_IOSIZE;
2487423Sroot 	do {
2497423Sroot 		lbn = u.u_offset / bsize;
2507423Sroot 		on = u.u_offset % bsize;
2517423Sroot 		n = MIN((unsigned)(bsize - on), u.u_count);
2527423Sroot 		if (type != IFBLK) {
2537423Sroot 			diff = ip->i_size - u.u_offset;
2547423Sroot 			if (diff <= 0)
2557423Sroot 				return;
2567423Sroot 			if (diff < n)
2577423Sroot 				n = diff;
2587423Sroot 			bn = fsbtodb(fs, bmap(ip, lbn, B_READ));
2597423Sroot 			if (u.u_error)
2607423Sroot 				return;
2617423Sroot 			size = blksize(fs, ip, lbn);
2627423Sroot 		} else {
2637423Sroot 			size = bsize;
2647423Sroot 			bn = lbn * (BLKDEV_IOSIZE/DEV_BSIZE);
2657423Sroot 			rablock = bn + (BLKDEV_IOSIZE/DEV_BSIZE);
2667423Sroot 			rasize = bsize;
2677423Sroot 		}
2687423Sroot 		if ((long)bn<0) {
2697423Sroot 			bp = geteblk(size);
2707423Sroot 			clrbuf(bp);
2717423Sroot 		} else if (ip->i_lastr + 1 == lbn)
2727423Sroot 			bp = breada(dev, bn, size, rablock, rasize);
2737423Sroot 		else
2747423Sroot 			bp = bread(dev, bn, size);
2757423Sroot 		ip->i_lastr = lbn;
2767423Sroot 		n = MIN(n, size - bp->b_resid);
2777423Sroot 		if (n != 0) {
2787423Sroot #ifdef UNFAST
2797423Sroot 			iomove(bp->b_un.b_addr + on, n, B_READ);
2807423Sroot #else
2817423Sroot 			if (u.u_segflg != 1) {
2827423Sroot 				if (copyout(bp->b_un.b_addr+on, u.u_base, n)) {
2837423Sroot 					u.u_error = EFAULT;
2847423Sroot 					goto bad;
2857423Sroot 				}
2867423Sroot 			} else
2877423Sroot 				bcopy(bp->b_un.b_addr + on, u.u_base, n);
2887423Sroot 			u.u_base += n;
2897423Sroot 			u.u_offset += n;
2907423Sroot 			u.u_count -= n;
2917423Sroot bad:
2927423Sroot 			;
2937423Sroot #endif
2947423Sroot 		}
2957423Sroot 		if (n + on == bsize || u.u_offset == ip->i_size)
2967423Sroot 			bp->b_flags |= B_AGE;
2977423Sroot 		brelse(bp);
2987423Sroot 	} while (u.u_error == 0 && u.u_count != 0 && n != 0);
2997423Sroot }
3007423Sroot 
3017423Sroot /*
3027423Sroot  * Write the file corresponding to
3037423Sroot  * the inode pointed at by the argument.
3047423Sroot  * The actual write arguments are found
3057423Sroot  * in the variables:
3067423Sroot  *	u_base		core address for source
3077423Sroot  *	u_offset	byte offset in file
3087423Sroot  *	u_count		number of bytes to write
3097423Sroot  *	u_segflg	write to kernel/user/user I
3107423Sroot  */
3117423Sroot writei(ip)
3127423Sroot 	register struct inode *ip;
3137423Sroot {
3147423Sroot 	struct buf *bp;
3157423Sroot 	register struct fs *fs;
3167423Sroot 	dev_t dev;
3177423Sroot 	daddr_t lbn, bn;
3187423Sroot 	register int on, type;
3197423Sroot 	register unsigned n;
3207423Sroot 	long bsize;
3217423Sroot 	int size, i, count;
3227423Sroot 	extern int mem_no;
3237423Sroot 
3247423Sroot 	dev = (dev_t)ip->i_rdev;
3257423Sroot 	if (u.u_offset < 0 && ((ip->i_mode&IFMT) != IFCHR ||
3267423Sroot 	    mem_no != major(dev)) ) {
3277423Sroot 		u.u_error = EINVAL;
3287423Sroot 		return;
3297423Sroot 	}
3307423Sroot 	type = ip->i_mode & IFMT;
3317423Sroot 	if (type == IFCHR) {
3327423Sroot 		ip->i_flag |= IUPD|ICHG;
3337487Skre 		CHARGE(sc_tio * u.u_count);
3347423Sroot 		(*cdevsw[major(dev)].d_write)(dev);
3357423Sroot 		return;
3367423Sroot 	}
3377423Sroot 	if (u.u_count == 0)
3387423Sroot 		return;
3397423Sroot 	if ((ip->i_mode & IFMT) == IFREG &&
3407423Sroot 	    u.u_offset + u.u_count > u.u_limit[LIM_FSIZE]) {
3417423Sroot 		psignal(u.u_procp, SIGXFSZ);
3427423Sroot 		u.u_error = EMFILE;
3437423Sroot 		return;
3447423Sroot 	}
3457423Sroot 	if (type!=IFBLK) {
3467423Sroot 		dev = ip->i_dev;
3477423Sroot 		fs = ip->i_fs;
3487423Sroot 		bsize = fs->fs_bsize;
349*7532Sroot 	} else
3507423Sroot 		bsize = BLKDEV_IOSIZE;
3517423Sroot 	do {
3527423Sroot 		lbn = u.u_offset / bsize;
3537423Sroot 		on = u.u_offset % bsize;
3547423Sroot 		n = MIN((unsigned)(bsize - on), u.u_count);
3557423Sroot 		if (type != IFBLK) {
3567423Sroot 			bn = fsbtodb(fs, bmap(ip, lbn, B_WRITE, (int)(on + n)));
357*7532Sroot 			if (u.u_error || (long)bn<0)
3587423Sroot 				return;
3597423Sroot 			if(u.u_offset + n > ip->i_size &&
3607423Sroot 			   (type == IFDIR || type == IFREG || type == IFLNK))
3617423Sroot 				ip->i_size = u.u_offset + n;
3627423Sroot 			size = blksize(fs, ip, lbn);
3637423Sroot 		} else {
3647423Sroot 			size = bsize;
3657423Sroot 			bn = lbn * (BLKDEV_IOSIZE/DEV_BSIZE);
3667423Sroot 		}
367*7532Sroot 		count = howmany(size, DEV_BSIZE);
368*7532Sroot 		for (i = 0; i < count; i += CLSIZE)
369*7532Sroot 			if (mfind(dev, bn + i))
370*7532Sroot 				munhash(dev, bn + i);
371*7532Sroot 		if (n == bsize)
3727423Sroot 			bp = getblk(dev, bn, size);
3737423Sroot 		else
3747423Sroot 			bp = bread(dev, bn, size);
3757423Sroot #ifdef UNFAST
3767423Sroot 		iomove(bp->b_un.b_addr + on, n, B_WRITE);
3777423Sroot #else
3787423Sroot 		if (u.u_segflg != 1) {
3797423Sroot 			if (copyin(u.u_base, bp->b_un.b_addr + on, n)) {
3807423Sroot 				u.u_error = EFAULT;
3817423Sroot 				goto bad;
3827423Sroot 			}
3837423Sroot 		} else
3847423Sroot 			bcopy(u.u_base, bp->b_un.b_addr + on, n);
3857423Sroot 		u.u_base += n;
3867423Sroot 		u.u_offset += n;
3877423Sroot 		u.u_count -= n;
3887423Sroot bad:
3897423Sroot 		;
3907423Sroot #endif
3917423Sroot 		if (u.u_error != 0)
3927423Sroot 			brelse(bp);
3937423Sroot 		else {
3947423Sroot 			if ((ip->i_mode&IFMT) == IFDIR)
3957423Sroot 				/*
3967423Sroot 				 * Writing to clear a directory entry.
3977423Sroot 				 * Must insure the write occurs before
3987423Sroot 				 * the inode is freed, or may end up
3997423Sroot 				 * pointing at a new (different) file
4007423Sroot 				 * if inode is quickly allocated again
4017423Sroot 				 * and system crashes.
4027423Sroot 				 */
4037423Sroot 				bwrite(bp);
4047423Sroot 			else if (n + on == bsize) {
4057423Sroot 				bp->b_flags |= B_AGE;
4067423Sroot 				bawrite(bp);
4077423Sroot 			} else
4087423Sroot 				bdwrite(bp);
4097423Sroot 		}
4107423Sroot 		ip->i_flag |= IUPD|ICHG;
4117423Sroot 		if (u.u_ruid != 0)
4127423Sroot 			ip->i_mode &= ~(ISUID|ISGID);
4137423Sroot 	} while (u.u_error == 0 && u.u_count != 0);
4147423Sroot }
4157423Sroot 
4167423Sroot /*
4177423Sroot  * Move n bytes at byte location
4187423Sroot  * &bp->b_un.b_addr[o] to/from (flag) the
4197423Sroot  * user/kernel (u.segflg) area starting at u.base.
4207423Sroot  * Update all the arguments by the number
4217423Sroot  * of bytes moved.
4227423Sroot  */
4237423Sroot iomove(cp, n, flag)
4247423Sroot 	register caddr_t cp;
4257423Sroot 	register unsigned n;
4267423Sroot {
4277423Sroot 	register int t;
4287423Sroot 
4297423Sroot 	if (n==0)
4307423Sroot 		return;
4317423Sroot 	if (u.u_segflg != 1) {
4327423Sroot 		if (flag==B_WRITE)
4337423Sroot 			t = copyin(u.u_base, (caddr_t)cp, n);
4347423Sroot 		else
4357423Sroot 			t = copyout((caddr_t)cp, u.u_base, n);
4367423Sroot 		if (t) {
4377423Sroot 			u.u_error = EFAULT;
4387423Sroot 			return;
4397423Sroot 		}
4407423Sroot 	} else
4417423Sroot 		if (flag == B_WRITE)
4427423Sroot 			bcopy(u.u_base, (caddr_t)cp, n);
4437423Sroot 		else
4447423Sroot 			bcopy((caddr_t)cp, u.u_base, n);
4457423Sroot 	u.u_base += n;
4467423Sroot 	u.u_offset += n;
4477423Sroot 	u.u_count -= n;
4487423Sroot }
449