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*46511Smckusick * @(#)lfs_vnops.c 7.54 (Berkeley) 02/21/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 85246251Smckusick if ((unsigned short)ip->i_nlink >= LINK_MAX) 85346251Smckusick 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; 965*46511Smckusick tndp->ni_nameiop &= ~(MODMASK | OPMASK); 966*46511Smckusick 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 } 100537737Smckusick if (error = direnter(ip, tndp)) 10069167Ssam goto out; 10079167Ssam } else { 100837737Smckusick if (xp->i_dev != dp->i_dev || xp->i_dev != ip->i_dev) 100937737Smckusick panic("rename: EXDEV"); 10109167Ssam /* 101110590Ssam * Short circuit rename(foo, foo). 101210590Ssam */ 101310590Ssam if (xp->i_number == ip->i_number) 101437737Smckusick panic("rename: same file"); 101510590Ssam /* 101624433Sbloom * If the parent directory is "sticky", then the user must 101724433Sbloom * own the parent directory, or the destination of the rename, 101824433Sbloom * otherwise the destination may not be changed (except by 101924433Sbloom * root). This implements append-only directories. 102024433Sbloom */ 102137737Smckusick if ((dp->i_mode & ISVTX) && tndp->ni_cred->cr_uid != 0 && 102237737Smckusick tndp->ni_cred->cr_uid != dp->i_uid && 102337737Smckusick xp->i_uid != tndp->ni_cred->cr_uid) { 102424433Sbloom error = EPERM; 102524433Sbloom goto bad; 102624433Sbloom } 102724433Sbloom /* 102810051Ssam * Target must be empty if a directory 102910051Ssam * and have no links to it. 10309167Ssam * Also, insure source and target are 10319167Ssam * compatible (both directories, or both 10329167Ssam * not directories). 10339167Ssam */ 10349167Ssam if ((xp->i_mode&IFMT) == IFDIR) { 103537737Smckusick if (!dirempty(xp, dp->i_number, tndp->ni_cred) || 103637737Smckusick xp->i_nlink > 2) { 103710051Ssam error = ENOTEMPTY; 10389167Ssam goto bad; 10399167Ssam } 10409167Ssam if (!doingdirectory) { 104110051Ssam error = ENOTDIR; 10429167Ssam goto bad; 10439167Ssam } 104437737Smckusick cache_purge(ITOV(dp)); 10459167Ssam } else if (doingdirectory) { 104610051Ssam error = EISDIR; 10479167Ssam goto bad; 10489167Ssam } 104937737Smckusick if (error = dirrewrite(dp, ip, tndp)) 105037737Smckusick goto bad; 105145354Smckusick /* 105245354Smckusick * If the target directory is in the same 105345354Smckusick * directory as the source directory, 105445354Smckusick * decrement the link count on the parent 105545354Smckusick * of the target directory. 105645354Smckusick */ 105745354Smckusick if (doingdirectory && !newparent) { 105845354Smckusick dp->i_nlink--; 105945354Smckusick dp->i_flag |= ICHG; 106045354Smckusick } 106137737Smckusick vput(ITOV(dp)); 10629167Ssam /* 106310051Ssam * Adjust the link count of the target to 106410051Ssam * reflect the dirrewrite above. If this is 106510051Ssam * a directory it is empty and there are 106610051Ssam * no links to it, so we can squash the inode and 106710051Ssam * any space associated with it. We disallowed 106810051Ssam * renaming over top of a directory with links to 106916776Smckusick * it above, as the remaining link would point to 107016776Smckusick * a directory without "." or ".." entries. 10719167Ssam */ 107210051Ssam xp->i_nlink--; 10739167Ssam if (doingdirectory) { 107410051Ssam if (--xp->i_nlink != 0) 107510051Ssam panic("rename: linked directory"); 107639674Smckusick error = itrunc(xp, (u_long)0, IO_SYNC); 107710051Ssam } 10789167Ssam xp->i_flag |= ICHG; 107938398Smckusick iput(xp); 108010246Ssam xp = NULL; 10819167Ssam } 10829167Ssam 10839167Ssam /* 10849167Ssam * 3) Unlink the source. 10859167Ssam */ 1086*46511Smckusick fndp->ni_nameiop &= ~(MODMASK | OPMASK); 1087*46511Smckusick fndp->ni_nameiop |= DELETE | LOCKPARENT | LOCKLEAF; 108837737Smckusick (void)namei(fndp); 108937737Smckusick if (fndp->ni_vp != NULL) { 109037737Smckusick xp = VTOI(fndp->ni_vp); 109137737Smckusick dp = VTOI(fndp->ni_dvp); 109237737Smckusick } else { 109346250Smckusick /* 109446250Smckusick * From name has disappeared. 109546250Smckusick */ 109646250Smckusick if (doingdirectory) 109746250Smckusick panic("rename: lost dir entry"); 109846250Smckusick vrele(ITOV(ip)); 109946250Smckusick return (0); 110037737Smckusick } 11019167Ssam /* 110237737Smckusick * Ensure that the directory entry still exists and has not 110316776Smckusick * changed while the new name has been entered. If the source is 110416776Smckusick * a file then the entry may have been unlinked or renamed. In 110516776Smckusick * either case there is no further work to be done. If the source 110616776Smckusick * is a directory then it cannot have been rmdir'ed; its link 110716776Smckusick * count of three would cause a rmdir to fail with ENOTEMPTY. 110837737Smckusick * The IRENAME flag ensures that it cannot be moved by another 110916776Smckusick * rename. 11109167Ssam */ 111117758Smckusick if (xp != ip) { 111216776Smckusick if (doingdirectory) 111317758Smckusick panic("rename: lost dir entry"); 111416776Smckusick } else { 11159167Ssam /* 111616776Smckusick * If the source is a directory with a 111716776Smckusick * new parent, the link count of the old 111816776Smckusick * parent directory must be decremented 111916776Smckusick * and ".." set to point to the new parent. 11209167Ssam */ 112116776Smckusick if (doingdirectory && newparent) { 11229167Ssam dp->i_nlink--; 11239167Ssam dp->i_flag |= ICHG; 112439597Smckusick error = vn_rdwr(UIO_READ, ITOV(xp), (caddr_t)&dirbuf, 112537737Smckusick sizeof (struct dirtemplate), (off_t)0, 112639597Smckusick UIO_SYSSPACE, IO_NODELOCKED, 112739597Smckusick tndp->ni_cred, (int *)0); 112816776Smckusick if (error == 0) { 112916776Smckusick if (dirbuf.dotdot_namlen != 2 || 113016776Smckusick dirbuf.dotdot_name[0] != '.' || 113116776Smckusick dirbuf.dotdot_name[1] != '.') { 113239610Smckusick dirbad(xp, 12, "rename: mangled dir"); 113316776Smckusick } else { 113416776Smckusick dirbuf.dotdot_ino = newparent; 113539597Smckusick (void) vn_rdwr(UIO_WRITE, ITOV(xp), 113616776Smckusick (caddr_t)&dirbuf, 113716776Smckusick sizeof (struct dirtemplate), 113837740Smckusick (off_t)0, UIO_SYSSPACE, 113939597Smckusick IO_NODELOCKED|IO_SYNC, 114037737Smckusick tndp->ni_cred, (int *)0); 114137737Smckusick cache_purge(ITOV(dp)); 114216776Smckusick } 114316776Smckusick } 11449167Ssam } 114537737Smckusick error = dirremove(fndp); 114637737Smckusick if (!error) { 114716776Smckusick xp->i_nlink--; 114816776Smckusick xp->i_flag |= ICHG; 11499167Ssam } 115016776Smckusick xp->i_flag &= ~IRENAME; 11519167Ssam } 11529167Ssam if (dp) 115337737Smckusick vput(ITOV(dp)); 115416776Smckusick if (xp) 115537737Smckusick vput(ITOV(xp)); 115637737Smckusick vrele(ITOV(ip)); 115737737Smckusick return (error); 11589167Ssam 11599167Ssam bad: 11609167Ssam if (xp) 116137737Smckusick vput(ITOV(xp)); 116237737Smckusick vput(ITOV(dp)); 11639167Ssam out: 11649167Ssam ip->i_nlink--; 11659167Ssam ip->i_flag |= ICHG; 116637737Smckusick vrele(ITOV(ip)); 116737737Smckusick return (error); 11687701Ssam } 11697701Ssam 11707535Sroot /* 117112756Ssam * A virgin directory (no blushing please). 117212756Ssam */ 117312756Ssam struct dirtemplate mastertemplate = { 117412756Ssam 0, 12, 1, ".", 117512756Ssam 0, DIRBLKSIZ - 12, 2, ".." 117612756Ssam }; 117712756Ssam 117812756Ssam /* 117912756Ssam * Mkdir system call 118012756Ssam */ 118137737Smckusick ufs_mkdir(ndp, vap) 118237737Smckusick struct nameidata *ndp; 118337737Smckusick struct vattr *vap; 118412756Ssam { 118512756Ssam register struct inode *ip, *dp; 118637737Smckusick struct inode *tip; 118737737Smckusick struct vnode *dvp; 118812756Ssam struct dirtemplate dirtemplate; 118937737Smckusick int error; 119037737Smckusick int dmode; 119112756Ssam 119237737Smckusick dvp = ndp->ni_dvp; 119337737Smckusick dp = VTOI(dvp); 119446251Smckusick if ((unsigned short)dp->i_nlink >= LINK_MAX) { 119546251Smckusick iput(dp); 119646251Smckusick return (EMLINK); 119746251Smckusick } 119837737Smckusick dmode = vap->va_mode&0777; 119937737Smckusick dmode |= IFDIR; 120012756Ssam /* 120112756Ssam * Must simulate part of maknode here 120212756Ssam * in order to acquire the inode, but 120312756Ssam * not have it entered in the parent 120412756Ssam * directory. The entry is made later 120512756Ssam * after writing "." and ".." entries out. 120612756Ssam */ 120741312Smckusick if (error = ialloc(dp, dirpref(dp->i_fs), dmode, ndp->ni_cred, &tip)) { 120812756Ssam iput(dp); 120937737Smckusick return (error); 121012756Ssam } 121137737Smckusick ip = tip; 121241312Smckusick ip->i_uid = ndp->ni_cred->cr_uid; 121341312Smckusick ip->i_gid = dp->i_gid; 121412756Ssam #ifdef QUOTA 121541312Smckusick if ((error = getinoquota(ip)) || 121641312Smckusick (error = chkiq(ip, 1, ndp->ni_cred, 0))) { 121741312Smckusick ifree(ip, ip->i_number, dmode); 121841312Smckusick iput(ip); 121941312Smckusick iput(dp); 122041312Smckusick return (error); 122141312Smckusick } 122212756Ssam #endif 122312756Ssam ip->i_flag |= IACC|IUPD|ICHG; 122437737Smckusick ip->i_mode = dmode; 122537737Smckusick ITOV(ip)->v_type = VDIR; /* Rest init'd in iget() */ 122612756Ssam ip->i_nlink = 2; 122737737Smckusick error = iupdat(ip, &time, &time, 1); 122812756Ssam 122912756Ssam /* 123012756Ssam * Bump link count in parent directory 123112756Ssam * to reflect work done below. Should 123212756Ssam * be done before reference is created 123312756Ssam * so reparation is possible if we crash. 123412756Ssam */ 123512756Ssam dp->i_nlink++; 123612756Ssam dp->i_flag |= ICHG; 123737737Smckusick error = iupdat(dp, &time, &time, 1); 123812756Ssam 123912756Ssam /* 124012756Ssam * Initialize directory with "." 124112756Ssam * and ".." from static template. 124212756Ssam */ 124312756Ssam dirtemplate = mastertemplate; 124412756Ssam dirtemplate.dot_ino = ip->i_number; 124512756Ssam dirtemplate.dotdot_ino = dp->i_number; 124639597Smckusick error = vn_rdwr(UIO_WRITE, ITOV(ip), (caddr_t)&dirtemplate, 124737737Smckusick sizeof (dirtemplate), (off_t)0, UIO_SYSSPACE, 124839597Smckusick IO_NODELOCKED|IO_SYNC, ndp->ni_cred, (int *)0); 124937737Smckusick if (error) { 125012756Ssam dp->i_nlink--; 125112756Ssam dp->i_flag |= ICHG; 125212756Ssam goto bad; 125312756Ssam } 125443288Smckusick if (DIRBLKSIZ > dp->i_fs->fs_fsize) { 125537737Smckusick panic("mkdir: blksize"); /* XXX - should grow w/balloc() */ 125643288Smckusick } else { 125718103Smckusick ip->i_size = DIRBLKSIZ; 125843288Smckusick ip->i_flag |= ICHG; 125943288Smckusick } 126012756Ssam /* 126112756Ssam * Directory all set up, now 126212756Ssam * install the entry for it in 126312756Ssam * the parent directory. 126412756Ssam */ 126537737Smckusick error = direnter(ip, ndp); 126612756Ssam dp = NULL; 126737737Smckusick if (error) { 1268*46511Smckusick ndp->ni_nameiop &= ~(MODMASK | OPMASK); 1269*46511Smckusick ndp->ni_nameiop |= LOOKUP | NOCACHE; 127037737Smckusick error = namei(ndp); 127137737Smckusick if (!error) { 127237737Smckusick dp = VTOI(ndp->ni_vp); 127312756Ssam dp->i_nlink--; 127412756Ssam dp->i_flag |= ICHG; 127512756Ssam } 127612756Ssam } 127712756Ssam bad: 127812756Ssam /* 127912756Ssam * No need to do an explicit itrunc here, 128037737Smckusick * vrele will do this for us because we set 128112756Ssam * the link count to 0. 128212756Ssam */ 128337737Smckusick if (error) { 128412756Ssam ip->i_nlink = 0; 128512756Ssam ip->i_flag |= ICHG; 128638144Smckusick iput(ip); 128738144Smckusick } else 128838144Smckusick ndp->ni_vp = ITOV(ip); 128912756Ssam if (dp) 129012756Ssam iput(dp); 129137737Smckusick return (error); 129212756Ssam } 129312756Ssam 129412756Ssam /* 129512756Ssam * Rmdir system call. 129612756Ssam */ 129737737Smckusick ufs_rmdir(ndp) 129837737Smckusick register struct nameidata *ndp; 129912756Ssam { 130012756Ssam register struct inode *ip, *dp; 130137737Smckusick int error = 0; 130212756Ssam 130337737Smckusick ip = VTOI(ndp->ni_vp); 130437737Smckusick dp = VTOI(ndp->ni_dvp); 130512756Ssam /* 130612756Ssam * No rmdir "." please. 130712756Ssam */ 130812756Ssam if (dp == ip) { 130937737Smckusick vrele(ITOV(dp)); 131012756Ssam iput(ip); 131137737Smckusick return (EINVAL); 131212756Ssam } 131312756Ssam /* 131412756Ssam * Verify the directory is empty (and valid). 131512756Ssam * (Rmdir ".." won't be valid since 131612756Ssam * ".." will contain a reference to 131712756Ssam * the current directory and thus be 131812756Ssam * non-empty.) 131912756Ssam */ 132037737Smckusick if (ip->i_nlink != 2 || !dirempty(ip, dp->i_number, ndp->ni_cred)) { 132137737Smckusick error = ENOTEMPTY; 132212756Ssam goto out; 132312756Ssam } 132412756Ssam /* 132512756Ssam * Delete reference to directory before purging 132612756Ssam * inode. If we crash in between, the directory 132712756Ssam * will be reattached to lost+found, 132812756Ssam */ 132937737Smckusick if (error = dirremove(ndp)) 133012756Ssam goto out; 133112756Ssam dp->i_nlink--; 133212756Ssam dp->i_flag |= ICHG; 133337737Smckusick cache_purge(ITOV(dp)); 133412756Ssam iput(dp); 133537737Smckusick ndp->ni_dvp = NULL; 133612756Ssam /* 133712756Ssam * Truncate inode. The only stuff left 133812756Ssam * in the directory is "." and "..". The 133912756Ssam * "." reference is inconsequential since 134012756Ssam * we're quashing it. The ".." reference 134112756Ssam * has already been adjusted above. We've 134212756Ssam * removed the "." reference and the reference 134312756Ssam * in the parent directory, but there may be 134412756Ssam * other hard links so decrement by 2 and 134512756Ssam * worry about them later. 134612756Ssam */ 134712756Ssam ip->i_nlink -= 2; 134839674Smckusick error = itrunc(ip, (u_long)0, IO_SYNC); 134937737Smckusick cache_purge(ITOV(ip)); 135012756Ssam out: 135137737Smckusick if (ndp->ni_dvp) 135212756Ssam iput(dp); 135312756Ssam iput(ip); 135437737Smckusick return (error); 135512756Ssam } 135612756Ssam 135737737Smckusick /* 135837737Smckusick * symlink -- make a symbolic link 135937737Smckusick */ 136037737Smckusick ufs_symlink(ndp, vap, target) 136137737Smckusick struct nameidata *ndp; 136237737Smckusick struct vattr *vap; 136337737Smckusick char *target; 136412756Ssam { 136537737Smckusick struct inode *ip; 136637737Smckusick int error; 136712756Ssam 136837737Smckusick error = maknode(IFLNK | vap->va_mode, ndp, &ip); 136937737Smckusick if (error) 137037737Smckusick return (error); 137139597Smckusick error = vn_rdwr(UIO_WRITE, ITOV(ip), target, strlen(target), (off_t)0, 137239597Smckusick UIO_SYSSPACE, IO_NODELOCKED, ndp->ni_cred, (int *)0); 137337737Smckusick iput(ip); 137437737Smckusick return (error); 137537737Smckusick } 137637737Smckusick 137737737Smckusick /* 137837737Smckusick * Vnode op for read and write 137937737Smckusick */ 138040345Smckusick ufs_readdir(vp, uio, cred, eofflagp) 138137737Smckusick struct vnode *vp; 138237737Smckusick register struct uio *uio; 138337737Smckusick struct ucred *cred; 138440345Smckusick int *eofflagp; 138537737Smckusick { 138639597Smckusick int count, lost, error; 138737737Smckusick 138837737Smckusick count = uio->uio_resid; 138937737Smckusick count &= ~(DIRBLKSIZ - 1); 139039597Smckusick lost = uio->uio_resid - count; 139139597Smckusick if (count < DIRBLKSIZ || (uio->uio_offset & (DIRBLKSIZ -1))) 139237737Smckusick return (EINVAL); 139337737Smckusick uio->uio_resid = count; 139437737Smckusick uio->uio_iov->iov_len = count; 139539597Smckusick error = ufs_read(vp, uio, 0, cred); 139639597Smckusick uio->uio_resid += lost; 139740345Smckusick if ((VTOI(vp)->i_size - uio->uio_offset) <= 0) 139840345Smckusick *eofflagp = 1; 139940345Smckusick else 140040345Smckusick *eofflagp = 0; 140137737Smckusick return (error); 140237737Smckusick } 140337737Smckusick 140437737Smckusick /* 140537737Smckusick * Return target name of a symbolic link 140637737Smckusick */ 140737737Smckusick ufs_readlink(vp, uiop, cred) 140837737Smckusick struct vnode *vp; 140937737Smckusick struct uio *uiop; 141037737Smckusick struct ucred *cred; 141137737Smckusick { 141237737Smckusick 141339597Smckusick return (ufs_read(vp, uiop, 0, cred)); 141437737Smckusick } 141537737Smckusick 141637737Smckusick /* 141737737Smckusick * Ufs abort op, called after namei() when a CREATE/DELETE isn't actually 141842466Smckusick * done. Nothing to do at the moment. 141937737Smckusick */ 142042466Smckusick /* ARGSUSED */ 142137737Smckusick ufs_abortop(ndp) 142242466Smckusick struct nameidata *ndp; 142337737Smckusick { 142437737Smckusick 142542466Smckusick return (0); 142612756Ssam } 142712756Ssam 142839909Smckusick /* 142939909Smckusick * Lock an inode. 143039909Smckusick */ 143137737Smckusick ufs_lock(vp) 143237737Smckusick struct vnode *vp; 143337737Smckusick { 143437737Smckusick register struct inode *ip = VTOI(vp); 143537737Smckusick 143637737Smckusick ILOCK(ip); 143737737Smckusick return (0); 143837737Smckusick } 143937737Smckusick 144039909Smckusick /* 144139909Smckusick * Unlock an inode. 144239909Smckusick */ 144337737Smckusick ufs_unlock(vp) 144437737Smckusick struct vnode *vp; 144537737Smckusick { 144637737Smckusick register struct inode *ip = VTOI(vp); 144737737Smckusick 144837737Smckusick if (!(ip->i_flag & ILOCKED)) 144937737Smckusick panic("ufs_unlock NOT LOCKED"); 145037737Smckusick IUNLOCK(ip); 145137737Smckusick return (0); 145237737Smckusick } 145337737Smckusick 145412756Ssam /* 145539909Smckusick * Check for a locked inode. 145639909Smckusick */ 145739909Smckusick ufs_islocked(vp) 145839909Smckusick struct vnode *vp; 145939909Smckusick { 146039909Smckusick 146139909Smckusick if (VTOI(vp)->i_flag & ILOCKED) 146239909Smckusick return (1); 146339909Smckusick return (0); 146439909Smckusick } 146539909Smckusick 146639909Smckusick /* 146737737Smckusick * Get access to bmap 146812756Ssam */ 146937737Smckusick ufs_bmap(vp, bn, vpp, bnp) 147037737Smckusick struct vnode *vp; 147137737Smckusick daddr_t bn; 147237737Smckusick struct vnode **vpp; 147337737Smckusick daddr_t *bnp; 147412756Ssam { 147537737Smckusick struct inode *ip = VTOI(vp); 147612756Ssam 147737737Smckusick if (vpp != NULL) 147837737Smckusick *vpp = ip->i_devvp; 147937737Smckusick if (bnp == NULL) 148037737Smckusick return (0); 148141538Smckusick return (bmap(ip, bn, bnp)); 148212756Ssam } 148337737Smckusick 148437737Smckusick /* 148541538Smckusick * Calculate the logical to physical mapping if not done already, 148641538Smckusick * then call the device strategy routine. 148737737Smckusick */ 148841538Smckusick int checkoverlap = 0; 148939674Smckusick 149037737Smckusick ufs_strategy(bp) 149137737Smckusick register struct buf *bp; 149237737Smckusick { 149339674Smckusick register struct inode *ip = VTOI(bp->b_vp); 149439674Smckusick struct vnode *vp; 149539674Smckusick int error; 149639674Smckusick 149739674Smckusick if (bp->b_vp->v_type == VBLK || bp->b_vp->v_type == VCHR) 149839674Smckusick panic("ufs_strategy: spec"); 149939674Smckusick if (bp->b_blkno == bp->b_lblkno) { 150039674Smckusick if (error = bmap(ip, bp->b_lblkno, &bp->b_blkno)) 150139674Smckusick return (error); 150239896Smckusick if ((long)bp->b_blkno == -1) 150339674Smckusick clrbuf(bp); 150439674Smckusick } 150539896Smckusick if ((long)bp->b_blkno == -1) { 150639896Smckusick biodone(bp); 150739674Smckusick return (0); 150839896Smckusick } 150941538Smckusick #ifdef DIAGNOSTIC 151039674Smckusick if (checkoverlap) { 151141538Smckusick register struct buf *ep; 151241538Smckusick struct buf *ebp; 151341538Smckusick daddr_t start, last; 151441538Smckusick 151539674Smckusick ebp = &buf[nbuf]; 151639674Smckusick start = bp->b_blkno; 151739674Smckusick last = start + btodb(bp->b_bcount) - 1; 151839674Smckusick for (ep = buf; ep < ebp; ep++) { 151939674Smckusick if (ep == bp || (ep->b_flags & B_INVAL) || 152041396Smckusick ep->b_vp == NULLVP) 152139674Smckusick continue; 152239674Smckusick if (VOP_BMAP(ep->b_vp, (daddr_t)0, &vp, (daddr_t)0)) 152339674Smckusick continue; 152439674Smckusick if (vp != ip->i_devvp) 152539674Smckusick continue; 152639674Smckusick /* look for overlap */ 152739674Smckusick if (ep->b_bcount == 0 || ep->b_blkno > last || 152839674Smckusick ep->b_blkno + btodb(ep->b_bcount) <= start) 152939674Smckusick continue; 153039896Smckusick vprint("Disk overlap", vp); 153139896Smckusick printf("\tstart %d, end %d overlap start %d, end %d\n", 153239896Smckusick start, last, ep->b_blkno, 153339896Smckusick ep->b_blkno + btodb(ep->b_bcount) - 1); 153441538Smckusick panic("Disk buffer overlap"); 153539674Smckusick } 153639674Smckusick } 153741538Smckusick #endif /* DIAGNOSTIC */ 153839674Smckusick vp = ip->i_devvp; 153939674Smckusick bp->b_dev = vp->v_rdev; 154039674Smckusick (*(vp->v_op->vn_strategy))(bp); 154137737Smckusick return (0); 154237737Smckusick } 154337737Smckusick 154437737Smckusick /* 154539674Smckusick * Print out the contents of an inode. 154639674Smckusick */ 154739674Smckusick ufs_print(vp) 154839674Smckusick struct vnode *vp; 154939674Smckusick { 155039674Smckusick register struct inode *ip = VTOI(vp); 155139674Smckusick 155240293Smckusick printf("tag VT_UFS, ino %d, on dev %d, %d", ip->i_number, 155340293Smckusick major(ip->i_dev), minor(ip->i_dev)); 155440293Smckusick #ifdef FIFO 155540293Smckusick if (vp->v_type == VFIFO) 155640293Smckusick fifo_printinfo(vp); 155740293Smckusick #endif /* FIFO */ 155840293Smckusick printf("%s\n", (ip->i_flag & ILOCKED) ? " (LOCKED)" : ""); 155939900Smckusick if (ip->i_spare0 == 0) 156039900Smckusick return; 156139900Smckusick printf("\towner pid %d", ip->i_spare0); 156239900Smckusick if (ip->i_spare1) 156339900Smckusick printf(" waiting pid %d", ip->i_spare1); 156439900Smckusick printf("\n"); 156539674Smckusick } 156639674Smckusick 156739674Smckusick /* 156839628Smckusick * Read wrapper for special devices. 156939628Smckusick */ 157039628Smckusick ufsspec_read(vp, uio, ioflag, cred) 157139628Smckusick struct vnode *vp; 157239628Smckusick struct uio *uio; 157339628Smckusick int ioflag; 157439628Smckusick struct ucred *cred; 157539628Smckusick { 157639628Smckusick 157739628Smckusick /* 157839628Smckusick * Set access flag. 157939628Smckusick */ 158039628Smckusick VTOI(vp)->i_flag |= IACC; 158139628Smckusick return (spec_read(vp, uio, ioflag, cred)); 158239628Smckusick } 158339628Smckusick 158439628Smckusick /* 158539628Smckusick * Write wrapper for special devices. 158639628Smckusick */ 158739628Smckusick ufsspec_write(vp, uio, ioflag, cred) 158839628Smckusick struct vnode *vp; 158939628Smckusick struct uio *uio; 159039628Smckusick int ioflag; 159139628Smckusick struct ucred *cred; 159239628Smckusick { 159339628Smckusick 159439628Smckusick /* 159539628Smckusick * Set update and change flags. 159639628Smckusick */ 159739628Smckusick VTOI(vp)->i_flag |= IUPD|ICHG; 159839628Smckusick return (spec_write(vp, uio, ioflag, cred)); 159939628Smckusick } 160039628Smckusick 160139628Smckusick /* 160239628Smckusick * Close wrapper for special devices. 160339628Smckusick * 160439628Smckusick * Update the times on the inode then do device close. 160539628Smckusick */ 160639628Smckusick ufsspec_close(vp, fflag, cred) 160739628Smckusick struct vnode *vp; 160839628Smckusick int fflag; 160939628Smckusick struct ucred *cred; 161039628Smckusick { 161139628Smckusick register struct inode *ip = VTOI(vp); 161239628Smckusick 161339815Smckusick if (vp->v_usecount > 1 && !(ip->i_flag & ILOCKED)) 161439628Smckusick ITIMES(ip, &time, &time); 161539628Smckusick return (spec_close(vp, fflag, cred)); 161639628Smckusick } 161739628Smckusick 161840290Smckusick #ifdef FIFO 161939628Smckusick /* 162040290Smckusick * Read wrapper for fifo's 162140290Smckusick */ 162240290Smckusick ufsfifo_read(vp, uio, ioflag, cred) 162340290Smckusick struct vnode *vp; 162440290Smckusick struct uio *uio; 162540290Smckusick int ioflag; 162640290Smckusick struct ucred *cred; 162740290Smckusick { 162840290Smckusick 162940290Smckusick /* 163040290Smckusick * Set access flag. 163140290Smckusick */ 163240290Smckusick VTOI(vp)->i_flag |= IACC; 163340290Smckusick return (fifo_read(vp, uio, ioflag, cred)); 163440290Smckusick } 163540290Smckusick 163640290Smckusick /* 163740290Smckusick * Write wrapper for fifo's. 163840290Smckusick */ 163940290Smckusick ufsfifo_write(vp, uio, ioflag, cred) 164040290Smckusick struct vnode *vp; 164140290Smckusick struct uio *uio; 164240290Smckusick int ioflag; 164340290Smckusick struct ucred *cred; 164440290Smckusick { 164540290Smckusick 164640290Smckusick /* 164740290Smckusick * Set update and change flags. 164840290Smckusick */ 164940290Smckusick VTOI(vp)->i_flag |= IUPD|ICHG; 165040290Smckusick return (fifo_write(vp, uio, ioflag, cred)); 165140290Smckusick } 165240290Smckusick 165340290Smckusick /* 165440290Smckusick * Close wrapper for fifo's. 165540290Smckusick * 165640290Smckusick * Update the times on the inode then do device close. 165740290Smckusick */ 165840290Smckusick ufsfifo_close(vp, fflag, cred) 165940290Smckusick struct vnode *vp; 166040290Smckusick int fflag; 166140290Smckusick struct ucred *cred; 166240290Smckusick { 166340290Smckusick register struct inode *ip = VTOI(vp); 166440290Smckusick 166540290Smckusick if (vp->v_usecount > 1 && !(ip->i_flag & ILOCKED)) 166640290Smckusick ITIMES(ip, &time, &time); 166740290Smckusick return (fifo_close(vp, fflag, cred)); 166840290Smckusick } 166940290Smckusick #endif /* FIFO */ 167040290Smckusick 167140290Smckusick /* 167237737Smckusick * Make a new file. 167337737Smckusick */ 167437737Smckusick maknode(mode, ndp, ipp) 167537737Smckusick int mode; 167637737Smckusick register struct nameidata *ndp; 167737737Smckusick struct inode **ipp; 167837737Smckusick { 167937737Smckusick register struct inode *ip; 168037737Smckusick struct inode *tip; 168137737Smckusick register struct inode *pdir = VTOI(ndp->ni_dvp); 168237737Smckusick ino_t ipref; 168337737Smckusick int error; 168437737Smckusick 168537737Smckusick *ipp = 0; 168641312Smckusick if ((mode & IFMT) == 0) 168741312Smckusick mode |= IFREG; 168837737Smckusick if ((mode & IFMT) == IFDIR) 168937737Smckusick ipref = dirpref(pdir->i_fs); 169037737Smckusick else 169137737Smckusick ipref = pdir->i_number; 169241312Smckusick if (error = ialloc(pdir, ipref, mode, ndp->ni_cred, &tip)) { 169337737Smckusick iput(pdir); 169437737Smckusick return (error); 169537737Smckusick } 169637737Smckusick ip = tip; 169741312Smckusick ip->i_uid = ndp->ni_cred->cr_uid; 169841312Smckusick ip->i_gid = pdir->i_gid; 169937737Smckusick #ifdef QUOTA 170041312Smckusick if ((error = getinoquota(ip)) || 170141312Smckusick (error = chkiq(ip, 1, ndp->ni_cred, 0))) { 170241312Smckusick ifree(ip, ip->i_number, mode); 170341312Smckusick iput(ip); 170441312Smckusick iput(pdir); 170541312Smckusick return (error); 170641312Smckusick } 170737737Smckusick #endif 170837737Smckusick ip->i_flag |= IACC|IUPD|ICHG; 170937737Smckusick ip->i_mode = mode; 171037737Smckusick ITOV(ip)->v_type = IFTOVT(mode); /* Rest init'd in iget() */ 171137737Smckusick ip->i_nlink = 1; 171237737Smckusick if ((ip->i_mode & ISGID) && !groupmember(ip->i_gid, ndp->ni_cred) && 171337737Smckusick suser(ndp->ni_cred, NULL)) 171437737Smckusick ip->i_mode &= ~ISGID; 171537737Smckusick 171637737Smckusick /* 171737737Smckusick * Make sure inode goes to disk before directory entry. 171837737Smckusick */ 171941312Smckusick if (error = iupdat(ip, &time, &time, 1)) 172041312Smckusick goto bad; 172141312Smckusick if (error = direnter(ip, ndp)) { 172241312Smckusick pdir = NULL; 172341312Smckusick goto bad; 172437737Smckusick } 172537737Smckusick *ipp = ip; 172637737Smckusick return (0); 172741312Smckusick 172841312Smckusick bad: 172941312Smckusick /* 173041312Smckusick * Write error occurred trying to update the inode 173141312Smckusick * or the directory so must deallocate the inode. 173241312Smckusick */ 173341312Smckusick if (pdir) 173441312Smckusick iput(pdir); 173541312Smckusick ip->i_nlink = 0; 173641312Smckusick ip->i_flag |= ICHG; 173741312Smckusick iput(ip); 173841312Smckusick return (error); 173937737Smckusick } 174046207Smckusick 174146207Smckusick /* 174246207Smckusick * Advisory record locking support 174346207Smckusick */ 174446207Smckusick ufs_advlock(vp, id, op, fl, flags) 174546207Smckusick struct vnode *vp; 174646207Smckusick caddr_t id; 174746207Smckusick int op; 174846207Smckusick register struct flock *fl; 174946207Smckusick int flags; 175046207Smckusick { 175146207Smckusick register struct inode *ip = VTOI(vp); 175246207Smckusick register struct lockf *lock; 175346207Smckusick off_t start, end; 175446207Smckusick int error; 175546207Smckusick 175646207Smckusick /* 175746207Smckusick * Avoid the common case of unlocking when inode has no locks. 175846207Smckusick */ 175946207Smckusick if (ip->i_lockf == (struct lockf *)0) { 176046207Smckusick if (op != F_SETLK) { 176146207Smckusick fl->l_type = F_UNLCK; 176246207Smckusick return (0); 176346207Smckusick } 176446207Smckusick } 176546207Smckusick /* 176646207Smckusick * Convert the flock structure into a start and end. 176746207Smckusick */ 176846207Smckusick switch (fl->l_whence) { 176946207Smckusick 177046207Smckusick case SEEK_SET: 177146207Smckusick case SEEK_CUR: 177246207Smckusick /* 177346207Smckusick * Caller is responsible for adding any necessary offset 177446207Smckusick * when SEEK_CUR is used. 177546207Smckusick */ 177646207Smckusick start = fl->l_start; 177746207Smckusick break; 177846207Smckusick 177946207Smckusick case SEEK_END: 178046207Smckusick start = ip->i_size + fl->l_start; 178146207Smckusick break; 178246207Smckusick 178346207Smckusick default: 178446207Smckusick return (EINVAL); 178546207Smckusick } 178646207Smckusick if (start < 0) 178746207Smckusick return (EINVAL); 178846207Smckusick if (fl->l_len == 0) 178946207Smckusick end = -1; 179046207Smckusick else 179146507Smckusick end = start + fl->l_len - 1; 179246207Smckusick /* 179346207Smckusick * Create the lockf structure 179446207Smckusick */ 179546207Smckusick MALLOC(lock, struct lockf *, sizeof *lock, M_LOCKF, M_WAITOK); 179646207Smckusick lock->lf_start = start; 179746207Smckusick lock->lf_end = end; 179846207Smckusick lock->lf_id = id; 179946207Smckusick lock->lf_inode = ip; 180046207Smckusick lock->lf_type = fl->l_type; 180146207Smckusick lock->lf_next = (struct lockf *)0; 180246207Smckusick lock->lf_block = (struct lockf *)0; 180346207Smckusick lock->lf_flags = flags; 180446207Smckusick /* 180546207Smckusick * Do the requested operation. 180646207Smckusick */ 180746207Smckusick switch(op) { 180846207Smckusick case F_SETLK: 180946207Smckusick return (ufs_setlock(lock)); 181046207Smckusick 181146207Smckusick case F_UNLCK: 181246207Smckusick return (ufs_advunlock(lock)); 181346207Smckusick 181446207Smckusick case F_GETLK: 181546207Smckusick return (ufs_advgetlock(lock, fl)); 181646207Smckusick 181746207Smckusick default: 181846207Smckusick free(lock, M_LOCKF); 181946207Smckusick return (EINVAL); 182046207Smckusick } 182146207Smckusick /* NOTREACHED */ 182246207Smckusick } 182346207Smckusick 182446207Smckusick /* 182546207Smckusick * This variable controls the maximum number of processes that will 182646207Smckusick * be checked in doing deadlock detection. 182746207Smckusick */ 182846207Smckusick int maxlockdepth = MAXDEPTH; 182946207Smckusick 183046207Smckusick /* 183146207Smckusick * Set a byte-range lock. 183246207Smckusick */ 183346207Smckusick ufs_setlock(lock) 183446207Smckusick register struct lockf *lock; 183546207Smckusick { 183646207Smckusick register struct inode *ip = lock->lf_inode; 183746207Smckusick register struct lockf *block; 183846207Smckusick static char lockstr[] = "lockf"; 183946207Smckusick int priority, error; 184046207Smckusick 184146207Smckusick #ifdef LOCKF_DEBUG 184246207Smckusick if (lockf_debug & 4) 184346207Smckusick lf_print("ufs_setlock", lock); 184446207Smckusick #endif /* LOCKF_DEBUG */ 184546207Smckusick 184646207Smckusick /* 184746207Smckusick * Set the priority 184846207Smckusick */ 184946207Smckusick priority = PLOCK; 185046207Smckusick if ((lock->lf_type & F_WRLCK) == 0) 185146207Smckusick priority += 4; 185246207Smckusick priority |= PCATCH; 185346207Smckusick /* 185446207Smckusick * Scan lock list for this file looking for locks that would block us. 185546207Smckusick */ 185646207Smckusick while (block = lf_getblock(lock)) { 185746207Smckusick /* 185846207Smckusick * Free the structure and return if nonblocking. 185946207Smckusick */ 186046207Smckusick if ((lock->lf_flags & F_WAIT) == 0) { 186146207Smckusick free(lock, M_LOCKF); 186246207Smckusick return (EAGAIN); 186346207Smckusick } 186446207Smckusick /* 186546207Smckusick * We are blocked. Since flock style locks cover 186646207Smckusick * the whole file, there is no chance for deadlock. 186746207Smckusick * For byte-range locks we must check for deadlock. 186846207Smckusick * 186946207Smckusick * Deadlock detection is done by looking through the 187046207Smckusick * wait channels to see if there are any cycles that 187146207Smckusick * involve us. MAXDEPTH is set just to make sure we 187246207Smckusick * do not go off into neverland. 187346207Smckusick */ 187446207Smckusick if ((lock->lf_flags & F_POSIX) && 187546207Smckusick (block->lf_flags & F_POSIX)) { 187646207Smckusick register struct proc *wproc; 187746207Smckusick register struct lockf *waitblock; 187846207Smckusick int i = 0; 187946207Smckusick 188046207Smckusick /* The block is waiting on something */ 188146207Smckusick wproc = (struct proc *)block->lf_id; 188246207Smckusick while (wproc->p_wchan && 188346207Smckusick (wproc->p_wmesg == lockstr) && 188446207Smckusick (i++ < maxlockdepth)) { 188546207Smckusick waitblock = (struct lockf *)wproc->p_wchan; 188646207Smckusick /* Get the owner of the blocking lock */ 188746207Smckusick waitblock = waitblock->lf_next; 188846207Smckusick if ((waitblock->lf_flags & F_POSIX) == 0) 188946207Smckusick break; 189046207Smckusick wproc = (struct proc *)waitblock->lf_id; 189146207Smckusick if (wproc == (struct proc *)lock->lf_id) { 189246207Smckusick free(lock, M_LOCKF); 189346207Smckusick return (EDEADLK); 189446207Smckusick } 189546207Smckusick } 189646207Smckusick } 189746207Smckusick /* 189846207Smckusick * Add our lock to the blocked 189946207Smckusick * list and sleep until we're free. 190046207Smckusick */ 190146507Smckusick #ifdef LOCKF_DEBUG 190246207Smckusick if (lockf_debug & 4) 190346207Smckusick lf_print("ufs_advlock: blocking on", block); 190446207Smckusick #endif /* LOCKF_DEBUG */ 190546207Smckusick /* 190646207Smckusick * Remember who blocked us (for deadlock detection) 190746207Smckusick */ 190846207Smckusick lock->lf_next = block; 190946207Smckusick lf_addblock(block, lock); 191046207Smckusick if (error = tsleep((caddr_t *)lock, priority, lockstr, 0)) { 191146207Smckusick free(lock, M_LOCKF); 191246207Smckusick return (error); 191346207Smckusick } 191446207Smckusick } 191546207Smckusick /* 191646207Smckusick * No blocks!! Add the lock. Note that addlock will 191746207Smckusick * downgrade or upgrade any overlapping locks this 191846207Smckusick * process already owns. 191946207Smckusick */ 192046507Smckusick lf_addlock(lock); 192146507Smckusick #ifdef LOCKF_DEBUG 192246507Smckusick if (lockf_debug & 4) { 192346207Smckusick lf_print("ufs_advlock: got the lock", lock); 192446507Smckusick lf_printlist(lock); 192546507Smckusick } 192646207Smckusick #endif /* LOCKF_DEBUG */ 192746207Smckusick return (0); 192846207Smckusick } 192946207Smckusick 193046207Smckusick /* 193146207Smckusick * Remove a byte-range lock on an inode. 193246207Smckusick */ 193346207Smckusick ufs_advunlock(lock) 193446207Smckusick struct lockf *lock; 193546207Smckusick { 193646207Smckusick struct lockf *blocklist; 193746207Smckusick 193846207Smckusick if (lock->lf_inode->i_lockf == (struct lockf *)0) 193946207Smckusick return (0); 194046507Smckusick #ifdef LOCKF_DEBUG 194146207Smckusick if (lockf_debug & 4) 194246207Smckusick lf_print("ufs_advunlock", lock); 194346207Smckusick #endif /* LOCKF_DEBUG */ 194446207Smckusick /* 194546207Smckusick * Generally, find the lock (or an overlap to that lock) 194646207Smckusick * and remove it (or shrink it), then wakeup anyone we can. 194746207Smckusick */ 194846207Smckusick blocklist = lf_remove(lock); 194946507Smckusick #ifdef LOCKF_DEBUG 195046507Smckusick lf_printlist(lock); 195146507Smckusick #endif /* LOCKF_DEBUG */ 195246207Smckusick FREE(lock, M_LOCKF); 195346207Smckusick lf_wakelock(blocklist); 195446207Smckusick return (0); 195546207Smckusick } 195646207Smckusick 195746207Smckusick /* 195846207Smckusick * Return the blocking pid 195946207Smckusick */ 196046207Smckusick ufs_advgetlock(lock, fl) 196146207Smckusick register struct lockf *lock; 196246207Smckusick register struct flock *fl; 196346207Smckusick { 196446207Smckusick register struct lockf *block; 196546207Smckusick off_t start, end; 196646207Smckusick 196746507Smckusick #ifdef LOCKF_DEBUG 196846207Smckusick if (lockf_debug & 4) 196946207Smckusick lf_print("ufs_advgetlock", lock); 197046207Smckusick #endif /* LOCKF_DEBUG */ 197146207Smckusick 197246207Smckusick if (block = lf_getblock(lock)) { 197346207Smckusick fl->l_type = block->lf_type; 197446207Smckusick fl->l_whence = SEEK_SET; 197546207Smckusick fl->l_start = block->lf_start; 197646207Smckusick if (block->lf_end == -1) 197746207Smckusick fl->l_len = 0; 197846207Smckusick else 197946507Smckusick fl->l_len = block->lf_end - block->lf_start + 1; 198046207Smckusick if (block->lf_flags & F_POSIX) 198146207Smckusick fl->l_pid = ((struct proc *)(block->lf_id))->p_pid; 198246207Smckusick else 198346207Smckusick fl->l_pid = -1; 198446207Smckusick } 198546207Smckusick FREE(lock, M_LOCKF); 198646207Smckusick return (0); 198746207Smckusick } 1988