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*46251Smckusick * @(#)lfs_vnops.c 7.52 (Berkeley) 02/03/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(), 11639435Smckusick spec_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 */ 13239597Smckusick spec_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(), 16540290Smckusick fifo_badop(), 16640290Smckusick fifo_nullop(); 16740290Smckusick 16840290Smckusick struct vnodeops fifo_inodeops = { 16940290Smckusick fifo_lookup, /* lookup */ 17040290Smckusick fifo_badop, /* create */ 17140290Smckusick fifo_badop, /* mknod */ 17240290Smckusick fifo_open, /* open */ 17340290Smckusick ufsfifo_close, /* close */ 17440290Smckusick ufs_access, /* access */ 17540290Smckusick ufs_getattr, /* getattr */ 17640290Smckusick ufs_setattr, /* setattr */ 17740290Smckusick ufsfifo_read, /* read */ 17840290Smckusick ufsfifo_write, /* write */ 17940290Smckusick fifo_ioctl, /* ioctl */ 18040290Smckusick fifo_select, /* select */ 18140290Smckusick fifo_badop, /* mmap */ 18240290Smckusick fifo_nullop, /* fsync */ 18340290Smckusick fifo_badop, /* seek */ 18440290Smckusick fifo_badop, /* remove */ 18540290Smckusick fifo_badop, /* link */ 18640290Smckusick fifo_badop, /* rename */ 18740290Smckusick fifo_badop, /* mkdir */ 18840290Smckusick fifo_badop, /* rmdir */ 18940290Smckusick fifo_badop, /* symlink */ 19040290Smckusick fifo_badop, /* readdir */ 19140290Smckusick fifo_badop, /* readlink */ 19240290Smckusick fifo_badop, /* abortop */ 19340290Smckusick ufs_inactive, /* inactive */ 19440290Smckusick ufs_reclaim, /* reclaim */ 19540290Smckusick ufs_lock, /* lock */ 19640290Smckusick ufs_unlock, /* unlock */ 19740290Smckusick fifo_bmap, /* bmap */ 19840290Smckusick fifo_badop, /* strategy */ 19940290Smckusick ufs_print, /* print */ 20040290Smckusick ufs_islocked, /* islocked */ 20146207Smckusick fifo_advlock, /* advlock */ 20237737Smckusick }; 20340290Smckusick #endif /* FIFO */ 20440290Smckusick 20540290Smckusick enum vtype iftovt_tab[16] = { 20640290Smckusick VNON, VFIFO, VCHR, VNON, VDIR, VNON, VBLK, VNON, 20740290Smckusick VREG, VNON, VLNK, VNON, VSOCK, VNON, VNON, VBAD, 20837737Smckusick }; 20940290Smckusick int vttoif_tab[9] = { 21040290Smckusick 0, IFREG, IFDIR, IFBLK, IFCHR, IFLNK, IFSOCK, IFIFO, IFMT, 21140290Smckusick }; 2126254Sroot 2139167Ssam /* 21437737Smckusick * Create a regular file 2159167Ssam */ 21637737Smckusick ufs_create(ndp, vap) 21737737Smckusick struct nameidata *ndp; 21837737Smckusick struct vattr *vap; 2196254Sroot { 22037737Smckusick struct inode *ip; 22137737Smckusick int error; 2226254Sroot 22337737Smckusick if (error = maknode(MAKEIMODE(vap->va_type, vap->va_mode), ndp, &ip)) 22437737Smckusick return (error); 22537737Smckusick ndp->ni_vp = ITOV(ip); 22637737Smckusick return (0); 2276254Sroot } 2286254Sroot 22937Sbill /* 23037737Smckusick * Mknod vnode call 2316254Sroot */ 23237737Smckusick /* ARGSUSED */ 23337737Smckusick ufs_mknod(ndp, vap, cred) 23437737Smckusick struct nameidata *ndp; 23537737Smckusick struct ucred *cred; 23637737Smckusick struct vattr *vap; 2376254Sroot { 23839435Smckusick register struct vnode *vp; 23937737Smckusick struct inode *ip; 24037737Smckusick int error; 2416254Sroot 24237737Smckusick if (error = maknode(MAKEIMODE(vap->va_type, vap->va_mode), ndp, &ip)) 24337737Smckusick return (error); 24440290Smckusick ip->i_flag |= IACC|IUPD|ICHG; 24540290Smckusick if (vap->va_rdev != VNOVAL) { 24637737Smckusick /* 24737737Smckusick * Want to be able to use this to make badblock 24837737Smckusick * inodes, so don't truncate the dev number. 24937737Smckusick */ 25039608Smckusick ip->i_rdev = vap->va_rdev; 25112756Ssam } 25237737Smckusick /* 25337737Smckusick * Remove inode so that it will be reloaded by iget and 25437737Smckusick * checked to see if it is an alias of an existing entry 25537737Smckusick * in the inode cache. 25637737Smckusick */ 25740290Smckusick vp = ITOV(ip); 25840290Smckusick vput(vp); 25939435Smckusick vp->v_type = VNON; 26039435Smckusick vgone(vp); 26137737Smckusick return (0); 2626254Sroot } 2636254Sroot 2646254Sroot /* 26537737Smckusick * Open called. 26637737Smckusick * 26737737Smckusick * Nothing to do. 2686254Sroot */ 26937737Smckusick /* ARGSUSED */ 27037737Smckusick ufs_open(vp, mode, cred) 27137737Smckusick struct vnode *vp; 27237737Smckusick int mode; 27337737Smckusick struct ucred *cred; 2746254Sroot { 2756254Sroot 27637737Smckusick return (0); 2776254Sroot } 2786254Sroot 2796254Sroot /* 28037737Smckusick * Close called 28137737Smckusick * 28237737Smckusick * Update the times on the inode. 2836254Sroot */ 28437737Smckusick /* ARGSUSED */ 28537737Smckusick ufs_close(vp, fflag, cred) 28637737Smckusick struct vnode *vp; 28737737Smckusick int fflag; 28837737Smckusick struct ucred *cred; 2896254Sroot { 29037737Smckusick register struct inode *ip = VTOI(vp); 2916254Sroot 29239815Smckusick if (vp->v_usecount > 1 && !(ip->i_flag & ILOCKED)) 29337737Smckusick ITIMES(ip, &time, &time); 29437737Smckusick return (0); 2956254Sroot } 2966254Sroot 29741312Smckusick /* 29841312Smckusick * Check mode permission on inode pointer. Mode is READ, WRITE or EXEC. 29941312Smckusick * The mode is shifted to select the owner/group/other fields. The 30041312Smckusick * super user is granted all permissions. 30141312Smckusick */ 30237737Smckusick ufs_access(vp, mode, cred) 30337737Smckusick struct vnode *vp; 30441312Smckusick register int mode; 30537737Smckusick struct ucred *cred; 3066254Sroot { 30741312Smckusick register struct inode *ip = VTOI(vp); 30841312Smckusick register gid_t *gp; 30941312Smckusick int i, error; 3106254Sroot 31141312Smckusick #ifdef DIAGNOSTIC 31241312Smckusick if (!VOP_ISLOCKED(vp)) { 31341312Smckusick vprint("ufs_access: not locked", vp); 31441312Smckusick panic("ufs_access: not locked"); 31541312Smckusick } 31641312Smckusick #endif 31741312Smckusick #ifdef QUOTA 31841312Smckusick if (mode & VWRITE) { 31941312Smckusick switch (vp->v_type) { 32041312Smckusick case VREG: case VDIR: case VLNK: 32141312Smckusick if (error = getinoquota(ip)) 32241312Smckusick return (error); 32341312Smckusick } 32441312Smckusick } 32541312Smckusick #endif /* QUOTA */ 32641312Smckusick /* 32741312Smckusick * If you're the super-user, you always get access. 32841312Smckusick */ 32941312Smckusick if (cred->cr_uid == 0) 33041312Smckusick return (0); 33141312Smckusick /* 33241312Smckusick * Access check is based on only one of owner, group, public. 33341312Smckusick * If not owner, then check group. If not a member of the 33441312Smckusick * group, then check public access. 33541312Smckusick */ 33641312Smckusick if (cred->cr_uid != ip->i_uid) { 33741312Smckusick mode >>= 3; 33841312Smckusick gp = cred->cr_groups; 33941312Smckusick for (i = 0; i < cred->cr_ngroups; i++, gp++) 34041312Smckusick if (ip->i_gid == *gp) 34141312Smckusick goto found; 34241312Smckusick mode >>= 3; 34341312Smckusick found: 34441312Smckusick ; 34541312Smckusick } 34641312Smckusick if ((ip->i_mode & mode) != 0) 34741312Smckusick return (0); 34841312Smckusick return (EACCES); 3496254Sroot } 3506254Sroot 35137737Smckusick /* ARGSUSED */ 35237737Smckusick ufs_getattr(vp, vap, cred) 35337737Smckusick struct vnode *vp; 35437737Smckusick register struct vattr *vap; 35537737Smckusick struct ucred *cred; 3566254Sroot { 35737737Smckusick register struct inode *ip = VTOI(vp); 3586254Sroot 35937737Smckusick ITIMES(ip, &time, &time); 3606254Sroot /* 36137737Smckusick * Copy from inode table 3626254Sroot */ 36337737Smckusick vap->va_fsid = ip->i_dev; 36437737Smckusick vap->va_fileid = ip->i_number; 36537737Smckusick vap->va_mode = ip->i_mode & ~IFMT; 36637737Smckusick vap->va_nlink = ip->i_nlink; 36737737Smckusick vap->va_uid = ip->i_uid; 36837737Smckusick vap->va_gid = ip->i_gid; 36937737Smckusick vap->va_rdev = (dev_t)ip->i_rdev; 37041312Smckusick #ifdef tahoe 37141312Smckusick vap->va_size = ip->i_size; 37241312Smckusick vap->va_size_rsv = 0; 37341312Smckusick #else 37440641Smckusick vap->va_qsize = ip->i_din.di_qsize; 37541312Smckusick #endif 37637737Smckusick vap->va_atime.tv_sec = ip->i_atime; 37738578Smckusick vap->va_atime.tv_usec = 0; 37837737Smckusick vap->va_mtime.tv_sec = ip->i_mtime; 37938578Smckusick vap->va_mtime.tv_usec = 0; 38037737Smckusick vap->va_ctime.tv_sec = ip->i_ctime; 38138578Smckusick vap->va_ctime.tv_usec = 0; 38238254Smckusick vap->va_flags = ip->i_flags; 38338254Smckusick vap->va_gen = ip->i_gen; 38437737Smckusick /* this doesn't belong here */ 38537737Smckusick if (vp->v_type == VBLK) 38637737Smckusick vap->va_blocksize = BLKDEV_IOSIZE; 38737737Smckusick else if (vp->v_type == VCHR) 38837737Smckusick vap->va_blocksize = MAXBSIZE; 3897142Smckusick else 39037737Smckusick vap->va_blocksize = ip->i_fs->fs_bsize; 39138657Smckusick vap->va_bytes = dbtob(ip->i_blocks); 39240641Smckusick vap->va_bytes_rsv = 0; 39337737Smckusick vap->va_type = vp->v_type; 39437737Smckusick return (0); 3956254Sroot } 3966254Sroot 3976254Sroot /* 39837737Smckusick * Set attribute vnode op. called from several syscalls 3996254Sroot */ 40037737Smckusick ufs_setattr(vp, vap, cred) 40137737Smckusick register struct vnode *vp; 40237737Smckusick register struct vattr *vap; 40337737Smckusick register struct ucred *cred; 4046254Sroot { 40537737Smckusick register struct inode *ip = VTOI(vp); 40637737Smckusick int error = 0; 4076254Sroot 40837737Smckusick /* 40937737Smckusick * Check for unsetable attributes. 41037737Smckusick */ 41137737Smckusick if ((vap->va_type != VNON) || (vap->va_nlink != VNOVAL) || 41237737Smckusick (vap->va_fsid != VNOVAL) || (vap->va_fileid != VNOVAL) || 41337737Smckusick (vap->va_blocksize != VNOVAL) || (vap->va_rdev != VNOVAL) || 41438254Smckusick ((int)vap->va_bytes != VNOVAL) || (vap->va_gen != VNOVAL)) { 41537737Smckusick return (EINVAL); 41616540Ssam } 41737737Smckusick /* 41837737Smckusick * Go through the fields and update iff not VNOVAL. 41937737Smckusick */ 42037737Smckusick if (vap->va_uid != (u_short)VNOVAL || vap->va_gid != (u_short)VNOVAL) 42137737Smckusick if (error = chown1(vp, vap->va_uid, vap->va_gid, cred)) 42237737Smckusick return (error); 42337737Smckusick if (vap->va_size != VNOVAL) { 42437737Smckusick if (vp->v_type == VDIR) 42537737Smckusick return (EISDIR); 42639674Smckusick if (error = itrunc(ip, vap->va_size, 0)) /* XXX IO_SYNC? */ 42737737Smckusick return (error); 42813878Ssam } 42937737Smckusick if (vap->va_atime.tv_sec != VNOVAL || vap->va_mtime.tv_sec != VNOVAL) { 43037773Smckusick if (cred->cr_uid != ip->i_uid && 43137773Smckusick (error = suser(cred, &u.u_acflag))) 43237773Smckusick return (error); 43337737Smckusick if (vap->va_atime.tv_sec != VNOVAL) 43437737Smckusick ip->i_flag |= IACC; 43537737Smckusick if (vap->va_mtime.tv_sec != VNOVAL) 43637737Smckusick ip->i_flag |= IUPD; 43737737Smckusick ip->i_flag |= ICHG; 43837737Smckusick if (error = iupdat(ip, &vap->va_atime, &vap->va_mtime, 1)) 43937737Smckusick return (error); 4406254Sroot } 44137737Smckusick if (vap->va_mode != (u_short)VNOVAL) 44237737Smckusick error = chmod1(vp, (int)vap->va_mode, cred); 44338254Smckusick if (vap->va_flags != VNOVAL) { 44438254Smckusick if (cred->cr_uid != ip->i_uid && 44538254Smckusick (error = suser(cred, &u.u_acflag))) 44638254Smckusick return (error); 44738254Smckusick if (cred->cr_uid == 0) { 44838254Smckusick ip->i_flags = vap->va_flags; 44938254Smckusick } else { 45038254Smckusick ip->i_flags &= 0xffff0000; 45138254Smckusick ip->i_flags |= (vap->va_flags & 0xffff); 45238254Smckusick } 45338254Smckusick ip->i_flag |= ICHG; 45438254Smckusick } 45537737Smckusick return (error); 4566254Sroot } 4576254Sroot 4586254Sroot /* 4599167Ssam * Change the mode on a file. 4609167Ssam * Inode must be locked before calling. 4619167Ssam */ 46237737Smckusick chmod1(vp, mode, cred) 46337737Smckusick register struct vnode *vp; 4647701Ssam register int mode; 46537737Smckusick struct ucred *cred; 4667701Ssam { 46737737Smckusick register struct inode *ip = VTOI(vp); 46837773Smckusick int error; 4697868Sroot 47037773Smckusick if (cred->cr_uid != ip->i_uid && 47137773Smckusick (error = suser(cred, &u.u_acflag))) 47237773Smckusick return (error); 47337737Smckusick if (cred->cr_uid) { 47446206Smckusick if (vp->v_type != VDIR && (mode & ISVTX)) 47545783Sbostic return (EFTYPE); 47646206Smckusick if (!groupmember(ip->i_gid, cred) && (mode & ISGID)) 47745783Sbostic return (EPERM); 4787439Sroot } 47945783Sbostic ip->i_mode &= ~07777; 48037737Smckusick ip->i_mode |= mode & 07777; 4816254Sroot ip->i_flag |= ICHG; 48237737Smckusick if ((vp->v_flag & VTEXT) && (ip->i_mode & ISVTX) == 0) 48345722Smckusick (void) vnode_pager_uncache(vp); 48421015Smckusick return (0); 4855992Swnj } 4865992Swnj 4879167Ssam /* 4887701Ssam * Perform chown operation on inode ip; 4897701Ssam * inode must be locked prior to call. 4907701Ssam */ 49137737Smckusick chown1(vp, uid, gid, cred) 49237737Smckusick register struct vnode *vp; 49337737Smckusick uid_t uid; 49437737Smckusick gid_t gid; 49537737Smckusick struct ucred *cred; 4967701Ssam { 49737737Smckusick register struct inode *ip = VTOI(vp); 49841312Smckusick uid_t ouid; 49941312Smckusick gid_t ogid; 50041312Smckusick int error = 0; 5017701Ssam #ifdef QUOTA 50241312Smckusick register int i; 50341312Smckusick long change; 50411811Ssam #endif 5057701Ssam 50637737Smckusick if (uid == (u_short)VNOVAL) 50711811Ssam uid = ip->i_uid; 50837737Smckusick if (gid == (u_short)VNOVAL) 50911811Ssam gid = ip->i_gid; 51036614Sbostic /* 51136614Sbostic * If we don't own the file, are trying to change the owner 51236614Sbostic * of the file, or are not a member of the target group, 51336614Sbostic * the caller must be superuser or the call fails. 51436614Sbostic */ 51537737Smckusick if ((cred->cr_uid != ip->i_uid || uid != ip->i_uid || 51637737Smckusick !groupmember((gid_t)gid, cred)) && 51737737Smckusick (error = suser(cred, &u.u_acflag))) 51837737Smckusick return (error); 51941312Smckusick ouid = ip->i_uid; 52041312Smckusick ogid = ip->i_gid; 52111811Ssam #ifdef QUOTA 52241312Smckusick if (error = getinoquota(ip)) 52341312Smckusick return (error); 52441312Smckusick if (ouid == uid) { 52541312Smckusick dqrele(vp, ip->i_dquot[USRQUOTA]); 52641312Smckusick ip->i_dquot[USRQUOTA] = NODQUOT; 52741312Smckusick } 52841312Smckusick if (ogid == gid) { 52941312Smckusick dqrele(vp, ip->i_dquot[GRPQUOTA]); 53041312Smckusick ip->i_dquot[GRPQUOTA] = NODQUOT; 53141312Smckusick } 53241312Smckusick change = ip->i_blocks; 53341312Smckusick (void) chkdq(ip, -change, cred, CHOWN); 53441312Smckusick (void) chkiq(ip, -1, cred, CHOWN); 53541312Smckusick for (i = 0; i < MAXQUOTAS; i++) { 53641312Smckusick dqrele(vp, ip->i_dquot[i]); 53741312Smckusick ip->i_dquot[i] = NODQUOT; 53841312Smckusick } 5397482Skre #endif 54011811Ssam ip->i_uid = uid; 54111811Ssam ip->i_gid = gid; 5427701Ssam #ifdef QUOTA 54341312Smckusick if ((error = getinoquota(ip)) == 0) { 54441312Smckusick if (ouid == uid) { 54541312Smckusick dqrele(vp, ip->i_dquot[USRQUOTA]); 54641312Smckusick ip->i_dquot[USRQUOTA] = NODQUOT; 54741312Smckusick } 54841312Smckusick if (ogid == gid) { 54941312Smckusick dqrele(vp, ip->i_dquot[GRPQUOTA]); 55041312Smckusick ip->i_dquot[GRPQUOTA] = NODQUOT; 55141312Smckusick } 55241312Smckusick if ((error = chkdq(ip, change, cred, CHOWN)) == 0) { 55341312Smckusick if ((error = chkiq(ip, 1, cred, CHOWN)) == 0) 55441927Smckusick goto good; 55541312Smckusick else 55641312Smckusick (void) chkdq(ip, -change, cred, CHOWN|FORCE); 55741312Smckusick } 55841312Smckusick for (i = 0; i < MAXQUOTAS; i++) { 55941312Smckusick dqrele(vp, ip->i_dquot[i]); 56041312Smckusick ip->i_dquot[i] = NODQUOT; 56141312Smckusick } 56241312Smckusick } 56341312Smckusick ip->i_uid = ouid; 56441312Smckusick ip->i_gid = ogid; 56541312Smckusick if (getinoquota(ip) == 0) { 56641312Smckusick if (ouid == uid) { 56741312Smckusick dqrele(vp, ip->i_dquot[USRQUOTA]); 56841312Smckusick ip->i_dquot[USRQUOTA] = NODQUOT; 56941312Smckusick } 57041312Smckusick if (ogid == gid) { 57141312Smckusick dqrele(vp, ip->i_dquot[GRPQUOTA]); 57241312Smckusick ip->i_dquot[GRPQUOTA] = NODQUOT; 57341312Smckusick } 57441927Smckusick (void) chkdq(ip, change, cred, FORCE|CHOWN); 57541927Smckusick (void) chkiq(ip, 1, cred, FORCE|CHOWN); 57642440Smckusick (void) getinoquota(ip); 57741312Smckusick } 57842440Smckusick return (error); 57941927Smckusick good: 58042440Smckusick if (getinoquota(ip)) 58142440Smckusick panic("chown: lost quota"); 58242440Smckusick #endif /* QUOTA */ 58341312Smckusick if (ouid != uid || ogid != gid) 58441312Smckusick ip->i_flag |= ICHG; 58541312Smckusick if (ouid != uid && cred->cr_uid != 0) 58641312Smckusick ip->i_mode &= ~ISUID; 58741312Smckusick if (ogid != gid && cred->cr_uid != 0) 58841312Smckusick ip->i_mode &= ~ISGID; 58912646Ssam return (0); 59037Sbill } 59137Sbill 59239608Smckusick /* 59339608Smckusick * Vnode op for reading. 59439608Smckusick */ 59537737Smckusick /* ARGSUSED */ 59639608Smckusick ufs_read(vp, uio, ioflag, cred) 59739608Smckusick struct vnode *vp; 59839608Smckusick register struct uio *uio; 59939608Smckusick int ioflag; 60039608Smckusick struct ucred *cred; 60139608Smckusick { 60239608Smckusick register struct inode *ip = VTOI(vp); 60339608Smckusick register struct fs *fs; 60439608Smckusick struct buf *bp; 60539608Smckusick daddr_t lbn, bn, rablock; 60639896Smckusick int size, diff, error = 0; 60739608Smckusick long n, on, type; 60839608Smckusick 60939608Smckusick if (uio->uio_rw != UIO_READ) 61039608Smckusick panic("ufs_read mode"); 61139608Smckusick type = ip->i_mode & IFMT; 61239608Smckusick if (type != IFDIR && type != IFREG && type != IFLNK) 61339608Smckusick panic("ufs_read type"); 61439608Smckusick if (uio->uio_resid == 0) 61539608Smckusick return (0); 61639608Smckusick if (uio->uio_offset < 0) 61739608Smckusick return (EINVAL); 61839608Smckusick ip->i_flag |= IACC; 61939608Smckusick fs = ip->i_fs; 62039608Smckusick do { 62139608Smckusick lbn = lblkno(fs, uio->uio_offset); 62239608Smckusick on = blkoff(fs, uio->uio_offset); 62339608Smckusick n = MIN((unsigned)(fs->fs_bsize - on), uio->uio_resid); 62439608Smckusick diff = ip->i_size - uio->uio_offset; 62539608Smckusick if (diff <= 0) 62639608Smckusick return (0); 62739608Smckusick if (diff < n) 62839608Smckusick n = diff; 62939608Smckusick size = blksize(fs, ip, lbn); 63039674Smckusick rablock = lbn + 1; 63139896Smckusick if (vp->v_lastr + 1 == lbn && 63239896Smckusick lblktosize(fs, rablock) < ip->i_size) 63339896Smckusick error = breada(ITOV(ip), lbn, size, rablock, 63439896Smckusick blksize(fs, ip, rablock), NOCRED, &bp); 63539608Smckusick else 63639674Smckusick error = bread(ITOV(ip), lbn, size, NOCRED, &bp); 63739815Smckusick vp->v_lastr = lbn; 63839608Smckusick n = MIN(n, size - bp->b_resid); 63939608Smckusick if (error) { 64039608Smckusick brelse(bp); 64139608Smckusick return (error); 64239608Smckusick } 64339608Smckusick error = uiomove(bp->b_un.b_addr + on, (int)n, uio); 64439608Smckusick if (n + on == fs->fs_bsize || uio->uio_offset == ip->i_size) 64539608Smckusick bp->b_flags |= B_AGE; 64639608Smckusick brelse(bp); 64739608Smckusick } while (error == 0 && uio->uio_resid > 0 && n != 0); 64839608Smckusick return (error); 64939608Smckusick } 65039608Smckusick 65139608Smckusick /* 65239608Smckusick * Vnode op for writing. 65339608Smckusick */ 65439608Smckusick ufs_write(vp, uio, ioflag, cred) 65539608Smckusick register struct vnode *vp; 65639608Smckusick struct uio *uio; 65739608Smckusick int ioflag; 65839608Smckusick struct ucred *cred; 65939608Smckusick { 66039608Smckusick register struct inode *ip = VTOI(vp); 66139608Smckusick register struct fs *fs; 66239608Smckusick struct buf *bp; 66339608Smckusick daddr_t lbn, bn; 66439608Smckusick u_long osize; 66545722Smckusick int n, on, flags; 66645722Smckusick int size, resid, error = 0; 66739608Smckusick 66839608Smckusick if (uio->uio_rw != UIO_WRITE) 66939608Smckusick panic("ufs_write mode"); 67039608Smckusick switch (vp->v_type) { 67139608Smckusick case VREG: 67239608Smckusick if (ioflag & IO_APPEND) 67339608Smckusick uio->uio_offset = ip->i_size; 67439608Smckusick /* fall through */ 67539608Smckusick case VLNK: 67639608Smckusick break; 67739608Smckusick 67839608Smckusick case VDIR: 67939608Smckusick if ((ioflag & IO_SYNC) == 0) 68039608Smckusick panic("ufs_write nonsync dir write"); 68139608Smckusick break; 68239608Smckusick 68339608Smckusick default: 68439608Smckusick panic("ufs_write type"); 68539608Smckusick } 68639608Smckusick if (uio->uio_offset < 0) 68739608Smckusick return (EINVAL); 68839608Smckusick if (uio->uio_resid == 0) 68939608Smckusick return (0); 69039608Smckusick /* 69139608Smckusick * Maybe this should be above the vnode op call, but so long as 69239608Smckusick * file servers have no limits, i don't think it matters 69339608Smckusick */ 69439608Smckusick if (vp->v_type == VREG && 69539608Smckusick uio->uio_offset + uio->uio_resid > 69639608Smckusick u.u_rlimit[RLIMIT_FSIZE].rlim_cur) { 69739608Smckusick psignal(u.u_procp, SIGXFSZ); 69839608Smckusick return (EFBIG); 69939608Smckusick } 70039608Smckusick resid = uio->uio_resid; 70139608Smckusick osize = ip->i_size; 70239608Smckusick fs = ip->i_fs; 70339674Smckusick flags = 0; 70439674Smckusick if (ioflag & IO_SYNC) 70539674Smckusick flags = B_SYNC; 70639608Smckusick do { 70739608Smckusick lbn = lblkno(fs, uio->uio_offset); 70839608Smckusick on = blkoff(fs, uio->uio_offset); 70939608Smckusick n = MIN((unsigned)(fs->fs_bsize - on), uio->uio_resid); 71039608Smckusick if (n < fs->fs_bsize) 71139674Smckusick flags |= B_CLRBUF; 71239608Smckusick else 71339674Smckusick flags &= ~B_CLRBUF; 71439674Smckusick if (error = balloc(ip, lbn, (int)(on + n), &bp, flags)) 71539608Smckusick break; 71639674Smckusick bn = bp->b_blkno; 71745722Smckusick if (uio->uio_offset + n > ip->i_size) { 71839608Smckusick ip->i_size = uio->uio_offset + n; 71945722Smckusick vnode_pager_setsize(vp, ip->i_size); 72045722Smckusick } 72139608Smckusick size = blksize(fs, ip, lbn); 72245722Smckusick (void) vnode_pager_uncache(vp); 72339608Smckusick n = MIN(n, size - bp->b_resid); 72439608Smckusick error = uiomove(bp->b_un.b_addr + on, n, uio); 72539608Smckusick if (ioflag & IO_SYNC) 72639608Smckusick (void) bwrite(bp); 72739608Smckusick else if (n + on == fs->fs_bsize) { 72839608Smckusick bp->b_flags |= B_AGE; 72939608Smckusick bawrite(bp); 73039608Smckusick } else 73139608Smckusick bdwrite(bp); 73239608Smckusick ip->i_flag |= IUPD|ICHG; 73339608Smckusick if (cred->cr_uid != 0) 73439608Smckusick ip->i_mode &= ~(ISUID|ISGID); 73539608Smckusick } while (error == 0 && uio->uio_resid > 0 && n != 0); 73639608Smckusick if (error && (ioflag & IO_UNIT)) { 73739674Smckusick (void) itrunc(ip, osize, ioflag & IO_SYNC); 73839608Smckusick uio->uio_offset -= resid - uio->uio_resid; 73939608Smckusick uio->uio_resid = resid; 74039608Smckusick } 74142493Smckusick if (!error && (ioflag & IO_SYNC)) 74242493Smckusick error = iupdat(ip, &time, &time, 1); 74339608Smckusick return (error); 74439608Smckusick } 74539608Smckusick 74639608Smckusick /* ARGSUSED */ 74737737Smckusick ufs_ioctl(vp, com, data, fflag, cred) 74837737Smckusick struct vnode *vp; 74937737Smckusick int com; 75037737Smckusick caddr_t data; 75137737Smckusick int fflag; 75237737Smckusick struct ucred *cred; 75311811Ssam { 75411811Ssam 75537737Smckusick return (ENOTTY); 75611811Ssam } 75711811Ssam 75837737Smckusick /* ARGSUSED */ 75940290Smckusick ufs_select(vp, which, fflags, cred) 76037737Smckusick struct vnode *vp; 76140290Smckusick int which, fflags; 76237737Smckusick struct ucred *cred; 76337737Smckusick { 76437737Smckusick 76537737Smckusick return (1); /* XXX */ 76637737Smckusick } 76737737Smckusick 7689167Ssam /* 76937737Smckusick * Mmap a file 77037737Smckusick * 77137737Smckusick * NB Currently unsupported. 7729167Ssam */ 77337737Smckusick /* ARGSUSED */ 77437737Smckusick ufs_mmap(vp, fflags, cred) 77537737Smckusick struct vnode *vp; 77637737Smckusick int fflags; 77737737Smckusick struct ucred *cred; 77837Sbill { 77937Sbill 78037737Smckusick return (EINVAL); 78137Sbill } 7827535Sroot 7839167Ssam /* 78437737Smckusick * Synch an open file. 7859167Ssam */ 78637737Smckusick /* ARGSUSED */ 78739597Smckusick ufs_fsync(vp, fflags, cred, waitfor) 78837737Smckusick struct vnode *vp; 78937737Smckusick int fflags; 79037737Smckusick struct ucred *cred; 79139597Smckusick int waitfor; 7927701Ssam { 79339597Smckusick struct inode *ip = VTOI(vp); 7947701Ssam 79537737Smckusick if (fflags&FWRITE) 79637737Smckusick ip->i_flag |= ICHG; 79739674Smckusick vflushbuf(vp, waitfor == MNT_WAIT ? B_SYNC : 0); 79839674Smckusick return (iupdat(ip, &time, &time, waitfor == MNT_WAIT)); 7997701Ssam } 8007701Ssam 8019167Ssam /* 80237737Smckusick * Seek on a file 80337737Smckusick * 80437737Smckusick * Nothing to do, so just return. 8059167Ssam */ 80637737Smckusick /* ARGSUSED */ 80737737Smckusick ufs_seek(vp, oldoff, newoff, cred) 80837737Smckusick struct vnode *vp; 80937737Smckusick off_t oldoff, newoff; 81037737Smckusick struct ucred *cred; 8117701Ssam { 8127701Ssam 81337737Smckusick return (0); 81437737Smckusick } 81537737Smckusick 81637737Smckusick /* 81737737Smckusick * ufs remove 81837737Smckusick * Hard to avoid races here, especially 81937737Smckusick * in unlinking directories. 82037737Smckusick */ 82137737Smckusick ufs_remove(ndp) 82237737Smckusick struct nameidata *ndp; 82337737Smckusick { 82437737Smckusick register struct inode *ip, *dp; 82537737Smckusick int error; 82637737Smckusick 82737737Smckusick ip = VTOI(ndp->ni_vp); 82837737Smckusick dp = VTOI(ndp->ni_dvp); 82937737Smckusick error = dirremove(ndp); 83037737Smckusick if (!error) { 83137737Smckusick ip->i_nlink--; 83237737Smckusick ip->i_flag |= ICHG; 8337701Ssam } 83437737Smckusick if (dp == ip) 83537737Smckusick vrele(ITOV(ip)); 83637737Smckusick else 83737737Smckusick iput(ip); 83837737Smckusick iput(dp); 83937737Smckusick return (error); 8407701Ssam } 8417701Ssam 8429167Ssam /* 84337737Smckusick * link vnode call 8449167Ssam */ 84537737Smckusick ufs_link(vp, ndp) 84637737Smckusick register struct vnode *vp; 84737737Smckusick register struct nameidata *ndp; 8489167Ssam { 84937737Smckusick register struct inode *ip = VTOI(vp); 85037737Smckusick int error; 8519167Ssam 852*46251Smckusick if ((unsigned short)ip->i_nlink >= LINK_MAX) 853*46251Smckusick return (EMLINK); 85437737Smckusick if (ndp->ni_dvp != vp) 85537737Smckusick ILOCK(ip); 85637737Smckusick ip->i_nlink++; 85737737Smckusick ip->i_flag |= ICHG; 85837737Smckusick error = iupdat(ip, &time, &time, 1); 85937737Smckusick if (!error) 86037737Smckusick error = direnter(ip, ndp); 86137737Smckusick if (ndp->ni_dvp != vp) 86237737Smckusick IUNLOCK(ip); 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; 96537737Smckusick tndp->ni_nameiop = RENAME | LOCKPARENT | LOCKLEAF | NOCACHE; 96612816Smckusick do { 96737737Smckusick dp = VTOI(tndp->ni_dvp); 96812816Smckusick if (xp != NULL) 96938069Smckusick iput(xp); 97037737Smckusick if (error = checkpath(ip, dp, tndp->ni_cred)) 97112816Smckusick goto out; 97237737Smckusick if (error = namei(tndp)) 97312816Smckusick goto out; 97437737Smckusick xp = NULL; 97537737Smckusick if (tndp->ni_vp) 97637737Smckusick xp = VTOI(tndp->ni_vp); 97737737Smckusick } while (dp != VTOI(tndp->ni_dvp)); 97812816Smckusick } 97911641Ssam /* 9809167Ssam * 2) If target doesn't exist, link the target 9819167Ssam * to the source and unlink the source. 9829167Ssam * Otherwise, rewrite the target directory 9839167Ssam * entry to reference the source inode and 9849167Ssam * expunge the original entry's existence. 9859167Ssam */ 9869167Ssam if (xp == NULL) { 98737737Smckusick if (dp->i_dev != ip->i_dev) 98837737Smckusick panic("rename: EXDEV"); 9899167Ssam /* 99016776Smckusick * Account for ".." in new directory. 99116776Smckusick * When source and destination have the same 99216776Smckusick * parent we don't fool with the link count. 9939167Ssam */ 99416776Smckusick if (doingdirectory && newparent) { 995*46251Smckusick if ((unsigned short)dp->i_nlink >= LINK_MAX) { 996*46251Smckusick error = EMLINK; 997*46251Smckusick goto bad; 998*46251Smckusick } 9999167Ssam dp->i_nlink++; 10009167Ssam dp->i_flag |= ICHG; 1001*46251Smckusick if (error = iupdat(dp, &time, &time, 1)) 1002*46251Smckusick goto bad; 10039167Ssam } 100437737Smckusick if (error = direnter(ip, tndp)) 10059167Ssam goto out; 10069167Ssam } else { 100737737Smckusick if (xp->i_dev != dp->i_dev || xp->i_dev != ip->i_dev) 100837737Smckusick panic("rename: EXDEV"); 10099167Ssam /* 101010590Ssam * Short circuit rename(foo, foo). 101110590Ssam */ 101210590Ssam if (xp->i_number == ip->i_number) 101337737Smckusick panic("rename: same file"); 101410590Ssam /* 101524433Sbloom * If the parent directory is "sticky", then the user must 101624433Sbloom * own the parent directory, or the destination of the rename, 101724433Sbloom * otherwise the destination may not be changed (except by 101824433Sbloom * root). This implements append-only directories. 101924433Sbloom */ 102037737Smckusick if ((dp->i_mode & ISVTX) && tndp->ni_cred->cr_uid != 0 && 102137737Smckusick tndp->ni_cred->cr_uid != dp->i_uid && 102237737Smckusick xp->i_uid != tndp->ni_cred->cr_uid) { 102324433Sbloom error = EPERM; 102424433Sbloom goto bad; 102524433Sbloom } 102624433Sbloom /* 102710051Ssam * Target must be empty if a directory 102810051Ssam * and have no links to it. 10299167Ssam * Also, insure source and target are 10309167Ssam * compatible (both directories, or both 10319167Ssam * not directories). 10329167Ssam */ 10339167Ssam if ((xp->i_mode&IFMT) == IFDIR) { 103437737Smckusick if (!dirempty(xp, dp->i_number, tndp->ni_cred) || 103537737Smckusick xp->i_nlink > 2) { 103610051Ssam error = ENOTEMPTY; 10379167Ssam goto bad; 10389167Ssam } 10399167Ssam if (!doingdirectory) { 104010051Ssam error = ENOTDIR; 10419167Ssam goto bad; 10429167Ssam } 104337737Smckusick cache_purge(ITOV(dp)); 10449167Ssam } else if (doingdirectory) { 104510051Ssam error = EISDIR; 10469167Ssam goto bad; 10479167Ssam } 104837737Smckusick if (error = dirrewrite(dp, ip, tndp)) 104937737Smckusick goto bad; 105045354Smckusick /* 105145354Smckusick * If the target directory is in the same 105245354Smckusick * directory as the source directory, 105345354Smckusick * decrement the link count on the parent 105445354Smckusick * of the target directory. 105545354Smckusick */ 105645354Smckusick if (doingdirectory && !newparent) { 105745354Smckusick dp->i_nlink--; 105845354Smckusick dp->i_flag |= ICHG; 105945354Smckusick } 106037737Smckusick vput(ITOV(dp)); 10619167Ssam /* 106210051Ssam * Adjust the link count of the target to 106310051Ssam * reflect the dirrewrite above. If this is 106410051Ssam * a directory it is empty and there are 106510051Ssam * no links to it, so we can squash the inode and 106610051Ssam * any space associated with it. We disallowed 106710051Ssam * renaming over top of a directory with links to 106816776Smckusick * it above, as the remaining link would point to 106916776Smckusick * a directory without "." or ".." entries. 10709167Ssam */ 107110051Ssam xp->i_nlink--; 10729167Ssam if (doingdirectory) { 107310051Ssam if (--xp->i_nlink != 0) 107410051Ssam panic("rename: linked directory"); 107539674Smckusick error = itrunc(xp, (u_long)0, IO_SYNC); 107610051Ssam } 10779167Ssam xp->i_flag |= ICHG; 107838398Smckusick iput(xp); 107910246Ssam xp = NULL; 10809167Ssam } 10819167Ssam 10829167Ssam /* 10839167Ssam * 3) Unlink the source. 10849167Ssam */ 108537737Smckusick fndp->ni_nameiop = DELETE | LOCKPARENT | LOCKLEAF; 108637737Smckusick (void)namei(fndp); 108737737Smckusick if (fndp->ni_vp != NULL) { 108837737Smckusick xp = VTOI(fndp->ni_vp); 108937737Smckusick dp = VTOI(fndp->ni_dvp); 109037737Smckusick } else { 109146250Smckusick /* 109246250Smckusick * From name has disappeared. 109346250Smckusick */ 109446250Smckusick if (doingdirectory) 109546250Smckusick panic("rename: lost dir entry"); 109646250Smckusick vrele(ITOV(ip)); 109746250Smckusick return (0); 109837737Smckusick } 10999167Ssam /* 110037737Smckusick * Ensure that the directory entry still exists and has not 110116776Smckusick * changed while the new name has been entered. If the source is 110216776Smckusick * a file then the entry may have been unlinked or renamed. In 110316776Smckusick * either case there is no further work to be done. If the source 110416776Smckusick * is a directory then it cannot have been rmdir'ed; its link 110516776Smckusick * count of three would cause a rmdir to fail with ENOTEMPTY. 110637737Smckusick * The IRENAME flag ensures that it cannot be moved by another 110716776Smckusick * rename. 11089167Ssam */ 110917758Smckusick if (xp != ip) { 111016776Smckusick if (doingdirectory) 111117758Smckusick panic("rename: lost dir entry"); 111216776Smckusick } else { 11139167Ssam /* 111416776Smckusick * If the source is a directory with a 111516776Smckusick * new parent, the link count of the old 111616776Smckusick * parent directory must be decremented 111716776Smckusick * and ".." set to point to the new parent. 11189167Ssam */ 111916776Smckusick if (doingdirectory && newparent) { 11209167Ssam dp->i_nlink--; 11219167Ssam dp->i_flag |= ICHG; 112239597Smckusick error = vn_rdwr(UIO_READ, ITOV(xp), (caddr_t)&dirbuf, 112337737Smckusick sizeof (struct dirtemplate), (off_t)0, 112439597Smckusick UIO_SYSSPACE, IO_NODELOCKED, 112539597Smckusick tndp->ni_cred, (int *)0); 112616776Smckusick if (error == 0) { 112716776Smckusick if (dirbuf.dotdot_namlen != 2 || 112816776Smckusick dirbuf.dotdot_name[0] != '.' || 112916776Smckusick dirbuf.dotdot_name[1] != '.') { 113039610Smckusick dirbad(xp, 12, "rename: mangled dir"); 113116776Smckusick } else { 113216776Smckusick dirbuf.dotdot_ino = newparent; 113339597Smckusick (void) vn_rdwr(UIO_WRITE, ITOV(xp), 113416776Smckusick (caddr_t)&dirbuf, 113516776Smckusick sizeof (struct dirtemplate), 113637740Smckusick (off_t)0, UIO_SYSSPACE, 113739597Smckusick IO_NODELOCKED|IO_SYNC, 113837737Smckusick tndp->ni_cred, (int *)0); 113937737Smckusick cache_purge(ITOV(dp)); 114016776Smckusick } 114116776Smckusick } 11429167Ssam } 114337737Smckusick error = dirremove(fndp); 114437737Smckusick if (!error) { 114516776Smckusick xp->i_nlink--; 114616776Smckusick xp->i_flag |= ICHG; 11479167Ssam } 114816776Smckusick xp->i_flag &= ~IRENAME; 11499167Ssam } 11509167Ssam if (dp) 115137737Smckusick vput(ITOV(dp)); 115216776Smckusick if (xp) 115337737Smckusick vput(ITOV(xp)); 115437737Smckusick vrele(ITOV(ip)); 115537737Smckusick return (error); 11569167Ssam 11579167Ssam bad: 11589167Ssam if (xp) 115937737Smckusick vput(ITOV(xp)); 116037737Smckusick vput(ITOV(dp)); 11619167Ssam out: 11629167Ssam ip->i_nlink--; 11639167Ssam ip->i_flag |= ICHG; 116437737Smckusick vrele(ITOV(ip)); 116537737Smckusick return (error); 11667701Ssam } 11677701Ssam 11687535Sroot /* 116912756Ssam * A virgin directory (no blushing please). 117012756Ssam */ 117112756Ssam struct dirtemplate mastertemplate = { 117212756Ssam 0, 12, 1, ".", 117312756Ssam 0, DIRBLKSIZ - 12, 2, ".." 117412756Ssam }; 117512756Ssam 117612756Ssam /* 117712756Ssam * Mkdir system call 117812756Ssam */ 117937737Smckusick ufs_mkdir(ndp, vap) 118037737Smckusick struct nameidata *ndp; 118137737Smckusick struct vattr *vap; 118212756Ssam { 118312756Ssam register struct inode *ip, *dp; 118437737Smckusick struct inode *tip; 118537737Smckusick struct vnode *dvp; 118612756Ssam struct dirtemplate dirtemplate; 118737737Smckusick int error; 118837737Smckusick int dmode; 118912756Ssam 119037737Smckusick dvp = ndp->ni_dvp; 119137737Smckusick dp = VTOI(dvp); 1192*46251Smckusick if ((unsigned short)dp->i_nlink >= LINK_MAX) { 1193*46251Smckusick iput(dp); 1194*46251Smckusick return (EMLINK); 1195*46251Smckusick } 119637737Smckusick dmode = vap->va_mode&0777; 119737737Smckusick dmode |= IFDIR; 119812756Ssam /* 119912756Ssam * Must simulate part of maknode here 120012756Ssam * in order to acquire the inode, but 120112756Ssam * not have it entered in the parent 120212756Ssam * directory. The entry is made later 120312756Ssam * after writing "." and ".." entries out. 120412756Ssam */ 120541312Smckusick if (error = ialloc(dp, dirpref(dp->i_fs), dmode, ndp->ni_cred, &tip)) { 120612756Ssam iput(dp); 120737737Smckusick return (error); 120812756Ssam } 120937737Smckusick ip = tip; 121041312Smckusick ip->i_uid = ndp->ni_cred->cr_uid; 121141312Smckusick ip->i_gid = dp->i_gid; 121212756Ssam #ifdef QUOTA 121341312Smckusick if ((error = getinoquota(ip)) || 121441312Smckusick (error = chkiq(ip, 1, ndp->ni_cred, 0))) { 121541312Smckusick ifree(ip, ip->i_number, dmode); 121641312Smckusick iput(ip); 121741312Smckusick iput(dp); 121841312Smckusick return (error); 121941312Smckusick } 122012756Ssam #endif 122112756Ssam ip->i_flag |= IACC|IUPD|ICHG; 122237737Smckusick ip->i_mode = dmode; 122337737Smckusick ITOV(ip)->v_type = VDIR; /* Rest init'd in iget() */ 122412756Ssam ip->i_nlink = 2; 122537737Smckusick error = iupdat(ip, &time, &time, 1); 122612756Ssam 122712756Ssam /* 122812756Ssam * Bump link count in parent directory 122912756Ssam * to reflect work done below. Should 123012756Ssam * be done before reference is created 123112756Ssam * so reparation is possible if we crash. 123212756Ssam */ 123312756Ssam dp->i_nlink++; 123412756Ssam dp->i_flag |= ICHG; 123537737Smckusick error = iupdat(dp, &time, &time, 1); 123612756Ssam 123712756Ssam /* 123812756Ssam * Initialize directory with "." 123912756Ssam * and ".." from static template. 124012756Ssam */ 124112756Ssam dirtemplate = mastertemplate; 124212756Ssam dirtemplate.dot_ino = ip->i_number; 124312756Ssam dirtemplate.dotdot_ino = dp->i_number; 124439597Smckusick error = vn_rdwr(UIO_WRITE, ITOV(ip), (caddr_t)&dirtemplate, 124537737Smckusick sizeof (dirtemplate), (off_t)0, UIO_SYSSPACE, 124639597Smckusick IO_NODELOCKED|IO_SYNC, ndp->ni_cred, (int *)0); 124737737Smckusick if (error) { 124812756Ssam dp->i_nlink--; 124912756Ssam dp->i_flag |= ICHG; 125012756Ssam goto bad; 125112756Ssam } 125243288Smckusick if (DIRBLKSIZ > dp->i_fs->fs_fsize) { 125337737Smckusick panic("mkdir: blksize"); /* XXX - should grow w/balloc() */ 125443288Smckusick } else { 125518103Smckusick ip->i_size = DIRBLKSIZ; 125643288Smckusick ip->i_flag |= ICHG; 125743288Smckusick } 125812756Ssam /* 125912756Ssam * Directory all set up, now 126012756Ssam * install the entry for it in 126112756Ssam * the parent directory. 126212756Ssam */ 126337737Smckusick error = direnter(ip, ndp); 126412756Ssam dp = NULL; 126537737Smckusick if (error) { 126616694Smckusick ndp->ni_nameiop = LOOKUP | NOCACHE; 126737737Smckusick error = namei(ndp); 126837737Smckusick if (!error) { 126937737Smckusick dp = VTOI(ndp->ni_vp); 127012756Ssam dp->i_nlink--; 127112756Ssam dp->i_flag |= ICHG; 127212756Ssam } 127312756Ssam } 127412756Ssam bad: 127512756Ssam /* 127612756Ssam * No need to do an explicit itrunc here, 127737737Smckusick * vrele will do this for us because we set 127812756Ssam * the link count to 0. 127912756Ssam */ 128037737Smckusick if (error) { 128112756Ssam ip->i_nlink = 0; 128212756Ssam ip->i_flag |= ICHG; 128338144Smckusick iput(ip); 128438144Smckusick } else 128538144Smckusick ndp->ni_vp = ITOV(ip); 128612756Ssam if (dp) 128712756Ssam iput(dp); 128837737Smckusick return (error); 128912756Ssam } 129012756Ssam 129112756Ssam /* 129212756Ssam * Rmdir system call. 129312756Ssam */ 129437737Smckusick ufs_rmdir(ndp) 129537737Smckusick register struct nameidata *ndp; 129612756Ssam { 129712756Ssam register struct inode *ip, *dp; 129837737Smckusick int error = 0; 129912756Ssam 130037737Smckusick ip = VTOI(ndp->ni_vp); 130137737Smckusick dp = VTOI(ndp->ni_dvp); 130212756Ssam /* 130312756Ssam * No rmdir "." please. 130412756Ssam */ 130512756Ssam if (dp == ip) { 130637737Smckusick vrele(ITOV(dp)); 130712756Ssam iput(ip); 130837737Smckusick return (EINVAL); 130912756Ssam } 131012756Ssam /* 131112756Ssam * Verify the directory is empty (and valid). 131212756Ssam * (Rmdir ".." won't be valid since 131312756Ssam * ".." will contain a reference to 131412756Ssam * the current directory and thus be 131512756Ssam * non-empty.) 131612756Ssam */ 131737737Smckusick if (ip->i_nlink != 2 || !dirempty(ip, dp->i_number, ndp->ni_cred)) { 131837737Smckusick error = ENOTEMPTY; 131912756Ssam goto out; 132012756Ssam } 132112756Ssam /* 132212756Ssam * Delete reference to directory before purging 132312756Ssam * inode. If we crash in between, the directory 132412756Ssam * will be reattached to lost+found, 132512756Ssam */ 132637737Smckusick if (error = dirremove(ndp)) 132712756Ssam goto out; 132812756Ssam dp->i_nlink--; 132912756Ssam dp->i_flag |= ICHG; 133037737Smckusick cache_purge(ITOV(dp)); 133112756Ssam iput(dp); 133237737Smckusick ndp->ni_dvp = NULL; 133312756Ssam /* 133412756Ssam * Truncate inode. The only stuff left 133512756Ssam * in the directory is "." and "..". The 133612756Ssam * "." reference is inconsequential since 133712756Ssam * we're quashing it. The ".." reference 133812756Ssam * has already been adjusted above. We've 133912756Ssam * removed the "." reference and the reference 134012756Ssam * in the parent directory, but there may be 134112756Ssam * other hard links so decrement by 2 and 134212756Ssam * worry about them later. 134312756Ssam */ 134412756Ssam ip->i_nlink -= 2; 134539674Smckusick error = itrunc(ip, (u_long)0, IO_SYNC); 134637737Smckusick cache_purge(ITOV(ip)); 134712756Ssam out: 134837737Smckusick if (ndp->ni_dvp) 134912756Ssam iput(dp); 135012756Ssam iput(ip); 135137737Smckusick return (error); 135212756Ssam } 135312756Ssam 135437737Smckusick /* 135537737Smckusick * symlink -- make a symbolic link 135637737Smckusick */ 135737737Smckusick ufs_symlink(ndp, vap, target) 135837737Smckusick struct nameidata *ndp; 135937737Smckusick struct vattr *vap; 136037737Smckusick char *target; 136112756Ssam { 136237737Smckusick struct inode *ip; 136337737Smckusick int error; 136412756Ssam 136537737Smckusick error = maknode(IFLNK | vap->va_mode, ndp, &ip); 136637737Smckusick if (error) 136737737Smckusick return (error); 136839597Smckusick error = vn_rdwr(UIO_WRITE, ITOV(ip), target, strlen(target), (off_t)0, 136939597Smckusick UIO_SYSSPACE, IO_NODELOCKED, ndp->ni_cred, (int *)0); 137037737Smckusick iput(ip); 137137737Smckusick return (error); 137237737Smckusick } 137337737Smckusick 137437737Smckusick /* 137537737Smckusick * Vnode op for read and write 137637737Smckusick */ 137740345Smckusick ufs_readdir(vp, uio, cred, eofflagp) 137837737Smckusick struct vnode *vp; 137937737Smckusick register struct uio *uio; 138037737Smckusick struct ucred *cred; 138140345Smckusick int *eofflagp; 138237737Smckusick { 138339597Smckusick int count, lost, error; 138437737Smckusick 138537737Smckusick count = uio->uio_resid; 138637737Smckusick count &= ~(DIRBLKSIZ - 1); 138739597Smckusick lost = uio->uio_resid - count; 138839597Smckusick if (count < DIRBLKSIZ || (uio->uio_offset & (DIRBLKSIZ -1))) 138937737Smckusick return (EINVAL); 139037737Smckusick uio->uio_resid = count; 139137737Smckusick uio->uio_iov->iov_len = count; 139239597Smckusick error = ufs_read(vp, uio, 0, cred); 139339597Smckusick uio->uio_resid += lost; 139440345Smckusick if ((VTOI(vp)->i_size - uio->uio_offset) <= 0) 139540345Smckusick *eofflagp = 1; 139640345Smckusick else 139740345Smckusick *eofflagp = 0; 139837737Smckusick return (error); 139937737Smckusick } 140037737Smckusick 140137737Smckusick /* 140237737Smckusick * Return target name of a symbolic link 140337737Smckusick */ 140437737Smckusick ufs_readlink(vp, uiop, cred) 140537737Smckusick struct vnode *vp; 140637737Smckusick struct uio *uiop; 140737737Smckusick struct ucred *cred; 140837737Smckusick { 140937737Smckusick 141039597Smckusick return (ufs_read(vp, uiop, 0, cred)); 141137737Smckusick } 141237737Smckusick 141337737Smckusick /* 141437737Smckusick * Ufs abort op, called after namei() when a CREATE/DELETE isn't actually 141542466Smckusick * done. Nothing to do at the moment. 141637737Smckusick */ 141742466Smckusick /* ARGSUSED */ 141837737Smckusick ufs_abortop(ndp) 141942466Smckusick struct nameidata *ndp; 142037737Smckusick { 142137737Smckusick 142242466Smckusick return (0); 142312756Ssam } 142412756Ssam 142539909Smckusick /* 142639909Smckusick * Lock an inode. 142739909Smckusick */ 142837737Smckusick ufs_lock(vp) 142937737Smckusick struct vnode *vp; 143037737Smckusick { 143137737Smckusick register struct inode *ip = VTOI(vp); 143237737Smckusick 143337737Smckusick ILOCK(ip); 143437737Smckusick return (0); 143537737Smckusick } 143637737Smckusick 143739909Smckusick /* 143839909Smckusick * Unlock an inode. 143939909Smckusick */ 144037737Smckusick ufs_unlock(vp) 144137737Smckusick struct vnode *vp; 144237737Smckusick { 144337737Smckusick register struct inode *ip = VTOI(vp); 144437737Smckusick 144537737Smckusick if (!(ip->i_flag & ILOCKED)) 144637737Smckusick panic("ufs_unlock NOT LOCKED"); 144737737Smckusick IUNLOCK(ip); 144837737Smckusick return (0); 144937737Smckusick } 145037737Smckusick 145112756Ssam /* 145239909Smckusick * Check for a locked inode. 145339909Smckusick */ 145439909Smckusick ufs_islocked(vp) 145539909Smckusick struct vnode *vp; 145639909Smckusick { 145739909Smckusick 145839909Smckusick if (VTOI(vp)->i_flag & ILOCKED) 145939909Smckusick return (1); 146039909Smckusick return (0); 146139909Smckusick } 146239909Smckusick 146339909Smckusick /* 146437737Smckusick * Get access to bmap 146512756Ssam */ 146637737Smckusick ufs_bmap(vp, bn, vpp, bnp) 146737737Smckusick struct vnode *vp; 146837737Smckusick daddr_t bn; 146937737Smckusick struct vnode **vpp; 147037737Smckusick daddr_t *bnp; 147112756Ssam { 147237737Smckusick struct inode *ip = VTOI(vp); 147312756Ssam 147437737Smckusick if (vpp != NULL) 147537737Smckusick *vpp = ip->i_devvp; 147637737Smckusick if (bnp == NULL) 147737737Smckusick return (0); 147841538Smckusick return (bmap(ip, bn, bnp)); 147912756Ssam } 148037737Smckusick 148137737Smckusick /* 148241538Smckusick * Calculate the logical to physical mapping if not done already, 148341538Smckusick * then call the device strategy routine. 148437737Smckusick */ 148541538Smckusick int checkoverlap = 0; 148639674Smckusick 148737737Smckusick ufs_strategy(bp) 148837737Smckusick register struct buf *bp; 148937737Smckusick { 149039674Smckusick register struct inode *ip = VTOI(bp->b_vp); 149139674Smckusick struct vnode *vp; 149239674Smckusick int error; 149339674Smckusick 149439674Smckusick if (bp->b_vp->v_type == VBLK || bp->b_vp->v_type == VCHR) 149539674Smckusick panic("ufs_strategy: spec"); 149639674Smckusick if (bp->b_blkno == bp->b_lblkno) { 149739674Smckusick if (error = bmap(ip, bp->b_lblkno, &bp->b_blkno)) 149839674Smckusick return (error); 149939896Smckusick if ((long)bp->b_blkno == -1) 150039674Smckusick clrbuf(bp); 150139674Smckusick } 150239896Smckusick if ((long)bp->b_blkno == -1) { 150339896Smckusick biodone(bp); 150439674Smckusick return (0); 150539896Smckusick } 150641538Smckusick #ifdef DIAGNOSTIC 150739674Smckusick if (checkoverlap) { 150841538Smckusick register struct buf *ep; 150941538Smckusick struct buf *ebp; 151041538Smckusick daddr_t start, last; 151141538Smckusick 151239674Smckusick ebp = &buf[nbuf]; 151339674Smckusick start = bp->b_blkno; 151439674Smckusick last = start + btodb(bp->b_bcount) - 1; 151539674Smckusick for (ep = buf; ep < ebp; ep++) { 151639674Smckusick if (ep == bp || (ep->b_flags & B_INVAL) || 151741396Smckusick ep->b_vp == NULLVP) 151839674Smckusick continue; 151939674Smckusick if (VOP_BMAP(ep->b_vp, (daddr_t)0, &vp, (daddr_t)0)) 152039674Smckusick continue; 152139674Smckusick if (vp != ip->i_devvp) 152239674Smckusick continue; 152339674Smckusick /* look for overlap */ 152439674Smckusick if (ep->b_bcount == 0 || ep->b_blkno > last || 152539674Smckusick ep->b_blkno + btodb(ep->b_bcount) <= start) 152639674Smckusick continue; 152739896Smckusick vprint("Disk overlap", vp); 152839896Smckusick printf("\tstart %d, end %d overlap start %d, end %d\n", 152939896Smckusick start, last, ep->b_blkno, 153039896Smckusick ep->b_blkno + btodb(ep->b_bcount) - 1); 153141538Smckusick panic("Disk buffer overlap"); 153239674Smckusick } 153339674Smckusick } 153441538Smckusick #endif /* DIAGNOSTIC */ 153539674Smckusick vp = ip->i_devvp; 153639674Smckusick bp->b_dev = vp->v_rdev; 153739674Smckusick (*(vp->v_op->vn_strategy))(bp); 153837737Smckusick return (0); 153937737Smckusick } 154037737Smckusick 154137737Smckusick /* 154239674Smckusick * Print out the contents of an inode. 154339674Smckusick */ 154439674Smckusick ufs_print(vp) 154539674Smckusick struct vnode *vp; 154639674Smckusick { 154739674Smckusick register struct inode *ip = VTOI(vp); 154839674Smckusick 154940293Smckusick printf("tag VT_UFS, ino %d, on dev %d, %d", ip->i_number, 155040293Smckusick major(ip->i_dev), minor(ip->i_dev)); 155140293Smckusick #ifdef FIFO 155240293Smckusick if (vp->v_type == VFIFO) 155340293Smckusick fifo_printinfo(vp); 155440293Smckusick #endif /* FIFO */ 155540293Smckusick printf("%s\n", (ip->i_flag & ILOCKED) ? " (LOCKED)" : ""); 155639900Smckusick if (ip->i_spare0 == 0) 155739900Smckusick return; 155839900Smckusick printf("\towner pid %d", ip->i_spare0); 155939900Smckusick if (ip->i_spare1) 156039900Smckusick printf(" waiting pid %d", ip->i_spare1); 156139900Smckusick printf("\n"); 156239674Smckusick } 156339674Smckusick 156439674Smckusick /* 156539628Smckusick * Read wrapper for special devices. 156639628Smckusick */ 156739628Smckusick ufsspec_read(vp, uio, ioflag, cred) 156839628Smckusick struct vnode *vp; 156939628Smckusick struct uio *uio; 157039628Smckusick int ioflag; 157139628Smckusick struct ucred *cred; 157239628Smckusick { 157339628Smckusick 157439628Smckusick /* 157539628Smckusick * Set access flag. 157639628Smckusick */ 157739628Smckusick VTOI(vp)->i_flag |= IACC; 157839628Smckusick return (spec_read(vp, uio, ioflag, cred)); 157939628Smckusick } 158039628Smckusick 158139628Smckusick /* 158239628Smckusick * Write wrapper for special devices. 158339628Smckusick */ 158439628Smckusick ufsspec_write(vp, uio, ioflag, cred) 158539628Smckusick struct vnode *vp; 158639628Smckusick struct uio *uio; 158739628Smckusick int ioflag; 158839628Smckusick struct ucred *cred; 158939628Smckusick { 159039628Smckusick 159139628Smckusick /* 159239628Smckusick * Set update and change flags. 159339628Smckusick */ 159439628Smckusick VTOI(vp)->i_flag |= IUPD|ICHG; 159539628Smckusick return (spec_write(vp, uio, ioflag, cred)); 159639628Smckusick } 159739628Smckusick 159839628Smckusick /* 159939628Smckusick * Close wrapper for special devices. 160039628Smckusick * 160139628Smckusick * Update the times on the inode then do device close. 160239628Smckusick */ 160339628Smckusick ufsspec_close(vp, fflag, cred) 160439628Smckusick struct vnode *vp; 160539628Smckusick int fflag; 160639628Smckusick struct ucred *cred; 160739628Smckusick { 160839628Smckusick register struct inode *ip = VTOI(vp); 160939628Smckusick 161039815Smckusick if (vp->v_usecount > 1 && !(ip->i_flag & ILOCKED)) 161139628Smckusick ITIMES(ip, &time, &time); 161239628Smckusick return (spec_close(vp, fflag, cred)); 161339628Smckusick } 161439628Smckusick 161540290Smckusick #ifdef FIFO 161639628Smckusick /* 161740290Smckusick * Read wrapper for fifo's 161840290Smckusick */ 161940290Smckusick ufsfifo_read(vp, uio, ioflag, cred) 162040290Smckusick struct vnode *vp; 162140290Smckusick struct uio *uio; 162240290Smckusick int ioflag; 162340290Smckusick struct ucred *cred; 162440290Smckusick { 162540290Smckusick 162640290Smckusick /* 162740290Smckusick * Set access flag. 162840290Smckusick */ 162940290Smckusick VTOI(vp)->i_flag |= IACC; 163040290Smckusick return (fifo_read(vp, uio, ioflag, cred)); 163140290Smckusick } 163240290Smckusick 163340290Smckusick /* 163440290Smckusick * Write wrapper for fifo's. 163540290Smckusick */ 163640290Smckusick ufsfifo_write(vp, uio, ioflag, cred) 163740290Smckusick struct vnode *vp; 163840290Smckusick struct uio *uio; 163940290Smckusick int ioflag; 164040290Smckusick struct ucred *cred; 164140290Smckusick { 164240290Smckusick 164340290Smckusick /* 164440290Smckusick * Set update and change flags. 164540290Smckusick */ 164640290Smckusick VTOI(vp)->i_flag |= IUPD|ICHG; 164740290Smckusick return (fifo_write(vp, uio, ioflag, cred)); 164840290Smckusick } 164940290Smckusick 165040290Smckusick /* 165140290Smckusick * Close wrapper for fifo's. 165240290Smckusick * 165340290Smckusick * Update the times on the inode then do device close. 165440290Smckusick */ 165540290Smckusick ufsfifo_close(vp, fflag, cred) 165640290Smckusick struct vnode *vp; 165740290Smckusick int fflag; 165840290Smckusick struct ucred *cred; 165940290Smckusick { 166040290Smckusick register struct inode *ip = VTOI(vp); 166140290Smckusick 166240290Smckusick if (vp->v_usecount > 1 && !(ip->i_flag & ILOCKED)) 166340290Smckusick ITIMES(ip, &time, &time); 166440290Smckusick return (fifo_close(vp, fflag, cred)); 166540290Smckusick } 166640290Smckusick #endif /* FIFO */ 166740290Smckusick 166840290Smckusick /* 166937737Smckusick * Make a new file. 167037737Smckusick */ 167137737Smckusick maknode(mode, ndp, ipp) 167237737Smckusick int mode; 167337737Smckusick register struct nameidata *ndp; 167437737Smckusick struct inode **ipp; 167537737Smckusick { 167637737Smckusick register struct inode *ip; 167737737Smckusick struct inode *tip; 167837737Smckusick register struct inode *pdir = VTOI(ndp->ni_dvp); 167937737Smckusick ino_t ipref; 168037737Smckusick int error; 168137737Smckusick 168237737Smckusick *ipp = 0; 168341312Smckusick if ((mode & IFMT) == 0) 168441312Smckusick mode |= IFREG; 168537737Smckusick if ((mode & IFMT) == IFDIR) 168637737Smckusick ipref = dirpref(pdir->i_fs); 168737737Smckusick else 168837737Smckusick ipref = pdir->i_number; 168941312Smckusick if (error = ialloc(pdir, ipref, mode, ndp->ni_cred, &tip)) { 169037737Smckusick iput(pdir); 169137737Smckusick return (error); 169237737Smckusick } 169337737Smckusick ip = tip; 169441312Smckusick ip->i_uid = ndp->ni_cred->cr_uid; 169541312Smckusick ip->i_gid = pdir->i_gid; 169637737Smckusick #ifdef QUOTA 169741312Smckusick if ((error = getinoquota(ip)) || 169841312Smckusick (error = chkiq(ip, 1, ndp->ni_cred, 0))) { 169941312Smckusick ifree(ip, ip->i_number, mode); 170041312Smckusick iput(ip); 170141312Smckusick iput(pdir); 170241312Smckusick return (error); 170341312Smckusick } 170437737Smckusick #endif 170537737Smckusick ip->i_flag |= IACC|IUPD|ICHG; 170637737Smckusick ip->i_mode = mode; 170737737Smckusick ITOV(ip)->v_type = IFTOVT(mode); /* Rest init'd in iget() */ 170837737Smckusick ip->i_nlink = 1; 170937737Smckusick if ((ip->i_mode & ISGID) && !groupmember(ip->i_gid, ndp->ni_cred) && 171037737Smckusick suser(ndp->ni_cred, NULL)) 171137737Smckusick ip->i_mode &= ~ISGID; 171237737Smckusick 171337737Smckusick /* 171437737Smckusick * Make sure inode goes to disk before directory entry. 171537737Smckusick */ 171641312Smckusick if (error = iupdat(ip, &time, &time, 1)) 171741312Smckusick goto bad; 171841312Smckusick if (error = direnter(ip, ndp)) { 171941312Smckusick pdir = NULL; 172041312Smckusick goto bad; 172137737Smckusick } 172237737Smckusick *ipp = ip; 172337737Smckusick return (0); 172441312Smckusick 172541312Smckusick bad: 172641312Smckusick /* 172741312Smckusick * Write error occurred trying to update the inode 172841312Smckusick * or the directory so must deallocate the inode. 172941312Smckusick */ 173041312Smckusick if (pdir) 173141312Smckusick iput(pdir); 173241312Smckusick ip->i_nlink = 0; 173341312Smckusick ip->i_flag |= ICHG; 173441312Smckusick iput(ip); 173541312Smckusick return (error); 173637737Smckusick } 173746207Smckusick 173846207Smckusick /* 173946207Smckusick * Advisory record locking support 174046207Smckusick */ 174146207Smckusick ufs_advlock(vp, id, op, fl, flags) 174246207Smckusick struct vnode *vp; 174346207Smckusick caddr_t id; 174446207Smckusick int op; 174546207Smckusick register struct flock *fl; 174646207Smckusick int flags; 174746207Smckusick { 174846207Smckusick register struct inode *ip = VTOI(vp); 174946207Smckusick register struct lockf *lock; 175046207Smckusick off_t start, end; 175146207Smckusick int error; 175246207Smckusick 175346207Smckusick /* 175446207Smckusick * Avoid the common case of unlocking when inode has no locks. 175546207Smckusick */ 175646207Smckusick if (ip->i_lockf == (struct lockf *)0) { 175746207Smckusick if (op != F_SETLK) { 175846207Smckusick fl->l_type = F_UNLCK; 175946207Smckusick return (0); 176046207Smckusick } 176146207Smckusick } 176246207Smckusick /* 176346207Smckusick * Convert the flock structure into a start and end. 176446207Smckusick */ 176546207Smckusick switch (fl->l_whence) { 176646207Smckusick 176746207Smckusick case SEEK_SET: 176846207Smckusick case SEEK_CUR: 176946207Smckusick /* 177046207Smckusick * Caller is responsible for adding any necessary offset 177146207Smckusick * when SEEK_CUR is used. 177246207Smckusick */ 177346207Smckusick start = fl->l_start; 177446207Smckusick break; 177546207Smckusick 177646207Smckusick case SEEK_END: 177746207Smckusick start = ip->i_size + fl->l_start; 177846207Smckusick break; 177946207Smckusick 178046207Smckusick default: 178146207Smckusick return (EINVAL); 178246207Smckusick } 178346207Smckusick if (start < 0) 178446207Smckusick return (EINVAL); 178546207Smckusick if (fl->l_len == 0) 178646207Smckusick end = -1; 178746207Smckusick else 178846207Smckusick end = start + fl->l_len; 178946207Smckusick /* 179046207Smckusick * Create the lockf structure 179146207Smckusick */ 179246207Smckusick MALLOC(lock, struct lockf *, sizeof *lock, M_LOCKF, M_WAITOK); 179346207Smckusick lock->lf_start = start; 179446207Smckusick lock->lf_end = end; 179546207Smckusick lock->lf_id = id; 179646207Smckusick lock->lf_inode = ip; 179746207Smckusick lock->lf_type = fl->l_type; 179846207Smckusick lock->lf_next = (struct lockf *)0; 179946207Smckusick lock->lf_block = (struct lockf *)0; 180046207Smckusick lock->lf_flags = flags; 180146207Smckusick /* 180246207Smckusick * Do the requested operation. 180346207Smckusick */ 180446207Smckusick switch(op) { 180546207Smckusick case F_SETLK: 180646207Smckusick return (ufs_setlock(lock)); 180746207Smckusick 180846207Smckusick case F_UNLCK: 180946207Smckusick return (ufs_advunlock(lock)); 181046207Smckusick 181146207Smckusick case F_GETLK: 181246207Smckusick return (ufs_advgetlock(lock, fl)); 181346207Smckusick 181446207Smckusick default: 181546207Smckusick free(lock, M_LOCKF); 181646207Smckusick return (EINVAL); 181746207Smckusick } 181846207Smckusick /* NOTREACHED */ 181946207Smckusick } 182046207Smckusick 182146207Smckusick /* 182246207Smckusick * This variable controls the maximum number of processes that will 182346207Smckusick * be checked in doing deadlock detection. 182446207Smckusick */ 182546207Smckusick int maxlockdepth = MAXDEPTH; 182646207Smckusick 182746207Smckusick /* 182846207Smckusick * Set a byte-range lock. 182946207Smckusick */ 183046207Smckusick ufs_setlock(lock) 183146207Smckusick register struct lockf *lock; 183246207Smckusick { 183346207Smckusick register struct inode *ip = lock->lf_inode; 183446207Smckusick register struct lockf *block; 183546207Smckusick static char lockstr[] = "lockf"; 183646207Smckusick int priority, error; 183746207Smckusick 183846207Smckusick #ifdef LOCKF_DEBUG 183946207Smckusick if (lockf_debug & 4) 184046207Smckusick lf_print("ufs_setlock", lock); 184146207Smckusick #endif /* LOCKF_DEBUG */ 184246207Smckusick 184346207Smckusick /* 184446207Smckusick * Set the priority 184546207Smckusick */ 184646207Smckusick priority = PLOCK; 184746207Smckusick if ((lock->lf_type & F_WRLCK) == 0) 184846207Smckusick priority += 4; 184946207Smckusick priority |= PCATCH; 185046207Smckusick /* 185146207Smckusick * Scan lock list for this file looking for locks that would block us. 185246207Smckusick */ 185346207Smckusick while (block = lf_getblock(lock)) { 185446207Smckusick /* 185546207Smckusick * Free the structure and return if nonblocking. 185646207Smckusick */ 185746207Smckusick if ((lock->lf_flags & F_WAIT) == 0) { 185846207Smckusick free(lock, M_LOCKF); 185946207Smckusick return (EAGAIN); 186046207Smckusick } 186146207Smckusick /* 186246207Smckusick * We are blocked. Since flock style locks cover 186346207Smckusick * the whole file, there is no chance for deadlock. 186446207Smckusick * For byte-range locks we must check for deadlock. 186546207Smckusick * 186646207Smckusick * Deadlock detection is done by looking through the 186746207Smckusick * wait channels to see if there are any cycles that 186846207Smckusick * involve us. MAXDEPTH is set just to make sure we 186946207Smckusick * do not go off into neverland. 187046207Smckusick */ 187146207Smckusick if ((lock->lf_flags & F_POSIX) && 187246207Smckusick (block->lf_flags & F_POSIX)) { 187346207Smckusick register struct proc *wproc; 187446207Smckusick register struct lockf *waitblock; 187546207Smckusick int i = 0; 187646207Smckusick 187746207Smckusick /* The block is waiting on something */ 187846207Smckusick wproc = (struct proc *)block->lf_id; 187946207Smckusick while (wproc->p_wchan && 188046207Smckusick (wproc->p_wmesg == lockstr) && 188146207Smckusick (i++ < maxlockdepth)) { 188246207Smckusick waitblock = (struct lockf *)wproc->p_wchan; 188346207Smckusick /* Get the owner of the blocking lock */ 188446207Smckusick waitblock = waitblock->lf_next; 188546207Smckusick if ((waitblock->lf_flags & F_POSIX) == 0) 188646207Smckusick break; 188746207Smckusick wproc = (struct proc *)waitblock->lf_id; 188846207Smckusick if (wproc == (struct proc *)lock->lf_id) { 188946207Smckusick free(lock, M_LOCKF); 189046207Smckusick return (EDEADLK); 189146207Smckusick } 189246207Smckusick } 189346207Smckusick } 189446207Smckusick /* 189546207Smckusick * Add our lock to the blocked 189646207Smckusick * list and sleep until we're free. 189746207Smckusick */ 189846207Smckusick #ifdef LOCKF_DEBUG 189946207Smckusick if (lockf_debug & 4) 190046207Smckusick lf_print("ufs_advlock: blocking on", block); 190146207Smckusick #endif /* LOCKF_DEBUG */ 190246207Smckusick /* 190346207Smckusick * Remember who blocked us (for deadlock detection) 190446207Smckusick */ 190546207Smckusick lock->lf_next = block; 190646207Smckusick lf_addblock(block, lock); 190746207Smckusick if (error = tsleep((caddr_t *)lock, priority, lockstr, 0)) { 190846207Smckusick free(lock, M_LOCKF); 190946207Smckusick return (error); 191046207Smckusick } 191146207Smckusick } 191246207Smckusick /* 191346207Smckusick * No blocks!! Add the lock. Note that addlock will 191446207Smckusick * downgrade or upgrade any overlapping locks this 191546207Smckusick * process already owns. 191646207Smckusick */ 191746207Smckusick #ifdef LOCKF_DEBUG 191846207Smckusick if (lockf_debug & 4) 191946207Smckusick lf_print("ufs_advlock: got the lock", lock); 192046207Smckusick #endif /* LOCKF_DEBUG */ 192146207Smckusick lf_addlock(lock); 192246207Smckusick return (0); 192346207Smckusick } 192446207Smckusick 192546207Smckusick /* 192646207Smckusick * Remove a byte-range lock on an inode. 192746207Smckusick */ 192846207Smckusick ufs_advunlock(lock) 192946207Smckusick struct lockf *lock; 193046207Smckusick { 193146207Smckusick struct lockf *blocklist; 193246207Smckusick 193346207Smckusick if (lock->lf_inode->i_lockf == (struct lockf *)0) 193446207Smckusick return (0); 193546207Smckusick #ifdef LOCKF_DEBUG 193646207Smckusick if (lockf_debug & 4) 193746207Smckusick lf_print("ufs_advunlock", lock); 193846207Smckusick #endif /* LOCKF_DEBUG */ 193946207Smckusick /* 194046207Smckusick * Generally, find the lock (or an overlap to that lock) 194146207Smckusick * and remove it (or shrink it), then wakeup anyone we can. 194246207Smckusick */ 194346207Smckusick blocklist = lf_remove(lock); 194446207Smckusick FREE(lock, M_LOCKF); 194546207Smckusick lf_wakelock(blocklist); 194646207Smckusick return (0); 194746207Smckusick } 194846207Smckusick 194946207Smckusick /* 195046207Smckusick * Return the blocking pid 195146207Smckusick */ 195246207Smckusick ufs_advgetlock(lock, fl) 195346207Smckusick register struct lockf *lock; 195446207Smckusick register struct flock *fl; 195546207Smckusick { 195646207Smckusick register struct lockf *block; 195746207Smckusick off_t start, end; 195846207Smckusick 195946207Smckusick #ifdef LOCKF_DEBUG 196046207Smckusick if (lockf_debug & 4) 196146207Smckusick lf_print("ufs_advgetlock", lock); 196246207Smckusick #endif /* LOCKF_DEBUG */ 196346207Smckusick 196446207Smckusick if (block = lf_getblock(lock)) { 196546207Smckusick fl->l_type = block->lf_type; 196646207Smckusick fl->l_whence = SEEK_SET; 196746207Smckusick fl->l_start = block->lf_start; 196846207Smckusick if (block->lf_end == -1) 196946207Smckusick fl->l_len = 0; 197046207Smckusick else 197146207Smckusick fl->l_len = block->lf_end - block->lf_start; 197246207Smckusick if (block->lf_flags & F_POSIX) 197346207Smckusick fl->l_pid = ((struct proc *)(block->lf_id))->p_pid; 197446207Smckusick else 197546207Smckusick fl->l_pid = -1; 197646207Smckusick } 197746207Smckusick FREE(lock, M_LOCKF); 197846207Smckusick return (0); 197946207Smckusick } 1980