123405Smckusick /* 237737Smckusick * Copyright (c) 1982, 1986, 1989 Regents of the University of California. 337737Smckusick * All rights reserved. 423405Smckusick * 544539Sbostic * %sccs.include.redist.c% 637737Smckusick * 7*47234Smckusick * @(#)lfs_vnops.c 7.57 (Berkeley) 03/11/91 823405Smckusick */ 937Sbill 1017101Sbloom #include "param.h" 1117101Sbloom #include "systm.h" 1217101Sbloom #include "user.h" 1317101Sbloom #include "kernel.h" 1417101Sbloom #include "file.h" 1517101Sbloom #include "stat.h" 1617101Sbloom #include "buf.h" 1717101Sbloom #include "proc.h" 1817101Sbloom #include "socket.h" 1917101Sbloom #include "socketvar.h" 2037737Smckusick #include "conf.h" 2117101Sbloom #include "mount.h" 2237737Smckusick #include "vnode.h" 2340653Smckusick #include "specdev.h" 2446207Smckusick #include "fcntl.h" 2546207Smckusick #include "malloc.h" 2646207Smckusick #include "../ufs/lockf.h" 2741312Smckusick #include "../ufs/quota.h" 2837737Smckusick #include "../ufs/inode.h" 2937737Smckusick #include "../ufs/fs.h" 3037Sbill 319167Ssam /* 3237737Smckusick * Global vfs data structures for ufs 339167Ssam */ 346254Sroot 3537737Smckusick int ufs_lookup(), 3637737Smckusick ufs_create(), 3737737Smckusick ufs_mknod(), 3837737Smckusick ufs_open(), 3937737Smckusick ufs_close(), 4037737Smckusick ufs_access(), 4137737Smckusick ufs_getattr(), 4237737Smckusick ufs_setattr(), 4337737Smckusick ufs_read(), 4437737Smckusick ufs_write(), 4537737Smckusick ufs_ioctl(), 4637737Smckusick ufs_select(), 4737737Smckusick ufs_mmap(), 4837737Smckusick ufs_fsync(), 4937737Smckusick ufs_seek(), 5037737Smckusick ufs_remove(), 5137737Smckusick ufs_link(), 5237737Smckusick ufs_rename(), 5337737Smckusick ufs_mkdir(), 5437737Smckusick ufs_rmdir(), 5537737Smckusick ufs_symlink(), 5637737Smckusick ufs_readdir(), 5737737Smckusick ufs_readlink(), 5837737Smckusick ufs_abortop(), 5937737Smckusick ufs_inactive(), 6039391Smckusick ufs_reclaim(), 6137737Smckusick ufs_lock(), 6237737Smckusick ufs_unlock(), 6337737Smckusick ufs_bmap(), 6439674Smckusick ufs_strategy(), 6539909Smckusick ufs_print(), 6646207Smckusick ufs_islocked(), 6746207Smckusick ufs_advlock(); 686254Sroot 6937737Smckusick struct vnodeops ufs_vnodeops = { 7039674Smckusick ufs_lookup, /* lookup */ 7139674Smckusick ufs_create, /* create */ 7239674Smckusick ufs_mknod, /* mknod */ 7339674Smckusick ufs_open, /* open */ 7439674Smckusick ufs_close, /* close */ 7539674Smckusick ufs_access, /* access */ 7639674Smckusick ufs_getattr, /* getattr */ 7739674Smckusick ufs_setattr, /* setattr */ 7839674Smckusick ufs_read, /* read */ 7939674Smckusick ufs_write, /* write */ 8039674Smckusick ufs_ioctl, /* ioctl */ 8139674Smckusick ufs_select, /* select */ 8239674Smckusick ufs_mmap, /* mmap */ 8339674Smckusick ufs_fsync, /* fsync */ 8439674Smckusick ufs_seek, /* seek */ 8539674Smckusick ufs_remove, /* remove */ 8639674Smckusick ufs_link, /* link */ 8739674Smckusick ufs_rename, /* rename */ 8839674Smckusick ufs_mkdir, /* mkdir */ 8939674Smckusick ufs_rmdir, /* rmdir */ 9039674Smckusick ufs_symlink, /* symlink */ 9139674Smckusick ufs_readdir, /* readdir */ 9239674Smckusick ufs_readlink, /* readlink */ 9339674Smckusick ufs_abortop, /* abortop */ 9439674Smckusick ufs_inactive, /* inactive */ 9539674Smckusick ufs_reclaim, /* reclaim */ 9639674Smckusick ufs_lock, /* lock */ 9739674Smckusick ufs_unlock, /* unlock */ 9839674Smckusick ufs_bmap, /* bmap */ 9939674Smckusick ufs_strategy, /* strategy */ 10039674Smckusick ufs_print, /* print */ 10139909Smckusick ufs_islocked, /* islocked */ 10246207Smckusick ufs_advlock, /* advlock */ 10337737Smckusick }; 1046254Sroot 10539435Smckusick int spec_lookup(), 10639435Smckusick spec_open(), 10739628Smckusick ufsspec_read(), 10839628Smckusick ufsspec_write(), 10939435Smckusick spec_strategy(), 11039674Smckusick spec_bmap(), 11139435Smckusick spec_ioctl(), 11239435Smckusick spec_select(), 11339628Smckusick ufsspec_close(), 11446207Smckusick spec_advlock(), 11539435Smckusick spec_badop(), 11646679Smckusick nullop(); 11739435Smckusick 11839435Smckusick struct vnodeops spec_inodeops = { 11939597Smckusick spec_lookup, /* lookup */ 12039597Smckusick spec_badop, /* create */ 12139597Smckusick spec_badop, /* mknod */ 12239597Smckusick spec_open, /* open */ 12339628Smckusick ufsspec_close, /* close */ 12439597Smckusick ufs_access, /* access */ 12539597Smckusick ufs_getattr, /* getattr */ 12639597Smckusick ufs_setattr, /* setattr */ 12739628Smckusick ufsspec_read, /* read */ 12839628Smckusick ufsspec_write, /* write */ 12939597Smckusick spec_ioctl, /* ioctl */ 13039597Smckusick spec_select, /* select */ 13139597Smckusick spec_badop, /* mmap */ 13246679Smckusick nullop, /* fsync */ 13339597Smckusick spec_badop, /* seek */ 13439597Smckusick spec_badop, /* remove */ 13539597Smckusick spec_badop, /* link */ 13639597Smckusick spec_badop, /* rename */ 13739597Smckusick spec_badop, /* mkdir */ 13839597Smckusick spec_badop, /* rmdir */ 13939597Smckusick spec_badop, /* symlink */ 14039597Smckusick spec_badop, /* readdir */ 14139597Smckusick spec_badop, /* readlink */ 14239597Smckusick spec_badop, /* abortop */ 14339597Smckusick ufs_inactive, /* inactive */ 14439597Smckusick ufs_reclaim, /* reclaim */ 14539597Smckusick ufs_lock, /* lock */ 14639597Smckusick ufs_unlock, /* unlock */ 14739674Smckusick spec_bmap, /* bmap */ 14839597Smckusick spec_strategy, /* strategy */ 14939674Smckusick ufs_print, /* print */ 15039909Smckusick ufs_islocked, /* islocked */ 15146207Smckusick spec_advlock, /* advlock */ 15239435Smckusick }; 15339435Smckusick 15440290Smckusick #ifdef FIFO 15540290Smckusick int fifo_lookup(), 15640290Smckusick fifo_open(), 15740290Smckusick ufsfifo_read(), 15840290Smckusick ufsfifo_write(), 15940290Smckusick fifo_bmap(), 16040290Smckusick fifo_ioctl(), 16140290Smckusick fifo_select(), 16240290Smckusick ufsfifo_close(), 16340290Smckusick fifo_print(), 16446207Smckusick fifo_advlock(), 16546679Smckusick fifo_badop(); 16640290Smckusick 16740290Smckusick struct vnodeops fifo_inodeops = { 16840290Smckusick fifo_lookup, /* lookup */ 16940290Smckusick fifo_badop, /* create */ 17040290Smckusick fifo_badop, /* mknod */ 17140290Smckusick fifo_open, /* open */ 17240290Smckusick ufsfifo_close, /* close */ 17340290Smckusick ufs_access, /* access */ 17440290Smckusick ufs_getattr, /* getattr */ 17540290Smckusick ufs_setattr, /* setattr */ 17640290Smckusick ufsfifo_read, /* read */ 17740290Smckusick ufsfifo_write, /* write */ 17840290Smckusick fifo_ioctl, /* ioctl */ 17940290Smckusick fifo_select, /* select */ 18040290Smckusick fifo_badop, /* mmap */ 18146679Smckusick nullop, /* fsync */ 18240290Smckusick fifo_badop, /* seek */ 18340290Smckusick fifo_badop, /* remove */ 18440290Smckusick fifo_badop, /* link */ 18540290Smckusick fifo_badop, /* rename */ 18640290Smckusick fifo_badop, /* mkdir */ 18740290Smckusick fifo_badop, /* rmdir */ 18840290Smckusick fifo_badop, /* symlink */ 18940290Smckusick fifo_badop, /* readdir */ 19040290Smckusick fifo_badop, /* readlink */ 19140290Smckusick fifo_badop, /* abortop */ 19240290Smckusick ufs_inactive, /* inactive */ 19340290Smckusick ufs_reclaim, /* reclaim */ 19440290Smckusick ufs_lock, /* lock */ 19540290Smckusick ufs_unlock, /* unlock */ 19640290Smckusick fifo_bmap, /* bmap */ 19740290Smckusick fifo_badop, /* strategy */ 19840290Smckusick ufs_print, /* print */ 19940290Smckusick ufs_islocked, /* islocked */ 20046207Smckusick fifo_advlock, /* advlock */ 20137737Smckusick }; 20240290Smckusick #endif /* FIFO */ 20340290Smckusick 20440290Smckusick enum vtype iftovt_tab[16] = { 20540290Smckusick VNON, VFIFO, VCHR, VNON, VDIR, VNON, VBLK, VNON, 20640290Smckusick VREG, VNON, VLNK, VNON, VSOCK, VNON, VNON, VBAD, 20737737Smckusick }; 20840290Smckusick int vttoif_tab[9] = { 20940290Smckusick 0, IFREG, IFDIR, IFBLK, IFCHR, IFLNK, IFSOCK, IFIFO, IFMT, 21040290Smckusick }; 2116254Sroot 2129167Ssam /* 21337737Smckusick * Create a regular file 2149167Ssam */ 21537737Smckusick ufs_create(ndp, vap) 21637737Smckusick struct nameidata *ndp; 21737737Smckusick struct vattr *vap; 2186254Sroot { 21937737Smckusick struct inode *ip; 22037737Smckusick int error; 2216254Sroot 22237737Smckusick if (error = maknode(MAKEIMODE(vap->va_type, vap->va_mode), ndp, &ip)) 22337737Smckusick return (error); 22437737Smckusick ndp->ni_vp = ITOV(ip); 22537737Smckusick return (0); 2266254Sroot } 2276254Sroot 22837Sbill /* 22937737Smckusick * Mknod vnode call 2306254Sroot */ 23137737Smckusick /* ARGSUSED */ 23237737Smckusick ufs_mknod(ndp, vap, cred) 23337737Smckusick struct nameidata *ndp; 23437737Smckusick struct ucred *cred; 23537737Smckusick struct vattr *vap; 2366254Sroot { 23739435Smckusick register struct vnode *vp; 23837737Smckusick struct inode *ip; 23937737Smckusick int error; 2406254Sroot 24137737Smckusick if (error = maknode(MAKEIMODE(vap->va_type, vap->va_mode), ndp, &ip)) 24237737Smckusick return (error); 24340290Smckusick ip->i_flag |= IACC|IUPD|ICHG; 24440290Smckusick if (vap->va_rdev != VNOVAL) { 24537737Smckusick /* 24637737Smckusick * Want to be able to use this to make badblock 24737737Smckusick * inodes, so don't truncate the dev number. 24837737Smckusick */ 24939608Smckusick ip->i_rdev = vap->va_rdev; 25012756Ssam } 25137737Smckusick /* 25237737Smckusick * Remove inode so that it will be reloaded by iget and 25337737Smckusick * checked to see if it is an alias of an existing entry 25437737Smckusick * in the inode cache. 25537737Smckusick */ 25640290Smckusick vp = ITOV(ip); 25740290Smckusick vput(vp); 25839435Smckusick vp->v_type = VNON; 25939435Smckusick vgone(vp); 26037737Smckusick return (0); 2616254Sroot } 2626254Sroot 2636254Sroot /* 26437737Smckusick * Open called. 26537737Smckusick * 26637737Smckusick * Nothing to do. 2676254Sroot */ 26837737Smckusick /* ARGSUSED */ 26937737Smckusick ufs_open(vp, mode, cred) 27037737Smckusick struct vnode *vp; 27137737Smckusick int mode; 27237737Smckusick struct ucred *cred; 2736254Sroot { 2746254Sroot 27537737Smckusick return (0); 2766254Sroot } 2776254Sroot 2786254Sroot /* 27937737Smckusick * Close called 28037737Smckusick * 28137737Smckusick * Update the times on the inode. 2826254Sroot */ 28337737Smckusick /* ARGSUSED */ 28437737Smckusick ufs_close(vp, fflag, cred) 28537737Smckusick struct vnode *vp; 28637737Smckusick int fflag; 28737737Smckusick struct ucred *cred; 2886254Sroot { 28937737Smckusick register struct inode *ip = VTOI(vp); 2906254Sroot 29139815Smckusick if (vp->v_usecount > 1 && !(ip->i_flag & ILOCKED)) 29237737Smckusick ITIMES(ip, &time, &time); 29337737Smckusick return (0); 2946254Sroot } 2956254Sroot 29641312Smckusick /* 29741312Smckusick * Check mode permission on inode pointer. Mode is READ, WRITE or EXEC. 29841312Smckusick * The mode is shifted to select the owner/group/other fields. The 29941312Smckusick * super user is granted all permissions. 30041312Smckusick */ 30137737Smckusick ufs_access(vp, mode, cred) 30237737Smckusick struct vnode *vp; 30341312Smckusick register int mode; 30437737Smckusick struct ucred *cred; 3056254Sroot { 30641312Smckusick register struct inode *ip = VTOI(vp); 30741312Smckusick register gid_t *gp; 30841312Smckusick int i, error; 3096254Sroot 31041312Smckusick #ifdef DIAGNOSTIC 31141312Smckusick if (!VOP_ISLOCKED(vp)) { 31241312Smckusick vprint("ufs_access: not locked", vp); 31341312Smckusick panic("ufs_access: not locked"); 31441312Smckusick } 31541312Smckusick #endif 31641312Smckusick #ifdef QUOTA 31741312Smckusick if (mode & VWRITE) { 31841312Smckusick switch (vp->v_type) { 31941312Smckusick case VREG: case VDIR: case VLNK: 32041312Smckusick if (error = getinoquota(ip)) 32141312Smckusick return (error); 32241312Smckusick } 32341312Smckusick } 32441312Smckusick #endif /* QUOTA */ 32541312Smckusick /* 32641312Smckusick * If you're the super-user, you always get access. 32741312Smckusick */ 32841312Smckusick if (cred->cr_uid == 0) 32941312Smckusick return (0); 33041312Smckusick /* 33141312Smckusick * Access check is based on only one of owner, group, public. 33241312Smckusick * If not owner, then check group. If not a member of the 33341312Smckusick * group, then check public access. 33441312Smckusick */ 33541312Smckusick if (cred->cr_uid != ip->i_uid) { 33641312Smckusick mode >>= 3; 33741312Smckusick gp = cred->cr_groups; 33841312Smckusick for (i = 0; i < cred->cr_ngroups; i++, gp++) 33941312Smckusick if (ip->i_gid == *gp) 34041312Smckusick goto found; 34141312Smckusick mode >>= 3; 34241312Smckusick found: 34341312Smckusick ; 34441312Smckusick } 34541312Smckusick if ((ip->i_mode & mode) != 0) 34641312Smckusick return (0); 34741312Smckusick return (EACCES); 3486254Sroot } 3496254Sroot 35037737Smckusick /* ARGSUSED */ 35137737Smckusick ufs_getattr(vp, vap, cred) 35237737Smckusick struct vnode *vp; 35337737Smckusick register struct vattr *vap; 35437737Smckusick struct ucred *cred; 3556254Sroot { 35637737Smckusick register struct inode *ip = VTOI(vp); 3576254Sroot 35837737Smckusick ITIMES(ip, &time, &time); 3596254Sroot /* 36037737Smckusick * Copy from inode table 3616254Sroot */ 36237737Smckusick vap->va_fsid = ip->i_dev; 36337737Smckusick vap->va_fileid = ip->i_number; 36437737Smckusick vap->va_mode = ip->i_mode & ~IFMT; 36537737Smckusick vap->va_nlink = ip->i_nlink; 36637737Smckusick vap->va_uid = ip->i_uid; 36737737Smckusick vap->va_gid = ip->i_gid; 36837737Smckusick vap->va_rdev = (dev_t)ip->i_rdev; 36941312Smckusick #ifdef tahoe 37041312Smckusick vap->va_size = ip->i_size; 37141312Smckusick vap->va_size_rsv = 0; 37241312Smckusick #else 37340641Smckusick vap->va_qsize = ip->i_din.di_qsize; 37441312Smckusick #endif 37537737Smckusick vap->va_atime.tv_sec = ip->i_atime; 37638578Smckusick vap->va_atime.tv_usec = 0; 37737737Smckusick vap->va_mtime.tv_sec = ip->i_mtime; 37838578Smckusick vap->va_mtime.tv_usec = 0; 37937737Smckusick vap->va_ctime.tv_sec = ip->i_ctime; 38038578Smckusick vap->va_ctime.tv_usec = 0; 38138254Smckusick vap->va_flags = ip->i_flags; 38238254Smckusick vap->va_gen = ip->i_gen; 38337737Smckusick /* this doesn't belong here */ 38437737Smckusick if (vp->v_type == VBLK) 38537737Smckusick vap->va_blocksize = BLKDEV_IOSIZE; 38637737Smckusick else if (vp->v_type == VCHR) 38737737Smckusick vap->va_blocksize = MAXBSIZE; 3887142Smckusick else 38937737Smckusick vap->va_blocksize = ip->i_fs->fs_bsize; 39038657Smckusick vap->va_bytes = dbtob(ip->i_blocks); 39140641Smckusick vap->va_bytes_rsv = 0; 39237737Smckusick vap->va_type = vp->v_type; 39337737Smckusick return (0); 3946254Sroot } 3956254Sroot 3966254Sroot /* 39737737Smckusick * Set attribute vnode op. called from several syscalls 3986254Sroot */ 39937737Smckusick ufs_setattr(vp, vap, cred) 40037737Smckusick register struct vnode *vp; 40137737Smckusick register struct vattr *vap; 40237737Smckusick register struct ucred *cred; 4036254Sroot { 40437737Smckusick register struct inode *ip = VTOI(vp); 40537737Smckusick int error = 0; 4066254Sroot 40737737Smckusick /* 40837737Smckusick * Check for unsetable attributes. 40937737Smckusick */ 41037737Smckusick if ((vap->va_type != VNON) || (vap->va_nlink != VNOVAL) || 41137737Smckusick (vap->va_fsid != VNOVAL) || (vap->va_fileid != VNOVAL) || 41237737Smckusick (vap->va_blocksize != VNOVAL) || (vap->va_rdev != VNOVAL) || 41338254Smckusick ((int)vap->va_bytes != VNOVAL) || (vap->va_gen != VNOVAL)) { 41437737Smckusick return (EINVAL); 41516540Ssam } 41637737Smckusick /* 41737737Smckusick * Go through the fields and update iff not VNOVAL. 41837737Smckusick */ 41937737Smckusick if (vap->va_uid != (u_short)VNOVAL || vap->va_gid != (u_short)VNOVAL) 42037737Smckusick if (error = chown1(vp, vap->va_uid, vap->va_gid, cred)) 42137737Smckusick return (error); 42237737Smckusick if (vap->va_size != VNOVAL) { 42337737Smckusick if (vp->v_type == VDIR) 42437737Smckusick return (EISDIR); 42539674Smckusick if (error = itrunc(ip, vap->va_size, 0)) /* XXX IO_SYNC? */ 42637737Smckusick return (error); 42713878Ssam } 42837737Smckusick if (vap->va_atime.tv_sec != VNOVAL || vap->va_mtime.tv_sec != VNOVAL) { 42937773Smckusick if (cred->cr_uid != ip->i_uid && 43037773Smckusick (error = suser(cred, &u.u_acflag))) 43137773Smckusick return (error); 43237737Smckusick if (vap->va_atime.tv_sec != VNOVAL) 43337737Smckusick ip->i_flag |= IACC; 43437737Smckusick if (vap->va_mtime.tv_sec != VNOVAL) 43537737Smckusick ip->i_flag |= IUPD; 43637737Smckusick ip->i_flag |= ICHG; 43737737Smckusick if (error = iupdat(ip, &vap->va_atime, &vap->va_mtime, 1)) 43837737Smckusick return (error); 4396254Sroot } 44037737Smckusick if (vap->va_mode != (u_short)VNOVAL) 44137737Smckusick error = chmod1(vp, (int)vap->va_mode, cred); 44238254Smckusick if (vap->va_flags != VNOVAL) { 44338254Smckusick if (cred->cr_uid != ip->i_uid && 44438254Smckusick (error = suser(cred, &u.u_acflag))) 44538254Smckusick return (error); 44638254Smckusick if (cred->cr_uid == 0) { 44738254Smckusick ip->i_flags = vap->va_flags; 44838254Smckusick } else { 44938254Smckusick ip->i_flags &= 0xffff0000; 45038254Smckusick ip->i_flags |= (vap->va_flags & 0xffff); 45138254Smckusick } 45238254Smckusick ip->i_flag |= ICHG; 45338254Smckusick } 45437737Smckusick return (error); 4556254Sroot } 4566254Sroot 4576254Sroot /* 4589167Ssam * Change the mode on a file. 4599167Ssam * Inode must be locked before calling. 4609167Ssam */ 46137737Smckusick chmod1(vp, mode, cred) 46237737Smckusick register struct vnode *vp; 4637701Ssam register int mode; 46437737Smckusick struct ucred *cred; 4657701Ssam { 46637737Smckusick register struct inode *ip = VTOI(vp); 46737773Smckusick int error; 4687868Sroot 46937773Smckusick if (cred->cr_uid != ip->i_uid && 47037773Smckusick (error = suser(cred, &u.u_acflag))) 47137773Smckusick return (error); 47237737Smckusick if (cred->cr_uid) { 47346206Smckusick if (vp->v_type != VDIR && (mode & ISVTX)) 47445783Sbostic return (EFTYPE); 47546206Smckusick if (!groupmember(ip->i_gid, cred) && (mode & ISGID)) 47645783Sbostic return (EPERM); 4777439Sroot } 47845783Sbostic ip->i_mode &= ~07777; 47937737Smckusick ip->i_mode |= mode & 07777; 4806254Sroot ip->i_flag |= ICHG; 48137737Smckusick if ((vp->v_flag & VTEXT) && (ip->i_mode & ISVTX) == 0) 48245722Smckusick (void) vnode_pager_uncache(vp); 48321015Smckusick return (0); 4845992Swnj } 4855992Swnj 4869167Ssam /* 4877701Ssam * Perform chown operation on inode ip; 4887701Ssam * inode must be locked prior to call. 4897701Ssam */ 49037737Smckusick chown1(vp, uid, gid, cred) 49137737Smckusick register struct vnode *vp; 49237737Smckusick uid_t uid; 49337737Smckusick gid_t gid; 49437737Smckusick struct ucred *cred; 4957701Ssam { 49637737Smckusick register struct inode *ip = VTOI(vp); 49741312Smckusick uid_t ouid; 49841312Smckusick gid_t ogid; 49941312Smckusick int error = 0; 5007701Ssam #ifdef QUOTA 50141312Smckusick register int i; 50241312Smckusick long change; 50311811Ssam #endif 5047701Ssam 50537737Smckusick if (uid == (u_short)VNOVAL) 50611811Ssam uid = ip->i_uid; 50737737Smckusick if (gid == (u_short)VNOVAL) 50811811Ssam gid = ip->i_gid; 50936614Sbostic /* 51036614Sbostic * If we don't own the file, are trying to change the owner 51136614Sbostic * of the file, or are not a member of the target group, 51236614Sbostic * the caller must be superuser or the call fails. 51336614Sbostic */ 51437737Smckusick if ((cred->cr_uid != ip->i_uid || uid != ip->i_uid || 51537737Smckusick !groupmember((gid_t)gid, cred)) && 51637737Smckusick (error = suser(cred, &u.u_acflag))) 51737737Smckusick return (error); 51841312Smckusick ouid = ip->i_uid; 51941312Smckusick ogid = ip->i_gid; 52011811Ssam #ifdef QUOTA 52141312Smckusick if (error = getinoquota(ip)) 52241312Smckusick return (error); 52341312Smckusick if (ouid == uid) { 52441312Smckusick dqrele(vp, ip->i_dquot[USRQUOTA]); 52541312Smckusick ip->i_dquot[USRQUOTA] = NODQUOT; 52641312Smckusick } 52741312Smckusick if (ogid == gid) { 52841312Smckusick dqrele(vp, ip->i_dquot[GRPQUOTA]); 52941312Smckusick ip->i_dquot[GRPQUOTA] = NODQUOT; 53041312Smckusick } 53141312Smckusick change = ip->i_blocks; 53241312Smckusick (void) chkdq(ip, -change, cred, CHOWN); 53341312Smckusick (void) chkiq(ip, -1, cred, CHOWN); 53441312Smckusick for (i = 0; i < MAXQUOTAS; i++) { 53541312Smckusick dqrele(vp, ip->i_dquot[i]); 53641312Smckusick ip->i_dquot[i] = NODQUOT; 53741312Smckusick } 5387482Skre #endif 53911811Ssam ip->i_uid = uid; 54011811Ssam ip->i_gid = gid; 5417701Ssam #ifdef QUOTA 54241312Smckusick if ((error = getinoquota(ip)) == 0) { 54341312Smckusick if (ouid == uid) { 54441312Smckusick dqrele(vp, ip->i_dquot[USRQUOTA]); 54541312Smckusick ip->i_dquot[USRQUOTA] = NODQUOT; 54641312Smckusick } 54741312Smckusick if (ogid == gid) { 54841312Smckusick dqrele(vp, ip->i_dquot[GRPQUOTA]); 54941312Smckusick ip->i_dquot[GRPQUOTA] = NODQUOT; 55041312Smckusick } 55141312Smckusick if ((error = chkdq(ip, change, cred, CHOWN)) == 0) { 55241312Smckusick if ((error = chkiq(ip, 1, cred, CHOWN)) == 0) 55341927Smckusick goto good; 55441312Smckusick else 55541312Smckusick (void) chkdq(ip, -change, cred, CHOWN|FORCE); 55641312Smckusick } 55741312Smckusick for (i = 0; i < MAXQUOTAS; i++) { 55841312Smckusick dqrele(vp, ip->i_dquot[i]); 55941312Smckusick ip->i_dquot[i] = NODQUOT; 56041312Smckusick } 56141312Smckusick } 56241312Smckusick ip->i_uid = ouid; 56341312Smckusick ip->i_gid = ogid; 56441312Smckusick if (getinoquota(ip) == 0) { 56541312Smckusick if (ouid == uid) { 56641312Smckusick dqrele(vp, ip->i_dquot[USRQUOTA]); 56741312Smckusick ip->i_dquot[USRQUOTA] = NODQUOT; 56841312Smckusick } 56941312Smckusick if (ogid == gid) { 57041312Smckusick dqrele(vp, ip->i_dquot[GRPQUOTA]); 57141312Smckusick ip->i_dquot[GRPQUOTA] = NODQUOT; 57241312Smckusick } 57341927Smckusick (void) chkdq(ip, change, cred, FORCE|CHOWN); 57441927Smckusick (void) chkiq(ip, 1, cred, FORCE|CHOWN); 57542440Smckusick (void) getinoquota(ip); 57641312Smckusick } 57742440Smckusick return (error); 57841927Smckusick good: 57942440Smckusick if (getinoquota(ip)) 58042440Smckusick panic("chown: lost quota"); 58142440Smckusick #endif /* QUOTA */ 58241312Smckusick if (ouid != uid || ogid != gid) 58341312Smckusick ip->i_flag |= ICHG; 58441312Smckusick if (ouid != uid && cred->cr_uid != 0) 58541312Smckusick ip->i_mode &= ~ISUID; 58641312Smckusick if (ogid != gid && cred->cr_uid != 0) 58741312Smckusick ip->i_mode &= ~ISGID; 58812646Ssam return (0); 58937Sbill } 59037Sbill 59139608Smckusick /* 59239608Smckusick * Vnode op for reading. 59339608Smckusick */ 59437737Smckusick /* ARGSUSED */ 59539608Smckusick ufs_read(vp, uio, ioflag, cred) 59639608Smckusick struct vnode *vp; 59739608Smckusick register struct uio *uio; 59839608Smckusick int ioflag; 59939608Smckusick struct ucred *cred; 60039608Smckusick { 60139608Smckusick register struct inode *ip = VTOI(vp); 60239608Smckusick register struct fs *fs; 60339608Smckusick struct buf *bp; 60439608Smckusick daddr_t lbn, bn, rablock; 60539896Smckusick int size, diff, error = 0; 60639608Smckusick long n, on, type; 60739608Smckusick 60839608Smckusick if (uio->uio_rw != UIO_READ) 60939608Smckusick panic("ufs_read mode"); 61039608Smckusick type = ip->i_mode & IFMT; 61139608Smckusick if (type != IFDIR && type != IFREG && type != IFLNK) 61239608Smckusick panic("ufs_read type"); 61339608Smckusick if (uio->uio_resid == 0) 61439608Smckusick return (0); 61539608Smckusick if (uio->uio_offset < 0) 61639608Smckusick return (EINVAL); 61739608Smckusick ip->i_flag |= IACC; 61839608Smckusick fs = ip->i_fs; 61939608Smckusick do { 62039608Smckusick lbn = lblkno(fs, uio->uio_offset); 62139608Smckusick on = blkoff(fs, uio->uio_offset); 62239608Smckusick n = MIN((unsigned)(fs->fs_bsize - on), uio->uio_resid); 62339608Smckusick diff = ip->i_size - uio->uio_offset; 62439608Smckusick if (diff <= 0) 62539608Smckusick return (0); 62639608Smckusick if (diff < n) 62739608Smckusick n = diff; 62839608Smckusick size = blksize(fs, ip, lbn); 62939674Smckusick rablock = lbn + 1; 63039896Smckusick if (vp->v_lastr + 1 == lbn && 63139896Smckusick lblktosize(fs, rablock) < ip->i_size) 63239896Smckusick error = breada(ITOV(ip), lbn, size, rablock, 63339896Smckusick blksize(fs, ip, rablock), NOCRED, &bp); 63439608Smckusick else 63539674Smckusick error = bread(ITOV(ip), lbn, size, NOCRED, &bp); 63639815Smckusick vp->v_lastr = lbn; 63739608Smckusick n = MIN(n, size - bp->b_resid); 63839608Smckusick if (error) { 63939608Smckusick brelse(bp); 64039608Smckusick return (error); 64139608Smckusick } 64239608Smckusick error = uiomove(bp->b_un.b_addr + on, (int)n, uio); 64339608Smckusick if (n + on == fs->fs_bsize || uio->uio_offset == ip->i_size) 64439608Smckusick bp->b_flags |= B_AGE; 64539608Smckusick brelse(bp); 64639608Smckusick } while (error == 0 && uio->uio_resid > 0 && n != 0); 64739608Smckusick return (error); 64839608Smckusick } 64939608Smckusick 65039608Smckusick /* 65139608Smckusick * Vnode op for writing. 65239608Smckusick */ 65339608Smckusick ufs_write(vp, uio, ioflag, cred) 65439608Smckusick register struct vnode *vp; 65539608Smckusick struct uio *uio; 65639608Smckusick int ioflag; 65739608Smckusick struct ucred *cred; 65839608Smckusick { 65939608Smckusick register struct inode *ip = VTOI(vp); 66039608Smckusick register struct fs *fs; 66139608Smckusick struct buf *bp; 66239608Smckusick daddr_t lbn, bn; 66339608Smckusick u_long osize; 66445722Smckusick int n, on, flags; 66545722Smckusick int size, resid, error = 0; 66639608Smckusick 66739608Smckusick if (uio->uio_rw != UIO_WRITE) 66839608Smckusick panic("ufs_write mode"); 66939608Smckusick switch (vp->v_type) { 67039608Smckusick case VREG: 67139608Smckusick if (ioflag & IO_APPEND) 67239608Smckusick uio->uio_offset = ip->i_size; 67339608Smckusick /* fall through */ 67439608Smckusick case VLNK: 67539608Smckusick break; 67639608Smckusick 67739608Smckusick case VDIR: 67839608Smckusick if ((ioflag & IO_SYNC) == 0) 67939608Smckusick panic("ufs_write nonsync dir write"); 68039608Smckusick break; 68139608Smckusick 68239608Smckusick default: 68339608Smckusick panic("ufs_write type"); 68439608Smckusick } 68539608Smckusick if (uio->uio_offset < 0) 68639608Smckusick return (EINVAL); 68739608Smckusick if (uio->uio_resid == 0) 68839608Smckusick return (0); 68939608Smckusick /* 69039608Smckusick * Maybe this should be above the vnode op call, but so long as 69139608Smckusick * file servers have no limits, i don't think it matters 69239608Smckusick */ 69339608Smckusick if (vp->v_type == VREG && 69439608Smckusick uio->uio_offset + uio->uio_resid > 69539608Smckusick u.u_rlimit[RLIMIT_FSIZE].rlim_cur) { 69639608Smckusick psignal(u.u_procp, SIGXFSZ); 69739608Smckusick return (EFBIG); 69839608Smckusick } 69939608Smckusick resid = uio->uio_resid; 70039608Smckusick osize = ip->i_size; 70139608Smckusick fs = ip->i_fs; 70239674Smckusick flags = 0; 70339674Smckusick if (ioflag & IO_SYNC) 70439674Smckusick flags = B_SYNC; 70539608Smckusick do { 70639608Smckusick lbn = lblkno(fs, uio->uio_offset); 70739608Smckusick on = blkoff(fs, uio->uio_offset); 70839608Smckusick n = MIN((unsigned)(fs->fs_bsize - on), uio->uio_resid); 70939608Smckusick if (n < fs->fs_bsize) 71039674Smckusick flags |= B_CLRBUF; 71139608Smckusick else 71239674Smckusick flags &= ~B_CLRBUF; 71339674Smckusick if (error = balloc(ip, lbn, (int)(on + n), &bp, flags)) 71439608Smckusick break; 71539674Smckusick bn = bp->b_blkno; 71645722Smckusick if (uio->uio_offset + n > ip->i_size) { 71739608Smckusick ip->i_size = uio->uio_offset + n; 71845722Smckusick vnode_pager_setsize(vp, ip->i_size); 71945722Smckusick } 72039608Smckusick size = blksize(fs, ip, lbn); 72145722Smckusick (void) vnode_pager_uncache(vp); 72239608Smckusick n = MIN(n, size - bp->b_resid); 72339608Smckusick error = uiomove(bp->b_un.b_addr + on, n, uio); 72439608Smckusick if (ioflag & IO_SYNC) 72539608Smckusick (void) bwrite(bp); 72639608Smckusick else if (n + on == fs->fs_bsize) { 72739608Smckusick bp->b_flags |= B_AGE; 72839608Smckusick bawrite(bp); 72939608Smckusick } else 73039608Smckusick bdwrite(bp); 73139608Smckusick ip->i_flag |= IUPD|ICHG; 73239608Smckusick if (cred->cr_uid != 0) 73339608Smckusick ip->i_mode &= ~(ISUID|ISGID); 73439608Smckusick } while (error == 0 && uio->uio_resid > 0 && n != 0); 73539608Smckusick if (error && (ioflag & IO_UNIT)) { 73639674Smckusick (void) itrunc(ip, osize, ioflag & IO_SYNC); 73739608Smckusick uio->uio_offset -= resid - uio->uio_resid; 73839608Smckusick uio->uio_resid = resid; 73939608Smckusick } 74042493Smckusick if (!error && (ioflag & IO_SYNC)) 74142493Smckusick error = iupdat(ip, &time, &time, 1); 74239608Smckusick return (error); 74339608Smckusick } 74439608Smckusick 74539608Smckusick /* ARGSUSED */ 74637737Smckusick ufs_ioctl(vp, com, data, fflag, cred) 74737737Smckusick struct vnode *vp; 74837737Smckusick int com; 74937737Smckusick caddr_t data; 75037737Smckusick int fflag; 75137737Smckusick struct ucred *cred; 75211811Ssam { 75311811Ssam 75437737Smckusick return (ENOTTY); 75511811Ssam } 75611811Ssam 75737737Smckusick /* ARGSUSED */ 75840290Smckusick ufs_select(vp, which, fflags, cred) 75937737Smckusick struct vnode *vp; 76040290Smckusick int which, fflags; 76137737Smckusick struct ucred *cred; 76237737Smckusick { 76337737Smckusick 76437737Smckusick return (1); /* XXX */ 76537737Smckusick } 76637737Smckusick 7679167Ssam /* 76837737Smckusick * Mmap a file 76937737Smckusick * 77037737Smckusick * NB Currently unsupported. 7719167Ssam */ 77237737Smckusick /* ARGSUSED */ 77337737Smckusick ufs_mmap(vp, fflags, cred) 77437737Smckusick struct vnode *vp; 77537737Smckusick int fflags; 77637737Smckusick struct ucred *cred; 77737Sbill { 77837Sbill 77937737Smckusick return (EINVAL); 78037Sbill } 7817535Sroot 7829167Ssam /* 78337737Smckusick * Synch an open file. 7849167Ssam */ 78537737Smckusick /* ARGSUSED */ 78639597Smckusick ufs_fsync(vp, fflags, cred, waitfor) 78737737Smckusick struct vnode *vp; 78837737Smckusick int fflags; 78937737Smckusick struct ucred *cred; 79039597Smckusick int waitfor; 7917701Ssam { 79239597Smckusick struct inode *ip = VTOI(vp); 7937701Ssam 79437737Smckusick if (fflags&FWRITE) 79537737Smckusick ip->i_flag |= ICHG; 79639674Smckusick vflushbuf(vp, waitfor == MNT_WAIT ? B_SYNC : 0); 79739674Smckusick return (iupdat(ip, &time, &time, waitfor == MNT_WAIT)); 7987701Ssam } 7997701Ssam 8009167Ssam /* 80137737Smckusick * Seek on a file 80237737Smckusick * 80337737Smckusick * Nothing to do, so just return. 8049167Ssam */ 80537737Smckusick /* ARGSUSED */ 80637737Smckusick ufs_seek(vp, oldoff, newoff, cred) 80737737Smckusick struct vnode *vp; 80837737Smckusick off_t oldoff, newoff; 80937737Smckusick struct ucred *cred; 8107701Ssam { 8117701Ssam 81237737Smckusick return (0); 81337737Smckusick } 81437737Smckusick 81537737Smckusick /* 81637737Smckusick * ufs remove 81737737Smckusick * Hard to avoid races here, especially 81837737Smckusick * in unlinking directories. 81937737Smckusick */ 82037737Smckusick ufs_remove(ndp) 82137737Smckusick struct nameidata *ndp; 82237737Smckusick { 82337737Smckusick register struct inode *ip, *dp; 82437737Smckusick int error; 82537737Smckusick 82637737Smckusick ip = VTOI(ndp->ni_vp); 82737737Smckusick dp = VTOI(ndp->ni_dvp); 82837737Smckusick error = dirremove(ndp); 82937737Smckusick if (!error) { 83037737Smckusick ip->i_nlink--; 83137737Smckusick ip->i_flag |= ICHG; 8327701Ssam } 83337737Smckusick if (dp == ip) 83437737Smckusick vrele(ITOV(ip)); 83537737Smckusick else 83637737Smckusick iput(ip); 83737737Smckusick iput(dp); 83837737Smckusick return (error); 8397701Ssam } 8407701Ssam 8419167Ssam /* 84237737Smckusick * link vnode call 8439167Ssam */ 84437737Smckusick ufs_link(vp, ndp) 84537737Smckusick register struct vnode *vp; 84637737Smckusick register struct nameidata *ndp; 8479167Ssam { 84837737Smckusick register struct inode *ip = VTOI(vp); 84937737Smckusick int error; 8509167Ssam 85146251Smckusick if ((unsigned short)ip->i_nlink >= LINK_MAX) 85246251Smckusick return (EMLINK); 85337737Smckusick if (ndp->ni_dvp != vp) 85437737Smckusick ILOCK(ip); 85537737Smckusick ip->i_nlink++; 85637737Smckusick ip->i_flag |= ICHG; 85737737Smckusick error = iupdat(ip, &time, &time, 1); 85837737Smckusick if (!error) 85937737Smckusick error = direnter(ip, ndp); 86037737Smckusick if (ndp->ni_dvp != vp) 86137737Smckusick IUNLOCK(ip); 86247219Smckusick vput(ndp->ni_dvp); 86337737Smckusick if (error) { 86437737Smckusick ip->i_nlink--; 86530598Smckusick ip->i_flag |= ICHG; 86637737Smckusick } 86737737Smckusick return (error); 8689167Ssam } 8699167Ssam 8709167Ssam /* 8719167Ssam * Rename system call. 8729167Ssam * rename("foo", "bar"); 8739167Ssam * is essentially 8749167Ssam * unlink("bar"); 8759167Ssam * link("foo", "bar"); 8769167Ssam * unlink("foo"); 8779167Ssam * but ``atomically''. Can't do full commit without saving state in the 8789167Ssam * inode on disk which isn't feasible at this time. Best we can do is 8799167Ssam * always guarantee the target exists. 8809167Ssam * 8819167Ssam * Basic algorithm is: 8829167Ssam * 8839167Ssam * 1) Bump link count on source while we're linking it to the 88437737Smckusick * target. This also ensure the inode won't be deleted out 88516776Smckusick * from underneath us while we work (it may be truncated by 88616776Smckusick * a concurrent `trunc' or `open' for creation). 8879167Ssam * 2) Link source to destination. If destination already exists, 8889167Ssam * delete it first. 88916776Smckusick * 3) Unlink source reference to inode if still around. If a 89016776Smckusick * directory was moved and the parent of the destination 8919167Ssam * is different from the source, patch the ".." entry in the 8929167Ssam * directory. 8939167Ssam */ 89437737Smckusick ufs_rename(fndp, tndp) 89537737Smckusick register struct nameidata *fndp, *tndp; 8967701Ssam { 8979167Ssam register struct inode *ip, *xp, *dp; 89816776Smckusick struct dirtemplate dirbuf; 89916776Smckusick int doingdirectory = 0, oldparent = 0, newparent = 0; 90010051Ssam int error = 0; 9017701Ssam 90237737Smckusick dp = VTOI(fndp->ni_dvp); 90337737Smckusick ip = VTOI(fndp->ni_vp); 90437737Smckusick ILOCK(ip); 9059167Ssam if ((ip->i_mode&IFMT) == IFDIR) { 90637737Smckusick register struct direct *d = &fndp->ni_dent; 9079167Ssam 9089167Ssam /* 90911641Ssam * Avoid ".", "..", and aliases of "." for obvious reasons. 9109167Ssam */ 91137737Smckusick if ((d->d_namlen == 1 && d->d_name[0] == '.') || dp == ip || 91237737Smckusick fndp->ni_isdotdot || (ip->i_flag & IRENAME)) { 91342466Smckusick VOP_ABORTOP(tndp); 91442466Smckusick vput(tndp->ni_dvp); 91542466Smckusick if (tndp->ni_vp) 91642466Smckusick vput(tndp->ni_vp); 91742466Smckusick VOP_ABORTOP(fndp); 91842466Smckusick vrele(fndp->ni_dvp); 91942466Smckusick vput(fndp->ni_vp); 92037737Smckusick return (EINVAL); 9219167Ssam } 92216776Smckusick ip->i_flag |= IRENAME; 9239167Ssam oldparent = dp->i_number; 9249167Ssam doingdirectory++; 9259167Ssam } 92637737Smckusick vrele(fndp->ni_dvp); 9279167Ssam 9289167Ssam /* 9299167Ssam * 1) Bump link count while we're moving stuff 9309167Ssam * around. If we crash somewhere before 9319167Ssam * completing our work, the link count 9329167Ssam * may be wrong, but correctable. 9339167Ssam */ 9349167Ssam ip->i_nlink++; 9359167Ssam ip->i_flag |= ICHG; 93637737Smckusick error = iupdat(ip, &time, &time, 1); 93716664Smckusick IUNLOCK(ip); 9389167Ssam 9399167Ssam /* 9409167Ssam * When the target exists, both the directory 94137737Smckusick * and target vnodes are returned locked. 9429167Ssam */ 94337737Smckusick dp = VTOI(tndp->ni_dvp); 94437737Smckusick xp = NULL; 94537737Smckusick if (tndp->ni_vp) 94637737Smckusick xp = VTOI(tndp->ni_vp); 9479167Ssam /* 94811641Ssam * If ".." must be changed (ie the directory gets a new 94912816Smckusick * parent) then the source directory must not be in the 95012816Smckusick * directory heirarchy above the target, as this would 95112816Smckusick * orphan everything below the source directory. Also 95212816Smckusick * the user must have write permission in the source so 95312816Smckusick * as to be able to change "..". We must repeat the call 95412816Smckusick * to namei, as the parent directory is unlocked by the 95512816Smckusick * call to checkpath(). 95611641Ssam */ 95716776Smckusick if (oldparent != dp->i_number) 95816776Smckusick newparent = dp->i_number; 95916776Smckusick if (doingdirectory && newparent) { 96041466Smckusick VOP_LOCK(fndp->ni_vp); 96141466Smckusick error = ufs_access(fndp->ni_vp, VWRITE, tndp->ni_cred); 96241466Smckusick VOP_UNLOCK(fndp->ni_vp); 96341466Smckusick if (error) 96412816Smckusick goto bad; 96546511Smckusick tndp->ni_nameiop &= ~(MODMASK | OPMASK); 96646511Smckusick tndp->ni_nameiop |= RENAME | LOCKPARENT | LOCKLEAF | NOCACHE; 96712816Smckusick do { 96837737Smckusick dp = VTOI(tndp->ni_dvp); 96912816Smckusick if (xp != NULL) 97038069Smckusick iput(xp); 97137737Smckusick if (error = checkpath(ip, dp, tndp->ni_cred)) 97212816Smckusick goto out; 97337737Smckusick if (error = namei(tndp)) 97412816Smckusick goto out; 97537737Smckusick xp = NULL; 97637737Smckusick if (tndp->ni_vp) 97737737Smckusick xp = VTOI(tndp->ni_vp); 97837737Smckusick } while (dp != VTOI(tndp->ni_dvp)); 97912816Smckusick } 98011641Ssam /* 9819167Ssam * 2) If target doesn't exist, link the target 9829167Ssam * to the source and unlink the source. 9839167Ssam * Otherwise, rewrite the target directory 9849167Ssam * entry to reference the source inode and 9859167Ssam * expunge the original entry's existence. 9869167Ssam */ 9879167Ssam if (xp == NULL) { 98837737Smckusick if (dp->i_dev != ip->i_dev) 98937737Smckusick panic("rename: EXDEV"); 9909167Ssam /* 99116776Smckusick * Account for ".." in new directory. 99216776Smckusick * When source and destination have the same 99316776Smckusick * parent we don't fool with the link count. 9949167Ssam */ 99516776Smckusick if (doingdirectory && newparent) { 99646251Smckusick if ((unsigned short)dp->i_nlink >= LINK_MAX) { 99746251Smckusick error = EMLINK; 99846251Smckusick goto bad; 99946251Smckusick } 10009167Ssam dp->i_nlink++; 10019167Ssam dp->i_flag |= ICHG; 100246251Smckusick if (error = iupdat(dp, &time, &time, 1)) 100346251Smckusick goto bad; 10049167Ssam } 100547219Smckusick if (error = direnter(ip, tndp)) { 100647219Smckusick if (doingdirectory && newparent) { 100747219Smckusick dp->i_nlink--; 100847219Smckusick dp->i_flag |= ICHG; 100947219Smckusick (void) iupdat(dp, &time, &time, 1); 101047219Smckusick } 101147219Smckusick goto bad; 101247219Smckusick } 1013*47234Smckusick iput(dp); 10149167Ssam } else { 101537737Smckusick if (xp->i_dev != dp->i_dev || xp->i_dev != ip->i_dev) 101637737Smckusick panic("rename: EXDEV"); 10179167Ssam /* 101810590Ssam * Short circuit rename(foo, foo). 101910590Ssam */ 102010590Ssam if (xp->i_number == ip->i_number) 102137737Smckusick panic("rename: same file"); 102210590Ssam /* 102324433Sbloom * If the parent directory is "sticky", then the user must 102424433Sbloom * own the parent directory, or the destination of the rename, 102524433Sbloom * otherwise the destination may not be changed (except by 102624433Sbloom * root). This implements append-only directories. 102724433Sbloom */ 102837737Smckusick if ((dp->i_mode & ISVTX) && tndp->ni_cred->cr_uid != 0 && 102937737Smckusick tndp->ni_cred->cr_uid != dp->i_uid && 103037737Smckusick xp->i_uid != tndp->ni_cred->cr_uid) { 103124433Sbloom error = EPERM; 103224433Sbloom goto bad; 103324433Sbloom } 103424433Sbloom /* 103510051Ssam * Target must be empty if a directory 103610051Ssam * and have no links to it. 10379167Ssam * Also, insure source and target are 10389167Ssam * compatible (both directories, or both 10399167Ssam * not directories). 10409167Ssam */ 10419167Ssam if ((xp->i_mode&IFMT) == IFDIR) { 104237737Smckusick if (!dirempty(xp, dp->i_number, tndp->ni_cred) || 104337737Smckusick xp->i_nlink > 2) { 104410051Ssam error = ENOTEMPTY; 10459167Ssam goto bad; 10469167Ssam } 10479167Ssam if (!doingdirectory) { 104810051Ssam error = ENOTDIR; 10499167Ssam goto bad; 10509167Ssam } 105137737Smckusick cache_purge(ITOV(dp)); 10529167Ssam } else if (doingdirectory) { 105310051Ssam error = EISDIR; 10549167Ssam goto bad; 10559167Ssam } 105637737Smckusick if (error = dirrewrite(dp, ip, tndp)) 105737737Smckusick goto bad; 105845354Smckusick /* 105945354Smckusick * If the target directory is in the same 106045354Smckusick * directory as the source directory, 106145354Smckusick * decrement the link count on the parent 106245354Smckusick * of the target directory. 106345354Smckusick */ 106445354Smckusick if (doingdirectory && !newparent) { 106545354Smckusick dp->i_nlink--; 106645354Smckusick dp->i_flag |= ICHG; 106745354Smckusick } 106837737Smckusick vput(ITOV(dp)); 10699167Ssam /* 107010051Ssam * Adjust the link count of the target to 107110051Ssam * reflect the dirrewrite above. If this is 107210051Ssam * a directory it is empty and there are 107310051Ssam * no links to it, so we can squash the inode and 107410051Ssam * any space associated with it. We disallowed 107510051Ssam * renaming over top of a directory with links to 107616776Smckusick * it above, as the remaining link would point to 107716776Smckusick * a directory without "." or ".." entries. 10789167Ssam */ 107910051Ssam xp->i_nlink--; 10809167Ssam if (doingdirectory) { 108110051Ssam if (--xp->i_nlink != 0) 108210051Ssam panic("rename: linked directory"); 108339674Smckusick error = itrunc(xp, (u_long)0, IO_SYNC); 108410051Ssam } 10859167Ssam xp->i_flag |= ICHG; 108638398Smckusick iput(xp); 108710246Ssam xp = NULL; 10889167Ssam } 10899167Ssam 10909167Ssam /* 10919167Ssam * 3) Unlink the source. 10929167Ssam */ 109346511Smckusick fndp->ni_nameiop &= ~(MODMASK | OPMASK); 109446511Smckusick fndp->ni_nameiop |= DELETE | LOCKPARENT | LOCKLEAF; 109537737Smckusick (void)namei(fndp); 109637737Smckusick if (fndp->ni_vp != NULL) { 109737737Smckusick xp = VTOI(fndp->ni_vp); 109837737Smckusick dp = VTOI(fndp->ni_dvp); 109937737Smckusick } else { 110046250Smckusick /* 110146250Smckusick * From name has disappeared. 110246250Smckusick */ 110346250Smckusick if (doingdirectory) 110446250Smckusick panic("rename: lost dir entry"); 110546250Smckusick vrele(ITOV(ip)); 110646250Smckusick return (0); 110737737Smckusick } 11089167Ssam /* 110937737Smckusick * Ensure that the directory entry still exists and has not 111016776Smckusick * changed while the new name has been entered. If the source is 111116776Smckusick * a file then the entry may have been unlinked or renamed. In 111216776Smckusick * either case there is no further work to be done. If the source 111316776Smckusick * is a directory then it cannot have been rmdir'ed; its link 111416776Smckusick * count of three would cause a rmdir to fail with ENOTEMPTY. 111537737Smckusick * The IRENAME flag ensures that it cannot be moved by another 111616776Smckusick * rename. 11179167Ssam */ 111817758Smckusick if (xp != ip) { 111916776Smckusick if (doingdirectory) 112017758Smckusick panic("rename: lost dir entry"); 112116776Smckusick } else { 11229167Ssam /* 112316776Smckusick * If the source is a directory with a 112416776Smckusick * new parent, the link count of the old 112516776Smckusick * parent directory must be decremented 112616776Smckusick * and ".." set to point to the new parent. 11279167Ssam */ 112816776Smckusick if (doingdirectory && newparent) { 11299167Ssam dp->i_nlink--; 11309167Ssam dp->i_flag |= ICHG; 113139597Smckusick error = vn_rdwr(UIO_READ, ITOV(xp), (caddr_t)&dirbuf, 113237737Smckusick sizeof (struct dirtemplate), (off_t)0, 113339597Smckusick UIO_SYSSPACE, IO_NODELOCKED, 113439597Smckusick tndp->ni_cred, (int *)0); 113516776Smckusick if (error == 0) { 113616776Smckusick if (dirbuf.dotdot_namlen != 2 || 113716776Smckusick dirbuf.dotdot_name[0] != '.' || 113816776Smckusick dirbuf.dotdot_name[1] != '.') { 113939610Smckusick dirbad(xp, 12, "rename: mangled dir"); 114016776Smckusick } else { 114116776Smckusick dirbuf.dotdot_ino = newparent; 114239597Smckusick (void) vn_rdwr(UIO_WRITE, ITOV(xp), 114316776Smckusick (caddr_t)&dirbuf, 114416776Smckusick sizeof (struct dirtemplate), 114537740Smckusick (off_t)0, UIO_SYSSPACE, 114639597Smckusick IO_NODELOCKED|IO_SYNC, 114737737Smckusick tndp->ni_cred, (int *)0); 114837737Smckusick cache_purge(ITOV(dp)); 114916776Smckusick } 115016776Smckusick } 11519167Ssam } 115237737Smckusick error = dirremove(fndp); 115337737Smckusick if (!error) { 115416776Smckusick xp->i_nlink--; 115516776Smckusick xp->i_flag |= ICHG; 11569167Ssam } 115716776Smckusick xp->i_flag &= ~IRENAME; 11589167Ssam } 11599167Ssam if (dp) 116037737Smckusick vput(ITOV(dp)); 116116776Smckusick if (xp) 116237737Smckusick vput(ITOV(xp)); 116337737Smckusick vrele(ITOV(ip)); 116437737Smckusick return (error); 11659167Ssam 11669167Ssam bad: 11679167Ssam if (xp) 116837737Smckusick vput(ITOV(xp)); 116937737Smckusick vput(ITOV(dp)); 11709167Ssam out: 11719167Ssam ip->i_nlink--; 11729167Ssam ip->i_flag |= ICHG; 117337737Smckusick vrele(ITOV(ip)); 117437737Smckusick return (error); 11757701Ssam } 11767701Ssam 11777535Sroot /* 117812756Ssam * A virgin directory (no blushing please). 117912756Ssam */ 118012756Ssam struct dirtemplate mastertemplate = { 118112756Ssam 0, 12, 1, ".", 118212756Ssam 0, DIRBLKSIZ - 12, 2, ".." 118312756Ssam }; 118412756Ssam 118512756Ssam /* 118612756Ssam * Mkdir system call 118712756Ssam */ 118837737Smckusick ufs_mkdir(ndp, vap) 118937737Smckusick struct nameidata *ndp; 119037737Smckusick struct vattr *vap; 119112756Ssam { 119212756Ssam register struct inode *ip, *dp; 119337737Smckusick struct inode *tip; 119437737Smckusick struct vnode *dvp; 119512756Ssam struct dirtemplate dirtemplate; 119637737Smckusick int error; 119737737Smckusick int dmode; 119812756Ssam 119937737Smckusick dvp = ndp->ni_dvp; 120037737Smckusick dp = VTOI(dvp); 120146251Smckusick if ((unsigned short)dp->i_nlink >= LINK_MAX) { 120246251Smckusick iput(dp); 120346251Smckusick return (EMLINK); 120446251Smckusick } 120537737Smckusick dmode = vap->va_mode&0777; 120637737Smckusick dmode |= IFDIR; 120712756Ssam /* 120812756Ssam * Must simulate part of maknode here 120912756Ssam * in order to acquire the inode, but 121012756Ssam * not have it entered in the parent 121112756Ssam * directory. The entry is made later 121212756Ssam * after writing "." and ".." entries out. 121312756Ssam */ 121441312Smckusick if (error = ialloc(dp, dirpref(dp->i_fs), dmode, ndp->ni_cred, &tip)) { 121512756Ssam iput(dp); 121637737Smckusick return (error); 121712756Ssam } 121837737Smckusick ip = tip; 121941312Smckusick ip->i_uid = ndp->ni_cred->cr_uid; 122041312Smckusick ip->i_gid = dp->i_gid; 122112756Ssam #ifdef QUOTA 122241312Smckusick if ((error = getinoquota(ip)) || 122341312Smckusick (error = chkiq(ip, 1, ndp->ni_cred, 0))) { 122441312Smckusick ifree(ip, ip->i_number, dmode); 122541312Smckusick iput(ip); 122641312Smckusick iput(dp); 122741312Smckusick return (error); 122841312Smckusick } 122912756Ssam #endif 123012756Ssam ip->i_flag |= IACC|IUPD|ICHG; 123137737Smckusick ip->i_mode = dmode; 123237737Smckusick ITOV(ip)->v_type = VDIR; /* Rest init'd in iget() */ 123312756Ssam ip->i_nlink = 2; 123437737Smckusick error = iupdat(ip, &time, &time, 1); 123512756Ssam 123612756Ssam /* 123712756Ssam * Bump link count in parent directory 123812756Ssam * to reflect work done below. Should 123912756Ssam * be done before reference is created 124012756Ssam * so reparation is possible if we crash. 124112756Ssam */ 124212756Ssam dp->i_nlink++; 124312756Ssam dp->i_flag |= ICHG; 124447219Smckusick if (error = iupdat(dp, &time, &time, 1)) 124547219Smckusick goto bad; 124612756Ssam 124712756Ssam /* 124812756Ssam * Initialize directory with "." 124912756Ssam * and ".." from static template. 125012756Ssam */ 125112756Ssam dirtemplate = mastertemplate; 125212756Ssam dirtemplate.dot_ino = ip->i_number; 125312756Ssam dirtemplate.dotdot_ino = dp->i_number; 125439597Smckusick error = vn_rdwr(UIO_WRITE, ITOV(ip), (caddr_t)&dirtemplate, 125537737Smckusick sizeof (dirtemplate), (off_t)0, UIO_SYSSPACE, 125639597Smckusick IO_NODELOCKED|IO_SYNC, ndp->ni_cred, (int *)0); 125737737Smckusick if (error) { 125812756Ssam dp->i_nlink--; 125912756Ssam dp->i_flag |= ICHG; 126012756Ssam goto bad; 126112756Ssam } 126243288Smckusick if (DIRBLKSIZ > dp->i_fs->fs_fsize) { 126337737Smckusick panic("mkdir: blksize"); /* XXX - should grow w/balloc() */ 126443288Smckusick } else { 126518103Smckusick ip->i_size = DIRBLKSIZ; 126643288Smckusick ip->i_flag |= ICHG; 126743288Smckusick } 126812756Ssam /* 126912756Ssam * Directory all set up, now 127012756Ssam * install the entry for it in 127112756Ssam * the parent directory. 127212756Ssam */ 127347219Smckusick if (error = direnter(ip, ndp)) { 127446511Smckusick ndp->ni_nameiop &= ~(MODMASK | OPMASK); 127547219Smckusick ndp->ni_nameiop |= LOOKUP | LOCKLEAF | NOCACHE; 127637737Smckusick error = namei(ndp); 127737737Smckusick if (!error) { 127847219Smckusick iput(dp); 127937737Smckusick dp = VTOI(ndp->ni_vp); 128012756Ssam dp->i_nlink--; 128112756Ssam dp->i_flag |= ICHG; 128212756Ssam } 128312756Ssam } 128412756Ssam bad: 128512756Ssam /* 128612756Ssam * No need to do an explicit itrunc here, 128737737Smckusick * vrele will do this for us because we set 128812756Ssam * the link count to 0. 128912756Ssam */ 129037737Smckusick if (error) { 129112756Ssam ip->i_nlink = 0; 129212756Ssam ip->i_flag |= ICHG; 129338144Smckusick iput(ip); 129438144Smckusick } else 129538144Smckusick ndp->ni_vp = ITOV(ip); 129647219Smckusick iput(dp); 129737737Smckusick return (error); 129812756Ssam } 129912756Ssam 130012756Ssam /* 130112756Ssam * Rmdir system call. 130212756Ssam */ 130337737Smckusick ufs_rmdir(ndp) 130437737Smckusick register struct nameidata *ndp; 130512756Ssam { 130612756Ssam register struct inode *ip, *dp; 130737737Smckusick int error = 0; 130812756Ssam 130937737Smckusick ip = VTOI(ndp->ni_vp); 131037737Smckusick dp = VTOI(ndp->ni_dvp); 131112756Ssam /* 131212756Ssam * No rmdir "." please. 131312756Ssam */ 131412756Ssam if (dp == ip) { 131537737Smckusick vrele(ITOV(dp)); 131612756Ssam iput(ip); 131737737Smckusick return (EINVAL); 131812756Ssam } 131912756Ssam /* 132012756Ssam * Verify the directory is empty (and valid). 132112756Ssam * (Rmdir ".." won't be valid since 132212756Ssam * ".." will contain a reference to 132312756Ssam * the current directory and thus be 132412756Ssam * non-empty.) 132512756Ssam */ 132637737Smckusick if (ip->i_nlink != 2 || !dirempty(ip, dp->i_number, ndp->ni_cred)) { 132737737Smckusick error = ENOTEMPTY; 132812756Ssam goto out; 132912756Ssam } 133012756Ssam /* 133112756Ssam * Delete reference to directory before purging 133212756Ssam * inode. If we crash in between, the directory 133312756Ssam * will be reattached to lost+found, 133412756Ssam */ 133537737Smckusick if (error = dirremove(ndp)) 133612756Ssam goto out; 133712756Ssam dp->i_nlink--; 133812756Ssam dp->i_flag |= ICHG; 133937737Smckusick cache_purge(ITOV(dp)); 134012756Ssam iput(dp); 134137737Smckusick ndp->ni_dvp = NULL; 134212756Ssam /* 134312756Ssam * Truncate inode. The only stuff left 134412756Ssam * in the directory is "." and "..". The 134512756Ssam * "." reference is inconsequential since 134612756Ssam * we're quashing it. The ".." reference 134712756Ssam * has already been adjusted above. We've 134812756Ssam * removed the "." reference and the reference 134912756Ssam * in the parent directory, but there may be 135012756Ssam * other hard links so decrement by 2 and 135112756Ssam * worry about them later. 135212756Ssam */ 135312756Ssam ip->i_nlink -= 2; 135439674Smckusick error = itrunc(ip, (u_long)0, IO_SYNC); 135537737Smckusick cache_purge(ITOV(ip)); 135612756Ssam out: 135737737Smckusick if (ndp->ni_dvp) 135812756Ssam iput(dp); 135912756Ssam iput(ip); 136037737Smckusick return (error); 136112756Ssam } 136212756Ssam 136337737Smckusick /* 136437737Smckusick * symlink -- make a symbolic link 136537737Smckusick */ 136637737Smckusick ufs_symlink(ndp, vap, target) 136737737Smckusick struct nameidata *ndp; 136837737Smckusick struct vattr *vap; 136937737Smckusick char *target; 137012756Ssam { 137137737Smckusick struct inode *ip; 137237737Smckusick int error; 137312756Ssam 137437737Smckusick error = maknode(IFLNK | vap->va_mode, ndp, &ip); 137537737Smckusick if (error) 137637737Smckusick return (error); 137739597Smckusick error = vn_rdwr(UIO_WRITE, ITOV(ip), target, strlen(target), (off_t)0, 137839597Smckusick UIO_SYSSPACE, IO_NODELOCKED, ndp->ni_cred, (int *)0); 137937737Smckusick iput(ip); 138037737Smckusick return (error); 138137737Smckusick } 138237737Smckusick 138337737Smckusick /* 138437737Smckusick * Vnode op for read and write 138537737Smckusick */ 138640345Smckusick ufs_readdir(vp, uio, cred, eofflagp) 138737737Smckusick struct vnode *vp; 138837737Smckusick register struct uio *uio; 138937737Smckusick struct ucred *cred; 139040345Smckusick int *eofflagp; 139137737Smckusick { 139239597Smckusick int count, lost, error; 139337737Smckusick 139437737Smckusick count = uio->uio_resid; 139537737Smckusick count &= ~(DIRBLKSIZ - 1); 139639597Smckusick lost = uio->uio_resid - count; 139739597Smckusick if (count < DIRBLKSIZ || (uio->uio_offset & (DIRBLKSIZ -1))) 139837737Smckusick return (EINVAL); 139937737Smckusick uio->uio_resid = count; 140037737Smckusick uio->uio_iov->iov_len = count; 140139597Smckusick error = ufs_read(vp, uio, 0, cred); 140239597Smckusick uio->uio_resid += lost; 140340345Smckusick if ((VTOI(vp)->i_size - uio->uio_offset) <= 0) 140440345Smckusick *eofflagp = 1; 140540345Smckusick else 140640345Smckusick *eofflagp = 0; 140737737Smckusick return (error); 140837737Smckusick } 140937737Smckusick 141037737Smckusick /* 141137737Smckusick * Return target name of a symbolic link 141237737Smckusick */ 141337737Smckusick ufs_readlink(vp, uiop, cred) 141437737Smckusick struct vnode *vp; 141537737Smckusick struct uio *uiop; 141637737Smckusick struct ucred *cred; 141737737Smckusick { 141837737Smckusick 141939597Smckusick return (ufs_read(vp, uiop, 0, cred)); 142037737Smckusick } 142137737Smckusick 142237737Smckusick /* 142337737Smckusick * Ufs abort op, called after namei() when a CREATE/DELETE isn't actually 142442466Smckusick * done. Nothing to do at the moment. 142537737Smckusick */ 142642466Smckusick /* ARGSUSED */ 142737737Smckusick ufs_abortop(ndp) 142842466Smckusick struct nameidata *ndp; 142937737Smckusick { 143037737Smckusick 143142466Smckusick return (0); 143212756Ssam } 143312756Ssam 143439909Smckusick /* 143539909Smckusick * Lock an inode. 143639909Smckusick */ 143737737Smckusick ufs_lock(vp) 143837737Smckusick struct vnode *vp; 143937737Smckusick { 144037737Smckusick register struct inode *ip = VTOI(vp); 144137737Smckusick 144237737Smckusick ILOCK(ip); 144337737Smckusick return (0); 144437737Smckusick } 144537737Smckusick 144639909Smckusick /* 144739909Smckusick * Unlock an inode. 144839909Smckusick */ 144937737Smckusick ufs_unlock(vp) 145037737Smckusick struct vnode *vp; 145137737Smckusick { 145237737Smckusick register struct inode *ip = VTOI(vp); 145337737Smckusick 145437737Smckusick if (!(ip->i_flag & ILOCKED)) 145537737Smckusick panic("ufs_unlock NOT LOCKED"); 145637737Smckusick IUNLOCK(ip); 145737737Smckusick return (0); 145837737Smckusick } 145937737Smckusick 146012756Ssam /* 146139909Smckusick * Check for a locked inode. 146239909Smckusick */ 146339909Smckusick ufs_islocked(vp) 146439909Smckusick struct vnode *vp; 146539909Smckusick { 146639909Smckusick 146739909Smckusick if (VTOI(vp)->i_flag & ILOCKED) 146839909Smckusick return (1); 146939909Smckusick return (0); 147039909Smckusick } 147139909Smckusick 147239909Smckusick /* 147337737Smckusick * Get access to bmap 147412756Ssam */ 147537737Smckusick ufs_bmap(vp, bn, vpp, bnp) 147637737Smckusick struct vnode *vp; 147737737Smckusick daddr_t bn; 147837737Smckusick struct vnode **vpp; 147937737Smckusick daddr_t *bnp; 148012756Ssam { 148137737Smckusick struct inode *ip = VTOI(vp); 148212756Ssam 148337737Smckusick if (vpp != NULL) 148437737Smckusick *vpp = ip->i_devvp; 148537737Smckusick if (bnp == NULL) 148637737Smckusick return (0); 148741538Smckusick return (bmap(ip, bn, bnp)); 148812756Ssam } 148937737Smckusick 149037737Smckusick /* 149141538Smckusick * Calculate the logical to physical mapping if not done already, 149241538Smckusick * then call the device strategy routine. 149337737Smckusick */ 149441538Smckusick int checkoverlap = 0; 149539674Smckusick 149637737Smckusick ufs_strategy(bp) 149737737Smckusick register struct buf *bp; 149837737Smckusick { 149939674Smckusick register struct inode *ip = VTOI(bp->b_vp); 150039674Smckusick struct vnode *vp; 150139674Smckusick int error; 150239674Smckusick 150339674Smckusick if (bp->b_vp->v_type == VBLK || bp->b_vp->v_type == VCHR) 150439674Smckusick panic("ufs_strategy: spec"); 150539674Smckusick if (bp->b_blkno == bp->b_lblkno) { 150639674Smckusick if (error = bmap(ip, bp->b_lblkno, &bp->b_blkno)) 150739674Smckusick return (error); 150839896Smckusick if ((long)bp->b_blkno == -1) 150939674Smckusick clrbuf(bp); 151039674Smckusick } 151139896Smckusick if ((long)bp->b_blkno == -1) { 151239896Smckusick biodone(bp); 151339674Smckusick return (0); 151439896Smckusick } 151541538Smckusick #ifdef DIAGNOSTIC 151639674Smckusick if (checkoverlap) { 151741538Smckusick register struct buf *ep; 151841538Smckusick struct buf *ebp; 151941538Smckusick daddr_t start, last; 152041538Smckusick 152139674Smckusick ebp = &buf[nbuf]; 152239674Smckusick start = bp->b_blkno; 152339674Smckusick last = start + btodb(bp->b_bcount) - 1; 152439674Smckusick for (ep = buf; ep < ebp; ep++) { 152539674Smckusick if (ep == bp || (ep->b_flags & B_INVAL) || 152641396Smckusick ep->b_vp == NULLVP) 152739674Smckusick continue; 152839674Smckusick if (VOP_BMAP(ep->b_vp, (daddr_t)0, &vp, (daddr_t)0)) 152939674Smckusick continue; 153039674Smckusick if (vp != ip->i_devvp) 153139674Smckusick continue; 153239674Smckusick /* look for overlap */ 153339674Smckusick if (ep->b_bcount == 0 || ep->b_blkno > last || 153439674Smckusick ep->b_blkno + btodb(ep->b_bcount) <= start) 153539674Smckusick continue; 153639896Smckusick vprint("Disk overlap", vp); 153739896Smckusick printf("\tstart %d, end %d overlap start %d, end %d\n", 153839896Smckusick start, last, ep->b_blkno, 153939896Smckusick ep->b_blkno + btodb(ep->b_bcount) - 1); 154041538Smckusick panic("Disk buffer overlap"); 154139674Smckusick } 154239674Smckusick } 154341538Smckusick #endif /* DIAGNOSTIC */ 154439674Smckusick vp = ip->i_devvp; 154539674Smckusick bp->b_dev = vp->v_rdev; 154639674Smckusick (*(vp->v_op->vn_strategy))(bp); 154737737Smckusick return (0); 154837737Smckusick } 154937737Smckusick 155037737Smckusick /* 155139674Smckusick * Print out the contents of an inode. 155239674Smckusick */ 155339674Smckusick ufs_print(vp) 155439674Smckusick struct vnode *vp; 155539674Smckusick { 155639674Smckusick register struct inode *ip = VTOI(vp); 155739674Smckusick 155840293Smckusick printf("tag VT_UFS, ino %d, on dev %d, %d", ip->i_number, 155940293Smckusick major(ip->i_dev), minor(ip->i_dev)); 156040293Smckusick #ifdef FIFO 156140293Smckusick if (vp->v_type == VFIFO) 156240293Smckusick fifo_printinfo(vp); 156340293Smckusick #endif /* FIFO */ 156440293Smckusick printf("%s\n", (ip->i_flag & ILOCKED) ? " (LOCKED)" : ""); 156539900Smckusick if (ip->i_spare0 == 0) 156639900Smckusick return; 156739900Smckusick printf("\towner pid %d", ip->i_spare0); 156839900Smckusick if (ip->i_spare1) 156939900Smckusick printf(" waiting pid %d", ip->i_spare1); 157039900Smckusick printf("\n"); 157139674Smckusick } 157239674Smckusick 157339674Smckusick /* 157439628Smckusick * Read wrapper for special devices. 157539628Smckusick */ 157639628Smckusick ufsspec_read(vp, uio, ioflag, cred) 157739628Smckusick struct vnode *vp; 157839628Smckusick struct uio *uio; 157939628Smckusick int ioflag; 158039628Smckusick struct ucred *cred; 158139628Smckusick { 158239628Smckusick 158339628Smckusick /* 158439628Smckusick * Set access flag. 158539628Smckusick */ 158639628Smckusick VTOI(vp)->i_flag |= IACC; 158739628Smckusick return (spec_read(vp, uio, ioflag, cred)); 158839628Smckusick } 158939628Smckusick 159039628Smckusick /* 159139628Smckusick * Write wrapper for special devices. 159239628Smckusick */ 159339628Smckusick ufsspec_write(vp, uio, ioflag, cred) 159439628Smckusick struct vnode *vp; 159539628Smckusick struct uio *uio; 159639628Smckusick int ioflag; 159739628Smckusick struct ucred *cred; 159839628Smckusick { 159939628Smckusick 160039628Smckusick /* 160139628Smckusick * Set update and change flags. 160239628Smckusick */ 160339628Smckusick VTOI(vp)->i_flag |= IUPD|ICHG; 160439628Smckusick return (spec_write(vp, uio, ioflag, cred)); 160539628Smckusick } 160639628Smckusick 160739628Smckusick /* 160839628Smckusick * Close wrapper for special devices. 160939628Smckusick * 161039628Smckusick * Update the times on the inode then do device close. 161139628Smckusick */ 161239628Smckusick ufsspec_close(vp, fflag, cred) 161339628Smckusick struct vnode *vp; 161439628Smckusick int fflag; 161539628Smckusick struct ucred *cred; 161639628Smckusick { 161739628Smckusick register struct inode *ip = VTOI(vp); 161839628Smckusick 161939815Smckusick if (vp->v_usecount > 1 && !(ip->i_flag & ILOCKED)) 162039628Smckusick ITIMES(ip, &time, &time); 162139628Smckusick return (spec_close(vp, fflag, cred)); 162239628Smckusick } 162339628Smckusick 162440290Smckusick #ifdef FIFO 162539628Smckusick /* 162640290Smckusick * Read wrapper for fifo's 162740290Smckusick */ 162840290Smckusick ufsfifo_read(vp, uio, ioflag, cred) 162940290Smckusick struct vnode *vp; 163040290Smckusick struct uio *uio; 163140290Smckusick int ioflag; 163240290Smckusick struct ucred *cred; 163340290Smckusick { 163440290Smckusick 163540290Smckusick /* 163640290Smckusick * Set access flag. 163740290Smckusick */ 163840290Smckusick VTOI(vp)->i_flag |= IACC; 163940290Smckusick return (fifo_read(vp, uio, ioflag, cred)); 164040290Smckusick } 164140290Smckusick 164240290Smckusick /* 164340290Smckusick * Write wrapper for fifo's. 164440290Smckusick */ 164540290Smckusick ufsfifo_write(vp, uio, ioflag, cred) 164640290Smckusick struct vnode *vp; 164740290Smckusick struct uio *uio; 164840290Smckusick int ioflag; 164940290Smckusick struct ucred *cred; 165040290Smckusick { 165140290Smckusick 165240290Smckusick /* 165340290Smckusick * Set update and change flags. 165440290Smckusick */ 165540290Smckusick VTOI(vp)->i_flag |= IUPD|ICHG; 165640290Smckusick return (fifo_write(vp, uio, ioflag, cred)); 165740290Smckusick } 165840290Smckusick 165940290Smckusick /* 166040290Smckusick * Close wrapper for fifo's. 166140290Smckusick * 166240290Smckusick * Update the times on the inode then do device close. 166340290Smckusick */ 166440290Smckusick ufsfifo_close(vp, fflag, cred) 166540290Smckusick struct vnode *vp; 166640290Smckusick int fflag; 166740290Smckusick struct ucred *cred; 166840290Smckusick { 166940290Smckusick register struct inode *ip = VTOI(vp); 167040290Smckusick 167140290Smckusick if (vp->v_usecount > 1 && !(ip->i_flag & ILOCKED)) 167240290Smckusick ITIMES(ip, &time, &time); 167340290Smckusick return (fifo_close(vp, fflag, cred)); 167440290Smckusick } 167540290Smckusick #endif /* FIFO */ 167640290Smckusick 167740290Smckusick /* 167837737Smckusick * Make a new file. 167937737Smckusick */ 168037737Smckusick maknode(mode, ndp, ipp) 168137737Smckusick int mode; 168237737Smckusick register struct nameidata *ndp; 168337737Smckusick struct inode **ipp; 168437737Smckusick { 168537737Smckusick register struct inode *ip; 168637737Smckusick struct inode *tip; 168737737Smckusick register struct inode *pdir = VTOI(ndp->ni_dvp); 168837737Smckusick ino_t ipref; 168937737Smckusick int error; 169037737Smckusick 169137737Smckusick *ipp = 0; 169241312Smckusick if ((mode & IFMT) == 0) 169341312Smckusick mode |= IFREG; 169437737Smckusick if ((mode & IFMT) == IFDIR) 169537737Smckusick ipref = dirpref(pdir->i_fs); 169637737Smckusick else 169737737Smckusick ipref = pdir->i_number; 169841312Smckusick if (error = ialloc(pdir, ipref, mode, ndp->ni_cred, &tip)) { 169937737Smckusick iput(pdir); 170037737Smckusick return (error); 170137737Smckusick } 170237737Smckusick ip = tip; 170341312Smckusick ip->i_uid = ndp->ni_cred->cr_uid; 170441312Smckusick ip->i_gid = pdir->i_gid; 170537737Smckusick #ifdef QUOTA 170641312Smckusick if ((error = getinoquota(ip)) || 170741312Smckusick (error = chkiq(ip, 1, ndp->ni_cred, 0))) { 170841312Smckusick ifree(ip, ip->i_number, mode); 170941312Smckusick iput(ip); 171041312Smckusick iput(pdir); 171141312Smckusick return (error); 171241312Smckusick } 171337737Smckusick #endif 171437737Smckusick ip->i_flag |= IACC|IUPD|ICHG; 171537737Smckusick ip->i_mode = mode; 171637737Smckusick ITOV(ip)->v_type = IFTOVT(mode); /* Rest init'd in iget() */ 171737737Smckusick ip->i_nlink = 1; 171837737Smckusick if ((ip->i_mode & ISGID) && !groupmember(ip->i_gid, ndp->ni_cred) && 171937737Smckusick suser(ndp->ni_cred, NULL)) 172037737Smckusick ip->i_mode &= ~ISGID; 172137737Smckusick 172237737Smckusick /* 172337737Smckusick * Make sure inode goes to disk before directory entry. 172437737Smckusick */ 172541312Smckusick if (error = iupdat(ip, &time, &time, 1)) 172641312Smckusick goto bad; 172747219Smckusick if (error = direnter(ip, ndp)) 172841312Smckusick goto bad; 172947219Smckusick iput(pdir); 173037737Smckusick *ipp = ip; 173137737Smckusick return (0); 173241312Smckusick 173341312Smckusick bad: 173441312Smckusick /* 173541312Smckusick * Write error occurred trying to update the inode 173641312Smckusick * or the directory so must deallocate the inode. 173741312Smckusick */ 173847219Smckusick iput(pdir); 173941312Smckusick ip->i_nlink = 0; 174041312Smckusick ip->i_flag |= ICHG; 174141312Smckusick iput(ip); 174241312Smckusick return (error); 174337737Smckusick } 174446207Smckusick 174546207Smckusick /* 174646207Smckusick * Advisory record locking support 174746207Smckusick */ 174846207Smckusick ufs_advlock(vp, id, op, fl, flags) 174946207Smckusick struct vnode *vp; 175046207Smckusick caddr_t id; 175146207Smckusick int op; 175246207Smckusick register struct flock *fl; 175346207Smckusick int flags; 175446207Smckusick { 175546207Smckusick register struct inode *ip = VTOI(vp); 175646207Smckusick register struct lockf *lock; 175746207Smckusick off_t start, end; 175846207Smckusick int error; 175946207Smckusick 176046207Smckusick /* 176146207Smckusick * Avoid the common case of unlocking when inode has no locks. 176246207Smckusick */ 176346207Smckusick if (ip->i_lockf == (struct lockf *)0) { 176446207Smckusick if (op != F_SETLK) { 176546207Smckusick fl->l_type = F_UNLCK; 176646207Smckusick return (0); 176746207Smckusick } 176846207Smckusick } 176946207Smckusick /* 177046207Smckusick * Convert the flock structure into a start and end. 177146207Smckusick */ 177246207Smckusick switch (fl->l_whence) { 177346207Smckusick 177446207Smckusick case SEEK_SET: 177546207Smckusick case SEEK_CUR: 177646207Smckusick /* 177746207Smckusick * Caller is responsible for adding any necessary offset 177846207Smckusick * when SEEK_CUR is used. 177946207Smckusick */ 178046207Smckusick start = fl->l_start; 178146207Smckusick break; 178246207Smckusick 178346207Smckusick case SEEK_END: 178446207Smckusick start = ip->i_size + fl->l_start; 178546207Smckusick break; 178646207Smckusick 178746207Smckusick default: 178846207Smckusick return (EINVAL); 178946207Smckusick } 179046207Smckusick if (start < 0) 179146207Smckusick return (EINVAL); 179246207Smckusick if (fl->l_len == 0) 179346207Smckusick end = -1; 179446207Smckusick else 179546507Smckusick end = start + fl->l_len - 1; 179646207Smckusick /* 179746207Smckusick * Create the lockf structure 179846207Smckusick */ 179946207Smckusick MALLOC(lock, struct lockf *, sizeof *lock, M_LOCKF, M_WAITOK); 180046207Smckusick lock->lf_start = start; 180146207Smckusick lock->lf_end = end; 180246207Smckusick lock->lf_id = id; 180346207Smckusick lock->lf_inode = ip; 180446207Smckusick lock->lf_type = fl->l_type; 180546207Smckusick lock->lf_next = (struct lockf *)0; 180646207Smckusick lock->lf_block = (struct lockf *)0; 180746207Smckusick lock->lf_flags = flags; 180846207Smckusick /* 180946207Smckusick * Do the requested operation. 181046207Smckusick */ 181146207Smckusick switch(op) { 181246207Smckusick case F_SETLK: 181346679Smckusick return (lf_setlock(lock)); 181446207Smckusick 181546207Smckusick case F_UNLCK: 181646679Smckusick error = lf_clearlock(lock); 181746679Smckusick FREE(lock, M_LOCKF); 181846679Smckusick return (error); 181946207Smckusick 182046207Smckusick case F_GETLK: 182146679Smckusick error = lf_getlock(lock, fl); 182246679Smckusick FREE(lock, M_LOCKF); 182346679Smckusick return (error); 182446207Smckusick 182546207Smckusick default: 182646207Smckusick free(lock, M_LOCKF); 182746207Smckusick return (EINVAL); 182846207Smckusick } 182946207Smckusick /* NOTREACHED */ 183046207Smckusick } 1831