1*16776Smckusick /* vfs_syscalls.c 6.13 84/07/27 */ 237Sbill 337Sbill #include "../h/param.h" 437Sbill #include "../h/systm.h" 537Sbill #include "../h/dir.h" 637Sbill #include "../h/user.h" 78040Sroot #include "../h/kernel.h" 86254Sroot #include "../h/file.h" 96574Smckusic #include "../h/stat.h" 1037Sbill #include "../h/inode.h" 116574Smckusic #include "../h/fs.h" 126254Sroot #include "../h/buf.h" 136254Sroot #include "../h/proc.h" 147482Skre #include "../h/quota.h" 157826Sroot #include "../h/uio.h" 167826Sroot #include "../h/socket.h" 178632Sroot #include "../h/socketvar.h" 1812756Ssam #include "../h/mount.h" 1937Sbill 2012756Ssam extern struct fileops inodeops; 2112756Ssam struct file *getinode(); 2212756Ssam 239167Ssam /* 249167Ssam * Change current working directory (``.''). 259167Ssam */ 266254Sroot chdir() 276254Sroot { 286254Sroot 296254Sroot chdirec(&u.u_cdir); 306254Sroot } 316254Sroot 329167Ssam /* 339167Ssam * Change notion of root (``/'') directory. 349167Ssam */ 356254Sroot chroot() 366254Sroot { 376254Sroot 386254Sroot if (suser()) 396254Sroot chdirec(&u.u_rdir); 406254Sroot } 416254Sroot 429167Ssam /* 439167Ssam * Common routine for chroot and chdir. 449167Ssam */ 456254Sroot chdirec(ipp) 467701Ssam register struct inode **ipp; 476254Sroot { 486254Sroot register struct inode *ip; 496254Sroot struct a { 506254Sroot char *fname; 5116694Smckusick } *uap = (struct a *)u.u_ap; 5216694Smckusick register struct nameidata *ndp = &u.u_nd; 536254Sroot 5416694Smckusick ndp->ni_nameiop = LOOKUP | FOLLOW; 5516694Smckusick ndp->ni_segflg = UIO_USERSPACE; 5616694Smckusick ndp->ni_dirp = uap->fname; 5716694Smckusick ip = namei(ndp); 589167Ssam if (ip == NULL) 596254Sroot return; 609167Ssam if ((ip->i_mode&IFMT) != IFDIR) { 616254Sroot u.u_error = ENOTDIR; 626254Sroot goto bad; 636254Sroot } 649167Ssam if (access(ip, IEXEC)) 656254Sroot goto bad; 6616664Smckusick IUNLOCK(ip); 677142Smckusick if (*ipp) 687142Smckusick irele(*ipp); 696254Sroot *ipp = ip; 706254Sroot return; 716254Sroot 726254Sroot bad: 736254Sroot iput(ip); 746254Sroot } 756254Sroot 7637Sbill /* 776254Sroot * Open system call. 786254Sroot */ 796254Sroot open() 806254Sroot { 8112756Ssam struct a { 826254Sroot char *fname; 837701Ssam int mode; 8412756Ssam int crtmode; 8512756Ssam } *uap = (struct a *) u.u_ap; 866254Sroot 8716694Smckusick copen(uap->mode-FOPEN, uap->crtmode, uap->fname); 886254Sroot } 896254Sroot 906254Sroot /* 916254Sroot * Creat system call. 926254Sroot */ 9312756Ssam creat() 946254Sroot { 9512756Ssam struct a { 966254Sroot char *fname; 976254Sroot int fmode; 9812756Ssam } *uap = (struct a *)u.u_ap; 996254Sroot 10016694Smckusick copen(FWRITE|FCREAT|FTRUNC, uap->fmode, uap->fname); 1016254Sroot } 1026254Sroot 1036254Sroot /* 1046254Sroot * Common code for open and creat. 10512756Ssam * Check permissions, allocate an open file structure, 10612756Ssam * and call the device open routine if any. 1076254Sroot */ 10816694Smckusick copen(mode, arg, fname) 10912756Ssam register int mode; 11012756Ssam int arg; 11116694Smckusick caddr_t fname; 11212756Ssam { 1136254Sroot register struct inode *ip; 1146254Sroot register struct file *fp; 11516694Smckusick register struct nameidata *ndp = &u.u_nd; 11612756Ssam int i; 1176254Sroot 11812756Ssam #ifdef notdef 11912756Ssam if ((mode&(FREAD|FWRITE)) == 0) { 12012756Ssam u.u_error = EINVAL; 12112756Ssam return; 12212756Ssam } 12312756Ssam #endif 12416694Smckusick ndp->ni_segflg = UIO_USERSPACE; 12516694Smckusick ndp->ni_dirp = fname; 12612756Ssam if (mode&FCREAT) { 12716694Smckusick ndp->ni_nameiop = CREATE | FOLLOW; 12816694Smckusick ip = namei(ndp); 12912756Ssam if (ip == NULL) { 13012756Ssam if (u.u_error) 13112756Ssam return; 13216694Smckusick ip = maknode(arg&07777&(~ISVTX), ndp); 13312756Ssam if (ip == NULL) 13412756Ssam return; 13512756Ssam mode &= ~FTRUNC; 13612756Ssam } else { 13712756Ssam if (mode&FEXCL) { 13812756Ssam u.u_error = EEXIST; 13912756Ssam iput(ip); 14012756Ssam return; 14112756Ssam } 14212756Ssam mode &= ~FCREAT; 14312756Ssam } 14412756Ssam } else { 14516694Smckusick ndp->ni_nameiop = LOOKUP | FOLLOW; 14616694Smckusick ip = namei(ndp); 14712756Ssam if (ip == NULL) 14812756Ssam return; 14912756Ssam } 15012756Ssam if ((ip->i_mode & IFMT) == IFSOCK) { 15112756Ssam u.u_error = EOPNOTSUPP; 15212756Ssam goto bad; 15312756Ssam } 15412756Ssam if ((mode&FCREAT) == 0) { 1556254Sroot if (mode&FREAD) 1567701Ssam if (access(ip, IREAD)) 1577701Ssam goto bad; 15816032Skarels if (mode&(FWRITE|FTRUNC)) { 1597701Ssam if (access(ip, IWRITE)) 1607701Ssam goto bad; 1617701Ssam if ((ip->i_mode&IFMT) == IFDIR) { 1626254Sroot u.u_error = EISDIR; 1637701Ssam goto bad; 1647701Ssam } 1656254Sroot } 1666254Sroot } 16712756Ssam fp = falloc(); 16812756Ssam if (fp == NULL) 16912756Ssam goto bad; 17012756Ssam if (mode&FTRUNC) 1719167Ssam itrunc(ip, (u_long)0); 17216664Smckusick IUNLOCK(ip); 17312756Ssam fp->f_flag = mode&FMASK; 17412756Ssam fp->f_type = DTYPE_INODE; 17512756Ssam fp->f_ops = &inodeops; 17612756Ssam fp->f_data = (caddr_t)ip; 1776254Sroot i = u.u_r.r_val1; 17812756Ssam if (setjmp(&u.u_qsave)) { 17912756Ssam if (u.u_error == 0) 18012756Ssam u.u_error = EINTR; 18112756Ssam u.u_ofile[i] = NULL; 18212756Ssam closef(fp); 18312756Ssam return; 18412756Ssam } 1858559Sroot u.u_error = openi(ip, mode); 18612756Ssam if (u.u_error == 0) 1876254Sroot return; 1886254Sroot u.u_ofile[i] = NULL; 1896254Sroot fp->f_count--; 1907142Smckusick irele(ip); 1917701Ssam return; 1927701Ssam bad: 1937701Ssam iput(ip); 1946254Sroot } 1956254Sroot 1966254Sroot /* 1976254Sroot * Mknod system call 1986254Sroot */ 1996254Sroot mknod() 2006254Sroot { 2016254Sroot register struct inode *ip; 2026254Sroot register struct a { 2036254Sroot char *fname; 2046254Sroot int fmode; 2056254Sroot int dev; 20616694Smckusick } *uap = (struct a *)u.u_ap; 20716694Smckusick register struct nameidata *ndp = &u.u_nd; 2086254Sroot 20912756Ssam if (!suser()) 21012756Ssam return; 21116694Smckusick ndp->ni_nameiop = CREATE; 21216694Smckusick ndp->ni_segflg = UIO_USERSPACE; 21316694Smckusick ndp->ni_dirp = uap->fname; 21416694Smckusick ip = namei(ndp); 21512756Ssam if (ip != NULL) { 21612756Ssam u.u_error = EEXIST; 21712756Ssam goto out; 2186254Sroot } 2196254Sroot if (u.u_error) 2206254Sroot return; 22116694Smckusick ip = maknode(uap->fmode, ndp); 2226254Sroot if (ip == NULL) 2236254Sroot return; 22412756Ssam switch (ip->i_mode & IFMT) { 22512756Ssam 22615093Smckusick case IFMT: /* used by badsect to flag bad sectors */ 22712756Ssam case IFCHR: 22812756Ssam case IFBLK: 22912756Ssam if (uap->dev) { 23012756Ssam /* 23112756Ssam * Want to be able to use this to make badblock 23212756Ssam * inodes, so don't truncate the dev number. 23312756Ssam */ 23412756Ssam ip->i_rdev = uap->dev; 23512756Ssam ip->i_flag |= IACC|IUPD|ICHG; 23612756Ssam } 2376254Sroot } 2386254Sroot 2396254Sroot out: 2406254Sroot iput(ip); 2416254Sroot } 2426254Sroot 2436254Sroot /* 2446254Sroot * link system call 2456254Sroot */ 2466254Sroot link() 2476254Sroot { 2486254Sroot register struct inode *ip, *xp; 2496254Sroot register struct a { 2506254Sroot char *target; 2516254Sroot char *linkname; 25216694Smckusick } *uap = (struct a *)u.u_ap; 25316694Smckusick register struct nameidata *ndp = &u.u_nd; 2546254Sroot 25516694Smckusick ndp->ni_nameiop = LOOKUP | FOLLOW; 25616694Smckusick ndp->ni_segflg = UIO_USERSPACE; 25716694Smckusick ndp->ni_dirp = uap->target; 25816694Smckusick ip = namei(ndp); /* well, this routine is doomed anyhow */ 2596254Sroot if (ip == NULL) 2606254Sroot return; 2619167Ssam if ((ip->i_mode&IFMT) == IFDIR && !suser()) { 2627439Sroot iput(ip); 2637439Sroot return; 2647439Sroot } 2656254Sroot ip->i_nlink++; 2666254Sroot ip->i_flag |= ICHG; 2678673Sroot iupdat(ip, &time, &time, 1); 26816664Smckusick IUNLOCK(ip); 26916694Smckusick ndp->ni_nameiop = CREATE; 27016694Smckusick ndp->ni_segflg = UIO_USERSPACE; 27116694Smckusick ndp->ni_dirp = (caddr_t)uap->linkname; 27216694Smckusick xp = namei(ndp); 2736254Sroot if (xp != NULL) { 2746254Sroot u.u_error = EEXIST; 2756254Sroot iput(xp); 2766254Sroot goto out; 2776254Sroot } 2786254Sroot if (u.u_error) 2796254Sroot goto out; 28016694Smckusick if (ndp->ni_pdir->i_dev != ip->i_dev) { 28116694Smckusick iput(ndp->ni_pdir); 2826254Sroot u.u_error = EXDEV; 2836254Sroot goto out; 2846254Sroot } 28516694Smckusick u.u_error = direnter(ip, ndp); 2866254Sroot out: 2876254Sroot if (u.u_error) { 2886254Sroot ip->i_nlink--; 2896254Sroot ip->i_flag |= ICHG; 2906254Sroot } 2917142Smckusick irele(ip); 2926254Sroot } 2936254Sroot 2946254Sroot /* 2956254Sroot * symlink -- make a symbolic link 2966254Sroot */ 2976254Sroot symlink() 2986254Sroot { 2996254Sroot register struct a { 3006254Sroot char *target; 3016254Sroot char *linkname; 30216694Smckusick } *uap = (struct a *)u.u_ap; 3036254Sroot register struct inode *ip; 3046254Sroot register char *tp; 3056254Sroot register c, nc; 30616694Smckusick register struct nameidata *ndp = &u.u_nd; 3076254Sroot 3086254Sroot tp = uap->target; 3096254Sroot nc = 0; 3106254Sroot while (c = fubyte(tp)) { 3116254Sroot if (c < 0) { 3126254Sroot u.u_error = EFAULT; 3136254Sroot return; 3146254Sroot } 3156254Sroot tp++; 3166254Sroot nc++; 3176254Sroot } 31816694Smckusick ndp->ni_nameiop = CREATE; 31916694Smckusick ndp->ni_segflg = UIO_USERSPACE; 32016694Smckusick ndp->ni_dirp = uap->linkname; 32116694Smckusick ip = namei(ndp); 3226254Sroot if (ip) { 3236254Sroot iput(ip); 3246254Sroot u.u_error = EEXIST; 3256254Sroot return; 3266254Sroot } 3276254Sroot if (u.u_error) 3286254Sroot return; 32916694Smckusick ip = maknode(IFLNK | 0777, ndp); 3306254Sroot if (ip == NULL) 3316254Sroot return; 3327826Sroot u.u_error = rdwri(UIO_WRITE, ip, uap->target, nc, 0, 0, (int *)0); 3339167Ssam /* handle u.u_error != 0 */ 3346254Sroot iput(ip); 3356254Sroot } 3366254Sroot 3376254Sroot /* 3386254Sroot * Unlink system call. 3396254Sroot * Hard to avoid races here, especially 3406254Sroot * in unlinking directories. 3416254Sroot */ 3426254Sroot unlink() 3436254Sroot { 3446254Sroot struct a { 3456254Sroot char *fname; 34616694Smckusick } *uap = (struct a *)u.u_ap; 3479167Ssam register struct inode *ip, *dp; 34816694Smckusick register struct nameidata *ndp = &u.u_nd; 3496254Sroot 35016694Smckusick ndp->ni_nameiop = DELETE | LOCKPARENT; 35116694Smckusick ndp->ni_segflg = UIO_USERSPACE; 35216694Smckusick ndp->ni_dirp = uap->fname; 35316694Smckusick ip = namei(ndp); 3549167Ssam if (ip == NULL) 3556254Sroot return; 35616694Smckusick dp = ndp->ni_pdir; 3579167Ssam if ((ip->i_mode&IFMT) == IFDIR && !suser()) 3586254Sroot goto out; 3596254Sroot /* 3606254Sroot * Don't unlink a mounted file. 3616254Sroot */ 3629167Ssam if (ip->i_dev != dp->i_dev) { 3636254Sroot u.u_error = EBUSY; 3646254Sroot goto out; 3656254Sroot } 3666254Sroot if (ip->i_flag&ITEXT) 3676254Sroot xrele(ip); /* try once to free text */ 36816694Smckusick if (dirremove(ndp)) { 3697535Sroot ip->i_nlink--; 3707535Sroot ip->i_flag |= ICHG; 3716254Sroot } 3726254Sroot out: 3739167Ssam if (dp == ip) 3747142Smckusick irele(ip); 3757142Smckusick else 3767142Smckusick iput(ip); 3779167Ssam iput(dp); 3786254Sroot } 3796254Sroot 3806254Sroot /* 3816254Sroot * Seek system call 3826254Sroot */ 3838040Sroot lseek() 3846254Sroot { 3856254Sroot register struct file *fp; 3866254Sroot register struct a { 3877701Ssam int fd; 3886254Sroot off_t off; 3896254Sroot int sbase; 39016694Smckusick } *uap = (struct a *)u.u_ap; 3916254Sroot 39216540Ssam GETF(fp, uap->fd); 39316540Ssam if (fp->f_type != DTYPE_INODE) { 39416540Ssam u.u_error = ESPIPE; 3956254Sroot return; 39616540Ssam } 39713878Ssam switch (uap->sbase) { 39813878Ssam 39913878Ssam case L_INCR: 40013878Ssam fp->f_offset += uap->off; 40113878Ssam break; 40213878Ssam 40313878Ssam case L_XTND: 40413878Ssam fp->f_offset = uap->off + ((struct inode *)fp->f_data)->i_size; 40513878Ssam break; 40613878Ssam 40713878Ssam case L_SET: 40813878Ssam fp->f_offset = uap->off; 40913878Ssam break; 41013878Ssam 41113878Ssam default: 41213878Ssam u.u_error = EINVAL; 41313878Ssam return; 41413878Ssam } 41513878Ssam u.u_r.r_off = fp->f_offset; 4166254Sroot } 4176254Sroot 4186254Sroot /* 4196254Sroot * Access system call 4206254Sroot */ 4216254Sroot saccess() 4226254Sroot { 4236254Sroot register svuid, svgid; 4246254Sroot register struct inode *ip; 4256254Sroot register struct a { 4266254Sroot char *fname; 4276254Sroot int fmode; 42816694Smckusick } *uap = (struct a *)u.u_ap; 42916694Smckusick register struct nameidata *ndp = &u.u_nd; 4306254Sroot 4316254Sroot svuid = u.u_uid; 4326254Sroot svgid = u.u_gid; 4336254Sroot u.u_uid = u.u_ruid; 4346254Sroot u.u_gid = u.u_rgid; 43516694Smckusick ndp->ni_nameiop = LOOKUP | FOLLOW; 43616694Smckusick ndp->ni_segflg = UIO_USERSPACE; 43716694Smckusick ndp->ni_dirp = uap->fname; 43816694Smckusick ip = namei(ndp); 4396254Sroot if (ip != NULL) { 44012756Ssam if ((uap->fmode&R_OK) && access(ip, IREAD)) 4417701Ssam goto done; 44212756Ssam if ((uap->fmode&W_OK) && access(ip, IWRITE)) 4437701Ssam goto done; 44412756Ssam if ((uap->fmode&X_OK) && access(ip, IEXEC)) 4457701Ssam goto done; 4467701Ssam done: 4476254Sroot iput(ip); 4486254Sroot } 4496254Sroot u.u_uid = svuid; 4506254Sroot u.u_gid = svgid; 4516254Sroot } 4526254Sroot 4536254Sroot /* 4546574Smckusic * Stat system call. This version follows links. 45537Sbill */ 45637Sbill stat() 45737Sbill { 45837Sbill 45916694Smckusick stat1(FOLLOW); 46037Sbill } 46137Sbill 46237Sbill /* 4636574Smckusic * Lstat system call. This version does not follow links. 4645992Swnj */ 4655992Swnj lstat() 4665992Swnj { 46712756Ssam 46816694Smckusick stat1(NOFOLLOW); 46912756Ssam } 47012756Ssam 47112756Ssam stat1(follow) 47212756Ssam int follow; 47312756Ssam { 4745992Swnj register struct inode *ip; 4755992Swnj register struct a { 4765992Swnj char *fname; 47712756Ssam struct stat *ub; 47816694Smckusick } *uap = (struct a *)u.u_ap; 47912756Ssam struct stat sb; 48016694Smckusick register struct nameidata *ndp = &u.u_nd; 4815992Swnj 48216694Smckusick ndp->ni_nameiop = LOOKUP | follow; 48316694Smckusick ndp->ni_segflg = UIO_USERSPACE; 48416694Smckusick ndp->ni_dirp = uap->fname; 48516694Smckusick ip = namei(ndp); 4865992Swnj if (ip == NULL) 4875992Swnj return; 48813043Ssam (void) ino_stat(ip, &sb); 4895992Swnj iput(ip); 49012756Ssam u.u_error = copyout((caddr_t)&sb, (caddr_t)uap->ub, sizeof (sb)); 4915992Swnj } 4925992Swnj 4935992Swnj /* 4945992Swnj * Return target name of a symbolic link 49537Sbill */ 4965992Swnj readlink() 4975992Swnj { 4985992Swnj register struct inode *ip; 4995992Swnj register struct a { 5005992Swnj char *name; 5015992Swnj char *buf; 5025992Swnj int count; 5037826Sroot } *uap = (struct a *)u.u_ap; 50416694Smckusick register struct nameidata *ndp = &u.u_nd; 5057826Sroot int resid; 5065992Swnj 50716694Smckusick ndp->ni_nameiop = LOOKUP; 50816694Smckusick ndp->ni_segflg = UIO_USERSPACE; 50916694Smckusick ndp->ni_dirp = uap->name; 51016694Smckusick ip = namei(ndp); 5115992Swnj if (ip == NULL) 5125992Swnj return; 5135992Swnj if ((ip->i_mode&IFMT) != IFLNK) { 5145992Swnj u.u_error = ENXIO; 5155992Swnj goto out; 5165992Swnj } 5177826Sroot u.u_error = rdwri(UIO_READ, ip, uap->buf, uap->count, 0, 0, &resid); 5185992Swnj out: 5195992Swnj iput(ip); 5207826Sroot u.u_r.r_val1 = uap->count - resid; 5215992Swnj } 5225992Swnj 5239167Ssam /* 5249167Ssam * Change mode of a file given path name. 5259167Ssam */ 5266254Sroot chmod() 5275992Swnj { 5287701Ssam struct inode *ip; 5297701Ssam struct a { 5306254Sroot char *fname; 5316254Sroot int fmode; 53216694Smckusick } *uap = (struct a *)u.u_ap; 5335992Swnj 53416694Smckusick if ((ip = owner(uap->fname, FOLLOW)) == NULL) 5355992Swnj return; 5367701Ssam chmod1(ip, uap->fmode); 5379167Ssam iput(ip); 5387701Ssam } 5397439Sroot 5409167Ssam /* 5419167Ssam * Change mode of a file given a file descriptor. 5429167Ssam */ 5437701Ssam fchmod() 5447701Ssam { 5457701Ssam struct a { 5467701Ssam int fd; 5477701Ssam int fmode; 54816694Smckusick } *uap = (struct a *)u.u_ap; 5497701Ssam register struct inode *ip; 5507701Ssam register struct file *fp; 5517701Ssam 55212756Ssam fp = getinode(uap->fd); 5537701Ssam if (fp == NULL) 5547701Ssam return; 55512756Ssam ip = (struct inode *)fp->f_data; 5569167Ssam if (u.u_uid != ip->i_uid && !suser()) 5579167Ssam return; 55816664Smckusick ILOCK(ip); 5597701Ssam chmod1(ip, uap->fmode); 56016664Smckusick IUNLOCK(ip); 5617701Ssam } 5627701Ssam 5639167Ssam /* 5649167Ssam * Change the mode on a file. 5659167Ssam * Inode must be locked before calling. 5669167Ssam */ 5677701Ssam chmod1(ip, mode) 5687701Ssam register struct inode *ip; 5697701Ssam register int mode; 5707701Ssam { 5717868Sroot 5726254Sroot ip->i_mode &= ~07777; 5737439Sroot if (u.u_uid) { 5747701Ssam mode &= ~ISVTX; 57511811Ssam if (!groupmember(ip->i_gid)) 57611811Ssam mode &= ~ISGID; 5777439Sroot } 5787701Ssam ip->i_mode |= mode&07777; 5796254Sroot ip->i_flag |= ICHG; 5806254Sroot if (ip->i_flag&ITEXT && (ip->i_mode&ISVTX)==0) 5816254Sroot xrele(ip); 5825992Swnj } 5835992Swnj 5849167Ssam /* 5859167Ssam * Set ownership given a path name. 5869167Ssam */ 5876254Sroot chown() 58837Sbill { 5897701Ssam struct inode *ip; 5907701Ssam struct a { 5916254Sroot char *fname; 5926254Sroot int uid; 5936254Sroot int gid; 59416694Smckusick } *uap = (struct a *)u.u_ap; 59537Sbill 59616694Smckusick if (!suser() || (ip = owner(uap->fname, NOFOLLOW)) == NULL) 59737Sbill return; 59811811Ssam u.u_error = chown1(ip, uap->uid, uap->gid); 5999167Ssam iput(ip); 6007701Ssam } 6017439Sroot 6029167Ssam /* 6039167Ssam * Set ownership given a file descriptor. 6049167Ssam */ 6057701Ssam fchown() 6067701Ssam { 6077701Ssam struct a { 6087701Ssam int fd; 6097701Ssam int uid; 6107701Ssam int gid; 61116694Smckusick } *uap = (struct a *)u.u_ap; 6127701Ssam register struct inode *ip; 6137701Ssam register struct file *fp; 6147701Ssam 61512756Ssam fp = getinode(uap->fd); 6167701Ssam if (fp == NULL) 6177701Ssam return; 61812756Ssam ip = (struct inode *)fp->f_data; 61911821Ssam if (!suser()) 6209167Ssam return; 62116664Smckusick ILOCK(ip); 62211811Ssam u.u_error = chown1(ip, uap->uid, uap->gid); 62316664Smckusick IUNLOCK(ip); 6247701Ssam } 6257701Ssam 6267701Ssam /* 6277701Ssam * Perform chown operation on inode ip; 6287701Ssam * inode must be locked prior to call. 6297701Ssam */ 6307701Ssam chown1(ip, uid, gid) 6317701Ssam register struct inode *ip; 6327701Ssam int uid, gid; 6337701Ssam { 6347701Ssam #ifdef QUOTA 6357701Ssam register long change; 63611811Ssam #endif 6377701Ssam 63811811Ssam if (uid == -1) 63911811Ssam uid = ip->i_uid; 64011811Ssam if (gid == -1) 64111811Ssam gid = ip->i_gid; 64211811Ssam #ifdef QUOTA 64314385Ssam if (ip->i_uid == uid) /* this just speeds things a little */ 6447482Skre change = 0; 64512646Ssam else 64612646Ssam change = ip->i_blocks; 64712646Ssam (void) chkdq(ip, -change, 1); 64812646Ssam (void) chkiq(ip->i_dev, ip, ip->i_uid, 1); 6497482Skre dqrele(ip->i_dquot); 6507482Skre #endif 65111811Ssam ip->i_uid = uid; 65211811Ssam ip->i_gid = gid; 6536254Sroot ip->i_flag |= ICHG; 6546254Sroot if (u.u_ruid != 0) 6556254Sroot ip->i_mode &= ~(ISUID|ISGID); 6567701Ssam #ifdef QUOTA 6577482Skre ip->i_dquot = inoquota(ip); 65812646Ssam (void) chkdq(ip, change, 1); 65912646Ssam (void) chkiq(ip->i_dev, (struct inode *)NULL, uid, 1); 66012646Ssam return (u.u_error); /* should == 0 ALWAYS !! */ 66112646Ssam #else 66212646Ssam return (0); 6637482Skre #endif 66437Sbill } 66537Sbill 66611811Ssam utimes() 66711811Ssam { 66811811Ssam register struct a { 66911811Ssam char *fname; 67011811Ssam struct timeval *tptr; 67111811Ssam } *uap = (struct a *)u.u_ap; 67211811Ssam register struct inode *ip; 67311811Ssam struct timeval tv[2]; 67411811Ssam 67516694Smckusick if ((ip = owner(uap->fname, FOLLOW)) == NULL) 67611811Ssam return; 67711811Ssam u.u_error = copyin((caddr_t)uap->tptr, (caddr_t)tv, sizeof (tv)); 67811811Ssam if (u.u_error == 0) { 67911811Ssam ip->i_flag |= IACC|IUPD|ICHG; 68011811Ssam iupdat(ip, &tv[0], &tv[1], 0); 68111811Ssam } 68211811Ssam iput(ip); 68311811Ssam } 68411811Ssam 6859167Ssam /* 6869167Ssam * Flush any pending I/O. 6879167Ssam */ 6886254Sroot sync() 68937Sbill { 69037Sbill 6918673Sroot update(); 69237Sbill } 6937535Sroot 6949167Ssam /* 6959167Ssam * Truncate a file given its path name. 6969167Ssam */ 6977701Ssam truncate() 6987701Ssam { 6997701Ssam struct a { 7007701Ssam char *fname; 7019167Ssam u_long length; 7027826Sroot } *uap = (struct a *)u.u_ap; 7037701Ssam struct inode *ip; 70416694Smckusick register struct nameidata *ndp = &u.u_nd; 7057701Ssam 70616694Smckusick ndp->ni_nameiop = LOOKUP | FOLLOW; 70716694Smckusick ndp->ni_segflg = UIO_USERSPACE; 70816694Smckusick ndp->ni_dirp = uap->fname; 70916694Smckusick ip = namei(ndp); 7107701Ssam if (ip == NULL) 7117701Ssam return; 7127701Ssam if (access(ip, IWRITE)) 7137701Ssam goto bad; 7147701Ssam if ((ip->i_mode&IFMT) == IFDIR) { 7157701Ssam u.u_error = EISDIR; 7167701Ssam goto bad; 7177701Ssam } 7187701Ssam itrunc(ip, uap->length); 7197701Ssam bad: 7207701Ssam iput(ip); 7217701Ssam } 7227701Ssam 7239167Ssam /* 7249167Ssam * Truncate a file given a file descriptor. 7259167Ssam */ 7267701Ssam ftruncate() 7277701Ssam { 7287701Ssam struct a { 7297701Ssam int fd; 7309167Ssam u_long length; 7317826Sroot } *uap = (struct a *)u.u_ap; 7327701Ssam struct inode *ip; 7337701Ssam struct file *fp; 7347701Ssam 73512756Ssam fp = getinode(uap->fd); 7367701Ssam if (fp == NULL) 7377701Ssam return; 7387701Ssam if ((fp->f_flag&FWRITE) == 0) { 7397701Ssam u.u_error = EINVAL; 7407701Ssam return; 7417701Ssam } 74212756Ssam ip = (struct inode *)fp->f_data; 74316664Smckusick ILOCK(ip); 7447701Ssam itrunc(ip, uap->length); 74516664Smckusick IUNLOCK(ip); 7467701Ssam } 7477701Ssam 7489167Ssam /* 7499167Ssam * Synch an open file. 7509167Ssam */ 7519167Ssam fsync() 7529167Ssam { 7539167Ssam struct a { 7549167Ssam int fd; 7559167Ssam } *uap = (struct a *)u.u_ap; 7569167Ssam struct inode *ip; 7579167Ssam struct file *fp; 7589167Ssam 75912756Ssam fp = getinode(uap->fd); 7609167Ssam if (fp == NULL) 7619167Ssam return; 76212756Ssam ip = (struct inode *)fp->f_data; 76316664Smckusick ILOCK(ip); 7649167Ssam syncip(ip); 76516664Smckusick IUNLOCK(ip); 7669167Ssam } 7679167Ssam 7689167Ssam /* 7699167Ssam * Rename system call. 7709167Ssam * rename("foo", "bar"); 7719167Ssam * is essentially 7729167Ssam * unlink("bar"); 7739167Ssam * link("foo", "bar"); 7749167Ssam * unlink("foo"); 7759167Ssam * but ``atomically''. Can't do full commit without saving state in the 7769167Ssam * inode on disk which isn't feasible at this time. Best we can do is 7779167Ssam * always guarantee the target exists. 7789167Ssam * 7799167Ssam * Basic algorithm is: 7809167Ssam * 7819167Ssam * 1) Bump link count on source while we're linking it to the 7829167Ssam * target. This also insure the inode won't be deleted out 783*16776Smckusick * from underneath us while we work (it may be truncated by 784*16776Smckusick * a concurrent `trunc' or `open' for creation). 7859167Ssam * 2) Link source to destination. If destination already exists, 7869167Ssam * delete it first. 787*16776Smckusick * 3) Unlink source reference to inode if still around. If a 788*16776Smckusick * directory was moved and the parent of the destination 7899167Ssam * is different from the source, patch the ".." entry in the 7909167Ssam * directory. 7919167Ssam * 7929167Ssam * Source and destination must either both be directories, or both 7939167Ssam * not be directories. If target is a directory, it must be empty. 7949167Ssam */ 7957701Ssam rename() 7967701Ssam { 7977701Ssam struct a { 7987701Ssam char *from; 7997701Ssam char *to; 80016694Smckusick } *uap = (struct a *)u.u_ap; 8019167Ssam register struct inode *ip, *xp, *dp; 802*16776Smckusick struct dirtemplate dirbuf; 803*16776Smckusick int doingdirectory = 0, oldparent = 0, newparent = 0; 80416694Smckusick register struct nameidata *ndp = &u.u_nd; 80510051Ssam int error = 0; 8067701Ssam 80716694Smckusick ndp->ni_nameiop = DELETE | LOCKPARENT; 80816694Smckusick ndp->ni_segflg = UIO_USERSPACE; 80916694Smckusick ndp->ni_dirp = uap->from; 81016694Smckusick ip = namei(ndp); 8119167Ssam if (ip == NULL) 8129167Ssam return; 81316694Smckusick dp = ndp->ni_pdir; 8149167Ssam if ((ip->i_mode&IFMT) == IFDIR) { 8159167Ssam register struct direct *d; 8169167Ssam 81716694Smckusick d = &ndp->ni_dent; 8189167Ssam /* 81911641Ssam * Avoid ".", "..", and aliases of "." for obvious reasons. 8209167Ssam */ 82111641Ssam if ((d->d_namlen == 1 && d->d_name[0] == '.') || 82211641Ssam (d->d_namlen == 2 && bcmp(d->d_name, "..", 2) == 0) || 823*16776Smckusick (dp == ip) || (ip->i_flag & IRENAME)) { 82411641Ssam iput(dp); 82511641Ssam if (dp == ip) 82611641Ssam irele(ip); 82711641Ssam else 82810051Ssam iput(ip); 82911641Ssam u.u_error = EINVAL; 83011641Ssam return; 8319167Ssam } 832*16776Smckusick ip->i_flag |= IRENAME; 8339167Ssam oldparent = dp->i_number; 8349167Ssam doingdirectory++; 8359167Ssam } 83611641Ssam iput(dp); 8379167Ssam 8389167Ssam /* 8399167Ssam * 1) Bump link count while we're moving stuff 8409167Ssam * around. If we crash somewhere before 8419167Ssam * completing our work, the link count 8429167Ssam * may be wrong, but correctable. 8439167Ssam */ 8449167Ssam ip->i_nlink++; 8459167Ssam ip->i_flag |= ICHG; 8469167Ssam iupdat(ip, &time, &time, 1); 84716664Smckusick IUNLOCK(ip); 8489167Ssam 8499167Ssam /* 8509167Ssam * When the target exists, both the directory 8519167Ssam * and target inodes are returned locked. 8529167Ssam */ 85316694Smckusick ndp->ni_nameiop = CREATE | LOCKPARENT | NOCACHE; 85416694Smckusick ndp->ni_dirp = (caddr_t)uap->to; 85516694Smckusick xp = namei(ndp); 85610051Ssam if (u.u_error) { 85710051Ssam error = u.u_error; 8589167Ssam goto out; 85910051Ssam } 86016694Smckusick dp = ndp->ni_pdir; 8619167Ssam /* 86211641Ssam * If ".." must be changed (ie the directory gets a new 86312816Smckusick * parent) then the source directory must not be in the 86412816Smckusick * directory heirarchy above the target, as this would 86512816Smckusick * orphan everything below the source directory. Also 86612816Smckusick * the user must have write permission in the source so 86712816Smckusick * as to be able to change "..". We must repeat the call 86812816Smckusick * to namei, as the parent directory is unlocked by the 86912816Smckusick * call to checkpath(). 87011641Ssam */ 871*16776Smckusick if (oldparent != dp->i_number) 872*16776Smckusick newparent = dp->i_number; 873*16776Smckusick if (doingdirectory && newparent) { 87412816Smckusick if (access(ip, IWRITE)) 87512816Smckusick goto bad; 87612816Smckusick do { 87716694Smckusick dp = ndp->ni_pdir; 87812816Smckusick if (xp != NULL) 87912816Smckusick iput(xp); 88012816Smckusick u.u_error = checkpath(ip, dp); 88112816Smckusick if (u.u_error) 88212816Smckusick goto out; 88316694Smckusick xp = namei(ndp); 88412816Smckusick if (u.u_error) { 88512816Smckusick error = u.u_error; 88612816Smckusick goto out; 88712816Smckusick } 88816694Smckusick } while (dp != ndp->ni_pdir); 88912816Smckusick } 89011641Ssam /* 8919167Ssam * 2) If target doesn't exist, link the target 8929167Ssam * to the source and unlink the source. 8939167Ssam * Otherwise, rewrite the target directory 8949167Ssam * entry to reference the source inode and 8959167Ssam * expunge the original entry's existence. 8969167Ssam */ 8979167Ssam if (xp == NULL) { 8989167Ssam if (dp->i_dev != ip->i_dev) { 89910051Ssam error = EXDEV; 9009167Ssam goto bad; 9019167Ssam } 9029167Ssam /* 903*16776Smckusick * Account for ".." in new directory. 904*16776Smckusick * When source and destination have the same 905*16776Smckusick * parent we don't fool with the link count. 9069167Ssam */ 907*16776Smckusick if (doingdirectory && newparent) { 9089167Ssam dp->i_nlink++; 9099167Ssam dp->i_flag |= ICHG; 9109167Ssam iupdat(dp, &time, &time, 1); 9119167Ssam } 91216694Smckusick error = direnter(ip, ndp); 91310850Ssam if (error) 9149167Ssam goto out; 9159167Ssam } else { 9169167Ssam if (xp->i_dev != dp->i_dev || xp->i_dev != ip->i_dev) { 91710051Ssam error = EXDEV; 9189167Ssam goto bad; 9199167Ssam } 9209167Ssam /* 92110590Ssam * Short circuit rename(foo, foo). 92210590Ssam */ 92310590Ssam if (xp->i_number == ip->i_number) 92410590Ssam goto bad; 92510590Ssam /* 92610051Ssam * Target must be empty if a directory 92710051Ssam * and have no links to it. 9289167Ssam * Also, insure source and target are 9299167Ssam * compatible (both directories, or both 9309167Ssam * not directories). 9319167Ssam */ 9329167Ssam if ((xp->i_mode&IFMT) == IFDIR) { 933*16776Smckusick if (!dirempty(xp, dp->i_number) || xp->i_nlink > 2) { 93410051Ssam error = ENOTEMPTY; 9359167Ssam goto bad; 9369167Ssam } 9379167Ssam if (!doingdirectory) { 93810051Ssam error = ENOTDIR; 9399167Ssam goto bad; 9409167Ssam } 941*16776Smckusick cacheinval(dp); 9429167Ssam } else if (doingdirectory) { 94310051Ssam error = EISDIR; 9449167Ssam goto bad; 9459167Ssam } 94616694Smckusick dirrewrite(dp, ip, ndp); 94710051Ssam if (u.u_error) { 94810051Ssam error = u.u_error; 9499167Ssam goto bad1; 95010051Ssam } 9519167Ssam /* 95210051Ssam * Adjust the link count of the target to 95310051Ssam * reflect the dirrewrite above. If this is 95410051Ssam * a directory it is empty and there are 95510051Ssam * no links to it, so we can squash the inode and 95610051Ssam * any space associated with it. We disallowed 95710051Ssam * renaming over top of a directory with links to 958*16776Smckusick * it above, as the remaining link would point to 959*16776Smckusick * a directory without "." or ".." entries. 9609167Ssam */ 96110051Ssam xp->i_nlink--; 9629167Ssam if (doingdirectory) { 96310051Ssam if (--xp->i_nlink != 0) 96410051Ssam panic("rename: linked directory"); 9659167Ssam itrunc(xp, (u_long)0); 96610051Ssam } 9679167Ssam xp->i_flag |= ICHG; 9689167Ssam iput(xp); 96910246Ssam xp = NULL; 9709167Ssam } 9719167Ssam 9729167Ssam /* 9739167Ssam * 3) Unlink the source. 9749167Ssam */ 97516694Smckusick ndp->ni_nameiop = DELETE | LOCKPARENT; 97616694Smckusick ndp->ni_segflg = UIO_USERSPACE; 97716694Smckusick ndp->ni_dirp = uap->from; 978*16776Smckusick xp = namei(ndp); 97916694Smckusick dp = ndp->ni_pdir; 9809167Ssam /* 981*16776Smckusick * Insure that the directory entry still exists and has not 982*16776Smckusick * changed while the new name has been entered. If the source is 983*16776Smckusick * a file then the entry may have been unlinked or renamed. In 984*16776Smckusick * either case there is no further work to be done. If the source 985*16776Smckusick * is a directory then it cannot have been rmdir'ed; its link 986*16776Smckusick * count of three would cause a rmdir to fail with ENOTEMPTY. 987*16776Smckusick * The IRENAME flag insures that it cannot be moved by another 988*16776Smckusick * rename. 9899167Ssam */ 990*16776Smckusick if (dp == NULL || xp != ip) { 991*16776Smckusick if (doingdirectory) 992*16776Smckusick panic("rename: lost entry"); 993*16776Smckusick } else { 9949167Ssam /* 995*16776Smckusick * If the source is a directory with a 996*16776Smckusick * new parent, the link count of the old 997*16776Smckusick * parent directory must be decremented 998*16776Smckusick * and ".." set to point to the new parent. 9999167Ssam */ 1000*16776Smckusick if (doingdirectory && newparent) { 10019167Ssam dp->i_nlink--; 10029167Ssam dp->i_flag |= ICHG; 1003*16776Smckusick error = rdwri(UIO_READ, xp, (caddr_t)&dirbuf, 1004*16776Smckusick sizeof (struct dirtemplate), (off_t)0, 1, 1005*16776Smckusick (int *)0); 1006*16776Smckusick if (error == 0) { 1007*16776Smckusick if (dirbuf.dotdot_namlen != 2 || 1008*16776Smckusick dirbuf.dotdot_name[0] != '.' || 1009*16776Smckusick dirbuf.dotdot_name[1] != '.') { 1010*16776Smckusick printf("rename: mangled dir\n"); 1011*16776Smckusick } else { 1012*16776Smckusick dirbuf.dotdot_ino = newparent; 1013*16776Smckusick (void) rdwri(UIO_WRITE, xp, 1014*16776Smckusick (caddr_t)&dirbuf, 1015*16776Smckusick sizeof (struct dirtemplate), 1016*16776Smckusick (off_t)0, 1, (int *)0); 1017*16776Smckusick cacheinval(dp); 1018*16776Smckusick } 1019*16776Smckusick } 10209167Ssam } 102116694Smckusick if (dirremove(ndp)) { 1022*16776Smckusick xp->i_nlink--; 1023*16776Smckusick xp->i_flag |= ICHG; 10249167Ssam } 1025*16776Smckusick xp->i_flag &= ~IRENAME; 1026*16776Smckusick if (error == 0) /* XXX conservative */ 102710051Ssam error = u.u_error; 10289167Ssam } 10299167Ssam if (dp) 10309167Ssam iput(dp); 1031*16776Smckusick if (xp) 1032*16776Smckusick iput(xp); 1033*16776Smckusick irele(ip); 1034*16776Smckusick if (error) 1035*16776Smckusick u.u_error = error; 1036*16776Smckusick return; 10379167Ssam 10389167Ssam bad: 103910246Ssam iput(dp); 10409167Ssam bad1: 10419167Ssam if (xp) 104210246Ssam iput(xp); 10439167Ssam out: 10449167Ssam ip->i_nlink--; 10459167Ssam ip->i_flag |= ICHG; 10469167Ssam irele(ip); 104710051Ssam if (error) 104810051Ssam u.u_error = error; 10497701Ssam } 10507701Ssam 10517535Sroot /* 10527535Sroot * Make a new file. 10537535Sroot */ 10547535Sroot struct inode * 105516694Smckusick maknode(mode, ndp) 10567535Sroot int mode; 105716694Smckusick register struct nameidata *ndp; 10587535Sroot { 10597535Sroot register struct inode *ip; 106016694Smckusick register struct inode *pdir = ndp->ni_pdir; 10617535Sroot ino_t ipref; 10627535Sroot 10637535Sroot if ((mode & IFMT) == IFDIR) 106416694Smckusick ipref = dirpref(pdir->i_fs); 10657535Sroot else 106616694Smckusick ipref = pdir->i_number; 106716694Smckusick ip = ialloc(pdir, ipref, mode); 10687535Sroot if (ip == NULL) { 106916694Smckusick iput(pdir); 10707701Ssam return (NULL); 10717535Sroot } 10727701Ssam #ifdef QUOTA 10737535Sroot if (ip->i_dquot != NODQUOT) 10747535Sroot panic("maknode: dquot"); 10757535Sroot #endif 10767535Sroot ip->i_flag |= IACC|IUPD|ICHG; 10777535Sroot if ((mode & IFMT) == 0) 10787535Sroot mode |= IFREG; 10797535Sroot ip->i_mode = mode & ~u.u_cmask; 10807535Sroot ip->i_nlink = 1; 10817535Sroot ip->i_uid = u.u_uid; 108216694Smckusick ip->i_gid = pdir->i_gid; 108311811Ssam if (ip->i_mode & ISGID && !groupmember(ip->i_gid)) 108411811Ssam ip->i_mode &= ~ISGID; 10857701Ssam #ifdef QUOTA 10867535Sroot ip->i_dquot = inoquota(ip); 10877535Sroot #endif 10887535Sroot 10897535Sroot /* 10907535Sroot * Make sure inode goes to disk before directory entry. 10917535Sroot */ 10928673Sroot iupdat(ip, &time, &time, 1); 109316694Smckusick u.u_error = direnter(ip, ndp); 10947535Sroot if (u.u_error) { 10957535Sroot /* 109610850Ssam * Write error occurred trying to update directory 109710850Ssam * so must deallocate the inode. 10987535Sroot */ 10997535Sroot ip->i_nlink = 0; 11007535Sroot ip->i_flag |= ICHG; 11017535Sroot iput(ip); 11027701Ssam return (NULL); 11037535Sroot } 11047701Ssam return (ip); 11057535Sroot } 110612756Ssam 110712756Ssam /* 110812756Ssam * A virgin directory (no blushing please). 110912756Ssam */ 111012756Ssam struct dirtemplate mastertemplate = { 111112756Ssam 0, 12, 1, ".", 111212756Ssam 0, DIRBLKSIZ - 12, 2, ".." 111312756Ssam }; 111412756Ssam 111512756Ssam /* 111612756Ssam * Mkdir system call 111712756Ssam */ 111812756Ssam mkdir() 111912756Ssam { 112012756Ssam struct a { 112112756Ssam char *name; 112212756Ssam int dmode; 112316694Smckusick } *uap = (struct a *)u.u_ap; 112412756Ssam register struct inode *ip, *dp; 112512756Ssam struct dirtemplate dirtemplate; 112616694Smckusick register struct nameidata *ndp = &u.u_nd; 112712756Ssam 112816694Smckusick ndp->ni_nameiop = CREATE; 112916694Smckusick ndp->ni_segflg = UIO_USERSPACE; 113016694Smckusick ndp->ni_dirp = uap->name; 113116694Smckusick ip = namei(ndp); 113212756Ssam if (u.u_error) 113312756Ssam return; 113412756Ssam if (ip != NULL) { 113512756Ssam iput(ip); 113612756Ssam u.u_error = EEXIST; 113712756Ssam return; 113812756Ssam } 113916694Smckusick dp = ndp->ni_pdir; 114012756Ssam uap->dmode &= 0777; 114112756Ssam uap->dmode |= IFDIR; 114212756Ssam /* 114312756Ssam * Must simulate part of maknode here 114412756Ssam * in order to acquire the inode, but 114512756Ssam * not have it entered in the parent 114612756Ssam * directory. The entry is made later 114712756Ssam * after writing "." and ".." entries out. 114812756Ssam */ 114912756Ssam ip = ialloc(dp, dirpref(dp->i_fs), uap->dmode); 115012756Ssam if (ip == NULL) { 115112756Ssam iput(dp); 115212756Ssam return; 115312756Ssam } 115412756Ssam #ifdef QUOTA 115512756Ssam if (ip->i_dquot != NODQUOT) 115612756Ssam panic("mkdir: dquot"); 115712756Ssam #endif 115812756Ssam ip->i_flag |= IACC|IUPD|ICHG; 115912756Ssam ip->i_mode = uap->dmode & ~u.u_cmask; 116012756Ssam ip->i_nlink = 2; 116112756Ssam ip->i_uid = u.u_uid; 116212756Ssam ip->i_gid = dp->i_gid; 116312756Ssam #ifdef QUOTA 116412756Ssam ip->i_dquot = inoquota(ip); 116512756Ssam #endif 116612756Ssam iupdat(ip, &time, &time, 1); 116712756Ssam 116812756Ssam /* 116912756Ssam * Bump link count in parent directory 117012756Ssam * to reflect work done below. Should 117112756Ssam * be done before reference is created 117212756Ssam * so reparation is possible if we crash. 117312756Ssam */ 117412756Ssam dp->i_nlink++; 117512756Ssam dp->i_flag |= ICHG; 117612756Ssam iupdat(dp, &time, &time, 1); 117712756Ssam 117812756Ssam /* 117912756Ssam * Initialize directory with "." 118012756Ssam * and ".." from static template. 118112756Ssam */ 118212756Ssam dirtemplate = mastertemplate; 118312756Ssam dirtemplate.dot_ino = ip->i_number; 118412756Ssam dirtemplate.dotdot_ino = dp->i_number; 118512756Ssam u.u_error = rdwri(UIO_WRITE, ip, (caddr_t)&dirtemplate, 118612756Ssam sizeof (dirtemplate), (off_t)0, 1, (int *)0); 118712756Ssam if (u.u_error) { 118812756Ssam dp->i_nlink--; 118912756Ssam dp->i_flag |= ICHG; 119012756Ssam goto bad; 119112756Ssam } 119212756Ssam /* 119312756Ssam * Directory all set up, now 119412756Ssam * install the entry for it in 119512756Ssam * the parent directory. 119612756Ssam */ 119716694Smckusick u.u_error = direnter(ip, ndp); 119812756Ssam dp = NULL; 119912756Ssam if (u.u_error) { 120016694Smckusick ndp->ni_nameiop = LOOKUP | NOCACHE; 120116694Smckusick ndp->ni_segflg = UIO_USERSPACE; 120216694Smckusick ndp->ni_dirp = uap->name; 120316694Smckusick dp = namei(ndp); 120412756Ssam if (dp) { 120512756Ssam dp->i_nlink--; 120612756Ssam dp->i_flag |= ICHG; 120712756Ssam } 120812756Ssam } 120912756Ssam bad: 121012756Ssam /* 121112756Ssam * No need to do an explicit itrunc here, 121212756Ssam * irele will do this for us because we set 121312756Ssam * the link count to 0. 121412756Ssam */ 121512756Ssam if (u.u_error) { 121612756Ssam ip->i_nlink = 0; 121712756Ssam ip->i_flag |= ICHG; 121812756Ssam } 121912756Ssam if (dp) 122012756Ssam iput(dp); 122112756Ssam iput(ip); 122212756Ssam } 122312756Ssam 122412756Ssam /* 122512756Ssam * Rmdir system call. 122612756Ssam */ 122712756Ssam rmdir() 122812756Ssam { 122912756Ssam struct a { 123012756Ssam char *name; 123116694Smckusick } *uap = (struct a *)u.u_ap; 123212756Ssam register struct inode *ip, *dp; 123316694Smckusick register struct nameidata *ndp = &u.u_nd; 123412756Ssam 123516694Smckusick ndp->ni_nameiop = DELETE | LOCKPARENT; 123616694Smckusick ndp->ni_segflg = UIO_USERSPACE; 123716694Smckusick ndp->ni_dirp = uap->name; 123816694Smckusick ip = namei(ndp); 123912756Ssam if (ip == NULL) 124012756Ssam return; 124116694Smckusick dp = ndp->ni_pdir; 124212756Ssam /* 124312756Ssam * No rmdir "." please. 124412756Ssam */ 124512756Ssam if (dp == ip) { 124612756Ssam irele(dp); 124712756Ssam iput(ip); 124812756Ssam u.u_error = EINVAL; 124912756Ssam return; 125012756Ssam } 125112756Ssam if ((ip->i_mode&IFMT) != IFDIR) { 125212756Ssam u.u_error = ENOTDIR; 125312756Ssam goto out; 125412756Ssam } 125512756Ssam /* 125612756Ssam * Don't remove a mounted on directory. 125712756Ssam */ 125812756Ssam if (ip->i_dev != dp->i_dev) { 125912756Ssam u.u_error = EBUSY; 126012756Ssam goto out; 126112756Ssam } 126212756Ssam /* 126312756Ssam * Verify the directory is empty (and valid). 126412756Ssam * (Rmdir ".." won't be valid since 126512756Ssam * ".." will contain a reference to 126612756Ssam * the current directory and thus be 126712756Ssam * non-empty.) 126812756Ssam */ 1269*16776Smckusick if (ip->i_nlink != 2 || !dirempty(ip, dp->i_number)) { 127012756Ssam u.u_error = ENOTEMPTY; 127112756Ssam goto out; 127212756Ssam } 127312756Ssam /* 127412756Ssam * Delete reference to directory before purging 127512756Ssam * inode. If we crash in between, the directory 127612756Ssam * will be reattached to lost+found, 127712756Ssam */ 127816694Smckusick if (dirremove(ndp) == 0) 127912756Ssam goto out; 128012756Ssam dp->i_nlink--; 128112756Ssam dp->i_flag |= ICHG; 1282*16776Smckusick cacheinval(dp); 128312756Ssam iput(dp); 128412756Ssam dp = NULL; 128512756Ssam /* 128612756Ssam * Truncate inode. The only stuff left 128712756Ssam * in the directory is "." and "..". The 128812756Ssam * "." reference is inconsequential since 128912756Ssam * we're quashing it. The ".." reference 129012756Ssam * has already been adjusted above. We've 129112756Ssam * removed the "." reference and the reference 129212756Ssam * in the parent directory, but there may be 129312756Ssam * other hard links so decrement by 2 and 129412756Ssam * worry about them later. 129512756Ssam */ 129612756Ssam ip->i_nlink -= 2; 129712756Ssam itrunc(ip, (u_long)0); 129816739Smckusick cacheinval(ip); 129912756Ssam out: 130012756Ssam if (dp) 130112756Ssam iput(dp); 130212756Ssam iput(ip); 130312756Ssam } 130412756Ssam 130512756Ssam struct file * 130612756Ssam getinode(fdes) 130712756Ssam int fdes; 130812756Ssam { 130916540Ssam struct file *fp; 131012756Ssam 131116540Ssam if ((unsigned)fdes >= NOFILE || (fp = u.u_ofile[fdes]) == NULL) { 131216540Ssam u.u_error = EBADF; 131316540Ssam return ((struct file *)0); 131416540Ssam } 131512756Ssam if (fp->f_type != DTYPE_INODE) { 131612756Ssam u.u_error = EINVAL; 131716540Ssam return ((struct file *)0); 131812756Ssam } 131912756Ssam return (fp); 132012756Ssam } 132112756Ssam 132212756Ssam /* 132312756Ssam * mode mask for creation of files 132412756Ssam */ 132512756Ssam umask() 132612756Ssam { 132712756Ssam register struct a { 132812756Ssam int mask; 132916694Smckusick } *uap = (struct a *)u.u_ap; 133012756Ssam 133112756Ssam u.u_r.r_val1 = u.u_cmask; 133212756Ssam u.u_cmask = uap->mask & 07777; 133312756Ssam } 1334