1 /* lfs_vnops.c 4.45 82/12/24 */ 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 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 if (copyout((caddr_t)&ds, (caddr_t)ub, sizeof(ds)) < 0) 513 u.u_error = EFAULT; 514 } 515 516 /* 517 * Return target name of a symbolic link 518 */ 519 readlink() 520 { 521 register struct inode *ip; 522 register struct a { 523 char *name; 524 char *buf; 525 int count; 526 } *uap = (struct a *)u.u_ap; 527 int resid; 528 529 ip = namei(uchar, LOOKUP, 0); 530 if (ip == NULL) 531 return; 532 if ((ip->i_mode&IFMT) != IFLNK) { 533 u.u_error = ENXIO; 534 goto out; 535 } 536 u.u_error = rdwri(UIO_READ, ip, uap->buf, uap->count, 0, 0, &resid); 537 out: 538 iput(ip); 539 u.u_r.r_val1 = uap->count - resid; 540 } 541 542 /* 543 * Change mode of a file given path name. 544 */ 545 chmod() 546 { 547 struct inode *ip; 548 struct a { 549 char *fname; 550 int fmode; 551 } *uap; 552 553 uap = (struct a *)u.u_ap; 554 if ((ip = owner(1)) == NULL) 555 return; 556 chmod1(ip, uap->fmode); 557 iput(ip); 558 } 559 560 /* 561 * Change mode of a file given a file descriptor. 562 */ 563 fchmod() 564 { 565 struct a { 566 int fd; 567 int fmode; 568 } *uap; 569 register struct inode *ip; 570 register struct file *fp; 571 572 uap = (struct a *)u.u_ap; 573 fp = getf(uap->fd); 574 if (fp == NULL) 575 return; 576 if (fp->f_type == DTYPE_SOCKET) { 577 u.u_error = EINVAL; 578 return; 579 } 580 ip = fp->f_inode; 581 if (u.u_uid != ip->i_uid && !suser()) 582 return; 583 ilock(ip); 584 chmod1(ip, uap->fmode); 585 iunlock(ip); 586 } 587 588 /* 589 * Change the mode on a file. 590 * Inode must be locked before calling. 591 */ 592 chmod1(ip, mode) 593 register struct inode *ip; 594 register int mode; 595 { 596 register int *gp; 597 598 ip->i_mode &= ~07777; 599 if (u.u_uid) { 600 mode &= ~ISVTX; 601 for (gp = u.u_groups; gp < &u.u_groups[NGROUPS]; gp++) 602 if (*gp == ip->i_gid) 603 goto ok; 604 mode &= ~ISGID; 605 ok: 606 ; 607 #ifdef MUSH 608 if (u.u_quota->q_syflags & QF_UMASK && u.u_uid != 0 && 609 (ip->i_mode & IFMT) != IFCHR) 610 mode &= ~u.u_cmask; 611 #endif 612 } 613 ip->i_mode |= mode&07777; 614 ip->i_flag |= ICHG; 615 if (ip->i_flag&ITEXT && (ip->i_mode&ISVTX)==0) 616 xrele(ip); 617 } 618 619 /* 620 * Set ownership given a path name. 621 */ 622 chown() 623 { 624 struct inode *ip; 625 struct a { 626 char *fname; 627 int uid; 628 int gid; 629 } *uap; 630 631 uap = (struct a *)u.u_ap; 632 if (!suser() || (ip = owner(0)) == NULL) 633 return; 634 chown1(ip, uap->uid, uap->gid); 635 iput(ip); 636 } 637 638 /* 639 * Set ownership given a file descriptor. 640 */ 641 fchown() 642 { 643 struct a { 644 int fd; 645 int uid; 646 int gid; 647 } *uap; 648 register struct inode *ip; 649 register struct file *fp; 650 651 uap = (struct a *)u.u_ap; 652 fp = getf(uap->fd); 653 if (fp == NULL) 654 return; 655 if (fp->f_type == DTYPE_SOCKET) { 656 u.u_error = EINVAL; 657 return; 658 } 659 ip = fp->f_inode; 660 if (!suser()) 661 return; 662 ilock(ip); 663 chown1(ip, uap->uid, uap->gid); 664 iunlock(ip); 665 } 666 667 /* 668 * Perform chown operation on inode ip; 669 * inode must be locked prior to call. 670 */ 671 chown1(ip, uid, gid) 672 register struct inode *ip; 673 int uid, gid; 674 { 675 #ifdef QUOTA 676 register long change; 677 678 /* 679 * This doesn't allow for holes in files (which hopefully don't 680 * happen often in files that we chown), and is not accurate anyway 681 * (eg: it totally ignores 3 level indir blk files - but hopefully 682 * noone who can make a file that big will have a quota) 683 */ 684 if (ip->i_uid == uid) 685 change = 0; 686 else { 687 register struct fs *fs = ip->i_fs; 688 689 if (ip->i_size > (change = NDADDR * fs->fs_bsize)) { 690 register off_t size; 691 692 size = blkroundup(fs, ip->i_size) - change; 693 change += size; 694 change += fs->fs_bsize; 695 /* this assumes NIADDR <= 2 */ 696 if (size > NINDIR(fs) * fs->fs_bsize) 697 change += fs->fs_bsize; 698 } else 699 change = fragroundup(fs, ip->i_size); 700 change /= DEV_BSIZE; 701 } 702 (void)chkdq(ip, -change, 1); 703 (void)chkiq(ip->i_dev, ip, ip->i_uid, 1); 704 dqrele(ip->i_dquot); 705 #endif 706 /* 707 * keep uid/gid's in sane range -- no err, 708 * so chown(file, uid, -1) will do something useful 709 */ 710 if (uid >= 0 && uid <= 32767) /* should have a constant */ 711 ip->i_uid = uid; 712 if (gid >= 0 && gid <= 32767) /* same here */ 713 ip->i_gid = gid; 714 ip->i_flag |= ICHG; 715 if (u.u_ruid != 0) 716 ip->i_mode &= ~(ISUID|ISGID); 717 #ifdef QUOTA 718 ip->i_dquot = inoquota(ip); 719 (void)chkdq(ip, change, 1); 720 (void)chkiq(ip->i_dev, (struct inode *)NULL, uid, 1); 721 #endif 722 } 723 724 /* 725 * Set IUPD and IACC times on file. 726 * Can't set ICHG. 727 */ 728 outime() 729 { 730 register struct a { 731 char *fname; 732 time_t *tptr; 733 } *uap; 734 register struct inode *ip; 735 time_t tv[2]; 736 struct timeval tv0, tv1; 737 738 uap = (struct a *)u.u_ap; 739 if ((ip = owner(1)) == NULL) 740 return; 741 if (copyin((caddr_t)uap->tptr, (caddr_t)tv, sizeof(tv))) { 742 u.u_error = EFAULT; 743 } else { 744 ip->i_flag |= IACC|IUPD|ICHG; 745 tv0.tv_sec = tv[0]; tv0.tv_usec = 0; 746 tv1.tv_sec = tv[1]; tv1.tv_usec = 0; 747 iupdat(ip, &tv0, &tv1, 0); 748 } 749 iput(ip); 750 } 751 752 /* 753 * Flush any pending I/O. 754 */ 755 sync() 756 { 757 758 update(); 759 } 760 761 /* 762 * Apply an advisory lock on a file descriptor. 763 */ 764 flock() 765 { 766 struct a { 767 int fd; 768 int how; 769 } *uap; 770 register struct file *fp; 771 register int cmd, flags; 772 773 uap = (struct a *)u.u_ap; 774 fp = getf(uap->fd); 775 if (fp == NULL) 776 return; 777 if (fp->f_type == DTYPE_SOCKET) { /* XXX */ 778 u.u_error = EINVAL; 779 return; 780 } 781 cmd = uap->how; 782 flags = u.u_pofile[uap->fd] & (UF_SHLOCK|UF_EXLOCK); 783 if (cmd&FUNLOCK) { 784 if (flags == 0) { 785 u.u_error = EINVAL; 786 return; 787 } 788 funlocki(fp->f_inode, flags); 789 u.u_pofile[uap->fd] &= ~(UF_SHLOCK|UF_EXLOCK); 790 return; 791 } 792 /* 793 * No reason to write lock a file we've already 794 * write locked, similarly with a read lock. 795 */ 796 if ((flags&UF_EXLOCK) && (cmd&FEXLOCK) || 797 (flags&UF_SHLOCK) && (cmd&FSHLOCK)) 798 return; 799 u.u_pofile[uap->fd] = flocki(fp->f_inode, u.u_pofile[uap->fd], cmd); 800 } 801 802 /* 803 * Truncate a file given its path name. 804 */ 805 truncate() 806 { 807 struct a { 808 char *fname; 809 u_long length; 810 } *uap = (struct a *)u.u_ap; 811 struct inode *ip; 812 813 ip = namei(uchar, LOOKUP, 1); 814 if (ip == NULL) 815 return; 816 if (access(ip, IWRITE)) 817 goto bad; 818 if ((ip->i_mode&IFMT) == IFDIR) { 819 u.u_error = EISDIR; 820 goto bad; 821 } 822 itrunc(ip, uap->length); 823 bad: 824 iput(ip); 825 } 826 827 /* 828 * Truncate a file given a file descriptor. 829 */ 830 ftruncate() 831 { 832 struct a { 833 int fd; 834 u_long length; 835 } *uap = (struct a *)u.u_ap; 836 struct inode *ip; 837 struct file *fp; 838 839 fp = getf(uap->fd); 840 if (fp == NULL) 841 return; 842 if (fp->f_type == DTYPE_SOCKET) { 843 u.u_error = EINVAL; 844 return; 845 } 846 if ((fp->f_flag&FWRITE) == 0) { 847 u.u_error = EINVAL; 848 return; 849 } 850 ip = fp->f_inode; 851 ilock(ip); 852 itrunc(ip, uap->length); 853 iunlock(ip); 854 } 855 856 /* 857 * Synch an open file. 858 */ 859 fsync() 860 { 861 struct a { 862 int fd; 863 } *uap = (struct a *)u.u_ap; 864 struct inode *ip; 865 struct file *fp; 866 867 fp = getf(uap->fd); 868 if (fp == NULL) 869 return; 870 if (fp->f_type == DTYPE_SOCKET) { 871 u.u_error = EINVAL; 872 return; 873 } 874 ip = fp->f_inode; 875 ilock(ip); 876 syncip(ip); 877 iunlock(ip); 878 } 879 880 /* 881 * Rename system call. 882 * rename("foo", "bar"); 883 * is essentially 884 * unlink("bar"); 885 * link("foo", "bar"); 886 * unlink("foo"); 887 * but ``atomically''. Can't do full commit without saving state in the 888 * inode on disk which isn't feasible at this time. Best we can do is 889 * always guarantee the target exists. 890 * 891 * Basic algorithm is: 892 * 893 * 1) Bump link count on source while we're linking it to the 894 * target. This also insure the inode won't be deleted out 895 * from underneath us while we work. 896 * 2) Link source to destination. If destination already exists, 897 * delete it first. 898 * 3) Unlink source reference to inode if still around. 899 * 4) If a directory was moved and the parent of the destination 900 * is different from the source, patch the ".." entry in the 901 * directory. 902 * 903 * Source and destination must either both be directories, or both 904 * not be directories. If target is a directory, it must be empty. 905 */ 906 rename() 907 { 908 struct a { 909 char *from; 910 char *to; 911 } *uap; 912 register struct inode *ip, *xp, *dp; 913 int oldparent, parentdifferent, doingdirectory; 914 915 uap = (struct a *)u.u_ap; 916 ip = namei(uchar, LOOKUP | LOCKPARENT, 0); 917 if (ip == NULL) 918 return; 919 dp = u.u_pdir; 920 oldparent = 0, doingdirectory = 0; 921 if ((ip->i_mode&IFMT) == IFDIR) { 922 register struct direct *d; 923 924 d = &u.u_dent; 925 /* 926 * Avoid "." and ".." for obvious reasons. 927 */ 928 if (d->d_name[0] == '.') { 929 if (d->d_namlen == 1 || 930 (d->d_namlen == 2 && d->d_name[1] == '.')) { 931 u.u_error = EINVAL; 932 iput(ip); 933 return; 934 } 935 } 936 oldparent = dp->i_number; 937 doingdirectory++; 938 } 939 irele(dp); 940 941 /* 942 * 1) Bump link count while we're moving stuff 943 * around. If we crash somewhere before 944 * completing our work, the link count 945 * may be wrong, but correctable. 946 */ 947 ip->i_nlink++; 948 ip->i_flag |= ICHG; 949 iupdat(ip, &time, &time, 1); 950 iunlock(ip); 951 952 /* 953 * When the target exists, both the directory 954 * and target inodes are returned locked. 955 */ 956 u.u_dirp = (caddr_t)uap->to; 957 xp = namei(uchar, CREATE | LOCKPARENT, 0); 958 if (u.u_error) 959 goto out; 960 dp = u.u_pdir; 961 /* 962 * 2) If target doesn't exist, link the target 963 * to the source and unlink the source. 964 * Otherwise, rewrite the target directory 965 * entry to reference the source inode and 966 * expunge the original entry's existence. 967 */ 968 parentdifferent = oldparent != dp->i_number; 969 if (xp == NULL) { 970 if (dp->i_dev != ip->i_dev) { 971 u.u_error = EXDEV; 972 goto bad; 973 } 974 /* 975 * Account for ".." in directory. 976 * When source and destination have the 977 * same parent we don't fool with the 978 * link count -- this isn't required 979 * because we do a similar check below. 980 */ 981 if (doingdirectory && parentdifferent) { 982 dp->i_nlink++; 983 dp->i_flag |= ICHG; 984 iupdat(dp, &time, &time, 1); 985 } 986 direnter(ip); 987 if (u.u_error) 988 goto out; 989 } else { 990 if (xp->i_dev != dp->i_dev || xp->i_dev != ip->i_dev) { 991 u.u_error = EXDEV; 992 goto bad; 993 } 994 /* 995 * Target must be empty if a directory. 996 * Also, insure source and target are 997 * compatible (both directories, or both 998 * not directories). 999 */ 1000 if ((xp->i_mode&IFMT) == IFDIR) { 1001 if (!dirempty(xp)) { 1002 u.u_error = ENOTEMPTY; 1003 goto bad; 1004 } 1005 if (!doingdirectory) { 1006 u.u_error = ENOTDIR; 1007 goto bad; 1008 } 1009 } else if (doingdirectory) { 1010 u.u_error = EISDIR; 1011 goto bad; 1012 } 1013 dirrewrite(dp, ip); 1014 if (u.u_error) 1015 goto bad1; 1016 /* 1017 * If this is a directory we know it is 1018 * empty and we can squash the inode and 1019 * any space associated with it. Otherwise, 1020 * we've got a plain file and the link count 1021 * simply needs to be adjusted. 1022 */ 1023 if (doingdirectory) { 1024 xp->i_nlink = 0; 1025 itrunc(xp, (u_long)0); 1026 } else 1027 xp->i_nlink--; 1028 xp->i_flag |= ICHG; 1029 iput(xp); 1030 } 1031 1032 /* 1033 * 3) Unlink the source. 1034 */ 1035 u.u_dirp = uap->from; 1036 dp = namei(uchar, DELETE, 0); 1037 /* 1038 * Insure directory entry still exists and 1039 * has not changed since the start of all 1040 * this. If either has occured, forget about 1041 * about deleting the original entry and just 1042 * adjust the link count in the inode. 1043 */ 1044 if (dp == NULL || u.u_dent.d_ino != ip->i_number) { 1045 ip->i_nlink--; 1046 ip->i_flag |= ICHG; 1047 } else { 1048 /* 1049 * If source is a directory, must adjust 1050 * link count of parent directory also. 1051 * If target didn't exist and source and 1052 * target have the same parent, then we 1053 * needn't touch the link count, it all 1054 * balances out in the end. Otherwise, we 1055 * must do so to reflect deletion of ".." 1056 * done above. 1057 */ 1058 if (doingdirectory && (xp != NULL || parentdifferent)) { 1059 dp->i_nlink--; 1060 dp->i_flag |= ICHG; 1061 } 1062 if (dirremove()) { 1063 ip->i_nlink--; 1064 ip->i_flag |= ICHG; 1065 } 1066 } 1067 irele(ip); 1068 if (dp) 1069 iput(dp); 1070 1071 /* 1072 * 4) Renaming a directory with the parent 1073 * different requires ".." to be rewritten. 1074 * The window is still there for ".." to 1075 * be inconsistent, but this is unavoidable, 1076 * and a lot shorter than when it was done 1077 * in a user process. 1078 */ 1079 if (doingdirectory && parentdifferent && u.u_error == 0) { 1080 struct dirtemplate dirbuf; 1081 1082 u.u_dirp = uap->to; 1083 ip = namei(uchar, LOOKUP | LOCKPARENT, 0); 1084 if (ip == NULL) { 1085 printf("rename: .. went away\n"); 1086 return; 1087 } 1088 dp = u.u_pdir; 1089 if ((ip->i_mode&IFMT) != IFDIR) { 1090 printf("rename: .. not a directory\n"); 1091 goto stuck; 1092 } 1093 u.u_error = rdwri(UIO_READ, ip, (caddr_t)&dirbuf, 1094 sizeof (struct dirtemplate), (off_t)0, 1, (int *)0); 1095 if (u.u_error == 0) { 1096 dirbuf.dotdot_ino = dp->i_number; 1097 (void) rdwri(UIO_WRITE, ip, (caddr_t)&dirbuf, 1098 sizeof (struct dirtemplate), (off_t)0, 1, (int *)0); 1099 } 1100 stuck: 1101 irele(dp); 1102 iput(ip); 1103 } 1104 return; 1105 bad: 1106 iput(u.u_pdir); 1107 bad1: 1108 if (xp) 1109 irele(xp); 1110 out: 1111 ip->i_nlink--; 1112 ip->i_flag |= ICHG; 1113 irele(ip); 1114 } 1115 1116 /* 1117 * Make a new file. 1118 */ 1119 struct inode * 1120 maknode(mode) 1121 int mode; 1122 { 1123 register struct inode *ip; 1124 ino_t ipref; 1125 1126 if ((mode & IFMT) == IFDIR) 1127 ipref = dirpref(u.u_pdir->i_fs); 1128 else 1129 ipref = u.u_pdir->i_number; 1130 ip = ialloc(u.u_pdir, ipref, mode); 1131 if (ip == NULL) { 1132 iput(u.u_pdir); 1133 return (NULL); 1134 } 1135 #ifdef QUOTA 1136 if (ip->i_dquot != NODQUOT) 1137 panic("maknode: dquot"); 1138 #endif 1139 ip->i_flag |= IACC|IUPD|ICHG; 1140 if ((mode & IFMT) == 0) 1141 mode |= IFREG; 1142 ip->i_mode = mode & ~u.u_cmask; 1143 ip->i_nlink = 1; 1144 ip->i_uid = u.u_uid; 1145 ip->i_gid = u.u_pdir->i_gid; 1146 #ifdef QUOTA 1147 ip->i_dquot = inoquota(ip); 1148 #endif 1149 1150 /* 1151 * Make sure inode goes to disk before directory entry. 1152 */ 1153 iupdat(ip, &time, &time, 1); 1154 direnter(ip); 1155 if (u.u_error) { 1156 /* 1157 * write error occurred trying to update directory 1158 * so must deallocate the inode 1159 */ 1160 ip->i_nlink = 0; 1161 ip->i_flag |= ICHG; 1162 iput(ip); 1163 return (NULL); 1164 } 1165 return (ip); 1166 } 1167