1 /* sys_generic.c 5.11 82/08/22 */ 2 3 #include "../h/param.h" 4 #include "../h/systm.h" 5 #include "../h/dir.h" 6 #include "../h/user.h" 7 #include "../h/tty.h" 8 #include "../h/file.h" 9 #include "../h/inode.h" 10 #include "../h/buf.h" 11 #include "../h/proc.h" 12 #include "../h/inline.h" 13 #include "../h/conf.h" 14 #include "../h/socket.h" 15 #include "../h/socketvar.h" 16 #include "../h/fs.h" 17 #ifdef MUSH 18 #include "../h/quota.h" 19 #include "../h/share.h" 20 #else 21 #define CHARGE(nothing) 22 #endif 23 #include "../h/vlimit.h" 24 #include "../h/descrip.h" 25 #include "../h/uio.h" 26 27 /* 28 * Read system call. 29 */ 30 read() 31 { 32 register struct a { 33 int fdes; 34 char *cbuf; 35 unsigned count; 36 } *uap = (struct a *)u.u_ap; 37 struct uio auio; 38 struct iovec aiov; 39 40 aiov.iov_base = (caddr_t)uap->cbuf; 41 aiov.iov_len = uap->count; 42 auio.uio_iov = &aiov; 43 auio.uio_iovcnt = 1; 44 rwuio(&auio, UIO_READ); 45 } 46 47 readv() 48 { 49 register struct a { 50 int fdes; 51 struct iovec *iovp; 52 int iovcnt; 53 } *uap = (struct a *)u.u_ap; 54 struct uio auio; 55 struct iovec aiov[16]; /* XXX */ 56 57 if (uap->iovcnt <= 0 || uap->iovcnt > sizeof(aiov)/sizeof(aiov[0])) { 58 u.u_error = EINVAL; 59 return; 60 } 61 auio.uio_iov = aiov; 62 auio.uio_iovcnt = uap->iovcnt; 63 if (copyin((caddr_t)uap->iovp, (caddr_t)aiov, 64 (unsigned)(uap->iovcnt * sizeof (struct iovec)))) { 65 u.u_error = EFAULT; 66 return; 67 } 68 rwuio(&auio, UIO_READ); 69 } 70 71 /* 72 * Write system call 73 */ 74 write() 75 { 76 register struct a { 77 int fdes; 78 char *cbuf; 79 int count; 80 } *uap = (struct a *)u.u_ap; 81 struct uio auio; 82 struct iovec aiov; 83 84 auio.uio_iov = &aiov; 85 auio.uio_iovcnt = 1; 86 aiov.iov_base = uap->cbuf; 87 aiov.iov_len = uap->count; 88 rwuio(&auio, UIO_WRITE); 89 } 90 91 writev() 92 { 93 register struct a { 94 int fdes; 95 struct iovec *iovp; 96 int iovcnt; 97 } *uap = (struct a *)u.u_ap; 98 struct uio auio; 99 struct iovec aiov[16]; /* XXX */ 100 101 if (uap->iovcnt <= 0 || uap->iovcnt > sizeof(aiov)/sizeof(aiov[0])) { 102 u.u_error = EINVAL; 103 return; 104 } 105 auio.uio_iov = aiov; 106 auio.uio_iovcnt = uap->iovcnt; 107 if (copyin((caddr_t)uap->iovp, (caddr_t)aiov, 108 (unsigned)(uap->iovcnt * sizeof (struct iovec)))) { 109 u.u_error = EFAULT; 110 return; 111 } 112 rwuio(&auio, UIO_WRITE); 113 } 114 115 rwuio(uio, rw) 116 register struct uio *uio; 117 enum uio_rw rw; 118 { 119 struct a { 120 int fdes; 121 }; 122 register struct file *fp; 123 register struct iovec *iov; 124 register struct inode *ip; 125 int i, count; 126 127 GETF(fp, ((struct a *)u.u_ap)->fdes); 128 if ((fp->f_flag&(rw==UIO_READ ? FREAD : FWRITE)) == 0) { 129 u.u_error = EBADF; 130 return; 131 } 132 uio->uio_resid = 0; 133 uio->uio_segflg = 0; 134 iov = uio->uio_iov; 135 for (i = 0; i < uio->uio_iovcnt; i++) { 136 if (iov->iov_len < 0) { 137 u.u_error = EINVAL; 138 return; 139 } 140 uio->uio_resid += iov->iov_len; 141 if (uio->uio_resid < 0) { 142 u.u_error = EINVAL; 143 return; 144 } 145 } 146 count = uio->uio_resid; 147 if ((u.u_procp->p_flag&SNUSIG) && setjmp(u.u_qsav)) { 148 if (uio->uio_resid == count) 149 u.u_eosys = RESTARTSYS; 150 } else if (fp->f_type == DTYPE_SOCKET) { 151 int sosend(), soreceive(); 152 u.u_error = 153 (*(rw==UIO_READ?soreceive:sosend)) 154 (fp->f_socket, (struct sockaddr *)0, uio); 155 } else { 156 ip = fp->f_inode; 157 uio->uio_offset = fp->f_offset; 158 if ((ip->i_mode&IFMT) == IFREG) { 159 ilock(ip); 160 u.u_error = rwip(ip, uio, rw); 161 iunlock(ip); 162 } else 163 u.u_error = rwip(ip, uio, rw); 164 fp->f_offset += count - uio->uio_resid; 165 } 166 u.u_r.r_val1 = count - uio->uio_resid; 167 } 168 169 rdwri(rw, ip, base, len, offset, segflg, aresid) 170 struct inode *ip; 171 caddr_t base; 172 int len, offset, segflg; 173 int *aresid; 174 enum uio_rw rw; 175 { 176 struct uio auio; 177 struct iovec aiov; 178 int error; 179 180 auio.uio_iov = &aiov; 181 auio.uio_iovcnt = 1; 182 aiov.iov_base = base; 183 aiov.iov_len = len; 184 auio.uio_resid = len; 185 auio.uio_offset = offset; 186 auio.uio_segflg = segflg; 187 error = rwip(ip, &auio, rw); 188 if (aresid) 189 *aresid = auio.uio_resid; 190 else 191 if (auio.uio_resid) 192 error = EIO; 193 return (error); 194 } 195 196 rwip(ip, uio, rw) 197 register struct inode *ip; 198 register struct uio *uio; 199 enum uio_rw rw; 200 { 201 dev_t dev = (dev_t)ip->i_rdev; 202 struct buf *bp; 203 struct fs *fs; 204 daddr_t lbn, bn; 205 register int on, type; 206 register unsigned n; 207 int size; 208 long bsize; 209 extern int mem_no; 210 int error = 0; 211 212 if (rw != UIO_READ && rw != UIO_WRITE) 213 panic("rwip"); 214 if (uio->uio_offset < 0 && 215 ((ip->i_mode&IFMT) != IFCHR || mem_no != major(dev))) 216 return (EINVAL); if (rw == UIO_READ) 217 ip->i_flag |= IACC; 218 type = ip->i_mode&IFMT; 219 if (type == IFCHR) { 220 #ifdef QUOTA 221 register c = uio->uio_resid; 222 #endif 223 if (rw == UIO_READ) 224 (*cdevsw[major(dev)].d_read)(dev, uio); 225 else { 226 ip->i_flag |= IUPD|ICHG; 227 (*cdevsw[major(dev)].d_write)(dev, uio); 228 } 229 CHARGE(sc_tio * (c - uio->uio_resid)); 230 return (u.u_error); 231 } 232 if (rw == UIO_WRITE && type == IFREG && 233 uio->uio_offset + uio->uio_resid > u.u_limit[LIM_FSIZE]) { 234 psignal(u.u_procp, SIGXFSZ); 235 return (EMFILE); 236 } 237 if (type != IFBLK) { 238 dev = ip->i_dev; 239 fs = ip->i_fs; 240 bsize = fs->fs_bsize; 241 } else 242 bsize = BLKDEV_IOSIZE; 243 do { 244 lbn = uio->uio_offset / bsize; 245 on = uio->uio_offset % bsize; 246 n = MIN((unsigned)(bsize - on), uio->uio_resid); 247 if (type != IFBLK) { 248 if (rw == UIO_READ) { 249 int diff = ip->i_size - uio->uio_offset; 250 if (diff <= 0) 251 return (0); 252 if (diff < n) 253 n = diff; 254 } 255 bn = fsbtodb(fs, 256 bmap(ip, lbn, rw == UIO_WRITE ? B_WRITE: B_READ, (int)(on+n))); 257 if (u.u_error || rw == UIO_WRITE && (long)bn<0) 258 return (u.u_error); 259 if (rw == UIO_WRITE && uio->uio_offset + n > ip->i_size && 260 (type == IFDIR || type == IFREG || type == IFLNK)) 261 ip->i_size = uio->uio_offset + n; 262 size = blksize(fs, ip, lbn); 263 } else { 264 bn = lbn * (BLKDEV_IOSIZE/DEV_BSIZE); 265 rablock = bn + (BLKDEV_IOSIZE/DEV_BSIZE); 266 rasize = size = bsize; 267 } 268 if (rw == UIO_READ) { 269 if ((long)bn<0) { 270 bp = geteblk(size); 271 clrbuf(bp); 272 } else if (ip->i_lastr + 1 == lbn) 273 bp = breada(dev, bn, size, rablock, rasize); 274 else 275 bp = bread(dev, bn, size); 276 ip->i_lastr = lbn; 277 } else { 278 int i, count; 279 280 count = howmany(size, DEV_BSIZE); 281 for (i = 0; i < count; i += CLSIZE) 282 if (mfind(dev, bn + i)) 283 munhash(dev, bn + i); 284 if (n == bsize) 285 bp = getblk(dev, bn, size); 286 else 287 bp = bread(dev, bn, size); 288 } 289 n = MIN(n, size - bp->b_resid); 290 if (bp->b_flags & B_ERROR) { 291 error = EIO; 292 brelse(bp); 293 goto bad; 294 } 295 u.u_error = 296 uiomove(bp->b_un.b_addr+on, (u_int)n, rw, uio); 297 if (rw == UIO_READ) { 298 if (n + on == bsize || uio->uio_offset == ip->i_size) 299 bp->b_flags |= B_AGE; 300 brelse(bp); 301 } else { 302 if ((ip->i_mode&IFMT) == IFDIR) 303 bwrite(bp); 304 else if (n + on == bsize) { 305 bp->b_flags |= B_AGE; 306 bawrite(bp); 307 } else 308 bdwrite(bp); 309 ip->i_flag |= IUPD|ICHG; 310 if (u.u_ruid != 0) 311 ip->i_mode &= ~(ISUID|ISGID); 312 } 313 } while (u.u_error == 0 && uio->uio_resid > 0 && n != 0); 314 bad: 315 return (error); 316 } 317 318 uiomove(cp, n, rw, uio) 319 register caddr_t cp; 320 register int n; 321 enum uio_rw rw; 322 register struct uio *uio; 323 { 324 register struct iovec *iov; 325 int error; 326 u_int cnt; 327 328 while (n > 0 && uio->uio_resid) { 329 iov = uio->uio_iov; 330 cnt = iov->iov_len; 331 if (cnt == 0) { 332 uio->uio_iov++; 333 uio->uio_iovcnt--; 334 continue; 335 } 336 if (cnt > n) 337 cnt = n; 338 switch (uio->uio_segflg) { 339 340 case 0: 341 case 2: 342 if (rw == UIO_READ) 343 error = copyout(cp, iov->iov_base, cnt); 344 else 345 error = copyin(iov->iov_base, cp, cnt); 346 if (error) 347 return (error); 348 break; 349 350 case 1: 351 if (rw == UIO_READ) 352 bcopy((caddr_t)cp, iov->iov_base, cnt); 353 else 354 bcopy(iov->iov_base, (caddr_t)cp, cnt); 355 break; 356 } 357 iov->iov_base += cnt; 358 iov->iov_len -= cnt; 359 uio->uio_resid -= cnt; 360 uio->uio_offset += cnt; 361 cp += cnt; 362 n -= cnt; 363 } 364 return (error); 365 } 366 367 /* 368 * Give next character to user as result of read. 369 */ 370 ureadc(c, uio) 371 register int c; 372 register struct uio *uio; 373 { 374 register struct iovec *iov; 375 376 again: 377 if (uio->uio_iovcnt == 0) 378 panic("ureadc"); 379 iov = uio->uio_iov; 380 if (iov->iov_len <= 0 || uio->uio_resid <= 0) { 381 uio->uio_iovcnt--; 382 uio->uio_iov++; 383 goto again; 384 } 385 switch (uio->uio_segflg) { 386 387 case 0: 388 if (subyte(iov->iov_base, c) < 0) 389 return (EFAULT); 390 break; 391 392 case 1: 393 *iov->iov_base = c; 394 break; 395 396 case 2: 397 if (suibyte(iov->iov_base, c) < 0) 398 return (EFAULT); 399 break; 400 } 401 iov->iov_base++; 402 iov->iov_len--; 403 uio->uio_resid--; 404 uio->uio_offset++; 405 return (0); 406 } 407 408 /* 409 * Get next character written in by user from uio. 410 */ 411 uwritec(uio) 412 struct uio *uio; 413 { 414 register struct iovec *iov; 415 register int c; 416 417 again: 418 if (uio->uio_iovcnt <= 0 || uio->uio_resid <= 0) 419 panic("uwritec"); 420 iov = uio->uio_iov; 421 if (iov->iov_len == 0) { 422 uio->uio_iovcnt--; 423 uio->uio_iov++; 424 goto again; 425 } 426 switch (uio->uio_segflg) { 427 428 case 0: 429 c = fubyte(iov->iov_base); 430 break; 431 432 case 1: 433 c = *iov->iov_base & 0377; 434 break; 435 436 case 2: 437 c = fuibyte(iov->iov_base); 438 break; 439 } 440 if (c < 0) 441 return (-1); 442 iov->iov_base++; 443 iov->iov_len--; 444 uio->uio_resid--; 445 uio->uio_offset++; 446 return (c & 0377); 447 } 448 449 /* 450 * Ioctl system call 451 * Check legality, execute common code, 452 * and switch out to individual device routine. 453 */ 454 ioctl() 455 { 456 register struct file *fp; 457 struct a { 458 int fdes; 459 int cmd; 460 caddr_t cmarg; 461 } *uap; 462 register int com; 463 register u_int size; 464 char data[IOCPARM_MASK+1]; 465 466 uap = (struct a *)u.u_ap; 467 if ((fp = getf(uap->fdes)) == NULL) 468 return; 469 if ((fp->f_flag & (FREAD|FWRITE)) == 0) { 470 u.u_error = EBADF; 471 return; 472 } 473 com = uap->cmd; 474 475 #ifndef NOCOMPAT 476 /* 477 * Map old style ioctl's into new for the 478 * sake of backwards compatibility (sigh). 479 */ 480 if ((com&~0xffff) == 0) { 481 com = mapioctl(com); 482 if (com == 0) { 483 u.u_error = EINVAL; 484 return; 485 } 486 } 487 #endif 488 if (com == FIOCLEX) { 489 u.u_pofile[uap->fdes] |= EXCLOSE; 490 return; 491 } 492 if (com == FIONCLEX) { 493 u.u_pofile[uap->fdes] &= ~EXCLOSE; 494 return; 495 } 496 497 /* 498 * Interpret high order word to find 499 * amount of data to be copied to/from the 500 * user's address space. 501 */ 502 size = (com &~ (IOC_INOUT|IOC_VOID)) >> 16; 503 if (size > sizeof (data)) { 504 u.u_error = EFAULT; 505 return; 506 } 507 if (com&IOC_IN && size) { 508 if (copyin(uap->cmarg, (caddr_t)data, (u_int)size)) { 509 u.u_error = EFAULT; 510 return; 511 } 512 } else 513 *(caddr_t *)data = uap->cmarg; 514 /* 515 * Zero the buffer on the stack so the user 516 * always gets back something deterministic. 517 */ 518 if ((com&IOC_OUT) && size) 519 bzero((caddr_t)data, size); 520 521 if (fp->f_type == DTYPE_SOCKET) 522 soioctl(fp->f_socket, com, data); 523 else { 524 register struct inode *ip = fp->f_inode; 525 int fmt = ip->i_mode & IFMT; 526 dev_t dev; 527 528 if (fmt != IFCHR) { 529 if (com == FIONREAD && (fmt == IFREG || fmt == IFDIR)) { 530 *(off_t *)data = ip->i_size - fp->f_offset; 531 goto returndata; 532 } 533 if (com != FIONBIO && com != FIOASYNC) 534 u.u_error = ENOTTY; 535 return; 536 } 537 dev = ip->i_rdev; 538 u.u_r.r_val1 = 0; 539 if ((u.u_procp->p_flag&SNUSIG) && setjmp(u.u_qsav)) { 540 u.u_eosys = RESTARTSYS; 541 return; 542 } 543 (*cdevsw[major(dev)].d_ioctl)(dev, com, data, 0); 544 } 545 546 returndata: 547 /* 548 * Copy any data to user, size was 549 * already set and checked above. 550 */ 551 if (u.u_error == 0 && (com&IOC_OUT)) 552 if (size && copyout(data, uap->cmarg, (u_int)size)) 553 u.u_error = EFAULT; 554 } 555 556 /* 557 * Do nothing specific version of line 558 * discipline specific ioctl command. 559 */ 560 /*ARGSUSED*/ 561 nullioctl(tp, cmd, data, flags) 562 struct tty *tp; 563 char *data; 564 int flags; 565 { 566 567 #ifdef lint 568 tp = tp; data = data; flags = flags; 569 #endif 570 return (cmd); 571 } 572