123405Smckusick /* 223405Smckusick * Copyright (c) 1982 Regents of the University of California. 323405Smckusick * All rights reserved. The Berkeley software License Agreement 423405Smckusick * specifies the terms and conditions for redistribution. 523405Smckusick * 6*26361Skarels * @(#)vfs_syscalls.c 6.22 (Berkeley) 02/23/86 723405Smckusick */ 837Sbill 917101Sbloom #include "param.h" 1017101Sbloom #include "systm.h" 1117101Sbloom #include "dir.h" 1217101Sbloom #include "user.h" 1317101Sbloom #include "kernel.h" 1417101Sbloom #include "file.h" 1517101Sbloom #include "stat.h" 1617101Sbloom #include "inode.h" 1717101Sbloom #include "fs.h" 1817101Sbloom #include "buf.h" 1917101Sbloom #include "proc.h" 2017101Sbloom #include "quota.h" 2117101Sbloom #include "uio.h" 2217101Sbloom #include "socket.h" 2317101Sbloom #include "socketvar.h" 2417101Sbloom #include "mount.h" 2537Sbill 2612756Ssam extern struct fileops inodeops; 2712756Ssam struct file *getinode(); 2812756Ssam 299167Ssam /* 309167Ssam * Change current working directory (``.''). 319167Ssam */ 326254Sroot chdir() 336254Sroot { 346254Sroot 356254Sroot chdirec(&u.u_cdir); 366254Sroot } 376254Sroot 389167Ssam /* 399167Ssam * Change notion of root (``/'') directory. 409167Ssam */ 416254Sroot chroot() 426254Sroot { 436254Sroot 446254Sroot if (suser()) 456254Sroot chdirec(&u.u_rdir); 466254Sroot } 476254Sroot 489167Ssam /* 499167Ssam * Common routine for chroot and chdir. 509167Ssam */ 516254Sroot chdirec(ipp) 527701Ssam register struct inode **ipp; 536254Sroot { 546254Sroot register struct inode *ip; 556254Sroot struct a { 566254Sroot char *fname; 5716694Smckusick } *uap = (struct a *)u.u_ap; 5816694Smckusick register struct nameidata *ndp = &u.u_nd; 596254Sroot 6016694Smckusick ndp->ni_nameiop = LOOKUP | FOLLOW; 6116694Smckusick ndp->ni_segflg = UIO_USERSPACE; 6216694Smckusick ndp->ni_dirp = uap->fname; 6316694Smckusick ip = namei(ndp); 649167Ssam if (ip == NULL) 656254Sroot return; 669167Ssam if ((ip->i_mode&IFMT) != IFDIR) { 676254Sroot u.u_error = ENOTDIR; 686254Sroot goto bad; 696254Sroot } 709167Ssam if (access(ip, IEXEC)) 716254Sroot goto bad; 7216664Smckusick IUNLOCK(ip); 737142Smckusick if (*ipp) 747142Smckusick irele(*ipp); 756254Sroot *ipp = ip; 766254Sroot return; 776254Sroot 786254Sroot bad: 796254Sroot iput(ip); 806254Sroot } 816254Sroot 8237Sbill /* 836254Sroot * Open system call. 846254Sroot */ 856254Sroot open() 866254Sroot { 8712756Ssam struct a { 886254Sroot char *fname; 897701Ssam int mode; 9012756Ssam int crtmode; 9112756Ssam } *uap = (struct a *) u.u_ap; 926254Sroot 9316694Smckusick copen(uap->mode-FOPEN, uap->crtmode, uap->fname); 946254Sroot } 956254Sroot 966254Sroot /* 976254Sroot * Creat system call. 986254Sroot */ 9912756Ssam creat() 1006254Sroot { 10112756Ssam struct a { 1026254Sroot char *fname; 1036254Sroot int fmode; 10412756Ssam } *uap = (struct a *)u.u_ap; 1056254Sroot 10616694Smckusick copen(FWRITE|FCREAT|FTRUNC, uap->fmode, uap->fname); 1076254Sroot } 1086254Sroot 1096254Sroot /* 1106254Sroot * Common code for open and creat. 11112756Ssam * Check permissions, allocate an open file structure, 11212756Ssam * and call the device open routine if any. 1136254Sroot */ 11416694Smckusick copen(mode, arg, fname) 11512756Ssam register int mode; 11612756Ssam int arg; 11716694Smckusick caddr_t fname; 11812756Ssam { 1196254Sroot register struct inode *ip; 1206254Sroot register struct file *fp; 12116694Smckusick register struct nameidata *ndp = &u.u_nd; 12224543Smckusick int indx; 1236254Sroot 12424543Smckusick fp = falloc(); 12524543Smckusick if (fp == NULL) 12612756Ssam return; 12724543Smckusick indx = u.u_r.r_val1; 12816694Smckusick ndp->ni_segflg = UIO_USERSPACE; 12916694Smckusick ndp->ni_dirp = fname; 13012756Ssam if (mode&FCREAT) { 13118416Smckusick if (mode & FEXCL) 13218416Smckusick ndp->ni_nameiop = CREATE; 13318416Smckusick else 13418416Smckusick ndp->ni_nameiop = CREATE | FOLLOW; 13516694Smckusick ip = namei(ndp); 13612756Ssam if (ip == NULL) { 13712756Ssam if (u.u_error) 13824543Smckusick goto bad1; 13916694Smckusick ip = maknode(arg&07777&(~ISVTX), ndp); 14012756Ssam if (ip == NULL) 14124543Smckusick goto bad1; 14212756Ssam mode &= ~FTRUNC; 14312756Ssam } else { 14412756Ssam if (mode&FEXCL) { 14512756Ssam u.u_error = EEXIST; 14624543Smckusick goto bad; 14712756Ssam } 14812756Ssam mode &= ~FCREAT; 14912756Ssam } 15012756Ssam } else { 15116694Smckusick ndp->ni_nameiop = LOOKUP | FOLLOW; 15216694Smckusick ip = namei(ndp); 15312756Ssam if (ip == NULL) 15424543Smckusick goto bad1; 15512756Ssam } 15612756Ssam if ((ip->i_mode & IFMT) == IFSOCK) { 15712756Ssam u.u_error = EOPNOTSUPP; 15812756Ssam goto bad; 15912756Ssam } 16012756Ssam if ((mode&FCREAT) == 0) { 1616254Sroot if (mode&FREAD) 1627701Ssam if (access(ip, IREAD)) 1637701Ssam goto bad; 16416032Skarels if (mode&(FWRITE|FTRUNC)) { 1657701Ssam if (access(ip, IWRITE)) 1667701Ssam goto bad; 1677701Ssam if ((ip->i_mode&IFMT) == IFDIR) { 1686254Sroot u.u_error = EISDIR; 1697701Ssam goto bad; 1707701Ssam } 1716254Sroot } 1726254Sroot } 17312756Ssam if (mode&FTRUNC) 1749167Ssam itrunc(ip, (u_long)0); 17516664Smckusick IUNLOCK(ip); 17612756Ssam fp->f_flag = mode&FMASK; 17712756Ssam fp->f_type = DTYPE_INODE; 17812756Ssam fp->f_ops = &inodeops; 17912756Ssam fp->f_data = (caddr_t)ip; 18012756Ssam if (setjmp(&u.u_qsave)) { 18112756Ssam if (u.u_error == 0) 18212756Ssam u.u_error = EINTR; 18324543Smckusick u.u_ofile[indx] = NULL; 18412756Ssam closef(fp); 18512756Ssam return; 18612756Ssam } 1878559Sroot u.u_error = openi(ip, mode); 18812756Ssam if (u.u_error == 0) 1896254Sroot return; 19024543Smckusick ILOCK(ip); 1917701Ssam bad: 1927701Ssam iput(ip); 19324543Smckusick bad1: 19424543Smckusick u.u_ofile[indx] = NULL; 19524543Smckusick fp->f_count--; 1966254Sroot } 1976254Sroot 1986254Sroot /* 1996254Sroot * Mknod system call 2006254Sroot */ 2016254Sroot mknod() 2026254Sroot { 2036254Sroot register struct inode *ip; 2046254Sroot register struct a { 2056254Sroot char *fname; 2066254Sroot int fmode; 2076254Sroot int dev; 20816694Smckusick } *uap = (struct a *)u.u_ap; 20916694Smckusick register struct nameidata *ndp = &u.u_nd; 2106254Sroot 21112756Ssam if (!suser()) 21212756Ssam return; 21316694Smckusick ndp->ni_nameiop = CREATE; 21416694Smckusick ndp->ni_segflg = UIO_USERSPACE; 21516694Smckusick ndp->ni_dirp = uap->fname; 21616694Smckusick ip = namei(ndp); 21712756Ssam if (ip != NULL) { 21812756Ssam u.u_error = EEXIST; 21912756Ssam goto out; 2206254Sroot } 2216254Sroot if (u.u_error) 2226254Sroot return; 22316694Smckusick ip = maknode(uap->fmode, ndp); 2246254Sroot if (ip == NULL) 2256254Sroot return; 22612756Ssam switch (ip->i_mode & IFMT) { 22712756Ssam 22815093Smckusick case IFMT: /* used by badsect to flag bad sectors */ 22912756Ssam case IFCHR: 23012756Ssam case IFBLK: 23112756Ssam if (uap->dev) { 23212756Ssam /* 23312756Ssam * Want to be able to use this to make badblock 23412756Ssam * inodes, so don't truncate the dev number. 23512756Ssam */ 23612756Ssam ip->i_rdev = uap->dev; 23712756Ssam ip->i_flag |= IACC|IUPD|ICHG; 23812756Ssam } 2396254Sroot } 2406254Sroot 2416254Sroot out: 2426254Sroot iput(ip); 2436254Sroot } 2446254Sroot 2456254Sroot /* 2466254Sroot * link system call 2476254Sroot */ 2486254Sroot link() 2496254Sroot { 2506254Sroot register struct inode *ip, *xp; 2516254Sroot register struct a { 2526254Sroot char *target; 2536254Sroot char *linkname; 25416694Smckusick } *uap = (struct a *)u.u_ap; 25516694Smckusick register struct nameidata *ndp = &u.u_nd; 2566254Sroot 25716694Smckusick ndp->ni_nameiop = LOOKUP | FOLLOW; 25816694Smckusick ndp->ni_segflg = UIO_USERSPACE; 25916694Smckusick ndp->ni_dirp = uap->target; 26016694Smckusick ip = namei(ndp); /* well, this routine is doomed anyhow */ 2616254Sroot if (ip == NULL) 2626254Sroot return; 2639167Ssam if ((ip->i_mode&IFMT) == IFDIR && !suser()) { 2647439Sroot iput(ip); 2657439Sroot return; 2667439Sroot } 2676254Sroot ip->i_nlink++; 2686254Sroot ip->i_flag |= ICHG; 2698673Sroot iupdat(ip, &time, &time, 1); 27016664Smckusick IUNLOCK(ip); 27116694Smckusick ndp->ni_nameiop = CREATE; 27216694Smckusick ndp->ni_segflg = UIO_USERSPACE; 27316694Smckusick ndp->ni_dirp = (caddr_t)uap->linkname; 27416694Smckusick xp = namei(ndp); 2756254Sroot if (xp != NULL) { 2766254Sroot u.u_error = EEXIST; 2776254Sroot iput(xp); 2786254Sroot goto out; 2796254Sroot } 2806254Sroot if (u.u_error) 2816254Sroot goto out; 28216694Smckusick if (ndp->ni_pdir->i_dev != ip->i_dev) { 28316694Smckusick iput(ndp->ni_pdir); 2846254Sroot u.u_error = EXDEV; 2856254Sroot goto out; 2866254Sroot } 28716694Smckusick u.u_error = direnter(ip, ndp); 2886254Sroot out: 2896254Sroot if (u.u_error) { 2906254Sroot ip->i_nlink--; 2916254Sroot ip->i_flag |= ICHG; 2926254Sroot } 2937142Smckusick irele(ip); 2946254Sroot } 2956254Sroot 2966254Sroot /* 2976254Sroot * symlink -- make a symbolic link 2986254Sroot */ 2996254Sroot symlink() 3006254Sroot { 3016254Sroot register struct a { 3026254Sroot char *target; 3036254Sroot char *linkname; 30416694Smckusick } *uap = (struct a *)u.u_ap; 3056254Sroot register struct inode *ip; 3066254Sroot register char *tp; 3076254Sroot register c, nc; 30816694Smckusick register struct nameidata *ndp = &u.u_nd; 3096254Sroot 3106254Sroot tp = uap->target; 3116254Sroot nc = 0; 3126254Sroot while (c = fubyte(tp)) { 3136254Sroot if (c < 0) { 3146254Sroot u.u_error = EFAULT; 3156254Sroot return; 3166254Sroot } 3176254Sroot tp++; 3186254Sroot nc++; 3196254Sroot } 32016694Smckusick ndp->ni_nameiop = CREATE; 32116694Smckusick ndp->ni_segflg = UIO_USERSPACE; 32216694Smckusick ndp->ni_dirp = uap->linkname; 32316694Smckusick ip = namei(ndp); 3246254Sroot if (ip) { 3256254Sroot iput(ip); 3266254Sroot u.u_error = EEXIST; 3276254Sroot return; 3286254Sroot } 3296254Sroot if (u.u_error) 3306254Sroot return; 33116694Smckusick ip = maknode(IFLNK | 0777, ndp); 3326254Sroot if (ip == NULL) 3336254Sroot return; 334*26361Skarels u.u_error = rdwri(UIO_WRITE, ip, uap->target, nc, (off_t)0, 0, 335*26361Skarels (int *)0); 3369167Ssam /* handle u.u_error != 0 */ 3376254Sroot iput(ip); 3386254Sroot } 3396254Sroot 3406254Sroot /* 3416254Sroot * Unlink system call. 3426254Sroot * Hard to avoid races here, especially 3436254Sroot * in unlinking directories. 3446254Sroot */ 3456254Sroot unlink() 3466254Sroot { 3476254Sroot struct a { 3486254Sroot char *fname; 34916694Smckusick } *uap = (struct a *)u.u_ap; 3509167Ssam register struct inode *ip, *dp; 35116694Smckusick register struct nameidata *ndp = &u.u_nd; 3526254Sroot 35316694Smckusick ndp->ni_nameiop = DELETE | LOCKPARENT; 35416694Smckusick ndp->ni_segflg = UIO_USERSPACE; 35516694Smckusick ndp->ni_dirp = uap->fname; 35616694Smckusick ip = namei(ndp); 3579167Ssam if (ip == NULL) 3586254Sroot return; 35916694Smckusick dp = ndp->ni_pdir; 3609167Ssam if ((ip->i_mode&IFMT) == IFDIR && !suser()) 3616254Sroot goto out; 3626254Sroot /* 3636254Sroot * Don't unlink a mounted file. 3646254Sroot */ 3659167Ssam if (ip->i_dev != dp->i_dev) { 3666254Sroot u.u_error = EBUSY; 3676254Sroot goto out; 3686254Sroot } 3696254Sroot if (ip->i_flag&ITEXT) 3706254Sroot xrele(ip); /* try once to free text */ 37116694Smckusick if (dirremove(ndp)) { 3727535Sroot ip->i_nlink--; 3737535Sroot ip->i_flag |= ICHG; 3746254Sroot } 3756254Sroot out: 3769167Ssam if (dp == ip) 3777142Smckusick irele(ip); 3787142Smckusick else 3797142Smckusick iput(ip); 3809167Ssam iput(dp); 3816254Sroot } 3826254Sroot 3836254Sroot /* 3846254Sroot * Seek system call 3856254Sroot */ 3868040Sroot lseek() 3876254Sroot { 3886254Sroot register struct file *fp; 3896254Sroot register struct a { 3907701Ssam int fd; 3916254Sroot off_t off; 3926254Sroot int sbase; 39316694Smckusick } *uap = (struct a *)u.u_ap; 3946254Sroot 39516540Ssam GETF(fp, uap->fd); 39616540Ssam if (fp->f_type != DTYPE_INODE) { 39716540Ssam u.u_error = ESPIPE; 3986254Sroot return; 39916540Ssam } 40013878Ssam switch (uap->sbase) { 40113878Ssam 40213878Ssam case L_INCR: 40313878Ssam fp->f_offset += uap->off; 40413878Ssam break; 40513878Ssam 40613878Ssam case L_XTND: 40713878Ssam fp->f_offset = uap->off + ((struct inode *)fp->f_data)->i_size; 40813878Ssam break; 40913878Ssam 41013878Ssam case L_SET: 41113878Ssam fp->f_offset = uap->off; 41213878Ssam break; 41313878Ssam 41413878Ssam default: 41513878Ssam u.u_error = EINVAL; 41613878Ssam return; 41713878Ssam } 41813878Ssam u.u_r.r_off = fp->f_offset; 4196254Sroot } 4206254Sroot 4216254Sroot /* 4226254Sroot * Access system call 4236254Sroot */ 4246254Sroot saccess() 4256254Sroot { 4266254Sroot register svuid, svgid; 4276254Sroot register struct inode *ip; 4286254Sroot register struct a { 4296254Sroot char *fname; 4306254Sroot int fmode; 43116694Smckusick } *uap = (struct a *)u.u_ap; 43216694Smckusick register struct nameidata *ndp = &u.u_nd; 4336254Sroot 4346254Sroot svuid = u.u_uid; 4356254Sroot svgid = u.u_gid; 4366254Sroot u.u_uid = u.u_ruid; 4376254Sroot u.u_gid = u.u_rgid; 43816694Smckusick ndp->ni_nameiop = LOOKUP | FOLLOW; 43916694Smckusick ndp->ni_segflg = UIO_USERSPACE; 44016694Smckusick ndp->ni_dirp = uap->fname; 44116694Smckusick ip = namei(ndp); 4426254Sroot if (ip != NULL) { 44312756Ssam if ((uap->fmode&R_OK) && access(ip, IREAD)) 4447701Ssam goto done; 44512756Ssam if ((uap->fmode&W_OK) && access(ip, IWRITE)) 4467701Ssam goto done; 44712756Ssam if ((uap->fmode&X_OK) && access(ip, IEXEC)) 4487701Ssam goto done; 4497701Ssam done: 4506254Sroot iput(ip); 4516254Sroot } 4526254Sroot u.u_uid = svuid; 4536254Sroot u.u_gid = svgid; 4546254Sroot } 4556254Sroot 4566254Sroot /* 4576574Smckusic * Stat system call. This version follows links. 45837Sbill */ 45937Sbill stat() 46037Sbill { 46137Sbill 46216694Smckusick stat1(FOLLOW); 46337Sbill } 46437Sbill 46537Sbill /* 4666574Smckusic * Lstat system call. This version does not follow links. 4675992Swnj */ 4685992Swnj lstat() 4695992Swnj { 47012756Ssam 47116694Smckusick stat1(NOFOLLOW); 47212756Ssam } 47312756Ssam 47412756Ssam stat1(follow) 47512756Ssam int follow; 47612756Ssam { 4775992Swnj register struct inode *ip; 4785992Swnj register struct a { 4795992Swnj char *fname; 48012756Ssam struct stat *ub; 48116694Smckusick } *uap = (struct a *)u.u_ap; 48212756Ssam struct stat sb; 48316694Smckusick register struct nameidata *ndp = &u.u_nd; 4845992Swnj 48516694Smckusick ndp->ni_nameiop = LOOKUP | follow; 48616694Smckusick ndp->ni_segflg = UIO_USERSPACE; 48716694Smckusick ndp->ni_dirp = uap->fname; 48816694Smckusick ip = namei(ndp); 4895992Swnj if (ip == NULL) 4905992Swnj return; 49113043Ssam (void) ino_stat(ip, &sb); 4925992Swnj iput(ip); 49312756Ssam u.u_error = copyout((caddr_t)&sb, (caddr_t)uap->ub, sizeof (sb)); 4945992Swnj } 4955992Swnj 4965992Swnj /* 4975992Swnj * Return target name of a symbolic link 49837Sbill */ 4995992Swnj readlink() 5005992Swnj { 5015992Swnj register struct inode *ip; 5025992Swnj register struct a { 5035992Swnj char *name; 5045992Swnj char *buf; 5055992Swnj int count; 5067826Sroot } *uap = (struct a *)u.u_ap; 50716694Smckusick register struct nameidata *ndp = &u.u_nd; 5087826Sroot int resid; 5095992Swnj 51016694Smckusick ndp->ni_nameiop = LOOKUP; 51116694Smckusick ndp->ni_segflg = UIO_USERSPACE; 51216694Smckusick ndp->ni_dirp = uap->name; 51316694Smckusick ip = namei(ndp); 5145992Swnj if (ip == NULL) 5155992Swnj return; 5165992Swnj if ((ip->i_mode&IFMT) != IFLNK) { 51721015Smckusick u.u_error = EINVAL; 5185992Swnj goto out; 5195992Swnj } 520*26361Skarels u.u_error = rdwri(UIO_READ, ip, uap->buf, uap->count, (off_t)0, 0, 521*26361Skarels &resid); 5225992Swnj out: 5235992Swnj iput(ip); 5247826Sroot u.u_r.r_val1 = uap->count - resid; 5255992Swnj } 5265992Swnj 5279167Ssam /* 5289167Ssam * Change mode of a file given path name. 5299167Ssam */ 5306254Sroot chmod() 5315992Swnj { 5327701Ssam struct inode *ip; 5337701Ssam struct a { 5346254Sroot char *fname; 5356254Sroot int fmode; 53616694Smckusick } *uap = (struct a *)u.u_ap; 5375992Swnj 53816694Smckusick if ((ip = owner(uap->fname, FOLLOW)) == NULL) 5395992Swnj return; 54021015Smckusick u.u_error = chmod1(ip, uap->fmode); 5419167Ssam iput(ip); 5427701Ssam } 5437439Sroot 5449167Ssam /* 5459167Ssam * Change mode of a file given a file descriptor. 5469167Ssam */ 5477701Ssam fchmod() 5487701Ssam { 5497701Ssam struct a { 5507701Ssam int fd; 5517701Ssam int fmode; 55216694Smckusick } *uap = (struct a *)u.u_ap; 5537701Ssam register struct inode *ip; 5547701Ssam register struct file *fp; 5557701Ssam 55612756Ssam fp = getinode(uap->fd); 5577701Ssam if (fp == NULL) 5587701Ssam return; 55912756Ssam ip = (struct inode *)fp->f_data; 5609167Ssam if (u.u_uid != ip->i_uid && !suser()) 5619167Ssam return; 56216664Smckusick ILOCK(ip); 56321015Smckusick u.u_error = chmod1(ip, uap->fmode); 56416664Smckusick IUNLOCK(ip); 5657701Ssam } 5667701Ssam 5679167Ssam /* 5689167Ssam * Change the mode on a file. 5699167Ssam * Inode must be locked before calling. 5709167Ssam */ 5717701Ssam chmod1(ip, mode) 5727701Ssam register struct inode *ip; 5737701Ssam register int mode; 5747701Ssam { 5757868Sroot 57621015Smckusick if (ip->i_fs->fs_ronly) 57721015Smckusick return (EROFS); 5786254Sroot ip->i_mode &= ~07777; 5797439Sroot if (u.u_uid) { 58021015Smckusick if ((ip->i_mode & IFMT) != IFDIR) 58121015Smckusick mode &= ~ISVTX; 58211811Ssam if (!groupmember(ip->i_gid)) 58311811Ssam mode &= ~ISGID; 5847439Sroot } 5857701Ssam ip->i_mode |= mode&07777; 5866254Sroot ip->i_flag |= ICHG; 5876254Sroot if (ip->i_flag&ITEXT && (ip->i_mode&ISVTX)==0) 5886254Sroot xrele(ip); 58921015Smckusick return (0); 5905992Swnj } 5915992Swnj 5929167Ssam /* 5939167Ssam * Set ownership given a path name. 5949167Ssam */ 5956254Sroot chown() 59637Sbill { 5977701Ssam struct inode *ip; 5987701Ssam struct a { 5996254Sroot char *fname; 6006254Sroot int uid; 6016254Sroot int gid; 60216694Smckusick } *uap = (struct a *)u.u_ap; 60337Sbill 60416694Smckusick if (!suser() || (ip = owner(uap->fname, NOFOLLOW)) == NULL) 60537Sbill return; 60611811Ssam u.u_error = chown1(ip, uap->uid, uap->gid); 6079167Ssam iput(ip); 6087701Ssam } 6097439Sroot 6109167Ssam /* 6119167Ssam * Set ownership given a file descriptor. 6129167Ssam */ 6137701Ssam fchown() 6147701Ssam { 6157701Ssam struct a { 6167701Ssam int fd; 6177701Ssam int uid; 6187701Ssam int gid; 61916694Smckusick } *uap = (struct a *)u.u_ap; 6207701Ssam register struct inode *ip; 6217701Ssam register struct file *fp; 6227701Ssam 62312756Ssam fp = getinode(uap->fd); 6247701Ssam if (fp == NULL) 6257701Ssam return; 62612756Ssam ip = (struct inode *)fp->f_data; 62711821Ssam if (!suser()) 6289167Ssam return; 62916664Smckusick ILOCK(ip); 63011811Ssam u.u_error = chown1(ip, uap->uid, uap->gid); 63116664Smckusick IUNLOCK(ip); 6327701Ssam } 6337701Ssam 6347701Ssam /* 6357701Ssam * Perform chown operation on inode ip; 6367701Ssam * inode must be locked prior to call. 6377701Ssam */ 6387701Ssam chown1(ip, uid, gid) 6397701Ssam register struct inode *ip; 6407701Ssam int uid, gid; 6417701Ssam { 6427701Ssam #ifdef QUOTA 6437701Ssam register long change; 64411811Ssam #endif 6457701Ssam 64621015Smckusick if (ip->i_fs->fs_ronly) 64721015Smckusick return (EROFS); 64811811Ssam if (uid == -1) 64911811Ssam uid = ip->i_uid; 65011811Ssam if (gid == -1) 65111811Ssam gid = ip->i_gid; 65211811Ssam #ifdef QUOTA 65314385Ssam if (ip->i_uid == uid) /* this just speeds things a little */ 6547482Skre change = 0; 65512646Ssam else 65612646Ssam change = ip->i_blocks; 65712646Ssam (void) chkdq(ip, -change, 1); 65812646Ssam (void) chkiq(ip->i_dev, ip, ip->i_uid, 1); 6597482Skre dqrele(ip->i_dquot); 6607482Skre #endif 66111811Ssam ip->i_uid = uid; 66211811Ssam ip->i_gid = gid; 6636254Sroot ip->i_flag |= ICHG; 6646254Sroot if (u.u_ruid != 0) 6656254Sroot ip->i_mode &= ~(ISUID|ISGID); 6667701Ssam #ifdef QUOTA 6677482Skre ip->i_dquot = inoquota(ip); 66812646Ssam (void) chkdq(ip, change, 1); 669*26361Skarels (void) chkiq(ip->i_dev, (struct inode *)NULL, (uid_t)uid, 1); 67012646Ssam return (u.u_error); /* should == 0 ALWAYS !! */ 67112646Ssam #else 67212646Ssam return (0); 6737482Skre #endif 67437Sbill } 67537Sbill 67611811Ssam utimes() 67711811Ssam { 67811811Ssam register struct a { 67911811Ssam char *fname; 68011811Ssam struct timeval *tptr; 68111811Ssam } *uap = (struct a *)u.u_ap; 68211811Ssam register struct inode *ip; 68311811Ssam struct timeval tv[2]; 68411811Ssam 68516694Smckusick if ((ip = owner(uap->fname, FOLLOW)) == NULL) 68611811Ssam return; 68721015Smckusick if (ip->i_fs->fs_ronly) { 68821015Smckusick u.u_error = EROFS; 68921015Smckusick iput(ip); 69021015Smckusick return; 69121015Smckusick } 69211811Ssam u.u_error = copyin((caddr_t)uap->tptr, (caddr_t)tv, sizeof (tv)); 69311811Ssam if (u.u_error == 0) { 69411811Ssam ip->i_flag |= IACC|IUPD|ICHG; 69511811Ssam iupdat(ip, &tv[0], &tv[1], 0); 69611811Ssam } 69711811Ssam iput(ip); 69811811Ssam } 69911811Ssam 7009167Ssam /* 7019167Ssam * Flush any pending I/O. 7029167Ssam */ 7036254Sroot sync() 70437Sbill { 70537Sbill 7068673Sroot update(); 70737Sbill } 7087535Sroot 7099167Ssam /* 7109167Ssam * Truncate a file given its path name. 7119167Ssam */ 7127701Ssam truncate() 7137701Ssam { 7147701Ssam struct a { 7157701Ssam char *fname; 7169167Ssam u_long length; 7177826Sroot } *uap = (struct a *)u.u_ap; 7187701Ssam struct inode *ip; 71916694Smckusick register struct nameidata *ndp = &u.u_nd; 7207701Ssam 72116694Smckusick ndp->ni_nameiop = LOOKUP | FOLLOW; 72216694Smckusick ndp->ni_segflg = UIO_USERSPACE; 72316694Smckusick ndp->ni_dirp = uap->fname; 72416694Smckusick ip = namei(ndp); 7257701Ssam if (ip == NULL) 7267701Ssam return; 7277701Ssam if (access(ip, IWRITE)) 7287701Ssam goto bad; 7297701Ssam if ((ip->i_mode&IFMT) == IFDIR) { 7307701Ssam u.u_error = EISDIR; 7317701Ssam goto bad; 7327701Ssam } 7337701Ssam itrunc(ip, uap->length); 7347701Ssam bad: 7357701Ssam iput(ip); 7367701Ssam } 7377701Ssam 7389167Ssam /* 7399167Ssam * Truncate a file given a file descriptor. 7409167Ssam */ 7417701Ssam ftruncate() 7427701Ssam { 7437701Ssam struct a { 7447701Ssam int fd; 7459167Ssam u_long length; 7467826Sroot } *uap = (struct a *)u.u_ap; 7477701Ssam struct inode *ip; 7487701Ssam struct file *fp; 7497701Ssam 75012756Ssam fp = getinode(uap->fd); 7517701Ssam if (fp == NULL) 7527701Ssam return; 7537701Ssam if ((fp->f_flag&FWRITE) == 0) { 7547701Ssam u.u_error = EINVAL; 7557701Ssam return; 7567701Ssam } 75712756Ssam ip = (struct inode *)fp->f_data; 75816664Smckusick ILOCK(ip); 7597701Ssam itrunc(ip, uap->length); 76016664Smckusick IUNLOCK(ip); 7617701Ssam } 7627701Ssam 7639167Ssam /* 7649167Ssam * Synch an open file. 7659167Ssam */ 7669167Ssam fsync() 7679167Ssam { 7689167Ssam struct a { 7699167Ssam int fd; 7709167Ssam } *uap = (struct a *)u.u_ap; 7719167Ssam struct inode *ip; 7729167Ssam struct file *fp; 7739167Ssam 77412756Ssam fp = getinode(uap->fd); 7759167Ssam if (fp == NULL) 7769167Ssam return; 77712756Ssam ip = (struct inode *)fp->f_data; 77816664Smckusick ILOCK(ip); 7799167Ssam syncip(ip); 78016664Smckusick IUNLOCK(ip); 7819167Ssam } 7829167Ssam 7839167Ssam /* 7849167Ssam * Rename system call. 7859167Ssam * rename("foo", "bar"); 7869167Ssam * is essentially 7879167Ssam * unlink("bar"); 7889167Ssam * link("foo", "bar"); 7899167Ssam * unlink("foo"); 7909167Ssam * but ``atomically''. Can't do full commit without saving state in the 7919167Ssam * inode on disk which isn't feasible at this time. Best we can do is 7929167Ssam * always guarantee the target exists. 7939167Ssam * 7949167Ssam * Basic algorithm is: 7959167Ssam * 7969167Ssam * 1) Bump link count on source while we're linking it to the 7979167Ssam * target. This also insure the inode won't be deleted out 79816776Smckusick * from underneath us while we work (it may be truncated by 79916776Smckusick * a concurrent `trunc' or `open' for creation). 8009167Ssam * 2) Link source to destination. If destination already exists, 8019167Ssam * delete it first. 80216776Smckusick * 3) Unlink source reference to inode if still around. If a 80316776Smckusick * directory was moved and the parent of the destination 8049167Ssam * is different from the source, patch the ".." entry in the 8059167Ssam * directory. 8069167Ssam * 8079167Ssam * Source and destination must either both be directories, or both 8089167Ssam * not be directories. If target is a directory, it must be empty. 8099167Ssam */ 8107701Ssam rename() 8117701Ssam { 8127701Ssam struct a { 8137701Ssam char *from; 8147701Ssam char *to; 81516694Smckusick } *uap = (struct a *)u.u_ap; 8169167Ssam register struct inode *ip, *xp, *dp; 81716776Smckusick struct dirtemplate dirbuf; 81816776Smckusick int doingdirectory = 0, oldparent = 0, newparent = 0; 81916694Smckusick register struct nameidata *ndp = &u.u_nd; 82010051Ssam int error = 0; 8217701Ssam 82216694Smckusick ndp->ni_nameiop = DELETE | LOCKPARENT; 82316694Smckusick ndp->ni_segflg = UIO_USERSPACE; 82416694Smckusick ndp->ni_dirp = uap->from; 82516694Smckusick ip = namei(ndp); 8269167Ssam if (ip == NULL) 8279167Ssam return; 82816694Smckusick dp = ndp->ni_pdir; 8299167Ssam if ((ip->i_mode&IFMT) == IFDIR) { 8309167Ssam register struct direct *d; 8319167Ssam 83216694Smckusick d = &ndp->ni_dent; 8339167Ssam /* 83411641Ssam * Avoid ".", "..", and aliases of "." for obvious reasons. 8359167Ssam */ 83611641Ssam if ((d->d_namlen == 1 && d->d_name[0] == '.') || 83711641Ssam (d->d_namlen == 2 && bcmp(d->d_name, "..", 2) == 0) || 83816776Smckusick (dp == ip) || (ip->i_flag & IRENAME)) { 83911641Ssam iput(dp); 84011641Ssam if (dp == ip) 84111641Ssam irele(ip); 84211641Ssam else 84310051Ssam iput(ip); 84411641Ssam u.u_error = EINVAL; 84511641Ssam return; 8469167Ssam } 84716776Smckusick ip->i_flag |= IRENAME; 8489167Ssam oldparent = dp->i_number; 8499167Ssam doingdirectory++; 8509167Ssam } 85111641Ssam iput(dp); 8529167Ssam 8539167Ssam /* 8549167Ssam * 1) Bump link count while we're moving stuff 8559167Ssam * around. If we crash somewhere before 8569167Ssam * completing our work, the link count 8579167Ssam * may be wrong, but correctable. 8589167Ssam */ 8599167Ssam ip->i_nlink++; 8609167Ssam ip->i_flag |= ICHG; 8619167Ssam iupdat(ip, &time, &time, 1); 86216664Smckusick IUNLOCK(ip); 8639167Ssam 8649167Ssam /* 8659167Ssam * When the target exists, both the directory 8669167Ssam * and target inodes are returned locked. 8679167Ssam */ 86816694Smckusick ndp->ni_nameiop = CREATE | LOCKPARENT | NOCACHE; 86916694Smckusick ndp->ni_dirp = (caddr_t)uap->to; 87016694Smckusick xp = namei(ndp); 87110051Ssam if (u.u_error) { 87210051Ssam error = u.u_error; 8739167Ssam goto out; 87410051Ssam } 87516694Smckusick dp = ndp->ni_pdir; 8769167Ssam /* 87711641Ssam * If ".." must be changed (ie the directory gets a new 87812816Smckusick * parent) then the source directory must not be in the 87912816Smckusick * directory heirarchy above the target, as this would 88012816Smckusick * orphan everything below the source directory. Also 88112816Smckusick * the user must have write permission in the source so 88212816Smckusick * as to be able to change "..". We must repeat the call 88312816Smckusick * to namei, as the parent directory is unlocked by the 88412816Smckusick * call to checkpath(). 88511641Ssam */ 88616776Smckusick if (oldparent != dp->i_number) 88716776Smckusick newparent = dp->i_number; 88816776Smckusick if (doingdirectory && newparent) { 88912816Smckusick if (access(ip, IWRITE)) 89012816Smckusick goto bad; 89112816Smckusick do { 89216694Smckusick dp = ndp->ni_pdir; 89312816Smckusick if (xp != NULL) 89412816Smckusick iput(xp); 89512816Smckusick u.u_error = checkpath(ip, dp); 89612816Smckusick if (u.u_error) 89712816Smckusick goto out; 89816694Smckusick xp = namei(ndp); 89912816Smckusick if (u.u_error) { 90012816Smckusick error = u.u_error; 90112816Smckusick goto out; 90212816Smckusick } 90316694Smckusick } while (dp != ndp->ni_pdir); 90412816Smckusick } 90511641Ssam /* 9069167Ssam * 2) If target doesn't exist, link the target 9079167Ssam * to the source and unlink the source. 9089167Ssam * Otherwise, rewrite the target directory 9099167Ssam * entry to reference the source inode and 9109167Ssam * expunge the original entry's existence. 9119167Ssam */ 9129167Ssam if (xp == NULL) { 9139167Ssam if (dp->i_dev != ip->i_dev) { 91410051Ssam error = EXDEV; 9159167Ssam goto bad; 9169167Ssam } 9179167Ssam /* 91816776Smckusick * Account for ".." in new directory. 91916776Smckusick * When source and destination have the same 92016776Smckusick * parent we don't fool with the link count. 9219167Ssam */ 92216776Smckusick if (doingdirectory && newparent) { 9239167Ssam dp->i_nlink++; 9249167Ssam dp->i_flag |= ICHG; 9259167Ssam iupdat(dp, &time, &time, 1); 9269167Ssam } 92716694Smckusick error = direnter(ip, ndp); 92810850Ssam if (error) 9299167Ssam goto out; 9309167Ssam } else { 9319167Ssam if (xp->i_dev != dp->i_dev || xp->i_dev != ip->i_dev) { 93210051Ssam error = EXDEV; 9339167Ssam goto bad; 9349167Ssam } 9359167Ssam /* 93610590Ssam * Short circuit rename(foo, foo). 93710590Ssam */ 93810590Ssam if (xp->i_number == ip->i_number) 93910590Ssam goto bad; 94010590Ssam /* 94124433Sbloom * If the parent directory is "sticky", then the user must 94224433Sbloom * own the parent directory, or the destination of the rename, 94324433Sbloom * otherwise the destination may not be changed (except by 94424433Sbloom * root). This implements append-only directories. 94524433Sbloom */ 94624433Sbloom if ((dp->i_mode & ISVTX) && u.u_uid != 0 && 94724433Sbloom u.u_uid != dp->i_uid && xp->i_uid != u.u_uid) { 94824433Sbloom error = EPERM; 94924433Sbloom goto bad; 95024433Sbloom } 95124433Sbloom /* 95210051Ssam * Target must be empty if a directory 95310051Ssam * and have no links to it. 9549167Ssam * Also, insure source and target are 9559167Ssam * compatible (both directories, or both 9569167Ssam * not directories). 9579167Ssam */ 9589167Ssam if ((xp->i_mode&IFMT) == IFDIR) { 95916776Smckusick if (!dirempty(xp, dp->i_number) || xp->i_nlink > 2) { 96010051Ssam error = ENOTEMPTY; 9619167Ssam goto bad; 9629167Ssam } 9639167Ssam if (!doingdirectory) { 96410051Ssam error = ENOTDIR; 9659167Ssam goto bad; 9669167Ssam } 96716776Smckusick cacheinval(dp); 9689167Ssam } else if (doingdirectory) { 96910051Ssam error = EISDIR; 9709167Ssam goto bad; 9719167Ssam } 97216694Smckusick dirrewrite(dp, ip, ndp); 97310051Ssam if (u.u_error) { 97410051Ssam error = u.u_error; 9759167Ssam goto bad1; 97610051Ssam } 9779167Ssam /* 97810051Ssam * Adjust the link count of the target to 97910051Ssam * reflect the dirrewrite above. If this is 98010051Ssam * a directory it is empty and there are 98110051Ssam * no links to it, so we can squash the inode and 98210051Ssam * any space associated with it. We disallowed 98310051Ssam * renaming over top of a directory with links to 98416776Smckusick * it above, as the remaining link would point to 98516776Smckusick * a directory without "." or ".." entries. 9869167Ssam */ 98710051Ssam xp->i_nlink--; 9889167Ssam if (doingdirectory) { 98910051Ssam if (--xp->i_nlink != 0) 99010051Ssam panic("rename: linked directory"); 9919167Ssam itrunc(xp, (u_long)0); 99210051Ssam } 9939167Ssam xp->i_flag |= ICHG; 9949167Ssam iput(xp); 99510246Ssam xp = NULL; 9969167Ssam } 9979167Ssam 9989167Ssam /* 9999167Ssam * 3) Unlink the source. 10009167Ssam */ 100116694Smckusick ndp->ni_nameiop = DELETE | LOCKPARENT; 100216694Smckusick ndp->ni_segflg = UIO_USERSPACE; 100316694Smckusick ndp->ni_dirp = uap->from; 100416776Smckusick xp = namei(ndp); 100517758Smckusick if (xp != NULL) 100617758Smckusick dp = ndp->ni_pdir; 100717758Smckusick else 100817758Smckusick dp = NULL; 10099167Ssam /* 101016776Smckusick * Insure that the directory entry still exists and has not 101116776Smckusick * changed while the new name has been entered. If the source is 101216776Smckusick * a file then the entry may have been unlinked or renamed. In 101316776Smckusick * either case there is no further work to be done. If the source 101416776Smckusick * is a directory then it cannot have been rmdir'ed; its link 101516776Smckusick * count of three would cause a rmdir to fail with ENOTEMPTY. 101616776Smckusick * The IRENAME flag insures that it cannot be moved by another 101716776Smckusick * rename. 10189167Ssam */ 101917758Smckusick if (xp != ip) { 102016776Smckusick if (doingdirectory) 102117758Smckusick panic("rename: lost dir entry"); 102216776Smckusick } else { 10239167Ssam /* 102416776Smckusick * If the source is a directory with a 102516776Smckusick * new parent, the link count of the old 102616776Smckusick * parent directory must be decremented 102716776Smckusick * and ".." set to point to the new parent. 10289167Ssam */ 102916776Smckusick if (doingdirectory && newparent) { 10309167Ssam dp->i_nlink--; 10319167Ssam dp->i_flag |= ICHG; 103216776Smckusick error = rdwri(UIO_READ, xp, (caddr_t)&dirbuf, 103316776Smckusick sizeof (struct dirtemplate), (off_t)0, 1, 103416776Smckusick (int *)0); 103516776Smckusick if (error == 0) { 103616776Smckusick if (dirbuf.dotdot_namlen != 2 || 103716776Smckusick dirbuf.dotdot_name[0] != '.' || 103816776Smckusick dirbuf.dotdot_name[1] != '.') { 103916776Smckusick printf("rename: mangled dir\n"); 104016776Smckusick } else { 104116776Smckusick dirbuf.dotdot_ino = newparent; 104216776Smckusick (void) rdwri(UIO_WRITE, xp, 104316776Smckusick (caddr_t)&dirbuf, 104416776Smckusick sizeof (struct dirtemplate), 104516776Smckusick (off_t)0, 1, (int *)0); 104616776Smckusick cacheinval(dp); 104716776Smckusick } 104816776Smckusick } 10499167Ssam } 105016694Smckusick if (dirremove(ndp)) { 105116776Smckusick xp->i_nlink--; 105216776Smckusick xp->i_flag |= ICHG; 10539167Ssam } 105416776Smckusick xp->i_flag &= ~IRENAME; 105516776Smckusick if (error == 0) /* XXX conservative */ 105610051Ssam error = u.u_error; 10579167Ssam } 10589167Ssam if (dp) 10599167Ssam iput(dp); 106016776Smckusick if (xp) 106116776Smckusick iput(xp); 106216776Smckusick irele(ip); 106316776Smckusick if (error) 106416776Smckusick u.u_error = error; 106516776Smckusick return; 10669167Ssam 10679167Ssam bad: 106810246Ssam iput(dp); 10699167Ssam bad1: 10709167Ssam if (xp) 107110246Ssam iput(xp); 10729167Ssam out: 10739167Ssam ip->i_nlink--; 10749167Ssam ip->i_flag |= ICHG; 10759167Ssam irele(ip); 107610051Ssam if (error) 107710051Ssam u.u_error = error; 10787701Ssam } 10797701Ssam 10807535Sroot /* 10817535Sroot * Make a new file. 10827535Sroot */ 10837535Sroot struct inode * 108416694Smckusick maknode(mode, ndp) 10857535Sroot int mode; 108616694Smckusick register struct nameidata *ndp; 10877535Sroot { 10887535Sroot register struct inode *ip; 108916694Smckusick register struct inode *pdir = ndp->ni_pdir; 10907535Sroot ino_t ipref; 10917535Sroot 10927535Sroot if ((mode & IFMT) == IFDIR) 109316694Smckusick ipref = dirpref(pdir->i_fs); 10947535Sroot else 109516694Smckusick ipref = pdir->i_number; 109616694Smckusick ip = ialloc(pdir, ipref, mode); 10977535Sroot if (ip == NULL) { 109816694Smckusick iput(pdir); 10997701Ssam return (NULL); 11007535Sroot } 11017701Ssam #ifdef QUOTA 11027535Sroot if (ip->i_dquot != NODQUOT) 11037535Sroot panic("maknode: dquot"); 11047535Sroot #endif 11057535Sroot ip->i_flag |= IACC|IUPD|ICHG; 11067535Sroot if ((mode & IFMT) == 0) 11077535Sroot mode |= IFREG; 11087535Sroot ip->i_mode = mode & ~u.u_cmask; 11097535Sroot ip->i_nlink = 1; 11107535Sroot ip->i_uid = u.u_uid; 111116694Smckusick ip->i_gid = pdir->i_gid; 111211811Ssam if (ip->i_mode & ISGID && !groupmember(ip->i_gid)) 111311811Ssam ip->i_mode &= ~ISGID; 11147701Ssam #ifdef QUOTA 11157535Sroot ip->i_dquot = inoquota(ip); 11167535Sroot #endif 11177535Sroot 11187535Sroot /* 11197535Sroot * Make sure inode goes to disk before directory entry. 11207535Sroot */ 11218673Sroot iupdat(ip, &time, &time, 1); 112216694Smckusick u.u_error = direnter(ip, ndp); 11237535Sroot if (u.u_error) { 11247535Sroot /* 112510850Ssam * Write error occurred trying to update directory 112610850Ssam * so must deallocate the inode. 11277535Sroot */ 11287535Sroot ip->i_nlink = 0; 11297535Sroot ip->i_flag |= ICHG; 11307535Sroot iput(ip); 11317701Ssam return (NULL); 11327535Sroot } 11337701Ssam return (ip); 11347535Sroot } 113512756Ssam 113612756Ssam /* 113712756Ssam * A virgin directory (no blushing please). 113812756Ssam */ 113912756Ssam struct dirtemplate mastertemplate = { 114012756Ssam 0, 12, 1, ".", 114112756Ssam 0, DIRBLKSIZ - 12, 2, ".." 114212756Ssam }; 114312756Ssam 114412756Ssam /* 114512756Ssam * Mkdir system call 114612756Ssam */ 114712756Ssam mkdir() 114812756Ssam { 114912756Ssam struct a { 115012756Ssam char *name; 115112756Ssam int dmode; 115216694Smckusick } *uap = (struct a *)u.u_ap; 115312756Ssam register struct inode *ip, *dp; 115412756Ssam struct dirtemplate dirtemplate; 115516694Smckusick register struct nameidata *ndp = &u.u_nd; 115612756Ssam 115716694Smckusick ndp->ni_nameiop = CREATE; 115816694Smckusick ndp->ni_segflg = UIO_USERSPACE; 115916694Smckusick ndp->ni_dirp = uap->name; 116016694Smckusick ip = namei(ndp); 116112756Ssam if (u.u_error) 116212756Ssam return; 116312756Ssam if (ip != NULL) { 116412756Ssam iput(ip); 116512756Ssam u.u_error = EEXIST; 116612756Ssam return; 116712756Ssam } 116816694Smckusick dp = ndp->ni_pdir; 116912756Ssam uap->dmode &= 0777; 117012756Ssam uap->dmode |= IFDIR; 117112756Ssam /* 117212756Ssam * Must simulate part of maknode here 117312756Ssam * in order to acquire the inode, but 117412756Ssam * not have it entered in the parent 117512756Ssam * directory. The entry is made later 117612756Ssam * after writing "." and ".." entries out. 117712756Ssam */ 117812756Ssam ip = ialloc(dp, dirpref(dp->i_fs), uap->dmode); 117912756Ssam if (ip == NULL) { 118012756Ssam iput(dp); 118112756Ssam return; 118212756Ssam } 118312756Ssam #ifdef QUOTA 118412756Ssam if (ip->i_dquot != NODQUOT) 118512756Ssam panic("mkdir: dquot"); 118612756Ssam #endif 118712756Ssam ip->i_flag |= IACC|IUPD|ICHG; 118812756Ssam ip->i_mode = uap->dmode & ~u.u_cmask; 118912756Ssam ip->i_nlink = 2; 119012756Ssam ip->i_uid = u.u_uid; 119112756Ssam ip->i_gid = dp->i_gid; 119212756Ssam #ifdef QUOTA 119312756Ssam ip->i_dquot = inoquota(ip); 119412756Ssam #endif 119512756Ssam iupdat(ip, &time, &time, 1); 119612756Ssam 119712756Ssam /* 119812756Ssam * Bump link count in parent directory 119912756Ssam * to reflect work done below. Should 120012756Ssam * be done before reference is created 120112756Ssam * so reparation is possible if we crash. 120212756Ssam */ 120312756Ssam dp->i_nlink++; 120412756Ssam dp->i_flag |= ICHG; 120512756Ssam iupdat(dp, &time, &time, 1); 120612756Ssam 120712756Ssam /* 120812756Ssam * Initialize directory with "." 120912756Ssam * and ".." from static template. 121012756Ssam */ 121112756Ssam dirtemplate = mastertemplate; 121212756Ssam dirtemplate.dot_ino = ip->i_number; 121312756Ssam dirtemplate.dotdot_ino = dp->i_number; 121412756Ssam u.u_error = rdwri(UIO_WRITE, ip, (caddr_t)&dirtemplate, 121512756Ssam sizeof (dirtemplate), (off_t)0, 1, (int *)0); 121612756Ssam if (u.u_error) { 121712756Ssam dp->i_nlink--; 121812756Ssam dp->i_flag |= ICHG; 121912756Ssam goto bad; 122012756Ssam } 122118103Smckusick if (DIRBLKSIZ > ip->i_fs->fs_fsize) 122218103Smckusick panic("mkdir: blksize"); /* XXX - should grow with bmap() */ 122318103Smckusick else 122418103Smckusick ip->i_size = DIRBLKSIZ; 122512756Ssam /* 122612756Ssam * Directory all set up, now 122712756Ssam * install the entry for it in 122812756Ssam * the parent directory. 122912756Ssam */ 123016694Smckusick u.u_error = direnter(ip, ndp); 123112756Ssam dp = NULL; 123212756Ssam if (u.u_error) { 123316694Smckusick ndp->ni_nameiop = LOOKUP | NOCACHE; 123416694Smckusick ndp->ni_segflg = UIO_USERSPACE; 123516694Smckusick ndp->ni_dirp = uap->name; 123616694Smckusick dp = namei(ndp); 123712756Ssam if (dp) { 123812756Ssam dp->i_nlink--; 123912756Ssam dp->i_flag |= ICHG; 124012756Ssam } 124112756Ssam } 124212756Ssam bad: 124312756Ssam /* 124412756Ssam * No need to do an explicit itrunc here, 124512756Ssam * irele will do this for us because we set 124612756Ssam * the link count to 0. 124712756Ssam */ 124812756Ssam if (u.u_error) { 124912756Ssam ip->i_nlink = 0; 125012756Ssam ip->i_flag |= ICHG; 125112756Ssam } 125212756Ssam if (dp) 125312756Ssam iput(dp); 125412756Ssam iput(ip); 125512756Ssam } 125612756Ssam 125712756Ssam /* 125812756Ssam * Rmdir system call. 125912756Ssam */ 126012756Ssam rmdir() 126112756Ssam { 126212756Ssam struct a { 126312756Ssam char *name; 126416694Smckusick } *uap = (struct a *)u.u_ap; 126512756Ssam register struct inode *ip, *dp; 126616694Smckusick register struct nameidata *ndp = &u.u_nd; 126712756Ssam 126816694Smckusick ndp->ni_nameiop = DELETE | LOCKPARENT; 126916694Smckusick ndp->ni_segflg = UIO_USERSPACE; 127016694Smckusick ndp->ni_dirp = uap->name; 127116694Smckusick ip = namei(ndp); 127212756Ssam if (ip == NULL) 127312756Ssam return; 127416694Smckusick dp = ndp->ni_pdir; 127512756Ssam /* 127612756Ssam * No rmdir "." please. 127712756Ssam */ 127812756Ssam if (dp == ip) { 127912756Ssam irele(dp); 128012756Ssam iput(ip); 128112756Ssam u.u_error = EINVAL; 128212756Ssam return; 128312756Ssam } 128412756Ssam if ((ip->i_mode&IFMT) != IFDIR) { 128512756Ssam u.u_error = ENOTDIR; 128612756Ssam goto out; 128712756Ssam } 128812756Ssam /* 128912756Ssam * Don't remove a mounted on directory. 129012756Ssam */ 129112756Ssam if (ip->i_dev != dp->i_dev) { 129212756Ssam u.u_error = EBUSY; 129312756Ssam goto out; 129412756Ssam } 129512756Ssam /* 129612756Ssam * Verify the directory is empty (and valid). 129712756Ssam * (Rmdir ".." won't be valid since 129812756Ssam * ".." will contain a reference to 129912756Ssam * the current directory and thus be 130012756Ssam * non-empty.) 130112756Ssam */ 130216776Smckusick if (ip->i_nlink != 2 || !dirempty(ip, dp->i_number)) { 130312756Ssam u.u_error = ENOTEMPTY; 130412756Ssam goto out; 130512756Ssam } 130612756Ssam /* 130712756Ssam * Delete reference to directory before purging 130812756Ssam * inode. If we crash in between, the directory 130912756Ssam * will be reattached to lost+found, 131012756Ssam */ 131116694Smckusick if (dirremove(ndp) == 0) 131212756Ssam goto out; 131312756Ssam dp->i_nlink--; 131412756Ssam dp->i_flag |= ICHG; 131516776Smckusick cacheinval(dp); 131612756Ssam iput(dp); 131712756Ssam dp = NULL; 131812756Ssam /* 131912756Ssam * Truncate inode. The only stuff left 132012756Ssam * in the directory is "." and "..". The 132112756Ssam * "." reference is inconsequential since 132212756Ssam * we're quashing it. The ".." reference 132312756Ssam * has already been adjusted above. We've 132412756Ssam * removed the "." reference and the reference 132512756Ssam * in the parent directory, but there may be 132612756Ssam * other hard links so decrement by 2 and 132712756Ssam * worry about them later. 132812756Ssam */ 132912756Ssam ip->i_nlink -= 2; 133012756Ssam itrunc(ip, (u_long)0); 133116739Smckusick cacheinval(ip); 133212756Ssam out: 133312756Ssam if (dp) 133412756Ssam iput(dp); 133512756Ssam iput(ip); 133612756Ssam } 133712756Ssam 133812756Ssam struct file * 133912756Ssam getinode(fdes) 134012756Ssam int fdes; 134112756Ssam { 134216540Ssam struct file *fp; 134312756Ssam 134416540Ssam if ((unsigned)fdes >= NOFILE || (fp = u.u_ofile[fdes]) == NULL) { 134516540Ssam u.u_error = EBADF; 134616540Ssam return ((struct file *)0); 134716540Ssam } 134812756Ssam if (fp->f_type != DTYPE_INODE) { 134912756Ssam u.u_error = EINVAL; 135016540Ssam return ((struct file *)0); 135112756Ssam } 135212756Ssam return (fp); 135312756Ssam } 135412756Ssam 135512756Ssam /* 135612756Ssam * mode mask for creation of files 135712756Ssam */ 135812756Ssam umask() 135912756Ssam { 136012756Ssam register struct a { 136112756Ssam int mask; 136216694Smckusick } *uap = (struct a *)u.u_ap; 136312756Ssam 136412756Ssam u.u_r.r_val1 = u.u_cmask; 136512756Ssam u.u_cmask = uap->mask & 07777; 136612756Ssam } 1367