1 /* vfs_syscalls.c 4.55 83/04/01 */ 2 3 #include "../h/param.h" 4 #include "../h/systm.h" 5 #include "../h/dir.h" 6 #include "../h/user.h" 7 #include "../h/kernel.h" 8 #include "../h/file.h" 9 #include "../h/stat.h" 10 #include "../h/inode.h" 11 #include "../h/fs.h" 12 #include "../h/buf.h" 13 #include "../h/proc.h" 14 #include "../h/quota.h" 15 #include "../h/descrip.h" 16 #include "../h/uio.h" 17 #include "../h/socket.h" 18 #include "../h/socketvar.h" 19 #include "../h/nami.h" 20 21 /* 22 * Change current working directory (``.''). 23 */ 24 chdir() 25 { 26 27 chdirec(&u.u_cdir); 28 } 29 30 /* 31 * Change notion of root (``/'') directory. 32 */ 33 chroot() 34 { 35 36 if (suser()) 37 chdirec(&u.u_rdir); 38 } 39 40 /* 41 * Common routine for chroot and chdir. 42 */ 43 chdirec(ipp) 44 register struct inode **ipp; 45 { 46 register struct inode *ip; 47 struct a { 48 char *fname; 49 }; 50 51 ip = namei(uchar, LOOKUP, 1); 52 if (ip == NULL) 53 return; 54 if ((ip->i_mode&IFMT) != IFDIR) { 55 u.u_error = ENOTDIR; 56 goto bad; 57 } 58 if (access(ip, IEXEC)) 59 goto bad; 60 iunlock(ip); 61 if (*ipp) 62 irele(*ipp); 63 *ipp = ip; 64 return; 65 66 bad: 67 iput(ip); 68 } 69 70 /* 71 * Open system call. 72 */ 73 open() 74 { 75 register struct inode *ip; 76 register struct a { 77 char *fname; 78 int flags; 79 int mode; 80 } *uap; 81 int checkpermissions = 1, flags; 82 83 uap = (struct a *)u.u_ap; 84 flags = uap->flags + 1; 85 if ((flags&FTRUNCATE) && (flags&FWRITE) == 0) { 86 u.u_error = EINVAL; 87 return; 88 } 89 if (flags&FCREATE) { 90 ip = namei(uchar, CREATE, 1); 91 if (ip == NULL) { 92 if (u.u_error) 93 return; 94 ip = maknode(uap->mode&07777&(~ISVTX)); 95 checkpermissions = 0; 96 flags &= ~FTRUNCATE; 97 } 98 } else 99 ip = namei(uchar, LOOKUP, 1); 100 if (ip == NULL) 101 return; 102 open1(ip, flags, checkpermissions); 103 } 104 105 #ifndef NOCOMPAT 106 /* 107 * Creat system call. 108 */ 109 ocreat() 110 { 111 register struct inode *ip; 112 register struct a { 113 char *fname; 114 int fmode; 115 } *uap; 116 117 uap = (struct a *)u.u_ap; 118 ip = namei(uchar, CREATE, 1); 119 if (ip == NULL) { 120 if (u.u_error) 121 return; 122 ip = maknode(uap->fmode&07777&(~ISVTX)); 123 if (ip == NULL) 124 return; 125 open1(ip, FWRITE, 0); 126 } else 127 open1(ip, FWRITE|FTRUNCATE, 1); 128 } 129 #endif 130 131 /* 132 * Common code for open and creat. 133 * Check permissions (if we haven't done so already), 134 * allocate an open file structure, and call 135 * the device open routine, if any. 136 */ 137 open1(ip, mode, checkpermissions) 138 register struct inode *ip; 139 register mode; 140 { 141 register struct file *fp; 142 int i, flags; 143 144 if (checkpermissions) { 145 if (mode&FREAD) 146 if (access(ip, IREAD)) 147 goto bad; 148 if (mode&FWRITE) { 149 if (access(ip, IWRITE)) 150 goto bad; 151 if ((ip->i_mode&IFMT) == IFDIR) { 152 u.u_error = EISDIR; 153 goto bad; 154 } 155 } 156 } 157 158 /* 159 * Check locking on inode. Release "inode lock" 160 * while doing so in case we block inside flocki. 161 */ 162 flags = 0; 163 if (mode&(FSHLOCK|FEXLOCK)) { 164 iunlock(ip); 165 flags = flocki(ip, 0, mode); 166 ilock(ip); 167 if (u.u_error) 168 goto bad; 169 } 170 if (mode&FTRUNCATE) 171 itrunc(ip, (u_long)0); 172 iunlock(ip); 173 if ((fp = falloc()) == NULL) 174 goto out; 175 fp->f_flag = mode & FMODES; 176 fp->f_type = DTYPE_FILE; 177 i = u.u_r.r_val1; 178 fp->f_inode = ip; 179 u.u_error = openi(ip, mode); 180 if (u.u_error == 0) { 181 u.u_pofile[i] = flags; 182 return; 183 } 184 u.u_ofile[i] = NULL; 185 fp->f_count--; 186 out: 187 irele(ip); 188 return; 189 bad: 190 iput(ip); 191 } 192 193 /* 194 * Mknod system call 195 */ 196 mknod() 197 { 198 register struct inode *ip; 199 register struct a { 200 char *fname; 201 int fmode; 202 int dev; 203 } *uap; 204 205 uap = (struct a *)u.u_ap; 206 if (suser()) { 207 ip = namei(uchar, CREATE, 0); 208 if (ip != NULL) { 209 u.u_error = EEXIST; 210 goto out; 211 } 212 } 213 if (u.u_error) 214 return; 215 ip = maknode(uap->fmode); 216 if (ip == NULL) 217 return; 218 if (uap->dev) { 219 /* 220 * Want to be able to use this to make badblock 221 * inodes, so don't truncate the dev number. 222 */ 223 ip->i_rdev = uap->dev; 224 ip->i_flag |= IACC|IUPD|ICHG; 225 } 226 227 out: 228 iput(ip); 229 } 230 231 /* 232 * link system call 233 */ 234 link() 235 { 236 register struct inode *ip, *xp; 237 register struct a { 238 char *target; 239 char *linkname; 240 } *uap; 241 242 uap = (struct a *)u.u_ap; 243 ip = namei(uchar, LOOKUP, 1); /* well, this routine is doomed anyhow */ 244 if (ip == NULL) 245 return; 246 if ((ip->i_mode&IFMT) == IFDIR && !suser()) { 247 iput(ip); 248 return; 249 } 250 ip->i_nlink++; 251 ip->i_flag |= ICHG; 252 iupdat(ip, &time, &time, 1); 253 iunlock(ip); 254 u.u_dirp = (caddr_t)uap->linkname; 255 xp = namei(uchar, CREATE, 0); 256 if (xp != NULL) { 257 u.u_error = EEXIST; 258 iput(xp); 259 goto out; 260 } 261 if (u.u_error) 262 goto out; 263 if (u.u_pdir->i_dev != ip->i_dev) { 264 iput(u.u_pdir); 265 u.u_error = EXDEV; 266 goto out; 267 } 268 u.u_error = direnter(ip); 269 out: 270 if (u.u_error) { 271 ip->i_nlink--; 272 ip->i_flag |= ICHG; 273 } 274 irele(ip); 275 } 276 277 /* 278 * symlink -- make a symbolic link 279 */ 280 symlink() 281 { 282 register struct a { 283 char *target; 284 char *linkname; 285 } *uap; 286 register struct inode *ip; 287 register char *tp; 288 register c, nc; 289 290 uap = (struct a *)u.u_ap; 291 tp = uap->target; 292 nc = 0; 293 while (c = fubyte(tp)) { 294 if (c < 0) { 295 u.u_error = EFAULT; 296 return; 297 } 298 tp++; 299 nc++; 300 } 301 u.u_dirp = uap->linkname; 302 ip = namei(uchar, CREATE, 0); 303 if (ip) { 304 iput(ip); 305 u.u_error = EEXIST; 306 return; 307 } 308 if (u.u_error) 309 return; 310 ip = maknode(IFLNK | 0777); 311 if (ip == NULL) 312 return; 313 u.u_error = rdwri(UIO_WRITE, ip, uap->target, nc, 0, 0, (int *)0); 314 /* handle u.u_error != 0 */ 315 iput(ip); 316 } 317 318 /* 319 * Unlink system call. 320 * Hard to avoid races here, especially 321 * in unlinking directories. 322 */ 323 unlink() 324 { 325 struct a { 326 char *fname; 327 }; 328 register struct inode *ip, *dp; 329 330 ip = namei(uchar, DELETE | LOCKPARENT, 0); 331 if (ip == NULL) 332 return; 333 dp = u.u_pdir; 334 if ((ip->i_mode&IFMT) == IFDIR && !suser()) 335 goto out; 336 /* 337 * Don't unlink a mounted file. 338 */ 339 if (ip->i_dev != dp->i_dev) { 340 u.u_error = EBUSY; 341 goto out; 342 } 343 if (ip->i_flag&ITEXT) 344 xrele(ip); /* try once to free text */ 345 if (dirremove()) { 346 ip->i_nlink--; 347 ip->i_flag |= ICHG; 348 } 349 out: 350 if (dp == ip) 351 irele(ip); 352 else 353 iput(ip); 354 iput(dp); 355 } 356 357 /* 358 * Seek system call 359 */ 360 lseek() 361 { 362 register struct file *fp; 363 register struct a { 364 int fd; 365 off_t off; 366 int sbase; 367 } *uap; 368 369 uap = (struct a *)u.u_ap; 370 fp = getf(uap->fd); 371 if (fp == NULL) 372 return; 373 if (fp->f_type == DTYPE_SOCKET) { 374 u.u_error = ESPIPE; 375 return; 376 } 377 if (uap->sbase == FSEEK_RELATIVE) 378 uap->off += fp->f_offset; 379 else if (uap->sbase == FSEEK_EOF) 380 uap->off += fp->f_inode->i_size; 381 fp->f_offset = uap->off; 382 u.u_r.r_off = uap->off; 383 } 384 385 /* 386 * Access system call 387 */ 388 saccess() 389 { 390 register svuid, svgid; 391 register struct inode *ip; 392 register struct a { 393 char *fname; 394 int fmode; 395 } *uap; 396 397 uap = (struct a *)u.u_ap; 398 svuid = u.u_uid; 399 svgid = u.u_gid; 400 u.u_uid = u.u_ruid; 401 u.u_gid = u.u_rgid; 402 ip = namei(uchar, LOOKUP, 1); 403 if (ip != NULL) { 404 if ((uap->fmode&FACCESS_READ) && access(ip, IREAD)) 405 goto done; 406 if ((uap->fmode&FACCESS_WRITE) && access(ip, IWRITE)) 407 goto done; 408 if ((uap->fmode&FACCESS_EXECUTE) && access(ip, IEXEC)) 409 goto done; 410 done: 411 iput(ip); 412 } 413 u.u_uid = svuid; 414 u.u_gid = svgid; 415 } 416 417 /* 418 * the fstat system call. 419 */ 420 fstat() 421 { 422 register struct file *fp; 423 register struct a { 424 int fd; 425 struct stat *sb; 426 } *uap; 427 428 uap = (struct a *)u.u_ap; 429 fp = getf(uap->fd); 430 if (fp == NULL) 431 return; 432 if (fp->f_type == DTYPE_SOCKET) 433 u.u_error = sostat(fp->f_socket, uap->sb); 434 else 435 stat1(fp->f_inode, uap->sb); 436 } 437 438 /* 439 * Stat system call. This version follows links. 440 */ 441 stat() 442 { 443 register struct inode *ip; 444 register struct a { 445 char *fname; 446 struct stat *sb; 447 } *uap; 448 449 uap = (struct a *)u.u_ap; 450 ip = namei(uchar, LOOKUP, 1); 451 if (ip == NULL) 452 return; 453 stat1(ip, uap->sb); 454 iput(ip); 455 } 456 457 /* 458 * Lstat system call. This version does not follow links. 459 */ 460 lstat() 461 { 462 register struct inode *ip; 463 register struct a { 464 char *fname; 465 struct stat *sb; 466 } *uap; 467 468 uap = (struct a *)u.u_ap; 469 ip = namei(uchar, LOOKUP, 0); 470 if (ip == NULL) 471 return; 472 stat1(ip, uap->sb); 473 iput(ip); 474 } 475 476 /* 477 * The basic routine for fstat and stat: 478 * get the inode and pass appropriate parts back. 479 */ 480 stat1(ip, ub) 481 register struct inode *ip; 482 struct stat *ub; 483 { 484 struct stat ds; 485 486 IUPDAT(ip, &time, &time, 0); 487 /* 488 * Copy from inode table 489 */ 490 ds.st_dev = ip->i_dev; 491 ds.st_ino = ip->i_number; 492 ds.st_mode = ip->i_mode; 493 ds.st_nlink = ip->i_nlink; 494 ds.st_uid = ip->i_uid; 495 ds.st_gid = ip->i_gid; 496 ds.st_rdev = (dev_t)ip->i_rdev; 497 ds.st_size = ip->i_size; 498 ds.st_atime = ip->i_atime; 499 ds.st_spare1 = 0; 500 ds.st_mtime = ip->i_mtime; 501 ds.st_spare2 = 0; 502 ds.st_ctime = ip->i_ctime; 503 ds.st_spare3 = 0; 504 /* this doesn't belong here */ 505 if ((ip->i_mode&IFMT) == IFBLK) 506 ds.st_blksize = BLKDEV_IOSIZE; 507 else if ((ip->i_mode&IFMT) == IFCHR) 508 ds.st_blksize = MAXBSIZE; 509 else 510 ds.st_blksize = ip->i_fs->fs_bsize; 511 ds.st_spare4[0] = ds.st_spare4[1] = ds.st_spare4[2] = 0; 512 u.u_error = copyout((caddr_t)&ds, (caddr_t)ub, sizeof(ds)); 513 } 514 515 /* 516 * Return target name of a symbolic link 517 */ 518 readlink() 519 { 520 register struct inode *ip; 521 register struct a { 522 char *name; 523 char *buf; 524 int count; 525 } *uap = (struct a *)u.u_ap; 526 int resid; 527 528 ip = namei(uchar, LOOKUP, 0); 529 if (ip == NULL) 530 return; 531 if ((ip->i_mode&IFMT) != IFLNK) { 532 u.u_error = ENXIO; 533 goto out; 534 } 535 u.u_error = rdwri(UIO_READ, ip, uap->buf, uap->count, 0, 0, &resid); 536 out: 537 iput(ip); 538 u.u_r.r_val1 = uap->count - resid; 539 } 540 541 /* 542 * Change mode of a file given path name. 543 */ 544 chmod() 545 { 546 struct inode *ip; 547 struct a { 548 char *fname; 549 int fmode; 550 } *uap; 551 552 uap = (struct a *)u.u_ap; 553 if ((ip = owner(1)) == NULL) 554 return; 555 chmod1(ip, uap->fmode); 556 iput(ip); 557 } 558 559 /* 560 * Change mode of a file given a file descriptor. 561 */ 562 fchmod() 563 { 564 struct a { 565 int fd; 566 int fmode; 567 } *uap; 568 register struct inode *ip; 569 register struct file *fp; 570 571 uap = (struct a *)u.u_ap; 572 fp = getf(uap->fd); 573 if (fp == NULL) 574 return; 575 if (fp->f_type == DTYPE_SOCKET) { 576 u.u_error = EINVAL; 577 return; 578 } 579 ip = fp->f_inode; 580 if (u.u_uid != ip->i_uid && !suser()) 581 return; 582 ilock(ip); 583 chmod1(ip, uap->fmode); 584 iunlock(ip); 585 } 586 587 /* 588 * Change the mode on a file. 589 * Inode must be locked before calling. 590 */ 591 chmod1(ip, mode) 592 register struct inode *ip; 593 register int mode; 594 { 595 register int *gp; 596 597 ip->i_mode &= ~07777; 598 if (u.u_uid) { 599 mode &= ~ISVTX; 600 if (!groupmember(ip->i_gid)) 601 mode &= ~ISGID; 602 #ifdef MUSH 603 if (u.u_quota->q_syflags & QF_UMASK && 604 (ip->i_mode & IFMT) != IFCHR) 605 mode &= ~u.u_cmask; 606 #endif 607 } 608 ip->i_mode |= mode&07777; 609 ip->i_flag |= ICHG; 610 if (ip->i_flag&ITEXT && (ip->i_mode&ISVTX)==0) 611 xrele(ip); 612 } 613 614 /* 615 * Set ownership given a path name. 616 */ 617 chown() 618 { 619 struct inode *ip; 620 struct a { 621 char *fname; 622 int uid; 623 int gid; 624 } *uap; 625 626 uap = (struct a *)u.u_ap; 627 if (!suser() || (ip = owner(0)) == NULL) 628 return; 629 u.u_error = chown1(ip, uap->uid, uap->gid); 630 iput(ip); 631 } 632 633 /* 634 * Set ownership given a file descriptor. 635 */ 636 fchown() 637 { 638 struct a { 639 int fd; 640 int uid; 641 int gid; 642 } *uap; 643 register struct inode *ip; 644 register struct file *fp; 645 646 uap = (struct a *)u.u_ap; 647 fp = getf(uap->fd); 648 if (fp == NULL) 649 return; 650 if (fp->f_type == DTYPE_SOCKET) { 651 u.u_error = EINVAL; 652 return; 653 } 654 ip = fp->f_inode; 655 if (!suser()) 656 return; 657 ilock(ip); 658 u.u_error = chown1(ip, uap->uid, uap->gid); 659 iunlock(ip); 660 } 661 662 /* 663 * Perform chown operation on inode ip; 664 * inode must be locked prior to call. 665 */ 666 chown1(ip, uid, gid) 667 register struct inode *ip; 668 int uid, gid; 669 { 670 #ifdef QUOTA 671 register long change; 672 #endif 673 674 if (uid == -1) 675 uid = ip->i_uid; 676 if (gid == -1) 677 gid = ip->i_gid; 678 #ifdef QUOTA 679 /* 680 * This doesn't allow for holes in files (which hopefully don't 681 * happen often in files that we chown), and is not accurate anyway 682 * (eg: it totally ignores 3 level indir blk files - but hopefully 683 * noone who can make a file that big will have a quota) 684 */ 685 if (ip->i_uid == uid) 686 change = 0; 687 else { 688 register struct fs *fs = ip->i_fs; 689 690 if (ip->i_size > (change = NDADDR * fs->fs_bsize)) { 691 register off_t size; 692 693 size = blkroundup(fs, ip->i_size) - change; 694 change += size; 695 change += fs->fs_bsize; 696 /* this assumes NIADDR <= 2 */ 697 if (size > NINDIR(fs) * fs->fs_bsize) 698 change += fs->fs_bsize; 699 } else 700 change = fragroundup(fs, ip->i_size); 701 change /= DEV_BSIZE; 702 } 703 (void)chkdq(ip, -change, 1); 704 (void)chkiq(ip->i_dev, ip, ip->i_uid, 1); 705 dqrele(ip->i_dquot); 706 #endif 707 ip->i_uid = uid; 708 ip->i_gid = gid; 709 ip->i_flag |= ICHG; 710 if (u.u_ruid != 0) 711 ip->i_mode &= ~(ISUID|ISGID); 712 #ifdef QUOTA 713 ip->i_dquot = inoquota(ip); 714 (void)chkdq(ip, change, 1); 715 (void)chkiq(ip->i_dev, (struct inode *)NULL, uid, 1); 716 return (u.u_error); 717 #endif 718 return (0); 719 } 720 721 #ifndef NOCOMPAT 722 /* 723 * Set IUPD and IACC times on file. 724 * Can't set ICHG. 725 */ 726 outime() 727 { 728 register struct a { 729 char *fname; 730 time_t *tptr; 731 } *uap = (struct a *)u.u_ap; 732 register struct inode *ip; 733 time_t tv[2]; 734 struct timeval tv0, tv1; 735 736 if ((ip = owner(1)) == NULL) 737 return; 738 u.u_error = copyin((caddr_t)uap->tptr, (caddr_t)tv, sizeof (tv)); 739 if (u.u_error == 0) { 740 ip->i_flag |= IACC|IUPD|ICHG; 741 tv0.tv_sec = tv[0]; tv0.tv_usec = 0; 742 tv1.tv_sec = tv[1]; tv1.tv_usec = 0; 743 iupdat(ip, &tv0, &tv1, 0); 744 } 745 iput(ip); 746 } 747 #endif 748 749 utimes() 750 { 751 register struct a { 752 char *fname; 753 struct timeval *tptr; 754 } *uap = (struct a *)u.u_ap; 755 register struct inode *ip; 756 struct timeval tv[2]; 757 758 if ((ip = owner(1)) == NULL) 759 return; 760 u.u_error = copyin((caddr_t)uap->tptr, (caddr_t)tv, sizeof (tv)); 761 if (u.u_error == 0) { 762 ip->i_flag |= IACC|IUPD|ICHG; 763 iupdat(ip, &tv[0], &tv[1], 0); 764 } 765 iput(ip); 766 } 767 768 /* 769 * Flush any pending I/O. 770 */ 771 sync() 772 { 773 774 update(); 775 } 776 777 /* 778 * Apply an advisory lock on a file descriptor. 779 */ 780 flock() 781 { 782 struct a { 783 int fd; 784 int how; 785 } *uap; 786 register struct file *fp; 787 register int cmd, flags; 788 789 uap = (struct a *)u.u_ap; 790 fp = getf(uap->fd); 791 if (fp == NULL) 792 return; 793 if (fp->f_type == DTYPE_SOCKET) { /* XXX */ 794 u.u_error = EINVAL; 795 return; 796 } 797 cmd = uap->how; 798 flags = u.u_pofile[uap->fd] & (UF_SHLOCK|UF_EXLOCK); 799 if (cmd&FUNLOCK) { 800 if (flags == 0) { 801 u.u_error = EINVAL; 802 return; 803 } 804 funlocki(fp->f_inode, flags); 805 u.u_pofile[uap->fd] &= ~(UF_SHLOCK|UF_EXLOCK); 806 return; 807 } 808 /* 809 * No reason to write lock a file we've already 810 * write locked, similarly with a read lock. 811 */ 812 if ((flags&UF_EXLOCK) && (cmd&FEXLOCK) || 813 (flags&UF_SHLOCK) && (cmd&FSHLOCK)) 814 return; 815 u.u_pofile[uap->fd] = flocki(fp->f_inode, u.u_pofile[uap->fd], cmd); 816 } 817 818 /* 819 * Truncate a file given its path name. 820 */ 821 truncate() 822 { 823 struct a { 824 char *fname; 825 u_long length; 826 } *uap = (struct a *)u.u_ap; 827 struct inode *ip; 828 829 ip = namei(uchar, LOOKUP, 1); 830 if (ip == NULL) 831 return; 832 if (access(ip, IWRITE)) 833 goto bad; 834 if ((ip->i_mode&IFMT) == IFDIR) { 835 u.u_error = EISDIR; 836 goto bad; 837 } 838 itrunc(ip, uap->length); 839 bad: 840 iput(ip); 841 } 842 843 /* 844 * Truncate a file given a file descriptor. 845 */ 846 ftruncate() 847 { 848 struct a { 849 int fd; 850 u_long length; 851 } *uap = (struct a *)u.u_ap; 852 struct inode *ip; 853 struct file *fp; 854 855 fp = getf(uap->fd); 856 if (fp == NULL) 857 return; 858 if (fp->f_type == DTYPE_SOCKET) { 859 u.u_error = EINVAL; 860 return; 861 } 862 if ((fp->f_flag&FWRITE) == 0) { 863 u.u_error = EINVAL; 864 return; 865 } 866 ip = fp->f_inode; 867 ilock(ip); 868 itrunc(ip, uap->length); 869 iunlock(ip); 870 } 871 872 /* 873 * Synch an open file. 874 */ 875 fsync() 876 { 877 struct a { 878 int fd; 879 } *uap = (struct a *)u.u_ap; 880 struct inode *ip; 881 struct file *fp; 882 883 fp = getf(uap->fd); 884 if (fp == NULL) 885 return; 886 if (fp->f_type == DTYPE_SOCKET) { 887 u.u_error = EINVAL; 888 return; 889 } 890 ip = fp->f_inode; 891 ilock(ip); 892 syncip(ip); 893 iunlock(ip); 894 } 895 896 /* 897 * Rename system call. 898 * rename("foo", "bar"); 899 * is essentially 900 * unlink("bar"); 901 * link("foo", "bar"); 902 * unlink("foo"); 903 * but ``atomically''. Can't do full commit without saving state in the 904 * inode on disk which isn't feasible at this time. Best we can do is 905 * always guarantee the target exists. 906 * 907 * Basic algorithm is: 908 * 909 * 1) Bump link count on source while we're linking it to the 910 * target. This also insure the inode won't be deleted out 911 * from underneath us while we work. 912 * 2) Link source to destination. If destination already exists, 913 * delete it first. 914 * 3) Unlink source reference to inode if still around. 915 * 4) If a directory was moved and the parent of the destination 916 * is different from the source, patch the ".." entry in the 917 * directory. 918 * 919 * Source and destination must either both be directories, or both 920 * not be directories. If target is a directory, it must be empty. 921 */ 922 rename() 923 { 924 struct a { 925 char *from; 926 char *to; 927 } *uap; 928 register struct inode *ip, *xp, *dp; 929 int oldparent, parentdifferent, doingdirectory; 930 int error = 0; 931 932 uap = (struct a *)u.u_ap; 933 ip = namei(uchar, DELETE | LOCKPARENT, 0); 934 if (ip == NULL) 935 return; 936 dp = u.u_pdir; 937 oldparent = 0, doingdirectory = 0; 938 if ((ip->i_mode&IFMT) == IFDIR) { 939 register struct direct *d; 940 941 d = &u.u_dent; 942 /* 943 * Avoid ".", "..", and aliases of "." for obvious reasons. 944 */ 945 if ((d->d_namlen == 1 && d->d_name[0] == '.') || 946 (d->d_namlen == 2 && bcmp(d->d_name, "..", 2) == 0) || 947 (dp == ip)) { 948 iput(dp); 949 if (dp == ip) 950 irele(ip); 951 else 952 iput(ip); 953 u.u_error = EINVAL; 954 return; 955 } 956 oldparent = dp->i_number; 957 doingdirectory++; 958 } 959 iput(dp); 960 961 /* 962 * 1) Bump link count while we're moving stuff 963 * around. If we crash somewhere before 964 * completing our work, the link count 965 * may be wrong, but correctable. 966 */ 967 ip->i_nlink++; 968 ip->i_flag |= ICHG; 969 iupdat(ip, &time, &time, 1); 970 iunlock(ip); 971 972 /* 973 * When the target exists, both the directory 974 * and target inodes are returned locked. 975 */ 976 u.u_dirp = (caddr_t)uap->to; 977 xp = namei(uchar, CREATE | LOCKPARENT, 0); 978 if (u.u_error) { 979 error = u.u_error; 980 goto out; 981 } 982 dp = u.u_pdir; 983 /* 984 * If ".." must be changed (ie the directory gets a new 985 * parent) then the user must have write permission. 986 */ 987 parentdifferent = oldparent != dp->i_number; 988 if (doingdirectory && parentdifferent && access(ip, IWRITE)) 989 goto bad; 990 /* 991 * 2) If target doesn't exist, link the target 992 * to the source and unlink the source. 993 * Otherwise, rewrite the target directory 994 * entry to reference the source inode and 995 * expunge the original entry's existence. 996 */ 997 if (xp == NULL) { 998 if (dp->i_dev != ip->i_dev) { 999 error = EXDEV; 1000 goto bad; 1001 } 1002 /* 1003 * Disallow rename(foo, foo/bar). 1004 */ 1005 if (dp->i_number == ip->i_number) { 1006 error = EEXIST; 1007 goto bad; 1008 } 1009 /* 1010 * Account for ".." in directory. 1011 * When source and destination have the 1012 * same parent we don't fool with the 1013 * link count -- this isn't required 1014 * because we do a similar check below. 1015 */ 1016 if (doingdirectory && parentdifferent) { 1017 dp->i_nlink++; 1018 dp->i_flag |= ICHG; 1019 iupdat(dp, &time, &time, 1); 1020 } 1021 error = direnter(ip); 1022 if (error) 1023 goto out; 1024 } else { 1025 if (xp->i_dev != dp->i_dev || xp->i_dev != ip->i_dev) { 1026 error = EXDEV; 1027 goto bad; 1028 } 1029 /* 1030 * Short circuit rename(foo, foo). 1031 */ 1032 if (xp->i_number == ip->i_number) 1033 goto bad; 1034 /* 1035 * Target must be empty if a directory 1036 * and have no links to it. 1037 * Also, insure source and target are 1038 * compatible (both directories, or both 1039 * not directories). 1040 */ 1041 if ((xp->i_mode&IFMT) == IFDIR) { 1042 if (!dirempty(xp) || xp->i_nlink > 2) { 1043 error = ENOTEMPTY; 1044 goto bad; 1045 } 1046 if (!doingdirectory) { 1047 error = ENOTDIR; 1048 goto bad; 1049 } 1050 } else if (doingdirectory) { 1051 error = EISDIR; 1052 goto bad; 1053 } 1054 dirrewrite(dp, ip); 1055 if (u.u_error) { 1056 error = u.u_error; 1057 goto bad1; 1058 } 1059 /* 1060 * Adjust the link count of the target to 1061 * reflect the dirrewrite above. If this is 1062 * a directory it is empty and there are 1063 * no links to it, so we can squash the inode and 1064 * any space associated with it. We disallowed 1065 * renaming over top of a directory with links to 1066 * it above, as we've no way to determine if 1067 * we've got a link or the directory itself, and 1068 * if we get a link, then ".." will be screwed up. 1069 */ 1070 xp->i_nlink--; 1071 if (doingdirectory) { 1072 if (--xp->i_nlink != 0) 1073 panic("rename: linked directory"); 1074 itrunc(xp, (u_long)0); 1075 } 1076 xp->i_flag |= ICHG; 1077 iput(xp); 1078 xp = NULL; 1079 } 1080 1081 /* 1082 * 3) Unlink the source. 1083 */ 1084 u.u_dirp = uap->from; 1085 dp = namei(uchar, DELETE, 0); 1086 /* 1087 * Insure directory entry still exists and 1088 * has not changed since the start of all 1089 * this. If either has occured, forget about 1090 * about deleting the original entry and just 1091 * adjust the link count in the inode. 1092 */ 1093 if (dp == NULL || u.u_dent.d_ino != ip->i_number) { 1094 ip->i_nlink--; 1095 ip->i_flag |= ICHG; 1096 } else { 1097 /* 1098 * If source is a directory, must adjust 1099 * link count of parent directory also. 1100 * If target didn't exist and source and 1101 * target have the same parent, then we 1102 * needn't touch the link count, it all 1103 * balances out in the end. Otherwise, we 1104 * must do so to reflect deletion of ".." 1105 * done above. 1106 */ 1107 if (doingdirectory && (xp != NULL || parentdifferent)) { 1108 dp->i_nlink--; 1109 dp->i_flag |= ICHG; 1110 } 1111 if (dirremove()) { 1112 ip->i_nlink--; 1113 ip->i_flag |= ICHG; 1114 } 1115 if (error == 0) /* conservative */ 1116 error = u.u_error; 1117 } 1118 irele(ip); 1119 if (dp) 1120 iput(dp); 1121 1122 /* 1123 * 4) Renaming a directory with the parent 1124 * different requires ".." to be rewritten. 1125 * The window is still there for ".." to 1126 * be inconsistent, but this is unavoidable, 1127 * and a lot shorter than when it was done 1128 * in a user process. 1129 */ 1130 if (doingdirectory && parentdifferent && error == 0) { 1131 struct dirtemplate dirbuf; 1132 1133 u.u_dirp = uap->to; 1134 ip = namei(uchar, LOOKUP | LOCKPARENT, 0); 1135 if (ip == NULL) { 1136 printf("rename: .. went away\n"); 1137 return; 1138 } 1139 dp = u.u_pdir; 1140 if ((ip->i_mode&IFMT) != IFDIR) { 1141 printf("rename: .. not a directory\n"); 1142 goto stuck; 1143 } 1144 error = rdwri(UIO_READ, ip, (caddr_t)&dirbuf, 1145 sizeof (struct dirtemplate), (off_t)0, 1, (int *)0); 1146 if (error == 0) { 1147 dirbuf.dotdot_ino = dp->i_number; 1148 (void) rdwri(UIO_WRITE, ip, (caddr_t)&dirbuf, 1149 sizeof (struct dirtemplate), (off_t)0, 1, (int *)0); 1150 } 1151 stuck: 1152 irele(dp); 1153 iput(ip); 1154 } 1155 goto done; 1156 1157 bad: 1158 iput(dp); 1159 bad1: 1160 if (xp) 1161 iput(xp); 1162 out: 1163 ip->i_nlink--; 1164 ip->i_flag |= ICHG; 1165 irele(ip); 1166 done: 1167 if (error) 1168 u.u_error = error; 1169 } 1170 1171 /* 1172 * Make a new file. 1173 */ 1174 struct inode * 1175 maknode(mode) 1176 int mode; 1177 { 1178 register struct inode *ip; 1179 ino_t ipref; 1180 1181 if ((mode & IFMT) == IFDIR) 1182 ipref = dirpref(u.u_pdir->i_fs); 1183 else 1184 ipref = u.u_pdir->i_number; 1185 ip = ialloc(u.u_pdir, ipref, mode); 1186 if (ip == NULL) { 1187 iput(u.u_pdir); 1188 return (NULL); 1189 } 1190 #ifdef QUOTA 1191 if (ip->i_dquot != NODQUOT) 1192 panic("maknode: dquot"); 1193 #endif 1194 ip->i_flag |= IACC|IUPD|ICHG; 1195 if ((mode & IFMT) == 0) 1196 mode |= IFREG; 1197 ip->i_mode = mode & ~u.u_cmask; 1198 ip->i_nlink = 1; 1199 ip->i_uid = u.u_uid; 1200 ip->i_gid = u.u_pdir->i_gid; 1201 if (ip->i_mode & ISGID && !groupmember(ip->i_gid)) 1202 ip->i_mode &= ~ISGID; 1203 #ifdef QUOTA 1204 ip->i_dquot = inoquota(ip); 1205 #endif 1206 1207 /* 1208 * Make sure inode goes to disk before directory entry. 1209 */ 1210 iupdat(ip, &time, &time, 1); 1211 u.u_error = direnter(ip); 1212 if (u.u_error) { 1213 /* 1214 * Write error occurred trying to update directory 1215 * so must deallocate the inode. 1216 */ 1217 ip->i_nlink = 0; 1218 ip->i_flag |= ICHG; 1219 iput(ip); 1220 return (NULL); 1221 } 1222 return (ip); 1223 } 1224